br2684.c 22 KB

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