root/net/appletalk/ddp.c

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

DEFINITIONS

This source file includes following definitions.
  1. atalk_remove_socket
  2. atalk_insert_socket
  3. atalk_search_socket
  4. atalk_find_socket
  5. atalk_destroy_timer
  6. atalk_destroy_socket
  7. atalk_get_info
  8. atif_drop_device
  9. atif_add_device
  10. atif_probe_device
  11. atalk_find_dev_addr
  12. atalk_find_primary
  13. atalk_find_dev
  14. atalk_find_anynet
  15. atalk_find_interface
  16. atrtr_find
  17. atrtr_get_dev
  18. atrtr_set_default
  19. atrtr_create
  20. atrtr_delete
  21. atrtr_device_down
  22. ddp_device_event
  23. atif_ioctl
  24. atrtr_ioctl
  25. atalk_if_get_info
  26. atalk_rt_get_info
  27. atalk_checksum
  28. atalk_fcntl
  29. atalk_setsockopt
  30. atalk_getsockopt
  31. atalk_listen
  32. def_callback1
  33. def_callback2
  34. atalk_create
  35. atalk_dup
  36. atalk_release
  37. atalk_pick_port
  38. atalk_autobind
  39. atalk_bind
  40. atalk_connect
  41. atalk_socketpair
  42. atalk_accept
  43. atalk_getname
  44. atalk_rcv
  45. atalk_sendmsg
  46. atalk_recvmsg
  47. atalk_shutdown
  48. atalk_select
  49. atalk_ioctl
  50. atalk_proto_init

   1 /*
   2  *      DDP:    An implementation of the Appletalk DDP protocol for
   3  *              ethernet 'ELAP'.
   4  *
   5  *              Alan Cox  <Alan.Cox@linux.org>
   6  *                        <iialan@www.linux.org.uk>
   7  *
   8  *              With more than a little assistance from 
   9  *      
  10  *              Wesley Craig <netatalk@umich.edu>
  11  *
  12  *      Fixes:
  13  *              Michael Callahan        :       Made routing work
  14  *              Wesley Craig            :       Fix probing to listen to a
  15  *                                              passed node id.
  16  *              Alan Cox                :       Added send/recvmsg support
  17  *              Alan Cox                :       Moved at. to protinfo in
  18  *                                              socket.
  19  *              Alan Cox                :       Added firewall hooks.
  20  *              Alan Cox                :       Supports new ARPHRD_LOOPBACK
  21  *
  22  *              This program is free software; you can redistribute it and/or
  23  *              modify it under the terms of the GNU General Public License
  24  *              as published by the Free Software Foundation; either version
  25  *              2 of the License, or (at your option) any later version.
  26  *
  27  *      TODO
  28  *              ASYNC I/O
  29  */
  30  
  31 #include <asm/segment.h>
  32 #include <asm/system.h>
  33 #include <asm/bitops.h>
  34 #include <linux/config.h>
  35 #include <linux/types.h>
  36 #include <linux/kernel.h>
  37 #include <linux/sched.h>
  38 #include <linux/string.h>
  39 #include <linux/mm.h>
  40 #include <linux/socket.h>
  41 #include <linux/sockios.h>
  42 #include <linux/in.h>
  43 #include <linux/errno.h>
  44 #include <linux/interrupt.h>
  45 #include <linux/if_ether.h>
  46 #include <linux/if_arp.h>
  47 #include <linux/route.h>
  48 #include <linux/inet.h>
  49 #include <linux/notifier.h>
  50 #include <linux/netdevice.h>
  51 #include <linux/etherdevice.h>
  52 #include <linux/skbuff.h>
  53 #include <linux/termios.h>      /* For TIOCOUTQ/INQ */
  54 #include <net/datalink.h>
  55 #include <net/p8022.h>
  56 #include <net/psnap.h>
  57 #include <net/sock.h>
  58 #include <linux/atalk.h>
  59 #include <linux/proc_fs.h>
  60 #include <linux/stat.h>
  61 #include <linux/firewall.h>
  62 
  63 #ifdef CONFIG_ATALK
  64 
  65 #define APPLETALK_DEBUG
  66 
  67 
  68 #ifdef APPLETALK_DEBUG
  69 #define DPRINT(x)               print(x)
  70 #else
  71 #define DPRINT(x)
  72 #endif
  73 
  74 struct datalink_proto *ddp_dl, *aarp_dl;
  75 
  76 #define min(a,b)        (((a)<(b))?(a):(b))
  77 
  78 /***********************************************************************************************************************\
  79 *                                                                                                                       *
  80 *                                               Handlers for the socket list.                                           *
  81 *                                                                                                                       *
  82 \***********************************************************************************************************************/
  83 
  84 static atalk_socket *volatile atalk_socket_list=NULL;
  85 
  86 /*
  87  *      Note: Sockets may not be removed _during_ an interrupt or inet_bh
  88  *      handler using this technique. They can be added although we do not
  89  *      use this facility.
  90  */
  91  
  92 static void atalk_remove_socket(atalk_socket *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
  93 {
  94         unsigned long flags;
  95         atalk_socket *s;
  96         
  97         save_flags(flags);
  98         cli();
  99         
 100         s=atalk_socket_list;
 101         if(s==sk)
 102         {
 103                 atalk_socket_list=s->next;
 104                 restore_flags(flags);
 105                 return;
 106         }
 107         while(s && s->next)
 108         {
 109                 if(s->next==sk)
 110                 {
 111                         s->next=sk->next;
 112                         restore_flags(flags);
 113                         return;
 114                 }
 115                 s=s->next;
 116         }
 117         restore_flags(flags);
 118 }
 119 
 120 static void atalk_insert_socket(atalk_socket *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 121 {
 122         unsigned long flags;
 123         save_flags(flags);
 124         cli();
 125         sk->next=atalk_socket_list;
 126         atalk_socket_list=sk;
 127         restore_flags(flags);
 128 }
 129 
 130 static atalk_socket *atalk_search_socket(struct sockaddr_at *to, struct atalk_iface *atif)
     /* [previous][next][first][last][top][bottom][index][help] */
 131 {
 132         atalk_socket *s;
 133 
 134         for( s = atalk_socket_list; s != NULL; s = s->next ) 
 135         {
 136                 if ( to->sat_port != s->protinfo.af_at.src_port ) 
 137                 {
 138                         continue;
 139                 }
 140 
 141                 if ( to->sat_addr.s_net == 0 &&
 142                     to->sat_addr.s_node == ATADDR_BCAST &&
 143                     s->protinfo.af_at.src_net == atif->address.s_net ) 
 144                 {
 145                         break;
 146                 }
 147 
 148                 if ( to->sat_addr.s_net == s->protinfo.af_at.src_net &&
 149                     to->sat_addr.s_node == s->protinfo.af_at.src_node ) 
 150                 {
 151                         break;
 152                 }
 153 
 154                 /* XXXX.0 */
 155         }
 156         return( s );
 157 }
 158 
 159 /*
 160  *      Find a socket in the list.
 161  */
 162  
 163 static atalk_socket *atalk_find_socket(struct sockaddr_at *sat)
     /* [previous][next][first][last][top][bottom][index][help] */
 164 {
 165         atalk_socket *s;
 166 
 167         for ( s = atalk_socket_list; s != NULL; s = s->next ) 
 168         {
 169                 if ( s->protinfo.af_at.src_net != sat->sat_addr.s_net ) 
 170                 {
 171                         continue;
 172                 }
 173                 if ( s->protinfo.af_at.src_node != sat->sat_addr.s_node ) 
 174                 {
 175                         continue;
 176                 }
 177                 if ( s->protinfo.af_at.src_port != sat->sat_port ) 
 178                 {
 179                         continue;
 180                 }
 181                 break;
 182         }
 183         return( s );
 184 }
 185 
 186 /*
 187  *      This is only called from user mode. Thus it protects itself against
 188  *      interrupt users but doesn't worry about being called during work.
 189  *      Once it is removed from the queue no interrupt or bottom half will
 190  *      touch it and we are (fairly 8-) ) safe.
 191  */
 192 
 193 static void atalk_destroy_socket(atalk_socket *sk);
 194 
 195 /*
 196  *      Handler for deferred kills.
 197  */
 198  
 199 static void atalk_destroy_timer(unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
 200 {
 201         atalk_destroy_socket((atalk_socket *)data);
 202 }
 203 
 204 static void atalk_destroy_socket(atalk_socket *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 205 {
 206         struct sk_buff *skb;
 207         atalk_remove_socket(sk);
 208         
 209         while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
 210         {
 211                 kfree_skb(skb,FREE_READ);
 212         }
 213         
 214         if(sk->wmem_alloc == 0 && sk->rmem_alloc == 0 && sk->dead)
 215                 kfree_s(sk,sizeof(*sk));
 216         else
 217         {
 218                 /*
 219                  *      Someone is using our buffers still.. defer
 220                  */
 221                 init_timer(&sk->timer);
 222                 sk->timer.expires=jiffies+10*HZ;
 223                 sk->timer.function=atalk_destroy_timer;
 224                 sk->timer.data = (unsigned long)sk;
 225                 add_timer(&sk->timer);
 226         }
 227 }
 228 
 229 
 230 /*
 231  *      Called from proc fs 
 232  */
 233  
 234 int atalk_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
 235 {
 236         atalk_socket *s;
 237         int len=0;
 238         off_t pos=0;
 239         off_t begin=0;
 240 
 241         /*
 242          *      Output the appletalk data for the /proc virtual fs.
 243          */
 244 
 245         len += sprintf (buffer,"Type local_addr  remote_addr tx_queue rx_queue st uid\n");
 246         for (s = atalk_socket_list; s != NULL; s = s->next)
 247         {
 248                 len += sprintf (buffer+len,"%02X   ", s->type);
 249                 len += sprintf (buffer+len,"%04X:%02X:%02X  ",
 250                         s->protinfo.af_at.src_net,s->protinfo.af_at.src_node,s->protinfo.af_at.src_port);
 251                 len += sprintf (buffer+len,"%04X:%02X:%02X  ",
 252                         s->protinfo.af_at.dest_net,s->protinfo.af_at.dest_node,s->protinfo.af_at.dest_port);
 253                 len += sprintf (buffer+len,"%08lX:%08lX ", s->wmem_alloc, s->rmem_alloc);
 254                 len += sprintf (buffer+len,"%02X %d\n", s->state, SOCK_INODE(s->socket)->i_uid);
 255                 
 256                 /* Are we still dumping unwanted data then discard the record */
 257                 pos=begin+len;
 258                 
 259                 if(pos<offset)
 260                 {
 261                         len=0;                  /* Keep dumping into the buffer start */
 262                         begin=pos;
 263                 }
 264                 if(pos>offset+length)           /* We have dumped enough */
 265                         break;
 266         }
 267         
 268         /* The data in question runs from begin to begin+len */
 269         *start=buffer+(offset-begin);   /* Start of wanted data */
 270         len-=(offset-begin);            /* Remove unwanted header data from length */
 271         if(len>length)
 272                 len=length;             /* Remove unwanted tail data from length */
 273         
 274         return len;
 275 }
 276 
 277 /*******************************************************************************************************************\
 278 *                                                                                                                   *
 279 *                                       Routing tables for the Appletalk socket layer                               *
 280 *                                                                                                                   *
 281 \*******************************************************************************************************************/
 282 
 283 
 284 static struct atalk_route *atalk_router_list=NULL;
 285 static struct atalk_route atrtr_default;                /* For probing devices or in a routerless network */
 286 static struct atalk_iface *atalk_iface_list=NULL;
 287 
 288 /*
 289  *      Appletalk interface control
 290  */
 291  
 292 /*
 293  *      Drop a device. Doesn't drop any of its routes - that is the 
 294  *      the callers problem. Called when we down the interface or 
 295  *      delete the address.
 296  */
 297  
 298 static void atif_drop_device(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 299 {
 300         struct atalk_iface **iface = &atalk_iface_list;
 301         struct atalk_iface *tmp;
 302 
 303         while ((tmp = *iface) != NULL) 
 304         {
 305                 if (tmp->dev == dev) 
 306                 {
 307                         *iface = tmp->next;
 308                         kfree_s(tmp, sizeof(struct atalk_iface));
 309                 }
 310                 else
 311                         iface = &tmp->next;
 312         }
 313 }
 314 
 315 static struct atalk_iface *atif_add_device(struct device *dev, struct at_addr *sa)
     /* [previous][next][first][last][top][bottom][index][help] */
 316 {
 317         struct atalk_iface *iface=(struct atalk_iface *)
 318                                         kmalloc(sizeof(*iface), GFP_KERNEL);
 319         unsigned long flags;                                    
 320         if(iface==NULL)
 321                 return NULL;
 322         iface->dev=dev;
 323         iface->address= *sa;
 324         iface->status=0;
 325         save_flags(flags);
 326         cli();
 327         iface->next=atalk_iface_list;
 328         atalk_iface_list=iface;
 329         restore_flags(flags);
 330         return iface;
 331 }
 332 
 333 /*
 334  *      Perform phase 2 AARP probing on our tentative address.
 335  */
 336  
 337 static int atif_probe_device(struct atalk_iface *atif)
     /* [previous][next][first][last][top][bottom][index][help] */
 338 {
 339         int ct;
 340         int netrange=ntohs(atif->nets.nr_lastnet)-ntohs(atif->nets.nr_firstnet)+1;
 341         int probe_net=ntohs(atif->address.s_net);
 342         int probe_node=atif->address.s_node;
 343         int netct;
 344         int nodect;
 345         
 346         
 347         /*
 348          *      Offset the network we start probing with.
 349          */
 350          
 351         if(probe_net==ATADDR_ANYNET)
 352         {
 353                 if(!netrange)
 354                         probe_net=ntohs(atif->nets.nr_firstnet);
 355                 else
 356                         probe_net=ntohs(atif->nets.nr_firstnet) + (jiffies%netrange);
 357         }
 358         
 359         if(probe_node == ATADDR_ANYNODE)
 360                 probe_node = jiffies&0xFF;
 361         
 362         
 363         /*
 364          *      Scan the networks.
 365          */
 366          
 367         for(netct=0;netct<=netrange;netct++)
 368         {
 369                 /*
 370                  *      Sweep the available nodes from a given start.
 371                  */
 372 
 373                 atif->address.s_net=htons(probe_net);
 374                 for(nodect=0;nodect<256;nodect++)
 375                 {
 376                         atif->address.s_node=((nodect+probe_node)&0xFF);
 377                         if(atif->address.s_node>0&&atif->address.s_node<254)
 378                         {
 379                                 /*
 380                                  *      Probe a proposed address.
 381                                  */
 382                                 for(ct=0;ct<AARP_RETRANSMIT_LIMIT;ct++)
 383                                 {
 384                                         aarp_send_probe(atif->dev, &atif->address);
 385                                         /*
 386                                          *      Defer 1/10th
 387                                          */
 388                                         current->timeout = jiffies + (HZ/10);
 389                                         current->state = TASK_INTERRUPTIBLE;
 390                                         schedule();
 391                                         if(atif->status&ATIF_PROBE_FAIL)
 392                                                 break;
 393                                 }
 394                                 if(!(atif->status&ATIF_PROBE_FAIL))
 395                                         return 0;
 396                         }
 397                         atif->status&=~ATIF_PROBE_FAIL;
 398                 }
 399                 probe_net++;
 400                 if(probe_net>ntohs(atif->nets.nr_lastnet))
 401                         probe_net=ntohs(atif->nets.nr_firstnet);
 402         }
 403         return -EADDRINUSE;     /* Network is full... */
 404 }
 405 
 406 struct at_addr *atalk_find_dev_addr(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 407 {
 408         struct atalk_iface *iface;
 409         for(iface=atalk_iface_list;iface!=NULL;iface=iface->next)
 410                 if(iface->dev==dev)
 411                         return &iface->address;
 412         return NULL;
 413 }
 414 
 415 static struct at_addr *atalk_find_primary(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 416 {
 417         struct atalk_iface *iface;
 418         for(iface=atalk_iface_list;iface!=NULL;iface=iface->next)
 419                 if(!(iface->dev->flags&IFF_LOOPBACK))
 420                         return &iface->address;
 421         if ( atalk_iface_list != NULL ) {
 422             return &atalk_iface_list->address;
 423         } else {
 424             return NULL;
 425         }
 426 }
 427 
 428 /*
 429  *      Give a device find its atif control structure
 430  */
 431  
 432 struct atalk_iface *atalk_find_dev(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 433 {
 434         struct atalk_iface *iface;
 435         for(iface=atalk_iface_list;iface!=NULL;iface=iface->next)
 436                 if(iface->dev==dev)
 437                         return iface;
 438         return NULL;
 439 }
 440 
 441 /*
 442  *      Find a match for 'any network' - ie any of our interfaces with that
 443  *      node number will do just nicely.
 444  */
 445  
 446 static struct atalk_iface *atalk_find_anynet(int node, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 447 {
 448         struct atalk_iface *iface;
 449         for(iface=atalk_iface_list;iface!=NULL;iface=iface->next) 
 450         {
 451                 if ( iface->dev != dev || ( iface->status & ATIF_PROBE ))
 452                         continue;
 453                 if ( node == ATADDR_BCAST || iface->address.s_node == node ) 
 454                         return iface;
 455         }
 456         return NULL;
 457 }
 458 
 459 /*
 460  *      Find a match for a specific network:node pair
 461  */
 462  
 463 static struct atalk_iface *atalk_find_interface(int net, int node)
     /* [previous][next][first][last][top][bottom][index][help] */
 464 {
 465         struct atalk_iface *iface;
 466         for(iface=atalk_iface_list;iface!=NULL;iface=iface->next)
 467         {
 468                 if((node==ATADDR_BCAST || iface->address.s_node==node) 
 469                         && iface->address.s_net==net && !(iface->status&ATIF_PROBE))
 470                         return iface;
 471         }
 472         return NULL;
 473 }
 474 
 475 
 476 /*
 477  *      Find a route for an appletalk packet. This ought to get cached in
 478  *      the socket (later on...). We know about host routes and the fact
 479  *      that a route must be direct to broadcast.
 480  */
 481  
 482 static struct atalk_route *atrtr_find(struct at_addr *target)
     /* [previous][next][first][last][top][bottom][index][help] */
 483 {
 484         struct atalk_route *r;
 485         for(r=atalk_router_list;r!=NULL;r=r->next)
 486         {
 487                 if(!(r->flags&RTF_UP))
 488                         continue;
 489                 if(r->target.s_net==target->s_net)
 490                 {
 491                         if(!(r->flags&RTF_HOST) || r->target.s_node==target->s_node)
 492                                 return r;
 493                 }
 494         }
 495         if(atrtr_default.dev)
 496                 return &atrtr_default;
 497         return NULL;
 498 }
 499 
 500                         
 501 /*
 502  *      Given an appletalk network find the device to use. This can be
 503  *      a simple lookup. Funny stuff like routers can wait 8)
 504  */
 505  
 506 static struct device *atrtr_get_dev(struct at_addr *sa)
     /* [previous][next][first][last][top][bottom][index][help] */
 507 {
 508         struct atalk_route *atr=atrtr_find(sa);
 509         if(atr==NULL)
 510                 return NULL;
 511         else
 512                 return atr->dev;
 513 }
 514 
 515 /*
 516  *      Set up a default router.
 517  */
 518  
 519 static void atrtr_set_default(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 520 {
 521         atrtr_default.dev=dev;
 522         atrtr_default.flags= RTF_UP;
 523         atrtr_default.gateway.s_net=htons(0);
 524         atrtr_default.gateway.s_node=0;
 525 }
 526 
 527 /*
 528  *      Add a router. Basically make sure it looks valid and stuff the
 529  *      entry in the list. While it uses netranges we always set them to one
 530  *      entry to work like netatalk.
 531  */
 532  
 533 static int atrtr_create(struct rtentry *r, struct device *devhint)
     /* [previous][next][first][last][top][bottom][index][help] */
 534 {
 535         struct sockaddr_at *ta=(struct sockaddr_at *)&r->rt_dst;
 536         struct sockaddr_at *ga=(struct sockaddr_at *)&r->rt_gateway;
 537         struct atalk_route *rt;
 538         struct atalk_iface *iface, *riface;
 539         unsigned long flags;
 540         
 541         save_flags(flags);
 542         
 543         /*
 544          *      Fixme: Raise/Lower a routing change semaphore for these
 545          *      operations.
 546          */
 547          
 548         /*
 549          *      Validate the request
 550          */     
 551         if(ta->sat_family!=AF_APPLETALK)
 552                 return -EINVAL;
 553         if(devhint == NULL && ga->sat_family != AF_APPLETALK)
 554                 return -EINVAL;
 555         
 556         /*
 557          *      Now walk the routing table and make our decisions
 558          */
 559 
 560         for(rt=atalk_router_list;rt!=NULL;rt=rt->next)
 561         {
 562                 if(r->rt_flags != rt->flags)
 563                         continue;
 564 
 565                 if(ta->sat_addr.s_net == rt->target.s_net) 
 566                 {
 567                         if(!(rt->flags&RTF_HOST))
 568                                 break;
 569                         if(ta->sat_addr.s_node == rt->target.s_node)
 570                                 break;
 571                 }
 572         }
 573 
 574         if ( devhint == NULL ) 
 575         {
 576                 for ( riface = NULL, iface = atalk_iface_list; iface; iface = iface->next ) 
 577                 {
 578                         if ( riface == NULL && ntohs( ga->sat_addr.s_net ) >= ntohs( iface->nets.nr_firstnet ) &&
 579                                 ntohs( ga->sat_addr.s_net ) <=  ntohs( iface->nets.nr_lastnet ))
 580                         {
 581                                 riface = iface;
 582                         }
 583                         if ( ga->sat_addr.s_net == iface->address.s_net && ga->sat_addr.s_node == iface->address.s_node )
 584                                 riface = iface;
 585                 }
 586                 if ( riface == NULL )
 587                         return -ENETUNREACH;
 588                 devhint = riface->dev;
 589         }
 590 
 591         if(rt==NULL)
 592         {
 593                 rt=(struct atalk_route *)kmalloc(sizeof(struct atalk_route), GFP_KERNEL);
 594                 if(rt==NULL)
 595                         return -ENOBUFS;
 596                 cli();
 597                 rt->next=atalk_router_list;
 598                 atalk_router_list=rt;
 599         }
 600 
 601         /*
 602          *      Fill in the entry.
 603          */
 604         rt->target=ta->sat_addr;                        
 605         rt->dev=devhint;
 606         rt->flags=r->rt_flags;
 607         rt->gateway=ga->sat_addr;
 608         
 609         restore_flags(flags);
 610         return 0;
 611 }
 612 
 613 
 614 /*
 615  *      Delete a route. Find it and discard it.
 616  */
 617  
 618 static int atrtr_delete( struct at_addr *addr )
     /* [previous][next][first][last][top][bottom][index][help] */
 619 {
 620         struct atalk_route **r = &atalk_router_list;
 621         struct atalk_route *tmp;
 622 
 623         while ((tmp = *r) != NULL) 
 624         {
 625                 if (tmp->target.s_net == addr->s_net &&
 626                             (!(tmp->flags&RTF_GATEWAY) ||
 627                             tmp->target.s_node == addr->s_node )) 
 628                 {
 629                         *r = tmp->next;
 630                         kfree_s(tmp, sizeof(struct atalk_route));
 631                         return 0;
 632                 }
 633                 r = &tmp->next;
 634         }
 635         return -ENOENT;
 636 }
 637 
 638 /*
 639  *      Called when a device is downed. Just throw away any routes
 640  *      via it.
 641  */
 642  
 643 void atrtr_device_down(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 644 {
 645         struct atalk_route **r = &atalk_router_list;
 646         struct atalk_route *tmp;
 647 
 648         while ((tmp = *r) != NULL) 
 649         {
 650                 if (tmp->dev == dev) 
 651                 {
 652                         *r = tmp->next;
 653                         kfree_s(tmp, sizeof(struct atalk_route));
 654                 }
 655                 else
 656                         r = &tmp->next;
 657         }
 658         if(atrtr_default.dev==dev)
 659                 atrtr_set_default(NULL);
 660 }
 661 
 662 /*
 663  *      A device event has occured. Watch for devices going down and
 664  *      delete our use of them (iface and route).
 665  */
 666 
 667 static int ddp_device_event(struct notifier_block *this, unsigned long event, void *ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 668 {
 669         if(event==NETDEV_DOWN)
 670         {
 671                 /* Discard any use of this */
 672                 atrtr_device_down((struct device *)ptr);
 673                 atif_drop_device((struct device *)ptr);
 674         }
 675         return NOTIFY_DONE;
 676 }
 677 
 678 /*
 679  *      ioctl calls. Shouldn't even need touching.
 680  */
 681 
 682 /*
 683  *      Device configuration ioctl calls.
 684  */
 685  
 686 int atif_ioctl(int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 687 {
 688         struct ifreq atreq;
 689         static char aarp_mcast[6]={0x09,0x00,0x00,0xFF,0xFF,0xFF};
 690         struct netrange *nr;
 691         struct sockaddr_at *sa;
 692         struct device *dev;
 693         struct atalk_iface *atif;
 694         int ro=(cmd==SIOCSIFADDR);
 695         int err=verify_area(ro?VERIFY_READ:VERIFY_WRITE, arg,sizeof(atreq));
 696         int ct;
 697         int limit;
 698         struct rtentry rtdef;
 699         
 700         if(err)
 701                 return err;
 702         
 703         memcpy_fromfs(&atreq,arg,sizeof(atreq));
 704         
 705         if((dev=dev_get(atreq.ifr_name))==NULL)
 706                 return -ENODEV;
 707                 
 708         sa=(struct sockaddr_at*)&atreq.ifr_addr;
 709         atif=atalk_find_dev(dev);
 710         
 711         switch(cmd)
 712         {
 713                 case SIOCSIFADDR:
 714                         if(!suser())
 715                                 return -EPERM;
 716                         if(sa->sat_family!=AF_APPLETALK)
 717                                 return -EINVAL;
 718                         if(dev->type!=ARPHRD_ETHER&&dev->type!=ARPHRD_LOOPBACK)
 719                                 return -EPROTONOSUPPORT;
 720                         nr=(struct netrange *)&sa->sat_zero[0];
 721                         if(nr->nr_phase!=2)
 722                                 return -EPROTONOSUPPORT;
 723                         if(sa->sat_addr.s_node==ATADDR_BCAST || sa->sat_addr.s_node == 254)
 724                                 return -EINVAL;
 725                         if(atif)
 726                         {
 727                                 /*
 728                                  *      Already setting address.
 729                                  */
 730                                 if(atif->status&ATIF_PROBE)
 731                                         return -EBUSY;
 732                                         
 733                                 atif->address.s_net=sa->sat_addr.s_net;
 734                                 atif->address.s_node=sa->sat_addr.s_node;
 735                                 atrtr_device_down(dev); /* Flush old routes */
 736                         }
 737                         else
 738                         {
 739                                 atif=atif_add_device(dev, &sa->sat_addr);
 740                         }
 741                         atif->nets= *nr;
 742 
 743                         /*
 744                          *      Check if the chosen address is used. If so we 
 745                          *      error and atalkd will try another. 
 746                          */
 747                          
 748                         if(!(dev->flags&IFF_LOOPBACK) && atif_probe_device(atif)<0)
 749                         {
 750                                 atif_drop_device(dev);
 751                                 return -EADDRINUSE;
 752                         }
 753 
 754                         /*
 755                          *      Hey it worked - add the direct
 756                          *      routes.
 757                          */
 758                                 
 759                         sa=(struct sockaddr_at *)&rtdef.rt_gateway;
 760                         sa->sat_family=AF_APPLETALK;
 761                         sa->sat_addr.s_net=atif->address.s_net;
 762                         sa->sat_addr.s_node=atif->address.s_node;
 763                         sa=(struct sockaddr_at *)&rtdef.rt_dst;
 764                         rtdef.rt_flags=RTF_UP;
 765                         sa->sat_family=AF_APPLETALK;
 766                         sa->sat_addr.s_node=ATADDR_ANYNODE;
 767                         if(dev->flags&IFF_LOOPBACK)
 768                                 rtdef.rt_flags|=RTF_HOST;
 769                         /*
 770                          *      Routerless initial state.
 771                          */
 772                         if(nr->nr_firstnet==htons(0) && nr->nr_lastnet==htons(0xFFFE)) 
 773                         {
 774                                 sa->sat_addr.s_net=atif->address.s_net;
 775                                 atrtr_create(&rtdef, dev);
 776                                 atrtr_set_default(dev);
 777                         } 
 778                         else 
 779                         {
 780                                 limit=ntohs(nr->nr_lastnet);
 781                                 if(limit-ntohs(nr->nr_firstnet) > 256)
 782                                 {
 783                                         printk("Too many routes/iface.\n");
 784                                         return -EINVAL;
 785                                 }
 786                                 for(ct=ntohs(nr->nr_firstnet);ct<=limit;ct++)
 787                                 {
 788                                         sa->sat_addr.s_net=htons(ct);
 789                                         atrtr_create(&rtdef, dev);
 790                                 }
 791                         }
 792                         dev_mc_add(dev, aarp_mcast, 6, 1);
 793                         return 0;
 794                 case SIOCGIFADDR:
 795                         if(atif==NULL)
 796                                 return -EADDRNOTAVAIL;
 797                         ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_family=AF_APPLETALK;
 798                         ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_addr=atif->address;
 799                         break;
 800                 case SIOCGIFBRDADDR:
 801                         if(atif==NULL)
 802                                 return -EADDRNOTAVAIL;
 803                         ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_family=AF_APPLETALK;
 804                         ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_addr.s_net=atif->address.s_net;
 805                         ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_addr.s_node=ATADDR_BCAST;
 806                         break;
 807         }
 808         memcpy_tofs(arg,&atreq,sizeof(atreq));
 809         return 0;
 810 }
 811 
 812 /*
 813  *      Routing ioctl() calls
 814  */
 815   
 816 static int atrtr_ioctl(unsigned int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 817 {
 818         int err;
 819         struct rtentry rt;
 820         
 821         err=verify_area(VERIFY_READ, arg, sizeof(rt));
 822         if(err)
 823                 return err;
 824         memcpy_fromfs(&rt,arg,sizeof(rt));
 825         
 826         switch(cmd)
 827         {
 828                 case SIOCDELRT:
 829                         if(rt.rt_dst.sa_family!=AF_APPLETALK)
 830                                 return -EINVAL;
 831                         return atrtr_delete(&((struct sockaddr_at *)&rt.rt_dst)->sat_addr);
 832                 case SIOCADDRT:
 833                         return atrtr_create(&rt, NULL);
 834                 default:
 835                         return -EINVAL;
 836         }
 837 }
 838 
 839 /* Called from proc fs - just make it print the ifaces neatly */
 840 
 841 int atalk_if_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
 842 {
 843         struct atalk_iface *iface;
 844         int len=0;
 845         off_t pos=0;
 846         off_t begin=0;
 847 
 848         len += sprintf (buffer,"Interface         Address   Networks   Status\n");
 849         for (iface = atalk_iface_list; iface != NULL; iface = iface->next)
 850         {
 851                 len += sprintf (buffer+len,"%-16s %04X:%02X  %04X-%04X  %d\n",
 852                         iface->dev->name,
 853                         ntohs(iface->address.s_net),iface->address.s_node,
 854                         ntohs(iface->nets.nr_firstnet),ntohs(iface->nets.nr_lastnet),
 855                         iface->status);
 856                 pos=begin+len;
 857                 if(pos<offset)
 858                 {
 859                         len=0;
 860                         begin=pos;
 861                 }
 862                 if(pos>offset+length)
 863                         break;
 864         }
 865         *start=buffer+(offset-begin);
 866         len-=(offset-begin);
 867         if(len>length)
 868                 len=length;
 869         return len;
 870 }
 871 
 872 /* Called from proc fs - just make it print the routes neatly */
 873 
 874 int atalk_rt_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
 875 {
 876         struct atalk_route *rt;
 877         int len=0;
 878         off_t pos=0;
 879         off_t begin=0;
 880 
 881         len += sprintf (buffer,"Target        Router  Flags Dev\n");
 882         if(atrtr_default.dev)
 883         {
 884                 rt=&atrtr_default;
 885                 len += sprintf (buffer+len,"Default     %5d:%-3d  %-4d  %s\n",
 886                         ntohs(rt->gateway.s_net), rt->gateway.s_node, rt->flags,
 887                         rt->dev->name);
 888         }
 889         for (rt = atalk_router_list; rt != NULL; rt = rt->next)
 890         {
 891                 len += sprintf (buffer+len,"%04X:%02X     %5d:%-3d  %-4d  %s\n",
 892                         ntohs(rt->target.s_net),rt->target.s_node,
 893                         ntohs(rt->gateway.s_net), rt->gateway.s_node, rt->flags,
 894                         rt->dev->name);
 895                 pos=begin+len;
 896                 if(pos<offset)
 897                 {
 898                         len=0;
 899                         begin=pos;
 900                 }
 901                 if(pos>offset+length)
 902                         break;
 903         }
 904         *start=buffer+(offset-begin);
 905         len-=(offset-begin);
 906         if(len>length)
 907                 len=length;
 908         return len;
 909 }
 910 
 911 /*******************************************************************************************************************\
 912 *                                                                                                                   *
 913 *             Handling for system calls applied via the various interfaces to an Appletalk socket object            *
 914 *                                                                                                                   *
 915 \*******************************************************************************************************************/
 916 
 917 /*
 918  *      Checksum: This is 'optional'. It's quite likely also a good
 919  *      candidate for assembler hackery 8)
 920  */
 921  
 922 unsigned short atalk_checksum(struct ddpehdr *ddp, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 923 {
 924         unsigned long sum=0;    /* Assume unsigned long is >16 bits */
 925         unsigned char *data=(unsigned char *)ddp;
 926         
 927         len-=4;         /* skip header 4 bytes */
 928         data+=4;        
 929         
 930         /* This ought to be unwrapped neatly. I'll trust gcc for now */
 931         while(len--)
 932         {
 933                 sum+=*data;
 934                 sum<<=1;
 935                 if(sum&0x10000)
 936                 {
 937                         sum++;
 938                         sum&=0xFFFF;
 939                 }
 940                 data++;
 941         }
 942         if(sum)
 943                 return htons((unsigned short)sum);
 944         return 0xFFFF;          /* Use 0xFFFF for 0. 0 itself means none */
 945 }
 946         
 947 /*
 948  *      Generic fcntl calls are already dealt with. If we don't need funny ones
 949  *      this is the all you need. Async I/O is also seperate.
 950  */
 951   
 952 static int atalk_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 953 {
 954 /*      atalk_socket *sk=(atalk_socket *)sock->data;*/
 955         switch(cmd)
 956         {
 957                 default:
 958                         return(-EINVAL);
 959         }
 960 }
 961 
 962 /*
 963  *      Set 'magic' options for appletalk. If we don't have any this is fine 
 964  *      as it is.
 965  */
 966  
 967 static int atalk_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 968 {
 969         atalk_socket *sk;
 970         int err,opt;
 971         
 972         sk=(atalk_socket *)sock->data;
 973         
 974         if(optval==NULL)
 975                 return(-EINVAL);
 976 
 977         err=verify_area(VERIFY_READ,optval,sizeof(int));
 978         if(err)
 979                 return err;
 980         opt=get_fs_long((unsigned long *)optval);
 981         
 982         switch(level)
 983         {
 984                 case SOL_ATALK:
 985                         switch(optname)
 986                         {
 987                                 default:
 988                                         return -EOPNOTSUPP;
 989                         }
 990                         break;
 991                         
 992                 case SOL_SOCKET:
 993                         return sock_setsockopt(sk,level,optname,optval,optlen);
 994 
 995                 default:
 996                         return -EOPNOTSUPP;
 997         }
 998 }
 999 
1000 
1001 /*
1002  *      Get any magic options. Comment above applies.
1003  */
1004  
1005 static int atalk_getsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
1006         char *optval, int *optlen)
1007 {
1008         atalk_socket *sk;
1009         int val=0;
1010         int err;
1011         
1012         sk=(atalk_socket *)sock->data;
1013 
1014         switch(level)
1015         {
1016 
1017                 case SOL_ATALK:
1018                         switch(optname)
1019                         {
1020                                 default:
1021                                         return -ENOPROTOOPT;
1022                         }
1023                         break;
1024                         
1025                 case SOL_SOCKET:
1026                         return sock_getsockopt(sk,level,optname,optval,optlen);
1027                         
1028                 default:
1029                         return -EOPNOTSUPP;
1030         }
1031         err=verify_area(VERIFY_WRITE,optlen,sizeof(int));
1032         if(err)
1033                 return err;
1034         put_user(sizeof(int),optlen);
1035         err=verify_area(VERIFY_WRITE,optval,sizeof(int));
1036         if (err) 
1037                 return err;
1038         put_user(val,optval);
1039         return(0);
1040 }
1041 
1042 /*
1043  *      Only for connection oriented sockets - ignore
1044  */
1045  
1046 static int atalk_listen(struct socket *sock, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
1047 {
1048         return -EOPNOTSUPP;
1049 }
1050 
1051 /*
1052  *      These are standard.
1053  */
1054  
1055 static void def_callback1(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1056 {
1057         if(!sk->dead)
1058                 wake_up_interruptible(sk->sleep);
1059 }
1060 
1061 static void def_callback2(struct sock *sk, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1062 {
1063         if(!sk->dead)
1064         {
1065                 wake_up_interruptible(sk->sleep);
1066                 sock_wake_async(sk->socket,0);
1067         }
1068 }
1069 
1070 /*
1071  *      Create a socket. Initialise the socket, blank the addresses
1072  *      set the state.
1073  */
1074  
1075 static int atalk_create(struct socket *sock, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
1076 {
1077         atalk_socket *sk;
1078         sk=(atalk_socket *)kmalloc(sizeof(*sk),GFP_KERNEL);
1079         if(sk==NULL)
1080                 return(-ENOMEM);
1081         switch(sock->type)
1082         {
1083                 /* This RAW is an extension. It is trivial to do and gives you
1084                    the full ELAP frame. Should be handy for CAP 8) */
1085                 case SOCK_RAW:
1086                 /* We permit DDP datagram sockets */
1087                 case SOCK_DGRAM:
1088                         break;
1089                 default:
1090                         kfree_s((void *)sk,sizeof(*sk));
1091                         return(-ESOCKTNOSUPPORT);
1092         }
1093         sk->dead=0;
1094         sk->next=NULL;
1095         sk->broadcast=0;
1096         sk->no_check=0;         /* Checksums on by default */
1097         sk->allocation=GFP_KERNEL;
1098         sk->rcvbuf=SK_RMEM_MAX;
1099         sk->sndbuf=SK_WMEM_MAX;
1100         sk->pair=NULL;
1101         sk->wmem_alloc=0;
1102         sk->rmem_alloc=0;
1103         sk->users=0;
1104         sk->proc=0;
1105         sk->priority=1;
1106         sk->shutdown=0;
1107         sk->prot=NULL;  /* So we use default free mechanisms */
1108         sk->broadcast=0;
1109         sk->err=0;
1110         skb_queue_head_init(&sk->receive_queue);
1111         skb_queue_head_init(&sk->write_queue);
1112         sk->send_head=NULL;
1113         skb_queue_head_init(&sk->back_log);
1114         sk->state=TCP_CLOSE;
1115         sk->socket=sock;
1116         sk->type=sock->type;
1117         sk->debug=0;
1118         
1119         sk->protinfo.af_at.src_net=0;
1120         sk->protinfo.af_at.src_node=0;
1121         sk->protinfo.af_at.src_port=0;
1122         
1123         sk->protinfo.af_at.dest_net=0;
1124         sk->protinfo.af_at.dest_node=0;
1125         sk->protinfo.af_at.dest_port=0;
1126 
1127         sk->mtu=DDP_MAXSZ;
1128         
1129         if(sock!=NULL)
1130         {
1131                 sock->data=(void *)sk;
1132                 sk->sleep=sock->wait;
1133         }
1134         
1135         sk->state_change=def_callback1;
1136         sk->data_ready=def_callback2;
1137         sk->write_space=def_callback1;
1138         sk->error_report=def_callback1;
1139 
1140         sk->zapped=1;
1141         return(0);
1142 }
1143 
1144 /*
1145  *      Copy a socket. No work needed.
1146  */
1147  
1148 static int atalk_dup(struct socket *newsock,struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
1149 {
1150         return(atalk_create(newsock,SOCK_DGRAM));
1151 }
1152 
1153 /*
1154  *      Free a socket. No work needed
1155  */
1156  
1157 static int atalk_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
1158 {
1159         atalk_socket *sk=(atalk_socket *)sock->data;
1160         if(sk==NULL)
1161                 return(0);
1162         if(!sk->dead)
1163                 sk->state_change(sk);
1164         sk->dead=1;
1165         sock->data=NULL;
1166         atalk_destroy_socket(sk);
1167         return(0);
1168 }
1169                 
1170 /*
1171  *      Pick a source address if one is not given. Just return
1172  *      an error if not supportable.
1173  */
1174  
1175 static int atalk_pick_port(struct sockaddr_at *sat)
     /* [previous][next][first][last][top][bottom][index][help] */
1176 {
1177         for ( sat->sat_port = ATPORT_RESERVED; sat->sat_port < ATPORT_LAST; sat->sat_port++ )
1178         {
1179                 if ( atalk_find_socket( sat ) == NULL )
1180                         return sat->sat_port;
1181         }
1182         return -EBUSY;
1183 }
1184                 
1185 static int atalk_autobind(atalk_socket *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1186 {
1187         struct at_addr *ap = atalk_find_primary();
1188         struct sockaddr_at sat;
1189         int n;
1190 
1191         if ( ap == NULL || ap->s_net == htons( ATADDR_ANYNET ))
1192                 return -EADDRNOTAVAIL;
1193         sk->protinfo.af_at.src_net = sat.sat_addr.s_net = ap->s_net;
1194         sk->protinfo.af_at.src_node = sat.sat_addr.s_node = ap->s_node;
1195 
1196         if (( n = atalk_pick_port( &sat )) < 0 )
1197                 return( n );
1198         sk->protinfo.af_at.src_port=n;
1199         atalk_insert_socket(sk);
1200         sk->zapped=0;
1201         return 0;
1202 }
1203 
1204 /*
1205  *      Set the address 'our end' of the connection.
1206  */
1207  
1208 static int atalk_bind(struct socket *sock, struct sockaddr *uaddr,int addr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
1209 {
1210         atalk_socket *sk;
1211         struct sockaddr_at *addr=(struct sockaddr_at *)uaddr;
1212         
1213         sk=(atalk_socket *)sock->data;
1214         
1215         if(sk->zapped==0)
1216                 return(-EIO);
1217                 
1218         if(addr_len!=sizeof(struct sockaddr_at))
1219                 return -EINVAL;
1220 
1221         if(addr->sat_family!=AF_APPLETALK)
1222                 return -EAFNOSUPPORT;
1223 
1224         if(addr->sat_addr.s_net==htons(ATADDR_ANYNET))
1225         {
1226                 struct at_addr *ap=atalk_find_primary();
1227                 if(ap==NULL)
1228                         return -EADDRNOTAVAIL;
1229                 sk->protinfo.af_at.src_net=addr->sat_addr.s_net=ap->s_net;
1230                 sk->protinfo.af_at.src_node=addr->sat_addr.s_node=ap->s_node;
1231         }
1232         else
1233         {                       
1234                 if ( atalk_find_interface( addr->sat_addr.s_net, addr->sat_addr.s_node ) == NULL )
1235                         return -EADDRNOTAVAIL;
1236                 sk->protinfo.af_at.src_net=addr->sat_addr.s_net;
1237                 sk->protinfo.af_at.src_node=addr->sat_addr.s_node;
1238         }
1239 
1240         if(addr->sat_port == ATADDR_ANYPORT)
1241         {
1242                 int n = atalk_pick_port(addr);
1243                 if(n < 0)
1244                         return n;
1245                 sk->protinfo.af_at.src_port=addr->sat_port=n;
1246         }
1247         else
1248                 sk->protinfo.af_at.src_port=addr->sat_port;
1249 
1250         if(atalk_find_socket(addr)!=NULL)
1251                 return -EADDRINUSE;        
1252 
1253         atalk_insert_socket(sk);
1254         sk->zapped=0;
1255         return(0);
1256 }
1257 
1258 /*
1259  *      Set the address we talk to.
1260  */
1261  
1262 static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1263         int addr_len, int flags)
1264 {
1265         atalk_socket *sk=(atalk_socket *)sock->data;
1266         struct sockaddr_at *addr;
1267         
1268         sk->state = TCP_CLOSE;  
1269         sock->state = SS_UNCONNECTED;
1270 
1271         if(addr_len!=sizeof(*addr))
1272                 return(-EINVAL);
1273         addr=(struct sockaddr_at *)uaddr;
1274         
1275         if(addr->sat_family!=AF_APPLETALK)
1276                 return -EAFNOSUPPORT;
1277 #if 0   /* Netatalk doesnt check this */
1278         if(addr->sat_addr.s_node==ATADDR_BCAST && !sk->broadcast)
1279                 return -EPERM;
1280 #endif          
1281         if(sk->zapped)
1282         {
1283                 if(atalk_autobind(sk)<0)
1284                         return -EBUSY;
1285         }       
1286         
1287         if(atrtr_get_dev(&addr->sat_addr)==NULL)
1288                 return -ENETUNREACH;
1289                 
1290         sk->protinfo.af_at.dest_port=addr->sat_port;
1291         sk->protinfo.af_at.dest_net=addr->sat_addr.s_net;
1292         sk->protinfo.af_at.dest_node=addr->sat_addr.s_node;
1293         sock->state = SS_CONNECTED;
1294         sk->state=TCP_ESTABLISHED;
1295         return(0);
1296 }
1297 
1298 /*
1299  *      Not relevant
1300  */
1301  
1302 static int atalk_socketpair(struct socket *sock1, struct socket *sock2)
     /* [previous][next][first][last][top][bottom][index][help] */
1303 {
1304         return(-EOPNOTSUPP);
1305 }
1306 
1307 /*
1308  *      Not relevant
1309  */
1310  
1311 static int atalk_accept(struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1312 {
1313         if(newsock->data)
1314                 kfree_s(newsock->data,sizeof(atalk_socket));
1315         return -EOPNOTSUPP;
1316 }
1317 
1318 /*
1319  *      Find the name of an appletalk socket. Just copy the right
1320  *      fields into the sockaddr.
1321  */
1322  
1323 static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1324         int *uaddr_len, int peer)
1325 {
1326         struct sockaddr_at sat;
1327         atalk_socket *sk;
1328         
1329         sk=(atalk_socket *)sock->data;
1330         if(sk->zapped)
1331         {
1332                 if(atalk_autobind(sk)<0)
1333                         return -EBUSY;
1334         }       
1335         
1336         *uaddr_len = sizeof(struct sockaddr_at);
1337                 
1338         if(peer)
1339         {
1340                 if(sk->state!=TCP_ESTABLISHED)
1341                         return -ENOTCONN;
1342                 sat.sat_addr.s_net=sk->protinfo.af_at.dest_net;
1343                 sat.sat_addr.s_node=sk->protinfo.af_at.dest_node;
1344                 sat.sat_port=sk->protinfo.af_at.dest_port;
1345         }
1346         else
1347         {
1348                 sat.sat_addr.s_net=sk->protinfo.af_at.src_net;
1349                 sat.sat_addr.s_node=sk->protinfo.af_at.src_node;
1350                 sat.sat_port=sk->protinfo.af_at.src_port;
1351         }
1352         sat.sat_family = AF_APPLETALK;
1353         memcpy(uaddr,&sat,sizeof(sat));
1354         return(0);
1355 }
1356 
1357 /*
1358  *      Receive a packet (in skb) from device dev. This has come from the SNAP decoder, and on entry
1359  *      skb->h.raw is the DDP header, skb->len is the DDP length. The physical headers have been 
1360  *      extracted.
1361  */
1362  
1363 int atalk_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
1364 {
1365         atalk_socket *sock;
1366         struct ddpehdr *ddp=(void *)skb->h.raw;
1367         struct atalk_iface *atif;
1368         struct sockaddr_at tosat;
1369         int origlen;
1370         
1371         /* Size check */
1372         if(skb->len<sizeof(*ddp))
1373         {
1374                 kfree_skb(skb,FREE_READ);
1375                 return(0);
1376         }
1377         
1378         
1379         /*
1380          *      Fix up the length field [Ok this is horrible but otherwise
1381          *      I end up with unions of bit fields and messy bit field order
1382          *      compiler/endian dependancies..]
1383          */
1384 
1385         *((__u16 *)ddp)=ntohs(*((__u16 *)ddp));
1386 
1387         /*
1388          *      Trim buffer in case of stray trailing data
1389          */
1390            
1391         origlen = skb->len;
1392         
1393         skb_trim(skb,min(skb->len,ddp->deh_len));
1394 
1395         /*
1396          *      Size check to see if ddp->deh_len was crap
1397          *      (Otherwise we'll detonate most spectacularly
1398          *       in the middle of recvmsg()).
1399          */
1400          
1401         if(skb->len<sizeof(*ddp))
1402         {
1403                 kfree_skb(skb,FREE_READ);
1404                 return(0);
1405         }
1406 
1407         /*
1408          *      Any checksums. Note we don't do htons() on this == is assumed to be
1409          *      valid for net byte orders all over the networking code... 
1410          */
1411 
1412         if(ddp->deh_sum && atalk_checksum(ddp, ddp->deh_len)!= ddp->deh_sum)
1413         {
1414                 /* Not a valid appletalk frame - dustbin time */
1415                 kfree_skb(skb,FREE_READ);
1416                 return(0);
1417         }
1418 
1419 #ifdef CONFIG_FIREWALL
1420         
1421         if(call_in_firewall(AF_APPLETALK, skb, ddp)!=FW_ACCEPT)
1422         {
1423                 kfree_skb(skb, FREE_READ);
1424                 return 0;
1425         }
1426         
1427 #endif  
1428         
1429         /* Check the packet is aimed at us */
1430 
1431         if(ddp->deh_dnet == 0)  /* Net 0 is 'this network' */
1432                 atif=atalk_find_anynet(ddp->deh_dnode, dev);
1433         else
1434                 atif=atalk_find_interface(ddp->deh_dnet,ddp->deh_dnode);
1435 
1436         /* Not ours */
1437         if(atif==NULL)          
1438         {
1439                 struct atalk_route *rt;
1440                 struct at_addr ta;
1441 
1442                 /* Don't route multicast, etc., packets, or packets
1443                    sent to "this network" */
1444                 if (skb->pkt_type != PACKET_HOST || ddp->deh_dnet == 0) 
1445                 {
1446                         kfree_skb(skb, FREE_READ);
1447                         return(0);
1448                 }
1449                 
1450 #ifdef CONFIG_FIREWALL          
1451                 /*
1452                  *      Check firewall allows this routing
1453                  */
1454                 
1455                 if(call_fw_firewall(AF_APPLETALK, skb, ddp)!=FW_ACCEPT)
1456                 {
1457                         kfree_skb(skb, FREE_READ);
1458                         return(0);
1459                 }
1460 #endif
1461                 ta.s_net=ddp->deh_dnet;
1462                 ta.s_node=ddp->deh_dnode;
1463 
1464                 /* Route the packet */
1465                 rt=atrtr_find(&ta);
1466                 if(rt==NULL || ddp->deh_hops==15)
1467                 {
1468                         kfree_skb(skb, FREE_READ);
1469                         return(0);
1470                 }
1471                 ddp->deh_hops++;
1472 
1473                 /* Fix up skb->len field */
1474                 skb_trim(skb,min(origlen, rt->dev->hard_header_len + 
1475                         ddp_dl->header_length + ddp->deh_len));
1476 
1477                 *((__u16 *)ddp)=ntohs(*((__u16 *)ddp));         /* Mend the byte order */
1478                 /*
1479                  *      Send the buffer onwards
1480                  */
1481                 if(aarp_send_ddp(rt->dev, skb, &ta, NULL)==-1)
1482                         kfree_skb(skb, FREE_READ);
1483                 return 0;
1484         }
1485 
1486         /* Which socket - atalk_search_socket() looks for a *full match*
1487            of the <net,node,port> tuple */
1488         tosat.sat_addr.s_net = ddp->deh_dnet;
1489         tosat.sat_addr.s_node = ddp->deh_dnode;
1490         tosat.sat_port = ddp->deh_dport;
1491 
1492         sock=atalk_search_socket( &tosat, atif );
1493         
1494         if(sock==NULL)  /* But not one of our sockets */
1495         {
1496                 kfree_skb(skb,FREE_READ);
1497                 return(0);
1498         }
1499 
1500         
1501         /*
1502          *      Queue packet (standard)
1503          */
1504          
1505         skb->sk = sock;
1506 
1507         if(sock_queue_rcv_skb(sock,skb)<0)
1508         {
1509                 skb->sk=NULL;
1510                 kfree_skb(skb, FREE_WRITE);
1511         }
1512         return(0);
1513 }
1514 
1515 static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, int len, int nonblock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1516 {
1517         atalk_socket *sk=(atalk_socket *)sock->data;
1518         struct sockaddr_at *usat=(struct sockaddr_at *)msg->msg_name;
1519         struct sockaddr_at local_satalk, gsat;
1520         struct sk_buff *skb;
1521         struct device *dev;
1522         struct ddpehdr *ddp;
1523         int size;
1524         struct atalk_route *rt;
1525         int loopback=0;
1526         int err;
1527         
1528         if(flags)
1529                 return -EINVAL;
1530                 
1531         if(len>587)
1532                 return -EMSGSIZE;
1533                 
1534         if(usat)
1535         {
1536                 if(sk->zapped)
1537                 /* put the autobinding in */
1538                 {
1539                         if(atalk_autobind(sk)<0)
1540                                 return -EBUSY;
1541                 }
1542 
1543                 if(msg->msg_namelen <sizeof(*usat))
1544                         return(-EINVAL);
1545                 if(usat->sat_family != AF_APPLETALK)
1546                         return -EINVAL;
1547 #if 0   /* netatalk doesnt implement this check */
1548                 if(usat->sat_addr.s_node==ATADDR_BCAST && !sk->broadcast)
1549                         return -EPERM;
1550 #endif                  
1551         }
1552         else
1553         {
1554                 if(sk->state!=TCP_ESTABLISHED)
1555                         return -ENOTCONN;
1556                 usat=&local_satalk;
1557                 usat->sat_family=AF_APPLETALK;
1558                 usat->sat_port=sk->protinfo.af_at.dest_port;
1559                 usat->sat_addr.s_node=sk->protinfo.af_at.dest_node;
1560                 usat->sat_addr.s_net=sk->protinfo.af_at.dest_net;
1561         }
1562         
1563         /* Build a packet */
1564         
1565         if(sk->debug)
1566                 printk("SK %p: Got address.\n",sk);
1567         
1568         size=sizeof(struct ddpehdr)+len+ddp_dl->header_length;  /* For headers */
1569 
1570         if(usat->sat_addr.s_net!=0 || usat->sat_addr.s_node == ATADDR_ANYNODE)
1571         {
1572                 rt=atrtr_find(&usat->sat_addr);
1573                 if(rt==NULL)
1574                         return -ENETUNREACH;    
1575                 dev=rt->dev;
1576         }
1577         else
1578         {
1579                 struct at_addr at_hint;
1580                 at_hint.s_node=0;
1581                 at_hint.s_net=sk->protinfo.af_at.src_net;
1582                 rt=atrtr_find(&at_hint);
1583                 if(rt==NULL)
1584                         return -ENETUNREACH;
1585                 dev=rt->dev;
1586         }
1587 
1588         if(sk->debug)
1589                 printk("SK %p: Size needed %d, device %s\n", sk, size, dev->name);
1590         
1591         size += dev->hard_header_len;
1592 
1593         skb = sock_alloc_send_skb(sk, size, 0, 0 , &err);
1594         if(skb==NULL)
1595                 return err;
1596 
1597         skb->sk=sk;
1598         skb->free=1;
1599         skb->arp=1;
1600         skb_reserve(skb,ddp_dl->header_length);
1601         skb_reserve(skb,dev->hard_header_len);
1602 
1603         skb->dev=dev;
1604         
1605         if(sk->debug)
1606                 printk("SK %p: Begin build.\n", sk);
1607         
1608         ddp=(struct ddpehdr *)skb_put(skb,sizeof(struct ddpehdr));
1609         ddp->deh_pad=0;
1610         ddp->deh_hops=0;
1611         ddp->deh_len=len+sizeof(*ddp);
1612         /*
1613          *      Fix up the length field [Ok this is horrible but otherwise
1614          *      I end up with unions of bit fields and messy bit field order
1615          *      compiler/endian dependancies..
1616          */
1617         *((__u16 *)ddp)=ntohs(*((__u16 *)ddp));
1618 
1619         ddp->deh_dnet=usat->sat_addr.s_net;
1620         ddp->deh_snet=sk->protinfo.af_at.src_net;
1621         ddp->deh_dnode=usat->sat_addr.s_node;
1622         ddp->deh_snode=sk->protinfo.af_at.src_node;
1623         ddp->deh_dport=usat->sat_port;
1624         ddp->deh_sport=sk->protinfo.af_at.src_port;
1625 
1626         if(sk->debug)
1627                 printk("SK %p: Copy user data (%d bytes).\n", sk, len);
1628                 
1629         memcpy_fromiovec(skb_put(skb,len),msg->msg_iov,len);
1630 
1631         if(sk->no_check==1)
1632                 ddp->deh_sum=0;
1633         else
1634                 ddp->deh_sum=atalk_checksum(ddp, len+sizeof(*ddp));
1635                 
1636 #ifdef CONFIG_FIREWALL
1637 
1638         if(call_out_firewall(AF_APPLETALK, skb, ddp)!=FW_ACCEPT)
1639         {
1640                 kfree_skb(skb, FREE_WRITE);
1641                 return -EPERM;
1642         }       
1643         
1644 #endif
1645         
1646         /*
1647          *      Loopback broadcast packets to non gateway targets (ie routes
1648          *      to group we are in)
1649          */
1650          
1651         if(ddp->deh_dnode==ATADDR_BCAST)
1652         {
1653                 if((!(rt->flags&RTF_GATEWAY))&&(!(dev->flags&IFF_LOOPBACK)))
1654                 {
1655                         struct sk_buff *skb2=skb_clone(skb, GFP_KERNEL);
1656                         if(skb2)
1657                         {
1658                                 loopback=1;
1659                                 if(sk->debug)
1660                                         printk("SK %p: send out(copy).\n", sk);
1661                                 if(aarp_send_ddp(dev,skb2,&usat->sat_addr, NULL)==-1)
1662                                         kfree_skb(skb2, FREE_WRITE);
1663                                 /* else queued/sent above in the aarp queue */
1664                         }
1665                 }
1666         }
1667 
1668         if((dev->flags&IFF_LOOPBACK) || loopback) 
1669         {
1670                 if(sk->debug)
1671                         printk("SK %p: Loop back.\n", sk);
1672                 /* loop back */
1673                 sk->wmem_alloc-=skb->truesize;
1674                 ddp_dl->datalink_header(ddp_dl, skb, dev->dev_addr);
1675                 skb->sk = NULL;
1676                 skb->mac.raw=skb->data;
1677                 skb->h.raw = skb->data + ddp_dl->header_length + dev->hard_header_len;
1678                 skb_pull(skb,dev->hard_header_len);
1679                 skb_pull(skb,ddp_dl->header_length);
1680                 atalk_rcv(skb,dev,NULL);
1681         }
1682         else 
1683         {
1684                 if(sk->debug)
1685                         printk("SK %p: send out.\n", sk);
1686 
1687                 if ( rt->flags & RTF_GATEWAY ) {
1688                     gsat.sat_addr = rt->gateway;
1689                     usat = &gsat;
1690                 }
1691         
1692                 if(aarp_send_ddp(dev,skb,&usat->sat_addr, NULL)==-1)
1693                         kfree_skb(skb, FREE_WRITE);
1694                 /* else queued/sent above in the aarp queue */
1695         }
1696         if(sk->debug)
1697                 printk("SK %p: Done write (%d).\n", sk, len);
1698         return len;
1699 }
1700 
1701 
1702 static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, int size, int noblock, int flags, int *addr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
1703 {
1704         atalk_socket *sk=(atalk_socket *)sock->data;
1705         struct sockaddr_at *sat=(struct sockaddr_at *)msg->msg_name;
1706         struct ddpehdr  *ddp = NULL;
1707         int copied = 0;
1708         struct sk_buff *skb;
1709         int er;
1710         
1711         if(sk->err)
1712                 return sock_error(sk);
1713         
1714         if(addr_len)
1715                 *addr_len=sizeof(*sat);
1716 
1717         skb=skb_recv_datagram(sk,flags,noblock,&er);
1718         if(skb==NULL)
1719                 return er;
1720 
1721         ddp = (struct ddpehdr *)(skb->h.raw);
1722         if(sk->type==SOCK_RAW)
1723         {
1724                 copied=ddp->deh_len;
1725                 if(copied > size)
1726                         copied=size;
1727                 skb_copy_datagram_iovec(skb,0,msg->msg_iov,copied);
1728         }
1729         else
1730         {
1731                 copied=ddp->deh_len - sizeof(*ddp);
1732                 if (copied > size)
1733                         copied = size;
1734                 skb_copy_datagram_iovec(skb,sizeof(*ddp),msg->msg_iov,copied);
1735         }
1736         if(sat)
1737         {
1738                 sat->sat_family=AF_APPLETALK;
1739                 sat->sat_port=ddp->deh_sport;
1740                 sat->sat_addr.s_node=ddp->deh_snode;
1741                 sat->sat_addr.s_net=ddp->deh_snet;
1742         }
1743         skb_free_datagram(sk, skb);
1744         return(copied);
1745 }               
1746 
1747 
1748 static int atalk_shutdown(struct socket *sk,int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1749 {
1750         return -EOPNOTSUPP;
1751 }
1752 
1753 static int atalk_select(struct socket *sock , int sel_type, select_table *wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1754 {
1755         atalk_socket *sk=(atalk_socket *)sock->data;
1756         
1757         return datagram_select(sk,sel_type,wait);
1758 }
1759 
1760 /*
1761  *      Appletalk ioctl calls.
1762  */
1763 
1764 static int atalk_ioctl(struct socket *sock,unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1765 {
1766         int err;
1767         long amount=0;
1768         atalk_socket *sk=(atalk_socket *)sock->data;
1769         
1770         switch(cmd)
1771         {
1772                 /*
1773                  *      Protocol layer
1774                  */
1775                 case TIOCOUTQ:
1776                         amount=sk->sndbuf-sk->wmem_alloc;
1777                         if(amount<0)
1778                                 amount=0;
1779                         break;
1780                 case TIOCINQ:
1781                 {
1782                         struct sk_buff *skb;
1783                         /* These two are safe on a single CPU system as only user tasks fiddle here */
1784                         if((skb=skb_peek(&sk->receive_queue))!=NULL)
1785                                 amount=skb->len-sizeof(struct ddpehdr);
1786                         break;
1787                 }
1788                 case SIOCGSTAMP:
1789                         if (sk)
1790                         {
1791                                 if(sk->stamp.tv_sec==0)
1792                                         return -ENOENT;
1793                                 err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval));
1794                                 if(err)
1795                                         return err;
1796                                         memcpy_tofs((void *)arg,&sk->stamp,sizeof(struct timeval));
1797                                 return 0;
1798                         }
1799                         return -EINVAL;
1800                 /*
1801                  *      Routing
1802                  */
1803                 case SIOCADDRT:
1804                 case SIOCDELRT:
1805                         if(!suser())
1806                                 return -EPERM;
1807                         return(atrtr_ioctl(cmd,(void *)arg));
1808                 /*
1809                  *      Interface
1810                  */                     
1811                 case SIOCGIFADDR:
1812                 case SIOCSIFADDR:
1813                 case SIOCGIFBRDADDR:
1814                         return atif_ioctl(cmd,(void *)arg);
1815                 /*
1816                  *      Physical layer ioctl calls
1817                  */
1818                 case SIOCSIFLINK:
1819                 case SIOCGIFHWADDR:
1820                 case SIOCSIFHWADDR:
1821                 case SIOCGIFFLAGS:
1822                 case SIOCSIFFLAGS:
1823                 case SIOCGIFMTU:
1824                 case SIOCGIFCONF:
1825                 case SIOCADDMULTI:
1826                 case SIOCDELMULTI:
1827 
1828                         return(dev_ioctl(cmd,(void *) arg));
1829 
1830                 case SIOCSIFMETRIC:
1831                 case SIOCSIFBRDADDR:
1832                 case SIOCGIFNETMASK:
1833                 case SIOCSIFNETMASK:
1834                 case SIOCGIFMEM:
1835                 case SIOCSIFMEM:
1836                 case SIOCGIFDSTADDR:
1837                 case SIOCSIFDSTADDR:
1838                         return -EINVAL;
1839 
1840                 default:
1841                         return -EINVAL;
1842         }
1843         err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
1844         if(err)
1845                 return err;
1846         put_fs_long(amount,(unsigned long *)arg);
1847         return(0);
1848 }
1849 
1850 static struct proto_ops atalk_proto_ops = {
1851         AF_APPLETALK,
1852         
1853         atalk_create,
1854         atalk_dup,
1855         atalk_release,
1856         atalk_bind,
1857         atalk_connect,
1858         atalk_socketpair,
1859         atalk_accept,
1860         atalk_getname,
1861         atalk_select,
1862         atalk_ioctl,
1863         atalk_listen,
1864         atalk_shutdown,
1865         atalk_setsockopt,
1866         atalk_getsockopt,
1867         atalk_fcntl,
1868         atalk_sendmsg,
1869         atalk_recvmsg
1870 };
1871 
1872 static struct notifier_block ddp_notifier={
1873         ddp_device_event,
1874         NULL,
1875         0
1876 };
1877 
1878 /* Called by proto.c on kernel start up */
1879 
1880 void atalk_proto_init(struct net_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
1881 {
1882         static char ddp_snap_id[]={0x08,0x00,0x07,0x80,0x9B};
1883         (void) sock_register(atalk_proto_ops.family, &atalk_proto_ops);
1884         if ((ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv)) == NULL)
1885                 printk("Unable to register DDP with SNAP.\n");
1886         register_netdevice_notifier(&ddp_notifier);
1887         aarp_proto_init();
1888 
1889         proc_net_register(&(struct proc_dir_entry) {
1890                 PROC_NET_ATALK, 9, "appletalk",
1891                 S_IFREG | S_IRUGO, 1, 0, 0,
1892                 0, &proc_net_inode_operations,
1893                 atalk_get_info
1894         });
1895         proc_net_register(&(struct proc_dir_entry) {
1896                 PROC_NET_AT_ROUTE, 11,"atalk_route",
1897                 S_IFREG | S_IRUGO, 1, 0, 0,
1898                 0, &proc_net_inode_operations,
1899                 atalk_rt_get_info
1900         });
1901         proc_net_register(&(struct proc_dir_entry) {
1902                 PROC_NET_ATIF, 11,"atalk_iface",
1903                 S_IFREG | S_IRUGO, 1, 0, 0,
1904                 0, &proc_net_inode_operations,
1905                 atalk_if_get_info
1906         });
1907 
1908         printk("Appletalk 0.14 for Linux NET3.032\n");
1909 }
1910 #endif

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