vlan_dev.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/ethtool.h>
  28. #include <net/arp.h>
  29. #include "vlan.h"
  30. #include "vlanproc.h"
  31. #include <linux/if_vlan.h>
  32. /*
  33. * Rebuild the Ethernet MAC header. This is called after an ARP
  34. * (or in future other address resolution) has completed on this
  35. * sk_buff. We now let ARP fill in the other fields.
  36. *
  37. * This routine CANNOT use cached dst->neigh!
  38. * Really, it is used only when dst->neigh is wrong.
  39. *
  40. * TODO: This needs a checkup, I'm ignorant here. --BLG
  41. */
  42. static int vlan_dev_rebuild_header(struct sk_buff *skb)
  43. {
  44. struct net_device *dev = skb->dev;
  45. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  46. switch (veth->h_vlan_encapsulated_proto) {
  47. #ifdef CONFIG_INET
  48. case htons(ETH_P_IP):
  49. /* TODO: Confirm this will work with VLAN headers... */
  50. return arp_find(veth->h_dest, skb);
  51. #endif
  52. default:
  53. pr_debug("%s: unable to resolve type %X addresses.\n",
  54. dev->name, ntohs(veth->h_vlan_encapsulated_proto));
  55. memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
  56. break;
  57. }
  58. return 0;
  59. }
  60. static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb)
  61. {
  62. if (vlan_dev_info(skb->dev)->flags & VLAN_FLAG_REORDER_HDR) {
  63. if (skb_cow(skb, skb_headroom(skb)) < 0)
  64. skb = NULL;
  65. if (skb) {
  66. /* Lifted from Gleb's VLAN code... */
  67. memmove(skb->data - ETH_HLEN,
  68. skb->data - VLAN_ETH_HLEN, 12);
  69. skb->mac_header += VLAN_HLEN;
  70. }
  71. }
  72. return skb;
  73. }
  74. static inline void vlan_set_encap_proto(struct sk_buff *skb,
  75. struct vlan_hdr *vhdr)
  76. {
  77. __be16 proto;
  78. unsigned char *rawp;
  79. /*
  80. * Was a VLAN packet, grab the encapsulated protocol, which the layer
  81. * three protocols care about.
  82. */
  83. proto = vhdr->h_vlan_encapsulated_proto;
  84. if (ntohs(proto) >= 1536) {
  85. skb->protocol = proto;
  86. return;
  87. }
  88. rawp = skb->data;
  89. if (*(unsigned short *)rawp == 0xFFFF)
  90. /*
  91. * This is a magic hack to spot IPX packets. Older Novell
  92. * breaks the protocol design and runs IPX over 802.3 without
  93. * an 802.2 LLC layer. We look for FFFF which isn't a used
  94. * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
  95. * but does for the rest.
  96. */
  97. skb->protocol = htons(ETH_P_802_3);
  98. else
  99. /*
  100. * Real 802.2 LLC
  101. */
  102. skb->protocol = htons(ETH_P_802_2);
  103. }
  104. /*
  105. * Determine the packet's protocol ID. The rule here is that we
  106. * assume 802.3 if the type field is short enough to be a length.
  107. * This is normal practice and works for any 'now in use' protocol.
  108. *
  109. * Also, at this point we assume that we ARE dealing exclusively with
  110. * VLAN packets, or packets that should be made into VLAN packets based
  111. * on a default VLAN ID.
  112. *
  113. * NOTE: Should be similar to ethernet/eth.c.
  114. *
  115. * SANITY NOTE: This method is called when a packet is moving up the stack
  116. * towards userland. To get here, it would have already passed
  117. * through the ethernet/eth.c eth_type_trans() method.
  118. * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
  119. * stored UNALIGNED in the memory. RISC systems don't like
  120. * such cases very much...
  121. * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be
  122. * aligned, so there doesn't need to be any of the unaligned
  123. * stuff. It has been commented out now... --Ben
  124. *
  125. */
  126. int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
  127. struct packet_type *ptype, struct net_device *orig_dev)
  128. {
  129. struct vlan_hdr *vhdr;
  130. struct vlan_pcpu_stats *rx_stats;
  131. struct net_device *vlan_dev;
  132. u16 vlan_id;
  133. u16 vlan_tci;
  134. skb = skb_share_check(skb, GFP_ATOMIC);
  135. if (skb == NULL)
  136. goto err_free;
  137. if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
  138. goto err_free;
  139. vhdr = (struct vlan_hdr *)skb->data;
  140. vlan_tci = ntohs(vhdr->h_vlan_TCI);
  141. vlan_id = vlan_tci & VLAN_VID_MASK;
  142. rcu_read_lock();
  143. vlan_dev = vlan_find_dev(dev, vlan_id);
  144. /* If the VLAN device is defined, we use it.
  145. * If not, and the VID is 0, it is a 802.1p packet (not
  146. * really a VLAN), so we will just netif_rx it later to the
  147. * original interface, but with the skb->proto set to the
  148. * wrapped proto: we do nothing here.
  149. */
  150. if (!vlan_dev) {
  151. if (vlan_id) {
  152. pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
  153. __func__, vlan_id, dev->name);
  154. goto err_unlock;
  155. }
  156. rx_stats = NULL;
  157. } else {
  158. skb->dev = vlan_dev;
  159. rx_stats = this_cpu_ptr(vlan_dev_info(skb->dev)->vlan_pcpu_stats);
  160. u64_stats_update_begin(&rx_stats->syncp);
  161. rx_stats->rx_packets++;
  162. rx_stats->rx_bytes += skb->len;
  163. skb->priority = vlan_get_ingress_priority(skb->dev, vlan_tci);
  164. pr_debug("%s: priority: %u for TCI: %hu\n",
  165. __func__, skb->priority, vlan_tci);
  166. switch (skb->pkt_type) {
  167. case PACKET_BROADCAST:
  168. /* Yeah, stats collect these together.. */
  169. /* stats->broadcast ++; // no such counter :-( */
  170. break;
  171. case PACKET_MULTICAST:
  172. rx_stats->rx_multicast++;
  173. break;
  174. case PACKET_OTHERHOST:
  175. /* Our lower layer thinks this is not local, let's make
  176. * sure.
  177. * This allows the VLAN to have a different MAC than the
  178. * underlying device, and still route correctly.
  179. */
  180. if (!compare_ether_addr(eth_hdr(skb)->h_dest,
  181. skb->dev->dev_addr))
  182. skb->pkt_type = PACKET_HOST;
  183. break;
  184. default:
  185. break;
  186. }
  187. u64_stats_update_end(&rx_stats->syncp);
  188. }
  189. skb_pull_rcsum(skb, VLAN_HLEN);
  190. vlan_set_encap_proto(skb, vhdr);
  191. if (vlan_dev) {
  192. skb = vlan_check_reorder_header(skb);
  193. if (!skb) {
  194. rx_stats->rx_errors++;
  195. goto err_unlock;
  196. }
  197. }
  198. netif_rx(skb);
  199. rcu_read_unlock();
  200. return NET_RX_SUCCESS;
  201. err_unlock:
  202. rcu_read_unlock();
  203. err_free:
  204. atomic_long_inc(&dev->rx_dropped);
  205. kfree_skb(skb);
  206. return NET_RX_DROP;
  207. }
  208. static inline u16
  209. vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
  210. {
  211. struct vlan_priority_tci_mapping *mp;
  212. mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
  213. while (mp) {
  214. if (mp->priority == skb->priority) {
  215. return mp->vlan_qos; /* This should already be shifted
  216. * to mask correctly with the
  217. * VLAN's TCI */
  218. }
  219. mp = mp->next;
  220. }
  221. return 0;
  222. }
  223. /*
  224. * Create the VLAN header for an arbitrary protocol layer
  225. *
  226. * saddr=NULL means use device source address
  227. * daddr=NULL means leave destination address (eg unresolved arp)
  228. *
  229. * This is called when the SKB is moving down the stack towards the
  230. * physical devices.
  231. */
  232. static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
  233. unsigned short type,
  234. const void *daddr, const void *saddr,
  235. unsigned int len)
  236. {
  237. struct vlan_hdr *vhdr;
  238. unsigned int vhdrlen = 0;
  239. u16 vlan_tci = 0;
  240. int rc;
  241. if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR)) {
  242. vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
  243. vlan_tci = vlan_dev_info(dev)->vlan_id;
  244. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
  245. vhdr->h_vlan_TCI = htons(vlan_tci);
  246. /*
  247. * Set the protocol type. For a packet of type ETH_P_802_3/2 we
  248. * put the length in here instead.
  249. */
  250. if (type != ETH_P_802_3 && type != ETH_P_802_2)
  251. vhdr->h_vlan_encapsulated_proto = htons(type);
  252. else
  253. vhdr->h_vlan_encapsulated_proto = htons(len);
  254. skb->protocol = htons(ETH_P_8021Q);
  255. type = ETH_P_8021Q;
  256. vhdrlen = VLAN_HLEN;
  257. }
  258. /* Before delegating work to the lower layer, enter our MAC-address */
  259. if (saddr == NULL)
  260. saddr = dev->dev_addr;
  261. /* Now make the underlying real hard header */
  262. dev = vlan_dev_info(dev)->real_dev;
  263. rc = dev_hard_header(skb, dev, type, daddr, saddr, len + vhdrlen);
  264. if (rc > 0)
  265. rc += vhdrlen;
  266. return rc;
  267. }
  268. static netdev_tx_t vlan_dev_hard_start_xmit(struct sk_buff *skb,
  269. struct net_device *dev)
  270. {
  271. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  272. unsigned int len;
  273. int ret;
  274. /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
  275. *
  276. * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
  277. * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
  278. */
  279. if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
  280. vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
  281. u16 vlan_tci;
  282. vlan_tci = vlan_dev_info(dev)->vlan_id;
  283. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
  284. skb = __vlan_hwaccel_put_tag(skb, vlan_tci);
  285. }
  286. skb_set_dev(skb, vlan_dev_info(dev)->real_dev);
  287. len = skb->len;
  288. ret = dev_queue_xmit(skb);
  289. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  290. struct vlan_pcpu_stats *stats;
  291. stats = this_cpu_ptr(vlan_dev_info(dev)->vlan_pcpu_stats);
  292. u64_stats_update_begin(&stats->syncp);
  293. stats->tx_packets++;
  294. stats->tx_bytes += len;
  295. u64_stats_update_begin(&stats->syncp);
  296. } else {
  297. this_cpu_inc(vlan_dev_info(dev)->vlan_pcpu_stats->tx_dropped);
  298. }
  299. return ret;
  300. }
  301. static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
  302. {
  303. /* TODO: gotta make sure the underlying layer can handle it,
  304. * maybe an IFF_VLAN_CAPABLE flag for devices?
  305. */
  306. if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
  307. return -ERANGE;
  308. dev->mtu = new_mtu;
  309. return 0;
  310. }
  311. void vlan_dev_set_ingress_priority(const struct net_device *dev,
  312. u32 skb_prio, u16 vlan_prio)
  313. {
  314. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  315. if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
  316. vlan->nr_ingress_mappings--;
  317. else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
  318. vlan->nr_ingress_mappings++;
  319. vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
  320. }
  321. int vlan_dev_set_egress_priority(const struct net_device *dev,
  322. u32 skb_prio, u16 vlan_prio)
  323. {
  324. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  325. struct vlan_priority_tci_mapping *mp = NULL;
  326. struct vlan_priority_tci_mapping *np;
  327. u32 vlan_qos = (vlan_prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
  328. /* See if a priority mapping exists.. */
  329. mp = vlan->egress_priority_map[skb_prio & 0xF];
  330. while (mp) {
  331. if (mp->priority == skb_prio) {
  332. if (mp->vlan_qos && !vlan_qos)
  333. vlan->nr_egress_mappings--;
  334. else if (!mp->vlan_qos && vlan_qos)
  335. vlan->nr_egress_mappings++;
  336. mp->vlan_qos = vlan_qos;
  337. return 0;
  338. }
  339. mp = mp->next;
  340. }
  341. /* Create a new mapping then. */
  342. mp = vlan->egress_priority_map[skb_prio & 0xF];
  343. np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
  344. if (!np)
  345. return -ENOBUFS;
  346. np->next = mp;
  347. np->priority = skb_prio;
  348. np->vlan_qos = vlan_qos;
  349. vlan->egress_priority_map[skb_prio & 0xF] = np;
  350. if (vlan_qos)
  351. vlan->nr_egress_mappings++;
  352. return 0;
  353. }
  354. /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
  355. int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
  356. {
  357. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  358. u32 old_flags = vlan->flags;
  359. if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
  360. VLAN_FLAG_LOOSE_BINDING))
  361. return -EINVAL;
  362. vlan->flags = (old_flags & ~mask) | (flags & mask);
  363. if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
  364. if (vlan->flags & VLAN_FLAG_GVRP)
  365. vlan_gvrp_request_join(dev);
  366. else
  367. vlan_gvrp_request_leave(dev);
  368. }
  369. return 0;
  370. }
  371. void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
  372. {
  373. strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
  374. }
  375. static int vlan_dev_open(struct net_device *dev)
  376. {
  377. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  378. struct net_device *real_dev = vlan->real_dev;
  379. int err;
  380. if (!(real_dev->flags & IFF_UP) &&
  381. !(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
  382. return -ENETDOWN;
  383. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
  384. err = dev_uc_add(real_dev, dev->dev_addr);
  385. if (err < 0)
  386. goto out;
  387. }
  388. if (dev->flags & IFF_ALLMULTI) {
  389. err = dev_set_allmulti(real_dev, 1);
  390. if (err < 0)
  391. goto del_unicast;
  392. }
  393. if (dev->flags & IFF_PROMISC) {
  394. err = dev_set_promiscuity(real_dev, 1);
  395. if (err < 0)
  396. goto clear_allmulti;
  397. }
  398. memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
  399. if (vlan->flags & VLAN_FLAG_GVRP)
  400. vlan_gvrp_request_join(dev);
  401. if (netif_carrier_ok(real_dev))
  402. netif_carrier_on(dev);
  403. return 0;
  404. clear_allmulti:
  405. if (dev->flags & IFF_ALLMULTI)
  406. dev_set_allmulti(real_dev, -1);
  407. del_unicast:
  408. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  409. dev_uc_del(real_dev, dev->dev_addr);
  410. out:
  411. netif_carrier_off(dev);
  412. return err;
  413. }
  414. static int vlan_dev_stop(struct net_device *dev)
  415. {
  416. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  417. struct net_device *real_dev = vlan->real_dev;
  418. if (vlan->flags & VLAN_FLAG_GVRP)
  419. vlan_gvrp_request_leave(dev);
  420. dev_mc_unsync(real_dev, dev);
  421. dev_uc_unsync(real_dev, dev);
  422. if (dev->flags & IFF_ALLMULTI)
  423. dev_set_allmulti(real_dev, -1);
  424. if (dev->flags & IFF_PROMISC)
  425. dev_set_promiscuity(real_dev, -1);
  426. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  427. dev_uc_del(real_dev, dev->dev_addr);
  428. netif_carrier_off(dev);
  429. return 0;
  430. }
  431. static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
  432. {
  433. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  434. struct sockaddr *addr = p;
  435. int err;
  436. if (!is_valid_ether_addr(addr->sa_data))
  437. return -EADDRNOTAVAIL;
  438. if (!(dev->flags & IFF_UP))
  439. goto out;
  440. if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
  441. err = dev_uc_add(real_dev, addr->sa_data);
  442. if (err < 0)
  443. return err;
  444. }
  445. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  446. dev_uc_del(real_dev, dev->dev_addr);
  447. out:
  448. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  449. return 0;
  450. }
  451. static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  452. {
  453. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  454. const struct net_device_ops *ops = real_dev->netdev_ops;
  455. struct ifreq ifrr;
  456. int err = -EOPNOTSUPP;
  457. strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
  458. ifrr.ifr_ifru = ifr->ifr_ifru;
  459. switch (cmd) {
  460. case SIOCGMIIPHY:
  461. case SIOCGMIIREG:
  462. case SIOCSMIIREG:
  463. if (netif_device_present(real_dev) && ops->ndo_do_ioctl)
  464. err = ops->ndo_do_ioctl(real_dev, &ifrr, cmd);
  465. break;
  466. }
  467. if (!err)
  468. ifr->ifr_ifru = ifrr.ifr_ifru;
  469. return err;
  470. }
  471. static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa)
  472. {
  473. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  474. const struct net_device_ops *ops = real_dev->netdev_ops;
  475. int err = 0;
  476. if (netif_device_present(real_dev) && ops->ndo_neigh_setup)
  477. err = ops->ndo_neigh_setup(real_dev, pa);
  478. return err;
  479. }
  480. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  481. static int vlan_dev_fcoe_ddp_setup(struct net_device *dev, u16 xid,
  482. struct scatterlist *sgl, unsigned int sgc)
  483. {
  484. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  485. const struct net_device_ops *ops = real_dev->netdev_ops;
  486. int rc = 0;
  487. if (ops->ndo_fcoe_ddp_setup)
  488. rc = ops->ndo_fcoe_ddp_setup(real_dev, xid, sgl, sgc);
  489. return rc;
  490. }
  491. static int vlan_dev_fcoe_ddp_done(struct net_device *dev, u16 xid)
  492. {
  493. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  494. const struct net_device_ops *ops = real_dev->netdev_ops;
  495. int len = 0;
  496. if (ops->ndo_fcoe_ddp_done)
  497. len = ops->ndo_fcoe_ddp_done(real_dev, xid);
  498. return len;
  499. }
  500. static int vlan_dev_fcoe_enable(struct net_device *dev)
  501. {
  502. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  503. const struct net_device_ops *ops = real_dev->netdev_ops;
  504. int rc = -EINVAL;
  505. if (ops->ndo_fcoe_enable)
  506. rc = ops->ndo_fcoe_enable(real_dev);
  507. return rc;
  508. }
  509. static int vlan_dev_fcoe_disable(struct net_device *dev)
  510. {
  511. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  512. const struct net_device_ops *ops = real_dev->netdev_ops;
  513. int rc = -EINVAL;
  514. if (ops->ndo_fcoe_disable)
  515. rc = ops->ndo_fcoe_disable(real_dev);
  516. return rc;
  517. }
  518. static int vlan_dev_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
  519. {
  520. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  521. const struct net_device_ops *ops = real_dev->netdev_ops;
  522. int rc = -EINVAL;
  523. if (ops->ndo_fcoe_get_wwn)
  524. rc = ops->ndo_fcoe_get_wwn(real_dev, wwn, type);
  525. return rc;
  526. }
  527. static int vlan_dev_fcoe_ddp_target(struct net_device *dev, u16 xid,
  528. struct scatterlist *sgl, unsigned int sgc)
  529. {
  530. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  531. const struct net_device_ops *ops = real_dev->netdev_ops;
  532. int rc = 0;
  533. if (ops->ndo_fcoe_ddp_target)
  534. rc = ops->ndo_fcoe_ddp_target(real_dev, xid, sgl, sgc);
  535. return rc;
  536. }
  537. #endif
  538. static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
  539. {
  540. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  541. if (change & IFF_ALLMULTI)
  542. dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  543. if (change & IFF_PROMISC)
  544. dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
  545. }
  546. static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
  547. {
  548. dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
  549. dev_uc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
  550. }
  551. /*
  552. * vlan network devices have devices nesting below it, and are a special
  553. * "super class" of normal network devices; split their locks off into a
  554. * separate class since they always nest.
  555. */
  556. static struct lock_class_key vlan_netdev_xmit_lock_key;
  557. static struct lock_class_key vlan_netdev_addr_lock_key;
  558. static void vlan_dev_set_lockdep_one(struct net_device *dev,
  559. struct netdev_queue *txq,
  560. void *_subclass)
  561. {
  562. lockdep_set_class_and_subclass(&txq->_xmit_lock,
  563. &vlan_netdev_xmit_lock_key,
  564. *(int *)_subclass);
  565. }
  566. static void vlan_dev_set_lockdep_class(struct net_device *dev, int subclass)
  567. {
  568. lockdep_set_class_and_subclass(&dev->addr_list_lock,
  569. &vlan_netdev_addr_lock_key,
  570. subclass);
  571. netdev_for_each_tx_queue(dev, vlan_dev_set_lockdep_one, &subclass);
  572. }
  573. static const struct header_ops vlan_header_ops = {
  574. .create = vlan_dev_hard_header,
  575. .rebuild = vlan_dev_rebuild_header,
  576. .parse = eth_header_parse,
  577. };
  578. static const struct net_device_ops vlan_netdev_ops;
  579. static int vlan_dev_init(struct net_device *dev)
  580. {
  581. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  582. int subclass = 0;
  583. netif_carrier_off(dev);
  584. /* IFF_BROADCAST|IFF_MULTICAST; ??? */
  585. dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
  586. IFF_MASTER | IFF_SLAVE);
  587. dev->iflink = real_dev->ifindex;
  588. dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
  589. (1<<__LINK_STATE_DORMANT))) |
  590. (1<<__LINK_STATE_PRESENT);
  591. dev->features |= real_dev->features & real_dev->vlan_features;
  592. dev->features |= NETIF_F_LLTX;
  593. dev->gso_max_size = real_dev->gso_max_size;
  594. /* ipv6 shared card related stuff */
  595. dev->dev_id = real_dev->dev_id;
  596. if (is_zero_ether_addr(dev->dev_addr))
  597. memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
  598. if (is_zero_ether_addr(dev->broadcast))
  599. memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
  600. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  601. dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid;
  602. #endif
  603. dev->needed_headroom = real_dev->needed_headroom;
  604. if (real_dev->features & NETIF_F_HW_VLAN_TX) {
  605. dev->header_ops = real_dev->header_ops;
  606. dev->hard_header_len = real_dev->hard_header_len;
  607. } else {
  608. dev->header_ops = &vlan_header_ops;
  609. dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
  610. }
  611. dev->netdev_ops = &vlan_netdev_ops;
  612. if (is_vlan_dev(real_dev))
  613. subclass = 1;
  614. vlan_dev_set_lockdep_class(dev, subclass);
  615. vlan_dev_info(dev)->vlan_pcpu_stats = alloc_percpu(struct vlan_pcpu_stats);
  616. if (!vlan_dev_info(dev)->vlan_pcpu_stats)
  617. return -ENOMEM;
  618. return 0;
  619. }
  620. static void vlan_dev_uninit(struct net_device *dev)
  621. {
  622. struct vlan_priority_tci_mapping *pm;
  623. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  624. int i;
  625. free_percpu(vlan->vlan_pcpu_stats);
  626. vlan->vlan_pcpu_stats = NULL;
  627. for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
  628. while ((pm = vlan->egress_priority_map[i]) != NULL) {
  629. vlan->egress_priority_map[i] = pm->next;
  630. kfree(pm);
  631. }
  632. }
  633. }
  634. static int vlan_ethtool_get_settings(struct net_device *dev,
  635. struct ethtool_cmd *cmd)
  636. {
  637. const struct vlan_dev_info *vlan = vlan_dev_info(dev);
  638. return dev_ethtool_get_settings(vlan->real_dev, cmd);
  639. }
  640. static void vlan_ethtool_get_drvinfo(struct net_device *dev,
  641. struct ethtool_drvinfo *info)
  642. {
  643. strcpy(info->driver, vlan_fullname);
  644. strcpy(info->version, vlan_version);
  645. strcpy(info->fw_version, "N/A");
  646. }
  647. static u32 vlan_ethtool_get_rx_csum(struct net_device *dev)
  648. {
  649. const struct vlan_dev_info *vlan = vlan_dev_info(dev);
  650. return dev_ethtool_get_rx_csum(vlan->real_dev);
  651. }
  652. static u32 vlan_ethtool_get_flags(struct net_device *dev)
  653. {
  654. const struct vlan_dev_info *vlan = vlan_dev_info(dev);
  655. return dev_ethtool_get_flags(vlan->real_dev);
  656. }
  657. static struct rtnl_link_stats64 *vlan_dev_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  658. {
  659. if (vlan_dev_info(dev)->vlan_pcpu_stats) {
  660. struct vlan_pcpu_stats *p;
  661. u32 rx_errors = 0, tx_dropped = 0;
  662. int i;
  663. for_each_possible_cpu(i) {
  664. u64 rxpackets, rxbytes, rxmulticast, txpackets, txbytes;
  665. unsigned int start;
  666. p = per_cpu_ptr(vlan_dev_info(dev)->vlan_pcpu_stats, i);
  667. do {
  668. start = u64_stats_fetch_begin_bh(&p->syncp);
  669. rxpackets = p->rx_packets;
  670. rxbytes = p->rx_bytes;
  671. rxmulticast = p->rx_multicast;
  672. txpackets = p->tx_packets;
  673. txbytes = p->tx_bytes;
  674. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  675. stats->rx_packets += rxpackets;
  676. stats->rx_bytes += rxbytes;
  677. stats->multicast += rxmulticast;
  678. stats->tx_packets += txpackets;
  679. stats->tx_bytes += txbytes;
  680. /* rx_errors & tx_dropped are u32 */
  681. rx_errors += p->rx_errors;
  682. tx_dropped += p->tx_dropped;
  683. }
  684. stats->rx_errors = rx_errors;
  685. stats->tx_dropped = tx_dropped;
  686. }
  687. return stats;
  688. }
  689. static int vlan_ethtool_set_tso(struct net_device *dev, u32 data)
  690. {
  691. if (data) {
  692. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  693. /* Underlying device must support TSO for VLAN-tagged packets
  694. * and must have TSO enabled now.
  695. */
  696. if (!(real_dev->vlan_features & NETIF_F_TSO))
  697. return -EOPNOTSUPP;
  698. if (!(real_dev->features & NETIF_F_TSO))
  699. return -EINVAL;
  700. dev->features |= NETIF_F_TSO;
  701. } else {
  702. dev->features &= ~NETIF_F_TSO;
  703. }
  704. return 0;
  705. }
  706. static const struct ethtool_ops vlan_ethtool_ops = {
  707. .get_settings = vlan_ethtool_get_settings,
  708. .get_drvinfo = vlan_ethtool_get_drvinfo,
  709. .get_link = ethtool_op_get_link,
  710. .get_rx_csum = vlan_ethtool_get_rx_csum,
  711. .get_flags = vlan_ethtool_get_flags,
  712. .set_tso = vlan_ethtool_set_tso,
  713. };
  714. static const struct net_device_ops vlan_netdev_ops = {
  715. .ndo_change_mtu = vlan_dev_change_mtu,
  716. .ndo_init = vlan_dev_init,
  717. .ndo_uninit = vlan_dev_uninit,
  718. .ndo_open = vlan_dev_open,
  719. .ndo_stop = vlan_dev_stop,
  720. .ndo_start_xmit = vlan_dev_hard_start_xmit,
  721. .ndo_validate_addr = eth_validate_addr,
  722. .ndo_set_mac_address = vlan_dev_set_mac_address,
  723. .ndo_set_rx_mode = vlan_dev_set_rx_mode,
  724. .ndo_set_multicast_list = vlan_dev_set_rx_mode,
  725. .ndo_change_rx_flags = vlan_dev_change_rx_flags,
  726. .ndo_do_ioctl = vlan_dev_ioctl,
  727. .ndo_neigh_setup = vlan_dev_neigh_setup,
  728. .ndo_get_stats64 = vlan_dev_get_stats64,
  729. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  730. .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
  731. .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done,
  732. .ndo_fcoe_enable = vlan_dev_fcoe_enable,
  733. .ndo_fcoe_disable = vlan_dev_fcoe_disable,
  734. .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn,
  735. .ndo_fcoe_ddp_target = vlan_dev_fcoe_ddp_target,
  736. #endif
  737. };
  738. void vlan_setup(struct net_device *dev)
  739. {
  740. ether_setup(dev);
  741. dev->priv_flags |= IFF_802_1Q_VLAN;
  742. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  743. dev->tx_queue_len = 0;
  744. dev->netdev_ops = &vlan_netdev_ops;
  745. dev->destructor = free_netdev;
  746. dev->ethtool_ops = &vlan_ethtool_ops;
  747. memset(dev->broadcast, 0, ETH_ALEN);
  748. }