blob: c0f6861eefe35061061d7ae39d4ecde87d8777da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2Experimental ethernet netdevice using ATM AAL5 as underlying carrier
3(RFC1483 obsoleted by RFC2684) for Linux 2.4
4Author: Marcell GAL, 2000, XDSL Ltd, Hungary
5*/
6
7#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/init.h>
9#include <linux/kernel.h>
10#include <linux/list.h>
11#include <linux/netdevice.h>
12#include <linux/skbuff.h>
13#include <linux/etherdevice.h>
14#include <linux/rtnetlink.h>
15#include <linux/ip.h>
16#include <asm/uaccess.h>
17#include <net/arp.h>
18#include <linux/atm.h>
19#include <linux/atmdev.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080020#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/seq_file.h>
22
23#include <linux/atmbr2684.h>
24
25#include "common.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/*
28 * Define this to use a version of the code which interacts with the higher
29 * layers in a more intellegent way, by always reserving enough space for
30 * our header at the begining of the packet. However, there may still be
31 * some problems with programs like tcpdump. In 2.5 we'll sort out what
32 * we need to do to get this perfect. For now we just will copy the packet
33 * if we need space for the header
34 */
35/* #define FASTER_VERSION */
36
37#ifdef DEBUG
38#define DPRINTK(format, args...) printk(KERN_DEBUG "br2684: " format, ##args)
39#else
40#define DPRINTK(format, args...)
41#endif
42
43#ifdef SKB_DEBUG
44static void skb_debug(const struct sk_buff *skb)
45{
46#define NUM2PRINT 50
47 char buf[NUM2PRINT * 3 + 1]; /* 3 chars per byte */
48 int i = 0;
49 for (i = 0; i < skb->len && i < NUM2PRINT; i++) {
50 sprintf(buf + i * 3, "%2.2x ", 0xff & skb->data[i]);
51 }
52 printk(KERN_DEBUG "br2684: skb: %s\n", buf);
53}
54#else
55#define skb_debug(skb) do {} while (0)
56#endif
57
58static unsigned char llc_oui_pid_pad[] =
59 { 0xAA, 0xAA, 0x03, 0x00, 0x80, 0xC2, 0x00, 0x07, 0x00, 0x00 };
60#define PADLEN (2)
61
62enum br2684_encaps {
63 e_vc = BR2684_ENCAPS_VC,
64 e_llc = BR2684_ENCAPS_LLC,
65};
66
67struct br2684_vcc {
68 struct atm_vcc *atmvcc;
69 struct net_device *device;
70 /* keep old push,pop functions for chaining */
71 void (*old_push)(struct atm_vcc *vcc,struct sk_buff *skb);
72 /* void (*old_pop)(struct atm_vcc *vcc,struct sk_buff *skb); */
73 enum br2684_encaps encaps;
74 struct list_head brvccs;
75#ifdef CONFIG_ATM_BR2684_IPFILTER
76 struct br2684_filter filter;
77#endif /* CONFIG_ATM_BR2684_IPFILTER */
78#ifndef FASTER_VERSION
79 unsigned copies_needed, copies_failed;
80#endif /* FASTER_VERSION */
81};
82
83struct br2684_dev {
84 struct net_device *net_dev;
85 struct list_head br2684_devs;
86 int number;
87 struct list_head brvccs; /* one device <=> one vcc (before xmas) */
88 struct net_device_stats stats;
89 int mac_was_set;
90};
91
92/*
93 * This lock should be held for writing any time the list of devices or
94 * their attached vcc's could be altered. It should be held for reading
95 * any time these are being queried. Note that we sometimes need to
96 * do read-locking under interrupt context, so write locking must block
97 * the current CPU's interrupts
98 */
99static DEFINE_RWLOCK(devs_lock);
100
101static LIST_HEAD(br2684_devs);
102
103static inline struct br2684_dev *BRPRIV(const struct net_device *net_dev)
104{
105 return (struct br2684_dev *) net_dev->priv;
106}
107
108static inline struct net_device *list_entry_brdev(const struct list_head *le)
109{
110 return list_entry(le, struct br2684_dev, br2684_devs)->net_dev;
111}
112
113static inline struct br2684_vcc *BR2684_VCC(const struct atm_vcc *atmvcc)
114{
115 return (struct br2684_vcc *) (atmvcc->user_back);
116}
117
118static inline struct br2684_vcc *list_entry_brvcc(const struct list_head *le)
119{
120 return list_entry(le, struct br2684_vcc, brvccs);
121}
122
123/* Caller should hold read_lock(&devs_lock) */
124static struct net_device *br2684_find_dev(const struct br2684_if_spec *s)
125{
126 struct list_head *lh;
127 struct net_device *net_dev;
128 switch (s->method) {
129 case BR2684_FIND_BYNUM:
130 list_for_each(lh, &br2684_devs) {
131 net_dev = list_entry_brdev(lh);
132 if (BRPRIV(net_dev)->number == s->spec.devnum)
133 return net_dev;
134 }
135 break;
136 case BR2684_FIND_BYIFNAME:
137 list_for_each(lh, &br2684_devs) {
138 net_dev = list_entry_brdev(lh);
139 if (!strncmp(net_dev->name, s->spec.ifname, IFNAMSIZ))
140 return net_dev;
141 }
142 break;
143 }
144 return NULL;
145}
146
147/*
148 * Send a packet out a particular vcc. Not to useful right now, but paves
149 * the way for multiple vcc's per itf. Returns true if we can send,
150 * otherwise false
151 */
152static int br2684_xmit_vcc(struct sk_buff *skb, struct br2684_dev *brdev,
153 struct br2684_vcc *brvcc)
154{
155 struct atm_vcc *atmvcc;
156#ifdef FASTER_VERSION
157 if (brvcc->encaps == e_llc)
158 memcpy(skb_push(skb, 8), llc_oui_pid_pad, 8);
159 /* last 2 bytes of llc_oui_pid_pad are managed by header routines;
160 yes, you got it: 8 + 2 = sizeof(llc_oui_pid_pad)
161 */
162#else
163 int minheadroom = (brvcc->encaps == e_llc) ? 10 : 2;
164 if (skb_headroom(skb) < minheadroom) {
165 struct sk_buff *skb2 = skb_realloc_headroom(skb, minheadroom);
166 brvcc->copies_needed++;
167 dev_kfree_skb(skb);
168 if (skb2 == NULL) {
169 brvcc->copies_failed++;
170 return 0;
171 }
172 skb = skb2;
173 }
174 skb_push(skb, minheadroom);
175 if (brvcc->encaps == e_llc)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300176 skb_copy_to_linear_data(skb, llc_oui_pid_pad, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 else
178 memset(skb->data, 0, 2);
179#endif /* FASTER_VERSION */
180 skb_debug(skb);
181
182 ATM_SKB(skb)->vcc = atmvcc = brvcc->atmvcc;
183 DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, atmvcc, atmvcc->dev);
184 if (!atm_may_send(atmvcc, skb->truesize)) {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900185 /* we free this here for now, because we cannot know in a higher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 layer whether the skb point it supplied wasn't freed yet.
187 now, it always is.
188 */
189 dev_kfree_skb(skb);
190 return 0;
191 }
192 atomic_add(skb->truesize, &sk_atm(atmvcc)->sk_wmem_alloc);
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
200static 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
207static 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);
Jean-Denis Boyer4f55cd12005-10-07 13:44:35 -0700222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
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
240static 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 */
252static int (*my_eth_header)(struct sk_buff *, struct net_device *,
253 unsigned short, void *, void *, unsigned);
254static int (*my_eth_header_cache)(struct neighbour *, struct hh_cache *);
255
256static int
257br2684_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
270static int
271br2684_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
274xmit 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 */
Alexey Dobriyanab611482005-07-12 12:08:43 -0700291static inline __be16 br_type_trans(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct ethhdr *eth;
294 unsigned char *rawp;
295 eth = eth_hdr(skb);
296
Kris Katterjohndbbc0982006-01-06 13:05:58 -0800297 if (is_multicast_ether_addr(eth->h_dest)) {
Kris Katterjohnd3f4a682006-01-09 16:01:43 -0800298 if (!compare_ether_addr(eth->h_dest, dev->broadcast))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 skb->pkt_type = PACKET_BROADCAST;
300 else
301 skb->pkt_type = PACKET_MULTICAST;
302 }
303
Kris Katterjohnd3f4a682006-01-09 16:01:43 -0800304 else if (compare_ether_addr(eth->h_dest, dev->dev_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 skb->pkt_type = PACKET_OTHERHOST;
306
307 if (ntohs(eth->h_proto) >= 1536)
308 return eth->h_proto;
309
310 rawp = skb->data;
311
312 /*
313 * This is a magic hack to spot IPX packets. Older Novell breaks
314 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
315 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
316 * won't work for fault tolerant netware but does for the rest.
317 */
318 if (*(unsigned short *) rawp == 0xFFFF)
319 return htons(ETH_P_802_3);
320
321 /*
322 * Real 802.2 LLC
323 */
324 return htons(ETH_P_802_2);
325}
326#endif /* FASTER_VERSION */
327
328/*
329 * We remember when the MAC gets set, so we don't override it later with
330 * the ESI of the ATM card of the first VC
331 */
332static int (*my_eth_mac_addr)(struct net_device *, void *);
333static int br2684_mac_addr(struct net_device *dev, void *p)
334{
335 int err = my_eth_mac_addr(dev, p);
336 if (!err)
337 BRPRIV(dev)->mac_was_set = 1;
338 return err;
339}
340
341#ifdef CONFIG_ATM_BR2684_IPFILTER
342/* this IOCTL is experimental. */
343static int br2684_setfilt(struct atm_vcc *atmvcc, void __user *arg)
344{
345 struct br2684_vcc *brvcc;
346 struct br2684_filter_set fs;
347
348 if (copy_from_user(&fs, arg, sizeof fs))
349 return -EFAULT;
350 if (fs.ifspec.method != BR2684_FIND_BYNOTHING) {
351 /*
352 * This is really a per-vcc thing, but we can also search
353 * by device
354 */
355 struct br2684_dev *brdev;
356 read_lock(&devs_lock);
357 brdev = BRPRIV(br2684_find_dev(&fs.ifspec));
358 if (brdev == NULL || list_empty(&brdev->brvccs) ||
359 brdev->brvccs.next != brdev->brvccs.prev) /* >1 VCC */
360 brvcc = NULL;
361 else
362 brvcc = list_entry_brvcc(brdev->brvccs.next);
363 read_unlock(&devs_lock);
364 if (brvcc == NULL)
365 return -ESRCH;
366 } else
367 brvcc = BR2684_VCC(atmvcc);
368 memcpy(&brvcc->filter, &fs.filter, sizeof(brvcc->filter));
369 return 0;
370}
371
372/* Returns 1 if packet should be dropped */
373static inline int
Al Viro30d492d2006-11-14 21:11:29 -0800374packet_fails_filter(__be16 type, struct br2684_vcc *brvcc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 if (brvcc->filter.netmask == 0)
377 return 0; /* no filter in place */
YOSHIFUJI Hideakiacde4852007-03-25 20:12:32 -0700378 if (type == htons(ETH_P_IP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 (((struct iphdr *) (skb->data))->daddr & brvcc->filter.
380 netmask) == brvcc->filter.prefix)
381 return 0;
YOSHIFUJI Hideakiacde4852007-03-25 20:12:32 -0700382 if (type == htons(ETH_P_ARP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return 0;
384 /* TODO: we should probably filter ARPs too.. don't want to have
385 * them returning values that don't make sense, or is that ok?
386 */
387 return 1; /* drop */
388}
389#endif /* CONFIG_ATM_BR2684_IPFILTER */
390
391static void br2684_close_vcc(struct br2684_vcc *brvcc)
392{
393 DPRINTK("removing VCC %p from dev %p\n", brvcc, brvcc->device);
394 write_lock_irq(&devs_lock);
395 list_del(&brvcc->brvccs);
396 write_unlock_irq(&devs_lock);
397 brvcc->atmvcc->user_back = NULL; /* what about vcc->recvq ??? */
398 brvcc->old_push(brvcc->atmvcc, NULL); /* pass on the bad news */
399 kfree(brvcc);
400 module_put(THIS_MODULE);
401}
402
403/* when AAL5 PDU comes in: */
404static void br2684_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
405{
406 struct br2684_vcc *brvcc = BR2684_VCC(atmvcc);
407 struct net_device *net_dev = brvcc->device;
408 struct br2684_dev *brdev = BRPRIV(net_dev);
409 int plen = sizeof(llc_oui_pid_pad) + ETH_HLEN;
410
411 DPRINTK("br2684_push\n");
412
413 if (unlikely(skb == NULL)) {
414 /* skb==NULL means VCC is being destroyed */
415 br2684_close_vcc(brvcc);
416 if (list_empty(&brdev->brvccs)) {
417 read_lock(&devs_lock);
418 list_del(&brdev->br2684_devs);
419 read_unlock(&devs_lock);
420 unregister_netdev(net_dev);
421 free_netdev(net_dev);
422 }
423 return;
424 }
425
426 skb_debug(skb);
427 atm_return(atmvcc, skb->truesize);
428 DPRINTK("skb from brdev %p\n", brdev);
429 if (brvcc->encaps == e_llc) {
430 /* let us waste some time for checking the encapsulation.
431 Note, that only 7 char is checked so frames with a valid FCS
432 are also accepted (but FCS is not checked of course) */
433 if (memcmp(skb->data, llc_oui_pid_pad, 7)) {
434 brdev->stats.rx_errors++;
435 dev_kfree_skb(skb);
436 return;
437 }
438
439 /* Strip FCS if present */
440 if (skb->len > 7 && skb->data[7] == 0x01)
441 __skb_trim(skb, skb->len - 4);
442 } else {
443 plen = PADLEN + ETH_HLEN; /* pad, dstmac,srcmac, ethtype */
444 /* first 2 chars should be 0 */
445 if (*((u16 *) (skb->data)) != 0) {
446 brdev->stats.rx_errors++;
447 dev_kfree_skb(skb);
448 return;
449 }
450 }
451 if (skb->len < plen) {
452 brdev->stats.rx_errors++;
453 dev_kfree_skb(skb); /* dev_ not needed? */
454 return;
455 }
456
457#ifdef FASTER_VERSION
458 /* FIXME: tcpdump shows that pointer to mac header is 2 bytes earlier,
459 than should be. What else should I set? */
460 skb_pull(skb, plen);
Arnaldo Carvalho de Melo48d49d0c2007-03-10 12:30:58 -0300461 skb_set_mac_header(skb, -ETH_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 skb->pkt_type = PACKET_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 skb->protocol = br_type_trans(skb, net_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464#else
465 skb_pull(skb, plen - ETH_HLEN);
466 skb->protocol = eth_type_trans(skb, net_dev);
467#endif /* FASTER_VERSION */
468#ifdef CONFIG_ATM_BR2684_IPFILTER
469 if (unlikely(packet_fails_filter(skb->protocol, brvcc, skb))) {
470 brdev->stats.rx_dropped++;
471 dev_kfree_skb(skb);
472 return;
473 }
474#endif /* CONFIG_ATM_BR2684_IPFILTER */
475 skb->dev = net_dev;
476 ATM_SKB(skb)->vcc = atmvcc; /* needed ? */
477 DPRINTK("received packet's protocol: %x\n", ntohs(skb->protocol));
478 skb_debug(skb);
479 if (unlikely(!(net_dev->flags & IFF_UP))) {
480 /* sigh, interface is down */
481 brdev->stats.rx_dropped++;
482 dev_kfree_skb(skb);
483 return;
484 }
485 brdev->stats.rx_packets++;
486 brdev->stats.rx_bytes += skb->len;
487 memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
488 netif_rx(skb);
489}
490
491static int br2684_regvcc(struct atm_vcc *atmvcc, void __user *arg)
492{
493/* assign a vcc to a dev
494Note: we do not have explicit unassign, but look at _push()
495*/
496 int err;
497 struct br2684_vcc *brvcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct sk_buff *skb;
David S. Millerc40a27f2006-11-30 21:05:23 -0800499 struct sk_buff_head *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 struct br2684_dev *brdev;
501 struct net_device *net_dev;
502 struct atm_backend_br2684 be;
David S. Millerc40a27f2006-11-30 21:05:23 -0800503 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 if (copy_from_user(&be, arg, sizeof be))
506 return -EFAULT;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700507 brvcc = kzalloc(sizeof(struct br2684_vcc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (!brvcc)
509 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 write_lock_irq(&devs_lock);
511 net_dev = br2684_find_dev(&be.ifspec);
512 if (net_dev == NULL) {
513 printk(KERN_ERR
514 "br2684: tried to attach to non-existant device\n");
515 err = -ENXIO;
516 goto error;
517 }
518 brdev = BRPRIV(net_dev);
519 if (atmvcc->push == NULL) {
520 err = -EBADFD;
521 goto error;
522 }
523 if (!list_empty(&brdev->brvccs)) {
524 /* Only 1 VCC/dev right now */
525 err = -EEXIST;
526 goto error;
527 }
528 if (be.fcs_in != BR2684_FCSIN_NO || be.fcs_out != BR2684_FCSOUT_NO ||
529 be.fcs_auto || be.has_vpiid || be.send_padding || (be.encaps !=
530 BR2684_ENCAPS_VC && be.encaps != BR2684_ENCAPS_LLC) ||
531 be.min_size != 0) {
532 err = -EINVAL;
533 goto error;
534 }
535 DPRINTK("br2684_regvcc vcc=%p, encaps=%d, brvcc=%p\n", atmvcc, be.encaps,
536 brvcc);
537 if (list_empty(&brdev->brvccs) && !brdev->mac_was_set) {
538 unsigned char *esi = atmvcc->dev->esi;
539 if (esi[0] | esi[1] | esi[2] | esi[3] | esi[4] | esi[5])
540 memcpy(net_dev->dev_addr, esi, net_dev->addr_len);
541 else
542 net_dev->dev_addr[2] = 1;
543 }
544 list_add(&brvcc->brvccs, &brdev->brvccs);
545 write_unlock_irq(&devs_lock);
546 brvcc->device = net_dev;
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;
David S. Millerc40a27f2006-11-30 21:05:23 -0800553
554 rq = &sk_atm(atmvcc)->sk_receive_queue;
555
556 spin_lock_irqsave(&rq->lock, flags);
557 if (skb_queue_empty(rq)) {
558 skb = NULL;
559 } else {
560 /* NULL terminate the list. */
561 rq->prev->next = NULL;
562 skb = rq->next;
563 }
564 rq->prev = rq->next = (struct sk_buff *)rq;
565 rq->qlen = 0;
566 spin_unlock_irqrestore(&rq->lock, flags);
567
568 while (skb) {
569 struct sk_buff *next = skb->next;
570
571 skb->next = skb->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 BRPRIV(skb->dev)->stats.rx_bytes -= skb->len;
573 BRPRIV(skb->dev)->stats.rx_packets--;
574 br2684_push(atmvcc, skb);
David S. Millerc40a27f2006-11-30 21:05:23 -0800575
576 skb = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578 __module_get(THIS_MODULE);
579 return 0;
580 error:
581 write_unlock_irq(&devs_lock);
582 kfree(brvcc);
583 return err;
584}
585
586static void br2684_setup(struct net_device *netdev)
587{
588 struct br2684_dev *brdev = BRPRIV(netdev);
589
590 ether_setup(netdev);
591 brdev->net_dev = netdev;
592
593#ifdef FASTER_VERSION
594 my_eth_header = netdev->hard_header;
595 netdev->hard_header = br2684_header;
596 my_eth_header_cache = netdev->hard_header_cache;
597 netdev->hard_header_cache = br2684_header_cache;
598 netdev->hard_header_len = sizeof(llc_oui_pid_pad) + ETH_HLEN; /* 10 + 14 */
599#endif
600 my_eth_mac_addr = netdev->set_mac_address;
601 netdev->set_mac_address = br2684_mac_addr;
602 netdev->hard_start_xmit = br2684_start_xmit;
603 netdev->get_stats = br2684_get_stats;
604
605 INIT_LIST_HEAD(&brdev->brvccs);
606}
607
608static int br2684_create(void __user *arg)
609{
610 int err;
611 struct net_device *netdev;
612 struct br2684_dev *brdev;
613 struct atm_newif_br2684 ni;
614
615 DPRINTK("br2684_create\n");
616
617 if (copy_from_user(&ni, arg, sizeof ni)) {
618 return -EFAULT;
619 }
620 if (ni.media != BR2684_MEDIA_ETHERNET || ni.mtu != 1500) {
621 return -EINVAL;
622 }
623
624 netdev = alloc_netdev(sizeof(struct br2684_dev),
625 ni.ifname[0] ? ni.ifname : "nas%d",
626 br2684_setup);
627 if (!netdev)
628 return -ENOMEM;
629
630 brdev = BRPRIV(netdev);
631
632 DPRINTK("registered netdev %s\n", netdev->name);
633 /* open, stop, do_ioctl ? */
634 err = register_netdev(netdev);
635 if (err < 0) {
636 printk(KERN_ERR "br2684_create: register_netdev failed\n");
637 free_netdev(netdev);
638 return err;
639 }
640
641 write_lock_irq(&devs_lock);
642 brdev->number = list_empty(&br2684_devs) ? 1 :
643 BRPRIV(list_entry_brdev(br2684_devs.prev))->number + 1;
644 list_add_tail(&brdev->br2684_devs, &br2684_devs);
645 write_unlock_irq(&devs_lock);
646 return 0;
647}
648
649/*
650 * This handles ioctls actually performed on our vcc - we must return
651 * -ENOIOCTLCMD for any unrecognized ioctl
652 */
653static int br2684_ioctl(struct socket *sock, unsigned int cmd,
654 unsigned long arg)
655{
656 struct atm_vcc *atmvcc = ATM_SD(sock);
657 void __user *argp = (void __user *)arg;
658
659 int err;
660 switch(cmd) {
661 case ATM_SETBACKEND:
662 case ATM_NEWBACKENDIF: {
663 atm_backend_t b;
664 err = get_user(b, (atm_backend_t __user *) argp);
665 if (err)
666 return -EFAULT;
667 if (b != ATM_BACKEND_BR2684)
668 return -ENOIOCTLCMD;
669 if (!capable(CAP_NET_ADMIN))
670 return -EPERM;
671 if (cmd == ATM_SETBACKEND)
672 return br2684_regvcc(atmvcc, argp);
673 else
674 return br2684_create(argp);
675 }
676#ifdef CONFIG_ATM_BR2684_IPFILTER
677 case BR2684_SETFILT:
678 if (atmvcc->push != br2684_push)
679 return -ENOIOCTLCMD;
680 if (!capable(CAP_NET_ADMIN))
681 return -EPERM;
682 err = br2684_setfilt(atmvcc, argp);
683 return err;
684#endif /* CONFIG_ATM_BR2684_IPFILTER */
685 }
686 return -ENOIOCTLCMD;
687}
688
689static struct atm_ioctl br2684_ioctl_ops = {
690 .owner = THIS_MODULE,
691 .ioctl = br2684_ioctl,
692};
693
694
695#ifdef CONFIG_PROC_FS
696static void *br2684_seq_start(struct seq_file *seq, loff_t *pos)
697{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 read_lock(&devs_lock);
Pavel Emelianov9af97182007-07-09 13:12:24 -0700699 return seq_list_start(&br2684_devs, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702static void *br2684_seq_next(struct seq_file *seq, void *v, loff_t *pos)
703{
Pavel Emelianov9af97182007-07-09 13:12:24 -0700704 return seq_list_next(v, &br2684_devs, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707static void br2684_seq_stop(struct seq_file *seq, void *v)
708{
709 read_unlock(&devs_lock);
710}
711
712static int br2684_seq_show(struct seq_file *seq, void *v)
713{
Pavel Emelianov9af97182007-07-09 13:12:24 -0700714 const struct br2684_dev *brdev = list_entry(v, struct br2684_dev,
715 br2684_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 const struct net_device *net_dev = brdev->net_dev;
717 const struct br2684_vcc *brvcc;
718
719 seq_printf(seq, "dev %.16s: num=%d, mac=%02X:%02X:"
720 "%02X:%02X:%02X:%02X (%s)\n", net_dev->name,
721 brdev->number,
722 net_dev->dev_addr[0],
723 net_dev->dev_addr[1],
724 net_dev->dev_addr[2],
725 net_dev->dev_addr[3],
726 net_dev->dev_addr[4],
727 net_dev->dev_addr[5],
728 brdev->mac_was_set ? "set" : "auto");
729
730 list_for_each_entry(brvcc, &brdev->brvccs, brvccs) {
731 seq_printf(seq, " vcc %d.%d.%d: encaps=%s"
732#ifndef FASTER_VERSION
733 ", failed copies %u/%u"
734#endif /* FASTER_VERSION */
735 "\n", brvcc->atmvcc->dev->number,
736 brvcc->atmvcc->vpi, brvcc->atmvcc->vci,
737 (brvcc->encaps == e_llc) ? "LLC" : "VC"
738#ifndef FASTER_VERSION
739 , brvcc->copies_failed
740 , brvcc->copies_needed
741#endif /* FASTER_VERSION */
742 );
743#ifdef CONFIG_ATM_BR2684_IPFILTER
744#define b1(var, byte) ((u8 *) &brvcc->filter.var)[byte]
745#define bs(var) b1(var, 0), b1(var, 1), b1(var, 2), b1(var, 3)
746 if (brvcc->filter.netmask != 0)
747 seq_printf(seq, " filter=%d.%d.%d.%d/"
748 "%d.%d.%d.%d\n",
749 bs(prefix), bs(netmask));
750#undef bs
751#undef b1
752#endif /* CONFIG_ATM_BR2684_IPFILTER */
753 }
754 return 0;
755}
756
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700757static const struct seq_operations br2684_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 .start = br2684_seq_start,
759 .next = br2684_seq_next,
760 .stop = br2684_seq_stop,
761 .show = br2684_seq_show,
762};
763
764static int br2684_proc_open(struct inode *inode, struct file *file)
765{
766 return seq_open(file, &br2684_seq_ops);
767}
768
Arjan van de Ven9a321442007-02-12 00:55:35 -0800769static const struct file_operations br2684_proc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 .owner = THIS_MODULE,
771 .open = br2684_proc_open,
772 .read = seq_read,
773 .llseek = seq_lseek,
774 .release = seq_release,
775};
776
777extern struct proc_dir_entry *atm_proc_root; /* from proc.c */
778#endif
779
780static int __init br2684_init(void)
781{
782#ifdef CONFIG_PROC_FS
783 struct proc_dir_entry *p;
784 if ((p = create_proc_entry("br2684", 0, atm_proc_root)) == NULL)
785 return -ENOMEM;
786 p->proc_fops = &br2684_proc_ops;
787#endif
788 register_atm_ioctl(&br2684_ioctl_ops);
789 return 0;
790}
791
792static void __exit br2684_exit(void)
793{
794 struct net_device *net_dev;
795 struct br2684_dev *brdev;
796 struct br2684_vcc *brvcc;
797 deregister_atm_ioctl(&br2684_ioctl_ops);
798
799#ifdef CONFIG_PROC_FS
800 remove_proc_entry("br2684", atm_proc_root);
801#endif
802
803 while (!list_empty(&br2684_devs)) {
804 net_dev = list_entry_brdev(br2684_devs.next);
805 brdev = BRPRIV(net_dev);
806 while (!list_empty(&brdev->brvccs)) {
807 brvcc = list_entry_brvcc(brdev->brvccs.next);
808 br2684_close_vcc(brvcc);
809 }
810
811 list_del(&brdev->br2684_devs);
812 unregister_netdev(net_dev);
813 free_netdev(net_dev);
814 }
815}
816
817module_init(br2684_init);
818module_exit(br2684_exit);
819
820MODULE_AUTHOR("Marcell GAL");
821MODULE_DESCRIPTION("RFC2684 bridged protocols over ATM/AAL5");
822MODULE_LICENSE("GPL");