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