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

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