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