blob: b3cea200c85e232829b8b64f38e000ef896462b8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Neighbour Discovery for IPv6
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Mike Shaver <shaver@ingenia.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15/*
16 * Changes:
17 *
Alexey I. Froloffe35f30c2012-04-06 05:50:58 +000018 * Alexey I. Froloff : RFC6106 (DNSSL) support
Pierre Ynard31910572007-10-10 21:22:05 -070019 * Pierre Ynard : export userland ND options
20 * through netlink (RDNSS support)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * Lars Fenneberg : fixed MTU setting on receipt
22 * of an RA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * Janos Farkas : kmalloc failure checks
24 * Alexey Kuznetsov : state machine reworked
25 * and moved to net/core.
26 * Pekka Savola : RFC2461 validation
27 * YOSHIFUJI Hideaki @USAGI : Verify ND options properly
28 */
29
Joe Perches675418d2012-05-16 19:28:38 +000030#define pr_fmt(fmt) "ICMPv6: " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
34#include <linux/types.h>
35#include <linux/socket.h>
36#include <linux/sockios.h>
37#include <linux/sched.h>
38#include <linux/net.h>
39#include <linux/in6.h>
40#include <linux/route.h>
41#include <linux/init.h>
42#include <linux/rcupdate.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#ifdef CONFIG_SYSCTL
45#include <linux/sysctl.h>
46#endif
47
Thomas Graf18237302006-08-04 23:04:54 -070048#include <linux/if_addr.h>
Vishwanath Paida13c592017-10-30 19:38:52 -040049#include <linux/if_ether.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/if_arp.h>
51#include <linux/ipv6.h>
52#include <linux/icmpv6.h>
53#include <linux/jhash.h>
54
55#include <net/sock.h>
56#include <net/snmp.h>
57
58#include <net/ipv6.h>
59#include <net/protocol.h>
60#include <net/ndisc.h>
61#include <net/ip6_route.h>
62#include <net/addrconf.h>
63#include <net/icmp.h>
64
Pierre Ynard31910572007-10-10 21:22:05 -070065#include <net/netlink.h>
66#include <linux/rtnetlink.h>
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <net/flow.h>
69#include <net/ip6_checksum.h>
Denis V. Lunev1ed85162008-04-03 14:31:03 -070070#include <net/inet_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/proc_fs.h>
72
73#include <linux/netfilter.h>
74#include <linux/netfilter_ipv6.h>
75
Eric Dumazetd6bf7812010-10-04 06:15:44 +000076static u32 ndisc_hash(const void *pkey,
77 const struct net_device *dev,
David S. Miller2c2aba62011-12-28 15:06:58 -050078 __u32 *hash_rnd);
Eric W. Biederman60395a22015-03-03 17:10:44 -060079static bool ndisc_key_eq(const struct neighbour *neigh, const void *pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int ndisc_constructor(struct neighbour *neigh);
81static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb);
82static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb);
83static int pndisc_constructor(struct pneigh_entry *n);
84static void pndisc_destructor(struct pneigh_entry *n);
85static void pndisc_redo(struct sk_buff *skb);
86
Stephen Hemminger89d69d22009-09-01 11:13:19 +000087static const struct neigh_ops ndisc_generic_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 .family = AF_INET6,
89 .solicit = ndisc_solicit,
90 .error_report = ndisc_error_report,
91 .output = neigh_resolve_output,
92 .connected_output = neigh_connected_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
Stephen Hemminger89d69d22009-09-01 11:13:19 +000095static const struct neigh_ops ndisc_hh_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .family = AF_INET6,
97 .solicit = ndisc_solicit,
98 .error_report = ndisc_error_report,
99 .output = neigh_resolve_output,
100 .connected_output = neigh_resolve_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
103
Stephen Hemminger89d69d22009-09-01 11:13:19 +0000104static const struct neigh_ops ndisc_direct_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .family = AF_INET6,
David S. Miller8f40b162011-07-17 13:34:11 -0700106 .output = neigh_direct_output,
107 .connected_output = neigh_direct_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110struct neigh_table nd_tbl = {
111 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .key_len = sizeof(struct in6_addr),
Eric W. Biedermanbdf53c52015-03-02 00:13:22 -0600113 .protocol = cpu_to_be16(ETH_P_IPV6),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .hash = ndisc_hash,
Eric W. Biederman60395a22015-03-03 17:10:44 -0600115 .key_eq = ndisc_key_eq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .constructor = ndisc_constructor,
117 .pconstructor = pndisc_constructor,
118 .pdestructor = pndisc_destructor,
119 .proxy_redo = pndisc_redo,
120 .id = "ndisc_cache",
121 .parms = {
Shan Weib6720832010-12-01 18:05:12 +0000122 .tbl = &nd_tbl,
Shan Weib6720832010-12-01 18:05:12 +0000123 .reachable_time = ND_REACHABLE_TIME,
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100124 .data = {
125 [NEIGH_VAR_MCAST_PROBES] = 3,
126 [NEIGH_VAR_UCAST_PROBES] = 3,
127 [NEIGH_VAR_RETRANS_TIME] = ND_RETRANS_TIMER,
128 [NEIGH_VAR_BASE_REACHABLE_TIME] = ND_REACHABLE_TIME,
129 [NEIGH_VAR_DELAY_PROBE_TIME] = 5 * HZ,
130 [NEIGH_VAR_GC_STALETIME] = 60 * HZ,
Eric Dumazeteaa72dc2017-08-29 15:16:01 -0700131 [NEIGH_VAR_QUEUE_LEN_BYTES] = SK_WMEM_MAX,
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100132 [NEIGH_VAR_PROXY_QLEN] = 64,
133 [NEIGH_VAR_ANYCAST_DELAY] = 1 * HZ,
134 [NEIGH_VAR_PROXY_DELAY] = (8 * HZ) / 10,
135 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 },
137 .gc_interval = 30 * HZ,
138 .gc_thresh1 = 128,
139 .gc_thresh2 = 512,
140 .gc_thresh3 = 1024,
141};
David Ahernc4850682015-10-12 11:47:08 -0700142EXPORT_SYMBOL_GPL(nd_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Alexander Aringcc84b3c2016-06-15 21:20:24 +0200144void __ndisc_fill_addr_option(struct sk_buff *skb, int type, void *data,
145 int data_len, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Alexander Aring8ec5da42016-06-15 21:20:21 +0200147 int space = __ndisc_opt_addr_space(data_len, pad);
YOSHIFUJI Hideaki / 吉藤英明5f5a0112013-01-21 06:48:53 +0000148 u8 *opt = skb_put(skb, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 opt[0] = type;
151 opt[1] = space>>3;
152
153 memset(opt + 2, 0, pad);
154 opt += pad;
155 space -= pad;
156
157 memcpy(opt+2, data, data_len);
158 data_len += 2;
159 opt += data_len;
Ian Morrise5d08d72014-11-23 21:28:43 +0000160 space -= data_len;
161 if (space > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 memset(opt, 0, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
Alexander Aringcc84b3c2016-06-15 21:20:24 +0200164EXPORT_SYMBOL_GPL(__ndisc_fill_addr_option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Alexander Aring8ec5da42016-06-15 21:20:21 +0200166static inline void ndisc_fill_addr_option(struct sk_buff *skb, int type,
Alexander Aringf997c552016-06-15 21:20:23 +0200167 void *data, u8 icmp6_type)
Alexander Aring8ec5da42016-06-15 21:20:21 +0200168{
169 __ndisc_fill_addr_option(skb, type, data, skb->dev->addr_len,
170 ndisc_addr_option_pad(skb->dev->type));
Alexander Aringf997c552016-06-15 21:20:23 +0200171 ndisc_ops_fill_addr_option(skb->dev, skb, icmp6_type);
172}
173
174static inline void ndisc_fill_redirect_addr_option(struct sk_buff *skb,
175 void *ha,
176 const u8 *ops_data)
177{
178 ndisc_fill_addr_option(skb, ND_OPT_TARGET_LL_ADDR, ha, NDISC_REDIRECT);
179 ndisc_ops_fill_redirect_addr_option(skb->dev, skb, ops_data);
Alexander Aring8ec5da42016-06-15 21:20:21 +0200180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static struct nd_opt_hdr *ndisc_next_option(struct nd_opt_hdr *cur,
183 struct nd_opt_hdr *end)
184{
185 int type;
186 if (!cur || !end || cur >= end)
187 return NULL;
188 type = cur->nd_opt_type;
189 do {
190 cur = ((void *)cur) + (cur->nd_opt_len << 3);
Ian Morris67ba4152014-08-24 21:53:10 +0100191 } while (cur < end && cur->nd_opt_type != type);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000192 return cur <= end && cur->nd_opt_type == type ? cur : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Alexander Aringf997c552016-06-15 21:20:23 +0200195static inline int ndisc_is_useropt(const struct net_device *dev,
196 struct nd_opt_hdr *opt)
Pierre Ynard31910572007-10-10 21:22:05 -0700197{
Alexey I. Froloffe35f30c2012-04-06 05:50:58 +0000198 return opt->nd_opt_type == ND_OPT_RDNSS ||
Alexander Aringf997c552016-06-15 21:20:23 +0200199 opt->nd_opt_type == ND_OPT_DNSSL ||
200 ndisc_ops_is_useropt(dev, opt->nd_opt_type);
Pierre Ynard31910572007-10-10 21:22:05 -0700201}
202
Alexander Aringf997c552016-06-15 21:20:23 +0200203static struct nd_opt_hdr *ndisc_next_useropt(const struct net_device *dev,
204 struct nd_opt_hdr *cur,
Pierre Ynard31910572007-10-10 21:22:05 -0700205 struct nd_opt_hdr *end)
206{
207 if (!cur || !end || cur >= end)
208 return NULL;
209 do {
210 cur = ((void *)cur) + (cur->nd_opt_len << 3);
Alexander Aringf997c552016-06-15 21:20:23 +0200211 } while (cur < end && !ndisc_is_useropt(dev, cur));
212 return cur <= end && ndisc_is_useropt(dev, cur) ? cur : NULL;
Pierre Ynard31910572007-10-10 21:22:05 -0700213}
214
Alexander Aringf997c552016-06-15 21:20:23 +0200215struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
216 u8 *opt, int opt_len,
David S. Miller30f2a5f2012-07-11 23:26:46 -0700217 struct ndisc_options *ndopts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)opt;
220
221 if (!nd_opt || opt_len < 0 || !ndopts)
222 return NULL;
223 memset(ndopts, 0, sizeof(*ndopts));
224 while (opt_len) {
225 int l;
226 if (opt_len < sizeof(struct nd_opt_hdr))
227 return NULL;
228 l = nd_opt->nd_opt_len << 3;
229 if (opt_len < l || l == 0)
230 return NULL;
Alexander Aringf997c552016-06-15 21:20:23 +0200231 if (ndisc_ops_parse_options(dev, nd_opt, ndopts))
232 goto next_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 switch (nd_opt->nd_opt_type) {
234 case ND_OPT_SOURCE_LL_ADDR:
235 case ND_OPT_TARGET_LL_ADDR:
236 case ND_OPT_MTU:
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800237 case ND_OPT_NONCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 case ND_OPT_REDIRECT_HDR:
239 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
Joe Perches675418d2012-05-16 19:28:38 +0000240 ND_PRINTK(2, warn,
241 "%s: duplicated ND6 option found: type=%d\n",
242 __func__, nd_opt->nd_opt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 } else {
244 ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
245 }
246 break;
247 case ND_OPT_PREFIX_INFO:
248 ndopts->nd_opts_pi_end = nd_opt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700249 if (!ndopts->nd_opt_array[nd_opt->nd_opt_type])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
251 break;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800252#ifdef CONFIG_IPV6_ROUTE_INFO
253 case ND_OPT_ROUTE_INFO:
254 ndopts->nd_opts_ri_end = nd_opt;
255 if (!ndopts->nd_opts_ri)
256 ndopts->nd_opts_ri = nd_opt;
257 break;
258#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 default:
Alexander Aringf997c552016-06-15 21:20:23 +0200260 if (ndisc_is_useropt(dev, nd_opt)) {
Pierre Ynard31910572007-10-10 21:22:05 -0700261 ndopts->nd_useropts_end = nd_opt;
262 if (!ndopts->nd_useropts)
263 ndopts->nd_useropts = nd_opt;
264 } else {
265 /*
266 * Unknown options must be silently ignored,
267 * to accommodate future extension to the
268 * protocol.
269 */
Joe Perches675418d2012-05-16 19:28:38 +0000270 ND_PRINTK(2, notice,
271 "%s: ignored unsupported option; type=%d, len=%d\n",
272 __func__,
273 nd_opt->nd_opt_type,
274 nd_opt->nd_opt_len);
Pierre Ynard31910572007-10-10 21:22:05 -0700275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Alexander Aringf997c552016-06-15 21:20:23 +0200277next_opt:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 opt_len -= l;
279 nd_opt = ((void *)nd_opt) + l;
280 }
281 return ndopts;
282}
283
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000284int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 switch (dev->type) {
287 case ARPHRD_ETHER:
288 case ARPHRD_IEEE802: /* Not sure. Check it later. --ANK */
289 case ARPHRD_FDDI:
290 ipv6_eth_mc_map(addr, buf);
291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 case ARPHRD_ARCNET:
293 ipv6_arcnet_mc_map(addr, buf);
294 return 0;
295 case ARPHRD_INFINIBAND:
Rolf Manderscheida9e527e2007-12-10 13:38:41 -0700296 ipv6_ib_mc_map(addr, dev->broadcast, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return 0;
Timo Teräs93ca3bb2011-03-28 22:40:53 +0000298 case ARPHRD_IPGRE:
299 return ipv6_ipgre_mc_map(addr, dev->broadcast, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 default:
301 if (dir) {
302 memcpy(buf, dev->broadcast, dev->addr_len);
303 return 0;
304 }
305 }
306 return -EINVAL;
307}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900308EXPORT_SYMBOL(ndisc_mc_map);
309
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000310static u32 ndisc_hash(const void *pkey,
311 const struct net_device *dev,
David S. Miller2c2aba62011-12-28 15:06:58 -0500312 __u32 *hash_rnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
David S. Miller2c2aba62011-12-28 15:06:58 -0500314 return ndisc_hashfn(pkey, dev, hash_rnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Eric W. Biederman60395a22015-03-03 17:10:44 -0600317static bool ndisc_key_eq(const struct neighbour *n, const void *pkey)
318{
319 return neigh_key_eq128(n, pkey);
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static int ndisc_constructor(struct neighbour *neigh)
323{
Ian Morris67ba4152014-08-24 21:53:10 +0100324 struct in6_addr *addr = (struct in6_addr *)&neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct net_device *dev = neigh->dev;
326 struct inet6_dev *in6_dev;
327 struct neigh_parms *parms;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000328 bool is_multicast = ipv6_addr_is_multicast(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 in6_dev = in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100331 if (!in6_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return -EINVAL;
333 }
334
335 parms = in6_dev->nd_parms;
336 __neigh_parms_put(neigh->parms);
337 neigh->parms = neigh_parms_clone(parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 neigh->type = is_multicast ? RTN_MULTICAST : RTN_UNICAST;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700340 if (!dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 neigh->nud_state = NUD_NOARP;
342 neigh->ops = &ndisc_direct_ops;
David S. Miller8f40b162011-07-17 13:34:11 -0700343 neigh->output = neigh_direct_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 } else {
345 if (is_multicast) {
346 neigh->nud_state = NUD_NOARP;
347 ndisc_mc_map(addr, neigh->ha, dev, 1);
348 } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) {
349 neigh->nud_state = NUD_NOARP;
350 memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
351 if (dev->flags&IFF_LOOPBACK)
352 neigh->type = RTN_LOCAL;
353 } else if (dev->flags&IFF_POINTOPOINT) {
354 neigh->nud_state = NUD_NOARP;
355 memcpy(neigh->ha, dev->broadcast, dev->addr_len);
356 }
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700357 if (dev->header_ops->cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 neigh->ops = &ndisc_hh_ops;
359 else
360 neigh->ops = &ndisc_generic_ops;
361 if (neigh->nud_state&NUD_VALID)
362 neigh->output = neigh->ops->connected_output;
363 else
364 neigh->output = neigh->ops->output;
365 }
366 in6_dev_put(in6_dev);
367 return 0;
368}
369
370static int pndisc_constructor(struct pneigh_entry *n)
371{
Ian Morris67ba4152014-08-24 21:53:10 +0100372 struct in6_addr *addr = (struct in6_addr *)&n->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct in6_addr maddr;
374 struct net_device *dev = n->dev;
375
Ian Morris63159f22015-03-29 14:00:04 +0100376 if (!dev || !__in6_dev_get(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return -EINVAL;
378 addrconf_addr_solict_mult(addr, &maddr);
379 ipv6_dev_mc_inc(dev, &maddr);
380 return 0;
381}
382
383static void pndisc_destructor(struct pneigh_entry *n)
384{
Ian Morris67ba4152014-08-24 21:53:10 +0100385 struct in6_addr *addr = (struct in6_addr *)&n->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct in6_addr maddr;
387 struct net_device *dev = n->dev;
388
Ian Morris63159f22015-03-29 14:00:04 +0100389 if (!dev || !__in6_dev_get(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return;
391 addrconf_addr_solict_mult(addr, &maddr);
392 ipv6_dev_mc_dec(dev, &maddr);
393}
394
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +0000395static struct sk_buff *ndisc_alloc_skb(struct net_device *dev,
396 int len)
397{
398 int hlen = LL_RESERVED_SPACE(dev);
399 int tlen = dev->needed_tailroom;
400 struct sock *sk = dev_net(dev)->ipv6.ndisc_sk;
401 struct sk_buff *skb;
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +0000402
Thomas Graf25a6e6b2013-09-03 13:37:01 +0200403 skb = alloc_skb(hlen + sizeof(struct ipv6hdr) + len + tlen, GFP_ATOMIC);
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +0000404 if (!skb) {
Thomas Graf25a6e6b2013-09-03 13:37:01 +0200405 ND_PRINTK(0, err, "ndisc: %s failed to allocate an skb\n",
406 __func__);
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +0000407 return NULL;
408 }
409
YOSHIFUJI Hideaki / 吉藤英明f382d032013-01-21 06:48:29 +0000410 skb->protocol = htons(ETH_P_IPV6);
411 skb->dev = dev;
412
YOSHIFUJI Hideaki / 吉藤英明527a1502013-01-21 06:48:39 +0000413 skb_reserve(skb, hlen + sizeof(struct ipv6hdr));
YOSHIFUJI Hideaki / 吉藤英明5135e632013-01-21 06:48:44 +0000414 skb_reset_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +0000415
Thomas Graf25a6e6b2013-09-03 13:37:01 +0200416 /* Manually assign socket ownership as we avoid calling
417 * sock_alloc_send_pskb() to bypass wmem buffer limits
418 */
419 skb_set_owner_w(skb, sk);
420
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +0000421 return skb;
422}
423
YOSHIFUJI Hideaki / 吉藤英明f382d032013-01-21 06:48:29 +0000424static void ip6_nd_hdr(struct sk_buff *skb,
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +0000425 const struct in6_addr *saddr,
426 const struct in6_addr *daddr,
YOSHIFUJI Hideaki / 吉藤英明c8d6c382013-01-21 06:48:24 +0000427 int hop_limit, int len)
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +0000428{
429 struct ipv6hdr *hdr;
Maciej Żenczykowski2210d6b2017-11-07 21:52:09 -0800430 struct inet6_dev *idev;
431 unsigned tclass;
432
433 rcu_read_lock();
434 idev = __in6_dev_get(skb->dev);
435 tclass = idev ? idev->cnf.ndisc_tclass : 0;
436 rcu_read_unlock();
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +0000437
YOSHIFUJI Hideaki / 吉藤英明527a1502013-01-21 06:48:39 +0000438 skb_push(skb, sizeof(*hdr));
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +0000439 skb_reset_network_header(skb);
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +0000440 hdr = ipv6_hdr(skb);
441
Maciej Żenczykowski2210d6b2017-11-07 21:52:09 -0800442 ip6_flow_hdr(hdr, tclass, 0);
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +0000443
444 hdr->payload_len = htons(len);
YOSHIFUJI Hideaki / 吉藤英明c8d6c382013-01-21 06:48:24 +0000445 hdr->nexthdr = IPPROTO_ICMPV6;
446 hdr->hop_limit = hop_limit;
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +0000447
448 hdr->saddr = *saddr;
449 hdr->daddr = *daddr;
450}
451
YOSHIFUJI Hideaki / 吉藤英明af9a9972013-01-21 06:48:34 +0000452static void ndisc_send_skb(struct sk_buff *skb,
YOSHIFUJI Hideakifd0ea7d2012-12-13 02:40:26 +0900453 const struct in6_addr *daddr,
YOSHIFUJI Hideaki / 吉藤英明aa4bdd42013-01-21 06:48:58 +0000454 const struct in6_addr *saddr)
Brian Haley305d5522008-11-04 17:51:14 -0800455{
YOSHIFUJI Hideaki / 吉藤英明f4de84c2013-01-21 06:49:03 +0000456 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki / 吉藤英明af9a9972013-01-21 06:48:34 +0000457 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideaki / 吉藤英明7b3d9b02013-01-21 06:49:08 +0000458 struct sock *sk = net->ipv6.ndisc_sk;
Brian Haley305d5522008-11-04 17:51:14 -0800459 struct inet6_dev *idev;
460 int err;
YOSHIFUJI Hideaki / 吉藤英明aa4bdd42013-01-21 06:48:58 +0000461 struct icmp6hdr *icmp6h = icmp6_hdr(skb);
Brian Haley305d5522008-11-04 17:51:14 -0800462 u8 type;
463
464 type = icmp6h->icmp6_type;
465
YOSHIFUJI Hideaki / 吉藤英明f4de84c2013-01-21 06:49:03 +0000466 if (!dst) {
YOSHIFUJI Hideaki / 吉藤英明f4de84c2013-01-21 06:49:03 +0000467 struct flowi6 fl6;
David Aherne0d56fd2016-09-10 12:09:57 -0700468 int oif = skb->dev->ifindex;
Brian Haley305d5522008-11-04 17:51:14 -0800469
David Ahernca254492015-10-12 11:47:10 -0700470 icmpv6_flow_init(sk, &fl6, type, saddr, daddr, oif);
YOSHIFUJI Hideaki / 吉藤英明f4de84c2013-01-21 06:49:03 +0000471 dst = icmp6_dst_alloc(skb->dev, &fl6);
472 if (IS_ERR(dst)) {
473 kfree_skb(skb);
474 return;
475 }
476
477 skb_dst_set(skb, dst);
478 }
YOSHIFUJI Hideakie1ec7842007-04-24 20:44:52 +0900479
YOSHIFUJI Hideaki / 吉藤英明7b3d9b02013-01-21 06:49:08 +0000480 icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, skb->len,
481 IPPROTO_ICMPV6,
482 csum_partial(icmp6h,
483 skb->len, 0));
484
485 ip6_nd_hdr(skb, saddr, daddr, inet6_sk(sk)->hop_limit, skb->len);
486
Eric Dumazetcfdf7642011-07-27 21:13:03 +0000487 rcu_read_lock();
488 idev = __in6_dev_get(dst->dev);
Neil Hormanedf391f2009-04-27 02:45:02 -0700489 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
YOSHIFUJI Hideakie1ec7842007-04-24 20:44:52 +0900490
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500491 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
492 net, sk, skb, NULL, dst->dev,
Eric W. Biederman13206b62015-10-07 16:48:35 -0500493 dst_output);
YOSHIFUJI Hideakie1ec7842007-04-24 20:44:52 +0900494 if (!err) {
Denis V. Lunev5c5d2442008-10-08 10:33:50 -0700495 ICMP6MSGOUT_INC_STATS(net, idev, type);
Denis V. Luneva862f6a2008-10-08 10:33:06 -0700496 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
YOSHIFUJI Hideakie1ec7842007-04-24 20:44:52 +0900497 }
498
Eric Dumazetcfdf7642011-07-27 21:13:03 +0000499 rcu_read_unlock();
YOSHIFUJI Hideakie1ec7842007-04-24 20:44:52 +0900500}
501
Jiri Benc38cf5952015-09-22 18:57:13 +0200502void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
Cong Wangf564f452013-08-31 13:44:36 +0800503 const struct in6_addr *solicited_addr,
504 bool router, bool solicited, bool override, bool inc_opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000506 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct in6_addr tmpaddr;
508 struct inet6_ifaddr *ifp;
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900509 const struct in6_addr *src_addr;
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000510 struct nd_msg *msg;
511 int optlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* for anycast or proxy, solicited_addr != src_addr */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900514 ifp = ipv6_get_ifaddr(dev_net(dev), solicited_addr, dev, 1);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900515 if (ifp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 src_addr = solicited_addr;
Neil Horman95c385b2007-04-25 17:08:10 -0700517 if (ifp->flags & IFA_F_OPTIMISTIC)
Daniel Balutaf2f79cc2013-07-13 11:26:51 +0300518 override = false;
stephen hemminger9f888162010-06-21 11:00:13 +0000519 inc_opt |= ifp->idev->cnf.force_tllao;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 in6_ifa_put(ifp);
521 } else {
Brian Haley191cd582008-08-14 15:33:21 -0700522 if (ipv6_dev_get_saddr(dev_net(dev), dev, daddr,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900523 inet6_sk(dev_net(dev)->ipv6.ndisc_sk)->srcprefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900524 &tmpaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return;
526 src_addr = &tmpaddr;
527 }
528
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000529 if (!dev->addr_len)
530 inc_opt = 0;
531 if (inc_opt)
Alexander Aringf997c552016-06-15 21:20:23 +0200532 optlen += ndisc_opt_addr_space(dev,
533 NDISC_NEIGHBOUR_ADVERTISEMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000535 skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000536 if (!skb)
537 return;
538
Johannes Berg4df864c2017-06-16 14:29:21 +0200539 msg = skb_put(skb, sizeof(*msg));
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000540 *msg = (struct nd_msg) {
541 .icmph = {
542 .icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT,
543 .icmp6_router = router,
544 .icmp6_solicited = solicited,
545 .icmp6_override = override,
546 },
547 .target = *solicited_addr,
548 };
549
550 if (inc_opt)
551 ndisc_fill_addr_option(skb, ND_OPT_TARGET_LL_ADDR,
Alexander Aringf997c552016-06-15 21:20:23 +0200552 dev->dev_addr,
553 NDISC_NEIGHBOUR_ADVERTISEMENT);
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000554
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000555 ndisc_send_skb(skb, daddr, src_addr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900556}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Ben Hutchingsf47b9462011-04-15 13:46:02 +0000558static void ndisc_send_unsol_na(struct net_device *dev)
559{
560 struct inet6_dev *idev;
561 struct inet6_ifaddr *ifa;
Ben Hutchingsf47b9462011-04-15 13:46:02 +0000562
563 idev = in6_dev_get(dev);
564 if (!idev)
565 return;
566
567 read_lock_bh(&idev->lock);
568 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Jiri Benc38cf5952015-09-22 18:57:13 +0200569 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifa->addr,
Ben Hutchingsf47b9462011-04-15 13:46:02 +0000570 /*router=*/ !!idev->cnf.forwarding,
571 /*solicited=*/ false, /*override=*/ true,
572 /*inc_opt=*/ true);
573 }
574 read_unlock_bh(&idev->lock);
575
576 in6_dev_put(idev);
577}
578
Jiri Benc38cf5952015-09-22 18:57:13 +0200579void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800580 const struct in6_addr *daddr, const struct in6_addr *saddr,
581 u64 nonce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000583 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct in6_addr addr_buf;
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000585 int inc_opt = dev->addr_len;
586 int optlen = 0;
587 struct nd_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Ian Morris63159f22015-03-29 14:00:04 +0100589 if (!saddr) {
Neil Horman95c385b2007-04-25 17:08:10 -0700590 if (ipv6_get_lladdr(dev, &addr_buf,
591 (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return;
593 saddr = &addr_buf;
594 }
595
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000596 if (ipv6_addr_any(saddr))
Daniel Balutaf2f79cc2013-07-13 11:26:51 +0300597 inc_opt = false;
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000598 if (inc_opt)
Alexander Aringf997c552016-06-15 21:20:23 +0200599 optlen += ndisc_opt_addr_space(dev,
600 NDISC_NEIGHBOUR_SOLICITATION);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800601 if (nonce != 0)
602 optlen += 8;
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000603
604 skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000605 if (!skb)
606 return;
607
Johannes Berg4df864c2017-06-16 14:29:21 +0200608 msg = skb_put(skb, sizeof(*msg));
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000609 *msg = (struct nd_msg) {
610 .icmph = {
611 .icmp6_type = NDISC_NEIGHBOUR_SOLICITATION,
612 },
613 .target = *solicit,
614 };
615
616 if (inc_opt)
617 ndisc_fill_addr_option(skb, ND_OPT_SOURCE_LL_ADDR,
Alexander Aringf997c552016-06-15 21:20:23 +0200618 dev->dev_addr,
619 NDISC_NEIGHBOUR_SOLICITATION);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800620 if (nonce != 0) {
621 u8 *opt = skb_put(skb, 8);
622
623 opt[0] = ND_OPT_NONCE;
624 opt[1] = 8 >> 3;
625 memcpy(opt + 2, &nonce, 6);
626 }
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000627
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000628 ndisc_send_skb(skb, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900631void ndisc_send_rs(struct net_device *dev, const struct in6_addr *saddr,
632 const struct in6_addr *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000634 struct sk_buff *skb;
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000635 struct rs_msg *msg;
Neil Horman95c385b2007-04-25 17:08:10 -0700636 int send_sllao = dev->addr_len;
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000637 int optlen = 0;
Neil Horman95c385b2007-04-25 17:08:10 -0700638
639#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
640 /*
641 * According to section 2.2 of RFC 4429, we must not
642 * send router solicitations with a sllao from
643 * optimistic addresses, but we may send the solicitation
644 * if we don't include the sllao. So here we check
645 * if our address is optimistic, and if so, we
Joe Perchesbea85192007-12-20 14:01:35 -0800646 * suppress the inclusion of the sllao.
Neil Horman95c385b2007-04-25 17:08:10 -0700647 */
648 if (send_sllao) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900649 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(dev_net(dev), saddr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -0800650 dev, 1);
Neil Horman95c385b2007-04-25 17:08:10 -0700651 if (ifp) {
652 if (ifp->flags & IFA_F_OPTIMISTIC) {
YOSHIFUJI Hideakica043562007-02-28 23:13:20 +0900653 send_sllao = 0;
Neil Horman95c385b2007-04-25 17:08:10 -0700654 }
YOSHIFUJI Hideakica043562007-02-28 23:13:20 +0900655 in6_ifa_put(ifp);
Neil Horman95c385b2007-04-25 17:08:10 -0700656 } else {
657 send_sllao = 0;
658 }
659 }
660#endif
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000661 if (send_sllao)
Alexander Aringf997c552016-06-15 21:20:23 +0200662 optlen += ndisc_opt_addr_space(dev, NDISC_ROUTER_SOLICITATION);
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000663
664 skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000665 if (!skb)
666 return;
667
Johannes Berg4df864c2017-06-16 14:29:21 +0200668 msg = skb_put(skb, sizeof(*msg));
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000669 *msg = (struct rs_msg) {
670 .icmph = {
671 .icmp6_type = NDISC_ROUTER_SOLICITATION,
672 },
673 };
674
675 if (send_sllao)
676 ndisc_fill_addr_option(skb, ND_OPT_SOURCE_LL_ADDR,
Alexander Aringf997c552016-06-15 21:20:23 +0200677 dev->dev_addr,
678 NDISC_ROUTER_SOLICITATION);
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000679
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000680 ndisc_send_skb(skb, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb)
685{
686 /*
687 * "The sender MUST return an ICMP
688 * destination unreachable"
689 */
690 dst_link_failure(skb);
691 kfree_skb(skb);
692}
693
694/* Called with locked neigh: either read or both */
695
696static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb)
697{
698 struct in6_addr *saddr = NULL;
699 struct in6_addr mcaddr;
700 struct net_device *dev = neigh->dev;
701 struct in6_addr *target = (struct in6_addr *)&neigh->primary_key;
702 int probes = atomic_read(&neigh->probes);
703
Erik Klinec58da4c2015-02-04 20:01:23 +0900704 if (skb && ipv6_chk_addr_and_flags(dev_net(dev), &ipv6_hdr(skb)->saddr,
705 dev, 1,
706 IFA_F_TENTATIVE|IFA_F_OPTIMISTIC))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700707 saddr = &ipv6_hdr(skb)->saddr;
Ian Morrise5d08d72014-11-23 21:28:43 +0000708 probes -= NEIGH_VAR(neigh->parms, UCAST_PROBES);
709 if (probes < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (!(neigh->nud_state & NUD_VALID)) {
Joe Perches675418d2012-05-16 19:28:38 +0000711 ND_PRINTK(1, dbg,
712 "%s: trying to ucast probe in NUD_INVALID: %pI6\n",
713 __func__, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800715 ndisc_send_ns(dev, target, target, saddr, 0);
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100716 } else if ((probes -= NEIGH_VAR(neigh->parms, APP_PROBES)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 neigh_app_ns(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 } else {
719 addrconf_addr_solict_mult(target, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800720 ndisc_send_ns(dev, target, &mcaddr, saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722}
723
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900724static int pndisc_is_router(const void *pkey,
725 struct net_device *dev)
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700726{
727 struct pneigh_entry *n;
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900728 int ret = -1;
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700729
730 read_lock_bh(&nd_tbl.lock);
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900731 n = __pneigh_lookup(&nd_tbl, dev_net(dev), pkey, dev);
732 if (n)
733 ret = !!(n->flags & NTF_ROUTER);
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700734 read_unlock_bh(&nd_tbl.lock);
735
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900736 return ret;
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700737}
738
Alexander Aringf997c552016-06-15 21:20:23 +0200739void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
740 const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
741 struct ndisc_options *ndopts)
742{
Roopa Prabhu7b8f7a42017-03-19 22:01:28 -0700743 neigh_update(neigh, lladdr, new, flags, 0);
Alexander Aringf997c552016-06-15 21:20:23 +0200744 /* report ndisc ops about neighbour update */
745 ndisc_ops_update(dev, neigh, flags, icmp6_type, ndopts);
746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748static void ndisc_recv_ns(struct sk_buff *skb)
749{
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700750 struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000751 const struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
752 const struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 u8 *lladdr = NULL;
Simon Horman29a3cad2013-05-28 20:34:26 +0000754 u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700755 offsetof(struct nd_msg, opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct ndisc_options ndopts;
757 struct net_device *dev = skb->dev;
758 struct inet6_ifaddr *ifp;
759 struct inet6_dev *idev = NULL;
760 struct neighbour *neigh;
761 int dad = ipv6_addr_any(saddr);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000762 bool inc;
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900763 int is_router = -1;
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800764 u64 nonce = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
YOSHIFUJI Hideaki / 吉藤英明115b0aa2013-01-18 02:05:03 +0000766 if (skb->len < sizeof(struct nd_msg)) {
767 ND_PRINTK(2, warn, "NS: packet too short\n");
768 return;
769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (ipv6_addr_is_multicast(&msg->target)) {
Joe Perches675418d2012-05-16 19:28:38 +0000772 ND_PRINTK(2, warn, "NS: multicast target address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return;
774 }
775
776 /*
777 * RFC2461 7.1.1:
778 * DAD has to be destined for solicited node multicast address.
779 */
YOSHIFUJI Hideaki / 吉藤英明ca97a642013-01-20 07:39:00 +0000780 if (dad && !ipv6_addr_is_solict_mult(daddr)) {
Joe Perches675418d2012-05-16 19:28:38 +0000781 ND_PRINTK(2, warn, "NS: bad DAD packet (wrong destination)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return;
783 }
784
Alexander Aringf997c552016-06-15 21:20:23 +0200785 if (!ndisc_parse_options(dev, msg->opt, ndoptlen, &ndopts)) {
Joe Perches675418d2012-05-16 19:28:38 +0000786 ND_PRINTK(2, warn, "NS: invalid ND options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return;
788 }
789
790 if (ndopts.nd_opts_src_lladdr) {
791 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr, dev);
792 if (!lladdr) {
Joe Perches675418d2012-05-16 19:28:38 +0000793 ND_PRINTK(2, warn,
794 "NS: invalid link-layer address length\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return;
796 }
797
798 /* RFC2461 7.1.1:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900799 * If the IP source address is the unspecified address,
800 * there MUST NOT be source link-layer address option
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 * in the message.
802 */
803 if (dad) {
Joe Perches675418d2012-05-16 19:28:38 +0000804 ND_PRINTK(2, warn,
805 "NS: bad DAD packet (link-layer address option)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return;
807 }
808 }
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800809 if (ndopts.nd_opts_nonce)
810 memcpy(&nonce, (u8 *)(ndopts.nd_opts_nonce + 1), 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 inc = ipv6_addr_is_multicast(daddr);
813
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900814 ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
Daniel Lezcanoa18bc692008-03-07 11:14:49 -0800815 if (ifp) {
David Ahernca254492015-10-12 11:47:10 -0700816have_ifp:
Neil Horman95c385b2007-04-25 17:08:10 -0700817 if (ifp->flags & (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)) {
818 if (dad) {
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800819 if (nonce != 0 && ifp->dad_nonce == nonce) {
820 u8 *np = (u8 *)&nonce;
821 /* Matching nonce if looped back */
822 ND_PRINTK(2, notice,
823 "%s: IPv6 DAD loopback for address %pI6c nonce %pM ignored\n",
824 ifp->idev->dev->name,
825 &ifp->addr, np);
826 goto out;
827 }
Neil Horman95c385b2007-04-25 17:08:10 -0700828 /*
829 * We are colliding with another node
830 * who is doing DAD
831 * so fail our DAD process
832 */
Vishwanath Paida13c592017-10-30 19:38:52 -0400833 addrconf_dad_failure(skb, ifp);
Denis V. Lunev9e3be4b2007-09-11 11:04:49 +0200834 return;
Neil Horman95c385b2007-04-25 17:08:10 -0700835 } else {
836 /*
837 * This is not a dad solicitation.
838 * If we are an optimistic node,
839 * we should respond.
840 * Otherwise, we should ignore it.
841 */
842 if (!(ifp->flags & IFA_F_OPTIMISTIC))
843 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
847 idev = ifp->idev;
848 } else {
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700849 struct net *net = dev_net(dev);
850
David Ahernca254492015-10-12 11:47:10 -0700851 /* perhaps an address on the master device */
852 if (netif_is_l3_slave(dev)) {
853 struct net_device *mdev;
854
855 mdev = netdev_master_upper_dev_get_rcu(dev);
856 if (mdev) {
857 ifp = ipv6_get_ifaddr(net, &msg->target, mdev, 1);
858 if (ifp)
859 goto have_ifp;
860 }
861 }
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 idev = in6_dev_get(dev);
864 if (!idev) {
865 /* XXX: count this drop? */
866 return;
867 }
868
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700869 if (ipv6_chk_acast_addr(net, dev, &msg->target) ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900870 (idev->cnf.forwarding &&
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700871 (net->ipv6.devconf_all->proxy_ndp || idev->cnf.proxy_ndp) &&
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900872 (is_router = pndisc_is_router(&msg->target, dev)) >= 0)) {
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700873 if (!(NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 skb->pkt_type != PACKET_HOST &&
Daniel Balutaf2f79cc2013-07-13 11:26:51 +0300875 inc &&
Jiri Pirko1f9248e2013-12-07 19:26:53 +0100876 NEIGH_VAR(idev->nd_parms, PROXY_DELAY) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /*
878 * for anycast or proxy,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900879 * sender should delay its response
880 * by a random time between 0 and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * MAX_ANYCAST_DELAY_TIME seconds.
882 * (RFC2461) -- yoshfuji
883 */
884 struct sk_buff *n = skb_clone(skb, GFP_ATOMIC);
885 if (n)
886 pneigh_enqueue(&nd_tbl, idev->nd_parms, n);
887 goto out;
888 }
889 } else
890 goto out;
891 }
892
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900893 if (is_router < 0)
YOSHIFUJI Hideaki / 吉藤英明fb568632013-01-20 07:39:18 +0000894 is_router = idev->cnf.forwarding;
Ville Nuorvala62dd9312006-09-22 14:43:19 -0700895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (dad) {
Jiri Benc38cf5952015-09-22 18:57:13 +0200897 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &msg->target,
YOSHIFUJI Hideaki / 吉藤英明fb568632013-01-20 07:39:18 +0000898 !!is_router, false, (ifp != NULL), true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 goto out;
900 }
901
902 if (inc)
903 NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_mcast);
904 else
905 NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_ucast);
906
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900907 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 * update / create cache entry
909 * for the source address
910 */
911 neigh = __neigh_lookup(&nd_tbl, saddr, dev,
912 !inc || lladdr || !dev->addr_len);
913 if (neigh)
Alexander Aringf997c552016-06-15 21:20:23 +0200914 ndisc_update(dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 NEIGH_UPDATE_F_WEAK_OVERRIDE|
Alexander Aringf997c552016-06-15 21:20:23 +0200916 NEIGH_UPDATE_F_OVERRIDE,
917 NDISC_NEIGHBOUR_SOLICITATION, &ndopts);
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700918 if (neigh || !dev->header_ops) {
Jiri Benc38cf5952015-09-22 18:57:13 +0200919 ndisc_send_na(dev, saddr, &msg->target, !!is_router,
YOSHIFUJI Hideaki / 吉藤英明fb568632013-01-20 07:39:18 +0000920 true, (ifp != NULL && inc), inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (neigh)
922 neigh_release(neigh);
923 }
924
925out:
926 if (ifp)
927 in6_ifa_put(ifp);
928 else
929 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
932static void ndisc_recv_na(struct sk_buff *skb)
933{
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700934 struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
Duan Jiongbe7a0102014-05-15 15:56:14 +0800935 struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000936 const struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 u8 *lladdr = NULL;
Simon Horman29a3cad2013-05-28 20:34:26 +0000938 u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700939 offsetof(struct nd_msg, opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 struct ndisc_options ndopts;
941 struct net_device *dev = skb->dev;
Johannes Berg7a02bf82016-02-04 13:31:20 +0100942 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 struct inet6_ifaddr *ifp;
944 struct neighbour *neigh;
945
946 if (skb->len < sizeof(struct nd_msg)) {
Joe Perches675418d2012-05-16 19:28:38 +0000947 ND_PRINTK(2, warn, "NA: packet too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return;
949 }
950
951 if (ipv6_addr_is_multicast(&msg->target)) {
Joe Perches675418d2012-05-16 19:28:38 +0000952 ND_PRINTK(2, warn, "NA: target address is multicast\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return;
954 }
955
956 if (ipv6_addr_is_multicast(daddr) &&
957 msg->icmph.icmp6_solicited) {
Joe Perches675418d2012-05-16 19:28:38 +0000958 ND_PRINTK(2, warn, "NA: solicited NA is multicasted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return;
960 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900961
Johannes Berg7a02bf82016-02-04 13:31:20 +0100962 /* For some 802.11 wireless deployments (and possibly other networks),
963 * there will be a NA proxy and unsolicitd packets are attacks
964 * and thus should not be accepted.
965 */
966 if (!msg->icmph.icmp6_solicited && idev &&
967 idev->cnf.drop_unsolicited_na)
968 return;
969
Alexander Aringf997c552016-06-15 21:20:23 +0200970 if (!ndisc_parse_options(dev, msg->opt, ndoptlen, &ndopts)) {
Joe Perches675418d2012-05-16 19:28:38 +0000971 ND_PRINTK(2, warn, "NS: invalid ND option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return;
973 }
974 if (ndopts.nd_opts_tgt_lladdr) {
975 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, dev);
976 if (!lladdr) {
Joe Perches675418d2012-05-16 19:28:38 +0000977 ND_PRINTK(2, warn,
978 "NA: invalid link-layer address length\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return;
980 }
981 }
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900982 ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
Daniel Lezcanoa18bc692008-03-07 11:14:49 -0800983 if (ifp) {
Daniel Walterbd015922011-04-13 21:09:25 +0000984 if (skb->pkt_type != PACKET_LOOPBACK
985 && (ifp->flags & IFA_F_TENTATIVE)) {
Vishwanath Paida13c592017-10-30 19:38:52 -0400986 addrconf_dad_failure(skb, ifp);
Daniel Walterbd015922011-04-13 21:09:25 +0000987 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989 /* What should we make now? The advertisement
990 is invalid, but ndisc specs say nothing
991 about it. It could be misconfiguration, or
992 an smart proxy agent tries to help us :-)
Jan Sembera24fc7b82008-12-09 15:48:32 -0800993
994 We should not print the error if NA has been
995 received from loopback - it is just our own
996 unsolicited advertisement.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 */
Jan Sembera24fc7b82008-12-09 15:48:32 -0800998 if (skb->pkt_type != PACKET_LOOPBACK)
Joe Perches675418d2012-05-16 19:28:38 +0000999 ND_PRINTK(1, warn,
Vishwanath Paida13c592017-10-30 19:38:52 -04001000 "NA: %pM advertised our address %pI6c on %s!\n",
1001 eth_hdr(skb)->h_source, &ifp->addr, ifp->idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 in6_ifa_put(ifp);
1003 return;
1004 }
1005 neigh = neigh_lookup(&nd_tbl, &msg->target, dev);
1006
1007 if (neigh) {
1008 u8 old_flags = neigh->flags;
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001009 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 if (neigh->nud_state & NUD_FAILED)
1012 goto out;
1013
Ville Nuorvala5f3e6e92006-09-22 14:42:46 -07001014 /*
1015 * Don't update the neighbor cache entry on a proxy NA from
1016 * ourselves because either the proxied node is off link or it
1017 * has already sent a NA to us.
1018 */
1019 if (lladdr && !memcmp(lladdr, dev->dev_addr, dev->addr_len) &&
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001020 net->ipv6.devconf_all->forwarding && net->ipv6.devconf_all->proxy_ndp &&
1021 pneigh_lookup(&nd_tbl, net, &msg->target, dev, 0)) {
Nicolas Dichtelb20b6d92012-11-07 05:05:38 +00001022 /* XXX: idev->cnf.proxy_ndp */
Ville Nuorvala5f3e6e92006-09-22 14:42:46 -07001023 goto out;
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07001024 }
Ville Nuorvala5f3e6e92006-09-22 14:42:46 -07001025
Alexander Aringf997c552016-06-15 21:20:23 +02001026 ndisc_update(dev, neigh, lladdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 msg->icmph.icmp6_solicited ? NUD_REACHABLE : NUD_STALE,
1028 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1029 (msg->icmph.icmp6_override ? NEIGH_UPDATE_F_OVERRIDE : 0)|
1030 NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02001031 (msg->icmph.icmp6_router ? NEIGH_UPDATE_F_ISROUTER : 0),
1032 NDISC_NEIGHBOUR_ADVERTISEMENT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 if ((old_flags & ~neigh->flags) & NTF_ROUTER) {
1035 /*
1036 * Change: router to host
1037 */
Duan Jiongbe7a0102014-05-15 15:56:14 +08001038 rt6_clean_tohost(dev_net(dev), saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040
1041out:
1042 neigh_release(neigh);
1043 }
1044}
1045
1046static void ndisc_recv_rs(struct sk_buff *skb)
1047{
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001048 struct rs_msg *rs_msg = (struct rs_msg *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 unsigned long ndoptlen = skb->len - sizeof(*rs_msg);
1050 struct neighbour *neigh;
1051 struct inet6_dev *idev;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001052 const struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct ndisc_options ndopts;
1054 u8 *lladdr = NULL;
1055
1056 if (skb->len < sizeof(*rs_msg))
1057 return;
1058
Eric Dumazetcfdf7642011-07-27 21:13:03 +00001059 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (!idev) {
Joe Perches675418d2012-05-16 19:28:38 +00001061 ND_PRINTK(1, err, "RS: can't find in6 device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return;
1063 }
1064
1065 /* Don't accept RS if we're not in router mode */
1066 if (!idev->cnf.forwarding)
1067 goto out;
1068
1069 /*
1070 * Don't update NCE if src = ::;
1071 * this implies that the source node has no ip address assigned yet.
1072 */
1073 if (ipv6_addr_any(saddr))
1074 goto out;
1075
1076 /* Parse ND options */
Alexander Aringf997c552016-06-15 21:20:23 +02001077 if (!ndisc_parse_options(skb->dev, rs_msg->opt, ndoptlen, &ndopts)) {
Joe Perches675418d2012-05-16 19:28:38 +00001078 ND_PRINTK(2, notice, "NS: invalid ND option, ignored\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 goto out;
1080 }
1081
1082 if (ndopts.nd_opts_src_lladdr) {
1083 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
1084 skb->dev);
1085 if (!lladdr)
1086 goto out;
1087 }
1088
1089 neigh = __neigh_lookup(&nd_tbl, saddr, skb->dev, 1);
1090 if (neigh) {
Alexander Aringf997c552016-06-15 21:20:23 +02001091 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1093 NEIGH_UPDATE_F_OVERRIDE|
Alexander Aringf997c552016-06-15 21:20:23 +02001094 NEIGH_UPDATE_F_OVERRIDE_ISROUTER,
1095 NDISC_ROUTER_SOLICITATION, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 neigh_release(neigh);
1097 }
1098out:
Eric Dumazetcfdf7642011-07-27 21:13:03 +00001099 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
Pierre Ynard31910572007-10-10 21:22:05 -07001102static void ndisc_ra_useropt(struct sk_buff *ra, struct nd_opt_hdr *opt)
1103{
1104 struct icmp6hdr *icmp6h = (struct icmp6hdr *)skb_transport_header(ra);
1105 struct sk_buff *skb;
1106 struct nlmsghdr *nlh;
1107 struct nduseroptmsg *ndmsg;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001108 struct net *net = dev_net(ra->dev);
Pierre Ynard31910572007-10-10 21:22:05 -07001109 int err;
1110 int base_size = NLMSG_ALIGN(sizeof(struct nduseroptmsg)
1111 + (opt->nd_opt_len << 3));
1112 size_t msg_size = base_size + nla_total_size(sizeof(struct in6_addr));
1113
1114 skb = nlmsg_new(msg_size, GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01001115 if (!skb) {
Pierre Ynard31910572007-10-10 21:22:05 -07001116 err = -ENOBUFS;
1117 goto errout;
1118 }
1119
1120 nlh = nlmsg_put(skb, 0, 0, RTM_NEWNDUSEROPT, base_size, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001121 if (!nlh) {
Pierre Ynard31910572007-10-10 21:22:05 -07001122 goto nla_put_failure;
1123 }
1124
1125 ndmsg = nlmsg_data(nlh);
1126 ndmsg->nduseropt_family = AF_INET6;
Pierre Ynarddbb2ed22007-11-12 17:58:35 -08001127 ndmsg->nduseropt_ifindex = ra->dev->ifindex;
Pierre Ynard31910572007-10-10 21:22:05 -07001128 ndmsg->nduseropt_icmp_type = icmp6h->icmp6_type;
1129 ndmsg->nduseropt_icmp_code = icmp6h->icmp6_code;
1130 ndmsg->nduseropt_opts_len = opt->nd_opt_len << 3;
1131
1132 memcpy(ndmsg + 1, opt, opt->nd_opt_len << 3);
1133
Jiri Benc930345e2015-03-29 16:59:25 +02001134 if (nla_put_in6_addr(skb, NDUSEROPT_SRCADDR, &ipv6_hdr(ra)->saddr))
David S. Millerc78679e2012-04-01 20:27:33 -04001135 goto nla_put_failure;
Pierre Ynard31910572007-10-10 21:22:05 -07001136 nlmsg_end(skb, nlh);
1137
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001138 rtnl_notify(skb, net, 0, RTNLGRP_ND_USEROPT, NULL, GFP_ATOMIC);
Pierre Ynard31910572007-10-10 21:22:05 -07001139 return;
1140
1141nla_put_failure:
1142 nlmsg_free(skb);
1143 err = -EMSGSIZE;
1144errout:
Daniel Lezcanoa18bc692008-03-07 11:14:49 -08001145 rtnl_set_sk_err(net, RTNLGRP_ND_USEROPT, err);
Pierre Ynard31910572007-10-10 21:22:05 -07001146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148static void ndisc_router_discovery(struct sk_buff *skb)
1149{
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001150 struct ra_msg *ra_msg = (struct ra_msg *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 struct neighbour *neigh = NULL;
1152 struct inet6_dev *in6_dev;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08001153 struct rt6_info *rt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 int lifetime;
1155 struct ndisc_options ndopts;
1156 int optlen;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001157 unsigned int pref = 0;
Marius Tomaschewskia394eef2015-08-31 15:59:22 +02001158 __u32 old_if_flags;
Marius Tomaschewski2053aeb2015-09-01 01:57:30 +02001159 bool send_ifinfo_notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Ian Morris67ba4152014-08-24 21:53:10 +01001161 __u8 *opt = (__u8 *)(ra_msg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Simon Horman29a3cad2013-05-28 20:34:26 +00001163 optlen = (skb_tail_pointer(skb) - skb_transport_header(skb)) -
1164 sizeof(struct ra_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Ben Greearf2a762d2014-06-25 14:44:52 -07001166 ND_PRINTK(2, info,
1167 "RA: %s, dev: %s\n",
1168 __func__, skb->dev->name);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001169 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
Joe Perches675418d2012-05-16 19:28:38 +00001170 ND_PRINTK(2, warn, "RA: source address is not link-local\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return;
1172 }
1173 if (optlen < 0) {
Joe Perches675418d2012-05-16 19:28:38 +00001174 ND_PRINTK(2, warn, "RA: packet too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return;
1176 }
1177
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001178#ifdef CONFIG_IPV6_NDISC_NODETYPE
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001179 if (skb->ndisc_nodetype == NDISC_NODETYPE_HOST) {
Joe Perches675418d2012-05-16 19:28:38 +00001180 ND_PRINTK(2, warn, "RA: from host or unauthorized router\n");
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001181 return;
1182 }
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001183#endif
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /*
1186 * set the RA_RECV flag in the interface
1187 */
1188
Eric Dumazetcfdf7642011-07-27 21:13:03 +00001189 in6_dev = __in6_dev_get(skb->dev);
Ian Morris63159f22015-03-29 14:00:04 +01001190 if (!in6_dev) {
Joe Perches675418d2012-05-16 19:28:38 +00001191 ND_PRINTK(0, err, "RA: can't find inet6 device for %s\n",
1192 skb->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return;
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Alexander Aringf997c552016-06-15 21:20:23 +02001196 if (!ndisc_parse_options(skb->dev, opt, optlen, &ndopts)) {
Joe Perches675418d2012-05-16 19:28:38 +00001197 ND_PRINTK(2, warn, "RA: invalid ND options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return;
1199 }
1200
Ben Greearf2a762d2014-06-25 14:44:52 -07001201 if (!ipv6_accept_ra(in6_dev)) {
1202 ND_PRINTK(2, info,
1203 "RA: %s, did not accept ra for dev: %s\n",
1204 __func__, skb->dev->name);
David Ward31ce8c72009-08-29 00:04:09 -07001205 goto skip_linkparms;
Ben Greearf2a762d2014-06-25 14:44:52 -07001206 }
David Ward31ce8c72009-08-29 00:04:09 -07001207
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001208#ifdef CONFIG_IPV6_NDISC_NODETYPE
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001209 /* skip link-specific parameters from interior routers */
Ben Greearf2a762d2014-06-25 14:44:52 -07001210 if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) {
1211 ND_PRINTK(2, info,
1212 "RA: %s, nodetype is NODEFAULT, dev: %s\n",
1213 __func__, skb->dev->name);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001214 goto skip_linkparms;
Ben Greearf2a762d2014-06-25 14:44:52 -07001215 }
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001216#endif
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (in6_dev->if_flags & IF_RS_SENT) {
1219 /*
1220 * flag that an RA was received after an RS was sent
1221 * out on this interface.
1222 */
1223 in6_dev->if_flags |= IF_RA_RCVD;
1224 }
1225
1226 /*
1227 * Remember the managed/otherconf flags from most recently
1228 * received RA message (RFC 2462) -- yoshfuji
1229 */
Marius Tomaschewskia394eef2015-08-31 15:59:22 +02001230 old_if_flags = in6_dev->if_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 in6_dev->if_flags = (in6_dev->if_flags & ~(IF_RA_MANAGED |
1232 IF_RA_OTHERCONF)) |
1233 (ra_msg->icmph.icmp6_addrconf_managed ?
1234 IF_RA_MANAGED : 0) |
1235 (ra_msg->icmph.icmp6_addrconf_other ?
1236 IF_RA_OTHERCONF : 0);
1237
Marius Tomaschewskia394eef2015-08-31 15:59:22 +02001238 if (old_if_flags != in6_dev->if_flags)
Marius Tomaschewski2053aeb2015-09-01 01:57:30 +02001239 send_ifinfo_notify = true;
Marius Tomaschewskia394eef2015-08-31 15:59:22 +02001240
Ben Greearf2a762d2014-06-25 14:44:52 -07001241 if (!in6_dev->cnf.accept_ra_defrtr) {
1242 ND_PRINTK(2, info,
1243 "RA: %s, defrtr is false for dev: %s\n",
1244 __func__, skb->dev->name);
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08001245 goto skip_defrtr;
Ben Greearf2a762d2014-06-25 14:44:52 -07001246 }
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08001247
Ben Greeard9333192014-06-25 14:44:53 -07001248 /* Do not accept RA with source-addr found on local machine unless
1249 * accept_ra_from_local is set to true.
1250 */
Li RongQingb6428812014-07-10 18:02:46 +08001251 if (!in6_dev->cnf.accept_ra_from_local &&
1252 ipv6_chk_addr(dev_net(in6_dev->dev), &ipv6_hdr(skb)->saddr,
Hannes Frederic Sowac1a9a292015-12-23 22:44:37 +01001253 in6_dev->dev, 0)) {
Ben Greearf2a762d2014-06-25 14:44:52 -07001254 ND_PRINTK(2, info,
Ben Greeard9333192014-06-25 14:44:53 -07001255 "RA from local address detected on dev: %s: default router ignored\n",
1256 skb->dev->name);
Andreas Hofmeister9f562202011-10-24 19:13:15 -04001257 goto skip_defrtr;
Ben Greearf2a762d2014-06-25 14:44:52 -07001258 }
Andreas Hofmeister9f562202011-10-24 19:13:15 -04001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 lifetime = ntohs(ra_msg->icmph.icmp6_rt_lifetime);
1261
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001262#ifdef CONFIG_IPV6_ROUTER_PREF
1263 pref = ra_msg->icmph.icmp6_router_pref;
1264 /* 10b is handled as if it were 00b (medium) */
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08001265 if (pref == ICMPV6_ROUTER_PREF_INVALID ||
YOSHIFUJI Hideaki6d5b78c2007-06-22 16:07:04 -07001266 !in6_dev->cnf.accept_ra_rtr_pref)
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001267 pref = ICMPV6_ROUTER_PREF_MEDIUM;
1268#endif
1269
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001270 rt = rt6_get_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
David S. Millereb857182012-01-27 15:07:56 -08001272 if (rt) {
1273 neigh = dst_neigh_lookup(&rt->dst, &ipv6_hdr(skb)->saddr);
1274 if (!neigh) {
Joe Perches675418d2012-05-16 19:28:38 +00001275 ND_PRINTK(0, err,
1276 "RA: %s got default router without neighbour\n",
1277 __func__);
Amerigo Wang94e187c2012-10-29 00:13:19 +00001278 ip6_rt_put(rt);
David S. Millereb857182012-01-27 15:07:56 -08001279 return;
1280 }
1281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (rt && lifetime == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001283 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 rt = NULL;
1285 }
1286
Ben Greearf2a762d2014-06-25 14:44:52 -07001287 ND_PRINTK(3, info, "RA: rt: %p lifetime: %d, for dev: %s\n",
1288 rt, lifetime, skb->dev->name);
Ian Morris63159f22015-03-29 14:00:04 +01001289 if (!rt && lifetime) {
Ben Greearf2a762d2014-06-25 14:44:52 -07001290 ND_PRINTK(3, info, "RA: adding default router\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001292 rt = rt6_add_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev, pref);
Ian Morris63159f22015-03-29 14:00:04 +01001293 if (!rt) {
Joe Perches675418d2012-05-16 19:28:38 +00001294 ND_PRINTK(0, err,
1295 "RA: %s failed to add default route\n",
1296 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return;
1298 }
1299
David S. Millereb857182012-01-27 15:07:56 -08001300 neigh = dst_neigh_lookup(&rt->dst, &ipv6_hdr(skb)->saddr);
Ian Morris63159f22015-03-29 14:00:04 +01001301 if (!neigh) {
Joe Perches675418d2012-05-16 19:28:38 +00001302 ND_PRINTK(0, err,
1303 "RA: %s got default router without neighbour\n",
1304 __func__);
Amerigo Wang94e187c2012-10-29 00:13:19 +00001305 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return;
1307 }
1308 neigh->flags |= NTF_ROUTER;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001309 } else if (rt) {
Pedro Ribeiro22441cf2008-10-15 15:47:49 -07001310 rt->rt6i_flags = (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312
1313 if (rt)
Gao feng1716a962012-04-06 00:13:10 +00001314 rt6_set_expires(rt, jiffies + (HZ * lifetime));
Hangbin Liu8013d1d2015-07-30 14:28:42 +08001315 if (in6_dev->cnf.accept_ra_min_hop_limit < 256 &&
1316 ra_msg->icmph.icmp6_hop_limit) {
1317 if (in6_dev->cnf.accept_ra_min_hop_limit <= ra_msg->icmph.icmp6_hop_limit) {
D.S. Ljungmark6fd99092015-03-25 09:28:15 +01001318 in6_dev->cnf.hop_limit = ra_msg->icmph.icmp6_hop_limit;
Hangbin Liu8013d1d2015-07-30 14:28:42 +08001319 if (rt)
1320 dst_metric_set(&rt->dst, RTAX_HOPLIMIT,
1321 ra_msg->icmph.icmp6_hop_limit);
D.S. Ljungmark6fd99092015-03-25 09:28:15 +01001322 } else {
Hangbin Liu8013d1d2015-07-30 14:28:42 +08001323 ND_PRINTK(2, warn, "RA: Got route advertisement with lower hop_limit than minimum\n");
D.S. Ljungmark6fd99092015-03-25 09:28:15 +01001324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08001327skip_defrtr:
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 /*
1330 * Update Reachable Time and Retrans Timer
1331 */
1332
1333 if (in6_dev->nd_parms) {
1334 unsigned long rtime = ntohl(ra_msg->retrans_timer);
1335
1336 if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/HZ) {
1337 rtime = (rtime*HZ)/1000;
1338 if (rtime < HZ/10)
1339 rtime = HZ/10;
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001340 NEIGH_VAR_SET(in6_dev->nd_parms, RETRANS_TIME, rtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 in6_dev->tstamp = jiffies;
Marius Tomaschewski2053aeb2015-09-01 01:57:30 +02001342 send_ifinfo_notify = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344
1345 rtime = ntohl(ra_msg->reachable_time);
1346 if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/(3*HZ)) {
1347 rtime = (rtime*HZ)/1000;
1348
1349 if (rtime < HZ/10)
1350 rtime = HZ/10;
1351
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001352 if (rtime != NEIGH_VAR(in6_dev->nd_parms, BASE_REACHABLE_TIME)) {
1353 NEIGH_VAR_SET(in6_dev->nd_parms,
1354 BASE_REACHABLE_TIME, rtime);
1355 NEIGH_VAR_SET(in6_dev->nd_parms,
1356 GC_STALETIME, 3 * rtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 in6_dev->nd_parms->reachable_time = neigh_rand_reach_time(rtime);
1358 in6_dev->tstamp = jiffies;
Marius Tomaschewski2053aeb2015-09-01 01:57:30 +02001359 send_ifinfo_notify = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361 }
1362 }
1363
Marius Tomaschewski2053aeb2015-09-01 01:57:30 +02001364 /*
1365 * Send a notify if RA changed managed/otherconf flags or timer settings
1366 */
1367 if (send_ifinfo_notify)
1368 inet6_ifinfo_notify(RTM_NEWLINK, in6_dev);
1369
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001370skip_linkparms:
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 /*
1373 * Process options.
1374 */
1375
1376 if (!neigh)
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001377 neigh = __neigh_lookup(&nd_tbl, &ipv6_hdr(skb)->saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 skb->dev, 1);
1379 if (neigh) {
1380 u8 *lladdr = NULL;
1381 if (ndopts.nd_opts_src_lladdr) {
1382 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
1383 skb->dev);
1384 if (!lladdr) {
Joe Perches675418d2012-05-16 19:28:38 +00001385 ND_PRINTK(2, warn,
1386 "RA: invalid link-layer address length\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 goto out;
1388 }
1389 }
Alexander Aringf997c552016-06-15 21:20:23 +02001390 ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1392 NEIGH_UPDATE_F_OVERRIDE|
1393 NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
Alexander Aringf997c552016-06-15 21:20:23 +02001394 NEIGH_UPDATE_F_ISROUTER,
1395 NDISC_ROUTER_ADVERTISEMENT, &ndopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
Ben Greearf2a762d2014-06-25 14:44:52 -07001398 if (!ipv6_accept_ra(in6_dev)) {
1399 ND_PRINTK(2, info,
1400 "RA: %s, accept_ra is false for dev: %s\n",
1401 __func__, skb->dev->name);
David Ward31ce8c72009-08-29 00:04:09 -07001402 goto out;
Ben Greearf2a762d2014-06-25 14:44:52 -07001403 }
David Ward31ce8c72009-08-29 00:04:09 -07001404
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001405#ifdef CONFIG_IPV6_ROUTE_INFO
Li RongQingb6428812014-07-10 18:02:46 +08001406 if (!in6_dev->cnf.accept_ra_from_local &&
1407 ipv6_chk_addr(dev_net(in6_dev->dev), &ipv6_hdr(skb)->saddr,
Hannes Frederic Sowac1a9a292015-12-23 22:44:37 +01001408 in6_dev->dev, 0)) {
Ben Greearf2a762d2014-06-25 14:44:52 -07001409 ND_PRINTK(2, info,
Ben Greeard9333192014-06-25 14:44:53 -07001410 "RA from local address detected on dev: %s: router info ignored.\n",
1411 skb->dev->name);
Andreas Hofmeister9f562202011-10-24 19:13:15 -04001412 goto skip_routeinfo;
Ben Greearf2a762d2014-06-25 14:44:52 -07001413 }
Andreas Hofmeister9f562202011-10-24 19:13:15 -04001414
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08001415 if (in6_dev->cnf.accept_ra_rtr_pref && ndopts.nd_opts_ri) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001416 struct nd_opt_hdr *p;
1417 for (p = ndopts.nd_opts_ri;
1418 p;
1419 p = ndisc_next_option(p, ndopts.nd_opts_ri_end)) {
YOSHIFUJI Hideaki6294e002008-03-15 23:56:52 -04001420 struct route_info *ri = (struct route_info *)p;
1421#ifdef CONFIG_IPV6_NDISC_NODETYPE
1422 if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT &&
1423 ri->prefix_len == 0)
1424 continue;
1425#endif
Duan Jiong30e56912013-11-26 15:46:56 +08001426 if (ri->prefix_len == 0 &&
1427 !in6_dev->cnf.accept_ra_defrtr)
1428 continue;
Joel Scherpelzbbea1242017-03-22 18:19:04 +09001429 if (ri->prefix_len < in6_dev->cnf.accept_ra_rt_info_min_plen)
1430 continue;
YOSHIFUJI Hideaki6294e002008-03-15 23:56:52 -04001431 if (ri->prefix_len > in6_dev->cnf.accept_ra_rt_info_max_plen)
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08001432 continue;
Ian Morris67ba4152014-08-24 21:53:10 +01001433 rt6_route_rcv(skb->dev, (u8 *)p, (p->nd_opt_len) << 3,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001434 &ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001435 }
1436 }
Andreas Hofmeister9f562202011-10-24 19:13:15 -04001437
1438skip_routeinfo:
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001439#endif
1440
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001441#ifdef CONFIG_IPV6_NDISC_NODETYPE
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001442 /* skip link-specific ndopts from interior routers */
Ben Greearf2a762d2014-06-25 14:44:52 -07001443 if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) {
1444 ND_PRINTK(2, info,
1445 "RA: %s, nodetype is NODEFAULT (interior routes), dev: %s\n",
1446 __func__, skb->dev->name);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001447 goto out;
Ben Greearf2a762d2014-06-25 14:44:52 -07001448 }
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001449#endif
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001450
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08001451 if (in6_dev->cnf.accept_ra_pinfo && ndopts.nd_opts_pi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 struct nd_opt_hdr *p;
1453 for (p = ndopts.nd_opts_pi;
1454 p;
1455 p = ndisc_next_option(p, ndopts.nd_opts_pi_end)) {
Neil Hormane6bff992012-01-04 10:49:15 +00001456 addrconf_prefix_rcv(skb->dev, (u8 *)p,
1457 (p->nd_opt_len) << 3,
1458 ndopts.nd_opts_src_lladdr != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460 }
1461
Harout Hedeshianc2943f12015-01-20 10:06:05 -07001462 if (ndopts.nd_opts_mtu && in6_dev->cnf.accept_ra_mtu) {
Al Viroe69a4ad2006-11-14 20:56:00 -08001463 __be32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 u32 mtu;
1465
Ian Morris67ba4152014-08-24 21:53:10 +01001466 memcpy(&n, ((u8 *)(ndopts.nd_opts_mtu+1))+2, sizeof(mtu));
Al Viroe69a4ad2006-11-14 20:56:00 -08001467 mtu = ntohl(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 if (mtu < IPV6_MIN_MTU || mtu > skb->dev->mtu) {
Joe Perches675418d2012-05-16 19:28:38 +00001470 ND_PRINTK(2, warn, "RA: invalid mtu: %d\n", mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 } else if (in6_dev->cnf.mtu6 != mtu) {
1472 in6_dev->cnf.mtu6 = mtu;
1473
1474 if (rt)
David S. Millerdefb3512010-12-08 21:16:57 -08001475 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 rt6_mtu_change(skb->dev, mtu);
1478 }
1479 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001480
Pierre Ynard31910572007-10-10 21:22:05 -07001481 if (ndopts.nd_useropts) {
YOSHIFUJI Hideaki61cf46ad2008-01-22 17:32:53 +09001482 struct nd_opt_hdr *p;
1483 for (p = ndopts.nd_useropts;
1484 p;
Alexander Aringf997c552016-06-15 21:20:23 +02001485 p = ndisc_next_useropt(skb->dev, p,
1486 ndopts.nd_useropts_end)) {
YOSHIFUJI Hideaki61cf46ad2008-01-22 17:32:53 +09001487 ndisc_ra_useropt(skb, p);
Pierre Ynard31910572007-10-10 21:22:05 -07001488 }
1489 }
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (ndopts.nd_opts_tgt_lladdr || ndopts.nd_opts_rh) {
Joe Perches675418d2012-05-16 19:28:38 +00001492 ND_PRINTK(2, warn, "RA: invalid RA options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494out:
Amerigo Wang94e187c2012-10-29 00:13:19 +00001495 ip6_rt_put(rt);
David S. Millereb857182012-01-27 15:07:56 -08001496 if (neigh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 neigh_release(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500static void ndisc_redirect_rcv(struct sk_buff *skb)
1501{
Duan Jiong093d04d2012-12-14 02:59:59 +00001502 u8 *hdr;
1503 struct ndisc_options ndopts;
1504 struct rd_msg *msg = (struct rd_msg *)skb_transport_header(skb);
Simon Horman29a3cad2013-05-28 20:34:26 +00001505 u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
Duan Jiong093d04d2012-12-14 02:59:59 +00001506 offsetof(struct rd_msg, opt));
1507
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001508#ifdef CONFIG_IPV6_NDISC_NODETYPE
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001509 switch (skb->ndisc_nodetype) {
1510 case NDISC_NODETYPE_HOST:
1511 case NDISC_NODETYPE_NODEFAULT:
Joe Perches675418d2012-05-16 19:28:38 +00001512 ND_PRINTK(2, warn,
1513 "Redirect: from host or unauthorized router\n");
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001514 return;
1515 }
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001516#endif
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001517
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001518 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
Joe Perches675418d2012-05-16 19:28:38 +00001519 ND_PRINTK(2, warn,
1520 "Redirect: source address is not link-local\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return;
1522 }
1523
Alexander Aringf997c552016-06-15 21:20:23 +02001524 if (!ndisc_parse_options(skb->dev, msg->opt, ndoptlen, &ndopts))
Duan Jiong093d04d2012-12-14 02:59:59 +00001525 return;
1526
Duan Jiongc92a59e2013-08-22 12:07:35 +08001527 if (!ndopts.nd_opts_rh) {
Duan Jiongb55b76b2013-09-04 19:44:21 +08001528 ip6_redirect_no_header(skb, dev_net(skb->dev),
1529 skb->dev->ifindex, 0);
Duan Jiong093d04d2012-12-14 02:59:59 +00001530 return;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001531 }
Duan Jiong093d04d2012-12-14 02:59:59 +00001532
1533 hdr = (u8 *)ndopts.nd_opts_rh;
1534 hdr += 8;
1535 if (!pskb_pull(skb, hdr - skb_transport_header(skb)))
1536 return;
1537
David S. Millerb94f1c02012-07-12 00:33:37 -07001538 icmpv6_notify(skb, NDISC_REDIRECT, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
YOSHIFUJI Hideaki / 吉藤英明5f5a0112013-01-21 06:48:53 +00001541static void ndisc_fill_redirect_hdr_option(struct sk_buff *skb,
1542 struct sk_buff *orig_skb,
1543 int rd_len)
YOSHIFUJI Hideaki / 吉藤英明9c86daf2013-01-21 06:48:09 +00001544{
YOSHIFUJI Hideaki / 吉藤英明5f5a0112013-01-21 06:48:53 +00001545 u8 *opt = skb_put(skb, rd_len);
1546
YOSHIFUJI Hideaki / 吉藤英明9c86daf2013-01-21 06:48:09 +00001547 memset(opt, 0, 8);
1548 *(opt++) = ND_OPT_REDIRECT_HDR;
1549 *(opt++) = (rd_len >> 3);
1550 opt += 6;
1551
1552 memcpy(opt, ipv6_hdr(orig_skb), rd_len - 8);
YOSHIFUJI Hideaki / 吉藤英明9c86daf2013-01-21 06:48:09 +00001553}
1554
David S. Miller49919692012-01-27 15:30:48 -08001555void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001557 struct net_device *dev = skb->dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001558 struct net *net = dev_net(dev);
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001559 struct sock *sk = net->ipv6.ndisc_sk;
YOSHIFUJI Hideaki / 吉藤英明2ce135762013-01-21 06:48:49 +00001560 int optlen = 0;
David S. Millerfbfe95a2012-06-08 23:24:18 -07001561 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 struct sk_buff *buff;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001563 struct rd_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 struct in6_addr saddr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 struct rt6_info *rt;
1566 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -05001567 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 int rd_len;
Alexander Aringf997c552016-06-15 21:20:23 +02001569 u8 ha_buf[MAX_ADDR_LEN], *ha = NULL,
1570 ops_data_buf[NDISC_OPS_REDIRECT_DATA_SPACE], *ops_data = NULL;
David S. Miller1d861aa2012-07-10 03:58:16 -07001571 bool ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Neil Horman95c385b2007-04-25 17:08:10 -07001573 if (ipv6_get_lladdr(dev, &saddr_buf, IFA_F_TENTATIVE)) {
Joe Perches675418d2012-05-16 19:28:38 +00001574 ND_PRINTK(2, warn, "Redirect: no link-local address on %s\n",
1575 dev->name);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001576 return;
1577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001579 if (!ipv6_addr_equal(&ipv6_hdr(skb)->daddr, target) &&
Brian Haleybf0b48d2007-10-08 00:12:05 -07001580 ipv6_addr_type(target) != (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
Joe Perches675418d2012-05-16 19:28:38 +00001581 ND_PRINTK(2, warn,
1582 "Redirect: target address is not link-local unicast\n");
Li Yewang29556522007-01-30 14:33:20 -08001583 return;
1584 }
1585
David S. Miller4c9483b2011-03-12 16:22:43 -05001586 icmpv6_flow_init(sk, &fl6, NDISC_REDIRECT,
David Aherne0d56fd2016-09-10 12:09:57 -07001587 &saddr_buf, &ipv6_hdr(skb)->saddr, dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
David S. Miller4c9483b2011-03-12 16:22:43 -05001589 dst = ip6_route_output(net, NULL, &fl6);
RongQing.Li5095d642012-02-21 22:10:49 +00001590 if (dst->error) {
1591 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return;
RongQing.Li5095d642012-02-21 22:10:49 +00001593 }
David S. Miller4c9483b2011-03-12 16:22:43 -05001594 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
David S. Miller452edd52011-03-02 13:27:41 -08001595 if (IS_ERR(dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 rt = (struct rt6_info *) dst;
1599
1600 if (rt->rt6i_flags & RTF_GATEWAY) {
Joe Perches675418d2012-05-16 19:28:38 +00001601 ND_PRINTK(2, warn,
1602 "Redirect: destination is not a neighbour\n");
Ilpo Järvinend73f0802009-02-06 23:47:37 -08001603 goto release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
Martin KaFai Laufd0273d2015-05-22 20:55:57 -07001605 peer = inet_getpeer_v6(net->ipv6.peers, &ipv6_hdr(skb)->saddr, 1);
David S. Miller1d861aa2012-07-10 03:58:16 -07001606 ret = inet_peer_xrlim_allow(peer, 1*HZ);
1607 if (peer)
1608 inet_putpeer(peer);
1609 if (!ret)
Ilpo Järvinend73f0802009-02-06 23:47:37 -08001610 goto release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 if (dev->addr_len) {
David S. Miller49919692012-01-27 15:30:48 -08001613 struct neighbour *neigh = dst_neigh_lookup(skb_dst(skb), target);
1614 if (!neigh) {
Joe Perches675418d2012-05-16 19:28:38 +00001615 ND_PRINTK(2, warn,
1616 "Redirect: no neigh for target address\n");
David S. Miller49919692012-01-27 15:30:48 -08001617 goto release;
1618 }
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 read_lock_bh(&neigh->lock);
1621 if (neigh->nud_state & NUD_VALID) {
1622 memcpy(ha_buf, neigh->ha, dev->addr_len);
1623 read_unlock_bh(&neigh->lock);
1624 ha = ha_buf;
Alexander Aringf997c552016-06-15 21:20:23 +02001625 optlen += ndisc_redirect_opt_addr_space(dev, neigh,
1626 ops_data_buf,
1627 &ops_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 } else
1629 read_unlock_bh(&neigh->lock);
David S. Miller49919692012-01-27 15:30:48 -08001630
1631 neigh_release(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633
1634 rd_len = min_t(unsigned int,
YOSHIFUJI Hideaki / 吉藤英明2ce135762013-01-21 06:48:49 +00001635 IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(*msg) - optlen,
1636 skb->len + 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 rd_len &= ~0x7;
YOSHIFUJI Hideaki / 吉藤英明2ce135762013-01-21 06:48:49 +00001638 optlen += rd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
YOSHIFUJI Hideaki / 吉藤英明2ce135762013-01-21 06:48:49 +00001640 buff = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +00001641 if (!buff)
Ilpo Järvinend73f0802009-02-06 23:47:37 -08001642 goto release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Johannes Berg4df864c2017-06-16 14:29:21 +02001644 msg = skb_put(buff, sizeof(*msg));
YOSHIFUJI Hideaki / 吉藤英明4d5c1522013-01-21 06:49:25 +00001645 *msg = (struct rd_msg) {
1646 .icmph = {
1647 .icmp6_type = NDISC_REDIRECT,
1648 },
1649 .target = *target,
1650 .dest = ipv6_hdr(skb)->daddr,
1651 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /*
1654 * include target_address option
1655 */
1656
1657 if (ha)
Alexander Aringf997c552016-06-15 21:20:23 +02001658 ndisc_fill_redirect_addr_option(buff, ha, ops_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 /*
1661 * build redirect option and copy skb over to the new packet.
1662 */
1663
YOSHIFUJI Hideaki / 吉藤英明9c86daf2013-01-21 06:48:09 +00001664 if (rd_len)
YOSHIFUJI Hideaki / 吉藤英明5f5a0112013-01-21 06:48:53 +00001665 ndisc_fill_redirect_hdr_option(buff, skb, rd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Eric Dumazetadf30902009-06-02 05:19:30 +00001667 skb_dst_set(buff, dst);
YOSHIFUJI Hideaki / 吉藤英明f4de84c2013-01-21 06:49:03 +00001668 ndisc_send_skb(buff, &ipv6_hdr(skb)->saddr, &saddr_buf);
Ilpo Järvinend73f0802009-02-06 23:47:37 -08001669 return;
1670
1671release:
1672 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674
1675static void pndisc_redo(struct sk_buff *skb)
1676{
YOSHIFUJI Hideaki140e26fc2005-10-05 12:11:41 -07001677 ndisc_recv_ns(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 kfree_skb(skb);
1679}
1680
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02001681static bool ndisc_suppress_frag_ndisc(struct sk_buff *skb)
1682{
1683 struct inet6_dev *idev = __in6_dev_get(skb->dev);
1684
1685 if (!idev)
1686 return true;
1687 if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED &&
1688 idev->cnf.suppress_frag_ndisc) {
1689 net_warn_ratelimited("Received fragmented ndisc packet. Carefully consider disabling suppress_frag_ndisc.\n");
1690 return true;
1691 }
1692 return false;
1693}
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695int ndisc_rcv(struct sk_buff *skb)
1696{
1697 struct nd_msg *msg;
1698
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02001699 if (ndisc_suppress_frag_ndisc(skb))
1700 return 0;
1701
YOSHIFUJI Hideaki / 吉藤英明6bce6b42013-01-21 06:48:03 +00001702 if (skb_linearize(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return 0;
1704
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001705 msg = (struct nd_msg *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001707 __skb_push(skb, skb->data - skb_transport_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001709 if (ipv6_hdr(skb)->hop_limit != 255) {
Joe Perches675418d2012-05-16 19:28:38 +00001710 ND_PRINTK(2, warn, "NDISC: invalid hop-limit: %d\n",
1711 ipv6_hdr(skb)->hop_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return 0;
1713 }
1714
1715 if (msg->icmph.icmp6_code != 0) {
Joe Perches675418d2012-05-16 19:28:38 +00001716 ND_PRINTK(2, warn, "NDISC: invalid ICMPv6 code: %d\n",
1717 msg->icmph.icmp6_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return 0;
1719 }
1720
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001721 memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 switch (msg->icmph.icmp6_type) {
1724 case NDISC_NEIGHBOUR_SOLICITATION:
1725 ndisc_recv_ns(skb);
1726 break;
1727
1728 case NDISC_NEIGHBOUR_ADVERTISEMENT:
1729 ndisc_recv_na(skb);
1730 break;
1731
1732 case NDISC_ROUTER_SOLICITATION:
1733 ndisc_recv_rs(skb);
1734 break;
1735
1736 case NDISC_ROUTER_ADVERTISEMENT:
1737 ndisc_router_discovery(skb);
1738 break;
1739
1740 case NDISC_REDIRECT:
1741 ndisc_redirect_rcv(skb);
1742 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 return 0;
1746}
1747
1748static int ndisc_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
1749{
Jiri Pirko351638e2013-05-28 01:30:21 +00001750 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric Dumazetc8507fb2015-07-29 12:01:41 +02001751 struct netdev_notifier_change_info *change_info;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001752 struct net *net = dev_net(dev);
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00001753 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 switch (event) {
1756 case NETDEV_CHANGEADDR:
1757 neigh_changeaddr(&nd_tbl, dev);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001758 fib6_run_gc(0, net, false);
David Ahern4a6e3c52017-04-12 11:49:04 -07001759 /* fallthrough */
1760 case NETDEV_UP:
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00001761 idev = in6_dev_get(dev);
1762 if (!idev)
1763 break;
David Ahernfc1f8f42017-04-22 09:10:13 -07001764 if (idev->cnf.ndisc_notify ||
1765 net->ipv6.devconf_all->ndisc_notify)
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00001766 ndisc_send_unsol_na(dev);
1767 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 break;
Eric Dumazetc8507fb2015-07-29 12:01:41 +02001769 case NETDEV_CHANGE:
1770 change_info = ptr;
1771 if (change_info->flags_changed & IFF_NOARP)
1772 neigh_changeaddr(&nd_tbl, dev);
1773 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 case NETDEV_DOWN:
1775 neigh_ifdown(&nd_tbl, dev);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001776 fib6_run_gc(0, net, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 break;
Ben Hutchingsf47b9462011-04-15 13:46:02 +00001778 case NETDEV_NOTIFY_PEERS:
1779 ndisc_send_unsol_na(dev);
1780 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 default:
1782 break;
1783 }
1784
1785 return NOTIFY_DONE;
1786}
1787
1788static struct notifier_block ndisc_netdev_notifier = {
1789 .notifier_call = ndisc_netdev_event,
David Ahern6eb79392017-08-08 15:51:02 -06001790 .priority = ADDRCONF_NOTIFY_PRIORITY - 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791};
1792
1793#ifdef CONFIG_SYSCTL
1794static void ndisc_warn_deprecated_sysctl(struct ctl_table *ctl,
1795 const char *func, const char *dev_name)
1796{
1797 static char warncomm[TASK_COMM_LEN];
1798 static int warned;
1799 if (strcmp(warncomm, current->comm) && warned < 5) {
1800 strcpy(warncomm, current->comm);
Joe Perchesf3213832012-05-15 14:11:53 +00001801 pr_warn("process `%s' is using deprecated sysctl (%s) net.ipv6.neigh.%s.%s - use net.ipv6.neigh.%s.%s_ms instead\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 warncomm, func,
1803 dev_name, ctl->procname,
1804 dev_name, ctl->procname);
1805 warned++;
1806 }
1807}
1808
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001809int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
1811 struct net_device *dev = ctl->extra1;
1812 struct inet6_dev *idev;
1813 int ret;
1814
Eric W. Biedermand12af672007-10-18 03:05:25 -07001815 if ((strcmp(ctl->procname, "retrans_time") == 0) ||
1816 (strcmp(ctl->procname, "base_reachable_time") == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 ndisc_warn_deprecated_sysctl(ctl, "syscall", dev ? dev->name : "default");
1818
Eric W. Biedermand12af672007-10-18 03:05:25 -07001819 if (strcmp(ctl->procname, "retrans_time") == 0)
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01001820 ret = neigh_proc_dointvec(ctl, write, buffer, lenp, ppos);
Eric W. Biedermand12af672007-10-18 03:05:25 -07001821
1822 else if (strcmp(ctl->procname, "base_reachable_time") == 0)
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01001823 ret = neigh_proc_dointvec_jiffies(ctl, write,
1824 buffer, lenp, ppos);
Eric W. Biedermand12af672007-10-18 03:05:25 -07001825
1826 else if ((strcmp(ctl->procname, "retrans_time_ms") == 0) ||
YOSHIFUJI Hideakiad02ac12007-10-29 01:32:23 -07001827 (strcmp(ctl->procname, "base_reachable_time_ms") == 0))
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01001828 ret = neigh_proc_dointvec_ms_jiffies(ctl, write,
1829 buffer, lenp, ppos);
Eric W. Biedermand12af672007-10-18 03:05:25 -07001830 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 if (write && ret == 0 && dev && (idev = in6_dev_get(dev)) != NULL) {
Jiri Pirko1f9248e2013-12-07 19:26:53 +01001834 if (ctl->data == &NEIGH_VAR(idev->nd_parms, BASE_REACHABLE_TIME))
1835 idev->nd_parms->reachable_time =
1836 neigh_rand_reach_time(NEIGH_VAR(idev->nd_parms, BASE_REACHABLE_TIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 idev->tstamp = jiffies;
1838 inet6_ifinfo_notify(RTM_NEWLINK, idev);
1839 in6_dev_put(idev);
1840 }
1841 return ret;
1842}
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845#endif
1846
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001847static int __net_init ndisc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 struct ipv6_pinfo *np;
1850 struct sock *sk;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001851 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Denis V. Lunev1ed85162008-04-03 14:31:03 -07001853 err = inet_ctl_sock_create(&sk, PF_INET6,
1854 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (err < 0) {
Joe Perches675418d2012-05-16 19:28:38 +00001856 ND_PRINTK(0, err,
1857 "NDISC: Failed to initialize the control socket (err %d)\n",
1858 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return err;
1860 }
1861
Denis V. Lunev1ed85162008-04-03 14:31:03 -07001862 net->ipv6.ndisc_sk = sk;
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 np->hop_limit = 255;
1866 /* Do not loopback ndisc messages */
1867 np->mc_loop = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001869 return 0;
1870}
1871
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001872static void __net_exit ndisc_net_exit(struct net *net)
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001873{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07001874 inet_ctl_sock_destroy(net->ipv6.ndisc_sk);
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001875}
1876
1877static struct pernet_operations ndisc_net_ops = {
1878 .init = ndisc_net_init,
1879 .exit = ndisc_net_exit,
1880};
1881
1882int __init ndisc_init(void)
1883{
1884 int err;
1885
1886 err = register_pernet_subsys(&ndisc_net_ops);
1887 if (err)
1888 return err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001889 /*
1890 * Initialize the neighbour table
1891 */
WANG Congd7480fd2014-11-10 15:59:36 -08001892 neigh_table_init(NEIGH_ND_TABLE, &nd_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894#ifdef CONFIG_SYSCTL
Jiri Pirko73af6142013-12-07 19:26:55 +01001895 err = neigh_sysctl_register(NULL, &nd_tbl.parms,
Himangi Saraogi56ec0fb2014-07-25 01:49:37 +05301896 ndisc_ifinfo_sysctl_change);
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001897 if (err)
1898 goto out_unregister_pernet;
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001899out:
Fabio Estevambcd081a2013-11-16 00:52:08 -02001900#endif
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001901 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001903#ifdef CONFIG_SYSCTL
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001904out_unregister_pernet:
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001905 unregister_pernet_subsys(&ndisc_net_ops);
1906 goto out;
Michal Kubeček2c861cc2013-09-09 21:45:04 +02001907#endif
1908}
1909
1910int __init ndisc_late_init(void)
1911{
1912 return register_netdevice_notifier(&ndisc_netdev_notifier);
1913}
1914
1915void ndisc_late_cleanup(void)
1916{
1917 unregister_netdevice_notifier(&ndisc_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
1920void ndisc_cleanup(void)
1921{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922#ifdef CONFIG_SYSCTL
1923 neigh_sysctl_unregister(&nd_tbl.parms);
1924#endif
WANG Congd7480fd2014-11-10 15:59:36 -08001925 neigh_table_clear(NEIGH_ND_TABLE, &nd_tbl);
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001926 unregister_pernet_subsys(&ndisc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}