blob: 4cbe9e27287d95e33d745c6551d3af26601578ae [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
Florian Westphal31338222017-04-20 18:08:15 +020012static unsigned int ipvlan_netid __read_mostly;
13
14struct ipvlan_netns {
15 unsigned int ipvl_nf_hook_refcnt;
16};
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070017
Florian Westphal591bb272017-07-26 11:40:52 +020018static const struct nf_hook_ops ipvl_nfops[] = {
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070019 {
20 .hook = ipvlan_nf_input,
21 .pf = NFPROTO_IPV4,
22 .hooknum = NF_INET_LOCAL_IN,
23 .priority = INT_MAX,
24 },
Matteo Croce94333fa2018-02-21 01:31:13 +010025#if IS_ENABLED(CONFIG_IPV6)
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070026 {
27 .hook = ipvlan_nf_input,
28 .pf = NFPROTO_IPV6,
29 .hooknum = NF_INET_LOCAL_IN,
30 .priority = INT_MAX,
31 },
Matteo Croce94333fa2018-02-21 01:31:13 +010032#endif
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070033};
34
Julia Lawallab530f62016-10-15 17:40:30 +020035static const struct l3mdev_ops ipvl_l3mdev_ops = {
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070036 .l3mdev_l3_rcv = ipvlan_l3_rcv,
37};
38
Mahesh Bandewarab5b7012016-02-20 19:31:41 -080039static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080040{
Gao Feng8f679ed2016-11-30 08:48:44 +080041 ipvlan->dev->mtu = dev->mtu;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080042}
43
Florian Westphal31338222017-04-20 18:08:15 +020044static int ipvlan_register_nf_hook(struct net *net)
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070045{
Florian Westphal31338222017-04-20 18:08:15 +020046 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070047 int err = 0;
48
Florian Westphal31338222017-04-20 18:08:15 +020049 if (!vnet->ipvl_nf_hook_refcnt) {
50 err = nf_register_net_hooks(net, ipvl_nfops,
51 ARRAY_SIZE(ipvl_nfops));
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070052 if (!err)
Florian Westphal31338222017-04-20 18:08:15 +020053 vnet->ipvl_nf_hook_refcnt = 1;
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070054 } else {
Florian Westphal31338222017-04-20 18:08:15 +020055 vnet->ipvl_nf_hook_refcnt++;
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070056 }
57
58 return err;
59}
60
Florian Westphal31338222017-04-20 18:08:15 +020061static void ipvlan_unregister_nf_hook(struct net *net)
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070062{
Florian Westphal31338222017-04-20 18:08:15 +020063 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070064
Florian Westphal31338222017-04-20 18:08:15 +020065 if (WARN_ON(!vnet->ipvl_nf_hook_refcnt))
66 return;
67
68 vnet->ipvl_nf_hook_refcnt--;
69 if (!vnet->ipvl_nf_hook_refcnt)
70 nf_unregister_net_hooks(net, ipvl_nfops,
71 ARRAY_SIZE(ipvl_nfops));
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070072}
73
74static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080075{
76 struct ipvl_dev *ipvlan;
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070077 struct net_device *mdev = port->dev;
78 int err = 0;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080079
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070080 ASSERT_RTNL();
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080081 if (port->mode != nval) {
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070082 if (nval == IPVLAN_MODE_L3S) {
83 /* New mode is L3S */
Florian Westphal31338222017-04-20 18:08:15 +020084 err = ipvlan_register_nf_hook(read_pnet(&port->pnet));
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070085 if (!err) {
86 mdev->l3mdev_ops = &ipvl_l3mdev_ops;
87 mdev->priv_flags |= IFF_L3MDEV_MASTER;
88 } else
89 return err;
90 } else if (port->mode == IPVLAN_MODE_L3S) {
91 /* Old mode was L3S */
92 mdev->priv_flags &= ~IFF_L3MDEV_MASTER;
Florian Westphal31338222017-04-20 18:08:15 +020093 ipvlan_unregister_nf_hook(read_pnet(&port->pnet));
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070094 mdev->l3mdev_ops = NULL;
95 }
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080096 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -070097 if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -080098 ipvlan->dev->flags |= IFF_NOARP;
99 else
100 ipvlan->dev->flags &= ~IFF_NOARP;
101 }
102 port->mode = nval;
103 }
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700104 return err;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800105}
106
107static int ipvlan_port_create(struct net_device *dev)
108{
109 struct ipvl_port *port;
110 int err, idx;
111
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800112 port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
113 if (!port)
114 return -ENOMEM;
115
Florian Westphal31338222017-04-20 18:08:15 +0200116 write_pnet(&port->pnet, dev_net(dev));
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800117 port->dev = dev;
118 port->mode = IPVLAN_MODE_L3;
119 INIT_LIST_HEAD(&port->ipvlans);
120 for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
121 INIT_HLIST_HEAD(&port->hlhead[idx]);
122
Mahesh Bandewarba35f852015-05-04 17:06:03 -0700123 skb_queue_head_init(&port->backlog);
124 INIT_WORK(&port->wq, ipvlan_process_multicast);
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800125 ida_init(&port->ida);
Mahesh Bandewarda36e132017-01-09 15:05:54 -0800126 port->dev_id_start = 1;
Mahesh Bandewarba35f852015-05-04 17:06:03 -0700127
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800128 err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
129 if (err)
130 goto err;
131
132 dev->priv_flags |= IFF_IPVLAN_MASTER;
133 return 0;
134
135err:
Gao Feng48140a22016-12-07 08:44:47 +0800136 kfree(port);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800137 return err;
138}
139
140static void ipvlan_port_destroy(struct net_device *dev)
141{
142 struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
Eric Dumazetb1227d02016-12-21 18:00:24 -0800143 struct sk_buff *skb;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800144
145 dev->priv_flags &= ~IFF_IPVLAN_MASTER;
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700146 if (port->mode == IPVLAN_MODE_L3S) {
147 dev->priv_flags &= ~IFF_L3MDEV_MASTER;
Florian Westphal31338222017-04-20 18:08:15 +0200148 ipvlan_unregister_nf_hook(dev_net(dev));
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700149 dev->l3mdev_ops = NULL;
150 }
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800151 netdev_rx_handler_unregister(dev);
Mahesh Bandewarba35f852015-05-04 17:06:03 -0700152 cancel_work_sync(&port->wq);
Eric Dumazetb1227d02016-12-21 18:00:24 -0800153 while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
154 if (skb->dev)
155 dev_put(skb->dev);
156 kfree_skb(skb);
157 }
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800158 ida_destroy(&port->ida);
Gao Feng48140a22016-12-07 08:44:47 +0800159 kfree(port);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800160}
161
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800162#define IPVLAN_FEATURES \
Tom Herberta1882222015-12-14 11:19:43 -0800163 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
David S. Miller182e0b62017-07-03 06:32:14 -0700164 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_GSO_ROBUST | \
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800165 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
166 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
167
168#define IPVLAN_STATE_MASK \
169 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
170
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800171static int ipvlan_init(struct net_device *dev)
172{
173 struct ipvl_dev *ipvlan = netdev_priv(dev);
Girish Moodalbailfe18da62017-11-16 23:16:17 -0800174 struct net_device *phy_dev = ipvlan->phy_dev;
175 struct ipvl_port *port;
176 int err;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800177
178 dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
179 (phy_dev->state & IPVLAN_STATE_MASK);
180 dev->features = phy_dev->features & IPVLAN_FEATURES;
Paolo Abeni3518e402018-03-02 16:03:32 +0100181 dev->features |= NETIF_F_LLTX | NETIF_F_VLAN_CHALLENGED;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800182 dev->gso_max_size = phy_dev->gso_max_size;
Eric Dumazetf6773c52016-03-16 21:59:49 -0700183 dev->gso_max_segs = phy_dev->gso_max_segs;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800184 dev->hard_header_len = phy_dev->hard_header_len;
185
Eric Dumazet0d7dd792016-06-09 07:45:15 -0700186 netdev_lockdep_set_classes(dev);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800187
Florian Fainelli87173cd2017-08-01 12:11:13 -0700188 ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800189 if (!ipvlan->pcpu_stats)
190 return -ENOMEM;
191
Girish Moodalbailfe18da62017-11-16 23:16:17 -0800192 if (!netif_is_ipvlan_port(phy_dev)) {
193 err = ipvlan_port_create(phy_dev);
194 if (err < 0) {
195 free_percpu(ipvlan->pcpu_stats);
196 return err;
197 }
198 }
199 port = ipvlan_port_get_rtnl(phy_dev);
Mahesh Bandewar494e8482016-04-27 14:59:27 -0700200 port->count += 1;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800201 return 0;
202}
203
204static void ipvlan_uninit(struct net_device *dev)
205{
206 struct ipvl_dev *ipvlan = netdev_priv(dev);
Girish Moodalbailfe18da62017-11-16 23:16:17 -0800207 struct net_device *phy_dev = ipvlan->phy_dev;
208 struct ipvl_port *port;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800209
Markus Elfring04901ce2014-11-29 16:23:20 +0100210 free_percpu(ipvlan->pcpu_stats);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800211
Girish Moodalbailfe18da62017-11-16 23:16:17 -0800212 port = ipvlan_port_get_rtnl(phy_dev);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800213 port->count -= 1;
214 if (!port->count)
215 ipvlan_port_destroy(port->dev);
216}
217
218static int ipvlan_open(struct net_device *dev)
219{
220 struct ipvl_dev *ipvlan = netdev_priv(dev);
221 struct net_device *phy_dev = ipvlan->phy_dev;
222 struct ipvl_addr *addr;
223
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700224 if (ipvlan->port->mode == IPVLAN_MODE_L3 ||
225 ipvlan->port->mode == IPVLAN_MODE_L3S)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800226 dev->flags |= IFF_NOARP;
227 else
228 dev->flags &= ~IFF_NOARP;
229
Paolo Abeni82308192018-02-28 10:59:27 +0100230 rcu_read_lock();
231 list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300232 ipvlan_ht_addr_add(ipvlan, addr);
Paolo Abeni82308192018-02-28 10:59:27 +0100233 rcu_read_unlock();
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300234
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800235 return dev_uc_add(phy_dev, phy_dev->dev_addr);
236}
237
238static int ipvlan_stop(struct net_device *dev)
239{
240 struct ipvl_dev *ipvlan = netdev_priv(dev);
241 struct net_device *phy_dev = ipvlan->phy_dev;
242 struct ipvl_addr *addr;
243
244 dev_uc_unsync(phy_dev, dev);
245 dev_mc_unsync(phy_dev, dev);
246
247 dev_uc_del(phy_dev, phy_dev->dev_addr);
248
Paolo Abeni82308192018-02-28 10:59:27 +0100249 rcu_read_lock();
250 list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
Konstantin Khlebnikov6640e672015-07-14 16:35:53 +0300251 ipvlan_ht_addr_del(addr);
Paolo Abeni82308192018-02-28 10:59:27 +0100252 rcu_read_unlock();
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300253
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800254 return 0;
255}
256
Mahesh Bandewar92c7b0d2014-11-25 21:24:43 -0800257static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
258 struct net_device *dev)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800259{
260 const struct ipvl_dev *ipvlan = netdev_priv(dev);
261 int skblen = skb->len;
262 int ret;
263
264 ret = ipvlan_queue_xmit(skb, dev);
265 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
266 struct ipvl_pcpu_stats *pcptr;
267
268 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
269
270 u64_stats_update_begin(&pcptr->syncp);
271 pcptr->tx_pkts++;
272 pcptr->tx_bytes += skblen;
273 u64_stats_update_end(&pcptr->syncp);
274 } else {
275 this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
276 }
277 return ret;
278}
279
280static netdev_features_t ipvlan_fix_features(struct net_device *dev,
281 netdev_features_t features)
282{
283 struct ipvl_dev *ipvlan = netdev_priv(dev);
284
285 return features & (ipvlan->sfeatures | ~IPVLAN_FEATURES);
286}
287
288static void ipvlan_change_rx_flags(struct net_device *dev, int change)
289{
290 struct ipvl_dev *ipvlan = netdev_priv(dev);
291 struct net_device *phy_dev = ipvlan->phy_dev;
292
293 if (change & IFF_ALLMULTI)
294 dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
295}
296
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800297static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
298{
299 struct ipvl_dev *ipvlan = netdev_priv(dev);
300
301 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
302 bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
303 } else {
304 struct netdev_hw_addr *ha;
305 DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
306
307 bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
308 netdev_for_each_mc_addr(ha, dev)
309 __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
310
Mahesh Bandewarf631c442015-05-04 17:06:11 -0700311 /* Turn-on broadcast bit irrespective of address family,
312 * since broadcast is deferred to a work-queue, hence no
313 * impact on fast-path processing.
314 */
315 __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
316
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800317 bitmap_copy(ipvlan->mac_filters, mc_filters,
318 IPVLAN_MAC_FILTER_SIZE);
319 }
320 dev_uc_sync(ipvlan->phy_dev, dev);
321 dev_mc_sync(ipvlan->phy_dev, dev);
322}
323
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800324static void ipvlan_get_stats64(struct net_device *dev,
325 struct rtnl_link_stats64 *s)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800326{
327 struct ipvl_dev *ipvlan = netdev_priv(dev);
328
329 if (ipvlan->pcpu_stats) {
330 struct ipvl_pcpu_stats *pcptr;
331 u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
332 u32 rx_errs = 0, tx_drps = 0;
333 u32 strt;
334 int idx;
335
336 for_each_possible_cpu(idx) {
337 pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
338 do {
339 strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
340 rx_pkts = pcptr->rx_pkts;
341 rx_bytes = pcptr->rx_bytes;
342 rx_mcast = pcptr->rx_mcast;
343 tx_pkts = pcptr->tx_pkts;
344 tx_bytes = pcptr->tx_bytes;
345 } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
346 strt));
347
348 s->rx_packets += rx_pkts;
349 s->rx_bytes += rx_bytes;
350 s->multicast += rx_mcast;
351 s->tx_packets += tx_pkts;
352 s->tx_bytes += tx_bytes;
353
354 /* u32 values are updated without syncp protection. */
355 rx_errs += pcptr->rx_errs;
356 tx_drps += pcptr->tx_drps;
357 }
358 s->rx_errors = rx_errs;
359 s->rx_dropped = rx_errs;
360 s->tx_dropped = tx_drps;
361 }
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800362}
363
364static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
365{
366 struct ipvl_dev *ipvlan = netdev_priv(dev);
367 struct net_device *phy_dev = ipvlan->phy_dev;
368
369 return vlan_vid_add(phy_dev, proto, vid);
370}
371
372static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
373 u16 vid)
374{
375 struct ipvl_dev *ipvlan = netdev_priv(dev);
376 struct net_device *phy_dev = ipvlan->phy_dev;
377
378 vlan_vid_del(phy_dev, proto, vid);
379 return 0;
380}
381
Nicolas Dichtel7c411652015-04-02 17:07:06 +0200382static int ipvlan_get_iflink(const struct net_device *dev)
383{
384 struct ipvl_dev *ipvlan = netdev_priv(dev);
385
386 return ipvlan->phy_dev->ifindex;
387}
388
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800389static const struct net_device_ops ipvlan_netdev_ops = {
390 .ndo_init = ipvlan_init,
391 .ndo_uninit = ipvlan_uninit,
392 .ndo_open = ipvlan_open,
393 .ndo_stop = ipvlan_stop,
394 .ndo_start_xmit = ipvlan_start_xmit,
395 .ndo_fix_features = ipvlan_fix_features,
396 .ndo_change_rx_flags = ipvlan_change_rx_flags,
397 .ndo_set_rx_mode = ipvlan_set_multicast_mac_filter,
398 .ndo_get_stats64 = ipvlan_get_stats64,
399 .ndo_vlan_rx_add_vid = ipvlan_vlan_rx_add_vid,
400 .ndo_vlan_rx_kill_vid = ipvlan_vlan_rx_kill_vid,
Nicolas Dichtel7c411652015-04-02 17:07:06 +0200401 .ndo_get_iflink = ipvlan_get_iflink,
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800402};
403
404static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
405 unsigned short type, const void *daddr,
406 const void *saddr, unsigned len)
407{
408 const struct ipvl_dev *ipvlan = netdev_priv(dev);
409 struct net_device *phy_dev = ipvlan->phy_dev;
410
411 /* TODO Probably use a different field than dev_addr so that the
412 * mac-address on the virtual device is portable and can be carried
413 * while the packets use the mac-addr on the physical device.
414 */
415 return dev_hard_header(skb, phy_dev, type, daddr,
Mahesh Bandewar32c10bb2017-10-11 17:16:26 -0700416 saddr ? : phy_dev->dev_addr, len);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800417}
418
419static const struct header_ops ipvlan_header_ops = {
420 .create = ipvlan_hard_header,
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800421 .parse = eth_header_parse,
422 .cache = eth_header_cache,
423 .cache_update = eth_header_cache_update,
424};
425
David Decotigny314d10d2016-02-24 10:58:03 -0800426static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
427 struct ethtool_link_ksettings *cmd)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800428{
429 const struct ipvl_dev *ipvlan = netdev_priv(dev);
430
David Decotigny314d10d2016-02-24 10:58:03 -0800431 return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800432}
433
434static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
435 struct ethtool_drvinfo *drvinfo)
436{
437 strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
438 strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
439}
440
441static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
442{
443 const struct ipvl_dev *ipvlan = netdev_priv(dev);
444
445 return ipvlan->msg_enable;
446}
447
448static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
449{
450 struct ipvl_dev *ipvlan = netdev_priv(dev);
451
452 ipvlan->msg_enable = value;
453}
454
455static const struct ethtool_ops ipvlan_ethtool_ops = {
456 .get_link = ethtool_op_get_link,
David Decotigny314d10d2016-02-24 10:58:03 -0800457 .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800458 .get_drvinfo = ipvlan_ethtool_get_drvinfo,
459 .get_msglevel = ipvlan_ethtool_get_msglevel,
460 .set_msglevel = ipvlan_ethtool_set_msglevel,
461};
462
463static int ipvlan_nl_changelink(struct net_device *dev,
Matthias Schifferad744b22017-06-25 23:56:00 +0200464 struct nlattr *tb[], struct nlattr *data[],
465 struct netlink_ext_ack *extack)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800466{
467 struct ipvl_dev *ipvlan = netdev_priv(dev);
468 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700469 int err = 0;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800470
Mahesh Bandewara190d042017-10-26 15:09:21 -0700471 if (!data)
472 return 0;
473
474 if (data[IFLA_IPVLAN_MODE]) {
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800475 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
476
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700477 err = ipvlan_set_port_mode(port, nmode);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800478 }
Mahesh Bandewara190d042017-10-26 15:09:21 -0700479
480 if (!err && data[IFLA_IPVLAN_FLAGS]) {
481 u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
482
483 if (flags & IPVLAN_F_PRIVATE)
484 ipvlan_mark_private(port);
485 else
486 ipvlan_clear_private(port);
Mahesh Bandewarfe89aa62017-10-26 15:09:25 -0700487
488 if (flags & IPVLAN_F_VEPA)
489 ipvlan_mark_vepa(port);
490 else
491 ipvlan_clear_vepa(port);
Mahesh Bandewara190d042017-10-26 15:09:21 -0700492 }
493
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700494 return err;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800495}
496
497static size_t ipvlan_nl_getsize(const struct net_device *dev)
498{
499 return (0
500 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
Mahesh Bandewara190d042017-10-26 15:09:21 -0700501 + nla_total_size(2) /* IFLA_IPVLAN_FLAGS */
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800502 );
503}
504
Matthias Schiffera8b8a8892017-06-25 23:56:01 +0200505static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
506 struct netlink_ext_ack *extack)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800507{
Mahesh Bandewara190d042017-10-26 15:09:21 -0700508 if (!data)
509 return 0;
510
511 if (data[IFLA_IPVLAN_MODE]) {
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800512 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
513
514 if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
515 return -EINVAL;
516 }
Mahesh Bandewara190d042017-10-26 15:09:21 -0700517 if (data[IFLA_IPVLAN_FLAGS]) {
518 u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
519
Mahesh Bandewarfe89aa62017-10-26 15:09:25 -0700520 /* Only two bits are used at this moment. */
521 if (flags & ~(IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
522 return -EINVAL;
523 /* Also both flags can't be active at the same time. */
524 if ((flags & (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA)) ==
525 (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
Mahesh Bandewara190d042017-10-26 15:09:21 -0700526 return -EINVAL;
527 }
528
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800529 return 0;
530}
531
532static int ipvlan_nl_fillinfo(struct sk_buff *skb,
533 const struct net_device *dev)
534{
535 struct ipvl_dev *ipvlan = netdev_priv(dev);
536 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
537 int ret = -EINVAL;
538
539 if (!port)
540 goto err;
541
542 ret = -EMSGSIZE;
543 if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
544 goto err;
Mahesh Bandewara190d042017-10-26 15:09:21 -0700545 if (nla_put_u16(skb, IFLA_IPVLAN_FLAGS, port->flags))
546 goto err;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800547
548 return 0;
549
550err:
551 return ret;
552}
553
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800554int ipvlan_link_new(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +0200555 struct nlattr *tb[], struct nlattr *data[],
556 struct netlink_ext_ack *extack)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800557{
558 struct ipvl_dev *ipvlan = netdev_priv(dev);
559 struct ipvl_port *port;
560 struct net_device *phy_dev;
561 int err;
Mahesh Bandeware93fbc52016-02-20 19:31:36 -0800562 u16 mode = IPVLAN_MODE_L3;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800563
564 if (!tb[IFLA_LINK])
565 return -EINVAL;
566
567 phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
568 if (!phy_dev)
569 return -ENODEV;
570
Mahesh Bandewar5933fea2014-12-06 15:53:33 -0800571 if (netif_is_ipvlan(phy_dev)) {
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800572 struct ipvl_dev *tmp = netdev_priv(phy_dev);
573
574 phy_dev = tmp->phy_dev;
Mahesh Bandewar5933fea2014-12-06 15:53:33 -0800575 } else if (!netif_is_ipvlan_port(phy_dev)) {
Girish Moodalbailfe18da62017-11-16 23:16:17 -0800576 /* Exit early if the underlying link is invalid or busy */
577 if (phy_dev->type != ARPHRD_ETHER ||
578 phy_dev->flags & IFF_LOOPBACK) {
579 netdev_err(phy_dev,
580 "Master is either lo or non-ether device\n");
581 return -EINVAL;
582 }
583
584 if (netdev_is_rx_handler_busy(phy_dev)) {
585 netdev_err(phy_dev, "Device is already in use.\n");
586 return -EBUSY;
587 }
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800588 }
589
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800590 ipvlan->phy_dev = phy_dev;
591 ipvlan->dev = dev;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800592 ipvlan->sfeatures = IPVLAN_FEATURES;
Mahesh Bandewar296d4852016-01-27 23:33:28 -0800593 ipvlan_adjust_mtu(ipvlan, phy_dev);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800594 INIT_LIST_HEAD(&ipvlan->addrs);
Paolo Abeni82308192018-02-28 10:59:27 +0100595 spin_lock_init(&ipvlan->addrs_lock);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800596
Girish Moodalbailfe18da62017-11-16 23:16:17 -0800597 /* TODO Probably put random address here to be presented to the
598 * world but keep using the physical-dev address for the outgoing
599 * packets.
Mahesh Bandewara190d042017-10-26 15:09:21 -0700600 */
Girish Moodalbailfe18da62017-11-16 23:16:17 -0800601 memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
602
603 dev->priv_flags |= IFF_IPVLAN_SLAVE;
604
605 err = register_netdevice(dev);
606 if (err < 0)
607 return err;
608
609 /* ipvlan_init() would have created the port, if required */
610 port = ipvlan_port_get_rtnl(phy_dev);
611 ipvlan->port = port;
Mahesh Bandewara190d042017-10-26 15:09:21 -0700612
Mahesh Bandewarda36e132017-01-09 15:05:54 -0800613 /* If the port-id base is at the MAX value, then wrap it around and
614 * begin from 0x1 again. This may be due to a busy system where lots
615 * of slaves are getting created and deleted.
616 */
617 if (port->dev_id_start == 0xFFFE)
618 port->dev_id_start = 0x1;
619
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800620 /* Since L2 address is shared among all IPvlan slaves including
621 * master, use unique 16 bit dev-ids to diffentiate among them.
622 * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
623 * slave link [see addrconf_ifid_eui48()].
624 */
Mahesh Bandewarda36e132017-01-09 15:05:54 -0800625 err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
626 GFP_KERNEL);
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800627 if (err < 0)
Mahesh Bandewar019ec002017-01-13 15:48:30 -0800628 err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
629 GFP_KERNEL);
630 if (err < 0)
Girish Moodalbailfe18da62017-11-16 23:16:17 -0800631 goto unregister_netdev;
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800632 dev->dev_id = err;
Girish Moodalbailfe18da62017-11-16 23:16:17 -0800633
Mahesh Bandewarda36e132017-01-09 15:05:54 -0800634 /* Increment id-base to the next slot for the future assignment */
635 port->dev_id_start = err + 1;
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800636
Girish Moodalbailfe18da62017-11-16 23:16:17 -0800637 err = netdev_upper_dev_link(phy_dev, dev, extack);
638 if (err)
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800639 goto remove_ida;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800640
Girish Moodalbailfe18da62017-11-16 23:16:17 -0800641 /* Flags are per port and latest update overrides. User has
642 * to be consistent in setting it just like the mode attribute.
643 */
644 if (data && data[IFLA_IPVLAN_FLAGS])
645 port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
646
647 if (data && data[IFLA_IPVLAN_MODE])
648 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
649
Mahesh Bandewar4fbae7d2016-09-16 12:59:19 -0700650 err = ipvlan_set_port_mode(port, mode);
Girish Moodalbailfe18da62017-11-16 23:16:17 -0800651 if (err)
Gao Feng1a31cc82016-12-08 11:16:58 +0800652 goto unlink_netdev;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800653
654 list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
655 netif_stacked_transfer_operstate(phy_dev, dev);
656 return 0;
Gao Feng147fd282016-11-24 23:39:59 +0800657
Gao Feng1a31cc82016-12-08 11:16:58 +0800658unlink_netdev:
659 netdev_upper_dev_unlink(phy_dev, dev);
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800660remove_ida:
661 ida_simple_remove(&port->ida, dev->dev_id);
Girish Moodalbailfe18da62017-11-16 23:16:17 -0800662unregister_netdev:
663 unregister_netdevice(dev);
Gao Feng147fd282016-11-24 23:39:59 +0800664 return err;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800665}
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800666EXPORT_SYMBOL_GPL(ipvlan_link_new);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800667
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800668void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800669{
670 struct ipvl_dev *ipvlan = netdev_priv(dev);
671 struct ipvl_addr *addr, *next;
672
Paolo Abeni82308192018-02-28 10:59:27 +0100673 spin_lock_bh(&ipvlan->addrs_lock);
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300674 list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
Konstantin Khlebnikov6640e672015-07-14 16:35:53 +0300675 ipvlan_ht_addr_del(addr);
Paolo Abeni82308192018-02-28 10:59:27 +0100676 list_del_rcu(&addr->anode);
Konstantin Khlebnikov6a725492015-07-14 16:35:51 +0300677 kfree_rcu(addr, rcu);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800678 }
Paolo Abeni82308192018-02-28 10:59:27 +0100679 spin_unlock_bh(&ipvlan->addrs_lock);
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300680
Mahesh Bandewar009146d2017-01-03 12:47:16 -0800681 ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800682 list_del_rcu(&ipvlan->pnode);
683 unregister_netdevice_queue(dev, head);
684 netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
685}
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800686EXPORT_SYMBOL_GPL(ipvlan_link_delete);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800687
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800688void ipvlan_link_setup(struct net_device *dev)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800689{
690 ether_setup(dev);
691
692 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
Phil Sutterbf485bc2015-08-18 10:30:40 +0200693 dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800694 dev->netdev_ops = &ipvlan_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400695 dev->needs_free_netdev = true;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800696 dev->header_ops = &ipvlan_header_ops;
697 dev->ethtool_ops = &ipvlan_ethtool_ops;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800698}
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800699EXPORT_SYMBOL_GPL(ipvlan_link_setup);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800700
701static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
702{
703 [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
Mahesh Bandewara190d042017-10-26 15:09:21 -0700704 [IFLA_IPVLAN_FLAGS] = { .type = NLA_U16 },
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800705};
706
707static struct rtnl_link_ops ipvlan_link_ops = {
708 .kind = "ipvlan",
709 .priv_size = sizeof(struct ipvl_dev),
710
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800711 .setup = ipvlan_link_setup,
712 .newlink = ipvlan_link_new,
713 .dellink = ipvlan_link_delete,
714};
715
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800716int ipvlan_link_register(struct rtnl_link_ops *ops)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800717{
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800718 ops->get_size = ipvlan_nl_getsize;
719 ops->policy = ipvlan_nl_policy;
720 ops->validate = ipvlan_nl_validate;
721 ops->fill_info = ipvlan_nl_fillinfo;
722 ops->changelink = ipvlan_nl_changelink;
723 ops->maxtype = IFLA_IPVLAN_MAX;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800724 return rtnl_link_register(ops);
725}
Sainath Grandhi235a9d82017-02-10 16:03:52 -0800726EXPORT_SYMBOL_GPL(ipvlan_link_register);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800727
728static int ipvlan_device_event(struct notifier_block *unused,
729 unsigned long event, void *ptr)
730{
731 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
732 struct ipvl_dev *ipvlan, *next;
733 struct ipvl_port *port;
734 LIST_HEAD(lst_kill);
735
Mahesh Bandewar5933fea2014-12-06 15:53:33 -0800736 if (!netif_is_ipvlan_port(dev))
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800737 return NOTIFY_DONE;
738
739 port = ipvlan_port_get_rtnl(dev);
740
741 switch (event) {
742 case NETDEV_CHANGE:
743 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
744 netif_stacked_transfer_operstate(ipvlan->phy_dev,
745 ipvlan->dev);
746 break;
747
Florian Westphal31338222017-04-20 18:08:15 +0200748 case NETDEV_REGISTER: {
749 struct net *oldnet, *newnet = dev_net(dev);
750 struct ipvlan_netns *old_vnet;
751
752 oldnet = read_pnet(&port->pnet);
753 if (net_eq(newnet, oldnet))
754 break;
755
756 write_pnet(&port->pnet, newnet);
757
758 old_vnet = net_generic(oldnet, ipvlan_netid);
759 if (!old_vnet->ipvl_nf_hook_refcnt)
760 break;
761
762 ipvlan_register_nf_hook(newnet);
763 ipvlan_unregister_nf_hook(oldnet);
764 break;
765 }
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800766 case NETDEV_UNREGISTER:
767 if (dev->reg_state != NETREG_UNREGISTERING)
768 break;
769
Paolo Abeni82308192018-02-28 10:59:27 +0100770 list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800771 ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
772 &lst_kill);
773 unregister_netdevice_many(&lst_kill);
774 break;
775
776 case NETDEV_FEAT_CHANGE:
777 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
778 ipvlan->dev->features = dev->features & IPVLAN_FEATURES;
779 ipvlan->dev->gso_max_size = dev->gso_max_size;
Eric Dumazetf6773c52016-03-16 21:59:49 -0700780 ipvlan->dev->gso_max_segs = dev->gso_max_segs;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800781 netdev_features_change(ipvlan->dev);
782 }
783 break;
784
785 case NETDEV_CHANGEMTU:
786 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
787 ipvlan_adjust_mtu(ipvlan, dev);
788 break;
789
Mahesh Bandewar32c10bb2017-10-11 17:16:26 -0700790 case NETDEV_CHANGEADDR:
791 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
792 ether_addr_copy(ipvlan->dev->dev_addr, dev->dev_addr);
793 break;
794
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800795 case NETDEV_PRE_TYPE_CHANGE:
796 /* Forbid underlying device to change its type. */
797 return NOTIFY_BAD;
798 }
799 return NOTIFY_DONE;
800}
801
Paolo Abeni82308192018-02-28 10:59:27 +0100802/* the caller must held the addrs lock */
Gao Feng86673982016-12-28 16:46:51 +0800803static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800804{
805 struct ipvl_addr *addr;
806
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800807 addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
808 if (!addr)
809 return -ENOMEM;
810
811 addr->master = ipvlan;
Matteo Croce94333fa2018-02-21 01:31:13 +0100812 if (!is_v6) {
Gao Feng86673982016-12-28 16:46:51 +0800813 memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
814 addr->atype = IPVL_IPV4;
Matteo Croce94333fa2018-02-21 01:31:13 +0100815#if IS_ENABLED(CONFIG_IPV6)
816 } else {
817 memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
818 addr->atype = IPVL_IPV6;
819#endif
Gao Feng86673982016-12-28 16:46:51 +0800820 }
Paolo Abeni82308192018-02-28 10:59:27 +0100821
822 list_add_tail_rcu(&addr->anode, &ipvlan->addrs);
Konstantin Khlebnikov515866f2015-07-14 16:35:50 +0300823
Jiri Benc27705f72015-03-28 19:13:22 +0100824 /* If the interface is not up, the address will be added to the hash
825 * list by ipvlan_open.
826 */
827 if (netif_running(ipvlan->dev))
828 ipvlan_ht_addr_add(ipvlan, addr);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800829
830 return 0;
831}
832
Gao Feng86673982016-12-28 16:46:51 +0800833static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800834{
835 struct ipvl_addr *addr;
836
Paolo Abeni82308192018-02-28 10:59:27 +0100837 spin_lock_bh(&ipvlan->addrs_lock);
Gao Feng86673982016-12-28 16:46:51 +0800838 addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
Paolo Abeni82308192018-02-28 10:59:27 +0100839 if (!addr) {
840 spin_unlock_bh(&ipvlan->addrs_lock);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800841 return;
Paolo Abeni82308192018-02-28 10:59:27 +0100842 }
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800843
Konstantin Khlebnikov6640e672015-07-14 16:35:53 +0300844 ipvlan_ht_addr_del(addr);
Paolo Abeni82308192018-02-28 10:59:27 +0100845 list_del_rcu(&addr->anode);
846 spin_unlock_bh(&ipvlan->addrs_lock);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800847 kfree_rcu(addr, rcu);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800848}
849
Matteo Croce94333fa2018-02-21 01:31:13 +0100850static bool ipvlan_is_valid_dev(const struct net_device *dev)
851{
852 struct ipvl_dev *ipvlan = netdev_priv(dev);
853
854 if (!netif_is_ipvlan(dev))
855 return false;
856
857 if (!ipvlan || !ipvlan->port)
858 return false;
859
860 return true;
861}
862
863#if IS_ENABLED(CONFIG_IPV6)
Gao Feng86673982016-12-28 16:46:51 +0800864static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
865{
Paolo Abeni82308192018-02-28 10:59:27 +0100866 int ret = -EINVAL;
867
868 spin_lock_bh(&ipvlan->addrs_lock);
869 if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true))
Gao Feng86673982016-12-28 16:46:51 +0800870 netif_err(ipvlan, ifup, ipvlan->dev,
871 "Failed to add IPv6=%pI6c addr for %s intf\n",
872 ip6_addr, ipvlan->dev->name);
Paolo Abeni82308192018-02-28 10:59:27 +0100873 else
874 ret = ipvlan_add_addr(ipvlan, ip6_addr, true);
875 spin_unlock_bh(&ipvlan->addrs_lock);
876 return ret;
Gao Feng86673982016-12-28 16:46:51 +0800877}
878
879static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
880{
881 return ipvlan_del_addr(ipvlan, ip6_addr, true);
882}
883
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800884static int ipvlan_addr6_event(struct notifier_block *unused,
885 unsigned long event, void *ptr)
886{
887 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
888 struct net_device *dev = (struct net_device *)if6->idev->dev;
889 struct ipvl_dev *ipvlan = netdev_priv(dev);
890
Gao Feng5e51fe62017-12-01 16:33:03 +0800891 if (!ipvlan_is_valid_dev(dev))
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800892 return NOTIFY_DONE;
893
894 switch (event) {
895 case NETDEV_UP:
896 if (ipvlan_add_addr6(ipvlan, &if6->addr))
897 return NOTIFY_BAD;
898 break;
899
900 case NETDEV_DOWN:
901 ipvlan_del_addr6(ipvlan, &if6->addr);
902 break;
903 }
904
905 return NOTIFY_OK;
906}
907
Krister Johansen3ad7d242017-06-08 13:12:14 -0700908static int ipvlan_addr6_validator_event(struct notifier_block *unused,
909 unsigned long event, void *ptr)
910{
911 struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr;
912 struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev;
913 struct ipvl_dev *ipvlan = netdev_priv(dev);
914
Gao Feng5e51fe62017-12-01 16:33:03 +0800915 if (!ipvlan_is_valid_dev(dev))
Krister Johansen3ad7d242017-06-08 13:12:14 -0700916 return NOTIFY_DONE;
917
918 switch (event) {
919 case NETDEV_UP:
David Ahernde95e042017-10-18 09:56:54 -0700920 if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true)) {
921 NL_SET_ERR_MSG(i6vi->extack,
922 "Address already assigned to an ipvlan device");
Krister Johansen3ad7d242017-06-08 13:12:14 -0700923 return notifier_from_errno(-EADDRINUSE);
David Ahernde95e042017-10-18 09:56:54 -0700924 }
Krister Johansen3ad7d242017-06-08 13:12:14 -0700925 break;
926 }
927
928 return NOTIFY_OK;
929}
Matteo Croce94333fa2018-02-21 01:31:13 +0100930#endif
Krister Johansen3ad7d242017-06-08 13:12:14 -0700931
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800932static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
933{
Paolo Abeni82308192018-02-28 10:59:27 +0100934 int ret = -EINVAL;
935
936 spin_lock_bh(&ipvlan->addrs_lock);
937 if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false))
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800938 netif_err(ipvlan, ifup, ipvlan->dev,
939 "Failed to add IPv4=%pI4 on %s intf.\n",
940 ip4_addr, ipvlan->dev->name);
Paolo Abeni82308192018-02-28 10:59:27 +0100941 else
942 ret = ipvlan_add_addr(ipvlan, ip4_addr, false);
943 spin_unlock_bh(&ipvlan->addrs_lock);
944 return ret;
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800945}
946
947static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
948{
Gao Feng86673982016-12-28 16:46:51 +0800949 return ipvlan_del_addr(ipvlan, ip4_addr, false);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800950}
951
952static int ipvlan_addr4_event(struct notifier_block *unused,
953 unsigned long event, void *ptr)
954{
955 struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
956 struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
957 struct ipvl_dev *ipvlan = netdev_priv(dev);
958 struct in_addr ip4_addr;
959
Gao Feng5e51fe62017-12-01 16:33:03 +0800960 if (!ipvlan_is_valid_dev(dev))
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -0800961 return NOTIFY_DONE;
962
963 switch (event) {
964 case NETDEV_UP:
965 ip4_addr.s_addr = if4->ifa_address;
966 if (ipvlan_add_addr4(ipvlan, &ip4_addr))
967 return NOTIFY_BAD;
968 break;
969
970 case NETDEV_DOWN:
971 ip4_addr.s_addr = if4->ifa_address;
972 ipvlan_del_addr4(ipvlan, &ip4_addr);
973 break;
974 }
975
976 return NOTIFY_OK;
977}
978
Krister Johansen3ad7d242017-06-08 13:12:14 -0700979static int ipvlan_addr4_validator_event(struct notifier_block *unused,
980 unsigned long event, void *ptr)
981{
982 struct in_validator_info *ivi = (struct in_validator_info *)ptr;
983 struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev;
984 struct ipvl_dev *ipvlan = netdev_priv(dev);
985
Gao Feng5e51fe62017-12-01 16:33:03 +0800986 if (!ipvlan_is_valid_dev(dev))
Krister Johansen3ad7d242017-06-08 13:12:14 -0700987 return NOTIFY_DONE;
988
989 switch (event) {
990 case NETDEV_UP:
David Ahernde95e042017-10-18 09:56:54 -0700991 if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false)) {
992 NL_SET_ERR_MSG(ivi->extack,
993 "Address already assigned to an ipvlan device");
Krister Johansen3ad7d242017-06-08 13:12:14 -0700994 return notifier_from_errno(-EADDRINUSE);
David Ahernde95e042017-10-18 09:56:54 -0700995 }
Krister Johansen3ad7d242017-06-08 13:12:14 -0700996 break;
997 }
998
999 return NOTIFY_OK;
1000}
1001
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001002static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
1003 .notifier_call = ipvlan_addr4_event,
1004};
1005
Krister Johansen3ad7d242017-06-08 13:12:14 -07001006static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
1007 .notifier_call = ipvlan_addr4_validator_event,
1008};
1009
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001010static struct notifier_block ipvlan_notifier_block __read_mostly = {
1011 .notifier_call = ipvlan_device_event,
1012};
1013
Matteo Croce94333fa2018-02-21 01:31:13 +01001014#if IS_ENABLED(CONFIG_IPV6)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001015static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
1016 .notifier_call = ipvlan_addr6_event,
1017};
1018
Krister Johansen3ad7d242017-06-08 13:12:14 -07001019static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
1020 .notifier_call = ipvlan_addr6_validator_event,
1021};
Matteo Croce94333fa2018-02-21 01:31:13 +01001022#endif
Krister Johansen3ad7d242017-06-08 13:12:14 -07001023
Florian Westphal31338222017-04-20 18:08:15 +02001024static void ipvlan_ns_exit(struct net *net)
1025{
1026 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
1027
1028 if (WARN_ON_ONCE(vnet->ipvl_nf_hook_refcnt)) {
1029 vnet->ipvl_nf_hook_refcnt = 0;
1030 nf_unregister_net_hooks(net, ipvl_nfops,
1031 ARRAY_SIZE(ipvl_nfops));
1032 }
1033}
1034
1035static struct pernet_operations ipvlan_net_ops = {
1036 .id = &ipvlan_netid,
1037 .size = sizeof(struct ipvlan_netns),
1038 .exit = ipvlan_ns_exit,
Kirill Tkhai68eabe82018-02-26 16:02:48 +03001039 .async = true,
Florian Westphal31338222017-04-20 18:08:15 +02001040};
1041
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001042static int __init ipvlan_init_module(void)
1043{
1044 int err;
1045
1046 ipvlan_init_secret();
1047 register_netdevice_notifier(&ipvlan_notifier_block);
Matteo Croce94333fa2018-02-21 01:31:13 +01001048#if IS_ENABLED(CONFIG_IPV6)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001049 register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
Krister Johansen3ad7d242017-06-08 13:12:14 -07001050 register_inet6addr_validator_notifier(
1051 &ipvlan_addr6_vtor_notifier_block);
Matteo Croce94333fa2018-02-21 01:31:13 +01001052#endif
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001053 register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
Krister Johansen3ad7d242017-06-08 13:12:14 -07001054 register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001055
Florian Westphal31338222017-04-20 18:08:15 +02001056 err = register_pernet_subsys(&ipvlan_net_ops);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001057 if (err < 0)
1058 goto error;
1059
Florian Westphal31338222017-04-20 18:08:15 +02001060 err = ipvlan_link_register(&ipvlan_link_ops);
1061 if (err < 0) {
1062 unregister_pernet_subsys(&ipvlan_net_ops);
1063 goto error;
1064 }
1065
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001066 return 0;
1067error:
1068 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
Krister Johansen3ad7d242017-06-08 13:12:14 -07001069 unregister_inetaddr_validator_notifier(
1070 &ipvlan_addr4_vtor_notifier_block);
Matteo Croce94333fa2018-02-21 01:31:13 +01001071#if IS_ENABLED(CONFIG_IPV6)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001072 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
Krister Johansen3ad7d242017-06-08 13:12:14 -07001073 unregister_inet6addr_validator_notifier(
1074 &ipvlan_addr6_vtor_notifier_block);
Matteo Croce94333fa2018-02-21 01:31:13 +01001075#endif
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001076 unregister_netdevice_notifier(&ipvlan_notifier_block);
1077 return err;
1078}
1079
1080static void __exit ipvlan_cleanup_module(void)
1081{
1082 rtnl_link_unregister(&ipvlan_link_ops);
Florian Westphal31338222017-04-20 18:08:15 +02001083 unregister_pernet_subsys(&ipvlan_net_ops);
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001084 unregister_netdevice_notifier(&ipvlan_notifier_block);
1085 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
Krister Johansen3ad7d242017-06-08 13:12:14 -07001086 unregister_inetaddr_validator_notifier(
1087 &ipvlan_addr4_vtor_notifier_block);
Matteo Croce94333fa2018-02-21 01:31:13 +01001088#if IS_ENABLED(CONFIG_IPV6)
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001089 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
Krister Johansen3ad7d242017-06-08 13:12:14 -07001090 unregister_inet6addr_validator_notifier(
1091 &ipvlan_addr6_vtor_notifier_block);
Matteo Croce94333fa2018-02-21 01:31:13 +01001092#endif
Mahesh Bandewar2ad7bf32014-11-23 23:07:46 -08001093}
1094
1095module_init(ipvlan_init_module);
1096module_exit(ipvlan_cleanup_module);
1097
1098MODULE_LICENSE("GPL");
1099MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
1100MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
1101MODULE_ALIAS_RTNL_LINK("ipvlan");