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