root/net/ax25/af_ax25.c

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

DEFINITIONS

This source file includes following definitions.
  1. ax2asc
  2. ax25cmp
  3. ax25_remove_socket
  4. ax25_kill_by_device
  5. ax25_device_event
  6. ax25_insert_socket
  7. ax25_find_listener
  8. ax25_find_socket
  9. ax25_find_cb
  10. ax25_addr_match
  11. ax25_send_to_raw
  12. ax25_destroy_timer
  13. ax25_destroy_socket
  14. ax25_findbyuid
  15. ax25_uid_ioctl
  16. ax25_create_cb
  17. ax25_send_frame
  18. ax25rtr_get_dev
  19. ax25_fcntl
  20. ax25_setsockopt
  21. ax25_getsockopt
  22. ax25_listen
  23. def_callback1
  24. def_callback2
  25. ax25_create
  26. ax25_make_new
  27. ax25_dup
  28. ax25_release
  29. ax25_bind
  30. ax25_connect
  31. ax25_socketpair
  32. ax25_accept
  33. ax25_getname
  34. ax25_rcv
  35. kiss_rcv
  36. bpq_rcv
  37. ax25_sendto
  38. ax25_send
  39. ax25_write
  40. ax25_recvfrom
  41. ax25_recv
  42. ax25_read
  43. ax25_shutdown
  44. ax25_select
  45. ax25_ioctl
  46. ax25_get_info
  47. ax25_proto_init
  48. ax25_queue_xmit
  49. ax25_encapsulate
  50. ax25_rebuild_header

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

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