vlan_dev.c 28 KB

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