vlan_dev.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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_rx_stats *rx_stats;
  131. u16 vlan_id;
  132. u16 vlan_tci;
  133. skb = skb_share_check(skb, GFP_ATOMIC);
  134. if (skb == NULL)
  135. goto err_free;
  136. if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
  137. goto err_free;
  138. vhdr = (struct vlan_hdr *)skb->data;
  139. vlan_tci = ntohs(vhdr->h_vlan_TCI);
  140. vlan_id = vlan_tci & VLAN_VID_MASK;
  141. rcu_read_lock();
  142. skb->dev = __find_vlan_dev(dev, vlan_id);
  143. if (!skb->dev) {
  144. pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
  145. __func__, vlan_id, dev->name);
  146. goto err_unlock;
  147. }
  148. rx_stats = per_cpu_ptr(vlan_dev_info(skb->dev)->vlan_rx_stats,
  149. smp_processor_id());
  150. u64_stats_update_begin(&rx_stats->syncp);
  151. rx_stats->rx_packets++;
  152. rx_stats->rx_bytes += skb->len;
  153. skb_pull_rcsum(skb, VLAN_HLEN);
  154. skb->priority = vlan_get_ingress_priority(skb->dev, vlan_tci);
  155. pr_debug("%s: priority: %u for TCI: %hu\n",
  156. __func__, skb->priority, vlan_tci);
  157. switch (skb->pkt_type) {
  158. case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
  159. /* stats->broadcast ++; // no such counter :-( */
  160. break;
  161. case PACKET_MULTICAST:
  162. rx_stats->rx_multicast++;
  163. break;
  164. case PACKET_OTHERHOST:
  165. /* Our lower layer thinks this is not local, let's make sure.
  166. * This allows the VLAN to have a different MAC than the
  167. * underlying device, and still route correctly.
  168. */
  169. if (!compare_ether_addr(eth_hdr(skb)->h_dest,
  170. skb->dev->dev_addr))
  171. skb->pkt_type = PACKET_HOST;
  172. break;
  173. default:
  174. break;
  175. }
  176. u64_stats_update_end(&rx_stats->syncp);
  177. vlan_set_encap_proto(skb, vhdr);
  178. skb = vlan_check_reorder_header(skb);
  179. if (!skb) {
  180. rx_stats->rx_errors++;
  181. goto err_unlock;
  182. }
  183. netif_rx(skb);
  184. rcu_read_unlock();
  185. return NET_RX_SUCCESS;
  186. err_unlock:
  187. rcu_read_unlock();
  188. err_free:
  189. kfree_skb(skb);
  190. return NET_RX_DROP;
  191. }
  192. static inline u16
  193. vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
  194. {
  195. struct vlan_priority_tci_mapping *mp;
  196. mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
  197. while (mp) {
  198. if (mp->priority == skb->priority) {
  199. return mp->vlan_qos; /* This should already be shifted
  200. * to mask correctly with the
  201. * VLAN's TCI */
  202. }
  203. mp = mp->next;
  204. }
  205. return 0;
  206. }
  207. /*
  208. * Create the VLAN header for an arbitrary protocol layer
  209. *
  210. * saddr=NULL means use device source address
  211. * daddr=NULL means leave destination address (eg unresolved arp)
  212. *
  213. * This is called when the SKB is moving down the stack towards the
  214. * physical devices.
  215. */
  216. static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
  217. unsigned short type,
  218. const void *daddr, const void *saddr,
  219. unsigned int len)
  220. {
  221. struct vlan_hdr *vhdr;
  222. unsigned int vhdrlen = 0;
  223. u16 vlan_tci = 0;
  224. int rc;
  225. if (WARN_ON(skb_headroom(skb) < dev->hard_header_len))
  226. return -ENOSPC;
  227. if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR)) {
  228. vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
  229. vlan_tci = vlan_dev_info(dev)->vlan_id;
  230. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
  231. vhdr->h_vlan_TCI = htons(vlan_tci);
  232. /*
  233. * Set the protocol type. For a packet of type ETH_P_802_3/2 we
  234. * put the length in here instead.
  235. */
  236. if (type != ETH_P_802_3 && type != ETH_P_802_2)
  237. vhdr->h_vlan_encapsulated_proto = htons(type);
  238. else
  239. vhdr->h_vlan_encapsulated_proto = htons(len);
  240. skb->protocol = htons(ETH_P_8021Q);
  241. type = ETH_P_8021Q;
  242. vhdrlen = VLAN_HLEN;
  243. }
  244. /* Before delegating work to the lower layer, enter our MAC-address */
  245. if (saddr == NULL)
  246. saddr = dev->dev_addr;
  247. /* Now make the underlying real hard header */
  248. dev = vlan_dev_info(dev)->real_dev;
  249. rc = dev_hard_header(skb, dev, type, daddr, saddr, len + vhdrlen);
  250. if (rc > 0)
  251. rc += vhdrlen;
  252. return rc;
  253. }
  254. static netdev_tx_t vlan_dev_hard_start_xmit(struct sk_buff *skb,
  255. struct net_device *dev)
  256. {
  257. int i = skb_get_queue_mapping(skb);
  258. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  259. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  260. unsigned int len;
  261. int ret;
  262. /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
  263. *
  264. * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
  265. * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
  266. */
  267. if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
  268. vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
  269. unsigned int orig_headroom = skb_headroom(skb);
  270. u16 vlan_tci;
  271. vlan_dev_info(dev)->cnt_encap_on_xmit++;
  272. vlan_tci = vlan_dev_info(dev)->vlan_id;
  273. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
  274. skb = __vlan_put_tag(skb, vlan_tci);
  275. if (!skb) {
  276. txq->tx_dropped++;
  277. return NETDEV_TX_OK;
  278. }
  279. if (orig_headroom < VLAN_HLEN)
  280. vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
  281. }
  282. skb_set_dev(skb, vlan_dev_info(dev)->real_dev);
  283. len = skb->len;
  284. ret = dev_queue_xmit(skb);
  285. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  286. txq->tx_packets++;
  287. txq->tx_bytes += len;
  288. } else
  289. txq->tx_dropped++;
  290. return ret;
  291. }
  292. static netdev_tx_t vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
  293. struct net_device *dev)
  294. {
  295. int i = skb_get_queue_mapping(skb);
  296. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  297. u16 vlan_tci;
  298. unsigned int len;
  299. int ret;
  300. vlan_tci = vlan_dev_info(dev)->vlan_id;
  301. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
  302. skb = __vlan_hwaccel_put_tag(skb, vlan_tci);
  303. skb->dev = vlan_dev_info(dev)->real_dev;
  304. len = skb->len;
  305. ret = dev_queue_xmit(skb);
  306. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  307. txq->tx_packets++;
  308. txq->tx_bytes += len;
  309. } else
  310. txq->tx_dropped++;
  311. return ret;
  312. }
  313. static u16 vlan_dev_select_queue(struct net_device *dev, struct sk_buff *skb)
  314. {
  315. struct net_device *rdev = vlan_dev_info(dev)->real_dev;
  316. const struct net_device_ops *ops = rdev->netdev_ops;
  317. return ops->ndo_select_queue(rdev, skb);
  318. }
  319. static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
  320. {
  321. /* TODO: gotta make sure the underlying layer can handle it,
  322. * maybe an IFF_VLAN_CAPABLE flag for devices?
  323. */
  324. if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
  325. return -ERANGE;
  326. dev->mtu = new_mtu;
  327. return 0;
  328. }
  329. void vlan_dev_set_ingress_priority(const struct net_device *dev,
  330. u32 skb_prio, u16 vlan_prio)
  331. {
  332. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  333. if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
  334. vlan->nr_ingress_mappings--;
  335. else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
  336. vlan->nr_ingress_mappings++;
  337. vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
  338. }
  339. int vlan_dev_set_egress_priority(const struct net_device *dev,
  340. u32 skb_prio, u16 vlan_prio)
  341. {
  342. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  343. struct vlan_priority_tci_mapping *mp = NULL;
  344. struct vlan_priority_tci_mapping *np;
  345. u32 vlan_qos = (vlan_prio << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
  346. /* See if a priority mapping exists.. */
  347. mp = vlan->egress_priority_map[skb_prio & 0xF];
  348. while (mp) {
  349. if (mp->priority == skb_prio) {
  350. if (mp->vlan_qos && !vlan_qos)
  351. vlan->nr_egress_mappings--;
  352. else if (!mp->vlan_qos && vlan_qos)
  353. vlan->nr_egress_mappings++;
  354. mp->vlan_qos = vlan_qos;
  355. return 0;
  356. }
  357. mp = mp->next;
  358. }
  359. /* Create a new mapping then. */
  360. mp = vlan->egress_priority_map[skb_prio & 0xF];
  361. np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
  362. if (!np)
  363. return -ENOBUFS;
  364. np->next = mp;
  365. np->priority = skb_prio;
  366. np->vlan_qos = vlan_qos;
  367. vlan->egress_priority_map[skb_prio & 0xF] = np;
  368. if (vlan_qos)
  369. vlan->nr_egress_mappings++;
  370. return 0;
  371. }
  372. /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
  373. int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
  374. {
  375. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  376. u32 old_flags = vlan->flags;
  377. if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP |
  378. VLAN_FLAG_LOOSE_BINDING))
  379. return -EINVAL;
  380. vlan->flags = (old_flags & ~mask) | (flags & mask);
  381. if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
  382. if (vlan->flags & VLAN_FLAG_GVRP)
  383. vlan_gvrp_request_join(dev);
  384. else
  385. vlan_gvrp_request_leave(dev);
  386. }
  387. return 0;
  388. }
  389. void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
  390. {
  391. strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
  392. }
  393. static int vlan_dev_open(struct net_device *dev)
  394. {
  395. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  396. struct net_device *real_dev = vlan->real_dev;
  397. int err;
  398. if (!(real_dev->flags & IFF_UP) &&
  399. !(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
  400. return -ENETDOWN;
  401. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
  402. err = dev_uc_add(real_dev, dev->dev_addr);
  403. if (err < 0)
  404. goto out;
  405. }
  406. if (dev->flags & IFF_ALLMULTI) {
  407. err = dev_set_allmulti(real_dev, 1);
  408. if (err < 0)
  409. goto del_unicast;
  410. }
  411. if (dev->flags & IFF_PROMISC) {
  412. err = dev_set_promiscuity(real_dev, 1);
  413. if (err < 0)
  414. goto clear_allmulti;
  415. }
  416. memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
  417. if (vlan->flags & VLAN_FLAG_GVRP)
  418. vlan_gvrp_request_join(dev);
  419. netif_carrier_on(dev);
  420. return 0;
  421. clear_allmulti:
  422. if (dev->flags & IFF_ALLMULTI)
  423. dev_set_allmulti(real_dev, -1);
  424. del_unicast:
  425. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  426. dev_uc_del(real_dev, dev->dev_addr);
  427. out:
  428. netif_carrier_off(dev);
  429. return err;
  430. }
  431. static int vlan_dev_stop(struct net_device *dev)
  432. {
  433. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  434. struct net_device *real_dev = vlan->real_dev;
  435. if (vlan->flags & VLAN_FLAG_GVRP)
  436. vlan_gvrp_request_leave(dev);
  437. dev_mc_unsync(real_dev, dev);
  438. dev_uc_unsync(real_dev, dev);
  439. if (dev->flags & IFF_ALLMULTI)
  440. dev_set_allmulti(real_dev, -1);
  441. if (dev->flags & IFF_PROMISC)
  442. dev_set_promiscuity(real_dev, -1);
  443. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  444. dev_uc_del(real_dev, dev->dev_addr);
  445. netif_carrier_off(dev);
  446. return 0;
  447. }
  448. static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
  449. {
  450. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  451. struct sockaddr *addr = p;
  452. int err;
  453. if (!is_valid_ether_addr(addr->sa_data))
  454. return -EADDRNOTAVAIL;
  455. if (!(dev->flags & IFF_UP))
  456. goto out;
  457. if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
  458. err = dev_uc_add(real_dev, addr->sa_data);
  459. if (err < 0)
  460. return err;
  461. }
  462. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  463. dev_uc_del(real_dev, dev->dev_addr);
  464. out:
  465. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  466. return 0;
  467. }
  468. static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  469. {
  470. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  471. const struct net_device_ops *ops = real_dev->netdev_ops;
  472. struct ifreq ifrr;
  473. int err = -EOPNOTSUPP;
  474. strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
  475. ifrr.ifr_ifru = ifr->ifr_ifru;
  476. switch (cmd) {
  477. case SIOCGMIIPHY:
  478. case SIOCGMIIREG:
  479. case SIOCSMIIREG:
  480. if (netif_device_present(real_dev) && ops->ndo_do_ioctl)
  481. err = ops->ndo_do_ioctl(real_dev, &ifrr, cmd);
  482. break;
  483. }
  484. if (!err)
  485. ifr->ifr_ifru = ifrr.ifr_ifru;
  486. return err;
  487. }
  488. static int vlan_dev_neigh_setup(struct net_device *dev, struct neigh_parms *pa)
  489. {
  490. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  491. const struct net_device_ops *ops = real_dev->netdev_ops;
  492. int err = 0;
  493. if (netif_device_present(real_dev) && ops->ndo_neigh_setup)
  494. err = ops->ndo_neigh_setup(real_dev, pa);
  495. return err;
  496. }
  497. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  498. static int vlan_dev_fcoe_ddp_setup(struct net_device *dev, u16 xid,
  499. struct scatterlist *sgl, unsigned int sgc)
  500. {
  501. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  502. const struct net_device_ops *ops = real_dev->netdev_ops;
  503. int rc = 0;
  504. if (ops->ndo_fcoe_ddp_setup)
  505. rc = ops->ndo_fcoe_ddp_setup(real_dev, xid, sgl, sgc);
  506. return rc;
  507. }
  508. static int vlan_dev_fcoe_ddp_done(struct net_device *dev, u16 xid)
  509. {
  510. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  511. const struct net_device_ops *ops = real_dev->netdev_ops;
  512. int len = 0;
  513. if (ops->ndo_fcoe_ddp_done)
  514. len = ops->ndo_fcoe_ddp_done(real_dev, xid);
  515. return len;
  516. }
  517. static int vlan_dev_fcoe_enable(struct net_device *dev)
  518. {
  519. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  520. const struct net_device_ops *ops = real_dev->netdev_ops;
  521. int rc = -EINVAL;
  522. if (ops->ndo_fcoe_enable)
  523. rc = ops->ndo_fcoe_enable(real_dev);
  524. return rc;
  525. }
  526. static int vlan_dev_fcoe_disable(struct net_device *dev)
  527. {
  528. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  529. const struct net_device_ops *ops = real_dev->netdev_ops;
  530. int rc = -EINVAL;
  531. if (ops->ndo_fcoe_disable)
  532. rc = ops->ndo_fcoe_disable(real_dev);
  533. return rc;
  534. }
  535. static int vlan_dev_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
  536. {
  537. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  538. const struct net_device_ops *ops = real_dev->netdev_ops;
  539. int rc = -EINVAL;
  540. if (ops->ndo_fcoe_get_wwn)
  541. rc = ops->ndo_fcoe_get_wwn(real_dev, wwn, type);
  542. return rc;
  543. }
  544. #endif
  545. static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
  546. {
  547. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  548. if (change & IFF_ALLMULTI)
  549. dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  550. if (change & IFF_PROMISC)
  551. dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
  552. }
  553. static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
  554. {
  555. dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
  556. dev_uc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
  557. }
  558. /*
  559. * vlan network devices have devices nesting below it, and are a special
  560. * "super class" of normal network devices; split their locks off into a
  561. * separate class since they always nest.
  562. */
  563. static struct lock_class_key vlan_netdev_xmit_lock_key;
  564. static struct lock_class_key vlan_netdev_addr_lock_key;
  565. static void vlan_dev_set_lockdep_one(struct net_device *dev,
  566. struct netdev_queue *txq,
  567. void *_subclass)
  568. {
  569. lockdep_set_class_and_subclass(&txq->_xmit_lock,
  570. &vlan_netdev_xmit_lock_key,
  571. *(int *)_subclass);
  572. }
  573. static void vlan_dev_set_lockdep_class(struct net_device *dev, int subclass)
  574. {
  575. lockdep_set_class_and_subclass(&dev->addr_list_lock,
  576. &vlan_netdev_addr_lock_key,
  577. subclass);
  578. netdev_for_each_tx_queue(dev, vlan_dev_set_lockdep_one, &subclass);
  579. }
  580. static const struct header_ops vlan_header_ops = {
  581. .create = vlan_dev_hard_header,
  582. .rebuild = vlan_dev_rebuild_header,
  583. .parse = eth_header_parse,
  584. };
  585. static const struct net_device_ops vlan_netdev_ops, vlan_netdev_accel_ops,
  586. vlan_netdev_ops_sq, vlan_netdev_accel_ops_sq;
  587. static int vlan_dev_init(struct net_device *dev)
  588. {
  589. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  590. int subclass = 0;
  591. netif_carrier_off(dev);
  592. /* IFF_BROADCAST|IFF_MULTICAST; ??? */
  593. dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
  594. IFF_MASTER | IFF_SLAVE);
  595. dev->iflink = real_dev->ifindex;
  596. dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
  597. (1<<__LINK_STATE_DORMANT))) |
  598. (1<<__LINK_STATE_PRESENT);
  599. dev->features |= real_dev->features & real_dev->vlan_features;
  600. dev->gso_max_size = real_dev->gso_max_size;
  601. /* ipv6 shared card related stuff */
  602. dev->dev_id = real_dev->dev_id;
  603. if (is_zero_ether_addr(dev->dev_addr))
  604. memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
  605. if (is_zero_ether_addr(dev->broadcast))
  606. memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
  607. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  608. dev->fcoe_ddp_xid = real_dev->fcoe_ddp_xid;
  609. #endif
  610. if (real_dev->features & NETIF_F_HW_VLAN_TX) {
  611. dev->header_ops = real_dev->header_ops;
  612. dev->hard_header_len = real_dev->hard_header_len;
  613. if (real_dev->netdev_ops->ndo_select_queue)
  614. dev->netdev_ops = &vlan_netdev_accel_ops_sq;
  615. else
  616. dev->netdev_ops = &vlan_netdev_accel_ops;
  617. } else {
  618. dev->header_ops = &vlan_header_ops;
  619. dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
  620. if (real_dev->netdev_ops->ndo_select_queue)
  621. dev->netdev_ops = &vlan_netdev_ops_sq;
  622. else
  623. dev->netdev_ops = &vlan_netdev_ops;
  624. }
  625. if (is_vlan_dev(real_dev))
  626. subclass = 1;
  627. vlan_dev_set_lockdep_class(dev, subclass);
  628. vlan_dev_info(dev)->vlan_rx_stats = alloc_percpu(struct vlan_rx_stats);
  629. if (!vlan_dev_info(dev)->vlan_rx_stats)
  630. return -ENOMEM;
  631. return 0;
  632. }
  633. static void vlan_dev_uninit(struct net_device *dev)
  634. {
  635. struct vlan_priority_tci_mapping *pm;
  636. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  637. int i;
  638. free_percpu(vlan->vlan_rx_stats);
  639. vlan->vlan_rx_stats = NULL;
  640. for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
  641. while ((pm = vlan->egress_priority_map[i]) != NULL) {
  642. vlan->egress_priority_map[i] = pm->next;
  643. kfree(pm);
  644. }
  645. }
  646. }
  647. static int vlan_ethtool_get_settings(struct net_device *dev,
  648. struct ethtool_cmd *cmd)
  649. {
  650. const struct vlan_dev_info *vlan = vlan_dev_info(dev);
  651. return dev_ethtool_get_settings(vlan->real_dev, cmd);
  652. }
  653. static void vlan_ethtool_get_drvinfo(struct net_device *dev,
  654. struct ethtool_drvinfo *info)
  655. {
  656. strcpy(info->driver, vlan_fullname);
  657. strcpy(info->version, vlan_version);
  658. strcpy(info->fw_version, "N/A");
  659. }
  660. static u32 vlan_ethtool_get_rx_csum(struct net_device *dev)
  661. {
  662. const struct vlan_dev_info *vlan = vlan_dev_info(dev);
  663. return dev_ethtool_get_rx_csum(vlan->real_dev);
  664. }
  665. static u32 vlan_ethtool_get_flags(struct net_device *dev)
  666. {
  667. const struct vlan_dev_info *vlan = vlan_dev_info(dev);
  668. return dev_ethtool_get_flags(vlan->real_dev);
  669. }
  670. static struct rtnl_link_stats64 *vlan_dev_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  671. {
  672. dev_txq_stats_fold(dev, stats);
  673. if (vlan_dev_info(dev)->vlan_rx_stats) {
  674. struct vlan_rx_stats *p, accum = {0};
  675. int i;
  676. for_each_possible_cpu(i) {
  677. u64 rxpackets, rxbytes, rxmulticast;
  678. unsigned int start;
  679. p = per_cpu_ptr(vlan_dev_info(dev)->vlan_rx_stats, i);
  680. do {
  681. start = u64_stats_fetch_begin_bh(&p->syncp);
  682. rxpackets = p->rx_packets;
  683. rxbytes = p->rx_bytes;
  684. rxmulticast = p->rx_multicast;
  685. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  686. accum.rx_packets += rxpackets;
  687. accum.rx_bytes += rxbytes;
  688. accum.rx_multicast += rxmulticast;
  689. /* rx_errors is an ulong, not protected by syncp */
  690. accum.rx_errors += p->rx_errors;
  691. }
  692. stats->rx_packets = accum.rx_packets;
  693. stats->rx_bytes = accum.rx_bytes;
  694. stats->rx_errors = accum.rx_errors;
  695. stats->multicast = accum.rx_multicast;
  696. }
  697. return stats;
  698. }
  699. static int vlan_ethtool_set_tso(struct net_device *dev, u32 data)
  700. {
  701. if (data) {
  702. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  703. /* Underlying device must support TSO for VLAN-tagged packets
  704. * and must have TSO enabled now.
  705. */
  706. if (!(real_dev->vlan_features & NETIF_F_TSO))
  707. return -EOPNOTSUPP;
  708. if (!(real_dev->features & NETIF_F_TSO))
  709. return -EINVAL;
  710. dev->features |= NETIF_F_TSO;
  711. } else {
  712. dev->features &= ~NETIF_F_TSO;
  713. }
  714. return 0;
  715. }
  716. static const struct ethtool_ops vlan_ethtool_ops = {
  717. .get_settings = vlan_ethtool_get_settings,
  718. .get_drvinfo = vlan_ethtool_get_drvinfo,
  719. .get_link = ethtool_op_get_link,
  720. .get_rx_csum = vlan_ethtool_get_rx_csum,
  721. .get_flags = vlan_ethtool_get_flags,
  722. .set_tso = vlan_ethtool_set_tso,
  723. };
  724. static const struct net_device_ops vlan_netdev_ops = {
  725. .ndo_change_mtu = vlan_dev_change_mtu,
  726. .ndo_init = vlan_dev_init,
  727. .ndo_uninit = vlan_dev_uninit,
  728. .ndo_open = vlan_dev_open,
  729. .ndo_stop = vlan_dev_stop,
  730. .ndo_start_xmit = vlan_dev_hard_start_xmit,
  731. .ndo_validate_addr = eth_validate_addr,
  732. .ndo_set_mac_address = vlan_dev_set_mac_address,
  733. .ndo_set_rx_mode = vlan_dev_set_rx_mode,
  734. .ndo_set_multicast_list = vlan_dev_set_rx_mode,
  735. .ndo_change_rx_flags = vlan_dev_change_rx_flags,
  736. .ndo_do_ioctl = vlan_dev_ioctl,
  737. .ndo_neigh_setup = vlan_dev_neigh_setup,
  738. .ndo_get_stats64 = vlan_dev_get_stats64,
  739. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  740. .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
  741. .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done,
  742. .ndo_fcoe_enable = vlan_dev_fcoe_enable,
  743. .ndo_fcoe_disable = vlan_dev_fcoe_disable,
  744. .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn,
  745. #endif
  746. };
  747. static const struct net_device_ops vlan_netdev_accel_ops = {
  748. .ndo_change_mtu = vlan_dev_change_mtu,
  749. .ndo_init = vlan_dev_init,
  750. .ndo_uninit = vlan_dev_uninit,
  751. .ndo_open = vlan_dev_open,
  752. .ndo_stop = vlan_dev_stop,
  753. .ndo_start_xmit = vlan_dev_hwaccel_hard_start_xmit,
  754. .ndo_validate_addr = eth_validate_addr,
  755. .ndo_set_mac_address = vlan_dev_set_mac_address,
  756. .ndo_set_rx_mode = vlan_dev_set_rx_mode,
  757. .ndo_set_multicast_list = vlan_dev_set_rx_mode,
  758. .ndo_change_rx_flags = vlan_dev_change_rx_flags,
  759. .ndo_do_ioctl = vlan_dev_ioctl,
  760. .ndo_neigh_setup = vlan_dev_neigh_setup,
  761. .ndo_get_stats64 = vlan_dev_get_stats64,
  762. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  763. .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
  764. .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done,
  765. .ndo_fcoe_enable = vlan_dev_fcoe_enable,
  766. .ndo_fcoe_disable = vlan_dev_fcoe_disable,
  767. .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn,
  768. #endif
  769. };
  770. static const struct net_device_ops vlan_netdev_ops_sq = {
  771. .ndo_select_queue = vlan_dev_select_queue,
  772. .ndo_change_mtu = vlan_dev_change_mtu,
  773. .ndo_init = vlan_dev_init,
  774. .ndo_uninit = vlan_dev_uninit,
  775. .ndo_open = vlan_dev_open,
  776. .ndo_stop = vlan_dev_stop,
  777. .ndo_start_xmit = vlan_dev_hard_start_xmit,
  778. .ndo_validate_addr = eth_validate_addr,
  779. .ndo_set_mac_address = vlan_dev_set_mac_address,
  780. .ndo_set_rx_mode = vlan_dev_set_rx_mode,
  781. .ndo_set_multicast_list = vlan_dev_set_rx_mode,
  782. .ndo_change_rx_flags = vlan_dev_change_rx_flags,
  783. .ndo_do_ioctl = vlan_dev_ioctl,
  784. .ndo_neigh_setup = vlan_dev_neigh_setup,
  785. .ndo_get_stats64 = vlan_dev_get_stats64,
  786. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  787. .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
  788. .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done,
  789. .ndo_fcoe_enable = vlan_dev_fcoe_enable,
  790. .ndo_fcoe_disable = vlan_dev_fcoe_disable,
  791. .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn,
  792. #endif
  793. };
  794. static const struct net_device_ops vlan_netdev_accel_ops_sq = {
  795. .ndo_select_queue = vlan_dev_select_queue,
  796. .ndo_change_mtu = vlan_dev_change_mtu,
  797. .ndo_init = vlan_dev_init,
  798. .ndo_uninit = vlan_dev_uninit,
  799. .ndo_open = vlan_dev_open,
  800. .ndo_stop = vlan_dev_stop,
  801. .ndo_start_xmit = vlan_dev_hwaccel_hard_start_xmit,
  802. .ndo_validate_addr = eth_validate_addr,
  803. .ndo_set_mac_address = vlan_dev_set_mac_address,
  804. .ndo_set_rx_mode = vlan_dev_set_rx_mode,
  805. .ndo_set_multicast_list = vlan_dev_set_rx_mode,
  806. .ndo_change_rx_flags = vlan_dev_change_rx_flags,
  807. .ndo_do_ioctl = vlan_dev_ioctl,
  808. .ndo_neigh_setup = vlan_dev_neigh_setup,
  809. .ndo_get_stats64 = vlan_dev_get_stats64,
  810. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  811. .ndo_fcoe_ddp_setup = vlan_dev_fcoe_ddp_setup,
  812. .ndo_fcoe_ddp_done = vlan_dev_fcoe_ddp_done,
  813. .ndo_fcoe_enable = vlan_dev_fcoe_enable,
  814. .ndo_fcoe_disable = vlan_dev_fcoe_disable,
  815. .ndo_fcoe_get_wwn = vlan_dev_fcoe_get_wwn,
  816. #endif
  817. };
  818. void vlan_setup(struct net_device *dev)
  819. {
  820. ether_setup(dev);
  821. dev->priv_flags |= IFF_802_1Q_VLAN;
  822. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  823. dev->tx_queue_len = 0;
  824. dev->netdev_ops = &vlan_netdev_ops;
  825. dev->destructor = free_netdev;
  826. dev->ethtool_ops = &vlan_ethtool_ops;
  827. memset(dev->broadcast, 0, ETH_ALEN);
  828. }