Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * IPv6 Address [auto]configuration |
| 4 | * Linux INET6 implementation |
| 5 | * |
| 6 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 7 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * Changes: |
| 13 | * |
| 14 | * Janos Farkas : delete timer on ifdown |
| 15 | * <chexum@bankinf.banki.hu> |
| 16 | * Andi Kleen : kill double kfree on module |
| 17 | * unload. |
| 18 | * Maciej W. Rozycki : FDDI support |
| 19 | * sekiya@USAGI : Don't send too many RS |
| 20 | * packets. |
| 21 | * yoshfuji@USAGI : Fixed interval between DAD |
| 22 | * packets. |
| 23 | * YOSHIFUJI Hideaki @USAGI : improved accuracy of |
| 24 | * address validation timer. |
| 25 | * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) |
| 26 | * support. |
| 27 | * Yuji SEKIYA @USAGI : Don't assign a same IPv6 |
| 28 | * address on a same interface. |
| 29 | * YOSHIFUJI Hideaki @USAGI : ARCnet support |
| 30 | * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to |
| 31 | * seq_file. |
YOSHIFUJI Hideaki | b1cacb6 | 2005-11-08 09:38:12 -0800 | [diff] [blame] | 32 | * YOSHIFUJI Hideaki @USAGI : improved source address |
| 33 | * selection; consider scope, |
| 34 | * status etc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | */ |
| 36 | |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 37 | #define pr_fmt(fmt) "IPv6: " fmt |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/errno.h> |
| 40 | #include <linux/types.h> |
Ilpo Järvinen | a0bffff | 2009-03-21 13:36:17 -0700 | [diff] [blame] | 41 | #include <linux/kernel.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 42 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/socket.h> |
| 44 | #include <linux/sockios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/net.h> |
Hannes Frederic Sowa | 3d1bec9 | 2015-03-23 23:36:00 +0100 | [diff] [blame] | 46 | #include <linux/inet.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/in6.h> |
| 48 | #include <linux/netdevice.h> |
Thomas Graf | 1823730 | 2006-08-04 23:04:54 -0700 | [diff] [blame] | 49 | #include <linux/if_addr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/if_arp.h> |
| 51 | #include <linux/if_arcnet.h> |
| 52 | #include <linux/if_infiniband.h> |
| 53 | #include <linux/route.h> |
| 54 | #include <linux/inetdevice.h> |
| 55 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 56 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #ifdef CONFIG_SYSCTL |
| 58 | #include <linux/sysctl.h> |
| 59 | #endif |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 60 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <linux/delay.h> |
| 62 | #include <linux/notifier.h> |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 63 | #include <linux/string.h> |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 64 | #include <linux/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 66 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #include <net/sock.h> |
| 68 | #include <net/snmp.h> |
| 69 | |
Alexander Aring | 5241c2d | 2015-12-14 20:55:22 +0100 | [diff] [blame] | 70 | #include <net/6lowpan.h> |
YOSHIFUJI Hideaki / 吉藤英明 | cb6bf35 | 2013-03-25 08:26:24 +0000 | [diff] [blame] | 71 | #include <net/firewire.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <net/ipv6.h> |
| 73 | #include <net/protocol.h> |
| 74 | #include <net/ndisc.h> |
| 75 | #include <net/ip6_route.h> |
| 76 | #include <net/addrconf.h> |
| 77 | #include <net/tcp.h> |
| 78 | #include <net/ip.h> |
Thomas Graf | 5d62026 | 2006-08-15 00:35:02 -0700 | [diff] [blame] | 79 | #include <net/netlink.h> |
Mitsuru Chinen | f24e3d6 | 2007-10-10 02:53:43 -0700 | [diff] [blame] | 80 | #include <net/pkt_sched.h> |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 81 | #include <net/l3mdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #include <linux/if_tunnel.h> |
| 83 | #include <linux/rtnetlink.h> |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 84 | #include <linux/netconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | #include <linux/random.h> |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 86 | #include <linux/uaccess.h> |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 87 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | #include <linux/proc_fs.h> |
| 90 | #include <linux/seq_file.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 91 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | #define INFINITY_LIFE_TIME 0xFFFFFFFF |
Thomas Graf | 18a31e1 | 2010-11-17 04:12:02 +0000 | [diff] [blame] | 94 | |
Hannes Frederic Sowa | 3d1bec9 | 2015-03-23 23:36:00 +0100 | [diff] [blame] | 95 | #define IPV6_MAX_STRLEN \ |
| 96 | sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") |
| 97 | |
Thomas Graf | 18a31e1 | 2010-11-17 04:12:02 +0000 | [diff] [blame] | 98 | static inline u32 cstamp_delta(unsigned long cstamp) |
| 99 | { |
| 100 | return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; |
| 101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 103 | static inline s32 rfc3315_s14_backoff_init(s32 irt) |
| 104 | { |
| 105 | /* multiply 'initial retransmission time' by 0.9 .. 1.1 */ |
| 106 | u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt; |
| 107 | do_div(tmp, 1000000); |
| 108 | return (s32)tmp; |
| 109 | } |
| 110 | |
| 111 | static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt) |
| 112 | { |
| 113 | /* multiply 'retransmission timeout' by 1.9 .. 2.1 */ |
| 114 | u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt; |
| 115 | do_div(tmp, 1000000); |
| 116 | if ((s32)tmp > mrt) { |
| 117 | /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */ |
| 118 | tmp = (900000 + prandom_u32() % 200001) * (u64)mrt; |
| 119 | do_div(tmp, 1000000); |
| 120 | } |
| 121 | return (s32)tmp; |
| 122 | } |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | #ifdef CONFIG_SYSCTL |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 125 | static int addrconf_sysctl_register(struct inet6_dev *idev); |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 126 | static void addrconf_sysctl_unregister(struct inet6_dev *idev); |
| 127 | #else |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 128 | static inline int addrconf_sysctl_register(struct inet6_dev *idev) |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 129 | { |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 130 | return 0; |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) |
| 134 | { |
| 135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | #endif |
| 137 | |
Jiri Bohac | 9d6280d | 2016-10-13 18:50:02 +0200 | [diff] [blame] | 138 | static void ipv6_regen_rndid(struct inet6_dev *idev); |
| 139 | static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 141 | static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); |
Eric Dumazet | d9bf82c | 2017-10-07 19:30:24 -0700 | [diff] [blame] | 142 | static int ipv6_count_addresses(const struct inet6_dev *idev); |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 143 | static int ipv6_generate_stable_address(struct in6_addr *addr, |
| 144 | u8 dad_count, |
| 145 | const struct inet6_dev *idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Eric Dumazet | 27c565a | 2017-11-04 08:53:27 -0700 | [diff] [blame] | 147 | #define IN6_ADDR_HSIZE_SHIFT 8 |
| 148 | #define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | /* |
| 150 | * Configured unicast address hash table |
| 151 | */ |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 152 | static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 153 | static DEFINE_SPINLOCK(addrconf_hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 155 | static void addrconf_verify(void); |
| 156 | static void addrconf_verify_rtnl(void); |
| 157 | static void addrconf_verify_work(struct work_struct *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 159 | static struct workqueue_struct *addrconf_wq; |
| 160 | static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | static void addrconf_join_anycast(struct inet6_ifaddr *ifp); |
| 163 | static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); |
| 164 | |
Jiri Pirko | 93d9b7d | 2010-03-10 10:28:56 +0000 | [diff] [blame] | 165 | static void addrconf_type_change(struct net_device *dev, |
| 166 | unsigned long event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | static int addrconf_ifdown(struct net_device *dev, int how); |
| 168 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 169 | static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, |
Romain Kuntz | 21caa66 | 2013-01-09 21:06:03 +0000 | [diff] [blame] | 170 | int plen, |
| 171 | const struct net_device *dev, |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 172 | u32 flags, u32 noflags, |
| 173 | bool no_gw); |
Romain Kuntz | 21caa66 | 2013-01-09 21:06:03 +0000 | [diff] [blame] | 174 | |
David S. Miller | cf22f9a | 2012-04-14 21:37:40 -0400 | [diff] [blame] | 175 | static void addrconf_dad_start(struct inet6_ifaddr *ifp); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 176 | static void addrconf_dad_work(struct work_struct *w); |
David Ahern | c76fe2d | 2018-01-25 20:16:29 -0800 | [diff] [blame] | 177 | static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, |
| 178 | bool send_na); |
Hangbin Liu | 896585d | 2018-11-21 21:52:33 +0800 | [diff] [blame] | 179 | static void addrconf_dad_run(struct inet6_dev *idev, bool restart); |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 180 | static void addrconf_rs_timer(struct timer_list *t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); |
| 182 | static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); |
| 183 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 184 | static void inet6_prefix_notify(int event, struct inet6_dev *idev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | struct prefix_info *pinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Adrian Bunk | 888c848 | 2008-07-22 14:21:58 -0700 | [diff] [blame] | 187 | static struct ipv6_devconf ipv6_devconf __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | .forwarding = 0, |
| 189 | .hop_limit = IPV6_DEFAULT_HOPLIMIT, |
| 190 | .mtu6 = IPV6_MIN_MTU, |
| 191 | .accept_ra = 1, |
| 192 | .accept_redirects = 1, |
| 193 | .autoconf = 1, |
| 194 | .force_mld_version = 0, |
Hannes Frederic Sowa | fc4eba5 | 2013-08-14 01:03:46 +0200 | [diff] [blame] | 195 | .mldv1_unsolicited_report_interval = 10 * HZ, |
| 196 | .mldv2_unsolicited_report_interval = HZ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | .dad_transmits = 1, |
| 198 | .rtr_solicits = MAX_RTR_SOLICITATIONS, |
| 199 | .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 200 | .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 202 | .use_tempaddr = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | .temp_valid_lft = TEMP_VALID_LIFETIME, |
| 204 | .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, |
| 205 | .regen_max_retry = REGEN_MAX_RETRY, |
| 206 | .max_desync_factor = MAX_DESYNC_FACTOR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | .max_addresses = IPV6_MAX_ADDRESSES, |
YOSHIFUJI Hideaki | 65f5c7c | 2006-03-20 16:55:08 -0800 | [diff] [blame] | 208 | .accept_ra_defrtr = 1, |
Ben Greear | d933319 | 2014-06-25 14:44:53 -0700 | [diff] [blame] | 209 | .accept_ra_from_local = 0, |
Hangbin Liu | 8013d1d7 | 2015-07-30 14:28:42 +0800 | [diff] [blame] | 210 | .accept_ra_min_hop_limit= 1, |
YOSHIFUJI Hideaki | c4fd30e | 2006-03-20 16:55:26 -0800 | [diff] [blame] | 211 | .accept_ra_pinfo = 1, |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 212 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 213 | .accept_ra_rtr_pref = 1, |
YOSHIFUJI Hideaki | 52e16356 | 2006-03-20 17:05:47 -0800 | [diff] [blame] | 214 | .rtr_probe_interval = 60 * HZ, |
YOSHIFUJI Hideaki | 09c884d | 2006-03-20 17:07:03 -0800 | [diff] [blame] | 215 | #ifdef CONFIG_IPV6_ROUTE_INFO |
Joel Scherpelz | bbea124 | 2017-03-22 18:19:04 +0900 | [diff] [blame] | 216 | .accept_ra_rt_info_min_plen = 0, |
YOSHIFUJI Hideaki | 09c884d | 2006-03-20 17:07:03 -0800 | [diff] [blame] | 217 | .accept_ra_rt_info_max_plen = 0, |
| 218 | #endif |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 219 | #endif |
YOSHIFUJI Hideaki | fbea49e | 2006-09-22 14:43:49 -0700 | [diff] [blame] | 220 | .proxy_ndp = 0, |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 221 | .accept_source_route = 0, /* we do not accept RH0 by default. */ |
YOSHIFUJI Hideaki | 778d80b | 2008-06-28 14:17:11 +0900 | [diff] [blame] | 222 | .disable_ipv6 = 0, |
Nicolas Dichtel | 0940095 | 2017-11-14 14:21:32 +0100 | [diff] [blame] | 223 | .accept_dad = 0, |
Hannes Frederic Sowa | b800c3b | 2013-08-27 01:36:51 +0200 | [diff] [blame] | 224 | .suppress_frag_ndisc = 1, |
Harout Hedeshian | c2943f1 | 2015-01-20 10:06:05 -0700 | [diff] [blame] | 225 | .accept_ra_mtu = 1, |
Hannes Frederic Sowa | 3d1bec9 | 2015-03-23 23:36:00 +0100 | [diff] [blame] | 226 | .stable_secret = { |
| 227 | .initialized = false, |
Erik Kline | 3985e8a | 2015-07-22 16:38:25 +0900 | [diff] [blame] | 228 | }, |
| 229 | .use_oif_addrs_only = 0, |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 230 | .ignore_routes_with_linkdown = 0, |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 231 | .keep_addr_on_down = 0, |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 232 | .seg6_enabled = 0, |
David Lebrun | bf355b8 | 2016-11-08 14:57:42 +0100 | [diff] [blame] | 233 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 234 | .seg6_require_hmac = 0, |
| 235 | #endif |
Erik Nordmark | adc176c | 2016-12-02 14:00:08 -0800 | [diff] [blame] | 236 | .enhanced_dad = 1, |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 237 | .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, |
David Forster | df789fe | 2017-02-23 16:27:18 +0000 | [diff] [blame] | 238 | .disable_policy = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
Brian Haley | ab32ea5 | 2006-09-22 14:15:41 -0700 | [diff] [blame] | 241 | static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | .forwarding = 0, |
| 243 | .hop_limit = IPV6_DEFAULT_HOPLIMIT, |
| 244 | .mtu6 = IPV6_MIN_MTU, |
| 245 | .accept_ra = 1, |
| 246 | .accept_redirects = 1, |
| 247 | .autoconf = 1, |
Hannes Frederic Sowa | fc4eba5 | 2013-08-14 01:03:46 +0200 | [diff] [blame] | 248 | .force_mld_version = 0, |
| 249 | .mldv1_unsolicited_report_interval = 10 * HZ, |
| 250 | .mldv2_unsolicited_report_interval = HZ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | .dad_transmits = 1, |
| 252 | .rtr_solicits = MAX_RTR_SOLICITATIONS, |
| 253 | .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 254 | .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | .use_tempaddr = 0, |
| 257 | .temp_valid_lft = TEMP_VALID_LIFETIME, |
| 258 | .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, |
| 259 | .regen_max_retry = REGEN_MAX_RETRY, |
| 260 | .max_desync_factor = MAX_DESYNC_FACTOR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | .max_addresses = IPV6_MAX_ADDRESSES, |
YOSHIFUJI Hideaki | 65f5c7c | 2006-03-20 16:55:08 -0800 | [diff] [blame] | 262 | .accept_ra_defrtr = 1, |
Ben Greear | d933319 | 2014-06-25 14:44:53 -0700 | [diff] [blame] | 263 | .accept_ra_from_local = 0, |
Hangbin Liu | 8013d1d7 | 2015-07-30 14:28:42 +0800 | [diff] [blame] | 264 | .accept_ra_min_hop_limit= 1, |
YOSHIFUJI Hideaki | c4fd30e | 2006-03-20 16:55:26 -0800 | [diff] [blame] | 265 | .accept_ra_pinfo = 1, |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 266 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 267 | .accept_ra_rtr_pref = 1, |
YOSHIFUJI Hideaki | 52e16356 | 2006-03-20 17:05:47 -0800 | [diff] [blame] | 268 | .rtr_probe_interval = 60 * HZ, |
YOSHIFUJI Hideaki | 09c884d | 2006-03-20 17:07:03 -0800 | [diff] [blame] | 269 | #ifdef CONFIG_IPV6_ROUTE_INFO |
Joel Scherpelz | bbea124 | 2017-03-22 18:19:04 +0900 | [diff] [blame] | 270 | .accept_ra_rt_info_min_plen = 0, |
YOSHIFUJI Hideaki | 09c884d | 2006-03-20 17:07:03 -0800 | [diff] [blame] | 271 | .accept_ra_rt_info_max_plen = 0, |
| 272 | #endif |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 273 | #endif |
YOSHIFUJI Hideaki | fbea49e | 2006-09-22 14:43:49 -0700 | [diff] [blame] | 274 | .proxy_ndp = 0, |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 275 | .accept_source_route = 0, /* we do not accept RH0 by default. */ |
YOSHIFUJI Hideaki | 778d80b | 2008-06-28 14:17:11 +0900 | [diff] [blame] | 276 | .disable_ipv6 = 0, |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 277 | .accept_dad = 1, |
Hannes Frederic Sowa | b800c3b | 2013-08-27 01:36:51 +0200 | [diff] [blame] | 278 | .suppress_frag_ndisc = 1, |
Harout Hedeshian | c2943f1 | 2015-01-20 10:06:05 -0700 | [diff] [blame] | 279 | .accept_ra_mtu = 1, |
Hannes Frederic Sowa | 3d1bec9 | 2015-03-23 23:36:00 +0100 | [diff] [blame] | 280 | .stable_secret = { |
| 281 | .initialized = false, |
| 282 | }, |
Erik Kline | 3985e8a | 2015-07-22 16:38:25 +0900 | [diff] [blame] | 283 | .use_oif_addrs_only = 0, |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 284 | .ignore_routes_with_linkdown = 0, |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 285 | .keep_addr_on_down = 0, |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 286 | .seg6_enabled = 0, |
David Lebrun | bf355b8 | 2016-11-08 14:57:42 +0100 | [diff] [blame] | 287 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 288 | .seg6_require_hmac = 0, |
| 289 | #endif |
Erik Nordmark | adc176c | 2016-12-02 14:00:08 -0800 | [diff] [blame] | 290 | .enhanced_dad = 1, |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 291 | .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64, |
David Forster | df789fe | 2017-02-23 16:27:18 +0000 | [diff] [blame] | 292 | .disable_policy = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
Mike Manning | 1f372c7 | 2017-09-25 22:01:36 +0100 | [diff] [blame] | 295 | /* Check if link is ready: is it up and is a valid qdisc available */ |
| 296 | static inline bool addrconf_link_ready(const struct net_device *dev) |
Mitsuru Chinen | f24e3d6 | 2007-10-10 02:53:43 -0700 | [diff] [blame] | 297 | { |
Mike Manning | 1f372c7 | 2017-09-25 22:01:36 +0100 | [diff] [blame] | 298 | return netif_oper_up(dev) && !qdisc_tx_is_noop(dev); |
Mitsuru Chinen | f24e3d6 | 2007-10-10 02:53:43 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 301 | static void addrconf_del_rs_timer(struct inet6_dev *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 303 | if (del_timer(&idev->rs_timer)) |
| 304 | __in6_dev_put(idev); |
| 305 | } |
| 306 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 307 | static void addrconf_del_dad_work(struct inet6_ifaddr *ifp) |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 308 | { |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 309 | if (cancel_delayed_work(&ifp->dad_work)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | __in6_ifa_put(ifp); |
| 311 | } |
| 312 | |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 313 | static void addrconf_mod_rs_timer(struct inet6_dev *idev, |
| 314 | unsigned long when) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 316 | if (!timer_pending(&idev->rs_timer)) |
| 317 | in6_dev_hold(idev); |
| 318 | mod_timer(&idev->rs_timer, jiffies + when); |
| 319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 321 | static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp, |
| 322 | unsigned long delay) |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 323 | { |
Xin Long | f8a894b | 2017-06-15 16:33:58 +0800 | [diff] [blame] | 324 | in6_ifa_hold(ifp); |
| 325 | if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay)) |
| 326 | in6_ifa_put(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 329 | static int snmp6_alloc_dev(struct inet6_dev *idev) |
| 330 | { |
John Stultz | 827da44 | 2013-10-07 15:51:58 -0700 | [diff] [blame] | 331 | int i; |
| 332 | |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 333 | idev->stats.ipv6 = alloc_percpu(struct ipstats_mib); |
| 334 | if (!idev->stats.ipv6) |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 335 | goto err_ip; |
John Stultz | 827da44 | 2013-10-07 15:51:58 -0700 | [diff] [blame] | 336 | |
| 337 | for_each_possible_cpu(i) { |
| 338 | struct ipstats_mib *addrconf_stats; |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 339 | addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i); |
John Stultz | 827da44 | 2013-10-07 15:51:58 -0700 | [diff] [blame] | 340 | u64_stats_init(&addrconf_stats->syncp); |
John Stultz | 827da44 | 2013-10-07 15:51:58 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 344 | idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), |
| 345 | GFP_KERNEL); |
| 346 | if (!idev->stats.icmpv6dev) |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 347 | goto err_icmp; |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 348 | idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), |
| 349 | GFP_KERNEL); |
| 350 | if (!idev->stats.icmpv6msgdev) |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 351 | goto err_icmpmsg; |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 352 | |
| 353 | return 0; |
| 354 | |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 355 | err_icmpmsg: |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 356 | kfree(idev->stats.icmpv6dev); |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 357 | err_icmp: |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 358 | free_percpu(idev->stats.ipv6); |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 359 | err_ip: |
Pavel Emelyanov | aaf70ec | 2007-10-17 21:25:32 -0700 | [diff] [blame] | 360 | return -ENOMEM; |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 363 | static struct inet6_dev *ipv6_add_dev(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
| 365 | struct inet6_dev *ndev; |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 366 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | ASSERT_RTNL(); |
| 369 | |
| 370 | if (dev->mtu < IPV6_MIN_MTU) |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 371 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 373 | ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 374 | if (!ndev) |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 375 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 377 | rwlock_init(&ndev->lock); |
| 378 | ndev->dev = dev; |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 379 | INIT_LIST_HEAD(&ndev->addr_list); |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 380 | timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 381 | memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); |
Hannes Frederic Sowa | 9b29c69 | 2015-12-15 22:59:12 +0100 | [diff] [blame] | 382 | |
| 383 | if (ndev->cnf.stable_secret.initialized) |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 384 | ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; |
Hannes Frederic Sowa | 9b29c69 | 2015-12-15 22:59:12 +0100 | [diff] [blame] | 385 | |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 386 | ndev->cnf.mtu6 = dev->mtu; |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 387 | ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 388 | if (!ndev->nd_parms) { |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 389 | kfree(ndev); |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 390 | return ERR_PTR(err); |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 391 | } |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 392 | if (ndev->cnf.forwarding) |
| 393 | dev_disable_lro(dev); |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 394 | /* We refer to the device */ |
| 395 | dev_hold(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 397 | if (snmp6_alloc_dev(ndev) < 0) { |
Joe Perches | e32ac25 | 2018-03-26 08:35:01 -0700 | [diff] [blame] | 398 | netdev_dbg(dev, "%s: cannot allocate memory for statistics\n", |
| 399 | __func__); |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 400 | neigh_parms_release(&nd_tbl, ndev->nd_parms); |
Roy Li | 8603e33 | 2011-09-20 15:10:16 -0400 | [diff] [blame] | 401 | dev_put(dev); |
| 402 | kfree(ndev); |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 403 | return ERR_PTR(err); |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 404 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 406 | if (snmp6_register_dev(ndev) < 0) { |
Joe Perches | e32ac25 | 2018-03-26 08:35:01 -0700 | [diff] [blame] | 407 | netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n", |
| 408 | __func__, dev->name); |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 409 | goto err_release; |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
Jiri Bohac | 9d6280d | 2016-10-13 18:50:02 +0200 | [diff] [blame] | 412 | /* One reference from device. */ |
Reshetova, Elena | 1be9246 | 2017-07-04 09:34:55 +0300 | [diff] [blame] | 413 | refcount_set(&ndev->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 415 | if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) |
| 416 | ndev->cnf.accept_dad = -1; |
| 417 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 418 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
YOSHIFUJI Hideaki | b077d7a | 2008-04-13 23:42:18 -0700 | [diff] [blame] | 419 | if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 420 | pr_info("%s: Disabled Multicast RS\n", dev->name); |
YOSHIFUJI Hideaki | b077d7a | 2008-04-13 23:42:18 -0700 | [diff] [blame] | 421 | ndev->cnf.rtr_solicits = 0; |
| 422 | } |
| 423 | #endif |
| 424 | |
stephen hemminger | 372e6c8 | 2010-03-17 20:31:09 +0000 | [diff] [blame] | 425 | INIT_LIST_HEAD(&ndev->tempaddr_list); |
Jiri Bohac | 76506a9 | 2016-10-13 18:52:15 +0200 | [diff] [blame] | 426 | ndev->desync_factor = U32_MAX; |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 427 | if ((dev->flags&IFF_LOOPBACK) || |
| 428 | dev->type == ARPHRD_TUNNEL || |
YOSHIFUJI Hideaki | 9625ed7 | 2008-04-13 23:47:11 -0700 | [diff] [blame] | 429 | dev->type == ARPHRD_TUNNEL6 || |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 430 | dev->type == ARPHRD_SIT || |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 431 | dev->type == ARPHRD_NONE) { |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 432 | ndev->cnf.use_tempaddr = -1; |
Jiri Bohac | 9d6280d | 2016-10-13 18:50:02 +0200 | [diff] [blame] | 433 | } else |
| 434 | ipv6_regen_rndid(ndev); |
David S. Miller | 5d9efa7 | 2013-10-28 20:07:50 -0400 | [diff] [blame] | 435 | |
Daniel Borkmann | 914faa1 | 2013-04-09 03:47:14 +0000 | [diff] [blame] | 436 | ndev->token = in6addr_any; |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 437 | |
Mike Manning | 1f372c7 | 2017-09-25 22:01:36 +0100 | [diff] [blame] | 438 | if (netif_running(dev) && addrconf_link_ready(dev)) |
Herbert Xu | 53aadcc | 2007-03-27 14:31:52 -0700 | [diff] [blame] | 439 | ndev->if_flags |= IF_READY; |
| 440 | |
Ingo Oeser | 322f74a | 2006-03-20 23:01:47 -0800 | [diff] [blame] | 441 | ipv6_mc_init_dev(ndev); |
| 442 | ndev->tstamp = jiffies; |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 443 | err = addrconf_sysctl_register(ndev); |
| 444 | if (err) { |
| 445 | ipv6_mc_destroy_dev(ndev); |
Sabrina Dubroca | 2a189f9e | 2015-11-04 14:47:53 +0100 | [diff] [blame] | 446 | snmp6_unregister_dev(ndev); |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 447 | goto err_release; |
| 448 | } |
David L Stevens | 30c4cf5 | 2007-01-04 12:31:14 -0800 | [diff] [blame] | 449 | /* protected by rtnl_lock */ |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 450 | rcu_assign_pointer(dev->ip6_ptr, ndev); |
YOSHIFUJI Hideaki | d88ae4c | 2007-01-14 21:48:40 -0800 | [diff] [blame] | 451 | |
Hannes Frederic Sowa | 2c5e893 | 2013-02-10 03:50:18 +0000 | [diff] [blame] | 452 | /* Join interface-local all-node multicast group */ |
| 453 | ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); |
| 454 | |
YOSHIFUJI Hideaki | d88ae4c | 2007-01-14 21:48:40 -0800 | [diff] [blame] | 455 | /* Join all-node multicast group */ |
YOSHIFUJI Hideaki | f3ee401 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 456 | ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); |
YOSHIFUJI Hideaki | d88ae4c | 2007-01-14 21:48:40 -0800 | [diff] [blame] | 457 | |
Li Wei | d6ddef9 | 2012-03-05 14:45:17 +0000 | [diff] [blame] | 458 | /* Join all-router multicast group if forwarding is set */ |
Li Wei | 8b2aaed | 2012-03-07 14:58:07 +0000 | [diff] [blame] | 459 | if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) |
Li Wei | d6ddef9 | 2012-03-05 14:45:17 +0000 | [diff] [blame] | 460 | ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); |
| 461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | return ndev; |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 463 | |
| 464 | err_release: |
| 465 | neigh_parms_release(&nd_tbl, ndev->nd_parms); |
| 466 | ndev->dead = 1; |
| 467 | in6_dev_finish_destroy(ndev); |
| 468 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 471 | static struct inet6_dev *ipv6_find_idev(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
| 473 | struct inet6_dev *idev; |
| 474 | |
| 475 | ASSERT_RTNL(); |
| 476 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 477 | idev = __in6_dev_get(dev); |
| 478 | if (!idev) { |
| 479 | idev = ipv6_add_dev(dev); |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 480 | if (IS_ERR(idev)) |
Sabrina Dubroca | db0b99f | 2019-08-23 15:44:36 +0200 | [diff] [blame^] | 481 | return idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | if (dev->flags&IFF_UP) |
| 485 | ipv6_mc_up(idev); |
| 486 | return idev; |
| 487 | } |
| 488 | |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 489 | static int inet6_netconf_msgsize_devconf(int type) |
| 490 | { |
| 491 | int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) |
| 492 | + nla_total_size(4); /* NETCONFA_IFINDEX */ |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 493 | bool all = false; |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 494 | |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 495 | if (type == NETCONFA_ALL) |
| 496 | all = true; |
| 497 | |
| 498 | if (all || type == NETCONFA_FORWARDING) |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 499 | size += nla_total_size(4); |
David S. Miller | b1afce9 | 2012-12-04 14:46:34 -0500 | [diff] [blame] | 500 | #ifdef CONFIG_IPV6_MROUTE |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 501 | if (all || type == NETCONFA_MC_FORWARDING) |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 502 | size += nla_total_size(4); |
David S. Miller | b1afce9 | 2012-12-04 14:46:34 -0500 | [diff] [blame] | 503 | #endif |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 504 | if (all || type == NETCONFA_PROXY_NEIGH) |
stephen hemminger | c92d549 | 2013-12-17 22:37:14 -0800 | [diff] [blame] | 505 | size += nla_total_size(4); |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 506 | |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 507 | if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 508 | size += nla_total_size(4); |
| 509 | |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 510 | return size; |
| 511 | } |
| 512 | |
| 513 | static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, |
| 514 | struct ipv6_devconf *devconf, u32 portid, |
| 515 | u32 seq, int event, unsigned int flags, |
| 516 | int type) |
| 517 | { |
| 518 | struct nlmsghdr *nlh; |
| 519 | struct netconfmsg *ncm; |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 520 | bool all = false; |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 521 | |
| 522 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), |
| 523 | flags); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 524 | if (!nlh) |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 525 | return -EMSGSIZE; |
| 526 | |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 527 | if (type == NETCONFA_ALL) |
| 528 | all = true; |
| 529 | |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 530 | ncm = nlmsg_data(nlh); |
| 531 | ncm->ncm_family = AF_INET6; |
| 532 | |
| 533 | if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) |
| 534 | goto nla_put_failure; |
| 535 | |
David Ahern | 2345217 | 2017-03-28 14:28:05 -0700 | [diff] [blame] | 536 | if (!devconf) |
| 537 | goto out; |
| 538 | |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 539 | if ((all || type == NETCONFA_FORWARDING) && |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 540 | nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) |
| 541 | goto nla_put_failure; |
David S. Miller | b1afce9 | 2012-12-04 14:46:34 -0500 | [diff] [blame] | 542 | #ifdef CONFIG_IPV6_MROUTE |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 543 | if ((all || type == NETCONFA_MC_FORWARDING) && |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 544 | nla_put_s32(skb, NETCONFA_MC_FORWARDING, |
| 545 | devconf->mc_forwarding) < 0) |
| 546 | goto nla_put_failure; |
David S. Miller | b1afce9 | 2012-12-04 14:46:34 -0500 | [diff] [blame] | 547 | #endif |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 548 | if ((all || type == NETCONFA_PROXY_NEIGH) && |
stephen hemminger | c92d549 | 2013-12-17 22:37:14 -0800 | [diff] [blame] | 549 | nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0) |
| 550 | goto nla_put_failure; |
| 551 | |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 552 | if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 553 | nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, |
| 554 | devconf->ignore_routes_with_linkdown) < 0) |
| 555 | goto nla_put_failure; |
| 556 | |
David Ahern | 2345217 | 2017-03-28 14:28:05 -0700 | [diff] [blame] | 557 | out: |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 558 | nlmsg_end(skb, nlh); |
| 559 | return 0; |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 560 | |
| 561 | nla_put_failure: |
| 562 | nlmsg_cancel(skb, nlh); |
| 563 | return -EMSGSIZE; |
| 564 | } |
| 565 | |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 566 | void inet6_netconf_notify_devconf(struct net *net, int event, int type, |
| 567 | int ifindex, struct ipv6_devconf *devconf) |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 568 | { |
| 569 | struct sk_buff *skb; |
| 570 | int err = -ENOBUFS; |
| 571 | |
Eric Dumazet | 927265b | 2016-07-08 05:46:04 +0200 | [diff] [blame] | 572 | skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 573 | if (!skb) |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 574 | goto errout; |
| 575 | |
| 576 | err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 577 | event, 0, type); |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 578 | if (err < 0) { |
| 579 | /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ |
| 580 | WARN_ON(err == -EMSGSIZE); |
| 581 | kfree_skb(skb); |
| 582 | goto errout; |
| 583 | } |
Eric Dumazet | 927265b | 2016-07-08 05:46:04 +0200 | [diff] [blame] | 584 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL); |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 585 | return; |
| 586 | errout: |
Cong Ding | bd77902 | 2012-12-18 12:08:56 +0000 | [diff] [blame] | 587 | rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 590 | static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { |
| 591 | [NETCONFA_IFINDEX] = { .len = sizeof(int) }, |
| 592 | [NETCONFA_FORWARDING] = { .len = sizeof(int) }, |
stephen hemminger | c92d549 | 2013-12-17 22:37:14 -0800 | [diff] [blame] | 593 | [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 594 | [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 595 | }; |
| 596 | |
Jakub Kicinski | 38d5181 | 2019-01-18 10:46:22 -0800 | [diff] [blame] | 597 | static int inet6_netconf_valid_get_req(struct sk_buff *skb, |
| 598 | const struct nlmsghdr *nlh, |
| 599 | struct nlattr **tb, |
| 600 | struct netlink_ext_ack *extack) |
| 601 | { |
| 602 | int i, err; |
| 603 | |
| 604 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) { |
| 605 | NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request"); |
| 606 | return -EINVAL; |
| 607 | } |
| 608 | |
| 609 | if (!netlink_strict_get_check(skb)) |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 610 | return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg), |
| 611 | tb, NETCONFA_MAX, |
| 612 | devconf_ipv6_policy, extack); |
Jakub Kicinski | 38d5181 | 2019-01-18 10:46:22 -0800 | [diff] [blame] | 613 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 614 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg), |
| 615 | tb, NETCONFA_MAX, |
| 616 | devconf_ipv6_policy, extack); |
Jakub Kicinski | 38d5181 | 2019-01-18 10:46:22 -0800 | [diff] [blame] | 617 | if (err) |
| 618 | return err; |
| 619 | |
| 620 | for (i = 0; i <= NETCONFA_MAX; i++) { |
| 621 | if (!tb[i]) |
| 622 | continue; |
| 623 | |
| 624 | switch (i) { |
| 625 | case NETCONFA_IFINDEX: |
| 626 | break; |
| 627 | default: |
| 628 | NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request"); |
| 629 | return -EINVAL; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | return 0; |
| 634 | } |
| 635 | |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 636 | static int inet6_netconf_get_devconf(struct sk_buff *in_skb, |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 637 | struct nlmsghdr *nlh, |
| 638 | struct netlink_ext_ack *extack) |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 639 | { |
| 640 | struct net *net = sock_net(in_skb->sk); |
| 641 | struct nlattr *tb[NETCONFA_MAX+1]; |
Florian Westphal | 4ea2607 | 2017-10-11 10:28:00 +0200 | [diff] [blame] | 642 | struct inet6_dev *in6_dev = NULL; |
| 643 | struct net_device *dev = NULL; |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 644 | struct sk_buff *skb; |
| 645 | struct ipv6_devconf *devconf; |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 646 | int ifindex; |
| 647 | int err; |
| 648 | |
Jakub Kicinski | 38d5181 | 2019-01-18 10:46:22 -0800 | [diff] [blame] | 649 | err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack); |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 650 | if (err < 0) |
Florian Westphal | 4ea2607 | 2017-10-11 10:28:00 +0200 | [diff] [blame] | 651 | return err; |
| 652 | |
| 653 | if (!tb[NETCONFA_IFINDEX]) |
| 654 | return -EINVAL; |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 655 | |
Anton Protopopov | a97eb33 | 2016-02-16 21:43:16 -0500 | [diff] [blame] | 656 | err = -EINVAL; |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 657 | ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); |
| 658 | switch (ifindex) { |
| 659 | case NETCONFA_IFINDEX_ALL: |
| 660 | devconf = net->ipv6.devconf_all; |
| 661 | break; |
| 662 | case NETCONFA_IFINDEX_DEFAULT: |
| 663 | devconf = net->ipv6.devconf_dflt; |
| 664 | break; |
| 665 | default: |
Florian Westphal | 4ea2607 | 2017-10-11 10:28:00 +0200 | [diff] [blame] | 666 | dev = dev_get_by_index(net, ifindex); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 667 | if (!dev) |
Florian Westphal | 4ea2607 | 2017-10-11 10:28:00 +0200 | [diff] [blame] | 668 | return -EINVAL; |
| 669 | in6_dev = in6_dev_get(dev); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 670 | if (!in6_dev) |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 671 | goto errout; |
| 672 | devconf = &in6_dev->cnf; |
| 673 | break; |
| 674 | } |
| 675 | |
| 676 | err = -ENOBUFS; |
Florian Westphal | 4ea2607 | 2017-10-11 10:28:00 +0200 | [diff] [blame] | 677 | skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 678 | if (!skb) |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 679 | goto errout; |
| 680 | |
| 681 | err = inet6_netconf_fill_devconf(skb, ifindex, devconf, |
| 682 | NETLINK_CB(in_skb).portid, |
| 683 | nlh->nlmsg_seq, RTM_NEWNETCONF, 0, |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 684 | NETCONFA_ALL); |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 685 | if (err < 0) { |
| 686 | /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ |
| 687 | WARN_ON(err == -EMSGSIZE); |
| 688 | kfree_skb(skb); |
| 689 | goto errout; |
| 690 | } |
| 691 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
| 692 | errout: |
Florian Westphal | 4ea2607 | 2017-10-11 10:28:00 +0200 | [diff] [blame] | 693 | if (in6_dev) |
| 694 | in6_dev_put(in6_dev); |
| 695 | if (dev) |
| 696 | dev_put(dev); |
Nicolas Dichtel | 76f8f6c | 2012-10-25 22:28:51 +0000 | [diff] [blame] | 697 | return err; |
| 698 | } |
| 699 | |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 700 | static int inet6_netconf_dump_devconf(struct sk_buff *skb, |
| 701 | struct netlink_callback *cb) |
| 702 | { |
David Ahern | addd383 | 2018-10-07 20:16:41 -0700 | [diff] [blame] | 703 | const struct nlmsghdr *nlh = cb->nlh; |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 704 | struct net *net = sock_net(skb->sk); |
| 705 | int h, s_h; |
| 706 | int idx, s_idx; |
| 707 | struct net_device *dev; |
| 708 | struct inet6_dev *idev; |
| 709 | struct hlist_head *head; |
| 710 | |
David Ahern | addd383 | 2018-10-07 20:16:41 -0700 | [diff] [blame] | 711 | if (cb->strict_check) { |
| 712 | struct netlink_ext_ack *extack = cb->extack; |
| 713 | struct netconfmsg *ncm; |
| 714 | |
| 715 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) { |
| 716 | NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request"); |
| 717 | return -EINVAL; |
| 718 | } |
| 719 | |
| 720 | if (nlmsg_attrlen(nlh, sizeof(*ncm))) { |
| 721 | NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request"); |
| 722 | return -EINVAL; |
| 723 | } |
| 724 | } |
| 725 | |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 726 | s_h = cb->args[0]; |
| 727 | s_idx = idx = cb->args[1]; |
| 728 | |
| 729 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 730 | idx = 0; |
| 731 | head = &net->dev_index_head[h]; |
| 732 | rcu_read_lock(); |
Nicolas Dichtel | 63998ac | 2013-03-22 06:28:43 +0000 | [diff] [blame] | 733 | cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ |
| 734 | net->dev_base_seq; |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 735 | hlist_for_each_entry_rcu(dev, head, index_hlist) { |
| 736 | if (idx < s_idx) |
| 737 | goto cont; |
| 738 | idev = __in6_dev_get(dev); |
| 739 | if (!idev) |
| 740 | goto cont; |
| 741 | |
| 742 | if (inet6_netconf_fill_devconf(skb, dev->ifindex, |
| 743 | &idev->cnf, |
| 744 | NETLINK_CB(cb->skb).portid, |
David Ahern | addd383 | 2018-10-07 20:16:41 -0700 | [diff] [blame] | 745 | nlh->nlmsg_seq, |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 746 | RTM_NEWNETCONF, |
| 747 | NLM_F_MULTI, |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 748 | NETCONFA_ALL) < 0) { |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 749 | rcu_read_unlock(); |
| 750 | goto done; |
| 751 | } |
Nicolas Dichtel | 63998ac | 2013-03-22 06:28:43 +0000 | [diff] [blame] | 752 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 753 | cont: |
| 754 | idx++; |
| 755 | } |
| 756 | rcu_read_unlock(); |
| 757 | } |
| 758 | if (h == NETDEV_HASHENTRIES) { |
| 759 | if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, |
| 760 | net->ipv6.devconf_all, |
| 761 | NETLINK_CB(cb->skb).portid, |
David Ahern | addd383 | 2018-10-07 20:16:41 -0700 | [diff] [blame] | 762 | nlh->nlmsg_seq, |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 763 | RTM_NEWNETCONF, NLM_F_MULTI, |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 764 | NETCONFA_ALL) < 0) |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 765 | goto done; |
| 766 | else |
| 767 | h++; |
| 768 | } |
| 769 | if (h == NETDEV_HASHENTRIES + 1) { |
| 770 | if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, |
| 771 | net->ipv6.devconf_dflt, |
| 772 | NETLINK_CB(cb->skb).portid, |
David Ahern | addd383 | 2018-10-07 20:16:41 -0700 | [diff] [blame] | 773 | nlh->nlmsg_seq, |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 774 | RTM_NEWNETCONF, NLM_F_MULTI, |
Zhang Shengju | 136ba62 | 2016-03-10 08:55:50 +0000 | [diff] [blame] | 775 | NETCONFA_ALL) < 0) |
Nicolas Dichtel | 7a67420 | 2013-03-05 23:42:06 +0000 | [diff] [blame] | 776 | goto done; |
| 777 | else |
| 778 | h++; |
| 779 | } |
| 780 | done: |
| 781 | cb->args[0] = h; |
| 782 | cb->args[1] = idx; |
| 783 | |
| 784 | return skb->len; |
| 785 | } |
| 786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | #ifdef CONFIG_SYSCTL |
| 788 | static void dev_forward_change(struct inet6_dev *idev) |
| 789 | { |
| 790 | struct net_device *dev; |
| 791 | struct inet6_ifaddr *ifa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
| 793 | if (!idev) |
| 794 | return; |
| 795 | dev = idev->dev; |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 796 | if (idev->cnf.forwarding) |
| 797 | dev_disable_lro(dev); |
Amerigo Wang | 1a94083 | 2012-10-28 17:43:53 +0000 | [diff] [blame] | 798 | if (dev->flags & IFF_MULTICAST) { |
Hannes Frederic Sowa | 2c5e893 | 2013-02-10 03:50:18 +0000 | [diff] [blame] | 799 | if (idev->cnf.forwarding) { |
YOSHIFUJI Hideaki | f3ee401 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 800 | ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); |
Hannes Frederic Sowa | 2c5e893 | 2013-02-10 03:50:18 +0000 | [diff] [blame] | 801 | ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); |
| 802 | ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); |
| 803 | } else { |
YOSHIFUJI Hideaki | f3ee401 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 804 | ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); |
Hannes Frederic Sowa | 2c5e893 | 2013-02-10 03:50:18 +0000 | [diff] [blame] | 805 | ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); |
| 806 | ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); |
| 807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 809 | |
| 810 | list_for_each_entry(ifa, &idev->addr_list, if_list) { |
Michal Wrobel | 2c12a74 | 2007-02-26 15:36:10 -0800 | [diff] [blame] | 811 | if (ifa->flags&IFA_F_TENTATIVE) |
| 812 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | if (idev->cnf.forwarding) |
| 814 | addrconf_join_anycast(ifa); |
| 815 | else |
| 816 | addrconf_leave_anycast(ifa); |
| 817 | } |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 818 | inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, |
| 819 | NETCONFA_FORWARDING, |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 820 | dev->ifindex, &idev->cnf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | |
Pavel Emelyanov | e186932 | 2008-01-10 17:43:50 -0800 | [diff] [blame] | 824 | static void addrconf_forward_change(struct net *net, __s32 newf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | { |
| 826 | struct net_device *dev; |
| 827 | struct inet6_dev *idev; |
| 828 | |
Ben Hutchings | 4acd494 | 2012-08-14 08:54:51 +0000 | [diff] [blame] | 829 | for_each_netdev(net, dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | idev = __in6_dev_get(dev); |
| 831 | if (idev) { |
Pavel Emelyanov | e186932 | 2008-01-10 17:43:50 -0800 | [diff] [blame] | 832 | int changed = (!idev->cnf.forwarding) ^ (!newf); |
| 833 | idev->cnf.forwarding = newf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | if (changed) |
| 835 | dev_forward_change(idev); |
| 836 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 839 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 840 | static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 841 | { |
Pavel Emelyanov | bff16c2 | 2008-01-10 17:42:13 -0800 | [diff] [blame] | 842 | struct net *net; |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 843 | int old; |
| 844 | |
| 845 | if (!rtnl_trylock()) |
| 846 | return restart_syscall(); |
Pavel Emelyanov | bff16c2 | 2008-01-10 17:42:13 -0800 | [diff] [blame] | 847 | |
| 848 | net = (struct net *)table->extra2; |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 849 | old = *p; |
| 850 | *p = newf; |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 851 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 852 | if (p == &net->ipv6.devconf_dflt->forwarding) { |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 853 | if ((!newf) ^ (!old)) |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 854 | inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 855 | NETCONFA_FORWARDING, |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 856 | NETCONFA_IFINDEX_DEFAULT, |
| 857 | net->ipv6.devconf_dflt); |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 858 | rtnl_unlock(); |
| 859 | return 0; |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 860 | } |
Stephen Hemminger | b325fdd | 2009-02-26 06:55:31 +0000 | [diff] [blame] | 861 | |
Pavel Emelyanov | e186932 | 2008-01-10 17:43:50 -0800 | [diff] [blame] | 862 | if (p == &net->ipv6.devconf_all->forwarding) { |
Nicolas Dichtel | d26c638 | 2016-08-30 10:09:21 +0200 | [diff] [blame] | 863 | int old_dflt = net->ipv6.devconf_dflt->forwarding; |
| 864 | |
Pavel Emelyanov | e186932 | 2008-01-10 17:43:50 -0800 | [diff] [blame] | 865 | net->ipv6.devconf_dflt->forwarding = newf; |
Nicolas Dichtel | d26c638 | 2016-08-30 10:09:21 +0200 | [diff] [blame] | 866 | if ((!newf) ^ (!old_dflt)) |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 867 | inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 868 | NETCONFA_FORWARDING, |
Nicolas Dichtel | d26c638 | 2016-08-30 10:09:21 +0200 | [diff] [blame] | 869 | NETCONFA_IFINDEX_DEFAULT, |
| 870 | net->ipv6.devconf_dflt); |
| 871 | |
Pavel Emelyanov | e186932 | 2008-01-10 17:43:50 -0800 | [diff] [blame] | 872 | addrconf_forward_change(net, newf); |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 873 | if ((!newf) ^ (!old)) |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 874 | inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 875 | NETCONFA_FORWARDING, |
Nicolas Dichtel | f3a1bfb | 2012-10-25 22:28:50 +0000 | [diff] [blame] | 876 | NETCONFA_IFINDEX_ALL, |
| 877 | net->ipv6.devconf_all); |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 878 | } else if ((!newf) ^ (!old)) |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 879 | dev_forward_change((struct inet6_dev *)table->extra1); |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 880 | rtnl_unlock(); |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 881 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 882 | if (newf) |
Daniel Lezcano | 7b4da53 | 2008-03-04 13:47:14 -0800 | [diff] [blame] | 883 | rt6_purge_dflt_routers(net); |
Stephen Hemminger | b325fdd | 2009-02-26 06:55:31 +0000 | [diff] [blame] | 884 | return 1; |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 885 | } |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 886 | |
| 887 | static void addrconf_linkdown_change(struct net *net, __s32 newf) |
| 888 | { |
| 889 | struct net_device *dev; |
| 890 | struct inet6_dev *idev; |
| 891 | |
| 892 | for_each_netdev(net, dev) { |
| 893 | idev = __in6_dev_get(dev); |
| 894 | if (idev) { |
| 895 | int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf); |
| 896 | |
| 897 | idev->cnf.ignore_routes_with_linkdown = newf; |
| 898 | if (changed) |
| 899 | inet6_netconf_notify_devconf(dev_net(dev), |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 900 | RTM_NEWNETCONF, |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 901 | NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, |
| 902 | dev->ifindex, |
| 903 | &idev->cnf); |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf) |
| 909 | { |
| 910 | struct net *net; |
| 911 | int old; |
| 912 | |
| 913 | if (!rtnl_trylock()) |
| 914 | return restart_syscall(); |
| 915 | |
| 916 | net = (struct net *)table->extra2; |
| 917 | old = *p; |
| 918 | *p = newf; |
| 919 | |
| 920 | if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) { |
| 921 | if ((!newf) ^ (!old)) |
| 922 | inet6_netconf_notify_devconf(net, |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 923 | RTM_NEWNETCONF, |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 924 | NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, |
| 925 | NETCONFA_IFINDEX_DEFAULT, |
| 926 | net->ipv6.devconf_dflt); |
| 927 | rtnl_unlock(); |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) { |
| 932 | net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf; |
| 933 | addrconf_linkdown_change(net, newf); |
| 934 | if ((!newf) ^ (!old)) |
| 935 | inet6_netconf_notify_devconf(net, |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 936 | RTM_NEWNETCONF, |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 937 | NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, |
| 938 | NETCONFA_IFINDEX_ALL, |
| 939 | net->ipv6.devconf_all); |
| 940 | } |
| 941 | rtnl_unlock(); |
| 942 | |
| 943 | return 1; |
| 944 | } |
| 945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | #endif |
| 947 | |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 948 | /* Nobody refers to this ifaddr, destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) |
| 950 | { |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 951 | WARN_ON(!hlist_unhashed(&ifp->addr_lst)); |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | #ifdef NET_REFCNT_DEBUG |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 954 | pr_debug("%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | #endif |
| 956 | |
| 957 | in6_dev_put(ifp->idev); |
| 958 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 959 | if (cancel_delayed_work(&ifp->dad_work)) |
| 960 | pr_notice("delayed DAD work was pending while freeing ifa=%p\n", |
| 961 | ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | |
Herbert Xu | e9d3e08 | 2010-05-18 15:36:06 -0700 | [diff] [blame] | 963 | if (ifp->state != INET6_IFADDR_STATE_DEAD) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 964 | pr_warn("Freeing alive inet6 address %p\n", ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | return; |
| 966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
Lai Jiangshan | e578598 | 2011-03-15 18:00:14 +0800 | [diff] [blame] | 968 | kfree_rcu(ifp, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Brian Haley | e55ffac | 2006-07-10 15:25:51 -0700 | [diff] [blame] | 971 | static void |
| 972 | ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) |
| 973 | { |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 974 | struct list_head *p; |
YOSHIFUJI Hideaki | 8a6ce0c | 2006-07-11 13:05:30 -0700 | [diff] [blame] | 975 | int ifp_scope = ipv6_addr_src_scope(&ifp->addr); |
Brian Haley | e55ffac | 2006-07-10 15:25:51 -0700 | [diff] [blame] | 976 | |
| 977 | /* |
| 978 | * Each device address list is sorted in order of scope - |
| 979 | * global before linklocal. |
| 980 | */ |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 981 | list_for_each(p, &idev->addr_list) { |
| 982 | struct inet6_ifaddr *ifa |
| 983 | = list_entry(p, struct inet6_ifaddr, if_list); |
YOSHIFUJI Hideaki | 8a6ce0c | 2006-07-11 13:05:30 -0700 | [diff] [blame] | 984 | if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) |
Brian Haley | e55ffac | 2006-07-10 15:25:51 -0700 | [diff] [blame] | 985 | break; |
| 986 | } |
| 987 | |
Eric Dumazet | 8ef802a | 2017-10-07 19:30:23 -0700 | [diff] [blame] | 988 | list_add_tail_rcu(&ifp->if_list, p); |
Brian Haley | e55ffac | 2006-07-10 15:25:51 -0700 | [diff] [blame] | 989 | } |
| 990 | |
Eric Dumazet | 3f27fb2 | 2017-10-23 16:17:47 -0700 | [diff] [blame] | 991 | static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr) |
YOSHIFUJI Hideaki | 3eb84f4 | 2008-04-10 15:42:08 +0900 | [diff] [blame] | 992 | { |
Eric Dumazet | 3f27fb2 | 2017-10-23 16:17:47 -0700 | [diff] [blame] | 993 | u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net); |
| 994 | |
| 995 | return hash_32(val, IN6_ADDR_HSIZE_SHIFT); |
YOSHIFUJI Hideaki | 3eb84f4 | 2008-04-10 15:42:08 +0900 | [diff] [blame] | 996 | } |
| 997 | |
Eric Dumazet | 56fc709 | 2017-10-23 16:17:45 -0700 | [diff] [blame] | 998 | static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, |
Eric Dumazet | 752a929 | 2017-10-23 16:17:46 -0700 | [diff] [blame] | 999 | struct net_device *dev, unsigned int hash) |
Eric Dumazet | 56fc709 | 2017-10-23 16:17:45 -0700 | [diff] [blame] | 1000 | { |
Eric Dumazet | 56fc709 | 2017-10-23 16:17:45 -0700 | [diff] [blame] | 1001 | struct inet6_ifaddr *ifp; |
| 1002 | |
| 1003 | hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { |
| 1004 | if (!net_eq(dev_net(ifp->idev->dev), net)) |
| 1005 | continue; |
| 1006 | if (ipv6_addr_equal(&ifp->addr, addr)) { |
| 1007 | if (!dev || ifp->idev->dev == dev) |
| 1008 | return true; |
| 1009 | } |
| 1010 | } |
| 1011 | return false; |
| 1012 | } |
| 1013 | |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1014 | static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa) |
| 1015 | { |
Eric Dumazet | 3f27fb2 | 2017-10-23 16:17:47 -0700 | [diff] [blame] | 1016 | unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr); |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1017 | int err = 0; |
| 1018 | |
| 1019 | spin_lock(&addrconf_hash_lock); |
| 1020 | |
| 1021 | /* Ignore adding duplicate addresses on an interface */ |
Eric Dumazet | 752a929 | 2017-10-23 16:17:46 -0700 | [diff] [blame] | 1022 | if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) { |
Joe Perches | e32ac25 | 2018-03-26 08:35:01 -0700 | [diff] [blame] | 1023 | netdev_dbg(dev, "ipv6_add_addr: already assigned\n"); |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1024 | err = -EEXIST; |
Eric Dumazet | 752a929 | 2017-10-23 16:17:46 -0700 | [diff] [blame] | 1025 | } else { |
| 1026 | hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1029 | spin_unlock(&addrconf_hash_lock); |
| 1030 | |
| 1031 | return err; |
| 1032 | } |
| 1033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | /* On success it returns ifp with increased reference count */ |
| 1035 | |
| 1036 | static struct inet6_ifaddr * |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1037 | ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg, |
David Ahern | de95e04 | 2017-10-18 09:56:54 -0700 | [diff] [blame] | 1038 | bool can_block, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | { |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1040 | gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC; |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1041 | int addr_type = ipv6_addr_type(cfg->pfx); |
David Forster | df789fe | 2017-02-23 16:27:18 +0000 | [diff] [blame] | 1042 | struct net *net = dev_net(idev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | struct inet6_ifaddr *ifa = NULL; |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 1044 | struct fib6_info *f6i = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | int err = 0; |
YOSHIFUJI Hideaki | d68b827 | 2008-06-25 16:26:47 +0900 | [diff] [blame] | 1046 | |
| 1047 | if (addr_type == IPV6_ADDR_ANY || |
Hangbin Liu | f17f764 | 2019-08-20 10:19:47 +0800 | [diff] [blame] | 1048 | (addr_type & IPV6_ADDR_MULTICAST && |
| 1049 | !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) || |
YOSHIFUJI Hideaki | d68b827 | 2008-06-25 16:26:47 +0900 | [diff] [blame] | 1050 | (!(idev->dev->flags & IFF_LOOPBACK) && |
Robert Shearman | 3ede0bb | 2018-09-19 13:56:53 +0100 | [diff] [blame] | 1051 | !netif_is_l3_master(idev->dev) && |
YOSHIFUJI Hideaki | d68b827 | 2008-06-25 16:26:47 +0900 | [diff] [blame] | 1052 | addr_type & IPV6_ADDR_LOOPBACK)) |
| 1053 | return ERR_PTR(-EADDRNOTAVAIL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | if (idev->dead) { |
| 1056 | err = -ENODEV; /*XXX*/ |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1057 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 1060 | if (idev->cnf.disable_ipv6) { |
Brian Haley | 9bdd8d4 | 2009-03-18 18:22:48 -0700 | [diff] [blame] | 1061 | err = -EACCES; |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1062 | goto out; |
Brian Haley | 9bdd8d4 | 2009-03-18 18:22:48 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
David Ahern | ff7883e | 2017-10-18 09:56:53 -0700 | [diff] [blame] | 1065 | /* validator notifier needs to be blocking; |
| 1066 | * do not call in atomic context |
| 1067 | */ |
| 1068 | if (can_block) { |
| 1069 | struct in6_validator_info i6vi = { |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1070 | .i6vi_addr = *cfg->pfx, |
David Ahern | ff7883e | 2017-10-18 09:56:53 -0700 | [diff] [blame] | 1071 | .i6vi_dev = idev, |
David Ahern | de95e04 | 2017-10-18 09:56:54 -0700 | [diff] [blame] | 1072 | .extack = extack, |
David Ahern | ff7883e | 2017-10-18 09:56:53 -0700 | [diff] [blame] | 1073 | }; |
| 1074 | |
| 1075 | err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi); |
| 1076 | err = notifier_to_errno(err); |
| 1077 | if (err < 0) |
| 1078 | goto out; |
| 1079 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1081 | ifa = kzalloc(sizeof(*ifa), gfp_flags); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 1082 | if (!ifa) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | err = -ENOBUFS; |
| 1084 | goto out; |
| 1085 | } |
| 1086 | |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1087 | f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags); |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 1088 | if (IS_ERR(f6i)) { |
| 1089 | err = PTR_ERR(f6i); |
| 1090 | f6i = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | goto out; |
| 1092 | } |
| 1093 | |
David Forster | df789fe | 2017-02-23 16:27:18 +0000 | [diff] [blame] | 1094 | if (net->ipv6.devconf_all->disable_policy || |
| 1095 | idev->cnf.disable_policy) |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 1096 | f6i->dst_nopolicy = true; |
David Forster | df789fe | 2017-02-23 16:27:18 +0000 | [diff] [blame] | 1097 | |
Jiri Pirko | bba2489 | 2013-12-07 19:26:57 +0100 | [diff] [blame] | 1098 | neigh_parms_data_state_setall(idev->nd_parms); |
| 1099 | |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1100 | ifa->addr = *cfg->pfx; |
| 1101 | if (cfg->peer_pfx) |
| 1102 | ifa->peer_addr = *cfg->peer_pfx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | |
| 1104 | spin_lock_init(&ifa->lock); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 1105 | INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 1106 | INIT_HLIST_NODE(&ifa->addr_lst); |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1107 | ifa->scope = cfg->scope; |
| 1108 | ifa->prefix_len = cfg->plen; |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 1109 | ifa->rt_priority = cfg->rt_priority; |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1110 | ifa->flags = cfg->ifa_flags; |
Mahesh Bandewar | 66eb9f8 | 2017-05-12 17:03:39 -0700 | [diff] [blame] | 1111 | /* No need to add the TENTATIVE flag for addresses with NODAD */ |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1112 | if (!(cfg->ifa_flags & IFA_F_NODAD)) |
Mahesh Bandewar | 66eb9f8 | 2017-05-12 17:03:39 -0700 | [diff] [blame] | 1113 | ifa->flags |= IFA_F_TENTATIVE; |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1114 | ifa->valid_lft = cfg->valid_lft; |
| 1115 | ifa->prefered_lft = cfg->preferred_lft; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | ifa->cstamp = ifa->tstamp = jiffies; |
Daniel Borkmann | 617fe29 | 2013-04-09 03:47:16 +0000 | [diff] [blame] | 1117 | ifa->tokenized = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 1119 | ifa->rt = f6i; |
Keir Fraser | 57f5f54 | 2006-08-29 02:43:49 -0700 | [diff] [blame] | 1120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | ifa->idev = idev; |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1122 | in6_dev_hold(idev); |
| 1123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | /* For caller */ |
Reshetova, Elena | 271201c | 2017-07-04 09:34:56 +0300 | [diff] [blame] | 1125 | refcount_set(&ifa->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1127 | rcu_read_lock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1129 | err = ipv6_add_addr_hash(idev->dev, ifa); |
| 1130 | if (err < 0) { |
| 1131 | rcu_read_unlock_bh(); |
| 1132 | goto out; |
| 1133 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | |
| 1135 | write_lock(&idev->lock); |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | /* Add to inet6_dev unicast addr list. */ |
Brian Haley | e55ffac | 2006-07-10 15:25:51 -0700 | [diff] [blame] | 1138 | ipv6_link_dev_addr(idev, ifa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | if (ifa->flags&IFA_F_TEMPORARY) { |
stephen hemminger | 372e6c8 | 2010-03-17 20:31:09 +0000 | [diff] [blame] | 1141 | list_add(&ifa->tmp_list, &idev->tempaddr_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | in6_ifa_hold(ifa); |
| 1143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | in6_ifa_hold(ifa); |
| 1146 | write_unlock(&idev->lock); |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1147 | |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 1148 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1150 | inet6addr_notifier_call_chain(NETDEV_UP, ifa); |
| 1151 | out: |
| 1152 | if (unlikely(err < 0)) { |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 1153 | fib6_info_release(f6i); |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 1154 | |
David Ahern | f3d9832 | 2017-10-18 09:56:52 -0700 | [diff] [blame] | 1155 | if (ifa) { |
| 1156 | if (ifa->idev) |
| 1157 | in6_dev_put(ifa->idev); |
| 1158 | kfree(ifa); |
| 1159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | ifa = ERR_PTR(err); |
| 1161 | } |
| 1162 | |
| 1163 | return ifa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 1166 | enum cleanup_prefix_rt_t { |
| 1167 | CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */ |
| 1168 | CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */ |
| 1169 | CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */ |
| 1170 | }; |
| 1171 | |
| 1172 | /* |
| 1173 | * Check, whether the prefix for ifp would still need a prefix route |
| 1174 | * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_* |
| 1175 | * constants. |
| 1176 | * |
| 1177 | * 1) we don't purge prefix if address was not permanent. |
| 1178 | * prefix is managed by its own lifetime. |
| 1179 | * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE. |
| 1180 | * 3) if there are no addresses, delete prefix. |
| 1181 | * 4) if there are still other permanent address(es), |
| 1182 | * corresponding prefix is still permanent. |
| 1183 | * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE, |
| 1184 | * don't purge the prefix, assume user space is managing it. |
| 1185 | * 6) otherwise, update prefix lifetime to the |
| 1186 | * longest valid lifetime among the corresponding |
| 1187 | * addresses on the device. |
| 1188 | * Note: subsequent RA will update lifetime. |
| 1189 | **/ |
| 1190 | static enum cleanup_prefix_rt_t |
| 1191 | check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires) |
| 1192 | { |
| 1193 | struct inet6_ifaddr *ifa; |
| 1194 | struct inet6_dev *idev = ifp->idev; |
| 1195 | unsigned long lifetime; |
| 1196 | enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL; |
| 1197 | |
| 1198 | *expires = jiffies; |
| 1199 | |
| 1200 | list_for_each_entry(ifa, &idev->addr_list, if_list) { |
| 1201 | if (ifa == ifp) |
| 1202 | continue; |
Zhiqiang Liu | e75913c | 2019-02-11 10:57:46 +0800 | [diff] [blame] | 1203 | if (ifa->prefix_len != ifp->prefix_len || |
| 1204 | !ipv6_prefix_equal(&ifa->addr, &ifp->addr, |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 1205 | ifp->prefix_len)) |
| 1206 | continue; |
| 1207 | if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE)) |
| 1208 | return CLEANUP_PREFIX_RT_NOP; |
| 1209 | |
| 1210 | action = CLEANUP_PREFIX_RT_EXPIRE; |
| 1211 | |
| 1212 | spin_lock(&ifa->lock); |
| 1213 | |
| 1214 | lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); |
| 1215 | /* |
| 1216 | * Note: Because this address is |
| 1217 | * not permanent, lifetime < |
| 1218 | * LONG_MAX / HZ here. |
| 1219 | */ |
| 1220 | if (time_before(*expires, ifa->tstamp + lifetime * HZ)) |
| 1221 | *expires = ifa->tstamp + lifetime * HZ; |
| 1222 | spin_unlock(&ifa->lock); |
| 1223 | } |
| 1224 | |
| 1225 | return action; |
| 1226 | } |
| 1227 | |
| 1228 | static void |
| 1229 | cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt) |
| 1230 | { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1231 | struct fib6_info *f6i; |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 1232 | |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 1233 | f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len, |
| 1234 | ifp->idev->dev, 0, RTF_DEFAULT, true); |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1235 | if (f6i) { |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 1236 | if (del_rt) |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1237 | ip6_del_rt(dev_net(ifp->idev->dev), f6i); |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 1238 | else { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 1239 | if (!(f6i->fib6_flags & RTF_EXPIRES)) |
| 1240 | fib6_set_expires(f6i, expires); |
| 1241 | fib6_info_release(f6i); |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 1242 | } |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | /* This function wants to get referenced ifp and releases it before return */ |
| 1248 | |
| 1249 | static void ipv6_del_addr(struct inet6_ifaddr *ifp) |
| 1250 | { |
Herbert Xu | 4c5ff6a | 2010-05-18 15:54:18 -0700 | [diff] [blame] | 1251 | int state; |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 1252 | enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP; |
| 1253 | unsigned long expires; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 1255 | ASSERT_RTNL(); |
| 1256 | |
Hannes Frederic Sowa | 8e8e676 | 2015-03-23 23:36:03 +0100 | [diff] [blame] | 1257 | spin_lock_bh(&ifp->lock); |
Herbert Xu | 4c5ff6a | 2010-05-18 15:54:18 -0700 | [diff] [blame] | 1258 | state = ifp->state; |
Herbert Xu | e9d3e08 | 2010-05-18 15:36:06 -0700 | [diff] [blame] | 1259 | ifp->state = INET6_IFADDR_STATE_DEAD; |
Hannes Frederic Sowa | 8e8e676 | 2015-03-23 23:36:03 +0100 | [diff] [blame] | 1260 | spin_unlock_bh(&ifp->lock); |
Herbert Xu | 4c5ff6a | 2010-05-18 15:54:18 -0700 | [diff] [blame] | 1261 | |
| 1262 | if (state == INET6_IFADDR_STATE_DEAD) |
| 1263 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 1265 | spin_lock_bh(&addrconf_hash_lock); |
| 1266 | hlist_del_init_rcu(&ifp->addr_lst); |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 1267 | spin_unlock_bh(&addrconf_hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 1269 | write_lock_bh(&ifp->idev->lock); |
David S. Miller | 5d9efa7 | 2013-10-28 20:07:50 -0400 | [diff] [blame] | 1270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | if (ifp->flags&IFA_F_TEMPORARY) { |
stephen hemminger | 372e6c8 | 2010-03-17 20:31:09 +0000 | [diff] [blame] | 1272 | list_del(&ifp->tmp_list); |
| 1273 | if (ifp->ifpub) { |
| 1274 | in6_ifa_put(ifp->ifpub); |
| 1275 | ifp->ifpub = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | } |
stephen hemminger | 372e6c8 | 2010-03-17 20:31:09 +0000 | [diff] [blame] | 1277 | __in6_ifa_put(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 1280 | if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE)) |
| 1281 | action = check_cleanup_prefix_route(ifp, &expires); |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 1282 | |
Eric Dumazet | 8ef802a | 2017-10-07 19:30:23 -0700 | [diff] [blame] | 1283 | list_del_rcu(&ifp->if_list); |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 1284 | __in6_ifa_put(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 1286 | write_unlock_bh(&ifp->idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 1288 | addrconf_del_dad_work(ifp); |
Andrey Vagin | b223856 | 2008-07-08 15:13:31 -0700 | [diff] [blame] | 1289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | ipv6_ifa_notify(RTM_DELADDR, ifp); |
| 1291 | |
Cong Wang | f88c91d | 2013-04-14 23:18:43 +0800 | [diff] [blame] | 1292 | inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 1294 | if (action != CLEANUP_PREFIX_RT_NOP) { |
| 1295 | cleanup_prefix_route(ifp, expires, |
| 1296 | action == CLEANUP_PREFIX_RT_DEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 1299 | /* clean up prefsrc entries */ |
| 1300 | rt6_remove_prefsrc(ifp); |
Herbert Xu | 4c5ff6a | 2010-05-18 15:54:18 -0700 | [diff] [blame] | 1301 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | in6_ifa_put(ifp); |
| 1303 | } |
| 1304 | |
Eric Dumazet | fffcefe | 2017-11-06 14:13:29 -0800 | [diff] [blame] | 1305 | static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, |
| 1306 | struct inet6_ifaddr *ift, |
| 1307 | bool block) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | { |
| 1309 | struct inet6_dev *idev = ifp->idev; |
| 1310 | struct in6_addr addr, *tmpaddr; |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1311 | unsigned long tmp_tstamp, age; |
Benoit Boissinot | eac55bf | 2008-04-02 00:01:35 -0700 | [diff] [blame] | 1312 | unsigned long regen_advance; |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1313 | struct ifa6_config cfg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | int ret = 0; |
Lorenzo Colitti | 76f793e | 2011-07-26 13:50:49 +0000 | [diff] [blame] | 1315 | unsigned long now = jiffies; |
Jiri Bohac | 76506a9 | 2016-10-13 18:52:15 +0200 | [diff] [blame] | 1316 | long max_desync_factor; |
Jiri Bohac | 7aa8e63 | 2016-10-20 12:29:26 +0200 | [diff] [blame] | 1317 | s32 cnf_temp_preferred_lft; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 1319 | write_lock_bh(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | if (ift) { |
| 1321 | spin_lock_bh(&ift->lock); |
| 1322 | memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); |
| 1323 | spin_unlock_bh(&ift->lock); |
| 1324 | tmpaddr = &addr; |
| 1325 | } else { |
| 1326 | tmpaddr = NULL; |
| 1327 | } |
| 1328 | retry: |
| 1329 | in6_dev_hold(idev); |
| 1330 | if (idev->cnf.use_tempaddr <= 0) { |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 1331 | write_unlock_bh(&idev->lock); |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 1332 | pr_info("%s: use_tempaddr is disabled\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | in6_dev_put(idev); |
| 1334 | ret = -1; |
| 1335 | goto out; |
| 1336 | } |
| 1337 | spin_lock_bh(&ifp->lock); |
| 1338 | if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { |
| 1339 | idev->cnf.use_tempaddr = -1; /*XXX*/ |
| 1340 | spin_unlock_bh(&ifp->lock); |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 1341 | write_unlock_bh(&idev->lock); |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 1342 | pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", |
| 1343 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | in6_dev_put(idev); |
| 1345 | ret = -1; |
| 1346 | goto out; |
| 1347 | } |
| 1348 | in6_ifa_hold(ifp); |
| 1349 | memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); |
Jiri Bohac | 9d6280d | 2016-10-13 18:50:02 +0200 | [diff] [blame] | 1350 | ipv6_try_regen_rndid(idev, tmpaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | memcpy(&addr.s6_addr[8], idev->rndid, 8); |
Lorenzo Colitti | 76f793e | 2011-07-26 13:50:49 +0000 | [diff] [blame] | 1352 | age = (now - ifp->tstamp) / HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | |
Benoit Boissinot | eac55bf | 2008-04-02 00:01:35 -0700 | [diff] [blame] | 1354 | regen_advance = idev->cnf.regen_max_retry * |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1355 | idev->cnf.dad_transmits * |
| 1356 | NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ; |
Jiri Bohac | 76506a9 | 2016-10-13 18:52:15 +0200 | [diff] [blame] | 1357 | |
| 1358 | /* recalculate max_desync_factor each time and update |
| 1359 | * idev->desync_factor if it's larger |
| 1360 | */ |
Jiri Bohac | 7aa8e63 | 2016-10-20 12:29:26 +0200 | [diff] [blame] | 1361 | cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft); |
Jiri Bohac | 76506a9 | 2016-10-13 18:52:15 +0200 | [diff] [blame] | 1362 | max_desync_factor = min_t(__u32, |
| 1363 | idev->cnf.max_desync_factor, |
Jiri Bohac | 7aa8e63 | 2016-10-20 12:29:26 +0200 | [diff] [blame] | 1364 | cnf_temp_preferred_lft - regen_advance); |
Jiri Bohac | 76506a9 | 2016-10-13 18:52:15 +0200 | [diff] [blame] | 1365 | |
| 1366 | if (unlikely(idev->desync_factor > max_desync_factor)) { |
| 1367 | if (max_desync_factor > 0) { |
| 1368 | get_random_bytes(&idev->desync_factor, |
| 1369 | sizeof(idev->desync_factor)); |
| 1370 | idev->desync_factor %= max_desync_factor; |
| 1371 | } else { |
| 1372 | idev->desync_factor = 0; |
| 1373 | } |
| 1374 | } |
| 1375 | |
David Ahern | 3f2d67b | 2018-06-11 07:12:12 -0700 | [diff] [blame] | 1376 | memset(&cfg, 0, sizeof(cfg)); |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1377 | cfg.valid_lft = min_t(__u32, ifp->valid_lft, |
Jiri Bohac | 76506a9 | 2016-10-13 18:52:15 +0200 | [diff] [blame] | 1378 | idev->cnf.temp_valid_lft + age); |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1379 | cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor; |
| 1380 | cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft); |
| 1381 | |
| 1382 | cfg.plen = ifp->prefix_len; |
Jiri Bohac | 76506a9 | 2016-10-13 18:52:15 +0200 | [diff] [blame] | 1383 | tmp_tstamp = ifp->tstamp; |
| 1384 | spin_unlock_bh(&ifp->lock); |
| 1385 | |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 1386 | write_unlock_bh(&idev->lock); |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 1387 | |
Benoit Boissinot | eac55bf | 2008-04-02 00:01:35 -0700 | [diff] [blame] | 1388 | /* A temporary address is created only if this calculated Preferred |
| 1389 | * Lifetime is greater than REGEN_ADVANCE time units. In particular, |
| 1390 | * an implementation must not create a temporary address with a zero |
| 1391 | * Preferred Lifetime. |
Heiner Kallweit | ecab670 | 2014-03-12 22:13:19 +0100 | [diff] [blame] | 1392 | * Use age calculation as in addrconf_verify to avoid unnecessary |
| 1393 | * temporary addresses being generated. |
Benoit Boissinot | eac55bf | 2008-04-02 00:01:35 -0700 | [diff] [blame] | 1394 | */ |
Heiner Kallweit | ecab670 | 2014-03-12 22:13:19 +0100 | [diff] [blame] | 1395 | age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1396 | if (cfg.preferred_lft <= regen_advance + age) { |
Benoit Boissinot | eac55bf | 2008-04-02 00:01:35 -0700 | [diff] [blame] | 1397 | in6_ifa_put(ifp); |
| 1398 | in6_dev_put(idev); |
| 1399 | ret = -1; |
| 1400 | goto out; |
| 1401 | } |
| 1402 | |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1403 | cfg.ifa_flags = IFA_F_TEMPORARY; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 1404 | /* set in addrconf_prefix_rcv() */ |
| 1405 | if (ifp->flags & IFA_F_OPTIMISTIC) |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1406 | cfg.ifa_flags |= IFA_F_OPTIMISTIC; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 1407 | |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 1408 | cfg.pfx = &addr; |
| 1409 | cfg.scope = ipv6_addr_scope(cfg.pfx); |
| 1410 | |
| 1411 | ift = ipv6_add_addr(idev, &cfg, block, NULL); |
Hannes Frederic Sowa | 4b08a8f | 2013-08-16 13:02:27 +0200 | [diff] [blame] | 1412 | if (IS_ERR(ift)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | in6_ifa_put(ifp); |
| 1414 | in6_dev_put(idev); |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 1415 | pr_info("%s: retry temporary address regeneration\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | tmpaddr = &addr; |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 1417 | write_lock_bh(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | goto retry; |
| 1419 | } |
| 1420 | |
| 1421 | spin_lock_bh(&ift->lock); |
| 1422 | ift->ifpub = ifp; |
Lorenzo Colitti | 76f793e | 2011-07-26 13:50:49 +0000 | [diff] [blame] | 1423 | ift->cstamp = now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | ift->tstamp = tmp_tstamp; |
| 1425 | spin_unlock_bh(&ift->lock); |
| 1426 | |
David S. Miller | cf22f9a | 2012-04-14 21:37:40 -0400 | [diff] [blame] | 1427 | addrconf_dad_start(ift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | in6_ifa_put(ift); |
| 1429 | in6_dev_put(idev); |
| 1430 | out: |
| 1431 | return ret; |
| 1432 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | |
| 1434 | /* |
YOSHIFUJI Hideaki | 072047e4 | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1435 | * Choose an appropriate source address (RFC3484) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | */ |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1437 | enum { |
| 1438 | IPV6_SADDR_RULE_INIT = 0, |
| 1439 | IPV6_SADDR_RULE_LOCAL, |
| 1440 | IPV6_SADDR_RULE_SCOPE, |
| 1441 | IPV6_SADDR_RULE_PREFERRED, |
| 1442 | #ifdef CONFIG_IPV6_MIP6 |
| 1443 | IPV6_SADDR_RULE_HOA, |
| 1444 | #endif |
| 1445 | IPV6_SADDR_RULE_OIF, |
| 1446 | IPV6_SADDR_RULE_LABEL, |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1447 | IPV6_SADDR_RULE_PRIVACY, |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1448 | IPV6_SADDR_RULE_ORCHID, |
| 1449 | IPV6_SADDR_RULE_PREFIX, |
Erik Kline | 7fd2561 | 2014-10-28 18:11:14 +0900 | [diff] [blame] | 1450 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
| 1451 | IPV6_SADDR_RULE_NOT_OPTIMISTIC, |
| 1452 | #endif |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1453 | IPV6_SADDR_RULE_MAX |
YOSHIFUJI Hideaki | 072047e4 | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1454 | }; |
| 1455 | |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1456 | struct ipv6_saddr_score { |
| 1457 | int rule; |
| 1458 | int addr_type; |
| 1459 | struct inet6_ifaddr *ifa; |
| 1460 | DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); |
| 1461 | int scopedist; |
| 1462 | int matchlen; |
| 1463 | }; |
| 1464 | |
| 1465 | struct ipv6_saddr_dst { |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1466 | const struct in6_addr *addr; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1467 | int ifindex; |
| 1468 | int scope; |
| 1469 | int label; |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1470 | unsigned int prefs; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1471 | }; |
YOSHIFUJI Hideaki | 072047e4 | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1472 | |
Dave Jones | b6f99a2 | 2007-03-22 12:27:49 -0700 | [diff] [blame] | 1473 | static inline int ipv6_saddr_preferred(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | { |
Ulrich Weber | 45bb006 | 2010-02-25 23:28:58 +0000 | [diff] [blame] | 1475 | if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) |
YOSHIFUJI Hideaki | 072047e4 | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1476 | return 1; |
| 1477 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
Matteo Croce | 35e015e | 2017-09-12 17:46:37 +0200 | [diff] [blame] | 1480 | static bool ipv6_use_optimistic_addr(struct net *net, |
| 1481 | struct inet6_dev *idev) |
Erik Kline | 7fd2561 | 2014-10-28 18:11:14 +0900 | [diff] [blame] | 1482 | { |
| 1483 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
Matteo Croce | 35e015e | 2017-09-12 17:46:37 +0200 | [diff] [blame] | 1484 | if (!idev) |
| 1485 | return false; |
| 1486 | if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) |
| 1487 | return false; |
| 1488 | if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic) |
| 1489 | return false; |
| 1490 | |
| 1491 | return true; |
Erik Kline | 7fd2561 | 2014-10-28 18:11:14 +0900 | [diff] [blame] | 1492 | #else |
| 1493 | return false; |
| 1494 | #endif |
| 1495 | } |
| 1496 | |
Sabrina Dubroca | f1c02cf | 2018-02-28 16:40:08 +0100 | [diff] [blame] | 1497 | static bool ipv6_allow_optimistic_dad(struct net *net, |
| 1498 | struct inet6_dev *idev) |
| 1499 | { |
| 1500 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
| 1501 | if (!idev) |
| 1502 | return false; |
| 1503 | if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad) |
| 1504 | return false; |
| 1505 | |
| 1506 | return true; |
| 1507 | #else |
| 1508 | return false; |
| 1509 | #endif |
| 1510 | } |
| 1511 | |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 1512 | static int ipv6_get_saddr_eval(struct net *net, |
| 1513 | struct ipv6_saddr_score *score, |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1514 | struct ipv6_saddr_dst *dst, |
| 1515 | int i) |
| 1516 | { |
| 1517 | int ret; |
| 1518 | |
| 1519 | if (i <= score->rule) { |
| 1520 | switch (i) { |
| 1521 | case IPV6_SADDR_RULE_SCOPE: |
| 1522 | ret = score->scopedist; |
| 1523 | break; |
| 1524 | case IPV6_SADDR_RULE_PREFIX: |
| 1525 | ret = score->matchlen; |
| 1526 | break; |
| 1527 | default: |
| 1528 | ret = !!test_bit(i, score->scorebits); |
| 1529 | } |
| 1530 | goto out; |
| 1531 | } |
| 1532 | |
| 1533 | switch (i) { |
| 1534 | case IPV6_SADDR_RULE_INIT: |
| 1535 | /* Rule 0: remember if hiscore is not ready yet */ |
| 1536 | ret = !!score->ifa; |
| 1537 | break; |
| 1538 | case IPV6_SADDR_RULE_LOCAL: |
| 1539 | /* Rule 1: Prefer same address */ |
| 1540 | ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); |
| 1541 | break; |
| 1542 | case IPV6_SADDR_RULE_SCOPE: |
| 1543 | /* Rule 2: Prefer appropriate scope |
| 1544 | * |
| 1545 | * ret |
| 1546 | * ^ |
| 1547 | * -1 | d 15 |
| 1548 | * ---+--+-+---> scope |
| 1549 | * | |
| 1550 | * | d is scope of the destination. |
| 1551 | * B-d | \ |
| 1552 | * | \ <- smaller scope is better if |
stephen hemminger | db9c7c3 | 2014-01-12 11:26:32 -0800 | [diff] [blame] | 1553 | * B-15 | \ if scope is enough for destination. |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1554 | * | ret = B - scope (-1 <= scope >= d <= 15). |
| 1555 | * d-C-1 | / |
| 1556 | * |/ <- greater is better |
| 1557 | * -C / if scope is not enough for destination. |
| 1558 | * /| ret = scope - C (-1 <= d < scope <= 15). |
| 1559 | * |
| 1560 | * d - C - 1 < B -15 (for all -1 <= d <= 15). |
| 1561 | * C > d + 14 - B >= 15 + 14 - B = 29 - B. |
| 1562 | * Assume B = 0 and we get C > 29. |
| 1563 | */ |
| 1564 | ret = __ipv6_addr_src_scope(score->addr_type); |
| 1565 | if (ret >= dst->scope) |
| 1566 | ret = -ret; |
| 1567 | else |
| 1568 | ret -= 128; /* 30 is enough */ |
| 1569 | score->scopedist = ret; |
| 1570 | break; |
| 1571 | case IPV6_SADDR_RULE_PREFERRED: |
Erik Kline | 7fd2561 | 2014-10-28 18:11:14 +0900 | [diff] [blame] | 1572 | { |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1573 | /* Rule 3: Avoid deprecated and optimistic addresses */ |
Erik Kline | 7fd2561 | 2014-10-28 18:11:14 +0900 | [diff] [blame] | 1574 | u8 avoid = IFA_F_DEPRECATED; |
| 1575 | |
Matteo Croce | 35e015e | 2017-09-12 17:46:37 +0200 | [diff] [blame] | 1576 | if (!ipv6_use_optimistic_addr(net, score->ifa->idev)) |
Erik Kline | 7fd2561 | 2014-10-28 18:11:14 +0900 | [diff] [blame] | 1577 | avoid |= IFA_F_OPTIMISTIC; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1578 | ret = ipv6_saddr_preferred(score->addr_type) || |
Erik Kline | 7fd2561 | 2014-10-28 18:11:14 +0900 | [diff] [blame] | 1579 | !(score->ifa->flags & avoid); |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1580 | break; |
Erik Kline | 7fd2561 | 2014-10-28 18:11:14 +0900 | [diff] [blame] | 1581 | } |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1582 | #ifdef CONFIG_IPV6_MIP6 |
| 1583 | case IPV6_SADDR_RULE_HOA: |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1584 | { |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1585 | /* Rule 4: Prefer home address */ |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1586 | int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); |
| 1587 | ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1588 | break; |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1589 | } |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1590 | #endif |
| 1591 | case IPV6_SADDR_RULE_OIF: |
| 1592 | /* Rule 5: Prefer outgoing interface */ |
| 1593 | ret = (!dst->ifindex || |
| 1594 | dst->ifindex == score->ifa->idev->dev->ifindex); |
| 1595 | break; |
| 1596 | case IPV6_SADDR_RULE_LABEL: |
| 1597 | /* Rule 6: Prefer matching label */ |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 1598 | ret = ipv6_addr_label(net, |
| 1599 | &score->ifa->addr, score->addr_type, |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1600 | score->ifa->idev->dev->ifindex) == dst->label; |
| 1601 | break; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1602 | case IPV6_SADDR_RULE_PRIVACY: |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1603 | { |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1604 | /* Rule 7: Prefer public address |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1605 | * Note: prefer temporary address if use_tempaddr >= 2 |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1606 | */ |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1607 | int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? |
| 1608 | !!(dst->prefs & IPV6_PREFER_SRC_TMP) : |
| 1609 | score->ifa->idev->cnf.use_tempaddr >= 2; |
| 1610 | ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1611 | break; |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1612 | } |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1613 | case IPV6_SADDR_RULE_ORCHID: |
| 1614 | /* Rule 8-: Prefer ORCHID vs ORCHID or |
| 1615 | * non-ORCHID vs non-ORCHID |
| 1616 | */ |
| 1617 | ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ |
| 1618 | ipv6_addr_orchid(dst->addr)); |
| 1619 | break; |
| 1620 | case IPV6_SADDR_RULE_PREFIX: |
| 1621 | /* Rule 8: Use longest matching prefix */ |
YOSHIFUJI Hideaki / 吉藤英明 | 91b4b04 | 2012-09-10 18:41:07 +0000 | [diff] [blame] | 1622 | ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); |
| 1623 | if (ret > score->ifa->prefix_len) |
| 1624 | ret = score->ifa->prefix_len; |
| 1625 | score->matchlen = ret; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1626 | break; |
Erik Kline | 7fd2561 | 2014-10-28 18:11:14 +0900 | [diff] [blame] | 1627 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
| 1628 | case IPV6_SADDR_RULE_NOT_OPTIMISTIC: |
| 1629 | /* Optimistic addresses still have lower precedence than other |
| 1630 | * preferred addresses. |
| 1631 | */ |
| 1632 | ret = !(score->ifa->flags & IFA_F_OPTIMISTIC); |
| 1633 | break; |
| 1634 | #endif |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1635 | default: |
| 1636 | ret = 0; |
| 1637 | } |
| 1638 | |
| 1639 | if (ret) |
| 1640 | __set_bit(i, score->scorebits); |
| 1641 | score->rule = i; |
| 1642 | out: |
| 1643 | return ret; |
| 1644 | } |
| 1645 | |
YOSHIFUJI Hideaki/吉藤英明 | c0b8da1 | 2015-07-13 23:28:10 +0900 | [diff] [blame] | 1646 | static int __ipv6_dev_get_saddr(struct net *net, |
| 1647 | struct ipv6_saddr_dst *dst, |
YOSHIFUJI Hideaki/吉藤英明 | c0b8da1 | 2015-07-13 23:28:10 +0900 | [diff] [blame] | 1648 | struct inet6_dev *idev, |
| 1649 | struct ipv6_saddr_score *scores, |
| 1650 | int hiscore_idx) |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1651 | { |
YOSHIFUJI Hideaki/吉藤英明 | c0b8da1 | 2015-07-13 23:28:10 +0900 | [diff] [blame] | 1652 | struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx]; |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1653 | |
Eric Dumazet | f59c031 | 2017-10-07 19:30:27 -0700 | [diff] [blame] | 1654 | list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) { |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1655 | int i; |
| 1656 | |
| 1657 | /* |
| 1658 | * - Tentative Address (RFC2462 section 5.4) |
| 1659 | * - A tentative address is not considered |
| 1660 | * "assigned to an interface" in the traditional |
| 1661 | * sense, unless it is also flagged as optimistic. |
| 1662 | * - Candidate Source Address (section 4) |
| 1663 | * - In any case, anycast addresses, multicast |
| 1664 | * addresses, and the unspecified address MUST |
| 1665 | * NOT be included in a candidate set. |
| 1666 | */ |
| 1667 | if ((score->ifa->flags & IFA_F_TENTATIVE) && |
| 1668 | (!(score->ifa->flags & IFA_F_OPTIMISTIC))) |
| 1669 | continue; |
| 1670 | |
| 1671 | score->addr_type = __ipv6_addr_type(&score->ifa->addr); |
| 1672 | |
| 1673 | if (unlikely(score->addr_type == IPV6_ADDR_ANY || |
| 1674 | score->addr_type & IPV6_ADDR_MULTICAST)) { |
| 1675 | net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s", |
| 1676 | idev->dev->name); |
| 1677 | continue; |
| 1678 | } |
| 1679 | |
| 1680 | score->rule = -1; |
| 1681 | bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); |
| 1682 | |
| 1683 | for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { |
| 1684 | int minihiscore, miniscore; |
| 1685 | |
| 1686 | minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i); |
| 1687 | miniscore = ipv6_get_saddr_eval(net, score, dst, i); |
| 1688 | |
| 1689 | if (minihiscore > miniscore) { |
| 1690 | if (i == IPV6_SADDR_RULE_SCOPE && |
| 1691 | score->scopedist > 0) { |
| 1692 | /* |
| 1693 | * special case: |
| 1694 | * each remaining entry |
| 1695 | * has too small (not enough) |
| 1696 | * scope, because ifa entries |
| 1697 | * are sorted by their scope |
| 1698 | * values. |
| 1699 | */ |
| 1700 | goto out; |
| 1701 | } |
| 1702 | break; |
| 1703 | } else if (minihiscore < miniscore) { |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1704 | swap(hiscore, score); |
YOSHIFUJI Hideaki/吉藤英明 | c0b8da1 | 2015-07-13 23:28:10 +0900 | [diff] [blame] | 1705 | hiscore_idx = 1 - hiscore_idx; |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1706 | |
| 1707 | /* restore our iterator */ |
| 1708 | score->ifa = hiscore->ifa; |
| 1709 | |
| 1710 | break; |
| 1711 | } |
| 1712 | } |
| 1713 | } |
| 1714 | out: |
YOSHIFUJI Hideaki/吉藤英明 | c0b8da1 | 2015-07-13 23:28:10 +0900 | [diff] [blame] | 1715 | return hiscore_idx; |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1716 | } |
| 1717 | |
David Ahern | afbac601 | 2016-06-16 16:24:26 -0700 | [diff] [blame] | 1718 | static int ipv6_get_saddr_master(struct net *net, |
| 1719 | const struct net_device *dst_dev, |
| 1720 | const struct net_device *master, |
| 1721 | struct ipv6_saddr_dst *dst, |
| 1722 | struct ipv6_saddr_score *scores, |
| 1723 | int hiscore_idx) |
| 1724 | { |
| 1725 | struct inet6_dev *idev; |
| 1726 | |
| 1727 | idev = __in6_dev_get(dst_dev); |
| 1728 | if (idev) |
| 1729 | hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, |
| 1730 | scores, hiscore_idx); |
| 1731 | |
| 1732 | idev = __in6_dev_get(master); |
| 1733 | if (idev) |
| 1734 | hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev, |
| 1735 | scores, hiscore_idx); |
| 1736 | |
| 1737 | return hiscore_idx; |
| 1738 | } |
| 1739 | |
Patrick McHardy | b3f644f | 2012-08-26 19:14:14 +0200 | [diff] [blame] | 1740 | int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1741 | const struct in6_addr *daddr, unsigned int prefs, |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1742 | struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | { |
YOSHIFUJI Hideaki/吉藤英明 | c0b8da1 | 2015-07-13 23:28:10 +0900 | [diff] [blame] | 1744 | struct ipv6_saddr_score scores[2], *hiscore; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1745 | struct ipv6_saddr_dst dst; |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1746 | struct inet6_dev *idev; |
YOSHIFUJI Hideaki | 072047e4 | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1747 | struct net_device *dev; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1748 | int dst_type; |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1749 | bool use_oif_addr = false; |
YOSHIFUJI Hideaki/吉藤英明 | c0b8da1 | 2015-07-13 23:28:10 +0900 | [diff] [blame] | 1750 | int hiscore_idx = 0; |
Eric Dumazet | cc429c8 | 2017-10-07 19:30:28 -0700 | [diff] [blame] | 1751 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1753 | dst_type = __ipv6_addr_type(daddr); |
| 1754 | dst.addr = daddr; |
| 1755 | dst.ifindex = dst_dev ? dst_dev->ifindex : 0; |
| 1756 | dst.scope = __ipv6_addr_src_scope(dst_type); |
Benjamin Thery | 3de2325 | 2008-05-28 14:51:24 +0200 | [diff] [blame] | 1757 | dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 1758 | dst.prefs = prefs; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1759 | |
YOSHIFUJI Hideaki/吉藤英明 | c0b8da1 | 2015-07-13 23:28:10 +0900 | [diff] [blame] | 1760 | scores[hiscore_idx].rule = -1; |
| 1761 | scores[hiscore_idx].ifa = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 1763 | rcu_read_lock(); |
YOSHIFUJI Hideaki | 072047e4 | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1764 | |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1765 | /* Candidate Source Address (section 4) |
| 1766 | * - multicast and link-local destination address, |
| 1767 | * the set of candidate source address MUST only |
| 1768 | * include addresses assigned to interfaces |
| 1769 | * belonging to the same link as the outgoing |
| 1770 | * interface. |
| 1771 | * (- For site-local destination addresses, the |
| 1772 | * set of candidate source addresses MUST only |
| 1773 | * include addresses assigned to interfaces |
| 1774 | * belonging to the same site as the outgoing |
| 1775 | * interface.) |
Erik Kline | 3985e8a | 2015-07-22 16:38:25 +0900 | [diff] [blame] | 1776 | * - "It is RECOMMENDED that the candidate source addresses |
| 1777 | * be the set of unicast addresses assigned to the |
| 1778 | * interface that will be used to send to the destination |
| 1779 | * (the 'outgoing' interface)." (RFC 6724) |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1780 | */ |
| 1781 | if (dst_dev) { |
Erik Kline | 3985e8a | 2015-07-22 16:38:25 +0900 | [diff] [blame] | 1782 | idev = __in6_dev_get(dst_dev); |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1783 | if ((dst_type & IPV6_ADDR_MULTICAST) || |
Erik Kline | 3985e8a | 2015-07-22 16:38:25 +0900 | [diff] [blame] | 1784 | dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL || |
| 1785 | (idev && idev->cnf.use_oif_addrs_only)) { |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1786 | use_oif_addr = true; |
YOSHIFUJI Hideaki | 072047e4 | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1787 | } |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | if (use_oif_addr) { |
YOSHIFUJI Hideaki/吉藤英明 | c0b8da1 | 2015-07-13 23:28:10 +0900 | [diff] [blame] | 1791 | if (idev) |
YOSHIFUJI Hideaki | c15df30 | 2015-07-16 16:51:30 +0900 | [diff] [blame] | 1792 | hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1793 | } else { |
David Ahern | afbac601 | 2016-06-16 16:24:26 -0700 | [diff] [blame] | 1794 | const struct net_device *master; |
| 1795 | int master_idx = 0; |
| 1796 | |
| 1797 | /* if dst_dev exists and is enslaved to an L3 device, then |
| 1798 | * prefer addresses from dst_dev and then the master over |
| 1799 | * any other enslaved devices in the L3 domain. |
| 1800 | */ |
| 1801 | master = l3mdev_master_dev_rcu(dst_dev); |
| 1802 | if (master) { |
| 1803 | master_idx = master->ifindex; |
| 1804 | |
| 1805 | hiscore_idx = ipv6_get_saddr_master(net, dst_dev, |
| 1806 | master, &dst, |
| 1807 | scores, hiscore_idx); |
| 1808 | |
| 1809 | if (scores[hiscore_idx].ifa) |
| 1810 | goto out; |
| 1811 | } |
| 1812 | |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1813 | for_each_netdev_rcu(net, dev) { |
David Ahern | afbac601 | 2016-06-16 16:24:26 -0700 | [diff] [blame] | 1814 | /* only consider addresses on devices in the |
| 1815 | * same L3 domain |
| 1816 | */ |
| 1817 | if (l3mdev_master_ifindex_rcu(dev) != master_idx) |
| 1818 | continue; |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1819 | idev = __in6_dev_get(dev); |
| 1820 | if (!idev) |
| 1821 | continue; |
YOSHIFUJI Hideaki | c15df30 | 2015-07-16 16:51:30 +0900 | [diff] [blame] | 1822 | hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx); |
YOSHIFUJI Hideaki/吉藤英明 | 9131f3d | 2015-07-10 16:58:31 +0900 | [diff] [blame] | 1823 | } |
YOSHIFUJI Hideaki | 072047e4 | 2005-11-08 09:38:30 -0800 | [diff] [blame] | 1824 | } |
David Ahern | afbac601 | 2016-06-16 16:24:26 -0700 | [diff] [blame] | 1825 | |
| 1826 | out: |
YOSHIFUJI Hideaki/吉藤英明 | c0b8da1 | 2015-07-13 23:28:10 +0900 | [diff] [blame] | 1827 | hiscore = &scores[hiscore_idx]; |
YOSHIFUJI Hideaki | a9b0572 | 2008-03-02 10:48:21 +0900 | [diff] [blame] | 1828 | if (!hiscore->ifa) |
Eric Dumazet | cc429c8 | 2017-10-07 19:30:28 -0700 | [diff] [blame] | 1829 | ret = -EADDRNOTAVAIL; |
| 1830 | else |
| 1831 | *saddr = hiscore->ifa->addr; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1832 | |
Eric Dumazet | cc429c8 | 2017-10-07 19:30:28 -0700 | [diff] [blame] | 1833 | rcu_read_unlock(); |
| 1834 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | } |
YOSHIFUJI Hideaki | 5e5f3f0 | 2008-03-03 21:44:34 +0900 | [diff] [blame] | 1836 | EXPORT_SYMBOL(ipv6_dev_get_saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | |
Amerigo Wang | 8965779 | 2013-06-29 21:30:49 +0800 | [diff] [blame] | 1838 | int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, |
Jiri Pirko | 479840f | 2013-12-06 09:45:21 +0100 | [diff] [blame] | 1839 | u32 banned_flags) |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 1840 | { |
| 1841 | struct inet6_ifaddr *ifp; |
| 1842 | int err = -EADDRNOTAVAIL; |
| 1843 | |
Hannes Frederic Sowa | 602582c | 2014-01-19 21:58:19 +0100 | [diff] [blame] | 1844 | list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { |
| 1845 | if (ifp->scope > IFA_LINK) |
| 1846 | break; |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 1847 | if (ifp->scope == IFA_LINK && |
| 1848 | !(ifp->flags & banned_flags)) { |
| 1849 | *addr = ifp->addr; |
| 1850 | err = 0; |
| 1851 | break; |
| 1852 | } |
| 1853 | } |
| 1854 | return err; |
| 1855 | } |
| 1856 | |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 1857 | int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, |
Jiri Pirko | 479840f | 2013-12-06 09:45:21 +0100 | [diff] [blame] | 1858 | u32 banned_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | { |
| 1860 | struct inet6_dev *idev; |
| 1861 | int err = -EADDRNOTAVAIL; |
| 1862 | |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 1863 | rcu_read_lock(); |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 1864 | idev = __in6_dev_get(dev); |
| 1865 | if (idev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | read_lock_bh(&idev->lock); |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 1867 | err = __ipv6_get_lladdr(idev, addr, banned_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | read_unlock_bh(&idev->lock); |
| 1869 | } |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 1870 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | return err; |
| 1872 | } |
| 1873 | |
Eric Dumazet | d9bf82c | 2017-10-07 19:30:24 -0700 | [diff] [blame] | 1874 | static int ipv6_count_addresses(const struct inet6_dev *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | { |
Eric Dumazet | d9bf82c | 2017-10-07 19:30:24 -0700 | [diff] [blame] | 1876 | const struct inet6_ifaddr *ifp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | int cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | |
Eric Dumazet | d9bf82c | 2017-10-07 19:30:24 -0700 | [diff] [blame] | 1879 | rcu_read_lock(); |
| 1880 | list_for_each_entry_rcu(ifp, &idev->addr_list, if_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | cnt++; |
Eric Dumazet | d9bf82c | 2017-10-07 19:30:24 -0700 | [diff] [blame] | 1882 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | return cnt; |
| 1884 | } |
| 1885 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1886 | int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, |
Florian Westphal | 2a7851b | 2013-05-17 03:56:10 +0000 | [diff] [blame] | 1887 | const struct net_device *dev, int strict) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | { |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 1889 | return ipv6_chk_addr_and_flags(net, addr, dev, !dev, |
| 1890 | strict, IFA_F_TENTATIVE); |
Erik Kline | c58da4c | 2015-02-04 20:01:23 +0900 | [diff] [blame] | 1891 | } |
| 1892 | EXPORT_SYMBOL(ipv6_chk_addr); |
| 1893 | |
David Ahern | 1893ff2 | 2018-03-13 08:29:38 -0700 | [diff] [blame] | 1894 | /* device argument is used to find the L3 domain of interest. If |
| 1895 | * skip_dev_check is set, then the ifp device is not checked against |
| 1896 | * the passed in dev argument. So the 2 cases for addresses checks are: |
| 1897 | * 1. does the address exist in the L3 domain that dev is part of |
| 1898 | * (skip_dev_check = true), or |
| 1899 | * |
| 1900 | * 2. does the address exist on the specific device |
| 1901 | * (skip_dev_check = false) |
| 1902 | */ |
Erik Kline | c58da4c | 2015-02-04 20:01:23 +0900 | [diff] [blame] | 1903 | int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 1904 | const struct net_device *dev, bool skip_dev_check, |
| 1905 | int strict, u32 banned_flags) |
Erik Kline | c58da4c | 2015-02-04 20:01:23 +0900 | [diff] [blame] | 1906 | { |
Eric Dumazet | 3f27fb2 | 2017-10-23 16:17:47 -0700 | [diff] [blame] | 1907 | unsigned int hash = inet6_addr_hash(net, addr); |
David Ahern | 1893ff2 | 2018-03-13 08:29:38 -0700 | [diff] [blame] | 1908 | const struct net_device *l3mdev; |
Stephen Hemminger | eedf042 | 2010-05-17 22:27:12 -0700 | [diff] [blame] | 1909 | struct inet6_ifaddr *ifp; |
Erik Kline | c58da4c | 2015-02-04 20:01:23 +0900 | [diff] [blame] | 1910 | u32 ifp_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | |
Eric Dumazet | 480318a | 2017-10-23 16:17:48 -0700 | [diff] [blame] | 1912 | rcu_read_lock(); |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 1913 | |
David Ahern | 1893ff2 | 2018-03-13 08:29:38 -0700 | [diff] [blame] | 1914 | l3mdev = l3mdev_master_dev_rcu(dev); |
David Ahern | 232378e | 2018-03-13 08:29:37 -0700 | [diff] [blame] | 1915 | if (skip_dev_check) |
| 1916 | dev = NULL; |
| 1917 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1918 | hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1919 | if (!net_eq(dev_net(ifp->idev->dev), net)) |
Daniel Lezcano | bfeade0 | 2008-01-10 22:43:18 -0800 | [diff] [blame] | 1920 | continue; |
David Ahern | 1893ff2 | 2018-03-13 08:29:38 -0700 | [diff] [blame] | 1921 | |
| 1922 | if (l3mdev_master_dev_rcu(ifp->idev->dev) != l3mdev) |
| 1923 | continue; |
| 1924 | |
Erik Kline | c58da4c | 2015-02-04 20:01:23 +0900 | [diff] [blame] | 1925 | /* Decouple optimistic from tentative for evaluation here. |
| 1926 | * Ban optimistic addresses explicitly, when required. |
| 1927 | */ |
| 1928 | ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC) |
| 1929 | ? (ifp->flags&~IFA_F_TENTATIVE) |
| 1930 | : ifp->flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | if (ipv6_addr_equal(&ifp->addr, addr) && |
Erik Kline | c58da4c | 2015-02-04 20:01:23 +0900 | [diff] [blame] | 1932 | !(ifp_flags&banned_flags) && |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 1933 | (!dev || ifp->idev->dev == dev || |
Stephen Hemminger | eedf042 | 2010-05-17 22:27:12 -0700 | [diff] [blame] | 1934 | !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { |
Eric Dumazet | 480318a | 2017-10-23 16:17:48 -0700 | [diff] [blame] | 1935 | rcu_read_unlock(); |
Stephen Hemminger | eedf042 | 2010-05-17 22:27:12 -0700 | [diff] [blame] | 1936 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | } |
| 1938 | } |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 1939 | |
Eric Dumazet | 480318a | 2017-10-23 16:17:48 -0700 | [diff] [blame] | 1940 | rcu_read_unlock(); |
Stephen Hemminger | eedf042 | 2010-05-17 22:27:12 -0700 | [diff] [blame] | 1941 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | } |
Erik Kline | c58da4c | 2015-02-04 20:01:23 +0900 | [diff] [blame] | 1943 | EXPORT_SYMBOL(ipv6_chk_addr_and_flags); |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 1944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | |
Catalin\(ux\) M. BOIE | 7df37ff | 2013-09-23 23:04:19 +0300 | [diff] [blame] | 1946 | /* Compares an address/prefix_len with addresses on device @dev. |
| 1947 | * If one is found it returns true. |
| 1948 | */ |
| 1949 | bool ipv6_chk_custom_prefix(const struct in6_addr *addr, |
| 1950 | const unsigned int prefix_len, struct net_device *dev) |
| 1951 | { |
Eric Dumazet | 47e2694 | 2017-10-07 19:30:25 -0700 | [diff] [blame] | 1952 | const struct inet6_ifaddr *ifa; |
| 1953 | const struct inet6_dev *idev; |
Catalin\(ux\) M. BOIE | 7df37ff | 2013-09-23 23:04:19 +0300 | [diff] [blame] | 1954 | bool ret = false; |
| 1955 | |
| 1956 | rcu_read_lock(); |
| 1957 | idev = __in6_dev_get(dev); |
| 1958 | if (idev) { |
Eric Dumazet | 47e2694 | 2017-10-07 19:30:25 -0700 | [diff] [blame] | 1959 | list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { |
Catalin\(ux\) M. BOIE | 7df37ff | 2013-09-23 23:04:19 +0300 | [diff] [blame] | 1960 | ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len); |
| 1961 | if (ret) |
| 1962 | break; |
| 1963 | } |
Catalin\(ux\) M. BOIE | 7df37ff | 2013-09-23 23:04:19 +0300 | [diff] [blame] | 1964 | } |
| 1965 | rcu_read_unlock(); |
| 1966 | |
| 1967 | return ret; |
| 1968 | } |
| 1969 | EXPORT_SYMBOL(ipv6_chk_custom_prefix); |
| 1970 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1971 | int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) |
YOSHIFUJI Hideaki | 52eeeb8 | 2008-03-15 22:54:23 -0400 | [diff] [blame] | 1972 | { |
Eric Dumazet | 24ba333 | 2017-10-07 19:30:26 -0700 | [diff] [blame] | 1973 | const struct inet6_ifaddr *ifa; |
| 1974 | const struct inet6_dev *idev; |
YOSHIFUJI Hideaki | 52eeeb8 | 2008-03-15 22:54:23 -0400 | [diff] [blame] | 1975 | int onlink; |
| 1976 | |
| 1977 | onlink = 0; |
| 1978 | rcu_read_lock(); |
| 1979 | idev = __in6_dev_get(dev); |
| 1980 | if (idev) { |
Eric Dumazet | 24ba333 | 2017-10-07 19:30:26 -0700 | [diff] [blame] | 1981 | list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) { |
YOSHIFUJI Hideaki | 52eeeb8 | 2008-03-15 22:54:23 -0400 | [diff] [blame] | 1982 | onlink = ipv6_prefix_equal(addr, &ifa->addr, |
| 1983 | ifa->prefix_len); |
| 1984 | if (onlink) |
| 1985 | break; |
| 1986 | } |
YOSHIFUJI Hideaki | 52eeeb8 | 2008-03-15 22:54:23 -0400 | [diff] [blame] | 1987 | } |
| 1988 | rcu_read_unlock(); |
| 1989 | return onlink; |
| 1990 | } |
YOSHIFUJI Hideaki | 52eeeb8 | 2008-03-15 22:54:23 -0400 | [diff] [blame] | 1991 | EXPORT_SYMBOL(ipv6_chk_prefix); |
| 1992 | |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1993 | struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, |
Daniel Lezcano | 1cab3da | 2008-01-10 22:44:09 -0800 | [diff] [blame] | 1994 | struct net_device *dev, int strict) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | { |
Eric Dumazet | 3f27fb2 | 2017-10-23 16:17:47 -0700 | [diff] [blame] | 1996 | unsigned int hash = inet6_addr_hash(net, addr); |
David S. Miller | b79d1d5 | 2010-03-25 21:39:21 -0700 | [diff] [blame] | 1997 | struct inet6_ifaddr *ifp, *result = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | |
Eric Dumazet | 24f226d | 2017-10-23 16:17:49 -0700 | [diff] [blame] | 1999 | rcu_read_lock(); |
| 2000 | hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 2001 | if (!net_eq(dev_net(ifp->idev->dev), net)) |
Daniel Lezcano | 1cab3da | 2008-01-10 22:44:09 -0800 | [diff] [blame] | 2002 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | if (ipv6_addr_equal(&ifp->addr, addr)) { |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 2004 | if (!dev || ifp->idev->dev == dev || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { |
David S. Miller | b79d1d5 | 2010-03-25 21:39:21 -0700 | [diff] [blame] | 2006 | result = ifp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | in6_ifa_hold(ifp); |
| 2008 | break; |
| 2009 | } |
| 2010 | } |
| 2011 | } |
Eric Dumazet | 24f226d | 2017-10-23 16:17:49 -0700 | [diff] [blame] | 2012 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | |
David S. Miller | b79d1d5 | 2010-03-25 21:39:21 -0700 | [diff] [blame] | 2014 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | } |
| 2016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | /* Gets referenced address, destroys ifaddr */ |
| 2018 | |
Brian Haley | cc411d0 | 2009-09-09 14:41:32 +0000 | [diff] [blame] | 2019 | static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | { |
Lubomir Rintel | 3d171f3 | 2016-01-08 13:47:23 +0100 | [diff] [blame] | 2021 | if (dad_failed) |
| 2022 | ifp->flags |= IFA_F_DADFAILED; |
| 2023 | |
Sabrina Dubroca | ec8add2 | 2017-06-29 16:56:54 +0200 | [diff] [blame] | 2024 | if (ifp->flags&IFA_F_TEMPORARY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | struct inet6_ifaddr *ifpub; |
| 2026 | spin_lock_bh(&ifp->lock); |
| 2027 | ifpub = ifp->ifpub; |
| 2028 | if (ifpub) { |
| 2029 | in6_ifa_hold(ifpub); |
| 2030 | spin_unlock_bh(&ifp->lock); |
Eric Dumazet | fffcefe | 2017-11-06 14:13:29 -0800 | [diff] [blame] | 2031 | ipv6_create_tempaddr(ifpub, ifp, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | in6_ifa_put(ifpub); |
| 2033 | } else { |
| 2034 | spin_unlock_bh(&ifp->lock); |
| 2035 | } |
| 2036 | ipv6_del_addr(ifp); |
Sabrina Dubroca | ec8add2 | 2017-06-29 16:56:54 +0200 | [diff] [blame] | 2037 | } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) { |
| 2038 | spin_lock_bh(&ifp->lock); |
| 2039 | addrconf_del_dad_work(ifp); |
| 2040 | ifp->flags |= IFA_F_TENTATIVE; |
Sabrina Dubroca | f1c02cf | 2018-02-28 16:40:08 +0100 | [diff] [blame] | 2041 | if (dad_failed) |
| 2042 | ifp->flags &= ~IFA_F_OPTIMISTIC; |
Sabrina Dubroca | ec8add2 | 2017-06-29 16:56:54 +0200 | [diff] [blame] | 2043 | spin_unlock_bh(&ifp->lock); |
| 2044 | if (dad_failed) |
| 2045 | ipv6_ifa_notify(0, ifp); |
| 2046 | in6_ifa_put(ifp); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 2047 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | ipv6_del_addr(ifp); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 2049 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
Herbert Xu | f2344a1 | 2010-05-18 15:55:27 -0700 | [diff] [blame] | 2052 | static int addrconf_dad_end(struct inet6_ifaddr *ifp) |
| 2053 | { |
| 2054 | int err = -ENOENT; |
| 2055 | |
Hannes Frederic Sowa | 8e8e676 | 2015-03-23 23:36:03 +0100 | [diff] [blame] | 2056 | spin_lock_bh(&ifp->lock); |
Herbert Xu | f2344a1 | 2010-05-18 15:55:27 -0700 | [diff] [blame] | 2057 | if (ifp->state == INET6_IFADDR_STATE_DAD) { |
| 2058 | ifp->state = INET6_IFADDR_STATE_POSTDAD; |
| 2059 | err = 0; |
| 2060 | } |
Hannes Frederic Sowa | 8e8e676 | 2015-03-23 23:36:03 +0100 | [diff] [blame] | 2061 | spin_unlock_bh(&ifp->lock); |
Herbert Xu | f2344a1 | 2010-05-18 15:55:27 -0700 | [diff] [blame] | 2062 | |
| 2063 | return err; |
| 2064 | } |
| 2065 | |
Vishwanath Pai | da13c59 | 2017-10-30 19:38:52 -0400 | [diff] [blame] | 2066 | void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp) |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 2067 | { |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 2068 | struct inet6_dev *idev = ifp->idev; |
Hannes Frederic Sowa | 1855b7c | 2015-03-23 23:36:05 +0100 | [diff] [blame] | 2069 | struct net *net = dev_net(ifp->idev->dev); |
Brian Haley | 9bdd8d4 | 2009-03-18 18:22:48 -0700 | [diff] [blame] | 2070 | |
Ursula Braun | 853dc2e | 2010-10-24 23:06:43 +0000 | [diff] [blame] | 2071 | if (addrconf_dad_end(ifp)) { |
| 2072 | in6_ifa_put(ifp); |
Herbert Xu | f2344a1 | 2010-05-18 15:55:27 -0700 | [diff] [blame] | 2073 | return; |
Ursula Braun | 853dc2e | 2010-10-24 23:06:43 +0000 | [diff] [blame] | 2074 | } |
Herbert Xu | f2344a1 | 2010-05-18 15:55:27 -0700 | [diff] [blame] | 2075 | |
Vishwanath Pai | da13c59 | 2017-10-30 19:38:52 -0400 | [diff] [blame] | 2076 | net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n", |
| 2077 | ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source); |
Brian Haley | 9bdd8d4 | 2009-03-18 18:22:48 -0700 | [diff] [blame] | 2078 | |
Hannes Frederic Sowa | 5f40ef7 | 2015-03-23 23:36:04 +0100 | [diff] [blame] | 2079 | spin_lock_bh(&ifp->lock); |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 2080 | |
Hannes Frederic Sowa | 5f40ef7 | 2015-03-23 23:36:04 +0100 | [diff] [blame] | 2081 | if (ifp->flags & IFA_F_STABLE_PRIVACY) { |
Hannes Frederic Sowa | 5f40ef7 | 2015-03-23 23:36:04 +0100 | [diff] [blame] | 2082 | struct in6_addr new_addr; |
| 2083 | struct inet6_ifaddr *ifp2; |
Hannes Frederic Sowa | 5f40ef7 | 2015-03-23 23:36:04 +0100 | [diff] [blame] | 2084 | int retries = ifp->stable_privacy_retry + 1; |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 2085 | struct ifa6_config cfg = { |
| 2086 | .pfx = &new_addr, |
| 2087 | .plen = ifp->prefix_len, |
| 2088 | .ifa_flags = ifp->flags, |
| 2089 | .valid_lft = ifp->valid_lft, |
| 2090 | .preferred_lft = ifp->prefered_lft, |
| 2091 | .scope = ifp->scope, |
| 2092 | }; |
Hannes Frederic Sowa | 5f40ef7 | 2015-03-23 23:36:04 +0100 | [diff] [blame] | 2093 | |
Hannes Frederic Sowa | 1855b7c | 2015-03-23 23:36:05 +0100 | [diff] [blame] | 2094 | if (retries > net->ipv6.sysctl.idgen_retries) { |
Hannes Frederic Sowa | 5f40ef7 | 2015-03-23 23:36:04 +0100 | [diff] [blame] | 2095 | net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n", |
| 2096 | ifp->idev->dev->name); |
| 2097 | goto errdad; |
| 2098 | } |
| 2099 | |
| 2100 | new_addr = ifp->addr; |
| 2101 | if (ipv6_generate_stable_address(&new_addr, retries, |
| 2102 | idev)) |
| 2103 | goto errdad; |
| 2104 | |
Hannes Frederic Sowa | 5f40ef7 | 2015-03-23 23:36:04 +0100 | [diff] [blame] | 2105 | spin_unlock_bh(&ifp->lock); |
| 2106 | |
| 2107 | if (idev->cnf.max_addresses && |
| 2108 | ipv6_count_addresses(idev) >= |
| 2109 | idev->cnf.max_addresses) |
| 2110 | goto lock_errdad; |
| 2111 | |
| 2112 | net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n", |
| 2113 | ifp->idev->dev->name); |
| 2114 | |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 2115 | ifp2 = ipv6_add_addr(idev, &cfg, false, NULL); |
Hannes Frederic Sowa | 5f40ef7 | 2015-03-23 23:36:04 +0100 | [diff] [blame] | 2116 | if (IS_ERR(ifp2)) |
| 2117 | goto lock_errdad; |
| 2118 | |
| 2119 | spin_lock_bh(&ifp2->lock); |
| 2120 | ifp2->stable_privacy_retry = retries; |
| 2121 | ifp2->state = INET6_IFADDR_STATE_PREDAD; |
| 2122 | spin_unlock_bh(&ifp2->lock); |
| 2123 | |
Hannes Frederic Sowa | 1855b7c | 2015-03-23 23:36:05 +0100 | [diff] [blame] | 2124 | addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay); |
Hannes Frederic Sowa | 5f40ef7 | 2015-03-23 23:36:04 +0100 | [diff] [blame] | 2125 | in6_ifa_put(ifp2); |
| 2126 | lock_errdad: |
| 2127 | spin_lock_bh(&ifp->lock); |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 2128 | } |
| 2129 | |
Hannes Frederic Sowa | 5f40ef7 | 2015-03-23 23:36:04 +0100 | [diff] [blame] | 2130 | errdad: |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 2131 | /* transition from _POSTDAD to _ERRDAD */ |
| 2132 | ifp->state = INET6_IFADDR_STATE_ERRDAD; |
Hannes Frederic Sowa | 8e8e676 | 2015-03-23 23:36:03 +0100 | [diff] [blame] | 2133 | spin_unlock_bh(&ifp->lock); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 2134 | |
| 2135 | addrconf_mod_dad_work(ifp, 0); |
Wei Yongjun | 751eb6b | 2016-09-05 16:06:31 +0800 | [diff] [blame] | 2136 | in6_ifa_put(ifp); |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 2137 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | |
Sabrina Dubroca | a9ed4a2 | 2014-09-02 10:29:29 +0200 | [diff] [blame] | 2139 | /* Join to solicited addr multicast group. |
| 2140 | * caller must hold RTNL */ |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2141 | void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | { |
| 2143 | struct in6_addr maddr; |
| 2144 | |
| 2145 | if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) |
| 2146 | return; |
| 2147 | |
| 2148 | addrconf_addr_solict_mult(addr, &maddr); |
| 2149 | ipv6_dev_mc_inc(dev, &maddr); |
| 2150 | } |
| 2151 | |
Sabrina Dubroca | a9ed4a2 | 2014-09-02 10:29:29 +0200 | [diff] [blame] | 2152 | /* caller must hold RTNL */ |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2153 | void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | { |
| 2155 | struct in6_addr maddr; |
| 2156 | |
| 2157 | if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) |
| 2158 | return; |
| 2159 | |
| 2160 | addrconf_addr_solict_mult(addr, &maddr); |
| 2161 | __ipv6_dev_mc_dec(idev, &maddr); |
| 2162 | } |
| 2163 | |
Sabrina Dubroca | a9ed4a2 | 2014-09-02 10:29:29 +0200 | [diff] [blame] | 2164 | /* caller must hold RTNL */ |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 2165 | static void addrconf_join_anycast(struct inet6_ifaddr *ifp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | { |
| 2167 | struct in6_addr addr; |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 2168 | |
Hannes Frederic Sowa | 88ad314 | 2014-01-06 17:53:14 +0100 | [diff] [blame] | 2169 | if (ifp->prefix_len >= 127) /* RFC 6164 */ |
Bjørn Mork | 2bda8a0 | 2011-07-05 23:04:13 +0000 | [diff] [blame] | 2170 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); |
| 2172 | if (ipv6_addr_any(&addr)) |
| 2173 | return; |
WANG Cong | 013b4d9 | 2014-09-11 15:35:11 -0700 | [diff] [blame] | 2174 | __ipv6_dev_ac_inc(ifp->idev, &addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
Sabrina Dubroca | a9ed4a2 | 2014-09-02 10:29:29 +0200 | [diff] [blame] | 2177 | /* caller must hold RTNL */ |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 2178 | static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | { |
| 2180 | struct in6_addr addr; |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 2181 | |
Hannes Frederic Sowa | 88ad314 | 2014-01-06 17:53:14 +0100 | [diff] [blame] | 2182 | if (ifp->prefix_len >= 127) /* RFC 6164 */ |
YOSHIFUJI Hideaki | 32019e6 | 2011-07-24 11:44:34 +0000 | [diff] [blame] | 2183 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); |
| 2185 | if (ipv6_addr_any(&addr)) |
| 2186 | return; |
| 2187 | __ipv6_dev_ac_dec(ifp->idev, &addr); |
| 2188 | } |
| 2189 | |
Alexander Aring | 8a7a4b4 | 2017-03-12 10:19:36 +0200 | [diff] [blame] | 2190 | static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev) |
alex.bluesman.smirnov@gmail.com | 06a4c1c | 2012-05-10 03:25:52 +0000 | [diff] [blame] | 2191 | { |
Alexander Aring | 8a7a4b4 | 2017-03-12 10:19:36 +0200 | [diff] [blame] | 2192 | switch (dev->addr_len) { |
| 2193 | case ETH_ALEN: |
Luiz Augusto von Dentz | 9dae2e0 | 2017-03-12 10:19:38 +0200 | [diff] [blame] | 2194 | memcpy(eui, dev->dev_addr, 3); |
| 2195 | eui[3] = 0xFF; |
| 2196 | eui[4] = 0xFE; |
| 2197 | memcpy(eui + 5, dev->dev_addr + 3, 3); |
| 2198 | break; |
Alexander Aring | 8a7a4b4 | 2017-03-12 10:19:36 +0200 | [diff] [blame] | 2199 | case EUI64_ADDR_LEN: |
| 2200 | memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN); |
| 2201 | eui[0] ^= 2; |
| 2202 | break; |
| 2203 | default: |
alex.bluesman.smirnov@gmail.com | 06a4c1c | 2012-05-10 03:25:52 +0000 | [diff] [blame] | 2204 | return -1; |
Alexander Aring | 8a7a4b4 | 2017-03-12 10:19:36 +0200 | [diff] [blame] | 2205 | } |
| 2206 | |
alex.bluesman.smirnov@gmail.com | 06a4c1c | 2012-05-10 03:25:52 +0000 | [diff] [blame] | 2207 | return 0; |
| 2208 | } |
| 2209 | |
YOSHIFUJI Hideaki / 吉藤英明 | cb6bf35 | 2013-03-25 08:26:24 +0000 | [diff] [blame] | 2210 | static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) |
| 2211 | { |
| 2212 | union fwnet_hwaddr *ha; |
| 2213 | |
| 2214 | if (dev->addr_len != FWNET_ALEN) |
| 2215 | return -1; |
| 2216 | |
| 2217 | ha = (union fwnet_hwaddr *)dev->dev_addr; |
| 2218 | |
| 2219 | memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); |
| 2220 | eui[0] ^= 2; |
| 2221 | return 0; |
| 2222 | } |
| 2223 | |
YOSHIFUJI Hideaki | 073a8e0 | 2006-03-20 16:54:49 -0800 | [diff] [blame] | 2224 | static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) |
| 2225 | { |
| 2226 | /* XXX: inherit EUI-64 from other interface -- yoshfuji */ |
| 2227 | if (dev->addr_len != ARCNET_ALEN) |
| 2228 | return -1; |
| 2229 | memset(eui, 0, 7); |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 2230 | eui[7] = *(u8 *)dev->dev_addr; |
YOSHIFUJI Hideaki | 073a8e0 | 2006-03-20 16:54:49 -0800 | [diff] [blame] | 2231 | return 0; |
| 2232 | } |
| 2233 | |
| 2234 | static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) |
| 2235 | { |
| 2236 | if (dev->addr_len != INFINIBAND_ALEN) |
| 2237 | return -1; |
| 2238 | memcpy(eui, dev->dev_addr + 12, 8); |
| 2239 | eui[0] |= 2; |
| 2240 | return 0; |
| 2241 | } |
| 2242 | |
stephen hemminger | c61393e | 2010-10-04 20:17:53 +0000 | [diff] [blame] | 2243 | static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) |
YOSHIFUJI Hideaki | dfd982b | 2008-04-10 15:42:09 +0900 | [diff] [blame] | 2244 | { |
Sascha Hlusiak | 9af2851 | 2009-05-19 12:56:51 +0000 | [diff] [blame] | 2245 | if (addr == 0) |
| 2246 | return -1; |
YOSHIFUJI Hideaki | dfd982b | 2008-04-10 15:42:09 +0900 | [diff] [blame] | 2247 | eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || |
| 2248 | ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || |
| 2249 | ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || |
| 2250 | ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || |
| 2251 | ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || |
| 2252 | ipv4_is_lbcast(addr)) ? 0x00 : 0x02; |
| 2253 | eui[1] = 0; |
| 2254 | eui[2] = 0x5E; |
| 2255 | eui[3] = 0xFE; |
| 2256 | memcpy(eui + 4, &addr, 4); |
| 2257 | return 0; |
| 2258 | } |
YOSHIFUJI Hideaki | dfd982b | 2008-04-10 15:42:09 +0900 | [diff] [blame] | 2259 | |
| 2260 | static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) |
| 2261 | { |
| 2262 | if (dev->priv_flags & IFF_ISATAP) |
| 2263 | return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); |
| 2264 | return -1; |
| 2265 | } |
| 2266 | |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 2267 | static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) |
| 2268 | { |
| 2269 | return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); |
| 2270 | } |
| 2271 | |
Nicolas Dichtel | e837735 | 2013-08-20 12:16:06 +0200 | [diff] [blame] | 2272 | static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev) |
| 2273 | { |
| 2274 | memcpy(eui, dev->perm_addr, 3); |
| 2275 | memcpy(eui + 5, dev->perm_addr + 3, 3); |
| 2276 | eui[3] = 0xFF; |
| 2277 | eui[4] = 0xFE; |
| 2278 | eui[0] ^= 2; |
| 2279 | return 0; |
| 2280 | } |
| 2281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) |
| 2283 | { |
| 2284 | switch (dev->type) { |
| 2285 | case ARPHRD_ETHER: |
| 2286 | case ARPHRD_FDDI: |
YOSHIFUJI Hideaki | 073a8e0 | 2006-03-20 16:54:49 -0800 | [diff] [blame] | 2287 | return addrconf_ifid_eui48(eui, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | case ARPHRD_ARCNET: |
YOSHIFUJI Hideaki | 073a8e0 | 2006-03-20 16:54:49 -0800 | [diff] [blame] | 2289 | return addrconf_ifid_arcnet(eui, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | case ARPHRD_INFINIBAND: |
YOSHIFUJI Hideaki | 073a8e0 | 2006-03-20 16:54:49 -0800 | [diff] [blame] | 2291 | return addrconf_ifid_infiniband(eui, dev); |
Fred L. Templin | c7dc89c | 2007-11-29 22:11:40 +1100 | [diff] [blame] | 2292 | case ARPHRD_SIT: |
YOSHIFUJI Hideaki | dfd982b | 2008-04-10 15:42:09 +0900 | [diff] [blame] | 2293 | return addrconf_ifid_sit(eui, dev); |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 2294 | case ARPHRD_IPGRE: |
Felix Jia | 45ce0fd | 2017-01-26 16:59:18 +1300 | [diff] [blame] | 2295 | case ARPHRD_TUNNEL: |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 2296 | return addrconf_ifid_gre(eui, dev); |
Jukka Rissanen | e74bccb8 | 2013-12-11 17:05:36 +0200 | [diff] [blame] | 2297 | case ARPHRD_6LOWPAN: |
Alexander Aring | 8a7a4b4 | 2017-03-12 10:19:36 +0200 | [diff] [blame] | 2298 | return addrconf_ifid_6lowpan(eui, dev); |
YOSHIFUJI Hideaki / 吉藤英明 | cb6bf35 | 2013-03-25 08:26:24 +0000 | [diff] [blame] | 2299 | case ARPHRD_IEEE1394: |
| 2300 | return addrconf_ifid_ieee1394(eui, dev); |
Nicolas Dichtel | e837735 | 2013-08-20 12:16:06 +0200 | [diff] [blame] | 2301 | case ARPHRD_TUNNEL6: |
Felix Jia | 45ce0fd | 2017-01-26 16:59:18 +1300 | [diff] [blame] | 2302 | case ARPHRD_IP6GRE: |
Subash Abhinov Kasiviswanathan | 9deb441 | 2018-06-04 19:26:07 -0600 | [diff] [blame] | 2303 | case ARPHRD_RAWIP: |
Nicolas Dichtel | e837735 | 2013-08-20 12:16:06 +0200 | [diff] [blame] | 2304 | return addrconf_ifid_ip6tnl(eui, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | } |
| 2306 | return -1; |
| 2307 | } |
| 2308 | |
| 2309 | static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) |
| 2310 | { |
| 2311 | int err = -1; |
| 2312 | struct inet6_ifaddr *ifp; |
| 2313 | |
| 2314 | read_lock_bh(&idev->lock); |
Hannes Frederic Sowa | 602582c | 2014-01-19 21:58:19 +0100 | [diff] [blame] | 2315 | list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) { |
| 2316 | if (ifp->scope > IFA_LINK) |
| 2317 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { |
| 2319 | memcpy(eui, ifp->addr.s6_addr+8, 8); |
| 2320 | err = 0; |
| 2321 | break; |
| 2322 | } |
| 2323 | } |
| 2324 | read_unlock_bh(&idev->lock); |
| 2325 | return err; |
| 2326 | } |
| 2327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ |
Jiri Bohac | 9d6280d | 2016-10-13 18:50:02 +0200 | [diff] [blame] | 2329 | static void ipv6_regen_rndid(struct inet6_dev *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | regen: |
YOSHIFUJI Hideaki | 955189e | 2006-03-20 16:54:09 -0800 | [diff] [blame] | 2332 | get_random_bytes(idev->rndid, sizeof(idev->rndid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | idev->rndid[0] &= ~0x02; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | |
| 2335 | /* |
| 2336 | * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: |
| 2337 | * check if generated address is not inappropriate |
| 2338 | * |
| 2339 | * - Reserved subnet anycast (RFC 2526) |
| 2340 | * 11111101 11....11 1xxxxxxx |
Fred L. Templin | c7dc89c | 2007-11-29 22:11:40 +1100 | [diff] [blame] | 2341 | * - ISATAP (RFC4214) 6.1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | * 00-00-5E-FE-xx-xx-xx-xx |
| 2343 | * - value 0 |
| 2344 | * - XXX: already assigned to an address on the device |
| 2345 | */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2346 | if (idev->rndid[0] == 0xfd && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && |
| 2348 | (idev->rndid[7]&0x80)) |
| 2349 | goto regen; |
| 2350 | if ((idev->rndid[0]|idev->rndid[1]) == 0) { |
| 2351 | if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) |
| 2352 | goto regen; |
| 2353 | if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) |
| 2354 | goto regen; |
| 2355 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | } |
| 2357 | |
Jiri Bohac | 9d6280d | 2016-10-13 18:50:02 +0200 | [diff] [blame] | 2358 | static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 2359 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) |
Jiri Bohac | 9d6280d | 2016-10-13 18:50:02 +0200 | [diff] [blame] | 2361 | ipv6_regen_rndid(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | |
| 2364 | /* |
| 2365 | * Add prefix route. |
| 2366 | */ |
| 2367 | |
| 2368 | static void |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 2369 | addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric, |
| 2370 | struct net_device *dev, unsigned long expires, |
| 2371 | u32 flags, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2373 | struct fib6_config cfg = { |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 2374 | .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX, |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 2375 | .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2376 | .fc_ifindex = dev->ifindex, |
| 2377 | .fc_expires = expires, |
| 2378 | .fc_dst_len = plen, |
| 2379 | .fc_flags = RTF_UP | flags, |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2380 | .fc_nlinfo.nl_net = dev_net(dev), |
Stephen Hemminger | f410a1f | 2008-08-23 05:16:46 -0700 | [diff] [blame] | 2381 | .fc_protocol = RTPROT_KERNEL, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 2382 | .fc_type = RTN_UNICAST, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2383 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 2385 | cfg.fc_dst = *pfx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | |
| 2387 | /* Prevent useless cloning on PtP SIT. |
| 2388 | This thing is done here expecting that the whole |
| 2389 | class of non-broadcast devices need not cloning. |
| 2390 | */ |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 2391 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2392 | if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) |
| 2393 | cfg.fc_flags |= RTF_NONEXTHOP; |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 2394 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 2396 | ip6_route_add(&cfg, gfp_flags, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | } |
| 2398 | |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2399 | |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2400 | static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2401 | int plen, |
| 2402 | const struct net_device *dev, |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 2403 | u32 flags, u32 noflags, |
| 2404 | bool no_gw) |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2405 | { |
| 2406 | struct fib6_node *fn; |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2407 | struct fib6_info *rt = NULL; |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2408 | struct fib6_table *table; |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 2409 | u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX; |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2410 | |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 2411 | table = fib6_get_table(dev_net(dev), tb_id); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 2412 | if (!table) |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2413 | return NULL; |
| 2414 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2415 | rcu_read_lock(); |
Wei Wang | 38fbeee | 2017-10-06 12:06:02 -0700 | [diff] [blame] | 2416 | fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true); |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2417 | if (!fn) |
| 2418 | goto out; |
Martin KaFai Lau | 1f56a01f | 2015-04-28 13:03:03 -0700 | [diff] [blame] | 2419 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2420 | for_each_fib6_node_rt_rcu(fn) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 2421 | /* prefix routes only use builtin fib6_nh */ |
| 2422 | if (rt->nh) |
| 2423 | continue; |
| 2424 | |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 2425 | if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex) |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2426 | continue; |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 2427 | if (no_gw && rt->fib6_nh->fib_nh_gw_family) |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 2428 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2429 | if ((rt->fib6_flags & flags) != flags) |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2430 | continue; |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 2431 | if ((rt->fib6_flags & noflags) != 0) |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2432 | continue; |
Wei Wang | e873e4b | 2018-07-21 20:56:32 -0700 | [diff] [blame] | 2433 | if (!fib6_info_hold_safe(rt)) |
| 2434 | continue; |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2435 | break; |
| 2436 | } |
| 2437 | out: |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 2438 | rcu_read_unlock(); |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2439 | return rt; |
| 2440 | } |
| 2441 | |
| 2442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | /* Create "default" multicast route to the interface */ |
| 2444 | |
| 2445 | static void addrconf_add_mroute(struct net_device *dev) |
| 2446 | { |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2447 | struct fib6_config cfg = { |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 2448 | .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL, |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2449 | .fc_metric = IP6_RT_PRIO_ADDRCONF, |
| 2450 | .fc_ifindex = dev->ifindex, |
| 2451 | .fc_dst_len = 8, |
| 2452 | .fc_flags = RTF_UP, |
David Ahern | e8478e8 | 2018-04-17 17:33:13 -0700 | [diff] [blame] | 2453 | .fc_type = RTN_UNICAST, |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2454 | .fc_nlinfo.nl_net = dev_net(dev), |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2455 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | |
Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2457 | ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); |
| 2458 | |
Cong Wang | e500c6d | 2018-08-22 12:58:34 -0700 | [diff] [blame] | 2459 | ip6_route_add(&cfg, GFP_KERNEL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | } |
| 2461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | static struct inet6_dev *addrconf_add_dev(struct net_device *dev) |
| 2463 | { |
| 2464 | struct inet6_dev *idev; |
| 2465 | |
| 2466 | ASSERT_RTNL(); |
| 2467 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 2468 | idev = ipv6_find_idev(dev); |
Sabrina Dubroca | db0b99f | 2019-08-23 15:44:36 +0200 | [diff] [blame^] | 2469 | if (IS_ERR(idev)) |
| 2470 | return idev; |
Brian Haley | 64e724f | 2010-07-20 10:34:30 +0000 | [diff] [blame] | 2471 | |
| 2472 | if (idev->cnf.disable_ipv6) |
| 2473 | return ERR_PTR(-EACCES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | |
| 2475 | /* Add default multicast route */ |
David Ahern | ba46ee4 | 2016-06-13 13:44:18 -0700 | [diff] [blame] | 2476 | if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev)) |
Li Wei | 4af04ab | 2011-12-06 21:23:45 +0000 | [diff] [blame] | 2477 | addrconf_add_mroute(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | return idev; |
| 2480 | } |
| 2481 | |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 2482 | static void manage_tempaddrs(struct inet6_dev *idev, |
| 2483 | struct inet6_ifaddr *ifp, |
| 2484 | __u32 valid_lft, __u32 prefered_lft, |
| 2485 | bool create, unsigned long now) |
| 2486 | { |
| 2487 | u32 flags; |
| 2488 | struct inet6_ifaddr *ift; |
| 2489 | |
| 2490 | read_lock_bh(&idev->lock); |
| 2491 | /* update all temporary addresses in the list */ |
| 2492 | list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) { |
| 2493 | int age, max_valid, max_prefered; |
| 2494 | |
| 2495 | if (ifp != ift->ifpub) |
| 2496 | continue; |
| 2497 | |
| 2498 | /* RFC 4941 section 3.3: |
| 2499 | * If a received option will extend the lifetime of a public |
| 2500 | * address, the lifetimes of temporary addresses should |
| 2501 | * be extended, subject to the overall constraint that no |
| 2502 | * temporary addresses should ever remain "valid" or "preferred" |
| 2503 | * for a time longer than (TEMP_VALID_LIFETIME) or |
| 2504 | * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively. |
| 2505 | */ |
| 2506 | age = (now - ift->cstamp) / HZ; |
| 2507 | max_valid = idev->cnf.temp_valid_lft - age; |
| 2508 | if (max_valid < 0) |
| 2509 | max_valid = 0; |
| 2510 | |
| 2511 | max_prefered = idev->cnf.temp_prefered_lft - |
Jiri Bohac | 76506a9 | 2016-10-13 18:52:15 +0200 | [diff] [blame] | 2512 | idev->desync_factor - age; |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 2513 | if (max_prefered < 0) |
| 2514 | max_prefered = 0; |
| 2515 | |
| 2516 | if (valid_lft > max_valid) |
| 2517 | valid_lft = max_valid; |
| 2518 | |
| 2519 | if (prefered_lft > max_prefered) |
| 2520 | prefered_lft = max_prefered; |
| 2521 | |
| 2522 | spin_lock(&ift->lock); |
| 2523 | flags = ift->flags; |
| 2524 | ift->valid_lft = valid_lft; |
| 2525 | ift->prefered_lft = prefered_lft; |
| 2526 | ift->tstamp = now; |
| 2527 | if (prefered_lft > 0) |
| 2528 | ift->flags &= ~IFA_F_DEPRECATED; |
| 2529 | |
| 2530 | spin_unlock(&ift->lock); |
| 2531 | if (!(flags&IFA_F_TENTATIVE)) |
| 2532 | ipv6_ifa_notify(0, ift); |
| 2533 | } |
| 2534 | |
| 2535 | if ((create || list_empty(&idev->tempaddr_list)) && |
| 2536 | idev->cnf.use_tempaddr > 0) { |
| 2537 | /* When a new public address is created as described |
| 2538 | * in [ADDRCONF], also create a new temporary address. |
| 2539 | * Also create a temporary address if it's enabled but |
| 2540 | * no temporary address currently exists. |
| 2541 | */ |
| 2542 | read_unlock_bh(&idev->lock); |
Eric Dumazet | fffcefe | 2017-11-06 14:13:29 -0800 | [diff] [blame] | 2543 | ipv6_create_tempaddr(ifp, NULL, false); |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 2544 | } else { |
| 2545 | read_unlock_bh(&idev->lock); |
| 2546 | } |
| 2547 | } |
| 2548 | |
Bjørn Mork | cc9da6c | 2015-12-16 16:44:38 +0100 | [diff] [blame] | 2549 | static bool is_addr_mode_generate_stable(struct inet6_dev *idev) |
| 2550 | { |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 2551 | return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY || |
| 2552 | idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM; |
Bjørn Mork | cc9da6c | 2015-12-16 16:44:38 +0100 | [diff] [blame] | 2553 | } |
| 2554 | |
Alexander Aring | cc84b3c | 2016-06-15 21:20:24 +0200 | [diff] [blame] | 2555 | int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev, |
| 2556 | const struct prefix_info *pinfo, |
| 2557 | struct inet6_dev *in6_dev, |
| 2558 | const struct in6_addr *addr, int addr_type, |
| 2559 | u32 addr_flags, bool sllao, bool tokenized, |
| 2560 | __u32 valid_lft, u32 prefered_lft) |
Alexander Aring | 4f67223 | 2016-06-15 21:20:22 +0200 | [diff] [blame] | 2561 | { |
| 2562 | struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1); |
| 2563 | int create = 0, update_lft = 0; |
| 2564 | |
| 2565 | if (!ifp && valid_lft) { |
| 2566 | int max_addresses = in6_dev->cnf.max_addresses; |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 2567 | struct ifa6_config cfg = { |
| 2568 | .pfx = addr, |
| 2569 | .plen = pinfo->prefix_len, |
| 2570 | .ifa_flags = addr_flags, |
| 2571 | .valid_lft = valid_lft, |
| 2572 | .preferred_lft = prefered_lft, |
| 2573 | .scope = addr_type & IPV6_ADDR_SCOPE_MASK, |
| 2574 | }; |
Alexander Aring | 4f67223 | 2016-06-15 21:20:22 +0200 | [diff] [blame] | 2575 | |
| 2576 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
Matteo Croce | 35e015e | 2017-09-12 17:46:37 +0200 | [diff] [blame] | 2577 | if ((net->ipv6.devconf_all->optimistic_dad || |
| 2578 | in6_dev->cnf.optimistic_dad) && |
Alexander Aring | 4f67223 | 2016-06-15 21:20:22 +0200 | [diff] [blame] | 2579 | !net->ipv6.devconf_all->forwarding && sllao) |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 2580 | cfg.ifa_flags |= IFA_F_OPTIMISTIC; |
Alexander Aring | 4f67223 | 2016-06-15 21:20:22 +0200 | [diff] [blame] | 2581 | #endif |
| 2582 | |
| 2583 | /* Do not allow to create too much of autoconfigured |
| 2584 | * addresses; this would be too easy way to crash kernel. |
| 2585 | */ |
| 2586 | if (!max_addresses || |
| 2587 | ipv6_count_addresses(in6_dev) < max_addresses) |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 2588 | ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL); |
Alexander Aring | 4f67223 | 2016-06-15 21:20:22 +0200 | [diff] [blame] | 2589 | |
| 2590 | if (IS_ERR_OR_NULL(ifp)) |
| 2591 | return -1; |
| 2592 | |
Alexander Aring | 4f67223 | 2016-06-15 21:20:22 +0200 | [diff] [blame] | 2593 | create = 1; |
| 2594 | spin_lock_bh(&ifp->lock); |
| 2595 | ifp->flags |= IFA_F_MANAGETEMPADDR; |
| 2596 | ifp->cstamp = jiffies; |
| 2597 | ifp->tokenized = tokenized; |
| 2598 | spin_unlock_bh(&ifp->lock); |
| 2599 | addrconf_dad_start(ifp); |
| 2600 | } |
| 2601 | |
| 2602 | if (ifp) { |
| 2603 | u32 flags; |
| 2604 | unsigned long now; |
| 2605 | u32 stored_lft; |
| 2606 | |
| 2607 | /* update lifetime (RFC2462 5.5.3 e) */ |
| 2608 | spin_lock_bh(&ifp->lock); |
| 2609 | now = jiffies; |
| 2610 | if (ifp->valid_lft > (now - ifp->tstamp) / HZ) |
| 2611 | stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; |
| 2612 | else |
| 2613 | stored_lft = 0; |
Lorenzo Bianconi | f85f94b | 2018-04-16 17:52:59 +0200 | [diff] [blame] | 2614 | if (!create && stored_lft) { |
Alexander Aring | 4f67223 | 2016-06-15 21:20:22 +0200 | [diff] [blame] | 2615 | const u32 minimum_lft = min_t(u32, |
| 2616 | stored_lft, MIN_VALID_LIFETIME); |
| 2617 | valid_lft = max(valid_lft, minimum_lft); |
| 2618 | |
| 2619 | /* RFC4862 Section 5.5.3e: |
| 2620 | * "Note that the preferred lifetime of the |
| 2621 | * corresponding address is always reset to |
| 2622 | * the Preferred Lifetime in the received |
| 2623 | * Prefix Information option, regardless of |
| 2624 | * whether the valid lifetime is also reset or |
| 2625 | * ignored." |
| 2626 | * |
| 2627 | * So we should always update prefered_lft here. |
| 2628 | */ |
| 2629 | update_lft = 1; |
| 2630 | } |
| 2631 | |
| 2632 | if (update_lft) { |
| 2633 | ifp->valid_lft = valid_lft; |
| 2634 | ifp->prefered_lft = prefered_lft; |
| 2635 | ifp->tstamp = now; |
| 2636 | flags = ifp->flags; |
| 2637 | ifp->flags &= ~IFA_F_DEPRECATED; |
| 2638 | spin_unlock_bh(&ifp->lock); |
| 2639 | |
| 2640 | if (!(flags&IFA_F_TENTATIVE)) |
| 2641 | ipv6_ifa_notify(0, ifp); |
| 2642 | } else |
| 2643 | spin_unlock_bh(&ifp->lock); |
| 2644 | |
| 2645 | manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, |
| 2646 | create, now); |
| 2647 | |
| 2648 | in6_ifa_put(ifp); |
| 2649 | addrconf_verify(); |
| 2650 | } |
| 2651 | |
| 2652 | return 0; |
| 2653 | } |
Alexander Aring | cc84b3c | 2016-06-15 21:20:24 +0200 | [diff] [blame] | 2654 | EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr); |
Alexander Aring | 4f67223 | 2016-06-15 21:20:22 +0200 | [diff] [blame] | 2655 | |
Neil Horman | e6bff99 | 2012-01-04 10:49:15 +0000 | [diff] [blame] | 2656 | void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | { |
| 2658 | struct prefix_info *pinfo; |
| 2659 | __u32 valid_lft; |
| 2660 | __u32 prefered_lft; |
Alexander Aring | 4f67223 | 2016-06-15 21:20:22 +0200 | [diff] [blame] | 2661 | int addr_type, err; |
Hannes Frederic Sowa | 64236f3 | 2015-03-23 23:36:02 +0100 | [diff] [blame] | 2662 | u32 addr_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | struct inet6_dev *in6_dev; |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 2664 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | |
| 2666 | pinfo = (struct prefix_info *) opt; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | if (len < sizeof(struct prefix_info)) { |
Joe Perches | e32ac25 | 2018-03-26 08:35:01 -0700 | [diff] [blame] | 2669 | netdev_dbg(dev, "addrconf: prefix option too short\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | return; |
| 2671 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | /* |
| 2674 | * Validation checks ([ADDRCONF], page 19) |
| 2675 | */ |
| 2676 | |
| 2677 | addr_type = ipv6_addr_type(&pinfo->prefix); |
| 2678 | |
| 2679 | if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) |
| 2680 | return; |
| 2681 | |
| 2682 | valid_lft = ntohl(pinfo->valid); |
| 2683 | prefered_lft = ntohl(pinfo->prefered); |
| 2684 | |
| 2685 | if (prefered_lft > valid_lft) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 2686 | net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | return; |
| 2688 | } |
| 2689 | |
| 2690 | in6_dev = in6_dev_get(dev); |
| 2691 | |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 2692 | if (!in6_dev) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 2693 | net_dbg_ratelimited("addrconf: device %s not configured\n", |
| 2694 | dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | return; |
| 2696 | } |
| 2697 | |
| 2698 | /* |
| 2699 | * Two things going on here: |
| 2700 | * 1) Add routes for on-link prefixes |
| 2701 | * 2) Configure prefixes with the auto flag set |
| 2702 | */ |
| 2703 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2704 | if (pinfo->onlink) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 2705 | struct fib6_info *rt; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2706 | unsigned long rt_expires; |
| 2707 | |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2708 | /* Avoid arithmetic overflow. Really, we could |
| 2709 | * save rt_expires in seconds, likely valid_lft, |
| 2710 | * but it would require division in fib gc, that it |
| 2711 | * not good. |
| 2712 | */ |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2713 | if (HZ > USER_HZ) |
| 2714 | rt_expires = addrconf_timeout_fixup(valid_lft, HZ); |
| 2715 | else |
| 2716 | rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); |
YOSHIFUJI Hideaki | 3dd4bc6 | 2005-12-19 14:02:45 -0800 | [diff] [blame] | 2717 | |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2718 | if (addrconf_finite_timeout(rt_expires)) |
| 2719 | rt_expires *= HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2721 | rt = addrconf_get_prefix_route(&pinfo->prefix, |
| 2722 | pinfo->prefix_len, |
| 2723 | dev, |
| 2724 | RTF_ADDRCONF | RTF_PREFIX_RT, |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 2725 | RTF_DEFAULT, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | |
Andreas Hofmeister | 14ef37b | 2011-10-26 03:24:29 +0000 | [diff] [blame] | 2727 | if (rt) { |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2728 | /* Autoconf prefix route */ |
| 2729 | if (valid_lft == 0) { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 2730 | ip6_del_rt(net, rt); |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2731 | rt = NULL; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2732 | } else if (addrconf_finite_timeout(rt_expires)) { |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2733 | /* not infinity */ |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 2734 | fib6_set_expires(rt, jiffies + rt_expires); |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2735 | } else { |
David Ahern | 1489568 | 2018-04-17 17:33:17 -0700 | [diff] [blame] | 2736 | fib6_clean_expires(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | } |
| 2738 | } else if (valid_lft) { |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2739 | clock_t expires = 0; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2740 | int flags = RTF_ADDRCONF | RTF_PREFIX_RT; |
| 2741 | if (addrconf_finite_timeout(rt_expires)) { |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2742 | /* not infinity */ |
| 2743 | flags |= RTF_EXPIRES; |
| 2744 | expires = jiffies_to_clock_t(rt_expires); |
| 2745 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 2747 | 0, dev, expires, flags, |
| 2748 | GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | } |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 2750 | fib6_info_release(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | /* Try to figure out our local address for this prefix */ |
| 2754 | |
| 2755 | if (pinfo->autoconf && in6_dev->cnf.autoconf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | struct in6_addr addr; |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 2757 | bool tokenized = false, dev_addr_generated = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | |
| 2759 | if (pinfo->prefix_len == 64) { |
| 2760 | memcpy(&addr, &pinfo->prefix, 8); |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 2761 | |
| 2762 | if (!ipv6_addr_any(&in6_dev->token)) { |
| 2763 | read_lock_bh(&in6_dev->lock); |
| 2764 | memcpy(addr.s6_addr + 8, |
| 2765 | in6_dev->token.s6_addr + 8, 8); |
| 2766 | read_unlock_bh(&in6_dev->lock); |
Daniel Borkmann | 617fe29 | 2013-04-09 03:47:16 +0000 | [diff] [blame] | 2767 | tokenized = true; |
Bjørn Mork | cc9da6c | 2015-12-16 16:44:38 +0100 | [diff] [blame] | 2768 | } else if (is_addr_mode_generate_stable(in6_dev) && |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 2769 | !ipv6_generate_stable_address(&addr, 0, |
| 2770 | in6_dev)) { |
Hannes Frederic Sowa | 64236f3 | 2015-03-23 23:36:02 +0100 | [diff] [blame] | 2771 | addr_flags |= IFA_F_STABLE_PRIVACY; |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 2772 | goto ok; |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 2773 | } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && |
| 2774 | ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { |
Alexander Aring | 4f67223 | 2016-06-15 21:20:22 +0200 | [diff] [blame] | 2775 | goto put; |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 2776 | } else { |
| 2777 | dev_addr_generated = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | } |
| 2779 | goto ok; |
| 2780 | } |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 2781 | net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", |
| 2782 | pinfo->prefix_len); |
Alexander Aring | 4f67223 | 2016-06-15 21:20:22 +0200 | [diff] [blame] | 2783 | goto put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | |
| 2785 | ok: |
Alexander Aring | 4f67223 | 2016-06-15 21:20:22 +0200 | [diff] [blame] | 2786 | err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, |
| 2787 | &addr, addr_type, |
| 2788 | addr_flags, sllao, |
| 2789 | tokenized, valid_lft, |
| 2790 | prefered_lft); |
| 2791 | if (err) |
| 2792 | goto put; |
Alexander Aring | f997c55 | 2016-06-15 21:20:23 +0200 | [diff] [blame] | 2793 | |
| 2794 | /* Ignore error case here because previous prefix add addr was |
| 2795 | * successful which will be notified. |
| 2796 | */ |
| 2797 | ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr, |
| 2798 | addr_type, addr_flags, sllao, |
| 2799 | tokenized, valid_lft, |
| 2800 | prefered_lft, |
| 2801 | dev_addr_generated); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | } |
| 2803 | inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); |
Alexander Aring | 4f67223 | 2016-06-15 21:20:22 +0200 | [diff] [blame] | 2804 | put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | in6_dev_put(in6_dev); |
| 2806 | } |
| 2807 | |
| 2808 | /* |
| 2809 | * Set destination address. |
| 2810 | * Special case for SIT interfaces where we create a new "virtual" |
| 2811 | * device. |
| 2812 | */ |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2813 | int addrconf_set_dstaddr(struct net *net, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | { |
| 2815 | struct in6_ifreq ireq; |
| 2816 | struct net_device *dev; |
| 2817 | int err = -EINVAL; |
| 2818 | |
| 2819 | rtnl_lock(); |
| 2820 | |
| 2821 | err = -EFAULT; |
| 2822 | if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) |
| 2823 | goto err_exit; |
| 2824 | |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2825 | dev = __dev_get_by_index(net, ireq.ifr6_ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | |
| 2827 | err = -ENODEV; |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 2828 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | goto err_exit; |
| 2830 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 2831 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | if (dev->type == ARPHRD_SIT) { |
Stephen Hemminger | 5bc3eb7 | 2008-11-19 21:52:05 -0800 | [diff] [blame] | 2833 | const struct net_device_ops *ops = dev->netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | struct ifreq ifr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | struct ip_tunnel_parm p; |
| 2836 | |
| 2837 | err = -EADDRNOTAVAIL; |
| 2838 | if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) |
| 2839 | goto err_exit; |
| 2840 | |
| 2841 | memset(&p, 0, sizeof(p)); |
| 2842 | p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; |
| 2843 | p.iph.saddr = 0; |
| 2844 | p.iph.version = 4; |
| 2845 | p.iph.ihl = 5; |
| 2846 | p.iph.protocol = IPPROTO_IPV6; |
| 2847 | p.iph.ttl = 64; |
Stephen Hemminger | d20b310 | 2008-01-21 00:48:43 -0800 | [diff] [blame] | 2848 | ifr.ifr_ifru.ifru_data = (__force void __user *)&p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | |
Stephen Hemminger | 5bc3eb7 | 2008-11-19 21:52:05 -0800 | [diff] [blame] | 2850 | if (ops->ndo_do_ioctl) { |
| 2851 | mm_segment_t oldfs = get_fs(); |
| 2852 | |
| 2853 | set_fs(KERNEL_DS); |
| 2854 | err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); |
| 2855 | set_fs(oldfs); |
| 2856 | } else |
| 2857 | err = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | |
| 2859 | if (err == 0) { |
| 2860 | err = -ENOBUFS; |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2861 | dev = __dev_get_by_name(net, p.name); |
| 2862 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | goto err_exit; |
Petr Machata | 00f54e6 | 2018-12-06 17:05:36 +0000 | [diff] [blame] | 2864 | err = dev_open(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | } |
| 2866 | } |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 2867 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | |
| 2869 | err_exit: |
| 2870 | rtnl_unlock(); |
| 2871 | return err; |
| 2872 | } |
| 2873 | |
Madhu Challa | 93a714d | 2015-02-25 09:58:35 -0800 | [diff] [blame] | 2874 | static int ipv6_mc_config(struct sock *sk, bool join, |
| 2875 | const struct in6_addr *addr, int ifindex) |
| 2876 | { |
| 2877 | int ret; |
| 2878 | |
| 2879 | ASSERT_RTNL(); |
| 2880 | |
| 2881 | lock_sock(sk); |
| 2882 | if (join) |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 2883 | ret = ipv6_sock_mc_join(sk, ifindex, addr); |
Madhu Challa | 93a714d | 2015-02-25 09:58:35 -0800 | [diff] [blame] | 2884 | else |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 2885 | ret = ipv6_sock_mc_drop(sk, ifindex, addr); |
Madhu Challa | 93a714d | 2015-02-25 09:58:35 -0800 | [diff] [blame] | 2886 | release_sock(sk); |
| 2887 | |
| 2888 | return ret; |
| 2889 | } |
| 2890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | /* |
| 2892 | * Manual configuration of address on an interface |
| 2893 | */ |
Jiri Pirko | 479840f | 2013-12-06 09:45:21 +0100 | [diff] [blame] | 2894 | static int inet6_addr_add(struct net *net, int ifindex, |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2895 | struct ifa6_config *cfg, |
David Ahern | de95e04 | 2017-10-18 09:56:54 -0700 | [diff] [blame] | 2896 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2897 | { |
| 2898 | struct inet6_ifaddr *ifp; |
| 2899 | struct inet6_dev *idev; |
| 2900 | struct net_device *dev; |
Madhu Challa | 93a714d | 2015-02-25 09:58:35 -0800 | [diff] [blame] | 2901 | unsigned long timeout; |
| 2902 | clock_t expires; |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2903 | u32 flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | |
| 2905 | ASSERT_RTNL(); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2906 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2907 | if (cfg->plen > 128) |
Thomas Graf | 24ef0da | 2008-05-28 16:54:22 +0200 | [diff] [blame] | 2908 | return -EINVAL; |
| 2909 | |
Noriaki TAKAMIYA | 0778769d | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 2910 | /* check the lifetime */ |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2911 | if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) |
Noriaki TAKAMIYA | 0778769d | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 2912 | return -EINVAL; |
| 2913 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2914 | if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 2915 | return -EINVAL; |
| 2916 | |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2917 | dev = __dev_get_by_index(net, ifindex); |
| 2918 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | return -ENODEV; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2920 | |
Brian Haley | 64e724f | 2010-07-20 10:34:30 +0000 | [diff] [blame] | 2921 | idev = addrconf_add_dev(dev); |
| 2922 | if (IS_ERR(idev)) |
| 2923 | return PTR_ERR(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2925 | if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { |
Madhu Challa | 93a714d | 2015-02-25 09:58:35 -0800 | [diff] [blame] | 2926 | int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2927 | true, cfg->pfx, ifindex); |
Madhu Challa | 93a714d | 2015-02-25 09:58:35 -0800 | [diff] [blame] | 2928 | |
| 2929 | if (ret < 0) |
| 2930 | return ret; |
| 2931 | } |
| 2932 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2933 | cfg->scope = ipv6_addr_scope(cfg->pfx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2934 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2935 | timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2936 | if (addrconf_finite_timeout(timeout)) { |
| 2937 | expires = jiffies_to_clock_t(timeout * HZ); |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2938 | cfg->valid_lft = timeout; |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2939 | flags = RTF_EXPIRES; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2940 | } else { |
| 2941 | expires = 0; |
| 2942 | flags = 0; |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2943 | cfg->ifa_flags |= IFA_F_PERMANENT; |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 2944 | } |
Noriaki TAKAMIYA | 0778769d | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 2945 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2946 | timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2947 | if (addrconf_finite_timeout(timeout)) { |
| 2948 | if (timeout == 0) |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2949 | cfg->ifa_flags |= IFA_F_DEPRECATED; |
| 2950 | cfg->preferred_lft = timeout; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 2951 | } |
Noriaki TAKAMIYA | 0778769d | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 2952 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2953 | ifp = ipv6_add_addr(idev, cfg, true, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | if (!IS_ERR(ifp)) { |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2955 | if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 2956 | addrconf_prefix_route(&ifp->addr, ifp->prefix_len, |
| 2957 | ifp->rt_priority, dev, expires, |
| 2958 | flags, GFP_KERNEL); |
Thomas Haller | 761aac7 | 2014-01-15 15:36:58 +0100 | [diff] [blame] | 2959 | } |
| 2960 | |
Lorenzo Bianconi | a2d481b | 2018-04-17 11:54:39 +0200 | [diff] [blame] | 2961 | /* Send a netlink notification if DAD is enabled and |
| 2962 | * optimistic flag is not set |
| 2963 | */ |
| 2964 | if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD))) |
| 2965 | ipv6_ifa_notify(0, ifp); |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 2966 | /* |
| 2967 | * Note that section 3.1 of RFC 4429 indicates |
| 2968 | * that the Optimistic flag should not be set for |
| 2969 | * manually configured addresses |
| 2970 | */ |
David S. Miller | cf22f9a | 2012-04-14 21:37:40 -0400 | [diff] [blame] | 2971 | addrconf_dad_start(ifp); |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2972 | if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR) |
| 2973 | manage_tempaddrs(idev, ifp, cfg->valid_lft, |
| 2974 | cfg->preferred_lft, true, jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | in6_ifa_put(ifp); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 2976 | addrconf_verify_rtnl(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | return 0; |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 2978 | } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { |
| 2979 | ipv6_mc_config(net->ipv6.mc_autojoin_sk, false, |
| 2980 | cfg->pfx, ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2981 | } |
| 2982 | |
| 2983 | return PTR_ERR(ifp); |
| 2984 | } |
| 2985 | |
Heiner Kallweit | 6046d5b | 2014-04-20 21:29:36 +0200 | [diff] [blame] | 2986 | static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, |
| 2987 | const struct in6_addr *pfx, unsigned int plen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | { |
| 2989 | struct inet6_ifaddr *ifp; |
| 2990 | struct inet6_dev *idev; |
| 2991 | struct net_device *dev; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2992 | |
Thomas Graf | 24ef0da | 2008-05-28 16:54:22 +0200 | [diff] [blame] | 2993 | if (plen > 128) |
| 2994 | return -EINVAL; |
| 2995 | |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 2996 | dev = __dev_get_by_index(net, ifindex); |
| 2997 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | return -ENODEV; |
| 2999 | |
Ian Morris | e5d08d7 | 2014-11-23 21:28:43 +0000 | [diff] [blame] | 3000 | idev = __in6_dev_get(dev); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 3001 | if (!idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | return -ENXIO; |
| 3003 | |
| 3004 | read_lock_bh(&idev->lock); |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 3005 | list_for_each_entry(ifp, &idev->addr_list, if_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | if (ifp->prefix_len == plen && |
| 3007 | ipv6_addr_equal(pfx, &ifp->addr)) { |
| 3008 | in6_ifa_hold(ifp); |
| 3009 | read_unlock_bh(&idev->lock); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3010 | |
Heiner Kallweit | 6046d5b | 2014-04-20 21:29:36 +0200 | [diff] [blame] | 3011 | if (!(ifp->flags & IFA_F_TEMPORARY) && |
| 3012 | (ifa_flags & IFA_F_MANAGETEMPADDR)) |
| 3013 | manage_tempaddrs(idev, ifp, 0, 0, false, |
| 3014 | jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3015 | ipv6_del_addr(ifp); |
Heiner Kallweit | 6046d5b | 2014-04-20 21:29:36 +0200 | [diff] [blame] | 3016 | addrconf_verify_rtnl(); |
Madhu Challa | 93a714d | 2015-02-25 09:58:35 -0800 | [diff] [blame] | 3017 | if (ipv6_addr_is_multicast(pfx)) { |
| 3018 | ipv6_mc_config(net->ipv6.mc_autojoin_sk, |
| 3019 | false, pfx, dev->ifindex); |
| 3020 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | return 0; |
| 3022 | } |
| 3023 | } |
| 3024 | read_unlock_bh(&idev->lock); |
| 3025 | return -EADDRNOTAVAIL; |
| 3026 | } |
| 3027 | |
| 3028 | |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 3029 | int addrconf_add_ifaddr(struct net *net, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | { |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 3031 | struct ifa6_config cfg = { |
| 3032 | .ifa_flags = IFA_F_PERMANENT, |
| 3033 | .preferred_lft = INFINITY_LIFE_TIME, |
| 3034 | .valid_lft = INFINITY_LIFE_TIME, |
| 3035 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | struct in6_ifreq ireq; |
| 3037 | int err; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3038 | |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 3039 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | return -EPERM; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) |
| 3043 | return -EFAULT; |
| 3044 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 3045 | cfg.pfx = &ireq.ifr6_addr; |
| 3046 | cfg.plen = ireq.ifr6_prefixlen; |
| 3047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | rtnl_lock(); |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 3049 | err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3050 | rtnl_unlock(); |
| 3051 | return err; |
| 3052 | } |
| 3053 | |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 3054 | int addrconf_del_ifaddr(struct net *net, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | { |
| 3056 | struct in6_ifreq ireq; |
| 3057 | int err; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3058 | |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 3059 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | return -EPERM; |
| 3061 | |
| 3062 | if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) |
| 3063 | return -EFAULT; |
| 3064 | |
| 3065 | rtnl_lock(); |
Heiner Kallweit | 6046d5b | 2014-04-20 21:29:36 +0200 | [diff] [blame] | 3066 | err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 3067 | ireq.ifr6_prefixlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | rtnl_unlock(); |
| 3069 | return err; |
| 3070 | } |
| 3071 | |
Ilpo Järvinen | b5f348e | 2009-02-06 23:48:01 -0800 | [diff] [blame] | 3072 | static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, |
| 3073 | int plen, int scope) |
| 3074 | { |
| 3075 | struct inet6_ifaddr *ifp; |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 3076 | struct ifa6_config cfg = { |
| 3077 | .pfx = addr, |
| 3078 | .plen = plen, |
| 3079 | .ifa_flags = IFA_F_PERMANENT, |
| 3080 | .valid_lft = INFINITY_LIFE_TIME, |
| 3081 | .preferred_lft = INFINITY_LIFE_TIME, |
| 3082 | .scope = scope |
| 3083 | }; |
Ilpo Järvinen | b5f348e | 2009-02-06 23:48:01 -0800 | [diff] [blame] | 3084 | |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 3085 | ifp = ipv6_add_addr(idev, &cfg, true, NULL); |
Ilpo Järvinen | b5f348e | 2009-02-06 23:48:01 -0800 | [diff] [blame] | 3086 | if (!IS_ERR(ifp)) { |
| 3087 | spin_lock_bh(&ifp->lock); |
| 3088 | ifp->flags &= ~IFA_F_TENTATIVE; |
| 3089 | spin_unlock_bh(&ifp->lock); |
Paolo Abeni | 764d3be | 2016-11-22 16:57:40 +0100 | [diff] [blame] | 3090 | rt_genid_bump_ipv6(dev_net(idev->dev)); |
Ilpo Järvinen | b5f348e | 2009-02-06 23:48:01 -0800 | [diff] [blame] | 3091 | ipv6_ifa_notify(RTM_NEWADDR, ifp); |
| 3092 | in6_ifa_put(ifp); |
| 3093 | } |
| 3094 | } |
| 3095 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 3096 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | static void sit_add_v4_addrs(struct inet6_dev *idev) |
| 3098 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | struct in6_addr addr; |
| 3100 | struct net_device *dev; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 3101 | struct net *net = dev_net(idev->dev); |
Nicolas Dichtel | 929c9cf | 2013-11-14 13:51:05 +0100 | [diff] [blame] | 3102 | int scope, plen; |
Nicolas Dichtel | f0e2acf | 2013-11-14 13:51:06 +0100 | [diff] [blame] | 3103 | u32 pflags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | |
| 3105 | ASSERT_RTNL(); |
| 3106 | |
| 3107 | memset(&addr, 0, sizeof(struct in6_addr)); |
| 3108 | memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); |
| 3109 | |
| 3110 | if (idev->dev->flags&IFF_POINTOPOINT) { |
| 3111 | addr.s6_addr32[0] = htonl(0xfe800000); |
| 3112 | scope = IFA_LINK; |
Nicolas Dichtel | 929c9cf | 2013-11-14 13:51:05 +0100 | [diff] [blame] | 3113 | plen = 64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | } else { |
| 3115 | scope = IPV6_ADDR_COMPATv4; |
Nicolas Dichtel | 929c9cf | 2013-11-14 13:51:05 +0100 | [diff] [blame] | 3116 | plen = 96; |
Nicolas Dichtel | f0e2acf | 2013-11-14 13:51:06 +0100 | [diff] [blame] | 3117 | pflags |= RTF_NONEXTHOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | if (addr.s6_addr32[3]) { |
Nicolas Dichtel | 929c9cf | 2013-11-14 13:51:05 +0100 | [diff] [blame] | 3121 | add_addr(idev, &addr, plen, scope); |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 3122 | addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags, |
Cong Wang | e500c6d | 2018-08-22 12:58:34 -0700 | [diff] [blame] | 3123 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3124 | return; |
| 3125 | } |
| 3126 | |
Benjamin Thery | 6fda735 | 2008-03-05 10:47:47 -0800 | [diff] [blame] | 3127 | for_each_netdev(net, dev) { |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 3128 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | if (in_dev && (dev->flags & IFF_UP)) { |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 3130 | struct in_ifaddr *ifa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | int flag = scope; |
| 3132 | |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 3133 | in_dev_for_each_ifa_rtnl(ifa, in_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | addr.s6_addr32[3] = ifa->ifa_local; |
| 3135 | |
| 3136 | if (ifa->ifa_scope == RT_SCOPE_LINK) |
| 3137 | continue; |
| 3138 | if (ifa->ifa_scope >= RT_SCOPE_HOST) { |
| 3139 | if (idev->dev->flags&IFF_POINTOPOINT) |
| 3140 | continue; |
| 3141 | flag |= IFA_HOST; |
| 3142 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | |
Ilpo Järvinen | b5f348e | 2009-02-06 23:48:01 -0800 | [diff] [blame] | 3144 | add_addr(idev, &addr, plen, flag); |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 3145 | addrconf_prefix_route(&addr, plen, 0, idev->dev, |
Cong Wang | e500c6d | 2018-08-22 12:58:34 -0700 | [diff] [blame] | 3146 | 0, pflags, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | } |
| 3148 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | } |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 3151 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | |
| 3153 | static void init_loopback(struct net_device *dev) |
| 3154 | { |
| 3155 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | |
| 3157 | /* ::1 */ |
| 3158 | |
| 3159 | ASSERT_RTNL(); |
| 3160 | |
Ian Morris | e5d08d7 | 2014-11-23 21:28:43 +0000 | [diff] [blame] | 3161 | idev = ipv6_find_idev(dev); |
Sabrina Dubroca | db0b99f | 2019-08-23 15:44:36 +0200 | [diff] [blame^] | 3162 | if (IS_ERR(idev)) { |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 3163 | pr_debug("%s: add_dev failed\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | return; |
| 3165 | } |
| 3166 | |
Ilpo Järvinen | b5f348e | 2009-02-06 23:48:01 -0800 | [diff] [blame] | 3167 | add_addr(idev, &in6addr_loopback, 128, IFA_HOST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | } |
| 3169 | |
Alexander Aring | 2ad3ed5 | 2016-06-15 21:20:17 +0200 | [diff] [blame] | 3170 | void addrconf_add_linklocal(struct inet6_dev *idev, |
| 3171 | const struct in6_addr *addr, u32 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | { |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 3173 | struct ifa6_config cfg = { |
| 3174 | .pfx = addr, |
| 3175 | .plen = 64, |
| 3176 | .ifa_flags = flags | IFA_F_PERMANENT, |
| 3177 | .valid_lft = INFINITY_LIFE_TIME, |
| 3178 | .preferred_lft = INFINITY_LIFE_TIME, |
| 3179 | .scope = IFA_LINK |
| 3180 | }; |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 3181 | struct inet6_ifaddr *ifp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3182 | |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 3183 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
Matteo Croce | 35e015e | 2017-09-12 17:46:37 +0200 | [diff] [blame] | 3184 | if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad || |
| 3185 | idev->cnf.optimistic_dad) && |
David Miller | 702beb8 | 2008-07-20 18:17:02 -0700 | [diff] [blame] | 3186 | !dev_net(idev->dev)->ipv6.devconf_all->forwarding) |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 3187 | cfg.ifa_flags |= IFA_F_OPTIMISTIC; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 3188 | #endif |
| 3189 | |
David Ahern | e6464b8 | 2018-05-27 08:09:53 -0700 | [diff] [blame] | 3190 | ifp = ipv6_add_addr(idev, &cfg, true, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | if (!IS_ERR(ifp)) { |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 3192 | addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev, |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3193 | 0, 0, GFP_ATOMIC); |
David S. Miller | cf22f9a | 2012-04-14 21:37:40 -0400 | [diff] [blame] | 3194 | addrconf_dad_start(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | in6_ifa_put(ifp); |
| 3196 | } |
| 3197 | } |
Alexander Aring | 2ad3ed5 | 2016-06-15 21:20:17 +0200 | [diff] [blame] | 3198 | EXPORT_SYMBOL_GPL(addrconf_add_linklocal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 3200 | static bool ipv6_reserved_interfaceid(struct in6_addr address) |
| 3201 | { |
| 3202 | if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0) |
| 3203 | return true; |
| 3204 | |
| 3205 | if (address.s6_addr32[2] == htonl(0x02005eff) && |
| 3206 | ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000))) |
| 3207 | return true; |
| 3208 | |
| 3209 | if (address.s6_addr32[2] == htonl(0xfdffffff) && |
| 3210 | ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80))) |
| 3211 | return true; |
| 3212 | |
| 3213 | return false; |
| 3214 | } |
| 3215 | |
| 3216 | static int ipv6_generate_stable_address(struct in6_addr *address, |
| 3217 | u8 dad_count, |
| 3218 | const struct inet6_dev *idev) |
| 3219 | { |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 3220 | static DEFINE_SPINLOCK(lock); |
| 3221 | static __u32 digest[SHA_DIGEST_WORDS]; |
| 3222 | static __u32 workspace[SHA_WORKSPACE_WORDS]; |
| 3223 | |
| 3224 | static union { |
| 3225 | char __data[SHA_MESSAGE_BYTES]; |
| 3226 | struct { |
| 3227 | struct in6_addr secret; |
Hannes Frederic Sowa | ff40217 | 2015-03-24 11:05:28 +0100 | [diff] [blame] | 3228 | __be32 prefix[2]; |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 3229 | unsigned char hwaddr[MAX_ADDR_LEN]; |
| 3230 | u8 dad_count; |
| 3231 | } __packed; |
| 3232 | } data; |
| 3233 | |
| 3234 | struct in6_addr secret; |
| 3235 | struct in6_addr temp; |
| 3236 | struct net *net = dev_net(idev->dev); |
| 3237 | |
| 3238 | BUILD_BUG_ON(sizeof(data.__data) != sizeof(data)); |
| 3239 | |
| 3240 | if (idev->cnf.stable_secret.initialized) |
| 3241 | secret = idev->cnf.stable_secret.secret; |
| 3242 | else if (net->ipv6.devconf_dflt->stable_secret.initialized) |
| 3243 | secret = net->ipv6.devconf_dflt->stable_secret.secret; |
| 3244 | else |
| 3245 | return -1; |
| 3246 | |
| 3247 | retry: |
| 3248 | spin_lock_bh(&lock); |
| 3249 | |
| 3250 | sha_init(digest); |
| 3251 | memset(&data, 0, sizeof(data)); |
| 3252 | memset(workspace, 0, sizeof(workspace)); |
| 3253 | memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len); |
Hannes Frederic Sowa | ff40217 | 2015-03-24 11:05:28 +0100 | [diff] [blame] | 3254 | data.prefix[0] = address->s6_addr32[0]; |
| 3255 | data.prefix[1] = address->s6_addr32[1]; |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 3256 | data.secret = secret; |
| 3257 | data.dad_count = dad_count; |
| 3258 | |
| 3259 | sha_transform(digest, data.__data, workspace); |
| 3260 | |
| 3261 | temp = *address; |
Hannes Frederic Sowa | ff40217 | 2015-03-24 11:05:28 +0100 | [diff] [blame] | 3262 | temp.s6_addr32[2] = (__force __be32)digest[0]; |
| 3263 | temp.s6_addr32[3] = (__force __be32)digest[1]; |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 3264 | |
| 3265 | spin_unlock_bh(&lock); |
| 3266 | |
| 3267 | if (ipv6_reserved_interfaceid(temp)) { |
| 3268 | dad_count++; |
Hannes Frederic Sowa | 1855b7c | 2015-03-23 23:36:05 +0100 | [diff] [blame] | 3269 | if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries) |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 3270 | return -1; |
| 3271 | goto retry; |
| 3272 | } |
| 3273 | |
| 3274 | *address = temp; |
| 3275 | return 0; |
| 3276 | } |
| 3277 | |
Bjørn Mork | cc9da6c | 2015-12-16 16:44:38 +0100 | [diff] [blame] | 3278 | static void ipv6_gen_mode_random_init(struct inet6_dev *idev) |
| 3279 | { |
| 3280 | struct ipv6_stable_secret *s = &idev->cnf.stable_secret; |
| 3281 | |
| 3282 | if (s->initialized) |
| 3283 | return; |
| 3284 | s = &idev->cnf.stable_secret; |
| 3285 | get_random_bytes(&s->secret, sizeof(s->secret)); |
| 3286 | s->initialized = true; |
| 3287 | } |
| 3288 | |
Jiri Pirko | bc91b0f | 2014-07-11 21:10:18 +0200 | [diff] [blame] | 3289 | static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) |
| 3290 | { |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 3291 | struct in6_addr addr; |
Jiri Pirko | bc91b0f | 2014-07-11 21:10:18 +0200 | [diff] [blame] | 3292 | |
David Ahern | ca25449 | 2015-10-12 11:47:10 -0700 | [diff] [blame] | 3293 | /* no link local addresses on L3 master devices */ |
| 3294 | if (netif_is_l3_master(idev->dev)) |
| 3295 | return; |
| 3296 | |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 3297 | ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); |
| 3298 | |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 3299 | switch (idev->cnf.addr_gen_mode) { |
Bjørn Mork | cc9da6c | 2015-12-16 16:44:38 +0100 | [diff] [blame] | 3300 | case IN6_ADDR_GEN_MODE_RANDOM: |
| 3301 | ipv6_gen_mode_random_init(idev); |
| 3302 | /* fallthrough */ |
| 3303 | case IN6_ADDR_GEN_MODE_STABLE_PRIVACY: |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 3304 | if (!ipv6_generate_stable_address(&addr, 0, idev)) |
Hannes Frederic Sowa | 64236f3 | 2015-03-23 23:36:02 +0100 | [diff] [blame] | 3305 | addrconf_add_linklocal(idev, &addr, |
| 3306 | IFA_F_STABLE_PRIVACY); |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 3307 | else if (prefix_route) |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 3308 | addrconf_prefix_route(&addr, 64, 0, idev->dev, |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 3309 | 0, 0, GFP_KERNEL); |
Bjørn Mork | cc9da6c | 2015-12-16 16:44:38 +0100 | [diff] [blame] | 3310 | break; |
| 3311 | case IN6_ADDR_GEN_MODE_EUI64: |
Jiri Pirko | bc91b0f | 2014-07-11 21:10:18 +0200 | [diff] [blame] | 3312 | /* addrconf_add_linklocal also adds a prefix_route and we |
| 3313 | * only need to care about prefix routes if ipv6_generate_eui64 |
| 3314 | * couldn't generate one. |
| 3315 | */ |
| 3316 | if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) |
Hannes Frederic Sowa | 64236f3 | 2015-03-23 23:36:02 +0100 | [diff] [blame] | 3317 | addrconf_add_linklocal(idev, &addr, 0); |
Jiri Pirko | bc91b0f | 2014-07-11 21:10:18 +0200 | [diff] [blame] | 3318 | else if (prefix_route) |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 3319 | addrconf_prefix_route(&addr, 64, 0, idev->dev, |
David Ahern | 27b1060 | 2018-04-18 15:39:06 -0700 | [diff] [blame] | 3320 | 0, 0, GFP_KERNEL); |
Bjørn Mork | cc9da6c | 2015-12-16 16:44:38 +0100 | [diff] [blame] | 3321 | break; |
| 3322 | case IN6_ADDR_GEN_MODE_NONE: |
| 3323 | default: |
| 3324 | /* will not add any link local address */ |
| 3325 | break; |
Jiri Pirko | bc91b0f | 2014-07-11 21:10:18 +0200 | [diff] [blame] | 3326 | } |
| 3327 | } |
| 3328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | static void addrconf_dev_config(struct net_device *dev) |
| 3330 | { |
Eldad Zack | 8e5e8f3 | 2012-04-01 07:49:08 +0000 | [diff] [blame] | 3331 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | |
| 3333 | ASSERT_RTNL(); |
| 3334 | |
Herbert Xu | 74235a2 | 2007-06-14 13:02:55 -0700 | [diff] [blame] | 3335 | if ((dev->type != ARPHRD_ETHER) && |
| 3336 | (dev->type != ARPHRD_FDDI) && |
Herbert Xu | 74235a2 | 2007-06-14 13:02:55 -0700 | [diff] [blame] | 3337 | (dev->type != ARPHRD_ARCNET) && |
alex.bluesman.smirnov@gmail.com | 06a4c1c | 2012-05-10 03:25:52 +0000 | [diff] [blame] | 3338 | (dev->type != ARPHRD_INFINIBAND) && |
Nicolas Dichtel | e837735 | 2013-08-20 12:16:06 +0200 | [diff] [blame] | 3339 | (dev->type != ARPHRD_IEEE1394) && |
Jukka Rissanen | e74bccb8 | 2013-12-11 17:05:36 +0200 | [diff] [blame] | 3340 | (dev->type != ARPHRD_TUNNEL6) && |
Bjørn Mork | cc9da6c | 2015-12-16 16:44:38 +0100 | [diff] [blame] | 3341 | (dev->type != ARPHRD_6LOWPAN) && |
Felix Jia | 45ce0fd | 2017-01-26 16:59:18 +1300 | [diff] [blame] | 3342 | (dev->type != ARPHRD_IP6GRE) && |
| 3343 | (dev->type != ARPHRD_IPGRE) && |
| 3344 | (dev->type != ARPHRD_TUNNEL) && |
Subash Abhinov Kasiviswanathan | 9deb441 | 2018-06-04 19:26:07 -0600 | [diff] [blame] | 3345 | (dev->type != ARPHRD_NONE) && |
| 3346 | (dev->type != ARPHRD_RAWIP)) { |
Herbert Xu | 74235a2 | 2007-06-14 13:02:55 -0700 | [diff] [blame] | 3347 | /* Alas, we support only Ethernet autoconfiguration. */ |
| 3348 | return; |
| 3349 | } |
| 3350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3351 | idev = addrconf_add_dev(dev); |
Brian Haley | 64e724f | 2010-07-20 10:34:30 +0000 | [diff] [blame] | 3352 | if (IS_ERR(idev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3353 | return; |
| 3354 | |
Bjørn Mork | cc9da6c | 2015-12-16 16:44:38 +0100 | [diff] [blame] | 3355 | /* this device type has no EUI support */ |
| 3356 | if (dev->type == ARPHRD_NONE && |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 3357 | idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) |
| 3358 | idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM; |
Bjørn Mork | cc9da6c | 2015-12-16 16:44:38 +0100 | [diff] [blame] | 3359 | |
Jiri Pirko | bc91b0f | 2014-07-11 21:10:18 +0200 | [diff] [blame] | 3360 | addrconf_addr_gen(idev, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | } |
| 3362 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 3363 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | static void addrconf_sit_config(struct net_device *dev) |
| 3365 | { |
| 3366 | struct inet6_dev *idev; |
| 3367 | |
| 3368 | ASSERT_RTNL(); |
| 3369 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3370 | /* |
| 3371 | * Configure the tunnel with one of our IPv4 |
| 3372 | * addresses... we should configure all of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | * our v4 addrs in the tunnel |
| 3374 | */ |
| 3375 | |
Ian Morris | e5d08d7 | 2014-11-23 21:28:43 +0000 | [diff] [blame] | 3376 | idev = ipv6_find_idev(dev); |
Sabrina Dubroca | db0b99f | 2019-08-23 15:44:36 +0200 | [diff] [blame^] | 3377 | if (IS_ERR(idev)) { |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 3378 | pr_debug("%s: add_dev failed\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | return; |
| 3380 | } |
| 3381 | |
Fred L. Templin | c7dc89c | 2007-11-29 22:11:40 +1100 | [diff] [blame] | 3382 | if (dev->priv_flags & IFF_ISATAP) { |
Jiri Pirko | bc91b0f | 2014-07-11 21:10:18 +0200 | [diff] [blame] | 3383 | addrconf_addr_gen(idev, false); |
Fred L. Templin | c7dc89c | 2007-11-29 22:11:40 +1100 | [diff] [blame] | 3384 | return; |
| 3385 | } |
| 3386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3387 | sit_add_v4_addrs(idev); |
| 3388 | |
Nicolas Dichtel | 62b54dd | 2012-10-01 23:19:14 +0000 | [diff] [blame] | 3389 | if (dev->flags&IFF_POINTOPOINT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | addrconf_add_mroute(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3391 | } |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 3392 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 3394 | #if IS_ENABLED(CONFIG_NET_IPGRE) |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 3395 | static void addrconf_gre_config(struct net_device *dev) |
| 3396 | { |
| 3397 | struct inet6_dev *idev; |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 3398 | |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 3399 | ASSERT_RTNL(); |
| 3400 | |
Ian Morris | e5d08d7 | 2014-11-23 21:28:43 +0000 | [diff] [blame] | 3401 | idev = ipv6_find_idev(dev); |
Sabrina Dubroca | db0b99f | 2019-08-23 15:44:36 +0200 | [diff] [blame^] | 3402 | if (IS_ERR(idev)) { |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 3403 | pr_debug("%s: add_dev failed\n", __func__); |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 3404 | return; |
| 3405 | } |
| 3406 | |
Jiri Pirko | bc91b0f | 2014-07-11 21:10:18 +0200 | [diff] [blame] | 3407 | addrconf_addr_gen(idev, true); |
Hannes Frederic Sowa | d9e4ce6 | 2015-10-08 18:19:39 +0200 | [diff] [blame] | 3408 | if (dev->flags & IFF_POINTOPOINT) |
| 3409 | addrconf_add_mroute(dev); |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 3410 | } |
| 3411 | #endif |
| 3412 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3413 | static int fixup_permanent_addr(struct net *net, |
| 3414 | struct inet6_dev *idev, |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3415 | struct inet6_ifaddr *ifp) |
| 3416 | { |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3417 | /* !fib6_node means the host route was removed from the |
David Ahern | 8048ced | 2017-04-25 09:17:29 -0700 | [diff] [blame] | 3418 | * FIB, for example, if 'lo' device is taken down. In that |
| 3419 | * case regenerate the host route. |
| 3420 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 3421 | if (!ifp->rt || !ifp->rt->fib6_node) { |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3422 | struct fib6_info *f6i, *prev; |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3423 | |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3424 | f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false, |
| 3425 | GFP_ATOMIC); |
| 3426 | if (IS_ERR(f6i)) |
| 3427 | return PTR_ERR(f6i); |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3428 | |
David Ahern | 8048ced | 2017-04-25 09:17:29 -0700 | [diff] [blame] | 3429 | /* ifp->rt can be accessed outside of rtnl */ |
| 3430 | spin_lock(&ifp->lock); |
| 3431 | prev = ifp->rt; |
David Ahern | 360a988 | 2018-04-18 15:39:00 -0700 | [diff] [blame] | 3432 | ifp->rt = f6i; |
David Ahern | 8048ced | 2017-04-25 09:17:29 -0700 | [diff] [blame] | 3433 | spin_unlock(&ifp->lock); |
| 3434 | |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 3435 | fib6_info_release(prev); |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) { |
| 3439 | addrconf_prefix_route(&ifp->addr, ifp->prefix_len, |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 3440 | ifp->rt_priority, idev->dev, 0, 0, |
| 3441 | GFP_ATOMIC); |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3442 | } |
| 3443 | |
David Ahern | 6d71713 | 2017-05-02 14:43:44 -0700 | [diff] [blame] | 3444 | if (ifp->state == INET6_IFADDR_STATE_PREDAD) |
| 3445 | addrconf_dad_start(ifp); |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3446 | |
| 3447 | return 0; |
| 3448 | } |
| 3449 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3450 | static void addrconf_permanent_addr(struct net *net, struct net_device *dev) |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3451 | { |
| 3452 | struct inet6_ifaddr *ifp, *tmp; |
| 3453 | struct inet6_dev *idev; |
| 3454 | |
| 3455 | idev = __in6_dev_get(dev); |
| 3456 | if (!idev) |
| 3457 | return; |
| 3458 | |
| 3459 | write_lock_bh(&idev->lock); |
| 3460 | |
| 3461 | list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) { |
| 3462 | if ((ifp->flags & IFA_F_PERMANENT) && |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3463 | fixup_permanent_addr(net, idev, ifp) < 0) { |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3464 | write_unlock_bh(&idev->lock); |
Eric Dumazet | e669b86 | 2017-10-30 22:47:09 -0700 | [diff] [blame] | 3465 | in6_ifa_hold(ifp); |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3466 | ipv6_del_addr(ifp); |
| 3467 | write_lock_bh(&idev->lock); |
| 3468 | |
| 3469 | net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n", |
| 3470 | idev->dev->name, &ifp->addr); |
| 3471 | } |
| 3472 | } |
| 3473 | |
| 3474 | write_unlock_bh(&idev->lock); |
| 3475 | } |
| 3476 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3477 | static int addrconf_notify(struct notifier_block *this, unsigned long event, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 3478 | void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 3480 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Hangbin Liu | 896585d | 2018-11-21 21:52:33 +0800 | [diff] [blame] | 3481 | struct netdev_notifier_change_info *change_info; |
David Ahern | 4f7f34e | 2016-04-07 11:10:41 -0700 | [diff] [blame] | 3482 | struct netdev_notifier_changeupper_info *info; |
Eric Dumazet | 748e2d9 | 2012-08-22 21:50:59 +0000 | [diff] [blame] | 3483 | struct inet6_dev *idev = __in6_dev_get(dev); |
WANG Cong | 60abc0b | 2017-06-21 14:34:58 -0700 | [diff] [blame] | 3484 | struct net *net = dev_net(dev); |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 3485 | int run_pending = 0; |
Herbert Xu | b217d61 | 2007-07-30 17:04:52 -0700 | [diff] [blame] | 3486 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 3488 | switch (event) { |
YOSHIFUJI Hideaki | 45ba9dd | 2007-02-15 02:07:27 +0900 | [diff] [blame] | 3489 | case NETDEV_REGISTER: |
Herbert Xu | 74235a2 | 2007-06-14 13:02:55 -0700 | [diff] [blame] | 3490 | if (!idev && dev->mtu >= IPV6_MIN_MTU) { |
YOSHIFUJI Hideaki | 45ba9dd | 2007-02-15 02:07:27 +0900 | [diff] [blame] | 3491 | idev = ipv6_add_dev(dev); |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 3492 | if (IS_ERR(idev)) |
| 3493 | return notifier_from_errno(PTR_ERR(idev)); |
YOSHIFUJI Hideaki | 45ba9dd | 2007-02-15 02:07:27 +0900 | [diff] [blame] | 3494 | } |
| 3495 | break; |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3496 | |
Alexander Duyck | b7b0b1d | 2015-10-26 11:06:33 -0700 | [diff] [blame] | 3497 | case NETDEV_CHANGEMTU: |
| 3498 | /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */ |
| 3499 | if (dev->mtu < IPV6_MIN_MTU) { |
WANG Cong | 60abc0b | 2017-06-21 14:34:58 -0700 | [diff] [blame] | 3500 | addrconf_ifdown(dev, dev != net->loopback_dev); |
Alexander Duyck | b7b0b1d | 2015-10-26 11:06:33 -0700 | [diff] [blame] | 3501 | break; |
| 3502 | } |
| 3503 | |
| 3504 | if (idev) { |
| 3505 | rt6_mtu_change(dev, dev->mtu); |
| 3506 | idev->cnf.mtu6 = dev->mtu; |
| 3507 | break; |
| 3508 | } |
| 3509 | |
| 3510 | /* allocate new idev */ |
| 3511 | idev = ipv6_add_dev(dev); |
| 3512 | if (IS_ERR(idev)) |
| 3513 | break; |
| 3514 | |
| 3515 | /* device is still not ready */ |
| 3516 | if (!(idev->if_flags & IF_READY)) |
| 3517 | break; |
| 3518 | |
| 3519 | run_pending = 1; |
| 3520 | |
| 3521 | /* fall through */ |
| 3522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3523 | case NETDEV_UP: |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3524 | case NETDEV_CHANGE: |
Jay Vosburgh | c2edacf | 2007-07-09 10:42:47 -0700 | [diff] [blame] | 3525 | if (dev->flags & IFF_SLAVE) |
| 3526 | break; |
| 3527 | |
WANG Cong | 3ce62a8 | 2014-09-11 15:07:16 -0700 | [diff] [blame] | 3528 | if (idev && idev->cnf.disable_ipv6) |
| 3529 | break; |
| 3530 | |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3531 | if (event == NETDEV_UP) { |
David Ahern | 38bd10c | 2016-04-21 20:56:12 -0700 | [diff] [blame] | 3532 | /* restore routes for permanent addresses */ |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3533 | addrconf_permanent_addr(net, dev); |
David Ahern | 38bd10c | 2016-04-21 20:56:12 -0700 | [diff] [blame] | 3534 | |
Mike Manning | 1f372c7 | 2017-09-25 22:01:36 +0100 | [diff] [blame] | 3535 | if (!addrconf_link_ready(dev)) { |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3536 | /* device is not ready yet. */ |
Lubomir Rintel | 7c62b8d | 2019-01-21 14:54:20 +0100 | [diff] [blame] | 3537 | pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n", |
| 3538 | dev->name); |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3539 | break; |
| 3540 | } |
Kristian Slavov | 9908104 | 2006-02-08 16:10:53 -0800 | [diff] [blame] | 3541 | |
Evgeniy Polyakov | d31c7b8 | 2007-11-30 23:36:08 +1100 | [diff] [blame] | 3542 | if (!idev && dev->mtu >= IPV6_MIN_MTU) |
| 3543 | idev = ipv6_add_dev(dev); |
| 3544 | |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 3545 | if (!IS_ERR_OR_NULL(idev)) { |
Kristian Slavov | 9908104 | 2006-02-08 16:10:53 -0800 | [diff] [blame] | 3546 | idev->if_flags |= IF_READY; |
Benjamin Thery | e3ec6cf | 2008-11-05 01:43:57 -0800 | [diff] [blame] | 3547 | run_pending = 1; |
| 3548 | } |
Alexander Duyck | b7b0b1d | 2015-10-26 11:06:33 -0700 | [diff] [blame] | 3549 | } else if (event == NETDEV_CHANGE) { |
Mike Manning | 1f372c7 | 2017-09-25 22:01:36 +0100 | [diff] [blame] | 3550 | if (!addrconf_link_ready(dev)) { |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3551 | /* device is still not ready. */ |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3552 | rt6_sync_down_dev(dev, event); |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3553 | break; |
| 3554 | } |
| 3555 | |
Hangbin Liu | 896585d | 2018-11-21 21:52:33 +0800 | [diff] [blame] | 3556 | if (!IS_ERR_OR_NULL(idev)) { |
Linus Lüssing | a088d1d | 2017-02-03 08:11:03 +0100 | [diff] [blame] | 3557 | if (idev->if_flags & IF_READY) { |
| 3558 | /* device is already configured - |
| 3559 | * but resend MLD reports, we might |
| 3560 | * have roamed and need to update |
| 3561 | * multicast snooping switches |
| 3562 | */ |
| 3563 | ipv6_mc_up(idev); |
Hangbin Liu | 896585d | 2018-11-21 21:52:33 +0800 | [diff] [blame] | 3564 | change_info = ptr; |
| 3565 | if (change_info->flags_changed & IFF_NOARP) |
| 3566 | addrconf_dad_run(idev, true); |
Ido Schimmel | 27c6fa7 | 2018-01-07 12:45:05 +0200 | [diff] [blame] | 3567 | rt6_sync_up(dev, RTNH_F_LINKDOWN); |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3568 | break; |
Linus Lüssing | a088d1d | 2017-02-03 08:11:03 +0100 | [diff] [blame] | 3569 | } |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3570 | idev->if_flags |= IF_READY; |
| 3571 | } |
| 3572 | |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 3573 | pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", |
| 3574 | dev->name); |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3575 | |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 3576 | run_pending = 1; |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3577 | } |
| 3578 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 3579 | switch (dev->type) { |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 3580 | #if IS_ENABLED(CONFIG_IPV6_SIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | case ARPHRD_SIT: |
| 3582 | addrconf_sit_config(dev); |
| 3583 | break; |
Joerg Roedel | 0be669b | 2006-10-10 14:49:53 -0700 | [diff] [blame] | 3584 | #endif |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 3585 | #if IS_ENABLED(CONFIG_NET_IPGRE) |
stephen hemminger | aee80b5 | 2011-06-08 10:44:30 +0000 | [diff] [blame] | 3586 | case ARPHRD_IPGRE: |
| 3587 | addrconf_gre_config(dev); |
| 3588 | break; |
| 3589 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3590 | case ARPHRD_LOOPBACK: |
| 3591 | init_loopback(dev); |
| 3592 | break; |
| 3593 | |
| 3594 | default: |
| 3595 | addrconf_dev_config(dev); |
| 3596 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 3597 | } |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3598 | |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 3599 | if (!IS_ERR_OR_NULL(idev)) { |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 3600 | if (run_pending) |
Hangbin Liu | 896585d | 2018-11-21 21:52:33 +0800 | [diff] [blame] | 3601 | addrconf_dad_run(idev, false); |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 3602 | |
Ido Schimmel | 2127d95 | 2018-01-07 12:45:03 +0200 | [diff] [blame] | 3603 | /* Device has an address by now */ |
| 3604 | rt6_sync_up(dev, RTNH_F_DEAD); |
| 3605 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3606 | /* |
| 3607 | * If the MTU changed during the interface down, |
| 3608 | * when the interface up, the changed MTU must be |
| 3609 | * reflected in the idev as well as routers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3610 | */ |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3611 | if (idev->cnf.mtu6 != dev->mtu && |
| 3612 | dev->mtu >= IPV6_MIN_MTU) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | rt6_mtu_change(dev, dev->mtu); |
| 3614 | idev->cnf.mtu6 = dev->mtu; |
| 3615 | } |
| 3616 | idev->tstamp = jiffies; |
| 3617 | inet6_ifinfo_notify(RTM_NEWLINK, idev); |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3618 | |
| 3619 | /* |
| 3620 | * If the changed mtu during down is lower than |
| 3621 | * IPV6_MIN_MTU stop IPv6 on this interface. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3622 | */ |
| 3623 | if (dev->mtu < IPV6_MIN_MTU) |
WANG Cong | 60abc0b | 2017-06-21 14:34:58 -0700 | [diff] [blame] | 3624 | addrconf_ifdown(dev, dev != net->loopback_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3625 | } |
| 3626 | break; |
| 3627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3628 | case NETDEV_DOWN: |
| 3629 | case NETDEV_UNREGISTER: |
| 3630 | /* |
| 3631 | * Remove all addresses from this interface. |
| 3632 | */ |
| 3633 | addrconf_ifdown(dev, event != NETDEV_DOWN); |
| 3634 | break; |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3636 | case NETDEV_CHANGENAME: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3637 | if (idev) { |
Stephen Hemminger | 5632c51 | 2007-04-28 21:16:39 -0700 | [diff] [blame] | 3638 | snmp6_unregister_dev(idev); |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 3639 | addrconf_sysctl_unregister(idev); |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 3640 | err = addrconf_sysctl_register(idev); |
Herbert Xu | b217d61 | 2007-07-30 17:04:52 -0700 | [diff] [blame] | 3641 | if (err) |
| 3642 | return notifier_from_errno(err); |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 3643 | err = snmp6_register_dev(idev); |
| 3644 | if (err) { |
| 3645 | addrconf_sysctl_unregister(idev); |
| 3646 | return notifier_from_errno(err); |
| 3647 | } |
Stephen Hemminger | 5632c51 | 2007-04-28 21:16:39 -0700 | [diff] [blame] | 3648 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | break; |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3650 | |
Jiri Pirko | 93d9b7d | 2010-03-10 10:28:56 +0000 | [diff] [blame] | 3651 | case NETDEV_PRE_TYPE_CHANGE: |
| 3652 | case NETDEV_POST_TYPE_CHANGE: |
Andrew Lunn | 3ef0952 | 2015-12-03 21:12:32 +0100 | [diff] [blame] | 3653 | if (idev) |
| 3654 | addrconf_type_change(dev, event); |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 3655 | break; |
David Ahern | 4f7f34e | 2016-04-07 11:10:41 -0700 | [diff] [blame] | 3656 | |
| 3657 | case NETDEV_CHANGEUPPER: |
| 3658 | info = ptr; |
| 3659 | |
| 3660 | /* flush all routes if dev is linked to or unlinked from |
| 3661 | * an L3 master device (e.g., VRF) |
| 3662 | */ |
| 3663 | if (info->upper_dev && netif_is_l3_master(info->upper_dev)) |
| 3664 | addrconf_ifdown(dev, 0); |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 3665 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | |
| 3667 | return NOTIFY_OK; |
| 3668 | } |
| 3669 | |
| 3670 | /* |
| 3671 | * addrconf module should be notified of a device going up |
| 3672 | */ |
| 3673 | static struct notifier_block ipv6_dev_notf = { |
| 3674 | .notifier_call = addrconf_notify, |
WANG Cong | 242d3a4 | 2017-05-08 10:12:13 -0700 | [diff] [blame] | 3675 | .priority = ADDRCONF_NOTIFY_PRIORITY, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3676 | }; |
| 3677 | |
Jiri Pirko | 93d9b7d | 2010-03-10 10:28:56 +0000 | [diff] [blame] | 3678 | static void addrconf_type_change(struct net_device *dev, unsigned long event) |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 3679 | { |
| 3680 | struct inet6_dev *idev; |
| 3681 | ASSERT_RTNL(); |
| 3682 | |
| 3683 | idev = __in6_dev_get(dev); |
| 3684 | |
Jiri Pirko | 93d9b7d | 2010-03-10 10:28:56 +0000 | [diff] [blame] | 3685 | if (event == NETDEV_POST_TYPE_CHANGE) |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 3686 | ipv6_mc_remap(idev); |
Jiri Pirko | 93d9b7d | 2010-03-10 10:28:56 +0000 | [diff] [blame] | 3687 | else if (event == NETDEV_PRE_TYPE_CHANGE) |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 3688 | ipv6_mc_unmap(idev); |
| 3689 | } |
| 3690 | |
David Ahern | 70af921 | 2016-04-08 12:01:21 -0700 | [diff] [blame] | 3691 | static bool addr_is_local(const struct in6_addr *addr) |
| 3692 | { |
| 3693 | return ipv6_addr_type(addr) & |
| 3694 | (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); |
| 3695 | } |
| 3696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3697 | static int addrconf_ifdown(struct net_device *dev, int how) |
| 3698 | { |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3699 | unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 3700 | struct net *net = dev_net(dev); |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 3701 | struct inet6_dev *idev; |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3702 | struct inet6_ifaddr *ifa, *tmp; |
Ivan Vecera | 0aef78a | 2018-04-24 19:51:29 +0200 | [diff] [blame] | 3703 | bool keep_addr = false; |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3704 | int state, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3705 | |
| 3706 | ASSERT_RTNL(); |
| 3707 | |
Ido Schimmel | 4c981e2 | 2018-01-07 12:45:04 +0200 | [diff] [blame] | 3708 | rt6_disable_ip(dev, event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3709 | |
| 3710 | idev = __in6_dev_get(dev); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 3711 | if (!idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3712 | return -ENODEV; |
| 3713 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3714 | /* |
| 3715 | * Step 1: remove reference to ipv6 device from parent device. |
| 3716 | * Do not dev_put! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3717 | */ |
Denis V. Lunev | 439e238 | 2008-04-03 13:30:17 -0700 | [diff] [blame] | 3718 | if (how) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3719 | idev->dead = 1; |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 3720 | |
| 3721 | /* protected by rtnl_lock */ |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 3722 | RCU_INIT_POINTER(dev->ip6_ptr, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3723 | |
| 3724 | /* Step 1.5: remove snmp6 entry */ |
| 3725 | snmp6_unregister_dev(idev); |
| 3726 | |
| 3727 | } |
| 3728 | |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3729 | /* combine the user config with event to determine if permanent |
| 3730 | * addresses are to be removed from address hash table |
| 3731 | */ |
Ivan Vecera | 0aef78a | 2018-04-24 19:51:29 +0200 | [diff] [blame] | 3732 | if (!how && !idev->cnf.disable_ipv6) { |
| 3733 | /* aggregate the system setting and interface setting */ |
| 3734 | int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down; |
| 3735 | |
| 3736 | if (!_keep_addr) |
| 3737 | _keep_addr = idev->cnf.keep_addr_on_down; |
| 3738 | |
| 3739 | keep_addr = (_keep_addr > 0); |
| 3740 | } |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3741 | |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3742 | /* Step 2: clear hash table */ |
| 3743 | for (i = 0; i < IN6_ADDR_HSIZE; i++) { |
| 3744 | struct hlist_head *h = &inet6_addr_lst[i]; |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3745 | |
| 3746 | spin_lock_bh(&addrconf_hash_lock); |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 3747 | restart: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3748 | hlist_for_each_entry_rcu(ifa, h, addr_lst) { |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3749 | if (ifa->idev == idev) { |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 3750 | addrconf_del_dad_work(ifa); |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3751 | /* combined flag + permanent flag decide if |
| 3752 | * address is retained on a down event |
| 3753 | */ |
| 3754 | if (!keep_addr || |
David Ahern | 70af921 | 2016-04-08 12:01:21 -0700 | [diff] [blame] | 3755 | !(ifa->flags & IFA_F_PERMANENT) || |
| 3756 | addr_is_local(&ifa->addr)) { |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3757 | hlist_del_init_rcu(&ifa->addr_lst); |
| 3758 | goto restart; |
| 3759 | } |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3760 | } |
| 3761 | } |
| 3762 | spin_unlock_bh(&addrconf_hash_lock); |
| 3763 | } |
| 3764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | write_lock_bh(&idev->lock); |
| 3766 | |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 3767 | addrconf_del_rs_timer(idev); |
| 3768 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3769 | /* Step 2: clear flags for stateless addrconf */ |
Denis V. Lunev | 439e238 | 2008-04-03 13:30:17 -0700 | [diff] [blame] | 3770 | if (!how) |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3771 | idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3773 | /* Step 3: clear tempaddr list */ |
stephen hemminger | 372e6c8 | 2010-03-17 20:31:09 +0000 | [diff] [blame] | 3774 | while (!list_empty(&idev->tempaddr_list)) { |
| 3775 | ifa = list_first_entry(&idev->tempaddr_list, |
| 3776 | struct inet6_ifaddr, tmp_list); |
| 3777 | list_del(&ifa->tmp_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3778 | write_unlock_bh(&idev->lock); |
| 3779 | spin_lock_bh(&ifa->lock); |
| 3780 | |
| 3781 | if (ifa->ifpub) { |
| 3782 | in6_ifa_put(ifa->ifpub); |
| 3783 | ifa->ifpub = NULL; |
| 3784 | } |
| 3785 | spin_unlock_bh(&ifa->lock); |
| 3786 | in6_ifa_put(ifa); |
| 3787 | write_lock_bh(&idev->lock); |
| 3788 | } |
stephen hemminger | 8f37ada | 2010-03-03 08:19:59 +0000 | [diff] [blame] | 3789 | |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3790 | list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 3791 | struct fib6_info *rt = NULL; |
Rabin Vincent | a2d6cbb | 2017-04-10 08:36:39 +0200 | [diff] [blame] | 3792 | bool keep; |
David Ahern | 38bd10c | 2016-04-21 20:56:12 -0700 | [diff] [blame] | 3793 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 3794 | addrconf_del_dad_work(ifa); |
stephen hemminger | 84e8b80 | 2010-03-02 13:32:46 +0000 | [diff] [blame] | 3795 | |
Rabin Vincent | a2d6cbb | 2017-04-10 08:36:39 +0200 | [diff] [blame] | 3796 | keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) && |
| 3797 | !addr_is_local(&ifa->addr); |
Rabin Vincent | a2d6cbb | 2017-04-10 08:36:39 +0200 | [diff] [blame] | 3798 | |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3799 | write_unlock_bh(&idev->lock); |
Hannes Frederic Sowa | 8e8e676 | 2015-03-23 23:36:03 +0100 | [diff] [blame] | 3800 | spin_lock_bh(&ifa->lock); |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3801 | |
Rabin Vincent | a2d6cbb | 2017-04-10 08:36:39 +0200 | [diff] [blame] | 3802 | if (keep) { |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3803 | /* set state to skip the notifier below */ |
| 3804 | state = INET6_IFADDR_STATE_DEAD; |
David Ahern | 6d71713 | 2017-05-02 14:43:44 -0700 | [diff] [blame] | 3805 | ifa->state = INET6_IFADDR_STATE_PREDAD; |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3806 | if (!(ifa->flags & IFA_F_NODAD)) |
| 3807 | ifa->flags |= IFA_F_TENTATIVE; |
David Ahern | 38bd10c | 2016-04-21 20:56:12 -0700 | [diff] [blame] | 3808 | |
| 3809 | rt = ifa->rt; |
| 3810 | ifa->rt = NULL; |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3811 | } else { |
| 3812 | state = ifa->state; |
| 3813 | ifa->state = INET6_IFADDR_STATE_DEAD; |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 3814 | } |
| 3815 | |
Hannes Frederic Sowa | 8e8e676 | 2015-03-23 23:36:03 +0100 | [diff] [blame] | 3816 | spin_unlock_bh(&ifa->lock); |
stephen hemminger | 84e8b80 | 2010-03-02 13:32:46 +0000 | [diff] [blame] | 3817 | |
David Ahern | 38bd10c | 2016-04-21 20:56:12 -0700 | [diff] [blame] | 3818 | if (rt) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3819 | ip6_del_rt(net, rt); |
David Ahern | 38bd10c | 2016-04-21 20:56:12 -0700 | [diff] [blame] | 3820 | |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3821 | if (state != INET6_IFADDR_STATE_DEAD) { |
| 3822 | __ipv6_ifa_notify(RTM_DELADDR, ifa); |
Cong Wang | f88c91d | 2013-04-14 23:18:43 +0800 | [diff] [blame] | 3823 | inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); |
Mike Manning | ea06f71 | 2016-07-22 18:32:11 +0100 | [diff] [blame] | 3824 | } else { |
| 3825 | if (idev->cnf.forwarding) |
| 3826 | addrconf_leave_anycast(ifa); |
| 3827 | addrconf_leave_solict(ifa->idev, &ifa->addr); |
stephen hemminger | 27bdb2a | 2010-04-12 05:41:32 +0000 | [diff] [blame] | 3828 | } |
stephen hemminger | 8f37ada | 2010-03-03 08:19:59 +0000 | [diff] [blame] | 3829 | |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3830 | write_lock_bh(&idev->lock); |
Eric Dumazet | 8ef802a | 2017-10-07 19:30:23 -0700 | [diff] [blame] | 3831 | if (!keep) { |
| 3832 | list_del_rcu(&ifa->if_list); |
| 3833 | in6_ifa_put(ifa); |
| 3834 | } |
David S. Miller | 73a8bd7 | 2011-01-23 23:27:15 -0800 | [diff] [blame] | 3835 | } |
stephen hemminger | 8f37ada | 2010-03-03 08:19:59 +0000 | [diff] [blame] | 3836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3837 | write_unlock_bh(&idev->lock); |
| 3838 | |
Sabrina Dubroca | 381f4dc | 2014-09-10 23:23:02 +0200 | [diff] [blame] | 3839 | /* Step 5: Discard anycast and multicast list */ |
| 3840 | if (how) { |
| 3841 | ipv6_ac_destroy_dev(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3842 | ipv6_mc_destroy_dev(idev); |
Sabrina Dubroca | 381f4dc | 2014-09-10 23:23:02 +0200 | [diff] [blame] | 3843 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3844 | ipv6_mc_down(idev); |
Sabrina Dubroca | 381f4dc | 2014-09-10 23:23:02 +0200 | [diff] [blame] | 3845 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3847 | idev->tstamp = jiffies; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3848 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 3849 | /* Last: Shot the device (if unregistered) */ |
Denis V. Lunev | 439e238 | 2008-04-03 13:30:17 -0700 | [diff] [blame] | 3850 | if (how) { |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 3851 | addrconf_sysctl_unregister(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3852 | neigh_parms_release(&nd_tbl, idev->nd_parms); |
| 3853 | neigh_ifdown(&nd_tbl, dev); |
| 3854 | in6_dev_put(idev); |
| 3855 | } |
| 3856 | return 0; |
| 3857 | } |
| 3858 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 3859 | static void addrconf_rs_timer(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3860 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 3861 | struct inet6_dev *idev = from_timer(idev, t, rs_timer); |
Cong Wang | caf92bc | 2013-08-31 13:44:32 +0800 | [diff] [blame] | 3862 | struct net_device *dev = idev->dev; |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 3863 | struct in6_addr lladdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3864 | |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 3865 | write_lock(&idev->lock); |
stephen hemminger | 5b2a195 | 2010-03-02 13:32:45 +0000 | [diff] [blame] | 3866 | if (idev->dead || !(idev->if_flags & IF_READY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3867 | goto out; |
| 3868 | |
Shmulik Ladkani | 9ba2add | 2012-12-02 01:44:53 +0000 | [diff] [blame] | 3869 | if (!ipv6_accept_ra(idev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3870 | goto out; |
stephen hemminger | 5b2a195 | 2010-03-02 13:32:45 +0000 | [diff] [blame] | 3871 | |
| 3872 | /* Announcement received after solicitation was sent */ |
| 3873 | if (idev->if_flags & IF_RA_RCVD) |
| 3874 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3875 | |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 3876 | if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) { |
Cong Wang | caf92bc | 2013-08-31 13:44:32 +0800 | [diff] [blame] | 3877 | write_unlock(&idev->lock); |
| 3878 | if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) |
| 3879 | ndisc_send_rs(dev, &lladdr, |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 3880 | &in6addr_linklocal_allrouters); |
| 3881 | else |
Cong Wang | caf92bc | 2013-08-31 13:44:32 +0800 | [diff] [blame] | 3882 | goto put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3883 | |
Cong Wang | caf92bc | 2013-08-31 13:44:32 +0800 | [diff] [blame] | 3884 | write_lock(&idev->lock); |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 3885 | idev->rs_interval = rfc3315_s14_backoff_update( |
| 3886 | idev->rs_interval, idev->cnf.rtr_solicit_max_interval); |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 3887 | /* The wait after the last probe can be shorter */ |
| 3888 | addrconf_mod_rs_timer(idev, (idev->rs_probes == |
| 3889 | idev->cnf.rtr_solicits) ? |
| 3890 | idev->cnf.rtr_solicit_delay : |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 3891 | idev->rs_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3892 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3893 | /* |
| 3894 | * Note: we do not support deprecated "all on-link" |
| 3895 | * assumption any longer. |
| 3896 | */ |
Joe Perches | 91df42b | 2012-05-15 14:11:54 +0000 | [diff] [blame] | 3897 | pr_debug("%s: no IPv6 routers present\n", idev->dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | out: |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 3901 | write_unlock(&idev->lock); |
Cong Wang | caf92bc | 2013-08-31 13:44:32 +0800 | [diff] [blame] | 3902 | put: |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 3903 | in6_dev_put(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3904 | } |
| 3905 | |
| 3906 | /* |
| 3907 | * Duplicate Address Detection |
| 3908 | */ |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3909 | static void addrconf_dad_kick(struct inet6_ifaddr *ifp) |
| 3910 | { |
| 3911 | unsigned long rand_num; |
| 3912 | struct inet6_dev *idev = ifp->idev; |
Erik Nordmark | adc176c | 2016-12-02 14:00:08 -0800 | [diff] [blame] | 3913 | u64 nonce; |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3914 | |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 3915 | if (ifp->flags & IFA_F_OPTIMISTIC) |
| 3916 | rand_num = 0; |
| 3917 | else |
Aruna-Hewapathirane | 63862b5 | 2014-01-11 07:15:59 -0500 | [diff] [blame] | 3918 | rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1); |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 3919 | |
Erik Nordmark | adc176c | 2016-12-02 14:00:08 -0800 | [diff] [blame] | 3920 | nonce = 0; |
| 3921 | if (idev->cnf.enhanced_dad || |
| 3922 | dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) { |
| 3923 | do |
| 3924 | get_random_bytes(&nonce, 6); |
| 3925 | while (nonce == 0); |
| 3926 | } |
| 3927 | ifp->dad_nonce = nonce; |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 3928 | ifp->dad_probes = idev->cnf.dad_transmits; |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 3929 | addrconf_mod_dad_work(ifp, rand_num); |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3930 | } |
| 3931 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 3932 | static void addrconf_dad_begin(struct inet6_ifaddr *ifp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | { |
| 3934 | struct inet6_dev *idev = ifp->idev; |
| 3935 | struct net_device *dev = idev->dev; |
Paolo Abeni | 764d3be | 2016-11-22 16:57:40 +0100 | [diff] [blame] | 3936 | bool bump_id, notify = false; |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3937 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3938 | |
| 3939 | addrconf_join_solict(dev, &ifp->addr); |
| 3940 | |
Aruna-Hewapathirane | 63862b5 | 2014-01-11 07:15:59 -0500 | [diff] [blame] | 3941 | prandom_seed((__force u32) ifp->addr.s6_addr32[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | |
| 3943 | read_lock_bh(&idev->lock); |
Herbert Xu | 622ccdf | 2010-05-18 15:56:06 -0700 | [diff] [blame] | 3944 | spin_lock(&ifp->lock); |
Herbert Xu | e9d3e08 | 2010-05-18 15:36:06 -0700 | [diff] [blame] | 3945 | if (ifp->state == INET6_IFADDR_STATE_DEAD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3947 | |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3948 | net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3949 | if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3950 | (net->ipv6.devconf_all->accept_dad < 1 && |
Matteo Croce | a2d3f3e | 2017-10-05 19:03:05 +0200 | [diff] [blame] | 3951 | idev->cnf.accept_dad < 1) || |
Noriaki TAKAMIYA | 55ebaef | 2006-09-22 14:45:27 -0700 | [diff] [blame] | 3952 | !(ifp->flags&IFA_F_TENTATIVE) || |
| 3953 | ifp->flags & IFA_F_NODAD) { |
David Ahern | c76fe2d | 2018-01-25 20:16:29 -0800 | [diff] [blame] | 3954 | bool send_na = false; |
| 3955 | |
| 3956 | if (ifp->flags & IFA_F_TENTATIVE && |
| 3957 | !(ifp->flags & IFA_F_OPTIMISTIC)) |
| 3958 | send_na = true; |
Paolo Abeni | 764d3be | 2016-11-22 16:57:40 +0100 | [diff] [blame] | 3959 | bump_id = ifp->flags & IFA_F_TENTATIVE; |
Brian Haley | cc411d0 | 2009-09-09 14:41:32 +0000 | [diff] [blame] | 3960 | ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 3961 | spin_unlock(&ifp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3962 | read_unlock_bh(&idev->lock); |
| 3963 | |
David Ahern | c76fe2d | 2018-01-25 20:16:29 -0800 | [diff] [blame] | 3964 | addrconf_dad_completed(ifp, bump_id, send_na); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | return; |
| 3966 | } |
| 3967 | |
YOSHIFUJI Hideaki | 6732bad | 2005-12-27 13:35:15 -0800 | [diff] [blame] | 3968 | if (!(idev->if_flags & IF_READY)) { |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 3969 | spin_unlock(&ifp->lock); |
YOSHIFUJI Hideaki | 6732bad | 2005-12-27 13:35:15 -0800 | [diff] [blame] | 3970 | read_unlock_bh(&idev->lock); |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3971 | /* |
Masatake YAMATO | 590a988 | 2009-06-09 10:41:12 +0900 | [diff] [blame] | 3972 | * If the device is not ready: |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3973 | * - keep it tentative if it is a permanent address. |
| 3974 | * - otherwise, kill it. |
| 3975 | */ |
| 3976 | in6_ifa_hold(ifp); |
Brian Haley | cc411d0 | 2009-09-09 14:41:32 +0000 | [diff] [blame] | 3977 | addrconf_dad_stop(ifp, 0); |
YOSHIFUJI Hideaki | 6732bad | 2005-12-27 13:35:15 -0800 | [diff] [blame] | 3978 | return; |
YOSHIFUJI Hideaki | 3c21edb | 2005-12-21 22:57:24 +0900 | [diff] [blame] | 3979 | } |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 3980 | |
| 3981 | /* |
| 3982 | * Optimistic nodes can start receiving |
| 3983 | * Frames right away |
| 3984 | */ |
Erik Kline | 7fd2561 | 2014-10-28 18:11:14 +0900 | [diff] [blame] | 3985 | if (ifp->flags & IFA_F_OPTIMISTIC) { |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 3986 | ip6_ins_rt(net, ifp->rt); |
| 3987 | if (ipv6_use_optimistic_addr(net, idev)) { |
Erik Kline | 7fd2561 | 2014-10-28 18:11:14 +0900 | [diff] [blame] | 3988 | /* Because optimistic nodes can use this address, |
| 3989 | * notify listeners. If DAD fails, RTM_DELADDR is sent. |
| 3990 | */ |
subashab@codeaurora.org | 16186a8 | 2016-02-02 02:11:10 +0000 | [diff] [blame] | 3991 | notify = true; |
Erik Kline | 7fd2561 | 2014-10-28 18:11:14 +0900 | [diff] [blame] | 3992 | } |
| 3993 | } |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 3994 | |
YOSHIFUJI Hideaki | 6732bad | 2005-12-27 13:35:15 -0800 | [diff] [blame] | 3995 | addrconf_dad_kick(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3996 | out: |
Herbert Xu | 622ccdf | 2010-05-18 15:56:06 -0700 | [diff] [blame] | 3997 | spin_unlock(&ifp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3998 | read_unlock_bh(&idev->lock); |
subashab@codeaurora.org | 16186a8 | 2016-02-02 02:11:10 +0000 | [diff] [blame] | 3999 | if (notify) |
| 4000 | ipv6_ifa_notify(RTM_NEWADDR, ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | } |
| 4002 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4003 | static void addrconf_dad_start(struct inet6_ifaddr *ifp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4004 | { |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4005 | bool begin_dad = false; |
| 4006 | |
Hannes Frederic Sowa | 8e8e676 | 2015-03-23 23:36:03 +0100 | [diff] [blame] | 4007 | spin_lock_bh(&ifp->lock); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4008 | if (ifp->state != INET6_IFADDR_STATE_DEAD) { |
| 4009 | ifp->state = INET6_IFADDR_STATE_PREDAD; |
| 4010 | begin_dad = true; |
| 4011 | } |
Hannes Frederic Sowa | 8e8e676 | 2015-03-23 23:36:03 +0100 | [diff] [blame] | 4012 | spin_unlock_bh(&ifp->lock); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4013 | |
| 4014 | if (begin_dad) |
| 4015 | addrconf_mod_dad_work(ifp, 0); |
| 4016 | } |
| 4017 | |
| 4018 | static void addrconf_dad_work(struct work_struct *w) |
| 4019 | { |
| 4020 | struct inet6_ifaddr *ifp = container_of(to_delayed_work(w), |
| 4021 | struct inet6_ifaddr, |
| 4022 | dad_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4023 | struct inet6_dev *idev = ifp->idev; |
Paolo Abeni | 764d3be | 2016-11-22 16:57:40 +0100 | [diff] [blame] | 4024 | bool bump_id, disable_ipv6 = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4025 | struct in6_addr mcaddr; |
| 4026 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4027 | enum { |
| 4028 | DAD_PROCESS, |
| 4029 | DAD_BEGIN, |
| 4030 | DAD_ABORT, |
| 4031 | } action = DAD_PROCESS; |
| 4032 | |
| 4033 | rtnl_lock(); |
| 4034 | |
Hannes Frederic Sowa | 8e8e676 | 2015-03-23 23:36:03 +0100 | [diff] [blame] | 4035 | spin_lock_bh(&ifp->lock); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4036 | if (ifp->state == INET6_IFADDR_STATE_PREDAD) { |
| 4037 | action = DAD_BEGIN; |
| 4038 | ifp->state = INET6_IFADDR_STATE_DAD; |
| 4039 | } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) { |
| 4040 | action = DAD_ABORT; |
| 4041 | ifp->state = INET6_IFADDR_STATE_POSTDAD; |
Mike Manning | 85b51b1 | 2016-08-18 14:39:40 +0100 | [diff] [blame] | 4042 | |
Matteo Croce | 35e015e | 2017-09-12 17:46:37 +0200 | [diff] [blame] | 4043 | if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 || |
| 4044 | idev->cnf.accept_dad > 1) && |
| 4045 | !idev->cnf.disable_ipv6 && |
Mike Manning | 85b51b1 | 2016-08-18 14:39:40 +0100 | [diff] [blame] | 4046 | !(ifp->flags & IFA_F_STABLE_PRIVACY)) { |
| 4047 | struct in6_addr addr; |
| 4048 | |
| 4049 | addr.s6_addr32[0] = htonl(0xfe800000); |
| 4050 | addr.s6_addr32[1] = 0; |
| 4051 | |
| 4052 | if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && |
| 4053 | ipv6_addr_equal(&ifp->addr, &addr)) { |
| 4054 | /* DAD failed for link-local based on MAC */ |
| 4055 | idev->cnf.disable_ipv6 = 1; |
| 4056 | |
| 4057 | pr_info("%s: IPv6 being disabled!\n", |
| 4058 | ifp->idev->dev->name); |
| 4059 | disable_ipv6 = true; |
| 4060 | } |
| 4061 | } |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4062 | } |
Hannes Frederic Sowa | 8e8e676 | 2015-03-23 23:36:03 +0100 | [diff] [blame] | 4063 | spin_unlock_bh(&ifp->lock); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4064 | |
| 4065 | if (action == DAD_BEGIN) { |
| 4066 | addrconf_dad_begin(ifp); |
| 4067 | goto out; |
| 4068 | } else if (action == DAD_ABORT) { |
Wei Yongjun | 751eb6b | 2016-09-05 16:06:31 +0800 | [diff] [blame] | 4069 | in6_ifa_hold(ifp); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4070 | addrconf_dad_stop(ifp, 1); |
Mike Manning | 85b51b1 | 2016-08-18 14:39:40 +0100 | [diff] [blame] | 4071 | if (disable_ipv6) |
| 4072 | addrconf_ifdown(idev->dev, 0); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4073 | goto out; |
| 4074 | } |
| 4075 | |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 4076 | if (!ifp->dad_probes && addrconf_dad_end(ifp)) |
Herbert Xu | f2344a1 | 2010-05-18 15:55:27 -0700 | [diff] [blame] | 4077 | goto out; |
| 4078 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4079 | write_lock_bh(&idev->lock); |
stephen hemminger | 122e451 | 2010-03-02 13:32:44 +0000 | [diff] [blame] | 4080 | if (idev->dead || !(idev->if_flags & IF_READY)) { |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4081 | write_unlock_bh(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4082 | goto out; |
| 4083 | } |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 4084 | |
| 4085 | spin_lock(&ifp->lock); |
Herbert Xu | 622ccdf | 2010-05-18 15:56:06 -0700 | [diff] [blame] | 4086 | if (ifp->state == INET6_IFADDR_STATE_DEAD) { |
| 4087 | spin_unlock(&ifp->lock); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4088 | write_unlock_bh(&idev->lock); |
Herbert Xu | 622ccdf | 2010-05-18 15:56:06 -0700 | [diff] [blame] | 4089 | goto out; |
| 4090 | } |
| 4091 | |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 4092 | if (ifp->dad_probes == 0) { |
David Ahern | c76fe2d | 2018-01-25 20:16:29 -0800 | [diff] [blame] | 4093 | bool send_na = false; |
| 4094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4095 | /* |
| 4096 | * DAD was successful |
| 4097 | */ |
| 4098 | |
David Ahern | c76fe2d | 2018-01-25 20:16:29 -0800 | [diff] [blame] | 4099 | if (ifp->flags & IFA_F_TENTATIVE && |
| 4100 | !(ifp->flags & IFA_F_OPTIMISTIC)) |
| 4101 | send_na = true; |
Paolo Abeni | 764d3be | 2016-11-22 16:57:40 +0100 | [diff] [blame] | 4102 | bump_id = ifp->flags & IFA_F_TENTATIVE; |
Brian Haley | cc411d0 | 2009-09-09 14:41:32 +0000 | [diff] [blame] | 4103 | ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 4104 | spin_unlock(&ifp->lock); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4105 | write_unlock_bh(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4106 | |
David Ahern | c76fe2d | 2018-01-25 20:16:29 -0800 | [diff] [blame] | 4107 | addrconf_dad_completed(ifp, bump_id, send_na); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | |
| 4109 | goto out; |
| 4110 | } |
| 4111 | |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 4112 | ifp->dad_probes--; |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4113 | addrconf_mod_dad_work(ifp, |
| 4114 | NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME)); |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 4115 | spin_unlock(&ifp->lock); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4116 | write_unlock_bh(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4117 | |
| 4118 | /* send a neighbour solicitation for our addr */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | addrconf_addr_solict_mult(&ifp->addr, &mcaddr); |
Erik Nordmark | adc176c | 2016-12-02 14:00:08 -0800 | [diff] [blame] | 4120 | ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any, |
| 4121 | ifp->dad_nonce); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4122 | out: |
| 4123 | in6_ifa_put(ifp); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4124 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | } |
| 4126 | |
Hannes Frederic Sowa | 11ffff7 | 2014-01-16 20:13:04 +0100 | [diff] [blame] | 4127 | /* ifp->idev must be at least read locked */ |
| 4128 | static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp) |
| 4129 | { |
| 4130 | struct inet6_ifaddr *ifpiter; |
| 4131 | struct inet6_dev *idev = ifp->idev; |
| 4132 | |
Hannes Frederic Sowa | 602582c | 2014-01-19 21:58:19 +0100 | [diff] [blame] | 4133 | list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) { |
| 4134 | if (ifpiter->scope > IFA_LINK) |
| 4135 | break; |
Hannes Frederic Sowa | 11ffff7 | 2014-01-16 20:13:04 +0100 | [diff] [blame] | 4136 | if (ifp != ifpiter && ifpiter->scope == IFA_LINK && |
| 4137 | (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE| |
| 4138 | IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) == |
| 4139 | IFA_F_PERMANENT) |
| 4140 | return false; |
| 4141 | } |
| 4142 | return true; |
| 4143 | } |
| 4144 | |
David Ahern | c76fe2d | 2018-01-25 20:16:29 -0800 | [diff] [blame] | 4145 | static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id, |
| 4146 | bool send_na) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4147 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4148 | struct net_device *dev = ifp->idev->dev; |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 4149 | struct in6_addr lladdr; |
Hannes Frederic Sowa | b173ee4 | 2013-06-27 00:07:01 +0200 | [diff] [blame] | 4150 | bool send_rs, send_mld; |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 4151 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4152 | addrconf_del_dad_work(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | |
| 4154 | /* |
| 4155 | * Configure the address for reception. Now it is valid. |
| 4156 | */ |
| 4157 | |
| 4158 | ipv6_ifa_notify(RTM_NEWADDR, ifp); |
| 4159 | |
Tore Anderson | 026359b | 2011-08-28 23:47:33 +0000 | [diff] [blame] | 4160 | /* If added prefix is link local and we are prepared to process |
| 4161 | router advertisements, start sending router solicitations. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4162 | */ |
| 4163 | |
Hannes Frederic Sowa | 1ec047e | 2013-06-27 00:06:56 +0200 | [diff] [blame] | 4164 | read_lock_bh(&ifp->idev->lock); |
Hannes Frederic Sowa | 11ffff7 | 2014-01-16 20:13:04 +0100 | [diff] [blame] | 4165 | send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp); |
Hannes Frederic Sowa | b173ee4 | 2013-06-27 00:07:01 +0200 | [diff] [blame] | 4166 | send_rs = send_mld && |
| 4167 | ipv6_accept_ra(ifp->idev) && |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 4168 | ifp->idev->cnf.rtr_solicits != 0 && |
Hannes Frederic Sowa | b173ee4 | 2013-06-27 00:07:01 +0200 | [diff] [blame] | 4169 | (dev->flags&IFF_LOOPBACK) == 0; |
Hannes Frederic Sowa | 1ec047e | 2013-06-27 00:06:56 +0200 | [diff] [blame] | 4170 | read_unlock_bh(&ifp->idev->lock); |
| 4171 | |
Hannes Frederic Sowa | b173ee4 | 2013-06-27 00:07:01 +0200 | [diff] [blame] | 4172 | /* While dad is in progress mld report's source address is in6_addrany. |
| 4173 | * Resend with proper ll now. |
| 4174 | */ |
| 4175 | if (send_mld) |
| 4176 | ipv6_mc_dad_complete(ifp->idev); |
| 4177 | |
David Ahern | c76fe2d | 2018-01-25 20:16:29 -0800 | [diff] [blame] | 4178 | /* send unsolicited NA if enabled */ |
| 4179 | if (send_na && |
| 4180 | (ifp->idev->cnf.ndisc_notify || |
| 4181 | dev_net(dev)->ipv6.devconf_all->ndisc_notify)) { |
| 4182 | ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr, |
| 4183 | /*router=*/ !!ifp->idev->cnf.forwarding, |
| 4184 | /*solicited=*/ false, /*override=*/ true, |
| 4185 | /*inc_opt=*/ true); |
| 4186 | } |
| 4187 | |
Hannes Frederic Sowa | 1ec047e | 2013-06-27 00:06:56 +0200 | [diff] [blame] | 4188 | if (send_rs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | /* |
| 4190 | * If a host as already performed a random delay |
| 4191 | * [...] as part of DAD [...] there is no need |
| 4192 | * to delay again before sending the first RS |
| 4193 | */ |
Hannes Frederic Sowa | 1ec047e | 2013-06-27 00:06:56 +0200 | [diff] [blame] | 4194 | if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 4195 | return; |
Hannes Frederic Sowa | 1ec047e | 2013-06-27 00:06:56 +0200 | [diff] [blame] | 4196 | ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 4198 | write_lock_bh(&ifp->idev->lock); |
| 4199 | spin_lock(&ifp->lock); |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 4200 | ifp->idev->rs_interval = rfc3315_s14_backoff_init( |
| 4201 | ifp->idev->cnf.rtr_solicit_interval); |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 4202 | ifp->idev->rs_probes = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | ifp->idev->if_flags |= IF_RS_SENT; |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 4204 | addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval); |
Hannes Frederic Sowa | b7b1bfc | 2013-06-23 18:39:01 +0200 | [diff] [blame] | 4205 | spin_unlock(&ifp->lock); |
| 4206 | write_unlock_bh(&ifp->idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | } |
Paolo Abeni | 764d3be | 2016-11-22 16:57:40 +0100 | [diff] [blame] | 4208 | |
| 4209 | if (bump_id) |
| 4210 | rt_genid_bump_ipv6(dev_net(dev)); |
Marcus Huewe | a11a7f7 | 2017-02-06 18:34:56 +0100 | [diff] [blame] | 4211 | |
| 4212 | /* Make sure that a new temporary address will be created |
| 4213 | * before this temporary address becomes deprecated. |
| 4214 | */ |
| 4215 | if (ifp->flags & IFA_F_TEMPORARY) |
| 4216 | addrconf_verify_rtnl(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | } |
| 4218 | |
Hangbin Liu | 896585d | 2018-11-21 21:52:33 +0800 | [diff] [blame] | 4219 | static void addrconf_dad_run(struct inet6_dev *idev, bool restart) |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 4220 | { |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 4221 | struct inet6_ifaddr *ifp; |
| 4222 | |
| 4223 | read_lock_bh(&idev->lock); |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 4224 | list_for_each_entry(ifp, &idev->addr_list, if_list) { |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 4225 | spin_lock(&ifp->lock); |
Hangbin Liu | 896585d | 2018-11-21 21:52:33 +0800 | [diff] [blame] | 4226 | if ((ifp->flags & IFA_F_TENTATIVE && |
| 4227 | ifp->state == INET6_IFADDR_STATE_DAD) || restart) { |
| 4228 | if (restart) |
| 4229 | ifp->state = INET6_IFADDR_STATE_PREDAD; |
Herbert Xu | f2344a1 | 2010-05-18 15:55:27 -0700 | [diff] [blame] | 4230 | addrconf_dad_kick(ifp); |
Hangbin Liu | 896585d | 2018-11-21 21:52:33 +0800 | [diff] [blame] | 4231 | } |
stephen hemminger | 21809fa | 2010-02-08 19:48:52 +0000 | [diff] [blame] | 4232 | spin_unlock(&ifp->lock); |
YOSHIFUJI Hideaki | c5e33bd | 2005-12-21 22:57:44 +0900 | [diff] [blame] | 4233 | } |
| 4234 | read_unlock_bh(&idev->lock); |
| 4235 | } |
| 4236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | #ifdef CONFIG_PROC_FS |
| 4238 | struct if6_iter_state { |
Daniel Lezcano | 3c40090 | 2008-01-10 22:42:49 -0800 | [diff] [blame] | 4239 | struct seq_net_private p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | int bucket; |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 4241 | int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | }; |
| 4243 | |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 4244 | static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | struct if6_iter_state *state = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 4247 | struct net *net = seq_file_net(seq); |
Eric Dumazet | a5c1d98 | 2017-10-23 16:17:50 -0700 | [diff] [blame] | 4248 | struct inet6_ifaddr *ifa = NULL; |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 4249 | int p = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 4251 | /* initial bucket if pos is 0 */ |
| 4252 | if (pos == 0) { |
| 4253 | state->bucket = 0; |
| 4254 | state->offset = 0; |
| 4255 | } |
| 4256 | |
| 4257 | for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { |
Eric Dumazet | a5c1d98 | 2017-10-23 16:17:50 -0700 | [diff] [blame] | 4258 | hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket], |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 4259 | addr_lst) { |
Eric Dumazet | 9f0d3c2 | 2012-10-16 07:37:27 +0000 | [diff] [blame] | 4260 | if (!net_eq(dev_net(ifa->idev->dev), net)) |
| 4261 | continue; |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 4262 | /* sync with offset */ |
| 4263 | if (p < state->offset) { |
| 4264 | p++; |
| 4265 | continue; |
| 4266 | } |
Eric Dumazet | 9f0d3c2 | 2012-10-16 07:37:27 +0000 | [diff] [blame] | 4267 | return ifa; |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | /* prepare for next bucket */ |
| 4271 | state->offset = 0; |
| 4272 | p = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | } |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 4274 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | } |
| 4276 | |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 4277 | static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, |
| 4278 | struct inet6_ifaddr *ifa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | { |
| 4280 | struct if6_iter_state *state = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 4281 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | |
Eric Dumazet | a5c1d98 | 2017-10-23 16:17:50 -0700 | [diff] [blame] | 4283 | hlist_for_each_entry_continue_rcu(ifa, addr_lst) { |
Eric Dumazet | 9f0d3c2 | 2012-10-16 07:37:27 +0000 | [diff] [blame] | 4284 | if (!net_eq(dev_net(ifa->idev->dev), net)) |
| 4285 | continue; |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 4286 | state->offset++; |
Eric Dumazet | 9f0d3c2 | 2012-10-16 07:37:27 +0000 | [diff] [blame] | 4287 | return ifa; |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 4288 | } |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 4289 | |
Jeff Barnhill | 86f9bd1 | 2018-09-21 00:45:27 +0000 | [diff] [blame] | 4290 | state->offset = 0; |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 4291 | while (++state->bucket < IN6_ADDR_HSIZE) { |
Eric Dumazet | a5c1d98 | 2017-10-23 16:17:50 -0700 | [diff] [blame] | 4292 | hlist_for_each_entry_rcu(ifa, |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 4293 | &inet6_addr_lst[state->bucket], addr_lst) { |
Eric Dumazet | 9f0d3c2 | 2012-10-16 07:37:27 +0000 | [diff] [blame] | 4294 | if (!net_eq(dev_net(ifa->idev->dev), net)) |
| 4295 | continue; |
Eric Dumazet | 9f0d3c2 | 2012-10-16 07:37:27 +0000 | [diff] [blame] | 4296 | return ifa; |
Daniel Lezcano | 3c40090 | 2008-01-10 22:42:49 -0800 | [diff] [blame] | 4297 | } |
| 4298 | } |
| 4299 | |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 4300 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | } |
| 4302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | static void *if6_seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | a5c1d98 | 2017-10-23 16:17:50 -0700 | [diff] [blame] | 4304 | __acquires(rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | { |
Eric Dumazet | a5c1d98 | 2017-10-23 16:17:50 -0700 | [diff] [blame] | 4306 | rcu_read_lock(); |
Mihai Maruseac | 1d57830 | 2012-01-03 23:31:35 +0000 | [diff] [blame] | 4307 | return if6_get_first(seq, *pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 4311 | { |
| 4312 | struct inet6_ifaddr *ifa; |
| 4313 | |
| 4314 | ifa = if6_get_next(seq, v); |
| 4315 | ++*pos; |
| 4316 | return ifa; |
| 4317 | } |
| 4318 | |
| 4319 | static void if6_seq_stop(struct seq_file *seq, void *v) |
Eric Dumazet | a5c1d98 | 2017-10-23 16:17:50 -0700 | [diff] [blame] | 4320 | __releases(rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | { |
Eric Dumazet | a5c1d98 | 2017-10-23 16:17:50 -0700 | [diff] [blame] | 4322 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | } |
| 4324 | |
| 4325 | static int if6_seq_show(struct seq_file *seq, void *v) |
| 4326 | { |
| 4327 | struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; |
Jiri Pirko | 971a351 | 2013-12-10 13:56:29 +0100 | [diff] [blame] | 4328 | seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", |
Harvey Harrison | b071195 | 2008-10-28 16:05:40 -0700 | [diff] [blame] | 4329 | &ifp->addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | ifp->idev->dev->ifindex, |
| 4331 | ifp->prefix_len, |
| 4332 | ifp->scope, |
Jiri Pirko | 971a351 | 2013-12-10 13:56:29 +0100 | [diff] [blame] | 4333 | (u8) ifp->flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | ifp->idev->dev->name); |
| 4335 | return 0; |
| 4336 | } |
| 4337 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 4338 | static const struct seq_operations if6_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | .start = if6_seq_start, |
| 4340 | .next = if6_seq_next, |
| 4341 | .show = if6_seq_show, |
| 4342 | .stop = if6_seq_stop, |
| 4343 | }; |
| 4344 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 4345 | static int __net_init if6_proc_net_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | { |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 4347 | if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops, |
| 4348 | sizeof(struct if6_iter_state))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | return -ENOMEM; |
| 4350 | return 0; |
| 4351 | } |
| 4352 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 4353 | static void __net_exit if6_proc_net_exit(struct net *net) |
Daniel Lezcano | 3c40090 | 2008-01-10 22:42:49 -0800 | [diff] [blame] | 4354 | { |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 4355 | remove_proc_entry("if_inet6", net->proc_net); |
Daniel Lezcano | 3c40090 | 2008-01-10 22:42:49 -0800 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | static struct pernet_operations if6_proc_net_ops = { |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 4359 | .init = if6_proc_net_init, |
| 4360 | .exit = if6_proc_net_exit, |
Daniel Lezcano | 3c40090 | 2008-01-10 22:42:49 -0800 | [diff] [blame] | 4361 | }; |
| 4362 | |
| 4363 | int __init if6_proc_init(void) |
| 4364 | { |
| 4365 | return register_pernet_subsys(&if6_proc_net_ops); |
| 4366 | } |
| 4367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | void if6_proc_exit(void) |
| 4369 | { |
Daniel Lezcano | 3c40090 | 2008-01-10 22:42:49 -0800 | [diff] [blame] | 4370 | unregister_pernet_subsys(&if6_proc_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | } |
| 4372 | #endif /* CONFIG_PROC_FS */ |
| 4373 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 4374 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
Noriaki TAKAMIYA | 3b9f9a1 | 2006-09-22 14:45:56 -0700 | [diff] [blame] | 4375 | /* Check if address is a home address configured on any interface. */ |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 4376 | int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) |
Noriaki TAKAMIYA | 3b9f9a1 | 2006-09-22 14:45:56 -0700 | [diff] [blame] | 4377 | { |
Eric Dumazet | 3f27fb2 | 2017-10-23 16:17:47 -0700 | [diff] [blame] | 4378 | unsigned int hash = inet6_addr_hash(net, addr); |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 4379 | struct inet6_ifaddr *ifp = NULL; |
Eric Dumazet | 3f27fb2 | 2017-10-23 16:17:47 -0700 | [diff] [blame] | 4380 | int ret = 0; |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 4381 | |
Eric Dumazet | 4e5f47a | 2017-10-23 16:17:51 -0700 | [diff] [blame] | 4382 | rcu_read_lock(); |
| 4383 | hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 4384 | if (!net_eq(dev_net(ifp->idev->dev), net)) |
Daniel Lezcano | 389f661 | 2008-01-10 22:44:40 -0800 | [diff] [blame] | 4385 | continue; |
YOSHIFUJI Hideaki | caad295 | 2008-04-10 15:42:07 +0900 | [diff] [blame] | 4386 | if (ipv6_addr_equal(&ifp->addr, addr) && |
Noriaki TAKAMIYA | 3b9f9a1 | 2006-09-22 14:45:56 -0700 | [diff] [blame] | 4387 | (ifp->flags & IFA_F_HOMEADDRESS)) { |
| 4388 | ret = 1; |
| 4389 | break; |
| 4390 | } |
| 4391 | } |
Eric Dumazet | 4e5f47a | 2017-10-23 16:17:51 -0700 | [diff] [blame] | 4392 | rcu_read_unlock(); |
Noriaki TAKAMIYA | 3b9f9a1 | 2006-09-22 14:45:56 -0700 | [diff] [blame] | 4393 | return ret; |
| 4394 | } |
| 4395 | #endif |
| 4396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | /* |
| 4398 | * Periodic address status verification |
| 4399 | */ |
| 4400 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4401 | static void addrconf_verify_rtnl(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | { |
YOSHIFUJI Hideaki | b2db756 | 2010-03-20 16:11:12 -0700 | [diff] [blame] | 4403 | unsigned long now, next, next_sec, next_sched; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | struct inet6_ifaddr *ifp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | int i; |
| 4406 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4407 | ASSERT_RTNL(); |
| 4408 | |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 4409 | rcu_read_lock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | now = jiffies; |
YOSHIFUJI Hideaki | b2db756 | 2010-03-20 16:11:12 -0700 | [diff] [blame] | 4411 | next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4413 | cancel_delayed_work(&addr_chk_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | |
Stephen Hemminger | bcdd553 | 2010-03-20 16:08:18 -0700 | [diff] [blame] | 4415 | for (i = 0; i < IN6_ADDR_HSIZE; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4416 | restart: |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4417 | hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | unsigned long age; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | |
Yasushi Asano | fad8da3 | 2013-12-31 12:04:19 +0900 | [diff] [blame] | 4420 | /* When setting preferred_lft to a value not zero or |
| 4421 | * infinity, while valid_lft is infinity |
| 4422 | * IFA_F_PERMANENT has a non-infinity life time. |
| 4423 | */ |
| 4424 | if ((ifp->flags & IFA_F_PERMANENT) && |
| 4425 | (ifp->prefered_lft == INFINITY_LIFE_TIME)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | continue; |
| 4427 | |
| 4428 | spin_lock(&ifp->lock); |
YOSHIFUJI Hideaki | b2db756 | 2010-03-20 16:11:12 -0700 | [diff] [blame] | 4429 | /* We try to batch several events at once. */ |
| 4430 | age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4431 | |
YOSHIFUJI Hideaki | 8f27ebb | 2006-07-28 18:12:11 +0900 | [diff] [blame] | 4432 | if (ifp->valid_lft != INFINITY_LIFE_TIME && |
| 4433 | age >= ifp->valid_lft) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | spin_unlock(&ifp->lock); |
| 4435 | in6_ifa_hold(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | ipv6_del_addr(ifp); |
| 4437 | goto restart; |
YOSHIFUJI Hideaki | 8f27ebb | 2006-07-28 18:12:11 +0900 | [diff] [blame] | 4438 | } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { |
| 4439 | spin_unlock(&ifp->lock); |
| 4440 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | } else if (age >= ifp->prefered_lft) { |
Brian Haley | a1ed052 | 2009-07-02 07:10:52 +0000 | [diff] [blame] | 4442 | /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | int deprecate = 0; |
| 4444 | |
| 4445 | if (!(ifp->flags&IFA_F_DEPRECATED)) { |
| 4446 | deprecate = 1; |
| 4447 | ifp->flags |= IFA_F_DEPRECATED; |
| 4448 | } |
| 4449 | |
Yasushi Asano | fad8da3 | 2013-12-31 12:04:19 +0900 | [diff] [blame] | 4450 | if ((ifp->valid_lft != INFINITY_LIFE_TIME) && |
| 4451 | (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | next = ifp->tstamp + ifp->valid_lft * HZ; |
| 4453 | |
| 4454 | spin_unlock(&ifp->lock); |
| 4455 | |
| 4456 | if (deprecate) { |
| 4457 | in6_ifa_hold(ifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | |
| 4459 | ipv6_ifa_notify(0, ifp); |
| 4460 | in6_ifa_put(ifp); |
| 4461 | goto restart; |
| 4462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | } else if ((ifp->flags&IFA_F_TEMPORARY) && |
| 4464 | !(ifp->flags&IFA_F_TENTATIVE)) { |
stephen hemminger | 88949cf | 2010-03-17 20:31:17 +0000 | [diff] [blame] | 4465 | unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * |
| 4466 | ifp->idev->cnf.dad_transmits * |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 4467 | NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ; |
stephen hemminger | 88949cf | 2010-03-17 20:31:17 +0000 | [diff] [blame] | 4468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | if (age >= ifp->prefered_lft - regen_advance) { |
| 4470 | struct inet6_ifaddr *ifpub = ifp->ifpub; |
| 4471 | if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) |
| 4472 | next = ifp->tstamp + ifp->prefered_lft * HZ; |
| 4473 | if (!ifp->regen_count && ifpub) { |
| 4474 | ifp->regen_count++; |
| 4475 | in6_ifa_hold(ifp); |
| 4476 | in6_ifa_hold(ifpub); |
| 4477 | spin_unlock(&ifp->lock); |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 4478 | |
Hiroyuki YAMAMORI | 291d809 | 2005-12-23 11:24:05 -0800 | [diff] [blame] | 4479 | spin_lock(&ifpub->lock); |
| 4480 | ifpub->regen_count = 0; |
| 4481 | spin_unlock(&ifpub->lock); |
Eric Dumazet | e64e469 | 2018-01-26 16:10:43 -0800 | [diff] [blame] | 4482 | rcu_read_unlock_bh(); |
Eric Dumazet | fffcefe | 2017-11-06 14:13:29 -0800 | [diff] [blame] | 4483 | ipv6_create_tempaddr(ifpub, ifp, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | in6_ifa_put(ifpub); |
| 4485 | in6_ifa_put(ifp); |
Eric Dumazet | e64e469 | 2018-01-26 16:10:43 -0800 | [diff] [blame] | 4486 | rcu_read_lock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | goto restart; |
| 4488 | } |
| 4489 | } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) |
| 4490 | next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; |
| 4491 | spin_unlock(&ifp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | } else { |
| 4493 | /* ifp->prefered_lft <= ifp->valid_lft */ |
| 4494 | if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) |
| 4495 | next = ifp->tstamp + ifp->prefered_lft * HZ; |
| 4496 | spin_unlock(&ifp->lock); |
| 4497 | } |
| 4498 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | } |
| 4500 | |
YOSHIFUJI Hideaki | b2db756 | 2010-03-20 16:11:12 -0700 | [diff] [blame] | 4501 | next_sec = round_jiffies_up(next); |
| 4502 | next_sched = next; |
| 4503 | |
| 4504 | /* If rounded timeout is accurate enough, accept it. */ |
| 4505 | if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) |
| 4506 | next_sched = next_sec; |
| 4507 | |
| 4508 | /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ |
| 4509 | if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) |
| 4510 | next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; |
| 4511 | |
Joe Perches | e32ac25 | 2018-03-26 08:35:01 -0700 | [diff] [blame] | 4512 | pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", |
| 4513 | now, next, next_sec, next_sched); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4514 | mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now); |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 4515 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4516 | } |
| 4517 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4518 | static void addrconf_verify_work(struct work_struct *w) |
| 4519 | { |
| 4520 | rtnl_lock(); |
| 4521 | addrconf_verify_rtnl(); |
| 4522 | rtnl_unlock(); |
| 4523 | } |
| 4524 | |
| 4525 | static void addrconf_verify(void) |
| 4526 | { |
| 4527 | mod_delayed_work(addrconf_wq, &addr_chk_work, 0); |
| 4528 | } |
| 4529 | |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 4530 | static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, |
| 4531 | struct in6_addr **peer_pfx) |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 4532 | { |
| 4533 | struct in6_addr *pfx = NULL; |
| 4534 | |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 4535 | *peer_pfx = NULL; |
| 4536 | |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 4537 | if (addr) |
| 4538 | pfx = nla_data(addr); |
| 4539 | |
| 4540 | if (local) { |
| 4541 | if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 4542 | *peer_pfx = pfx; |
| 4543 | pfx = nla_data(local); |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | return pfx; |
| 4547 | } |
| 4548 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 4549 | static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 4550 | [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, |
| 4551 | [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, |
| 4552 | [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, |
Jiri Pirko | 479840f | 2013-12-06 09:45:21 +0100 | [diff] [blame] | 4553 | [IFA_FLAGS] = { .len = sizeof(u32) }, |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 4554 | [IFA_RT_PRIORITY] = { .len = sizeof(u32) }, |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 4555 | [IFA_TARGET_NETNSID] = { .type = NLA_S32 }, |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 4556 | }; |
| 4557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | static int |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4559 | inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4560 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 4562 | struct net *net = sock_net(skb->sk); |
Thomas Graf | b933f71 | 2006-09-18 00:10:19 -0700 | [diff] [blame] | 4563 | struct ifaddrmsg *ifm; |
| 4564 | struct nlattr *tb[IFA_MAX+1]; |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 4565 | struct in6_addr *pfx, *peer_pfx; |
Heiner Kallweit | 6046d5b | 2014-04-20 21:29:36 +0200 | [diff] [blame] | 4566 | u32 ifa_flags; |
Thomas Graf | b933f71 | 2006-09-18 00:10:19 -0700 | [diff] [blame] | 4567 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4568 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4569 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, |
| 4570 | ifa_ipv6_policy, extack); |
Thomas Graf | b933f71 | 2006-09-18 00:10:19 -0700 | [diff] [blame] | 4571 | if (err < 0) |
| 4572 | return err; |
| 4573 | |
| 4574 | ifm = nlmsg_data(nlh); |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 4575 | pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 4576 | if (!pfx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | return -EINVAL; |
| 4578 | |
Heiner Kallweit | 6046d5b | 2014-04-20 21:29:36 +0200 | [diff] [blame] | 4579 | ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; |
| 4580 | |
| 4581 | /* We ignore other flags so far. */ |
| 4582 | ifa_flags &= IFA_F_MANAGETEMPADDR; |
| 4583 | |
| 4584 | return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, |
| 4585 | ifm->ifa_prefixlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | } |
| 4587 | |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 4588 | static int modify_prefix_route(struct inet6_ifaddr *ifp, |
| 4589 | unsigned long expires, u32 flags) |
| 4590 | { |
| 4591 | struct fib6_info *f6i; |
David Ahern | e7c7faa | 2018-06-28 13:36:55 -0700 | [diff] [blame] | 4592 | u32 prio; |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 4593 | |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 4594 | f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len, |
| 4595 | ifp->idev->dev, 0, RTF_DEFAULT, true); |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 4596 | if (!f6i) |
| 4597 | return -ENOENT; |
| 4598 | |
David Ahern | e7c7faa | 2018-06-28 13:36:55 -0700 | [diff] [blame] | 4599 | prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF; |
| 4600 | if (f6i->fib6_metric != prio) { |
| 4601 | /* delete old one */ |
| 4602 | ip6_del_rt(dev_net(ifp->idev->dev), f6i); |
| 4603 | |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 4604 | /* add new one */ |
| 4605 | addrconf_prefix_route(&ifp->addr, ifp->prefix_len, |
| 4606 | ifp->rt_priority, ifp->idev->dev, |
| 4607 | expires, flags, GFP_KERNEL); |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 4608 | } else { |
| 4609 | if (!expires) |
| 4610 | fib6_clean_expires(f6i); |
| 4611 | else |
| 4612 | fib6_set_expires(f6i, expires); |
| 4613 | |
| 4614 | fib6_info_release(f6i); |
| 4615 | } |
| 4616 | |
| 4617 | return 0; |
| 4618 | } |
| 4619 | |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4620 | static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg) |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 4621 | { |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 4622 | u32 flags; |
| 4623 | clock_t expires; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 4624 | unsigned long timeout; |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 4625 | bool was_managetempaddr; |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 4626 | bool had_prefixroute; |
YOSHIFUJI Hideaki | 46d4804 | 2007-02-07 20:36:26 +0900 | [diff] [blame] | 4627 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4628 | ASSERT_RTNL(); |
| 4629 | |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4630 | if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 4631 | return -EINVAL; |
| 4632 | |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4633 | if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 4634 | (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64)) |
| 4635 | return -EINVAL; |
| 4636 | |
Sabrina Dubroca | f1c02cf | 2018-02-28 16:40:08 +0100 | [diff] [blame] | 4637 | if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED) |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4638 | cfg->ifa_flags &= ~IFA_F_OPTIMISTIC; |
Sabrina Dubroca | f1c02cf | 2018-02-28 16:40:08 +0100 | [diff] [blame] | 4639 | |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4640 | timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ); |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 4641 | if (addrconf_finite_timeout(timeout)) { |
| 4642 | expires = jiffies_to_clock_t(timeout * HZ); |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4643 | cfg->valid_lft = timeout; |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 4644 | flags = RTF_EXPIRES; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 4645 | } else { |
| 4646 | expires = 0; |
| 4647 | flags = 0; |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4648 | cfg->ifa_flags |= IFA_F_PERMANENT; |
YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 4649 | } |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 4650 | |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4651 | timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ); |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 4652 | if (addrconf_finite_timeout(timeout)) { |
| 4653 | if (timeout == 0) |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4654 | cfg->ifa_flags |= IFA_F_DEPRECATED; |
| 4655 | cfg->preferred_lft = timeout; |
YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 4656 | } |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 4657 | |
| 4658 | spin_lock_bh(&ifp->lock); |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 4659 | was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR; |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 4660 | had_prefixroute = ifp->flags & IFA_F_PERMANENT && |
| 4661 | !(ifp->flags & IFA_F_NOPREFIXROUTE); |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 4662 | ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | |
Thomas Haller | 761aac7 | 2014-01-15 15:36:58 +0100 | [diff] [blame] | 4663 | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | |
| 4664 | IFA_F_NOPREFIXROUTE); |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4665 | ifp->flags |= cfg->ifa_flags; |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 4666 | ifp->tstamp = jiffies; |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4667 | ifp->valid_lft = cfg->valid_lft; |
| 4668 | ifp->prefered_lft = cfg->preferred_lft; |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 4669 | |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 4670 | if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority) |
| 4671 | ifp->rt_priority = cfg->rt_priority; |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 4672 | |
| 4673 | spin_unlock_bh(&ifp->lock); |
| 4674 | if (!(ifp->flags&IFA_F_TENTATIVE)) |
| 4675 | ipv6_ifa_notify(0, ifp); |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 4676 | |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4677 | if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) { |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 4678 | int rc = -ENOENT; |
| 4679 | |
| 4680 | if (had_prefixroute) |
| 4681 | rc = modify_prefix_route(ifp, expires, flags); |
| 4682 | |
| 4683 | /* prefix route could have been deleted; if so restore it */ |
| 4684 | if (rc == -ENOENT) { |
| 4685 | addrconf_prefix_route(&ifp->addr, ifp->prefix_len, |
| 4686 | ifp->rt_priority, ifp->idev->dev, |
| 4687 | expires, flags, GFP_KERNEL); |
| 4688 | } |
Thomas Haller | 5b84efe | 2014-01-15 15:36:59 +0100 | [diff] [blame] | 4689 | } else if (had_prefixroute) { |
| 4690 | enum cleanup_prefix_rt_t action; |
| 4691 | unsigned long rt_expires; |
| 4692 | |
| 4693 | write_lock_bh(&ifp->idev->lock); |
| 4694 | action = check_cleanup_prefix_route(ifp, &rt_expires); |
| 4695 | write_unlock_bh(&ifp->idev->lock); |
| 4696 | |
| 4697 | if (action != CLEANUP_PREFIX_RT_NOP) { |
| 4698 | cleanup_prefix_route(ifp, rt_expires, |
| 4699 | action == CLEANUP_PREFIX_RT_DEL); |
| 4700 | } |
Thomas Haller | 761aac7 | 2014-01-15 15:36:58 +0100 | [diff] [blame] | 4701 | } |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 4702 | |
| 4703 | if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) { |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4704 | if (was_managetempaddr && |
| 4705 | !(ifp->flags & IFA_F_MANAGETEMPADDR)) { |
| 4706 | cfg->valid_lft = 0; |
| 4707 | cfg->preferred_lft = 0; |
| 4708 | } |
| 4709 | manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft, |
| 4710 | cfg->preferred_lft, !was_managetempaddr, |
| 4711 | jiffies); |
Jiri Pirko | 53bd674 | 2013-12-06 09:45:22 +0100 | [diff] [blame] | 4712 | } |
| 4713 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 4714 | addrconf_verify_rtnl(); |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 4715 | |
| 4716 | return 0; |
| 4717 | } |
| 4718 | |
| 4719 | static int |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 4720 | inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 4721 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 4723 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 4724 | struct ifaddrmsg *ifm; |
| 4725 | struct nlattr *tb[IFA_MAX+1]; |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 4726 | struct in6_addr *peer_pfx; |
Thomas Graf | 7198f8c | 2006-09-18 00:13:46 -0700 | [diff] [blame] | 4727 | struct inet6_ifaddr *ifa; |
| 4728 | struct net_device *dev; |
Sabrina Dubroca | f1c02cf | 2018-02-28 16:40:08 +0100 | [diff] [blame] | 4729 | struct inet6_dev *idev; |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 4730 | struct ifa6_config cfg; |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 4731 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4732 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 4733 | err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, |
| 4734 | ifa_ipv6_policy, extack); |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 4735 | if (err < 0) |
| 4736 | return err; |
| 4737 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 4738 | memset(&cfg, 0, sizeof(cfg)); |
| 4739 | |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 4740 | ifm = nlmsg_data(nlh); |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 4741 | cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); |
| 4742 | if (!cfg.pfx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4743 | return -EINVAL; |
| 4744 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 4745 | cfg.peer_pfx = peer_pfx; |
| 4746 | cfg.plen = ifm->ifa_prefixlen; |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 4747 | if (tb[IFA_RT_PRIORITY]) |
| 4748 | cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]); |
| 4749 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 4750 | cfg.valid_lft = INFINITY_LIFE_TIME; |
| 4751 | cfg.preferred_lft = INFINITY_LIFE_TIME; |
| 4752 | |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 4753 | if (tb[IFA_CACHEINFO]) { |
Noriaki TAKAMIYA | 0778769d | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 4754 | struct ifa_cacheinfo *ci; |
Thomas Graf | 461d883 | 2006-09-18 00:09:49 -0700 | [diff] [blame] | 4755 | |
| 4756 | ci = nla_data(tb[IFA_CACHEINFO]); |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 4757 | cfg.valid_lft = ci->ifa_valid; |
| 4758 | cfg.preferred_lft = ci->ifa_prefered; |
Noriaki TAKAMIYA | 0778769d | 2006-07-28 18:12:10 +0900 | [diff] [blame] | 4759 | } |
| 4760 | |
Daniel Lezcano | af28493 | 2008-03-05 10:46:57 -0800 | [diff] [blame] | 4761 | dev = __dev_get_by_index(net, ifm->ifa_index); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 4762 | if (!dev) |
Thomas Graf | 7198f8c | 2006-09-18 00:13:46 -0700 | [diff] [blame] | 4763 | return -ENODEV; |
| 4764 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 4765 | if (tb[IFA_FLAGS]) |
| 4766 | cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]); |
| 4767 | else |
| 4768 | cfg.ifa_flags = ifm->ifa_flags; |
Jiri Pirko | 479840f | 2013-12-06 09:45:21 +0100 | [diff] [blame] | 4769 | |
Noriaki TAKAMIYA | 55ebaef | 2006-09-22 14:45:27 -0700 | [diff] [blame] | 4770 | /* We ignore other flags so far. */ |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 4771 | cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | |
| 4772 | IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE | |
| 4773 | IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC; |
Sabrina Dubroca | f1c02cf | 2018-02-28 16:40:08 +0100 | [diff] [blame] | 4774 | |
| 4775 | idev = ipv6_find_idev(dev); |
Sabrina Dubroca | db0b99f | 2019-08-23 15:44:36 +0200 | [diff] [blame^] | 4776 | if (IS_ERR(idev)) |
| 4777 | return PTR_ERR(idev); |
Sabrina Dubroca | f1c02cf | 2018-02-28 16:40:08 +0100 | [diff] [blame] | 4778 | |
| 4779 | if (!ipv6_allow_optimistic_dad(net, idev)) |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 4780 | cfg.ifa_flags &= ~IFA_F_OPTIMISTIC; |
Sabrina Dubroca | f1c02cf | 2018-02-28 16:40:08 +0100 | [diff] [blame] | 4781 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 4782 | if (cfg.ifa_flags & IFA_F_NODAD && |
| 4783 | cfg.ifa_flags & IFA_F_OPTIMISTIC) { |
Sabrina Dubroca | f1c02cf | 2018-02-28 16:40:08 +0100 | [diff] [blame] | 4784 | NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive"); |
| 4785 | return -EINVAL; |
| 4786 | } |
Noriaki TAKAMIYA | 55ebaef | 2006-09-22 14:45:27 -0700 | [diff] [blame] | 4787 | |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 4788 | ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 4789 | if (!ifa) { |
Thomas Graf | 7198f8c | 2006-09-18 00:13:46 -0700 | [diff] [blame] | 4790 | /* |
| 4791 | * It would be best to check for !NLM_F_CREATE here but |
stephen hemminger | db9c7c3 | 2014-01-12 11:26:32 -0800 | [diff] [blame] | 4792 | * userspace already relies on not having to provide this. |
Thomas Graf | 7198f8c | 2006-09-18 00:13:46 -0700 | [diff] [blame] | 4793 | */ |
David Ahern | 19b1518 | 2018-05-27 08:09:54 -0700 | [diff] [blame] | 4794 | return inet6_addr_add(net, ifm->ifa_index, &cfg, extack); |
Noriaki TAKAMIYA | 081bba5 | 2006-07-28 18:12:13 +0900 | [diff] [blame] | 4795 | } |
| 4796 | |
Thomas Graf | 7198f8c | 2006-09-18 00:13:46 -0700 | [diff] [blame] | 4797 | if (nlh->nlmsg_flags & NLM_F_EXCL || |
| 4798 | !(nlh->nlmsg_flags & NLM_F_REPLACE)) |
| 4799 | err = -EEXIST; |
| 4800 | else |
David Ahern | d169a1f | 2018-05-27 08:09:55 -0700 | [diff] [blame] | 4801 | err = inet6_addr_modify(ifa, &cfg); |
Thomas Graf | 7198f8c | 2006-09-18 00:13:46 -0700 | [diff] [blame] | 4802 | |
| 4803 | in6_ifa_put(ifa); |
| 4804 | |
| 4805 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | } |
| 4807 | |
Jiri Pirko | 479840f | 2013-12-06 09:45:21 +0100 | [diff] [blame] | 4808 | static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags, |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 4809 | u8 scope, int ifindex) |
| 4810 | { |
| 4811 | struct ifaddrmsg *ifm; |
| 4812 | |
| 4813 | ifm = nlmsg_data(nlh); |
| 4814 | ifm->ifa_family = AF_INET6; |
| 4815 | ifm->ifa_prefixlen = prefixlen; |
| 4816 | ifm->ifa_flags = flags; |
| 4817 | ifm->ifa_scope = scope; |
| 4818 | ifm->ifa_index = ifindex; |
| 4819 | } |
| 4820 | |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 4821 | static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, |
| 4822 | unsigned long tstamp, u32 preferred, u32 valid) |
| 4823 | { |
| 4824 | struct ifa_cacheinfo ci; |
| 4825 | |
Thomas Graf | 18a31e1 | 2010-11-17 04:12:02 +0000 | [diff] [blame] | 4826 | ci.cstamp = cstamp_delta(cstamp); |
| 4827 | ci.tstamp = cstamp_delta(tstamp); |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 4828 | ci.ifa_prefered = preferred; |
| 4829 | ci.ifa_valid = valid; |
| 4830 | |
| 4831 | return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); |
| 4832 | } |
| 4833 | |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 4834 | static inline int rt_scope(int ifa_scope) |
| 4835 | { |
| 4836 | if (ifa_scope & IFA_HOST) |
| 4837 | return RT_SCOPE_HOST; |
| 4838 | else if (ifa_scope & IFA_LINK) |
| 4839 | return RT_SCOPE_LINK; |
| 4840 | else if (ifa_scope & IFA_SITE) |
| 4841 | return RT_SCOPE_SITE; |
| 4842 | else |
| 4843 | return RT_SCOPE_UNIVERSE; |
| 4844 | } |
| 4845 | |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 4846 | static inline int inet6_ifaddr_msgsize(void) |
| 4847 | { |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4848 | return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 4849 | + nla_total_size(16) /* IFA_LOCAL */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 4850 | + nla_total_size(16) /* IFA_ADDRESS */ |
Jiri Pirko | 479840f | 2013-12-06 09:45:21 +0100 | [diff] [blame] | 4851 | + nla_total_size(sizeof(struct ifa_cacheinfo)) |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 4852 | + nla_total_size(4) /* IFA_FLAGS */ |
| 4853 | + nla_total_size(4) /* IFA_RT_PRIORITY */; |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 4854 | } |
YOSHIFUJI Hideaki | c5396a3 | 2006-06-17 22:48:48 -0700 | [diff] [blame] | 4855 | |
David Ahern | 6ba1e6e | 2018-10-07 20:16:26 -0700 | [diff] [blame] | 4856 | enum addr_type_t { |
| 4857 | UNICAST_ADDR, |
| 4858 | MULTICAST_ADDR, |
| 4859 | ANYCAST_ADDR, |
| 4860 | }; |
| 4861 | |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 4862 | struct inet6_fill_args { |
| 4863 | u32 portid; |
| 4864 | u32 seq; |
| 4865 | int event; |
| 4866 | unsigned int flags; |
| 4867 | int netnsid; |
David Ahern | 6371a71 | 2018-10-19 12:45:30 -0700 | [diff] [blame] | 4868 | int ifindex; |
David Ahern | 6ba1e6e | 2018-10-07 20:16:26 -0700 | [diff] [blame] | 4869 | enum addr_type_t type; |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 4870 | }; |
| 4871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4872 | static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 4873 | struct inet6_fill_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | struct nlmsghdr *nlh; |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 4876 | u32 preferred, valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 4878 | nlh = nlmsg_put(skb, args->portid, args->seq, args->event, |
| 4879 | sizeof(struct ifaddrmsg), args->flags); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 4880 | if (!nlh) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4881 | return -EMSGSIZE; |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 4882 | |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 4883 | put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), |
| 4884 | ifa->idev->dev->ifindex); |
| 4885 | |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 4886 | if (args->netnsid >= 0 && |
| 4887 | nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 4888 | goto error; |
| 4889 | |
Yasushi Asano | fad8da3 | 2013-12-31 12:04:19 +0900 | [diff] [blame] | 4890 | if (!((ifa->flags&IFA_F_PERMANENT) && |
| 4891 | (ifa->prefered_lft == INFINITY_LIFE_TIME))) { |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 4892 | preferred = ifa->prefered_lft; |
| 4893 | valid = ifa->valid_lft; |
| 4894 | if (preferred != INFINITY_LIFE_TIME) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4895 | long tval = (jiffies - ifa->tstamp)/HZ; |
Jens Rosenboom | a1faa69 | 2009-06-25 04:55:50 +0000 | [diff] [blame] | 4896 | if (preferred > tval) |
| 4897 | preferred -= tval; |
| 4898 | else |
| 4899 | preferred = 0; |
Ben Hutchings | f56619f | 2010-06-26 11:37:47 +0000 | [diff] [blame] | 4900 | if (valid != INFINITY_LIFE_TIME) { |
| 4901 | if (valid > tval) |
| 4902 | valid -= tval; |
| 4903 | else |
| 4904 | valid = 0; |
| 4905 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | } |
| 4907 | } else { |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 4908 | preferred = INFINITY_LIFE_TIME; |
| 4909 | valid = INFINITY_LIFE_TIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | } |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 4911 | |
Cong Wang | 7996c79 | 2013-05-22 05:41:06 +0000 | [diff] [blame] | 4912 | if (!ipv6_addr_any(&ifa->peer_addr)) { |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4913 | if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 || |
| 4914 | nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0) |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 4915 | goto error; |
| 4916 | } else |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4917 | if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0) |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 4918 | goto error; |
| 4919 | |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 4920 | if (ifa->rt_priority && |
| 4921 | nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority)) |
| 4922 | goto error; |
| 4923 | |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 4924 | if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) |
| 4925 | goto error; |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 4926 | |
Jiri Pirko | 479840f | 2013-12-06 09:45:21 +0100 | [diff] [blame] | 4927 | if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0) |
| 4928 | goto error; |
| 4929 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 4930 | nlmsg_end(skb, nlh); |
| 4931 | return 0; |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 4932 | |
| 4933 | error: |
| 4934 | nlmsg_cancel(skb, nlh); |
| 4935 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | } |
| 4937 | |
| 4938 | static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 4939 | struct inet6_fill_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | struct nlmsghdr *nlh; |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 4942 | u8 scope = RT_SCOPE_UNIVERSE; |
| 4943 | int ifindex = ifmca->idev->dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 4945 | if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) |
| 4946 | scope = RT_SCOPE_SITE; |
| 4947 | |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 4948 | nlh = nlmsg_put(skb, args->portid, args->seq, args->event, |
| 4949 | sizeof(struct ifaddrmsg), args->flags); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 4950 | if (!nlh) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4951 | return -EMSGSIZE; |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 4952 | |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 4953 | if (args->netnsid >= 0 && |
| 4954 | nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 4955 | return -EMSGSIZE; |
| 4956 | |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 4957 | put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4958 | if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 || |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 4959 | put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4960 | INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { |
| 4961 | nlmsg_cancel(skb, nlh); |
| 4962 | return -EMSGSIZE; |
| 4963 | } |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 4964 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 4965 | nlmsg_end(skb, nlh); |
| 4966 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | } |
| 4968 | |
| 4969 | static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 4970 | struct inet6_fill_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4971 | { |
David Ahern | 9ee8cbb | 2018-04-18 15:39:01 -0700 | [diff] [blame] | 4972 | struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt); |
| 4973 | int ifindex = dev ? dev->ifindex : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | struct nlmsghdr *nlh; |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 4975 | u8 scope = RT_SCOPE_UNIVERSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 4977 | if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) |
| 4978 | scope = RT_SCOPE_SITE; |
| 4979 | |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 4980 | nlh = nlmsg_put(skb, args->portid, args->seq, args->event, |
| 4981 | sizeof(struct ifaddrmsg), args->flags); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 4982 | if (!nlh) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4983 | return -EMSGSIZE; |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 4984 | |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 4985 | if (args->netnsid >= 0 && |
| 4986 | nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 4987 | return -EMSGSIZE; |
| 4988 | |
Thomas Graf | 101bb22 | 2006-09-18 00:11:52 -0700 | [diff] [blame] | 4989 | put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 4990 | if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 || |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 4991 | put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 4992 | INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { |
| 4993 | nlmsg_cancel(skb, nlh); |
| 4994 | return -EMSGSIZE; |
| 4995 | } |
Thomas Graf | 85486af | 2006-09-18 00:11:24 -0700 | [diff] [blame] | 4996 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 4997 | nlmsg_end(skb, nlh); |
| 4998 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4999 | } |
| 5000 | |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5001 | /* called with rcu_read_lock() */ |
| 5002 | static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, |
David Ahern | fe884c2 | 2018-10-19 12:45:28 -0700 | [diff] [blame] | 5003 | struct netlink_callback *cb, int s_ip_idx, |
David Ahern | 6ba1e6e | 2018-10-07 20:16:26 -0700 | [diff] [blame] | 5004 | struct inet6_fill_args *fillargs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5005 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5006 | struct ifmcaddr6 *ifmca; |
| 5007 | struct ifacaddr6 *ifaca; |
David Ahern | fe884c2 | 2018-10-19 12:45:28 -0700 | [diff] [blame] | 5008 | int ip_idx = 0; |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5009 | int err = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5010 | |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5011 | read_lock_bh(&idev->lock); |
David Ahern | 6ba1e6e | 2018-10-07 20:16:26 -0700 | [diff] [blame] | 5012 | switch (fillargs->type) { |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 5013 | case UNICAST_ADDR: { |
| 5014 | struct inet6_ifaddr *ifa; |
David Ahern | 6ba1e6e | 2018-10-07 20:16:26 -0700 | [diff] [blame] | 5015 | fillargs->event = RTM_NEWADDR; |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 5016 | |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5017 | /* unicast address incl. temp addr */ |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 5018 | list_for_each_entry(ifa, &idev->addr_list, if_list) { |
David Ahern | 4ba4c56 | 2018-10-19 10:00:19 -0700 | [diff] [blame] | 5019 | if (ip_idx < s_ip_idx) |
| 5020 | goto next; |
David Ahern | 6ba1e6e | 2018-10-07 20:16:26 -0700 | [diff] [blame] | 5021 | err = inet6_fill_ifaddr(skb, ifa, fillargs); |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 5022 | if (err < 0) |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5023 | break; |
Nicolas Dichtel | 63998ac | 2013-03-22 06:28:43 +0000 | [diff] [blame] | 5024 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
David Ahern | 4ba4c56 | 2018-10-19 10:00:19 -0700 | [diff] [blame] | 5025 | next: |
| 5026 | ip_idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | } |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5028 | break; |
stephen hemminger | 502a2ff | 2010-03-17 20:31:13 +0000 | [diff] [blame] | 5029 | } |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5030 | case MULTICAST_ADDR: |
David Ahern | 6ba1e6e | 2018-10-07 20:16:26 -0700 | [diff] [blame] | 5031 | fillargs->event = RTM_GETMULTICAST; |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 5032 | |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5033 | /* multicast address */ |
| 5034 | for (ifmca = idev->mc_list; ifmca; |
| 5035 | ifmca = ifmca->next, ip_idx++) { |
| 5036 | if (ip_idx < s_ip_idx) |
| 5037 | continue; |
David Ahern | 6ba1e6e | 2018-10-07 20:16:26 -0700 | [diff] [blame] | 5038 | err = inet6_fill_ifmcaddr(skb, ifmca, fillargs); |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 5039 | if (err < 0) |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5040 | break; |
| 5041 | } |
| 5042 | break; |
| 5043 | case ANYCAST_ADDR: |
David Ahern | 6ba1e6e | 2018-10-07 20:16:26 -0700 | [diff] [blame] | 5044 | fillargs->event = RTM_GETANYCAST; |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5045 | /* anycast address */ |
| 5046 | for (ifaca = idev->ac_list; ifaca; |
| 5047 | ifaca = ifaca->aca_next, ip_idx++) { |
| 5048 | if (ip_idx < s_ip_idx) |
| 5049 | continue; |
David Ahern | 6ba1e6e | 2018-10-07 20:16:26 -0700 | [diff] [blame] | 5050 | err = inet6_fill_ifacaddr(skb, ifaca, fillargs); |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 5051 | if (err < 0) |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5052 | break; |
| 5053 | } |
| 5054 | break; |
| 5055 | default: |
| 5056 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | } |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5058 | read_unlock_bh(&idev->lock); |
David Ahern | fe884c2 | 2018-10-19 12:45:28 -0700 | [diff] [blame] | 5059 | cb->args[2] = ip_idx; |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5060 | return err; |
| 5061 | } |
| 5062 | |
David Ahern | ed6eff1 | 2018-10-07 20:16:29 -0700 | [diff] [blame] | 5063 | static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh, |
| 5064 | struct inet6_fill_args *fillargs, |
| 5065 | struct net **tgt_net, struct sock *sk, |
David Ahern | 6371a71 | 2018-10-19 12:45:30 -0700 | [diff] [blame] | 5066 | struct netlink_callback *cb) |
David Ahern | ed6eff1 | 2018-10-07 20:16:29 -0700 | [diff] [blame] | 5067 | { |
David Ahern | 6371a71 | 2018-10-19 12:45:30 -0700 | [diff] [blame] | 5068 | struct netlink_ext_ack *extack = cb->extack; |
David Ahern | ed6eff1 | 2018-10-07 20:16:29 -0700 | [diff] [blame] | 5069 | struct nlattr *tb[IFA_MAX+1]; |
| 5070 | struct ifaddrmsg *ifm; |
| 5071 | int err, i; |
| 5072 | |
| 5073 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { |
| 5074 | NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request"); |
| 5075 | return -EINVAL; |
| 5076 | } |
| 5077 | |
| 5078 | ifm = nlmsg_data(nlh); |
| 5079 | if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { |
| 5080 | NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request"); |
| 5081 | return -EINVAL; |
| 5082 | } |
David Ahern | 6371a71 | 2018-10-19 12:45:30 -0700 | [diff] [blame] | 5083 | |
| 5084 | fillargs->ifindex = ifm->ifa_index; |
| 5085 | if (fillargs->ifindex) { |
| 5086 | cb->answer_flags |= NLM_F_DUMP_FILTERED; |
| 5087 | fillargs->flags |= NLM_F_DUMP_FILTERED; |
David Ahern | ed6eff1 | 2018-10-07 20:16:29 -0700 | [diff] [blame] | 5088 | } |
| 5089 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 5090 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, |
| 5091 | ifa_ipv6_policy, extack); |
David Ahern | ed6eff1 | 2018-10-07 20:16:29 -0700 | [diff] [blame] | 5092 | if (err < 0) |
| 5093 | return err; |
| 5094 | |
| 5095 | for (i = 0; i <= IFA_MAX; ++i) { |
| 5096 | if (!tb[i]) |
| 5097 | continue; |
| 5098 | |
| 5099 | if (i == IFA_TARGET_NETNSID) { |
| 5100 | struct net *net; |
| 5101 | |
| 5102 | fillargs->netnsid = nla_get_s32(tb[i]); |
| 5103 | net = rtnl_get_net_ns_capable(sk, fillargs->netnsid); |
| 5104 | if (IS_ERR(net)) { |
Bjørn Mork | bf4cc40 | 2018-10-25 21:18:25 +0200 | [diff] [blame] | 5105 | fillargs->netnsid = -1; |
David Ahern | ed6eff1 | 2018-10-07 20:16:29 -0700 | [diff] [blame] | 5106 | NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id"); |
| 5107 | return PTR_ERR(net); |
| 5108 | } |
| 5109 | *tgt_net = net; |
| 5110 | } else { |
| 5111 | NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request"); |
| 5112 | return -EINVAL; |
| 5113 | } |
| 5114 | } |
| 5115 | |
| 5116 | return 0; |
| 5117 | } |
| 5118 | |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5119 | static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, |
| 5120 | enum addr_type_t type) |
| 5121 | { |
David Ahern | ed6eff1 | 2018-10-07 20:16:29 -0700 | [diff] [blame] | 5122 | const struct nlmsghdr *nlh = cb->nlh; |
David Ahern | 6ba1e6e | 2018-10-07 20:16:26 -0700 | [diff] [blame] | 5123 | struct inet6_fill_args fillargs = { |
| 5124 | .portid = NETLINK_CB(cb->skb).portid, |
| 5125 | .seq = cb->nlh->nlmsg_seq, |
| 5126 | .flags = NLM_F_MULTI, |
| 5127 | .netnsid = -1, |
| 5128 | .type = type, |
| 5129 | }; |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5130 | struct net *net = sock_net(skb->sk); |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 5131 | struct net *tgt_net = net; |
David Ahern | fe884c2 | 2018-10-19 12:45:28 -0700 | [diff] [blame] | 5132 | int idx, s_idx, s_ip_idx; |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5133 | int h, s_h; |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5134 | struct net_device *dev; |
| 5135 | struct inet6_dev *idev; |
| 5136 | struct hlist_head *head; |
David Ahern | 242afaa | 2018-10-24 12:59:00 -0700 | [diff] [blame] | 5137 | int err = 0; |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5138 | |
| 5139 | s_h = cb->args[0]; |
| 5140 | s_idx = idx = cb->args[1]; |
David Ahern | fe884c2 | 2018-10-19 12:45:28 -0700 | [diff] [blame] | 5141 | s_ip_idx = cb->args[2]; |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5142 | |
David Ahern | ed6eff1 | 2018-10-07 20:16:29 -0700 | [diff] [blame] | 5143 | if (cb->strict_check) { |
David Ahern | ed6eff1 | 2018-10-07 20:16:29 -0700 | [diff] [blame] | 5144 | err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net, |
David Ahern | 6371a71 | 2018-10-19 12:45:30 -0700 | [diff] [blame] | 5145 | skb->sk, cb); |
David Ahern | ed6eff1 | 2018-10-07 20:16:29 -0700 | [diff] [blame] | 5146 | if (err < 0) |
David Ahern | 242afaa | 2018-10-24 12:59:00 -0700 | [diff] [blame] | 5147 | goto put_tgt_net; |
David Ahern | 6371a71 | 2018-10-19 12:45:30 -0700 | [diff] [blame] | 5148 | |
David Ahern | 242afaa | 2018-10-24 12:59:00 -0700 | [diff] [blame] | 5149 | err = 0; |
David Ahern | 6371a71 | 2018-10-19 12:45:30 -0700 | [diff] [blame] | 5150 | if (fillargs.ifindex) { |
| 5151 | dev = __dev_get_by_index(tgt_net, fillargs.ifindex); |
David Ahern | 242afaa | 2018-10-24 12:59:00 -0700 | [diff] [blame] | 5152 | if (!dev) { |
| 5153 | err = -ENODEV; |
| 5154 | goto put_tgt_net; |
| 5155 | } |
David Ahern | 6371a71 | 2018-10-19 12:45:30 -0700 | [diff] [blame] | 5156 | idev = __in6_dev_get(dev); |
| 5157 | if (idev) { |
| 5158 | err = in6_dump_addrs(idev, skb, cb, s_ip_idx, |
| 5159 | &fillargs); |
Jakub Kicinski | 1518039 | 2019-01-22 14:47:19 -0800 | [diff] [blame] | 5160 | if (err > 0) |
| 5161 | err = 0; |
David Ahern | 6371a71 | 2018-10-19 12:45:30 -0700 | [diff] [blame] | 5162 | } |
| 5163 | goto put_tgt_net; |
| 5164 | } |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 5165 | } |
| 5166 | |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5167 | rcu_read_lock(); |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 5168 | cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq; |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5169 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 5170 | idx = 0; |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 5171 | head = &tgt_net->dev_index_head[h]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5172 | hlist_for_each_entry_rcu(dev, head, index_hlist) { |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5173 | if (idx < s_idx) |
| 5174 | goto cont; |
Patrick McHardy | 4b97efd | 2010-03-26 20:27:49 -0700 | [diff] [blame] | 5175 | if (h > s_h || idx > s_idx) |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5176 | s_ip_idx = 0; |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 5177 | idev = __in6_dev_get(dev); |
| 5178 | if (!idev) |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5179 | goto cont; |
| 5180 | |
David Ahern | fe884c2 | 2018-10-19 12:45:28 -0700 | [diff] [blame] | 5181 | if (in6_dump_addrs(idev, skb, cb, s_ip_idx, |
David Ahern | 6ba1e6e | 2018-10-07 20:16:26 -0700 | [diff] [blame] | 5182 | &fillargs) < 0) |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5183 | goto done; |
| 5184 | cont: |
| 5185 | idx++; |
| 5186 | } |
| 5187 | } |
| 5188 | done: |
| 5189 | rcu_read_unlock(); |
| 5190 | cb->args[0] = h; |
| 5191 | cb->args[1] = idx; |
David Ahern | 6371a71 | 2018-10-19 12:45:30 -0700 | [diff] [blame] | 5192 | put_tgt_net: |
David Ahern | 6ba1e6e | 2018-10-07 20:16:26 -0700 | [diff] [blame] | 5193 | if (fillargs.netnsid >= 0) |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 5194 | put_net(tgt_net); |
Eric Dumazet | 234b27c | 2009-11-12 04:11:50 +0000 | [diff] [blame] | 5195 | |
Arthur Gautier | 7c1e8a3 | 2018-12-31 02:10:58 +0000 | [diff] [blame] | 5196 | return skb->len ? : err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5197 | } |
| 5198 | |
| 5199 | static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) |
| 5200 | { |
| 5201 | enum addr_type_t type = UNICAST_ADDR; |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 5202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | return inet6_dump_addr(skb, cb, type); |
| 5204 | } |
| 5205 | |
| 5206 | static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) |
| 5207 | { |
| 5208 | enum addr_type_t type = MULTICAST_ADDR; |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 5209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5210 | return inet6_dump_addr(skb, cb, type); |
| 5211 | } |
| 5212 | |
| 5213 | |
| 5214 | static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) |
| 5215 | { |
| 5216 | enum addr_type_t type = ANYCAST_ADDR; |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 5217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | return inet6_dump_addr(skb, cb, type); |
| 5219 | } |
| 5220 | |
Jakub Kicinski | 4b1373d | 2019-01-18 10:46:21 -0800 | [diff] [blame] | 5221 | static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb, |
| 5222 | const struct nlmsghdr *nlh, |
| 5223 | struct nlattr **tb, |
| 5224 | struct netlink_ext_ack *extack) |
| 5225 | { |
| 5226 | struct ifaddrmsg *ifm; |
| 5227 | int i, err; |
| 5228 | |
| 5229 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { |
| 5230 | NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request"); |
| 5231 | return -EINVAL; |
| 5232 | } |
| 5233 | |
| 5234 | ifm = nlmsg_data(nlh); |
| 5235 | if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { |
| 5236 | NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request"); |
| 5237 | return -EINVAL; |
| 5238 | } |
| 5239 | |
| 5240 | if (!netlink_strict_get_check(skb)) |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 5241 | return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, |
| 5242 | ifa_ipv6_policy, extack); |
Jakub Kicinski | 4b1373d | 2019-01-18 10:46:21 -0800 | [diff] [blame] | 5243 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 5244 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, |
| 5245 | ifa_ipv6_policy, extack); |
Jakub Kicinski | 4b1373d | 2019-01-18 10:46:21 -0800 | [diff] [blame] | 5246 | if (err) |
| 5247 | return err; |
| 5248 | |
| 5249 | for (i = 0; i <= IFA_MAX; i++) { |
| 5250 | if (!tb[i]) |
| 5251 | continue; |
| 5252 | |
| 5253 | switch (i) { |
| 5254 | case IFA_TARGET_NETNSID: |
| 5255 | case IFA_ADDRESS: |
| 5256 | case IFA_LOCAL: |
| 5257 | break; |
| 5258 | default: |
| 5259 | NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request"); |
| 5260 | return -EINVAL; |
| 5261 | } |
| 5262 | } |
| 5263 | |
| 5264 | return 0; |
| 5265 | } |
| 5266 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 5267 | static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh, |
| 5268 | struct netlink_ext_ack *extack) |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 5269 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 5270 | struct net *net = sock_net(in_skb->sk); |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 5271 | struct inet6_fill_args fillargs = { |
| 5272 | .portid = NETLINK_CB(in_skb).portid, |
| 5273 | .seq = nlh->nlmsg_seq, |
| 5274 | .event = RTM_NEWADDR, |
| 5275 | .flags = 0, |
| 5276 | .netnsid = -1, |
| 5277 | }; |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 5278 | struct net *tgt_net = net; |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 5279 | struct ifaddrmsg *ifm; |
| 5280 | struct nlattr *tb[IFA_MAX+1]; |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 5281 | struct in6_addr *addr = NULL, *peer; |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 5282 | struct net_device *dev = NULL; |
| 5283 | struct inet6_ifaddr *ifa; |
| 5284 | struct sk_buff *skb; |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 5285 | int err; |
| 5286 | |
Jakub Kicinski | 4b1373d | 2019-01-18 10:46:21 -0800 | [diff] [blame] | 5287 | err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack); |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 5288 | if (err < 0) |
Florian Westphal | c24675f | 2017-10-11 10:28:01 +0200 | [diff] [blame] | 5289 | return err; |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 5290 | |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 5291 | if (tb[IFA_TARGET_NETNSID]) { |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 5292 | fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]); |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 5293 | |
| 5294 | tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk, |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 5295 | fillargs.netnsid); |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 5296 | if (IS_ERR(tgt_net)) |
| 5297 | return PTR_ERR(tgt_net); |
| 5298 | } |
| 5299 | |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 5300 | addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); |
Florian Westphal | c24675f | 2017-10-11 10:28:01 +0200 | [diff] [blame] | 5301 | if (!addr) |
| 5302 | return -EINVAL; |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 5303 | |
| 5304 | ifm = nlmsg_data(nlh); |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 5305 | if (ifm->ifa_index) |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 5306 | dev = dev_get_by_index(tgt_net, ifm->ifa_index); |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 5307 | |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 5308 | ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1); |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 5309 | if (!ifa) { |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 5310 | err = -EADDRNOTAVAIL; |
| 5311 | goto errout; |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 5312 | } |
| 5313 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 5314 | skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); |
| 5315 | if (!skb) { |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 5316 | err = -ENOBUFS; |
| 5317 | goto errout_ifa; |
| 5318 | } |
| 5319 | |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 5320 | err = inet6_fill_ifaddr(skb, ifa, &fillargs); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5321 | if (err < 0) { |
| 5322 | /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ |
| 5323 | WARN_ON(err == -EMSGSIZE); |
| 5324 | kfree_skb(skb); |
| 5325 | goto errout_ifa; |
| 5326 | } |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 5327 | err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid); |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 5328 | errout_ifa: |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 5329 | in6_ifa_put(ifa); |
Thomas Graf | 1b29fc2 | 2006-09-18 00:10:50 -0700 | [diff] [blame] | 5330 | errout: |
Florian Westphal | c24675f | 2017-10-11 10:28:01 +0200 | [diff] [blame] | 5331 | if (dev) |
| 5332 | dev_put(dev); |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 5333 | if (fillargs.netnsid >= 0) |
Christian Brauner | 6ecf4c3 | 2018-09-04 21:53:50 +0200 | [diff] [blame] | 5334 | put_net(tgt_net); |
| 5335 | |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 5336 | return err; |
Noriaki TAKAMIYA | 6c22382 | 2006-07-28 18:12:12 +0900 | [diff] [blame] | 5337 | } |
| 5338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) |
| 5340 | { |
| 5341 | struct sk_buff *skb; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 5342 | struct net *net = dev_net(ifa->idev->dev); |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 5343 | struct inet6_fill_args fillargs = { |
| 5344 | .portid = 0, |
| 5345 | .seq = 0, |
| 5346 | .event = event, |
| 5347 | .flags = 0, |
| 5348 | .netnsid = -1, |
| 5349 | }; |
Thomas Graf | 5d62026 | 2006-08-15 00:35:02 -0700 | [diff] [blame] | 5350 | int err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5351 | |
Thomas Graf | 0ab6803 | 2006-09-18 00:12:35 -0700 | [diff] [blame] | 5352 | skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 5353 | if (!skb) |
Thomas Graf | 5d62026 | 2006-08-15 00:35:02 -0700 | [diff] [blame] | 5354 | goto errout; |
| 5355 | |
Christian Brauner | 203651b | 2018-09-04 21:53:55 +0200 | [diff] [blame] | 5356 | err = inet6_fill_ifaddr(skb, ifa, &fillargs); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5357 | if (err < 0) { |
| 5358 | /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ |
| 5359 | WARN_ON(err == -EMSGSIZE); |
| 5360 | kfree_skb(skb); |
| 5361 | goto errout; |
| 5362 | } |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 5363 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); |
| 5364 | return; |
Thomas Graf | 5d62026 | 2006-08-15 00:35:02 -0700 | [diff] [blame] | 5365 | errout: |
| 5366 | if (err < 0) |
Benjamin Thery | 6fda735 | 2008-03-05 10:47:47 -0800 | [diff] [blame] | 5367 | rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5368 | } |
| 5369 | |
Dave Jones | b6f99a2 | 2007-03-22 12:27:49 -0700 | [diff] [blame] | 5370 | static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | __s32 *array, int bytes) |
| 5372 | { |
Thomas Graf | 04561c1 | 2006-11-14 19:53:58 -0800 | [diff] [blame] | 5373 | BUG_ON(bytes < (DEVCONF_MAX * 4)); |
| 5374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5375 | memset(array, 0, bytes); |
| 5376 | array[DEVCONF_FORWARDING] = cnf->forwarding; |
| 5377 | array[DEVCONF_HOPLIMIT] = cnf->hop_limit; |
| 5378 | array[DEVCONF_MTU6] = cnf->mtu6; |
| 5379 | array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; |
| 5380 | array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; |
| 5381 | array[DEVCONF_AUTOCONF] = cnf->autoconf; |
| 5382 | array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; |
| 5383 | array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; |
Thomas Graf | 93908d1 | 2010-11-17 01:44:24 +0000 | [diff] [blame] | 5384 | array[DEVCONF_RTR_SOLICIT_INTERVAL] = |
| 5385 | jiffies_to_msecs(cnf->rtr_solicit_interval); |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 5386 | array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] = |
| 5387 | jiffies_to_msecs(cnf->rtr_solicit_max_interval); |
Thomas Graf | 93908d1 | 2010-11-17 01:44:24 +0000 | [diff] [blame] | 5388 | array[DEVCONF_RTR_SOLICIT_DELAY] = |
| 5389 | jiffies_to_msecs(cnf->rtr_solicit_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; |
Hannes Frederic Sowa | fc4eba5 | 2013-08-14 01:03:46 +0200 | [diff] [blame] | 5391 | array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] = |
| 5392 | jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); |
| 5393 | array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = |
| 5394 | jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5395 | array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; |
| 5396 | array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; |
| 5397 | array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; |
| 5398 | array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; |
| 5399 | array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5400 | array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; |
YOSHIFUJI Hideaki | 65f5c7c | 2006-03-20 16:55:08 -0800 | [diff] [blame] | 5401 | array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; |
Hangbin Liu | 8013d1d7 | 2015-07-30 14:28:42 +0800 | [diff] [blame] | 5402 | array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit; |
YOSHIFUJI Hideaki | c4fd30e | 2006-03-20 16:55:26 -0800 | [diff] [blame] | 5403 | array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 5404 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 5405 | array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; |
Thomas Graf | 93908d1 | 2010-11-17 01:44:24 +0000 | [diff] [blame] | 5406 | array[DEVCONF_RTR_PROBE_INTERVAL] = |
| 5407 | jiffies_to_msecs(cnf->rtr_probe_interval); |
Neil Horman | fa03ef3 | 2007-01-30 14:30:10 -0800 | [diff] [blame] | 5408 | #ifdef CONFIG_IPV6_ROUTE_INFO |
Joel Scherpelz | bbea124 | 2017-03-22 18:19:04 +0900 | [diff] [blame] | 5409 | array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen; |
YOSHIFUJI Hideaki | 09c884d | 2006-03-20 17:07:03 -0800 | [diff] [blame] | 5410 | array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; |
| 5411 | #endif |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 5412 | #endif |
YOSHIFUJI Hideaki | fbea49e | 2006-09-22 14:43:49 -0700 | [diff] [blame] | 5413 | array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 5414 | array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 5415 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
| 5416 | array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; |
Erik Kline | 7fd2561 | 2014-10-28 18:11:14 +0900 | [diff] [blame] | 5417 | array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 5418 | #endif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 5419 | #ifdef CONFIG_IPV6_MROUTE |
| 5420 | array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; |
| 5421 | #endif |
YOSHIFUJI Hideaki | 778d80b | 2008-06-28 14:17:11 +0900 | [diff] [blame] | 5422 | array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; |
YOSHIFUJI Hideaki | 1b34be7 | 2008-06-28 14:18:38 +0900 | [diff] [blame] | 5423 | array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; |
Cosmin Ratiu | c3faca0 | 2009-10-09 03:11:14 +0000 | [diff] [blame] | 5424 | array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; |
Hannes Frederic Sowa | 5cb0443 | 2012-11-06 16:46:20 +0000 | [diff] [blame] | 5425 | array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; |
Hannes Frederic Sowa | b800c3b | 2013-08-27 01:36:51 +0200 | [diff] [blame] | 5426 | array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; |
Ben Greear | d933319 | 2014-06-25 14:44:53 -0700 | [diff] [blame] | 5427 | array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; |
Harout Hedeshian | c2943f1 | 2015-01-20 10:06:05 -0700 | [diff] [blame] | 5428 | array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 5429 | array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown; |
Hannes Frederic Sowa | 3d1bec9 | 2015-03-23 23:36:00 +0100 | [diff] [blame] | 5430 | /* we omit DEVCONF_STABLE_SECRET for now */ |
Erik Kline | 3985e8a | 2015-07-22 16:38:25 +0900 | [diff] [blame] | 5431 | array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only; |
Johannes Berg | abbc304 | 2016-02-04 13:31:19 +0100 | [diff] [blame] | 5432 | array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast; |
Johannes Berg | 7a02bf8 | 2016-02-04 13:31:20 +0100 | [diff] [blame] | 5433 | array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na; |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 5434 | array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down; |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 5435 | array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled; |
David Lebrun | bf355b8 | 2016-11-08 14:57:42 +0100 | [diff] [blame] | 5436 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 5437 | array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac; |
| 5438 | #endif |
Erik Nordmark | adc176c | 2016-12-02 14:00:08 -0800 | [diff] [blame] | 5439 | array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad; |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 5440 | array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode; |
David Forster | df789fe | 2017-02-23 16:27:18 +0000 | [diff] [blame] | 5441 | array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy; |
Maciej Żenczykowski | 2210d6b | 2017-11-07 21:52:09 -0800 | [diff] [blame] | 5442 | array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5443 | } |
| 5444 | |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5445 | static inline size_t inet6_ifla6_size(void) |
| 5446 | { |
| 5447 | return nla_total_size(4) /* IFLA_INET6_FLAGS */ |
| 5448 | + nla_total_size(sizeof(struct ifla_cacheinfo)) |
| 5449 | + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ |
| 5450 | + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5451 | + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ |
Sabrina Dubroca | bdd72f4 | 2018-07-09 12:25:16 +0200 | [diff] [blame] | 5452 | + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */ |
| 5453 | + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */ |
| 5454 | + 0; |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5455 | } |
| 5456 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 5457 | static inline size_t inet6_if_nlmsg_size(void) |
| 5458 | { |
| 5459 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) |
| 5460 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ |
| 5461 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ |
| 5462 | + nla_total_size(4) /* IFLA_MTU */ |
| 5463 | + nla_total_size(4) /* IFLA_LINK */ |
Andy Gospodarek | 0344338 | 2015-08-13 15:26:35 -0400 | [diff] [blame] | 5464 | + nla_total_size(1) /* IFLA_OPERSTATE */ |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5465 | + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 5466 | } |
YOSHIFUJI Hideaki | c5396a3 | 2006-06-17 22:48:48 -0700 | [diff] [blame] | 5467 | |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 5468 | static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, |
Jia He | aca0567 | 2016-09-30 11:29:03 +0800 | [diff] [blame] | 5469 | int bytes) |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 5470 | { |
| 5471 | int i; |
Jia He | aca0567 | 2016-09-30 11:29:03 +0800 | [diff] [blame] | 5472 | int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX; |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 5473 | BUG_ON(pad < 0); |
| 5474 | |
| 5475 | /* Use put_unaligned() because stats may not be aligned for u64. */ |
Jia He | aca0567 | 2016-09-30 11:29:03 +0800 | [diff] [blame] | 5476 | put_unaligned(ICMP6_MIB_MAX, &stats[0]); |
| 5477 | for (i = 1; i < ICMP6_MIB_MAX; i++) |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 5478 | put_unaligned(atomic_long_read(&mib[i]), &stats[i]); |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 5479 | |
Jia He | aca0567 | 2016-09-30 11:29:03 +0800 | [diff] [blame] | 5480 | memset(&stats[ICMP6_MIB_MAX], 0, pad); |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 5481 | } |
| 5482 | |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 5483 | static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib, |
Raghavendra K T | a3a7737 | 2015-08-30 11:29:42 +0530 | [diff] [blame] | 5484 | int bytes, size_t syncpoff) |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 5485 | { |
Raghavendra K T | a3a7737 | 2015-08-30 11:29:42 +0530 | [diff] [blame] | 5486 | int i, c; |
| 5487 | u64 buff[IPSTATS_MIB_MAX]; |
| 5488 | int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX; |
| 5489 | |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 5490 | BUG_ON(pad < 0); |
| 5491 | |
Raghavendra K T | a3a7737 | 2015-08-30 11:29:42 +0530 | [diff] [blame] | 5492 | memset(buff, 0, sizeof(buff)); |
| 5493 | buff[0] = IPSTATS_MIB_MAX; |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 5494 | |
Raghavendra K T | a3a7737 | 2015-08-30 11:29:42 +0530 | [diff] [blame] | 5495 | for_each_possible_cpu(c) { |
| 5496 | for (i = 1; i < IPSTATS_MIB_MAX; i++) |
| 5497 | buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff); |
| 5498 | } |
| 5499 | |
| 5500 | memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64)); |
| 5501 | memset(&stats[IPSTATS_MIB_MAX], 0, pad); |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 5502 | } |
| 5503 | |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 5504 | static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, |
| 5505 | int bytes) |
| 5506 | { |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 5507 | switch (attrtype) { |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 5508 | case IFLA_INET6_STATS: |
Raghavendra K T | a3a7737 | 2015-08-30 11:29:42 +0530 | [diff] [blame] | 5509 | __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes, |
| 5510 | offsetof(struct ipstats_mib, syncp)); |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 5511 | break; |
| 5512 | case IFLA_INET6_ICMP6STATS: |
Jia He | aca0567 | 2016-09-30 11:29:03 +0800 | [diff] [blame] | 5513 | __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes); |
Herbert Xu | 7f7d9a6 | 2007-04-24 21:54:09 -0700 | [diff] [blame] | 5514 | break; |
| 5515 | } |
| 5516 | } |
| 5517 | |
Sowmini Varadhan | d5566fd | 2015-09-11 16:48:48 -0400 | [diff] [blame] | 5518 | static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev, |
| 5519 | u32 ext_filter_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | { |
YOSHIFUJI Hideaki | bf99f1b | 2007-04-20 15:56:20 -0700 | [diff] [blame] | 5521 | struct nlattr *nla; |
Thomas Graf | 04561c1 | 2006-11-14 19:53:58 -0800 | [diff] [blame] | 5522 | struct ifla_cacheinfo ci; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5523 | |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5524 | if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) |
| 5525 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | ci.max_reasm_len = IPV6_MAXPLEN; |
David S. Miller | 2491242 | 2010-11-19 13:13:47 -0800 | [diff] [blame] | 5527 | ci.tstamp = cstamp_delta(idev->tstamp); |
| 5528 | ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); |
Jiri Pirko | 1f9248e | 2013-12-07 19:26:53 +0100 | [diff] [blame] | 5529 | ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME)); |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5530 | if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) |
| 5531 | goto nla_put_failure; |
YOSHIFUJI Hideaki | bf99f1b | 2007-04-20 15:56:20 -0700 | [diff] [blame] | 5532 | nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 5533 | if (!nla) |
Thomas Graf | 04561c1 | 2006-11-14 19:53:58 -0800 | [diff] [blame] | 5534 | goto nla_put_failure; |
YOSHIFUJI Hideaki | bf99f1b | 2007-04-20 15:56:20 -0700 | [diff] [blame] | 5535 | ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | |
YOSHIFUJI Hideaki | bf99f1b | 2007-04-20 15:56:20 -0700 | [diff] [blame] | 5537 | /* XXX - MC not implemented */ |
| 5538 | |
Sowmini Varadhan | d5566fd | 2015-09-11 16:48:48 -0400 | [diff] [blame] | 5539 | if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS) |
| 5540 | return 0; |
| 5541 | |
YOSHIFUJI Hideaki | bf99f1b | 2007-04-20 15:56:20 -0700 | [diff] [blame] | 5542 | nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 5543 | if (!nla) |
YOSHIFUJI Hideaki | bf99f1b | 2007-04-20 15:56:20 -0700 | [diff] [blame] | 5544 | goto nla_put_failure; |
| 5545 | snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); |
| 5546 | |
| 5547 | nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 5548 | if (!nla) |
YOSHIFUJI Hideaki | bf99f1b | 2007-04-20 15:56:20 -0700 | [diff] [blame] | 5549 | goto nla_put_failure; |
| 5550 | snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5551 | |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5552 | nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 5553 | if (!nla) |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5554 | goto nla_put_failure; |
Jiri Pirko | bc91b0f | 2014-07-11 21:10:18 +0200 | [diff] [blame] | 5555 | |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 5556 | if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode)) |
Jiri Pirko | bc91b0f | 2014-07-11 21:10:18 +0200 | [diff] [blame] | 5557 | goto nla_put_failure; |
| 5558 | |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5559 | read_lock_bh(&idev->lock); |
| 5560 | memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); |
| 5561 | read_unlock_bh(&idev->lock); |
| 5562 | |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5563 | return 0; |
| 5564 | |
| 5565 | nla_put_failure: |
| 5566 | return -EMSGSIZE; |
| 5567 | } |
| 5568 | |
Arad, Ronen | b1974ed | 2015-10-19 09:23:28 -0700 | [diff] [blame] | 5569 | static size_t inet6_get_link_af_size(const struct net_device *dev, |
| 5570 | u32 ext_filter_mask) |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5571 | { |
| 5572 | if (!__in6_dev_get(dev)) |
| 5573 | return 0; |
| 5574 | |
| 5575 | return inet6_ifla6_size(); |
| 5576 | } |
| 5577 | |
Sowmini Varadhan | d5566fd | 2015-09-11 16:48:48 -0400 | [diff] [blame] | 5578 | static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev, |
| 5579 | u32 ext_filter_mask) |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5580 | { |
| 5581 | struct inet6_dev *idev = __in6_dev_get(dev); |
| 5582 | |
| 5583 | if (!idev) |
| 5584 | return -ENODATA; |
| 5585 | |
Sowmini Varadhan | d5566fd | 2015-09-11 16:48:48 -0400 | [diff] [blame] | 5586 | if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0) |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5587 | return -EMSGSIZE; |
| 5588 | |
| 5589 | return 0; |
| 5590 | } |
| 5591 | |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5592 | static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) |
| 5593 | { |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5594 | struct inet6_ifaddr *ifp; |
| 5595 | struct net_device *dev = idev->dev; |
Daniel Borkmann | 47e27d5 | 2016-04-08 15:55:00 +0200 | [diff] [blame] | 5596 | bool clear_token, update_rs = false; |
Hannes Frederic Sowa | dc84829 | 2013-06-24 21:42:40 +0200 | [diff] [blame] | 5597 | struct in6_addr ll_addr; |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5598 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 5599 | ASSERT_RTNL(); |
| 5600 | |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 5601 | if (!token) |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5602 | return -EINVAL; |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5603 | if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) |
| 5604 | return -EINVAL; |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5605 | if (!ipv6_accept_ra(idev)) |
| 5606 | return -EINVAL; |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 5607 | if (idev->cnf.rtr_solicits == 0) |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5608 | return -EINVAL; |
| 5609 | |
| 5610 | write_lock_bh(&idev->lock); |
| 5611 | |
| 5612 | BUILD_BUG_ON(sizeof(token->s6_addr) != 16); |
| 5613 | memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); |
| 5614 | |
| 5615 | write_unlock_bh(&idev->lock); |
| 5616 | |
Daniel Borkmann | 47e27d5 | 2016-04-08 15:55:00 +0200 | [diff] [blame] | 5617 | clear_token = ipv6_addr_any(token); |
| 5618 | if (clear_token) |
| 5619 | goto update_lft; |
| 5620 | |
Hannes Frederic Sowa | dc84829 | 2013-06-24 21:42:40 +0200 | [diff] [blame] | 5621 | if (!idev->dead && (idev->if_flags & IF_READY) && |
| 5622 | !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | |
| 5623 | IFA_F_OPTIMISTIC)) { |
Daniel Borkmann | fc40383 | 2013-04-09 03:47:15 +0000 | [diff] [blame] | 5624 | /* If we're not ready, then normal ifup will take care |
| 5625 | * of this. Otherwise, we need to request our rs here. |
| 5626 | */ |
| 5627 | ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); |
| 5628 | update_rs = true; |
| 5629 | } |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5630 | |
Daniel Borkmann | 47e27d5 | 2016-04-08 15:55:00 +0200 | [diff] [blame] | 5631 | update_lft: |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5632 | write_lock_bh(&idev->lock); |
Daniel Borkmann | fc40383 | 2013-04-09 03:47:15 +0000 | [diff] [blame] | 5633 | |
Hannes Frederic Sowa | 77ecaac | 2013-06-26 03:41:49 +0200 | [diff] [blame] | 5634 | if (update_rs) { |
Daniel Borkmann | fc40383 | 2013-04-09 03:47:15 +0000 | [diff] [blame] | 5635 | idev->if_flags |= IF_RS_SENT; |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 5636 | idev->rs_interval = rfc3315_s14_backoff_init( |
| 5637 | idev->cnf.rtr_solicit_interval); |
Hannes Frederic Sowa | 77ecaac | 2013-06-26 03:41:49 +0200 | [diff] [blame] | 5638 | idev->rs_probes = 1; |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 5639 | addrconf_mod_rs_timer(idev, idev->rs_interval); |
Hannes Frederic Sowa | 77ecaac | 2013-06-26 03:41:49 +0200 | [diff] [blame] | 5640 | } |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5641 | |
| 5642 | /* Well, that's kinda nasty ... */ |
| 5643 | list_for_each_entry(ifp, &idev->addr_list, if_list) { |
| 5644 | spin_lock(&ifp->lock); |
Daniel Borkmann | 617fe29 | 2013-04-09 03:47:16 +0000 | [diff] [blame] | 5645 | if (ifp->tokenized) { |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5646 | ifp->valid_lft = 0; |
| 5647 | ifp->prefered_lft = 0; |
| 5648 | } |
| 5649 | spin_unlock(&ifp->lock); |
| 5650 | } |
| 5651 | |
| 5652 | write_unlock_bh(&idev->lock); |
Lubomir Rintel | b2ed64a | 2014-10-27 17:39:16 +0100 | [diff] [blame] | 5653 | inet6_ifinfo_notify(RTM_NEWLINK, idev); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 5654 | addrconf_verify_rtnl(); |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5655 | return 0; |
| 5656 | } |
| 5657 | |
Daniel Borkmann | 11b1f82 | 2015-02-05 14:39:11 +0100 | [diff] [blame] | 5658 | static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = { |
| 5659 | [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 }, |
| 5660 | [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) }, |
| 5661 | }; |
| 5662 | |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 5663 | static int check_addr_gen_mode(int mode) |
| 5664 | { |
| 5665 | if (mode != IN6_ADDR_GEN_MODE_EUI64 && |
| 5666 | mode != IN6_ADDR_GEN_MODE_NONE && |
| 5667 | mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY && |
| 5668 | mode != IN6_ADDR_GEN_MODE_RANDOM) |
| 5669 | return -EINVAL; |
| 5670 | return 1; |
| 5671 | } |
| 5672 | |
| 5673 | static int check_stable_privacy(struct inet6_dev *idev, struct net *net, |
| 5674 | int mode) |
| 5675 | { |
| 5676 | if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY && |
| 5677 | !idev->cnf.stable_secret.initialized && |
| 5678 | !net->ipv6.devconf_dflt->stable_secret.initialized) |
| 5679 | return -EINVAL; |
| 5680 | return 1; |
| 5681 | } |
| 5682 | |
Maxim Mikityanskiy | 7dc2bcc | 2019-05-21 06:40:04 +0000 | [diff] [blame] | 5683 | static int inet6_validate_link_af(const struct net_device *dev, |
| 5684 | const struct nlattr *nla) |
| 5685 | { |
| 5686 | struct nlattr *tb[IFLA_INET6_MAX + 1]; |
| 5687 | struct inet6_dev *idev = NULL; |
| 5688 | int err; |
| 5689 | |
| 5690 | if (dev) { |
| 5691 | idev = __in6_dev_get(dev); |
| 5692 | if (!idev) |
| 5693 | return -EAFNOSUPPORT; |
| 5694 | } |
| 5695 | |
| 5696 | err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, |
| 5697 | inet6_af_policy, NULL); |
| 5698 | if (err) |
| 5699 | return err; |
| 5700 | |
| 5701 | if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE]) |
| 5702 | return -EINVAL; |
| 5703 | |
| 5704 | if (tb[IFLA_INET6_ADDR_GEN_MODE]) { |
| 5705 | u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); |
| 5706 | |
| 5707 | if (check_addr_gen_mode(mode) < 0) |
| 5708 | return -EINVAL; |
| 5709 | if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0) |
| 5710 | return -EINVAL; |
| 5711 | } |
| 5712 | |
| 5713 | return 0; |
| 5714 | } |
| 5715 | |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5716 | static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) |
| 5717 | { |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5718 | struct inet6_dev *idev = __in6_dev_get(dev); |
| 5719 | struct nlattr *tb[IFLA_INET6_MAX + 1]; |
Maxim Mikityanskiy | 7dc2bcc | 2019-05-21 06:40:04 +0000 | [diff] [blame] | 5720 | int err; |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5721 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 5722 | if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0) |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5723 | BUG(); |
| 5724 | |
Jiri Pirko | bc91b0f | 2014-07-11 21:10:18 +0200 | [diff] [blame] | 5725 | if (tb[IFLA_INET6_TOKEN]) { |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5726 | err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); |
Jiri Pirko | bc91b0f | 2014-07-11 21:10:18 +0200 | [diff] [blame] | 5727 | if (err) |
| 5728 | return err; |
| 5729 | } |
| 5730 | |
| 5731 | if (tb[IFLA_INET6_ADDR_GEN_MODE]) { |
| 5732 | u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); |
| 5733 | |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 5734 | idev->cnf.addr_gen_mode = mode; |
Jiri Pirko | bc91b0f | 2014-07-11 21:10:18 +0200 | [diff] [blame] | 5735 | } |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5736 | |
Maxim Mikityanskiy | 7dc2bcc | 2019-05-21 06:40:04 +0000 | [diff] [blame] | 5737 | return 0; |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 5738 | } |
| 5739 | |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5740 | static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5741 | u32 portid, u32 seq, int event, unsigned int flags) |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5742 | { |
| 5743 | struct net_device *dev = idev->dev; |
| 5744 | struct ifinfomsg *hdr; |
| 5745 | struct nlmsghdr *nlh; |
| 5746 | void *protoinfo; |
| 5747 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5748 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 5749 | if (!nlh) |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5750 | return -EMSGSIZE; |
| 5751 | |
| 5752 | hdr = nlmsg_data(nlh); |
| 5753 | hdr->ifi_family = AF_INET6; |
| 5754 | hdr->__ifi_pad = 0; |
| 5755 | hdr->ifi_type = dev->type; |
| 5756 | hdr->ifi_index = dev->ifindex; |
| 5757 | hdr->ifi_flags = dev_get_flags(dev); |
| 5758 | hdr->ifi_change = 0; |
| 5759 | |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5760 | if (nla_put_string(skb, IFLA_IFNAME, dev->name) || |
| 5761 | (dev->addr_len && |
| 5762 | nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || |
| 5763 | nla_put_u32(skb, IFLA_MTU, dev->mtu) || |
Nicolas Dichtel | a54acb3 | 2015-04-02 17:07:00 +0200 | [diff] [blame] | 5764 | (dev->ifindex != dev_get_iflink(dev) && |
Andy Gospodarek | 0344338 | 2015-08-13 15:26:35 -0400 | [diff] [blame] | 5765 | nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || |
| 5766 | nla_put_u8(skb, IFLA_OPERSTATE, |
| 5767 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5768 | goto nla_put_failure; |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 5769 | protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 5770 | if (!protoinfo) |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5771 | goto nla_put_failure; |
| 5772 | |
Sowmini Varadhan | d5566fd | 2015-09-11 16:48:48 -0400 | [diff] [blame] | 5773 | if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0) |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 5774 | goto nla_put_failure; |
| 5775 | |
Thomas Graf | 04561c1 | 2006-11-14 19:53:58 -0800 | [diff] [blame] | 5776 | nla_nest_end(skb, protoinfo); |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 5777 | nlmsg_end(skb, nlh); |
| 5778 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | |
Thomas Graf | 04561c1 | 2006-11-14 19:53:58 -0800 | [diff] [blame] | 5780 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5781 | nlmsg_cancel(skb, nlh); |
| 5782 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5783 | } |
| 5784 | |
David Ahern | 786e000 | 2018-10-07 20:16:33 -0700 | [diff] [blame] | 5785 | static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh, |
| 5786 | struct netlink_ext_ack *extack) |
| 5787 | { |
| 5788 | struct ifinfomsg *ifm; |
| 5789 | |
| 5790 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { |
| 5791 | NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request"); |
| 5792 | return -EINVAL; |
| 5793 | } |
| 5794 | |
| 5795 | if (nlmsg_attrlen(nlh, sizeof(*ifm))) { |
| 5796 | NL_SET_ERR_MSG_MOD(extack, "Invalid data after header"); |
| 5797 | return -EINVAL; |
| 5798 | } |
| 5799 | |
| 5800 | ifm = nlmsg_data(nlh); |
| 5801 | if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || |
| 5802 | ifm->ifi_change || ifm->ifi_index) { |
| 5803 | NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request"); |
| 5804 | return -EINVAL; |
| 5805 | } |
| 5806 | |
| 5807 | return 0; |
| 5808 | } |
| 5809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5810 | static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) |
| 5811 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 5812 | struct net *net = sock_net(skb->sk); |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 5813 | int h, s_h; |
David S. Miller | 434a8a5 | 2009-11-11 18:53:00 -0800 | [diff] [blame] | 5814 | int idx = 0, s_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5815 | struct net_device *dev; |
| 5816 | struct inet6_dev *idev; |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 5817 | struct hlist_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5818 | |
David Ahern | 786e000 | 2018-10-07 20:16:33 -0700 | [diff] [blame] | 5819 | /* only requests using strict checking can pass data to |
| 5820 | * influence the dump |
| 5821 | */ |
| 5822 | if (cb->strict_check) { |
| 5823 | int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack); |
| 5824 | |
| 5825 | if (err < 0) |
| 5826 | return err; |
| 5827 | } |
| 5828 | |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 5829 | s_h = cb->args[0]; |
| 5830 | s_idx = cb->args[1]; |
| 5831 | |
| 5832 | rcu_read_lock(); |
| 5833 | for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { |
| 5834 | idx = 0; |
| 5835 | head = &net->dev_index_head[h]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 5836 | hlist_for_each_entry_rcu(dev, head, index_hlist) { |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 5837 | if (idx < s_idx) |
| 5838 | goto cont; |
| 5839 | idev = __in6_dev_get(dev); |
| 5840 | if (!idev) |
| 5841 | goto cont; |
| 5842 | if (inet6_fill_ifinfo(skb, idev, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5843 | NETLINK_CB(cb->skb).portid, |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 5844 | cb->nlh->nlmsg_seq, |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 5845 | RTM_NEWLINK, NLM_F_MULTI) < 0) |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 5846 | goto out; |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 5847 | cont: |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 5848 | idx++; |
| 5849 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | } |
Eric Dumazet | 84d2697 | 2009-11-09 12:11:28 +0000 | [diff] [blame] | 5851 | out: |
| 5852 | rcu_read_unlock(); |
| 5853 | cb->args[1] = idx; |
| 5854 | cb->args[0] = h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5855 | |
| 5856 | return skb->len; |
| 5857 | } |
| 5858 | |
| 5859 | void inet6_ifinfo_notify(int event, struct inet6_dev *idev) |
| 5860 | { |
| 5861 | struct sk_buff *skb; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 5862 | struct net *net = dev_net(idev->dev); |
Thomas Graf | 8d7a76c | 2006-08-15 00:35:47 -0700 | [diff] [blame] | 5863 | int err = -ENOBUFS; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 5864 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 5865 | skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 5866 | if (!skb) |
Thomas Graf | 8d7a76c | 2006-08-15 00:35:47 -0700 | [diff] [blame] | 5867 | goto errout; |
| 5868 | |
| 5869 | err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5870 | if (err < 0) { |
| 5871 | /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ |
| 5872 | WARN_ON(err == -EMSGSIZE); |
| 5873 | kfree_skb(skb); |
| 5874 | goto errout; |
| 5875 | } |
Nicolas Dichtel | 5f75a10 | 2010-12-07 23:38:31 +0000 | [diff] [blame] | 5876 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 5877 | return; |
Thomas Graf | 8d7a76c | 2006-08-15 00:35:47 -0700 | [diff] [blame] | 5878 | errout: |
| 5879 | if (err < 0) |
Nicolas Dichtel | 5f75a10 | 2010-12-07 23:38:31 +0000 | [diff] [blame] | 5880 | rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5881 | } |
| 5882 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 5883 | static inline size_t inet6_prefix_nlmsg_size(void) |
| 5884 | { |
| 5885 | return NLMSG_ALIGN(sizeof(struct prefixmsg)) |
| 5886 | + nla_total_size(sizeof(struct in6_addr)) |
| 5887 | + nla_total_size(sizeof(struct prefix_cacheinfo)); |
| 5888 | } |
YOSHIFUJI Hideaki | c5396a3 | 2006-06-17 22:48:48 -0700 | [diff] [blame] | 5889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5890 | static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5891 | struct prefix_info *pinfo, u32 portid, u32 seq, |
Thomas Graf | 6051e2f | 2006-11-14 19:54:19 -0800 | [diff] [blame] | 5892 | int event, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5893 | { |
Thomas Graf | 6051e2f | 2006-11-14 19:54:19 -0800 | [diff] [blame] | 5894 | struct prefixmsg *pmsg; |
| 5895 | struct nlmsghdr *nlh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5896 | struct prefix_cacheinfo ci; |
| 5897 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 5898 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 5899 | if (!nlh) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5900 | return -EMSGSIZE; |
Thomas Graf | 6051e2f | 2006-11-14 19:54:19 -0800 | [diff] [blame] | 5901 | |
| 5902 | pmsg = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | pmsg->prefix_family = AF_INET6; |
Patrick McHardy | 8a47077 | 2005-06-28 12:56:45 -0700 | [diff] [blame] | 5904 | pmsg->prefix_pad1 = 0; |
| 5905 | pmsg->prefix_pad2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5906 | pmsg->prefix_ifindex = idev->dev->ifindex; |
| 5907 | pmsg->prefix_len = pinfo->prefix_len; |
| 5908 | pmsg->prefix_type = pinfo->type; |
Patrick McHardy | 8a47077 | 2005-06-28 12:56:45 -0700 | [diff] [blame] | 5909 | pmsg->prefix_pad3 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | pmsg->prefix_flags = 0; |
| 5911 | if (pinfo->onlink) |
| 5912 | pmsg->prefix_flags |= IF_PREFIX_ONLINK; |
| 5913 | if (pinfo->autoconf) |
| 5914 | pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; |
| 5915 | |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5916 | if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) |
| 5917 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | ci.preferred_time = ntohl(pinfo->prefered); |
| 5919 | ci.valid_time = ntohl(pinfo->valid); |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 5920 | if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) |
| 5921 | goto nla_put_failure; |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 5922 | nlmsg_end(skb, nlh); |
| 5923 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | |
Thomas Graf | 6051e2f | 2006-11-14 19:54:19 -0800 | [diff] [blame] | 5925 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5926 | nlmsg_cancel(skb, nlh); |
| 5927 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5928 | } |
| 5929 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 5930 | static void inet6_prefix_notify(int event, struct inet6_dev *idev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | struct prefix_info *pinfo) |
| 5932 | { |
| 5933 | struct sk_buff *skb; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 5934 | struct net *net = dev_net(idev->dev); |
Thomas Graf | 8c384bfa | 2006-08-15 00:36:07 -0700 | [diff] [blame] | 5935 | int err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5936 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 5937 | skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 5938 | if (!skb) |
Thomas Graf | 8c384bfa | 2006-08-15 00:36:07 -0700 | [diff] [blame] | 5939 | goto errout; |
| 5940 | |
| 5941 | err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 5942 | if (err < 0) { |
| 5943 | /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ |
| 5944 | WARN_ON(err == -EMSGSIZE); |
| 5945 | kfree_skb(skb); |
| 5946 | goto errout; |
| 5947 | } |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 5948 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); |
| 5949 | return; |
Thomas Graf | 8c384bfa | 2006-08-15 00:36:07 -0700 | [diff] [blame] | 5950 | errout: |
| 5951 | if (err < 0) |
Benjamin Thery | 6fda735 | 2008-03-05 10:47:47 -0800 | [diff] [blame] | 5952 | rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | } |
| 5954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) |
| 5956 | { |
Nicolas Dichtel | 63998ac | 2013-03-22 06:28:43 +0000 | [diff] [blame] | 5957 | struct net *net = dev_net(ifp->idev->dev); |
| 5958 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 5959 | if (event) |
| 5960 | ASSERT_RTNL(); |
| 5961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5962 | inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); |
| 5963 | |
| 5964 | switch (event) { |
| 5965 | case RTM_NEWADDR: |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 5966 | /* |
| 5967 | * If the address was optimistic |
| 5968 | * we inserted the route at the start of |
| 5969 | * our DAD process, so we don't need |
| 5970 | * to do it again |
| 5971 | */ |
David Ahern | 93c2fb2 | 2018-04-18 15:38:59 -0700 | [diff] [blame] | 5972 | if (!rcu_access_pointer(ifp->rt->fib6_node)) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 5973 | ip6_ins_rt(net, ifp->rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5974 | if (ifp->idev->cnf.forwarding) |
| 5975 | addrconf_join_anycast(ifp); |
Cong Wang | 7996c79 | 2013-05-22 05:41:06 +0000 | [diff] [blame] | 5976 | if (!ipv6_addr_any(&ifp->peer_addr)) |
David Ahern | 8308f3f | 2018-05-27 08:09:58 -0700 | [diff] [blame] | 5977 | addrconf_prefix_route(&ifp->peer_addr, 128, 0, |
David Ahern | acb54e3 | 2018-04-17 17:33:22 -0700 | [diff] [blame] | 5978 | ifp->idev->dev, 0, 0, |
David Ahern | 27b1060 | 2018-04-18 15:39:06 -0700 | [diff] [blame] | 5979 | GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5980 | break; |
| 5981 | case RTM_DELADDR: |
| 5982 | if (ifp->idev->cnf.forwarding) |
| 5983 | addrconf_leave_anycast(ifp); |
| 5984 | addrconf_leave_solict(ifp->idev, &ifp->addr); |
Cong Wang | 7996c79 | 2013-05-22 05:41:06 +0000 | [diff] [blame] | 5985 | if (!ipv6_addr_any(&ifp->peer_addr)) { |
David Ahern | 8d1c802 | 2018-04-17 17:33:26 -0700 | [diff] [blame] | 5986 | struct fib6_info *rt; |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 5987 | |
Nicolas Dichtel | e7478df | 2014-09-03 23:59:22 +0200 | [diff] [blame] | 5988 | rt = addrconf_get_prefix_route(&ifp->peer_addr, 128, |
David Ahern | 2b2450c | 2019-03-27 20:53:52 -0700 | [diff] [blame] | 5989 | ifp->idev->dev, 0, 0, |
| 5990 | false); |
Martin KaFai Lau | 8e3d5be | 2015-09-15 14:30:08 -0700 | [diff] [blame] | 5991 | if (rt) |
David Ahern | afb1d4b5 | 2018-04-17 17:33:11 -0700 | [diff] [blame] | 5992 | ip6_del_rt(net, rt); |
Nicolas Dichtel | caeaba7 | 2013-05-16 22:32:00 +0000 | [diff] [blame] | 5993 | } |
David Ahern | 38bd10c | 2016-04-21 20:56:12 -0700 | [diff] [blame] | 5994 | if (ifp->rt) { |
David Ahern | 93531c6 | 2018-04-17 17:33:25 -0700 | [diff] [blame] | 5995 | ip6_del_rt(net, ifp->rt); |
| 5996 | ifp->rt = NULL; |
David Ahern | 38bd10c | 2016-04-21 20:56:12 -0700 | [diff] [blame] | 5997 | } |
Hannes Frederic Sowa | 705f1c8 | 2014-09-28 00:46:06 +0200 | [diff] [blame] | 5998 | rt_genid_bump_ipv6(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5999 | break; |
| 6000 | } |
Nicolas Dichtel | 63998ac | 2013-03-22 06:28:43 +0000 | [diff] [blame] | 6001 | atomic_inc(&net->ipv6.dev_addr_genid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6002 | } |
| 6003 | |
| 6004 | static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) |
| 6005 | { |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 6006 | rcu_read_lock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6007 | if (likely(ifp->idev->dead == 0)) |
| 6008 | __ipv6_ifa_notify(event, ifp); |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 6009 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | } |
| 6011 | |
| 6012 | #ifdef CONFIG_SYSCTL |
| 6013 | |
| 6014 | static |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 6015 | int addrconf_sysctl_forward(struct ctl_table *ctl, int write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6016 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 6017 | { |
| 6018 | int *valp = ctl->data; |
| 6019 | int val = *valp; |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 6020 | loff_t pos = *ppos; |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 6021 | struct ctl_table lctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | int ret; |
| 6023 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 6024 | /* |
| 6025 | * ctl->data points to idev->cnf.forwarding, we should |
| 6026 | * not modify it until we get the rtnl lock. |
| 6027 | */ |
| 6028 | lctl = *ctl; |
| 6029 | lctl.data = &val; |
| 6030 | |
| 6031 | ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6032 | |
Pavel Emelyanov | c8fecf2 | 2007-12-05 01:50:24 -0800 | [diff] [blame] | 6033 | if (write) |
Stephen Hemminger | b325fdd | 2009-02-26 06:55:31 +0000 | [diff] [blame] | 6034 | ret = addrconf_fixup_forwarding(ctl, valp, val); |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 6035 | if (ret) |
| 6036 | *ppos = pos; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6037 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6038 | } |
| 6039 | |
Marcelo Leitner | 7775142 | 2015-02-23 11:17:13 -0300 | [diff] [blame] | 6040 | static |
| 6041 | int addrconf_sysctl_mtu(struct ctl_table *ctl, int write, |
| 6042 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 6043 | { |
| 6044 | struct inet6_dev *idev = ctl->extra1; |
| 6045 | int min_mtu = IPV6_MIN_MTU; |
| 6046 | struct ctl_table lctl; |
| 6047 | |
| 6048 | lctl = *ctl; |
| 6049 | lctl.extra1 = &min_mtu; |
| 6050 | lctl.extra2 = idev ? &idev->dev->mtu : NULL; |
| 6051 | |
| 6052 | return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos); |
| 6053 | } |
| 6054 | |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6055 | static void dev_disable_change(struct inet6_dev *idev) |
| 6056 | { |
Cong Wang | 75538c2 | 2013-05-29 11:30:50 +0800 | [diff] [blame] | 6057 | struct netdev_notifier_info info; |
| 6058 | |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6059 | if (!idev || !idev->dev) |
| 6060 | return; |
| 6061 | |
Cong Wang | 75538c2 | 2013-05-29 11:30:50 +0800 | [diff] [blame] | 6062 | netdev_notifier_info_init(&info, idev->dev); |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6063 | if (idev->cnf.disable_ipv6) |
Cong Wang | 75538c2 | 2013-05-29 11:30:50 +0800 | [diff] [blame] | 6064 | addrconf_notify(NULL, NETDEV_DOWN, &info); |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6065 | else |
Cong Wang | 75538c2 | 2013-05-29 11:30:50 +0800 | [diff] [blame] | 6066 | addrconf_notify(NULL, NETDEV_UP, &info); |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6067 | } |
| 6068 | |
| 6069 | static void addrconf_disable_change(struct net *net, __s32 newf) |
| 6070 | { |
| 6071 | struct net_device *dev; |
| 6072 | struct inet6_dev *idev; |
| 6073 | |
Kefeng Wang | 03e4def | 2017-01-19 16:26:21 +0800 | [diff] [blame] | 6074 | for_each_netdev(net, dev) { |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6075 | idev = __in6_dev_get(dev); |
| 6076 | if (idev) { |
| 6077 | int changed = (!idev->cnf.disable_ipv6) ^ (!newf); |
| 6078 | idev->cnf.disable_ipv6 = newf; |
| 6079 | if (changed) |
| 6080 | dev_disable_change(idev); |
| 6081 | } |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6082 | } |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6083 | } |
| 6084 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 6085 | static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6086 | { |
| 6087 | struct net *net; |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 6088 | int old; |
| 6089 | |
| 6090 | if (!rtnl_trylock()) |
| 6091 | return restart_syscall(); |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6092 | |
| 6093 | net = (struct net *)table->extra2; |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 6094 | old = *p; |
| 6095 | *p = newf; |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6096 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 6097 | if (p == &net->ipv6.devconf_dflt->disable_ipv6) { |
| 6098 | rtnl_unlock(); |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6099 | return 0; |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 6100 | } |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6101 | |
| 6102 | if (p == &net->ipv6.devconf_all->disable_ipv6) { |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6103 | net->ipv6.devconf_dflt->disable_ipv6 = newf; |
| 6104 | addrconf_disable_change(net, newf); |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 6105 | } else if ((!newf) ^ (!old)) |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6106 | dev_disable_change((struct inet6_dev *)table->extra1); |
| 6107 | |
| 6108 | rtnl_unlock(); |
| 6109 | return 0; |
| 6110 | } |
| 6111 | |
| 6112 | static |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 6113 | int addrconf_sysctl_disable(struct ctl_table *ctl, int write, |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6114 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 6115 | { |
| 6116 | int *valp = ctl->data; |
| 6117 | int val = *valp; |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 6118 | loff_t pos = *ppos; |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 6119 | struct ctl_table lctl; |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6120 | int ret; |
| 6121 | |
Francesco Ruggeri | 013d97e | 2012-01-16 10:40:10 +0000 | [diff] [blame] | 6122 | /* |
| 6123 | * ctl->data points to idev->cnf.disable_ipv6, we should |
| 6124 | * not modify it until we get the rtnl lock. |
| 6125 | */ |
| 6126 | lctl = *ctl; |
| 6127 | lctl.data = &val; |
| 6128 | |
| 6129 | ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6130 | |
| 6131 | if (write) |
| 6132 | ret = addrconf_disable_ipv6(ctl, valp, val); |
Eric W. Biederman | 88af182 | 2010-02-19 13:22:59 +0000 | [diff] [blame] | 6133 | if (ret) |
| 6134 | *ppos = pos; |
Brian Haley | 56d417b | 2009-06-01 03:07:33 -0700 | [diff] [blame] | 6135 | return ret; |
| 6136 | } |
| 6137 | |
stephen hemminger | c92d549 | 2013-12-17 22:37:14 -0800 | [diff] [blame] | 6138 | static |
| 6139 | int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write, |
| 6140 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 6141 | { |
| 6142 | int *valp = ctl->data; |
| 6143 | int ret; |
| 6144 | int old, new; |
| 6145 | |
| 6146 | old = *valp; |
| 6147 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
| 6148 | new = *valp; |
| 6149 | |
| 6150 | if (write && old != new) { |
| 6151 | struct net *net = ctl->extra2; |
| 6152 | |
| 6153 | if (!rtnl_trylock()) |
| 6154 | return restart_syscall(); |
| 6155 | |
| 6156 | if (valp == &net->ipv6.devconf_dflt->proxy_ndp) |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 6157 | inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 6158 | NETCONFA_PROXY_NEIGH, |
stephen hemminger | c92d549 | 2013-12-17 22:37:14 -0800 | [diff] [blame] | 6159 | NETCONFA_IFINDEX_DEFAULT, |
| 6160 | net->ipv6.devconf_dflt); |
| 6161 | else if (valp == &net->ipv6.devconf_all->proxy_ndp) |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 6162 | inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 6163 | NETCONFA_PROXY_NEIGH, |
stephen hemminger | c92d549 | 2013-12-17 22:37:14 -0800 | [diff] [blame] | 6164 | NETCONFA_IFINDEX_ALL, |
| 6165 | net->ipv6.devconf_all); |
| 6166 | else { |
| 6167 | struct inet6_dev *idev = ctl->extra1; |
| 6168 | |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 6169 | inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 6170 | NETCONFA_PROXY_NEIGH, |
stephen hemminger | c92d549 | 2013-12-17 22:37:14 -0800 | [diff] [blame] | 6171 | idev->dev->ifindex, |
| 6172 | &idev->cnf); |
| 6173 | } |
| 6174 | rtnl_unlock(); |
| 6175 | } |
| 6176 | |
| 6177 | return ret; |
| 6178 | } |
| 6179 | |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6180 | static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write, |
| 6181 | void __user *buffer, size_t *lenp, |
| 6182 | loff_t *ppos) |
| 6183 | { |
| 6184 | int ret = 0; |
Sabrina Dubroca | c6dbf7a | 2018-07-09 12:25:14 +0200 | [diff] [blame] | 6185 | u32 new_val; |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6186 | struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1; |
| 6187 | struct net *net = (struct net *)ctl->extra2; |
Sabrina Dubroca | c6dbf7a | 2018-07-09 12:25:14 +0200 | [diff] [blame] | 6188 | struct ctl_table tmp = { |
| 6189 | .data = &new_val, |
| 6190 | .maxlen = sizeof(new_val), |
| 6191 | .mode = ctl->mode, |
| 6192 | }; |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6193 | |
Felix Jia | 8c171d6 | 2017-02-27 12:41:23 +1300 | [diff] [blame] | 6194 | if (!rtnl_trylock()) |
| 6195 | return restart_syscall(); |
| 6196 | |
Sabrina Dubroca | c6dbf7a | 2018-07-09 12:25:14 +0200 | [diff] [blame] | 6197 | new_val = *((u32 *)ctl->data); |
| 6198 | |
| 6199 | ret = proc_douintvec(&tmp, write, buffer, lenp, ppos); |
| 6200 | if (ret != 0) |
| 6201 | goto out; |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6202 | |
| 6203 | if (write) { |
Felix Jia | 8c171d6 | 2017-02-27 12:41:23 +1300 | [diff] [blame] | 6204 | if (check_addr_gen_mode(new_val) < 0) { |
| 6205 | ret = -EINVAL; |
| 6206 | goto out; |
| 6207 | } |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6208 | |
Sabrina Dubroca | c6dbf7a | 2018-07-09 12:25:14 +0200 | [diff] [blame] | 6209 | if (idev) { |
Felix Jia | 8c171d6 | 2017-02-27 12:41:23 +1300 | [diff] [blame] | 6210 | if (check_stable_privacy(idev, net, new_val) < 0) { |
| 6211 | ret = -EINVAL; |
| 6212 | goto out; |
| 6213 | } |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6214 | |
| 6215 | if (idev->cnf.addr_gen_mode != new_val) { |
| 6216 | idev->cnf.addr_gen_mode = new_val; |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6217 | addrconf_dev_config(idev->dev); |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6218 | } |
Sabrina Dubroca | f24c598 | 2018-07-09 12:25:17 +0200 | [diff] [blame] | 6219 | } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) { |
| 6220 | struct net_device *dev; |
| 6221 | |
| 6222 | net->ipv6.devconf_dflt->addr_gen_mode = new_val; |
| 6223 | for_each_netdev(net, dev) { |
| 6224 | idev = __in6_dev_get(dev); |
| 6225 | if (idev && |
| 6226 | idev->cnf.addr_gen_mode != new_val) { |
| 6227 | idev->cnf.addr_gen_mode = new_val; |
| 6228 | addrconf_dev_config(idev->dev); |
| 6229 | } |
| 6230 | } |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6231 | } |
Sabrina Dubroca | c6dbf7a | 2018-07-09 12:25:14 +0200 | [diff] [blame] | 6232 | |
| 6233 | *((u32 *)ctl->data) = new_val; |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6234 | } |
| 6235 | |
Felix Jia | 8c171d6 | 2017-02-27 12:41:23 +1300 | [diff] [blame] | 6236 | out: |
| 6237 | rtnl_unlock(); |
| 6238 | |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6239 | return ret; |
| 6240 | } |
| 6241 | |
Hannes Frederic Sowa | 3d1bec9 | 2015-03-23 23:36:00 +0100 | [diff] [blame] | 6242 | static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write, |
| 6243 | void __user *buffer, size_t *lenp, |
| 6244 | loff_t *ppos) |
| 6245 | { |
| 6246 | int err; |
| 6247 | struct in6_addr addr; |
| 6248 | char str[IPV6_MAX_STRLEN]; |
| 6249 | struct ctl_table lctl = *ctl; |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 6250 | struct net *net = ctl->extra2; |
Hannes Frederic Sowa | 3d1bec9 | 2015-03-23 23:36:00 +0100 | [diff] [blame] | 6251 | struct ipv6_stable_secret *secret = ctl->data; |
| 6252 | |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 6253 | if (&net->ipv6.devconf_all->stable_secret == ctl->data) |
| 6254 | return -EIO; |
| 6255 | |
Hannes Frederic Sowa | 3d1bec9 | 2015-03-23 23:36:00 +0100 | [diff] [blame] | 6256 | lctl.maxlen = IPV6_MAX_STRLEN; |
| 6257 | lctl.data = str; |
| 6258 | |
| 6259 | if (!rtnl_trylock()) |
| 6260 | return restart_syscall(); |
| 6261 | |
| 6262 | if (!write && !secret->initialized) { |
| 6263 | err = -EIO; |
| 6264 | goto out; |
| 6265 | } |
| 6266 | |
WANG Cong | 5449a5c | 2015-12-21 10:55:45 -0800 | [diff] [blame] | 6267 | err = snprintf(str, sizeof(str), "%pI6", &secret->secret); |
| 6268 | if (err >= sizeof(str)) { |
| 6269 | err = -EIO; |
| 6270 | goto out; |
Hannes Frederic Sowa | 3d1bec9 | 2015-03-23 23:36:00 +0100 | [diff] [blame] | 6271 | } |
| 6272 | |
| 6273 | err = proc_dostring(&lctl, write, buffer, lenp, ppos); |
| 6274 | if (err || !write) |
| 6275 | goto out; |
| 6276 | |
| 6277 | if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) { |
| 6278 | err = -EIO; |
| 6279 | goto out; |
| 6280 | } |
| 6281 | |
| 6282 | secret->initialized = true; |
| 6283 | secret->secret = addr; |
| 6284 | |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 6285 | if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) { |
| 6286 | struct net_device *dev; |
| 6287 | |
| 6288 | for_each_netdev(net, dev) { |
| 6289 | struct inet6_dev *idev = __in6_dev_get(dev); |
| 6290 | |
| 6291 | if (idev) { |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6292 | idev->cnf.addr_gen_mode = |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 6293 | IN6_ADDR_GEN_MODE_STABLE_PRIVACY; |
| 6294 | } |
| 6295 | } |
| 6296 | } else { |
| 6297 | struct inet6_dev *idev = ctl->extra1; |
| 6298 | |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6299 | idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; |
Hannes Frederic Sowa | 622c81d5 | 2015-03-23 23:36:01 +0100 | [diff] [blame] | 6300 | } |
| 6301 | |
Hannes Frederic Sowa | 3d1bec9 | 2015-03-23 23:36:00 +0100 | [diff] [blame] | 6302 | out: |
| 6303 | rtnl_unlock(); |
| 6304 | |
| 6305 | return err; |
| 6306 | } |
stephen hemminger | c92d549 | 2013-12-17 22:37:14 -0800 | [diff] [blame] | 6307 | |
Andy Gospodarek | 35103d1 | 2015-08-13 10:39:01 -0400 | [diff] [blame] | 6308 | static |
| 6309 | int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl, |
| 6310 | int write, |
| 6311 | void __user *buffer, |
| 6312 | size_t *lenp, |
| 6313 | loff_t *ppos) |
| 6314 | { |
| 6315 | int *valp = ctl->data; |
| 6316 | int val = *valp; |
| 6317 | loff_t pos = *ppos; |
| 6318 | struct ctl_table lctl; |
| 6319 | int ret; |
| 6320 | |
| 6321 | /* ctl->data points to idev->cnf.ignore_routes_when_linkdown |
| 6322 | * we should not modify it until we get the rtnl lock. |
| 6323 | */ |
| 6324 | lctl = *ctl; |
| 6325 | lctl.data = &val; |
| 6326 | |
| 6327 | ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); |
| 6328 | |
| 6329 | if (write) |
| 6330 | ret = addrconf_fixup_linkdown(ctl, valp, val); |
| 6331 | if (ret) |
| 6332 | *ppos = pos; |
| 6333 | return ret; |
| 6334 | } |
| 6335 | |
David Forster | df789fe | 2017-02-23 16:27:18 +0000 | [diff] [blame] | 6336 | static |
| 6337 | void addrconf_set_nopolicy(struct rt6_info *rt, int action) |
| 6338 | { |
| 6339 | if (rt) { |
| 6340 | if (action) |
| 6341 | rt->dst.flags |= DST_NOPOLICY; |
| 6342 | else |
| 6343 | rt->dst.flags &= ~DST_NOPOLICY; |
| 6344 | } |
| 6345 | } |
| 6346 | |
| 6347 | static |
| 6348 | void addrconf_disable_policy_idev(struct inet6_dev *idev, int val) |
| 6349 | { |
| 6350 | struct inet6_ifaddr *ifa; |
| 6351 | |
| 6352 | read_lock_bh(&idev->lock); |
| 6353 | list_for_each_entry(ifa, &idev->addr_list, if_list) { |
| 6354 | spin_lock(&ifa->lock); |
| 6355 | if (ifa->rt) { |
David Ahern | f88d8ea | 2019-06-03 20:19:52 -0700 | [diff] [blame] | 6356 | /* host routes only use builtin fib6_nh */ |
David Ahern | 1cf844c | 2019-05-22 20:27:59 -0700 | [diff] [blame] | 6357 | struct fib6_nh *nh = ifa->rt->fib6_nh; |
David Forster | df789fe | 2017-02-23 16:27:18 +0000 | [diff] [blame] | 6358 | int cpu; |
| 6359 | |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 6360 | rcu_read_lock(); |
David Ahern | 3b6761d | 2018-04-17 17:33:20 -0700 | [diff] [blame] | 6361 | ifa->rt->dst_nopolicy = val ? true : false; |
David Ahern | f40b6ae | 2019-05-22 20:27:55 -0700 | [diff] [blame] | 6362 | if (nh->rt6i_pcpu) { |
David Forster | df789fe | 2017-02-23 16:27:18 +0000 | [diff] [blame] | 6363 | for_each_possible_cpu(cpu) { |
| 6364 | struct rt6_info **rtp; |
| 6365 | |
David Ahern | f40b6ae | 2019-05-22 20:27:55 -0700 | [diff] [blame] | 6366 | rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu); |
David Forster | df789fe | 2017-02-23 16:27:18 +0000 | [diff] [blame] | 6367 | addrconf_set_nopolicy(*rtp, val); |
| 6368 | } |
| 6369 | } |
Wei Wang | 66f5d6c | 2017-10-06 12:06:10 -0700 | [diff] [blame] | 6370 | rcu_read_unlock(); |
David Forster | df789fe | 2017-02-23 16:27:18 +0000 | [diff] [blame] | 6371 | } |
| 6372 | spin_unlock(&ifa->lock); |
| 6373 | } |
| 6374 | read_unlock_bh(&idev->lock); |
| 6375 | } |
| 6376 | |
| 6377 | static |
| 6378 | int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val) |
| 6379 | { |
| 6380 | struct inet6_dev *idev; |
| 6381 | struct net *net; |
| 6382 | |
| 6383 | if (!rtnl_trylock()) |
| 6384 | return restart_syscall(); |
| 6385 | |
| 6386 | *valp = val; |
| 6387 | |
| 6388 | net = (struct net *)ctl->extra2; |
| 6389 | if (valp == &net->ipv6.devconf_dflt->disable_policy) { |
| 6390 | rtnl_unlock(); |
| 6391 | return 0; |
| 6392 | } |
| 6393 | |
| 6394 | if (valp == &net->ipv6.devconf_all->disable_policy) { |
| 6395 | struct net_device *dev; |
| 6396 | |
| 6397 | for_each_netdev(net, dev) { |
| 6398 | idev = __in6_dev_get(dev); |
| 6399 | if (idev) |
| 6400 | addrconf_disable_policy_idev(idev, val); |
| 6401 | } |
| 6402 | } else { |
| 6403 | idev = (struct inet6_dev *)ctl->extra1; |
| 6404 | addrconf_disable_policy_idev(idev, val); |
| 6405 | } |
| 6406 | |
| 6407 | rtnl_unlock(); |
| 6408 | return 0; |
| 6409 | } |
| 6410 | |
| 6411 | static |
| 6412 | int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write, |
| 6413 | void __user *buffer, size_t *lenp, |
| 6414 | loff_t *ppos) |
| 6415 | { |
| 6416 | int *valp = ctl->data; |
| 6417 | int val = *valp; |
| 6418 | loff_t pos = *ppos; |
| 6419 | struct ctl_table lctl; |
| 6420 | int ret; |
| 6421 | |
| 6422 | lctl = *ctl; |
| 6423 | lctl.data = &val; |
| 6424 | ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); |
| 6425 | |
| 6426 | if (write && (*valp != val)) |
| 6427 | ret = addrconf_disable_policy(ctl, valp, val); |
| 6428 | |
| 6429 | if (ret) |
| 6430 | *ppos = pos; |
| 6431 | |
| 6432 | return ret; |
| 6433 | } |
| 6434 | |
Maciej Żenczykowski | cb4a4c6 | 2016-10-07 01:00:49 -0700 | [diff] [blame] | 6435 | static int minus_one = -1; |
Maciej Żenczykowski | cb9e684 | 2016-09-29 00:33:43 -0700 | [diff] [blame] | 6436 | static const int two_five_five = 255; |
| 6437 | |
Konstantin Khlebnikov | 607ea7c | 2016-04-18 14:41:10 +0300 | [diff] [blame] | 6438 | static const struct ctl_table addrconf_sysctl[] = { |
Konstantin Khlebnikov | 5df1f77 | 2016-04-18 14:41:17 +0300 | [diff] [blame] | 6439 | { |
| 6440 | .procname = "forwarding", |
| 6441 | .data = &ipv6_devconf.forwarding, |
| 6442 | .maxlen = sizeof(int), |
| 6443 | .mode = 0644, |
| 6444 | .proc_handler = addrconf_sysctl_forward, |
| 6445 | }, |
| 6446 | { |
| 6447 | .procname = "hop_limit", |
| 6448 | .data = &ipv6_devconf.hop_limit, |
| 6449 | .maxlen = sizeof(int), |
| 6450 | .mode = 0644, |
Maciej Żenczykowski | cb9e684 | 2016-09-29 00:33:43 -0700 | [diff] [blame] | 6451 | .proc_handler = proc_dointvec_minmax, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 6452 | .extra1 = (void *)SYSCTL_ONE, |
Maciej Żenczykowski | cb9e684 | 2016-09-29 00:33:43 -0700 | [diff] [blame] | 6453 | .extra2 = (void *)&two_five_five, |
Konstantin Khlebnikov | 5df1f77 | 2016-04-18 14:41:17 +0300 | [diff] [blame] | 6454 | }, |
| 6455 | { |
| 6456 | .procname = "mtu", |
| 6457 | .data = &ipv6_devconf.mtu6, |
| 6458 | .maxlen = sizeof(int), |
| 6459 | .mode = 0644, |
| 6460 | .proc_handler = addrconf_sysctl_mtu, |
| 6461 | }, |
| 6462 | { |
| 6463 | .procname = "accept_ra", |
| 6464 | .data = &ipv6_devconf.accept_ra, |
| 6465 | .maxlen = sizeof(int), |
| 6466 | .mode = 0644, |
| 6467 | .proc_handler = proc_dointvec, |
| 6468 | }, |
| 6469 | { |
| 6470 | .procname = "accept_redirects", |
| 6471 | .data = &ipv6_devconf.accept_redirects, |
| 6472 | .maxlen = sizeof(int), |
| 6473 | .mode = 0644, |
| 6474 | .proc_handler = proc_dointvec, |
| 6475 | }, |
| 6476 | { |
| 6477 | .procname = "autoconf", |
| 6478 | .data = &ipv6_devconf.autoconf, |
| 6479 | .maxlen = sizeof(int), |
| 6480 | .mode = 0644, |
| 6481 | .proc_handler = proc_dointvec, |
| 6482 | }, |
| 6483 | { |
| 6484 | .procname = "dad_transmits", |
| 6485 | .data = &ipv6_devconf.dad_transmits, |
| 6486 | .maxlen = sizeof(int), |
| 6487 | .mode = 0644, |
| 6488 | .proc_handler = proc_dointvec, |
| 6489 | }, |
| 6490 | { |
| 6491 | .procname = "router_solicitations", |
| 6492 | .data = &ipv6_devconf.rtr_solicits, |
| 6493 | .maxlen = sizeof(int), |
| 6494 | .mode = 0644, |
Maciej Żenczykowski | cb4a4c6 | 2016-10-07 01:00:49 -0700 | [diff] [blame] | 6495 | .proc_handler = proc_dointvec_minmax, |
| 6496 | .extra1 = &minus_one, |
Konstantin Khlebnikov | 5df1f77 | 2016-04-18 14:41:17 +0300 | [diff] [blame] | 6497 | }, |
| 6498 | { |
| 6499 | .procname = "router_solicitation_interval", |
| 6500 | .data = &ipv6_devconf.rtr_solicit_interval, |
| 6501 | .maxlen = sizeof(int), |
| 6502 | .mode = 0644, |
| 6503 | .proc_handler = proc_dointvec_jiffies, |
| 6504 | }, |
| 6505 | { |
Maciej Żenczykowski | bd11f07 | 2016-09-27 23:57:58 -0700 | [diff] [blame] | 6506 | .procname = "router_solicitation_max_interval", |
| 6507 | .data = &ipv6_devconf.rtr_solicit_max_interval, |
| 6508 | .maxlen = sizeof(int), |
| 6509 | .mode = 0644, |
| 6510 | .proc_handler = proc_dointvec_jiffies, |
| 6511 | }, |
| 6512 | { |
Konstantin Khlebnikov | 5df1f77 | 2016-04-18 14:41:17 +0300 | [diff] [blame] | 6513 | .procname = "router_solicitation_delay", |
| 6514 | .data = &ipv6_devconf.rtr_solicit_delay, |
| 6515 | .maxlen = sizeof(int), |
| 6516 | .mode = 0644, |
| 6517 | .proc_handler = proc_dointvec_jiffies, |
| 6518 | }, |
| 6519 | { |
| 6520 | .procname = "force_mld_version", |
| 6521 | .data = &ipv6_devconf.force_mld_version, |
| 6522 | .maxlen = sizeof(int), |
| 6523 | .mode = 0644, |
| 6524 | .proc_handler = proc_dointvec, |
| 6525 | }, |
| 6526 | { |
| 6527 | .procname = "mldv1_unsolicited_report_interval", |
| 6528 | .data = |
| 6529 | &ipv6_devconf.mldv1_unsolicited_report_interval, |
| 6530 | .maxlen = sizeof(int), |
| 6531 | .mode = 0644, |
| 6532 | .proc_handler = proc_dointvec_ms_jiffies, |
| 6533 | }, |
| 6534 | { |
| 6535 | .procname = "mldv2_unsolicited_report_interval", |
| 6536 | .data = |
| 6537 | &ipv6_devconf.mldv2_unsolicited_report_interval, |
| 6538 | .maxlen = sizeof(int), |
| 6539 | .mode = 0644, |
| 6540 | .proc_handler = proc_dointvec_ms_jiffies, |
| 6541 | }, |
| 6542 | { |
| 6543 | .procname = "use_tempaddr", |
| 6544 | .data = &ipv6_devconf.use_tempaddr, |
| 6545 | .maxlen = sizeof(int), |
| 6546 | .mode = 0644, |
| 6547 | .proc_handler = proc_dointvec, |
| 6548 | }, |
| 6549 | { |
| 6550 | .procname = "temp_valid_lft", |
| 6551 | .data = &ipv6_devconf.temp_valid_lft, |
| 6552 | .maxlen = sizeof(int), |
| 6553 | .mode = 0644, |
| 6554 | .proc_handler = proc_dointvec, |
| 6555 | }, |
| 6556 | { |
| 6557 | .procname = "temp_prefered_lft", |
| 6558 | .data = &ipv6_devconf.temp_prefered_lft, |
| 6559 | .maxlen = sizeof(int), |
| 6560 | .mode = 0644, |
| 6561 | .proc_handler = proc_dointvec, |
| 6562 | }, |
| 6563 | { |
| 6564 | .procname = "regen_max_retry", |
| 6565 | .data = &ipv6_devconf.regen_max_retry, |
| 6566 | .maxlen = sizeof(int), |
| 6567 | .mode = 0644, |
| 6568 | .proc_handler = proc_dointvec, |
| 6569 | }, |
| 6570 | { |
| 6571 | .procname = "max_desync_factor", |
| 6572 | .data = &ipv6_devconf.max_desync_factor, |
| 6573 | .maxlen = sizeof(int), |
| 6574 | .mode = 0644, |
| 6575 | .proc_handler = proc_dointvec, |
| 6576 | }, |
| 6577 | { |
| 6578 | .procname = "max_addresses", |
| 6579 | .data = &ipv6_devconf.max_addresses, |
| 6580 | .maxlen = sizeof(int), |
| 6581 | .mode = 0644, |
| 6582 | .proc_handler = proc_dointvec, |
| 6583 | }, |
| 6584 | { |
| 6585 | .procname = "accept_ra_defrtr", |
| 6586 | .data = &ipv6_devconf.accept_ra_defrtr, |
| 6587 | .maxlen = sizeof(int), |
| 6588 | .mode = 0644, |
| 6589 | .proc_handler = proc_dointvec, |
| 6590 | }, |
| 6591 | { |
| 6592 | .procname = "accept_ra_min_hop_limit", |
| 6593 | .data = &ipv6_devconf.accept_ra_min_hop_limit, |
| 6594 | .maxlen = sizeof(int), |
| 6595 | .mode = 0644, |
| 6596 | .proc_handler = proc_dointvec, |
| 6597 | }, |
| 6598 | { |
| 6599 | .procname = "accept_ra_pinfo", |
| 6600 | .data = &ipv6_devconf.accept_ra_pinfo, |
| 6601 | .maxlen = sizeof(int), |
| 6602 | .mode = 0644, |
| 6603 | .proc_handler = proc_dointvec, |
| 6604 | }, |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 6605 | #ifdef CONFIG_IPV6_ROUTER_PREF |
Konstantin Khlebnikov | 5df1f77 | 2016-04-18 14:41:17 +0300 | [diff] [blame] | 6606 | { |
| 6607 | .procname = "accept_ra_rtr_pref", |
| 6608 | .data = &ipv6_devconf.accept_ra_rtr_pref, |
| 6609 | .maxlen = sizeof(int), |
| 6610 | .mode = 0644, |
| 6611 | .proc_handler = proc_dointvec, |
| 6612 | }, |
| 6613 | { |
| 6614 | .procname = "router_probe_interval", |
| 6615 | .data = &ipv6_devconf.rtr_probe_interval, |
| 6616 | .maxlen = sizeof(int), |
| 6617 | .mode = 0644, |
| 6618 | .proc_handler = proc_dointvec_jiffies, |
| 6619 | }, |
Neil Horman | fa03ef3 | 2007-01-30 14:30:10 -0800 | [diff] [blame] | 6620 | #ifdef CONFIG_IPV6_ROUTE_INFO |
Konstantin Khlebnikov | 5df1f77 | 2016-04-18 14:41:17 +0300 | [diff] [blame] | 6621 | { |
Joel Scherpelz | bbea124 | 2017-03-22 18:19:04 +0900 | [diff] [blame] | 6622 | .procname = "accept_ra_rt_info_min_plen", |
| 6623 | .data = &ipv6_devconf.accept_ra_rt_info_min_plen, |
| 6624 | .maxlen = sizeof(int), |
| 6625 | .mode = 0644, |
| 6626 | .proc_handler = proc_dointvec, |
| 6627 | }, |
| 6628 | { |
Konstantin Khlebnikov | 5df1f77 | 2016-04-18 14:41:17 +0300 | [diff] [blame] | 6629 | .procname = "accept_ra_rt_info_max_plen", |
| 6630 | .data = &ipv6_devconf.accept_ra_rt_info_max_plen, |
| 6631 | .maxlen = sizeof(int), |
| 6632 | .mode = 0644, |
| 6633 | .proc_handler = proc_dointvec, |
| 6634 | }, |
YOSHIFUJI Hideaki | 09c884d | 2006-03-20 17:07:03 -0800 | [diff] [blame] | 6635 | #endif |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 6636 | #endif |
Konstantin Khlebnikov | 5df1f77 | 2016-04-18 14:41:17 +0300 | [diff] [blame] | 6637 | { |
| 6638 | .procname = "proxy_ndp", |
| 6639 | .data = &ipv6_devconf.proxy_ndp, |
| 6640 | .maxlen = sizeof(int), |
| 6641 | .mode = 0644, |
| 6642 | .proc_handler = addrconf_sysctl_proxy_ndp, |
| 6643 | }, |
| 6644 | { |
| 6645 | .procname = "accept_source_route", |
| 6646 | .data = &ipv6_devconf.accept_source_route, |
| 6647 | .maxlen = sizeof(int), |
| 6648 | .mode = 0644, |
| 6649 | .proc_handler = proc_dointvec, |
| 6650 | }, |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 6651 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
Konstantin Khlebnikov | 5df1f77 | 2016-04-18 14:41:17 +0300 | [diff] [blame] | 6652 | { |
| 6653 | .procname = "optimistic_dad", |
| 6654 | .data = &ipv6_devconf.optimistic_dad, |
| 6655 | .maxlen = sizeof(int), |
| 6656 | .mode = 0644, |
| 6657 | .proc_handler = proc_dointvec, |
| 6658 | }, |
| 6659 | { |
| 6660 | .procname = "use_optimistic", |
| 6661 | .data = &ipv6_devconf.use_optimistic, |
| 6662 | .maxlen = sizeof(int), |
| 6663 | .mode = 0644, |
| 6664 | .proc_handler = proc_dointvec, |
| 6665 | }, |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 6666 | #endif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 6667 | #ifdef CONFIG_IPV6_MROUTE |
Konstantin Khlebnikov | 5df1f77 | 2016-04-18 14:41:17 +0300 | [diff] [blame] | 6668 | { |
| 6669 | .procname = "mc_forwarding", |
| 6670 | .data = &ipv6_devconf.mc_forwarding, |
| 6671 | .maxlen = sizeof(int), |
| 6672 | .mode = 0444, |
| 6673 | .proc_handler = proc_dointvec, |
| 6674 | }, |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 6675 | #endif |
Konstantin Khlebnikov | 5df1f77 | 2016-04-18 14:41:17 +0300 | [diff] [blame] | 6676 | { |
| 6677 | .procname = "disable_ipv6", |
| 6678 | .data = &ipv6_devconf.disable_ipv6, |
| 6679 | .maxlen = sizeof(int), |
| 6680 | .mode = 0644, |
| 6681 | .proc_handler = addrconf_sysctl_disable, |
| 6682 | }, |
| 6683 | { |
| 6684 | .procname = "accept_dad", |
| 6685 | .data = &ipv6_devconf.accept_dad, |
| 6686 | .maxlen = sizeof(int), |
| 6687 | .mode = 0644, |
| 6688 | .proc_handler = proc_dointvec, |
| 6689 | }, |
| 6690 | { |
| 6691 | .procname = "force_tllao", |
| 6692 | .data = &ipv6_devconf.force_tllao, |
| 6693 | .maxlen = sizeof(int), |
| 6694 | .mode = 0644, |
| 6695 | .proc_handler = proc_dointvec |
| 6696 | }, |
| 6697 | { |
| 6698 | .procname = "ndisc_notify", |
| 6699 | .data = &ipv6_devconf.ndisc_notify, |
| 6700 | .maxlen = sizeof(int), |
| 6701 | .mode = 0644, |
| 6702 | .proc_handler = proc_dointvec |
| 6703 | }, |
| 6704 | { |
| 6705 | .procname = "suppress_frag_ndisc", |
| 6706 | .data = &ipv6_devconf.suppress_frag_ndisc, |
| 6707 | .maxlen = sizeof(int), |
| 6708 | .mode = 0644, |
| 6709 | .proc_handler = proc_dointvec |
| 6710 | }, |
| 6711 | { |
| 6712 | .procname = "accept_ra_from_local", |
| 6713 | .data = &ipv6_devconf.accept_ra_from_local, |
| 6714 | .maxlen = sizeof(int), |
| 6715 | .mode = 0644, |
| 6716 | .proc_handler = proc_dointvec, |
| 6717 | }, |
| 6718 | { |
| 6719 | .procname = "accept_ra_mtu", |
| 6720 | .data = &ipv6_devconf.accept_ra_mtu, |
| 6721 | .maxlen = sizeof(int), |
| 6722 | .mode = 0644, |
| 6723 | .proc_handler = proc_dointvec, |
| 6724 | }, |
| 6725 | { |
| 6726 | .procname = "stable_secret", |
| 6727 | .data = &ipv6_devconf.stable_secret, |
| 6728 | .maxlen = IPV6_MAX_STRLEN, |
| 6729 | .mode = 0600, |
| 6730 | .proc_handler = addrconf_sysctl_stable_secret, |
| 6731 | }, |
| 6732 | { |
| 6733 | .procname = "use_oif_addrs_only", |
| 6734 | .data = &ipv6_devconf.use_oif_addrs_only, |
| 6735 | .maxlen = sizeof(int), |
| 6736 | .mode = 0644, |
| 6737 | .proc_handler = proc_dointvec, |
| 6738 | }, |
| 6739 | { |
| 6740 | .procname = "ignore_routes_with_linkdown", |
| 6741 | .data = &ipv6_devconf.ignore_routes_with_linkdown, |
| 6742 | .maxlen = sizeof(int), |
| 6743 | .mode = 0644, |
| 6744 | .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown, |
| 6745 | }, |
| 6746 | { |
| 6747 | .procname = "drop_unicast_in_l2_multicast", |
| 6748 | .data = &ipv6_devconf.drop_unicast_in_l2_multicast, |
| 6749 | .maxlen = sizeof(int), |
| 6750 | .mode = 0644, |
| 6751 | .proc_handler = proc_dointvec, |
| 6752 | }, |
| 6753 | { |
| 6754 | .procname = "drop_unsolicited_na", |
| 6755 | .data = &ipv6_devconf.drop_unsolicited_na, |
| 6756 | .maxlen = sizeof(int), |
| 6757 | .mode = 0644, |
| 6758 | .proc_handler = proc_dointvec, |
| 6759 | }, |
| 6760 | { |
| 6761 | .procname = "keep_addr_on_down", |
| 6762 | .data = &ipv6_devconf.keep_addr_on_down, |
| 6763 | .maxlen = sizeof(int), |
| 6764 | .mode = 0644, |
| 6765 | .proc_handler = proc_dointvec, |
David Ahern | f1705ec | 2016-02-24 09:25:37 -0800 | [diff] [blame] | 6766 | |
Konstantin Khlebnikov | 5df1f77 | 2016-04-18 14:41:17 +0300 | [diff] [blame] | 6767 | }, |
| 6768 | { |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 6769 | .procname = "seg6_enabled", |
| 6770 | .data = &ipv6_devconf.seg6_enabled, |
| 6771 | .maxlen = sizeof(int), |
| 6772 | .mode = 0644, |
| 6773 | .proc_handler = proc_dointvec, |
| 6774 | }, |
David Lebrun | bf355b8 | 2016-11-08 14:57:42 +0100 | [diff] [blame] | 6775 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 6776 | { |
| 6777 | .procname = "seg6_require_hmac", |
| 6778 | .data = &ipv6_devconf.seg6_require_hmac, |
| 6779 | .maxlen = sizeof(int), |
| 6780 | .mode = 0644, |
| 6781 | .proc_handler = proc_dointvec, |
| 6782 | }, |
| 6783 | #endif |
David Lebrun | 1ababeb | 2016-11-08 14:57:39 +0100 | [diff] [blame] | 6784 | { |
Erik Nordmark | adc176c | 2016-12-02 14:00:08 -0800 | [diff] [blame] | 6785 | .procname = "enhanced_dad", |
| 6786 | .data = &ipv6_devconf.enhanced_dad, |
| 6787 | .maxlen = sizeof(int), |
| 6788 | .mode = 0644, |
| 6789 | .proc_handler = proc_dointvec, |
| 6790 | }, |
| 6791 | { |
Felix Jia | d35a00b | 2017-01-26 16:59:17 +1300 | [diff] [blame] | 6792 | .procname = "addr_gen_mode", |
| 6793 | .data = &ipv6_devconf.addr_gen_mode, |
| 6794 | .maxlen = sizeof(int), |
| 6795 | .mode = 0644, |
| 6796 | .proc_handler = addrconf_sysctl_addr_gen_mode, |
| 6797 | }, |
| 6798 | { |
David Forster | df789fe | 2017-02-23 16:27:18 +0000 | [diff] [blame] | 6799 | .procname = "disable_policy", |
| 6800 | .data = &ipv6_devconf.disable_policy, |
| 6801 | .maxlen = sizeof(int), |
| 6802 | .mode = 0644, |
| 6803 | .proc_handler = addrconf_sysctl_disable_policy, |
| 6804 | }, |
| 6805 | { |
Maciej Żenczykowski | 2210d6b | 2017-11-07 21:52:09 -0800 | [diff] [blame] | 6806 | .procname = "ndisc_tclass", |
| 6807 | .data = &ipv6_devconf.ndisc_tclass, |
| 6808 | .maxlen = sizeof(int), |
| 6809 | .mode = 0644, |
| 6810 | .proc_handler = proc_dointvec_minmax, |
Matteo Croce | eec4844 | 2019-07-18 15:58:50 -0700 | [diff] [blame] | 6811 | .extra1 = (void *)SYSCTL_ZERO, |
Maciej Żenczykowski | 2210d6b | 2017-11-07 21:52:09 -0800 | [diff] [blame] | 6812 | .extra2 = (void *)&two_five_five, |
| 6813 | }, |
| 6814 | { |
Konstantin Khlebnikov | 5df1f77 | 2016-04-18 14:41:17 +0300 | [diff] [blame] | 6815 | /* sentinel */ |
| 6816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | }; |
| 6818 | |
Pavel Emelyanov | bff16c2 | 2008-01-10 17:42:13 -0800 | [diff] [blame] | 6819 | static int __addrconf_sysctl_register(struct net *net, char *dev_name, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 6820 | struct inet6_dev *idev, struct ipv6_devconf *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6821 | { |
Nicolas Dichtel | 29c994e | 2016-08-30 10:09:22 +0200 | [diff] [blame] | 6822 | int i, ifindex; |
Konstantin Khlebnikov | 607ea7c | 2016-04-18 14:41:10 +0300 | [diff] [blame] | 6823 | struct ctl_table *table; |
Eric W. Biederman | 6105e29 | 2012-04-19 13:41:24 +0000 | [diff] [blame] | 6824 | char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; |
Pavel Emelyanov | 1dab622 | 2007-12-02 00:59:38 +1100 | [diff] [blame] | 6825 | |
Konstantin Khlebnikov | 607ea7c | 2016-04-18 14:41:10 +0300 | [diff] [blame] | 6826 | table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL); |
| 6827 | if (!table) |
Pavel Emelyanov | f68635e | 2007-12-02 00:21:52 +1100 | [diff] [blame] | 6828 | goto out; |
| 6829 | |
Konstantin Khlebnikov | 607ea7c | 2016-04-18 14:41:10 +0300 | [diff] [blame] | 6830 | for (i = 0; table[i].data; i++) { |
| 6831 | table[i].data += (char *)p - (char *)&ipv6_devconf; |
Maciej Żenczykowski | cb9e684 | 2016-09-29 00:33:43 -0700 | [diff] [blame] | 6832 | /* If one of these is already set, then it is not safe to |
| 6833 | * overwrite either of them: this makes proc_dointvec_minmax |
| 6834 | * usable. |
| 6835 | */ |
| 6836 | if (!table[i].extra1 && !table[i].extra2) { |
| 6837 | table[i].extra1 = idev; /* embedded; no ref */ |
| 6838 | table[i].extra2 = net; |
| 6839 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6840 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6841 | |
Eric W. Biederman | 6105e29 | 2012-04-19 13:41:24 +0000 | [diff] [blame] | 6842 | snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6843 | |
Konstantin Khlebnikov | 607ea7c | 2016-04-18 14:41:10 +0300 | [diff] [blame] | 6844 | p->sysctl_header = register_net_sysctl(net, path, table); |
| 6845 | if (!p->sysctl_header) |
Eric W. Biederman | 6105e29 | 2012-04-19 13:41:24 +0000 | [diff] [blame] | 6846 | goto free; |
Pavel Emelyanov | f68635e | 2007-12-02 00:21:52 +1100 | [diff] [blame] | 6847 | |
Nicolas Dichtel | 29c994e | 2016-08-30 10:09:22 +0200 | [diff] [blame] | 6848 | if (!strcmp(dev_name, "all")) |
| 6849 | ifindex = NETCONFA_IFINDEX_ALL; |
| 6850 | else if (!strcmp(dev_name, "default")) |
| 6851 | ifindex = NETCONFA_IFINDEX_DEFAULT; |
| 6852 | else |
| 6853 | ifindex = idev->dev->ifindex; |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 6854 | inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, |
| 6855 | ifindex, p); |
Pavel Emelyanov | 9589731 | 2008-01-10 17:41:45 -0800 | [diff] [blame] | 6856 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6857 | |
Pavel Emelyanov | f68635e | 2007-12-02 00:21:52 +1100 | [diff] [blame] | 6858 | free: |
Konstantin Khlebnikov | 607ea7c | 2016-04-18 14:41:10 +0300 | [diff] [blame] | 6859 | kfree(table); |
Pavel Emelyanov | f68635e | 2007-12-02 00:21:52 +1100 | [diff] [blame] | 6860 | out: |
Pavel Emelyanov | 9589731 | 2008-01-10 17:41:45 -0800 | [diff] [blame] | 6861 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6862 | } |
| 6863 | |
David Ahern | 2345217 | 2017-03-28 14:28:05 -0700 | [diff] [blame] | 6864 | static void __addrconf_sysctl_unregister(struct net *net, |
| 6865 | struct ipv6_devconf *p, int ifindex) |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 6866 | { |
Konstantin Khlebnikov | 607ea7c | 2016-04-18 14:41:10 +0300 | [diff] [blame] | 6867 | struct ctl_table *table; |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 6868 | |
Konstantin Khlebnikov | 607ea7c | 2016-04-18 14:41:10 +0300 | [diff] [blame] | 6869 | if (!p->sysctl_header) |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 6870 | return; |
| 6871 | |
Konstantin Khlebnikov | 607ea7c | 2016-04-18 14:41:10 +0300 | [diff] [blame] | 6872 | table = p->sysctl_header->ctl_table_arg; |
| 6873 | unregister_net_sysctl_table(p->sysctl_header); |
| 6874 | p->sysctl_header = NULL; |
| 6875 | kfree(table); |
David Ahern | 2345217 | 2017-03-28 14:28:05 -0700 | [diff] [blame] | 6876 | |
| 6877 | inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 6878 | } |
| 6879 | |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 6880 | static int addrconf_sysctl_register(struct inet6_dev *idev) |
Pavel Emelyanov | f52295a | 2007-12-02 00:58:37 +1100 | [diff] [blame] | 6881 | { |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 6882 | int err; |
| 6883 | |
| 6884 | if (!sysctl_dev_name_is_allowed(idev->dev->name)) |
| 6885 | return -EINVAL; |
| 6886 | |
| 6887 | err = neigh_sysctl_register(idev->dev, idev->nd_parms, |
| 6888 | &ndisc_ifinfo_sysctl_change); |
| 6889 | if (err) |
| 6890 | return err; |
| 6891 | err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, |
| 6892 | idev, &idev->cnf); |
| 6893 | if (err) |
| 6894 | neigh_sysctl_unregister(idev->nd_parms); |
| 6895 | |
| 6896 | return err; |
Pavel Emelyanov | f52295a | 2007-12-02 00:58:37 +1100 | [diff] [blame] | 6897 | } |
| 6898 | |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 6899 | static void addrconf_sysctl_unregister(struct inet6_dev *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6900 | { |
David Ahern | 2345217 | 2017-03-28 14:28:05 -0700 | [diff] [blame] | 6901 | __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf, |
| 6902 | idev->dev->ifindex); |
Pavel Emelyanov | 408c476 | 2008-01-10 17:41:21 -0800 | [diff] [blame] | 6903 | neigh_sysctl_unregister(idev->nd_parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6904 | } |
| 6905 | |
| 6906 | |
| 6907 | #endif |
| 6908 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 6909 | static int __net_init addrconf_init_net(struct net *net) |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 6910 | { |
Hong Zhiguo | a79ca22 | 2013-03-26 01:52:45 +0800 | [diff] [blame] | 6911 | int err = -ENOMEM; |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 6912 | struct ipv6_devconf *all, *dflt; |
| 6913 | |
Hong Zhiguo | a79ca22 | 2013-03-26 01:52:45 +0800 | [diff] [blame] | 6914 | all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 6915 | if (!all) |
Hong Zhiguo | a79ca22 | 2013-03-26 01:52:45 +0800 | [diff] [blame] | 6916 | goto err_alloc_all; |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 6917 | |
Hong Zhiguo | a79ca22 | 2013-03-26 01:52:45 +0800 | [diff] [blame] | 6918 | dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 6919 | if (!dflt) |
Hong Zhiguo | a79ca22 | 2013-03-26 01:52:45 +0800 | [diff] [blame] | 6920 | goto err_alloc_dflt; |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 6921 | |
Arnd Bergmann | a154d5d | 2019-03-04 21:38:03 +0100 | [diff] [blame] | 6922 | if (IS_ENABLED(CONFIG_SYSCTL) && |
| 6923 | sysctl_devconf_inherit_init_net == 1 && !net_eq(net, &init_net)) { |
Cong Wang | 856c395 | 2019-01-17 23:27:11 -0800 | [diff] [blame] | 6924 | memcpy(all, init_net.ipv6.devconf_all, sizeof(ipv6_devconf)); |
| 6925 | memcpy(dflt, init_net.ipv6.devconf_dflt, sizeof(ipv6_devconf_dflt)); |
| 6926 | } |
| 6927 | |
Hong Zhiguo | a79ca22 | 2013-03-26 01:52:45 +0800 | [diff] [blame] | 6928 | /* these will be inherited by all namespaces */ |
| 6929 | dflt->autoconf = ipv6_defaults.autoconf; |
| 6930 | dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 6931 | |
Hannes Frederic Sowa | 3d1bec9 | 2015-03-23 23:36:00 +0100 | [diff] [blame] | 6932 | dflt->stable_secret.initialized = false; |
| 6933 | all->stable_secret.initialized = false; |
| 6934 | |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 6935 | net->ipv6.devconf_all = all; |
| 6936 | net->ipv6.devconf_dflt = dflt; |
| 6937 | |
| 6938 | #ifdef CONFIG_SYSCTL |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 6939 | err = __addrconf_sysctl_register(net, "all", NULL, all); |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 6940 | if (err < 0) |
| 6941 | goto err_reg_all; |
| 6942 | |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 6943 | err = __addrconf_sysctl_register(net, "default", NULL, dflt); |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 6944 | if (err < 0) |
| 6945 | goto err_reg_dflt; |
| 6946 | #endif |
| 6947 | return 0; |
| 6948 | |
| 6949 | #ifdef CONFIG_SYSCTL |
| 6950 | err_reg_dflt: |
David Ahern | 2345217 | 2017-03-28 14:28:05 -0700 | [diff] [blame] | 6951 | __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 6952 | err_reg_all: |
| 6953 | kfree(dflt); |
| 6954 | #endif |
| 6955 | err_alloc_dflt: |
| 6956 | kfree(all); |
| 6957 | err_alloc_all: |
| 6958 | return err; |
| 6959 | } |
| 6960 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 6961 | static void __net_exit addrconf_exit_net(struct net *net) |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 6962 | { |
| 6963 | #ifdef CONFIG_SYSCTL |
David Ahern | 2345217 | 2017-03-28 14:28:05 -0700 | [diff] [blame] | 6964 | __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt, |
| 6965 | NETCONFA_IFINDEX_DEFAULT); |
| 6966 | __addrconf_sysctl_unregister(net, net->ipv6.devconf_all, |
| 6967 | NETCONFA_IFINDEX_ALL); |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 6968 | #endif |
zhuyj | 73cf0e92 | 2014-11-26 10:25:58 +0800 | [diff] [blame] | 6969 | kfree(net->ipv6.devconf_dflt); |
| 6970 | kfree(net->ipv6.devconf_all); |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 6971 | } |
| 6972 | |
| 6973 | static struct pernet_operations addrconf_ops = { |
| 6974 | .init = addrconf_init_net, |
| 6975 | .exit = addrconf_exit_net, |
| 6976 | }; |
| 6977 | |
Daniel Borkmann | 207895fd3 | 2015-01-29 12:15:03 +0100 | [diff] [blame] | 6978 | static struct rtnl_af_ops inet6_ops __read_mostly = { |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 6979 | .family = AF_INET6, |
| 6980 | .fill_link_af = inet6_fill_link_af, |
| 6981 | .get_link_af_size = inet6_get_link_af_size, |
Daniel Borkmann | 11b1f82 | 2015-02-05 14:39:11 +0100 | [diff] [blame] | 6982 | .validate_link_af = inet6_validate_link_af, |
Daniel Borkmann | f53adae | 2013-04-08 04:01:30 +0000 | [diff] [blame] | 6983 | .set_link_af = inet6_set_link_af, |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 6984 | }; |
| 6985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6986 | /* |
| 6987 | * Init / cleanup code |
| 6988 | */ |
| 6989 | |
| 6990 | int __init addrconf_init(void) |
| 6991 | { |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 6992 | struct inet6_dev *idev; |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 6993 | int i, err; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 6994 | |
Stephen Hemminger | e21e846 | 2010-03-20 16:09:01 -0700 | [diff] [blame] | 6995 | err = ipv6_addr_label_init(); |
| 6996 | if (err < 0) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 6997 | pr_crit("%s: cannot initialize default policy table: %d\n", |
| 6998 | __func__, err); |
Neil Horman | 2cc6d2b | 2010-09-24 09:55:52 +0000 | [diff] [blame] | 6999 | goto out; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 7000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7001 | |
Neil Horman | 2cc6d2b | 2010-09-24 09:55:52 +0000 | [diff] [blame] | 7002 | err = register_pernet_subsys(&addrconf_ops); |
| 7003 | if (err < 0) |
| 7004 | goto out_addrlabel; |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 7005 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 7006 | addrconf_wq = create_workqueue("ipv6_addrconf"); |
| 7007 | if (!addrconf_wq) { |
| 7008 | err = -ENOMEM; |
| 7009 | goto out_nowq; |
| 7010 | } |
| 7011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7012 | /* The addrconf netdev notifier requires that loopback_dev |
| 7013 | * has it's ipv6 private information allocated and setup |
| 7014 | * before it can bring up and give link-local addresses |
| 7015 | * to other devices which are up. |
| 7016 | * |
| 7017 | * Unfortunately, loopback_dev is not necessarily the first |
| 7018 | * entry in the global dev_base list of net devices. In fact, |
| 7019 | * it is likely to be the very last entry on that list. |
| 7020 | * So this causes the notifier registry below to try and |
| 7021 | * give link-local addresses to all devices besides loopback_dev |
| 7022 | * first, then loopback_dev, which cases all the non-loopback_dev |
| 7023 | * devices to fail to get a link-local address. |
| 7024 | * |
| 7025 | * So, as a temporary fix, allocate the ipv6 structure for |
| 7026 | * loopback_dev first by hand. |
| 7027 | * Longer term, all of the dependencies ipv6 has upon the loopback |
| 7028 | * device and it being up should be removed. |
| 7029 | */ |
| 7030 | rtnl_lock(); |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 7031 | idev = ipv6_add_dev(init_net.loopback_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7032 | rtnl_unlock(); |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 7033 | if (IS_ERR(idev)) { |
| 7034 | err = PTR_ERR(idev); |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 7035 | goto errlo; |
WANG Cong | a317a2f | 2014-07-25 15:25:09 -0700 | [diff] [blame] | 7036 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7037 | |
WANG Cong | 2f46093 | 2017-05-03 22:07:31 -0700 | [diff] [blame] | 7038 | ip6_route_init_special_entries(); |
| 7039 | |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 7040 | for (i = 0; i < IN6_ADDR_HSIZE; i++) |
| 7041 | INIT_HLIST_HEAD(&inet6_addr_lst[i]); |
| 7042 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7043 | register_netdevice_notifier(&ipv6_dev_notf); |
| 7044 | |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 7045 | addrconf_verify(); |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 7046 | |
stephen hemminger | 3678a9d | 2013-12-30 10:41:32 -0800 | [diff] [blame] | 7047 | rtnl_af_register(&inet6_ops); |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 7048 | |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 7049 | err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK, |
| 7050 | NULL, inet6_dump_ifinfo, 0); |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 7051 | if (err < 0) |
| 7052 | goto errout; |
| 7053 | |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 7054 | err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR, |
| 7055 | inet6_rtm_newaddr, NULL, 0); |
| 7056 | if (err < 0) |
| 7057 | goto errout; |
| 7058 | err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR, |
| 7059 | inet6_rtm_deladdr, NULL, 0); |
| 7060 | if (err < 0) |
| 7061 | goto errout; |
| 7062 | err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR, |
| 7063 | inet6_rtm_getaddr, inet6_dump_ifaddr, |
| 7064 | RTNL_FLAG_DOIT_UNLOCKED); |
| 7065 | if (err < 0) |
| 7066 | goto errout; |
| 7067 | err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST, |
| 7068 | NULL, inet6_dump_ifmcaddr, 0); |
| 7069 | if (err < 0) |
| 7070 | goto errout; |
| 7071 | err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST, |
| 7072 | NULL, inet6_dump_ifacaddr, 0); |
| 7073 | if (err < 0) |
| 7074 | goto errout; |
| 7075 | err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF, |
| 7076 | inet6_netconf_get_devconf, |
| 7077 | inet6_netconf_dump_devconf, |
| 7078 | RTNL_FLAG_DOIT_UNLOCKED); |
| 7079 | if (err < 0) |
| 7080 | goto errout; |
Florian Westphal | a3fde2a | 2017-12-04 19:19:18 +0100 | [diff] [blame] | 7081 | err = ipv6_addr_label_rtnl_register(); |
| 7082 | if (err < 0) |
| 7083 | goto errout; |
YOSHIFUJI Hideaki | 2a8cc6c | 2007-11-14 15:56:23 +0900 | [diff] [blame] | 7084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7085 | return 0; |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 7086 | errout: |
Florian Westphal | 16feebc | 2017-12-02 21:44:08 +0100 | [diff] [blame] | 7087 | rtnl_unregister_all(PF_INET6); |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 7088 | rtnl_af_unregister(&inet6_ops); |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 7089 | unregister_netdevice_notifier(&ipv6_dev_notf); |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 7090 | errlo: |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 7091 | destroy_workqueue(addrconf_wq); |
| 7092 | out_nowq: |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 7093 | unregister_pernet_subsys(&addrconf_ops); |
Neil Horman | 2cc6d2b | 2010-09-24 09:55:52 +0000 | [diff] [blame] | 7094 | out_addrlabel: |
| 7095 | ipv6_addr_label_cleanup(); |
| 7096 | out: |
Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 7097 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7098 | } |
| 7099 | |
Daniel Lezcano | 09f7709 | 2007-12-13 05:34:58 -0800 | [diff] [blame] | 7100 | void addrconf_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7101 | { |
Daniel Lezcano | 176c39a | 2009-03-03 01:06:45 -0800 | [diff] [blame] | 7102 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7103 | int i; |
| 7104 | |
| 7105 | unregister_netdevice_notifier(&ipv6_dev_notf); |
Pavel Emelyanov | e0da5a4 | 2008-01-10 17:42:55 -0800 | [diff] [blame] | 7106 | unregister_pernet_subsys(&addrconf_ops); |
Neil Horman | 2cc6d2b | 2010-09-24 09:55:52 +0000 | [diff] [blame] | 7107 | ipv6_addr_label_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7108 | |
Florian Westphal | 5c45121 | 2017-10-04 15:58:49 +0200 | [diff] [blame] | 7109 | rtnl_af_unregister(&inet6_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7110 | |
Florian Westphal | 5c45121 | 2017-10-04 15:58:49 +0200 | [diff] [blame] | 7111 | rtnl_lock(); |
Thomas Graf | b382b19 | 2010-11-16 04:33:57 +0000 | [diff] [blame] | 7112 | |
Daniel Lezcano | 176c39a | 2009-03-03 01:06:45 -0800 | [diff] [blame] | 7113 | /* clean dev list */ |
| 7114 | for_each_netdev(&init_net, dev) { |
| 7115 | if (__in6_dev_get(dev) == NULL) |
| 7116 | continue; |
| 7117 | addrconf_ifdown(dev, 1); |
| 7118 | } |
| 7119 | addrconf_ifdown(init_net.loopback_dev, 2); |
| 7120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7121 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7122 | * Check hash table. |
| 7123 | */ |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 7124 | spin_lock_bh(&addrconf_hash_lock); |
stephen hemminger | c2e2129 | 2010-03-17 20:31:10 +0000 | [diff] [blame] | 7125 | for (i = 0; i < IN6_ADDR_HSIZE; i++) |
| 7126 | WARN_ON(!hlist_empty(&inet6_addr_lst[i])); |
stephen hemminger | 5c578aed | 2010-03-17 20:31:11 +0000 | [diff] [blame] | 7127 | spin_unlock_bh(&addrconf_hash_lock); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 7128 | cancel_delayed_work(&addr_chk_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7129 | rtnl_unlock(); |
Hannes Frederic Sowa | c15b1cc | 2014-03-27 18:28:07 +0100 | [diff] [blame] | 7130 | |
| 7131 | destroy_workqueue(addrconf_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7132 | } |