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