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> |
Jiri Pirko | 87002b0 | 2011-12-08 04:11:17 +0000 | [diff] [blame] | 29 | #include <linux/if_vlan.h> |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 30 | #include <linux/if_link.h> |
| 31 | #include <linux/if_macvlan.h> |
Eric Dumazet | cd431e7 | 2013-02-05 20:22:50 +0000 | [diff] [blame] | 32 | #include <linux/hash.h> |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 33 | #include <net/rtnetlink.h> |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 34 | #include <net/xfrm.h> |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 35 | |
| 36 | #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE) |
| 37 | |
| 38 | struct macvlan_port { |
| 39 | struct net_device *dev; |
| 40 | struct hlist_head vlan_hash[MACVLAN_HASH_SIZE]; |
| 41 | struct list_head vlans; |
Jiri Pirko | 8b37ef0 | 2010-06-07 01:36:29 +0000 | [diff] [blame] | 42 | struct rcu_head rcu; |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 43 | bool passthru; |
Eric W. Biederman | d5cd9244 | 2011-03-21 18:22:22 -0700 | [diff] [blame] | 44 | int count; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Eric W. Biederman | d5cd9244 | 2011-03-21 18:22:22 -0700 | [diff] [blame] | 47 | static void macvlan_port_destroy(struct net_device *dev); |
| 48 | |
Jiri Pirko | a35e2c1 | 2010-06-15 03:27:57 +0000 | [diff] [blame] | 49 | #define macvlan_port_get_rcu(dev) \ |
| 50 | ((struct macvlan_port *) rcu_dereference(dev->rx_handler_data)) |
| 51 | #define macvlan_port_get(dev) ((struct macvlan_port *) dev->rx_handler_data) |
| 52 | #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT) |
| 53 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 54 | static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port, |
| 55 | const unsigned char *addr) |
| 56 | { |
| 57 | struct macvlan_dev *vlan; |
| 58 | struct hlist_node *n; |
| 59 | |
| 60 | hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) { |
Joe Perches | a6700db | 2012-05-09 17:04:04 +0000 | [diff] [blame] | 61 | if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr)) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 62 | return vlan; |
| 63 | } |
| 64 | return NULL; |
| 65 | } |
| 66 | |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 67 | static void macvlan_hash_add(struct macvlan_dev *vlan) |
| 68 | { |
| 69 | struct macvlan_port *port = vlan->port; |
| 70 | const unsigned char *addr = vlan->dev->dev_addr; |
| 71 | |
| 72 | hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]); |
| 73 | } |
| 74 | |
Eric Dumazet | 449f454 | 2011-05-19 12:24:16 +0000 | [diff] [blame] | 75 | static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync) |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 76 | { |
| 77 | hlist_del_rcu(&vlan->hlist); |
Eric Dumazet | 449f454 | 2011-05-19 12:24:16 +0000 | [diff] [blame] | 78 | if (sync) |
| 79 | synchronize_rcu(); |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static void macvlan_hash_change_addr(struct macvlan_dev *vlan, |
| 83 | const unsigned char *addr) |
| 84 | { |
Eric Dumazet | 449f454 | 2011-05-19 12:24:16 +0000 | [diff] [blame] | 85 | macvlan_hash_del(vlan, true); |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 86 | /* Now that we are unhashed it is safe to change the device |
| 87 | * address without confusing packet delivery. |
| 88 | */ |
| 89 | memcpy(vlan->dev->dev_addr, addr, ETH_ALEN); |
| 90 | macvlan_hash_add(vlan); |
| 91 | } |
| 92 | |
| 93 | static int macvlan_addr_busy(const struct macvlan_port *port, |
| 94 | const unsigned char *addr) |
| 95 | { |
| 96 | /* Test to see if the specified multicast address is |
| 97 | * currently in use by the underlying device or |
| 98 | * another macvlan. |
| 99 | */ |
Joe Perches | a6700db | 2012-05-09 17:04:04 +0000 | [diff] [blame] | 100 | if (ether_addr_equal_64bits(port->dev->dev_addr, addr)) |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 101 | return 1; |
| 102 | |
| 103 | if (macvlan_hash_lookup(port, addr)) |
| 104 | return 1; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 109 | |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 110 | static int macvlan_broadcast_one(struct sk_buff *skb, |
| 111 | const struct macvlan_dev *vlan, |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 112 | const struct ethhdr *eth, bool local) |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 113 | { |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 114 | struct net_device *dev = vlan->dev; |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 115 | if (!skb) |
| 116 | return NET_RX_DROP; |
| 117 | |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 118 | if (local) |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 119 | return vlan->forward(dev, skb); |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 120 | |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 121 | skb->dev = dev; |
Joe Perches | a6700db | 2012-05-09 17:04:04 +0000 | [diff] [blame] | 122 | if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast)) |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 123 | skb->pkt_type = PACKET_BROADCAST; |
| 124 | else |
| 125 | skb->pkt_type = PACKET_MULTICAST; |
| 126 | |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 127 | return vlan->receive(skb); |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Eric Dumazet | 3807ff5 | 2013-02-07 16:41:02 +0000 | [diff] [blame] | 130 | static u32 macvlan_hash_mix(const struct macvlan_dev *vlan) |
| 131 | { |
| 132 | return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | static unsigned int mc_hash(const struct macvlan_dev *vlan, |
| 137 | const unsigned char *addr) |
Eric Dumazet | cd431e7 | 2013-02-05 20:22:50 +0000 | [diff] [blame] | 138 | { |
| 139 | u32 val = __get_unaligned_cpu32(addr + 2); |
| 140 | |
Eric Dumazet | 3807ff5 | 2013-02-07 16:41:02 +0000 | [diff] [blame] | 141 | val ^= macvlan_hash_mix(vlan); |
Eric Dumazet | cd431e7 | 2013-02-05 20:22:50 +0000 | [diff] [blame] | 142 | return hash_32(val, MACVLAN_MC_FILTER_BITS); |
| 143 | } |
| 144 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 145 | static void macvlan_broadcast(struct sk_buff *skb, |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 146 | const struct macvlan_port *port, |
| 147 | struct net_device *src, |
| 148 | enum macvlan_mode mode) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 149 | { |
| 150 | const struct ethhdr *eth = eth_hdr(skb); |
| 151 | const struct macvlan_dev *vlan; |
| 152 | struct hlist_node *n; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 153 | struct sk_buff *nskb; |
| 154 | unsigned int i; |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 155 | int err; |
Eric Dumazet | 3807ff5 | 2013-02-07 16:41:02 +0000 | [diff] [blame] | 156 | unsigned int hash; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 157 | |
Patrick McHardy | efbbced | 2008-11-26 15:30:48 -0800 | [diff] [blame] | 158 | if (skb->protocol == htons(ETH_P_PAUSE)) |
| 159 | return; |
| 160 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 161 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) { |
| 162 | hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) { |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 163 | if (vlan->dev == src || !(vlan->mode & mode)) |
| 164 | continue; |
| 165 | |
Eric Dumazet | 3807ff5 | 2013-02-07 16:41:02 +0000 | [diff] [blame] | 166 | hash = mc_hash(vlan, eth->h_dest); |
Eric Dumazet | cd431e7 | 2013-02-05 20:22:50 +0000 | [diff] [blame] | 167 | if (!test_bit(hash, vlan->mc_filter)) |
| 168 | continue; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 169 | nskb = skb_clone(skb, GFP_ATOMIC); |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 170 | err = macvlan_broadcast_one(nskb, vlan, eth, |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 171 | mode == MACVLAN_MODE_BRIDGE); |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 172 | macvlan_count_rx(vlan, skb->len + ETH_HLEN, |
| 173 | err == NET_RX_SUCCESS, 1); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* called under rcu_read_lock() from netif_receive_skb */ |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 179 | static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 180 | { |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 181 | struct macvlan_port *port; |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 182 | struct sk_buff *skb = *pskb; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 183 | const struct ethhdr *eth = eth_hdr(skb); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 184 | const struct macvlan_dev *vlan; |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 185 | const struct macvlan_dev *src; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 186 | struct net_device *dev; |
Sridhar Samudrala | ba01877 | 2010-07-27 09:10:07 +0000 | [diff] [blame] | 187 | unsigned int len = 0; |
| 188 | int ret = NET_RX_DROP; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 189 | |
Jiri Pirko | a35e2c1 | 2010-06-15 03:27:57 +0000 | [diff] [blame] | 190 | port = macvlan_port_get_rcu(skb->dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 191 | if (is_multicast_ether_addr(eth->h_dest)) { |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 192 | skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN); |
| 193 | if (!skb) |
| 194 | return RX_HANDLER_CONSUMED; |
Eric Dumazet | 4ec7ac1 | 2012-01-23 05:38:59 +0000 | [diff] [blame] | 195 | eth = eth_hdr(skb); |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 196 | src = macvlan_hash_lookup(port, eth->h_source); |
| 197 | if (!src) |
| 198 | /* frame comes from an external address */ |
| 199 | macvlan_broadcast(skb, port, NULL, |
| 200 | MACVLAN_MODE_PRIVATE | |
| 201 | MACVLAN_MODE_VEPA | |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 202 | MACVLAN_MODE_PASSTHRU| |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 203 | MACVLAN_MODE_BRIDGE); |
| 204 | else if (src->mode == MACVLAN_MODE_VEPA) |
| 205 | /* flood to everyone except source */ |
| 206 | macvlan_broadcast(skb, port, src->dev, |
| 207 | MACVLAN_MODE_VEPA | |
| 208 | MACVLAN_MODE_BRIDGE); |
| 209 | else if (src->mode == MACVLAN_MODE_BRIDGE) |
| 210 | /* |
| 211 | * flood only to VEPA ports, bridge ports |
| 212 | * already saw the frame on the way out. |
| 213 | */ |
| 214 | macvlan_broadcast(skb, port, src->dev, |
| 215 | MACVLAN_MODE_VEPA); |
stephen hemminger | 729e72a | 2011-11-02 12:11:53 +0000 | [diff] [blame] | 216 | else { |
| 217 | /* forward to original port. */ |
| 218 | vlan = src; |
| 219 | ret = macvlan_broadcast_one(skb, vlan, eth, 0); |
| 220 | goto out; |
| 221 | } |
| 222 | |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 223 | return RX_HANDLER_PASS; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 226 | if (port->passthru) |
| 227 | vlan = list_first_entry(&port->vlans, struct macvlan_dev, list); |
| 228 | else |
| 229 | vlan = macvlan_hash_lookup(port, eth->h_dest); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 230 | if (vlan == NULL) |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 231 | return RX_HANDLER_PASS; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 232 | |
| 233 | dev = vlan->dev; |
| 234 | if (unlikely(!(dev->flags & IFF_UP))) { |
| 235 | kfree_skb(skb); |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 236 | return RX_HANDLER_CONSUMED; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 237 | } |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 238 | len = skb->len + ETH_HLEN; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 239 | skb = skb_share_check(skb, GFP_ATOMIC); |
Arnd Bergmann | a1e514c | 2009-11-26 06:07:09 +0000 | [diff] [blame] | 240 | if (!skb) |
Sridhar Samudrala | ba01877 | 2010-07-27 09:10:07 +0000 | [diff] [blame] | 241 | goto out; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 242 | |
| 243 | skb->dev = dev; |
| 244 | skb->pkt_type = PACKET_HOST; |
| 245 | |
Sridhar Samudrala | ba01877 | 2010-07-27 09:10:07 +0000 | [diff] [blame] | 246 | ret = vlan->receive(skb); |
| 247 | |
| 248 | out: |
| 249 | macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0); |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 250 | return RX_HANDLER_CONSUMED; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 253 | static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev) |
| 254 | { |
| 255 | const struct macvlan_dev *vlan = netdev_priv(dev); |
| 256 | const struct macvlan_port *port = vlan->port; |
| 257 | const struct macvlan_dev *dest; |
Daniel Lezcano | 12a2856 | 2011-03-14 06:08:07 +0000 | [diff] [blame] | 258 | __u8 ip_summed = skb->ip_summed; |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 259 | |
| 260 | if (vlan->mode == MACVLAN_MODE_BRIDGE) { |
| 261 | const struct ethhdr *eth = (void *)skb->data; |
Daniel Lezcano | 12a2856 | 2011-03-14 06:08:07 +0000 | [diff] [blame] | 262 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 263 | |
| 264 | /* send to other bridge ports directly */ |
| 265 | if (is_multicast_ether_addr(eth->h_dest)) { |
| 266 | macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE); |
| 267 | goto xmit_world; |
| 268 | } |
| 269 | |
| 270 | dest = macvlan_hash_lookup(port, eth->h_dest); |
| 271 | if (dest && dest->mode == MACVLAN_MODE_BRIDGE) { |
David Ward | a37dd33 | 2011-05-19 02:53:20 +0000 | [diff] [blame] | 272 | /* send to lowerdev first for its network taps */ |
David Ward | cb2d0f3 | 2011-09-18 12:53:20 +0000 | [diff] [blame] | 273 | dev_forward_skb(vlan->lowerdev, skb); |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 274 | |
| 275 | return NET_XMIT_SUCCESS; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | xmit_world: |
Daniel Lezcano | 12a2856 | 2011-03-14 06:08:07 +0000 | [diff] [blame] | 280 | skb->ip_summed = ip_summed; |
David S. Miller | 59b9997 | 2012-05-10 23:03:34 -0400 | [diff] [blame] | 281 | skb->dev = vlan->lowerdev; |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 282 | return dev_queue_xmit(skb); |
| 283 | } |
| 284 | |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 285 | netdev_tx_t macvlan_start_xmit(struct sk_buff *skb, |
| 286 | struct net_device *dev) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 287 | { |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 288 | unsigned int len = skb->len; |
| 289 | int ret; |
Eric Dumazet | 8ffab51 | 2010-11-10 21:14:04 +0000 | [diff] [blame] | 290 | const struct macvlan_dev *vlan = netdev_priv(dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 291 | |
Arnd Bergmann | 618e1b7 | 2009-11-26 06:07:10 +0000 | [diff] [blame] | 292 | ret = macvlan_queue_xmit(skb, dev); |
Eric Dumazet | 2d6c9ff | 2010-05-10 04:51:02 +0000 | [diff] [blame] | 293 | if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { |
Eric Dumazet | 8ffab51 | 2010-11-10 21:14:04 +0000 | [diff] [blame] | 294 | struct macvlan_pcpu_stats *pcpu_stats; |
Eric Dumazet | 2c11455 | 2009-09-03 00:11:45 +0000 | [diff] [blame] | 295 | |
Eric Dumazet | 8ffab51 | 2010-11-10 21:14:04 +0000 | [diff] [blame] | 296 | pcpu_stats = this_cpu_ptr(vlan->pcpu_stats); |
| 297 | u64_stats_update_begin(&pcpu_stats->syncp); |
| 298 | pcpu_stats->tx_packets++; |
| 299 | pcpu_stats->tx_bytes += len; |
| 300 | u64_stats_update_end(&pcpu_stats->syncp); |
| 301 | } else { |
| 302 | this_cpu_inc(vlan->pcpu_stats->tx_dropped); |
| 303 | } |
Patrick McHardy | cbbef5e | 2009-11-10 06:14:24 +0000 | [diff] [blame] | 304 | return ret; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 305 | } |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 306 | EXPORT_SYMBOL_GPL(macvlan_start_xmit); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 307 | |
| 308 | 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] | 309 | unsigned short type, const void *daddr, |
| 310 | const void *saddr, unsigned len) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 311 | { |
| 312 | const struct macvlan_dev *vlan = netdev_priv(dev); |
| 313 | struct net_device *lowerdev = vlan->lowerdev; |
| 314 | |
Stephen Hemminger | 0c4e858 | 2007-10-09 01:36:32 -0700 | [diff] [blame] | 315 | return dev_hard_header(skb, lowerdev, type, daddr, |
| 316 | saddr ? : dev->dev_addr, len); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 319 | static const struct header_ops macvlan_hard_header_ops = { |
| 320 | .create = macvlan_hard_header, |
| 321 | .rebuild = eth_rebuild_header, |
| 322 | .parse = eth_header_parse, |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 323 | .cache = eth_header_cache, |
| 324 | .cache_update = eth_header_cache_update, |
| 325 | }; |
| 326 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 327 | static int macvlan_open(struct net_device *dev) |
| 328 | { |
| 329 | struct macvlan_dev *vlan = netdev_priv(dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 330 | struct net_device *lowerdev = vlan->lowerdev; |
| 331 | int err; |
| 332 | |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 333 | if (vlan->port->passthru) { |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 334 | if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) |
| 335 | dev_set_promiscuity(lowerdev, 1); |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 336 | goto hash_add; |
| 337 | } |
| 338 | |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 339 | err = -EBUSY; |
| 340 | if (macvlan_addr_busy(vlan->port, dev->dev_addr)) |
| 341 | goto out; |
| 342 | |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 343 | err = dev_uc_add(lowerdev, dev->dev_addr); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 344 | if (err < 0) |
Wang Chen | b89fb7d | 2008-07-14 20:57:07 -0700 | [diff] [blame] | 345 | goto out; |
| 346 | if (dev->flags & IFF_ALLMULTI) { |
| 347 | err = dev_set_allmulti(lowerdev, 1); |
| 348 | if (err < 0) |
| 349 | goto del_unicast; |
| 350 | } |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 351 | |
| 352 | hash_add: |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 353 | macvlan_hash_add(vlan); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 354 | return 0; |
Wang Chen | b89fb7d | 2008-07-14 20:57:07 -0700 | [diff] [blame] | 355 | |
| 356 | del_unicast: |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 357 | dev_uc_del(lowerdev, dev->dev_addr); |
Wang Chen | b89fb7d | 2008-07-14 20:57:07 -0700 | [diff] [blame] | 358 | out: |
| 359 | return err; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static int macvlan_stop(struct net_device *dev) |
| 363 | { |
| 364 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 365 | struct net_device *lowerdev = vlan->lowerdev; |
| 366 | |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 367 | dev_uc_unsync(lowerdev, dev); |
| 368 | dev_mc_unsync(lowerdev, dev); |
| 369 | |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 370 | if (vlan->port->passthru) { |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 371 | if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) |
| 372 | dev_set_promiscuity(lowerdev, -1); |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 373 | goto hash_del; |
| 374 | } |
| 375 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 376 | if (dev->flags & IFF_ALLMULTI) |
| 377 | dev_set_allmulti(lowerdev, -1); |
| 378 | |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 379 | dev_uc_del(lowerdev, dev->dev_addr); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 380 | |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 381 | hash_del: |
Eric Dumazet | 449f454 | 2011-05-19 12:24:16 +0000 | [diff] [blame] | 382 | macvlan_hash_del(vlan, !dev->dismantle); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 383 | return 0; |
| 384 | } |
| 385 | |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 386 | static int macvlan_set_mac_address(struct net_device *dev, void *p) |
| 387 | { |
| 388 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 389 | struct net_device *lowerdev = vlan->lowerdev; |
| 390 | struct sockaddr *addr = p; |
| 391 | int err; |
| 392 | |
| 393 | if (!is_valid_ether_addr(addr->sa_data)) |
| 394 | return -EADDRNOTAVAIL; |
| 395 | |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 396 | if (!(dev->flags & IFF_UP)) { |
| 397 | /* Just copy in the new address */ |
| 398 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); |
| 399 | } else { |
| 400 | /* Rehash and update the device filters */ |
| 401 | if (macvlan_addr_busy(vlan->port, addr->sa_data)) |
| 402 | return -EBUSY; |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 403 | |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 404 | err = dev_uc_add(lowerdev, addr->sa_data); |
Jiri Pirko | ccffad25 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 405 | if (err) |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 406 | return err; |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 407 | |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 408 | dev_uc_del(lowerdev, dev->dev_addr); |
Eric Biederman | f9ac30f | 2009-03-13 13:16:13 -0700 | [diff] [blame] | 409 | |
| 410 | macvlan_hash_change_addr(vlan, addr->sa_data); |
| 411 | } |
Patrick McHardy | ad5d20a | 2007-11-19 22:00:42 -0800 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 415 | static void macvlan_change_rx_flags(struct net_device *dev, int change) |
| 416 | { |
| 417 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 418 | struct net_device *lowerdev = vlan->lowerdev; |
| 419 | |
| 420 | if (change & IFF_ALLMULTI) |
| 421 | dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1); |
| 422 | } |
| 423 | |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 424 | static void macvlan_set_mac_lists(struct net_device *dev) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 425 | { |
| 426 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 427 | |
Eric Dumazet | cd431e7 | 2013-02-05 20:22:50 +0000 | [diff] [blame] | 428 | if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) { |
| 429 | bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ); |
| 430 | } else { |
| 431 | struct netdev_hw_addr *ha; |
| 432 | DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ); |
| 433 | |
| 434 | bitmap_zero(filter, MACVLAN_MC_FILTER_SZ); |
| 435 | netdev_for_each_mc_addr(ha, dev) { |
Eric Dumazet | 3807ff5 | 2013-02-07 16:41:02 +0000 | [diff] [blame] | 436 | __set_bit(mc_hash(vlan, ha->addr), filter); |
Eric Dumazet | cd431e7 | 2013-02-05 20:22:50 +0000 | [diff] [blame] | 437 | } |
Eric Dumazet | d527043 | 2013-02-07 16:02:57 +0000 | [diff] [blame] | 438 | |
Eric Dumazet | 3807ff5 | 2013-02-07 16:41:02 +0000 | [diff] [blame] | 439 | __set_bit(mc_hash(vlan, dev->broadcast), filter); |
Eric Dumazet | d527043 | 2013-02-07 16:02:57 +0000 | [diff] [blame] | 440 | |
Eric Dumazet | cd431e7 | 2013-02-05 20:22:50 +0000 | [diff] [blame] | 441 | bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ); |
| 442 | } |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 443 | dev_uc_sync(vlan->lowerdev, dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 444 | dev_mc_sync(vlan->lowerdev, dev); |
| 445 | } |
| 446 | |
| 447 | static int macvlan_change_mtu(struct net_device *dev, int new_mtu) |
| 448 | { |
| 449 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 450 | |
| 451 | if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu) |
| 452 | return -EINVAL; |
| 453 | dev->mtu = new_mtu; |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * macvlan network devices have devices nesting below it and are a special |
| 459 | * "super class" of normal network devices; split their locks off into a |
| 460 | * separate class since they always nest. |
| 461 | */ |
| 462 | static struct lock_class_key macvlan_netdev_xmit_lock_key; |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 463 | static struct lock_class_key macvlan_netdev_addr_lock_key; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 464 | |
| 465 | #define MACVLAN_FEATURES \ |
| 466 | (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ |
| 467 | NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \ |
John Fastabend | 8d13e67 | 2011-06-06 04:27:16 +0000 | [diff] [blame] | 468 | NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \ |
| 469 | NETIF_F_HW_VLAN_FILTER) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 470 | |
| 471 | #define MACVLAN_STATE_MASK \ |
| 472 | ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) |
| 473 | |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 474 | static void macvlan_set_lockdep_class_one(struct net_device *dev, |
| 475 | struct netdev_queue *txq, |
| 476 | void *_unused) |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 477 | { |
| 478 | lockdep_set_class(&txq->_xmit_lock, |
| 479 | &macvlan_netdev_xmit_lock_key); |
| 480 | } |
| 481 | |
| 482 | static void macvlan_set_lockdep_class(struct net_device *dev) |
| 483 | { |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 484 | lockdep_set_class(&dev->addr_list_lock, |
| 485 | &macvlan_netdev_addr_lock_key); |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 486 | 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] | 487 | } |
| 488 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 489 | static int macvlan_init(struct net_device *dev) |
| 490 | { |
| 491 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 492 | const struct net_device *lowerdev = vlan->lowerdev; |
| 493 | |
| 494 | dev->state = (dev->state & ~MACVLAN_STATE_MASK) | |
| 495 | (lowerdev->state & MACVLAN_STATE_MASK); |
| 496 | dev->features = lowerdev->features & MACVLAN_FEATURES; |
Eric Dumazet | 8ffab51 | 2010-11-10 21:14:04 +0000 | [diff] [blame] | 497 | dev->features |= NETIF_F_LLTX; |
Patrick McHardy | 8c2acc5 | 2009-11-23 14:18:53 -0800 | [diff] [blame] | 498 | dev->gso_max_size = lowerdev->gso_max_size; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 499 | dev->iflink = lowerdev->ifindex; |
sg.tweak@gmail.com | ef5c899 | 2009-06-10 09:55:02 +0000 | [diff] [blame] | 500 | dev->hard_header_len = lowerdev->hard_header_len; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 501 | |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 502 | macvlan_set_lockdep_class(dev); |
| 503 | |
Eric Dumazet | 8ffab51 | 2010-11-10 21:14:04 +0000 | [diff] [blame] | 504 | vlan->pcpu_stats = alloc_percpu(struct macvlan_pcpu_stats); |
| 505 | if (!vlan->pcpu_stats) |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 506 | return -ENOMEM; |
| 507 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 508 | return 0; |
| 509 | } |
| 510 | |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 511 | static void macvlan_uninit(struct net_device *dev) |
| 512 | { |
| 513 | struct macvlan_dev *vlan = netdev_priv(dev); |
Eric W. Biederman | d5cd9244 | 2011-03-21 18:22:22 -0700 | [diff] [blame] | 514 | struct macvlan_port *port = vlan->port; |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 515 | |
Eric Dumazet | 8ffab51 | 2010-11-10 21:14:04 +0000 | [diff] [blame] | 516 | free_percpu(vlan->pcpu_stats); |
Eric W. Biederman | d5cd9244 | 2011-03-21 18:22:22 -0700 | [diff] [blame] | 517 | |
| 518 | port->count -= 1; |
| 519 | if (!port->count) |
| 520 | macvlan_port_destroy(port->dev); |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Eric Dumazet | 2817273 | 2010-07-07 14:58:56 -0700 | [diff] [blame] | 523 | static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev, |
| 524 | struct rtnl_link_stats64 *stats) |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 525 | { |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 526 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 527 | |
Eric Dumazet | 8ffab51 | 2010-11-10 21:14:04 +0000 | [diff] [blame] | 528 | if (vlan->pcpu_stats) { |
| 529 | struct macvlan_pcpu_stats *p; |
| 530 | u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; |
| 531 | u32 rx_errors = 0, tx_dropped = 0; |
Eric Dumazet | bc66154 | 2010-06-24 00:54:21 +0000 | [diff] [blame] | 532 | unsigned int start; |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 533 | int i; |
| 534 | |
| 535 | for_each_possible_cpu(i) { |
Eric Dumazet | 8ffab51 | 2010-11-10 21:14:04 +0000 | [diff] [blame] | 536 | p = per_cpu_ptr(vlan->pcpu_stats, i); |
Eric Dumazet | bc66154 | 2010-06-24 00:54:21 +0000 | [diff] [blame] | 537 | do { |
| 538 | start = u64_stats_fetch_begin_bh(&p->syncp); |
| 539 | rx_packets = p->rx_packets; |
| 540 | rx_bytes = p->rx_bytes; |
| 541 | rx_multicast = p->rx_multicast; |
Eric Dumazet | 8ffab51 | 2010-11-10 21:14:04 +0000 | [diff] [blame] | 542 | tx_packets = p->tx_packets; |
| 543 | tx_bytes = p->tx_bytes; |
Eric Dumazet | bc66154 | 2010-06-24 00:54:21 +0000 | [diff] [blame] | 544 | } while (u64_stats_fetch_retry_bh(&p->syncp, start)); |
Eric Dumazet | 8ffab51 | 2010-11-10 21:14:04 +0000 | [diff] [blame] | 545 | |
| 546 | stats->rx_packets += rx_packets; |
| 547 | stats->rx_bytes += rx_bytes; |
| 548 | stats->multicast += rx_multicast; |
| 549 | stats->tx_packets += tx_packets; |
| 550 | stats->tx_bytes += tx_bytes; |
| 551 | /* rx_errors & tx_dropped are u32, updated |
| 552 | * without syncp protection. |
| 553 | */ |
| 554 | rx_errors += p->rx_errors; |
| 555 | tx_dropped += p->tx_dropped; |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 556 | } |
Eric Dumazet | 8ffab51 | 2010-11-10 21:14:04 +0000 | [diff] [blame] | 557 | stats->rx_errors = rx_errors; |
| 558 | stats->rx_dropped = rx_errors; |
| 559 | stats->tx_dropped = tx_dropped; |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 560 | } |
| 561 | return stats; |
| 562 | } |
| 563 | |
Jiri Pirko | 8e58613 | 2011-12-08 19:52:37 -0500 | [diff] [blame] | 564 | static int macvlan_vlan_rx_add_vid(struct net_device *dev, |
John Fastabend | 8d13e67 | 2011-06-06 04:27:16 +0000 | [diff] [blame] | 565 | unsigned short vid) |
| 566 | { |
| 567 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 568 | struct net_device *lowerdev = vlan->lowerdev; |
John Fastabend | 8d13e67 | 2011-06-06 04:27:16 +0000 | [diff] [blame] | 569 | |
Jiri Pirko | 87002b0 | 2011-12-08 04:11:17 +0000 | [diff] [blame] | 570 | return vlan_vid_add(lowerdev, vid); |
John Fastabend | 8d13e67 | 2011-06-06 04:27:16 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Jiri Pirko | 8e58613 | 2011-12-08 19:52:37 -0500 | [diff] [blame] | 573 | static int macvlan_vlan_rx_kill_vid(struct net_device *dev, |
John Fastabend | 8d13e67 | 2011-06-06 04:27:16 +0000 | [diff] [blame] | 574 | unsigned short vid) |
| 575 | { |
| 576 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 577 | struct net_device *lowerdev = vlan->lowerdev; |
John Fastabend | 8d13e67 | 2011-06-06 04:27:16 +0000 | [diff] [blame] | 578 | |
Jiri Pirko | 87002b0 | 2011-12-08 04:11:17 +0000 | [diff] [blame] | 579 | vlan_vid_del(lowerdev, vid); |
Jiri Pirko | 8e58613 | 2011-12-08 19:52:37 -0500 | [diff] [blame] | 580 | return 0; |
John Fastabend | 8d13e67 | 2011-06-06 04:27:16 +0000 | [diff] [blame] | 581 | } |
| 582 | |
stephen hemminger | edc7d57 | 2012-10-01 12:32:33 +0000 | [diff] [blame] | 583 | static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 584 | struct net_device *dev, |
stephen hemminger | 6b6e272 | 2012-09-17 10:03:26 +0000 | [diff] [blame] | 585 | const unsigned char *addr, |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 586 | u16 flags) |
| 587 | { |
| 588 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 589 | int err = -EINVAL; |
| 590 | |
| 591 | if (!vlan->port->passthru) |
| 592 | return -EOPNOTSUPP; |
| 593 | |
| 594 | if (is_unicast_ether_addr(addr)) |
| 595 | err = dev_uc_add_excl(dev, addr); |
| 596 | else if (is_multicast_ether_addr(addr)) |
| 597 | err = dev_mc_add_excl(dev, addr); |
| 598 | |
| 599 | return err; |
| 600 | } |
| 601 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 602 | static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 603 | struct net_device *dev, |
stephen hemminger | 6b6e272 | 2012-09-17 10:03:26 +0000 | [diff] [blame] | 604 | const unsigned char *addr) |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 605 | { |
| 606 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 607 | int err = -EINVAL; |
| 608 | |
| 609 | if (!vlan->port->passthru) |
| 610 | return -EOPNOTSUPP; |
| 611 | |
| 612 | if (is_unicast_ether_addr(addr)) |
| 613 | err = dev_uc_del(dev, addr); |
| 614 | else if (is_multicast_ether_addr(addr)) |
| 615 | err = dev_mc_del(dev, addr); |
| 616 | |
| 617 | return err; |
| 618 | } |
| 619 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 620 | static void macvlan_ethtool_get_drvinfo(struct net_device *dev, |
| 621 | struct ethtool_drvinfo *drvinfo) |
| 622 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 623 | strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver)); |
| 624 | strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version)); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 627 | static int macvlan_ethtool_get_settings(struct net_device *dev, |
| 628 | struct ethtool_cmd *cmd) |
| 629 | { |
| 630 | const struct macvlan_dev *vlan = netdev_priv(dev); |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 631 | |
| 632 | return __ethtool_get_settings(vlan->lowerdev, cmd); |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 635 | static const struct ethtool_ops macvlan_ethtool_ops = { |
| 636 | .get_link = ethtool_op_get_link, |
Stephen Hemminger | 9edb8bb | 2008-10-29 15:31:53 -0700 | [diff] [blame] | 637 | .get_settings = macvlan_ethtool_get_settings, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 638 | .get_drvinfo = macvlan_ethtool_get_drvinfo, |
| 639 | }; |
| 640 | |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 641 | static const struct net_device_ops macvlan_netdev_ops = { |
| 642 | .ndo_init = macvlan_init, |
Eric Dumazet | fccaf71 | 2009-11-17 08:53:49 +0000 | [diff] [blame] | 643 | .ndo_uninit = macvlan_uninit, |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 644 | .ndo_open = macvlan_open, |
| 645 | .ndo_stop = macvlan_stop, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 646 | .ndo_start_xmit = macvlan_start_xmit, |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 647 | .ndo_change_mtu = macvlan_change_mtu, |
| 648 | .ndo_change_rx_flags = macvlan_change_rx_flags, |
| 649 | .ndo_set_mac_address = macvlan_set_mac_address, |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 650 | .ndo_set_rx_mode = macvlan_set_mac_lists, |
Eric Dumazet | bc66154 | 2010-06-24 00:54:21 +0000 | [diff] [blame] | 651 | .ndo_get_stats64 = macvlan_dev_get_stats64, |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 652 | .ndo_validate_addr = eth_validate_addr, |
John Fastabend | 8d13e67 | 2011-06-06 04:27:16 +0000 | [diff] [blame] | 653 | .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid, |
| 654 | .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid, |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 655 | .ndo_fdb_add = macvlan_fdb_add, |
| 656 | .ndo_fdb_del = macvlan_fdb_del, |
| 657 | .ndo_fdb_dump = ndo_dflt_fdb_dump, |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 658 | }; |
| 659 | |
Herbert Xu | 8a35747 | 2010-07-21 21:44:31 +0000 | [diff] [blame] | 660 | void macvlan_common_setup(struct net_device *dev) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 661 | { |
| 662 | ether_setup(dev); |
| 663 | |
Neil Horman | 550fd08 | 2011-07-26 06:05:38 +0000 | [diff] [blame] | 664 | dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); |
Stephen Hemminger | 54a30c9 | 2008-11-19 21:51:06 -0800 | [diff] [blame] | 665 | dev->netdev_ops = &macvlan_netdev_ops; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 666 | dev->destructor = free_netdev; |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 667 | dev->header_ops = &macvlan_hard_header_ops, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 668 | dev->ethtool_ops = &macvlan_ethtool_ops; |
Herbert Xu | 8a35747 | 2010-07-21 21:44:31 +0000 | [diff] [blame] | 669 | } |
| 670 | EXPORT_SYMBOL_GPL(macvlan_common_setup); |
| 671 | |
| 672 | static void macvlan_setup(struct net_device *dev) |
| 673 | { |
| 674 | macvlan_common_setup(dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 675 | dev->tx_queue_len = 0; |
| 676 | } |
| 677 | |
| 678 | static int macvlan_port_create(struct net_device *dev) |
| 679 | { |
| 680 | struct macvlan_port *port; |
| 681 | unsigned int i; |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 682 | int err; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 683 | |
| 684 | if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) |
| 685 | return -EINVAL; |
| 686 | |
| 687 | port = kzalloc(sizeof(*port), GFP_KERNEL); |
| 688 | if (port == NULL) |
| 689 | return -ENOMEM; |
| 690 | |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 691 | port->passthru = false; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 692 | port->dev = dev; |
| 693 | INIT_LIST_HEAD(&port->vlans); |
| 694 | for (i = 0; i < MACVLAN_HASH_SIZE; i++) |
| 695 | INIT_HLIST_HEAD(&port->vlan_hash[i]); |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 696 | |
Jiri Pirko | a35e2c1 | 2010-06-15 03:27:57 +0000 | [diff] [blame] | 697 | err = netdev_rx_handler_register(dev, macvlan_handle_frame, port); |
| 698 | if (err) |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 699 | kfree(port); |
Eric Dumazet | d935156 | 2011-05-20 14:59:23 -0400 | [diff] [blame] | 700 | else |
| 701 | dev->priv_flags |= IFF_MACVLAN_PORT; |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 702 | return err; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | static void macvlan_port_destroy(struct net_device *dev) |
| 706 | { |
Jiri Pirko | a35e2c1 | 2010-06-15 03:27:57 +0000 | [diff] [blame] | 707 | struct macvlan_port *port = macvlan_port_get(dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 708 | |
Jiri Pirko | a35e2c1 | 2010-06-15 03:27:57 +0000 | [diff] [blame] | 709 | dev->priv_flags &= ~IFF_MACVLAN_PORT; |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 710 | netdev_rx_handler_unregister(dev); |
Lai Jiangshan | 6e070ae | 2011-03-18 12:00:07 +0800 | [diff] [blame] | 711 | kfree_rcu(port, rcu); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 714 | static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 715 | { |
| 716 | if (tb[IFLA_ADDRESS]) { |
| 717 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 718 | return -EINVAL; |
| 719 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 720 | return -EADDRNOTAVAIL; |
| 721 | } |
Arnd Bergmann | 27c0b1a | 2009-11-26 06:07:11 +0000 | [diff] [blame] | 722 | |
| 723 | if (data && data[IFLA_MACVLAN_MODE]) { |
| 724 | switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) { |
| 725 | case MACVLAN_MODE_PRIVATE: |
| 726 | case MACVLAN_MODE_VEPA: |
| 727 | case MACVLAN_MODE_BRIDGE: |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 728 | case MACVLAN_MODE_PASSTHRU: |
Arnd Bergmann | 27c0b1a | 2009-11-26 06:07:11 +0000 | [diff] [blame] | 729 | break; |
| 730 | default: |
| 731 | return -EINVAL; |
| 732 | } |
| 733 | } |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 734 | return 0; |
| 735 | } |
| 736 | |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 737 | int macvlan_common_newlink(struct net *src_net, struct net_device *dev, |
| 738 | struct nlattr *tb[], struct nlattr *data[], |
| 739 | int (*receive)(struct sk_buff *skb), |
| 740 | int (*forward)(struct net_device *dev, |
| 741 | struct sk_buff *skb)) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 742 | { |
| 743 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 744 | struct macvlan_port *port; |
| 745 | struct net_device *lowerdev; |
| 746 | int err; |
| 747 | |
| 748 | if (!tb[IFLA_LINK]) |
| 749 | return -EINVAL; |
| 750 | |
Eric W. Biederman | 81adee4 | 2009-11-08 00:53:51 -0800 | [diff] [blame] | 751 | 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] | 752 | if (lowerdev == NULL) |
| 753 | return -ENODEV; |
| 754 | |
Eric Biederman | b0832a2 | 2009-03-13 13:15:37 -0700 | [diff] [blame] | 755 | /* When creating macvlans on top of other macvlans - use |
| 756 | * the real device as the lowerdev. |
Patrick McHardy | a6ca5f1 | 2008-01-10 22:39:28 -0800 | [diff] [blame] | 757 | */ |
Eric Biederman | b0832a2 | 2009-03-13 13:15:37 -0700 | [diff] [blame] | 758 | if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) { |
| 759 | struct macvlan_dev *lowervlan = netdev_priv(lowerdev); |
| 760 | lowerdev = lowervlan->lowerdev; |
| 761 | } |
Patrick McHardy | a6ca5f1 | 2008-01-10 22:39:28 -0800 | [diff] [blame] | 762 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 763 | if (!tb[IFLA_MTU]) |
| 764 | dev->mtu = lowerdev->mtu; |
| 765 | else if (dev->mtu > lowerdev->mtu) |
| 766 | return -EINVAL; |
| 767 | |
| 768 | if (!tb[IFLA_ADDRESS]) |
Danny Kukawka | 7ce5d22 | 2012-02-15 06:45:40 +0000 | [diff] [blame] | 769 | eth_hw_addr_random(dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 770 | |
Jiri Pirko | a35e2c1 | 2010-06-15 03:27:57 +0000 | [diff] [blame] | 771 | if (!macvlan_port_exists(lowerdev)) { |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 772 | err = macvlan_port_create(lowerdev); |
| 773 | if (err < 0) |
| 774 | return err; |
| 775 | } |
Jiri Pirko | a35e2c1 | 2010-06-15 03:27:57 +0000 | [diff] [blame] | 776 | port = macvlan_port_get(lowerdev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 777 | |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 778 | /* Only 1 macvlan device can be created in passthru mode */ |
| 779 | if (port->passthru) |
| 780 | return -EINVAL; |
| 781 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 782 | vlan->lowerdev = lowerdev; |
| 783 | vlan->dev = dev; |
| 784 | vlan->port = port; |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 785 | vlan->receive = receive; |
| 786 | vlan->forward = forward; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 787 | |
Arnd Bergmann | 27c0b1a | 2009-11-26 06:07:11 +0000 | [diff] [blame] | 788 | vlan->mode = MACVLAN_MODE_VEPA; |
| 789 | if (data && data[IFLA_MACVLAN_MODE]) |
| 790 | vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); |
| 791 | |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 792 | if (data && data[IFLA_MACVLAN_FLAGS]) |
| 793 | vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); |
| 794 | |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 795 | if (vlan->mode == MACVLAN_MODE_PASSTHRU) { |
Eric W. Biederman | d5cd9244 | 2011-03-21 18:22:22 -0700 | [diff] [blame] | 796 | if (port->count) |
Sridhar Samudrala | eb06acd | 2010-10-28 13:10:50 +0000 | [diff] [blame] | 797 | return -EINVAL; |
| 798 | port->passthru = true; |
| 799 | memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN); |
| 800 | } |
| 801 | |
Jiri Pirko | 7cd43db | 2013-01-03 22:48:50 +0000 | [diff] [blame] | 802 | err = netdev_upper_dev_link(lowerdev, dev); |
| 803 | if (err) |
| 804 | goto destroy_port; |
| 805 | |
Eric W. Biederman | d5cd9244 | 2011-03-21 18:22:22 -0700 | [diff] [blame] | 806 | port->count += 1; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 807 | err = register_netdevice(dev); |
| 808 | if (err < 0) |
Jiri Pirko | 7cd43db | 2013-01-03 22:48:50 +0000 | [diff] [blame] | 809 | goto upper_dev_unlink; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 810 | |
| 811 | list_add_tail(&vlan->list, &port->vlans); |
Patrick Mullaney | fc4a748 | 2009-12-03 15:59:22 -0800 | [diff] [blame] | 812 | netif_stacked_transfer_operstate(lowerdev, dev); |
Jiri Pirko | f16d3d5 | 2010-05-24 07:02:25 +0000 | [diff] [blame] | 813 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 814 | return 0; |
Jiri Pirko | f16d3d5 | 2010-05-24 07:02:25 +0000 | [diff] [blame] | 815 | |
Jiri Pirko | 7cd43db | 2013-01-03 22:48:50 +0000 | [diff] [blame] | 816 | upper_dev_unlink: |
| 817 | netdev_upper_dev_unlink(lowerdev, dev); |
Jiri Pirko | f16d3d5 | 2010-05-24 07:02:25 +0000 | [diff] [blame] | 818 | destroy_port: |
Eric W. Biederman | d5cd9244 | 2011-03-21 18:22:22 -0700 | [diff] [blame] | 819 | port->count -= 1; |
| 820 | if (!port->count) |
Jiri Pirko | f16d3d5 | 2010-05-24 07:02:25 +0000 | [diff] [blame] | 821 | macvlan_port_destroy(lowerdev); |
| 822 | |
| 823 | return err; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 824 | } |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 825 | EXPORT_SYMBOL_GPL(macvlan_common_newlink); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 826 | |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 827 | static int macvlan_newlink(struct net *src_net, struct net_device *dev, |
| 828 | struct nlattr *tb[], struct nlattr *data[]) |
| 829 | { |
| 830 | return macvlan_common_newlink(src_net, dev, tb, data, |
| 831 | netif_rx, |
| 832 | dev_forward_skb); |
| 833 | } |
| 834 | |
| 835 | void macvlan_dellink(struct net_device *dev, struct list_head *head) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 836 | { |
| 837 | struct macvlan_dev *vlan = netdev_priv(dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 838 | |
| 839 | list_del(&vlan->list); |
Eric Dumazet | 23289a3 | 2009-10-27 07:06:36 +0000 | [diff] [blame] | 840 | unregister_netdevice_queue(dev, head); |
Jiri Pirko | 7cd43db | 2013-01-03 22:48:50 +0000 | [diff] [blame] | 841 | netdev_upper_dev_unlink(vlan->lowerdev, dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 842 | } |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 843 | EXPORT_SYMBOL_GPL(macvlan_dellink); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 844 | |
Arnd Bergmann | 27c0b1a | 2009-11-26 06:07:11 +0000 | [diff] [blame] | 845 | static int macvlan_changelink(struct net_device *dev, |
| 846 | struct nlattr *tb[], struct nlattr *data[]) |
| 847 | { |
| 848 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 849 | if (data && data[IFLA_MACVLAN_MODE]) |
| 850 | vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]); |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 851 | if (data && data[IFLA_MACVLAN_FLAGS]) { |
| 852 | __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]); |
| 853 | bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC; |
| 854 | |
| 855 | if (promisc && (flags & MACVLAN_FLAG_NOPROMISC)) |
| 856 | dev_set_promiscuity(vlan->lowerdev, -1); |
| 857 | else if (promisc && !(flags & MACVLAN_FLAG_NOPROMISC)) |
| 858 | dev_set_promiscuity(vlan->lowerdev, 1); |
| 859 | vlan->flags = flags; |
| 860 | } |
Arnd Bergmann | 27c0b1a | 2009-11-26 06:07:11 +0000 | [diff] [blame] | 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | static size_t macvlan_get_size(const struct net_device *dev) |
| 865 | { |
Eric Dumazet | 01fe944 | 2013-01-17 13:30:49 -0800 | [diff] [blame] | 866 | return (0 |
| 867 | + nla_total_size(4) /* IFLA_MACVLAN_MODE */ |
| 868 | + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */ |
| 869 | ); |
Arnd Bergmann | 27c0b1a | 2009-11-26 06:07:11 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | static int macvlan_fill_info(struct sk_buff *skb, |
| 873 | const struct net_device *dev) |
| 874 | { |
| 875 | struct macvlan_dev *vlan = netdev_priv(dev); |
| 876 | |
David S. Miller | ead9a76 | 2012-04-01 20:23:06 -0400 | [diff] [blame] | 877 | if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode)) |
| 878 | goto nla_put_failure; |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 879 | if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags)) |
| 880 | goto nla_put_failure; |
Arnd Bergmann | 27c0b1a | 2009-11-26 06:07:11 +0000 | [diff] [blame] | 881 | return 0; |
| 882 | |
| 883 | nla_put_failure: |
| 884 | return -EMSGSIZE; |
| 885 | } |
| 886 | |
| 887 | static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = { |
John Fastabend | df8ef8f | 2012-04-15 06:44:37 +0000 | [diff] [blame] | 888 | [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, |
| 889 | [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 }, |
Arnd Bergmann | 27c0b1a | 2009-11-26 06:07:11 +0000 | [diff] [blame] | 890 | }; |
| 891 | |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 892 | int macvlan_link_register(struct rtnl_link_ops *ops) |
| 893 | { |
| 894 | /* common fields */ |
| 895 | ops->priv_size = sizeof(struct macvlan_dev); |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 896 | ops->validate = macvlan_validate; |
| 897 | ops->maxtype = IFLA_MACVLAN_MAX; |
| 898 | ops->policy = macvlan_policy; |
| 899 | ops->changelink = macvlan_changelink; |
| 900 | ops->get_size = macvlan_get_size; |
| 901 | ops->fill_info = macvlan_fill_info; |
| 902 | |
| 903 | return rtnl_link_register(ops); |
| 904 | }; |
| 905 | EXPORT_SYMBOL_GPL(macvlan_link_register); |
| 906 | |
| 907 | static struct rtnl_link_ops macvlan_link_ops = { |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 908 | .kind = "macvlan", |
Herbert Xu | 8a35747 | 2010-07-21 21:44:31 +0000 | [diff] [blame] | 909 | .setup = macvlan_setup, |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 910 | .newlink = macvlan_newlink, |
| 911 | .dellink = macvlan_dellink, |
| 912 | }; |
| 913 | |
| 914 | static int macvlan_device_event(struct notifier_block *unused, |
| 915 | unsigned long event, void *ptr) |
| 916 | { |
| 917 | struct net_device *dev = ptr; |
| 918 | struct macvlan_dev *vlan, *next; |
| 919 | struct macvlan_port *port; |
Eric Dumazet | 226bd34 | 2011-05-08 23:17:57 +0000 | [diff] [blame] | 920 | LIST_HEAD(list_kill); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 921 | |
Jiri Pirko | a35e2c1 | 2010-06-15 03:27:57 +0000 | [diff] [blame] | 922 | if (!macvlan_port_exists(dev)) |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 923 | return NOTIFY_DONE; |
| 924 | |
Jiri Pirko | a35e2c1 | 2010-06-15 03:27:57 +0000 | [diff] [blame] | 925 | port = macvlan_port_get(dev); |
| 926 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 927 | switch (event) { |
| 928 | case NETDEV_CHANGE: |
| 929 | list_for_each_entry(vlan, &port->vlans, list) |
Patrick Mullaney | fc4a748 | 2009-12-03 15:59:22 -0800 | [diff] [blame] | 930 | netif_stacked_transfer_operstate(vlan->lowerdev, |
| 931 | vlan->dev); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 932 | break; |
| 933 | case NETDEV_FEAT_CHANGE: |
| 934 | list_for_each_entry(vlan, &port->vlans, list) { |
| 935 | vlan->dev->features = dev->features & MACVLAN_FEATURES; |
Patrick McHardy | 8c2acc5 | 2009-11-23 14:18:53 -0800 | [diff] [blame] | 936 | vlan->dev->gso_max_size = dev->gso_max_size; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 937 | netdev_features_change(vlan->dev); |
| 938 | } |
| 939 | break; |
| 940 | case NETDEV_UNREGISTER: |
David Lamparter | 3b27e10 | 2010-09-17 03:22:19 +0000 | [diff] [blame] | 941 | /* twiddle thumbs on netns device moves */ |
| 942 | if (dev->reg_state != NETREG_UNREGISTERING) |
| 943 | break; |
| 944 | |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 945 | list_for_each_entry_safe(vlan, next, &port->vlans, list) |
Eric Dumazet | 226bd34 | 2011-05-08 23:17:57 +0000 | [diff] [blame] | 946 | vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill); |
| 947 | unregister_netdevice_many(&list_kill); |
| 948 | list_del(&list_kill); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 949 | break; |
Jiri Pirko | 1c01fe1 | 2010-03-10 10:30:19 +0000 | [diff] [blame] | 950 | case NETDEV_PRE_TYPE_CHANGE: |
| 951 | /* Forbid underlaying device to change its type. */ |
| 952 | return NOTIFY_BAD; |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 953 | } |
| 954 | return NOTIFY_DONE; |
| 955 | } |
| 956 | |
| 957 | static struct notifier_block macvlan_notifier_block __read_mostly = { |
| 958 | .notifier_call = macvlan_device_event, |
| 959 | }; |
| 960 | |
| 961 | static int __init macvlan_init_module(void) |
| 962 | { |
| 963 | int err; |
| 964 | |
| 965 | register_netdevice_notifier(&macvlan_notifier_block); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 966 | |
Arnd Bergmann | fc0663d | 2010-01-30 12:23:40 +0000 | [diff] [blame] | 967 | err = macvlan_link_register(&macvlan_link_ops); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 968 | if (err < 0) |
| 969 | goto err1; |
| 970 | return 0; |
| 971 | err1: |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 972 | unregister_netdevice_notifier(&macvlan_notifier_block); |
| 973 | return err; |
| 974 | } |
| 975 | |
| 976 | static void __exit macvlan_cleanup_module(void) |
| 977 | { |
| 978 | rtnl_link_unregister(&macvlan_link_ops); |
Patrick McHardy | b863ceb | 2007-07-14 18:55:06 -0700 | [diff] [blame] | 979 | unregister_netdevice_notifier(&macvlan_notifier_block); |
| 980 | } |
| 981 | |
| 982 | module_init(macvlan_init_module); |
| 983 | module_exit(macvlan_cleanup_module); |
| 984 | |
| 985 | MODULE_LICENSE("GPL"); |
| 986 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 987 | MODULE_DESCRIPTION("Driver for MAC address based VLANs"); |
| 988 | MODULE_ALIAS_RTNL_LINK("macvlan"); |