OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [net/] [appletalk/] [ddp.c] - Blame information for rev 1772

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.