]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* -*- linux-c -*- |
2 | * INET 802.1Q VLAN | |
3 | * Ethernet-type device handling. | |
4 | * | |
5 | * Authors: Ben Greear <greearb@candelatech.com> | |
ad712087 | 6 | * Please send support related email to: netdev@vger.kernel.org |
1da177e4 | 7 | * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html |
122952fc | 8 | * |
1da177e4 LT |
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. | |
122952fc | 15 | * |
1da177e4 LT |
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 | ||
23 | #include <linux/module.h> | |
24 | #include <linux/mm.h> | |
25 | #include <linux/in.h> | |
26 | #include <linux/init.h> | |
27 | #include <asm/uaccess.h> /* for copy_from_user */ | |
28 | #include <linux/skbuff.h> | |
29 | #include <linux/netdevice.h> | |
30 | #include <linux/etherdevice.h> | |
75b8846a | 31 | #include <linux/ethtool.h> |
1da177e4 LT |
32 | #include <net/datalink.h> |
33 | #include <net/p8022.h> | |
34 | #include <net/arp.h> | |
35 | ||
36 | #include "vlan.h" | |
37 | #include "vlanproc.h" | |
38 | #include <linux/if_vlan.h> | |
39 | #include <net/ip.h> | |
40 | ||
41 | /* | |
42 | * Rebuild the Ethernet MAC header. This is called after an ARP | |
43 | * (or in future other address resolution) has completed on this | |
44 | * sk_buff. We now let ARP fill in the other fields. | |
45 | * | |
46 | * This routine CANNOT use cached dst->neigh! | |
47 | * Really, it is used only when dst->neigh is wrong. | |
48 | * | |
49 | * TODO: This needs a checkup, I'm ignorant here. --BLG | |
50 | */ | |
ef3eb3e5 | 51 | static int vlan_dev_rebuild_header(struct sk_buff *skb) |
1da177e4 LT |
52 | { |
53 | struct net_device *dev = skb->dev; | |
54 | struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data); | |
55 | ||
56 | switch (veth->h_vlan_encapsulated_proto) { | |
57 | #ifdef CONFIG_INET | |
58 | case __constant_htons(ETH_P_IP): | |
59 | ||
60 | /* TODO: Confirm this will work with VLAN headers... */ | |
61 | return arp_find(veth->h_dest, skb); | |
122952fc | 62 | #endif |
1da177e4 | 63 | default: |
40f98e1a PM |
64 | pr_debug("%s: unable to resolve type %X addresses.\n", |
65 | dev->name, ntohs(veth->h_vlan_encapsulated_proto)); | |
122952fc | 66 | |
1da177e4 LT |
67 | memcpy(veth->h_source, dev->dev_addr, ETH_ALEN); |
68 | break; | |
3ff50b79 | 69 | } |
1da177e4 LT |
70 | |
71 | return 0; | |
72 | } | |
73 | ||
74 | static inline struct sk_buff *vlan_check_reorder_header(struct sk_buff *skb) | |
75 | { | |
9dfebcc6 | 76 | if (vlan_dev_info(skb->dev)->flags & VLAN_FLAG_REORDER_HDR) { |
11a100f8 PM |
77 | if (skb_cow(skb, skb_headroom(skb)) < 0) |
78 | skb = NULL; | |
1da177e4 LT |
79 | if (skb) { |
80 | /* Lifted from Gleb's VLAN code... */ | |
81 | memmove(skb->data - ETH_HLEN, | |
82 | skb->data - VLAN_ETH_HLEN, 12); | |
b0e380b1 | 83 | skb->mac_header += VLAN_HLEN; |
1da177e4 LT |
84 | } |
85 | } | |
86 | ||
87 | return skb; | |
88 | } | |
89 | ||
91b4f954 PE |
90 | static inline void vlan_set_encap_proto(struct sk_buff *skb, |
91 | struct vlan_hdr *vhdr) | |
92 | { | |
93 | __be16 proto; | |
94 | unsigned char *rawp; | |
95 | ||
96 | /* | |
97 | * Was a VLAN packet, grab the encapsulated protocol, which the layer | |
98 | * three protocols care about. | |
99 | */ | |
100 | ||
101 | proto = vhdr->h_vlan_encapsulated_proto; | |
102 | if (ntohs(proto) >= 1536) { | |
103 | skb->protocol = proto; | |
104 | return; | |
105 | } | |
106 | ||
107 | rawp = skb->data; | |
108 | if (*(unsigned short *)rawp == 0xFFFF) | |
109 | /* | |
110 | * This is a magic hack to spot IPX packets. Older Novell | |
111 | * breaks the protocol design and runs IPX over 802.3 without | |
112 | * an 802.2 LLC layer. We look for FFFF which isn't a used | |
113 | * 802.2 SSAP/DSAP. This won't work for fault tolerant netware | |
114 | * but does for the rest. | |
115 | */ | |
116 | skb->protocol = htons(ETH_P_802_3); | |
117 | else | |
118 | /* | |
119 | * Real 802.2 LLC | |
120 | */ | |
121 | skb->protocol = htons(ETH_P_802_2); | |
122 | } | |
123 | ||
1da177e4 | 124 | /* |
122952fc | 125 | * Determine the packet's protocol ID. The rule here is that we |
1da177e4 LT |
126 | * assume 802.3 if the type field is short enough to be a length. |
127 | * This is normal practice and works for any 'now in use' protocol. | |
128 | * | |
129 | * Also, at this point we assume that we ARE dealing exclusively with | |
130 | * VLAN packets, or packets that should be made into VLAN packets based | |
131 | * on a default VLAN ID. | |
132 | * | |
133 | * NOTE: Should be similar to ethernet/eth.c. | |
134 | * | |
135 | * SANITY NOTE: This method is called when a packet is moving up the stack | |
136 | * towards userland. To get here, it would have already passed | |
137 | * through the ethernet/eth.c eth_type_trans() method. | |
138 | * SANITY NOTE 2: We are referencing to the VLAN_HDR frields, which MAY be | |
139 | * stored UNALIGNED in the memory. RISC systems don't like | |
140 | * such cases very much... | |
2029cc2c PM |
141 | * SANITY NOTE 2a: According to Dave Miller & Alexey, it will always be |
142 | * aligned, so there doesn't need to be any of the unaligned | |
143 | * stuff. It has been commented out now... --Ben | |
1da177e4 LT |
144 | * |
145 | */ | |
146 | int vlan_skb_recv(struct sk_buff *skb, struct net_device *dev, | |
2029cc2c | 147 | struct packet_type *ptype, struct net_device *orig_dev) |
1da177e4 | 148 | { |
e7c243c9 | 149 | struct vlan_hdr *vhdr; |
1da177e4 | 150 | struct net_device_stats *stats; |
9bb8582e PM |
151 | u16 vlan_id; |
152 | u16 vlan_tci; | |
1da177e4 | 153 | |
2029cc2c PM |
154 | skb = skb_share_check(skb, GFP_ATOMIC); |
155 | if (skb == NULL) | |
31ffdbcb | 156 | goto err_free; |
e7c243c9 | 157 | |
31ffdbcb PM |
158 | if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) |
159 | goto err_free; | |
e7c243c9 | 160 | |
31ffdbcb | 161 | vhdr = (struct vlan_hdr *)skb->data; |
9bb8582e PM |
162 | vlan_tci = ntohs(vhdr->h_vlan_TCI); |
163 | vlan_id = vlan_tci & VLAN_VID_MASK; | |
1da177e4 | 164 | |
1da177e4 | 165 | rcu_read_lock(); |
9bb8582e | 166 | skb->dev = __find_vlan_dev(dev, vlan_id); |
1da177e4 | 167 | if (!skb->dev) { |
2029cc2c | 168 | pr_debug("%s: ERROR: No net_device for VID: %u on dev: %s\n", |
9bb8582e | 169 | __func__, vlan_id, dev->name); |
31ffdbcb | 170 | goto err_unlock; |
1da177e4 LT |
171 | } |
172 | ||
173 | skb->dev->last_rx = jiffies; | |
174 | ||
7bd38d77 | 175 | stats = &skb->dev->stats; |
1da177e4 LT |
176 | stats->rx_packets++; |
177 | stats->rx_bytes += skb->len; | |
178 | ||
cbb042f9 | 179 | skb_pull_rcsum(skb, VLAN_HLEN); |
a388442c | 180 | |
9bb8582e | 181 | skb->priority = vlan_get_ingress_priority(skb->dev, vlan_tci); |
1da177e4 | 182 | |
40f98e1a | 183 | pr_debug("%s: priority: %u for TCI: %hu\n", |
9bb8582e | 184 | __func__, skb->priority, vlan_tci); |
1da177e4 | 185 | |
1da177e4 LT |
186 | switch (skb->pkt_type) { |
187 | case PACKET_BROADCAST: /* Yeah, stats collect these together.. */ | |
2029cc2c | 188 | /* stats->broadcast ++; // no such counter :-( */ |
1da177e4 LT |
189 | break; |
190 | ||
191 | case PACKET_MULTICAST: | |
192 | stats->multicast++; | |
193 | break; | |
194 | ||
122952fc | 195 | case PACKET_OTHERHOST: |
1da177e4 | 196 | /* Our lower layer thinks this is not local, let's make sure. |
2029cc2c PM |
197 | * This allows the VLAN to have a different MAC than the |
198 | * underlying device, and still route correctly. | |
1da177e4 | 199 | */ |
2029cc2c PM |
200 | if (!compare_ether_addr(eth_hdr(skb)->h_dest, |
201 | skb->dev->dev_addr)) | |
1da177e4 | 202 | skb->pkt_type = PACKET_HOST; |
1da177e4 LT |
203 | break; |
204 | default: | |
205 | break; | |
3ff50b79 | 206 | } |
1da177e4 | 207 | |
91b4f954 | 208 | vlan_set_encap_proto(skb, vhdr); |
1da177e4 | 209 | |
1da177e4 | 210 | skb = vlan_check_reorder_header(skb); |
31ffdbcb | 211 | if (!skb) { |
1da177e4 | 212 | stats->rx_errors++; |
31ffdbcb | 213 | goto err_unlock; |
1da177e4 | 214 | } |
31ffdbcb PM |
215 | |
216 | netif_rx(skb); | |
1da177e4 | 217 | rcu_read_unlock(); |
31ffdbcb PM |
218 | return NET_RX_SUCCESS; |
219 | ||
220 | err_unlock: | |
221 | rcu_read_unlock(); | |
222 | err_free: | |
223 | kfree_skb(skb); | |
224 | return NET_RX_DROP; | |
1da177e4 LT |
225 | } |
226 | ||
9bb8582e | 227 | static inline u16 |
2029cc2c | 228 | vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb) |
1da177e4 | 229 | { |
2029cc2c | 230 | struct vlan_priority_tci_mapping *mp; |
1da177e4 | 231 | |
2029cc2c | 232 | mp = vlan_dev_info(dev)->egress_priority_map[(skb->priority & 0xF)]; |
1da177e4 LT |
233 | while (mp) { |
234 | if (mp->priority == skb->priority) { | |
2029cc2c PM |
235 | return mp->vlan_qos; /* This should already be shifted |
236 | * to mask correctly with the | |
237 | * VLAN's TCI */ | |
1da177e4 LT |
238 | } |
239 | mp = mp->next; | |
240 | } | |
241 | return 0; | |
242 | } | |
243 | ||
244 | /* | |
122952fc | 245 | * Create the VLAN header for an arbitrary protocol layer |
1da177e4 LT |
246 | * |
247 | * saddr=NULL means use device source address | |
248 | * daddr=NULL means leave destination address (eg unresolved arp) | |
249 | * | |
250 | * This is called when the SKB is moving down the stack towards the | |
251 | * physical devices. | |
252 | */ | |
ef3eb3e5 PM |
253 | static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev, |
254 | unsigned short type, | |
255 | const void *daddr, const void *saddr, | |
256 | unsigned int len) | |
1da177e4 LT |
257 | { |
258 | struct vlan_hdr *vhdr; | |
9bb8582e | 259 | u16 vlan_tci = 0; |
1da177e4 LT |
260 | int rc = 0; |
261 | int build_vlan_header = 0; | |
1da177e4 | 262 | |
40f98e1a | 263 | pr_debug("%s: skb: %p type: %hx len: %u vlan_id: %hx, daddr: %p\n", |
0dc47877 | 264 | __func__, skb, type, len, vlan_dev_info(dev)->vlan_id, |
2029cc2c | 265 | daddr); |
1da177e4 | 266 | |
11a100f8 PM |
267 | if (WARN_ON(skb_headroom(skb) < dev->hard_header_len)) |
268 | return -ENOSPC; | |
269 | ||
1da177e4 LT |
270 | /* build vlan header only if re_order_header flag is NOT set. This |
271 | * fixes some programs that get confused when they see a VLAN device | |
272 | * sending a frame that is VLAN encoded (the consensus is that the VLAN | |
273 | * device should look completely like an Ethernet device when the | |
122952fc | 274 | * REORDER_HEADER flag is set) The drawback to this is some extra |
1da177e4 LT |
275 | * header shuffling in the hard_start_xmit. Users can turn off this |
276 | * REORDER behaviour with the vconfig tool. | |
277 | */ | |
9dfebcc6 | 278 | if (!(vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR)) |
a4bf3af4 | 279 | build_vlan_header = 1; |
1da177e4 LT |
280 | |
281 | if (build_vlan_header) { | |
282 | vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN); | |
283 | ||
284 | /* build the four bytes that make this a VLAN header. */ | |
285 | ||
2029cc2c PM |
286 | /* Now, construct the second two bytes. This field looks |
287 | * something like: | |
1da177e4 LT |
288 | * usr_priority: 3 bits (high bits) |
289 | * CFI 1 bit | |
290 | * VLAN ID 12 bits (low bits) | |
291 | * | |
292 | */ | |
9bb8582e PM |
293 | vlan_tci = vlan_dev_info(dev)->vlan_id; |
294 | vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb); | |
1da177e4 | 295 | |
9bb8582e | 296 | vhdr->h_vlan_TCI = htons(vlan_tci); |
1da177e4 LT |
297 | |
298 | /* | |
2029cc2c PM |
299 | * Set the protocol type. For a packet of type ETH_P_802_3 we |
300 | * put the length in here instead. It is up to the 802.2 | |
301 | * layer to carry protocol information. | |
1da177e4 LT |
302 | */ |
303 | ||
2029cc2c | 304 | if (type != ETH_P_802_3) |
1da177e4 | 305 | vhdr->h_vlan_encapsulated_proto = htons(type); |
2029cc2c | 306 | else |
1da177e4 | 307 | vhdr->h_vlan_encapsulated_proto = htons(len); |
279e172a JB |
308 | |
309 | skb->protocol = htons(ETH_P_8021Q); | |
1da177e4 LT |
310 | } |
311 | ||
312 | /* Before delegating work to the lower layer, enter our MAC-address */ | |
313 | if (saddr == NULL) | |
314 | saddr = dev->dev_addr; | |
315 | ||
9dfebcc6 | 316 | dev = vlan_dev_info(dev)->real_dev; |
1da177e4 | 317 | |
1da177e4 LT |
318 | if (build_vlan_header) { |
319 | /* Now make the underlying real hard header */ | |
0c4e8581 SH |
320 | rc = dev_hard_header(skb, dev, ETH_P_8021Q, daddr, saddr, |
321 | len + VLAN_HLEN); | |
322 | if (rc > 0) | |
1da177e4 | 323 | rc += VLAN_HLEN; |
0c4e8581 | 324 | else if (rc < 0) |
1da177e4 | 325 | rc -= VLAN_HLEN; |
0c4e8581 | 326 | } else |
2029cc2c PM |
327 | /* If here, then we'll just make a normal looking ethernet |
328 | * frame, but, the hard_start_xmit method will insert the tag | |
329 | * (it has to be able to do this for bridged and other skbs | |
330 | * that don't come down the protocol stack in an orderly manner. | |
1da177e4 | 331 | */ |
0c4e8581 | 332 | rc = dev_hard_header(skb, dev, type, daddr, saddr, len); |
1da177e4 LT |
333 | |
334 | return rc; | |
335 | } | |
336 | ||
ef3eb3e5 | 337 | static int vlan_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev) |
1da177e4 | 338 | { |
7bd38d77 | 339 | struct net_device_stats *stats = &dev->stats; |
1da177e4 | 340 | struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data); |
55b01e86 | 341 | |
1da177e4 LT |
342 | /* Handle non-VLAN frames if they are sent to us, for example by DHCP. |
343 | * | |
344 | * NOTE: THIS ASSUMES DIX ETHERNET, SPECIFICALLY NOT SUPPORTING | |
345 | * OTHER THINGS LIKE FDDI/TokenRing/802.3 SNAPs... | |
346 | */ | |
347 | ||
6ab3b487 | 348 | if (veth->h_vlan_proto != htons(ETH_P_8021Q) || |
9dfebcc6 | 349 | vlan_dev_info(dev)->flags & VLAN_FLAG_REORDER_HDR) { |
1da177e4 | 350 | int orig_headroom = skb_headroom(skb); |
9bb8582e | 351 | u16 vlan_tci; |
1da177e4 LT |
352 | |
353 | /* This is not a VLAN frame...but we can fix that! */ | |
9dfebcc6 | 354 | vlan_dev_info(dev)->cnt_encap_on_xmit++; |
1da177e4 | 355 | |
40f98e1a | 356 | pr_debug("%s: proto to encap: 0x%hx\n", |
577f99c1 | 357 | __func__, ntohs(veth->h_vlan_proto)); |
1da177e4 LT |
358 | /* Construct the second two bytes. This field looks something |
359 | * like: | |
360 | * usr_priority: 3 bits (high bits) | |
361 | * CFI 1 bit | |
362 | * VLAN ID 12 bits (low bits) | |
363 | */ | |
9bb8582e PM |
364 | vlan_tci = vlan_dev_info(dev)->vlan_id; |
365 | vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb); | |
1da177e4 | 366 | |
9bb8582e | 367 | skb = __vlan_put_tag(skb, vlan_tci); |
1da177e4 LT |
368 | if (!skb) { |
369 | stats->tx_dropped++; | |
370 | return 0; | |
371 | } | |
372 | ||
2029cc2c | 373 | if (orig_headroom < VLAN_HLEN) |
9dfebcc6 | 374 | vlan_dev_info(dev)->cnt_inc_headroom_on_tx++; |
1da177e4 LT |
375 | } |
376 | ||
40f98e1a | 377 | pr_debug("%s: about to send skb: %p to dev: %s\n", |
0dc47877 | 378 | __func__, skb, skb->dev->name); |
55b01e86 DM |
379 | pr_debug(" " MAC_FMT " " MAC_FMT " %4hx %4hx %4hx\n", |
380 | veth->h_dest[0], veth->h_dest[1], veth->h_dest[2], | |
381 | veth->h_dest[3], veth->h_dest[4], veth->h_dest[5], | |
382 | veth->h_source[0], veth->h_source[1], veth->h_source[2], | |
383 | veth->h_source[3], veth->h_source[4], veth->h_source[5], | |
40f98e1a PM |
384 | veth->h_vlan_proto, veth->h_vlan_TCI, |
385 | veth->h_vlan_encapsulated_proto); | |
1da177e4 LT |
386 | |
387 | stats->tx_packets++; /* for statics only */ | |
388 | stats->tx_bytes += skb->len; | |
389 | ||
9dfebcc6 | 390 | skb->dev = vlan_dev_info(dev)->real_dev; |
1da177e4 LT |
391 | dev_queue_xmit(skb); |
392 | ||
393 | return 0; | |
394 | } | |
395 | ||
ef3eb3e5 PM |
396 | static int vlan_dev_hwaccel_hard_start_xmit(struct sk_buff *skb, |
397 | struct net_device *dev) | |
1da177e4 | 398 | { |
7bd38d77 | 399 | struct net_device_stats *stats = &dev->stats; |
9bb8582e | 400 | u16 vlan_tci; |
1da177e4 LT |
401 | |
402 | /* Construct the second two bytes. This field looks something | |
403 | * like: | |
404 | * usr_priority: 3 bits (high bits) | |
405 | * CFI 1 bit | |
406 | * VLAN ID 12 bits (low bits) | |
407 | */ | |
9bb8582e PM |
408 | vlan_tci = vlan_dev_info(dev)->vlan_id; |
409 | vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb); | |
410 | skb = __vlan_hwaccel_put_tag(skb, vlan_tci); | |
1da177e4 LT |
411 | |
412 | stats->tx_packets++; | |
413 | stats->tx_bytes += skb->len; | |
414 | ||
9dfebcc6 | 415 | skb->dev = vlan_dev_info(dev)->real_dev; |
1da177e4 LT |
416 | dev_queue_xmit(skb); |
417 | ||
418 | return 0; | |
419 | } | |
420 | ||
ef3eb3e5 | 421 | static int vlan_dev_change_mtu(struct net_device *dev, int new_mtu) |
1da177e4 LT |
422 | { |
423 | /* TODO: gotta make sure the underlying layer can handle it, | |
424 | * maybe an IFF_VLAN_CAPABLE flag for devices? | |
425 | */ | |
9dfebcc6 | 426 | if (vlan_dev_info(dev)->real_dev->mtu < new_mtu) |
1da177e4 LT |
427 | return -ERANGE; |
428 | ||
429 | dev->mtu = new_mtu; | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
c17d8874 | 434 | void vlan_dev_set_ingress_priority(const struct net_device *dev, |
9bb8582e | 435 | u32 skb_prio, u16 vlan_prio) |
1da177e4 | 436 | { |
9dfebcc6 | 437 | struct vlan_dev_info *vlan = vlan_dev_info(dev); |
b020cb48 PM |
438 | |
439 | if (vlan->ingress_priority_map[vlan_prio & 0x7] && !skb_prio) | |
440 | vlan->nr_ingress_mappings--; | |
441 | else if (!vlan->ingress_priority_map[vlan_prio & 0x7] && skb_prio) | |
442 | vlan->nr_ingress_mappings++; | |
443 | ||
444 | vlan->ingress_priority_map[vlan_prio & 0x7] = skb_prio; | |
1da177e4 LT |
445 | } |
446 | ||
c17d8874 | 447 | int vlan_dev_set_egress_priority(const struct net_device *dev, |
9bb8582e | 448 | u32 skb_prio, u16 vlan_prio) |
1da177e4 | 449 | { |
9dfebcc6 | 450 | struct vlan_dev_info *vlan = vlan_dev_info(dev); |
1da177e4 LT |
451 | struct vlan_priority_tci_mapping *mp = NULL; |
452 | struct vlan_priority_tci_mapping *np; | |
b020cb48 | 453 | u32 vlan_qos = (vlan_prio << 13) & 0xE000; |
122952fc | 454 | |
c17d8874 | 455 | /* See if a priority mapping exists.. */ |
b020cb48 | 456 | mp = vlan->egress_priority_map[skb_prio & 0xF]; |
c17d8874 PM |
457 | while (mp) { |
458 | if (mp->priority == skb_prio) { | |
b020cb48 PM |
459 | if (mp->vlan_qos && !vlan_qos) |
460 | vlan->nr_egress_mappings--; | |
461 | else if (!mp->vlan_qos && vlan_qos) | |
462 | vlan->nr_egress_mappings++; | |
463 | mp->vlan_qos = vlan_qos; | |
c17d8874 | 464 | return 0; |
1da177e4 | 465 | } |
c17d8874 | 466 | mp = mp->next; |
1da177e4 | 467 | } |
c17d8874 PM |
468 | |
469 | /* Create a new mapping then. */ | |
b020cb48 | 470 | mp = vlan->egress_priority_map[skb_prio & 0xF]; |
c17d8874 PM |
471 | np = kmalloc(sizeof(struct vlan_priority_tci_mapping), GFP_KERNEL); |
472 | if (!np) | |
473 | return -ENOBUFS; | |
474 | ||
475 | np->next = mp; | |
476 | np->priority = skb_prio; | |
b020cb48 PM |
477 | np->vlan_qos = vlan_qos; |
478 | vlan->egress_priority_map[skb_prio & 0xF] = np; | |
479 | if (vlan_qos) | |
480 | vlan->nr_egress_mappings++; | |
c17d8874 | 481 | return 0; |
1da177e4 LT |
482 | } |
483 | ||
a4bf3af4 | 484 | /* Flags are defined in the vlan_flags enum in include/linux/if_vlan.h file. */ |
b3ce0325 | 485 | int vlan_dev_change_flags(const struct net_device *dev, u32 flags, u32 mask) |
1da177e4 | 486 | { |
b3ce0325 PM |
487 | struct vlan_dev_info *vlan = vlan_dev_info(dev); |
488 | u32 old_flags = vlan->flags; | |
489 | ||
70c03b49 | 490 | if (mask & ~(VLAN_FLAG_REORDER_HDR | VLAN_FLAG_GVRP)) |
b3ce0325 PM |
491 | return -EINVAL; |
492 | ||
493 | vlan->flags = (old_flags & ~mask) | (flags & mask); | |
70c03b49 PM |
494 | |
495 | if (netif_running(dev) && (vlan->flags ^ old_flags) & VLAN_FLAG_GVRP) { | |
496 | if (vlan->flags & VLAN_FLAG_GVRP) | |
497 | vlan_gvrp_request_join(dev); | |
498 | else | |
499 | vlan_gvrp_request_leave(dev); | |
500 | } | |
b3ce0325 | 501 | return 0; |
1da177e4 LT |
502 | } |
503 | ||
c17d8874 | 504 | void vlan_dev_get_realdev_name(const struct net_device *dev, char *result) |
1da177e4 | 505 | { |
9dfebcc6 | 506 | strncpy(result, vlan_dev_info(dev)->real_dev->name, 23); |
1da177e4 LT |
507 | } |
508 | ||
ef3eb3e5 | 509 | static int vlan_dev_open(struct net_device *dev) |
1da177e4 | 510 | { |
9dfebcc6 | 511 | struct vlan_dev_info *vlan = vlan_dev_info(dev); |
8c979c26 PM |
512 | struct net_device *real_dev = vlan->real_dev; |
513 | int err; | |
514 | ||
515 | if (!(real_dev->flags & IFF_UP)) | |
1da177e4 LT |
516 | return -ENETDOWN; |
517 | ||
8c979c26 PM |
518 | if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) { |
519 | err = dev_unicast_add(real_dev, dev->dev_addr, ETH_ALEN); | |
520 | if (err < 0) | |
521 | return err; | |
522 | } | |
523 | memcpy(vlan->real_dev_addr, real_dev->dev_addr, ETH_ALEN); | |
524 | ||
6c78dcbd PM |
525 | if (dev->flags & IFF_ALLMULTI) |
526 | dev_set_allmulti(real_dev, 1); | |
527 | if (dev->flags & IFF_PROMISC) | |
528 | dev_set_promiscuity(real_dev, 1); | |
529 | ||
70c03b49 PM |
530 | if (vlan->flags & VLAN_FLAG_GVRP) |
531 | vlan_gvrp_request_join(dev); | |
532 | ||
1da177e4 LT |
533 | return 0; |
534 | } | |
535 | ||
ef3eb3e5 | 536 | static int vlan_dev_stop(struct net_device *dev) |
1da177e4 | 537 | { |
70c03b49 PM |
538 | struct vlan_dev_info *vlan = vlan_dev_info(dev); |
539 | struct net_device *real_dev = vlan->real_dev; | |
540 | ||
541 | if (vlan->flags & VLAN_FLAG_GVRP) | |
542 | vlan_gvrp_request_leave(dev); | |
8c979c26 | 543 | |
56addd6e | 544 | dev_mc_unsync(real_dev, dev); |
e83a2ea8 | 545 | dev_unicast_unsync(real_dev, dev); |
6c78dcbd PM |
546 | if (dev->flags & IFF_ALLMULTI) |
547 | dev_set_allmulti(real_dev, -1); | |
548 | if (dev->flags & IFF_PROMISC) | |
549 | dev_set_promiscuity(real_dev, -1); | |
550 | ||
8c979c26 PM |
551 | if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) |
552 | dev_unicast_delete(real_dev, dev->dev_addr, dev->addr_len); | |
553 | ||
1da177e4 LT |
554 | return 0; |
555 | } | |
556 | ||
ef3eb3e5 | 557 | static int vlan_dev_set_mac_address(struct net_device *dev, void *p) |
39aaac11 | 558 | { |
9dfebcc6 | 559 | struct net_device *real_dev = vlan_dev_info(dev)->real_dev; |
39aaac11 PM |
560 | struct sockaddr *addr = p; |
561 | int err; | |
562 | ||
563 | if (!is_valid_ether_addr(addr->sa_data)) | |
564 | return -EADDRNOTAVAIL; | |
565 | ||
566 | if (!(dev->flags & IFF_UP)) | |
567 | goto out; | |
568 | ||
569 | if (compare_ether_addr(addr->sa_data, real_dev->dev_addr)) { | |
570 | err = dev_unicast_add(real_dev, addr->sa_data, ETH_ALEN); | |
571 | if (err < 0) | |
572 | return err; | |
573 | } | |
574 | ||
575 | if (compare_ether_addr(dev->dev_addr, real_dev->dev_addr)) | |
576 | dev_unicast_delete(real_dev, dev->dev_addr, ETH_ALEN); | |
577 | ||
578 | out: | |
579 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); | |
580 | return 0; | |
581 | } | |
582 | ||
ef3eb3e5 | 583 | static int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
1da177e4 | 584 | { |
9dfebcc6 | 585 | struct net_device *real_dev = vlan_dev_info(dev)->real_dev; |
1da177e4 LT |
586 | struct ifreq ifrr; |
587 | int err = -EOPNOTSUPP; | |
588 | ||
589 | strncpy(ifrr.ifr_name, real_dev->name, IFNAMSIZ); | |
590 | ifrr.ifr_ifru = ifr->ifr_ifru; | |
591 | ||
2029cc2c | 592 | switch (cmd) { |
1da177e4 LT |
593 | case SIOCGMIIPHY: |
594 | case SIOCGMIIREG: | |
595 | case SIOCSMIIREG: | |
122952fc | 596 | if (real_dev->do_ioctl && netif_device_present(real_dev)) |
1da177e4 LT |
597 | err = real_dev->do_ioctl(real_dev, &ifrr, cmd); |
598 | break; | |
1da177e4 LT |
599 | } |
600 | ||
122952fc | 601 | if (!err) |
1da177e4 LT |
602 | ifr->ifr_ifru = ifrr.ifr_ifru; |
603 | ||
604 | return err; | |
605 | } | |
606 | ||
ef3eb3e5 | 607 | static void vlan_dev_change_rx_flags(struct net_device *dev, int change) |
6c78dcbd | 608 | { |
9dfebcc6 | 609 | struct net_device *real_dev = vlan_dev_info(dev)->real_dev; |
6c78dcbd PM |
610 | |
611 | if (change & IFF_ALLMULTI) | |
612 | dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1); | |
613 | if (change & IFF_PROMISC) | |
614 | dev_set_promiscuity(real_dev, dev->flags & IFF_PROMISC ? 1 : -1); | |
615 | } | |
616 | ||
e83a2ea8 | 617 | static void vlan_dev_set_rx_mode(struct net_device *vlan_dev) |
1da177e4 | 618 | { |
9dfebcc6 | 619 | dev_mc_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev); |
e83a2ea8 | 620 | dev_unicast_sync(vlan_dev_info(vlan_dev)->real_dev, vlan_dev); |
1da177e4 | 621 | } |
ef3eb3e5 PM |
622 | |
623 | /* | |
624 | * vlan network devices have devices nesting below it, and are a special | |
625 | * "super class" of normal network devices; split their locks off into a | |
626 | * separate class since they always nest. | |
627 | */ | |
628 | static struct lock_class_key vlan_netdev_xmit_lock_key; | |
629 | ||
630 | static const struct header_ops vlan_header_ops = { | |
631 | .create = vlan_dev_hard_header, | |
632 | .rebuild = vlan_dev_rebuild_header, | |
633 | .parse = eth_header_parse, | |
634 | }; | |
635 | ||
636 | static int vlan_dev_init(struct net_device *dev) | |
637 | { | |
9dfebcc6 | 638 | struct net_device *real_dev = vlan_dev_info(dev)->real_dev; |
ef3eb3e5 PM |
639 | int subclass = 0; |
640 | ||
641 | /* IFF_BROADCAST|IFF_MULTICAST; ??? */ | |
0ed21b32 | 642 | dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI); |
ef3eb3e5 PM |
643 | dev->iflink = real_dev->ifindex; |
644 | dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) | | |
645 | (1<<__LINK_STATE_DORMANT))) | | |
646 | (1<<__LINK_STATE_PRESENT); | |
647 | ||
289c79a4 | 648 | dev->features |= real_dev->features & real_dev->vlan_features; |
5fb13570 | 649 | |
ef3eb3e5 PM |
650 | /* ipv6 shared card related stuff */ |
651 | dev->dev_id = real_dev->dev_id; | |
652 | ||
653 | if (is_zero_ether_addr(dev->dev_addr)) | |
654 | memcpy(dev->dev_addr, real_dev->dev_addr, dev->addr_len); | |
655 | if (is_zero_ether_addr(dev->broadcast)) | |
656 | memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len); | |
657 | ||
658 | if (real_dev->features & NETIF_F_HW_VLAN_TX) { | |
659 | dev->header_ops = real_dev->header_ops; | |
660 | dev->hard_header_len = real_dev->hard_header_len; | |
661 | dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit; | |
662 | } else { | |
663 | dev->header_ops = &vlan_header_ops; | |
664 | dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN; | |
665 | dev->hard_start_xmit = vlan_dev_hard_start_xmit; | |
666 | } | |
667 | ||
26a25239 | 668 | if (is_vlan_dev(real_dev)) |
ef3eb3e5 PM |
669 | subclass = 1; |
670 | ||
671 | lockdep_set_class_and_subclass(&dev->_xmit_lock, | |
672 | &vlan_netdev_xmit_lock_key, subclass); | |
673 | return 0; | |
674 | } | |
675 | ||
23556323 PE |
676 | static void vlan_dev_uninit(struct net_device *dev) |
677 | { | |
678 | struct vlan_priority_tci_mapping *pm; | |
679 | struct vlan_dev_info *vlan = vlan_dev_info(dev); | |
680 | int i; | |
681 | ||
682 | for (i = 0; i < ARRAY_SIZE(vlan->egress_priority_map); i++) { | |
683 | while ((pm = vlan->egress_priority_map[i]) != NULL) { | |
684 | vlan->egress_priority_map[i] = pm->next; | |
685 | kfree(pm); | |
686 | } | |
687 | } | |
688 | } | |
689 | ||
75b8846a PM |
690 | static u32 vlan_ethtool_get_rx_csum(struct net_device *dev) |
691 | { | |
692 | const struct vlan_dev_info *vlan = vlan_dev_info(dev); | |
693 | struct net_device *real_dev = vlan->real_dev; | |
694 | ||
695 | if (real_dev->ethtool_ops == NULL || | |
696 | real_dev->ethtool_ops->get_rx_csum == NULL) | |
697 | return 0; | |
698 | return real_dev->ethtool_ops->get_rx_csum(real_dev); | |
699 | } | |
700 | ||
701 | static const struct ethtool_ops vlan_ethtool_ops = { | |
702 | .get_link = ethtool_op_get_link, | |
703 | .get_rx_csum = vlan_ethtool_get_rx_csum, | |
704 | }; | |
705 | ||
ef3eb3e5 PM |
706 | void vlan_setup(struct net_device *dev) |
707 | { | |
708 | ether_setup(dev); | |
709 | ||
710 | dev->priv_flags |= IFF_802_1Q_VLAN; | |
711 | dev->tx_queue_len = 0; | |
712 | ||
713 | dev->change_mtu = vlan_dev_change_mtu; | |
714 | dev->init = vlan_dev_init; | |
23556323 | 715 | dev->uninit = vlan_dev_uninit; |
ef3eb3e5 PM |
716 | dev->open = vlan_dev_open; |
717 | dev->stop = vlan_dev_stop; | |
718 | dev->set_mac_address = vlan_dev_set_mac_address; | |
e83a2ea8 CL |
719 | dev->set_rx_mode = vlan_dev_set_rx_mode; |
720 | dev->set_multicast_list = vlan_dev_set_rx_mode; | |
ef3eb3e5 PM |
721 | dev->change_rx_flags = vlan_dev_change_rx_flags; |
722 | dev->do_ioctl = vlan_dev_ioctl; | |
723 | dev->destructor = free_netdev; | |
75b8846a | 724 | dev->ethtool_ops = &vlan_ethtool_ops; |
ef3eb3e5 PM |
725 | |
726 | memset(dev->broadcast, 0, ETH_ALEN); | |
727 | } |