root/net/ax25/af_ax25.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ax2asc
  2. ax25cmp
  3. ax25_remove_socket
  4. ax25_kill_by_device
  5. ax25_device_event
  6. ax25_insert_socket
  7. ax25_find_listener
  8. ax25_find_socket
  9. ax25_find_cb
  10. ax25_addr_match
  11. ax25_send_to_raw
  12. ax25_destroy_timer
  13. ax25_destroy_socket
  14. ax25_findbyuid
  15. ax25_uid_ioctl
  16. ax25_create_cb
  17. ax25_send_frame
  18. ax25rtr_get_dev
  19. ax25_fcntl
  20. ax25_setsockopt
  21. ax25_getsockopt
  22. ax25_listen
  23. def_callback1
  24. def_callback2
  25. ax25_create
  26. ax25_make_new
  27. ax25_dup
  28. ax25_release
  29. ax25_bind
  30. ax25_connect
  31. ax25_socketpair
  32. ax25_accept
  33. ax25_getname
  34. ax25_rcv
  35. kiss_rcv
  36. bpq_rcv
  37. ax25_sendto
  38. ax25_send
  39. ax25_write
  40. ax25_recvfrom
  41. ax25_recv
  42. ax25_read
  43. ax25_shutdown
  44. ax25_select
  45. ax25_ioctl
  46. ax25_get_info
  47. ax25_proto_init
  48. ax25_queue_xmit
  49. ax25_encapsulate
  50. ax25_rebuild_header

   1 /*
   2  *      AX.25 release 030
   3  *
   4  *      This is ALPHA test software. This code may break your machine, randomly fail to work with new 
   5  *      releases, misbehave and/or generally screw up. It might even work. 
   6  *
   7  *      This code REQUIRES 1.2.1 or higher/ NET3.029
   8  *
   9  *      This module:
  10  *              This module is free software; you can redistribute it and/or
  11  *              modify it under the terms of the GNU General Public License
  12  *              as published by the Free Software Foundation; either version
  13  *              2 of the License, or (at your option) any later version.
  14  *
  15  *      History
  16  *      AX.25 006       Alan(GW4PTS)            Nearly died of shock - its working 8-)
  17  *      AX.25 007       Alan(GW4PTS)            Removed the silliest bugs
  18  *      AX.25 008       Alan(GW4PTS)            Cleaned up, fixed a few state machine problems, added callbacks
  19  *      AX.25 009       Alan(GW4PTS)            Emergency patch kit to fix memory corruption
  20  *      AX.25 010       Alan(GW4PTS)            Added RAW sockets/Digipeat.
  21  *      AX.25 011       Alan(GW4PTS)            RAW socket and datagram fixes (thanks) - Raw sendto now gets PID right
  22  *                                              datagram sendto uses correct target address.
  23  *      AX.25 012       Alan(GW4PTS)            Correct incoming connection handling, send DM to failed connects.
  24  *                                              Use skb->data not skb+1. Support sk->priority correctly.
  25  *                                              Correct receive on SOCK_DGRAM.
  26  *      AX.25 013       Alan(GW4PTS)            Send DM to all unknown frames, missing initialiser fixed
  27  *                                              Leave spare SSID bits set (DAMA etc) - thanks for bug report,
  28  *                                              removed device registration (its not used or needed). Clean up for
  29  *                                              gcc 2.5.8. PID to AX25_P_
  30  *      AX.25 014       Alan(GW4PTS)            Cleanup and NET3 merge
  31  *      AX.25 015       Alan(GW4PTS)            Internal test version.
  32  *      AX.25 016       Alan(GW4PTS)            Semi Internal version for PI card
  33  *                                              work.
  34  *      AX.25 017       Alan(GW4PTS)            Fixed some small bugs reported by
  35  *                                              G4KLX
  36  *      AX.25 018       Alan(GW4PTS)            Fixed a small error in SOCK_DGRAM
  37  *      AX.25 019       Alan(GW4PTS)            Clean ups for the non INET kernel and device ioctls in AX.25
  38  *      AX.25 020       Jonathan(G4KLX)         /proc support and other changes.
  39  *      AX.25 021       Alan(GW4PTS)            Added AX25_T1, AX25_N2, AX25_T3 as requested.
  40  *      AX.25 022       Jonathan(G4KLX)         More work on the ax25 auto router and /proc improved (again)!
  41  *                      Alan(GW4PTS)            Added TIOCINQ/OUTQ
  42  *      AX.25 023       Alan(GW4PTS)            Fixed shutdown bug
  43  *      AX.25 023       Alan(GW4PTS)            Linus changed timers
  44  *      AX.25 024       Alan(GW4PTS)            Small bug fixes
  45  *      AX.25 025       Alan(GW4PTS)            More fixes, Linux 1.1.51 compatibility stuff, timers again!
  46  *      AX.25 026       Alan(GW4PTS)            Small state fix.
  47  *      AX.25 027       Alan(GW4PTS)            Socket close crash fixes.
  48  *      AX.25 028       Alan(GW4PTS)            Callsign control including settings per uid.
  49  *                                              Small bug fixes.
  50  *                                              Protocol set by sockets only.
  51  *                                              Small changes to allow for start of NET/ROM layer.
  52  *      AX.25 028a      Jonathan(G4KLX)         Changes to state machine.
  53  *      AX.25 028b      Jonathan(G4KLX)         Extracted ax25 control block
  54  *                                              from sock structure.
  55  *      AX.25 029       Alan(GW4PTS)            Combined 028b and some KA9Q code
  56  *                      Jonathan(G4KLX)         and removed all the old Berkeley, added IP mode registration.
  57  *                      Darryl(G7LED)           stuff. Cross-port digipeating. Minor fixes and enhancements.
  58  *                      Alan(GW4PTS)            Missed suser() on axassociate checks
  59  *      AX.25 030       Alan(GW4PTS)            Added variable length headers.
  60  *                      Jonathan(G4KLX)         Added BPQ Ethernet interface.
  61  *                      Steven(GW7RRM)          Added digi-peating control ioctl.
  62  *                                              Added extended AX.25 support.
  63  *                                              Added AX.25 frame segmentation.
  64  *
  65  *      To do:
  66  *              Support use as digipeater, including an on/off ioctl
  67  *              Restructure the ax25_rcv code to be cleaner/faster and
  68  *              copy only when needed.
  69  *              Consider better arbitary protocol support.
  70  *              Fix non-blocking connect failure.
  71  *              VC mode doesnt work on the PI card.
  72  *              Settable protocol id for SEQPACKET sockets
  73  */
  74  
  75 #include <linux/config.h>
  76 #ifdef CONFIG_AX25
  77 #include <linux/errno.h>
  78 #include <linux/types.h>
  79 #include <linux/socket.h>
  80 #include <linux/in.h>
  81 #include <linux/kernel.h>
  82 #include <linux/sched.h>
  83 #include <linux/timer.h>
  84 #include <linux/string.h>
  85 #include <linux/sockios.h>
  86 #include <linux/net.h>
  87 #include <net/ax25.h>
  88 #include <linux/inet.h>
  89 #include <linux/netdevice.h>
  90 #include <linux/skbuff.h>
  91 #include <net/sock.h>
  92 #include <asm/segment.h>
  93 #include <asm/system.h>
  94 #include <linux/fcntl.h>
  95 #include <linux/termios.h>      /* For TIOCINQ/OUTQ */
  96 #include <linux/mm.h>
  97 #include <linux/interrupt.h>
  98 #include <linux/notifier.h>
  99 #include <linux/proc_fs.h>
 100 
 101 #include <net/ip.h>
 102 #include <net/arp.h>
 103 
 104 static int ax25_digi_on = 1;
 105 
 106 #define CONFIG_AX25_XDIGI       /* Cross port (band) digi stuff */
 107 
 108 /**********************************************************************************************************************\
 109 *                                                                                                                      *
 110 *                                               Handlers for the socket list.                                          *
 111 *                                                                                                                      *
 112 \**********************************************************************************************************************/
 113 
 114 static ax25_cb *volatile ax25_list = NULL;
 115 
 116 /*
 117  *      ax25 -> ascii conversion
 118  */
 119 char *ax2asc(ax25_address *a)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121         static char buf[11];
 122         char c, *s;
 123         int n;
 124 
 125         for (n = 0, s = buf; n < 6; n++)
 126         {
 127                 c = (a->ax25_call[n] >> 1) & 0x7F;
 128 
 129                 if (c != ' ') *s++ = c;
 130         }
 131         
 132         *s++ = '-';
 133 
 134         if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9)
 135         {
 136                 *s++ = '1';
 137                 n -= 10;
 138         }
 139         
 140         *s++ = n + '0';
 141         *s++ = '\0';
 142 
 143         return(buf);
 144 
 145 }
 146 
 147 /*
 148  *      Compare two ax.25 addresses
 149  */
 150 int ax25cmp(ax25_address *a, ax25_address *b)
     /* [previous][next][first][last][top][bottom][index][help] */
 151 {
 152         int ct = 0;
 153 
 154         while (ct < 6) {
 155                 if ((a->ax25_call[ct] & 0xFE) != (b->ax25_call[ct] & 0xFE))     /* Clean off repeater bits */
 156                         return 1;
 157                 ct++;
 158         }
 159 
 160         if ((a->ax25_call[ct] & 0x1E) == (b->ax25_call[ct] & 0x1E))     /* SSID without control bit */
 161                 return 0;
 162 
 163         return 2;                       /* Partial match */
 164 }
 165 
 166 /*
 167  *      Socket removal during an interrupt is now safe.
 168  */
 169 static void ax25_remove_socket(ax25_cb *ax25)
     /* [previous][next][first][last][top][bottom][index][help] */
 170 {
 171         ax25_cb *s;
 172         unsigned long flags;
 173         
 174         save_flags(flags);
 175         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 device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 200 {
 201         ax25_cb *s;
 202         
 203         for (s = ax25_list; s != NULL; s = s->next) {
 204                 if (s->device == dev) {
 205                         s->state  = AX25_STATE_0;
 206                         s->device = NULL;
 207                         if (s->sk != NULL) {
 208                                 s->sk->state = TCP_CLOSE;
 209                                 s->sk->err   = ENETUNREACH;
 210                                 if (!s->sk->dead)
 211                                         s->sk->state_change(s->sk);
 212                                 s->sk->dead  = 1;
 213                         }
 214                 }
 215         }
 216         
 217         ax25_rt_device_down(dev);
 218 }
 219 
 220 /*
 221  *      Handle device status changes.
 222  */
 223 static int ax25_device_event(unsigned long event, void *ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 224 {
 225         if (event != NETDEV_DOWN)
 226                 return NOTIFY_DONE;
 227                 
 228         ax25_kill_by_device(ptr);
 229         
 230         return NOTIFY_DONE;
 231 }
 232 
 233 /*
 234  *      Add a socket to the bound sockets list.
 235  */
 236 static void ax25_insert_socket(ax25_cb *ax25)
     /* [previous][next][first][last][top][bottom][index][help] */
 237 {
 238         unsigned long flags;
 239 
 240         save_flags(flags);
 241         cli();
 242 
 243         ax25->next = ax25_list;
 244         ax25_list  = ax25;
 245 
 246         restore_flags(flags);
 247 }
 248 
 249 /*
 250  *      Find a socket that wants to accept the SABM we just
 251  *      received.
 252  */
 253 static struct sock *ax25_find_listener(ax25_address *addr, struct device *dev, int type)
     /* [previous][next][first][last][top][bottom][index][help] */
 254 {
 255         unsigned long flags;
 256         ax25_cb *s;
 257 
 258         save_flags(flags);
 259         cli();
 260 
 261         for (s = ax25_list; s != NULL; s = s->next) {
 262                 if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 && s->sk->type == type && s->sk->state == TCP_LISTEN) {
 263                         /* If device is null we match any device */
 264                         if (s->device == NULL || s->device == dev) {
 265                                 restore_flags(flags);
 266                                 return s->sk;
 267                         }
 268                 }
 269         }
 270 
 271         restore_flags(flags);
 272         return NULL;
 273 }
 274 
 275 /*
 276  *      Find an AX.25 socket given both ends.
 277  */
 278 static struct sock *ax25_find_socket(ax25_address *my_addr, ax25_address *dest_addr, int type)
     /* [previous][next][first][last][top][bottom][index][help] */
 279 {
 280         ax25_cb *s;
 281         unsigned long flags;
 282 
 283         save_flags(flags);
 284         cli();
 285 
 286         for (s = ax25_list; s != NULL; s = s->next) {
 287                 if (s->sk != NULL && ax25cmp(&s->source_addr, my_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->sk->type == type) {
 288                         restore_flags(flags);
 289                         return s->sk;
 290                 }
 291         }
 292 
 293         restore_flags(flags);
 294 
 295         return NULL;
 296 }
 297 
 298 /*
 299  *      Find an AX.25 control block given both ends. It will only pick up
 300  *      floating AX.25 control blocks or non Raw socket bound control blocks.
 301  */
 302 static ax25_cb *ax25_find_cb(ax25_address *my_addr, ax25_address *dest_addr, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 303 {
 304         ax25_cb *s;
 305         unsigned long flags;
 306 
 307         save_flags(flags);
 308         cli();
 309 
 310         for (s = ax25_list; s != NULL; s = s->next) {
 311                 if (s->sk != NULL && s->sk->type != SOCK_SEQPACKET)
 312                         continue;
 313                 if (ax25cmp(&s->source_addr, my_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->device == dev) {
 314                         restore_flags(flags);
 315                         return s;
 316                 }
 317         }
 318 
 319         restore_flags(flags);
 320 
 321         return NULL;
 322 }
 323 
 324 /*
 325  *      Look for any matching address - RAW sockets can bind to arbitary names
 326  */
 327 static struct sock *ax25_addr_match(ax25_address *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 328 {
 329         unsigned long flags;
 330         ax25_cb *s;
 331 
 332         save_flags(flags);
 333         cli();
 334 
 335         for (s = ax25_list; s != NULL; s = s->next) {
 336                 if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 && s->sk->type == SOCK_RAW) {
 337                         restore_flags(flags);
 338                         return s->sk;
 339                 }
 340         }
 341 
 342         restore_flags(flags);
 343 
 344         return NULL;
 345 }
 346 
 347 static void ax25_send_to_raw(struct sock *sk, struct sk_buff *skb, int proto)
     /* [previous][next][first][last][top][bottom][index][help] */
 348 {
 349         struct sk_buff *copy;
 350         
 351         while (sk != NULL) {
 352                 if (sk->type == SOCK_RAW && sk->protocol == proto && sk->rmem_alloc <= sk->rcvbuf) {
 353                         if ((copy = skb_clone(skb, GFP_ATOMIC)) == NULL)
 354                                 return;
 355 
 356                         copy->sk = sk;
 357                         sk->rmem_alloc += copy->truesize;
 358                         skb_queue_tail(&sk->receive_queue, copy);
 359                         if (!sk->dead)
 360                                 sk->data_ready(sk, skb->len);
 361                 }
 362 
 363                 sk = sk->next;
 364         }
 365 }       
 366 
 367 /*
 368  *      Deferred destroy.
 369  */
 370 void ax25_destory_socket(ax25_cb *);
 371 
 372 /*
 373  *      Handler for deferred kills.
 374  */
 375 static void ax25_destroy_timer(unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
 376 {
 377         ax25_destroy_socket((ax25_cb *)data);
 378 }
 379 
 380 /*
 381  *      This is called from user mode and the timers. Thus it protects itself against
 382  *      interrupt users but doesn't worry about being called during work.
 383  *      Once it is removed from the queue no interrupt or bottom half will
 384  *      touch it and we are (fairly 8-) ) safe.
 385  */
 386 void ax25_destroy_socket(ax25_cb *ax25) /* Not static as its used by the timer */
     /* [previous][next][first][last][top][bottom][index][help] */
 387 {
 388         struct sk_buff *skb;
 389         unsigned long flags;
 390         
 391         save_flags(flags);
 392         cli();
 393         
 394         del_timer(&ax25->timer);
 395         
 396         ax25_remove_socket(ax25);
 397         ax25_clear_queues(ax25);        /* Flush the queues */
 398         
 399         if (ax25->sk != NULL) {
 400                 while ((skb = skb_dequeue(&ax25->sk->receive_queue)) != NULL) {
 401                         if (skb->sk != ax25->sk) {                      /* A pending connection */
 402                                 skb->sk->dead = 1;      /* Queue the unaccepted socket for death */
 403                                 ax25_set_timer(skb->sk->ax25);
 404                                 skb->sk->ax25->state = AX25_STATE_0;
 405                         }
 406 
 407                         kfree_skb(skb, FREE_READ);
 408                 }
 409         }
 410         
 411         if (ax25->digipeat != NULL) {
 412                 kfree_s(ax25->digipeat, sizeof(ax25_digi));
 413                 ax25->digipeat = NULL;
 414         }
 415 
 416         if (ax25->sk != NULL) {
 417                 if (ax25->sk->wmem_alloc || ax25->sk->rmem_alloc) { /* Defer: outstanding buffers */
 418                         init_timer(&ax25->timer);
 419                         ax25->timer.expires  = jiffies + 10 * HZ;
 420                         ax25->timer.function = ax25_destroy_timer;
 421                         ax25->timer.data     = (unsigned long)ax25;
 422                         add_timer(&ax25->timer);
 423                 } else {
 424                         kfree_s(ax25->sk, sizeof(*ax25->sk));
 425                         kfree_s(ax25, sizeof(*ax25));
 426                 }
 427         } else {
 428                 kfree_s(ax25, sizeof(*ax25));
 429         }
 430 
 431         restore_flags(flags);
 432 }
 433 
 434 /*
 435  *      Callsign/UID mapper. This is in kernel space for security on multi-amateur machines.
 436  */
 437 
 438 ax25_uid_assoc *ax25_uid_list;
 439 
 440 int ax25_uid_policy = 0;
 441 
 442 ax25_address *ax25_findbyuid(uid_t uid)
     /* [previous][next][first][last][top][bottom][index][help] */
 443 {
 444         ax25_uid_assoc *a;
 445         
 446         for (a = ax25_uid_list; a != NULL; a = a->next) {
 447                 if (a->uid == uid)
 448                         return &a->call;
 449         }
 450 
 451         return NULL;
 452 }
 453 
 454 static int ax25_uid_ioctl(int cmd, struct sockaddr_ax25 *sax)
     /* [previous][next][first][last][top][bottom][index][help] */
 455 {
 456         ax25_uid_assoc *a;
 457         
 458         switch (cmd) {
 459                 case SIOCAX25GETUID:
 460                         for (a = ax25_uid_list; a != NULL; a = a->next) {
 461                                 if (ax25cmp(&sax->sax25_call, &a->call) == 0)
 462                                         return a->uid;
 463                         }
 464                         return -ENOENT;
 465                 case SIOCAX25ADDUID:
 466                         if(!suser())
 467                                 return -EPERM;
 468                         if (ax25_findbyuid(sax->sax25_uid))
 469                                 return -EEXIST;
 470                         a = (ax25_uid_assoc *)kmalloc(sizeof(*a), GFP_KERNEL);
 471                         a->uid  = sax->sax25_uid;
 472                         a->call = sax->sax25_call;
 473                         a->next = ax25_uid_list;
 474                         ax25_uid_list = a;
 475                         return 0;
 476                 case SIOCAX25DELUID:
 477                 {
 478                         ax25_uid_assoc **l;
 479                         
 480                         if(!suser())
 481                                 return -EPERM;
 482                         l = &ax25_uid_list;
 483                         while ((*l) != NULL) {
 484                                 if (ax25cmp(&((*l)->call), &(sax->sax25_call)) == 0) {
 485                                         a = *l;
 486                                         *l = (*l)->next;
 487                                         kfree_s(a, sizeof(*a));
 488                                         return 0;
 489                                 }
 490                                 
 491                                 l = &((*l)->next);
 492                         }
 493                         return -ENOENT;
 494                 }
 495         }
 496 
 497         return -EINVAL; /*NOTREACHED */
 498 }       
 499 
 500 /*
 501  * Create an empty AX.25 control block.
 502  */
 503 static ax25_cb *ax25_create_cb(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 504 {
 505         ax25_cb *ax25;
 506 
 507         if ((ax25 = (ax25_cb *)kmalloc(sizeof(*ax25), GFP_ATOMIC)) == NULL)
 508                 return NULL;
 509 
 510         skb_queue_head_init(&ax25->write_queue);
 511         skb_queue_head_init(&ax25->frag_queue);
 512         skb_queue_head_init(&ax25->ack_queue);
 513         skb_queue_head_init(&ax25->reseq_queue);
 514 
 515         init_timer(&ax25->timer);
 516 
 517         ax25->rtt     = DEFAULT_T1;
 518         ax25->t1      = DEFAULT_T1;
 519         ax25->t2      = DEFAULT_T2;
 520         ax25->n2      = DEFAULT_N2;
 521         ax25->t3      = DEFAULT_T3;
 522 
 523         ax25->modulus   = MODULUS;
 524         ax25->fragno    = 0;
 525         ax25->fraglen   = 0;
 526         ax25->backoff   = 1;
 527         ax25->condition = 0x00;
 528         ax25->t1timer   = 0;
 529         ax25->t2timer   = 0;
 530         ax25->t3timer   = 0;
 531         ax25->n2count   = 0;
 532 
 533         ax25->va      = 0;
 534         ax25->vr      = 0;
 535         ax25->vs      = 0;
 536 
 537         ax25->window   = DEFAULT_WINDOW;
 538         ax25->device   = NULL;
 539         ax25->digipeat = NULL;
 540         ax25->sk       = NULL;
 541 
 542         ax25->state    = AX25_STATE_0;
 543 
 544         memset(&ax25->dest_addr,   '\0', sizeof(ax25_address));
 545         memset(&ax25->source_addr, '\0', sizeof(ax25_address));
 546 
 547         return ax25;
 548 }
 549 
 550 int ax25_send_frame(struct sk_buff *skb, ax25_address *src, ax25_address *dest,
     /* [previous][next][first][last][top][bottom][index][help] */
 551         ax25_digi *digi, struct device *dev)
 552 {
 553         ax25_cb *ax25;
 554 
 555         if (skb == NULL)
 556                 return 0;
 557 
 558         /*
 559          * Look for an existing connection.
 560          */
 561         for (ax25 = ax25_list; ax25 != NULL; ax25 = ax25->next) {
 562                 if (ax25->sk != NULL && ax25->sk->type != SOCK_SEQPACKET)
 563                         continue;
 564 
 565                 if (ax25cmp(&ax25->source_addr, src) == 0 && ax25cmp(&ax25->dest_addr, dest) == 0 && ax25->device == dev) {
 566                         ax25_output(ax25, skb);
 567                         return 1;               /* It already existed */
 568                 }
 569         }
 570 
 571         if ((ax25 = ax25_create_cb()) == NULL)
 572                 return 0;
 573 
 574         ax25->device = dev;
 575 
 576         memcpy(&ax25->source_addr, src,  sizeof(ax25_address));
 577         memcpy(&ax25->dest_addr,   dest, sizeof(ax25_address));
 578 
 579         if (digi != NULL) {
 580                 if ((ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
 581                         kfree_s(ax25, sizeof(ax25));
 582                         return 0;
 583                 }
 584                 memcpy(ax25->digipeat, digi, sizeof(ax25_digi));
 585         }
 586 
 587         ax25_establish_data_link(ax25);
 588         ax25_insert_socket(ax25);
 589 
 590         ax25->state = AX25_STATE_1;
 591 
 592         ax25_set_timer(ax25);
 593 
 594         ax25_output(ax25, skb);
 595                         
 596         return 1;                       /* We had to create it */       
 597 }
 598 
 599 /*******************************************************************************************************************\
 600 *                                                                                                                   *
 601 *               Routing rules for AX.25: Basically iterate over the active interfaces                               *
 602 *                                                                                                                   *
 603 \*******************************************************************************************************************/
 604 
 605 struct device *ax25rtr_get_dev(ax25_address *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 606 {
 607         struct device *dev;
 608         ax25_address dev_addr;
 609         
 610         for (dev = dev_base; dev != NULL; dev = dev->next) {
 611                 if (dev->flags & IFF_UP) {
 612                         switch (dev->type) {
 613                                 case ARPHRD_AX25: /* Active kiss ax25 mode */ 
 614                                         if (ax25cmp(addr, (ax25_address *)dev->dev_addr) == 0)
 615                                                 return dev;
 616                                         break;
 617                                 case ARPHRD_ETHER:
 618                                         if (arp_query((unsigned char *)&dev_addr, dev->pa_addr, ARPHRD_AX25))
 619                                                 if (ax25cmp(addr, &dev_addr) == 0)
 620                                                         return dev;
 621                                         break;
 622                                 default:
 623                                         break;
 624                         }
 625                 }
 626         }
 627 
 628         return NULL;
 629 }
 630 
 631 /*******************************************************************************************************************\
 632 *                                                                                                                   *
 633 *             Handling for system calls applied via the various interfaces to an AX25 socket object                 *
 634 *                                                                                                                   *
 635 \*******************************************************************************************************************/
 636  
 637 static int ax25_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 638 {
 639         switch(cmd)
 640         {
 641                 default:
 642                         return(-EINVAL);
 643         }
 644 }
 645 
 646 static int ax25_setsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 647         char *optval, int optlen)
 648 {
 649         struct sock *sk;
 650         int err, opt;
 651 
 652         sk = (struct sock *)sock->data;
 653         
 654         if (level == SOL_SOCKET)
 655                 return sock_setsockopt(sk, level, optname, optval, optlen);
 656 
 657         if (level != SOL_AX25)
 658                 return -EOPNOTSUPP;
 659 
 660         if (optval == NULL)
 661                 return -EINVAL;
 662 
 663         if ((err = verify_area(VERIFY_READ, optval, sizeof(int))) != 0)
 664                 return err;
 665 
 666         opt = get_fs_long((unsigned long *)optval);
 667 
 668         switch (optname) {
 669                 case AX25_WINDOW:
 670                         if (sk->ax25->modulus == MODULUS) {
 671                                 if (opt < 1 || opt > 7)
 672                                         return -EINVAL;
 673                         } else {
 674                                 if (opt < 1 || opt > 63)
 675                                         return -EINVAL;
 676                         }
 677                         sk->ax25->window = opt;
 678                         return 0;
 679 
 680                 case AX25_T1:
 681                         if (opt < 1)
 682                                 return -EINVAL;
 683                         sk->ax25->rtt = (opt * PR_SLOWHZ) / 2;
 684                         return 0;
 685 
 686                 case AX25_T2:
 687                         if (opt < 1)
 688                                 return -EINVAL;
 689                         sk->ax25->t2 = opt * PR_SLOWHZ;
 690                         return 0;
 691 
 692                 case AX25_N2:
 693                         if (opt < 1 || opt > 31)
 694                                 return -EINVAL;
 695                         sk->ax25->n2 = opt;
 696                         return 0;
 697 
 698                 case AX25_T3:
 699                         if (opt < 1)
 700                                 return -EINVAL;
 701                         sk->ax25->t3 = opt * PR_SLOWHZ;
 702                         return 0;
 703 
 704                 case AX25_BACKOFF:
 705                         sk->ax25->backoff = opt ? 1 : 0;
 706                         return 0;
 707 
 708                 case AX25_EXTSEQ:
 709                         sk->ax25->modulus = opt ? EMODULUS : MODULUS;
 710                         return 0;
 711 
 712                 default:
 713                         return -ENOPROTOOPT;
 714         }
 715 }
 716 
 717 static int ax25_getsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 718         char *optval, int *optlen)
 719 {
 720         struct sock *sk;
 721         int val = 0;
 722         int err; 
 723 
 724         sk = (struct sock *)sock->data;
 725         
 726         if (level == SOL_SOCKET)
 727                 return sock_getsockopt(sk, level, optname, optval, optlen);
 728         
 729         if (level != SOL_AX25)
 730                 return -EOPNOTSUPP;
 731 
 732         switch (optname) {
 733                 case AX25_WINDOW:
 734                         val = sk->ax25->window;
 735                         break;
 736 
 737                 case AX25_T1:
 738                         val = sk->ax25->t1 / PR_SLOWHZ;
 739                         break;
 740 
 741                 case AX25_T2:
 742                         val = sk->ax25->t2 / PR_SLOWHZ;
 743                         break;
 744 
 745                 case AX25_N2:
 746                         val = sk->ax25->n2;
 747                         break;
 748 
 749                 case AX25_T3:
 750                         val = sk->ax25->t3 / PR_SLOWHZ;
 751                         break;
 752 
 753                 case AX25_BACKOFF:
 754                         val = sk->ax25->backoff;
 755                         break;
 756 
 757                 case AX25_EXTSEQ:
 758                         val = (sk->ax25->modulus == EMODULUS);
 759                         return 0;
 760 
 761                 default:
 762                         return -ENOPROTOOPT;
 763         }
 764 
 765         if ((err = verify_area(VERIFY_WRITE, optlen, sizeof(int))) != 0)
 766                 return err;
 767 
 768         put_user(sizeof(int), optlen);
 769 
 770         if ((err = verify_area(VERIFY_WRITE, optval, sizeof(int))) != 0)
 771                 return err;
 772 
 773         put_user(val, optval);
 774 
 775         return 0;
 776 }
 777 
 778 static int ax25_listen(struct socket *sock, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
 779 {
 780         struct sock *sk = (struct sock *)sock->data;
 781 
 782         if (sk->type == SOCK_SEQPACKET && sk->state != TCP_LISTEN) {
 783                 sk->max_ack_backlog = backlog;
 784                 sk->state           = TCP_LISTEN;
 785                 return 0;
 786         }
 787 
 788         return -EOPNOTSUPP;
 789 }
 790 
 791 static void def_callback1(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 792 {
 793         if (!sk->dead)
 794                 wake_up_interruptible(sk->sleep);
 795 }
 796 
 797 static void def_callback2(struct sock *sk, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 798 {
 799         if (!sk->dead)
 800                 wake_up_interruptible(sk->sleep);
 801 }
 802 
 803 static int ax25_create(struct socket *sock, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
 804 {
 805         struct sock *sk;
 806         ax25_cb *ax25;
 807 
 808         if ((sk = (struct sock *)kmalloc(sizeof(*sk), GFP_ATOMIC)) == NULL)
 809                 return -ENOMEM;
 810 
 811         if ((ax25 = ax25_create_cb()) == NULL) {
 812                 kfree_s(sk, sizeof(*sk));
 813                 return -ENOMEM;
 814         }
 815 
 816         sk->type = sock->type;
 817 
 818         switch (sock->type) {
 819                 case SOCK_DGRAM:
 820                 case SOCK_SEQPACKET:
 821                         if (protocol == 0)
 822                                 protocol = AX25_P_TEXT;
 823                         break;
 824                 case SOCK_RAW:
 825                         break;
 826                 default:
 827                         kfree_s((void *)sk, sizeof(*sk));
 828                         kfree_s((void *)ax25, sizeof(*ax25));
 829                         return -ESOCKTNOSUPPORT;
 830         }
 831 
 832         skb_queue_head_init(&sk->receive_queue);
 833         skb_queue_head_init(&sk->write_queue);
 834         skb_queue_head_init(&sk->back_log);
 835 
 836         sk->socket        = sock;
 837         sk->protocol      = protocol;
 838         sk->dead          = 0;
 839         sk->next          = NULL;
 840         sk->broadcast     = 0;
 841         sk->rcvbuf        = SK_RMEM_MAX;
 842         sk->sndbuf        = SK_WMEM_MAX;
 843         sk->wmem_alloc    = 0;
 844         sk->rmem_alloc    = 0;
 845         sk->inuse         = 0;
 846         sk->debug         = 0;
 847         sk->prot          = NULL;       /* So we use default free mechanisms */
 848         sk->err           = 0;
 849         sk->localroute    = 0;
 850         sk->send_head     = NULL;
 851         sk->state         = TCP_CLOSE;
 852         sk->shutdown      = 0;
 853         sk->priority      = SOPRI_NORMAL;
 854         sk->ack_backlog   = 0;
 855         sk->mtu           = AX25_MTU;   /* 256 */
 856         sk->zapped        = 1;
 857 
 858         sk->state_change = def_callback1;
 859         sk->data_ready   = def_callback2;
 860         sk->write_space  = def_callback1;
 861         sk->error_report = def_callback1;
 862 
 863         if (sock != NULL) {
 864                 sock->data = (void *)sk;
 865                 sk->sleep  = sock->wait;
 866         }
 867 
 868         ax25->sk = sk;
 869         sk->ax25 = ax25;
 870         
 871         return 0;
 872 }
 873 
 874 static struct sock *ax25_make_new(struct sock *osk, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 875 {
 876         struct sock *sk;
 877         ax25_cb *ax25;
 878 
 879         if ((sk = (struct sock *)kmalloc(sizeof(*sk), GFP_ATOMIC)) == NULL)
 880                 return NULL;
 881 
 882         if ((ax25 = ax25_create_cb()) == NULL) {
 883                 kfree_s(sk, sizeof(*sk));
 884                 return NULL;
 885         }
 886 
 887         sk->type   = osk->type;
 888         sk->socket = osk->socket;
 889 
 890         switch(osk->type)
 891         {
 892                 case SOCK_DGRAM:
 893                         break;
 894                 case SOCK_SEQPACKET:
 895                         break;
 896                 default:
 897                         kfree_s((void *)sk, sizeof(*sk));
 898                         kfree_s((void *)ax25, sizeof(*ax25));
 899                         return NULL;
 900         }
 901 
 902         skb_queue_head_init(&sk->receive_queue);
 903         skb_queue_head_init(&sk->write_queue);
 904         skb_queue_head_init(&sk->back_log);
 905 
 906         sk->dead        = 0;
 907         sk->next        = NULL;
 908         sk->priority    = osk->priority;
 909         sk->broadcast   = 0;
 910         sk->protocol    = osk->protocol;
 911         sk->rcvbuf      = osk->rcvbuf;
 912         sk->sndbuf      = osk->sndbuf;
 913         sk->wmem_alloc  = 0;
 914         sk->rmem_alloc  = 0;
 915         sk->inuse       = 0;
 916         sk->ack_backlog = 0;
 917         sk->prot        = NULL; /* So we use default free mechanisms */
 918         sk->err         = 0;
 919         sk->localroute  = 0;
 920         sk->send_head   = NULL;
 921         sk->debug       = osk->debug;
 922         sk->state       = TCP_ESTABLISHED;
 923         sk->window      = osk->window;
 924         sk->shutdown    = 0;
 925         sk->mtu         = osk->mtu;
 926         sk->sleep       = osk->sleep;
 927         sk->zapped      = osk->zapped;
 928 
 929         sk->state_change = def_callback1;
 930         sk->data_ready   = def_callback2;
 931         sk->write_space  = def_callback1;
 932         sk->error_report = def_callback1;
 933 
 934         ax25->modulus = osk->ax25->modulus;
 935         ax25->backoff = osk->ax25->backoff;
 936         ax25->rtt     = osk->ax25->rtt;
 937         ax25->t1      = osk->ax25->t1;
 938         ax25->t2      = osk->ax25->t2;
 939         ax25->t3      = osk->ax25->t3;
 940         ax25->n2      = osk->ax25->n2;
 941 
 942         ax25->window  = osk->ax25->window;
 943         ax25->device  = dev;
 944 
 945         memcpy(&ax25->source_addr, &osk->ax25->source_addr, sizeof(ax25_address));
 946         
 947         if (osk->ax25->digipeat != NULL) {
 948                 if ((ax25->digipeat = (ax25_digi *)kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
 949                         kfree_s(sk, sizeof(*sk));
 950                         kfree_s(ax25, sizeof(*ax25));
 951                         return NULL;
 952                 }
 953         }
 954 
 955         sk->ax25 = ax25;
 956         ax25->sk = sk;
 957 
 958         return sk;
 959 }
 960 
 961 static int ax25_dup(struct socket *newsock, struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
 962 {
 963         struct sock *sk = (struct sock *)oldsock->data;
 964 
 965         return ax25_create(newsock, sk->protocol);
 966 }
 967 
 968 static int ax25_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 969 {
 970         struct sock *sk = (struct sock *)sock->data;
 971 
 972         if (sk == NULL) return 0;
 973 
 974         if (sk->type == SOCK_SEQPACKET) {
 975                 switch (sk->ax25->state) {
 976                         case AX25_STATE_0:
 977                                 sk->dead        = 1;
 978                                 sk->state_change(sk);
 979                                 ax25_destroy_socket(sk->ax25);
 980                                 break;
 981 
 982                         case AX25_STATE_1:
 983                                 ax25_send_control(sk->ax25, DISC, POLLON, C_COMMAND);
 984                                 sk->ax25->state = AX25_STATE_0;
 985                                 sk->dead        = 1;
 986                                 sk->state_change(sk);
 987                                 ax25_destroy_socket(sk->ax25);
 988                                 break;
 989 
 990                         case AX25_STATE_2:
 991                                 ax25_send_control(sk->ax25, DM, POLLON, C_RESPONSE);
 992                                 sk->ax25->state = AX25_STATE_0;
 993                                 sk->dead        = 1;
 994                                 sk->state_change(sk);
 995                                 ax25_destroy_socket(sk->ax25);
 996                                 break;                  
 997 
 998                         case AX25_STATE_3:
 999                         case AX25_STATE_4:
1000                                 ax25_clear_queues(sk->ax25);
1001                                 sk->ax25->n2count = 0;
1002                                 ax25_send_control(sk->ax25, DISC, POLLON, C_COMMAND);
1003                                 sk->ax25->t3timer = 0;
1004                                 sk->ax25->t1timer = sk->ax25->t1 = ax25_calculate_t1(sk->ax25);
1005                                 sk->ax25->state   = AX25_STATE_2;
1006                                 sk->state_change(sk);
1007                                 sk->dead         = 1;
1008                                 break;
1009 
1010                         default:
1011                                 break;
1012                 }
1013         } else {
1014                 sk->dead = 1;
1015                 sk->state_change(sk);
1016                 ax25_destroy_socket(sk->ax25);
1017         }
1018 
1019         sock->data = NULL;      
1020 
1021         return 0;
1022 }
1023 
1024 /*
1025  *      We support a funny extension here so you can (as root) give any callsign
1026  *      digipeated via a local address as source. This is a hack until we add
1027  *      BSD 4.4 ADDIFADDR type support. It is however small and trivially backward
1028  *      compatible 8)
1029  */
1030 static int ax25_bind(struct socket *sock, struct sockaddr *uaddr,int addr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
1031 {
1032         struct sock *sk;
1033         struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
1034         struct device *dev;
1035         ax25_address *call;
1036         
1037         sk = (struct sock *)sock->data;
1038         
1039         if (sk->zapped == 0)
1040                 return -EIO;
1041                 
1042         if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25))
1043                 return -EINVAL;
1044 
1045 #ifdef DONTDO
1046         if (ax25_find_socket(&addr->fsa_ax25.sax25_call, sk->type) != NULL) {
1047                 if (sk->debug)
1048                         printk("AX25: bind failed: in use\n");
1049                 return -EADDRINUSE;
1050         }
1051 #endif
1052 
1053         call = ax25_findbyuid(current->euid);
1054         if (call == NULL && ax25_uid_policy && !suser())
1055                 return -EPERM;
1056                 
1057         if (call == NULL)
1058                 memcpy(&sk->ax25->source_addr, &addr->fsa_ax25.sax25_call, sizeof(ax25_address));
1059         else
1060                 memcpy(&sk->ax25->source_addr, call, sizeof(ax25_address));
1061 
1062         if (addr_len == sizeof(struct full_sockaddr_ax25) && addr->fsa_ax25.sax25_ndigis == 1) {
1063                 if (!suser())
1064                         return -EPERM;
1065                 call = &addr->fsa_digipeater[0];
1066         } else {
1067                 call = &addr->fsa_ax25.sax25_call;
1068         }
1069 
1070         if ((dev = ax25rtr_get_dev(call)) == NULL) {
1071                 if (sk->debug)
1072                         printk("AX25 bind failed: no device\n");
1073                 return -EADDRNOTAVAIL;
1074         }
1075 
1076         sk->ax25->device = dev;
1077         ax25_insert_socket(sk->ax25);
1078 
1079         sk->zapped = 0;
1080 
1081         if (sk->debug)
1082                 printk("AX25: socket is bound\n");
1083 
1084         return 0;
1085 }
1086 
1087 static int ax25_connect(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1088         int addr_len, int flags)
1089 {
1090         struct sock *sk = (struct sock *)sock->data;
1091         struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
1092         int err;
1093         
1094         if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
1095                 sock->state = SS_CONNECTED;
1096                 return 0;       /* Connect completed during a ERESTARTSYS event */
1097         }
1098         
1099         if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
1100                 sock->state = SS_UNCONNECTED;
1101                 return -ECONNREFUSED;
1102         }
1103         
1104         if (sk->state == TCP_ESTABLISHED && sk->type == SOCK_SEQPACKET)
1105                 return -EISCONN;        /* No reconnect on a seqpacket socket */
1106                 
1107         sk->state   = TCP_CLOSE;        
1108         sock->state = SS_UNCONNECTED;
1109 
1110         if (addr_len > sizeof(*addr)) {
1111                 int ct           = 0;
1112                 int ndigi        = addr_len - sizeof(*addr);
1113                 ax25_address *ap = (ax25_address *)(((char *)addr) + sizeof(*addr));
1114 
1115                 /* Size is an exact number of digipeaters ? */
1116                 if (ndigi % sizeof(ax25_address))
1117                         return -EINVAL;
1118 
1119                 ndigi /= sizeof(ax25_address);
1120 
1121                 /* Valid number of digipeaters ? */
1122                 if (ndigi < 1 || ndigi > 6)
1123                         return -EINVAL;
1124 
1125                 if (sk->ax25->digipeat == NULL) {
1126                         if ((sk->ax25->digipeat = (ax25_digi *)kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL)
1127                                 return -ENOMEM;
1128                 }
1129 
1130                 sk->ax25->digipeat->ndigi = ndigi;
1131 
1132                 while (ct < ndigi) {
1133                         sk->ax25->digipeat->repeated[ct] = 0;
1134                         memcpy(&sk->ax25->digipeat->calls[ct], &ap[ct], sizeof(ax25_address));
1135                         ct++;
1136                 }
1137 
1138                 sk->ax25->digipeat->lastrepeat = 0;
1139                 addr_len -= ndigi * sizeof(ax25_address);       
1140         }
1141 
1142         if (addr_len != sizeof(struct sockaddr_ax25))
1143                 return -EINVAL;
1144 
1145         if (sk->zapped) {       /* Must bind first - autobinding in this may or may not work */
1146                 if ((err = ax25_rt_autobind(sk->ax25, &addr->sax25_call)) < 0)
1147                         return err;
1148                 ax25_insert_socket(sk->ax25);           /* Finish the bind */
1149         }
1150                 
1151         if (sk->type == SOCK_SEQPACKET && ax25_find_cb(&sk->ax25->source_addr, &addr->sax25_call, sk->ax25->device) != NULL)
1152                 return -EBUSY;                          /* Already such a connection */
1153                 
1154         memcpy(&sk->ax25->dest_addr, &addr->sax25_call, sizeof(ax25_address));
1155         
1156         /* First the easy one */
1157         if (sk->type != SOCK_SEQPACKET) {
1158                 sock->state = SS_CONNECTED;
1159                 sk->state   = TCP_ESTABLISHED;
1160                 return 0;
1161         }
1162         
1163         /* Move to connecting socket, ax.25 lapb WAIT_UA.. */   
1164         sock->state        = SS_CONNECTING;
1165         sk->state          = TCP_SYN_SENT;
1166         ax25_establish_data_link(sk->ax25);
1167         sk->ax25->state     = AX25_STATE_1;
1168         ax25_set_timer(sk->ax25);               /* Start going SABM SABM until a UA or a give up and DM */
1169         
1170         /* Now the loop */
1171         if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
1172                 return -EINPROGRESS;
1173                 
1174         cli();  /* To avoid races on the sleep */
1175 
1176         /* A DM or timeout will go to closed, a UA will go to ABM */
1177         while (sk->state == TCP_SYN_SENT) {
1178                 interruptible_sleep_on(sk->sleep);
1179                 if (current->signal & ~current->blocked) {
1180                         sti();
1181                         return -ERESTARTSYS;
1182                 }
1183         }
1184 
1185         if (sk->state != TCP_ESTABLISHED) {     /* Not in ABM, not in WAIT_UA -> failed */
1186                 sti();
1187                 sock->state = SS_UNCONNECTED;
1188                 return -sk->err;        /* Always set at this point */
1189         }
1190         
1191         sock->state = SS_CONNECTED;
1192 
1193         sti();
1194         
1195         return 0;
1196 }
1197         
1198 static int ax25_socketpair(struct socket *sock1, struct socket *sock2)
     /* [previous][next][first][last][top][bottom][index][help] */
1199 {
1200         return -EOPNOTSUPP;
1201 }
1202 
1203 static int ax25_accept(struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1204 {
1205         struct sock *sk;
1206         struct sock *newsk;
1207         struct sk_buff *skb;
1208 
1209         if (newsock->data)
1210                 kfree_s(newsock->data, sizeof(struct sock));
1211 
1212         newsock->data = NULL;
1213         
1214         sk = (struct sock *)sock->data;
1215 
1216         if (sk->type != SOCK_SEQPACKET)
1217                 return -EOPNOTSUPP;
1218         
1219         if (sk->state != TCP_LISTEN)
1220                 return -EINVAL;
1221                 
1222         /* The write queue this time is holding sockets ready to use
1223            hooked into the SABM we saved */
1224         do {
1225                 cli();
1226                 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
1227                         if (flags & O_NONBLOCK) {
1228                                 sti();
1229                                 return 0;
1230                         }
1231                         interruptible_sleep_on(sk->sleep);
1232                         if (current->signal & ~current->blocked) {
1233                                 sti();
1234                                 return -ERESTARTSYS;
1235                         }
1236                 }
1237         } while (skb == NULL);
1238 
1239         newsk = skb->sk;
1240         newsk->pair = NULL;
1241         sti();
1242 
1243         /* Now attach up the new socket */
1244         skb->sk = NULL;
1245         kfree_skb(skb, FREE_READ);
1246         sk->ack_backlog--;
1247         newsock->data = newsk;
1248 
1249         return 0;
1250 }
1251 
1252 static int ax25_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1253         int *uaddr_len, int peer)
1254 {
1255         ax25_address *addr;
1256         struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
1257         struct sock *sk;
1258         unsigned char ndigi, i;
1259         
1260         sk = (struct sock *)sock->data;
1261         
1262         if (peer != 0) {
1263                 if (sk->state != TCP_ESTABLISHED)
1264                         return -ENOTCONN;
1265                 addr = &sk->ax25->dest_addr;
1266         } else {
1267                 addr = &sk->ax25->source_addr;
1268         }
1269                 
1270         sax->fsa_ax25.sax25_family = AF_AX25;
1271         memcpy(&sax->fsa_ax25.sax25_call, addr, sizeof(ax25_address));
1272         sax->fsa_ax25.sax25_ndigis = 0;
1273         *uaddr_len = sizeof(struct sockaddr_ax25);
1274 
1275         /* This will supply digipeat path on both getpeername() and getsockname() */
1276         if (sk->ax25->digipeat != NULL) {
1277                 ndigi = sk->ax25->digipeat->ndigi;
1278                 sax->fsa_ax25.sax25_ndigis = ndigi;
1279                 *uaddr_len += sizeof(ax25_address) * ndigi;
1280                 for (i = 0; i < ndigi; i++)
1281                         memcpy(&sax->fsa_digipeater[i], &sk->ax25->digipeat->calls[i], sizeof(ax25_address));
1282         }
1283 
1284         return 0;
1285 }
1286  
1287 static int ax25_rcv(struct sk_buff *skb, struct device *dev, ax25_address *dev_addr, struct packet_type *ptype)
     /* [previous][next][first][last][top][bottom][index][help] */
1288 {
1289         struct sock *make;
1290         struct sock *sk;
1291         int type = 0;
1292         ax25_digi dp;
1293         ax25_cb *ax25;
1294         ax25_address src, dest;
1295         struct sock *raw;
1296         int mine = 0;
1297 
1298         /*
1299          *      Process the AX.25/LAPB frame.
1300          */
1301 
1302         /*
1303          *      Parse the address header.
1304          */
1305         if (ax25_parse_addr(skb->data, skb->len, &src, &dest, &dp, &type) == NULL) {
1306                 kfree_skb(skb, FREE_READ);
1307                 return 0;
1308         }
1309 
1310         /*
1311          *      Send the frame to the AX.25 auto-router
1312          */
1313         ax25_rt_rx_frame(&src, dev);
1314         
1315         /*
1316          *      Ours perhaps ?
1317          */
1318         if (dp.lastrepeat + 1 < dp.ndigi) {             /* Not yet digipeated completely */
1319                 if (ax25cmp(&dp.calls[dp.lastrepeat + 1], dev_addr) == 0) {
1320                         /* We are the digipeater. Mark ourselves as repeated
1321                            and throw the packet back out of the same device */
1322                         dp.lastrepeat++;
1323                         dp.repeated[(int)dp.lastrepeat] = 1;
1324 #ifdef CONFIG_AX25_XDIGI
1325                         while (dp.lastrepeat + 1 < dp.ndigi) {
1326                                 struct device *dev_scan;
1327                                 if ((dev_scan = ax25rtr_get_dev(&dp.calls[dp.lastrepeat + 1])) == NULL)
1328                                         break;
1329                                 dp.lastrepeat++;
1330                                 dp.repeated[(int)dp.lastrepeat] = 1;
1331                                 dev = dev_scan;
1332                         }
1333 #endif
1334                         build_ax25_addr(skb->data, &src, &dest, &dp, type, MODULUS);
1335                         skb->arp = 1;
1336                         if (ax25_digi_on) {
1337                                 ax25_queue_xmit(skb, dev, SOPRI_NORMAL);
1338                         } else {
1339                                 kfree_skb(skb, FREE_READ);
1340                         }
1341                 } else {
1342                         kfree_skb(skb, FREE_READ);
1343                 }
1344 
1345                 return 0;
1346         }
1347 
1348         /*
1349          *      Pull of the AX.25 headers leaving the CTRL/PID bytes
1350          */
1351         skb_pull(skb, size_ax25_addr(&dp));
1352 
1353         /* For our port addreses ? */
1354         if (ax25cmp(&dest, dev_addr) == 0)
1355                 mine = 1;
1356 
1357 #ifdef CONFIG_NETROM
1358         /* Also match on any NET/ROM callsign */
1359         if (!mine && nr_dev_get(&dest) != NULL)
1360                 mine = 1;
1361 #endif  
1362         
1363         if ((*skb->data & ~0x10) == LAPB_UI) {  /* UI frame - bypass LAPB processing */
1364                 skb->h.raw = skb->data + 2;             /* skip control and pid */
1365 
1366                 if ((raw = ax25_addr_match(&dest)) != NULL)
1367                         ax25_send_to_raw(raw, skb, skb->data[1]);
1368 
1369                 if (!mine && ax25cmp(&dest, (ax25_address *)dev->broadcast) != 0) {
1370                         kfree_skb(skb, FREE_READ);
1371                         return 0;
1372                 }
1373 
1374                 /* Now we are pointing at the pid byte */
1375                 switch (skb->data[1]) {
1376 #ifdef CONFIG_INET              
1377                         case AX25_P_IP:
1378                                 skb_pull(skb,2);                /* drop PID/CTRL */
1379                                 ax25_ip_mode_set(&src, dev, 'D');
1380                                 ip_rcv(skb, dev, ptype);        /* Note ptype here is the wrong one, fix me later */
1381                                 break;
1382 
1383                         case AX25_P_ARP:
1384                                 skb_pull(skb,2);
1385                                 arp_rcv(skb, dev, ptype);       /* Note ptype here is wrong... */
1386                                 break;
1387 #endif                          
1388                         case AX25_P_TEXT:
1389                                 /* Now find a suitable dgram socket */
1390                                 if ((sk = ax25_find_socket(&dest, &src, SOCK_DGRAM)) != NULL) {
1391                                         if (sk->rmem_alloc >= sk->rcvbuf) {
1392                                                 kfree_skb(skb, FREE_READ);
1393                                         } else {
1394                                                 /*
1395                                                  *      Remove the control and PID.
1396                                                  */
1397                                                 skb_pull(skb, 2);
1398                                                 skb_queue_tail(&sk->receive_queue, skb);
1399                                                 skb->sk = sk;
1400                                                 sk->rmem_alloc += skb->truesize;
1401                                                 if (!sk->dead)
1402                                                         sk->data_ready(sk, skb->len);
1403                                         }
1404                                 } else {
1405                                         kfree_skb(skb, FREE_READ);
1406                                 }
1407                                 break;  
1408 
1409                         default:
1410                                 kfree_skb(skb, FREE_READ);      /* Will scan SOCK_AX25 RAW sockets */
1411                                 break;
1412                 }
1413 
1414                 return 0;
1415         }
1416         
1417         /* LAPB */
1418         if ((ax25 = ax25_find_cb(&dest, &src, dev)) != NULL) {
1419                 /*
1420                  *      Process the frame. If it is queued up internally it returns one otherwise we 
1421                  *      free it immediately. This routine itself wakes the user context layers so we
1422                  *      do no further work
1423                  */
1424                 if (ax25_process_rx_frame(ax25, skb, type) == 0)
1425                         kfree_skb(skb, FREE_READ);
1426 
1427                 return 0;
1428         }
1429 
1430         if ((*skb->data & ~PF) != SABM && (*skb->data & ~PF) != SABME) {
1431                 /*
1432                  *      Never reply to a DM. Also ignore any connects for
1433                  *      addresses that are not our interfaces and not a socket.
1434                  */
1435                 if ((*skb->data & ~PF) != DM && mine)
1436                         ax25_return_dm(dev, &src, &dest, &dp);
1437 
1438                 kfree_skb(skb, FREE_READ);
1439                 return 0;
1440         }
1441 
1442         if ((sk = ax25_find_listener(&dest, dev, SOCK_SEQPACKET)) != NULL) {
1443                 if (sk->ack_backlog == sk->max_ack_backlog || (make = ax25_make_new(sk, dev)) == NULL) {
1444                         if (mine)
1445                                 ax25_return_dm(dev, &src, &dest, &dp);
1446 
1447                         kfree_skb(skb, FREE_READ);
1448                         return 0;
1449                 }
1450                 
1451                 ax25 = make->ax25;
1452 
1453                 skb_queue_head(&sk->receive_queue, skb);
1454 
1455                 skb->sk     = make;
1456                 make->state = TCP_ESTABLISHED;
1457                 make->pair  = sk;
1458 
1459                 sk->ack_backlog++;
1460         } else {
1461 #ifdef CONFIG_NETROM
1462                 if (!mine) {
1463                         kfree_skb(skb, FREE_READ);
1464                         return 0;
1465                 }
1466 
1467                 if ((ax25 = ax25_create_cb()) == NULL) {
1468                         ax25_return_dm(dev, &src, &dest, &dp);
1469                         kfree_skb(skb, FREE_READ);
1470                         return 0;
1471                 }
1472 #else
1473                 if (mine)
1474                         ax25_return_dm(dev, &src, &dest, &dp);
1475 
1476                 kfree_skb(skb, FREE_READ);
1477                 return 0;
1478 #endif
1479         }
1480 
1481         memcpy(&ax25->source_addr, &dest, sizeof(ax25_address));
1482         memcpy(&ax25->dest_addr,   &src,  sizeof(ax25_address));
1483 
1484         /*
1485          *      Sort out any digipeated paths.
1486          */
1487         if (dp.ndigi != 0 && ax25->digipeat == NULL && (ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
1488                 kfree_skb(skb, FREE_READ);
1489                 ax25_destroy_socket(ax25);
1490                 return 0;
1491         }
1492 
1493         if (dp.ndigi == 0) {
1494                 if (ax25->digipeat != NULL) {
1495                         kfree_s(ax25->digipeat, sizeof(ax25_digi));
1496                         ax25->digipeat = NULL;
1497                 }
1498         } else {
1499                 /* Reverse the source SABM's path */
1500                 ax25_digi_invert(&dp, ax25->digipeat);
1501         }
1502 
1503         if ((*skb->data & ~PF) == SABME) {
1504                 ax25->modulus = EMODULUS;
1505         } else {
1506                 ax25->modulus = MODULUS;
1507         }
1508 
1509         ax25->device = dev;
1510         
1511         ax25_send_control(ax25, UA, POLLON, C_RESPONSE);
1512 
1513         ax25->t3timer = ax25->t3;
1514         ax25->state   = AX25_STATE_3;
1515 
1516         ax25_insert_socket(ax25);
1517 
1518         ax25_set_timer(ax25);
1519 
1520         if (sk != NULL) {
1521                 if (!sk->dead)
1522                         sk->data_ready(sk, skb->len );
1523         } else {
1524                 kfree_skb(skb, FREE_READ);
1525         }
1526 
1527         return 0;
1528 }
1529 
1530 /*
1531  *      Receive an AX.25 frame via a SLIP interface.
1532  */
1533 static int kiss_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *ptype)
     /* [previous][next][first][last][top][bottom][index][help] */
1534 {
1535         skb->sk = NULL;         /* Initially we don't know who its for */
1536 
1537         if ((*skb->data & 0x0F) != 0) {
1538                 kfree_skb(skb, FREE_READ);      /* Not a KISS data frame */
1539                 return 0;
1540         }
1541 
1542         skb_pull(skb, AX25_KISS_HEADER_LEN);    /* Remove the KISS byte */
1543 
1544         return ax25_rcv(skb, dev, (ax25_address *)dev->dev_addr, ptype);
1545 }
1546 
1547 /*
1548  *      Receive an AX.25 frame via an Ethernet interface.
1549  */
1550 static int bpq_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *ptype)
     /* [previous][next][first][last][top][bottom][index][help] */
1551 {
1552         ax25_address port_call;
1553         int len;
1554 
1555         skb->sk = NULL;         /* Initially we don't know who its for */
1556 
1557         if (!arp_query((unsigned char *)&port_call, dev->pa_addr, ARPHRD_AX25)) {
1558                 kfree_skb(skb, FREE_READ);      /* We have no port callsign */
1559                 return 0;
1560         }
1561 
1562         len = skb->data[0] + skb->data[1] * 256 - 5;
1563 
1564         skb_pull(skb, 2);       /* Remove the length bytes */
1565         skb_trim(skb, len);     /* Set the length of the data */
1566 
1567         return ax25_rcv(skb, dev, &port_call, ptype);
1568 }
1569 
1570 static int ax25_sendto(struct socket *sock, const void *ubuf, int len, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
1571         unsigned flags, struct sockaddr *usip, int addr_len)
1572 {
1573         struct sock *sk = (struct sock *)sock->data;
1574         struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)usip;
1575         unsigned char *uaddr = (unsigned char *)usip;
1576         int err;
1577         struct sockaddr_ax25 sax;
1578         struct sk_buff *skb;
1579         unsigned char *asmptr;
1580         int size;
1581         ax25_digi *dp;
1582         ax25_digi dtmp;
1583         int lv;
1584         
1585         if (sk->err) {
1586                 err     = sk->err;
1587                 sk->err = 0;
1588                 return -err;
1589         }
1590 
1591         if (flags)
1592                 return -EINVAL;
1593 
1594         if (sk->zapped)
1595                 return -EADDRNOTAVAIL;
1596                 
1597         if (sk->ax25->device == NULL)
1598                 return -ENETUNREACH;
1599                 
1600         if (usax) {
1601                 int ndigi = addr_len - sizeof(sax);
1602                 if (addr_len < sizeof(sax))
1603                         return -EINVAL;
1604 
1605                 /* Trailing digipeaters on address ?? */
1606                 if (addr_len > sizeof(sax)) {
1607                         int ct = 0;
1608 
1609                         ax25_address *ap = (ax25_address *)(((char *)uaddr) + sizeof(sax));
1610                         /* Size is an exact number of digipeaters ? */
1611                         if (ndigi % sizeof(ax25_address))
1612                                 return -EINVAL;
1613                         ndigi /= sizeof(ax25_address);
1614 
1615                         /* Valid number of digipeaters ? */
1616                         if (ndigi < 1 || ndigi > 6)
1617                                 return -EINVAL;
1618 
1619                         /* Copy data into digipeat structure */
1620                         while (ct < ndigi) {
1621                                 dtmp.repeated[ct] = 0;
1622                                 memcpy(&dtmp.calls[ct], &ap[ct], sizeof(ax25_address));
1623                                 ct++;
1624                         }
1625 
1626                         dtmp.lastrepeat = 0;
1627                         dtmp.ndigi      = ndigi;
1628                         addr_len -= ndigi * sizeof(ax25_address);       
1629                 }
1630 
1631                 memcpy(&sax, usax, sizeof(sax));
1632                 if (sk->type == SOCK_SEQPACKET && memcmp(&sk->ax25->dest_addr, &sax.sax25_call, sizeof(ax25_address)) != 0)
1633                         return -EISCONN;
1634                 if (sax.sax25_family != AF_AX25)
1635                         return -EINVAL;
1636                 if (ndigi != 0)
1637                         dp = &dtmp;
1638                 else
1639                         dp = NULL;
1640         } else {
1641                 if (sk->state != TCP_ESTABLISHED)
1642                         return -ENOTCONN;
1643                 sax.sax25_family = AF_AX25;
1644                 memcpy(&sax.sax25_call, &sk->ax25->dest_addr, sizeof(ax25_address));
1645                 dp = sk->ax25->digipeat;
1646         }
1647         
1648         if (sk->debug)
1649                 printk("AX.25: sendto: Addresses built.\n");
1650 
1651         /* Build a packet */
1652         if (sk->debug)
1653                 printk("AX.25: sendto: building packet.\n");
1654 
1655         /* Assume the worst case */
1656         size = len + 3 + size_ax25_addr(dp) + AX25_BPQ_HEADER_LEN;
1657 
1658         if ((skb = sock_alloc_send_skb(sk, size, 0, &err)) == NULL)
1659                 return err;
1660 
1661         skb->sk   = sk;
1662         skb->free = 1;
1663         skb->arp  = 1;
1664 
1665         skb_reserve(skb, size - len);
1666 
1667         if (sk->debug)
1668                 printk("AX.25: Appending user data\n");
1669 
1670         /* User data follows immediately after the AX.25 data */
1671         memcpy_fromfs(skb_put(skb, len), ubuf, len);
1672 
1673         /* Add the PID, usually AX25_TEXT */
1674         asmptr  = skb_push(skb, 1);
1675         *asmptr = sk->protocol;
1676 
1677         if (sk->debug)
1678                 printk("AX.25: Transmitting buffer\n");
1679 
1680         if (sk->type == SOCK_SEQPACKET) {
1681                 /* Connected mode sockets go via the LAPB machine */
1682                 if (sk->state != TCP_ESTABLISHED) {
1683                         kfree_skb(skb, FREE_WRITE);
1684                         return -ENOTCONN;
1685                 }
1686 
1687                 ax25_output(sk->ax25, skb);     /* Shove it onto the queue and kick */
1688 
1689                 return len;
1690         } else {
1691                 asmptr = skb_push(skb, 1 + size_ax25_addr(dp));
1692 
1693                 if (sk->debug) {
1694                         printk("Building AX.25 Header (dp=%p).\n", dp);
1695                         if (dp != 0)
1696                                 printk("Num digipeaters=%d\n", dp->ndigi);
1697                 }
1698 
1699                 /* Build an AX.25 header */
1700                 asmptr += (lv = build_ax25_addr(asmptr, &sk->ax25->source_addr, &sax.sax25_call, dp, C_COMMAND, MODULUS));
1701 
1702                 if (sk->debug)
1703                         printk("Built header (%d bytes)\n",lv);
1704 
1705                 skb->h.raw = asmptr;
1706         
1707                 if (sk->debug)
1708                         printk("base=%p pos=%p\n", skb->data, asmptr);
1709 
1710                 *asmptr = LAPB_UI;
1711 
1712                 /* Datagram frames go straight out of the door as UI */
1713                 ax25_queue_xmit(skb, sk->ax25->device, SOPRI_NORMAL);
1714 
1715                 return len;
1716         }
1717                 
1718 }
1719 
1720 static int ax25_send(struct socket *sock, const void *ubuf, int size, int noblock, unsigned flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1721 {
1722         return ax25_sendto(sock, ubuf, size, noblock, flags, NULL, 0);
1723 }
1724 
1725 static int ax25_write(struct socket *sock, const char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1726 {
1727         return ax25_send(sock, ubuf, size, noblock, 0);
1728 }
1729 
1730 static int ax25_recvfrom(struct socket *sock, void *ubuf, int size, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
1731                    unsigned flags, struct sockaddr *sip, int *addr_len)
1732 {
1733         struct sock *sk = (struct sock *)sock->data;
1734         struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)sip;
1735         char *addrptr = (char *)sip;
1736         int copied = 0;
1737         struct sk_buff *skb;
1738         int er;
1739         int bias = 0;
1740 
1741         if (sk->err) {
1742                 er      = -sk->err;
1743                 sk->err = 0;
1744                 return er;
1745         }
1746         
1747         if (addr_len != NULL)
1748                 *addr_len = sizeof(*sax);
1749 
1750         /* This works for seqpacket too. The receiver has ordered the queue for us! We do one quick check first though */
1751         if (sk->type == SOCK_SEQPACKET) {
1752                 if (sk->state != TCP_ESTABLISHED)
1753                         return -ENOTCONN;
1754                 bias = 1;
1755         }
1756 
1757         /* Now we can treat all alike */
1758         if ((skb = skb_recv_datagram(sk, flags, noblock, &er)) == NULL)
1759                 return er;
1760 
1761         copied = (size < skb->len - bias) ? size : skb->len - bias;
1762         skb_copy_datagram(skb, bias, ubuf, copied);
1763         
1764         if (sax) {
1765                 struct sockaddr_ax25 addr;
1766                 ax25_digi digi;
1767                 ax25_address dest;
1768                 unsigned char *dp = skb->data;
1769                 int ct = 0;
1770                 
1771                 ax25_parse_addr(dp, skb->len, NULL, &dest, &digi, NULL);
1772                 addr.sax25_family = AF_AX25;
1773                 memcpy(&addr.sax25_call, &dest, sizeof(ax25_address));
1774                 memcpy(sax,&addr, sizeof(*sax));
1775                 addrptr += sizeof(*sax);
1776 
1777                 while (ct < digi.ndigi) {
1778                         memcpy(addrptr, &digi. calls[ct], AX25_ADDR_LEN);
1779                         addrptr += AX25_ADDR_LEN;
1780                         ct++;
1781                 }
1782                 if (addr_len)
1783                         *addr_len = sizeof(*sax) + AX25_ADDR_LEN * digi.ndigi;
1784         }
1785 
1786         skb_free_datagram(skb);
1787 
1788         return copied;
1789 }               
1790 
1791 static int ax25_recv(struct socket *sock, void *ubuf, int size , int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
1792         unsigned flags)
1793 {
1794         struct sock *sk = (struct sock *)sock->data;
1795 
1796         if (sk->zapped)
1797                 return -ENOTCONN;
1798 
1799         return ax25_recvfrom(sock, ubuf, size, noblock, flags, NULL, NULL);
1800 }
1801 
1802 static int ax25_read(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1803 {
1804         return ax25_recv(sock, ubuf, size, noblock, 0);
1805 }
1806 
1807 static int ax25_shutdown(struct socket *sk, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1808 {
1809         /* FIXME - generate DM and RNR states */
1810         return -EOPNOTSUPP;
1811 }
1812 
1813 static int ax25_select(struct socket *sock , int sel_type, select_table *wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1814 {
1815         struct sock *sk = (struct sock *)sock->data;
1816 
1817         return datagram_select(sk, sel_type, wait);
1818 }
1819 
1820 static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1821 {
1822         struct sock *sk = (struct sock *)sock->data;
1823         int err;
1824         long amount = 0;
1825 
1826         switch (cmd) {
1827                 case TIOCOUTQ:
1828                         if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(unsigned long))) != 0)
1829                                 return err;
1830                         amount = sk->sndbuf - sk->wmem_alloc;
1831                         if (amount < 0)
1832                                 amount = 0;
1833                         put_fs_long(amount, (unsigned long *)arg);
1834                         return 0;
1835 
1836                 case TIOCINQ:
1837                 {
1838                         struct sk_buff *skb;
1839                         /* These two are safe on a single CPU system as only user tasks fiddle here */
1840                         if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1841                                 amount = skb->len;
1842                         if ((err = verify_area(VERIFY_WRITE, (void *)arg, sizeof(unsigned long))) != 0)
1843                                 return err;
1844                         put_fs_long(amount, (unsigned long *)arg);
1845                         return 0;
1846                 }
1847 
1848                 case SIOCGSTAMP:
1849                         if (sk != NULL) {
1850                                 if (sk->stamp.tv_sec==0)
1851                                         return -ENOENT;
1852                                 if ((err = verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval))) != 0)
1853                                         return err;
1854                                 memcpy_tofs((void *)arg, &sk->stamp, sizeof(struct timeval));
1855                                 return 0;
1856                         }
1857                         return -EINVAL;
1858 
1859                 case SIOCAX25ADDUID:    /* Add a uid to the uid/call map table */
1860                 case SIOCAX25DELUID:    /* Delete a uid from the uid/call map table */
1861                 case SIOCAX25GETUID:
1862                 {
1863                         struct sockaddr_ax25 sax25;
1864                         if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(struct sockaddr_ax25))) != 0)
1865                                 return err;
1866                         memcpy_fromfs(&sax25, (void *)arg, sizeof(sax25));
1867                         return ax25_uid_ioctl(cmd, &sax25);
1868                 }
1869 
1870                 case SIOCAX25NOUID:     /* Set the default policy (default/bar) */
1871                         if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(unsigned long))) != 0)
1872                                 return err;
1873                         if(!suser())
1874                                 return -EPERM;
1875                         amount = get_fs_long((void *)arg);
1876                         if (amount > AX25_NOUID_BLOCK)
1877                                 return -EINVAL;
1878                         ax25_uid_policy = amount;
1879                         return 0;
1880 
1881                 case SIOCAX25DIGCTL:
1882                         if ((err = verify_area(VERIFY_READ, (void *)arg, sizeof(int))) != 0)
1883                                 return err;
1884                         if (!suser())
1885                                 return -EPERM;
1886                         amount = get_fs_long((void *)arg);
1887                         ax25_digi_on = amount ? 1 : 0;
1888                         return 0;
1889 
1890                 case SIOCGIFADDR:
1891                 case SIOCSIFADDR:
1892                 case SIOCGIFDSTADDR:
1893                 case SIOCSIFDSTADDR:
1894                 case SIOCGIFBRDADDR:
1895                 case SIOCSIFBRDADDR:
1896                 case SIOCGIFNETMASK:
1897                 case SIOCSIFNETMASK:
1898                 case SIOCGIFMETRIC:
1899                 case SIOCSIFMETRIC:
1900                         return -EINVAL;
1901 
1902                 default:
1903                         return(dev_ioctl(cmd, (void *)arg));
1904         }
1905 
1906         /*NOTREACHED*/
1907         return(0);
1908 }
1909 
1910 int ax25_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
1911 {
1912         ax25_cb *ax25;
1913         struct device *dev;
1914         const char *devname;
1915         int len = 0;
1916         off_t pos = 0;
1917         off_t begin = 0;
1918   
1919         cli();
1920 
1921         len += sprintf(buffer, "dest_addr src_addr  dev  st  vs  vr  va    t1     t2     t3     n2  rtt wnd Snd-Q Rcv-Q\n");
1922 
1923         for (ax25 = ax25_list; ax25 != NULL; ax25 = ax25->next) {
1924                 if ((dev = ax25->device) == NULL)
1925                         devname = "???";
1926                 else
1927                         devname = dev->name;
1928 
1929                 len += sprintf(buffer + len, "%-9s ",
1930                         ax2asc(&ax25->dest_addr));
1931                 len += sprintf(buffer + len, "%-9s %-4s %2d %3d %3d %3d %3d/%03d %2d/%02d %3d/%03d %2d/%02d %3d %3d",
1932                         ax2asc(&ax25->source_addr), devname,
1933                         ax25->state,
1934                         ax25->vs, ax25->vr, ax25->va,
1935                         ax25->t1timer / PR_SLOWHZ,
1936                         ax25->t1      / PR_SLOWHZ,
1937                         ax25->t2timer / PR_SLOWHZ,
1938                         ax25->t2      / PR_SLOWHZ,
1939                         ax25->t3timer / PR_SLOWHZ,
1940                         ax25->t3      / PR_SLOWHZ,
1941                         ax25->n2count, ax25->n2,
1942                         ax25->rtt     / PR_SLOWHZ,
1943                         ax25->window);
1944 
1945                 if (ax25->sk != NULL) {
1946                         len += sprintf(buffer + len, " %5ld %5ld\n",
1947                                 ax25->sk->wmem_alloc,
1948                                 ax25->sk->rmem_alloc);
1949                 } else {
1950                         len += sprintf(buffer + len, "\n");
1951                 }
1952                 
1953                 pos = begin + len;
1954 
1955                 if (pos < offset) {
1956                         len   = 0;
1957                         begin = pos;
1958                 }
1959                 
1960                 if (pos > offset + length)
1961                         break;
1962         }
1963 
1964         sti();
1965 
1966         *start = buffer + (offset - begin);
1967         len   -= (offset - begin);
1968 
1969         if (len > length) len = length;
1970 
1971         return(len);
1972 } 
1973 
1974 static struct proto_ops ax25_proto_ops = {
1975         AF_AX25,
1976         
1977         ax25_create,
1978         ax25_dup,
1979         ax25_release,
1980         ax25_bind,
1981         ax25_connect,
1982         ax25_socketpair,
1983         ax25_accept,
1984         ax25_getname,
1985         ax25_read,
1986         ax25_write,
1987         ax25_select,
1988         ax25_ioctl,
1989         ax25_listen,
1990         ax25_send,
1991         ax25_recv,
1992         ax25_sendto,
1993         ax25_recvfrom,
1994         ax25_shutdown,
1995         ax25_setsockopt,
1996         ax25_getsockopt,
1997         ax25_fcntl,
1998 };
1999 
2000 /* Called by socket.c on kernel start up */
2001 
2002 static struct packet_type ax25_packet_type = 
2003 {
2004         0,      /* MUTTER ntohs(ETH_P_AX25),*/
2005         0,              /* copy */
2006         kiss_rcv,
2007         NULL,
2008         NULL,
2009 };
2010 
2011 static struct packet_type bpq_packet_type = 
2012 {
2013         0,      /* MUTTER ntohs(ETH_P_BPQ),*/
2014         0,              /* copy */
2015         bpq_rcv,
2016         NULL,
2017         NULL,
2018 };
2019 
2020 static struct notifier_block ax25_dev_notifier = {
2021         ax25_device_event,
2022         0
2023 };
2024 
2025 void ax25_proto_init(struct net_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
2026 {
2027         sock_register(ax25_proto_ops.family, &ax25_proto_ops);
2028         ax25_packet_type.type = htons(ETH_P_AX25);
2029         dev_add_pack(&ax25_packet_type);        
2030         bpq_packet_type.type  = htons(ETH_P_BPQ);
2031         dev_add_pack(&bpq_packet_type);
2032         register_netdevice_notifier(&ax25_dev_notifier);
2033                           
2034 proc_net_register(&(struct proc_dir_entry)
2035         { PROC_NET_AX25_ROUTE, 10, "ax25_route", ax25_rt_get_info });
2036 proc_net_register(&(struct proc_dir_entry)
2037         { PROC_NET_AX25, 4, "ax25", ax25_get_info });
2038 proc_net_register(&(struct proc_dir_entry)
2039         { PROC_NET_AX25_CALLS, 10, "ax25_calls", ax25_cs_get_info });
2040 
2041         printk("GW4PTS/G4KLX AX.25 for Linux. Version 0.30 ALPHA for Linux NET3.030 (Linux 1.3.0)\n");
2042 }
2043 
2044 /*
2045  *      A small shim to dev_queue_xmit to handle the difference between
2046  *      KISS AX.25 and BPQ AX.25.
2047  */
2048 void ax25_queue_xmit(struct sk_buff *skb, struct device *dev, int pri)
     /* [previous][next][first][last][top][bottom][index][help] */
2049 {
2050         static char bcast_addr[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2051         unsigned char *ptr;
2052         int size;
2053 
2054         if (dev->type == ARPHRD_ETHER) {
2055                 if (skb_headroom(skb) < AX25_BPQ_HEADER_LEN) {
2056                         printk("ax25_queue_xmit: not enough space to add BPQ Ether header\n");
2057                         skb->free = 1;
2058                         kfree_skb(skb, FREE_WRITE);
2059                         return;
2060                 }
2061 
2062                 size = skb->len;
2063         
2064                 ptr = skb_push(skb, 2);
2065 
2066                 *ptr++ = (size + 5) % 256;
2067                 *ptr++ = (size + 5) / 256;
2068 
2069                 dev->hard_header(skb, dev, ETH_P_BPQ, bcast_addr, NULL, 0);
2070         } else {
2071                 ptr = skb_push(skb, 1);
2072                 
2073                 *ptr++ = 0;                     /* KISS */
2074         }
2075 
2076         dev_queue_xmit(skb, dev, pri);
2077 }
2078 
2079 /*******************************************************************************************************************\
2080 *                                                                                                                   *
2081 *               Driver encapsulation support: Moved out of SLIP because a) it should be here                        *
2082 *                                                                       b) for HDLC cards                           *
2083 *                                                                                                                   *
2084 \*******************************************************************************************************************/
2085 
2086 /*
2087  *      Shove an AX.25 UI header on an IP packet and handle ARP
2088  */
2089 
2090 #ifdef CONFIG_INET
2091  
2092 int ax25_encapsulate(struct sk_buff *skb, struct device *dev, unsigned short type, void *daddr,
     /* [previous][next][first][last][top][bottom][index][help] */
2093                 void *saddr, unsigned len)
2094 {
2095         /* header is an AX.25 UI frame from us to them */
2096         unsigned char *buff = skb_push(skb, AX25_HEADER_LEN);
2097 
2098         *buff++ = 0;    /* KISS DATA */
2099         
2100         if (daddr != NULL)
2101                 memcpy(buff, daddr, dev->addr_len);     /* Address specified */
2102 
2103         buff[6] &= ~LAPB_C;
2104         buff[6] &= ~LAPB_E;
2105         buff[6] |= SSSID_SPARE;
2106         buff += AX25_ADDR_LEN;
2107 
2108         if (saddr != NULL)
2109                 memcpy(buff, saddr, dev->addr_len);
2110         else
2111                 memcpy(buff, dev->dev_addr, dev->addr_len);
2112 
2113         buff[6] &= ~LAPB_C;
2114         buff[6] |= LAPB_E;
2115         buff[6] |= SSSID_SPARE;
2116         buff   += AX25_ADDR_LEN;
2117 
2118         *buff++ = LAPB_UI;      /* UI */
2119 
2120         /* Append a suitable AX.25 PID */
2121         switch (type) {
2122                 case ETH_P_IP:
2123                         *buff++ = AX25_P_IP;
2124                         break;
2125 
2126                 case ETH_P_ARP:
2127                         *buff++ = AX25_P_ARP;
2128                         break;
2129                 default:
2130                         *buff++ = 0;
2131                         break;
2132         }
2133         
2134         if (daddr != NULL)
2135                 return AX25_HEADER_LEN;
2136 
2137         return -AX25_HEADER_LEN;        /* Unfinished header */
2138 }
2139 
2140 int ax25_rebuild_header(unsigned char *bp, struct device *dev, unsigned long dest, struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
2141 {
2142         int mode;
2143 
2144         if (arp_find(bp + 1, dest, dev, dev->pa_addr, skb))
2145                 return 1;
2146 
2147         if (bp[16] == AX25_P_IP) {
2148                 mode = ax25_ip_mode_get((ax25_address *)(bp + 1), dev);
2149 
2150                 if (mode == 'V' || mode == 'v' || (mode == ' ' && sl_get_ax25_mode(dev))) {
2151                         skb_device_unlock(skb);
2152                         skb_pull(skb, AX25_HEADER_LEN - 1);     /* Keep PID */
2153                         ax25_send_frame(skb, (ax25_address *)(bp + 8), (ax25_address *)(bp + 1), NULL, dev);
2154                         return 1;
2155                 }
2156         }
2157 
2158         bp[7]  &= ~LAPB_C;
2159         bp[7]  &= ~LAPB_E;
2160         bp[7]  |= SSSID_SPARE;
2161 
2162         bp[14] &= ~LAPB_C;
2163         bp[14] |= LAPB_E;
2164         bp[14] |= SSSID_SPARE;
2165 
2166         return 0;
2167 }       
2168 
2169 #endif
2170 
2171 #endif

/* [previous][next][first][last][top][bottom][index][help] */