vlan_dev.c 24 KB

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