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

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