blob: 30fa42f5997dbc942b03d5b71a172b041b5238cf [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * NET3 IP device support routines.
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Derived from the IP parts of dev.c 1.0.19
Jesper Juhl02c30a82005-05-05 16:16:16 -07006 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
8 * Mark Evans, <evansmp@uhura.aston.ac.uk>
9 *
10 * Additional Authors:
11 * Alan Cox, <gw4pts@gw4pts.ampr.org>
12 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13 *
14 * Changes:
15 * Alexey Kuznetsov: pa_* fields are replaced with ifaddr
16 * lists.
17 * Cyrus Durgin: updated for kmod
18 * Matthias Andree: in devinet_ioctl, compare label and
19 * address (4.4BSD alias style support),
20 * fall back to comparing just the label
21 * if no match found.
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080025#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/types.h>
30#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010031#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/string.h>
33#include <linux/mm.h>
34#include <linux/socket.h>
35#include <linux/sockios.h>
36#include <linux/in.h>
37#include <linux/errno.h>
38#include <linux/interrupt.h>
Thomas Graf18237302006-08-04 23:04:54 -070039#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_ether.h>
41#include <linux/inet.h>
42#include <linux/netdevice.h>
43#include <linux/etherdevice.h>
44#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/init.h>
46#include <linux/notifier.h>
47#include <linux/inetdevice.h>
48#include <linux/igmp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
David S. Millerfd23c3b2011-02-18 12:42:28 -080050#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifdef CONFIG_SYSCTL
52#include <linux/sysctl.h>
53#endif
54#include <linux/kmod.h>
Nicolas Dichteledc9e742012-10-25 22:28:52 +000055#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020057#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <net/ip.h>
59#include <net/route.h>
60#include <net/ip_fib.h>
Thomas Graf63f34442007-03-22 11:55:17 -070061#include <net/rtnetlink.h>
Pavel Emelyanov752d14d2007-12-16 13:31:47 -080062#include <net/net_namespace.h>
Jiri Pirko5c766d62013-01-24 09:41:41 +000063#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Matteo Croce2e605462019-07-01 19:01:55 +020065#define IPV6ONLY_FLAGS \
66 (IFA_F_NODAD | IFA_F_OPTIMISTIC | IFA_F_DADFAILED | \
67 IFA_F_HOMEADDRESS | IFA_F_TENTATIVE | \
68 IFA_F_MANAGETEMPADDR | IFA_F_STABLE_PRIVACY)
69
Adrian Bunk0027ba82008-01-31 17:17:31 -080070static struct ipv4_devconf ipv4_devconf = {
Herbert Xu42f811b2007-06-04 23:34:44 -070071 .data = {
Eric W. Biederman02291682010-02-14 03:25:51 +000072 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
73 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
74 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
75 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
William Manley26900482013-08-06 19:03:15 +010076 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
77 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/,
Herbert Xu42f811b2007-06-04 23:34:44 -070078 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
81static struct ipv4_devconf ipv4_devconf_dflt = {
Herbert Xu42f811b2007-06-04 23:34:44 -070082 .data = {
Eric W. Biederman02291682010-02-14 03:25:51 +000083 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1,
84 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1,
85 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1,
86 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1,
87 [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1,
William Manley26900482013-08-06 19:03:15 +010088 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/,
89 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/,
Herbert Xu42f811b2007-06-04 23:34:44 -070090 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -080093#define IPV4_DEVCONF_DFLT(net, attr) \
94 IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
Herbert Xu42f811b2007-06-04 23:34:44 -070095
Patrick McHardyef7c79e2007-06-05 12:38:30 -070096static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
Thomas Graf5c753972006-08-04 23:03:53 -070097 [IFA_LOCAL] = { .type = NLA_U32 },
98 [IFA_ADDRESS] = { .type = NLA_U32 },
99 [IFA_BROADCAST] = { .type = NLA_U32 },
Thomas Graf5176f912006-08-26 20:13:18 -0700100 [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
Jiri Pirko5c766d62013-01-24 09:41:41 +0000101 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirkoad6c8132013-12-08 12:16:10 +0100102 [IFA_FLAGS] = { .type = NLA_U32 },
David Ahernaf4d7682018-05-27 08:09:57 -0700103 [IFA_RT_PRIORITY] = { .type = NLA_U32 },
Christian Braunerd3807142018-09-04 21:53:49 +0200104 [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
Thomas Graf5c753972006-08-04 23:03:53 -0700105};
106
Christian Brauner978a46f2018-09-04 21:53:54 +0200107struct inet_fill_args {
108 u32 portid;
109 u32 seq;
110 int event;
111 unsigned int flags;
112 int netnsid;
David Ahern5fcd2662018-10-19 12:45:29 -0700113 int ifindex;
Christian Brauner978a46f2018-09-04 21:53:54 +0200114};
115
Eric Dumazet40384992012-08-03 21:06:50 +0000116#define IN4_ADDR_HSIZE_SHIFT 8
117#define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT)
118
David S. Millerfd23c3b2011-02-18 12:42:28 -0800119static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE];
David S. Millerfd23c3b2011-02-18 12:42:28 -0800120
Eric Dumazet6eada012015-03-18 14:05:33 -0700121static u32 inet_addr_hash(const struct net *net, __be32 addr)
David S. Millerfd23c3b2011-02-18 12:42:28 -0800122{
Eric Dumazet40384992012-08-03 21:06:50 +0000123 u32 val = (__force u32) addr ^ net_hash_mix(net);
David S. Millerfd23c3b2011-02-18 12:42:28 -0800124
Eric Dumazet40384992012-08-03 21:06:50 +0000125 return hash_32(val, IN4_ADDR_HSIZE_SHIFT);
David S. Millerfd23c3b2011-02-18 12:42:28 -0800126}
127
128static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa)
129{
Eric Dumazet40384992012-08-03 21:06:50 +0000130 u32 hash = inet_addr_hash(net, ifa->ifa_local);
David S. Millerfd23c3b2011-02-18 12:42:28 -0800131
WANG Cong32a4be42014-05-06 11:15:56 -0700132 ASSERT_RTNL();
David S. Millerfd23c3b2011-02-18 12:42:28 -0800133 hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]);
David S. Millerfd23c3b2011-02-18 12:42:28 -0800134}
135
136static void inet_hash_remove(struct in_ifaddr *ifa)
137{
WANG Cong32a4be42014-05-06 11:15:56 -0700138 ASSERT_RTNL();
David S. Millerfd23c3b2011-02-18 12:42:28 -0800139 hlist_del_init_rcu(&ifa->hash);
David S. Millerfd23c3b2011-02-18 12:42:28 -0800140}
141
David S. Miller9435eb12011-02-18 12:43:09 -0800142/**
143 * __ip_dev_find - find the first device with a given source address.
144 * @net: the net namespace
145 * @addr: the source address
146 * @devref: if true, take a reference on the found device
147 *
148 * If a caller uses devref=false, it should be protected by RCU, or RTNL
149 */
150struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
151{
David S. Miller9435eb12011-02-18 12:43:09 -0800152 struct net_device *result = NULL;
153 struct in_ifaddr *ifa;
David S. Miller9435eb12011-02-18 12:43:09 -0800154
155 rcu_read_lock();
Paolo Abeni6e617de2017-09-20 18:26:53 +0200156 ifa = inet_lookup_ifaddr_rcu(net, addr);
157 if (!ifa) {
David S. Miller406b6f92011-03-22 21:56:23 -0700158 struct flowi4 fl4 = { .daddr = addr };
159 struct fib_result res = { 0 };
160 struct fib_table *local;
161
162 /* Fallback to FIB local table so that communication
163 * over loopback subnets work.
164 */
165 local = fib_get_table(net, RT_TABLE_LOCAL);
166 if (local &&
167 !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) &&
168 res.type == RTN_LOCAL)
169 result = FIB_RES_DEV(res);
Paolo Abeni6e617de2017-09-20 18:26:53 +0200170 } else {
171 result = ifa->ifa_dev->dev;
David S. Miller406b6f92011-03-22 21:56:23 -0700172 }
David S. Miller9435eb12011-02-18 12:43:09 -0800173 if (result && devref)
174 dev_hold(result);
175 rcu_read_unlock();
176 return result;
177}
178EXPORT_SYMBOL(__ip_dev_find);
179
Paolo Abeni6e617de2017-09-20 18:26:53 +0200180/* called under RCU lock */
181struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr)
182{
183 u32 hash = inet_addr_hash(net, addr);
184 struct in_ifaddr *ifa;
185
186 hlist_for_each_entry_rcu(ifa, &inet_addr_lst[hash], hash)
187 if (ifa->ifa_local == addr &&
188 net_eq(dev_net(ifa->ifa_dev->dev), net))
189 return ifa;
190
191 return NULL;
192}
193
Thomas Grafd6062cb2006-08-15 00:33:59 -0700194static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Alan Sterne041c682006-03-27 01:16:30 -0800196static BLOCKING_NOTIFIER_HEAD(inetaddr_chain);
Krister Johansen3ad7d242017-06-08 13:12:14 -0700197static BLOCKING_NOTIFIER_HEAD(inetaddr_validator_chain);
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200198static void inet_del_ifa(struct in_device *in_dev,
199 struct in_ifaddr __rcu **ifap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int destroy);
201#ifdef CONFIG_SYSCTL
WANG Cong20e61da2014-07-25 15:25:08 -0700202static int devinet_sysctl_register(struct in_device *idev);
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800203static void devinet_sysctl_unregister(struct in_device *idev);
204#else
WANG Cong20e61da2014-07-25 15:25:08 -0700205static int devinet_sysctl_register(struct in_device *idev)
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800206{
WANG Cong20e61da2014-07-25 15:25:08 -0700207 return 0;
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800208}
Eric Dumazet40384992012-08-03 21:06:50 +0000209static void devinet_sysctl_unregister(struct in_device *idev)
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800210{
211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#endif
213
214/* Locks all the inet devices. */
215
216static struct in_ifaddr *inet_alloc_ifa(void)
217{
Alexey Dobriyan93adcc82008-10-28 13:25:09 -0700218 return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221static void inet_rcu_free_ifa(struct rcu_head *head)
222{
223 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head);
224 if (ifa->ifa_dev)
225 in_dev_put(ifa->ifa_dev);
226 kfree(ifa);
227}
228
Eric Dumazet40384992012-08-03 21:06:50 +0000229static void inet_free_ifa(struct in_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 call_rcu(&ifa->rcu_head, inet_rcu_free_ifa);
232}
233
234void in_dev_finish_destroy(struct in_device *idev)
235{
236 struct net_device *dev = idev->dev;
237
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700238 WARN_ON(idev->ifa_list);
239 WARN_ON(idev->mc_list);
Eric Dumazete9897072013-06-07 08:48:57 -0700240 kfree(rcu_dereference_protected(idev->mc_hash, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000242 pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#endif
244 dev_put(dev);
245 if (!idev->dead)
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800246 pr_err("Freeing alive in_device %p\n", idev);
247 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 kfree(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800250EXPORT_SYMBOL(in_dev_finish_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Herbert Xu71e27da2007-06-04 23:36:06 -0700252static struct in_device *inetdev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 struct in_device *in_dev;
WANG Cong20e61da2014-07-25 15:25:08 -0700255 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 ASSERT_RTNL();
258
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700259 in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!in_dev)
261 goto out;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900262 memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt,
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -0800263 sizeof(in_dev->cnf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 in_dev->cnf.sysctl = NULL;
265 in_dev->dev = dev;
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800266 in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl);
267 if (!in_dev->arp_parms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto out_kfree;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700269 if (IPV4_DEVCONF(in_dev->cnf, FORWARDING))
270 dev_disable_lro(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* Reference in_dev->dev */
272 dev_hold(dev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800273 /* Account for reference dev->ip_ptr (below) */
Reshetova, Elena7658b362017-06-30 13:08:03 +0300274 refcount_set(&in_dev->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
WANG Cong20e61da2014-07-25 15:25:08 -0700276 err = devinet_sysctl_register(in_dev);
277 if (err) {
278 in_dev->dead = 1;
279 in_dev_put(in_dev);
280 in_dev = NULL;
281 goto out;
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 ip_mc_init_dev(in_dev);
284 if (dev->flags & IFF_UP)
285 ip_mc_up(in_dev);
Jarek Poplawski483479e2007-01-09 14:38:31 -0800286
David L Stevens30c4cf52007-01-04 12:31:14 -0800287 /* we can receive as soon as ip_ptr is set -- do this last */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000288 rcu_assign_pointer(dev->ip_ptr, in_dev);
Jarek Poplawski483479e2007-01-09 14:38:31 -0800289out:
WANG Cong20e61da2014-07-25 15:25:08 -0700290 return in_dev ?: ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291out_kfree:
292 kfree(in_dev);
293 in_dev = NULL;
294 goto out;
295}
296
297static void in_dev_rcu_put(struct rcu_head *head)
298{
299 struct in_device *idev = container_of(head, struct in_device, rcu_head);
300 in_dev_put(idev);
301}
302
303static void inetdev_destroy(struct in_device *in_dev)
304{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct net_device *dev;
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200306 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 ASSERT_RTNL();
309
310 dev = in_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 in_dev->dead = 1;
313
314 ip_mc_destroy_dev(in_dev);
315
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200316 while ((ifa = rtnl_dereference(in_dev->ifa_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 inet_del_ifa(in_dev, &in_dev->ifa_list, 0);
318 inet_free_ifa(ifa);
319 }
320
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000321 RCU_INIT_POINTER(dev->ip_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Pavel Emelyanov51602b22007-12-11 02:17:40 -0800323 devinet_sysctl_unregister(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 neigh_parms_release(&arp_tbl, in_dev->arp_parms);
325 arp_ifdown(dev);
326
327 call_rcu(&in_dev->rcu_head, in_dev_rcu_put);
328}
329
Al Viroff428d72006-09-26 22:13:35 -0700330int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Florian Westphald519e872019-05-31 18:27:05 +0200332 const struct in_ifaddr *ifa;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 rcu_read_lock();
Florian Westphald519e872019-05-31 18:27:05 +0200335 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (inet_ifa_match(a, ifa)) {
337 if (!b || inet_ifa_match(b, ifa)) {
338 rcu_read_unlock();
339 return 1;
340 }
341 }
Florian Westphald519e872019-05-31 18:27:05 +0200342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 rcu_read_unlock();
344 return 0;
345}
346
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200347static void __inet_del_ifa(struct in_device *in_dev,
348 struct in_ifaddr __rcu **ifap,
349 int destroy, struct nlmsghdr *nlh, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Harald Welte8f937c62005-05-29 20:23:46 -0700351 struct in_ifaddr *promote = NULL;
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200352 struct in_ifaddr *ifa, *ifa1;
353 struct in_ifaddr *last_prim;
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800354 struct in_ifaddr *prev_prom = NULL;
355 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 ASSERT_RTNL();
358
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200359 ifa1 = rtnl_dereference(*ifap);
360 last_prim = rtnl_dereference(in_dev->ifa_list);
David S. Millerfbd40ea2016-03-13 23:28:00 -0400361 if (in_dev->dead)
362 goto no_promotions;
363
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900364 /* 1. Deleting primary ifaddr forces deletion all secondaries
Harald Welte8f937c62005-05-29 20:23:46 -0700365 * unless alias promotion is set
366 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) {
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200369 struct in_ifaddr __rcu **ifap1 = &ifa1->ifa_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200371 while ((ifa = rtnl_dereference(*ifap1)) != NULL) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900372 if (!(ifa->ifa_flags & IFA_F_SECONDARY) &&
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800373 ifa1->ifa_scope <= ifa->ifa_scope)
374 last_prim = ifa;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (!(ifa->ifa_flags & IFA_F_SECONDARY) ||
377 ifa1->ifa_mask != ifa->ifa_mask ||
378 !inet_ifa_match(ifa1->ifa_address, ifa)) {
379 ifap1 = &ifa->ifa_next;
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800380 prev_prom = ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 continue;
382 }
383
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800384 if (!do_promote) {
David S. Millerfd23c3b2011-02-18 12:42:28 -0800385 inet_hash_remove(ifa);
Harald Welte8f937c62005-05-29 20:23:46 -0700386 *ifap1 = ifa->ifa_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Eric W. Biederman15e47302012-09-07 20:12:54 +0000388 rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid);
Alan Sterne041c682006-03-27 01:16:30 -0800389 blocking_notifier_call_chain(&inetaddr_chain,
390 NETDEV_DOWN, ifa);
Harald Welte8f937c62005-05-29 20:23:46 -0700391 inet_free_ifa(ifa);
392 } else {
393 promote = ifa;
394 break;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397 }
398
Julian Anastasov2d230e22011-03-19 12:13:52 +0000399 /* On promotion all secondaries from subnet are changing
400 * the primary IP, we must remove all their routes silently
401 * and later to add them back with new prefsrc. Do this
402 * while all addresses are on the device list.
403 */
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200404 for (ifa = promote; ifa; ifa = rtnl_dereference(ifa->ifa_next)) {
Julian Anastasov2d230e22011-03-19 12:13:52 +0000405 if (ifa1->ifa_mask == ifa->ifa_mask &&
406 inet_ifa_match(ifa1->ifa_address, ifa))
407 fib_del_ifaddr(ifa, ifa1);
408 }
409
David S. Millerfbd40ea2016-03-13 23:28:00 -0400410no_promotions:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* 2. Unlink it */
412
413 *ifap = ifa1->ifa_next;
David S. Millerfd23c3b2011-02-18 12:42:28 -0800414 inet_hash_remove(ifa1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* 3. Announce address deletion */
417
418 /* Send message first, then call notifier.
419 At first sight, FIB update triggered by notifier
420 will refer to already deleted ifaddr, that could confuse
421 netlink listeners. It is not true: look, gated sees
422 that route deleted and if it still thinks that ifaddr
423 is valid, it will try to restore deleted routes... Grr.
424 So that, this order is correct.
425 */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000426 rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid);
Alan Sterne041c682006-03-27 01:16:30 -0800427 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1);
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800428
429 if (promote) {
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200430 struct in_ifaddr *next_sec;
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800431
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200432 next_sec = rtnl_dereference(promote->ifa_next);
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800433 if (prev_prom) {
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200434 struct in_ifaddr *last_sec;
435
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200436 rcu_assign_pointer(prev_prom->ifa_next, next_sec);
Florian Westphal6a9e9ce2019-06-27 14:03:32 +0200437
438 last_sec = rtnl_dereference(last_prim->ifa_next);
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200439 rcu_assign_pointer(promote->ifa_next, last_sec);
440 rcu_assign_pointer(last_prim->ifa_next, promote);
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800441 }
442
443 promote->ifa_flags &= ~IFA_F_SECONDARY;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000444 rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid);
Alan Sterne041c682006-03-27 01:16:30 -0800445 blocking_notifier_call_chain(&inetaddr_chain,
446 NETDEV_UP, promote);
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200447 for (ifa = next_sec; ifa;
448 ifa = rtnl_dereference(ifa->ifa_next)) {
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800449 if (ifa1->ifa_mask != ifa->ifa_mask ||
450 !inet_ifa_match(ifa1->ifa_address, ifa))
451 continue;
452 fib_add_ifaddr(ifa);
453 }
454
455 }
Herbert Xu63630972007-06-07 18:35:38 -0700456 if (destroy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 inet_free_ifa(ifa1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200460static void inet_del_ifa(struct in_device *in_dev,
461 struct in_ifaddr __rcu **ifap,
Thomas Grafd6062cb2006-08-15 00:33:59 -0700462 int destroy)
463{
464 __inet_del_ifa(in_dev, ifap, destroy, NULL, 0);
465}
466
Jiri Pirko5c766d62013-01-24 09:41:41 +0000467static void check_lifetime(struct work_struct *work);
468
469static DECLARE_DELAYED_WORK(check_lifetime_work, check_lifetime);
470
Thomas Grafd6062cb2006-08-15 00:33:59 -0700471static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
David Ahernde95e042017-10-18 09:56:54 -0700472 u32 portid, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200474 struct in_ifaddr __rcu **last_primary, **ifap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct in_device *in_dev = ifa->ifa_dev;
Krister Johansen3ad7d242017-06-08 13:12:14 -0700476 struct in_validator_info ivi;
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200477 struct in_ifaddr *ifa1;
Krister Johansen3ad7d242017-06-08 13:12:14 -0700478 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 ASSERT_RTNL();
481
482 if (!ifa->ifa_local) {
483 inet_free_ifa(ifa);
484 return 0;
485 }
486
487 ifa->ifa_flags &= ~IFA_F_SECONDARY;
488 last_primary = &in_dev->ifa_list;
489
Matteo Croce2e605462019-07-01 19:01:55 +0200490 /* Don't set IPv6 only flags to IPv4 addresses */
491 ifa->ifa_flags &= ~IPV6ONLY_FLAGS;
492
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200493 ifap = &in_dev->ifa_list;
494 ifa1 = rtnl_dereference(*ifap);
495
496 while (ifa1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) &&
498 ifa->ifa_scope <= ifa1->ifa_scope)
499 last_primary = &ifa1->ifa_next;
500 if (ifa1->ifa_mask == ifa->ifa_mask &&
501 inet_ifa_match(ifa1->ifa_address, ifa)) {
502 if (ifa1->ifa_local == ifa->ifa_local) {
503 inet_free_ifa(ifa);
504 return -EEXIST;
505 }
506 if (ifa1->ifa_scope != ifa->ifa_scope) {
507 inet_free_ifa(ifa);
508 return -EINVAL;
509 }
510 ifa->ifa_flags |= IFA_F_SECONDARY;
511 }
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200512
513 ifap = &ifa1->ifa_next;
514 ifa1 = rtnl_dereference(*ifap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
Krister Johansen3ad7d242017-06-08 13:12:14 -0700517 /* Allow any devices that wish to register ifaddr validtors to weigh
518 * in now, before changes are committed. The rntl lock is serializing
519 * access here, so the state should not change between a validator call
520 * and a final notify on commit. This isn't invoked on promotion under
521 * the assumption that validators are checking the address itself, and
522 * not the flags.
523 */
524 ivi.ivi_addr = ifa->ifa_address;
525 ivi.ivi_dev = ifa->ifa_dev;
David Ahernde95e042017-10-18 09:56:54 -0700526 ivi.extack = extack;
Krister Johansen3ad7d242017-06-08 13:12:14 -0700527 ret = blocking_notifier_call_chain(&inetaddr_validator_chain,
528 NETDEV_UP, &ivi);
529 ret = notifier_to_errno(ret);
530 if (ret) {
531 inet_free_ifa(ifa);
532 return ret;
533 }
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500536 prandom_seed((__force u32) ifa->ifa_local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ifap = last_primary;
538 }
539
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200540 rcu_assign_pointer(ifa->ifa_next, *ifap);
541 rcu_assign_pointer(*ifap, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
David S. Millerfd23c3b2011-02-18 12:42:28 -0800543 inet_hash_insert(dev_net(in_dev->dev), ifa);
544
Jiri Pirko5c766d62013-01-24 09:41:41 +0000545 cancel_delayed_work(&check_lifetime_work);
viresh kumar906e0732014-01-22 12:23:32 +0530546 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
Jiri Pirko5c766d62013-01-24 09:41:41 +0000547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* Send message first, then call notifier.
549 Notifier will trigger FIB update, so that
550 listeners of netlink will know about new ifaddr */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000551 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, portid);
Alan Sterne041c682006-03-27 01:16:30 -0800552 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 return 0;
555}
556
Thomas Grafd6062cb2006-08-15 00:33:59 -0700557static int inet_insert_ifa(struct in_ifaddr *ifa)
558{
David Ahernde95e042017-10-18 09:56:54 -0700559 return __inet_insert_ifa(ifa, NULL, 0, NULL);
Thomas Grafd6062cb2006-08-15 00:33:59 -0700560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
563{
Herbert Xue5ed6392005-10-03 14:35:55 -0700564 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 ASSERT_RTNL();
567
568 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700569 inet_free_ifa(ifa);
570 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Herbert Xu71e27da2007-06-04 23:36:06 -0700572 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100573 neigh_parms_data_state_setall(in_dev->arp_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (ifa->ifa_dev != in_dev) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700575 WARN_ON(ifa->ifa_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 in_dev_hold(in_dev);
577 ifa->ifa_dev = in_dev;
578 }
Joe Perchesf97c1e02007-12-16 13:45:43 -0800579 if (ipv4_is_loopback(ifa->ifa_local))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 ifa->ifa_scope = RT_SCOPE_HOST;
581 return inet_insert_ifa(ifa);
582}
583
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000584/* Caller must hold RCU or RTNL :
585 * We dont take a reference on found in_device
586 */
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -0800587struct in_device *inetdev_by_index(struct net *net, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct net_device *dev;
590 struct in_device *in_dev = NULL;
Eric Dumazetc148fc22009-11-01 19:23:04 +0000591
592 rcu_read_lock();
593 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (dev)
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000595 in_dev = rcu_dereference_rtnl(dev->ip_ptr);
Eric Dumazetc148fc22009-11-01 19:23:04 +0000596 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return in_dev;
598}
Eric Dumazet9f9354b2009-11-04 22:05:10 -0800599EXPORT_SYMBOL(inetdev_by_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601/* Called only from RTNL semaphored context. No locks. */
602
Al Viro60cad5d2006-09-26 22:17:09 -0700603struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
604 __be32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Florian Westphald519e872019-05-31 18:27:05 +0200606 struct in_ifaddr *ifa;
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 ASSERT_RTNL();
609
Florian Westphald519e872019-05-31 18:27:05 +0200610 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa))
612 return ifa;
Florian Westphald519e872019-05-31 18:27:05 +0200613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return NULL;
615}
616
Madhu Challa93a714d2015-02-25 09:58:35 -0800617static int ip_mc_config(struct sock *sk, bool join, const struct in_ifaddr *ifa)
618{
619 struct ip_mreqn mreq = {
620 .imr_multiaddr.s_addr = ifa->ifa_address,
621 .imr_ifindex = ifa->ifa_dev->dev->ifindex,
622 };
623 int ret;
624
625 ASSERT_RTNL();
626
627 lock_sock(sk);
628 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300629 ret = ip_mc_join_group(sk, &mreq);
Madhu Challa93a714d2015-02-25 09:58:35 -0800630 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -0300631 ret = ip_mc_leave_group(sk, &mreq);
Madhu Challa93a714d2015-02-25 09:58:35 -0800632 release_sock(sk);
633
634 return ret;
635}
636
David Ahernc21ef3e2017-04-16 09:48:24 -0700637static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
638 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900640 struct net *net = sock_net(skb->sk);
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200641 struct in_ifaddr __rcu **ifap;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700642 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct in_device *in_dev;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700644 struct ifaddrmsg *ifm;
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200645 struct in_ifaddr *ifa;
646
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700647 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 ASSERT_RTNL();
650
Johannes Berg8cb08172019-04-26 14:07:28 +0200651 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
652 ifa_ipv4_policy, extack);
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700653 if (err < 0)
654 goto errout;
655
656 ifm = nlmsg_data(nlh);
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -0800657 in_dev = inetdev_by_index(net, ifm->ifa_index);
Ian Morris51456b22015-04-03 09:17:26 +0100658 if (!in_dev) {
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700659 err = -ENODEV;
660 goto errout;
661 }
662
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200663 for (ifap = &in_dev->ifa_list; (ifa = rtnl_dereference(*ifap)) != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 ifap = &ifa->ifa_next) {
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700665 if (tb[IFA_LOCAL] &&
Jiri Benc67b61f62015-03-29 16:59:26 +0200666 ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 continue;
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700668
669 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
670 continue;
671
672 if (tb[IFA_ADDRESS] &&
673 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
Jiri Benc67b61f62015-03-29 16:59:26 +0200674 !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa)))
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700675 continue;
676
Madhu Challa93a714d2015-02-25 09:58:35 -0800677 if (ipv4_is_multicast(ifa->ifa_address))
678 ip_mc_config(net->ipv4.mc_autojoin_sk, false, ifa);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000679 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
681 }
Thomas Grafdfdd5fd2006-08-04 23:04:17 -0700682
683 err = -EADDRNOTAVAIL;
684errout:
685 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
Jiri Pirko5c766d62013-01-24 09:41:41 +0000688#define INFINITY_LIFE_TIME 0xFFFFFFFF
689
690static void check_lifetime(struct work_struct *work)
691{
692 unsigned long now, next, next_sec, next_sched;
693 struct in_ifaddr *ifa;
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000694 struct hlist_node *n;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000695 int i;
696
697 now = jiffies;
698 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
699
Jiri Pirko5c766d62013-01-24 09:41:41 +0000700 for (i = 0; i < IN4_ADDR_HSIZE; i++) {
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000701 bool change_needed = false;
702
703 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800704 hlist_for_each_entry_rcu(ifa, &inet_addr_lst[i], hash) {
Jiri Pirko5c766d62013-01-24 09:41:41 +0000705 unsigned long age;
706
707 if (ifa->ifa_flags & IFA_F_PERMANENT)
708 continue;
709
710 /* We try to batch several events at once. */
711 age = (now - ifa->ifa_tstamp +
712 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
713
714 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
715 age >= ifa->ifa_valid_lft) {
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000716 change_needed = true;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000717 } else if (ifa->ifa_preferred_lft ==
718 INFINITY_LIFE_TIME) {
719 continue;
720 } else if (age >= ifa->ifa_preferred_lft) {
721 if (time_before(ifa->ifa_tstamp +
722 ifa->ifa_valid_lft * HZ, next))
723 next = ifa->ifa_tstamp +
724 ifa->ifa_valid_lft * HZ;
725
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000726 if (!(ifa->ifa_flags & IFA_F_DEPRECATED))
727 change_needed = true;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000728 } else if (time_before(ifa->ifa_tstamp +
729 ifa->ifa_preferred_lft * HZ,
730 next)) {
731 next = ifa->ifa_tstamp +
732 ifa->ifa_preferred_lft * HZ;
733 }
734 }
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000735 rcu_read_unlock();
736 if (!change_needed)
737 continue;
738 rtnl_lock();
739 hlist_for_each_entry_safe(ifa, n, &inet_addr_lst[i], hash) {
740 unsigned long age;
741
742 if (ifa->ifa_flags & IFA_F_PERMANENT)
743 continue;
744
745 /* We try to batch several events at once. */
746 age = (now - ifa->ifa_tstamp +
747 ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
748
749 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME &&
750 age >= ifa->ifa_valid_lft) {
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200751 struct in_ifaddr __rcu **ifap;
752 struct in_ifaddr *tmp;
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000753
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200754 ifap = &ifa->ifa_dev->ifa_list;
755 tmp = rtnl_dereference(*ifap);
756 while (tmp) {
Florian Westphal40008e92019-06-17 16:02:27 +0200757 if (tmp == ifa) {
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000758 inet_del_ifa(ifa->ifa_dev,
759 ifap, 1);
760 break;
761 }
Florian Westphal2638eb8b2019-05-31 18:27:09 +0200762 ifap = &tmp->ifa_next;
763 tmp = rtnl_dereference(*ifap);
Jiri Pirkoc988d1e2013-04-04 23:39:39 +0000764 }
765 } else if (ifa->ifa_preferred_lft !=
766 INFINITY_LIFE_TIME &&
767 age >= ifa->ifa_preferred_lft &&
768 !(ifa->ifa_flags & IFA_F_DEPRECATED)) {
769 ifa->ifa_flags |= IFA_F_DEPRECATED;
770 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
771 }
772 }
773 rtnl_unlock();
Jiri Pirko5c766d62013-01-24 09:41:41 +0000774 }
Jiri Pirko5c766d62013-01-24 09:41:41 +0000775
776 next_sec = round_jiffies_up(next);
777 next_sched = next;
778
779 /* If rounded timeout is accurate enough, accept it. */
780 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
781 next_sched = next_sec;
782
783 now = jiffies;
784 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
785 if (time_before(next_sched, now + ADDRCONF_TIMER_FUZZ_MAX))
786 next_sched = now + ADDRCONF_TIMER_FUZZ_MAX;
787
viresh kumar906e0732014-01-22 12:23:32 +0530788 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work,
789 next_sched - now);
Jiri Pirko5c766d62013-01-24 09:41:41 +0000790}
791
792static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft,
793 __u32 prefered_lft)
794{
795 unsigned long timeout;
796
797 ifa->ifa_flags &= ~(IFA_F_PERMANENT | IFA_F_DEPRECATED);
798
799 timeout = addrconf_timeout_fixup(valid_lft, HZ);
800 if (addrconf_finite_timeout(timeout))
801 ifa->ifa_valid_lft = timeout;
802 else
803 ifa->ifa_flags |= IFA_F_PERMANENT;
804
805 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
806 if (addrconf_finite_timeout(timeout)) {
807 if (timeout == 0)
808 ifa->ifa_flags |= IFA_F_DEPRECATED;
809 ifa->ifa_preferred_lft = timeout;
810 }
811 ifa->ifa_tstamp = jiffies;
812 if (!ifa->ifa_cstamp)
813 ifa->ifa_cstamp = ifa->ifa_tstamp;
814}
815
816static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
David Aherndac9c972018-10-07 20:16:24 -0700817 __u32 *pvalid_lft, __u32 *pprefered_lft,
818 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Thomas Graf5c753972006-08-04 23:03:53 -0700820 struct nlattr *tb[IFA_MAX+1];
821 struct in_ifaddr *ifa;
822 struct ifaddrmsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 struct net_device *dev;
824 struct in_device *in_dev;
Denis V. Lunev7b218572008-01-31 18:47:00 -0800825 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Johannes Berg8cb08172019-04-26 14:07:28 +0200827 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
828 ifa_ipv4_policy, extack);
Thomas Graf5c753972006-08-04 23:03:53 -0700829 if (err < 0)
830 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Thomas Graf5c753972006-08-04 23:03:53 -0700832 ifm = nlmsg_data(nlh);
Denis V. Lunev7b218572008-01-31 18:47:00 -0800833 err = -EINVAL;
Ian Morris51456b22015-04-03 09:17:26 +0100834 if (ifm->ifa_prefixlen > 32 || !tb[IFA_LOCAL])
Thomas Graf5c753972006-08-04 23:03:53 -0700835 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -0800837 dev = __dev_get_by_index(net, ifm->ifa_index);
Denis V. Lunev7b218572008-01-31 18:47:00 -0800838 err = -ENODEV;
Ian Morris51456b22015-04-03 09:17:26 +0100839 if (!dev)
Thomas Graf5c753972006-08-04 23:03:53 -0700840 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Thomas Graf5c753972006-08-04 23:03:53 -0700842 in_dev = __in_dev_get_rtnl(dev);
Denis V. Lunev7b218572008-01-31 18:47:00 -0800843 err = -ENOBUFS;
Ian Morris51456b22015-04-03 09:17:26 +0100844 if (!in_dev)
Herbert Xu71e27da2007-06-04 23:36:06 -0700845 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Thomas Graf5c753972006-08-04 23:03:53 -0700847 ifa = inet_alloc_ifa();
Ian Morris51456b22015-04-03 09:17:26 +0100848 if (!ifa)
Thomas Graf5c753972006-08-04 23:03:53 -0700849 /*
850 * A potential indev allocation can be left alive, it stays
851 * assigned to its device and is destroy with it.
852 */
Thomas Graf5c753972006-08-04 23:03:53 -0700853 goto errout;
Thomas Graf5c753972006-08-04 23:03:53 -0700854
Pavel Emelyanova4e65d32007-12-07 23:55:43 -0800855 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100856 neigh_parms_data_state_setall(in_dev->arp_parms);
Thomas Graf5c753972006-08-04 23:03:53 -0700857 in_dev_hold(in_dev);
858
Ian Morris51456b22015-04-03 09:17:26 +0100859 if (!tb[IFA_ADDRESS])
Thomas Graf5c753972006-08-04 23:03:53 -0700860 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
861
David S. Millerfd23c3b2011-02-18 12:42:28 -0800862 INIT_HLIST_NODE(&ifa->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 ifa->ifa_prefixlen = ifm->ifa_prefixlen;
864 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen);
Jiri Pirkoad6c8132013-12-08 12:16:10 +0100865 ifa->ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) :
866 ifm->ifa_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 ifa->ifa_scope = ifm->ifa_scope;
Thomas Graf5c753972006-08-04 23:03:53 -0700868 ifa->ifa_dev = in_dev;
869
Jiri Benc67b61f62015-03-29 16:59:26 +0200870 ifa->ifa_local = nla_get_in_addr(tb[IFA_LOCAL]);
871 ifa->ifa_address = nla_get_in_addr(tb[IFA_ADDRESS]);
Thomas Graf5c753972006-08-04 23:03:53 -0700872
873 if (tb[IFA_BROADCAST])
Jiri Benc67b61f62015-03-29 16:59:26 +0200874 ifa->ifa_broadcast = nla_get_in_addr(tb[IFA_BROADCAST]);
Thomas Graf5c753972006-08-04 23:03:53 -0700875
Thomas Graf5c753972006-08-04 23:03:53 -0700876 if (tb[IFA_LABEL])
877 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 else
879 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
880
David Ahernaf4d7682018-05-27 08:09:57 -0700881 if (tb[IFA_RT_PRIORITY])
882 ifa->ifa_rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
883
Jiri Pirko5c766d62013-01-24 09:41:41 +0000884 if (tb[IFA_CACHEINFO]) {
885 struct ifa_cacheinfo *ci;
886
887 ci = nla_data(tb[IFA_CACHEINFO]);
888 if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) {
889 err = -EINVAL;
Daniel Borkmann446266b2013-08-02 11:32:43 +0200890 goto errout_free;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000891 }
892 *pvalid_lft = ci->ifa_valid;
893 *pprefered_lft = ci->ifa_prefered;
894 }
895
Thomas Graf5c753972006-08-04 23:03:53 -0700896 return ifa;
897
Daniel Borkmann446266b2013-08-02 11:32:43 +0200898errout_free:
899 inet_free_ifa(ifa);
Thomas Graf5c753972006-08-04 23:03:53 -0700900errout:
901 return ERR_PTR(err);
902}
903
Jiri Pirko5c766d62013-01-24 09:41:41 +0000904static struct in_ifaddr *find_matching_ifa(struct in_ifaddr *ifa)
905{
906 struct in_device *in_dev = ifa->ifa_dev;
Florian Westphalef11db32019-05-31 18:27:04 +0200907 struct in_ifaddr *ifa1;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000908
909 if (!ifa->ifa_local)
910 return NULL;
911
Florian Westphalef11db32019-05-31 18:27:04 +0200912 in_dev_for_each_ifa_rtnl(ifa1, in_dev) {
Jiri Pirko5c766d62013-01-24 09:41:41 +0000913 if (ifa1->ifa_mask == ifa->ifa_mask &&
914 inet_ifa_match(ifa1->ifa_address, ifa) &&
915 ifa1->ifa_local == ifa->ifa_local)
916 return ifa1;
917 }
918 return NULL;
919}
920
David Ahernc21ef3e2017-04-16 09:48:24 -0700921static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
922 struct netlink_ext_ack *extack)
Thomas Graf5c753972006-08-04 23:03:53 -0700923{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900924 struct net *net = sock_net(skb->sk);
Thomas Graf5c753972006-08-04 23:03:53 -0700925 struct in_ifaddr *ifa;
Jiri Pirko5c766d62013-01-24 09:41:41 +0000926 struct in_ifaddr *ifa_existing;
927 __u32 valid_lft = INFINITY_LIFE_TIME;
928 __u32 prefered_lft = INFINITY_LIFE_TIME;
Thomas Graf5c753972006-08-04 23:03:53 -0700929
930 ASSERT_RTNL();
931
David Aherndac9c972018-10-07 20:16:24 -0700932 ifa = rtm_to_ifaddr(net, nlh, &valid_lft, &prefered_lft, extack);
Thomas Graf5c753972006-08-04 23:03:53 -0700933 if (IS_ERR(ifa))
934 return PTR_ERR(ifa);
935
Jiri Pirko5c766d62013-01-24 09:41:41 +0000936 ifa_existing = find_matching_ifa(ifa);
937 if (!ifa_existing) {
938 /* It would be best to check for !NLM_F_CREATE here but
stephen hemminger614d0562014-05-16 20:46:58 -0700939 * userspace already relies on not having to provide this.
Jiri Pirko5c766d62013-01-24 09:41:41 +0000940 */
941 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
Madhu Challa93a714d2015-02-25 09:58:35 -0800942 if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) {
943 int ret = ip_mc_config(net->ipv4.mc_autojoin_sk,
944 true, ifa);
945
946 if (ret < 0) {
947 inet_free_ifa(ifa);
948 return ret;
949 }
950 }
David Ahernde95e042017-10-18 09:56:54 -0700951 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid,
952 extack);
Jiri Pirko5c766d62013-01-24 09:41:41 +0000953 } else {
David Ahernaf4d7682018-05-27 08:09:57 -0700954 u32 new_metric = ifa->ifa_rt_priority;
955
Jiri Pirko5c766d62013-01-24 09:41:41 +0000956 inet_free_ifa(ifa);
957
958 if (nlh->nlmsg_flags & NLM_F_EXCL ||
959 !(nlh->nlmsg_flags & NLM_F_REPLACE))
960 return -EEXIST;
Jiri Pirko34e2ed32013-04-04 08:33:00 +0000961 ifa = ifa_existing;
David Ahernaf4d7682018-05-27 08:09:57 -0700962
963 if (ifa->ifa_rt_priority != new_metric) {
964 fib_modify_prefix_metric(ifa, new_metric);
965 ifa->ifa_rt_priority = new_metric;
966 }
967
Jiri Pirko34e2ed32013-04-04 08:33:00 +0000968 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
Jiri Pirko05a324b2013-04-04 23:39:38 +0000969 cancel_delayed_work(&check_lifetime_work);
viresh kumar906e0732014-01-22 12:23:32 +0530970 queue_delayed_work(system_power_efficient_wq,
971 &check_lifetime_work, 0);
Jiri Pirko34e2ed32013-04-04 08:33:00 +0000972 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid);
Jiri Pirko5c766d62013-01-24 09:41:41 +0000973 }
974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977/*
978 * Determine a default network mask, based on the IP address.
979 */
980
Eric Dumazet40384992012-08-03 21:06:50 +0000981static int inet_abc_len(__be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
983 int rc = -1; /* Something else, probably a multicast. */
984
Dave Taht65cab852018-12-11 15:30:34 -0800985 if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900986 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 else {
Al Viro714e85b2006-11-14 20:51:49 -0800988 __u32 haddr = ntohl(addr);
Al Viro714e85b2006-11-14 20:51:49 -0800989 if (IN_CLASSA(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 rc = 8;
Al Viro714e85b2006-11-14 20:51:49 -0800991 else if (IN_CLASSB(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 rc = 16;
Al Viro714e85b2006-11-14 20:51:49 -0800993 else if (IN_CLASSC(haddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 rc = 24;
Dave Taht65cab852018-12-11 15:30:34 -0800995 else if (IN_CLASSE(haddr))
996 rc = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900999 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002
Al Viro03aef172017-07-01 07:53:12 -04001003int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 struct sockaddr_in sin_orig;
Al Viro03aef172017-07-01 07:53:12 -04001006 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
Florian Westphal2638eb8b2019-05-31 18:27:09 +02001007 struct in_ifaddr __rcu **ifap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 struct in_ifaddr *ifa = NULL;
1010 struct net_device *dev;
1011 char *colon;
1012 int ret = -EFAULT;
1013 int tryaddrmatch = 0;
1014
Al Viro03aef172017-07-01 07:53:12 -04001015 ifr->ifr_name[IFNAMSIZ - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 /* save original address for comparison */
1018 memcpy(&sin_orig, sin, sizeof(*sin));
1019
Al Viro03aef172017-07-01 07:53:12 -04001020 colon = strchr(ifr->ifr_name, ':');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (colon)
1022 *colon = 0;
1023
Al Viro03aef172017-07-01 07:53:12 -04001024 dev_load(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Stephen Hemminger132adf52007-03-08 20:44:43 -08001026 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 case SIOCGIFADDR: /* Get interface address */
1028 case SIOCGIFBRDADDR: /* Get the broadcast address */
1029 case SIOCGIFDSTADDR: /* Get the destination address */
1030 case SIOCGIFNETMASK: /* Get the netmask for the interface */
1031 /* Note that these ioctls will not sleep,
1032 so that we do not impose a lock.
1033 One day we will be forced to put shlock here (I mean SMP)
1034 */
1035 tryaddrmatch = (sin_orig.sin_family == AF_INET);
1036 memset(sin, 0, sizeof(*sin));
1037 sin->sin_family = AF_INET;
1038 break;
1039
1040 case SIOCSIFFLAGS:
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +00001041 ret = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001042 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 goto out;
1044 break;
1045 case SIOCSIFADDR: /* Set interface address (and family) */
1046 case SIOCSIFBRDADDR: /* Set the broadcast address */
1047 case SIOCSIFDSTADDR: /* Set the destination address */
1048 case SIOCSIFNETMASK: /* Set the netmask for the interface */
Zhao Hongjiangbf5b30b2012-09-20 22:37:25 +00001049 ret = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001050 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 goto out;
1052 ret = -EINVAL;
1053 if (sin->sin_family != AF_INET)
1054 goto out;
1055 break;
1056 default:
1057 ret = -EINVAL;
1058 goto out;
1059 }
1060
1061 rtnl_lock();
1062
1063 ret = -ENODEV;
Al Viro03aef172017-07-01 07:53:12 -04001064 dev = __dev_get_by_name(net, ifr->ifr_name);
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001065 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 goto done;
1067
1068 if (colon)
1069 *colon = ':';
1070
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001071 in_dev = __in_dev_get_rtnl(dev);
1072 if (in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (tryaddrmatch) {
1074 /* Matthias Andree */
1075 /* compare label and address (4.4BSD style) */
1076 /* note: we only do this for a limited set of ioctls
1077 and only if the original address family was AF_INET.
1078 This is checked above. */
Florian Westphal2638eb8b2019-05-31 18:27:09 +02001079
1080 for (ifap = &in_dev->ifa_list;
1081 (ifa = rtnl_dereference(*ifap)) != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 ifap = &ifa->ifa_next) {
Al Viro03aef172017-07-01 07:53:12 -04001083 if (!strcmp(ifr->ifr_name, ifa->ifa_label) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 sin_orig.sin_addr.s_addr ==
David S. Miller6c91afe2011-03-09 13:27:16 -08001085 ifa->ifa_local) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 break; /* found */
1087 }
1088 }
1089 }
1090 /* we didn't get a match, maybe the application is
1091 4.3BSD-style and passed in junk so we fall back to
1092 comparing just the label */
1093 if (!ifa) {
Florian Westphal2638eb8b2019-05-31 18:27:09 +02001094 for (ifap = &in_dev->ifa_list;
1095 (ifa = rtnl_dereference(*ifap)) != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 ifap = &ifa->ifa_next)
Al Viro03aef172017-07-01 07:53:12 -04001097 if (!strcmp(ifr->ifr_name, ifa->ifa_label))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 break;
1099 }
1100 }
1101
1102 ret = -EADDRNOTAVAIL;
1103 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS)
1104 goto done;
1105
Stephen Hemminger132adf52007-03-08 20:44:43 -08001106 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 case SIOCGIFADDR: /* Get interface address */
Tonghao Zhang30e948a2018-01-28 03:38:58 -08001108 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 sin->sin_addr.s_addr = ifa->ifa_local;
Al Viro03aef172017-07-01 07:53:12 -04001110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 case SIOCGIFBRDADDR: /* Get the broadcast 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_broadcast;
Al Viro03aef172017-07-01 07:53:12 -04001115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 case SIOCGIFDSTADDR: /* Get the destination 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_address;
Al Viro03aef172017-07-01 07:53:12 -04001120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 case SIOCGIFNETMASK: /* Get the netmask for the interface */
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_mask;
Al Viro03aef172017-07-01 07:53:12 -04001125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 case SIOCSIFFLAGS:
1128 if (colon) {
1129 ret = -EADDRNOTAVAIL;
1130 if (!ifa)
1131 break;
1132 ret = 0;
Al Viro03aef172017-07-01 07:53:12 -04001133 if (!(ifr->ifr_flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 inet_del_ifa(in_dev, ifap, 1);
1135 break;
1136 }
Petr Machata567c5e12018-12-06 17:05:42 +00001137 ret = dev_change_flags(dev, ifr->ifr_flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 break;
1139
1140 case SIOCSIFADDR: /* Set interface address (and family) */
1141 ret = -EINVAL;
1142 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1143 break;
1144
1145 if (!ifa) {
1146 ret = -ENOBUFS;
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001147 ifa = inet_alloc_ifa();
1148 if (!ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 break;
Xi Wangc7e2e1d2013-01-05 11:19:24 +00001150 INIT_HLIST_NODE(&ifa->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (colon)
Al Viro03aef172017-07-01 07:53:12 -04001152 memcpy(ifa->ifa_label, ifr->ifr_name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 else
1154 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
1155 } else {
1156 ret = 0;
1157 if (ifa->ifa_local == sin->sin_addr.s_addr)
1158 break;
1159 inet_del_ifa(in_dev, ifap, 0);
1160 ifa->ifa_broadcast = 0;
Bjorn Mork148f9722008-02-26 18:17:53 -08001161 ifa->ifa_scope = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163
1164 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr;
1165
1166 if (!(dev->flags & IFF_POINTOPOINT)) {
1167 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address);
1168 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen);
1169 if ((dev->flags & IFF_BROADCAST) &&
1170 ifa->ifa_prefixlen < 31)
1171 ifa->ifa_broadcast = ifa->ifa_address |
1172 ~ifa->ifa_mask;
1173 } else {
1174 ifa->ifa_prefixlen = 32;
1175 ifa->ifa_mask = inet_make_mask(32);
1176 }
Jiri Pirko5c766d62013-01-24 09:41:41 +00001177 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 ret = inet_set_ifa(dev, ifa);
1179 break;
1180
1181 case SIOCSIFBRDADDR: /* Set the broadcast address */
1182 ret = 0;
1183 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) {
1184 inet_del_ifa(in_dev, ifap, 0);
1185 ifa->ifa_broadcast = sin->sin_addr.s_addr;
1186 inet_insert_ifa(ifa);
1187 }
1188 break;
1189
1190 case SIOCSIFDSTADDR: /* Set the destination address */
1191 ret = 0;
1192 if (ifa->ifa_address == sin->sin_addr.s_addr)
1193 break;
1194 ret = -EINVAL;
1195 if (inet_abc_len(sin->sin_addr.s_addr) < 0)
1196 break;
1197 ret = 0;
1198 inet_del_ifa(in_dev, ifap, 0);
1199 ifa->ifa_address = sin->sin_addr.s_addr;
1200 inet_insert_ifa(ifa);
1201 break;
1202
1203 case SIOCSIFNETMASK: /* Set the netmask for the interface */
1204
1205 /*
1206 * The mask we set must be legal.
1207 */
1208 ret = -EINVAL;
1209 if (bad_mask(sin->sin_addr.s_addr, 0))
1210 break;
1211 ret = 0;
1212 if (ifa->ifa_mask != sin->sin_addr.s_addr) {
Al Viroa144ea42006-09-28 18:00:55 -07001213 __be32 old_mask = ifa->ifa_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 inet_del_ifa(in_dev, ifap, 0);
1215 ifa->ifa_mask = sin->sin_addr.s_addr;
1216 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask);
1217
1218 /* See if current broadcast address matches
1219 * with current netmask, then recalculate
1220 * the broadcast address. Otherwise it's a
1221 * funny address, so don't touch it since
1222 * the user seems to know what (s)he's doing...
1223 */
1224 if ((dev->flags & IFF_BROADCAST) &&
1225 (ifa->ifa_prefixlen < 31) &&
1226 (ifa->ifa_broadcast ==
David Engeldcab5e12005-10-21 22:09:16 -05001227 (ifa->ifa_local|~old_mask))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 ifa->ifa_broadcast = (ifa->ifa_local |
1229 ~sin->sin_addr.s_addr);
1230 }
1231 inet_insert_ifa(ifa);
1232 }
1233 break;
1234 }
1235done:
1236 rtnl_unlock();
1237out:
1238 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Al Viro36fd6332017-06-26 13:19:16 -04001241static int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Herbert Xue5ed6392005-10-03 14:35:55 -07001243 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Florian Westphalef11db32019-05-31 18:27:04 +02001244 const struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 struct ifreq ifr;
1246 int done = 0;
1247
Al Viro36fd6332017-06-26 13:19:16 -04001248 if (WARN_ON(size > sizeof(struct ifreq)))
1249 goto out;
1250
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001251 if (!in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 goto out;
1253
Florian Westphalef11db32019-05-31 18:27:04 +02001254 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (!buf) {
Al Viro36fd6332017-06-26 13:19:16 -04001256 done += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 continue;
1258 }
Al Viro36fd6332017-06-26 13:19:16 -04001259 if (len < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 break;
1261 memset(&ifr, 0, sizeof(struct ifreq));
Dan Carpenter4299c8a2013-07-29 22:15:19 +03001262 strcpy(ifr.ifr_name, ifa->ifa_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET;
1265 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr =
1266 ifa->ifa_local;
1267
Al Viro36fd6332017-06-26 13:19:16 -04001268 if (copy_to_user(buf + done, &ifr, size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 done = -EFAULT;
1270 break;
1271 }
Al Viro36fd6332017-06-26 13:19:16 -04001272 len -= size;
1273 done += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275out:
1276 return done;
1277}
1278
Gao Feng8b57fd12017-03-10 12:38:47 +08001279static __be32 in_dev_select_addr(const struct in_device *in_dev,
1280 int scope)
1281{
Florian Westphald519e872019-05-31 18:27:05 +02001282 const struct in_ifaddr *ifa;
1283
1284 in_dev_for_each_ifa_rcu(ifa, in_dev) {
1285 if (ifa->ifa_flags & IFA_F_SECONDARY)
1286 continue;
Gao Feng8b57fd12017-03-10 12:38:47 +08001287 if (ifa->ifa_scope != RT_SCOPE_LINK &&
1288 ifa->ifa_scope <= scope)
1289 return ifa->ifa_local;
Florian Westphald519e872019-05-31 18:27:05 +02001290 }
Gao Feng8b57fd12017-03-10 12:38:47 +08001291
1292 return 0;
1293}
1294
Al Viroa61ced52006-09-26 21:27:54 -07001295__be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Florian Westphald519e872019-05-31 18:27:05 +02001297 const struct in_ifaddr *ifa;
Al Viroa61ced52006-09-26 21:27:54 -07001298 __be32 addr = 0;
Shijie Luod8c444d2019-06-18 15:14:03 +00001299 unsigned char localnet_scope = RT_SCOPE_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 struct in_device *in_dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001301 struct net *net = dev_net(dev);
David Ahern3f2fb9a2016-02-24 11:47:02 -08001302 int master_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07001305 in_dev = __in_dev_get_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (!in_dev)
1307 goto no_in_dev;
1308
Shijie Luod8c444d2019-06-18 15:14:03 +00001309 if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev)))
1310 localnet_scope = RT_SCOPE_LINK;
1311
Florian Westphald519e872019-05-31 18:27:05 +02001312 in_dev_for_each_ifa_rcu(ifa, in_dev) {
1313 if (ifa->ifa_flags & IFA_F_SECONDARY)
1314 continue;
Shijie Luod8c444d2019-06-18 15:14:03 +00001315 if (min(ifa->ifa_scope, localnet_scope) > scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 continue;
1317 if (!dst || inet_ifa_match(dst, ifa)) {
1318 addr = ifa->ifa_local;
1319 break;
1320 }
1321 if (!addr)
1322 addr = ifa->ifa_local;
Florian Westphald519e872019-05-31 18:27:05 +02001323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 if (addr)
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001326 goto out_unlock;
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001327no_in_dev:
David Ahern3f2fb9a2016-02-24 11:47:02 -08001328 master_idx = l3mdev_master_ifindex_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
David Lamparter17b693c2016-02-24 11:47:03 -08001330 /* For VRFs, the VRF device takes the place of the loopback device,
1331 * with addresses on it being preferred. Note in such cases the
1332 * loopback device will be among the devices that fail the master_idx
1333 * equality check in the loop below.
1334 */
1335 if (master_idx &&
1336 (dev = dev_get_by_index_rcu(net, master_idx)) &&
1337 (in_dev = __in_dev_get_rcu(dev))) {
Gao Feng8b57fd12017-03-10 12:38:47 +08001338 addr = in_dev_select_addr(in_dev, scope);
1339 if (addr)
1340 goto out_unlock;
David Lamparter17b693c2016-02-24 11:47:03 -08001341 }
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /* Not loopback addresses on loopback should be preferred
Stephen Hemmingerca9f1fd2015-02-14 13:47:54 -05001344 in this case. It is important that lo is the first interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 in dev_base list.
1346 */
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001347 for_each_netdev_rcu(net, dev) {
David Ahern3f2fb9a2016-02-24 11:47:02 -08001348 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1349 continue;
1350
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001351 in_dev = __in_dev_get_rcu(dev);
1352 if (!in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 continue;
1354
Gao Feng8b57fd12017-03-10 12:38:47 +08001355 addr = in_dev_select_addr(in_dev, scope);
1356 if (addr)
1357 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001359out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return addr;
1362}
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001363EXPORT_SYMBOL(inet_select_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Al Viro60cad5d2006-09-26 22:17:09 -07001365static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
1366 __be32 local, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Shijie Luo650638a2019-06-18 15:14:04 +00001368 unsigned char localnet_scope = RT_SCOPE_HOST;
Florian Westphalef11db32019-05-31 18:27:04 +02001369 const struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07001370 __be32 addr = 0;
Florian Westphalef11db32019-05-31 18:27:04 +02001371 int same = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Shijie Luo650638a2019-06-18 15:14:04 +00001373 if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev)))
1374 localnet_scope = RT_SCOPE_LINK;
1375
Florian Westphalef11db32019-05-31 18:27:04 +02001376 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Shijie Luo650638a2019-06-18 15:14:04 +00001377 unsigned char min_scope = min(ifa->ifa_scope, localnet_scope);
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (!addr &&
1380 (local == ifa->ifa_local || !local) &&
Shijie Luo650638a2019-06-18 15:14:04 +00001381 min_scope <= scope) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 addr = ifa->ifa_local;
1383 if (same)
1384 break;
1385 }
1386 if (!same) {
1387 same = (!local || inet_ifa_match(local, ifa)) &&
1388 (!dst || inet_ifa_match(dst, ifa));
1389 if (same && addr) {
1390 if (local || !dst)
1391 break;
1392 /* Is the selected addr into dst subnet? */
1393 if (inet_ifa_match(addr, ifa))
1394 break;
1395 /* No, then can we use new local src? */
Shijie Luo650638a2019-06-18 15:14:04 +00001396 if (min_scope <= scope) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 addr = ifa->ifa_local;
1398 break;
1399 }
1400 /* search for large dst subnet for addr */
1401 same = 0;
1402 }
1403 }
Florian Westphalef11db32019-05-31 18:27:04 +02001404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001406 return same ? addr : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
1409/*
1410 * Confirm that local IP address exists using wildcards:
Nicolas Dichtelb601fa192013-12-10 15:02:40 +01001411 * - net: netns to check, cannot be NULL
1412 * - in_dev: only on this interface, NULL=any interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 * - dst: only in the same subnet as dst, 0=any dst
1414 * - local: address, 0=autoselect the local address
1415 * - scope: maximum allowed scope value for the local address
1416 */
Nicolas Dichtelb601fa192013-12-10 15:02:40 +01001417__be32 inet_confirm_addr(struct net *net, struct in_device *in_dev,
Denis V. Lunev9bd85e32008-01-14 23:05:55 -08001418 __be32 dst, __be32 local, int scope)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Al Viro60cad5d2006-09-26 22:17:09 -07001420 __be32 addr = 0;
Denis V. Lunev9bd85e32008-01-14 23:05:55 -08001421 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Ian Morris00db4122015-04-03 09:17:27 +01001423 if (in_dev)
Denis V. Lunev9bd85e32008-01-14 23:05:55 -08001424 return confirm_addr_indev(in_dev, dst, local, scope);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 rcu_read_lock();
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001427 for_each_netdev_rcu(net, dev) {
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001428 in_dev = __in_dev_get_rcu(dev);
1429 if (in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 addr = confirm_addr_indev(in_dev, dst, local, scope);
1431 if (addr)
1432 break;
1433 }
1434 }
1435 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 return addr;
1438}
Andy Gospodarekeaddcd72012-03-22 16:14:29 +00001439EXPORT_SYMBOL(inet_confirm_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441/*
1442 * Device notifier
1443 */
1444
1445int register_inetaddr_notifier(struct notifier_block *nb)
1446{
Alan Sterne041c682006-03-27 01:16:30 -08001447 return blocking_notifier_chain_register(&inetaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001449EXPORT_SYMBOL(register_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451int unregister_inetaddr_notifier(struct notifier_block *nb)
1452{
Alan Sterne041c682006-03-27 01:16:30 -08001453 return blocking_notifier_chain_unregister(&inetaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001455EXPORT_SYMBOL(unregister_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Krister Johansen3ad7d242017-06-08 13:12:14 -07001457int register_inetaddr_validator_notifier(struct notifier_block *nb)
1458{
1459 return blocking_notifier_chain_register(&inetaddr_validator_chain, nb);
1460}
1461EXPORT_SYMBOL(register_inetaddr_validator_notifier);
1462
1463int unregister_inetaddr_validator_notifier(struct notifier_block *nb)
1464{
1465 return blocking_notifier_chain_unregister(&inetaddr_validator_chain,
1466 nb);
1467}
1468EXPORT_SYMBOL(unregister_inetaddr_validator_notifier);
1469
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001470/* Rename ifa_labels for a device name change. Make some effort to preserve
1471 * existing alias numbering and to create unique labels if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472*/
1473static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001474{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 struct in_ifaddr *ifa;
1476 int named = 0;
1477
Florian Westphalef11db32019-05-31 18:27:04 +02001478 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001479 char old[IFNAMSIZ], *dot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 memcpy(old, ifa->ifa_label, IFNAMSIZ);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001482 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (named++ == 0)
Thomas Graf573bf472008-06-10 15:40:04 -07001484 goto skip;
Mark McLoughlin44344b22008-01-04 00:56:25 -08001485 dot = strchr(old, ':');
Ian Morris51456b22015-04-03 09:17:26 +01001486 if (!dot) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001487 sprintf(old, ":%d", named);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 dot = old;
1489 }
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001490 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001491 strcat(ifa->ifa_label, dot);
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001492 else
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001493 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot);
Thomas Graf573bf472008-06-10 15:40:04 -07001494skip:
1495 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001496 }
1497}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Ian Campbelld11327ad2011-02-11 07:44:16 +00001499static void inetdev_send_gratuitous_arp(struct net_device *dev,
1500 struct in_device *in_dev)
1501
1502{
Florian Westphalef11db32019-05-31 18:27:04 +02001503 const struct in_ifaddr *ifa;
Ian Campbelld11327ad2011-02-11 07:44:16 +00001504
Florian Westphalef11db32019-05-31 18:27:04 +02001505 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Zoltan Kissb76d0782011-07-24 13:09:30 +00001506 arp_send(ARPOP_REQUEST, ETH_P_ARP,
1507 ifa->ifa_local, dev,
1508 ifa->ifa_local, NULL,
1509 dev->dev_addr, NULL);
1510 }
Ian Campbelld11327ad2011-02-11 07:44:16 +00001511}
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513/* Called only under RTNL semaphore */
1514
1515static int inetdev_event(struct notifier_block *this, unsigned long event,
1516 void *ptr)
1517{
Jiri Pirko351638e2013-05-28 01:30:21 +00001518 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric Dumazet748e2d92012-08-22 21:50:59 +00001519 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 ASSERT_RTNL();
1522
1523 if (!in_dev) {
Herbert Xu8030f542007-02-22 01:53:47 +09001524 if (event == NETDEV_REGISTER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 in_dev = inetdev_init(dev);
WANG Cong20e61da2014-07-25 15:25:08 -07001526 if (IS_ERR(in_dev))
1527 return notifier_from_errno(PTR_ERR(in_dev));
Eric W. Biederman0cc217e2007-09-26 22:10:06 -07001528 if (dev->flags & IFF_LOOPBACK) {
Herbert Xu42f811b2007-06-04 23:34:44 -07001529 IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
1530 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
Herbert Xu8030f542007-02-22 01:53:47 +09001531 }
Breno Leitao06770842008-09-02 17:28:58 -07001532 } else if (event == NETDEV_CHANGEMTU) {
1533 /* Re-enabling IP */
1534 if (inetdev_valid_mtu(dev->mtu))
1535 in_dev = inetdev_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537 goto out;
1538 }
1539
1540 switch (event) {
1541 case NETDEV_REGISTER:
Joe Perches91df42b2012-05-15 14:11:54 +00001542 pr_debug("%s: bug\n", __func__);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001543 RCU_INIT_POINTER(dev->ip_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 break;
1545 case NETDEV_UP:
Breno Leitao06770842008-09-02 17:28:58 -07001546 if (!inetdev_valid_mtu(dev->mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 break;
Eric W. Biederman0cc217e2007-09-26 22:10:06 -07001548 if (dev->flags & IFF_LOOPBACK) {
Eric Dumazet9f9354b2009-11-04 22:05:10 -08001549 struct in_ifaddr *ifa = inet_alloc_ifa();
1550
1551 if (ifa) {
David S. Millerfd23c3b2011-02-18 12:42:28 -08001552 INIT_HLIST_NODE(&ifa->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 ifa->ifa_local =
1554 ifa->ifa_address = htonl(INADDR_LOOPBACK);
1555 ifa->ifa_prefixlen = 8;
1556 ifa->ifa_mask = inet_make_mask(8);
1557 in_dev_hold(in_dev);
1558 ifa->ifa_dev = in_dev;
1559 ifa->ifa_scope = RT_SCOPE_HOST;
1560 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
Jiri Pirko5c766d62013-01-24 09:41:41 +00001561 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME,
1562 INFINITY_LIFE_TIME);
Jiri Pirkodfd15822014-01-07 15:55:45 +01001563 ipv4_devconf_setall(in_dev);
1564 neigh_parms_data_state_setall(in_dev->arp_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 inet_insert_ifa(ifa);
1566 }
1567 }
1568 ip_mc_up(in_dev);
Joe Perchesa8eceea2020-03-12 15:50:22 -07001569 fallthrough;
Stephen Hemmingereefef1c2009-02-01 01:04:33 -08001570 case NETDEV_CHANGEADDR:
Ian Campbelld11327ad2011-02-11 07:44:16 +00001571 if (!IN_DEV_ARP_NOTIFY(in_dev))
1572 break;
Joe Perchesa8eceea2020-03-12 15:50:22 -07001573 fallthrough;
Ian Campbelld11327ad2011-02-11 07:44:16 +00001574 case NETDEV_NOTIFY_PEERS:
Stephen Hemmingera21090c2009-10-07 03:18:17 -07001575 /* Send gratuitous ARP to notify of link change */
Ian Campbelld11327ad2011-02-11 07:44:16 +00001576 inetdev_send_gratuitous_arp(dev, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 break;
1578 case NETDEV_DOWN:
1579 ip_mc_down(in_dev);
1580 break;
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001581 case NETDEV_PRE_TYPE_CHANGE:
Moni Shoua75c78502009-09-15 02:37:40 -07001582 ip_mc_unmap(in_dev);
1583 break;
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001584 case NETDEV_POST_TYPE_CHANGE:
Moni Shoua75c78502009-09-15 02:37:40 -07001585 ip_mc_remap(in_dev);
1586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 case NETDEV_CHANGEMTU:
Breno Leitao06770842008-09-02 17:28:58 -07001588 if (inetdev_valid_mtu(dev->mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 break;
Breno Leitao06770842008-09-02 17:28:58 -07001590 /* disable IP when MTU is not enough */
Joe Perchesa8eceea2020-03-12 15:50:22 -07001591 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 case NETDEV_UNREGISTER:
1593 inetdev_destroy(in_dev);
1594 break;
1595 case NETDEV_CHANGENAME:
1596 /* Do not notify about label change, this event is
1597 * not interesting to applications using netlink.
1598 */
1599 inetdev_changename(dev, in_dev);
1600
Pavel Emelyanov51602b22007-12-11 02:17:40 -08001601 devinet_sysctl_unregister(in_dev);
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11001602 devinet_sysctl_register(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 break;
1604 }
1605out:
1606 return NOTIFY_DONE;
1607}
1608
1609static struct notifier_block ip_netdev_notifier = {
Jianjun Kong539afed2008-11-03 02:48:48 -08001610 .notifier_call = inetdev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611};
1612
Eric Dumazet40384992012-08-03 21:06:50 +00001613static size_t inet_nlmsg_size(void)
Thomas Graf339bf982006-11-10 14:10:15 -08001614{
1615 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
1616 + nla_total_size(4) /* IFA_ADDRESS */
1617 + nla_total_size(4) /* IFA_LOCAL */
1618 + nla_total_size(4) /* IFA_BROADCAST */
Jiri Pirkoad6c8132013-12-08 12:16:10 +01001619 + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
Geert Uytterhoeven63b5f152014-02-05 08:38:25 +01001620 + nla_total_size(4) /* IFA_FLAGS */
David Ahernaf4d7682018-05-27 08:09:57 -07001621 + nla_total_size(4) /* IFA_RT_PRIORITY */
Geert Uytterhoeven63b5f152014-02-05 08:38:25 +01001622 + nla_total_size(sizeof(struct ifa_cacheinfo)); /* IFA_CACHEINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08001623}
1624
Jiri Pirko5c766d62013-01-24 09:41:41 +00001625static inline u32 cstamp_delta(unsigned long cstamp)
1626{
1627 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
1628}
1629
1630static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
1631 unsigned long tstamp, u32 preferred, u32 valid)
1632{
1633 struct ifa_cacheinfo ci;
1634
1635 ci.cstamp = cstamp_delta(cstamp);
1636 ci.tstamp = cstamp_delta(tstamp);
1637 ci.ifa_prefered = preferred;
1638 ci.ifa_valid = valid;
1639
1640 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
1641}
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
Christian Brauner978a46f2018-09-04 21:53:54 +02001644 struct inet_fill_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646 struct ifaddrmsg *ifm;
1647 struct nlmsghdr *nlh;
Jiri Pirko5c766d62013-01-24 09:41:41 +00001648 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Christian Brauner978a46f2018-09-04 21:53:54 +02001650 nlh = nlmsg_put(skb, args->portid, args->seq, args->event, sizeof(*ifm),
1651 args->flags);
Ian Morris51456b22015-04-03 09:17:26 +01001652 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08001653 return -EMSGSIZE;
Thomas Graf47f68512006-08-04 23:04:36 -07001654
1655 ifm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 ifm->ifa_family = AF_INET;
1657 ifm->ifa_prefixlen = ifa->ifa_prefixlen;
Jiri Pirko5c766d62013-01-24 09:41:41 +00001658 ifm->ifa_flags = ifa->ifa_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 ifm->ifa_scope = ifa->ifa_scope;
1660 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Christian Brauner978a46f2018-09-04 21:53:54 +02001662 if (args->netnsid >= 0 &&
1663 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
Christian Braunerd3807142018-09-04 21:53:49 +02001664 goto nla_put_failure;
1665
Jiri Pirko5c766d62013-01-24 09:41:41 +00001666 if (!(ifm->ifa_flags & IFA_F_PERMANENT)) {
1667 preferred = ifa->ifa_preferred_lft;
1668 valid = ifa->ifa_valid_lft;
1669 if (preferred != INFINITY_LIFE_TIME) {
1670 long tval = (jiffies - ifa->ifa_tstamp) / HZ;
1671
1672 if (preferred > tval)
1673 preferred -= tval;
1674 else
1675 preferred = 0;
1676 if (valid != INFINITY_LIFE_TIME) {
1677 if (valid > tval)
1678 valid -= tval;
1679 else
1680 valid = 0;
1681 }
1682 }
1683 } else {
1684 preferred = INFINITY_LIFE_TIME;
1685 valid = INFINITY_LIFE_TIME;
1686 }
David S. Millerf3756b72012-04-01 20:39:02 -04001687 if ((ifa->ifa_address &&
Jiri Benc930345e2015-03-29 16:59:25 +02001688 nla_put_in_addr(skb, IFA_ADDRESS, ifa->ifa_address)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001689 (ifa->ifa_local &&
Jiri Benc930345e2015-03-29 16:59:25 +02001690 nla_put_in_addr(skb, IFA_LOCAL, ifa->ifa_local)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001691 (ifa->ifa_broadcast &&
Jiri Benc930345e2015-03-29 16:59:25 +02001692 nla_put_in_addr(skb, IFA_BROADCAST, ifa->ifa_broadcast)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001693 (ifa->ifa_label[0] &&
Jiri Pirko5c766d62013-01-24 09:41:41 +00001694 nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) ||
Jiri Pirkoad6c8132013-12-08 12:16:10 +01001695 nla_put_u32(skb, IFA_FLAGS, ifa->ifa_flags) ||
David Ahernaf4d7682018-05-27 08:09:57 -07001696 (ifa->ifa_rt_priority &&
1697 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->ifa_rt_priority)) ||
Jiri Pirko5c766d62013-01-24 09:41:41 +00001698 put_cacheinfo(skb, ifa->ifa_cstamp, ifa->ifa_tstamp,
1699 preferred, valid))
David S. Millerf3756b72012-04-01 20:39:02 -04001700 goto nla_put_failure;
Thomas Graf47f68512006-08-04 23:04:36 -07001701
Johannes Berg053c0952015-01-16 22:09:00 +01001702 nlmsg_end(skb, nlh);
1703 return 0;
Thomas Graf47f68512006-08-04 23:04:36 -07001704
1705nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001706 nlmsg_cancel(skb, nlh);
1707 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
David Ahernc33078e2018-10-07 20:16:28 -07001710static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
1711 struct inet_fill_args *fillargs,
1712 struct net **tgt_net, struct sock *sk,
David Ahern5fcd2662018-10-19 12:45:29 -07001713 struct netlink_callback *cb)
David Ahernc33078e2018-10-07 20:16:28 -07001714{
David Ahern5fcd2662018-10-19 12:45:29 -07001715 struct netlink_ext_ack *extack = cb->extack;
David Ahernc33078e2018-10-07 20:16:28 -07001716 struct nlattr *tb[IFA_MAX+1];
1717 struct ifaddrmsg *ifm;
1718 int err, i;
1719
1720 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1721 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for address dump request");
1722 return -EINVAL;
1723 }
1724
1725 ifm = nlmsg_data(nlh);
1726 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
1727 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for address dump request");
1728 return -EINVAL;
1729 }
David Ahern5fcd2662018-10-19 12:45:29 -07001730
1731 fillargs->ifindex = ifm->ifa_index;
1732 if (fillargs->ifindex) {
1733 cb->answer_flags |= NLM_F_DUMP_FILTERED;
1734 fillargs->flags |= NLM_F_DUMP_FILTERED;
David Ahernc33078e2018-10-07 20:16:28 -07001735 }
1736
Johannes Berg8cb08172019-04-26 14:07:28 +02001737 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
1738 ifa_ipv4_policy, extack);
David Ahernc33078e2018-10-07 20:16:28 -07001739 if (err < 0)
1740 return err;
1741
1742 for (i = 0; i <= IFA_MAX; ++i) {
1743 if (!tb[i])
1744 continue;
1745
1746 if (i == IFA_TARGET_NETNSID) {
1747 struct net *net;
1748
1749 fillargs->netnsid = nla_get_s32(tb[i]);
1750
1751 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
1752 if (IS_ERR(net)) {
Bjørn Morkbf4cc402018-10-25 21:18:25 +02001753 fillargs->netnsid = -1;
David Ahernc33078e2018-10-07 20:16:28 -07001754 NL_SET_ERR_MSG(extack, "ipv4: Invalid target network namespace id");
1755 return PTR_ERR(net);
1756 }
1757 *tgt_net = net;
1758 } else {
1759 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in dump request");
1760 return -EINVAL;
1761 }
1762 }
1763
1764 return 0;
1765}
1766
David Ahern1c98eca2018-10-19 12:45:27 -07001767static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb,
1768 struct netlink_callback *cb, int s_ip_idx,
1769 struct inet_fill_args *fillargs)
1770{
1771 struct in_ifaddr *ifa;
1772 int ip_idx = 0;
1773 int err;
1774
Florian Westphald3e6e282019-06-03 22:41:44 +02001775 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Florian Westphalef11db32019-05-31 18:27:04 +02001776 if (ip_idx < s_ip_idx) {
1777 ip_idx++;
David Ahern1c98eca2018-10-19 12:45:27 -07001778 continue;
Florian Westphalef11db32019-05-31 18:27:04 +02001779 }
David Ahern1c98eca2018-10-19 12:45:27 -07001780 err = inet_fill_ifaddr(skb, ifa, fillargs);
1781 if (err < 0)
1782 goto done;
1783
1784 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Florian Westphalef11db32019-05-31 18:27:04 +02001785 ip_idx++;
David Ahern1c98eca2018-10-19 12:45:27 -07001786 }
1787 err = 0;
1788
1789done:
1790 cb->args[2] = ip_idx;
1791
1792 return err;
1793}
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1796{
David Ahernc33078e2018-10-07 20:16:28 -07001797 const struct nlmsghdr *nlh = cb->nlh;
Christian Brauner978a46f2018-09-04 21:53:54 +02001798 struct inet_fill_args fillargs = {
1799 .portid = NETLINK_CB(cb->skb).portid,
David Ahernc33078e2018-10-07 20:16:28 -07001800 .seq = nlh->nlmsg_seq,
Christian Brauner978a46f2018-09-04 21:53:54 +02001801 .event = RTM_NEWADDR,
1802 .flags = NLM_F_MULTI,
1803 .netnsid = -1,
1804 };
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001805 struct net *net = sock_net(skb->sk);
Christian Braunerd3807142018-09-04 21:53:49 +02001806 struct net *tgt_net = net;
Eric Dumazeteec4df92009-11-12 07:44:25 +00001807 int h, s_h;
1808 int idx, s_idx;
David Ahern1c98eca2018-10-19 12:45:27 -07001809 int s_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 struct net_device *dev;
1811 struct in_device *in_dev;
Eric Dumazeteec4df92009-11-12 07:44:25 +00001812 struct hlist_head *head;
David Ahernd7e38612018-10-24 12:58:59 -07001813 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Eric Dumazeteec4df92009-11-12 07:44:25 +00001815 s_h = cb->args[0];
1816 s_idx = idx = cb->args[1];
David Ahern1c98eca2018-10-19 12:45:27 -07001817 s_ip_idx = cb->args[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
David Ahernc33078e2018-10-07 20:16:28 -07001819 if (cb->strict_check) {
David Ahernc33078e2018-10-07 20:16:28 -07001820 err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
David Ahern5fcd2662018-10-19 12:45:29 -07001821 skb->sk, cb);
David Ahernc33078e2018-10-07 20:16:28 -07001822 if (err < 0)
David Ahernd7e38612018-10-24 12:58:59 -07001823 goto put_tgt_net;
David Ahern5fcd2662018-10-19 12:45:29 -07001824
David Ahernd7e38612018-10-24 12:58:59 -07001825 err = 0;
David Ahern5fcd2662018-10-19 12:45:29 -07001826 if (fillargs.ifindex) {
1827 dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
David Ahernd7e38612018-10-24 12:58:59 -07001828 if (!dev) {
1829 err = -ENODEV;
1830 goto put_tgt_net;
1831 }
David Ahern5fcd2662018-10-19 12:45:29 -07001832
1833 in_dev = __in_dev_get_rtnl(dev);
1834 if (in_dev) {
1835 err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx,
1836 &fillargs);
1837 }
1838 goto put_tgt_net;
1839 }
Christian Braunerd3807142018-09-04 21:53:49 +02001840 }
1841
Eric Dumazeteec4df92009-11-12 07:44:25 +00001842 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1843 idx = 0;
Christian Braunerd3807142018-09-04 21:53:49 +02001844 head = &tgt_net->dev_index_head[h];
Eric Dumazeteec4df92009-11-12 07:44:25 +00001845 rcu_read_lock();
Christian Braunerd3807142018-09-04 21:53:49 +02001846 cb->seq = atomic_read(&tgt_net->ipv4.dev_addr_genid) ^
1847 tgt_net->dev_base_seq;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001848 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazeteec4df92009-11-12 07:44:25 +00001849 if (idx < s_idx)
1850 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07001851 if (h > s_h || idx > s_idx)
Eric Dumazeteec4df92009-11-12 07:44:25 +00001852 s_ip_idx = 0;
1853 in_dev = __in_dev_get_rcu(dev);
1854 if (!in_dev)
1855 goto cont;
1856
David Ahern1c98eca2018-10-19 12:45:27 -07001857 err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx,
1858 &fillargs);
1859 if (err < 0) {
1860 rcu_read_unlock();
1861 goto done;
Eric Dumazeteec4df92009-11-12 07:44:25 +00001862 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001863cont:
Eric Dumazeteec4df92009-11-12 07:44:25 +00001864 idx++;
1865 }
1866 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868
1869done:
Eric Dumazeteec4df92009-11-12 07:44:25 +00001870 cb->args[0] = h;
1871 cb->args[1] = idx;
David Ahern5fcd2662018-10-19 12:45:29 -07001872put_tgt_net:
Christian Brauner978a46f2018-09-04 21:53:54 +02001873 if (fillargs.netnsid >= 0)
Christian Braunerd3807142018-09-04 21:53:49 +02001874 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Arthur Gautier7c1e8a32018-12-31 02:10:58 +00001876 return skb->len ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Jianjun Kong539afed2008-11-03 02:48:48 -08001879static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001880 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Christian Brauner978a46f2018-09-04 21:53:54 +02001882 struct inet_fill_args fillargs = {
1883 .portid = portid,
1884 .seq = nlh ? nlh->nlmsg_seq : 0,
1885 .event = event,
1886 .flags = 0,
1887 .netnsid = -1,
1888 };
Thomas Graf47f68512006-08-04 23:04:36 -07001889 struct sk_buff *skb;
Thomas Grafd6062cb2006-08-15 00:33:59 -07001890 int err = -ENOBUFS;
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -08001891 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001893 net = dev_net(ifa->ifa_dev->dev);
Thomas Graf339bf982006-11-10 14:10:15 -08001894 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001895 if (!skb)
Thomas Grafd6062cb2006-08-15 00:33:59 -07001896 goto errout;
1897
Christian Brauner978a46f2018-09-04 21:53:54 +02001898 err = inet_fill_ifaddr(skb, ifa, &fillargs);
Patrick McHardy26932562007-01-31 23:16:40 -08001899 if (err < 0) {
1900 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */
1901 WARN_ON(err == -EMSGSIZE);
1902 kfree_skb(skb);
1903 goto errout;
1904 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001905 rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001906 return;
Thomas Grafd6062cb2006-08-15 00:33:59 -07001907errout:
1908 if (err < 0)
Denis V. Lunev4b8aa9a2008-01-31 18:47:40 -08001909 rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001912static size_t inet_get_link_af_size(const struct net_device *dev,
1913 u32 ext_filter_mask)
Thomas Graf9f0f7272010-11-16 04:32:48 +00001914{
Eric Dumazet1fc19af2011-05-09 20:55:03 -07001915 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
Thomas Graf9f0f7272010-11-16 04:32:48 +00001916
1917 if (!in_dev)
1918 return 0;
1919
1920 return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
1921}
1922
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001923static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
1924 u32 ext_filter_mask)
Thomas Graf9f0f7272010-11-16 04:32:48 +00001925{
Eric Dumazet1fc19af2011-05-09 20:55:03 -07001926 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr);
Thomas Graf9f0f7272010-11-16 04:32:48 +00001927 struct nlattr *nla;
1928 int i;
1929
1930 if (!in_dev)
1931 return -ENODATA;
1932
1933 nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
Ian Morris51456b22015-04-03 09:17:26 +01001934 if (!nla)
Thomas Graf9f0f7272010-11-16 04:32:48 +00001935 return -EMSGSIZE;
1936
1937 for (i = 0; i < IPV4_DEVCONF_MAX; i++)
1938 ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
1939
1940 return 0;
1941}
1942
1943static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
1944 [IFLA_INET_CONF] = { .type = NLA_NESTED },
1945};
1946
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001947static int inet_validate_link_af(const struct net_device *dev,
1948 const struct nlattr *nla)
Thomas Graf9f0f7272010-11-16 04:32:48 +00001949{
Thomas Graf9f0f7272010-11-16 04:32:48 +00001950 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1951 int err, rem;
1952
Florian Westphal5fa85a02017-10-16 15:44:36 +02001953 if (dev && !__in_dev_get_rcu(dev))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001954 return -EAFNOSUPPORT;
Thomas Graf9f0f7272010-11-16 04:32:48 +00001955
Johannes Berg8cb08172019-04-26 14:07:28 +02001956 err = nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla,
1957 inet_af_policy, NULL);
Thomas Graf9f0f7272010-11-16 04:32:48 +00001958 if (err < 0)
1959 return err;
1960
1961 if (tb[IFLA_INET_CONF]) {
1962 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
1963 int cfgid = nla_type(a);
1964
1965 if (nla_len(a) < 4)
1966 return -EINVAL;
1967
1968 if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
1969 return -EINVAL;
1970 }
1971 }
1972
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001973 return 0;
1974}
1975
1976static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla)
1977{
Florian Westphal5fa85a02017-10-16 15:44:36 +02001978 struct in_device *in_dev = __in_dev_get_rcu(dev);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001979 struct nlattr *a, *tb[IFLA_INET_MAX+1];
1980 int rem;
1981
1982 if (!in_dev)
1983 return -EAFNOSUPPORT;
1984
Johannes Berg8cb08172019-04-26 14:07:28 +02001985 if (nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla, NULL, NULL) < 0)
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001986 BUG();
1987
Thomas Graf9f0f7272010-11-16 04:32:48 +00001988 if (tb[IFLA_INET_CONF]) {
1989 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
1990 ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
1991 }
1992
1993 return 0;
1994}
1995
Nicolas Dichteledc9e742012-10-25 22:28:52 +00001996static int inet_netconf_msgsize_devconf(int type)
1997{
1998 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
1999 + nla_total_size(4); /* NETCONFA_IFINDEX */
Zhang Shengju136ba622016-03-10 08:55:50 +00002000 bool all = false;
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002001
Zhang Shengju136ba622016-03-10 08:55:50 +00002002 if (type == NETCONFA_ALL)
2003 all = true;
2004
2005 if (all || type == NETCONFA_FORWARDING)
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002006 size += nla_total_size(4);
Zhang Shengju136ba622016-03-10 08:55:50 +00002007 if (all || type == NETCONFA_RP_FILTER)
Nicolas Dichtelcc535df2012-10-29 04:53:27 +00002008 size += nla_total_size(4);
Zhang Shengju136ba622016-03-10 08:55:50 +00002009 if (all || type == NETCONFA_MC_FORWARDING)
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00002010 size += nla_total_size(4);
Xin Long5cbf7772018-07-27 16:37:28 +08002011 if (all || type == NETCONFA_BC_FORWARDING)
2012 size += nla_total_size(4);
Zhang Shengju136ba622016-03-10 08:55:50 +00002013 if (all || type == NETCONFA_PROXY_NEIGH)
stephen hemmingerf085ff12013-12-12 13:06:50 -08002014 size += nla_total_size(4);
Zhang Shengju136ba622016-03-10 08:55:50 +00002015 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
Andy Gospodarek974d7af2015-07-07 13:56:57 -04002016 size += nla_total_size(4);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002017
2018 return size;
2019}
2020
2021static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
2022 struct ipv4_devconf *devconf, u32 portid,
2023 u32 seq, int event, unsigned int flags,
2024 int type)
2025{
2026 struct nlmsghdr *nlh;
2027 struct netconfmsg *ncm;
Zhang Shengju136ba622016-03-10 08:55:50 +00002028 bool all = false;
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002029
2030 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
2031 flags);
Ian Morris51456b22015-04-03 09:17:26 +01002032 if (!nlh)
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002033 return -EMSGSIZE;
2034
Zhang Shengju136ba622016-03-10 08:55:50 +00002035 if (type == NETCONFA_ALL)
2036 all = true;
2037
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002038 ncm = nlmsg_data(nlh);
2039 ncm->ncm_family = AF_INET;
2040
2041 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
2042 goto nla_put_failure;
2043
David Ahernb5c96412017-03-28 14:28:03 -07002044 if (!devconf)
2045 goto out;
2046
Zhang Shengju136ba622016-03-10 08:55:50 +00002047 if ((all || type == NETCONFA_FORWARDING) &&
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002048 nla_put_s32(skb, NETCONFA_FORWARDING,
2049 IPV4_DEVCONF(*devconf, FORWARDING)) < 0)
2050 goto nla_put_failure;
Zhang Shengju136ba622016-03-10 08:55:50 +00002051 if ((all || type == NETCONFA_RP_FILTER) &&
Nicolas Dichtelcc535df2012-10-29 04:53:27 +00002052 nla_put_s32(skb, NETCONFA_RP_FILTER,
2053 IPV4_DEVCONF(*devconf, RP_FILTER)) < 0)
2054 goto nla_put_failure;
Zhang Shengju136ba622016-03-10 08:55:50 +00002055 if ((all || type == NETCONFA_MC_FORWARDING) &&
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00002056 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
2057 IPV4_DEVCONF(*devconf, MC_FORWARDING)) < 0)
2058 goto nla_put_failure;
Xin Long5cbf7772018-07-27 16:37:28 +08002059 if ((all || type == NETCONFA_BC_FORWARDING) &&
2060 nla_put_s32(skb, NETCONFA_BC_FORWARDING,
2061 IPV4_DEVCONF(*devconf, BC_FORWARDING)) < 0)
2062 goto nla_put_failure;
Zhang Shengju136ba622016-03-10 08:55:50 +00002063 if ((all || type == NETCONFA_PROXY_NEIGH) &&
stephen hemminger09aea5d2013-12-17 22:35:52 -08002064 nla_put_s32(skb, NETCONFA_PROXY_NEIGH,
stephen hemmingerf085ff12013-12-12 13:06:50 -08002065 IPV4_DEVCONF(*devconf, PROXY_ARP)) < 0)
2066 goto nla_put_failure;
Zhang Shengju136ba622016-03-10 08:55:50 +00002067 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
Andy Gospodarek974d7af2015-07-07 13:56:57 -04002068 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
2069 IPV4_DEVCONF(*devconf, IGNORE_ROUTES_WITH_LINKDOWN)) < 0)
2070 goto nla_put_failure;
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002071
David Ahernb5c96412017-03-28 14:28:03 -07002072out:
Johannes Berg053c0952015-01-16 22:09:00 +01002073 nlmsg_end(skb, nlh);
2074 return 0;
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002075
2076nla_put_failure:
2077 nlmsg_cancel(skb, nlh);
2078 return -EMSGSIZE;
2079}
2080
David Ahern3b022862017-03-28 14:28:02 -07002081void inet_netconf_notify_devconf(struct net *net, int event, int type,
2082 int ifindex, struct ipv4_devconf *devconf)
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002083{
2084 struct sk_buff *skb;
2085 int err = -ENOBUFS;
2086
Eric Dumazetfa178062016-07-08 05:18:24 +02002087 skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002088 if (!skb)
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002089 goto errout;
2090
2091 err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
David Ahern3b022862017-03-28 14:28:02 -07002092 event, 0, type);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002093 if (err < 0) {
2094 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2095 WARN_ON(err == -EMSGSIZE);
2096 kfree_skb(skb);
2097 goto errout;
2098 }
Eric Dumazetfa178062016-07-08 05:18:24 +02002099 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_KERNEL);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002100 return;
2101errout:
2102 if (err < 0)
2103 rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err);
2104}
2105
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002106static const struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = {
2107 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
2108 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
Nicolas Dichtelcc535df2012-10-29 04:53:27 +00002109 [NETCONFA_RP_FILTER] = { .len = sizeof(int) },
stephen hemminger09aea5d2013-12-17 22:35:52 -08002110 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Andy Gospodarek974d7af2015-07-07 13:56:57 -04002111 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002112};
2113
Jakub Kicinskieede3702019-01-18 10:46:18 -08002114static int inet_netconf_valid_get_req(struct sk_buff *skb,
2115 const struct nlmsghdr *nlh,
2116 struct nlattr **tb,
2117 struct netlink_ext_ack *extack)
2118{
2119 int i, err;
2120
2121 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
2122 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf get request");
2123 return -EINVAL;
2124 }
2125
2126 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02002127 return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
2128 tb, NETCONFA_MAX,
2129 devconf_ipv4_policy, extack);
Jakub Kicinskieede3702019-01-18 10:46:18 -08002130
Johannes Berg8cb08172019-04-26 14:07:28 +02002131 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
2132 tb, NETCONFA_MAX,
2133 devconf_ipv4_policy, extack);
Jakub Kicinskieede3702019-01-18 10:46:18 -08002134 if (err)
2135 return err;
2136
2137 for (i = 0; i <= NETCONFA_MAX; i++) {
2138 if (!tb[i])
2139 continue;
2140
2141 switch (i) {
2142 case NETCONFA_IFINDEX:
2143 break;
2144 default:
2145 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in netconf get request");
2146 return -EINVAL;
2147 }
2148 }
2149
2150 return 0;
2151}
2152
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002153static int inet_netconf_get_devconf(struct sk_buff *in_skb,
David Ahernc21ef3e2017-04-16 09:48:24 -07002154 struct nlmsghdr *nlh,
2155 struct netlink_ext_ack *extack)
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002156{
2157 struct net *net = sock_net(in_skb->sk);
2158 struct nlattr *tb[NETCONFA_MAX+1];
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002159 struct sk_buff *skb;
2160 struct ipv4_devconf *devconf;
2161 struct in_device *in_dev;
2162 struct net_device *dev;
2163 int ifindex;
2164 int err;
2165
Jakub Kicinskieede3702019-01-18 10:46:18 -08002166 err = inet_netconf_valid_get_req(in_skb, nlh, tb, extack);
2167 if (err)
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002168 goto errout;
2169
Anton Protopopova97eb332016-02-16 21:43:16 -05002170 err = -EINVAL;
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002171 if (!tb[NETCONFA_IFINDEX])
2172 goto errout;
2173
2174 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
2175 switch (ifindex) {
2176 case NETCONFA_IFINDEX_ALL:
2177 devconf = net->ipv4.devconf_all;
2178 break;
2179 case NETCONFA_IFINDEX_DEFAULT:
2180 devconf = net->ipv4.devconf_dflt;
2181 break;
2182 default:
2183 dev = __dev_get_by_index(net, ifindex);
Ian Morris51456b22015-04-03 09:17:26 +01002184 if (!dev)
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002185 goto errout;
2186 in_dev = __in_dev_get_rtnl(dev);
Ian Morris51456b22015-04-03 09:17:26 +01002187 if (!in_dev)
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002188 goto errout;
2189 devconf = &in_dev->cnf;
2190 break;
2191 }
2192
2193 err = -ENOBUFS;
Eric Dumazetfa178062016-07-08 05:18:24 +02002194 skb = nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002195 if (!skb)
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002196 goto errout;
2197
2198 err = inet_netconf_fill_devconf(skb, ifindex, devconf,
2199 NETLINK_CB(in_skb).portid,
2200 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
Zhang Shengju136ba622016-03-10 08:55:50 +00002201 NETCONFA_ALL);
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002202 if (err < 0) {
2203 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */
2204 WARN_ON(err == -EMSGSIZE);
2205 kfree_skb(skb);
2206 goto errout;
2207 }
2208 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2209errout:
2210 return err;
2211}
2212
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002213static int inet_netconf_dump_devconf(struct sk_buff *skb,
2214 struct netlink_callback *cb)
2215{
David Ahernaddd3832018-10-07 20:16:41 -07002216 const struct nlmsghdr *nlh = cb->nlh;
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002217 struct net *net = sock_net(skb->sk);
2218 int h, s_h;
2219 int idx, s_idx;
2220 struct net_device *dev;
2221 struct in_device *in_dev;
2222 struct hlist_head *head;
2223
David Ahernaddd3832018-10-07 20:16:41 -07002224 if (cb->strict_check) {
2225 struct netlink_ext_ack *extack = cb->extack;
2226 struct netconfmsg *ncm;
2227
2228 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
2229 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf dump request");
2230 return -EINVAL;
2231 }
2232
2233 if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
2234 NL_SET_ERR_MSG(extack, "ipv4: Invalid data after header in netconf dump request");
2235 return -EINVAL;
2236 }
2237 }
2238
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002239 s_h = cb->args[0];
2240 s_idx = idx = cb->args[1];
2241
2242 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2243 idx = 0;
2244 head = &net->dev_index_head[h];
2245 rcu_read_lock();
Nicolas Dichtel04652772013-03-22 06:28:42 +00002246 cb->seq = atomic_read(&net->ipv4.dev_addr_genid) ^
2247 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002248 hlist_for_each_entry_rcu(dev, head, index_hlist) {
2249 if (idx < s_idx)
2250 goto cont;
2251 in_dev = __in_dev_get_rcu(dev);
2252 if (!in_dev)
2253 goto cont;
2254
2255 if (inet_netconf_fill_devconf(skb, dev->ifindex,
2256 &in_dev->cnf,
2257 NETLINK_CB(cb->skb).portid,
David Ahernaddd3832018-10-07 20:16:41 -07002258 nlh->nlmsg_seq,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002259 RTM_NEWNETCONF,
2260 NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +00002261 NETCONFA_ALL) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002262 rcu_read_unlock();
2263 goto done;
2264 }
Nicolas Dichtel04652772013-03-22 06:28:42 +00002265 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002266cont:
2267 idx++;
2268 }
2269 rcu_read_unlock();
2270 }
2271 if (h == NETDEV_HASHENTRIES) {
2272 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
2273 net->ipv4.devconf_all,
2274 NETLINK_CB(cb->skb).portid,
David Ahernaddd3832018-10-07 20:16:41 -07002275 nlh->nlmsg_seq,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002276 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +00002277 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002278 goto done;
2279 else
2280 h++;
2281 }
2282 if (h == NETDEV_HASHENTRIES + 1) {
2283 if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
2284 net->ipv4.devconf_dflt,
2285 NETLINK_CB(cb->skb).portid,
David Ahernaddd3832018-10-07 20:16:41 -07002286 nlh->nlmsg_seq,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002287 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +00002288 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +00002289 goto done;
2290 else
2291 h++;
2292 }
2293done:
2294 cb->args[0] = h;
2295 cb->args[1] = idx;
2296
2297 return skb->len;
2298}
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300#ifdef CONFIG_SYSCTL
2301
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002302static void devinet_copy_dflt_conf(struct net *net, int i)
Herbert Xu31be3082007-06-04 23:35:37 -07002303{
2304 struct net_device *dev;
2305
Eric Dumazetc6d14c82009-11-04 05:43:23 -08002306 rcu_read_lock();
2307 for_each_netdev_rcu(net, dev) {
Herbert Xu31be3082007-06-04 23:35:37 -07002308 struct in_device *in_dev;
Eric Dumazetc6d14c82009-11-04 05:43:23 -08002309
Herbert Xu31be3082007-06-04 23:35:37 -07002310 in_dev = __in_dev_get_rcu(dev);
2311 if (in_dev && !test_bit(i, in_dev->cnf.state))
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -08002312 in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i];
Herbert Xu31be3082007-06-04 23:35:37 -07002313 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08002314 rcu_read_unlock();
Herbert Xu31be3082007-06-04 23:35:37 -07002315}
2316
Eric Dumazetc6d14c82009-11-04 05:43:23 -08002317/* called with RTNL locked */
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002318static void inet_forward_change(struct net *net)
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002319{
2320 struct net_device *dev;
Pavel Emelyanov586f1212007-12-16 13:32:48 -08002321 int on = IPV4_DEVCONF_ALL(net, FORWARDING);
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002322
Pavel Emelyanov586f1212007-12-16 13:32:48 -08002323 IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on;
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -08002324 IPV4_DEVCONF_DFLT(net, FORWARDING) = on;
David Ahern3b022862017-03-28 14:28:02 -07002325 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2326 NETCONFA_FORWARDING,
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002327 NETCONFA_IFINDEX_ALL,
2328 net->ipv4.devconf_all);
David Ahern3b022862017-03-28 14:28:02 -07002329 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2330 NETCONFA_FORWARDING,
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002331 NETCONFA_IFINDEX_DEFAULT,
2332 net->ipv4.devconf_dflt);
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002333
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002334 for_each_netdev(net, dev) {
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002335 struct in_device *in_dev;
Eric Dumazetfa178062016-07-08 05:18:24 +02002336
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002337 if (on)
2338 dev_disable_lro(dev);
Eric Dumazetfa178062016-07-08 05:18:24 +02002339
2340 in_dev = __in_dev_get_rtnl(dev);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002341 if (in_dev) {
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002342 IN_DEV_CONF_SET(in_dev, FORWARDING, on);
David Ahern3b022862017-03-28 14:28:02 -07002343 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2344 NETCONFA_FORWARDING,
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002345 dev->ifindex, &in_dev->cnf);
2346 }
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002347 }
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002348}
2349
stephen hemmingerf085ff12013-12-12 13:06:50 -08002350static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf)
2351{
2352 if (cnf == net->ipv4.devconf_dflt)
2353 return NETCONFA_IFINDEX_DEFAULT;
2354 else if (cnf == net->ipv4.devconf_all)
2355 return NETCONFA_IFINDEX_ALL;
2356 else {
2357 struct in_device *idev
2358 = container_of(cnf, struct in_device, cnf);
2359 return idev->dev->ifindex;
2360 }
2361}
2362
Joe Perchesfe2c6332013-06-11 23:04:25 -07002363static int devinet_conf_proc(struct ctl_table *ctl, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002364 void __user *buffer,
Herbert Xu31be3082007-06-04 23:35:37 -07002365 size_t *lenp, loff_t *ppos)
2366{
Peter Pan(潘卫平)d01ff0a2011-12-01 15:47:06 +00002367 int old_value = *(int *)ctl->data;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002368 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Peter Pan(潘卫平)d01ff0a2011-12-01 15:47:06 +00002369 int new_value = *(int *)ctl->data;
Herbert Xu31be3082007-06-04 23:35:37 -07002370
2371 if (write) {
2372 struct ipv4_devconf *cnf = ctl->extra1;
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002373 struct net *net = ctl->extra2;
Herbert Xu31be3082007-06-04 23:35:37 -07002374 int i = (int *)ctl->data - cnf->data;
stephen hemmingerf085ff12013-12-12 13:06:50 -08002375 int ifindex;
Herbert Xu31be3082007-06-04 23:35:37 -07002376
2377 set_bit(i, cnf->state);
2378
Pavel Emelyanov9355bbd2007-12-16 13:32:16 -08002379 if (cnf == net->ipv4.devconf_dflt)
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002380 devinet_copy_dflt_conf(net, i);
Thomas Grafd0daebc32012-06-12 00:44:01 +00002381 if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1 ||
2382 i == IPV4_DEVCONF_ROUTE_LOCALNET - 1)
Peter Pan(潘卫平)d01ff0a2011-12-01 15:47:06 +00002383 if ((new_value == 0) && (old_value != 0))
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00002384 rt_cache_flush(net);
stephen hemmingerf085ff12013-12-12 13:06:50 -08002385
Xin Long5cbf7772018-07-27 16:37:28 +08002386 if (i == IPV4_DEVCONF_BC_FORWARDING - 1 &&
2387 new_value != old_value)
2388 rt_cache_flush(net);
2389
Nicolas Dichtelcc535df2012-10-29 04:53:27 +00002390 if (i == IPV4_DEVCONF_RP_FILTER - 1 &&
2391 new_value != old_value) {
stephen hemmingerf085ff12013-12-12 13:06:50 -08002392 ifindex = devinet_conf_ifindex(net, cnf);
David Ahern3b022862017-03-28 14:28:02 -07002393 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2394 NETCONFA_RP_FILTER,
Nicolas Dichtelcc535df2012-10-29 04:53:27 +00002395 ifindex, cnf);
2396 }
stephen hemmingerf085ff12013-12-12 13:06:50 -08002397 if (i == IPV4_DEVCONF_PROXY_ARP - 1 &&
2398 new_value != old_value) {
2399 ifindex = devinet_conf_ifindex(net, cnf);
David Ahern3b022862017-03-28 14:28:02 -07002400 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2401 NETCONFA_PROXY_NEIGH,
stephen hemmingerf085ff12013-12-12 13:06:50 -08002402 ifindex, cnf);
2403 }
Andy Gospodarek974d7af2015-07-07 13:56:57 -04002404 if (i == IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN - 1 &&
2405 new_value != old_value) {
2406 ifindex = devinet_conf_ifindex(net, cnf);
David Ahern3b022862017-03-28 14:28:02 -07002407 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2408 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
Andy Gospodarek974d7af2015-07-07 13:56:57 -04002409 ifindex, cnf);
2410 }
Herbert Xu31be3082007-06-04 23:35:37 -07002411 }
2412
2413 return ret;
2414}
2415
Joe Perchesfe2c6332013-06-11 23:04:25 -07002416static int devinet_sysctl_forward(struct ctl_table *ctl, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002417 void __user *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 size_t *lenp, loff_t *ppos)
2419{
2420 int *valp = ctl->data;
2421 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00002422 loff_t pos = *ppos;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002423 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 if (write && *valp != val) {
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002426 struct net *net = ctl->extra2;
2427
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002428 if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) {
Eric W. Biederman88af1822010-02-19 13:22:59 +00002429 if (!rtnl_trylock()) {
2430 /* Restore the original values before restarting */
2431 *valp = val;
2432 *ppos = pos;
Eric W. Biederman9b8adb52009-05-13 16:59:21 +00002433 return restart_syscall();
Eric W. Biederman88af1822010-02-19 13:22:59 +00002434 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002435 if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) {
2436 inet_forward_change(net);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002437 } else {
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002438 struct ipv4_devconf *cnf = ctl->extra1;
2439 struct in_device *idev =
2440 container_of(cnf, struct in_device, cnf);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002441 if (*valp)
2442 dev_disable_lro(idev->dev);
David Ahern3b022862017-03-28 14:28:02 -07002443 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002444 NETCONFA_FORWARDING,
2445 idev->dev->ifindex,
2446 cnf);
Ben Hutchings0187bdf2008-06-19 16:15:47 -07002447 }
2448 rtnl_unlock();
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00002449 rt_cache_flush(net);
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002450 } else
David Ahern3b022862017-03-28 14:28:02 -07002451 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
2452 NETCONFA_FORWARDING,
Nicolas Dichteledc9e742012-10-25 22:28:52 +00002453 NETCONFA_IFINDEX_DEFAULT,
2454 net->ipv4.devconf_dflt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
2456
2457 return ret;
2458}
2459
Joe Perchesfe2c6332013-06-11 23:04:25 -07002460static int ipv4_doint_and_flush(struct ctl_table *ctl, int write,
David S. Miller323e1262010-12-12 21:55:08 -08002461 void __user *buffer,
2462 size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 int *valp = ctl->data;
2465 int val = *valp;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002466 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -07002467 struct net *net = ctl->extra2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 if (write && *valp != val)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00002470 rt_cache_flush(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 return ret;
2473}
2474
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002475#define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \
Herbert Xu42f811b2007-06-04 23:34:44 -07002476 { \
Herbert Xu42f811b2007-06-04 23:34:44 -07002477 .procname = name, \
2478 .data = ipv4_devconf.data + \
Eric W. Biederman02291682010-02-14 03:25:51 +00002479 IPV4_DEVCONF_ ## attr - 1, \
Herbert Xu42f811b2007-06-04 23:34:44 -07002480 .maxlen = sizeof(int), \
2481 .mode = mval, \
2482 .proc_handler = proc, \
Herbert Xu31be3082007-06-04 23:35:37 -07002483 .extra1 = &ipv4_devconf, \
Herbert Xu42f811b2007-06-04 23:34:44 -07002484 }
2485
2486#define DEVINET_SYSCTL_RW_ENTRY(attr, name) \
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002487 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc)
Herbert Xu42f811b2007-06-04 23:34:44 -07002488
2489#define DEVINET_SYSCTL_RO_ENTRY(attr, name) \
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002490 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc)
Herbert Xu42f811b2007-06-04 23:34:44 -07002491
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002492#define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \
2493 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc)
Herbert Xu42f811b2007-06-04 23:34:44 -07002494
2495#define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002496 DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush)
Herbert Xu42f811b2007-06-04 23:34:44 -07002497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498static struct devinet_sysctl_table {
2499 struct ctl_table_header *sysctl_header;
Eric W. Biederman02291682010-02-14 03:25:51 +00002500 struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501} devinet_sysctl = {
2502 .devinet_vars = {
Herbert Xu42f811b2007-06-04 23:34:44 -07002503 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding",
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08002504 devinet_sysctl_forward),
Herbert Xu42f811b2007-06-04 23:34:44 -07002505 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"),
Xin Long5cbf7772018-07-27 16:37:28 +08002506 DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"),
Herbert Xu42f811b2007-06-04 23:34:44 -07002507
2508 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"),
2509 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"),
2510 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"),
2511 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"),
2512 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"),
2513 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE,
2514 "accept_source_route"),
Patrick McHardy8153a102009-12-03 01:25:58 +00002515 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"),
Jamal Hadi Salim28f6aee2009-12-25 17:30:22 -08002516 DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"),
Herbert Xu42f811b2007-06-04 23:34:44 -07002517 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"),
2518 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"),
2519 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"),
2520 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"),
2521 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"),
2522 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"),
2523 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"),
2524 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"),
2525 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"),
Stephen Hemmingereefef1c2009-02-01 01:04:33 -08002526 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"),
Jesper Dangaard Brouer65324142010-01-05 05:50:47 +00002527 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"),
William Manley5c6fe012013-08-06 19:03:14 +01002528 DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION,
2529 "force_igmp_version"),
William Manley26900482013-08-06 19:03:15 +01002530 DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL,
2531 "igmpv2_unsolicited_report_interval"),
2532 DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL,
2533 "igmpv3_unsolicited_report_interval"),
Andy Gospodarek0eeb0752015-06-23 13:45:37 -04002534 DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN,
2535 "ignore_routes_with_linkdown"),
Johannes Berg97daf332016-02-04 13:31:18 +01002536 DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP,
2537 "drop_gratuitous_arp"),
Herbert Xu42f811b2007-06-04 23:34:44 -07002538
2539 DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"),
2540 DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"),
Herbert Xu42f811b2007-06-04 23:34:44 -07002541 DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES,
2542 "promote_secondaries"),
Thomas Grafd0daebc32012-06-12 00:44:01 +00002543 DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET,
2544 "route_localnet"),
Johannes Berg12b74df2016-02-04 13:31:17 +01002545 DEVINET_SYSCTL_FLUSHING_ENTRY(DROP_UNICAST_IN_L2_MULTICAST,
2546 "drop_unicast_in_l2_multicast"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548};
2549
Pavel Emelyanovea40b322007-12-16 13:30:07 -08002550static int __devinet_sysctl_register(struct net *net, char *dev_name,
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002551 int ifindex, struct ipv4_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
2553 int i;
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002554 struct devinet_sysctl_table *t;
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002555 char path[sizeof("net/ipv4/conf/") + IFNAMSIZ];
Pavel Emelyanovbfada692007-12-02 00:57:08 +11002556
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002557 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (!t)
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002559 goto out;
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) {
2562 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf;
Herbert Xu31be3082007-06-04 23:35:37 -07002563 t->devinet_vars[i].extra1 = p;
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002564 t->devinet_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
2566
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002567 snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002569 t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 if (!t->sysctl_header)
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002571 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 p->sysctl = t;
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002574
David Ahern3b022862017-03-28 14:28:02 -07002575 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
2576 ifindex, p);
Pavel Emelyanovea40b322007-12-16 13:30:07 -08002577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002579free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 kfree(t);
Pavel Emelyanov9fa89642007-12-02 00:17:46 +11002581out:
Pavel Emelyanovea40b322007-12-16 13:30:07 -08002582 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
David Ahernb5c96412017-03-28 14:28:03 -07002585static void __devinet_sysctl_unregister(struct net *net,
2586 struct ipv4_devconf *cnf, int ifindex)
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002587{
2588 struct devinet_sysctl_table *t = cnf->sysctl;
2589
David Ahernb5c96412017-03-28 14:28:03 -07002590 if (t) {
2591 cnf->sysctl = NULL;
2592 unregister_net_sysctl_table(t->sysctl_header);
2593 kfree(t);
2594 }
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002595
David Ahernb5c96412017-03-28 14:28:03 -07002596 inet_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002597}
2598
WANG Cong20e61da2014-07-25 15:25:08 -07002599static int devinet_sysctl_register(struct in_device *idev)
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11002600{
WANG Cong20e61da2014-07-25 15:25:08 -07002601 int err;
2602
2603 if (!sysctl_dev_name_is_allowed(idev->dev->name))
2604 return -EINVAL;
2605
2606 err = neigh_sysctl_register(idev->dev, idev->arp_parms, NULL);
2607 if (err)
2608 return err;
2609 err = __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name,
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002610 idev->dev->ifindex, &idev->cnf);
WANG Cong20e61da2014-07-25 15:25:08 -07002611 if (err)
2612 neigh_sysctl_unregister(idev->arp_parms);
2613 return err;
Pavel Emelyanov66f27a52007-12-02 00:55:54 +11002614}
2615
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002616static void devinet_sysctl_unregister(struct in_device *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
David Ahernb5c96412017-03-28 14:28:03 -07002618 struct net *net = dev_net(idev->dev);
2619
2620 __devinet_sysctl_unregister(net, &idev->cnf, idev->dev->ifindex);
Pavel Emelyanov51602b22007-12-11 02:17:40 -08002621 neigh_sysctl_unregister(idev->arp_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002624static struct ctl_table ctl_forward_entry[] = {
2625 {
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002626 .procname = "ip_forward",
2627 .data = &ipv4_devconf.data[
Eric W. Biederman02291682010-02-14 03:25:51 +00002628 IPV4_DEVCONF_FORWARDING - 1],
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002629 .maxlen = sizeof(int),
2630 .mode = 0644,
2631 .proc_handler = devinet_sysctl_forward,
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002632 .extra1 = &ipv4_devconf,
Pavel Emelyanovc0ce9fb2007-12-16 13:31:14 -08002633 .extra2 = &init_net,
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002634 },
2635 { },
2636};
Eric Dumazet2a75de02008-01-05 23:08:49 -08002637#endif
Pavel Emelyanov68dd2992007-12-05 01:44:58 -08002638
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002639static __net_init int devinet_init_net(struct net *net)
2640{
2641 int err;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002642 struct ipv4_devconf *all, *dflt;
Eric Dumazet2a75de02008-01-05 23:08:49 -08002643#ifdef CONFIG_SYSCTL
Cong Wang856c3952019-01-17 23:27:11 -08002644 struct ctl_table *tbl;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002645 struct ctl_table_header *forw_hdr;
Eric Dumazet2a75de02008-01-05 23:08:49 -08002646#endif
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002647
2648 err = -ENOMEM;
Cong Wang856c3952019-01-17 23:27:11 -08002649 all = kmemdup(&ipv4_devconf, sizeof(ipv4_devconf), GFP_KERNEL);
2650 if (!all)
2651 goto err_alloc_all;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002652
Cong Wang856c3952019-01-17 23:27:11 -08002653 dflt = kmemdup(&ipv4_devconf_dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
2654 if (!dflt)
2655 goto err_alloc_dflt;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002656
Eric Dumazet2a75de02008-01-05 23:08:49 -08002657#ifdef CONFIG_SYSCTL
Cong Wang856c3952019-01-17 23:27:11 -08002658 tbl = kmemdup(ctl_forward_entry, sizeof(ctl_forward_entry), GFP_KERNEL);
2659 if (!tbl)
2660 goto err_alloc_ctl;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002661
Cong Wang856c3952019-01-17 23:27:11 -08002662 tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
2663 tbl[0].extra1 = all;
2664 tbl[0].extra2 = net;
Eric Dumazet2a75de02008-01-05 23:08:49 -08002665#endif
Cong Wang856c3952019-01-17 23:27:11 -08002666
Arnd Bergmanna154d5d2019-03-04 21:38:03 +01002667 if ((!IS_ENABLED(CONFIG_SYSCTL) ||
2668 sysctl_devconf_inherit_init_net != 2) &&
2669 !net_eq(net, &init_net)) {
Cong Wang856c3952019-01-17 23:27:11 -08002670 memcpy(all, init_net.ipv4.devconf_all, sizeof(ipv4_devconf));
2671 memcpy(dflt, init_net.ipv4.devconf_dflt, sizeof(ipv4_devconf_dflt));
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002672 }
2673
2674#ifdef CONFIG_SYSCTL
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002675 err = __devinet_sysctl_register(net, "all", NETCONFA_IFINDEX_ALL, all);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002676 if (err < 0)
2677 goto err_reg_all;
2678
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02002679 err = __devinet_sysctl_register(net, "default",
2680 NETCONFA_IFINDEX_DEFAULT, dflt);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002681 if (err < 0)
2682 goto err_reg_dflt;
2683
2684 err = -ENOMEM;
Eric W. Biederman8607ddb2012-04-19 13:42:09 +00002685 forw_hdr = register_net_sysctl(net, "net/ipv4", tbl);
Ian Morris51456b22015-04-03 09:17:26 +01002686 if (!forw_hdr)
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002687 goto err_reg_ctl;
Eric Dumazet2a75de02008-01-05 23:08:49 -08002688 net->ipv4.forw_hdr = forw_hdr;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002689#endif
2690
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002691 net->ipv4.devconf_all = all;
2692 net->ipv4.devconf_dflt = dflt;
2693 return 0;
2694
2695#ifdef CONFIG_SYSCTL
2696err_reg_ctl:
David Ahernb5c96412017-03-28 14:28:03 -07002697 __devinet_sysctl_unregister(net, dflt, NETCONFA_IFINDEX_DEFAULT);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002698err_reg_dflt:
David Ahernb5c96412017-03-28 14:28:03 -07002699 __devinet_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002700err_reg_all:
Cong Wang856c3952019-01-17 23:27:11 -08002701 kfree(tbl);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002702err_alloc_ctl:
Eric Dumazet2a75de02008-01-05 23:08:49 -08002703#endif
Cong Wang856c3952019-01-17 23:27:11 -08002704 kfree(dflt);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002705err_alloc_dflt:
Cong Wang856c3952019-01-17 23:27:11 -08002706 kfree(all);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002707err_alloc_all:
2708 return err;
2709}
2710
2711static __net_exit void devinet_exit_net(struct net *net)
2712{
Eric Dumazet2a75de02008-01-05 23:08:49 -08002713#ifdef CONFIG_SYSCTL
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002714 struct ctl_table *tbl;
2715
2716 tbl = net->ipv4.forw_hdr->ctl_table_arg;
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002717 unregister_net_sysctl_table(net->ipv4.forw_hdr);
David Ahernb5c96412017-03-28 14:28:03 -07002718 __devinet_sysctl_unregister(net, net->ipv4.devconf_dflt,
2719 NETCONFA_IFINDEX_DEFAULT);
2720 __devinet_sysctl_unregister(net, net->ipv4.devconf_all,
2721 NETCONFA_IFINDEX_ALL);
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002722 kfree(tbl);
Eric Dumazet2a75de02008-01-05 23:08:49 -08002723#endif
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002724 kfree(net->ipv4.devconf_dflt);
2725 kfree(net->ipv4.devconf_all);
2726}
2727
2728static __net_initdata struct pernet_operations devinet_ops = {
2729 .init = devinet_init_net,
2730 .exit = devinet_exit_net,
2731};
2732
Daniel Borkmann207895fd32015-01-29 12:15:03 +01002733static struct rtnl_af_ops inet_af_ops __read_mostly = {
Thomas Graf9f0f7272010-11-16 04:32:48 +00002734 .family = AF_INET,
2735 .fill_link_af = inet_fill_link_af,
2736 .get_link_af_size = inet_get_link_af_size,
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002737 .validate_link_af = inet_validate_link_af,
2738 .set_link_af = inet_set_link_af,
Thomas Graf9f0f7272010-11-16 04:32:48 +00002739};
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741void __init devinet_init(void)
2742{
David S. Millerfd23c3b2011-02-18 12:42:28 -08002743 int i;
2744
2745 for (i = 0; i < IN4_ADDR_HSIZE; i++)
2746 INIT_HLIST_HEAD(&inet_addr_lst[i]);
2747
Pavel Emelyanov752d14d2007-12-16 13:31:47 -08002748 register_pernet_subsys(&devinet_ops);
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 register_gifconf(PF_INET, inet_gifconf);
2751 register_netdevice_notifier(&ip_netdev_notifier);
Thomas Graf63f34442007-03-22 11:55:17 -07002752
viresh kumar906e0732014-01-22 12:23:32 +05302753 queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0);
Jiri Pirko5c766d62013-01-24 09:41:41 +00002754
Thomas Graf9f0f7272010-11-16 04:32:48 +00002755 rtnl_af_register(&inet_af_ops);
2756
Florian Westphalb97bac62017-08-09 20:41:48 +02002757 rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, 0);
2758 rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, 0);
2759 rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, 0);
Nicolas Dichtel9e551112012-10-25 22:28:53 +00002760 rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf,
Florian Westphalb97bac62017-08-09 20:41:48 +02002761 inet_netconf_dump_devconf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}