vlan_dev.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:24k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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