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