vlan_dev.c 22 KB

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