vlan_dev.c 26 KB

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