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