vlan_dev.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/ethtool.h>
  29. #include <net/arp.h>
  30. #include "vlan.h"
  31. #include "vlanproc.h"
  32. #include <linux/if_vlan.h>
  33. /*
  34. * Rebuild the Ethernet MAC header. This is called after an ARP
  35. * (or in future other address resolution) has completed on this
  36. * sk_buff. We now let ARP fill in the other fields.
  37. *
  38. * This routine CANNOT use cached dst->neigh!
  39. * Really, it is used only when dst->neigh is wrong.
  40. *
  41. * TODO: This needs a checkup, I'm ignorant here. --BLG
  42. */
  43. static int vlan_dev_rebuild_header(struct sk_buff *skb)
  44. {
  45. struct net_device *dev = skb->dev;
  46. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  47. switch (veth->h_vlan_encapsulated_proto) {
  48. #ifdef CONFIG_INET
  49. case htons(ETH_P_IP):
  50. /* TODO: Confirm this will work with VLAN headers... */
  51. return arp_find(veth->h_dest, skb);
  52. #endif
  53. default:
  54. pr_debug("%s: unable to resolve type %X addresses\n",
  55. dev->name, ntohs(veth->h_vlan_encapsulated_proto));
  56. memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
  57. break;
  58. }
  59. return 0;
  60. }
  61. static inline u16
  62. vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
  63. {
  64. struct vlan_priority_tci_mapping *mp;
  65. mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
  66. while (mp) {
  67. if (mp->priority == skb->priority) {
  68. return mp->vlan_qos; /* This should already be shifted
  69. * to mask correctly with the
  70. * VLAN's TCI */
  71. }
  72. mp = mp->next;
  73. }
  74. return 0;
  75. }
  76. /*
  77. * Create the VLAN header for an arbitrary protocol layer
  78. *
  79. * saddr=NULL means use device source address
  80. * daddr=NULL means leave destination address (eg unresolved arp)
  81. *
  82. * This is called when the SKB is moving down the stack towards the
  83. * physical devices.
  84. */
  85. static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
  86. unsigned short type,
  87. const void *daddr, const void *saddr,
  88. unsigned int len)
  89. {
  90. struct vlan_hdr *vhdr;
  91. unsigned int vhdrlen = 0;
  92. u16 vlan_tci = 0;
  93. int rc;
  94. if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR)) {
  95. vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
  96. vlan_tci = vlan_dev_info(dev)->vlan_id;
  97. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
  98. vhdr->h_vlan_TCI = htons(vlan_tci);
  99. /*
  100. * Set the protocol type. For a packet of type ETH_P_802_3/2 we
  101. * put the length in here instead.
  102. */
  103. if (type != ETH_P_802_3 && type != ETH_P_802_2)
  104. vhdr->h_vlan_encapsulated_proto = htons(type);
  105. else
  106. vhdr->h_vlan_encapsulated_proto = htons(len);
  107. skb->protocol = htons(ETH_P_8021Q);
  108. type = ETH_P_8021Q;
  109. vhdrlen = VLAN_HLEN;
  110. }
  111. /* Before delegating work to the lower layer, enter our MAC-address */
  112. if (saddr == NULL)
  113. saddr = dev->dev_addr;
  114. /* Now make the underlying real hard header */
  115. dev = vlan_dev_info(dev)->real_dev;
  116. rc = dev_hard_header(skb, dev, type, daddr, saddr, len + vhdrlen);
  117. if (rc > 0)
  118. rc += vhdrlen;
  119. return rc;
  120. }
  121. static netdev_tx_t vlan_dev_hard_start_xmit(struct sk_buff *skb,
  122. struct net_device *dev)
  123. {
  124. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  125. unsigned int len;
  126. int ret;
  127. /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
  128. *
  129. * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
  130. * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
  131. */
  132. if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
  133. vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
  134. u16 vlan_tci;
  135. vlan_tci = vlan_dev_info(dev)->vlan_id;
  136. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
  137. skb = __vlan_hwaccel_put_tag(skb, vlan_tci);
  138. }
  139. skb_set_dev(skb, vlan_dev_info(dev)->real_dev);
  140. len = skb->len;
  141. ret = dev_queue_xmit(skb);
  142. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  143. struct vlan_pcpu_stats *stats;
  144. stats = this_cpu_ptr(vlan_dev_info(dev)->vlan_pcpu_stats);
  145. u64_stats_update_begin(&stats->syncp);
  146. stats->tx_packets++;
  147. stats->tx_bytes += len;
  148. u64_stats_update_end(&stats->syncp);
  149. } else {
  150. this_cpu_inc(vlan_dev_info(dev)->vlan_pcpu_stats->tx_dropped);
  151. }
  152. return ret;
  153. }
  154. static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
  155. {
  156. /* TODO: gotta make sure the underlying layer can handle it,
  157. * maybe an IFF_VLAN_CAPABLE flag for devices?
  158. */
  159. if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
  160. return -ERANGE;
  161. dev->mtu = new_mtu;
  162. return 0;
  163. }
  164. void vlan_dev_set_ingress_priority(const struct net_device *dev,
  165. u32 skb_prio, u16 vlan_prio)
  166. {
  167. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  168. if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
  169. vlan->nr_ingress_mappings--;
  170. else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
  171. vlan->nr_ingress_mappings++;
  172. vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
  173. }
  174. int vlan_dev_set_egress_priority(const struct net_device *dev,
  175. u32 skb_prio, u16 vlan_prio)
  176. {
  177. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  178. struct vlan_priority_tci_mapping *mp = NULL;
  179. struct vlan_priority_tci_mapping *np;
  180. u32 vlan_qos = (vlan_prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
  181. /* See if a priority mapping exists.. */
  182. mp = vlan->egress_priority_map[skb_prio & 0xF];
  183. while (mp) {
  184. if (mp->priority == skb_prio) {
  185. if (mp->vlan_qos && !vlan_qos)
  186. vlan->nr_egress_mappings--;
  187. else if (!mp->vlan_qos && vlan_qos)
  188. vlan->nr_egress_mappings++;
  189. mp->vlan_qos = vlan_qos;
  190. return 0;
  191. }
  192. mp = mp->next;
  193. }
  194. /* Create a new mapping then. */
  195. mp = vlan->egress_priority_map[skb_prio & 0xF];
  196. np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
  197. if (!np)
  198. return -ENOBUFS;
  199. np->next = mp;
  200. np->priority = skb_prio;
  201. np->vlan_qos = vlan_qos;
  202. vlan->egress_priority_map[skb_prio & 0xF] = np;
  203. if (vlan_qos)
  204. vlan->nr_egress_mappings++;
  205. return 0;
  206. }
  207. /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
  208. int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
  209. {
  210. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  211. u32 old_flags = vlan->flags;
  212. if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
  213. VLAN_FLAG_LOOSE_BINDING))
  214. return -EINVAL;
  215. vlan->flags = (old_flags & ~mask) | (flags & mask);
  216. if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
  217. if (vlan->flags & VLAN_FLAG_GVRP)
  218. vlan_gvrp_request_join(dev);
  219. else
  220. vlan_gvrp_request_leave(dev);
  221. }
  222. return 0;
  223. }
  224. void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
  225. {
  226. strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
  227. }
  228. static int vlan_dev_open(struct net_device *dev)
  229. {
  230. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  231. struct net_device *real_dev = vlan->real_dev;
  232. int err;
  233. if (!(real_dev->flags & IFF_UP) &&
  234. !(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
  235. return -ENETDOWN;
  236. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
  237. err = dev_uc_add(real_dev, dev->dev_addr);
  238. if (err < 0)
  239. goto out;
  240. }
  241. if (dev->flags & IFF_ALLMULTI) {
  242. err = dev_set_allmulti(real_dev, 1);
  243. if (err < 0)
  244. goto del_unicast;
  245. }
  246. if (dev->flags & IFF_PROMISC) {
  247. err = dev_set_promiscuity(real_dev, 1);
  248. if (err < 0)
  249. goto clear_allmulti;
  250. }
  251. memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
  252. if (vlan->flags & VLAN_FLAG_GVRP)
  253. vlan_gvrp_request_join(dev);
  254. if (netif_carrier_ok(real_dev))
  255. netif_carrier_on(dev);
  256. return 0;
  257. clear_allmulti:
  258. if (dev->flags & IFF_ALLMULTI)
  259. dev_set_allmulti(real_dev, -1);
  260. del_unicast:
  261. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  262. dev_uc_del(real_dev, dev->dev_addr);
  263. out:
  264. netif_carrier_off(dev);
  265. return err;
  266. }
  267. static int vlan_dev_stop(struct net_device *dev)
  268. {
  269. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  270. struct net_device *real_dev = vlan->real_dev;
  271. dev_mc_unsync(real_dev, dev);
  272. dev_uc_unsync(real_dev, dev);
  273. if (dev->flags & IFF_ALLMULTI)
  274. dev_set_allmulti(real_dev, -1);
  275. if (dev->flags & IFF_PROMISC)
  276. dev_set_promiscuity(real_dev, -1);
  277. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  278. dev_uc_del(real_dev, dev->dev_addr);
  279. netif_carrier_off(dev);
  280. return 0;
  281. }
  282. static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
  283. {
  284. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  285. struct sockaddr *addr = p;
  286. int err;
  287. if (!is_valid_ether_addr(addr->sa_data))
  288. return -EADDRNOTAVAIL;
  289. if (!(dev->flags & IFF_UP))
  290. goto out;
  291. if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
  292. err = dev_uc_add(real_dev, addr->sa_data);
  293. if (err < 0)
  294. return err;
  295. }
  296. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  297. dev_uc_del(real_dev, dev->dev_addr);
  298. out:
  299. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  300. return 0;
  301. }
  302. static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  303. {
  304. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  305. const struct net_device_ops *ops = real_dev->netdev_ops;
  306. struct ifreq ifrr;
  307. int err = -EOPNOTSUPP;
  308. strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
  309. ifrr.ifr_ifru = ifr->ifr_ifru;
  310. switch (cmd) {
  311. case SIOCGMIIPHY:
  312. case SIOCGMIIREG:
  313. case SIOCSMIIREG:
  314. if (netif_device_present(real_dev) && ops->ndo_do_ioctl)
  315. err = ops->ndo_do_ioctl(real_dev, &ifrr, cmd);
  316. break;
  317. }
  318. if (!err)
  319. ifr->ifr_ifru = ifrr.ifr_ifru;
  320. return err;
  321. }
  322. static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa)
  323. {
  324. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  325. const struct net_device_ops *ops = real_dev->netdev_ops;
  326. int err = 0;
  327. if (netif_device_present(real_dev) && ops->ndo_neigh_setup)
  328. err = ops->ndo_neigh_setup(real_dev, pa);
  329. return err;
  330. }
  331. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  332. static int vlan_dev_fcoe_ddp_setup(struct net_device *dev, u16 xid,
  333. struct scatterlist *sgl, unsigned int sgc)
  334. {
  335. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  336. const struct net_device_ops *ops = real_dev->netdev_ops;
  337. int rc = 0;
  338. if (ops->ndo_fcoe_ddp_setup)
  339. rc = ops->ndo_fcoe_ddp_setup(real_dev, xid, sgl, sgc);
  340. return rc;
  341. }
  342. static int vlan_dev_fcoe_ddp_done(struct net_device *dev, u16 xid)
  343. {
  344. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  345. const struct net_device_ops *ops = real_dev->netdev_ops;
  346. int len = 0;
  347. if (ops->ndo_fcoe_ddp_done)
  348. len = ops->ndo_fcoe_ddp_done(real_dev, xid);
  349. return len;
  350. }
  351. static int vlan_dev_fcoe_enable(struct net_device *dev)
  352. {
  353. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  354. const struct net_device_ops *ops = real_dev->netdev_ops;
  355. int rc = -EINVAL;
  356. if (ops->ndo_fcoe_enable)
  357. rc = ops->ndo_fcoe_enable(real_dev);
  358. return rc;
  359. }
  360. static int vlan_dev_fcoe_disable(struct net_device *dev)
  361. {
  362. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  363. const struct net_device_ops *ops = real_dev->netdev_ops;
  364. int rc = -EINVAL;
  365. if (ops->ndo_fcoe_disable)
  366. rc = ops->ndo_fcoe_disable(real_dev);
  367. return rc;
  368. }
  369. static int vlan_dev_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
  370. {
  371. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  372. const struct net_device_ops *ops = real_dev->netdev_ops;
  373. int rc = -EINVAL;
  374. if (ops->ndo_fcoe_get_wwn)
  375. rc = ops->ndo_fcoe_get_wwn(real_dev, wwn, type);
  376. return rc;
  377. }
  378. static int vlan_dev_fcoe_ddp_target(struct net_device *dev, u16 xid,
  379. struct scatterlist *sgl, unsigned int sgc)
  380. {
  381. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  382. const struct net_device_ops *ops = real_dev->netdev_ops;
  383. int rc = 0;
  384. if (ops->ndo_fcoe_ddp_target)
  385. rc = ops->ndo_fcoe_ddp_target(real_dev, xid, sgl, sgc);
  386. return rc;
  387. }
  388. #endif
  389. static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
  390. {
  391. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  392. if (dev->flags & IFF_UP) {
  393. if (change & IFF_ALLMULTI)
  394. dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  395. if (change & IFF_PROMISC)
  396. dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
  397. }
  398. }
  399. static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
  400. {
  401. dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
  402. dev_uc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
  403. }
  404. /*
  405. * vlan network devices have devices nesting below it, and are a special
  406. * "super class" of normal network devices; split their locks off into a
  407. * separate class since they always nest.
  408. */
  409. static struct lock_class_key vlan_netdev_xmit_lock_key;
  410. static struct lock_class_key vlan_netdev_addr_lock_key;
  411. static void vlan_dev_set_lockdep_one(struct net_device *dev,
  412. struct netdev_queue *txq,
  413. void *_subclass)
  414. {
  415. lockdep_set_class_and_subclass(&txq->_xmit_lock,
  416. &vlan_netdev_xmit_lock_key,
  417. *(int *)_subclass);
  418. }
  419. static void vlan_dev_set_lockdep_class(struct net_device *dev, int subclass)
  420. {
  421. lockdep_set_class_and_subclass(&dev->addr_list_lock,
  422. &vlan_netdev_addr_lock_key,
  423. subclass);
  424. netdev_for_each_tx_queue(dev, vlan_dev_set_lockdep_one, &subclass);
  425. }
  426. static const struct header_ops vlan_header_ops = {
  427. .create = vlan_dev_hard_header,
  428. .rebuild = vlan_dev_rebuild_header,
  429. .parse = eth_header_parse,
  430. };
  431. static const struct net_device_ops vlan_netdev_ops;
  432. static int vlan_dev_init(struct net_device *dev)
  433. {
  434. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  435. int subclass = 0;
  436. netif_carrier_off(dev);
  437. /* IFF_BROADCAST|IFF_MULTICAST; ??? */
  438. dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
  439. IFF_MASTER | IFF_SLAVE);
  440. dev->iflink = real_dev->ifindex;
  441. dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
  442. (1<<__LINK_STATE_DORMANT))) |
  443. (1<<__LINK_STATE_PRESENT);
  444. dev->hw_features = NETIF_F_ALL_CSUM | NETIF_F_SG |
  445. NETIF_F_FRAGLIST | NETIF_F_ALL_TSO |
  446. NETIF_F_HIGHDMA | NETIF_F_SCTP_CSUM |
  447. NETIF_F_ALL_FCOE;
  448. dev->features |= real_dev->vlan_features | NETIF_F_LLTX;
  449. dev->gso_max_size = real_dev->gso_max_size;
  450. /* ipv6 shared card related stuff */
  451. dev->dev_id = real_dev->dev_id;
  452. if (is_zero_ether_addr(dev->dev_addr))
  453. memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
  454. if (is_zero_ether_addr(dev->broadcast))
  455. memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
  456. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  457. dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid;
  458. #endif
  459. dev->needed_headroom = real_dev->needed_headroom;
  460. if (real_dev->features & NETIF_F_HW_VLAN_TX) {
  461. dev->header_ops = real_dev->header_ops;
  462. dev->hard_header_len = real_dev->hard_header_len;
  463. } else {
  464. dev->header_ops = &vlan_header_ops;
  465. dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
  466. }
  467. dev->netdev_ops = &vlan_netdev_ops;
  468. if (is_vlan_dev(real_dev))
  469. subclass = 1;
  470. vlan_dev_set_lockdep_class(dev, subclass);
  471. vlan_dev_info(dev)->vlan_pcpu_stats = alloc_percpu(struct vlan_pcpu_stats);
  472. if (!vlan_dev_info(dev)->vlan_pcpu_stats)
  473. return -ENOMEM;
  474. return 0;
  475. }
  476. static void vlan_dev_uninit(struct net_device *dev)
  477. {
  478. struct vlan_priority_tci_mapping *pm;
  479. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  480. int i;
  481. free_percpu(vlan->vlan_pcpu_stats);
  482. vlan->vlan_pcpu_stats = NULL;
  483. for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
  484. while ((pm = vlan->egress_priority_map[i]) != NULL) {
  485. vlan->egress_priority_map[i] = pm->next;
  486. kfree(pm);
  487. }
  488. }
  489. }
  490. static u32 vlan_dev_fix_features(struct net_device *dev, u32 features)
  491. {
  492. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  493. u32 old_features = features;
  494. features &= real_dev->features;
  495. features &= real_dev->vlan_features;
  496. features |= old_features & NETIF_F_SOFT_FEATURES;
  497. if (dev_ethtool_get_rx_csum(real_dev))
  498. features |= NETIF_F_RXCSUM;
  499. features |= NETIF_F_LLTX;
  500. return features;
  501. }
  502. static int vlan_ethtool_get_settings(struct net_device *dev,
  503. struct ethtool_cmd *cmd)
  504. {
  505. const struct vlan_dev_info *vlan = vlan_dev_info(dev);
  506. return __ethtool_get_settings(vlan->real_dev, cmd);
  507. }
  508. static void vlan_ethtool_get_drvinfo(struct net_device *dev,
  509. struct ethtool_drvinfo *info)
  510. {
  511. strcpy(info->driver, vlan_fullname);
  512. strcpy(info->version, vlan_version);
  513. strcpy(info->fw_version, "N/A");
  514. }
  515. static struct rtnl_link_stats64 *vlan_dev_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  516. {
  517. if (vlan_dev_info(dev)->vlan_pcpu_stats) {
  518. struct vlan_pcpu_stats *p;
  519. u32 rx_errors = 0, tx_dropped = 0;
  520. int i;
  521. for_each_possible_cpu(i) {
  522. u64 rxpackets, rxbytes, rxmulticast, txpackets, txbytes;
  523. unsigned int start;
  524. p = per_cpu_ptr(vlan_dev_info(dev)->vlan_pcpu_stats, i);
  525. do {
  526. start = u64_stats_fetch_begin_bh(&p->syncp);
  527. rxpackets = p->rx_packets;
  528. rxbytes = p->rx_bytes;
  529. rxmulticast = p->rx_multicast;
  530. txpackets = p->tx_packets;
  531. txbytes = p->tx_bytes;
  532. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  533. stats->rx_packets += rxpackets;
  534. stats->rx_bytes += rxbytes;
  535. stats->multicast += rxmulticast;
  536. stats->tx_packets += txpackets;
  537. stats->tx_bytes += txbytes;
  538. /* rx_errors & tx_dropped are u32 */
  539. rx_errors += p->rx_errors;
  540. tx_dropped += p->tx_dropped;
  541. }
  542. stats->rx_errors = rx_errors;
  543. stats->tx_dropped = tx_dropped;
  544. }
  545. return stats;
  546. }
  547. static const struct ethtool_ops vlan_ethtool_ops = {
  548. .get_settings = vlan_ethtool_get_settings,
  549. .get_drvinfo = vlan_ethtool_get_drvinfo,
  550. .get_link = ethtool_op_get_link,
  551. };
  552. static const struct net_device_ops vlan_netdev_ops = {
  553. .ndo_change_mtu = vlan_dev_change_mtu,
  554. .ndo_init = vlan_dev_init,
  555. .ndo_uninit = vlan_dev_uninit,
  556. .ndo_open = vlan_dev_open,
  557. .ndo_stop = vlan_dev_stop,
  558. .ndo_start_xmit = vlan_dev_hard_start_xmit,
  559. .ndo_validate_addr = eth_validate_addr,
  560. .ndo_set_mac_address = vlan_dev_set_mac_address,
  561. .ndo_set_rx_mode = vlan_dev_set_rx_mode,
  562. .ndo_change_rx_flags = vlan_dev_change_rx_flags,
  563. .ndo_do_ioctl = vlan_dev_ioctl,
  564. .ndo_neigh_setup = vlan_dev_neigh_setup,
  565. .ndo_get_stats64 = vlan_dev_get_stats64,
  566. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  567. .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
  568. .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done,
  569. .ndo_fcoe_enable = vlan_dev_fcoe_enable,
  570. .ndo_fcoe_disable = vlan_dev_fcoe_disable,
  571. .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn,
  572. .ndo_fcoe_ddp_target = vlan_dev_fcoe_ddp_target,
  573. #endif
  574. .ndo_fix_features = vlan_dev_fix_features,
  575. };
  576. void vlan_setup(struct net_device *dev)
  577. {
  578. ether_setup(dev);
  579. dev->priv_flags |= IFF_802_1Q_VLAN;
  580. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  581. dev->tx_queue_len = 0;
  582. dev->netdev_ops = &vlan_netdev_ops;
  583. dev->destructor = free_netdev;
  584. dev->ethtool_ops = &vlan_ethtool_ops;
  585. memset(dev->broadcast, 0, ETH_ALEN);
  586. }