vlan_dev.c 22 KB

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