added files
[bcm963xx.git] / kernel / linux / 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 #include <linux/atm.h>
20 #include <linux/atmdev.h>
21 #include <linux/seq_file.h>
22
23 #include <linux/atmbr2684.h>
24
25 #include "common.h"
26 #include "ipcommon.h"
27
28 /*
29  * Define this to use a version of the code which interacts with the higher
30  * layers in a more intellegent way, by always reserving enough space for
31  * our header at the begining of the packet.  However, there may still be
32  * some problems with programs like tcpdump.  In 2.5 we'll sort out what
33  * we need to do to get this perfect.  For now we just will copy the packet
34  * if we need space for the header
35  */
36 /* #define FASTER_VERSION */
37 //#define VLAN_DEBUG
38 //#define SKB_DEBUG
39
40 #ifdef DEBUG
41 #define DPRINTK(format, args...) printk(KERN_DEBUG "br2684: " format, ##args)
42 #else
43 #define DPRINTK(format, args...)
44 #endif
45
46 #ifdef SKB_DEBUG
47 static void skb_debug(const struct sk_buff *skb)
48 {
49 #define NUM2PRINT 50
50         char buf[NUM2PRINT * 3 + 1];    /* 3 chars per byte */
51         int i = 0;
52         for (i = 0; i < skb->len && i < NUM2PRINT; i++) {
53                 sprintf(buf + i * 3, "%2.2x ", 0xff & skb->data[i]);
54         }
55         printk(KERN_DEBUG "br2684: skb: %s\n", buf);
56 }
57 #else
58 #define skb_debug(skb)  do {} while (0)
59 #endif
60
61 static unsigned char llc_oui_pid_pad[] =
62     { 0xAA, 0xAA, 0x03, 0x00, 0x80, 0xC2, 0x00, 0x07, 0x00, 0x00 };
63 #define PADLEN  (2)
64
65 enum br2684_encaps {
66         e_vc  = BR2684_ENCAPS_VC,
67         e_llc = BR2684_ENCAPS_LLC,
68 };
69
70 struct br2684_vcc {
71         struct atm_vcc  *atmvcc;
72         struct net_device *device;
73         /* keep old push,pop functions for chaining */
74         void (*old_push)(struct atm_vcc *vcc,struct sk_buff *skb);
75         /* void (*old_pop)(struct atm_vcc *vcc,struct sk_buff *skb); */
76         enum br2684_encaps encaps;
77         struct list_head brvccs;
78 #ifdef CONFIG_ATM_BR2684_IPFILTER
79         struct br2684_filter filter;
80 #endif /* CONFIG_ATM_BR2684_IPFILTER */
81 #ifndef FASTER_VERSION
82         unsigned copies_needed, copies_failed;
83 #endif /* FASTER_VERSION */
84 #if defined(CONFIG_MIPS_BRCM)
85         /* Protocol filter flag, currently only PPPoE.
86            When turned on, all non-PPPoE traffic will be dropped
87            on this PVC */  
88         int proto_filter;
89 #ifdef SUPPORT_VLAN
90         unsigned short vlan_id;        /* vlan id (0-4096) */
91 #endif  // SUPPORT_VLAN
92
93
94 #endif
95 };
96
97 struct br2684_dev {
98         struct net_device *net_dev;
99         struct list_head br2684_devs;
100         int number;
101         struct list_head brvccs; /* one device <=> one vcc (before xmas) */
102         struct net_device_stats stats;
103         int mac_was_set;
104 };
105
106 #if defined(CONFIG_MIPS_BRCM)
107 #define MIN_PKT_SIZE 70
108 #endif
109
110 /*
111  * This lock should be held for writing any time the list of devices or
112  * their attached vcc's could be altered.  It should be held for reading
113  * any time these are being queried.  Note that we sometimes need to
114  * do read-locking under interrupt context, so write locking must block
115  * the current CPU's interrupts
116  */
117 static rwlock_t devs_lock = RW_LOCK_UNLOCKED;
118
119 static LIST_HEAD(br2684_devs);
120
121 static inline struct br2684_dev *BRPRIV(const struct net_device *net_dev)
122 {
123         return (struct br2684_dev *) net_dev->priv;
124 }
125
126 static inline struct net_device *list_entry_brdev(const struct list_head *le)
127 {
128         return list_entry(le, struct br2684_dev, br2684_devs)->net_dev;
129 }
130
131 static inline struct br2684_vcc *BR2684_VCC(const struct atm_vcc *atmvcc)
132 {
133         return (struct br2684_vcc *) (atmvcc->user_back);
134 }
135
136 static inline struct br2684_vcc *list_entry_brvcc(const struct list_head *le)
137 {
138         return list_entry(le, struct br2684_vcc, brvccs);
139 }
140
141 /* Caller should hold read_lock(&devs_lock) */
142 static struct net_device *br2684_find_dev(const struct br2684_if_spec *s)
143 {
144         struct list_head *lh;
145         struct net_device *net_dev;
146         switch (s->method) {
147         case BR2684_FIND_BYNUM:
148                 list_for_each(lh, &br2684_devs) {
149                         net_dev = list_entry_brdev(lh);
150                         if (BRPRIV(net_dev)->number == s->spec.devnum)
151                                 return net_dev;
152                 }
153                 break;
154         case BR2684_FIND_BYIFNAME:
155                 list_for_each(lh, &br2684_devs) {
156                         net_dev = list_entry_brdev(lh);
157                         if (!strncmp(net_dev->name, s->spec.ifname, IFNAMSIZ))
158                                 return net_dev;
159                 }
160                 break;
161         }
162         return NULL;
163 }
164
165 #ifdef SUPPORT_VLAN
166 #include <linux/if_vlan.h>
167 /**
168  * vlan_tag_insert - regular VLAN tag inserting
169  * @skb: skbuff to tag
170  * @tag: VLAN tag to insert
171  *
172  * Inserts the VLAN tag into @skb as part of the payload
173  * Returns a VLAN tagged skb. If a new skb is created, @skb is freed.
174  * 
175  * Following the skb_unshare() example, in case of error, the calling function
176  * doesn't have to worry about freeing the original skb.
177  */
178 #define ATM_HEADER_LEN     10
179 #define ATM_AND_MAC_LEN    ((ATM_HEADER_LEN) + (2 * VLAN_ETH_ALEN))
180
181 struct atm_vlan_ethhdr {
182    unsigned char        h_atm_stuff[ATM_HEADER_LEN]; /* atm stuff 10 bytes */
183    unsigned char        h_dest[ETH_ALEN];          /* destination eth addr      */
184    unsigned char        h_source[ETH_ALEN];        /* source ether addr */
185    unsigned short       h_vlan_proto;              /* Should always be 0x8100 */
186    unsigned short       h_vlan_TCI;                /* Encapsulates priority and VLAN ID */
187    unsigned short       h_vlan_encapsulated_proto; /* packet type ID field (or len) */
188 };
189
190 static inline struct sk_buff *vlan_tag_insert(struct sk_buff *skb, unsigned short tag)
191 {
192         struct atm_vlan_ethhdr *veth;
193
194         if (skb_headroom(skb) < VLAN_HLEN) {
195                 struct sk_buff *sk_tmp = skb;
196                 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
197                 kfree_skb(sk_tmp);
198                 if (!skb) {
199                         printk(KERN_ERR "vlan: failed to realloc headroom\n");
200                         return NULL;
201                 }
202         } else {
203                 skb = skb_unshare(skb, GFP_ATOMIC);
204                 if (!skb) {
205                         printk(KERN_ERR "vlan: failed to unshare skbuff\n");
206                         return NULL;
207                 }
208         }
209
210         veth = (struct atm_vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
211
212         /* Move the mac addresses to the beginning of the new header. */
213         memmove(skb->data, skb->data + VLAN_HLEN, (2 * VLAN_ETH_ALEN) + ATM_HEADER_LEN);
214
215         /* first, the ethernet type */
216         veth->h_vlan_proto = __constant_htons(ETH_P_8021Q);
217
218         /* now, the tag */
219         veth->h_vlan_TCI = htons(tag);
220
221         skb->protocol = __constant_htons(ETH_P_8021Q);
222         skb->mac.raw -= VLAN_HLEN;
223         skb->nh.raw -= VLAN_HLEN;
224
225         return skb;
226 }
227 #endif  // SUPPORT_VLAN
228
229 /*
230  * Send a packet out a particular vcc.  Not to useful right now, but paves
231  * the way for multiple vcc's per itf.  Returns true if we can send,
232  * otherwise false
233  */
234 static int br2684_xmit_vcc(struct sk_buff *skb, struct br2684_dev *brdev,
235         struct br2684_vcc *brvcc)
236 {
237         struct atm_vcc *atmvcc;
238
239 #ifdef FASTER_VERSION
240         if (brvcc->encaps == e_llc)
241                 memcpy(skb_push(skb, 8), llc_oui_pid_pad, 8);
242         /* last 2 bytes of llc_oui_pid_pad are managed by header routines;
243            yes, you got it: 8 + 2 = sizeof(llc_oui_pid_pad)
244          */
245 #else
246         int minheadroom = (brvcc->encaps == e_llc) ? 10 : 2;
247         if (skb_headroom(skb) < minheadroom) {
248                 struct sk_buff *skb2 = skb_realloc_headroom(skb, minheadroom);
249                 brvcc->copies_needed++;
250                 dev_kfree_skb(skb);
251                 if (skb2 == NULL) {
252                         brvcc->copies_failed++;
253                         return 0;
254                 }
255                 skb = skb2;
256         }
257         skb_push(skb, minheadroom);
258         if (brvcc->encaps == e_llc)
259                 memcpy(skb->data, llc_oui_pid_pad, 10);
260         else
261                 memset(skb->data, 0, 2);
262 #endif /* FASTER_VERSION */
263
264 #if defined(CONFIG_MIPS_BRCM)
265         if (skb->len < MIN_PKT_SIZE)
266         {
267                 struct sk_buff *skb2=skb_copy_expand(skb, 0, MIN_PKT_SIZE - skb->len, GFP_ATOMIC);
268                 dev_kfree_skb(skb);
269                 if (skb2 == NULL) {
270                         brvcc->copies_failed++;
271                         return 0;
272                 }
273                 skb = skb2;             
274                 memset(skb->tail, 0, MIN_PKT_SIZE - skb->len);          
275                 skb_put(skb, MIN_PKT_SIZE - skb->len);
276         }
277 #endif
278
279 #ifdef SUPPORT_VLAN
280 #ifdef VLAN_DEBUG
281         printk("=====> br2684_xmit_vcc  bef add vlan tag, skb->len=0x%04x\n", skb->len);
282         skb_debug(skb);
283 #endif // VLAN_DEBUG
284
285         /* Construct the second two bytes. This field looks something
286          * like:
287          * usr_priority: 3 bits  (high bits)
288          * CFI           1 bit
289          * VLAN ID       12 bits (low bits)
290          */
291         //brvcc->vlan_id |= vlan_dev_get_egress_qos_mask(dev, skb);
292         /* bit 3-0 of the 32-bit nfmark is the atm priority, set by iptables
293          * bit 7-4 is the Ethernet switch physical port number, set by lan port drivers.
294          * bit 8-11 is the wanVlan priority bits
295          */
296         if (brvcc->vlan_id != 0xffff) {
297                 brvcc->vlan_id &= 0xffff0fff;   // clear the priority bits first
298                 // if bit 8-11 is set (none zeros), add in the priority bits
299                 if (skb->nfmark & 0x0000f000) {
300                         brvcc->vlan_id |= (skb->nfmark & 0x0000f000);
301                 }
302                 skb = vlan_tag_insert(skb, brvcc->vlan_id);
303                 if (!skb) {
304                         brdev->stats.tx_dropped++;
305                         return 1;
306                 }
307 #ifdef VLAN_DEBUG
308                 printk("=====> br2684_xmit_vcc  aft add vlan tag, skb->len=%d\n", skb->len);
309                 skb_debug(skb);
310 #endif // VLAN_DEBUG
311         }
312 #endif  // SUPPORT_VLAN
313
314         ATM_SKB(skb)->vcc = atmvcc = brvcc->atmvcc;
315         DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, atmvcc, atmvcc->dev);
316         if (!atm_may_send(atmvcc, skb->truesize)) {
317                 /* we free this here for now, because we cannot know in a higher 
318                         layer whether the skb point it supplied wasn't freed yet.
319                         now, it always is.
320                 */
321                 dev_kfree_skb(skb);
322                 return 0;
323                 }
324         atomic_add(skb->truesize, &atmvcc->sk->sk_wmem_alloc);
325         ATM_SKB(skb)->atm_options = atmvcc->atm_options;
326         brdev->stats.tx_packets++;
327         brdev->stats.tx_bytes += skb->len;
328 #if defined(CONFIG_MIPS_BRCM)
329         if (atmvcc->send(atmvcc, skb) != 0)
330             brdev->stats.tx_dropped++;
331 #else
332         atmvcc->send(atmvcc, skb);
333 #endif  
334
335         return 1;
336 }
337
338 static inline struct br2684_vcc *pick_outgoing_vcc(struct sk_buff *skb,
339         struct br2684_dev *brdev)
340 {
341         return list_empty(&brdev->brvccs) ? NULL :
342             list_entry_brvcc(brdev->brvccs.next); /* 1 vcc/dev right now */
343 }
344
345 static int br2684_start_xmit(struct sk_buff *skb, struct net_device *dev)
346 {
347         struct br2684_dev *brdev = BRPRIV(dev);
348         struct br2684_vcc *brvcc;
349         DPRINTK("br2684_start_xmit, skb->dst=%p\n", skb->dst);
350         read_lock(&devs_lock);
351         brvcc = pick_outgoing_vcc(skb, brdev);
352         if (brvcc == NULL) {
353                 DPRINTK("no vcc attached to dev %s\n", dev->name);
354                 brdev->stats.tx_errors++;
355                 brdev->stats.tx_carrier_errors++;
356                 /* netif_stop_queue(dev); */
357                 dev_kfree_skb(skb);
358                 read_unlock(&devs_lock);
359                 return -EUNATCH;
360         }
361         
362 #if defined(CONFIG_MIPS_BRCM)
363         if (brvcc->proto_filter & FILTER_PPPOE) {
364                 if ((skb->protocol != htons(ETH_P_PPP_DISC)) && (skb->protocol != htons(ETH_P_PPP_SES))) {
365                         DPRINTK("non-PPPOE packet dropped on TX dev %s\n", dev->name);
366                         dev_kfree_skb(skb);
367                         read_unlock(&devs_lock);
368                         return 0;
369                 }
370         }
371 #endif  
372
373 #ifdef VLAN_DEBUG
374         if (brvcc->vlan_id != 0xffff)
375                 printk("=====> br2684_start_xmit  vlan_id=0x%04x\n", brvcc->vlan_id);
376 #endif // VLAN_DEBUG
377
378         if (!br2684_xmit_vcc(skb, brdev, brvcc)) {
379                 /*
380                  * We should probably use netif_*_queue() here, but that
381                  * involves added complication.  We need to walk before
382                  * we can run
383                  */
384                 /* don't free here! this pointer might be no longer valid!
385                 dev_kfree_skb(skb);
386                 */
387                 brdev->stats.tx_errors++;
388                 brdev->stats.tx_fifo_errors++;
389         }
390         read_unlock(&devs_lock);
391         return 0;
392 }
393
394 static struct net_device_stats *br2684_get_stats(struct net_device *dev)
395 {
396         DPRINTK("br2684_get_stats\n");
397         return &BRPRIV(dev)->stats;
398 }
399
400 #ifdef FASTER_VERSION
401 /*
402  * These mirror eth_header and eth_header_cache.  They are not usually
403  * exported for use in modules, so we grab them from net_device
404  * after ether_setup() is done with it.  Bit of a hack.
405  */
406 static int (*my_eth_header)(struct sk_buff *, struct net_device *,
407         unsigned short, void *, void *, unsigned);
408 static int (*my_eth_header_cache)(struct neighbour *, struct hh_cache *);
409
410 static int
411 br2684_header(struct sk_buff *skb, struct net_device *dev,
412               unsigned short type, void *daddr, void *saddr, unsigned len)
413 {
414         u16 *pad_before_eth;
415         int t = my_eth_header(skb, dev, type, daddr, saddr, len);
416         if (t > 0) {
417                 pad_before_eth = (u16 *) skb_push(skb, 2);
418                 *pad_before_eth = 0;
419                 return dev->hard_header_len;    /* or return 16; ? */
420         } else
421                 return t;
422 }
423
424 static int
425 br2684_header_cache(struct neighbour *neigh, struct hh_cache *hh)
426 {
427 /* hh_data is 16 bytes long. if encaps is ether-llc we need 24, so
428 xmit will add the additional header part in that case */
429         u16 *pad_before_eth = (u16 *)(hh->hh_data);
430         int t = my_eth_header_cache(neigh, hh);
431         DPRINTK("br2684_header_cache, neigh=%p, hh_cache=%p\n", neigh, hh);
432         if (t < 0)
433                 return t;
434         else {
435                 *pad_before_eth = 0;
436                 hh->hh_len = PADLEN + ETH_HLEN;
437         }
438         return 0;
439 }
440
441 /*
442  * This is similar to eth_type_trans, which cannot be used because of
443  * our dev->hard_header_len
444  */
445 static inline unsigned short br_type_trans(struct sk_buff *skb,
446                                                struct net_device *dev)
447 {
448         struct ethhdr *eth;
449         unsigned char *rawp;
450         eth = skb->mac.ethernet;
451
452         if (*eth->h_dest & 1) {
453                 if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0)
454                         skb->pkt_type = PACKET_BROADCAST;
455                 else
456                         skb->pkt_type = PACKET_MULTICAST;
457         }
458
459         else if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
460                 skb->pkt_type = PACKET_OTHERHOST;
461
462         if (ntohs(eth->h_proto) >= 1536)
463                 return eth->h_proto;
464
465         rawp = skb->data;
466
467         /*
468          * This is a magic hack to spot IPX packets. Older Novell breaks
469          * the protocol design and runs IPX over 802.3 without an 802.2 LLC
470          * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
471          * won't work for fault tolerant netware but does for the rest.
472          */
473         if (*(unsigned short *) rawp == 0xFFFF)
474                 return htons(ETH_P_802_3);
475
476         /*
477          * Real 802.2 LLC
478          */
479         return htons(ETH_P_802_2);
480 }
481 #endif /* FASTER_VERSION */
482
483 /*
484  * We remember when the MAC gets set, so we don't override it later with
485  * the ESI of the ATM card of the first VC
486  */
487 static int (*my_eth_mac_addr)(struct net_device *, void *);
488 static int br2684_mac_addr(struct net_device *dev, void *p)
489 {
490         int err = my_eth_mac_addr(dev, p);
491         if (!err)
492                 BRPRIV(dev)->mac_was_set = 1;
493         return err;
494 }
495
496 #ifdef CONFIG_ATM_BR2684_IPFILTER
497 /* this IOCTL is experimental. */
498 static int br2684_setfilt(struct atm_vcc *atmvcc, void __user *arg)
499 {
500         struct br2684_vcc *brvcc;
501         struct br2684_filter_set fs;
502
503         if (copy_from_user(&fs, arg, sizeof fs))
504                 return -EFAULT;
505         if (fs.ifspec.method != BR2684_FIND_BYNOTHING) {
506                 /*
507                  * This is really a per-vcc thing, but we can also search
508                  * by device
509                  */
510                 struct br2684_dev *brdev;
511                 read_lock(&devs_lock);
512                 brdev = BRPRIV(br2684_find_dev(&fs.ifspec));
513                 if (brdev == NULL || list_empty(&brdev->brvccs) ||
514                     brdev->brvccs.next != brdev->brvccs.prev)  /* >1 VCC */
515                         brvcc = NULL;
516                 else
517                         brvcc = list_entry_brvcc(brdev->brvccs.next);
518                 read_unlock(&devs_lock);
519                 if (brvcc == NULL)
520                         return -ESRCH;
521         } else
522                 brvcc = BR2684_VCC(atmvcc);
523         memcpy(&brvcc->filter, &fs.filter, sizeof(brvcc->filter));
524         return 0;
525 }
526
527 /* Returns 1 if packet should be dropped */
528 static inline int
529 packet_fails_filter(u16 type, struct br2684_vcc *brvcc, struct sk_buff *skb)
530 {
531         if (brvcc->filter.netmask == 0)
532                 return 0;                       /* no filter in place */
533         if (type == __constant_htons(ETH_P_IP) &&
534             (((struct iphdr *) (skb->data))->daddr & brvcc->filter.
535              netmask) == brvcc->filter.prefix)
536                 return 0;
537         if (type == __constant_htons(ETH_P_ARP))
538                 return 0;
539         /* TODO: we should probably filter ARPs too.. don't want to have
540          *   them returning values that don't make sense, or is that ok?
541          */
542         return 1;               /* drop */
543 }
544 #endif /* CONFIG_ATM_BR2684_IPFILTER */
545
546 static void br2684_close_vcc(struct br2684_vcc *brvcc)
547 {
548         DPRINTK("removing VCC %p from dev %p\n", brvcc, brvcc->device);
549         write_lock_irq(&devs_lock);
550         list_del(&brvcc->brvccs);
551         write_unlock_irq(&devs_lock);
552         brvcc->atmvcc->user_back = NULL;        /* what about vcc->recvq ??? */
553         brvcc->old_push(brvcc->atmvcc, NULL);   /* pass on the bad news */
554         kfree(brvcc);
555         module_put(THIS_MODULE);
556 }
557
558 /* when AAL5 PDU comes in: */
559 static void br2684_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
560 {
561         struct br2684_vcc *brvcc = BR2684_VCC(atmvcc);
562         struct net_device *net_dev = brvcc->device;
563         struct br2684_dev *brdev = BRPRIV(net_dev);
564         int plen = sizeof(llc_oui_pid_pad) + ETH_HLEN;
565
566         DPRINTK("br2684_push\n");
567
568         if (unlikely(skb == NULL)) {
569                 /* skb==NULL means VCC is being destroyed */
570                 br2684_close_vcc(brvcc);
571                 if (list_empty(&brdev->brvccs)) {
572                         read_lock(&devs_lock);
573                         list_del(&brdev->br2684_devs);
574                         read_unlock(&devs_lock);
575                         unregister_netdev(net_dev);
576                         free_netdev(net_dev);
577                 }
578                 return;
579         }
580 #if defined(CONFIG_MIPS_BRCM)
581         //skb->__unused=FROM_WAN;
582 #endif  
583
584         atm_return(atmvcc, skb->truesize);
585         DPRINTK("skb from brdev %p\n", brdev);
586         if (brvcc->encaps == e_llc) {
587                 /* let us waste some time for checking the encapsulation.
588                    Note, that only 7 char is checked so frames with a valid FCS
589                    are also accepted (but FCS is not checked of course) */
590                 if (memcmp(skb->data, llc_oui_pid_pad, 7)) {
591                         brdev->stats.rx_errors++;
592                         dev_kfree_skb(skb);
593                         return;
594                 }
595
596                 /* Strip FCS if present */
597                 if (skb->len > 7 && skb->data[7] == 0x01)
598                         __skb_trim(skb, skb->len - 4);
599         } else {
600                 plen = PADLEN + ETH_HLEN;       /* pad, dstmac,srcmac, ethtype */
601                 /* first 2 chars should be 0 */
602                 if (*((u16 *) (skb->data)) != 0) {
603                         brdev->stats.rx_errors++;
604                         dev_kfree_skb(skb);
605                         return;
606                 }
607         }
608         if (skb->len < plen) {
609                 brdev->stats.rx_errors++;
610                 dev_kfree_skb(skb);     /* dev_ not needed? */
611                 return;
612         }
613 #ifdef SUPPORT_VLAN
614    if (brvcc->vlan_id != 0xffff) { /*  Vcc was configured to be vlan tagged*/
615                 unsigned tmp[ATM_AND_MAC_LEN];
616 #ifdef VLAN_DEBUG
617                 printk("=====> before removing vlan id\n");
618                 skb_debug(skb);
619 #endif
620 /*
621 ** There exist a situation where we tag vlan id upstream. But DSLAM sends untagged frame downstream.  So we need to check this situation before we move data around
622 */
623 //eddie added if {}
624             if ( skb->data[22] == 0x81 && skb->data[23] == 0x0) {
625                 memcpy(tmp, skb->data, ATM_AND_MAC_LEN);
626                 skb_pull(skb, VLAN_HLEN);
627                 memcpy(skb->data, tmp, ATM_AND_MAC_LEN);
628             }
629 #ifdef VLAN_DEBUG
630                 printk("=====> after removing vlan id\n");
631                 skb_debug(skb);
632 #endif
633         }
634 #endif // SUPPORT_VLAN
635
636 #ifdef FASTER_VERSION
637         /* FIXME: tcpdump shows that pointer to mac header is 2 bytes earlier,
638            than should be. What else should I set? */
639         skb_pull(skb, plen);
640         skb->mac.raw = ((char *) (skb->data)) - ETH_HLEN;
641         skb->pkt_type = PACKET_HOST;
642 #ifdef CONFIG_BR2684_FAST_TRANS
643         skb->protocol = ((u16 *) skb->data)[-1];
644 #else                           /* some protocols might require this: */
645         skb->protocol = br_type_trans(skb, net_dev);
646 #endif /* CONFIG_BR2684_FAST_TRANS */
647 #else
648         skb_pull(skb, plen - ETH_HLEN);
649         skb->protocol = eth_type_trans(skb, net_dev);
650 #endif /* FASTER_VERSION */
651 #ifdef CONFIG_ATM_BR2684_IPFILTER
652         if (unlikely(packet_fails_filter(skb->protocol, brvcc, skb))) {
653                 brdev->stats.rx_dropped++;
654                 dev_kfree_skb(skb);
655                 return;
656         }
657 #endif /* CONFIG_ATM_BR2684_IPFILTER */
658 #if defined(CONFIG_MIPS_BRCM)
659         if (brvcc->proto_filter & FILTER_PPPOE) {
660                 if ((skb->protocol != htons(ETH_P_PPP_DISC)) && (skb->protocol != htons(ETH_P_PPP_SES))) {
661                         DPRINTK("non-PPPOE packet dropped on RX dev %s\n", net_dev->name);
662                         dev_kfree_skb(skb);
663                         return;
664                 }
665         }
666 #endif
667
668         skb->dev = net_dev;
669         ATM_SKB(skb)->vcc = atmvcc;     /* needed ? */
670         DPRINTK("received packet's protocol: %x\n", ntohs(skb->protocol));
671
672         if (unlikely(!(net_dev->flags & IFF_UP))) {
673                 /* sigh, interface is down */
674                 brdev->stats.rx_dropped++;
675                 dev_kfree_skb(skb);
676                 return;
677         }
678         brdev->stats.rx_packets++;
679         brdev->stats.rx_bytes += skb->len;
680         memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
681
682         netif_rx(skb);
683 }
684
685 static int br2684_regvcc(struct atm_vcc *atmvcc, void __user *arg)
686 {
687 /* assign a vcc to a dev
688 Note: we do not have explicit unassign, but look at _push()
689 */
690         int err;
691         struct br2684_vcc *brvcc;
692         struct sk_buff_head copy;
693         struct sk_buff *skb;
694         struct br2684_dev *brdev;
695         struct net_device *net_dev;
696         struct atm_backend_br2684 be;
697
698         if (copy_from_user(&be, arg, sizeof be))
699                 return -EFAULT;
700         brvcc = kmalloc(sizeof(struct br2684_vcc), GFP_KERNEL);
701         if (!brvcc)
702                 return -ENOMEM;
703         memset(brvcc, 0, sizeof(struct br2684_vcc));
704         write_lock_irq(&devs_lock);
705         net_dev = br2684_find_dev(&be.ifspec);
706         if (net_dev == NULL) {
707                 printk(KERN_ERR
708                     "br2684: tried to attach to non-existant device\n");
709                 err = -ENXIO;
710                 goto error;
711         }
712         brdev = BRPRIV(net_dev);
713         if (atmvcc->push == NULL) {
714                 err = -EBADFD;
715                 goto error;
716         }
717         if (!list_empty(&brdev->brvccs)) {
718                 /* Only 1 VCC/dev right now */
719                 err = -EEXIST;
720                 goto error;
721         }
722         if (be.fcs_in != BR2684_FCSIN_NO || be.fcs_out != BR2684_FCSOUT_NO ||
723             be.fcs_auto || be.has_vpiid || be.send_padding || (be.encaps !=
724             BR2684_ENCAPS_VC && be.encaps != BR2684_ENCAPS_LLC) ||
725             be.min_size != 0) {
726                 err = -EINVAL;
727                 goto error;
728         }
729         DPRINTK("br2684_regvcc vcc=%p, encaps=%d, brvcc=%p\n", atmvcc, be.encaps,
730                 brvcc);
731         if (list_empty(&brdev->brvccs) && !brdev->mac_was_set) {
732                 unsigned char *esi = atmvcc->dev->esi;
733                 if (esi[0] | esi[1] | esi[2] | esi[3] | esi[4] | esi[5])
734                         memcpy(net_dev->dev_addr, esi, net_dev->addr_len);
735                 else
736                         net_dev->dev_addr[2] = 1;
737         }
738         list_add(&brvcc->brvccs, &brdev->brvccs);
739         write_unlock_irq(&devs_lock);
740         brvcc->device = net_dev;
741         brvcc->atmvcc = atmvcc;
742         atmvcc->user_back = brvcc;
743         brvcc->encaps = (enum br2684_encaps) be.encaps;
744         brvcc->old_push = atmvcc->push;
745 #if defined(CONFIG_MIPS_BRCM)
746         brvcc->proto_filter |= be.proto_filter;
747 #ifdef SUPPORT_VLAN
748         brvcc->vlan_id = be.vlan_id;
749 #endif // SUPPORT_VLAN
750 #endif  
751         barrier();
752         atmvcc->push = br2684_push;
753         skb_queue_head_init(&copy);
754         skb_migrate(&atmvcc->sk->sk_receive_queue, &copy);
755         while ((skb = skb_dequeue(&copy)) != NULL) {
756                 BRPRIV(skb->dev)->stats.rx_bytes -= skb->len;
757                 BRPRIV(skb->dev)->stats.rx_packets--;
758                 br2684_push(atmvcc, skb);
759         }
760         __module_get(THIS_MODULE);
761         return 0;
762     error:
763         write_unlock_irq(&devs_lock);
764         kfree(brvcc);
765         return err;
766 }
767
768 static void br2684_setup(struct net_device *netdev)
769 {
770         struct br2684_dev *brdev = BRPRIV(netdev);
771
772         ether_setup(netdev);
773         brdev->net_dev = netdev;
774
775 #ifdef FASTER_VERSION
776         my_eth_header = netdev->hard_header;
777         netdev->hard_header = br2684_header;
778         my_eth_header_cache = netdev->hard_header_cache;
779         netdev->hard_header_cache = br2684_header_cache;
780         netdev->hard_header_len = sizeof(llc_oui_pid_pad) + ETH_HLEN;   /* 10 + 14 */
781 #endif
782         my_eth_mac_addr = netdev->set_mac_address;
783         netdev->set_mac_address = br2684_mac_addr;
784         netdev->hard_start_xmit = br2684_start_xmit;
785         netdev->get_stats = br2684_get_stats;
786
787         INIT_LIST_HEAD(&brdev->brvccs);
788 }
789
790 static int br2684_create(void __user *arg)
791 {
792         int err;
793         struct net_device *netdev;
794         struct br2684_dev *brdev;
795         struct atm_newif_br2684 ni;
796
797         DPRINTK("br2684_create\n");
798
799         if (copy_from_user(&ni, arg, sizeof ni)) {
800                 return -EFAULT;
801         }
802         if (ni.media != BR2684_MEDIA_ETHERNET || ni.mtu != 1500) {
803                 return -EINVAL;
804         }
805
806         netdev = alloc_netdev(sizeof(struct br2684_dev),
807                               ni.ifname[0] ? ni.ifname : "nas%d",
808                               br2684_setup);
809         if (!netdev)
810                 return -ENOMEM;
811
812         brdev = BRPRIV(netdev);
813
814         DPRINTK("registered netdev %s\n", netdev->name);
815         /* open, stop, do_ioctl ? */
816         err = register_netdev(netdev);
817         if (err < 0) {
818                 printk(KERN_ERR "br2684_create: register_netdev failed\n");
819                 free_netdev(netdev);
820                 return err;
821         }
822
823         write_lock_irq(&devs_lock);
824         brdev->number = list_empty(&br2684_devs) ? 1 :
825             BRPRIV(list_entry_brdev(br2684_devs.prev))->number + 1;
826         list_add_tail(&brdev->br2684_devs, &br2684_devs);
827         write_unlock_irq(&devs_lock);
828         return 0;
829 }
830
831 /*
832  * This handles ioctls actually performed on our vcc - we must return
833  * -ENOIOCTLCMD for any unrecognized ioctl
834  */
835 static int br2684_ioctl(struct socket *sock, unsigned int cmd,
836         unsigned long arg)
837 {
838         struct atm_vcc *atmvcc = ATM_SD(sock);
839         void __user *argp = (void __user *)arg;
840
841         int err;
842         switch(cmd) {
843         case ATM_SETBACKEND:
844         case ATM_NEWBACKENDIF: {
845                 atm_backend_t b;
846                 err = get_user(b, (atm_backend_t __user *) argp);
847                 if (err)
848                         return -EFAULT;
849                 if (b != ATM_BACKEND_BR2684)
850                         return -ENOIOCTLCMD;
851                 if (!capable(CAP_NET_ADMIN))
852                         return -EPERM;
853                 if (cmd == ATM_SETBACKEND)
854                         return br2684_regvcc(atmvcc, argp);
855                 else
856                         return br2684_create(argp);
857                 }
858 #ifdef CONFIG_ATM_BR2684_IPFILTER
859         case BR2684_SETFILT:
860                 if (atmvcc->push != br2684_push)
861                         return -ENOIOCTLCMD;
862                 if (!capable(CAP_NET_ADMIN))
863                         return -EPERM;
864                 err = br2684_setfilt(atmvcc, argp);
865                 return err;
866 #endif /* CONFIG_ATM_BR2684_IPFILTER */
867         }
868         return -ENOIOCTLCMD;
869 }
870
871 static struct atm_ioctl br2684_ioctl_ops = {
872         .owner  = THIS_MODULE,
873         .ioctl  = br2684_ioctl,
874 };
875
876
877 #ifdef CONFIG_PROC_FS
878 static void *br2684_seq_start(struct seq_file *seq, loff_t *pos)
879 {
880         loff_t offs = 0;
881         struct br2684_dev *brd;
882
883         read_lock(&devs_lock);
884
885         list_for_each_entry(brd, &br2684_devs, br2684_devs) {
886                 if (offs == *pos)
887                         return brd;
888                 ++offs;
889         }
890         return NULL;
891 }
892
893 static void *br2684_seq_next(struct seq_file *seq, void *v, loff_t *pos)
894 {
895         struct br2684_dev *brd = v;
896
897         ++*pos;
898
899         brd = list_entry(brd->br2684_devs.next, 
900                          struct br2684_dev, br2684_devs);
901         return (&brd->br2684_devs != &br2684_devs) ? brd : NULL;
902 }
903
904 static void br2684_seq_stop(struct seq_file *seq, void *v)
905 {
906         read_unlock(&devs_lock);
907 }
908
909 static int br2684_seq_show(struct seq_file *seq, void *v)
910 {
911         const struct br2684_dev *brdev = v;
912         const struct net_device *net_dev = brdev->net_dev;
913         const struct br2684_vcc *brvcc;
914
915         seq_printf(seq, "dev %.16s: num=%d, mac=%02X:%02X:"
916                        "%02X:%02X:%02X:%02X (%s)\n", net_dev->name,
917                        brdev->number,
918                        net_dev->dev_addr[0],
919                        net_dev->dev_addr[1],
920                        net_dev->dev_addr[2],
921                        net_dev->dev_addr[3],
922                        net_dev->dev_addr[4],
923                        net_dev->dev_addr[5],
924                        brdev->mac_was_set ? "set" : "auto");
925
926         list_for_each_entry(brvcc, &brdev->brvccs, brvccs) {
927                 seq_printf(seq, "  vcc %d.%d.%d: encaps=%s"
928 #ifndef FASTER_VERSION
929                                     ", failed copies %u/%u"
930 #endif /* FASTER_VERSION */
931                                     "\n", brvcc->atmvcc->dev->number,
932                                     brvcc->atmvcc->vpi, brvcc->atmvcc->vci,
933                                     (brvcc->encaps == e_llc) ? "LLC" : "VC"
934 #ifndef FASTER_VERSION
935                                     , brvcc->copies_failed
936                                     , brvcc->copies_needed
937 #endif /* FASTER_VERSION */
938                                     );
939 #ifdef CONFIG_ATM_BR2684_IPFILTER
940 #define b1(var, byte)   ((u8 *) &brvcc->filter.var)[byte]
941 #define bs(var)         b1(var, 0), b1(var, 1), b1(var, 2), b1(var, 3)
942                         if (brvcc->filter.netmask != 0)
943                                 seq_printf(seq, "    filter=%d.%d.%d.%d/"
944                                                 "%d.%d.%d.%d\n",
945                                                 bs(prefix), bs(netmask));
946 #undef bs
947 #undef b1
948 #endif /* CONFIG_ATM_BR2684_IPFILTER */
949         }
950         return 0;
951 }
952
953 static struct seq_operations br2684_seq_ops = {
954         .start = br2684_seq_start,
955         .next  = br2684_seq_next,
956         .stop  = br2684_seq_stop,
957         .show  = br2684_seq_show,
958 };
959
960 static int br2684_proc_open(struct inode *inode, struct file *file)
961 {
962         return seq_open(file, &br2684_seq_ops);
963 }
964
965 static struct file_operations br2684_proc_ops = {
966         .owner   = THIS_MODULE,
967         .open    = br2684_proc_open,
968         .read    = seq_read,
969         .llseek  = seq_lseek,
970         .release = seq_release,
971 };
972
973 extern struct proc_dir_entry *atm_proc_root;    /* from proc.c */
974 #endif
975
976 static int __init br2684_init(void)
977 {
978 #ifdef CONFIG_PROC_FS
979         struct proc_dir_entry *p;
980         if ((p = create_proc_entry("br2684", 0, atm_proc_root)) == NULL)
981                 return -ENOMEM;
982         p->proc_fops = &br2684_proc_ops;
983 #endif
984         register_atm_ioctl(&br2684_ioctl_ops);
985         return 0;
986 }
987
988 static void __exit br2684_exit(void)
989 {
990         struct net_device *net_dev;
991         struct br2684_dev *brdev;
992         struct br2684_vcc *brvcc;
993         deregister_atm_ioctl(&br2684_ioctl_ops);
994
995 #ifdef CONFIG_PROC_FS
996         remove_proc_entry("br2684", atm_proc_root);
997 #endif
998
999         while (!list_empty(&br2684_devs)) {
1000                 net_dev = list_entry_brdev(br2684_devs.next);
1001                 brdev = BRPRIV(net_dev);
1002                 while (!list_empty(&brdev->brvccs)) {
1003                         brvcc = list_entry_brvcc(brdev->brvccs.next);
1004                         br2684_close_vcc(brvcc);
1005                 }
1006
1007                 list_del(&brdev->br2684_devs);
1008                 unregister_netdev(net_dev);
1009                 free_netdev(net_dev);
1010         }
1011 }
1012
1013 module_init(br2684_init);
1014 module_exit(br2684_exit);
1015
1016 MODULE_AUTHOR("Marcell GAL");
1017 MODULE_DESCRIPTION("RFC2684 bridged protocols over ATM/AAL5");
1018 MODULE_LICENSE("GPL");