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