vlan_dev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /* -*- linux-c -*-
  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: Mar 22 2001: Martin Bokaemper <mbokaemper@unispherenetworks.com>
  10. * - reset skb->pkt_type on incoming packets when MAC was changed
  11. * - see that changed MAC is saddr for outgoing packets
  12. * Oct 20, 2001: Ard van Breeman:
  13. * - Fix MC-list, finally.
  14. * - Flush MC-list on VLAN destroy.
  15. *
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version
  20. * 2 of the License, or (at your option) any later version.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/ethtool.h>
  27. #include <net/arp.h>
  28. #include "vlan.h"
  29. #include "vlanproc.h"
  30. #include <linux/if_vlan.h>
  31. /*
  32. * Rebuild the Ethernet MAC header. This is called after an ARP
  33. * (or in future other address resolution) has completed on this
  34. * sk_buff. We now let ARP fill in the other fields.
  35. *
  36. * This routine CANNOT use cached dst->neigh!
  37. * Really, it is used only when dst->neigh is wrong.
  38. *
  39. * TODO: This needs a checkup, I'm ignorant here. --BLG
  40. */
  41. static int vlan_dev_rebuild_header(struct sk_buff *skb)
  42. {
  43. struct net_device *dev = skb->dev;
  44. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  45. switch (veth->h_vlan_encapsulated_proto) {
  46. #ifdef CONFIG_INET
  47. case htons(ETH_P_IP):
  48. /* TODO: Confirm this will work with VLAN headers... */
  49. return arp_find(veth->h_dest, skb);
  50. #endif
  51. default:
  52. pr_debug("%s: unable to resolve type %X addresses.\n",
  53. dev->name, ntohs(veth->h_vlan_encapsulated_proto));
  54. memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
  55. break;
  56. }
  57. return 0;
  58. }
  59. static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb)
  60. {
  61. if (vlan_dev_info(skb->dev)->flags & VLAN_FLAG_REORDER_HDR) {
  62. if (skb_cow(skb, skb_headroom(skb)) < 0)
  63. skb = NULL;
  64. if (skb) {
  65. /* Lifted from Gleb's VLAN code... */
  66. memmove(skb->data - ETH_HLEN,
  67. skb->data - VLAN_ETH_HLEN, 12);
  68. skb->mac_header += VLAN_HLEN;
  69. }
  70. }
  71. return skb;
  72. }
  73. static inline void vlan_set_encap_proto(struct sk_buff *skb,
  74. struct vlan_hdr *vhdr)
  75. {
  76. __be16 proto;
  77. unsigned char *rawp;
  78. /*
  79. * Was a VLAN packet, grab the encapsulated protocol, which the layer
  80. * three protocols care about.
  81. */
  82. proto = vhdr->h_vlan_encapsulated_proto;
  83. if (ntohs(proto) >= 1536) {
  84. skb->protocol = proto;
  85. return;
  86. }
  87. rawp = skb->data;
  88. if (*(unsigned short *)rawp == 0xFFFF)
  89. /*
  90. * This is a magic hack to spot IPX packets. Older Novell
  91. * breaks the protocol design and runs IPX over 802.3 without
  92. * an 802.2 LLC layer. We look for FFFF which isn't a used
  93. * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
  94. * but does for the rest.
  95. */
  96. skb->protocol = htons(ETH_P_802_3);
  97. else
  98. /*
  99. * Real 802.2 LLC
  100. */
  101. skb->protocol = htons(ETH_P_802_2);
  102. }
  103. /*
  104. * Determine the packet's protocol ID. The rule here is that we
  105. * assume 802.3 if the type field is short enough to be a length.
  106. * This is normal practice and works for any 'now in use' protocol.
  107. *
  108. * Also, at this point we assume that we ARE dealing exclusively with
  109. * VLAN packets, or packets that should be made into VLAN packets based
  110. * on a default VLAN ID.
  111. *
  112. * NOTE: Should be similar to ethernet/eth.c.
  113. *
  114. * SANITY NOTE: This method is called when a packet is moving up the stack
  115. * towards userland. To get here, it would have already passed
  116. * through the ethernet/eth.c eth_type_trans() method.
  117. * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
  118. * stored UNALIGNED in the memory. RISC systems don't like
  119. * such cases very much...
  120. * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be
  121. * aligned, so there doesn't need to be any of the unaligned
  122. * stuff. It has been commented out now... --Ben
  123. *
  124. */
  125. int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
  126. struct packet_type *ptype, struct net_device *orig_dev)
  127. {
  128. struct vlan_hdr *vhdr;
  129. struct net_device_stats *stats;
  130. u16 vlan_id;
  131. u16 vlan_tci;
  132. skb = skb_share_check(skb, GFP_ATOMIC);
  133. if (skb == NULL)
  134. goto err_free;
  135. if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
  136. goto err_free;
  137. vhdr = (struct vlan_hdr *)skb->data;
  138. vlan_tci = ntohs(vhdr->h_vlan_TCI);
  139. vlan_id = vlan_tci & VLAN_VID_MASK;
  140. rcu_read_lock();
  141. skb->dev = __find_vlan_dev(dev, vlan_id);
  142. if (!skb->dev) {
  143. pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
  144. __func__, vlan_id, dev->name);
  145. goto err_unlock;
  146. }
  147. stats = &skb->dev->stats;
  148. stats->rx_packets++;
  149. stats->rx_bytes += skb->len;
  150. skb_pull_rcsum(skb, VLAN_HLEN);
  151. skb->priority = vlan_get_ingress_priority(skb->dev, vlan_tci);
  152. pr_debug("%s: priority: %u for TCI: %hu\n",
  153. __func__, skb->priority, vlan_tci);
  154. switch (skb->pkt_type) {
  155. case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
  156. /* stats->broadcast ++; // no such counter :-( */
  157. break;
  158. case PACKET_MULTICAST:
  159. stats->multicast++;
  160. break;
  161. case PACKET_OTHERHOST:
  162. /* Our lower layer thinks this is not local, let's make sure.
  163. * This allows the VLAN to have a different MAC than the
  164. * underlying device, and still route correctly.
  165. */
  166. if (!compare_ether_addr(eth_hdr(skb)->h_dest,
  167. skb->dev->dev_addr))
  168. skb->pkt_type = PACKET_HOST;
  169. break;
  170. default:
  171. break;
  172. }
  173. vlan_set_encap_proto(skb, vhdr);
  174. skb = vlan_check_reorder_header(skb);
  175. if (!skb) {
  176. stats->rx_errors++;
  177. goto err_unlock;
  178. }
  179. netif_rx(skb);
  180. rcu_read_unlock();
  181. return NET_RX_SUCCESS;
  182. err_unlock:
  183. rcu_read_unlock();
  184. err_free:
  185. kfree_skb(skb);
  186. return NET_RX_DROP;
  187. }
  188. static inline u16
  189. vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
  190. {
  191. struct vlan_priority_tci_mapping *mp;
  192. mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
  193. while (mp) {
  194. if (mp->priority == skb->priority) {
  195. return mp->vlan_qos; /* This should already be shifted
  196. * to mask correctly with the
  197. * VLAN's TCI */
  198. }
  199. mp = mp->next;
  200. }
  201. return 0;
  202. }
  203. /*
  204. * Create the VLAN header for an arbitrary protocol layer
  205. *
  206. * saddr=NULL means use device source address
  207. * daddr=NULL means leave destination address (eg unresolved arp)
  208. *
  209. * This is called when the SKB is moving down the stack towards the
  210. * physical devices.
  211. */
  212. static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
  213. unsigned short type,
  214. const void *daddr, const void *saddr,
  215. unsigned int len)
  216. {
  217. struct vlan_hdr *vhdr;
  218. unsigned int vhdrlen = 0;
  219. u16 vlan_tci = 0;
  220. int rc;
  221. if (WARN_ON(skb_headroom(skb) < dev->hard_header_len))
  222. return -ENOSPC;
  223. if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR)) {
  224. vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
  225. vlan_tci = vlan_dev_info(dev)->vlan_id;
  226. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
  227. vhdr->h_vlan_TCI = htons(vlan_tci);
  228. /*
  229. * Set the protocol type. For a packet of type ETH_P_802_3 we
  230. * put the length in here instead. It is up to the 802.2
  231. * layer to carry protocol information.
  232. */
  233. if (type != ETH_P_802_3)
  234. vhdr->h_vlan_encapsulated_proto = htons(type);
  235. else
  236. vhdr->h_vlan_encapsulated_proto = htons(len);
  237. skb->protocol = htons(ETH_P_8021Q);
  238. type = ETH_P_8021Q;
  239. vhdrlen = VLAN_HLEN;
  240. }
  241. /* Before delegating work to the lower layer, enter our MAC-address */
  242. if (saddr == NULL)
  243. saddr = dev->dev_addr;
  244. /* Now make the underlying real hard header */
  245. dev = vlan_dev_info(dev)->real_dev;
  246. rc = dev_hard_header(skb, dev, type, daddr, saddr, len + vhdrlen);
  247. if (rc > 0)
  248. rc += vhdrlen;
  249. return rc;
  250. }
  251. static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  252. {
  253. struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
  254. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  255. /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
  256. *
  257. * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
  258. * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
  259. */
  260. if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
  261. vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
  262. unsigned int orig_headroom = skb_headroom(skb);
  263. u16 vlan_tci;
  264. vlan_dev_info(dev)->cnt_encap_on_xmit++;
  265. vlan_tci = vlan_dev_info(dev)->vlan_id;
  266. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
  267. skb = __vlan_put_tag(skb, vlan_tci);
  268. if (!skb) {
  269. txq->tx_dropped++;
  270. return NETDEV_TX_OK;
  271. }
  272. if (orig_headroom < VLAN_HLEN)
  273. vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
  274. }
  275. txq->tx_packets++;
  276. txq->tx_bytes += skb->len;
  277. skb->dev = vlan_dev_info(dev)->real_dev;
  278. dev_queue_xmit(skb);
  279. return NETDEV_TX_OK;
  280. }
  281. static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
  282. struct net_device *dev)
  283. {
  284. struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
  285. u16 vlan_tci;
  286. vlan_tci = vlan_dev_info(dev)->vlan_id;
  287. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
  288. skb = __vlan_hwaccel_put_tag(skb, vlan_tci);
  289. txq->tx_packets++;
  290. txq->tx_bytes += skb->len;
  291. skb->dev = vlan_dev_info(dev)->real_dev;
  292. dev_queue_xmit(skb);
  293. return NETDEV_TX_OK;
  294. }
  295. static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
  296. {
  297. /* TODO: gotta make sure the underlying layer can handle it,
  298. * maybe an IFF_VLAN_CAPABLE flag for devices?
  299. */
  300. if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
  301. return -ERANGE;
  302. dev->mtu = new_mtu;
  303. return 0;
  304. }
  305. void vlan_dev_set_ingress_priority(const struct net_device *dev,
  306. u32 skb_prio, u16 vlan_prio)
  307. {
  308. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  309. if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
  310. vlan->nr_ingress_mappings--;
  311. else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
  312. vlan->nr_ingress_mappings++;
  313. vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
  314. }
  315. int vlan_dev_set_egress_priority(const struct net_device *dev,
  316. u32 skb_prio, u16 vlan_prio)
  317. {
  318. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  319. struct vlan_priority_tci_mapping *mp = NULL;
  320. struct vlan_priority_tci_mapping *np;
  321. u32 vlan_qos = (vlan_prio << 13) & 0xE000;
  322. /* See if a priority mapping exists.. */
  323. mp = vlan->egress_priority_map[skb_prio & 0xF];
  324. while (mp) {
  325. if (mp->priority == skb_prio) {
  326. if (mp->vlan_qos && !vlan_qos)
  327. vlan->nr_egress_mappings--;
  328. else if (!mp->vlan_qos && vlan_qos)
  329. vlan->nr_egress_mappings++;
  330. mp->vlan_qos = vlan_qos;
  331. return 0;
  332. }
  333. mp = mp->next;
  334. }
  335. /* Create a new mapping then. */
  336. mp = vlan->egress_priority_map[skb_prio & 0xF];
  337. np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
  338. if (!np)
  339. return -ENOBUFS;
  340. np->next = mp;
  341. np->priority = skb_prio;
  342. np->vlan_qos = vlan_qos;
  343. vlan->egress_priority_map[skb_prio & 0xF] = np;
  344. if (vlan_qos)
  345. vlan->nr_egress_mappings++;
  346. return 0;
  347. }
  348. /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
  349. int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
  350. {
  351. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  352. u32 old_flags = vlan->flags;
  353. if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP))
  354. return -EINVAL;
  355. vlan->flags = (old_flags & ~mask) | (flags & mask);
  356. if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
  357. if (vlan->flags & VLAN_FLAG_GVRP)
  358. vlan_gvrp_request_join(dev);
  359. else
  360. vlan_gvrp_request_leave(dev);
  361. }
  362. return 0;
  363. }
  364. void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
  365. {
  366. strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
  367. }
  368. static int vlan_dev_open(struct net_device *dev)
  369. {
  370. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  371. struct net_device *real_dev = vlan->real_dev;
  372. int err;
  373. if (!(real_dev->flags & IFF_UP))
  374. return -ENETDOWN;
  375. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
  376. err = dev_unicast_add(real_dev, dev->dev_addr);
  377. if (err < 0)
  378. goto out;
  379. }
  380. if (dev->flags & IFF_ALLMULTI) {
  381. err = dev_set_allmulti(real_dev, 1);
  382. if (err < 0)
  383. goto del_unicast;
  384. }
  385. if (dev->flags & IFF_PROMISC) {
  386. err = dev_set_promiscuity(real_dev, 1);
  387. if (err < 0)
  388. goto clear_allmulti;
  389. }
  390. memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
  391. if (vlan->flags & VLAN_FLAG_GVRP)
  392. vlan_gvrp_request_join(dev);
  393. netif_carrier_on(dev);
  394. return 0;
  395. clear_allmulti:
  396. if (dev->flags & IFF_ALLMULTI)
  397. dev_set_allmulti(real_dev, -1);
  398. del_unicast:
  399. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  400. dev_unicast_delete(real_dev, dev->dev_addr);
  401. out:
  402. netif_carrier_off(dev);
  403. return err;
  404. }
  405. static int vlan_dev_stop(struct net_device *dev)
  406. {
  407. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  408. struct net_device *real_dev = vlan->real_dev;
  409. if (vlan->flags & VLAN_FLAG_GVRP)
  410. vlan_gvrp_request_leave(dev);
  411. dev_mc_unsync(real_dev, dev);
  412. dev_unicast_unsync(real_dev, dev);
  413. if (dev->flags & IFF_ALLMULTI)
  414. dev_set_allmulti(real_dev, -1);
  415. if (dev->flags & IFF_PROMISC)
  416. dev_set_promiscuity(real_dev, -1);
  417. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  418. dev_unicast_delete(real_dev, dev->dev_addr);
  419. netif_carrier_off(dev);
  420. return 0;
  421. }
  422. static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
  423. {
  424. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  425. struct sockaddr *addr = p;
  426. int err;
  427. if (!is_valid_ether_addr(addr->sa_data))
  428. return -EADDRNOTAVAIL;
  429. if (!(dev->flags & IFF_UP))
  430. goto out;
  431. if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
  432. err = dev_unicast_add(real_dev, addr->sa_data);
  433. if (err < 0)
  434. return err;
  435. }
  436. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  437. dev_unicast_delete(real_dev, dev->dev_addr);
  438. out:
  439. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  440. return 0;
  441. }
  442. static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  443. {
  444. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  445. const struct net_device_ops *ops = real_dev->netdev_ops;
  446. struct ifreq ifrr;
  447. int err = -EOPNOTSUPP;
  448. strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
  449. ifrr.ifr_ifru = ifr->ifr_ifru;
  450. switch (cmd) {
  451. case SIOCGMIIPHY:
  452. case SIOCGMIIREG:
  453. case SIOCSMIIREG:
  454. if (netif_device_present(real_dev) && ops->ndo_do_ioctl)
  455. err = ops->ndo_do_ioctl(real_dev, &ifrr, cmd);
  456. break;
  457. }
  458. if (!err)
  459. ifr->ifr_ifru = ifrr.ifr_ifru;
  460. return err;
  461. }
  462. static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa)
  463. {
  464. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  465. const struct net_device_ops *ops = real_dev->netdev_ops;
  466. int err = 0;
  467. if (netif_device_present(real_dev) && ops->ndo_neigh_setup)
  468. err = ops->ndo_neigh_setup(real_dev, pa);
  469. return err;
  470. }
  471. static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
  472. {
  473. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  474. if (change & IFF_ALLMULTI)
  475. dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  476. if (change & IFF_PROMISC)
  477. dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
  478. }
  479. static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
  480. {
  481. dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
  482. dev_unicast_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
  483. }
  484. /*
  485. * vlan network devices have devices nesting below it, and are a special
  486. * "super class" of normal network devices; split their locks off into a
  487. * separate class since they always nest.
  488. */
  489. static struct lock_class_key vlan_netdev_xmit_lock_key;
  490. static struct lock_class_key vlan_netdev_addr_lock_key;
  491. static void vlan_dev_set_lockdep_one(struct net_device *dev,
  492. struct netdev_queue *txq,
  493. void *_subclass)
  494. {
  495. lockdep_set_class_and_subclass(&txq->_xmit_lock,
  496. &vlan_netdev_xmit_lock_key,
  497. *(int *)_subclass);
  498. }
  499. static void vlan_dev_set_lockdep_class(struct net_device *dev, int subclass)
  500. {
  501. lockdep_set_class_and_subclass(&dev->addr_list_lock,
  502. &vlan_netdev_addr_lock_key,
  503. subclass);
  504. netdev_for_each_tx_queue(dev, vlan_dev_set_lockdep_one, &subclass);
  505. }
  506. static const struct header_ops vlan_header_ops = {
  507. .create = vlan_dev_hard_header,
  508. .rebuild = vlan_dev_rebuild_header,
  509. .parse = eth_header_parse,
  510. };
  511. static const struct net_device_ops vlan_netdev_ops, vlan_netdev_accel_ops;
  512. static int vlan_dev_init(struct net_device *dev)
  513. {
  514. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  515. int subclass = 0;
  516. netif_carrier_off(dev);
  517. /* IFF_BROADCAST|IFF_MULTICAST; ??? */
  518. dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI);
  519. dev->iflink = real_dev->ifindex;
  520. dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
  521. (1<<__LINK_STATE_DORMANT))) |
  522. (1<<__LINK_STATE_PRESENT);
  523. dev->features |= real_dev->features & real_dev->vlan_features;
  524. dev->gso_max_size = real_dev->gso_max_size;
  525. /* ipv6 shared card related stuff */
  526. dev->dev_id = real_dev->dev_id;
  527. if (is_zero_ether_addr(dev->dev_addr))
  528. memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
  529. if (is_zero_ether_addr(dev->broadcast))
  530. memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
  531. if (real_dev->features & NETIF_F_HW_VLAN_TX) {
  532. dev->header_ops = real_dev->header_ops;
  533. dev->hard_header_len = real_dev->hard_header_len;
  534. dev->netdev_ops = &vlan_netdev_accel_ops;
  535. } else {
  536. dev->header_ops = &vlan_header_ops;
  537. dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
  538. dev->netdev_ops = &vlan_netdev_ops;
  539. }
  540. if (is_vlan_dev(real_dev))
  541. subclass = 1;
  542. vlan_dev_set_lockdep_class(dev, subclass);
  543. return 0;
  544. }
  545. static void vlan_dev_uninit(struct net_device *dev)
  546. {
  547. struct vlan_priority_tci_mapping *pm;
  548. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  549. int i;
  550. for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
  551. while ((pm = vlan->egress_priority_map[i]) != NULL) {
  552. vlan->egress_priority_map[i] = pm->next;
  553. kfree(pm);
  554. }
  555. }
  556. }
  557. static int vlan_ethtool_get_settings(struct net_device *dev,
  558. struct ethtool_cmd *cmd)
  559. {
  560. const struct vlan_dev_info *vlan = vlan_dev_info(dev);
  561. return dev_ethtool_get_settings(vlan->real_dev, cmd);
  562. }
  563. static void vlan_ethtool_get_drvinfo(struct net_device *dev,
  564. struct ethtool_drvinfo *info)
  565. {
  566. strcpy(info->driver, vlan_fullname);
  567. strcpy(info->version, vlan_version);
  568. strcpy(info->fw_version, "N/A");
  569. }
  570. static u32 vlan_ethtool_get_rx_csum(struct net_device *dev)
  571. {
  572. const struct vlan_dev_info *vlan = vlan_dev_info(dev);
  573. return dev_ethtool_get_rx_csum(vlan->real_dev);
  574. }
  575. static u32 vlan_ethtool_get_flags(struct net_device *dev)
  576. {
  577. const struct vlan_dev_info *vlan = vlan_dev_info(dev);
  578. return dev_ethtool_get_flags(vlan->real_dev);
  579. }
  580. static const struct ethtool_ops vlan_ethtool_ops = {
  581. .get_settings = vlan_ethtool_get_settings,
  582. .get_drvinfo = vlan_ethtool_get_drvinfo,
  583. .get_link = ethtool_op_get_link,
  584. .get_rx_csum = vlan_ethtool_get_rx_csum,
  585. .get_flags = vlan_ethtool_get_flags,
  586. };
  587. static const struct net_device_ops vlan_netdev_ops = {
  588. .ndo_change_mtu = vlan_dev_change_mtu,
  589. .ndo_init = vlan_dev_init,
  590. .ndo_uninit = vlan_dev_uninit,
  591. .ndo_open = vlan_dev_open,
  592. .ndo_stop = vlan_dev_stop,
  593. .ndo_start_xmit = vlan_dev_hard_start_xmit,
  594. .ndo_validate_addr = eth_validate_addr,
  595. .ndo_set_mac_address = vlan_dev_set_mac_address,
  596. .ndo_set_rx_mode = vlan_dev_set_rx_mode,
  597. .ndo_set_multicast_list = vlan_dev_set_rx_mode,
  598. .ndo_change_rx_flags = vlan_dev_change_rx_flags,
  599. .ndo_do_ioctl = vlan_dev_ioctl,
  600. .ndo_neigh_setup = vlan_dev_neigh_setup,
  601. };
  602. static const struct net_device_ops vlan_netdev_accel_ops = {
  603. .ndo_change_mtu = vlan_dev_change_mtu,
  604. .ndo_init = vlan_dev_init,
  605. .ndo_uninit = vlan_dev_uninit,
  606. .ndo_open = vlan_dev_open,
  607. .ndo_stop = vlan_dev_stop,
  608. .ndo_start_xmit = vlan_dev_hwaccel_hard_start_xmit,
  609. .ndo_validate_addr = eth_validate_addr,
  610. .ndo_set_mac_address = vlan_dev_set_mac_address,
  611. .ndo_set_rx_mode = vlan_dev_set_rx_mode,
  612. .ndo_set_multicast_list = vlan_dev_set_rx_mode,
  613. .ndo_change_rx_flags = vlan_dev_change_rx_flags,
  614. .ndo_do_ioctl = vlan_dev_ioctl,
  615. .ndo_neigh_setup = vlan_dev_neigh_setup,
  616. };
  617. void vlan_setup(struct net_device *dev)
  618. {
  619. ether_setup(dev);
  620. dev->priv_flags |= IFF_802_1Q_VLAN;
  621. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  622. dev->tx_queue_len = 0;
  623. dev->netdev_ops = &vlan_netdev_ops;
  624. dev->destructor = free_netdev;
  625. dev->ethtool_ops = &vlan_ethtool_ops;
  626. memset(dev->broadcast, 0, ETH_ALEN);
  627. }