vlan_dev.c 28 KB

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