12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * IPv6 Address [auto]configuration
41da177e4SLinus Torvalds * Linux INET6 implementation
51da177e4SLinus Torvalds *
61da177e4SLinus Torvalds * Authors:
71da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt>
81da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
91da177e4SLinus Torvalds */
101da177e4SLinus Torvalds
111da177e4SLinus Torvalds /*
121da177e4SLinus Torvalds * Changes:
131da177e4SLinus Torvalds *
141da177e4SLinus Torvalds * Janos Farkas : delete timer on ifdown
151da177e4SLinus Torvalds * <chexum@bankinf.banki.hu>
161da177e4SLinus Torvalds * Andi Kleen : kill double kfree on module
171da177e4SLinus Torvalds * unload.
181da177e4SLinus Torvalds * Maciej W. Rozycki : FDDI support
191da177e4SLinus Torvalds * sekiya@USAGI : Don't send too many RS
201da177e4SLinus Torvalds * packets.
211da177e4SLinus Torvalds * yoshfuji@USAGI : Fixed interval between DAD
221da177e4SLinus Torvalds * packets.
231da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : improved accuracy of
241da177e4SLinus Torvalds * address validation timer.
251da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
261da177e4SLinus Torvalds * support.
271da177e4SLinus Torvalds * Yuji SEKIYA @USAGI : Don't assign a same IPv6
281da177e4SLinus Torvalds * address on a same interface.
291da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : ARCnet support
301da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
311da177e4SLinus Torvalds * seq_file.
32b1cacb68SYOSHIFUJI Hideaki * YOSHIFUJI Hideaki @USAGI : improved source address
33b1cacb68SYOSHIFUJI Hideaki * selection; consider scope,
34b1cacb68SYOSHIFUJI Hideaki * status etc.
351da177e4SLinus Torvalds */
361da177e4SLinus Torvalds
37f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
38f3213831SJoe Perches
391da177e4SLinus Torvalds #include <linux/errno.h>
401da177e4SLinus Torvalds #include <linux/types.h>
41a0bffffcSIlpo Järvinen #include <linux/kernel.h>
42174cd4b1SIngo Molnar #include <linux/sched/signal.h>
431da177e4SLinus Torvalds #include <linux/socket.h>
441da177e4SLinus Torvalds #include <linux/sockios.h>
451da177e4SLinus Torvalds #include <linux/net.h>
463d1bec99SHannes Frederic Sowa #include <linux/inet.h>
471da177e4SLinus Torvalds #include <linux/in6.h>
481da177e4SLinus Torvalds #include <linux/netdevice.h>
491823730fSThomas Graf #include <linux/if_addr.h>
501da177e4SLinus Torvalds #include <linux/if_arp.h>
511da177e4SLinus Torvalds #include <linux/if_arcnet.h>
521da177e4SLinus Torvalds #include <linux/if_infiniband.h>
531da177e4SLinus Torvalds #include <linux/route.h>
541da177e4SLinus Torvalds #include <linux/inetdevice.h>
551da177e4SLinus Torvalds #include <linux/init.h>
565a0e3ad6STejun Heo #include <linux/slab.h>
571da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
581da177e4SLinus Torvalds #include <linux/sysctl.h>
591da177e4SLinus Torvalds #endif
604fc268d2SRandy Dunlap #include <linux/capability.h>
611da177e4SLinus Torvalds #include <linux/delay.h>
621da177e4SLinus Torvalds #include <linux/notifier.h>
63543537bdSPaulo Marques #include <linux/string.h>
64ddbe5032SEric Dumazet #include <linux/hash.h>
651da177e4SLinus Torvalds
66457c4cbcSEric W. Biederman #include <net/net_namespace.h>
671da177e4SLinus Torvalds #include <net/sock.h>
681da177e4SLinus Torvalds #include <net/snmp.h>
691da177e4SLinus Torvalds
705241c2d7SAlexander Aring #include <net/6lowpan.h>
71cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h>
721da177e4SLinus Torvalds #include <net/ipv6.h>
731da177e4SLinus Torvalds #include <net/protocol.h>
741da177e4SLinus Torvalds #include <net/ndisc.h>
751da177e4SLinus Torvalds #include <net/ip6_route.h>
761da177e4SLinus Torvalds #include <net/addrconf.h>
771da177e4SLinus Torvalds #include <net/tcp.h>
781da177e4SLinus Torvalds #include <net/ip.h>
795d620266SThomas Graf #include <net/netlink.h>
80f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
81ca254490SDavid Ahern #include <net/l3mdev.h>
821da177e4SLinus Torvalds #include <linux/if_tunnel.h>
831da177e4SLinus Torvalds #include <linux/rtnetlink.h>
84f3a1bfb1SNicolas Dichtel #include <linux/netconf.h>
851da177e4SLinus Torvalds #include <linux/random.h>
86e21e8467SStephen Hemminger #include <linux/uaccess.h>
877f7d9a6bSHerbert Xu #include <asm/unaligned.h>
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds #include <linux/proc_fs.h>
901da177e4SLinus Torvalds #include <linux/seq_file.h>
91bc3b2d7fSPaul Gortmaker #include <linux/export.h>
929ee11f0fSJustin Iurman #include <linux/ioam6.h>
931da177e4SLinus Torvalds
941da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF
9518a31e1eSThomas Graf
963d1bec99SHannes Frederic Sowa #define IPV6_MAX_STRLEN \
973d1bec99SHannes Frederic Sowa sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
983d1bec99SHannes Frederic Sowa
cstamp_delta(unsigned long cstamp)9918a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp)
10018a31e1eSThomas Graf {
10118a31e1eSThomas Graf return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
10218a31e1eSThomas Graf }
1031da177e4SLinus Torvalds
rfc3315_s14_backoff_init(s32 irt)104bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_init(s32 irt)
105bd11f074SMaciej Żenczykowski {
106bd11f074SMaciej Żenczykowski /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
107e8a533cbSJason A. Donenfeld u64 tmp = get_random_u32_inclusive(900000, 1100000) * (u64)irt;
108bd11f074SMaciej Żenczykowski do_div(tmp, 1000000);
109bd11f074SMaciej Żenczykowski return (s32)tmp;
110bd11f074SMaciej Żenczykowski }
111bd11f074SMaciej Żenczykowski
rfc3315_s14_backoff_update(s32 rt,s32 mrt)112bd11f074SMaciej Żenczykowski static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
113bd11f074SMaciej Żenczykowski {
114bd11f074SMaciej Żenczykowski /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
115e8a533cbSJason A. Donenfeld u64 tmp = get_random_u32_inclusive(1900000, 2100000) * (u64)rt;
116bd11f074SMaciej Żenczykowski do_div(tmp, 1000000);
117bd11f074SMaciej Żenczykowski if ((s32)tmp > mrt) {
118bd11f074SMaciej Żenczykowski /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
119e8a533cbSJason A. Donenfeld tmp = get_random_u32_inclusive(900000, 1100000) * (u64)mrt;
120bd11f074SMaciej Żenczykowski do_div(tmp, 1000000);
121bd11f074SMaciej Żenczykowski }
122bd11f074SMaciej Żenczykowski return (s32)tmp;
123bd11f074SMaciej Żenczykowski }
124bd11f074SMaciej Żenczykowski
1251da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
126a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev);
127408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
128408c4768SPavel Emelyanov #else
addrconf_sysctl_register(struct inet6_dev * idev)129a317a2f1SWANG Cong static inline int addrconf_sysctl_register(struct inet6_dev *idev)
130408c4768SPavel Emelyanov {
131a317a2f1SWANG Cong return 0;
132408c4768SPavel Emelyanov }
133408c4768SPavel Emelyanov
addrconf_sysctl_unregister(struct inet6_dev * idev)134408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
135408c4768SPavel Emelyanov {
136408c4768SPavel Emelyanov }
1371da177e4SLinus Torvalds #endif
1381da177e4SLinus Torvalds
139969c5464SFernando Gont static void ipv6_gen_rnd_iid(struct in6_addr *addr);
1401da177e4SLinus Torvalds
1411b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
142d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev);
143622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *addr,
144622c81d5SHannes Frederic Sowa u8 dad_count,
145622c81d5SHannes Frederic Sowa const struct inet6_dev *idev);
1461da177e4SLinus Torvalds
14727c565aeSEric Dumazet #define IN6_ADDR_HSIZE_SHIFT 8
14827c565aeSEric Dumazet #define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT)
1491da177e4SLinus Torvalds
1508805d13fSEric Dumazet static void addrconf_verify(struct net *net);
1518805d13fSEric Dumazet static void addrconf_verify_rtnl(struct net *net);
1521da177e4SLinus Torvalds
153c15b1ccaSHannes Frederic Sowa static struct workqueue_struct *addrconf_wq;
1541da177e4SLinus Torvalds
1551da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1561da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1571da177e4SLinus Torvalds
15893d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev,
15975c78500SMoni Shoua unsigned long event);
160ae79dbf6SFlorent Fourcot static int addrconf_ifdown(struct net_device *dev, bool unregister);
1611da177e4SLinus Torvalds
1628d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
16321caa662SRomain Kuntz int plen,
16421caa662SRomain Kuntz const struct net_device *dev,
1652b2450caSDavid Ahern u32 flags, u32 noflags,
1662b2450caSDavid Ahern bool no_gw);
16721caa662SRomain Kuntz
168cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp);
169c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w);
170c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
171c76fe2d9SDavid Ahern bool send_na);
172896585d4SHangbin Liu static void addrconf_dad_run(struct inet6_dev *idev, bool restart);
173e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t);
1741da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1751da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1761da177e4SLinus Torvalds
1771da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1781da177e4SLinus Torvalds struct prefix_info *pinfo);
1791da177e4SLinus Torvalds
180888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1811da177e4SLinus Torvalds .forwarding = 0,
1821da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT,
1831da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU,
1841da177e4SLinus Torvalds .accept_ra = 1,
1851da177e4SLinus Torvalds .accept_redirects = 1,
1861da177e4SLinus Torvalds .autoconf = 1,
1871da177e4SLinus Torvalds .force_mld_version = 0,
188fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ,
189fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ,
1901da177e4SLinus Torvalds .dad_transmits = 1,
1911da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS,
1921da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
193bd11f074SMaciej Żenczykowski .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
1941da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
1951da177e4SLinus Torvalds .use_tempaddr = 0,
1961da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME,
1971da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
1981da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY,
1991da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR,
2001da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES,
20165f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1,
2026b2e04bcSPraveen Chaudhary .ra_defrtr_metric = IP6_RT_PRIO_USER,
203d9333196SBen Greear .accept_ra_from_local = 0,
2048013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1,
2055027d54aSPatrick Rohr .accept_ra_min_lft = 0,
206c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1,
207930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
208930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1,
20952e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ,
21009c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
211bbea124bSJoel Scherpelz .accept_ra_rt_info_min_plen = 0,
21209c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0,
21309c884d4SYOSHIFUJI Hideaki #endif
214930d6ff2SYOSHIFUJI Hideaki #endif
215fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0,
2160bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */
217778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0,
21809400953SNicolas Dichtel .accept_dad = 0,
219b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1,
220c2943f14SHarout Hedeshian .accept_ra_mtu = 1,
2213d1bec99SHannes Frederic Sowa .stable_secret = {
2223d1bec99SHannes Frederic Sowa .initialized = false,
2233985e8a3SErik Kline },
2243985e8a3SErik Kline .use_oif_addrs_only = 0,
22535103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0,
226f1705ec1SDavid Ahern .keep_addr_on_down = 0,
2271ababebaSDavid Lebrun .seg6_enabled = 0,
228bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
229bf355b8dSDavid Lebrun .seg6_require_hmac = 0,
230bf355b8dSDavid Lebrun #endif
231adc176c5SErik Nordmark .enhanced_dad = 1,
232d35a00b8SFelix Jia .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
233df789fe7SDavid Forster .disable_policy = 0,
2348610c7c6SAlexander Aring .rpl_seg_enabled = 0,
2359ee11f0fSJustin Iurman .ioam6_enabled = 0,
2369ee11f0fSJustin Iurman .ioam6_id = IOAM6_DEFAULT_IF_ID,
2379ee11f0fSJustin Iurman .ioam6_id_wide = IOAM6_DEFAULT_IF_ID_WIDE,
23818ac597aSJames Prestwood .ndisc_evict_nocarrier = 1,
2391da177e4SLinus Torvalds };
2401da177e4SLinus Torvalds
241ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2421da177e4SLinus Torvalds .forwarding = 0,
2431da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT,
2441da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU,
2451da177e4SLinus Torvalds .accept_ra = 1,
2461da177e4SLinus Torvalds .accept_redirects = 1,
2471da177e4SLinus Torvalds .autoconf = 1,
248fc4eba58SHannes Frederic Sowa .force_mld_version = 0,
249fc4eba58SHannes Frederic Sowa .mldv1_unsolicited_report_interval = 10 * HZ,
250fc4eba58SHannes Frederic Sowa .mldv2_unsolicited_report_interval = HZ,
2511da177e4SLinus Torvalds .dad_transmits = 1,
2521da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS,
2531da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
254bd11f074SMaciej Żenczykowski .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
2551da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
2561da177e4SLinus Torvalds .use_tempaddr = 0,
2571da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME,
2581da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
2591da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY,
2601da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR,
2611da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES,
26265f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1,
2636b2e04bcSPraveen Chaudhary .ra_defrtr_metric = IP6_RT_PRIO_USER,
264d9333196SBen Greear .accept_ra_from_local = 0,
2658013d1d7SHangbin Liu .accept_ra_min_hop_limit= 1,
2665027d54aSPatrick Rohr .accept_ra_min_lft = 0,
267c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1,
268930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
269930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1,
27052e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ,
27109c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
272bbea124bSJoel Scherpelz .accept_ra_rt_info_min_plen = 0,
27309c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0,
27409c884d4SYOSHIFUJI Hideaki #endif
275930d6ff2SYOSHIFUJI Hideaki #endif
276fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0,
2770bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */
278778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0,
2791b34be74SYOSHIFUJI Hideaki .accept_dad = 1,
280b800c3b9SHannes Frederic Sowa .suppress_frag_ndisc = 1,
281c2943f14SHarout Hedeshian .accept_ra_mtu = 1,
2823d1bec99SHannes Frederic Sowa .stable_secret = {
2833d1bec99SHannes Frederic Sowa .initialized = false,
2843d1bec99SHannes Frederic Sowa },
2853985e8a3SErik Kline .use_oif_addrs_only = 0,
28635103d11SAndy Gospodarek .ignore_routes_with_linkdown = 0,
287f1705ec1SDavid Ahern .keep_addr_on_down = 0,
2881ababebaSDavid Lebrun .seg6_enabled = 0,
289bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
290bf355b8dSDavid Lebrun .seg6_require_hmac = 0,
291bf355b8dSDavid Lebrun #endif
292adc176c5SErik Nordmark .enhanced_dad = 1,
293d35a00b8SFelix Jia .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
294df789fe7SDavid Forster .disable_policy = 0,
2958610c7c6SAlexander Aring .rpl_seg_enabled = 0,
2969ee11f0fSJustin Iurman .ioam6_enabled = 0,
2979ee11f0fSJustin Iurman .ioam6_id = IOAM6_DEFAULT_IF_ID,
2989ee11f0fSJustin Iurman .ioam6_id_wide = IOAM6_DEFAULT_IF_ID_WIDE,
29918ac597aSJames Prestwood .ndisc_evict_nocarrier = 1,
3001da177e4SLinus Torvalds };
3011da177e4SLinus Torvalds
3021f372c7bSMike Manning /* Check if link is ready: is it up and is a valid qdisc available */
addrconf_link_ready(const struct net_device * dev)3031f372c7bSMike Manning static inline bool addrconf_link_ready(const struct net_device *dev)
304f24e3d65SMitsuru Chinen {
3051f372c7bSMike Manning return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
306f24e3d65SMitsuru Chinen }
307f24e3d65SMitsuru Chinen
addrconf_del_rs_timer(struct inet6_dev * idev)308b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev)
3091da177e4SLinus Torvalds {
310b7b1bfceSHannes Frederic Sowa if (del_timer(&idev->rs_timer))
311b7b1bfceSHannes Frederic Sowa __in6_dev_put(idev);
312b7b1bfceSHannes Frederic Sowa }
313b7b1bfceSHannes Frederic Sowa
addrconf_del_dad_work(struct inet6_ifaddr * ifp)314c15b1ccaSHannes Frederic Sowa static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
315b7b1bfceSHannes Frederic Sowa {
316c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work))
3171da177e4SLinus Torvalds __in6_ifa_put(ifp);
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds
addrconf_mod_rs_timer(struct inet6_dev * idev,unsigned long when)320b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev,
3211da177e4SLinus Torvalds unsigned long when)
3221da177e4SLinus Torvalds {
32306a07169SZiyang Xuan if (!mod_timer(&idev->rs_timer, jiffies + when))
324b7b1bfceSHannes Frederic Sowa in6_dev_hold(idev);
3251da177e4SLinus Torvalds }
326b7b1bfceSHannes Frederic Sowa
addrconf_mod_dad_work(struct inet6_ifaddr * ifp,unsigned long delay)327c15b1ccaSHannes Frederic Sowa static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
328c15b1ccaSHannes Frederic Sowa unsigned long delay)
329b7b1bfceSHannes Frederic Sowa {
330b7b1bfceSHannes Frederic Sowa in6_ifa_hold(ifp);
331f8a894b2SXin Long if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
332f8a894b2SXin Long in6_ifa_put(ifp);
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds
snmp6_alloc_dev(struct inet6_dev * idev)3357f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
3367f7d9a6bSHerbert Xu {
337827da44cSJohn Stultz int i;
338827da44cSJohn Stultz
339425b9c7fSVasily Averin idev->stats.ipv6 = alloc_percpu_gfp(struct ipstats_mib, GFP_KERNEL_ACCOUNT);
340698365faSWANG Cong if (!idev->stats.ipv6)
3417f7d9a6bSHerbert Xu goto err_ip;
342827da44cSJohn Stultz
343827da44cSJohn Stultz for_each_possible_cpu(i) {
344827da44cSJohn Stultz struct ipstats_mib *addrconf_stats;
345698365faSWANG Cong addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
346827da44cSJohn Stultz u64_stats_init(&addrconf_stats->syncp);
347827da44cSJohn Stultz }
348827da44cSJohn Stultz
349827da44cSJohn Stultz
350be281e55SEric Dumazet idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
351be281e55SEric Dumazet GFP_KERNEL);
352be281e55SEric Dumazet if (!idev->stats.icmpv6dev)
3537f7d9a6bSHerbert Xu goto err_icmp;
354be281e55SEric Dumazet idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
355425b9c7fSVasily Averin GFP_KERNEL_ACCOUNT);
356be281e55SEric Dumazet if (!idev->stats.icmpv6msgdev)
35714878f75SDavid L Stevens goto err_icmpmsg;
3587f7d9a6bSHerbert Xu
3597f7d9a6bSHerbert Xu return 0;
3607f7d9a6bSHerbert Xu
36114878f75SDavid L Stevens err_icmpmsg:
362be281e55SEric Dumazet kfree(idev->stats.icmpv6dev);
3637f7d9a6bSHerbert Xu err_icmp:
364698365faSWANG Cong free_percpu(idev->stats.ipv6);
3657f7d9a6bSHerbert Xu err_ip:
366aaf70ec7SPavel Emelyanov return -ENOMEM;
3677f7d9a6bSHerbert Xu }
3687f7d9a6bSHerbert Xu
ipv6_add_dev(struct net_device * dev)3691da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
3701da177e4SLinus Torvalds {
3711da177e4SLinus Torvalds struct inet6_dev *ndev;
372a317a2f1SWANG Cong int err = -ENOMEM;
3731da177e4SLinus Torvalds
3741da177e4SLinus Torvalds ASSERT_RTNL();
3751da177e4SLinus Torvalds
376e5f80fcfSEric Dumazet if (dev->mtu < IPV6_MIN_MTU && dev != blackhole_netdev)
377a317a2f1SWANG Cong return ERR_PTR(-EINVAL);
3781da177e4SLinus Torvalds
379425b9c7fSVasily Averin ndev = kzalloc(sizeof(*ndev), GFP_KERNEL_ACCOUNT);
38063159f29SIan Morris if (!ndev)
381a317a2f1SWANG Cong return ERR_PTR(err);
3821da177e4SLinus Torvalds
3831da177e4SLinus Torvalds rwlock_init(&ndev->lock);
3841da177e4SLinus Torvalds ndev->dev = dev;
385502a2ffdSstephen hemminger INIT_LIST_HEAD(&ndev->addr_list);
386e99e88a9SKees Cook timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0);
387c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3889b29c696SHannes Frederic Sowa
3899b29c696SHannes Frederic Sowa if (ndev->cnf.stable_secret.initialized)
390d35a00b8SFelix Jia ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
3919b29c696SHannes Frederic Sowa
3921da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu;
39349b99da2SRocco Yue ndev->ra_mtu = 0;
3941da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
39563159f29SIan Morris if (!ndev->nd_parms) {
3961da177e4SLinus Torvalds kfree(ndev);
397a317a2f1SWANG Cong return ERR_PTR(err);
3981da177e4SLinus Torvalds }
3990187bdfbSBen Hutchings if (ndev->cnf.forwarding)
4000187bdfbSBen Hutchings dev_disable_lro(dev);
4011da177e4SLinus Torvalds /* We refer to the device */
402d62607c3SJakub Kicinski netdev_hold(dev, &ndev->dev_tracker, GFP_KERNEL);
4031da177e4SLinus Torvalds
4041da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) {
405e32ac250SJoe Perches netdev_dbg(dev, "%s: cannot allocate memory for statistics\n",
406e32ac250SJoe Perches __func__);
4071da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms);
408d62607c3SJakub Kicinski netdev_put(dev, &ndev->dev_tracker);
4098603e33dSRoy Li kfree(ndev);
410a317a2f1SWANG Cong return ERR_PTR(err);
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds
413dd263a8cSIdo Schimmel if (dev != blackhole_netdev) {
4141da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) {
415e32ac250SJoe Perches netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n",
416ba3542e1Sdingtianhong __func__, dev->name);
417a317a2f1SWANG Cong goto err_release;
4181da177e4SLinus Torvalds }
419e5f80fcfSEric Dumazet }
4209d6280daSJiri Bohac /* One reference from device. */
4211be92460SReshetova, Elena refcount_set(&ndev->refcnt, 1);
4221da177e4SLinus Torvalds
4231b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
4241b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1;
4251b34be74SYOSHIFUJI Hideaki
42607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
427c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
428f3213831SJoe Perches pr_info("%s: Disabled Multicast RS\n", dev->name);
429c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0;
430c7dc89c0SFred L. Templin }
431b077d7abSYOSHIFUJI Hideaki #endif
432b077d7abSYOSHIFUJI Hideaki
433372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list);
43476506a98SJiri Bohac ndev->desync_factor = U32_MAX;
4351da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) ||
4361da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL ||
4379625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 ||
4381da177e4SLinus Torvalds dev->type == ARPHRD_SIT ||
4391da177e4SLinus Torvalds dev->type == ARPHRD_NONE) {
4400be669bbSJoerg Roedel ndev->cnf.use_tempaddr = -1;
441969c5464SFernando Gont }
4425d9efa7eSDavid S. Miller
443914faa14SDaniel Borkmann ndev->token = in6addr_any;
4441da177e4SLinus Torvalds
4451f372c7bSMike Manning if (netif_running(dev) && addrconf_link_ready(dev))
44653aadcc9SHerbert Xu ndev->if_flags |= IF_READY;
44753aadcc9SHerbert Xu
4481da177e4SLinus Torvalds ipv6_mc_init_dev(ndev);
4491da177e4SLinus Torvalds ndev->tstamp = jiffies;
450e5f80fcfSEric Dumazet if (dev != blackhole_netdev) {
451a317a2f1SWANG Cong err = addrconf_sysctl_register(ndev);
452a317a2f1SWANG Cong if (err) {
453a317a2f1SWANG Cong ipv6_mc_destroy_dev(ndev);
4542a189f9eSSabrina Dubroca snmp6_unregister_dev(ndev);
455a317a2f1SWANG Cong goto err_release;
456a317a2f1SWANG Cong }
457e5f80fcfSEric Dumazet }
45830c4cf57SDavid L Stevens /* protected by rtnl_lock */
459cf778b00SEric Dumazet rcu_assign_pointer(dev->ip6_ptr, ndev);
460d88ae4ccSYOSHIFUJI Hideaki
461e5f80fcfSEric Dumazet if (dev != blackhole_netdev) {
4622c5e8933SHannes Frederic Sowa /* Join interface-local all-node multicast group */
4632c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
4642c5e8933SHannes Frederic Sowa
465d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */
466f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
467d88ae4ccSYOSHIFUJI Hideaki
468d6ddef9eSLi Wei /* Join all-router multicast group if forwarding is set */
4698b2aaedeSLi Wei if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
470d6ddef9eSLi Wei ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
471e5f80fcfSEric Dumazet }
4721da177e4SLinus Torvalds return ndev;
473a317a2f1SWANG Cong
474a317a2f1SWANG Cong err_release:
475a317a2f1SWANG Cong neigh_parms_release(&nd_tbl, ndev->nd_parms);
476a317a2f1SWANG Cong ndev->dead = 1;
477a317a2f1SWANG Cong in6_dev_finish_destroy(ndev);
478a317a2f1SWANG Cong return ERR_PTR(err);
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds
ipv6_find_idev(struct net_device * dev)481cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
4821da177e4SLinus Torvalds {
4831da177e4SLinus Torvalds struct inet6_dev *idev;
4841da177e4SLinus Torvalds
4851da177e4SLinus Torvalds ASSERT_RTNL();
4861da177e4SLinus Torvalds
487e21e8467SStephen Hemminger idev = __in6_dev_get(dev);
488e21e8467SStephen Hemminger if (!idev) {
489e21e8467SStephen Hemminger idev = ipv6_add_dev(dev);
490a317a2f1SWANG Cong if (IS_ERR(idev))
491db0b99f5SSabrina Dubroca return idev;
4921da177e4SLinus Torvalds }
493c5e33bddSYOSHIFUJI Hideaki
4941da177e4SLinus Torvalds if (dev->flags&IFF_UP)
4951da177e4SLinus Torvalds ipv6_mc_up(idev);
4961da177e4SLinus Torvalds return idev;
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds
inet6_netconf_msgsize_devconf(int type)499f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type)
500f3a1bfb1SNicolas Dichtel {
501f3a1bfb1SNicolas Dichtel int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
502f3a1bfb1SNicolas Dichtel + nla_total_size(4); /* NETCONFA_IFINDEX */
503136ba622SZhang Shengju bool all = false;
504f3a1bfb1SNicolas Dichtel
505136ba622SZhang Shengju if (type == NETCONFA_ALL)
506136ba622SZhang Shengju all = true;
507136ba622SZhang Shengju
508136ba622SZhang Shengju if (all || type == NETCONFA_FORWARDING)
509f3a1bfb1SNicolas Dichtel size += nla_total_size(4);
510b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
511136ba622SZhang Shengju if (all || type == NETCONFA_MC_FORWARDING)
512d67b8c61SNicolas Dichtel size += nla_total_size(4);
513b1afce95SDavid S. Miller #endif
514136ba622SZhang Shengju if (all || type == NETCONFA_PROXY_NEIGH)
515c92d5491Sstephen hemminger size += nla_total_size(4);
516f3a1bfb1SNicolas Dichtel
517136ba622SZhang Shengju if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
51835103d11SAndy Gospodarek size += nla_total_size(4);
51935103d11SAndy Gospodarek
520f3a1bfb1SNicolas Dichtel return size;
521f3a1bfb1SNicolas Dichtel }
522f3a1bfb1SNicolas Dichtel
inet6_netconf_fill_devconf(struct sk_buff * skb,int ifindex,struct ipv6_devconf * devconf,u32 portid,u32 seq,int event,unsigned int flags,int type)523f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
524f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf, u32 portid,
525f3a1bfb1SNicolas Dichtel u32 seq, int event, unsigned int flags,
526f3a1bfb1SNicolas Dichtel int type)
527f3a1bfb1SNicolas Dichtel {
528f3a1bfb1SNicolas Dichtel struct nlmsghdr *nlh;
529f3a1bfb1SNicolas Dichtel struct netconfmsg *ncm;
530136ba622SZhang Shengju bool all = false;
531f3a1bfb1SNicolas Dichtel
532f3a1bfb1SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
533f3a1bfb1SNicolas Dichtel flags);
53463159f29SIan Morris if (!nlh)
535f3a1bfb1SNicolas Dichtel return -EMSGSIZE;
536f3a1bfb1SNicolas Dichtel
537136ba622SZhang Shengju if (type == NETCONFA_ALL)
538136ba622SZhang Shengju all = true;
539136ba622SZhang Shengju
540f3a1bfb1SNicolas Dichtel ncm = nlmsg_data(nlh);
541f3a1bfb1SNicolas Dichtel ncm->ncm_family = AF_INET6;
542f3a1bfb1SNicolas Dichtel
543f3a1bfb1SNicolas Dichtel if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
544f3a1bfb1SNicolas Dichtel goto nla_put_failure;
545f3a1bfb1SNicolas Dichtel
54623452170SDavid Ahern if (!devconf)
54723452170SDavid Ahern goto out;
54823452170SDavid Ahern
549136ba622SZhang Shengju if ((all || type == NETCONFA_FORWARDING) &&
550f3a1bfb1SNicolas Dichtel nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
551f3a1bfb1SNicolas Dichtel goto nla_put_failure;
552b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
553136ba622SZhang Shengju if ((all || type == NETCONFA_MC_FORWARDING) &&
554d67b8c61SNicolas Dichtel nla_put_s32(skb, NETCONFA_MC_FORWARDING,
555145c7a79SEric Dumazet atomic_read(&devconf->mc_forwarding)) < 0)
556d67b8c61SNicolas Dichtel goto nla_put_failure;
557b1afce95SDavid S. Miller #endif
558136ba622SZhang Shengju if ((all || type == NETCONFA_PROXY_NEIGH) &&
559c92d5491Sstephen hemminger nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
560c92d5491Sstephen hemminger goto nla_put_failure;
561c92d5491Sstephen hemminger
562136ba622SZhang Shengju if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
56335103d11SAndy Gospodarek nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
56435103d11SAndy Gospodarek devconf->ignore_routes_with_linkdown) < 0)
56535103d11SAndy Gospodarek goto nla_put_failure;
56635103d11SAndy Gospodarek
56723452170SDavid Ahern out:
568053c095aSJohannes Berg nlmsg_end(skb, nlh);
569053c095aSJohannes Berg return 0;
570f3a1bfb1SNicolas Dichtel
571f3a1bfb1SNicolas Dichtel nla_put_failure:
572f3a1bfb1SNicolas Dichtel nlmsg_cancel(skb, nlh);
573f3a1bfb1SNicolas Dichtel return -EMSGSIZE;
574f3a1bfb1SNicolas Dichtel }
575f3a1bfb1SNicolas Dichtel
inet6_netconf_notify_devconf(struct net * net,int event,int type,int ifindex,struct ipv6_devconf * devconf)57685b3daadSDavid Ahern void inet6_netconf_notify_devconf(struct net *net, int event, int type,
57785b3daadSDavid Ahern int ifindex, struct ipv6_devconf *devconf)
578f3a1bfb1SNicolas Dichtel {
579f3a1bfb1SNicolas Dichtel struct sk_buff *skb;
580f3a1bfb1SNicolas Dichtel int err = -ENOBUFS;
581f3a1bfb1SNicolas Dichtel
582927265bcSEric Dumazet skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
58363159f29SIan Morris if (!skb)
584f3a1bfb1SNicolas Dichtel goto errout;
585f3a1bfb1SNicolas Dichtel
586f3a1bfb1SNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
58785b3daadSDavid Ahern event, 0, type);
588f3a1bfb1SNicolas Dichtel if (err < 0) {
589f3a1bfb1SNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
590f3a1bfb1SNicolas Dichtel WARN_ON(err == -EMSGSIZE);
591f3a1bfb1SNicolas Dichtel kfree_skb(skb);
592f3a1bfb1SNicolas Dichtel goto errout;
593f3a1bfb1SNicolas Dichtel }
594927265bcSEric Dumazet rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
595f3a1bfb1SNicolas Dichtel return;
596f3a1bfb1SNicolas Dichtel errout:
597f3a1bfb1SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
598f3a1bfb1SNicolas Dichtel }
599f3a1bfb1SNicolas Dichtel
60076f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
60176f8f6cbSNicolas Dichtel [NETCONFA_IFINDEX] = { .len = sizeof(int) },
60276f8f6cbSNicolas Dichtel [NETCONFA_FORWARDING] = { .len = sizeof(int) },
603c92d5491Sstephen hemminger [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
60435103d11SAndy Gospodarek [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
60576f8f6cbSNicolas Dichtel };
60676f8f6cbSNicolas Dichtel
inet6_netconf_valid_get_req(struct sk_buff * skb,const struct nlmsghdr * nlh,struct nlattr ** tb,struct netlink_ext_ack * extack)60738d51810SJakub Kicinski static int inet6_netconf_valid_get_req(struct sk_buff *skb,
60838d51810SJakub Kicinski const struct nlmsghdr *nlh,
60938d51810SJakub Kicinski struct nlattr **tb,
61038d51810SJakub Kicinski struct netlink_ext_ack *extack)
61138d51810SJakub Kicinski {
61238d51810SJakub Kicinski int i, err;
61338d51810SJakub Kicinski
61438d51810SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
61538d51810SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request");
61638d51810SJakub Kicinski return -EINVAL;
61738d51810SJakub Kicinski }
61838d51810SJakub Kicinski
61938d51810SJakub Kicinski if (!netlink_strict_get_check(skb))
6208cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
6218cb08174SJohannes Berg tb, NETCONFA_MAX,
6228cb08174SJohannes Berg devconf_ipv6_policy, extack);
62338d51810SJakub Kicinski
6248cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
6258cb08174SJohannes Berg tb, NETCONFA_MAX,
6268cb08174SJohannes Berg devconf_ipv6_policy, extack);
62738d51810SJakub Kicinski if (err)
62838d51810SJakub Kicinski return err;
62938d51810SJakub Kicinski
63038d51810SJakub Kicinski for (i = 0; i <= NETCONFA_MAX; i++) {
63138d51810SJakub Kicinski if (!tb[i])
63238d51810SJakub Kicinski continue;
63338d51810SJakub Kicinski
63438d51810SJakub Kicinski switch (i) {
63538d51810SJakub Kicinski case NETCONFA_IFINDEX:
63638d51810SJakub Kicinski break;
63738d51810SJakub Kicinski default:
63838d51810SJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
63938d51810SJakub Kicinski return -EINVAL;
64038d51810SJakub Kicinski }
64138d51810SJakub Kicinski }
64238d51810SJakub Kicinski
64338d51810SJakub Kicinski return 0;
64438d51810SJakub Kicinski }
64538d51810SJakub Kicinski
inet6_netconf_get_devconf(struct sk_buff * in_skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)64676f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
647c21ef3e3SDavid Ahern struct nlmsghdr *nlh,
648c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
64976f8f6cbSNicolas Dichtel {
65076f8f6cbSNicolas Dichtel struct net *net = sock_net(in_skb->sk);
65176f8f6cbSNicolas Dichtel struct nlattr *tb[NETCONFA_MAX+1];
6524ea2607fSFlorian Westphal struct inet6_dev *in6_dev = NULL;
6534ea2607fSFlorian Westphal struct net_device *dev = NULL;
65476f8f6cbSNicolas Dichtel struct sk_buff *skb;
65576f8f6cbSNicolas Dichtel struct ipv6_devconf *devconf;
65676f8f6cbSNicolas Dichtel int ifindex;
65776f8f6cbSNicolas Dichtel int err;
65876f8f6cbSNicolas Dichtel
65938d51810SJakub Kicinski err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack);
66076f8f6cbSNicolas Dichtel if (err < 0)
6614ea2607fSFlorian Westphal return err;
6624ea2607fSFlorian Westphal
6634ea2607fSFlorian Westphal if (!tb[NETCONFA_IFINDEX])
6644ea2607fSFlorian Westphal return -EINVAL;
66576f8f6cbSNicolas Dichtel
666a97eb33fSAnton Protopopov err = -EINVAL;
66776f8f6cbSNicolas Dichtel ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
66876f8f6cbSNicolas Dichtel switch (ifindex) {
66976f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_ALL:
67076f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_all;
67176f8f6cbSNicolas Dichtel break;
67276f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_DEFAULT:
67376f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_dflt;
67476f8f6cbSNicolas Dichtel break;
67576f8f6cbSNicolas Dichtel default:
6764ea2607fSFlorian Westphal dev = dev_get_by_index(net, ifindex);
67763159f29SIan Morris if (!dev)
6784ea2607fSFlorian Westphal return -EINVAL;
6794ea2607fSFlorian Westphal in6_dev = in6_dev_get(dev);
68063159f29SIan Morris if (!in6_dev)
68176f8f6cbSNicolas Dichtel goto errout;
68276f8f6cbSNicolas Dichtel devconf = &in6_dev->cnf;
68376f8f6cbSNicolas Dichtel break;
68476f8f6cbSNicolas Dichtel }
68576f8f6cbSNicolas Dichtel
68676f8f6cbSNicolas Dichtel err = -ENOBUFS;
6874ea2607fSFlorian Westphal skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
68863159f29SIan Morris if (!skb)
68976f8f6cbSNicolas Dichtel goto errout;
69076f8f6cbSNicolas Dichtel
69176f8f6cbSNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
69276f8f6cbSNicolas Dichtel NETLINK_CB(in_skb).portid,
69376f8f6cbSNicolas Dichtel nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
694136ba622SZhang Shengju NETCONFA_ALL);
69576f8f6cbSNicolas Dichtel if (err < 0) {
69676f8f6cbSNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
69776f8f6cbSNicolas Dichtel WARN_ON(err == -EMSGSIZE);
69876f8f6cbSNicolas Dichtel kfree_skb(skb);
69976f8f6cbSNicolas Dichtel goto errout;
70076f8f6cbSNicolas Dichtel }
70176f8f6cbSNicolas Dichtel err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
70276f8f6cbSNicolas Dichtel errout:
7034ea2607fSFlorian Westphal if (in6_dev)
7044ea2607fSFlorian Westphal in6_dev_put(in6_dev);
7054ea2607fSFlorian Westphal dev_put(dev);
70676f8f6cbSNicolas Dichtel return err;
70776f8f6cbSNicolas Dichtel }
70876f8f6cbSNicolas Dichtel
709cf761c81SEric Dumazet /* Combine dev_addr_genid and dev_base_seq to detect changes.
710cf761c81SEric Dumazet */
inet6_base_seq(const struct net * net)711cf761c81SEric Dumazet static u32 inet6_base_seq(const struct net *net)
712cf761c81SEric Dumazet {
713cf761c81SEric Dumazet u32 res = atomic_read(&net->ipv6.dev_addr_genid) +
714cf761c81SEric Dumazet net->dev_base_seq;
715cf761c81SEric Dumazet
716cf761c81SEric Dumazet /* Must not return 0 (see nl_dump_check_consistent()).
717cf761c81SEric Dumazet * Chose a value far away from 0.
718cf761c81SEric Dumazet */
719cf761c81SEric Dumazet if (!res)
720cf761c81SEric Dumazet res = 0x80000000;
721cf761c81SEric Dumazet return res;
722cf761c81SEric Dumazet }
723cf761c81SEric Dumazet
724cf761c81SEric Dumazet
inet6_netconf_dump_devconf(struct sk_buff * skb,struct netlink_callback * cb)7257a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
7267a674200SNicolas Dichtel struct netlink_callback *cb)
7277a674200SNicolas Dichtel {
728addd383fSDavid Ahern const struct nlmsghdr *nlh = cb->nlh;
7297a674200SNicolas Dichtel struct net *net = sock_net(skb->sk);
7307a674200SNicolas Dichtel int h, s_h;
7317a674200SNicolas Dichtel int idx, s_idx;
7327a674200SNicolas Dichtel struct net_device *dev;
7337a674200SNicolas Dichtel struct inet6_dev *idev;
7347a674200SNicolas Dichtel struct hlist_head *head;
7357a674200SNicolas Dichtel
736addd383fSDavid Ahern if (cb->strict_check) {
737addd383fSDavid Ahern struct netlink_ext_ack *extack = cb->extack;
738addd383fSDavid Ahern struct netconfmsg *ncm;
739addd383fSDavid Ahern
740addd383fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
741addd383fSDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
742addd383fSDavid Ahern return -EINVAL;
743addd383fSDavid Ahern }
744addd383fSDavid Ahern
745addd383fSDavid Ahern if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
746addd383fSDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
747addd383fSDavid Ahern return -EINVAL;
748addd383fSDavid Ahern }
749addd383fSDavid Ahern }
750addd383fSDavid Ahern
7517a674200SNicolas Dichtel s_h = cb->args[0];
7527a674200SNicolas Dichtel s_idx = idx = cb->args[1];
7537a674200SNicolas Dichtel
7547a674200SNicolas Dichtel for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
7557a674200SNicolas Dichtel idx = 0;
7567a674200SNicolas Dichtel head = &net->dev_index_head[h];
7577a674200SNicolas Dichtel rcu_read_lock();
758cf761c81SEric Dumazet cb->seq = inet6_base_seq(net);
7597a674200SNicolas Dichtel hlist_for_each_entry_rcu(dev, head, index_hlist) {
7607a674200SNicolas Dichtel if (idx < s_idx)
7617a674200SNicolas Dichtel goto cont;
7627a674200SNicolas Dichtel idev = __in6_dev_get(dev);
7637a674200SNicolas Dichtel if (!idev)
7647a674200SNicolas Dichtel goto cont;
7657a674200SNicolas Dichtel
7667a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, dev->ifindex,
7677a674200SNicolas Dichtel &idev->cnf,
7687a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid,
769addd383fSDavid Ahern nlh->nlmsg_seq,
7707a674200SNicolas Dichtel RTM_NEWNETCONF,
7717a674200SNicolas Dichtel NLM_F_MULTI,
772136ba622SZhang Shengju NETCONFA_ALL) < 0) {
7737a674200SNicolas Dichtel rcu_read_unlock();
7747a674200SNicolas Dichtel goto done;
7757a674200SNicolas Dichtel }
77663998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb));
7777a674200SNicolas Dichtel cont:
7787a674200SNicolas Dichtel idx++;
7797a674200SNicolas Dichtel }
7807a674200SNicolas Dichtel rcu_read_unlock();
7817a674200SNicolas Dichtel }
7827a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES) {
7837a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
7847a674200SNicolas Dichtel net->ipv6.devconf_all,
7857a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid,
786addd383fSDavid Ahern nlh->nlmsg_seq,
7877a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI,
788136ba622SZhang Shengju NETCONFA_ALL) < 0)
7897a674200SNicolas Dichtel goto done;
7907a674200SNicolas Dichtel else
7917a674200SNicolas Dichtel h++;
7927a674200SNicolas Dichtel }
7937a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES + 1) {
7947a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
7957a674200SNicolas Dichtel net->ipv6.devconf_dflt,
7967a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid,
797addd383fSDavid Ahern nlh->nlmsg_seq,
7987a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI,
799136ba622SZhang Shengju NETCONFA_ALL) < 0)
8007a674200SNicolas Dichtel goto done;
8017a674200SNicolas Dichtel else
8027a674200SNicolas Dichtel h++;
8037a674200SNicolas Dichtel }
8047a674200SNicolas Dichtel done:
8057a674200SNicolas Dichtel cb->args[0] = h;
8067a674200SNicolas Dichtel cb->args[1] = idx;
8077a674200SNicolas Dichtel
8087a674200SNicolas Dichtel return skb->len;
8097a674200SNicolas Dichtel }
8107a674200SNicolas Dichtel
8111da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
dev_forward_change(struct inet6_dev * idev)8121da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
8131da177e4SLinus Torvalds {
8141da177e4SLinus Torvalds struct net_device *dev;
8151da177e4SLinus Torvalds struct inet6_ifaddr *ifa;
81651454ea4SNiels Dossche LIST_HEAD(tmp_addr_list);
8171da177e4SLinus Torvalds
8181da177e4SLinus Torvalds if (!idev)
8191da177e4SLinus Torvalds return;
8201da177e4SLinus Torvalds dev = idev->dev;
8210187bdfbSBen Hutchings if (idev->cnf.forwarding)
8220187bdfbSBen Hutchings dev_disable_lro(dev);
8231a940835SAmerigo Wang if (dev->flags & IFF_MULTICAST) {
8242c5e8933SHannes Frederic Sowa if (idev->cnf.forwarding) {
825f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
8262c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
8272c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
8282c5e8933SHannes Frederic Sowa } else {
829f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
8302c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
8312c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
8322c5e8933SHannes Frederic Sowa }
8331da177e4SLinus Torvalds }
834502a2ffdSstephen hemminger
83551454ea4SNiels Dossche read_lock_bh(&idev->lock);
836502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) {
8372c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE)
8382c12a74cSMichal Wrobel continue;
83951454ea4SNiels Dossche list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
84051454ea4SNiels Dossche }
84151454ea4SNiels Dossche read_unlock_bh(&idev->lock);
84251454ea4SNiels Dossche
84351454ea4SNiels Dossche while (!list_empty(&tmp_addr_list)) {
84451454ea4SNiels Dossche ifa = list_first_entry(&tmp_addr_list,
84551454ea4SNiels Dossche struct inet6_ifaddr, if_list_aux);
84651454ea4SNiels Dossche list_del(&ifa->if_list_aux);
8471da177e4SLinus Torvalds if (idev->cnf.forwarding)
8481da177e4SLinus Torvalds addrconf_join_anycast(ifa);
8491da177e4SLinus Torvalds else
8501da177e4SLinus Torvalds addrconf_leave_anycast(ifa);
8511da177e4SLinus Torvalds }
85251454ea4SNiels Dossche
85385b3daadSDavid Ahern inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
85485b3daadSDavid Ahern NETCONFA_FORWARDING,
855f3a1bfb1SNicolas Dichtel dev->ifindex, &idev->cnf);
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds
8581da177e4SLinus Torvalds
addrconf_forward_change(struct net * net,__s32 newf)859e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
8601da177e4SLinus Torvalds {
8611da177e4SLinus Torvalds struct net_device *dev;
8621da177e4SLinus Torvalds struct inet6_dev *idev;
8631da177e4SLinus Torvalds
8644acd4945SBen Hutchings for_each_netdev(net, dev) {
8651da177e4SLinus Torvalds idev = __in6_dev_get(dev);
8661da177e4SLinus Torvalds if (idev) {
867e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf);
868e186932bSPavel Emelyanov idev->cnf.forwarding = newf;
8691da177e4SLinus Torvalds if (changed)
8701da177e4SLinus Torvalds dev_forward_change(idev);
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds }
874c8fecf22SPavel Emelyanov
addrconf_fixup_forwarding(struct ctl_table * table,int * p,int newf)875013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
876c8fecf22SPavel Emelyanov {
877bff16c2fSPavel Emelyanov struct net *net;
878013d97e9SFrancesco Ruggeri int old;
879013d97e9SFrancesco Ruggeri
880013d97e9SFrancesco Ruggeri if (!rtnl_trylock())
881013d97e9SFrancesco Ruggeri return restart_syscall();
882bff16c2fSPavel Emelyanov
883bff16c2fSPavel Emelyanov net = (struct net *)table->extra2;
884013d97e9SFrancesco Ruggeri old = *p;
885013d97e9SFrancesco Ruggeri *p = newf;
886c8fecf22SPavel Emelyanov
887013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->forwarding) {
888f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old))
88985b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
89085b3daadSDavid Ahern NETCONFA_FORWARDING,
891f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_DEFAULT,
892f3a1bfb1SNicolas Dichtel net->ipv6.devconf_dflt);
893013d97e9SFrancesco Ruggeri rtnl_unlock();
894013d97e9SFrancesco Ruggeri return 0;
89588af182eSEric W. Biederman }
896b325fddbSStephen Hemminger
897e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) {
898d26c638cSNicolas Dichtel int old_dflt = net->ipv6.devconf_dflt->forwarding;
899d26c638cSNicolas Dichtel
900e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf;
901d26c638cSNicolas Dichtel if ((!newf) ^ (!old_dflt))
90285b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
90385b3daadSDavid Ahern NETCONFA_FORWARDING,
904d26c638cSNicolas Dichtel NETCONFA_IFINDEX_DEFAULT,
905d26c638cSNicolas Dichtel net->ipv6.devconf_dflt);
906d26c638cSNicolas Dichtel
907e186932bSPavel Emelyanov addrconf_forward_change(net, newf);
908f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old))
90985b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
91085b3daadSDavid Ahern NETCONFA_FORWARDING,
911f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_ALL,
912f3a1bfb1SNicolas Dichtel net->ipv6.devconf_all);
913013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old))
914c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1);
9150187bdfbSBen Hutchings rtnl_unlock();
916c8fecf22SPavel Emelyanov
917013d97e9SFrancesco Ruggeri if (newf)
9187b4da532SDaniel Lezcano rt6_purge_dflt_routers(net);
919b325fddbSStephen Hemminger return 1;
920c8fecf22SPavel Emelyanov }
92135103d11SAndy Gospodarek
addrconf_linkdown_change(struct net * net,__s32 newf)92235103d11SAndy Gospodarek static void addrconf_linkdown_change(struct net *net, __s32 newf)
92335103d11SAndy Gospodarek {
92435103d11SAndy Gospodarek struct net_device *dev;
92535103d11SAndy Gospodarek struct inet6_dev *idev;
92635103d11SAndy Gospodarek
92735103d11SAndy Gospodarek for_each_netdev(net, dev) {
92835103d11SAndy Gospodarek idev = __in6_dev_get(dev);
92935103d11SAndy Gospodarek if (idev) {
93035103d11SAndy Gospodarek int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
93135103d11SAndy Gospodarek
93235103d11SAndy Gospodarek idev->cnf.ignore_routes_with_linkdown = newf;
93335103d11SAndy Gospodarek if (changed)
93435103d11SAndy Gospodarek inet6_netconf_notify_devconf(dev_net(dev),
93585b3daadSDavid Ahern RTM_NEWNETCONF,
93635103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
93735103d11SAndy Gospodarek dev->ifindex,
93835103d11SAndy Gospodarek &idev->cnf);
93935103d11SAndy Gospodarek }
94035103d11SAndy Gospodarek }
94135103d11SAndy Gospodarek }
94235103d11SAndy Gospodarek
addrconf_fixup_linkdown(struct ctl_table * table,int * p,int newf)94335103d11SAndy Gospodarek static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
94435103d11SAndy Gospodarek {
94535103d11SAndy Gospodarek struct net *net;
94635103d11SAndy Gospodarek int old;
94735103d11SAndy Gospodarek
94835103d11SAndy Gospodarek if (!rtnl_trylock())
94935103d11SAndy Gospodarek return restart_syscall();
95035103d11SAndy Gospodarek
95135103d11SAndy Gospodarek net = (struct net *)table->extra2;
95235103d11SAndy Gospodarek old = *p;
95335103d11SAndy Gospodarek *p = newf;
95435103d11SAndy Gospodarek
95535103d11SAndy Gospodarek if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
95635103d11SAndy Gospodarek if ((!newf) ^ (!old))
95735103d11SAndy Gospodarek inet6_netconf_notify_devconf(net,
95885b3daadSDavid Ahern RTM_NEWNETCONF,
95935103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
96035103d11SAndy Gospodarek NETCONFA_IFINDEX_DEFAULT,
96135103d11SAndy Gospodarek net->ipv6.devconf_dflt);
96235103d11SAndy Gospodarek rtnl_unlock();
96335103d11SAndy Gospodarek return 0;
96435103d11SAndy Gospodarek }
96535103d11SAndy Gospodarek
96635103d11SAndy Gospodarek if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
96735103d11SAndy Gospodarek net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
96835103d11SAndy Gospodarek addrconf_linkdown_change(net, newf);
96935103d11SAndy Gospodarek if ((!newf) ^ (!old))
97035103d11SAndy Gospodarek inet6_netconf_notify_devconf(net,
97185b3daadSDavid Ahern RTM_NEWNETCONF,
97235103d11SAndy Gospodarek NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
97335103d11SAndy Gospodarek NETCONFA_IFINDEX_ALL,
97435103d11SAndy Gospodarek net->ipv6.devconf_all);
97535103d11SAndy Gospodarek }
97635103d11SAndy Gospodarek rtnl_unlock();
97735103d11SAndy Gospodarek
97835103d11SAndy Gospodarek return 1;
97935103d11SAndy Gospodarek }
98035103d11SAndy Gospodarek
9811da177e4SLinus Torvalds #endif
9821da177e4SLinus Torvalds
9835c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
inet6_ifa_finish_destroy(struct inet6_ifaddr * ifp)9841da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
9851da177e4SLinus Torvalds {
986c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst));
987547b792cSIlpo Järvinen
9881da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
98991df42beSJoe Perches pr_debug("%s\n", __func__);
9901da177e4SLinus Torvalds #endif
9911da177e4SLinus Torvalds
9921da177e4SLinus Torvalds in6_dev_put(ifp->idev);
9931da177e4SLinus Torvalds
994c15b1ccaSHannes Frederic Sowa if (cancel_delayed_work(&ifp->dad_work))
995c15b1ccaSHannes Frederic Sowa pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
996c15b1ccaSHannes Frederic Sowa ifp);
9971da177e4SLinus Torvalds
998e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) {
999f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp);
10001da177e4SLinus Torvalds return;
10011da177e4SLinus Torvalds }
10021da177e4SLinus Torvalds
1003e5785985SLai Jiangshan kfree_rcu(ifp, rcu);
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds
1006e55ffac6SBrian Haley static void
ipv6_link_dev_addr(struct inet6_dev * idev,struct inet6_ifaddr * ifp)1007e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
1008e55ffac6SBrian Haley {
1009502a2ffdSstephen hemminger struct list_head *p;
10108a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
1011e55ffac6SBrian Haley
1012e55ffac6SBrian Haley /*
1013e55ffac6SBrian Haley * Each device address list is sorted in order of scope -
1014e55ffac6SBrian Haley * global before linklocal.
1015e55ffac6SBrian Haley */
1016502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) {
1017502a2ffdSstephen hemminger struct inet6_ifaddr *ifa
1018502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list);
10198a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
1020e55ffac6SBrian Haley break;
1021e55ffac6SBrian Haley }
1022e55ffac6SBrian Haley
10238ef802aaSEric Dumazet list_add_tail_rcu(&ifp->if_list, p);
1024e55ffac6SBrian Haley }
1025e55ffac6SBrian Haley
inet6_addr_hash(const struct net * net,const struct in6_addr * addr)10263f27fb23SEric Dumazet static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
10273eb84f49SYOSHIFUJI Hideaki {
10283f27fb23SEric Dumazet u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
10293f27fb23SEric Dumazet
10303f27fb23SEric Dumazet return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
10313eb84f49SYOSHIFUJI Hideaki }
10323eb84f49SYOSHIFUJI Hideaki
ipv6_chk_same_addr(struct net * net,const struct in6_addr * addr,struct net_device * dev,unsigned int hash)103356fc709bSEric Dumazet static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1034752a9292SEric Dumazet struct net_device *dev, unsigned int hash)
103556fc709bSEric Dumazet {
103656fc709bSEric Dumazet struct inet6_ifaddr *ifp;
103756fc709bSEric Dumazet
1038e66d1172SEric Dumazet hlist_for_each_entry(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
103956fc709bSEric Dumazet if (ipv6_addr_equal(&ifp->addr, addr)) {
104056fc709bSEric Dumazet if (!dev || ifp->idev->dev == dev)
104156fc709bSEric Dumazet return true;
104256fc709bSEric Dumazet }
104356fc709bSEric Dumazet }
104456fc709bSEric Dumazet return false;
104556fc709bSEric Dumazet }
104656fc709bSEric Dumazet
ipv6_add_addr_hash(struct net_device * dev,struct inet6_ifaddr * ifa)1047f3d9832eSDavid Ahern static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
1048f3d9832eSDavid Ahern {
1049e66d1172SEric Dumazet struct net *net = dev_net(dev);
1050e66d1172SEric Dumazet unsigned int hash = inet6_addr_hash(net, &ifa->addr);
1051f3d9832eSDavid Ahern int err = 0;
1052f3d9832eSDavid Ahern
105309eed119SEric Dumazet spin_lock_bh(&net->ipv6.addrconf_hash_lock);
1054f3d9832eSDavid Ahern
1055f3d9832eSDavid Ahern /* Ignore adding duplicate addresses on an interface */
1056e66d1172SEric Dumazet if (ipv6_chk_same_addr(net, &ifa->addr, dev, hash)) {
1057e32ac250SJoe Perches netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
1058f3d9832eSDavid Ahern err = -EEXIST;
1059752a9292SEric Dumazet } else {
1060e66d1172SEric Dumazet hlist_add_head_rcu(&ifa->addr_lst, &net->ipv6.inet6_addr_lst[hash]);
1061f3d9832eSDavid Ahern }
1062f3d9832eSDavid Ahern
106309eed119SEric Dumazet spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
1064f3d9832eSDavid Ahern
1065f3d9832eSDavid Ahern return err;
1066f3d9832eSDavid Ahern }
1067f3d9832eSDavid Ahern
10681da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
10691da177e4SLinus Torvalds
10701da177e4SLinus Torvalds static struct inet6_ifaddr *
ipv6_add_addr(struct inet6_dev * idev,struct ifa6_config * cfg,bool can_block,struct netlink_ext_ack * extack)1071e6464b8cSDavid Ahern ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
1072de95e047SDavid Ahern bool can_block, struct netlink_ext_ack *extack)
10731da177e4SLinus Torvalds {
1074f3d9832eSDavid Ahern gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
1075e6464b8cSDavid Ahern int addr_type = ipv6_addr_type(cfg->pfx);
1076df789fe7SDavid Forster struct net *net = dev_net(idev->dev);
10771da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL;
1078360a9887SDavid Ahern struct fib6_info *f6i = NULL;
10791da177e4SLinus Torvalds int err = 0;
1080d68b8270SYOSHIFUJI Hideaki
10817f6c4039SHangbin Liu if (addr_type == IPV6_ADDR_ANY) {
10827f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "Invalid address");
1083d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL);
10847f6c4039SHangbin Liu } else if (addr_type & IPV6_ADDR_MULTICAST &&
10857f6c4039SHangbin Liu !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) {
10867f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "Cannot assign multicast address without \"IFA_F_MCAUTOJOIN\" flag");
10877f6c4039SHangbin Liu return ERR_PTR(-EADDRNOTAVAIL);
10887f6c4039SHangbin Liu } else if (!(idev->dev->flags & IFF_LOOPBACK) &&
10897f6c4039SHangbin Liu !netif_is_l3_master(idev->dev) &&
10907f6c4039SHangbin Liu addr_type & IPV6_ADDR_LOOPBACK) {
10917f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "Cannot assign loopback address on this device");
10927f6c4039SHangbin Liu return ERR_PTR(-EADDRNOTAVAIL);
10937f6c4039SHangbin Liu }
10941da177e4SLinus Torvalds
10951da177e4SLinus Torvalds if (idev->dead) {
10967f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "device is going away");
10977f6c4039SHangbin Liu err = -ENODEV;
1098f3d9832eSDavid Ahern goto out;
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds
110156d417b1SBrian Haley if (idev->cnf.disable_ipv6) {
11027f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
11039bdd8d40SBrian Haley err = -EACCES;
1104f3d9832eSDavid Ahern goto out;
11059bdd8d40SBrian Haley }
11069bdd8d40SBrian Haley
1107ff7883eaSDavid Ahern /* validator notifier needs to be blocking;
1108ff7883eaSDavid Ahern * do not call in atomic context
1109ff7883eaSDavid Ahern */
1110ff7883eaSDavid Ahern if (can_block) {
1111ff7883eaSDavid Ahern struct in6_validator_info i6vi = {
1112e6464b8cSDavid Ahern .i6vi_addr = *cfg->pfx,
1113ff7883eaSDavid Ahern .i6vi_dev = idev,
1114de95e047SDavid Ahern .extack = extack,
1115ff7883eaSDavid Ahern };
1116ff7883eaSDavid Ahern
11173ad7d246SKrister Johansen err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
11183ad7d246SKrister Johansen err = notifier_to_errno(err);
1119f3d9832eSDavid Ahern if (err < 0)
11201da177e4SLinus Torvalds goto out;
1121ff7883eaSDavid Ahern }
11221da177e4SLinus Torvalds
11236126891cSVasily Averin ifa = kzalloc(sizeof(*ifa), gfp_flags | __GFP_ACCOUNT);
112463159f29SIan Morris if (!ifa) {
11251da177e4SLinus Torvalds err = -ENOBUFS;
11261da177e4SLinus Torvalds goto out;
11271da177e4SLinus Torvalds }
11281da177e4SLinus Torvalds
11297f6c4039SHangbin Liu f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags, extack);
1130360a9887SDavid Ahern if (IS_ERR(f6i)) {
1131360a9887SDavid Ahern err = PTR_ERR(f6i);
1132360a9887SDavid Ahern f6i = NULL;
11331da177e4SLinus Torvalds goto out;
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds
1136bba24896SJiri Pirko neigh_parms_data_state_setall(idev->nd_parms);
1137bba24896SJiri Pirko
1138e6464b8cSDavid Ahern ifa->addr = *cfg->pfx;
1139e6464b8cSDavid Ahern if (cfg->peer_pfx)
1140e6464b8cSDavid Ahern ifa->peer_addr = *cfg->peer_pfx;
11411da177e4SLinus Torvalds
11421da177e4SLinus Torvalds spin_lock_init(&ifa->lock);
1143c15b1ccaSHannes Frederic Sowa INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1144c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst);
1145e6464b8cSDavid Ahern ifa->scope = cfg->scope;
1146e6464b8cSDavid Ahern ifa->prefix_len = cfg->plen;
11478308f3ffSDavid Ahern ifa->rt_priority = cfg->rt_priority;
1148e6464b8cSDavid Ahern ifa->flags = cfg->ifa_flags;
114947f0bd50SJacques de Laval ifa->ifa_proto = cfg->ifa_proto;
115066eb9f86SMahesh Bandewar /* No need to add the TENTATIVE flag for addresses with NODAD */
1151e6464b8cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NODAD))
115266eb9f86SMahesh Bandewar ifa->flags |= IFA_F_TENTATIVE;
1153e6464b8cSDavid Ahern ifa->valid_lft = cfg->valid_lft;
1154e6464b8cSDavid Ahern ifa->prefered_lft = cfg->preferred_lft;
11551da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies;
1156617fe29dSDaniel Borkmann ifa->tokenized = false;
11571da177e4SLinus Torvalds
1158360a9887SDavid Ahern ifa->rt = f6i;
115957f5f544SKeir Fraser
11601da177e4SLinus Torvalds ifa->idev = idev;
1161f3d9832eSDavid Ahern in6_dev_hold(idev);
1162f3d9832eSDavid Ahern
11631da177e4SLinus Torvalds /* For caller */
1164271201c0SReshetova, Elena refcount_set(&ifa->refcnt, 1);
11651da177e4SLinus Torvalds
116609eed119SEric Dumazet rcu_read_lock();
11671da177e4SLinus Torvalds
1168f3d9832eSDavid Ahern err = ipv6_add_addr_hash(idev->dev, ifa);
1169f3d9832eSDavid Ahern if (err < 0) {
117009eed119SEric Dumazet rcu_read_unlock();
1171f3d9832eSDavid Ahern goto out;
1172f3d9832eSDavid Ahern }
11731da177e4SLinus Torvalds
117409eed119SEric Dumazet write_lock_bh(&idev->lock);
1175f3d9832eSDavid Ahern
11761da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */
1177e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa);
11781da177e4SLinus Torvalds
11791da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) {
1180372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list);
11811da177e4SLinus Torvalds in6_ifa_hold(ifa);
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds
11841da177e4SLinus Torvalds in6_ifa_hold(ifa);
118509eed119SEric Dumazet write_unlock_bh(&idev->lock);
1186f3d9832eSDavid Ahern
118709eed119SEric Dumazet rcu_read_unlock();
11881da177e4SLinus Torvalds
1189f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1190f3d9832eSDavid Ahern out:
1191f3d9832eSDavid Ahern if (unlikely(err < 0)) {
1192360a9887SDavid Ahern fib6_info_release(f6i);
119393531c67SDavid Ahern
1194f3d9832eSDavid Ahern if (ifa) {
1195f3d9832eSDavid Ahern if (ifa->idev)
1196f3d9832eSDavid Ahern in6_dev_put(ifa->idev);
11971da177e4SLinus Torvalds kfree(ifa);
1198f3d9832eSDavid Ahern }
11991da177e4SLinus Torvalds ifa = ERR_PTR(err);
12001da177e4SLinus Torvalds }
12011da177e4SLinus Torvalds
12021da177e4SLinus Torvalds return ifa;
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds
12055b84efecSThomas Haller enum cleanup_prefix_rt_t {
12065b84efecSThomas Haller CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
12075b84efecSThomas Haller CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
12085b84efecSThomas Haller CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
12095b84efecSThomas Haller };
12105b84efecSThomas Haller
12115b84efecSThomas Haller /*
12125b84efecSThomas Haller * Check, whether the prefix for ifp would still need a prefix route
12135b84efecSThomas Haller * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
12145b84efecSThomas Haller * constants.
12155b84efecSThomas Haller *
12165b84efecSThomas Haller * 1) we don't purge prefix if address was not permanent.
12175b84efecSThomas Haller * prefix is managed by its own lifetime.
12185b84efecSThomas Haller * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
12195b84efecSThomas Haller * 3) if there are no addresses, delete prefix.
12205b84efecSThomas Haller * 4) if there are still other permanent address(es),
12215b84efecSThomas Haller * corresponding prefix is still permanent.
12225b84efecSThomas Haller * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
12235b84efecSThomas Haller * don't purge the prefix, assume user space is managing it.
12245b84efecSThomas Haller * 6) otherwise, update prefix lifetime to the
12255b84efecSThomas Haller * longest valid lifetime among the corresponding
12265b84efecSThomas Haller * addresses on the device.
12275b84efecSThomas Haller * Note: subsequent RA will update lifetime.
12285b84efecSThomas Haller **/
12295b84efecSThomas Haller static enum cleanup_prefix_rt_t
check_cleanup_prefix_route(struct inet6_ifaddr * ifp,unsigned long * expires)12305b84efecSThomas Haller check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
12315b84efecSThomas Haller {
12325b84efecSThomas Haller struct inet6_ifaddr *ifa;
12335b84efecSThomas Haller struct inet6_dev *idev = ifp->idev;
12345b84efecSThomas Haller unsigned long lifetime;
12355b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
12365b84efecSThomas Haller
12375b84efecSThomas Haller *expires = jiffies;
12385b84efecSThomas Haller
12395b84efecSThomas Haller list_for_each_entry(ifa, &idev->addr_list, if_list) {
12405b84efecSThomas Haller if (ifa == ifp)
12415b84efecSThomas Haller continue;
1242e75913c9SZhiqiang Liu if (ifa->prefix_len != ifp->prefix_len ||
1243e75913c9SZhiqiang Liu !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
12445b84efecSThomas Haller ifp->prefix_len))
12455b84efecSThomas Haller continue;
12465b84efecSThomas Haller if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
12475b84efecSThomas Haller return CLEANUP_PREFIX_RT_NOP;
12485b84efecSThomas Haller
12495b84efecSThomas Haller action = CLEANUP_PREFIX_RT_EXPIRE;
12505b84efecSThomas Haller
12515b84efecSThomas Haller spin_lock(&ifa->lock);
12525b84efecSThomas Haller
12535b84efecSThomas Haller lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
12545b84efecSThomas Haller /*
12555b84efecSThomas Haller * Note: Because this address is
12565b84efecSThomas Haller * not permanent, lifetime <
12575b84efecSThomas Haller * LONG_MAX / HZ here.
12585b84efecSThomas Haller */
12595b84efecSThomas Haller if (time_before(*expires, ifa->tstamp + lifetime * HZ))
12605b84efecSThomas Haller *expires = ifa->tstamp + lifetime * HZ;
12615b84efecSThomas Haller spin_unlock(&ifa->lock);
12625b84efecSThomas Haller }
12635b84efecSThomas Haller
12645b84efecSThomas Haller return action;
12655b84efecSThomas Haller }
12665b84efecSThomas Haller
12675b84efecSThomas Haller static void
cleanup_prefix_route(struct inet6_ifaddr * ifp,unsigned long expires,bool del_rt,bool del_peer)1268d0098e4cSHangbin Liu cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires,
1269d0098e4cSHangbin Liu bool del_rt, bool del_peer)
12705b84efecSThomas Haller {
127193c2fb25SDavid Ahern struct fib6_info *f6i;
12725b84efecSThomas Haller
1273d0098e4cSHangbin Liu f6i = addrconf_get_prefix_route(del_peer ? &ifp->peer_addr : &ifp->addr,
1274d0098e4cSHangbin Liu ifp->prefix_len,
12752b2450caSDavid Ahern ifp->idev->dev, 0, RTF_DEFAULT, true);
127693c2fb25SDavid Ahern if (f6i) {
12775b84efecSThomas Haller if (del_rt)
127811dd74b3SRoopa Prabhu ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
12795b84efecSThomas Haller else {
128093c2fb25SDavid Ahern if (!(f6i->fib6_flags & RTF_EXPIRES))
128193c2fb25SDavid Ahern fib6_set_expires(f6i, expires);
128293c2fb25SDavid Ahern fib6_info_release(f6i);
12835b84efecSThomas Haller }
12845b84efecSThomas Haller }
12855b84efecSThomas Haller }
12865b84efecSThomas Haller
12875b84efecSThomas Haller
12881da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
12891da177e4SLinus Torvalds
ipv6_del_addr(struct inet6_ifaddr * ifp)12901da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
12911da177e4SLinus Torvalds {
12925b84efecSThomas Haller enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1293e66d1172SEric Dumazet struct net *net = dev_net(ifp->idev->dev);
12945b84efecSThomas Haller unsigned long expires;
1295e66d1172SEric Dumazet int state;
12961da177e4SLinus Torvalds
1297c15b1ccaSHannes Frederic Sowa ASSERT_RTNL();
1298c15b1ccaSHannes Frederic Sowa
12998e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock);
13004c5ff6a6SHerbert Xu state = ifp->state;
1301e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD;
13028e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock);
13034c5ff6a6SHerbert Xu
13044c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD)
13054c5ff6a6SHerbert Xu goto out;
13061da177e4SLinus Torvalds
1307e66d1172SEric Dumazet spin_lock_bh(&net->ipv6.addrconf_hash_lock);
13085c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst);
1309e66d1172SEric Dumazet spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
13101da177e4SLinus Torvalds
13115b84efecSThomas Haller write_lock_bh(&ifp->idev->lock);
13125d9efa7eSDavid S. Miller
13131da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) {
1314372e6c8fSstephen hemminger list_del(&ifp->tmp_list);
13151da177e4SLinus Torvalds if (ifp->ifpub) {
13161da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub);
13171da177e4SLinus Torvalds ifp->ifpub = NULL;
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds __in6_ifa_put(ifp);
13201da177e4SLinus Torvalds }
13211da177e4SLinus Torvalds
13225b84efecSThomas Haller if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
13235b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &expires);
13245b84efecSThomas Haller
13258ef802aaSEric Dumazet list_del_rcu(&ifp->if_list);
13261da177e4SLinus Torvalds __in6_ifa_put(ifp);
1327502a2ffdSstephen hemminger
13285b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock);
13291da177e4SLinus Torvalds
1330c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp);
1331b2238566SAndrey Vagin
13321da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp);
13331da177e4SLinus Torvalds
1334f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
13351da177e4SLinus Torvalds
13365b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) {
13375b84efecSThomas Haller cleanup_prefix_route(ifp, expires,
1338d0098e4cSHangbin Liu action == CLEANUP_PREFIX_RT_DEL, false);
13391da177e4SLinus Torvalds }
13401da177e4SLinus Torvalds
1341c3968a85SDaniel Walter /* clean up prefsrc entries */
1342c3968a85SDaniel Walter rt6_remove_prefsrc(ifp);
13434c5ff6a6SHerbert Xu out:
13441da177e4SLinus Torvalds in6_ifa_put(ifp);
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds
ipv6_create_tempaddr(struct inet6_ifaddr * ifp,bool block)1347969c5464SFernando Gont static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, bool block)
13481da177e4SLinus Torvalds {
13491da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev;
1350e6464b8cSDavid Ahern unsigned long tmp_tstamp, age;
1351eac55bf9SBenoit Boissinot unsigned long regen_advance;
135276f793e3SLorenzo Colitti unsigned long now = jiffies;
13537aa8e63fSJiri Bohac s32 cnf_temp_preferred_lft;
1354969c5464SFernando Gont struct inet6_ifaddr *ift;
1355969c5464SFernando Gont struct ifa6_config cfg;
1356969c5464SFernando Gont long max_desync_factor;
1357969c5464SFernando Gont struct in6_addr addr;
1358969c5464SFernando Gont int ret = 0;
13591da177e4SLinus Torvalds
136053bd6749SJiri Pirko write_lock_bh(&idev->lock);
1361969c5464SFernando Gont
13621da177e4SLinus Torvalds retry:
13631da177e4SLinus Torvalds in6_dev_hold(idev);
13641da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) {
136553bd6749SJiri Pirko write_unlock_bh(&idev->lock);
1366f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__);
13671da177e4SLinus Torvalds in6_dev_put(idev);
13681da177e4SLinus Torvalds ret = -1;
13691da177e4SLinus Torvalds goto out;
13701da177e4SLinus Torvalds }
13711da177e4SLinus Torvalds spin_lock_bh(&ifp->lock);
13721da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
13731da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/
13741da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock);
137553bd6749SJiri Pirko write_unlock_bh(&idev->lock);
1376f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1377f3213831SJoe Perches __func__);
13781da177e4SLinus Torvalds in6_dev_put(idev);
13791da177e4SLinus Torvalds ret = -1;
13801da177e4SLinus Torvalds goto out;
13811da177e4SLinus Torvalds }
13821da177e4SLinus Torvalds in6_ifa_hold(ifp);
13831da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1384969c5464SFernando Gont ipv6_gen_rnd_iid(&addr);
1385969c5464SFernando Gont
138676f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ;
13871da177e4SLinus Torvalds
1388eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry *
1389eac55bf9SBenoit Boissinot idev->cnf.dad_transmits *
139019e16d22SHangbin Liu max(NEIGH_VAR(idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
139176506a98SJiri Bohac
139276506a98SJiri Bohac /* recalculate max_desync_factor each time and update
139376506a98SJiri Bohac * idev->desync_factor if it's larger
139476506a98SJiri Bohac */
13957aa8e63fSJiri Bohac cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
1396f31867d0SAlex Henrie max_desync_factor = min_t(long,
139776506a98SJiri Bohac idev->cnf.max_desync_factor,
13987aa8e63fSJiri Bohac cnf_temp_preferred_lft - regen_advance);
139976506a98SJiri Bohac
140076506a98SJiri Bohac if (unlikely(idev->desync_factor > max_desync_factor)) {
140176506a98SJiri Bohac if (max_desync_factor > 0) {
140276506a98SJiri Bohac get_random_bytes(&idev->desync_factor,
140376506a98SJiri Bohac sizeof(idev->desync_factor));
140476506a98SJiri Bohac idev->desync_factor %= max_desync_factor;
140576506a98SJiri Bohac } else {
140676506a98SJiri Bohac idev->desync_factor = 0;
140776506a98SJiri Bohac }
140876506a98SJiri Bohac }
140976506a98SJiri Bohac
14103f2d67b6SDavid Ahern memset(&cfg, 0, sizeof(cfg));
1411e6464b8cSDavid Ahern cfg.valid_lft = min_t(__u32, ifp->valid_lft,
141276506a98SJiri Bohac idev->cnf.temp_valid_lft + age);
1413e6464b8cSDavid Ahern cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
1414e6464b8cSDavid Ahern cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft);
1415e6464b8cSDavid Ahern
1416e6464b8cSDavid Ahern cfg.plen = ifp->prefix_len;
141776506a98SJiri Bohac tmp_tstamp = ifp->tstamp;
141876506a98SJiri Bohac spin_unlock_bh(&ifp->lock);
141976506a98SJiri Bohac
142053bd6749SJiri Pirko write_unlock_bh(&idev->lock);
142195c385b4SNeil Horman
1422eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred
1423eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1424eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero
1425eac55bf9SBenoit Boissinot * Preferred Lifetime.
1426ecab6701SHeiner Kallweit * Use age calculation as in addrconf_verify to avoid unnecessary
1427ecab6701SHeiner Kallweit * temporary addresses being generated.
1428eac55bf9SBenoit Boissinot */
1429ecab6701SHeiner Kallweit age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1430e6464b8cSDavid Ahern if (cfg.preferred_lft <= regen_advance + age) {
1431eac55bf9SBenoit Boissinot in6_ifa_put(ifp);
1432eac55bf9SBenoit Boissinot in6_dev_put(idev);
1433eac55bf9SBenoit Boissinot ret = -1;
1434eac55bf9SBenoit Boissinot goto out;
1435eac55bf9SBenoit Boissinot }
1436eac55bf9SBenoit Boissinot
1437e6464b8cSDavid Ahern cfg.ifa_flags = IFA_F_TEMPORARY;
143895c385b4SNeil Horman /* set in addrconf_prefix_rcv() */
143995c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC)
1440e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC;
144195c385b4SNeil Horman
1442e6464b8cSDavid Ahern cfg.pfx = &addr;
1443e6464b8cSDavid Ahern cfg.scope = ipv6_addr_scope(cfg.pfx);
1444e6464b8cSDavid Ahern
1445e6464b8cSDavid Ahern ift = ipv6_add_addr(idev, &cfg, block, NULL);
14464b08a8f1SHannes Frederic Sowa if (IS_ERR(ift)) {
14471da177e4SLinus Torvalds in6_ifa_put(ifp);
14481da177e4SLinus Torvalds in6_dev_put(idev);
1449f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__);
145053bd6749SJiri Pirko write_lock_bh(&idev->lock);
14511da177e4SLinus Torvalds goto retry;
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds
14541da177e4SLinus Torvalds spin_lock_bh(&ift->lock);
14551da177e4SLinus Torvalds ift->ifpub = ifp;
145676f793e3SLorenzo Colitti ift->cstamp = now;
14571da177e4SLinus Torvalds ift->tstamp = tmp_tstamp;
14581da177e4SLinus Torvalds spin_unlock_bh(&ift->lock);
14591da177e4SLinus Torvalds
1460cf22f9a2SDavid S. Miller addrconf_dad_start(ift);
14611da177e4SLinus Torvalds in6_ifa_put(ift);
14621da177e4SLinus Torvalds in6_dev_put(idev);
14631da177e4SLinus Torvalds out:
14641da177e4SLinus Torvalds return ret;
14651da177e4SLinus Torvalds }
14661da177e4SLinus Torvalds
14671da177e4SLinus Torvalds /*
1468072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484)
14691da177e4SLinus Torvalds */
1470a9b05723SYOSHIFUJI Hideaki enum {
1471a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0,
1472a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL,
1473a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE,
1474a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED,
1475a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1476a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA,
1477a9b05723SYOSHIFUJI Hideaki #endif
1478a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF,
1479a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL,
1480a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY,
1481a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID,
1482a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX,
14837fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
14847fd2561eSErik Kline IPV6_SADDR_RULE_NOT_OPTIMISTIC,
14857fd2561eSErik Kline #endif
1486a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX
1487072047e4SYOSHIFUJI Hideaki };
1488072047e4SYOSHIFUJI Hideaki
1489a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1490a9b05723SYOSHIFUJI Hideaki int rule;
1491a9b05723SYOSHIFUJI Hideaki int addr_type;
1492a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa;
1493a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1494a9b05723SYOSHIFUJI Hideaki int scopedist;
1495a9b05723SYOSHIFUJI Hideaki int matchlen;
1496a9b05723SYOSHIFUJI Hideaki };
1497a9b05723SYOSHIFUJI Hideaki
1498a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
14999acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr;
1500a9b05723SYOSHIFUJI Hideaki int ifindex;
1501a9b05723SYOSHIFUJI Hideaki int scope;
1502a9b05723SYOSHIFUJI Hideaki int label;
15037cbca67cSYOSHIFUJI Hideaki unsigned int prefs;
1504a9b05723SYOSHIFUJI Hideaki };
1505072047e4SYOSHIFUJI Hideaki
ipv6_saddr_preferred(int type)1506b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
15071da177e4SLinus Torvalds {
150845bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1509072047e4SYOSHIFUJI Hideaki return 1;
1510072047e4SYOSHIFUJI Hideaki return 0;
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds
ipv6_use_optimistic_addr(struct net * net,struct inet6_dev * idev)151335e015e1SMatteo Croce static bool ipv6_use_optimistic_addr(struct net *net,
151435e015e1SMatteo Croce struct inet6_dev *idev)
15157fd2561eSErik Kline {
15167fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
151735e015e1SMatteo Croce if (!idev)
151835e015e1SMatteo Croce return false;
151935e015e1SMatteo Croce if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
152035e015e1SMatteo Croce return false;
152135e015e1SMatteo Croce if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
152235e015e1SMatteo Croce return false;
152335e015e1SMatteo Croce
152435e015e1SMatteo Croce return true;
15257fd2561eSErik Kline #else
15267fd2561eSErik Kline return false;
15277fd2561eSErik Kline #endif
15287fd2561eSErik Kline }
15297fd2561eSErik Kline
ipv6_allow_optimistic_dad(struct net * net,struct inet6_dev * idev)1530f1c02cfbSSabrina Dubroca static bool ipv6_allow_optimistic_dad(struct net *net,
1531f1c02cfbSSabrina Dubroca struct inet6_dev *idev)
1532f1c02cfbSSabrina Dubroca {
1533f1c02cfbSSabrina Dubroca #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1534f1c02cfbSSabrina Dubroca if (!idev)
1535f1c02cfbSSabrina Dubroca return false;
1536f1c02cfbSSabrina Dubroca if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1537f1c02cfbSSabrina Dubroca return false;
1538f1c02cfbSSabrina Dubroca
1539f1c02cfbSSabrina Dubroca return true;
1540f1c02cfbSSabrina Dubroca #else
1541f1c02cfbSSabrina Dubroca return false;
1542f1c02cfbSSabrina Dubroca #endif
1543f1c02cfbSSabrina Dubroca }
1544f1c02cfbSSabrina Dubroca
ipv6_get_saddr_eval(struct net * net,struct ipv6_saddr_score * score,struct ipv6_saddr_dst * dst,int i)15453de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
15463de23255SBenjamin Thery struct ipv6_saddr_score *score,
1547a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst,
1548a9b05723SYOSHIFUJI Hideaki int i)
1549a9b05723SYOSHIFUJI Hideaki {
1550a9b05723SYOSHIFUJI Hideaki int ret;
1551a9b05723SYOSHIFUJI Hideaki
1552a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) {
1553a9b05723SYOSHIFUJI Hideaki switch (i) {
1554a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE:
1555a9b05723SYOSHIFUJI Hideaki ret = score->scopedist;
1556a9b05723SYOSHIFUJI Hideaki break;
1557a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX:
1558a9b05723SYOSHIFUJI Hideaki ret = score->matchlen;
1559a9b05723SYOSHIFUJI Hideaki break;
1560a9b05723SYOSHIFUJI Hideaki default:
1561a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits);
1562a9b05723SYOSHIFUJI Hideaki }
1563a9b05723SYOSHIFUJI Hideaki goto out;
1564a9b05723SYOSHIFUJI Hideaki }
1565a9b05723SYOSHIFUJI Hideaki
1566a9b05723SYOSHIFUJI Hideaki switch (i) {
1567a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT:
1568a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */
1569a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa;
1570a9b05723SYOSHIFUJI Hideaki break;
1571a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL:
1572a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */
1573a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1574a9b05723SYOSHIFUJI Hideaki break;
1575a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE:
1576a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope
1577a9b05723SYOSHIFUJI Hideaki *
1578a9b05723SYOSHIFUJI Hideaki * ret
1579a9b05723SYOSHIFUJI Hideaki * ^
1580a9b05723SYOSHIFUJI Hideaki * -1 | d 15
1581a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope
1582a9b05723SYOSHIFUJI Hideaki * |
1583a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination.
1584a9b05723SYOSHIFUJI Hideaki * B-d | \
1585a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if
1586db9c7c39Sstephen hemminger * B-15 | \ if scope is enough for destination.
1587a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15).
1588a9b05723SYOSHIFUJI Hideaki * d-C-1 | /
1589a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better
1590a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination.
1591a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15).
1592a9b05723SYOSHIFUJI Hideaki *
1593a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15).
1594a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1595a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29.
1596a9b05723SYOSHIFUJI Hideaki */
1597a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type);
1598a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope)
1599a9b05723SYOSHIFUJI Hideaki ret = -ret;
1600a9b05723SYOSHIFUJI Hideaki else
1601a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */
1602a9b05723SYOSHIFUJI Hideaki score->scopedist = ret;
1603a9b05723SYOSHIFUJI Hideaki break;
1604a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED:
16057fd2561eSErik Kline {
1606a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */
16077fd2561eSErik Kline u8 avoid = IFA_F_DEPRECATED;
16087fd2561eSErik Kline
160935e015e1SMatteo Croce if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
16107fd2561eSErik Kline avoid |= IFA_F_OPTIMISTIC;
1611a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) ||
16127fd2561eSErik Kline !(score->ifa->flags & avoid);
1613a9b05723SYOSHIFUJI Hideaki break;
16147fd2561eSErik Kline }
1615a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1616a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA:
16177cbca67cSYOSHIFUJI Hideaki {
1618a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */
16197cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
16207cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1621a9b05723SYOSHIFUJI Hideaki break;
16227cbca67cSYOSHIFUJI Hideaki }
1623a9b05723SYOSHIFUJI Hideaki #endif
1624a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF:
1625a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */
1626a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex ||
1627a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex);
1628a9b05723SYOSHIFUJI Hideaki break;
1629a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL:
1630a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */
16313de23255SBenjamin Thery ret = ipv6_addr_label(net,
16323de23255SBenjamin Thery &score->ifa->addr, score->addr_type,
1633a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label;
1634a9b05723SYOSHIFUJI Hideaki break;
1635a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY:
16367cbca67cSYOSHIFUJI Hideaki {
1637a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address
163825985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2
1639a9b05723SYOSHIFUJI Hideaki */
16407cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
16417cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
16427cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2;
16437cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1644a9b05723SYOSHIFUJI Hideaki break;
16457cbca67cSYOSHIFUJI Hideaki }
1646a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID:
1647a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or
1648a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID
1649a9b05723SYOSHIFUJI Hideaki */
1650a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1651a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr));
1652a9b05723SYOSHIFUJI Hideaki break;
1653a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX:
1654a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */
165591b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
165691b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len)
165791b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len;
165891b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret;
1659a9b05723SYOSHIFUJI Hideaki break;
16607fd2561eSErik Kline #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
16617fd2561eSErik Kline case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
16627fd2561eSErik Kline /* Optimistic addresses still have lower precedence than other
16637fd2561eSErik Kline * preferred addresses.
16647fd2561eSErik Kline */
16657fd2561eSErik Kline ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
16667fd2561eSErik Kline break;
16677fd2561eSErik Kline #endif
1668a9b05723SYOSHIFUJI Hideaki default:
1669a9b05723SYOSHIFUJI Hideaki ret = 0;
1670a9b05723SYOSHIFUJI Hideaki }
1671a9b05723SYOSHIFUJI Hideaki
1672a9b05723SYOSHIFUJI Hideaki if (ret)
1673a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits);
1674a9b05723SYOSHIFUJI Hideaki score->rule = i;
1675a9b05723SYOSHIFUJI Hideaki out:
1676a9b05723SYOSHIFUJI Hideaki return ret;
1677a9b05723SYOSHIFUJI Hideaki }
1678a9b05723SYOSHIFUJI Hideaki
__ipv6_dev_get_saddr(struct net * net,struct ipv6_saddr_dst * dst,struct inet6_dev * idev,struct ipv6_saddr_score * scores,int hiscore_idx)1679c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 static int __ipv6_dev_get_saddr(struct net *net,
16809131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst *dst,
16819131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev,
1682c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *scores,
1683c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx)
16841da177e4SLinus Torvalds {
1685c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1686072047e4SYOSHIFUJI Hideaki
1687f59c031eSEric Dumazet list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
1688a9b05723SYOSHIFUJI Hideaki int i;
1689072047e4SYOSHIFUJI Hideaki
1690a9b05723SYOSHIFUJI Hideaki /*
16916b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4)
16926b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered
16936b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional
169495c385b4SNeil Horman * sense, unless it is also flagged as optimistic.
16956b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4)
1696072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast
1697072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST
1698072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set.
1699072047e4SYOSHIFUJI Hideaki */
1700a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1701a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
17026b3ae80aSYOSHIFUJI Hideaki continue;
1703a9b05723SYOSHIFUJI Hideaki
1704a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1705a9b05723SYOSHIFUJI Hideaki
1706a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1707a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) {
1708ba7a46f1SJoe Perches net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
17099131f3deSYOSHIFUJI Hideaki/吉藤英明 idev->dev->name);
17101da177e4SLinus Torvalds continue;
1711072047e4SYOSHIFUJI Hideaki }
1712072047e4SYOSHIFUJI Hideaki
1713a9b05723SYOSHIFUJI Hideaki score->rule = -1;
1714a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1715072047e4SYOSHIFUJI Hideaki
1716a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1717a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore;
1718072047e4SYOSHIFUJI Hideaki
17199131f3deSYOSHIFUJI Hideaki/吉藤英明 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
17209131f3deSYOSHIFUJI Hideaki/吉藤英明 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1721072047e4SYOSHIFUJI Hideaki
1722a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) {
1723a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE &&
1724a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) {
1725a9b05723SYOSHIFUJI Hideaki /*
1726a9b05723SYOSHIFUJI Hideaki * special case:
1727a9b05723SYOSHIFUJI Hideaki * each remaining entry
1728a9b05723SYOSHIFUJI Hideaki * has too small (not enough)
1729a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries
1730a9b05723SYOSHIFUJI Hideaki * are sorted by their scope
1731a9b05723SYOSHIFUJI Hideaki * values.
1732072047e4SYOSHIFUJI Hideaki */
17339131f3deSYOSHIFUJI Hideaki/吉藤英明 goto out;
1734072047e4SYOSHIFUJI Hideaki }
1735a9b05723SYOSHIFUJI Hideaki break;
1736a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) {
1737a0bffffcSIlpo Järvinen swap(hiscore, score);
1738c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore_idx = 1 - hiscore_idx;
1739a9b05723SYOSHIFUJI Hideaki
1740a9b05723SYOSHIFUJI Hideaki /* restore our iterator */
1741a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa;
1742a9b05723SYOSHIFUJI Hideaki
1743a9b05723SYOSHIFUJI Hideaki break;
17441da177e4SLinus Torvalds }
1745a9b05723SYOSHIFUJI Hideaki }
1746a9b05723SYOSHIFUJI Hideaki }
17479131f3deSYOSHIFUJI Hideaki/吉藤英明 out:
1748c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 return hiscore_idx;
17491da177e4SLinus Torvalds }
17509131f3deSYOSHIFUJI Hideaki/吉藤英明
ipv6_get_saddr_master(struct net * net,const struct net_device * dst_dev,const struct net_device * master,struct ipv6_saddr_dst * dst,struct ipv6_saddr_score * scores,int hiscore_idx)1751afbac601SDavid Ahern static int ipv6_get_saddr_master(struct net *net,
1752afbac601SDavid Ahern const struct net_device *dst_dev,
1753afbac601SDavid Ahern const struct net_device *master,
1754afbac601SDavid Ahern struct ipv6_saddr_dst *dst,
1755afbac601SDavid Ahern struct ipv6_saddr_score *scores,
1756afbac601SDavid Ahern int hiscore_idx)
1757afbac601SDavid Ahern {
1758afbac601SDavid Ahern struct inet6_dev *idev;
1759afbac601SDavid Ahern
1760afbac601SDavid Ahern idev = __in6_dev_get(dst_dev);
1761afbac601SDavid Ahern if (idev)
1762afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1763afbac601SDavid Ahern scores, hiscore_idx);
1764afbac601SDavid Ahern
1765afbac601SDavid Ahern idev = __in6_dev_get(master);
1766afbac601SDavid Ahern if (idev)
1767afbac601SDavid Ahern hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1768afbac601SDavid Ahern scores, hiscore_idx);
1769afbac601SDavid Ahern
1770afbac601SDavid Ahern return hiscore_idx;
1771afbac601SDavid Ahern }
1772afbac601SDavid Ahern
ipv6_dev_get_saddr(struct net * net,const struct net_device * dst_dev,const struct in6_addr * daddr,unsigned int prefs,struct in6_addr * saddr)17739131f3deSYOSHIFUJI Hideaki/吉藤英明 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
17749131f3deSYOSHIFUJI Hideaki/吉藤英明 const struct in6_addr *daddr, unsigned int prefs,
17759131f3deSYOSHIFUJI Hideaki/吉藤英明 struct in6_addr *saddr)
17769131f3deSYOSHIFUJI Hideaki/吉藤英明 {
1777c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_score scores[2], *hiscore;
17789131f3deSYOSHIFUJI Hideaki/吉藤英明 struct ipv6_saddr_dst dst;
17799131f3deSYOSHIFUJI Hideaki/吉藤英明 struct inet6_dev *idev;
17809131f3deSYOSHIFUJI Hideaki/吉藤英明 struct net_device *dev;
17819131f3deSYOSHIFUJI Hideaki/吉藤英明 int dst_type;
17829131f3deSYOSHIFUJI Hideaki/吉藤英明 bool use_oif_addr = false;
1783c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 int hiscore_idx = 0;
1784cc429c8fSEric Dumazet int ret = 0;
17859131f3deSYOSHIFUJI Hideaki/吉藤英明
17869131f3deSYOSHIFUJI Hideaki/吉藤英明 dst_type = __ipv6_addr_type(daddr);
17879131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.addr = daddr;
17889131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
17899131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.scope = __ipv6_addr_src_scope(dst_type);
17909131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
17919131f3deSYOSHIFUJI Hideaki/吉藤英明 dst.prefs = prefs;
17929131f3deSYOSHIFUJI Hideaki/吉藤英明
1793c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].rule = -1;
1794c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 scores[hiscore_idx].ifa = NULL;
17959131f3deSYOSHIFUJI Hideaki/吉藤英明
17969131f3deSYOSHIFUJI Hideaki/吉藤英明 rcu_read_lock();
17979131f3deSYOSHIFUJI Hideaki/吉藤英明
17989131f3deSYOSHIFUJI Hideaki/吉藤英明 /* Candidate Source Address (section 4)
17999131f3deSYOSHIFUJI Hideaki/吉藤英明 * - multicast and link-local destination address,
18009131f3deSYOSHIFUJI Hideaki/吉藤英明 * the set of candidate source address MUST only
18019131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces
18029131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same link as the outgoing
18039131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface.
18049131f3deSYOSHIFUJI Hideaki/吉藤英明 * (- For site-local destination addresses, the
18059131f3deSYOSHIFUJI Hideaki/吉藤英明 * set of candidate source addresses MUST only
18069131f3deSYOSHIFUJI Hideaki/吉藤英明 * include addresses assigned to interfaces
18079131f3deSYOSHIFUJI Hideaki/吉藤英明 * belonging to the same site as the outgoing
18089131f3deSYOSHIFUJI Hideaki/吉藤英明 * interface.)
18093985e8a3SErik Kline * - "It is RECOMMENDED that the candidate source addresses
18103985e8a3SErik Kline * be the set of unicast addresses assigned to the
18113985e8a3SErik Kline * interface that will be used to send to the destination
18123985e8a3SErik Kline * (the 'outgoing' interface)." (RFC 6724)
18139131f3deSYOSHIFUJI Hideaki/吉藤英明 */
18149131f3deSYOSHIFUJI Hideaki/吉藤英明 if (dst_dev) {
18159131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dst_dev);
18163985e8a3SErik Kline if ((dst_type & IPV6_ADDR_MULTICAST) ||
18173985e8a3SErik Kline dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
18183985e8a3SErik Kline (idev && idev->cnf.use_oif_addrs_only)) {
18199131f3deSYOSHIFUJI Hideaki/吉藤英明 use_oif_addr = true;
18209131f3deSYOSHIFUJI Hideaki/吉藤英明 }
18219131f3deSYOSHIFUJI Hideaki/吉藤英明 }
18229131f3deSYOSHIFUJI Hideaki/吉藤英明
18239131f3deSYOSHIFUJI Hideaki/吉藤英明 if (use_oif_addr) {
1824c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 if (idev)
1825c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
18269131f3deSYOSHIFUJI Hideaki/吉藤英明 } else {
1827afbac601SDavid Ahern const struct net_device *master;
1828afbac601SDavid Ahern int master_idx = 0;
1829afbac601SDavid Ahern
1830afbac601SDavid Ahern /* if dst_dev exists and is enslaved to an L3 device, then
1831afbac601SDavid Ahern * prefer addresses from dst_dev and then the master over
1832afbac601SDavid Ahern * any other enslaved devices in the L3 domain.
1833afbac601SDavid Ahern */
1834afbac601SDavid Ahern master = l3mdev_master_dev_rcu(dst_dev);
1835afbac601SDavid Ahern if (master) {
1836afbac601SDavid Ahern master_idx = master->ifindex;
1837afbac601SDavid Ahern
1838afbac601SDavid Ahern hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1839afbac601SDavid Ahern master, &dst,
1840afbac601SDavid Ahern scores, hiscore_idx);
1841afbac601SDavid Ahern
184246cb65e5SNicolas Dichtel if (scores[hiscore_idx].ifa &&
184346cb65e5SNicolas Dichtel scores[hiscore_idx].scopedist >= 0)
1844afbac601SDavid Ahern goto out;
1845afbac601SDavid Ahern }
1846afbac601SDavid Ahern
18479131f3deSYOSHIFUJI Hideaki/吉藤英明 for_each_netdev_rcu(net, dev) {
1848afbac601SDavid Ahern /* only consider addresses on devices in the
1849afbac601SDavid Ahern * same L3 domain
1850afbac601SDavid Ahern */
1851afbac601SDavid Ahern if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1852afbac601SDavid Ahern continue;
18539131f3deSYOSHIFUJI Hideaki/吉藤英明 idev = __in6_dev_get(dev);
18549131f3deSYOSHIFUJI Hideaki/吉藤英明 if (!idev)
18559131f3deSYOSHIFUJI Hideaki/吉藤英明 continue;
1856c15df306SYOSHIFUJI Hideaki hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
18579131f3deSYOSHIFUJI Hideaki/吉藤英明 }
18589131f3deSYOSHIFUJI Hideaki/吉藤英明 }
1859afbac601SDavid Ahern
1860afbac601SDavid Ahern out:
1861c0b8da1eSYOSHIFUJI Hideaki/吉藤英明 hiscore = &scores[hiscore_idx];
1862a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa)
1863cc429c8fSEric Dumazet ret = -EADDRNOTAVAIL;
1864cc429c8fSEric Dumazet else
18654e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr;
1866cc429c8fSEric Dumazet
1867cc429c8fSEric Dumazet rcu_read_unlock();
1868cc429c8fSEric Dumazet return ret;
18691da177e4SLinus Torvalds }
18705e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
18711da177e4SLinus Torvalds
__ipv6_get_lladdr(struct inet6_dev * idev,struct in6_addr * addr,u32 banned_flags)187226394fc1SIgnat Korchagin static int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1873479840ffSJiri Pirko u32 banned_flags)
1874b7b1bfceSHannes Frederic Sowa {
1875b7b1bfceSHannes Frederic Sowa struct inet6_ifaddr *ifp;
1876b7b1bfceSHannes Frederic Sowa int err = -EADDRNOTAVAIL;
1877b7b1bfceSHannes Frederic Sowa
1878602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1879602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK)
1880602582caSHannes Frederic Sowa break;
1881b7b1bfceSHannes Frederic Sowa if (ifp->scope == IFA_LINK &&
1882b7b1bfceSHannes Frederic Sowa !(ifp->flags & banned_flags)) {
1883b7b1bfceSHannes Frederic Sowa *addr = ifp->addr;
1884b7b1bfceSHannes Frederic Sowa err = 0;
1885b7b1bfceSHannes Frederic Sowa break;
1886b7b1bfceSHannes Frederic Sowa }
1887b7b1bfceSHannes Frederic Sowa }
1888b7b1bfceSHannes Frederic Sowa return err;
1889b7b1bfceSHannes Frederic Sowa }
1890b7b1bfceSHannes Frederic Sowa
ipv6_get_lladdr(struct net_device * dev,struct in6_addr * addr,u32 banned_flags)189195c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1892479840ffSJiri Pirko u32 banned_flags)
18931da177e4SLinus Torvalds {
18941da177e4SLinus Torvalds struct inet6_dev *idev;
18951da177e4SLinus Torvalds int err = -EADDRNOTAVAIL;
18961da177e4SLinus Torvalds
18978814c4b5SYOSHIFUJI Hideaki rcu_read_lock();
1898e21e8467SStephen Hemminger idev = __in6_dev_get(dev);
1899e21e8467SStephen Hemminger if (idev) {
19001da177e4SLinus Torvalds read_lock_bh(&idev->lock);
1901b7b1bfceSHannes Frederic Sowa err = __ipv6_get_lladdr(idev, addr, banned_flags);
19021da177e4SLinus Torvalds read_unlock_bh(&idev->lock);
19031da177e4SLinus Torvalds }
19048814c4b5SYOSHIFUJI Hideaki rcu_read_unlock();
19051da177e4SLinus Torvalds return err;
19061da177e4SLinus Torvalds }
19071da177e4SLinus Torvalds
ipv6_count_addresses(const struct inet6_dev * idev)1908d9bf82c2SEric Dumazet static int ipv6_count_addresses(const struct inet6_dev *idev)
19091da177e4SLinus Torvalds {
1910d9bf82c2SEric Dumazet const struct inet6_ifaddr *ifp;
19111da177e4SLinus Torvalds int cnt = 0;
19121da177e4SLinus Torvalds
1913d9bf82c2SEric Dumazet rcu_read_lock();
1914d9bf82c2SEric Dumazet list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
19151da177e4SLinus Torvalds cnt++;
1916d9bf82c2SEric Dumazet rcu_read_unlock();
19171da177e4SLinus Torvalds return cnt;
19181da177e4SLinus Torvalds }
19191da177e4SLinus Torvalds
ipv6_chk_addr(struct net * net,const struct in6_addr * addr,const struct net_device * dev,int strict)1920b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
19212a7851bfSFlorian Westphal const struct net_device *dev, int strict)
19221da177e4SLinus Torvalds {
1923232378e8SDavid Ahern return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1924232378e8SDavid Ahern strict, IFA_F_TENTATIVE);
1925c58da4c6SErik Kline }
1926c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr);
1927c58da4c6SErik Kline
19281893ff20SDavid Ahern /* device argument is used to find the L3 domain of interest. If
19291893ff20SDavid Ahern * skip_dev_check is set, then the ifp device is not checked against
19301893ff20SDavid Ahern * the passed in dev argument. So the 2 cases for addresses checks are:
19311893ff20SDavid Ahern * 1. does the address exist in the L3 domain that dev is part of
19321893ff20SDavid Ahern * (skip_dev_check = true), or
19331893ff20SDavid Ahern *
19341893ff20SDavid Ahern * 2. does the address exist on the specific device
19351893ff20SDavid Ahern * (skip_dev_check = false)
19361893ff20SDavid Ahern */
19374ef1a7cbSXin Long static struct net_device *
__ipv6_chk_addr_and_flags(struct net * net,const struct in6_addr * addr,const struct net_device * dev,bool skip_dev_check,int strict,u32 banned_flags)19384ef1a7cbSXin Long __ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1939232378e8SDavid Ahern const struct net_device *dev, bool skip_dev_check,
1940232378e8SDavid Ahern int strict, u32 banned_flags)
1941c58da4c6SErik Kline {
19423f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr);
19434ef1a7cbSXin Long struct net_device *l3mdev, *ndev;
1944eedf042aSStephen Hemminger struct inet6_ifaddr *ifp;
1945c58da4c6SErik Kline u32 ifp_flags;
19461da177e4SLinus Torvalds
1947480318a0SEric Dumazet rcu_read_lock();
1948232378e8SDavid Ahern
19491893ff20SDavid Ahern l3mdev = l3mdev_master_dev_rcu(dev);
1950232378e8SDavid Ahern if (skip_dev_check)
1951232378e8SDavid Ahern dev = NULL;
1952232378e8SDavid Ahern
1953e66d1172SEric Dumazet hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
19544ef1a7cbSXin Long ndev = ifp->idev->dev;
19551893ff20SDavid Ahern
19564ef1a7cbSXin Long if (l3mdev_master_dev_rcu(ndev) != l3mdev)
19571893ff20SDavid Ahern continue;
19581893ff20SDavid Ahern
1959c58da4c6SErik Kline /* Decouple optimistic from tentative for evaluation here.
1960c58da4c6SErik Kline * Ban optimistic addresses explicitly, when required.
1961c58da4c6SErik Kline */
1962c58da4c6SErik Kline ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1963c58da4c6SErik Kline ? (ifp->flags&~IFA_F_TENTATIVE)
1964c58da4c6SErik Kline : ifp->flags;
19651da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) &&
1966c58da4c6SErik Kline !(ifp_flags&banned_flags) &&
19674ef1a7cbSXin Long (!dev || ndev == dev ||
1968eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1969480318a0SEric Dumazet rcu_read_unlock();
19704ef1a7cbSXin Long return ndev;
1971eedf042aSStephen Hemminger }
1972eedf042aSStephen Hemminger }
19735c578aedSstephen hemminger
1974480318a0SEric Dumazet rcu_read_unlock();
19754ef1a7cbSXin Long return NULL;
19764ef1a7cbSXin Long }
19774ef1a7cbSXin Long
ipv6_chk_addr_and_flags(struct net * net,const struct in6_addr * addr,const struct net_device * dev,bool skip_dev_check,int strict,u32 banned_flags)19784ef1a7cbSXin Long int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
19794ef1a7cbSXin Long const struct net_device *dev, bool skip_dev_check,
19804ef1a7cbSXin Long int strict, u32 banned_flags)
19814ef1a7cbSXin Long {
19824ef1a7cbSXin Long return __ipv6_chk_addr_and_flags(net, addr, dev, skip_dev_check,
19834ef1a7cbSXin Long strict, banned_flags) ? 1 : 0;
19841da177e4SLinus Torvalds }
1985c58da4c6SErik Kline EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
19867159039aSYOSHIFUJI Hideaki
19871da177e4SLinus Torvalds
19887df37ff3SCatalin\(ux\) M. BOIE /* Compares an address/prefix_len with addresses on device @dev.
19897df37ff3SCatalin\(ux\) M. BOIE * If one is found it returns true.
19907df37ff3SCatalin\(ux\) M. BOIE */
ipv6_chk_custom_prefix(const struct in6_addr * addr,const unsigned int prefix_len,struct net_device * dev)19917df37ff3SCatalin\(ux\) M. BOIE bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
19927df37ff3SCatalin\(ux\) M. BOIE const unsigned int prefix_len, struct net_device *dev)
19937df37ff3SCatalin\(ux\) M. BOIE {
199447e26941SEric Dumazet const struct inet6_ifaddr *ifa;
199547e26941SEric Dumazet const struct inet6_dev *idev;
19967df37ff3SCatalin\(ux\) M. BOIE bool ret = false;
19977df37ff3SCatalin\(ux\) M. BOIE
19987df37ff3SCatalin\(ux\) M. BOIE rcu_read_lock();
19997df37ff3SCatalin\(ux\) M. BOIE idev = __in6_dev_get(dev);
20007df37ff3SCatalin\(ux\) M. BOIE if (idev) {
200147e26941SEric Dumazet list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
20027df37ff3SCatalin\(ux\) M. BOIE ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
20037df37ff3SCatalin\(ux\) M. BOIE if (ret)
20047df37ff3SCatalin\(ux\) M. BOIE break;
20057df37ff3SCatalin\(ux\) M. BOIE }
20067df37ff3SCatalin\(ux\) M. BOIE }
20077df37ff3SCatalin\(ux\) M. BOIE rcu_read_unlock();
20087df37ff3SCatalin\(ux\) M. BOIE
20097df37ff3SCatalin\(ux\) M. BOIE return ret;
20107df37ff3SCatalin\(ux\) M. BOIE }
20117df37ff3SCatalin\(ux\) M. BOIE EXPORT_SYMBOL(ipv6_chk_custom_prefix);
20127df37ff3SCatalin\(ux\) M. BOIE
ipv6_chk_prefix(const struct in6_addr * addr,struct net_device * dev)2013b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
201452eeeb84SYOSHIFUJI Hideaki {
201524ba333bSEric Dumazet const struct inet6_ifaddr *ifa;
201624ba333bSEric Dumazet const struct inet6_dev *idev;
201752eeeb84SYOSHIFUJI Hideaki int onlink;
201852eeeb84SYOSHIFUJI Hideaki
201952eeeb84SYOSHIFUJI Hideaki onlink = 0;
202052eeeb84SYOSHIFUJI Hideaki rcu_read_lock();
202152eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev);
202252eeeb84SYOSHIFUJI Hideaki if (idev) {
202324ba333bSEric Dumazet list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
202452eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr,
202552eeeb84SYOSHIFUJI Hideaki ifa->prefix_len);
202652eeeb84SYOSHIFUJI Hideaki if (onlink)
202752eeeb84SYOSHIFUJI Hideaki break;
202852eeeb84SYOSHIFUJI Hideaki }
202952eeeb84SYOSHIFUJI Hideaki }
203052eeeb84SYOSHIFUJI Hideaki rcu_read_unlock();
203152eeeb84SYOSHIFUJI Hideaki return onlink;
203252eeeb84SYOSHIFUJI Hideaki }
203352eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
203452eeeb84SYOSHIFUJI Hideaki
203581f6cb31SXin Long /**
203681f6cb31SXin Long * ipv6_dev_find - find the first device with a given source address.
203781f6cb31SXin Long * @net: the net namespace
203881f6cb31SXin Long * @addr: the source address
20392c4de211SXin Long * @dev: used to find the L3 domain of interest
204081f6cb31SXin Long *
204181f6cb31SXin Long * The caller should be protected by RCU, or RTNL.
204281f6cb31SXin Long */
ipv6_dev_find(struct net * net,const struct in6_addr * addr,struct net_device * dev)20434ef1a7cbSXin Long struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr,
20444ef1a7cbSXin Long struct net_device *dev)
204581f6cb31SXin Long {
20464ef1a7cbSXin Long return __ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1,
20474ef1a7cbSXin Long IFA_F_TENTATIVE);
204881f6cb31SXin Long }
204981f6cb31SXin Long EXPORT_SYMBOL(ipv6_dev_find);
205081f6cb31SXin Long
ipv6_get_ifaddr(struct net * net,const struct in6_addr * addr,struct net_device * dev,int strict)20519acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
20521cab3da6SDaniel Lezcano struct net_device *dev, int strict)
20531da177e4SLinus Torvalds {
20543f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr);
2055b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL;
20561da177e4SLinus Torvalds
205724f226daSEric Dumazet rcu_read_lock();
2058e66d1172SEric Dumazet hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
20591da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) {
206063159f29SIan Morris if (!dev || ifp->idev->dev == dev ||
20611da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
206201b11a05SJiri Benc if (in6_ifa_hold_safe(ifp)) {
2063b79d1d54SDavid S. Miller result = ifp;
20641da177e4SLinus Torvalds break;
20651da177e4SLinus Torvalds }
20661da177e4SLinus Torvalds }
20671da177e4SLinus Torvalds }
206801b11a05SJiri Benc }
206924f226daSEric Dumazet rcu_read_unlock();
20701da177e4SLinus Torvalds
2071b79d1d54SDavid S. Miller return result;
20721da177e4SLinus Torvalds }
20731da177e4SLinus Torvalds
20741da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
20751da177e4SLinus Torvalds
addrconf_dad_stop(struct inet6_ifaddr * ifp,int dad_failed)2076cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
20771da177e4SLinus Torvalds {
20783d171f39SLubomir Rintel if (dad_failed)
20793d171f39SLubomir Rintel ifp->flags |= IFA_F_DADFAILED;
20803d171f39SLubomir Rintel
2081ec8add2aSSabrina Dubroca if (ifp->flags&IFA_F_TEMPORARY) {
20821da177e4SLinus Torvalds struct inet6_ifaddr *ifpub;
20831da177e4SLinus Torvalds spin_lock_bh(&ifp->lock);
20841da177e4SLinus Torvalds ifpub = ifp->ifpub;
20851da177e4SLinus Torvalds if (ifpub) {
20861da177e4SLinus Torvalds in6_ifa_hold(ifpub);
20871da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock);
2088969c5464SFernando Gont ipv6_create_tempaddr(ifpub, true);
20891da177e4SLinus Torvalds in6_ifa_put(ifpub);
20901da177e4SLinus Torvalds } else {
20911da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock);
20921da177e4SLinus Torvalds }
20931da177e4SLinus Torvalds ipv6_del_addr(ifp);
2094ec8add2aSSabrina Dubroca } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
2095ec8add2aSSabrina Dubroca spin_lock_bh(&ifp->lock);
2096ec8add2aSSabrina Dubroca addrconf_del_dad_work(ifp);
2097ec8add2aSSabrina Dubroca ifp->flags |= IFA_F_TENTATIVE;
2098f1c02cfbSSabrina Dubroca if (dad_failed)
2099f1c02cfbSSabrina Dubroca ifp->flags &= ~IFA_F_OPTIMISTIC;
2100ec8add2aSSabrina Dubroca spin_unlock_bh(&ifp->lock);
2101ec8add2aSSabrina Dubroca if (dad_failed)
2102ec8add2aSSabrina Dubroca ipv6_ifa_notify(0, ifp);
2103ec8add2aSSabrina Dubroca in6_ifa_put(ifp);
2104c15b1ccaSHannes Frederic Sowa } else {
21051da177e4SLinus Torvalds ipv6_del_addr(ifp);
21061da177e4SLinus Torvalds }
2107c15b1ccaSHannes Frederic Sowa }
21081da177e4SLinus Torvalds
addrconf_dad_end(struct inet6_ifaddr * ifp)2109f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2110f2344a13SHerbert Xu {
2111f2344a13SHerbert Xu int err = -ENOENT;
2112f2344a13SHerbert Xu
21138e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock);
2114f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) {
2115f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD;
2116f2344a13SHerbert Xu err = 0;
2117f2344a13SHerbert Xu }
21188e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock);
2119f2344a13SHerbert Xu
2120f2344a13SHerbert Xu return err;
2121f2344a13SHerbert Xu }
2122f2344a13SHerbert Xu
addrconf_dad_failure(struct sk_buff * skb,struct inet6_ifaddr * ifp)2123da13c59bSVishwanath Pai void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
21243c21edbdSYOSHIFUJI Hideaki {
21251b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev;
2126e66d1172SEric Dumazet struct net *net = dev_net(idev->dev);
21279bdd8d40SBrian Haley
2128853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) {
2129853dc2e0SUrsula Braun in6_ifa_put(ifp);
2130f2344a13SHerbert Xu return;
2131853dc2e0SUrsula Braun }
2132f2344a13SHerbert Xu
2133da13c59bSVishwanath Pai net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2134da13c59bSVishwanath Pai ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
21359bdd8d40SBrian Haley
21365f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock);
21371b34be74SYOSHIFUJI Hideaki
21385f40ef77SHannes Frederic Sowa if (ifp->flags & IFA_F_STABLE_PRIVACY) {
21395f40ef77SHannes Frederic Sowa struct in6_addr new_addr;
21405f40ef77SHannes Frederic Sowa struct inet6_ifaddr *ifp2;
21415f40ef77SHannes Frederic Sowa int retries = ifp->stable_privacy_retry + 1;
2142e6464b8cSDavid Ahern struct ifa6_config cfg = {
2143e6464b8cSDavid Ahern .pfx = &new_addr,
2144e6464b8cSDavid Ahern .plen = ifp->prefix_len,
2145e6464b8cSDavid Ahern .ifa_flags = ifp->flags,
2146e6464b8cSDavid Ahern .valid_lft = ifp->valid_lft,
2147e6464b8cSDavid Ahern .preferred_lft = ifp->prefered_lft,
2148e6464b8cSDavid Ahern .scope = ifp->scope,
2149e6464b8cSDavid Ahern };
21505f40ef77SHannes Frederic Sowa
21511855b7c3SHannes Frederic Sowa if (retries > net->ipv6.sysctl.idgen_retries) {
21525f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
21535f40ef77SHannes Frederic Sowa ifp->idev->dev->name);
21545f40ef77SHannes Frederic Sowa goto errdad;
21555f40ef77SHannes Frederic Sowa }
21565f40ef77SHannes Frederic Sowa
21575f40ef77SHannes Frederic Sowa new_addr = ifp->addr;
21585f40ef77SHannes Frederic Sowa if (ipv6_generate_stable_address(&new_addr, retries,
21595f40ef77SHannes Frederic Sowa idev))
21605f40ef77SHannes Frederic Sowa goto errdad;
21615f40ef77SHannes Frederic Sowa
21625f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp->lock);
21635f40ef77SHannes Frederic Sowa
21645f40ef77SHannes Frederic Sowa if (idev->cnf.max_addresses &&
21655f40ef77SHannes Frederic Sowa ipv6_count_addresses(idev) >=
21665f40ef77SHannes Frederic Sowa idev->cnf.max_addresses)
21675f40ef77SHannes Frederic Sowa goto lock_errdad;
21685f40ef77SHannes Frederic Sowa
21695f40ef77SHannes Frederic Sowa net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
21705f40ef77SHannes Frederic Sowa ifp->idev->dev->name);
21715f40ef77SHannes Frederic Sowa
2172e6464b8cSDavid Ahern ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
21735f40ef77SHannes Frederic Sowa if (IS_ERR(ifp2))
21745f40ef77SHannes Frederic Sowa goto lock_errdad;
21755f40ef77SHannes Frederic Sowa
21765f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp2->lock);
21775f40ef77SHannes Frederic Sowa ifp2->stable_privacy_retry = retries;
21785f40ef77SHannes Frederic Sowa ifp2->state = INET6_IFADDR_STATE_PREDAD;
21795f40ef77SHannes Frederic Sowa spin_unlock_bh(&ifp2->lock);
21805f40ef77SHannes Frederic Sowa
21811855b7c3SHannes Frederic Sowa addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
21825f40ef77SHannes Frederic Sowa in6_ifa_put(ifp2);
21835f40ef77SHannes Frederic Sowa lock_errdad:
21845f40ef77SHannes Frederic Sowa spin_lock_bh(&ifp->lock);
21851b34be74SYOSHIFUJI Hideaki }
21861b34be74SYOSHIFUJI Hideaki
21875f40ef77SHannes Frederic Sowa errdad:
2188c15b1ccaSHannes Frederic Sowa /* transition from _POSTDAD to _ERRDAD */
2189c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_ERRDAD;
21908e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock);
2191c15b1ccaSHannes Frederic Sowa
2192c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0);
2193751eb6b6SWei Yongjun in6_ifa_put(ifp);
21943c21edbdSYOSHIFUJI Hideaki }
21951da177e4SLinus Torvalds
2196a9ed4a29SSabrina Dubroca /* Join to solicited addr multicast group.
2197a9ed4a29SSabrina Dubroca * caller must hold RTNL */
addrconf_join_solict(struct net_device * dev,const struct in6_addr * addr)2198b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
21991da177e4SLinus Torvalds {
22001da177e4SLinus Torvalds struct in6_addr maddr;
22011da177e4SLinus Torvalds
22021da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
22031da177e4SLinus Torvalds return;
22041da177e4SLinus Torvalds
22051da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr);
22061da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr);
22071da177e4SLinus Torvalds }
22081da177e4SLinus Torvalds
2209a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
addrconf_leave_solict(struct inet6_dev * idev,const struct in6_addr * addr)2210b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
22111da177e4SLinus Torvalds {
22121da177e4SLinus Torvalds struct in6_addr maddr;
22131da177e4SLinus Torvalds
22141da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
22151da177e4SLinus Torvalds return;
22161da177e4SLinus Torvalds
22171da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr);
22181da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr);
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds
2221a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
addrconf_join_anycast(struct inet6_ifaddr * ifp)222220380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
22231da177e4SLinus Torvalds {
22241da177e4SLinus Torvalds struct in6_addr addr;
2225c15b1ccaSHannes Frederic Sowa
222688ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */
22272bda8a0cSBjørn Mork return;
22281da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
22291da177e4SLinus Torvalds if (ipv6_addr_any(&addr))
22301da177e4SLinus Torvalds return;
2231013b4d90SWANG Cong __ipv6_dev_ac_inc(ifp->idev, &addr);
22321da177e4SLinus Torvalds }
22331da177e4SLinus Torvalds
2234a9ed4a29SSabrina Dubroca /* caller must hold RTNL */
addrconf_leave_anycast(struct inet6_ifaddr * ifp)223520380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
22361da177e4SLinus Torvalds {
22371da177e4SLinus Torvalds struct in6_addr addr;
2238c15b1ccaSHannes Frederic Sowa
223988ad3149SHannes Frederic Sowa if (ifp->prefix_len >= 127) /* RFC 6164 */
224032019e65SYOSHIFUJI Hideaki return;
22411da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
22421da177e4SLinus Torvalds if (ipv6_addr_any(&addr))
22431da177e4SLinus Torvalds return;
22441da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr);
22451da177e4SLinus Torvalds }
22461da177e4SLinus Torvalds
addrconf_ifid_6lowpan(u8 * eui,struct net_device * dev)22478a7a4b47SAlexander Aring static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
224806a4c1c5Salex.bluesman.smirnov@gmail.com {
22498a7a4b47SAlexander Aring switch (dev->addr_len) {
22508a7a4b47SAlexander Aring case ETH_ALEN:
22519dae2e03SLuiz Augusto von Dentz memcpy(eui, dev->dev_addr, 3);
22529dae2e03SLuiz Augusto von Dentz eui[3] = 0xFF;
22539dae2e03SLuiz Augusto von Dentz eui[4] = 0xFE;
22549dae2e03SLuiz Augusto von Dentz memcpy(eui + 5, dev->dev_addr + 3, 3);
22559dae2e03SLuiz Augusto von Dentz break;
22568a7a4b47SAlexander Aring case EUI64_ADDR_LEN:
22575241c2d7SAlexander Aring memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
22585e98a36eSYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2;
22598a7a4b47SAlexander Aring break;
22608a7a4b47SAlexander Aring default:
22618a7a4b47SAlexander Aring return -1;
22628a7a4b47SAlexander Aring }
22638a7a4b47SAlexander Aring
226406a4c1c5Salex.bluesman.smirnov@gmail.com return 0;
226506a4c1c5Salex.bluesman.smirnov@gmail.com }
226606a4c1c5Salex.bluesman.smirnov@gmail.com
addrconf_ifid_ieee1394(u8 * eui,struct net_device * dev)2267cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2268cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
22691a8a23d2SJakub Kicinski const union fwnet_hwaddr *ha;
2270cb6bf355SYOSHIFUJI Hideaki / 吉藤英明
2271cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 if (dev->addr_len != FWNET_ALEN)
2272cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return -1;
2273cb6bf355SYOSHIFUJI Hideaki / 吉藤英明
22741a8a23d2SJakub Kicinski ha = (const union fwnet_hwaddr *)dev->dev_addr;
2275cb6bf355SYOSHIFUJI Hideaki / 吉藤英明
2276cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2277cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2;
2278cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return 0;
2279cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
2280cb6bf355SYOSHIFUJI Hideaki / 吉藤英明
addrconf_ifid_arcnet(u8 * eui,struct net_device * dev)2281073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2282073a8e0eSYOSHIFUJI Hideaki {
22831da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */
22841da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN)
22851da177e4SLinus Torvalds return -1;
22861da177e4SLinus Torvalds memset(eui, 0, 7);
22871da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr;
22881da177e4SLinus Torvalds return 0;
2289073a8e0eSYOSHIFUJI Hideaki }
2290073a8e0eSYOSHIFUJI Hideaki
addrconf_ifid_infiniband(u8 * eui,struct net_device * dev)2291073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2292073a8e0eSYOSHIFUJI Hideaki {
22931da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN)
22941da177e4SLinus Torvalds return -1;
22951da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8);
22961da177e4SLinus Torvalds eui[0] |= 2;
22971da177e4SLinus Torvalds return 0;
22981da177e4SLinus Torvalds }
2299073a8e0eSYOSHIFUJI Hideaki
__ipv6_isatap_ifid(u8 * eui,__be32 addr)2300c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2301dfd982baSYOSHIFUJI Hideaki {
23029af28511SSascha Hlusiak if (addr == 0)
23039af28511SSascha Hlusiak return -1;
2304dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2305dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2306dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2307dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2308dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2309dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2310dfd982baSYOSHIFUJI Hideaki eui[1] = 0;
2311dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E;
2312dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE;
2313dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4);
2314dfd982baSYOSHIFUJI Hideaki return 0;
2315dfd982baSYOSHIFUJI Hideaki }
2316dfd982baSYOSHIFUJI Hideaki
addrconf_ifid_sit(u8 * eui,struct net_device * dev)2317dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2318dfd982baSYOSHIFUJI Hideaki {
2319dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP)
2320dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2321dfd982baSYOSHIFUJI Hideaki return -1;
2322dfd982baSYOSHIFUJI Hideaki }
2323dfd982baSYOSHIFUJI Hideaki
addrconf_ifid_gre(u8 * eui,struct net_device * dev)2324aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2325aee80b54Sstephen hemminger {
2326aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2327aee80b54Sstephen hemminger }
2328aee80b54Sstephen hemminger
addrconf_ifid_ip6tnl(u8 * eui,struct net_device * dev)2329e837735eSNicolas Dichtel static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2330e837735eSNicolas Dichtel {
2331e837735eSNicolas Dichtel memcpy(eui, dev->perm_addr, 3);
2332e837735eSNicolas Dichtel memcpy(eui + 5, dev->perm_addr + 3, 3);
2333e837735eSNicolas Dichtel eui[3] = 0xFF;
2334e837735eSNicolas Dichtel eui[4] = 0xFE;
2335e837735eSNicolas Dichtel eui[0] ^= 2;
2336e837735eSNicolas Dichtel return 0;
2337e837735eSNicolas Dichtel }
2338e837735eSNicolas Dichtel
ipv6_generate_eui64(u8 * eui,struct net_device * dev)2339073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2340073a8e0eSYOSHIFUJI Hideaki {
2341073a8e0eSYOSHIFUJI Hideaki switch (dev->type) {
2342073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER:
2343073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI:
2344073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev);
2345073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET:
2346073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev);
2347073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND:
2348073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev);
2349c7dc89c0SFred L. Templin case ARPHRD_SIT:
2350dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev);
2351aee80b54Sstephen hemminger case ARPHRD_IPGRE:
235245ce0fd1SFelix Jia case ARPHRD_TUNNEL:
2353aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev);
2354e74bccb8SJukka Rissanen case ARPHRD_6LOWPAN:
23558a7a4b47SAlexander Aring return addrconf_ifid_6lowpan(eui, dev);
2356cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 case ARPHRD_IEEE1394:
2357cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return addrconf_ifid_ieee1394(eui, dev);
2358e837735eSNicolas Dichtel case ARPHRD_TUNNEL6:
235945ce0fd1SFelix Jia case ARPHRD_IP6GRE:
23609deb441cSSubash Abhinov Kasiviswanathan case ARPHRD_RAWIP:
2361e837735eSNicolas Dichtel return addrconf_ifid_ip6tnl(eui, dev);
2362073a8e0eSYOSHIFUJI Hideaki }
23631da177e4SLinus Torvalds return -1;
23641da177e4SLinus Torvalds }
23651da177e4SLinus Torvalds
ipv6_inherit_eui64(u8 * eui,struct inet6_dev * idev)23661da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
23671da177e4SLinus Torvalds {
23681da177e4SLinus Torvalds int err = -1;
23691da177e4SLinus Torvalds struct inet6_ifaddr *ifp;
23701da177e4SLinus Torvalds
23711da177e4SLinus Torvalds read_lock_bh(&idev->lock);
2372602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2373602582caSHannes Frederic Sowa if (ifp->scope > IFA_LINK)
2374602582caSHannes Frederic Sowa break;
23751da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
23761da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8);
23771da177e4SLinus Torvalds err = 0;
23781da177e4SLinus Torvalds break;
23791da177e4SLinus Torvalds }
23801da177e4SLinus Torvalds }
23811da177e4SLinus Torvalds read_unlock_bh(&idev->lock);
23821da177e4SLinus Torvalds return err;
23831da177e4SLinus Torvalds }
23841da177e4SLinus Torvalds
2385969c5464SFernando Gont /* Generation of a randomized Interface Identifier
2386969c5464SFernando Gont * draft-ietf-6man-rfc4941bis, Section 3.3.1
2387969c5464SFernando Gont */
2388969c5464SFernando Gont
ipv6_gen_rnd_iid(struct in6_addr * addr)2389969c5464SFernando Gont static void ipv6_gen_rnd_iid(struct in6_addr *addr)
23901da177e4SLinus Torvalds {
23911da177e4SLinus Torvalds regen:
2392969c5464SFernando Gont get_random_bytes(&addr->s6_addr[8], 8);
23931da177e4SLinus Torvalds
2394969c5464SFernando Gont /* <draft-ietf-6man-rfc4941bis-08.txt>, Section 3.3.1:
2395969c5464SFernando Gont * check if generated address is not inappropriate:
23961da177e4SLinus Torvalds *
2397912b519aSBhaskar Chowdhury * - Reserved IPv6 Interface Identifiers
23981da177e4SLinus Torvalds * - XXX: already assigned to an address on the device
23991da177e4SLinus Torvalds */
24001da177e4SLinus Torvalds
2401969c5464SFernando Gont /* Subnet-router anycast: 0000:0000:0000:0000 */
2402969c5464SFernando Gont if (!(addr->s6_addr32[2] | addr->s6_addr32[3]))
2403969c5464SFernando Gont goto regen;
2404969c5464SFernando Gont
2405969c5464SFernando Gont /* IANA Ethernet block: 0200:5EFF:FE00:0000-0200:5EFF:FE00:5212
2406969c5464SFernando Gont * Proxy Mobile IPv6: 0200:5EFF:FE00:5213
2407969c5464SFernando Gont * IANA Ethernet block: 0200:5EFF:FE00:5214-0200:5EFF:FEFF:FFFF
2408969c5464SFernando Gont */
2409969c5464SFernando Gont if (ntohl(addr->s6_addr32[2]) == 0x02005eff &&
2410969c5464SFernando Gont (ntohl(addr->s6_addr32[3]) & 0Xff000000) == 0xfe000000)
2411969c5464SFernando Gont goto regen;
2412969c5464SFernando Gont
2413969c5464SFernando Gont /* Reserved subnet anycast addresses */
2414969c5464SFernando Gont if (ntohl(addr->s6_addr32[2]) == 0xfdffffff &&
2415969c5464SFernando Gont ntohl(addr->s6_addr32[3]) >= 0Xffffff80)
2416969c5464SFernando Gont goto regen;
24171da177e4SLinus Torvalds }
24181da177e4SLinus Torvalds
24191da177e4SLinus Torvalds /*
24201da177e4SLinus Torvalds * Add prefix route.
24211da177e4SLinus Torvalds */
24221da177e4SLinus Torvalds
24231da177e4SLinus Torvalds static void
addrconf_prefix_route(struct in6_addr * pfx,int plen,u32 metric,struct net_device * dev,unsigned long expires,u32 flags,gfp_t gfp_flags)24248308f3ffSDavid Ahern addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
24258308f3ffSDavid Ahern struct net_device *dev, unsigned long expires,
24268308f3ffSDavid Ahern u32 flags, gfp_t gfp_flags)
24271da177e4SLinus Torvalds {
242886872cb5SThomas Graf struct fib6_config cfg = {
2429ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
24308308f3ffSDavid Ahern .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
243186872cb5SThomas Graf .fc_ifindex = dev->ifindex,
243286872cb5SThomas Graf .fc_expires = expires,
243386872cb5SThomas Graf .fc_dst_len = plen,
243486872cb5SThomas Graf .fc_flags = RTF_UP | flags,
2435c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev),
2436f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL,
2437e8478e80SDavid Ahern .fc_type = RTN_UNICAST,
243886872cb5SThomas Graf };
24391da177e4SLinus Torvalds
24404e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx;
24411da177e4SLinus Torvalds
24421da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT.
24431da177e4SLinus Torvalds This thing is done here expecting that the whole
24441da177e4SLinus Torvalds class of non-broadcast devices need not cloning.
24451da177e4SLinus Torvalds */
244607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
24471da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
244886872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP;
24490be669bbSJoerg Roedel #endif
24501da177e4SLinus Torvalds
2451acb54e3cSDavid Ahern ip6_route_add(&cfg, gfp_flags, NULL);
24521da177e4SLinus Torvalds }
24531da177e4SLinus Torvalds
245414ef37b6SAndreas Hofmeister
addrconf_get_prefix_route(const struct in6_addr * pfx,int plen,const struct net_device * dev,u32 flags,u32 noflags,bool no_gw)24558d1c802bSDavid Ahern static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
245614ef37b6SAndreas Hofmeister int plen,
245714ef37b6SAndreas Hofmeister const struct net_device *dev,
24582b2450caSDavid Ahern u32 flags, u32 noflags,
24592b2450caSDavid Ahern bool no_gw)
246014ef37b6SAndreas Hofmeister {
246114ef37b6SAndreas Hofmeister struct fib6_node *fn;
24628d1c802bSDavid Ahern struct fib6_info *rt = NULL;
246314ef37b6SAndreas Hofmeister struct fib6_table *table;
2464ca254490SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
246514ef37b6SAndreas Hofmeister
2466ca254490SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id);
246763159f29SIan Morris if (!table)
246814ef37b6SAndreas Hofmeister return NULL;
246914ef37b6SAndreas Hofmeister
247066f5d6ceSWei Wang rcu_read_lock();
247138fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
247214ef37b6SAndreas Hofmeister if (!fn)
247314ef37b6SAndreas Hofmeister goto out;
24741f56a01fSMartin KaFai Lau
247566f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) {
2476f88d8ea6SDavid Ahern /* prefix routes only use builtin fib6_nh */
2477f88d8ea6SDavid Ahern if (rt->nh)
2478f88d8ea6SDavid Ahern continue;
2479f88d8ea6SDavid Ahern
24801cf844c7SDavid Ahern if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex)
248114ef37b6SAndreas Hofmeister continue;
24821cf844c7SDavid Ahern if (no_gw && rt->fib6_nh->fib_nh_gw_family)
24832b2450caSDavid Ahern continue;
248493c2fb25SDavid Ahern if ((rt->fib6_flags & flags) != flags)
248514ef37b6SAndreas Hofmeister continue;
248693c2fb25SDavid Ahern if ((rt->fib6_flags & noflags) != 0)
248714ef37b6SAndreas Hofmeister continue;
2488e873e4b9SWei Wang if (!fib6_info_hold_safe(rt))
2489e873e4b9SWei Wang continue;
249014ef37b6SAndreas Hofmeister break;
249114ef37b6SAndreas Hofmeister }
249214ef37b6SAndreas Hofmeister out:
249366f5d6ceSWei Wang rcu_read_unlock();
249414ef37b6SAndreas Hofmeister return rt;
249514ef37b6SAndreas Hofmeister }
249614ef37b6SAndreas Hofmeister
249714ef37b6SAndreas Hofmeister
24981da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
24991da177e4SLinus Torvalds
addrconf_add_mroute(struct net_device * dev)25001da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
25011da177e4SLinus Torvalds {
250286872cb5SThomas Graf struct fib6_config cfg = {
2503ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
250486872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF,
250586872cb5SThomas Graf .fc_ifindex = dev->ifindex,
250686872cb5SThomas Graf .fc_dst_len = 8,
250786872cb5SThomas Graf .fc_flags = RTF_UP,
2508ceed9038SMatteo Croce .fc_type = RTN_MULTICAST,
2509c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev),
2510a826b043SMatteo Croce .fc_protocol = RTPROT_KERNEL,
251186872cb5SThomas Graf };
25121da177e4SLinus Torvalds
251386872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
251486872cb5SThomas Graf
2515e500c6d3SCong Wang ip6_route_add(&cfg, GFP_KERNEL, NULL);
25161da177e4SLinus Torvalds }
25171da177e4SLinus Torvalds
addrconf_add_dev(struct net_device * dev)25181da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
25191da177e4SLinus Torvalds {
25201da177e4SLinus Torvalds struct inet6_dev *idev;
25211da177e4SLinus Torvalds
25221da177e4SLinus Torvalds ASSERT_RTNL();
25231da177e4SLinus Torvalds
2524e21e8467SStephen Hemminger idev = ipv6_find_idev(dev);
2525db0b99f5SSabrina Dubroca if (IS_ERR(idev))
2526db0b99f5SSabrina Dubroca return idev;
252764e724f6SBrian Haley
252864e724f6SBrian Haley if (idev->cnf.disable_ipv6)
252964e724f6SBrian Haley return ERR_PTR(-EACCES);
25301da177e4SLinus Torvalds
25311da177e4SLinus Torvalds /* Add default multicast route */
2532ba46ee4cSDavid Ahern if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
25331da177e4SLinus Torvalds addrconf_add_mroute(dev);
25341da177e4SLinus Torvalds
25351da177e4SLinus Torvalds return idev;
25361da177e4SLinus Torvalds }
25371da177e4SLinus Torvalds
delete_tempaddrs(struct inet6_dev * idev,struct inet6_ifaddr * ifp)2538*836deb96SHangbin Liu static void delete_tempaddrs(struct inet6_dev *idev,
2539*836deb96SHangbin Liu struct inet6_ifaddr *ifp)
2540*836deb96SHangbin Liu {
2541*836deb96SHangbin Liu struct inet6_ifaddr *ift, *tmp;
2542*836deb96SHangbin Liu
2543*836deb96SHangbin Liu write_lock_bh(&idev->lock);
2544*836deb96SHangbin Liu list_for_each_entry_safe(ift, tmp, &idev->tempaddr_list, tmp_list) {
2545*836deb96SHangbin Liu if (ift->ifpub != ifp)
2546*836deb96SHangbin Liu continue;
2547*836deb96SHangbin Liu
2548*836deb96SHangbin Liu in6_ifa_hold(ift);
2549*836deb96SHangbin Liu write_unlock_bh(&idev->lock);
2550*836deb96SHangbin Liu ipv6_del_addr(ift);
2551*836deb96SHangbin Liu write_lock_bh(&idev->lock);
2552*836deb96SHangbin Liu }
2553*836deb96SHangbin Liu write_unlock_bh(&idev->lock);
2554*836deb96SHangbin Liu }
2555*836deb96SHangbin Liu
manage_tempaddrs(struct inet6_dev * idev,struct inet6_ifaddr * ifp,__u32 valid_lft,__u32 prefered_lft,bool create,unsigned long now)255653bd6749SJiri Pirko static void manage_tempaddrs(struct inet6_dev *idev,
255753bd6749SJiri Pirko struct inet6_ifaddr *ifp,
255853bd6749SJiri Pirko __u32 valid_lft, __u32 prefered_lft,
255953bd6749SJiri Pirko bool create, unsigned long now)
256053bd6749SJiri Pirko {
256153bd6749SJiri Pirko u32 flags;
256253bd6749SJiri Pirko struct inet6_ifaddr *ift;
256353bd6749SJiri Pirko
256453bd6749SJiri Pirko read_lock_bh(&idev->lock);
256553bd6749SJiri Pirko /* update all temporary addresses in the list */
256653bd6749SJiri Pirko list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
256753bd6749SJiri Pirko int age, max_valid, max_prefered;
256853bd6749SJiri Pirko
256953bd6749SJiri Pirko if (ifp != ift->ifpub)
257053bd6749SJiri Pirko continue;
257153bd6749SJiri Pirko
257253bd6749SJiri Pirko /* RFC 4941 section 3.3:
257353bd6749SJiri Pirko * If a received option will extend the lifetime of a public
257453bd6749SJiri Pirko * address, the lifetimes of temporary addresses should
257553bd6749SJiri Pirko * be extended, subject to the overall constraint that no
257653bd6749SJiri Pirko * temporary addresses should ever remain "valid" or "preferred"
257753bd6749SJiri Pirko * for a time longer than (TEMP_VALID_LIFETIME) or
257853bd6749SJiri Pirko * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
257953bd6749SJiri Pirko */
258053bd6749SJiri Pirko age = (now - ift->cstamp) / HZ;
258153bd6749SJiri Pirko max_valid = idev->cnf.temp_valid_lft - age;
258253bd6749SJiri Pirko if (max_valid < 0)
258353bd6749SJiri Pirko max_valid = 0;
258453bd6749SJiri Pirko
258553bd6749SJiri Pirko max_prefered = idev->cnf.temp_prefered_lft -
258676506a98SJiri Bohac idev->desync_factor - age;
258753bd6749SJiri Pirko if (max_prefered < 0)
258853bd6749SJiri Pirko max_prefered = 0;
258953bd6749SJiri Pirko
259053bd6749SJiri Pirko if (valid_lft > max_valid)
259153bd6749SJiri Pirko valid_lft = max_valid;
259253bd6749SJiri Pirko
259353bd6749SJiri Pirko if (prefered_lft > max_prefered)
259453bd6749SJiri Pirko prefered_lft = max_prefered;
259553bd6749SJiri Pirko
259653bd6749SJiri Pirko spin_lock(&ift->lock);
259753bd6749SJiri Pirko flags = ift->flags;
259853bd6749SJiri Pirko ift->valid_lft = valid_lft;
259953bd6749SJiri Pirko ift->prefered_lft = prefered_lft;
260053bd6749SJiri Pirko ift->tstamp = now;
260153bd6749SJiri Pirko if (prefered_lft > 0)
260253bd6749SJiri Pirko ift->flags &= ~IFA_F_DEPRECATED;
260353bd6749SJiri Pirko
260453bd6749SJiri Pirko spin_unlock(&ift->lock);
260553bd6749SJiri Pirko if (!(flags&IFA_F_TENTATIVE))
260653bd6749SJiri Pirko ipv6_ifa_notify(0, ift);
260753bd6749SJiri Pirko }
260853bd6749SJiri Pirko
260969172f0bSMaciej Żenczykowski /* Also create a temporary address if it's enabled but no temporary
261069172f0bSMaciej Żenczykowski * address currently exists.
261169172f0bSMaciej Żenczykowski * However, we get called with valid_lft == 0, prefered_lft == 0, create == false
261269172f0bSMaciej Żenczykowski * as part of cleanup (ie. deleting the mngtmpaddr).
261369172f0bSMaciej Żenczykowski * We don't want that to result in creating a new temporary ip address.
261469172f0bSMaciej Żenczykowski */
261569172f0bSMaciej Żenczykowski if (list_empty(&idev->tempaddr_list) && (valid_lft || prefered_lft))
261669172f0bSMaciej Żenczykowski create = true;
261769172f0bSMaciej Żenczykowski
261869172f0bSMaciej Żenczykowski if (create && idev->cnf.use_tempaddr > 0) {
261953bd6749SJiri Pirko /* When a new public address is created as described
262053bd6749SJiri Pirko * in [ADDRCONF], also create a new temporary address.
262153bd6749SJiri Pirko */
262253bd6749SJiri Pirko read_unlock_bh(&idev->lock);
2623969c5464SFernando Gont ipv6_create_tempaddr(ifp, false);
262453bd6749SJiri Pirko } else {
262553bd6749SJiri Pirko read_unlock_bh(&idev->lock);
262653bd6749SJiri Pirko }
262753bd6749SJiri Pirko }
262853bd6749SJiri Pirko
is_addr_mode_generate_stable(struct inet6_dev * idev)2629cc9da6ccSBjørn Mork static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2630cc9da6ccSBjørn Mork {
2631d35a00b8SFelix Jia return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2632d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2633cc9da6ccSBjørn Mork }
2634cc9da6ccSBjørn Mork
addrconf_prefix_rcv_add_addr(struct net * net,struct net_device * dev,const struct prefix_info * pinfo,struct inet6_dev * in6_dev,const struct in6_addr * addr,int addr_type,u32 addr_flags,bool sllao,bool tokenized,__u32 valid_lft,u32 prefered_lft)2635cc84b3c6SAlexander Aring int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
26364f672235SAlexander Aring const struct prefix_info *pinfo,
26374f672235SAlexander Aring struct inet6_dev *in6_dev,
2638cc84b3c6SAlexander Aring const struct in6_addr *addr, int addr_type,
2639cc84b3c6SAlexander Aring u32 addr_flags, bool sllao, bool tokenized,
26404f672235SAlexander Aring __u32 valid_lft, u32 prefered_lft)
26414f672235SAlexander Aring {
26424f672235SAlexander Aring struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
264336268983SGuillaume Nault int create = 0, update_lft = 0;
26444f672235SAlexander Aring
26454f672235SAlexander Aring if (!ifp && valid_lft) {
26464f672235SAlexander Aring int max_addresses = in6_dev->cnf.max_addresses;
2647e6464b8cSDavid Ahern struct ifa6_config cfg = {
2648e6464b8cSDavid Ahern .pfx = addr,
2649e6464b8cSDavid Ahern .plen = pinfo->prefix_len,
2650e6464b8cSDavid Ahern .ifa_flags = addr_flags,
2651e6464b8cSDavid Ahern .valid_lft = valid_lft,
2652e6464b8cSDavid Ahern .preferred_lft = prefered_lft,
2653e6464b8cSDavid Ahern .scope = addr_type & IPV6_ADDR_SCOPE_MASK,
265447f0bd50SJacques de Laval .ifa_proto = IFAPROT_KERNEL_RA
2655e6464b8cSDavid Ahern };
26564f672235SAlexander Aring
26574f672235SAlexander Aring #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
265835e015e1SMatteo Croce if ((net->ipv6.devconf_all->optimistic_dad ||
265935e015e1SMatteo Croce in6_dev->cnf.optimistic_dad) &&
26604f672235SAlexander Aring !net->ipv6.devconf_all->forwarding && sllao)
2661e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC;
26624f672235SAlexander Aring #endif
26634f672235SAlexander Aring
26644f672235SAlexander Aring /* Do not allow to create too much of autoconfigured
26654f672235SAlexander Aring * addresses; this would be too easy way to crash kernel.
26664f672235SAlexander Aring */
26674f672235SAlexander Aring if (!max_addresses ||
26684f672235SAlexander Aring ipv6_count_addresses(in6_dev) < max_addresses)
2669e6464b8cSDavid Ahern ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
26704f672235SAlexander Aring
26714f672235SAlexander Aring if (IS_ERR_OR_NULL(ifp))
26724f672235SAlexander Aring return -1;
26734f672235SAlexander Aring
26744f672235SAlexander Aring create = 1;
26754f672235SAlexander Aring spin_lock_bh(&ifp->lock);
26764f672235SAlexander Aring ifp->flags |= IFA_F_MANAGETEMPADDR;
26774f672235SAlexander Aring ifp->cstamp = jiffies;
26784f672235SAlexander Aring ifp->tokenized = tokenized;
26794f672235SAlexander Aring spin_unlock_bh(&ifp->lock);
26804f672235SAlexander Aring addrconf_dad_start(ifp);
26814f672235SAlexander Aring }
26824f672235SAlexander Aring
26834f672235SAlexander Aring if (ifp) {
26844f672235SAlexander Aring u32 flags;
26854f672235SAlexander Aring unsigned long now;
26864f672235SAlexander Aring u32 stored_lft;
26874f672235SAlexander Aring
268836268983SGuillaume Nault /* update lifetime (RFC2462 5.5.3 e) */
26894f672235SAlexander Aring spin_lock_bh(&ifp->lock);
26904f672235SAlexander Aring now = jiffies;
26914f672235SAlexander Aring if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
26924f672235SAlexander Aring stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
26934f672235SAlexander Aring else
26944f672235SAlexander Aring stored_lft = 0;
2695f85f94b8SLorenzo Bianconi if (!create && stored_lft) {
269636268983SGuillaume Nault const u32 minimum_lft = min_t(u32,
269736268983SGuillaume Nault stored_lft, MIN_VALID_LIFETIME);
269836268983SGuillaume Nault valid_lft = max(valid_lft, minimum_lft);
269936268983SGuillaume Nault
270036268983SGuillaume Nault /* RFC4862 Section 5.5.3e:
270136268983SGuillaume Nault * "Note that the preferred lifetime of the
270236268983SGuillaume Nault * corresponding address is always reset to
270336268983SGuillaume Nault * the Preferred Lifetime in the received
270436268983SGuillaume Nault * Prefix Information option, regardless of
270536268983SGuillaume Nault * whether the valid lifetime is also reset or
270636268983SGuillaume Nault * ignored."
270736268983SGuillaume Nault *
270836268983SGuillaume Nault * So we should always update prefered_lft here.
270936268983SGuillaume Nault */
271036268983SGuillaume Nault update_lft = 1;
271136268983SGuillaume Nault }
271236268983SGuillaume Nault
271336268983SGuillaume Nault if (update_lft) {
27144f672235SAlexander Aring ifp->valid_lft = valid_lft;
27154f672235SAlexander Aring ifp->prefered_lft = prefered_lft;
27164f672235SAlexander Aring ifp->tstamp = now;
27174f672235SAlexander Aring flags = ifp->flags;
27184f672235SAlexander Aring ifp->flags &= ~IFA_F_DEPRECATED;
27194f672235SAlexander Aring spin_unlock_bh(&ifp->lock);
27204f672235SAlexander Aring
27214f672235SAlexander Aring if (!(flags&IFA_F_TENTATIVE))
27224f672235SAlexander Aring ipv6_ifa_notify(0, ifp);
27234f672235SAlexander Aring } else
27244f672235SAlexander Aring spin_unlock_bh(&ifp->lock);
27254f672235SAlexander Aring
27264f672235SAlexander Aring manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
27274f672235SAlexander Aring create, now);
27284f672235SAlexander Aring
27294f672235SAlexander Aring in6_ifa_put(ifp);
27308805d13fSEric Dumazet addrconf_verify(net);
27314f672235SAlexander Aring }
27324f672235SAlexander Aring
27334f672235SAlexander Aring return 0;
27344f672235SAlexander Aring }
2735cc84b3c6SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
27364f672235SAlexander Aring
addrconf_prefix_rcv(struct net_device * dev,u8 * opt,int len,bool sllao)2737e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
27381da177e4SLinus Torvalds {
27391da177e4SLinus Torvalds struct prefix_info *pinfo;
27401da177e4SLinus Torvalds __u32 valid_lft;
27411da177e4SLinus Torvalds __u32 prefered_lft;
27424f672235SAlexander Aring int addr_type, err;
274364236f3fSHannes Frederic Sowa u32 addr_flags = 0;
27441da177e4SLinus Torvalds struct inet6_dev *in6_dev;
274556d417b1SBrian Haley struct net *net = dev_net(dev);
27461da177e4SLinus Torvalds
27471da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt;
27481da177e4SLinus Torvalds
27491da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) {
2750e32ac250SJoe Perches netdev_dbg(dev, "addrconf: prefix option too short\n");
27511da177e4SLinus Torvalds return;
27521da177e4SLinus Torvalds }
27531da177e4SLinus Torvalds
27541da177e4SLinus Torvalds /*
27551da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19)
27561da177e4SLinus Torvalds */
27571da177e4SLinus Torvalds
27581da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix);
27591da177e4SLinus Torvalds
27601da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
27611da177e4SLinus Torvalds return;
27621da177e4SLinus Torvalds
27631da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid);
27641da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered);
27651da177e4SLinus Torvalds
27661da177e4SLinus Torvalds if (prefered_lft > valid_lft) {
2767e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
27681da177e4SLinus Torvalds return;
27691da177e4SLinus Torvalds }
27701da177e4SLinus Torvalds
27711da177e4SLinus Torvalds in6_dev = in6_dev_get(dev);
27721da177e4SLinus Torvalds
277363159f29SIan Morris if (!in6_dev) {
2774e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n",
2775e87cc472SJoe Perches dev->name);
27761da177e4SLinus Torvalds return;
27771da177e4SLinus Torvalds }
27781da177e4SLinus Torvalds
27795027d54aSPatrick Rohr if (valid_lft != 0 && valid_lft < in6_dev->cnf.accept_ra_min_lft)
27805cb24968SPatrick Rohr goto put;
27815027d54aSPatrick Rohr
27821da177e4SLinus Torvalds /*
27831da177e4SLinus Torvalds * Two things going on here:
27841da177e4SLinus Torvalds * 1) Add routes for on-link prefixes
27851da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set
27861da177e4SLinus Torvalds */
27871da177e4SLinus Torvalds
27884bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) {
27898d1c802bSDavid Ahern struct fib6_info *rt;
27904bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires;
27914bed72e4SYOSHIFUJI Hideaki
27921da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could
27936f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft,
27946f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it
27956f704992SYOSHIFUJI Hideaki * not good.
27961da177e4SLinus Torvalds */
27974bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ)
27984bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
27994bed72e4SYOSHIFUJI Hideaki else
28004bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
28013dd4bc68SYOSHIFUJI Hideaki
28024bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires))
28034bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ;
28041da177e4SLinus Torvalds
280514ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix,
280614ef37b6SAndreas Hofmeister pinfo->prefix_len,
280714ef37b6SAndreas Hofmeister dev,
280814ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT,
28092b2450caSDavid Ahern RTF_DEFAULT, true);
28101da177e4SLinus Torvalds
281114ef37b6SAndreas Hofmeister if (rt) {
28126f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */
28131da177e4SLinus Torvalds if (valid_lft == 0) {
281411dd74b3SRoopa Prabhu ip6_del_rt(net, rt, false);
28151da177e4SLinus Torvalds rt = NULL;
28164bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) {
28176f704992SYOSHIFUJI Hideaki /* not infinity */
281814895687SDavid Ahern fib6_set_expires(rt, jiffies + rt_expires);
28196f704992SYOSHIFUJI Hideaki } else {
282014895687SDavid Ahern fib6_clean_expires(rt);
28211da177e4SLinus Torvalds }
28221da177e4SLinus Torvalds } else if (valid_lft) {
28236f704992SYOSHIFUJI Hideaki clock_t expires = 0;
28244bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
28254bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) {
28266f704992SYOSHIFUJI Hideaki /* not infinity */
28276f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES;
28286f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires);
28296f704992SYOSHIFUJI Hideaki }
28301da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
28318308f3ffSDavid Ahern 0, dev, expires, flags,
28328308f3ffSDavid Ahern GFP_ATOMIC);
28331da177e4SLinus Torvalds }
283493531c67SDavid Ahern fib6_info_release(rt);
28351da177e4SLinus Torvalds }
28361da177e4SLinus Torvalds
28371da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */
28381da177e4SLinus Torvalds
28391da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) {
28401da177e4SLinus Torvalds struct in6_addr addr;
2841f997c55cSAlexander Aring bool tokenized = false, dev_addr_generated = false;
28421da177e4SLinus Torvalds
28431da177e4SLinus Torvalds if (pinfo->prefix_len == 64) {
28441da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8);
2845f53adae4SDaniel Borkmann
2846f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) {
2847f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock);
2848f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8,
2849f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8);
2850f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock);
2851617fe29dSDaniel Borkmann tokenized = true;
2852cc9da6ccSBjørn Mork } else if (is_addr_mode_generate_stable(in6_dev) &&
2853622c81d5SHannes Frederic Sowa !ipv6_generate_stable_address(&addr, 0,
2854622c81d5SHannes Frederic Sowa in6_dev)) {
285564236f3fSHannes Frederic Sowa addr_flags |= IFA_F_STABLE_PRIVACY;
2856622c81d5SHannes Frederic Sowa goto ok;
2857f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
28581da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
28594f672235SAlexander Aring goto put;
2860f997c55cSAlexander Aring } else {
2861f997c55cSAlexander Aring dev_addr_generated = true;
28621da177e4SLinus Torvalds }
28631da177e4SLinus Torvalds goto ok;
28641da177e4SLinus Torvalds }
2865e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
28661da177e4SLinus Torvalds pinfo->prefix_len);
28674f672235SAlexander Aring goto put;
28681da177e4SLinus Torvalds
28691da177e4SLinus Torvalds ok:
28704f672235SAlexander Aring err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
28714f672235SAlexander Aring &addr, addr_type,
28724f672235SAlexander Aring addr_flags, sllao,
28734f672235SAlexander Aring tokenized, valid_lft,
28748a226b2cSJiri Benc prefered_lft);
28754f672235SAlexander Aring if (err)
28764f672235SAlexander Aring goto put;
2877f997c55cSAlexander Aring
2878f997c55cSAlexander Aring /* Ignore error case here because previous prefix add addr was
2879f997c55cSAlexander Aring * successful which will be notified.
2880f997c55cSAlexander Aring */
2881f997c55cSAlexander Aring ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2882f997c55cSAlexander Aring addr_type, addr_flags, sllao,
2883f997c55cSAlexander Aring tokenized, valid_lft,
2884f997c55cSAlexander Aring prefered_lft,
2885f997c55cSAlexander Aring dev_addr_generated);
28861da177e4SLinus Torvalds }
28871da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
28884f672235SAlexander Aring put:
28891da177e4SLinus Torvalds in6_dev_put(in6_dev);
28901da177e4SLinus Torvalds }
28911da177e4SLinus Torvalds
addrconf_set_sit_dstaddr(struct net * net,struct net_device * dev,struct in6_ifreq * ireq)289268ad6886SChristoph Hellwig static int addrconf_set_sit_dstaddr(struct net *net, struct net_device *dev,
289368ad6886SChristoph Hellwig struct in6_ifreq *ireq)
289468ad6886SChristoph Hellwig {
289568ad6886SChristoph Hellwig struct ip_tunnel_parm p = { };
289668ad6886SChristoph Hellwig int err;
289768ad6886SChristoph Hellwig
289868ad6886SChristoph Hellwig if (!(ipv6_addr_type(&ireq->ifr6_addr) & IPV6_ADDR_COMPATv4))
289968ad6886SChristoph Hellwig return -EADDRNOTAVAIL;
290068ad6886SChristoph Hellwig
290168ad6886SChristoph Hellwig p.iph.daddr = ireq->ifr6_addr.s6_addr32[3];
290268ad6886SChristoph Hellwig p.iph.version = 4;
290368ad6886SChristoph Hellwig p.iph.ihl = 5;
290468ad6886SChristoph Hellwig p.iph.protocol = IPPROTO_IPV6;
290568ad6886SChristoph Hellwig p.iph.ttl = 64;
290668ad6886SChristoph Hellwig
29078e3db0bbSChristoph Hellwig if (!dev->netdev_ops->ndo_tunnel_ctl)
290868ad6886SChristoph Hellwig return -EOPNOTSUPP;
29098e3db0bbSChristoph Hellwig err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, SIOCADDTUNNEL);
291068ad6886SChristoph Hellwig if (err)
291168ad6886SChristoph Hellwig return err;
291268ad6886SChristoph Hellwig
291368ad6886SChristoph Hellwig dev = __dev_get_by_name(net, p.name);
291468ad6886SChristoph Hellwig if (!dev)
291568ad6886SChristoph Hellwig return -ENOBUFS;
291668ad6886SChristoph Hellwig return dev_open(dev, NULL);
291768ad6886SChristoph Hellwig }
291868ad6886SChristoph Hellwig
29191da177e4SLinus Torvalds /*
29201da177e4SLinus Torvalds * Set destination address.
29211da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual"
29221da177e4SLinus Torvalds * device.
29231da177e4SLinus Torvalds */
addrconf_set_dstaddr(struct net * net,void __user * arg)2924af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
29251da177e4SLinus Torvalds {
29261da177e4SLinus Torvalds struct net_device *dev;
292768ad6886SChristoph Hellwig struct in6_ifreq ireq;
292868ad6886SChristoph Hellwig int err = -ENODEV;
29291da177e4SLinus Torvalds
2930f0988460SChristoph Hellwig if (!IS_ENABLED(CONFIG_IPV6_SIT))
2931f0988460SChristoph Hellwig return -ENODEV;
293268ad6886SChristoph Hellwig if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
293368ad6886SChristoph Hellwig return -EFAULT;
29341da177e4SLinus Torvalds
29351da177e4SLinus Torvalds rtnl_lock();
2936af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
293768ad6886SChristoph Hellwig if (dev && dev->type == ARPHRD_SIT)
293868ad6886SChristoph Hellwig err = addrconf_set_sit_dstaddr(net, dev, &ireq);
29391da177e4SLinus Torvalds rtnl_unlock();
29401da177e4SLinus Torvalds return err;
29411da177e4SLinus Torvalds }
29421da177e4SLinus Torvalds
ipv6_mc_config(struct sock * sk,bool join,const struct in6_addr * addr,int ifindex)294393a714d6SMadhu Challa static int ipv6_mc_config(struct sock *sk, bool join,
294493a714d6SMadhu Challa const struct in6_addr *addr, int ifindex)
294593a714d6SMadhu Challa {
294693a714d6SMadhu Challa int ret;
294793a714d6SMadhu Challa
294893a714d6SMadhu Challa ASSERT_RTNL();
294993a714d6SMadhu Challa
295093a714d6SMadhu Challa lock_sock(sk);
295193a714d6SMadhu Challa if (join)
295254ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_join(sk, ifindex, addr);
295393a714d6SMadhu Challa else
295454ff9ef3SMarcelo Ricardo Leitner ret = ipv6_sock_mc_drop(sk, ifindex, addr);
295593a714d6SMadhu Challa release_sock(sk);
295693a714d6SMadhu Challa
295793a714d6SMadhu Challa return ret;
295893a714d6SMadhu Challa }
295993a714d6SMadhu Challa
29601da177e4SLinus Torvalds /*
29611da177e4SLinus Torvalds * Manual configuration of address on an interface
29621da177e4SLinus Torvalds */
inet6_addr_add(struct net * net,int ifindex,struct ifa6_config * cfg,struct netlink_ext_ack * extack)2963479840ffSJiri Pirko static int inet6_addr_add(struct net *net, int ifindex,
296419b1518cSDavid Ahern struct ifa6_config *cfg,
2965de95e047SDavid Ahern struct netlink_ext_ack *extack)
29661da177e4SLinus Torvalds {
29671da177e4SLinus Torvalds struct inet6_ifaddr *ifp;
29681da177e4SLinus Torvalds struct inet6_dev *idev;
29691da177e4SLinus Torvalds struct net_device *dev;
297093a714d6SMadhu Challa unsigned long timeout;
297193a714d6SMadhu Challa clock_t expires;
29726f704992SYOSHIFUJI Hideaki u32 flags;
29731da177e4SLinus Torvalds
29741da177e4SLinus Torvalds ASSERT_RTNL();
29751da177e4SLinus Torvalds
29767f6c4039SHangbin Liu if (cfg->plen > 128) {
29777f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length");
297824ef0da7SThomas Graf return -EINVAL;
29797f6c4039SHangbin Liu }
298024ef0da7SThomas Graf
29810778769dSNoriaki TAKAMIYA /* check the lifetime */
29827f6c4039SHangbin Liu if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) {
29837f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "address lifetime invalid");
29840778769dSNoriaki TAKAMIYA return -EINVAL;
29857f6c4039SHangbin Liu }
29860778769dSNoriaki TAKAMIYA
29877f6c4039SHangbin Liu if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) {
29887f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "address with \"mngtmpaddr\" flag must have a prefix length of 64");
298953bd6749SJiri Pirko return -EINVAL;
29907f6c4039SHangbin Liu }
299153bd6749SJiri Pirko
2992af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex);
2993af284937SDaniel Lezcano if (!dev)
29941da177e4SLinus Torvalds return -ENODEV;
29951da177e4SLinus Torvalds
299664e724f6SBrian Haley idev = addrconf_add_dev(dev);
29977f6c4039SHangbin Liu if (IS_ERR(idev)) {
29987f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
299964e724f6SBrian Haley return PTR_ERR(idev);
30007f6c4039SHangbin Liu }
30011da177e4SLinus Torvalds
300219b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
300393a714d6SMadhu Challa int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
300419b1518cSDavid Ahern true, cfg->pfx, ifindex);
300593a714d6SMadhu Challa
30067f6c4039SHangbin Liu if (ret < 0) {
30077f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "Multicast auto join failed");
300893a714d6SMadhu Challa return ret;
300993a714d6SMadhu Challa }
30107f6c4039SHangbin Liu }
301193a714d6SMadhu Challa
301219b1518cSDavid Ahern cfg->scope = ipv6_addr_scope(cfg->pfx);
30131da177e4SLinus Torvalds
301419b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
30154bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) {
30164bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ);
301719b1518cSDavid Ahern cfg->valid_lft = timeout;
30186f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES;
30194bed72e4SYOSHIFUJI Hideaki } else {
30204bed72e4SYOSHIFUJI Hideaki expires = 0;
30214bed72e4SYOSHIFUJI Hideaki flags = 0;
302219b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT;
30236f704992SYOSHIFUJI Hideaki }
30240778769dSNoriaki TAKAMIYA
302519b1518cSDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
30264bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) {
30274bed72e4SYOSHIFUJI Hideaki if (timeout == 0)
302819b1518cSDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED;
302919b1518cSDavid Ahern cfg->preferred_lft = timeout;
30304bed72e4SYOSHIFUJI Hideaki }
30310778769dSNoriaki TAKAMIYA
303219b1518cSDavid Ahern ifp = ipv6_add_addr(idev, cfg, true, extack);
30331da177e4SLinus Torvalds if (!IS_ERR(ifp)) {
303419b1518cSDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
30358308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
30368308f3ffSDavid Ahern ifp->rt_priority, dev, expires,
30378308f3ffSDavid Ahern flags, GFP_KERNEL);
3038761aac73SThomas Haller }
3039761aac73SThomas Haller
3040a2d481b3SLorenzo Bianconi /* Send a netlink notification if DAD is enabled and
3041a2d481b3SLorenzo Bianconi * optimistic flag is not set
3042a2d481b3SLorenzo Bianconi */
3043a2d481b3SLorenzo Bianconi if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
3044a2d481b3SLorenzo Bianconi ipv6_ifa_notify(0, ifp);
304595c385b4SNeil Horman /*
304695c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates
304795c385b4SNeil Horman * that the Optimistic flag should not be set for
304895c385b4SNeil Horman * manually configured addresses
304995c385b4SNeil Horman */
3050cf22f9a2SDavid S. Miller addrconf_dad_start(ifp);
305119b1518cSDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
305219b1518cSDavid Ahern manage_tempaddrs(idev, ifp, cfg->valid_lft,
305319b1518cSDavid Ahern cfg->preferred_lft, true, jiffies);
30541da177e4SLinus Torvalds in6_ifa_put(ifp);
30558805d13fSEric Dumazet addrconf_verify_rtnl(net);
30561da177e4SLinus Torvalds return 0;
305719b1518cSDavid Ahern } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
305819b1518cSDavid Ahern ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
305919b1518cSDavid Ahern cfg->pfx, ifindex);
30601da177e4SLinus Torvalds }
30611da177e4SLinus Torvalds
30621da177e4SLinus Torvalds return PTR_ERR(ifp);
30631da177e4SLinus Torvalds }
30641da177e4SLinus Torvalds
inet6_addr_del(struct net * net,int ifindex,u32 ifa_flags,const struct in6_addr * pfx,unsigned int plen,struct netlink_ext_ack * extack)30656046d5b4SHeiner Kallweit static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
30667f6c4039SHangbin Liu const struct in6_addr *pfx, unsigned int plen,
30677f6c4039SHangbin Liu struct netlink_ext_ack *extack)
30681da177e4SLinus Torvalds {
30691da177e4SLinus Torvalds struct inet6_ifaddr *ifp;
30701da177e4SLinus Torvalds struct inet6_dev *idev;
30711da177e4SLinus Torvalds struct net_device *dev;
30721da177e4SLinus Torvalds
30737f6c4039SHangbin Liu if (plen > 128) {
30747f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length");
307524ef0da7SThomas Graf return -EINVAL;
30767f6c4039SHangbin Liu }
307724ef0da7SThomas Graf
3078af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex);
30797f6c4039SHangbin Liu if (!dev) {
30807f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface");
30811da177e4SLinus Torvalds return -ENODEV;
30827f6c4039SHangbin Liu }
30831da177e4SLinus Torvalds
3084e5d08d71SIan Morris idev = __in6_dev_get(dev);
30857f6c4039SHangbin Liu if (!idev) {
30867f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
30871da177e4SLinus Torvalds return -ENXIO;
30887f6c4039SHangbin Liu }
30891da177e4SLinus Torvalds
30901da177e4SLinus Torvalds read_lock_bh(&idev->lock);
3091502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) {
30921da177e4SLinus Torvalds if (ifp->prefix_len == plen &&
30931da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) {
30941da177e4SLinus Torvalds in6_ifa_hold(ifp);
30951da177e4SLinus Torvalds read_unlock_bh(&idev->lock);
30961da177e4SLinus Torvalds
30971da177e4SLinus Torvalds ipv6_del_addr(ifp);
3098*836deb96SHangbin Liu
3099*836deb96SHangbin Liu if (!(ifp->flags & IFA_F_TEMPORARY) &&
3100*836deb96SHangbin Liu (ifp->flags & IFA_F_MANAGETEMPADDR))
3101*836deb96SHangbin Liu delete_tempaddrs(idev, ifp);
3102*836deb96SHangbin Liu
31038805d13fSEric Dumazet addrconf_verify_rtnl(net);
310493a714d6SMadhu Challa if (ipv6_addr_is_multicast(pfx)) {
310593a714d6SMadhu Challa ipv6_mc_config(net->ipv6.mc_autojoin_sk,
310693a714d6SMadhu Challa false, pfx, dev->ifindex);
310793a714d6SMadhu Challa }
31081da177e4SLinus Torvalds return 0;
31091da177e4SLinus Torvalds }
31101da177e4SLinus Torvalds }
31111da177e4SLinus Torvalds read_unlock_bh(&idev->lock);
31127f6c4039SHangbin Liu
31137f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "address not found");
31141da177e4SLinus Torvalds return -EADDRNOTAVAIL;
31151da177e4SLinus Torvalds }
31161da177e4SLinus Torvalds
31171da177e4SLinus Torvalds
addrconf_add_ifaddr(struct net * net,void __user * arg)3118af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
31191da177e4SLinus Torvalds {
312019b1518cSDavid Ahern struct ifa6_config cfg = {
312119b1518cSDavid Ahern .ifa_flags = IFA_F_PERMANENT,
312219b1518cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME,
312319b1518cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME,
312419b1518cSDavid Ahern };
31251da177e4SLinus Torvalds struct in6_ifreq ireq;
31261da177e4SLinus Torvalds int err;
31271da177e4SLinus Torvalds
3128af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
31291da177e4SLinus Torvalds return -EPERM;
31301da177e4SLinus Torvalds
31311da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
31321da177e4SLinus Torvalds return -EFAULT;
31331da177e4SLinus Torvalds
313419b1518cSDavid Ahern cfg.pfx = &ireq.ifr6_addr;
313519b1518cSDavid Ahern cfg.plen = ireq.ifr6_prefixlen;
313619b1518cSDavid Ahern
31371da177e4SLinus Torvalds rtnl_lock();
313819b1518cSDavid Ahern err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
31391da177e4SLinus Torvalds rtnl_unlock();
31401da177e4SLinus Torvalds return err;
31411da177e4SLinus Torvalds }
31421da177e4SLinus Torvalds
addrconf_del_ifaddr(struct net * net,void __user * arg)3143af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
31441da177e4SLinus Torvalds {
31451da177e4SLinus Torvalds struct in6_ifreq ireq;
31461da177e4SLinus Torvalds int err;
31471da177e4SLinus Torvalds
3148af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
31491da177e4SLinus Torvalds return -EPERM;
31501da177e4SLinus Torvalds
31511da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
31521da177e4SLinus Torvalds return -EFAULT;
31531da177e4SLinus Torvalds
31541da177e4SLinus Torvalds rtnl_lock();
31556046d5b4SHeiner Kallweit err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
31567f6c4039SHangbin Liu ireq.ifr6_prefixlen, NULL);
31571da177e4SLinus Torvalds rtnl_unlock();
31581da177e4SLinus Torvalds return err;
31591da177e4SLinus Torvalds }
31601da177e4SLinus Torvalds
add_addr(struct inet6_dev * idev,const struct in6_addr * addr,int plen,int scope,u8 proto)3161b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
316247f0bd50SJacques de Laval int plen, int scope, u8 proto)
3163b5f348e5SIlpo Järvinen {
3164b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp;
3165e6464b8cSDavid Ahern struct ifa6_config cfg = {
3166e6464b8cSDavid Ahern .pfx = addr,
3167e6464b8cSDavid Ahern .plen = plen,
3168e6464b8cSDavid Ahern .ifa_flags = IFA_F_PERMANENT,
3169e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME,
3170e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME,
317147f0bd50SJacques de Laval .scope = scope,
317247f0bd50SJacques de Laval .ifa_proto = proto
3173e6464b8cSDavid Ahern };
3174b5f348e5SIlpo Järvinen
3175e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3176b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) {
3177b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock);
3178b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE;
3179b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock);
3180764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(idev->dev));
3181b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp);
3182b5f348e5SIlpo Järvinen in6_ifa_put(ifp);
3183b5f348e5SIlpo Järvinen }
3184b5f348e5SIlpo Järvinen }
3185b5f348e5SIlpo Järvinen
3186e5dd7294SAntonio Quartulli #if IS_ENABLED(CONFIG_IPV6_SIT) || IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
add_v4_addrs(struct inet6_dev * idev)3187e5dd7294SAntonio Quartulli static void add_v4_addrs(struct inet6_dev *idev)
31881da177e4SLinus Torvalds {
31891da177e4SLinus Torvalds struct in6_addr addr;
31901da177e4SLinus Torvalds struct net_device *dev;
3191c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev);
3192e5dd7294SAntonio Quartulli int scope, plen, offset = 0;
3193f0e2acfaSNicolas Dichtel u32 pflags = 0;
31941da177e4SLinus Torvalds
31951da177e4SLinus Torvalds ASSERT_RTNL();
31961da177e4SLinus Torvalds
31971da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr));
3198e5dd7294SAntonio Quartulli /* in case of IP6GRE the dev_addr is an IPv6 and therefore we use only the last 4 bytes */
3199e5dd7294SAntonio Quartulli if (idev->dev->addr_len == sizeof(struct in6_addr))
3200e5dd7294SAntonio Quartulli offset = sizeof(struct in6_addr) - 4;
3201e5dd7294SAntonio Quartulli memcpy(&addr.s6_addr32[3], idev->dev->dev_addr + offset, 4);
32021da177e4SLinus Torvalds
320330e2291fSThomas Winter if (!(idev->dev->flags & IFF_POINTOPOINT) && idev->dev->type == ARPHRD_SIT) {
320430e2291fSThomas Winter scope = IPV6_ADDR_COMPATv4;
320530e2291fSThomas Winter plen = 96;
320630e2291fSThomas Winter pflags |= RTF_NONEXTHOP;
320730e2291fSThomas Winter } else {
320861e18ce7SStephen Suryaputra if (idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_NONE)
320961e18ce7SStephen Suryaputra return;
321061e18ce7SStephen Suryaputra
32111da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000);
32121da177e4SLinus Torvalds scope = IFA_LINK;
3213929c9cf3SNicolas Dichtel plen = 64;
32141da177e4SLinus Torvalds }
32151da177e4SLinus Torvalds
32161da177e4SLinus Torvalds if (addr.s6_addr32[3]) {
321747f0bd50SJacques de Laval add_addr(idev, &addr, plen, scope, IFAPROT_UNSPEC);
32188308f3ffSDavid Ahern addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
3219e500c6d3SCong Wang GFP_KERNEL);
32201da177e4SLinus Torvalds return;
32211da177e4SLinus Torvalds }
32221da177e4SLinus Torvalds
32236fda7350SBenjamin Thery for_each_netdev(net, dev) {
3224e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev);
32251da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) {
32261da177e4SLinus Torvalds struct in_ifaddr *ifa;
32271da177e4SLinus Torvalds int flag = scope;
32281da177e4SLinus Torvalds
3229cd5a411dSFlorian Westphal in_dev_for_each_ifa_rtnl(ifa, in_dev) {
32301da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local;
32311da177e4SLinus Torvalds
32321da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK)
32331da177e4SLinus Torvalds continue;
32341da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) {
32351da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT)
32361da177e4SLinus Torvalds continue;
32371da177e4SLinus Torvalds flag |= IFA_HOST;
32381da177e4SLinus Torvalds }
32391da177e4SLinus Torvalds
324047f0bd50SJacques de Laval add_addr(idev, &addr, plen, flag,
324147f0bd50SJacques de Laval IFAPROT_UNSPEC);
32428308f3ffSDavid Ahern addrconf_prefix_route(&addr, plen, 0, idev->dev,
3243e500c6d3SCong Wang 0, pflags, GFP_KERNEL);
32441da177e4SLinus Torvalds }
32451da177e4SLinus Torvalds }
32461da177e4SLinus Torvalds }
32471da177e4SLinus Torvalds }
32480be669bbSJoerg Roedel #endif
32491da177e4SLinus Torvalds
init_loopback(struct net_device * dev)32501da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
32511da177e4SLinus Torvalds {
32521da177e4SLinus Torvalds struct inet6_dev *idev;
32531da177e4SLinus Torvalds
32541da177e4SLinus Torvalds /* ::1 */
32551da177e4SLinus Torvalds
32561da177e4SLinus Torvalds ASSERT_RTNL();
32571da177e4SLinus Torvalds
3258e5d08d71SIan Morris idev = ipv6_find_idev(dev);
3259db0b99f5SSabrina Dubroca if (IS_ERR(idev)) {
326091df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__);
32611da177e4SLinus Torvalds return;
32621da177e4SLinus Torvalds }
32631da177e4SLinus Torvalds
326447f0bd50SJacques de Laval add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFAPROT_KERNEL_LO);
32651da177e4SLinus Torvalds }
32661da177e4SLinus Torvalds
addrconf_add_linklocal(struct inet6_dev * idev,const struct in6_addr * addr,u32 flags)32672ad3ed59SAlexander Aring void addrconf_add_linklocal(struct inet6_dev *idev,
326864236f3fSHannes Frederic Sowa const struct in6_addr *addr, u32 flags)
32691da177e4SLinus Torvalds {
3270e6464b8cSDavid Ahern struct ifa6_config cfg = {
3271e6464b8cSDavid Ahern .pfx = addr,
3272e6464b8cSDavid Ahern .plen = 64,
3273e6464b8cSDavid Ahern .ifa_flags = flags | IFA_F_PERMANENT,
3274e6464b8cSDavid Ahern .valid_lft = INFINITY_LIFE_TIME,
3275e6464b8cSDavid Ahern .preferred_lft = INFINITY_LIFE_TIME,
327647f0bd50SJacques de Laval .scope = IFA_LINK,
327747f0bd50SJacques de Laval .ifa_proto = IFAPROT_KERNEL_LL
3278e6464b8cSDavid Ahern };
32791da177e4SLinus Torvalds struct inet6_ifaddr *ifp;
32801da177e4SLinus Torvalds
328195c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
328235e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
328335e015e1SMatteo Croce idev->cnf.optimistic_dad) &&
3284702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3285e6464b8cSDavid Ahern cfg.ifa_flags |= IFA_F_OPTIMISTIC;
328695c385b4SNeil Horman #endif
328795c385b4SNeil Horman
3288e6464b8cSDavid Ahern ifp = ipv6_add_addr(idev, &cfg, true, NULL);
32891da177e4SLinus Torvalds if (!IS_ERR(ifp)) {
32908308f3ffSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
3291acb54e3cSDavid Ahern 0, 0, GFP_ATOMIC);
3292cf22f9a2SDavid S. Miller addrconf_dad_start(ifp);
32931da177e4SLinus Torvalds in6_ifa_put(ifp);
32941da177e4SLinus Torvalds }
32951da177e4SLinus Torvalds }
32962ad3ed59SAlexander Aring EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
32971da177e4SLinus Torvalds
ipv6_reserved_interfaceid(struct in6_addr address)3298622c81d5SHannes Frederic Sowa static bool ipv6_reserved_interfaceid(struct in6_addr address)
3299622c81d5SHannes Frederic Sowa {
3300622c81d5SHannes Frederic Sowa if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3301622c81d5SHannes Frederic Sowa return true;
3302622c81d5SHannes Frederic Sowa
3303622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0x02005eff) &&
3304622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3305622c81d5SHannes Frederic Sowa return true;
3306622c81d5SHannes Frederic Sowa
3307622c81d5SHannes Frederic Sowa if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3308622c81d5SHannes Frederic Sowa ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3309622c81d5SHannes Frederic Sowa return true;
3310622c81d5SHannes Frederic Sowa
3311622c81d5SHannes Frederic Sowa return false;
3312622c81d5SHannes Frederic Sowa }
3313622c81d5SHannes Frederic Sowa
ipv6_generate_stable_address(struct in6_addr * address,u8 dad_count,const struct inet6_dev * idev)3314622c81d5SHannes Frederic Sowa static int ipv6_generate_stable_address(struct in6_addr *address,
3315622c81d5SHannes Frederic Sowa u8 dad_count,
3316622c81d5SHannes Frederic Sowa const struct inet6_dev *idev)
3317622c81d5SHannes Frederic Sowa {
3318622c81d5SHannes Frederic Sowa static DEFINE_SPINLOCK(lock);
33196b0b0fa2SEric Biggers static __u32 digest[SHA1_DIGEST_WORDS];
33206b0b0fa2SEric Biggers static __u32 workspace[SHA1_WORKSPACE_WORDS];
3321622c81d5SHannes Frederic Sowa
3322622c81d5SHannes Frederic Sowa static union {
33236b0b0fa2SEric Biggers char __data[SHA1_BLOCK_SIZE];
3324622c81d5SHannes Frederic Sowa struct {
3325622c81d5SHannes Frederic Sowa struct in6_addr secret;
3326ff40217eSHannes Frederic Sowa __be32 prefix[2];
3327622c81d5SHannes Frederic Sowa unsigned char hwaddr[MAX_ADDR_LEN];
3328622c81d5SHannes Frederic Sowa u8 dad_count;
3329622c81d5SHannes Frederic Sowa } __packed;
3330622c81d5SHannes Frederic Sowa } data;
3331622c81d5SHannes Frederic Sowa
3332622c81d5SHannes Frederic Sowa struct in6_addr secret;
3333622c81d5SHannes Frederic Sowa struct in6_addr temp;
3334622c81d5SHannes Frederic Sowa struct net *net = dev_net(idev->dev);
3335622c81d5SHannes Frederic Sowa
3336622c81d5SHannes Frederic Sowa BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3337622c81d5SHannes Frederic Sowa
3338622c81d5SHannes Frederic Sowa if (idev->cnf.stable_secret.initialized)
3339622c81d5SHannes Frederic Sowa secret = idev->cnf.stable_secret.secret;
3340622c81d5SHannes Frederic Sowa else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3341622c81d5SHannes Frederic Sowa secret = net->ipv6.devconf_dflt->stable_secret.secret;
3342622c81d5SHannes Frederic Sowa else
3343622c81d5SHannes Frederic Sowa return -1;
3344622c81d5SHannes Frederic Sowa
3345622c81d5SHannes Frederic Sowa retry:
3346622c81d5SHannes Frederic Sowa spin_lock_bh(&lock);
3347622c81d5SHannes Frederic Sowa
33486b0b0fa2SEric Biggers sha1_init(digest);
3349622c81d5SHannes Frederic Sowa memset(&data, 0, sizeof(data));
3350622c81d5SHannes Frederic Sowa memset(workspace, 0, sizeof(workspace));
3351622c81d5SHannes Frederic Sowa memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3352ff40217eSHannes Frederic Sowa data.prefix[0] = address->s6_addr32[0];
3353ff40217eSHannes Frederic Sowa data.prefix[1] = address->s6_addr32[1];
3354622c81d5SHannes Frederic Sowa data.secret = secret;
3355622c81d5SHannes Frederic Sowa data.dad_count = dad_count;
3356622c81d5SHannes Frederic Sowa
33576b0b0fa2SEric Biggers sha1_transform(digest, data.__data, workspace);
3358622c81d5SHannes Frederic Sowa
3359622c81d5SHannes Frederic Sowa temp = *address;
3360ff40217eSHannes Frederic Sowa temp.s6_addr32[2] = (__force __be32)digest[0];
3361ff40217eSHannes Frederic Sowa temp.s6_addr32[3] = (__force __be32)digest[1];
3362622c81d5SHannes Frederic Sowa
3363622c81d5SHannes Frederic Sowa spin_unlock_bh(&lock);
3364622c81d5SHannes Frederic Sowa
3365622c81d5SHannes Frederic Sowa if (ipv6_reserved_interfaceid(temp)) {
3366622c81d5SHannes Frederic Sowa dad_count++;
33671855b7c3SHannes Frederic Sowa if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3368622c81d5SHannes Frederic Sowa return -1;
3369622c81d5SHannes Frederic Sowa goto retry;
3370622c81d5SHannes Frederic Sowa }
3371622c81d5SHannes Frederic Sowa
3372622c81d5SHannes Frederic Sowa *address = temp;
3373622c81d5SHannes Frederic Sowa return 0;
3374622c81d5SHannes Frederic Sowa }
3375622c81d5SHannes Frederic Sowa
ipv6_gen_mode_random_init(struct inet6_dev * idev)3376cc9da6ccSBjørn Mork static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3377cc9da6ccSBjørn Mork {
3378cc9da6ccSBjørn Mork struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3379cc9da6ccSBjørn Mork
3380cc9da6ccSBjørn Mork if (s->initialized)
3381cc9da6ccSBjørn Mork return;
3382cc9da6ccSBjørn Mork s = &idev->cnf.stable_secret;
3383cc9da6ccSBjørn Mork get_random_bytes(&s->secret, sizeof(s->secret));
3384cc9da6ccSBjørn Mork s->initialized = true;
3385cc9da6ccSBjørn Mork }
3386cc9da6ccSBjørn Mork
addrconf_addr_gen(struct inet6_dev * idev,bool prefix_route)3387bc91b0f0SJiri Pirko static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3388bc91b0f0SJiri Pirko {
3389bc91b0f0SJiri Pirko struct in6_addr addr;
3390bc91b0f0SJiri Pirko
3391ca254490SDavid Ahern /* no link local addresses on L3 master devices */
3392ca254490SDavid Ahern if (netif_is_l3_master(idev->dev))
3393ca254490SDavid Ahern return;
3394ca254490SDavid Ahern
3395744fdc82SJarod Wilson /* no link local addresses on devices flagged as slaves */
33968a321cf7SXin Long if (idev->dev->priv_flags & IFF_NO_ADDRCONF)
3397744fdc82SJarod Wilson return;
3398744fdc82SJarod Wilson
3399bc91b0f0SJiri Pirko ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3400622c81d5SHannes Frederic Sowa
3401d35a00b8SFelix Jia switch (idev->cnf.addr_gen_mode) {
3402cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_RANDOM:
3403cc9da6ccSBjørn Mork ipv6_gen_mode_random_init(idev);
3404a8eceea8SJoe Perches fallthrough;
3405cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3406622c81d5SHannes Frederic Sowa if (!ipv6_generate_stable_address(&addr, 0, idev))
340764236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr,
340864236f3fSHannes Frederic Sowa IFA_F_STABLE_PRIVACY);
3409622c81d5SHannes Frederic Sowa else if (prefix_route)
34108308f3ffSDavid Ahern addrconf_prefix_route(&addr, 64, 0, idev->dev,
3411acb54e3cSDavid Ahern 0, 0, GFP_KERNEL);
3412cc9da6ccSBjørn Mork break;
3413cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_EUI64:
3414bc91b0f0SJiri Pirko /* addrconf_add_linklocal also adds a prefix_route and we
3415bc91b0f0SJiri Pirko * only need to care about prefix routes if ipv6_generate_eui64
3416bc91b0f0SJiri Pirko * couldn't generate one.
3417bc91b0f0SJiri Pirko */
3418bc91b0f0SJiri Pirko if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
341964236f3fSHannes Frederic Sowa addrconf_add_linklocal(idev, &addr, 0);
3420bc91b0f0SJiri Pirko else if (prefix_route)
34218308f3ffSDavid Ahern addrconf_prefix_route(&addr, 64, 0, idev->dev,
342227b10608SDavid Ahern 0, 0, GFP_KERNEL);
3423cc9da6ccSBjørn Mork break;
3424cc9da6ccSBjørn Mork case IN6_ADDR_GEN_MODE_NONE:
3425cc9da6ccSBjørn Mork default:
3426cc9da6ccSBjørn Mork /* will not add any link local address */
3427cc9da6ccSBjørn Mork break;
3428bc91b0f0SJiri Pirko }
3429bc91b0f0SJiri Pirko }
3430bc91b0f0SJiri Pirko
addrconf_dev_config(struct net_device * dev)34311da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
34321da177e4SLinus Torvalds {
34331da177e4SLinus Torvalds struct inet6_dev *idev;
34341da177e4SLinus Torvalds
34351da177e4SLinus Torvalds ASSERT_RTNL();
34361da177e4SLinus Torvalds
343774235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) &&
343874235a25SHerbert Xu (dev->type != ARPHRD_FDDI) &&
343974235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) &&
344006a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) &&
3441e837735eSNicolas Dichtel (dev->type != ARPHRD_IEEE1394) &&
3442e74bccb8SJukka Rissanen (dev->type != ARPHRD_TUNNEL6) &&
3443cc9da6ccSBjørn Mork (dev->type != ARPHRD_6LOWPAN) &&
344445ce0fd1SFelix Jia (dev->type != ARPHRD_TUNNEL) &&
34459deb441cSSubash Abhinov Kasiviswanathan (dev->type != ARPHRD_NONE) &&
34469deb441cSSubash Abhinov Kasiviswanathan (dev->type != ARPHRD_RAWIP)) {
344774235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */
344860380488SHangbin Liu idev = __in6_dev_get(dev);
344960380488SHangbin Liu if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP &&
345060380488SHangbin Liu dev->flags & IFF_MULTICAST)
345160380488SHangbin Liu ipv6_mc_up(idev);
345274235a25SHerbert Xu return;
345374235a25SHerbert Xu }
345474235a25SHerbert Xu
34551da177e4SLinus Torvalds idev = addrconf_add_dev(dev);
345664e724f6SBrian Haley if (IS_ERR(idev))
34571da177e4SLinus Torvalds return;
34581da177e4SLinus Torvalds
3459cc9da6ccSBjørn Mork /* this device type has no EUI support */
3460cc9da6ccSBjørn Mork if (dev->type == ARPHRD_NONE &&
3461d35a00b8SFelix Jia idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3462d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3463cc9da6ccSBjørn Mork
3464bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false);
34651da177e4SLinus Torvalds }
34661da177e4SLinus Torvalds
346707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
addrconf_sit_config(struct net_device * dev)34681da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
34691da177e4SLinus Torvalds {
34701da177e4SLinus Torvalds struct inet6_dev *idev;
34711da177e4SLinus Torvalds
34721da177e4SLinus Torvalds ASSERT_RTNL();
34731da177e4SLinus Torvalds
34741da177e4SLinus Torvalds /*
34751da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4
34761da177e4SLinus Torvalds * addresses... we should configure all of
34771da177e4SLinus Torvalds * our v4 addrs in the tunnel
34781da177e4SLinus Torvalds */
34791da177e4SLinus Torvalds
3480e5d08d71SIan Morris idev = ipv6_find_idev(dev);
3481db0b99f5SSabrina Dubroca if (IS_ERR(idev)) {
348291df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__);
34831da177e4SLinus Torvalds return;
34841da177e4SLinus Torvalds }
34851da177e4SLinus Torvalds
3486c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) {
3487bc91b0f0SJiri Pirko addrconf_addr_gen(idev, false);
3488c7dc89c0SFred L. Templin return;
3489c7dc89c0SFred L. Templin }
3490c7dc89c0SFred L. Templin
3491e5dd7294SAntonio Quartulli add_v4_addrs(idev);
34921da177e4SLinus Torvalds
349362b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT)
34941da177e4SLinus Torvalds addrconf_add_mroute(dev);
34951da177e4SLinus Torvalds }
34960be669bbSJoerg Roedel #endif
34971da177e4SLinus Torvalds
3498e5dd7294SAntonio Quartulli #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
addrconf_gre_config(struct net_device * dev)3499aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
3500aee80b54Sstephen hemminger {
3501aee80b54Sstephen hemminger struct inet6_dev *idev;
3502aee80b54Sstephen hemminger
3503aee80b54Sstephen hemminger ASSERT_RTNL();
3504aee80b54Sstephen hemminger
3505e5d08d71SIan Morris idev = ipv6_find_idev(dev);
3506db0b99f5SSabrina Dubroca if (IS_ERR(idev)) {
350791df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__);
3508aee80b54Sstephen hemminger return;
3509aee80b54Sstephen hemminger }
3510aee80b54Sstephen hemminger
3511e5dd7294SAntonio Quartulli if (dev->type == ARPHRD_ETHER) {
3512bc91b0f0SJiri Pirko addrconf_addr_gen(idev, true);
3513e5dd7294SAntonio Quartulli return;
3514e5dd7294SAntonio Quartulli }
3515e5dd7294SAntonio Quartulli
3516e5dd7294SAntonio Quartulli add_v4_addrs(idev);
3517e5dd7294SAntonio Quartulli
3518d9e4ce65SHannes Frederic Sowa if (dev->flags & IFF_POINTOPOINT)
3519d9e4ce65SHannes Frederic Sowa addrconf_add_mroute(dev);
3520aee80b54Sstephen hemminger }
3521aee80b54Sstephen hemminger #endif
3522aee80b54Sstephen hemminger
addrconf_init_auto_addrs(struct net_device * dev)352323ca0c2cSThomas Winter static void addrconf_init_auto_addrs(struct net_device *dev)
352423ca0c2cSThomas Winter {
352523ca0c2cSThomas Winter switch (dev->type) {
352623ca0c2cSThomas Winter #if IS_ENABLED(CONFIG_IPV6_SIT)
352723ca0c2cSThomas Winter case ARPHRD_SIT:
352823ca0c2cSThomas Winter addrconf_sit_config(dev);
352923ca0c2cSThomas Winter break;
353023ca0c2cSThomas Winter #endif
353123ca0c2cSThomas Winter #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
353223ca0c2cSThomas Winter case ARPHRD_IP6GRE:
353323ca0c2cSThomas Winter case ARPHRD_IPGRE:
353423ca0c2cSThomas Winter addrconf_gre_config(dev);
353523ca0c2cSThomas Winter break;
353623ca0c2cSThomas Winter #endif
353723ca0c2cSThomas Winter case ARPHRD_LOOPBACK:
353823ca0c2cSThomas Winter init_loopback(dev);
353923ca0c2cSThomas Winter break;
354023ca0c2cSThomas Winter
354123ca0c2cSThomas Winter default:
354223ca0c2cSThomas Winter addrconf_dev_config(dev);
354323ca0c2cSThomas Winter break;
354423ca0c2cSThomas Winter }
354523ca0c2cSThomas Winter }
354623ca0c2cSThomas Winter
fixup_permanent_addr(struct net * net,struct inet6_dev * idev,struct inet6_ifaddr * ifp)3547afb1d4b5SDavid Ahern static int fixup_permanent_addr(struct net *net,
3548afb1d4b5SDavid Ahern struct inet6_dev *idev,
3549f1705ec1SDavid Ahern struct inet6_ifaddr *ifp)
3550f1705ec1SDavid Ahern {
355193c2fb25SDavid Ahern /* !fib6_node means the host route was removed from the
35528048ced9SDavid Ahern * FIB, for example, if 'lo' device is taken down. In that
35538048ced9SDavid Ahern * case regenerate the host route.
35548048ced9SDavid Ahern */
355593c2fb25SDavid Ahern if (!ifp->rt || !ifp->rt->fib6_node) {
3556360a9887SDavid Ahern struct fib6_info *f6i, *prev;
3557f1705ec1SDavid Ahern
3558360a9887SDavid Ahern f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
35597f6c4039SHangbin Liu GFP_ATOMIC, NULL);
3560360a9887SDavid Ahern if (IS_ERR(f6i))
3561360a9887SDavid Ahern return PTR_ERR(f6i);
3562f1705ec1SDavid Ahern
35638048ced9SDavid Ahern /* ifp->rt can be accessed outside of rtnl */
35648048ced9SDavid Ahern spin_lock(&ifp->lock);
35658048ced9SDavid Ahern prev = ifp->rt;
3566360a9887SDavid Ahern ifp->rt = f6i;
35678048ced9SDavid Ahern spin_unlock(&ifp->lock);
35688048ced9SDavid Ahern
356993531c67SDavid Ahern fib6_info_release(prev);
3570f1705ec1SDavid Ahern }
3571f1705ec1SDavid Ahern
3572f1705ec1SDavid Ahern if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3573f1705ec1SDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
35748308f3ffSDavid Ahern ifp->rt_priority, idev->dev, 0, 0,
35758308f3ffSDavid Ahern GFP_ATOMIC);
3576f1705ec1SDavid Ahern }
3577f1705ec1SDavid Ahern
35786d717134SDavid Ahern if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3579f1705ec1SDavid Ahern addrconf_dad_start(ifp);
3580f1705ec1SDavid Ahern
3581f1705ec1SDavid Ahern return 0;
3582f1705ec1SDavid Ahern }
3583f1705ec1SDavid Ahern
addrconf_permanent_addr(struct net * net,struct net_device * dev)3584afb1d4b5SDavid Ahern static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
3585f1705ec1SDavid Ahern {
3586f1705ec1SDavid Ahern struct inet6_ifaddr *ifp, *tmp;
3587f1705ec1SDavid Ahern struct inet6_dev *idev;
3588f1705ec1SDavid Ahern
3589f1705ec1SDavid Ahern idev = __in6_dev_get(dev);
3590f1705ec1SDavid Ahern if (!idev)
3591f1705ec1SDavid Ahern return;
3592f1705ec1SDavid Ahern
3593f1705ec1SDavid Ahern write_lock_bh(&idev->lock);
3594f1705ec1SDavid Ahern
3595f1705ec1SDavid Ahern list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3596f1705ec1SDavid Ahern if ((ifp->flags & IFA_F_PERMANENT) &&
3597afb1d4b5SDavid Ahern fixup_permanent_addr(net, idev, ifp) < 0) {
3598f1705ec1SDavid Ahern write_unlock_bh(&idev->lock);
3599e669b869SEric Dumazet in6_ifa_hold(ifp);
3600f1705ec1SDavid Ahern ipv6_del_addr(ifp);
3601f1705ec1SDavid Ahern write_lock_bh(&idev->lock);
3602f1705ec1SDavid Ahern
3603f1705ec1SDavid Ahern net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3604f1705ec1SDavid Ahern idev->dev->name, &ifp->addr);
3605f1705ec1SDavid Ahern }
3606f1705ec1SDavid Ahern }
3607f1705ec1SDavid Ahern
3608f1705ec1SDavid Ahern write_unlock_bh(&idev->lock);
3609f1705ec1SDavid Ahern }
3610f1705ec1SDavid Ahern
addrconf_notify(struct notifier_block * this,unsigned long event,void * ptr)36111da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
3612351638e7SJiri Pirko void *ptr)
36131da177e4SLinus Torvalds {
3614351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3615896585d4SHangbin Liu struct netdev_notifier_change_info *change_info;
36164f7f34eaSDavid Ahern struct netdev_notifier_changeupper_info *info;
3617748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev);
361860abc0beSWANG Cong struct net *net = dev_net(dev);
3619c5e33bddSYOSHIFUJI Hideaki int run_pending = 0;
3620b217d616SHerbert Xu int err;
36211da177e4SLinus Torvalds
36221da177e4SLinus Torvalds switch (event) {
362345ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER:
362474235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) {
362545ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev);
3626a317a2f1SWANG Cong if (IS_ERR(idev))
3627a317a2f1SWANG Cong return notifier_from_errno(PTR_ERR(idev));
362845ba9dd2SYOSHIFUJI Hideaki }
362945ba9dd2SYOSHIFUJI Hideaki break;
3630bcdd553fSStephen Hemminger
3631b7b0b1d2SAlexander Duyck case NETDEV_CHANGEMTU:
3632b7b0b1d2SAlexander Duyck /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3633b7b0b1d2SAlexander Duyck if (dev->mtu < IPV6_MIN_MTU) {
363460abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev);
3635b7b0b1d2SAlexander Duyck break;
3636b7b0b1d2SAlexander Duyck }
3637b7b0b1d2SAlexander Duyck
3638b7b0b1d2SAlexander Duyck if (idev) {
3639b7b0b1d2SAlexander Duyck rt6_mtu_change(dev, dev->mtu);
3640b7b0b1d2SAlexander Duyck idev->cnf.mtu6 = dev->mtu;
3641b7b0b1d2SAlexander Duyck break;
3642b7b0b1d2SAlexander Duyck }
3643b7b0b1d2SAlexander Duyck
3644b7b0b1d2SAlexander Duyck /* allocate new idev */
3645b7b0b1d2SAlexander Duyck idev = ipv6_add_dev(dev);
3646b7b0b1d2SAlexander Duyck if (IS_ERR(idev))
3647b7b0b1d2SAlexander Duyck break;
3648b7b0b1d2SAlexander Duyck
3649b7b0b1d2SAlexander Duyck /* device is still not ready */
3650b7b0b1d2SAlexander Duyck if (!(idev->if_flags & IF_READY))
3651b7b0b1d2SAlexander Duyck break;
3652b7b0b1d2SAlexander Duyck
3653b7b0b1d2SAlexander Duyck run_pending = 1;
3654a8eceea8SJoe Perches fallthrough;
36551da177e4SLinus Torvalds case NETDEV_UP:
36563c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE:
36573ce62a84SWANG Cong if (idev && idev->cnf.disable_ipv6)
36583ce62a84SWANG Cong break;
36593ce62a84SWANG Cong
36608a321cf7SXin Long if (dev->priv_flags & IFF_NO_ADDRCONF) {
3661fd16eb94SHangbin Liu if (event == NETDEV_UP && !IS_ERR_OR_NULL(idev) &&
3662fd16eb94SHangbin Liu dev->flags & IFF_UP && dev->flags & IFF_MULTICAST)
3663fd16eb94SHangbin Liu ipv6_mc_up(idev);
3664fd16eb94SHangbin Liu break;
3665fd16eb94SHangbin Liu }
3666fd16eb94SHangbin Liu
36673c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) {
366838bd10c4SDavid Ahern /* restore routes for permanent addresses */
3669afb1d4b5SDavid Ahern addrconf_permanent_addr(net, dev);
367038bd10c4SDavid Ahern
36711f372c7bSMike Manning if (!addrconf_link_ready(dev)) {
36723c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */
36737c62b8ddSLubomir Rintel pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
36743c21edbdSYOSHIFUJI Hideaki dev->name);
36753c21edbdSYOSHIFUJI Hideaki break;
36763c21edbdSYOSHIFUJI Hideaki }
367799081049SKristian Slavov
3678d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU)
3679d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev);
3680d31c7b8fSEvgeniy Polyakov
3681a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) {
368299081049SKristian Slavov idev->if_flags |= IF_READY;
3683e3ec6cfcSBenjamin Thery run_pending = 1;
3684e3ec6cfcSBenjamin Thery }
3685b7b0b1d2SAlexander Duyck } else if (event == NETDEV_CHANGE) {
36861f372c7bSMike Manning if (!addrconf_link_ready(dev)) {
36873c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */
368827c6fa73SIdo Schimmel rt6_sync_down_dev(dev, event);
36893c21edbdSYOSHIFUJI Hideaki break;
36903c21edbdSYOSHIFUJI Hideaki }
36913c21edbdSYOSHIFUJI Hideaki
3692896585d4SHangbin Liu if (!IS_ERR_OR_NULL(idev)) {
3693a088d1d7SLinus Lüssing if (idev->if_flags & IF_READY) {
3694a088d1d7SLinus Lüssing /* device is already configured -
3695a088d1d7SLinus Lüssing * but resend MLD reports, we might
3696a088d1d7SLinus Lüssing * have roamed and need to update
3697a088d1d7SLinus Lüssing * multicast snooping switches
3698a088d1d7SLinus Lüssing */
3699a088d1d7SLinus Lüssing ipv6_mc_up(idev);
3700896585d4SHangbin Liu change_info = ptr;
3701896585d4SHangbin Liu if (change_info->flags_changed & IFF_NOARP)
3702896585d4SHangbin Liu addrconf_dad_run(idev, true);
370327c6fa73SIdo Schimmel rt6_sync_up(dev, RTNH_F_LINKDOWN);
37043c21edbdSYOSHIFUJI Hideaki break;
3705a088d1d7SLinus Lüssing }
37063c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY;
37073c21edbdSYOSHIFUJI Hideaki }
37083c21edbdSYOSHIFUJI Hideaki
3709f69de8aaSMatthieu Baerts pr_debug("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
37103c21edbdSYOSHIFUJI Hideaki dev->name);
37113c21edbdSYOSHIFUJI Hideaki
3712c5e33bddSYOSHIFUJI Hideaki run_pending = 1;
37133c21edbdSYOSHIFUJI Hideaki }
37143c21edbdSYOSHIFUJI Hideaki
371523ca0c2cSThomas Winter addrconf_init_auto_addrs(dev);
3716bcdd553fSStephen Hemminger
3717a317a2f1SWANG Cong if (!IS_ERR_OR_NULL(idev)) {
3718c5e33bddSYOSHIFUJI Hideaki if (run_pending)
3719896585d4SHangbin Liu addrconf_dad_run(idev, false);
3720c5e33bddSYOSHIFUJI Hideaki
37212127d95aSIdo Schimmel /* Device has an address by now */
37222127d95aSIdo Schimmel rt6_sync_up(dev, RTNH_F_DEAD);
37232127d95aSIdo Schimmel
3724bcdd553fSStephen Hemminger /*
3725bcdd553fSStephen Hemminger * If the MTU changed during the interface down,
3726bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be
3727bcdd553fSStephen Hemminger * reflected in the idev as well as routers.
37281da177e4SLinus Torvalds */
3729bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu &&
3730bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) {
37311da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu);
37321da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu;
37331da177e4SLinus Torvalds }
37341da177e4SLinus Torvalds idev->tstamp = jiffies;
37351da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev);
3736bcdd553fSStephen Hemminger
3737bcdd553fSStephen Hemminger /*
3738bcdd553fSStephen Hemminger * If the changed mtu during down is lower than
3739bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface.
37401da177e4SLinus Torvalds */
37411da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU)
374260abc0beSWANG Cong addrconf_ifdown(dev, dev != net->loopback_dev);
37431da177e4SLinus Torvalds }
37441da177e4SLinus Torvalds break;
37451da177e4SLinus Torvalds
37461da177e4SLinus Torvalds case NETDEV_DOWN:
37471da177e4SLinus Torvalds case NETDEV_UNREGISTER:
37481da177e4SLinus Torvalds /*
37491da177e4SLinus Torvalds * Remove all addresses from this interface.
37501da177e4SLinus Torvalds */
37511da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN);
37521da177e4SLinus Torvalds break;
37533c21edbdSYOSHIFUJI Hideaki
37541da177e4SLinus Torvalds case NETDEV_CHANGENAME:
37551da177e4SLinus Torvalds if (idev) {
37565632c515SStephen Hemminger snmp6_unregister_dev(idev);
3757408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev);
3758a317a2f1SWANG Cong err = addrconf_sysctl_register(idev);
3759b217d616SHerbert Xu if (err)
3760b217d616SHerbert Xu return notifier_from_errno(err);
3761a317a2f1SWANG Cong err = snmp6_register_dev(idev);
3762a317a2f1SWANG Cong if (err) {
3763a317a2f1SWANG Cong addrconf_sysctl_unregister(idev);
3764a317a2f1SWANG Cong return notifier_from_errno(err);
3765a317a2f1SWANG Cong }
37665632c515SStephen Hemminger }
37671da177e4SLinus Torvalds break;
3768bcdd553fSStephen Hemminger
376993d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE:
377093d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE:
37713ef0952cSAndrew Lunn if (idev)
377293d9b7d7SJiri Pirko addrconf_type_change(dev, event);
377375c78500SMoni Shoua break;
37744f7f34eaSDavid Ahern
37754f7f34eaSDavid Ahern case NETDEV_CHANGEUPPER:
37764f7f34eaSDavid Ahern info = ptr;
37774f7f34eaSDavid Ahern
37784f7f34eaSDavid Ahern /* flush all routes if dev is linked to or unlinked from
37794f7f34eaSDavid Ahern * an L3 master device (e.g., VRF)
37804f7f34eaSDavid Ahern */
37814f7f34eaSDavid Ahern if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3782ae79dbf6SFlorent Fourcot addrconf_ifdown(dev, false);
37833ff50b79SStephen Hemminger }
37841da177e4SLinus Torvalds
37851da177e4SLinus Torvalds return NOTIFY_OK;
37861da177e4SLinus Torvalds }
37871da177e4SLinus Torvalds
37881da177e4SLinus Torvalds /*
37891da177e4SLinus Torvalds * addrconf module should be notified of a device going up
37901da177e4SLinus Torvalds */
37911da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
37921da177e4SLinus Torvalds .notifier_call = addrconf_notify,
3793242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY,
37941da177e4SLinus Torvalds };
37951da177e4SLinus Torvalds
addrconf_type_change(struct net_device * dev,unsigned long event)379693d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
379775c78500SMoni Shoua {
379875c78500SMoni Shoua struct inet6_dev *idev;
379975c78500SMoni Shoua ASSERT_RTNL();
380075c78500SMoni Shoua
380175c78500SMoni Shoua idev = __in6_dev_get(dev);
380275c78500SMoni Shoua
380393d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE)
380475c78500SMoni Shoua ipv6_mc_remap(idev);
380593d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE)
380675c78500SMoni Shoua ipv6_mc_unmap(idev);
380775c78500SMoni Shoua }
380875c78500SMoni Shoua
addr_is_local(const struct in6_addr * addr)380970af921dSDavid Ahern static bool addr_is_local(const struct in6_addr *addr)
381070af921dSDavid Ahern {
381170af921dSDavid Ahern return ipv6_addr_type(addr) &
381270af921dSDavid Ahern (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
381370af921dSDavid Ahern }
381470af921dSDavid Ahern
addrconf_ifdown(struct net_device * dev,bool unregister)3815ae79dbf6SFlorent Fourcot static int addrconf_ifdown(struct net_device *dev, bool unregister)
38161da177e4SLinus Torvalds {
3817ae79dbf6SFlorent Fourcot unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN;
3818c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev);
3819502a2ffdSstephen hemminger struct inet6_dev *idev;
382051454ea4SNiels Dossche struct inet6_ifaddr *ifa;
382151454ea4SNiels Dossche LIST_HEAD(tmp_addr_list);
38220aef78aaSIvan Vecera bool keep_addr = false;
38239995b408Sj.nixdorf@avm.de bool was_ready;
382473a8bd74SDavid S. Miller int state, i;
38251da177e4SLinus Torvalds
38261da177e4SLinus Torvalds ASSERT_RTNL();
38271da177e4SLinus Torvalds
38284c981e28SIdo Schimmel rt6_disable_ip(dev, event);
38291da177e4SLinus Torvalds
38301da177e4SLinus Torvalds idev = __in6_dev_get(dev);
383163159f29SIan Morris if (!idev)
38321da177e4SLinus Torvalds return -ENODEV;
38331da177e4SLinus Torvalds
3834bcdd553fSStephen Hemminger /*
3835bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device.
3836bcdd553fSStephen Hemminger * Do not dev_put!
38371da177e4SLinus Torvalds */
3838ae79dbf6SFlorent Fourcot if (unregister) {
38391da177e4SLinus Torvalds idev->dead = 1;
38408814c4b5SYOSHIFUJI Hideaki
38418814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */
3842a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL);
38431da177e4SLinus Torvalds
38441da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */
38451da177e4SLinus Torvalds snmp6_unregister_dev(idev);
38461da177e4SLinus Torvalds
38471da177e4SLinus Torvalds }
38481da177e4SLinus Torvalds
3849f1705ec1SDavid Ahern /* combine the user config with event to determine if permanent
3850f1705ec1SDavid Ahern * addresses are to be removed from address hash table
3851f1705ec1SDavid Ahern */
3852ae79dbf6SFlorent Fourcot if (!unregister && !idev->cnf.disable_ipv6) {
38530aef78aaSIvan Vecera /* aggregate the system setting and interface setting */
38540aef78aaSIvan Vecera int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
38550aef78aaSIvan Vecera
38560aef78aaSIvan Vecera if (!_keep_addr)
38570aef78aaSIvan Vecera _keep_addr = idev->cnf.keep_addr_on_down;
38580aef78aaSIvan Vecera
38590aef78aaSIvan Vecera keep_addr = (_keep_addr > 0);
38600aef78aaSIvan Vecera }
3861f1705ec1SDavid Ahern
386273a8bd74SDavid S. Miller /* Step 2: clear hash table */
386373a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3864e66d1172SEric Dumazet struct hlist_head *h = &net->ipv6.inet6_addr_lst[i];
386573a8bd74SDavid S. Miller
3866e66d1172SEric Dumazet spin_lock_bh(&net->ipv6.addrconf_hash_lock);
386773a8bd74SDavid S. Miller restart:
3868b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) {
386973a8bd74SDavid S. Miller if (ifa->idev == idev) {
3870c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa);
3871f1705ec1SDavid Ahern /* combined flag + permanent flag decide if
3872f1705ec1SDavid Ahern * address is retained on a down event
3873f1705ec1SDavid Ahern */
3874f1705ec1SDavid Ahern if (!keep_addr ||
387570af921dSDavid Ahern !(ifa->flags & IFA_F_PERMANENT) ||
387670af921dSDavid Ahern addr_is_local(&ifa->addr)) {
3877f1705ec1SDavid Ahern hlist_del_init_rcu(&ifa->addr_lst);
387873a8bd74SDavid S. Miller goto restart;
387973a8bd74SDavid S. Miller }
388073a8bd74SDavid S. Miller }
3881f1705ec1SDavid Ahern }
3882e66d1172SEric Dumazet spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
388373a8bd74SDavid S. Miller }
388473a8bd74SDavid S. Miller
38851da177e4SLinus Torvalds write_lock_bh(&idev->lock);
38861da177e4SLinus Torvalds
3887b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev);
3888b7b1bfceSHannes Frederic Sowa
38899995b408Sj.nixdorf@avm.de /* Step 2: clear flags for stateless addrconf, repeated down
38909995b408Sj.nixdorf@avm.de * detection
38919995b408Sj.nixdorf@avm.de */
38929995b408Sj.nixdorf@avm.de was_ready = idev->if_flags & IF_READY;
3893ae79dbf6SFlorent Fourcot if (!unregister)
38943c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
38951da177e4SLinus Torvalds
3896bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */
3897372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) {
3898372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list,
3899372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list);
3900372e6c8fSstephen hemminger list_del(&ifa->tmp_list);
39011da177e4SLinus Torvalds write_unlock_bh(&idev->lock);
39021da177e4SLinus Torvalds spin_lock_bh(&ifa->lock);
39031da177e4SLinus Torvalds
39041da177e4SLinus Torvalds if (ifa->ifpub) {
39051da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub);
39061da177e4SLinus Torvalds ifa->ifpub = NULL;
39071da177e4SLinus Torvalds }
39081da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock);
39091da177e4SLinus Torvalds in6_ifa_put(ifa);
39101da177e4SLinus Torvalds write_lock_bh(&idev->lock);
39111da177e4SLinus Torvalds }
39128f37ada5Sstephen hemminger
391351454ea4SNiels Dossche list_for_each_entry(ifa, &idev->addr_list, if_list)
391451454ea4SNiels Dossche list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
391551454ea4SNiels Dossche write_unlock_bh(&idev->lock);
391651454ea4SNiels Dossche
391751454ea4SNiels Dossche while (!list_empty(&tmp_addr_list)) {
39188d1c802bSDavid Ahern struct fib6_info *rt = NULL;
3919a2d6cbb0SRabin Vincent bool keep;
392038bd10c4SDavid Ahern
392151454ea4SNiels Dossche ifa = list_first_entry(&tmp_addr_list,
392251454ea4SNiels Dossche struct inet6_ifaddr, if_list_aux);
392351454ea4SNiels Dossche list_del(&ifa->if_list_aux);
392451454ea4SNiels Dossche
3925c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifa);
392684e8b803Sstephen hemminger
3927a2d6cbb0SRabin Vincent keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3928a2d6cbb0SRabin Vincent !addr_is_local(&ifa->addr);
3929a2d6cbb0SRabin Vincent
39308e8e676dSHannes Frederic Sowa spin_lock_bh(&ifa->lock);
3931f1705ec1SDavid Ahern
3932a2d6cbb0SRabin Vincent if (keep) {
3933f1705ec1SDavid Ahern /* set state to skip the notifier below */
3934f1705ec1SDavid Ahern state = INET6_IFADDR_STATE_DEAD;
39356d717134SDavid Ahern ifa->state = INET6_IFADDR_STATE_PREDAD;
3936f1705ec1SDavid Ahern if (!(ifa->flags & IFA_F_NODAD))
3937f1705ec1SDavid Ahern ifa->flags |= IFA_F_TENTATIVE;
393838bd10c4SDavid Ahern
393938bd10c4SDavid Ahern rt = ifa->rt;
394038bd10c4SDavid Ahern ifa->rt = NULL;
3941f1705ec1SDavid Ahern } else {
39424c5ff6a6SHerbert Xu state = ifa->state;
39434c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD;
3944f1705ec1SDavid Ahern }
3945f1705ec1SDavid Ahern
39468e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifa->lock);
39474c5ff6a6SHerbert Xu
394838bd10c4SDavid Ahern if (rt)
394911dd74b3SRoopa Prabhu ip6_del_rt(net, rt, false);
395038bd10c4SDavid Ahern
39519d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) {
39521da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa);
3953f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3954ea06f717SMike Manning } else {
3955ea06f717SMike Manning if (idev->cnf.forwarding)
3956ea06f717SMike Manning addrconf_leave_anycast(ifa);
3957ea06f717SMike Manning addrconf_leave_solict(ifa->idev, &ifa->addr);
39582de79570SLorenzo Colitti }
395973a8bd74SDavid S. Miller
39608ef802aaSEric Dumazet if (!keep) {
396151454ea4SNiels Dossche write_lock_bh(&idev->lock);
39628ef802aaSEric Dumazet list_del_rcu(&ifa->if_list);
396351454ea4SNiels Dossche write_unlock_bh(&idev->lock);
39648ef802aaSEric Dumazet in6_ifa_put(ifa);
39658ef802aaSEric Dumazet }
39661da177e4SLinus Torvalds }
39678f37ada5Sstephen hemminger
3968381f4dcaSSabrina Dubroca /* Step 5: Discard anycast and multicast list */
3969ae79dbf6SFlorent Fourcot if (unregister) {
3970381f4dcaSSabrina Dubroca ipv6_ac_destroy_dev(idev);
39711da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev);
39729995b408Sj.nixdorf@avm.de } else if (was_ready) {
39731da177e4SLinus Torvalds ipv6_mc_down(idev);
3974381f4dcaSSabrina Dubroca }
39751da177e4SLinus Torvalds
39761da177e4SLinus Torvalds idev->tstamp = jiffies;
397749b99da2SRocco Yue idev->ra_mtu = 0;
39781da177e4SLinus Torvalds
3979bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */
3980ae79dbf6SFlorent Fourcot if (unregister) {
3981408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev);
39821da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms);
39831da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev);
39841da177e4SLinus Torvalds in6_dev_put(idev);
39851da177e4SLinus Torvalds }
39861da177e4SLinus Torvalds return 0;
39871da177e4SLinus Torvalds }
39881da177e4SLinus Torvalds
addrconf_rs_timer(struct timer_list * t)3989e99e88a9SKees Cook static void addrconf_rs_timer(struct timer_list *t)
39901da177e4SLinus Torvalds {
3991e99e88a9SKees Cook struct inet6_dev *idev = from_timer(idev, t, rs_timer);
3992caf92bc4SCong Wang struct net_device *dev = idev->dev;
3993b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr;
39941da177e4SLinus Torvalds
3995b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock);
39965b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY))
39971da177e4SLinus Torvalds goto out;
39981da177e4SLinus Torvalds
39999ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev))
40001da177e4SLinus Torvalds goto out;
40015b2a1953Sstephen hemminger
40025b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */
40035b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD)
40045b2a1953Sstephen hemminger goto out;
40051da177e4SLinus Torvalds
4006bd11f074SMaciej Żenczykowski if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
4007caf92bc4SCong Wang write_unlock(&idev->lock);
4008caf92bc4SCong Wang if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4009caf92bc4SCong Wang ndisc_send_rs(dev, &lladdr,
4010b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters);
4011b7b1bfceSHannes Frederic Sowa else
4012caf92bc4SCong Wang goto put;
4013b7b1bfceSHannes Frederic Sowa
4014caf92bc4SCong Wang write_lock(&idev->lock);
4015bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_update(
4016bd11f074SMaciej Żenczykowski idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
40171da177e4SLinus Torvalds /* The wait after the last probe can be shorter */
4018b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes ==
4019b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ?
40205b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay :
4021bd11f074SMaciej Żenczykowski idev->rs_interval);
40221da177e4SLinus Torvalds } else {
40231da177e4SLinus Torvalds /*
40241da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link"
40251da177e4SLinus Torvalds * assumption any longer.
40261da177e4SLinus Torvalds */
402791df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
40281da177e4SLinus Torvalds }
40291da177e4SLinus Torvalds
40301da177e4SLinus Torvalds out:
4031b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock);
4032caf92bc4SCong Wang put:
4033b7b1bfceSHannes Frederic Sowa in6_dev_put(idev);
40341da177e4SLinus Torvalds }
40351da177e4SLinus Torvalds
40361da177e4SLinus Torvalds /*
40371da177e4SLinus Torvalds * Duplicate Address Detection
40381da177e4SLinus Torvalds */
addrconf_dad_kick(struct inet6_ifaddr * ifp)40393c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
40403c21edbdSYOSHIFUJI Hideaki {
40413c21edbdSYOSHIFUJI Hideaki unsigned long rand_num;
40423c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev;
4043adc176c5SErik Nordmark u64 nonce;
40443c21edbdSYOSHIFUJI Hideaki
404595c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC)
404695c385b4SNeil Horman rand_num = 0;
404795c385b4SNeil Horman else
40488032bf12SJason A. Donenfeld rand_num = get_random_u32_below(idev->cnf.rtr_solicit_delay ? : 1);
404995c385b4SNeil Horman
4050adc176c5SErik Nordmark nonce = 0;
4051adc176c5SErik Nordmark if (idev->cnf.enhanced_dad ||
4052adc176c5SErik Nordmark dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
4053adc176c5SErik Nordmark do
4054adc176c5SErik Nordmark get_random_bytes(&nonce, 6);
4055adc176c5SErik Nordmark while (nonce == 0);
4056adc176c5SErik Nordmark }
4057adc176c5SErik Nordmark ifp->dad_nonce = nonce;
4058b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits;
4059c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, rand_num);
40603c21edbdSYOSHIFUJI Hideaki }
40613c21edbdSYOSHIFUJI Hideaki
addrconf_dad_begin(struct inet6_ifaddr * ifp)4062c15b1ccaSHannes Frederic Sowa static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
40631da177e4SLinus Torvalds {
40641da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev;
40651da177e4SLinus Torvalds struct net_device *dev = idev->dev;
4066764d3be6SPaolo Abeni bool bump_id, notify = false;
4067afb1d4b5SDavid Ahern struct net *net;
40681da177e4SLinus Torvalds
40691da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr);
40701da177e4SLinus Torvalds
40711da177e4SLinus Torvalds read_lock_bh(&idev->lock);
4072622ccdf1SHerbert Xu spin_lock(&ifp->lock);
4073e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD)
40741da177e4SLinus Torvalds goto out;
40751da177e4SLinus Torvalds
4076afb1d4b5SDavid Ahern net = dev_net(dev);
40771da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
4078afb1d4b5SDavid Ahern (net->ipv6.devconf_all->accept_dad < 1 &&
4079a2d3f3e3SMatteo Croce idev->cnf.accept_dad < 1) ||
408055ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) ||
408155ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) {
4082c76fe2d9SDavid Ahern bool send_na = false;
4083c76fe2d9SDavid Ahern
4084c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE &&
4085c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC))
4086c76fe2d9SDavid Ahern send_na = true;
4087764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE;
4088cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
408921809fafSstephen hemminger spin_unlock(&ifp->lock);
40901da177e4SLinus Torvalds read_unlock_bh(&idev->lock);
40911da177e4SLinus Torvalds
4092c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na);
40931da177e4SLinus Torvalds return;
40941da177e4SLinus Torvalds }
40951da177e4SLinus Torvalds
40966732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) {
409721809fafSstephen hemminger spin_unlock(&ifp->lock);
40986732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock);
40993c21edbdSYOSHIFUJI Hideaki /*
4100590a9887SMasatake YAMATO * If the device is not ready:
41013c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address.
41023c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it.
41033c21edbdSYOSHIFUJI Hideaki */
41043c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp);
4105cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0);
41066732badeSYOSHIFUJI Hideaki return;
41073c21edbdSYOSHIFUJI Hideaki }
410895c385b4SNeil Horman
410995c385b4SNeil Horman /*
411095c385b4SNeil Horman * Optimistic nodes can start receiving
411195c385b4SNeil Horman * Frames right away
411295c385b4SNeil Horman */
41137fd2561eSErik Kline if (ifp->flags & IFA_F_OPTIMISTIC) {
4114afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt);
4115afb1d4b5SDavid Ahern if (ipv6_use_optimistic_addr(net, idev)) {
41167fd2561eSErik Kline /* Because optimistic nodes can use this address,
41177fd2561eSErik Kline * notify listeners. If DAD fails, RTM_DELADDR is sent.
41187fd2561eSErik Kline */
411916186a82Ssubashab@codeaurora.org notify = true;
41207fd2561eSErik Kline }
41217fd2561eSErik Kline }
412295c385b4SNeil Horman
41236732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp);
41241da177e4SLinus Torvalds out:
4125622ccdf1SHerbert Xu spin_unlock(&ifp->lock);
41261da177e4SLinus Torvalds read_unlock_bh(&idev->lock);
412716186a82Ssubashab@codeaurora.org if (notify)
412816186a82Ssubashab@codeaurora.org ipv6_ifa_notify(RTM_NEWADDR, ifp);
41291da177e4SLinus Torvalds }
41301da177e4SLinus Torvalds
addrconf_dad_start(struct inet6_ifaddr * ifp)4131c15b1ccaSHannes Frederic Sowa static void addrconf_dad_start(struct inet6_ifaddr *ifp)
41321da177e4SLinus Torvalds {
4133c15b1ccaSHannes Frederic Sowa bool begin_dad = false;
4134c15b1ccaSHannes Frederic Sowa
41358e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock);
4136c15b1ccaSHannes Frederic Sowa if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4137c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_PREDAD;
4138c15b1ccaSHannes Frederic Sowa begin_dad = true;
4139c15b1ccaSHannes Frederic Sowa }
41408e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock);
4141c15b1ccaSHannes Frederic Sowa
4142c15b1ccaSHannes Frederic Sowa if (begin_dad)
4143c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp, 0);
4144c15b1ccaSHannes Frederic Sowa }
4145c15b1ccaSHannes Frederic Sowa
addrconf_dad_work(struct work_struct * w)4146c15b1ccaSHannes Frederic Sowa static void addrconf_dad_work(struct work_struct *w)
4147c15b1ccaSHannes Frederic Sowa {
4148c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4149c15b1ccaSHannes Frederic Sowa struct inet6_ifaddr,
4150c15b1ccaSHannes Frederic Sowa dad_work);
41511da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev;
4152764d3be6SPaolo Abeni bool bump_id, disable_ipv6 = false;
41531da177e4SLinus Torvalds struct in6_addr mcaddr;
41541da177e4SLinus Torvalds
4155c15b1ccaSHannes Frederic Sowa enum {
4156c15b1ccaSHannes Frederic Sowa DAD_PROCESS,
4157c15b1ccaSHannes Frederic Sowa DAD_BEGIN,
4158c15b1ccaSHannes Frederic Sowa DAD_ABORT,
4159c15b1ccaSHannes Frederic Sowa } action = DAD_PROCESS;
4160c15b1ccaSHannes Frederic Sowa
4161c15b1ccaSHannes Frederic Sowa rtnl_lock();
4162c15b1ccaSHannes Frederic Sowa
41638e8e676dSHannes Frederic Sowa spin_lock_bh(&ifp->lock);
4164c15b1ccaSHannes Frederic Sowa if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4165c15b1ccaSHannes Frederic Sowa action = DAD_BEGIN;
4166c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_DAD;
4167c15b1ccaSHannes Frederic Sowa } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4168c15b1ccaSHannes Frederic Sowa action = DAD_ABORT;
4169c15b1ccaSHannes Frederic Sowa ifp->state = INET6_IFADDR_STATE_POSTDAD;
417085b51b12SMike Manning
417135e015e1SMatteo Croce if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
417235e015e1SMatteo Croce idev->cnf.accept_dad > 1) &&
417335e015e1SMatteo Croce !idev->cnf.disable_ipv6 &&
417485b51b12SMike Manning !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
417585b51b12SMike Manning struct in6_addr addr;
417685b51b12SMike Manning
417785b51b12SMike Manning addr.s6_addr32[0] = htonl(0xfe800000);
417885b51b12SMike Manning addr.s6_addr32[1] = 0;
417985b51b12SMike Manning
418085b51b12SMike Manning if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
418185b51b12SMike Manning ipv6_addr_equal(&ifp->addr, &addr)) {
418285b51b12SMike Manning /* DAD failed for link-local based on MAC */
41834b271168SEric Dumazet WRITE_ONCE(idev->cnf.disable_ipv6, 1);
418485b51b12SMike Manning
418585b51b12SMike Manning pr_info("%s: IPv6 being disabled!\n",
418685b51b12SMike Manning ifp->idev->dev->name);
418785b51b12SMike Manning disable_ipv6 = true;
418885b51b12SMike Manning }
418985b51b12SMike Manning }
4190c15b1ccaSHannes Frederic Sowa }
41918e8e676dSHannes Frederic Sowa spin_unlock_bh(&ifp->lock);
4192c15b1ccaSHannes Frederic Sowa
4193c15b1ccaSHannes Frederic Sowa if (action == DAD_BEGIN) {
4194c15b1ccaSHannes Frederic Sowa addrconf_dad_begin(ifp);
4195c15b1ccaSHannes Frederic Sowa goto out;
4196c15b1ccaSHannes Frederic Sowa } else if (action == DAD_ABORT) {
4197751eb6b6SWei Yongjun in6_ifa_hold(ifp);
4198c15b1ccaSHannes Frederic Sowa addrconf_dad_stop(ifp, 1);
419985b51b12SMike Manning if (disable_ipv6)
4200ae79dbf6SFlorent Fourcot addrconf_ifdown(idev->dev, false);
4201c15b1ccaSHannes Frederic Sowa goto out;
4202c15b1ccaSHannes Frederic Sowa }
4203c15b1ccaSHannes Frederic Sowa
4204b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp))
4205f2344a13SHerbert Xu goto out;
4206f2344a13SHerbert Xu
4207c15b1ccaSHannes Frederic Sowa write_lock_bh(&idev->lock);
4208122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) {
4209c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock);
42101da177e4SLinus Torvalds goto out;
42111da177e4SLinus Torvalds }
421221809fafSstephen hemminger
421321809fafSstephen hemminger spin_lock(&ifp->lock);
4214622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4215622ccdf1SHerbert Xu spin_unlock(&ifp->lock);
4216c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock);
4217622ccdf1SHerbert Xu goto out;
4218622ccdf1SHerbert Xu }
4219622ccdf1SHerbert Xu
4220b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) {
4221c76fe2d9SDavid Ahern bool send_na = false;
4222c76fe2d9SDavid Ahern
42231da177e4SLinus Torvalds /*
42241da177e4SLinus Torvalds * DAD was successful
42251da177e4SLinus Torvalds */
42261da177e4SLinus Torvalds
4227c76fe2d9SDavid Ahern if (ifp->flags & IFA_F_TENTATIVE &&
4228c76fe2d9SDavid Ahern !(ifp->flags & IFA_F_OPTIMISTIC))
4229c76fe2d9SDavid Ahern send_na = true;
4230764d3be6SPaolo Abeni bump_id = ifp->flags & IFA_F_TENTATIVE;
4231cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
423221809fafSstephen hemminger spin_unlock(&ifp->lock);
4233c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock);
42341da177e4SLinus Torvalds
4235c76fe2d9SDavid Ahern addrconf_dad_completed(ifp, bump_id, send_na);
42361da177e4SLinus Torvalds
42371da177e4SLinus Torvalds goto out;
42381da177e4SLinus Torvalds }
42391da177e4SLinus Torvalds
4240b7b1bfceSHannes Frederic Sowa ifp->dad_probes--;
4241c15b1ccaSHannes Frederic Sowa addrconf_mod_dad_work(ifp,
424219e16d22SHangbin Liu max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME),
424319e16d22SHangbin Liu HZ/100));
424421809fafSstephen hemminger spin_unlock(&ifp->lock);
4245c15b1ccaSHannes Frederic Sowa write_unlock_bh(&idev->lock);
42461da177e4SLinus Torvalds
42471da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */
42481da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
4249adc176c5SErik Nordmark ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4250adc176c5SErik Nordmark ifp->dad_nonce);
42511da177e4SLinus Torvalds out:
42521da177e4SLinus Torvalds in6_ifa_put(ifp);
4253c15b1ccaSHannes Frederic Sowa rtnl_unlock();
42541da177e4SLinus Torvalds }
42551da177e4SLinus Torvalds
425611ffff75SHannes Frederic Sowa /* ifp->idev must be at least read locked */
ipv6_lonely_lladdr(struct inet6_ifaddr * ifp)425711ffff75SHannes Frederic Sowa static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
425811ffff75SHannes Frederic Sowa {
425911ffff75SHannes Frederic Sowa struct inet6_ifaddr *ifpiter;
426011ffff75SHannes Frederic Sowa struct inet6_dev *idev = ifp->idev;
426111ffff75SHannes Frederic Sowa
4262602582caSHannes Frederic Sowa list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4263602582caSHannes Frederic Sowa if (ifpiter->scope > IFA_LINK)
4264602582caSHannes Frederic Sowa break;
426511ffff75SHannes Frederic Sowa if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
426611ffff75SHannes Frederic Sowa (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
426711ffff75SHannes Frederic Sowa IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
426811ffff75SHannes Frederic Sowa IFA_F_PERMANENT)
426911ffff75SHannes Frederic Sowa return false;
427011ffff75SHannes Frederic Sowa }
427111ffff75SHannes Frederic Sowa return true;
427211ffff75SHannes Frederic Sowa }
427311ffff75SHannes Frederic Sowa
addrconf_dad_completed(struct inet6_ifaddr * ifp,bool bump_id,bool send_na)4274c76fe2d9SDavid Ahern static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4275c76fe2d9SDavid Ahern bool send_na)
42761da177e4SLinus Torvalds {
42771da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev;
4278b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr;
4279b173ee48SHannes Frederic Sowa bool send_rs, send_mld;
4280b7b1bfceSHannes Frederic Sowa
4281c15b1ccaSHannes Frederic Sowa addrconf_del_dad_work(ifp);
42821da177e4SLinus Torvalds
42831da177e4SLinus Torvalds /*
42841da177e4SLinus Torvalds * Configure the address for reception. Now it is valid.
42851da177e4SLinus Torvalds */
42861da177e4SLinus Torvalds
42871da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp);
42881da177e4SLinus Torvalds
4289026359bcSTore Anderson /* If added prefix is link local and we are prepared to process
4290026359bcSTore Anderson router advertisements, start sending router solicitations.
42911da177e4SLinus Torvalds */
42921da177e4SLinus Torvalds
42931ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock);
429411ffff75SHannes Frederic Sowa send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4295b173ee48SHannes Frederic Sowa send_rs = send_mld &&
4296b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) &&
4297bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicits != 0 &&
4298b52e1cceSjianghaoran (dev->flags & IFF_LOOPBACK) == 0 &&
42992df9bf4dSXin Long (dev->type != ARPHRD_TUNNEL) &&
43002df9bf4dSXin Long !netif_is_team_port(dev);
43011ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock);
43021ec047ebSHannes Frederic Sowa
4303b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany.
4304b173ee48SHannes Frederic Sowa * Resend with proper ll now.
4305b173ee48SHannes Frederic Sowa */
4306b173ee48SHannes Frederic Sowa if (send_mld)
4307b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev);
4308b173ee48SHannes Frederic Sowa
4309c76fe2d9SDavid Ahern /* send unsolicited NA if enabled */
4310c76fe2d9SDavid Ahern if (send_na &&
4311c76fe2d9SDavid Ahern (ifp->idev->cnf.ndisc_notify ||
4312c76fe2d9SDavid Ahern dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
4313c76fe2d9SDavid Ahern ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4314c76fe2d9SDavid Ahern /*router=*/ !!ifp->idev->cnf.forwarding,
4315c76fe2d9SDavid Ahern /*solicited=*/ false, /*override=*/ true,
4316c76fe2d9SDavid Ahern /*inc_opt=*/ true);
4317c76fe2d9SDavid Ahern }
4318c76fe2d9SDavid Ahern
43191ec047ebSHannes Frederic Sowa if (send_rs) {
43201da177e4SLinus Torvalds /*
43211da177e4SLinus Torvalds * If a host as already performed a random delay
43221da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need
43231da177e4SLinus Torvalds * to delay again before sending the first RS
43241da177e4SLinus Torvalds */
43251ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4326b7b1bfceSHannes Frederic Sowa return;
43271ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
43281da177e4SLinus Torvalds
4329b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock);
4330b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock);
4331bd11f074SMaciej Żenczykowski ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4332bd11f074SMaciej Żenczykowski ifp->idev->cnf.rtr_solicit_interval);
4333b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1;
43341da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT;
4335bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4336b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock);
4337b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock);
43381da177e4SLinus Torvalds }
4339764d3be6SPaolo Abeni
4340764d3be6SPaolo Abeni if (bump_id)
4341764d3be6SPaolo Abeni rt_genid_bump_ipv6(dev_net(dev));
4342a11a7f71SMarcus Huewe
4343a11a7f71SMarcus Huewe /* Make sure that a new temporary address will be created
4344a11a7f71SMarcus Huewe * before this temporary address becomes deprecated.
4345a11a7f71SMarcus Huewe */
4346a11a7f71SMarcus Huewe if (ifp->flags & IFA_F_TEMPORARY)
43478805d13fSEric Dumazet addrconf_verify_rtnl(dev_net(dev));
43481da177e4SLinus Torvalds }
43491da177e4SLinus Torvalds
addrconf_dad_run(struct inet6_dev * idev,bool restart)4350896585d4SHangbin Liu static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
4351e21e8467SStephen Hemminger {
4352c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp;
4353c5e33bddSYOSHIFUJI Hideaki
4354c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock);
4355502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) {
435621809fafSstephen hemminger spin_lock(&ifp->lock);
4357896585d4SHangbin Liu if ((ifp->flags & IFA_F_TENTATIVE &&
4358896585d4SHangbin Liu ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4359896585d4SHangbin Liu if (restart)
4360896585d4SHangbin Liu ifp->state = INET6_IFADDR_STATE_PREDAD;
4361c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp);
4362896585d4SHangbin Liu }
4363f2344a13SHerbert Xu spin_unlock(&ifp->lock);
4364c5e33bddSYOSHIFUJI Hideaki }
4365c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock);
4366c5e33bddSYOSHIFUJI Hideaki }
4367c5e33bddSYOSHIFUJI Hideaki
43681da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
43691da177e4SLinus Torvalds struct if6_iter_state {
43703c40090aSDaniel Lezcano struct seq_net_private p;
43711da177e4SLinus Torvalds int bucket;
43721d578303SMihai Maruseac int offset;
43731da177e4SLinus Torvalds };
43741da177e4SLinus Torvalds
if6_get_first(struct seq_file * seq,loff_t pos)43751d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
43761da177e4SLinus Torvalds {
43771da177e4SLinus Torvalds struct if6_iter_state *state = seq->private;
43781218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq);
4379a5c1d98fSEric Dumazet struct inet6_ifaddr *ifa = NULL;
43801d578303SMihai Maruseac int p = 0;
43811da177e4SLinus Torvalds
43821d578303SMihai Maruseac /* initial bucket if pos is 0 */
43831d578303SMihai Maruseac if (pos == 0) {
43841d578303SMihai Maruseac state->bucket = 0;
43851d578303SMihai Maruseac state->offset = 0;
43861d578303SMihai Maruseac }
43871d578303SMihai Maruseac
43881d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4389e66d1172SEric Dumazet hlist_for_each_entry_rcu(ifa, &net->ipv6.inet6_addr_lst[state->bucket],
43901d578303SMihai Maruseac addr_lst) {
43911d578303SMihai Maruseac /* sync with offset */
43921d578303SMihai Maruseac if (p < state->offset) {
43931d578303SMihai Maruseac p++;
43941d578303SMihai Maruseac continue;
43951d578303SMihai Maruseac }
43961da177e4SLinus Torvalds return ifa;
43971da177e4SLinus Torvalds }
43981d578303SMihai Maruseac
43991d578303SMihai Maruseac /* prepare for next bucket */
44001d578303SMihai Maruseac state->offset = 0;
44011d578303SMihai Maruseac p = 0;
44021d578303SMihai Maruseac }
4403c2e21293Sstephen hemminger return NULL;
4404c2e21293Sstephen hemminger }
44051da177e4SLinus Torvalds
if6_get_next(struct seq_file * seq,struct inet6_ifaddr * ifa)4406c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4407c2e21293Sstephen hemminger struct inet6_ifaddr *ifa)
44081da177e4SLinus Torvalds {
44091da177e4SLinus Torvalds struct if6_iter_state *state = seq->private;
44101218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq);
44111da177e4SLinus Torvalds
4412a5c1d98fSEric Dumazet hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
44131d578303SMihai Maruseac state->offset++;
44141da177e4SLinus Torvalds return ifa;
44151d578303SMihai Maruseac }
44161da177e4SLinus Torvalds
44171d578303SMihai Maruseac state->offset = 0;
441886f9bd1fSJeff Barnhill while (++state->bucket < IN6_ADDR_HSIZE) {
4419a5c1d98fSEric Dumazet hlist_for_each_entry_rcu(ifa,
4420e66d1172SEric Dumazet &net->ipv6.inet6_addr_lst[state->bucket], addr_lst) {
4421c2e21293Sstephen hemminger return ifa;
4422c2e21293Sstephen hemminger }
4423c2e21293Sstephen hemminger }
4424c2e21293Sstephen hemminger
4425c2e21293Sstephen hemminger return NULL;
4426c2e21293Sstephen hemminger }
4427c2e21293Sstephen hemminger
if6_seq_start(struct seq_file * seq,loff_t * pos)44281da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4429a5c1d98fSEric Dumazet __acquires(rcu)
44301da177e4SLinus Torvalds {
4431a5c1d98fSEric Dumazet rcu_read_lock();
44321d578303SMihai Maruseac return if6_get_first(seq, *pos);
44331da177e4SLinus Torvalds }
44341da177e4SLinus Torvalds
if6_seq_next(struct seq_file * seq,void * v,loff_t * pos)44351da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
44361da177e4SLinus Torvalds {
44371da177e4SLinus Torvalds struct inet6_ifaddr *ifa;
44381da177e4SLinus Torvalds
44391da177e4SLinus Torvalds ifa = if6_get_next(seq, v);
44401da177e4SLinus Torvalds ++*pos;
44411da177e4SLinus Torvalds return ifa;
44421da177e4SLinus Torvalds }
44431da177e4SLinus Torvalds
if6_seq_stop(struct seq_file * seq,void * v)44441da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
4445a5c1d98fSEric Dumazet __releases(rcu)
44461da177e4SLinus Torvalds {
4447a5c1d98fSEric Dumazet rcu_read_unlock();
44481da177e4SLinus Torvalds }
44491da177e4SLinus Torvalds
if6_seq_show(struct seq_file * seq,void * v)44501da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
44511da177e4SLinus Torvalds {
44521da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4453971a351cSJiri Pirko seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4454b071195dSHarvey Harrison &ifp->addr,
44551da177e4SLinus Torvalds ifp->idev->dev->ifindex,
44561da177e4SLinus Torvalds ifp->prefix_len,
44571da177e4SLinus Torvalds ifp->scope,
4458971a351cSJiri Pirko (u8) ifp->flags,
44591da177e4SLinus Torvalds ifp->idev->dev->name);
44601da177e4SLinus Torvalds return 0;
44611da177e4SLinus Torvalds }
44621da177e4SLinus Torvalds
446356b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
44641da177e4SLinus Torvalds .start = if6_seq_start,
44651da177e4SLinus Torvalds .next = if6_seq_next,
44661da177e4SLinus Torvalds .show = if6_seq_show,
44671da177e4SLinus Torvalds .stop = if6_seq_stop,
44681da177e4SLinus Torvalds };
44691da177e4SLinus Torvalds
if6_proc_net_init(struct net * net)44702c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
44711da177e4SLinus Torvalds {
4472c3506372SChristoph Hellwig if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4473c3506372SChristoph Hellwig sizeof(struct if6_iter_state)))
44741da177e4SLinus Torvalds return -ENOMEM;
44751da177e4SLinus Torvalds return 0;
44761da177e4SLinus Torvalds }
44771da177e4SLinus Torvalds
if6_proc_net_exit(struct net * net)44782c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
44793c40090aSDaniel Lezcano {
4480ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net);
44813c40090aSDaniel Lezcano }
44823c40090aSDaniel Lezcano
44833c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
44843c40090aSDaniel Lezcano .init = if6_proc_net_init,
44853c40090aSDaniel Lezcano .exit = if6_proc_net_exit,
44863c40090aSDaniel Lezcano };
44873c40090aSDaniel Lezcano
if6_proc_init(void)44883c40090aSDaniel Lezcano int __init if6_proc_init(void)
44893c40090aSDaniel Lezcano {
44903c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops);
44913c40090aSDaniel Lezcano }
44923c40090aSDaniel Lezcano
if6_proc_exit(void)44931da177e4SLinus Torvalds void if6_proc_exit(void)
44941da177e4SLinus Torvalds {
44953c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops);
44961da177e4SLinus Torvalds }
44971da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
44981da177e4SLinus Torvalds
449907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
45003b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
ipv6_chk_home_addr(struct net * net,const struct in6_addr * addr)4501b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
45023b9f9a1cSNoriaki TAKAMIYA {
45033f27fb23SEric Dumazet unsigned int hash = inet6_addr_hash(net, addr);
4504c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL;
45053f27fb23SEric Dumazet int ret = 0;
4506c2e21293Sstephen hemminger
45074e5f47abSEric Dumazet rcu_read_lock();
4508e66d1172SEric Dumazet hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4509caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) &&
45103b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) {
45113b9f9a1cSNoriaki TAKAMIYA ret = 1;
45123b9f9a1cSNoriaki TAKAMIYA break;
45133b9f9a1cSNoriaki TAKAMIYA }
45143b9f9a1cSNoriaki TAKAMIYA }
45154e5f47abSEric Dumazet rcu_read_unlock();
45163b9f9a1cSNoriaki TAKAMIYA return ret;
45173b9f9a1cSNoriaki TAKAMIYA }
45183b9f9a1cSNoriaki TAKAMIYA #endif
45193b9f9a1cSNoriaki TAKAMIYA
4520f37c6059SAlexander Aring /* RFC6554 has some algorithm to avoid loops in segment routing by
4521f37c6059SAlexander Aring * checking if the segments contains any of a local interface address.
4522f37c6059SAlexander Aring *
4523f37c6059SAlexander Aring * Quote:
4524f37c6059SAlexander Aring *
4525f37c6059SAlexander Aring * To detect loops in the SRH, a router MUST determine if the SRH
4526f37c6059SAlexander Aring * includes multiple addresses assigned to any interface on that router.
4527f37c6059SAlexander Aring * If such addresses appear more than once and are separated by at least
4528f37c6059SAlexander Aring * one address not assigned to that router.
4529f37c6059SAlexander Aring */
ipv6_chk_rpl_srh_loop(struct net * net,const struct in6_addr * segs,unsigned char nsegs)4530f37c6059SAlexander Aring int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
4531f37c6059SAlexander Aring unsigned char nsegs)
4532f37c6059SAlexander Aring {
4533f37c6059SAlexander Aring const struct in6_addr *addr;
4534f37c6059SAlexander Aring int i, ret = 0, found = 0;
4535f37c6059SAlexander Aring struct inet6_ifaddr *ifp;
4536f37c6059SAlexander Aring bool separated = false;
4537f37c6059SAlexander Aring unsigned int hash;
4538f37c6059SAlexander Aring bool hash_found;
4539f37c6059SAlexander Aring
4540f37c6059SAlexander Aring rcu_read_lock();
4541f37c6059SAlexander Aring for (i = 0; i < nsegs; i++) {
4542f37c6059SAlexander Aring addr = &segs[i];
4543f37c6059SAlexander Aring hash = inet6_addr_hash(net, addr);
4544f37c6059SAlexander Aring
4545f37c6059SAlexander Aring hash_found = false;
4546e66d1172SEric Dumazet hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4547f37c6059SAlexander Aring
4548f37c6059SAlexander Aring if (ipv6_addr_equal(&ifp->addr, addr)) {
4549f37c6059SAlexander Aring hash_found = true;
4550f37c6059SAlexander Aring break;
4551f37c6059SAlexander Aring }
4552f37c6059SAlexander Aring }
4553f37c6059SAlexander Aring
4554f37c6059SAlexander Aring if (hash_found) {
4555f37c6059SAlexander Aring if (found > 1 && separated) {
4556f37c6059SAlexander Aring ret = 1;
4557f37c6059SAlexander Aring break;
4558f37c6059SAlexander Aring }
4559f37c6059SAlexander Aring
4560f37c6059SAlexander Aring separated = false;
4561f37c6059SAlexander Aring found++;
4562f37c6059SAlexander Aring } else {
4563f37c6059SAlexander Aring separated = true;
4564f37c6059SAlexander Aring }
4565f37c6059SAlexander Aring }
4566f37c6059SAlexander Aring rcu_read_unlock();
4567f37c6059SAlexander Aring
4568f37c6059SAlexander Aring return ret;
4569f37c6059SAlexander Aring }
4570f37c6059SAlexander Aring
45711da177e4SLinus Torvalds /*
45721da177e4SLinus Torvalds * Periodic address status verification
45731da177e4SLinus Torvalds */
45741da177e4SLinus Torvalds
addrconf_verify_rtnl(struct net * net)45758805d13fSEric Dumazet static void addrconf_verify_rtnl(struct net *net)
45761da177e4SLinus Torvalds {
4577b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched;
45781da177e4SLinus Torvalds struct inet6_ifaddr *ifp;
45791da177e4SLinus Torvalds int i;
45801da177e4SLinus Torvalds
4581c15b1ccaSHannes Frederic Sowa ASSERT_RTNL();
4582c15b1ccaSHannes Frederic Sowa
45835c578aedSstephen hemminger rcu_read_lock_bh();
45841da177e4SLinus Torvalds now = jiffies;
4585b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
45861da177e4SLinus Torvalds
45878805d13fSEric Dumazet cancel_delayed_work(&net->ipv6.addr_chk_work);
45881da177e4SLinus Torvalds
45891da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) {
45901da177e4SLinus Torvalds restart:
4591e66d1172SEric Dumazet hlist_for_each_entry_rcu_bh(ifp, &net->ipv6.inet6_addr_lst[i], addr_lst) {
45921da177e4SLinus Torvalds unsigned long age;
45931da177e4SLinus Torvalds
4594fad8da3eSYasushi Asano /* When setting preferred_lft to a value not zero or
4595fad8da3eSYasushi Asano * infinity, while valid_lft is infinity
4596fad8da3eSYasushi Asano * IFA_F_PERMANENT has a non-infinity life time.
4597fad8da3eSYasushi Asano */
4598fad8da3eSYasushi Asano if ((ifp->flags & IFA_F_PERMANENT) &&
4599fad8da3eSYasushi Asano (ifp->prefered_lft == INFINITY_LIFE_TIME))
46001da177e4SLinus Torvalds continue;
46011da177e4SLinus Torvalds
46021da177e4SLinus Torvalds spin_lock(&ifp->lock);
4603b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */
4604b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
46051da177e4SLinus Torvalds
4606778964f2SSam Edwards if ((ifp->flags&IFA_F_TEMPORARY) &&
4607778964f2SSam Edwards !(ifp->flags&IFA_F_TENTATIVE) &&
4608778964f2SSam Edwards ifp->prefered_lft != INFINITY_LIFE_TIME &&
4609778964f2SSam Edwards !ifp->regen_count && ifp->ifpub) {
4610778964f2SSam Edwards /* This is a non-regenerated temporary addr. */
4611778964f2SSam Edwards
4612778964f2SSam Edwards unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4613778964f2SSam Edwards ifp->idev->cnf.dad_transmits *
4614778964f2SSam Edwards max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
4615778964f2SSam Edwards
4616778964f2SSam Edwards if (age + regen_advance >= ifp->prefered_lft) {
4617778964f2SSam Edwards struct inet6_ifaddr *ifpub = ifp->ifpub;
4618778964f2SSam Edwards if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4619778964f2SSam Edwards next = ifp->tstamp + ifp->prefered_lft * HZ;
4620778964f2SSam Edwards
4621778964f2SSam Edwards ifp->regen_count++;
4622778964f2SSam Edwards in6_ifa_hold(ifp);
4623778964f2SSam Edwards in6_ifa_hold(ifpub);
4624778964f2SSam Edwards spin_unlock(&ifp->lock);
4625778964f2SSam Edwards
4626778964f2SSam Edwards spin_lock(&ifpub->lock);
4627778964f2SSam Edwards ifpub->regen_count = 0;
4628778964f2SSam Edwards spin_unlock(&ifpub->lock);
4629778964f2SSam Edwards rcu_read_unlock_bh();
4630778964f2SSam Edwards ipv6_create_tempaddr(ifpub, true);
4631778964f2SSam Edwards in6_ifa_put(ifpub);
4632778964f2SSam Edwards in6_ifa_put(ifp);
4633778964f2SSam Edwards rcu_read_lock_bh();
4634778964f2SSam Edwards goto restart;
4635778964f2SSam Edwards } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4636778964f2SSam Edwards next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4637778964f2SSam Edwards }
4638778964f2SSam Edwards
46398f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME &&
46408f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) {
46411da177e4SLinus Torvalds spin_unlock(&ifp->lock);
46421da177e4SLinus Torvalds in6_ifa_hold(ifp);
4643aa8caa76STaehee Yoo rcu_read_unlock_bh();
46441da177e4SLinus Torvalds ipv6_del_addr(ifp);
4645aa8caa76STaehee Yoo rcu_read_lock_bh();
46461da177e4SLinus Torvalds goto restart;
46478f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
46488f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock);
46498f27ebb9SYOSHIFUJI Hideaki continue;
46501da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) {
4651a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
46521da177e4SLinus Torvalds int deprecate = 0;
46531da177e4SLinus Torvalds
46541da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) {
46551da177e4SLinus Torvalds deprecate = 1;
46561da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED;
46571da177e4SLinus Torvalds }
46581da177e4SLinus Torvalds
4659fad8da3eSYasushi Asano if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4660fad8da3eSYasushi Asano (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
46611da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ;
46621da177e4SLinus Torvalds
46631da177e4SLinus Torvalds spin_unlock(&ifp->lock);
46641da177e4SLinus Torvalds
46651da177e4SLinus Torvalds if (deprecate) {
46661da177e4SLinus Torvalds in6_ifa_hold(ifp);
46671da177e4SLinus Torvalds
46681da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp);
46691da177e4SLinus Torvalds in6_ifa_put(ifp);
46701da177e4SLinus Torvalds goto restart;
46711da177e4SLinus Torvalds }
46721da177e4SLinus Torvalds } else {
46731da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */
46741da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
46751da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ;
46761da177e4SLinus Torvalds spin_unlock(&ifp->lock);
46771da177e4SLinus Torvalds }
46781da177e4SLinus Torvalds }
46791da177e4SLinus Torvalds }
46801da177e4SLinus Torvalds
4681b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next);
4682b2db7564SYOSHIFUJI Hideaki next_sched = next;
4683b2db7564SYOSHIFUJI Hideaki
4684b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */
4685b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4686b2db7564SYOSHIFUJI Hideaki next_sched = next_sec;
4687b2db7564SYOSHIFUJI Hideaki
4688b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4689b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4690b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4691b2db7564SYOSHIFUJI Hideaki
4692e32ac250SJoe Perches pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4693ba3542e1Sdingtianhong now, next, next_sec, next_sched);
46948805d13fSEric Dumazet mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, next_sched - now);
46955c578aedSstephen hemminger rcu_read_unlock_bh();
46961da177e4SLinus Torvalds }
46971da177e4SLinus Torvalds
addrconf_verify_work(struct work_struct * w)4698c15b1ccaSHannes Frederic Sowa static void addrconf_verify_work(struct work_struct *w)
4699c15b1ccaSHannes Frederic Sowa {
47008805d13fSEric Dumazet struct net *net = container_of(to_delayed_work(w), struct net,
47018805d13fSEric Dumazet ipv6.addr_chk_work);
47028805d13fSEric Dumazet
4703c15b1ccaSHannes Frederic Sowa rtnl_lock();
47048805d13fSEric Dumazet addrconf_verify_rtnl(net);
4705c15b1ccaSHannes Frederic Sowa rtnl_unlock();
4706c15b1ccaSHannes Frederic Sowa }
4707c15b1ccaSHannes Frederic Sowa
addrconf_verify(struct net * net)47088805d13fSEric Dumazet static void addrconf_verify(struct net *net)
4709c15b1ccaSHannes Frederic Sowa {
47108805d13fSEric Dumazet mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, 0);
4711c15b1ccaSHannes Frederic Sowa }
4712c15b1ccaSHannes Frederic Sowa
extract_addr(struct nlattr * addr,struct nlattr * local,struct in6_addr ** peer_pfx)4713caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4714caeaba79SNicolas Dichtel struct in6_addr **peer_pfx)
4715461d8837SThomas Graf {
4716461d8837SThomas Graf struct in6_addr *pfx = NULL;
4717461d8837SThomas Graf
4718caeaba79SNicolas Dichtel *peer_pfx = NULL;
4719caeaba79SNicolas Dichtel
4720461d8837SThomas Graf if (addr)
4721461d8837SThomas Graf pfx = nla_data(addr);
4722461d8837SThomas Graf
4723461d8837SThomas Graf if (local) {
4724461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4725caeaba79SNicolas Dichtel *peer_pfx = pfx;
4726461d8837SThomas Graf pfx = nla_data(local);
4727461d8837SThomas Graf }
4728461d8837SThomas Graf
4729461d8837SThomas Graf return pfx;
4730461d8837SThomas Graf }
4731461d8837SThomas Graf
4732ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4733461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4734461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4735461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
4736479840ffSJiri Pirko [IFA_FLAGS] = { .len = sizeof(u32) },
47378308f3ffSDavid Ahern [IFA_RT_PRIORITY] = { .len = sizeof(u32) },
47386ecf4c37SChristian Brauner [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
473947f0bd50SJacques de Laval [IFA_PROTO] = { .type = NLA_U8 },
4740461d8837SThomas Graf };
4741461d8837SThomas Graf
47421da177e4SLinus Torvalds static int
inet6_rtm_deladdr(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)4743c21ef3e3SDavid Ahern inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4744c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
47451da177e4SLinus Torvalds {
47463b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk);
4747b933f716SThomas Graf struct ifaddrmsg *ifm;
4748b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1];
4749caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx;
47506046d5b4SHeiner Kallweit u32 ifa_flags;
4751b933f716SThomas Graf int err;
47521da177e4SLinus Torvalds
47538cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
47548cb08174SJohannes Berg ifa_ipv6_policy, extack);
4755b933f716SThomas Graf if (err < 0)
4756b933f716SThomas Graf return err;
4757b933f716SThomas Graf
4758b933f716SThomas Graf ifm = nlmsg_data(nlh);
4759caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
476063159f29SIan Morris if (!pfx)
47611da177e4SLinus Torvalds return -EINVAL;
47621da177e4SLinus Torvalds
47636046d5b4SHeiner Kallweit ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
47646046d5b4SHeiner Kallweit
47656046d5b4SHeiner Kallweit /* We ignore other flags so far. */
47666046d5b4SHeiner Kallweit ifa_flags &= IFA_F_MANAGETEMPADDR;
47676046d5b4SHeiner Kallweit
47686046d5b4SHeiner Kallweit return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
47697f6c4039SHangbin Liu ifm->ifa_prefixlen, extack);
47701da177e4SLinus Torvalds }
47711da177e4SLinus Torvalds
modify_prefix_route(struct inet6_ifaddr * ifp,unsigned long expires,u32 flags,bool modify_peer)47728308f3ffSDavid Ahern static int modify_prefix_route(struct inet6_ifaddr *ifp,
477361794012SHangbin Liu unsigned long expires, u32 flags,
477461794012SHangbin Liu bool modify_peer)
47758308f3ffSDavid Ahern {
47768308f3ffSDavid Ahern struct fib6_info *f6i;
4777e7c7faa9SDavid Ahern u32 prio;
47788308f3ffSDavid Ahern
477961794012SHangbin Liu f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
478061794012SHangbin Liu ifp->prefix_len,
47812b2450caSDavid Ahern ifp->idev->dev, 0, RTF_DEFAULT, true);
47828308f3ffSDavid Ahern if (!f6i)
47838308f3ffSDavid Ahern return -ENOENT;
47848308f3ffSDavid Ahern
4785e7c7faa9SDavid Ahern prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4786e7c7faa9SDavid Ahern if (f6i->fib6_metric != prio) {
4787e7c7faa9SDavid Ahern /* delete old one */
478811dd74b3SRoopa Prabhu ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
4789e7c7faa9SDavid Ahern
47908308f3ffSDavid Ahern /* add new one */
479161794012SHangbin Liu addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
479261794012SHangbin Liu ifp->prefix_len,
47938308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev,
47948308f3ffSDavid Ahern expires, flags, GFP_KERNEL);
47958308f3ffSDavid Ahern } else {
47968308f3ffSDavid Ahern if (!expires)
47978308f3ffSDavid Ahern fib6_clean_expires(f6i);
47988308f3ffSDavid Ahern else
47998308f3ffSDavid Ahern fib6_set_expires(f6i, expires);
48008308f3ffSDavid Ahern
48018308f3ffSDavid Ahern fib6_info_release(f6i);
48028308f3ffSDavid Ahern }
48038308f3ffSDavid Ahern
48048308f3ffSDavid Ahern return 0;
48058308f3ffSDavid Ahern }
48068308f3ffSDavid Ahern
inet6_addr_modify(struct net * net,struct inet6_ifaddr * ifp,struct ifa6_config * cfg)48078805d13fSEric Dumazet static int inet6_addr_modify(struct net *net, struct inet6_ifaddr *ifp,
48088805d13fSEric Dumazet struct ifa6_config *cfg)
4809081bba5bSNoriaki TAKAMIYA {
48106f704992SYOSHIFUJI Hideaki u32 flags;
48116f704992SYOSHIFUJI Hideaki clock_t expires;
48124bed72e4SYOSHIFUJI Hideaki unsigned long timeout;
481353bd6749SJiri Pirko bool was_managetempaddr;
48145b84efecSThomas Haller bool had_prefixroute;
4815d0098e4cSHangbin Liu bool new_peer = false;
481646d48046SYOSHIFUJI Hideaki
4817c15b1ccaSHannes Frederic Sowa ASSERT_RTNL();
4818c15b1ccaSHannes Frederic Sowa
4819d169a1f8SDavid Ahern if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
4820081bba5bSNoriaki TAKAMIYA return -EINVAL;
4821081bba5bSNoriaki TAKAMIYA
4822d169a1f8SDavid Ahern if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
482353bd6749SJiri Pirko (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
482453bd6749SJiri Pirko return -EINVAL;
482553bd6749SJiri Pirko
4826f1c02cfbSSabrina Dubroca if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
4827d169a1f8SDavid Ahern cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
4828f1c02cfbSSabrina Dubroca
4829d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
48304bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) {
48314bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ);
4832d169a1f8SDavid Ahern cfg->valid_lft = timeout;
48336f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES;
48344bed72e4SYOSHIFUJI Hideaki } else {
48354bed72e4SYOSHIFUJI Hideaki expires = 0;
48364bed72e4SYOSHIFUJI Hideaki flags = 0;
4837d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_PERMANENT;
48386f704992SYOSHIFUJI Hideaki }
4839081bba5bSNoriaki TAKAMIYA
4840d169a1f8SDavid Ahern timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
48414bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) {
48424bed72e4SYOSHIFUJI Hideaki if (timeout == 0)
4843d169a1f8SDavid Ahern cfg->ifa_flags |= IFA_F_DEPRECATED;
4844d169a1f8SDavid Ahern cfg->preferred_lft = timeout;
48454bed72e4SYOSHIFUJI Hideaki }
4846081bba5bSNoriaki TAKAMIYA
4847d0098e4cSHangbin Liu if (cfg->peer_pfx &&
4848d0098e4cSHangbin Liu memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) {
4849d0098e4cSHangbin Liu if (!ipv6_addr_any(&ifp->peer_addr))
4850d0098e4cSHangbin Liu cleanup_prefix_route(ifp, expires, true, true);
4851d0098e4cSHangbin Liu new_peer = true;
4852d0098e4cSHangbin Liu }
4853d0098e4cSHangbin Liu
4854081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock);
485553bd6749SJiri Pirko was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
48565b84efecSThomas Haller had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
48575b84efecSThomas Haller !(ifp->flags & IFA_F_NOPREFIXROUTE);
485853bd6749SJiri Pirko ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4859761aac73SThomas Haller IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4860761aac73SThomas Haller IFA_F_NOPREFIXROUTE);
4861d169a1f8SDavid Ahern ifp->flags |= cfg->ifa_flags;
4862081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies;
4863d169a1f8SDavid Ahern ifp->valid_lft = cfg->valid_lft;
4864d169a1f8SDavid Ahern ifp->prefered_lft = cfg->preferred_lft;
486547f0bd50SJacques de Laval ifp->ifa_proto = cfg->ifa_proto;
4866081bba5bSNoriaki TAKAMIYA
48678308f3ffSDavid Ahern if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
48688308f3ffSDavid Ahern ifp->rt_priority = cfg->rt_priority;
4869081bba5bSNoriaki TAKAMIYA
4870d0098e4cSHangbin Liu if (new_peer)
4871d0098e4cSHangbin Liu ifp->peer_addr = *cfg->peer_pfx;
4872d0098e4cSHangbin Liu
4873081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock);
4874081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE))
4875081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp);
4876081bba5bSNoriaki TAKAMIYA
4877d169a1f8SDavid Ahern if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
48788308f3ffSDavid Ahern int rc = -ENOENT;
48798308f3ffSDavid Ahern
48808308f3ffSDavid Ahern if (had_prefixroute)
488161794012SHangbin Liu rc = modify_prefix_route(ifp, expires, flags, false);
48828308f3ffSDavid Ahern
48838308f3ffSDavid Ahern /* prefix route could have been deleted; if so restore it */
48848308f3ffSDavid Ahern if (rc == -ENOENT) {
4885acb54e3cSDavid Ahern addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
48868308f3ffSDavid Ahern ifp->rt_priority, ifp->idev->dev,
48878308f3ffSDavid Ahern expires, flags, GFP_KERNEL);
48888308f3ffSDavid Ahern }
488961794012SHangbin Liu
489061794012SHangbin Liu if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr))
489161794012SHangbin Liu rc = modify_prefix_route(ifp, expires, flags, true);
489261794012SHangbin Liu
489361794012SHangbin Liu if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) {
489461794012SHangbin Liu addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len,
489561794012SHangbin Liu ifp->rt_priority, ifp->idev->dev,
489661794012SHangbin Liu expires, flags, GFP_KERNEL);
489761794012SHangbin Liu }
48985b84efecSThomas Haller } else if (had_prefixroute) {
48995b84efecSThomas Haller enum cleanup_prefix_rt_t action;
49005b84efecSThomas Haller unsigned long rt_expires;
49015b84efecSThomas Haller
49025b84efecSThomas Haller write_lock_bh(&ifp->idev->lock);
49035b84efecSThomas Haller action = check_cleanup_prefix_route(ifp, &rt_expires);
49045b84efecSThomas Haller write_unlock_bh(&ifp->idev->lock);
49055b84efecSThomas Haller
49065b84efecSThomas Haller if (action != CLEANUP_PREFIX_RT_NOP) {
49075b84efecSThomas Haller cleanup_prefix_route(ifp, rt_expires,
4908d0098e4cSHangbin Liu action == CLEANUP_PREFIX_RT_DEL, false);
49095b84efecSThomas Haller }
4910761aac73SThomas Haller }
491153bd6749SJiri Pirko
491253bd6749SJiri Pirko if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4913*836deb96SHangbin Liu if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4914*836deb96SHangbin Liu delete_tempaddrs(ifp->idev, ifp);
4915*836deb96SHangbin Liu else
4916d169a1f8SDavid Ahern manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
4917d169a1f8SDavid Ahern cfg->preferred_lft, !was_managetempaddr,
4918d169a1f8SDavid Ahern jiffies);
491953bd6749SJiri Pirko }
492053bd6749SJiri Pirko
49218805d13fSEric Dumazet addrconf_verify_rtnl(net);
4922081bba5bSNoriaki TAKAMIYA
4923081bba5bSNoriaki TAKAMIYA return 0;
4924081bba5bSNoriaki TAKAMIYA }
4925081bba5bSNoriaki TAKAMIYA
4926081bba5bSNoriaki TAKAMIYA static int
inet6_rtm_newaddr(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)4927c21ef3e3SDavid Ahern inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4928c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
49291da177e4SLinus Torvalds {
49303b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk);
4931461d8837SThomas Graf struct ifaddrmsg *ifm;
4932461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1];
493319b1518cSDavid Ahern struct in6_addr *peer_pfx;
49347198f8ceSThomas Graf struct inet6_ifaddr *ifa;
49357198f8ceSThomas Graf struct net_device *dev;
4936f1c02cfbSSabrina Dubroca struct inet6_dev *idev;
493719b1518cSDavid Ahern struct ifa6_config cfg;
4938461d8837SThomas Graf int err;
49391da177e4SLinus Torvalds
49408cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
49418cb08174SJohannes Berg ifa_ipv6_policy, extack);
4942461d8837SThomas Graf if (err < 0)
4943461d8837SThomas Graf return err;
4944461d8837SThomas Graf
494519b1518cSDavid Ahern memset(&cfg, 0, sizeof(cfg));
494619b1518cSDavid Ahern
4947461d8837SThomas Graf ifm = nlmsg_data(nlh);
494819b1518cSDavid Ahern cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
494919b1518cSDavid Ahern if (!cfg.pfx)
49501da177e4SLinus Torvalds return -EINVAL;
49511da177e4SLinus Torvalds
495219b1518cSDavid Ahern cfg.peer_pfx = peer_pfx;
495319b1518cSDavid Ahern cfg.plen = ifm->ifa_prefixlen;
49548308f3ffSDavid Ahern if (tb[IFA_RT_PRIORITY])
49558308f3ffSDavid Ahern cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
49568308f3ffSDavid Ahern
495747f0bd50SJacques de Laval if (tb[IFA_PROTO])
495847f0bd50SJacques de Laval cfg.ifa_proto = nla_get_u8(tb[IFA_PROTO]);
495947f0bd50SJacques de Laval
496019b1518cSDavid Ahern cfg.valid_lft = INFINITY_LIFE_TIME;
496119b1518cSDavid Ahern cfg.preferred_lft = INFINITY_LIFE_TIME;
496219b1518cSDavid Ahern
4963461d8837SThomas Graf if (tb[IFA_CACHEINFO]) {
49640778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci;
4965461d8837SThomas Graf
4966461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]);
496719b1518cSDavid Ahern cfg.valid_lft = ci->ifa_valid;
496819b1518cSDavid Ahern cfg.preferred_lft = ci->ifa_prefered;
49690778769dSNoriaki TAKAMIYA }
49700778769dSNoriaki TAKAMIYA
4971af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index);
49727f6c4039SHangbin Liu if (!dev) {
49737f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface");
49747198f8ceSThomas Graf return -ENODEV;
49757f6c4039SHangbin Liu }
4976081bba5bSNoriaki TAKAMIYA
497719b1518cSDavid Ahern if (tb[IFA_FLAGS])
497819b1518cSDavid Ahern cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
497919b1518cSDavid Ahern else
498019b1518cSDavid Ahern cfg.ifa_flags = ifm->ifa_flags;
4981479840ffSJiri Pirko
498255ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */
498319b1518cSDavid Ahern cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
498419b1518cSDavid Ahern IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
498519b1518cSDavid Ahern IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
4986f1c02cfbSSabrina Dubroca
4987f1c02cfbSSabrina Dubroca idev = ipv6_find_idev(dev);
4988db0b99f5SSabrina Dubroca if (IS_ERR(idev))
4989db0b99f5SSabrina Dubroca return PTR_ERR(idev);
4990f1c02cfbSSabrina Dubroca
4991f1c02cfbSSabrina Dubroca if (!ipv6_allow_optimistic_dad(net, idev))
499219b1518cSDavid Ahern cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
4993f1c02cfbSSabrina Dubroca
499419b1518cSDavid Ahern if (cfg.ifa_flags & IFA_F_NODAD &&
499519b1518cSDavid Ahern cfg.ifa_flags & IFA_F_OPTIMISTIC) {
4996f1c02cfbSSabrina Dubroca NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4997f1c02cfbSSabrina Dubroca return -EINVAL;
4998f1c02cfbSSabrina Dubroca }
499955ebaef1SNoriaki TAKAMIYA
500019b1518cSDavid Ahern ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
500163159f29SIan Morris if (!ifa) {
50027198f8ceSThomas Graf /*
50037198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but
5004db9c7c39Sstephen hemminger * userspace already relies on not having to provide this.
50057198f8ceSThomas Graf */
500619b1518cSDavid Ahern return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
50071da177e4SLinus Torvalds }
50081da177e4SLinus Torvalds
50097198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL ||
50107f6c4039SHangbin Liu !(nlh->nlmsg_flags & NLM_F_REPLACE)) {
50117f6c4039SHangbin Liu NL_SET_ERR_MSG_MOD(extack, "address already assigned");
50127198f8ceSThomas Graf err = -EEXIST;
50137f6c4039SHangbin Liu } else {
50148805d13fSEric Dumazet err = inet6_addr_modify(net, ifa, &cfg);
50157f6c4039SHangbin Liu }
50167198f8ceSThomas Graf
50177198f8ceSThomas Graf in6_ifa_put(ifa);
50187198f8ceSThomas Graf
50197198f8ceSThomas Graf return err;
50207198f8ceSThomas Graf }
50217198f8ceSThomas Graf
put_ifaddrmsg(struct nlmsghdr * nlh,u8 prefixlen,u32 flags,u8 scope,int ifindex)5022479840ffSJiri Pirko static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
5023101bb229SThomas Graf u8 scope, int ifindex)
5024101bb229SThomas Graf {
5025101bb229SThomas Graf struct ifaddrmsg *ifm;
5026101bb229SThomas Graf
5027101bb229SThomas Graf ifm = nlmsg_data(nlh);
5028101bb229SThomas Graf ifm->ifa_family = AF_INET6;
5029101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen;
5030101bb229SThomas Graf ifm->ifa_flags = flags;
5031101bb229SThomas Graf ifm->ifa_scope = scope;
5032101bb229SThomas Graf ifm->ifa_index = ifindex;
5033101bb229SThomas Graf }
5034101bb229SThomas Graf
put_cacheinfo(struct sk_buff * skb,unsigned long cstamp,unsigned long tstamp,u32 preferred,u32 valid)503585486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
503685486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid)
503785486af0SThomas Graf {
503885486af0SThomas Graf struct ifa_cacheinfo ci;
503985486af0SThomas Graf
504018a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp);
504118a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp);
504285486af0SThomas Graf ci.ifa_prefered = preferred;
504385486af0SThomas Graf ci.ifa_valid = valid;
504485486af0SThomas Graf
504585486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
504685486af0SThomas Graf }
504785486af0SThomas Graf
rt_scope(int ifa_scope)5048101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
5049101bb229SThomas Graf {
5050101bb229SThomas Graf if (ifa_scope & IFA_HOST)
5051101bb229SThomas Graf return RT_SCOPE_HOST;
5052101bb229SThomas Graf else if (ifa_scope & IFA_LINK)
5053101bb229SThomas Graf return RT_SCOPE_LINK;
5054101bb229SThomas Graf else if (ifa_scope & IFA_SITE)
5055101bb229SThomas Graf return RT_SCOPE_SITE;
5056101bb229SThomas Graf else
5057101bb229SThomas Graf return RT_SCOPE_UNIVERSE;
5058101bb229SThomas Graf }
5059101bb229SThomas Graf
inet6_ifaddr_msgsize(void)50600ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
50610ab6803bSThomas Graf {
5062339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
5063caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */
5064339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */
5065479840ffSJiri Pirko + nla_total_size(sizeof(struct ifa_cacheinfo))
50668308f3ffSDavid Ahern + nla_total_size(4) /* IFA_FLAGS */
506747f0bd50SJacques de Laval + nla_total_size(1) /* IFA_PROTO */
50688308f3ffSDavid Ahern + nla_total_size(4) /* IFA_RT_PRIORITY */;
50690ab6803bSThomas Graf }
5070c5396a31SYOSHIFUJI Hideaki
50716ba1e6e8SDavid Ahern enum addr_type_t {
50726ba1e6e8SDavid Ahern UNICAST_ADDR,
50736ba1e6e8SDavid Ahern MULTICAST_ADDR,
50746ba1e6e8SDavid Ahern ANYCAST_ADDR,
50756ba1e6e8SDavid Ahern };
50766ba1e6e8SDavid Ahern
5077203651b6SChristian Brauner struct inet6_fill_args {
5078203651b6SChristian Brauner u32 portid;
5079203651b6SChristian Brauner u32 seq;
5080203651b6SChristian Brauner int event;
5081203651b6SChristian Brauner unsigned int flags;
5082203651b6SChristian Brauner int netnsid;
50836371a71fSDavid Ahern int ifindex;
50846ba1e6e8SDavid Ahern enum addr_type_t type;
5085203651b6SChristian Brauner };
5086203651b6SChristian Brauner
inet6_fill_ifaddr(struct sk_buff * skb,struct inet6_ifaddr * ifa,struct inet6_fill_args * args)50871da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
5088203651b6SChristian Brauner struct inet6_fill_args *args)
50891da177e4SLinus Torvalds {
50901da177e4SLinus Torvalds struct nlmsghdr *nlh;
509185486af0SThomas Graf u32 preferred, valid;
50921da177e4SLinus Torvalds
5093203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5094203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags);
509563159f29SIan Morris if (!nlh)
509626932566SPatrick McHardy return -EMSGSIZE;
50970ab6803bSThomas Graf
5098101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
5099101bb229SThomas Graf ifa->idev->dev->ifindex);
5100101bb229SThomas Graf
5101203651b6SChristian Brauner if (args->netnsid >= 0 &&
5102203651b6SChristian Brauner nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
51036ecf4c37SChristian Brauner goto error;
51046ecf4c37SChristian Brauner
51056c0d8833SNiels Dossche spin_lock_bh(&ifa->lock);
5106fad8da3eSYasushi Asano if (!((ifa->flags&IFA_F_PERMANENT) &&
5107fad8da3eSYasushi Asano (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
510885486af0SThomas Graf preferred = ifa->prefered_lft;
510985486af0SThomas Graf valid = ifa->valid_lft;
511085486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) {
51111da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ;
5112a1faa698SJens Rosenboom if (preferred > tval)
511385486af0SThomas Graf preferred -= tval;
5114a1faa698SJens Rosenboom else
5115a1faa698SJens Rosenboom preferred = 0;
5116f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) {
5117f56619fcSBen Hutchings if (valid > tval)
511885486af0SThomas Graf valid -= tval;
5119f56619fcSBen Hutchings else
5120f56619fcSBen Hutchings valid = 0;
5121f56619fcSBen Hutchings }
51221da177e4SLinus Torvalds }
51231da177e4SLinus Torvalds } else {
512485486af0SThomas Graf preferred = INFINITY_LIFE_TIME;
512585486af0SThomas Graf valid = INFINITY_LIFE_TIME;
51261da177e4SLinus Torvalds }
51276c0d8833SNiels Dossche spin_unlock_bh(&ifa->lock);
512885486af0SThomas Graf
51297996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) {
5130930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
5131930345eaSJiri Benc nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
5132caeaba79SNicolas Dichtel goto error;
5133caeaba79SNicolas Dichtel } else
5134930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
5135caeaba79SNicolas Dichtel goto error;
5136caeaba79SNicolas Dichtel
51378308f3ffSDavid Ahern if (ifa->rt_priority &&
51388308f3ffSDavid Ahern nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
51398308f3ffSDavid Ahern goto error;
51408308f3ffSDavid Ahern
5141caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
5142caeaba79SNicolas Dichtel goto error;
514385486af0SThomas Graf
5144479840ffSJiri Pirko if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
5145479840ffSJiri Pirko goto error;
5146479840ffSJiri Pirko
514747f0bd50SJacques de Laval if (ifa->ifa_proto &&
514847f0bd50SJacques de Laval nla_put_u8(skb, IFA_PROTO, ifa->ifa_proto))
514947f0bd50SJacques de Laval goto error;
515047f0bd50SJacques de Laval
5151053c095aSJohannes Berg nlmsg_end(skb, nlh);
5152053c095aSJohannes Berg return 0;
5153caeaba79SNicolas Dichtel
5154caeaba79SNicolas Dichtel error:
5155caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh);
5156caeaba79SNicolas Dichtel return -EMSGSIZE;
51571da177e4SLinus Torvalds }
51581da177e4SLinus Torvalds
inet6_fill_ifmcaddr(struct sk_buff * skb,struct ifmcaddr6 * ifmca,struct inet6_fill_args * args)51591da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
5160203651b6SChristian Brauner struct inet6_fill_args *args)
51611da177e4SLinus Torvalds {
51621da177e4SLinus Torvalds struct nlmsghdr *nlh;
5163101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE;
5164101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex;
51651da177e4SLinus Torvalds
51661da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
5167101bb229SThomas Graf scope = RT_SCOPE_SITE;
5168101bb229SThomas Graf
5169203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5170203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags);
517163159f29SIan Morris if (!nlh)
517226932566SPatrick McHardy return -EMSGSIZE;
51730ab6803bSThomas Graf
5174203651b6SChristian Brauner if (args->netnsid >= 0 &&
5175ceb736e1SZhang Qilong nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5176ceb736e1SZhang Qilong nlmsg_cancel(skb, nlh);
51776ecf4c37SChristian Brauner return -EMSGSIZE;
5178ceb736e1SZhang Qilong }
51796ecf4c37SChristian Brauner
5180101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5181930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
51820ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
518326932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
518426932566SPatrick McHardy nlmsg_cancel(skb, nlh);
518526932566SPatrick McHardy return -EMSGSIZE;
518626932566SPatrick McHardy }
518785486af0SThomas Graf
5188053c095aSJohannes Berg nlmsg_end(skb, nlh);
5189053c095aSJohannes Berg return 0;
51901da177e4SLinus Torvalds }
51911da177e4SLinus Torvalds
inet6_fill_ifacaddr(struct sk_buff * skb,struct ifacaddr6 * ifaca,struct inet6_fill_args * args)51921da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
5193203651b6SChristian Brauner struct inet6_fill_args *args)
51941da177e4SLinus Torvalds {
51959ee8cbb2SDavid Ahern struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
51969ee8cbb2SDavid Ahern int ifindex = dev ? dev->ifindex : 1;
51971da177e4SLinus Torvalds struct nlmsghdr *nlh;
5198101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE;
51991da177e4SLinus Torvalds
52001da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
5201101bb229SThomas Graf scope = RT_SCOPE_SITE;
5202101bb229SThomas Graf
5203203651b6SChristian Brauner nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5204203651b6SChristian Brauner sizeof(struct ifaddrmsg), args->flags);
520563159f29SIan Morris if (!nlh)
520626932566SPatrick McHardy return -EMSGSIZE;
52070ab6803bSThomas Graf
5208203651b6SChristian Brauner if (args->netnsid >= 0 &&
5209ceb736e1SZhang Qilong nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5210ceb736e1SZhang Qilong nlmsg_cancel(skb, nlh);
52116ecf4c37SChristian Brauner return -EMSGSIZE;
5212ceb736e1SZhang Qilong }
52136ecf4c37SChristian Brauner
5214101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5215930345eaSJiri Benc if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
52160ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
521726932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
521826932566SPatrick McHardy nlmsg_cancel(skb, nlh);
521926932566SPatrick McHardy return -EMSGSIZE;
522026932566SPatrick McHardy }
522185486af0SThomas Graf
5222053c095aSJohannes Berg nlmsg_end(skb, nlh);
5223053c095aSJohannes Berg return 0;
52241da177e4SLinus Torvalds }
52251da177e4SLinus Torvalds
5226234b27c3SEric Dumazet /* called with rcu_read_lock() */
in6_dump_addrs(struct inet6_dev * idev,struct sk_buff * skb,struct netlink_callback * cb,int s_ip_idx,struct inet6_fill_args * fillargs)5227234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
5228fe884c2bSDavid Ahern struct netlink_callback *cb, int s_ip_idx,
52296ba1e6e8SDavid Ahern struct inet6_fill_args *fillargs)
52301da177e4SLinus Torvalds {
52311da177e4SLinus Torvalds struct ifmcaddr6 *ifmca;
52321da177e4SLinus Torvalds struct ifacaddr6 *ifaca;
5233fe884c2bSDavid Ahern int ip_idx = 0;
5234234b27c3SEric Dumazet int err = 1;
52351da177e4SLinus Torvalds
52361da177e4SLinus Torvalds read_lock_bh(&idev->lock);
52376ba1e6e8SDavid Ahern switch (fillargs->type) {
5238502a2ffdSstephen hemminger case UNICAST_ADDR: {
5239502a2ffdSstephen hemminger struct inet6_ifaddr *ifa;
52406ba1e6e8SDavid Ahern fillargs->event = RTM_NEWADDR;
5241502a2ffdSstephen hemminger
5242ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */
5243502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) {
52444ba4c566SDavid Ahern if (ip_idx < s_ip_idx)
52454ba4c566SDavid Ahern goto next;
52466ba1e6e8SDavid Ahern err = inet6_fill_ifaddr(skb, ifa, fillargs);
5247053c095aSJohannes Berg if (err < 0)
5248bcd32326SEric Dumazet break;
524963998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb));
52504ba4c566SDavid Ahern next:
52514ba4c566SDavid Ahern ip_idx++;
52521da177e4SLinus Torvalds }
52531da177e4SLinus Torvalds break;
5254502a2ffdSstephen hemminger }
52551da177e4SLinus Torvalds case MULTICAST_ADDR:
525688e2ca30STaehee Yoo read_unlock_bh(&idev->lock);
52576ba1e6e8SDavid Ahern fillargs->event = RTM_GETMULTICAST;
5258203651b6SChristian Brauner
52591da177e4SLinus Torvalds /* multicast address */
52604e43e64dSEric Dumazet for (ifmca = rtnl_dereference(idev->mc_list);
526188e2ca30STaehee Yoo ifmca;
52624e43e64dSEric Dumazet ifmca = rtnl_dereference(ifmca->next), ip_idx++) {
52631da177e4SLinus Torvalds if (ip_idx < s_ip_idx)
52641da177e4SLinus Torvalds continue;
52656ba1e6e8SDavid Ahern err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
5266053c095aSJohannes Berg if (err < 0)
5267bcd32326SEric Dumazet break;
52681da177e4SLinus Torvalds }
526988e2ca30STaehee Yoo read_lock_bh(&idev->lock);
52701da177e4SLinus Torvalds break;
52711da177e4SLinus Torvalds case ANYCAST_ADDR:
52726ba1e6e8SDavid Ahern fillargs->event = RTM_GETANYCAST;
52731da177e4SLinus Torvalds /* anycast address */
52741da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca;
52751da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) {
52761da177e4SLinus Torvalds if (ip_idx < s_ip_idx)
52771da177e4SLinus Torvalds continue;
52786ba1e6e8SDavid Ahern err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
5279053c095aSJohannes Berg if (err < 0)
5280bcd32326SEric Dumazet break;
52811da177e4SLinus Torvalds }
52821da177e4SLinus Torvalds break;
52831da177e4SLinus Torvalds default:
52841da177e4SLinus Torvalds break;
52851da177e4SLinus Torvalds }
52861da177e4SLinus Torvalds read_unlock_bh(&idev->lock);
5287fe884c2bSDavid Ahern cb->args[2] = ip_idx;
5288234b27c3SEric Dumazet return err;
5289234b27c3SEric Dumazet }
52905d5619b4SYOSHIFUJI Hideaki
inet6_valid_dump_ifaddr_req(const struct nlmsghdr * nlh,struct inet6_fill_args * fillargs,struct net ** tgt_net,struct sock * sk,struct netlink_callback * cb)5291ed6eff11SDavid Ahern static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5292ed6eff11SDavid Ahern struct inet6_fill_args *fillargs,
5293ed6eff11SDavid Ahern struct net **tgt_net, struct sock *sk,
52946371a71fSDavid Ahern struct netlink_callback *cb)
5295ed6eff11SDavid Ahern {
52966371a71fSDavid Ahern struct netlink_ext_ack *extack = cb->extack;
5297ed6eff11SDavid Ahern struct nlattr *tb[IFA_MAX+1];
5298ed6eff11SDavid Ahern struct ifaddrmsg *ifm;
5299ed6eff11SDavid Ahern int err, i;
5300ed6eff11SDavid Ahern
5301ed6eff11SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5302ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5303ed6eff11SDavid Ahern return -EINVAL;
5304ed6eff11SDavid Ahern }
5305ed6eff11SDavid Ahern
5306ed6eff11SDavid Ahern ifm = nlmsg_data(nlh);
5307ed6eff11SDavid Ahern if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5308ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5309ed6eff11SDavid Ahern return -EINVAL;
5310ed6eff11SDavid Ahern }
53116371a71fSDavid Ahern
53126371a71fSDavid Ahern fillargs->ifindex = ifm->ifa_index;
53136371a71fSDavid Ahern if (fillargs->ifindex) {
53146371a71fSDavid Ahern cb->answer_flags |= NLM_F_DUMP_FILTERED;
53156371a71fSDavid Ahern fillargs->flags |= NLM_F_DUMP_FILTERED;
5316ed6eff11SDavid Ahern }
5317ed6eff11SDavid Ahern
53188cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5319ed6eff11SDavid Ahern ifa_ipv6_policy, extack);
5320ed6eff11SDavid Ahern if (err < 0)
5321ed6eff11SDavid Ahern return err;
5322ed6eff11SDavid Ahern
5323ed6eff11SDavid Ahern for (i = 0; i <= IFA_MAX; ++i) {
5324ed6eff11SDavid Ahern if (!tb[i])
5325ed6eff11SDavid Ahern continue;
5326ed6eff11SDavid Ahern
5327ed6eff11SDavid Ahern if (i == IFA_TARGET_NETNSID) {
5328ed6eff11SDavid Ahern struct net *net;
5329ed6eff11SDavid Ahern
5330ed6eff11SDavid Ahern fillargs->netnsid = nla_get_s32(tb[i]);
5331ed6eff11SDavid Ahern net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5332ed6eff11SDavid Ahern if (IS_ERR(net)) {
5333bf4cc40eSBjørn Mork fillargs->netnsid = -1;
5334ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5335ed6eff11SDavid Ahern return PTR_ERR(net);
5336ed6eff11SDavid Ahern }
5337ed6eff11SDavid Ahern *tgt_net = net;
5338ed6eff11SDavid Ahern } else {
5339ed6eff11SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5340ed6eff11SDavid Ahern return -EINVAL;
5341ed6eff11SDavid Ahern }
5342ed6eff11SDavid Ahern }
5343ed6eff11SDavid Ahern
5344ed6eff11SDavid Ahern return 0;
5345ed6eff11SDavid Ahern }
5346ed6eff11SDavid Ahern
inet6_dump_addr(struct sk_buff * skb,struct netlink_callback * cb,enum addr_type_t type)5347234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5348234b27c3SEric Dumazet enum addr_type_t type)
5349234b27c3SEric Dumazet {
5350ed6eff11SDavid Ahern const struct nlmsghdr *nlh = cb->nlh;
53516ba1e6e8SDavid Ahern struct inet6_fill_args fillargs = {
53526ba1e6e8SDavid Ahern .portid = NETLINK_CB(cb->skb).portid,
53536ba1e6e8SDavid Ahern .seq = cb->nlh->nlmsg_seq,
53546ba1e6e8SDavid Ahern .flags = NLM_F_MULTI,
53556ba1e6e8SDavid Ahern .netnsid = -1,
53566ba1e6e8SDavid Ahern .type = type,
53576ba1e6e8SDavid Ahern };
535887117bafSRocco Yue struct net *tgt_net = sock_net(skb->sk);
5359fe884c2bSDavid Ahern int idx, s_idx, s_ip_idx;
5360234b27c3SEric Dumazet int h, s_h;
5361234b27c3SEric Dumazet struct net_device *dev;
5362234b27c3SEric Dumazet struct inet6_dev *idev;
5363234b27c3SEric Dumazet struct hlist_head *head;
5364242afaa6SDavid Ahern int err = 0;
5365234b27c3SEric Dumazet
5366234b27c3SEric Dumazet s_h = cb->args[0];
5367234b27c3SEric Dumazet s_idx = idx = cb->args[1];
5368fe884c2bSDavid Ahern s_ip_idx = cb->args[2];
5369234b27c3SEric Dumazet
5370ed6eff11SDavid Ahern if (cb->strict_check) {
5371ed6eff11SDavid Ahern err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
53726371a71fSDavid Ahern skb->sk, cb);
5373ed6eff11SDavid Ahern if (err < 0)
5374242afaa6SDavid Ahern goto put_tgt_net;
53756371a71fSDavid Ahern
5376242afaa6SDavid Ahern err = 0;
53776371a71fSDavid Ahern if (fillargs.ifindex) {
53786371a71fSDavid Ahern dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
5379242afaa6SDavid Ahern if (!dev) {
5380242afaa6SDavid Ahern err = -ENODEV;
5381242afaa6SDavid Ahern goto put_tgt_net;
5382242afaa6SDavid Ahern }
53836371a71fSDavid Ahern idev = __in6_dev_get(dev);
53846371a71fSDavid Ahern if (idev) {
53856371a71fSDavid Ahern err = in6_dump_addrs(idev, skb, cb, s_ip_idx,
53866371a71fSDavid Ahern &fillargs);
53871518039fSJakub Kicinski if (err > 0)
53881518039fSJakub Kicinski err = 0;
53896371a71fSDavid Ahern }
53906371a71fSDavid Ahern goto put_tgt_net;
53916371a71fSDavid Ahern }
53926ecf4c37SChristian Brauner }
53936ecf4c37SChristian Brauner
5394234b27c3SEric Dumazet rcu_read_lock();
5395cf761c81SEric Dumazet cb->seq = inet6_base_seq(tgt_net);
5396234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5397234b27c3SEric Dumazet idx = 0;
53986ecf4c37SChristian Brauner head = &tgt_net->dev_index_head[h];
5399b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) {
5400234b27c3SEric Dumazet if (idx < s_idx)
5401234b27c3SEric Dumazet goto cont;
54024b97efdfSPatrick McHardy if (h > s_h || idx > s_idx)
5403234b27c3SEric Dumazet s_ip_idx = 0;
5404e21e8467SStephen Hemminger idev = __in6_dev_get(dev);
5405e21e8467SStephen Hemminger if (!idev)
5406234b27c3SEric Dumazet goto cont;
5407234b27c3SEric Dumazet
5408fe884c2bSDavid Ahern if (in6_dump_addrs(idev, skb, cb, s_ip_idx,
54096ba1e6e8SDavid Ahern &fillargs) < 0)
5410234b27c3SEric Dumazet goto done;
54117562f876SPavel Emelianov cont:
54127562f876SPavel Emelianov idx++;
54131da177e4SLinus Torvalds }
5414234b27c3SEric Dumazet }
5415234b27c3SEric Dumazet done:
5416234b27c3SEric Dumazet rcu_read_unlock();
5417234b27c3SEric Dumazet cb->args[0] = h;
5418234b27c3SEric Dumazet cb->args[1] = idx;
54196371a71fSDavid Ahern put_tgt_net:
54206ba1e6e8SDavid Ahern if (fillargs.netnsid >= 0)
54216ecf4c37SChristian Brauner put_net(tgt_net);
5422234b27c3SEric Dumazet
54237c1e8a38SArthur Gautier return skb->len ? : err;
54241da177e4SLinus Torvalds }
54251da177e4SLinus Torvalds
inet6_dump_ifaddr(struct sk_buff * skb,struct netlink_callback * cb)54261da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
54271da177e4SLinus Torvalds {
54281da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR;
5429b854272bSDenis V. Lunev
54301da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type);
54311da177e4SLinus Torvalds }
54321da177e4SLinus Torvalds
inet6_dump_ifmcaddr(struct sk_buff * skb,struct netlink_callback * cb)54331da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
54341da177e4SLinus Torvalds {
54351da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR;
5436b854272bSDenis V. Lunev
54371da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type);
54381da177e4SLinus Torvalds }
54391da177e4SLinus Torvalds
54401da177e4SLinus Torvalds
inet6_dump_ifacaddr(struct sk_buff * skb,struct netlink_callback * cb)54411da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
54421da177e4SLinus Torvalds {
54431da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR;
5444b854272bSDenis V. Lunev
54451da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type);
54461da177e4SLinus Torvalds }
54471da177e4SLinus Torvalds
inet6_rtm_valid_getaddr_req(struct sk_buff * skb,const struct nlmsghdr * nlh,struct nlattr ** tb,struct netlink_ext_ack * extack)54484b1373deSJakub Kicinski static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
54494b1373deSJakub Kicinski const struct nlmsghdr *nlh,
54504b1373deSJakub Kicinski struct nlattr **tb,
54514b1373deSJakub Kicinski struct netlink_ext_ack *extack)
54524b1373deSJakub Kicinski {
54534b1373deSJakub Kicinski struct ifaddrmsg *ifm;
54544b1373deSJakub Kicinski int i, err;
54554b1373deSJakub Kicinski
54564b1373deSJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
54574b1373deSJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
54584b1373deSJakub Kicinski return -EINVAL;
54594b1373deSJakub Kicinski }
54604b1373deSJakub Kicinski
54612beb6d29SHangbin Liu if (!netlink_strict_get_check(skb))
54622beb6d29SHangbin Liu return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
54632beb6d29SHangbin Liu ifa_ipv6_policy, extack);
54642beb6d29SHangbin Liu
54654b1373deSJakub Kicinski ifm = nlmsg_data(nlh);
54664b1373deSJakub Kicinski if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
54674b1373deSJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
54684b1373deSJakub Kicinski return -EINVAL;
54694b1373deSJakub Kicinski }
54704b1373deSJakub Kicinski
54718cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
54724b1373deSJakub Kicinski ifa_ipv6_policy, extack);
54734b1373deSJakub Kicinski if (err)
54744b1373deSJakub Kicinski return err;
54754b1373deSJakub Kicinski
54764b1373deSJakub Kicinski for (i = 0; i <= IFA_MAX; i++) {
54774b1373deSJakub Kicinski if (!tb[i])
54784b1373deSJakub Kicinski continue;
54794b1373deSJakub Kicinski
54804b1373deSJakub Kicinski switch (i) {
54814b1373deSJakub Kicinski case IFA_TARGET_NETNSID:
54824b1373deSJakub Kicinski case IFA_ADDRESS:
54834b1373deSJakub Kicinski case IFA_LOCAL:
54844b1373deSJakub Kicinski break;
54854b1373deSJakub Kicinski default:
54864b1373deSJakub Kicinski NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
54874b1373deSJakub Kicinski return -EINVAL;
54884b1373deSJakub Kicinski }
54894b1373deSJakub Kicinski }
54904b1373deSJakub Kicinski
54914b1373deSJakub Kicinski return 0;
54924b1373deSJakub Kicinski }
54934b1373deSJakub Kicinski
inet6_rtm_getaddr(struct sk_buff * in_skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)5494c21ef3e3SDavid Ahern static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5495c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
54966c223828SNoriaki TAKAMIYA {
549787117bafSRocco Yue struct net *tgt_net = sock_net(in_skb->sk);
5498203651b6SChristian Brauner struct inet6_fill_args fillargs = {
5499203651b6SChristian Brauner .portid = NETLINK_CB(in_skb).portid,
5500203651b6SChristian Brauner .seq = nlh->nlmsg_seq,
5501203651b6SChristian Brauner .event = RTM_NEWADDR,
5502203651b6SChristian Brauner .flags = 0,
5503203651b6SChristian Brauner .netnsid = -1,
5504203651b6SChristian Brauner };
55051b29fc2cSThomas Graf struct ifaddrmsg *ifm;
55061b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1];
5507caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer;
55086c223828SNoriaki TAKAMIYA struct net_device *dev = NULL;
55096c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa;
55106c223828SNoriaki TAKAMIYA struct sk_buff *skb;
55116c223828SNoriaki TAKAMIYA int err;
55126c223828SNoriaki TAKAMIYA
55134b1373deSJakub Kicinski err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
55141b29fc2cSThomas Graf if (err < 0)
5515c24675f8SFlorian Westphal return err;
55166c223828SNoriaki TAKAMIYA
55176ecf4c37SChristian Brauner if (tb[IFA_TARGET_NETNSID]) {
5518203651b6SChristian Brauner fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
55196ecf4c37SChristian Brauner
55206ecf4c37SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
5521203651b6SChristian Brauner fillargs.netnsid);
55226ecf4c37SChristian Brauner if (IS_ERR(tgt_net))
55236ecf4c37SChristian Brauner return PTR_ERR(tgt_net);
55246ecf4c37SChristian Brauner }
55256ecf4c37SChristian Brauner
5526caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
552744112bc5SEric Dumazet if (!addr) {
552844112bc5SEric Dumazet err = -EINVAL;
552944112bc5SEric Dumazet goto errout;
553044112bc5SEric Dumazet }
55311b29fc2cSThomas Graf ifm = nlmsg_data(nlh);
55326c223828SNoriaki TAKAMIYA if (ifm->ifa_index)
55336ecf4c37SChristian Brauner dev = dev_get_by_index(tgt_net, ifm->ifa_index);
55346c223828SNoriaki TAKAMIYA
55356ecf4c37SChristian Brauner ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
5536e21e8467SStephen Hemminger if (!ifa) {
55371b29fc2cSThomas Graf err = -EADDRNOTAVAIL;
55381b29fc2cSThomas Graf goto errout;
55396c223828SNoriaki TAKAMIYA }
55406c223828SNoriaki TAKAMIYA
5541e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5542e21e8467SStephen Hemminger if (!skb) {
55431b29fc2cSThomas Graf err = -ENOBUFS;
55441b29fc2cSThomas Graf goto errout_ifa;
55451b29fc2cSThomas Graf }
55461b29fc2cSThomas Graf
5547203651b6SChristian Brauner err = inet6_fill_ifaddr(skb, ifa, &fillargs);
554826932566SPatrick McHardy if (err < 0) {
554926932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
555026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE);
555126932566SPatrick McHardy kfree_skb(skb);
555226932566SPatrick McHardy goto errout_ifa;
555326932566SPatrick McHardy }
55546ecf4c37SChristian Brauner err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
55551b29fc2cSThomas Graf errout_ifa:
55566c223828SNoriaki TAKAMIYA in6_ifa_put(ifa);
55571b29fc2cSThomas Graf errout:
5558c24675f8SFlorian Westphal dev_put(dev);
5559203651b6SChristian Brauner if (fillargs.netnsid >= 0)
55606ecf4c37SChristian Brauner put_net(tgt_net);
55616ecf4c37SChristian Brauner
55626c223828SNoriaki TAKAMIYA return err;
55636c223828SNoriaki TAKAMIYA }
55646c223828SNoriaki TAKAMIYA
inet6_ifa_notify(int event,struct inet6_ifaddr * ifa)55651da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
55661da177e4SLinus Torvalds {
55671da177e4SLinus Torvalds struct sk_buff *skb;
5568c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev);
5569203651b6SChristian Brauner struct inet6_fill_args fillargs = {
5570203651b6SChristian Brauner .portid = 0,
5571203651b6SChristian Brauner .seq = 0,
5572203651b6SChristian Brauner .event = event,
5573203651b6SChristian Brauner .flags = 0,
5574203651b6SChristian Brauner .netnsid = -1,
5575203651b6SChristian Brauner };
55765d620266SThomas Graf int err = -ENOBUFS;
55771da177e4SLinus Torvalds
55780ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
557963159f29SIan Morris if (!skb)
55805d620266SThomas Graf goto errout;
55815d620266SThomas Graf
5582203651b6SChristian Brauner err = inet6_fill_ifaddr(skb, ifa, &fillargs);
558326932566SPatrick McHardy if (err < 0) {
558426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
558526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE);
558626932566SPatrick McHardy kfree_skb(skb);
558726932566SPatrick McHardy goto errout;
558826932566SPatrick McHardy }
55891ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
55901ce85fe4SPablo Neira Ayuso return;
55915d620266SThomas Graf errout:
55925d620266SThomas Graf if (err < 0)
55936fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
55941da177e4SLinus Torvalds }
55951da177e4SLinus Torvalds
ipv6_store_devconf(struct ipv6_devconf * cnf,__s32 * array,int bytes)5596b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
55971da177e4SLinus Torvalds __s32 *array, int bytes)
55981da177e4SLinus Torvalds {
559904561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4));
560004561c1fSThomas Graf
56011da177e4SLinus Torvalds memset(array, 0, bytes);
56021da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding;
56031da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
56041da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6;
56051da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
56061da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
56071da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf;
56081da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
56091da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
561093908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] =
561193908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval);
5612bd11f074SMaciej Żenczykowski array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5613bd11f074SMaciej Żenczykowski jiffies_to_msecs(cnf->rtr_solicit_max_interval);
561493908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] =
561593908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay);
56161da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
5617fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5618fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5619fc4eba58SHannes Frederic Sowa array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5620fc4eba58SHannes Frederic Sowa jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
56211da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
56221da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
56231da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
56241da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
56251da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
56261da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
562765f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
56286b2e04bcSPraveen Chaudhary array[DEVCONF_RA_DEFRTR_METRIC] = cnf->ra_defrtr_metric;
56298013d1d7SHangbin Liu array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
5630c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
5631930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
5632930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
563393908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] =
563493908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval);
5635fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
5636bbea124bSJoel Scherpelz array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
563709c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
563809c884d4SYOSHIFUJI Hideaki #endif
5639930d6ff2SYOSHIFUJI Hideaki #endif
5640fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
56410bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
564295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
564395c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
56447fd2561eSErik Kline array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
564595c385b4SNeil Horman #endif
56467bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
5647145c7a79SEric Dumazet array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding);
56487bc570c8SYOSHIFUJI Hideaki #endif
5649778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
56501b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
5651c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
56525cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
5653b800c3b9SHannes Frederic Sowa array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
5654d9333196SBen Greear array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
5655c2943f14SHarout Hedeshian array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
565635103d11SAndy Gospodarek array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
56573d1bec99SHannes Frederic Sowa /* we omit DEVCONF_STABLE_SECRET for now */
56583985e8a3SErik Kline array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
5659abbc3043SJohannes Berg array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
56607a02bf89SJohannes Berg array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
5661f1705ec1SDavid Ahern array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
56621ababebaSDavid Lebrun array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5663bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
5664bf355b8dSDavid Lebrun array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5665bf355b8dSDavid Lebrun #endif
5666adc176c5SErik Nordmark array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5667d35a00b8SFelix Jia array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5668df789fe7SDavid Forster array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
56692210d6b2SMaciej Żenczykowski array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
56708610c7c6SAlexander Aring array[DEVCONF_RPL_SEG_ENABLED] = cnf->rpl_seg_enabled;
56719ee11f0fSJustin Iurman array[DEVCONF_IOAM6_ENABLED] = cnf->ioam6_enabled;
56729ee11f0fSJustin Iurman array[DEVCONF_IOAM6_ID] = cnf->ioam6_id;
56739ee11f0fSJustin Iurman array[DEVCONF_IOAM6_ID_WIDE] = cnf->ioam6_id_wide;
567418ac597aSJames Prestwood array[DEVCONF_NDISC_EVICT_NOCARRIER] = cnf->ndisc_evict_nocarrier;
56753e0b8f52SArun Ajith S array[DEVCONF_ACCEPT_UNTRACKED_NA] = cnf->accept_untracked_na;
56765027d54aSPatrick Rohr array[DEVCONF_ACCEPT_RA_MIN_LFT] = cnf->accept_ra_min_lft;
56771da177e4SLinus Torvalds }
56781da177e4SLinus Torvalds
inet6_ifla6_size(void)5679b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
5680b382b191SThomas Graf {
5681b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */
5682b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo))
5683b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5684b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5685f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5686bdd72f41SSabrina Dubroca + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5687bdd72f41SSabrina Dubroca + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
568849b99da2SRocco Yue + nla_total_size(4) /* IFLA_INET6_RA_MTU */
5689bdd72f41SSabrina Dubroca + 0;
5690b382b191SThomas Graf }
5691b382b191SThomas Graf
inet6_if_nlmsg_size(void)5692339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
5693339bf98fSThomas Graf {
5694339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5695339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5696339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5697339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */
5698339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */
56990344338bSAndy Gospodarek + nla_total_size(1) /* IFLA_OPERSTATE */
5700b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5701339bf98fSThomas Graf }
5702c5396a31SYOSHIFUJI Hideaki
__snmp6_fill_statsdev(u64 * stats,atomic_long_t * mib,int bytes)5703be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5704aca05671SJia He int bytes)
57057f7d9a6bSHerbert Xu {
57067f7d9a6bSHerbert Xu int i;
5707aca05671SJia He int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
57087f7d9a6bSHerbert Xu BUG_ON(pad < 0);
57097f7d9a6bSHerbert Xu
57107f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */
5711aca05671SJia He put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5712aca05671SJia He for (i = 1; i < ICMP6_MIB_MAX; i++)
5713be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
57147f7d9a6bSHerbert Xu
5715aca05671SJia He memset(&stats[ICMP6_MIB_MAX], 0, pad);
57167f7d9a6bSHerbert Xu }
57177f7d9a6bSHerbert Xu
__snmp6_fill_stats64(u64 * stats,void __percpu * mib,int bytes,size_t syncpoff)5718698365faSWANG Cong static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5719a3a77372SRaghavendra K T int bytes, size_t syncpoff)
57204ce3c183SEric Dumazet {
5721a3a77372SRaghavendra K T int i, c;
5722a3a77372SRaghavendra K T u64 buff[IPSTATS_MIB_MAX];
5723a3a77372SRaghavendra K T int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5724a3a77372SRaghavendra K T
57254ce3c183SEric Dumazet BUG_ON(pad < 0);
57264ce3c183SEric Dumazet
5727a3a77372SRaghavendra K T memset(buff, 0, sizeof(buff));
5728a3a77372SRaghavendra K T buff[0] = IPSTATS_MIB_MAX;
57294ce3c183SEric Dumazet
5730a3a77372SRaghavendra K T for_each_possible_cpu(c) {
5731a3a77372SRaghavendra K T for (i = 1; i < IPSTATS_MIB_MAX; i++)
5732a3a77372SRaghavendra K T buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5733a3a77372SRaghavendra K T }
5734a3a77372SRaghavendra K T
5735a3a77372SRaghavendra K T memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5736a3a77372SRaghavendra K T memset(&stats[IPSTATS_MIB_MAX], 0, pad);
57374ce3c183SEric Dumazet }
57384ce3c183SEric Dumazet
snmp6_fill_stats(u64 * stats,struct inet6_dev * idev,int attrtype,int bytes)57397f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
57407f7d9a6bSHerbert Xu int bytes)
57417f7d9a6bSHerbert Xu {
57427f7d9a6bSHerbert Xu switch (attrtype) {
57437f7d9a6bSHerbert Xu case IFLA_INET6_STATS:
5744a3a77372SRaghavendra K T __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5745a3a77372SRaghavendra K T offsetof(struct ipstats_mib, syncp));
57467f7d9a6bSHerbert Xu break;
57477f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS:
5748aca05671SJia He __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
57497f7d9a6bSHerbert Xu break;
57507f7d9a6bSHerbert Xu }
57517f7d9a6bSHerbert Xu }
57527f7d9a6bSHerbert Xu
inet6_fill_ifla6_attrs(struct sk_buff * skb,struct inet6_dev * idev,u32 ext_filter_mask)5753d5566fd7SSowmini Varadhan static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5754d5566fd7SSowmini Varadhan u32 ext_filter_mask)
57551da177e4SLinus Torvalds {
5756bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla;
57571da177e4SLinus Torvalds struct ifla_cacheinfo ci;
57581da177e4SLinus Torvalds
5759c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5760c78679e8SDavid S. Miller goto nla_put_failure;
57611da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN;
576224912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp);
576324912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
57641f9248e5SJiri Pirko ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5765c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5766c78679e8SDavid S. Miller goto nla_put_failure;
5767bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
576863159f29SIan Morris if (!nla)
576904561c1fSThomas Graf goto nla_put_failure;
5770bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
57711da177e4SLinus Torvalds
5772bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */
5773bf99f1bdSYOSHIFUJI Hideaki
5774d5566fd7SSowmini Varadhan if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5775d5566fd7SSowmini Varadhan return 0;
5776d5566fd7SSowmini Varadhan
5777bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
577863159f29SIan Morris if (!nla)
5779bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure;
5780bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5781bf99f1bdSYOSHIFUJI Hideaki
5782bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
578363159f29SIan Morris if (!nla)
5784bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure;
5785bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
57861da177e4SLinus Torvalds
5787f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
578863159f29SIan Morris if (!nla)
5789f53adae4SDaniel Borkmann goto nla_put_failure;
5790f53adae4SDaniel Borkmann read_lock_bh(&idev->lock);
5791f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5792f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock);
5793f53adae4SDaniel Borkmann
57940d7982ceSNicolas Dichtel if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
57950d7982ceSNicolas Dichtel goto nla_put_failure;
57960d7982ceSNicolas Dichtel
579749b99da2SRocco Yue if (idev->ra_mtu &&
579849b99da2SRocco Yue nla_put_u32(skb, IFLA_INET6_RA_MTU, idev->ra_mtu))
579949b99da2SRocco Yue goto nla_put_failure;
580049b99da2SRocco Yue
5801b382b191SThomas Graf return 0;
5802b382b191SThomas Graf
5803b382b191SThomas Graf nla_put_failure:
5804b382b191SThomas Graf return -EMSGSIZE;
5805b382b191SThomas Graf }
5806b382b191SThomas Graf
inet6_get_link_af_size(const struct net_device * dev,u32 ext_filter_mask)5807b1974ed0SArad, Ronen static size_t inet6_get_link_af_size(const struct net_device *dev,
5808b1974ed0SArad, Ronen u32 ext_filter_mask)
5809b382b191SThomas Graf {
5810b382b191SThomas Graf if (!__in6_dev_get(dev))
5811b382b191SThomas Graf return 0;
5812b382b191SThomas Graf
5813b382b191SThomas Graf return inet6_ifla6_size();
5814b382b191SThomas Graf }
5815b382b191SThomas Graf
inet6_fill_link_af(struct sk_buff * skb,const struct net_device * dev,u32 ext_filter_mask)5816d5566fd7SSowmini Varadhan static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5817d5566fd7SSowmini Varadhan u32 ext_filter_mask)
5818b382b191SThomas Graf {
5819b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev);
5820b382b191SThomas Graf
5821b382b191SThomas Graf if (!idev)
5822b382b191SThomas Graf return -ENODATA;
5823b382b191SThomas Graf
5824d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5825b382b191SThomas Graf return -EMSGSIZE;
5826b382b191SThomas Graf
5827b382b191SThomas Graf return 0;
5828b382b191SThomas Graf }
5829b382b191SThomas Graf
inet6_set_iftoken(struct inet6_dev * idev,struct in6_addr * token,struct netlink_ext_ack * extack)58303583a4e8SStephen Hemminger static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token,
58313583a4e8SStephen Hemminger struct netlink_ext_ack *extack)
5832f53adae4SDaniel Borkmann {
5833f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp;
5834f53adae4SDaniel Borkmann struct net_device *dev = idev->dev;
583547e27d5eSDaniel Borkmann bool clear_token, update_rs = false;
5836dc848292SHannes Frederic Sowa struct in6_addr ll_addr;
5837f53adae4SDaniel Borkmann
5838c15b1ccaSHannes Frederic Sowa ASSERT_RTNL();
5839c15b1ccaSHannes Frederic Sowa
584063159f29SIan Morris if (!token)
5841f53adae4SDaniel Borkmann return -EINVAL;
58423583a4e8SStephen Hemminger
58433583a4e8SStephen Hemminger if (dev->flags & IFF_LOOPBACK) {
58443583a4e8SStephen Hemminger NL_SET_ERR_MSG_MOD(extack, "Device is loopback");
5845f53adae4SDaniel Borkmann return -EINVAL;
58463583a4e8SStephen Hemminger }
58473583a4e8SStephen Hemminger
58483583a4e8SStephen Hemminger if (dev->flags & IFF_NOARP) {
58493583a4e8SStephen Hemminger NL_SET_ERR_MSG_MOD(extack,
58503583a4e8SStephen Hemminger "Device does not do neighbour discovery");
5851f53adae4SDaniel Borkmann return -EINVAL;
58523583a4e8SStephen Hemminger }
58533583a4e8SStephen Hemminger
58543583a4e8SStephen Hemminger if (!ipv6_accept_ra(idev)) {
58553583a4e8SStephen Hemminger NL_SET_ERR_MSG_MOD(extack,
58563583a4e8SStephen Hemminger "Router advertisement is disabled on device");
5857f53adae4SDaniel Borkmann return -EINVAL;
58583583a4e8SStephen Hemminger }
58593583a4e8SStephen Hemminger
58603583a4e8SStephen Hemminger if (idev->cnf.rtr_solicits == 0) {
58613583a4e8SStephen Hemminger NL_SET_ERR_MSG(extack,
58623583a4e8SStephen Hemminger "Router solicitation is disabled on device");
58633583a4e8SStephen Hemminger return -EINVAL;
58643583a4e8SStephen Hemminger }
5865f53adae4SDaniel Borkmann
5866f53adae4SDaniel Borkmann write_lock_bh(&idev->lock);
5867f53adae4SDaniel Borkmann
5868f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5869f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5870f53adae4SDaniel Borkmann
5871f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock);
5872f53adae4SDaniel Borkmann
587347e27d5eSDaniel Borkmann clear_token = ipv6_addr_any(token);
587447e27d5eSDaniel Borkmann if (clear_token)
587547e27d5eSDaniel Borkmann goto update_lft;
587647e27d5eSDaniel Borkmann
5877dc848292SHannes Frederic Sowa if (!idev->dead && (idev->if_flags & IF_READY) &&
5878dc848292SHannes Frederic Sowa !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5879dc848292SHannes Frederic Sowa IFA_F_OPTIMISTIC)) {
5880fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care
5881fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here.
5882fc403832SDaniel Borkmann */
5883f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5884fc403832SDaniel Borkmann update_rs = true;
5885fc403832SDaniel Borkmann }
5886f53adae4SDaniel Borkmann
588747e27d5eSDaniel Borkmann update_lft:
5888f53adae4SDaniel Borkmann write_lock_bh(&idev->lock);
5889fc403832SDaniel Borkmann
589077ecaaceSHannes Frederic Sowa if (update_rs) {
5891f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT;
5892bd11f074SMaciej Żenczykowski idev->rs_interval = rfc3315_s14_backoff_init(
5893bd11f074SMaciej Żenczykowski idev->cnf.rtr_solicit_interval);
589477ecaaceSHannes Frederic Sowa idev->rs_probes = 1;
5895bd11f074SMaciej Żenczykowski addrconf_mod_rs_timer(idev, idev->rs_interval);
589677ecaaceSHannes Frederic Sowa }
5897f53adae4SDaniel Borkmann
5898f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */
5899f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) {
5900f53adae4SDaniel Borkmann spin_lock(&ifp->lock);
5901617fe29dSDaniel Borkmann if (ifp->tokenized) {
5902f53adae4SDaniel Borkmann ifp->valid_lft = 0;
5903f53adae4SDaniel Borkmann ifp->prefered_lft = 0;
5904f53adae4SDaniel Borkmann }
5905f53adae4SDaniel Borkmann spin_unlock(&ifp->lock);
5906f53adae4SDaniel Borkmann }
5907f53adae4SDaniel Borkmann
5908f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock);
5909b2ed64a9SLubomir Rintel inet6_ifinfo_notify(RTM_NEWLINK, idev);
59108805d13fSEric Dumazet addrconf_verify_rtnl(dev_net(dev));
5911f53adae4SDaniel Borkmann return 0;
5912f53adae4SDaniel Borkmann }
5913f53adae4SDaniel Borkmann
591411b1f828SDaniel Borkmann static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
591511b1f828SDaniel Borkmann [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
591611b1f828SDaniel Borkmann [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
591749b99da2SRocco Yue [IFLA_INET6_RA_MTU] = { .type = NLA_REJECT,
591849b99da2SRocco Yue .reject_message =
591949b99da2SRocco Yue "IFLA_INET6_RA_MTU can not be set" },
592011b1f828SDaniel Borkmann };
592111b1f828SDaniel Borkmann
check_addr_gen_mode(int mode)5922d35a00b8SFelix Jia static int check_addr_gen_mode(int mode)
5923d35a00b8SFelix Jia {
5924d35a00b8SFelix Jia if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5925d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_NONE &&
5926d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5927d35a00b8SFelix Jia mode != IN6_ADDR_GEN_MODE_RANDOM)
5928d35a00b8SFelix Jia return -EINVAL;
5929d35a00b8SFelix Jia return 1;
5930d35a00b8SFelix Jia }
5931d35a00b8SFelix Jia
check_stable_privacy(struct inet6_dev * idev,struct net * net,int mode)5932d35a00b8SFelix Jia static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5933d35a00b8SFelix Jia int mode)
5934d35a00b8SFelix Jia {
5935d35a00b8SFelix Jia if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5936d35a00b8SFelix Jia !idev->cnf.stable_secret.initialized &&
5937d35a00b8SFelix Jia !net->ipv6.devconf_dflt->stable_secret.initialized)
5938d35a00b8SFelix Jia return -EINVAL;
5939d35a00b8SFelix Jia return 1;
5940d35a00b8SFelix Jia }
5941d35a00b8SFelix Jia
inet6_validate_link_af(const struct net_device * dev,const struct nlattr * nla,struct netlink_ext_ack * extack)59427dc2bccaSMaxim Mikityanskiy static int inet6_validate_link_af(const struct net_device *dev,
59438679c31eSRocco Yue const struct nlattr *nla,
59448679c31eSRocco Yue struct netlink_ext_ack *extack)
5945f53adae4SDaniel Borkmann {
5946f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1];
59477dc2bccaSMaxim Mikityanskiy struct inet6_dev *idev = NULL;
59487dc2bccaSMaxim Mikityanskiy int err;
5949f53adae4SDaniel Borkmann
59507dc2bccaSMaxim Mikityanskiy if (dev) {
59517dc2bccaSMaxim Mikityanskiy idev = __in6_dev_get(dev);
5952f53adae4SDaniel Borkmann if (!idev)
5953f53adae4SDaniel Borkmann return -EAFNOSUPPORT;
59547dc2bccaSMaxim Mikityanskiy }
59557dc2bccaSMaxim Mikityanskiy
59567dc2bccaSMaxim Mikityanskiy err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
59578679c31eSRocco Yue inet6_af_policy, extack);
59587dc2bccaSMaxim Mikityanskiy if (err)
59597dc2bccaSMaxim Mikityanskiy return err;
59607dc2bccaSMaxim Mikityanskiy
59617dc2bccaSMaxim Mikityanskiy if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
59627dc2bccaSMaxim Mikityanskiy return -EINVAL;
59637dc2bccaSMaxim Mikityanskiy
59647dc2bccaSMaxim Mikityanskiy if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
59657dc2bccaSMaxim Mikityanskiy u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
59667dc2bccaSMaxim Mikityanskiy
59677dc2bccaSMaxim Mikityanskiy if (check_addr_gen_mode(mode) < 0)
59687dc2bccaSMaxim Mikityanskiy return -EINVAL;
59697dc2bccaSMaxim Mikityanskiy if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
59707dc2bccaSMaxim Mikityanskiy return -EINVAL;
59717dc2bccaSMaxim Mikityanskiy }
59727dc2bccaSMaxim Mikityanskiy
59737dc2bccaSMaxim Mikityanskiy return 0;
59747dc2bccaSMaxim Mikityanskiy }
59757dc2bccaSMaxim Mikityanskiy
inet6_set_link_af(struct net_device * dev,const struct nlattr * nla,struct netlink_ext_ack * extack)59763583a4e8SStephen Hemminger static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla,
59773583a4e8SStephen Hemminger struct netlink_ext_ack *extack)
59787dc2bccaSMaxim Mikityanskiy {
59797dc2bccaSMaxim Mikityanskiy struct inet6_dev *idev = __in6_dev_get(dev);
59807dc2bccaSMaxim Mikityanskiy struct nlattr *tb[IFLA_INET6_MAX + 1];
59817dc2bccaSMaxim Mikityanskiy int err;
5982f53adae4SDaniel Borkmann
5983db3fa271SEric Dumazet if (!idev)
5984db3fa271SEric Dumazet return -EAFNOSUPPORT;
5985db3fa271SEric Dumazet
59868cb08174SJohannes Berg if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
59875ac6b198SZheng Yongjun return -EINVAL;
5988f53adae4SDaniel Borkmann
5989bc91b0f0SJiri Pirko if (tb[IFLA_INET6_TOKEN]) {
59903583a4e8SStephen Hemminger err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]),
59913583a4e8SStephen Hemminger extack);
5992bc91b0f0SJiri Pirko if (err)
5993bc91b0f0SJiri Pirko return err;
5994bc91b0f0SJiri Pirko }
5995bc91b0f0SJiri Pirko
5996bc91b0f0SJiri Pirko if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5997bc91b0f0SJiri Pirko u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5998bc91b0f0SJiri Pirko
5999d35a00b8SFelix Jia idev->cnf.addr_gen_mode = mode;
6000bc91b0f0SJiri Pirko }
6001f53adae4SDaniel Borkmann
60027dc2bccaSMaxim Mikityanskiy return 0;
6003f53adae4SDaniel Borkmann }
6004f53adae4SDaniel Borkmann
inet6_fill_ifinfo(struct sk_buff * skb,struct inet6_dev * idev,u32 portid,u32 seq,int event,unsigned int flags)6005b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
600615e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags)
6007b382b191SThomas Graf {
6008b382b191SThomas Graf struct net_device *dev = idev->dev;
6009b382b191SThomas Graf struct ifinfomsg *hdr;
6010b382b191SThomas Graf struct nlmsghdr *nlh;
6011b382b191SThomas Graf void *protoinfo;
6012b382b191SThomas Graf
601315e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
601463159f29SIan Morris if (!nlh)
6015b382b191SThomas Graf return -EMSGSIZE;
6016b382b191SThomas Graf
6017b382b191SThomas Graf hdr = nlmsg_data(nlh);
6018b382b191SThomas Graf hdr->ifi_family = AF_INET6;
6019b382b191SThomas Graf hdr->__ifi_pad = 0;
6020b382b191SThomas Graf hdr->ifi_type = dev->type;
6021b382b191SThomas Graf hdr->ifi_index = dev->ifindex;
6022b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev);
6023b382b191SThomas Graf hdr->ifi_change = 0;
6024b382b191SThomas Graf
6025c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
6026c78679e8SDavid S. Miller (dev->addr_len &&
6027c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
6028c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
6029a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) &&
60300344338bSAndy Gospodarek nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
60310344338bSAndy Gospodarek nla_put_u8(skb, IFLA_OPERSTATE,
60321c2b0f08SEric Dumazet netif_running(dev) ? READ_ONCE(dev->operstate) : IF_OPER_DOWN))
6033c78679e8SDavid S. Miller goto nla_put_failure;
6034ae0be8deSMichal Kubecek protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
603563159f29SIan Morris if (!protoinfo)
6036b382b191SThomas Graf goto nla_put_failure;
6037b382b191SThomas Graf
6038d5566fd7SSowmini Varadhan if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
6039b382b191SThomas Graf goto nla_put_failure;
6040b382b191SThomas Graf
604104561c1fSThomas Graf nla_nest_end(skb, protoinfo);
6042053c095aSJohannes Berg nlmsg_end(skb, nlh);
6043053c095aSJohannes Berg return 0;
60441da177e4SLinus Torvalds
604504561c1fSThomas Graf nla_put_failure:
604626932566SPatrick McHardy nlmsg_cancel(skb, nlh);
604726932566SPatrick McHardy return -EMSGSIZE;
60481da177e4SLinus Torvalds }
60491da177e4SLinus Torvalds
inet6_valid_dump_ifinfo(const struct nlmsghdr * nlh,struct netlink_ext_ack * extack)6050786e0007SDavid Ahern static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
6051786e0007SDavid Ahern struct netlink_ext_ack *extack)
6052786e0007SDavid Ahern {
6053786e0007SDavid Ahern struct ifinfomsg *ifm;
6054786e0007SDavid Ahern
6055786e0007SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
6056786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
6057786e0007SDavid Ahern return -EINVAL;
6058786e0007SDavid Ahern }
6059786e0007SDavid Ahern
6060786e0007SDavid Ahern if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
6061786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
6062786e0007SDavid Ahern return -EINVAL;
6063786e0007SDavid Ahern }
6064786e0007SDavid Ahern
6065786e0007SDavid Ahern ifm = nlmsg_data(nlh);
6066786e0007SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
6067786e0007SDavid Ahern ifm->ifi_change || ifm->ifi_index) {
6068786e0007SDavid Ahern NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
6069786e0007SDavid Ahern return -EINVAL;
6070786e0007SDavid Ahern }
6071786e0007SDavid Ahern
6072786e0007SDavid Ahern return 0;
6073786e0007SDavid Ahern }
6074786e0007SDavid Ahern
inet6_dump_ifinfo(struct sk_buff * skb,struct netlink_callback * cb)60751da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
60761da177e4SLinus Torvalds {
60773b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk);
607884d2697dSEric Dumazet int h, s_h;
6079434a8a58SDavid S. Miller int idx = 0, s_idx;
60801da177e4SLinus Torvalds struct net_device *dev;
60811da177e4SLinus Torvalds struct inet6_dev *idev;
608284d2697dSEric Dumazet struct hlist_head *head;
60831da177e4SLinus Torvalds
6084786e0007SDavid Ahern /* only requests using strict checking can pass data to
6085786e0007SDavid Ahern * influence the dump
6086786e0007SDavid Ahern */
6087786e0007SDavid Ahern if (cb->strict_check) {
6088786e0007SDavid Ahern int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
6089786e0007SDavid Ahern
6090786e0007SDavid Ahern if (err < 0)
6091786e0007SDavid Ahern return err;
6092786e0007SDavid Ahern }
6093786e0007SDavid Ahern
609484d2697dSEric Dumazet s_h = cb->args[0];
609584d2697dSEric Dumazet s_idx = cb->args[1];
609684d2697dSEric Dumazet
609784d2697dSEric Dumazet rcu_read_lock();
609884d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
60997562f876SPavel Emelianov idx = 0;
610084d2697dSEric Dumazet head = &net->dev_index_head[h];
6101b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) {
61021da177e4SLinus Torvalds if (idx < s_idx)
61037562f876SPavel Emelianov goto cont;
610484d2697dSEric Dumazet idev = __in6_dev_get(dev);
610584d2697dSEric Dumazet if (!idev)
61067562f876SPavel Emelianov goto cont;
610784d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev,
610815e47304SEric W. Biederman NETLINK_CB(cb->skb).portid,
610984d2697dSEric Dumazet cb->nlh->nlmsg_seq,
6110053c095aSJohannes Berg RTM_NEWLINK, NLM_F_MULTI) < 0)
611184d2697dSEric Dumazet goto out;
61127562f876SPavel Emelianov cont:
61137562f876SPavel Emelianov idx++;
61141da177e4SLinus Torvalds }
611584d2697dSEric Dumazet }
611684d2697dSEric Dumazet out:
611784d2697dSEric Dumazet rcu_read_unlock();
611884d2697dSEric Dumazet cb->args[1] = idx;
611984d2697dSEric Dumazet cb->args[0] = h;
61201da177e4SLinus Torvalds
61211da177e4SLinus Torvalds return skb->len;
61221da177e4SLinus Torvalds }
61231da177e4SLinus Torvalds
inet6_ifinfo_notify(int event,struct inet6_dev * idev)61241da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
61251da177e4SLinus Torvalds {
61261da177e4SLinus Torvalds struct sk_buff *skb;
6127c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev);
61288d7a76c9SThomas Graf int err = -ENOBUFS;
61291da177e4SLinus Torvalds
6130339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
613163159f29SIan Morris if (!skb)
61328d7a76c9SThomas Graf goto errout;
61338d7a76c9SThomas Graf
61348d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
613526932566SPatrick McHardy if (err < 0) {
613626932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
613726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE);
613826932566SPatrick McHardy kfree_skb(skb);
613926932566SPatrick McHardy goto errout;
614026932566SPatrick McHardy }
61415f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
61421ce85fe4SPablo Neira Ayuso return;
61438d7a76c9SThomas Graf errout:
61448d7a76c9SThomas Graf if (err < 0)
61455f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
61461da177e4SLinus Torvalds }
61471da177e4SLinus Torvalds
inet6_prefix_nlmsg_size(void)6148339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
6149339bf98fSThomas Graf {
6150339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg))
6151339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr))
6152339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo));
6153339bf98fSThomas Graf }
6154c5396a31SYOSHIFUJI Hideaki
inet6_fill_prefix(struct sk_buff * skb,struct inet6_dev * idev,struct prefix_info * pinfo,u32 portid,u32 seq,int event,unsigned int flags)61551da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
615615e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq,
6157b6544c0bSJamal Hadi Salim int event, unsigned int flags)
61581da177e4SLinus Torvalds {
61591da177e4SLinus Torvalds struct prefixmsg *pmsg;
61601da177e4SLinus Torvalds struct nlmsghdr *nlh;
61611da177e4SLinus Torvalds struct prefix_cacheinfo ci;
61621da177e4SLinus Torvalds
616315e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
616463159f29SIan Morris if (!nlh)
616526932566SPatrick McHardy return -EMSGSIZE;
61666051e2f4SThomas Graf
61676051e2f4SThomas Graf pmsg = nlmsg_data(nlh);
61681da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6;
61698a47077aSPatrick McHardy pmsg->prefix_pad1 = 0;
61708a47077aSPatrick McHardy pmsg->prefix_pad2 = 0;
61711da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex;
61721da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len;
61731da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type;
61748a47077aSPatrick McHardy pmsg->prefix_pad3 = 0;
617592d813f7SMaciej Żenczykowski pmsg->prefix_flags = pinfo->flags;
61761da177e4SLinus Torvalds
6177c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
6178c78679e8SDavid S. Miller goto nla_put_failure;
61791da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered);
61801da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid);
6181c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
6182c78679e8SDavid S. Miller goto nla_put_failure;
6183053c095aSJohannes Berg nlmsg_end(skb, nlh);
6184053c095aSJohannes Berg return 0;
61851da177e4SLinus Torvalds
61866051e2f4SThomas Graf nla_put_failure:
618726932566SPatrick McHardy nlmsg_cancel(skb, nlh);
618826932566SPatrick McHardy return -EMSGSIZE;
61891da177e4SLinus Torvalds }
61901da177e4SLinus Torvalds
inet6_prefix_notify(int event,struct inet6_dev * idev,struct prefix_info * pinfo)61911da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
61921da177e4SLinus Torvalds struct prefix_info *pinfo)
61931da177e4SLinus Torvalds {
61941da177e4SLinus Torvalds struct sk_buff *skb;
6195c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev);
61968c384bfaSThomas Graf int err = -ENOBUFS;
61971da177e4SLinus Torvalds
6198339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
619963159f29SIan Morris if (!skb)
62008c384bfaSThomas Graf goto errout;
62018c384bfaSThomas Graf
62028c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
620326932566SPatrick McHardy if (err < 0) {
620426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
620526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE);
620626932566SPatrick McHardy kfree_skb(skb);
620726932566SPatrick McHardy goto errout;
620826932566SPatrick McHardy }
62091ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
62101ce85fe4SPablo Neira Ayuso return;
62118c384bfaSThomas Graf errout:
62128c384bfaSThomas Graf if (err < 0)
62136fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
62141da177e4SLinus Torvalds }
62151da177e4SLinus Torvalds
__ipv6_ifa_notify(int event,struct inet6_ifaddr * ifp)62161da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
62171da177e4SLinus Torvalds {
621863998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev);
621963998ac2SNicolas Dichtel
6220c15b1ccaSHannes Frederic Sowa if (event)
6221c15b1ccaSHannes Frederic Sowa ASSERT_RTNL();
6222c15b1ccaSHannes Frederic Sowa
62231da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
62241da177e4SLinus Torvalds
62251da177e4SLinus Torvalds switch (event) {
62261da177e4SLinus Torvalds case RTM_NEWADDR:
622795c385b4SNeil Horman /*
62282d819d25SDavid Ahern * If the address was optimistic we inserted the route at the
62292d819d25SDavid Ahern * start of our DAD process, so we don't need to do it again.
62302d819d25SDavid Ahern * If the device was taken down in the middle of the DAD
62312d819d25SDavid Ahern * cycle there is a race where we could get here without a
62322d819d25SDavid Ahern * host route, so nothing to insert. That will be fixed when
62332d819d25SDavid Ahern * the device is brought up.
623495c385b4SNeil Horman */
62352d819d25SDavid Ahern if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) {
6236afb1d4b5SDavid Ahern ip6_ins_rt(net, ifp->rt);
62372d819d25SDavid Ahern } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
62382d819d25SDavid Ahern pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
62392d819d25SDavid Ahern &ifp->addr, ifp->idev->dev->name);
62402d819d25SDavid Ahern }
62412d819d25SDavid Ahern
62421da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding)
62431da177e4SLinus Torvalds addrconf_join_anycast(ifp);
62447996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr))
624507758eb9SHangbin Liu addrconf_prefix_route(&ifp->peer_addr, 128,
624607758eb9SHangbin Liu ifp->rt_priority, ifp->idev->dev,
624707758eb9SHangbin Liu 0, 0, GFP_ATOMIC);
62481da177e4SLinus Torvalds break;
62491da177e4SLinus Torvalds case RTM_DELADDR:
62501da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding)
62511da177e4SLinus Torvalds addrconf_leave_anycast(ifp);
62521da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr);
62537996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) {
62548d1c802bSDavid Ahern struct fib6_info *rt;
6255caeaba79SNicolas Dichtel
6256e7478dfcSNicolas Dichtel rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
62572b2450caSDavid Ahern ifp->idev->dev, 0, 0,
62582b2450caSDavid Ahern false);
62598e3d5be7SMartin KaFai Lau if (rt)
626011dd74b3SRoopa Prabhu ip6_del_rt(net, rt, false);
6261caeaba79SNicolas Dichtel }
626238bd10c4SDavid Ahern if (ifp->rt) {
626311dd74b3SRoopa Prabhu ip6_del_rt(net, ifp->rt, false);
626493531c67SDavid Ahern ifp->rt = NULL;
626538bd10c4SDavid Ahern }
6266705f1c86SHannes Frederic Sowa rt_genid_bump_ipv6(net);
62671da177e4SLinus Torvalds break;
62681da177e4SLinus Torvalds }
626963998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid);
62701da177e4SLinus Torvalds }
62711da177e4SLinus Torvalds
ipv6_ifa_notify(int event,struct inet6_ifaddr * ifp)62721da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
62731da177e4SLinus Torvalds {
62741da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0))
62751da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp);
62761da177e4SLinus Torvalds }
62771da177e4SLinus Torvalds
62781da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
62791da177e4SLinus Torvalds
addrconf_sysctl_forward(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)628032927393SChristoph Hellwig static int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
628132927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
62821da177e4SLinus Torvalds {
62831da177e4SLinus Torvalds int *valp = ctl->data;
62841da177e4SLinus Torvalds int val = *valp;
628588af182eSEric W. Biederman loff_t pos = *ppos;
6286fe2c6338SJoe Perches struct ctl_table lctl;
62871da177e4SLinus Torvalds int ret;
62881da177e4SLinus Torvalds
6289013d97e9SFrancesco Ruggeri /*
6290013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should
6291013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock.
6292013d97e9SFrancesco Ruggeri */
6293013d97e9SFrancesco Ruggeri lctl = *ctl;
6294013d97e9SFrancesco Ruggeri lctl.data = &val;
6295013d97e9SFrancesco Ruggeri
6296013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
62971da177e4SLinus Torvalds
6298c8fecf22SPavel Emelyanov if (write)
6299b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val);
630088af182eSEric W. Biederman if (ret)
630188af182eSEric W. Biederman *ppos = pos;
63021da177e4SLinus Torvalds return ret;
63031da177e4SLinus Torvalds }
63041da177e4SLinus Torvalds
addrconf_sysctl_mtu(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)630532927393SChristoph Hellwig static int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
630632927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
630777751427SMarcelo Leitner {
630877751427SMarcelo Leitner struct inet6_dev *idev = ctl->extra1;
630977751427SMarcelo Leitner int min_mtu = IPV6_MIN_MTU;
631077751427SMarcelo Leitner struct ctl_table lctl;
631177751427SMarcelo Leitner
631277751427SMarcelo Leitner lctl = *ctl;
631377751427SMarcelo Leitner lctl.extra1 = &min_mtu;
631477751427SMarcelo Leitner lctl.extra2 = idev ? &idev->dev->mtu : NULL;
631577751427SMarcelo Leitner
631677751427SMarcelo Leitner return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
631777751427SMarcelo Leitner }
631877751427SMarcelo Leitner
dev_disable_change(struct inet6_dev * idev)631956d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
632056d417b1SBrian Haley {
632175538c2bSCong Wang struct netdev_notifier_info info;
632275538c2bSCong Wang
632356d417b1SBrian Haley if (!idev || !idev->dev)
632456d417b1SBrian Haley return;
632556d417b1SBrian Haley
632675538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev);
632756d417b1SBrian Haley if (idev->cnf.disable_ipv6)
632875538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info);
632956d417b1SBrian Haley else
633075538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info);
633156d417b1SBrian Haley }
633256d417b1SBrian Haley
addrconf_disable_change(struct net * net,__s32 newf)633356d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
633456d417b1SBrian Haley {
633556d417b1SBrian Haley struct net_device *dev;
633656d417b1SBrian Haley struct inet6_dev *idev;
633756d417b1SBrian Haley
633803e4deffSKefeng Wang for_each_netdev(net, dev) {
633956d417b1SBrian Haley idev = __in6_dev_get(dev);
634056d417b1SBrian Haley if (idev) {
634156d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
63424b271168SEric Dumazet
63434b271168SEric Dumazet WRITE_ONCE(idev->cnf.disable_ipv6, newf);
634456d417b1SBrian Haley if (changed)
634556d417b1SBrian Haley dev_disable_change(idev);
634656d417b1SBrian Haley }
634756d417b1SBrian Haley }
634856d417b1SBrian Haley }
634956d417b1SBrian Haley
addrconf_disable_ipv6(struct ctl_table * table,int * p,int newf)6350013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
635156d417b1SBrian Haley {
635256d417b1SBrian Haley struct net *net;
6353013d97e9SFrancesco Ruggeri int old;
6354013d97e9SFrancesco Ruggeri
6355013d97e9SFrancesco Ruggeri if (!rtnl_trylock())
6356013d97e9SFrancesco Ruggeri return restart_syscall();
635756d417b1SBrian Haley
635856d417b1SBrian Haley net = (struct net *)table->extra2;
6359013d97e9SFrancesco Ruggeri old = *p;
63604b271168SEric Dumazet WRITE_ONCE(*p, newf);
636156d417b1SBrian Haley
6362013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6363013d97e9SFrancesco Ruggeri rtnl_unlock();
636456d417b1SBrian Haley return 0;
636588af182eSEric W. Biederman }
636656d417b1SBrian Haley
636756d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) {
63684b271168SEric Dumazet WRITE_ONCE(net->ipv6.devconf_dflt->disable_ipv6, newf);
636956d417b1SBrian Haley addrconf_disable_change(net, newf);
6370013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old))
637156d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1);
637256d417b1SBrian Haley
637356d417b1SBrian Haley rtnl_unlock();
637456d417b1SBrian Haley return 0;
637556d417b1SBrian Haley }
637656d417b1SBrian Haley
addrconf_sysctl_disable(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)637732927393SChristoph Hellwig static int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
637832927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
637956d417b1SBrian Haley {
638056d417b1SBrian Haley int *valp = ctl->data;
638156d417b1SBrian Haley int val = *valp;
638288af182eSEric W. Biederman loff_t pos = *ppos;
6383fe2c6338SJoe Perches struct ctl_table lctl;
638456d417b1SBrian Haley int ret;
638556d417b1SBrian Haley
6386013d97e9SFrancesco Ruggeri /*
6387013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should
6388013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock.
6389013d97e9SFrancesco Ruggeri */
6390013d97e9SFrancesco Ruggeri lctl = *ctl;
6391013d97e9SFrancesco Ruggeri lctl.data = &val;
6392013d97e9SFrancesco Ruggeri
6393013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
639456d417b1SBrian Haley
639556d417b1SBrian Haley if (write)
639656d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val);
639788af182eSEric W. Biederman if (ret)
639888af182eSEric W. Biederman *ppos = pos;
639956d417b1SBrian Haley return ret;
640056d417b1SBrian Haley }
640156d417b1SBrian Haley
addrconf_sysctl_proxy_ndp(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)640232927393SChristoph Hellwig static int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
640332927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
6404c92d5491Sstephen hemminger {
6405c92d5491Sstephen hemminger int *valp = ctl->data;
6406c92d5491Sstephen hemminger int ret;
6407c92d5491Sstephen hemminger int old, new;
6408c92d5491Sstephen hemminger
6409c92d5491Sstephen hemminger old = *valp;
6410c92d5491Sstephen hemminger ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6411c92d5491Sstephen hemminger new = *valp;
6412c92d5491Sstephen hemminger
6413c92d5491Sstephen hemminger if (write && old != new) {
6414c92d5491Sstephen hemminger struct net *net = ctl->extra2;
6415c92d5491Sstephen hemminger
6416c92d5491Sstephen hemminger if (!rtnl_trylock())
6417c92d5491Sstephen hemminger return restart_syscall();
6418c92d5491Sstephen hemminger
6419c92d5491Sstephen hemminger if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
642085b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
642185b3daadSDavid Ahern NETCONFA_PROXY_NEIGH,
6422c92d5491Sstephen hemminger NETCONFA_IFINDEX_DEFAULT,
6423c92d5491Sstephen hemminger net->ipv6.devconf_dflt);
6424c92d5491Sstephen hemminger else if (valp == &net->ipv6.devconf_all->proxy_ndp)
642585b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
642685b3daadSDavid Ahern NETCONFA_PROXY_NEIGH,
6427c92d5491Sstephen hemminger NETCONFA_IFINDEX_ALL,
6428c92d5491Sstephen hemminger net->ipv6.devconf_all);
6429c92d5491Sstephen hemminger else {
6430c92d5491Sstephen hemminger struct inet6_dev *idev = ctl->extra1;
6431c92d5491Sstephen hemminger
643285b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
643385b3daadSDavid Ahern NETCONFA_PROXY_NEIGH,
6434c92d5491Sstephen hemminger idev->dev->ifindex,
6435c92d5491Sstephen hemminger &idev->cnf);
6436c92d5491Sstephen hemminger }
6437c92d5491Sstephen hemminger rtnl_unlock();
6438c92d5491Sstephen hemminger }
6439c92d5491Sstephen hemminger
6440c92d5491Sstephen hemminger return ret;
6441c92d5491Sstephen hemminger }
6442c92d5491Sstephen hemminger
addrconf_sysctl_addr_gen_mode(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)6443d35a00b8SFelix Jia static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
644432927393SChristoph Hellwig void *buffer, size_t *lenp,
6445d35a00b8SFelix Jia loff_t *ppos)
6446d35a00b8SFelix Jia {
6447d35a00b8SFelix Jia int ret = 0;
6448c6dbf7aaSSabrina Dubroca u32 new_val;
6449d35a00b8SFelix Jia struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6450d35a00b8SFelix Jia struct net *net = (struct net *)ctl->extra2;
6451c6dbf7aaSSabrina Dubroca struct ctl_table tmp = {
6452c6dbf7aaSSabrina Dubroca .data = &new_val,
6453c6dbf7aaSSabrina Dubroca .maxlen = sizeof(new_val),
6454c6dbf7aaSSabrina Dubroca .mode = ctl->mode,
6455c6dbf7aaSSabrina Dubroca };
6456d35a00b8SFelix Jia
64578c171d6cSFelix Jia if (!rtnl_trylock())
64588c171d6cSFelix Jia return restart_syscall();
64598c171d6cSFelix Jia
6460c6dbf7aaSSabrina Dubroca new_val = *((u32 *)ctl->data);
6461c6dbf7aaSSabrina Dubroca
6462c6dbf7aaSSabrina Dubroca ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6463c6dbf7aaSSabrina Dubroca if (ret != 0)
6464c6dbf7aaSSabrina Dubroca goto out;
6465d35a00b8SFelix Jia
6466d35a00b8SFelix Jia if (write) {
64678c171d6cSFelix Jia if (check_addr_gen_mode(new_val) < 0) {
64688c171d6cSFelix Jia ret = -EINVAL;
64698c171d6cSFelix Jia goto out;
64708c171d6cSFelix Jia }
6471d35a00b8SFelix Jia
6472c6dbf7aaSSabrina Dubroca if (idev) {
64738c171d6cSFelix Jia if (check_stable_privacy(idev, net, new_val) < 0) {
64748c171d6cSFelix Jia ret = -EINVAL;
64758c171d6cSFelix Jia goto out;
64768c171d6cSFelix Jia }
6477d35a00b8SFelix Jia
6478d35a00b8SFelix Jia if (idev->cnf.addr_gen_mode != new_val) {
6479d35a00b8SFelix Jia idev->cnf.addr_gen_mode = new_val;
648023ca0c2cSThomas Winter addrconf_init_auto_addrs(idev->dev);
64818c171d6cSFelix Jia }
6482f24c5987SSabrina Dubroca } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6483f24c5987SSabrina Dubroca struct net_device *dev;
6484f24c5987SSabrina Dubroca
6485f24c5987SSabrina Dubroca net->ipv6.devconf_dflt->addr_gen_mode = new_val;
6486f24c5987SSabrina Dubroca for_each_netdev(net, dev) {
6487f24c5987SSabrina Dubroca idev = __in6_dev_get(dev);
6488f24c5987SSabrina Dubroca if (idev &&
6489f24c5987SSabrina Dubroca idev->cnf.addr_gen_mode != new_val) {
6490f24c5987SSabrina Dubroca idev->cnf.addr_gen_mode = new_val;
649123ca0c2cSThomas Winter addrconf_init_auto_addrs(idev->dev);
6492f24c5987SSabrina Dubroca }
6493f24c5987SSabrina Dubroca }
64948c171d6cSFelix Jia }
6495c6dbf7aaSSabrina Dubroca
6496c6dbf7aaSSabrina Dubroca *((u32 *)ctl->data) = new_val;
64978c171d6cSFelix Jia }
64988c171d6cSFelix Jia
64998c171d6cSFelix Jia out:
6500d35a00b8SFelix Jia rtnl_unlock();
6501d35a00b8SFelix Jia
6502d35a00b8SFelix Jia return ret;
6503d35a00b8SFelix Jia }
6504d35a00b8SFelix Jia
addrconf_sysctl_stable_secret(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)65053d1bec99SHannes Frederic Sowa static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
650632927393SChristoph Hellwig void *buffer, size_t *lenp,
65073d1bec99SHannes Frederic Sowa loff_t *ppos)
65083d1bec99SHannes Frederic Sowa {
65093d1bec99SHannes Frederic Sowa int err;
65103d1bec99SHannes Frederic Sowa struct in6_addr addr;
65113d1bec99SHannes Frederic Sowa char str[IPV6_MAX_STRLEN];
65123d1bec99SHannes Frederic Sowa struct ctl_table lctl = *ctl;
6513622c81d5SHannes Frederic Sowa struct net *net = ctl->extra2;
65143d1bec99SHannes Frederic Sowa struct ipv6_stable_secret *secret = ctl->data;
65153d1bec99SHannes Frederic Sowa
6516622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6517622c81d5SHannes Frederic Sowa return -EIO;
6518622c81d5SHannes Frederic Sowa
65193d1bec99SHannes Frederic Sowa lctl.maxlen = IPV6_MAX_STRLEN;
65203d1bec99SHannes Frederic Sowa lctl.data = str;
65213d1bec99SHannes Frederic Sowa
65223d1bec99SHannes Frederic Sowa if (!rtnl_trylock())
65233d1bec99SHannes Frederic Sowa return restart_syscall();
65243d1bec99SHannes Frederic Sowa
65253d1bec99SHannes Frederic Sowa if (!write && !secret->initialized) {
65263d1bec99SHannes Frederic Sowa err = -EIO;
65273d1bec99SHannes Frederic Sowa goto out;
65283d1bec99SHannes Frederic Sowa }
65293d1bec99SHannes Frederic Sowa
65305449a5caSWANG Cong err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
65313d1bec99SHannes Frederic Sowa if (err >= sizeof(str)) {
65323d1bec99SHannes Frederic Sowa err = -EIO;
65333d1bec99SHannes Frederic Sowa goto out;
65343d1bec99SHannes Frederic Sowa }
65353d1bec99SHannes Frederic Sowa
65363d1bec99SHannes Frederic Sowa err = proc_dostring(&lctl, write, buffer, lenp, ppos);
65373d1bec99SHannes Frederic Sowa if (err || !write)
65383d1bec99SHannes Frederic Sowa goto out;
65393d1bec99SHannes Frederic Sowa
65403d1bec99SHannes Frederic Sowa if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
65413d1bec99SHannes Frederic Sowa err = -EIO;
65423d1bec99SHannes Frederic Sowa goto out;
65433d1bec99SHannes Frederic Sowa }
65443d1bec99SHannes Frederic Sowa
65453d1bec99SHannes Frederic Sowa secret->initialized = true;
65463d1bec99SHannes Frederic Sowa secret->secret = addr;
65473d1bec99SHannes Frederic Sowa
6548622c81d5SHannes Frederic Sowa if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6549622c81d5SHannes Frederic Sowa struct net_device *dev;
6550622c81d5SHannes Frederic Sowa
6551622c81d5SHannes Frederic Sowa for_each_netdev(net, dev) {
6552622c81d5SHannes Frederic Sowa struct inet6_dev *idev = __in6_dev_get(dev);
6553622c81d5SHannes Frederic Sowa
6554622c81d5SHannes Frederic Sowa if (idev) {
6555d35a00b8SFelix Jia idev->cnf.addr_gen_mode =
6556622c81d5SHannes Frederic Sowa IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6557622c81d5SHannes Frederic Sowa }
6558622c81d5SHannes Frederic Sowa }
6559622c81d5SHannes Frederic Sowa } else {
6560622c81d5SHannes Frederic Sowa struct inet6_dev *idev = ctl->extra1;
6561622c81d5SHannes Frederic Sowa
6562d35a00b8SFelix Jia idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6563622c81d5SHannes Frederic Sowa }
6564622c81d5SHannes Frederic Sowa
65653d1bec99SHannes Frederic Sowa out:
65663d1bec99SHannes Frederic Sowa rtnl_unlock();
65673d1bec99SHannes Frederic Sowa
65683d1bec99SHannes Frederic Sowa return err;
65693d1bec99SHannes Frederic Sowa }
6570c92d5491Sstephen hemminger
657135103d11SAndy Gospodarek static
addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)657235103d11SAndy Gospodarek int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
657332927393SChristoph Hellwig int write, void *buffer,
657435103d11SAndy Gospodarek size_t *lenp,
657535103d11SAndy Gospodarek loff_t *ppos)
657635103d11SAndy Gospodarek {
657735103d11SAndy Gospodarek int *valp = ctl->data;
657835103d11SAndy Gospodarek int val = *valp;
657935103d11SAndy Gospodarek loff_t pos = *ppos;
658035103d11SAndy Gospodarek struct ctl_table lctl;
658135103d11SAndy Gospodarek int ret;
658235103d11SAndy Gospodarek
658335103d11SAndy Gospodarek /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
658435103d11SAndy Gospodarek * we should not modify it until we get the rtnl lock.
658535103d11SAndy Gospodarek */
658635103d11SAndy Gospodarek lctl = *ctl;
658735103d11SAndy Gospodarek lctl.data = &val;
658835103d11SAndy Gospodarek
658935103d11SAndy Gospodarek ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
659035103d11SAndy Gospodarek
659135103d11SAndy Gospodarek if (write)
659235103d11SAndy Gospodarek ret = addrconf_fixup_linkdown(ctl, valp, val);
659335103d11SAndy Gospodarek if (ret)
659435103d11SAndy Gospodarek *ppos = pos;
659535103d11SAndy Gospodarek return ret;
659635103d11SAndy Gospodarek }
659735103d11SAndy Gospodarek
6598df789fe7SDavid Forster static
addrconf_set_nopolicy(struct rt6_info * rt,int action)6599df789fe7SDavid Forster void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6600df789fe7SDavid Forster {
6601df789fe7SDavid Forster if (rt) {
6602df789fe7SDavid Forster if (action)
6603df789fe7SDavid Forster rt->dst.flags |= DST_NOPOLICY;
6604df789fe7SDavid Forster else
6605df789fe7SDavid Forster rt->dst.flags &= ~DST_NOPOLICY;
6606df789fe7SDavid Forster }
6607df789fe7SDavid Forster }
6608df789fe7SDavid Forster
6609df789fe7SDavid Forster static
addrconf_disable_policy_idev(struct inet6_dev * idev,int val)6610df789fe7SDavid Forster void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6611df789fe7SDavid Forster {
6612df789fe7SDavid Forster struct inet6_ifaddr *ifa;
6613df789fe7SDavid Forster
6614df789fe7SDavid Forster read_lock_bh(&idev->lock);
6615df789fe7SDavid Forster list_for_each_entry(ifa, &idev->addr_list, if_list) {
6616df789fe7SDavid Forster spin_lock(&ifa->lock);
6617df789fe7SDavid Forster if (ifa->rt) {
6618f88d8ea6SDavid Ahern /* host routes only use builtin fib6_nh */
66191cf844c7SDavid Ahern struct fib6_nh *nh = ifa->rt->fib6_nh;
6620df789fe7SDavid Forster int cpu;
6621df789fe7SDavid Forster
662266f5d6ceSWei Wang rcu_read_lock();
66233b6761d1SDavid Ahern ifa->rt->dst_nopolicy = val ? true : false;
6624f40b6ae2SDavid Ahern if (nh->rt6i_pcpu) {
6625df789fe7SDavid Forster for_each_possible_cpu(cpu) {
6626df789fe7SDavid Forster struct rt6_info **rtp;
6627df789fe7SDavid Forster
6628f40b6ae2SDavid Ahern rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
6629df789fe7SDavid Forster addrconf_set_nopolicy(*rtp, val);
6630df789fe7SDavid Forster }
6631df789fe7SDavid Forster }
663266f5d6ceSWei Wang rcu_read_unlock();
6633df789fe7SDavid Forster }
6634df789fe7SDavid Forster spin_unlock(&ifa->lock);
6635df789fe7SDavid Forster }
6636df789fe7SDavid Forster read_unlock_bh(&idev->lock);
6637df789fe7SDavid Forster }
6638df789fe7SDavid Forster
6639df789fe7SDavid Forster static
addrconf_disable_policy(struct ctl_table * ctl,int * valp,int val)6640df789fe7SDavid Forster int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
6641df789fe7SDavid Forster {
6642df789fe7SDavid Forster struct inet6_dev *idev;
6643df789fe7SDavid Forster struct net *net;
6644df789fe7SDavid Forster
6645df789fe7SDavid Forster if (!rtnl_trylock())
6646df789fe7SDavid Forster return restart_syscall();
6647df789fe7SDavid Forster
6648df789fe7SDavid Forster *valp = val;
6649df789fe7SDavid Forster
6650df789fe7SDavid Forster net = (struct net *)ctl->extra2;
6651df789fe7SDavid Forster if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6652df789fe7SDavid Forster rtnl_unlock();
6653df789fe7SDavid Forster return 0;
6654df789fe7SDavid Forster }
6655df789fe7SDavid Forster
6656df789fe7SDavid Forster if (valp == &net->ipv6.devconf_all->disable_policy) {
6657df789fe7SDavid Forster struct net_device *dev;
6658df789fe7SDavid Forster
6659df789fe7SDavid Forster for_each_netdev(net, dev) {
6660df789fe7SDavid Forster idev = __in6_dev_get(dev);
6661df789fe7SDavid Forster if (idev)
6662df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val);
6663df789fe7SDavid Forster }
6664df789fe7SDavid Forster } else {
6665df789fe7SDavid Forster idev = (struct inet6_dev *)ctl->extra1;
6666df789fe7SDavid Forster addrconf_disable_policy_idev(idev, val);
6667df789fe7SDavid Forster }
6668df789fe7SDavid Forster
6669df789fe7SDavid Forster rtnl_unlock();
6670df789fe7SDavid Forster return 0;
6671df789fe7SDavid Forster }
6672df789fe7SDavid Forster
addrconf_sysctl_disable_policy(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)667332927393SChristoph Hellwig static int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
667432927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
6675df789fe7SDavid Forster {
6676df789fe7SDavid Forster int *valp = ctl->data;
6677df789fe7SDavid Forster int val = *valp;
6678df789fe7SDavid Forster loff_t pos = *ppos;
6679df789fe7SDavid Forster struct ctl_table lctl;
6680df789fe7SDavid Forster int ret;
6681df789fe7SDavid Forster
6682df789fe7SDavid Forster lctl = *ctl;
6683df789fe7SDavid Forster lctl.data = &val;
6684df789fe7SDavid Forster ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6685df789fe7SDavid Forster
6686df789fe7SDavid Forster if (write && (*valp != val))
6687df789fe7SDavid Forster ret = addrconf_disable_policy(ctl, valp, val);
6688df789fe7SDavid Forster
6689df789fe7SDavid Forster if (ret)
6690df789fe7SDavid Forster *ppos = pos;
6691df789fe7SDavid Forster
6692df789fe7SDavid Forster return ret;
6693df789fe7SDavid Forster }
6694df789fe7SDavid Forster
6695cb4a4c69SMaciej Żenczykowski static int minus_one = -1;
6696cb9e684eSMaciej Żenczykowski static const int two_five_five = 255;
6697176f716cSMatthieu Baerts static u32 ioam6_if_id_max = U16_MAX;
6698cb9e684eSMaciej Żenczykowski
6699607ea7cdSKonstantin Khlebnikov static const struct ctl_table addrconf_sysctl[] = {
67001da177e4SLinus Torvalds {
67011da177e4SLinus Torvalds .procname = "forwarding",
67021da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding,
67031da177e4SLinus Torvalds .maxlen = sizeof(int),
67041da177e4SLinus Torvalds .mode = 0644,
67056d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward,
67061da177e4SLinus Torvalds },
67071da177e4SLinus Torvalds {
67081da177e4SLinus Torvalds .procname = "hop_limit",
67091da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit,
67101da177e4SLinus Torvalds .maxlen = sizeof(int),
67111da177e4SLinus Torvalds .mode = 0644,
6712cb9e684eSMaciej Żenczykowski .proc_handler = proc_dointvec_minmax,
6713eec4844fSMatteo Croce .extra1 = (void *)SYSCTL_ONE,
6714cb9e684eSMaciej Żenczykowski .extra2 = (void *)&two_five_five,
67151da177e4SLinus Torvalds },
67161da177e4SLinus Torvalds {
67171da177e4SLinus Torvalds .procname = "mtu",
67181da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6,
67191da177e4SLinus Torvalds .maxlen = sizeof(int),
67201da177e4SLinus Torvalds .mode = 0644,
672177751427SMarcelo Leitner .proc_handler = addrconf_sysctl_mtu,
67221da177e4SLinus Torvalds },
67231da177e4SLinus Torvalds {
67241da177e4SLinus Torvalds .procname = "accept_ra",
67251da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra,
67261da177e4SLinus Torvalds .maxlen = sizeof(int),
67271da177e4SLinus Torvalds .mode = 0644,
67286d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
67291da177e4SLinus Torvalds },
67301da177e4SLinus Torvalds {
67311da177e4SLinus Torvalds .procname = "accept_redirects",
67321da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects,
67331da177e4SLinus Torvalds .maxlen = sizeof(int),
67341da177e4SLinus Torvalds .mode = 0644,
67356d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
67361da177e4SLinus Torvalds },
67371da177e4SLinus Torvalds {
67381da177e4SLinus Torvalds .procname = "autoconf",
67391da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf,
67401da177e4SLinus Torvalds .maxlen = sizeof(int),
67411da177e4SLinus Torvalds .mode = 0644,
67426d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
67431da177e4SLinus Torvalds },
67441da177e4SLinus Torvalds {
67451da177e4SLinus Torvalds .procname = "dad_transmits",
67461da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits,
67471da177e4SLinus Torvalds .maxlen = sizeof(int),
67481da177e4SLinus Torvalds .mode = 0644,
67496d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
67501da177e4SLinus Torvalds },
67511da177e4SLinus Torvalds {
67521da177e4SLinus Torvalds .procname = "router_solicitations",
67531da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits,
67541da177e4SLinus Torvalds .maxlen = sizeof(int),
67551da177e4SLinus Torvalds .mode = 0644,
6756cb4a4c69SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax,
6757cb4a4c69SMaciej Żenczykowski .extra1 = &minus_one,
67581da177e4SLinus Torvalds },
67591da177e4SLinus Torvalds {
67601da177e4SLinus Torvalds .procname = "router_solicitation_interval",
67611da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval,
67621da177e4SLinus Torvalds .maxlen = sizeof(int),
67631da177e4SLinus Torvalds .mode = 0644,
67646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies,
67651da177e4SLinus Torvalds },
67661da177e4SLinus Torvalds {
6767bd11f074SMaciej Żenczykowski .procname = "router_solicitation_max_interval",
6768bd11f074SMaciej Żenczykowski .data = &ipv6_devconf.rtr_solicit_max_interval,
6769bd11f074SMaciej Żenczykowski .maxlen = sizeof(int),
6770bd11f074SMaciej Żenczykowski .mode = 0644,
6771bd11f074SMaciej Żenczykowski .proc_handler = proc_dointvec_jiffies,
6772bd11f074SMaciej Żenczykowski },
6773bd11f074SMaciej Żenczykowski {
67741da177e4SLinus Torvalds .procname = "router_solicitation_delay",
67751da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay,
67761da177e4SLinus Torvalds .maxlen = sizeof(int),
67771da177e4SLinus Torvalds .mode = 0644,
67786d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies,
67791da177e4SLinus Torvalds },
67801da177e4SLinus Torvalds {
67811da177e4SLinus Torvalds .procname = "force_mld_version",
67821da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version,
67831da177e4SLinus Torvalds .maxlen = sizeof(int),
67841da177e4SLinus Torvalds .mode = 0644,
67856d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
67861da177e4SLinus Torvalds },
6787fc4eba58SHannes Frederic Sowa {
6788fc4eba58SHannes Frederic Sowa .procname = "mldv1_unsolicited_report_interval",
6789fc4eba58SHannes Frederic Sowa .data =
6790fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv1_unsolicited_report_interval,
6791fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int),
6792fc4eba58SHannes Frederic Sowa .mode = 0644,
6793fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies,
6794fc4eba58SHannes Frederic Sowa },
6795fc4eba58SHannes Frederic Sowa {
6796fc4eba58SHannes Frederic Sowa .procname = "mldv2_unsolicited_report_interval",
6797fc4eba58SHannes Frederic Sowa .data =
6798fc4eba58SHannes Frederic Sowa &ipv6_devconf.mldv2_unsolicited_report_interval,
6799fc4eba58SHannes Frederic Sowa .maxlen = sizeof(int),
6800fc4eba58SHannes Frederic Sowa .mode = 0644,
6801fc4eba58SHannes Frederic Sowa .proc_handler = proc_dointvec_ms_jiffies,
6802fc4eba58SHannes Frederic Sowa },
68031da177e4SLinus Torvalds {
68041da177e4SLinus Torvalds .procname = "use_tempaddr",
68051da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr,
68061da177e4SLinus Torvalds .maxlen = sizeof(int),
68071da177e4SLinus Torvalds .mode = 0644,
68086d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
68091da177e4SLinus Torvalds },
68101da177e4SLinus Torvalds {
68111da177e4SLinus Torvalds .procname = "temp_valid_lft",
68121da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft,
68131da177e4SLinus Torvalds .maxlen = sizeof(int),
68141da177e4SLinus Torvalds .mode = 0644,
68156d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
68161da177e4SLinus Torvalds },
68171da177e4SLinus Torvalds {
68181da177e4SLinus Torvalds .procname = "temp_prefered_lft",
68191da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft,
68201da177e4SLinus Torvalds .maxlen = sizeof(int),
68211da177e4SLinus Torvalds .mode = 0644,
68226d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
68231da177e4SLinus Torvalds },
68241da177e4SLinus Torvalds {
68251da177e4SLinus Torvalds .procname = "regen_max_retry",
68261da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry,
68271da177e4SLinus Torvalds .maxlen = sizeof(int),
68281da177e4SLinus Torvalds .mode = 0644,
68296d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
68301da177e4SLinus Torvalds },
68311da177e4SLinus Torvalds {
68321da177e4SLinus Torvalds .procname = "max_desync_factor",
68331da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor,
68341da177e4SLinus Torvalds .maxlen = sizeof(int),
68351da177e4SLinus Torvalds .mode = 0644,
68366d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
68371da177e4SLinus Torvalds },
68381da177e4SLinus Torvalds {
68391da177e4SLinus Torvalds .procname = "max_addresses",
68401da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses,
68411da177e4SLinus Torvalds .maxlen = sizeof(int),
68421da177e4SLinus Torvalds .mode = 0644,
68436d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
68441da177e4SLinus Torvalds },
68451da177e4SLinus Torvalds {
684665f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr",
684765f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr,
684865f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int),
684965f5c7c1SYOSHIFUJI Hideaki .mode = 0644,
68506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
685165f5c7c1SYOSHIFUJI Hideaki },
685265f5c7c1SYOSHIFUJI Hideaki {
68536b2e04bcSPraveen Chaudhary .procname = "ra_defrtr_metric",
68546b2e04bcSPraveen Chaudhary .data = &ipv6_devconf.ra_defrtr_metric,
68556b2e04bcSPraveen Chaudhary .maxlen = sizeof(u32),
68566b2e04bcSPraveen Chaudhary .mode = 0644,
68576b2e04bcSPraveen Chaudhary .proc_handler = proc_douintvec_minmax,
68586b2e04bcSPraveen Chaudhary .extra1 = (void *)SYSCTL_ONE,
68596b2e04bcSPraveen Chaudhary },
68606b2e04bcSPraveen Chaudhary {
68618013d1d7SHangbin Liu .procname = "accept_ra_min_hop_limit",
68628013d1d7SHangbin Liu .data = &ipv6_devconf.accept_ra_min_hop_limit,
68638013d1d7SHangbin Liu .maxlen = sizeof(int),
68648013d1d7SHangbin Liu .mode = 0644,
68658013d1d7SHangbin Liu .proc_handler = proc_dointvec,
68668013d1d7SHangbin Liu },
68678013d1d7SHangbin Liu {
68685027d54aSPatrick Rohr .procname = "accept_ra_min_lft",
68695027d54aSPatrick Rohr .data = &ipv6_devconf.accept_ra_min_lft,
68701671bcfdSPatrick Rohr .maxlen = sizeof(int),
68711671bcfdSPatrick Rohr .mode = 0644,
68721671bcfdSPatrick Rohr .proc_handler = proc_dointvec,
68731671bcfdSPatrick Rohr },
68741671bcfdSPatrick Rohr {
6875c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo",
6876c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo,
6877c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int),
6878c4fd30ebSYOSHIFUJI Hideaki .mode = 0644,
68796d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
6880c4fd30ebSYOSHIFUJI Hideaki },
6881930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
6882930d6ff2SYOSHIFUJI Hideaki {
6883930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref",
6884930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref,
6885930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int),
6886930d6ff2SYOSHIFUJI Hideaki .mode = 0644,
68876d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
6888930d6ff2SYOSHIFUJI Hideaki },
688952e16356SYOSHIFUJI Hideaki {
689052e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval",
689152e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval,
689252e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int),
689352e16356SYOSHIFUJI Hideaki .mode = 0644,
68946d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies,
689552e16356SYOSHIFUJI Hideaki },
6896fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
689709c884d4SYOSHIFUJI Hideaki {
6898bbea124bSJoel Scherpelz .procname = "accept_ra_rt_info_min_plen",
6899bbea124bSJoel Scherpelz .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6900bbea124bSJoel Scherpelz .maxlen = sizeof(int),
6901bbea124bSJoel Scherpelz .mode = 0644,
6902bbea124bSJoel Scherpelz .proc_handler = proc_dointvec,
6903bbea124bSJoel Scherpelz },
6904bbea124bSJoel Scherpelz {
690509c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen",
690609c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
690709c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int),
690809c884d4SYOSHIFUJI Hideaki .mode = 0644,
69096d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
691009c884d4SYOSHIFUJI Hideaki },
691109c884d4SYOSHIFUJI Hideaki #endif
6912930d6ff2SYOSHIFUJI Hideaki #endif
6913c4fd30ebSYOSHIFUJI Hideaki {
6914fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp",
6915fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp,
6916fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int),
6917fbea49e1SYOSHIFUJI Hideaki .mode = 0644,
6918c92d5491Sstephen hemminger .proc_handler = addrconf_sysctl_proxy_ndp,
6919fbea49e1SYOSHIFUJI Hideaki },
6920fbea49e1SYOSHIFUJI Hideaki {
69210bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route",
69220bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route,
69230bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int),
69240bcbc926SYOSHIFUJI Hideaki .mode = 0644,
69256d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
69260bcbc926SYOSHIFUJI Hideaki },
692795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
692895c385b4SNeil Horman {
692995c385b4SNeil Horman .procname = "optimistic_dad",
693095c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad,
693195c385b4SNeil Horman .maxlen = sizeof(int),
693295c385b4SNeil Horman .mode = 0644,
69336d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
693495c385b4SNeil Horman },
69357fd2561eSErik Kline {
69367fd2561eSErik Kline .procname = "use_optimistic",
69377fd2561eSErik Kline .data = &ipv6_devconf.use_optimistic,
69387fd2561eSErik Kline .maxlen = sizeof(int),
69397fd2561eSErik Kline .mode = 0644,
69407fd2561eSErik Kline .proc_handler = proc_dointvec,
69417fd2561eSErik Kline },
694295c385b4SNeil Horman #endif
69437bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
69447bc570c8SYOSHIFUJI Hideaki {
69457bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding",
69467bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding,
69477bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int),
6948a4e6db07SDavid S. Miller .mode = 0444,
69496d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
69507bc570c8SYOSHIFUJI Hideaki },
69517bc570c8SYOSHIFUJI Hideaki #endif
69520bcbc926SYOSHIFUJI Hideaki {
6953778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6",
6954778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6,
6955778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int),
6956778d80beSYOSHIFUJI Hideaki .mode = 0644,
695756d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable,
6958778d80beSYOSHIFUJI Hideaki },
6959778d80beSYOSHIFUJI Hideaki {
69601b34be74SYOSHIFUJI Hideaki .procname = "accept_dad",
69611b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad,
69621b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int),
69631b34be74SYOSHIFUJI Hideaki .mode = 0644,
69646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec,
69651b34be74SYOSHIFUJI Hideaki },
69661b34be74SYOSHIFUJI Hideaki {
6967f7734fdfSOctavian Purdila .procname = "force_tllao",
6968f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao,
6969f7734fdfSOctavian Purdila .maxlen = sizeof(int),
6970f7734fdfSOctavian Purdila .mode = 0644,
6971f7734fdfSOctavian Purdila .proc_handler = proc_dointvec
6972f7734fdfSOctavian Purdila },
6973f7734fdfSOctavian Purdila {
69745cb04436SHannes Frederic Sowa .procname = "ndisc_notify",
69755cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify,
69765cb04436SHannes Frederic Sowa .maxlen = sizeof(int),
69775cb04436SHannes Frederic Sowa .mode = 0644,
69785cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec
69795cb04436SHannes Frederic Sowa },
69805cb04436SHannes Frederic Sowa {
6981b800c3b9SHannes Frederic Sowa .procname = "suppress_frag_ndisc",
6982b800c3b9SHannes Frederic Sowa .data = &ipv6_devconf.suppress_frag_ndisc,
6983b800c3b9SHannes Frederic Sowa .maxlen = sizeof(int),
6984b800c3b9SHannes Frederic Sowa .mode = 0644,
6985b800c3b9SHannes Frederic Sowa .proc_handler = proc_dointvec
6986b800c3b9SHannes Frederic Sowa },
6987b800c3b9SHannes Frederic Sowa {
6988d9333196SBen Greear .procname = "accept_ra_from_local",
6989d9333196SBen Greear .data = &ipv6_devconf.accept_ra_from_local,
6990d9333196SBen Greear .maxlen = sizeof(int),
6991d9333196SBen Greear .mode = 0644,
6992d9333196SBen Greear .proc_handler = proc_dointvec,
6993d9333196SBen Greear },
6994d9333196SBen Greear {
6995c2943f14SHarout Hedeshian .procname = "accept_ra_mtu",
6996c2943f14SHarout Hedeshian .data = &ipv6_devconf.accept_ra_mtu,
6997c2943f14SHarout Hedeshian .maxlen = sizeof(int),
6998c2943f14SHarout Hedeshian .mode = 0644,
6999c2943f14SHarout Hedeshian .proc_handler = proc_dointvec,
7000c2943f14SHarout Hedeshian },
7001c2943f14SHarout Hedeshian {
70023d1bec99SHannes Frederic Sowa .procname = "stable_secret",
70033d1bec99SHannes Frederic Sowa .data = &ipv6_devconf.stable_secret,
70043d1bec99SHannes Frederic Sowa .maxlen = IPV6_MAX_STRLEN,
70053d1bec99SHannes Frederic Sowa .mode = 0600,
70063d1bec99SHannes Frederic Sowa .proc_handler = addrconf_sysctl_stable_secret,
70073d1bec99SHannes Frederic Sowa },
70083d1bec99SHannes Frederic Sowa {
70093985e8a3SErik Kline .procname = "use_oif_addrs_only",
70103985e8a3SErik Kline .data = &ipv6_devconf.use_oif_addrs_only,
70113985e8a3SErik Kline .maxlen = sizeof(int),
70123985e8a3SErik Kline .mode = 0644,
70133985e8a3SErik Kline .proc_handler = proc_dointvec,
701435103d11SAndy Gospodarek },
701535103d11SAndy Gospodarek {
701635103d11SAndy Gospodarek .procname = "ignore_routes_with_linkdown",
701735103d11SAndy Gospodarek .data = &ipv6_devconf.ignore_routes_with_linkdown,
701835103d11SAndy Gospodarek .maxlen = sizeof(int),
701935103d11SAndy Gospodarek .mode = 0644,
702035103d11SAndy Gospodarek .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
70213985e8a3SErik Kline },
70223985e8a3SErik Kline {
7023abbc3043SJohannes Berg .procname = "drop_unicast_in_l2_multicast",
7024abbc3043SJohannes Berg .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
7025abbc3043SJohannes Berg .maxlen = sizeof(int),
7026abbc3043SJohannes Berg .mode = 0644,
7027abbc3043SJohannes Berg .proc_handler = proc_dointvec,
7028abbc3043SJohannes Berg },
7029abbc3043SJohannes Berg {
70307a02bf89SJohannes Berg .procname = "drop_unsolicited_na",
70317a02bf89SJohannes Berg .data = &ipv6_devconf.drop_unsolicited_na,
70327a02bf89SJohannes Berg .maxlen = sizeof(int),
70337a02bf89SJohannes Berg .mode = 0644,
70347a02bf89SJohannes Berg .proc_handler = proc_dointvec,
70357a02bf89SJohannes Berg },
70367a02bf89SJohannes Berg {
7037f1705ec1SDavid Ahern .procname = "keep_addr_on_down",
7038f1705ec1SDavid Ahern .data = &ipv6_devconf.keep_addr_on_down,
7039f1705ec1SDavid Ahern .maxlen = sizeof(int),
7040f1705ec1SDavid Ahern .mode = 0644,
7041f1705ec1SDavid Ahern .proc_handler = proc_dointvec,
7042f1705ec1SDavid Ahern
7043f1705ec1SDavid Ahern },
7044f1705ec1SDavid Ahern {
70451ababebaSDavid Lebrun .procname = "seg6_enabled",
70461ababebaSDavid Lebrun .data = &ipv6_devconf.seg6_enabled,
70471ababebaSDavid Lebrun .maxlen = sizeof(int),
70481ababebaSDavid Lebrun .mode = 0644,
70491ababebaSDavid Lebrun .proc_handler = proc_dointvec,
70501ababebaSDavid Lebrun },
7051bf355b8dSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
7052bf355b8dSDavid Lebrun {
7053bf355b8dSDavid Lebrun .procname = "seg6_require_hmac",
7054bf355b8dSDavid Lebrun .data = &ipv6_devconf.seg6_require_hmac,
7055bf355b8dSDavid Lebrun .maxlen = sizeof(int),
7056bf355b8dSDavid Lebrun .mode = 0644,
7057bf355b8dSDavid Lebrun .proc_handler = proc_dointvec,
7058bf355b8dSDavid Lebrun },
7059bf355b8dSDavid Lebrun #endif
70601ababebaSDavid Lebrun {
7061adc176c5SErik Nordmark .procname = "enhanced_dad",
7062adc176c5SErik Nordmark .data = &ipv6_devconf.enhanced_dad,
7063adc176c5SErik Nordmark .maxlen = sizeof(int),
7064adc176c5SErik Nordmark .mode = 0644,
7065adc176c5SErik Nordmark .proc_handler = proc_dointvec,
7066adc176c5SErik Nordmark },
7067adc176c5SErik Nordmark {
7068d35a00b8SFelix Jia .procname = "addr_gen_mode",
7069d35a00b8SFelix Jia .data = &ipv6_devconf.addr_gen_mode,
7070d35a00b8SFelix Jia .maxlen = sizeof(int),
7071d35a00b8SFelix Jia .mode = 0644,
7072d35a00b8SFelix Jia .proc_handler = addrconf_sysctl_addr_gen_mode,
7073d35a00b8SFelix Jia },
7074d35a00b8SFelix Jia {
7075df789fe7SDavid Forster .procname = "disable_policy",
7076df789fe7SDavid Forster .data = &ipv6_devconf.disable_policy,
7077df789fe7SDavid Forster .maxlen = sizeof(int),
7078df789fe7SDavid Forster .mode = 0644,
7079df789fe7SDavid Forster .proc_handler = addrconf_sysctl_disable_policy,
7080df789fe7SDavid Forster },
7081df789fe7SDavid Forster {
70822210d6b2SMaciej Żenczykowski .procname = "ndisc_tclass",
70832210d6b2SMaciej Żenczykowski .data = &ipv6_devconf.ndisc_tclass,
70842210d6b2SMaciej Żenczykowski .maxlen = sizeof(int),
70852210d6b2SMaciej Żenczykowski .mode = 0644,
70862210d6b2SMaciej Żenczykowski .proc_handler = proc_dointvec_minmax,
7087eec4844fSMatteo Croce .extra1 = (void *)SYSCTL_ZERO,
70882210d6b2SMaciej Żenczykowski .extra2 = (void *)&two_five_five,
70892210d6b2SMaciej Żenczykowski },
70902210d6b2SMaciej Żenczykowski {
70918610c7c6SAlexander Aring .procname = "rpl_seg_enabled",
70928610c7c6SAlexander Aring .data = &ipv6_devconf.rpl_seg_enabled,
70938610c7c6SAlexander Aring .maxlen = sizeof(int),
70948610c7c6SAlexander Aring .mode = 0644,
70958610c7c6SAlexander Aring .proc_handler = proc_dointvec,
70968610c7c6SAlexander Aring },
70978610c7c6SAlexander Aring {
70989ee11f0fSJustin Iurman .procname = "ioam6_enabled",
70999ee11f0fSJustin Iurman .data = &ipv6_devconf.ioam6_enabled,
71009ee11f0fSJustin Iurman .maxlen = sizeof(u8),
71019ee11f0fSJustin Iurman .mode = 0644,
71029ee11f0fSJustin Iurman .proc_handler = proc_dou8vec_minmax,
71039ee11f0fSJustin Iurman .extra1 = (void *)SYSCTL_ZERO,
71049ee11f0fSJustin Iurman .extra2 = (void *)SYSCTL_ONE,
71059ee11f0fSJustin Iurman },
71069ee11f0fSJustin Iurman {
71079ee11f0fSJustin Iurman .procname = "ioam6_id",
71089ee11f0fSJustin Iurman .data = &ipv6_devconf.ioam6_id,
71099ee11f0fSJustin Iurman .maxlen = sizeof(u32),
71109ee11f0fSJustin Iurman .mode = 0644,
71119ee11f0fSJustin Iurman .proc_handler = proc_douintvec_minmax,
71129ee11f0fSJustin Iurman .extra1 = (void *)SYSCTL_ZERO,
71139ee11f0fSJustin Iurman .extra2 = (void *)&ioam6_if_id_max,
71149ee11f0fSJustin Iurman },
71159ee11f0fSJustin Iurman {
71169ee11f0fSJustin Iurman .procname = "ioam6_id_wide",
71179ee11f0fSJustin Iurman .data = &ipv6_devconf.ioam6_id_wide,
71189ee11f0fSJustin Iurman .maxlen = sizeof(u32),
71199ee11f0fSJustin Iurman .mode = 0644,
71209ee11f0fSJustin Iurman .proc_handler = proc_douintvec,
71219ee11f0fSJustin Iurman },
71229ee11f0fSJustin Iurman {
712318ac597aSJames Prestwood .procname = "ndisc_evict_nocarrier",
712418ac597aSJames Prestwood .data = &ipv6_devconf.ndisc_evict_nocarrier,
712518ac597aSJames Prestwood .maxlen = sizeof(u8),
712618ac597aSJames Prestwood .mode = 0644,
712718ac597aSJames Prestwood .proc_handler = proc_dou8vec_minmax,
712818ac597aSJames Prestwood .extra1 = (void *)SYSCTL_ZERO,
712918ac597aSJames Prestwood .extra2 = (void *)SYSCTL_ONE,
713018ac597aSJames Prestwood },
713118ac597aSJames Prestwood {
71323e0b8f52SArun Ajith S .procname = "accept_untracked_na",
71333e0b8f52SArun Ajith S .data = &ipv6_devconf.accept_untracked_na,
7134f9a2fb73SArun Ajith S .maxlen = sizeof(int),
7135f9a2fb73SArun Ajith S .mode = 0644,
7136b66eb3a6SJaehee Park .proc_handler = proc_dointvec_minmax,
7137b66eb3a6SJaehee Park .extra1 = SYSCTL_ZERO,
7138b66eb3a6SJaehee Park .extra2 = SYSCTL_TWO,
7139f9a2fb73SArun Ajith S },
7140f9a2fb73SArun Ajith S {
7141f8572d8fSEric W. Biederman /* sentinel */
71421da177e4SLinus Torvalds }
71431da177e4SLinus Torvalds };
71441da177e4SLinus Torvalds
__addrconf_sysctl_register(struct net * net,char * dev_name,struct inet6_dev * idev,struct ipv6_devconf * p)7145bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
7146f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p)
71471da177e4SLinus Torvalds {
714829c994e3SNicolas Dichtel int i, ifindex;
7149607ea7cdSKonstantin Khlebnikov struct ctl_table *table;
71506105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
71511dab6222SPavel Emelyanov
7152425b9c7fSVasily Averin table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL_ACCOUNT);
7153607ea7cdSKonstantin Khlebnikov if (!table)
7154f68635e6SPavel Emelyanov goto out;
7155f68635e6SPavel Emelyanov
7156607ea7cdSKonstantin Khlebnikov for (i = 0; table[i].data; i++) {
7157607ea7cdSKonstantin Khlebnikov table[i].data += (char *)p - (char *)&ipv6_devconf;
7158cb9e684eSMaciej Żenczykowski /* If one of these is already set, then it is not safe to
7159cb9e684eSMaciej Żenczykowski * overwrite either of them: this makes proc_dointvec_minmax
7160cb9e684eSMaciej Żenczykowski * usable.
7161cb9e684eSMaciej Żenczykowski */
7162cb9e684eSMaciej Żenczykowski if (!table[i].extra1 && !table[i].extra2) {
7163607ea7cdSKonstantin Khlebnikov table[i].extra1 = idev; /* embedded; no ref */
7164607ea7cdSKonstantin Khlebnikov table[i].extra2 = net;
71651da177e4SLinus Torvalds }
7166cb9e684eSMaciej Żenczykowski }
71671da177e4SLinus Torvalds
71686105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
71691da177e4SLinus Torvalds
7170c899710fSJoel Granados p->sysctl_header = register_net_sysctl_sz(net, path, table,
7171c899710fSJoel Granados ARRAY_SIZE(addrconf_sysctl));
7172607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header)
71736105e293SEric W. Biederman goto free;
7174f68635e6SPavel Emelyanov
717529c994e3SNicolas Dichtel if (!strcmp(dev_name, "all"))
717629c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_ALL;
717729c994e3SNicolas Dichtel else if (!strcmp(dev_name, "default"))
717829c994e3SNicolas Dichtel ifindex = NETCONFA_IFINDEX_DEFAULT;
717929c994e3SNicolas Dichtel else
718029c994e3SNicolas Dichtel ifindex = idev->dev->ifindex;
718185b3daadSDavid Ahern inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
718285b3daadSDavid Ahern ifindex, p);
718395897312SPavel Emelyanov return 0;
71841da177e4SLinus Torvalds
71851da177e4SLinus Torvalds free:
7186607ea7cdSKonstantin Khlebnikov kfree(table);
7187f68635e6SPavel Emelyanov out:
718895897312SPavel Emelyanov return -ENOBUFS;
71891da177e4SLinus Torvalds }
71901da177e4SLinus Torvalds
__addrconf_sysctl_unregister(struct net * net,struct ipv6_devconf * p,int ifindex)719123452170SDavid Ahern static void __addrconf_sysctl_unregister(struct net *net,
719223452170SDavid Ahern struct ipv6_devconf *p, int ifindex)
7193f52295a9SPavel Emelyanov {
7194607ea7cdSKonstantin Khlebnikov struct ctl_table *table;
7195f52295a9SPavel Emelyanov
7196607ea7cdSKonstantin Khlebnikov if (!p->sysctl_header)
7197408c4768SPavel Emelyanov return;
7198408c4768SPavel Emelyanov
7199607ea7cdSKonstantin Khlebnikov table = p->sysctl_header->ctl_table_arg;
7200607ea7cdSKonstantin Khlebnikov unregister_net_sysctl_table(p->sysctl_header);
7201607ea7cdSKonstantin Khlebnikov p->sysctl_header = NULL;
7202607ea7cdSKonstantin Khlebnikov kfree(table);
720323452170SDavid Ahern
720423452170SDavid Ahern inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
72051da177e4SLinus Torvalds }
7206408c4768SPavel Emelyanov
addrconf_sysctl_register(struct inet6_dev * idev)7207a317a2f1SWANG Cong static int addrconf_sysctl_register(struct inet6_dev *idev)
7208408c4768SPavel Emelyanov {
7209a317a2f1SWANG Cong int err;
7210a317a2f1SWANG Cong
7211a317a2f1SWANG Cong if (!sysctl_dev_name_is_allowed(idev->dev->name))
7212a317a2f1SWANG Cong return -EINVAL;
7213a317a2f1SWANG Cong
7214a317a2f1SWANG Cong err = neigh_sysctl_register(idev->dev, idev->nd_parms,
7215f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change);
7216a317a2f1SWANG Cong if (err)
7217a317a2f1SWANG Cong return err;
7218a317a2f1SWANG Cong err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
7219f8572d8fSEric W. Biederman idev, &idev->cnf);
7220a317a2f1SWANG Cong if (err)
7221a317a2f1SWANG Cong neigh_sysctl_unregister(idev->nd_parms);
7222a317a2f1SWANG Cong
7223a317a2f1SWANG Cong return err;
7224408c4768SPavel Emelyanov }
7225408c4768SPavel Emelyanov
addrconf_sysctl_unregister(struct inet6_dev * idev)7226408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
7227408c4768SPavel Emelyanov {
722823452170SDavid Ahern __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
722923452170SDavid Ahern idev->dev->ifindex);
7230408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms);
72311da177e4SLinus Torvalds }
72321da177e4SLinus Torvalds
72331da177e4SLinus Torvalds
72341da177e4SLinus Torvalds #endif
72351da177e4SLinus Torvalds
addrconf_init_net(struct net * net)72362c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
7237e0da5a48SPavel Emelyanov {
7238a79ca223SHong Zhiguo int err = -ENOMEM;
7239e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt;
7240e0da5a48SPavel Emelyanov
724121a216a8SEric Dumazet spin_lock_init(&net->ipv6.addrconf_hash_lock);
72428805d13fSEric Dumazet INIT_DEFERRABLE_WORK(&net->ipv6.addr_chk_work, addrconf_verify_work);
724321a216a8SEric Dumazet net->ipv6.inet6_addr_lst = kcalloc(IN6_ADDR_HSIZE,
724421a216a8SEric Dumazet sizeof(struct hlist_head),
724521a216a8SEric Dumazet GFP_KERNEL);
724621a216a8SEric Dumazet if (!net->ipv6.inet6_addr_lst)
724721a216a8SEric Dumazet goto err_alloc_addr;
724821a216a8SEric Dumazet
7249a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
725063159f29SIan Morris if (!all)
7251e0da5a48SPavel Emelyanov goto err_alloc_all;
7252e0da5a48SPavel Emelyanov
7253a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
725463159f29SIan Morris if (!dflt)
7255e0da5a48SPavel Emelyanov goto err_alloc_dflt;
7256a79ca223SHong Zhiguo
7257a5612ca1SKuniyuki Iwashima if (!net_eq(net, &init_net)) {
7258a5612ca1SKuniyuki Iwashima switch (net_inherit_devconf()) {
72599efd6a3cSNicolas Dichtel case 1: /* copy from init_net */
72609efd6a3cSNicolas Dichtel memcpy(all, init_net.ipv6.devconf_all,
72619efd6a3cSNicolas Dichtel sizeof(ipv6_devconf));
72629efd6a3cSNicolas Dichtel memcpy(dflt, init_net.ipv6.devconf_dflt,
72639efd6a3cSNicolas Dichtel sizeof(ipv6_devconf_dflt));
72649efd6a3cSNicolas Dichtel break;
72659efd6a3cSNicolas Dichtel case 3: /* copy from the current netns */
72669efd6a3cSNicolas Dichtel memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all,
72679efd6a3cSNicolas Dichtel sizeof(ipv6_devconf));
72689efd6a3cSNicolas Dichtel memcpy(dflt,
72699efd6a3cSNicolas Dichtel current->nsproxy->net_ns->ipv6.devconf_dflt,
72709efd6a3cSNicolas Dichtel sizeof(ipv6_devconf_dflt));
72719efd6a3cSNicolas Dichtel break;
72729efd6a3cSNicolas Dichtel case 0:
72739efd6a3cSNicolas Dichtel case 2:
72749efd6a3cSNicolas Dichtel /* use compiled values */
72759efd6a3cSNicolas Dichtel break;
72769efd6a3cSNicolas Dichtel }
7277856c395cSCong Wang }
7278856c395cSCong Wang
727956d417b1SBrian Haley /* these will be inherited by all namespaces */
728056d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf;
728156d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
7282e0da5a48SPavel Emelyanov
72833d1bec99SHannes Frederic Sowa dflt->stable_secret.initialized = false;
72843d1bec99SHannes Frederic Sowa all->stable_secret.initialized = false;
72853d1bec99SHannes Frederic Sowa
7286e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all;
7287e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt;
7288e0da5a48SPavel Emelyanov
7289e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
7290f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all);
7291e0da5a48SPavel Emelyanov if (err < 0)
7292e0da5a48SPavel Emelyanov goto err_reg_all;
7293e0da5a48SPavel Emelyanov
7294f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt);
7295e0da5a48SPavel Emelyanov if (err < 0)
7296e0da5a48SPavel Emelyanov goto err_reg_dflt;
7297e0da5a48SPavel Emelyanov #endif
7298e0da5a48SPavel Emelyanov return 0;
7299e0da5a48SPavel Emelyanov
7300e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
7301e0da5a48SPavel Emelyanov err_reg_dflt:
730223452170SDavid Ahern __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
7303e0da5a48SPavel Emelyanov err_reg_all:
7304e0da5a48SPavel Emelyanov kfree(dflt);
73051ca69520SZhengchao Shao net->ipv6.devconf_dflt = NULL;
7306e0da5a48SPavel Emelyanov #endif
7307e0da5a48SPavel Emelyanov err_alloc_dflt:
7308e0da5a48SPavel Emelyanov kfree(all);
73091ca69520SZhengchao Shao net->ipv6.devconf_all = NULL;
7310e0da5a48SPavel Emelyanov err_alloc_all:
731121a216a8SEric Dumazet kfree(net->ipv6.inet6_addr_lst);
731221a216a8SEric Dumazet err_alloc_addr:
7313e0da5a48SPavel Emelyanov return err;
7314e0da5a48SPavel Emelyanov }
7315e0da5a48SPavel Emelyanov
addrconf_exit_net(struct net * net)73162c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
7317e0da5a48SPavel Emelyanov {
731821a216a8SEric Dumazet int i;
731921a216a8SEric Dumazet
7320e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
732123452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
732223452170SDavid Ahern NETCONFA_IFINDEX_DEFAULT);
732323452170SDavid Ahern __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
732423452170SDavid Ahern NETCONFA_IFINDEX_ALL);
7325e0da5a48SPavel Emelyanov #endif
7326e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt);
73277d9b1b57SEric Dumazet net->ipv6.devconf_dflt = NULL;
7328e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all);
73297d9b1b57SEric Dumazet net->ipv6.devconf_all = NULL;
733021a216a8SEric Dumazet
7331be6b41c1SEric Dumazet cancel_delayed_work_sync(&net->ipv6.addr_chk_work);
733221a216a8SEric Dumazet /*
733321a216a8SEric Dumazet * Check hash table, then free it.
733421a216a8SEric Dumazet */
733521a216a8SEric Dumazet for (i = 0; i < IN6_ADDR_HSIZE; i++)
733621a216a8SEric Dumazet WARN_ON_ONCE(!hlist_empty(&net->ipv6.inet6_addr_lst[i]));
733721a216a8SEric Dumazet
733821a216a8SEric Dumazet kfree(net->ipv6.inet6_addr_lst);
733921a216a8SEric Dumazet net->ipv6.inet6_addr_lst = NULL;
7340e0da5a48SPavel Emelyanov }
7341e0da5a48SPavel Emelyanov
7342e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
7343e0da5a48SPavel Emelyanov .init = addrconf_init_net,
7344e0da5a48SPavel Emelyanov .exit = addrconf_exit_net,
7345e0da5a48SPavel Emelyanov };
7346e0da5a48SPavel Emelyanov
7347207895fdSDaniel Borkmann static struct rtnl_af_ops inet6_ops __read_mostly = {
7348b382b191SThomas Graf .family = AF_INET6,
7349b382b191SThomas Graf .fill_link_af = inet6_fill_link_af,
7350b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size,
735111b1f828SDaniel Borkmann .validate_link_af = inet6_validate_link_af,
7352f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af,
7353b382b191SThomas Graf };
7354b382b191SThomas Graf
73551da177e4SLinus Torvalds /*
73561da177e4SLinus Torvalds * Init / cleanup code
73571da177e4SLinus Torvalds */
73581da177e4SLinus Torvalds
addrconf_init(void)73591da177e4SLinus Torvalds int __init addrconf_init(void)
73601da177e4SLinus Torvalds {
7361a317a2f1SWANG Cong struct inet6_dev *idev;
7362e66d1172SEric Dumazet int err;
73632a8cc6c8SYOSHIFUJI Hideaki
7364e21e8467SStephen Hemminger err = ipv6_addr_label_init();
7365e21e8467SStephen Hemminger if (err < 0) {
7366f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n",
7367f3213831SJoe Perches __func__, err);
73682cc6d2bfSNeil Horman goto out;
73692a8cc6c8SYOSHIFUJI Hideaki }
73701da177e4SLinus Torvalds
73712cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops);
73722cc6d2bfSNeil Horman if (err < 0)
73732cc6d2bfSNeil Horman goto out_addrlabel;
7374e0da5a48SPavel Emelyanov
7375c15b1ccaSHannes Frederic Sowa addrconf_wq = create_workqueue("ipv6_addrconf");
7376c15b1ccaSHannes Frederic Sowa if (!addrconf_wq) {
7377c15b1ccaSHannes Frederic Sowa err = -ENOMEM;
7378c15b1ccaSHannes Frederic Sowa goto out_nowq;
7379c15b1ccaSHannes Frederic Sowa }
7380c15b1ccaSHannes Frederic Sowa
73811da177e4SLinus Torvalds rtnl_lock();
7382e5f80fcfSEric Dumazet idev = ipv6_add_dev(blackhole_netdev);
73831da177e4SLinus Torvalds rtnl_unlock();
7384a317a2f1SWANG Cong if (IS_ERR(idev)) {
7385a317a2f1SWANG Cong err = PTR_ERR(idev);
7386e0da5a48SPavel Emelyanov goto errlo;
7387a317a2f1SWANG Cong }
73881da177e4SLinus Torvalds
73892f460933SWANG Cong ip6_route_init_special_entries();
73902f460933SWANG Cong
73911da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf);
73921da177e4SLinus Torvalds
73938805d13fSEric Dumazet addrconf_verify(&init_net);
7394c127ea2cSThomas Graf
73953678a9d8Sstephen hemminger rtnl_af_register(&inet6_ops);
7396b382b191SThomas Graf
739716feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
739816feebcfSFlorian Westphal NULL, inet6_dump_ifinfo, 0);
7399c127ea2cSThomas Graf if (err < 0)
7400c127ea2cSThomas Graf goto errout;
7401c127ea2cSThomas Graf
740216feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
740316feebcfSFlorian Westphal inet6_rtm_newaddr, NULL, 0);
740416feebcfSFlorian Westphal if (err < 0)
740516feebcfSFlorian Westphal goto errout;
740616feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
740716feebcfSFlorian Westphal inet6_rtm_deladdr, NULL, 0);
740816feebcfSFlorian Westphal if (err < 0)
740916feebcfSFlorian Westphal goto errout;
741016feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
741116feebcfSFlorian Westphal inet6_rtm_getaddr, inet6_dump_ifaddr,
741216feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED);
741316feebcfSFlorian Westphal if (err < 0)
741416feebcfSFlorian Westphal goto errout;
741516feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
741616feebcfSFlorian Westphal NULL, inet6_dump_ifmcaddr, 0);
741716feebcfSFlorian Westphal if (err < 0)
741816feebcfSFlorian Westphal goto errout;
741916feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
742016feebcfSFlorian Westphal NULL, inet6_dump_ifacaddr, 0);
742116feebcfSFlorian Westphal if (err < 0)
742216feebcfSFlorian Westphal goto errout;
742316feebcfSFlorian Westphal err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
742416feebcfSFlorian Westphal inet6_netconf_get_devconf,
742516feebcfSFlorian Westphal inet6_netconf_dump_devconf,
742616feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED);
742716feebcfSFlorian Westphal if (err < 0)
742816feebcfSFlorian Westphal goto errout;
7429a3fde2adSFlorian Westphal err = ipv6_addr_label_rtnl_register();
7430a3fde2adSFlorian Westphal if (err < 0)
7431a3fde2adSFlorian Westphal goto errout;
74322a8cc6c8SYOSHIFUJI Hideaki
74331da177e4SLinus Torvalds return 0;
7434c127ea2cSThomas Graf errout:
743516feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6);
7436b382b191SThomas Graf rtnl_af_unregister(&inet6_ops);
7437c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf);
7438e0da5a48SPavel Emelyanov errlo:
7439c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq);
7440c15b1ccaSHannes Frederic Sowa out_nowq:
7441e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops);
74422cc6d2bfSNeil Horman out_addrlabel:
74432cc6d2bfSNeil Horman ipv6_addr_label_cleanup();
74442cc6d2bfSNeil Horman out:
7445c127ea2cSThomas Graf return err;
74461da177e4SLinus Torvalds }
74471da177e4SLinus Torvalds
addrconf_cleanup(void)744809f7709fSDaniel Lezcano void addrconf_cleanup(void)
74491da177e4SLinus Torvalds {
7450176c39afSDaniel Lezcano struct net_device *dev;
74511da177e4SLinus Torvalds
74521da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf);
7453e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops);
74542cc6d2bfSNeil Horman ipv6_addr_label_cleanup();
74551da177e4SLinus Torvalds
74565c45121dSFlorian Westphal rtnl_af_unregister(&inet6_ops);
74571da177e4SLinus Torvalds
74585c45121dSFlorian Westphal rtnl_lock();
7459b382b191SThomas Graf
7460176c39afSDaniel Lezcano /* clean dev list */
7461176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) {
7462176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL)
7463176c39afSDaniel Lezcano continue;
7464ae79dbf6SFlorent Fourcot addrconf_ifdown(dev, true);
7465176c39afSDaniel Lezcano }
7466ae79dbf6SFlorent Fourcot addrconf_ifdown(init_net.loopback_dev, true);
7467176c39afSDaniel Lezcano
74681da177e4SLinus Torvalds rtnl_unlock();
7469c15b1ccaSHannes Frederic Sowa
7470c15b1ccaSHannes Frederic Sowa destroy_workqueue(addrconf_wq);
74711da177e4SLinus Torvalds }
7472