Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * NET3 IP device support routines. |
| 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Derived from the IP parts of dev.c 1.0.19 |
Jesper Juhl | 02c30a8 | 2005-05-05 16:16:16 -0700 | [diff] [blame] | 6 | * Authors: Ross Biro |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 25 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/bitops.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 27 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/kernel.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 31 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #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 Graf | 1823730 | 2006-08-04 23:04:54 -0700 | [diff] [blame] | 39 | #include <linux/if_addr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/init.h> |
| 46 | #include <linux/notifier.h> |
| 47 | #include <linux/inetdevice.h> |
| 48 | #include <linux/igmp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 49 | #include <linux/slab.h> |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 50 | #include <linux/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #ifdef CONFIG_SYSCTL |
| 52 | #include <linux/sysctl.h> |
| 53 | #endif |
| 54 | #include <linux/kmod.h> |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 55 | #include <linux/netconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 57 | #include <net/arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <net/ip.h> |
| 59 | #include <net/route.h> |
| 60 | #include <net/ip_fib.h> |
Thomas Graf | 63f3444 | 2007-03-22 11:55:17 -0700 | [diff] [blame] | 61 | #include <net/rtnetlink.h> |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 62 | #include <net/net_namespace.h> |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 63 | #include <net/addrconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Adrian Bunk | 0027ba8 | 2008-01-31 17:17:31 -0800 | [diff] [blame] | 65 | static struct ipv4_devconf ipv4_devconf = { |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 66 | .data = { |
Eric W. Biederman | 0229168 | 2010-02-14 03:25:51 +0000 | [diff] [blame] | 67 | [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1, |
| 68 | [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1, |
| 69 | [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1, |
| 70 | [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1, |
William Manley | 2690048 | 2013-08-06 19:03:15 +0100 | [diff] [blame] | 71 | [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/, |
| 72 | [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/, |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 73 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | static struct ipv4_devconf ipv4_devconf_dflt = { |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 77 | .data = { |
Eric W. Biederman | 0229168 | 2010-02-14 03:25:51 +0000 | [diff] [blame] | 78 | [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1, |
| 79 | [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1, |
| 80 | [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1, |
| 81 | [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1, |
| 82 | [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1, |
William Manley | 2690048 | 2013-08-06 19:03:15 +0100 | [diff] [blame] | 83 | [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/, |
| 84 | [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/, |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 85 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Pavel Emelyanov | 9355bbd | 2007-12-16 13:32:16 -0800 | [diff] [blame] | 88 | #define IPV4_DEVCONF_DFLT(net, attr) \ |
| 89 | IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr) |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 90 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 91 | static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = { |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 92 | [IFA_LOCAL] = { .type = NLA_U32 }, |
| 93 | [IFA_ADDRESS] = { .type = NLA_U32 }, |
| 94 | [IFA_BROADCAST] = { .type = NLA_U32 }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 95 | [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 96 | [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, |
Jiri Pirko | ad6c813 | 2013-12-08 12:16:10 +0100 | [diff] [blame] | 97 | [IFA_FLAGS] = { .type = NLA_U32 }, |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 98 | [IFA_RT_PRIORITY] = { .type = NLA_U32 }, |
Christian Brauner | d380714 | 2018-09-04 21:53:49 +0200 | [diff] [blame] | 99 | [IFA_TARGET_NETNSID] = { .type = NLA_S32 }, |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Christian Brauner | 978a46f | 2018-09-04 21:53:54 +0200 | [diff] [blame] | 102 | struct inet_fill_args { |
| 103 | u32 portid; |
| 104 | u32 seq; |
| 105 | int event; |
| 106 | unsigned int flags; |
| 107 | int netnsid; |
David Ahern | 5fcd266 | 2018-10-19 12:45:29 -0700 | [diff] [blame] | 108 | int ifindex; |
Christian Brauner | 978a46f | 2018-09-04 21:53:54 +0200 | [diff] [blame] | 109 | }; |
| 110 | |
Eric Dumazet | 4038499 | 2012-08-03 21:06:50 +0000 | [diff] [blame] | 111 | #define IN4_ADDR_HSIZE_SHIFT 8 |
| 112 | #define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT) |
| 113 | |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 114 | static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE]; |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 115 | |
Eric Dumazet | 6eada01 | 2015-03-18 14:05:33 -0700 | [diff] [blame] | 116 | static u32 inet_addr_hash(const struct net *net, __be32 addr) |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 117 | { |
Eric Dumazet | 4038499 | 2012-08-03 21:06:50 +0000 | [diff] [blame] | 118 | u32 val = (__force u32) addr ^ net_hash_mix(net); |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 119 | |
Eric Dumazet | 4038499 | 2012-08-03 21:06:50 +0000 | [diff] [blame] | 120 | return hash_32(val, IN4_ADDR_HSIZE_SHIFT); |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa) |
| 124 | { |
Eric Dumazet | 4038499 | 2012-08-03 21:06:50 +0000 | [diff] [blame] | 125 | u32 hash = inet_addr_hash(net, ifa->ifa_local); |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 126 | |
WANG Cong | 32a4be4 | 2014-05-06 11:15:56 -0700 | [diff] [blame] | 127 | ASSERT_RTNL(); |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 128 | hlist_add_head_rcu(&ifa->hash, &inet_addr_lst[hash]); |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void inet_hash_remove(struct in_ifaddr *ifa) |
| 132 | { |
WANG Cong | 32a4be4 | 2014-05-06 11:15:56 -0700 | [diff] [blame] | 133 | ASSERT_RTNL(); |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 134 | hlist_del_init_rcu(&ifa->hash); |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 135 | } |
| 136 | |
David S. Miller | 9435eb1 | 2011-02-18 12:43:09 -0800 | [diff] [blame] | 137 | /** |
| 138 | * __ip_dev_find - find the first device with a given source address. |
| 139 | * @net: the net namespace |
| 140 | * @addr: the source address |
| 141 | * @devref: if true, take a reference on the found device |
| 142 | * |
| 143 | * If a caller uses devref=false, it should be protected by RCU, or RTNL |
| 144 | */ |
| 145 | struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref) |
| 146 | { |
David S. Miller | 9435eb1 | 2011-02-18 12:43:09 -0800 | [diff] [blame] | 147 | struct net_device *result = NULL; |
| 148 | struct in_ifaddr *ifa; |
David S. Miller | 9435eb1 | 2011-02-18 12:43:09 -0800 | [diff] [blame] | 149 | |
| 150 | rcu_read_lock(); |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 151 | ifa = inet_lookup_ifaddr_rcu(net, addr); |
| 152 | if (!ifa) { |
David S. Miller | 406b6f9 | 2011-03-22 21:56:23 -0700 | [diff] [blame] | 153 | struct flowi4 fl4 = { .daddr = addr }; |
| 154 | struct fib_result res = { 0 }; |
| 155 | struct fib_table *local; |
| 156 | |
| 157 | /* Fallback to FIB local table so that communication |
| 158 | * over loopback subnets work. |
| 159 | */ |
| 160 | local = fib_get_table(net, RT_TABLE_LOCAL); |
| 161 | if (local && |
| 162 | !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) && |
| 163 | res.type == RTN_LOCAL) |
| 164 | result = FIB_RES_DEV(res); |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 165 | } else { |
| 166 | result = ifa->ifa_dev->dev; |
David S. Miller | 406b6f9 | 2011-03-22 21:56:23 -0700 | [diff] [blame] | 167 | } |
David S. Miller | 9435eb1 | 2011-02-18 12:43:09 -0800 | [diff] [blame] | 168 | if (result && devref) |
| 169 | dev_hold(result); |
| 170 | rcu_read_unlock(); |
| 171 | return result; |
| 172 | } |
| 173 | EXPORT_SYMBOL(__ip_dev_find); |
| 174 | |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 175 | /* called under RCU lock */ |
| 176 | struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr) |
| 177 | { |
| 178 | u32 hash = inet_addr_hash(net, addr); |
| 179 | struct in_ifaddr *ifa; |
| 180 | |
| 181 | hlist_for_each_entry_rcu(ifa, &inet_addr_lst[hash], hash) |
| 182 | if (ifa->ifa_local == addr && |
| 183 | net_eq(dev_net(ifa->ifa_dev->dev), net)) |
| 184 | return ifa; |
| 185 | |
| 186 | return NULL; |
| 187 | } |
| 188 | |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 189 | static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 191 | static BLOCKING_NOTIFIER_HEAD(inetaddr_chain); |
Krister Johansen | 3ad7d24 | 2017-06-08 13:12:14 -0700 | [diff] [blame] | 192 | static BLOCKING_NOTIFIER_HEAD(inetaddr_validator_chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, |
| 194 | int destroy); |
| 195 | #ifdef CONFIG_SYSCTL |
WANG Cong | 20e61da | 2014-07-25 15:25:08 -0700 | [diff] [blame] | 196 | static int devinet_sysctl_register(struct in_device *idev); |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 197 | static void devinet_sysctl_unregister(struct in_device *idev); |
| 198 | #else |
WANG Cong | 20e61da | 2014-07-25 15:25:08 -0700 | [diff] [blame] | 199 | static int devinet_sysctl_register(struct in_device *idev) |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 200 | { |
WANG Cong | 20e61da | 2014-07-25 15:25:08 -0700 | [diff] [blame] | 201 | return 0; |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 202 | } |
Eric Dumazet | 4038499 | 2012-08-03 21:06:50 +0000 | [diff] [blame] | 203 | static void devinet_sysctl_unregister(struct in_device *idev) |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 204 | { |
| 205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | #endif |
| 207 | |
| 208 | /* Locks all the inet devices. */ |
| 209 | |
| 210 | static struct in_ifaddr *inet_alloc_ifa(void) |
| 211 | { |
Alexey Dobriyan | 93adcc8 | 2008-10-28 13:25:09 -0700 | [diff] [blame] | 212 | return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static void inet_rcu_free_ifa(struct rcu_head *head) |
| 216 | { |
| 217 | struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head); |
| 218 | if (ifa->ifa_dev) |
| 219 | in_dev_put(ifa->ifa_dev); |
| 220 | kfree(ifa); |
| 221 | } |
| 222 | |
Eric Dumazet | 4038499 | 2012-08-03 21:06:50 +0000 | [diff] [blame] | 223 | static void inet_free_ifa(struct in_ifaddr *ifa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
| 225 | call_rcu(&ifa->rcu_head, inet_rcu_free_ifa); |
| 226 | } |
| 227 | |
| 228 | void in_dev_finish_destroy(struct in_device *idev) |
| 229 | { |
| 230 | struct net_device *dev = idev->dev; |
| 231 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 232 | WARN_ON(idev->ifa_list); |
| 233 | WARN_ON(idev->mc_list); |
Eric Dumazet | e989707 | 2013-06-07 08:48:57 -0700 | [diff] [blame] | 234 | kfree(rcu_dereference_protected(idev->mc_hash, 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | #ifdef NET_REFCNT_DEBUG |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 236 | pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | #endif |
| 238 | dev_put(dev); |
| 239 | if (!idev->dead) |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 240 | pr_err("Freeing alive in_device %p\n", idev); |
| 241 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | kfree(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 244 | EXPORT_SYMBOL(in_dev_finish_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 246 | static struct in_device *inetdev_init(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
| 248 | struct in_device *in_dev; |
WANG Cong | 20e61da | 2014-07-25 15:25:08 -0700 | [diff] [blame] | 249 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | ASSERT_RTNL(); |
| 252 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 253 | in_dev = kzalloc(sizeof(*in_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | if (!in_dev) |
| 255 | goto out; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 256 | memcpy(&in_dev->cnf, dev_net(dev)->ipv4.devconf_dflt, |
Pavel Emelyanov | 9355bbd | 2007-12-16 13:32:16 -0800 | [diff] [blame] | 257 | sizeof(in_dev->cnf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | in_dev->cnf.sysctl = NULL; |
| 259 | in_dev->dev = dev; |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 260 | in_dev->arp_parms = neigh_parms_alloc(dev, &arp_tbl); |
| 261 | if (!in_dev->arp_parms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | goto out_kfree; |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 263 | if (IPV4_DEVCONF(in_dev->cnf, FORWARDING)) |
| 264 | dev_disable_lro(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | /* Reference in_dev->dev */ |
| 266 | dev_hold(dev); |
David L Stevens | 30c4cf5 | 2007-01-04 12:31:14 -0800 | [diff] [blame] | 267 | /* Account for reference dev->ip_ptr (below) */ |
Reshetova, Elena | 7658b36 | 2017-06-30 13:08:03 +0300 | [diff] [blame] | 268 | refcount_set(&in_dev->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
WANG Cong | 20e61da | 2014-07-25 15:25:08 -0700 | [diff] [blame] | 270 | err = devinet_sysctl_register(in_dev); |
| 271 | if (err) { |
| 272 | in_dev->dead = 1; |
| 273 | in_dev_put(in_dev); |
| 274 | in_dev = NULL; |
| 275 | goto out; |
| 276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | ip_mc_init_dev(in_dev); |
| 278 | if (dev->flags & IFF_UP) |
| 279 | ip_mc_up(in_dev); |
Jarek Poplawski | 483479e | 2007-01-09 14:38:31 -0800 | [diff] [blame] | 280 | |
David L Stevens | 30c4cf5 | 2007-01-04 12:31:14 -0800 | [diff] [blame] | 281 | /* we can receive as soon as ip_ptr is set -- do this last */ |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 282 | rcu_assign_pointer(dev->ip_ptr, in_dev); |
Jarek Poplawski | 483479e | 2007-01-09 14:38:31 -0800 | [diff] [blame] | 283 | out: |
WANG Cong | 20e61da | 2014-07-25 15:25:08 -0700 | [diff] [blame] | 284 | return in_dev ?: ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | out_kfree: |
| 286 | kfree(in_dev); |
| 287 | in_dev = NULL; |
| 288 | goto out; |
| 289 | } |
| 290 | |
| 291 | static void in_dev_rcu_put(struct rcu_head *head) |
| 292 | { |
| 293 | struct in_device *idev = container_of(head, struct in_device, rcu_head); |
| 294 | in_dev_put(idev); |
| 295 | } |
| 296 | |
| 297 | static void inetdev_destroy(struct in_device *in_dev) |
| 298 | { |
| 299 | struct in_ifaddr *ifa; |
| 300 | struct net_device *dev; |
| 301 | |
| 302 | ASSERT_RTNL(); |
| 303 | |
| 304 | dev = in_dev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
| 306 | in_dev->dead = 1; |
| 307 | |
| 308 | ip_mc_destroy_dev(in_dev); |
| 309 | |
| 310 | while ((ifa = in_dev->ifa_list) != NULL) { |
| 311 | inet_del_ifa(in_dev, &in_dev->ifa_list, 0); |
| 312 | inet_free_ifa(ifa); |
| 313 | } |
| 314 | |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 315 | RCU_INIT_POINTER(dev->ip_ptr, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 317 | devinet_sysctl_unregister(in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | neigh_parms_release(&arp_tbl, in_dev->arp_parms); |
| 319 | arp_ifdown(dev); |
| 320 | |
| 321 | call_rcu(&in_dev->rcu_head, in_dev_rcu_put); |
| 322 | } |
| 323 | |
Al Viro | ff428d7 | 2006-09-26 22:13:35 -0700 | [diff] [blame] | 324 | int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
| 326 | rcu_read_lock(); |
| 327 | for_primary_ifa(in_dev) { |
| 328 | if (inet_ifa_match(a, ifa)) { |
| 329 | if (!b || inet_ifa_match(b, ifa)) { |
| 330 | rcu_read_unlock(); |
| 331 | return 1; |
| 332 | } |
| 333 | } |
| 334 | } endfor_ifa(in_dev); |
| 335 | rcu_read_unlock(); |
| 336 | return 0; |
| 337 | } |
| 338 | |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 339 | static void __inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 340 | int destroy, struct nlmsghdr *nlh, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
Harald Welte | 8f937c6 | 2005-05-29 20:23:46 -0700 | [diff] [blame] | 342 | struct in_ifaddr *promote = NULL; |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 343 | struct in_ifaddr *ifa, *ifa1 = *ifap; |
| 344 | struct in_ifaddr *last_prim = in_dev->ifa_list; |
| 345 | struct in_ifaddr *prev_prom = NULL; |
| 346 | int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | ASSERT_RTNL(); |
| 349 | |
David S. Miller | fbd40ea | 2016-03-13 23:28:00 -0400 | [diff] [blame] | 350 | if (in_dev->dead) |
| 351 | goto no_promotions; |
| 352 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 353 | /* 1. Deleting primary ifaddr forces deletion all secondaries |
Harald Welte | 8f937c6 | 2005-05-29 20:23:46 -0700 | [diff] [blame] | 354 | * unless alias promotion is set |
| 355 | **/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
| 357 | if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | struct in_ifaddr **ifap1 = &ifa1->ifa_next; |
| 359 | |
| 360 | while ((ifa = *ifap1) != NULL) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 361 | if (!(ifa->ifa_flags & IFA_F_SECONDARY) && |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 362 | ifa1->ifa_scope <= ifa->ifa_scope) |
| 363 | last_prim = ifa; |
| 364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | if (!(ifa->ifa_flags & IFA_F_SECONDARY) || |
| 366 | ifa1->ifa_mask != ifa->ifa_mask || |
| 367 | !inet_ifa_match(ifa1->ifa_address, ifa)) { |
| 368 | ifap1 = &ifa->ifa_next; |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 369 | prev_prom = ifa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | continue; |
| 371 | } |
| 372 | |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 373 | if (!do_promote) { |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 374 | inet_hash_remove(ifa); |
Harald Welte | 8f937c6 | 2005-05-29 20:23:46 -0700 | [diff] [blame] | 375 | *ifap1 = ifa->ifa_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 377 | rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 378 | blocking_notifier_call_chain(&inetaddr_chain, |
| 379 | NETDEV_DOWN, ifa); |
Harald Welte | 8f937c6 | 2005-05-29 20:23:46 -0700 | [diff] [blame] | 380 | inet_free_ifa(ifa); |
| 381 | } else { |
| 382 | promote = ifa; |
| 383 | break; |
| 384 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Julian Anastasov | 2d230e2 | 2011-03-19 12:13:52 +0000 | [diff] [blame] | 388 | /* On promotion all secondaries from subnet are changing |
| 389 | * the primary IP, we must remove all their routes silently |
| 390 | * and later to add them back with new prefsrc. Do this |
| 391 | * while all addresses are on the device list. |
| 392 | */ |
| 393 | for (ifa = promote; ifa; ifa = ifa->ifa_next) { |
| 394 | if (ifa1->ifa_mask == ifa->ifa_mask && |
| 395 | inet_ifa_match(ifa1->ifa_address, ifa)) |
| 396 | fib_del_ifaddr(ifa, ifa1); |
| 397 | } |
| 398 | |
David S. Miller | fbd40ea | 2016-03-13 23:28:00 -0400 | [diff] [blame] | 399 | no_promotions: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | /* 2. Unlink it */ |
| 401 | |
| 402 | *ifap = ifa1->ifa_next; |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 403 | inet_hash_remove(ifa1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | /* 3. Announce address deletion */ |
| 406 | |
| 407 | /* Send message first, then call notifier. |
| 408 | At first sight, FIB update triggered by notifier |
| 409 | will refer to already deleted ifaddr, that could confuse |
| 410 | netlink listeners. It is not true: look, gated sees |
| 411 | that route deleted and if it still thinks that ifaddr |
| 412 | is valid, it will try to restore deleted routes... Grr. |
| 413 | So that, this order is correct. |
| 414 | */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 415 | rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 416 | blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1); |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 417 | |
| 418 | if (promote) { |
Julian Anastasov | 04024b9 | 2011-03-19 12:13:54 +0000 | [diff] [blame] | 419 | struct in_ifaddr *next_sec = promote->ifa_next; |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 420 | |
| 421 | if (prev_prom) { |
| 422 | prev_prom->ifa_next = promote->ifa_next; |
| 423 | promote->ifa_next = last_prim->ifa_next; |
| 424 | last_prim->ifa_next = promote; |
| 425 | } |
| 426 | |
| 427 | promote->ifa_flags &= ~IFA_F_SECONDARY; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 428 | rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 429 | blocking_notifier_call_chain(&inetaddr_chain, |
| 430 | NETDEV_UP, promote); |
Julian Anastasov | 04024b9 | 2011-03-19 12:13:54 +0000 | [diff] [blame] | 431 | for (ifa = next_sec; ifa; ifa = ifa->ifa_next) { |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 432 | if (ifa1->ifa_mask != ifa->ifa_mask || |
| 433 | !inet_ifa_match(ifa1->ifa_address, ifa)) |
| 434 | continue; |
| 435 | fib_add_ifaddr(ifa); |
| 436 | } |
| 437 | |
| 438 | } |
Herbert Xu | 6363097 | 2007-06-07 18:35:38 -0700 | [diff] [blame] | 439 | if (destroy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | inet_free_ifa(ifa1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 443 | static void inet_del_ifa(struct in_device *in_dev, struct in_ifaddr **ifap, |
| 444 | int destroy) |
| 445 | { |
| 446 | __inet_del_ifa(in_dev, ifap, destroy, NULL, 0); |
| 447 | } |
| 448 | |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 449 | static void check_lifetime(struct work_struct *work); |
| 450 | |
| 451 | static DECLARE_DELAYED_WORK(check_lifetime_work, check_lifetime); |
| 452 | |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 453 | static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh, |
David Ahern | de95e04 | 2017-10-18 09:56:54 -0700 | [diff] [blame] | 454 | u32 portid, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
| 456 | struct in_device *in_dev = ifa->ifa_dev; |
| 457 | struct in_ifaddr *ifa1, **ifap, **last_primary; |
Krister Johansen | 3ad7d24 | 2017-06-08 13:12:14 -0700 | [diff] [blame] | 458 | struct in_validator_info ivi; |
| 459 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
| 461 | ASSERT_RTNL(); |
| 462 | |
| 463 | if (!ifa->ifa_local) { |
| 464 | inet_free_ifa(ifa); |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | ifa->ifa_flags &= ~IFA_F_SECONDARY; |
| 469 | last_primary = &in_dev->ifa_list; |
| 470 | |
| 471 | for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL; |
| 472 | ifap = &ifa1->ifa_next) { |
| 473 | if (!(ifa1->ifa_flags & IFA_F_SECONDARY) && |
| 474 | ifa->ifa_scope <= ifa1->ifa_scope) |
| 475 | last_primary = &ifa1->ifa_next; |
| 476 | if (ifa1->ifa_mask == ifa->ifa_mask && |
| 477 | inet_ifa_match(ifa1->ifa_address, ifa)) { |
| 478 | if (ifa1->ifa_local == ifa->ifa_local) { |
| 479 | inet_free_ifa(ifa); |
| 480 | return -EEXIST; |
| 481 | } |
| 482 | if (ifa1->ifa_scope != ifa->ifa_scope) { |
| 483 | inet_free_ifa(ifa); |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | ifa->ifa_flags |= IFA_F_SECONDARY; |
| 487 | } |
| 488 | } |
| 489 | |
Krister Johansen | 3ad7d24 | 2017-06-08 13:12:14 -0700 | [diff] [blame] | 490 | /* Allow any devices that wish to register ifaddr validtors to weigh |
| 491 | * in now, before changes are committed. The rntl lock is serializing |
| 492 | * access here, so the state should not change between a validator call |
| 493 | * and a final notify on commit. This isn't invoked on promotion under |
| 494 | * the assumption that validators are checking the address itself, and |
| 495 | * not the flags. |
| 496 | */ |
| 497 | ivi.ivi_addr = ifa->ifa_address; |
| 498 | ivi.ivi_dev = ifa->ifa_dev; |
David Ahern | de95e04 | 2017-10-18 09:56:54 -0700 | [diff] [blame] | 499 | ivi.extack = extack; |
Krister Johansen | 3ad7d24 | 2017-06-08 13:12:14 -0700 | [diff] [blame] | 500 | ret = blocking_notifier_call_chain(&inetaddr_validator_chain, |
| 501 | NETDEV_UP, &ivi); |
| 502 | ret = notifier_to_errno(ret); |
| 503 | if (ret) { |
| 504 | inet_free_ifa(ifa); |
| 505 | return ret; |
| 506 | } |
| 507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | if (!(ifa->ifa_flags & IFA_F_SECONDARY)) { |
Aruna-Hewapathirane | 63862b5 | 2014-01-11 07:15:59 -0500 | [diff] [blame] | 509 | prandom_seed((__force u32) ifa->ifa_local); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | ifap = last_primary; |
| 511 | } |
| 512 | |
| 513 | ifa->ifa_next = *ifap; |
| 514 | *ifap = ifa; |
| 515 | |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 516 | inet_hash_insert(dev_net(in_dev->dev), ifa); |
| 517 | |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 518 | cancel_delayed_work(&check_lifetime_work); |
viresh kumar | 906e073 | 2014-01-22 12:23:32 +0530 | [diff] [blame] | 519 | queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0); |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | /* Send message first, then call notifier. |
| 522 | Notifier will trigger FIB update, so that |
| 523 | listeners of netlink will know about new ifaddr */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 524 | rtmsg_ifa(RTM_NEWADDR, ifa, nlh, portid); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 525 | blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 530 | static int inet_insert_ifa(struct in_ifaddr *ifa) |
| 531 | { |
David Ahern | de95e04 | 2017-10-18 09:56:54 -0700 | [diff] [blame] | 532 | return __inet_insert_ifa(ifa, NULL, 0, NULL); |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa) |
| 536 | { |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 537 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
| 539 | ASSERT_RTNL(); |
| 540 | |
| 541 | if (!in_dev) { |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 542 | inet_free_ifa(ifa); |
| 543 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 545 | ipv4_devconf_setall(in_dev); |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 546 | neigh_parms_data_state_setall(in_dev->arp_parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | if (ifa->ifa_dev != in_dev) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 548 | WARN_ON(ifa->ifa_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | in_dev_hold(in_dev); |
| 550 | ifa->ifa_dev = in_dev; |
| 551 | } |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 552 | if (ipv4_is_loopback(ifa->ifa_local)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | ifa->ifa_scope = RT_SCOPE_HOST; |
| 554 | return inet_insert_ifa(ifa); |
| 555 | } |
| 556 | |
Eric Dumazet | 8723e1b | 2010-10-19 00:39:26 +0000 | [diff] [blame] | 557 | /* Caller must hold RCU or RTNL : |
| 558 | * We dont take a reference on found in_device |
| 559 | */ |
Denis V. Lunev | 7fee0ca | 2008-01-21 17:32:38 -0800 | [diff] [blame] | 560 | struct in_device *inetdev_by_index(struct net *net, int ifindex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { |
| 562 | struct net_device *dev; |
| 563 | struct in_device *in_dev = NULL; |
Eric Dumazet | c148fc2 | 2009-11-01 19:23:04 +0000 | [diff] [blame] | 564 | |
| 565 | rcu_read_lock(); |
| 566 | dev = dev_get_by_index_rcu(net, ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | if (dev) |
Eric Dumazet | 8723e1b | 2010-10-19 00:39:26 +0000 | [diff] [blame] | 568 | in_dev = rcu_dereference_rtnl(dev->ip_ptr); |
Eric Dumazet | c148fc2 | 2009-11-01 19:23:04 +0000 | [diff] [blame] | 569 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | return in_dev; |
| 571 | } |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 572 | EXPORT_SYMBOL(inetdev_by_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
| 574 | /* Called only from RTNL semaphored context. No locks. */ |
| 575 | |
Al Viro | 60cad5d | 2006-09-26 22:17:09 -0700 | [diff] [blame] | 576 | struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix, |
| 577 | __be32 mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
| 579 | ASSERT_RTNL(); |
| 580 | |
| 581 | for_primary_ifa(in_dev) { |
| 582 | if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa)) |
| 583 | return ifa; |
| 584 | } endfor_ifa(in_dev); |
| 585 | return NULL; |
| 586 | } |
| 587 | |
Madhu Challa | 93a714d | 2015-02-25 09:58:35 -0800 | [diff] [blame] | 588 | static int ip_mc_config(struct sock *sk, bool join, const struct in_ifaddr *ifa) |
| 589 | { |
| 590 | struct ip_mreqn mreq = { |
| 591 | .imr_multiaddr.s_addr = ifa->ifa_address, |
| 592 | .imr_ifindex = ifa->ifa_dev->dev->ifindex, |
| 593 | }; |
| 594 | int ret; |
| 595 | |
| 596 | ASSERT_RTNL(); |
| 597 | |
| 598 | lock_sock(sk); |
| 599 | if (join) |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 600 | ret = ip_mc_join_group(sk, &mreq); |
Madhu Challa | 93a714d | 2015-02-25 09:58:35 -0800 | [diff] [blame] | 601 | else |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 602 | ret = ip_mc_leave_group(sk, &mreq); |
Madhu Challa | 93a714d | 2015-02-25 09:58:35 -0800 | [diff] [blame] | 603 | release_sock(sk); |
| 604 | |
| 605 | return ret; |
| 606 | } |
| 607 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 608 | static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 609 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 611 | struct net *net = sock_net(skb->sk); |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 612 | struct nlattr *tb[IFA_MAX+1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | struct in_device *in_dev; |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 614 | struct ifaddrmsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | struct in_ifaddr *ifa, **ifap; |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 616 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
| 618 | ASSERT_RTNL(); |
| 619 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 620 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, |
| 621 | ifa_ipv4_policy, extack); |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 622 | if (err < 0) |
| 623 | goto errout; |
| 624 | |
| 625 | ifm = nlmsg_data(nlh); |
Denis V. Lunev | 7fee0ca | 2008-01-21 17:32:38 -0800 | [diff] [blame] | 626 | in_dev = inetdev_by_index(net, ifm->ifa_index); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 627 | if (!in_dev) { |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 628 | err = -ENODEV; |
| 629 | goto errout; |
| 630 | } |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; |
| 633 | ifap = &ifa->ifa_next) { |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 634 | if (tb[IFA_LOCAL] && |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 635 | ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | continue; |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 637 | |
| 638 | if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label)) |
| 639 | continue; |
| 640 | |
| 641 | if (tb[IFA_ADDRESS] && |
| 642 | (ifm->ifa_prefixlen != ifa->ifa_prefixlen || |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 643 | !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa))) |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 644 | continue; |
| 645 | |
Madhu Challa | 93a714d | 2015-02-25 09:58:35 -0800 | [diff] [blame] | 646 | if (ipv4_is_multicast(ifa->ifa_address)) |
| 647 | ip_mc_config(net->ipv4.mc_autojoin_sk, false, ifa); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 648 | __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | return 0; |
| 650 | } |
Thomas Graf | dfdd5fd | 2006-08-04 23:04:17 -0700 | [diff] [blame] | 651 | |
| 652 | err = -EADDRNOTAVAIL; |
| 653 | errout: |
| 654 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 657 | #define INFINITY_LIFE_TIME 0xFFFFFFFF |
| 658 | |
| 659 | static void check_lifetime(struct work_struct *work) |
| 660 | { |
| 661 | unsigned long now, next, next_sec, next_sched; |
| 662 | struct in_ifaddr *ifa; |
Jiri Pirko | c988d1e | 2013-04-04 23:39:39 +0000 | [diff] [blame] | 663 | struct hlist_node *n; |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 664 | int i; |
| 665 | |
| 666 | now = jiffies; |
| 667 | next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); |
| 668 | |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 669 | for (i = 0; i < IN4_ADDR_HSIZE; i++) { |
Jiri Pirko | c988d1e | 2013-04-04 23:39:39 +0000 | [diff] [blame] | 670 | bool change_needed = false; |
| 671 | |
| 672 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 673 | hlist_for_each_entry_rcu(ifa, &inet_addr_lst[i], hash) { |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 674 | unsigned long age; |
| 675 | |
| 676 | if (ifa->ifa_flags & IFA_F_PERMANENT) |
| 677 | continue; |
| 678 | |
| 679 | /* We try to batch several events at once. */ |
| 680 | age = (now - ifa->ifa_tstamp + |
| 681 | ADDRCONF_TIMER_FUZZ_MINUS) / HZ; |
| 682 | |
| 683 | if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME && |
| 684 | age >= ifa->ifa_valid_lft) { |
Jiri Pirko | c988d1e | 2013-04-04 23:39:39 +0000 | [diff] [blame] | 685 | change_needed = true; |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 686 | } else if (ifa->ifa_preferred_lft == |
| 687 | INFINITY_LIFE_TIME) { |
| 688 | continue; |
| 689 | } else if (age >= ifa->ifa_preferred_lft) { |
| 690 | if (time_before(ifa->ifa_tstamp + |
| 691 | ifa->ifa_valid_lft * HZ, next)) |
| 692 | next = ifa->ifa_tstamp + |
| 693 | ifa->ifa_valid_lft * HZ; |
| 694 | |
Jiri Pirko | c988d1e | 2013-04-04 23:39:39 +0000 | [diff] [blame] | 695 | if (!(ifa->ifa_flags & IFA_F_DEPRECATED)) |
| 696 | change_needed = true; |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 697 | } else if (time_before(ifa->ifa_tstamp + |
| 698 | ifa->ifa_preferred_lft * HZ, |
| 699 | next)) { |
| 700 | next = ifa->ifa_tstamp + |
| 701 | ifa->ifa_preferred_lft * HZ; |
| 702 | } |
| 703 | } |
Jiri Pirko | c988d1e | 2013-04-04 23:39:39 +0000 | [diff] [blame] | 704 | rcu_read_unlock(); |
| 705 | if (!change_needed) |
| 706 | continue; |
| 707 | rtnl_lock(); |
| 708 | hlist_for_each_entry_safe(ifa, n, &inet_addr_lst[i], hash) { |
| 709 | unsigned long age; |
| 710 | |
| 711 | if (ifa->ifa_flags & IFA_F_PERMANENT) |
| 712 | continue; |
| 713 | |
| 714 | /* We try to batch several events at once. */ |
| 715 | age = (now - ifa->ifa_tstamp + |
| 716 | ADDRCONF_TIMER_FUZZ_MINUS) / HZ; |
| 717 | |
| 718 | if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME && |
| 719 | age >= ifa->ifa_valid_lft) { |
| 720 | struct in_ifaddr **ifap; |
| 721 | |
| 722 | for (ifap = &ifa->ifa_dev->ifa_list; |
| 723 | *ifap != NULL; ifap = &(*ifap)->ifa_next) { |
| 724 | if (*ifap == ifa) { |
| 725 | inet_del_ifa(ifa->ifa_dev, |
| 726 | ifap, 1); |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | } else if (ifa->ifa_preferred_lft != |
| 731 | INFINITY_LIFE_TIME && |
| 732 | age >= ifa->ifa_preferred_lft && |
| 733 | !(ifa->ifa_flags & IFA_F_DEPRECATED)) { |
| 734 | ifa->ifa_flags |= IFA_F_DEPRECATED; |
| 735 | rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0); |
| 736 | } |
| 737 | } |
| 738 | rtnl_unlock(); |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 739 | } |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 740 | |
| 741 | next_sec = round_jiffies_up(next); |
| 742 | next_sched = next; |
| 743 | |
| 744 | /* If rounded timeout is accurate enough, accept it. */ |
| 745 | if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) |
| 746 | next_sched = next_sec; |
| 747 | |
| 748 | now = jiffies; |
| 749 | /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ |
| 750 | if (time_before(next_sched, now + ADDRCONF_TIMER_FUZZ_MAX)) |
| 751 | next_sched = now + ADDRCONF_TIMER_FUZZ_MAX; |
| 752 | |
viresh kumar | 906e073 | 2014-01-22 12:23:32 +0530 | [diff] [blame] | 753 | queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, |
| 754 | next_sched - now); |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft, |
| 758 | __u32 prefered_lft) |
| 759 | { |
| 760 | unsigned long timeout; |
| 761 | |
| 762 | ifa->ifa_flags &= ~(IFA_F_PERMANENT | IFA_F_DEPRECATED); |
| 763 | |
| 764 | timeout = addrconf_timeout_fixup(valid_lft, HZ); |
| 765 | if (addrconf_finite_timeout(timeout)) |
| 766 | ifa->ifa_valid_lft = timeout; |
| 767 | else |
| 768 | ifa->ifa_flags |= IFA_F_PERMANENT; |
| 769 | |
| 770 | timeout = addrconf_timeout_fixup(prefered_lft, HZ); |
| 771 | if (addrconf_finite_timeout(timeout)) { |
| 772 | if (timeout == 0) |
| 773 | ifa->ifa_flags |= IFA_F_DEPRECATED; |
| 774 | ifa->ifa_preferred_lft = timeout; |
| 775 | } |
| 776 | ifa->ifa_tstamp = jiffies; |
| 777 | if (!ifa->ifa_cstamp) |
| 778 | ifa->ifa_cstamp = ifa->ifa_tstamp; |
| 779 | } |
| 780 | |
| 781 | static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh, |
David Ahern | dac9c97 | 2018-10-07 20:16:24 -0700 | [diff] [blame] | 782 | __u32 *pvalid_lft, __u32 *pprefered_lft, |
| 783 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | { |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 785 | struct nlattr *tb[IFA_MAX+1]; |
| 786 | struct in_ifaddr *ifa; |
| 787 | struct ifaddrmsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | struct net_device *dev; |
| 789 | struct in_device *in_dev; |
Denis V. Lunev | 7b21857 | 2008-01-31 18:47:00 -0800 | [diff] [blame] | 790 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 792 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, |
| 793 | ifa_ipv4_policy, extack); |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 794 | if (err < 0) |
| 795 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 797 | ifm = nlmsg_data(nlh); |
Denis V. Lunev | 7b21857 | 2008-01-31 18:47:00 -0800 | [diff] [blame] | 798 | err = -EINVAL; |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 799 | if (ifm->ifa_prefixlen > 32 || !tb[IFA_LOCAL]) |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 800 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
Denis V. Lunev | 4b8aa9a | 2008-01-31 18:47:40 -0800 | [diff] [blame] | 802 | dev = __dev_get_by_index(net, ifm->ifa_index); |
Denis V. Lunev | 7b21857 | 2008-01-31 18:47:00 -0800 | [diff] [blame] | 803 | err = -ENODEV; |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 804 | if (!dev) |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 805 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 807 | in_dev = __in_dev_get_rtnl(dev); |
Denis V. Lunev | 7b21857 | 2008-01-31 18:47:00 -0800 | [diff] [blame] | 808 | err = -ENOBUFS; |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 809 | if (!in_dev) |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 810 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 812 | ifa = inet_alloc_ifa(); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 813 | if (!ifa) |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 814 | /* |
| 815 | * A potential indev allocation can be left alive, it stays |
| 816 | * assigned to its device and is destroy with it. |
| 817 | */ |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 818 | goto errout; |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 819 | |
Pavel Emelyanov | a4e65d3 | 2007-12-07 23:55:43 -0800 | [diff] [blame] | 820 | ipv4_devconf_setall(in_dev); |
Jiri Pirko | 1d4c8c2 | 2013-12-07 19:26:56 +0100 | [diff] [blame] | 821 | neigh_parms_data_state_setall(in_dev->arp_parms); |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 822 | in_dev_hold(in_dev); |
| 823 | |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 824 | if (!tb[IFA_ADDRESS]) |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 825 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; |
| 826 | |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 827 | INIT_HLIST_NODE(&ifa->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | ifa->ifa_prefixlen = ifm->ifa_prefixlen; |
| 829 | ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen); |
Jiri Pirko | ad6c813 | 2013-12-08 12:16:10 +0100 | [diff] [blame] | 830 | ifa->ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : |
| 831 | ifm->ifa_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | ifa->ifa_scope = ifm->ifa_scope; |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 833 | ifa->ifa_dev = in_dev; |
| 834 | |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 835 | ifa->ifa_local = nla_get_in_addr(tb[IFA_LOCAL]); |
| 836 | ifa->ifa_address = nla_get_in_addr(tb[IFA_ADDRESS]); |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 837 | |
| 838 | if (tb[IFA_BROADCAST]) |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 839 | ifa->ifa_broadcast = nla_get_in_addr(tb[IFA_BROADCAST]); |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 840 | |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 841 | if (tb[IFA_LABEL]) |
| 842 | nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | else |
| 844 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); |
| 845 | |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 846 | if (tb[IFA_RT_PRIORITY]) |
| 847 | ifa->ifa_rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]); |
| 848 | |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 849 | if (tb[IFA_CACHEINFO]) { |
| 850 | struct ifa_cacheinfo *ci; |
| 851 | |
| 852 | ci = nla_data(tb[IFA_CACHEINFO]); |
| 853 | if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) { |
| 854 | err = -EINVAL; |
Daniel Borkmann | 446266b | 2013-08-02 11:32:43 +0200 | [diff] [blame] | 855 | goto errout_free; |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 856 | } |
| 857 | *pvalid_lft = ci->ifa_valid; |
| 858 | *pprefered_lft = ci->ifa_prefered; |
| 859 | } |
| 860 | |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 861 | return ifa; |
| 862 | |
Daniel Borkmann | 446266b | 2013-08-02 11:32:43 +0200 | [diff] [blame] | 863 | errout_free: |
| 864 | inet_free_ifa(ifa); |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 865 | errout: |
| 866 | return ERR_PTR(err); |
| 867 | } |
| 868 | |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 869 | static struct in_ifaddr *find_matching_ifa(struct in_ifaddr *ifa) |
| 870 | { |
| 871 | struct in_device *in_dev = ifa->ifa_dev; |
| 872 | struct in_ifaddr *ifa1, **ifap; |
| 873 | |
| 874 | if (!ifa->ifa_local) |
| 875 | return NULL; |
| 876 | |
| 877 | for (ifap = &in_dev->ifa_list; (ifa1 = *ifap) != NULL; |
| 878 | ifap = &ifa1->ifa_next) { |
| 879 | if (ifa1->ifa_mask == ifa->ifa_mask && |
| 880 | inet_ifa_match(ifa1->ifa_address, ifa) && |
| 881 | ifa1->ifa_local == ifa->ifa_local) |
| 882 | return ifa1; |
| 883 | } |
| 884 | return NULL; |
| 885 | } |
| 886 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 887 | static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 888 | struct netlink_ext_ack *extack) |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 889 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 890 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 891 | struct in_ifaddr *ifa; |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 892 | struct in_ifaddr *ifa_existing; |
| 893 | __u32 valid_lft = INFINITY_LIFE_TIME; |
| 894 | __u32 prefered_lft = INFINITY_LIFE_TIME; |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 895 | |
| 896 | ASSERT_RTNL(); |
| 897 | |
David Ahern | dac9c97 | 2018-10-07 20:16:24 -0700 | [diff] [blame] | 898 | ifa = rtm_to_ifaddr(net, nlh, &valid_lft, &prefered_lft, extack); |
Thomas Graf | 5c75397 | 2006-08-04 23:03:53 -0700 | [diff] [blame] | 899 | if (IS_ERR(ifa)) |
| 900 | return PTR_ERR(ifa); |
| 901 | |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 902 | ifa_existing = find_matching_ifa(ifa); |
| 903 | if (!ifa_existing) { |
| 904 | /* It would be best to check for !NLM_F_CREATE here but |
stephen hemminger | 614d056 | 2014-05-16 20:46:58 -0700 | [diff] [blame] | 905 | * userspace already relies on not having to provide this. |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 906 | */ |
| 907 | set_ifa_lifetime(ifa, valid_lft, prefered_lft); |
Madhu Challa | 93a714d | 2015-02-25 09:58:35 -0800 | [diff] [blame] | 908 | if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) { |
| 909 | int ret = ip_mc_config(net->ipv4.mc_autojoin_sk, |
| 910 | true, ifa); |
| 911 | |
| 912 | if (ret < 0) { |
| 913 | inet_free_ifa(ifa); |
| 914 | return ret; |
| 915 | } |
| 916 | } |
David Ahern | de95e04 | 2017-10-18 09:56:54 -0700 | [diff] [blame] | 917 | return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid, |
| 918 | extack); |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 919 | } else { |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 920 | u32 new_metric = ifa->ifa_rt_priority; |
| 921 | |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 922 | inet_free_ifa(ifa); |
| 923 | |
| 924 | if (nlh->nlmsg_flags & NLM_F_EXCL || |
| 925 | !(nlh->nlmsg_flags & NLM_F_REPLACE)) |
| 926 | return -EEXIST; |
Jiri Pirko | 34e2ed3 | 2013-04-04 08:33:00 +0000 | [diff] [blame] | 927 | ifa = ifa_existing; |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 928 | |
| 929 | if (ifa->ifa_rt_priority != new_metric) { |
| 930 | fib_modify_prefix_metric(ifa, new_metric); |
| 931 | ifa->ifa_rt_priority = new_metric; |
| 932 | } |
| 933 | |
Jiri Pirko | 34e2ed3 | 2013-04-04 08:33:00 +0000 | [diff] [blame] | 934 | set_ifa_lifetime(ifa, valid_lft, prefered_lft); |
Jiri Pirko | 05a324b | 2013-04-04 23:39:38 +0000 | [diff] [blame] | 935 | cancel_delayed_work(&check_lifetime_work); |
viresh kumar | 906e073 | 2014-01-22 12:23:32 +0530 | [diff] [blame] | 936 | queue_delayed_work(system_power_efficient_wq, |
| 937 | &check_lifetime_work, 0); |
Jiri Pirko | 34e2ed3 | 2013-04-04 08:33:00 +0000 | [diff] [blame] | 938 | rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid); |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 939 | } |
| 940 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /* |
| 944 | * Determine a default network mask, based on the IP address. |
| 945 | */ |
| 946 | |
Eric Dumazet | 4038499 | 2012-08-03 21:06:50 +0000 | [diff] [blame] | 947 | static int inet_abc_len(__be32 addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | { |
| 949 | int rc = -1; /* Something else, probably a multicast. */ |
| 950 | |
Dave Taht | 65cab85 | 2018-12-11 15:30:34 -0800 | [diff] [blame] | 951 | if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr)) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 952 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | else { |
Al Viro | 714e85b | 2006-11-14 20:51:49 -0800 | [diff] [blame] | 954 | __u32 haddr = ntohl(addr); |
Al Viro | 714e85b | 2006-11-14 20:51:49 -0800 | [diff] [blame] | 955 | if (IN_CLASSA(haddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | rc = 8; |
Al Viro | 714e85b | 2006-11-14 20:51:49 -0800 | [diff] [blame] | 957 | else if (IN_CLASSB(haddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | rc = 16; |
Al Viro | 714e85b | 2006-11-14 20:51:49 -0800 | [diff] [blame] | 959 | else if (IN_CLASSC(haddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | rc = 24; |
Dave Taht | 65cab85 | 2018-12-11 15:30:34 -0800 | [diff] [blame] | 961 | else if (IN_CLASSE(haddr)) |
| 962 | rc = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | } |
| 964 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 965 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 969 | int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | struct sockaddr_in sin_orig; |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 972 | struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | struct in_device *in_dev; |
| 974 | struct in_ifaddr **ifap = NULL; |
| 975 | struct in_ifaddr *ifa = NULL; |
| 976 | struct net_device *dev; |
| 977 | char *colon; |
| 978 | int ret = -EFAULT; |
| 979 | int tryaddrmatch = 0; |
| 980 | |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 981 | ifr->ifr_name[IFNAMSIZ - 1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | |
| 983 | /* save original address for comparison */ |
| 984 | memcpy(&sin_orig, sin, sizeof(*sin)); |
| 985 | |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 986 | colon = strchr(ifr->ifr_name, ':'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | if (colon) |
| 988 | *colon = 0; |
| 989 | |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 990 | dev_load(net, ifr->ifr_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 992 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | case SIOCGIFADDR: /* Get interface address */ |
| 994 | case SIOCGIFBRDADDR: /* Get the broadcast address */ |
| 995 | case SIOCGIFDSTADDR: /* Get the destination address */ |
| 996 | case SIOCGIFNETMASK: /* Get the netmask for the interface */ |
| 997 | /* Note that these ioctls will not sleep, |
| 998 | so that we do not impose a lock. |
| 999 | One day we will be forced to put shlock here (I mean SMP) |
| 1000 | */ |
| 1001 | tryaddrmatch = (sin_orig.sin_family == AF_INET); |
| 1002 | memset(sin, 0, sizeof(*sin)); |
| 1003 | sin->sin_family = AF_INET; |
| 1004 | break; |
| 1005 | |
| 1006 | case SIOCSIFFLAGS: |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 1007 | ret = -EPERM; |
Eric W. Biederman | 52e804c | 2012-11-16 03:03:05 +0000 | [diff] [blame] | 1008 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | goto out; |
| 1010 | break; |
| 1011 | case SIOCSIFADDR: /* Set interface address (and family) */ |
| 1012 | case SIOCSIFBRDADDR: /* Set the broadcast address */ |
| 1013 | case SIOCSIFDSTADDR: /* Set the destination address */ |
| 1014 | case SIOCSIFNETMASK: /* Set the netmask for the interface */ |
Zhao Hongjiang | bf5b30b | 2012-09-20 22:37:25 +0000 | [diff] [blame] | 1015 | ret = -EPERM; |
Eric W. Biederman | 52e804c | 2012-11-16 03:03:05 +0000 | [diff] [blame] | 1016 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | goto out; |
| 1018 | ret = -EINVAL; |
| 1019 | if (sin->sin_family != AF_INET) |
| 1020 | goto out; |
| 1021 | break; |
| 1022 | default: |
| 1023 | ret = -EINVAL; |
| 1024 | goto out; |
| 1025 | } |
| 1026 | |
| 1027 | rtnl_lock(); |
| 1028 | |
| 1029 | ret = -ENODEV; |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 1030 | dev = __dev_get_by_name(net, ifr->ifr_name); |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1031 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | goto done; |
| 1033 | |
| 1034 | if (colon) |
| 1035 | *colon = ':'; |
| 1036 | |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1037 | in_dev = __in_dev_get_rtnl(dev); |
| 1038 | if (in_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | if (tryaddrmatch) { |
| 1040 | /* Matthias Andree */ |
| 1041 | /* compare label and address (4.4BSD style) */ |
| 1042 | /* note: we only do this for a limited set of ioctls |
| 1043 | and only if the original address family was AF_INET. |
| 1044 | This is checked above. */ |
| 1045 | for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; |
| 1046 | ifap = &ifa->ifa_next) { |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 1047 | if (!strcmp(ifr->ifr_name, ifa->ifa_label) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | sin_orig.sin_addr.s_addr == |
David S. Miller | 6c91afe | 2011-03-09 13:27:16 -0800 | [diff] [blame] | 1049 | ifa->ifa_local) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | break; /* found */ |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | /* we didn't get a match, maybe the application is |
| 1055 | 4.3BSD-style and passed in junk so we fall back to |
| 1056 | comparing just the label */ |
| 1057 | if (!ifa) { |
| 1058 | for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; |
| 1059 | ifap = &ifa->ifa_next) |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 1060 | if (!strcmp(ifr->ifr_name, ifa->ifa_label)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | break; |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | ret = -EADDRNOTAVAIL; |
| 1066 | if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS) |
| 1067 | goto done; |
| 1068 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1069 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | case SIOCGIFADDR: /* Get interface address */ |
Tonghao Zhang | 30e948a | 2018-01-28 03:38:58 -0800 | [diff] [blame] | 1071 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | sin->sin_addr.s_addr = ifa->ifa_local; |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 1073 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
| 1075 | case SIOCGIFBRDADDR: /* Get the broadcast address */ |
Tonghao Zhang | 30e948a | 2018-01-28 03:38:58 -0800 | [diff] [blame] | 1076 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | sin->sin_addr.s_addr = ifa->ifa_broadcast; |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 1078 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
| 1080 | case SIOCGIFDSTADDR: /* Get the destination address */ |
Tonghao Zhang | 30e948a | 2018-01-28 03:38:58 -0800 | [diff] [blame] | 1081 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | sin->sin_addr.s_addr = ifa->ifa_address; |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 1083 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
| 1085 | case SIOCGIFNETMASK: /* Get the netmask for the interface */ |
Tonghao Zhang | 30e948a | 2018-01-28 03:38:58 -0800 | [diff] [blame] | 1086 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | sin->sin_addr.s_addr = ifa->ifa_mask; |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 1088 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
| 1090 | case SIOCSIFFLAGS: |
| 1091 | if (colon) { |
| 1092 | ret = -EADDRNOTAVAIL; |
| 1093 | if (!ifa) |
| 1094 | break; |
| 1095 | ret = 0; |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 1096 | if (!(ifr->ifr_flags & IFF_UP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | inet_del_ifa(in_dev, ifap, 1); |
| 1098 | break; |
| 1099 | } |
Petr Machata | 567c5e1 | 2018-12-06 17:05:42 +0000 | [diff] [blame] | 1100 | ret = dev_change_flags(dev, ifr->ifr_flags, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | break; |
| 1102 | |
| 1103 | case SIOCSIFADDR: /* Set interface address (and family) */ |
| 1104 | ret = -EINVAL; |
| 1105 | if (inet_abc_len(sin->sin_addr.s_addr) < 0) |
| 1106 | break; |
| 1107 | |
| 1108 | if (!ifa) { |
| 1109 | ret = -ENOBUFS; |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1110 | ifa = inet_alloc_ifa(); |
| 1111 | if (!ifa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | break; |
Xi Wang | c7e2e1d | 2013-01-05 11:19:24 +0000 | [diff] [blame] | 1113 | INIT_HLIST_NODE(&ifa->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | if (colon) |
Al Viro | 03aef17 | 2017-07-01 07:53:12 -0400 | [diff] [blame] | 1115 | memcpy(ifa->ifa_label, ifr->ifr_name, IFNAMSIZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | else |
| 1117 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); |
| 1118 | } else { |
| 1119 | ret = 0; |
| 1120 | if (ifa->ifa_local == sin->sin_addr.s_addr) |
| 1121 | break; |
| 1122 | inet_del_ifa(in_dev, ifap, 0); |
| 1123 | ifa->ifa_broadcast = 0; |
Bjorn Mork | 148f972 | 2008-02-26 18:17:53 -0800 | [diff] [blame] | 1124 | ifa->ifa_scope = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr; |
| 1128 | |
| 1129 | if (!(dev->flags & IFF_POINTOPOINT)) { |
| 1130 | ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address); |
| 1131 | ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen); |
| 1132 | if ((dev->flags & IFF_BROADCAST) && |
| 1133 | ifa->ifa_prefixlen < 31) |
| 1134 | ifa->ifa_broadcast = ifa->ifa_address | |
| 1135 | ~ifa->ifa_mask; |
| 1136 | } else { |
| 1137 | ifa->ifa_prefixlen = 32; |
| 1138 | ifa->ifa_mask = inet_make_mask(32); |
| 1139 | } |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 1140 | set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | ret = inet_set_ifa(dev, ifa); |
| 1142 | break; |
| 1143 | |
| 1144 | case SIOCSIFBRDADDR: /* Set the broadcast address */ |
| 1145 | ret = 0; |
| 1146 | if (ifa->ifa_broadcast != sin->sin_addr.s_addr) { |
| 1147 | inet_del_ifa(in_dev, ifap, 0); |
| 1148 | ifa->ifa_broadcast = sin->sin_addr.s_addr; |
| 1149 | inet_insert_ifa(ifa); |
| 1150 | } |
| 1151 | break; |
| 1152 | |
| 1153 | case SIOCSIFDSTADDR: /* Set the destination address */ |
| 1154 | ret = 0; |
| 1155 | if (ifa->ifa_address == sin->sin_addr.s_addr) |
| 1156 | break; |
| 1157 | ret = -EINVAL; |
| 1158 | if (inet_abc_len(sin->sin_addr.s_addr) < 0) |
| 1159 | break; |
| 1160 | ret = 0; |
| 1161 | inet_del_ifa(in_dev, ifap, 0); |
| 1162 | ifa->ifa_address = sin->sin_addr.s_addr; |
| 1163 | inet_insert_ifa(ifa); |
| 1164 | break; |
| 1165 | |
| 1166 | case SIOCSIFNETMASK: /* Set the netmask for the interface */ |
| 1167 | |
| 1168 | /* |
| 1169 | * The mask we set must be legal. |
| 1170 | */ |
| 1171 | ret = -EINVAL; |
| 1172 | if (bad_mask(sin->sin_addr.s_addr, 0)) |
| 1173 | break; |
| 1174 | ret = 0; |
| 1175 | if (ifa->ifa_mask != sin->sin_addr.s_addr) { |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 1176 | __be32 old_mask = ifa->ifa_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | inet_del_ifa(in_dev, ifap, 0); |
| 1178 | ifa->ifa_mask = sin->sin_addr.s_addr; |
| 1179 | ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask); |
| 1180 | |
| 1181 | /* See if current broadcast address matches |
| 1182 | * with current netmask, then recalculate |
| 1183 | * the broadcast address. Otherwise it's a |
| 1184 | * funny address, so don't touch it since |
| 1185 | * the user seems to know what (s)he's doing... |
| 1186 | */ |
| 1187 | if ((dev->flags & IFF_BROADCAST) && |
| 1188 | (ifa->ifa_prefixlen < 31) && |
| 1189 | (ifa->ifa_broadcast == |
David Engel | dcab5e1 | 2005-10-21 22:09:16 -0500 | [diff] [blame] | 1190 | (ifa->ifa_local|~old_mask))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | ifa->ifa_broadcast = (ifa->ifa_local | |
| 1192 | ~sin->sin_addr.s_addr); |
| 1193 | } |
| 1194 | inet_insert_ifa(ifa); |
| 1195 | } |
| 1196 | break; |
| 1197 | } |
| 1198 | done: |
| 1199 | rtnl_unlock(); |
| 1200 | out: |
| 1201 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
Al Viro | 36fd633 | 2017-06-26 13:19:16 -0400 | [diff] [blame] | 1204 | static int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | { |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 1206 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | struct in_ifaddr *ifa; |
| 1208 | struct ifreq ifr; |
| 1209 | int done = 0; |
| 1210 | |
Al Viro | 36fd633 | 2017-06-26 13:19:16 -0400 | [diff] [blame] | 1211 | if (WARN_ON(size > sizeof(struct ifreq))) |
| 1212 | goto out; |
| 1213 | |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1214 | if (!in_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | goto out; |
| 1216 | |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1217 | for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | if (!buf) { |
Al Viro | 36fd633 | 2017-06-26 13:19:16 -0400 | [diff] [blame] | 1219 | done += size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | continue; |
| 1221 | } |
Al Viro | 36fd633 | 2017-06-26 13:19:16 -0400 | [diff] [blame] | 1222 | if (len < size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | break; |
| 1224 | memset(&ifr, 0, sizeof(struct ifreq)); |
Dan Carpenter | 4299c8a | 2013-07-29 22:15:19 +0300 | [diff] [blame] | 1225 | strcpy(ifr.ifr_name, ifa->ifa_label); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | |
| 1227 | (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET; |
| 1228 | (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr = |
| 1229 | ifa->ifa_local; |
| 1230 | |
Al Viro | 36fd633 | 2017-06-26 13:19:16 -0400 | [diff] [blame] | 1231 | if (copy_to_user(buf + done, &ifr, size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | done = -EFAULT; |
| 1233 | break; |
| 1234 | } |
Al Viro | 36fd633 | 2017-06-26 13:19:16 -0400 | [diff] [blame] | 1235 | len -= size; |
| 1236 | done += size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | } |
| 1238 | out: |
| 1239 | return done; |
| 1240 | } |
| 1241 | |
Gao Feng | 8b57fd1 | 2017-03-10 12:38:47 +0800 | [diff] [blame] | 1242 | static __be32 in_dev_select_addr(const struct in_device *in_dev, |
| 1243 | int scope) |
| 1244 | { |
| 1245 | for_primary_ifa(in_dev) { |
| 1246 | if (ifa->ifa_scope != RT_SCOPE_LINK && |
| 1247 | ifa->ifa_scope <= scope) |
| 1248 | return ifa->ifa_local; |
| 1249 | } endfor_ifa(in_dev); |
| 1250 | |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
Al Viro | a61ced5 | 2006-09-26 21:27:54 -0700 | [diff] [blame] | 1254 | __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | { |
Al Viro | a61ced5 | 2006-09-26 21:27:54 -0700 | [diff] [blame] | 1256 | __be32 addr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | struct in_device *in_dev; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1258 | struct net *net = dev_net(dev); |
David Ahern | 3f2fb9a | 2016-02-24 11:47:02 -0800 | [diff] [blame] | 1259 | int master_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | |
| 1261 | rcu_read_lock(); |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 1262 | in_dev = __in_dev_get_rcu(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | if (!in_dev) |
| 1264 | goto no_in_dev; |
| 1265 | |
| 1266 | for_primary_ifa(in_dev) { |
| 1267 | if (ifa->ifa_scope > scope) |
| 1268 | continue; |
| 1269 | if (!dst || inet_ifa_match(dst, ifa)) { |
| 1270 | addr = ifa->ifa_local; |
| 1271 | break; |
| 1272 | } |
| 1273 | if (!addr) |
| 1274 | addr = ifa->ifa_local; |
| 1275 | } endfor_ifa(in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | |
| 1277 | if (addr) |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 1278 | goto out_unlock; |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1279 | no_in_dev: |
David Ahern | 3f2fb9a | 2016-02-24 11:47:02 -0800 | [diff] [blame] | 1280 | master_idx = l3mdev_master_ifindex_rcu(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | |
David Lamparter | 17b693c | 2016-02-24 11:47:03 -0800 | [diff] [blame] | 1282 | /* For VRFs, the VRF device takes the place of the loopback device, |
| 1283 | * with addresses on it being preferred. Note in such cases the |
| 1284 | * loopback device will be among the devices that fail the master_idx |
| 1285 | * equality check in the loop below. |
| 1286 | */ |
| 1287 | if (master_idx && |
| 1288 | (dev = dev_get_by_index_rcu(net, master_idx)) && |
| 1289 | (in_dev = __in_dev_get_rcu(dev))) { |
Gao Feng | 8b57fd1 | 2017-03-10 12:38:47 +0800 | [diff] [blame] | 1290 | addr = in_dev_select_addr(in_dev, scope); |
| 1291 | if (addr) |
| 1292 | goto out_unlock; |
David Lamparter | 17b693c | 2016-02-24 11:47:03 -0800 | [diff] [blame] | 1293 | } |
| 1294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | /* Not loopback addresses on loopback should be preferred |
Stephen Hemminger | ca9f1fd | 2015-02-14 13:47:54 -0500 | [diff] [blame] | 1296 | in this case. It is important that lo is the first interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | in dev_base list. |
| 1298 | */ |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 1299 | for_each_netdev_rcu(net, dev) { |
David Ahern | 3f2fb9a | 2016-02-24 11:47:02 -0800 | [diff] [blame] | 1300 | if (l3mdev_master_ifindex_rcu(dev) != master_idx) |
| 1301 | continue; |
| 1302 | |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1303 | in_dev = __in_dev_get_rcu(dev); |
| 1304 | if (!in_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | continue; |
| 1306 | |
Gao Feng | 8b57fd1 | 2017-03-10 12:38:47 +0800 | [diff] [blame] | 1307 | addr = in_dev_select_addr(in_dev, scope); |
| 1308 | if (addr) |
| 1309 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | } |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 1311 | out_unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | return addr; |
| 1314 | } |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1315 | EXPORT_SYMBOL(inet_select_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | |
Al Viro | 60cad5d | 2006-09-26 22:17:09 -0700 | [diff] [blame] | 1317 | static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst, |
| 1318 | __be32 local, int scope) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | { |
| 1320 | int same = 0; |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 1321 | __be32 addr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
| 1323 | for_ifa(in_dev) { |
| 1324 | if (!addr && |
| 1325 | (local == ifa->ifa_local || !local) && |
| 1326 | ifa->ifa_scope <= scope) { |
| 1327 | addr = ifa->ifa_local; |
| 1328 | if (same) |
| 1329 | break; |
| 1330 | } |
| 1331 | if (!same) { |
| 1332 | same = (!local || inet_ifa_match(local, ifa)) && |
| 1333 | (!dst || inet_ifa_match(dst, ifa)); |
| 1334 | if (same && addr) { |
| 1335 | if (local || !dst) |
| 1336 | break; |
| 1337 | /* Is the selected addr into dst subnet? */ |
| 1338 | if (inet_ifa_match(addr, ifa)) |
| 1339 | break; |
| 1340 | /* No, then can we use new local src? */ |
| 1341 | if (ifa->ifa_scope <= scope) { |
| 1342 | addr = ifa->ifa_local; |
| 1343 | break; |
| 1344 | } |
| 1345 | /* search for large dst subnet for addr */ |
| 1346 | same = 0; |
| 1347 | } |
| 1348 | } |
| 1349 | } endfor_ifa(in_dev); |
| 1350 | |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1351 | return same ? addr : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * Confirm that local IP address exists using wildcards: |
Nicolas Dichtel | b601fa19 | 2013-12-10 15:02:40 +0100 | [diff] [blame] | 1356 | * - net: netns to check, cannot be NULL |
| 1357 | * - in_dev: only on this interface, NULL=any interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | * - dst: only in the same subnet as dst, 0=any dst |
| 1359 | * - local: address, 0=autoselect the local address |
| 1360 | * - scope: maximum allowed scope value for the local address |
| 1361 | */ |
Nicolas Dichtel | b601fa19 | 2013-12-10 15:02:40 +0100 | [diff] [blame] | 1362 | __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev, |
Denis V. Lunev | 9bd85e3 | 2008-01-14 23:05:55 -0800 | [diff] [blame] | 1363 | __be32 dst, __be32 local, int scope) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | { |
Al Viro | 60cad5d | 2006-09-26 22:17:09 -0700 | [diff] [blame] | 1365 | __be32 addr = 0; |
Denis V. Lunev | 9bd85e3 | 2008-01-14 23:05:55 -0800 | [diff] [blame] | 1366 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | |
Ian Morris | 00db412 | 2015-04-03 09:17:27 +0100 | [diff] [blame] | 1368 | if (in_dev) |
Denis V. Lunev | 9bd85e3 | 2008-01-14 23:05:55 -0800 | [diff] [blame] | 1369 | return confirm_addr_indev(in_dev, dst, local, scope); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | rcu_read_lock(); |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 1372 | for_each_netdev_rcu(net, dev) { |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1373 | in_dev = __in_dev_get_rcu(dev); |
| 1374 | if (in_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | addr = confirm_addr_indev(in_dev, dst, local, scope); |
| 1376 | if (addr) |
| 1377 | break; |
| 1378 | } |
| 1379 | } |
| 1380 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
| 1382 | return addr; |
| 1383 | } |
Andy Gospodarek | eaddcd7 | 2012-03-22 16:14:29 +0000 | [diff] [blame] | 1384 | EXPORT_SYMBOL(inet_confirm_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | |
| 1386 | /* |
| 1387 | * Device notifier |
| 1388 | */ |
| 1389 | |
| 1390 | int register_inetaddr_notifier(struct notifier_block *nb) |
| 1391 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1392 | return blocking_notifier_chain_register(&inetaddr_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | } |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1394 | EXPORT_SYMBOL(register_inetaddr_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
| 1396 | int unregister_inetaddr_notifier(struct notifier_block *nb) |
| 1397 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1398 | return blocking_notifier_chain_unregister(&inetaddr_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | } |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1400 | EXPORT_SYMBOL(unregister_inetaddr_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
Krister Johansen | 3ad7d24 | 2017-06-08 13:12:14 -0700 | [diff] [blame] | 1402 | int register_inetaddr_validator_notifier(struct notifier_block *nb) |
| 1403 | { |
| 1404 | return blocking_notifier_chain_register(&inetaddr_validator_chain, nb); |
| 1405 | } |
| 1406 | EXPORT_SYMBOL(register_inetaddr_validator_notifier); |
| 1407 | |
| 1408 | int unregister_inetaddr_validator_notifier(struct notifier_block *nb) |
| 1409 | { |
| 1410 | return blocking_notifier_chain_unregister(&inetaddr_validator_chain, |
| 1411 | nb); |
| 1412 | } |
| 1413 | EXPORT_SYMBOL(unregister_inetaddr_validator_notifier); |
| 1414 | |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1415 | /* Rename ifa_labels for a device name change. Make some effort to preserve |
| 1416 | * existing alias numbering and to create unique labels if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | */ |
| 1418 | static void inetdev_changename(struct net_device *dev, struct in_device *in_dev) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1419 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | struct in_ifaddr *ifa; |
| 1421 | int named = 0; |
| 1422 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1423 | for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { |
| 1424 | char old[IFNAMSIZ], *dot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | |
| 1426 | memcpy(old, ifa->ifa_label, IFNAMSIZ); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1427 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | if (named++ == 0) |
Thomas Graf | 573bf47 | 2008-06-10 15:40:04 -0700 | [diff] [blame] | 1429 | goto skip; |
Mark McLoughlin | 44344b2 | 2008-01-04 00:56:25 -0800 | [diff] [blame] | 1430 | dot = strchr(old, ':'); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1431 | if (!dot) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1432 | sprintf(old, ":%d", named); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | dot = old; |
| 1434 | } |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1435 | if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1436 | strcat(ifa->ifa_label, dot); |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1437 | else |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1438 | strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot); |
Thomas Graf | 573bf47 | 2008-06-10 15:40:04 -0700 | [diff] [blame] | 1439 | skip: |
| 1440 | rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1441 | } |
| 1442 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | |
Eric Dumazet | 4038499 | 2012-08-03 21:06:50 +0000 | [diff] [blame] | 1444 | static bool inetdev_valid_mtu(unsigned int mtu) |
Breno Leitao | 0677084 | 2008-09-02 17:28:58 -0700 | [diff] [blame] | 1445 | { |
Eric Dumazet | b547602 | 2017-12-11 07:17:39 -0800 | [diff] [blame] | 1446 | return mtu >= IPV4_MIN_MTU; |
Breno Leitao | 0677084 | 2008-09-02 17:28:58 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
Ian Campbell | d11327ad | 2011-02-11 07:44:16 +0000 | [diff] [blame] | 1449 | static void inetdev_send_gratuitous_arp(struct net_device *dev, |
| 1450 | struct in_device *in_dev) |
| 1451 | |
| 1452 | { |
Zoltan Kiss | b76d078 | 2011-07-24 13:09:30 +0000 | [diff] [blame] | 1453 | struct in_ifaddr *ifa; |
Ian Campbell | d11327ad | 2011-02-11 07:44:16 +0000 | [diff] [blame] | 1454 | |
Zoltan Kiss | b76d078 | 2011-07-24 13:09:30 +0000 | [diff] [blame] | 1455 | for (ifa = in_dev->ifa_list; ifa; |
| 1456 | ifa = ifa->ifa_next) { |
| 1457 | arp_send(ARPOP_REQUEST, ETH_P_ARP, |
| 1458 | ifa->ifa_local, dev, |
| 1459 | ifa->ifa_local, NULL, |
| 1460 | dev->dev_addr, NULL); |
| 1461 | } |
Ian Campbell | d11327ad | 2011-02-11 07:44:16 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | /* Called only under RTNL semaphore */ |
| 1465 | |
| 1466 | static int inetdev_event(struct notifier_block *this, unsigned long event, |
| 1467 | void *ptr) |
| 1468 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 1469 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Eric Dumazet | 748e2d9 | 2012-08-22 21:50:59 +0000 | [diff] [blame] | 1470 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
| 1472 | ASSERT_RTNL(); |
| 1473 | |
| 1474 | if (!in_dev) { |
Herbert Xu | 8030f54 | 2007-02-22 01:53:47 +0900 | [diff] [blame] | 1475 | if (event == NETDEV_REGISTER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | in_dev = inetdev_init(dev); |
WANG Cong | 20e61da | 2014-07-25 15:25:08 -0700 | [diff] [blame] | 1477 | if (IS_ERR(in_dev)) |
| 1478 | return notifier_from_errno(PTR_ERR(in_dev)); |
Eric W. Biederman | 0cc217e | 2007-09-26 22:10:06 -0700 | [diff] [blame] | 1479 | if (dev->flags & IFF_LOOPBACK) { |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 1480 | IN_DEV_CONF_SET(in_dev, NOXFRM, 1); |
| 1481 | IN_DEV_CONF_SET(in_dev, NOPOLICY, 1); |
Herbert Xu | 8030f54 | 2007-02-22 01:53:47 +0900 | [diff] [blame] | 1482 | } |
Breno Leitao | 0677084 | 2008-09-02 17:28:58 -0700 | [diff] [blame] | 1483 | } else if (event == NETDEV_CHANGEMTU) { |
| 1484 | /* Re-enabling IP */ |
| 1485 | if (inetdev_valid_mtu(dev->mtu)) |
| 1486 | in_dev = inetdev_init(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | } |
| 1488 | goto out; |
| 1489 | } |
| 1490 | |
| 1491 | switch (event) { |
| 1492 | case NETDEV_REGISTER: |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 1493 | pr_debug("%s: bug\n", __func__); |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 1494 | RCU_INIT_POINTER(dev->ip_ptr, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | break; |
| 1496 | case NETDEV_UP: |
Breno Leitao | 0677084 | 2008-09-02 17:28:58 -0700 | [diff] [blame] | 1497 | if (!inetdev_valid_mtu(dev->mtu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | break; |
Eric W. Biederman | 0cc217e | 2007-09-26 22:10:06 -0700 | [diff] [blame] | 1499 | if (dev->flags & IFF_LOOPBACK) { |
Eric Dumazet | 9f9354b | 2009-11-04 22:05:10 -0800 | [diff] [blame] | 1500 | struct in_ifaddr *ifa = inet_alloc_ifa(); |
| 1501 | |
| 1502 | if (ifa) { |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 1503 | INIT_HLIST_NODE(&ifa->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | ifa->ifa_local = |
| 1505 | ifa->ifa_address = htonl(INADDR_LOOPBACK); |
| 1506 | ifa->ifa_prefixlen = 8; |
| 1507 | ifa->ifa_mask = inet_make_mask(8); |
| 1508 | in_dev_hold(in_dev); |
| 1509 | ifa->ifa_dev = in_dev; |
| 1510 | ifa->ifa_scope = RT_SCOPE_HOST; |
| 1511 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 1512 | set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, |
| 1513 | INFINITY_LIFE_TIME); |
Jiri Pirko | dfd1582 | 2014-01-07 15:55:45 +0100 | [diff] [blame] | 1514 | ipv4_devconf_setall(in_dev); |
| 1515 | neigh_parms_data_state_setall(in_dev->arp_parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | inet_insert_ifa(ifa); |
| 1517 | } |
| 1518 | } |
| 1519 | ip_mc_up(in_dev); |
Stephen Hemminger | eefef1c | 2009-02-01 01:04:33 -0800 | [diff] [blame] | 1520 | /* fall through */ |
| 1521 | case NETDEV_CHANGEADDR: |
Ian Campbell | d11327ad | 2011-02-11 07:44:16 +0000 | [diff] [blame] | 1522 | if (!IN_DEV_ARP_NOTIFY(in_dev)) |
| 1523 | break; |
| 1524 | /* fall through */ |
| 1525 | case NETDEV_NOTIFY_PEERS: |
Stephen Hemminger | a21090c | 2009-10-07 03:18:17 -0700 | [diff] [blame] | 1526 | /* Send gratuitous ARP to notify of link change */ |
Ian Campbell | d11327ad | 2011-02-11 07:44:16 +0000 | [diff] [blame] | 1527 | inetdev_send_gratuitous_arp(dev, in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | break; |
| 1529 | case NETDEV_DOWN: |
| 1530 | ip_mc_down(in_dev); |
| 1531 | break; |
Jiri Pirko | 93d9b7d | 2010-03-10 10:28:56 +0000 | [diff] [blame] | 1532 | case NETDEV_PRE_TYPE_CHANGE: |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1533 | ip_mc_unmap(in_dev); |
| 1534 | break; |
Jiri Pirko | 93d9b7d | 2010-03-10 10:28:56 +0000 | [diff] [blame] | 1535 | case NETDEV_POST_TYPE_CHANGE: |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 1536 | ip_mc_remap(in_dev); |
| 1537 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | case NETDEV_CHANGEMTU: |
Breno Leitao | 0677084 | 2008-09-02 17:28:58 -0700 | [diff] [blame] | 1539 | if (inetdev_valid_mtu(dev->mtu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | break; |
Breno Leitao | 0677084 | 2008-09-02 17:28:58 -0700 | [diff] [blame] | 1541 | /* disable IP when MTU is not enough */ |
Gustavo A. R. Silva | fcfd6df | 2017-10-16 15:48:55 -0500 | [diff] [blame] | 1542 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | case NETDEV_UNREGISTER: |
| 1544 | inetdev_destroy(in_dev); |
| 1545 | break; |
| 1546 | case NETDEV_CHANGENAME: |
| 1547 | /* Do not notify about label change, this event is |
| 1548 | * not interesting to applications using netlink. |
| 1549 | */ |
| 1550 | inetdev_changename(dev, in_dev); |
| 1551 | |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 1552 | devinet_sysctl_unregister(in_dev); |
Pavel Emelyanov | 66f27a5 | 2007-12-02 00:55:54 +1100 | [diff] [blame] | 1553 | devinet_sysctl_register(in_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | break; |
| 1555 | } |
| 1556 | out: |
| 1557 | return NOTIFY_DONE; |
| 1558 | } |
| 1559 | |
| 1560 | static struct notifier_block ip_netdev_notifier = { |
Jianjun Kong | 539afed | 2008-11-03 02:48:48 -0800 | [diff] [blame] | 1561 | .notifier_call = inetdev_event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | }; |
| 1563 | |
Eric Dumazet | 4038499 | 2012-08-03 21:06:50 +0000 | [diff] [blame] | 1564 | static size_t inet_nlmsg_size(void) |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1565 | { |
| 1566 | return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) |
| 1567 | + nla_total_size(4) /* IFA_ADDRESS */ |
| 1568 | + nla_total_size(4) /* IFA_LOCAL */ |
| 1569 | + nla_total_size(4) /* IFA_BROADCAST */ |
Jiri Pirko | ad6c813 | 2013-12-08 12:16:10 +0100 | [diff] [blame] | 1570 | + nla_total_size(IFNAMSIZ) /* IFA_LABEL */ |
Geert Uytterhoeven | 63b5f15 | 2014-02-05 08:38:25 +0100 | [diff] [blame] | 1571 | + nla_total_size(4) /* IFA_FLAGS */ |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1572 | + nla_total_size(4) /* IFA_RT_PRIORITY */ |
Geert Uytterhoeven | 63b5f15 | 2014-02-05 08:38:25 +0100 | [diff] [blame] | 1573 | + nla_total_size(sizeof(struct ifa_cacheinfo)); /* IFA_CACHEINFO */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1574 | } |
| 1575 | |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 1576 | static inline u32 cstamp_delta(unsigned long cstamp) |
| 1577 | { |
| 1578 | return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; |
| 1579 | } |
| 1580 | |
| 1581 | static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, |
| 1582 | unsigned long tstamp, u32 preferred, u32 valid) |
| 1583 | { |
| 1584 | struct ifa_cacheinfo ci; |
| 1585 | |
| 1586 | ci.cstamp = cstamp_delta(cstamp); |
| 1587 | ci.tstamp = cstamp_delta(tstamp); |
| 1588 | ci.ifa_prefered = preferred; |
| 1589 | ci.ifa_valid = valid; |
| 1590 | |
| 1591 | return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); |
| 1592 | } |
| 1593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, |
Christian Brauner | 978a46f | 2018-09-04 21:53:54 +0200 | [diff] [blame] | 1595 | struct inet_fill_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | { |
| 1597 | struct ifaddrmsg *ifm; |
| 1598 | struct nlmsghdr *nlh; |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 1599 | u32 preferred, valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | |
Christian Brauner | 978a46f | 2018-09-04 21:53:54 +0200 | [diff] [blame] | 1601 | nlh = nlmsg_put(skb, args->portid, args->seq, args->event, sizeof(*ifm), |
| 1602 | args->flags); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1603 | if (!nlh) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1604 | return -EMSGSIZE; |
Thomas Graf | 47f6851 | 2006-08-04 23:04:36 -0700 | [diff] [blame] | 1605 | |
| 1606 | ifm = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | ifm->ifa_family = AF_INET; |
| 1608 | ifm->ifa_prefixlen = ifa->ifa_prefixlen; |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 1609 | ifm->ifa_flags = ifa->ifa_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | ifm->ifa_scope = ifa->ifa_scope; |
| 1611 | ifm->ifa_index = ifa->ifa_dev->dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | |
Christian Brauner | 978a46f | 2018-09-04 21:53:54 +0200 | [diff] [blame] | 1613 | if (args->netnsid >= 0 && |
| 1614 | nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) |
Christian Brauner | d380714 | 2018-09-04 21:53:49 +0200 | [diff] [blame] | 1615 | goto nla_put_failure; |
| 1616 | |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 1617 | if (!(ifm->ifa_flags & IFA_F_PERMANENT)) { |
| 1618 | preferred = ifa->ifa_preferred_lft; |
| 1619 | valid = ifa->ifa_valid_lft; |
| 1620 | if (preferred != INFINITY_LIFE_TIME) { |
| 1621 | long tval = (jiffies - ifa->ifa_tstamp) / HZ; |
| 1622 | |
| 1623 | if (preferred > tval) |
| 1624 | preferred -= tval; |
| 1625 | else |
| 1626 | preferred = 0; |
| 1627 | if (valid != INFINITY_LIFE_TIME) { |
| 1628 | if (valid > tval) |
| 1629 | valid -= tval; |
| 1630 | else |
| 1631 | valid = 0; |
| 1632 | } |
| 1633 | } |
| 1634 | } else { |
| 1635 | preferred = INFINITY_LIFE_TIME; |
| 1636 | valid = INFINITY_LIFE_TIME; |
| 1637 | } |
David S. Miller | f3756b7 | 2012-04-01 20:39:02 -0400 | [diff] [blame] | 1638 | if ((ifa->ifa_address && |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 1639 | nla_put_in_addr(skb, IFA_ADDRESS, ifa->ifa_address)) || |
David S. Miller | f3756b7 | 2012-04-01 20:39:02 -0400 | [diff] [blame] | 1640 | (ifa->ifa_local && |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 1641 | nla_put_in_addr(skb, IFA_LOCAL, ifa->ifa_local)) || |
David S. Miller | f3756b7 | 2012-04-01 20:39:02 -0400 | [diff] [blame] | 1642 | (ifa->ifa_broadcast && |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 1643 | nla_put_in_addr(skb, IFA_BROADCAST, ifa->ifa_broadcast)) || |
David S. Miller | f3756b7 | 2012-04-01 20:39:02 -0400 | [diff] [blame] | 1644 | (ifa->ifa_label[0] && |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 1645 | nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) || |
Jiri Pirko | ad6c813 | 2013-12-08 12:16:10 +0100 | [diff] [blame] | 1646 | nla_put_u32(skb, IFA_FLAGS, ifa->ifa_flags) || |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1647 | (ifa->ifa_rt_priority && |
| 1648 | nla_put_u32(skb, IFA_RT_PRIORITY, ifa->ifa_rt_priority)) || |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 1649 | put_cacheinfo(skb, ifa->ifa_cstamp, ifa->ifa_tstamp, |
| 1650 | preferred, valid)) |
David S. Miller | f3756b7 | 2012-04-01 20:39:02 -0400 | [diff] [blame] | 1651 | goto nla_put_failure; |
Thomas Graf | 47f6851 | 2006-08-04 23:04:36 -0700 | [diff] [blame] | 1652 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1653 | nlmsg_end(skb, nlh); |
| 1654 | return 0; |
Thomas Graf | 47f6851 | 2006-08-04 23:04:36 -0700 | [diff] [blame] | 1655 | |
| 1656 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1657 | nlmsg_cancel(skb, nlh); |
| 1658 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
David Ahern | c33078e | 2018-10-07 20:16:28 -0700 | [diff] [blame] | 1661 | static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh, |
| 1662 | struct inet_fill_args *fillargs, |
| 1663 | struct net **tgt_net, struct sock *sk, |
David Ahern | 5fcd266 | 2018-10-19 12:45:29 -0700 | [diff] [blame] | 1664 | struct netlink_callback *cb) |
David Ahern | c33078e | 2018-10-07 20:16:28 -0700 | [diff] [blame] | 1665 | { |
David Ahern | 5fcd266 | 2018-10-19 12:45:29 -0700 | [diff] [blame] | 1666 | struct netlink_ext_ack *extack = cb->extack; |
David Ahern | c33078e | 2018-10-07 20:16:28 -0700 | [diff] [blame] | 1667 | struct nlattr *tb[IFA_MAX+1]; |
| 1668 | struct ifaddrmsg *ifm; |
| 1669 | int err, i; |
| 1670 | |
| 1671 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { |
| 1672 | NL_SET_ERR_MSG(extack, "ipv4: Invalid header for address dump request"); |
| 1673 | return -EINVAL; |
| 1674 | } |
| 1675 | |
| 1676 | ifm = nlmsg_data(nlh); |
| 1677 | if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { |
| 1678 | NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for address dump request"); |
| 1679 | return -EINVAL; |
| 1680 | } |
David Ahern | 5fcd266 | 2018-10-19 12:45:29 -0700 | [diff] [blame] | 1681 | |
| 1682 | fillargs->ifindex = ifm->ifa_index; |
| 1683 | if (fillargs->ifindex) { |
| 1684 | cb->answer_flags |= NLM_F_DUMP_FILTERED; |
| 1685 | fillargs->flags |= NLM_F_DUMP_FILTERED; |
David Ahern | c33078e | 2018-10-07 20:16:28 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1688 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, |
| 1689 | ifa_ipv4_policy, extack); |
David Ahern | c33078e | 2018-10-07 20:16:28 -0700 | [diff] [blame] | 1690 | if (err < 0) |
| 1691 | return err; |
| 1692 | |
| 1693 | for (i = 0; i <= IFA_MAX; ++i) { |
| 1694 | if (!tb[i]) |
| 1695 | continue; |
| 1696 | |
| 1697 | if (i == IFA_TARGET_NETNSID) { |
| 1698 | struct net *net; |
| 1699 | |
| 1700 | fillargs->netnsid = nla_get_s32(tb[i]); |
| 1701 | |
| 1702 | net = rtnl_get_net_ns_capable(sk, fillargs->netnsid); |
| 1703 | if (IS_ERR(net)) { |
Bjørn Mork | bf4cc40 | 2018-10-25 21:18:25 +0200 | [diff] [blame] | 1704 | fillargs->netnsid = -1; |
David Ahern | c33078e | 2018-10-07 20:16:28 -0700 | [diff] [blame] | 1705 | NL_SET_ERR_MSG(extack, "ipv4: Invalid target network namespace id"); |
| 1706 | return PTR_ERR(net); |
| 1707 | } |
| 1708 | *tgt_net = net; |
| 1709 | } else { |
| 1710 | NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in dump request"); |
| 1711 | return -EINVAL; |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
David Ahern | 1c98eca | 2018-10-19 12:45:27 -0700 | [diff] [blame] | 1718 | static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb, |
| 1719 | struct netlink_callback *cb, int s_ip_idx, |
| 1720 | struct inet_fill_args *fillargs) |
| 1721 | { |
| 1722 | struct in_ifaddr *ifa; |
| 1723 | int ip_idx = 0; |
| 1724 | int err; |
| 1725 | |
| 1726 | for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next, ip_idx++) { |
| 1727 | if (ip_idx < s_ip_idx) |
| 1728 | continue; |
| 1729 | |
| 1730 | err = inet_fill_ifaddr(skb, ifa, fillargs); |
| 1731 | if (err < 0) |
| 1732 | goto done; |
| 1733 | |
| 1734 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
| 1735 | } |
| 1736 | err = 0; |
| 1737 | |
| 1738 | done: |
| 1739 | cb->args[2] = ip_idx; |
| 1740 | |
| 1741 | return err; |
| 1742 | } |
| 1743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) |
| 1745 | { |
David Ahern | c33078e | 2018-10-07 20:16:28 -0700 | [diff] [blame] | 1746 | const struct nlmsghdr *nlh = cb->nlh; |
Christian Brauner | 978a46f | 2018-09-04 21:53:54 +0200 | [diff] [blame] | 1747 | struct inet_fill_args fillargs = { |
| 1748 | .portid = NETLINK_CB(cb->skb).portid, |
David Ahern | c33078e | 2018-10-07 20:16:28 -0700 | [diff] [blame] | 1749 | .seq = nlh->nlmsg_seq, |
Christian Brauner | 978a46f | 2018-09-04 21:53:54 +0200 | [diff] [blame] | 1750 | .event = RTM_NEWADDR, |
| 1751 | .flags = NLM_F_MULTI, |
| 1752 | .netnsid = -1, |
| 1753 | }; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1754 | struct net *net = sock_net(skb->sk); |
Christian Brauner | d380714 | 2018-09-04 21:53:49 +0200 | [diff] [blame] | 1755 | struct net *tgt_net = net; |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1756 | int h, s_h; |
| 1757 | int idx, s_idx; |
David Ahern | 1c98eca | 2018-10-19 12:45:27 -0700 | [diff] [blame] | 1758 | int s_ip_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | struct net_device *dev; |
| 1760 | struct in_device *in_dev; |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1761 | struct hlist_head *head; |
David Ahern | d7e3861 | 2018-10-24 12:58:59 -0700 | [diff] [blame] | 1762 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1764 | s_h = cb->args[0]; |
| 1765 | s_idx = idx = cb->args[1]; |
David Ahern | 1c98eca | 2018-10-19 12:45:27 -0700 | [diff] [blame] | 1766 | s_ip_idx = cb->args[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | |
David Ahern | c33078e | 2018-10-07 20:16:28 -0700 | [diff] [blame] | 1768 | if (cb->strict_check) { |
David Ahern | c33078e | 2018-10-07 20:16:28 -0700 | [diff] [blame] | 1769 | err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net, |
David Ahern | 5fcd266 | 2018-10-19 12:45:29 -0700 | [diff] [blame] | 1770 | skb->sk, cb); |
David Ahern | c33078e | 2018-10-07 20:16:28 -0700 | [diff] [blame] | 1771 | if (err < 0) |
David Ahern | d7e3861 | 2018-10-24 12:58:59 -0700 | [diff] [blame] | 1772 | goto put_tgt_net; |
David Ahern | 5fcd266 | 2018-10-19 12:45:29 -0700 | [diff] [blame] | 1773 | |
David Ahern | d7e3861 | 2018-10-24 12:58:59 -0700 | [diff] [blame] | 1774 | err = 0; |
David Ahern | 5fcd266 | 2018-10-19 12:45:29 -0700 | [diff] [blame] | 1775 | if (fillargs.ifindex) { |
| 1776 | dev = __dev_get_by_index(tgt_net, fillargs.ifindex); |
David Ahern | d7e3861 | 2018-10-24 12:58:59 -0700 | [diff] [blame] | 1777 | if (!dev) { |
| 1778 | err = -ENODEV; |
| 1779 | goto put_tgt_net; |
| 1780 | } |
David Ahern | 5fcd266 | 2018-10-19 12:45:29 -0700 | [diff] [blame] | 1781 | |
| 1782 | in_dev = __in_dev_get_rtnl(dev); |
| 1783 | if (in_dev) { |
| 1784 | err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx, |
| 1785 | &fillargs); |
| 1786 | } |
| 1787 | goto put_tgt_net; |
| 1788 | } |
Christian Brauner | d380714 | 2018-09-04 21:53:49 +0200 | [diff] [blame] | 1789 | } |
| 1790 | |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1791 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 1792 | idx = 0; |
Christian Brauner | d380714 | 2018-09-04 21:53:49 +0200 | [diff] [blame] | 1793 | head = &tgt_net->dev_index_head[h]; |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1794 | rcu_read_lock(); |
Christian Brauner | d380714 | 2018-09-04 21:53:49 +0200 | [diff] [blame] | 1795 | cb->seq = atomic_read(&tgt_net->ipv4.dev_addr_genid) ^ |
| 1796 | tgt_net->dev_base_seq; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1797 | hlist_for_each_entry_rcu(dev, head, index_hlist) { |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1798 | if (idx < s_idx) |
| 1799 | goto cont; |
Patrick McHardy | 4b97efd | 2010-03-26 20:27:49 -0700 | [diff] [blame] | 1800 | if (h > s_h || idx > s_idx) |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1801 | s_ip_idx = 0; |
| 1802 | in_dev = __in_dev_get_rcu(dev); |
| 1803 | if (!in_dev) |
| 1804 | goto cont; |
| 1805 | |
David Ahern | 1c98eca | 2018-10-19 12:45:27 -0700 | [diff] [blame] | 1806 | err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx, |
| 1807 | &fillargs); |
| 1808 | if (err < 0) { |
| 1809 | rcu_read_unlock(); |
| 1810 | goto done; |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1811 | } |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1812 | cont: |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1813 | idx++; |
| 1814 | } |
| 1815 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | done: |
Eric Dumazet | eec4df9 | 2009-11-12 07:44:25 +0000 | [diff] [blame] | 1819 | cb->args[0] = h; |
| 1820 | cb->args[1] = idx; |
David Ahern | 5fcd266 | 2018-10-19 12:45:29 -0700 | [diff] [blame] | 1821 | put_tgt_net: |
Christian Brauner | 978a46f | 2018-09-04 21:53:54 +0200 | [diff] [blame] | 1822 | if (fillargs.netnsid >= 0) |
Christian Brauner | d380714 | 2018-09-04 21:53:49 +0200 | [diff] [blame] | 1823 | put_net(tgt_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | |
Arthur Gautier | 7c1e8a3 | 2018-12-31 02:10:58 +0000 | [diff] [blame] | 1825 | return skb->len ? : err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
Jianjun Kong | 539afed | 2008-11-03 02:48:48 -0800 | [diff] [blame] | 1828 | static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1829 | u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | { |
Christian Brauner | 978a46f | 2018-09-04 21:53:54 +0200 | [diff] [blame] | 1831 | struct inet_fill_args fillargs = { |
| 1832 | .portid = portid, |
| 1833 | .seq = nlh ? nlh->nlmsg_seq : 0, |
| 1834 | .event = event, |
| 1835 | .flags = 0, |
| 1836 | .netnsid = -1, |
| 1837 | }; |
Thomas Graf | 47f6851 | 2006-08-04 23:04:36 -0700 | [diff] [blame] | 1838 | struct sk_buff *skb; |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 1839 | int err = -ENOBUFS; |
Denis V. Lunev | 4b8aa9a | 2008-01-31 18:47:40 -0800 | [diff] [blame] | 1840 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1842 | net = dev_net(ifa->ifa_dev->dev); |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1843 | skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1844 | if (!skb) |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 1845 | goto errout; |
| 1846 | |
Christian Brauner | 978a46f | 2018-09-04 21:53:54 +0200 | [diff] [blame] | 1847 | err = inet_fill_ifaddr(skb, ifa, &fillargs); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 1848 | if (err < 0) { |
| 1849 | /* -EMSGSIZE implies BUG in inet_nlmsg_size() */ |
| 1850 | WARN_ON(err == -EMSGSIZE); |
| 1851 | kfree_skb(skb); |
| 1852 | goto errout; |
| 1853 | } |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1854 | rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL); |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 1855 | return; |
Thomas Graf | d6062cb | 2006-08-15 00:33:59 -0700 | [diff] [blame] | 1856 | errout: |
| 1857 | if (err < 0) |
Denis V. Lunev | 4b8aa9a | 2008-01-31 18:47:40 -0800 | [diff] [blame] | 1858 | rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | } |
| 1860 | |
Arad, Ronen | b1974ed | 2015-10-19 09:23:28 -0700 | [diff] [blame] | 1861 | static size_t inet_get_link_af_size(const struct net_device *dev, |
| 1862 | u32 ext_filter_mask) |
Thomas Graf | 9f0f727 | 2010-11-16 04:32:48 +0000 | [diff] [blame] | 1863 | { |
Eric Dumazet | 1fc19af | 2011-05-09 20:55:03 -0700 | [diff] [blame] | 1864 | struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr); |
Thomas Graf | 9f0f727 | 2010-11-16 04:32:48 +0000 | [diff] [blame] | 1865 | |
| 1866 | if (!in_dev) |
| 1867 | return 0; |
| 1868 | |
| 1869 | return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */ |
| 1870 | } |
| 1871 | |
Sowmini Varadhan | d5566fd | 2015-09-11 16:48:48 -0400 | [diff] [blame] | 1872 | static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev, |
| 1873 | u32 ext_filter_mask) |
Thomas Graf | 9f0f727 | 2010-11-16 04:32:48 +0000 | [diff] [blame] | 1874 | { |
Eric Dumazet | 1fc19af | 2011-05-09 20:55:03 -0700 | [diff] [blame] | 1875 | struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr); |
Thomas Graf | 9f0f727 | 2010-11-16 04:32:48 +0000 | [diff] [blame] | 1876 | struct nlattr *nla; |
| 1877 | int i; |
| 1878 | |
| 1879 | if (!in_dev) |
| 1880 | return -ENODATA; |
| 1881 | |
| 1882 | nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1883 | if (!nla) |
Thomas Graf | 9f0f727 | 2010-11-16 04:32:48 +0000 | [diff] [blame] | 1884 | return -EMSGSIZE; |
| 1885 | |
| 1886 | for (i = 0; i < IPV4_DEVCONF_MAX; i++) |
| 1887 | ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i]; |
| 1888 | |
| 1889 | return 0; |
| 1890 | } |
| 1891 | |
| 1892 | static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = { |
| 1893 | [IFLA_INET_CONF] = { .type = NLA_NESTED }, |
| 1894 | }; |
| 1895 | |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1896 | static int inet_validate_link_af(const struct net_device *dev, |
| 1897 | const struct nlattr *nla) |
Thomas Graf | 9f0f727 | 2010-11-16 04:32:48 +0000 | [diff] [blame] | 1898 | { |
Thomas Graf | 9f0f727 | 2010-11-16 04:32:48 +0000 | [diff] [blame] | 1899 | struct nlattr *a, *tb[IFLA_INET_MAX+1]; |
| 1900 | int err, rem; |
| 1901 | |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 1902 | if (dev && !__in_dev_get_rcu(dev)) |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1903 | return -EAFNOSUPPORT; |
Thomas Graf | 9f0f727 | 2010-11-16 04:32:48 +0000 | [diff] [blame] | 1904 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1905 | err = nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla, |
| 1906 | inet_af_policy, NULL); |
Thomas Graf | 9f0f727 | 2010-11-16 04:32:48 +0000 | [diff] [blame] | 1907 | if (err < 0) |
| 1908 | return err; |
| 1909 | |
| 1910 | if (tb[IFLA_INET_CONF]) { |
| 1911 | nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) { |
| 1912 | int cfgid = nla_type(a); |
| 1913 | |
| 1914 | if (nla_len(a) < 4) |
| 1915 | return -EINVAL; |
| 1916 | |
| 1917 | if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX) |
| 1918 | return -EINVAL; |
| 1919 | } |
| 1920 | } |
| 1921 | |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1922 | return 0; |
| 1923 | } |
| 1924 | |
| 1925 | static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla) |
| 1926 | { |
Florian Westphal | 5fa85a0 | 2017-10-16 15:44:36 +0200 | [diff] [blame] | 1927 | struct in_device *in_dev = __in_dev_get_rcu(dev); |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1928 | struct nlattr *a, *tb[IFLA_INET_MAX+1]; |
| 1929 | int rem; |
| 1930 | |
| 1931 | if (!in_dev) |
| 1932 | return -EAFNOSUPPORT; |
| 1933 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1934 | if (nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla, NULL, NULL) < 0) |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 1935 | BUG(); |
| 1936 | |
Thomas Graf | 9f0f727 | 2010-11-16 04:32:48 +0000 | [diff] [blame] | 1937 | if (tb[IFLA_INET_CONF]) { |
| 1938 | nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) |
| 1939 | ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a)); |
| 1940 | } |
| 1941 | |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 1945 | static int inet_netconf_msgsize_devconf(int type) |
| 1946 | { |
| 1947 | int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) |
| 1948 | + nla_total_size(4); /* NETCONFA_IFINDEX */ |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 1949 | bool all = false; |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 1950 | |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 1951 | if (type == NETCONFA_ALL) |
| 1952 | all = true; |
| 1953 | |
| 1954 | if (all || type == NETCONFA_FORWARDING) |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 1955 | size += nla_total_size(4); |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 1956 | if (all || type == NETCONFA_RP_FILTER) |
Nicolas Dichtel | cc535df | 2012-10-29 04:53:27 +0000 | [diff] [blame] | 1957 | size += nla_total_size(4); |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 1958 | if (all || type == NETCONFA_MC_FORWARDING) |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 1959 | size += nla_total_size(4); |
Xin Long | 5cbf777 | 2018-07-27 16:37:28 +0800 | [diff] [blame] | 1960 | if (all || type == NETCONFA_BC_FORWARDING) |
| 1961 | size += nla_total_size(4); |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 1962 | if (all || type == NETCONFA_PROXY_NEIGH) |
stephen hemminger | f085ff1 | 2013-12-12 13:06:50 -0800 | [diff] [blame] | 1963 | size += nla_total_size(4); |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 1964 | if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) |
Andy Gospodarek | 974d7af | 2015-07-07 13:56:57 -0400 | [diff] [blame] | 1965 | size += nla_total_size(4); |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 1966 | |
| 1967 | return size; |
| 1968 | } |
| 1969 | |
| 1970 | static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex, |
| 1971 | struct ipv4_devconf *devconf, u32 portid, |
| 1972 | u32 seq, int event, unsigned int flags, |
| 1973 | int type) |
| 1974 | { |
| 1975 | struct nlmsghdr *nlh; |
| 1976 | struct netconfmsg *ncm; |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 1977 | bool all = false; |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 1978 | |
| 1979 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), |
| 1980 | flags); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1981 | if (!nlh) |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 1982 | return -EMSGSIZE; |
| 1983 | |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 1984 | if (type == NETCONFA_ALL) |
| 1985 | all = true; |
| 1986 | |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 1987 | ncm = nlmsg_data(nlh); |
| 1988 | ncm->ncm_family = AF_INET; |
| 1989 | |
| 1990 | if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) |
| 1991 | goto nla_put_failure; |
| 1992 | |
David Ahern | b5c9641 | 2017-03-28 14:28:03 -0700 | [diff] [blame] | 1993 | if (!devconf) |
| 1994 | goto out; |
| 1995 | |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 1996 | if ((all || type == NETCONFA_FORWARDING) && |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 1997 | nla_put_s32(skb, NETCONFA_FORWARDING, |
| 1998 | IPV4_DEVCONF(*devconf, FORWARDING)) < 0) |
| 1999 | goto nla_put_failure; |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 2000 | if ((all || type == NETCONFA_RP_FILTER) && |
Nicolas Dichtel | cc535df | 2012-10-29 04:53:27 +0000 | [diff] [blame] | 2001 | nla_put_s32(skb, NETCONFA_RP_FILTER, |
| 2002 | IPV4_DEVCONF(*devconf, RP_FILTER)) < 0) |
| 2003 | goto nla_put_failure; |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 2004 | if ((all || type == NETCONFA_MC_FORWARDING) && |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 2005 | nla_put_s32(skb, NETCONFA_MC_FORWARDING, |
| 2006 | IPV4_DEVCONF(*devconf, MC_FORWARDING)) < 0) |
| 2007 | goto nla_put_failure; |
Xin Long | 5cbf777 | 2018-07-27 16:37:28 +0800 | [diff] [blame] | 2008 | if ((all || type == NETCONFA_BC_FORWARDING) && |
| 2009 | nla_put_s32(skb, NETCONFA_BC_FORWARDING, |
| 2010 | IPV4_DEVCONF(*devconf, BC_FORWARDING)) < 0) |
| 2011 | goto nla_put_failure; |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 2012 | if ((all || type == NETCONFA_PROXY_NEIGH) && |
stephen hemminger | 09aea5d | 2013-12-17 22:35:52 -0800 | [diff] [blame] | 2013 | nla_put_s32(skb, NETCONFA_PROXY_NEIGH, |
stephen hemminger | f085ff1 | 2013-12-12 13:06:50 -0800 | [diff] [blame] | 2014 | IPV4_DEVCONF(*devconf, PROXY_ARP)) < 0) |
| 2015 | goto nla_put_failure; |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 2016 | if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && |
Andy Gospodarek | 974d7af | 2015-07-07 13:56:57 -0400 | [diff] [blame] | 2017 | nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, |
| 2018 | IPV4_DEVCONF(*devconf, IGNORE_ROUTES_WITH_LINKDOWN)) < 0) |
| 2019 | goto nla_put_failure; |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2020 | |
David Ahern | b5c9641 | 2017-03-28 14:28:03 -0700 | [diff] [blame] | 2021 | out: |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 2022 | nlmsg_end(skb, nlh); |
| 2023 | return 0; |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2024 | |
| 2025 | nla_put_failure: |
| 2026 | nlmsg_cancel(skb, nlh); |
| 2027 | return -EMSGSIZE; |
| 2028 | } |
| 2029 | |
David Ahern | 3b02286 | 2017-03-28 14:28:02 -0700 | [diff] [blame] | 2030 | void inet_netconf_notify_devconf(struct net *net, int event, int type, |
| 2031 | int ifindex, struct ipv4_devconf *devconf) |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2032 | { |
| 2033 | struct sk_buff *skb; |
| 2034 | int err = -ENOBUFS; |
| 2035 | |
Eric Dumazet | fa17806 | 2016-07-08 05:18:24 +0200 | [diff] [blame] | 2036 | skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_KERNEL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 2037 | if (!skb) |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2038 | goto errout; |
| 2039 | |
| 2040 | err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, |
David Ahern | 3b02286 | 2017-03-28 14:28:02 -0700 | [diff] [blame] | 2041 | event, 0, type); |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2042 | if (err < 0) { |
| 2043 | /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */ |
| 2044 | WARN_ON(err == -EMSGSIZE); |
| 2045 | kfree_skb(skb); |
| 2046 | goto errout; |
| 2047 | } |
Eric Dumazet | fa17806 | 2016-07-08 05:18:24 +0200 | [diff] [blame] | 2048 | rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_KERNEL); |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2049 | return; |
| 2050 | errout: |
| 2051 | if (err < 0) |
| 2052 | rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err); |
| 2053 | } |
| 2054 | |
Nicolas Dichtel | 9e55111 | 2012-10-25 22:28:53 +0000 | [diff] [blame] | 2055 | static const struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = { |
| 2056 | [NETCONFA_IFINDEX] = { .len = sizeof(int) }, |
| 2057 | [NETCONFA_FORWARDING] = { .len = sizeof(int) }, |
Nicolas Dichtel | cc535df | 2012-10-29 04:53:27 +0000 | [diff] [blame] | 2058 | [NETCONFA_RP_FILTER] = { .len = sizeof(int) }, |
stephen hemminger | 09aea5d | 2013-12-17 22:35:52 -0800 | [diff] [blame] | 2059 | [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, |
Andy Gospodarek | 974d7af | 2015-07-07 13:56:57 -0400 | [diff] [blame] | 2060 | [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, |
Nicolas Dichtel | 9e55111 | 2012-10-25 22:28:53 +0000 | [diff] [blame] | 2061 | }; |
| 2062 | |
Jakub Kicinski | eede370 | 2019-01-18 10:46:18 -0800 | [diff] [blame] | 2063 | static int inet_netconf_valid_get_req(struct sk_buff *skb, |
| 2064 | const struct nlmsghdr *nlh, |
| 2065 | struct nlattr **tb, |
| 2066 | struct netlink_ext_ack *extack) |
| 2067 | { |
| 2068 | int i, err; |
| 2069 | |
| 2070 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) { |
| 2071 | NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf get request"); |
| 2072 | return -EINVAL; |
| 2073 | } |
| 2074 | |
| 2075 | if (!netlink_strict_get_check(skb)) |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2076 | return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg), |
| 2077 | tb, NETCONFA_MAX, |
| 2078 | devconf_ipv4_policy, extack); |
Jakub Kicinski | eede370 | 2019-01-18 10:46:18 -0800 | [diff] [blame] | 2079 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2080 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg), |
| 2081 | tb, NETCONFA_MAX, |
| 2082 | devconf_ipv4_policy, extack); |
Jakub Kicinski | eede370 | 2019-01-18 10:46:18 -0800 | [diff] [blame] | 2083 | if (err) |
| 2084 | return err; |
| 2085 | |
| 2086 | for (i = 0; i <= NETCONFA_MAX; i++) { |
| 2087 | if (!tb[i]) |
| 2088 | continue; |
| 2089 | |
| 2090 | switch (i) { |
| 2091 | case NETCONFA_IFINDEX: |
| 2092 | break; |
| 2093 | default: |
| 2094 | NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in netconf get request"); |
| 2095 | return -EINVAL; |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | return 0; |
| 2100 | } |
| 2101 | |
Nicolas Dichtel | 9e55111 | 2012-10-25 22:28:53 +0000 | [diff] [blame] | 2102 | static int inet_netconf_get_devconf(struct sk_buff *in_skb, |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 2103 | struct nlmsghdr *nlh, |
| 2104 | struct netlink_ext_ack *extack) |
Nicolas Dichtel | 9e55111 | 2012-10-25 22:28:53 +0000 | [diff] [blame] | 2105 | { |
| 2106 | struct net *net = sock_net(in_skb->sk); |
| 2107 | struct nlattr *tb[NETCONFA_MAX+1]; |
Nicolas Dichtel | 9e55111 | 2012-10-25 22:28:53 +0000 | [diff] [blame] | 2108 | struct sk_buff *skb; |
| 2109 | struct ipv4_devconf *devconf; |
| 2110 | struct in_device *in_dev; |
| 2111 | struct net_device *dev; |
| 2112 | int ifindex; |
| 2113 | int err; |
| 2114 | |
Jakub Kicinski | eede370 | 2019-01-18 10:46:18 -0800 | [diff] [blame] | 2115 | err = inet_netconf_valid_get_req(in_skb, nlh, tb, extack); |
| 2116 | if (err) |
Nicolas Dichtel | 9e55111 | 2012-10-25 22:28:53 +0000 | [diff] [blame] | 2117 | goto errout; |
| 2118 | |
Anton Protopopov | a97eb33 | 2016-02-16 21:43:16 -0500 | [diff] [blame] | 2119 | err = -EINVAL; |
Nicolas Dichtel | 9e55111 | 2012-10-25 22:28:53 +0000 | [diff] [blame] | 2120 | if (!tb[NETCONFA_IFINDEX]) |
| 2121 | goto errout; |
| 2122 | |
| 2123 | ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); |
| 2124 | switch (ifindex) { |
| 2125 | case NETCONFA_IFINDEX_ALL: |
| 2126 | devconf = net->ipv4.devconf_all; |
| 2127 | break; |
| 2128 | case NETCONFA_IFINDEX_DEFAULT: |
| 2129 | devconf = net->ipv4.devconf_dflt; |
| 2130 | break; |
| 2131 | default: |
| 2132 | dev = __dev_get_by_index(net, ifindex); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 2133 | if (!dev) |
Nicolas Dichtel | 9e55111 | 2012-10-25 22:28:53 +0000 | [diff] [blame] | 2134 | goto errout; |
| 2135 | in_dev = __in_dev_get_rtnl(dev); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 2136 | if (!in_dev) |
Nicolas Dichtel | 9e55111 | 2012-10-25 22:28:53 +0000 | [diff] [blame] | 2137 | goto errout; |
| 2138 | devconf = &in_dev->cnf; |
| 2139 | break; |
| 2140 | } |
| 2141 | |
| 2142 | err = -ENOBUFS; |
Eric Dumazet | fa17806 | 2016-07-08 05:18:24 +0200 | [diff] [blame] | 2143 | skb = nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 2144 | if (!skb) |
Nicolas Dichtel | 9e55111 | 2012-10-25 22:28:53 +0000 | [diff] [blame] | 2145 | goto errout; |
| 2146 | |
| 2147 | err = inet_netconf_fill_devconf(skb, ifindex, devconf, |
| 2148 | NETLINK_CB(in_skb).portid, |
| 2149 | nlh->nlmsg_seq, RTM_NEWNETCONF, 0, |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 2150 | NETCONFA_ALL); |
Nicolas Dichtel | 9e55111 | 2012-10-25 22:28:53 +0000 | [diff] [blame] | 2151 | if (err < 0) { |
| 2152 | /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */ |
| 2153 | WARN_ON(err == -EMSGSIZE); |
| 2154 | kfree_skb(skb); |
| 2155 | goto errout; |
| 2156 | } |
| 2157 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
| 2158 | errout: |
| 2159 | return err; |
| 2160 | } |
| 2161 | |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 2162 | static int inet_netconf_dump_devconf(struct sk_buff *skb, |
| 2163 | struct netlink_callback *cb) |
| 2164 | { |
David Ahern | addd383 | 2018-10-07 20:16:41 -0700 | [diff] [blame] | 2165 | const struct nlmsghdr *nlh = cb->nlh; |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 2166 | struct net *net = sock_net(skb->sk); |
| 2167 | int h, s_h; |
| 2168 | int idx, s_idx; |
| 2169 | struct net_device *dev; |
| 2170 | struct in_device *in_dev; |
| 2171 | struct hlist_head *head; |
| 2172 | |
David Ahern | addd383 | 2018-10-07 20:16:41 -0700 | [diff] [blame] | 2173 | if (cb->strict_check) { |
| 2174 | struct netlink_ext_ack *extack = cb->extack; |
| 2175 | struct netconfmsg *ncm; |
| 2176 | |
| 2177 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) { |
| 2178 | NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf dump request"); |
| 2179 | return -EINVAL; |
| 2180 | } |
| 2181 | |
| 2182 | if (nlmsg_attrlen(nlh, sizeof(*ncm))) { |
| 2183 | NL_SET_ERR_MSG(extack, "ipv4: Invalid data after header in netconf dump request"); |
| 2184 | return -EINVAL; |
| 2185 | } |
| 2186 | } |
| 2187 | |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 2188 | s_h = cb->args[0]; |
| 2189 | s_idx = idx = cb->args[1]; |
| 2190 | |
| 2191 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 2192 | idx = 0; |
| 2193 | head = &net->dev_index_head[h]; |
| 2194 | rcu_read_lock(); |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 2195 | cb->seq = atomic_read(&net->ipv4.dev_addr_genid) ^ |
| 2196 | net->dev_base_seq; |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 2197 | hlist_for_each_entry_rcu(dev, head, index_hlist) { |
| 2198 | if (idx < s_idx) |
| 2199 | goto cont; |
| 2200 | in_dev = __in_dev_get_rcu(dev); |
| 2201 | if (!in_dev) |
| 2202 | goto cont; |
| 2203 | |
| 2204 | if (inet_netconf_fill_devconf(skb, dev->ifindex, |
| 2205 | &in_dev->cnf, |
| 2206 | NETLINK_CB(cb->skb).portid, |
David Ahern | addd383 | 2018-10-07 20:16:41 -0700 | [diff] [blame] | 2207 | nlh->nlmsg_seq, |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 2208 | RTM_NEWNETCONF, |
| 2209 | NLM_F_MULTI, |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 2210 | NETCONFA_ALL) < 0) { |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 2211 | rcu_read_unlock(); |
| 2212 | goto done; |
| 2213 | } |
Nicolas Dichtel | 0465277 | 2013-03-22 06:28:42 +0000 | [diff] [blame] | 2214 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 2215 | cont: |
| 2216 | idx++; |
| 2217 | } |
| 2218 | rcu_read_unlock(); |
| 2219 | } |
| 2220 | if (h == NETDEV_HASHENTRIES) { |
| 2221 | if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, |
| 2222 | net->ipv4.devconf_all, |
| 2223 | NETLINK_CB(cb->skb).portid, |
David Ahern | addd383 | 2018-10-07 20:16:41 -0700 | [diff] [blame] | 2224 | nlh->nlmsg_seq, |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 2225 | RTM_NEWNETCONF, NLM_F_MULTI, |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 2226 | NETCONFA_ALL) < 0) |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 2227 | goto done; |
| 2228 | else |
| 2229 | h++; |
| 2230 | } |
| 2231 | if (h == NETDEV_HASHENTRIES + 1) { |
| 2232 | if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, |
| 2233 | net->ipv4.devconf_dflt, |
| 2234 | NETLINK_CB(cb->skb).portid, |
David Ahern | addd383 | 2018-10-07 20:16:41 -0700 | [diff] [blame] | 2235 | nlh->nlmsg_seq, |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 2236 | RTM_NEWNETCONF, NLM_F_MULTI, |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 2237 | NETCONFA_ALL) < 0) |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 2238 | goto done; |
| 2239 | else |
| 2240 | h++; |
| 2241 | } |
| 2242 | done: |
| 2243 | cb->args[0] = h; |
| 2244 | cb->args[1] = idx; |
| 2245 | |
| 2246 | return skb->len; |
| 2247 | } |
| 2248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | #ifdef CONFIG_SYSCTL |
| 2250 | |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 2251 | static void devinet_copy_dflt_conf(struct net *net, int i) |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 2252 | { |
| 2253 | struct net_device *dev; |
| 2254 | |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 2255 | rcu_read_lock(); |
| 2256 | for_each_netdev_rcu(net, dev) { |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 2257 | struct in_device *in_dev; |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 2258 | |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 2259 | in_dev = __in_dev_get_rcu(dev); |
| 2260 | if (in_dev && !test_bit(i, in_dev->cnf.state)) |
Pavel Emelyanov | 9355bbd | 2007-12-16 13:32:16 -0800 | [diff] [blame] | 2261 | in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i]; |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 2262 | } |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 2263 | rcu_read_unlock(); |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 2264 | } |
| 2265 | |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 2266 | /* called with RTNL locked */ |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 2267 | static void inet_forward_change(struct net *net) |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 2268 | { |
| 2269 | struct net_device *dev; |
Pavel Emelyanov | 586f121 | 2007-12-16 13:32:48 -0800 | [diff] [blame] | 2270 | int on = IPV4_DEVCONF_ALL(net, FORWARDING); |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 2271 | |
Pavel Emelyanov | 586f121 | 2007-12-16 13:32:48 -0800 | [diff] [blame] | 2272 | IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on; |
Pavel Emelyanov | 9355bbd | 2007-12-16 13:32:16 -0800 | [diff] [blame] | 2273 | IPV4_DEVCONF_DFLT(net, FORWARDING) = on; |
David Ahern | 3b02286 | 2017-03-28 14:28:02 -0700 | [diff] [blame] | 2274 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 2275 | NETCONFA_FORWARDING, |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2276 | NETCONFA_IFINDEX_ALL, |
| 2277 | net->ipv4.devconf_all); |
David Ahern | 3b02286 | 2017-03-28 14:28:02 -0700 | [diff] [blame] | 2278 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 2279 | NETCONFA_FORWARDING, |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2280 | NETCONFA_IFINDEX_DEFAULT, |
| 2281 | net->ipv4.devconf_dflt); |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 2282 | |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 2283 | for_each_netdev(net, dev) { |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 2284 | struct in_device *in_dev; |
Eric Dumazet | fa17806 | 2016-07-08 05:18:24 +0200 | [diff] [blame] | 2285 | |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 2286 | if (on) |
| 2287 | dev_disable_lro(dev); |
Eric Dumazet | fa17806 | 2016-07-08 05:18:24 +0200 | [diff] [blame] | 2288 | |
| 2289 | in_dev = __in_dev_get_rtnl(dev); |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2290 | if (in_dev) { |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 2291 | IN_DEV_CONF_SET(in_dev, FORWARDING, on); |
David Ahern | 3b02286 | 2017-03-28 14:28:02 -0700 | [diff] [blame] | 2292 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 2293 | NETCONFA_FORWARDING, |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2294 | dev->ifindex, &in_dev->cnf); |
| 2295 | } |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 2296 | } |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 2297 | } |
| 2298 | |
stephen hemminger | f085ff1 | 2013-12-12 13:06:50 -0800 | [diff] [blame] | 2299 | static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf) |
| 2300 | { |
| 2301 | if (cnf == net->ipv4.devconf_dflt) |
| 2302 | return NETCONFA_IFINDEX_DEFAULT; |
| 2303 | else if (cnf == net->ipv4.devconf_all) |
| 2304 | return NETCONFA_IFINDEX_ALL; |
| 2305 | else { |
| 2306 | struct in_device *idev |
| 2307 | = container_of(cnf, struct in_device, cnf); |
| 2308 | return idev->dev->ifindex; |
| 2309 | } |
| 2310 | } |
| 2311 | |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 2312 | static int devinet_conf_proc(struct ctl_table *ctl, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 2313 | void __user *buffer, |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 2314 | size_t *lenp, loff_t *ppos) |
| 2315 | { |
Peter Pan(潘卫平) | d01ff0a | 2011-12-01 15:47:06 +0000 | [diff] [blame] | 2316 | int old_value = *(int *)ctl->data; |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 2317 | int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
Peter Pan(潘卫平) | d01ff0a | 2011-12-01 15:47:06 +0000 | [diff] [blame] | 2318 | int new_value = *(int *)ctl->data; |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 2319 | |
| 2320 | if (write) { |
| 2321 | struct ipv4_devconf *cnf = ctl->extra1; |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 2322 | struct net *net = ctl->extra2; |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 2323 | int i = (int *)ctl->data - cnf->data; |
stephen hemminger | f085ff1 | 2013-12-12 13:06:50 -0800 | [diff] [blame] | 2324 | int ifindex; |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 2325 | |
| 2326 | set_bit(i, cnf->state); |
| 2327 | |
Pavel Emelyanov | 9355bbd | 2007-12-16 13:32:16 -0800 | [diff] [blame] | 2328 | if (cnf == net->ipv4.devconf_dflt) |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 2329 | devinet_copy_dflt_conf(net, i); |
Thomas Graf | d0daebc3 | 2012-06-12 00:44:01 +0000 | [diff] [blame] | 2330 | if (i == IPV4_DEVCONF_ACCEPT_LOCAL - 1 || |
| 2331 | i == IPV4_DEVCONF_ROUTE_LOCALNET - 1) |
Peter Pan(潘卫平) | d01ff0a | 2011-12-01 15:47:06 +0000 | [diff] [blame] | 2332 | if ((new_value == 0) && (old_value != 0)) |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 2333 | rt_cache_flush(net); |
stephen hemminger | f085ff1 | 2013-12-12 13:06:50 -0800 | [diff] [blame] | 2334 | |
Xin Long | 5cbf777 | 2018-07-27 16:37:28 +0800 | [diff] [blame] | 2335 | if (i == IPV4_DEVCONF_BC_FORWARDING - 1 && |
| 2336 | new_value != old_value) |
| 2337 | rt_cache_flush(net); |
| 2338 | |
Nicolas Dichtel | cc535df | 2012-10-29 04:53:27 +0000 | [diff] [blame] | 2339 | if (i == IPV4_DEVCONF_RP_FILTER - 1 && |
| 2340 | new_value != old_value) { |
stephen hemminger | f085ff1 | 2013-12-12 13:06:50 -0800 | [diff] [blame] | 2341 | ifindex = devinet_conf_ifindex(net, cnf); |
David Ahern | 3b02286 | 2017-03-28 14:28:02 -0700 | [diff] [blame] | 2342 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 2343 | NETCONFA_RP_FILTER, |
Nicolas Dichtel | cc535df | 2012-10-29 04:53:27 +0000 | [diff] [blame] | 2344 | ifindex, cnf); |
| 2345 | } |
stephen hemminger | f085ff1 | 2013-12-12 13:06:50 -0800 | [diff] [blame] | 2346 | if (i == IPV4_DEVCONF_PROXY_ARP - 1 && |
| 2347 | new_value != old_value) { |
| 2348 | ifindex = devinet_conf_ifindex(net, cnf); |
David Ahern | 3b02286 | 2017-03-28 14:28:02 -0700 | [diff] [blame] | 2349 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 2350 | NETCONFA_PROXY_NEIGH, |
stephen hemminger | f085ff1 | 2013-12-12 13:06:50 -0800 | [diff] [blame] | 2351 | ifindex, cnf); |
| 2352 | } |
Andy Gospodarek | 974d7af | 2015-07-07 13:56:57 -0400 | [diff] [blame] | 2353 | if (i == IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN - 1 && |
| 2354 | new_value != old_value) { |
| 2355 | ifindex = devinet_conf_ifindex(net, cnf); |
David Ahern | 3b02286 | 2017-03-28 14:28:02 -0700 | [diff] [blame] | 2356 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 2357 | NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, |
Andy Gospodarek | 974d7af | 2015-07-07 13:56:57 -0400 | [diff] [blame] | 2358 | ifindex, cnf); |
| 2359 | } |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | return ret; |
| 2363 | } |
| 2364 | |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 2365 | static int devinet_sysctl_forward(struct ctl_table *ctl, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 2366 | void __user *buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | size_t *lenp, loff_t *ppos) |
| 2368 | { |
| 2369 | int *valp = ctl->data; |
| 2370 | int val = *valp; |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 2371 | loff_t pos = *ppos; |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 2372 | int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | |
| 2374 | if (write && *valp != val) { |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 2375 | struct net *net = ctl->extra2; |
| 2376 | |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 2377 | if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) { |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 2378 | if (!rtnl_trylock()) { |
| 2379 | /* Restore the original values before restarting */ |
| 2380 | *valp = val; |
| 2381 | *ppos = pos; |
Eric W. Biederman | 9b8adb5 | 2009-05-13 16:59:21 +0000 | [diff] [blame] | 2382 | return restart_syscall(); |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 2383 | } |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 2384 | if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) { |
| 2385 | inet_forward_change(net); |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2386 | } else { |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 2387 | struct ipv4_devconf *cnf = ctl->extra1; |
| 2388 | struct in_device *idev = |
| 2389 | container_of(cnf, struct in_device, cnf); |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2390 | if (*valp) |
| 2391 | dev_disable_lro(idev->dev); |
David Ahern | 3b02286 | 2017-03-28 14:28:02 -0700 | [diff] [blame] | 2392 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2393 | NETCONFA_FORWARDING, |
| 2394 | idev->dev->ifindex, |
| 2395 | cnf); |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 2396 | } |
| 2397 | rtnl_unlock(); |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 2398 | rt_cache_flush(net); |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2399 | } else |
David Ahern | 3b02286 | 2017-03-28 14:28:02 -0700 | [diff] [blame] | 2400 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 2401 | NETCONFA_FORWARDING, |
Nicolas Dichtel | edc9e74 | 2012-10-25 22:28:52 +0000 | [diff] [blame] | 2402 | NETCONFA_IFINDEX_DEFAULT, |
| 2403 | net->ipv4.devconf_dflt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | return ret; |
| 2407 | } |
| 2408 | |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 2409 | static int ipv4_doint_and_flush(struct ctl_table *ctl, int write, |
David S. Miller | 323e126 | 2010-12-12 21:55:08 -0800 | [diff] [blame] | 2410 | void __user *buffer, |
| 2411 | size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | { |
| 2413 | int *valp = ctl->data; |
| 2414 | int val = *valp; |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 2415 | int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
Denis V. Lunev | 76e6ebf | 2008-07-05 19:00:44 -0700 | [diff] [blame] | 2416 | struct net *net = ctl->extra2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | |
| 2418 | if (write && *valp != val) |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 2419 | rt_cache_flush(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | |
| 2421 | return ret; |
| 2422 | } |
| 2423 | |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 2424 | #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \ |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2425 | { \ |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2426 | .procname = name, \ |
| 2427 | .data = ipv4_devconf.data + \ |
Eric W. Biederman | 0229168 | 2010-02-14 03:25:51 +0000 | [diff] [blame] | 2428 | IPV4_DEVCONF_ ## attr - 1, \ |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2429 | .maxlen = sizeof(int), \ |
| 2430 | .mode = mval, \ |
| 2431 | .proc_handler = proc, \ |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 2432 | .extra1 = &ipv4_devconf, \ |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \ |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 2436 | DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc) |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2437 | |
| 2438 | #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \ |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 2439 | DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc) |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2440 | |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 2441 | #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \ |
| 2442 | DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc) |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2443 | |
| 2444 | #define DEVINET_SYSCTL_FLUSHING_ENTRY(attr, name) \ |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 2445 | DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, ipv4_doint_and_flush) |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | static struct devinet_sysctl_table { |
| 2448 | struct ctl_table_header *sysctl_header; |
Eric W. Biederman | 0229168 | 2010-02-14 03:25:51 +0000 | [diff] [blame] | 2449 | struct ctl_table devinet_vars[__IPV4_DEVCONF_MAX]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | } devinet_sysctl = { |
| 2451 | .devinet_vars = { |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2452 | DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding", |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 2453 | devinet_sysctl_forward), |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2454 | DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"), |
Xin Long | 5cbf777 | 2018-07-27 16:37:28 +0800 | [diff] [blame] | 2455 | DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"), |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2456 | |
| 2457 | DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"), |
| 2458 | DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"), |
| 2459 | DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"), |
| 2460 | DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"), |
| 2461 | DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"), |
| 2462 | DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE, |
| 2463 | "accept_source_route"), |
Patrick McHardy | 8153a10 | 2009-12-03 01:25:58 +0000 | [diff] [blame] | 2464 | DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"), |
Jamal Hadi Salim | 28f6aee | 2009-12-25 17:30:22 -0800 | [diff] [blame] | 2465 | DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"), |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2466 | DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"), |
| 2467 | DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"), |
| 2468 | DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"), |
| 2469 | DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"), |
| 2470 | DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"), |
| 2471 | DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"), |
| 2472 | DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"), |
| 2473 | DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"), |
| 2474 | DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"), |
Stephen Hemminger | eefef1c | 2009-02-01 01:04:33 -0800 | [diff] [blame] | 2475 | DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"), |
Jesper Dangaard Brouer | 6532414 | 2010-01-05 05:50:47 +0000 | [diff] [blame] | 2476 | DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"), |
William Manley | 5c6fe01 | 2013-08-06 19:03:14 +0100 | [diff] [blame] | 2477 | DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION, |
| 2478 | "force_igmp_version"), |
William Manley | 2690048 | 2013-08-06 19:03:15 +0100 | [diff] [blame] | 2479 | DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL, |
| 2480 | "igmpv2_unsolicited_report_interval"), |
| 2481 | DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL, |
| 2482 | "igmpv3_unsolicited_report_interval"), |
Andy Gospodarek | 0eeb075 | 2015-06-23 13:45:37 -0400 | [diff] [blame] | 2483 | DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN, |
| 2484 | "ignore_routes_with_linkdown"), |
Johannes Berg | 97daf33 | 2016-02-04 13:31:18 +0100 | [diff] [blame] | 2485 | DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP, |
| 2486 | "drop_gratuitous_arp"), |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2487 | |
| 2488 | DEVINET_SYSCTL_FLUSHING_ENTRY(NOXFRM, "disable_xfrm"), |
| 2489 | DEVINET_SYSCTL_FLUSHING_ENTRY(NOPOLICY, "disable_policy"), |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 2490 | DEVINET_SYSCTL_FLUSHING_ENTRY(PROMOTE_SECONDARIES, |
| 2491 | "promote_secondaries"), |
Thomas Graf | d0daebc3 | 2012-06-12 00:44:01 +0000 | [diff] [blame] | 2492 | DEVINET_SYSCTL_FLUSHING_ENTRY(ROUTE_LOCALNET, |
| 2493 | "route_localnet"), |
Johannes Berg | 12b74df | 2016-02-04 13:31:17 +0100 | [diff] [blame] | 2494 | DEVINET_SYSCTL_FLUSHING_ENTRY(DROP_UNICAST_IN_L2_MULTICAST, |
| 2495 | "drop_unicast_in_l2_multicast"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | }; |
| 2498 | |
Pavel Emelyanov | ea40b32 | 2007-12-16 13:30:07 -0800 | [diff] [blame] | 2499 | static int __devinet_sysctl_register(struct net *net, char *dev_name, |
Nicolas Dichtel | 29c994e | 2016-08-30 10:09:22 +0200 | [diff] [blame] | 2500 | int ifindex, struct ipv4_devconf *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | { |
| 2502 | int i; |
Pavel Emelyanov | 9fa8964 | 2007-12-02 00:17:46 +1100 | [diff] [blame] | 2503 | struct devinet_sysctl_table *t; |
Eric W. Biederman | 8607ddb | 2012-04-19 13:42:09 +0000 | [diff] [blame] | 2504 | char path[sizeof("net/ipv4/conf/") + IFNAMSIZ]; |
Pavel Emelyanov | bfada69 | 2007-12-02 00:57:08 +1100 | [diff] [blame] | 2505 | |
Pavel Emelyanov | 9fa8964 | 2007-12-02 00:17:46 +1100 | [diff] [blame] | 2506 | t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | if (!t) |
Pavel Emelyanov | 9fa8964 | 2007-12-02 00:17:46 +1100 | [diff] [blame] | 2508 | goto out; |
| 2509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | for (i = 0; i < ARRAY_SIZE(t->devinet_vars) - 1; i++) { |
| 2511 | t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf; |
Herbert Xu | 31be308 | 2007-06-04 23:35:37 -0700 | [diff] [blame] | 2512 | t->devinet_vars[i].extra1 = p; |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 2513 | t->devinet_vars[i].extra2 = net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | } |
| 2515 | |
Eric W. Biederman | 8607ddb | 2012-04-19 13:42:09 +0000 | [diff] [blame] | 2516 | snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | |
Eric W. Biederman | 8607ddb | 2012-04-19 13:42:09 +0000 | [diff] [blame] | 2518 | t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | if (!t->sysctl_header) |
Eric W. Biederman | 8607ddb | 2012-04-19 13:42:09 +0000 | [diff] [blame] | 2520 | goto free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | |
| 2522 | p->sysctl = t; |
Nicolas Dichtel | 29c994e | 2016-08-30 10:09:22 +0200 | [diff] [blame] | 2523 | |
David Ahern | 3b02286 | 2017-03-28 14:28:02 -0700 | [diff] [blame] | 2524 | inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, |
| 2525 | ifindex, p); |
Pavel Emelyanov | ea40b32 | 2007-12-16 13:30:07 -0800 | [diff] [blame] | 2526 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | |
Pavel Emelyanov | 9fa8964 | 2007-12-02 00:17:46 +1100 | [diff] [blame] | 2528 | free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | kfree(t); |
Pavel Emelyanov | 9fa8964 | 2007-12-02 00:17:46 +1100 | [diff] [blame] | 2530 | out: |
Pavel Emelyanov | ea40b32 | 2007-12-16 13:30:07 -0800 | [diff] [blame] | 2531 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | } |
| 2533 | |
David Ahern | b5c9641 | 2017-03-28 14:28:03 -0700 | [diff] [blame] | 2534 | static void __devinet_sysctl_unregister(struct net *net, |
| 2535 | struct ipv4_devconf *cnf, int ifindex) |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 2536 | { |
| 2537 | struct devinet_sysctl_table *t = cnf->sysctl; |
| 2538 | |
David Ahern | b5c9641 | 2017-03-28 14:28:03 -0700 | [diff] [blame] | 2539 | if (t) { |
| 2540 | cnf->sysctl = NULL; |
| 2541 | unregister_net_sysctl_table(t->sysctl_header); |
| 2542 | kfree(t); |
| 2543 | } |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 2544 | |
David Ahern | b5c9641 | 2017-03-28 14:28:03 -0700 | [diff] [blame] | 2545 | inet_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 2546 | } |
| 2547 | |
WANG Cong | 20e61da | 2014-07-25 15:25:08 -0700 | [diff] [blame] | 2548 | static int devinet_sysctl_register(struct in_device *idev) |
Pavel Emelyanov | 66f27a5 | 2007-12-02 00:55:54 +1100 | [diff] [blame] | 2549 | { |
WANG Cong | 20e61da | 2014-07-25 15:25:08 -0700 | [diff] [blame] | 2550 | int err; |
| 2551 | |
| 2552 | if (!sysctl_dev_name_is_allowed(idev->dev->name)) |
| 2553 | return -EINVAL; |
| 2554 | |
| 2555 | err = neigh_sysctl_register(idev->dev, idev->arp_parms, NULL); |
| 2556 | if (err) |
| 2557 | return err; |
| 2558 | err = __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name, |
Nicolas Dichtel | 29c994e | 2016-08-30 10:09:22 +0200 | [diff] [blame] | 2559 | idev->dev->ifindex, &idev->cnf); |
WANG Cong | 20e61da | 2014-07-25 15:25:08 -0700 | [diff] [blame] | 2560 | if (err) |
| 2561 | neigh_sysctl_unregister(idev->arp_parms); |
| 2562 | return err; |
Pavel Emelyanov | 66f27a5 | 2007-12-02 00:55:54 +1100 | [diff] [blame] | 2563 | } |
| 2564 | |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 2565 | static void devinet_sysctl_unregister(struct in_device *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | { |
David Ahern | b5c9641 | 2017-03-28 14:28:03 -0700 | [diff] [blame] | 2567 | struct net *net = dev_net(idev->dev); |
| 2568 | |
| 2569 | __devinet_sysctl_unregister(net, &idev->cnf, idev->dev->ifindex); |
Pavel Emelyanov | 51602b2 | 2007-12-11 02:17:40 -0800 | [diff] [blame] | 2570 | neigh_sysctl_unregister(idev->arp_parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 2573 | static struct ctl_table ctl_forward_entry[] = { |
| 2574 | { |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 2575 | .procname = "ip_forward", |
| 2576 | .data = &ipv4_devconf.data[ |
Eric W. Biederman | 0229168 | 2010-02-14 03:25:51 +0000 | [diff] [blame] | 2577 | IPV4_DEVCONF_FORWARDING - 1], |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 2578 | .maxlen = sizeof(int), |
| 2579 | .mode = 0644, |
| 2580 | .proc_handler = devinet_sysctl_forward, |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 2581 | .extra1 = &ipv4_devconf, |
Pavel Emelyanov | c0ce9fb | 2007-12-16 13:31:14 -0800 | [diff] [blame] | 2582 | .extra2 = &init_net, |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 2583 | }, |
| 2584 | { }, |
| 2585 | }; |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 2586 | #endif |
Pavel Emelyanov | 68dd299 | 2007-12-05 01:44:58 -0800 | [diff] [blame] | 2587 | |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2588 | static __net_init int devinet_init_net(struct net *net) |
| 2589 | { |
| 2590 | int err; |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2591 | struct ipv4_devconf *all, *dflt; |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 2592 | #ifdef CONFIG_SYSCTL |
Cong Wang | 856c395 | 2019-01-17 23:27:11 -0800 | [diff] [blame] | 2593 | struct ctl_table *tbl; |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2594 | struct ctl_table_header *forw_hdr; |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 2595 | #endif |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2596 | |
| 2597 | err = -ENOMEM; |
Cong Wang | 856c395 | 2019-01-17 23:27:11 -0800 | [diff] [blame] | 2598 | all = kmemdup(&ipv4_devconf, sizeof(ipv4_devconf), GFP_KERNEL); |
| 2599 | if (!all) |
| 2600 | goto err_alloc_all; |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2601 | |
Cong Wang | 856c395 | 2019-01-17 23:27:11 -0800 | [diff] [blame] | 2602 | dflt = kmemdup(&ipv4_devconf_dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL); |
| 2603 | if (!dflt) |
| 2604 | goto err_alloc_dflt; |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2605 | |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 2606 | #ifdef CONFIG_SYSCTL |
Cong Wang | 856c395 | 2019-01-17 23:27:11 -0800 | [diff] [blame] | 2607 | tbl = kmemdup(ctl_forward_entry, sizeof(ctl_forward_entry), GFP_KERNEL); |
| 2608 | if (!tbl) |
| 2609 | goto err_alloc_ctl; |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2610 | |
Cong Wang | 856c395 | 2019-01-17 23:27:11 -0800 | [diff] [blame] | 2611 | tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1]; |
| 2612 | tbl[0].extra1 = all; |
| 2613 | tbl[0].extra2 = net; |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 2614 | #endif |
Cong Wang | 856c395 | 2019-01-17 23:27:11 -0800 | [diff] [blame] | 2615 | |
Arnd Bergmann | a154d5d | 2019-03-04 21:38:03 +0100 | [diff] [blame] | 2616 | if ((!IS_ENABLED(CONFIG_SYSCTL) || |
| 2617 | sysctl_devconf_inherit_init_net != 2) && |
| 2618 | !net_eq(net, &init_net)) { |
Cong Wang | 856c395 | 2019-01-17 23:27:11 -0800 | [diff] [blame] | 2619 | memcpy(all, init_net.ipv4.devconf_all, sizeof(ipv4_devconf)); |
| 2620 | memcpy(dflt, init_net.ipv4.devconf_dflt, sizeof(ipv4_devconf_dflt)); |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | #ifdef CONFIG_SYSCTL |
Nicolas Dichtel | 29c994e | 2016-08-30 10:09:22 +0200 | [diff] [blame] | 2624 | err = __devinet_sysctl_register(net, "all", NETCONFA_IFINDEX_ALL, all); |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2625 | if (err < 0) |
| 2626 | goto err_reg_all; |
| 2627 | |
Nicolas Dichtel | 29c994e | 2016-08-30 10:09:22 +0200 | [diff] [blame] | 2628 | err = __devinet_sysctl_register(net, "default", |
| 2629 | NETCONFA_IFINDEX_DEFAULT, dflt); |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2630 | if (err < 0) |
| 2631 | goto err_reg_dflt; |
| 2632 | |
| 2633 | err = -ENOMEM; |
Eric W. Biederman | 8607ddb | 2012-04-19 13:42:09 +0000 | [diff] [blame] | 2634 | forw_hdr = register_net_sysctl(net, "net/ipv4", tbl); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 2635 | if (!forw_hdr) |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2636 | goto err_reg_ctl; |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 2637 | net->ipv4.forw_hdr = forw_hdr; |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2638 | #endif |
| 2639 | |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2640 | net->ipv4.devconf_all = all; |
| 2641 | net->ipv4.devconf_dflt = dflt; |
| 2642 | return 0; |
| 2643 | |
| 2644 | #ifdef CONFIG_SYSCTL |
| 2645 | err_reg_ctl: |
David Ahern | b5c9641 | 2017-03-28 14:28:03 -0700 | [diff] [blame] | 2646 | __devinet_sysctl_unregister(net, dflt, NETCONFA_IFINDEX_DEFAULT); |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2647 | err_reg_dflt: |
David Ahern | b5c9641 | 2017-03-28 14:28:03 -0700 | [diff] [blame] | 2648 | __devinet_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2649 | err_reg_all: |
Cong Wang | 856c395 | 2019-01-17 23:27:11 -0800 | [diff] [blame] | 2650 | kfree(tbl); |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2651 | err_alloc_ctl: |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 2652 | #endif |
Cong Wang | 856c395 | 2019-01-17 23:27:11 -0800 | [diff] [blame] | 2653 | kfree(dflt); |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2654 | err_alloc_dflt: |
Cong Wang | 856c395 | 2019-01-17 23:27:11 -0800 | [diff] [blame] | 2655 | kfree(all); |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2656 | err_alloc_all: |
| 2657 | return err; |
| 2658 | } |
| 2659 | |
| 2660 | static __net_exit void devinet_exit_net(struct net *net) |
| 2661 | { |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 2662 | #ifdef CONFIG_SYSCTL |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2663 | struct ctl_table *tbl; |
| 2664 | |
| 2665 | tbl = net->ipv4.forw_hdr->ctl_table_arg; |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2666 | unregister_net_sysctl_table(net->ipv4.forw_hdr); |
David Ahern | b5c9641 | 2017-03-28 14:28:03 -0700 | [diff] [blame] | 2667 | __devinet_sysctl_unregister(net, net->ipv4.devconf_dflt, |
| 2668 | NETCONFA_IFINDEX_DEFAULT); |
| 2669 | __devinet_sysctl_unregister(net, net->ipv4.devconf_all, |
| 2670 | NETCONFA_IFINDEX_ALL); |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2671 | kfree(tbl); |
Eric Dumazet | 2a75de0 | 2008-01-05 23:08:49 -0800 | [diff] [blame] | 2672 | #endif |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2673 | kfree(net->ipv4.devconf_dflt); |
| 2674 | kfree(net->ipv4.devconf_all); |
| 2675 | } |
| 2676 | |
| 2677 | static __net_initdata struct pernet_operations devinet_ops = { |
| 2678 | .init = devinet_init_net, |
| 2679 | .exit = devinet_exit_net, |
| 2680 | }; |
| 2681 | |
Daniel Borkmann | 207895fd3 | 2015-01-29 12:15:03 +0100 | [diff] [blame] | 2682 | static struct rtnl_af_ops inet_af_ops __read_mostly = { |
Thomas Graf | 9f0f727 | 2010-11-16 04:32:48 +0000 | [diff] [blame] | 2683 | .family = AF_INET, |
| 2684 | .fill_link_af = inet_fill_link_af, |
| 2685 | .get_link_af_size = inet_get_link_af_size, |
Thomas Graf | cf7afbf | 2010-11-22 01:31:54 +0000 | [diff] [blame] | 2686 | .validate_link_af = inet_validate_link_af, |
| 2687 | .set_link_af = inet_set_link_af, |
Thomas Graf | 9f0f727 | 2010-11-16 04:32:48 +0000 | [diff] [blame] | 2688 | }; |
| 2689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | void __init devinet_init(void) |
| 2691 | { |
David S. Miller | fd23c3b | 2011-02-18 12:42:28 -0800 | [diff] [blame] | 2692 | int i; |
| 2693 | |
| 2694 | for (i = 0; i < IN4_ADDR_HSIZE; i++) |
| 2695 | INIT_HLIST_HEAD(&inet_addr_lst[i]); |
| 2696 | |
Pavel Emelyanov | 752d14d | 2007-12-16 13:31:47 -0800 | [diff] [blame] | 2697 | register_pernet_subsys(&devinet_ops); |
| 2698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | register_gifconf(PF_INET, inet_gifconf); |
| 2700 | register_netdevice_notifier(&ip_netdev_notifier); |
Thomas Graf | 63f3444 | 2007-03-22 11:55:17 -0700 | [diff] [blame] | 2701 | |
viresh kumar | 906e073 | 2014-01-22 12:23:32 +0530 | [diff] [blame] | 2702 | queue_delayed_work(system_power_efficient_wq, &check_lifetime_work, 0); |
Jiri Pirko | 5c766d6 | 2013-01-24 09:41:41 +0000 | [diff] [blame] | 2703 | |
Thomas Graf | 9f0f727 | 2010-11-16 04:32:48 +0000 | [diff] [blame] | 2704 | rtnl_af_register(&inet_af_ops); |
| 2705 | |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 2706 | rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, 0); |
| 2707 | rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, 0); |
| 2708 | rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, 0); |
Nicolas Dichtel | 9e55111 | 2012-10-25 22:28:53 +0000 | [diff] [blame] | 2709 | rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 2710 | inet_netconf_dump_devconf, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | } |