make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / net / atm / br2684.c
1 /*
2 Experimental ethernet netdevice using ATM AAL5 as underlying carrier
3 (RFC1483 obsoleted by RFC2684) for Linux 2.4
4 Author: Marcell GAL, 2000, XDSL Ltd, Hungary
5 */
6
7 #include <linux/module.h>
8 #include <linux/config.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/netdevice.h>
13 #include <linux/skbuff.h>
14 #include <linux/etherdevice.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/ip.h>
17 #include <asm/uaccess.h>
18 #include <net/arp.h>
19
20 #include <linux/atmbr2684.h>
21
22 #include "ipcommon.h"
23
24 /*
25  * Define this to use a version of the code which interacts with the higher
26  * layers in a more intellegent way, by always reserving enough space for
27  * our header at the begining of the packet.  However, there may still be
28  * some problems with programs like tcpdump.  In 2.5 we'll sort out what
29  * we need to do to get this perfect.  For now we just will copy the packet
30  * if we need space for the header
31  */
32 /* #define FASTER_VERSION */
33
34 #ifdef DEBUG
35 #define DPRINTK(format, args...) printk(KERN_DEBUG "br2684: " format, ##args)
36 #else
37 #define DPRINTK(format, args...)
38 #endif
39
40 #ifdef SKB_DEBUG
41 static void skb_debug(const struct sk_buff *skb)
42 {
43 #define NUM2PRINT 50
44         char buf[NUM2PRINT * 3 + 1];    /* 3 chars per byte */
45         int i = 0;
46         for (i = 0; i < skb->len && i < NUM2PRINT; i++) {
47                 sprintf(buf + i * 3, "%2.2x ", 0xff & skb->data[i]);
48         }
49         printk(KERN_DEBUG "br2684: skb: %s\n", buf);
50 }
51 #else
52 #define skb_debug(skb)  do {} while (0)
53 #endif
54
55 static unsigned char llc_oui_pid_pad[] =
56     { 0xAA, 0xAA, 0x03, 0x00, 0x80, 0xC2, 0x00, 0x07, 0x00, 0x00 };
57 #define PADLEN  (2)
58
59 enum br2684_encaps {
60         e_vc  = BR2684_ENCAPS_VC,
61         e_llc = BR2684_ENCAPS_LLC,
62 };
63
64 struct br2684_vcc {
65         struct atm_vcc  *atmvcc;
66         struct br2684_dev *brdev;
67         /* keep old push,pop functions for chaining */
68         void (*old_push)(struct atm_vcc *vcc,struct sk_buff *skb);
69         /* void (*old_pop)(struct atm_vcc *vcc,struct sk_buff *skb); */
70         enum br2684_encaps encaps;
71         struct list_head brvccs;
72 #ifdef CONFIG_ATM_BR2684_IPFILTER
73         struct br2684_filter filter;
74 #endif /* CONFIG_ATM_BR2684_IPFILTER */
75 #ifndef FASTER_VERSION
76         unsigned copies_needed, copies_failed;
77 #endif /* FASTER_VERSION */
78 };
79
80 struct br2684_dev {
81         struct net_device net_dev;
82         struct list_head br2684_devs;
83         int number;
84         struct list_head brvccs; /* one device <=> one vcc (before xmas) */
85         struct net_device_stats stats;
86         int mac_was_set;
87 };
88
89 /*
90  * This lock should be held for writing any time the list of devices or
91  * their attached vcc's could be altered.  It should be held for reading
92  * any time these are being queried.  Note that we sometimes need to
93  * do read-locking under interrupt context, so write locking must block
94  * the current CPU's interrupts
95  */
96 static rwlock_t devs_lock = RW_LOCK_UNLOCKED;
97
98 static LIST_HEAD(br2684_devs);
99
100 static inline struct br2684_dev *BRPRIV(const struct net_device *net_dev)
101 {
102         return (struct br2684_dev *) ((char *) (net_dev) -
103             (unsigned long) (&((struct br2684_dev *) 0)->net_dev));
104 }
105
106 static inline struct br2684_dev *list_entry_brdev(const struct list_head *le)
107 {
108         return list_entry(le, struct br2684_dev, br2684_devs);
109 }
110
111 static inline struct br2684_vcc *BR2684_VCC(const struct atm_vcc *atmvcc)
112 {
113         return (struct br2684_vcc *) (atmvcc->user_back);
114 }
115
116 static inline struct br2684_vcc *list_entry_brvcc(const struct list_head *le)
117 {
118         return list_entry(le, struct br2684_vcc, brvccs);
119 }
120
121 /* Caller should hold read_lock(&devs_lock) */
122 static struct br2684_dev *br2684_find_dev(const struct br2684_if_spec *s)
123 {
124         struct list_head *lh;
125         struct br2684_dev *brdev;
126         switch (s->method) {
127         case BR2684_FIND_BYNUM:
128                 list_for_each(lh, &br2684_devs) {
129                         brdev = list_entry_brdev(lh);
130                         if (brdev->number == s->spec.devnum)
131                                 return brdev;
132                 }
133                 break;
134         case BR2684_FIND_BYIFNAME:
135                 list_for_each(lh, &br2684_devs) {
136                         brdev = list_entry_brdev(lh);
137                         if (!strncmp(brdev->net_dev.name, s->spec.ifname,
138                             sizeof brdev->net_dev.name))
139                                 return brdev;
140                 }
141                 break;
142         }
143         return NULL;
144 }
145
146 /*
147  * Send a packet out a particular vcc.  Not to useful right now, but paves
148  * the way for multiple vcc's per itf.  Returns true if we can send,
149  * otherwise false
150  */
151 static int br2684_xmit_vcc(struct sk_buff *skb, struct br2684_dev *brdev,
152         struct br2684_vcc *brvcc)
153 {
154         struct atm_vcc *atmvcc;
155 #ifdef FASTER_VERSION
156         if (brvcc->encaps == e_llc)
157                 memcpy(skb_push(skb, 8), llc_oui_pid_pad, 8);
158         /* last 2 bytes of llc_oui_pid_pad are managed by header routines;
159            yes, you got it: 8 + 2 = sizeof(llc_oui_pid_pad)
160          */
161 #else
162         int minheadroom = (brvcc->encaps == e_llc) ? 10 : 2;
163         if (skb_headroom(skb) < minheadroom) {
164                 struct sk_buff *skb2 = skb_realloc_headroom(skb, minheadroom);
165                 brvcc->copies_needed++;
166                 dev_kfree_skb(skb);
167                 if (skb2 == NULL) {
168                         brvcc->copies_failed++;
169                         return 0;
170                 }
171                 skb = skb2;
172         }
173         skb_push(skb, minheadroom);
174         if (brvcc->encaps == e_llc)
175                 memcpy(skb->data, llc_oui_pid_pad, 10);
176         else
177                 memset(skb->data, 0, 2);
178 #endif /* FASTER_VERSION */
179         skb_debug(skb);
180
181         ATM_SKB(skb)->vcc = atmvcc = brvcc->atmvcc;
182         DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, atmvcc, atmvcc->dev);
183         if (!atm_may_send(atmvcc, skb->truesize)) {
184                 /* we free this here for now, because we cannot know in a higher 
185                         layer whether the skb point it supplied wasn't freed yet.
186                         now, it always is.
187                 */
188                 dev_kfree_skb(skb);
189                 return 0;
190                 }
191         atomic_add(skb->truesize, &atmvcc->tx_inuse);
192         ATM_SKB(skb)->iovcnt = 0;
193         ATM_SKB(skb)->atm_options = atmvcc->atm_options;
194         brdev->stats.tx_packets++;
195         brdev->stats.tx_bytes += skb->len;
196         atmvcc->send(atmvcc, skb);
197         return 1;
198 }
199
200 static inline struct br2684_vcc *pick_outgoing_vcc(struct sk_buff *skb,
201         struct br2684_dev *brdev)
202 {
203         return list_empty(&brdev->brvccs) ? NULL :
204             list_entry_brvcc(brdev->brvccs.next); /* 1 vcc/dev right now */
205 }
206
207 static int br2684_start_xmit(struct sk_buff *skb, struct net_device *dev)
208 {
209         struct br2684_dev *brdev = BRPRIV(dev);
210         struct br2684_vcc *brvcc;
211
212         DPRINTK("br2684_start_xmit, skb->dst=%p\n", skb->dst);
213         read_lock(&devs_lock);
214         brvcc = pick_outgoing_vcc(skb, brdev);
215         if (brvcc == NULL) {
216                 DPRINTK("no vcc attached to dev %s\n", dev->name);
217                 brdev->stats.tx_errors++;
218                 brdev->stats.tx_carrier_errors++;
219                 /* netif_stop_queue(dev); */
220                 dev_kfree_skb(skb);
221                 read_unlock(&devs_lock);
222                 return -EUNATCH;
223         }
224         if (!br2684_xmit_vcc(skb, brdev, brvcc)) {
225                 /*
226                  * We should probably use netif_*_queue() here, but that
227                  * involves added complication.  We need to walk before
228                  * we can run
229                  */
230                 /* don't free here! this pointer might be no longer valid!
231                 dev_kfree_skb(skb);
232                 */
233                 brdev->stats.tx_errors++;
234                 brdev->stats.tx_fifo_errors++;
235         }
236         read_unlock(&devs_lock);
237         return 0;
238 }
239
240 static struct net_device_stats *br2684_get_stats(struct net_device *dev)
241 {
242         DPRINTK("br2684_get_stats\n");
243         return &BRPRIV(dev)->stats;
244 }
245
246 #ifdef FASTER_VERSION
247 /*
248  * These mirror eth_header and eth_header_cache.  They are not usually
249  * exported for use in modules, so we grab them from net_device
250  * after ether_setup() is done with it.  Bit of a hack.
251  */
252 static int (*my_eth_header)(struct sk_buff *, struct net_device *,
253         unsigned short, void *, void *, unsigned);
254 static int (*my_eth_header_cache)(struct neighbour *, struct hh_cache *);
255
256 static int
257 br2684_header(struct sk_buff *skb, struct net_device *dev,
258               unsigned short type, void *daddr, void *saddr, unsigned len)
259 {
260         u16 *pad_before_eth;
261         int t = my_eth_header(skb, dev, type, daddr, saddr, len);
262         if (t > 0) {
263                 pad_before_eth = (u16 *) skb_push(skb, 2);
264                 *pad_before_eth = 0;
265                 return dev->hard_header_len;    /* or return 16; ? */
266         } else
267                 return t;
268 }
269
270 static int
271 br2684_header_cache(struct neighbour *neigh, struct hh_cache *hh)
272 {
273 /* hh_data is 16 bytes long. if encaps is ether-llc we need 24, so
274 xmit will add the additional header part in that case */
275         u16 *pad_before_eth = (u16 *)(hh->hh_data);
276         int t = my_eth_header_cache(neigh, hh);
277         DPRINTK("br2684_header_cache, neigh=%p, hh_cache=%p\n", neigh, hh);
278         if (t < 0)
279                 return t;
280         else {
281                 *pad_before_eth = 0;
282                 hh->hh_len = PADLEN + ETH_HLEN;
283         }
284         return 0;
285 }
286
287 /*
288  * This is similar to eth_type_trans, which cannot be used because of
289  * our dev->hard_header_len
290  */
291 static inline unsigned short br_type_trans(struct sk_buff *skb,
292                                                struct net_device *dev)
293 {
294         struct ethhdr *eth;
295         unsigned char *rawp;
296         eth = skb->mac.ethernet;
297
298         if (*eth->h_dest & 1) {
299                 if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0)
300                         skb->pkt_type = PACKET_BROADCAST;
301                 else
302                         skb->pkt_type = PACKET_MULTICAST;
303         }
304
305         else if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
306                 skb->pkt_type = PACKET_OTHERHOST;
307
308         if (ntohs(eth->h_proto) >= 1536)
309                 return eth->h_proto;
310
311         rawp = skb->data;
312
313         /*
314          * This is a magic hack to spot IPX packets. Older Novell breaks
315          * the protocol design and runs IPX over 802.3 without an 802.2 LLC
316          * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
317          * won't work for fault tolerant netware but does for the rest.
318          */
319         if (*(unsigned short *) rawp == 0xFFFF)
320                 return htons(ETH_P_802_3);
321
322         /*
323          * Real 802.2 LLC
324          */
325         return htons(ETH_P_802_2);
326 }
327 #endif /* FASTER_VERSION */
328
329 /*
330  * We remember when the MAC gets set, so we don't override it later with
331  * the ESI of the ATM card of the first VC
332  */
333 static int (*my_eth_mac_addr)(struct net_device *, void *);
334 static int br2684_mac_addr(struct net_device *dev, void *p)
335 {
336         int err = my_eth_mac_addr(dev, p);
337         if (!err)
338                 BRPRIV(dev)->mac_was_set = 1;
339         return err;
340 }
341
342 #ifdef CONFIG_ATM_BR2684_IPFILTER
343 /* this IOCTL is experimental. */
344 static int br2684_setfilt(struct atm_vcc *atmvcc, unsigned long arg)
345 {
346         struct br2684_vcc *brvcc;
347         struct br2684_filter_set fs;
348
349         if (copy_from_user(&fs, (void *) arg, sizeof fs))
350                 return -EFAULT;
351         if (fs.ifspec.method != BR2684_FIND_BYNOTHING) {
352                 /*
353                  * This is really a per-vcc thing, but we can also search
354                  * by device
355                  */
356                 struct br2684_dev *brdev;
357                 read_lock(&devs_lock);
358                 brdev = br2684_find_dev(&fs.ifspec);
359                 if (brdev == NULL || list_empty(&brdev->brvccs) ||
360                     brdev->brvccs.next != brdev->brvccs.prev)  /* >1 VCC */
361                         brvcc = NULL;
362                 else
363                         brvcc = list_entry_brvcc(brdev->brvccs.next);
364                 read_unlock(&devs_lock);
365                 if (brvcc == NULL)
366                         return -ESRCH;
367         } else
368                 brvcc = BR2684_VCC(atmvcc);
369         memcpy(&brvcc->filter, &fs.filter, sizeof(brvcc->filter));
370         return 0;
371 }
372
373 /* Returns 1 if packet should be dropped */
374 static inline int
375 packet_fails_filter(u16 type, struct br2684_vcc *brvcc, struct sk_buff *skb)
376 {
377         if (brvcc->filter.netmask == 0)
378                 return 0;                       /* no filter in place */
379         if (type == __constant_htons(ETH_P_IP) &&
380             (((struct iphdr *) (skb->data))->daddr & brvcc->filter.
381              netmask) == brvcc->filter.prefix)
382                 return 0;
383         if (type == __constant_htons(ETH_P_ARP))
384                 return 0;
385         /* TODO: we should probably filter ARPs too.. don't want to have
386          *   them returning values that don't make sense, or is that ok?
387          */
388         return 1;               /* drop */
389 }
390 #endif /* CONFIG_ATM_BR2684_IPFILTER */
391
392 static void br2684_close_vcc(struct br2684_vcc *brvcc)
393 {
394         DPRINTK("removing VCC %p from dev %p\n", brvcc, brvcc->brdev);
395         write_lock_irq(&devs_lock);
396         list_del(&brvcc->brvccs);
397         write_unlock_irq(&devs_lock);
398         brvcc->atmvcc->user_back = NULL;        /* what about vcc->recvq ??? */
399         brvcc->old_push(brvcc->atmvcc, NULL);   /* pass on the bad news */
400         kfree(brvcc);
401         MOD_DEC_USE_COUNT;
402 }
403
404 /* when AAL5 PDU comes in: */
405 static void br2684_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
406 {
407         struct br2684_vcc *brvcc = BR2684_VCC(atmvcc);
408         struct br2684_dev *brdev = brvcc->brdev;
409         int plen = sizeof(llc_oui_pid_pad) + ETH_HLEN;
410
411         DPRINTK("br2684_push\n");
412
413         if (skb == NULL) {      /* skb==NULL means VCC is being destroyed */
414                 br2684_close_vcc(brvcc);
415                 if (list_empty(&brdev->brvccs)) {
416                         read_lock(&devs_lock);
417                         list_del(&brdev->br2684_devs);
418                         read_unlock(&devs_lock);
419                         unregister_netdev(&brdev->net_dev);
420                         kfree(brdev);
421                 }
422                 return;
423         }
424
425         skb_debug(skb);
426         atm_return(atmvcc, skb->truesize);
427         DPRINTK("skb from brdev %p\n", brdev);
428         if (brvcc->encaps == e_llc) {
429                 /* let us waste some time for checking the encapsulation.
430                    Note, that only 7 char is checked so frames with a valid FCS
431                    are also accepted (but FCS is not checked of course) */
432                 if (memcmp(skb->data, llc_oui_pid_pad, 7)) {
433                         brdev->stats.rx_errors++;
434                         dev_kfree_skb(skb);
435                         return;
436                 }
437         } else {
438                 plen = PADLEN + ETH_HLEN;       /* pad, dstmac,srcmac, ethtype */
439                 /* first 2 chars should be 0 */
440                 if (*((u16 *) (skb->data)) != 0) {
441                         brdev->stats.rx_errors++;
442                         dev_kfree_skb(skb);
443                         return;
444                 }
445         }
446         if (skb->len < plen) {
447                 brdev->stats.rx_errors++;
448                 dev_kfree_skb(skb);     /* dev_ not needed? */
449                 return;
450         }
451
452 #ifdef FASTER_VERSION
453         /* FIXME: tcpdump shows that pointer to mac header is 2 bytes earlier,
454            than should be. What else should I set? */
455         skb_pull(skb, plen);
456         skb->mac.raw = ((char *) (skb->data)) - ETH_HLEN;
457         skb->pkt_type = PACKET_HOST;
458 #ifdef CONFIG_BR2684_FAST_TRANS
459         skb->protocol = ((u16 *) skb->data)[-1];
460 #else                           /* some protocols might require this: */
461         skb->protocol = br_type_trans(skb, &brdev->net_dev);
462 #endif /* CONFIG_BR2684_FAST_TRANS */
463 #else
464         skb_pull(skb, plen - ETH_HLEN);
465         skb->protocol = eth_type_trans(skb, &brdev->net_dev);
466 #endif /* FASTER_VERSION */
467 #ifdef CONFIG_ATM_BR2684_IPFILTER
468         if (packet_fails_filter(skb->protocol, brvcc, skb)) {
469                 brdev->stats.rx_dropped++;
470                 dev_kfree_skb(skb);
471                 return;
472         }
473 #endif /* CONFIG_ATM_BR2684_IPFILTER */
474         skb->dev = &brdev->net_dev;
475         ATM_SKB(skb)->vcc = atmvcc;     /* needed ? */
476         DPRINTK("received packet's protocol: %x\n", ntohs(skb->protocol));
477         skb_debug(skb);
478         if (!(brdev->net_dev.flags & IFF_UP)) { /* sigh, interface is down */
479                 brdev->stats.rx_dropped++;
480                 dev_kfree_skb(skb);
481                 return;
482         }
483         brdev->stats.rx_packets++;
484         brdev->stats.rx_bytes += skb->len;
485         netif_rx(skb);
486 }
487
488 static int br2684_regvcc(struct atm_vcc *atmvcc, unsigned long arg)
489 {
490 /* assign a vcc to a dev
491 Note: we do not have explicit unassign, but look at _push()
492 */
493         int err;
494         struct br2684_vcc *brvcc;
495         struct sk_buff_head copy;
496         struct sk_buff *skb;
497         struct br2684_dev *brdev;
498         struct atm_backend_br2684 be;
499
500         MOD_INC_USE_COUNT;
501         if (copy_from_user(&be, (void *) arg, sizeof be)) {
502                 MOD_DEC_USE_COUNT;
503                 return -EFAULT;
504         }
505         write_lock_irq(&devs_lock);
506         brdev = br2684_find_dev(&be.ifspec);
507         if (brdev == NULL) {
508                 printk(KERN_ERR
509                     "br2684: tried to attach to non-existant device\n");
510                 err = -ENXIO;
511                 goto error;
512         }
513         if (atmvcc->push == NULL) {
514                 err = -EBADFD;
515                 goto error;
516         }
517         if (!list_empty(&brdev->brvccs)) {      /* Only 1 VCC/dev right now */
518                 err = -EEXIST;
519                 goto error;
520         }
521         if (be.fcs_in != BR2684_FCSIN_NO || be.fcs_out != BR2684_FCSOUT_NO ||
522             be.fcs_auto || be.has_vpiid || be.send_padding || (be.encaps !=
523             BR2684_ENCAPS_VC && be.encaps != BR2684_ENCAPS_LLC) ||
524             be.min_size != 0) {
525                 err = -EINVAL;
526                 goto error;
527         }
528         brvcc = kmalloc(sizeof(struct br2684_vcc), GFP_KERNEL);
529         if (!brvcc) {
530                 err = -ENOMEM;
531                 goto error;
532         }
533         memset(brvcc, 0, sizeof(struct br2684_vcc));
534         DPRINTK("br2684_regvcc vcc=%p, encaps=%d, brvcc=%p\n", atmvcc, be.encaps,
535                 brvcc);
536         if (list_empty(&brdev->brvccs) && !brdev->mac_was_set) {
537                 unsigned char *esi = atmvcc->dev->esi;
538                 if (esi[0] | esi[1] | esi[2] | esi[3] | esi[4] | esi[5])
539                         memcpy(brdev->net_dev.dev_addr, esi,
540                             brdev->net_dev.addr_len);
541                 else
542                         brdev->net_dev.dev_addr[2] = 1;
543         }
544         list_add(&brvcc->brvccs, &brdev->brvccs);
545         write_unlock_irq(&devs_lock);
546         brvcc->brdev = brdev;
547         brvcc->atmvcc = atmvcc;
548         atmvcc->user_back = brvcc;
549         brvcc->encaps = (enum br2684_encaps) be.encaps;
550         brvcc->old_push = atmvcc->push;
551         barrier();
552         atmvcc->push = br2684_push;
553         skb_queue_head_init(&copy);
554         skb_migrate(&atmvcc->recvq, &copy);
555         while ((skb = skb_dequeue(&copy))) {
556                 BRPRIV(skb->dev)->stats.rx_bytes -= skb->len;
557                 BRPRIV(skb->dev)->stats.rx_packets--;
558                 br2684_push(atmvcc, skb);
559         }
560         return 0;
561     error:
562         write_unlock_irq(&devs_lock);
563         MOD_DEC_USE_COUNT;
564         return err;
565 }
566
567 static int br2684_create(unsigned long arg)
568 {
569         int err;
570         struct br2684_dev *brdev;
571         struct atm_newif_br2684 ni;
572
573         DPRINTK("br2684_create\n");
574         /*
575          * We track module use by vcc's NOT the devices they're on.  We're
576          * protected here against module death by the kernel_lock, but if
577          * we need to sleep we should make sure that the module doesn't
578          * disappear under us.
579          */
580         MOD_INC_USE_COUNT;
581         if (copy_from_user(&ni, (void *) arg, sizeof ni)) {
582                 MOD_DEC_USE_COUNT;
583                 return -EFAULT;
584         }
585         if (ni.media != BR2684_MEDIA_ETHERNET || ni.mtu != 1500) {
586                 MOD_DEC_USE_COUNT;
587                 return -EINVAL;
588         }
589         if ((brdev = kmalloc(sizeof(struct br2684_dev), GFP_KERNEL)) == NULL) {
590                 MOD_DEC_USE_COUNT;
591                 return -ENOMEM;
592         }
593         memset(brdev, 0, sizeof(struct br2684_dev));
594         INIT_LIST_HEAD(&brdev->brvccs);
595
596         write_lock_irq(&devs_lock);
597         brdev->number = list_empty(&br2684_devs) ? 1 :
598             list_entry_brdev(br2684_devs.prev)->number + 1;
599         list_add_tail(&brdev->br2684_devs, &br2684_devs);
600         write_unlock_irq(&devs_lock);
601
602         if (ni.ifname[0] != '\0') {
603                 memcpy(brdev->net_dev.name, ni.ifname,
604                     sizeof(brdev->net_dev.name));
605                 brdev->net_dev.name[sizeof(brdev->net_dev.name) - 1] = '\0';
606         } else
607                 sprintf(brdev->net_dev.name, "nas%d", brdev->number);
608         DPRINTK("registered netdev %s\n", brdev->net_dev.name);
609         ether_setup(&brdev->net_dev);
610         brdev->mac_was_set = 0;
611 #ifdef FASTER_VERSION
612         my_eth_header = brdev->net_dev.hard_header;
613         brdev->net_dev.hard_header = br2684_header;
614         my_eth_header_cache = brdev->net_dev.hard_header_cache;
615         brdev->net_dev.hard_header_cache = br2684_header_cache;
616         brdev->net_dev.hard_header_len = sizeof(llc_oui_pid_pad) + ETH_HLEN;    /* 10 + 14 */
617 #endif
618         my_eth_mac_addr = brdev->net_dev.set_mac_address;
619         brdev->net_dev.set_mac_address = br2684_mac_addr;
620         brdev->net_dev.hard_start_xmit = br2684_start_xmit;
621         brdev->net_dev.get_stats = br2684_get_stats;
622
623         /* open, stop, do_ioctl ? */
624         err = register_netdev(&brdev->net_dev);
625         MOD_DEC_USE_COUNT;
626         if (err < 0) {
627                 printk(KERN_ERR "br2684_create: register_netdev failed\n");
628                 write_lock_irq(&devs_lock);
629                 list_del(&brdev->br2684_devs);
630                 write_unlock_irq(&devs_lock);
631                 kfree(brdev);
632                 return err;
633         }
634         return 0;
635 }
636
637 /*
638  * This handles ioctls actually performed on our vcc - we must return
639  * -ENOIOCTLCMD for any unrecognized ioctl
640  */
641 static int br2684_ioctl(struct atm_vcc *atmvcc, unsigned int cmd,
642         unsigned long arg)
643 {
644         int err;
645         switch(cmd) {
646         case ATM_SETBACKEND:
647         case ATM_NEWBACKENDIF: {
648                 atm_backend_t b;
649                 MOD_INC_USE_COUNT;
650                 err = get_user(b, (atm_backend_t *) arg);
651                 MOD_DEC_USE_COUNT;
652                 if (err)
653                         return -EFAULT;
654                 if (b != ATM_BACKEND_BR2684)
655                         return -ENOIOCTLCMD;
656                 if (!capable(CAP_NET_ADMIN))
657                         return -EPERM;
658                 if (cmd == ATM_SETBACKEND)
659                         return br2684_regvcc(atmvcc, arg);
660                 else
661                         return br2684_create(arg);
662                 }
663 #ifdef CONFIG_ATM_BR2684_IPFILTER
664         case BR2684_SETFILT:
665                 if (atmvcc->push != br2684_push)
666                         return -ENOIOCTLCMD;
667                 if (!capable(CAP_NET_ADMIN))
668                         return -EPERM;
669                 MOD_INC_USE_COUNT;
670                 err = br2684_setfilt(atmvcc, arg);
671                 MOD_DEC_USE_COUNT;
672                 return err;
673 #endif /* CONFIG_ATM_BR2684_IPFILTER */
674         }
675         return -ENOIOCTLCMD;
676 }
677
678 /* Never put more than 256 bytes in at once */
679 static int br2684_proc_engine(loff_t pos, char *buf)
680 {
681         struct list_head *lhd, *lhc;
682         struct br2684_dev *brdev;
683         struct br2684_vcc *brvcc;
684         list_for_each(lhd, &br2684_devs) {
685                 brdev = list_entry_brdev(lhd);
686                 if (pos-- == 0)
687                         return sprintf(buf, "dev %.16s: num=%d, mac=%02X:%02X:"
688                             "%02X:%02X:%02X:%02X (%s)\n", brdev->net_dev.name,
689                             brdev->number,
690                             brdev->net_dev.dev_addr[0],
691                             brdev->net_dev.dev_addr[1],
692                             brdev->net_dev.dev_addr[2],
693                             brdev->net_dev.dev_addr[3],
694                             brdev->net_dev.dev_addr[4],
695                             brdev->net_dev.dev_addr[5],
696                             brdev->mac_was_set ? "set" : "auto");
697                 list_for_each(lhc, &brdev->brvccs) {
698                         brvcc = list_entry_brvcc(lhc);
699                         if (pos-- == 0)
700                                 return sprintf(buf, "  vcc %d.%d.%d: encaps=%s"
701 #ifndef FASTER_VERSION
702                                     ", failed copies %u/%u"
703 #endif /* FASTER_VERSION */
704                                     "\n", brvcc->atmvcc->dev->number,
705                                     brvcc->atmvcc->vpi, brvcc->atmvcc->vci,
706                                     (brvcc->encaps == e_llc) ? "LLC" : "VC"
707 #ifndef FASTER_VERSION
708                                     , brvcc->copies_failed
709                                     , brvcc->copies_needed
710 #endif /* FASTER_VERSION */
711                                     );
712 #ifdef CONFIG_ATM_BR2684_IPFILTER
713 #define b1(var, byte)   ((u8 *) &brvcc->filter.var)[byte]
714 #define bs(var)         b1(var, 0), b1(var, 1), b1(var, 2), b1(var, 3)
715                         if (brvcc->filter.netmask != 0 && pos-- == 0)
716                                 return sprintf(buf, "    filter=%d.%d.%d.%d/"
717                                     "%d.%d.%d.%d\n", bs(prefix), bs(netmask));
718 #undef bs
719 #undef b1
720 #endif /* CONFIG_ATM_BR2684_IPFILTER */
721                 }
722         }
723         return 0;
724 }
725
726 static ssize_t br2684_proc_read(struct file *file, char *buf, size_t count,
727         loff_t *pos)
728 {
729         unsigned long page;
730         int len = 0, x, left;
731         page = get_free_page(GFP_KERNEL);
732         if (!page)
733                 return -ENOMEM;
734         left = PAGE_SIZE - 256;
735         if (count < left)
736                 left = count;
737         read_lock(&devs_lock);
738         for (;;) {
739                 x = br2684_proc_engine(*pos, &((char *) page)[len]);
740                 if (x == 0)
741                         break;
742                 if (x > left)
743                         /*
744                          * This should only happen if the user passed in
745                          * a "count" too small for even one line
746                          */
747                         x = -EINVAL;
748                 if (x < 0) {
749                         len = x;
750                         break;
751                 }
752                 len += x;
753                 left -= x;
754                 (*pos)++;
755                 if (left < 256)
756                         break;
757         }
758         read_unlock(&devs_lock);
759         if (len > 0 && copy_to_user(buf, (char *) page, len))
760                 len = -EFAULT;
761         free_page(page);
762         return len;
763 }
764
765 static struct file_operations br2684_proc_operations = {
766         read: br2684_proc_read,
767 };
768
769 extern struct proc_dir_entry *atm_proc_root;    /* from proc.c */
770
771 extern int (*br2684_ioctl_hook)(struct atm_vcc *, unsigned int, unsigned long);
772
773 /* the following avoids some spurious warnings from the compiler */
774 #define UNUSED __attribute__((unused))
775
776 static int __init UNUSED br2684_init(void)
777 {
778         struct proc_dir_entry *p;
779         if ((p = create_proc_entry("br2684", 0, atm_proc_root)) == NULL)
780                 return -ENOMEM;
781         p->proc_fops = &br2684_proc_operations;
782         br2684_ioctl_hook = br2684_ioctl;
783         return 0;
784 }
785
786 static void __exit UNUSED br2684_exit(void)
787 {
788         struct br2684_dev *brdev;
789         br2684_ioctl_hook = NULL;
790         remove_proc_entry("br2684", atm_proc_root);
791         while (!list_empty(&br2684_devs)) {
792                 brdev = list_entry_brdev(br2684_devs.next);
793                 unregister_netdev(&brdev->net_dev);
794                 list_del(&brdev->br2684_devs);
795                 kfree(brdev);
796         }
797 }
798
799 module_init(br2684_init);
800 module_exit(br2684_exit);
801
802 MODULE_AUTHOR("Marcell GAL");
803 MODULE_DESCRIPTION("RFC2684 bridged protocols over ATM/AAL5");
804 MODULE_LICENSE("GPL");