blob: 0e9f00c5c89942cfccbd5f76afa84e82f7afc4cc [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;
463#ifdef CONFIG_BR2684_FAST_TRANS
464 skb->protocol = ((u16 *) skb->data)[-1];
465#else /* some protocols might require this: */
466 skb->protocol = br_type_trans(skb, net_dev);
467#endif /* CONFIG_BR2684_FAST_TRANS */
468#else
469 skb_pull(skb, plen - ETH_HLEN);
470 skb->protocol = eth_type_trans(skb, net_dev);
471#endif /* FASTER_VERSION */
472#ifdef CONFIG_ATM_BR2684_IPFILTER
473 if (unlikely(packet_fails_filter(skb->protocol, brvcc, skb))) {
474 brdev->stats.rx_dropped++;
475 dev_kfree_skb(skb);
476 return;
477 }
478#endif /* CONFIG_ATM_BR2684_IPFILTER */
479 skb->dev = net_dev;
480 ATM_SKB(skb)->vcc = atmvcc; /* needed ? */
481 DPRINTK("received packet's protocol: %x\n", ntohs(skb->protocol));
482 skb_debug(skb);
483 if (unlikely(!(net_dev->flags & IFF_UP))) {
484 /* sigh, interface is down */
485 brdev->stats.rx_dropped++;
486 dev_kfree_skb(skb);
487 return;
488 }
489 brdev->stats.rx_packets++;
490 brdev->stats.rx_bytes += skb->len;
491 memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
492 netif_rx(skb);
493}
494
495static int br2684_regvcc(struct atm_vcc *atmvcc, void __user *arg)
496{
497/* assign a vcc to a dev
498Note: we do not have explicit unassign, but look at _push()
499*/
500 int err;
501 struct br2684_vcc *brvcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct sk_buff *skb;
David S. Millerc40a27f2006-11-30 21:05:23 -0800503 struct sk_buff_head *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct br2684_dev *brdev;
505 struct net_device *net_dev;
506 struct atm_backend_br2684 be;
David S. Millerc40a27f2006-11-30 21:05:23 -0800507 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (copy_from_user(&be, arg, sizeof be))
510 return -EFAULT;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700511 brvcc = kzalloc(sizeof(struct br2684_vcc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (!brvcc)
513 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 write_lock_irq(&devs_lock);
515 net_dev = br2684_find_dev(&be.ifspec);
516 if (net_dev == NULL) {
517 printk(KERN_ERR
518 "br2684: tried to attach to non-existant device\n");
519 err = -ENXIO;
520 goto error;
521 }
522 brdev = BRPRIV(net_dev);
523 if (atmvcc->push == NULL) {
524 err = -EBADFD;
525 goto error;
526 }
527 if (!list_empty(&brdev->brvccs)) {
528 /* Only 1 VCC/dev right now */
529 err = -EEXIST;
530 goto error;
531 }
532 if (be.fcs_in != BR2684_FCSIN_NO || be.fcs_out != BR2684_FCSOUT_NO ||
533 be.fcs_auto || be.has_vpiid || be.send_padding || (be.encaps !=
534 BR2684_ENCAPS_VC && be.encaps != BR2684_ENCAPS_LLC) ||
535 be.min_size != 0) {
536 err = -EINVAL;
537 goto error;
538 }
539 DPRINTK("br2684_regvcc vcc=%p, encaps=%d, brvcc=%p\n", atmvcc, be.encaps,
540 brvcc);
541 if (list_empty(&brdev->brvccs) && !brdev->mac_was_set) {
542 unsigned char *esi = atmvcc->dev->esi;
543 if (esi[0] | esi[1] | esi[2] | esi[3] | esi[4] | esi[5])
544 memcpy(net_dev->dev_addr, esi, net_dev->addr_len);
545 else
546 net_dev->dev_addr[2] = 1;
547 }
548 list_add(&brvcc->brvccs, &brdev->brvccs);
549 write_unlock_irq(&devs_lock);
550 brvcc->device = net_dev;
551 brvcc->atmvcc = atmvcc;
552 atmvcc->user_back = brvcc;
553 brvcc->encaps = (enum br2684_encaps) be.encaps;
554 brvcc->old_push = atmvcc->push;
555 barrier();
556 atmvcc->push = br2684_push;
David S. Millerc40a27f2006-11-30 21:05:23 -0800557
558 rq = &sk_atm(atmvcc)->sk_receive_queue;
559
560 spin_lock_irqsave(&rq->lock, flags);
561 if (skb_queue_empty(rq)) {
562 skb = NULL;
563 } else {
564 /* NULL terminate the list. */
565 rq->prev->next = NULL;
566 skb = rq->next;
567 }
568 rq->prev = rq->next = (struct sk_buff *)rq;
569 rq->qlen = 0;
570 spin_unlock_irqrestore(&rq->lock, flags);
571
572 while (skb) {
573 struct sk_buff *next = skb->next;
574
575 skb->next = skb->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 BRPRIV(skb->dev)->stats.rx_bytes -= skb->len;
577 BRPRIV(skb->dev)->stats.rx_packets--;
578 br2684_push(atmvcc, skb);
David S. Millerc40a27f2006-11-30 21:05:23 -0800579
580 skb = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582 __module_get(THIS_MODULE);
583 return 0;
584 error:
585 write_unlock_irq(&devs_lock);
586 kfree(brvcc);
587 return err;
588}
589
590static void br2684_setup(struct net_device *netdev)
591{
592 struct br2684_dev *brdev = BRPRIV(netdev);
593
594 ether_setup(netdev);
595 brdev->net_dev = netdev;
596
597#ifdef FASTER_VERSION
598 my_eth_header = netdev->hard_header;
599 netdev->hard_header = br2684_header;
600 my_eth_header_cache = netdev->hard_header_cache;
601 netdev->hard_header_cache = br2684_header_cache;
602 netdev->hard_header_len = sizeof(llc_oui_pid_pad) + ETH_HLEN; /* 10 + 14 */
603#endif
604 my_eth_mac_addr = netdev->set_mac_address;
605 netdev->set_mac_address = br2684_mac_addr;
606 netdev->hard_start_xmit = br2684_start_xmit;
607 netdev->get_stats = br2684_get_stats;
608
609 INIT_LIST_HEAD(&brdev->brvccs);
610}
611
612static int br2684_create(void __user *arg)
613{
614 int err;
615 struct net_device *netdev;
616 struct br2684_dev *brdev;
617 struct atm_newif_br2684 ni;
618
619 DPRINTK("br2684_create\n");
620
621 if (copy_from_user(&ni, arg, sizeof ni)) {
622 return -EFAULT;
623 }
624 if (ni.media != BR2684_MEDIA_ETHERNET || ni.mtu != 1500) {
625 return -EINVAL;
626 }
627
628 netdev = alloc_netdev(sizeof(struct br2684_dev),
629 ni.ifname[0] ? ni.ifname : "nas%d",
630 br2684_setup);
631 if (!netdev)
632 return -ENOMEM;
633
634 brdev = BRPRIV(netdev);
635
636 DPRINTK("registered netdev %s\n", netdev->name);
637 /* open, stop, do_ioctl ? */
638 err = register_netdev(netdev);
639 if (err < 0) {
640 printk(KERN_ERR "br2684_create: register_netdev failed\n");
641 free_netdev(netdev);
642 return err;
643 }
644
645 write_lock_irq(&devs_lock);
646 brdev->number = list_empty(&br2684_devs) ? 1 :
647 BRPRIV(list_entry_brdev(br2684_devs.prev))->number + 1;
648 list_add_tail(&brdev->br2684_devs, &br2684_devs);
649 write_unlock_irq(&devs_lock);
650 return 0;
651}
652
653/*
654 * This handles ioctls actually performed on our vcc - we must return
655 * -ENOIOCTLCMD for any unrecognized ioctl
656 */
657static int br2684_ioctl(struct socket *sock, unsigned int cmd,
658 unsigned long arg)
659{
660 struct atm_vcc *atmvcc = ATM_SD(sock);
661 void __user *argp = (void __user *)arg;
662
663 int err;
664 switch(cmd) {
665 case ATM_SETBACKEND:
666 case ATM_NEWBACKENDIF: {
667 atm_backend_t b;
668 err = get_user(b, (atm_backend_t __user *) argp);
669 if (err)
670 return -EFAULT;
671 if (b != ATM_BACKEND_BR2684)
672 return -ENOIOCTLCMD;
673 if (!capable(CAP_NET_ADMIN))
674 return -EPERM;
675 if (cmd == ATM_SETBACKEND)
676 return br2684_regvcc(atmvcc, argp);
677 else
678 return br2684_create(argp);
679 }
680#ifdef CONFIG_ATM_BR2684_IPFILTER
681 case BR2684_SETFILT:
682 if (atmvcc->push != br2684_push)
683 return -ENOIOCTLCMD;
684 if (!capable(CAP_NET_ADMIN))
685 return -EPERM;
686 err = br2684_setfilt(atmvcc, argp);
687 return err;
688#endif /* CONFIG_ATM_BR2684_IPFILTER */
689 }
690 return -ENOIOCTLCMD;
691}
692
693static struct atm_ioctl br2684_ioctl_ops = {
694 .owner = THIS_MODULE,
695 .ioctl = br2684_ioctl,
696};
697
698
699#ifdef CONFIG_PROC_FS
700static void *br2684_seq_start(struct seq_file *seq, loff_t *pos)
701{
702 loff_t offs = 0;
703 struct br2684_dev *brd;
704
705 read_lock(&devs_lock);
706
707 list_for_each_entry(brd, &br2684_devs, br2684_devs) {
708 if (offs == *pos)
709 return brd;
710 ++offs;
711 }
712 return NULL;
713}
714
715static void *br2684_seq_next(struct seq_file *seq, void *v, loff_t *pos)
716{
717 struct br2684_dev *brd = v;
718
719 ++*pos;
720
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900721 brd = list_entry(brd->br2684_devs.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct br2684_dev, br2684_devs);
723 return (&brd->br2684_devs != &br2684_devs) ? brd : NULL;
724}
725
726static void br2684_seq_stop(struct seq_file *seq, void *v)
727{
728 read_unlock(&devs_lock);
729}
730
731static int br2684_seq_show(struct seq_file *seq, void *v)
732{
733 const struct br2684_dev *brdev = v;
734 const struct net_device *net_dev = brdev->net_dev;
735 const struct br2684_vcc *brvcc;
736
737 seq_printf(seq, "dev %.16s: num=%d, mac=%02X:%02X:"
738 "%02X:%02X:%02X:%02X (%s)\n", net_dev->name,
739 brdev->number,
740 net_dev->dev_addr[0],
741 net_dev->dev_addr[1],
742 net_dev->dev_addr[2],
743 net_dev->dev_addr[3],
744 net_dev->dev_addr[4],
745 net_dev->dev_addr[5],
746 brdev->mac_was_set ? "set" : "auto");
747
748 list_for_each_entry(brvcc, &brdev->brvccs, brvccs) {
749 seq_printf(seq, " vcc %d.%d.%d: encaps=%s"
750#ifndef FASTER_VERSION
751 ", failed copies %u/%u"
752#endif /* FASTER_VERSION */
753 "\n", brvcc->atmvcc->dev->number,
754 brvcc->atmvcc->vpi, brvcc->atmvcc->vci,
755 (brvcc->encaps == e_llc) ? "LLC" : "VC"
756#ifndef FASTER_VERSION
757 , brvcc->copies_failed
758 , brvcc->copies_needed
759#endif /* FASTER_VERSION */
760 );
761#ifdef CONFIG_ATM_BR2684_IPFILTER
762#define b1(var, byte) ((u8 *) &brvcc->filter.var)[byte]
763#define bs(var) b1(var, 0), b1(var, 1), b1(var, 2), b1(var, 3)
764 if (brvcc->filter.netmask != 0)
765 seq_printf(seq, " filter=%d.%d.%d.%d/"
766 "%d.%d.%d.%d\n",
767 bs(prefix), bs(netmask));
768#undef bs
769#undef b1
770#endif /* CONFIG_ATM_BR2684_IPFILTER */
771 }
772 return 0;
773}
774
775static struct seq_operations br2684_seq_ops = {
776 .start = br2684_seq_start,
777 .next = br2684_seq_next,
778 .stop = br2684_seq_stop,
779 .show = br2684_seq_show,
780};
781
782static int br2684_proc_open(struct inode *inode, struct file *file)
783{
784 return seq_open(file, &br2684_seq_ops);
785}
786
Arjan van de Ven9a321442007-02-12 00:55:35 -0800787static const struct file_operations br2684_proc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 .owner = THIS_MODULE,
789 .open = br2684_proc_open,
790 .read = seq_read,
791 .llseek = seq_lseek,
792 .release = seq_release,
793};
794
795extern struct proc_dir_entry *atm_proc_root; /* from proc.c */
796#endif
797
798static int __init br2684_init(void)
799{
800#ifdef CONFIG_PROC_FS
801 struct proc_dir_entry *p;
802 if ((p = create_proc_entry("br2684", 0, atm_proc_root)) == NULL)
803 return -ENOMEM;
804 p->proc_fops = &br2684_proc_ops;
805#endif
806 register_atm_ioctl(&br2684_ioctl_ops);
807 return 0;
808}
809
810static void __exit br2684_exit(void)
811{
812 struct net_device *net_dev;
813 struct br2684_dev *brdev;
814 struct br2684_vcc *brvcc;
815 deregister_atm_ioctl(&br2684_ioctl_ops);
816
817#ifdef CONFIG_PROC_FS
818 remove_proc_entry("br2684", atm_proc_root);
819#endif
820
821 while (!list_empty(&br2684_devs)) {
822 net_dev = list_entry_brdev(br2684_devs.next);
823 brdev = BRPRIV(net_dev);
824 while (!list_empty(&brdev->brvccs)) {
825 brvcc = list_entry_brvcc(brdev->brvccs.next);
826 br2684_close_vcc(brvcc);
827 }
828
829 list_del(&brdev->br2684_devs);
830 unregister_netdev(net_dev);
831 free_netdev(net_dev);
832 }
833}
834
835module_init(br2684_init);
836module_exit(br2684_exit);
837
838MODULE_AUTHOR("Marcell GAL");
839MODULE_DESCRIPTION("RFC2684 bridged protocols over ATM/AAL5");
840MODULE_LICENSE("GPL");