blob: 71fde6cafb35d6e63da6bd18811a1b4322936892 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/if_arp.h>
50#include <linux/ipv6.h>
51#include <linux/icmpv6.h>
52#include <linux/jhash.h>
53
54#include <net/sock.h>
55#include <net/snmp.h>
56
57#include <net/ipv6.h>
58#include <net/protocol.h>
59#include <net/ndisc.h>
60#include <net/ip6_route.h>
61#include <net/addrconf.h>
62#include <net/icmp.h>
63
Pierre Ynard31910572007-10-10 21:22:05 -070064#include <net/netlink.h>
65#include <linux/rtnetlink.h>
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <net/flow.h>
68#include <net/ip6_checksum.h>
Denis V. Lunev1ed85162008-04-03 14:31:03 -070069#include <net/inet_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/proc_fs.h>
71
72#include <linux/netfilter.h>
73#include <linux/netfilter_ipv6.h>
74
Joe Perches675418d2012-05-16 19:28:38 +000075/* Set to 3 to get tracing... */
76#define ND_DEBUG 1
77
78#define ND_PRINTK(val, level, fmt, ...) \
79do { \
80 if (val <= ND_DEBUG) \
81 net_##level##_ratelimited(fmt, ##__VA_ARGS__); \
82} while (0)
83
Eric Dumazetd6bf7812010-10-04 06:15:44 +000084static u32 ndisc_hash(const void *pkey,
85 const struct net_device *dev,
David S. Miller2c2aba62011-12-28 15:06:58 -050086 __u32 *hash_rnd);
Eric W. Biederman60395a22015-03-03 17:10:44 -060087static bool ndisc_key_eq(const struct neighbour *neigh, const void *pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static int ndisc_constructor(struct neighbour *neigh);
89static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb);
90static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb);
91static int pndisc_constructor(struct pneigh_entry *n);
92static void pndisc_destructor(struct pneigh_entry *n);
93static void pndisc_redo(struct sk_buff *skb);
94
Stephen Hemminger89d69d22009-09-01 11:13:19 +000095static const struct neigh_ops ndisc_generic_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_connected_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Stephen Hemminger89d69d22009-09-01 11:13:19 +0000103static const struct neigh_ops ndisc_hh_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .family = AF_INET6,
105 .solicit = ndisc_solicit,
106 .error_report = ndisc_error_report,
107 .output = neigh_resolve_output,
108 .connected_output = neigh_resolve_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111
Stephen Hemminger89d69d22009-09-01 11:13:19 +0000112static const struct neigh_ops ndisc_direct_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .family = AF_INET6,
David S. Miller8f40b162011-07-17 13:34:11 -0700114 .output = neigh_direct_output,
115 .connected_output = neigh_direct_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
118struct neigh_table nd_tbl = {
119 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .key_len = sizeof(struct in6_addr),
Eric W. Biedermanbdf53c52015-03-02 00:13:22 -0600121 .protocol = cpu_to_be16(ETH_P_IPV6),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .hash = ndisc_hash,
Eric W. Biederman60395a22015-03-03 17:10:44 -0600123 .key_eq = ndisc_key_eq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .constructor = ndisc_constructor,
125 .pconstructor = pndisc_constructor,
126 .pdestructor = pndisc_destructor,
127 .proxy_redo = pndisc_redo,
128 .id = "ndisc_cache",
129 .parms = {
Shan Weib6720832010-12-01 18:05:12 +0000130 .tbl = &nd_tbl,
Shan Weib6720832010-12-01 18:05:12 +0000131 .reachable_time = ND_REACHABLE_TIME,
Jiri Pirko1f9248e52013-12-07 19:26:53 +0100132 .data = {
133 [NEIGH_VAR_MCAST_PROBES] = 3,
134 [NEIGH_VAR_UCAST_PROBES] = 3,
135 [NEIGH_VAR_RETRANS_TIME] = ND_RETRANS_TIMER,
136 [NEIGH_VAR_BASE_REACHABLE_TIME] = ND_REACHABLE_TIME,
137 [NEIGH_VAR_DELAY_PROBE_TIME] = 5 * HZ,
138 [NEIGH_VAR_GC_STALETIME] = 60 * HZ,
139 [NEIGH_VAR_QUEUE_LEN_BYTES] = 64 * 1024,
140 [NEIGH_VAR_PROXY_QLEN] = 64,
141 [NEIGH_VAR_ANYCAST_DELAY] = 1 * HZ,
142 [NEIGH_VAR_PROXY_DELAY] = (8 * HZ) / 10,
143 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 },
145 .gc_interval = 30 * HZ,
146 .gc_thresh1 = 128,
147 .gc_thresh2 = 512,
148 .gc_thresh3 = 1024,
149};
150
YOSHIFUJI Hideaki / 吉藤英明5f5a0112013-01-21 06:48:53 +0000151static void ndisc_fill_addr_option(struct sk_buff *skb, int type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
YOSHIFUJI Hideaki / 吉藤英明5f5a0112013-01-21 06:48:53 +0000153 int pad = ndisc_addr_option_pad(skb->dev->type);
154 int data_len = skb->dev->addr_len;
155 int space = ndisc_opt_addr_space(skb->dev);
156 u8 *opt = skb_put(skb, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 opt[0] = type;
159 opt[1] = space>>3;
160
161 memset(opt + 2, 0, pad);
162 opt += pad;
163 space -= pad;
164
165 memcpy(opt+2, data, data_len);
166 data_len += 2;
167 opt += data_len;
Ian Morrise5d08d72014-11-23 21:28:43 +0000168 space -= data_len;
169 if (space > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 memset(opt, 0, space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173static struct nd_opt_hdr *ndisc_next_option(struct nd_opt_hdr *cur,
174 struct nd_opt_hdr *end)
175{
176 int type;
177 if (!cur || !end || cur >= end)
178 return NULL;
179 type = cur->nd_opt_type;
180 do {
181 cur = ((void *)cur) + (cur->nd_opt_len << 3);
Ian Morris67ba4152014-08-24 21:53:10 +0100182 } while (cur < end && cur->nd_opt_type != type);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000183 return cur <= end && cur->nd_opt_type == type ? cur : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Pierre Ynard31910572007-10-10 21:22:05 -0700186static inline int ndisc_is_useropt(struct nd_opt_hdr *opt)
187{
Alexey I. Froloffe35f30c2012-04-06 05:50:58 +0000188 return opt->nd_opt_type == ND_OPT_RDNSS ||
189 opt->nd_opt_type == ND_OPT_DNSSL;
Pierre Ynard31910572007-10-10 21:22:05 -0700190}
191
192static struct nd_opt_hdr *ndisc_next_useropt(struct nd_opt_hdr *cur,
193 struct nd_opt_hdr *end)
194{
195 if (!cur || !end || cur >= end)
196 return NULL;
197 do {
198 cur = ((void *)cur) + (cur->nd_opt_len << 3);
Ian Morris67ba4152014-08-24 21:53:10 +0100199 } while (cur < end && !ndisc_is_useropt(cur));
Eric Dumazeta02cec22010-09-22 20:43:57 +0000200 return cur <= end && ndisc_is_useropt(cur) ? cur : NULL;
Pierre Ynard31910572007-10-10 21:22:05 -0700201}
202
David S. Miller30f2a5f2012-07-11 23:26:46 -0700203struct ndisc_options *ndisc_parse_options(u8 *opt, int opt_len,
204 struct ndisc_options *ndopts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)opt;
207
208 if (!nd_opt || opt_len < 0 || !ndopts)
209 return NULL;
210 memset(ndopts, 0, sizeof(*ndopts));
211 while (opt_len) {
212 int l;
213 if (opt_len < sizeof(struct nd_opt_hdr))
214 return NULL;
215 l = nd_opt->nd_opt_len << 3;
216 if (opt_len < l || l == 0)
217 return NULL;
218 switch (nd_opt->nd_opt_type) {
219 case ND_OPT_SOURCE_LL_ADDR:
220 case ND_OPT_TARGET_LL_ADDR:
221 case ND_OPT_MTU:
222 case ND_OPT_REDIRECT_HDR:
223 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
Joe Perches675418d2012-05-16 19:28:38 +0000224 ND_PRINTK(2, warn,
225 "%s: duplicated ND6 option found: type=%d\n",
226 __func__, nd_opt->nd_opt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 } else {
228 ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
229 }
230 break;
231 case ND_OPT_PREFIX_INFO:
232 ndopts->nd_opts_pi_end = nd_opt;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700233 if (!ndopts->nd_opt_array[nd_opt->nd_opt_type])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
235 break;
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -0800236#ifdef CONFIG_IPV6_ROUTE_INFO
237 case ND_OPT_ROUTE_INFO:
238 ndopts->nd_opts_ri_end = nd_opt;
239 if (!ndopts->nd_opts_ri)
240 ndopts->nd_opts_ri = nd_opt;
241 break;
242#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 default:
Pierre Ynard31910572007-10-10 21:22:05 -0700244 if (ndisc_is_useropt(nd_opt)) {
245 ndopts->nd_useropts_end = nd_opt;
246 if (!ndopts->nd_useropts)
247 ndopts->nd_useropts = nd_opt;
248 } else {
249 /*
250 * Unknown options must be silently ignored,
251 * to accommodate future extension to the
252 * protocol.
253 */
Joe Perches675418d2012-05-16 19:28:38 +0000254 ND_PRINTK(2, notice,
255 "%s: ignored unsupported option; type=%d, len=%d\n",
256 __func__,
257 nd_opt->nd_opt_type,
258 nd_opt->nd_opt_len);
Pierre Ynard31910572007-10-10 21:22:05 -0700259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 opt_len -= l;
262 nd_opt = ((void *)nd_opt) + l;
263 }
264 return ndopts;
265}
266
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000267int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 switch (dev->type) {
270 case ARPHRD_ETHER:
271 case ARPHRD_IEEE802: /* Not sure. Check it later. --ANK */
272 case ARPHRD_FDDI:
273 ipv6_eth_mc_map(addr, buf);
274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 case ARPHRD_ARCNET:
276 ipv6_arcnet_mc_map(addr, buf);
277 return 0;
278 case ARPHRD_INFINIBAND:
Rolf Manderscheida9e527e2007-12-10 13:38:41 -0700279 ipv6_ib_mc_map(addr, dev->broadcast, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return 0;
Timo Teräs93ca3bb2011-03-28 22:40:53 +0000281 case ARPHRD_IPGRE:
282 return ipv6_ipgre_mc_map(addr, dev->broadcast, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 default:
284 if (dir) {
285 memcpy(buf, dev->broadcast, dev->addr_len);
286 return 0;
287 }
288 }
289 return -EINVAL;
290}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900291EXPORT_SYMBOL(ndisc_mc_map);
292
Eric Dumazetd6bf7812010-10-04 06:15:44 +0000293static u32 ndisc_hash(const void *pkey,
294 const struct net_device *dev,
David S. Miller2c2aba62011-12-28 15:06:58 -0500295 __u32 *hash_rnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
David S. Miller2c2aba62011-12-28 15:06:58 -0500297 return ndisc_hashfn(pkey, dev, hash_rnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Eric W. Biederman60395a22015-03-03 17:10:44 -0600300static bool ndisc_key_eq(const struct neighbour *n, const void *pkey)
301{
302 return neigh_key_eq128(n, pkey);
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static int ndisc_constructor(struct neighbour *neigh)
306{
Ian Morris67ba4152014-08-24 21:53:10 +0100307 struct in6_addr *addr = (struct in6_addr *)&neigh->primary_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct net_device *dev = neigh->dev;
309 struct inet6_dev *in6_dev;
310 struct neigh_parms *parms;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000311 bool is_multicast = ipv6_addr_is_multicast(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 in6_dev = in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100314 if (!in6_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return -EINVAL;
316 }
317
318 parms = in6_dev->nd_parms;
319 __neigh_parms_put(neigh->parms);
320 neigh->parms = neigh_parms_clone(parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 neigh->type = is_multicast ? RTN_MULTICAST : RTN_UNICAST;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700323 if (!dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 neigh->nud_state = NUD_NOARP;
325 neigh->ops = &ndisc_direct_ops;
David S. Miller8f40b162011-07-17 13:34:11 -0700326 neigh->output = neigh_direct_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 } else {
328 if (is_multicast) {
329 neigh->nud_state = NUD_NOARP;
330 ndisc_mc_map(addr, neigh->ha, dev, 1);
331 } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) {
332 neigh->nud_state = NUD_NOARP;
333 memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
334 if (dev->flags&IFF_LOOPBACK)
335 neigh->type = RTN_LOCAL;
336 } else if (dev->flags&IFF_POINTOPOINT) {
337 neigh->nud_state = NUD_NOARP;
338 memcpy(neigh->ha, dev->broadcast, dev->addr_len);
339 }
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700340 if (dev->header_ops->cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 neigh->ops = &ndisc_hh_ops;
342 else
343 neigh->ops = &ndisc_generic_ops;
344 if (neigh->nud_state&NUD_VALID)
345 neigh->output = neigh->ops->connected_output;
346 else
347 neigh->output = neigh->ops->output;
348 }
349 in6_dev_put(in6_dev);
350 return 0;
351}
352
353static int pndisc_constructor(struct pneigh_entry *n)
354{
Ian Morris67ba4152014-08-24 21:53:10 +0100355 struct in6_addr *addr = (struct in6_addr *)&n->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct in6_addr maddr;
357 struct net_device *dev = n->dev;
358
Ian Morris63159f22015-03-29 14:00:04 +0100359 if (!dev || !__in6_dev_get(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return -EINVAL;
361 addrconf_addr_solict_mult(addr, &maddr);
362 ipv6_dev_mc_inc(dev, &maddr);
363 return 0;
364}
365
366static void pndisc_destructor(struct pneigh_entry *n)
367{
Ian Morris67ba4152014-08-24 21:53:10 +0100368 struct in6_addr *addr = (struct in6_addr *)&n->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct in6_addr maddr;
370 struct net_device *dev = n->dev;
371
Ian Morris63159f22015-03-29 14:00:04 +0100372 if (!dev || !__in6_dev_get(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return;
374 addrconf_addr_solict_mult(addr, &maddr);
375 ipv6_dev_mc_dec(dev, &maddr);
376}
377
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +0000378static struct sk_buff *ndisc_alloc_skb(struct net_device *dev,
379 int len)
380{
381 int hlen = LL_RESERVED_SPACE(dev);
382 int tlen = dev->needed_tailroom;
383 struct sock *sk = dev_net(dev)->ipv6.ndisc_sk;
384 struct sk_buff *skb;
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +0000385
Thomas Graf25a6e6b2013-09-03 13:37:01 +0200386 skb = alloc_skb(hlen + sizeof(struct ipv6hdr) + len + tlen, GFP_ATOMIC);
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +0000387 if (!skb) {
Thomas Graf25a6e6b2013-09-03 13:37:01 +0200388 ND_PRINTK(0, err, "ndisc: %s failed to allocate an skb\n",
389 __func__);
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +0000390 return NULL;
391 }
392
YOSHIFUJI Hideaki / 吉藤英明f382d032013-01-21 06:48:29 +0000393 skb->protocol = htons(ETH_P_IPV6);
394 skb->dev = dev;
395
YOSHIFUJI Hideaki / 吉藤英明527a1502013-01-21 06:48:39 +0000396 skb_reserve(skb, hlen + sizeof(struct ipv6hdr));
YOSHIFUJI Hideaki / 吉藤英明5135e632013-01-21 06:48:44 +0000397 skb_reset_transport_header(skb);
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +0000398
Thomas Graf25a6e6b2013-09-03 13:37:01 +0200399 /* Manually assign socket ownership as we avoid calling
400 * sock_alloc_send_pskb() to bypass wmem buffer limits
401 */
402 skb_set_owner_w(skb, sk);
403
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +0000404 return skb;
405}
406
YOSHIFUJI Hideaki / 吉藤英明f382d032013-01-21 06:48:29 +0000407static void ip6_nd_hdr(struct sk_buff *skb,
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +0000408 const struct in6_addr *saddr,
409 const struct in6_addr *daddr,
YOSHIFUJI Hideaki / 吉藤英明c8d6c382013-01-21 06:48:24 +0000410 int hop_limit, int len)
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +0000411{
412 struct ipv6hdr *hdr;
413
YOSHIFUJI Hideaki / 吉藤英明527a1502013-01-21 06:48:39 +0000414 skb_push(skb, sizeof(*hdr));
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +0000415 skb_reset_network_header(skb);
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +0000416 hdr = ipv6_hdr(skb);
417
418 ip6_flow_hdr(hdr, 0, 0);
419
420 hdr->payload_len = htons(len);
YOSHIFUJI Hideaki / 吉藤英明c8d6c382013-01-21 06:48:24 +0000421 hdr->nexthdr = IPPROTO_ICMPV6;
422 hdr->hop_limit = hop_limit;
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +0000423
424 hdr->saddr = *saddr;
425 hdr->daddr = *daddr;
426}
427
YOSHIFUJI Hideaki / 吉藤英明af9a9972013-01-21 06:48:34 +0000428static void ndisc_send_skb(struct sk_buff *skb,
YOSHIFUJI Hideakifd0ea7d2012-12-13 02:40:26 +0900429 const struct in6_addr *daddr,
YOSHIFUJI Hideaki / 吉藤英明aa4bdd42013-01-21 06:48:58 +0000430 const struct in6_addr *saddr)
Brian Haley305d5522008-11-04 17:51:14 -0800431{
YOSHIFUJI Hideaki / 吉藤英明f4de84c2013-01-21 06:49:03 +0000432 struct dst_entry *dst = skb_dst(skb);
YOSHIFUJI Hideaki / 吉藤英明af9a9972013-01-21 06:48:34 +0000433 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideaki / 吉藤英明7b3d9b02013-01-21 06:49:08 +0000434 struct sock *sk = net->ipv6.ndisc_sk;
Brian Haley305d5522008-11-04 17:51:14 -0800435 struct inet6_dev *idev;
436 int err;
YOSHIFUJI Hideaki / 吉藤英明aa4bdd42013-01-21 06:48:58 +0000437 struct icmp6hdr *icmp6h = icmp6_hdr(skb);
Brian Haley305d5522008-11-04 17:51:14 -0800438 u8 type;
439
440 type = icmp6h->icmp6_type;
441
YOSHIFUJI Hideaki / 吉藤英明f4de84c2013-01-21 06:49:03 +0000442 if (!dst) {
YOSHIFUJI Hideaki / 吉藤英明f4de84c2013-01-21 06:49:03 +0000443 struct flowi6 fl6;
Brian Haley305d5522008-11-04 17:51:14 -0800444
YOSHIFUJI Hideaki / 吉藤英明f4de84c2013-01-21 06:49:03 +0000445 icmpv6_flow_init(sk, &fl6, type, saddr, daddr, skb->dev->ifindex);
446 dst = icmp6_dst_alloc(skb->dev, &fl6);
447 if (IS_ERR(dst)) {
448 kfree_skb(skb);
449 return;
450 }
451
452 skb_dst_set(skb, dst);
453 }
YOSHIFUJI Hideakie1ec7842007-04-24 20:44:52 +0900454
YOSHIFUJI Hideaki / 吉藤英明7b3d9b02013-01-21 06:49:08 +0000455 icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, skb->len,
456 IPPROTO_ICMPV6,
457 csum_partial(icmp6h,
458 skb->len, 0));
459
460 ip6_nd_hdr(skb, saddr, daddr, inet6_sk(sk)->hop_limit, skb->len);
461
Eric Dumazetcfdf7642011-07-27 21:13:03 +0000462 rcu_read_lock();
463 idev = __in6_dev_get(dst->dev);
Neil Hormanedf391f2009-04-27 02:45:02 -0700464 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
YOSHIFUJI Hideakie1ec7842007-04-24 20:44:52 +0900465
Jan Engelhardtb2e0b382010-03-23 04:09:07 +0100466 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, dst->dev,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800467 dst_output);
YOSHIFUJI Hideakie1ec7842007-04-24 20:44:52 +0900468 if (!err) {
Denis V. Lunev5c5d2442008-10-08 10:33:50 -0700469 ICMP6MSGOUT_INC_STATS(net, idev, type);
Denis V. Luneva862f6a2008-10-08 10:33:06 -0700470 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
YOSHIFUJI Hideakie1ec7842007-04-24 20:44:52 +0900471 }
472
Eric Dumazetcfdf7642011-07-27 21:13:03 +0000473 rcu_read_unlock();
YOSHIFUJI Hideakie1ec7842007-04-24 20:44:52 +0900474}
475
Cong Wangf564f452013-08-31 13:44:36 +0800476void ndisc_send_na(struct net_device *dev, struct neighbour *neigh,
477 const struct in6_addr *daddr,
478 const struct in6_addr *solicited_addr,
479 bool router, bool solicited, bool override, bool inc_opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000481 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct in6_addr tmpaddr;
483 struct inet6_ifaddr *ifp;
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900484 const struct in6_addr *src_addr;
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000485 struct nd_msg *msg;
486 int optlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* for anycast or proxy, solicited_addr != src_addr */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900489 ifp = ipv6_get_ifaddr(dev_net(dev), solicited_addr, dev, 1);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900490 if (ifp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 src_addr = solicited_addr;
Neil Horman95c385b2007-04-25 17:08:10 -0700492 if (ifp->flags & IFA_F_OPTIMISTIC)
Daniel Balutaf2f79cc2013-07-13 11:26:51 +0300493 override = false;
stephen hemminger9f888162010-06-21 11:00:13 +0000494 inc_opt |= ifp->idev->cnf.force_tllao;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 in6_ifa_put(ifp);
496 } else {
Brian Haley191cd582008-08-14 15:33:21 -0700497 if (ipv6_dev_get_saddr(dev_net(dev), dev, daddr,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900498 inet6_sk(dev_net(dev)->ipv6.ndisc_sk)->srcprefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900499 &tmpaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return;
501 src_addr = &tmpaddr;
502 }
503
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000504 if (!dev->addr_len)
505 inc_opt = 0;
506 if (inc_opt)
507 optlen += ndisc_opt_addr_space(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000509 skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000510 if (!skb)
511 return;
512
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000513 msg = (struct nd_msg *)skb_put(skb, sizeof(*msg));
514 *msg = (struct nd_msg) {
515 .icmph = {
516 .icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT,
517 .icmp6_router = router,
518 .icmp6_solicited = solicited,
519 .icmp6_override = override,
520 },
521 .target = *solicited_addr,
522 };
523
524 if (inc_opt)
525 ndisc_fill_addr_option(skb, ND_OPT_TARGET_LL_ADDR,
526 dev->dev_addr);
527
528
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000529 ndisc_send_skb(skb, daddr, src_addr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900530}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Ben Hutchingsf47b9462011-04-15 13:46:02 +0000532static void ndisc_send_unsol_na(struct net_device *dev)
533{
534 struct inet6_dev *idev;
535 struct inet6_ifaddr *ifa;
Ben Hutchingsf47b9462011-04-15 13:46:02 +0000536
537 idev = in6_dev_get(dev);
538 if (!idev)
539 return;
540
541 read_lock_bh(&idev->lock);
542 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki / 吉藤英明9fafd652012-11-12 07:50:17 +0000543 ndisc_send_na(dev, NULL, &in6addr_linklocal_allnodes, &ifa->addr,
Ben Hutchingsf47b9462011-04-15 13:46:02 +0000544 /*router=*/ !!idev->cnf.forwarding,
545 /*solicited=*/ false, /*override=*/ true,
546 /*inc_opt=*/ true);
547 }
548 read_unlock_bh(&idev->lock);
549
550 in6_dev_put(idev);
551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553void ndisc_send_ns(struct net_device *dev, struct neighbour *neigh,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900554 const struct in6_addr *solicit,
555 const struct in6_addr *daddr, const struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000557 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct in6_addr addr_buf;
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000559 int inc_opt = dev->addr_len;
560 int optlen = 0;
561 struct nd_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Ian Morris63159f22015-03-29 14:00:04 +0100563 if (!saddr) {
Neil Horman95c385b2007-04-25 17:08:10 -0700564 if (ipv6_get_lladdr(dev, &addr_buf,
565 (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return;
567 saddr = &addr_buf;
568 }
569
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000570 if (ipv6_addr_any(saddr))
Daniel Balutaf2f79cc2013-07-13 11:26:51 +0300571 inc_opt = false;
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000572 if (inc_opt)
573 optlen += ndisc_opt_addr_space(dev);
574
575 skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000576 if (!skb)
577 return;
578
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000579 msg = (struct nd_msg *)skb_put(skb, sizeof(*msg));
580 *msg = (struct nd_msg) {
581 .icmph = {
582 .icmp6_type = NDISC_NEIGHBOUR_SOLICITATION,
583 },
584 .target = *solicit,
585 };
586
587 if (inc_opt)
588 ndisc_fill_addr_option(skb, ND_OPT_SOURCE_LL_ADDR,
589 dev->dev_addr);
590
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000591 ndisc_send_skb(skb, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900594void ndisc_send_rs(struct net_device *dev, const struct in6_addr *saddr,
595 const struct in6_addr *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000597 struct sk_buff *skb;
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000598 struct rs_msg *msg;
Neil Horman95c385b2007-04-25 17:08:10 -0700599 int send_sllao = dev->addr_len;
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000600 int optlen = 0;
Neil Horman95c385b2007-04-25 17:08:10 -0700601
602#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
603 /*
604 * According to section 2.2 of RFC 4429, we must not
605 * send router solicitations with a sllao from
606 * optimistic addresses, but we may send the solicitation
607 * if we don't include the sllao. So here we check
608 * if our address is optimistic, and if so, we
Joe Perchesbea85192007-12-20 14:01:35 -0800609 * suppress the inclusion of the sllao.
Neil Horman95c385b2007-04-25 17:08:10 -0700610 */
611 if (send_sllao) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900612 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(dev_net(dev), saddr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -0800613 dev, 1);
Neil Horman95c385b2007-04-25 17:08:10 -0700614 if (ifp) {
615 if (ifp->flags & IFA_F_OPTIMISTIC) {
YOSHIFUJI Hideakica043562007-02-28 23:13:20 +0900616 send_sllao = 0;
Neil Horman95c385b2007-04-25 17:08:10 -0700617 }
YOSHIFUJI Hideakica043562007-02-28 23:13:20 +0900618 in6_ifa_put(ifp);
Neil Horman95c385b2007-04-25 17:08:10 -0700619 } else {
620 send_sllao = 0;
621 }
622 }
623#endif
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000624 if (send_sllao)
625 optlen += ndisc_opt_addr_space(dev);
626
627 skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000628 if (!skb)
629 return;
630
YOSHIFUJI Hideaki / 吉藤英明1cb3fe52013-01-21 06:49:17 +0000631 msg = (struct rs_msg *)skb_put(skb, sizeof(*msg));
632 *msg = (struct rs_msg) {
633 .icmph = {
634 .icmp6_type = NDISC_ROUTER_SOLICITATION,
635 },
636 };
637
638 if (send_sllao)
639 ndisc_fill_addr_option(skb, ND_OPT_SOURCE_LL_ADDR,
640 dev->dev_addr);
641
YOSHIFUJI Hideaki / 吉藤英明b44b5f42013-01-21 06:49:13 +0000642 ndisc_send_skb(skb, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb)
647{
648 /*
649 * "The sender MUST return an ICMP
650 * destination unreachable"
651 */
652 dst_link_failure(skb);
653 kfree_skb(skb);
654}
655
656/* Called with locked neigh: either read or both */
657
658static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb)
659{
660 struct in6_addr *saddr = NULL;
661 struct in6_addr mcaddr;
662 struct net_device *dev = neigh->dev;
663 struct in6_addr *target = (struct in6_addr *)&neigh->primary_key;
664 int probes = atomic_read(&neigh->probes);
665
Erik Klinec58da4c2015-02-04 20:01:23 +0900666 if (skb && ipv6_chk_addr_and_flags(dev_net(dev), &ipv6_hdr(skb)->saddr,
667 dev, 1,
668 IFA_F_TENTATIVE|IFA_F_OPTIMISTIC))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700669 saddr = &ipv6_hdr(skb)->saddr;
Ian Morrise5d08d72014-11-23 21:28:43 +0000670 probes -= NEIGH_VAR(neigh->parms, UCAST_PROBES);
671 if (probes < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (!(neigh->nud_state & NUD_VALID)) {
Joe Perches675418d2012-05-16 19:28:38 +0000673 ND_PRINTK(1, dbg,
674 "%s: trying to ucast probe in NUD_INVALID: %pI6\n",
675 __func__, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 ndisc_send_ns(dev, neigh, target, target, saddr);
Jiri Pirko1f9248e52013-12-07 19:26:53 +0100678 } else if ((probes -= NEIGH_VAR(neigh->parms, APP_PROBES)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 neigh_app_ns(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 } else {
681 addrconf_addr_solict_mult(target, &mcaddr);
682 ndisc_send_ns(dev, NULL, target, &mcaddr, saddr);
683 }
684}
685
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900686static int pndisc_is_router(const void *pkey,
687 struct net_device *dev)
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700688{
689 struct pneigh_entry *n;
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900690 int ret = -1;
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700691
692 read_lock_bh(&nd_tbl.lock);
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900693 n = __pneigh_lookup(&nd_tbl, dev_net(dev), pkey, dev);
694 if (n)
695 ret = !!(n->flags & NTF_ROUTER);
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700696 read_unlock_bh(&nd_tbl.lock);
697
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900698 return ret;
Pavel Emelyanovfa86d322008-03-24 14:48:59 -0700699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static void ndisc_recv_ns(struct sk_buff *skb)
702{
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700703 struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000704 const struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
705 const struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 u8 *lladdr = NULL;
Simon Horman29a3cad2013-05-28 20:34:26 +0000707 u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700708 offsetof(struct nd_msg, opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 struct ndisc_options ndopts;
710 struct net_device *dev = skb->dev;
711 struct inet6_ifaddr *ifp;
712 struct inet6_dev *idev = NULL;
713 struct neighbour *neigh;
714 int dad = ipv6_addr_any(saddr);
Eric Dumazeta50feda2012-05-18 18:57:34 +0000715 bool inc;
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900716 int is_router = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
YOSHIFUJI Hideaki / 吉藤英明115b0aa2013-01-18 02:05:03 +0000718 if (skb->len < sizeof(struct nd_msg)) {
719 ND_PRINTK(2, warn, "NS: packet too short\n");
720 return;
721 }
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (ipv6_addr_is_multicast(&msg->target)) {
Joe Perches675418d2012-05-16 19:28:38 +0000724 ND_PRINTK(2, warn, "NS: multicast target address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return;
726 }
727
728 /*
729 * RFC2461 7.1.1:
730 * DAD has to be destined for solicited node multicast address.
731 */
YOSHIFUJI Hideaki / 吉藤英明ca97a642013-01-20 07:39:00 +0000732 if (dad && !ipv6_addr_is_solict_mult(daddr)) {
Joe Perches675418d2012-05-16 19:28:38 +0000733 ND_PRINTK(2, warn, "NS: bad DAD packet (wrong destination)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return;
735 }
736
737 if (!ndisc_parse_options(msg->opt, ndoptlen, &ndopts)) {
Joe Perches675418d2012-05-16 19:28:38 +0000738 ND_PRINTK(2, warn, "NS: invalid ND options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return;
740 }
741
742 if (ndopts.nd_opts_src_lladdr) {
743 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr, dev);
744 if (!lladdr) {
Joe Perches675418d2012-05-16 19:28:38 +0000745 ND_PRINTK(2, warn,
746 "NS: invalid link-layer address length\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return;
748 }
749
750 /* RFC2461 7.1.1:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900751 * If the IP source address is the unspecified address,
752 * there MUST NOT be source link-layer address option
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 * in the message.
754 */
755 if (dad) {
Joe Perches675418d2012-05-16 19:28:38 +0000756 ND_PRINTK(2, warn,
757 "NS: bad DAD packet (link-layer address option)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return;
759 }
760 }
761
762 inc = ipv6_addr_is_multicast(daddr);
763
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900764 ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
Daniel Lezcanoa18bc692008-03-07 11:14:49 -0800765 if (ifp) {
Neil Horman95c385b2007-04-25 17:08:10 -0700766
767 if (ifp->flags & (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)) {
768 if (dad) {
Neil Horman95c385b2007-04-25 17:08:10 -0700769 /*
770 * We are colliding with another node
771 * who is doing DAD
772 * so fail our DAD process
773 */
774 addrconf_dad_failure(ifp);
Denis V. Lunev9e3be4b2007-09-11 11:04:49 +0200775 return;
Neil Horman95c385b2007-04-25 17:08:10 -0700776 } else {
777 /*
778 * This is not a dad solicitation.
779 * If we are an optimistic node,
780 * we should respond.
781 * Otherwise, we should ignore it.
782 */
783 if (!(ifp->flags & IFA_F_OPTIMISTIC))
784 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
788 idev = ifp->idev;
789 } else {
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700790 struct net *net = dev_net(dev);
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 idev = in6_dev_get(dev);
793 if (!idev) {
794 /* XXX: count this drop? */
795 return;
796 }
797
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700798 if (ipv6_chk_acast_addr(net, dev, &msg->target) ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900799 (idev->cnf.forwarding &&
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700800 (net->ipv6.devconf_all->proxy_ndp || idev->cnf.proxy_ndp) &&
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900801 (is_router = pndisc_is_router(&msg->target, dev)) >= 0)) {
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700802 if (!(NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 skb->pkt_type != PACKET_HOST &&
Daniel Balutaf2f79cc2013-07-13 11:26:51 +0300804 inc &&
Jiri Pirko1f9248e52013-12-07 19:26:53 +0100805 NEIGH_VAR(idev->nd_parms, PROXY_DELAY) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /*
807 * for anycast or proxy,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900808 * sender should delay its response
809 * by a random time between 0 and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 * MAX_ANYCAST_DELAY_TIME seconds.
811 * (RFC2461) -- yoshfuji
812 */
813 struct sk_buff *n = skb_clone(skb, GFP_ATOMIC);
814 if (n)
815 pneigh_enqueue(&nd_tbl, idev->nd_parms, n);
816 goto out;
817 }
818 } else
819 goto out;
820 }
821
YOSHIFUJI Hideaki0736ffc2008-03-28 13:37:58 +0900822 if (is_router < 0)
YOSHIFUJI Hideaki / 吉藤英明fb568632013-01-20 07:39:18 +0000823 is_router = idev->cnf.forwarding;
Ville Nuorvala62dd9312006-09-22 14:43:19 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (dad) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900826 ndisc_send_na(dev, NULL, &in6addr_linklocal_allnodes, &msg->target,
YOSHIFUJI Hideaki / 吉藤英明fb568632013-01-20 07:39:18 +0000827 !!is_router, false, (ifp != NULL), true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 goto out;
829 }
830
831 if (inc)
832 NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_mcast);
833 else
834 NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_ucast);
835
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900836 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 * update / create cache entry
838 * for the source address
839 */
840 neigh = __neigh_lookup(&nd_tbl, saddr, dev,
841 !inc || lladdr || !dev->addr_len);
842 if (neigh)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900843 neigh_update(neigh, lladdr, NUD_STALE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 NEIGH_UPDATE_F_WEAK_OVERRIDE|
845 NEIGH_UPDATE_F_OVERRIDE);
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700846 if (neigh || !dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 ndisc_send_na(dev, neigh, saddr, &msg->target,
YOSHIFUJI Hideaki / 吉藤英明fb568632013-01-20 07:39:18 +0000848 !!is_router,
849 true, (ifp != NULL && inc), inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (neigh)
851 neigh_release(neigh);
852 }
853
854out:
855 if (ifp)
856 in6_ifa_put(ifp);
857 else
858 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861static void ndisc_recv_na(struct sk_buff *skb)
862{
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700863 struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
Duan Jiongbe7a0102014-05-15 15:56:14 +0800864 struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000865 const struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 u8 *lladdr = NULL;
Simon Horman29a3cad2013-05-28 20:34:26 +0000867 u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700868 offsetof(struct nd_msg, opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 struct ndisc_options ndopts;
870 struct net_device *dev = skb->dev;
871 struct inet6_ifaddr *ifp;
872 struct neighbour *neigh;
873
874 if (skb->len < sizeof(struct nd_msg)) {
Joe Perches675418d2012-05-16 19:28:38 +0000875 ND_PRINTK(2, warn, "NA: packet too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return;
877 }
878
879 if (ipv6_addr_is_multicast(&msg->target)) {
Joe Perches675418d2012-05-16 19:28:38 +0000880 ND_PRINTK(2, warn, "NA: target address is multicast\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return;
882 }
883
884 if (ipv6_addr_is_multicast(daddr) &&
885 msg->icmph.icmp6_solicited) {
Joe Perches675418d2012-05-16 19:28:38 +0000886 ND_PRINTK(2, warn, "NA: solicited NA is multicasted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return;
888 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (!ndisc_parse_options(msg->opt, ndoptlen, &ndopts)) {
Joe Perches675418d2012-05-16 19:28:38 +0000891 ND_PRINTK(2, warn, "NS: invalid ND option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return;
893 }
894 if (ndopts.nd_opts_tgt_lladdr) {
895 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, dev);
896 if (!lladdr) {
Joe Perches675418d2012-05-16 19:28:38 +0000897 ND_PRINTK(2, warn,
898 "NA: invalid link-layer address length\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return;
900 }
901 }
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900902 ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
Daniel Lezcanoa18bc692008-03-07 11:14:49 -0800903 if (ifp) {
Daniel Walterbd015922011-04-13 21:09:25 +0000904 if (skb->pkt_type != PACKET_LOOPBACK
905 && (ifp->flags & IFA_F_TENTATIVE)) {
906 addrconf_dad_failure(ifp);
907 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909 /* What should we make now? The advertisement
910 is invalid, but ndisc specs say nothing
911 about it. It could be misconfiguration, or
912 an smart proxy agent tries to help us :-)
Jan Sembera24fc7b82008-12-09 15:48:32 -0800913
914 We should not print the error if NA has been
915 received from loopback - it is just our own
916 unsolicited advertisement.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 */
Jan Sembera24fc7b82008-12-09 15:48:32 -0800918 if (skb->pkt_type != PACKET_LOOPBACK)
Joe Perches675418d2012-05-16 19:28:38 +0000919 ND_PRINTK(1, warn,
920 "NA: someone advertises our address %pI6 on %s!\n",
921 &ifp->addr, ifp->idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 in6_ifa_put(ifp);
923 return;
924 }
925 neigh = neigh_lookup(&nd_tbl, &msg->target, dev);
926
927 if (neigh) {
928 u8 old_flags = neigh->flags;
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700929 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 if (neigh->nud_state & NUD_FAILED)
932 goto out;
933
Ville Nuorvala5f3e6e92006-09-22 14:42:46 -0700934 /*
935 * Don't update the neighbor cache entry on a proxy NA from
936 * ourselves because either the proxied node is off link or it
937 * has already sent a NA to us.
938 */
939 if (lladdr && !memcmp(lladdr, dev->dev_addr, dev->addr_len) &&
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700940 net->ipv6.devconf_all->forwarding && net->ipv6.devconf_all->proxy_ndp &&
941 pneigh_lookup(&nd_tbl, net, &msg->target, dev, 0)) {
Nicolas Dichtelb20b6d92012-11-07 05:05:38 +0000942 /* XXX: idev->cnf.proxy_ndp */
Ville Nuorvala5f3e6e92006-09-22 14:42:46 -0700943 goto out;
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700944 }
Ville Nuorvala5f3e6e92006-09-22 14:42:46 -0700945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 neigh_update(neigh, lladdr,
947 msg->icmph.icmp6_solicited ? NUD_REACHABLE : NUD_STALE,
948 NEIGH_UPDATE_F_WEAK_OVERRIDE|
949 (msg->icmph.icmp6_override ? NEIGH_UPDATE_F_OVERRIDE : 0)|
950 NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
951 (msg->icmph.icmp6_router ? NEIGH_UPDATE_F_ISROUTER : 0));
952
953 if ((old_flags & ~neigh->flags) & NTF_ROUTER) {
954 /*
955 * Change: router to host
956 */
Duan Jiongbe7a0102014-05-15 15:56:14 +0800957 rt6_clean_tohost(dev_net(dev), saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959
960out:
961 neigh_release(neigh);
962 }
963}
964
965static void ndisc_recv_rs(struct sk_buff *skb)
966{
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700967 struct rs_msg *rs_msg = (struct rs_msg *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 unsigned long ndoptlen = skb->len - sizeof(*rs_msg);
969 struct neighbour *neigh;
970 struct inet6_dev *idev;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000971 const struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct ndisc_options ndopts;
973 u8 *lladdr = NULL;
974
975 if (skb->len < sizeof(*rs_msg))
976 return;
977
Eric Dumazetcfdf7642011-07-27 21:13:03 +0000978 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (!idev) {
Joe Perches675418d2012-05-16 19:28:38 +0000980 ND_PRINTK(1, err, "RS: can't find in6 device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return;
982 }
983
984 /* Don't accept RS if we're not in router mode */
985 if (!idev->cnf.forwarding)
986 goto out;
987
988 /*
989 * Don't update NCE if src = ::;
990 * this implies that the source node has no ip address assigned yet.
991 */
992 if (ipv6_addr_any(saddr))
993 goto out;
994
995 /* Parse ND options */
996 if (!ndisc_parse_options(rs_msg->opt, ndoptlen, &ndopts)) {
Joe Perches675418d2012-05-16 19:28:38 +0000997 ND_PRINTK(2, notice, "NS: invalid ND option, ignored\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 goto out;
999 }
1000
1001 if (ndopts.nd_opts_src_lladdr) {
1002 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
1003 skb->dev);
1004 if (!lladdr)
1005 goto out;
1006 }
1007
1008 neigh = __neigh_lookup(&nd_tbl, saddr, skb->dev, 1);
1009 if (neigh) {
1010 neigh_update(neigh, lladdr, NUD_STALE,
1011 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1012 NEIGH_UPDATE_F_OVERRIDE|
1013 NEIGH_UPDATE_F_OVERRIDE_ISROUTER);
1014 neigh_release(neigh);
1015 }
1016out:
Eric Dumazetcfdf7642011-07-27 21:13:03 +00001017 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Pierre Ynard31910572007-10-10 21:22:05 -07001020static void ndisc_ra_useropt(struct sk_buff *ra, struct nd_opt_hdr *opt)
1021{
1022 struct icmp6hdr *icmp6h = (struct icmp6hdr *)skb_transport_header(ra);
1023 struct sk_buff *skb;
1024 struct nlmsghdr *nlh;
1025 struct nduseroptmsg *ndmsg;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001026 struct net *net = dev_net(ra->dev);
Pierre Ynard31910572007-10-10 21:22:05 -07001027 int err;
1028 int base_size = NLMSG_ALIGN(sizeof(struct nduseroptmsg)
1029 + (opt->nd_opt_len << 3));
1030 size_t msg_size = base_size + nla_total_size(sizeof(struct in6_addr));
1031
1032 skb = nlmsg_new(msg_size, GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01001033 if (!skb) {
Pierre Ynard31910572007-10-10 21:22:05 -07001034 err = -ENOBUFS;
1035 goto errout;
1036 }
1037
1038 nlh = nlmsg_put(skb, 0, 0, RTM_NEWNDUSEROPT, base_size, 0);
Ian Morris63159f22015-03-29 14:00:04 +01001039 if (!nlh) {
Pierre Ynard31910572007-10-10 21:22:05 -07001040 goto nla_put_failure;
1041 }
1042
1043 ndmsg = nlmsg_data(nlh);
1044 ndmsg->nduseropt_family = AF_INET6;
Pierre Ynarddbb2ed22007-11-12 17:58:35 -08001045 ndmsg->nduseropt_ifindex = ra->dev->ifindex;
Pierre Ynard31910572007-10-10 21:22:05 -07001046 ndmsg->nduseropt_icmp_type = icmp6h->icmp6_type;
1047 ndmsg->nduseropt_icmp_code = icmp6h->icmp6_code;
1048 ndmsg->nduseropt_opts_len = opt->nd_opt_len << 3;
1049
1050 memcpy(ndmsg + 1, opt, opt->nd_opt_len << 3);
1051
Jiri Benc930345e2015-03-29 16:59:25 +02001052 if (nla_put_in6_addr(skb, NDUSEROPT_SRCADDR, &ipv6_hdr(ra)->saddr))
David S. Millerc78679e2012-04-01 20:27:33 -04001053 goto nla_put_failure;
Pierre Ynard31910572007-10-10 21:22:05 -07001054 nlmsg_end(skb, nlh);
1055
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001056 rtnl_notify(skb, net, 0, RTNLGRP_ND_USEROPT, NULL, GFP_ATOMIC);
Pierre Ynard31910572007-10-10 21:22:05 -07001057 return;
1058
1059nla_put_failure:
1060 nlmsg_free(skb);
1061 err = -EMSGSIZE;
1062errout:
Daniel Lezcanoa18bc692008-03-07 11:14:49 -08001063 rtnl_set_sk_err(net, RTNLGRP_ND_USEROPT, err);
Pierre Ynard31910572007-10-10 21:22:05 -07001064}
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066static void ndisc_router_discovery(struct sk_buff *skb)
1067{
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001068 struct ra_msg *ra_msg = (struct ra_msg *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 struct neighbour *neigh = NULL;
1070 struct inet6_dev *in6_dev;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08001071 struct rt6_info *rt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 int lifetime;
1073 struct ndisc_options ndopts;
1074 int optlen;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001075 unsigned int pref = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Ian Morris67ba4152014-08-24 21:53:10 +01001077 __u8 *opt = (__u8 *)(ra_msg + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Simon Horman29a3cad2013-05-28 20:34:26 +00001079 optlen = (skb_tail_pointer(skb) - skb_transport_header(skb)) -
1080 sizeof(struct ra_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Ben Greearf2a762d2014-06-25 14:44:52 -07001082 ND_PRINTK(2, info,
1083 "RA: %s, dev: %s\n",
1084 __func__, skb->dev->name);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001085 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
Joe Perches675418d2012-05-16 19:28:38 +00001086 ND_PRINTK(2, warn, "RA: source address is not link-local\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return;
1088 }
1089 if (optlen < 0) {
Joe Perches675418d2012-05-16 19:28:38 +00001090 ND_PRINTK(2, warn, "RA: packet too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return;
1092 }
1093
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001094#ifdef CONFIG_IPV6_NDISC_NODETYPE
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001095 if (skb->ndisc_nodetype == NDISC_NODETYPE_HOST) {
Joe Perches675418d2012-05-16 19:28:38 +00001096 ND_PRINTK(2, warn, "RA: from host or unauthorized router\n");
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001097 return;
1098 }
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001099#endif
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /*
1102 * set the RA_RECV flag in the interface
1103 */
1104
Eric Dumazetcfdf7642011-07-27 21:13:03 +00001105 in6_dev = __in6_dev_get(skb->dev);
Ian Morris63159f22015-03-29 14:00:04 +01001106 if (!in6_dev) {
Joe Perches675418d2012-05-16 19:28:38 +00001107 ND_PRINTK(0, err, "RA: can't find inet6 device for %s\n",
1108 skb->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return;
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (!ndisc_parse_options(opt, optlen, &ndopts)) {
Joe Perches675418d2012-05-16 19:28:38 +00001113 ND_PRINTK(2, warn, "RA: invalid ND options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return;
1115 }
1116
Ben Greearf2a762d2014-06-25 14:44:52 -07001117 if (!ipv6_accept_ra(in6_dev)) {
1118 ND_PRINTK(2, info,
1119 "RA: %s, did not accept ra for dev: %s\n",
1120 __func__, skb->dev->name);
David Ward31ce8c72009-08-29 00:04:09 -07001121 goto skip_linkparms;
Ben Greearf2a762d2014-06-25 14:44:52 -07001122 }
David Ward31ce8c72009-08-29 00:04:09 -07001123
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001124#ifdef CONFIG_IPV6_NDISC_NODETYPE
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001125 /* skip link-specific parameters from interior routers */
Ben Greearf2a762d2014-06-25 14:44:52 -07001126 if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) {
1127 ND_PRINTK(2, info,
1128 "RA: %s, nodetype is NODEFAULT, dev: %s\n",
1129 __func__, skb->dev->name);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001130 goto skip_linkparms;
Ben Greearf2a762d2014-06-25 14:44:52 -07001131 }
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001132#endif
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (in6_dev->if_flags & IF_RS_SENT) {
1135 /*
1136 * flag that an RA was received after an RS was sent
1137 * out on this interface.
1138 */
1139 in6_dev->if_flags |= IF_RA_RCVD;
1140 }
1141
1142 /*
1143 * Remember the managed/otherconf flags from most recently
1144 * received RA message (RFC 2462) -- yoshfuji
1145 */
1146 in6_dev->if_flags = (in6_dev->if_flags & ~(IF_RA_MANAGED |
1147 IF_RA_OTHERCONF)) |
1148 (ra_msg->icmph.icmp6_addrconf_managed ?
1149 IF_RA_MANAGED : 0) |
1150 (ra_msg->icmph.icmp6_addrconf_other ?
1151 IF_RA_OTHERCONF : 0);
1152
Ben Greearf2a762d2014-06-25 14:44:52 -07001153 if (!in6_dev->cnf.accept_ra_defrtr) {
1154 ND_PRINTK(2, info,
1155 "RA: %s, defrtr is false for dev: %s\n",
1156 __func__, skb->dev->name);
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08001157 goto skip_defrtr;
Ben Greearf2a762d2014-06-25 14:44:52 -07001158 }
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08001159
Ben Greeard9333192014-06-25 14:44:53 -07001160 /* Do not accept RA with source-addr found on local machine unless
1161 * accept_ra_from_local is set to true.
1162 */
Li RongQingb6428812014-07-10 18:02:46 +08001163 if (!in6_dev->cnf.accept_ra_from_local &&
1164 ipv6_chk_addr(dev_net(in6_dev->dev), &ipv6_hdr(skb)->saddr,
1165 NULL, 0)) {
Ben Greearf2a762d2014-06-25 14:44:52 -07001166 ND_PRINTK(2, info,
Ben Greeard9333192014-06-25 14:44:53 -07001167 "RA from local address detected on dev: %s: default router ignored\n",
1168 skb->dev->name);
Andreas Hofmeister9f562202011-10-24 19:13:15 -04001169 goto skip_defrtr;
Ben Greearf2a762d2014-06-25 14:44:52 -07001170 }
Andreas Hofmeister9f562202011-10-24 19:13:15 -04001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 lifetime = ntohs(ra_msg->icmph.icmp6_rt_lifetime);
1173
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001174#ifdef CONFIG_IPV6_ROUTER_PREF
1175 pref = ra_msg->icmph.icmp6_router_pref;
1176 /* 10b is handled as if it were 00b (medium) */
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08001177 if (pref == ICMPV6_ROUTER_PREF_INVALID ||
YOSHIFUJI Hideaki6d5b78c2007-06-22 16:07:04 -07001178 !in6_dev->cnf.accept_ra_rtr_pref)
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001179 pref = ICMPV6_ROUTER_PREF_MEDIUM;
1180#endif
1181
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001182 rt = rt6_get_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
David S. Millereb857182012-01-27 15:07:56 -08001184 if (rt) {
1185 neigh = dst_neigh_lookup(&rt->dst, &ipv6_hdr(skb)->saddr);
1186 if (!neigh) {
Joe Perches675418d2012-05-16 19:28:38 +00001187 ND_PRINTK(0, err,
1188 "RA: %s got default router without neighbour\n",
1189 __func__);
Amerigo Wang94e187c2012-10-29 00:13:19 +00001190 ip6_rt_put(rt);
David S. Millereb857182012-01-27 15:07:56 -08001191 return;
1192 }
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (rt && lifetime == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -07001195 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 rt = NULL;
1197 }
1198
Ben Greearf2a762d2014-06-25 14:44:52 -07001199 ND_PRINTK(3, info, "RA: rt: %p lifetime: %d, for dev: %s\n",
1200 rt, lifetime, skb->dev->name);
Ian Morris63159f22015-03-29 14:00:04 +01001201 if (!rt && lifetime) {
Ben Greearf2a762d2014-06-25 14:44:52 -07001202 ND_PRINTK(3, info, "RA: adding default router\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001204 rt = rt6_add_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev, pref);
Ian Morris63159f22015-03-29 14:00:04 +01001205 if (!rt) {
Joe Perches675418d2012-05-16 19:28:38 +00001206 ND_PRINTK(0, err,
1207 "RA: %s failed to add default route\n",
1208 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return;
1210 }
1211
David S. Millereb857182012-01-27 15:07:56 -08001212 neigh = dst_neigh_lookup(&rt->dst, &ipv6_hdr(skb)->saddr);
Ian Morris63159f22015-03-29 14:00:04 +01001213 if (!neigh) {
Joe Perches675418d2012-05-16 19:28:38 +00001214 ND_PRINTK(0, err,
1215 "RA: %s got default router without neighbour\n",
1216 __func__);
Amerigo Wang94e187c2012-10-29 00:13:19 +00001217 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return;
1219 }
1220 neigh->flags |= NTF_ROUTER;
YOSHIFUJI Hideakiebacaaa2006-03-20 17:04:53 -08001221 } else if (rt) {
Pedro Ribeiro22441cf2008-10-15 15:47:49 -07001222 rt->rt6i_flags = (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224
1225 if (rt)
Gao feng1716a962012-04-06 00:13:10 +00001226 rt6_set_expires(rt, jiffies + (HZ * lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (ra_msg->icmph.icmp6_hop_limit) {
D.S. Ljungmark6fd99092015-03-25 09:28:15 +01001228 /* Only set hop_limit on the interface if it is higher than
1229 * the current hop_limit.
1230 */
1231 if (in6_dev->cnf.hop_limit < ra_msg->icmph.icmp6_hop_limit) {
1232 in6_dev->cnf.hop_limit = ra_msg->icmph.icmp6_hop_limit;
1233 } else {
1234 ND_PRINTK(2, warn, "RA: Got route advertisement with lower hop_limit than current\n");
1235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (rt)
David S. Millerdefb3512010-12-08 21:16:57 -08001237 dst_metric_set(&rt->dst, RTAX_HOPLIMIT,
1238 ra_msg->icmph.icmp6_hop_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08001241skip_defrtr:
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /*
1244 * Update Reachable Time and Retrans Timer
1245 */
1246
1247 if (in6_dev->nd_parms) {
1248 unsigned long rtime = ntohl(ra_msg->retrans_timer);
1249
1250 if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/HZ) {
1251 rtime = (rtime*HZ)/1000;
1252 if (rtime < HZ/10)
1253 rtime = HZ/10;
Jiri Pirko1f9248e52013-12-07 19:26:53 +01001254 NEIGH_VAR_SET(in6_dev->nd_parms, RETRANS_TIME, rtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 in6_dev->tstamp = jiffies;
1256 inet6_ifinfo_notify(RTM_NEWLINK, in6_dev);
1257 }
1258
1259 rtime = ntohl(ra_msg->reachable_time);
1260 if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/(3*HZ)) {
1261 rtime = (rtime*HZ)/1000;
1262
1263 if (rtime < HZ/10)
1264 rtime = HZ/10;
1265
Jiri Pirko1f9248e52013-12-07 19:26:53 +01001266 if (rtime != NEIGH_VAR(in6_dev->nd_parms, BASE_REACHABLE_TIME)) {
1267 NEIGH_VAR_SET(in6_dev->nd_parms,
1268 BASE_REACHABLE_TIME, rtime);
1269 NEIGH_VAR_SET(in6_dev->nd_parms,
1270 GC_STALETIME, 3 * rtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 in6_dev->nd_parms->reachable_time = neigh_rand_reach_time(rtime);
1272 in6_dev->tstamp = jiffies;
1273 inet6_ifinfo_notify(RTM_NEWLINK, in6_dev);
1274 }
1275 }
1276 }
1277
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001278skip_linkparms:
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /*
1281 * Process options.
1282 */
1283
1284 if (!neigh)
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001285 neigh = __neigh_lookup(&nd_tbl, &ipv6_hdr(skb)->saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 skb->dev, 1);
1287 if (neigh) {
1288 u8 *lladdr = NULL;
1289 if (ndopts.nd_opts_src_lladdr) {
1290 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
1291 skb->dev);
1292 if (!lladdr) {
Joe Perches675418d2012-05-16 19:28:38 +00001293 ND_PRINTK(2, warn,
1294 "RA: invalid link-layer address length\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 goto out;
1296 }
1297 }
1298 neigh_update(neigh, lladdr, NUD_STALE,
1299 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1300 NEIGH_UPDATE_F_OVERRIDE|
1301 NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1302 NEIGH_UPDATE_F_ISROUTER);
1303 }
1304
Ben Greearf2a762d2014-06-25 14:44:52 -07001305 if (!ipv6_accept_ra(in6_dev)) {
1306 ND_PRINTK(2, info,
1307 "RA: %s, accept_ra is false for dev: %s\n",
1308 __func__, skb->dev->name);
David Ward31ce8c72009-08-29 00:04:09 -07001309 goto out;
Ben Greearf2a762d2014-06-25 14:44:52 -07001310 }
David Ward31ce8c72009-08-29 00:04:09 -07001311
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001312#ifdef CONFIG_IPV6_ROUTE_INFO
Li RongQingb6428812014-07-10 18:02:46 +08001313 if (!in6_dev->cnf.accept_ra_from_local &&
1314 ipv6_chk_addr(dev_net(in6_dev->dev), &ipv6_hdr(skb)->saddr,
1315 NULL, 0)) {
Ben Greearf2a762d2014-06-25 14:44:52 -07001316 ND_PRINTK(2, info,
Ben Greeard9333192014-06-25 14:44:53 -07001317 "RA from local address detected on dev: %s: router info ignored.\n",
1318 skb->dev->name);
Andreas Hofmeister9f562202011-10-24 19:13:15 -04001319 goto skip_routeinfo;
Ben Greearf2a762d2014-06-25 14:44:52 -07001320 }
Andreas Hofmeister9f562202011-10-24 19:13:15 -04001321
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08001322 if (in6_dev->cnf.accept_ra_rtr_pref && ndopts.nd_opts_ri) {
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001323 struct nd_opt_hdr *p;
1324 for (p = ndopts.nd_opts_ri;
1325 p;
1326 p = ndisc_next_option(p, ndopts.nd_opts_ri_end)) {
YOSHIFUJI Hideaki6294e002008-03-15 23:56:52 -04001327 struct route_info *ri = (struct route_info *)p;
1328#ifdef CONFIG_IPV6_NDISC_NODETYPE
1329 if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT &&
1330 ri->prefix_len == 0)
1331 continue;
1332#endif
Duan Jiong30e56912013-11-26 15:46:56 +08001333 if (ri->prefix_len == 0 &&
1334 !in6_dev->cnf.accept_ra_defrtr)
1335 continue;
YOSHIFUJI Hideaki6294e002008-03-15 23:56:52 -04001336 if (ri->prefix_len > in6_dev->cnf.accept_ra_rt_info_max_plen)
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08001337 continue;
Ian Morris67ba4152014-08-24 21:53:10 +01001338 rt6_route_rcv(skb->dev, (u8 *)p, (p->nd_opt_len) << 3,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001339 &ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001340 }
1341 }
Andreas Hofmeister9f562202011-10-24 19:13:15 -04001342
1343skip_routeinfo:
YOSHIFUJI Hideaki70ceb4f2006-03-20 17:06:24 -08001344#endif
1345
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001346#ifdef CONFIG_IPV6_NDISC_NODETYPE
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001347 /* skip link-specific ndopts from interior routers */
Ben Greearf2a762d2014-06-25 14:44:52 -07001348 if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) {
1349 ND_PRINTK(2, info,
1350 "RA: %s, nodetype is NODEFAULT (interior routes), dev: %s\n",
1351 __func__, skb->dev->name);
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001352 goto out;
Ben Greearf2a762d2014-06-25 14:44:52 -07001353 }
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001354#endif
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001355
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08001356 if (in6_dev->cnf.accept_ra_pinfo && ndopts.nd_opts_pi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 struct nd_opt_hdr *p;
1358 for (p = ndopts.nd_opts_pi;
1359 p;
1360 p = ndisc_next_option(p, ndopts.nd_opts_pi_end)) {
Neil Hormane6bff992012-01-04 10:49:15 +00001361 addrconf_prefix_rcv(skb->dev, (u8 *)p,
1362 (p->nd_opt_len) << 3,
1363 ndopts.nd_opts_src_lladdr != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 }
1366
Harout Hedeshianc2943f12015-01-20 10:06:05 -07001367 if (ndopts.nd_opts_mtu && in6_dev->cnf.accept_ra_mtu) {
Al Viroe69a4adc2006-11-14 20:56:00 -08001368 __be32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 u32 mtu;
1370
Ian Morris67ba4152014-08-24 21:53:10 +01001371 memcpy(&n, ((u8 *)(ndopts.nd_opts_mtu+1))+2, sizeof(mtu));
Al Viroe69a4adc2006-11-14 20:56:00 -08001372 mtu = ntohl(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 if (mtu < IPV6_MIN_MTU || mtu > skb->dev->mtu) {
Joe Perches675418d2012-05-16 19:28:38 +00001375 ND_PRINTK(2, warn, "RA: invalid mtu: %d\n", mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 } else if (in6_dev->cnf.mtu6 != mtu) {
1377 in6_dev->cnf.mtu6 = mtu;
1378
1379 if (rt)
David S. Millerdefb3512010-12-08 21:16:57 -08001380 dst_metric_set(&rt->dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 rt6_mtu_change(skb->dev, mtu);
1383 }
1384 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001385
Pierre Ynard31910572007-10-10 21:22:05 -07001386 if (ndopts.nd_useropts) {
YOSHIFUJI Hideaki61cf46ad2008-01-22 17:32:53 +09001387 struct nd_opt_hdr *p;
1388 for (p = ndopts.nd_useropts;
1389 p;
1390 p = ndisc_next_useropt(p, ndopts.nd_useropts_end)) {
1391 ndisc_ra_useropt(skb, p);
Pierre Ynard31910572007-10-10 21:22:05 -07001392 }
1393 }
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (ndopts.nd_opts_tgt_lladdr || ndopts.nd_opts_rh) {
Joe Perches675418d2012-05-16 19:28:38 +00001396 ND_PRINTK(2, warn, "RA: invalid RA options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398out:
Amerigo Wang94e187c2012-10-29 00:13:19 +00001399 ip6_rt_put(rt);
David S. Millereb857182012-01-27 15:07:56 -08001400 if (neigh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 neigh_release(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
1404static void ndisc_redirect_rcv(struct sk_buff *skb)
1405{
Duan Jiong093d04d2012-12-14 02:59:59 +00001406 u8 *hdr;
1407 struct ndisc_options ndopts;
1408 struct rd_msg *msg = (struct rd_msg *)skb_transport_header(skb);
Simon Horman29a3cad2013-05-28 20:34:26 +00001409 u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
Duan Jiong093d04d2012-12-14 02:59:59 +00001410 offsetof(struct rd_msg, opt));
1411
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001412#ifdef CONFIG_IPV6_NDISC_NODETYPE
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001413 switch (skb->ndisc_nodetype) {
1414 case NDISC_NODETYPE_HOST:
1415 case NDISC_NODETYPE_NODEFAULT:
Joe Perches675418d2012-05-16 19:28:38 +00001416 ND_PRINTK(2, warn,
1417 "Redirect: from host or unauthorized router\n");
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001418 return;
1419 }
YOSHIFUJI Hideakide357cc2008-03-15 23:59:18 -04001420#endif
Templin, Fred Lfadf6bf2008-03-11 18:35:59 -04001421
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001422 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
Joe Perches675418d2012-05-16 19:28:38 +00001423 ND_PRINTK(2, warn,
1424 "Redirect: source address is not link-local\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return;
1426 }
1427
Duan Jiong093d04d2012-12-14 02:59:59 +00001428 if (!ndisc_parse_options(msg->opt, ndoptlen, &ndopts))
1429 return;
1430
Duan Jiongc92a59e2013-08-22 12:07:35 +08001431 if (!ndopts.nd_opts_rh) {
Duan Jiongb55b76b2013-09-04 19:44:21 +08001432 ip6_redirect_no_header(skb, dev_net(skb->dev),
1433 skb->dev->ifindex, 0);
Duan Jiong093d04d2012-12-14 02:59:59 +00001434 return;
Duan Jiongc92a59e2013-08-22 12:07:35 +08001435 }
Duan Jiong093d04d2012-12-14 02:59:59 +00001436
1437 hdr = (u8 *)ndopts.nd_opts_rh;
1438 hdr += 8;
1439 if (!pskb_pull(skb, hdr - skb_transport_header(skb)))
1440 return;
1441
David S. Millerb94f1c02012-07-12 00:33:37 -07001442 icmpv6_notify(skb, NDISC_REDIRECT, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
YOSHIFUJI Hideaki / 吉藤英明5f5a0112013-01-21 06:48:53 +00001445static void ndisc_fill_redirect_hdr_option(struct sk_buff *skb,
1446 struct sk_buff *orig_skb,
1447 int rd_len)
YOSHIFUJI Hideaki / 吉藤英明9c86daf2013-01-21 06:48:09 +00001448{
YOSHIFUJI Hideaki / 吉藤英明5f5a0112013-01-21 06:48:53 +00001449 u8 *opt = skb_put(skb, rd_len);
1450
YOSHIFUJI Hideaki / 吉藤英明9c86daf2013-01-21 06:48:09 +00001451 memset(opt, 0, 8);
1452 *(opt++) = ND_OPT_REDIRECT_HDR;
1453 *(opt++) = (rd_len >> 3);
1454 opt += 6;
1455
1456 memcpy(opt, ipv6_hdr(orig_skb), rd_len - 8);
YOSHIFUJI Hideaki / 吉藤英明9c86daf2013-01-21 06:48:09 +00001457}
1458
David S. Miller49919692012-01-27 15:30:48 -08001459void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001461 struct net_device *dev = skb->dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001462 struct net *net = dev_net(dev);
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001463 struct sock *sk = net->ipv6.ndisc_sk;
YOSHIFUJI Hideaki / 吉藤英明2ce135762013-01-21 06:48:49 +00001464 int optlen = 0;
David S. Millerfbfe95a2012-06-08 23:24:18 -07001465 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 struct sk_buff *buff;
YOSHIFUJI Hideaki / 吉藤英明71bcdba2013-01-05 16:34:51 +00001467 struct rd_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 struct in6_addr saddr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 struct rt6_info *rt;
1470 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -05001471 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int rd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 u8 ha_buf[MAX_ADDR_LEN], *ha = NULL;
David S. Miller1d861aa2012-07-10 03:58:16 -07001474 bool ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Neil Horman95c385b2007-04-25 17:08:10 -07001476 if (ipv6_get_lladdr(dev, &saddr_buf, IFA_F_TENTATIVE)) {
Joe Perches675418d2012-05-16 19:28:38 +00001477 ND_PRINTK(2, warn, "Redirect: no link-local address on %s\n",
1478 dev->name);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001479 return;
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001482 if (!ipv6_addr_equal(&ipv6_hdr(skb)->daddr, target) &&
Brian Haleybf0b48d2007-10-08 00:12:05 -07001483 ipv6_addr_type(target) != (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
Joe Perches675418d2012-05-16 19:28:38 +00001484 ND_PRINTK(2, warn,
1485 "Redirect: target address is not link-local unicast\n");
Li Yewang29556522007-01-30 14:33:20 -08001486 return;
1487 }
1488
David S. Miller4c9483b2011-03-12 16:22:43 -05001489 icmpv6_flow_init(sk, &fl6, NDISC_REDIRECT,
YOSHIFUJI Hideaki95e41e92007-12-06 15:43:30 -08001490 &saddr_buf, &ipv6_hdr(skb)->saddr, dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
David S. Miller4c9483b2011-03-12 16:22:43 -05001492 dst = ip6_route_output(net, NULL, &fl6);
RongQing.Li5095d642012-02-21 22:10:49 +00001493 if (dst->error) {
1494 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return;
RongQing.Li5095d642012-02-21 22:10:49 +00001496 }
David S. Miller4c9483b2011-03-12 16:22:43 -05001497 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
David S. Miller452edd52011-03-02 13:27:41 -08001498 if (IS_ERR(dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 rt = (struct rt6_info *) dst;
1502
1503 if (rt->rt6i_flags & RTF_GATEWAY) {
Joe Perches675418d2012-05-16 19:28:38 +00001504 ND_PRINTK(2, warn,
1505 "Redirect: destination is not a neighbour\n");
Ilpo Järvinend73f0802009-02-06 23:47:37 -08001506 goto release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
David S. Miller1d861aa2012-07-10 03:58:16 -07001508 peer = inet_getpeer_v6(net->ipv6.peers, &rt->rt6i_dst.addr, 1);
1509 ret = inet_peer_xrlim_allow(peer, 1*HZ);
1510 if (peer)
1511 inet_putpeer(peer);
1512 if (!ret)
Ilpo Järvinend73f0802009-02-06 23:47:37 -08001513 goto release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 if (dev->addr_len) {
David S. Miller49919692012-01-27 15:30:48 -08001516 struct neighbour *neigh = dst_neigh_lookup(skb_dst(skb), target);
1517 if (!neigh) {
Joe Perches675418d2012-05-16 19:28:38 +00001518 ND_PRINTK(2, warn,
1519 "Redirect: no neigh for target address\n");
David S. Miller49919692012-01-27 15:30:48 -08001520 goto release;
1521 }
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 read_lock_bh(&neigh->lock);
1524 if (neigh->nud_state & NUD_VALID) {
1525 memcpy(ha_buf, neigh->ha, dev->addr_len);
1526 read_unlock_bh(&neigh->lock);
1527 ha = ha_buf;
YOSHIFUJI Hideaki / 吉藤英明2ce135762013-01-21 06:48:49 +00001528 optlen += ndisc_opt_addr_space(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 } else
1530 read_unlock_bh(&neigh->lock);
David S. Miller49919692012-01-27 15:30:48 -08001531
1532 neigh_release(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534
1535 rd_len = min_t(unsigned int,
YOSHIFUJI Hideaki / 吉藤英明2ce135762013-01-21 06:48:49 +00001536 IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(*msg) - optlen,
1537 skb->len + 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 rd_len &= ~0x7;
YOSHIFUJI Hideaki / 吉藤英明2ce135762013-01-21 06:48:49 +00001539 optlen += rd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
YOSHIFUJI Hideaki / 吉藤英明2ce135762013-01-21 06:48:49 +00001541 buff = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
YOSHIFUJI Hideaki / 吉藤英明de093342013-01-21 06:48:14 +00001542 if (!buff)
Ilpo Järvinend73f0802009-02-06 23:47:37 -08001543 goto release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
YOSHIFUJI Hideaki / 吉藤英明4d5c1522013-01-21 06:49:25 +00001545 msg = (struct rd_msg *)skb_put(buff, sizeof(*msg));
1546 *msg = (struct rd_msg) {
1547 .icmph = {
1548 .icmp6_type = NDISC_REDIRECT,
1549 },
1550 .target = *target,
1551 .dest = ipv6_hdr(skb)->daddr,
1552 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /*
1555 * include target_address option
1556 */
1557
1558 if (ha)
Matthias Schiffer33be0812013-05-31 03:27:55 +02001559 ndisc_fill_addr_option(buff, ND_OPT_TARGET_LL_ADDR, ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 /*
1562 * build redirect option and copy skb over to the new packet.
1563 */
1564
YOSHIFUJI Hideaki / 吉藤英明9c86daf2013-01-21 06:48:09 +00001565 if (rd_len)
YOSHIFUJI Hideaki / 吉藤英明5f5a0112013-01-21 06:48:53 +00001566 ndisc_fill_redirect_hdr_option(buff, skb, rd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Eric Dumazetadf30902009-06-02 05:19:30 +00001568 skb_dst_set(buff, dst);
YOSHIFUJI Hideaki / 吉藤英明f4de84c2013-01-21 06:49:03 +00001569 ndisc_send_skb(buff, &ipv6_hdr(skb)->saddr, &saddr_buf);
Ilpo Järvinend73f0802009-02-06 23:47:37 -08001570 return;
1571
1572release:
1573 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
1576static void pndisc_redo(struct sk_buff *skb)
1577{
YOSHIFUJI Hideaki140e26fc2005-10-05 12:11:41 -07001578 ndisc_recv_ns(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 kfree_skb(skb);
1580}
1581
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02001582static bool ndisc_suppress_frag_ndisc(struct sk_buff *skb)
1583{
1584 struct inet6_dev *idev = __in6_dev_get(skb->dev);
1585
1586 if (!idev)
1587 return true;
1588 if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED &&
1589 idev->cnf.suppress_frag_ndisc) {
1590 net_warn_ratelimited("Received fragmented ndisc packet. Carefully consider disabling suppress_frag_ndisc.\n");
1591 return true;
1592 }
1593 return false;
1594}
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596int ndisc_rcv(struct sk_buff *skb)
1597{
1598 struct nd_msg *msg;
1599
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02001600 if (ndisc_suppress_frag_ndisc(skb))
1601 return 0;
1602
YOSHIFUJI Hideaki / 吉藤英明6bce6b42013-01-21 06:48:03 +00001603 if (skb_linearize(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return 0;
1605
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001606 msg = (struct nd_msg *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001608 __skb_push(skb, skb->data - skb_transport_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001610 if (ipv6_hdr(skb)->hop_limit != 255) {
Joe Perches675418d2012-05-16 19:28:38 +00001611 ND_PRINTK(2, warn, "NDISC: invalid hop-limit: %d\n",
1612 ipv6_hdr(skb)->hop_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return 0;
1614 }
1615
1616 if (msg->icmph.icmp6_code != 0) {
Joe Perches675418d2012-05-16 19:28:38 +00001617 ND_PRINTK(2, warn, "NDISC: invalid ICMPv6 code: %d\n",
1618 msg->icmph.icmp6_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return 0;
1620 }
1621
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001622 memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 switch (msg->icmph.icmp6_type) {
1625 case NDISC_NEIGHBOUR_SOLICITATION:
1626 ndisc_recv_ns(skb);
1627 break;
1628
1629 case NDISC_NEIGHBOUR_ADVERTISEMENT:
1630 ndisc_recv_na(skb);
1631 break;
1632
1633 case NDISC_ROUTER_SOLICITATION:
1634 ndisc_recv_rs(skb);
1635 break;
1636
1637 case NDISC_ROUTER_ADVERTISEMENT:
1638 ndisc_router_discovery(skb);
1639 break;
1640
1641 case NDISC_REDIRECT:
1642 ndisc_redirect_rcv(skb);
1643 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 return 0;
1647}
1648
1649static int ndisc_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
1650{
Jiri Pirko351638e2013-05-28 01:30:21 +00001651 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001652 struct net *net = dev_net(dev);
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00001653 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 switch (event) {
1656 case NETDEV_CHANGEADDR:
1657 neigh_changeaddr(&nd_tbl, dev);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001658 fib6_run_gc(0, net, false);
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00001659 idev = in6_dev_get(dev);
1660 if (!idev)
1661 break;
1662 if (idev->cnf.ndisc_notify)
1663 ndisc_send_unsol_na(dev);
1664 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 break;
1666 case NETDEV_DOWN:
1667 neigh_ifdown(&nd_tbl, dev);
Michal Kubeček2ac3ac82013-08-01 10:04:14 +02001668 fib6_run_gc(0, net, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 break;
Ben Hutchingsf47b9462011-04-15 13:46:02 +00001670 case NETDEV_NOTIFY_PEERS:
1671 ndisc_send_unsol_na(dev);
1672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 default:
1674 break;
1675 }
1676
1677 return NOTIFY_DONE;
1678}
1679
1680static struct notifier_block ndisc_netdev_notifier = {
1681 .notifier_call = ndisc_netdev_event,
1682};
1683
1684#ifdef CONFIG_SYSCTL
1685static void ndisc_warn_deprecated_sysctl(struct ctl_table *ctl,
1686 const char *func, const char *dev_name)
1687{
1688 static char warncomm[TASK_COMM_LEN];
1689 static int warned;
1690 if (strcmp(warncomm, current->comm) && warned < 5) {
1691 strcpy(warncomm, current->comm);
Joe Perchesf3213832012-05-15 14:11:53 +00001692 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 -07001693 warncomm, func,
1694 dev_name, ctl->procname,
1695 dev_name, ctl->procname);
1696 warned++;
1697 }
1698}
1699
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07001700int 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 -07001701{
1702 struct net_device *dev = ctl->extra1;
1703 struct inet6_dev *idev;
1704 int ret;
1705
Eric W. Biedermand12af672007-10-18 03:05:25 -07001706 if ((strcmp(ctl->procname, "retrans_time") == 0) ||
1707 (strcmp(ctl->procname, "base_reachable_time") == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 ndisc_warn_deprecated_sysctl(ctl, "syscall", dev ? dev->name : "default");
1709
Eric W. Biedermand12af672007-10-18 03:05:25 -07001710 if (strcmp(ctl->procname, "retrans_time") == 0)
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01001711 ret = neigh_proc_dointvec(ctl, write, buffer, lenp, ppos);
Eric W. Biedermand12af672007-10-18 03:05:25 -07001712
1713 else if (strcmp(ctl->procname, "base_reachable_time") == 0)
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01001714 ret = neigh_proc_dointvec_jiffies(ctl, write,
1715 buffer, lenp, ppos);
Eric W. Biedermand12af672007-10-18 03:05:25 -07001716
1717 else if ((strcmp(ctl->procname, "retrans_time_ms") == 0) ||
YOSHIFUJI Hideakiad02ac12007-10-29 01:32:23 -07001718 (strcmp(ctl->procname, "base_reachable_time_ms") == 0))
Jiri Pirkocb5b09c2013-12-07 19:26:54 +01001719 ret = neigh_proc_dointvec_ms_jiffies(ctl, write,
1720 buffer, lenp, ppos);
Eric W. Biedermand12af672007-10-18 03:05:25 -07001721 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 if (write && ret == 0 && dev && (idev = in6_dev_get(dev)) != NULL) {
Jiri Pirko1f9248e52013-12-07 19:26:53 +01001725 if (ctl->data == &NEIGH_VAR(idev->nd_parms, BASE_REACHABLE_TIME))
1726 idev->nd_parms->reachable_time =
1727 neigh_rand_reach_time(NEIGH_VAR(idev->nd_parms, BASE_REACHABLE_TIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 idev->tstamp = jiffies;
1729 inet6_ifinfo_notify(RTM_NEWLINK, idev);
1730 in6_dev_put(idev);
1731 }
1732 return ret;
1733}
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736#endif
1737
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001738static int __net_init ndisc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
1740 struct ipv6_pinfo *np;
1741 struct sock *sk;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001742 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Denis V. Lunev1ed85162008-04-03 14:31:03 -07001744 err = inet_ctl_sock_create(&sk, PF_INET6,
1745 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (err < 0) {
Joe Perches675418d2012-05-16 19:28:38 +00001747 ND_PRINTK(0, err,
1748 "NDISC: Failed to initialize the control socket (err %d)\n",
1749 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return err;
1751 }
1752
Denis V. Lunev1ed85162008-04-03 14:31:03 -07001753 net->ipv6.ndisc_sk = sk;
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 np->hop_limit = 255;
1757 /* Do not loopback ndisc messages */
1758 np->mc_loop = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001760 return 0;
1761}
1762
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001763static void __net_exit ndisc_net_exit(struct net *net)
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001764{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07001765 inet_ctl_sock_destroy(net->ipv6.ndisc_sk);
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001766}
1767
1768static struct pernet_operations ndisc_net_ops = {
1769 .init = ndisc_net_init,
1770 .exit = ndisc_net_exit,
1771};
1772
1773int __init ndisc_init(void)
1774{
1775 int err;
1776
1777 err = register_pernet_subsys(&ndisc_net_ops);
1778 if (err)
1779 return err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001780 /*
1781 * Initialize the neighbour table
1782 */
WANG Congd7480fd2014-11-10 15:59:36 -08001783 neigh_table_init(NEIGH_ND_TABLE, &nd_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785#ifdef CONFIG_SYSCTL
Jiri Pirko73af6142013-12-07 19:26:55 +01001786 err = neigh_sysctl_register(NULL, &nd_tbl.parms,
Himangi Saraogi56ec0fb2014-07-25 01:49:37 +05301787 ndisc_ifinfo_sysctl_change);
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001788 if (err)
1789 goto out_unregister_pernet;
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001790out:
Fabio Estevambcd081a2013-11-16 00:52:08 -02001791#endif
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001792 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001794#ifdef CONFIG_SYSCTL
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001795out_unregister_pernet:
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001796 unregister_pernet_subsys(&ndisc_net_ops);
1797 goto out;
Michal Kubeček2c861cc2013-09-09 21:45:04 +02001798#endif
1799}
1800
1801int __init ndisc_late_init(void)
1802{
1803 return register_netdevice_notifier(&ndisc_netdev_notifier);
1804}
1805
1806void ndisc_late_cleanup(void)
1807{
1808 unregister_netdevice_notifier(&ndisc_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
1811void ndisc_cleanup(void)
1812{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813#ifdef CONFIG_SYSCTL
1814 neigh_sysctl_unregister(&nd_tbl.parms);
1815#endif
WANG Congd7480fd2014-11-10 15:59:36 -08001816 neigh_table_clear(NEIGH_ND_TABLE, &nd_tbl);
Daniel Lezcano1762f7e2008-03-07 11:15:34 -08001817 unregister_pernet_subsys(&ndisc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}