vlan_dev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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/mm.h>
  24. #include <linux/in.h>
  25. #include <linux/init.h>
  26. #include <asm/uaccess.h> /* for copy_from_user */
  27. #include <linux/skbuff.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/ethtool.h>
  31. #include <net/datalink.h>
  32. #include <net/p8022.h>
  33. #include <net/arp.h>
  34. #include "vlan.h"
  35. #include "vlanproc.h"
  36. #include <linux/if_vlan.h>
  37. #include <net/ip.h>
  38. /*
  39. * Rebuild the Ethernet MAC header. This is called after an ARP
  40. * (or in future other address resolution) has completed on this
  41. * sk_buff. We now let ARP fill in the other fields.
  42. *
  43. * This routine CANNOT use cached dst->neigh!
  44. * Really, it is used only when dst->neigh is wrong.
  45. *
  46. * TODO: This needs a checkup, I'm ignorant here. --BLG
  47. */
  48. static int vlan_dev_rebuild_header(struct sk_buff *skb)
  49. {
  50. struct net_device *dev = skb->dev;
  51. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  52. switch (veth->h_vlan_encapsulated_proto) {
  53. #ifdef CONFIG_INET
  54. case __constant_htons(ETH_P_IP):
  55. /* TODO: Confirm this will work with VLAN headers... */
  56. return arp_find(veth->h_dest, skb);
  57. #endif
  58. default:
  59. pr_debug("%s: unable to resolve type %X addresses.\n",
  60. dev->name, ntohs(veth->h_vlan_encapsulated_proto));
  61. memcpy(veth->h_source, dev->dev_addr, ETH_ALEN);
  62. break;
  63. }
  64. return 0;
  65. }
  66. static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb)
  67. {
  68. if (vlan_dev_info(skb->dev)->flags & VLAN_FLAG_REORDER_HDR) {
  69. if (skb_cow(skb, skb_headroom(skb)) < 0)
  70. skb = NULL;
  71. if (skb) {
  72. /* Lifted from Gleb's VLAN code... */
  73. memmove(skb->data - ETH_HLEN,
  74. skb->data - VLAN_ETH_HLEN, 12);
  75. skb->mac_header += VLAN_HLEN;
  76. }
  77. }
  78. return skb;
  79. }
  80. static inline void vlan_set_encap_proto(struct sk_buff *skb,
  81. struct vlan_hdr *vhdr)
  82. {
  83. __be16 proto;
  84. unsigned char *rawp;
  85. /*
  86. * Was a VLAN packet, grab the encapsulated protocol, which the layer
  87. * three protocols care about.
  88. */
  89. proto = vhdr->h_vlan_encapsulated_proto;
  90. if (ntohs(proto) >= 1536) {
  91. skb->protocol = proto;
  92. return;
  93. }
  94. rawp = skb->data;
  95. if (*(unsigned short *)rawp == 0xFFFF)
  96. /*
  97. * This is a magic hack to spot IPX packets. Older Novell
  98. * breaks the protocol design and runs IPX over 802.3 without
  99. * an 802.2 LLC layer. We look for FFFF which isn't a used
  100. * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
  101. * but does for the rest.
  102. */
  103. skb->protocol = htons(ETH_P_802_3);
  104. else
  105. /*
  106. * Real 802.2 LLC
  107. */
  108. skb->protocol = htons(ETH_P_802_2);
  109. }
  110. /*
  111. * Determine the packet's protocol ID. The rule here is that we
  112. * assume 802.3 if the type field is short enough to be a length.
  113. * This is normal practice and works for any 'now in use' protocol.
  114. *
  115. * Also, at this point we assume that we ARE dealing exclusively with
  116. * VLAN packets, or packets that should be made into VLAN packets based
  117. * on a default VLAN ID.
  118. *
  119. * NOTE: Should be similar to ethernet/eth.c.
  120. *
  121. * SANITY NOTE: This method is called when a packet is moving up the stack
  122. * towards userland. To get here, it would have already passed
  123. * through the ethernet/eth.c eth_type_trans() method.
  124. * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be
  125. * stored UNALIGNED in the memory. RISC systems don't like
  126. * such cases very much...
  127. * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be
  128. * aligned, so there doesn't need to be any of the unaligned
  129. * stuff. It has been commented out now... --Ben
  130. *
  131. */
  132. int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev,
  133. struct packet_type *ptype, struct net_device *orig_dev)
  134. {
  135. struct vlan_hdr *vhdr;
  136. struct net_device_stats *stats;
  137. u16 vlan_id;
  138. u16 vlan_tci;
  139. skb = skb_share_check(skb, GFP_ATOMIC);
  140. if (skb == NULL)
  141. goto err_free;
  142. if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
  143. goto err_free;
  144. vhdr = (struct vlan_hdr *)skb->data;
  145. vlan_tci = ntohs(vhdr->h_vlan_TCI);
  146. vlan_id = vlan_tci & VLAN_VID_MASK;
  147. rcu_read_lock();
  148. skb->dev = __find_vlan_dev(dev, vlan_id);
  149. if (!skb->dev) {
  150. pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
  151. __func__, vlan_id, dev->name);
  152. goto err_unlock;
  153. }
  154. skb->dev->last_rx = jiffies;
  155. stats = &skb->dev->stats;
  156. stats->rx_packets++;
  157. stats->rx_bytes += skb->len;
  158. skb_pull_rcsum(skb, VLAN_HLEN);
  159. skb->priority = vlan_get_ingress_priority(skb->dev, vlan_tci);
  160. pr_debug("%s: priority: %u for TCI: %hu\n",
  161. __func__, skb->priority, vlan_tci);
  162. switch (skb->pkt_type) {
  163. case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
  164. /* stats->broadcast ++; // no such counter :-( */
  165. break;
  166. case PACKET_MULTICAST:
  167. stats->multicast++;
  168. break;
  169. case PACKET_OTHERHOST:
  170. /* Our lower layer thinks this is not local, let's make sure.
  171. * This allows the VLAN to have a different MAC than the
  172. * underlying device, and still route correctly.
  173. */
  174. if (!compare_ether_addr(eth_hdr(skb)->h_dest,
  175. skb->dev->dev_addr))
  176. skb->pkt_type = PACKET_HOST;
  177. break;
  178. default:
  179. break;
  180. }
  181. vlan_set_encap_proto(skb, vhdr);
  182. skb = vlan_check_reorder_header(skb);
  183. if (!skb) {
  184. stats->rx_errors++;
  185. goto err_unlock;
  186. }
  187. netif_rx(skb);
  188. rcu_read_unlock();
  189. return NET_RX_SUCCESS;
  190. err_unlock:
  191. rcu_read_unlock();
  192. err_free:
  193. kfree_skb(skb);
  194. return NET_RX_DROP;
  195. }
  196. static inline u16
  197. vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
  198. {
  199. struct vlan_priority_tci_mapping *mp;
  200. mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
  201. while (mp) {
  202. if (mp->priority == skb->priority) {
  203. return mp->vlan_qos; /* This should already be shifted
  204. * to mask correctly with the
  205. * VLAN's TCI */
  206. }
  207. mp = mp->next;
  208. }
  209. return 0;
  210. }
  211. /*
  212. * Create the VLAN header for an arbitrary protocol layer
  213. *
  214. * saddr=NULL means use device source address
  215. * daddr=NULL means leave destination address (eg unresolved arp)
  216. *
  217. * This is called when the SKB is moving down the stack towards the
  218. * physical devices.
  219. */
  220. static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
  221. unsigned short type,
  222. const void *daddr, const void *saddr,
  223. unsigned int len)
  224. {
  225. struct vlan_hdr *vhdr;
  226. u16 vlan_tci = 0;
  227. int rc = 0;
  228. int build_vlan_header = 0;
  229. pr_debug("%s: skb: %p type: %hx len: %u vlan_id: %hx, daddr: %p\n",
  230. __func__, skb, type, len, vlan_dev_info(dev)->vlan_id,
  231. daddr);
  232. if (WARN_ON(skb_headroom(skb) < dev->hard_header_len))
  233. return -ENOSPC;
  234. /* build vlan header only if re_order_header flag is NOT set. This
  235. * fixes some programs that get confused when they see a VLAN device
  236. * sending a frame that is VLAN encoded (the consensus is that the VLAN
  237. * device should look completely like an Ethernet device when the
  238. * REORDER_HEADER flag is set) The drawback to this is some extra
  239. * header shuffling in the hard_start_xmit. Users can turn off this
  240. * REORDER behaviour with the vconfig tool.
  241. */
  242. if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR))
  243. build_vlan_header = 1;
  244. if (build_vlan_header) {
  245. vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
  246. /* build the four bytes that make this a VLAN header. */
  247. /* Now, construct the second two bytes. This field looks
  248. * something like:
  249. * usr_priority: 3 bits (high bits)
  250. * CFI 1 bit
  251. * VLAN ID 12 bits (low bits)
  252. *
  253. */
  254. vlan_tci = vlan_dev_info(dev)->vlan_id;
  255. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
  256. vhdr->h_vlan_TCI = htons(vlan_tci);
  257. /*
  258. * Set the protocol type. For a packet of type ETH_P_802_3 we
  259. * put the length in here instead. It is up to the 802.2
  260. * layer to carry protocol information.
  261. */
  262. if (type != ETH_P_802_3)
  263. vhdr->h_vlan_encapsulated_proto = htons(type);
  264. else
  265. vhdr->h_vlan_encapsulated_proto = htons(len);
  266. skb->protocol = htons(ETH_P_8021Q);
  267. }
  268. /* Before delegating work to the lower layer, enter our MAC-address */
  269. if (saddr == NULL)
  270. saddr = dev->dev_addr;
  271. dev = vlan_dev_info(dev)->real_dev;
  272. if (build_vlan_header) {
  273. /* Now make the underlying real hard header */
  274. rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
  275. len + VLAN_HLEN);
  276. if (rc > 0)
  277. rc += VLAN_HLEN;
  278. else if (rc < 0)
  279. rc -= VLAN_HLEN;
  280. } else
  281. /* If here, then we'll just make a normal looking ethernet
  282. * frame, but, the hard_start_xmit method will insert the tag
  283. * (it has to be able to do this for bridged and other skbs
  284. * that don't come down the protocol stack in an orderly manner.
  285. */
  286. rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
  287. return rc;
  288. }
  289. static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  290. {
  291. struct net_device_stats *stats = &dev->stats;
  292. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  293. /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
  294. *
  295. * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
  296. * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
  297. */
  298. if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
  299. vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
  300. int orig_headroom = skb_headroom(skb);
  301. u16 vlan_tci;
  302. /* This is not a VLAN frame...but we can fix that! */
  303. vlan_dev_info(dev)->cnt_encap_on_xmit++;
  304. pr_debug("%s: proto to encap: 0x%hx\n",
  305. __func__, ntohs(veth->h_vlan_proto));
  306. /* Construct the second two bytes. This field looks something
  307. * like:
  308. * usr_priority: 3 bits (high bits)
  309. * CFI 1 bit
  310. * VLAN ID 12 bits (low bits)
  311. */
  312. vlan_tci = vlan_dev_info(dev)->vlan_id;
  313. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
  314. skb = __vlan_put_tag(skb, vlan_tci);
  315. if (!skb) {
  316. stats->tx_dropped++;
  317. return 0;
  318. }
  319. if (orig_headroom < VLAN_HLEN)
  320. vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
  321. }
  322. pr_debug("%s: about to send skb: %p to dev: %s\n",
  323. __func__, skb, skb->dev->name);
  324. pr_debug(" " MAC_FMT " " MAC_FMT " %4hx %4hx %4hx\n",
  325. veth->h_dest[0], veth->h_dest[1], veth->h_dest[2],
  326. veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
  327. veth->h_source[0], veth->h_source[1], veth->h_source[2],
  328. veth->h_source[3], veth->h_source[4], veth->h_source[5],
  329. veth->h_vlan_proto, veth->h_vlan_TCI,
  330. veth->h_vlan_encapsulated_proto);
  331. stats->tx_packets++; /* for statics only */
  332. stats->tx_bytes += skb->len;
  333. skb->dev = vlan_dev_info(dev)->real_dev;
  334. dev_queue_xmit(skb);
  335. return 0;
  336. }
  337. static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
  338. struct net_device *dev)
  339. {
  340. struct net_device_stats *stats = &dev->stats;
  341. u16 vlan_tci;
  342. /* Construct the second two bytes. This field looks something
  343. * like:
  344. * usr_priority: 3 bits (high bits)
  345. * CFI 1 bit
  346. * VLAN ID 12 bits (low bits)
  347. */
  348. vlan_tci = vlan_dev_info(dev)->vlan_id;
  349. vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb);
  350. skb = __vlan_hwaccel_put_tag(skb, vlan_tci);
  351. stats->tx_packets++;
  352. stats->tx_bytes += skb->len;
  353. skb->dev = vlan_dev_info(dev)->real_dev;
  354. dev_queue_xmit(skb);
  355. return 0;
  356. }
  357. static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
  358. {
  359. /* TODO: gotta make sure the underlying layer can handle it,
  360. * maybe an IFF_VLAN_CAPABLE flag for devices?
  361. */
  362. if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
  363. return -ERANGE;
  364. dev->mtu = new_mtu;
  365. return 0;
  366. }
  367. void vlan_dev_set_ingress_priority(const struct net_device *dev,
  368. u32 skb_prio, u16 vlan_prio)
  369. {
  370. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  371. if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
  372. vlan->nr_ingress_mappings--;
  373. else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
  374. vlan->nr_ingress_mappings++;
  375. vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
  376. }
  377. int vlan_dev_set_egress_priority(const struct net_device *dev,
  378. u32 skb_prio, u16 vlan_prio)
  379. {
  380. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  381. struct vlan_priority_tci_mapping *mp = NULL;
  382. struct vlan_priority_tci_mapping *np;
  383. u32 vlan_qos = (vlan_prio << 13) & 0xE000;
  384. /* See if a priority mapping exists.. */
  385. mp = vlan->egress_priority_map[skb_prio & 0xF];
  386. while (mp) {
  387. if (mp->priority == skb_prio) {
  388. if (mp->vlan_qos && !vlan_qos)
  389. vlan->nr_egress_mappings--;
  390. else if (!mp->vlan_qos && vlan_qos)
  391. vlan->nr_egress_mappings++;
  392. mp->vlan_qos = vlan_qos;
  393. return 0;
  394. }
  395. mp = mp->next;
  396. }
  397. /* Create a new mapping then. */
  398. mp = vlan->egress_priority_map[skb_prio & 0xF];
  399. np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
  400. if (!np)
  401. return -ENOBUFS;
  402. np->next = mp;
  403. np->priority = skb_prio;
  404. np->vlan_qos = vlan_qos;
  405. vlan->egress_priority_map[skb_prio & 0xF] = np;
  406. if (vlan_qos)
  407. vlan->nr_egress_mappings++;
  408. return 0;
  409. }
  410. /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
  411. int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask)
  412. {
  413. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  414. u32 old_flags = vlan->flags;
  415. if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP))
  416. return -EINVAL;
  417. vlan->flags = (old_flags & ~mask) | (flags & mask);
  418. if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) {
  419. if (vlan->flags & VLAN_FLAG_GVRP)
  420. vlan_gvrp_request_join(dev);
  421. else
  422. vlan_gvrp_request_leave(dev);
  423. }
  424. return 0;
  425. }
  426. void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
  427. {
  428. strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
  429. }
  430. static int vlan_dev_open(struct net_device *dev)
  431. {
  432. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  433. struct net_device *real_dev = vlan->real_dev;
  434. int err;
  435. if (!(real_dev->flags & IFF_UP))
  436. return -ENETDOWN;
  437. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
  438. err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN);
  439. if (err < 0)
  440. return err;
  441. }
  442. memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
  443. if (dev->flags & IFF_ALLMULTI)
  444. dev_set_allmulti(real_dev, 1);
  445. if (dev->flags & IFF_PROMISC)
  446. dev_set_promiscuity(real_dev, 1);
  447. if (vlan->flags & VLAN_FLAG_GVRP)
  448. vlan_gvrp_request_join(dev);
  449. return 0;
  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_unicast_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_unicast_delete(real_dev, dev->dev_addr, dev->addr_len);
  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_unicast_add(real_dev, addr->sa_data, ETH_ALEN);
  478. if (err < 0)
  479. return err;
  480. }
  481. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  482. dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN);
  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. struct ifreq ifrr;
  491. int err = -EOPNOTSUPP;
  492. strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
  493. ifrr.ifr_ifru = ifr->ifr_ifru;
  494. switch (cmd) {
  495. case SIOCGMIIPHY:
  496. case SIOCGMIIREG:
  497. case SIOCSMIIREG:
  498. if (real_dev->do_ioctl && netif_device_present(real_dev))
  499. err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
  500. break;
  501. }
  502. if (!err)
  503. ifr->ifr_ifru = ifrr.ifr_ifru;
  504. return err;
  505. }
  506. static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
  507. {
  508. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  509. if (change & IFF_ALLMULTI)
  510. dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  511. if (change & IFF_PROMISC)
  512. dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
  513. }
  514. static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
  515. {
  516. dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
  517. dev_unicast_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
  518. }
  519. /*
  520. * vlan network devices have devices nesting below it, and are a special
  521. * "super class" of normal network devices; split their locks off into a
  522. * separate class since they always nest.
  523. */
  524. static struct lock_class_key vlan_netdev_xmit_lock_key;
  525. static void vlan_dev_set_lockdep_one(struct netdev_queue *txq,
  526. int subclass)
  527. {
  528. lockdep_set_class_and_subclass(&txq->_xmit_lock,
  529. &vlan_netdev_xmit_lock_key, subclass);
  530. }
  531. static void vlan_dev_set_lockdep_class(struct net_device *dev, int subclass)
  532. {
  533. vlan_dev_set_lockdep_one(&dev->tx_queue, subclass);
  534. }
  535. static const struct header_ops vlan_header_ops = {
  536. .create = vlan_dev_hard_header,
  537. .rebuild = vlan_dev_rebuild_header,
  538. .parse = eth_header_parse,
  539. };
  540. static int vlan_dev_init(struct net_device *dev)
  541. {
  542. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  543. int subclass = 0;
  544. /* IFF_BROADCAST|IFF_MULTICAST; ??? */
  545. dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI);
  546. dev->iflink = real_dev->ifindex;
  547. dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
  548. (1<<__LINK_STATE_DORMANT))) |
  549. (1<<__LINK_STATE_PRESENT);
  550. dev->features |= real_dev->features & real_dev->vlan_features;
  551. /* ipv6 shared card related stuff */
  552. dev->dev_id = real_dev->dev_id;
  553. if (is_zero_ether_addr(dev->dev_addr))
  554. memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
  555. if (is_zero_ether_addr(dev->broadcast))
  556. memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
  557. if (real_dev->features & NETIF_F_HW_VLAN_TX) {
  558. dev->header_ops = real_dev->header_ops;
  559. dev->hard_header_len = real_dev->hard_header_len;
  560. dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
  561. } else {
  562. dev->header_ops = &vlan_header_ops;
  563. dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
  564. dev->hard_start_xmit = vlan_dev_hard_start_xmit;
  565. }
  566. if (is_vlan_dev(real_dev))
  567. subclass = 1;
  568. vlan_dev_set_lockdep_class(dev, subclass);
  569. return 0;
  570. }
  571. static void vlan_dev_uninit(struct net_device *dev)
  572. {
  573. struct vlan_priority_tci_mapping *pm;
  574. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  575. int i;
  576. for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) {
  577. while ((pm = vlan->egress_priority_map[i]) != NULL) {
  578. vlan->egress_priority_map[i] = pm->next;
  579. kfree(pm);
  580. }
  581. }
  582. }
  583. static u32 vlan_ethtool_get_rx_csum(struct net_device *dev)
  584. {
  585. const struct vlan_dev_info *vlan = vlan_dev_info(dev);
  586. struct net_device *real_dev = vlan->real_dev;
  587. if (real_dev->ethtool_ops == NULL ||
  588. real_dev->ethtool_ops->get_rx_csum == NULL)
  589. return 0;
  590. return real_dev->ethtool_ops->get_rx_csum(real_dev);
  591. }
  592. static const struct ethtool_ops vlan_ethtool_ops = {
  593. .get_link = ethtool_op_get_link,
  594. .get_rx_csum = vlan_ethtool_get_rx_csum,
  595. };
  596. void vlan_setup(struct net_device *dev)
  597. {
  598. ether_setup(dev);
  599. dev->priv_flags |= IFF_802_1Q_VLAN;
  600. dev->tx_queue_len = 0;
  601. dev->change_mtu = vlan_dev_change_mtu;
  602. dev->init = vlan_dev_init;
  603. dev->uninit = vlan_dev_uninit;
  604. dev->open = vlan_dev_open;
  605. dev->stop = vlan_dev_stop;
  606. dev->set_mac_address = vlan_dev_set_mac_address;
  607. dev->set_rx_mode = vlan_dev_set_rx_mode;
  608. dev->set_multicast_list = vlan_dev_set_rx_mode;
  609. dev->change_rx_flags = vlan_dev_change_rx_flags;
  610. dev->do_ioctl = vlan_dev_ioctl;
  611. dev->destructor = free_netdev;
  612. dev->ethtool_ops = &vlan_ethtool_ops;
  613. memset(dev->broadcast, 0, ETH_ALEN);
  614. }