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

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