Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
| 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 |
| 45 | static 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 | |
| 59 | static unsigned char llc_oui_pid_pad[] = |
| 60 | { 0xAA, 0xAA, 0x03, 0x00, 0x80, 0xC2, 0x00, 0x07, 0x00, 0x00 }; |
| 61 | #define PADLEN (2) |
| 62 | |
| 63 | enum br2684_encaps { |
| 64 | e_vc = BR2684_ENCAPS_VC, |
| 65 | e_llc = BR2684_ENCAPS_LLC, |
| 66 | }; |
| 67 | |
| 68 | struct 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 | |
| 84 | struct 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 | */ |
| 100 | static DEFINE_RWLOCK(devs_lock); |
| 101 | |
| 102 | static LIST_HEAD(br2684_devs); |
| 103 | |
| 104 | static inline struct br2684_dev *BRPRIV(const struct net_device *net_dev) |
| 105 | { |
| 106 | return (struct br2684_dev *) net_dev->priv; |
| 107 | } |
| 108 | |
| 109 | static 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 | |
| 114 | static inline struct br2684_vcc *BR2684_VCC(const struct atm_vcc *atmvcc) |
| 115 | { |
| 116 | return (struct br2684_vcc *) (atmvcc->user_back); |
| 117 | } |
| 118 | |
| 119 | static 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) */ |
| 125 | static 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 | */ |
| 153 | static 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 | |
| 201 | static 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 | |
| 208 | static 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); |
| 223 | return -EUNATCH; |
| 224 | } |
| 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 | |
| 241 | static 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 | */ |
| 253 | static int (*my_eth_header)(struct sk_buff *, struct net_device *, |
| 254 | unsigned short, void *, void *, unsigned); |
| 255 | static int (*my_eth_header_cache)(struct neighbour *, struct hh_cache *); |
| 256 | |
| 257 | static int |
| 258 | br2684_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 | |
| 271 | static int |
| 272 | br2684_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 |
| 275 | xmit 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 | */ |
| 292 | static inline unsigned short br_type_trans(struct sk_buff *skb, |
| 293 | struct net_device *dev) |
| 294 | { |
| 295 | struct ethhdr *eth; |
| 296 | unsigned char *rawp; |
| 297 | eth = eth_hdr(skb); |
| 298 | |
| 299 | if (*eth->h_dest & 1) { |
| 300 | if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0) |
| 301 | skb->pkt_type = PACKET_BROADCAST; |
| 302 | else |
| 303 | skb->pkt_type = PACKET_MULTICAST; |
| 304 | } |
| 305 | |
| 306 | else if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN)) |
| 307 | 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 | */ |
| 334 | static int (*my_eth_mac_addr)(struct net_device *, void *); |
| 335 | static 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. */ |
| 345 | static 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 */ |
| 375 | static inline int |
| 376 | packet_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 | |
| 393 | static 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: */ |
| 406 | static 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 | |
| 497 | static int br2684_regvcc(struct atm_vcc *atmvcc, void __user *arg) |
| 498 | { |
| 499 | /* assign a vcc to a dev |
| 500 | Note: 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(©); |
| 560 | skb_migrate(&sk_atm(atmvcc)->sk_receive_queue, ©); |
| 561 | while ((skb = skb_dequeue(©)) != 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 | |
| 574 | static 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 | |
| 596 | static 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 | */ |
| 641 | static 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 | |
| 677 | static struct atm_ioctl br2684_ioctl_ops = { |
| 678 | .owner = THIS_MODULE, |
| 679 | .ioctl = br2684_ioctl, |
| 680 | }; |
| 681 | |
| 682 | |
| 683 | #ifdef CONFIG_PROC_FS |
| 684 | static 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 | |
| 699 | static 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 | |
| 710 | static void br2684_seq_stop(struct seq_file *seq, void *v) |
| 711 | { |
| 712 | read_unlock(&devs_lock); |
| 713 | } |
| 714 | |
| 715 | static 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 | |
| 759 | static 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 | |
| 766 | static int br2684_proc_open(struct inode *inode, struct file *file) |
| 767 | { |
| 768 | return seq_open(file, &br2684_seq_ops); |
| 769 | } |
| 770 | |
| 771 | static 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 | |
| 779 | extern struct proc_dir_entry *atm_proc_root; /* from proc.c */ |
| 780 | #endif |
| 781 | |
| 782 | static 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 | |
| 794 | static 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 | |
| 819 | module_init(br2684_init); |
| 820 | module_exit(br2684_exit); |
| 821 | |
| 822 | MODULE_AUTHOR("Marcell GAL"); |
| 823 | MODULE_DESCRIPTION("RFC2684 bridged protocols over ATM/AAL5"); |
| 824 | MODULE_LICENSE("GPL"); |