vlan_dev.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* -*- linux-c -*-
  3. * INET 802.1Q VLAN
  4. * Ethernet-type device handling.
  5. *
  6. * Authors: Ben Greear <greearb@candelatech.com>
  7. * Please send support related email to: netdev@vger.kernel.org
  8. * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
  9. *
  10. * Fixes: Mar 22 2001: Martin Bokaemper <mbokaemper@unispherenetworks.com>
  11. * - reset skb->pkt_type on incoming packets when MAC was changed
  12. * - see that changed MAC is saddr for outgoing packets
  13. * Oct 20, 2001: Ard van Breeman:
  14. * - Fix MC-list, finally.
  15. * - Flush MC-list on VLAN destroy.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/net_tstamp.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/phy.h>
  26. #include <net/arp.h>
  27. #include <net/macsec.h>
  28. #include <net/netdev_lock.h>
  29. #include "vlan.h"
  30. #include "vlanproc.h"
  31. #include <linux/if_vlan.h>
  32. #include <linux/netpoll.h>
  33. /*
  34. * Create the VLAN header for an arbitrary protocol layer
  35. *
  36. * saddr=NULL means use device source address
  37. * daddr=NULL means leave destination address (eg unresolved arp)
  38. *
  39. * This is called when the SKB is moving down the stack towards the
  40. * physical devices.
  41. */
  42. static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
  43. unsigned short type,
  44. const void *daddr, const void *saddr,
  45. unsigned int len)
  46. {
  47. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  48. struct vlan_hdr *vhdr;
  49. unsigned int vhdrlen = 0;
  50. u16 vlan_tci = 0;
  51. int rc;
  52. if (!(vlan->flags & VLAN_FLAG_REORDER_HDR)) {
  53. vhdr = skb_push(skb, VLAN_HLEN);
  54. vlan_tci = vlan->vlan_id;
  55. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
  56. vhdr->h_vlan_TCI = htons(vlan_tci);
  57. /*
  58. * Set the protocol type. For a packet of type ETH_P_802_3/2 we
  59. * put the length in here instead.
  60. */
  61. if (type != ETH_P_802_3 && type != ETH_P_802_2)
  62. vhdr->h_vlan_encapsulated_proto = htons(type);
  63. else
  64. vhdr->h_vlan_encapsulated_proto = htons(len);
  65. skb->protocol = vlan->vlan_proto;
  66. type = ntohs(vlan->vlan_proto);
  67. vhdrlen = VLAN_HLEN;
  68. }
  69. /* Before delegating work to the lower layer, enter our MAC-address */
  70. if (saddr == NULL)
  71. saddr = dev->dev_addr;
  72. /* Now make the underlying real hard header */
  73. dev = vlan->real_dev;
  74. rc = dev_hard_header(skb, dev, type, daddr, saddr, len + vhdrlen);
  75. if (rc > 0)
  76. rc += vhdrlen;
  77. return rc;
  78. }
  79. static inline netdev_tx_t vlan_netpoll_send_skb(struct vlan_dev_priv *vlan, struct sk_buff *skb)
  80. {
  81. #ifdef CONFIG_NET_POLL_CONTROLLER
  82. return netpoll_send_skb(vlan->netpoll, skb);
  83. #else
  84. BUG();
  85. return NETDEV_TX_OK;
  86. #endif
  87. }
  88. static netdev_tx_t vlan_dev_hard_start_xmit(struct sk_buff *skb,
  89. struct net_device *dev)
  90. {
  91. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  92. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  93. unsigned int len;
  94. int ret;
  95. /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
  96. *
  97. * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
  98. * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
  99. */
  100. if (vlan->flags & VLAN_FLAG_REORDER_HDR ||
  101. veth->h_vlan_proto != vlan->vlan_proto) {
  102. u16 vlan_tci;
  103. vlan_tci = vlan->vlan_id;
  104. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
  105. __vlan_hwaccel_put_tag(skb, vlan->vlan_proto, vlan_tci);
  106. }
  107. skb->dev = vlan->real_dev;
  108. len = skb->len;
  109. if (unlikely(netpoll_tx_running(dev)))
  110. return vlan_netpoll_send_skb(vlan, skb);
  111. ret = dev_queue_xmit(skb);
  112. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  113. struct vlan_pcpu_stats *stats;
  114. stats = this_cpu_ptr(vlan->vlan_pcpu_stats);
  115. u64_stats_update_begin(&stats->syncp);
  116. u64_stats_inc(&stats->tx_packets);
  117. u64_stats_add(&stats->tx_bytes, len);
  118. u64_stats_update_end(&stats->syncp);
  119. } else {
  120. this_cpu_inc(vlan->vlan_pcpu_stats->tx_dropped);
  121. }
  122. return ret;
  123. }
  124. static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
  125. {
  126. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  127. unsigned int max_mtu = real_dev->mtu;
  128. if (netif_reduces_vlan_mtu(real_dev))
  129. max_mtu -= VLAN_HLEN;
  130. if (max_mtu < new_mtu)
  131. return -ERANGE;
  132. WRITE_ONCE(dev->mtu, new_mtu);
  133. return 0;
  134. }
  135. void vlan_dev_set_ingress_priority(const struct net_device *dev,
  136. u32 skb_prio, u16 vlan_prio)
  137. {
  138. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  139. if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
  140. vlan->nr_ingress_mappings--;
  141. else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
  142. vlan->nr_ingress_mappings++;
  143. vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
  144. }
  145. int vlan_dev_set_egress_priority(const struct net_device *dev,
  146. u32 skb_prio, u16 vlan_prio)
  147. {
  148. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  149. struct vlan_priority_tci_mapping *mp = NULL;
  150. struct vlan_priority_tci_mapping *np;
  151. u32 vlan_qos = (vlan_prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
  152. /* See if a priority mapping exists.. */
  153. mp = vlan->egress_priority_map[skb_prio & 0xF];
  154. while (mp) {
  155. if (mp->priority == skb_prio) {
  156. if (mp->vlan_qos && !vlan_qos)
  157. vlan->nr_egress_mappings--;
  158. else if (!mp->vlan_qos && vlan_qos)
  159. vlan->nr_egress_mappings++;
  160. mp->vlan_qos = vlan_qos;
  161. return 0;
  162. }
  163. mp = mp->next;
  164. }
  165. /* Create a new mapping then. */
  166. mp = vlan->egress_priority_map[skb_prio & 0xF];
  167. np = kmalloc_obj(struct vlan_priority_tci_mapping);
  168. if (!np)
  169. return -ENOBUFS;
  170. np->next = mp;
  171. np->priority = skb_prio;
  172. np->vlan_qos = vlan_qos;
  173. /* Before inserting this element in hash table, make sure all its fields
  174. * are committed to memory.
  175. * coupled with smp_rmb() in vlan_dev_get_egress_qos_mask()
  176. */
  177. smp_wmb();
  178. vlan->egress_priority_map[skb_prio & 0xF] = np;
  179. if (vlan_qos)
  180. vlan->nr_egress_mappings++;
  181. return 0;
  182. }
  183. /* Flags are defined in the vlan_flags enum in
  184. * include/uapi/linux/if_vlan.h file.
  185. */
  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. VLAN_FLAG_BRIDGE_BINDING))
  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, size_t size)
  210. {
  211. strscpy_pad(result, vlan_dev_priv(dev)->real_dev->name, size);
  212. }
  213. bool vlan_dev_inherit_address(struct net_device *dev,
  214. struct net_device *real_dev)
  215. {
  216. if (dev->addr_assign_type != NET_ADDR_STOLEN)
  217. return false;
  218. eth_hw_addr_set(dev, real_dev->dev_addr);
  219. call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
  220. return true;
  221. }
  222. static int vlan_dev_open(struct net_device *dev)
  223. {
  224. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  225. struct net_device *real_dev = vlan->real_dev;
  226. int err;
  227. if (!(real_dev->flags & IFF_UP) &&
  228. !(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
  229. return -ENETDOWN;
  230. if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr) &&
  231. !vlan_dev_inherit_address(dev, real_dev)) {
  232. err = dev_uc_add(real_dev, dev->dev_addr);
  233. if (err < 0)
  234. goto out;
  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. !(vlan->flags & VLAN_FLAG_BRIDGE_BINDING))
  243. netif_carrier_on(dev);
  244. return 0;
  245. out:
  246. netif_carrier_off(dev);
  247. return err;
  248. }
  249. static int vlan_dev_stop(struct net_device *dev)
  250. {
  251. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  252. struct net_device *real_dev = vlan->real_dev;
  253. dev_mc_unsync(real_dev, dev);
  254. dev_uc_unsync(real_dev, dev);
  255. if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
  256. dev_uc_del(real_dev, dev->dev_addr);
  257. if (!(vlan->flags & VLAN_FLAG_BRIDGE_BINDING))
  258. netif_carrier_off(dev);
  259. return 0;
  260. }
  261. static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
  262. {
  263. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  264. struct sockaddr *addr = p;
  265. int err;
  266. if (!is_valid_ether_addr(addr->sa_data))
  267. return -EADDRNOTAVAIL;
  268. if (!(dev->flags & IFF_UP))
  269. goto out;
  270. if (!ether_addr_equal(addr->sa_data, real_dev->dev_addr)) {
  271. err = dev_uc_add(real_dev, addr->sa_data);
  272. if (err < 0)
  273. return err;
  274. }
  275. if (!ether_addr_equal(dev->dev_addr, real_dev->dev_addr))
  276. dev_uc_del(real_dev, dev->dev_addr);
  277. out:
  278. eth_hw_addr_set(dev, addr->sa_data);
  279. return 0;
  280. }
  281. static int vlan_hwtstamp_get(struct net_device *dev,
  282. struct kernel_hwtstamp_config *cfg)
  283. {
  284. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  285. return generic_hwtstamp_get_lower(real_dev, cfg);
  286. }
  287. static int vlan_hwtstamp_set(struct net_device *dev,
  288. struct kernel_hwtstamp_config *cfg,
  289. struct netlink_ext_ack *extack)
  290. {
  291. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  292. if (!net_eq(dev_net(dev), dev_net(real_dev)))
  293. return -EOPNOTSUPP;
  294. return generic_hwtstamp_set_lower(real_dev, cfg, extack);
  295. }
  296. static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  297. {
  298. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  299. struct ifreq ifrr;
  300. int err = -EOPNOTSUPP;
  301. strscpy_pad(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
  302. ifrr.ifr_ifru = ifr->ifr_ifru;
  303. switch (cmd) {
  304. case SIOCGMIIPHY:
  305. case SIOCGMIIREG:
  306. case SIOCSMIIREG:
  307. err = dev_eth_ioctl(real_dev, &ifrr, cmd);
  308. break;
  309. }
  310. if (!err)
  311. ifr->ifr_ifru = ifrr.ifr_ifru;
  312. return err;
  313. }
  314. static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa)
  315. {
  316. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  317. const struct net_device_ops *ops = real_dev->netdev_ops;
  318. int err = 0;
  319. if (netif_device_present(real_dev) && ops->ndo_neigh_setup)
  320. err = ops->ndo_neigh_setup(real_dev, pa);
  321. return err;
  322. }
  323. #if IS_ENABLED(CONFIG_FCOE)
  324. static int vlan_dev_fcoe_ddp_setup(struct net_device *dev, u16 xid,
  325. struct scatterlist *sgl, unsigned int sgc)
  326. {
  327. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  328. const struct net_device_ops *ops = real_dev->netdev_ops;
  329. int rc = 0;
  330. if (ops->ndo_fcoe_ddp_setup)
  331. rc = ops->ndo_fcoe_ddp_setup(real_dev, xid, sgl, sgc);
  332. return rc;
  333. }
  334. static int vlan_dev_fcoe_ddp_done(struct net_device *dev, u16 xid)
  335. {
  336. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  337. const struct net_device_ops *ops = real_dev->netdev_ops;
  338. int len = 0;
  339. if (ops->ndo_fcoe_ddp_done)
  340. len = ops->ndo_fcoe_ddp_done(real_dev, xid);
  341. return len;
  342. }
  343. static int vlan_dev_fcoe_enable(struct net_device *dev)
  344. {
  345. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  346. const struct net_device_ops *ops = real_dev->netdev_ops;
  347. int rc = -EINVAL;
  348. if (ops->ndo_fcoe_enable)
  349. rc = ops->ndo_fcoe_enable(real_dev);
  350. return rc;
  351. }
  352. static int vlan_dev_fcoe_disable(struct net_device *dev)
  353. {
  354. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  355. const struct net_device_ops *ops = real_dev->netdev_ops;
  356. int rc = -EINVAL;
  357. if (ops->ndo_fcoe_disable)
  358. rc = ops->ndo_fcoe_disable(real_dev);
  359. return rc;
  360. }
  361. static int vlan_dev_fcoe_ddp_target(struct net_device *dev, u16 xid,
  362. struct scatterlist *sgl, unsigned int sgc)
  363. {
  364. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  365. const struct net_device_ops *ops = real_dev->netdev_ops;
  366. int rc = 0;
  367. if (ops->ndo_fcoe_ddp_target)
  368. rc = ops->ndo_fcoe_ddp_target(real_dev, xid, sgl, sgc);
  369. return rc;
  370. }
  371. #endif
  372. #ifdef NETDEV_FCOE_WWNN
  373. static int vlan_dev_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
  374. {
  375. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  376. const struct net_device_ops *ops = real_dev->netdev_ops;
  377. int rc = -EINVAL;
  378. if (ops->ndo_fcoe_get_wwn)
  379. rc = ops->ndo_fcoe_get_wwn(real_dev, wwn, type);
  380. return rc;
  381. }
  382. #endif
  383. static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
  384. {
  385. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  386. if (change & IFF_ALLMULTI)
  387. dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  388. if (change & IFF_PROMISC)
  389. dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
  390. }
  391. static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
  392. {
  393. dev_mc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev);
  394. dev_uc_sync(vlan_dev_priv(vlan_dev)->real_dev, vlan_dev);
  395. }
  396. static __be16 vlan_parse_protocol(const struct sk_buff *skb)
  397. {
  398. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  399. return __vlan_get_protocol(skb, veth->h_vlan_proto, NULL);
  400. }
  401. static const struct header_ops vlan_header_ops = {
  402. .create = vlan_dev_hard_header,
  403. .parse = eth_header_parse,
  404. .parse_protocol = vlan_parse_protocol,
  405. };
  406. static int vlan_passthru_hard_header(struct sk_buff *skb, struct net_device *dev,
  407. unsigned short type,
  408. const void *daddr, const void *saddr,
  409. unsigned int len)
  410. {
  411. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  412. struct net_device *real_dev = vlan->real_dev;
  413. if (saddr == NULL)
  414. saddr = dev->dev_addr;
  415. return dev_hard_header(skb, real_dev, type, daddr, saddr, len);
  416. }
  417. static const struct header_ops vlan_passthru_header_ops = {
  418. .create = vlan_passthru_hard_header,
  419. .parse = eth_header_parse,
  420. .parse_protocol = vlan_parse_protocol,
  421. };
  422. static const struct device_type vlan_type = {
  423. .name = "vlan",
  424. };
  425. static const struct net_device_ops vlan_netdev_ops;
  426. static int vlan_dev_init(struct net_device *dev)
  427. {
  428. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  429. struct net_device *real_dev = vlan->real_dev;
  430. netif_carrier_off(dev);
  431. /* IFF_BROADCAST|IFF_MULTICAST; ??? */
  432. dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
  433. IFF_MASTER | IFF_SLAVE);
  434. dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
  435. (1<<__LINK_STATE_DORMANT))) |
  436. (1<<__LINK_STATE_PRESENT);
  437. if (vlan->flags & VLAN_FLAG_BRIDGE_BINDING)
  438. dev->state |= (1 << __LINK_STATE_NOCARRIER);
  439. dev->hw_features = NETIF_F_HW_CSUM | NETIF_F_SG |
  440. NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE |
  441. NETIF_F_GSO_ENCAP_ALL |
  442. NETIF_F_HIGHDMA | NETIF_F_SCTP_CRC |
  443. NETIF_F_FCOE_CRC | NETIF_F_FSO;
  444. if (real_dev->vlan_features & NETIF_F_HW_MACSEC)
  445. dev->hw_features |= NETIF_F_HW_MACSEC;
  446. dev->features |= dev->hw_features;
  447. dev->lltx = true;
  448. dev->fcoe_mtu = true;
  449. netif_inherit_tso_max(dev, real_dev);
  450. if (dev->features & NETIF_F_VLAN_FEATURES)
  451. netdev_warn(real_dev, "VLAN features are set incorrectly. Q-in-Q configurations may not work correctly.\n");
  452. dev->vlan_features = real_dev->vlan_features &
  453. ~(NETIF_F_FCOE_CRC | NETIF_F_FSO);
  454. dev->hw_enc_features = vlan_tnl_features(real_dev);
  455. dev->mpls_features = real_dev->mpls_features;
  456. /* ipv6 shared card related stuff */
  457. dev->dev_id = real_dev->dev_id;
  458. if (is_zero_ether_addr(dev->dev_addr)) {
  459. eth_hw_addr_set(dev, real_dev->dev_addr);
  460. dev->addr_assign_type = NET_ADDR_STOLEN;
  461. }
  462. if (is_zero_ether_addr(dev->broadcast))
  463. memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
  464. #if IS_ENABLED(CONFIG_FCOE)
  465. dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid;
  466. #endif
  467. dev->needed_headroom = real_dev->needed_headroom;
  468. if (vlan_hw_offload_capable(real_dev->features, vlan->vlan_proto)) {
  469. dev->header_ops = &vlan_passthru_header_ops;
  470. dev->hard_header_len = real_dev->hard_header_len;
  471. } else {
  472. dev->header_ops = &vlan_header_ops;
  473. dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
  474. }
  475. dev->netdev_ops = &vlan_netdev_ops;
  476. SET_NETDEV_DEVTYPE(dev, &vlan_type);
  477. netdev_lockdep_set_classes(dev);
  478. vlan->vlan_pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
  479. if (!vlan->vlan_pcpu_stats)
  480. return -ENOMEM;
  481. /* Get vlan's reference to real_dev */
  482. netdev_hold(real_dev, &vlan->dev_tracker, GFP_KERNEL);
  483. return 0;
  484. }
  485. /* Note: this function might be called multiple times for the same device. */
  486. void vlan_dev_free_egress_priority(const struct net_device *dev)
  487. {
  488. struct vlan_priority_tci_mapping *pm;
  489. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  490. int i;
  491. for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
  492. while ((pm = vlan->egress_priority_map[i]) != NULL) {
  493. vlan->egress_priority_map[i] = pm->next;
  494. kfree(pm);
  495. }
  496. }
  497. }
  498. static void vlan_dev_uninit(struct net_device *dev)
  499. {
  500. vlan_dev_free_egress_priority(dev);
  501. }
  502. static netdev_features_t vlan_dev_fix_features(struct net_device *dev,
  503. netdev_features_t features)
  504. {
  505. struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  506. netdev_features_t old_features = features;
  507. netdev_features_t lower_features;
  508. lower_features = netdev_intersect_features((real_dev->vlan_features |
  509. NETIF_F_RXCSUM),
  510. real_dev->features);
  511. /* Add HW_CSUM setting to preserve user ability to control
  512. * checksum offload on the vlan device.
  513. */
  514. if (lower_features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
  515. lower_features |= NETIF_F_HW_CSUM;
  516. features = netdev_intersect_features(features, lower_features);
  517. features |= old_features & (NETIF_F_SOFT_FEATURES | NETIF_F_GSO_SOFTWARE);
  518. return features;
  519. }
  520. static int vlan_ethtool_get_link_ksettings(struct net_device *dev,
  521. struct ethtool_link_ksettings *cmd)
  522. {
  523. const struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  524. return __ethtool_get_link_ksettings(vlan->real_dev, cmd);
  525. }
  526. static void vlan_ethtool_get_drvinfo(struct net_device *dev,
  527. struct ethtool_drvinfo *info)
  528. {
  529. strscpy(info->driver, vlan_fullname, sizeof(info->driver));
  530. strscpy(info->version, vlan_version, sizeof(info->version));
  531. strscpy(info->fw_version, "N/A", sizeof(info->fw_version));
  532. }
  533. static int vlan_ethtool_get_ts_info(struct net_device *dev,
  534. struct kernel_ethtool_ts_info *info)
  535. {
  536. const struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  537. return ethtool_get_ts_info_by_layer(vlan->real_dev, info);
  538. }
  539. static void vlan_dev_get_stats64(struct net_device *dev,
  540. struct rtnl_link_stats64 *stats)
  541. {
  542. struct vlan_pcpu_stats *p;
  543. u32 rx_errors = 0, tx_dropped = 0;
  544. int i;
  545. for_each_possible_cpu(i) {
  546. u64 rxpackets, rxbytes, rxmulticast, txpackets, txbytes;
  547. unsigned int start;
  548. p = per_cpu_ptr(vlan_dev_priv(dev)->vlan_pcpu_stats, i);
  549. do {
  550. start = u64_stats_fetch_begin(&p->syncp);
  551. rxpackets = u64_stats_read(&p->rx_packets);
  552. rxbytes = u64_stats_read(&p->rx_bytes);
  553. rxmulticast = u64_stats_read(&p->rx_multicast);
  554. txpackets = u64_stats_read(&p->tx_packets);
  555. txbytes = u64_stats_read(&p->tx_bytes);
  556. } while (u64_stats_fetch_retry(&p->syncp, start));
  557. stats->rx_packets += rxpackets;
  558. stats->rx_bytes += rxbytes;
  559. stats->multicast += rxmulticast;
  560. stats->tx_packets += txpackets;
  561. stats->tx_bytes += txbytes;
  562. /* rx_errors & tx_dropped are u32 */
  563. rx_errors += READ_ONCE(p->rx_errors);
  564. tx_dropped += READ_ONCE(p->tx_dropped);
  565. }
  566. stats->rx_errors = rx_errors;
  567. stats->tx_dropped = tx_dropped;
  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)
  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_obj(*netpoll);
  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(netpoll);
  601. }
  602. #endif /* CONFIG_NET_POLL_CONTROLLER */
  603. static int vlan_dev_get_iflink(const struct net_device *dev)
  604. {
  605. const struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
  606. return READ_ONCE(real_dev->ifindex);
  607. }
  608. static int vlan_dev_fill_forward_path(struct net_device_path_ctx *ctx,
  609. struct net_device_path *path)
  610. {
  611. struct vlan_dev_priv *vlan = vlan_dev_priv(ctx->dev);
  612. path->type = DEV_PATH_VLAN;
  613. path->encap.id = vlan->vlan_id;
  614. path->encap.proto = vlan->vlan_proto;
  615. path->dev = ctx->dev;
  616. ctx->dev = vlan->real_dev;
  617. if (ctx->num_vlans >= ARRAY_SIZE(ctx->vlan))
  618. return -ENOSPC;
  619. ctx->vlan[ctx->num_vlans].id = vlan->vlan_id;
  620. ctx->vlan[ctx->num_vlans].proto = vlan->vlan_proto;
  621. ctx->num_vlans++;
  622. return 0;
  623. }
  624. #if IS_ENABLED(CONFIG_MACSEC)
  625. static const struct macsec_ops *vlan_get_macsec_ops(const struct macsec_context *ctx)
  626. {
  627. return vlan_dev_priv(ctx->netdev)->real_dev->macsec_ops;
  628. }
  629. static int vlan_macsec_offload(int (* const func)(struct macsec_context *),
  630. struct macsec_context *ctx)
  631. {
  632. if (unlikely(!func))
  633. return 0;
  634. return (*func)(ctx);
  635. }
  636. static int vlan_macsec_dev_open(struct macsec_context *ctx)
  637. {
  638. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  639. if (!ops)
  640. return -EOPNOTSUPP;
  641. return vlan_macsec_offload(ops->mdo_dev_open, ctx);
  642. }
  643. static int vlan_macsec_dev_stop(struct macsec_context *ctx)
  644. {
  645. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  646. if (!ops)
  647. return -EOPNOTSUPP;
  648. return vlan_macsec_offload(ops->mdo_dev_stop, ctx);
  649. }
  650. static int vlan_macsec_add_secy(struct macsec_context *ctx)
  651. {
  652. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  653. if (!ops)
  654. return -EOPNOTSUPP;
  655. return vlan_macsec_offload(ops->mdo_add_secy, ctx);
  656. }
  657. static int vlan_macsec_upd_secy(struct macsec_context *ctx)
  658. {
  659. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  660. if (!ops)
  661. return -EOPNOTSUPP;
  662. return vlan_macsec_offload(ops->mdo_upd_secy, ctx);
  663. }
  664. static int vlan_macsec_del_secy(struct macsec_context *ctx)
  665. {
  666. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  667. if (!ops)
  668. return -EOPNOTSUPP;
  669. return vlan_macsec_offload(ops->mdo_del_secy, ctx);
  670. }
  671. static int vlan_macsec_add_rxsc(struct macsec_context *ctx)
  672. {
  673. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  674. if (!ops)
  675. return -EOPNOTSUPP;
  676. return vlan_macsec_offload(ops->mdo_add_rxsc, ctx);
  677. }
  678. static int vlan_macsec_upd_rxsc(struct macsec_context *ctx)
  679. {
  680. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  681. if (!ops)
  682. return -EOPNOTSUPP;
  683. return vlan_macsec_offload(ops->mdo_upd_rxsc, ctx);
  684. }
  685. static int vlan_macsec_del_rxsc(struct macsec_context *ctx)
  686. {
  687. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  688. if (!ops)
  689. return -EOPNOTSUPP;
  690. return vlan_macsec_offload(ops->mdo_del_rxsc, ctx);
  691. }
  692. static int vlan_macsec_add_rxsa(struct macsec_context *ctx)
  693. {
  694. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  695. if (!ops)
  696. return -EOPNOTSUPP;
  697. return vlan_macsec_offload(ops->mdo_add_rxsa, ctx);
  698. }
  699. static int vlan_macsec_upd_rxsa(struct macsec_context *ctx)
  700. {
  701. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  702. if (!ops)
  703. return -EOPNOTSUPP;
  704. return vlan_macsec_offload(ops->mdo_upd_rxsa, ctx);
  705. }
  706. static int vlan_macsec_del_rxsa(struct macsec_context *ctx)
  707. {
  708. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  709. if (!ops)
  710. return -EOPNOTSUPP;
  711. return vlan_macsec_offload(ops->mdo_del_rxsa, ctx);
  712. }
  713. static int vlan_macsec_add_txsa(struct macsec_context *ctx)
  714. {
  715. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  716. if (!ops)
  717. return -EOPNOTSUPP;
  718. return vlan_macsec_offload(ops->mdo_add_txsa, ctx);
  719. }
  720. static int vlan_macsec_upd_txsa(struct macsec_context *ctx)
  721. {
  722. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  723. if (!ops)
  724. return -EOPNOTSUPP;
  725. return vlan_macsec_offload(ops->mdo_upd_txsa, ctx);
  726. }
  727. static int vlan_macsec_del_txsa(struct macsec_context *ctx)
  728. {
  729. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  730. if (!ops)
  731. return -EOPNOTSUPP;
  732. return vlan_macsec_offload(ops->mdo_del_txsa, ctx);
  733. }
  734. static int vlan_macsec_get_dev_stats(struct macsec_context *ctx)
  735. {
  736. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  737. if (!ops)
  738. return -EOPNOTSUPP;
  739. return vlan_macsec_offload(ops->mdo_get_dev_stats, ctx);
  740. }
  741. static int vlan_macsec_get_tx_sc_stats(struct macsec_context *ctx)
  742. {
  743. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  744. if (!ops)
  745. return -EOPNOTSUPP;
  746. return vlan_macsec_offload(ops->mdo_get_tx_sc_stats, ctx);
  747. }
  748. static int vlan_macsec_get_tx_sa_stats(struct macsec_context *ctx)
  749. {
  750. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  751. if (!ops)
  752. return -EOPNOTSUPP;
  753. return vlan_macsec_offload(ops->mdo_get_tx_sa_stats, ctx);
  754. }
  755. static int vlan_macsec_get_rx_sc_stats(struct macsec_context *ctx)
  756. {
  757. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  758. if (!ops)
  759. return -EOPNOTSUPP;
  760. return vlan_macsec_offload(ops->mdo_get_rx_sc_stats, ctx);
  761. }
  762. static int vlan_macsec_get_rx_sa_stats(struct macsec_context *ctx)
  763. {
  764. const struct macsec_ops *ops = vlan_get_macsec_ops(ctx);
  765. if (!ops)
  766. return -EOPNOTSUPP;
  767. return vlan_macsec_offload(ops->mdo_get_rx_sa_stats, ctx);
  768. }
  769. static const struct macsec_ops macsec_offload_ops = {
  770. /* Device wide */
  771. .mdo_dev_open = vlan_macsec_dev_open,
  772. .mdo_dev_stop = vlan_macsec_dev_stop,
  773. /* SecY */
  774. .mdo_add_secy = vlan_macsec_add_secy,
  775. .mdo_upd_secy = vlan_macsec_upd_secy,
  776. .mdo_del_secy = vlan_macsec_del_secy,
  777. /* Security channels */
  778. .mdo_add_rxsc = vlan_macsec_add_rxsc,
  779. .mdo_upd_rxsc = vlan_macsec_upd_rxsc,
  780. .mdo_del_rxsc = vlan_macsec_del_rxsc,
  781. /* Security associations */
  782. .mdo_add_rxsa = vlan_macsec_add_rxsa,
  783. .mdo_upd_rxsa = vlan_macsec_upd_rxsa,
  784. .mdo_del_rxsa = vlan_macsec_del_rxsa,
  785. .mdo_add_txsa = vlan_macsec_add_txsa,
  786. .mdo_upd_txsa = vlan_macsec_upd_txsa,
  787. .mdo_del_txsa = vlan_macsec_del_txsa,
  788. /* Statistics */
  789. .mdo_get_dev_stats = vlan_macsec_get_dev_stats,
  790. .mdo_get_tx_sc_stats = vlan_macsec_get_tx_sc_stats,
  791. .mdo_get_tx_sa_stats = vlan_macsec_get_tx_sa_stats,
  792. .mdo_get_rx_sc_stats = vlan_macsec_get_rx_sc_stats,
  793. .mdo_get_rx_sa_stats = vlan_macsec_get_rx_sa_stats,
  794. };
  795. #endif
  796. static const struct ethtool_ops vlan_ethtool_ops = {
  797. .get_link_ksettings = vlan_ethtool_get_link_ksettings,
  798. .get_drvinfo = vlan_ethtool_get_drvinfo,
  799. .get_link = ethtool_op_get_link,
  800. .get_ts_info = vlan_ethtool_get_ts_info,
  801. };
  802. static const struct net_device_ops vlan_netdev_ops = {
  803. .ndo_change_mtu = vlan_dev_change_mtu,
  804. .ndo_init = vlan_dev_init,
  805. .ndo_uninit = vlan_dev_uninit,
  806. .ndo_open = vlan_dev_open,
  807. .ndo_stop = vlan_dev_stop,
  808. .ndo_start_xmit = vlan_dev_hard_start_xmit,
  809. .ndo_validate_addr = eth_validate_addr,
  810. .ndo_set_mac_address = vlan_dev_set_mac_address,
  811. .ndo_set_rx_mode = vlan_dev_set_rx_mode,
  812. .ndo_change_rx_flags = vlan_dev_change_rx_flags,
  813. .ndo_eth_ioctl = vlan_dev_ioctl,
  814. .ndo_neigh_setup = vlan_dev_neigh_setup,
  815. .ndo_get_stats64 = vlan_dev_get_stats64,
  816. #if IS_ENABLED(CONFIG_FCOE)
  817. .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
  818. .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done,
  819. .ndo_fcoe_enable = vlan_dev_fcoe_enable,
  820. .ndo_fcoe_disable = vlan_dev_fcoe_disable,
  821. .ndo_fcoe_ddp_target = vlan_dev_fcoe_ddp_target,
  822. #endif
  823. #ifdef NETDEV_FCOE_WWNN
  824. .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn,
  825. #endif
  826. #ifdef CONFIG_NET_POLL_CONTROLLER
  827. .ndo_poll_controller = vlan_dev_poll_controller,
  828. .ndo_netpoll_setup = vlan_dev_netpoll_setup,
  829. .ndo_netpoll_cleanup = vlan_dev_netpoll_cleanup,
  830. #endif
  831. .ndo_fix_features = vlan_dev_fix_features,
  832. .ndo_get_iflink = vlan_dev_get_iflink,
  833. .ndo_fill_forward_path = vlan_dev_fill_forward_path,
  834. .ndo_hwtstamp_get = vlan_hwtstamp_get,
  835. .ndo_hwtstamp_set = vlan_hwtstamp_set,
  836. };
  837. static void vlan_dev_free(struct net_device *dev)
  838. {
  839. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  840. free_percpu(vlan->vlan_pcpu_stats);
  841. vlan->vlan_pcpu_stats = NULL;
  842. /* Get rid of the vlan's reference to real_dev */
  843. netdev_put(vlan->real_dev, &vlan->dev_tracker);
  844. }
  845. void vlan_setup(struct net_device *dev)
  846. {
  847. ether_setup(dev);
  848. dev->priv_flags |= IFF_802_1Q_VLAN | IFF_NO_QUEUE;
  849. dev->priv_flags |= IFF_UNICAST_FLT;
  850. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  851. netif_keep_dst(dev);
  852. dev->netdev_ops = &vlan_netdev_ops;
  853. dev->needs_free_netdev = true;
  854. dev->priv_destructor = vlan_dev_free;
  855. dev->ethtool_ops = &vlan_ethtool_ops;
  856. #if IS_ENABLED(CONFIG_MACSEC)
  857. dev->macsec_ops = &macsec_offload_ops;
  858. #endif
  859. dev->min_mtu = 0;
  860. dev->max_mtu = ETH_MAX_MTU;
  861. eth_zero_addr(dev->broadcast);
  862. }