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_dev_is_dama_slave
  18. ax25_fillin_cb
  19. ax25_send_frame
  20. ax25rtr_get_dev
  21. ax25_fcntl
  22. ax25_setsockopt
  23. ax25_getsockopt
  24. ax25_listen
  25. def_callback1
  26. def_callback2
  27. ax25_create
  28. ax25_make_new
  29. ax25_dup
  30. ax25_release
  31. ax25_bind
  32. ax25_connect
  33. ax25_socketpair
  34. ax25_accept
  35. ax25_getname
  36. ax25_rcv
  37. kiss_rcv
  38. bpq_rcv
  39. ax25_sendmsg
  40. ax25_recvmsg
  41. ax25_shutdown
  42. ax25_select
  43. ax25_ioctl
  44. ax25_get_info
  45. ax25_proto_init
  46. ax25_queue_xmit
  47. ax25_encapsulate
  48. ax25_rebuild_header

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

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