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

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