- pre3:
[davej-history.git] / net / ax25 / af_ax25.c
blobfc8910f2dee6006be0efe9cd377c548246d9f45f
1 /*
2 * AX.25 release 038
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
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.
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
106 #include <linux/config.h>
107 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
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>
140 ax25_cb *volatile ax25_list = NULL;
142 static struct proto_ops ax25_proto_ops;
145 * Free an allocated ax25 control block. This is done to centralise
146 * the MOD count code.
148 void ax25_free_cb(ax25_cb *ax25)
150 if (ax25->digipeat != NULL) {
151 kfree(ax25->digipeat);
152 ax25->digipeat = NULL;
155 kfree(ax25);
157 MOD_DEC_USE_COUNT;
160 static void ax25_free_sock(struct sock *sk)
162 ax25_free_cb(sk->protinfo.ax25);
166 * Socket removal during an interrupt is now safe.
168 static void ax25_remove_socket(ax25_cb *ax25)
170 ax25_cb *s;
171 unsigned long flags;
173 save_flags(flags); cli();
175 if ((s = ax25_list) == ax25) {
176 ax25_list = s->next;
177 restore_flags(flags);
178 return;
181 while (s != NULL && s->next != NULL) {
182 if (s->next == ax25) {
183 s->next = ax25->next;
184 restore_flags(flags);
185 return;
188 s = s->next;
191 restore_flags(flags);
195 * Kill all bound sockets on a dropped device.
197 static void ax25_kill_by_device(struct net_device *dev)
199 ax25_dev *ax25_dev;
200 ax25_cb *s;
202 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
203 return;
205 for (s = ax25_list; s != NULL; s = s->next) {
206 if (s->ax25_dev == ax25_dev) {
207 s->ax25_dev = NULL;
208 ax25_disconnect(s, ENETUNREACH);
214 * Handle device status changes.
216 static int ax25_device_event(struct notifier_block *this,unsigned long event, void *ptr)
218 struct net_device *dev = (struct net_device *)ptr;
220 /* Reject non AX.25 devices */
221 if (dev->type != ARPHRD_AX25)
222 return NOTIFY_DONE;
224 switch (event) {
225 case NETDEV_UP:
226 ax25_dev_device_up(dev);
227 break;
228 case NETDEV_DOWN:
229 ax25_kill_by_device(dev);
230 ax25_rt_device_down(dev);
231 ax25_dev_device_down(dev);
232 break;
233 default:
234 break;
237 return NOTIFY_DONE;
241 * Add a socket to the bound sockets list.
243 void ax25_insert_socket(ax25_cb *ax25)
245 unsigned long flags;
247 save_flags(flags);
248 cli();
250 ax25->next = ax25_list;
251 ax25_list = ax25;
253 restore_flags(flags);
257 * Find a socket that wants to accept the SABM we have just
258 * received.
260 struct sock *ax25_find_listener(ax25_address *addr, int digi, struct net_device *dev, int type)
262 unsigned long flags;
263 ax25_cb *s;
265 save_flags(flags);
266 cli();
268 for (s = ax25_list; s != NULL; s = s->next) {
269 if ((s->iamdigi && !digi) || (!s->iamdigi && digi))
270 continue;
271 if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 && s->sk->type == type && s->sk->state == TCP_LISTEN) {
272 /* If device is null we match any device */
273 if (s->ax25_dev == NULL || s->ax25_dev->dev == dev) {
274 restore_flags(flags);
275 return s->sk;
280 restore_flags(flags);
281 return NULL;
285 * Find an AX.25 socket given both ends.
287 struct sock *ax25_find_socket(ax25_address *my_addr, ax25_address *dest_addr, int type)
289 ax25_cb *s;
290 unsigned long flags;
292 save_flags(flags);
293 cli();
295 for (s = ax25_list; s != NULL; s = s->next) {
296 if (s->sk != NULL && ax25cmp(&s->source_addr, my_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->sk->type == type) {
297 restore_flags(flags);
298 return s->sk;
302 restore_flags(flags);
304 return NULL;
308 * Find an AX.25 control block given both ends. It will only pick up
309 * floating AX.25 control blocks or non Raw socket bound control blocks.
311 ax25_cb *ax25_find_cb(ax25_address *src_addr, ax25_address *dest_addr, ax25_digi *digi, struct net_device *dev)
313 ax25_cb *s;
314 unsigned long flags;
316 save_flags(flags);
317 cli();
319 for (s = ax25_list; s != NULL; s = s->next) {
320 if (s->sk != NULL && s->sk->type != SOCK_SEQPACKET)
321 continue;
322 if (s->ax25_dev == NULL)
323 continue;
324 if (ax25cmp(&s->source_addr, src_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->ax25_dev->dev == dev) {
325 if (digi != NULL && digi->ndigi != 0) {
326 if (s->digipeat == NULL)
327 continue;
328 if (ax25digicmp(s->digipeat, digi) != 0)
329 continue;
330 } else {
331 if (s->digipeat != NULL && s->digipeat->ndigi != 0)
332 continue;
334 restore_flags(flags);
335 return s;
339 restore_flags(flags);
341 return NULL;
345 * Look for any matching address - RAW sockets can bind to arbitrary names
347 struct sock *ax25_addr_match(ax25_address *addr)
349 unsigned long flags;
350 ax25_cb *s;
352 save_flags(flags);
353 cli();
355 for (s = ax25_list; s != NULL; s = s->next) {
356 if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 && s->sk->type == SOCK_RAW) {
357 restore_flags(flags);
358 return s->sk;
362 restore_flags(flags);
364 return NULL;
367 void ax25_send_to_raw(struct sock *sk, struct sk_buff *skb, int proto)
369 struct sk_buff *copy;
371 while (sk != NULL) {
372 if (sk->type == SOCK_RAW &&
373 sk->protocol == proto &&
374 atomic_read(&sk->rmem_alloc) <= sk->rcvbuf) {
375 if ((copy = skb_clone(skb, GFP_ATOMIC)) == NULL)
376 return;
378 if (sock_queue_rcv_skb(sk, copy) != 0)
379 kfree_skb(copy);
382 sk = sk->next;
387 * Deferred destroy.
389 void ax25_destroy_socket(ax25_cb *);
392 * Handler for deferred kills.
394 static void ax25_destroy_timer(unsigned long data)
396 ax25_destroy_socket((ax25_cb *)data);
400 * This is called from user mode and the timers. Thus it protects itself against
401 * interrupt users but doesn't worry about being called during work.
402 * Once it is removed from the queue no interrupt or bottom half will
403 * touch it and we are (fairly 8-) ) safe.
405 void ax25_destroy_socket(ax25_cb *ax25) /* Not static as it's used by the timer */
407 struct sk_buff *skb;
408 unsigned long flags;
410 save_flags(flags); cli();
412 ax25_stop_heartbeat(ax25);
413 ax25_stop_t1timer(ax25);
414 ax25_stop_t2timer(ax25);
415 ax25_stop_t3timer(ax25);
416 ax25_stop_idletimer(ax25);
418 ax25_remove_socket(ax25);
419 ax25_clear_queues(ax25); /* Flush the queues */
421 if (ax25->sk != NULL) {
422 while ((skb = skb_dequeue(&ax25->sk->receive_queue)) != NULL) {
423 if (skb->sk != ax25->sk) { /* A pending connection */
424 skb->sk->dead = 1; /* Queue the unaccepted socket for death */
425 ax25_start_heartbeat(skb->sk->protinfo.ax25);
426 skb->sk->protinfo.ax25->state = AX25_STATE_0;
429 kfree_skb(skb);
433 if (ax25->sk != NULL) {
434 if (atomic_read(&ax25->sk->wmem_alloc) != 0 ||
435 atomic_read(&ax25->sk->rmem_alloc) != 0) {
436 /* Defer: outstanding buffers */
437 init_timer(&ax25->timer);
438 ax25->timer.expires = jiffies + 10 * HZ;
439 ax25->timer.function = ax25_destroy_timer;
440 ax25->timer.data = (unsigned long)ax25;
441 add_timer(&ax25->timer);
442 } else {
443 sk_free(ax25->sk);
445 } else {
446 ax25_free_cb(ax25);
449 restore_flags(flags);
453 * dl1bke 960311: set parameters for existing AX.25 connections,
454 * includes a KILL command to abort any connection.
455 * VERY useful for debugging ;-)
457 static int ax25_ctl_ioctl(const unsigned int cmd, void *arg)
459 struct ax25_ctl_struct ax25_ctl;
460 ax25_digi digi;
461 ax25_dev *ax25_dev;
462 ax25_cb *ax25;
463 unsigned int k;
465 if (copy_from_user(&ax25_ctl, arg, sizeof(ax25_ctl)))
466 return -EFAULT;
468 if ((ax25_dev = ax25_addr_ax25dev(&ax25_ctl.port_addr)) == NULL)
469 return -ENODEV;
471 if (ax25_ctl.digi_count > AX25_MAX_DIGIS)
472 return -EINVAL;
474 digi.ndigi = ax25_ctl.digi_count;
475 for (k = 0; k < digi.ndigi; k++)
476 digi.calls[k] = ax25_ctl.digi_addr[k];
478 if ((ax25 = ax25_find_cb(&ax25_ctl.source_addr, &ax25_ctl.dest_addr, &digi, ax25_dev->dev)) == NULL)
479 return -ENOTCONN;
481 switch (ax25_ctl.cmd) {
482 case AX25_KILL:
483 ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
484 #ifdef CONFIG_AX25_DAMA_SLAVE
485 if (ax25_dev->dama.slave && ax25->ax25_dev->values[AX25_VALUES_PROTOCOL] == AX25_PROTO_DAMA_SLAVE)
486 ax25_dama_off(ax25);
487 #endif
488 ax25_disconnect(ax25, ENETRESET);
489 break;
491 case AX25_WINDOW:
492 if (ax25->modulus == AX25_MODULUS) {
493 if (ax25_ctl.arg < 1 || ax25_ctl.arg > 7)
494 return -EINVAL;
495 } else {
496 if (ax25_ctl.arg < 1 || ax25_ctl.arg > 63)
497 return -EINVAL;
499 ax25->window = ax25_ctl.arg;
500 break;
502 case AX25_T1:
503 if (ax25_ctl.arg < 1)
504 return -EINVAL;
505 ax25->rtt = (ax25_ctl.arg * HZ) / 2;
506 ax25->t1 = ax25_ctl.arg * HZ;
507 break;
509 case AX25_T2:
510 if (ax25_ctl.arg < 1)
511 return -EINVAL;
512 ax25->t2 = ax25_ctl.arg * HZ;
513 break;
515 case AX25_N2:
516 if (ax25_ctl.arg < 1 || ax25_ctl.arg > 31)
517 return -EINVAL;
518 ax25->n2count = 0;
519 ax25->n2 = ax25_ctl.arg;
520 break;
522 case AX25_T3:
523 if (ax25_ctl.arg < 0)
524 return -EINVAL;
525 ax25->t3 = ax25_ctl.arg * HZ;
526 break;
528 case AX25_IDLE:
529 if (ax25_ctl.arg < 0)
530 return -EINVAL;
531 ax25->idle = ax25_ctl.arg * 60 * HZ;
532 break;
534 case AX25_PACLEN:
535 if (ax25_ctl.arg < 16 || ax25_ctl.arg > 65535)
536 return -EINVAL;
537 ax25->paclen = ax25_ctl.arg;
538 break;
540 default:
541 return -EINVAL;
544 return 0;
548 * Fill in a created AX.25 created control block with the default
549 * values for a particular device.
551 void ax25_fillin_cb(ax25_cb *ax25, ax25_dev *ax25_dev)
553 ax25->ax25_dev = ax25_dev;
555 if (ax25->ax25_dev != NULL) {
556 ax25->rtt = ax25_dev->values[AX25_VALUES_T1] / 2;
557 ax25->t1 = ax25_dev->values[AX25_VALUES_T1];
558 ax25->t2 = ax25_dev->values[AX25_VALUES_T2];
559 ax25->t3 = ax25_dev->values[AX25_VALUES_T3];
560 ax25->n2 = ax25_dev->values[AX25_VALUES_N2];
561 ax25->paclen = ax25_dev->values[AX25_VALUES_PACLEN];
562 ax25->idle = ax25_dev->values[AX25_VALUES_IDLE];
563 ax25->backoff = ax25_dev->values[AX25_VALUES_BACKOFF];
565 if (ax25_dev->values[AX25_VALUES_AXDEFMODE]) {
566 ax25->modulus = AX25_EMODULUS;
567 ax25->window = ax25_dev->values[AX25_VALUES_EWINDOW];
568 } else {
569 ax25->modulus = AX25_MODULUS;
570 ax25->window = ax25_dev->values[AX25_VALUES_WINDOW];
572 } else {
573 ax25->rtt = AX25_DEF_T1 / 2;
574 ax25->t1 = AX25_DEF_T1;
575 ax25->t2 = AX25_DEF_T2;
576 ax25->t3 = AX25_DEF_T3;
577 ax25->n2 = AX25_DEF_N2;
578 ax25->paclen = AX25_DEF_PACLEN;
579 ax25->idle = AX25_DEF_IDLE;
580 ax25->backoff = AX25_DEF_BACKOFF;
582 if (AX25_DEF_AXDEFMODE) {
583 ax25->modulus = AX25_EMODULUS;
584 ax25->window = AX25_DEF_EWINDOW;
585 } else {
586 ax25->modulus = AX25_MODULUS;
587 ax25->window = AX25_DEF_WINDOW;
593 * Create an empty AX.25 control block.
595 ax25_cb *ax25_create_cb(void)
597 ax25_cb *ax25;
599 if ((ax25 = kmalloc(sizeof(*ax25), GFP_ATOMIC)) == NULL)
600 return NULL;
602 MOD_INC_USE_COUNT;
604 memset(ax25, 0x00, sizeof(*ax25));
606 skb_queue_head_init(&ax25->write_queue);
607 skb_queue_head_init(&ax25->frag_queue);
608 skb_queue_head_init(&ax25->ack_queue);
609 skb_queue_head_init(&ax25->reseq_queue);
611 init_timer(&ax25->timer);
612 init_timer(&ax25->t1timer);
613 init_timer(&ax25->t2timer);
614 init_timer(&ax25->t3timer);
615 init_timer(&ax25->idletimer);
617 ax25_fillin_cb(ax25, NULL);
619 ax25->state = AX25_STATE_0;
621 return ax25;
625 * Handling for system calls applied via the various interfaces to an
626 * AX25 socket object
629 static int ax25_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
631 struct sock *sk = sock->sk;
632 struct net_device *dev;
633 char devname[IFNAMSIZ];
634 int opt;
636 if (level != SOL_AX25)
637 return -ENOPROTOOPT;
639 if (optlen < sizeof(int))
640 return -EINVAL;
642 if (get_user(opt, (int *)optval))
643 return -EFAULT;
645 switch (optname) {
646 case AX25_WINDOW:
647 if (sk->protinfo.ax25->modulus == AX25_MODULUS) {
648 if (opt < 1 || opt > 7)
649 return -EINVAL;
650 } else {
651 if (opt < 1 || opt > 63)
652 return -EINVAL;
654 sk->protinfo.ax25->window = opt;
655 return 0;
657 case AX25_T1:
658 if (opt < 1)
659 return -EINVAL;
660 sk->protinfo.ax25->rtt = (opt * HZ) / 2;
661 sk->protinfo.ax25->t1 = opt * HZ;
662 return 0;
664 case AX25_T2:
665 if (opt < 1)
666 return -EINVAL;
667 sk->protinfo.ax25->t2 = opt * HZ;
668 return 0;
670 case AX25_N2:
671 if (opt < 1 || opt > 31)
672 return -EINVAL;
673 sk->protinfo.ax25->n2 = opt;
674 return 0;
676 case AX25_T3:
677 if (opt < 1)
678 return -EINVAL;
679 sk->protinfo.ax25->t3 = opt * HZ;
680 return 0;
682 case AX25_IDLE:
683 if (opt < 0)
684 return -EINVAL;
685 sk->protinfo.ax25->idle = opt * 60 * HZ;
686 return 0;
688 case AX25_BACKOFF:
689 if (opt < 0 || opt > 2)
690 return -EINVAL;
691 sk->protinfo.ax25->backoff = opt;
692 return 0;
694 case AX25_EXTSEQ:
695 sk->protinfo.ax25->modulus = opt ? AX25_EMODULUS : AX25_MODULUS;
696 return 0;
698 case AX25_PIDINCL:
699 sk->protinfo.ax25->pidincl = opt ? 1 : 0;
700 return 0;
702 case AX25_IAMDIGI:
703 sk->protinfo.ax25->iamdigi = opt ? 1 : 0;
704 return 0;
706 case AX25_PACLEN:
707 if (opt < 16 || opt > 65535)
708 return -EINVAL;
709 sk->protinfo.ax25->paclen = opt;
710 return 0;
712 case SO_BINDTODEVICE:
713 if (optlen > IFNAMSIZ) optlen=IFNAMSIZ;
714 if (copy_from_user(devname, optval, optlen))
715 return -EFAULT;
717 dev = dev_get_by_name(devname);
718 if (dev == NULL) return -ENODEV;
720 if (sk->type == SOCK_SEQPACKET &&
721 (sock->state != SS_UNCONNECTED || sk->state == TCP_LISTEN))
722 return -EADDRNOTAVAIL;
724 sk->protinfo.ax25->ax25_dev = ax25_dev_ax25dev(dev);
725 ax25_fillin_cb(sk->protinfo.ax25, sk->protinfo.ax25->ax25_dev);
726 return 0;
728 default:
729 return -ENOPROTOOPT;
733 static int ax25_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
735 struct sock *sk = sock->sk;
736 struct ax25_dev *ax25_dev;
737 char devname[IFNAMSIZ];
738 void *valptr;
739 int val = 0;
740 int maxlen, length;
742 if (level != SOL_AX25)
743 return -ENOPROTOOPT;
745 if (get_user(maxlen, optlen))
746 return -EFAULT;
748 if (maxlen < 1)
749 return -EFAULT;
751 valptr = (void *) &val;
752 length = min(maxlen, sizeof(int));
754 switch (optname) {
755 case AX25_WINDOW:
756 val = sk->protinfo.ax25->window;
757 break;
759 case AX25_T1:
760 val = sk->protinfo.ax25->t1 / HZ;
761 break;
763 case AX25_T2:
764 val = sk->protinfo.ax25->t2 / HZ;
765 break;
767 case AX25_N2:
768 val = sk->protinfo.ax25->n2;
769 break;
771 case AX25_T3:
772 val = sk->protinfo.ax25->t3 / HZ;
773 break;
775 case AX25_IDLE:
776 val = sk->protinfo.ax25->idle / (60 * HZ);
777 break;
779 case AX25_BACKOFF:
780 val = sk->protinfo.ax25->backoff;
781 break;
783 case AX25_EXTSEQ:
784 val = (sk->protinfo.ax25->modulus == AX25_EMODULUS);
785 break;
787 case AX25_PIDINCL:
788 val = sk->protinfo.ax25->pidincl;
789 break;
791 case AX25_IAMDIGI:
792 val = sk->protinfo.ax25->iamdigi;
793 break;
795 case AX25_PACLEN:
796 val = sk->protinfo.ax25->paclen;
797 break;
799 case SO_BINDTODEVICE:
800 ax25_dev = sk->protinfo.ax25->ax25_dev;
802 if (ax25_dev != NULL && ax25_dev->dev != NULL) {
803 strncpy(devname, ax25_dev->dev->name, IFNAMSIZ);
804 length = min(strlen(ax25_dev->dev->name)+1, maxlen);
805 devname[length-1] = '\0';
806 } else {
807 *devname = '\0';
808 length = 1;
811 valptr = (void *) devname;
812 break;
814 default:
815 return -ENOPROTOOPT;
818 if (put_user(length, optlen))
819 return -EFAULT;
821 return copy_to_user(optval, valptr, length) ? -EFAULT : 0;
824 static int ax25_listen(struct socket *sock, int backlog)
826 struct sock *sk = sock->sk;
828 if (sk->type == SOCK_SEQPACKET && sk->state != TCP_LISTEN) {
829 sk->max_ack_backlog = backlog;
830 sk->state = TCP_LISTEN;
831 return 0;
834 return -EOPNOTSUPP;
837 int ax25_create(struct socket *sock, int protocol)
839 struct sock *sk;
840 ax25_cb *ax25;
842 switch (sock->type) {
843 case SOCK_DGRAM:
844 if (protocol == 0 || protocol == PF_AX25)
845 protocol = AX25_P_TEXT;
846 break;
847 case SOCK_SEQPACKET:
848 switch (protocol) {
849 case 0:
850 case PF_AX25: /* For CLX */
851 protocol = AX25_P_TEXT;
852 break;
853 case AX25_P_SEGMENT:
854 #ifdef CONFIG_INET
855 case AX25_P_ARP:
856 case AX25_P_IP:
857 #endif
858 #ifdef CONFIG_NETROM
859 case AX25_P_NETROM:
860 #endif
861 #ifdef CONFIG_ROSE
862 case AX25_P_ROSE:
863 #endif
864 return -ESOCKTNOSUPPORT;
865 #ifdef CONFIG_NETROM_MODULE
866 case AX25_P_NETROM:
867 if (ax25_protocol_is_registered(AX25_P_NETROM))
868 return -ESOCKTNOSUPPORT;
869 #endif
870 #ifdef CONFIG_ROSE_MODULE
871 case AX25_P_ROSE:
872 if (ax25_protocol_is_registered(AX25_P_ROSE))
873 return -ESOCKTNOSUPPORT;
874 #endif
875 default:
876 break;
878 break;
879 case SOCK_RAW:
880 break;
881 default:
882 return -ESOCKTNOSUPPORT;
885 if ((sk = sk_alloc(PF_AX25, GFP_ATOMIC, 1)) == NULL)
886 return -ENOMEM;
888 if ((ax25 = ax25_create_cb()) == NULL) {
889 sk_free(sk);
890 return -ENOMEM;
893 sock_init_data(sock, sk);
895 sk->destruct = ax25_free_sock;
896 sock->ops = &ax25_proto_ops;
897 sk->protocol = protocol;
899 ax25->sk = sk;
900 sk->protinfo.ax25 = ax25;
902 return 0;
905 struct sock *ax25_make_new(struct sock *osk, struct ax25_dev *ax25_dev)
907 struct sock *sk;
908 ax25_cb *ax25;
910 if ((sk = sk_alloc(PF_AX25, GFP_ATOMIC, 1)) == NULL)
911 return NULL;
913 if ((ax25 = ax25_create_cb()) == NULL) {
914 sk_free(sk);
915 return NULL;
918 switch (osk->type) {
919 case SOCK_DGRAM:
920 break;
921 case SOCK_SEQPACKET:
922 break;
923 default:
924 sk_free(sk);
925 ax25_free_cb(ax25);
926 return NULL;
929 sock_init_data(NULL, sk);
931 sk->destruct = ax25_free_sock;
932 sk->type = osk->type;
933 sk->socket = osk->socket;
934 sk->priority = osk->priority;
935 sk->protocol = osk->protocol;
936 sk->rcvbuf = osk->rcvbuf;
937 sk->sndbuf = osk->sndbuf;
938 sk->debug = osk->debug;
939 sk->state = TCP_ESTABLISHED;
940 sk->sleep = osk->sleep;
941 sk->zapped = osk->zapped;
943 ax25->modulus = osk->protinfo.ax25->modulus;
944 ax25->backoff = osk->protinfo.ax25->backoff;
945 ax25->pidincl = osk->protinfo.ax25->pidincl;
946 ax25->iamdigi = osk->protinfo.ax25->iamdigi;
947 ax25->rtt = osk->protinfo.ax25->rtt;
948 ax25->t1 = osk->protinfo.ax25->t1;
949 ax25->t2 = osk->protinfo.ax25->t2;
950 ax25->t3 = osk->protinfo.ax25->t3;
951 ax25->n2 = osk->protinfo.ax25->n2;
952 ax25->idle = osk->protinfo.ax25->idle;
953 ax25->paclen = osk->protinfo.ax25->paclen;
954 ax25->window = osk->protinfo.ax25->window;
956 ax25->ax25_dev = ax25_dev;
957 ax25->source_addr = osk->protinfo.ax25->source_addr;
959 if (osk->protinfo.ax25->digipeat != NULL) {
960 if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
961 sk_free(sk);
962 return NULL;
965 memcpy(ax25->digipeat, osk->protinfo.ax25->digipeat, sizeof(ax25_digi));
968 sk->protinfo.ax25 = ax25;
969 ax25->sk = sk;
971 return sk;
974 static int ax25_release(struct socket *sock)
976 struct sock *sk = sock->sk;
978 if (sk == NULL) return 0;
980 if (sk->type == SOCK_SEQPACKET) {
981 switch (sk->protinfo.ax25->state) {
982 case AX25_STATE_0:
983 ax25_disconnect(sk->protinfo.ax25, 0);
984 ax25_destroy_socket(sk->protinfo.ax25);
985 break;
987 case AX25_STATE_1:
988 case AX25_STATE_2:
989 ax25_send_control(sk->protinfo.ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
990 ax25_disconnect(sk->protinfo.ax25, 0);
991 ax25_destroy_socket(sk->protinfo.ax25);
992 break;
994 case AX25_STATE_3:
995 case AX25_STATE_4:
996 ax25_clear_queues(sk->protinfo.ax25);
997 sk->protinfo.ax25->n2count = 0;
998 switch (sk->protinfo.ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
999 case AX25_PROTO_STD_SIMPLEX:
1000 case AX25_PROTO_STD_DUPLEX:
1001 ax25_send_control(sk->protinfo.ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
1002 ax25_stop_t2timer(sk->protinfo.ax25);
1003 ax25_stop_t3timer(sk->protinfo.ax25);
1004 ax25_stop_idletimer(sk->protinfo.ax25);
1005 break;
1006 #ifdef CONFIG_AX25_DAMA_SLAVE
1007 case AX25_PROTO_DAMA_SLAVE:
1008 ax25_stop_t3timer(sk->protinfo.ax25);
1009 ax25_stop_idletimer(sk->protinfo.ax25);
1010 break;
1011 #endif
1013 ax25_calculate_t1(sk->protinfo.ax25);
1014 ax25_start_t1timer(sk->protinfo.ax25);
1015 sk->protinfo.ax25->state = AX25_STATE_2;
1016 sk->state = TCP_CLOSE;
1017 sk->shutdown |= SEND_SHUTDOWN;
1018 sk->state_change(sk);
1019 sk->dead = 1;
1020 sk->destroy = 1;
1021 break;
1023 default:
1024 break;
1026 } else {
1027 sk->state = TCP_CLOSE;
1028 sk->shutdown |= SEND_SHUTDOWN;
1029 sk->state_change(sk);
1030 sk->dead = 1;
1031 ax25_destroy_socket(sk->protinfo.ax25);
1034 sock->sk = NULL;
1035 sk->socket = NULL; /* Not used, but we should do this */
1037 return 0;
1041 * We support a funny extension here so you can (as root) give any callsign
1042 * digipeated via a local address as source. This hack is obsolete now
1043 * that we've implemented support for SO_BINDTODEVICE. It is however small
1044 * and trivially backward compatible.
1046 static int ax25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1048 struct sock *sk = sock->sk;
1049 struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
1050 ax25_address *call;
1051 ax25_dev *ax25_dev = NULL;
1053 if (sk->zapped == 0)
1054 return -EINVAL;
1056 if (addr_len != sizeof(struct sockaddr_ax25) &&
1057 addr_len != sizeof(struct full_sockaddr_ax25)) {
1058 /* support for old structure may go away some time */
1059 if ((addr_len < sizeof(struct sockaddr_ax25) + sizeof(ax25_address) * 6) ||
1060 (addr_len > sizeof(struct full_sockaddr_ax25)))
1061 return -EINVAL;
1063 printk(KERN_WARNING "ax25_bind(): %s uses old (6 digipeater) socket structure.\n",
1064 current->comm);
1067 if (addr->fsa_ax25.sax25_family != AF_AX25)
1068 return -EINVAL;
1070 call = ax25_findbyuid(current->euid);
1071 if (call == NULL && ax25_uid_policy && !capable(CAP_NET_ADMIN))
1072 return -EACCES;
1074 if (call == NULL)
1075 sk->protinfo.ax25->source_addr = addr->fsa_ax25.sax25_call;
1076 else
1077 sk->protinfo.ax25->source_addr = *call;
1080 * User already set interface with SO_BINDTODEVICE
1083 if (sk->protinfo.ax25->ax25_dev != NULL)
1084 goto done;
1086 if (addr_len > sizeof(struct sockaddr_ax25) && addr->fsa_ax25.sax25_ndigis == 1) {
1087 if (ax25cmp(&addr->fsa_digipeater[0], &null_ax25_address) != 0 &&
1088 (ax25_dev = ax25_addr_ax25dev(&addr->fsa_digipeater[0])) == NULL)
1089 return -EADDRNOTAVAIL;
1090 } else {
1091 if ((ax25_dev = ax25_addr_ax25dev(&addr->fsa_ax25.sax25_call)) == NULL)
1092 return -EADDRNOTAVAIL;
1095 if (ax25_dev != NULL)
1096 ax25_fillin_cb(sk->protinfo.ax25, ax25_dev);
1098 done:
1099 ax25_insert_socket(sk->protinfo.ax25);
1100 sk->zapped = 0;
1101 return 0;
1105 * FIXME: nonblock behaviour looks like it may have a bug.
1107 static int ax25_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
1109 struct sock *sk = sock->sk;
1110 struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)uaddr;
1111 ax25_digi *digi = NULL;
1112 int ct = 0, err;
1114 /* deal with restarts */
1115 if (sock->state == SS_CONNECTING) {
1116 switch (sk->state) {
1117 case TCP_SYN_SENT: /* still trying */
1118 return -EINPROGRESS;
1120 case TCP_ESTABLISHED: /* connection established */
1121 sock->state = SS_CONNECTED;
1122 return 0;
1124 case TCP_CLOSE: /* connection refused */
1125 sock->state = SS_UNCONNECTED;
1126 return -ECONNREFUSED;
1130 if (sk->state == TCP_ESTABLISHED && sk->type == SOCK_SEQPACKET)
1131 return -EISCONN; /* No reconnect on a seqpacket socket */
1133 sk->state = TCP_CLOSE;
1134 sock->state = SS_UNCONNECTED;
1137 * some sanity checks. code further down depends on this
1140 if (addr_len == sizeof(struct sockaddr_ax25)) {
1141 /* support for this will go away in early 2.5.x */
1142 printk(KERN_WARNING "ax25_connect(): %s uses obsolete socket structure\n",
1143 current->comm);
1145 else if (addr_len != sizeof(struct full_sockaddr_ax25)) {
1146 /* support for old structure may go away some time */
1147 if ((addr_len < sizeof(struct sockaddr_ax25) + sizeof(ax25_address) * 6) ||
1148 (addr_len > sizeof(struct full_sockaddr_ax25)))
1149 return -EINVAL;
1151 printk(KERN_WARNING "ax25_connect(): %s uses old (6 digipeater) socket structure.\n",
1152 current->comm);
1155 if (fsa->fsa_ax25.sax25_family != AF_AX25)
1156 return -EINVAL;
1158 if (sk->protinfo.ax25->digipeat != NULL) {
1159 kfree(sk->protinfo.ax25->digipeat);
1160 sk->protinfo.ax25->digipeat = NULL;
1164 * Handle digi-peaters to be used.
1166 if (addr_len > sizeof(struct sockaddr_ax25) && fsa->fsa_ax25.sax25_ndigis != 0) {
1167 /* Valid number of digipeaters ? */
1168 if (fsa->fsa_ax25.sax25_ndigis < 1 || fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS)
1169 return -EINVAL;
1171 if ((digi = kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL)
1172 return -ENOBUFS;
1174 digi->ndigi = fsa->fsa_ax25.sax25_ndigis;
1175 digi->lastrepeat = -1;
1177 while (ct < fsa->fsa_ax25.sax25_ndigis) {
1178 if ((fsa->fsa_digipeater[ct].ax25_call[6] & AX25_HBIT) && sk->protinfo.ax25->iamdigi) {
1179 digi->repeated[ct] = 1;
1180 digi->lastrepeat = ct;
1181 } else {
1182 digi->repeated[ct] = 0;
1184 digi->calls[ct] = fsa->fsa_digipeater[ct];
1185 ct++;
1190 * Must bind first - autobinding in this may or may not work. If
1191 * the socket is already bound, check to see if the device has
1192 * been filled in, error if it hasn't.
1194 if (sk->zapped) {
1195 /* check if we can remove this feature. It is broken. */
1196 printk(KERN_WARNING "ax25_connect(): %s uses autobind, please contact jreuter@poboxes.com\n",
1197 current->comm);
1198 if ((err = ax25_rt_autobind(sk->protinfo.ax25, &fsa->fsa_ax25.sax25_call)) < 0)
1199 return err;
1200 ax25_fillin_cb(sk->protinfo.ax25, sk->protinfo.ax25->ax25_dev);
1201 ax25_insert_socket(sk->protinfo.ax25);
1202 } else {
1203 if (sk->protinfo.ax25->ax25_dev == NULL)
1204 return -EHOSTUNREACH;
1207 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) {
1208 if (digi != NULL) kfree(digi);
1209 return -EADDRINUSE; /* Already such a connection */
1212 sk->protinfo.ax25->dest_addr = fsa->fsa_ax25.sax25_call;
1213 sk->protinfo.ax25->digipeat = digi;
1215 /* First the easy one */
1216 if (sk->type != SOCK_SEQPACKET) {
1217 sock->state = SS_CONNECTED;
1218 sk->state = TCP_ESTABLISHED;
1219 return 0;
1222 /* Move to connecting socket, ax.25 lapb WAIT_UA.. */
1223 sock->state = SS_CONNECTING;
1224 sk->state = TCP_SYN_SENT;
1226 switch (sk->protinfo.ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
1227 case AX25_PROTO_STD_SIMPLEX:
1228 case AX25_PROTO_STD_DUPLEX:
1229 ax25_std_establish_data_link(sk->protinfo.ax25);
1230 break;
1232 #ifdef CONFIG_AX25_DAMA_SLAVE
1233 case AX25_PROTO_DAMA_SLAVE:
1234 sk->protinfo.ax25->modulus = AX25_MODULUS;
1235 sk->protinfo.ax25->window = sk->protinfo.ax25->ax25_dev->values[AX25_VALUES_WINDOW];
1236 if (sk->protinfo.ax25->ax25_dev->dama.slave)
1237 ax25_ds_establish_data_link(sk->protinfo.ax25);
1238 else
1239 ax25_std_establish_data_link(sk->protinfo.ax25);
1240 break;
1241 #endif
1244 sk->protinfo.ax25->state = AX25_STATE_1;
1246 ax25_start_heartbeat(sk->protinfo.ax25);
1248 /* Now the loop */
1249 if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
1250 return -EINPROGRESS;
1252 cli(); /* To avoid races on the sleep */
1254 /* A DM or timeout will go to closed, a UA will go to ABM */
1255 while (sk->state == TCP_SYN_SENT) {
1256 interruptible_sleep_on(sk->sleep);
1257 if (signal_pending(current)) {
1258 sti();
1259 return -ERESTARTSYS;
1263 if (sk->state != TCP_ESTABLISHED) {
1264 /* Not in ABM, not in WAIT_UA -> failed */
1265 sti();
1266 sock->state = SS_UNCONNECTED;
1267 return sock_error(sk); /* Always set at this point */
1270 sock->state = SS_CONNECTED;
1272 sti();
1274 return 0;
1278 static int ax25_accept(struct socket *sock, struct socket *newsock, int flags)
1280 struct sock *sk;
1281 struct sock *newsk;
1282 struct sk_buff *skb;
1284 if (sock->state != SS_UNCONNECTED)
1285 return -EINVAL;
1287 if ((sk = sock->sk) == NULL)
1288 return -EINVAL;
1290 if (sk->type != SOCK_SEQPACKET)
1291 return -EOPNOTSUPP;
1293 if (sk->state != TCP_LISTEN)
1294 return -EINVAL;
1297 * The read queue this time is holding sockets ready to use
1298 * hooked into the SABM we saved
1300 do {
1301 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
1302 if (flags & O_NONBLOCK)
1303 return -EWOULDBLOCK;
1305 interruptible_sleep_on(sk->sleep);
1306 if (signal_pending(current))
1307 return -ERESTARTSYS;
1309 } while (skb == NULL);
1311 newsk = skb->sk;
1312 newsk->pair = NULL;
1313 newsk->socket = newsock;
1314 newsk->sleep = &newsock->wait;
1316 /* Now attach up the new socket */
1317 kfree_skb(skb);
1318 sk->ack_backlog--;
1319 newsock->sk = newsk;
1320 newsock->state = SS_CONNECTED;
1322 return 0;
1325 static int ax25_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
1327 struct sock *sk = sock->sk;
1328 struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)uaddr;
1329 unsigned char ndigi, i;
1331 if (peer != 0) {
1332 if (sk->state != TCP_ESTABLISHED)
1333 return -ENOTCONN;
1335 fsa->fsa_ax25.sax25_family = AF_AX25;
1336 fsa->fsa_ax25.sax25_call = sk->protinfo.ax25->dest_addr;
1337 fsa->fsa_ax25.sax25_ndigis = 0;
1339 if (sk->protinfo.ax25->digipeat != NULL) {
1340 ndigi = sk->protinfo.ax25->digipeat->ndigi;
1341 fsa->fsa_ax25.sax25_ndigis = ndigi;
1342 for (i = 0; i < ndigi; i++)
1343 fsa->fsa_digipeater[i] = sk->protinfo.ax25->digipeat->calls[i];
1345 } else {
1346 fsa->fsa_ax25.sax25_family = AF_AX25;
1347 fsa->fsa_ax25.sax25_call = sk->protinfo.ax25->source_addr;
1348 fsa->fsa_ax25.sax25_ndigis = 1;
1349 if (sk->protinfo.ax25->ax25_dev != NULL) {
1350 memcpy(&fsa->fsa_digipeater[0], sk->protinfo.ax25->ax25_dev->dev->dev_addr, AX25_ADDR_LEN);
1351 } else {
1352 fsa->fsa_digipeater[0] = null_ax25_address;
1355 *uaddr_len = sizeof (struct full_sockaddr_ax25);
1356 return 0;
1359 static int ax25_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
1361 struct sock *sk = sock->sk;
1362 struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1363 int err;
1364 struct sockaddr_ax25 sax;
1365 struct sk_buff *skb;
1366 unsigned char *asmptr;
1367 int size;
1368 ax25_digi *dp;
1369 ax25_digi dtmp;
1370 int lv;
1371 int addr_len = msg->msg_namelen;
1373 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR))
1374 return -EINVAL;
1376 if (sk->zapped)
1377 return -EADDRNOTAVAIL;
1379 if (sk->shutdown & SEND_SHUTDOWN) {
1380 send_sig(SIGPIPE, current, 0);
1381 return -EPIPE;
1384 if (sk->protinfo.ax25->ax25_dev == NULL)
1385 return -ENETUNREACH;
1387 if (usax != NULL) {
1388 if (usax->sax25_family != AF_AX25)
1389 return -EINVAL;
1391 if (addr_len == sizeof(struct sockaddr_ax25)) {
1392 printk(KERN_WARNING "ax25_sendmsg(): %s uses obsolete socket structure\n",
1393 current->comm);
1395 else if (addr_len != sizeof(struct full_sockaddr_ax25)) {
1396 /* support for old structure may go away some time */
1397 if ((addr_len < sizeof(struct sockaddr_ax25) + sizeof(ax25_address) * 6) ||
1398 (addr_len > sizeof(struct full_sockaddr_ax25)))
1399 return -EINVAL;
1401 printk(KERN_WARNING "ax25_sendmsg(): %s uses old (6 digipeater) socket structure.\n",
1402 current->comm);
1405 if (addr_len > sizeof(struct sockaddr_ax25) && usax->sax25_ndigis != 0) {
1406 int ct = 0;
1407 struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)usax;
1409 /* Valid number of digipeaters ? */
1410 if (usax->sax25_ndigis < 1 || usax->sax25_ndigis > AX25_MAX_DIGIS)
1411 return -EINVAL;
1413 dtmp.ndigi = usax->sax25_ndigis;
1415 while (ct < usax->sax25_ndigis) {
1416 dtmp.repeated[ct] = 0;
1417 dtmp.calls[ct] = fsa->fsa_digipeater[ct];
1418 ct++;
1421 dtmp.lastrepeat = 0;
1424 sax = *usax;
1425 if (sk->type == SOCK_SEQPACKET && ax25cmp(&sk->protinfo.ax25->dest_addr, &sax.sax25_call) != 0)
1426 return -EISCONN;
1427 if (usax->sax25_ndigis == 0)
1428 dp = NULL;
1429 else
1430 dp = &dtmp;
1431 } else {
1433 * FIXME: 1003.1g - if the socket is like this because
1434 * it has become closed (not started closed) and is VC
1435 * we ought to SIGPIPE, EPIPE
1437 if (sk->state != TCP_ESTABLISHED)
1438 return -ENOTCONN;
1439 sax.sax25_family = AF_AX25;
1440 sax.sax25_call = sk->protinfo.ax25->dest_addr;
1441 dp = sk->protinfo.ax25->digipeat;
1444 SOCK_DEBUG(sk, "AX.25: sendto: Addresses built.\n");
1446 /* Build a packet */
1447 SOCK_DEBUG(sk, "AX.25: sendto: building packet.\n");
1449 /* Assume the worst case */
1450 size = len + 3 + ax25_addr_size(dp) + AX25_BPQ_HEADER_LEN;
1452 if ((skb = sock_alloc_send_skb(sk, size, 0, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1453 return err;
1455 skb_reserve(skb, size - len);
1457 SOCK_DEBUG(sk, "AX.25: Appending user data\n");
1459 /* User data follows immediately after the AX.25 data */
1460 memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1461 skb->nh.raw = skb->data;
1463 /* Add the PID if one is not supplied by the user in the skb */
1464 if (!sk->protinfo.ax25->pidincl) {
1465 asmptr = skb_push(skb, 1);
1466 *asmptr = sk->protocol;
1469 SOCK_DEBUG(sk, "AX.25: Transmitting buffer\n");
1471 if (sk->type == SOCK_SEQPACKET) {
1472 /* Connected mode sockets go via the LAPB machine */
1473 if (sk->state != TCP_ESTABLISHED) {
1474 kfree_skb(skb);
1475 return -ENOTCONN;
1478 ax25_output(sk->protinfo.ax25, sk->protinfo.ax25->paclen, skb); /* Shove it onto the queue and kick */
1480 return len;
1481 } else {
1482 asmptr = skb_push(skb, 1 + ax25_addr_size(dp));
1484 SOCK_DEBUG(sk, "Building AX.25 Header (dp=%p).\n", dp);
1486 if (dp != NULL)
1487 SOCK_DEBUG(sk, "Num digipeaters=%d\n", dp->ndigi);
1489 /* Build an AX.25 header */
1490 asmptr += (lv = ax25_addr_build(asmptr, &sk->protinfo.ax25->source_addr, &sax.sax25_call, dp, AX25_COMMAND, AX25_MODULUS));
1492 SOCK_DEBUG(sk, "Built header (%d bytes)\n",lv);
1494 skb->h.raw = asmptr;
1496 SOCK_DEBUG(sk, "base=%p pos=%p\n", skb->data, asmptr);
1498 *asmptr = AX25_UI;
1500 /* Datagram frames go straight out of the door as UI */
1501 skb->dev = sk->protinfo.ax25->ax25_dev->dev;
1503 ax25_queue_xmit(skb);
1505 return len;
1509 static int ax25_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags, struct scm_cookie *scm)
1511 struct sock *sk = sock->sk;
1512 int copied;
1513 struct sk_buff *skb;
1514 int er;
1517 * This works for seqpacket too. The receiver has ordered the
1518 * queue for us! We do one quick check first though
1520 if (sk->type == SOCK_SEQPACKET && sk->state != TCP_ESTABLISHED)
1521 return -ENOTCONN;
1523 /* Now we can treat all alike */
1524 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
1525 return er;
1527 if (!sk->protinfo.ax25->pidincl)
1528 skb_pull(skb, 1); /* Remove PID */
1530 skb->h.raw = skb->data;
1531 copied = skb->len;
1533 if (copied > size) {
1534 copied = size;
1535 msg->msg_flags |= MSG_TRUNC;
1538 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1540 if (msg->msg_namelen != 0) {
1541 struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1542 ax25_digi digi;
1543 ax25_address dest;
1545 ax25_addr_parse(skb->mac.raw+1, skb->data-skb->mac.raw-1, NULL, &dest, &digi, NULL, NULL);
1547 sax->sax25_family = AF_AX25;
1548 /* We set this correctly, even though we may not let the
1549 application know the digi calls further down (because it
1550 did NOT ask to know them). This could get political... **/
1551 sax->sax25_ndigis = digi.ndigi;
1552 sax->sax25_call = dest;
1554 if (sax->sax25_ndigis != 0) {
1555 int ct;
1556 struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)sax;
1558 for (ct = 0; ct < digi.ndigi; ct++)
1559 fsa->fsa_digipeater[ct] = digi.calls[ct];
1561 msg->msg_namelen = sizeof(struct full_sockaddr_ax25);
1564 skb_free_datagram(sk, skb);
1566 return copied;
1569 static int ax25_shutdown(struct socket *sk, int how)
1571 /* FIXME - generate DM and RNR states */
1572 return -EOPNOTSUPP;
1575 static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1577 struct sock *sk = sock->sk;
1579 switch (cmd) {
1580 case TIOCOUTQ: {
1581 long amount;
1582 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1583 if (amount < 0)
1584 amount = 0;
1585 return put_user(amount, (int *)arg);
1588 case TIOCINQ: {
1589 struct sk_buff *skb;
1590 long amount = 0L;
1591 /* These two are safe on a single CPU system as only user tasks fiddle here */
1592 if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1593 amount = skb->len;
1594 return put_user(amount, (int *)arg);
1597 case SIOCGSTAMP:
1598 if (sk != NULL) {
1599 if (sk->stamp.tv_sec == 0)
1600 return -ENOENT;
1601 return copy_to_user((void *)arg, &sk->stamp, sizeof(struct timeval)) ? -EFAULT : 0;
1603 return -EINVAL;
1605 case SIOCAX25ADDUID: /* Add a uid to the uid/call map table */
1606 case SIOCAX25DELUID: /* Delete a uid from the uid/call map table */
1607 case SIOCAX25GETUID: {
1608 struct sockaddr_ax25 sax25;
1609 if (copy_from_user(&sax25, (void *)arg, sizeof(sax25)))
1610 return -EFAULT;
1611 return ax25_uid_ioctl(cmd, &sax25);
1614 case SIOCAX25NOUID: { /* Set the default policy (default/bar) */
1615 long amount;
1616 if (!capable(CAP_NET_ADMIN))
1617 return -EPERM;
1618 if (get_user(amount, (long *)arg))
1619 return -EFAULT;
1620 if (amount > AX25_NOUID_BLOCK)
1621 return -EINVAL;
1622 ax25_uid_policy = amount;
1623 return 0;
1626 case SIOCADDRT:
1627 case SIOCDELRT:
1628 case SIOCAX25OPTRT:
1629 if (!capable(CAP_NET_ADMIN))
1630 return -EPERM;
1631 return ax25_rt_ioctl(cmd, (void *)arg);
1633 case SIOCAX25CTLCON:
1634 if (!capable(CAP_NET_ADMIN))
1635 return -EPERM;
1636 return ax25_ctl_ioctl(cmd, (void *)arg);
1638 case SIOCAX25GETINFO:
1639 case SIOCAX25GETINFOOLD: {
1640 struct ax25_info_struct ax25_info;
1642 ax25_info.t1 = sk->protinfo.ax25->t1 / HZ;
1643 ax25_info.t2 = sk->protinfo.ax25->t2 / HZ;
1644 ax25_info.t3 = sk->protinfo.ax25->t3 / HZ;
1645 ax25_info.idle = sk->protinfo.ax25->idle / (60 * HZ);
1646 ax25_info.n2 = sk->protinfo.ax25->n2;
1647 ax25_info.t1timer = ax25_display_timer(&sk->protinfo.ax25->t1timer) / HZ;
1648 ax25_info.t2timer = ax25_display_timer(&sk->protinfo.ax25->t2timer) / HZ;
1649 ax25_info.t3timer = ax25_display_timer(&sk->protinfo.ax25->t3timer) / HZ;
1650 ax25_info.idletimer = ax25_display_timer(&sk->protinfo.ax25->idletimer) / (60 * HZ);
1651 ax25_info.n2count = sk->protinfo.ax25->n2count;
1652 ax25_info.state = sk->protinfo.ax25->state;
1653 ax25_info.rcv_q = atomic_read(&sk->rmem_alloc);
1654 ax25_info.snd_q = atomic_read(&sk->wmem_alloc);
1655 ax25_info.vs = sk->protinfo.ax25->vs;
1656 ax25_info.vr = sk->protinfo.ax25->vr;
1657 ax25_info.va = sk->protinfo.ax25->va;
1658 ax25_info.vs_max = sk->protinfo.ax25->vs; /* reserved */
1659 ax25_info.paclen = sk->protinfo.ax25->paclen;
1660 ax25_info.window = sk->protinfo.ax25->window;
1662 /* old structure? */
1663 if (cmd == SIOCAX25GETINFOOLD) {
1664 static int warned = 0;
1665 if (!warned) {
1666 printk(KERN_INFO "%s uses old SIOCAX25GETINFO\n",
1667 current->comm);
1668 warned=1;
1671 if (copy_to_user((void *)arg, &ax25_info, sizeof(struct ax25_info_struct_depreciated)))
1672 return -EFAULT;
1673 } else {
1674 if (copy_to_user((void *)arg, &ax25_info, sizeof(struct ax25_info_struct)))
1675 return -EINVAL;
1677 return 0;
1680 case SIOCAX25ADDFWD:
1681 case SIOCAX25DELFWD: {
1682 struct ax25_fwd_struct ax25_fwd;
1683 if (!capable(CAP_NET_ADMIN))
1684 return -EPERM;
1685 if (copy_from_user(&ax25_fwd, (void *)arg, sizeof(ax25_fwd)))
1686 return -EFAULT;
1687 return ax25_fwd_ioctl(cmd, &ax25_fwd);
1690 case SIOCGIFADDR:
1691 case SIOCSIFADDR:
1692 case SIOCGIFDSTADDR:
1693 case SIOCSIFDSTADDR:
1694 case SIOCGIFBRDADDR:
1695 case SIOCSIFBRDADDR:
1696 case SIOCGIFNETMASK:
1697 case SIOCSIFNETMASK:
1698 case SIOCGIFMETRIC:
1699 case SIOCSIFMETRIC:
1700 return -EINVAL;
1702 default:
1703 return dev_ioctl(cmd, (void *)arg);
1706 /*NOTREACHED*/
1707 return 0;
1710 static int ax25_get_info(char *buffer, char **start, off_t offset, int length)
1712 ax25_cb *ax25;
1713 int k;
1714 int len = 0;
1715 off_t pos = 0;
1716 off_t begin = 0;
1718 cli();
1721 * New format:
1722 * 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 for (ax25 = ax25_list; ax25 != NULL; ax25 = ax25->next) {
1726 len += sprintf(buffer+len, "%8.8lx %s %s%s ",
1727 (long) ax25,
1728 ax25->ax25_dev == NULL? "???" : ax25->ax25_dev->dev->name,
1729 ax2asc(&ax25->source_addr),
1730 ax25->iamdigi? "*":"");
1732 len += sprintf(buffer+len, "%s", ax2asc(&ax25->dest_addr));
1734 for (k=0; (ax25->digipeat != NULL) && (k < ax25->digipeat->ndigi); k++) {
1735 len += sprintf(buffer+len, ",%s%s",
1736 ax2asc(&ax25->digipeat->calls[k]),
1737 ax25->digipeat->repeated[k]? "*":"");
1740 len += sprintf(buffer+len, " %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %d %d",
1741 ax25->state,
1742 ax25->vs, ax25->vr, ax25->va,
1743 ax25_display_timer(&ax25->t1timer) / HZ, ax25->t1 / HZ,
1744 ax25_display_timer(&ax25->t2timer) / HZ, ax25->t2 / HZ,
1745 ax25_display_timer(&ax25->t3timer) / HZ, ax25->t3 / HZ,
1746 ax25_display_timer(&ax25->idletimer) / (60 * HZ),
1747 ax25->idle / (60 * HZ),
1748 ax25->n2count, ax25->n2,
1749 ax25->rtt / HZ,
1750 ax25->window,
1751 ax25->paclen);
1753 if (ax25->sk != NULL) {
1754 len += sprintf(buffer + len, " %d %d %ld\n",
1755 atomic_read(&ax25->sk->wmem_alloc),
1756 atomic_read(&ax25->sk->rmem_alloc),
1757 ax25->sk->socket != NULL ? ax25->sk->socket->inode->i_ino : 0L);
1758 } else {
1759 len += sprintf(buffer + len, " * * *\n");
1762 pos = begin + len;
1764 if (pos < offset) {
1765 len = 0;
1766 begin = pos;
1769 if (pos > offset + length)
1770 break;
1773 sti();
1775 *start = buffer + (offset - begin);
1776 len -= (offset - begin);
1778 if (len > length) len = length;
1780 return(len);
1783 static struct net_proto_family ax25_family_ops =
1785 PF_AX25,
1786 ax25_create
1789 static struct proto_ops SOCKOPS_WRAPPED(ax25_proto_ops) = {
1790 family: PF_AX25,
1792 release: ax25_release,
1793 bind: ax25_bind,
1794 connect: ax25_connect,
1795 socketpair: sock_no_socketpair,
1796 accept: ax25_accept,
1797 getname: ax25_getname,
1798 poll: datagram_poll,
1799 ioctl: ax25_ioctl,
1800 listen: ax25_listen,
1801 shutdown: ax25_shutdown,
1802 setsockopt: ax25_setsockopt,
1803 getsockopt: ax25_getsockopt,
1804 sendmsg: ax25_sendmsg,
1805 recvmsg: ax25_recvmsg,
1806 mmap: sock_no_mmap,
1809 #include <linux/smp_lock.h>
1810 SOCKOPS_WRAP(ax25_proto, PF_AX25);
1813 * Called by socket.c on kernel start up
1815 static struct packet_type ax25_packet_type =
1817 0, /* MUTTER ntohs(ETH_P_AX25),*/
1818 0, /* copy */
1819 ax25_kiss_rcv,
1820 NULL,
1821 NULL,
1824 static struct notifier_block ax25_dev_notifier = {
1825 ax25_device_event,
1829 EXPORT_SYMBOL(ax25_encapsulate);
1830 EXPORT_SYMBOL(ax25_rebuild_header);
1831 EXPORT_SYMBOL(ax25_findbyuid);
1832 EXPORT_SYMBOL(ax25_find_cb);
1833 EXPORT_SYMBOL(ax25_linkfail_register);
1834 EXPORT_SYMBOL(ax25_linkfail_release);
1835 EXPORT_SYMBOL(ax25_listen_register);
1836 EXPORT_SYMBOL(ax25_listen_release);
1837 EXPORT_SYMBOL(ax25_protocol_register);
1838 EXPORT_SYMBOL(ax25_protocol_release);
1839 EXPORT_SYMBOL(ax25_send_frame);
1840 EXPORT_SYMBOL(ax25_uid_policy);
1841 EXPORT_SYMBOL(ax25cmp);
1842 EXPORT_SYMBOL(ax2asc);
1843 EXPORT_SYMBOL(asc2ax);
1844 EXPORT_SYMBOL(null_ax25_address);
1845 EXPORT_SYMBOL(ax25_display_timer);
1847 void __init ax25_proto_init(struct net_proto *pro)
1849 sock_register(&ax25_family_ops);
1850 ax25_packet_type.type = htons(ETH_P_AX25);
1851 dev_add_pack(&ax25_packet_type);
1852 register_netdevice_notifier(&ax25_dev_notifier);
1853 #ifdef CONFIG_SYSCTL
1854 ax25_register_sysctl();
1855 #endif
1857 #ifdef CONFIG_PROC_FS
1858 proc_net_create("ax25_route", 0, ax25_rt_get_info);
1859 proc_net_create("ax25", 0, ax25_get_info);
1860 proc_net_create("ax25_calls", 0, ax25_uid_get_info);
1861 #endif
1863 printk(KERN_INFO "NET4: G4KLX/GW4PTS AX.25 for Linux. Version 0.37 for Linux NET4.0\n");
1866 #ifdef MODULE
1867 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1868 MODULE_DESCRIPTION("The amateur radio AX.25 link layer protocol");
1870 int init_module(void)
1872 ax25_proto_init(NULL);
1874 return 0;
1877 void cleanup_module(void)
1879 #ifdef CONFIG_PROC_FS
1880 proc_net_remove("ax25_route");
1881 proc_net_remove("ax25");
1882 proc_net_remove("ax25_calls");
1883 #endif
1884 ax25_rt_free();
1885 ax25_uid_free();
1886 ax25_dev_free();
1888 #ifdef CONFIG_SYSCTL
1889 ax25_unregister_sysctl();
1890 #endif
1891 unregister_netdevice_notifier(&ax25_dev_notifier);
1893 ax25_packet_type.type = htons(ETH_P_AX25);
1894 dev_remove_pack(&ax25_packet_type);
1896 sock_unregister(PF_AX25);
1898 #endif
1900 #endif