vlan.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * INET 802.1Q VLAN
  3. * Ethernet-type device handling.
  4. *
  5. * Authors: Ben Greear <greearb@candelatech.com>
  6. * Please send support related email to: vlan@scry.wanfear.com
  7. * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
  8. *
  9. * Fixes:
  10. * Fix for packet capture - Nick Eggleston <nick@dccinc.com>;
  11. * Add HW acceleration hooks - David S. Miller <davem@redhat.com>;
  12. * Correct all the locking - David S. Miller <davem@redhat.com>;
  13. * Use hash table for VLAN groups - David S. Miller <davem@redhat.com>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <asm/uaccess.h> /* for copy_from_user */
  21. #include <linux/capability.h>
  22. #include <linux/module.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <net/datalink.h>
  26. #include <linux/mm.h>
  27. #include <linux/in.h>
  28. #include <linux/init.h>
  29. #include <net/p8022.h>
  30. #include <net/arp.h>
  31. #include <linux/rtnetlink.h>
  32. #include <linux/notifier.h>
  33. #include <net/net_namespace.h>
  34. #include <linux/if_vlan.h>
  35. #include "vlan.h"
  36. #include "vlanproc.h"
  37. #define DRV_VERSION "1.8"
  38. /* Global VLAN variables */
  39. /* Our listing of VLAN group(s) */
  40. static struct hlist_head vlan_group_hash[VLAN_GRP_HASH_SIZE];
  41. #define vlan_grp_hashfn(IDX) ((((IDX) >> VLAN_GRP_HASH_SHIFT) ^ (IDX)) & VLAN_GRP_HASH_MASK)
  42. static char vlan_fullname[] = "802.1Q VLAN Support";
  43. static char vlan_version[] = DRV_VERSION;
  44. static char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>";
  45. static char vlan_buggyright[] = "David S. Miller <davem@redhat.com>";
  46. /* Determines interface naming scheme. */
  47. unsigned short vlan_name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
  48. static struct packet_type vlan_packet_type = {
  49. .type = __constant_htons(ETH_P_8021Q),
  50. .func = vlan_skb_recv, /* VLAN receive method */
  51. };
  52. /* End of global variables definitions. */
  53. /* Must be invoked with RCU read lock (no preempt) */
  54. static struct vlan_group *__vlan_find_group(int real_dev_ifindex)
  55. {
  56. struct vlan_group *grp;
  57. struct hlist_node *n;
  58. int hash = vlan_grp_hashfn(real_dev_ifindex);
  59. hlist_for_each_entry_rcu(grp, n, &vlan_group_hash[hash], hlist) {
  60. if (grp->real_dev_ifindex == real_dev_ifindex)
  61. return grp;
  62. }
  63. return NULL;
  64. }
  65. /* Find the protocol handler. Assumes VID < VLAN_VID_MASK.
  66. *
  67. * Must be invoked with RCU read lock (no preempt)
  68. */
  69. struct net_device *__find_vlan_dev(struct net_device *real_dev,
  70. unsigned short VID)
  71. {
  72. struct vlan_group *grp = __vlan_find_group(real_dev->ifindex);
  73. if (grp)
  74. return vlan_group_get_device(grp, VID);
  75. return NULL;
  76. }
  77. static void vlan_group_free(struct vlan_group *grp)
  78. {
  79. int i;
  80. for (i=0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
  81. kfree(grp->vlan_devices_arrays[i]);
  82. kfree(grp);
  83. }
  84. static struct vlan_group *vlan_group_alloc(int ifindex)
  85. {
  86. struct vlan_group *grp;
  87. unsigned int size;
  88. unsigned int i;
  89. grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL);
  90. if (!grp)
  91. return NULL;
  92. size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
  93. for (i = 0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++) {
  94. grp->vlan_devices_arrays[i] = kzalloc(size, GFP_KERNEL);
  95. if (!grp->vlan_devices_arrays[i])
  96. goto err;
  97. }
  98. grp->real_dev_ifindex = ifindex;
  99. hlist_add_head_rcu(&grp->hlist,
  100. &vlan_group_hash[vlan_grp_hashfn(ifindex)]);
  101. return grp;
  102. err:
  103. vlan_group_free(grp);
  104. return NULL;
  105. }
  106. static void vlan_rcu_free(struct rcu_head *rcu)
  107. {
  108. vlan_group_free(container_of(rcu, struct vlan_group, rcu));
  109. }
  110. void unregister_vlan_dev(struct net_device *dev)
  111. {
  112. struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
  113. struct net_device *real_dev = vlan->real_dev;
  114. struct vlan_group *grp;
  115. unsigned short vlan_id = vlan->vlan_id;
  116. ASSERT_RTNL();
  117. grp = __vlan_find_group(real_dev->ifindex);
  118. BUG_ON(!grp);
  119. vlan_proc_rem_dev(dev);
  120. /* Take it out of our own structures, but be sure to interlock with
  121. * HW accelerating devices or SW vlan input packet processing.
  122. */
  123. if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
  124. real_dev->vlan_rx_kill_vid(real_dev, vlan_id);
  125. vlan_group_set_device(grp, vlan_id, NULL);
  126. grp->nr_vlans--;
  127. synchronize_net();
  128. /* If the group is now empty, kill off the group. */
  129. if (grp->nr_vlans == 0) {
  130. if (real_dev->features & NETIF_F_HW_VLAN_RX)
  131. real_dev->vlan_rx_register(real_dev, NULL);
  132. hlist_del_rcu(&grp->hlist);
  133. /* Free the group, after all cpu's are done. */
  134. call_rcu(&grp->rcu, vlan_rcu_free);
  135. }
  136. /* Get rid of the vlan's reference to real_dev */
  137. dev_put(real_dev);
  138. unregister_netdevice(dev);
  139. }
  140. static void vlan_transfer_operstate(const struct net_device *dev, struct net_device *vlandev)
  141. {
  142. /* Have to respect userspace enforced dormant state
  143. * of real device, also must allow supplicant running
  144. * on VLAN device
  145. */
  146. if (dev->operstate == IF_OPER_DORMANT)
  147. netif_dormant_on(vlandev);
  148. else
  149. netif_dormant_off(vlandev);
  150. if (netif_carrier_ok(dev)) {
  151. if (!netif_carrier_ok(vlandev))
  152. netif_carrier_on(vlandev);
  153. } else {
  154. if (netif_carrier_ok(vlandev))
  155. netif_carrier_off(vlandev);
  156. }
  157. }
  158. int vlan_check_real_dev(struct net_device *real_dev, unsigned short vlan_id)
  159. {
  160. char *name = real_dev->name;
  161. if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
  162. pr_info("8021q: VLANs not supported on %s\n", name);
  163. return -EOPNOTSUPP;
  164. }
  165. if ((real_dev->features & NETIF_F_HW_VLAN_RX) &&
  166. !real_dev->vlan_rx_register) {
  167. pr_info("8021q: device %s has buggy VLAN hw accel\n", name);
  168. return -EOPNOTSUPP;
  169. }
  170. if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
  171. (!real_dev->vlan_rx_add_vid || !real_dev->vlan_rx_kill_vid)) {
  172. pr_info("8021q: Device %s has buggy VLAN hw accel\n", name);
  173. return -EOPNOTSUPP;
  174. }
  175. /* The real device must be up and operating in order to
  176. * assosciate a VLAN device with it.
  177. */
  178. if (!(real_dev->flags & IFF_UP))
  179. return -ENETDOWN;
  180. if (__find_vlan_dev(real_dev, vlan_id) != NULL)
  181. return -EEXIST;
  182. return 0;
  183. }
  184. int register_vlan_dev(struct net_device *dev)
  185. {
  186. struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev);
  187. struct net_device *real_dev = vlan->real_dev;
  188. unsigned short vlan_id = vlan->vlan_id;
  189. struct vlan_group *grp, *ngrp = NULL;
  190. int err;
  191. grp = __vlan_find_group(real_dev->ifindex);
  192. if (!grp) {
  193. ngrp = grp = vlan_group_alloc(real_dev->ifindex);
  194. if (!grp)
  195. return -ENOBUFS;
  196. }
  197. err = register_netdevice(dev);
  198. if (err < 0)
  199. goto out_free_group;
  200. /* Account for reference in struct vlan_dev_info */
  201. dev_hold(real_dev);
  202. vlan_transfer_operstate(real_dev, dev);
  203. linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
  204. /* So, got the sucker initialized, now lets place
  205. * it into our local structure.
  206. */
  207. vlan_group_set_device(grp, vlan_id, dev);
  208. grp->nr_vlans++;
  209. if (ngrp && real_dev->features & NETIF_F_HW_VLAN_RX)
  210. real_dev->vlan_rx_register(real_dev, ngrp);
  211. if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
  212. real_dev->vlan_rx_add_vid(real_dev, vlan_id);
  213. if (vlan_proc_add_dev(dev) < 0)
  214. pr_warning("8021q: failed to add proc entry for %s\n",
  215. dev->name);
  216. return 0;
  217. out_free_group:
  218. if (ngrp)
  219. vlan_group_free(ngrp);
  220. return err;
  221. }
  222. /* Attach a VLAN device to a mac address (ie Ethernet Card).
  223. * Returns 0 if the device was created or a negative error code otherwise.
  224. */
  225. static int register_vlan_device(struct net_device *real_dev,
  226. unsigned short VLAN_ID)
  227. {
  228. struct net_device *new_dev;
  229. char name[IFNAMSIZ];
  230. int err;
  231. if (VLAN_ID >= VLAN_VID_MASK)
  232. return -ERANGE;
  233. err = vlan_check_real_dev(real_dev, VLAN_ID);
  234. if (err < 0)
  235. return err;
  236. /* Gotta set up the fields for the device. */
  237. switch (vlan_name_type) {
  238. case VLAN_NAME_TYPE_RAW_PLUS_VID:
  239. /* name will look like: eth1.0005 */
  240. snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, VLAN_ID);
  241. break;
  242. case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
  243. /* Put our vlan.VID in the name.
  244. * Name will look like: vlan5
  245. */
  246. snprintf(name, IFNAMSIZ, "vlan%i", VLAN_ID);
  247. break;
  248. case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
  249. /* Put our vlan.VID in the name.
  250. * Name will look like: eth0.5
  251. */
  252. snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, VLAN_ID);
  253. break;
  254. case VLAN_NAME_TYPE_PLUS_VID:
  255. /* Put our vlan.VID in the name.
  256. * Name will look like: vlan0005
  257. */
  258. default:
  259. snprintf(name, IFNAMSIZ, "vlan%.4i", VLAN_ID);
  260. }
  261. new_dev = alloc_netdev(sizeof(struct vlan_dev_info), name,
  262. vlan_setup);
  263. if (new_dev == NULL)
  264. return -ENOBUFS;
  265. /* need 4 bytes for extra VLAN header info,
  266. * hope the underlying device can handle it.
  267. */
  268. new_dev->mtu = real_dev->mtu;
  269. VLAN_DEV_INFO(new_dev)->vlan_id = VLAN_ID; /* 1 through VLAN_VID_MASK */
  270. VLAN_DEV_INFO(new_dev)->real_dev = real_dev;
  271. VLAN_DEV_INFO(new_dev)->dent = NULL;
  272. VLAN_DEV_INFO(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
  273. new_dev->rtnl_link_ops = &vlan_link_ops;
  274. err = register_vlan_dev(new_dev);
  275. if (err < 0)
  276. goto out_free_newdev;
  277. return 0;
  278. out_free_newdev:
  279. free_netdev(new_dev);
  280. return err;
  281. }
  282. static void vlan_sync_address(struct net_device *dev,
  283. struct net_device *vlandev)
  284. {
  285. struct vlan_dev_info *vlan = VLAN_DEV_INFO(vlandev);
  286. /* May be called without an actual change */
  287. if (!compare_ether_addr(vlan->real_dev_addr, dev->dev_addr))
  288. return;
  289. /* vlan address was different from the old address and is equal to
  290. * the new address */
  291. if (compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
  292. !compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
  293. dev_unicast_delete(dev, vlandev->dev_addr, ETH_ALEN);
  294. /* vlan address was equal to the old address and is different from
  295. * the new address */
  296. if (!compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
  297. compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
  298. dev_unicast_add(dev, vlandev->dev_addr, ETH_ALEN);
  299. memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
  300. }
  301. static int vlan_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
  302. {
  303. struct net_device *dev = ptr;
  304. struct vlan_group *grp = __vlan_find_group(dev->ifindex);
  305. int i, flgs;
  306. struct net_device *vlandev;
  307. if (dev->nd_net != &init_net)
  308. return NOTIFY_DONE;
  309. if (!grp)
  310. goto out;
  311. /* It is OK that we do not hold the group lock right now,
  312. * as we run under the RTNL lock.
  313. */
  314. switch (event) {
  315. case NETDEV_CHANGE:
  316. /* Propagate real device state to vlan devices */
  317. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  318. vlandev = vlan_group_get_device(grp, i);
  319. if (!vlandev)
  320. continue;
  321. vlan_transfer_operstate(dev, vlandev);
  322. }
  323. break;
  324. case NETDEV_CHANGEADDR:
  325. /* Adjust unicast filters on underlying device */
  326. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  327. vlandev = vlan_group_get_device(grp, i);
  328. if (!vlandev)
  329. continue;
  330. flgs = vlandev->flags;
  331. if (!(flgs & IFF_UP))
  332. continue;
  333. vlan_sync_address(dev, vlandev);
  334. }
  335. break;
  336. case NETDEV_DOWN:
  337. /* Put all VLANs for this dev in the down state too. */
  338. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  339. vlandev = vlan_group_get_device(grp, i);
  340. if (!vlandev)
  341. continue;
  342. flgs = vlandev->flags;
  343. if (!(flgs & IFF_UP))
  344. continue;
  345. dev_change_flags(vlandev, flgs & ~IFF_UP);
  346. }
  347. break;
  348. case NETDEV_UP:
  349. /* Put all VLANs for this dev in the up state too. */
  350. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  351. vlandev = vlan_group_get_device(grp, i);
  352. if (!vlandev)
  353. continue;
  354. flgs = vlandev->flags;
  355. if (flgs & IFF_UP)
  356. continue;
  357. dev_change_flags(vlandev, flgs | IFF_UP);
  358. }
  359. break;
  360. case NETDEV_UNREGISTER:
  361. /* Delete all VLANs for this dev. */
  362. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  363. vlandev = vlan_group_get_device(grp, i);
  364. if (!vlandev)
  365. continue;
  366. /* unregistration of last vlan destroys group, abort
  367. * afterwards */
  368. if (grp->nr_vlans == 1)
  369. i = VLAN_GROUP_ARRAY_LEN;
  370. unregister_vlan_dev(vlandev);
  371. }
  372. break;
  373. }
  374. out:
  375. return NOTIFY_DONE;
  376. }
  377. static struct notifier_block vlan_notifier_block __read_mostly = {
  378. .notifier_call = vlan_device_event,
  379. };
  380. /*
  381. * VLAN IOCTL handler.
  382. * o execute requested action or pass command to the device driver
  383. * arg is really a struct vlan_ioctl_args __user *.
  384. */
  385. static int vlan_ioctl_handler(struct net *net, void __user *arg)
  386. {
  387. int err;
  388. unsigned short vid = 0;
  389. struct vlan_ioctl_args args;
  390. struct net_device *dev = NULL;
  391. if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
  392. return -EFAULT;
  393. /* Null terminate this sucker, just in case. */
  394. args.device1[23] = 0;
  395. args.u.device2[23] = 0;
  396. rtnl_lock();
  397. switch (args.cmd) {
  398. case SET_VLAN_INGRESS_PRIORITY_CMD:
  399. case SET_VLAN_EGRESS_PRIORITY_CMD:
  400. case SET_VLAN_FLAG_CMD:
  401. case ADD_VLAN_CMD:
  402. case DEL_VLAN_CMD:
  403. case GET_VLAN_REALDEV_NAME_CMD:
  404. case GET_VLAN_VID_CMD:
  405. err = -ENODEV;
  406. dev = __dev_get_by_name(&init_net, args.device1);
  407. if (!dev)
  408. goto out;
  409. err = -EINVAL;
  410. if (args.cmd != ADD_VLAN_CMD &&
  411. !(dev->priv_flags & IFF_802_1Q_VLAN))
  412. goto out;
  413. }
  414. switch (args.cmd) {
  415. case SET_VLAN_INGRESS_PRIORITY_CMD:
  416. err = -EPERM;
  417. if (!capable(CAP_NET_ADMIN))
  418. break;
  419. vlan_dev_set_ingress_priority(dev,
  420. args.u.skb_priority,
  421. args.vlan_qos);
  422. err = 0;
  423. break;
  424. case SET_VLAN_EGRESS_PRIORITY_CMD:
  425. err = -EPERM;
  426. if (!capable(CAP_NET_ADMIN))
  427. break;
  428. err = vlan_dev_set_egress_priority(dev,
  429. args.u.skb_priority,
  430. args.vlan_qos);
  431. break;
  432. case SET_VLAN_FLAG_CMD:
  433. err = -EPERM;
  434. if (!capable(CAP_NET_ADMIN))
  435. break;
  436. err = vlan_dev_set_vlan_flag(dev,
  437. args.u.flag,
  438. args.vlan_qos);
  439. break;
  440. case SET_VLAN_NAME_TYPE_CMD:
  441. err = -EPERM;
  442. if (!capable(CAP_NET_ADMIN))
  443. break;
  444. if ((args.u.name_type >= 0) &&
  445. (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
  446. vlan_name_type = args.u.name_type;
  447. err = 0;
  448. } else {
  449. err = -EINVAL;
  450. }
  451. break;
  452. case ADD_VLAN_CMD:
  453. err = -EPERM;
  454. if (!capable(CAP_NET_ADMIN))
  455. break;
  456. err = register_vlan_device(dev, args.u.VID);
  457. break;
  458. case DEL_VLAN_CMD:
  459. err = -EPERM;
  460. if (!capable(CAP_NET_ADMIN))
  461. break;
  462. unregister_vlan_dev(dev);
  463. err = 0;
  464. break;
  465. case GET_VLAN_REALDEV_NAME_CMD:
  466. err = 0;
  467. vlan_dev_get_realdev_name(dev, args.u.device2);
  468. if (copy_to_user(arg, &args,
  469. sizeof(struct vlan_ioctl_args))) {
  470. err = -EFAULT;
  471. }
  472. break;
  473. case GET_VLAN_VID_CMD:
  474. err = 0;
  475. vlan_dev_get_vid(dev, &vid);
  476. args.u.VID = vid;
  477. if (copy_to_user(arg, &args,
  478. sizeof(struct vlan_ioctl_args))) {
  479. err = -EFAULT;
  480. }
  481. break;
  482. default:
  483. err = -EOPNOTSUPP;
  484. break;
  485. }
  486. out:
  487. rtnl_unlock();
  488. return err;
  489. }
  490. static int __init vlan_proto_init(void)
  491. {
  492. int err;
  493. pr_info("%s v%s %s\n", vlan_fullname, vlan_version, vlan_copyright);
  494. pr_info("All bugs added by %s\n", vlan_buggyright);
  495. err = vlan_proc_init();
  496. if (err < 0)
  497. goto err1;
  498. err = register_netdevice_notifier(&vlan_notifier_block);
  499. if (err < 0)
  500. goto err2;
  501. err = vlan_netlink_init();
  502. if (err < 0)
  503. goto err3;
  504. dev_add_pack(&vlan_packet_type);
  505. vlan_ioctl_set(vlan_ioctl_handler);
  506. return 0;
  507. err3:
  508. unregister_netdevice_notifier(&vlan_notifier_block);
  509. err2:
  510. vlan_proc_cleanup();
  511. err1:
  512. return err;
  513. }
  514. static void __exit vlan_cleanup_module(void)
  515. {
  516. unsigned int i;
  517. vlan_ioctl_set(NULL);
  518. vlan_netlink_fini();
  519. unregister_netdevice_notifier(&vlan_notifier_block);
  520. dev_remove_pack(&vlan_packet_type);
  521. /* This table must be empty if there are no module references left. */
  522. for (i = 0; i < VLAN_GRP_HASH_SIZE; i++)
  523. BUG_ON(!hlist_empty(&vlan_group_hash[i]));
  524. vlan_proc_cleanup();
  525. synchronize_net();
  526. }
  527. module_init(vlan_proto_init);
  528. module_exit(vlan_cleanup_module);
  529. MODULE_LICENSE("GPL");
  530. MODULE_VERSION(DRV_VERSION);