vlan_dev.c 24 KB

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