Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2007 Patrick McHardy <kaber@trash.net> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation; either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * The code this is based on carried the following copyright notice: |
| 10 | * --- |
| 11 | * (C) Copyright 2001-2006 |
| 12 | * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com |
| 13 | * Re-worked by Ben Greear <greearb@candelatech.com> |
| 14 | * --- |
| 15 | */ |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/string.h> |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 23 | #include <linux/rculist.h> |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 24 | #include <linux/notifier.h> |
| 25 | #include <linux/netdevice.h> |
| 26 | #include <linux/etherdevice.h> |
| 27 | #include <linux/ethtool.h> |
| 28 | #include <linux/if_arp.h> |
| 29 | #include <linux/if_link.h> |
| 30 | #include <linux/if_macvlan.h> |
| 31 | #include <net/rtnetlink.h> |
| 32 | |
| 33 | #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE) |
| 34 | |
| 35 | struct macvlan_port { |
| 36 | struct net_device *dev; |
| 37 | struct hlist_head vlan_hash[MACVLAN_HASH_SIZE]; |
| 38 | struct list_head vlans; |
| 39 | }; |
| 40 | |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 41 | /** |
| 42 | * struct macvlan_rx_stats - MACVLAN percpu rx stats |
| 43 | * @rx_packets: number of received packets |
| 44 | * @rx_bytes: number of received bytes |
| 45 | * @multicast: number of received multicast packets |
| 46 | * @rx_errors: number of errors |
| 47 | */ |
| 48 | struct macvlan_rx_stats { |
| 49 | unsigned long rx_packets; |
| 50 | unsigned long rx_bytes; |
| 51 | unsigned long multicast; |
| 52 | unsigned long rx_errors; |
| 53 | }; |
| 54 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 55 | struct macvlan_dev { |
| 56 | struct net_device *dev; |
| 57 | struct list_head list; |
| 58 | struct hlist_node hlist; |
| 59 | struct macvlan_port *port; |
| 60 | struct net_device *lowerdev; |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 61 | struct macvlan_rx_stats *rx_stats; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | |
| 65 | static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port, |
| 66 | const unsigned char *addr) |
| 67 | { |
| 68 | struct macvlan_dev *vlan; |
| 69 | struct hlist_node *n; |
| 70 | |
| 71 | hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) { |
Eric Dumazet | ac06713 | 2009-09-01 05:46:05 +0000 | [diff] [blame] | 72 | if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr)) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 73 | return vlan; |
| 74 | } |
| 75 | return NULL; |
| 76 | } |
| 77 | |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 78 | static void macvlan_hash_add(struct macvlan_dev *vlan) |
| 79 | { |
| 80 | struct macvlan_port *port = vlan->port; |
| 81 | const unsigned char *addr = vlan->dev->dev_addr; |
| 82 | |
| 83 | hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]); |
| 84 | } |
| 85 | |
| 86 | static void macvlan_hash_del(struct macvlan_dev *vlan) |
| 87 | { |
| 88 | hlist_del_rcu(&vlan->hlist); |
| 89 | synchronize_rcu(); |
| 90 | } |
| 91 | |
| 92 | static void macvlan_hash_change_addr(struct macvlan_dev *vlan, |
| 93 | const unsigned char *addr) |
| 94 | { |
| 95 | macvlan_hash_del(vlan); |
| 96 | /* Now that we are unhashed it is safe to change the device |
| 97 | * address without confusing packet delivery. |
| 98 | */ |
| 99 | memcpy(vlan->dev->dev_addr, addr, ETH_ALEN); |
| 100 | macvlan_hash_add(vlan); |
| 101 | } |
| 102 | |
| 103 | static int macvlan_addr_busy(const struct macvlan_port *port, |
| 104 | const unsigned char *addr) |
| 105 | { |
| 106 | /* Test to see if the specified multicast address is |
| 107 | * currently in use by the underlying device or |
| 108 | * another macvlan. |
| 109 | */ |
Eric Dumazet | ac06713 | 2009-09-01 05:46:05 +0000 | [diff] [blame] | 110 | if (!compare_ether_addr_64bits(port->dev->dev_addr, addr)) |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 111 | return 1; |
| 112 | |
| 113 | if (macvlan_hash_lookup(port, addr)) |
| 114 | return 1; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 119 | static void macvlan_broadcast(struct sk_buff *skb, |
| 120 | const struct macvlan_port *port) |
| 121 | { |
| 122 | const struct ethhdr *eth = eth_hdr(skb); |
| 123 | const struct macvlan_dev *vlan; |
| 124 | struct hlist_node *n; |
| 125 | struct net_device *dev; |
| 126 | struct sk_buff *nskb; |
| 127 | unsigned int i; |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 128 | struct macvlan_rx_stats *rx_stats; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 129 | |
Patrick McHardy | efbbced | 2008-11-26 15:30:48 -0800 | [diff] [blame] | 130 | if (skb->protocol == htons(ETH_P_PAUSE)) |
| 131 | return; |
| 132 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 133 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) { |
| 134 | hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) { |
| 135 | dev = vlan->dev; |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 136 | rx_stats = per_cpu_ptr(vlan->rx_stats, smp_processor_id()); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 137 | |
| 138 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 139 | if (nskb == NULL) { |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 140 | rx_stats->rx_errors++; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 141 | continue; |
| 142 | } |
| 143 | |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 144 | rx_stats->rx_bytes += skb->len + ETH_HLEN; |
| 145 | rx_stats->rx_packets++; |
| 146 | rx_stats->multicast++; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 147 | |
| 148 | nskb->dev = dev; |
Eric Dumazet | ac06713 | 2009-09-01 05:46:05 +0000 | [diff] [blame] | 149 | if (!compare_ether_addr_64bits(eth->h_dest, dev->broadcast)) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 150 | nskb->pkt_type = PACKET_BROADCAST; |
| 151 | else |
| 152 | nskb->pkt_type = PACKET_MULTICAST; |
| 153 | |
| 154 | netif_rx(nskb); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /* called under rcu_read_lock() from netif_receive_skb */ |
| 160 | static struct sk_buff *macvlan_handle_frame(struct sk_buff *skb) |
| 161 | { |
| 162 | const struct ethhdr *eth = eth_hdr(skb); |
| 163 | const struct macvlan_port *port; |
| 164 | const struct macvlan_dev *vlan; |
| 165 | struct net_device *dev; |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 166 | struct macvlan_rx_stats *rx_stats; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 167 | |
| 168 | port = rcu_dereference(skb->dev->macvlan_port); |
| 169 | if (port == NULL) |
| 170 | return skb; |
| 171 | |
| 172 | if (is_multicast_ether_addr(eth->h_dest)) { |
| 173 | macvlan_broadcast(skb, port); |
| 174 | return skb; |
| 175 | } |
| 176 | |
| 177 | vlan = macvlan_hash_lookup(port, eth->h_dest); |
| 178 | if (vlan == NULL) |
| 179 | return skb; |
| 180 | |
| 181 | dev = vlan->dev; |
| 182 | if (unlikely(!(dev->flags & IFF_UP))) { |
| 183 | kfree_skb(skb); |
| 184 | return NULL; |
| 185 | } |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 186 | rx_stats = per_cpu_ptr(vlan->rx_stats, smp_processor_id()); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 187 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 188 | if (skb == NULL) { |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 189 | rx_stats->rx_errors++; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 190 | return NULL; |
| 191 | } |
| 192 | |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 193 | rx_stats->rx_bytes += skb->len + ETH_HLEN; |
| 194 | rx_stats->rx_packets++; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 195 | |
| 196 | skb->dev = dev; |
| 197 | skb->pkt_type = PACKET_HOST; |
| 198 | |
| 199 | netif_rx(skb); |
| 200 | return NULL; |
| 201 | } |
| 202 | |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 203 | static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, |
| 204 | struct net_device *dev) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 205 | { |
Eric Dumazet | 2c11455 | 2009-09-03 00:11:45 +0000 | [diff] [blame] | 206 | int i = skb_get_queue_mapping(skb); |
| 207 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 208 | const struct macvlan_dev *vlan = netdev_priv(dev); |
| 209 | unsigned int len = skb->len; |
| 210 | int ret; |
| 211 | |
| 212 | skb->dev = vlan->lowerdev; |
| 213 | ret = dev_queue_xmit(skb); |
| 214 | |
| 215 | if (likely(ret == NET_XMIT_SUCCESS)) { |
Eric Dumazet | 2c11455 | 2009-09-03 00:11:45 +0000 | [diff] [blame] | 216 | txq->tx_packets++; |
| 217 | txq->tx_bytes += len; |
| 218 | } else |
| 219 | txq->tx_dropped++; |
| 220 | |
Patrick McHardy | cbbef5e | 2009-11-10 06:14:24 +0000 | [diff] [blame] | 221 | return ret; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev, |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 225 | unsigned short type, const void *daddr, |
| 226 | const void *saddr, unsigned len) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 227 | { |
| 228 | const struct macvlan_dev *vlan = netdev_priv(dev); |
| 229 | struct net_device *lowerdev = vlan->lowerdev; |
| 230 | |
Stephen Hemminger | 0c4e858 | 2007-10-09 01:36:32 -0700 | [diff] [blame] | 231 | return dev_hard_header(skb, lowerdev, type, daddr, |
| 232 | saddr ? : dev->dev_addr, len); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 235 | static const struct header_ops macvlan_hard_header_ops = { |
| 236 | .create = macvlan_hard_header, |
| 237 | .rebuild = eth_rebuild_header, |
| 238 | .parse = eth_header_parse, |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 239 | .cache = eth_header_cache, |
| 240 | .cache_update = eth_header_cache_update, |
| 241 | }; |
| 242 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 243 | static int macvlan_open(struct net_device *dev) |
| 244 | { |
| 245 | struct macvlan_dev *vlan = netdev_priv(dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 246 | struct net_device *lowerdev = vlan->lowerdev; |
| 247 | int err; |
| 248 | |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 249 | err = -EBUSY; |
| 250 | if (macvlan_addr_busy(vlan->port, dev->dev_addr)) |
| 251 | goto out; |
| 252 | |
Jiri Pirko | ccffad25 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 253 | err = dev_unicast_add(lowerdev, dev->dev_addr); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 254 | if (err < 0) |
Wang Chen | b89fb7d | 2008-07-14 20:57:07 -0700 | [diff] [blame] | 255 | goto out; |
| 256 | if (dev->flags & IFF_ALLMULTI) { |
| 257 | err = dev_set_allmulti(lowerdev, 1); |
| 258 | if (err < 0) |
| 259 | goto del_unicast; |
| 260 | } |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 261 | macvlan_hash_add(vlan); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 262 | return 0; |
Wang Chen | b89fb7d | 2008-07-14 20:57:07 -0700 | [diff] [blame] | 263 | |
| 264 | del_unicast: |
Jiri Pirko | ccffad25 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 265 | dev_unicast_delete(lowerdev, dev->dev_addr); |
Wang Chen | b89fb7d | 2008-07-14 20:57:07 -0700 | [diff] [blame] | 266 | out: |
| 267 | return err; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static int macvlan_stop(struct net_device *dev) |
| 271 | { |
| 272 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 273 | struct net_device *lowerdev = vlan->lowerdev; |
| 274 | |
| 275 | dev_mc_unsync(lowerdev, dev); |
| 276 | if (dev->flags & IFF_ALLMULTI) |
| 277 | dev_set_allmulti(lowerdev, -1); |
| 278 | |
Jiri Pirko | ccffad25 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 279 | dev_unicast_delete(lowerdev, dev->dev_addr); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 280 | |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 281 | macvlan_hash_del(vlan); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 285 | static int macvlan_set_mac_address(struct net_device *dev, void *p) |
| 286 | { |
| 287 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 288 | struct net_device *lowerdev = vlan->lowerdev; |
| 289 | struct sockaddr *addr = p; |
| 290 | int err; |
| 291 | |
| 292 | if (!is_valid_ether_addr(addr->sa_data)) |
| 293 | return -EADDRNOTAVAIL; |
| 294 | |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 295 | if (!(dev->flags & IFF_UP)) { |
| 296 | /* Just copy in the new address */ |
| 297 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); |
| 298 | } else { |
| 299 | /* Rehash and update the device filters */ |
| 300 | if (macvlan_addr_busy(vlan->port, addr->sa_data)) |
| 301 | return -EBUSY; |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 302 | |
Jiri Pirko | ccffad25 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 303 | err = dev_unicast_add(lowerdev, addr->sa_data); |
| 304 | if (err) |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 305 | return err; |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 306 | |
Jiri Pirko | ccffad25 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 307 | dev_unicast_delete(lowerdev, dev->dev_addr); |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 308 | |
| 309 | macvlan_hash_change_addr(vlan, addr->sa_data); |
| 310 | } |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 311 | return 0; |
| 312 | } |
| 313 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 314 | static void macvlan_change_rx_flags(struct net_device *dev, int change) |
| 315 | { |
| 316 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 317 | struct net_device *lowerdev = vlan->lowerdev; |
| 318 | |
| 319 | if (change & IFF_ALLMULTI) |
| 320 | dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); |
| 321 | } |
| 322 | |
| 323 | static void macvlan_set_multicast_list(struct net_device *dev) |
| 324 | { |
| 325 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 326 | |
| 327 | dev_mc_sync(vlan->lowerdev, dev); |
| 328 | } |
| 329 | |
| 330 | static int macvlan_change_mtu(struct net_device *dev, int new_mtu) |
| 331 | { |
| 332 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 333 | |
| 334 | if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) |
| 335 | return -EINVAL; |
| 336 | dev->mtu = new_mtu; |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * macvlan network devices have devices nesting below it and are a special |
| 342 | * "super class" of normal network devices; split their locks off into a |
| 343 | * separate class since they always nest. |
| 344 | */ |
| 345 | static struct lock_class_key macvlan_netdev_xmit_lock_key; |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 346 | static struct lock_class_key macvlan_netdev_addr_lock_key; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 347 | |
| 348 | #define MACVLAN_FEATURES \ |
| 349 | (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ |
| 350 | NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ |
| 351 | NETIF_F_TSO_ECN | NETIF_F_TSO6) |
| 352 | |
| 353 | #define MACVLAN_STATE_MASK \ |
| 354 | ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) |
| 355 | |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 356 | static void macvlan_set_lockdep_class_one(struct net_device *dev, |
| 357 | struct netdev_queue *txq, |
| 358 | void *_unused) |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 359 | { |
| 360 | lockdep_set_class(&txq->_xmit_lock, |
| 361 | &macvlan_netdev_xmit_lock_key); |
| 362 | } |
| 363 | |
| 364 | static void macvlan_set_lockdep_class(struct net_device *dev) |
| 365 | { |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 366 | lockdep_set_class(&dev->addr_list_lock, |
| 367 | &macvlan_netdev_addr_lock_key); |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 368 | netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL); |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 371 | static int macvlan_init(struct net_device *dev) |
| 372 | { |
| 373 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 374 | const struct net_device *lowerdev = vlan->lowerdev; |
| 375 | |
| 376 | dev->state = (dev->state & ~MACVLAN_STATE_MASK) | |
| 377 | (lowerdev->state & MACVLAN_STATE_MASK); |
| 378 | dev->features = lowerdev->features & MACVLAN_FEATURES; |
| 379 | dev->iflink = lowerdev->ifindex; |
sg.tweak@gmail.com | ef5c899 | 2009-06-10 09:55:02 +0000 | [diff] [blame] | 380 | dev->hard_header_len = lowerdev->hard_header_len; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 381 | |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 382 | macvlan_set_lockdep_class(dev); |
| 383 | |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 384 | vlan->rx_stats = alloc_percpu(struct macvlan_rx_stats); |
| 385 | if (!vlan->rx_stats) |
| 386 | return -ENOMEM; |
| 387 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 391 | static void macvlan_uninit(struct net_device *dev) |
| 392 | { |
| 393 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 394 | |
| 395 | free_percpu(vlan->rx_stats); |
| 396 | } |
| 397 | |
| 398 | static struct net_device_stats *macvlan_dev_get_stats(struct net_device *dev) |
| 399 | { |
| 400 | struct net_device_stats *stats = &dev->stats; |
| 401 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 402 | |
| 403 | dev_txq_stats_fold(dev, stats); |
| 404 | |
| 405 | if (vlan->rx_stats) { |
| 406 | struct macvlan_rx_stats *p, rx = {0}; |
| 407 | int i; |
| 408 | |
| 409 | for_each_possible_cpu(i) { |
| 410 | p = per_cpu_ptr(vlan->rx_stats, i); |
| 411 | rx.rx_packets += p->rx_packets; |
| 412 | rx.rx_bytes += p->rx_bytes; |
| 413 | rx.rx_errors += p->rx_errors; |
| 414 | rx.multicast += p->multicast; |
| 415 | } |
| 416 | stats->rx_packets = rx.rx_packets; |
| 417 | stats->rx_bytes = rx.rx_bytes; |
| 418 | stats->rx_errors = rx.rx_errors; |
| 419 | stats->rx_dropped = rx.rx_errors; |
| 420 | stats->multicast = rx.multicast; |
| 421 | } |
| 422 | return stats; |
| 423 | } |
| 424 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 425 | static void macvlan_ethtool_get_drvinfo(struct net_device *dev, |
| 426 | struct ethtool_drvinfo *drvinfo) |
| 427 | { |
| 428 | snprintf(drvinfo->driver, 32, "macvlan"); |
| 429 | snprintf(drvinfo->version, 32, "0.1"); |
| 430 | } |
| 431 | |
| 432 | static u32 macvlan_ethtool_get_rx_csum(struct net_device *dev) |
| 433 | { |
| 434 | const struct macvlan_dev *vlan = netdev_priv(dev); |
Patrick McHardy | b1b67dd | 2009-04-20 04:49:28 +0000 | [diff] [blame] | 435 | return dev_ethtool_get_rx_csum(vlan->lowerdev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 438 | static int macvlan_ethtool_get_settings(struct net_device *dev, |
| 439 | struct ethtool_cmd *cmd) |
| 440 | { |
| 441 | const struct macvlan_dev *vlan = netdev_priv(dev); |
Patrick McHardy | b1b67dd | 2009-04-20 04:49:28 +0000 | [diff] [blame] | 442 | return dev_ethtool_get_settings(vlan->lowerdev, cmd); |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static u32 macvlan_ethtool_get_flags(struct net_device *dev) |
| 446 | { |
| 447 | const struct macvlan_dev *vlan = netdev_priv(dev); |
Patrick McHardy | b1b67dd | 2009-04-20 04:49:28 +0000 | [diff] [blame] | 448 | return dev_ethtool_get_flags(vlan->lowerdev); |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 451 | static const struct ethtool_ops macvlan_ethtool_ops = { |
| 452 | .get_link = ethtool_op_get_link, |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 453 | .get_settings = macvlan_ethtool_get_settings, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 454 | .get_rx_csum = macvlan_ethtool_get_rx_csum, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 455 | .get_drvinfo = macvlan_ethtool_get_drvinfo, |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 456 | .get_flags = macvlan_ethtool_get_flags, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 457 | }; |
| 458 | |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 459 | static const struct net_device_ops macvlan_netdev_ops = { |
| 460 | .ndo_init = macvlan_init, |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 461 | .ndo_uninit = macvlan_uninit, |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 462 | .ndo_open = macvlan_open, |
| 463 | .ndo_stop = macvlan_stop, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 464 | .ndo_start_xmit = macvlan_start_xmit, |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 465 | .ndo_change_mtu = macvlan_change_mtu, |
| 466 | .ndo_change_rx_flags = macvlan_change_rx_flags, |
| 467 | .ndo_set_mac_address = macvlan_set_mac_address, |
| 468 | .ndo_set_multicast_list = macvlan_set_multicast_list, |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 469 | .ndo_get_stats = macvlan_dev_get_stats, |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 470 | .ndo_validate_addr = eth_validate_addr, |
| 471 | }; |
| 472 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 473 | static void macvlan_setup(struct net_device *dev) |
| 474 | { |
| 475 | ether_setup(dev); |
| 476 | |
Eric Dumazet | 93f154b | 2009-05-18 22:19:19 -0700 | [diff] [blame] | 477 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 478 | dev->netdev_ops = &macvlan_netdev_ops; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 479 | dev->destructor = free_netdev; |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 480 | dev->header_ops = &macvlan_hard_header_ops, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 481 | dev->ethtool_ops = &macvlan_ethtool_ops; |
| 482 | dev->tx_queue_len = 0; |
| 483 | } |
| 484 | |
| 485 | static int macvlan_port_create(struct net_device *dev) |
| 486 | { |
| 487 | struct macvlan_port *port; |
| 488 | unsigned int i; |
| 489 | |
| 490 | if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) |
| 491 | return -EINVAL; |
| 492 | |
| 493 | port = kzalloc(sizeof(*port), GFP_KERNEL); |
| 494 | if (port == NULL) |
| 495 | return -ENOMEM; |
| 496 | |
| 497 | port->dev = dev; |
| 498 | INIT_LIST_HEAD(&port->vlans); |
| 499 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) |
| 500 | INIT_HLIST_HEAD(&port->vlan_hash[i]); |
| 501 | rcu_assign_pointer(dev->macvlan_port, port); |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static void macvlan_port_destroy(struct net_device *dev) |
| 506 | { |
| 507 | struct macvlan_port *port = dev->macvlan_port; |
| 508 | |
| 509 | rcu_assign_pointer(dev->macvlan_port, NULL); |
| 510 | synchronize_rcu(); |
| 511 | kfree(port); |
| 512 | } |
| 513 | |
| 514 | static void macvlan_transfer_operstate(struct net_device *dev) |
| 515 | { |
| 516 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 517 | const struct net_device *lowerdev = vlan->lowerdev; |
| 518 | |
| 519 | if (lowerdev->operstate == IF_OPER_DORMANT) |
| 520 | netif_dormant_on(dev); |
| 521 | else |
| 522 | netif_dormant_off(dev); |
| 523 | |
| 524 | if (netif_carrier_ok(lowerdev)) { |
| 525 | if (!netif_carrier_ok(dev)) |
| 526 | netif_carrier_on(dev); |
| 527 | } else { |
Patrick McHardy | f12ca5f | 2008-01-21 00:47:08 -0800 | [diff] [blame] | 528 | if (netif_carrier_ok(dev)) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 529 | netif_carrier_off(dev); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 534 | { |
| 535 | if (tb[IFLA_ADDRESS]) { |
| 536 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 537 | return -EINVAL; |
| 538 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 539 | return -EADDRNOTAVAIL; |
| 540 | } |
| 541 | return 0; |
| 542 | } |
| 543 | |
Eric Dumazet | 2c11455 | 2009-09-03 00:11:45 +0000 | [diff] [blame] | 544 | static int macvlan_get_tx_queues(struct net *net, |
| 545 | struct nlattr *tb[], |
| 546 | unsigned int *num_tx_queues, |
| 547 | unsigned int *real_num_tx_queues) |
| 548 | { |
| 549 | struct net_device *real_dev; |
| 550 | |
| 551 | if (!tb[IFLA_LINK]) |
| 552 | return -EINVAL; |
| 553 | |
| 554 | real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK])); |
| 555 | if (!real_dev) |
| 556 | return -ENODEV; |
| 557 | |
| 558 | *num_tx_queues = real_dev->num_tx_queues; |
| 559 | *real_num_tx_queues = real_dev->real_num_tx_queues; |
| 560 | return 0; |
| 561 | } |
| 562 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 563 | static int macvlan_newlink(struct net *src_net, struct net_device *dev, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 564 | struct nlattr *tb[], struct nlattr *data[]) |
| 565 | { |
| 566 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 567 | struct macvlan_port *port; |
| 568 | struct net_device *lowerdev; |
| 569 | int err; |
| 570 | |
| 571 | if (!tb[IFLA_LINK]) |
| 572 | return -EINVAL; |
| 573 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 574 | lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 575 | if (lowerdev == NULL) |
| 576 | return -ENODEV; |
| 577 | |
Eric Biederman | b0832a2 | 2009-03-13 13:15:37 -0700 | [diff] [blame] | 578 | /* When creating macvlans on top of other macvlans - use |
| 579 | * the real device as the lowerdev. |
Patrick McHardy | a6ca5f1 | 2008-01-10 22:39:28 -0800 | [diff] [blame] | 580 | */ |
Eric Biederman | b0832a2 | 2009-03-13 13:15:37 -0700 | [diff] [blame] | 581 | if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) { |
| 582 | struct macvlan_dev *lowervlan = netdev_priv(lowerdev); |
| 583 | lowerdev = lowervlan->lowerdev; |
| 584 | } |
Patrick McHardy | a6ca5f1 | 2008-01-10 22:39:28 -0800 | [diff] [blame] | 585 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 586 | if (!tb[IFLA_MTU]) |
| 587 | dev->mtu = lowerdev->mtu; |
| 588 | else if (dev->mtu > lowerdev->mtu) |
| 589 | return -EINVAL; |
| 590 | |
| 591 | if (!tb[IFLA_ADDRESS]) |
| 592 | random_ether_addr(dev->dev_addr); |
| 593 | |
| 594 | if (lowerdev->macvlan_port == NULL) { |
| 595 | err = macvlan_port_create(lowerdev); |
| 596 | if (err < 0) |
| 597 | return err; |
| 598 | } |
| 599 | port = lowerdev->macvlan_port; |
| 600 | |
| 601 | vlan->lowerdev = lowerdev; |
| 602 | vlan->dev = dev; |
| 603 | vlan->port = port; |
| 604 | |
| 605 | err = register_netdevice(dev); |
| 606 | if (err < 0) |
| 607 | return err; |
| 608 | |
| 609 | list_add_tail(&vlan->list, &port->vlans); |
| 610 | macvlan_transfer_operstate(dev); |
| 611 | return 0; |
| 612 | } |
| 613 | |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 614 | static void macvlan_dellink(struct net_device *dev, struct list_head *head) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 615 | { |
| 616 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 617 | struct macvlan_port *port = vlan->port; |
| 618 | |
| 619 | list_del(&vlan->list); |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 620 | unregister_netdevice_queue(dev, head); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 621 | |
| 622 | if (list_empty(&port->vlans)) |
Patrick McHardy | 7312096 | 2008-05-08 01:13:31 -0700 | [diff] [blame] | 623 | macvlan_port_destroy(port->dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | static struct rtnl_link_ops macvlan_link_ops __read_mostly = { |
| 627 | .kind = "macvlan", |
| 628 | .priv_size = sizeof(struct macvlan_dev), |
Eric Dumazet | 2c11455 | 2009-09-03 00:11:45 +0000 | [diff] [blame] | 629 | .get_tx_queues = macvlan_get_tx_queues, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 630 | .setup = macvlan_setup, |
| 631 | .validate = macvlan_validate, |
| 632 | .newlink = macvlan_newlink, |
| 633 | .dellink = macvlan_dellink, |
| 634 | }; |
| 635 | |
| 636 | static int macvlan_device_event(struct notifier_block *unused, |
| 637 | unsigned long event, void *ptr) |
| 638 | { |
| 639 | struct net_device *dev = ptr; |
| 640 | struct macvlan_dev *vlan, *next; |
| 641 | struct macvlan_port *port; |
| 642 | |
| 643 | port = dev->macvlan_port; |
| 644 | if (port == NULL) |
| 645 | return NOTIFY_DONE; |
| 646 | |
| 647 | switch (event) { |
| 648 | case NETDEV_CHANGE: |
| 649 | list_for_each_entry(vlan, &port->vlans, list) |
| 650 | macvlan_transfer_operstate(vlan->dev); |
| 651 | break; |
| 652 | case NETDEV_FEAT_CHANGE: |
| 653 | list_for_each_entry(vlan, &port->vlans, list) { |
| 654 | vlan->dev->features = dev->features & MACVLAN_FEATURES; |
| 655 | netdev_features_change(vlan->dev); |
| 656 | } |
| 657 | break; |
| 658 | case NETDEV_UNREGISTER: |
| 659 | list_for_each_entry_safe(vlan, next, &port->vlans, list) |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 660 | macvlan_dellink(vlan->dev, NULL); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 661 | break; |
| 662 | } |
| 663 | return NOTIFY_DONE; |
| 664 | } |
| 665 | |
| 666 | static struct notifier_block macvlan_notifier_block __read_mostly = { |
| 667 | .notifier_call = macvlan_device_event, |
| 668 | }; |
| 669 | |
| 670 | static int __init macvlan_init_module(void) |
| 671 | { |
| 672 | int err; |
| 673 | |
| 674 | register_netdevice_notifier(&macvlan_notifier_block); |
| 675 | macvlan_handle_frame_hook = macvlan_handle_frame; |
| 676 | |
| 677 | err = rtnl_link_register(&macvlan_link_ops); |
| 678 | if (err < 0) |
| 679 | goto err1; |
| 680 | return 0; |
| 681 | err1: |
Rami Rosen | 5291324 | 2008-01-31 16:56:03 -0800 | [diff] [blame] | 682 | macvlan_handle_frame_hook = NULL; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 683 | unregister_netdevice_notifier(&macvlan_notifier_block); |
| 684 | return err; |
| 685 | } |
| 686 | |
| 687 | static void __exit macvlan_cleanup_module(void) |
| 688 | { |
| 689 | rtnl_link_unregister(&macvlan_link_ops); |
| 690 | macvlan_handle_frame_hook = NULL; |
| 691 | unregister_netdevice_notifier(&macvlan_notifier_block); |
| 692 | } |
| 693 | |
| 694 | module_init(macvlan_init_module); |
| 695 | module_exit(macvlan_cleanup_module); |
| 696 | |
| 697 | MODULE_LICENSE("GPL"); |
| 698 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 699 | MODULE_DESCRIPTION("Driver for MAC address based VLANs"); |
| 700 | MODULE_ALIAS_RTNL_LINK("macvlan"); |