vlan_dev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. if (dev->nd_net != &init_net)
  142. goto err_free;
  143. skb = skb_share_check(skb, GFP_ATOMIC);
  144. if (skb == NULL)
  145. goto err_free;
  146. if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
  147. goto err_free;
  148. vhdr = (struct vlan_hdr *)skb->data;
  149. vlan_TCI = ntohs(vhdr->h_vlan_TCI);
  150. vid = (vlan_TCI & VLAN_VID_MASK);
  151. rcu_read_lock();
  152. skb->dev = __find_vlan_dev(dev, vid);
  153. if (!skb->dev) {
  154. pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n",
  155. __FUNCTION__, (unsigned int)vid, dev->name);
  156. goto err_unlock;
  157. }
  158. skb->dev->last_rx = jiffies;
  159. stats = &skb->dev->stats;
  160. stats->rx_packets++;
  161. stats->rx_bytes += skb->len;
  162. skb_pull_rcsum(skb, VLAN_HLEN);
  163. skb->priority = vlan_get_ingress_priority(skb->dev,
  164. ntohs(vhdr->h_vlan_TCI));
  165. pr_debug("%s: priority: %u for TCI: %hu\n",
  166. __FUNCTION__, skb->priority, ntohs(vhdr->h_vlan_TCI));
  167. switch (skb->pkt_type) {
  168. case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
  169. /* stats->broadcast ++; // no such counter :-( */
  170. break;
  171. case PACKET_MULTICAST:
  172. stats->multicast++;
  173. break;
  174. case PACKET_OTHERHOST:
  175. /* Our lower layer thinks this is not local, let's make sure.
  176. * This allows the VLAN to have a different MAC than the
  177. * underlying device, and still route correctly.
  178. */
  179. if (!compare_ether_addr(eth_hdr(skb)->h_dest,
  180. skb->dev->dev_addr))
  181. skb->pkt_type = PACKET_HOST;
  182. break;
  183. default:
  184. break;
  185. }
  186. vlan_set_encap_proto(skb, vhdr);
  187. skb = vlan_check_reorder_header(skb);
  188. if (!skb) {
  189. stats->rx_errors++;
  190. goto err_unlock;
  191. }
  192. netif_rx(skb);
  193. rcu_read_unlock();
  194. return NET_RX_SUCCESS;
  195. err_unlock:
  196. rcu_read_unlock();
  197. err_free:
  198. kfree_skb(skb);
  199. return NET_RX_DROP;
  200. }
  201. static inline unsigned short
  202. vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
  203. {
  204. struct vlan_priority_tci_mapping *mp;
  205. mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
  206. while (mp) {
  207. if (mp->priority == skb->priority) {
  208. return mp->vlan_qos; /* This should already be shifted
  209. * to mask correctly with the
  210. * VLAN's TCI */
  211. }
  212. mp = mp->next;
  213. }
  214. return 0;
  215. }
  216. /*
  217. * Create the VLAN header for an arbitrary protocol layer
  218. *
  219. * saddr=NULL means use device source address
  220. * daddr=NULL means leave destination address (eg unresolved arp)
  221. *
  222. * This is called when the SKB is moving down the stack towards the
  223. * physical devices.
  224. */
  225. static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
  226. unsigned short type,
  227. const void *daddr, const void *saddr,
  228. unsigned int len)
  229. {
  230. struct vlan_hdr *vhdr;
  231. unsigned short veth_TCI = 0;
  232. int rc = 0;
  233. int build_vlan_header = 0;
  234. struct net_device *vdev = dev;
  235. pr_debug("%s: skb: %p type: %hx len: %u vlan_id: %hx, daddr: %p\n",
  236. __FUNCTION__, skb, type, len, vlan_dev_info(dev)->vlan_id,
  237. daddr);
  238. /* build vlan header only if re_order_header flag is NOT set. This
  239. * fixes some programs that get confused when they see a VLAN device
  240. * sending a frame that is VLAN encoded (the consensus is that the VLAN
  241. * device should look completely like an Ethernet device when the
  242. * REORDER_HEADER flag is set) The drawback to this is some extra
  243. * header shuffling in the hard_start_xmit. Users can turn off this
  244. * REORDER behaviour with the vconfig tool.
  245. */
  246. if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR))
  247. build_vlan_header = 1;
  248. if (build_vlan_header) {
  249. vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
  250. /* build the four bytes that make this a VLAN header. */
  251. /* Now, construct the second two bytes. This field looks
  252. * something like:
  253. * usr_priority: 3 bits (high bits)
  254. * CFI 1 bit
  255. * VLAN ID 12 bits (low bits)
  256. *
  257. */
  258. veth_TCI = vlan_dev_info(dev)->vlan_id;
  259. veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
  260. vhdr->h_vlan_TCI = htons(veth_TCI);
  261. /*
  262. * Set the protocol type. For a packet of type ETH_P_802_3 we
  263. * put the length in here instead. It is up to the 802.2
  264. * layer to carry protocol information.
  265. */
  266. if (type != ETH_P_802_3)
  267. vhdr->h_vlan_encapsulated_proto = htons(type);
  268. else
  269. vhdr->h_vlan_encapsulated_proto = htons(len);
  270. skb->protocol = htons(ETH_P_8021Q);
  271. skb_reset_network_header(skb);
  272. }
  273. /* Before delegating work to the lower layer, enter our MAC-address */
  274. if (saddr == NULL)
  275. saddr = dev->dev_addr;
  276. dev = vlan_dev_info(dev)->real_dev;
  277. /* MPLS can send us skbuffs w/out enough space. This check will grow
  278. * the skb if it doesn't have enough headroom. Not a beautiful solution,
  279. * so I'll tick a counter so that users can know it's happening...
  280. * If they care...
  281. */
  282. /* NOTE: This may still break if the underlying device is not the final
  283. * device (and thus there are more headers to add...) It should work for
  284. * good-ole-ethernet though.
  285. */
  286. if (skb_headroom(skb) < dev->hard_header_len) {
  287. struct sk_buff *sk_tmp = skb;
  288. skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
  289. kfree_skb(sk_tmp);
  290. if (skb == NULL) {
  291. struct net_device_stats *stats = &vdev->stats;
  292. stats->tx_dropped++;
  293. return -ENOMEM;
  294. }
  295. vlan_dev_info(vdev)->cnt_inc_headroom_on_tx++;
  296. pr_debug("%s: %s: had to grow skb\n", __FUNCTION__, vdev->name);
  297. }
  298. if (build_vlan_header) {
  299. /* Now make the underlying real hard header */
  300. rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
  301. len + VLAN_HLEN);
  302. if (rc > 0)
  303. rc += VLAN_HLEN;
  304. else if (rc < 0)
  305. rc -= VLAN_HLEN;
  306. } else
  307. /* If here, then we'll just make a normal looking ethernet
  308. * frame, but, the hard_start_xmit method will insert the tag
  309. * (it has to be able to do this for bridged and other skbs
  310. * that don't come down the protocol stack in an orderly manner.
  311. */
  312. rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
  313. return rc;
  314. }
  315. static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  316. {
  317. struct net_device_stats *stats = &dev->stats;
  318. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  319. /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
  320. *
  321. * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
  322. * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
  323. */
  324. if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
  325. vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
  326. int orig_headroom = skb_headroom(skb);
  327. unsigned short veth_TCI;
  328. /* This is not a VLAN frame...but we can fix that! */
  329. vlan_dev_info(dev)->cnt_encap_on_xmit++;
  330. pr_debug("%s: proto to encap: 0x%hx\n",
  331. __FUNCTION__, htons(veth->h_vlan_proto));
  332. /* Construct the second two bytes. This field looks something
  333. * like:
  334. * usr_priority: 3 bits (high bits)
  335. * CFI 1 bit
  336. * VLAN ID 12 bits (low bits)
  337. */
  338. veth_TCI = vlan_dev_info(dev)->vlan_id;
  339. veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
  340. skb = __vlan_put_tag(skb, veth_TCI);
  341. if (!skb) {
  342. stats->tx_dropped++;
  343. return 0;
  344. }
  345. if (orig_headroom < VLAN_HLEN)
  346. vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
  347. }
  348. pr_debug("%s: about to send skb: %p to dev: %s\n",
  349. __FUNCTION__, skb, skb->dev->name);
  350. pr_debug(" " MAC_FMT " " MAC_FMT " %4hx %4hx %4hx\n",
  351. veth->h_dest[0], veth->h_dest[1], veth->h_dest[2],
  352. veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
  353. veth->h_source[0], veth->h_source[1], veth->h_source[2],
  354. veth->h_source[3], veth->h_source[4], veth->h_source[5],
  355. veth->h_vlan_proto, veth->h_vlan_TCI,
  356. veth->h_vlan_encapsulated_proto);
  357. stats->tx_packets++; /* for statics only */
  358. stats->tx_bytes += skb->len;
  359. skb->dev = vlan_dev_info(dev)->real_dev;
  360. dev_queue_xmit(skb);
  361. return 0;
  362. }
  363. static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
  364. struct net_device *dev)
  365. {
  366. struct net_device_stats *stats = &dev->stats;
  367. unsigned short veth_TCI;
  368. /* Construct the second two bytes. This field looks something
  369. * like:
  370. * usr_priority: 3 bits (high bits)
  371. * CFI 1 bit
  372. * VLAN ID 12 bits (low bits)
  373. */
  374. veth_TCI = vlan_dev_info(dev)->vlan_id;
  375. veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
  376. skb = __vlan_hwaccel_put_tag(skb, veth_TCI);
  377. stats->tx_packets++;
  378. stats->tx_bytes += skb->len;
  379. skb->dev = vlan_dev_info(dev)->real_dev;
  380. dev_queue_xmit(skb);
  381. return 0;
  382. }
  383. static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
  384. {
  385. /* TODO: gotta make sure the underlying layer can handle it,
  386. * maybe an IFF_VLAN_CAPABLE flag for devices?
  387. */
  388. if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
  389. return -ERANGE;
  390. dev->mtu = new_mtu;
  391. return 0;
  392. }
  393. void vlan_dev_set_ingress_priority(const struct net_device *dev,
  394. u32 skb_prio, short vlan_prio)
  395. {
  396. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  397. if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
  398. vlan->nr_ingress_mappings--;
  399. else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
  400. vlan->nr_ingress_mappings++;
  401. vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
  402. }
  403. int vlan_dev_set_egress_priority(const struct net_device *dev,
  404. u32 skb_prio, short vlan_prio)
  405. {
  406. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  407. struct vlan_priority_tci_mapping *mp = NULL;
  408. struct vlan_priority_tci_mapping *np;
  409. u32 vlan_qos = (vlan_prio << 13) & 0xE000;
  410. /* See if a priority mapping exists.. */
  411. mp = vlan->egress_priority_map[skb_prio & 0xF];
  412. while (mp) {
  413. if (mp->priority == skb_prio) {
  414. if (mp->vlan_qos && !vlan_qos)
  415. vlan->nr_egress_mappings--;
  416. else if (!mp->vlan_qos && vlan_qos)
  417. vlan->nr_egress_mappings++;
  418. mp->vlan_qos = vlan_qos;
  419. return 0;
  420. }
  421. mp = mp->next;
  422. }
  423. /* Create a new mapping then. */
  424. mp = vlan->egress_priority_map[skb_prio & 0xF];
  425. np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
  426. if (!np)
  427. return -ENOBUFS;
  428. np->next = mp;
  429. np->priority = skb_prio;
  430. np->vlan_qos = vlan_qos;
  431. vlan->egress_priority_map[skb_prio & 0xF] = np;
  432. if (vlan_qos)
  433. vlan->nr_egress_mappings++;
  434. return 0;
  435. }
  436. /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
  437. int vlan_dev_set_vlan_flag(const struct net_device *dev,
  438. u32 flag, short flag_val)
  439. {
  440. /* verify flag is supported */
  441. if (flag == VLAN_FLAG_REORDER_HDR) {
  442. if (flag_val)
  443. vlan_dev_info(dev)->flags |= VLAN_FLAG_REORDER_HDR;
  444. else
  445. vlan_dev_info(dev)->flags &= ~VLAN_FLAG_REORDER_HDR;
  446. return 0;
  447. }
  448. return -EINVAL;
  449. }
  450. void vlan_dev_get_realdev_name(const struct net_device *dev, char *result)
  451. {
  452. strncpy(result, vlan_dev_info(dev)->real_dev->name, 23);
  453. }
  454. void vlan_dev_get_vid(const struct net_device *dev, unsigned short *result)
  455. {
  456. *result = vlan_dev_info(dev)->vlan_id;
  457. }
  458. static int vlan_dev_open(struct net_device *dev)
  459. {
  460. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  461. struct net_device *real_dev = vlan->real_dev;
  462. int err;
  463. if (!(real_dev->flags & IFF_UP))
  464. return -ENETDOWN;
  465. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) {
  466. err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN);
  467. if (err < 0)
  468. return err;
  469. }
  470. memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN);
  471. if (dev->flags & IFF_ALLMULTI)
  472. dev_set_allmulti(real_dev, 1);
  473. if (dev->flags & IFF_PROMISC)
  474. dev_set_promiscuity(real_dev, 1);
  475. return 0;
  476. }
  477. static int vlan_dev_stop(struct net_device *dev)
  478. {
  479. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  480. dev_mc_unsync(real_dev, dev);
  481. dev_unicast_unsync(real_dev, dev);
  482. if (dev->flags & IFF_ALLMULTI)
  483. dev_set_allmulti(real_dev, -1);
  484. if (dev->flags & IFF_PROMISC)
  485. dev_set_promiscuity(real_dev, -1);
  486. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  487. dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len);
  488. return 0;
  489. }
  490. static int vlan_dev_set_mac_address(struct net_device *dev, void *p)
  491. {
  492. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  493. struct sockaddr *addr = p;
  494. int err;
  495. if (!is_valid_ether_addr(addr->sa_data))
  496. return -EADDRNOTAVAIL;
  497. if (!(dev->flags & IFF_UP))
  498. goto out;
  499. if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) {
  500. err = dev_unicast_add(real_dev, addr->sa_data, ETH_ALEN);
  501. if (err < 0)
  502. return err;
  503. }
  504. if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr))
  505. dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN);
  506. out:
  507. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  508. return 0;
  509. }
  510. static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  511. {
  512. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  513. struct ifreq ifrr;
  514. int err = -EOPNOTSUPP;
  515. strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
  516. ifrr.ifr_ifru = ifr->ifr_ifru;
  517. switch (cmd) {
  518. case SIOCGMIIPHY:
  519. case SIOCGMIIREG:
  520. case SIOCSMIIREG:
  521. if (real_dev->do_ioctl && netif_device_present(real_dev))
  522. err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
  523. break;
  524. }
  525. if (!err)
  526. ifr->ifr_ifru = ifrr.ifr_ifru;
  527. return err;
  528. }
  529. static void vlan_dev_change_rx_flags(struct net_device *dev, int change)
  530. {
  531. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  532. if (change & IFF_ALLMULTI)
  533. dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  534. if (change & IFF_PROMISC)
  535. dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1);
  536. }
  537. static void vlan_dev_set_rx_mode(struct net_device *vlan_dev)
  538. {
  539. dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
  540. dev_unicast_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev);
  541. }
  542. /*
  543. * vlan network devices have devices nesting below it, and are a special
  544. * "super class" of normal network devices; split their locks off into a
  545. * separate class since they always nest.
  546. */
  547. static struct lock_class_key vlan_netdev_xmit_lock_key;
  548. static const struct header_ops vlan_header_ops = {
  549. .create = vlan_dev_hard_header,
  550. .rebuild = vlan_dev_rebuild_header,
  551. .parse = eth_header_parse,
  552. };
  553. static int vlan_dev_init(struct net_device *dev)
  554. {
  555. struct net_device *real_dev = vlan_dev_info(dev)->real_dev;
  556. int subclass = 0;
  557. /* IFF_BROADCAST|IFF_MULTICAST; ??? */
  558. dev->flags = real_dev->flags & ~IFF_UP;
  559. dev->iflink = real_dev->ifindex;
  560. dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
  561. (1<<__LINK_STATE_DORMANT))) |
  562. (1<<__LINK_STATE_PRESENT);
  563. /* ipv6 shared card related stuff */
  564. dev->dev_id = real_dev->dev_id;
  565. if (is_zero_ether_addr(dev->dev_addr))
  566. memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len);
  567. if (is_zero_ether_addr(dev->broadcast))
  568. memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
  569. if (real_dev->features & NETIF_F_HW_VLAN_TX) {
  570. dev->header_ops = real_dev->header_ops;
  571. dev->hard_header_len = real_dev->hard_header_len;
  572. dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
  573. } else {
  574. dev->header_ops = &vlan_header_ops;
  575. dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN;
  576. dev->hard_start_xmit = vlan_dev_hard_start_xmit;
  577. }
  578. if (real_dev->priv_flags & IFF_802_1Q_VLAN)
  579. subclass = 1;
  580. lockdep_set_class_and_subclass(&dev->_xmit_lock,
  581. &vlan_netdev_xmit_lock_key, subclass);
  582. return 0;
  583. }
  584. void vlan_setup(struct net_device *dev)
  585. {
  586. ether_setup(dev);
  587. dev->priv_flags |= IFF_802_1Q_VLAN;
  588. dev->tx_queue_len = 0;
  589. dev->change_mtu = vlan_dev_change_mtu;
  590. dev->init = vlan_dev_init;
  591. dev->open = vlan_dev_open;
  592. dev->stop = vlan_dev_stop;
  593. dev->set_mac_address = vlan_dev_set_mac_address;
  594. dev->set_rx_mode = vlan_dev_set_rx_mode;
  595. dev->set_multicast_list = vlan_dev_set_rx_mode;
  596. dev->change_rx_flags = vlan_dev_change_rx_flags;
  597. dev->do_ioctl = vlan_dev_ioctl;
  598. dev->destructor = free_netdev;
  599. memset(dev->broadcast, 0, ETH_ALEN);
  600. }