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

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