Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Neighbour Discovery for IPv6 |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3 | * Linux INET6 implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * 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 | * |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 18 | * Pierre Ynard : export userland ND options |
| 19 | * through netlink (RDNSS support) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | * Lars Fenneberg : fixed MTU setting on receipt |
| 21 | * of an RA. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * Janos Farkas : kmalloc failure checks |
| 23 | * Alexey Kuznetsov : state machine reworked |
| 24 | * and moved to net/core. |
| 25 | * Pekka Savola : RFC2461 validation |
| 26 | * YOSHIFUJI Hideaki @USAGI : Verify ND options properly |
| 27 | */ |
| 28 | |
| 29 | /* Set to 3 to get tracing... */ |
| 30 | #define ND_DEBUG 1 |
| 31 | |
| 32 | #define ND_PRINTK(fmt, args...) do { if (net_ratelimit()) { printk(fmt, ## args); } } while(0) |
| 33 | #define ND_NOPRINTK(x...) do { ; } while(0) |
| 34 | #define ND_PRINTK0 ND_PRINTK |
| 35 | #define ND_PRINTK1 ND_NOPRINTK |
| 36 | #define ND_PRINTK2 ND_NOPRINTK |
| 37 | #define ND_PRINTK3 ND_NOPRINTK |
| 38 | #if ND_DEBUG >= 1 |
| 39 | #undef ND_PRINTK1 |
| 40 | #define ND_PRINTK1 ND_PRINTK |
| 41 | #endif |
| 42 | #if ND_DEBUG >= 2 |
| 43 | #undef ND_PRINTK2 |
| 44 | #define ND_PRINTK2 ND_PRINTK |
| 45 | #endif |
| 46 | #if ND_DEBUG >= 3 |
| 47 | #undef ND_PRINTK3 |
| 48 | #define ND_PRINTK3 ND_PRINTK |
| 49 | #endif |
| 50 | |
| 51 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/errno.h> |
| 53 | #include <linux/types.h> |
| 54 | #include <linux/socket.h> |
| 55 | #include <linux/sockios.h> |
| 56 | #include <linux/sched.h> |
| 57 | #include <linux/net.h> |
| 58 | #include <linux/in6.h> |
| 59 | #include <linux/route.h> |
| 60 | #include <linux/init.h> |
| 61 | #include <linux/rcupdate.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 62 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #ifdef CONFIG_SYSCTL |
| 64 | #include <linux/sysctl.h> |
| 65 | #endif |
| 66 | |
Thomas Graf | 1823730 | 2006-08-04 23:04:54 -0700 | [diff] [blame] | 67 | #include <linux/if_addr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <linux/if_arp.h> |
| 69 | #include <linux/ipv6.h> |
| 70 | #include <linux/icmpv6.h> |
| 71 | #include <linux/jhash.h> |
| 72 | |
| 73 | #include <net/sock.h> |
| 74 | #include <net/snmp.h> |
| 75 | |
| 76 | #include <net/ipv6.h> |
| 77 | #include <net/protocol.h> |
| 78 | #include <net/ndisc.h> |
| 79 | #include <net/ip6_route.h> |
| 80 | #include <net/addrconf.h> |
| 81 | #include <net/icmp.h> |
| 82 | |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 83 | #include <net/netlink.h> |
| 84 | #include <linux/rtnetlink.h> |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | #include <net/flow.h> |
| 87 | #include <net/ip6_checksum.h> |
Denis V. Lunev | 1ed8516 | 2008-04-03 14:31:03 -0700 | [diff] [blame] | 88 | #include <net/inet_common.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | #include <linux/proc_fs.h> |
| 90 | |
| 91 | #include <linux/netfilter.h> |
| 92 | #include <linux/netfilter_ipv6.h> |
| 93 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 94 | static u32 ndisc_hash(const void *pkey, |
| 95 | const struct net_device *dev, |
| 96 | __u32 rnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | static int ndisc_constructor(struct neighbour *neigh); |
| 98 | static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb); |
| 99 | static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb); |
| 100 | static int pndisc_constructor(struct pneigh_entry *n); |
| 101 | static void pndisc_destructor(struct pneigh_entry *n); |
| 102 | static void pndisc_redo(struct sk_buff *skb); |
| 103 | |
Stephen Hemminger | 89d69d2 | 2009-09-01 11:13:19 +0000 | [diff] [blame] | 104 | static const struct neigh_ops ndisc_generic_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | .family = AF_INET6, |
| 106 | .solicit = ndisc_solicit, |
| 107 | .error_report = ndisc_error_report, |
| 108 | .output = neigh_resolve_output, |
| 109 | .connected_output = neigh_connected_output, |
| 110 | .hh_output = dev_queue_xmit, |
| 111 | .queue_xmit = dev_queue_xmit, |
| 112 | }; |
| 113 | |
Stephen Hemminger | 89d69d2 | 2009-09-01 11:13:19 +0000 | [diff] [blame] | 114 | static const struct neigh_ops ndisc_hh_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | .family = AF_INET6, |
| 116 | .solicit = ndisc_solicit, |
| 117 | .error_report = ndisc_error_report, |
| 118 | .output = neigh_resolve_output, |
| 119 | .connected_output = neigh_resolve_output, |
| 120 | .hh_output = dev_queue_xmit, |
| 121 | .queue_xmit = dev_queue_xmit, |
| 122 | }; |
| 123 | |
| 124 | |
Stephen Hemminger | 89d69d2 | 2009-09-01 11:13:19 +0000 | [diff] [blame] | 125 | static const struct neigh_ops ndisc_direct_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | .family = AF_INET6, |
| 127 | .output = dev_queue_xmit, |
| 128 | .connected_output = dev_queue_xmit, |
| 129 | .hh_output = dev_queue_xmit, |
| 130 | .queue_xmit = dev_queue_xmit, |
| 131 | }; |
| 132 | |
| 133 | struct neigh_table nd_tbl = { |
| 134 | .family = AF_INET6, |
| 135 | .entry_size = sizeof(struct neighbour) + sizeof(struct in6_addr), |
| 136 | .key_len = sizeof(struct in6_addr), |
| 137 | .hash = ndisc_hash, |
| 138 | .constructor = ndisc_constructor, |
| 139 | .pconstructor = pndisc_constructor, |
| 140 | .pdestructor = pndisc_destructor, |
| 141 | .proxy_redo = pndisc_redo, |
| 142 | .id = "ndisc_cache", |
| 143 | .parms = { |
Shan Wei | b672083 | 2010-12-01 18:05:12 +0000 | [diff] [blame] | 144 | .tbl = &nd_tbl, |
| 145 | .base_reachable_time = ND_REACHABLE_TIME, |
| 146 | .retrans_time = ND_RETRANS_TIMER, |
| 147 | .gc_staletime = 60 * HZ, |
| 148 | .reachable_time = ND_REACHABLE_TIME, |
| 149 | .delay_probe_time = 5 * HZ, |
| 150 | .queue_len = 3, |
| 151 | .ucast_probes = 3, |
| 152 | .mcast_probes = 3, |
| 153 | .anycast_delay = 1 * HZ, |
| 154 | .proxy_delay = (8 * HZ) / 10, |
| 155 | .proxy_qlen = 64, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | }, |
| 157 | .gc_interval = 30 * HZ, |
| 158 | .gc_thresh1 = 128, |
| 159 | .gc_thresh2 = 512, |
| 160 | .gc_thresh3 = 1024, |
| 161 | }; |
| 162 | |
| 163 | /* ND options */ |
| 164 | struct ndisc_options { |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 165 | struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX]; |
| 166 | #ifdef CONFIG_IPV6_ROUTE_INFO |
| 167 | struct nd_opt_hdr *nd_opts_ri; |
| 168 | struct nd_opt_hdr *nd_opts_ri_end; |
| 169 | #endif |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 170 | struct nd_opt_hdr *nd_useropts; |
| 171 | struct nd_opt_hdr *nd_useropts_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | #define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR] |
| 175 | #define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR] |
| 176 | #define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO] |
| 177 | #define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END] |
| 178 | #define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR] |
| 179 | #define nd_opts_mtu nd_opt_array[ND_OPT_MTU] |
| 180 | |
| 181 | #define NDISC_OPT_SPACE(len) (((len)+2+7)&~7) |
| 182 | |
| 183 | /* |
| 184 | * Return the padding between the option length and the start of the |
| 185 | * link addr. Currently only IP-over-InfiniBand needs this, although |
| 186 | * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may |
| 187 | * also need a pad of 2. |
| 188 | */ |
| 189 | static int ndisc_addr_option_pad(unsigned short type) |
| 190 | { |
| 191 | switch (type) { |
| 192 | case ARPHRD_INFINIBAND: return 2; |
| 193 | default: return 0; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static inline int ndisc_opt_addr_space(struct net_device *dev) |
| 198 | { |
| 199 | return NDISC_OPT_SPACE(dev->addr_len + ndisc_addr_option_pad(dev->type)); |
| 200 | } |
| 201 | |
| 202 | static u8 *ndisc_fill_addr_option(u8 *opt, int type, void *data, int data_len, |
| 203 | unsigned short addr_type) |
| 204 | { |
| 205 | int space = NDISC_OPT_SPACE(data_len); |
| 206 | int pad = ndisc_addr_option_pad(addr_type); |
| 207 | |
| 208 | opt[0] = type; |
| 209 | opt[1] = space>>3; |
| 210 | |
| 211 | memset(opt + 2, 0, pad); |
| 212 | opt += pad; |
| 213 | space -= pad; |
| 214 | |
| 215 | memcpy(opt+2, data, data_len); |
| 216 | data_len += 2; |
| 217 | opt += data_len; |
| 218 | if ((space -= data_len) > 0) |
| 219 | memset(opt, 0, space); |
| 220 | return opt + space; |
| 221 | } |
| 222 | |
| 223 | static struct nd_opt_hdr *ndisc_next_option(struct nd_opt_hdr *cur, |
| 224 | struct nd_opt_hdr *end) |
| 225 | { |
| 226 | int type; |
| 227 | if (!cur || !end || cur >= end) |
| 228 | return NULL; |
| 229 | type = cur->nd_opt_type; |
| 230 | do { |
| 231 | cur = ((void *)cur) + (cur->nd_opt_len << 3); |
| 232 | } while(cur < end && cur->nd_opt_type != type); |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 233 | return cur <= end && cur->nd_opt_type == type ? cur : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 236 | static inline int ndisc_is_useropt(struct nd_opt_hdr *opt) |
| 237 | { |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 238 | return opt->nd_opt_type == ND_OPT_RDNSS; |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static struct nd_opt_hdr *ndisc_next_useropt(struct nd_opt_hdr *cur, |
| 242 | struct nd_opt_hdr *end) |
| 243 | { |
| 244 | if (!cur || !end || cur >= end) |
| 245 | return NULL; |
| 246 | do { |
| 247 | cur = ((void *)cur) + (cur->nd_opt_len << 3); |
| 248 | } while(cur < end && !ndisc_is_useropt(cur)); |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 249 | return cur <= end && ndisc_is_useropt(cur) ? cur : NULL; |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | static struct ndisc_options *ndisc_parse_options(u8 *opt, int opt_len, |
| 253 | struct ndisc_options *ndopts) |
| 254 | { |
| 255 | struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)opt; |
| 256 | |
| 257 | if (!nd_opt || opt_len < 0 || !ndopts) |
| 258 | return NULL; |
| 259 | memset(ndopts, 0, sizeof(*ndopts)); |
| 260 | while (opt_len) { |
| 261 | int l; |
| 262 | if (opt_len < sizeof(struct nd_opt_hdr)) |
| 263 | return NULL; |
| 264 | l = nd_opt->nd_opt_len << 3; |
| 265 | if (opt_len < l || l == 0) |
| 266 | return NULL; |
| 267 | switch (nd_opt->nd_opt_type) { |
| 268 | case ND_OPT_SOURCE_LL_ADDR: |
| 269 | case ND_OPT_TARGET_LL_ADDR: |
| 270 | case ND_OPT_MTU: |
| 271 | case ND_OPT_REDIRECT_HDR: |
| 272 | if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) { |
| 273 | ND_PRINTK2(KERN_WARNING |
| 274 | "%s(): duplicated ND6 option found: type=%d\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 275 | __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | nd_opt->nd_opt_type); |
| 277 | } else { |
| 278 | ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt; |
| 279 | } |
| 280 | break; |
| 281 | case ND_OPT_PREFIX_INFO: |
| 282 | ndopts->nd_opts_pi_end = nd_opt; |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 283 | if (!ndopts->nd_opt_array[nd_opt->nd_opt_type]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt; |
| 285 | break; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 286 | #ifdef CONFIG_IPV6_ROUTE_INFO |
| 287 | case ND_OPT_ROUTE_INFO: |
| 288 | ndopts->nd_opts_ri_end = nd_opt; |
| 289 | if (!ndopts->nd_opts_ri) |
| 290 | ndopts->nd_opts_ri = nd_opt; |
| 291 | break; |
| 292 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | default: |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 294 | if (ndisc_is_useropt(nd_opt)) { |
| 295 | ndopts->nd_useropts_end = nd_opt; |
| 296 | if (!ndopts->nd_useropts) |
| 297 | ndopts->nd_useropts = nd_opt; |
| 298 | } else { |
| 299 | /* |
| 300 | * Unknown options must be silently ignored, |
| 301 | * to accommodate future extension to the |
| 302 | * protocol. |
| 303 | */ |
| 304 | ND_PRINTK2(KERN_NOTICE |
| 305 | "%s(): ignored unsupported option; type=%d, len=%d\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 306 | __func__, |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 307 | nd_opt->nd_opt_type, nd_opt->nd_opt_len); |
| 308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | opt_len -= l; |
| 311 | nd_opt = ((void *)nd_opt) + l; |
| 312 | } |
| 313 | return ndopts; |
| 314 | } |
| 315 | |
| 316 | static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p, |
| 317 | struct net_device *dev) |
| 318 | { |
| 319 | u8 *lladdr = (u8 *)(p + 1); |
| 320 | int lladdrlen = p->nd_opt_len << 3; |
| 321 | int prepad = ndisc_addr_option_pad(dev->type); |
| 322 | if (lladdrlen != NDISC_OPT_SPACE(dev->addr_len + prepad)) |
| 323 | return NULL; |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 324 | return lladdr + prepad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | int ndisc_mc_map(struct in6_addr *addr, char *buf, struct net_device *dev, int dir) |
| 328 | { |
| 329 | switch (dev->type) { |
| 330 | case ARPHRD_ETHER: |
| 331 | case ARPHRD_IEEE802: /* Not sure. Check it later. --ANK */ |
| 332 | case ARPHRD_FDDI: |
| 333 | ipv6_eth_mc_map(addr, buf); |
| 334 | return 0; |
| 335 | case ARPHRD_IEEE802_TR: |
| 336 | ipv6_tr_mc_map(addr,buf); |
| 337 | return 0; |
| 338 | case ARPHRD_ARCNET: |
| 339 | ipv6_arcnet_mc_map(addr, buf); |
| 340 | return 0; |
| 341 | case ARPHRD_INFINIBAND: |
Rolf Manderscheid | a9e527e3 | 2007-12-10 13:38:41 -0700 | [diff] [blame] | 342 | ipv6_ib_mc_map(addr, dev->broadcast, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | return 0; |
| 344 | default: |
| 345 | if (dir) { |
| 346 | memcpy(buf, dev->broadcast, dev->addr_len); |
| 347 | return 0; |
| 348 | } |
| 349 | } |
| 350 | return -EINVAL; |
| 351 | } |
| 352 | |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 353 | EXPORT_SYMBOL(ndisc_mc_map); |
| 354 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 355 | static u32 ndisc_hash(const void *pkey, |
| 356 | const struct net_device *dev, |
| 357 | __u32 hash_rnd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
| 359 | const u32 *p32 = pkey; |
| 360 | u32 addr_hash, i; |
| 361 | |
| 362 | addr_hash = 0; |
| 363 | for (i = 0; i < (sizeof(struct in6_addr) / sizeof(u32)); i++) |
| 364 | addr_hash ^= *p32++; |
| 365 | |
Eric Dumazet | d6bf781 | 2010-10-04 06:15:44 +0000 | [diff] [blame] | 366 | return jhash_2words(addr_hash, dev->ifindex, hash_rnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static int ndisc_constructor(struct neighbour *neigh) |
| 370 | { |
| 371 | struct in6_addr *addr = (struct in6_addr*)&neigh->primary_key; |
| 372 | struct net_device *dev = neigh->dev; |
| 373 | struct inet6_dev *in6_dev; |
| 374 | struct neigh_parms *parms; |
| 375 | int is_multicast = ipv6_addr_is_multicast(addr); |
| 376 | |
| 377 | rcu_read_lock(); |
| 378 | in6_dev = in6_dev_get(dev); |
| 379 | if (in6_dev == NULL) { |
| 380 | rcu_read_unlock(); |
| 381 | return -EINVAL; |
| 382 | } |
| 383 | |
| 384 | parms = in6_dev->nd_parms; |
| 385 | __neigh_parms_put(neigh->parms); |
| 386 | neigh->parms = neigh_parms_clone(parms); |
| 387 | rcu_read_unlock(); |
| 388 | |
| 389 | neigh->type = is_multicast ? RTN_MULTICAST : RTN_UNICAST; |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 390 | if (!dev->header_ops) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | neigh->nud_state = NUD_NOARP; |
| 392 | neigh->ops = &ndisc_direct_ops; |
| 393 | neigh->output = neigh->ops->queue_xmit; |
| 394 | } else { |
| 395 | if (is_multicast) { |
| 396 | neigh->nud_state = NUD_NOARP; |
| 397 | ndisc_mc_map(addr, neigh->ha, dev, 1); |
| 398 | } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) { |
| 399 | neigh->nud_state = NUD_NOARP; |
| 400 | memcpy(neigh->ha, dev->dev_addr, dev->addr_len); |
| 401 | if (dev->flags&IFF_LOOPBACK) |
| 402 | neigh->type = RTN_LOCAL; |
| 403 | } else if (dev->flags&IFF_POINTOPOINT) { |
| 404 | neigh->nud_state = NUD_NOARP; |
| 405 | memcpy(neigh->ha, dev->broadcast, dev->addr_len); |
| 406 | } |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 407 | if (dev->header_ops->cache) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | neigh->ops = &ndisc_hh_ops; |
| 409 | else |
| 410 | neigh->ops = &ndisc_generic_ops; |
| 411 | if (neigh->nud_state&NUD_VALID) |
| 412 | neigh->output = neigh->ops->connected_output; |
| 413 | else |
| 414 | neigh->output = neigh->ops->output; |
| 415 | } |
| 416 | in6_dev_put(in6_dev); |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static int pndisc_constructor(struct pneigh_entry *n) |
| 421 | { |
| 422 | struct in6_addr *addr = (struct in6_addr*)&n->key; |
| 423 | struct in6_addr maddr; |
| 424 | struct net_device *dev = n->dev; |
| 425 | |
| 426 | if (dev == NULL || __in6_dev_get(dev) == NULL) |
| 427 | return -EINVAL; |
| 428 | addrconf_addr_solict_mult(addr, &maddr); |
| 429 | ipv6_dev_mc_inc(dev, &maddr); |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static void pndisc_destructor(struct pneigh_entry *n) |
| 434 | { |
| 435 | struct in6_addr *addr = (struct in6_addr*)&n->key; |
| 436 | struct in6_addr maddr; |
| 437 | struct net_device *dev = n->dev; |
| 438 | |
| 439 | if (dev == NULL || __in6_dev_get(dev) == NULL) |
| 440 | return; |
| 441 | addrconf_addr_solict_mult(addr, &maddr); |
| 442 | ipv6_dev_mc_dec(dev, &maddr); |
| 443 | } |
| 444 | |
Brian Haley | 305d552 | 2008-11-04 17:51:14 -0800 | [diff] [blame] | 445 | struct sk_buff *ndisc_build_skb(struct net_device *dev, |
| 446 | const struct in6_addr *daddr, |
| 447 | const struct in6_addr *saddr, |
| 448 | struct icmp6hdr *icmp6h, |
| 449 | const struct in6_addr *target, |
| 450 | int llinfo) |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 451 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 452 | struct net *net = dev_net(dev); |
Daniel Lezcano | 1762f7e | 2008-03-07 11:15:34 -0800 | [diff] [blame] | 453 | struct sock *sk = net->ipv6.ndisc_sk; |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 454 | struct sk_buff *skb; |
| 455 | struct icmp6hdr *hdr; |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 456 | int len; |
| 457 | int err; |
Brian Haley | 305d552 | 2008-11-04 17:51:14 -0800 | [diff] [blame] | 458 | u8 *opt; |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 459 | |
| 460 | if (!dev->addr_len) |
| 461 | llinfo = 0; |
| 462 | |
| 463 | len = sizeof(struct icmp6hdr) + (target ? sizeof(*target) : 0); |
| 464 | if (llinfo) |
| 465 | len += ndisc_opt_addr_space(dev); |
| 466 | |
| 467 | skb = sock_alloc_send_skb(sk, |
| 468 | (MAX_HEADER + sizeof(struct ipv6hdr) + |
Johannes Berg | f5184d2 | 2008-05-12 20:48:31 -0700 | [diff] [blame] | 469 | len + LL_ALLOCATED_SPACE(dev)), |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 470 | 1, &err); |
| 471 | if (!skb) { |
| 472 | ND_PRINTK0(KERN_ERR |
Brian Haley | dae9de8 | 2009-06-02 00:20:26 -0700 | [diff] [blame] | 473 | "ICMPv6 ND: %s() failed to allocate an skb, err=%d.\n", |
| 474 | __func__, err); |
Brian Haley | 305d552 | 2008-11-04 17:51:14 -0800 | [diff] [blame] | 475 | return NULL; |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | skb_reserve(skb, LL_RESERVED_SPACE(dev)); |
| 479 | ip6_nd_hdr(sk, skb, dev, saddr, daddr, IPPROTO_ICMPV6, len); |
| 480 | |
| 481 | skb->transport_header = skb->tail; |
| 482 | skb_put(skb, len); |
| 483 | |
| 484 | hdr = (struct icmp6hdr *)skb_transport_header(skb); |
| 485 | memcpy(hdr, icmp6h, sizeof(*hdr)); |
| 486 | |
| 487 | opt = skb_transport_header(skb) + sizeof(struct icmp6hdr); |
| 488 | if (target) { |
| 489 | ipv6_addr_copy((struct in6_addr *)opt, target); |
| 490 | opt += sizeof(*target); |
| 491 | } |
| 492 | |
| 493 | if (llinfo) |
| 494 | ndisc_fill_addr_option(opt, llinfo, dev->dev_addr, |
| 495 | dev->addr_len, dev->type); |
| 496 | |
| 497 | hdr->icmp6_cksum = csum_ipv6_magic(saddr, daddr, len, |
| 498 | IPPROTO_ICMPV6, |
Joe Perches | 07f0757 | 2008-11-19 15:44:53 -0800 | [diff] [blame] | 499 | csum_partial(hdr, |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 500 | len, 0)); |
| 501 | |
Brian Haley | 305d552 | 2008-11-04 17:51:14 -0800 | [diff] [blame] | 502 | return skb; |
| 503 | } |
| 504 | |
| 505 | EXPORT_SYMBOL(ndisc_build_skb); |
| 506 | |
| 507 | void ndisc_send_skb(struct sk_buff *skb, |
| 508 | struct net_device *dev, |
| 509 | struct neighbour *neigh, |
| 510 | const struct in6_addr *daddr, |
| 511 | const struct in6_addr *saddr, |
| 512 | struct icmp6hdr *icmp6h) |
| 513 | { |
| 514 | struct flowi fl; |
| 515 | struct dst_entry *dst; |
| 516 | struct net *net = dev_net(dev); |
| 517 | struct sock *sk = net->ipv6.ndisc_sk; |
| 518 | struct inet6_dev *idev; |
| 519 | int err; |
| 520 | u8 type; |
| 521 | |
| 522 | type = icmp6h->icmp6_type; |
| 523 | |
| 524 | icmpv6_flow_init(sk, &fl, type, saddr, daddr, dev->ifindex); |
| 525 | |
| 526 | dst = icmp6_dst_alloc(dev, neigh, daddr); |
| 527 | if (!dst) { |
| 528 | kfree_skb(skb); |
| 529 | return; |
| 530 | } |
| 531 | |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 532 | err = xfrm_lookup(net, &dst, &fl, NULL, 0); |
Brian Haley | 305d552 | 2008-11-04 17:51:14 -0800 | [diff] [blame] | 533 | if (err < 0) { |
| 534 | kfree_skb(skb); |
| 535 | return; |
| 536 | } |
| 537 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 538 | skb_dst_set(skb, dst); |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 539 | |
| 540 | idev = in6_dev_get(dst->dev); |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 541 | IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len); |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 542 | |
Jan Engelhardt | b2e0b38 | 2010-03-23 04:09:07 +0100 | [diff] [blame] | 543 | err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, dst->dev, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 544 | dst_output); |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 545 | if (!err) { |
Denis V. Lunev | 5c5d244 | 2008-10-08 10:33:50 -0700 | [diff] [blame] | 546 | ICMP6MSGOUT_INC_STATS(net, idev, type); |
Denis V. Lunev | a862f6a | 2008-10-08 10:33:06 -0700 | [diff] [blame] | 547 | ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS); |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | if (likely(idev != NULL)) |
| 551 | in6_dev_put(idev); |
| 552 | } |
| 553 | |
Brian Haley | 305d552 | 2008-11-04 17:51:14 -0800 | [diff] [blame] | 554 | EXPORT_SYMBOL(ndisc_send_skb); |
| 555 | |
| 556 | /* |
| 557 | * Send a Neighbour Discover packet |
| 558 | */ |
| 559 | static void __ndisc_send(struct net_device *dev, |
| 560 | struct neighbour *neigh, |
| 561 | const struct in6_addr *daddr, |
| 562 | const struct in6_addr *saddr, |
| 563 | struct icmp6hdr *icmp6h, const struct in6_addr *target, |
| 564 | int llinfo) |
| 565 | { |
| 566 | struct sk_buff *skb; |
| 567 | |
| 568 | skb = ndisc_build_skb(dev, daddr, saddr, icmp6h, target, llinfo); |
| 569 | if (!skb) |
| 570 | return; |
| 571 | |
| 572 | ndisc_send_skb(skb, dev, neigh, daddr, saddr, icmp6h); |
| 573 | } |
| 574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | static void ndisc_send_na(struct net_device *dev, struct neighbour *neigh, |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 576 | const struct in6_addr *daddr, |
| 577 | const struct in6_addr *solicited_addr, |
| 578 | int router, int solicited, int override, int inc_opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
| 580 | struct in6_addr tmpaddr; |
| 581 | struct inet6_ifaddr *ifp; |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 582 | const struct in6_addr *src_addr; |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 583 | struct icmp6hdr icmp6h = { |
| 584 | .icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT, |
| 585 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
| 587 | /* for anycast or proxy, solicited_addr != src_addr */ |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 588 | ifp = ipv6_get_ifaddr(dev_net(dev), solicited_addr, dev, 1); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 589 | if (ifp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | src_addr = solicited_addr; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 591 | if (ifp->flags & IFA_F_OPTIMISTIC) |
| 592 | override = 0; |
stephen hemminger | 9f88816 | 2010-06-21 11:00:13 +0000 | [diff] [blame] | 593 | inc_opt |= ifp->idev->cnf.force_tllao; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | in6_ifa_put(ifp); |
| 595 | } else { |
Brian Haley | 191cd58 | 2008-08-14 15:33:21 -0700 | [diff] [blame] | 596 | if (ipv6_dev_get_saddr(dev_net(dev), dev, daddr, |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 597 | inet6_sk(dev_net(dev)->ipv6.ndisc_sk)->srcprefs, |
YOSHIFUJI Hideaki | 7cbca67 | 2008-03-25 09:37:42 +0900 | [diff] [blame] | 598 | &tmpaddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | return; |
| 600 | src_addr = &tmpaddr; |
| 601 | } |
| 602 | |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 603 | icmp6h.icmp6_router = router; |
| 604 | icmp6h.icmp6_solicited = solicited; |
| 605 | icmp6h.icmp6_override = override; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 607 | __ndisc_send(dev, neigh, daddr, src_addr, |
| 608 | &icmp6h, solicited_addr, |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 609 | inc_opt ? ND_OPT_TARGET_LL_ADDR : 0); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 610 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
| 612 | void ndisc_send_ns(struct net_device *dev, struct neighbour *neigh, |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 613 | const struct in6_addr *solicit, |
| 614 | const struct in6_addr *daddr, const struct in6_addr *saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | struct in6_addr addr_buf; |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 617 | struct icmp6hdr icmp6h = { |
| 618 | .icmp6_type = NDISC_NEIGHBOUR_SOLICITATION, |
| 619 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
| 621 | if (saddr == NULL) { |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 622 | if (ipv6_get_lladdr(dev, &addr_buf, |
| 623 | (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | return; |
| 625 | saddr = &addr_buf; |
| 626 | } |
| 627 | |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 628 | __ndisc_send(dev, neigh, daddr, saddr, |
| 629 | &icmp6h, solicit, |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 630 | !ipv6_addr_any(saddr) ? ND_OPT_SOURCE_LL_ADDR : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 633 | void ndisc_send_rs(struct net_device *dev, const struct in6_addr *saddr, |
| 634 | const struct in6_addr *daddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | { |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 636 | struct icmp6hdr icmp6h = { |
| 637 | .icmp6_type = NDISC_ROUTER_SOLICITATION, |
| 638 | }; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 639 | int send_sllao = dev->addr_len; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 640 | |
| 641 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
| 642 | /* |
| 643 | * According to section 2.2 of RFC 4429, we must not |
| 644 | * send router solicitations with a sllao from |
| 645 | * optimistic addresses, but we may send the solicitation |
| 646 | * if we don't include the sllao. So here we check |
| 647 | * if our address is optimistic, and if so, we |
Joe Perches | bea8519 | 2007-12-20 14:01:35 -0800 | [diff] [blame] | 648 | * suppress the inclusion of the sllao. |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 649 | */ |
| 650 | if (send_sllao) { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 651 | struct inet6_ifaddr *ifp = ipv6_get_ifaddr(dev_net(dev), saddr, |
Daniel Lezcano | 1cab3da | 2008-01-10 22:44:09 -0800 | [diff] [blame] | 652 | dev, 1); |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 653 | if (ifp) { |
| 654 | if (ifp->flags & IFA_F_OPTIMISTIC) { |
YOSHIFUJI Hideaki | ca04356 | 2007-02-28 23:13:20 +0900 | [diff] [blame] | 655 | send_sllao = 0; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 656 | } |
YOSHIFUJI Hideaki | ca04356 | 2007-02-28 23:13:20 +0900 | [diff] [blame] | 657 | in6_ifa_put(ifp); |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 658 | } else { |
| 659 | send_sllao = 0; |
| 660 | } |
| 661 | } |
| 662 | #endif |
YOSHIFUJI Hideaki | e1ec784 | 2007-04-24 20:44:52 +0900 | [diff] [blame] | 663 | __ndisc_send(dev, NULL, daddr, saddr, |
| 664 | &icmp6h, NULL, |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 665 | send_sllao ? ND_OPT_SOURCE_LL_ADDR : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
| 669 | static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb) |
| 670 | { |
| 671 | /* |
| 672 | * "The sender MUST return an ICMP |
| 673 | * destination unreachable" |
| 674 | */ |
| 675 | dst_link_failure(skb); |
| 676 | kfree_skb(skb); |
| 677 | } |
| 678 | |
| 679 | /* Called with locked neigh: either read or both */ |
| 680 | |
| 681 | static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb) |
| 682 | { |
| 683 | struct in6_addr *saddr = NULL; |
| 684 | struct in6_addr mcaddr; |
| 685 | struct net_device *dev = neigh->dev; |
| 686 | struct in6_addr *target = (struct in6_addr *)&neigh->primary_key; |
| 687 | int probes = atomic_read(&neigh->probes); |
| 688 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 689 | if (skb && ipv6_chk_addr(dev_net(dev), &ipv6_hdr(skb)->saddr, dev, 1)) |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 690 | saddr = &ipv6_hdr(skb)->saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
| 692 | if ((probes -= neigh->parms->ucast_probes) < 0) { |
| 693 | if (!(neigh->nud_state & NUD_VALID)) { |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 694 | ND_PRINTK1(KERN_DEBUG "%s(): trying to ucast probe in NUD_INVALID: %pI6\n", |
Harvey Harrison | 0c6ce78 | 2008-10-28 16:09:23 -0700 | [diff] [blame] | 695 | __func__, target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
| 697 | ndisc_send_ns(dev, neigh, target, target, saddr); |
| 698 | } else if ((probes -= neigh->parms->app_probes) < 0) { |
| 699 | #ifdef CONFIG_ARPD |
| 700 | neigh_app_ns(neigh); |
| 701 | #endif |
| 702 | } else { |
| 703 | addrconf_addr_solict_mult(target, &mcaddr); |
| 704 | ndisc_send_ns(dev, NULL, target, &mcaddr, saddr); |
| 705 | } |
| 706 | } |
| 707 | |
YOSHIFUJI Hideaki | 0736ffc | 2008-03-28 13:37:58 +0900 | [diff] [blame] | 708 | static int pndisc_is_router(const void *pkey, |
| 709 | struct net_device *dev) |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 710 | { |
| 711 | struct pneigh_entry *n; |
YOSHIFUJI Hideaki | 0736ffc | 2008-03-28 13:37:58 +0900 | [diff] [blame] | 712 | int ret = -1; |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 713 | |
| 714 | read_lock_bh(&nd_tbl.lock); |
YOSHIFUJI Hideaki | 0736ffc | 2008-03-28 13:37:58 +0900 | [diff] [blame] | 715 | n = __pneigh_lookup(&nd_tbl, dev_net(dev), pkey, dev); |
| 716 | if (n) |
| 717 | ret = !!(n->flags & NTF_ROUTER); |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 718 | read_unlock_bh(&nd_tbl.lock); |
| 719 | |
YOSHIFUJI Hideaki | 0736ffc | 2008-03-28 13:37:58 +0900 | [diff] [blame] | 720 | return ret; |
Pavel Emelyanov | fa86d32 | 2008-03-24 14:48:59 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | static void ndisc_recv_ns(struct sk_buff *skb) |
| 724 | { |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 725 | struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 726 | struct in6_addr *saddr = &ipv6_hdr(skb)->saddr; |
| 727 | struct in6_addr *daddr = &ipv6_hdr(skb)->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | u8 *lladdr = NULL; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 729 | u32 ndoptlen = skb->tail - (skb->transport_header + |
| 730 | offsetof(struct nd_msg, opt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | struct ndisc_options ndopts; |
| 732 | struct net_device *dev = skb->dev; |
| 733 | struct inet6_ifaddr *ifp; |
| 734 | struct inet6_dev *idev = NULL; |
| 735 | struct neighbour *neigh; |
| 736 | int dad = ipv6_addr_any(saddr); |
| 737 | int inc; |
YOSHIFUJI Hideaki | 0736ffc | 2008-03-28 13:37:58 +0900 | [diff] [blame] | 738 | int is_router = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
| 740 | if (ipv6_addr_is_multicast(&msg->target)) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 741 | ND_PRINTK2(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | "ICMPv6 NS: multicast target address"); |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * RFC2461 7.1.1: |
| 748 | * DAD has to be destined for solicited node multicast address. |
| 749 | */ |
| 750 | if (dad && |
| 751 | !(daddr->s6_addr32[0] == htonl(0xff020000) && |
| 752 | daddr->s6_addr32[1] == htonl(0x00000000) && |
| 753 | daddr->s6_addr32[2] == htonl(0x00000001) && |
| 754 | daddr->s6_addr [12] == 0xff )) { |
| 755 | ND_PRINTK2(KERN_WARNING |
| 756 | "ICMPv6 NS: bad DAD packet (wrong destination)\n"); |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | if (!ndisc_parse_options(msg->opt, ndoptlen, &ndopts)) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 761 | ND_PRINTK2(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | "ICMPv6 NS: invalid ND options\n"); |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | if (ndopts.nd_opts_src_lladdr) { |
| 767 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr, dev); |
| 768 | if (!lladdr) { |
| 769 | ND_PRINTK2(KERN_WARNING |
| 770 | "ICMPv6 NS: invalid link-layer address length\n"); |
| 771 | return; |
| 772 | } |
| 773 | |
| 774 | /* RFC2461 7.1.1: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 775 | * If the IP source address is the unspecified address, |
| 776 | * there MUST NOT be source link-layer address option |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | * in the message. |
| 778 | */ |
| 779 | if (dad) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 780 | ND_PRINTK2(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | "ICMPv6 NS: bad DAD packet (link-layer address option)\n"); |
| 782 | return; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | inc = ipv6_addr_is_multicast(daddr); |
| 787 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 788 | ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1); |
Daniel Lezcano | a18bc69 | 2008-03-07 11:14:49 -0800 | [diff] [blame] | 789 | if (ifp) { |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 790 | |
| 791 | if (ifp->flags & (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)) { |
| 792 | if (dad) { |
| 793 | if (dev->type == ARPHRD_IEEE802_TR) { |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 794 | const unsigned char *sadr; |
| 795 | sadr = skb_mac_header(skb); |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 796 | if (((sadr[8] ^ dev->dev_addr[0]) & 0x7f) == 0 && |
| 797 | sadr[9] == dev->dev_addr[1] && |
| 798 | sadr[10] == dev->dev_addr[2] && |
| 799 | sadr[11] == dev->dev_addr[3] && |
| 800 | sadr[12] == dev->dev_addr[4] && |
| 801 | sadr[13] == dev->dev_addr[5]) { |
| 802 | /* looped-back to us */ |
| 803 | goto out; |
| 804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | } |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 806 | |
| 807 | /* |
| 808 | * We are colliding with another node |
| 809 | * who is doing DAD |
| 810 | * so fail our DAD process |
| 811 | */ |
| 812 | addrconf_dad_failure(ifp); |
Denis V. Lunev | 9e3be4b | 2007-09-11 11:04:49 +0200 | [diff] [blame] | 813 | return; |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 814 | } else { |
| 815 | /* |
| 816 | * This is not a dad solicitation. |
| 817 | * If we are an optimistic node, |
| 818 | * we should respond. |
| 819 | * Otherwise, we should ignore it. |
| 820 | */ |
| 821 | if (!(ifp->flags & IFA_F_OPTIMISTIC)) |
| 822 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | idev = ifp->idev; |
| 827 | } else { |
YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 828 | struct net *net = dev_net(dev); |
| 829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | idev = in6_dev_get(dev); |
| 831 | if (!idev) { |
| 832 | /* XXX: count this drop? */ |
| 833 | return; |
| 834 | } |
| 835 | |
YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 836 | if (ipv6_chk_acast_addr(net, dev, &msg->target) || |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 837 | (idev->cnf.forwarding && |
YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 838 | (net->ipv6.devconf_all->proxy_ndp || idev->cnf.proxy_ndp) && |
YOSHIFUJI Hideaki | 0736ffc | 2008-03-28 13:37:58 +0900 | [diff] [blame] | 839 | (is_router = pndisc_is_router(&msg->target, dev)) >= 0)) { |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 840 | if (!(NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | skb->pkt_type != PACKET_HOST && |
| 842 | inc != 0 && |
| 843 | idev->nd_parms->proxy_delay != 0) { |
| 844 | /* |
| 845 | * for anycast or proxy, |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 846 | * sender should delay its response |
| 847 | * by a random time between 0 and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | * MAX_ANYCAST_DELAY_TIME seconds. |
| 849 | * (RFC2461) -- yoshfuji |
| 850 | */ |
| 851 | struct sk_buff *n = skb_clone(skb, GFP_ATOMIC); |
| 852 | if (n) |
| 853 | pneigh_enqueue(&nd_tbl, idev->nd_parms, n); |
| 854 | goto out; |
| 855 | } |
| 856 | } else |
| 857 | goto out; |
| 858 | } |
| 859 | |
YOSHIFUJI Hideaki | 0736ffc | 2008-03-28 13:37:58 +0900 | [diff] [blame] | 860 | if (is_router < 0) |
| 861 | is_router = !!idev->cnf.forwarding; |
Ville Nuorvala | 62dd931 | 2006-09-22 14:43:19 -0700 | [diff] [blame] | 862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | if (dad) { |
YOSHIFUJI Hideaki | f3ee401 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 864 | ndisc_send_na(dev, NULL, &in6addr_linklocal_allnodes, &msg->target, |
Ville Nuorvala | 62dd931 | 2006-09-22 14:43:19 -0700 | [diff] [blame] | 865 | is_router, 0, (ifp != NULL), 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | goto out; |
| 867 | } |
| 868 | |
| 869 | if (inc) |
| 870 | NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_mcast); |
| 871 | else |
| 872 | NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_ucast); |
| 873 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 874 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | * update / create cache entry |
| 876 | * for the source address |
| 877 | */ |
| 878 | neigh = __neigh_lookup(&nd_tbl, saddr, dev, |
| 879 | !inc || lladdr || !dev->addr_len); |
| 880 | if (neigh) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 881 | neigh_update(neigh, lladdr, NUD_STALE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
| 883 | NEIGH_UPDATE_F_OVERRIDE); |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 884 | if (neigh || !dev->header_ops) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | ndisc_send_na(dev, neigh, saddr, &msg->target, |
Ville Nuorvala | 62dd931 | 2006-09-22 14:43:19 -0700 | [diff] [blame] | 886 | is_router, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | 1, (ifp != NULL && inc), inc); |
| 888 | if (neigh) |
| 889 | neigh_release(neigh); |
| 890 | } |
| 891 | |
| 892 | out: |
| 893 | if (ifp) |
| 894 | in6_ifa_put(ifp); |
| 895 | else |
| 896 | in6_dev_put(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | static void ndisc_recv_na(struct sk_buff *skb) |
| 900 | { |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 901 | struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 902 | struct in6_addr *saddr = &ipv6_hdr(skb)->saddr; |
| 903 | struct in6_addr *daddr = &ipv6_hdr(skb)->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | u8 *lladdr = NULL; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 905 | u32 ndoptlen = skb->tail - (skb->transport_header + |
| 906 | offsetof(struct nd_msg, opt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | struct ndisc_options ndopts; |
| 908 | struct net_device *dev = skb->dev; |
| 909 | struct inet6_ifaddr *ifp; |
| 910 | struct neighbour *neigh; |
| 911 | |
| 912 | if (skb->len < sizeof(struct nd_msg)) { |
| 913 | ND_PRINTK2(KERN_WARNING |
| 914 | "ICMPv6 NA: packet too short\n"); |
| 915 | return; |
| 916 | } |
| 917 | |
| 918 | if (ipv6_addr_is_multicast(&msg->target)) { |
| 919 | ND_PRINTK2(KERN_WARNING |
| 920 | "ICMPv6 NA: target address is multicast.\n"); |
| 921 | return; |
| 922 | } |
| 923 | |
| 924 | if (ipv6_addr_is_multicast(daddr) && |
| 925 | msg->icmph.icmp6_solicited) { |
| 926 | ND_PRINTK2(KERN_WARNING |
| 927 | "ICMPv6 NA: solicited NA is multicasted.\n"); |
| 928 | return; |
| 929 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | if (!ndisc_parse_options(msg->opt, ndoptlen, &ndopts)) { |
| 932 | ND_PRINTK2(KERN_WARNING |
| 933 | "ICMPv6 NS: invalid ND option\n"); |
| 934 | return; |
| 935 | } |
| 936 | if (ndopts.nd_opts_tgt_lladdr) { |
| 937 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, dev); |
| 938 | if (!lladdr) { |
| 939 | ND_PRINTK2(KERN_WARNING |
| 940 | "ICMPv6 NA: invalid link-layer address length\n"); |
| 941 | return; |
| 942 | } |
| 943 | } |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 944 | ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1); |
Daniel Lezcano | a18bc69 | 2008-03-07 11:14:49 -0800 | [diff] [blame] | 945 | if (ifp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | if (ifp->flags & IFA_F_TENTATIVE) { |
| 947 | addrconf_dad_failure(ifp); |
| 948 | return; |
| 949 | } |
| 950 | /* What should we make now? The advertisement |
| 951 | is invalid, but ndisc specs say nothing |
| 952 | about it. It could be misconfiguration, or |
| 953 | an smart proxy agent tries to help us :-) |
Jan Sembera | 24fc7b8 | 2008-12-09 15:48:32 -0800 | [diff] [blame] | 954 | |
| 955 | We should not print the error if NA has been |
| 956 | received from loopback - it is just our own |
| 957 | unsolicited advertisement. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | */ |
Jan Sembera | 24fc7b8 | 2008-12-09 15:48:32 -0800 | [diff] [blame] | 959 | if (skb->pkt_type != PACKET_LOOPBACK) |
| 960 | ND_PRINTK1(KERN_WARNING |
Jens Rosenboom | a6fa328 | 2009-08-12 22:16:04 +0000 | [diff] [blame] | 961 | "ICMPv6 NA: someone advertises our address %pI6 on %s!\n", |
| 962 | &ifp->addr, ifp->idev->dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | in6_ifa_put(ifp); |
| 964 | return; |
| 965 | } |
| 966 | neigh = neigh_lookup(&nd_tbl, &msg->target, dev); |
| 967 | |
| 968 | if (neigh) { |
| 969 | u8 old_flags = neigh->flags; |
YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 970 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | |
| 972 | if (neigh->nud_state & NUD_FAILED) |
| 973 | goto out; |
| 974 | |
Ville Nuorvala | 5f3e6e9 | 2006-09-22 14:42:46 -0700 | [diff] [blame] | 975 | /* |
| 976 | * Don't update the neighbor cache entry on a proxy NA from |
| 977 | * ourselves because either the proxied node is off link or it |
| 978 | * has already sent a NA to us. |
| 979 | */ |
| 980 | if (lladdr && !memcmp(lladdr, dev->dev_addr, dev->addr_len) && |
YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 981 | net->ipv6.devconf_all->forwarding && net->ipv6.devconf_all->proxy_ndp && |
| 982 | pneigh_lookup(&nd_tbl, net, &msg->target, dev, 0)) { |
YOSHIFUJI Hideaki | fbea49e | 2006-09-22 14:43:49 -0700 | [diff] [blame] | 983 | /* XXX: idev->cnf.prixy_ndp */ |
Ville Nuorvala | 5f3e6e9 | 2006-09-22 14:42:46 -0700 | [diff] [blame] | 984 | goto out; |
YOSHIFUJI Hideaki | fbea49e | 2006-09-22 14:43:49 -0700 | [diff] [blame] | 985 | } |
Ville Nuorvala | 5f3e6e9 | 2006-09-22 14:42:46 -0700 | [diff] [blame] | 986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | neigh_update(neigh, lladdr, |
| 988 | msg->icmph.icmp6_solicited ? NUD_REACHABLE : NUD_STALE, |
| 989 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
| 990 | (msg->icmph.icmp6_override ? NEIGH_UPDATE_F_OVERRIDE : 0)| |
| 991 | NEIGH_UPDATE_F_OVERRIDE_ISROUTER| |
| 992 | (msg->icmph.icmp6_router ? NEIGH_UPDATE_F_ISROUTER : 0)); |
| 993 | |
| 994 | if ((old_flags & ~neigh->flags) & NTF_ROUTER) { |
| 995 | /* |
| 996 | * Change: router to host |
| 997 | */ |
| 998 | struct rt6_info *rt; |
| 999 | rt = rt6_get_dflt_router(saddr, dev); |
| 1000 | if (rt) |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 1001 | ip6_del_rt(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | out: |
| 1005 | neigh_release(neigh); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | static void ndisc_recv_rs(struct sk_buff *skb) |
| 1010 | { |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1011 | struct rs_msg *rs_msg = (struct rs_msg *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | unsigned long ndoptlen = skb->len - sizeof(*rs_msg); |
| 1013 | struct neighbour *neigh; |
| 1014 | struct inet6_dev *idev; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1015 | struct in6_addr *saddr = &ipv6_hdr(skb)->saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | struct ndisc_options ndopts; |
| 1017 | u8 *lladdr = NULL; |
| 1018 | |
| 1019 | if (skb->len < sizeof(*rs_msg)) |
| 1020 | return; |
| 1021 | |
| 1022 | idev = in6_dev_get(skb->dev); |
| 1023 | if (!idev) { |
| 1024 | if (net_ratelimit()) |
| 1025 | ND_PRINTK1("ICMP6 RS: can't find in6 device\n"); |
| 1026 | return; |
| 1027 | } |
| 1028 | |
| 1029 | /* Don't accept RS if we're not in router mode */ |
| 1030 | if (!idev->cnf.forwarding) |
| 1031 | goto out; |
| 1032 | |
| 1033 | /* |
| 1034 | * Don't update NCE if src = ::; |
| 1035 | * this implies that the source node has no ip address assigned yet. |
| 1036 | */ |
| 1037 | if (ipv6_addr_any(saddr)) |
| 1038 | goto out; |
| 1039 | |
| 1040 | /* Parse ND options */ |
| 1041 | if (!ndisc_parse_options(rs_msg->opt, ndoptlen, &ndopts)) { |
| 1042 | if (net_ratelimit()) |
| 1043 | ND_PRINTK2("ICMP6 NS: invalid ND option, ignored\n"); |
| 1044 | goto out; |
| 1045 | } |
| 1046 | |
| 1047 | if (ndopts.nd_opts_src_lladdr) { |
| 1048 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr, |
| 1049 | skb->dev); |
| 1050 | if (!lladdr) |
| 1051 | goto out; |
| 1052 | } |
| 1053 | |
| 1054 | neigh = __neigh_lookup(&nd_tbl, saddr, skb->dev, 1); |
| 1055 | if (neigh) { |
| 1056 | neigh_update(neigh, lladdr, NUD_STALE, |
| 1057 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
| 1058 | NEIGH_UPDATE_F_OVERRIDE| |
| 1059 | NEIGH_UPDATE_F_OVERRIDE_ISROUTER); |
| 1060 | neigh_release(neigh); |
| 1061 | } |
| 1062 | out: |
| 1063 | in6_dev_put(idev); |
| 1064 | } |
| 1065 | |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 1066 | static void ndisc_ra_useropt(struct sk_buff *ra, struct nd_opt_hdr *opt) |
| 1067 | { |
| 1068 | struct icmp6hdr *icmp6h = (struct icmp6hdr *)skb_transport_header(ra); |
| 1069 | struct sk_buff *skb; |
| 1070 | struct nlmsghdr *nlh; |
| 1071 | struct nduseroptmsg *ndmsg; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1072 | struct net *net = dev_net(ra->dev); |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 1073 | int err; |
| 1074 | int base_size = NLMSG_ALIGN(sizeof(struct nduseroptmsg) |
| 1075 | + (opt->nd_opt_len << 3)); |
| 1076 | size_t msg_size = base_size + nla_total_size(sizeof(struct in6_addr)); |
| 1077 | |
| 1078 | skb = nlmsg_new(msg_size, GFP_ATOMIC); |
| 1079 | if (skb == NULL) { |
| 1080 | err = -ENOBUFS; |
| 1081 | goto errout; |
| 1082 | } |
| 1083 | |
| 1084 | nlh = nlmsg_put(skb, 0, 0, RTM_NEWNDUSEROPT, base_size, 0); |
| 1085 | if (nlh == NULL) { |
| 1086 | goto nla_put_failure; |
| 1087 | } |
| 1088 | |
| 1089 | ndmsg = nlmsg_data(nlh); |
| 1090 | ndmsg->nduseropt_family = AF_INET6; |
Pierre Ynard | dbb2ed2 | 2007-11-12 17:58:35 -0800 | [diff] [blame] | 1091 | ndmsg->nduseropt_ifindex = ra->dev->ifindex; |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 1092 | ndmsg->nduseropt_icmp_type = icmp6h->icmp6_type; |
| 1093 | ndmsg->nduseropt_icmp_code = icmp6h->icmp6_code; |
| 1094 | ndmsg->nduseropt_opts_len = opt->nd_opt_len << 3; |
| 1095 | |
| 1096 | memcpy(ndmsg + 1, opt, opt->nd_opt_len << 3); |
| 1097 | |
| 1098 | NLA_PUT(skb, NDUSEROPT_SRCADDR, sizeof(struct in6_addr), |
| 1099 | &ipv6_hdr(ra)->saddr); |
| 1100 | nlmsg_end(skb, nlh); |
| 1101 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 1102 | rtnl_notify(skb, net, 0, RTNLGRP_ND_USEROPT, NULL, GFP_ATOMIC); |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 1103 | return; |
| 1104 | |
| 1105 | nla_put_failure: |
| 1106 | nlmsg_free(skb); |
| 1107 | err = -EMSGSIZE; |
| 1108 | errout: |
Daniel Lezcano | a18bc69 | 2008-03-07 11:14:49 -0800 | [diff] [blame] | 1109 | rtnl_set_sk_err(net, RTNLGRP_ND_USEROPT, err); |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Thomas Graf | 65e9b62 | 2010-09-03 02:59:14 +0000 | [diff] [blame] | 1112 | static inline int accept_ra(struct inet6_dev *in6_dev) |
| 1113 | { |
| 1114 | /* |
| 1115 | * If forwarding is enabled, RA are not accepted unless the special |
| 1116 | * hybrid mode (accept_ra=2) is enabled. |
| 1117 | */ |
| 1118 | if (in6_dev->cnf.forwarding && in6_dev->cnf.accept_ra < 2) |
| 1119 | return 0; |
| 1120 | |
| 1121 | return in6_dev->cnf.accept_ra; |
| 1122 | } |
| 1123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | static void ndisc_router_discovery(struct sk_buff *skb) |
| 1125 | { |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1126 | struct ra_msg *ra_msg = (struct ra_msg *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | struct neighbour *neigh = NULL; |
| 1128 | struct inet6_dev *in6_dev; |
YOSHIFUJI Hideaki | 65f5c7c | 2006-03-20 16:55:08 -0800 | [diff] [blame] | 1129 | struct rt6_info *rt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | int lifetime; |
| 1131 | struct ndisc_options ndopts; |
| 1132 | int optlen; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 1133 | unsigned int pref = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | |
| 1135 | __u8 * opt = (__u8 *)(ra_msg + 1); |
| 1136 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1137 | optlen = (skb->tail - skb->transport_header) - sizeof(struct ra_msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1139 | if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | ND_PRINTK2(KERN_WARNING |
| 1141 | "ICMPv6 RA: source address is not link-local.\n"); |
| 1142 | return; |
| 1143 | } |
| 1144 | if (optlen < 0) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1145 | ND_PRINTK2(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | "ICMPv6 RA: packet too short\n"); |
| 1147 | return; |
| 1148 | } |
| 1149 | |
YOSHIFUJI Hideaki | de357cc | 2008-03-15 23:59:18 -0400 | [diff] [blame] | 1150 | #ifdef CONFIG_IPV6_NDISC_NODETYPE |
Templin, Fred L | fadf6bf | 2008-03-11 18:35:59 -0400 | [diff] [blame] | 1151 | if (skb->ndisc_nodetype == NDISC_NODETYPE_HOST) { |
| 1152 | ND_PRINTK2(KERN_WARNING |
| 1153 | "ICMPv6 RA: from host or unauthorized router\n"); |
| 1154 | return; |
| 1155 | } |
YOSHIFUJI Hideaki | de357cc | 2008-03-15 23:59:18 -0400 | [diff] [blame] | 1156 | #endif |
Templin, Fred L | fadf6bf | 2008-03-11 18:35:59 -0400 | [diff] [blame] | 1157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | /* |
| 1159 | * set the RA_RECV flag in the interface |
| 1160 | */ |
| 1161 | |
| 1162 | in6_dev = in6_dev_get(skb->dev); |
| 1163 | if (in6_dev == NULL) { |
| 1164 | ND_PRINTK0(KERN_ERR |
| 1165 | "ICMPv6 RA: can't find inet6 device for %s.\n", |
| 1166 | skb->dev->name); |
| 1167 | return; |
| 1168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | |
| 1170 | if (!ndisc_parse_options(opt, optlen, &ndopts)) { |
| 1171 | in6_dev_put(in6_dev); |
| 1172 | ND_PRINTK2(KERN_WARNING |
| 1173 | "ICMP6 RA: invalid ND options\n"); |
| 1174 | return; |
| 1175 | } |
| 1176 | |
Thomas Graf | 65e9b62 | 2010-09-03 02:59:14 +0000 | [diff] [blame] | 1177 | if (!accept_ra(in6_dev)) |
David Ward | 31ce8c7 | 2009-08-29 00:04:09 -0700 | [diff] [blame] | 1178 | goto skip_linkparms; |
| 1179 | |
YOSHIFUJI Hideaki | de357cc | 2008-03-15 23:59:18 -0400 | [diff] [blame] | 1180 | #ifdef CONFIG_IPV6_NDISC_NODETYPE |
Templin, Fred L | fadf6bf | 2008-03-11 18:35:59 -0400 | [diff] [blame] | 1181 | /* skip link-specific parameters from interior routers */ |
| 1182 | if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) |
| 1183 | goto skip_linkparms; |
YOSHIFUJI Hideaki | de357cc | 2008-03-15 23:59:18 -0400 | [diff] [blame] | 1184 | #endif |
Templin, Fred L | fadf6bf | 2008-03-11 18:35:59 -0400 | [diff] [blame] | 1185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | if (in6_dev->if_flags & IF_RS_SENT) { |
| 1187 | /* |
| 1188 | * flag that an RA was received after an RS was sent |
| 1189 | * out on this interface. |
| 1190 | */ |
| 1191 | in6_dev->if_flags |= IF_RA_RCVD; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * Remember the managed/otherconf flags from most recently |
| 1196 | * received RA message (RFC 2462) -- yoshfuji |
| 1197 | */ |
| 1198 | in6_dev->if_flags = (in6_dev->if_flags & ~(IF_RA_MANAGED | |
| 1199 | IF_RA_OTHERCONF)) | |
| 1200 | (ra_msg->icmph.icmp6_addrconf_managed ? |
| 1201 | IF_RA_MANAGED : 0) | |
| 1202 | (ra_msg->icmph.icmp6_addrconf_other ? |
| 1203 | IF_RA_OTHERCONF : 0); |
| 1204 | |
YOSHIFUJI Hideaki | 65f5c7c | 2006-03-20 16:55:08 -0800 | [diff] [blame] | 1205 | if (!in6_dev->cnf.accept_ra_defrtr) |
| 1206 | goto skip_defrtr; |
| 1207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | lifetime = ntohs(ra_msg->icmph.icmp6_rt_lifetime); |
| 1209 | |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 1210 | #ifdef CONFIG_IPV6_ROUTER_PREF |
| 1211 | pref = ra_msg->icmph.icmp6_router_pref; |
| 1212 | /* 10b is handled as if it were 00b (medium) */ |
YOSHIFUJI Hideaki | 930d6ff | 2006-03-20 17:05:30 -0800 | [diff] [blame] | 1213 | if (pref == ICMPV6_ROUTER_PREF_INVALID || |
YOSHIFUJI Hideaki | 6d5b78c | 2007-06-22 16:07:04 -0700 | [diff] [blame] | 1214 | !in6_dev->cnf.accept_ra_rtr_pref) |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 1215 | pref = ICMPV6_ROUTER_PREF_MEDIUM; |
| 1216 | #endif |
| 1217 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1218 | rt = rt6_get_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | |
| 1220 | if (rt) |
| 1221 | neigh = rt->rt6i_nexthop; |
| 1222 | |
| 1223 | if (rt && lifetime == 0) { |
| 1224 | neigh_clone(neigh); |
Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 1225 | ip6_del_rt(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | rt = NULL; |
| 1227 | } |
| 1228 | |
| 1229 | if (rt == NULL && lifetime) { |
| 1230 | ND_PRINTK3(KERN_DEBUG |
| 1231 | "ICMPv6 RA: adding default router.\n"); |
| 1232 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1233 | rt = rt6_add_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev, pref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | if (rt == NULL) { |
| 1235 | ND_PRINTK0(KERN_ERR |
| 1236 | "ICMPv6 RA: %s() failed to add default route.\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 1237 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | in6_dev_put(in6_dev); |
| 1239 | return; |
| 1240 | } |
| 1241 | |
| 1242 | neigh = rt->rt6i_nexthop; |
| 1243 | if (neigh == NULL) { |
| 1244 | ND_PRINTK0(KERN_ERR |
| 1245 | "ICMPv6 RA: %s() got default router without neighbour.\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 1246 | __func__); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1247 | dst_release(&rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | in6_dev_put(in6_dev); |
| 1249 | return; |
| 1250 | } |
| 1251 | neigh->flags |= NTF_ROUTER; |
YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 1252 | } else if (rt) { |
Pedro Ribeiro | 22441cf | 2008-10-15 15:47:49 -0700 | [diff] [blame] | 1253 | rt->rt6i_flags = (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | if (rt) |
| 1257 | rt->rt6i_expires = jiffies + (HZ * lifetime); |
| 1258 | |
| 1259 | if (ra_msg->icmph.icmp6_hop_limit) { |
| 1260 | in6_dev->cnf.hop_limit = ra_msg->icmph.icmp6_hop_limit; |
| 1261 | if (rt) |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1262 | dst_metric_set(&rt->dst, RTAX_HOPLIMIT, |
| 1263 | ra_msg->icmph.icmp6_hop_limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
YOSHIFUJI Hideaki | 65f5c7c | 2006-03-20 16:55:08 -0800 | [diff] [blame] | 1266 | skip_defrtr: |
| 1267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | /* |
| 1269 | * Update Reachable Time and Retrans Timer |
| 1270 | */ |
| 1271 | |
| 1272 | if (in6_dev->nd_parms) { |
| 1273 | unsigned long rtime = ntohl(ra_msg->retrans_timer); |
| 1274 | |
| 1275 | if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/HZ) { |
| 1276 | rtime = (rtime*HZ)/1000; |
| 1277 | if (rtime < HZ/10) |
| 1278 | rtime = HZ/10; |
| 1279 | in6_dev->nd_parms->retrans_time = rtime; |
| 1280 | in6_dev->tstamp = jiffies; |
| 1281 | inet6_ifinfo_notify(RTM_NEWLINK, in6_dev); |
| 1282 | } |
| 1283 | |
| 1284 | rtime = ntohl(ra_msg->reachable_time); |
| 1285 | if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/(3*HZ)) { |
| 1286 | rtime = (rtime*HZ)/1000; |
| 1287 | |
| 1288 | if (rtime < HZ/10) |
| 1289 | rtime = HZ/10; |
| 1290 | |
| 1291 | if (rtime != in6_dev->nd_parms->base_reachable_time) { |
| 1292 | in6_dev->nd_parms->base_reachable_time = rtime; |
| 1293 | in6_dev->nd_parms->gc_staletime = 3 * rtime; |
| 1294 | in6_dev->nd_parms->reachable_time = neigh_rand_reach_time(rtime); |
| 1295 | in6_dev->tstamp = jiffies; |
| 1296 | inet6_ifinfo_notify(RTM_NEWLINK, in6_dev); |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | |
Templin, Fred L | fadf6bf | 2008-03-11 18:35:59 -0400 | [diff] [blame] | 1301 | skip_linkparms: |
| 1302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | /* |
| 1304 | * Process options. |
| 1305 | */ |
| 1306 | |
| 1307 | if (!neigh) |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1308 | neigh = __neigh_lookup(&nd_tbl, &ipv6_hdr(skb)->saddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | skb->dev, 1); |
| 1310 | if (neigh) { |
| 1311 | u8 *lladdr = NULL; |
| 1312 | if (ndopts.nd_opts_src_lladdr) { |
| 1313 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr, |
| 1314 | skb->dev); |
| 1315 | if (!lladdr) { |
| 1316 | ND_PRINTK2(KERN_WARNING |
| 1317 | "ICMPv6 RA: invalid link-layer address length\n"); |
| 1318 | goto out; |
| 1319 | } |
| 1320 | } |
| 1321 | neigh_update(neigh, lladdr, NUD_STALE, |
| 1322 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
| 1323 | NEIGH_UPDATE_F_OVERRIDE| |
| 1324 | NEIGH_UPDATE_F_OVERRIDE_ISROUTER| |
| 1325 | NEIGH_UPDATE_F_ISROUTER); |
| 1326 | } |
| 1327 | |
Thomas Graf | 65e9b62 | 2010-09-03 02:59:14 +0000 | [diff] [blame] | 1328 | if (!accept_ra(in6_dev)) |
David Ward | 31ce8c7 | 2009-08-29 00:04:09 -0700 | [diff] [blame] | 1329 | goto out; |
| 1330 | |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1331 | #ifdef CONFIG_IPV6_ROUTE_INFO |
YOSHIFUJI Hideaki | 09c884d | 2006-03-20 17:07:03 -0800 | [diff] [blame] | 1332 | if (in6_dev->cnf.accept_ra_rtr_pref && ndopts.nd_opts_ri) { |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1333 | struct nd_opt_hdr *p; |
| 1334 | for (p = ndopts.nd_opts_ri; |
| 1335 | p; |
| 1336 | p = ndisc_next_option(p, ndopts.nd_opts_ri_end)) { |
YOSHIFUJI Hideaki | 6294e00 | 2008-03-15 23:56:52 -0400 | [diff] [blame] | 1337 | struct route_info *ri = (struct route_info *)p; |
| 1338 | #ifdef CONFIG_IPV6_NDISC_NODETYPE |
| 1339 | if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT && |
| 1340 | ri->prefix_len == 0) |
| 1341 | continue; |
| 1342 | #endif |
| 1343 | if (ri->prefix_len > in6_dev->cnf.accept_ra_rt_info_max_plen) |
YOSHIFUJI Hideaki | 09c884d | 2006-03-20 17:07:03 -0800 | [diff] [blame] | 1344 | continue; |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1345 | rt6_route_rcv(skb->dev, (u8*)p, (p->nd_opt_len) << 3, |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1346 | &ipv6_hdr(skb)->saddr); |
YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | #endif |
| 1350 | |
YOSHIFUJI Hideaki | de357cc | 2008-03-15 23:59:18 -0400 | [diff] [blame] | 1351 | #ifdef CONFIG_IPV6_NDISC_NODETYPE |
Templin, Fred L | fadf6bf | 2008-03-11 18:35:59 -0400 | [diff] [blame] | 1352 | /* skip link-specific ndopts from interior routers */ |
| 1353 | if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) |
| 1354 | goto out; |
YOSHIFUJI Hideaki | de357cc | 2008-03-15 23:59:18 -0400 | [diff] [blame] | 1355 | #endif |
Templin, Fred L | fadf6bf | 2008-03-11 18:35:59 -0400 | [diff] [blame] | 1356 | |
YOSHIFUJI Hideaki | c4fd30e | 2006-03-20 16:55:26 -0800 | [diff] [blame] | 1357 | if (in6_dev->cnf.accept_ra_pinfo && ndopts.nd_opts_pi) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | struct nd_opt_hdr *p; |
| 1359 | for (p = ndopts.nd_opts_pi; |
| 1360 | p; |
| 1361 | p = ndisc_next_option(p, ndopts.nd_opts_pi_end)) { |
| 1362 | addrconf_prefix_rcv(skb->dev, (u8*)p, (p->nd_opt_len) << 3); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | if (ndopts.nd_opts_mtu) { |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 1367 | __be32 n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | u32 mtu; |
| 1369 | |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 1370 | memcpy(&n, ((u8*)(ndopts.nd_opts_mtu+1))+2, sizeof(mtu)); |
| 1371 | mtu = ntohl(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
| 1373 | if (mtu < IPV6_MIN_MTU || mtu > skb->dev->mtu) { |
| 1374 | ND_PRINTK2(KERN_WARNING |
| 1375 | "ICMPv6 RA: invalid mtu: %d\n", |
| 1376 | mtu); |
| 1377 | } else if (in6_dev->cnf.mtu6 != mtu) { |
| 1378 | in6_dev->cnf.mtu6 = mtu; |
| 1379 | |
| 1380 | if (rt) |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1381 | dst_metric_set(&rt->dst, RTAX_MTU, mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | |
| 1383 | rt6_mtu_change(skb->dev, mtu); |
| 1384 | } |
| 1385 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1386 | |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 1387 | if (ndopts.nd_useropts) { |
YOSHIFUJI Hideaki | 61cf46ad | 2008-01-22 17:32:53 +0900 | [diff] [blame] | 1388 | struct nd_opt_hdr *p; |
| 1389 | for (p = ndopts.nd_useropts; |
| 1390 | p; |
| 1391 | p = ndisc_next_useropt(p, ndopts.nd_useropts_end)) { |
| 1392 | ndisc_ra_useropt(skb, p); |
Pierre Ynard | 3191057 | 2007-10-10 21:22:05 -0700 | [diff] [blame] | 1393 | } |
| 1394 | } |
| 1395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | if (ndopts.nd_opts_tgt_lladdr || ndopts.nd_opts_rh) { |
| 1397 | ND_PRINTK2(KERN_WARNING |
| 1398 | "ICMPv6 RA: invalid RA options"); |
| 1399 | } |
| 1400 | out: |
| 1401 | if (rt) |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1402 | dst_release(&rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | else if (neigh) |
| 1404 | neigh_release(neigh); |
| 1405 | in6_dev_put(in6_dev); |
| 1406 | } |
| 1407 | |
| 1408 | static void ndisc_redirect_rcv(struct sk_buff *skb) |
| 1409 | { |
| 1410 | struct inet6_dev *in6_dev; |
| 1411 | struct icmp6hdr *icmph; |
| 1412 | struct in6_addr *dest; |
| 1413 | struct in6_addr *target; /* new first hop to destination */ |
| 1414 | struct neighbour *neigh; |
| 1415 | int on_link = 0; |
| 1416 | struct ndisc_options ndopts; |
| 1417 | int optlen; |
| 1418 | u8 *lladdr = NULL; |
| 1419 | |
YOSHIFUJI Hideaki | de357cc | 2008-03-15 23:59:18 -0400 | [diff] [blame] | 1420 | #ifdef CONFIG_IPV6_NDISC_NODETYPE |
Templin, Fred L | fadf6bf | 2008-03-11 18:35:59 -0400 | [diff] [blame] | 1421 | switch (skb->ndisc_nodetype) { |
| 1422 | case NDISC_NODETYPE_HOST: |
| 1423 | case NDISC_NODETYPE_NODEFAULT: |
| 1424 | ND_PRINTK2(KERN_WARNING |
| 1425 | "ICMPv6 Redirect: from host or unauthorized router\n"); |
| 1426 | return; |
| 1427 | } |
YOSHIFUJI Hideaki | de357cc | 2008-03-15 23:59:18 -0400 | [diff] [blame] | 1428 | #endif |
Templin, Fred L | fadf6bf | 2008-03-11 18:35:59 -0400 | [diff] [blame] | 1429 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1430 | if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | ND_PRINTK2(KERN_WARNING |
| 1432 | "ICMPv6 Redirect: source address is not link-local.\n"); |
| 1433 | return; |
| 1434 | } |
| 1435 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1436 | optlen = skb->tail - skb->transport_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | optlen -= sizeof(struct icmp6hdr) + 2 * sizeof(struct in6_addr); |
| 1438 | |
| 1439 | if (optlen < 0) { |
| 1440 | ND_PRINTK2(KERN_WARNING |
| 1441 | "ICMPv6 Redirect: packet too short\n"); |
| 1442 | return; |
| 1443 | } |
| 1444 | |
Arnaldo Carvalho de Melo | cc70ab2 | 2007-03-13 14:03:22 -0300 | [diff] [blame] | 1445 | icmph = icmp6_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | target = (struct in6_addr *) (icmph + 1); |
| 1447 | dest = target + 1; |
| 1448 | |
| 1449 | if (ipv6_addr_is_multicast(dest)) { |
| 1450 | ND_PRINTK2(KERN_WARNING |
| 1451 | "ICMPv6 Redirect: destination address is multicast.\n"); |
| 1452 | return; |
| 1453 | } |
| 1454 | |
| 1455 | if (ipv6_addr_equal(dest, target)) { |
| 1456 | on_link = 1; |
Brian Haley | bf0b48d | 2007-10-08 00:12:05 -0700 | [diff] [blame] | 1457 | } else if (ipv6_addr_type(target) != |
| 1458 | (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1459 | ND_PRINTK2(KERN_WARNING |
Brian Haley | bf0b48d | 2007-10-08 00:12:05 -0700 | [diff] [blame] | 1460 | "ICMPv6 Redirect: target address is not link-local unicast.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | return; |
| 1462 | } |
| 1463 | |
| 1464 | in6_dev = in6_dev_get(skb->dev); |
| 1465 | if (!in6_dev) |
| 1466 | return; |
| 1467 | if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) { |
| 1468 | in6_dev_put(in6_dev); |
| 1469 | return; |
| 1470 | } |
| 1471 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1472 | /* RFC2461 8.1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | * The IP source address of the Redirect MUST be the same as the current |
| 1474 | * first-hop router for the specified ICMP Destination Address. |
| 1475 | */ |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | if (!ndisc_parse_options((u8*)(dest + 1), optlen, &ndopts)) { |
| 1478 | ND_PRINTK2(KERN_WARNING |
| 1479 | "ICMPv6 Redirect: invalid ND options\n"); |
| 1480 | in6_dev_put(in6_dev); |
| 1481 | return; |
| 1482 | } |
| 1483 | if (ndopts.nd_opts_tgt_lladdr) { |
| 1484 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, |
| 1485 | skb->dev); |
| 1486 | if (!lladdr) { |
| 1487 | ND_PRINTK2(KERN_WARNING |
| 1488 | "ICMPv6 Redirect: invalid link-layer address length\n"); |
| 1489 | in6_dev_put(in6_dev); |
| 1490 | return; |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | neigh = __neigh_lookup(&nd_tbl, target, skb->dev, 1); |
| 1495 | if (neigh) { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1496 | rt6_redirect(dest, &ipv6_hdr(skb)->daddr, |
| 1497 | &ipv6_hdr(skb)->saddr, neigh, lladdr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | on_link); |
| 1499 | neigh_release(neigh); |
| 1500 | } |
| 1501 | in6_dev_put(in6_dev); |
| 1502 | } |
| 1503 | |
| 1504 | void ndisc_send_redirect(struct sk_buff *skb, struct neighbour *neigh, |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1505 | const struct in6_addr *target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | { |
Daniel Lezcano | 1762f7e | 2008-03-07 11:15:34 -0800 | [diff] [blame] | 1507 | struct net_device *dev = skb->dev; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1508 | struct net *net = dev_net(dev); |
Daniel Lezcano | 1762f7e | 2008-03-07 11:15:34 -0800 | [diff] [blame] | 1509 | struct sock *sk = net->ipv6.ndisc_sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | int len = sizeof(struct icmp6hdr) + 2 * sizeof(struct in6_addr); |
| 1511 | struct sk_buff *buff; |
| 1512 | struct icmp6hdr *icmph; |
| 1513 | struct in6_addr saddr_buf; |
| 1514 | struct in6_addr *addrp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | struct rt6_info *rt; |
| 1516 | struct dst_entry *dst; |
| 1517 | struct inet6_dev *idev; |
| 1518 | struct flowi fl; |
| 1519 | u8 *opt; |
| 1520 | int rd_len; |
| 1521 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | u8 ha_buf[MAX_ADDR_LEN], *ha = NULL; |
| 1523 | |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 1524 | if (ipv6_get_lladdr(dev, &saddr_buf, IFA_F_TENTATIVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | ND_PRINTK2(KERN_WARNING |
| 1526 | "ICMPv6 Redirect: no link-local address on %s\n", |
| 1527 | dev->name); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1528 | return; |
| 1529 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1531 | if (!ipv6_addr_equal(&ipv6_hdr(skb)->daddr, target) && |
Brian Haley | bf0b48d | 2007-10-08 00:12:05 -0700 | [diff] [blame] | 1532 | ipv6_addr_type(target) != (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { |
Li Yewang | 2955652 | 2007-01-30 14:33:20 -0800 | [diff] [blame] | 1533 | ND_PRINTK2(KERN_WARNING |
Brian Haley | bf0b48d | 2007-10-08 00:12:05 -0700 | [diff] [blame] | 1534 | "ICMPv6 Redirect: target address is not link-local unicast.\n"); |
Li Yewang | 2955652 | 2007-01-30 14:33:20 -0800 | [diff] [blame] | 1535 | return; |
| 1536 | } |
| 1537 | |
Daniel Lezcano | 1762f7e | 2008-03-07 11:15:34 -0800 | [diff] [blame] | 1538 | icmpv6_flow_init(sk, &fl, NDISC_REDIRECT, |
YOSHIFUJI Hideaki | 95e41e9 | 2007-12-06 15:43:30 -0800 | [diff] [blame] | 1539 | &saddr_buf, &ipv6_hdr(skb)->saddr, dev->ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | |
Daniel Lezcano | 1762f7e | 2008-03-07 11:15:34 -0800 | [diff] [blame] | 1541 | dst = ip6_route_output(net, NULL, &fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | if (dst == NULL) |
| 1543 | return; |
| 1544 | |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 1545 | err = xfrm_lookup(net, &dst, &fl, NULL, 0); |
Patrick McHardy | e104411b | 2005-09-08 15:11:55 -0700 | [diff] [blame] | 1546 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | |
| 1549 | rt = (struct rt6_info *) dst; |
| 1550 | |
| 1551 | if (rt->rt6i_flags & RTF_GATEWAY) { |
| 1552 | ND_PRINTK2(KERN_WARNING |
| 1553 | "ICMPv6 Redirect: destination is not a neighbour.\n"); |
Ilpo Järvinen | d73f080 | 2009-02-06 23:47:37 -0800 | [diff] [blame] | 1554 | goto release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | } |
Ilpo Järvinen | d73f080 | 2009-02-06 23:47:37 -0800 | [diff] [blame] | 1556 | if (!xrlim_allow(dst, 1*HZ)) |
| 1557 | goto release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
| 1559 | if (dev->addr_len) { |
| 1560 | read_lock_bh(&neigh->lock); |
| 1561 | if (neigh->nud_state & NUD_VALID) { |
| 1562 | memcpy(ha_buf, neigh->ha, dev->addr_len); |
| 1563 | read_unlock_bh(&neigh->lock); |
| 1564 | ha = ha_buf; |
| 1565 | len += ndisc_opt_addr_space(dev); |
| 1566 | } else |
| 1567 | read_unlock_bh(&neigh->lock); |
| 1568 | } |
| 1569 | |
| 1570 | rd_len = min_t(unsigned int, |
| 1571 | IPV6_MIN_MTU-sizeof(struct ipv6hdr)-len, skb->len + 8); |
| 1572 | rd_len &= ~0x7; |
| 1573 | len += rd_len; |
| 1574 | |
David S. Miller | d54a81d | 2006-12-02 21:00:06 -0800 | [diff] [blame] | 1575 | buff = sock_alloc_send_skb(sk, |
| 1576 | (MAX_HEADER + sizeof(struct ipv6hdr) + |
Johannes Berg | f5184d2 | 2008-05-12 20:48:31 -0700 | [diff] [blame] | 1577 | len + LL_ALLOCATED_SPACE(dev)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | 1, &err); |
| 1579 | if (buff == NULL) { |
| 1580 | ND_PRINTK0(KERN_ERR |
Brian Haley | dae9de8 | 2009-06-02 00:20:26 -0700 | [diff] [blame] | 1581 | "ICMPv6 Redirect: %s() failed to allocate an skb, err=%d.\n", |
| 1582 | __func__, err); |
Ilpo Järvinen | d73f080 | 2009-02-06 23:47:37 -0800 | [diff] [blame] | 1583 | goto release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | skb_reserve(buff, LL_RESERVED_SPACE(dev)); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1587 | ip6_nd_hdr(sk, buff, dev, &saddr_buf, &ipv6_hdr(skb)->saddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | IPPROTO_ICMPV6, len); |
| 1589 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1590 | skb_set_transport_header(buff, skb_tail_pointer(buff) - buff->data); |
Arnaldo Carvalho de Melo | d10ba34 | 2007-03-14 21:05:37 -0300 | [diff] [blame] | 1591 | skb_put(buff, len); |
| 1592 | icmph = icmp6_hdr(buff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | |
| 1594 | memset(icmph, 0, sizeof(struct icmp6hdr)); |
| 1595 | icmph->icmp6_type = NDISC_REDIRECT; |
| 1596 | |
| 1597 | /* |
| 1598 | * copy target and destination addresses |
| 1599 | */ |
| 1600 | |
| 1601 | addrp = (struct in6_addr *)(icmph + 1); |
| 1602 | ipv6_addr_copy(addrp, target); |
| 1603 | addrp++; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1604 | ipv6_addr_copy(addrp, &ipv6_hdr(skb)->daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | |
| 1606 | opt = (u8*) (addrp + 1); |
| 1607 | |
| 1608 | /* |
| 1609 | * include target_address option |
| 1610 | */ |
| 1611 | |
| 1612 | if (ha) |
| 1613 | opt = ndisc_fill_addr_option(opt, ND_OPT_TARGET_LL_ADDR, ha, |
| 1614 | dev->addr_len, dev->type); |
| 1615 | |
| 1616 | /* |
| 1617 | * build redirect option and copy skb over to the new packet. |
| 1618 | */ |
| 1619 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1620 | memset(opt, 0, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | *(opt++) = ND_OPT_REDIRECT_HDR; |
| 1622 | *(opt++) = (rd_len >> 3); |
| 1623 | opt += 6; |
| 1624 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1625 | memcpy(opt, ipv6_hdr(skb), rd_len - 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1627 | icmph->icmp6_cksum = csum_ipv6_magic(&saddr_buf, &ipv6_hdr(skb)->saddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | len, IPPROTO_ICMPV6, |
Joe Perches | 07f0757 | 2008-11-19 15:44:53 -0800 | [diff] [blame] | 1629 | csum_partial(icmph, len, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1631 | skb_dst_set(buff, dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | idev = in6_dev_get(dst->dev); |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 1633 | IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len); |
Jan Engelhardt | b2e0b38 | 2010-03-23 04:09:07 +0100 | [diff] [blame] | 1634 | err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, buff, NULL, dst->dev, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 1635 | dst_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | if (!err) { |
Denis V. Lunev | 5c5d244 | 2008-10-08 10:33:50 -0700 | [diff] [blame] | 1637 | ICMP6MSGOUT_INC_STATS(net, idev, NDISC_REDIRECT); |
Denis V. Lunev | a862f6a | 2008-10-08 10:33:06 -0700 | [diff] [blame] | 1638 | ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | if (likely(idev != NULL)) |
| 1642 | in6_dev_put(idev); |
Ilpo Järvinen | d73f080 | 2009-02-06 23:47:37 -0800 | [diff] [blame] | 1643 | return; |
| 1644 | |
| 1645 | release: |
| 1646 | dst_release(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | static void pndisc_redo(struct sk_buff *skb) |
| 1650 | { |
YOSHIFUJI Hideaki | 140e26fc | 2005-10-05 12:11:41 -0700 | [diff] [blame] | 1651 | ndisc_recv_ns(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | kfree_skb(skb); |
| 1653 | } |
| 1654 | |
| 1655 | int ndisc_rcv(struct sk_buff *skb) |
| 1656 | { |
| 1657 | struct nd_msg *msg; |
| 1658 | |
| 1659 | if (!pskb_may_pull(skb, skb->len)) |
| 1660 | return 0; |
| 1661 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1662 | msg = (struct nd_msg *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1664 | __skb_push(skb, skb->data - skb_transport_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1666 | if (ipv6_hdr(skb)->hop_limit != 255) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | ND_PRINTK2(KERN_WARNING |
| 1668 | "ICMPv6 NDISC: invalid hop-limit: %d\n", |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1669 | ipv6_hdr(skb)->hop_limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | if (msg->icmph.icmp6_code != 0) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1674 | ND_PRINTK2(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | "ICMPv6 NDISC: invalid ICMPv6 code: %d\n", |
| 1676 | msg->icmph.icmp6_code); |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 1680 | memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb)); |
| 1681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | switch (msg->icmph.icmp6_type) { |
| 1683 | case NDISC_NEIGHBOUR_SOLICITATION: |
| 1684 | ndisc_recv_ns(skb); |
| 1685 | break; |
| 1686 | |
| 1687 | case NDISC_NEIGHBOUR_ADVERTISEMENT: |
| 1688 | ndisc_recv_na(skb); |
| 1689 | break; |
| 1690 | |
| 1691 | case NDISC_ROUTER_SOLICITATION: |
| 1692 | ndisc_recv_rs(skb); |
| 1693 | break; |
| 1694 | |
| 1695 | case NDISC_ROUTER_ADVERTISEMENT: |
| 1696 | ndisc_router_discovery(skb); |
| 1697 | break; |
| 1698 | |
| 1699 | case NDISC_REDIRECT: |
| 1700 | ndisc_redirect_rcv(skb); |
| 1701 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | |
| 1704 | return 0; |
| 1705 | } |
| 1706 | |
| 1707 | static int ndisc_netdev_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 1708 | { |
| 1709 | struct net_device *dev = ptr; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1710 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | |
| 1712 | switch (event) { |
| 1713 | case NETDEV_CHANGEADDR: |
| 1714 | neigh_changeaddr(&nd_tbl, dev); |
Daniel Lezcano | 5b7c931 | 2008-03-03 23:28:58 -0800 | [diff] [blame] | 1715 | fib6_run_gc(~0UL, net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | break; |
| 1717 | case NETDEV_DOWN: |
| 1718 | neigh_ifdown(&nd_tbl, dev); |
Daniel Lezcano | 5b7c931 | 2008-03-03 23:28:58 -0800 | [diff] [blame] | 1719 | fib6_run_gc(~0UL, net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | break; |
| 1721 | default: |
| 1722 | break; |
| 1723 | } |
| 1724 | |
| 1725 | return NOTIFY_DONE; |
| 1726 | } |
| 1727 | |
| 1728 | static struct notifier_block ndisc_netdev_notifier = { |
| 1729 | .notifier_call = ndisc_netdev_event, |
| 1730 | }; |
| 1731 | |
| 1732 | #ifdef CONFIG_SYSCTL |
| 1733 | static void ndisc_warn_deprecated_sysctl(struct ctl_table *ctl, |
| 1734 | const char *func, const char *dev_name) |
| 1735 | { |
| 1736 | static char warncomm[TASK_COMM_LEN]; |
| 1737 | static int warned; |
| 1738 | if (strcmp(warncomm, current->comm) && warned < 5) { |
| 1739 | strcpy(warncomm, current->comm); |
| 1740 | printk(KERN_WARNING |
| 1741 | "process `%s' is using deprecated sysctl (%s) " |
| 1742 | "net.ipv6.neigh.%s.%s; " |
| 1743 | "Use net.ipv6.neigh.%s.%s_ms " |
| 1744 | "instead.\n", |
| 1745 | warncomm, func, |
| 1746 | dev_name, ctl->procname, |
| 1747 | dev_name, ctl->procname); |
| 1748 | warned++; |
| 1749 | } |
| 1750 | } |
| 1751 | |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1752 | int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | { |
| 1754 | struct net_device *dev = ctl->extra1; |
| 1755 | struct inet6_dev *idev; |
| 1756 | int ret; |
| 1757 | |
Eric W. Biederman | d12af67 | 2007-10-18 03:05:25 -0700 | [diff] [blame] | 1758 | if ((strcmp(ctl->procname, "retrans_time") == 0) || |
| 1759 | (strcmp(ctl->procname, "base_reachable_time") == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | ndisc_warn_deprecated_sysctl(ctl, "syscall", dev ? dev->name : "default"); |
| 1761 | |
Eric W. Biederman | d12af67 | 2007-10-18 03:05:25 -0700 | [diff] [blame] | 1762 | if (strcmp(ctl->procname, "retrans_time") == 0) |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1763 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
Eric W. Biederman | d12af67 | 2007-10-18 03:05:25 -0700 | [diff] [blame] | 1764 | |
| 1765 | else if (strcmp(ctl->procname, "base_reachable_time") == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | ret = proc_dointvec_jiffies(ctl, write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1767 | buffer, lenp, ppos); |
Eric W. Biederman | d12af67 | 2007-10-18 03:05:25 -0700 | [diff] [blame] | 1768 | |
| 1769 | else if ((strcmp(ctl->procname, "retrans_time_ms") == 0) || |
YOSHIFUJI Hideaki | ad02ac1 | 2007-10-29 01:32:23 -0700 | [diff] [blame] | 1770 | (strcmp(ctl->procname, "base_reachable_time_ms") == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | ret = proc_dointvec_ms_jiffies(ctl, write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1772 | buffer, lenp, ppos); |
Eric W. Biederman | d12af67 | 2007-10-18 03:05:25 -0700 | [diff] [blame] | 1773 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
| 1776 | if (write && ret == 0 && dev && (idev = in6_dev_get(dev)) != NULL) { |
Eric W. Biederman | d12af67 | 2007-10-18 03:05:25 -0700 | [diff] [blame] | 1777 | if (ctl->data == &idev->nd_parms->base_reachable_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | idev->nd_parms->reachable_time = neigh_rand_reach_time(idev->nd_parms->base_reachable_time); |
| 1779 | idev->tstamp = jiffies; |
| 1780 | inet6_ifinfo_notify(RTM_NEWLINK, idev); |
| 1781 | in6_dev_put(idev); |
| 1782 | } |
| 1783 | return ret; |
| 1784 | } |
| 1785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | |
| 1787 | #endif |
| 1788 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 1789 | static int __net_init ndisc_net_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | { |
| 1791 | struct ipv6_pinfo *np; |
| 1792 | struct sock *sk; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1793 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | |
Denis V. Lunev | 1ed8516 | 2008-04-03 14:31:03 -0700 | [diff] [blame] | 1795 | err = inet_ctl_sock_create(&sk, PF_INET6, |
| 1796 | SOCK_RAW, IPPROTO_ICMPV6, net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | if (err < 0) { |
| 1798 | ND_PRINTK0(KERN_ERR |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1799 | "ICMPv6 NDISC: Failed to initialize the control socket (err %d).\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | return err; |
| 1802 | } |
| 1803 | |
Denis V. Lunev | 1ed8516 | 2008-04-03 14:31:03 -0700 | [diff] [blame] | 1804 | net->ipv6.ndisc_sk = sk; |
Daniel Lezcano | 1762f7e | 2008-03-07 11:15:34 -0800 | [diff] [blame] | 1805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | np = inet6_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | np->hop_limit = 255; |
| 1808 | /* Do not loopback ndisc messages */ |
| 1809 | np->mc_loop = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | |
Daniel Lezcano | 1762f7e | 2008-03-07 11:15:34 -0800 | [diff] [blame] | 1811 | return 0; |
| 1812 | } |
| 1813 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 1814 | static void __net_exit ndisc_net_exit(struct net *net) |
Daniel Lezcano | 1762f7e | 2008-03-07 11:15:34 -0800 | [diff] [blame] | 1815 | { |
Denis V. Lunev | 1ed8516 | 2008-04-03 14:31:03 -0700 | [diff] [blame] | 1816 | inet_ctl_sock_destroy(net->ipv6.ndisc_sk); |
Daniel Lezcano | 1762f7e | 2008-03-07 11:15:34 -0800 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | static struct pernet_operations ndisc_net_ops = { |
| 1820 | .init = ndisc_net_init, |
| 1821 | .exit = ndisc_net_exit, |
| 1822 | }; |
| 1823 | |
| 1824 | int __init ndisc_init(void) |
| 1825 | { |
| 1826 | int err; |
| 1827 | |
| 1828 | err = register_pernet_subsys(&ndisc_net_ops); |
| 1829 | if (err) |
| 1830 | return err; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1831 | /* |
| 1832 | * Initialize the neighbour table |
| 1833 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | neigh_table_init(&nd_tbl); |
| 1835 | |
| 1836 | #ifdef CONFIG_SYSCTL |
Eric W. Biederman | 54716e3 | 2010-02-14 03:27:03 +0000 | [diff] [blame] | 1837 | err = neigh_sysctl_register(NULL, &nd_tbl.parms, "ipv6", |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1838 | &ndisc_ifinfo_sysctl_change); |
Daniel Lezcano | 1762f7e | 2008-03-07 11:15:34 -0800 | [diff] [blame] | 1839 | if (err) |
| 1840 | goto out_unregister_pernet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | #endif |
Daniel Lezcano | 1762f7e | 2008-03-07 11:15:34 -0800 | [diff] [blame] | 1842 | err = register_netdevice_notifier(&ndisc_netdev_notifier); |
| 1843 | if (err) |
| 1844 | goto out_unregister_sysctl; |
| 1845 | out: |
| 1846 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | |
Daniel Lezcano | 1762f7e | 2008-03-07 11:15:34 -0800 | [diff] [blame] | 1848 | out_unregister_sysctl: |
| 1849 | #ifdef CONFIG_SYSCTL |
| 1850 | neigh_sysctl_unregister(&nd_tbl.parms); |
| 1851 | out_unregister_pernet: |
| 1852 | #endif |
| 1853 | unregister_pernet_subsys(&ndisc_net_ops); |
| 1854 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | void ndisc_cleanup(void) |
| 1858 | { |
Dmitry Mishin | 36f73d0 | 2006-11-03 16:08:19 -0800 | [diff] [blame] | 1859 | unregister_netdevice_notifier(&ndisc_netdev_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | #ifdef CONFIG_SYSCTL |
| 1861 | neigh_sysctl_unregister(&nd_tbl.parms); |
| 1862 | #endif |
| 1863 | neigh_table_clear(&nd_tbl); |
Daniel Lezcano | 1762f7e | 2008-03-07 11:15:34 -0800 | [diff] [blame] | 1864 | unregister_pernet_subsys(&ndisc_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | } |