vlan_dev.c 21 KB

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