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