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

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

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