1 /*
2  *	AX.25 release 038
3  *
4  *	This code REQUIRES 2.1.15 or higher/ NET3.038
5  *
6  *	This module:
7  *		This module is free software; you can redistribute it and/or
8  *		modify it under the terms of the GNU General Public License
9  *		as published by the Free Software Foundation; either version
10  *		2 of the License, or (at your option) any later version.
11  *
12  *	History
13  *	AX.25 006	Alan(GW4PTS)		Nearly died of shock - it's working 8-)
14  *	AX.25 007	Alan(GW4PTS)		Removed the silliest bugs
15  *	AX.25 008	Alan(GW4PTS)		Cleaned up, fixed a few state machine problems, added callbacks
16  *	AX.25 009	Alan(GW4PTS)		Emergency patch kit to fix memory corruption
17  * 	AX.25 010	Alan(GW4PTS)		Added RAW sockets/Digipeat.
18  *	AX.25 011	Alan(GW4PTS)		RAW socket and datagram fixes (thanks) - Raw sendto now gets PID right
19  *						datagram sendto uses correct target address.
20  *	AX.25 012	Alan(GW4PTS)		Correct incoming connection handling, send DM to failed connects.
21  *						Use skb->data not skb+1. Support sk->priority correctly.
22  *						Correct receive on SOCK_DGRAM.
23  *	AX.25 013	Alan(GW4PTS)		Send DM to all unknown frames, missing initialiser fixed
24  *						Leave spare SSID bits set (DAMA etc) - thanks for bug report,
25  *						removed device registration (it's not used or needed). Clean up for
26  *						gcc 2.5.8. PID to AX25_P_
27  *	AX.25 014	Alan(GW4PTS)		Cleanup and NET3 merge
28  *	AX.25 015	Alan(GW4PTS)		Internal test version.
29  *	AX.25 016	Alan(GW4PTS)		Semi Internal version for PI card
30  *						work.
31  *	AX.25 017	Alan(GW4PTS)		Fixed some small bugs reported by
32  *						G4KLX
33  *	AX.25 018	Alan(GW4PTS)		Fixed a small error in SOCK_DGRAM
34  *	AX.25 019	Alan(GW4PTS)		Clean ups for the non INET kernel and device ioctls in AX.25
35  *	AX.25 020	Jonathan(G4KLX)		/proc support and other changes.
36  *	AX.25 021	Alan(GW4PTS)		Added AX25_T1, AX25_N2, AX25_T3 as requested.
37  *	AX.25 022	Jonathan(G4KLX)		More work on the ax25 auto router and /proc improved (again)!
38  *			Alan(GW4PTS)		Added TIOCINQ/OUTQ
39  *	AX.25 023	Alan(GW4PTS)		Fixed shutdown bug
40  *	AX.25 023	Alan(GW4PTS)		Linus changed timers
41  *	AX.25 024	Alan(GW4PTS)		Small bug fixes
42  *	AX.25 025	Alan(GW4PTS)		More fixes, Linux 1.1.51 compatibility stuff, timers again!
43  *	AX.25 026	Alan(GW4PTS)		Small state fix.
44  *	AX.25 027	Alan(GW4PTS)		Socket close crash fixes.
45  *	AX.25 028	Alan(GW4PTS)		Callsign control including settings per uid.
46  *						Small bug fixes.
47  *						Protocol set by sockets only.
48  *						Small changes to allow for start of NET/ROM layer.
49  *	AX.25 028a	Jonathan(G4KLX)		Changes to state machine.
50  *	AX.25 028b	Jonathan(G4KLX)		Extracted ax25 control block
51  *						from sock structure.
52  *	AX.25 029	Alan(GW4PTS)		Combined 028b and some KA9Q code
53  *			Jonathan(G4KLX)		and removed all the old Berkeley, added IP mode registration.
54  *			Darryl(G7LED)		stuff. Cross-port digipeating. Minor fixes and enhancements.
55  *			Alan(GW4PTS)		Missed suser() on axassociate checks
56  *	AX.25 030	Alan(GW4PTS)		Added variable length headers.
57  *			Jonathan(G4KLX)		Added BPQ Ethernet interface.
58  *			Steven(GW7RRM)		Added digi-peating control ioctl.
59  *						Added extended AX.25 support.
60  *						Added AX.25 frame segmentation.
61  *			Darryl(G7LED)		Changed connect(), recvfrom(), sendto() sockaddr/addrlen to
62  *						fall inline with bind() and new policy.
63  *						Moved digipeating ctl to new ax25_dev structs.
64  *						Fixed ax25_release(), set TCP_CLOSE, wakeup app
65  *						context, THEN make the sock dead.
66  *			Alan(GW4PTS)		Cleaned up for single recvmsg methods.
67  *			Alan(GW4PTS)		Fixed not clearing error on connect failure.
68  *	AX.25 031	Jonathan(G4KLX)		Added binding to any device.
69  *			Joerg(DL1BKE)		Added DAMA support, fixed (?) digipeating, fixed buffer locking
70  *						for "virtual connect" mode... Result: Probably the
71  *						"Most Buggiest Code You've Ever Seen" (TM)
72  *			HaJo(DD8NE)		Implementation of a T5 (idle) timer
73  *			Joerg(DL1BKE)		Renamed T5 to IDLE and changed behaviour:
74  *						the timer gets reloaded on every received or transmitted
75  *						I frame for IP or NETROM. The idle timer is not active
76  *						on "vanilla AX.25" connections. Furthermore added PACLEN
77  *						to provide AX.25-layer based fragmentation (like WAMPES)
78  *      AX.25 032	Joerg(DL1BKE)		Fixed DAMA timeout error.
79  *						ax25_send_frame() limits the number of enqueued
80  *						datagrams per socket.
81  *	AX.25 033	Jonathan(G4KLX)		Removed auto-router.
82  *			Hans(PE1AYX)		Converted to Module.
83  *			Joerg(DL1BKE)		Moved BPQ Ethernet to separate driver.
84  *	AX.25 034	Jonathan(G4KLX)		2.1 changes
85  *			Alan(GW4PTS)		Small POSIXisations
86  *	AX.25 035	Alan(GW4PTS)		Started fixing to the new
87  *						format.
88  *			Hans(PE1AYX)		Fixed interface to IP layer.
89  *			Alan(GW4PTS)		Added asynchronous support.
90  *			Frederic(F1OAT)		Support for pseudo-digipeating.
91  *			Jonathan(G4KLX)		Support for packet forwarding.
92  *	AX.25 036	Jonathan(G4KLX)		Major restructuring.
93  *			Joerg(DL1BKE)		Fixed DAMA Slave.
94  *			Jonathan(G4KLX)		Fix wildcard listen parameter setting.
95  *	AX.25 037	Jonathan(G4KLX)		New timer architecture.
96  *      AX.25 038       Matthias(DG2FEF)        Small fixes to the syscall interface to make kernel
97  *                                              independent of AX25_MAX_DIGIS used by applications.
98  *                      Tomi(OH2BNS)            Fixed ax25_getname().
99  *			Joerg(DL1BKE)		Starting to phase out the support for full_sockaddr_ax25
100  *						with only 6 digipeaters and sockaddr_ax25 in ax25_bind(),
101  *						ax25_connect() and ax25_sendmsg()
102  *			Joerg(DL1BKE)		Added support for SO_BINDTODEVICE
103  *			Arnaldo C. Melo		s/suser/capable(CAP_NET_ADMIN)/, some more cleanups
104  *			Michal Ostrowski	Module initialization cleanup.
105  *			Jeroen(PE1RXQ)		Use sock_orphan() on release.
106  */
107 
108 #include <linux/config.h>
109 #include <linux/module.h>
110 #include <linux/errno.h>
111 #include <linux/types.h>
112 #include <linux/socket.h>
113 #include <linux/in.h>
114 #include <linux/kernel.h>
115 #include <linux/sched.h>
116 #include <linux/timer.h>
117 #include <linux/string.h>
118 #include <linux/sockios.h>
119 #include <linux/net.h>
120 #include <net/ax25.h>
121 #include <linux/inet.h>
122 #include <linux/netdevice.h>
123 #include <linux/if_arp.h>
124 #include <linux/skbuff.h>
125 #include <net/sock.h>
126 #include <asm/uaccess.h>
127 #include <asm/system.h>
128 #include <linux/fcntl.h>
129 #include <linux/termios.h>	/* For TIOCINQ/OUTQ */
130 #include <linux/mm.h>
131 #include <linux/interrupt.h>
132 #include <linux/notifier.h>
133 #include <linux/proc_fs.h>
134 #include <linux/stat.h>
135 #include <linux/netfilter.h>
136 #include <linux/sysctl.h>
137 #include <linux/init.h>
138 #include <net/ip.h>
139 #include <net/arp.h>
140 
141 
142 
143 ax25_cb *volatile ax25_list;
144 
145 static struct proto_ops ax25_proto_ops;
146 
147 /*
148  *	Free an allocated ax25 control block. This is done to centralise
149  *	the MOD count code.
150  */
ax25_free_cb(ax25_cb * ax25)151 void ax25_free_cb(ax25_cb *ax25)
152 {
153 	if (ax25->digipeat != NULL) {
154 		kfree(ax25->digipeat);
155 		ax25->digipeat = NULL;
156 	}
157 
158 	kfree(ax25);
159 
160 	MOD_DEC_USE_COUNT;
161 }
162 
ax25_free_sock(struct sock * sk)163 static void ax25_free_sock(struct sock *sk)
164 {
165 	ax25_free_cb(sk->protinfo.ax25);
166 }
167 
168 /*
169  *	Socket removal during an interrupt is now safe.
170  */
ax25_remove_socket(ax25_cb * ax25)171 static void ax25_remove_socket(ax25_cb *ax25)
172 {
173 	ax25_cb *s;
174 	unsigned long flags;
175 
176 	save_flags(flags); cli();
177 
178 	if ((s = ax25_list) == ax25) {
179 		ax25_list = s->next;
180 		restore_flags(flags);
181 		return;
182 	}
183 
184 	while (s != NULL && s->next != NULL) {
185 		if (s->next == ax25) {
186 			s->next = ax25->next;
187 			restore_flags(flags);
188 			return;
189 		}
190 
191 		s = s->next;
192 	}
193 
194 	restore_flags(flags);
195 }
196 
197 /*
198  *	Kill all bound sockets on a dropped device.
199  */
ax25_kill_by_device(struct net_device * dev)200 static void ax25_kill_by_device(struct net_device *dev)
201 {
202 	ax25_dev *ax25_dev;
203 	ax25_cb *s;
204 
205 	if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
206 		return;
207 
208 	for (s = ax25_list; s != NULL; s = s->next) {
209 		if (s->ax25_dev == ax25_dev) {
210 			s->ax25_dev = NULL;
211 			ax25_disconnect(s, ENETUNREACH);
212 		}
213 	}
214 }
215 
216 /*
217  *	Handle device status changes.
218  */
ax25_device_event(struct notifier_block * this,unsigned long event,void * ptr)219 static int ax25_device_event(struct notifier_block *this,unsigned long event, void *ptr)
220 {
221 	struct net_device *dev = (struct net_device *)ptr;
222 
223 	/* Reject non AX.25 devices */
224 	if (dev->type != ARPHRD_AX25)
225 		return NOTIFY_DONE;
226 
227 	switch (event) {
228 		case NETDEV_UP:
229 			ax25_dev_device_up(dev);
230 			break;
231 		case NETDEV_DOWN:
232 			ax25_kill_by_device(dev);
233 			ax25_rt_device_down(dev);
234 			ax25_dev_device_down(dev);
235 			break;
236 		default:
237 			break;
238 	}
239 
240 	return NOTIFY_DONE;
241 }
242 
243 /*
244  *	Add a socket to the bound sockets list.
245  */
ax25_insert_socket(ax25_cb * ax25)246 void ax25_insert_socket(ax25_cb *ax25)
247 {
248 	unsigned long flags;
249 
250 	save_flags(flags);
251 	cli();
252 
253 	ax25->next = ax25_list;
254 	ax25_list  = ax25;
255 
256 	restore_flags(flags);
257 }
258 
259 /*
260  *	Find a socket that wants to accept the SABM we have just
261  *	received.
262  */
ax25_find_listener(ax25_address * addr,int digi,struct net_device * dev,int type)263 struct sock *ax25_find_listener(ax25_address *addr, int digi, struct net_device *dev, int type)
264 {
265 	unsigned long flags;
266 	ax25_cb *s;
267 
268 	save_flags(flags);
269 	cli();
270 
271 	for (s = ax25_list; s != NULL; s = s->next) {
272 		if ((s->iamdigi && !digi) || (!s->iamdigi && digi))
273 			continue;
274 		if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 && s->sk->type == type && s->sk->state == TCP_LISTEN) {
275 			/* If device is null we match any device */
276 			if (s->ax25_dev == NULL || s->ax25_dev->dev == dev) {
277 				restore_flags(flags);
278 				return s->sk;
279 			}
280 		}
281 	}
282 
283 	restore_flags(flags);
284 	return NULL;
285 }
286 
287 /*
288  *	Find an AX.25 socket given both ends.
289  */
ax25_find_socket(ax25_address * my_addr,ax25_address * dest_addr,int type)290 struct sock *ax25_find_socket(ax25_address *my_addr, ax25_address *dest_addr, int type)
291 {
292 	ax25_cb *s;
293 	unsigned long flags;
294 
295 	save_flags(flags);
296 	cli();
297 
298 	for (s = ax25_list; s != NULL; s = s->next) {
299 		if (s->sk != NULL && ax25cmp(&s->source_addr, my_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->sk->type == type) {
300 			restore_flags(flags);
301 			return s->sk;
302 		}
303 	}
304 
305 	restore_flags(flags);
306 
307 	return NULL;
308 }
309 
310 /*
311  *	Find an AX.25 control block given both ends. It will only pick up
312  *	floating AX.25 control blocks or non Raw socket bound control blocks.
313  */
ax25_find_cb(ax25_address * src_addr,ax25_address * dest_addr,ax25_digi * digi,struct net_device * dev)314 ax25_cb *ax25_find_cb(ax25_address *src_addr, ax25_address *dest_addr, ax25_digi *digi, struct net_device *dev)
315 {
316 	ax25_cb *s;
317 	unsigned long flags;
318 
319 	save_flags(flags);
320 	cli();
321 
322 	for (s = ax25_list; s != NULL; s = s->next) {
323 		if (s->sk != NULL && s->sk->type != SOCK_SEQPACKET)
324 			continue;
325 		if (s->ax25_dev == NULL)
326 			continue;
327 		if (ax25cmp(&s->source_addr, src_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->ax25_dev->dev == dev) {
328 			if (digi != NULL && digi->ndigi != 0) {
329 				if (s->digipeat == NULL)
330 					continue;
331 				if (ax25digicmp(s->digipeat, digi) != 0)
332 					continue;
333 			} else {
334 				if (s->digipeat != NULL && s->digipeat->ndigi != 0)
335 					continue;
336 			}
337 			restore_flags(flags);
338 			return s;
339 		}
340 	}
341 
342 	restore_flags(flags);
343 
344 	return NULL;
345 }
346 
347 /*
348  *	Look for any matching address - RAW sockets can bind to arbitrary names
349  */
ax25_addr_match(ax25_address * addr)350 struct sock *ax25_addr_match(ax25_address *addr)
351 {
352 	unsigned long flags;
353 	ax25_cb *s;
354 
355 	save_flags(flags);
356 	cli();
357 
358 	for (s = ax25_list; s != NULL; s = s->next) {
359 		if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 && s->sk->type == SOCK_RAW) {
360 			restore_flags(flags);
361 			return s->sk;
362 		}
363 	}
364 
365 	restore_flags(flags);
366 
367 	return NULL;
368 }
369 
ax25_send_to_raw(struct sock * sk,struct sk_buff * skb,int proto)370 void ax25_send_to_raw(struct sock *sk, struct sk_buff *skb, int proto)
371 {
372 	struct sk_buff *copy;
373 
374 	while (sk != NULL) {
375 		if (sk->type == SOCK_RAW &&
376 		    sk->protocol == proto &&
377 		    atomic_read(&sk->rmem_alloc) <= sk->rcvbuf) {
378 			if ((copy = skb_clone(skb, GFP_ATOMIC)) == NULL)
379 				return;
380 
381 			if (sock_queue_rcv_skb(sk, copy) != 0)
382 				kfree_skb(copy);
383 		}
384 
385 		sk = sk->next;
386 	}
387 }
388 
389 /*
390  *	Deferred destroy.
391  */
392 void ax25_destroy_socket(ax25_cb *);
393 
394 /*
395  *	Handler for deferred kills.
396  */
ax25_destroy_timer(unsigned long data)397 static void ax25_destroy_timer(unsigned long data)
398 {
399 	ax25_destroy_socket((ax25_cb *)data);
400 }
401 
402 /*
403  *	This is called from user mode and the timers. Thus it protects itself against
404  *	interrupt users but doesn't worry about being called during work.
405  *	Once it is removed from the queue no interrupt or bottom half will
406  *	touch it and we are (fairly 8-) ) safe.
407  */
ax25_destroy_socket(ax25_cb * ax25)408 void ax25_destroy_socket(ax25_cb *ax25)	/* Not static as it's used by the timer */
409 {
410 	struct sk_buff *skb;
411 	unsigned long flags;
412 
413 	save_flags(flags); cli();
414 
415 	ax25_stop_heartbeat(ax25);
416 	ax25_stop_t1timer(ax25);
417 	ax25_stop_t2timer(ax25);
418 	ax25_stop_t3timer(ax25);
419 	ax25_stop_idletimer(ax25);
420 
421 	ax25_remove_socket(ax25);
422 	ax25_clear_queues(ax25);	/* Flush the queues */
423 
424 	if (ax25->sk != NULL) {
425 		while ((skb = skb_dequeue(&ax25->sk->receive_queue)) != NULL) {
426 			if (skb->sk != ax25->sk) {			/* A pending connection */
427 				skb->sk->dead = 1;	/* Queue the unaccepted socket for death */
428 				ax25_start_heartbeat(skb->sk->protinfo.ax25);
429 				skb->sk->protinfo.ax25->state = AX25_STATE_0;
430 			}
431 
432 			kfree_skb(skb);
433 		}
434 	}
435 
436 	if (ax25->sk != NULL) {
437 		if (atomic_read(&ax25->sk->wmem_alloc) != 0 ||
438 		    atomic_read(&ax25->sk->rmem_alloc) != 0) {
439 			/* Defer: outstanding buffers */
440 			init_timer(&ax25->timer);
441 			ax25->timer.expires  = jiffies + 10 * HZ;
442 			ax25->timer.function = ax25_destroy_timer;
443 			ax25->timer.data     = (unsigned long)ax25;
444 			add_timer(&ax25->timer);
445 		} else {
446 			sk_free(ax25->sk);
447 		}
448 	} else {
449 		ax25_free_cb(ax25);
450 	}
451 
452 	restore_flags(flags);
453 }
454 
455 /*
456  * dl1bke 960311: set parameters for existing AX.25 connections,
457  *		  includes a KILL command to abort any connection.
458  *		  VERY useful for debugging ;-)
459  */
ax25_ctl_ioctl(const unsigned int cmd,void * arg)460 static int ax25_ctl_ioctl(const unsigned int cmd, void *arg)
461 {
462 	struct ax25_ctl_struct ax25_ctl;
463 	ax25_digi digi;
464 	ax25_dev *ax25_dev;
465 	ax25_cb *ax25;
466 	unsigned int k;
467 
468 	if (copy_from_user(&ax25_ctl, arg, sizeof(ax25_ctl)))
469 		return -EFAULT;
470 
471 	if ((ax25_dev = ax25_addr_ax25dev(&ax25_ctl.port_addr)) == NULL)
472 		return -ENODEV;
473 
474 	if (ax25_ctl.digi_count > AX25_MAX_DIGIS)
475 		return -EINVAL;
476 
477 	digi.ndigi = ax25_ctl.digi_count;
478 	for (k = 0; k < digi.ndigi; k++)
479 		digi.calls[k] = ax25_ctl.digi_addr[k];
480 
481 	if ((ax25 = ax25_find_cb(&ax25_ctl.source_addr, &ax25_ctl.dest_addr, &digi, ax25_dev->dev)) == NULL)
482 		return -ENOTCONN;
483 
484 	switch (ax25_ctl.cmd) {
485 		case AX25_KILL:
486 			ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
487 #ifdef CONFIG_AX25_DAMA_SLAVE
488 			if (ax25_dev->dama.slave && ax25->ax25_dev->values[AX25_VALUES_PROTOCOL] == AX25_PROTO_DAMA_SLAVE)
489 				ax25_dama_off(ax25);
490 #endif
491 			ax25_disconnect(ax25, ENETRESET);
492 			break;
493 
494 	  	case AX25_WINDOW:
495 	  		if (ax25->modulus == AX25_MODULUS) {
496 	  			if (ax25_ctl.arg < 1 || ax25_ctl.arg > 7)
497 	  				return -EINVAL;
498 	  		} else {
499 	  			if (ax25_ctl.arg < 1 || ax25_ctl.arg > 63)
500 	  				return -EINVAL;
501 	  		}
502 	  		ax25->window = ax25_ctl.arg;
503 	  		break;
504 
505 	  	case AX25_T1:
506   			if (ax25_ctl.arg < 1)
507   				return -EINVAL;
508   			ax25->rtt = (ax25_ctl.arg * HZ) / 2;
509   			ax25->t1  = ax25_ctl.arg * HZ;
510   			break;
511 
512 	  	case AX25_T2:
513 	  		if (ax25_ctl.arg < 1)
514 	  			return -EINVAL;
515 	  		ax25->t2 = ax25_ctl.arg * HZ;
516 	  		break;
517 
518 	  	case AX25_N2:
519 	  		if (ax25_ctl.arg < 1 || ax25_ctl.arg > 31)
520 	  			return -EINVAL;
521 	  		ax25->n2count = 0;
522 	  		ax25->n2 = ax25_ctl.arg;
523 	  		break;
524 
525 	  	case AX25_T3:
526 	  		if (ax25_ctl.arg < 0)
527 	  			return -EINVAL;
528 	  		ax25->t3 = ax25_ctl.arg * HZ;
529 	  		break;
530 
531 	  	case AX25_IDLE:
532 	  		if (ax25_ctl.arg < 0)
533 	  			return -EINVAL;
534 	  		ax25->idle = ax25_ctl.arg * 60 * HZ;
535 	  		break;
536 
537 	  	case AX25_PACLEN:
538 	  		if (ax25_ctl.arg < 16 || ax25_ctl.arg > 65535)
539 	  			return -EINVAL;
540 	  		ax25->paclen = ax25_ctl.arg;
541 	  		break;
542 
543 	  	default:
544 	  		return -EINVAL;
545 	  }
546 
547 	  return 0;
548 }
549 
550 /*
551  *	Fill in a created AX.25 created control block with the default
552  *	values for a particular device.
553  */
ax25_fillin_cb(ax25_cb * ax25,ax25_dev * ax25_dev)554 void ax25_fillin_cb(ax25_cb *ax25, ax25_dev *ax25_dev)
555 {
556 	ax25->ax25_dev = ax25_dev;
557 
558 	if (ax25->ax25_dev != NULL) {
559 		ax25->rtt     = ax25_dev->values[AX25_VALUES_T1] / 2;
560 		ax25->t1      = ax25_dev->values[AX25_VALUES_T1];
561 		ax25->t2      = ax25_dev->values[AX25_VALUES_T2];
562 		ax25->t3      = ax25_dev->values[AX25_VALUES_T3];
563 		ax25->n2      = ax25_dev->values[AX25_VALUES_N2];
564 		ax25->paclen  = ax25_dev->values[AX25_VALUES_PACLEN];
565 		ax25->idle    = ax25_dev->values[AX25_VALUES_IDLE];
566 		ax25->backoff = ax25_dev->values[AX25_VALUES_BACKOFF];
567 
568 		if (ax25_dev->values[AX25_VALUES_AXDEFMODE]) {
569 			ax25->modulus = AX25_EMODULUS;
570 			ax25->window  = ax25_dev->values[AX25_VALUES_EWINDOW];
571 		} else {
572 			ax25->modulus = AX25_MODULUS;
573 			ax25->window  = ax25_dev->values[AX25_VALUES_WINDOW];
574 		}
575 	} else {
576 		ax25->rtt     = AX25_DEF_T1 / 2;
577 		ax25->t1      = AX25_DEF_T1;
578 		ax25->t2      = AX25_DEF_T2;
579 		ax25->t3      = AX25_DEF_T3;
580 		ax25->n2      = AX25_DEF_N2;
581 		ax25->paclen  = AX25_DEF_PACLEN;
582 		ax25->idle    = AX25_DEF_IDLE;
583 		ax25->backoff = AX25_DEF_BACKOFF;
584 
585 		if (AX25_DEF_AXDEFMODE) {
586 			ax25->modulus = AX25_EMODULUS;
587 			ax25->window  = AX25_DEF_EWINDOW;
588 		} else {
589 			ax25->modulus = AX25_MODULUS;
590 			ax25->window  = AX25_DEF_WINDOW;
591 		}
592 	}
593 }
594 
595 /*
596  * Create an empty AX.25 control block.
597  */
ax25_create_cb(void)598 ax25_cb *ax25_create_cb(void)
599 {
600 	ax25_cb *ax25;
601 
602 	if ((ax25 = kmalloc(sizeof(*ax25), GFP_ATOMIC)) == NULL)
603 		return NULL;
604 
605 	MOD_INC_USE_COUNT;
606 
607 	memset(ax25, 0x00, sizeof(*ax25));
608 
609 	skb_queue_head_init(&ax25->write_queue);
610 	skb_queue_head_init(&ax25->frag_queue);
611 	skb_queue_head_init(&ax25->ack_queue);
612 	skb_queue_head_init(&ax25->reseq_queue);
613 
614 	init_timer(&ax25->timer);
615 	init_timer(&ax25->t1timer);
616 	init_timer(&ax25->t2timer);
617 	init_timer(&ax25->t3timer);
618 	init_timer(&ax25->idletimer);
619 
620 	ax25_fillin_cb(ax25, NULL);
621 
622 	ax25->state = AX25_STATE_0;
623 
624 	return ax25;
625 }
626 
627 /*
628  *	Handling for system calls applied via the various interfaces to an
629  *	AX25 socket object
630  */
631 
ax25_setsockopt(struct socket * sock,int level,int optname,char * optval,int optlen)632 static int ax25_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
633 {
634 	struct sock *sk = sock->sk;
635 	struct net_device *dev;
636 	char devname[IFNAMSIZ];
637 	int opt;
638 
639 	if (level != SOL_AX25)
640 		return -ENOPROTOOPT;
641 
642 	if (optlen < (int)sizeof(int))
643 		return -EINVAL;
644 
645 	if (get_user(opt, (int *)optval))
646 		return -EFAULT;
647 
648 	switch (optname) {
649 		case AX25_WINDOW:
650 			if (sk->protinfo.ax25->modulus == AX25_MODULUS) {
651 				if (opt < 1 || opt > 7)
652 					return -EINVAL;
653 			} else {
654 				if (opt < 1 || opt > 63)
655 					return -EINVAL;
656 			}
657 			sk->protinfo.ax25->window = opt;
658 			return 0;
659 
660 		case AX25_T1:
661 			if (opt < 1)
662 				return -EINVAL;
663 			sk->protinfo.ax25->rtt = (opt * HZ) / 2;
664 			sk->protinfo.ax25->t1  = opt * HZ;
665 			return 0;
666 
667 		case AX25_T2:
668 			if (opt < 1)
669 				return -EINVAL;
670 			sk->protinfo.ax25->t2 = opt * HZ;
671 			return 0;
672 
673 		case AX25_N2:
674 			if (opt < 1 || opt > 31)
675 				return -EINVAL;
676 			sk->protinfo.ax25->n2 = opt;
677 			return 0;
678 
679 		case AX25_T3:
680 			if (opt < 1)
681 				return -EINVAL;
682 			sk->protinfo.ax25->t3 = opt * HZ;
683 			return 0;
684 
685 		case AX25_IDLE:
686 			if (opt < 0)
687 				return -EINVAL;
688 			sk->protinfo.ax25->idle = opt * 60 * HZ;
689 			return 0;
690 
691 		case AX25_BACKOFF:
692 			if (opt < 0 || opt > 2)
693 				return -EINVAL;
694 			sk->protinfo.ax25->backoff = opt;
695 			return 0;
696 
697 		case AX25_EXTSEQ:
698 			sk->protinfo.ax25->modulus = opt ? AX25_EMODULUS : AX25_MODULUS;
699 			return 0;
700 
701 		case AX25_PIDINCL:
702 			sk->protinfo.ax25->pidincl = opt ? 1 : 0;
703 			return 0;
704 
705 		case AX25_IAMDIGI:
706 			sk->protinfo.ax25->iamdigi = opt ? 1 : 0;
707 			return 0;
708 
709 		case AX25_PACLEN:
710 			if (opt < 16 || opt > 65535)
711 				return -EINVAL;
712 			sk->protinfo.ax25->paclen = opt;
713 			return 0;
714 
715 		case SO_BINDTODEVICE:
716 			if (optlen > IFNAMSIZ) optlen=IFNAMSIZ;
717 			if (copy_from_user(devname, optval, optlen))
718 				return -EFAULT;
719 
720 			dev = dev_get_by_name(devname);
721 			if (dev == NULL) return -ENODEV;
722 
723 			if (sk->type == SOCK_SEQPACKET &&
724 			   (sock->state != SS_UNCONNECTED || sk->state == TCP_LISTEN))
725 				return -EADDRNOTAVAIL;
726 
727 			sk->protinfo.ax25->ax25_dev = ax25_dev_ax25dev(dev);
728 			ax25_fillin_cb(sk->protinfo.ax25, sk->protinfo.ax25->ax25_dev);
729 			return 0;
730 
731 		default:
732 			return -ENOPROTOOPT;
733 	}
734 }
735 
ax25_getsockopt(struct socket * sock,int level,int optname,char * optval,int * optlen)736 static int ax25_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
737 {
738 	struct sock *sk = sock->sk;
739 	struct ax25_dev *ax25_dev;
740 	char devname[IFNAMSIZ];
741 	void *valptr;
742 	int val = 0;
743 	int maxlen, length;
744 
745 	if (level != SOL_AX25)
746 		return -ENOPROTOOPT;
747 
748 	if (get_user(maxlen, optlen))
749 		return -EFAULT;
750 
751 	if (maxlen < 1)
752 		return -EFAULT;
753 
754 	valptr = (void *) &val;
755 	length = min_t(unsigned int, maxlen, sizeof(int));
756 
757 	switch (optname) {
758 		case AX25_WINDOW:
759 			val = sk->protinfo.ax25->window;
760 			break;
761 
762 		case AX25_T1:
763 			val = sk->protinfo.ax25->t1 / HZ;
764 			break;
765 
766 		case AX25_T2:
767 			val = sk->protinfo.ax25->t2 / HZ;
768 			break;
769 
770 		case AX25_N2:
771 			val = sk->protinfo.ax25->n2;
772 			break;
773 
774 		case AX25_T3:
775 			val = sk->protinfo.ax25->t3 / HZ;
776 			break;
777 
778 		case AX25_IDLE:
779 			val = sk->protinfo.ax25->idle / (60 * HZ);
780 			break;
781 
782 		case AX25_BACKOFF:
783 			val = sk->protinfo.ax25->backoff;
784 			break;
785 
786 		case AX25_EXTSEQ:
787 			val = (sk->protinfo.ax25->modulus == AX25_EMODULUS);
788 			break;
789 
790 		case AX25_PIDINCL:
791 			val = sk->protinfo.ax25->pidincl;
792 			break;
793 
794 		case AX25_IAMDIGI:
795 			val = sk->protinfo.ax25->iamdigi;
796 			break;
797 
798 		case AX25_PACLEN:
799 			val = sk->protinfo.ax25->paclen;
800 			break;
801 
802 		case SO_BINDTODEVICE:
803 			ax25_dev = sk->protinfo.ax25->ax25_dev;
804 
805 			if (ax25_dev != NULL && ax25_dev->dev != NULL) {
806 				strncpy(devname, ax25_dev->dev->name, IFNAMSIZ);
807 				length = min_t(unsigned int, strlen(ax25_dev->dev->name)+1, maxlen);
808 				devname[length-1] = '\0';
809 			} else {
810 				*devname = '\0';
811 				length = 1;
812 			}
813 
814 			valptr = (void *) devname;
815 			break;
816 
817 		default:
818 			return -ENOPROTOOPT;
819 	}
820 
821 	if (put_user(length, optlen))
822 		return -EFAULT;
823 
824 	return copy_to_user(optval, valptr, length) ? -EFAULT : 0;
825 }
826 
ax25_listen(struct socket * sock,int backlog)827 static int ax25_listen(struct socket *sock, int backlog)
828 {
829 	struct sock *sk = sock->sk;
830 
831 	if (sk->type == SOCK_SEQPACKET && sk->state != TCP_LISTEN) {
832 		sk->max_ack_backlog = backlog;
833 		sk->state           = TCP_LISTEN;
834 		return 0;
835 	}
836 
837 	return -EOPNOTSUPP;
838 }
839 
ax25_create(struct socket * sock,int protocol)840 int ax25_create(struct socket *sock, int protocol)
841 {
842 	struct sock *sk;
843 	ax25_cb *ax25;
844 
845 	switch (sock->type) {
846 		case SOCK_DGRAM:
847 			if (protocol == 0 || protocol == PF_AX25)
848 				protocol = AX25_P_TEXT;
849 			break;
850 		case SOCK_SEQPACKET:
851 			switch (protocol) {
852 				case 0:
853 				case PF_AX25:	/* For CLX */
854 					protocol = AX25_P_TEXT;
855 					break;
856 				case AX25_P_SEGMENT:
857 #ifdef CONFIG_INET
858 				case AX25_P_ARP:
859 				case AX25_P_IP:
860 #endif
861 #ifdef CONFIG_NETROM
862 				case AX25_P_NETROM:
863 #endif
864 #ifdef CONFIG_ROSE
865 				case AX25_P_ROSE:
866 #endif
867 					return -ESOCKTNOSUPPORT;
868 #ifdef CONFIG_NETROM_MODULE
869 				case AX25_P_NETROM:
870 					if (ax25_protocol_is_registered(AX25_P_NETROM))
871 						return -ESOCKTNOSUPPORT;
872 #endif
873 #ifdef CONFIG_ROSE_MODULE
874 				case AX25_P_ROSE:
875 					if (ax25_protocol_is_registered(AX25_P_ROSE))
876 						return -ESOCKTNOSUPPORT;
877 #endif
878 				default:
879 					break;
880 			}
881 			break;
882 		case SOCK_RAW:
883 			break;
884 		default:
885 			return -ESOCKTNOSUPPORT;
886 	}
887 
888 	if ((sk = sk_alloc(PF_AX25, GFP_ATOMIC, 1)) == NULL)
889 		return -ENOMEM;
890 
891 	if ((ax25 = ax25_create_cb()) == NULL) {
892 		sk_free(sk);
893 		return -ENOMEM;
894 	}
895 
896 	sock_init_data(sock, sk);
897 
898 	sk->destruct = ax25_free_sock;
899 	sock->ops    = &ax25_proto_ops;
900 	sk->protocol = protocol;
901 
902 	ax25->sk          = sk;
903 	sk->protinfo.ax25 = ax25;
904 
905 	return 0;
906 }
907 
ax25_make_new(struct sock * osk,struct ax25_dev * ax25_dev)908 struct sock *ax25_make_new(struct sock *osk, struct ax25_dev *ax25_dev)
909 {
910 	struct sock *sk;
911 	ax25_cb *ax25;
912 
913 	if ((sk = sk_alloc(PF_AX25, GFP_ATOMIC, 1)) == NULL)
914 		return NULL;
915 
916 	if ((ax25 = ax25_create_cb()) == NULL) {
917 		sk_free(sk);
918 		return NULL;
919 	}
920 
921 	switch (osk->type) {
922 		case SOCK_DGRAM:
923 			break;
924 		case SOCK_SEQPACKET:
925 			break;
926 		default:
927 			sk_free(sk);
928 			ax25_free_cb(ax25);
929 			return NULL;
930 	}
931 
932 	sock_init_data(NULL, sk);
933 
934 	sk->destruct = ax25_free_sock;
935 	sk->type     = osk->type;
936 	sk->socket   = osk->socket;
937 	sk->priority = osk->priority;
938 	sk->protocol = osk->protocol;
939 	sk->rcvbuf   = osk->rcvbuf;
940 	sk->sndbuf   = osk->sndbuf;
941 	sk->debug    = osk->debug;
942 	sk->state    = TCP_ESTABLISHED;
943 	sk->sleep    = osk->sleep;
944 	sk->zapped   = osk->zapped;
945 
946 	ax25->modulus = osk->protinfo.ax25->modulus;
947 	ax25->backoff = osk->protinfo.ax25->backoff;
948 	ax25->pidincl = osk->protinfo.ax25->pidincl;
949 	ax25->iamdigi = osk->protinfo.ax25->iamdigi;
950 	ax25->rtt     = osk->protinfo.ax25->rtt;
951 	ax25->t1      = osk->protinfo.ax25->t1;
952 	ax25->t2      = osk->protinfo.ax25->t2;
953 	ax25->t3      = osk->protinfo.ax25->t3;
954 	ax25->n2      = osk->protinfo.ax25->n2;
955 	ax25->idle    = osk->protinfo.ax25->idle;
956 	ax25->paclen  = osk->protinfo.ax25->paclen;
957 	ax25->window  = osk->protinfo.ax25->window;
958 
959 	ax25->ax25_dev    = ax25_dev;
960 	ax25->source_addr = osk->protinfo.ax25->source_addr;
961 
962 	if (osk->protinfo.ax25->digipeat != NULL) {
963 		if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
964 			sk_free(sk);
965 			return NULL;
966 		}
967 
968 		memcpy(ax25->digipeat, osk->protinfo.ax25->digipeat, sizeof(ax25_digi));
969 	}
970 
971 	sk->protinfo.ax25 = ax25;
972 	ax25->sk          = sk;
973 
974 	return sk;
975 }
976 
ax25_release(struct socket * sock)977 static int ax25_release(struct socket *sock)
978 {
979 	struct sock *sk = sock->sk;
980 
981 	if (sk == NULL) return 0;
982 
983 	if (sk->type == SOCK_SEQPACKET) {
984 		switch (sk->protinfo.ax25->state) {
985 			case AX25_STATE_0:
986 				ax25_disconnect(sk->protinfo.ax25, 0);
987 				ax25_destroy_socket(sk->protinfo.ax25);
988 				break;
989 
990 			case AX25_STATE_1:
991 			case AX25_STATE_2:
992 				ax25_send_control(sk->protinfo.ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
993 				ax25_disconnect(sk->protinfo.ax25, 0);
994 				ax25_destroy_socket(sk->protinfo.ax25);
995 				break;
996 
997 			case AX25_STATE_3:
998 			case AX25_STATE_4:
999 				ax25_clear_queues(sk->protinfo.ax25);
1000 				sk->protinfo.ax25->n2count = 0;
1001 				switch (sk->protinfo.ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
1002 					case AX25_PROTO_STD_SIMPLEX:
1003 					case AX25_PROTO_STD_DUPLEX:
1004 						ax25_send_control(sk->protinfo.ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
1005 						ax25_stop_t2timer(sk->protinfo.ax25);
1006 						ax25_stop_t3timer(sk->protinfo.ax25);
1007 						ax25_stop_idletimer(sk->protinfo.ax25);
1008 						break;
1009 #ifdef CONFIG_AX25_DAMA_SLAVE
1010 					case AX25_PROTO_DAMA_SLAVE:
1011 						ax25_stop_t3timer(sk->protinfo.ax25);
1012 						ax25_stop_idletimer(sk->protinfo.ax25);
1013 						break;
1014 #endif
1015 				}
1016 				ax25_calculate_t1(sk->protinfo.ax25);
1017 				ax25_start_t1timer(sk->protinfo.ax25);
1018 				sk->protinfo.ax25->state = AX25_STATE_2;
1019 				sk->state                = TCP_CLOSE;
1020 				sk->shutdown            |= SEND_SHUTDOWN;
1021 				sk->state_change(sk);
1022 				sock_orphan(sk);
1023 				sk->destroy              = 1;
1024 				break;
1025 
1026 			default:
1027 				break;
1028 		}
1029 	} else {
1030 		sk->state     = TCP_CLOSE;
1031 		sk->shutdown |= SEND_SHUTDOWN;
1032 		sk->state_change(sk);
1033 		sock_orphan(sk);
1034 		ax25_destroy_socket(sk->protinfo.ax25);
1035 	}
1036 
1037 	sock->sk   = NULL;
1038 	sk->socket = NULL;	/* Not used, but we should do this */
1039 
1040 	return 0;
1041 }
1042 
1043 /*
1044  *	We support a funny extension here so you can (as root) give any callsign
1045  *	digipeated via a local address as source. This hack is obsolete now
1046  *	that we've implemented support for SO_BINDTODEVICE. It is however small
1047  *	and trivially backward compatible.
1048  */
ax25_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)1049 static int ax25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1050 {
1051 	struct sock *sk = sock->sk;
1052 	struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
1053 	ax25_address *call;
1054 	ax25_dev *ax25_dev = NULL;
1055 
1056 	if (sk->zapped == 0)
1057 		return -EINVAL;
1058 
1059 	if (addr_len != sizeof(struct sockaddr_ax25) &&
1060 	    addr_len != sizeof(struct full_sockaddr_ax25)) {
1061 		/* support for old structure may go away some time */
1062 		if ((addr_len < sizeof(struct sockaddr_ax25) + sizeof(ax25_address) * 6) ||
1063 		    (addr_len > sizeof(struct full_sockaddr_ax25)))
1064 			return -EINVAL;
1065 
1066 		printk(KERN_WARNING "ax25_bind(): %s uses old (6 digipeater) socket structure.\n",
1067 			current->comm);
1068 	}
1069 
1070 	if (addr->fsa_ax25.sax25_family != AF_AX25)
1071 		return -EINVAL;
1072 
1073 	call = ax25_findbyuid(current->euid);
1074 	if (call == NULL && ax25_uid_policy && !capable(CAP_NET_ADMIN))
1075 		return -EACCES;
1076 
1077 	if (call == NULL)
1078 		sk->protinfo.ax25->source_addr = addr->fsa_ax25.sax25_call;
1079 	else
1080 		sk->protinfo.ax25->source_addr = *call;
1081 
1082 	/*
1083 	 * User already set interface with SO_BINDTODEVICE
1084 	 */
1085 
1086 	if (sk->protinfo.ax25->ax25_dev != NULL)
1087 		goto done;
1088 
1089 	if (addr_len > sizeof(struct sockaddr_ax25) && addr->fsa_ax25.sax25_ndigis == 1) {
1090 		if (ax25cmp(&addr->fsa_digipeater[0], &null_ax25_address) != 0 &&
1091 		    (ax25_dev = ax25_addr_ax25dev(&addr->fsa_digipeater[0])) == NULL)
1092 			return -EADDRNOTAVAIL;
1093 	}  else {
1094 		if ((ax25_dev = ax25_addr_ax25dev(&addr->fsa_ax25.sax25_call)) == NULL)
1095 			return -EADDRNOTAVAIL;
1096 	}
1097 
1098 	if (ax25_dev != NULL)
1099 		ax25_fillin_cb(sk->protinfo.ax25, ax25_dev);
1100 
1101 done:
1102 	ax25_insert_socket(sk->protinfo.ax25);
1103 	sk->zapped = 0;
1104 	return 0;
1105 }
1106 
1107 /*
1108  *	FIXME: nonblock behaviour looks like it may have a bug.
1109  */
ax25_connect(struct socket * sock,struct sockaddr * uaddr,int addr_len,int flags)1110 static int ax25_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
1111 {
1112 	struct sock *sk = sock->sk;
1113 	struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)uaddr;
1114 	ax25_digi *digi = NULL;
1115 	int ct = 0, err;
1116 
1117 	/* deal with restarts */
1118 	if (sock->state == SS_CONNECTING) {
1119 		switch (sk->state) {
1120 		case TCP_SYN_SENT: /* still trying */
1121 			return -EINPROGRESS;
1122 
1123 		case TCP_ESTABLISHED: /* connection established */
1124 			sock->state = SS_CONNECTED;
1125 			return 0;
1126 
1127 		case TCP_CLOSE: /* connection refused */
1128 			sock->state = SS_UNCONNECTED;
1129 			return -ECONNREFUSED;
1130 		}
1131 	}
1132 
1133 	if (sk->state == TCP_ESTABLISHED && sk->type == SOCK_SEQPACKET)
1134 		return -EISCONN;	/* No reconnect on a seqpacket socket */
1135 
1136 	sk->state   = TCP_CLOSE;
1137 	sock->state = SS_UNCONNECTED;
1138 
1139 	/*
1140 	 * some sanity checks. code further down depends on this
1141 	 */
1142 
1143 	if (addr_len == sizeof(struct sockaddr_ax25)) {
1144 		/* support for this will go away in early 2.5.x */
1145 		printk(KERN_WARNING "ax25_connect(): %s uses obsolete socket structure\n",
1146 			current->comm);
1147 	}
1148 	else if (addr_len != sizeof(struct full_sockaddr_ax25)) {
1149 		/* support for old structure may go away some time */
1150 		if ((addr_len < sizeof(struct sockaddr_ax25) + sizeof(ax25_address) * 6) ||
1151 		    (addr_len > sizeof(struct full_sockaddr_ax25)))
1152 			return -EINVAL;
1153 
1154 		printk(KERN_WARNING "ax25_connect(): %s uses old (6 digipeater) socket structure.\n",
1155 			current->comm);
1156 	}
1157 
1158 	if (fsa->fsa_ax25.sax25_family != AF_AX25)
1159 		return -EINVAL;
1160 
1161 	if (sk->protinfo.ax25->digipeat != NULL) {
1162 		kfree(sk->protinfo.ax25->digipeat);
1163 		sk->protinfo.ax25->digipeat = NULL;
1164 	}
1165 
1166 	/*
1167 	 *	Handle digi-peaters to be used.
1168 	 */
1169 	if (addr_len > sizeof(struct sockaddr_ax25) && fsa->fsa_ax25.sax25_ndigis != 0) {
1170 		/* Valid number of digipeaters ? */
1171 		if (fsa->fsa_ax25.sax25_ndigis < 1 || fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS)
1172 			return -EINVAL;
1173 
1174 		if ((digi = kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL)
1175 			return -ENOBUFS;
1176 
1177 		digi->ndigi      = fsa->fsa_ax25.sax25_ndigis;
1178 		digi->lastrepeat = -1;
1179 
1180 		while (ct < fsa->fsa_ax25.sax25_ndigis) {
1181 			if ((fsa->fsa_digipeater[ct].ax25_call[6] & AX25_HBIT) && sk->protinfo.ax25->iamdigi) {
1182 				digi->repeated[ct] = 1;
1183 				digi->lastrepeat   = ct;
1184 			} else {
1185 				digi->repeated[ct] = 0;
1186 			}
1187 			digi->calls[ct] = fsa->fsa_digipeater[ct];
1188 			ct++;
1189 		}
1190 	}
1191 
1192 	/*
1193 	 *	Must bind first - autobinding in this may or may not work. If
1194 	 *	the socket is already bound, check to see if the device has
1195 	 *	been filled in, error if it hasn't.
1196 	 */
1197 	if (sk->zapped) {
1198 		/* check if we can remove this feature. It is broken. */
1199 		printk(KERN_WARNING "ax25_connect(): %s uses autobind, please contact jreuter@yaina.de\n",
1200 			current->comm);
1201 		if ((err = ax25_rt_autobind(sk->protinfo.ax25, &fsa->fsa_ax25.sax25_call)) < 0)
1202 			return err;
1203 		ax25_fillin_cb(sk->protinfo.ax25, sk->protinfo.ax25->ax25_dev);
1204 		ax25_insert_socket(sk->protinfo.ax25);
1205 	} else {
1206 		if (sk->protinfo.ax25->ax25_dev == NULL)
1207 			return -EHOSTUNREACH;
1208 	}
1209 
1210 	if (sk->type == SOCK_SEQPACKET && ax25_find_cb(&sk->protinfo.ax25->source_addr, &fsa->fsa_ax25.sax25_call, digi, sk->protinfo.ax25->ax25_dev->dev) != NULL) {
1211 		if (digi != NULL) kfree(digi);
1212 		return -EADDRINUSE;			/* Already such a connection */
1213 	}
1214 
1215 	sk->protinfo.ax25->dest_addr = fsa->fsa_ax25.sax25_call;
1216 	sk->protinfo.ax25->digipeat  = digi;
1217 
1218 	/* First the easy one */
1219 	if (sk->type != SOCK_SEQPACKET) {
1220 		sock->state = SS_CONNECTED;
1221 		sk->state   = TCP_ESTABLISHED;
1222 		return 0;
1223 	}
1224 
1225 	/* Move to connecting socket, ax.25 lapb WAIT_UA.. */
1226 	sock->state        = SS_CONNECTING;
1227 	sk->state          = TCP_SYN_SENT;
1228 
1229 	switch (sk->protinfo.ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
1230 		case AX25_PROTO_STD_SIMPLEX:
1231 		case AX25_PROTO_STD_DUPLEX:
1232 			ax25_std_establish_data_link(sk->protinfo.ax25);
1233 			break;
1234 
1235 #ifdef CONFIG_AX25_DAMA_SLAVE
1236 		case AX25_PROTO_DAMA_SLAVE:
1237 			sk->protinfo.ax25->modulus = AX25_MODULUS;
1238 			sk->protinfo.ax25->window  = sk->protinfo.ax25->ax25_dev->values[AX25_VALUES_WINDOW];
1239 			if (sk->protinfo.ax25->ax25_dev->dama.slave)
1240 				ax25_ds_establish_data_link(sk->protinfo.ax25);
1241 			else
1242 				ax25_std_establish_data_link(sk->protinfo.ax25);
1243 			break;
1244 #endif
1245 	}
1246 
1247 	sk->protinfo.ax25->state = AX25_STATE_1;
1248 
1249 	ax25_start_heartbeat(sk->protinfo.ax25);
1250 
1251 	/* Now the loop */
1252 	if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
1253 		return -EINPROGRESS;
1254 
1255 	cli();	/* To avoid races on the sleep */
1256 
1257 	/* A DM or timeout will go to closed, a UA will go to ABM */
1258 	while (sk->state == TCP_SYN_SENT) {
1259 		interruptible_sleep_on(sk->sleep);
1260 		if (signal_pending(current)) {
1261 			sti();
1262 			return -ERESTARTSYS;
1263 		}
1264 	}
1265 
1266 	if (sk->state != TCP_ESTABLISHED) {
1267 		/* Not in ABM, not in WAIT_UA -> failed */
1268 		sti();
1269 		sock->state = SS_UNCONNECTED;
1270 		return sock_error(sk);	/* Always set at this point */
1271 	}
1272 
1273 	sock->state = SS_CONNECTED;
1274 
1275 	sti();
1276 
1277 	return 0;
1278 }
1279 
1280 
ax25_accept(struct socket * sock,struct socket * newsock,int flags)1281 static int ax25_accept(struct socket *sock, struct socket *newsock, int flags)
1282 {
1283 	struct sock *sk;
1284 	struct sock *newsk;
1285 	struct sk_buff *skb;
1286 
1287 	if (sock->state != SS_UNCONNECTED)
1288 		return -EINVAL;
1289 
1290 	if ((sk = sock->sk) == NULL)
1291 		return -EINVAL;
1292 
1293 	if (sk->type != SOCK_SEQPACKET)
1294 		return -EOPNOTSUPP;
1295 
1296 	if (sk->state != TCP_LISTEN)
1297 		return -EINVAL;
1298 
1299 	/*
1300 	 *	The read queue this time is holding sockets ready to use
1301 	 *	hooked into the SABM we saved
1302 	 */
1303 	do {
1304 		if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
1305 			if (flags & O_NONBLOCK)
1306 				return -EWOULDBLOCK;
1307 
1308 			interruptible_sleep_on(sk->sleep);
1309 			if (signal_pending(current))
1310 				return -ERESTARTSYS;
1311 		}
1312 	} while (skb == NULL);
1313 
1314 	newsk = skb->sk;
1315 	newsk->pair = NULL;
1316 	newsk->socket = newsock;
1317 	newsk->sleep = &newsock->wait;
1318 
1319 	/* Now attach up the new socket */
1320 	kfree_skb(skb);
1321 	sk->ack_backlog--;
1322 	newsock->sk    = newsk;
1323 	newsock->state = SS_CONNECTED;
1324 
1325 	return 0;
1326 }
1327 
ax25_getname(struct socket * sock,struct sockaddr * uaddr,int * uaddr_len,int peer)1328 static int ax25_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
1329 {
1330 	struct sock *sk = sock->sk;
1331 	struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)uaddr;
1332 	unsigned char ndigi, i;
1333 
1334 	if (peer != 0) {
1335 		if (sk->state != TCP_ESTABLISHED)
1336 			return -ENOTCONN;
1337 
1338 		fsa->fsa_ax25.sax25_family = AF_AX25;
1339 		fsa->fsa_ax25.sax25_call   = sk->protinfo.ax25->dest_addr;
1340 		fsa->fsa_ax25.sax25_ndigis = 0;
1341 
1342 		if (sk->protinfo.ax25->digipeat != NULL) {
1343 			ndigi = sk->protinfo.ax25->digipeat->ndigi;
1344 			fsa->fsa_ax25.sax25_ndigis = ndigi;
1345 			for (i = 0; i < ndigi; i++)
1346 				fsa->fsa_digipeater[i] = sk->protinfo.ax25->digipeat->calls[i];
1347 		}
1348 	} else {
1349 		fsa->fsa_ax25.sax25_family = AF_AX25;
1350 		fsa->fsa_ax25.sax25_call   = sk->protinfo.ax25->source_addr;
1351 		fsa->fsa_ax25.sax25_ndigis = 1;
1352 		if (sk->protinfo.ax25->ax25_dev != NULL) {
1353 			memcpy(&fsa->fsa_digipeater[0], sk->protinfo.ax25->ax25_dev->dev->dev_addr, AX25_ADDR_LEN);
1354 		} else {
1355 			fsa->fsa_digipeater[0] = null_ax25_address;
1356 		}
1357 	}
1358 	*uaddr_len = sizeof (struct full_sockaddr_ax25);
1359 	return 0;
1360 }
1361 
ax25_sendmsg(struct socket * sock,struct msghdr * msg,int len,struct scm_cookie * scm)1362 static int ax25_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
1363 {
1364 	struct sock *sk = sock->sk;
1365 	struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1366 	int err;
1367 	struct sockaddr_ax25 sax;
1368 	struct sk_buff *skb;
1369 	unsigned char *asmptr;
1370 	int size;
1371 	ax25_digi *dp;
1372 	ax25_digi dtmp;
1373 	int lv;
1374 	int addr_len = msg->msg_namelen;
1375 
1376 	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR))
1377 		return -EINVAL;
1378 
1379 	if (sk->zapped)
1380 		return -EADDRNOTAVAIL;
1381 
1382 	if (sk->shutdown & SEND_SHUTDOWN) {
1383 		send_sig(SIGPIPE, current, 0);
1384 		return -EPIPE;
1385 	}
1386 
1387 	if (sk->protinfo.ax25->ax25_dev == NULL)
1388 		return -ENETUNREACH;
1389 
1390 	if (usax != NULL) {
1391 		if (usax->sax25_family != AF_AX25)
1392 			return -EINVAL;
1393 
1394 		if (addr_len == sizeof(struct sockaddr_ax25)) {
1395 			printk(KERN_WARNING "ax25_sendmsg(): %s uses obsolete socket structure\n",
1396 				current->comm);
1397 		}
1398 		else if (addr_len != sizeof(struct full_sockaddr_ax25)) {
1399 			/* support for old structure may go away some time */
1400 			if ((addr_len < sizeof(struct sockaddr_ax25) + sizeof(ax25_address) * 6) ||
1401 		    	    (addr_len > sizeof(struct full_sockaddr_ax25)))
1402 		    		return -EINVAL;
1403 
1404 			printk(KERN_WARNING "ax25_sendmsg(): %s uses old (6 digipeater) socket structure.\n",
1405 				current->comm);
1406 		}
1407 
1408 		if (addr_len > sizeof(struct sockaddr_ax25) && usax->sax25_ndigis != 0) {
1409 			int ct           = 0;
1410 			struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)usax;
1411 
1412 			/* Valid number of digipeaters ? */
1413 			if (usax->sax25_ndigis < 1 || usax->sax25_ndigis > AX25_MAX_DIGIS)
1414 				return -EINVAL;
1415 
1416 			dtmp.ndigi      = usax->sax25_ndigis;
1417 
1418 			while (ct < usax->sax25_ndigis) {
1419 				dtmp.repeated[ct] = 0;
1420 				dtmp.calls[ct]    = fsa->fsa_digipeater[ct];
1421 				ct++;
1422 			}
1423 
1424 			dtmp.lastrepeat = 0;
1425 		}
1426 
1427 		sax = *usax;
1428 		if (sk->type == SOCK_SEQPACKET && ax25cmp(&sk->protinfo.ax25->dest_addr, &sax.sax25_call) != 0)
1429 			return -EISCONN;
1430 		if (usax->sax25_ndigis == 0)
1431 			dp = NULL;
1432 		else
1433 			dp = &dtmp;
1434 	} else {
1435 		/*
1436 		 *	FIXME: 1003.1g - if the socket is like this because
1437 		 *	it has become closed (not started closed) and is VC
1438 		 *	we ought to SIGPIPE, EPIPE
1439 		 */
1440 		if (sk->state != TCP_ESTABLISHED)
1441 			return -ENOTCONN;
1442 		sax.sax25_family = AF_AX25;
1443 		sax.sax25_call   = sk->protinfo.ax25->dest_addr;
1444 		dp = sk->protinfo.ax25->digipeat;
1445 	}
1446 
1447 	SOCK_DEBUG(sk, "AX.25: sendto: Addresses built.\n");
1448 
1449 	/* Build a packet */
1450 	SOCK_DEBUG(sk, "AX.25: sendto: building packet.\n");
1451 
1452 	/* Assume the worst case */
1453 	size = len + 3 + ax25_addr_size(dp) + AX25_BPQ_HEADER_LEN;
1454 
1455 	if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1456 		return err;
1457 
1458 	skb_reserve(skb, size - len);
1459 
1460 	SOCK_DEBUG(sk, "AX.25: Appending user data\n");
1461 
1462 	/* User data follows immediately after the AX.25 data */
1463 	memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1464 	skb->nh.raw = skb->data;
1465 
1466 	/* Add the PID if one is not supplied by the user in the skb */
1467 	if (!sk->protinfo.ax25->pidincl) {
1468 		asmptr  = skb_push(skb, 1);
1469 		*asmptr = sk->protocol;
1470 	}
1471 
1472 	SOCK_DEBUG(sk, "AX.25: Transmitting buffer\n");
1473 
1474 	if (sk->type == SOCK_SEQPACKET) {
1475 		/* Connected mode sockets go via the LAPB machine */
1476 		if (sk->state != TCP_ESTABLISHED) {
1477 			kfree_skb(skb);
1478 			return -ENOTCONN;
1479 		}
1480 
1481 		ax25_output(sk->protinfo.ax25, sk->protinfo.ax25->paclen, skb);	/* Shove it onto the queue and kick */
1482 
1483 		return len;
1484 	} else {
1485 		asmptr = skb_push(skb, 1 + ax25_addr_size(dp));
1486 
1487 		SOCK_DEBUG(sk, "Building AX.25 Header (dp=%p).\n", dp);
1488 
1489 		if (dp != NULL)
1490 			SOCK_DEBUG(sk, "Num digipeaters=%d\n", dp->ndigi);
1491 
1492 		/* Build an AX.25 header */
1493 		asmptr += (lv = ax25_addr_build(asmptr, &sk->protinfo.ax25->source_addr, &sax.sax25_call, dp, AX25_COMMAND, AX25_MODULUS));
1494 
1495 		SOCK_DEBUG(sk, "Built header (%d bytes)\n",lv);
1496 
1497 		skb->h.raw = asmptr;
1498 
1499 		SOCK_DEBUG(sk, "base=%p pos=%p\n", skb->data, asmptr);
1500 
1501 		*asmptr = AX25_UI;
1502 
1503 		/* Datagram frames go straight out of the door as UI */
1504 		skb->dev      = sk->protinfo.ax25->ax25_dev->dev;
1505 
1506 		ax25_queue_xmit(skb);
1507 
1508 		return len;
1509 	}
1510 }
1511 
ax25_recvmsg(struct socket * sock,struct msghdr * msg,int size,int flags,struct scm_cookie * scm)1512 static int ax25_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags, struct scm_cookie *scm)
1513 {
1514 	struct sock *sk = sock->sk;
1515 	int copied;
1516 	struct sk_buff *skb;
1517 	int er;
1518 
1519 	/*
1520 	 * 	This works for seqpacket too. The receiver has ordered the
1521 	 *	queue for us! We do one quick check first though
1522 	 */
1523 	if (sk->type == SOCK_SEQPACKET && sk->state != TCP_ESTABLISHED)
1524 		return -ENOTCONN;
1525 
1526 	/* Now we can treat all alike */
1527 	if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
1528 		return er;
1529 
1530 	if (!sk->protinfo.ax25->pidincl)
1531 		skb_pull(skb, 1);		/* Remove PID */
1532 
1533 	skb->h.raw = skb->data;
1534 	copied     = skb->len;
1535 
1536 	if (copied > size) {
1537 		copied = size;
1538 		msg->msg_flags |= MSG_TRUNC;
1539 	}
1540 
1541 	skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1542 
1543 	if (msg->msg_namelen != 0) {
1544 		struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1545 		ax25_digi digi;
1546 		ax25_address dest;
1547 
1548 		ax25_addr_parse(skb->mac.raw+1, skb->data-skb->mac.raw-1, NULL, &dest, &digi, NULL, NULL);
1549 
1550 		sax->sax25_family = AF_AX25;
1551 		/* We set this correctly, even though we may not let the
1552 		   application know the digi calls further down (because it
1553 		   did NOT ask to know them).  This could get political... **/
1554 		sax->sax25_ndigis = digi.ndigi;
1555 		sax->sax25_call   = dest;
1556 
1557 		if (sax->sax25_ndigis != 0) {
1558 			int ct;
1559 			struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)sax;
1560 
1561 			for (ct = 0; ct < digi.ndigi; ct++)
1562 				fsa->fsa_digipeater[ct] = digi.calls[ct];
1563 		}
1564 		msg->msg_namelen = sizeof(struct full_sockaddr_ax25);
1565 	}
1566 
1567 	skb_free_datagram(sk, skb);
1568 
1569 	return copied;
1570 }
1571 
ax25_shutdown(struct socket * sk,int how)1572 static int ax25_shutdown(struct socket *sk, int how)
1573 {
1574 	/* FIXME - generate DM and RNR states */
1575 	return -EOPNOTSUPP;
1576 }
1577 
ax25_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)1578 static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1579 {
1580 	struct sock *sk = sock->sk;
1581 
1582 	switch (cmd) {
1583 		case TIOCOUTQ: {
1584 			long amount;
1585 			amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1586 			if (amount < 0)
1587 				amount = 0;
1588 			return put_user(amount, (int *)arg);
1589 		}
1590 
1591 		case TIOCINQ: {
1592 			struct sk_buff *skb;
1593 			long amount = 0L;
1594 			/* These two are safe on a single CPU system as only user tasks fiddle here */
1595 			if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1596 				amount = skb->len;
1597 			return put_user(amount, (int *)arg);
1598 		}
1599 
1600 		case SIOCGSTAMP:
1601 			if (sk != NULL) {
1602 				if (sk->stamp.tv_sec == 0)
1603 					return -ENOENT;
1604 				return copy_to_user((void *)arg, &sk->stamp, sizeof(struct timeval)) ? -EFAULT : 0;
1605 	 		}
1606 			return -EINVAL;
1607 
1608 		case SIOCAX25ADDUID:	/* Add a uid to the uid/call map table */
1609 		case SIOCAX25DELUID:	/* Delete a uid from the uid/call map table */
1610 		case SIOCAX25GETUID: {
1611 			struct sockaddr_ax25 sax25;
1612 			if (copy_from_user(&sax25, (void *)arg, sizeof(sax25)))
1613 				return -EFAULT;
1614 			return ax25_uid_ioctl(cmd, &sax25);
1615 		}
1616 
1617 		case SIOCAX25NOUID: {	/* Set the default policy (default/bar) */
1618 			long amount;
1619 			if (!capable(CAP_NET_ADMIN))
1620 				return -EPERM;
1621 			if (get_user(amount, (long *)arg))
1622 				return -EFAULT;
1623 			if (amount > AX25_NOUID_BLOCK)
1624 				return -EINVAL;
1625 			ax25_uid_policy = amount;
1626 			return 0;
1627 		}
1628 
1629 		case SIOCADDRT:
1630 		case SIOCDELRT:
1631 		case SIOCAX25OPTRT:
1632 			if (!capable(CAP_NET_ADMIN))
1633 				return -EPERM;
1634 			return ax25_rt_ioctl(cmd, (void *)arg);
1635 
1636 		case SIOCAX25CTLCON:
1637 			if (!capable(CAP_NET_ADMIN))
1638 				return -EPERM;
1639 			return ax25_ctl_ioctl(cmd, (void *)arg);
1640 
1641 		case SIOCAX25GETINFO:
1642 		case SIOCAX25GETINFOOLD: {
1643 			struct ax25_info_struct ax25_info;
1644 
1645 			ax25_info.t1        = sk->protinfo.ax25->t1   / HZ;
1646 			ax25_info.t2        = sk->protinfo.ax25->t2   / HZ;
1647 			ax25_info.t3        = sk->protinfo.ax25->t3   / HZ;
1648 			ax25_info.idle      = sk->protinfo.ax25->idle / (60 * HZ);
1649 			ax25_info.n2        = sk->protinfo.ax25->n2;
1650 			ax25_info.t1timer   = ax25_display_timer(&sk->protinfo.ax25->t1timer)   / HZ;
1651 			ax25_info.t2timer   = ax25_display_timer(&sk->protinfo.ax25->t2timer)   / HZ;
1652 			ax25_info.t3timer   = ax25_display_timer(&sk->protinfo.ax25->t3timer)   / HZ;
1653 			ax25_info.idletimer = ax25_display_timer(&sk->protinfo.ax25->idletimer) / (60 * HZ);
1654 			ax25_info.n2count   = sk->protinfo.ax25->n2count;
1655 			ax25_info.state     = sk->protinfo.ax25->state;
1656 			ax25_info.rcv_q     = atomic_read(&sk->rmem_alloc);
1657 			ax25_info.snd_q     = atomic_read(&sk->wmem_alloc);
1658 			ax25_info.vs        = sk->protinfo.ax25->vs;
1659 			ax25_info.vr        = sk->protinfo.ax25->vr;
1660 			ax25_info.va        = sk->protinfo.ax25->va;
1661 			ax25_info.vs_max    = sk->protinfo.ax25->vs; /* reserved */
1662 			ax25_info.paclen    = sk->protinfo.ax25->paclen;
1663 			ax25_info.window    = sk->protinfo.ax25->window;
1664 
1665 			/* old structure? */
1666 			if (cmd == SIOCAX25GETINFOOLD) {
1667 				static int warned = 0;
1668 				if (!warned) {
1669 					printk(KERN_INFO "%s uses old SIOCAX25GETINFO\n",
1670 						current->comm);
1671 					warned=1;
1672 				}
1673 
1674 				if (copy_to_user((void *)arg, &ax25_info, sizeof(struct ax25_info_struct_depreciated)))
1675 					return -EFAULT;
1676 			} else {
1677 				if (copy_to_user((void *)arg, &ax25_info, sizeof(struct ax25_info_struct)))
1678 					return -EINVAL;
1679 			}
1680 			return 0;
1681 		}
1682 
1683 		case SIOCAX25ADDFWD:
1684 		case SIOCAX25DELFWD: {
1685 			struct ax25_fwd_struct ax25_fwd;
1686 			if (!capable(CAP_NET_ADMIN))
1687 				return -EPERM;
1688 			if (copy_from_user(&ax25_fwd, (void *)arg, sizeof(ax25_fwd)))
1689 				return -EFAULT;
1690 			return ax25_fwd_ioctl(cmd, &ax25_fwd);
1691 		}
1692 
1693 		case SIOCGIFADDR:
1694 		case SIOCSIFADDR:
1695 		case SIOCGIFDSTADDR:
1696 		case SIOCSIFDSTADDR:
1697 		case SIOCGIFBRDADDR:
1698 		case SIOCSIFBRDADDR:
1699 		case SIOCGIFNETMASK:
1700 		case SIOCSIFNETMASK:
1701 		case SIOCGIFMETRIC:
1702 		case SIOCSIFMETRIC:
1703 			return -EINVAL;
1704 
1705 		default:
1706 			return dev_ioctl(cmd, (void *)arg);
1707 	}
1708 
1709 	/*NOTREACHED*/
1710 	return 0;
1711 }
1712 
ax25_get_info(char * buffer,char ** start,off_t offset,int length)1713 static int ax25_get_info(char *buffer, char **start, off_t offset, int length)
1714 {
1715 	ax25_cb *ax25;
1716 	int k;
1717 	int len = 0;
1718 	off_t pos = 0;
1719 	off_t begin = 0;
1720 
1721 	cli();
1722 
1723 	/*
1724 	 * New format:
1725 	 * magic dev src_addr dest_addr,digi1,digi2,.. st vs vr va t1 t1 t2 t2 t3 t3 idle idle n2 n2 rtt window paclen Snd-Q Rcv-Q inode
1726 	 */
1727 
1728 	for (ax25 = ax25_list; ax25 != NULL; ax25 = ax25->next) {
1729 		len += sprintf(buffer+len, "%8.8lx %s %s%s ",
1730 				(long) ax25,
1731 				ax25->ax25_dev == NULL? "???" : ax25->ax25_dev->dev->name,
1732 				ax2asc(&ax25->source_addr),
1733 				ax25->iamdigi? "*":"");
1734 
1735 		len += sprintf(buffer+len, "%s", ax2asc(&ax25->dest_addr));
1736 
1737 		for (k=0; (ax25->digipeat != NULL) && (k < ax25->digipeat->ndigi); k++) {
1738 			len += sprintf(buffer+len, ",%s%s",
1739 					ax2asc(&ax25->digipeat->calls[k]),
1740 					ax25->digipeat->repeated[k]? "*":"");
1741 		}
1742 
1743 		len += sprintf(buffer+len, " %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %d %d",
1744 			ax25->state,
1745 			ax25->vs, ax25->vr, ax25->va,
1746 			ax25_display_timer(&ax25->t1timer) / HZ, ax25->t1 / HZ,
1747 			ax25_display_timer(&ax25->t2timer) / HZ, ax25->t2 / HZ,
1748 			ax25_display_timer(&ax25->t3timer) / HZ, ax25->t3 / HZ,
1749 			ax25_display_timer(&ax25->idletimer) / (60 * HZ),
1750 			ax25->idle / (60 * HZ),
1751 			ax25->n2count, ax25->n2,
1752 			ax25->rtt / HZ,
1753 			ax25->window,
1754 			ax25->paclen);
1755 
1756 		if (ax25->sk != NULL) {
1757 			len += sprintf(buffer + len, " %d %d %ld\n",
1758 				atomic_read(&ax25->sk->wmem_alloc),
1759 				atomic_read(&ax25->sk->rmem_alloc),
1760 				ax25->sk->socket != NULL ? ax25->sk->socket->inode->i_ino : 0L);
1761 		} else {
1762 			len += sprintf(buffer + len, " * * *\n");
1763 		}
1764 
1765 		pos = begin + len;
1766 
1767 		if (pos < offset) {
1768 			len   = 0;
1769 			begin = pos;
1770 		}
1771 
1772 		if (pos > offset + length)
1773 			break;
1774 	}
1775 
1776 	sti();
1777 
1778 	*start = buffer + (offset - begin);
1779 	len   -= (offset - begin);
1780 
1781 	if (len > length) len = length;
1782 
1783 	return(len);
1784 }
1785 
1786 static struct net_proto_family ax25_family_ops = {
1787 	family:		PF_AX25,
1788 	create:		ax25_create,
1789 };
1790 
1791 static struct proto_ops SOCKOPS_WRAPPED(ax25_proto_ops) = {
1792 	family:		PF_AX25,
1793 
1794 	release:	ax25_release,
1795 	bind:		ax25_bind,
1796 	connect:	ax25_connect,
1797 	socketpair:	sock_no_socketpair,
1798 	accept:		ax25_accept,
1799 	getname:	ax25_getname,
1800 	poll:		datagram_poll,
1801 	ioctl:		ax25_ioctl,
1802 	listen:		ax25_listen,
1803 	shutdown:	ax25_shutdown,
1804 	setsockopt:	ax25_setsockopt,
1805 	getsockopt:	ax25_getsockopt,
1806 	sendmsg:	ax25_sendmsg,
1807 	recvmsg:	ax25_recvmsg,
1808 	mmap:		sock_no_mmap,
1809 	sendpage:	sock_no_sendpage,
1810 };
1811 
1812 #include <linux/smp_lock.h>
1813 SOCKOPS_WRAP(ax25_proto, PF_AX25);
1814 
1815 /*
1816  *	Called by socket.c on kernel start up
1817  */
1818 static struct packet_type ax25_packet_type = {
1819 	type:		__constant_htons(ETH_P_AX25),
1820 	func:		ax25_kiss_rcv,
1821 };
1822 
1823 static struct notifier_block ax25_dev_notifier = {
1824 	notifier_call:	ax25_device_event,
1825 };
1826 
1827 EXPORT_SYMBOL(ax25_encapsulate);
1828 EXPORT_SYMBOL(ax25_rebuild_header);
1829 EXPORT_SYMBOL(ax25_findbyuid);
1830 EXPORT_SYMBOL(ax25_find_cb);
1831 EXPORT_SYMBOL(ax25_linkfail_register);
1832 EXPORT_SYMBOL(ax25_linkfail_release);
1833 EXPORT_SYMBOL(ax25_listen_register);
1834 EXPORT_SYMBOL(ax25_listen_release);
1835 EXPORT_SYMBOL(ax25_protocol_register);
1836 EXPORT_SYMBOL(ax25_protocol_release);
1837 EXPORT_SYMBOL(ax25_send_frame);
1838 EXPORT_SYMBOL(ax25_uid_policy);
1839 EXPORT_SYMBOL(ax25cmp);
1840 EXPORT_SYMBOL(ax2asc);
1841 EXPORT_SYMBOL(asc2ax);
1842 EXPORT_SYMBOL(null_ax25_address);
1843 EXPORT_SYMBOL(ax25_display_timer);
1844 
1845 static char banner[] __initdata = KERN_INFO "NET4: G4KLX/GW4PTS AX.25 for Linux. Version 0.37 for Linux NET4.0\n";
1846 
ax25_init(void)1847 static int __init ax25_init(void)
1848 {
1849 	sock_register(&ax25_family_ops);
1850 	dev_add_pack(&ax25_packet_type);
1851 	register_netdevice_notifier(&ax25_dev_notifier);
1852 	ax25_register_sysctl();
1853 
1854 	proc_net_create("ax25_route", 0, ax25_rt_get_info);
1855 	proc_net_create("ax25", 0, ax25_get_info);
1856 	proc_net_create("ax25_calls", 0, ax25_uid_get_info);
1857 
1858 	printk(banner);
1859 	return 0;
1860 }
1861 module_init(ax25_init);
1862 
1863 
1864 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1865 MODULE_DESCRIPTION("The amateur radio AX.25 link layer protocol");
1866 MODULE_LICENSE("GPL");
1867 
ax25_exit(void)1868 static void __exit ax25_exit(void)
1869 {
1870 	proc_net_remove("ax25_route");
1871 	proc_net_remove("ax25");
1872 	proc_net_remove("ax25_calls");
1873 	ax25_rt_free();
1874 	ax25_uid_free();
1875 	ax25_dev_free();
1876 
1877 	ax25_unregister_sysctl();
1878 	unregister_netdevice_notifier(&ax25_dev_notifier);
1879 
1880 	dev_remove_pack(&ax25_packet_type);
1881 
1882 	sock_unregister(PF_AX25);
1883 }
1884 module_exit(ax25_exit);
1885