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

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