vlan_dev.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. 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 & 1) {
  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.raw += 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 = (struct vlan_hdr *)(skb->data);
  110. unsigned short vid;
  111. struct net_device_stats *stats;
  112. unsigned short vlan_TCI;
  113. __be16 proto;
  114. /* vlan_TCI = ntohs(get_unaligned(&vhdr->h_vlan_TCI)); */
  115. vlan_TCI = ntohs(vhdr->h_vlan_TCI);
  116. vid = (vlan_TCI & VLAN_VID_MASK);
  117. #ifdef VLAN_DEBUG
  118. printk(VLAN_DBG "%s: skb: %p vlan_id: %hx\n",
  119. __FUNCTION__, skb, vid);
  120. #endif
  121. /* Ok, we will find the correct VLAN device, strip the header,
  122. * and then go on as usual.
  123. */
  124. /* We have 12 bits of vlan ID.
  125. *
  126. * We must not drop allow preempt until we hold a
  127. * reference to the device (netif_rx does that) or we
  128. * fail.
  129. */
  130. rcu_read_lock();
  131. skb->dev = __find_vlan_dev(dev, vid);
  132. if (!skb->dev) {
  133. rcu_read_unlock();
  134. #ifdef VLAN_DEBUG
  135. printk(VLAN_DBG "%s: ERROR: No net_device for VID: %i on dev: %s [%i]\n",
  136. __FUNCTION__, (unsigned int)(vid), dev->name, dev->ifindex);
  137. #endif
  138. kfree_skb(skb);
  139. return -1;
  140. }
  141. skb->dev->last_rx = jiffies;
  142. /* Bump the rx counters for the VLAN device. */
  143. stats = vlan_dev_get_stats(skb->dev);
  144. stats->rx_packets++;
  145. stats->rx_bytes += skb->len;
  146. /* Take off the VLAN header (4 bytes currently) */
  147. skb_pull_rcsum(skb, VLAN_HLEN);
  148. /* Ok, lets check to make sure the device (dev) we
  149. * came in on is what this VLAN is attached to.
  150. */
  151. if (dev != VLAN_DEV_INFO(skb->dev)->real_dev) {
  152. rcu_read_unlock();
  153. #ifdef VLAN_DEBUG
  154. printk(VLAN_DBG "%s: dropping skb: %p because came in on wrong device, dev: %s real_dev: %s, skb_dev: %s\n",
  155. __FUNCTION__, skb, dev->name,
  156. VLAN_DEV_INFO(skb->dev)->real_dev->name,
  157. skb->dev->name);
  158. #endif
  159. kfree_skb(skb);
  160. stats->rx_errors++;
  161. return -1;
  162. }
  163. /*
  164. * Deal with ingress priority mapping.
  165. */
  166. skb->priority = vlan_get_ingress_priority(skb->dev, ntohs(vhdr->h_vlan_TCI));
  167. #ifdef VLAN_DEBUG
  168. printk(VLAN_DBG "%s: priority: %lu for TCI: %hu (hbo)\n",
  169. __FUNCTION__, (unsigned long)(skb->priority),
  170. ntohs(vhdr->h_vlan_TCI));
  171. #endif
  172. /* The ethernet driver already did the pkt_type calculations
  173. * for us...
  174. */
  175. switch (skb->pkt_type) {
  176. case PACKET_BROADCAST: /* Yeah, stats collect these together.. */
  177. // stats->broadcast ++; // no such counter :-(
  178. break;
  179. case PACKET_MULTICAST:
  180. stats->multicast++;
  181. break;
  182. case PACKET_OTHERHOST:
  183. /* Our lower layer thinks this is not local, let's make sure.
  184. * This allows the VLAN to have a different MAC than the underlying
  185. * device, and still route correctly.
  186. */
  187. if (!compare_ether_addr(eth_hdr(skb)->h_dest, skb->dev->dev_addr)) {
  188. /* It is for our (changed) MAC-address! */
  189. skb->pkt_type = PACKET_HOST;
  190. }
  191. break;
  192. default:
  193. break;
  194. };
  195. /* Was a VLAN packet, grab the encapsulated protocol, which the layer
  196. * three protocols care about.
  197. */
  198. /* proto = get_unaligned(&vhdr->h_vlan_encapsulated_proto); */
  199. proto = vhdr->h_vlan_encapsulated_proto;
  200. skb->protocol = proto;
  201. if (ntohs(proto) >= 1536) {
  202. /* place it back on the queue to be handled by
  203. * true layer 3 protocols.
  204. */
  205. /* See if we are configured to re-write the VLAN header
  206. * to make it look like ethernet...
  207. */
  208. skb = vlan_check_reorder_header(skb);
  209. /* Can be null if skb-clone fails when re-ordering */
  210. if (skb) {
  211. netif_rx(skb);
  212. } else {
  213. /* TODO: Add a more specific counter here. */
  214. stats->rx_errors++;
  215. }
  216. rcu_read_unlock();
  217. return 0;
  218. }
  219. rawp = skb->data;
  220. /*
  221. * This is a magic hack to spot IPX packets. Older Novell breaks
  222. * the protocol design and runs IPX over 802.3 without an 802.2 LLC
  223. * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
  224. * won't work for fault tolerant netware but does for the rest.
  225. */
  226. if (*(unsigned short *)rawp == 0xFFFF) {
  227. skb->protocol = __constant_htons(ETH_P_802_3);
  228. /* place it back on the queue to be handled by true layer 3 protocols.
  229. */
  230. /* See if we are configured to re-write the VLAN header
  231. * to make it look like ethernet...
  232. */
  233. skb = vlan_check_reorder_header(skb);
  234. /* Can be null if skb-clone fails when re-ordering */
  235. if (skb) {
  236. netif_rx(skb);
  237. } else {
  238. /* TODO: Add a more specific counter here. */
  239. stats->rx_errors++;
  240. }
  241. rcu_read_unlock();
  242. return 0;
  243. }
  244. /*
  245. * Real 802.2 LLC
  246. */
  247. skb->protocol = __constant_htons(ETH_P_802_2);
  248. /* place it back on the queue to be handled by upper layer protocols.
  249. */
  250. /* See if we are configured to re-write the VLAN header
  251. * to make it look like ethernet...
  252. */
  253. skb = vlan_check_reorder_header(skb);
  254. /* Can be null if skb-clone fails when re-ordering */
  255. if (skb) {
  256. netif_rx(skb);
  257. } else {
  258. /* TODO: Add a more specific counter here. */
  259. stats->rx_errors++;
  260. }
  261. rcu_read_unlock();
  262. return 0;
  263. }
  264. static inline unsigned short vlan_dev_get_egress_qos_mask(struct net_device* dev,
  265. struct sk_buff* skb)
  266. {
  267. struct vlan_priority_tci_mapping *mp =
  268. VLAN_DEV_INFO(dev)->egress_priority_map[(skb->priority & 0xF)];
  269. while (mp) {
  270. if (mp->priority == skb->priority) {
  271. return mp->vlan_qos; /* This should already be shifted to mask
  272. * correctly with the VLAN's TCI
  273. */
  274. }
  275. mp = mp->next;
  276. }
  277. return 0;
  278. }
  279. /*
  280. * Create the VLAN header for an arbitrary protocol layer
  281. *
  282. * saddr=NULL means use device source address
  283. * daddr=NULL means leave destination address (eg unresolved arp)
  284. *
  285. * This is called when the SKB is moving down the stack towards the
  286. * physical devices.
  287. */
  288. int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
  289. unsigned short type, void *daddr, void *saddr,
  290. unsigned len)
  291. {
  292. struct vlan_hdr *vhdr;
  293. unsigned short veth_TCI = 0;
  294. int rc = 0;
  295. int build_vlan_header = 0;
  296. struct net_device *vdev = dev; /* save this for the bottom of the method */
  297. #ifdef VLAN_DEBUG
  298. printk(VLAN_DBG "%s: skb: %p type: %hx len: %x vlan_id: %hx, daddr: %p\n",
  299. __FUNCTION__, skb, type, len, VLAN_DEV_INFO(dev)->vlan_id, daddr);
  300. #endif
  301. /* build vlan header only if re_order_header flag is NOT set. This
  302. * fixes some programs that get confused when they see a VLAN device
  303. * sending a frame that is VLAN encoded (the consensus is that the VLAN
  304. * device should look completely like an Ethernet device when the
  305. * REORDER_HEADER flag is set) The drawback to this is some extra
  306. * header shuffling in the hard_start_xmit. Users can turn off this
  307. * REORDER behaviour with the vconfig tool.
  308. */
  309. build_vlan_header = ((VLAN_DEV_INFO(dev)->flags & 1) == 0);
  310. if (build_vlan_header) {
  311. vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
  312. /* build the four bytes that make this a VLAN header. */
  313. /* Now, construct the second two bytes. This field looks something
  314. * like:
  315. * usr_priority: 3 bits (high bits)
  316. * CFI 1 bit
  317. * VLAN ID 12 bits (low bits)
  318. *
  319. */
  320. veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
  321. veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
  322. vhdr->h_vlan_TCI = htons(veth_TCI);
  323. /*
  324. * Set the protocol type.
  325. * For a packet of type ETH_P_802_3 we put the length in here instead.
  326. * It is up to the 802.2 layer to carry protocol information.
  327. */
  328. if (type != ETH_P_802_3) {
  329. vhdr->h_vlan_encapsulated_proto = htons(type);
  330. } else {
  331. vhdr->h_vlan_encapsulated_proto = htons(len);
  332. }
  333. skb->protocol = htons(ETH_P_8021Q);
  334. skb->nh.raw = skb->data;
  335. }
  336. /* Before delegating work to the lower layer, enter our MAC-address */
  337. if (saddr == NULL)
  338. saddr = dev->dev_addr;
  339. dev = VLAN_DEV_INFO(dev)->real_dev;
  340. /* MPLS can send us skbuffs w/out enough space. This check will grow the
  341. * skb if it doesn't have enough headroom. Not a beautiful solution, so
  342. * I'll tick a counter so that users can know it's happening... If they
  343. * care...
  344. */
  345. /* NOTE: This may still break if the underlying device is not the final
  346. * device (and thus there are more headers to add...) It should work for
  347. * good-ole-ethernet though.
  348. */
  349. if (skb_headroom(skb) < dev->hard_header_len) {
  350. struct sk_buff *sk_tmp = skb;
  351. skb = skb_realloc_headroom(sk_tmp, dev->hard_header_len);
  352. kfree_skb(sk_tmp);
  353. if (skb == NULL) {
  354. struct net_device_stats *stats = vlan_dev_get_stats(vdev);
  355. stats->tx_dropped++;
  356. return -ENOMEM;
  357. }
  358. VLAN_DEV_INFO(vdev)->cnt_inc_headroom_on_tx++;
  359. #ifdef VLAN_DEBUG
  360. printk(VLAN_DBG "%s: %s: had to grow skb.\n", __FUNCTION__, vdev->name);
  361. #endif
  362. }
  363. if (build_vlan_header) {
  364. /* Now make the underlying real hard header */
  365. rc = dev->hard_header(skb, dev, ETH_P_8021Q, daddr, saddr, len + VLAN_HLEN);
  366. if (rc > 0) {
  367. rc += VLAN_HLEN;
  368. } else if (rc < 0) {
  369. rc -= VLAN_HLEN;
  370. }
  371. } else {
  372. /* If here, then we'll just make a normal looking ethernet frame,
  373. * but, the hard_start_xmit method will insert the tag (it has to
  374. * be able to do this for bridged and other skbs that don't come
  375. * down the protocol stack in an orderly manner.
  376. */
  377. rc = dev->hard_header(skb, dev, type, daddr, saddr, len);
  378. }
  379. return rc;
  380. }
  381. int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  382. {
  383. struct net_device_stats *stats = vlan_dev_get_stats(dev);
  384. struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
  385. /* Handle non-VLAN frames if they are sent to us, for example by DHCP.
  386. *
  387. * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING
  388. * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs...
  389. */
  390. if (veth->h_vlan_proto != __constant_htons(ETH_P_8021Q)) {
  391. int orig_headroom = skb_headroom(skb);
  392. unsigned short veth_TCI;
  393. /* This is not a VLAN frame...but we can fix that! */
  394. VLAN_DEV_INFO(dev)->cnt_encap_on_xmit++;
  395. #ifdef VLAN_DEBUG
  396. printk(VLAN_DBG "%s: proto to encap: 0x%hx (hbo)\n",
  397. __FUNCTION__, htons(veth->h_vlan_proto));
  398. #endif
  399. /* Construct the second two bytes. This field looks something
  400. * like:
  401. * usr_priority: 3 bits (high bits)
  402. * CFI 1 bit
  403. * VLAN ID 12 bits (low bits)
  404. */
  405. veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
  406. veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
  407. skb = __vlan_put_tag(skb, veth_TCI);
  408. if (!skb) {
  409. stats->tx_dropped++;
  410. return 0;
  411. }
  412. if (orig_headroom < VLAN_HLEN) {
  413. VLAN_DEV_INFO(dev)->cnt_inc_headroom_on_tx++;
  414. }
  415. }
  416. #ifdef VLAN_DEBUG
  417. printk(VLAN_DBG "%s: about to send skb: %p to dev: %s\n",
  418. __FUNCTION__, skb, skb->dev->name);
  419. printk(VLAN_DBG " %2hx.%2hx.%2hx.%2xh.%2hx.%2hx %2hx.%2hx.%2hx.%2hx.%2hx.%2hx %4hx %4hx %4hx\n",
  420. veth->h_dest[0], veth->h_dest[1], veth->h_dest[2], veth->h_dest[3], veth->h_dest[4], veth->h_dest[5],
  421. veth->h_source[0], veth->h_source[1], veth->h_source[2], veth->h_source[3], veth->h_source[4], veth->h_source[5],
  422. veth->h_vlan_proto, veth->h_vlan_TCI, veth->h_vlan_encapsulated_proto);
  423. #endif
  424. stats->tx_packets++; /* for statics only */
  425. stats->tx_bytes += skb->len;
  426. skb->dev = VLAN_DEV_INFO(dev)->real_dev;
  427. dev_queue_xmit(skb);
  428. return 0;
  429. }
  430. int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  431. {
  432. struct net_device_stats *stats = vlan_dev_get_stats(dev);
  433. unsigned short veth_TCI;
  434. /* Construct the second two bytes. This field looks something
  435. * like:
  436. * usr_priority: 3 bits (high bits)
  437. * CFI 1 bit
  438. * VLAN ID 12 bits (low bits)
  439. */
  440. veth_TCI = VLAN_DEV_INFO(dev)->vlan_id;
  441. veth_TCI |= vlan_dev_get_egress_qos_mask(dev, skb);
  442. skb = __vlan_hwaccel_put_tag(skb, veth_TCI);
  443. stats->tx_packets++;
  444. stats->tx_bytes += skb->len;
  445. skb->dev = VLAN_DEV_INFO(dev)->real_dev;
  446. dev_queue_xmit(skb);
  447. return 0;
  448. }
  449. int vlan_dev_change_mtu(struct net_device *dev, int new_mtu)
  450. {
  451. /* TODO: gotta make sure the underlying layer can handle it,
  452. * maybe an IFF_VLAN_CAPABLE flag for devices?
  453. */
  454. if (VLAN_DEV_INFO(dev)->real_dev->mtu < new_mtu)
  455. return -ERANGE;
  456. dev->mtu = new_mtu;
  457. return 0;
  458. }
  459. int vlan_dev_set_ingress_priority(char *dev_name, __u32 skb_prio, short vlan_prio)
  460. {
  461. struct net_device *dev = dev_get_by_name(dev_name);
  462. if (dev) {
  463. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  464. /* see if a priority mapping exists.. */
  465. VLAN_DEV_INFO(dev)->ingress_priority_map[vlan_prio & 0x7] = skb_prio;
  466. dev_put(dev);
  467. return 0;
  468. }
  469. dev_put(dev);
  470. }
  471. return -EINVAL;
  472. }
  473. int vlan_dev_set_egress_priority(char *dev_name, __u32 skb_prio, short vlan_prio)
  474. {
  475. struct net_device *dev = dev_get_by_name(dev_name);
  476. struct vlan_priority_tci_mapping *mp = NULL;
  477. struct vlan_priority_tci_mapping *np;
  478. if (dev) {
  479. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  480. /* See if a priority mapping exists.. */
  481. mp = VLAN_DEV_INFO(dev)->egress_priority_map[skb_prio & 0xF];
  482. while (mp) {
  483. if (mp->priority == skb_prio) {
  484. mp->vlan_qos = ((vlan_prio << 13) & 0xE000);
  485. dev_put(dev);
  486. return 0;
  487. }
  488. mp = mp->next;
  489. }
  490. /* Create a new mapping then. */
  491. mp = VLAN_DEV_INFO(dev)->egress_priority_map[skb_prio & 0xF];
  492. np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL);
  493. if (np) {
  494. np->next = mp;
  495. np->priority = skb_prio;
  496. np->vlan_qos = ((vlan_prio << 13) & 0xE000);
  497. VLAN_DEV_INFO(dev)->egress_priority_map[skb_prio & 0xF] = np;
  498. dev_put(dev);
  499. return 0;
  500. } else {
  501. dev_put(dev);
  502. return -ENOBUFS;
  503. }
  504. }
  505. dev_put(dev);
  506. }
  507. return -EINVAL;
  508. }
  509. /* Flags are defined in the vlan_dev_info class in include/linux/if_vlan.h file. */
  510. int vlan_dev_set_vlan_flag(char *dev_name, __u32 flag, short flag_val)
  511. {
  512. struct net_device *dev = dev_get_by_name(dev_name);
  513. if (dev) {
  514. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  515. /* verify flag is supported */
  516. if (flag == 1) {
  517. if (flag_val) {
  518. VLAN_DEV_INFO(dev)->flags |= 1;
  519. } else {
  520. VLAN_DEV_INFO(dev)->flags &= ~1;
  521. }
  522. dev_put(dev);
  523. return 0;
  524. } else {
  525. printk(KERN_ERR "%s: flag %i is not valid.\n",
  526. __FUNCTION__, (int)(flag));
  527. dev_put(dev);
  528. return -EINVAL;
  529. }
  530. } else {
  531. printk(KERN_ERR
  532. "%s: %s is not a vlan device, priv_flags: %hX.\n",
  533. __FUNCTION__, dev->name, dev->priv_flags);
  534. dev_put(dev);
  535. }
  536. } else {
  537. printk(KERN_ERR "%s: Could not find device: %s\n",
  538. __FUNCTION__, dev_name);
  539. }
  540. return -EINVAL;
  541. }
  542. int vlan_dev_get_realdev_name(const char *dev_name, char* result)
  543. {
  544. struct net_device *dev = dev_get_by_name(dev_name);
  545. int rv = 0;
  546. if (dev) {
  547. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  548. strncpy(result, VLAN_DEV_INFO(dev)->real_dev->name, 23);
  549. rv = 0;
  550. } else {
  551. rv = -EINVAL;
  552. }
  553. dev_put(dev);
  554. } else {
  555. rv = -ENODEV;
  556. }
  557. return rv;
  558. }
  559. int vlan_dev_get_vid(const char *dev_name, unsigned short* result)
  560. {
  561. struct net_device *dev = dev_get_by_name(dev_name);
  562. int rv = 0;
  563. if (dev) {
  564. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  565. *result = VLAN_DEV_INFO(dev)->vlan_id;
  566. rv = 0;
  567. } else {
  568. rv = -EINVAL;
  569. }
  570. dev_put(dev);
  571. } else {
  572. rv = -ENODEV;
  573. }
  574. return rv;
  575. }
  576. int vlan_dev_set_mac_address(struct net_device *dev, void *addr_struct_p)
  577. {
  578. struct sockaddr *addr = (struct sockaddr *)(addr_struct_p);
  579. int i;
  580. if (netif_running(dev))
  581. return -EBUSY;
  582. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  583. printk("%s: Setting MAC address to ", dev->name);
  584. for (i = 0; i < 6; i++)
  585. printk(" %2.2x", dev->dev_addr[i]);
  586. printk(".\n");
  587. if (memcmp(VLAN_DEV_INFO(dev)->real_dev->dev_addr,
  588. dev->dev_addr,
  589. dev->addr_len) != 0) {
  590. if (!(VLAN_DEV_INFO(dev)->real_dev->flags & IFF_PROMISC)) {
  591. int flgs = VLAN_DEV_INFO(dev)->real_dev->flags;
  592. /* Increment our in-use promiscuity counter */
  593. dev_set_promiscuity(VLAN_DEV_INFO(dev)->real_dev, 1);
  594. /* Make PROMISC visible to the user. */
  595. flgs |= IFF_PROMISC;
  596. printk("VLAN (%s): Setting underlying device (%s) to promiscious mode.\n",
  597. dev->name, VLAN_DEV_INFO(dev)->real_dev->name);
  598. dev_change_flags(VLAN_DEV_INFO(dev)->real_dev, flgs);
  599. }
  600. } else {
  601. printk("VLAN (%s): Underlying device (%s) has same MAC, not checking promiscious mode.\n",
  602. dev->name, VLAN_DEV_INFO(dev)->real_dev->name);
  603. }
  604. return 0;
  605. }
  606. static inline int vlan_dmi_equals(struct dev_mc_list *dmi1,
  607. struct dev_mc_list *dmi2)
  608. {
  609. return ((dmi1->dmi_addrlen == dmi2->dmi_addrlen) &&
  610. (memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0));
  611. }
  612. /** dmi is a single entry into a dev_mc_list, a single node. mc_list is
  613. * an entire list, and we'll iterate through it.
  614. */
  615. static int vlan_should_add_mc(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
  616. {
  617. struct dev_mc_list *idmi;
  618. for (idmi = mc_list; idmi != NULL; ) {
  619. if (vlan_dmi_equals(dmi, idmi)) {
  620. if (dmi->dmi_users > idmi->dmi_users)
  621. return 1;
  622. else
  623. return 0;
  624. } else {
  625. idmi = idmi->next;
  626. }
  627. }
  628. return 1;
  629. }
  630. static inline void vlan_destroy_mc_list(struct dev_mc_list *mc_list)
  631. {
  632. struct dev_mc_list *dmi = mc_list;
  633. struct dev_mc_list *next;
  634. while(dmi) {
  635. next = dmi->next;
  636. kfree(dmi);
  637. dmi = next;
  638. }
  639. }
  640. static void vlan_copy_mc_list(struct dev_mc_list *mc_list, struct vlan_dev_info *vlan_info)
  641. {
  642. struct dev_mc_list *dmi, *new_dmi;
  643. vlan_destroy_mc_list(vlan_info->old_mc_list);
  644. vlan_info->old_mc_list = NULL;
  645. for (dmi = mc_list; dmi != NULL; dmi = dmi->next) {
  646. new_dmi = kmalloc(sizeof(*new_dmi), GFP_ATOMIC);
  647. if (new_dmi == NULL) {
  648. printk(KERN_ERR "vlan: cannot allocate memory. "
  649. "Multicast may not work properly from now.\n");
  650. return;
  651. }
  652. /* Copy whole structure, then make new 'next' pointer */
  653. *new_dmi = *dmi;
  654. new_dmi->next = vlan_info->old_mc_list;
  655. vlan_info->old_mc_list = new_dmi;
  656. }
  657. }
  658. static void vlan_flush_mc_list(struct net_device *dev)
  659. {
  660. struct dev_mc_list *dmi = dev->mc_list;
  661. while (dmi) {
  662. printk(KERN_DEBUG "%s: del %.2x:%.2x:%.2x:%.2x:%.2x:%.2x mcast address from vlan interface\n",
  663. dev->name,
  664. dmi->dmi_addr[0],
  665. dmi->dmi_addr[1],
  666. dmi->dmi_addr[2],
  667. dmi->dmi_addr[3],
  668. dmi->dmi_addr[4],
  669. dmi->dmi_addr[5]);
  670. dev_mc_delete(dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
  671. dmi = dev->mc_list;
  672. }
  673. /* dev->mc_list is NULL by the time we get here. */
  674. vlan_destroy_mc_list(VLAN_DEV_INFO(dev)->old_mc_list);
  675. VLAN_DEV_INFO(dev)->old_mc_list = NULL;
  676. }
  677. int vlan_dev_open(struct net_device *dev)
  678. {
  679. if (!(VLAN_DEV_INFO(dev)->real_dev->flags & IFF_UP))
  680. return -ENETDOWN;
  681. return 0;
  682. }
  683. int vlan_dev_stop(struct net_device *dev)
  684. {
  685. vlan_flush_mc_list(dev);
  686. return 0;
  687. }
  688. int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  689. {
  690. struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev;
  691. struct ifreq ifrr;
  692. int err = -EOPNOTSUPP;
  693. strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
  694. ifrr.ifr_ifru = ifr->ifr_ifru;
  695. switch(cmd) {
  696. case SIOCGMIIPHY:
  697. case SIOCGMIIREG:
  698. case SIOCSMIIREG:
  699. if (real_dev->do_ioctl && netif_device_present(real_dev))
  700. err = real_dev->do_ioctl(real_dev, &ifrr, cmd);
  701. break;
  702. case SIOCETHTOOL:
  703. err = dev_ethtool(&ifrr);
  704. }
  705. if (!err)
  706. ifr->ifr_ifru = ifrr.ifr_ifru;
  707. return err;
  708. }
  709. /** Taken from Gleb + Lennert's VLAN code, and modified... */
  710. void vlan_dev_set_multicast_list(struct net_device *vlan_dev)
  711. {
  712. struct dev_mc_list *dmi;
  713. struct net_device *real_dev;
  714. int inc;
  715. if (vlan_dev && (vlan_dev->priv_flags & IFF_802_1Q_VLAN)) {
  716. /* Then it's a real vlan device, as far as we can tell.. */
  717. real_dev = VLAN_DEV_INFO(vlan_dev)->real_dev;
  718. /* compare the current promiscuity to the last promisc we had.. */
  719. inc = vlan_dev->promiscuity - VLAN_DEV_INFO(vlan_dev)->old_promiscuity;
  720. if (inc) {
  721. printk(KERN_INFO "%s: dev_set_promiscuity(master, %d)\n",
  722. vlan_dev->name, inc);
  723. dev_set_promiscuity(real_dev, inc); /* found in dev.c */
  724. VLAN_DEV_INFO(vlan_dev)->old_promiscuity = vlan_dev->promiscuity;
  725. }
  726. inc = vlan_dev->allmulti - VLAN_DEV_INFO(vlan_dev)->old_allmulti;
  727. if (inc) {
  728. printk(KERN_INFO "%s: dev_set_allmulti(master, %d)\n",
  729. vlan_dev->name, inc);
  730. dev_set_allmulti(real_dev, inc); /* dev.c */
  731. VLAN_DEV_INFO(vlan_dev)->old_allmulti = vlan_dev->allmulti;
  732. }
  733. /* looking for addresses to add to master's list */
  734. for (dmi = vlan_dev->mc_list; dmi != NULL; dmi = dmi->next) {
  735. if (vlan_should_add_mc(dmi, VLAN_DEV_INFO(vlan_dev)->old_mc_list)) {
  736. dev_mc_add(real_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
  737. printk(KERN_DEBUG "%s: add %.2x:%.2x:%.2x:%.2x:%.2x:%.2x mcast address to master interface\n",
  738. vlan_dev->name,
  739. dmi->dmi_addr[0],
  740. dmi->dmi_addr[1],
  741. dmi->dmi_addr[2],
  742. dmi->dmi_addr[3],
  743. dmi->dmi_addr[4],
  744. dmi->dmi_addr[5]);
  745. }
  746. }
  747. /* looking for addresses to delete from master's list */
  748. for (dmi = VLAN_DEV_INFO(vlan_dev)->old_mc_list; dmi != NULL; dmi = dmi->next) {
  749. if (vlan_should_add_mc(dmi, vlan_dev->mc_list)) {
  750. /* if we think we should add it to the new list, then we should really
  751. * delete it from the real list on the underlying device.
  752. */
  753. dev_mc_delete(real_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
  754. printk(KERN_DEBUG "%s: del %.2x:%.2x:%.2x:%.2x:%.2x:%.2x mcast address from master interface\n",
  755. vlan_dev->name,
  756. dmi->dmi_addr[0],
  757. dmi->dmi_addr[1],
  758. dmi->dmi_addr[2],
  759. dmi->dmi_addr[3],
  760. dmi->dmi_addr[4],
  761. dmi->dmi_addr[5]);
  762. }
  763. }
  764. /* save multicast list */
  765. vlan_copy_mc_list(vlan_dev->mc_list, VLAN_DEV_INFO(vlan_dev));
  766. }
  767. }