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 aligned,
  100. * so there doesn't need to be any of the unaligned stuff. It has
  101. * 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. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  118. return -1;
  119. if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) {
  120. kfree_skb(skb);
  121. return -1;
  122. }
  123. vhdr = (struct vlan_hdr *)(skb->data);
  124. /* vlan_TCI = ntohs(get_unaligned(&vhdr->h_vlan_TCI)); */
  125. vlan_TCI = ntohs(vhdr->h_vlan_TCI);
  126. vid = (vlan_TCI & VLAN_VID_MASK);
  127. /* Ok, we will find the correct VLAN device, strip the header,
  128. * and then go on as usual.
  129. */
  130. /* We have 12 bits of vlan ID.
  131. *
  132. * We must not drop allow preempt until we hold a
  133. * reference to the device (netif_rx does that) or we
  134. * fail.
  135. */
  136. rcu_read_lock();
  137. skb->dev = __find_vlan_dev(dev, vid);
  138. if (!skb->dev) {
  139. rcu_read_unlock();
  140. pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s [%i]\n",
  141. __FUNCTION__, (unsigned int)vid, dev->name, dev->ifindex);
  142. kfree_skb(skb);
  143. return -1;
  144. }
  145. skb->dev->last_rx = jiffies;
  146. /* Bump the rx counters for the VLAN device. */
  147. stats = &skb->dev->stats;
  148. stats->rx_packets++;
  149. stats->rx_bytes += skb->len;
  150. /* Take off the VLAN header (4 bytes currently) */
  151. skb_pull_rcsum(skb, VLAN_HLEN);
  152. /*
  153. * Deal with ingress priority mapping.
  154. */
  155. skb->priority = vlan_get_ingress_priority(skb->dev, ntohs(vhdr->h_vlan_TCI));
  156. pr_debug("%s: priority: %u for TCI: %hu\n",
  157. __FUNCTION__, skb->priority, ntohs(vhdr->h_vlan_TCI));
  158. /* The ethernet driver already did the pkt_type calculations
  159. * for us...
  160. */
  161. switch (skb->pkt_type) {
  162. case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
  163. // stats->broadcast ++; // no such counter :-(
  164. break;
  165. case PACKET_MULTICAST:
  166. stats->multicast++;
  167. break;
  168. case PACKET_OTHERHOST:
  169. /* Our lower layer thinks this is not local, let's make sure.
  170. * This allows the VLAN to have a different MAC than the underlying
  171. * device, and still route correctly.
  172. */
  173. if (!compare_ether_addr(eth_hdr(skb)->h_dest, skb->dev->dev_addr)) {
  174. /* It is for our (changed) MAC-address! */
  175. skb->pkt_type = PACKET_HOST;
  176. }
  177. break;
  178. default:
  179. break;
  180. }
  181. /* Was a VLAN packet, grab the encapsulated protocol, which the layer
  182. * three protocols care about.
  183. */
  184. /* proto = get_unaligned(&vhdr->h_vlan_encapsulated_proto); */
  185. proto = vhdr->h_vlan_encapsulated_proto;
  186. skb->protocol = proto;
  187. if (ntohs(proto) >= 1536) {
  188. /* place it back on the queue to be handled by
  189. * true layer 3 protocols.
  190. */
  191. /* See if we are configured to re-write the VLAN header
  192. * to make it look like ethernet...
  193. */
  194. skb = vlan_check_reorder_header(skb);
  195. /* Can be null if skb-clone fails when re-ordering */
  196. if (skb) {
  197. netif_rx(skb);
  198. } else {
  199. /* TODO: Add a more specific counter here. */
  200. stats->rx_errors++;
  201. }
  202. rcu_read_unlock();
  203. return 0;
  204. }
  205. rawp = skb->data;
  206. /*
  207. * This is a magic hack to spot IPX packets. Older Novell breaks
  208. * the protocol design and runs IPX over 802.3 without an 802.2 LLC
  209. * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
  210. * won't work for fault tolerant netware but does for the rest.
  211. */
  212. if (*(unsigned short *)rawp == 0xFFFF) {
  213. skb->protocol = htons(ETH_P_802_3);
  214. /* place it back on the queue to be handled by true layer 3 protocols.
  215. */
  216. /* See if we are configured to re-write the VLAN header
  217. * to make it look like ethernet...
  218. */
  219. skb = vlan_check_reorder_header(skb);
  220. /* Can be null if skb-clone fails when re-ordering */
  221. if (skb) {
  222. netif_rx(skb);
  223. } else {
  224. /* TODO: Add a more specific counter here. */
  225. stats->rx_errors++;
  226. }
  227. rcu_read_unlock();
  228. return 0;
  229. }
  230. /*
  231. * Real 802.2 LLC
  232. */
  233. skb->protocol = htons(ETH_P_802_2);
  234. /* place it back on the queue to be handled by upper layer protocols.
  235. */
  236. /* See if we are configured to re-write the VLAN header
  237. * to make it look like ethernet...
  238. */
  239. skb = vlan_check_reorder_header(skb);
  240. /* Can be null if skb-clone fails when re-ordering */
  241. if (skb) {
  242. netif_rx(skb);
  243. } else {
  244. /* TODO: Add a more specific counter here. */
  245. stats->rx_errors++;
  246. }
  247. rcu_read_unlock();
  248. return 0;
  249. }
  250. static inline unsigned short vlan_dev_get_egress_qos_mask(struct net_device* dev,
  251. struct sk_buff* skb)
  252. {
  253. struct vlan_priority_tci_mapping *mp =
  254. vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)];
  255. while (mp) {
  256. if (mp->priority == skb->priority) {
  257. return mp->vlan_qos; /* This should already be shifted to mask
  258. * correctly with the VLAN's TCI
  259. */
  260. }
  261. mp = mp->next;
  262. }
  263. return 0;
  264. }
  265. /*
  266. * Create the VLAN header for an arbitrary protocol layer
  267. *
  268. * saddr=NULL means use device source address
  269. * daddr=NULL means leave destination address (eg unresolved arp)
  270. *
  271. * This is called when the SKB is moving down the stack towards the
  272. * physical devices.
  273. */
  274. static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
  275. unsigned short type,
  276. const void *daddr, const void *saddr,
  277. unsigned int len)
  278. {
  279. struct vlan_hdr *vhdr;
  280. unsigned short veth_TCI = 0;
  281. int rc = 0;
  282. int build_vlan_header = 0;
  283. struct net_device *vdev = dev; /* save this for the bottom of the method */
  284. pr_debug("%s: skb: %p type: %hx len: %u vlan_id: %hx, daddr: %p\n",
  285. __FUNCTION__, skb, type, len, vlan_dev_info(dev)->vlan_id, daddr);
  286. /* build vlan header only if re_order_header flag is NOT set. This
  287. * fixes some programs that get confused when they see a VLAN device
  288. * sending a frame that is VLAN encoded (the consensus is that the VLAN
  289. * device should look completely like an Ethernet device when the
  290. * REORDER_HEADER flag is set) The drawback to this is some extra
  291. * header shuffling in the hard_start_xmit. Users can turn off this
  292. * REORDER behaviour with the vconfig tool.
  293. */
  294. if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR))
  295. build_vlan_header = 1;
  296. if (build_vlan_header) {
  297. vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
  298. /* build the four bytes that make this a VLAN header. */
  299. /* Now, construct the second two bytes. This field looks something
  300. * like:
  301. * usr_priority: 3 bits (high bits)
  302. * CFI 1 bit
  303. * VLAN ID 12 bits (low bits)
  304. *
  305. */
  306. veth_TCI = vlan_dev_info(dev)->vlan_id;
  307. veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
  308. vhdr->h_vlan_TCI = htons(veth_TCI);
  309. /*
  310. * Set the protocol type.
  311. * For a packet of type ETH_P_802_3 we put the length in here instead.
  312. * It is up to the 802.2 layer to carry protocol information.
  313. */
  314. if (type != ETH_P_802_3) {
  315. vhdr->h_vlan_encapsulated_proto = htons(type);
  316. } else {
  317. vhdr->h_vlan_encapsulated_proto = htons(len);
  318. }
  319. skb->protocol = htons(ETH_P_8021Q);
  320. skb_reset_network_header(skb);
  321. }
  322. /* Before delegating work to the lower layer, enter our MAC-address */
  323. if (saddr == NULL)
  324. saddr = dev->dev_addr;
  325. dev = vlan_dev_info(dev)->real_dev;
  326. /* MPLS can send us skbuffs w/out enough space. This check will grow the
  327. * skb if it doesn't have enough headroom. Not a beautiful solution, so
  328. * I'll tick a counter so that users can know it's happening... If they
  329. * care...
  330. */
  331. /* NOTE: This may still break if the underlying device is not the final
  332. * device (and thus there are more headers to add...) It should work for
  333. * good-ole-ethernet though.
  334. */
  335. if (skb_headroom(skb) < dev->hard_header_len) {
  336. struct sk_buff *sk_tmp = skb;
  337. skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
  338. kfree_skb(sk_tmp);
  339. if (skb == NULL) {
  340. struct net_device_stats *stats = &vdev->stats;
  341. stats->tx_dropped++;
  342. return -ENOMEM;
  343. }
  344. vlan_dev_info(vdev)->cnt_inc_headroom_on_tx++;
  345. pr_debug("%s: %s: had to grow skb.\n", __FUNCTION__, vdev->name);
  346. }
  347. if (build_vlan_header) {
  348. /* Now make the underlying real hard header */
  349. rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr,
  350. len + VLAN_HLEN);
  351. if (rc > 0)
  352. rc += VLAN_HLEN;
  353. else if (rc < 0)
  354. rc -= VLAN_HLEN;
  355. } else
  356. /* If here, then we'll just make a normal looking ethernet frame,
  357. * but, the hard_start_xmit method will insert the tag (it has to
  358. * be able to do this for bridged and other skbs that don't come
  359. * down the protocol stack in an orderly manner.
  360. */
  361. rc = dev_hard_header(skb, dev, type, daddr, saddr, len);
  362. return rc;
  363. }
  364. static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  365. {
  366. struct net_device_stats *stats = &dev->stats;
  367. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  368. /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
  369. *
  370. * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
  371. * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
  372. */
  373. if (veth->h_vlan_proto != htons(ETH_P_8021Q) ||
  374. vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) {
  375. int orig_headroom = skb_headroom(skb);
  376. unsigned short veth_TCI;
  377. /* This is not a VLAN frame...but we can fix that! */
  378. vlan_dev_info(dev)->cnt_encap_on_xmit++;
  379. pr_debug("%s: proto to encap: 0x%hx\n",
  380. __FUNCTION__, htons(veth->h_vlan_proto));
  381. /* Construct the second two bytes. This field looks something
  382. * like:
  383. * usr_priority: 3 bits (high bits)
  384. * CFI 1 bit
  385. * VLAN ID 12 bits (low bits)
  386. */
  387. veth_TCI = vlan_dev_info(dev)->vlan_id;
  388. veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
  389. skb = __vlan_put_tag(skb, veth_TCI);
  390. if (!skb) {
  391. stats->tx_dropped++;
  392. return 0;
  393. }
  394. if (orig_headroom < VLAN_HLEN) {
  395. vlan_dev_info(dev)->cnt_inc_headroom_on_tx++;
  396. }
  397. }
  398. pr_debug("%s: about to send skb: %p to dev: %s\n",
  399. __FUNCTION__, skb, skb->dev->name);
  400. pr_debug(" " MAC_FMT " " MAC_FMT " %4hx %4hx %4hx\n",
  401. veth->h_dest[0], veth->h_dest[1], veth->h_dest[2],
  402. veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
  403. veth->h_source[0], veth->h_source[1], veth->h_source[2],
  404. veth->h_source[3], veth->h_source[4], veth->h_source[5],
  405. veth->h_vlan_proto, veth->h_vlan_TCI,
  406. veth->h_vlan_encapsulated_proto);
  407. stats->tx_packets++; /* for statics only */
  408. stats->tx_bytes += skb->len;
  409. skb->dev = vlan_dev_info(dev)->real_dev;
  410. dev_queue_xmit(skb);
  411. return 0;
  412. }
  413. static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb,
  414. struct net_device *dev)
  415. {
  416. struct net_device_stats *stats = &dev->stats;
  417. unsigned short veth_TCI;
  418. /* Construct the second two bytes. This field looks something
  419. * like:
  420. * usr_priority: 3 bits (high bits)
  421. * CFI 1 bit
  422. * VLAN ID 12 bits (low bits)
  423. */
  424. veth_TCI = vlan_dev_info(dev)->vlan_id;
  425. veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
  426. skb = __vlan_hwaccel_put_tag(skb, veth_TCI);
  427. stats->tx_packets++;
  428. stats->tx_bytes += skb->len;
  429. skb->dev = vlan_dev_info(dev)->real_dev;
  430. dev_queue_xmit(skb);
  431. return 0;
  432. }
  433. static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
  434. {
  435. /* TODO: gotta make sure the underlying layer can handle it,
  436. * maybe an IFF_VLAN_CAPABLE flag for devices?
  437. */
  438. if (vlan_dev_info(dev)->real_dev->mtu < new_mtu)
  439. return -ERANGE;
  440. dev->mtu = new_mtu;
  441. return 0;
  442. }
  443. void vlan_dev_set_ingress_priority(const struct net_device *dev,
  444. u32 skb_prio, short vlan_prio)
  445. {
  446. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  447. if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio)
  448. vlan->nr_ingress_mappings--;
  449. else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio)
  450. vlan->nr_ingress_mappings++;
  451. vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
  452. }
  453. int vlan_dev_set_egress_priority(const struct net_device *dev,
  454. u32 skb_prio, short vlan_prio)
  455. {
  456. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  457. struct vlan_priority_tci_mapping *mp = NULL;
  458. struct vlan_priority_tci_mapping *np;
  459. u32 vlan_qos = (vlan_prio << 13) & 0xE000;
  460. /* See if a priority mapping exists.. */
  461. mp = vlan->egress_priority_map[skb_prio & 0xF];
  462. while (mp) {
  463. if (mp->priority == skb_prio) {
  464. if (mp->vlan_qos && !vlan_qos)
  465. vlan->nr_egress_mappings--;
  466. else if (!mp->vlan_qos && vlan_qos)
  467. vlan->nr_egress_mappings++;
  468. mp->vlan_qos = vlan_qos;
  469. return 0;
  470. }
  471. mp = mp->next;
  472. }
  473. /* Create a new mapping then. */
  474. mp = vlan->egress_priority_map[skb_prio & 0xF];
  475. np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
  476. if (!np)
  477. return -ENOBUFS;
  478. np->next = mp;
  479. np->priority = skb_prio;
  480. np->vlan_qos = vlan_qos;
  481. vlan->egress_priority_map[skb_prio & 0xF] = np;
  482. if (vlan_qos)
  483. vlan->nr_egress_mappings++;
  484. return 0;
  485. }
  486. /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */
  487. int vlan_dev_set_vlan_flag(const struct net_device *dev,
  488. u32 flag, short flag_val)
  489. {
  490. /* verify flag is supported */
  491. if (flag == VLAN_FLAG_REORDER_HDR) {
  492. if (flag_val) {
  493. vlan_dev_info(dev)->flags |= VLAN_FLAG_REORDER_HDR;
  494. } else {
  495. vlan_dev_info(dev)->flags &= ~VLAN_FLAG_REORDER_HDR;
  496. }
  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. }