vlan_dev.c 19 KB

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