blob: 18b4e8c7f68aa1824838f583f6d03fd19705f8a1 [file] [log] [blame]
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001/* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
7 *
8 */
9
10#include "ipvlan.h"
11
Mahesh Bandewarab5b7012016-02-20 19:31:41 -080012static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080013{
14 ipvlan->dev->mtu = dev->mtu - ipvlan->mtu_adj;
15}
16
Mahesh Bandeware93fbc52016-02-20 19:31:36 -080017static void ipvlan_set_port_mode(struct ipvl_port *port, u16 nval)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080018{
19 struct ipvl_dev *ipvlan;
20
21 if (port->mode != nval) {
22 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
23 if (nval == IPVLAN_MODE_L3)
24 ipvlan->dev->flags |= IFF_NOARP;
25 else
26 ipvlan->dev->flags &= ~IFF_NOARP;
27 }
28 port->mode = nval;
29 }
30}
31
32static int ipvlan_port_create(struct net_device *dev)
33{
34 struct ipvl_port *port;
35 int err, idx;
36
37 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) {
38 netdev_err(dev, "Master is either lo or non-ether device\n");
39 return -EINVAL;
40 }
Mahesh Bandewar764e4332014-12-06 15:53:19 -080041
42 if (netif_is_macvlan_port(dev)) {
43 netdev_err(dev, "Master is a macvlan port.\n");
44 return -EBUSY;
45 }
46
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080047 port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
48 if (!port)
49 return -ENOMEM;
50
51 port->dev = dev;
52 port->mode = IPVLAN_MODE_L3;
53 INIT_LIST_HEAD(&port->ipvlans);
54 for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
55 INIT_HLIST_HEAD(&port->hlhead[idx]);
56
Mahesh Bandewarba35f852015-05-04 17:06:03 -070057 skb_queue_head_init(&port->backlog);
58 INIT_WORK(&port->wq, ipvlan_process_multicast);
59
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080060 err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
61 if (err)
62 goto err;
63
64 dev->priv_flags |= IFF_IPVLAN_MASTER;
65 return 0;
66
67err:
68 kfree_rcu(port, rcu);
69 return err;
70}
71
72static void ipvlan_port_destroy(struct net_device *dev)
73{
74 struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
75
76 dev->priv_flags &= ~IFF_IPVLAN_MASTER;
77 netdev_rx_handler_unregister(dev);
Mahesh Bandewarba35f852015-05-04 17:06:03 -070078 cancel_work_sync(&port->wq);
79 __skb_queue_purge(&port->backlog);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080080 kfree_rcu(port, rcu);
81}
82
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080083#define IPVLAN_FEATURES \
Tom Herberta1882222015-12-14 11:19:43 -080084 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080085 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
86 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
87 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
88
89#define IPVLAN_STATE_MASK \
90 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
91
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080092static int ipvlan_init(struct net_device *dev)
93{
94 struct ipvl_dev *ipvlan = netdev_priv(dev);
95 const struct net_device *phy_dev = ipvlan->phy_dev;
Mahesh Bandewar494e8482016-04-27 14:59:27 -070096 struct ipvl_port *port = ipvlan->port;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080097
98 dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
99 (phy_dev->state & IPVLAN_STATE_MASK);
100 dev->features = phy_dev->features & IPVLAN_FEATURES;
101 dev->features |= NETIF_F_LLTX;
102 dev->gso_max_size = phy_dev->gso_max_size;
Eric Dumazetf6773c52016-03-16 21:59:49 -0700103 dev->gso_max_segs = phy_dev->gso_max_segs;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800104 dev->hard_header_len = phy_dev->hard_header_len;
105
Eric Dumazet0d7dd792016-06-09 07:45:15 -0700106 netdev_lockdep_set_classes(dev);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800107
108 ipvlan->pcpu_stats = alloc_percpu(struct ipvl_pcpu_stats);
109 if (!ipvlan->pcpu_stats)
110 return -ENOMEM;
111
Mahesh Bandewar494e8482016-04-27 14:59:27 -0700112 port->count += 1;
113
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800114 return 0;
115}
116
117static void ipvlan_uninit(struct net_device *dev)
118{
119 struct ipvl_dev *ipvlan = netdev_priv(dev);
120 struct ipvl_port *port = ipvlan->port;
121
Markus Elfring04901ce2014-11-29 16:23:20 +0100122 free_percpu(ipvlan->pcpu_stats);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800123
124 port->count -= 1;
125 if (!port->count)
126 ipvlan_port_destroy(port->dev);
127}
128
129static int ipvlan_open(struct net_device *dev)
130{
131 struct ipvl_dev *ipvlan = netdev_priv(dev);
132 struct net_device *phy_dev = ipvlan->phy_dev;
133 struct ipvl_addr *addr;
134
135 if (ipvlan->port->mode == IPVLAN_MODE_L3)
136 dev->flags |= IFF_NOARP;
137 else
138 dev->flags &= ~IFF_NOARP;
139
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300140 list_for_each_entry(addr, &ipvlan->addrs, anode)
141 ipvlan_ht_addr_add(ipvlan, addr);
142
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800143 return dev_uc_add(phy_dev, phy_dev->dev_addr);
144}
145
146static int ipvlan_stop(struct net_device *dev)
147{
148 struct ipvl_dev *ipvlan = netdev_priv(dev);
149 struct net_device *phy_dev = ipvlan->phy_dev;
150 struct ipvl_addr *addr;
151
152 dev_uc_unsync(phy_dev, dev);
153 dev_mc_unsync(phy_dev, dev);
154
155 dev_uc_del(phy_dev, phy_dev->dev_addr);
156
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300157 list_for_each_entry(addr, &ipvlan->addrs, anode)
Konstantin Khlebnikov6640e672015-07-14 16:35:53 +0300158 ipvlan_ht_addr_del(addr);
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300159
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800160 return 0;
161}
162
Mahesh Bandewar92c7b0d2014-11-25 21:24:43 -0800163static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
164 struct net_device *dev)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800165{
166 const struct ipvl_dev *ipvlan = netdev_priv(dev);
167 int skblen = skb->len;
168 int ret;
169
170 ret = ipvlan_queue_xmit(skb, dev);
171 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
172 struct ipvl_pcpu_stats *pcptr;
173
174 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
175
176 u64_stats_update_begin(&pcptr->syncp);
177 pcptr->tx_pkts++;
178 pcptr->tx_bytes += skblen;
179 u64_stats_update_end(&pcptr->syncp);
180 } else {
181 this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
182 }
183 return ret;
184}
185
186static netdev_features_t ipvlan_fix_features(struct net_device *dev,
187 netdev_features_t features)
188{
189 struct ipvl_dev *ipvlan = netdev_priv(dev);
190
191 return features & (ipvlan->sfeatures | ~IPVLAN_FEATURES);
192}
193
194static void ipvlan_change_rx_flags(struct net_device *dev, int change)
195{
196 struct ipvl_dev *ipvlan = netdev_priv(dev);
197 struct net_device *phy_dev = ipvlan->phy_dev;
198
199 if (change & IFF_ALLMULTI)
200 dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
201}
202
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800203static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
204{
205 struct ipvl_dev *ipvlan = netdev_priv(dev);
206
207 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
208 bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
209 } else {
210 struct netdev_hw_addr *ha;
211 DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
212
213 bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
214 netdev_for_each_mc_addr(ha, dev)
215 __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
216
Mahesh Bandewarf631c442015-05-04 17:06:11 -0700217 /* Turn-on broadcast bit irrespective of address family,
218 * since broadcast is deferred to a work-queue, hence no
219 * impact on fast-path processing.
220 */
221 __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
222
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800223 bitmap_copy(ipvlan->mac_filters, mc_filters,
224 IPVLAN_MAC_FILTER_SIZE);
225 }
226 dev_uc_sync(ipvlan->phy_dev, dev);
227 dev_mc_sync(ipvlan->phy_dev, dev);
228}
229
230static struct rtnl_link_stats64 *ipvlan_get_stats64(struct net_device *dev,
231 struct rtnl_link_stats64 *s)
232{
233 struct ipvl_dev *ipvlan = netdev_priv(dev);
234
235 if (ipvlan->pcpu_stats) {
236 struct ipvl_pcpu_stats *pcptr;
237 u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
238 u32 rx_errs = 0, tx_drps = 0;
239 u32 strt;
240 int idx;
241
242 for_each_possible_cpu(idx) {
243 pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
244 do {
245 strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
246 rx_pkts = pcptr->rx_pkts;
247 rx_bytes = pcptr->rx_bytes;
248 rx_mcast = pcptr->rx_mcast;
249 tx_pkts = pcptr->tx_pkts;
250 tx_bytes = pcptr->tx_bytes;
251 } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
252 strt));
253
254 s->rx_packets += rx_pkts;
255 s->rx_bytes += rx_bytes;
256 s->multicast += rx_mcast;
257 s->tx_packets += tx_pkts;
258 s->tx_bytes += tx_bytes;
259
260 /* u32 values are updated without syncp protection. */
261 rx_errs += pcptr->rx_errs;
262 tx_drps += pcptr->tx_drps;
263 }
264 s->rx_errors = rx_errs;
265 s->rx_dropped = rx_errs;
266 s->tx_dropped = tx_drps;
267 }
268 return s;
269}
270
271static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
272{
273 struct ipvl_dev *ipvlan = netdev_priv(dev);
274 struct net_device *phy_dev = ipvlan->phy_dev;
275
276 return vlan_vid_add(phy_dev, proto, vid);
277}
278
279static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
280 u16 vid)
281{
282 struct ipvl_dev *ipvlan = netdev_priv(dev);
283 struct net_device *phy_dev = ipvlan->phy_dev;
284
285 vlan_vid_del(phy_dev, proto, vid);
286 return 0;
287}
288
Nicolas Dichtel7c411652015-04-02 17:07:06 +0200289static int ipvlan_get_iflink(const struct net_device *dev)
290{
291 struct ipvl_dev *ipvlan = netdev_priv(dev);
292
293 return ipvlan->phy_dev->ifindex;
294}
295
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800296static const struct net_device_ops ipvlan_netdev_ops = {
297 .ndo_init = ipvlan_init,
298 .ndo_uninit = ipvlan_uninit,
299 .ndo_open = ipvlan_open,
300 .ndo_stop = ipvlan_stop,
301 .ndo_start_xmit = ipvlan_start_xmit,
302 .ndo_fix_features = ipvlan_fix_features,
303 .ndo_change_rx_flags = ipvlan_change_rx_flags,
304 .ndo_set_rx_mode = ipvlan_set_multicast_mac_filter,
305 .ndo_get_stats64 = ipvlan_get_stats64,
306 .ndo_vlan_rx_add_vid = ipvlan_vlan_rx_add_vid,
307 .ndo_vlan_rx_kill_vid = ipvlan_vlan_rx_kill_vid,
Nicolas Dichtel7c411652015-04-02 17:07:06 +0200308 .ndo_get_iflink = ipvlan_get_iflink,
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800309};
310
311static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
312 unsigned short type, const void *daddr,
313 const void *saddr, unsigned len)
314{
315 const struct ipvl_dev *ipvlan = netdev_priv(dev);
316 struct net_device *phy_dev = ipvlan->phy_dev;
317
318 /* TODO Probably use a different field than dev_addr so that the
319 * mac-address on the virtual device is portable and can be carried
320 * while the packets use the mac-addr on the physical device.
321 */
322 return dev_hard_header(skb, phy_dev, type, daddr,
323 saddr ? : dev->dev_addr, len);
324}
325
326static const struct header_ops ipvlan_header_ops = {
327 .create = ipvlan_hard_header,
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800328 .parse = eth_header_parse,
329 .cache = eth_header_cache,
330 .cache_update = eth_header_cache_update,
331};
332
David Decotigny314d10d2016-02-24 10:58:03 -0800333static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
334 struct ethtool_link_ksettings *cmd)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800335{
336 const struct ipvl_dev *ipvlan = netdev_priv(dev);
337
David Decotigny314d10d2016-02-24 10:58:03 -0800338 return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800339}
340
341static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
342 struct ethtool_drvinfo *drvinfo)
343{
344 strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
345 strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
346}
347
348static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
349{
350 const struct ipvl_dev *ipvlan = netdev_priv(dev);
351
352 return ipvlan->msg_enable;
353}
354
355static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
356{
357 struct ipvl_dev *ipvlan = netdev_priv(dev);
358
359 ipvlan->msg_enable = value;
360}
361
362static const struct ethtool_ops ipvlan_ethtool_ops = {
363 .get_link = ethtool_op_get_link,
David Decotigny314d10d2016-02-24 10:58:03 -0800364 .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800365 .get_drvinfo = ipvlan_ethtool_get_drvinfo,
366 .get_msglevel = ipvlan_ethtool_get_msglevel,
367 .set_msglevel = ipvlan_ethtool_set_msglevel,
368};
369
370static int ipvlan_nl_changelink(struct net_device *dev,
371 struct nlattr *tb[], struct nlattr *data[])
372{
373 struct ipvl_dev *ipvlan = netdev_priv(dev);
374 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
375
376 if (data && data[IFLA_IPVLAN_MODE]) {
377 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
378
379 ipvlan_set_port_mode(port, nmode);
380 }
381 return 0;
382}
383
384static size_t ipvlan_nl_getsize(const struct net_device *dev)
385{
386 return (0
387 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
388 );
389}
390
391static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[])
392{
393 if (data && data[IFLA_IPVLAN_MODE]) {
394 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
395
396 if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
397 return -EINVAL;
398 }
399 return 0;
400}
401
402static int ipvlan_nl_fillinfo(struct sk_buff *skb,
403 const struct net_device *dev)
404{
405 struct ipvl_dev *ipvlan = netdev_priv(dev);
406 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
407 int ret = -EINVAL;
408
409 if (!port)
410 goto err;
411
412 ret = -EMSGSIZE;
413 if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
414 goto err;
415
416 return 0;
417
418err:
419 return ret;
420}
421
422static int ipvlan_link_new(struct net *src_net, struct net_device *dev,
423 struct nlattr *tb[], struct nlattr *data[])
424{
425 struct ipvl_dev *ipvlan = netdev_priv(dev);
426 struct ipvl_port *port;
427 struct net_device *phy_dev;
428 int err;
Mahesh Bandeware93fbc52016-02-20 19:31:36 -0800429 u16 mode = IPVLAN_MODE_L3;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800430
431 if (!tb[IFLA_LINK])
432 return -EINVAL;
433
434 phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
435 if (!phy_dev)
436 return -ENODEV;
437
Mahesh Bandewar5933fea2014-12-06 15:53:33 -0800438 if (netif_is_ipvlan(phy_dev)) {
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800439 struct ipvl_dev *tmp = netdev_priv(phy_dev);
440
441 phy_dev = tmp->phy_dev;
Mahesh Bandewar5933fea2014-12-06 15:53:33 -0800442 } else if (!netif_is_ipvlan_port(phy_dev)) {
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800443 err = ipvlan_port_create(phy_dev);
444 if (err < 0)
445 return err;
446 }
447
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800448 if (data && data[IFLA_IPVLAN_MODE])
Mahesh Bandeware93fbc52016-02-20 19:31:36 -0800449 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800450
Mahesh Bandeware93fbc52016-02-20 19:31:36 -0800451 port = ipvlan_port_get_rtnl(phy_dev);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800452 ipvlan->phy_dev = phy_dev;
453 ipvlan->dev = dev;
454 ipvlan->port = port;
455 ipvlan->sfeatures = IPVLAN_FEATURES;
Mahesh Bandewar296d4852016-01-27 23:33:28 -0800456 ipvlan_adjust_mtu(ipvlan, phy_dev);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800457 INIT_LIST_HEAD(&ipvlan->addrs);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800458
459 /* TODO Probably put random address here to be presented to the
460 * world but keep using the physical-dev address for the outgoing
461 * packets.
462 */
463 memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
464
465 dev->priv_flags |= IFF_IPVLAN_SLAVE;
466
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800467 err = register_netdevice(dev);
468 if (err < 0)
Mahesh Bandewar494e8482016-04-27 14:59:27 -0700469 return err;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800470
471 err = netdev_upper_dev_link(phy_dev, dev);
Mahesh Bandewar494e8482016-04-27 14:59:27 -0700472 if (err) {
473 unregister_netdevice(dev);
474 return err;
475 }
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800476
477 list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
Mahesh Bandeware93fbc52016-02-20 19:31:36 -0800478 ipvlan_set_port_mode(port, mode);
479
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800480 netif_stacked_transfer_operstate(phy_dev, dev);
481 return 0;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800482}
483
484static void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
485{
486 struct ipvl_dev *ipvlan = netdev_priv(dev);
487 struct ipvl_addr *addr, *next;
488
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300489 list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
Konstantin Khlebnikov6640e672015-07-14 16:35:53 +0300490 ipvlan_ht_addr_del(addr);
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300491 list_del(&addr->anode);
Konstantin Khlebnikov6a725492015-07-14 16:35:51 +0300492 kfree_rcu(addr, rcu);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800493 }
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300494
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800495 list_del_rcu(&ipvlan->pnode);
496 unregister_netdevice_queue(dev, head);
497 netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
498}
499
500static void ipvlan_link_setup(struct net_device *dev)
501{
502 ether_setup(dev);
503
504 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
Phil Sutterbf485bc2015-08-18 10:30:40 +0200505 dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800506 dev->netdev_ops = &ipvlan_netdev_ops;
507 dev->destructor = free_netdev;
508 dev->header_ops = &ipvlan_header_ops;
509 dev->ethtool_ops = &ipvlan_ethtool_ops;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800510}
511
512static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
513{
514 [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
515};
516
517static struct rtnl_link_ops ipvlan_link_ops = {
518 .kind = "ipvlan",
519 .priv_size = sizeof(struct ipvl_dev),
520
521 .get_size = ipvlan_nl_getsize,
522 .policy = ipvlan_nl_policy,
523 .validate = ipvlan_nl_validate,
524 .fill_info = ipvlan_nl_fillinfo,
525 .changelink = ipvlan_nl_changelink,
526 .maxtype = IFLA_IPVLAN_MAX,
527
528 .setup = ipvlan_link_setup,
529 .newlink = ipvlan_link_new,
530 .dellink = ipvlan_link_delete,
531};
532
Mahesh Bandewar92c7b0d2014-11-25 21:24:43 -0800533static int ipvlan_link_register(struct rtnl_link_ops *ops)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800534{
535 return rtnl_link_register(ops);
536}
537
538static int ipvlan_device_event(struct notifier_block *unused,
539 unsigned long event, void *ptr)
540{
541 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
542 struct ipvl_dev *ipvlan, *next;
543 struct ipvl_port *port;
544 LIST_HEAD(lst_kill);
545
Mahesh Bandewar5933fea2014-12-06 15:53:33 -0800546 if (!netif_is_ipvlan_port(dev))
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800547 return NOTIFY_DONE;
548
549 port = ipvlan_port_get_rtnl(dev);
550
551 switch (event) {
552 case NETDEV_CHANGE:
553 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
554 netif_stacked_transfer_operstate(ipvlan->phy_dev,
555 ipvlan->dev);
556 break;
557
558 case NETDEV_UNREGISTER:
559 if (dev->reg_state != NETREG_UNREGISTERING)
560 break;
561
562 list_for_each_entry_safe(ipvlan, next, &port->ipvlans,
563 pnode)
564 ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
565 &lst_kill);
566 unregister_netdevice_many(&lst_kill);
567 break;
568
569 case NETDEV_FEAT_CHANGE:
570 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
571 ipvlan->dev->features = dev->features & IPVLAN_FEATURES;
572 ipvlan->dev->gso_max_size = dev->gso_max_size;
Eric Dumazetf6773c52016-03-16 21:59:49 -0700573 ipvlan->dev->gso_max_segs = dev->gso_max_segs;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800574 netdev_features_change(ipvlan->dev);
575 }
576 break;
577
578 case NETDEV_CHANGEMTU:
579 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
580 ipvlan_adjust_mtu(ipvlan, dev);
581 break;
582
583 case NETDEV_PRE_TYPE_CHANGE:
584 /* Forbid underlying device to change its type. */
585 return NOTIFY_BAD;
586 }
587 return NOTIFY_DONE;
588}
589
590static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
591{
592 struct ipvl_addr *addr;
593
Jiri Bence9997c22015-03-28 19:13:25 +0100594 if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true)) {
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800595 netif_err(ipvlan, ifup, ipvlan->dev,
596 "Failed to add IPv6=%pI6c addr for %s intf\n",
597 ip6_addr, ipvlan->dev->name);
598 return -EINVAL;
599 }
600 addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
601 if (!addr)
602 return -ENOMEM;
603
604 addr->master = ipvlan;
605 memcpy(&addr->ip6addr, ip6_addr, sizeof(struct in6_addr));
606 addr->atype = IPVL_IPV6;
Jiri Benc40891e82015-03-28 19:13:24 +0100607 list_add_tail(&addr->anode, &ipvlan->addrs);
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300608
Jiri Benc27705f72015-03-28 19:13:22 +0100609 /* If the interface is not up, the address will be added to the hash
610 * list by ipvlan_open.
611 */
612 if (netif_running(ipvlan->dev))
613 ipvlan_ht_addr_add(ipvlan, addr);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800614
615 return 0;
616}
617
618static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
619{
620 struct ipvl_addr *addr;
621
Jiri Bence9997c22015-03-28 19:13:25 +0100622 addr = ipvlan_find_addr(ipvlan, ip6_addr, true);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800623 if (!addr)
624 return;
625
Konstantin Khlebnikov6640e672015-07-14 16:35:53 +0300626 ipvlan_ht_addr_del(addr);
Jiri Benc40891e82015-03-28 19:13:24 +0100627 list_del(&addr->anode);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800628 kfree_rcu(addr, rcu);
629
630 return;
631}
632
633static int ipvlan_addr6_event(struct notifier_block *unused,
634 unsigned long event, void *ptr)
635{
636 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
637 struct net_device *dev = (struct net_device *)if6->idev->dev;
638 struct ipvl_dev *ipvlan = netdev_priv(dev);
639
Konstantin Khlebnikov23a5a492015-07-14 16:35:55 +0300640 /* FIXME IPv6 autoconf calls us from bh without RTNL */
641 if (in_softirq())
642 return NOTIFY_DONE;
643
Mahesh Bandewar5933fea2014-12-06 15:53:33 -0800644 if (!netif_is_ipvlan(dev))
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800645 return NOTIFY_DONE;
646
647 if (!ipvlan || !ipvlan->port)
648 return NOTIFY_DONE;
649
650 switch (event) {
651 case NETDEV_UP:
652 if (ipvlan_add_addr6(ipvlan, &if6->addr))
653 return NOTIFY_BAD;
654 break;
655
656 case NETDEV_DOWN:
657 ipvlan_del_addr6(ipvlan, &if6->addr);
658 break;
659 }
660
661 return NOTIFY_OK;
662}
663
664static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
665{
666 struct ipvl_addr *addr;
667
Jiri Bence9997c22015-03-28 19:13:25 +0100668 if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false)) {
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800669 netif_err(ipvlan, ifup, ipvlan->dev,
670 "Failed to add IPv4=%pI4 on %s intf.\n",
671 ip4_addr, ipvlan->dev->name);
672 return -EINVAL;
673 }
674 addr = kzalloc(sizeof(struct ipvl_addr), GFP_KERNEL);
675 if (!addr)
676 return -ENOMEM;
677
678 addr->master = ipvlan;
679 memcpy(&addr->ip4addr, ip4_addr, sizeof(struct in_addr));
680 addr->atype = IPVL_IPV4;
Jiri Benc40891e82015-03-28 19:13:24 +0100681 list_add_tail(&addr->anode, &ipvlan->addrs);
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300682
Jiri Benc27705f72015-03-28 19:13:22 +0100683 /* If the interface is not up, the address will be added to the hash
684 * list by ipvlan_open.
685 */
686 if (netif_running(ipvlan->dev))
687 ipvlan_ht_addr_add(ipvlan, addr);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800688
689 return 0;
690}
691
692static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
693{
694 struct ipvl_addr *addr;
695
Jiri Bence9997c22015-03-28 19:13:25 +0100696 addr = ipvlan_find_addr(ipvlan, ip4_addr, false);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800697 if (!addr)
698 return;
699
Konstantin Khlebnikov6640e672015-07-14 16:35:53 +0300700 ipvlan_ht_addr_del(addr);
Jiri Benc40891e82015-03-28 19:13:24 +0100701 list_del(&addr->anode);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800702 kfree_rcu(addr, rcu);
703
704 return;
705}
706
707static int ipvlan_addr4_event(struct notifier_block *unused,
708 unsigned long event, void *ptr)
709{
710 struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
711 struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
712 struct ipvl_dev *ipvlan = netdev_priv(dev);
713 struct in_addr ip4_addr;
714
Mahesh Bandewar5933fea2014-12-06 15:53:33 -0800715 if (!netif_is_ipvlan(dev))
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800716 return NOTIFY_DONE;
717
718 if (!ipvlan || !ipvlan->port)
719 return NOTIFY_DONE;
720
721 switch (event) {
722 case NETDEV_UP:
723 ip4_addr.s_addr = if4->ifa_address;
724 if (ipvlan_add_addr4(ipvlan, &ip4_addr))
725 return NOTIFY_BAD;
726 break;
727
728 case NETDEV_DOWN:
729 ip4_addr.s_addr = if4->ifa_address;
730 ipvlan_del_addr4(ipvlan, &ip4_addr);
731 break;
732 }
733
734 return NOTIFY_OK;
735}
736
737static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
738 .notifier_call = ipvlan_addr4_event,
739};
740
741static struct notifier_block ipvlan_notifier_block __read_mostly = {
742 .notifier_call = ipvlan_device_event,
743};
744
745static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
746 .notifier_call = ipvlan_addr6_event,
747};
748
749static int __init ipvlan_init_module(void)
750{
751 int err;
752
753 ipvlan_init_secret();
754 register_netdevice_notifier(&ipvlan_notifier_block);
755 register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
756 register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
757
758 err = ipvlan_link_register(&ipvlan_link_ops);
759 if (err < 0)
760 goto error;
761
762 return 0;
763error:
764 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
765 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
766 unregister_netdevice_notifier(&ipvlan_notifier_block);
767 return err;
768}
769
770static void __exit ipvlan_cleanup_module(void)
771{
772 rtnl_link_unregister(&ipvlan_link_ops);
773 unregister_netdevice_notifier(&ipvlan_notifier_block);
774 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
775 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
776}
777
778module_init(ipvlan_init_module);
779module_exit(ipvlan_cleanup_module);
780
781MODULE_LICENSE("GPL");
782MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
783MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
784MODULE_ALIAS_RTNL_LINK("ipvlan");