vlan_dev.c 28 KB

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