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

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