br2684.c 23 KB

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