vlan_dev.c 22 KB

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