blob: 123a6d39438f8dc04c4745c7063c8ba22ec50b7a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * NET3 IP device support routines.
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Derived from the IP parts of dev.c 1.0.19
Jesper Juhl02c30a82005-05-05 16:16:16 -07006 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
8 * Mark Evans, <evansmp@uhura.aston.ac.uk>
9 *
10 * Additional Authors:
11 * Alan Cox, <gw4pts@gw4pts.ampr.org>
12 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13 *
14 * Changes:
15 * Alexey Kuznetsov: pa_* fields are replaced with ifaddr
16 * lists.
17 * Cyrus Durgin: updated for kmod
18 * Matthias Andree: in devinet_ioctl, compare label and
19 * address (4.4BSD alias style support),
20 * fall back to comparing just the label
21 * if no match found.
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080025#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/types.h>
30#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010031#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/string.h>
33#include <linux/mm.h>
34#include <linux/socket.h>
35#include <linux/sockios.h>
36#include <linux/in.h>
37#include <linux/errno.h>
38#include <linux/interrupt.h>
Thomas Graf18237302006-08-04 23:04:54 -070039#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_ether.h>
41#include <linux/inet.h>
42#include <linux/netdevice.h>
43#include <linux/etherdevice.h>
44#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/init.h>
46#include <linux/notifier.h>
47#include <linux/inetdevice.h>
48#include <linux/igmp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
David S. Millerfd23c3b2011-02-18 12:42:28 -080050#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifdef CONFIG_SYSCTL
52#include <linux/sysctl.h>
53#endif
54#include <linux/kmod.h>
Nicolas Dichteledc9e742012-10-25 22:28:52 +000055#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020057#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <net/ip.h>
59#include <net/route.h>
60#include <net/ip_fib.h>
Thomas Graf63f34442007-03-22 11:55:17 -070061#include <net/rtnetlink.h>
Pavel Emelyanov752d14d2007-12-16 13:31:47 -080062#include <net/net_namespace.h>
Jiri Pirko5c766d62013-01-24 09:41:41 +000063#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Matteo Croce2e605462019-07-01 19:01:55 +020065#define IPV6ONLY_FLAGS \
66 (IFA_F_NODAD | IFA_F_OPTIMISTIC | IFA_F_DADFAILED | \
67 IFA_F_HOMEADDRESS | IFA_F_TENTATIVE | \
68 IFA_F_MANAGETEMPADDR | IFA_F_STABLE_PRIVACY)
69
Adrian Bunk0027ba82008-01-31 17:17:31 -080070static struct ipv4_devconf ipv4_devconf = {
Herbert Xu42f811b2007-06-04 23:34:44 -070071 .data = {
Eric W. Biederman02291682010-02-14 03:25:51 +000072 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
73 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
74 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
75 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
William Manley26900482013-08-06 19:03:15 +010076 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
77 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/,
Herbert Xu42f811b2007-06-04 23:34:44 -070078 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
81static struct ipv4_devconf ipv4_devconf_dflt = {
Herbert Xu42f811b2007-06-04 23:34:44 -070082 .data = {
Eric W. Biederman02291682010-02-14 03:25:51 +000083 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
84 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
85 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
86 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
87 [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
William Manley26900482013-08-06 19:03:15 +010088 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
89 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/,
Herbert Xu42f811b2007-06-04 23:34:44 -070090 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -080093#define IPV4_DEVCONF_DFLT(net, attr) \
94 IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
Herbert Xu42f811b2007-06-04 23:34:44 -070095
Patrick McHardyef7c79e2007-06-05 12:38:30 -070096static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
Thomas Graf5c753972006-08-04 23:03:53 -070097 [IFA_LOCAL] = { .type = NLA_U32 },
98 [IFA_ADDRESS] = { .type = NLA_U32 },
99 [IFA_BROADCAST] = { .type = NLA_U32 },
Thomas Graf5176f912006-08-26 20:13:18 -0700100 [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
Jiri Pirko5c766d62013-01-24 09:41:41 +0000101 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirkoad6c8132013-12-08 12:16:10 +0100102 [IFA_FLAGS] = { .type = NLA_U32 },
David Ahernaf4d7682018-05-27 08:09:57 -0700103 [IFA_RT_PRIORITY] = { .type = NLA_U32 },
Christian Braunerd3807142018-09-04 21:53:49 +0200104 [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
Thomas Graf5c753972006-08-04 23:03:53 -0700105};
106
Christian Brauner978a46f2018-09-04 21:53:54 +0200107struct inet_fill_args {
108 u32 portid;
109 u32 seq;
110 int event;
111 unsigned int flags;
112 int netnsid;
David Ahern5fcd2662018-10-19 12:45:29 -0700113 int ifindex;
Christian Brauner978a46f2018-09-04 21:53:54 +0200114};
115
Eric Dumazet40384992012-08-03 21:06:50 +0000116#define IN4_ADDR_HSIZE_SHIFT 8
117#define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT)
118
David S. Millerfd23c3b2011-02-18 12:42:28 -0800119static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE];
David S. Millerfd23c3b2011-02-18 12:42:28 -0800120
Eric Dumazet6eada012015-03-18 14:05:33 -0700121static u32 inet_addr_hash(const struct net *net, __be32 addr)
David S. Millerfd23c3b2011-02-18 12:42:28 -0800122{
Eric Dumazet40384992012-08-03 21:06:50 +0000123 u32 val = (__force u32) addr ^ net_hash_mix(net);
David S. Millerfd23c3b2011-02-18 12:42:28 -0800124
Eric Dumazet40384992012-08-03 21:06:50 +0000125 return hash_32(val, IN4_ADDR_HSIZE_SHIFT);
David S. Millerfd23c3b2011-02-18 12:42:28 -0800126}
127
128static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
129{
Eric Dumazet40384992012-08-03 21:06:50 +0000130 u32 hash = inet_addr_hash(net, ifa->ifa_local);
David S. Millerfd23c3b2011-02-18 12:42:28 -0800131
WANG Cong32a4be42014-05-06 11:15:56 -0700132 ASSERT_RTNL();
David S. Millerfd23c3b2011-02-18 12:42:28 -0800133 hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]);
David S. Millerfd23c3b2011-02-18 12:42:28 -0800134}
135
136static void inet_hash_remove(struct in_ifaddr *ifa)
137{
WANG Cong32a4be42014-05-06 11:15:56 -0700138 ASSERT_RTNL();
David S. Millerfd23c3b2011-02-18 12:42:28 -0800139 hlist_del_init_rcu(&ifa->hash);
David S. Millerfd23c3b2011-02-18 12:42:28 -0800140}
141
David S. Miller9435eb12011-02-18 12:43:09 -0800142/**
143 * __ip_dev_find - find the first device with a given source address.
144 * @net: the net namespace
145 * @addr: the source address
146 * @devref: if true, take a reference on the found device
147 *
148 * If a caller uses devref=false, it should be protected by RCU, or RTNL
149 */
150struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
151{
David S. Miller9435eb12011-02-18 12:43:09 -0800152 struct net_device *result = NULL;
153 struct in_ifaddr *ifa;
David S. Miller9435eb12011-02-18 12:43:09 -0800154
155 rcu_read_lock();
Paolo Abeni6e617de2017-09-20 18:26:53 +0200156 ifa = inet_lookup_ifaddr_rcu(net, addr);
157 if (!ifa) {
David S. Miller406b6f92011-03-22 21:56:23 -0700158 struct flowi4 fl4 = { .daddr = addr };
159 struct fib_result res = { 0 };
160 struct fib_table *local;
161
162 /* Fallback to FIB local table so that communication
163 * over loopback subnets work.
164 */
165 local = fib_get_table(net, RT_TABLE_LOCAL);
166 if (local &&
167 !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) &&
168 res.type == RTN_LOCAL)
169 result = FIB_RES_DEV(res);
Paolo Abeni6e617de2017-09-20 18:26:53 +0200170 } else {
171 result = ifa->ifa_dev->dev;
David S. Miller406b6f92011-03-22 21:56:23 -0700172 }
David S. Miller9435eb12011-02-18 12:43:09 -0800173 if (result && devref)
174 dev_hold(result);
175 rcu_read_unlock();
176 return result;
177}
178EXPORT_SYMBOL(__ip_dev_find);
179
Paolo Abeni6e617de2017-09-20 18:26:53 +0200180/* called under RCU lock */
181struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr)
182{
183 u32 hash = inet_addr_hash(net, addr);
184 struct in_ifaddr *ifa;
185
186 hlist_for_each_entry_rcu(ifa, &inet_addr_lst[hash], hash)
187 if (ifa->ifa_local == addr &&
188 net_eq(dev_net(ifa->ifa_dev->dev), net))
189 return ifa;
190
191 return NULL;
192}
193
Thomas Grafd6062cb2006-08-15 00:33:59 -0700194static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Alan Sterne041c682006-03-27 01:16:30 -0800196static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
Krister Johansen3ad7d242017-06-08 13:12:14 -0700197static BLOCKING_NOTIFIER_HEAD(inetaddr_validator_chain);
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200198static void inet_del_ifa(struct in_device *in_dev,
199 struct in_ifaddr __rcu **ifap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int destroy);
201#ifdef CONFIG_SYSCTL
WANG Cong20e61da2014-07-25 15:25:08 -0700202static int devinet_sysctl_register(struct in_device *idev);
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800203static void devinet_sysctl_unregister(struct in_device *idev);
204#else
WANG Cong20e61da2014-07-25 15:25:08 -0700205static int devinet_sysctl_register(struct in_device *idev)
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800206{
WANG Cong20e61da2014-07-25 15:25:08 -0700207 return 0;
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800208}
Eric Dumazet40384992012-08-03 21:06:50 +0000209static void devinet_sysctl_unregister(struct in_device *idev)
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800210{
211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#endif
213
214/* Locks all the inet devices. */
215
216static struct in_ifaddr *inet_alloc_ifa(void)
217{
Alexey Dobriyan93adcc82008-10-28 13:25:09 -0700218 return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221static void inet_rcu_free_ifa(struct rcu_head *head)
222{
223 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
224 if (ifa->ifa_dev)
225 in_dev_put(ifa->ifa_dev);
226 kfree(ifa);
227}
228
Eric Dumazet40384992012-08-03 21:06:50 +0000229static void inet_free_ifa(struct in_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
232}
233
234void in_dev_finish_destroy(struct in_device *idev)
235{
236 struct net_device *dev = idev->dev;
237
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700238 WARN_ON(idev->ifa_list);
239 WARN_ON(idev->mc_list);
Eric Dumazete9897072013-06-07 08:48:57 -0700240 kfree(rcu_dereference_protected(idev->mc_hash, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000242 pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#endif
244 dev_put(dev);
245 if (!idev->dead)
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800246 pr_err("Freeing alive in_device %p\n", idev);
247 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 kfree(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800250EXPORT_SYMBOL(in_dev_finish_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Herbert Xu71e27da2007-06-04 23:36:06 -0700252static struct in_device *inetdev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 struct in_device *in_dev;
WANG Cong20e61da2014-07-25 15:25:08 -0700255 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 ASSERT_RTNL();
258
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700259 in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!in_dev)
261 goto out;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900262 memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -0800263 sizeof(in_dev->cnf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 in_dev->cnf.sysctl = NULL;
265 in_dev->dev = dev;
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800266 in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl);
267 if (!in_dev->arp_parms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto out_kfree;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700269 if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
270 dev_disable_lro(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* Reference in_dev->dev */
272 dev_hold(dev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800273 /* Account for reference dev->ip_ptr (below) */
Reshetova, Elena7658b362017-06-30 13:08:03 +0300274 refcount_set(&in_dev->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
WANG Cong20e61da2014-07-25 15:25:08 -0700276 err = devinet_sysctl_register(in_dev);
277 if (err) {
278 in_dev->dead = 1;
Yang Yingliang1b49cd72020-05-30 11:34:33 +0800279 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
WANG Cong20e61da2014-07-25 15:25:08 -0700280 in_dev_put(in_dev);
281 in_dev = NULL;
282 goto out;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 ip_mc_init_dev(in_dev);
285 if (dev->flags & IFF_UP)
286 ip_mc_up(in_dev);
Jarek Poplawski483479e2007-01-09 14:38:31 -0800287
David L Stevens30c4cf52007-01-04 12:31:14 -0800288 /* we can receive as soon as ip_ptr is set -- do this last */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000289 rcu_assign_pointer(dev->ip_ptr, in_dev);
Jarek Poplawski483479e2007-01-09 14:38:31 -0800290out:
WANG Cong20e61da2014-07-25 15:25:08 -0700291 return in_dev ?: ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292out_kfree:
293 kfree(in_dev);
294 in_dev = NULL;
295 goto out;
296}
297
298static void in_dev_rcu_put(struct rcu_head *head)
299{
300 struct in_device *idev = container_of(head, struct in_device, rcu_head);
301 in_dev_put(idev);
302}
303
304static void inetdev_destroy(struct in_device *in_dev)
305{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct net_device *dev;
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200307 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 ASSERT_RTNL();
310
311 dev = in_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 in_dev->dead = 1;
314
315 ip_mc_destroy_dev(in_dev);
316
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200317 while ((ifa = rtnl_dereference(in_dev->ifa_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
319 inet_free_ifa(ifa);
320 }
321
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000322 RCU_INIT_POINTER(dev->ip_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800324 devinet_sysctl_unregister(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
326 arp_ifdown(dev);
327
328 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
329}
330
Al Viroff428d72006-09-26 22:13:35 -0700331int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Florian Westphald519e872019-05-31 18:27:05 +0200333 const struct in_ifaddr *ifa;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 rcu_read_lock();
Florian Westphald519e872019-05-31 18:27:05 +0200336 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (inet_ifa_match(a, ifa)) {
338 if (!b || inet_ifa_match(b, ifa)) {
339 rcu_read_unlock();
340 return 1;
341 }
342 }
Florian Westphald519e872019-05-31 18:27:05 +0200343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 rcu_read_unlock();
345 return 0;
346}
347
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200348static void __inet_del_ifa(struct in_device *in_dev,
349 struct in_ifaddr __rcu **ifap,
350 int destroy, struct nlmsghdr *nlh, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Harald Welte8f937c62005-05-29 20:23:46 -0700352 struct in_ifaddr *promote = NULL;
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200353 struct in_ifaddr *ifa, *ifa1;
354 struct in_ifaddr *last_prim;
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800355 struct in_ifaddr *prev_prom = NULL;
356 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 ASSERT_RTNL();
359
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200360 ifa1 = rtnl_dereference(*ifap);
361 last_prim = rtnl_dereference(in_dev->ifa_list);
David S. Millerfbd40ea2016-03-13 23:28:00 -0400362 if (in_dev->dead)
363 goto no_promotions;
364
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900365 /* 1. Deleting primary ifaddr forces deletion all secondaries
Harald Welte8f937c62005-05-29 20:23:46 -0700366 * unless alias promotion is set
367 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200370 struct in_ifaddr __rcu **ifap1 = &ifa1->ifa_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200372 while ((ifa = rtnl_dereference(*ifap1)) != NULL) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900373 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800374 ifa1->ifa_scope <= ifa->ifa_scope)
375 last_prim = ifa;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
378 ifa1->ifa_mask != ifa->ifa_mask ||
379 !inet_ifa_match(ifa1->ifa_address, ifa)) {
380 ifap1 = &ifa->ifa_next;
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800381 prev_prom = ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 continue;
383 }
384
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800385 if (!do_promote) {
David S. Millerfd23c3b2011-02-18 12:42:28 -0800386 inet_hash_remove(ifa);
Harald Welte8f937c62005-05-29 20:23:46 -0700387 *ifap1 = ifa->ifa_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Eric W. Biederman15e47302012-09-07 20:12:54 +0000389 rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid);
Alan Sterne041c682006-03-27 01:16:30 -0800390 blocking_notifier_call_chain(&inetaddr_chain,
391 NETDEV_DOWN, ifa);
Harald Welte8f937c62005-05-29 20:23:46 -0700392 inet_free_ifa(ifa);
393 } else {
394 promote = ifa;
395 break;
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 }
399
Julian Anastasov2d230e22011-03-19 12:13:52 +0000400 /* On promotion all secondaries from subnet are changing
401 * the primary IP, we must remove all their routes silently
402 * and later to add them back with new prefsrc. Do this
403 * while all addresses are on the device list.
404 */
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200405 for (ifa = promote; ifa; ifa = rtnl_dereference(ifa->ifa_next)) {
Julian Anastasov2d230e22011-03-19 12:13:52 +0000406 if (ifa1->ifa_mask == ifa->ifa_mask &&
407 inet_ifa_match(ifa1->ifa_address, ifa))
408 fib_del_ifaddr(ifa, ifa1);
409 }
410
David S. Millerfbd40ea2016-03-13 23:28:00 -0400411no_promotions:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* 2. Unlink it */
413
414 *ifap = ifa1->ifa_next;
David S. Millerfd23c3b2011-02-18 12:42:28 -0800415 inet_hash_remove(ifa1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /* 3. Announce address deletion */
418
419 /* Send message first, then call notifier.
420 At first sight, FIB update triggered by notifier
421 will refer to already deleted ifaddr, that could confuse
422 netlink listeners. It is not true: look, gated sees
423 that route deleted and if it still thinks that ifaddr
424 is valid, it will try to restore deleted routes... Grr.
425 So that, this order is correct.
426 */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000427 rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid);
Alan Sterne041c682006-03-27 01:16:30 -0800428 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800429
430 if (promote) {
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200431 struct in_ifaddr *next_sec;
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800432
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200433 next_sec = rtnl_dereference(promote->ifa_next);
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800434 if (prev_prom) {
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200435 struct in_ifaddr *last_sec;
436
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200437 rcu_assign_pointer(prev_prom->ifa_next, next_sec);
Florian Westphal6a9e9ce2019-06-27 14:03:32 +0200438
439 last_sec = rtnl_dereference(last_prim->ifa_next);
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200440 rcu_assign_pointer(promote->ifa_next, last_sec);
441 rcu_assign_pointer(last_prim->ifa_next, promote);
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800442 }
443
444 promote->ifa_flags &= ~IFA_F_SECONDARY;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000445 rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid);
Alan Sterne041c682006-03-27 01:16:30 -0800446 blocking_notifier_call_chain(&inetaddr_chain,
447 NETDEV_UP, promote);
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200448 for (ifa = next_sec; ifa;
449 ifa = rtnl_dereference(ifa->ifa_next)) {
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800450 if (ifa1->ifa_mask != ifa->ifa_mask ||
451 !inet_ifa_match(ifa1->ifa_address, ifa))
452 continue;
453 fib_add_ifaddr(ifa);
454 }
455
456 }
Herbert Xu63630972007-06-07 18:35:38 -0700457 if (destroy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 inet_free_ifa(ifa1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200461static void inet_del_ifa(struct in_device *in_dev,
462 struct in_ifaddr __rcu **ifap,
Thomas Grafd6062cb2006-08-15 00:33:59 -0700463 int destroy)
464{
465 __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
466}
467
Jiri Pirko5c766d62013-01-24 09:41:41 +0000468static void check_lifetime(struct work_struct *work);
469
470static DECLARE_DELAYED_WORK(check_lifetime_work, check_lifetime);
471
Thomas Grafd6062cb2006-08-15 00:33:59 -0700472static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
David Ahernde95e042017-10-18 09:56:54 -0700473 u32 portid, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200475 struct in_ifaddr __rcu **last_primary, **ifap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 struct in_device *in_dev = ifa->ifa_dev;
Krister Johansen3ad7d242017-06-08 13:12:14 -0700477 struct in_validator_info ivi;
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200478 struct in_ifaddr *ifa1;
Krister Johansen3ad7d242017-06-08 13:12:14 -0700479 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 ASSERT_RTNL();
482
483 if (!ifa->ifa_local) {
484 inet_free_ifa(ifa);
485 return 0;
486 }
487
488 ifa->ifa_flags &= ~IFA_F_SECONDARY;
489 last_primary = &in_dev->ifa_list;
490
Matteo Croce2e605462019-07-01 19:01:55 +0200491 /* Don't set IPv6 only flags to IPv4 addresses */
492 ifa->ifa_flags &= ~IPV6ONLY_FLAGS;
493
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200494 ifap = &in_dev->ifa_list;
495 ifa1 = rtnl_dereference(*ifap);
496
497 while (ifa1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
499 ifa->ifa_scope <= ifa1->ifa_scope)
500 last_primary = &ifa1->ifa_next;
501 if (ifa1->ifa_mask == ifa->ifa_mask &&
502 inet_ifa_match(ifa1->ifa_address, ifa)) {
503 if (ifa1->ifa_local == ifa->ifa_local) {
504 inet_free_ifa(ifa);
505 return -EEXIST;
506 }
507 if (ifa1->ifa_scope != ifa->ifa_scope) {
508 inet_free_ifa(ifa);
509 return -EINVAL;
510 }
511 ifa->ifa_flags |= IFA_F_SECONDARY;
512 }
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200513
514 ifap = &ifa1->ifa_next;
515 ifa1 = rtnl_dereference(*ifap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
Krister Johansen3ad7d242017-06-08 13:12:14 -0700518 /* Allow any devices that wish to register ifaddr validtors to weigh
519 * in now, before changes are committed. The rntl lock is serializing
520 * access here, so the state should not change between a validator call
521 * and a final notify on commit. This isn't invoked on promotion under
522 * the assumption that validators are checking the address itself, and
523 * not the flags.
524 */
525 ivi.ivi_addr = ifa->ifa_address;
526 ivi.ivi_dev = ifa->ifa_dev;
David Ahernde95e042017-10-18 09:56:54 -0700527 ivi.extack = extack;
Krister Johansen3ad7d242017-06-08 13:12:14 -0700528 ret = blocking_notifier_call_chain(&inetaddr_validator_chain,
529 NETDEV_UP, &ivi);
530 ret = notifier_to_errno(ret);
531 if (ret) {
532 inet_free_ifa(ifa);
533 return ret;
534 }
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500537 prandom_seed((__force u32) ifa->ifa_local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 ifap = last_primary;
539 }
540
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200541 rcu_assign_pointer(ifa->ifa_next, *ifap);
542 rcu_assign_pointer(*ifap, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
David S. Millerfd23c3b2011-02-18 12:42:28 -0800544 inet_hash_insert(dev_net(in_dev->dev), ifa);
545
Jiri Pirko5c766d62013-01-24 09:41:41 +0000546 cancel_delayed_work(&check_lifetime_work);
viresh kumar906e0732014-01-22 12:23:32 +0530547 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
Jiri Pirko5c766d62013-01-24 09:41:41 +0000548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* Send message first, then call notifier.
550 Notifier will trigger FIB update, so that
551 listeners of netlink will know about new ifaddr */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000552 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, portid);
Alan Sterne041c682006-03-27 01:16:30 -0800553 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 return 0;
556}
557
Thomas Grafd6062cb2006-08-15 00:33:59 -0700558static int inet_insert_ifa(struct in_ifaddr *ifa)
559{
David Ahernde95e042017-10-18 09:56:54 -0700560 return __inet_insert_ifa(ifa, NULL, 0, NULL);
Thomas Grafd6062cb2006-08-15 00:33:59 -0700561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
564{
Herbert Xue5ed6392005-10-03 14:35:55 -0700565 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 ASSERT_RTNL();
568
569 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700570 inet_free_ifa(ifa);
571 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Herbert Xu71e27da2007-06-04 23:36:06 -0700573 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100574 neigh_parms_data_state_setall(in_dev->arp_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (ifa->ifa_dev != in_dev) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700576 WARN_ON(ifa->ifa_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 in_dev_hold(in_dev);
578 ifa->ifa_dev = in_dev;
579 }
Joe Perchesf97c1e02007-12-16 13:45:43 -0800580 if (ipv4_is_loopback(ifa->ifa_local))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 ifa->ifa_scope = RT_SCOPE_HOST;
582 return inet_insert_ifa(ifa);
583}
584
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000585/* Caller must hold RCU or RTNL :
586 * We dont take a reference on found in_device
587 */
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -0800588struct in_device *inetdev_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct net_device *dev;
591 struct in_device *in_dev = NULL;
Eric Dumazetc148fc22009-11-01 19:23:04 +0000592
593 rcu_read_lock();
594 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (dev)
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000596 in_dev = rcu_dereference_rtnl(dev->ip_ptr);
Eric Dumazetc148fc22009-11-01 19:23:04 +0000597 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return in_dev;
599}
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800600EXPORT_SYMBOL(inetdev_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602/* Called only from RTNL semaphored context. No locks. */
603
Al Viro60cad5d2006-09-26 22:17:09 -0700604struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
605 __be32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Florian Westphald519e872019-05-31 18:27:05 +0200607 struct in_ifaddr *ifa;
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 ASSERT_RTNL();
610
Florian Westphald519e872019-05-31 18:27:05 +0200611 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
613 return ifa;
Florian Westphald519e872019-05-31 18:27:05 +0200614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return NULL;
616}
617
Taras Chornyi690cc862020-04-09 20:25:24 +0300618static int ip_mc_autojoin_config(struct net *net, bool join,
619 const struct in_ifaddr *ifa)
Madhu Challa93a714d2015-02-25 09:58:35 -0800620{
Taras Chornyi690cc862020-04-09 20:25:24 +0300621#if defined(CONFIG_IP_MULTICAST)
Madhu Challa93a714d2015-02-25 09:58:35 -0800622 struct ip_mreqn mreq = {
623 .imr_multiaddr.s_addr = ifa->ifa_address,
624 .imr_ifindex = ifa->ifa_dev->dev->ifindex,
625 };
Taras Chornyi690cc862020-04-09 20:25:24 +0300626 struct sock *sk = net->ipv4.mc_autojoin_sk;
Madhu Challa93a714d2015-02-25 09:58:35 -0800627 int ret;
628
629 ASSERT_RTNL();
630
631 lock_sock(sk);
632 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300633 ret = ip_mc_join_group(sk, &mreq);
Madhu Challa93a714d2015-02-25 09:58:35 -0800634 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300635 ret = ip_mc_leave_group(sk, &mreq);
Madhu Challa93a714d2015-02-25 09:58:35 -0800636 release_sock(sk);
637
638 return ret;
Taras Chornyi690cc862020-04-09 20:25:24 +0300639#else
640 return -EOPNOTSUPP;
641#endif
Madhu Challa93a714d2015-02-25 09:58:35 -0800642}
643
David Ahernc21ef3e2017-04-16 09:48:24 -0700644static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
645 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900647 struct net *net = sock_net(skb->sk);
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200648 struct in_ifaddr __rcu **ifap;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700649 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct in_device *in_dev;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700651 struct ifaddrmsg *ifm;
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200652 struct in_ifaddr *ifa;
653
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700654 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 ASSERT_RTNL();
657
Johannes Berg8cb08172019-04-26 14:07:28 +0200658 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
659 ifa_ipv4_policy, extack);
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700660 if (err < 0)
661 goto errout;
662
663 ifm = nlmsg_data(nlh);
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -0800664 in_dev = inetdev_by_index(net, ifm->ifa_index);
Ian Morris51456b22015-04-03 09:17:26 +0100665 if (!in_dev) {
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700666 err = -ENODEV;
667 goto errout;
668 }
669
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200670 for (ifap = &in_dev->ifa_list; (ifa = rtnl_dereference(*ifap)) != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 ifap = &ifa->ifa_next) {
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700672 if (tb[IFA_LOCAL] &&
Jiri Benc67b61f62015-03-29 16:59:26 +0200673 ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 continue;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700675
676 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
677 continue;
678
679 if (tb[IFA_ADDRESS] &&
680 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
Jiri Benc67b61f62015-03-29 16:59:26 +0200681 !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa)))
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700682 continue;
683
Madhu Challa93a714d2015-02-25 09:58:35 -0800684 if (ipv4_is_multicast(ifa->ifa_address))
Taras Chornyi690cc862020-04-09 20:25:24 +0300685 ip_mc_autojoin_config(net, false, ifa);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000686 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
688 }
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700689
690 err = -EADDRNOTAVAIL;
691errout:
692 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Jiri Pirko5c766d62013-01-24 09:41:41 +0000695#define INFINITY_LIFE_TIME 0xFFFFFFFF
696
697static void check_lifetime(struct work_struct *work)
698{
699 unsigned long now, next, next_sec, next_sched;
700 struct in_ifaddr *ifa;
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000701 struct hlist_node *n;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000702 int i;
703
704 now = jiffies;
705 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
706
Jiri Pirko5c766d62013-01-24 09:41:41 +0000707 for (i = 0; i < IN4_ADDR_HSIZE; i++) {
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000708 bool change_needed = false;
709
710 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800711 hlist_for_each_entry_rcu(ifa, &inet_addr_lst[i], hash) {
Jiri Pirko5c766d62013-01-24 09:41:41 +0000712 unsigned long age;
713
714 if (ifa->ifa_flags & IFA_F_PERMANENT)
715 continue;
716
717 /* We try to batch several events at once. */
718 age = (now - ifa->ifa_tstamp +
719 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
720
721 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
722 age >= ifa->ifa_valid_lft) {
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000723 change_needed = true;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000724 } else if (ifa->ifa_preferred_lft ==
725 INFINITY_LIFE_TIME) {
726 continue;
727 } else if (age >= ifa->ifa_preferred_lft) {
728 if (time_before(ifa->ifa_tstamp +
729 ifa->ifa_valid_lft * HZ, next))
730 next = ifa->ifa_tstamp +
731 ifa->ifa_valid_lft * HZ;
732
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000733 if (!(ifa->ifa_flags & IFA_F_DEPRECATED))
734 change_needed = true;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000735 } else if (time_before(ifa->ifa_tstamp +
736 ifa->ifa_preferred_lft * HZ,
737 next)) {
738 next = ifa->ifa_tstamp +
739 ifa->ifa_preferred_lft * HZ;
740 }
741 }
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000742 rcu_read_unlock();
743 if (!change_needed)
744 continue;
745 rtnl_lock();
746 hlist_for_each_entry_safe(ifa, n, &inet_addr_lst[i], hash) {
747 unsigned long age;
748
749 if (ifa->ifa_flags & IFA_F_PERMANENT)
750 continue;
751
752 /* We try to batch several events at once. */
753 age = (now - ifa->ifa_tstamp +
754 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
755
756 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
757 age >= ifa->ifa_valid_lft) {
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200758 struct in_ifaddr __rcu **ifap;
759 struct in_ifaddr *tmp;
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000760
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200761 ifap = &ifa->ifa_dev->ifa_list;
762 tmp = rtnl_dereference(*ifap);
763 while (tmp) {
Florian Westphal40008e92019-06-17 16:02:27 +0200764 if (tmp == ifa) {
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000765 inet_del_ifa(ifa->ifa_dev,
766 ifap, 1);
767 break;
768 }
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200769 ifap = &tmp->ifa_next;
770 tmp = rtnl_dereference(*ifap);
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000771 }
772 } else if (ifa->ifa_preferred_lft !=
773 INFINITY_LIFE_TIME &&
774 age >= ifa->ifa_preferred_lft &&
775 !(ifa->ifa_flags & IFA_F_DEPRECATED)) {
776 ifa->ifa_flags |= IFA_F_DEPRECATED;
777 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
778 }
779 }
780 rtnl_unlock();
Jiri Pirko5c766d62013-01-24 09:41:41 +0000781 }
Jiri Pirko5c766d62013-01-24 09:41:41 +0000782
783 next_sec = round_jiffies_up(next);
784 next_sched = next;
785
786 /* If rounded timeout is accurate enough, accept it. */
787 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
788 next_sched = next_sec;
789
790 now = jiffies;
791 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
792 if (time_before(next_sched, now + ADDRCONF_TIMER_FUZZ_MAX))
793 next_sched = now + ADDRCONF_TIMER_FUZZ_MAX;
794
viresh kumar906e0732014-01-22 12:23:32 +0530795 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work,
796 next_sched - now);
Jiri Pirko5c766d62013-01-24 09:41:41 +0000797}
798
799static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft,
800 __u32 prefered_lft)
801{
802 unsigned long timeout;
803
804 ifa->ifa_flags &= ~(IFA_F_PERMANENT | IFA_F_DEPRECATED);
805
806 timeout = addrconf_timeout_fixup(valid_lft, HZ);
807 if (addrconf_finite_timeout(timeout))
808 ifa->ifa_valid_lft = timeout;
809 else
810 ifa->ifa_flags |= IFA_F_PERMANENT;
811
812 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
813 if (addrconf_finite_timeout(timeout)) {
814 if (timeout == 0)
815 ifa->ifa_flags |= IFA_F_DEPRECATED;
816 ifa->ifa_preferred_lft = timeout;
817 }
818 ifa->ifa_tstamp = jiffies;
819 if (!ifa->ifa_cstamp)
820 ifa->ifa_cstamp = ifa->ifa_tstamp;
821}
822
823static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
David Aherndac9c972018-10-07 20:16:24 -0700824 __u32 *pvalid_lft, __u32 *pprefered_lft,
825 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Thomas Graf5c753972006-08-04 23:03:53 -0700827 struct nlattr *tb[IFA_MAX+1];
828 struct in_ifaddr *ifa;
829 struct ifaddrmsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 struct net_device *dev;
831 struct in_device *in_dev;
Denis V. Lunev7b218572008-01-31 18:47:00 -0800832 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Johannes Berg8cb08172019-04-26 14:07:28 +0200834 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
835 ifa_ipv4_policy, extack);
Thomas Graf5c753972006-08-04 23:03:53 -0700836 if (err < 0)
837 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Thomas Graf5c753972006-08-04 23:03:53 -0700839 ifm = nlmsg_data(nlh);
Denis V. Lunev7b218572008-01-31 18:47:00 -0800840 err = -EINVAL;
Ian Morris51456b22015-04-03 09:17:26 +0100841 if (ifm->ifa_prefixlen > 32 || !tb[IFA_LOCAL])
Thomas Graf5c753972006-08-04 23:03:53 -0700842 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -0800844 dev = __dev_get_by_index(net, ifm->ifa_index);
Denis V. Lunev7b218572008-01-31 18:47:00 -0800845 err = -ENODEV;
Ian Morris51456b22015-04-03 09:17:26 +0100846 if (!dev)
Thomas Graf5c753972006-08-04 23:03:53 -0700847 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Thomas Graf5c753972006-08-04 23:03:53 -0700849 in_dev = __in_dev_get_rtnl(dev);
Denis V. Lunev7b218572008-01-31 18:47:00 -0800850 err = -ENOBUFS;
Ian Morris51456b22015-04-03 09:17:26 +0100851 if (!in_dev)
Herbert Xu71e27da2007-06-04 23:36:06 -0700852 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Thomas Graf5c753972006-08-04 23:03:53 -0700854 ifa = inet_alloc_ifa();
Ian Morris51456b22015-04-03 09:17:26 +0100855 if (!ifa)
Thomas Graf5c753972006-08-04 23:03:53 -0700856 /*
857 * A potential indev allocation can be left alive, it stays
858 * assigned to its device and is destroy with it.
859 */
Thomas Graf5c753972006-08-04 23:03:53 -0700860 goto errout;
Thomas Graf5c753972006-08-04 23:03:53 -0700861
Pavel Emelyanova4e65d32007-12-07 23:55:43 -0800862 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100863 neigh_parms_data_state_setall(in_dev->arp_parms);
Thomas Graf5c753972006-08-04 23:03:53 -0700864 in_dev_hold(in_dev);
865
Ian Morris51456b22015-04-03 09:17:26 +0100866 if (!tb[IFA_ADDRESS])
Thomas Graf5c753972006-08-04 23:03:53 -0700867 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
868
David S. Millerfd23c3b2011-02-18 12:42:28 -0800869 INIT_HLIST_NODE(&ifa->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
871 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
Jiri Pirkoad6c8132013-12-08 12:16:10 +0100872 ifa->ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) :
873 ifm->ifa_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 ifa->ifa_scope = ifm->ifa_scope;
Thomas Graf5c753972006-08-04 23:03:53 -0700875 ifa->ifa_dev = in_dev;
876
Jiri Benc67b61f62015-03-29 16:59:26 +0200877 ifa->ifa_local = nla_get_in_addr(tb[IFA_LOCAL]);
878 ifa->ifa_address = nla_get_in_addr(tb[IFA_ADDRESS]);
Thomas Graf5c753972006-08-04 23:03:53 -0700879
880 if (tb[IFA_BROADCAST])
Jiri Benc67b61f62015-03-29 16:59:26 +0200881 ifa->ifa_broadcast = nla_get_in_addr(tb[IFA_BROADCAST]);
Thomas Graf5c753972006-08-04 23:03:53 -0700882
Thomas Graf5c753972006-08-04 23:03:53 -0700883 if (tb[IFA_LABEL])
884 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 else
886 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
887
David Ahernaf4d7682018-05-27 08:09:57 -0700888 if (tb[IFA_RT_PRIORITY])
889 ifa->ifa_rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
890
Jiri Pirko5c766d62013-01-24 09:41:41 +0000891 if (tb[IFA_CACHEINFO]) {
892 struct ifa_cacheinfo *ci;
893
894 ci = nla_data(tb[IFA_CACHEINFO]);
895 if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) {
896 err = -EINVAL;
Daniel Borkmann446266b2013-08-02 11:32:43 +0200897 goto errout_free;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000898 }
899 *pvalid_lft = ci->ifa_valid;
900 *pprefered_lft = ci->ifa_prefered;
901 }
902
Thomas Graf5c753972006-08-04 23:03:53 -0700903 return ifa;
904
Daniel Borkmann446266b2013-08-02 11:32:43 +0200905errout_free:
906 inet_free_ifa(ifa);
Thomas Graf5c753972006-08-04 23:03:53 -0700907errout:
908 return ERR_PTR(err);
909}
910
Jiri Pirko5c766d62013-01-24 09:41:41 +0000911static struct in_ifaddr *find_matching_ifa(struct in_ifaddr *ifa)
912{
913 struct in_device *in_dev = ifa->ifa_dev;
Florian Westphalef11db32019-05-31 18:27:04 +0200914 struct in_ifaddr *ifa1;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000915
916 if (!ifa->ifa_local)
917 return NULL;
918
Florian Westphalef11db32019-05-31 18:27:04 +0200919 in_dev_for_each_ifa_rtnl(ifa1, in_dev) {
Jiri Pirko5c766d62013-01-24 09:41:41 +0000920 if (ifa1->ifa_mask == ifa->ifa_mask &&
921 inet_ifa_match(ifa1->ifa_address, ifa) &&
922 ifa1->ifa_local == ifa->ifa_local)
923 return ifa1;
924 }
925 return NULL;
926}
927
David Ahernc21ef3e2017-04-16 09:48:24 -0700928static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
929 struct netlink_ext_ack *extack)
Thomas Graf5c753972006-08-04 23:03:53 -0700930{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900931 struct net *net = sock_net(skb->sk);
Thomas Graf5c753972006-08-04 23:03:53 -0700932 struct in_ifaddr *ifa;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000933 struct in_ifaddr *ifa_existing;
934 __u32 valid_lft = INFINITY_LIFE_TIME;
935 __u32 prefered_lft = INFINITY_LIFE_TIME;
Thomas Graf5c753972006-08-04 23:03:53 -0700936
937 ASSERT_RTNL();
938
David Aherndac9c972018-10-07 20:16:24 -0700939 ifa = rtm_to_ifaddr(net, nlh, &valid_lft, &prefered_lft, extack);
Thomas Graf5c753972006-08-04 23:03:53 -0700940 if (IS_ERR(ifa))
941 return PTR_ERR(ifa);
942
Jiri Pirko5c766d62013-01-24 09:41:41 +0000943 ifa_existing = find_matching_ifa(ifa);
944 if (!ifa_existing) {
945 /* It would be best to check for !NLM_F_CREATE here but
stephen hemminger614d0562014-05-16 20:46:58 -0700946 * userspace already relies on not having to provide this.
Jiri Pirko5c766d62013-01-24 09:41:41 +0000947 */
948 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
Madhu Challa93a714d2015-02-25 09:58:35 -0800949 if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) {
Taras Chornyi690cc862020-04-09 20:25:24 +0300950 int ret = ip_mc_autojoin_config(net, true, ifa);
Madhu Challa93a714d2015-02-25 09:58:35 -0800951
952 if (ret < 0) {
953 inet_free_ifa(ifa);
954 return ret;
955 }
956 }
David Ahernde95e042017-10-18 09:56:54 -0700957 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid,
958 extack);
Jiri Pirko5c766d62013-01-24 09:41:41 +0000959 } else {
David Ahernaf4d7682018-05-27 08:09:57 -0700960 u32 new_metric = ifa->ifa_rt_priority;
961
Jiri Pirko5c766d62013-01-24 09:41:41 +0000962 inet_free_ifa(ifa);
963
964 if (nlh->nlmsg_flags & NLM_F_EXCL ||
965 !(nlh->nlmsg_flags & NLM_F_REPLACE))
966 return -EEXIST;
Jiri Pirko34e2ed32013-04-04 08:33:00 +0000967 ifa = ifa_existing;
David Ahernaf4d7682018-05-27 08:09:57 -0700968
969 if (ifa->ifa_rt_priority != new_metric) {
970 fib_modify_prefix_metric(ifa, new_metric);
971 ifa->ifa_rt_priority = new_metric;
972 }
973
Jiri Pirko34e2ed32013-04-04 08:33:00 +0000974 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
Jiri Pirko05a324b2013-04-04 23:39:38 +0000975 cancel_delayed_work(&check_lifetime_work);
viresh kumar906e0732014-01-22 12:23:32 +0530976 queue_delayed_work(system_power_efficient_wq,
977 &check_lifetime_work, 0);
Jiri Pirko34e2ed32013-04-04 08:33:00 +0000978 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid);
Jiri Pirko5c766d62013-01-24 09:41:41 +0000979 }
980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983/*
984 * Determine a default network mask, based on the IP address.
985 */
986
Eric Dumazet40384992012-08-03 21:06:50 +0000987static int inet_abc_len(__be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 int rc = -1; /* Something else, probably a multicast. */
990
Dave Taht65cab852018-12-11 15:30:34 -0800991 if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900992 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 else {
Al Viro714e85b2006-11-14 20:51:49 -0800994 __u32 haddr = ntohl(addr);
Al Viro714e85b2006-11-14 20:51:49 -0800995 if (IN_CLASSA(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 rc = 8;
Al Viro714e85b2006-11-14 20:51:49 -0800997 else if (IN_CLASSB(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 rc = 16;
Al Viro714e85b2006-11-14 20:51:49 -0800999 else if (IN_CLASSC(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 rc = 24;
Dave Taht65cab852018-12-11 15:30:34 -08001001 else if (IN_CLASSE(haddr))
1002 rc = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001005 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008
Al Viro03aef172017-07-01 07:53:12 -04001009int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 struct sockaddr_in sin_orig;
Al Viro03aef172017-07-01 07:53:12 -04001012 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
Florian Westphal2638eb8b2019-05-31 18:27:09 +02001013 struct in_ifaddr __rcu **ifap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 struct in_ifaddr *ifa = NULL;
1016 struct net_device *dev;
1017 char *colon;
1018 int ret = -EFAULT;
1019 int tryaddrmatch = 0;
1020
Al Viro03aef172017-07-01 07:53:12 -04001021 ifr->ifr_name[IFNAMSIZ - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 /* save original address for comparison */
1024 memcpy(&sin_orig, sin, sizeof(*sin));
1025
Al Viro03aef172017-07-01 07:53:12 -04001026 colon = strchr(ifr->ifr_name, ':');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (colon)
1028 *colon = 0;
1029
Al Viro03aef172017-07-01 07:53:12 -04001030 dev_load(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Stephen Hemminger132adf52007-03-08 20:44:43 -08001032 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 case SIOCGIFADDR: /* Get interface address */
1034 case SIOCGIFBRDADDR: /* Get the broadcast address */
1035 case SIOCGIFDSTADDR: /* Get the destination address */
1036 case SIOCGIFNETMASK: /* Get the netmask for the interface */
1037 /* Note that these ioctls will not sleep,
1038 so that we do not impose a lock.
1039 One day we will be forced to put shlock here (I mean SMP)
1040 */
1041 tryaddrmatch = (sin_orig.sin_family == AF_INET);
1042 memset(sin, 0, sizeof(*sin));
1043 sin->sin_family = AF_INET;
1044 break;
1045
1046 case SIOCSIFFLAGS:
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +00001047 ret = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001048 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 goto out;
1050 break;
1051 case SIOCSIFADDR: /* Set interface address (and family) */
1052 case SIOCSIFBRDADDR: /* Set the broadcast address */
1053 case SIOCSIFDSTADDR: /* Set the destination address */
1054 case SIOCSIFNETMASK: /* Set the netmask for the interface */
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +00001055 ret = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001056 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 goto out;
1058 ret = -EINVAL;
1059 if (sin->sin_family != AF_INET)
1060 goto out;
1061 break;
1062 default:
1063 ret = -EINVAL;
1064 goto out;
1065 }
1066
1067 rtnl_lock();
1068
1069 ret = -ENODEV;
Al Viro03aef172017-07-01 07:53:12 -04001070 dev = __dev_get_by_name(net, ifr->ifr_name);
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001071 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 goto done;
1073
1074 if (colon)
1075 *colon = ':';
1076
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001077 in_dev = __in_dev_get_rtnl(dev);
1078 if (in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (tryaddrmatch) {
1080 /* Matthias Andree */
1081 /* compare label and address (4.4BSD style) */
1082 /* note: we only do this for a limited set of ioctls
1083 and only if the original address family was AF_INET.
1084 This is checked above. */
Florian Westphal2638eb8b2019-05-31 18:27:09 +02001085
1086 for (ifap = &in_dev->ifa_list;
1087 (ifa = rtnl_dereference(*ifap)) != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 ifap = &ifa->ifa_next) {
Al Viro03aef172017-07-01 07:53:12 -04001089 if (!strcmp(ifr->ifr_name, ifa->ifa_label) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 sin_orig.sin_addr.s_addr ==
David S. Miller6c91afe2011-03-09 13:27:16 -08001091 ifa->ifa_local) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 break; /* found */
1093 }
1094 }
1095 }
1096 /* we didn't get a match, maybe the application is
1097 4.3BSD-style and passed in junk so we fall back to
1098 comparing just the label */
1099 if (!ifa) {
Florian Westphal2638eb8b2019-05-31 18:27:09 +02001100 for (ifap = &in_dev->ifa_list;
1101 (ifa = rtnl_dereference(*ifap)) != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 ifap = &ifa->ifa_next)
Al Viro03aef172017-07-01 07:53:12 -04001103 if (!strcmp(ifr->ifr_name, ifa->ifa_label))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 break;
1105 }
1106 }
1107
1108 ret = -EADDRNOTAVAIL;
1109 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
1110 goto done;
1111
Stephen Hemminger132adf52007-03-08 20:44:43 -08001112 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 case SIOCGIFADDR: /* Get interface address */
Tonghao Zhang30e948a2018-01-28 03:38:58 -08001114 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 sin->sin_addr.s_addr = ifa->ifa_local;
Al Viro03aef172017-07-01 07:53:12 -04001116 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 case SIOCGIFBRDADDR: /* Get the broadcast address */
Tonghao Zhang30e948a2018-01-28 03:38:58 -08001119 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 sin->sin_addr.s_addr = ifa->ifa_broadcast;
Al Viro03aef172017-07-01 07:53:12 -04001121 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 case SIOCGIFDSTADDR: /* Get the destination address */
Tonghao Zhang30e948a2018-01-28 03:38:58 -08001124 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 sin->sin_addr.s_addr = ifa->ifa_address;
Al Viro03aef172017-07-01 07:53:12 -04001126 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 case SIOCGIFNETMASK: /* Get the netmask for the interface */
Tonghao Zhang30e948a2018-01-28 03:38:58 -08001129 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 sin->sin_addr.s_addr = ifa->ifa_mask;
Al Viro03aef172017-07-01 07:53:12 -04001131 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 case SIOCSIFFLAGS:
1134 if (colon) {
1135 ret = -EADDRNOTAVAIL;
1136 if (!ifa)
1137 break;
1138 ret = 0;
Al Viro03aef172017-07-01 07:53:12 -04001139 if (!(ifr->ifr_flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 inet_del_ifa(in_dev, ifap, 1);
1141 break;
1142 }
Petr Machata567c5e12018-12-06 17:05:42 +00001143 ret = dev_change_flags(dev, ifr->ifr_flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 break;
1145
1146 case SIOCSIFADDR: /* Set interface address (and family) */
1147 ret = -EINVAL;
1148 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1149 break;
1150
1151 if (!ifa) {
1152 ret = -ENOBUFS;
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001153 ifa = inet_alloc_ifa();
1154 if (!ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
Xi Wangc7e2e1d2013-01-05 11:19:24 +00001156 INIT_HLIST_NODE(&ifa->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (colon)
Al Viro03aef172017-07-01 07:53:12 -04001158 memcpy(ifa->ifa_label, ifr->ifr_name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 else
1160 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1161 } else {
1162 ret = 0;
1163 if (ifa->ifa_local == sin->sin_addr.s_addr)
1164 break;
1165 inet_del_ifa(in_dev, ifap, 0);
1166 ifa->ifa_broadcast = 0;
Bjorn Mork148f9722008-02-26 18:17:53 -08001167 ifa->ifa_scope = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
1169
1170 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
1171
1172 if (!(dev->flags & IFF_POINTOPOINT)) {
1173 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
1174 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
1175 if ((dev->flags & IFF_BROADCAST) &&
1176 ifa->ifa_prefixlen < 31)
1177 ifa->ifa_broadcast = ifa->ifa_address |
1178 ~ifa->ifa_mask;
1179 } else {
1180 ifa->ifa_prefixlen = 32;
1181 ifa->ifa_mask = inet_make_mask(32);
1182 }
Jiri Pirko5c766d62013-01-24 09:41:41 +00001183 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 ret = inet_set_ifa(dev, ifa);
1185 break;
1186
1187 case SIOCSIFBRDADDR: /* Set the broadcast address */
1188 ret = 0;
1189 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
1190 inet_del_ifa(in_dev, ifap, 0);
1191 ifa->ifa_broadcast = sin->sin_addr.s_addr;
1192 inet_insert_ifa(ifa);
1193 }
1194 break;
1195
1196 case SIOCSIFDSTADDR: /* Set the destination address */
1197 ret = 0;
1198 if (ifa->ifa_address == sin->sin_addr.s_addr)
1199 break;
1200 ret = -EINVAL;
1201 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1202 break;
1203 ret = 0;
1204 inet_del_ifa(in_dev, ifap, 0);
1205 ifa->ifa_address = sin->sin_addr.s_addr;
1206 inet_insert_ifa(ifa);
1207 break;
1208
1209 case SIOCSIFNETMASK: /* Set the netmask for the interface */
1210
1211 /*
1212 * The mask we set must be legal.
1213 */
1214 ret = -EINVAL;
1215 if (bad_mask(sin->sin_addr.s_addr, 0))
1216 break;
1217 ret = 0;
1218 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
Al Viroa144ea42006-09-28 18:00:55 -07001219 __be32 old_mask = ifa->ifa_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 inet_del_ifa(in_dev, ifap, 0);
1221 ifa->ifa_mask = sin->sin_addr.s_addr;
1222 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
1223
1224 /* See if current broadcast address matches
1225 * with current netmask, then recalculate
1226 * the broadcast address. Otherwise it's a
1227 * funny address, so don't touch it since
1228 * the user seems to know what (s)he's doing...
1229 */
1230 if ((dev->flags & IFF_BROADCAST) &&
1231 (ifa->ifa_prefixlen < 31) &&
1232 (ifa->ifa_broadcast ==
David Engeldcab5e12005-10-21 22:09:16 -05001233 (ifa->ifa_local|~old_mask))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 ifa->ifa_broadcast = (ifa->ifa_local |
1235 ~sin->sin_addr.s_addr);
1236 }
1237 inet_insert_ifa(ifa);
1238 }
1239 break;
1240 }
1241done:
1242 rtnl_unlock();
1243out:
1244 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Al Viro36fd6332017-06-26 13:19:16 -04001247static int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Herbert Xue5ed6392005-10-03 14:35:55 -07001249 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Florian Westphalef11db32019-05-31 18:27:04 +02001250 const struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 struct ifreq ifr;
1252 int done = 0;
1253
Al Viro36fd6332017-06-26 13:19:16 -04001254 if (WARN_ON(size > sizeof(struct ifreq)))
1255 goto out;
1256
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001257 if (!in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 goto out;
1259
Florian Westphalef11db32019-05-31 18:27:04 +02001260 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (!buf) {
Al Viro36fd6332017-06-26 13:19:16 -04001262 done += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 continue;
1264 }
Al Viro36fd6332017-06-26 13:19:16 -04001265 if (len < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 break;
1267 memset(&ifr, 0, sizeof(struct ifreq));
Dan Carpenter4299c8a2013-07-29 22:15:19 +03001268 strcpy(ifr.ifr_name, ifa->ifa_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
1271 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
1272 ifa->ifa_local;
1273
Al Viro36fd6332017-06-26 13:19:16 -04001274 if (copy_to_user(buf + done, &ifr, size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 done = -EFAULT;
1276 break;
1277 }
Al Viro36fd6332017-06-26 13:19:16 -04001278 len -= size;
1279 done += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281out:
1282 return done;
1283}
1284
Gao Feng8b57fd12017-03-10 12:38:47 +08001285static __be32 in_dev_select_addr(const struct in_device *in_dev,
1286 int scope)
1287{
Florian Westphald519e872019-05-31 18:27:05 +02001288 const struct in_ifaddr *ifa;
1289
1290 in_dev_for_each_ifa_rcu(ifa, in_dev) {
1291 if (ifa->ifa_flags & IFA_F_SECONDARY)
1292 continue;
Gao Feng8b57fd12017-03-10 12:38:47 +08001293 if (ifa->ifa_scope != RT_SCOPE_LINK &&
1294 ifa->ifa_scope <= scope)
1295 return ifa->ifa_local;
Florian Westphald519e872019-05-31 18:27:05 +02001296 }
Gao Feng8b57fd12017-03-10 12:38:47 +08001297
1298 return 0;
1299}
1300
Al Viroa61ced52006-09-26 21:27:54 -07001301__be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Florian Westphald519e872019-05-31 18:27:05 +02001303 const struct in_ifaddr *ifa;
Al Viroa61ced52006-09-26 21:27:54 -07001304 __be32 addr = 0;
Shijie Luod8c444d2019-06-18 15:14:03 +00001305 unsigned char localnet_scope = RT_SCOPE_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 struct in_device *in_dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001307 struct net *net = dev_net(dev);
David Ahern3f2fb9a2016-02-24 11:47:02 -08001308 int master_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07001311 in_dev = __in_dev_get_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (!in_dev)
1313 goto no_in_dev;
1314
Shijie Luod8c444d2019-06-18 15:14:03 +00001315 if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev)))
1316 localnet_scope = RT_SCOPE_LINK;
1317
Florian Westphald519e872019-05-31 18:27:05 +02001318 in_dev_for_each_ifa_rcu(ifa, in_dev) {
1319 if (ifa->ifa_flags & IFA_F_SECONDARY)
1320 continue;
Shijie Luod8c444d2019-06-18 15:14:03 +00001321 if (min(ifa->ifa_scope, localnet_scope) > scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 continue;
1323 if (!dst || inet_ifa_match(dst, ifa)) {
1324 addr = ifa->ifa_local;
1325 break;
1326 }
1327 if (!addr)
1328 addr = ifa->ifa_local;
Florian Westphald519e872019-05-31 18:27:05 +02001329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 if (addr)
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001332 goto out_unlock;
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001333no_in_dev:
David Ahern3f2fb9a2016-02-24 11:47:02 -08001334 master_idx = l3mdev_master_ifindex_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
David Lamparter17b693c2016-02-24 11:47:03 -08001336 /* For VRFs, the VRF device takes the place of the loopback device,
1337 * with addresses on it being preferred. Note in such cases the
1338 * loopback device will be among the devices that fail the master_idx
1339 * equality check in the loop below.
1340 */
1341 if (master_idx &&
1342 (dev = dev_get_by_index_rcu(net, master_idx)) &&
1343 (in_dev = __in_dev_get_rcu(dev))) {
Gao Feng8b57fd12017-03-10 12:38:47 +08001344 addr = in_dev_select_addr(in_dev, scope);
1345 if (addr)
1346 goto out_unlock;
David Lamparter17b693c2016-02-24 11:47:03 -08001347 }
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 /* Not loopback addresses on loopback should be preferred
Stephen Hemmingerca9f1fd2015-02-14 13:47:54 -05001350 in this case. It is important that lo is the first interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 in dev_base list.
1352 */
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001353 for_each_netdev_rcu(net, dev) {
David Ahern3f2fb9a2016-02-24 11:47:02 -08001354 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1355 continue;
1356
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001357 in_dev = __in_dev_get_rcu(dev);
1358 if (!in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 continue;
1360
Gao Feng8b57fd12017-03-10 12:38:47 +08001361 addr = in_dev_select_addr(in_dev, scope);
1362 if (addr)
1363 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001365out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return addr;
1368}
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001369EXPORT_SYMBOL(inet_select_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Al Viro60cad5d2006-09-26 22:17:09 -07001371static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
1372 __be32 local, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Shijie Luo650638a2019-06-18 15:14:04 +00001374 unsigned char localnet_scope = RT_SCOPE_HOST;
Florian Westphalef11db32019-05-31 18:27:04 +02001375 const struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07001376 __be32 addr = 0;
Florian Westphalef11db32019-05-31 18:27:04 +02001377 int same = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Shijie Luo650638a2019-06-18 15:14:04 +00001379 if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev)))
1380 localnet_scope = RT_SCOPE_LINK;
1381
Florian Westphalef11db32019-05-31 18:27:04 +02001382 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Shijie Luo650638a2019-06-18 15:14:04 +00001383 unsigned char min_scope = min(ifa->ifa_scope, localnet_scope);
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (!addr &&
1386 (local == ifa->ifa_local || !local) &&
Shijie Luo650638a2019-06-18 15:14:04 +00001387 min_scope <= scope) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 addr = ifa->ifa_local;
1389 if (same)
1390 break;
1391 }
1392 if (!same) {
1393 same = (!local || inet_ifa_match(local, ifa)) &&
1394 (!dst || inet_ifa_match(dst, ifa));
1395 if (same && addr) {
1396 if (local || !dst)
1397 break;
1398 /* Is the selected addr into dst subnet? */
1399 if (inet_ifa_match(addr, ifa))
1400 break;
1401 /* No, then can we use new local src? */
Shijie Luo650638a2019-06-18 15:14:04 +00001402 if (min_scope <= scope) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 addr = ifa->ifa_local;
1404 break;
1405 }
1406 /* search for large dst subnet for addr */
1407 same = 0;
1408 }
1409 }
Florian Westphalef11db32019-05-31 18:27:04 +02001410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001412 return same ? addr : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415/*
1416 * Confirm that local IP address exists using wildcards:
Nicolas Dichtelb601fa192013-12-10 15:02:40 +01001417 * - net: netns to check, cannot be NULL
1418 * - in_dev: only on this interface, NULL=any interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 * - dst: only in the same subnet as dst, 0=any dst
1420 * - local: address, 0=autoselect the local address
1421 * - scope: maximum allowed scope value for the local address
1422 */
Nicolas Dichtelb601fa192013-12-10 15:02:40 +01001423__be32 inet_confirm_addr(struct net *net, struct in_device *in_dev,
Denis V. Lunev9bd85e32008-01-14 23:05:55 -08001424 __be32 dst, __be32 local, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Al Viro60cad5d2006-09-26 22:17:09 -07001426 __be32 addr = 0;
Denis V. Lunev9bd85e32008-01-14 23:05:55 -08001427 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Ian Morris00db4122015-04-03 09:17:27 +01001429 if (in_dev)
Denis V. Lunev9bd85e32008-01-14 23:05:55 -08001430 return confirm_addr_indev(in_dev, dst, local, scope);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 rcu_read_lock();
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001433 for_each_netdev_rcu(net, dev) {
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001434 in_dev = __in_dev_get_rcu(dev);
1435 if (in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 addr = confirm_addr_indev(in_dev, dst, local, scope);
1437 if (addr)
1438 break;
1439 }
1440 }
1441 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 return addr;
1444}
Andy Gospodarekeaddcd72012-03-22 16:14:29 +00001445EXPORT_SYMBOL(inet_confirm_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447/*
1448 * Device notifier
1449 */
1450
1451int register_inetaddr_notifier(struct notifier_block *nb)
1452{
Alan Sterne041c682006-03-27 01:16:30 -08001453 return blocking_notifier_chain_register(&inetaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001455EXPORT_SYMBOL(register_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457int unregister_inetaddr_notifier(struct notifier_block *nb)
1458{
Alan Sterne041c682006-03-27 01:16:30 -08001459 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001461EXPORT_SYMBOL(unregister_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Krister Johansen3ad7d242017-06-08 13:12:14 -07001463int register_inetaddr_validator_notifier(struct notifier_block *nb)
1464{
1465 return blocking_notifier_chain_register(&inetaddr_validator_chain, nb);
1466}
1467EXPORT_SYMBOL(register_inetaddr_validator_notifier);
1468
1469int unregister_inetaddr_validator_notifier(struct notifier_block *nb)
1470{
1471 return blocking_notifier_chain_unregister(&inetaddr_validator_chain,
1472 nb);
1473}
1474EXPORT_SYMBOL(unregister_inetaddr_validator_notifier);
1475
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001476/* Rename ifa_labels for a device name change. Make some effort to preserve
1477 * existing alias numbering and to create unique labels if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478*/
1479static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 struct in_ifaddr *ifa;
1482 int named = 0;
1483
Florian Westphalef11db32019-05-31 18:27:04 +02001484 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001485 char old[IFNAMSIZ], *dot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 memcpy(old, ifa->ifa_label, IFNAMSIZ);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001488 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (named++ == 0)
Thomas Graf573bf472008-06-10 15:40:04 -07001490 goto skip;
Mark McLoughlin44344b22008-01-04 00:56:25 -08001491 dot = strchr(old, ':');
Ian Morris51456b22015-04-03 09:17:26 +01001492 if (!dot) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001493 sprintf(old, ":%d", named);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 dot = old;
1495 }
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001496 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001497 strcat(ifa->ifa_label, dot);
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001498 else
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001499 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
Thomas Graf573bf472008-06-10 15:40:04 -07001500skip:
1501 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001502 }
1503}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Ian Campbelld11327ad2011-02-11 07:44:16 +00001505static void inetdev_send_gratuitous_arp(struct net_device *dev,
1506 struct in_device *in_dev)
1507
1508{
Florian Westphalef11db32019-05-31 18:27:04 +02001509 const struct in_ifaddr *ifa;
Ian Campbelld11327ad2011-02-11 07:44:16 +00001510
Florian Westphalef11db32019-05-31 18:27:04 +02001511 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Zoltan Kissb76d0782011-07-24 13:09:30 +00001512 arp_send(ARPOP_REQUEST, ETH_P_ARP,
1513 ifa->ifa_local, dev,
1514 ifa->ifa_local, NULL,
1515 dev->dev_addr, NULL);
1516 }
Ian Campbelld11327ad2011-02-11 07:44:16 +00001517}
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519/* Called only under RTNL semaphore */
1520
1521static int inetdev_event(struct notifier_block *this, unsigned long event,
1522 void *ptr)
1523{
Jiri Pirko351638e2013-05-28 01:30:21 +00001524 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric Dumazet748e2d92012-08-22 21:50:59 +00001525 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 ASSERT_RTNL();
1528
1529 if (!in_dev) {
Herbert Xu8030f542007-02-22 01:53:47 +09001530 if (event == NETDEV_REGISTER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 in_dev = inetdev_init(dev);
WANG Cong20e61da2014-07-25 15:25:08 -07001532 if (IS_ERR(in_dev))
1533 return notifier_from_errno(PTR_ERR(in_dev));
Eric W. Biederman0cc217e2007-09-26 22:10:06 -07001534 if (dev->flags & IFF_LOOPBACK) {
Herbert Xu42f811b2007-06-04 23:34:44 -07001535 IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1536 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
Herbert Xu8030f542007-02-22 01:53:47 +09001537 }
Breno Leitao06770842008-09-02 17:28:58 -07001538 } else if (event == NETDEV_CHANGEMTU) {
1539 /* Re-enabling IP */
1540 if (inetdev_valid_mtu(dev->mtu))
1541 in_dev = inetdev_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543 goto out;
1544 }
1545
1546 switch (event) {
1547 case NETDEV_REGISTER:
Joe Perches91df42b2012-05-15 14:11:54 +00001548 pr_debug("%s: bug\n", __func__);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001549 RCU_INIT_POINTER(dev->ip_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 break;
1551 case NETDEV_UP:
Breno Leitao06770842008-09-02 17:28:58 -07001552 if (!inetdev_valid_mtu(dev->mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 break;
Eric W. Biederman0cc217e2007-09-26 22:10:06 -07001554 if (dev->flags & IFF_LOOPBACK) {
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001555 struct in_ifaddr *ifa = inet_alloc_ifa();
1556
1557 if (ifa) {
David S. Millerfd23c3b2011-02-18 12:42:28 -08001558 INIT_HLIST_NODE(&ifa->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 ifa->ifa_local =
1560 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1561 ifa->ifa_prefixlen = 8;
1562 ifa->ifa_mask = inet_make_mask(8);
1563 in_dev_hold(in_dev);
1564 ifa->ifa_dev = in_dev;
1565 ifa->ifa_scope = RT_SCOPE_HOST;
1566 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
Jiri Pirko5c766d62013-01-24 09:41:41 +00001567 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME,
1568 INFINITY_LIFE_TIME);
Jiri Pirkodfd15822014-01-07 15:55:45 +01001569 ipv4_devconf_setall(in_dev);
1570 neigh_parms_data_state_setall(in_dev->arp_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 inet_insert_ifa(ifa);
1572 }
1573 }
1574 ip_mc_up(in_dev);
Joe Perchesa8eceea2020-03-12 15:50:22 -07001575 fallthrough;
Stephen Hemmingereefef1c2009-02-01 01:04:33 -08001576 case NETDEV_CHANGEADDR:
Ian Campbelld11327ad2011-02-11 07:44:16 +00001577 if (!IN_DEV_ARP_NOTIFY(in_dev))
1578 break;
Joe Perchesa8eceea2020-03-12 15:50:22 -07001579 fallthrough;
Ian Campbelld11327ad2011-02-11 07:44:16 +00001580 case NETDEV_NOTIFY_PEERS:
Stephen Hemmingera21090c2009-10-07 03:18:17 -07001581 /* Send gratuitous ARP to notify of link change */
Ian Campbelld11327ad2011-02-11 07:44:16 +00001582 inetdev_send_gratuitous_arp(dev, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 break;
1584 case NETDEV_DOWN:
1585 ip_mc_down(in_dev);
1586 break;
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001587 case NETDEV_PRE_TYPE_CHANGE:
Moni Shoua75c78502009-09-15 02:37:40 -07001588 ip_mc_unmap(in_dev);
1589 break;
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001590 case NETDEV_POST_TYPE_CHANGE:
Moni Shoua75c78502009-09-15 02:37:40 -07001591 ip_mc_remap(in_dev);
1592 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 case NETDEV_CHANGEMTU:
Breno Leitao06770842008-09-02 17:28:58 -07001594 if (inetdev_valid_mtu(dev->mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 break;
Breno Leitao06770842008-09-02 17:28:58 -07001596 /* disable IP when MTU is not enough */
Joe Perchesa8eceea2020-03-12 15:50:22 -07001597 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 case NETDEV_UNREGISTER:
1599 inetdev_destroy(in_dev);
1600 break;
1601 case NETDEV_CHANGENAME:
1602 /* Do not notify about label change, this event is
1603 * not interesting to applications using netlink.
1604 */
1605 inetdev_changename(dev, in_dev);
1606
Pavel Emelyanov51602b22007-12-11 02:17:40 -08001607 devinet_sysctl_unregister(in_dev);
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11001608 devinet_sysctl_register(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 break;
1610 }
1611out:
1612 return NOTIFY_DONE;
1613}
1614
1615static struct notifier_block ip_netdev_notifier = {
Jianjun Kong539afed2008-11-03 02:48:48 -08001616 .notifier_call = inetdev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617};
1618
Eric Dumazet40384992012-08-03 21:06:50 +00001619static size_t inet_nlmsg_size(void)
Thomas Graf339bf982006-11-10 14:10:15 -08001620{
1621 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1622 + nla_total_size(4) /* IFA_ADDRESS */
1623 + nla_total_size(4) /* IFA_LOCAL */
1624 + nla_total_size(4) /* IFA_BROADCAST */
Jiri Pirkoad6c8132013-12-08 12:16:10 +01001625 + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
Geert Uytterhoeven63b5f152014-02-05 08:38:25 +01001626 + nla_total_size(4) /* IFA_FLAGS */
David Ahernaf4d7682018-05-27 08:09:57 -07001627 + nla_total_size(4) /* IFA_RT_PRIORITY */
Geert Uytterhoeven63b5f152014-02-05 08:38:25 +01001628 + nla_total_size(sizeof(struct ifa_cacheinfo)); /* IFA_CACHEINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08001629}
1630
Jiri Pirko5c766d62013-01-24 09:41:41 +00001631static inline u32 cstamp_delta(unsigned long cstamp)
1632{
1633 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
1634}
1635
1636static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
1637 unsigned long tstamp, u32 preferred, u32 valid)
1638{
1639 struct ifa_cacheinfo ci;
1640
1641 ci.cstamp = cstamp_delta(cstamp);
1642 ci.tstamp = cstamp_delta(tstamp);
1643 ci.ifa_prefered = preferred;
1644 ci.ifa_valid = valid;
1645
1646 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
1647}
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
Christian Brauner978a46f2018-09-04 21:53:54 +02001650 struct inet_fill_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 struct ifaddrmsg *ifm;
1653 struct nlmsghdr *nlh;
Jiri Pirko5c766d62013-01-24 09:41:41 +00001654 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Christian Brauner978a46f2018-09-04 21:53:54 +02001656 nlh = nlmsg_put(skb, args->portid, args->seq, args->event, sizeof(*ifm),
1657 args->flags);
Ian Morris51456b22015-04-03 09:17:26 +01001658 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08001659 return -EMSGSIZE;
Thomas Graf47f68512006-08-04 23:04:36 -07001660
1661 ifm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 ifm->ifa_family = AF_INET;
1663 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
Jiri Pirko5c766d62013-01-24 09:41:41 +00001664 ifm->ifa_flags = ifa->ifa_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 ifm->ifa_scope = ifa->ifa_scope;
1666 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Christian Brauner978a46f2018-09-04 21:53:54 +02001668 if (args->netnsid >= 0 &&
1669 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
Christian Braunerd3807142018-09-04 21:53:49 +02001670 goto nla_put_failure;
1671
Jiri Pirko5c766d62013-01-24 09:41:41 +00001672 if (!(ifm->ifa_flags & IFA_F_PERMANENT)) {
1673 preferred = ifa->ifa_preferred_lft;
1674 valid = ifa->ifa_valid_lft;
1675 if (preferred != INFINITY_LIFE_TIME) {
1676 long tval = (jiffies - ifa->ifa_tstamp) / HZ;
1677
1678 if (preferred > tval)
1679 preferred -= tval;
1680 else
1681 preferred = 0;
1682 if (valid != INFINITY_LIFE_TIME) {
1683 if (valid > tval)
1684 valid -= tval;
1685 else
1686 valid = 0;
1687 }
1688 }
1689 } else {
1690 preferred = INFINITY_LIFE_TIME;
1691 valid = INFINITY_LIFE_TIME;
1692 }
David S. Millerf3756b72012-04-01 20:39:02 -04001693 if ((ifa->ifa_address &&
Jiri Benc930345e2015-03-29 16:59:25 +02001694 nla_put_in_addr(skb, IFA_ADDRESS, ifa->ifa_address)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001695 (ifa->ifa_local &&
Jiri Benc930345e2015-03-29 16:59:25 +02001696 nla_put_in_addr(skb, IFA_LOCAL, ifa->ifa_local)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001697 (ifa->ifa_broadcast &&
Jiri Benc930345e2015-03-29 16:59:25 +02001698 nla_put_in_addr(skb, IFA_BROADCAST, ifa->ifa_broadcast)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001699 (ifa->ifa_label[0] &&
Jiri Pirko5c766d62013-01-24 09:41:41 +00001700 nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) ||
Jiri Pirkoad6c8132013-12-08 12:16:10 +01001701 nla_put_u32(skb, IFA_FLAGS, ifa->ifa_flags) ||
David Ahernaf4d7682018-05-27 08:09:57 -07001702 (ifa->ifa_rt_priority &&
1703 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->ifa_rt_priority)) ||
Jiri Pirko5c766d62013-01-24 09:41:41 +00001704 put_cacheinfo(skb, ifa->ifa_cstamp, ifa->ifa_tstamp,
1705 preferred, valid))
David S. Millerf3756b72012-04-01 20:39:02 -04001706 goto nla_put_failure;
Thomas Graf47f68512006-08-04 23:04:36 -07001707
Johannes Berg053c0952015-01-16 22:09:00 +01001708 nlmsg_end(skb, nlh);
1709 return 0;
Thomas Graf47f68512006-08-04 23:04:36 -07001710
1711nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001712 nlmsg_cancel(skb, nlh);
1713 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
David Ahernc33078e2018-10-07 20:16:28 -07001716static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
1717 struct inet_fill_args *fillargs,
1718 struct net **tgt_net, struct sock *sk,
David Ahern5fcd2662018-10-19 12:45:29 -07001719 struct netlink_callback *cb)
David Ahernc33078e2018-10-07 20:16:28 -07001720{
David Ahern5fcd2662018-10-19 12:45:29 -07001721 struct netlink_ext_ack *extack = cb->extack;
David Ahernc33078e2018-10-07 20:16:28 -07001722 struct nlattr *tb[IFA_MAX+1];
1723 struct ifaddrmsg *ifm;
1724 int err, i;
1725
1726 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1727 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for address dump request");
1728 return -EINVAL;
1729 }
1730
1731 ifm = nlmsg_data(nlh);
1732 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
1733 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for address dump request");
1734 return -EINVAL;
1735 }
David Ahern5fcd2662018-10-19 12:45:29 -07001736
1737 fillargs->ifindex = ifm->ifa_index;
1738 if (fillargs->ifindex) {
1739 cb->answer_flags |= NLM_F_DUMP_FILTERED;
1740 fillargs->flags |= NLM_F_DUMP_FILTERED;
David Ahernc33078e2018-10-07 20:16:28 -07001741 }
1742
Johannes Berg8cb08172019-04-26 14:07:28 +02001743 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
1744 ifa_ipv4_policy, extack);
David Ahernc33078e2018-10-07 20:16:28 -07001745 if (err < 0)
1746 return err;
1747
1748 for (i = 0; i <= IFA_MAX; ++i) {
1749 if (!tb[i])
1750 continue;
1751
1752 if (i == IFA_TARGET_NETNSID) {
1753 struct net *net;
1754
1755 fillargs->netnsid = nla_get_s32(tb[i]);
1756
1757 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
1758 if (IS_ERR(net)) {
Bjørn Morkbf4cc402018-10-25 21:18:25 +02001759 fillargs->netnsid = -1;
David Ahernc33078e2018-10-07 20:16:28 -07001760 NL_SET_ERR_MSG(extack, "ipv4: Invalid target network namespace id");
1761 return PTR_ERR(net);
1762 }
1763 *tgt_net = net;
1764 } else {
1765 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in dump request");
1766 return -EINVAL;
1767 }
1768 }
1769
1770 return 0;
1771}
1772
David Ahern1c98eca2018-10-19 12:45:27 -07001773static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb,
1774 struct netlink_callback *cb, int s_ip_idx,
1775 struct inet_fill_args *fillargs)
1776{
1777 struct in_ifaddr *ifa;
1778 int ip_idx = 0;
1779 int err;
1780
Florian Westphald3e6e282019-06-03 22:41:44 +02001781 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Florian Westphalef11db32019-05-31 18:27:04 +02001782 if (ip_idx < s_ip_idx) {
1783 ip_idx++;
David Ahern1c98eca2018-10-19 12:45:27 -07001784 continue;
Florian Westphalef11db32019-05-31 18:27:04 +02001785 }
David Ahern1c98eca2018-10-19 12:45:27 -07001786 err = inet_fill_ifaddr(skb, ifa, fillargs);
1787 if (err < 0)
1788 goto done;
1789
1790 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Florian Westphalef11db32019-05-31 18:27:04 +02001791 ip_idx++;
David Ahern1c98eca2018-10-19 12:45:27 -07001792 }
1793 err = 0;
1794
1795done:
1796 cb->args[2] = ip_idx;
1797
1798 return err;
1799}
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1802{
David Ahernc33078e2018-10-07 20:16:28 -07001803 const struct nlmsghdr *nlh = cb->nlh;
Christian Brauner978a46f2018-09-04 21:53:54 +02001804 struct inet_fill_args fillargs = {
1805 .portid = NETLINK_CB(cb->skb).portid,
David Ahernc33078e2018-10-07 20:16:28 -07001806 .seq = nlh->nlmsg_seq,
Christian Brauner978a46f2018-09-04 21:53:54 +02001807 .event = RTM_NEWADDR,
1808 .flags = NLM_F_MULTI,
1809 .netnsid = -1,
1810 };
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001811 struct net *net = sock_net(skb->sk);
Christian Braunerd3807142018-09-04 21:53:49 +02001812 struct net *tgt_net = net;
Eric Dumazeteec4df92009-11-12 07:44:25 +00001813 int h, s_h;
1814 int idx, s_idx;
David Ahern1c98eca2018-10-19 12:45:27 -07001815 int s_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 struct net_device *dev;
1817 struct in_device *in_dev;
Eric Dumazeteec4df92009-11-12 07:44:25 +00001818 struct hlist_head *head;
David Ahernd7e38612018-10-24 12:58:59 -07001819 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Eric Dumazeteec4df92009-11-12 07:44:25 +00001821 s_h = cb->args[0];
1822 s_idx = idx = cb->args[1];
David Ahern1c98eca2018-10-19 12:45:27 -07001823 s_ip_idx = cb->args[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
David Ahernc33078e2018-10-07 20:16:28 -07001825 if (cb->strict_check) {
David Ahernc33078e2018-10-07 20:16:28 -07001826 err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
David Ahern5fcd2662018-10-19 12:45:29 -07001827 skb->sk, cb);
David Ahernc33078e2018-10-07 20:16:28 -07001828 if (err < 0)
David Ahernd7e38612018-10-24 12:58:59 -07001829 goto put_tgt_net;
David Ahern5fcd2662018-10-19 12:45:29 -07001830
David Ahernd7e38612018-10-24 12:58:59 -07001831 err = 0;
David Ahern5fcd2662018-10-19 12:45:29 -07001832 if (fillargs.ifindex) {
1833 dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
David Ahernd7e38612018-10-24 12:58:59 -07001834 if (!dev) {
1835 err = -ENODEV;
1836 goto put_tgt_net;
1837 }
David Ahern5fcd2662018-10-19 12:45:29 -07001838
1839 in_dev = __in_dev_get_rtnl(dev);
1840 if (in_dev) {
1841 err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx,
1842 &fillargs);
1843 }
1844 goto put_tgt_net;
1845 }
Christian Braunerd3807142018-09-04 21:53:49 +02001846 }
1847
Eric Dumazeteec4df92009-11-12 07:44:25 +00001848 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1849 idx = 0;
Christian Braunerd3807142018-09-04 21:53:49 +02001850 head = &tgt_net->dev_index_head[h];
Eric Dumazeteec4df92009-11-12 07:44:25 +00001851 rcu_read_lock();
Christian Braunerd3807142018-09-04 21:53:49 +02001852 cb->seq = atomic_read(&tgt_net->ipv4.dev_addr_genid) ^
1853 tgt_net->dev_base_seq;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001854 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazeteec4df92009-11-12 07:44:25 +00001855 if (idx < s_idx)
1856 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07001857 if (h > s_h || idx > s_idx)
Eric Dumazeteec4df92009-11-12 07:44:25 +00001858 s_ip_idx = 0;
1859 in_dev = __in_dev_get_rcu(dev);
1860 if (!in_dev)
1861 goto cont;
1862
David Ahern1c98eca2018-10-19 12:45:27 -07001863 err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx,
1864 &fillargs);
1865 if (err < 0) {
1866 rcu_read_unlock();
1867 goto done;
Eric Dumazeteec4df92009-11-12 07:44:25 +00001868 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001869cont:
Eric Dumazeteec4df92009-11-12 07:44:25 +00001870 idx++;
1871 }
1872 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874
1875done:
Eric Dumazeteec4df92009-11-12 07:44:25 +00001876 cb->args[0] = h;
1877 cb->args[1] = idx;
David Ahern5fcd2662018-10-19 12:45:29 -07001878put_tgt_net:
Christian Brauner978a46f2018-09-04 21:53:54 +02001879 if (fillargs.netnsid >= 0)
Christian Braunerd3807142018-09-04 21:53:49 +02001880 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Arthur Gautier7c1e8a32018-12-31 02:10:58 +00001882 return skb->len ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
Jianjun Kong539afed2008-11-03 02:48:48 -08001885static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001886 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Christian Brauner978a46f2018-09-04 21:53:54 +02001888 struct inet_fill_args fillargs = {
1889 .portid = portid,
1890 .seq = nlh ? nlh->nlmsg_seq : 0,
1891 .event = event,
1892 .flags = 0,
1893 .netnsid = -1,
1894 };
Thomas Graf47f68512006-08-04 23:04:36 -07001895 struct sk_buff *skb;
Thomas Grafd6062cb2006-08-15 00:33:59 -07001896 int err = -ENOBUFS;
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -08001897 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001899 net = dev_net(ifa->ifa_dev->dev);
Thomas Graf339bf982006-11-10 14:10:15 -08001900 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001901 if (!skb)
Thomas Grafd6062cb2006-08-15 00:33:59 -07001902 goto errout;
1903
Christian Brauner978a46f2018-09-04 21:53:54 +02001904 err = inet_fill_ifaddr(skb, ifa, &fillargs);
Patrick McHardy26932562007-01-31 23:16:40 -08001905 if (err < 0) {
1906 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1907 WARN_ON(err == -EMSGSIZE);
1908 kfree_skb(skb);
1909 goto errout;
1910 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001911 rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001912 return;
Thomas Grafd6062cb2006-08-15 00:33:59 -07001913errout:
1914 if (err < 0)
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -08001915 rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001918static size_t inet_get_link_af_size(const struct net_device *dev,
1919 u32 ext_filter_mask)
Thomas Graf9f0f7272010-11-16 04:32:48 +00001920{
Eric Dumazet1fc19af2011-05-09 20:55:03 -07001921 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
Thomas Graf9f0f7272010-11-16 04:32:48 +00001922
1923 if (!in_dev)
1924 return 0;
1925
1926 return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
1927}
1928
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001929static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
1930 u32 ext_filter_mask)
Thomas Graf9f0f7272010-11-16 04:32:48 +00001931{
Eric Dumazet1fc19af2011-05-09 20:55:03 -07001932 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
Thomas Graf9f0f7272010-11-16 04:32:48 +00001933 struct nlattr *nla;
1934 int i;
1935
1936 if (!in_dev)
1937 return -ENODATA;
1938
1939 nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
Ian Morris51456b22015-04-03 09:17:26 +01001940 if (!nla)
Thomas Graf9f0f7272010-11-16 04:32:48 +00001941 return -EMSGSIZE;
1942
1943 for (i = 0; i < IPV4_DEVCONF_MAX; i++)
1944 ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
1945
1946 return 0;
1947}
1948
1949static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
1950 [IFLA_INET_CONF] = { .type = NLA_NESTED },
1951};
1952
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001953static int inet_validate_link_af(const struct net_device *dev,
1954 const struct nlattr *nla)
Thomas Graf9f0f7272010-11-16 04:32:48 +00001955{
Thomas Graf9f0f7272010-11-16 04:32:48 +00001956 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1957 int err, rem;
1958
Florian Westphal5fa85a02017-10-16 15:44:36 +02001959 if (dev && !__in_dev_get_rcu(dev))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001960 return -EAFNOSUPPORT;
Thomas Graf9f0f7272010-11-16 04:32:48 +00001961
Johannes Berg8cb08172019-04-26 14:07:28 +02001962 err = nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla,
1963 inet_af_policy, NULL);
Thomas Graf9f0f7272010-11-16 04:32:48 +00001964 if (err < 0)
1965 return err;
1966
1967 if (tb[IFLA_INET_CONF]) {
1968 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
1969 int cfgid = nla_type(a);
1970
1971 if (nla_len(a) < 4)
1972 return -EINVAL;
1973
1974 if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
1975 return -EINVAL;
1976 }
1977 }
1978
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001979 return 0;
1980}
1981
1982static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla)
1983{
Florian Westphal5fa85a02017-10-16 15:44:36 +02001984 struct in_device *in_dev = __in_dev_get_rcu(dev);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001985 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1986 int rem;
1987
1988 if (!in_dev)
1989 return -EAFNOSUPPORT;
1990
Johannes Berg8cb08172019-04-26 14:07:28 +02001991 if (nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla, NULL, NULL) < 0)
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001992 BUG();
1993
Thomas Graf9f0f7272010-11-16 04:32:48 +00001994 if (tb[IFLA_INET_CONF]) {
1995 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
1996 ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
1997 }
1998
1999 return 0;
2000}
2001
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002002static int inet_netconf_msgsize_devconf(int type)
2003{
2004 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
2005 + nla_total_size(4); /* NETCONFA_IFINDEX */
Zhang Shengju136ba622016-03-10 08:55:50 +00002006 bool all = false;
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002007
Zhang Shengju136ba622016-03-10 08:55:50 +00002008 if (type == NETCONFA_ALL)
2009 all = true;
2010
2011 if (all || type == NETCONFA_FORWARDING)
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002012 size += nla_total_size(4);
Zhang Shengju136ba622016-03-10 08:55:50 +00002013 if (all || type == NETCONFA_RP_FILTER)
Nicolas Dichtelcc535df2012-10-29 04:53:27 +00002014 size += nla_total_size(4);
Zhang Shengju136ba622016-03-10 08:55:50 +00002015 if (all || type == NETCONFA_MC_FORWARDING)
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00002016 size += nla_total_size(4);
Xin Long5cbf7772018-07-27 16:37:28 +08002017 if (all || type == NETCONFA_BC_FORWARDING)
2018 size += nla_total_size(4);
Zhang Shengju136ba622016-03-10 08:55:50 +00002019 if (all || type == NETCONFA_PROXY_NEIGH)
stephen hemmingerf085ff12013-12-12 13:06:50 -08002020 size += nla_total_size(4);
Zhang Shengju136ba622016-03-10 08:55:50 +00002021 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
Andy Gospodarek974d7af2015-07-07 13:56:57 -04002022 size += nla_total_size(4);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002023
2024 return size;
2025}
2026
2027static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
2028 struct ipv4_devconf *devconf, u32 portid,
2029 u32 seq, int event, unsigned int flags,
2030 int type)
2031{
2032 struct nlmsghdr *nlh;
2033 struct netconfmsg *ncm;
Zhang Shengju136ba622016-03-10 08:55:50 +00002034 bool all = false;
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002035
2036 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
2037 flags);
Ian Morris51456b22015-04-03 09:17:26 +01002038 if (!nlh)
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002039 return -EMSGSIZE;
2040
Zhang Shengju136ba622016-03-10 08:55:50 +00002041 if (type == NETCONFA_ALL)
2042 all = true;
2043
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002044 ncm = nlmsg_data(nlh);
2045 ncm->ncm_family = AF_INET;
2046
2047 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
2048 goto nla_put_failure;
2049
David Ahernb5c96412017-03-28 14:28:03 -07002050 if (!devconf)
2051 goto out;
2052
Zhang Shengju136ba622016-03-10 08:55:50 +00002053 if ((all || type == NETCONFA_FORWARDING) &&
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002054 nla_put_s32(skb, NETCONFA_FORWARDING,
2055 IPV4_DEVCONF(*devconf, FORWARDING)) < 0)
2056 goto nla_put_failure;
Zhang Shengju136ba622016-03-10 08:55:50 +00002057 if ((all || type == NETCONFA_RP_FILTER) &&
Nicolas Dichtelcc535df2012-10-29 04:53:27 +00002058 nla_put_s32(skb, NETCONFA_RP_FILTER,
2059 IPV4_DEVCONF(*devconf, RP_FILTER)) < 0)
2060 goto nla_put_failure;
Zhang Shengju136ba622016-03-10 08:55:50 +00002061 if ((all || type == NETCONFA_MC_FORWARDING) &&
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00002062 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
2063 IPV4_DEVCONF(*devconf, MC_FORWARDING)) < 0)
2064 goto nla_put_failure;
Xin Long5cbf7772018-07-27 16:37:28 +08002065 if ((all || type == NETCONFA_BC_FORWARDING) &&
2066 nla_put_s32(skb, NETCONFA_BC_FORWARDING,
2067 IPV4_DEVCONF(*devconf, BC_FORWARDING)) < 0)
2068 goto nla_put_failure;
Zhang Shengju136ba622016-03-10 08:55:50 +00002069 if ((all || type == NETCONFA_PROXY_NEIGH) &&
stephen hemminger09aea5d2013-12-17 22:35:52 -08002070 nla_put_s32(skb, NETCONFA_PROXY_NEIGH,
stephen hemmingerf085ff12013-12-12 13:06:50 -08002071 IPV4_DEVCONF(*devconf, PROXY_ARP)) < 0)
2072 goto nla_put_failure;
Zhang Shengju136ba622016-03-10 08:55:50 +00002073 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
Andy Gospodarek974d7af2015-07-07 13:56:57 -04002074 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
2075 IPV4_DEVCONF(*devconf, IGNORE_ROUTES_WITH_LINKDOWN)) < 0)
2076 goto nla_put_failure;
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002077
David Ahernb5c96412017-03-28 14:28:03 -07002078out:
Johannes Berg053c0952015-01-16 22:09:00 +01002079 nlmsg_end(skb, nlh);
2080 return 0;
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002081
2082nla_put_failure:
2083 nlmsg_cancel(skb, nlh);
2084 return -EMSGSIZE;
2085}
2086
David Ahern3b022862017-03-28 14:28:02 -07002087void inet_netconf_notify_devconf(struct net *net, int event, int type,
2088 int ifindex, struct ipv4_devconf *devconf)
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002089{
2090 struct sk_buff *skb;
2091 int err = -ENOBUFS;
2092
Eric Dumazetfa178062016-07-08 05:18:24 +02002093 skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002094 if (!skb)
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002095 goto errout;
2096
2097 err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
David Ahern3b022862017-03-28 14:28:02 -07002098 event, 0, type);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002099 if (err < 0) {
2100 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2101 WARN_ON(err == -EMSGSIZE);
2102 kfree_skb(skb);
2103 goto errout;
2104 }
Eric Dumazetfa178062016-07-08 05:18:24 +02002105 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_KERNEL);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002106 return;
2107errout:
2108 if (err < 0)
2109 rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err);
2110}
2111
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002112static const struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = {
2113 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
2114 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
Nicolas Dichtelcc535df2012-10-29 04:53:27 +00002115 [NETCONFA_RP_FILTER] = { .len = sizeof(int) },
stephen hemminger09aea5d2013-12-17 22:35:52 -08002116 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Andy Gospodarek974d7af2015-07-07 13:56:57 -04002117 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002118};
2119
Jakub Kicinskieede3702019-01-18 10:46:18 -08002120static int inet_netconf_valid_get_req(struct sk_buff *skb,
2121 const struct nlmsghdr *nlh,
2122 struct nlattr **tb,
2123 struct netlink_ext_ack *extack)
2124{
2125 int i, err;
2126
2127 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
2128 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf get request");
2129 return -EINVAL;
2130 }
2131
2132 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02002133 return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
2134 tb, NETCONFA_MAX,
2135 devconf_ipv4_policy, extack);
Jakub Kicinskieede3702019-01-18 10:46:18 -08002136
Johannes Berg8cb08172019-04-26 14:07:28 +02002137 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
2138 tb, NETCONFA_MAX,
2139 devconf_ipv4_policy, extack);
Jakub Kicinskieede3702019-01-18 10:46:18 -08002140 if (err)
2141 return err;
2142
2143 for (i = 0; i <= NETCONFA_MAX; i++) {
2144 if (!tb[i])
2145 continue;
2146
2147 switch (i) {
2148 case NETCONFA_IFINDEX:
2149 break;
2150 default:
2151 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in netconf get request");
2152 return -EINVAL;
2153 }
2154 }
2155
2156 return 0;
2157}
2158
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002159static int inet_netconf_get_devconf(struct sk_buff *in_skb,
David Ahernc21ef3e2017-04-16 09:48:24 -07002160 struct nlmsghdr *nlh,
2161 struct netlink_ext_ack *extack)
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002162{
2163 struct net *net = sock_net(in_skb->sk);
2164 struct nlattr *tb[NETCONFA_MAX+1];
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002165 struct sk_buff *skb;
2166 struct ipv4_devconf *devconf;
2167 struct in_device *in_dev;
2168 struct net_device *dev;
2169 int ifindex;
2170 int err;
2171
Jakub Kicinskieede3702019-01-18 10:46:18 -08002172 err = inet_netconf_valid_get_req(in_skb, nlh, tb, extack);
2173 if (err)
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002174 goto errout;
2175
Anton Protopopova97eb332016-02-16 21:43:16 -05002176 err = -EINVAL;
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002177 if (!tb[NETCONFA_IFINDEX])
2178 goto errout;
2179
2180 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
2181 switch (ifindex) {
2182 case NETCONFA_IFINDEX_ALL:
2183 devconf = net->ipv4.devconf_all;
2184 break;
2185 case NETCONFA_IFINDEX_DEFAULT:
2186 devconf = net->ipv4.devconf_dflt;
2187 break;
2188 default:
2189 dev = __dev_get_by_index(net, ifindex);
Ian Morris51456b22015-04-03 09:17:26 +01002190 if (!dev)
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002191 goto errout;
2192 in_dev = __in_dev_get_rtnl(dev);
Ian Morris51456b22015-04-03 09:17:26 +01002193 if (!in_dev)
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002194 goto errout;
2195 devconf = &in_dev->cnf;
2196 break;
2197 }
2198
2199 err = -ENOBUFS;
Eric Dumazetfa178062016-07-08 05:18:24 +02002200 skb = nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002201 if (!skb)
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002202 goto errout;
2203
2204 err = inet_netconf_fill_devconf(skb, ifindex, devconf,
2205 NETLINK_CB(in_skb).portid,
2206 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
Zhang Shengju136ba622016-03-10 08:55:50 +00002207 NETCONFA_ALL);
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002208 if (err < 0) {
2209 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2210 WARN_ON(err == -EMSGSIZE);
2211 kfree_skb(skb);
2212 goto errout;
2213 }
2214 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2215errout:
2216 return err;
2217}
2218
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002219static int inet_netconf_dump_devconf(struct sk_buff *skb,
2220 struct netlink_callback *cb)
2221{
David Ahernaddd3832018-10-07 20:16:41 -07002222 const struct nlmsghdr *nlh = cb->nlh;
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002223 struct net *net = sock_net(skb->sk);
2224 int h, s_h;
2225 int idx, s_idx;
2226 struct net_device *dev;
2227 struct in_device *in_dev;
2228 struct hlist_head *head;
2229
David Ahernaddd3832018-10-07 20:16:41 -07002230 if (cb->strict_check) {
2231 struct netlink_ext_ack *extack = cb->extack;
2232 struct netconfmsg *ncm;
2233
2234 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
2235 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf dump request");
2236 return -EINVAL;
2237 }
2238
2239 if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
2240 NL_SET_ERR_MSG(extack, "ipv4: Invalid data after header in netconf dump request");
2241 return -EINVAL;
2242 }
2243 }
2244
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002245 s_h = cb->args[0];
2246 s_idx = idx = cb->args[1];
2247
2248 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2249 idx = 0;
2250 head = &net->dev_index_head[h];
2251 rcu_read_lock();
Nicolas Dichtel04652772013-03-22 06:28:42 +00002252 cb->seq = atomic_read(&net->ipv4.dev_addr_genid) ^
2253 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002254 hlist_for_each_entry_rcu(dev, head, index_hlist) {
2255 if (idx < s_idx)
2256 goto cont;
2257 in_dev = __in_dev_get_rcu(dev);
2258 if (!in_dev)
2259 goto cont;
2260
2261 if (inet_netconf_fill_devconf(skb, dev->ifindex,
2262 &in_dev->cnf,
2263 NETLINK_CB(cb->skb).portid,
David Ahernaddd3832018-10-07 20:16:41 -07002264 nlh->nlmsg_seq,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002265 RTM_NEWNETCONF,
2266 NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +00002267 NETCONFA_ALL) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002268 rcu_read_unlock();
2269 goto done;
2270 }
Nicolas Dichtel04652772013-03-22 06:28:42 +00002271 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002272cont:
2273 idx++;
2274 }
2275 rcu_read_unlock();
2276 }
2277 if (h == NETDEV_HASHENTRIES) {
2278 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
2279 net->ipv4.devconf_all,
2280 NETLINK_CB(cb->skb).portid,
David Ahernaddd3832018-10-07 20:16:41 -07002281 nlh->nlmsg_seq,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002282 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +00002283 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002284 goto done;
2285 else
2286 h++;
2287 }
2288 if (h == NETDEV_HASHENTRIES + 1) {
2289 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
2290 net->ipv4.devconf_dflt,
2291 NETLINK_CB(cb->skb).portid,
David Ahernaddd3832018-10-07 20:16:41 -07002292 nlh->nlmsg_seq,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002293 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +00002294 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002295 goto done;
2296 else
2297 h++;
2298 }
2299done:
2300 cb->args[0] = h;
2301 cb->args[1] = idx;
2302
2303 return skb->len;
2304}
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306#ifdef CONFIG_SYSCTL
2307
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002308static void devinet_copy_dflt_conf(struct net *net, int i)
Herbert Xu31be3082007-06-04 23:35:37 -07002309{
2310 struct net_device *dev;
2311
Eric Dumazetc6d14c82009-11-04 05:43:23 -08002312 rcu_read_lock();
2313 for_each_netdev_rcu(net, dev) {
Herbert Xu31be3082007-06-04 23:35:37 -07002314 struct in_device *in_dev;
Eric Dumazetc6d14c82009-11-04 05:43:23 -08002315
Herbert Xu31be3082007-06-04 23:35:37 -07002316 in_dev = __in_dev_get_rcu(dev);
2317 if (in_dev && !test_bit(i, in_dev->cnf.state))
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -08002318 in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
Herbert Xu31be3082007-06-04 23:35:37 -07002319 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08002320 rcu_read_unlock();
Herbert Xu31be3082007-06-04 23:35:37 -07002321}
2322
Eric Dumazetc6d14c82009-11-04 05:43:23 -08002323/* called with RTNL locked */
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002324static void inet_forward_change(struct net *net)
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002325{
2326 struct net_device *dev;
Pavel Emelyanov586f1212007-12-16 13:32:48 -08002327 int on = IPV4_DEVCONF_ALL(net, FORWARDING);
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002328
Pavel Emelyanov586f1212007-12-16 13:32:48 -08002329 IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -08002330 IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
David Ahern3b022862017-03-28 14:28:02 -07002331 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2332 NETCONFA_FORWARDING,
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002333 NETCONFA_IFINDEX_ALL,
2334 net->ipv4.devconf_all);
David Ahern3b022862017-03-28 14:28:02 -07002335 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2336 NETCONFA_FORWARDING,
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002337 NETCONFA_IFINDEX_DEFAULT,
2338 net->ipv4.devconf_dflt);
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002339
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002340 for_each_netdev(net, dev) {
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002341 struct in_device *in_dev;
Eric Dumazetfa178062016-07-08 05:18:24 +02002342
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002343 if (on)
2344 dev_disable_lro(dev);
Eric Dumazetfa178062016-07-08 05:18:24 +02002345
2346 in_dev = __in_dev_get_rtnl(dev);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002347 if (in_dev) {
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002348 IN_DEV_CONF_SET(in_dev, FORWARDING, on);
David Ahern3b022862017-03-28 14:28:02 -07002349 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2350 NETCONFA_FORWARDING,
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002351 dev->ifindex, &in_dev->cnf);
2352 }
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002353 }
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002354}
2355
stephen hemmingerf085ff12013-12-12 13:06:50 -08002356static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf)
2357{
2358 if (cnf == net->ipv4.devconf_dflt)
2359 return NETCONFA_IFINDEX_DEFAULT;
2360 else if (cnf == net->ipv4.devconf_all)
2361 return NETCONFA_IFINDEX_ALL;
2362 else {
2363 struct in_device *idev
2364 = container_of(cnf, struct in_device, cnf);
2365 return idev->dev->ifindex;
2366 }
2367}
2368
Joe Perchesfe2c6332013-06-11 23:04:25 -07002369static int devinet_conf_proc(struct ctl_table *ctl, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02002370 void *buffer, size_t *lenp, loff_t *ppos)
Herbert Xu31be3082007-06-04 23:35:37 -07002371{
Peter Pan(潘卫平)d01ff0a2011-12-01 15:47:06 +00002372 int old_value = *(int *)ctl->data;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002373 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Peter Pan(潘卫平)d01ff0a2011-12-01 15:47:06 +00002374 int new_value = *(int *)ctl->data;
Herbert Xu31be3082007-06-04 23:35:37 -07002375
2376 if (write) {
2377 struct ipv4_devconf *cnf = ctl->extra1;
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002378 struct net *net = ctl->extra2;
Herbert Xu31be3082007-06-04 23:35:37 -07002379 int i = (int *)ctl->data - cnf->data;
stephen hemmingerf085ff12013-12-12 13:06:50 -08002380 int ifindex;
Herbert Xu31be3082007-06-04 23:35:37 -07002381
2382 set_bit(i, cnf->state);
2383
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -08002384 if (cnf == net->ipv4.devconf_dflt)
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002385 devinet_copy_dflt_conf(net, i);
Thomas Grafd0daebc32012-06-12 00:44:01 +00002386 if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1 ||
2387 i == IPV4_DEVCONF_ROUTE_LOCALNET - 1)
Peter Pan(潘卫平)d01ff0a2011-12-01 15:47:06 +00002388 if ((new_value == 0) && (old_value != 0))
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00002389 rt_cache_flush(net);
stephen hemmingerf085ff12013-12-12 13:06:50 -08002390
Xin Long5cbf7772018-07-27 16:37:28 +08002391 if (i == IPV4_DEVCONF_BC_FORWARDING - 1 &&
2392 new_value != old_value)
2393 rt_cache_flush(net);
2394
Nicolas Dichtelcc535df2012-10-29 04:53:27 +00002395 if (i == IPV4_DEVCONF_RP_FILTER - 1 &&
2396 new_value != old_value) {
stephen hemmingerf085ff12013-12-12 13:06:50 -08002397 ifindex = devinet_conf_ifindex(net, cnf);
David Ahern3b022862017-03-28 14:28:02 -07002398 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2399 NETCONFA_RP_FILTER,
Nicolas Dichtelcc535df2012-10-29 04:53:27 +00002400 ifindex, cnf);
2401 }
stephen hemmingerf085ff12013-12-12 13:06:50 -08002402 if (i == IPV4_DEVCONF_PROXY_ARP - 1 &&
2403 new_value != old_value) {
2404 ifindex = devinet_conf_ifindex(net, cnf);
David Ahern3b022862017-03-28 14:28:02 -07002405 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2406 NETCONFA_PROXY_NEIGH,
stephen hemmingerf085ff12013-12-12 13:06:50 -08002407 ifindex, cnf);
2408 }
Andy Gospodarek974d7af2015-07-07 13:56:57 -04002409 if (i == IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN - 1 &&
2410 new_value != old_value) {
2411 ifindex = devinet_conf_ifindex(net, cnf);
David Ahern3b022862017-03-28 14:28:02 -07002412 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2413 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
Andy Gospodarek974d7af2015-07-07 13:56:57 -04002414 ifindex, cnf);
2415 }
Herbert Xu31be3082007-06-04 23:35:37 -07002416 }
2417
2418 return ret;
2419}
2420
Joe Perchesfe2c6332013-06-11 23:04:25 -07002421static int devinet_sysctl_forward(struct ctl_table *ctl, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02002422 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
2424 int *valp = ctl->data;
2425 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00002426 loff_t pos = *ppos;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002427 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429 if (write && *valp != val) {
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002430 struct net *net = ctl->extra2;
2431
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002432 if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
Eric W. Biederman88af1822010-02-19 13:22:59 +00002433 if (!rtnl_trylock()) {
2434 /* Restore the original values before restarting */
2435 *valp = val;
2436 *ppos = pos;
Eric W. Biederman9b8adb52009-05-13 16:59:21 +00002437 return restart_syscall();
Eric W. Biederman88af1822010-02-19 13:22:59 +00002438 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002439 if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
2440 inet_forward_change(net);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002441 } else {
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002442 struct ipv4_devconf *cnf = ctl->extra1;
2443 struct in_device *idev =
2444 container_of(cnf, struct in_device, cnf);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002445 if (*valp)
2446 dev_disable_lro(idev->dev);
David Ahern3b022862017-03-28 14:28:02 -07002447 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002448 NETCONFA_FORWARDING,
2449 idev->dev->ifindex,
2450 cnf);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002451 }
2452 rtnl_unlock();
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00002453 rt_cache_flush(net);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002454 } else
David Ahern3b022862017-03-28 14:28:02 -07002455 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2456 NETCONFA_FORWARDING,
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002457 NETCONFA_IFINDEX_DEFAULT,
2458 net->ipv4.devconf_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
2460
2461 return ret;
2462}
2463
Joe Perchesfe2c6332013-06-11 23:04:25 -07002464static int ipv4_doint_and_flush(struct ctl_table *ctl, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02002465 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
2467 int *valp = ctl->data;
2468 int val = *valp;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002469 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -07002470 struct net *net = ctl->extra2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 if (write && *valp != val)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00002473 rt_cache_flush(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 return ret;
2476}
2477
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002478#define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
Herbert Xu42f811b2007-06-04 23:34:44 -07002479 { \
Herbert Xu42f811b2007-06-04 23:34:44 -07002480 .procname = name, \
2481 .data = ipv4_devconf.data + \
Eric W. Biederman02291682010-02-14 03:25:51 +00002482 IPV4_DEVCONF_ ## attr - 1, \
Herbert Xu42f811b2007-06-04 23:34:44 -07002483 .maxlen = sizeof(int), \
2484 .mode = mval, \
2485 .proc_handler = proc, \
Herbert Xu31be3082007-06-04 23:35:37 -07002486 .extra1 = &ipv4_devconf, \
Herbert Xu42f811b2007-06-04 23:34:44 -07002487 }
2488
2489#define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002490 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
Herbert Xu42f811b2007-06-04 23:34:44 -07002491
2492#define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002493 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
Herbert Xu42f811b2007-06-04 23:34:44 -07002494
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002495#define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
2496 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
Herbert Xu42f811b2007-06-04 23:34:44 -07002497
2498#define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002499 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
Herbert Xu42f811b2007-06-04 23:34:44 -07002500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501static struct devinet_sysctl_table {
2502 struct ctl_table_header *sysctl_header;
Eric W. Biederman02291682010-02-14 03:25:51 +00002503 struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504} devinet_sysctl = {
2505 .devinet_vars = {
Herbert Xu42f811b2007-06-04 23:34:44 -07002506 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002507 devinet_sysctl_forward),
Herbert Xu42f811b2007-06-04 23:34:44 -07002508 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
Xin Long5cbf7772018-07-27 16:37:28 +08002509 DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"),
Herbert Xu42f811b2007-06-04 23:34:44 -07002510
2511 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
2512 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
2513 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
2514 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
2515 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
2516 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
2517 "accept_source_route"),
Patrick McHardy8153a102009-12-03 01:25:58 +00002518 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
Jamal Hadi Salim28f6aee2009-12-25 17:30:22 -08002519 DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
Herbert Xu42f811b2007-06-04 23:34:44 -07002520 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
2521 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
2522 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
2523 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
2524 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
2525 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
2526 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
2527 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
2528 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
Stephen Hemmingereefef1c2009-02-01 01:04:33 -08002529 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
Jesper Dangaard Brouer65324142010-01-05 05:50:47 +00002530 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
William Manley5c6fe012013-08-06 19:03:14 +01002531 DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION,
2532 "force_igmp_version"),
William Manley26900482013-08-06 19:03:15 +01002533 DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL,
2534 "igmpv2_unsolicited_report_interval"),
2535 DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL,
2536 "igmpv3_unsolicited_report_interval"),
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04002537 DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN,
2538 "ignore_routes_with_linkdown"),
Johannes Berg97daf332016-02-04 13:31:18 +01002539 DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP,
2540 "drop_gratuitous_arp"),
Herbert Xu42f811b2007-06-04 23:34:44 -07002541
2542 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
2543 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
Herbert Xu42f811b2007-06-04 23:34:44 -07002544 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
2545 "promote_secondaries"),
Thomas Grafd0daebc32012-06-12 00:44:01 +00002546 DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET,
2547 "route_localnet"),
Johannes Berg12b74df2016-02-04 13:31:17 +01002548 DEVINET_SYSCTL_FLUSHING_ENTRY(DROP_UNICAST_IN_L2_MULTICAST,
2549 "drop_unicast_in_l2_multicast"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551};
2552
Pavel Emelyanovea40b322007-12-16 13:30:07 -08002553static int __devinet_sysctl_register(struct net *net, char *dev_name,
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002554 int ifindex, struct ipv4_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556 int i;
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002557 struct devinet_sysctl_table *t;
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002558 char path[sizeof("net/ipv4/conf/") + IFNAMSIZ];
Pavel Emelyanovbfada692007-12-02 00:57:08 +11002559
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002560 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (!t)
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002562 goto out;
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
2565 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
Herbert Xu31be3082007-06-04 23:35:37 -07002566 t->devinet_vars[i].extra1 = p;
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002567 t->devinet_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
2569
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002570 snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002572 t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (!t->sysctl_header)
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002574 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 p->sysctl = t;
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002577
David Ahern3b022862017-03-28 14:28:02 -07002578 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
2579 ifindex, p);
Pavel Emelyanovea40b322007-12-16 13:30:07 -08002580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002582free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 kfree(t);
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002584out:
Pavel Emelyanovea40b322007-12-16 13:30:07 -08002585 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
2587
David Ahernb5c96412017-03-28 14:28:03 -07002588static void __devinet_sysctl_unregister(struct net *net,
2589 struct ipv4_devconf *cnf, int ifindex)
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002590{
2591 struct devinet_sysctl_table *t = cnf->sysctl;
2592
David Ahernb5c96412017-03-28 14:28:03 -07002593 if (t) {
2594 cnf->sysctl = NULL;
2595 unregister_net_sysctl_table(t->sysctl_header);
2596 kfree(t);
2597 }
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002598
David Ahernb5c96412017-03-28 14:28:03 -07002599 inet_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002600}
2601
WANG Cong20e61da2014-07-25 15:25:08 -07002602static int devinet_sysctl_register(struct in_device *idev)
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11002603{
WANG Cong20e61da2014-07-25 15:25:08 -07002604 int err;
2605
2606 if (!sysctl_dev_name_is_allowed(idev->dev->name))
2607 return -EINVAL;
2608
2609 err = neigh_sysctl_register(idev->dev, idev->arp_parms, NULL);
2610 if (err)
2611 return err;
2612 err = __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002613 idev->dev->ifindex, &idev->cnf);
WANG Cong20e61da2014-07-25 15:25:08 -07002614 if (err)
2615 neigh_sysctl_unregister(idev->arp_parms);
2616 return err;
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11002617}
2618
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002619static void devinet_sysctl_unregister(struct in_device *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
David Ahernb5c96412017-03-28 14:28:03 -07002621 struct net *net = dev_net(idev->dev);
2622
2623 __devinet_sysctl_unregister(net, &idev->cnf, idev->dev->ifindex);
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002624 neigh_sysctl_unregister(idev->arp_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002627static struct ctl_table ctl_forward_entry[] = {
2628 {
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002629 .procname = "ip_forward",
2630 .data = &ipv4_devconf.data[
Eric W. Biederman02291682010-02-14 03:25:51 +00002631 IPV4_DEVCONF_FORWARDING - 1],
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002632 .maxlen = sizeof(int),
2633 .mode = 0644,
2634 .proc_handler = devinet_sysctl_forward,
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002635 .extra1 = &ipv4_devconf,
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002636 .extra2 = &init_net,
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002637 },
2638 { },
2639};
Eric Dumazet2a75de02008-01-05 23:08:49 -08002640#endif
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002641
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002642static __net_init int devinet_init_net(struct net *net)
2643{
2644 int err;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002645 struct ipv4_devconf *all, *dflt;
Eric Dumazet2a75de02008-01-05 23:08:49 -08002646#ifdef CONFIG_SYSCTL
Cong Wang856c3952019-01-17 23:27:11 -08002647 struct ctl_table *tbl;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002648 struct ctl_table_header *forw_hdr;
Eric Dumazet2a75de02008-01-05 23:08:49 -08002649#endif
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002650
2651 err = -ENOMEM;
Cong Wang856c3952019-01-17 23:27:11 -08002652 all = kmemdup(&ipv4_devconf, sizeof(ipv4_devconf), GFP_KERNEL);
2653 if (!all)
2654 goto err_alloc_all;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002655
Cong Wang856c3952019-01-17 23:27:11 -08002656 dflt = kmemdup(&ipv4_devconf_dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
2657 if (!dflt)
2658 goto err_alloc_dflt;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002659
Eric Dumazet2a75de02008-01-05 23:08:49 -08002660#ifdef CONFIG_SYSCTL
Cong Wang856c3952019-01-17 23:27:11 -08002661 tbl = kmemdup(ctl_forward_entry, sizeof(ctl_forward_entry), GFP_KERNEL);
2662 if (!tbl)
2663 goto err_alloc_ctl;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002664
Cong Wang856c3952019-01-17 23:27:11 -08002665 tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
2666 tbl[0].extra1 = all;
2667 tbl[0].extra2 = net;
Eric Dumazet2a75de02008-01-05 23:08:49 -08002668#endif
Cong Wang856c3952019-01-17 23:27:11 -08002669
Nicolas Dichtel9efd6a32020-05-13 15:58:43 +02002670 if (!net_eq(net, &init_net)) {
2671 if (IS_ENABLED(CONFIG_SYSCTL) &&
2672 sysctl_devconf_inherit_init_net == 3) {
2673 /* copy from the current netns */
2674 memcpy(all, current->nsproxy->net_ns->ipv4.devconf_all,
2675 sizeof(ipv4_devconf));
2676 memcpy(dflt,
2677 current->nsproxy->net_ns->ipv4.devconf_dflt,
2678 sizeof(ipv4_devconf_dflt));
2679 } else if (!IS_ENABLED(CONFIG_SYSCTL) ||
2680 sysctl_devconf_inherit_init_net != 2) {
2681 /* inherit == 0 or 1: copy from init_net */
2682 memcpy(all, init_net.ipv4.devconf_all,
2683 sizeof(ipv4_devconf));
2684 memcpy(dflt, init_net.ipv4.devconf_dflt,
2685 sizeof(ipv4_devconf_dflt));
2686 }
2687 /* else inherit == 2: use compiled values */
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002688 }
2689
2690#ifdef CONFIG_SYSCTL
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002691 err = __devinet_sysctl_register(net, "all", NETCONFA_IFINDEX_ALL, all);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002692 if (err < 0)
2693 goto err_reg_all;
2694
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002695 err = __devinet_sysctl_register(net, "default",
2696 NETCONFA_IFINDEX_DEFAULT, dflt);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002697 if (err < 0)
2698 goto err_reg_dflt;
2699
2700 err = -ENOMEM;
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002701 forw_hdr = register_net_sysctl(net, "net/ipv4", tbl);
Ian Morris51456b22015-04-03 09:17:26 +01002702 if (!forw_hdr)
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002703 goto err_reg_ctl;
Eric Dumazet2a75de02008-01-05 23:08:49 -08002704 net->ipv4.forw_hdr = forw_hdr;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002705#endif
2706
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002707 net->ipv4.devconf_all = all;
2708 net->ipv4.devconf_dflt = dflt;
2709 return 0;
2710
2711#ifdef CONFIG_SYSCTL
2712err_reg_ctl:
David Ahernb5c96412017-03-28 14:28:03 -07002713 __devinet_sysctl_unregister(net, dflt, NETCONFA_IFINDEX_DEFAULT);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002714err_reg_dflt:
David Ahernb5c96412017-03-28 14:28:03 -07002715 __devinet_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002716err_reg_all:
Cong Wang856c3952019-01-17 23:27:11 -08002717 kfree(tbl);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002718err_alloc_ctl:
Eric Dumazet2a75de02008-01-05 23:08:49 -08002719#endif
Cong Wang856c3952019-01-17 23:27:11 -08002720 kfree(dflt);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002721err_alloc_dflt:
Cong Wang856c3952019-01-17 23:27:11 -08002722 kfree(all);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002723err_alloc_all:
2724 return err;
2725}
2726
2727static __net_exit void devinet_exit_net(struct net *net)
2728{
Eric Dumazet2a75de02008-01-05 23:08:49 -08002729#ifdef CONFIG_SYSCTL
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002730 struct ctl_table *tbl;
2731
2732 tbl = net->ipv4.forw_hdr->ctl_table_arg;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002733 unregister_net_sysctl_table(net->ipv4.forw_hdr);
David Ahernb5c96412017-03-28 14:28:03 -07002734 __devinet_sysctl_unregister(net, net->ipv4.devconf_dflt,
2735 NETCONFA_IFINDEX_DEFAULT);
2736 __devinet_sysctl_unregister(net, net->ipv4.devconf_all,
2737 NETCONFA_IFINDEX_ALL);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002738 kfree(tbl);
Eric Dumazet2a75de02008-01-05 23:08:49 -08002739#endif
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002740 kfree(net->ipv4.devconf_dflt);
2741 kfree(net->ipv4.devconf_all);
2742}
2743
2744static __net_initdata struct pernet_operations devinet_ops = {
2745 .init = devinet_init_net,
2746 .exit = devinet_exit_net,
2747};
2748
Daniel Borkmann207895fd32015-01-29 12:15:03 +01002749static struct rtnl_af_ops inet_af_ops __read_mostly = {
Thomas Graf9f0f7272010-11-16 04:32:48 +00002750 .family = AF_INET,
2751 .fill_link_af = inet_fill_link_af,
2752 .get_link_af_size = inet_get_link_af_size,
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002753 .validate_link_af = inet_validate_link_af,
2754 .set_link_af = inet_set_link_af,
Thomas Graf9f0f7272010-11-16 04:32:48 +00002755};
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757void __init devinet_init(void)
2758{
David S. Millerfd23c3b2011-02-18 12:42:28 -08002759 int i;
2760
2761 for (i = 0; i < IN4_ADDR_HSIZE; i++)
2762 INIT_HLIST_HEAD(&inet_addr_lst[i]);
2763
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002764 register_pernet_subsys(&devinet_ops);
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 register_gifconf(PF_INET, inet_gifconf);
2767 register_netdevice_notifier(&ip_netdev_notifier);
Thomas Graf63f34442007-03-22 11:55:17 -07002768
viresh kumar906e0732014-01-22 12:23:32 +05302769 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
Jiri Pirko5c766d62013-01-24 09:41:41 +00002770
Thomas Graf9f0f7272010-11-16 04:32:48 +00002771 rtnl_af_register(&inet_af_ops);
2772
Florian Westphalb97bac62017-08-09 20:41:48 +02002773 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, 0);
2774 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, 0);
2775 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, 0);
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002776 rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf,
Florian Westphalb97bac62017-08-09 20:41:48 +02002777 inet_netconf_dump_devconf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}