br2684.c 23 KB

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