blob: c82aded8da7dd34cd9e9639b21a0a957d1e7d862 [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{
Vasily Averin61268912021-07-19 13:44:31 +0300218 return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL_ACCOUNT);
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;
Menglong Dong30e23792020-11-08 09:05:41 +0800653 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 ASSERT_RTNL();
656
Johannes Berg8cb08172019-04-26 14:07:28 +0200657 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
658 ifa_ipv4_policy, extack);
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700659 if (err < 0)
660 goto errout;
661
662 ifm = nlmsg_data(nlh);
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -0800663 in_dev = inetdev_by_index(net, ifm->ifa_index);
Ian Morris51456b22015-04-03 09:17:26 +0100664 if (!in_dev) {
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700665 err = -ENODEV;
666 goto errout;
667 }
668
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200669 for (ifap = &in_dev->ifa_list; (ifa = rtnl_dereference(*ifap)) != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 ifap = &ifa->ifa_next) {
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700671 if (tb[IFA_LOCAL] &&
Jiri Benc67b61f62015-03-29 16:59:26 +0200672 ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 continue;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700674
675 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
676 continue;
677
678 if (tb[IFA_ADDRESS] &&
679 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
Jiri Benc67b61f62015-03-29 16:59:26 +0200680 !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa)))
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700681 continue;
682
Madhu Challa93a714d2015-02-25 09:58:35 -0800683 if (ipv4_is_multicast(ifa->ifa_address))
Taras Chornyi690cc862020-04-09 20:25:24 +0300684 ip_mc_autojoin_config(net, false, ifa);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000685 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return 0;
687 }
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700688
689 err = -EADDRNOTAVAIL;
690errout:
691 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Jiri Pirko5c766d62013-01-24 09:41:41 +0000694#define INFINITY_LIFE_TIME 0xFFFFFFFF
695
696static void check_lifetime(struct work_struct *work)
697{
698 unsigned long now, next, next_sec, next_sched;
699 struct in_ifaddr *ifa;
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000700 struct hlist_node *n;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000701 int i;
702
703 now = jiffies;
704 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
705
Jiri Pirko5c766d62013-01-24 09:41:41 +0000706 for (i = 0; i < IN4_ADDR_HSIZE; i++) {
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000707 bool change_needed = false;
708
709 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800710 hlist_for_each_entry_rcu(ifa, &inet_addr_lst[i], hash) {
Jiri Pirko5c766d62013-01-24 09:41:41 +0000711 unsigned long age;
712
713 if (ifa->ifa_flags & IFA_F_PERMANENT)
714 continue;
715
716 /* We try to batch several events at once. */
717 age = (now - ifa->ifa_tstamp +
718 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
719
720 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
721 age >= ifa->ifa_valid_lft) {
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000722 change_needed = true;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000723 } else if (ifa->ifa_preferred_lft ==
724 INFINITY_LIFE_TIME) {
725 continue;
726 } else if (age >= ifa->ifa_preferred_lft) {
727 if (time_before(ifa->ifa_tstamp +
728 ifa->ifa_valid_lft * HZ, next))
729 next = ifa->ifa_tstamp +
730 ifa->ifa_valid_lft * HZ;
731
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000732 if (!(ifa->ifa_flags & IFA_F_DEPRECATED))
733 change_needed = true;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000734 } else if (time_before(ifa->ifa_tstamp +
735 ifa->ifa_preferred_lft * HZ,
736 next)) {
737 next = ifa->ifa_tstamp +
738 ifa->ifa_preferred_lft * HZ;
739 }
740 }
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000741 rcu_read_unlock();
742 if (!change_needed)
743 continue;
744 rtnl_lock();
745 hlist_for_each_entry_safe(ifa, n, &inet_addr_lst[i], hash) {
746 unsigned long age;
747
748 if (ifa->ifa_flags & IFA_F_PERMANENT)
749 continue;
750
751 /* We try to batch several events at once. */
752 age = (now - ifa->ifa_tstamp +
753 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
754
755 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
756 age >= ifa->ifa_valid_lft) {
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200757 struct in_ifaddr __rcu **ifap;
758 struct in_ifaddr *tmp;
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000759
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200760 ifap = &ifa->ifa_dev->ifa_list;
761 tmp = rtnl_dereference(*ifap);
762 while (tmp) {
Florian Westphal40008e92019-06-17 16:02:27 +0200763 if (tmp == ifa) {
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000764 inet_del_ifa(ifa->ifa_dev,
765 ifap, 1);
766 break;
767 }
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200768 ifap = &tmp->ifa_next;
769 tmp = rtnl_dereference(*ifap);
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000770 }
771 } else if (ifa->ifa_preferred_lft !=
772 INFINITY_LIFE_TIME &&
773 age >= ifa->ifa_preferred_lft &&
774 !(ifa->ifa_flags & IFA_F_DEPRECATED)) {
775 ifa->ifa_flags |= IFA_F_DEPRECATED;
776 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
777 }
778 }
779 rtnl_unlock();
Jiri Pirko5c766d62013-01-24 09:41:41 +0000780 }
Jiri Pirko5c766d62013-01-24 09:41:41 +0000781
782 next_sec = round_jiffies_up(next);
783 next_sched = next;
784
785 /* If rounded timeout is accurate enough, accept it. */
786 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
787 next_sched = next_sec;
788
789 now = jiffies;
790 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
791 if (time_before(next_sched, now + ADDRCONF_TIMER_FUZZ_MAX))
792 next_sched = now + ADDRCONF_TIMER_FUZZ_MAX;
793
viresh kumar906e0732014-01-22 12:23:32 +0530794 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work,
795 next_sched - now);
Jiri Pirko5c766d62013-01-24 09:41:41 +0000796}
797
798static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft,
799 __u32 prefered_lft)
800{
801 unsigned long timeout;
802
803 ifa->ifa_flags &= ~(IFA_F_PERMANENT | IFA_F_DEPRECATED);
804
805 timeout = addrconf_timeout_fixup(valid_lft, HZ);
806 if (addrconf_finite_timeout(timeout))
807 ifa->ifa_valid_lft = timeout;
808 else
809 ifa->ifa_flags |= IFA_F_PERMANENT;
810
811 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
812 if (addrconf_finite_timeout(timeout)) {
813 if (timeout == 0)
814 ifa->ifa_flags |= IFA_F_DEPRECATED;
815 ifa->ifa_preferred_lft = timeout;
816 }
817 ifa->ifa_tstamp = jiffies;
818 if (!ifa->ifa_cstamp)
819 ifa->ifa_cstamp = ifa->ifa_tstamp;
820}
821
822static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
David Aherndac9c972018-10-07 20:16:24 -0700823 __u32 *pvalid_lft, __u32 *pprefered_lft,
824 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Thomas Graf5c753972006-08-04 23:03:53 -0700826 struct nlattr *tb[IFA_MAX+1];
827 struct in_ifaddr *ifa;
828 struct ifaddrmsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 struct net_device *dev;
830 struct in_device *in_dev;
Denis V. Lunev7b218572008-01-31 18:47:00 -0800831 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Johannes Berg8cb08172019-04-26 14:07:28 +0200833 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
834 ifa_ipv4_policy, extack);
Thomas Graf5c753972006-08-04 23:03:53 -0700835 if (err < 0)
836 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Thomas Graf5c753972006-08-04 23:03:53 -0700838 ifm = nlmsg_data(nlh);
Denis V. Lunev7b218572008-01-31 18:47:00 -0800839 err = -EINVAL;
Ian Morris51456b22015-04-03 09:17:26 +0100840 if (ifm->ifa_prefixlen > 32 || !tb[IFA_LOCAL])
Thomas Graf5c753972006-08-04 23:03:53 -0700841 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -0800843 dev = __dev_get_by_index(net, ifm->ifa_index);
Denis V. Lunev7b218572008-01-31 18:47:00 -0800844 err = -ENODEV;
Ian Morris51456b22015-04-03 09:17:26 +0100845 if (!dev)
Thomas Graf5c753972006-08-04 23:03:53 -0700846 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Thomas Graf5c753972006-08-04 23:03:53 -0700848 in_dev = __in_dev_get_rtnl(dev);
Denis V. Lunev7b218572008-01-31 18:47:00 -0800849 err = -ENOBUFS;
Ian Morris51456b22015-04-03 09:17:26 +0100850 if (!in_dev)
Herbert Xu71e27da2007-06-04 23:36:06 -0700851 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Thomas Graf5c753972006-08-04 23:03:53 -0700853 ifa = inet_alloc_ifa();
Ian Morris51456b22015-04-03 09:17:26 +0100854 if (!ifa)
Thomas Graf5c753972006-08-04 23:03:53 -0700855 /*
856 * A potential indev allocation can be left alive, it stays
857 * assigned to its device and is destroy with it.
858 */
Thomas Graf5c753972006-08-04 23:03:53 -0700859 goto errout;
Thomas Graf5c753972006-08-04 23:03:53 -0700860
Pavel Emelyanova4e65d32007-12-07 23:55:43 -0800861 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100862 neigh_parms_data_state_setall(in_dev->arp_parms);
Thomas Graf5c753972006-08-04 23:03:53 -0700863 in_dev_hold(in_dev);
864
Ian Morris51456b22015-04-03 09:17:26 +0100865 if (!tb[IFA_ADDRESS])
Thomas Graf5c753972006-08-04 23:03:53 -0700866 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
867
David S. Millerfd23c3b2011-02-18 12:42:28 -0800868 INIT_HLIST_NODE(&ifa->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
870 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
Jiri Pirkoad6c8132013-12-08 12:16:10 +0100871 ifa->ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) :
872 ifm->ifa_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 ifa->ifa_scope = ifm->ifa_scope;
Thomas Graf5c753972006-08-04 23:03:53 -0700874 ifa->ifa_dev = in_dev;
875
Jiri Benc67b61f62015-03-29 16:59:26 +0200876 ifa->ifa_local = nla_get_in_addr(tb[IFA_LOCAL]);
877 ifa->ifa_address = nla_get_in_addr(tb[IFA_ADDRESS]);
Thomas Graf5c753972006-08-04 23:03:53 -0700878
879 if (tb[IFA_BROADCAST])
Jiri Benc67b61f62015-03-29 16:59:26 +0200880 ifa->ifa_broadcast = nla_get_in_addr(tb[IFA_BROADCAST]);
Thomas Graf5c753972006-08-04 23:03:53 -0700881
Thomas Graf5c753972006-08-04 23:03:53 -0700882 if (tb[IFA_LABEL])
Francis Laniel872f6902020-11-15 18:08:06 +0100883 nla_strscpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 else
885 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
886
David Ahernaf4d7682018-05-27 08:09:57 -0700887 if (tb[IFA_RT_PRIORITY])
888 ifa->ifa_rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
889
Jiri Pirko5c766d62013-01-24 09:41:41 +0000890 if (tb[IFA_CACHEINFO]) {
891 struct ifa_cacheinfo *ci;
892
893 ci = nla_data(tb[IFA_CACHEINFO]);
894 if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) {
895 err = -EINVAL;
Daniel Borkmann446266b2013-08-02 11:32:43 +0200896 goto errout_free;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000897 }
898 *pvalid_lft = ci->ifa_valid;
899 *pprefered_lft = ci->ifa_prefered;
900 }
901
Thomas Graf5c753972006-08-04 23:03:53 -0700902 return ifa;
903
Daniel Borkmann446266b2013-08-02 11:32:43 +0200904errout_free:
905 inet_free_ifa(ifa);
Thomas Graf5c753972006-08-04 23:03:53 -0700906errout:
907 return ERR_PTR(err);
908}
909
Jiri Pirko5c766d62013-01-24 09:41:41 +0000910static struct in_ifaddr *find_matching_ifa(struct in_ifaddr *ifa)
911{
912 struct in_device *in_dev = ifa->ifa_dev;
Florian Westphalef11db32019-05-31 18:27:04 +0200913 struct in_ifaddr *ifa1;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000914
915 if (!ifa->ifa_local)
916 return NULL;
917
Florian Westphalef11db32019-05-31 18:27:04 +0200918 in_dev_for_each_ifa_rtnl(ifa1, in_dev) {
Jiri Pirko5c766d62013-01-24 09:41:41 +0000919 if (ifa1->ifa_mask == ifa->ifa_mask &&
920 inet_ifa_match(ifa1->ifa_address, ifa) &&
921 ifa1->ifa_local == ifa->ifa_local)
922 return ifa1;
923 }
924 return NULL;
925}
926
David Ahernc21ef3e2017-04-16 09:48:24 -0700927static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
928 struct netlink_ext_ack *extack)
Thomas Graf5c753972006-08-04 23:03:53 -0700929{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900930 struct net *net = sock_net(skb->sk);
Thomas Graf5c753972006-08-04 23:03:53 -0700931 struct in_ifaddr *ifa;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000932 struct in_ifaddr *ifa_existing;
933 __u32 valid_lft = INFINITY_LIFE_TIME;
934 __u32 prefered_lft = INFINITY_LIFE_TIME;
Thomas Graf5c753972006-08-04 23:03:53 -0700935
936 ASSERT_RTNL();
937
David Aherndac9c972018-10-07 20:16:24 -0700938 ifa = rtm_to_ifaddr(net, nlh, &valid_lft, &prefered_lft, extack);
Thomas Graf5c753972006-08-04 23:03:53 -0700939 if (IS_ERR(ifa))
940 return PTR_ERR(ifa);
941
Jiri Pirko5c766d62013-01-24 09:41:41 +0000942 ifa_existing = find_matching_ifa(ifa);
943 if (!ifa_existing) {
944 /* It would be best to check for !NLM_F_CREATE here but
stephen hemminger614d0562014-05-16 20:46:58 -0700945 * userspace already relies on not having to provide this.
Jiri Pirko5c766d62013-01-24 09:41:41 +0000946 */
947 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
Madhu Challa93a714d2015-02-25 09:58:35 -0800948 if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) {
Taras Chornyi690cc862020-04-09 20:25:24 +0300949 int ret = ip_mc_autojoin_config(net, true, ifa);
Madhu Challa93a714d2015-02-25 09:58:35 -0800950
951 if (ret < 0) {
952 inet_free_ifa(ifa);
953 return ret;
954 }
955 }
David Ahernde95e042017-10-18 09:56:54 -0700956 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid,
957 extack);
Jiri Pirko5c766d62013-01-24 09:41:41 +0000958 } else {
David Ahernaf4d7682018-05-27 08:09:57 -0700959 u32 new_metric = ifa->ifa_rt_priority;
960
Jiri Pirko5c766d62013-01-24 09:41:41 +0000961 inet_free_ifa(ifa);
962
963 if (nlh->nlmsg_flags & NLM_F_EXCL ||
964 !(nlh->nlmsg_flags & NLM_F_REPLACE))
965 return -EEXIST;
Jiri Pirko34e2ed32013-04-04 08:33:00 +0000966 ifa = ifa_existing;
David Ahernaf4d7682018-05-27 08:09:57 -0700967
968 if (ifa->ifa_rt_priority != new_metric) {
969 fib_modify_prefix_metric(ifa, new_metric);
970 ifa->ifa_rt_priority = new_metric;
971 }
972
Jiri Pirko34e2ed32013-04-04 08:33:00 +0000973 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
Jiri Pirko05a324b2013-04-04 23:39:38 +0000974 cancel_delayed_work(&check_lifetime_work);
viresh kumar906e0732014-01-22 12:23:32 +0530975 queue_delayed_work(system_power_efficient_wq,
976 &check_lifetime_work, 0);
Jiri Pirko34e2ed32013-04-04 08:33:00 +0000977 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid);
Jiri Pirko5c766d62013-01-24 09:41:41 +0000978 }
979 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982/*
983 * Determine a default network mask, based on the IP address.
984 */
985
Eric Dumazet40384992012-08-03 21:06:50 +0000986static int inet_abc_len(__be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 int rc = -1; /* Something else, probably a multicast. */
989
Dave Taht65cab852018-12-11 15:30:34 -0800990 if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900991 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 else {
Al Viro714e85b2006-11-14 20:51:49 -0800993 __u32 haddr = ntohl(addr);
Al Viro714e85b2006-11-14 20:51:49 -0800994 if (IN_CLASSA(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 rc = 8;
Al Viro714e85b2006-11-14 20:51:49 -0800996 else if (IN_CLASSB(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 rc = 16;
Al Viro714e85b2006-11-14 20:51:49 -0800998 else if (IN_CLASSC(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 rc = 24;
Dave Taht65cab852018-12-11 15:30:34 -08001000 else if (IN_CLASSE(haddr))
1001 rc = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001004 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
1007
Al Viro03aef172017-07-01 07:53:12 -04001008int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 struct sockaddr_in sin_orig;
Al Viro03aef172017-07-01 07:53:12 -04001011 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
Florian Westphal2638eb8b2019-05-31 18:27:09 +02001012 struct in_ifaddr __rcu **ifap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 struct in_ifaddr *ifa = NULL;
1015 struct net_device *dev;
1016 char *colon;
1017 int ret = -EFAULT;
1018 int tryaddrmatch = 0;
1019
Al Viro03aef172017-07-01 07:53:12 -04001020 ifr->ifr_name[IFNAMSIZ - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 /* save original address for comparison */
1023 memcpy(&sin_orig, sin, sizeof(*sin));
1024
Al Viro03aef172017-07-01 07:53:12 -04001025 colon = strchr(ifr->ifr_name, ':');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (colon)
1027 *colon = 0;
1028
Al Viro03aef172017-07-01 07:53:12 -04001029 dev_load(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Stephen Hemminger132adf52007-03-08 20:44:43 -08001031 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 case SIOCGIFADDR: /* Get interface address */
1033 case SIOCGIFBRDADDR: /* Get the broadcast address */
1034 case SIOCGIFDSTADDR: /* Get the destination address */
1035 case SIOCGIFNETMASK: /* Get the netmask for the interface */
1036 /* Note that these ioctls will not sleep,
1037 so that we do not impose a lock.
1038 One day we will be forced to put shlock here (I mean SMP)
1039 */
1040 tryaddrmatch = (sin_orig.sin_family == AF_INET);
1041 memset(sin, 0, sizeof(*sin));
1042 sin->sin_family = AF_INET;
1043 break;
1044
1045 case SIOCSIFFLAGS:
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +00001046 ret = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001047 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 goto out;
1049 break;
1050 case SIOCSIFADDR: /* Set interface address (and family) */
1051 case SIOCSIFBRDADDR: /* Set the broadcast address */
1052 case SIOCSIFDSTADDR: /* Set the destination address */
1053 case SIOCSIFNETMASK: /* Set the netmask for the interface */
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +00001054 ret = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001055 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 goto out;
1057 ret = -EINVAL;
1058 if (sin->sin_family != AF_INET)
1059 goto out;
1060 break;
1061 default:
1062 ret = -EINVAL;
1063 goto out;
1064 }
1065
1066 rtnl_lock();
1067
1068 ret = -ENODEV;
Al Viro03aef172017-07-01 07:53:12 -04001069 dev = __dev_get_by_name(net, ifr->ifr_name);
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001070 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 goto done;
1072
1073 if (colon)
1074 *colon = ':';
1075
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001076 in_dev = __in_dev_get_rtnl(dev);
1077 if (in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (tryaddrmatch) {
1079 /* Matthias Andree */
1080 /* compare label and address (4.4BSD style) */
1081 /* note: we only do this for a limited set of ioctls
1082 and only if the original address family was AF_INET.
1083 This is checked above. */
Florian Westphal2638eb8b2019-05-31 18:27:09 +02001084
1085 for (ifap = &in_dev->ifa_list;
1086 (ifa = rtnl_dereference(*ifap)) != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ifap = &ifa->ifa_next) {
Al Viro03aef172017-07-01 07:53:12 -04001088 if (!strcmp(ifr->ifr_name, ifa->ifa_label) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 sin_orig.sin_addr.s_addr ==
David S. Miller6c91afe2011-03-09 13:27:16 -08001090 ifa->ifa_local) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 break; /* found */
1092 }
1093 }
1094 }
1095 /* we didn't get a match, maybe the application is
1096 4.3BSD-style and passed in junk so we fall back to
1097 comparing just the label */
1098 if (!ifa) {
Florian Westphal2638eb8b2019-05-31 18:27:09 +02001099 for (ifap = &in_dev->ifa_list;
1100 (ifa = rtnl_dereference(*ifap)) != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 ifap = &ifa->ifa_next)
Al Viro03aef172017-07-01 07:53:12 -04001102 if (!strcmp(ifr->ifr_name, ifa->ifa_label))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 break;
1104 }
1105 }
1106
1107 ret = -EADDRNOTAVAIL;
1108 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
1109 goto done;
1110
Stephen Hemminger132adf52007-03-08 20:44:43 -08001111 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 case SIOCGIFADDR: /* Get interface address */
Tonghao Zhang30e948a2018-01-28 03:38:58 -08001113 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 sin->sin_addr.s_addr = ifa->ifa_local;
Al Viro03aef172017-07-01 07:53:12 -04001115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 case SIOCGIFBRDADDR: /* Get the broadcast address */
Tonghao Zhang30e948a2018-01-28 03:38:58 -08001118 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 sin->sin_addr.s_addr = ifa->ifa_broadcast;
Al Viro03aef172017-07-01 07:53:12 -04001120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 case SIOCGIFDSTADDR: /* Get the destination address */
Tonghao Zhang30e948a2018-01-28 03:38:58 -08001123 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 sin->sin_addr.s_addr = ifa->ifa_address;
Al Viro03aef172017-07-01 07:53:12 -04001125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 case SIOCGIFNETMASK: /* Get the netmask for the interface */
Tonghao Zhang30e948a2018-01-28 03:38:58 -08001128 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 sin->sin_addr.s_addr = ifa->ifa_mask;
Al Viro03aef172017-07-01 07:53:12 -04001130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 case SIOCSIFFLAGS:
1133 if (colon) {
1134 ret = -EADDRNOTAVAIL;
1135 if (!ifa)
1136 break;
1137 ret = 0;
Al Viro03aef172017-07-01 07:53:12 -04001138 if (!(ifr->ifr_flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 inet_del_ifa(in_dev, ifap, 1);
1140 break;
1141 }
Petr Machata567c5e12018-12-06 17:05:42 +00001142 ret = dev_change_flags(dev, ifr->ifr_flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 break;
1144
1145 case SIOCSIFADDR: /* Set interface address (and family) */
1146 ret = -EINVAL;
1147 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1148 break;
1149
1150 if (!ifa) {
1151 ret = -ENOBUFS;
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001152 ifa = inet_alloc_ifa();
1153 if (!ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 break;
Xi Wangc7e2e1d2013-01-05 11:19:24 +00001155 INIT_HLIST_NODE(&ifa->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (colon)
Al Viro03aef172017-07-01 07:53:12 -04001157 memcpy(ifa->ifa_label, ifr->ifr_name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 else
1159 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1160 } else {
1161 ret = 0;
1162 if (ifa->ifa_local == sin->sin_addr.s_addr)
1163 break;
1164 inet_del_ifa(in_dev, ifap, 0);
1165 ifa->ifa_broadcast = 0;
Bjorn Mork148f9722008-02-26 18:17:53 -08001166 ifa->ifa_scope = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168
1169 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
1170
1171 if (!(dev->flags & IFF_POINTOPOINT)) {
1172 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
1173 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
1174 if ((dev->flags & IFF_BROADCAST) &&
1175 ifa->ifa_prefixlen < 31)
1176 ifa->ifa_broadcast = ifa->ifa_address |
1177 ~ifa->ifa_mask;
1178 } else {
1179 ifa->ifa_prefixlen = 32;
1180 ifa->ifa_mask = inet_make_mask(32);
1181 }
Jiri Pirko5c766d62013-01-24 09:41:41 +00001182 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 ret = inet_set_ifa(dev, ifa);
1184 break;
1185
1186 case SIOCSIFBRDADDR: /* Set the broadcast address */
1187 ret = 0;
1188 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
1189 inet_del_ifa(in_dev, ifap, 0);
1190 ifa->ifa_broadcast = sin->sin_addr.s_addr;
1191 inet_insert_ifa(ifa);
1192 }
1193 break;
1194
1195 case SIOCSIFDSTADDR: /* Set the destination address */
1196 ret = 0;
1197 if (ifa->ifa_address == sin->sin_addr.s_addr)
1198 break;
1199 ret = -EINVAL;
1200 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1201 break;
1202 ret = 0;
1203 inet_del_ifa(in_dev, ifap, 0);
1204 ifa->ifa_address = sin->sin_addr.s_addr;
1205 inet_insert_ifa(ifa);
1206 break;
1207
1208 case SIOCSIFNETMASK: /* Set the netmask for the interface */
1209
1210 /*
1211 * The mask we set must be legal.
1212 */
1213 ret = -EINVAL;
1214 if (bad_mask(sin->sin_addr.s_addr, 0))
1215 break;
1216 ret = 0;
1217 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
Al Viroa144ea42006-09-28 18:00:55 -07001218 __be32 old_mask = ifa->ifa_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 inet_del_ifa(in_dev, ifap, 0);
1220 ifa->ifa_mask = sin->sin_addr.s_addr;
1221 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
1222
1223 /* See if current broadcast address matches
1224 * with current netmask, then recalculate
1225 * the broadcast address. Otherwise it's a
1226 * funny address, so don't touch it since
1227 * the user seems to know what (s)he's doing...
1228 */
1229 if ((dev->flags & IFF_BROADCAST) &&
1230 (ifa->ifa_prefixlen < 31) &&
1231 (ifa->ifa_broadcast ==
David Engeldcab5e12005-10-21 22:09:16 -05001232 (ifa->ifa_local|~old_mask))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 ifa->ifa_broadcast = (ifa->ifa_local |
1234 ~sin->sin_addr.s_addr);
1235 }
1236 inet_insert_ifa(ifa);
1237 }
1238 break;
1239 }
1240done:
1241 rtnl_unlock();
1242out:
1243 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Arnd Bergmannb0e99d02021-07-22 16:29:01 +02001246int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Herbert Xue5ed6392005-10-03 14:35:55 -07001248 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Florian Westphalef11db32019-05-31 18:27:04 +02001249 const struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 struct ifreq ifr;
1251 int done = 0;
1252
Al Viro36fd6332017-06-26 13:19:16 -04001253 if (WARN_ON(size > sizeof(struct ifreq)))
1254 goto out;
1255
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001256 if (!in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 goto out;
1258
Florian Westphalef11db32019-05-31 18:27:04 +02001259 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (!buf) {
Al Viro36fd6332017-06-26 13:19:16 -04001261 done += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 continue;
1263 }
Al Viro36fd6332017-06-26 13:19:16 -04001264 if (len < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 break;
1266 memset(&ifr, 0, sizeof(struct ifreq));
Dan Carpenter4299c8a2013-07-29 22:15:19 +03001267 strcpy(ifr.ifr_name, ifa->ifa_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
1270 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
1271 ifa->ifa_local;
1272
Al Viro36fd6332017-06-26 13:19:16 -04001273 if (copy_to_user(buf + done, &ifr, size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 done = -EFAULT;
1275 break;
1276 }
Al Viro36fd6332017-06-26 13:19:16 -04001277 len -= size;
1278 done += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280out:
1281 return done;
1282}
1283
Gao Feng8b57fd12017-03-10 12:38:47 +08001284static __be32 in_dev_select_addr(const struct in_device *in_dev,
1285 int scope)
1286{
Florian Westphald519e872019-05-31 18:27:05 +02001287 const struct in_ifaddr *ifa;
1288
1289 in_dev_for_each_ifa_rcu(ifa, in_dev) {
1290 if (ifa->ifa_flags & IFA_F_SECONDARY)
1291 continue;
Gao Feng8b57fd12017-03-10 12:38:47 +08001292 if (ifa->ifa_scope != RT_SCOPE_LINK &&
1293 ifa->ifa_scope <= scope)
1294 return ifa->ifa_local;
Florian Westphald519e872019-05-31 18:27:05 +02001295 }
Gao Feng8b57fd12017-03-10 12:38:47 +08001296
1297 return 0;
1298}
1299
Al Viroa61ced52006-09-26 21:27:54 -07001300__be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Florian Westphald519e872019-05-31 18:27:05 +02001302 const struct in_ifaddr *ifa;
Al Viroa61ced52006-09-26 21:27:54 -07001303 __be32 addr = 0;
Shijie Luod8c444d2019-06-18 15:14:03 +00001304 unsigned char localnet_scope = RT_SCOPE_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 struct in_device *in_dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001306 struct net *net = dev_net(dev);
David Ahern3f2fb9a2016-02-24 11:47:02 -08001307 int master_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07001310 in_dev = __in_dev_get_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (!in_dev)
1312 goto no_in_dev;
1313
Shijie Luod8c444d2019-06-18 15:14:03 +00001314 if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev)))
1315 localnet_scope = RT_SCOPE_LINK;
1316
Florian Westphald519e872019-05-31 18:27:05 +02001317 in_dev_for_each_ifa_rcu(ifa, in_dev) {
1318 if (ifa->ifa_flags & IFA_F_SECONDARY)
1319 continue;
Shijie Luod8c444d2019-06-18 15:14:03 +00001320 if (min(ifa->ifa_scope, localnet_scope) > scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 continue;
1322 if (!dst || inet_ifa_match(dst, ifa)) {
1323 addr = ifa->ifa_local;
1324 break;
1325 }
1326 if (!addr)
1327 addr = ifa->ifa_local;
Florian Westphald519e872019-05-31 18:27:05 +02001328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 if (addr)
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001331 goto out_unlock;
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001332no_in_dev:
David Ahern3f2fb9a2016-02-24 11:47:02 -08001333 master_idx = l3mdev_master_ifindex_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
David Lamparter17b693c2016-02-24 11:47:03 -08001335 /* For VRFs, the VRF device takes the place of the loopback device,
1336 * with addresses on it being preferred. Note in such cases the
1337 * loopback device will be among the devices that fail the master_idx
1338 * equality check in the loop below.
1339 */
1340 if (master_idx &&
1341 (dev = dev_get_by_index_rcu(net, master_idx)) &&
1342 (in_dev = __in_dev_get_rcu(dev))) {
Gao Feng8b57fd12017-03-10 12:38:47 +08001343 addr = in_dev_select_addr(in_dev, scope);
1344 if (addr)
1345 goto out_unlock;
David Lamparter17b693c2016-02-24 11:47:03 -08001346 }
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /* Not loopback addresses on loopback should be preferred
Stephen Hemmingerca9f1fd2015-02-14 13:47:54 -05001349 in this case. It is important that lo is the first interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 in dev_base list.
1351 */
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001352 for_each_netdev_rcu(net, dev) {
David Ahern3f2fb9a2016-02-24 11:47:02 -08001353 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1354 continue;
1355
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001356 in_dev = __in_dev_get_rcu(dev);
1357 if (!in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 continue;
1359
Gao Feng8b57fd12017-03-10 12:38:47 +08001360 addr = in_dev_select_addr(in_dev, scope);
1361 if (addr)
1362 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001364out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return addr;
1367}
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001368EXPORT_SYMBOL(inet_select_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Al Viro60cad5d2006-09-26 22:17:09 -07001370static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
1371 __be32 local, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Shijie Luo650638a2019-06-18 15:14:04 +00001373 unsigned char localnet_scope = RT_SCOPE_HOST;
Florian Westphalef11db32019-05-31 18:27:04 +02001374 const struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07001375 __be32 addr = 0;
Florian Westphalef11db32019-05-31 18:27:04 +02001376 int same = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Shijie Luo650638a2019-06-18 15:14:04 +00001378 if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev)))
1379 localnet_scope = RT_SCOPE_LINK;
1380
Florian Westphalef11db32019-05-31 18:27:04 +02001381 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Shijie Luo650638a2019-06-18 15:14:04 +00001382 unsigned char min_scope = min(ifa->ifa_scope, localnet_scope);
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (!addr &&
1385 (local == ifa->ifa_local || !local) &&
Shijie Luo650638a2019-06-18 15:14:04 +00001386 min_scope <= scope) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 addr = ifa->ifa_local;
1388 if (same)
1389 break;
1390 }
1391 if (!same) {
1392 same = (!local || inet_ifa_match(local, ifa)) &&
1393 (!dst || inet_ifa_match(dst, ifa));
1394 if (same && addr) {
1395 if (local || !dst)
1396 break;
1397 /* Is the selected addr into dst subnet? */
1398 if (inet_ifa_match(addr, ifa))
1399 break;
1400 /* No, then can we use new local src? */
Shijie Luo650638a2019-06-18 15:14:04 +00001401 if (min_scope <= scope) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 addr = ifa->ifa_local;
1403 break;
1404 }
1405 /* search for large dst subnet for addr */
1406 same = 0;
1407 }
1408 }
Florian Westphalef11db32019-05-31 18:27:04 +02001409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001411 return same ? addr : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
1414/*
1415 * Confirm that local IP address exists using wildcards:
Nicolas Dichtelb601fa192013-12-10 15:02:40 +01001416 * - net: netns to check, cannot be NULL
1417 * - in_dev: only on this interface, NULL=any interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 * - dst: only in the same subnet as dst, 0=any dst
1419 * - local: address, 0=autoselect the local address
1420 * - scope: maximum allowed scope value for the local address
1421 */
Nicolas Dichtelb601fa192013-12-10 15:02:40 +01001422__be32 inet_confirm_addr(struct net *net, struct in_device *in_dev,
Denis V. Lunev9bd85e32008-01-14 23:05:55 -08001423 __be32 dst, __be32 local, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Al Viro60cad5d2006-09-26 22:17:09 -07001425 __be32 addr = 0;
Denis V. Lunev9bd85e32008-01-14 23:05:55 -08001426 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Ian Morris00db4122015-04-03 09:17:27 +01001428 if (in_dev)
Denis V. Lunev9bd85e32008-01-14 23:05:55 -08001429 return confirm_addr_indev(in_dev, dst, local, scope);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 rcu_read_lock();
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001432 for_each_netdev_rcu(net, dev) {
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001433 in_dev = __in_dev_get_rcu(dev);
1434 if (in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 addr = confirm_addr_indev(in_dev, dst, local, scope);
1436 if (addr)
1437 break;
1438 }
1439 }
1440 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 return addr;
1443}
Andy Gospodarekeaddcd72012-03-22 16:14:29 +00001444EXPORT_SYMBOL(inet_confirm_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446/*
1447 * Device notifier
1448 */
1449
1450int register_inetaddr_notifier(struct notifier_block *nb)
1451{
Alan Sterne041c682006-03-27 01:16:30 -08001452 return blocking_notifier_chain_register(&inetaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001454EXPORT_SYMBOL(register_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456int unregister_inetaddr_notifier(struct notifier_block *nb)
1457{
Alan Sterne041c682006-03-27 01:16:30 -08001458 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001460EXPORT_SYMBOL(unregister_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Krister Johansen3ad7d242017-06-08 13:12:14 -07001462int register_inetaddr_validator_notifier(struct notifier_block *nb)
1463{
1464 return blocking_notifier_chain_register(&inetaddr_validator_chain, nb);
1465}
1466EXPORT_SYMBOL(register_inetaddr_validator_notifier);
1467
1468int unregister_inetaddr_validator_notifier(struct notifier_block *nb)
1469{
1470 return blocking_notifier_chain_unregister(&inetaddr_validator_chain,
1471 nb);
1472}
1473EXPORT_SYMBOL(unregister_inetaddr_validator_notifier);
1474
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001475/* Rename ifa_labels for a device name change. Make some effort to preserve
1476 * existing alias numbering and to create unique labels if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477*/
1478static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001479{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 struct in_ifaddr *ifa;
1481 int named = 0;
1482
Florian Westphalef11db32019-05-31 18:27:04 +02001483 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001484 char old[IFNAMSIZ], *dot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 memcpy(old, ifa->ifa_label, IFNAMSIZ);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001487 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (named++ == 0)
Thomas Graf573bf472008-06-10 15:40:04 -07001489 goto skip;
Mark McLoughlin44344b22008-01-04 00:56:25 -08001490 dot = strchr(old, ':');
Ian Morris51456b22015-04-03 09:17:26 +01001491 if (!dot) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001492 sprintf(old, ":%d", named);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 dot = old;
1494 }
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001495 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001496 strcat(ifa->ifa_label, dot);
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001497 else
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001498 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
Thomas Graf573bf472008-06-10 15:40:04 -07001499skip:
1500 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001501 }
1502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Ian Campbelld11327ad2011-02-11 07:44:16 +00001504static void inetdev_send_gratuitous_arp(struct net_device *dev,
1505 struct in_device *in_dev)
1506
1507{
Florian Westphalef11db32019-05-31 18:27:04 +02001508 const struct in_ifaddr *ifa;
Ian Campbelld11327ad2011-02-11 07:44:16 +00001509
Florian Westphalef11db32019-05-31 18:27:04 +02001510 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Zoltan Kissb76d0782011-07-24 13:09:30 +00001511 arp_send(ARPOP_REQUEST, ETH_P_ARP,
1512 ifa->ifa_local, dev,
1513 ifa->ifa_local, NULL,
1514 dev->dev_addr, NULL);
1515 }
Ian Campbelld11327ad2011-02-11 07:44:16 +00001516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518/* Called only under RTNL semaphore */
1519
1520static int inetdev_event(struct notifier_block *this, unsigned long event,
1521 void *ptr)
1522{
Jiri Pirko351638e2013-05-28 01:30:21 +00001523 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric Dumazet748e2d92012-08-22 21:50:59 +00001524 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 ASSERT_RTNL();
1527
1528 if (!in_dev) {
Herbert Xu8030f542007-02-22 01:53:47 +09001529 if (event == NETDEV_REGISTER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 in_dev = inetdev_init(dev);
WANG Cong20e61da2014-07-25 15:25:08 -07001531 if (IS_ERR(in_dev))
1532 return notifier_from_errno(PTR_ERR(in_dev));
Eric W. Biederman0cc217e2007-09-26 22:10:06 -07001533 if (dev->flags & IFF_LOOPBACK) {
Herbert Xu42f811b2007-06-04 23:34:44 -07001534 IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1535 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
Herbert Xu8030f542007-02-22 01:53:47 +09001536 }
Breno Leitao06770842008-09-02 17:28:58 -07001537 } else if (event == NETDEV_CHANGEMTU) {
1538 /* Re-enabling IP */
1539 if (inetdev_valid_mtu(dev->mtu))
1540 in_dev = inetdev_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542 goto out;
1543 }
1544
1545 switch (event) {
1546 case NETDEV_REGISTER:
Joe Perches91df42b2012-05-15 14:11:54 +00001547 pr_debug("%s: bug\n", __func__);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001548 RCU_INIT_POINTER(dev->ip_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 break;
1550 case NETDEV_UP:
Breno Leitao06770842008-09-02 17:28:58 -07001551 if (!inetdev_valid_mtu(dev->mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 break;
Eric W. Biederman0cc217e2007-09-26 22:10:06 -07001553 if (dev->flags & IFF_LOOPBACK) {
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001554 struct in_ifaddr *ifa = inet_alloc_ifa();
1555
1556 if (ifa) {
David S. Millerfd23c3b2011-02-18 12:42:28 -08001557 INIT_HLIST_NODE(&ifa->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 ifa->ifa_local =
1559 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1560 ifa->ifa_prefixlen = 8;
1561 ifa->ifa_mask = inet_make_mask(8);
1562 in_dev_hold(in_dev);
1563 ifa->ifa_dev = in_dev;
1564 ifa->ifa_scope = RT_SCOPE_HOST;
1565 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
Jiri Pirko5c766d62013-01-24 09:41:41 +00001566 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME,
1567 INFINITY_LIFE_TIME);
Jiri Pirkodfd15822014-01-07 15:55:45 +01001568 ipv4_devconf_setall(in_dev);
1569 neigh_parms_data_state_setall(in_dev->arp_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 inet_insert_ifa(ifa);
1571 }
1572 }
1573 ip_mc_up(in_dev);
Joe Perchesa8eceea2020-03-12 15:50:22 -07001574 fallthrough;
Stephen Hemmingereefef1c2009-02-01 01:04:33 -08001575 case NETDEV_CHANGEADDR:
Ian Campbelld11327ad2011-02-11 07:44:16 +00001576 if (!IN_DEV_ARP_NOTIFY(in_dev))
1577 break;
Joe Perchesa8eceea2020-03-12 15:50:22 -07001578 fallthrough;
Ian Campbelld11327ad2011-02-11 07:44:16 +00001579 case NETDEV_NOTIFY_PEERS:
Stephen Hemmingera21090c2009-10-07 03:18:17 -07001580 /* Send gratuitous ARP to notify of link change */
Ian Campbelld11327ad2011-02-11 07:44:16 +00001581 inetdev_send_gratuitous_arp(dev, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 break;
1583 case NETDEV_DOWN:
1584 ip_mc_down(in_dev);
1585 break;
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001586 case NETDEV_PRE_TYPE_CHANGE:
Moni Shoua75c78502009-09-15 02:37:40 -07001587 ip_mc_unmap(in_dev);
1588 break;
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001589 case NETDEV_POST_TYPE_CHANGE:
Moni Shoua75c78502009-09-15 02:37:40 -07001590 ip_mc_remap(in_dev);
1591 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 case NETDEV_CHANGEMTU:
Breno Leitao06770842008-09-02 17:28:58 -07001593 if (inetdev_valid_mtu(dev->mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 break;
Breno Leitao06770842008-09-02 17:28:58 -07001595 /* disable IP when MTU is not enough */
Joe Perchesa8eceea2020-03-12 15:50:22 -07001596 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 case NETDEV_UNREGISTER:
1598 inetdev_destroy(in_dev);
1599 break;
1600 case NETDEV_CHANGENAME:
1601 /* Do not notify about label change, this event is
1602 * not interesting to applications using netlink.
1603 */
1604 inetdev_changename(dev, in_dev);
1605
Pavel Emelyanov51602b22007-12-11 02:17:40 -08001606 devinet_sysctl_unregister(in_dev);
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11001607 devinet_sysctl_register(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 break;
1609 }
1610out:
1611 return NOTIFY_DONE;
1612}
1613
1614static struct notifier_block ip_netdev_notifier = {
Jianjun Kong539afed2008-11-03 02:48:48 -08001615 .notifier_call = inetdev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616};
1617
Eric Dumazet40384992012-08-03 21:06:50 +00001618static size_t inet_nlmsg_size(void)
Thomas Graf339bf982006-11-10 14:10:15 -08001619{
1620 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1621 + nla_total_size(4) /* IFA_ADDRESS */
1622 + nla_total_size(4) /* IFA_LOCAL */
1623 + nla_total_size(4) /* IFA_BROADCAST */
Jiri Pirkoad6c8132013-12-08 12:16:10 +01001624 + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
Geert Uytterhoeven63b5f152014-02-05 08:38:25 +01001625 + nla_total_size(4) /* IFA_FLAGS */
David Ahernaf4d7682018-05-27 08:09:57 -07001626 + nla_total_size(4) /* IFA_RT_PRIORITY */
Geert Uytterhoeven63b5f152014-02-05 08:38:25 +01001627 + nla_total_size(sizeof(struct ifa_cacheinfo)); /* IFA_CACHEINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08001628}
1629
Jiri Pirko5c766d62013-01-24 09:41:41 +00001630static inline u32 cstamp_delta(unsigned long cstamp)
1631{
1632 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
1633}
1634
1635static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
1636 unsigned long tstamp, u32 preferred, u32 valid)
1637{
1638 struct ifa_cacheinfo ci;
1639
1640 ci.cstamp = cstamp_delta(cstamp);
1641 ci.tstamp = cstamp_delta(tstamp);
1642 ci.ifa_prefered = preferred;
1643 ci.ifa_valid = valid;
1644
1645 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
1646}
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
Christian Brauner978a46f2018-09-04 21:53:54 +02001649 struct inet_fill_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 struct ifaddrmsg *ifm;
1652 struct nlmsghdr *nlh;
Jiri Pirko5c766d62013-01-24 09:41:41 +00001653 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Christian Brauner978a46f2018-09-04 21:53:54 +02001655 nlh = nlmsg_put(skb, args->portid, args->seq, args->event, sizeof(*ifm),
1656 args->flags);
Ian Morris51456b22015-04-03 09:17:26 +01001657 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08001658 return -EMSGSIZE;
Thomas Graf47f68512006-08-04 23:04:36 -07001659
1660 ifm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 ifm->ifa_family = AF_INET;
1662 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
Jiri Pirko5c766d62013-01-24 09:41:41 +00001663 ifm->ifa_flags = ifa->ifa_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 ifm->ifa_scope = ifa->ifa_scope;
1665 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Christian Brauner978a46f2018-09-04 21:53:54 +02001667 if (args->netnsid >= 0 &&
1668 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
Christian Braunerd3807142018-09-04 21:53:49 +02001669 goto nla_put_failure;
1670
Jiri Pirko5c766d62013-01-24 09:41:41 +00001671 if (!(ifm->ifa_flags & IFA_F_PERMANENT)) {
1672 preferred = ifa->ifa_preferred_lft;
1673 valid = ifa->ifa_valid_lft;
1674 if (preferred != INFINITY_LIFE_TIME) {
1675 long tval = (jiffies - ifa->ifa_tstamp) / HZ;
1676
1677 if (preferred > tval)
1678 preferred -= tval;
1679 else
1680 preferred = 0;
1681 if (valid != INFINITY_LIFE_TIME) {
1682 if (valid > tval)
1683 valid -= tval;
1684 else
1685 valid = 0;
1686 }
1687 }
1688 } else {
1689 preferred = INFINITY_LIFE_TIME;
1690 valid = INFINITY_LIFE_TIME;
1691 }
David S. Millerf3756b72012-04-01 20:39:02 -04001692 if ((ifa->ifa_address &&
Jiri Benc930345e2015-03-29 16:59:25 +02001693 nla_put_in_addr(skb, IFA_ADDRESS, ifa->ifa_address)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001694 (ifa->ifa_local &&
Jiri Benc930345e2015-03-29 16:59:25 +02001695 nla_put_in_addr(skb, IFA_LOCAL, ifa->ifa_local)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001696 (ifa->ifa_broadcast &&
Jiri Benc930345e2015-03-29 16:59:25 +02001697 nla_put_in_addr(skb, IFA_BROADCAST, ifa->ifa_broadcast)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001698 (ifa->ifa_label[0] &&
Jiri Pirko5c766d62013-01-24 09:41:41 +00001699 nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) ||
Jiri Pirkoad6c8132013-12-08 12:16:10 +01001700 nla_put_u32(skb, IFA_FLAGS, ifa->ifa_flags) ||
David Ahernaf4d7682018-05-27 08:09:57 -07001701 (ifa->ifa_rt_priority &&
1702 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->ifa_rt_priority)) ||
Jiri Pirko5c766d62013-01-24 09:41:41 +00001703 put_cacheinfo(skb, ifa->ifa_cstamp, ifa->ifa_tstamp,
1704 preferred, valid))
David S. Millerf3756b72012-04-01 20:39:02 -04001705 goto nla_put_failure;
Thomas Graf47f68512006-08-04 23:04:36 -07001706
Johannes Berg053c0952015-01-16 22:09:00 +01001707 nlmsg_end(skb, nlh);
1708 return 0;
Thomas Graf47f68512006-08-04 23:04:36 -07001709
1710nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001711 nlmsg_cancel(skb, nlh);
1712 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
David Ahernc33078e2018-10-07 20:16:28 -07001715static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
1716 struct inet_fill_args *fillargs,
1717 struct net **tgt_net, struct sock *sk,
David Ahern5fcd2662018-10-19 12:45:29 -07001718 struct netlink_callback *cb)
David Ahernc33078e2018-10-07 20:16:28 -07001719{
David Ahern5fcd2662018-10-19 12:45:29 -07001720 struct netlink_ext_ack *extack = cb->extack;
David Ahernc33078e2018-10-07 20:16:28 -07001721 struct nlattr *tb[IFA_MAX+1];
1722 struct ifaddrmsg *ifm;
1723 int err, i;
1724
1725 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1726 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for address dump request");
1727 return -EINVAL;
1728 }
1729
1730 ifm = nlmsg_data(nlh);
1731 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
1732 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for address dump request");
1733 return -EINVAL;
1734 }
David Ahern5fcd2662018-10-19 12:45:29 -07001735
1736 fillargs->ifindex = ifm->ifa_index;
1737 if (fillargs->ifindex) {
1738 cb->answer_flags |= NLM_F_DUMP_FILTERED;
1739 fillargs->flags |= NLM_F_DUMP_FILTERED;
David Ahernc33078e2018-10-07 20:16:28 -07001740 }
1741
Johannes Berg8cb08172019-04-26 14:07:28 +02001742 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
1743 ifa_ipv4_policy, extack);
David Ahernc33078e2018-10-07 20:16:28 -07001744 if (err < 0)
1745 return err;
1746
1747 for (i = 0; i <= IFA_MAX; ++i) {
1748 if (!tb[i])
1749 continue;
1750
1751 if (i == IFA_TARGET_NETNSID) {
1752 struct net *net;
1753
1754 fillargs->netnsid = nla_get_s32(tb[i]);
1755
1756 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
1757 if (IS_ERR(net)) {
Bjørn Morkbf4cc402018-10-25 21:18:25 +02001758 fillargs->netnsid = -1;
David Ahernc33078e2018-10-07 20:16:28 -07001759 NL_SET_ERR_MSG(extack, "ipv4: Invalid target network namespace id");
1760 return PTR_ERR(net);
1761 }
1762 *tgt_net = net;
1763 } else {
1764 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in dump request");
1765 return -EINVAL;
1766 }
1767 }
1768
1769 return 0;
1770}
1771
David Ahern1c98eca2018-10-19 12:45:27 -07001772static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb,
1773 struct netlink_callback *cb, int s_ip_idx,
1774 struct inet_fill_args *fillargs)
1775{
1776 struct in_ifaddr *ifa;
1777 int ip_idx = 0;
1778 int err;
1779
Florian Westphald3e6e282019-06-03 22:41:44 +02001780 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Florian Westphalef11db32019-05-31 18:27:04 +02001781 if (ip_idx < s_ip_idx) {
1782 ip_idx++;
David Ahern1c98eca2018-10-19 12:45:27 -07001783 continue;
Florian Westphalef11db32019-05-31 18:27:04 +02001784 }
David Ahern1c98eca2018-10-19 12:45:27 -07001785 err = inet_fill_ifaddr(skb, ifa, fillargs);
1786 if (err < 0)
1787 goto done;
1788
1789 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Florian Westphalef11db32019-05-31 18:27:04 +02001790 ip_idx++;
David Ahern1c98eca2018-10-19 12:45:27 -07001791 }
1792 err = 0;
1793
1794done:
1795 cb->args[2] = ip_idx;
1796
1797 return err;
1798}
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1801{
David Ahernc33078e2018-10-07 20:16:28 -07001802 const struct nlmsghdr *nlh = cb->nlh;
Christian Brauner978a46f2018-09-04 21:53:54 +02001803 struct inet_fill_args fillargs = {
1804 .portid = NETLINK_CB(cb->skb).portid,
David Ahernc33078e2018-10-07 20:16:28 -07001805 .seq = nlh->nlmsg_seq,
Christian Brauner978a46f2018-09-04 21:53:54 +02001806 .event = RTM_NEWADDR,
1807 .flags = NLM_F_MULTI,
1808 .netnsid = -1,
1809 };
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001810 struct net *net = sock_net(skb->sk);
Christian Braunerd3807142018-09-04 21:53:49 +02001811 struct net *tgt_net = net;
Eric Dumazeteec4df92009-11-12 07:44:25 +00001812 int h, s_h;
1813 int idx, s_idx;
David Ahern1c98eca2018-10-19 12:45:27 -07001814 int s_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 struct net_device *dev;
1816 struct in_device *in_dev;
Eric Dumazeteec4df92009-11-12 07:44:25 +00001817 struct hlist_head *head;
David Ahernd7e38612018-10-24 12:58:59 -07001818 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Eric Dumazeteec4df92009-11-12 07:44:25 +00001820 s_h = cb->args[0];
1821 s_idx = idx = cb->args[1];
David Ahern1c98eca2018-10-19 12:45:27 -07001822 s_ip_idx = cb->args[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
David Ahernc33078e2018-10-07 20:16:28 -07001824 if (cb->strict_check) {
David Ahernc33078e2018-10-07 20:16:28 -07001825 err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
David Ahern5fcd2662018-10-19 12:45:29 -07001826 skb->sk, cb);
David Ahernc33078e2018-10-07 20:16:28 -07001827 if (err < 0)
David Ahernd7e38612018-10-24 12:58:59 -07001828 goto put_tgt_net;
David Ahern5fcd2662018-10-19 12:45:29 -07001829
David Ahernd7e38612018-10-24 12:58:59 -07001830 err = 0;
David Ahern5fcd2662018-10-19 12:45:29 -07001831 if (fillargs.ifindex) {
1832 dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
David Ahernd7e38612018-10-24 12:58:59 -07001833 if (!dev) {
1834 err = -ENODEV;
1835 goto put_tgt_net;
1836 }
David Ahern5fcd2662018-10-19 12:45:29 -07001837
1838 in_dev = __in_dev_get_rtnl(dev);
1839 if (in_dev) {
1840 err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx,
1841 &fillargs);
1842 }
1843 goto put_tgt_net;
1844 }
Christian Braunerd3807142018-09-04 21:53:49 +02001845 }
1846
Eric Dumazeteec4df92009-11-12 07:44:25 +00001847 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1848 idx = 0;
Christian Braunerd3807142018-09-04 21:53:49 +02001849 head = &tgt_net->dev_index_head[h];
Eric Dumazeteec4df92009-11-12 07:44:25 +00001850 rcu_read_lock();
Christian Braunerd3807142018-09-04 21:53:49 +02001851 cb->seq = atomic_read(&tgt_net->ipv4.dev_addr_genid) ^
1852 tgt_net->dev_base_seq;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001853 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazeteec4df92009-11-12 07:44:25 +00001854 if (idx < s_idx)
1855 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07001856 if (h > s_h || idx > s_idx)
Eric Dumazeteec4df92009-11-12 07:44:25 +00001857 s_ip_idx = 0;
1858 in_dev = __in_dev_get_rcu(dev);
1859 if (!in_dev)
1860 goto cont;
1861
David Ahern1c98eca2018-10-19 12:45:27 -07001862 err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx,
1863 &fillargs);
1864 if (err < 0) {
1865 rcu_read_unlock();
1866 goto done;
Eric Dumazeteec4df92009-11-12 07:44:25 +00001867 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001868cont:
Eric Dumazeteec4df92009-11-12 07:44:25 +00001869 idx++;
1870 }
1871 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873
1874done:
Eric Dumazeteec4df92009-11-12 07:44:25 +00001875 cb->args[0] = h;
1876 cb->args[1] = idx;
David Ahern5fcd2662018-10-19 12:45:29 -07001877put_tgt_net:
Christian Brauner978a46f2018-09-04 21:53:54 +02001878 if (fillargs.netnsid >= 0)
Christian Braunerd3807142018-09-04 21:53:49 +02001879 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Arthur Gautier7c1e8a32018-12-31 02:10:58 +00001881 return skb->len ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Jianjun Kong539afed2008-11-03 02:48:48 -08001884static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001885 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Christian Brauner978a46f2018-09-04 21:53:54 +02001887 struct inet_fill_args fillargs = {
1888 .portid = portid,
1889 .seq = nlh ? nlh->nlmsg_seq : 0,
1890 .event = event,
1891 .flags = 0,
1892 .netnsid = -1,
1893 };
Thomas Graf47f68512006-08-04 23:04:36 -07001894 struct sk_buff *skb;
Thomas Grafd6062cb2006-08-15 00:33:59 -07001895 int err = -ENOBUFS;
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -08001896 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001898 net = dev_net(ifa->ifa_dev->dev);
Thomas Graf339bf982006-11-10 14:10:15 -08001899 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001900 if (!skb)
Thomas Grafd6062cb2006-08-15 00:33:59 -07001901 goto errout;
1902
Christian Brauner978a46f2018-09-04 21:53:54 +02001903 err = inet_fill_ifaddr(skb, ifa, &fillargs);
Patrick McHardy26932562007-01-31 23:16:40 -08001904 if (err < 0) {
1905 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1906 WARN_ON(err == -EMSGSIZE);
1907 kfree_skb(skb);
1908 goto errout;
1909 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001910 rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001911 return;
Thomas Grafd6062cb2006-08-15 00:33:59 -07001912errout:
1913 if (err < 0)
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -08001914 rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001917static size_t inet_get_link_af_size(const struct net_device *dev,
1918 u32 ext_filter_mask)
Thomas Graf9f0f7272010-11-16 04:32:48 +00001919{
Eric Dumazet1fc19af2011-05-09 20:55:03 -07001920 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
Thomas Graf9f0f7272010-11-16 04:32:48 +00001921
1922 if (!in_dev)
1923 return 0;
1924
1925 return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
1926}
1927
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001928static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
1929 u32 ext_filter_mask)
Thomas Graf9f0f7272010-11-16 04:32:48 +00001930{
Eric Dumazet1fc19af2011-05-09 20:55:03 -07001931 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
Thomas Graf9f0f7272010-11-16 04:32:48 +00001932 struct nlattr *nla;
1933 int i;
1934
1935 if (!in_dev)
1936 return -ENODATA;
1937
1938 nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
Ian Morris51456b22015-04-03 09:17:26 +01001939 if (!nla)
Thomas Graf9f0f7272010-11-16 04:32:48 +00001940 return -EMSGSIZE;
1941
1942 for (i = 0; i < IPV4_DEVCONF_MAX; i++)
1943 ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
1944
1945 return 0;
1946}
1947
1948static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
1949 [IFLA_INET_CONF] = { .type = NLA_NESTED },
1950};
1951
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001952static int inet_validate_link_af(const struct net_device *dev,
1953 const struct nlattr *nla)
Thomas Graf9f0f7272010-11-16 04:32:48 +00001954{
Thomas Graf9f0f7272010-11-16 04:32:48 +00001955 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1956 int err, rem;
1957
Cong Wanga1002432021-05-08 11:00:33 -07001958 if (dev && !__in_dev_get_rtnl(dev))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001959 return -EAFNOSUPPORT;
Thomas Graf9f0f7272010-11-16 04:32:48 +00001960
Johannes Berg8cb08172019-04-26 14:07:28 +02001961 err = nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla,
1962 inet_af_policy, NULL);
Thomas Graf9f0f7272010-11-16 04:32:48 +00001963 if (err < 0)
1964 return err;
1965
1966 if (tb[IFLA_INET_CONF]) {
1967 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
1968 int cfgid = nla_type(a);
1969
1970 if (nla_len(a) < 4)
1971 return -EINVAL;
1972
1973 if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
1974 return -EINVAL;
1975 }
1976 }
1977
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001978 return 0;
1979}
1980
Stephen Hemminger3583a4e2021-04-07 08:59:12 -07001981static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla,
1982 struct netlink_ext_ack *extack)
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001983{
Cong Wanga1002432021-05-08 11:00:33 -07001984 struct in_device *in_dev = __in_dev_get_rtnl(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)
Zheng Yongjun5ac6b192021-06-08 09:53:15 +08001992 return -EINVAL;
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001993
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;
Yang Yang8292d7f2021-07-19 18:43:28 -07002427 struct net *net = ctl->extra2;
2428 int ret;
2429
2430 if (write && !ns_capable(net->user_ns, CAP_NET_ADMIN))
2431 return -EPERM;
2432
2433 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 if (write && *valp != val) {
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002436 if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
Eric W. Biederman88af1822010-02-19 13:22:59 +00002437 if (!rtnl_trylock()) {
2438 /* Restore the original values before restarting */
2439 *valp = val;
2440 *ppos = pos;
Eric W. Biederman9b8adb52009-05-13 16:59:21 +00002441 return restart_syscall();
Eric W. Biederman88af1822010-02-19 13:22:59 +00002442 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002443 if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
2444 inet_forward_change(net);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002445 } else {
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002446 struct ipv4_devconf *cnf = ctl->extra1;
2447 struct in_device *idev =
2448 container_of(cnf, struct in_device, cnf);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002449 if (*valp)
2450 dev_disable_lro(idev->dev);
David Ahern3b022862017-03-28 14:28:02 -07002451 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002452 NETCONFA_FORWARDING,
2453 idev->dev->ifindex,
2454 cnf);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002455 }
2456 rtnl_unlock();
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00002457 rt_cache_flush(net);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002458 } else
David Ahern3b022862017-03-28 14:28:02 -07002459 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2460 NETCONFA_FORWARDING,
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002461 NETCONFA_IFINDEX_DEFAULT,
2462 net->ipv4.devconf_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
2464
2465 return ret;
2466}
2467
Joe Perchesfe2c6332013-06-11 23:04:25 -07002468static int ipv4_doint_and_flush(struct ctl_table *ctl, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02002469 void *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 int *valp = ctl->data;
2472 int val = *valp;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002473 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -07002474 struct net *net = ctl->extra2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 if (write && *valp != val)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00002477 rt_cache_flush(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
2479 return ret;
2480}
2481
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002482#define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
Herbert Xu42f811b2007-06-04 23:34:44 -07002483 { \
Herbert Xu42f811b2007-06-04 23:34:44 -07002484 .procname = name, \
2485 .data = ipv4_devconf.data + \
Eric W. Biederman02291682010-02-14 03:25:51 +00002486 IPV4_DEVCONF_ ## attr - 1, \
Herbert Xu42f811b2007-06-04 23:34:44 -07002487 .maxlen = sizeof(int), \
2488 .mode = mval, \
2489 .proc_handler = proc, \
Herbert Xu31be3082007-06-04 23:35:37 -07002490 .extra1 = &ipv4_devconf, \
Herbert Xu42f811b2007-06-04 23:34:44 -07002491 }
2492
2493#define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002494 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
Herbert Xu42f811b2007-06-04 23:34:44 -07002495
2496#define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002497 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
Herbert Xu42f811b2007-06-04 23:34:44 -07002498
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002499#define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
2500 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
Herbert Xu42f811b2007-06-04 23:34:44 -07002501
2502#define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002503 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
Herbert Xu42f811b2007-06-04 23:34:44 -07002504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505static struct devinet_sysctl_table {
2506 struct ctl_table_header *sysctl_header;
Eric W. Biederman02291682010-02-14 03:25:51 +00002507 struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508} devinet_sysctl = {
2509 .devinet_vars = {
Herbert Xu42f811b2007-06-04 23:34:44 -07002510 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002511 devinet_sysctl_forward),
Herbert Xu42f811b2007-06-04 23:34:44 -07002512 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
Xin Long5cbf7772018-07-27 16:37:28 +08002513 DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"),
Herbert Xu42f811b2007-06-04 23:34:44 -07002514
2515 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
2516 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
2517 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
2518 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
2519 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
2520 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
2521 "accept_source_route"),
Patrick McHardy8153a102009-12-03 01:25:58 +00002522 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
Jamal Hadi Salim28f6aee2009-12-25 17:30:22 -08002523 DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
Herbert Xu42f811b2007-06-04 23:34:44 -07002524 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
2525 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
2526 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
2527 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
2528 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
2529 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
2530 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
2531 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
2532 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
Stephen Hemmingereefef1c2009-02-01 01:04:33 -08002533 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
Jesper Dangaard Brouer65324142010-01-05 05:50:47 +00002534 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
William Manley5c6fe012013-08-06 19:03:14 +01002535 DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION,
2536 "force_igmp_version"),
William Manley26900482013-08-06 19:03:15 +01002537 DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL,
2538 "igmpv2_unsolicited_report_interval"),
2539 DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL,
2540 "igmpv3_unsolicited_report_interval"),
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04002541 DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN,
2542 "ignore_routes_with_linkdown"),
Johannes Berg97daf332016-02-04 13:31:18 +01002543 DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP,
2544 "drop_gratuitous_arp"),
Herbert Xu42f811b2007-06-04 23:34:44 -07002545
2546 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
2547 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
Herbert Xu42f811b2007-06-04 23:34:44 -07002548 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
2549 "promote_secondaries"),
Thomas Grafd0daebc32012-06-12 00:44:01 +00002550 DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET,
2551 "route_localnet"),
Johannes Berg12b74df2016-02-04 13:31:17 +01002552 DEVINET_SYSCTL_FLUSHING_ENTRY(DROP_UNICAST_IN_L2_MULTICAST,
2553 "drop_unicast_in_l2_multicast"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555};
2556
Pavel Emelyanovea40b322007-12-16 13:30:07 -08002557static int __devinet_sysctl_register(struct net *net, char *dev_name,
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002558 int ifindex, struct ipv4_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
2560 int i;
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002561 struct devinet_sysctl_table *t;
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002562 char path[sizeof("net/ipv4/conf/") + IFNAMSIZ];
Pavel Emelyanovbfada692007-12-02 00:57:08 +11002563
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002564 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (!t)
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002566 goto out;
2567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
2569 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
Herbert Xu31be3082007-06-04 23:35:37 -07002570 t->devinet_vars[i].extra1 = p;
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002571 t->devinet_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
2573
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002574 snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002576 t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (!t->sysctl_header)
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002578 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 p->sysctl = t;
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002581
David Ahern3b022862017-03-28 14:28:02 -07002582 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
2583 ifindex, p);
Pavel Emelyanovea40b322007-12-16 13:30:07 -08002584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002586free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 kfree(t);
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002588out:
Pavel Emelyanovea40b322007-12-16 13:30:07 -08002589 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
David Ahernb5c96412017-03-28 14:28:03 -07002592static void __devinet_sysctl_unregister(struct net *net,
2593 struct ipv4_devconf *cnf, int ifindex)
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002594{
2595 struct devinet_sysctl_table *t = cnf->sysctl;
2596
David Ahernb5c96412017-03-28 14:28:03 -07002597 if (t) {
2598 cnf->sysctl = NULL;
2599 unregister_net_sysctl_table(t->sysctl_header);
2600 kfree(t);
2601 }
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002602
David Ahernb5c96412017-03-28 14:28:03 -07002603 inet_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002604}
2605
WANG Cong20e61da2014-07-25 15:25:08 -07002606static int devinet_sysctl_register(struct in_device *idev)
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11002607{
WANG Cong20e61da2014-07-25 15:25:08 -07002608 int err;
2609
2610 if (!sysctl_dev_name_is_allowed(idev->dev->name))
2611 return -EINVAL;
2612
2613 err = neigh_sysctl_register(idev->dev, idev->arp_parms, NULL);
2614 if (err)
2615 return err;
2616 err = __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002617 idev->dev->ifindex, &idev->cnf);
WANG Cong20e61da2014-07-25 15:25:08 -07002618 if (err)
2619 neigh_sysctl_unregister(idev->arp_parms);
2620 return err;
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11002621}
2622
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002623static void devinet_sysctl_unregister(struct in_device *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
David Ahernb5c96412017-03-28 14:28:03 -07002625 struct net *net = dev_net(idev->dev);
2626
2627 __devinet_sysctl_unregister(net, &idev->cnf, idev->dev->ifindex);
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002628 neigh_sysctl_unregister(idev->arp_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002631static struct ctl_table ctl_forward_entry[] = {
2632 {
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002633 .procname = "ip_forward",
2634 .data = &ipv4_devconf.data[
Eric W. Biederman02291682010-02-14 03:25:51 +00002635 IPV4_DEVCONF_FORWARDING - 1],
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002636 .maxlen = sizeof(int),
2637 .mode = 0644,
2638 .proc_handler = devinet_sysctl_forward,
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002639 .extra1 = &ipv4_devconf,
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002640 .extra2 = &init_net,
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002641 },
2642 { },
2643};
Eric Dumazet2a75de02008-01-05 23:08:49 -08002644#endif
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002645
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002646static __net_init int devinet_init_net(struct net *net)
2647{
2648 int err;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002649 struct ipv4_devconf *all, *dflt;
Eric Dumazet2a75de02008-01-05 23:08:49 -08002650#ifdef CONFIG_SYSCTL
Cong Wang856c3952019-01-17 23:27:11 -08002651 struct ctl_table *tbl;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002652 struct ctl_table_header *forw_hdr;
Eric Dumazet2a75de02008-01-05 23:08:49 -08002653#endif
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002654
2655 err = -ENOMEM;
Cong Wang856c3952019-01-17 23:27:11 -08002656 all = kmemdup(&ipv4_devconf, sizeof(ipv4_devconf), GFP_KERNEL);
2657 if (!all)
2658 goto err_alloc_all;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002659
Cong Wang856c3952019-01-17 23:27:11 -08002660 dflt = kmemdup(&ipv4_devconf_dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
2661 if (!dflt)
2662 goto err_alloc_dflt;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002663
Eric Dumazet2a75de02008-01-05 23:08:49 -08002664#ifdef CONFIG_SYSCTL
Cong Wang856c3952019-01-17 23:27:11 -08002665 tbl = kmemdup(ctl_forward_entry, sizeof(ctl_forward_entry), GFP_KERNEL);
2666 if (!tbl)
2667 goto err_alloc_ctl;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002668
Cong Wang856c3952019-01-17 23:27:11 -08002669 tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
2670 tbl[0].extra1 = all;
2671 tbl[0].extra2 = net;
Eric Dumazet2a75de02008-01-05 23:08:49 -08002672#endif
Cong Wang856c3952019-01-17 23:27:11 -08002673
Nicolas Dichtel9efd6a32020-05-13 15:58:43 +02002674 if (!net_eq(net, &init_net)) {
2675 if (IS_ENABLED(CONFIG_SYSCTL) &&
2676 sysctl_devconf_inherit_init_net == 3) {
2677 /* copy from the current netns */
2678 memcpy(all, current->nsproxy->net_ns->ipv4.devconf_all,
2679 sizeof(ipv4_devconf));
2680 memcpy(dflt,
2681 current->nsproxy->net_ns->ipv4.devconf_dflt,
2682 sizeof(ipv4_devconf_dflt));
2683 } else if (!IS_ENABLED(CONFIG_SYSCTL) ||
2684 sysctl_devconf_inherit_init_net != 2) {
2685 /* inherit == 0 or 1: copy from init_net */
2686 memcpy(all, init_net.ipv4.devconf_all,
2687 sizeof(ipv4_devconf));
2688 memcpy(dflt, init_net.ipv4.devconf_dflt,
2689 sizeof(ipv4_devconf_dflt));
2690 }
2691 /* else inherit == 2: use compiled values */
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002692 }
2693
2694#ifdef CONFIG_SYSCTL
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002695 err = __devinet_sysctl_register(net, "all", NETCONFA_IFINDEX_ALL, all);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002696 if (err < 0)
2697 goto err_reg_all;
2698
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002699 err = __devinet_sysctl_register(net, "default",
2700 NETCONFA_IFINDEX_DEFAULT, dflt);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002701 if (err < 0)
2702 goto err_reg_dflt;
2703
2704 err = -ENOMEM;
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002705 forw_hdr = register_net_sysctl(net, "net/ipv4", tbl);
Ian Morris51456b22015-04-03 09:17:26 +01002706 if (!forw_hdr)
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002707 goto err_reg_ctl;
Eric Dumazet2a75de02008-01-05 23:08:49 -08002708 net->ipv4.forw_hdr = forw_hdr;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002709#endif
2710
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002711 net->ipv4.devconf_all = all;
2712 net->ipv4.devconf_dflt = dflt;
2713 return 0;
2714
2715#ifdef CONFIG_SYSCTL
2716err_reg_ctl:
David Ahernb5c96412017-03-28 14:28:03 -07002717 __devinet_sysctl_unregister(net, dflt, NETCONFA_IFINDEX_DEFAULT);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002718err_reg_dflt:
David Ahernb5c96412017-03-28 14:28:03 -07002719 __devinet_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002720err_reg_all:
Cong Wang856c3952019-01-17 23:27:11 -08002721 kfree(tbl);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002722err_alloc_ctl:
Eric Dumazet2a75de02008-01-05 23:08:49 -08002723#endif
Cong Wang856c3952019-01-17 23:27:11 -08002724 kfree(dflt);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002725err_alloc_dflt:
Cong Wang856c3952019-01-17 23:27:11 -08002726 kfree(all);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002727err_alloc_all:
2728 return err;
2729}
2730
2731static __net_exit void devinet_exit_net(struct net *net)
2732{
Eric Dumazet2a75de02008-01-05 23:08:49 -08002733#ifdef CONFIG_SYSCTL
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002734 struct ctl_table *tbl;
2735
2736 tbl = net->ipv4.forw_hdr->ctl_table_arg;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002737 unregister_net_sysctl_table(net->ipv4.forw_hdr);
David Ahernb5c96412017-03-28 14:28:03 -07002738 __devinet_sysctl_unregister(net, net->ipv4.devconf_dflt,
2739 NETCONFA_IFINDEX_DEFAULT);
2740 __devinet_sysctl_unregister(net, net->ipv4.devconf_all,
2741 NETCONFA_IFINDEX_ALL);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002742 kfree(tbl);
Eric Dumazet2a75de02008-01-05 23:08:49 -08002743#endif
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002744 kfree(net->ipv4.devconf_dflt);
2745 kfree(net->ipv4.devconf_all);
2746}
2747
2748static __net_initdata struct pernet_operations devinet_ops = {
2749 .init = devinet_init_net,
2750 .exit = devinet_exit_net,
2751};
2752
Daniel Borkmann207895fd32015-01-29 12:15:03 +01002753static struct rtnl_af_ops inet_af_ops __read_mostly = {
Thomas Graf9f0f7272010-11-16 04:32:48 +00002754 .family = AF_INET,
2755 .fill_link_af = inet_fill_link_af,
2756 .get_link_af_size = inet_get_link_af_size,
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002757 .validate_link_af = inet_validate_link_af,
2758 .set_link_af = inet_set_link_af,
Thomas Graf9f0f7272010-11-16 04:32:48 +00002759};
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761void __init devinet_init(void)
2762{
David S. Millerfd23c3b2011-02-18 12:42:28 -08002763 int i;
2764
2765 for (i = 0; i < IN4_ADDR_HSIZE; i++)
2766 INIT_HLIST_HEAD(&inet_addr_lst[i]);
2767
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002768 register_pernet_subsys(&devinet_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 register_netdevice_notifier(&ip_netdev_notifier);
Thomas Graf63f34442007-03-22 11:55:17 -07002770
viresh kumar906e0732014-01-22 12:23:32 +05302771 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
Jiri Pirko5c766d62013-01-24 09:41:41 +00002772
Thomas Graf9f0f7272010-11-16 04:32:48 +00002773 rtnl_af_register(&inet_af_ops);
2774
Florian Westphalb97bac62017-08-09 20:41:48 +02002775 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, 0);
2776 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, 0);
2777 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, 0);
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002778 rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf,
Florian Westphalb97bac62017-08-09 20:41:48 +02002779 inet_netconf_dump_devconf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}