vlan_dev.c 23 KB

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