br2684.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*
  2. * Ethernet netdevice using ATM AAL5 as underlying carrier
  3. * (RFC1483 obsoleted by RFC2684) for Linux
  4. *
  5. * Authors: Marcell GAL, 2000, XDSL Ltd, Hungary
  6. * Eric Kinzie, 2006-2007, US Naval Research Laboratory
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  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>
  17. #include <linux/ip.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/slab.h>
  20. #include <net/arp.h>
  21. #include <linux/atm.h>
  22. #include <linux/atmdev.h>
  23. #include <linux/capability.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/atmbr2684.h>
  26. #include "common.h"
  27. static void skb_debug(const struct sk_buff *skb)
  28. {
  29. #ifdef SKB_DEBUG
  30. #define NUM2PRINT 50
  31. print_hex_dump(KERN_DEBUG, "br2684: skb: ", DUMP_OFFSET,
  32. 16, 1, skb->data, min(NUM2PRINT, skb->len), true);
  33. #endif
  34. }
  35. #define BR2684_ETHERTYPE_LEN 2
  36. #define BR2684_PAD_LEN 2
  37. #define LLC 0xaa, 0xaa, 0x03
  38. #define SNAP_BRIDGED 0x00, 0x80, 0xc2
  39. #define SNAP_ROUTED 0x00, 0x00, 0x00
  40. #define PID_ETHERNET 0x00, 0x07
  41. #define ETHERTYPE_IPV4 0x08, 0x00
  42. #define ETHERTYPE_IPV6 0x86, 0xdd
  43. #define PAD_BRIDGED 0x00, 0x00
  44. static const unsigned char ethertype_ipv4[] = { ETHERTYPE_IPV4 };
  45. static const unsigned char ethertype_ipv6[] = { ETHERTYPE_IPV6 };
  46. static const unsigned char llc_oui_pid_pad[] =
  47. { LLC, SNAP_BRIDGED, PID_ETHERNET, PAD_BRIDGED };
  48. static const unsigned char llc_oui_ipv4[] = { LLC, SNAP_ROUTED, ETHERTYPE_IPV4 };
  49. static const unsigned char llc_oui_ipv6[] = { LLC, SNAP_ROUTED, ETHERTYPE_IPV6 };
  50. enum br2684_encaps {
  51. e_vc = BR2684_ENCAPS_VC,
  52. e_llc = BR2684_ENCAPS_LLC,
  53. };
  54. struct br2684_vcc {
  55. struct atm_vcc *atmvcc;
  56. struct net_device *device;
  57. /* keep old push, pop functions for chaining */
  58. void (*old_push)(struct atm_vcc *vcc, struct sk_buff *skb);
  59. void (*old_pop)(struct atm_vcc *vcc, struct sk_buff *skb);
  60. enum br2684_encaps encaps;
  61. struct list_head brvccs;
  62. #ifdef CONFIG_ATM_BR2684_IPFILTER
  63. struct br2684_filter filter;
  64. #endif /* CONFIG_ATM_BR2684_IPFILTER */
  65. unsigned copies_needed, copies_failed;
  66. };
  67. struct br2684_dev {
  68. struct net_device *net_dev;
  69. struct list_head br2684_devs;
  70. int number;
  71. struct list_head brvccs; /* one device <=> one vcc (before xmas) */
  72. int mac_was_set;
  73. enum br2684_payload payload;
  74. };
  75. /*
  76. * This lock should be held for writing any time the list of devices or
  77. * their attached vcc's could be altered. It should be held for reading
  78. * any time these are being queried. Note that we sometimes need to
  79. * do read-locking under interrupt context, so write locking must block
  80. * the current CPU's interrupts
  81. */
  82. static DEFINE_RWLOCK(devs_lock);
  83. static LIST_HEAD(br2684_devs);
  84. static inline struct br2684_dev *BRPRIV(const struct net_device *net_dev)
  85. {
  86. return (struct br2684_dev *)netdev_priv(net_dev);
  87. }
  88. static inline struct net_device *list_entry_brdev(const struct list_head *le)
  89. {
  90. return list_entry(le, struct br2684_dev, br2684_devs)->net_dev;
  91. }
  92. static inline struct br2684_vcc *BR2684_VCC(const struct atm_vcc *atmvcc)
  93. {
  94. return (struct br2684_vcc *)(atmvcc->user_back);
  95. }
  96. static inline struct br2684_vcc *list_entry_brvcc(const struct list_head *le)
  97. {
  98. return list_entry(le, struct br2684_vcc, brvccs);
  99. }
  100. /* Caller should hold read_lock(&devs_lock) */
  101. static struct net_device *br2684_find_dev(const struct br2684_if_spec *s)
  102. {
  103. struct list_head *lh;
  104. struct net_device *net_dev;
  105. switch (s->method) {
  106. case BR2684_FIND_BYNUM:
  107. list_for_each(lh, &br2684_devs) {
  108. net_dev = list_entry_brdev(lh);
  109. if (BRPRIV(net_dev)->number == s->spec.devnum)
  110. return net_dev;
  111. }
  112. break;
  113. case BR2684_FIND_BYIFNAME:
  114. list_for_each(lh, &br2684_devs) {
  115. net_dev = list_entry_brdev(lh);
  116. if (!strncmp(net_dev->name, s->spec.ifname, IFNAMSIZ))
  117. return net_dev;
  118. }
  119. break;
  120. }
  121. return NULL;
  122. }
  123. static int atm_dev_event(struct notifier_block *this, unsigned long event,
  124. void *arg)
  125. {
  126. struct atm_dev *atm_dev = arg;
  127. struct list_head *lh;
  128. struct net_device *net_dev;
  129. struct br2684_vcc *brvcc;
  130. struct atm_vcc *atm_vcc;
  131. unsigned long flags;
  132. pr_debug("event=%ld dev=%p\n", event, atm_dev);
  133. read_lock_irqsave(&devs_lock, flags);
  134. list_for_each(lh, &br2684_devs) {
  135. net_dev = list_entry_brdev(lh);
  136. list_for_each_entry(brvcc, &BRPRIV(net_dev)->brvccs, brvccs) {
  137. atm_vcc = brvcc->atmvcc;
  138. if (atm_vcc && brvcc->atmvcc->dev == atm_dev) {
  139. if (atm_vcc->dev->signal == ATM_PHY_SIG_LOST)
  140. netif_carrier_off(net_dev);
  141. else
  142. netif_carrier_on(net_dev);
  143. }
  144. }
  145. }
  146. read_unlock_irqrestore(&devs_lock, flags);
  147. return NOTIFY_DONE;
  148. }
  149. static struct notifier_block atm_dev_notifier = {
  150. .notifier_call = atm_dev_event,
  151. };
  152. /* chained vcc->pop function. Check if we should wake the netif_queue */
  153. static void br2684_pop(struct atm_vcc *vcc, struct sk_buff *skb)
  154. {
  155. struct br2684_vcc *brvcc = BR2684_VCC(vcc);
  156. struct net_device *net_dev = skb->dev;
  157. pr_debug("(vcc %p ; net_dev %p )\n", vcc, net_dev);
  158. brvcc->old_pop(vcc, skb);
  159. if (!net_dev)
  160. return;
  161. if (atm_may_send(vcc, 0))
  162. netif_wake_queue(net_dev);
  163. }
  164. /*
  165. * Send a packet out a particular vcc. Not to useful right now, but paves
  166. * the way for multiple vcc's per itf. Returns true if we can send,
  167. * otherwise false
  168. */
  169. static int br2684_xmit_vcc(struct sk_buff *skb, struct net_device *dev,
  170. struct br2684_vcc *brvcc)
  171. {
  172. struct br2684_dev *brdev = BRPRIV(dev);
  173. struct atm_vcc *atmvcc;
  174. int minheadroom = (brvcc->encaps == e_llc) ? 10 : 2;
  175. if (skb_headroom(skb) < minheadroom) {
  176. struct sk_buff *skb2 = skb_realloc_headroom(skb, minheadroom);
  177. brvcc->copies_needed++;
  178. dev_kfree_skb(skb);
  179. if (skb2 == NULL) {
  180. brvcc->copies_failed++;
  181. return 0;
  182. }
  183. skb = skb2;
  184. }
  185. if (brvcc->encaps == e_llc) {
  186. if (brdev->payload == p_bridged) {
  187. skb_push(skb, sizeof(llc_oui_pid_pad));
  188. skb_copy_to_linear_data(skb, llc_oui_pid_pad,
  189. sizeof(llc_oui_pid_pad));
  190. } else if (brdev->payload == p_routed) {
  191. unsigned short prot = ntohs(skb->protocol);
  192. skb_push(skb, sizeof(llc_oui_ipv4));
  193. switch (prot) {
  194. case ETH_P_IP:
  195. skb_copy_to_linear_data(skb, llc_oui_ipv4,
  196. sizeof(llc_oui_ipv4));
  197. break;
  198. case ETH_P_IPV6:
  199. skb_copy_to_linear_data(skb, llc_oui_ipv6,
  200. sizeof(llc_oui_ipv6));
  201. break;
  202. default:
  203. dev_kfree_skb(skb);
  204. return 0;
  205. }
  206. }
  207. } else { /* e_vc */
  208. if (brdev->payload == p_bridged) {
  209. skb_push(skb, 2);
  210. memset(skb->data, 0, 2);
  211. } else { /* p_routed */
  212. skb_pull(skb, ETH_HLEN);
  213. }
  214. }
  215. skb_debug(skb);
  216. ATM_SKB(skb)->vcc = atmvcc = brvcc->atmvcc;
  217. pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, atmvcc, atmvcc->dev);
  218. atomic_add(skb->truesize, &sk_atm(atmvcc)->sk_wmem_alloc);
  219. ATM_SKB(skb)->atm_options = atmvcc->atm_options;
  220. dev->stats.tx_packets++;
  221. dev->stats.tx_bytes += skb->len;
  222. atmvcc->send(atmvcc, skb);
  223. if (!atm_may_send(atmvcc, 0)) {
  224. netif_stop_queue(brvcc->device);
  225. /*check for race with br2684_pop*/
  226. if (atm_may_send(atmvcc, 0))
  227. netif_start_queue(brvcc->device);
  228. }
  229. return 1;
  230. }
  231. static inline struct br2684_vcc *pick_outgoing_vcc(const struct sk_buff *skb,
  232. const struct br2684_dev *brdev)
  233. {
  234. return list_empty(&brdev->brvccs) ? NULL : list_entry_brvcc(brdev->brvccs.next); /* 1 vcc/dev right now */
  235. }
  236. static netdev_tx_t br2684_start_xmit(struct sk_buff *skb,
  237. struct net_device *dev)
  238. {
  239. struct br2684_dev *brdev = BRPRIV(dev);
  240. struct br2684_vcc *brvcc;
  241. pr_debug("skb_dst(skb)=%p\n", skb_dst(skb));
  242. read_lock(&devs_lock);
  243. brvcc = pick_outgoing_vcc(skb, brdev);
  244. if (brvcc == NULL) {
  245. pr_debug("no vcc attached to dev %s\n", dev->name);
  246. dev->stats.tx_errors++;
  247. dev->stats.tx_carrier_errors++;
  248. /* netif_stop_queue(dev); */
  249. dev_kfree_skb(skb);
  250. read_unlock(&devs_lock);
  251. return NETDEV_TX_OK;
  252. }
  253. if (!br2684_xmit_vcc(skb, dev, brvcc)) {
  254. /*
  255. * We should probably use netif_*_queue() here, but that
  256. * involves added complication. We need to walk before
  257. * we can run.
  258. *
  259. * Don't free here! this pointer might be no longer valid!
  260. */
  261. dev->stats.tx_errors++;
  262. dev->stats.tx_fifo_errors++;
  263. }
  264. read_unlock(&devs_lock);
  265. return NETDEV_TX_OK;
  266. }
  267. /*
  268. * We remember when the MAC gets set, so we don't override it later with
  269. * the ESI of the ATM card of the first VC
  270. */
  271. static int br2684_mac_addr(struct net_device *dev, void *p)
  272. {
  273. int err = eth_mac_addr(dev, p);
  274. if (!err)
  275. BRPRIV(dev)->mac_was_set = 1;
  276. return err;
  277. }
  278. #ifdef CONFIG_ATM_BR2684_IPFILTER
  279. /* this IOCTL is experimental. */
  280. static int br2684_setfilt(struct atm_vcc *atmvcc, void __user * arg)
  281. {
  282. struct br2684_vcc *brvcc;
  283. struct br2684_filter_set fs;
  284. if (copy_from_user(&fs, arg, sizeof fs))
  285. return -EFAULT;
  286. if (fs.ifspec.method != BR2684_FIND_BYNOTHING) {
  287. /*
  288. * This is really a per-vcc thing, but we can also search
  289. * by device.
  290. */
  291. struct br2684_dev *brdev;
  292. read_lock(&devs_lock);
  293. brdev = BRPRIV(br2684_find_dev(&fs.ifspec));
  294. if (brdev == NULL || list_empty(&brdev->brvccs) ||
  295. brdev->brvccs.next != brdev->brvccs.prev) /* >1 VCC */
  296. brvcc = NULL;
  297. else
  298. brvcc = list_entry_brvcc(brdev->brvccs.next);
  299. read_unlock(&devs_lock);
  300. if (brvcc == NULL)
  301. return -ESRCH;
  302. } else
  303. brvcc = BR2684_VCC(atmvcc);
  304. memcpy(&brvcc->filter, &fs.filter, sizeof(brvcc->filter));
  305. return 0;
  306. }
  307. /* Returns 1 if packet should be dropped */
  308. static inline int
  309. packet_fails_filter(__be16 type, struct br2684_vcc *brvcc, struct sk_buff *skb)
  310. {
  311. if (brvcc->filter.netmask == 0)
  312. return 0; /* no filter in place */
  313. if (type == htons(ETH_P_IP) &&
  314. (((struct iphdr *)(skb->data))->daddr & brvcc->filter.
  315. netmask) == brvcc->filter.prefix)
  316. return 0;
  317. if (type == htons(ETH_P_ARP))
  318. return 0;
  319. /*
  320. * TODO: we should probably filter ARPs too.. don't want to have
  321. * them returning values that don't make sense, or is that ok?
  322. */
  323. return 1; /* drop */
  324. }
  325. #endif /* CONFIG_ATM_BR2684_IPFILTER */
  326. static void br2684_close_vcc(struct br2684_vcc *brvcc)
  327. {
  328. pr_debug("removing VCC %p from dev %p\n", brvcc, brvcc->device);
  329. write_lock_irq(&devs_lock);
  330. list_del(&brvcc->brvccs);
  331. write_unlock_irq(&devs_lock);
  332. brvcc->atmvcc->user_back = NULL; /* what about vcc->recvq ??? */
  333. brvcc->old_push(brvcc->atmvcc, NULL); /* pass on the bad news */
  334. kfree(brvcc);
  335. module_put(THIS_MODULE);
  336. }
  337. /* when AAL5 PDU comes in: */
  338. static void br2684_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
  339. {
  340. struct br2684_vcc *brvcc = BR2684_VCC(atmvcc);
  341. struct net_device *net_dev = brvcc->device;
  342. struct br2684_dev *brdev = BRPRIV(net_dev);
  343. pr_debug("\n");
  344. if (unlikely(skb == NULL)) {
  345. /* skb==NULL means VCC is being destroyed */
  346. br2684_close_vcc(brvcc);
  347. if (list_empty(&brdev->brvccs)) {
  348. write_lock_irq(&devs_lock);
  349. list_del(&brdev->br2684_devs);
  350. write_unlock_irq(&devs_lock);
  351. unregister_netdev(net_dev);
  352. free_netdev(net_dev);
  353. }
  354. read_lock_irq(&devs_lock);
  355. if (list_empty(&br2684_devs)) {
  356. /* last br2684 device */
  357. unregister_atmdevice_notifier(&atm_dev_notifier);
  358. }
  359. read_unlock_irq(&devs_lock);
  360. return;
  361. }
  362. skb_debug(skb);
  363. atm_return(atmvcc, skb->truesize);
  364. pr_debug("skb from brdev %p\n", brdev);
  365. if (brvcc->encaps == e_llc) {
  366. if (skb->len > 7 && skb->data[7] == 0x01)
  367. __skb_trim(skb, skb->len - 4);
  368. /* accept packets that have "ipv[46]" in the snap header */
  369. if ((skb->len >= (sizeof(llc_oui_ipv4))) &&
  370. (memcmp(skb->data, llc_oui_ipv4,
  371. sizeof(llc_oui_ipv4) - BR2684_ETHERTYPE_LEN) == 0)) {
  372. if (memcmp(skb->data + 6, ethertype_ipv6,
  373. sizeof(ethertype_ipv6)) == 0)
  374. skb->protocol = htons(ETH_P_IPV6);
  375. else if (memcmp(skb->data + 6, ethertype_ipv4,
  376. sizeof(ethertype_ipv4)) == 0)
  377. skb->protocol = htons(ETH_P_IP);
  378. else
  379. goto error;
  380. skb_pull(skb, sizeof(llc_oui_ipv4));
  381. skb_reset_network_header(skb);
  382. skb->pkt_type = PACKET_HOST;
  383. /*
  384. * Let us waste some time for checking the encapsulation.
  385. * Note, that only 7 char is checked so frames with a valid FCS
  386. * are also accepted (but FCS is not checked of course).
  387. */
  388. } else if ((skb->len >= sizeof(llc_oui_pid_pad)) &&
  389. (memcmp(skb->data, llc_oui_pid_pad, 7) == 0)) {
  390. skb_pull(skb, sizeof(llc_oui_pid_pad));
  391. skb->protocol = eth_type_trans(skb, net_dev);
  392. } else
  393. goto error;
  394. } else { /* e_vc */
  395. if (brdev->payload == p_routed) {
  396. struct iphdr *iph;
  397. skb_reset_network_header(skb);
  398. iph = ip_hdr(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);
  403. else
  404. goto error;
  405. skb->pkt_type = PACKET_HOST;
  406. } else { /* p_bridged */
  407. /* first 2 chars should be 0 */
  408. if (*((u16 *) (skb->data)) != 0)
  409. goto error;
  410. skb_pull(skb, BR2684_PAD_LEN);
  411. skb->protocol = eth_type_trans(skb, net_dev);
  412. }
  413. }
  414. #ifdef CONFIG_ATM_BR2684_IPFILTER
  415. if (unlikely(packet_fails_filter(skb->protocol, brvcc, skb)))
  416. goto dropped;
  417. #endif /* CONFIG_ATM_BR2684_IPFILTER */
  418. skb->dev = net_dev;
  419. ATM_SKB(skb)->vcc = atmvcc; /* needed ? */
  420. pr_debug("received packet's protocol: %x\n", ntohs(skb->protocol));
  421. skb_debug(skb);
  422. /* sigh, interface is down? */
  423. if (unlikely(!(net_dev->flags & IFF_UP)))
  424. goto dropped;
  425. net_dev->stats.rx_packets++;
  426. net_dev->stats.rx_bytes += skb->len;
  427. memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
  428. netif_rx(skb);
  429. return;
  430. dropped:
  431. net_dev->stats.rx_dropped++;
  432. goto free_skb;
  433. error:
  434. net_dev->stats.rx_errors++;
  435. free_skb:
  436. dev_kfree_skb(skb);
  437. }
  438. /*
  439. * Assign a vcc to a dev
  440. * Note: we do not have explicit unassign, but look at _push()
  441. */
  442. static int br2684_regvcc(struct atm_vcc *atmvcc, void __user * arg)
  443. {
  444. struct sk_buff_head queue;
  445. int err;
  446. struct br2684_vcc *brvcc;
  447. struct sk_buff *skb, *tmp;
  448. struct sk_buff_head *rq;
  449. struct br2684_dev *brdev;
  450. struct net_device *net_dev;
  451. struct atm_backend_br2684 be;
  452. unsigned long flags;
  453. if (copy_from_user(&be, arg, sizeof be))
  454. return -EFAULT;
  455. brvcc = kzalloc(sizeof(struct br2684_vcc), GFP_KERNEL);
  456. if (!brvcc)
  457. return -ENOMEM;
  458. write_lock_irq(&devs_lock);
  459. net_dev = br2684_find_dev(&be.ifspec);
  460. if (net_dev == NULL) {
  461. pr_err("tried to attach to non-existant device\n");
  462. err = -ENXIO;
  463. goto error;
  464. }
  465. brdev = BRPRIV(net_dev);
  466. if (atmvcc->push == NULL) {
  467. err = -EBADFD;
  468. goto error;
  469. }
  470. if (!list_empty(&brdev->brvccs)) {
  471. /* Only 1 VCC/dev right now */
  472. err = -EEXIST;
  473. goto error;
  474. }
  475. if (be.fcs_in != BR2684_FCSIN_NO ||
  476. be.fcs_out != BR2684_FCSOUT_NO ||
  477. be.fcs_auto || be.has_vpiid || be.send_padding ||
  478. (be.encaps != BR2684_ENCAPS_VC &&
  479. be.encaps != BR2684_ENCAPS_LLC) ||
  480. be.min_size != 0) {
  481. err = -EINVAL;
  482. goto error;
  483. }
  484. pr_debug("vcc=%p, encaps=%d, brvcc=%p\n", atmvcc, be.encaps, brvcc);
  485. if (list_empty(&brdev->brvccs) && !brdev->mac_was_set) {
  486. unsigned char *esi = atmvcc->dev->esi;
  487. if (esi[0] | esi[1] | esi[2] | esi[3] | esi[4] | esi[5])
  488. memcpy(net_dev->dev_addr, esi, net_dev->addr_len);
  489. else
  490. net_dev->dev_addr[2] = 1;
  491. }
  492. list_add(&brvcc->brvccs, &brdev->brvccs);
  493. write_unlock_irq(&devs_lock);
  494. brvcc->device = net_dev;
  495. brvcc->atmvcc = atmvcc;
  496. atmvcc->user_back = brvcc;
  497. brvcc->encaps = (enum br2684_encaps)be.encaps;
  498. brvcc->old_push = atmvcc->push;
  499. brvcc->old_pop = atmvcc->pop;
  500. barrier();
  501. atmvcc->push = br2684_push;
  502. atmvcc->pop = br2684_pop;
  503. __skb_queue_head_init(&queue);
  504. rq = &sk_atm(atmvcc)->sk_receive_queue;
  505. spin_lock_irqsave(&rq->lock, flags);
  506. skb_queue_splice_init(rq, &queue);
  507. spin_unlock_irqrestore(&rq->lock, flags);
  508. skb_queue_walk_safe(&queue, skb, tmp) {
  509. struct net_device *dev = skb->dev;
  510. dev->stats.rx_bytes -= skb->len;
  511. dev->stats.rx_packets--;
  512. br2684_push(atmvcc, skb);
  513. }
  514. /* initialize netdev carrier state */
  515. if (atmvcc->dev->signal == ATM_PHY_SIG_LOST)
  516. netif_carrier_off(net_dev);
  517. else
  518. netif_carrier_on(net_dev);
  519. __module_get(THIS_MODULE);
  520. return 0;
  521. error:
  522. write_unlock_irq(&devs_lock);
  523. kfree(brvcc);
  524. return err;
  525. }
  526. static const struct net_device_ops br2684_netdev_ops = {
  527. .ndo_start_xmit = br2684_start_xmit,
  528. .ndo_set_mac_address = br2684_mac_addr,
  529. .ndo_change_mtu = eth_change_mtu,
  530. .ndo_validate_addr = eth_validate_addr,
  531. };
  532. static const struct net_device_ops br2684_netdev_ops_routed = {
  533. .ndo_start_xmit = br2684_start_xmit,
  534. .ndo_set_mac_address = br2684_mac_addr,
  535. .ndo_change_mtu = eth_change_mtu
  536. };
  537. static void br2684_setup(struct net_device *netdev)
  538. {
  539. struct br2684_dev *brdev = BRPRIV(netdev);
  540. ether_setup(netdev);
  541. brdev->net_dev = netdev;
  542. netdev->netdev_ops = &br2684_netdev_ops;
  543. INIT_LIST_HEAD(&brdev->brvccs);
  544. }
  545. static void br2684_setup_routed(struct net_device *netdev)
  546. {
  547. struct br2684_dev *brdev = BRPRIV(netdev);
  548. brdev->net_dev = netdev;
  549. netdev->hard_header_len = 0;
  550. netdev->netdev_ops = &br2684_netdev_ops_routed;
  551. netdev->addr_len = 0;
  552. netdev->mtu = 1500;
  553. netdev->type = ARPHRD_PPP;
  554. netdev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  555. netdev->tx_queue_len = 100;
  556. INIT_LIST_HEAD(&brdev->brvccs);
  557. }
  558. static int br2684_create(void __user *arg)
  559. {
  560. int err;
  561. struct net_device *netdev;
  562. struct br2684_dev *brdev;
  563. struct atm_newif_br2684 ni;
  564. enum br2684_payload payload;
  565. pr_debug("\n");
  566. if (copy_from_user(&ni, arg, sizeof ni))
  567. return -EFAULT;
  568. if (ni.media & BR2684_FLAG_ROUTED)
  569. payload = p_routed;
  570. else
  571. payload = p_bridged;
  572. ni.media &= 0xffff; /* strip flags */
  573. if (ni.media != BR2684_MEDIA_ETHERNET || ni.mtu != 1500)
  574. return -EINVAL;
  575. netdev = alloc_netdev(sizeof(struct br2684_dev),
  576. ni.ifname[0] ? ni.ifname : "nas%d",
  577. (payload == p_routed) ?
  578. br2684_setup_routed : br2684_setup);
  579. if (!netdev)
  580. return -ENOMEM;
  581. brdev = BRPRIV(netdev);
  582. pr_debug("registered netdev %s\n", netdev->name);
  583. /* open, stop, do_ioctl ? */
  584. err = register_netdev(netdev);
  585. if (err < 0) {
  586. pr_err("register_netdev failed\n");
  587. free_netdev(netdev);
  588. return err;
  589. }
  590. write_lock_irq(&devs_lock);
  591. brdev->payload = payload;
  592. if (list_empty(&br2684_devs)) {
  593. /* 1st br2684 device */
  594. register_atmdevice_notifier(&atm_dev_notifier);
  595. brdev->number = 1;
  596. } else
  597. brdev->number = BRPRIV(list_entry_brdev(br2684_devs.prev))->number + 1;
  598. list_add_tail(&brdev->br2684_devs, &br2684_devs);
  599. write_unlock_irq(&devs_lock);
  600. return 0;
  601. }
  602. /*
  603. * This handles ioctls actually performed on our vcc - we must return
  604. * -ENOIOCTLCMD for any unrecognized ioctl
  605. */
  606. static int br2684_ioctl(struct socket *sock, unsigned int cmd,
  607. unsigned long arg)
  608. {
  609. struct atm_vcc *atmvcc = ATM_SD(sock);
  610. void __user *argp = (void __user *)arg;
  611. atm_backend_t b;
  612. int err;
  613. switch (cmd) {
  614. case ATM_SETBACKEND:
  615. case ATM_NEWBACKENDIF:
  616. err = get_user(b, (atm_backend_t __user *) argp);
  617. if (err)
  618. return -EFAULT;
  619. if (b != ATM_BACKEND_BR2684)
  620. return -ENOIOCTLCMD;
  621. if (!capable(CAP_NET_ADMIN))
  622. return -EPERM;
  623. if (cmd == ATM_SETBACKEND)
  624. return br2684_regvcc(atmvcc, argp);
  625. else
  626. return br2684_create(argp);
  627. #ifdef CONFIG_ATM_BR2684_IPFILTER
  628. case BR2684_SETFILT:
  629. if (atmvcc->push != br2684_push)
  630. return -ENOIOCTLCMD;
  631. if (!capable(CAP_NET_ADMIN))
  632. return -EPERM;
  633. err = br2684_setfilt(atmvcc, argp);
  634. return err;
  635. #endif /* CONFIG_ATM_BR2684_IPFILTER */
  636. }
  637. return -ENOIOCTLCMD;
  638. }
  639. static struct atm_ioctl br2684_ioctl_ops = {
  640. .owner = THIS_MODULE,
  641. .ioctl = br2684_ioctl,
  642. };
  643. #ifdef CONFIG_PROC_FS
  644. static void *br2684_seq_start(struct seq_file *seq, loff_t * pos)
  645. __acquires(devs_lock)
  646. {
  647. read_lock(&devs_lock);
  648. return seq_list_start(&br2684_devs, *pos);
  649. }
  650. static void *br2684_seq_next(struct seq_file *seq, void *v, loff_t * pos)
  651. {
  652. return seq_list_next(v, &br2684_devs, pos);
  653. }
  654. static void br2684_seq_stop(struct seq_file *seq, void *v)
  655. __releases(devs_lock)
  656. {
  657. read_unlock(&devs_lock);
  658. }
  659. static int br2684_seq_show(struct seq_file *seq, void *v)
  660. {
  661. const struct br2684_dev *brdev = list_entry(v, struct br2684_dev,
  662. br2684_devs);
  663. const struct net_device *net_dev = brdev->net_dev;
  664. const struct br2684_vcc *brvcc;
  665. seq_printf(seq, "dev %.16s: num=%d, mac=%pM (%s)\n",
  666. net_dev->name,
  667. brdev->number,
  668. net_dev->dev_addr,
  669. brdev->mac_was_set ? "set" : "auto");
  670. list_for_each_entry(brvcc, &brdev->brvccs, brvccs) {
  671. seq_printf(seq, " vcc %d.%d.%d: encaps=%s payload=%s"
  672. ", failed copies %u/%u"
  673. "\n", brvcc->atmvcc->dev->number,
  674. brvcc->atmvcc->vpi, brvcc->atmvcc->vci,
  675. (brvcc->encaps == e_llc) ? "LLC" : "VC",
  676. (brdev->payload == p_bridged) ? "bridged" : "routed",
  677. brvcc->copies_failed, brvcc->copies_needed);
  678. #ifdef CONFIG_ATM_BR2684_IPFILTER
  679. #define b1(var, byte) ((u8 *) &brvcc->filter.var)[byte]
  680. #define bs(var) b1(var, 0), b1(var, 1), b1(var, 2), b1(var, 3)
  681. if (brvcc->filter.netmask != 0)
  682. seq_printf(seq, " filter=%d.%d.%d.%d/"
  683. "%d.%d.%d.%d\n", bs(prefix), bs(netmask));
  684. #undef bs
  685. #undef b1
  686. #endif /* CONFIG_ATM_BR2684_IPFILTER */
  687. }
  688. return 0;
  689. }
  690. static const struct seq_operations br2684_seq_ops = {
  691. .start = br2684_seq_start,
  692. .next = br2684_seq_next,
  693. .stop = br2684_seq_stop,
  694. .show = br2684_seq_show,
  695. };
  696. static int br2684_proc_open(struct inode *inode, struct file *file)
  697. {
  698. return seq_open(file, &br2684_seq_ops);
  699. }
  700. static const struct file_operations br2684_proc_ops = {
  701. .owner = THIS_MODULE,
  702. .open = br2684_proc_open,
  703. .read = seq_read,
  704. .llseek = seq_lseek,
  705. .release = seq_release,
  706. };
  707. extern struct proc_dir_entry *atm_proc_root; /* from proc.c */
  708. #endif /* CONFIG_PROC_FS */
  709. static int __init br2684_init(void)
  710. {
  711. #ifdef CONFIG_PROC_FS
  712. struct proc_dir_entry *p;
  713. p = proc_create("br2684", 0, atm_proc_root, &br2684_proc_ops);
  714. if (p == NULL)
  715. return -ENOMEM;
  716. #endif
  717. register_atm_ioctl(&br2684_ioctl_ops);
  718. return 0;
  719. }
  720. static void __exit br2684_exit(void)
  721. {
  722. struct net_device *net_dev;
  723. struct br2684_dev *brdev;
  724. struct br2684_vcc *brvcc;
  725. deregister_atm_ioctl(&br2684_ioctl_ops);
  726. #ifdef CONFIG_PROC_FS
  727. remove_proc_entry("br2684", atm_proc_root);
  728. #endif
  729. /* if not already empty */
  730. if (!list_empty(&br2684_devs))
  731. unregister_atmdevice_notifier(&atm_dev_notifier);
  732. while (!list_empty(&br2684_devs)) {
  733. net_dev = list_entry_brdev(br2684_devs.next);
  734. brdev = BRPRIV(net_dev);
  735. while (!list_empty(&brdev->brvccs)) {
  736. brvcc = list_entry_brvcc(brdev->brvccs.next);
  737. br2684_close_vcc(brvcc);
  738. }
  739. list_del(&brdev->br2684_devs);
  740. unregister_netdev(net_dev);
  741. free_netdev(net_dev);
  742. }
  743. }
  744. module_init(br2684_init);
  745. module_exit(br2684_exit);
  746. MODULE_AUTHOR("Marcell GAL");
  747. MODULE_DESCRIPTION("RFC2684 bridged protocols over ATM/AAL5");
  748. MODULE_LICENSE("GPL");