2 * Ethernet netdevice using ATM AAL5 as underlying carrier
3 * (RFC1483 obsoleted by RFC2684) for Linux
5 * Authors: Marcell GAL, 2000, XDSL Ltd, Hungary
6 * Eric Kinzie, 2006-2007, US Naval Research Laboratory
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/netdevice.h>
14 #include <linux/skbuff.h>
15 #include <linux/etherdevice.h>
16 #include <linux/rtnetlink.h>
18 #include <asm/uaccess.h>
20 #include <linux/atm.h>
21 #include <linux/atmdev.h>
22 #include <linux/capability.h>
23 #include <linux/seq_file.h>
25 #include <linux/atmbr2684.h>
30 static void skb_debug(const struct sk_buff *skb)
33 char buf[NUM2PRINT * 3 + 1]; /* 3 chars per byte */
35 for (i = 0; i < skb->len && i < NUM2PRINT; i++) {
36 sprintf(buf + i * 3, "%2.2x ", 0xff & skb->data[i]);
38 printk(KERN_DEBUG "br2684: skb: %s\n", buf);
41 #define skb_debug(skb) do {} while (0)
44 #define BR2684_ETHERTYPE_LEN 2
45 #define BR2684_PAD_LEN 2
47 #define LLC 0xaa, 0xaa, 0x03
48 #define SNAP_BRIDGED 0x00, 0x80, 0xc2
49 #define SNAP_ROUTED 0x00, 0x00, 0x00
50 #define PID_ETHERNET 0x00, 0x07
51 #define ETHERTYPE_IPV4 0x08, 0x00
52 #define ETHERTYPE_IPV6 0x86, 0xdd
53 #define PAD_BRIDGED 0x00, 0x00
55 static const unsigned char ethertype_ipv4[] = { ETHERTYPE_IPV4 };
56 static const unsigned char ethertype_ipv6[] = { ETHERTYPE_IPV6 };
57 static const unsigned char llc_oui_pid_pad[] =
58 { LLC, SNAP_BRIDGED, PID_ETHERNET, PAD_BRIDGED };
59 static const unsigned char llc_oui_ipv4[] = { LLC, SNAP_ROUTED, ETHERTYPE_IPV4 };
60 static const unsigned char llc_oui_ipv6[] = { LLC, SNAP_ROUTED, ETHERTYPE_IPV6 };
63 e_vc = BR2684_ENCAPS_VC,
64 e_llc = BR2684_ENCAPS_LLC,
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 unsigned copies_needed, copies_failed;
82 struct net_device *net_dev;
83 struct list_head br2684_devs;
85 struct list_head brvccs; /* one device <=> one vcc (before xmas) */
87 enum br2684_payload payload;
91 * This lock should be held for writing any time the list of devices or
92 * their attached vcc's could be altered. It should be held for reading
93 * any time these are being queried. Note that we sometimes need to
94 * do read-locking under interrupt context, so write locking must block
95 * the current CPU's interrupts
97 static DEFINE_RWLOCK(devs_lock);
99 static LIST_HEAD(br2684_devs);
101 static inline struct br2684_dev *BRPRIV(const struct net_device *net_dev)
103 return (struct br2684_dev *)netdev_priv(net_dev);
106 static inline struct net_device *list_entry_brdev(const struct list_head *le)
108 return list_entry(le, struct br2684_dev, br2684_devs)->net_dev;
111 static inline struct br2684_vcc *BR2684_VCC(const struct atm_vcc *atmvcc)
113 return (struct br2684_vcc *)(atmvcc->user_back);
116 static inline struct br2684_vcc *list_entry_brvcc(const struct list_head *le)
118 return list_entry(le, struct br2684_vcc, brvccs);
121 /* Caller should hold read_lock(&devs_lock) */
122 static struct net_device *br2684_find_dev(const struct br2684_if_spec *s)
124 struct list_head *lh;
125 struct net_device *net_dev;
127 case BR2684_FIND_BYNUM:
128 list_for_each(lh, &br2684_devs) {
129 net_dev = list_entry_brdev(lh);
130 if (BRPRIV(net_dev)->number == s->spec.devnum)
134 case BR2684_FIND_BYIFNAME:
135 list_for_each(lh, &br2684_devs) {
136 net_dev = list_entry_brdev(lh);
137 if (!strncmp(net_dev->name, s->spec.ifname, IFNAMSIZ))
146 * Send a packet out a particular vcc. Not to useful right now, but paves
147 * the way for multiple vcc's per itf. Returns true if we can send,
150 static int br2684_xmit_vcc(struct sk_buff *skb, struct net_device *dev,
151 struct br2684_vcc *brvcc)
153 struct br2684_dev *brdev = BRPRIV(dev);
154 struct atm_vcc *atmvcc;
155 int minheadroom = (brvcc->encaps == e_llc) ? 10 : 2;
157 if (skb_headroom(skb) < minheadroom) {
158 struct sk_buff *skb2 = skb_realloc_headroom(skb, minheadroom);
159 brvcc->copies_needed++;
162 brvcc->copies_failed++;
168 if (brvcc->encaps == e_llc) {
169 if (brdev->payload == p_bridged) {
170 skb_push(skb, sizeof(llc_oui_pid_pad));
171 skb_copy_to_linear_data(skb, llc_oui_pid_pad,
172 sizeof(llc_oui_pid_pad));
173 } else if (brdev->payload == p_routed) {
174 unsigned short prot = ntohs(skb->protocol);
176 skb_push(skb, sizeof(llc_oui_ipv4));
179 skb_copy_to_linear_data(skb, llc_oui_ipv4,
180 sizeof(llc_oui_ipv4));
183 skb_copy_to_linear_data(skb, llc_oui_ipv6,
184 sizeof(llc_oui_ipv6));
192 if (brdev->payload == p_bridged) {
194 memset(skb->data, 0, 2);
195 } else { /* p_routed */
196 skb_pull(skb, ETH_HLEN);
201 ATM_SKB(skb)->vcc = atmvcc = brvcc->atmvcc;
202 pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, atmvcc, atmvcc->dev);
203 if (!atm_may_send(atmvcc, skb->truesize)) {
205 * We free this here for now, because we cannot know in a higher
206 * layer whether the skb pointer it supplied wasn't freed yet.
212 atomic_add(skb->truesize, &sk_atm(atmvcc)->sk_wmem_alloc);
213 ATM_SKB(skb)->atm_options = atmvcc->atm_options;
214 dev->stats.tx_packets++;
215 dev->stats.tx_bytes += skb->len;
216 atmvcc->send(atmvcc, skb);
220 static inline struct br2684_vcc *pick_outgoing_vcc(const struct sk_buff *skb,
221 const struct br2684_dev *brdev)
223 return list_empty(&brdev->brvccs) ? NULL : list_entry_brvcc(brdev->brvccs.next); /* 1 vcc/dev right now */
226 static int br2684_start_xmit(struct sk_buff *skb, struct net_device *dev)
228 struct br2684_dev *brdev = BRPRIV(dev);
229 struct br2684_vcc *brvcc;
231 pr_debug("br2684_start_xmit, skb->dst=%p\n", skb->dst);
232 read_lock(&devs_lock);
233 brvcc = pick_outgoing_vcc(skb, brdev);
235 pr_debug("no vcc attached to dev %s\n", dev->name);
236 dev->stats.tx_errors++;
237 dev->stats.tx_carrier_errors++;
238 /* netif_stop_queue(dev); */
240 read_unlock(&devs_lock);
243 if (!br2684_xmit_vcc(skb, dev, brvcc)) {
245 * We should probably use netif_*_queue() here, but that
246 * involves added complication. We need to walk before
249 * Don't free here! this pointer might be no longer valid!
251 dev->stats.tx_errors++;
252 dev->stats.tx_fifo_errors++;
254 read_unlock(&devs_lock);
259 * We remember when the MAC gets set, so we don't override it later with
260 * the ESI of the ATM card of the first VC
262 static int br2684_mac_addr(struct net_device *dev, void *p)
264 int err = eth_mac_addr(dev, p);
266 BRPRIV(dev)->mac_was_set = 1;
270 #ifdef CONFIG_ATM_BR2684_IPFILTER
271 /* this IOCTL is experimental. */
272 static int br2684_setfilt(struct atm_vcc *atmvcc, void __user * arg)
274 struct br2684_vcc *brvcc;
275 struct br2684_filter_set fs;
277 if (copy_from_user(&fs, arg, sizeof fs))
279 if (fs.ifspec.method != BR2684_FIND_BYNOTHING) {
281 * This is really a per-vcc thing, but we can also search
284 struct br2684_dev *brdev;
285 read_lock(&devs_lock);
286 brdev = BRPRIV(br2684_find_dev(&fs.ifspec));
287 if (brdev == NULL || list_empty(&brdev->brvccs) || brdev->brvccs.next != brdev->brvccs.prev) /* >1 VCC */
290 brvcc = list_entry_brvcc(brdev->brvccs.next);
291 read_unlock(&devs_lock);
295 brvcc = BR2684_VCC(atmvcc);
296 memcpy(&brvcc->filter, &fs.filter, sizeof(brvcc->filter));
300 /* Returns 1 if packet should be dropped */
302 packet_fails_filter(__be16 type, struct br2684_vcc *brvcc, struct sk_buff *skb)
304 if (brvcc->filter.netmask == 0)
305 return 0; /* no filter in place */
306 if (type == htons(ETH_P_IP) &&
307 (((struct iphdr *)(skb->data))->daddr & brvcc->filter.
308 netmask) == brvcc->filter.prefix)
310 if (type == htons(ETH_P_ARP))
313 * TODO: we should probably filter ARPs too.. don't want to have
314 * them returning values that don't make sense, or is that ok?
318 #endif /* CONFIG_ATM_BR2684_IPFILTER */
320 static void br2684_close_vcc(struct br2684_vcc *brvcc)
322 pr_debug("removing VCC %p from dev %p\n", brvcc, brvcc->device);
323 write_lock_irq(&devs_lock);
324 list_del(&brvcc->brvccs);
325 write_unlock_irq(&devs_lock);
326 brvcc->atmvcc->user_back = NULL; /* what about vcc->recvq ??? */
327 brvcc->old_push(brvcc->atmvcc, NULL); /* pass on the bad news */
329 module_put(THIS_MODULE);
332 /* when AAL5 PDU comes in: */
333 static void br2684_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
335 struct br2684_vcc *brvcc = BR2684_VCC(atmvcc);
336 struct net_device *net_dev = brvcc->device;
337 struct br2684_dev *brdev = BRPRIV(net_dev);
339 pr_debug("br2684_push\n");
341 if (unlikely(skb == NULL)) {
342 /* skb==NULL means VCC is being destroyed */
343 br2684_close_vcc(brvcc);
344 if (list_empty(&brdev->brvccs)) {
345 write_lock_irq(&devs_lock);
346 list_del(&brdev->br2684_devs);
347 write_unlock_irq(&devs_lock);
348 unregister_netdev(net_dev);
349 free_netdev(net_dev);
355 atm_return(atmvcc, skb->truesize);
356 pr_debug("skb from brdev %p\n", brdev);
357 if (brvcc->encaps == e_llc) {
359 if (skb->len > 7 && skb->data[7] == 0x01)
360 __skb_trim(skb, skb->len - 4);
362 /* accept packets that have "ipv[46]" in the snap header */
363 if ((skb->len >= (sizeof(llc_oui_ipv4)))
366 (skb->data, llc_oui_ipv4,
367 sizeof(llc_oui_ipv4) - BR2684_ETHERTYPE_LEN) == 0)) {
369 (skb->data + 6, ethertype_ipv6,
370 sizeof(ethertype_ipv6)) == 0)
371 skb->protocol = htons(ETH_P_IPV6);
373 (skb->data + 6, ethertype_ipv4,
374 sizeof(ethertype_ipv4)) == 0)
375 skb->protocol = htons(ETH_P_IP);
378 skb_pull(skb, sizeof(llc_oui_ipv4));
379 skb_reset_network_header(skb);
380 skb->pkt_type = PACKET_HOST;
382 * Let us waste some time for checking the encapsulation.
383 * Note, that only 7 char is checked so frames with a valid FCS
384 * are also accepted (but FCS is not checked of course).
386 } else if ((skb->len >= sizeof(llc_oui_pid_pad)) &&
387 (memcmp(skb->data, llc_oui_pid_pad, 7) == 0)) {
388 skb_pull(skb, sizeof(llc_oui_pid_pad));
389 skb->protocol = eth_type_trans(skb, net_dev);
394 if (brdev->payload == p_routed) {
397 skb_reset_network_header(skb);
399 if (iph->version == 4)
400 skb->protocol = htons(ETH_P_IP);
401 else if (iph->version == 6)
402 skb->protocol = htons(ETH_P_IPV6);
405 skb->pkt_type = PACKET_HOST;
406 } else { /* p_bridged */
407 /* first 2 chars should be 0 */
408 if (*((u16 *) (skb->data)) != 0)
410 skb_pull(skb, BR2684_PAD_LEN);
411 skb->protocol = eth_type_trans(skb, net_dev);
415 #ifdef CONFIG_ATM_BR2684_IPFILTER
416 if (unlikely(packet_fails_filter(skb->protocol, brvcc, skb)))
418 #endif /* CONFIG_ATM_BR2684_IPFILTER */
420 ATM_SKB(skb)->vcc = atmvcc; /* needed ? */
421 pr_debug("received packet's protocol: %x\n", ntohs(skb->protocol));
423 /* sigh, interface is down? */
424 if (unlikely(!(net_dev->flags & IFF_UP)))
426 net_dev->stats.rx_packets++;
427 net_dev->stats.rx_bytes += skb->len;
428 memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
433 net_dev->stats.rx_dropped++;
436 net_dev->stats.rx_errors++;
443 * Assign a vcc to a dev
444 * Note: we do not have explicit unassign, but look at _push()
446 static int br2684_regvcc(struct atm_vcc *atmvcc, void __user * arg)
449 struct br2684_vcc *brvcc;
451 struct sk_buff_head *rq;
452 struct br2684_dev *brdev;
453 struct net_device *net_dev;
454 struct atm_backend_br2684 be;
457 if (copy_from_user(&be, arg, sizeof be))
459 brvcc = kzalloc(sizeof(struct br2684_vcc), GFP_KERNEL);
462 write_lock_irq(&devs_lock);
463 net_dev = br2684_find_dev(&be.ifspec);
464 if (net_dev == NULL) {
466 "br2684: tried to attach to non-existant device\n");
470 brdev = BRPRIV(net_dev);
471 if (atmvcc->push == NULL) {
475 if (!list_empty(&brdev->brvccs)) {
476 /* Only 1 VCC/dev right now */
480 if (be.fcs_in != BR2684_FCSIN_NO || be.fcs_out != BR2684_FCSOUT_NO ||
481 be.fcs_auto || be.has_vpiid || be.send_padding || (be.encaps !=
485 || be.min_size != 0) {
489 pr_debug("br2684_regvcc vcc=%p, encaps=%d, brvcc=%p\n", atmvcc,
491 if (list_empty(&brdev->brvccs) && !brdev->mac_was_set) {
492 unsigned char *esi = atmvcc->dev->esi;
493 if (esi[0] | esi[1] | esi[2] | esi[3] | esi[4] | esi[5])
494 memcpy(net_dev->dev_addr, esi, net_dev->addr_len);
496 net_dev->dev_addr[2] = 1;
498 list_add(&brvcc->brvccs, &brdev->brvccs);
499 write_unlock_irq(&devs_lock);
500 brvcc->device = net_dev;
501 brvcc->atmvcc = atmvcc;
502 atmvcc->user_back = brvcc;
503 brvcc->encaps = (enum br2684_encaps)be.encaps;
504 brvcc->old_push = atmvcc->push;
506 atmvcc->push = br2684_push;
508 rq = &sk_atm(atmvcc)->sk_receive_queue;
510 spin_lock_irqsave(&rq->lock, flags);
511 if (skb_queue_empty(rq)) {
514 /* NULL terminate the list. */
515 rq->prev->next = NULL;
518 rq->prev = rq->next = (struct sk_buff *)rq;
520 spin_unlock_irqrestore(&rq->lock, flags);
523 struct sk_buff *next = skb->next;
525 skb->next = skb->prev = NULL;
526 br2684_push(atmvcc, skb);
527 skb->dev->stats.rx_bytes -= skb->len;
528 skb->dev->stats.rx_packets--;
532 __module_get(THIS_MODULE);
535 write_unlock_irq(&devs_lock);
540 static const struct net_device_ops br2684_netdev_ops = {
541 .ndo_start_xmit = br2684_start_xmit,
542 .ndo_set_mac_address = br2684_mac_addr,
543 .ndo_change_mtu = eth_change_mtu,
544 .ndo_validate_addr = eth_validate_addr,
547 static void br2684_setup(struct net_device *netdev)
549 struct br2684_dev *brdev = BRPRIV(netdev);
553 netdev->netdev_ops = &br2684_netdev_ops;
555 INIT_LIST_HEAD(&brdev->brvccs);
558 static void br2684_setup_routed(struct net_device *netdev)
560 struct br2684_dev *brdev = BRPRIV(netdev);
561 brdev->net_dev = netdev;
563 netdev->hard_header_len = 0;
565 netdev->netdev_ops = &br2684_netdev_ops;
566 netdev->addr_len = 0;
568 netdev->type = ARPHRD_PPP;
569 netdev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
570 netdev->tx_queue_len = 100;
571 INIT_LIST_HEAD(&brdev->brvccs);
574 static int br2684_create(void __user * arg)
577 struct net_device *netdev;
578 struct br2684_dev *brdev;
579 struct atm_newif_br2684 ni;
580 enum br2684_payload payload;
582 pr_debug("br2684_create\n");
584 if (copy_from_user(&ni, arg, sizeof ni)) {
588 if (ni.media & BR2684_FLAG_ROUTED)
592 ni.media &= 0xffff; /* strip flags */
594 if (ni.media != BR2684_MEDIA_ETHERNET || ni.mtu != 1500) {
598 netdev = alloc_netdev(sizeof(struct br2684_dev),
599 ni.ifname[0] ? ni.ifname : "nas%d",
600 (payload == p_routed) ?
601 br2684_setup_routed : br2684_setup);
605 brdev = BRPRIV(netdev);
607 pr_debug("registered netdev %s\n", netdev->name);
608 /* open, stop, do_ioctl ? */
609 err = register_netdev(netdev);
611 printk(KERN_ERR "br2684_create: register_netdev failed\n");
616 write_lock_irq(&devs_lock);
617 brdev->payload = payload;
618 brdev->number = list_empty(&br2684_devs) ? 1 :
619 BRPRIV(list_entry_brdev(br2684_devs.prev))->number + 1;
620 list_add_tail(&brdev->br2684_devs, &br2684_devs);
621 write_unlock_irq(&devs_lock);
626 * This handles ioctls actually performed on our vcc - we must return
627 * -ENOIOCTLCMD for any unrecognized ioctl
629 static int br2684_ioctl(struct socket *sock, unsigned int cmd,
632 struct atm_vcc *atmvcc = ATM_SD(sock);
633 void __user *argp = (void __user *)arg;
639 case ATM_NEWBACKENDIF:
640 err = get_user(b, (atm_backend_t __user *) argp);
643 if (b != ATM_BACKEND_BR2684)
645 if (!capable(CAP_NET_ADMIN))
647 if (cmd == ATM_SETBACKEND)
648 return br2684_regvcc(atmvcc, argp);
650 return br2684_create(argp);
651 #ifdef CONFIG_ATM_BR2684_IPFILTER
653 if (atmvcc->push != br2684_push)
655 if (!capable(CAP_NET_ADMIN))
657 err = br2684_setfilt(atmvcc, argp);
660 #endif /* CONFIG_ATM_BR2684_IPFILTER */
665 static struct atm_ioctl br2684_ioctl_ops = {
666 .owner = THIS_MODULE,
667 .ioctl = br2684_ioctl,
670 #ifdef CONFIG_PROC_FS
671 static void *br2684_seq_start(struct seq_file *seq, loff_t * pos)
672 __acquires(devs_lock)
674 read_lock(&devs_lock);
675 return seq_list_start(&br2684_devs, *pos);
678 static void *br2684_seq_next(struct seq_file *seq, void *v, loff_t * pos)
680 return seq_list_next(v, &br2684_devs, pos);
683 static void br2684_seq_stop(struct seq_file *seq, void *v)
684 __releases(devs_lock)
686 read_unlock(&devs_lock);
689 static int br2684_seq_show(struct seq_file *seq, void *v)
691 const struct br2684_dev *brdev = list_entry(v, struct br2684_dev,
693 const struct net_device *net_dev = brdev->net_dev;
694 const struct br2684_vcc *brvcc;
696 seq_printf(seq, "dev %.16s: num=%d, mac=%pM (%s)\n",
700 brdev->mac_was_set ? "set" : "auto");
702 list_for_each_entry(brvcc, &brdev->brvccs, brvccs) {
703 seq_printf(seq, " vcc %d.%d.%d: encaps=%s payload=%s"
704 ", failed copies %u/%u"
705 "\n", brvcc->atmvcc->dev->number,
706 brvcc->atmvcc->vpi, brvcc->atmvcc->vci,
707 (brvcc->encaps == e_llc) ? "LLC" : "VC",
708 (brdev->payload == p_bridged) ? "bridged" : "routed",
709 brvcc->copies_failed, brvcc->copies_needed);
710 #ifdef CONFIG_ATM_BR2684_IPFILTER
711 #define b1(var, byte) ((u8 *) &brvcc->filter.var)[byte]
712 #define bs(var) b1(var, 0), b1(var, 1), b1(var, 2), b1(var, 3)
713 if (brvcc->filter.netmask != 0)
714 seq_printf(seq, " filter=%d.%d.%d.%d/"
715 "%d.%d.%d.%d\n", bs(prefix), bs(netmask));
718 #endif /* CONFIG_ATM_BR2684_IPFILTER */
723 static const struct seq_operations br2684_seq_ops = {
724 .start = br2684_seq_start,
725 .next = br2684_seq_next,
726 .stop = br2684_seq_stop,
727 .show = br2684_seq_show,
730 static int br2684_proc_open(struct inode *inode, struct file *file)
732 return seq_open(file, &br2684_seq_ops);
735 static const struct file_operations br2684_proc_ops = {
736 .owner = THIS_MODULE,
737 .open = br2684_proc_open,
740 .release = seq_release,
743 extern struct proc_dir_entry *atm_proc_root; /* from proc.c */
744 #endif /* CONFIG_PROC_FS */
746 static int __init br2684_init(void)
748 #ifdef CONFIG_PROC_FS
749 struct proc_dir_entry *p;
750 p = proc_create("br2684", 0, atm_proc_root, &br2684_proc_ops);
754 register_atm_ioctl(&br2684_ioctl_ops);
758 static void __exit br2684_exit(void)
760 struct net_device *net_dev;
761 struct br2684_dev *brdev;
762 struct br2684_vcc *brvcc;
763 deregister_atm_ioctl(&br2684_ioctl_ops);
765 #ifdef CONFIG_PROC_FS
766 remove_proc_entry("br2684", atm_proc_root);
769 while (!list_empty(&br2684_devs)) {
770 net_dev = list_entry_brdev(br2684_devs.next);
771 brdev = BRPRIV(net_dev);
772 while (!list_empty(&brdev->brvccs)) {
773 brvcc = list_entry_brvcc(brdev->brvccs.next);
774 br2684_close_vcc(brvcc);
777 list_del(&brdev->br2684_devs);
778 unregister_netdev(net_dev);
779 free_netdev(net_dev);
783 module_init(br2684_init);
784 module_exit(br2684_exit);
786 MODULE_AUTHOR("Marcell GAL");
787 MODULE_DESCRIPTION("RFC2684 bridged protocols over ATM/AAL5");
788 MODULE_LICENSE("GPL");