vlan_dev.c 20 KB

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