2 Experimental ethernet netdevice using ATM AAL5 as underlying carrier
3 (RFC1483 obsoleted by RFC2684) for Linux 2.4
4 Author: Marcell GAL, 2000, XDSL Ltd, Hungary
7 #include <linux/module.h>
8 #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>
16 #include <asm/uaccess.h>
18 #include <linux/atm.h>
19 #include <linux/atmdev.h>
20 #include <linux/capability.h>
21 #include <linux/seq_file.h>
23 #include <linux/atmbr2684.h>
28 static void skb_debug(const struct sk_buff
*skb
)
31 char buf
[NUM2PRINT
* 3 + 1]; /* 3 chars per byte */
33 for (i
= 0; i
< skb
->len
&& i
< NUM2PRINT
; i
++) {
34 sprintf(buf
+ i
* 3, "%2.2x ", 0xff & skb
->data
[i
]);
36 printk(KERN_DEBUG
"br2684: skb: %s\n", buf
);
39 #define skb_debug(skb) do {} while (0)
42 static unsigned char llc_oui_pid_pad
[] =
43 { 0xAA, 0xAA, 0x03, 0x00, 0x80, 0xC2, 0x00, 0x07, 0x00, 0x00 };
47 e_vc
= BR2684_ENCAPS_VC
,
48 e_llc
= BR2684_ENCAPS_LLC
,
52 struct atm_vcc
*atmvcc
;
53 struct net_device
*device
;
54 /* keep old push,pop functions for chaining */
55 void (*old_push
)(struct atm_vcc
*vcc
,struct sk_buff
*skb
);
56 /* void (*old_pop)(struct atm_vcc *vcc,struct sk_buff *skb); */
57 enum br2684_encaps encaps
;
58 struct list_head brvccs
;
59 #ifdef CONFIG_ATM_BR2684_IPFILTER
60 struct br2684_filter filter
;
61 #endif /* CONFIG_ATM_BR2684_IPFILTER */
62 unsigned copies_needed
, copies_failed
;
66 struct net_device
*net_dev
;
67 struct list_head br2684_devs
;
69 struct list_head brvccs
; /* one device <=> one vcc (before xmas) */
70 struct net_device_stats stats
;
75 * This lock should be held for writing any time the list of devices or
76 * their attached vcc's could be altered. It should be held for reading
77 * any time these are being queried. Note that we sometimes need to
78 * do read-locking under interrupt context, so write locking must block
79 * the current CPU's interrupts
81 static DEFINE_RWLOCK(devs_lock
);
83 static LIST_HEAD(br2684_devs
);
85 static inline struct br2684_dev
*BRPRIV(const struct net_device
*net_dev
)
87 return (struct br2684_dev
*) net_dev
->priv
;
90 static inline struct net_device
*list_entry_brdev(const struct list_head
*le
)
92 return list_entry(le
, struct br2684_dev
, br2684_devs
)->net_dev
;
95 static inline struct br2684_vcc
*BR2684_VCC(const struct atm_vcc
*atmvcc
)
97 return (struct br2684_vcc
*) (atmvcc
->user_back
);
100 static inline struct br2684_vcc
*list_entry_brvcc(const struct list_head
*le
)
102 return list_entry(le
, struct br2684_vcc
, brvccs
);
105 /* Caller should hold read_lock(&devs_lock) */
106 static struct net_device
*br2684_find_dev(const struct br2684_if_spec
*s
)
108 struct list_head
*lh
;
109 struct net_device
*net_dev
;
111 case BR2684_FIND_BYNUM
:
112 list_for_each(lh
, &br2684_devs
) {
113 net_dev
= list_entry_brdev(lh
);
114 if (BRPRIV(net_dev
)->number
== s
->spec
.devnum
)
118 case BR2684_FIND_BYIFNAME
:
119 list_for_each(lh
, &br2684_devs
) {
120 net_dev
= list_entry_brdev(lh
);
121 if (!strncmp(net_dev
->name
, s
->spec
.ifname
, IFNAMSIZ
))
130 * Send a packet out a particular vcc. Not to useful right now, but paves
131 * the way for multiple vcc's per itf. Returns true if we can send,
134 static int br2684_xmit_vcc(struct sk_buff
*skb
, struct br2684_dev
*brdev
,
135 struct br2684_vcc
*brvcc
)
137 struct atm_vcc
*atmvcc
;
138 int minheadroom
= (brvcc
->encaps
== e_llc
) ? 10 : 2;
139 if (skb_headroom(skb
) < minheadroom
) {
140 struct sk_buff
*skb2
= skb_realloc_headroom(skb
, minheadroom
);
141 brvcc
->copies_needed
++;
144 brvcc
->copies_failed
++;
149 skb_push(skb
, minheadroom
);
150 if (brvcc
->encaps
== e_llc
)
151 skb_copy_to_linear_data(skb
, llc_oui_pid_pad
, 10);
153 memset(skb
->data
, 0, 2);
156 ATM_SKB(skb
)->vcc
= atmvcc
= brvcc
->atmvcc
;
157 pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb
, atmvcc
, atmvcc
->dev
);
158 if (!atm_may_send(atmvcc
, skb
->truesize
)) {
159 /* we free this here for now, because we cannot know in a higher
160 layer whether the skb point it supplied wasn't freed yet.
166 atomic_add(skb
->truesize
, &sk_atm(atmvcc
)->sk_wmem_alloc
);
167 ATM_SKB(skb
)->atm_options
= atmvcc
->atm_options
;
168 brdev
->stats
.tx_packets
++;
169 brdev
->stats
.tx_bytes
+= skb
->len
;
170 atmvcc
->send(atmvcc
, skb
);
174 static inline struct br2684_vcc
*pick_outgoing_vcc(struct sk_buff
*skb
,
175 struct br2684_dev
*brdev
)
177 return list_empty(&brdev
->brvccs
) ? NULL
:
178 list_entry_brvcc(brdev
->brvccs
.next
); /* 1 vcc/dev right now */
181 static int br2684_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
183 struct br2684_dev
*brdev
= BRPRIV(dev
);
184 struct br2684_vcc
*brvcc
;
186 pr_debug("br2684_start_xmit, skb->dst=%p\n", skb
->dst
);
187 read_lock(&devs_lock
);
188 brvcc
= pick_outgoing_vcc(skb
, brdev
);
190 pr_debug("no vcc attached to dev %s\n", dev
->name
);
191 brdev
->stats
.tx_errors
++;
192 brdev
->stats
.tx_carrier_errors
++;
193 /* netif_stop_queue(dev); */
195 read_unlock(&devs_lock
);
198 if (!br2684_xmit_vcc(skb
, brdev
, brvcc
)) {
200 * We should probably use netif_*_queue() here, but that
201 * involves added complication. We need to walk before
204 /* don't free here! this pointer might be no longer valid!
207 brdev
->stats
.tx_errors
++;
208 brdev
->stats
.tx_fifo_errors
++;
210 read_unlock(&devs_lock
);
214 static struct net_device_stats
*br2684_get_stats(struct net_device
*dev
)
216 pr_debug("br2684_get_stats\n");
217 return &BRPRIV(dev
)->stats
;
222 * We remember when the MAC gets set, so we don't override it later with
223 * the ESI of the ATM card of the first VC
225 static int (*my_eth_mac_addr
)(struct net_device
*, void *);
226 static int br2684_mac_addr(struct net_device
*dev
, void *p
)
228 int err
= my_eth_mac_addr(dev
, p
);
230 BRPRIV(dev
)->mac_was_set
= 1;
234 #ifdef CONFIG_ATM_BR2684_IPFILTER
235 /* this IOCTL is experimental. */
236 static int br2684_setfilt(struct atm_vcc
*atmvcc
, void __user
*arg
)
238 struct br2684_vcc
*brvcc
;
239 struct br2684_filter_set fs
;
241 if (copy_from_user(&fs
, arg
, sizeof fs
))
243 if (fs
.ifspec
.method
!= BR2684_FIND_BYNOTHING
) {
245 * This is really a per-vcc thing, but we can also search
248 struct br2684_dev
*brdev
;
249 read_lock(&devs_lock
);
250 brdev
= BRPRIV(br2684_find_dev(&fs
.ifspec
));
251 if (brdev
== NULL
|| list_empty(&brdev
->brvccs
) ||
252 brdev
->brvccs
.next
!= brdev
->brvccs
.prev
) /* >1 VCC */
255 brvcc
= list_entry_brvcc(brdev
->brvccs
.next
);
256 read_unlock(&devs_lock
);
260 brvcc
= BR2684_VCC(atmvcc
);
261 memcpy(&brvcc
->filter
, &fs
.filter
, sizeof(brvcc
->filter
));
265 /* Returns 1 if packet should be dropped */
267 packet_fails_filter(__be16 type
, struct br2684_vcc
*brvcc
, struct sk_buff
*skb
)
269 if (brvcc
->filter
.netmask
== 0)
270 return 0; /* no filter in place */
271 if (type
== htons(ETH_P_IP
) &&
272 (((struct iphdr
*) (skb
->data
))->daddr
& brvcc
->filter
.
273 netmask
) == brvcc
->filter
.prefix
)
275 if (type
== htons(ETH_P_ARP
))
277 /* TODO: we should probably filter ARPs too.. don't want to have
278 * them returning values that don't make sense, or is that ok?
282 #endif /* CONFIG_ATM_BR2684_IPFILTER */
284 static void br2684_close_vcc(struct br2684_vcc
*brvcc
)
286 pr_debug("removing VCC %p from dev %p\n", brvcc
, brvcc
->device
);
287 write_lock_irq(&devs_lock
);
288 list_del(&brvcc
->brvccs
);
289 write_unlock_irq(&devs_lock
);
290 brvcc
->atmvcc
->user_back
= NULL
; /* what about vcc->recvq ??? */
291 brvcc
->old_push(brvcc
->atmvcc
, NULL
); /* pass on the bad news */
293 module_put(THIS_MODULE
);
296 /* when AAL5 PDU comes in: */
297 static void br2684_push(struct atm_vcc
*atmvcc
, struct sk_buff
*skb
)
299 struct br2684_vcc
*brvcc
= BR2684_VCC(atmvcc
);
300 struct net_device
*net_dev
= brvcc
->device
;
301 struct br2684_dev
*brdev
= BRPRIV(net_dev
);
302 int plen
= sizeof(llc_oui_pid_pad
) + ETH_HLEN
;
304 pr_debug("br2684_push\n");
306 if (unlikely(skb
== NULL
)) {
307 /* skb==NULL means VCC is being destroyed */
308 br2684_close_vcc(brvcc
);
309 if (list_empty(&brdev
->brvccs
)) {
310 read_lock(&devs_lock
);
311 list_del(&brdev
->br2684_devs
);
312 read_unlock(&devs_lock
);
313 unregister_netdev(net_dev
);
314 free_netdev(net_dev
);
320 atm_return(atmvcc
, skb
->truesize
);
321 pr_debug("skb from brdev %p\n", brdev
);
322 if (brvcc
->encaps
== e_llc
) {
323 /* let us waste some time for checking the encapsulation.
324 Note, that only 7 char is checked so frames with a valid FCS
325 are also accepted (but FCS is not checked of course) */
326 if (memcmp(skb
->data
, llc_oui_pid_pad
, 7)) {
327 brdev
->stats
.rx_errors
++;
332 /* Strip FCS if present */
333 if (skb
->len
> 7 && skb
->data
[7] == 0x01)
334 __skb_trim(skb
, skb
->len
- 4);
336 plen
= PADLEN
+ ETH_HLEN
; /* pad, dstmac,srcmac, ethtype */
337 /* first 2 chars should be 0 */
338 if (*((u16
*) (skb
->data
)) != 0) {
339 brdev
->stats
.rx_errors
++;
344 if (skb
->len
< plen
) {
345 brdev
->stats
.rx_errors
++;
346 dev_kfree_skb(skb
); /* dev_ not needed? */
350 skb_pull(skb
, plen
- ETH_HLEN
);
351 skb
->protocol
= eth_type_trans(skb
, net_dev
);
352 #ifdef CONFIG_ATM_BR2684_IPFILTER
353 if (unlikely(packet_fails_filter(skb
->protocol
, brvcc
, skb
))) {
354 brdev
->stats
.rx_dropped
++;
358 #endif /* CONFIG_ATM_BR2684_IPFILTER */
360 ATM_SKB(skb
)->vcc
= atmvcc
; /* needed ? */
361 pr_debug("received packet's protocol: %x\n", ntohs(skb
->protocol
));
363 if (unlikely(!(net_dev
->flags
& IFF_UP
))) {
364 /* sigh, interface is down */
365 brdev
->stats
.rx_dropped
++;
369 brdev
->stats
.rx_packets
++;
370 brdev
->stats
.rx_bytes
+= skb
->len
;
371 memset(ATM_SKB(skb
), 0, sizeof(struct atm_skb_data
));
375 static int br2684_regvcc(struct atm_vcc
*atmvcc
, void __user
*arg
)
377 /* assign a vcc to a dev
378 Note: we do not have explicit unassign, but look at _push()
381 struct br2684_vcc
*brvcc
;
383 struct sk_buff_head
*rq
;
384 struct br2684_dev
*brdev
;
385 struct net_device
*net_dev
;
386 struct atm_backend_br2684 be
;
389 if (copy_from_user(&be
, arg
, sizeof be
))
391 brvcc
= kzalloc(sizeof(struct br2684_vcc
), GFP_KERNEL
);
394 write_lock_irq(&devs_lock
);
395 net_dev
= br2684_find_dev(&be
.ifspec
);
396 if (net_dev
== NULL
) {
398 "br2684: tried to attach to non-existant device\n");
402 brdev
= BRPRIV(net_dev
);
403 if (atmvcc
->push
== NULL
) {
407 if (!list_empty(&brdev
->brvccs
)) {
408 /* Only 1 VCC/dev right now */
412 if (be
.fcs_in
!= BR2684_FCSIN_NO
|| be
.fcs_out
!= BR2684_FCSOUT_NO
||
413 be
.fcs_auto
|| be
.has_vpiid
|| be
.send_padding
|| (be
.encaps
!=
414 BR2684_ENCAPS_VC
&& be
.encaps
!= BR2684_ENCAPS_LLC
) ||
419 pr_debug("br2684_regvcc vcc=%p, encaps=%d, brvcc=%p\n", atmvcc
, be
.encaps
,
421 if (list_empty(&brdev
->brvccs
) && !brdev
->mac_was_set
) {
422 unsigned char *esi
= atmvcc
->dev
->esi
;
423 if (esi
[0] | esi
[1] | esi
[2] | esi
[3] | esi
[4] | esi
[5])
424 memcpy(net_dev
->dev_addr
, esi
, net_dev
->addr_len
);
426 net_dev
->dev_addr
[2] = 1;
428 list_add(&brvcc
->brvccs
, &brdev
->brvccs
);
429 write_unlock_irq(&devs_lock
);
430 brvcc
->device
= net_dev
;
431 brvcc
->atmvcc
= atmvcc
;
432 atmvcc
->user_back
= brvcc
;
433 brvcc
->encaps
= (enum br2684_encaps
) be
.encaps
;
434 brvcc
->old_push
= atmvcc
->push
;
436 atmvcc
->push
= br2684_push
;
438 rq
= &sk_atm(atmvcc
)->sk_receive_queue
;
440 spin_lock_irqsave(&rq
->lock
, flags
);
441 if (skb_queue_empty(rq
)) {
444 /* NULL terminate the list. */
445 rq
->prev
->next
= NULL
;
448 rq
->prev
= rq
->next
= (struct sk_buff
*)rq
;
450 spin_unlock_irqrestore(&rq
->lock
, flags
);
453 struct sk_buff
*next
= skb
->next
;
455 skb
->next
= skb
->prev
= NULL
;
456 BRPRIV(skb
->dev
)->stats
.rx_bytes
-= skb
->len
;
457 BRPRIV(skb
->dev
)->stats
.rx_packets
--;
458 br2684_push(atmvcc
, skb
);
462 __module_get(THIS_MODULE
);
465 write_unlock_irq(&devs_lock
);
470 static void br2684_setup(struct net_device
*netdev
)
472 struct br2684_dev
*brdev
= BRPRIV(netdev
);
475 brdev
->net_dev
= netdev
;
477 my_eth_mac_addr
= netdev
->set_mac_address
;
478 netdev
->set_mac_address
= br2684_mac_addr
;
479 netdev
->hard_start_xmit
= br2684_start_xmit
;
480 netdev
->get_stats
= br2684_get_stats
;
482 INIT_LIST_HEAD(&brdev
->brvccs
);
485 static int br2684_create(void __user
*arg
)
488 struct net_device
*netdev
;
489 struct br2684_dev
*brdev
;
490 struct atm_newif_br2684 ni
;
492 pr_debug("br2684_create\n");
494 if (copy_from_user(&ni
, arg
, sizeof ni
)) {
497 if (ni
.media
!= BR2684_MEDIA_ETHERNET
|| ni
.mtu
!= 1500) {
501 netdev
= alloc_netdev(sizeof(struct br2684_dev
),
502 ni
.ifname
[0] ? ni
.ifname
: "nas%d",
507 brdev
= BRPRIV(netdev
);
509 pr_debug("registered netdev %s\n", netdev
->name
);
510 /* open, stop, do_ioctl ? */
511 err
= register_netdev(netdev
);
513 printk(KERN_ERR
"br2684_create: register_netdev failed\n");
518 write_lock_irq(&devs_lock
);
519 brdev
->number
= list_empty(&br2684_devs
) ? 1 :
520 BRPRIV(list_entry_brdev(br2684_devs
.prev
))->number
+ 1;
521 list_add_tail(&brdev
->br2684_devs
, &br2684_devs
);
522 write_unlock_irq(&devs_lock
);
527 * This handles ioctls actually performed on our vcc - we must return
528 * -ENOIOCTLCMD for any unrecognized ioctl
530 static int br2684_ioctl(struct socket
*sock
, unsigned int cmd
,
533 struct atm_vcc
*atmvcc
= ATM_SD(sock
);
534 void __user
*argp
= (void __user
*)arg
;
539 case ATM_NEWBACKENDIF
: {
541 err
= get_user(b
, (atm_backend_t __user
*) argp
);
544 if (b
!= ATM_BACKEND_BR2684
)
546 if (!capable(CAP_NET_ADMIN
))
548 if (cmd
== ATM_SETBACKEND
)
549 return br2684_regvcc(atmvcc
, argp
);
551 return br2684_create(argp
);
553 #ifdef CONFIG_ATM_BR2684_IPFILTER
555 if (atmvcc
->push
!= br2684_push
)
557 if (!capable(CAP_NET_ADMIN
))
559 err
= br2684_setfilt(atmvcc
, argp
);
561 #endif /* CONFIG_ATM_BR2684_IPFILTER */
566 static struct atm_ioctl br2684_ioctl_ops
= {
567 .owner
= THIS_MODULE
,
568 .ioctl
= br2684_ioctl
,
572 #ifdef CONFIG_PROC_FS
573 static void *br2684_seq_start(struct seq_file
*seq
, loff_t
*pos
)
575 read_lock(&devs_lock
);
576 return seq_list_start(&br2684_devs
, *pos
);
579 static void *br2684_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
581 return seq_list_next(v
, &br2684_devs
, pos
);
584 static void br2684_seq_stop(struct seq_file
*seq
, void *v
)
586 read_unlock(&devs_lock
);
589 static int br2684_seq_show(struct seq_file
*seq
, void *v
)
591 const struct br2684_dev
*brdev
= list_entry(v
, struct br2684_dev
,
593 const struct net_device
*net_dev
= brdev
->net_dev
;
594 const struct br2684_vcc
*brvcc
;
595 DECLARE_MAC_BUF(mac
);
597 seq_printf(seq
, "dev %.16s: num=%d, mac=%s (%s)\n",
600 print_mac(mac
, net_dev
->dev_addr
),
601 brdev
->mac_was_set
? "set" : "auto");
603 list_for_each_entry(brvcc
, &brdev
->brvccs
, brvccs
) {
604 seq_printf(seq
, " vcc %d.%d.%d: encaps=%s"
605 ", failed copies %u/%u"
606 "\n", brvcc
->atmvcc
->dev
->number
,
607 brvcc
->atmvcc
->vpi
, brvcc
->atmvcc
->vci
,
608 (brvcc
->encaps
== e_llc
) ? "LLC" : "VC"
609 , brvcc
->copies_failed
610 , brvcc
->copies_needed
612 #ifdef CONFIG_ATM_BR2684_IPFILTER
613 #define b1(var, byte) ((u8 *) &brvcc->filter.var)[byte]
614 #define bs(var) b1(var, 0), b1(var, 1), b1(var, 2), b1(var, 3)
615 if (brvcc
->filter
.netmask
!= 0)
616 seq_printf(seq
, " filter=%d.%d.%d.%d/"
618 bs(prefix
), bs(netmask
));
621 #endif /* CONFIG_ATM_BR2684_IPFILTER */
626 static const struct seq_operations br2684_seq_ops
= {
627 .start
= br2684_seq_start
,
628 .next
= br2684_seq_next
,
629 .stop
= br2684_seq_stop
,
630 .show
= br2684_seq_show
,
633 static int br2684_proc_open(struct inode
*inode
, struct file
*file
)
635 return seq_open(file
, &br2684_seq_ops
);
638 static const struct file_operations br2684_proc_ops
= {
639 .owner
= THIS_MODULE
,
640 .open
= br2684_proc_open
,
643 .release
= seq_release
,
646 extern struct proc_dir_entry
*atm_proc_root
; /* from proc.c */
649 static int __init
br2684_init(void)
651 #ifdef CONFIG_PROC_FS
652 struct proc_dir_entry
*p
;
653 if ((p
= create_proc_entry("br2684", 0, atm_proc_root
)) == NULL
)
655 p
->proc_fops
= &br2684_proc_ops
;
657 register_atm_ioctl(&br2684_ioctl_ops
);
661 static void __exit
br2684_exit(void)
663 struct net_device
*net_dev
;
664 struct br2684_dev
*brdev
;
665 struct br2684_vcc
*brvcc
;
666 deregister_atm_ioctl(&br2684_ioctl_ops
);
668 #ifdef CONFIG_PROC_FS
669 remove_proc_entry("br2684", atm_proc_root
);
672 while (!list_empty(&br2684_devs
)) {
673 net_dev
= list_entry_brdev(br2684_devs
.next
);
674 brdev
= BRPRIV(net_dev
);
675 while (!list_empty(&brdev
->brvccs
)) {
676 brvcc
= list_entry_brvcc(brdev
->brvccs
.next
);
677 br2684_close_vcc(brvcc
);
680 list_del(&brdev
->br2684_devs
);
681 unregister_netdev(net_dev
);
682 free_netdev(net_dev
);
686 module_init(br2684_init
);
687 module_exit(br2684_exit
);
689 MODULE_AUTHOR("Marcell GAL");
690 MODULE_DESCRIPTION("RFC2684 bridged protocols over ATM/AAL5");
691 MODULE_LICENSE("GPL");