vlan.c 15 KB

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