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