vlan_dev.c 21 KB

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