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

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