Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | |
122952fc | 8 | * |
1da177e4 LT |
9 | * Fixes: |
10 | * Fix for packet capture - Nick Eggleston <nick@dccinc.com>; | |
11 | * Add HW acceleration hooks - David S. Miller <davem@redhat.com>; | |
12 | * Correct all the locking - David S. Miller <davem@redhat.com>; | |
13 | * Use hash table for VLAN groups - David S. Miller <davem@redhat.com> | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU General Public License | |
17 | * as published by the Free Software Foundation; either version | |
18 | * 2 of the License, or (at your option) any later version. | |
19 | */ | |
20 | ||
21 | #include <asm/uaccess.h> /* for copy_from_user */ | |
4fc268d2 | 22 | #include <linux/capability.h> |
1da177e4 LT |
23 | #include <linux/module.h> |
24 | #include <linux/netdevice.h> | |
25 | #include <linux/skbuff.h> | |
26 | #include <net/datalink.h> | |
27 | #include <linux/mm.h> | |
28 | #include <linux/in.h> | |
29 | #include <linux/init.h> | |
30 | #include <net/p8022.h> | |
31 | #include <net/arp.h> | |
32 | #include <linux/rtnetlink.h> | |
33 | #include <linux/notifier.h> | |
34 | ||
35 | #include <linux/if_vlan.h> | |
36 | #include "vlan.h" | |
37 | #include "vlanproc.h" | |
38 | ||
39 | #define DRV_VERSION "1.8" | |
40 | ||
41 | /* Global VLAN variables */ | |
42 | ||
43 | /* Our listing of VLAN group(s) */ | |
44 | static struct hlist_head vlan_group_hash[VLAN_GRP_HASH_SIZE]; | |
45 | #define vlan_grp_hashfn(IDX) ((((IDX) >> VLAN_GRP_HASH_SHIFT) ^ (IDX)) & VLAN_GRP_HASH_MASK) | |
46 | ||
47 | static char vlan_fullname[] = "802.1Q VLAN Support"; | |
48 | static char vlan_version[] = DRV_VERSION; | |
49 | static char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>"; | |
50 | static char vlan_buggyright[] = "David S. Miller <davem@redhat.com>"; | |
51 | ||
52 | static int vlan_device_event(struct notifier_block *, unsigned long, void *); | |
53 | static int vlan_ioctl_handler(void __user *); | |
54 | static int unregister_vlan_dev(struct net_device *, unsigned short ); | |
55 | ||
56 | static struct notifier_block vlan_notifier_block = { | |
57 | .notifier_call = vlan_device_event, | |
58 | }; | |
59 | ||
60 | /* These may be changed at run-time through IOCTLs */ | |
61 | ||
62 | /* Determines interface naming scheme. */ | |
63 | unsigned short vlan_name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD; | |
64 | ||
65 | static struct packet_type vlan_packet_type = { | |
66 | .type = __constant_htons(ETH_P_8021Q), | |
67 | .func = vlan_skb_recv, /* VLAN receive method */ | |
68 | }; | |
69 | ||
1da177e4 LT |
70 | /* End of global variables definitions. */ |
71 | ||
72 | /* | |
73 | * Function vlan_proto_init (pro) | |
74 | * | |
122952fc | 75 | * Initialize VLAN protocol layer, |
1da177e4 LT |
76 | * |
77 | */ | |
78 | static int __init vlan_proto_init(void) | |
79 | { | |
80 | int err; | |
81 | ||
82 | printk(VLAN_INF "%s v%s %s\n", | |
83 | vlan_fullname, vlan_version, vlan_copyright); | |
84 | printk(VLAN_INF "All bugs added by %s\n", | |
85 | vlan_buggyright); | |
86 | ||
87 | /* proc file system initialization */ | |
88 | err = vlan_proc_init(); | |
89 | if (err < 0) { | |
122952fc | 90 | printk(KERN_ERR |
1da177e4 LT |
91 | "%s %s: can't create entry in proc filesystem!\n", |
92 | __FUNCTION__, VLAN_NAME); | |
93 | return err; | |
94 | } | |
95 | ||
96 | dev_add_pack(&vlan_packet_type); | |
97 | ||
98 | /* Register us to receive netdevice events */ | |
99 | err = register_netdevice_notifier(&vlan_notifier_block); | |
100 | if (err < 0) { | |
101 | dev_remove_pack(&vlan_packet_type); | |
102 | vlan_proc_cleanup(); | |
103 | return err; | |
104 | } | |
105 | ||
106 | vlan_ioctl_set(vlan_ioctl_handler); | |
107 | ||
108 | return 0; | |
109 | } | |
110 | ||
122952fc | 111 | /* Cleanup all vlan devices |
1da177e4 LT |
112 | * Note: devices that have been registered that but not |
113 | * brought up will exist but have no module ref count. | |
114 | */ | |
115 | static void __exit vlan_cleanup_devices(void) | |
116 | { | |
117 | struct net_device *dev, *nxt; | |
118 | ||
119 | rtnl_lock(); | |
7562f876 | 120 | for_each_netdev_safe(dev, nxt) { |
1da177e4 LT |
121 | if (dev->priv_flags & IFF_802_1Q_VLAN) { |
122 | unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev, | |
123 | VLAN_DEV_INFO(dev)->vlan_id); | |
124 | ||
125 | unregister_netdevice(dev); | |
126 | } | |
127 | } | |
128 | rtnl_unlock(); | |
129 | } | |
130 | ||
131 | /* | |
132 | * Module 'remove' entry point. | |
133 | * o delete /proc/net/router directory and static entries. | |
122952fc | 134 | */ |
1da177e4 LT |
135 | static void __exit vlan_cleanup_module(void) |
136 | { | |
137 | int i; | |
138 | ||
139 | vlan_ioctl_set(NULL); | |
140 | ||
141 | /* Un-register us from receiving netdevice events */ | |
142 | unregister_netdevice_notifier(&vlan_notifier_block); | |
143 | ||
144 | dev_remove_pack(&vlan_packet_type); | |
145 | vlan_cleanup_devices(); | |
146 | ||
147 | /* This table must be empty if there are no module | |
148 | * references left. | |
149 | */ | |
150 | for (i = 0; i < VLAN_GRP_HASH_SIZE; i++) { | |
151 | BUG_ON(!hlist_empty(&vlan_group_hash[i])); | |
152 | } | |
153 | vlan_proc_cleanup(); | |
154 | ||
155 | synchronize_net(); | |
156 | } | |
157 | ||
158 | module_init(vlan_proto_init); | |
159 | module_exit(vlan_cleanup_module); | |
160 | ||
161 | /* Must be invoked with RCU read lock (no preempt) */ | |
162 | static struct vlan_group *__vlan_find_group(int real_dev_ifindex) | |
163 | { | |
164 | struct vlan_group *grp; | |
165 | struct hlist_node *n; | |
166 | int hash = vlan_grp_hashfn(real_dev_ifindex); | |
167 | ||
168 | hlist_for_each_entry_rcu(grp, n, &vlan_group_hash[hash], hlist) { | |
169 | if (grp->real_dev_ifindex == real_dev_ifindex) | |
170 | return grp; | |
171 | } | |
172 | ||
173 | return NULL; | |
174 | } | |
175 | ||
176 | /* Find the protocol handler. Assumes VID < VLAN_VID_MASK. | |
177 | * | |
178 | * Must be invoked with RCU read lock (no preempt) | |
179 | */ | |
180 | struct net_device *__find_vlan_dev(struct net_device *real_dev, | |
181 | unsigned short VID) | |
182 | { | |
183 | struct vlan_group *grp = __vlan_find_group(real_dev->ifindex); | |
184 | ||
185 | if (grp) | |
5c15bdec | 186 | return vlan_group_get_device(grp, VID); |
1da177e4 LT |
187 | |
188 | return NULL; | |
189 | } | |
190 | ||
5c15bdec DA |
191 | static void vlan_group_free(struct vlan_group *grp) |
192 | { | |
193 | int i; | |
194 | ||
195 | for (i=0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++) | |
196 | kfree(grp->vlan_devices_arrays[i]); | |
197 | kfree(grp); | |
198 | } | |
199 | ||
42429aae PM |
200 | static struct vlan_group *vlan_group_alloc(int ifindex) |
201 | { | |
202 | struct vlan_group *grp; | |
203 | unsigned int size; | |
204 | unsigned int i; | |
205 | ||
206 | grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL); | |
207 | if (!grp) | |
208 | return NULL; | |
209 | ||
210 | size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN; | |
211 | ||
212 | for (i = 0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++) { | |
213 | grp->vlan_devices_arrays[i] = kzalloc(size, GFP_KERNEL); | |
214 | if (!grp->vlan_devices_arrays[i]) | |
215 | goto err; | |
216 | } | |
217 | ||
218 | grp->real_dev_ifindex = ifindex; | |
219 | hlist_add_head_rcu(&grp->hlist, | |
220 | &vlan_group_hash[vlan_grp_hashfn(ifindex)]); | |
221 | return grp; | |
222 | ||
223 | err: | |
224 | vlan_group_free(grp); | |
225 | return NULL; | |
226 | } | |
227 | ||
1da177e4 LT |
228 | static void vlan_rcu_free(struct rcu_head *rcu) |
229 | { | |
5c15bdec | 230 | vlan_group_free(container_of(rcu, struct vlan_group, rcu)); |
1da177e4 LT |
231 | } |
232 | ||
233 | ||
234 | /* This returns 0 if everything went fine. | |
235 | * It will return 1 if the group was killed as a result. | |
236 | * A negative return indicates failure. | |
237 | * | |
238 | * The RTNL lock must be held. | |
239 | */ | |
240 | static int unregister_vlan_dev(struct net_device *real_dev, | |
241 | unsigned short vlan_id) | |
242 | { | |
243 | struct net_device *dev = NULL; | |
244 | int real_dev_ifindex = real_dev->ifindex; | |
245 | struct vlan_group *grp; | |
246 | int i, ret; | |
247 | ||
248 | #ifdef VLAN_DEBUG | |
249 | printk(VLAN_DBG "%s: VID: %i\n", __FUNCTION__, vlan_id); | |
250 | #endif | |
251 | ||
252 | /* sanity check */ | |
253 | if (vlan_id >= VLAN_VID_MASK) | |
254 | return -EINVAL; | |
255 | ||
256 | ASSERT_RTNL(); | |
257 | grp = __vlan_find_group(real_dev_ifindex); | |
258 | ||
259 | ret = 0; | |
260 | ||
261 | if (grp) { | |
5c15bdec | 262 | dev = vlan_group_get_device(grp, vlan_id); |
1da177e4 LT |
263 | if (dev) { |
264 | /* Remove proc entry */ | |
265 | vlan_proc_rem_dev(dev); | |
266 | ||
267 | /* Take it out of our own structures, but be sure to | |
268 | * interlock with HW accelerating devices or SW vlan | |
269 | * input packet processing. | |
270 | */ | |
d2d1acdb | 271 | if (real_dev->features & NETIF_F_HW_VLAN_FILTER) |
1da177e4 | 272 | real_dev->vlan_rx_kill_vid(real_dev, vlan_id); |
1da177e4 | 273 | |
5c15bdec | 274 | vlan_group_set_device(grp, vlan_id, NULL); |
1da177e4 LT |
275 | synchronize_net(); |
276 | ||
277 | ||
278 | /* Caller unregisters (and if necessary, puts) | |
279 | * VLAN device, but we get rid of the reference to | |
280 | * real_dev here. | |
281 | */ | |
282 | dev_put(real_dev); | |
283 | ||
284 | /* If the group is now empty, kill off the | |
285 | * group. | |
286 | */ | |
287 | for (i = 0; i < VLAN_VID_MASK; i++) | |
5c15bdec | 288 | if (vlan_group_get_device(grp, i)) |
1da177e4 LT |
289 | break; |
290 | ||
291 | if (i == VLAN_VID_MASK) { | |
292 | if (real_dev->features & NETIF_F_HW_VLAN_RX) | |
293 | real_dev->vlan_rx_register(real_dev, NULL); | |
294 | ||
295 | hlist_del_rcu(&grp->hlist); | |
296 | ||
297 | /* Free the group, after all cpu's are done. */ | |
298 | call_rcu(&grp->rcu, vlan_rcu_free); | |
299 | ||
300 | grp = NULL; | |
301 | ret = 1; | |
302 | } | |
303 | } | |
304 | } | |
305 | ||
122952fc | 306 | return ret; |
1da177e4 LT |
307 | } |
308 | ||
c17d8874 | 309 | static int unregister_vlan_device(struct net_device *dev) |
1da177e4 | 310 | { |
1da177e4 LT |
311 | int ret; |
312 | ||
c17d8874 PM |
313 | ret = unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev, |
314 | VLAN_DEV_INFO(dev)->vlan_id); | |
315 | unregister_netdevice(dev); | |
1da177e4 | 316 | |
c17d8874 PM |
317 | if (ret == 1) |
318 | ret = 0; | |
1da177e4 LT |
319 | return ret; |
320 | } | |
321 | ||
2f4284a4 PM |
322 | /* |
323 | * vlan network devices have devices nesting below it, and are a special | |
324 | * "super class" of normal network devices; split their locks off into a | |
325 | * separate class since they always nest. | |
326 | */ | |
327 | static struct lock_class_key vlan_netdev_xmit_lock_key; | |
328 | ||
329 | static int vlan_dev_init(struct net_device *dev) | |
330 | { | |
331 | struct net_device *real_dev = VLAN_DEV_INFO(dev)->real_dev; | |
332 | ||
333 | /* IFF_BROADCAST|IFF_MULTICAST; ??? */ | |
334 | dev->flags = real_dev->flags & ~IFF_UP; | |
335 | dev->iflink = real_dev->ifindex; | |
336 | dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) | | |
337 | (1<<__LINK_STATE_DORMANT))) | | |
338 | (1<<__LINK_STATE_PRESENT); | |
339 | ||
340 | /* TODO: maybe just assign it to be ETHERNET? */ | |
341 | dev->type = real_dev->type; | |
342 | ||
343 | memcpy(dev->broadcast, real_dev->broadcast, real_dev->addr_len); | |
344 | memcpy(dev->dev_addr, real_dev->dev_addr, real_dev->addr_len); | |
345 | dev->addr_len = real_dev->addr_len; | |
346 | ||
347 | if (real_dev->features & NETIF_F_HW_VLAN_TX) { | |
348 | dev->hard_header = real_dev->hard_header; | |
349 | dev->hard_header_len = real_dev->hard_header_len; | |
350 | dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit; | |
351 | dev->rebuild_header = real_dev->rebuild_header; | |
352 | } else { | |
353 | dev->hard_header = vlan_dev_hard_header; | |
354 | dev->hard_header_len = real_dev->hard_header_len + VLAN_HLEN; | |
355 | dev->hard_start_xmit = vlan_dev_hard_start_xmit; | |
356 | dev->rebuild_header = vlan_dev_rebuild_header; | |
357 | } | |
358 | dev->hard_header_parse = real_dev->hard_header_parse; | |
359 | ||
360 | lockdep_set_class(&dev->_xmit_lock, &vlan_netdev_xmit_lock_key); | |
361 | return 0; | |
362 | } | |
363 | ||
1da177e4 LT |
364 | static void vlan_setup(struct net_device *new_dev) |
365 | { | |
366 | SET_MODULE_OWNER(new_dev); | |
122952fc | 367 | |
1da177e4 LT |
368 | /* new_dev->ifindex = 0; it will be set when added to |
369 | * the global list. | |
370 | * iflink is set as well. | |
371 | */ | |
372 | new_dev->get_stats = vlan_dev_get_stats; | |
373 | ||
374 | /* Make this thing known as a VLAN device */ | |
375 | new_dev->priv_flags |= IFF_802_1Q_VLAN; | |
122952fc | 376 | |
1da177e4 LT |
377 | /* Set us up to have no queue, as the underlying Hardware device |
378 | * can do all the queueing we could want. | |
379 | */ | |
380 | new_dev->tx_queue_len = 0; | |
381 | ||
382 | /* set up method calls */ | |
383 | new_dev->change_mtu = vlan_dev_change_mtu; | |
2f4284a4 | 384 | new_dev->init = vlan_dev_init; |
1da177e4 LT |
385 | new_dev->open = vlan_dev_open; |
386 | new_dev->stop = vlan_dev_stop; | |
387 | new_dev->set_mac_address = vlan_dev_set_mac_address; | |
388 | new_dev->set_multicast_list = vlan_dev_set_multicast_list; | |
389 | new_dev->destructor = free_netdev; | |
390 | new_dev->do_ioctl = vlan_dev_ioctl; | |
391 | } | |
392 | ||
ddd7bf9f SR |
393 | static void vlan_transfer_operstate(const struct net_device *dev, struct net_device *vlandev) |
394 | { | |
395 | /* Have to respect userspace enforced dormant state | |
396 | * of real device, also must allow supplicant running | |
397 | * on VLAN device | |
398 | */ | |
399 | if (dev->operstate == IF_OPER_DORMANT) | |
400 | netif_dormant_on(vlandev); | |
401 | else | |
402 | netif_dormant_off(vlandev); | |
403 | ||
404 | if (netif_carrier_ok(dev)) { | |
405 | if (!netif_carrier_ok(vlandev)) | |
406 | netif_carrier_on(vlandev); | |
407 | } else { | |
408 | if (netif_carrier_ok(vlandev)) | |
409 | netif_carrier_off(vlandev); | |
410 | } | |
411 | } | |
412 | ||
c1d3ee99 | 413 | static int vlan_check_real_dev(struct net_device *real_dev, unsigned short vlan_id) |
1da177e4 | 414 | { |
1da177e4 LT |
415 | if (real_dev->features & NETIF_F_VLAN_CHALLENGED) { |
416 | printk(VLAN_DBG "%s: VLANs not supported on %s.\n", | |
417 | __FUNCTION__, real_dev->name); | |
c1d3ee99 | 418 | return -EOPNOTSUPP; |
1da177e4 LT |
419 | } |
420 | ||
421 | if ((real_dev->features & NETIF_F_HW_VLAN_RX) && | |
d2d1acdb | 422 | !real_dev->vlan_rx_register) { |
1da177e4 LT |
423 | printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n", |
424 | __FUNCTION__, real_dev->name); | |
c1d3ee99 | 425 | return -EOPNOTSUPP; |
1da177e4 LT |
426 | } |
427 | ||
428 | if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) && | |
d2d1acdb | 429 | (!real_dev->vlan_rx_add_vid || !real_dev->vlan_rx_kill_vid)) { |
1da177e4 LT |
430 | printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n", |
431 | __FUNCTION__, real_dev->name); | |
c1d3ee99 | 432 | return -EOPNOTSUPP; |
1da177e4 LT |
433 | } |
434 | ||
1da177e4 LT |
435 | /* The real device must be up and operating in order to |
436 | * assosciate a VLAN device with it. | |
437 | */ | |
438 | if (!(real_dev->flags & IFF_UP)) | |
c1d3ee99 | 439 | return -ENETDOWN; |
1da177e4 | 440 | |
c1d3ee99 | 441 | if (__find_vlan_dev(real_dev, vlan_id) != NULL) { |
1da177e4 LT |
442 | /* was already registered. */ |
443 | printk(VLAN_DBG "%s: ALREADY had VLAN registered\n", __FUNCTION__); | |
c1d3ee99 | 444 | return -EEXIST; |
1da177e4 LT |
445 | } |
446 | ||
c1d3ee99 PM |
447 | return 0; |
448 | } | |
449 | ||
e89fe42c PM |
450 | static int register_vlan_dev(struct net_device *dev) |
451 | { | |
452 | struct vlan_dev_info *vlan = VLAN_DEV_INFO(dev); | |
453 | struct net_device *real_dev = vlan->real_dev; | |
454 | unsigned short vlan_id = vlan->vlan_id; | |
455 | struct vlan_group *grp, *ngrp = NULL; | |
456 | int err; | |
457 | ||
458 | grp = __vlan_find_group(real_dev->ifindex); | |
459 | if (!grp) { | |
460 | ngrp = grp = vlan_group_alloc(real_dev->ifindex); | |
461 | if (!grp) | |
462 | return -ENOBUFS; | |
463 | } | |
464 | ||
465 | err = register_netdevice(dev); | |
466 | if (err < 0) | |
467 | goto out_free_group; | |
468 | ||
469 | /* Account for reference in struct vlan_dev_info */ | |
470 | dev_hold(real_dev); | |
471 | ||
472 | vlan_transfer_operstate(real_dev, dev); | |
473 | linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */ | |
474 | ||
475 | /* So, got the sucker initialized, now lets place | |
476 | * it into our local structure. | |
477 | */ | |
478 | vlan_group_set_device(grp, vlan_id, dev); | |
479 | if (ngrp && real_dev->features & NETIF_F_HW_VLAN_RX) | |
480 | real_dev->vlan_rx_register(real_dev, ngrp); | |
481 | if (real_dev->features & NETIF_F_HW_VLAN_FILTER) | |
482 | real_dev->vlan_rx_add_vid(real_dev, vlan_id); | |
483 | ||
484 | if (vlan_proc_add_dev(dev) < 0) | |
485 | printk(KERN_WARNING "VLAN: failed to add proc entry for %s\n", | |
486 | dev->name); | |
487 | return 0; | |
488 | ||
489 | out_free_group: | |
490 | if (ngrp) | |
491 | vlan_group_free(ngrp); | |
492 | return err; | |
493 | } | |
494 | ||
c1d3ee99 PM |
495 | /* Attach a VLAN device to a mac address (ie Ethernet Card). |
496 | * Returns the device that was created, or NULL if there was | |
497 | * an error of some kind. | |
498 | */ | |
499 | static struct net_device *register_vlan_device(struct net_device *real_dev, | |
500 | unsigned short VLAN_ID) | |
501 | { | |
c1d3ee99 PM |
502 | struct net_device *new_dev; |
503 | char name[IFNAMSIZ]; | |
504 | ||
505 | #ifdef VLAN_DEBUG | |
506 | printk(VLAN_DBG "%s: if_name -:%s:- vid: %i\n", | |
507 | __FUNCTION__, eth_IF_name, VLAN_ID); | |
508 | #endif | |
509 | ||
510 | if (VLAN_ID >= VLAN_VID_MASK) | |
511 | goto out_ret_null; | |
512 | ||
513 | if (vlan_check_real_dev(real_dev, VLAN_ID) < 0) | |
514 | goto out_ret_null; | |
515 | ||
1da177e4 LT |
516 | /* Gotta set up the fields for the device. */ |
517 | #ifdef VLAN_DEBUG | |
518 | printk(VLAN_DBG "About to allocate name, vlan_name_type: %i\n", | |
519 | vlan_name_type); | |
520 | #endif | |
521 | switch (vlan_name_type) { | |
522 | case VLAN_NAME_TYPE_RAW_PLUS_VID: | |
523 | /* name will look like: eth1.0005 */ | |
524 | snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, VLAN_ID); | |
525 | break; | |
526 | case VLAN_NAME_TYPE_PLUS_VID_NO_PAD: | |
527 | /* Put our vlan.VID in the name. | |
528 | * Name will look like: vlan5 | |
529 | */ | |
530 | snprintf(name, IFNAMSIZ, "vlan%i", VLAN_ID); | |
531 | break; | |
532 | case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD: | |
533 | /* Put our vlan.VID in the name. | |
534 | * Name will look like: eth0.5 | |
535 | */ | |
536 | snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, VLAN_ID); | |
537 | break; | |
538 | case VLAN_NAME_TYPE_PLUS_VID: | |
539 | /* Put our vlan.VID in the name. | |
540 | * Name will look like: vlan0005 | |
541 | */ | |
542 | default: | |
543 | snprintf(name, IFNAMSIZ, "vlan%.4i", VLAN_ID); | |
3ff50b79 | 544 | } |
122952fc | 545 | |
1da177e4 LT |
546 | new_dev = alloc_netdev(sizeof(struct vlan_dev_info), name, |
547 | vlan_setup); | |
5dd8d1e9 | 548 | |
1da177e4 | 549 | if (new_dev == NULL) |
c17d8874 | 550 | goto out_ret_null; |
1da177e4 | 551 | |
1da177e4 LT |
552 | /* need 4 bytes for extra VLAN header info, |
553 | * hope the underlying device can handle it. | |
554 | */ | |
555 | new_dev->mtu = real_dev->mtu; | |
556 | ||
2f4284a4 PM |
557 | #ifdef VLAN_DEBUG |
558 | printk(VLAN_DBG "Allocated new name -:%s:-\n", new_dev->name); | |
1da177e4 LT |
559 | VLAN_MEM_DBG("new_dev->priv malloc, addr: %p size: %i\n", |
560 | new_dev->priv, | |
561 | sizeof(struct vlan_dev_info)); | |
2f4284a4 | 562 | #endif |
1da177e4 LT |
563 | |
564 | VLAN_DEV_INFO(new_dev)->vlan_id = VLAN_ID; /* 1 through VLAN_VID_MASK */ | |
565 | VLAN_DEV_INFO(new_dev)->real_dev = real_dev; | |
566 | VLAN_DEV_INFO(new_dev)->dent = NULL; | |
567 | VLAN_DEV_INFO(new_dev)->flags = 1; | |
568 | ||
e89fe42c PM |
569 | if (register_vlan_dev(new_dev) < 0) |
570 | goto out_free_newdev; | |
1da177e4 | 571 | |
c17d8874 PM |
572 | /* Account for reference in struct vlan_dev_info */ |
573 | dev_hold(real_dev); | |
1da177e4 LT |
574 | #ifdef VLAN_DEBUG |
575 | printk(VLAN_DBG "Allocated new device successfully, returning.\n"); | |
576 | #endif | |
577 | return new_dev; | |
578 | ||
1da177e4 LT |
579 | out_free_newdev: |
580 | free_netdev(new_dev); | |
581 | ||
1da177e4 LT |
582 | out_ret_null: |
583 | return NULL; | |
584 | } | |
585 | ||
586 | static int vlan_device_event(struct notifier_block *unused, unsigned long event, void *ptr) | |
587 | { | |
588 | struct net_device *dev = ptr; | |
589 | struct vlan_group *grp = __vlan_find_group(dev->ifindex); | |
590 | int i, flgs; | |
591 | struct net_device *vlandev; | |
592 | ||
593 | if (!grp) | |
594 | goto out; | |
595 | ||
596 | /* It is OK that we do not hold the group lock right now, | |
597 | * as we run under the RTNL lock. | |
598 | */ | |
599 | ||
600 | switch (event) { | |
601 | case NETDEV_CHANGE: | |
602 | /* Propagate real device state to vlan devices */ | |
1da177e4 | 603 | for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) { |
5c15bdec | 604 | vlandev = vlan_group_get_device(grp, i); |
1da177e4 LT |
605 | if (!vlandev) |
606 | continue; | |
607 | ||
ddd7bf9f | 608 | vlan_transfer_operstate(dev, vlandev); |
1da177e4 LT |
609 | } |
610 | break; | |
611 | ||
612 | case NETDEV_DOWN: | |
613 | /* Put all VLANs for this dev in the down state too. */ | |
614 | for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) { | |
5c15bdec | 615 | vlandev = vlan_group_get_device(grp, i); |
1da177e4 LT |
616 | if (!vlandev) |
617 | continue; | |
618 | ||
619 | flgs = vlandev->flags; | |
620 | if (!(flgs & IFF_UP)) | |
621 | continue; | |
622 | ||
623 | dev_change_flags(vlandev, flgs & ~IFF_UP); | |
624 | } | |
625 | break; | |
626 | ||
627 | case NETDEV_UP: | |
628 | /* Put all VLANs for this dev in the up state too. */ | |
629 | for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) { | |
5c15bdec | 630 | vlandev = vlan_group_get_device(grp, i); |
1da177e4 LT |
631 | if (!vlandev) |
632 | continue; | |
122952fc | 633 | |
1da177e4 LT |
634 | flgs = vlandev->flags; |
635 | if (flgs & IFF_UP) | |
636 | continue; | |
637 | ||
638 | dev_change_flags(vlandev, flgs | IFF_UP); | |
639 | } | |
640 | break; | |
122952fc | 641 | |
1da177e4 LT |
642 | case NETDEV_UNREGISTER: |
643 | /* Delete all VLANs for this dev. */ | |
644 | for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) { | |
645 | int ret; | |
646 | ||
5c15bdec | 647 | vlandev = vlan_group_get_device(grp, i); |
1da177e4 LT |
648 | if (!vlandev) |
649 | continue; | |
650 | ||
651 | ret = unregister_vlan_dev(dev, | |
652 | VLAN_DEV_INFO(vlandev)->vlan_id); | |
653 | ||
654 | unregister_netdevice(vlandev); | |
655 | ||
656 | /* Group was destroyed? */ | |
657 | if (ret == 1) | |
658 | break; | |
659 | } | |
660 | break; | |
3ff50b79 | 661 | } |
1da177e4 LT |
662 | |
663 | out: | |
664 | return NOTIFY_DONE; | |
665 | } | |
666 | ||
667 | /* | |
668 | * VLAN IOCTL handler. | |
669 | * o execute requested action or pass command to the device driver | |
670 | * arg is really a struct vlan_ioctl_args __user *. | |
671 | */ | |
672 | static int vlan_ioctl_handler(void __user *arg) | |
673 | { | |
c17d8874 | 674 | int err; |
1da177e4 LT |
675 | unsigned short vid = 0; |
676 | struct vlan_ioctl_args args; | |
c17d8874 | 677 | struct net_device *dev = NULL; |
1da177e4 LT |
678 | |
679 | if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args))) | |
680 | return -EFAULT; | |
681 | ||
682 | /* Null terminate this sucker, just in case. */ | |
683 | args.device1[23] = 0; | |
684 | args.u.device2[23] = 0; | |
685 | ||
686 | #ifdef VLAN_DEBUG | |
687 | printk(VLAN_DBG "%s: args.cmd: %x\n", __FUNCTION__, args.cmd); | |
688 | #endif | |
689 | ||
c17d8874 PM |
690 | rtnl_lock(); |
691 | ||
1da177e4 LT |
692 | switch (args.cmd) { |
693 | case SET_VLAN_INGRESS_PRIORITY_CMD: | |
c17d8874 PM |
694 | case SET_VLAN_EGRESS_PRIORITY_CMD: |
695 | case SET_VLAN_FLAG_CMD: | |
696 | case ADD_VLAN_CMD: | |
697 | case DEL_VLAN_CMD: | |
698 | case GET_VLAN_REALDEV_NAME_CMD: | |
699 | case GET_VLAN_VID_CMD: | |
700 | err = -ENODEV; | |
701 | dev = __dev_get_by_name(args.device1); | |
702 | if (!dev) | |
703 | goto out; | |
704 | ||
705 | err = -EINVAL; | |
706 | if (args.cmd != ADD_VLAN_CMD && | |
707 | !(dev->priv_flags & IFF_802_1Q_VLAN)) | |
708 | goto out; | |
709 | } | |
710 | ||
711 | switch (args.cmd) { | |
712 | case SET_VLAN_INGRESS_PRIORITY_CMD: | |
713 | err = -EPERM; | |
1da177e4 | 714 | if (!capable(CAP_NET_ADMIN)) |
c17d8874 PM |
715 | break; |
716 | vlan_dev_set_ingress_priority(dev, | |
717 | args.u.skb_priority, | |
718 | args.vlan_qos); | |
1da177e4 LT |
719 | break; |
720 | ||
721 | case SET_VLAN_EGRESS_PRIORITY_CMD: | |
c17d8874 | 722 | err = -EPERM; |
1da177e4 | 723 | if (!capable(CAP_NET_ADMIN)) |
c17d8874 PM |
724 | break; |
725 | err = vlan_dev_set_egress_priority(dev, | |
1da177e4 LT |
726 | args.u.skb_priority, |
727 | args.vlan_qos); | |
728 | break; | |
729 | ||
730 | case SET_VLAN_FLAG_CMD: | |
c17d8874 | 731 | err = -EPERM; |
1da177e4 | 732 | if (!capable(CAP_NET_ADMIN)) |
c17d8874 PM |
733 | break; |
734 | err = vlan_dev_set_vlan_flag(dev, | |
1da177e4 LT |
735 | args.u.flag, |
736 | args.vlan_qos); | |
737 | break; | |
738 | ||
739 | case SET_VLAN_NAME_TYPE_CMD: | |
c17d8874 | 740 | err = -EPERM; |
1da177e4 LT |
741 | if (!capable(CAP_NET_ADMIN)) |
742 | return -EPERM; | |
c17d8874 PM |
743 | if ((args.u.name_type >= 0) && |
744 | (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) { | |
1da177e4 LT |
745 | vlan_name_type = args.u.name_type; |
746 | err = 0; | |
747 | } else { | |
748 | err = -EINVAL; | |
749 | } | |
750 | break; | |
751 | ||
752 | case ADD_VLAN_CMD: | |
c17d8874 | 753 | err = -EPERM; |
1da177e4 | 754 | if (!capable(CAP_NET_ADMIN)) |
c17d8874 PM |
755 | break; |
756 | if (register_vlan_device(dev, args.u.VID)) { | |
1da177e4 LT |
757 | err = 0; |
758 | } else { | |
759 | err = -EINVAL; | |
760 | } | |
761 | break; | |
762 | ||
763 | case DEL_VLAN_CMD: | |
c17d8874 | 764 | err = -EPERM; |
1da177e4 | 765 | if (!capable(CAP_NET_ADMIN)) |
c17d8874 PM |
766 | break; |
767 | err = unregister_vlan_device(dev); | |
1da177e4 LT |
768 | break; |
769 | ||
770 | case GET_VLAN_INGRESS_PRIORITY_CMD: | |
771 | /* TODO: Implement | |
772 | err = vlan_dev_get_ingress_priority(args); | |
773 | if (copy_to_user((void*)arg, &args, | |
122952fc YH |
774 | sizeof(struct vlan_ioctl_args))) { |
775 | err = -EFAULT; | |
1da177e4 LT |
776 | } |
777 | */ | |
778 | err = -EINVAL; | |
779 | break; | |
780 | case GET_VLAN_EGRESS_PRIORITY_CMD: | |
781 | /* TODO: Implement | |
782 | err = vlan_dev_get_egress_priority(args.device1, &(args.args); | |
783 | if (copy_to_user((void*)arg, &args, | |
122952fc YH |
784 | sizeof(struct vlan_ioctl_args))) { |
785 | err = -EFAULT; | |
1da177e4 LT |
786 | } |
787 | */ | |
788 | err = -EINVAL; | |
789 | break; | |
790 | case GET_VLAN_REALDEV_NAME_CMD: | |
c17d8874 | 791 | vlan_dev_get_realdev_name(dev, args.u.device2); |
1da177e4 LT |
792 | if (copy_to_user(arg, &args, |
793 | sizeof(struct vlan_ioctl_args))) { | |
794 | err = -EFAULT; | |
795 | } | |
796 | break; | |
797 | ||
798 | case GET_VLAN_VID_CMD: | |
c17d8874 | 799 | vlan_dev_get_vid(dev, &vid); |
1da177e4 LT |
800 | args.u.VID = vid; |
801 | if (copy_to_user(arg, &args, | |
802 | sizeof(struct vlan_ioctl_args))) { | |
122952fc | 803 | err = -EFAULT; |
1da177e4 LT |
804 | } |
805 | break; | |
806 | ||
807 | default: | |
808 | /* pass on to underlying device instead?? */ | |
809 | printk(VLAN_DBG "%s: Unknown VLAN CMD: %x \n", | |
810 | __FUNCTION__, args.cmd); | |
c17d8874 PM |
811 | err = -EINVAL; |
812 | break; | |
3ff50b79 | 813 | } |
7eb1b3d3 | 814 | out: |
c17d8874 | 815 | rtnl_unlock(); |
1da177e4 LT |
816 | return err; |
817 | } | |
818 | ||
819 | MODULE_LICENSE("GPL"); | |
820 | MODULE_VERSION(DRV_VERSION); |