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

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

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