]>
Commit | Line | Data |
---|---|---|
2ad7bf36 MB |
1 | /* 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 | ||
3133822f FW |
12 | static unsigned int ipvlan_netid __read_mostly; |
13 | ||
14 | struct ipvlan_netns { | |
15 | unsigned int ipvl_nf_hook_refcnt; | |
16 | }; | |
4fbae7d8 | 17 | |
591bb278 | 18 | static const struct nf_hook_ops ipvl_nfops[] = { |
4fbae7d8 MB |
19 | { |
20 | .hook = ipvlan_nf_input, | |
21 | .pf = NFPROTO_IPV4, | |
22 | .hooknum = NF_INET_LOCAL_IN, | |
23 | .priority = INT_MAX, | |
24 | }, | |
94333fac | 25 | #if IS_ENABLED(CONFIG_IPV6) |
4fbae7d8 MB |
26 | { |
27 | .hook = ipvlan_nf_input, | |
28 | .pf = NFPROTO_IPV6, | |
29 | .hooknum = NF_INET_LOCAL_IN, | |
30 | .priority = INT_MAX, | |
31 | }, | |
94333fac | 32 | #endif |
4fbae7d8 MB |
33 | }; |
34 | ||
ab530f63 | 35 | static const struct l3mdev_ops ipvl_l3mdev_ops = { |
4fbae7d8 MB |
36 | .l3mdev_l3_rcv = ipvlan_l3_rcv, |
37 | }; | |
38 | ||
ab5b7013 | 39 | static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev) |
2ad7bf36 | 40 | { |
8f679ed8 | 41 | ipvlan->dev->mtu = dev->mtu; |
2ad7bf36 MB |
42 | } |
43 | ||
3133822f | 44 | static int ipvlan_register_nf_hook(struct net *net) |
4fbae7d8 | 45 | { |
3133822f | 46 | struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid); |
4fbae7d8 MB |
47 | int err = 0; |
48 | ||
3133822f FW |
49 | if (!vnet->ipvl_nf_hook_refcnt) { |
50 | err = nf_register_net_hooks(net, ipvl_nfops, | |
51 | ARRAY_SIZE(ipvl_nfops)); | |
4fbae7d8 | 52 | if (!err) |
3133822f | 53 | vnet->ipvl_nf_hook_refcnt = 1; |
4fbae7d8 | 54 | } else { |
3133822f | 55 | vnet->ipvl_nf_hook_refcnt++; |
4fbae7d8 MB |
56 | } |
57 | ||
58 | return err; | |
59 | } | |
60 | ||
3133822f | 61 | static void ipvlan_unregister_nf_hook(struct net *net) |
4fbae7d8 | 62 | { |
3133822f FW |
63 | struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid); |
64 | ||
65 | if (WARN_ON(!vnet->ipvl_nf_hook_refcnt)) | |
66 | return; | |
4fbae7d8 | 67 | |
3133822f FW |
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)); | |
4fbae7d8 MB |
72 | } |
73 | ||
74 | static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval) | |
2ad7bf36 MB |
75 | { |
76 | struct ipvl_dev *ipvlan; | |
4fbae7d8 MB |
77 | struct net_device *mdev = port->dev; |
78 | int err = 0; | |
2ad7bf36 | 79 | |
4fbae7d8 | 80 | ASSERT_RTNL(); |
2ad7bf36 | 81 | if (port->mode != nval) { |
4fbae7d8 MB |
82 | if (nval == IPVLAN_MODE_L3S) { |
83 | /* New mode is L3S */ | |
3133822f | 84 | err = ipvlan_register_nf_hook(read_pnet(&port->pnet)); |
4fbae7d8 MB |
85 | 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; | |
3133822f | 93 | ipvlan_unregister_nf_hook(read_pnet(&port->pnet)); |
4fbae7d8 MB |
94 | mdev->l3mdev_ops = NULL; |
95 | } | |
2ad7bf36 | 96 | list_for_each_entry(ipvlan, &port->ipvlans, pnode) { |
4fbae7d8 | 97 | if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S) |
2ad7bf36 MB |
98 | ipvlan->dev->flags |= IFF_NOARP; |
99 | else | |
100 | ipvlan->dev->flags &= ~IFF_NOARP; | |
101 | } | |
102 | port->mode = nval; | |
103 | } | |
4fbae7d8 | 104 | return err; |
2ad7bf36 MB |
105 | } |
106 | ||
107 | static int ipvlan_port_create(struct net_device *dev) | |
108 | { | |
109 | struct ipvl_port *port; | |
110 | int err, idx; | |
111 | ||
2ad7bf36 MB |
112 | port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL); |
113 | if (!port) | |
114 | return -ENOMEM; | |
115 | ||
3133822f | 116 | write_pnet(&port->pnet, dev_net(dev)); |
2ad7bf36 MB |
117 | 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 | ||
ba35f858 MB |
123 | skb_queue_head_init(&port->backlog); |
124 | INIT_WORK(&port->wq, ipvlan_process_multicast); | |
009146d1 | 125 | ida_init(&port->ida); |
da36e13c | 126 | port->dev_id_start = 1; |
ba35f858 | 127 | |
2ad7bf36 MB |
128 | 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 | ||
135 | err: | |
48140a21 | 136 | kfree(port); |
2ad7bf36 MB |
137 | return err; |
138 | } | |
139 | ||
140 | static void ipvlan_port_destroy(struct net_device *dev) | |
141 | { | |
142 | struct ipvl_port *port = ipvlan_port_get_rtnl(dev); | |
b1227d01 | 143 | struct sk_buff *skb; |
2ad7bf36 MB |
144 | |
145 | dev->priv_flags &= ~IFF_IPVLAN_MASTER; | |
4fbae7d8 MB |
146 | if (port->mode == IPVLAN_MODE_L3S) { |
147 | dev->priv_flags &= ~IFF_L3MDEV_MASTER; | |
3133822f | 148 | ipvlan_unregister_nf_hook(dev_net(dev)); |
4fbae7d8 MB |
149 | dev->l3mdev_ops = NULL; |
150 | } | |
2ad7bf36 | 151 | netdev_rx_handler_unregister(dev); |
ba35f858 | 152 | cancel_work_sync(&port->wq); |
b1227d01 ED |
153 | while ((skb = __skb_dequeue(&port->backlog)) != NULL) { |
154 | if (skb->dev) | |
155 | dev_put(skb->dev); | |
156 | kfree_skb(skb); | |
157 | } | |
009146d1 | 158 | ida_destroy(&port->ida); |
48140a21 | 159 | kfree(port); |
2ad7bf36 MB |
160 | } |
161 | ||
2ad7bf36 | 162 | #define IPVLAN_FEATURES \ |
a188222b | 163 | (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ |
182e0b6b | 164 | NETIF_F_GSO | NETIF_F_TSO | NETIF_F_GSO_ROBUST | \ |
2ad7bf36 MB |
165 | 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 | ||
2ad7bf36 MB |
171 | static int ipvlan_init(struct net_device *dev) |
172 | { | |
173 | struct ipvl_dev *ipvlan = netdev_priv(dev); | |
fe18da60 GM |
174 | struct net_device *phy_dev = ipvlan->phy_dev; |
175 | struct ipvl_port *port; | |
176 | int err; | |
2ad7bf36 MB |
177 | |
178 | dev->state = (dev->state & ~IPVLAN_STATE_MASK) | | |
179 | (phy_dev->state & IPVLAN_STATE_MASK); | |
180 | dev->features = phy_dev->features & IPVLAN_FEATURES; | |
3518e40b | 181 | dev->features |= NETIF_F_LLTX | NETIF_F_VLAN_CHALLENGED; |
2ad7bf36 | 182 | dev->gso_max_size = phy_dev->gso_max_size; |
f6773c5e | 183 | dev->gso_max_segs = phy_dev->gso_max_segs; |
2ad7bf36 MB |
184 | dev->hard_header_len = phy_dev->hard_header_len; |
185 | ||
0d7dd798 | 186 | netdev_lockdep_set_classes(dev); |
2ad7bf36 | 187 | |
87173cd6 | 188 | ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats); |
2ad7bf36 MB |
189 | if (!ipvlan->pcpu_stats) |
190 | return -ENOMEM; | |
191 | ||
fe18da60 GM |
192 | 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); | |
494e8489 | 200 | port->count += 1; |
2ad7bf36 MB |
201 | return 0; |
202 | } | |
203 | ||
204 | static void ipvlan_uninit(struct net_device *dev) | |
205 | { | |
206 | struct ipvl_dev *ipvlan = netdev_priv(dev); | |
fe18da60 GM |
207 | struct net_device *phy_dev = ipvlan->phy_dev; |
208 | struct ipvl_port *port; | |
2ad7bf36 | 209 | |
04901cea | 210 | free_percpu(ipvlan->pcpu_stats); |
2ad7bf36 | 211 | |
fe18da60 | 212 | port = ipvlan_port_get_rtnl(phy_dev); |
2ad7bf36 MB |
213 | port->count -= 1; |
214 | if (!port->count) | |
215 | ipvlan_port_destroy(port->dev); | |
216 | } | |
217 | ||
218 | static 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 | ||
4fbae7d8 MB |
224 | if (ipvlan->port->mode == IPVLAN_MODE_L3 || |
225 | ipvlan->port->mode == IPVLAN_MODE_L3S) | |
2ad7bf36 MB |
226 | dev->flags |= IFF_NOARP; |
227 | else | |
228 | dev->flags &= ~IFF_NOARP; | |
229 | ||
82308194 PA |
230 | rcu_read_lock(); |
231 | list_for_each_entry_rcu(addr, &ipvlan->addrs, anode) | |
515866f8 | 232 | ipvlan_ht_addr_add(ipvlan, addr); |
82308194 | 233 | rcu_read_unlock(); |
515866f8 | 234 | |
2ad7bf36 MB |
235 | return dev_uc_add(phy_dev, phy_dev->dev_addr); |
236 | } | |
237 | ||
238 | static 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 | ||
82308194 PA |
249 | rcu_read_lock(); |
250 | list_for_each_entry_rcu(addr, &ipvlan->addrs, anode) | |
6640e673 | 251 | ipvlan_ht_addr_del(addr); |
82308194 | 252 | rcu_read_unlock(); |
515866f8 | 253 | |
2ad7bf36 MB |
254 | return 0; |
255 | } | |
256 | ||
92c7b0de MB |
257 | static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb, |
258 | struct net_device *dev) | |
2ad7bf36 MB |
259 | { |
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 | ||
280 | static 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 | ||
288 | static 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 | ||
2ad7bf36 MB |
297 | static 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 | ||
f631c44b MB |
311 | /* 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 | ||
2ad7bf36 MB |
317 | 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 | ||
bc1f4470 | 324 | static void ipvlan_get_stats64(struct net_device *dev, |
325 | struct rtnl_link_stats64 *s) | |
2ad7bf36 MB |
326 | { |
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 | } | |
2ad7bf36 MB |
362 | } |
363 | ||
364 | static 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 | ||
372 | static 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 | ||
7c411658 ND |
382 | static 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 | ||
2ad7bf36 MB |
389 | static 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, | |
7c411658 | 401 | .ndo_get_iflink = ipvlan_get_iflink, |
2ad7bf36 MB |
402 | }; |
403 | ||
404 | static 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, | |
32c10bbf | 416 | saddr ? : phy_dev->dev_addr, len); |
2ad7bf36 MB |
417 | } |
418 | ||
419 | static const struct header_ops ipvlan_header_ops = { | |
420 | .create = ipvlan_hard_header, | |
2ad7bf36 MB |
421 | .parse = eth_header_parse, |
422 | .cache = eth_header_cache, | |
423 | .cache_update = eth_header_cache_update, | |
424 | }; | |
425 | ||
314d10d7 DD |
426 | static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev, |
427 | struct ethtool_link_ksettings *cmd) | |
2ad7bf36 MB |
428 | { |
429 | const struct ipvl_dev *ipvlan = netdev_priv(dev); | |
430 | ||
314d10d7 | 431 | return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd); |
2ad7bf36 MB |
432 | } |
433 | ||
434 | static 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 | ||
441 | static 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 | ||
448 | static 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 | ||
455 | static const struct ethtool_ops ipvlan_ethtool_ops = { | |
456 | .get_link = ethtool_op_get_link, | |
314d10d7 | 457 | .get_link_ksettings = ipvlan_ethtool_get_link_ksettings, |
2ad7bf36 MB |
458 | .get_drvinfo = ipvlan_ethtool_get_drvinfo, |
459 | .get_msglevel = ipvlan_ethtool_get_msglevel, | |
460 | .set_msglevel = ipvlan_ethtool_set_msglevel, | |
461 | }; | |
462 | ||
463 | static int ipvlan_nl_changelink(struct net_device *dev, | |
ad744b22 MS |
464 | struct nlattr *tb[], struct nlattr *data[], |
465 | struct netlink_ext_ack *extack) | |
2ad7bf36 MB |
466 | { |
467 | struct ipvl_dev *ipvlan = netdev_priv(dev); | |
468 | struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev); | |
4fbae7d8 | 469 | int err = 0; |
2ad7bf36 | 470 | |
a190d04d MB |
471 | if (!data) |
472 | return 0; | |
473 | ||
474 | if (data[IFLA_IPVLAN_MODE]) { | |
2ad7bf36 MB |
475 | u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]); |
476 | ||
4fbae7d8 | 477 | err = ipvlan_set_port_mode(port, nmode); |
2ad7bf36 | 478 | } |
a190d04d MB |
479 | |
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); | |
fe89aa6b MB |
487 | |
488 | if (flags & IPVLAN_F_VEPA) | |
489 | ipvlan_mark_vepa(port); | |
490 | else | |
491 | ipvlan_clear_vepa(port); | |
a190d04d MB |
492 | } |
493 | ||
4fbae7d8 | 494 | return err; |
2ad7bf36 MB |
495 | } |
496 | ||
497 | static size_t ipvlan_nl_getsize(const struct net_device *dev) | |
498 | { | |
499 | return (0 | |
500 | + nla_total_size(2) /* IFLA_IPVLAN_MODE */ | |
a190d04d | 501 | + nla_total_size(2) /* IFLA_IPVLAN_FLAGS */ |
2ad7bf36 MB |
502 | ); |
503 | } | |
504 | ||
a8b8a889 MS |
505 | static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[], |
506 | struct netlink_ext_ack *extack) | |
2ad7bf36 | 507 | { |
a190d04d MB |
508 | if (!data) |
509 | return 0; | |
510 | ||
511 | if (data[IFLA_IPVLAN_MODE]) { | |
2ad7bf36 MB |
512 | u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]); |
513 | ||
514 | if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX) | |
515 | return -EINVAL; | |
516 | } | |
a190d04d MB |
517 | if (data[IFLA_IPVLAN_FLAGS]) { |
518 | u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]); | |
519 | ||
fe89aa6b MB |
520 | /* 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)) | |
a190d04d MB |
526 | return -EINVAL; |
527 | } | |
528 | ||
2ad7bf36 MB |
529 | return 0; |
530 | } | |
531 | ||
532 | static 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; | |
a190d04d MB |
545 | if (nla_put_u16(skb, IFLA_IPVLAN_FLAGS, port->flags)) |
546 | goto err; | |
2ad7bf36 MB |
547 | |
548 | return 0; | |
549 | ||
550 | err: | |
551 | return ret; | |
552 | } | |
553 | ||
235a9d89 | 554 | int ipvlan_link_new(struct net *src_net, struct net_device *dev, |
7a3f4a18 MS |
555 | struct nlattr *tb[], struct nlattr *data[], |
556 | struct netlink_ext_ack *extack) | |
2ad7bf36 MB |
557 | { |
558 | struct ipvl_dev *ipvlan = netdev_priv(dev); | |
559 | struct ipvl_port *port; | |
560 | struct net_device *phy_dev; | |
561 | int err; | |
e93fbc5a | 562 | u16 mode = IPVLAN_MODE_L3; |
2ad7bf36 MB |
563 | |
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 | ||
5933fea7 | 571 | if (netif_is_ipvlan(phy_dev)) { |
2ad7bf36 MB |
572 | struct ipvl_dev *tmp = netdev_priv(phy_dev); |
573 | ||
574 | phy_dev = tmp->phy_dev; | |
5933fea7 | 575 | } else if (!netif_is_ipvlan_port(phy_dev)) { |
fe18da60 GM |
576 | /* 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 | } | |
2ad7bf36 | 583 | |
fe18da60 GM |
584 | if (netdev_is_rx_handler_busy(phy_dev)) { |
585 | netdev_err(phy_dev, "Device is already in use.\n"); | |
586 | return -EBUSY; | |
587 | } | |
588 | } | |
2ad7bf36 MB |
589 | |
590 | ipvlan->phy_dev = phy_dev; | |
591 | ipvlan->dev = dev; | |
2ad7bf36 | 592 | ipvlan->sfeatures = IPVLAN_FEATURES; |
296d4856 | 593 | ipvlan_adjust_mtu(ipvlan, phy_dev); |
2ad7bf36 | 594 | INIT_LIST_HEAD(&ipvlan->addrs); |
82308194 | 595 | spin_lock_init(&ipvlan->addrs_lock); |
2ad7bf36 | 596 | |
fe18da60 GM |
597 | /* 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. | |
a190d04d | 600 | */ |
fe18da60 GM |
601 | 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; | |
a190d04d | 612 | |
da36e13c MB |
613 | /* 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 | ||
009146d1 MB |
620 | /* 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 | */ | |
da36e13c MB |
625 | err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE, |
626 | GFP_KERNEL); | |
019ec003 MB |
627 | if (err < 0) |
628 | err = ida_simple_get(&port->ida, 0x1, port->dev_id_start, | |
629 | GFP_KERNEL); | |
009146d1 | 630 | if (err < 0) |
fe18da60 | 631 | goto unregister_netdev; |
009146d1 | 632 | dev->dev_id = err; |
fe18da60 | 633 | |
da36e13c MB |
634 | /* Increment id-base to the next slot for the future assignment */ |
635 | port->dev_id_start = err + 1; | |
009146d1 | 636 | |
fe18da60 GM |
637 | err = netdev_upper_dev_link(phy_dev, dev, extack); |
638 | if (err) | |
639 | goto remove_ida; | |
2ad7bf36 | 640 | |
fe18da60 GM |
641 | /* 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]); | |
2ad7bf36 | 646 | |
fe18da60 GM |
647 | if (data && data[IFLA_IPVLAN_MODE]) |
648 | mode = nla_get_u16(data[IFLA_IPVLAN_MODE]); | |
2ad7bf36 | 649 | |
4fbae7d8 | 650 | err = ipvlan_set_port_mode(port, mode); |
fe18da60 | 651 | if (err) |
1a31cc86 | 652 | goto unlink_netdev; |
2ad7bf36 MB |
653 | |
654 | list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans); | |
655 | netif_stacked_transfer_operstate(phy_dev, dev); | |
656 | return 0; | |
147fd287 | 657 | |
1a31cc86 GF |
658 | unlink_netdev: |
659 | netdev_upper_dev_unlink(phy_dev, dev); | |
009146d1 MB |
660 | remove_ida: |
661 | ida_simple_remove(&port->ida, dev->dev_id); | |
fe18da60 GM |
662 | unregister_netdev: |
663 | unregister_netdevice(dev); | |
147fd287 | 664 | return err; |
2ad7bf36 | 665 | } |
235a9d89 | 666 | EXPORT_SYMBOL_GPL(ipvlan_link_new); |
2ad7bf36 | 667 | |
235a9d89 | 668 | void ipvlan_link_delete(struct net_device *dev, struct list_head *head) |
2ad7bf36 MB |
669 | { |
670 | struct ipvl_dev *ipvlan = netdev_priv(dev); | |
671 | struct ipvl_addr *addr, *next; | |
672 | ||
82308194 | 673 | spin_lock_bh(&ipvlan->addrs_lock); |
515866f8 | 674 | list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) { |
6640e673 | 675 | ipvlan_ht_addr_del(addr); |
82308194 | 676 | list_del_rcu(&addr->anode); |
6a725497 | 677 | kfree_rcu(addr, rcu); |
2ad7bf36 | 678 | } |
82308194 | 679 | spin_unlock_bh(&ipvlan->addrs_lock); |
515866f8 | 680 | |
009146d1 | 681 | ida_simple_remove(&ipvlan->port->ida, dev->dev_id); |
2ad7bf36 MB |
682 | list_del_rcu(&ipvlan->pnode); |
683 | unregister_netdevice_queue(dev, head); | |
684 | netdev_upper_dev_unlink(ipvlan->phy_dev, dev); | |
685 | } | |
235a9d89 | 686 | EXPORT_SYMBOL_GPL(ipvlan_link_delete); |
2ad7bf36 | 687 | |
235a9d89 | 688 | void ipvlan_link_setup(struct net_device *dev) |
2ad7bf36 MB |
689 | { |
690 | ether_setup(dev); | |
691 | ||
692 | dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); | |
bf485bcf | 693 | dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE; |
2ad7bf36 | 694 | dev->netdev_ops = &ipvlan_netdev_ops; |
cf124db5 | 695 | dev->needs_free_netdev = true; |
2ad7bf36 MB |
696 | dev->header_ops = &ipvlan_header_ops; |
697 | dev->ethtool_ops = &ipvlan_ethtool_ops; | |
2ad7bf36 | 698 | } |
235a9d89 | 699 | EXPORT_SYMBOL_GPL(ipvlan_link_setup); |
2ad7bf36 MB |
700 | |
701 | static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] = | |
702 | { | |
703 | [IFLA_IPVLAN_MODE] = { .type = NLA_U16 }, | |
a190d04d | 704 | [IFLA_IPVLAN_FLAGS] = { .type = NLA_U16 }, |
2ad7bf36 MB |
705 | }; |
706 | ||
707 | static struct rtnl_link_ops ipvlan_link_ops = { | |
708 | .kind = "ipvlan", | |
709 | .priv_size = sizeof(struct ipvl_dev), | |
710 | ||
2ad7bf36 MB |
711 | .setup = ipvlan_link_setup, |
712 | .newlink = ipvlan_link_new, | |
713 | .dellink = ipvlan_link_delete, | |
714 | }; | |
715 | ||
235a9d89 | 716 | int ipvlan_link_register(struct rtnl_link_ops *ops) |
2ad7bf36 | 717 | { |
235a9d89 SG |
718 | 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; | |
2ad7bf36 MB |
724 | return rtnl_link_register(ops); |
725 | } | |
235a9d89 | 726 | EXPORT_SYMBOL_GPL(ipvlan_link_register); |
2ad7bf36 MB |
727 | |
728 | static 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 | ||
5933fea7 | 736 | if (!netif_is_ipvlan_port(dev)) |
2ad7bf36 MB |
737 | 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 | ||
3133822f FW |
748 | 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 | } | |
2ad7bf36 MB |
766 | case NETDEV_UNREGISTER: |
767 | if (dev->reg_state != NETREG_UNREGISTERING) | |
768 | break; | |
769 | ||
82308194 | 770 | list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode) |
2ad7bf36 MB |
771 | 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; | |
f6773c5e | 780 | ipvlan->dev->gso_max_segs = dev->gso_max_segs; |
2ad7bf36 MB |
781 | 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 | ||
32c10bbf MB |
790 | 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 | ||
2ad7bf36 MB |
795 | case NETDEV_PRE_TYPE_CHANGE: |
796 | /* Forbid underlying device to change its type. */ | |
797 | return NOTIFY_BAD; | |
798 | } | |
799 | return NOTIFY_DONE; | |
800 | } | |
801 | ||
82308194 | 802 | /* the caller must held the addrs lock */ |
86673982 | 803 | static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6) |
2ad7bf36 MB |
804 | { |
805 | struct ipvl_addr *addr; | |
806 | ||
2ad7bf36 MB |
807 | addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC); |
808 | if (!addr) | |
809 | return -ENOMEM; | |
810 | ||
811 | addr->master = ipvlan; | |
94333fac | 812 | if (!is_v6) { |
86673982 GF |
813 | memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr)); |
814 | addr->atype = IPVL_IPV4; | |
94333fac MC |
815 | #if IS_ENABLED(CONFIG_IPV6) |
816 | } else { | |
817 | memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr)); | |
818 | addr->atype = IPVL_IPV6; | |
819 | #endif | |
86673982 | 820 | } |
82308194 PA |
821 | |
822 | list_add_tail_rcu(&addr->anode, &ipvlan->addrs); | |
515866f8 | 823 | |
27705f70 JB |
824 | /* 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); | |
2ad7bf36 MB |
829 | |
830 | return 0; | |
831 | } | |
832 | ||
86673982 | 833 | static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6) |
2ad7bf36 MB |
834 | { |
835 | struct ipvl_addr *addr; | |
836 | ||
82308194 | 837 | spin_lock_bh(&ipvlan->addrs_lock); |
86673982 | 838 | addr = ipvlan_find_addr(ipvlan, iaddr, is_v6); |
82308194 PA |
839 | if (!addr) { |
840 | spin_unlock_bh(&ipvlan->addrs_lock); | |
2ad7bf36 | 841 | return; |
82308194 | 842 | } |
2ad7bf36 | 843 | |
6640e673 | 844 | ipvlan_ht_addr_del(addr); |
82308194 PA |
845 | list_del_rcu(&addr->anode); |
846 | spin_unlock_bh(&ipvlan->addrs_lock); | |
2ad7bf36 | 847 | kfree_rcu(addr, rcu); |
2ad7bf36 MB |
848 | } |
849 | ||
94333fac MC |
850 | static 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) | |
86673982 GF |
864 | static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr) |
865 | { | |
82308194 PA |
866 | int ret = -EINVAL; |
867 | ||
868 | spin_lock_bh(&ipvlan->addrs_lock); | |
869 | if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true)) | |
86673982 GF |
870 | netif_err(ipvlan, ifup, ipvlan->dev, |
871 | "Failed to add IPv6=%pI6c addr for %s intf\n", | |
872 | ip6_addr, ipvlan->dev->name); | |
82308194 PA |
873 | else |
874 | ret = ipvlan_add_addr(ipvlan, ip6_addr, true); | |
875 | spin_unlock_bh(&ipvlan->addrs_lock); | |
876 | return ret; | |
86673982 GF |
877 | } |
878 | ||
879 | static 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 | ||
2ad7bf36 MB |
884 | static 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 | ||
5e51fe6f | 891 | if (!ipvlan_is_valid_dev(dev)) |
2ad7bf36 MB |
892 | 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 | ||
3ad7d246 KJ |
908 | static 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 | ||
5e51fe6f | 915 | if (!ipvlan_is_valid_dev(dev)) |
3ad7d246 KJ |
916 | return NOTIFY_DONE; |
917 | ||
918 | switch (event) { | |
919 | case NETDEV_UP: | |
de95e047 DA |
920 | 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"); | |
3ad7d246 | 923 | return notifier_from_errno(-EADDRINUSE); |
de95e047 | 924 | } |
3ad7d246 KJ |
925 | break; |
926 | } | |
927 | ||
928 | return NOTIFY_OK; | |
929 | } | |
94333fac | 930 | #endif |
3ad7d246 | 931 | |
2ad7bf36 MB |
932 | static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr) |
933 | { | |
82308194 PA |
934 | int ret = -EINVAL; |
935 | ||
936 | spin_lock_bh(&ipvlan->addrs_lock); | |
937 | if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false)) | |
2ad7bf36 MB |
938 | netif_err(ipvlan, ifup, ipvlan->dev, |
939 | "Failed to add IPv4=%pI4 on %s intf.\n", | |
940 | ip4_addr, ipvlan->dev->name); | |
82308194 PA |
941 | else |
942 | ret = ipvlan_add_addr(ipvlan, ip4_addr, false); | |
943 | spin_unlock_bh(&ipvlan->addrs_lock); | |
944 | return ret; | |
2ad7bf36 MB |
945 | } |
946 | ||
947 | static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr) | |
948 | { | |
86673982 | 949 | return ipvlan_del_addr(ipvlan, ip4_addr, false); |
2ad7bf36 MB |
950 | } |
951 | ||
952 | static 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 | ||
5e51fe6f | 960 | if (!ipvlan_is_valid_dev(dev)) |
2ad7bf36 MB |
961 | 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 | ||
3ad7d246 KJ |
979 | static 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 | ||
5e51fe6f | 986 | if (!ipvlan_is_valid_dev(dev)) |
3ad7d246 KJ |
987 | return NOTIFY_DONE; |
988 | ||
989 | switch (event) { | |
990 | case NETDEV_UP: | |
de95e047 DA |
991 | 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"); | |
3ad7d246 | 994 | return notifier_from_errno(-EADDRINUSE); |
de95e047 | 995 | } |
3ad7d246 KJ |
996 | break; |
997 | } | |
998 | ||
999 | return NOTIFY_OK; | |
1000 | } | |
1001 | ||
2ad7bf36 MB |
1002 | static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = { |
1003 | .notifier_call = ipvlan_addr4_event, | |
1004 | }; | |
1005 | ||
3ad7d246 KJ |
1006 | static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = { |
1007 | .notifier_call = ipvlan_addr4_validator_event, | |
1008 | }; | |
1009 | ||
2ad7bf36 MB |
1010 | static struct notifier_block ipvlan_notifier_block __read_mostly = { |
1011 | .notifier_call = ipvlan_device_event, | |
1012 | }; | |
1013 | ||
94333fac | 1014 | #if IS_ENABLED(CONFIG_IPV6) |
2ad7bf36 MB |
1015 | static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = { |
1016 | .notifier_call = ipvlan_addr6_event, | |
1017 | }; | |
1018 | ||
3ad7d246 KJ |
1019 | static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = { |
1020 | .notifier_call = ipvlan_addr6_validator_event, | |
1021 | }; | |
94333fac | 1022 | #endif |
3ad7d246 | 1023 | |
3133822f FW |
1024 | static 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 | ||
1035 | static struct pernet_operations ipvlan_net_ops = { | |
1036 | .id = &ipvlan_netid, | |
1037 | .size = sizeof(struct ipvlan_netns), | |
1038 | .exit = ipvlan_ns_exit, | |
68eabe8b | 1039 | .async = true, |
3133822f FW |
1040 | }; |
1041 | ||
2ad7bf36 MB |
1042 | static int __init ipvlan_init_module(void) |
1043 | { | |
1044 | int err; | |
1045 | ||
1046 | ipvlan_init_secret(); | |
1047 | register_netdevice_notifier(&ipvlan_notifier_block); | |
94333fac | 1048 | #if IS_ENABLED(CONFIG_IPV6) |
2ad7bf36 | 1049 | register_inet6addr_notifier(&ipvlan_addr6_notifier_block); |
3ad7d246 KJ |
1050 | register_inet6addr_validator_notifier( |
1051 | &ipvlan_addr6_vtor_notifier_block); | |
94333fac | 1052 | #endif |
2ad7bf36 | 1053 | register_inetaddr_notifier(&ipvlan_addr4_notifier_block); |
3ad7d246 | 1054 | register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block); |
2ad7bf36 | 1055 | |
3133822f | 1056 | err = register_pernet_subsys(&ipvlan_net_ops); |
2ad7bf36 MB |
1057 | if (err < 0) |
1058 | goto error; | |
1059 | ||
3133822f FW |
1060 | err = ipvlan_link_register(&ipvlan_link_ops); |
1061 | if (err < 0) { | |
1062 | unregister_pernet_subsys(&ipvlan_net_ops); | |
1063 | goto error; | |
1064 | } | |
1065 | ||
2ad7bf36 MB |
1066 | return 0; |
1067 | error: | |
1068 | unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block); | |
3ad7d246 KJ |
1069 | unregister_inetaddr_validator_notifier( |
1070 | &ipvlan_addr4_vtor_notifier_block); | |
94333fac | 1071 | #if IS_ENABLED(CONFIG_IPV6) |
2ad7bf36 | 1072 | unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block); |
3ad7d246 KJ |
1073 | unregister_inet6addr_validator_notifier( |
1074 | &ipvlan_addr6_vtor_notifier_block); | |
94333fac | 1075 | #endif |
2ad7bf36 MB |
1076 | unregister_netdevice_notifier(&ipvlan_notifier_block); |
1077 | return err; | |
1078 | } | |
1079 | ||
1080 | static void __exit ipvlan_cleanup_module(void) | |
1081 | { | |
1082 | rtnl_link_unregister(&ipvlan_link_ops); | |
3133822f | 1083 | unregister_pernet_subsys(&ipvlan_net_ops); |
2ad7bf36 MB |
1084 | unregister_netdevice_notifier(&ipvlan_notifier_block); |
1085 | unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block); | |
3ad7d246 KJ |
1086 | unregister_inetaddr_validator_notifier( |
1087 | &ipvlan_addr4_vtor_notifier_block); | |
94333fac | 1088 | #if IS_ENABLED(CONFIG_IPV6) |
2ad7bf36 | 1089 | unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block); |
3ad7d246 KJ |
1090 | unregister_inet6addr_validator_notifier( |
1091 | &ipvlan_addr6_vtor_notifier_block); | |
94333fac | 1092 | #endif |
2ad7bf36 MB |
1093 | } |
1094 | ||
1095 | module_init(ipvlan_init_module); | |
1096 | module_exit(ipvlan_cleanup_module); | |
1097 | ||
1098 | MODULE_LICENSE("GPL"); | |
1099 | MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>"); | |
1100 | MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs"); | |
1101 | MODULE_ALIAS_RTNL_LINK("ipvlan"); |