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