1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * INET An implementation of the TCP/IP protocol suite for the LINUX 4 * operating system. INET is implemented using the BSD Socket 5 * interface as the means of communication with the user level. 6 * 7 * ROUTE - implementation of the IP router. 8 * 9 * Authors: Ross Biro 10 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 11 * Alan Cox, <gw4pts@gw4pts.ampr.org> 12 * Linus Torvalds, <Linus.Torvalds@helsinki.fi> 13 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 14 * 15 * Fixes: 16 * Alan Cox : Verify area fixes. 17 * Alan Cox : cli() protects routing changes 18 * Rui Oliveira : ICMP routing table updates 19 * (rco@di.uminho.pt) Routing table insertion and update 20 * Linus Torvalds : Rewrote bits to be sensible 21 * Alan Cox : Added BSD route gw semantics 22 * Alan Cox : Super /proc >4K 23 * Alan Cox : MTU in route table 24 * Alan Cox : MSS actually. Also added the window 25 * clamper. 26 * Sam Lantinga : Fixed route matching in rt_del() 27 * Alan Cox : Routing cache support. 28 * Alan Cox : Removed compatibility cruft. 29 * Alan Cox : RTF_REJECT support. 30 * Alan Cox : TCP irtt support. 31 * Jonathan Naylor : Added Metric support. 32 * Miquel van Smoorenburg : BSD API fixes. 33 * Miquel van Smoorenburg : Metrics. 34 * Alan Cox : Use __u32 properly 35 * Alan Cox : Aligned routing errors more closely with BSD 36 * our system is still very different. 37 * Alan Cox : Faster /proc handling 38 * Alexey Kuznetsov : Massive rework to support tree based routing, 39 * routing caches and better behaviour. 40 * 41 * Olaf Erb : irtt wasn't being copied right. 42 * Bjorn Ekwall : Kerneld route support. 43 * Alan Cox : Multicast fixed (I hope) 44 * Pavel Krauz : Limited broadcast fixed 45 * Mike McLagan : Routing by source 46 * Alexey Kuznetsov : End of old history. Split to fib.c and 47 * route.c and rewritten from scratch. 48 * Andi Kleen : Load-limit warning messages. 49 * Vitaly E. Lavrov : Transparent proxy revived after year coma. 50 * Vitaly E. Lavrov : Race condition in ip_route_input_slow. 51 * Tobias Ringstrom : Uninitialized res.type in ip_route_output_slow. 52 * Vladimir V. Ivanov : IP rule info (flowid) is really useful. 53 * Marc Boucher : routing by fwmark 54 * Robert Olsson : Added rt_cache statistics 55 * Arnaldo C. Melo : Convert proc stuff to seq_file 56 * Eric Dumazet : hashed spinlocks and rt_check_expire() fixes. 57 * Ilia Sotnikov : Ignore TOS on PMTUD and Redirect 58 * Ilia Sotnikov : Removed TOS from hash calculations 59 */ 60 61 #define pr_fmt(fmt) "IPv4: " fmt 62 63 #include <linux/module.h> 64 #include <linux/bitops.h> 65 #include <linux/kernel.h> 66 #include <linux/mm.h> 67 #include <linux/memblock.h> 68 #include <linux/socket.h> 69 #include <linux/errno.h> 70 #include <linux/in.h> 71 #include <linux/inet.h> 72 #include <linux/netdevice.h> 73 #include <linux/proc_fs.h> 74 #include <linux/init.h> 75 #include <linux/skbuff.h> 76 #include <linux/inetdevice.h> 77 #include <linux/igmp.h> 78 #include <linux/pkt_sched.h> 79 #include <linux/mroute.h> 80 #include <linux/netfilter_ipv4.h> 81 #include <linux/random.h> 82 #include <linux/rcupdate.h> 83 #include <linux/slab.h> 84 #include <linux/jhash.h> 85 #include <net/dst.h> 86 #include <net/dst_metadata.h> 87 #include <net/net_namespace.h> 88 #include <net/ip.h> 89 #include <net/route.h> 90 #include <net/inetpeer.h> 91 #include <net/sock.h> 92 #include <net/ip_fib.h> 93 #include <net/nexthop.h> 94 #include <net/tcp.h> 95 #include <net/icmp.h> 96 #include <net/xfrm.h> 97 #include <net/lwtunnel.h> 98 #include <net/netevent.h> 99 #include <net/rtnetlink.h> 100 #ifdef CONFIG_SYSCTL 101 #include <linux/sysctl.h> 102 #endif 103 #include <net/secure_seq.h> 104 #include <net/ip_tunnels.h> 105 106 #include "fib_lookup.h" 107 108 #define RT_FL_TOS(oldflp4) \ 109 ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK)) 110 111 #define RT_GC_TIMEOUT (300*HZ) 112 113 static int ip_rt_max_size; 114 static int ip_rt_redirect_number __read_mostly = 9; 115 static int ip_rt_redirect_load __read_mostly = HZ / 50; 116 static int ip_rt_redirect_silence __read_mostly = ((HZ / 50) << (9 + 1)); 117 static int ip_rt_error_cost __read_mostly = HZ; 118 static int ip_rt_error_burst __read_mostly = 5 * HZ; 119 static int ip_rt_mtu_expires __read_mostly = 10 * 60 * HZ; 120 static u32 ip_rt_min_pmtu __read_mostly = 512 + 20 + 20; 121 static int ip_rt_min_advmss __read_mostly = 256; 122 123 static int ip_rt_gc_timeout __read_mostly = RT_GC_TIMEOUT; 124 125 /* 126 * Interface to generic destination cache. 127 */ 128 129 INDIRECT_CALLABLE_SCOPE 130 struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie); 131 static unsigned int ipv4_default_advmss(const struct dst_entry *dst); 132 INDIRECT_CALLABLE_SCOPE 133 unsigned int ipv4_mtu(const struct dst_entry *dst); 134 static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst); 135 static void ipv4_link_failure(struct sk_buff *skb); 136 static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 137 struct sk_buff *skb, u32 mtu, 138 bool confirm_neigh); 139 static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, 140 struct sk_buff *skb); 141 static void ipv4_dst_destroy(struct dst_entry *dst); 142 143 static u32 *ipv4_cow_metrics(struct dst_entry *dst, unsigned long old) 144 { 145 WARN_ON(1); 146 return NULL; 147 } 148 149 static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst, 150 struct sk_buff *skb, 151 const void *daddr); 152 static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr); 153 154 static struct dst_ops ipv4_dst_ops = { 155 .family = AF_INET, 156 .check = ipv4_dst_check, 157 .default_advmss = ipv4_default_advmss, 158 .mtu = ipv4_mtu, 159 .cow_metrics = ipv4_cow_metrics, 160 .destroy = ipv4_dst_destroy, 161 .negative_advice = ipv4_negative_advice, 162 .link_failure = ipv4_link_failure, 163 .update_pmtu = ip_rt_update_pmtu, 164 .redirect = ip_do_redirect, 165 .local_out = __ip_local_out, 166 .neigh_lookup = ipv4_neigh_lookup, 167 .confirm_neigh = ipv4_confirm_neigh, 168 }; 169 170 #define ECN_OR_COST(class) TC_PRIO_##class 171 172 const __u8 ip_tos2prio[16] = { 173 TC_PRIO_BESTEFFORT, 174 ECN_OR_COST(BESTEFFORT), 175 TC_PRIO_BESTEFFORT, 176 ECN_OR_COST(BESTEFFORT), 177 TC_PRIO_BULK, 178 ECN_OR_COST(BULK), 179 TC_PRIO_BULK, 180 ECN_OR_COST(BULK), 181 TC_PRIO_INTERACTIVE, 182 ECN_OR_COST(INTERACTIVE), 183 TC_PRIO_INTERACTIVE, 184 ECN_OR_COST(INTERACTIVE), 185 TC_PRIO_INTERACTIVE_BULK, 186 ECN_OR_COST(INTERACTIVE_BULK), 187 TC_PRIO_INTERACTIVE_BULK, 188 ECN_OR_COST(INTERACTIVE_BULK) 189 }; 190 EXPORT_SYMBOL(ip_tos2prio); 191 192 static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat); 193 #define RT_CACHE_STAT_INC(field) raw_cpu_inc(rt_cache_stat.field) 194 195 #ifdef CONFIG_PROC_FS 196 static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos) 197 { 198 if (*pos) 199 return NULL; 200 return SEQ_START_TOKEN; 201 } 202 203 static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos) 204 { 205 ++*pos; 206 return NULL; 207 } 208 209 static void rt_cache_seq_stop(struct seq_file *seq, void *v) 210 { 211 } 212 213 static int rt_cache_seq_show(struct seq_file *seq, void *v) 214 { 215 if (v == SEQ_START_TOKEN) 216 seq_printf(seq, "%-127s\n", 217 "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t" 218 "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t" 219 "HHUptod\tSpecDst"); 220 return 0; 221 } 222 223 static const struct seq_operations rt_cache_seq_ops = { 224 .start = rt_cache_seq_start, 225 .next = rt_cache_seq_next, 226 .stop = rt_cache_seq_stop, 227 .show = rt_cache_seq_show, 228 }; 229 230 static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos) 231 { 232 int cpu; 233 234 if (*pos == 0) 235 return SEQ_START_TOKEN; 236 237 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) { 238 if (!cpu_possible(cpu)) 239 continue; 240 *pos = cpu+1; 241 return &per_cpu(rt_cache_stat, cpu); 242 } 243 return NULL; 244 } 245 246 static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos) 247 { 248 int cpu; 249 250 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) { 251 if (!cpu_possible(cpu)) 252 continue; 253 *pos = cpu+1; 254 return &per_cpu(rt_cache_stat, cpu); 255 } 256 (*pos)++; 257 return NULL; 258 259 } 260 261 static void rt_cpu_seq_stop(struct seq_file *seq, void *v) 262 { 263 264 } 265 266 static int rt_cpu_seq_show(struct seq_file *seq, void *v) 267 { 268 struct rt_cache_stat *st = v; 269 270 if (v == SEQ_START_TOKEN) { 271 seq_puts(seq, "entries in_hit in_slow_tot in_slow_mc in_no_route in_brd in_martian_dst in_martian_src out_hit out_slow_tot out_slow_mc gc_total gc_ignored gc_goal_miss gc_dst_overflow in_hlist_search out_hlist_search\n"); 272 return 0; 273 } 274 275 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x " 276 "%08x %08x %08x %08x %08x %08x " 277 "%08x %08x %08x %08x\n", 278 dst_entries_get_slow(&ipv4_dst_ops), 279 0, /* st->in_hit */ 280 st->in_slow_tot, 281 st->in_slow_mc, 282 st->in_no_route, 283 st->in_brd, 284 st->in_martian_dst, 285 st->in_martian_src, 286 287 0, /* st->out_hit */ 288 st->out_slow_tot, 289 st->out_slow_mc, 290 291 0, /* st->gc_total */ 292 0, /* st->gc_ignored */ 293 0, /* st->gc_goal_miss */ 294 0, /* st->gc_dst_overflow */ 295 0, /* st->in_hlist_search */ 296 0 /* st->out_hlist_search */ 297 ); 298 return 0; 299 } 300 301 static const struct seq_operations rt_cpu_seq_ops = { 302 .start = rt_cpu_seq_start, 303 .next = rt_cpu_seq_next, 304 .stop = rt_cpu_seq_stop, 305 .show = rt_cpu_seq_show, 306 }; 307 308 #ifdef CONFIG_IP_ROUTE_CLASSID 309 static int rt_acct_proc_show(struct seq_file *m, void *v) 310 { 311 struct ip_rt_acct *dst, *src; 312 unsigned int i, j; 313 314 dst = kcalloc(256, sizeof(struct ip_rt_acct), GFP_KERNEL); 315 if (!dst) 316 return -ENOMEM; 317 318 for_each_possible_cpu(i) { 319 src = (struct ip_rt_acct *)per_cpu_ptr(ip_rt_acct, i); 320 for (j = 0; j < 256; j++) { 321 dst[j].o_bytes += src[j].o_bytes; 322 dst[j].o_packets += src[j].o_packets; 323 dst[j].i_bytes += src[j].i_bytes; 324 dst[j].i_packets += src[j].i_packets; 325 } 326 } 327 328 seq_write(m, dst, 256 * sizeof(struct ip_rt_acct)); 329 kfree(dst); 330 return 0; 331 } 332 #endif 333 334 static int __net_init ip_rt_do_proc_init(struct net *net) 335 { 336 struct proc_dir_entry *pde; 337 338 pde = proc_create_seq("rt_cache", 0444, net->proc_net, 339 &rt_cache_seq_ops); 340 if (!pde) 341 goto err1; 342 343 pde = proc_create_seq("rt_cache", 0444, net->proc_net_stat, 344 &rt_cpu_seq_ops); 345 if (!pde) 346 goto err2; 347 348 #ifdef CONFIG_IP_ROUTE_CLASSID 349 pde = proc_create_single("rt_acct", 0, net->proc_net, 350 rt_acct_proc_show); 351 if (!pde) 352 goto err3; 353 #endif 354 return 0; 355 356 #ifdef CONFIG_IP_ROUTE_CLASSID 357 err3: 358 remove_proc_entry("rt_cache", net->proc_net_stat); 359 #endif 360 err2: 361 remove_proc_entry("rt_cache", net->proc_net); 362 err1: 363 return -ENOMEM; 364 } 365 366 static void __net_exit ip_rt_do_proc_exit(struct net *net) 367 { 368 remove_proc_entry("rt_cache", net->proc_net_stat); 369 remove_proc_entry("rt_cache", net->proc_net); 370 #ifdef CONFIG_IP_ROUTE_CLASSID 371 remove_proc_entry("rt_acct", net->proc_net); 372 #endif 373 } 374 375 static struct pernet_operations ip_rt_proc_ops __net_initdata = { 376 .init = ip_rt_do_proc_init, 377 .exit = ip_rt_do_proc_exit, 378 }; 379 380 static int __init ip_rt_proc_init(void) 381 { 382 return register_pernet_subsys(&ip_rt_proc_ops); 383 } 384 385 #else 386 static inline int ip_rt_proc_init(void) 387 { 388 return 0; 389 } 390 #endif /* CONFIG_PROC_FS */ 391 392 static inline bool rt_is_expired(const struct rtable *rth) 393 { 394 return rth->rt_genid != rt_genid_ipv4(dev_net(rth->dst.dev)); 395 } 396 397 void rt_cache_flush(struct net *net) 398 { 399 rt_genid_bump_ipv4(net); 400 } 401 402 static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst, 403 struct sk_buff *skb, 404 const void *daddr) 405 { 406 const struct rtable *rt = container_of(dst, struct rtable, dst); 407 struct net_device *dev = dst->dev; 408 struct neighbour *n; 409 410 rcu_read_lock_bh(); 411 412 if (likely(rt->rt_gw_family == AF_INET)) { 413 n = ip_neigh_gw4(dev, rt->rt_gw4); 414 } else if (rt->rt_gw_family == AF_INET6) { 415 n = ip_neigh_gw6(dev, &rt->rt_gw6); 416 } else { 417 __be32 pkey; 418 419 pkey = skb ? ip_hdr(skb)->daddr : *((__be32 *) daddr); 420 n = ip_neigh_gw4(dev, pkey); 421 } 422 423 if (!IS_ERR(n) && !refcount_inc_not_zero(&n->refcnt)) 424 n = NULL; 425 426 rcu_read_unlock_bh(); 427 428 return n; 429 } 430 431 static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr) 432 { 433 const struct rtable *rt = container_of(dst, struct rtable, dst); 434 struct net_device *dev = dst->dev; 435 const __be32 *pkey = daddr; 436 437 if (rt->rt_gw_family == AF_INET) { 438 pkey = (const __be32 *)&rt->rt_gw4; 439 } else if (rt->rt_gw_family == AF_INET6) { 440 return __ipv6_confirm_neigh_stub(dev, &rt->rt_gw6); 441 } else if (!daddr || 442 (rt->rt_flags & 443 (RTCF_MULTICAST | RTCF_BROADCAST | RTCF_LOCAL))) { 444 return; 445 } 446 __ipv4_confirm_neigh(dev, *(__force u32 *)pkey); 447 } 448 449 /* Hash tables of size 2048..262144 depending on RAM size. 450 * Each bucket uses 8 bytes. 451 */ 452 static u32 ip_idents_mask __read_mostly; 453 static atomic_t *ip_idents __read_mostly; 454 static u32 *ip_tstamps __read_mostly; 455 456 /* In order to protect privacy, we add a perturbation to identifiers 457 * if one generator is seldom used. This makes hard for an attacker 458 * to infer how many packets were sent between two points in time. 459 */ 460 u32 ip_idents_reserve(u32 hash, int segs) 461 { 462 u32 bucket, old, now = (u32)jiffies; 463 atomic_t *p_id; 464 u32 *p_tstamp; 465 u32 delta = 0; 466 467 bucket = hash & ip_idents_mask; 468 p_tstamp = ip_tstamps + bucket; 469 p_id = ip_idents + bucket; 470 old = READ_ONCE(*p_tstamp); 471 472 if (old != now && cmpxchg(p_tstamp, old, now) == old) 473 delta = prandom_u32_max(now - old); 474 475 /* If UBSAN reports an error there, please make sure your compiler 476 * supports -fno-strict-overflow before reporting it that was a bug 477 * in UBSAN, and it has been fixed in GCC-8. 478 */ 479 return atomic_add_return(segs + delta, p_id) - segs; 480 } 481 EXPORT_SYMBOL(ip_idents_reserve); 482 483 void __ip_select_ident(struct net *net, struct iphdr *iph, int segs) 484 { 485 u32 hash, id; 486 487 /* Note the following code is not safe, but this is okay. */ 488 if (unlikely(siphash_key_is_zero(&net->ipv4.ip_id_key))) 489 get_random_bytes(&net->ipv4.ip_id_key, 490 sizeof(net->ipv4.ip_id_key)); 491 492 hash = siphash_3u32((__force u32)iph->daddr, 493 (__force u32)iph->saddr, 494 iph->protocol, 495 &net->ipv4.ip_id_key); 496 id = ip_idents_reserve(hash, segs); 497 iph->id = htons(id); 498 } 499 EXPORT_SYMBOL(__ip_select_ident); 500 501 static void __build_flow_key(const struct net *net, struct flowi4 *fl4, 502 const struct sock *sk, 503 const struct iphdr *iph, 504 int oif, u8 tos, 505 u8 prot, u32 mark, int flow_flags) 506 { 507 if (sk) { 508 const struct inet_sock *inet = inet_sk(sk); 509 510 oif = sk->sk_bound_dev_if; 511 mark = sk->sk_mark; 512 tos = RT_CONN_FLAGS(sk); 513 prot = inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol; 514 } 515 flowi4_init_output(fl4, oif, mark, tos, 516 RT_SCOPE_UNIVERSE, prot, 517 flow_flags, 518 iph->daddr, iph->saddr, 0, 0, 519 sock_net_uid(net, sk)); 520 } 521 522 static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb, 523 const struct sock *sk) 524 { 525 const struct net *net = dev_net(skb->dev); 526 const struct iphdr *iph = ip_hdr(skb); 527 int oif = skb->dev->ifindex; 528 u8 tos = RT_TOS(iph->tos); 529 u8 prot = iph->protocol; 530 u32 mark = skb->mark; 531 532 __build_flow_key(net, fl4, sk, iph, oif, tos, prot, mark, 0); 533 } 534 535 static void build_sk_flow_key(struct flowi4 *fl4, const struct sock *sk) 536 { 537 const struct inet_sock *inet = inet_sk(sk); 538 const struct ip_options_rcu *inet_opt; 539 __be32 daddr = inet->inet_daddr; 540 541 rcu_read_lock(); 542 inet_opt = rcu_dereference(inet->inet_opt); 543 if (inet_opt && inet_opt->opt.srr) 544 daddr = inet_opt->opt.faddr; 545 flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark, 546 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, 547 inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol, 548 inet_sk_flowi_flags(sk), 549 daddr, inet->inet_saddr, 0, 0, sk->sk_uid); 550 rcu_read_unlock(); 551 } 552 553 static void ip_rt_build_flow_key(struct flowi4 *fl4, const struct sock *sk, 554 const struct sk_buff *skb) 555 { 556 if (skb) 557 build_skb_flow_key(fl4, skb, sk); 558 else 559 build_sk_flow_key(fl4, sk); 560 } 561 562 static DEFINE_SPINLOCK(fnhe_lock); 563 564 static void fnhe_flush_routes(struct fib_nh_exception *fnhe) 565 { 566 struct rtable *rt; 567 568 rt = rcu_dereference(fnhe->fnhe_rth_input); 569 if (rt) { 570 RCU_INIT_POINTER(fnhe->fnhe_rth_input, NULL); 571 dst_dev_put(&rt->dst); 572 dst_release(&rt->dst); 573 } 574 rt = rcu_dereference(fnhe->fnhe_rth_output); 575 if (rt) { 576 RCU_INIT_POINTER(fnhe->fnhe_rth_output, NULL); 577 dst_dev_put(&rt->dst); 578 dst_release(&rt->dst); 579 } 580 } 581 582 static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash) 583 { 584 struct fib_nh_exception __rcu **fnhe_p, **oldest_p; 585 struct fib_nh_exception *fnhe, *oldest = NULL; 586 587 for (fnhe_p = &hash->chain; ; fnhe_p = &fnhe->fnhe_next) { 588 fnhe = rcu_dereference_protected(*fnhe_p, 589 lockdep_is_held(&fnhe_lock)); 590 if (!fnhe) 591 break; 592 if (!oldest || 593 time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp)) { 594 oldest = fnhe; 595 oldest_p = fnhe_p; 596 } 597 } 598 fnhe_flush_routes(oldest); 599 *oldest_p = oldest->fnhe_next; 600 kfree_rcu(oldest, rcu); 601 } 602 603 static u32 fnhe_hashfun(__be32 daddr) 604 { 605 static siphash_aligned_key_t fnhe_hash_key; 606 u64 hval; 607 608 net_get_random_once(&fnhe_hash_key, sizeof(fnhe_hash_key)); 609 hval = siphash_1u32((__force u32)daddr, &fnhe_hash_key); 610 return hash_64(hval, FNHE_HASH_SHIFT); 611 } 612 613 static void fill_route_from_fnhe(struct rtable *rt, struct fib_nh_exception *fnhe) 614 { 615 rt->rt_pmtu = fnhe->fnhe_pmtu; 616 rt->rt_mtu_locked = fnhe->fnhe_mtu_locked; 617 rt->dst.expires = fnhe->fnhe_expires; 618 619 if (fnhe->fnhe_gw) { 620 rt->rt_flags |= RTCF_REDIRECTED; 621 rt->rt_uses_gateway = 1; 622 rt->rt_gw_family = AF_INET; 623 rt->rt_gw4 = fnhe->fnhe_gw; 624 } 625 } 626 627 static void update_or_create_fnhe(struct fib_nh_common *nhc, __be32 daddr, 628 __be32 gw, u32 pmtu, bool lock, 629 unsigned long expires) 630 { 631 struct fnhe_hash_bucket *hash; 632 struct fib_nh_exception *fnhe; 633 struct rtable *rt; 634 u32 genid, hval; 635 unsigned int i; 636 int depth; 637 638 genid = fnhe_genid(dev_net(nhc->nhc_dev)); 639 hval = fnhe_hashfun(daddr); 640 641 spin_lock_bh(&fnhe_lock); 642 643 hash = rcu_dereference(nhc->nhc_exceptions); 644 if (!hash) { 645 hash = kcalloc(FNHE_HASH_SIZE, sizeof(*hash), GFP_ATOMIC); 646 if (!hash) 647 goto out_unlock; 648 rcu_assign_pointer(nhc->nhc_exceptions, hash); 649 } 650 651 hash += hval; 652 653 depth = 0; 654 for (fnhe = rcu_dereference(hash->chain); fnhe; 655 fnhe = rcu_dereference(fnhe->fnhe_next)) { 656 if (fnhe->fnhe_daddr == daddr) 657 break; 658 depth++; 659 } 660 661 if (fnhe) { 662 if (fnhe->fnhe_genid != genid) 663 fnhe->fnhe_genid = genid; 664 if (gw) 665 fnhe->fnhe_gw = gw; 666 if (pmtu) { 667 fnhe->fnhe_pmtu = pmtu; 668 fnhe->fnhe_mtu_locked = lock; 669 } 670 fnhe->fnhe_expires = max(1UL, expires); 671 /* Update all cached dsts too */ 672 rt = rcu_dereference(fnhe->fnhe_rth_input); 673 if (rt) 674 fill_route_from_fnhe(rt, fnhe); 675 rt = rcu_dereference(fnhe->fnhe_rth_output); 676 if (rt) 677 fill_route_from_fnhe(rt, fnhe); 678 } else { 679 /* Randomize max depth to avoid some side channels attacks. */ 680 int max_depth = FNHE_RECLAIM_DEPTH + 681 prandom_u32_max(FNHE_RECLAIM_DEPTH); 682 683 while (depth > max_depth) { 684 fnhe_remove_oldest(hash); 685 depth--; 686 } 687 688 fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC); 689 if (!fnhe) 690 goto out_unlock; 691 692 fnhe->fnhe_next = hash->chain; 693 694 fnhe->fnhe_genid = genid; 695 fnhe->fnhe_daddr = daddr; 696 fnhe->fnhe_gw = gw; 697 fnhe->fnhe_pmtu = pmtu; 698 fnhe->fnhe_mtu_locked = lock; 699 fnhe->fnhe_expires = max(1UL, expires); 700 701 rcu_assign_pointer(hash->chain, fnhe); 702 703 /* Exception created; mark the cached routes for the nexthop 704 * stale, so anyone caching it rechecks if this exception 705 * applies to them. 706 */ 707 rt = rcu_dereference(nhc->nhc_rth_input); 708 if (rt) 709 rt->dst.obsolete = DST_OBSOLETE_KILL; 710 711 for_each_possible_cpu(i) { 712 struct rtable __rcu **prt; 713 714 prt = per_cpu_ptr(nhc->nhc_pcpu_rth_output, i); 715 rt = rcu_dereference(*prt); 716 if (rt) 717 rt->dst.obsolete = DST_OBSOLETE_KILL; 718 } 719 } 720 721 fnhe->fnhe_stamp = jiffies; 722 723 out_unlock: 724 spin_unlock_bh(&fnhe_lock); 725 } 726 727 static void __ip_do_redirect(struct rtable *rt, struct sk_buff *skb, struct flowi4 *fl4, 728 bool kill_route) 729 { 730 __be32 new_gw = icmp_hdr(skb)->un.gateway; 731 __be32 old_gw = ip_hdr(skb)->saddr; 732 struct net_device *dev = skb->dev; 733 struct in_device *in_dev; 734 struct fib_result res; 735 struct neighbour *n; 736 struct net *net; 737 738 switch (icmp_hdr(skb)->code & 7) { 739 case ICMP_REDIR_NET: 740 case ICMP_REDIR_NETTOS: 741 case ICMP_REDIR_HOST: 742 case ICMP_REDIR_HOSTTOS: 743 break; 744 745 default: 746 return; 747 } 748 749 if (rt->rt_gw_family != AF_INET || rt->rt_gw4 != old_gw) 750 return; 751 752 in_dev = __in_dev_get_rcu(dev); 753 if (!in_dev) 754 return; 755 756 net = dev_net(dev); 757 if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) || 758 ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) || 759 ipv4_is_zeronet(new_gw)) 760 goto reject_redirect; 761 762 if (!IN_DEV_SHARED_MEDIA(in_dev)) { 763 if (!inet_addr_onlink(in_dev, new_gw, old_gw)) 764 goto reject_redirect; 765 if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev)) 766 goto reject_redirect; 767 } else { 768 if (inet_addr_type(net, new_gw) != RTN_UNICAST) 769 goto reject_redirect; 770 } 771 772 n = __ipv4_neigh_lookup(rt->dst.dev, new_gw); 773 if (!n) 774 n = neigh_create(&arp_tbl, &new_gw, rt->dst.dev); 775 if (!IS_ERR(n)) { 776 if (!(n->nud_state & NUD_VALID)) { 777 neigh_event_send(n, NULL); 778 } else { 779 if (fib_lookup(net, fl4, &res, 0) == 0) { 780 struct fib_nh_common *nhc; 781 782 fib_select_path(net, &res, fl4, skb); 783 nhc = FIB_RES_NHC(res); 784 update_or_create_fnhe(nhc, fl4->daddr, new_gw, 785 0, false, 786 jiffies + ip_rt_gc_timeout); 787 } 788 if (kill_route) 789 rt->dst.obsolete = DST_OBSOLETE_KILL; 790 call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, n); 791 } 792 neigh_release(n); 793 } 794 return; 795 796 reject_redirect: 797 #ifdef CONFIG_IP_ROUTE_VERBOSE 798 if (IN_DEV_LOG_MARTIANS(in_dev)) { 799 const struct iphdr *iph = (const struct iphdr *) skb->data; 800 __be32 daddr = iph->daddr; 801 __be32 saddr = iph->saddr; 802 803 net_info_ratelimited("Redirect from %pI4 on %s about %pI4 ignored\n" 804 " Advised path = %pI4 -> %pI4\n", 805 &old_gw, dev->name, &new_gw, 806 &saddr, &daddr); 807 } 808 #endif 809 ; 810 } 811 812 static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 813 { 814 struct rtable *rt; 815 struct flowi4 fl4; 816 const struct iphdr *iph = (const struct iphdr *) skb->data; 817 struct net *net = dev_net(skb->dev); 818 int oif = skb->dev->ifindex; 819 u8 tos = RT_TOS(iph->tos); 820 u8 prot = iph->protocol; 821 u32 mark = skb->mark; 822 823 rt = (struct rtable *) dst; 824 825 __build_flow_key(net, &fl4, sk, iph, oif, tos, prot, mark, 0); 826 __ip_do_redirect(rt, skb, &fl4, true); 827 } 828 829 static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst) 830 { 831 struct rtable *rt = (struct rtable *)dst; 832 struct dst_entry *ret = dst; 833 834 if (rt) { 835 if (dst->obsolete > 0) { 836 ip_rt_put(rt); 837 ret = NULL; 838 } else if ((rt->rt_flags & RTCF_REDIRECTED) || 839 rt->dst.expires) { 840 ip_rt_put(rt); 841 ret = NULL; 842 } 843 } 844 return ret; 845 } 846 847 /* 848 * Algorithm: 849 * 1. The first ip_rt_redirect_number redirects are sent 850 * with exponential backoff, then we stop sending them at all, 851 * assuming that the host ignores our redirects. 852 * 2. If we did not see packets requiring redirects 853 * during ip_rt_redirect_silence, we assume that the host 854 * forgot redirected route and start to send redirects again. 855 * 856 * This algorithm is much cheaper and more intelligent than dumb load limiting 857 * in icmp.c. 858 * 859 * NOTE. Do not forget to inhibit load limiting for redirects (redundant) 860 * and "frag. need" (breaks PMTU discovery) in icmp.c. 861 */ 862 863 void ip_rt_send_redirect(struct sk_buff *skb) 864 { 865 struct rtable *rt = skb_rtable(skb); 866 struct in_device *in_dev; 867 struct inet_peer *peer; 868 struct net *net; 869 int log_martians; 870 int vif; 871 872 rcu_read_lock(); 873 in_dev = __in_dev_get_rcu(rt->dst.dev); 874 if (!in_dev || !IN_DEV_TX_REDIRECTS(in_dev)) { 875 rcu_read_unlock(); 876 return; 877 } 878 log_martians = IN_DEV_LOG_MARTIANS(in_dev); 879 vif = l3mdev_master_ifindex_rcu(rt->dst.dev); 880 rcu_read_unlock(); 881 882 net = dev_net(rt->dst.dev); 883 peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, vif, 1); 884 if (!peer) { 885 icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, 886 rt_nexthop(rt, ip_hdr(skb)->daddr)); 887 return; 888 } 889 890 /* No redirected packets during ip_rt_redirect_silence; 891 * reset the algorithm. 892 */ 893 if (time_after(jiffies, peer->rate_last + ip_rt_redirect_silence)) { 894 peer->rate_tokens = 0; 895 peer->n_redirects = 0; 896 } 897 898 /* Too many ignored redirects; do not send anything 899 * set dst.rate_last to the last seen redirected packet. 900 */ 901 if (peer->n_redirects >= ip_rt_redirect_number) { 902 peer->rate_last = jiffies; 903 goto out_put_peer; 904 } 905 906 /* Check for load limit; set rate_last to the latest sent 907 * redirect. 908 */ 909 if (peer->n_redirects == 0 || 910 time_after(jiffies, 911 (peer->rate_last + 912 (ip_rt_redirect_load << peer->n_redirects)))) { 913 __be32 gw = rt_nexthop(rt, ip_hdr(skb)->daddr); 914 915 icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, gw); 916 peer->rate_last = jiffies; 917 ++peer->n_redirects; 918 #ifdef CONFIG_IP_ROUTE_VERBOSE 919 if (log_martians && 920 peer->n_redirects == ip_rt_redirect_number) 921 net_warn_ratelimited("host %pI4/if%d ignores redirects for %pI4 to %pI4\n", 922 &ip_hdr(skb)->saddr, inet_iif(skb), 923 &ip_hdr(skb)->daddr, &gw); 924 #endif 925 } 926 out_put_peer: 927 inet_putpeer(peer); 928 } 929 930 static int ip_error(struct sk_buff *skb) 931 { 932 struct rtable *rt = skb_rtable(skb); 933 struct net_device *dev = skb->dev; 934 struct in_device *in_dev; 935 struct inet_peer *peer; 936 unsigned long now; 937 struct net *net; 938 bool send; 939 int code; 940 941 if (netif_is_l3_master(skb->dev)) { 942 dev = __dev_get_by_index(dev_net(skb->dev), IPCB(skb)->iif); 943 if (!dev) 944 goto out; 945 } 946 947 in_dev = __in_dev_get_rcu(dev); 948 949 /* IP on this device is disabled. */ 950 if (!in_dev) 951 goto out; 952 953 net = dev_net(rt->dst.dev); 954 if (!IN_DEV_FORWARD(in_dev)) { 955 switch (rt->dst.error) { 956 case EHOSTUNREACH: 957 __IP_INC_STATS(net, IPSTATS_MIB_INADDRERRORS); 958 break; 959 960 case ENETUNREACH: 961 __IP_INC_STATS(net, IPSTATS_MIB_INNOROUTES); 962 break; 963 } 964 goto out; 965 } 966 967 switch (rt->dst.error) { 968 case EINVAL: 969 default: 970 goto out; 971 case EHOSTUNREACH: 972 code = ICMP_HOST_UNREACH; 973 break; 974 case ENETUNREACH: 975 code = ICMP_NET_UNREACH; 976 __IP_INC_STATS(net, IPSTATS_MIB_INNOROUTES); 977 break; 978 case EACCES: 979 code = ICMP_PKT_FILTERED; 980 break; 981 } 982 983 peer = inet_getpeer_v4(net->ipv4.peers, ip_hdr(skb)->saddr, 984 l3mdev_master_ifindex(skb->dev), 1); 985 986 send = true; 987 if (peer) { 988 now = jiffies; 989 peer->rate_tokens += now - peer->rate_last; 990 if (peer->rate_tokens > ip_rt_error_burst) 991 peer->rate_tokens = ip_rt_error_burst; 992 peer->rate_last = now; 993 if (peer->rate_tokens >= ip_rt_error_cost) 994 peer->rate_tokens -= ip_rt_error_cost; 995 else 996 send = false; 997 inet_putpeer(peer); 998 } 999 if (send) 1000 icmp_send(skb, ICMP_DEST_UNREACH, code, 0); 1001 1002 out: kfree_skb(skb); 1003 return 0; 1004 } 1005 1006 static void __ip_rt_update_pmtu(struct rtable *rt, struct flowi4 *fl4, u32 mtu) 1007 { 1008 struct dst_entry *dst = &rt->dst; 1009 struct net *net = dev_net(dst->dev); 1010 struct fib_result res; 1011 bool lock = false; 1012 u32 old_mtu; 1013 1014 if (ip_mtu_locked(dst)) 1015 return; 1016 1017 old_mtu = ipv4_mtu(dst); 1018 if (old_mtu < mtu) 1019 return; 1020 1021 if (mtu < ip_rt_min_pmtu) { 1022 lock = true; 1023 mtu = min(old_mtu, ip_rt_min_pmtu); 1024 } 1025 1026 if (rt->rt_pmtu == mtu && !lock && 1027 time_before(jiffies, dst->expires - ip_rt_mtu_expires / 2)) 1028 return; 1029 1030 rcu_read_lock(); 1031 if (fib_lookup(net, fl4, &res, 0) == 0) { 1032 struct fib_nh_common *nhc; 1033 1034 fib_select_path(net, &res, fl4, NULL); 1035 nhc = FIB_RES_NHC(res); 1036 update_or_create_fnhe(nhc, fl4->daddr, 0, mtu, lock, 1037 jiffies + ip_rt_mtu_expires); 1038 } 1039 rcu_read_unlock(); 1040 } 1041 1042 static void ip_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 1043 struct sk_buff *skb, u32 mtu, 1044 bool confirm_neigh) 1045 { 1046 struct rtable *rt = (struct rtable *) dst; 1047 struct flowi4 fl4; 1048 1049 ip_rt_build_flow_key(&fl4, sk, skb); 1050 1051 /* Don't make lookup fail for bridged encapsulations */ 1052 if (skb && netif_is_any_bridge_port(skb->dev)) 1053 fl4.flowi4_oif = 0; 1054 1055 __ip_rt_update_pmtu(rt, &fl4, mtu); 1056 } 1057 1058 void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu, 1059 int oif, u8 protocol) 1060 { 1061 const struct iphdr *iph = (const struct iphdr *)skb->data; 1062 struct flowi4 fl4; 1063 struct rtable *rt; 1064 u32 mark = IP4_REPLY_MARK(net, skb->mark); 1065 1066 __build_flow_key(net, &fl4, NULL, iph, oif, 1067 RT_TOS(iph->tos), protocol, mark, 0); 1068 rt = __ip_route_output_key(net, &fl4); 1069 if (!IS_ERR(rt)) { 1070 __ip_rt_update_pmtu(rt, &fl4, mtu); 1071 ip_rt_put(rt); 1072 } 1073 } 1074 EXPORT_SYMBOL_GPL(ipv4_update_pmtu); 1075 1076 static void __ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu) 1077 { 1078 const struct iphdr *iph = (const struct iphdr *)skb->data; 1079 struct flowi4 fl4; 1080 struct rtable *rt; 1081 1082 __build_flow_key(sock_net(sk), &fl4, sk, iph, 0, 0, 0, 0, 0); 1083 1084 if (!fl4.flowi4_mark) 1085 fl4.flowi4_mark = IP4_REPLY_MARK(sock_net(sk), skb->mark); 1086 1087 rt = __ip_route_output_key(sock_net(sk), &fl4); 1088 if (!IS_ERR(rt)) { 1089 __ip_rt_update_pmtu(rt, &fl4, mtu); 1090 ip_rt_put(rt); 1091 } 1092 } 1093 1094 void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu) 1095 { 1096 const struct iphdr *iph = (const struct iphdr *)skb->data; 1097 struct flowi4 fl4; 1098 struct rtable *rt; 1099 struct dst_entry *odst = NULL; 1100 bool new = false; 1101 struct net *net = sock_net(sk); 1102 1103 bh_lock_sock(sk); 1104 1105 if (!ip_sk_accept_pmtu(sk)) 1106 goto out; 1107 1108 odst = sk_dst_get(sk); 1109 1110 if (sock_owned_by_user(sk) || !odst) { 1111 __ipv4_sk_update_pmtu(skb, sk, mtu); 1112 goto out; 1113 } 1114 1115 __build_flow_key(net, &fl4, sk, iph, 0, 0, 0, 0, 0); 1116 1117 rt = (struct rtable *)odst; 1118 if (odst->obsolete && !odst->ops->check(odst, 0)) { 1119 rt = ip_route_output_flow(sock_net(sk), &fl4, sk); 1120 if (IS_ERR(rt)) 1121 goto out; 1122 1123 new = true; 1124 } 1125 1126 __ip_rt_update_pmtu((struct rtable *)xfrm_dst_path(&rt->dst), &fl4, mtu); 1127 1128 if (!dst_check(&rt->dst, 0)) { 1129 if (new) 1130 dst_release(&rt->dst); 1131 1132 rt = ip_route_output_flow(sock_net(sk), &fl4, sk); 1133 if (IS_ERR(rt)) 1134 goto out; 1135 1136 new = true; 1137 } 1138 1139 if (new) 1140 sk_dst_set(sk, &rt->dst); 1141 1142 out: 1143 bh_unlock_sock(sk); 1144 dst_release(odst); 1145 } 1146 EXPORT_SYMBOL_GPL(ipv4_sk_update_pmtu); 1147 1148 void ipv4_redirect(struct sk_buff *skb, struct net *net, 1149 int oif, u8 protocol) 1150 { 1151 const struct iphdr *iph = (const struct iphdr *)skb->data; 1152 struct flowi4 fl4; 1153 struct rtable *rt; 1154 1155 __build_flow_key(net, &fl4, NULL, iph, oif, 1156 RT_TOS(iph->tos), protocol, 0, 0); 1157 rt = __ip_route_output_key(net, &fl4); 1158 if (!IS_ERR(rt)) { 1159 __ip_do_redirect(rt, skb, &fl4, false); 1160 ip_rt_put(rt); 1161 } 1162 } 1163 EXPORT_SYMBOL_GPL(ipv4_redirect); 1164 1165 void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk) 1166 { 1167 const struct iphdr *iph = (const struct iphdr *)skb->data; 1168 struct flowi4 fl4; 1169 struct rtable *rt; 1170 struct net *net = sock_net(sk); 1171 1172 __build_flow_key(net, &fl4, sk, iph, 0, 0, 0, 0, 0); 1173 rt = __ip_route_output_key(net, &fl4); 1174 if (!IS_ERR(rt)) { 1175 __ip_do_redirect(rt, skb, &fl4, false); 1176 ip_rt_put(rt); 1177 } 1178 } 1179 EXPORT_SYMBOL_GPL(ipv4_sk_redirect); 1180 1181 INDIRECT_CALLABLE_SCOPE struct dst_entry *ipv4_dst_check(struct dst_entry *dst, 1182 u32 cookie) 1183 { 1184 struct rtable *rt = (struct rtable *) dst; 1185 1186 /* All IPV4 dsts are created with ->obsolete set to the value 1187 * DST_OBSOLETE_FORCE_CHK which forces validation calls down 1188 * into this function always. 1189 * 1190 * When a PMTU/redirect information update invalidates a route, 1191 * this is indicated by setting obsolete to DST_OBSOLETE_KILL or 1192 * DST_OBSOLETE_DEAD. 1193 */ 1194 if (dst->obsolete != DST_OBSOLETE_FORCE_CHK || rt_is_expired(rt)) 1195 return NULL; 1196 return dst; 1197 } 1198 EXPORT_INDIRECT_CALLABLE(ipv4_dst_check); 1199 1200 static void ipv4_send_dest_unreach(struct sk_buff *skb) 1201 { 1202 struct ip_options opt; 1203 int res; 1204 1205 /* Recompile ip options since IPCB may not be valid anymore. 1206 * Also check we have a reasonable ipv4 header. 1207 */ 1208 if (!pskb_network_may_pull(skb, sizeof(struct iphdr)) || 1209 ip_hdr(skb)->version != 4 || ip_hdr(skb)->ihl < 5) 1210 return; 1211 1212 memset(&opt, 0, sizeof(opt)); 1213 if (ip_hdr(skb)->ihl > 5) { 1214 if (!pskb_network_may_pull(skb, ip_hdr(skb)->ihl * 4)) 1215 return; 1216 opt.optlen = ip_hdr(skb)->ihl * 4 - sizeof(struct iphdr); 1217 1218 rcu_read_lock(); 1219 res = __ip_options_compile(dev_net(skb->dev), &opt, skb, NULL); 1220 rcu_read_unlock(); 1221 1222 if (res) 1223 return; 1224 } 1225 __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0, &opt); 1226 } 1227 1228 static void ipv4_link_failure(struct sk_buff *skb) 1229 { 1230 struct rtable *rt; 1231 1232 ipv4_send_dest_unreach(skb); 1233 1234 rt = skb_rtable(skb); 1235 if (rt) 1236 dst_set_expires(&rt->dst, 0); 1237 } 1238 1239 static int ip_rt_bug(struct net *net, struct sock *sk, struct sk_buff *skb) 1240 { 1241 pr_debug("%s: %pI4 -> %pI4, %s\n", 1242 __func__, &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, 1243 skb->dev ? skb->dev->name : "?"); 1244 kfree_skb(skb); 1245 WARN_ON(1); 1246 return 0; 1247 } 1248 1249 /* 1250 * We do not cache source address of outgoing interface, 1251 * because it is used only by IP RR, TS and SRR options, 1252 * so that it out of fast path. 1253 * 1254 * BTW remember: "addr" is allowed to be not aligned 1255 * in IP options! 1256 */ 1257 1258 void ip_rt_get_source(u8 *addr, struct sk_buff *skb, struct rtable *rt) 1259 { 1260 __be32 src; 1261 1262 if (rt_is_output_route(rt)) 1263 src = ip_hdr(skb)->saddr; 1264 else { 1265 struct fib_result res; 1266 struct iphdr *iph = ip_hdr(skb); 1267 struct flowi4 fl4 = { 1268 .daddr = iph->daddr, 1269 .saddr = iph->saddr, 1270 .flowi4_tos = RT_TOS(iph->tos), 1271 .flowi4_oif = rt->dst.dev->ifindex, 1272 .flowi4_iif = skb->dev->ifindex, 1273 .flowi4_mark = skb->mark, 1274 }; 1275 1276 rcu_read_lock(); 1277 if (fib_lookup(dev_net(rt->dst.dev), &fl4, &res, 0) == 0) 1278 src = fib_result_prefsrc(dev_net(rt->dst.dev), &res); 1279 else 1280 src = inet_select_addr(rt->dst.dev, 1281 rt_nexthop(rt, iph->daddr), 1282 RT_SCOPE_UNIVERSE); 1283 rcu_read_unlock(); 1284 } 1285 memcpy(addr, &src, 4); 1286 } 1287 1288 #ifdef CONFIG_IP_ROUTE_CLASSID 1289 static void set_class_tag(struct rtable *rt, u32 tag) 1290 { 1291 if (!(rt->dst.tclassid & 0xFFFF)) 1292 rt->dst.tclassid |= tag & 0xFFFF; 1293 if (!(rt->dst.tclassid & 0xFFFF0000)) 1294 rt->dst.tclassid |= tag & 0xFFFF0000; 1295 } 1296 #endif 1297 1298 static unsigned int ipv4_default_advmss(const struct dst_entry *dst) 1299 { 1300 unsigned int header_size = sizeof(struct tcphdr) + sizeof(struct iphdr); 1301 unsigned int advmss = max_t(unsigned int, ipv4_mtu(dst) - header_size, 1302 ip_rt_min_advmss); 1303 1304 return min(advmss, IPV4_MAX_PMTU - header_size); 1305 } 1306 1307 INDIRECT_CALLABLE_SCOPE unsigned int ipv4_mtu(const struct dst_entry *dst) 1308 { 1309 return ip_dst_mtu_maybe_forward(dst, false); 1310 } 1311 EXPORT_INDIRECT_CALLABLE(ipv4_mtu); 1312 1313 static void ip_del_fnhe(struct fib_nh_common *nhc, __be32 daddr) 1314 { 1315 struct fnhe_hash_bucket *hash; 1316 struct fib_nh_exception *fnhe, __rcu **fnhe_p; 1317 u32 hval = fnhe_hashfun(daddr); 1318 1319 spin_lock_bh(&fnhe_lock); 1320 1321 hash = rcu_dereference_protected(nhc->nhc_exceptions, 1322 lockdep_is_held(&fnhe_lock)); 1323 hash += hval; 1324 1325 fnhe_p = &hash->chain; 1326 fnhe = rcu_dereference_protected(*fnhe_p, lockdep_is_held(&fnhe_lock)); 1327 while (fnhe) { 1328 if (fnhe->fnhe_daddr == daddr) { 1329 rcu_assign_pointer(*fnhe_p, rcu_dereference_protected( 1330 fnhe->fnhe_next, lockdep_is_held(&fnhe_lock))); 1331 /* set fnhe_daddr to 0 to ensure it won't bind with 1332 * new dsts in rt_bind_exception(). 1333 */ 1334 fnhe->fnhe_daddr = 0; 1335 fnhe_flush_routes(fnhe); 1336 kfree_rcu(fnhe, rcu); 1337 break; 1338 } 1339 fnhe_p = &fnhe->fnhe_next; 1340 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1341 lockdep_is_held(&fnhe_lock)); 1342 } 1343 1344 spin_unlock_bh(&fnhe_lock); 1345 } 1346 1347 static struct fib_nh_exception *find_exception(struct fib_nh_common *nhc, 1348 __be32 daddr) 1349 { 1350 struct fnhe_hash_bucket *hash = rcu_dereference(nhc->nhc_exceptions); 1351 struct fib_nh_exception *fnhe; 1352 u32 hval; 1353 1354 if (!hash) 1355 return NULL; 1356 1357 hval = fnhe_hashfun(daddr); 1358 1359 for (fnhe = rcu_dereference(hash[hval].chain); fnhe; 1360 fnhe = rcu_dereference(fnhe->fnhe_next)) { 1361 if (fnhe->fnhe_daddr == daddr) { 1362 if (fnhe->fnhe_expires && 1363 time_after(jiffies, fnhe->fnhe_expires)) { 1364 ip_del_fnhe(nhc, daddr); 1365 break; 1366 } 1367 return fnhe; 1368 } 1369 } 1370 return NULL; 1371 } 1372 1373 /* MTU selection: 1374 * 1. mtu on route is locked - use it 1375 * 2. mtu from nexthop exception 1376 * 3. mtu from egress device 1377 */ 1378 1379 u32 ip_mtu_from_fib_result(struct fib_result *res, __be32 daddr) 1380 { 1381 struct fib_nh_common *nhc = res->nhc; 1382 struct net_device *dev = nhc->nhc_dev; 1383 struct fib_info *fi = res->fi; 1384 u32 mtu = 0; 1385 1386 if (dev_net(dev)->ipv4.sysctl_ip_fwd_use_pmtu || 1387 fi->fib_metrics->metrics[RTAX_LOCK - 1] & (1 << RTAX_MTU)) 1388 mtu = fi->fib_mtu; 1389 1390 if (likely(!mtu)) { 1391 struct fib_nh_exception *fnhe; 1392 1393 fnhe = find_exception(nhc, daddr); 1394 if (fnhe && !time_after_eq(jiffies, fnhe->fnhe_expires)) 1395 mtu = fnhe->fnhe_pmtu; 1396 } 1397 1398 if (likely(!mtu)) 1399 mtu = min(READ_ONCE(dev->mtu), IP_MAX_MTU); 1400 1401 return mtu - lwtunnel_headroom(nhc->nhc_lwtstate, mtu); 1402 } 1403 1404 static bool rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe, 1405 __be32 daddr, const bool do_cache) 1406 { 1407 bool ret = false; 1408 1409 spin_lock_bh(&fnhe_lock); 1410 1411 if (daddr == fnhe->fnhe_daddr) { 1412 struct rtable __rcu **porig; 1413 struct rtable *orig; 1414 int genid = fnhe_genid(dev_net(rt->dst.dev)); 1415 1416 if (rt_is_input_route(rt)) 1417 porig = &fnhe->fnhe_rth_input; 1418 else 1419 porig = &fnhe->fnhe_rth_output; 1420 orig = rcu_dereference(*porig); 1421 1422 if (fnhe->fnhe_genid != genid) { 1423 fnhe->fnhe_genid = genid; 1424 fnhe->fnhe_gw = 0; 1425 fnhe->fnhe_pmtu = 0; 1426 fnhe->fnhe_expires = 0; 1427 fnhe->fnhe_mtu_locked = false; 1428 fnhe_flush_routes(fnhe); 1429 orig = NULL; 1430 } 1431 fill_route_from_fnhe(rt, fnhe); 1432 if (!rt->rt_gw4) { 1433 rt->rt_gw4 = daddr; 1434 rt->rt_gw_family = AF_INET; 1435 } 1436 1437 if (do_cache) { 1438 dst_hold(&rt->dst); 1439 rcu_assign_pointer(*porig, rt); 1440 if (orig) { 1441 dst_dev_put(&orig->dst); 1442 dst_release(&orig->dst); 1443 } 1444 ret = true; 1445 } 1446 1447 fnhe->fnhe_stamp = jiffies; 1448 } 1449 spin_unlock_bh(&fnhe_lock); 1450 1451 return ret; 1452 } 1453 1454 static bool rt_cache_route(struct fib_nh_common *nhc, struct rtable *rt) 1455 { 1456 struct rtable *orig, *prev, **p; 1457 bool ret = true; 1458 1459 if (rt_is_input_route(rt)) { 1460 p = (struct rtable **)&nhc->nhc_rth_input; 1461 } else { 1462 p = (struct rtable **)raw_cpu_ptr(nhc->nhc_pcpu_rth_output); 1463 } 1464 orig = *p; 1465 1466 /* hold dst before doing cmpxchg() to avoid race condition 1467 * on this dst 1468 */ 1469 dst_hold(&rt->dst); 1470 prev = cmpxchg(p, orig, rt); 1471 if (prev == orig) { 1472 if (orig) { 1473 rt_add_uncached_list(orig); 1474 dst_release(&orig->dst); 1475 } 1476 } else { 1477 dst_release(&rt->dst); 1478 ret = false; 1479 } 1480 1481 return ret; 1482 } 1483 1484 struct uncached_list { 1485 spinlock_t lock; 1486 struct list_head head; 1487 }; 1488 1489 static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt_uncached_list); 1490 1491 void rt_add_uncached_list(struct rtable *rt) 1492 { 1493 struct uncached_list *ul = raw_cpu_ptr(&rt_uncached_list); 1494 1495 rt->rt_uncached_list = ul; 1496 1497 spin_lock_bh(&ul->lock); 1498 list_add_tail(&rt->rt_uncached, &ul->head); 1499 spin_unlock_bh(&ul->lock); 1500 } 1501 1502 void rt_del_uncached_list(struct rtable *rt) 1503 { 1504 if (!list_empty(&rt->rt_uncached)) { 1505 struct uncached_list *ul = rt->rt_uncached_list; 1506 1507 spin_lock_bh(&ul->lock); 1508 list_del(&rt->rt_uncached); 1509 spin_unlock_bh(&ul->lock); 1510 } 1511 } 1512 1513 static void ipv4_dst_destroy(struct dst_entry *dst) 1514 { 1515 struct rtable *rt = (struct rtable *)dst; 1516 1517 ip_dst_metrics_put(dst); 1518 rt_del_uncached_list(rt); 1519 } 1520 1521 void rt_flush_dev(struct net_device *dev) 1522 { 1523 struct rtable *rt; 1524 int cpu; 1525 1526 for_each_possible_cpu(cpu) { 1527 struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu); 1528 1529 spin_lock_bh(&ul->lock); 1530 list_for_each_entry(rt, &ul->head, rt_uncached) { 1531 if (rt->dst.dev != dev) 1532 continue; 1533 rt->dst.dev = blackhole_netdev; 1534 dev_replace_track(dev, blackhole_netdev, 1535 &rt->dst.dev_tracker, 1536 GFP_ATOMIC); 1537 } 1538 spin_unlock_bh(&ul->lock); 1539 } 1540 } 1541 1542 static bool rt_cache_valid(const struct rtable *rt) 1543 { 1544 return rt && 1545 rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 1546 !rt_is_expired(rt); 1547 } 1548 1549 static void rt_set_nexthop(struct rtable *rt, __be32 daddr, 1550 const struct fib_result *res, 1551 struct fib_nh_exception *fnhe, 1552 struct fib_info *fi, u16 type, u32 itag, 1553 const bool do_cache) 1554 { 1555 bool cached = false; 1556 1557 if (fi) { 1558 struct fib_nh_common *nhc = FIB_RES_NHC(*res); 1559 1560 if (nhc->nhc_gw_family && nhc->nhc_scope == RT_SCOPE_LINK) { 1561 rt->rt_uses_gateway = 1; 1562 rt->rt_gw_family = nhc->nhc_gw_family; 1563 /* only INET and INET6 are supported */ 1564 if (likely(nhc->nhc_gw_family == AF_INET)) 1565 rt->rt_gw4 = nhc->nhc_gw.ipv4; 1566 else 1567 rt->rt_gw6 = nhc->nhc_gw.ipv6; 1568 } 1569 1570 ip_dst_init_metrics(&rt->dst, fi->fib_metrics); 1571 1572 #ifdef CONFIG_IP_ROUTE_CLASSID 1573 if (nhc->nhc_family == AF_INET) { 1574 struct fib_nh *nh; 1575 1576 nh = container_of(nhc, struct fib_nh, nh_common); 1577 rt->dst.tclassid = nh->nh_tclassid; 1578 } 1579 #endif 1580 rt->dst.lwtstate = lwtstate_get(nhc->nhc_lwtstate); 1581 if (unlikely(fnhe)) 1582 cached = rt_bind_exception(rt, fnhe, daddr, do_cache); 1583 else if (do_cache) 1584 cached = rt_cache_route(nhc, rt); 1585 if (unlikely(!cached)) { 1586 /* Routes we intend to cache in nexthop exception or 1587 * FIB nexthop have the DST_NOCACHE bit clear. 1588 * However, if we are unsuccessful at storing this 1589 * route into the cache we really need to set it. 1590 */ 1591 if (!rt->rt_gw4) { 1592 rt->rt_gw_family = AF_INET; 1593 rt->rt_gw4 = daddr; 1594 } 1595 rt_add_uncached_list(rt); 1596 } 1597 } else 1598 rt_add_uncached_list(rt); 1599 1600 #ifdef CONFIG_IP_ROUTE_CLASSID 1601 #ifdef CONFIG_IP_MULTIPLE_TABLES 1602 set_class_tag(rt, res->tclassid); 1603 #endif 1604 set_class_tag(rt, itag); 1605 #endif 1606 } 1607 1608 struct rtable *rt_dst_alloc(struct net_device *dev, 1609 unsigned int flags, u16 type, 1610 bool nopolicy, bool noxfrm) 1611 { 1612 struct rtable *rt; 1613 1614 rt = dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK, 1615 (nopolicy ? DST_NOPOLICY : 0) | 1616 (noxfrm ? DST_NOXFRM : 0)); 1617 1618 if (rt) { 1619 rt->rt_genid = rt_genid_ipv4(dev_net(dev)); 1620 rt->rt_flags = flags; 1621 rt->rt_type = type; 1622 rt->rt_is_input = 0; 1623 rt->rt_iif = 0; 1624 rt->rt_pmtu = 0; 1625 rt->rt_mtu_locked = 0; 1626 rt->rt_uses_gateway = 0; 1627 rt->rt_gw_family = 0; 1628 rt->rt_gw4 = 0; 1629 INIT_LIST_HEAD(&rt->rt_uncached); 1630 1631 rt->dst.output = ip_output; 1632 if (flags & RTCF_LOCAL) 1633 rt->dst.input = ip_local_deliver; 1634 } 1635 1636 return rt; 1637 } 1638 EXPORT_SYMBOL(rt_dst_alloc); 1639 1640 struct rtable *rt_dst_clone(struct net_device *dev, struct rtable *rt) 1641 { 1642 struct rtable *new_rt; 1643 1644 new_rt = dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK, 1645 rt->dst.flags); 1646 1647 if (new_rt) { 1648 new_rt->rt_genid = rt_genid_ipv4(dev_net(dev)); 1649 new_rt->rt_flags = rt->rt_flags; 1650 new_rt->rt_type = rt->rt_type; 1651 new_rt->rt_is_input = rt->rt_is_input; 1652 new_rt->rt_iif = rt->rt_iif; 1653 new_rt->rt_pmtu = rt->rt_pmtu; 1654 new_rt->rt_mtu_locked = rt->rt_mtu_locked; 1655 new_rt->rt_gw_family = rt->rt_gw_family; 1656 if (rt->rt_gw_family == AF_INET) 1657 new_rt->rt_gw4 = rt->rt_gw4; 1658 else if (rt->rt_gw_family == AF_INET6) 1659 new_rt->rt_gw6 = rt->rt_gw6; 1660 INIT_LIST_HEAD(&new_rt->rt_uncached); 1661 1662 new_rt->dst.input = rt->dst.input; 1663 new_rt->dst.output = rt->dst.output; 1664 new_rt->dst.error = rt->dst.error; 1665 new_rt->dst.lastuse = jiffies; 1666 new_rt->dst.lwtstate = lwtstate_get(rt->dst.lwtstate); 1667 } 1668 return new_rt; 1669 } 1670 EXPORT_SYMBOL(rt_dst_clone); 1671 1672 /* called in rcu_read_lock() section */ 1673 int ip_mc_validate_source(struct sk_buff *skb, __be32 daddr, __be32 saddr, 1674 u8 tos, struct net_device *dev, 1675 struct in_device *in_dev, u32 *itag) 1676 { 1677 int err; 1678 1679 /* Primary sanity checks. */ 1680 if (!in_dev) 1681 return -EINVAL; 1682 1683 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) || 1684 skb->protocol != htons(ETH_P_IP)) 1685 return -EINVAL; 1686 1687 if (ipv4_is_loopback(saddr) && !IN_DEV_ROUTE_LOCALNET(in_dev)) 1688 return -EINVAL; 1689 1690 if (ipv4_is_zeronet(saddr)) { 1691 if (!ipv4_is_local_multicast(daddr) && 1692 ip_hdr(skb)->protocol != IPPROTO_IGMP) 1693 return -EINVAL; 1694 } else { 1695 err = fib_validate_source(skb, saddr, 0, tos, 0, dev, 1696 in_dev, itag); 1697 if (err < 0) 1698 return err; 1699 } 1700 return 0; 1701 } 1702 1703 /* called in rcu_read_lock() section */ 1704 static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr, 1705 u8 tos, struct net_device *dev, int our) 1706 { 1707 struct in_device *in_dev = __in_dev_get_rcu(dev); 1708 unsigned int flags = RTCF_MULTICAST; 1709 struct rtable *rth; 1710 u32 itag = 0; 1711 int err; 1712 1713 err = ip_mc_validate_source(skb, daddr, saddr, tos, dev, in_dev, &itag); 1714 if (err) 1715 return err; 1716 1717 if (our) 1718 flags |= RTCF_LOCAL; 1719 1720 rth = rt_dst_alloc(dev_net(dev)->loopback_dev, flags, RTN_MULTICAST, 1721 IN_DEV_ORCONF(in_dev, NOPOLICY), false); 1722 if (!rth) 1723 return -ENOBUFS; 1724 1725 #ifdef CONFIG_IP_ROUTE_CLASSID 1726 rth->dst.tclassid = itag; 1727 #endif 1728 rth->dst.output = ip_rt_bug; 1729 rth->rt_is_input= 1; 1730 1731 #ifdef CONFIG_IP_MROUTE 1732 if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev)) 1733 rth->dst.input = ip_mr_input; 1734 #endif 1735 RT_CACHE_STAT_INC(in_slow_mc); 1736 1737 skb_dst_set(skb, &rth->dst); 1738 return 0; 1739 } 1740 1741 1742 static void ip_handle_martian_source(struct net_device *dev, 1743 struct in_device *in_dev, 1744 struct sk_buff *skb, 1745 __be32 daddr, 1746 __be32 saddr) 1747 { 1748 RT_CACHE_STAT_INC(in_martian_src); 1749 #ifdef CONFIG_IP_ROUTE_VERBOSE 1750 if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) { 1751 /* 1752 * RFC1812 recommendation, if source is martian, 1753 * the only hint is MAC header. 1754 */ 1755 pr_warn("martian source %pI4 from %pI4, on dev %s\n", 1756 &daddr, &saddr, dev->name); 1757 if (dev->hard_header_len && skb_mac_header_was_set(skb)) { 1758 print_hex_dump(KERN_WARNING, "ll header: ", 1759 DUMP_PREFIX_OFFSET, 16, 1, 1760 skb_mac_header(skb), 1761 dev->hard_header_len, false); 1762 } 1763 } 1764 #endif 1765 } 1766 1767 /* called in rcu_read_lock() section */ 1768 static int __mkroute_input(struct sk_buff *skb, 1769 const struct fib_result *res, 1770 struct in_device *in_dev, 1771 __be32 daddr, __be32 saddr, u32 tos) 1772 { 1773 struct fib_nh_common *nhc = FIB_RES_NHC(*res); 1774 struct net_device *dev = nhc->nhc_dev; 1775 struct fib_nh_exception *fnhe; 1776 struct rtable *rth; 1777 int err; 1778 struct in_device *out_dev; 1779 bool do_cache; 1780 u32 itag = 0; 1781 1782 /* get a working reference to the output device */ 1783 out_dev = __in_dev_get_rcu(dev); 1784 if (!out_dev) { 1785 net_crit_ratelimited("Bug in ip_route_input_slow(). Please report.\n"); 1786 return -EINVAL; 1787 } 1788 1789 err = fib_validate_source(skb, saddr, daddr, tos, FIB_RES_OIF(*res), 1790 in_dev->dev, in_dev, &itag); 1791 if (err < 0) { 1792 ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr, 1793 saddr); 1794 1795 goto cleanup; 1796 } 1797 1798 do_cache = res->fi && !itag; 1799 if (out_dev == in_dev && err && IN_DEV_TX_REDIRECTS(out_dev) && 1800 skb->protocol == htons(ETH_P_IP)) { 1801 __be32 gw; 1802 1803 gw = nhc->nhc_gw_family == AF_INET ? nhc->nhc_gw.ipv4 : 0; 1804 if (IN_DEV_SHARED_MEDIA(out_dev) || 1805 inet_addr_onlink(out_dev, saddr, gw)) 1806 IPCB(skb)->flags |= IPSKB_DOREDIRECT; 1807 } 1808 1809 if (skb->protocol != htons(ETH_P_IP)) { 1810 /* Not IP (i.e. ARP). Do not create route, if it is 1811 * invalid for proxy arp. DNAT routes are always valid. 1812 * 1813 * Proxy arp feature have been extended to allow, ARP 1814 * replies back to the same interface, to support 1815 * Private VLAN switch technologies. See arp.c. 1816 */ 1817 if (out_dev == in_dev && 1818 IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) { 1819 err = -EINVAL; 1820 goto cleanup; 1821 } 1822 } 1823 1824 fnhe = find_exception(nhc, daddr); 1825 if (do_cache) { 1826 if (fnhe) 1827 rth = rcu_dereference(fnhe->fnhe_rth_input); 1828 else 1829 rth = rcu_dereference(nhc->nhc_rth_input); 1830 if (rt_cache_valid(rth)) { 1831 skb_dst_set_noref(skb, &rth->dst); 1832 goto out; 1833 } 1834 } 1835 1836 rth = rt_dst_alloc(out_dev->dev, 0, res->type, 1837 IN_DEV_ORCONF(in_dev, NOPOLICY), 1838 IN_DEV_ORCONF(out_dev, NOXFRM)); 1839 if (!rth) { 1840 err = -ENOBUFS; 1841 goto cleanup; 1842 } 1843 1844 rth->rt_is_input = 1; 1845 RT_CACHE_STAT_INC(in_slow_tot); 1846 1847 rth->dst.input = ip_forward; 1848 1849 rt_set_nexthop(rth, daddr, res, fnhe, res->fi, res->type, itag, 1850 do_cache); 1851 lwtunnel_set_redirect(&rth->dst); 1852 skb_dst_set(skb, &rth->dst); 1853 out: 1854 err = 0; 1855 cleanup: 1856 return err; 1857 } 1858 1859 #ifdef CONFIG_IP_ROUTE_MULTIPATH 1860 /* To make ICMP packets follow the right flow, the multipath hash is 1861 * calculated from the inner IP addresses. 1862 */ 1863 static void ip_multipath_l3_keys(const struct sk_buff *skb, 1864 struct flow_keys *hash_keys) 1865 { 1866 const struct iphdr *outer_iph = ip_hdr(skb); 1867 const struct iphdr *key_iph = outer_iph; 1868 const struct iphdr *inner_iph; 1869 const struct icmphdr *icmph; 1870 struct iphdr _inner_iph; 1871 struct icmphdr _icmph; 1872 1873 if (likely(outer_iph->protocol != IPPROTO_ICMP)) 1874 goto out; 1875 1876 if (unlikely((outer_iph->frag_off & htons(IP_OFFSET)) != 0)) 1877 goto out; 1878 1879 icmph = skb_header_pointer(skb, outer_iph->ihl * 4, sizeof(_icmph), 1880 &_icmph); 1881 if (!icmph) 1882 goto out; 1883 1884 if (!icmp_is_err(icmph->type)) 1885 goto out; 1886 1887 inner_iph = skb_header_pointer(skb, 1888 outer_iph->ihl * 4 + sizeof(_icmph), 1889 sizeof(_inner_iph), &_inner_iph); 1890 if (!inner_iph) 1891 goto out; 1892 1893 key_iph = inner_iph; 1894 out: 1895 hash_keys->addrs.v4addrs.src = key_iph->saddr; 1896 hash_keys->addrs.v4addrs.dst = key_iph->daddr; 1897 } 1898 1899 static u32 fib_multipath_custom_hash_outer(const struct net *net, 1900 const struct sk_buff *skb, 1901 bool *p_has_inner) 1902 { 1903 u32 hash_fields = net->ipv4.sysctl_fib_multipath_hash_fields; 1904 struct flow_keys keys, hash_keys; 1905 1906 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK)) 1907 return 0; 1908 1909 memset(&hash_keys, 0, sizeof(hash_keys)); 1910 skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP); 1911 1912 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 1913 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP) 1914 hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src; 1915 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP) 1916 hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst; 1917 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO) 1918 hash_keys.basic.ip_proto = keys.basic.ip_proto; 1919 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT) 1920 hash_keys.ports.src = keys.ports.src; 1921 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT) 1922 hash_keys.ports.dst = keys.ports.dst; 1923 1924 *p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION); 1925 return flow_hash_from_keys(&hash_keys); 1926 } 1927 1928 static u32 fib_multipath_custom_hash_inner(const struct net *net, 1929 const struct sk_buff *skb, 1930 bool has_inner) 1931 { 1932 u32 hash_fields = net->ipv4.sysctl_fib_multipath_hash_fields; 1933 struct flow_keys keys, hash_keys; 1934 1935 /* We assume the packet carries an encapsulation, but if none was 1936 * encountered during dissection of the outer flow, then there is no 1937 * point in calling the flow dissector again. 1938 */ 1939 if (!has_inner) 1940 return 0; 1941 1942 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK)) 1943 return 0; 1944 1945 memset(&hash_keys, 0, sizeof(hash_keys)); 1946 skb_flow_dissect_flow_keys(skb, &keys, 0); 1947 1948 if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION)) 1949 return 0; 1950 1951 if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { 1952 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 1953 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP) 1954 hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src; 1955 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP) 1956 hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst; 1957 } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { 1958 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 1959 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP) 1960 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src; 1961 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP) 1962 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst; 1963 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL) 1964 hash_keys.tags.flow_label = keys.tags.flow_label; 1965 } 1966 1967 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO) 1968 hash_keys.basic.ip_proto = keys.basic.ip_proto; 1969 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT) 1970 hash_keys.ports.src = keys.ports.src; 1971 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT) 1972 hash_keys.ports.dst = keys.ports.dst; 1973 1974 return flow_hash_from_keys(&hash_keys); 1975 } 1976 1977 static u32 fib_multipath_custom_hash_skb(const struct net *net, 1978 const struct sk_buff *skb) 1979 { 1980 u32 mhash, mhash_inner; 1981 bool has_inner = true; 1982 1983 mhash = fib_multipath_custom_hash_outer(net, skb, &has_inner); 1984 mhash_inner = fib_multipath_custom_hash_inner(net, skb, has_inner); 1985 1986 return jhash_2words(mhash, mhash_inner, 0); 1987 } 1988 1989 static u32 fib_multipath_custom_hash_fl4(const struct net *net, 1990 const struct flowi4 *fl4) 1991 { 1992 u32 hash_fields = net->ipv4.sysctl_fib_multipath_hash_fields; 1993 struct flow_keys hash_keys; 1994 1995 if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK)) 1996 return 0; 1997 1998 memset(&hash_keys, 0, sizeof(hash_keys)); 1999 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2000 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP) 2001 hash_keys.addrs.v4addrs.src = fl4->saddr; 2002 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP) 2003 hash_keys.addrs.v4addrs.dst = fl4->daddr; 2004 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO) 2005 hash_keys.basic.ip_proto = fl4->flowi4_proto; 2006 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT) 2007 hash_keys.ports.src = fl4->fl4_sport; 2008 if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT) 2009 hash_keys.ports.dst = fl4->fl4_dport; 2010 2011 return flow_hash_from_keys(&hash_keys); 2012 } 2013 2014 /* if skb is set it will be used and fl4 can be NULL */ 2015 int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4, 2016 const struct sk_buff *skb, struct flow_keys *flkeys) 2017 { 2018 u32 multipath_hash = fl4 ? fl4->flowi4_multipath_hash : 0; 2019 struct flow_keys hash_keys; 2020 u32 mhash = 0; 2021 2022 switch (net->ipv4.sysctl_fib_multipath_hash_policy) { 2023 case 0: 2024 memset(&hash_keys, 0, sizeof(hash_keys)); 2025 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2026 if (skb) { 2027 ip_multipath_l3_keys(skb, &hash_keys); 2028 } else { 2029 hash_keys.addrs.v4addrs.src = fl4->saddr; 2030 hash_keys.addrs.v4addrs.dst = fl4->daddr; 2031 } 2032 mhash = flow_hash_from_keys(&hash_keys); 2033 break; 2034 case 1: 2035 /* skb is currently provided only when forwarding */ 2036 if (skb) { 2037 unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP; 2038 struct flow_keys keys; 2039 2040 /* short-circuit if we already have L4 hash present */ 2041 if (skb->l4_hash) 2042 return skb_get_hash_raw(skb) >> 1; 2043 2044 memset(&hash_keys, 0, sizeof(hash_keys)); 2045 2046 if (!flkeys) { 2047 skb_flow_dissect_flow_keys(skb, &keys, flag); 2048 flkeys = &keys; 2049 } 2050 2051 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2052 hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src; 2053 hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst; 2054 hash_keys.ports.src = flkeys->ports.src; 2055 hash_keys.ports.dst = flkeys->ports.dst; 2056 hash_keys.basic.ip_proto = flkeys->basic.ip_proto; 2057 } else { 2058 memset(&hash_keys, 0, sizeof(hash_keys)); 2059 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2060 hash_keys.addrs.v4addrs.src = fl4->saddr; 2061 hash_keys.addrs.v4addrs.dst = fl4->daddr; 2062 hash_keys.ports.src = fl4->fl4_sport; 2063 hash_keys.ports.dst = fl4->fl4_dport; 2064 hash_keys.basic.ip_proto = fl4->flowi4_proto; 2065 } 2066 mhash = flow_hash_from_keys(&hash_keys); 2067 break; 2068 case 2: 2069 memset(&hash_keys, 0, sizeof(hash_keys)); 2070 /* skb is currently provided only when forwarding */ 2071 if (skb) { 2072 struct flow_keys keys; 2073 2074 skb_flow_dissect_flow_keys(skb, &keys, 0); 2075 /* Inner can be v4 or v6 */ 2076 if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { 2077 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2078 hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src; 2079 hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst; 2080 } else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { 2081 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 2082 hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src; 2083 hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst; 2084 hash_keys.tags.flow_label = keys.tags.flow_label; 2085 hash_keys.basic.ip_proto = keys.basic.ip_proto; 2086 } else { 2087 /* Same as case 0 */ 2088 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2089 ip_multipath_l3_keys(skb, &hash_keys); 2090 } 2091 } else { 2092 /* Same as case 0 */ 2093 hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 2094 hash_keys.addrs.v4addrs.src = fl4->saddr; 2095 hash_keys.addrs.v4addrs.dst = fl4->daddr; 2096 } 2097 mhash = flow_hash_from_keys(&hash_keys); 2098 break; 2099 case 3: 2100 if (skb) 2101 mhash = fib_multipath_custom_hash_skb(net, skb); 2102 else 2103 mhash = fib_multipath_custom_hash_fl4(net, fl4); 2104 break; 2105 } 2106 2107 if (multipath_hash) 2108 mhash = jhash_2words(mhash, multipath_hash, 0); 2109 2110 return mhash >> 1; 2111 } 2112 #endif /* CONFIG_IP_ROUTE_MULTIPATH */ 2113 2114 static int ip_mkroute_input(struct sk_buff *skb, 2115 struct fib_result *res, 2116 struct in_device *in_dev, 2117 __be32 daddr, __be32 saddr, u32 tos, 2118 struct flow_keys *hkeys) 2119 { 2120 #ifdef CONFIG_IP_ROUTE_MULTIPATH 2121 if (res->fi && fib_info_num_path(res->fi) > 1) { 2122 int h = fib_multipath_hash(res->fi->fib_net, NULL, skb, hkeys); 2123 2124 fib_select_multipath(res, h); 2125 } 2126 #endif 2127 2128 /* create a routing cache entry */ 2129 return __mkroute_input(skb, res, in_dev, daddr, saddr, tos); 2130 } 2131 2132 /* Implements all the saddr-related checks as ip_route_input_slow(), 2133 * assuming daddr is valid and the destination is not a local broadcast one. 2134 * Uses the provided hint instead of performing a route lookup. 2135 */ 2136 int ip_route_use_hint(struct sk_buff *skb, __be32 daddr, __be32 saddr, 2137 u8 tos, struct net_device *dev, 2138 const struct sk_buff *hint) 2139 { 2140 struct in_device *in_dev = __in_dev_get_rcu(dev); 2141 struct rtable *rt = skb_rtable(hint); 2142 struct net *net = dev_net(dev); 2143 int err = -EINVAL; 2144 u32 tag = 0; 2145 2146 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr)) 2147 goto martian_source; 2148 2149 if (ipv4_is_zeronet(saddr)) 2150 goto martian_source; 2151 2152 if (ipv4_is_loopback(saddr) && !IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) 2153 goto martian_source; 2154 2155 if (rt->rt_type != RTN_LOCAL) 2156 goto skip_validate_source; 2157 2158 tos &= IPTOS_RT_MASK; 2159 err = fib_validate_source(skb, saddr, daddr, tos, 0, dev, in_dev, &tag); 2160 if (err < 0) 2161 goto martian_source; 2162 2163 skip_validate_source: 2164 skb_dst_copy(skb, hint); 2165 return 0; 2166 2167 martian_source: 2168 ip_handle_martian_source(dev, in_dev, skb, daddr, saddr); 2169 return err; 2170 } 2171 2172 /* get device for dst_alloc with local routes */ 2173 static struct net_device *ip_rt_get_dev(struct net *net, 2174 const struct fib_result *res) 2175 { 2176 struct fib_nh_common *nhc = res->fi ? res->nhc : NULL; 2177 struct net_device *dev = NULL; 2178 2179 if (nhc) 2180 dev = l3mdev_master_dev_rcu(nhc->nhc_dev); 2181 2182 return dev ? : net->loopback_dev; 2183 } 2184 2185 /* 2186 * NOTE. We drop all the packets that has local source 2187 * addresses, because every properly looped back packet 2188 * must have correct destination already attached by output routine. 2189 * Changes in the enforced policies must be applied also to 2190 * ip_route_use_hint(). 2191 * 2192 * Such approach solves two big problems: 2193 * 1. Not simplex devices are handled properly. 2194 * 2. IP spoofing attempts are filtered with 100% of guarantee. 2195 * called with rcu_read_lock() 2196 */ 2197 2198 static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr, 2199 u8 tos, struct net_device *dev, 2200 struct fib_result *res) 2201 { 2202 struct in_device *in_dev = __in_dev_get_rcu(dev); 2203 struct flow_keys *flkeys = NULL, _flkeys; 2204 struct net *net = dev_net(dev); 2205 struct ip_tunnel_info *tun_info; 2206 int err = -EINVAL; 2207 unsigned int flags = 0; 2208 u32 itag = 0; 2209 struct rtable *rth; 2210 struct flowi4 fl4; 2211 bool do_cache = true; 2212 2213 /* IP on this device is disabled. */ 2214 2215 if (!in_dev) 2216 goto out; 2217 2218 /* Check for the most weird martians, which can be not detected 2219 * by fib_lookup. 2220 */ 2221 2222 tun_info = skb_tunnel_info(skb); 2223 if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 2224 fl4.flowi4_tun_key.tun_id = tun_info->key.tun_id; 2225 else 2226 fl4.flowi4_tun_key.tun_id = 0; 2227 skb_dst_drop(skb); 2228 2229 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr)) 2230 goto martian_source; 2231 2232 res->fi = NULL; 2233 res->table = NULL; 2234 if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0)) 2235 goto brd_input; 2236 2237 /* Accept zero addresses only to limited broadcast; 2238 * I even do not know to fix it or not. Waiting for complains :-) 2239 */ 2240 if (ipv4_is_zeronet(saddr)) 2241 goto martian_source; 2242 2243 if (ipv4_is_zeronet(daddr)) 2244 goto martian_destination; 2245 2246 /* Following code try to avoid calling IN_DEV_NET_ROUTE_LOCALNET(), 2247 * and call it once if daddr or/and saddr are loopback addresses 2248 */ 2249 if (ipv4_is_loopback(daddr)) { 2250 if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) 2251 goto martian_destination; 2252 } else if (ipv4_is_loopback(saddr)) { 2253 if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) 2254 goto martian_source; 2255 } 2256 2257 /* 2258 * Now we are ready to route packet. 2259 */ 2260 fl4.flowi4_oif = 0; 2261 fl4.flowi4_iif = dev->ifindex; 2262 fl4.flowi4_mark = skb->mark; 2263 fl4.flowi4_tos = tos; 2264 fl4.flowi4_scope = RT_SCOPE_UNIVERSE; 2265 fl4.flowi4_flags = 0; 2266 fl4.daddr = daddr; 2267 fl4.saddr = saddr; 2268 fl4.flowi4_uid = sock_net_uid(net, NULL); 2269 fl4.flowi4_multipath_hash = 0; 2270 2271 if (fib4_rules_early_flow_dissect(net, skb, &fl4, &_flkeys)) { 2272 flkeys = &_flkeys; 2273 } else { 2274 fl4.flowi4_proto = 0; 2275 fl4.fl4_sport = 0; 2276 fl4.fl4_dport = 0; 2277 } 2278 2279 err = fib_lookup(net, &fl4, res, 0); 2280 if (err != 0) { 2281 if (!IN_DEV_FORWARD(in_dev)) 2282 err = -EHOSTUNREACH; 2283 goto no_route; 2284 } 2285 2286 if (res->type == RTN_BROADCAST) { 2287 if (IN_DEV_BFORWARD(in_dev)) 2288 goto make_route; 2289 /* not do cache if bc_forwarding is enabled */ 2290 if (IPV4_DEVCONF_ALL(net, BC_FORWARDING)) 2291 do_cache = false; 2292 goto brd_input; 2293 } 2294 2295 if (res->type == RTN_LOCAL) { 2296 err = fib_validate_source(skb, saddr, daddr, tos, 2297 0, dev, in_dev, &itag); 2298 if (err < 0) 2299 goto martian_source; 2300 goto local_input; 2301 } 2302 2303 if (!IN_DEV_FORWARD(in_dev)) { 2304 err = -EHOSTUNREACH; 2305 goto no_route; 2306 } 2307 if (res->type != RTN_UNICAST) 2308 goto martian_destination; 2309 2310 make_route: 2311 err = ip_mkroute_input(skb, res, in_dev, daddr, saddr, tos, flkeys); 2312 out: return err; 2313 2314 brd_input: 2315 if (skb->protocol != htons(ETH_P_IP)) 2316 goto e_inval; 2317 2318 if (!ipv4_is_zeronet(saddr)) { 2319 err = fib_validate_source(skb, saddr, 0, tos, 0, dev, 2320 in_dev, &itag); 2321 if (err < 0) 2322 goto martian_source; 2323 } 2324 flags |= RTCF_BROADCAST; 2325 res->type = RTN_BROADCAST; 2326 RT_CACHE_STAT_INC(in_brd); 2327 2328 local_input: 2329 do_cache &= res->fi && !itag; 2330 if (do_cache) { 2331 struct fib_nh_common *nhc = FIB_RES_NHC(*res); 2332 2333 rth = rcu_dereference(nhc->nhc_rth_input); 2334 if (rt_cache_valid(rth)) { 2335 skb_dst_set_noref(skb, &rth->dst); 2336 err = 0; 2337 goto out; 2338 } 2339 } 2340 2341 rth = rt_dst_alloc(ip_rt_get_dev(net, res), 2342 flags | RTCF_LOCAL, res->type, 2343 IN_DEV_ORCONF(in_dev, NOPOLICY), false); 2344 if (!rth) 2345 goto e_nobufs; 2346 2347 rth->dst.output= ip_rt_bug; 2348 #ifdef CONFIG_IP_ROUTE_CLASSID 2349 rth->dst.tclassid = itag; 2350 #endif 2351 rth->rt_is_input = 1; 2352 2353 RT_CACHE_STAT_INC(in_slow_tot); 2354 if (res->type == RTN_UNREACHABLE) { 2355 rth->dst.input= ip_error; 2356 rth->dst.error= -err; 2357 rth->rt_flags &= ~RTCF_LOCAL; 2358 } 2359 2360 if (do_cache) { 2361 struct fib_nh_common *nhc = FIB_RES_NHC(*res); 2362 2363 rth->dst.lwtstate = lwtstate_get(nhc->nhc_lwtstate); 2364 if (lwtunnel_input_redirect(rth->dst.lwtstate)) { 2365 WARN_ON(rth->dst.input == lwtunnel_input); 2366 rth->dst.lwtstate->orig_input = rth->dst.input; 2367 rth->dst.input = lwtunnel_input; 2368 } 2369 2370 if (unlikely(!rt_cache_route(nhc, rth))) 2371 rt_add_uncached_list(rth); 2372 } 2373 skb_dst_set(skb, &rth->dst); 2374 err = 0; 2375 goto out; 2376 2377 no_route: 2378 RT_CACHE_STAT_INC(in_no_route); 2379 res->type = RTN_UNREACHABLE; 2380 res->fi = NULL; 2381 res->table = NULL; 2382 goto local_input; 2383 2384 /* 2385 * Do not cache martian addresses: they should be logged (RFC1812) 2386 */ 2387 martian_destination: 2388 RT_CACHE_STAT_INC(in_martian_dst); 2389 #ifdef CONFIG_IP_ROUTE_VERBOSE 2390 if (IN_DEV_LOG_MARTIANS(in_dev)) 2391 net_warn_ratelimited("martian destination %pI4 from %pI4, dev %s\n", 2392 &daddr, &saddr, dev->name); 2393 #endif 2394 2395 e_inval: 2396 err = -EINVAL; 2397 goto out; 2398 2399 e_nobufs: 2400 err = -ENOBUFS; 2401 goto out; 2402 2403 martian_source: 2404 ip_handle_martian_source(dev, in_dev, skb, daddr, saddr); 2405 goto out; 2406 } 2407 2408 int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr, 2409 u8 tos, struct net_device *dev) 2410 { 2411 struct fib_result res; 2412 int err; 2413 2414 tos &= IPTOS_RT_MASK; 2415 rcu_read_lock(); 2416 err = ip_route_input_rcu(skb, daddr, saddr, tos, dev, &res); 2417 rcu_read_unlock(); 2418 2419 return err; 2420 } 2421 EXPORT_SYMBOL(ip_route_input_noref); 2422 2423 /* called with rcu_read_lock held */ 2424 int ip_route_input_rcu(struct sk_buff *skb, __be32 daddr, __be32 saddr, 2425 u8 tos, struct net_device *dev, struct fib_result *res) 2426 { 2427 /* Multicast recognition logic is moved from route cache to here. 2428 * The problem was that too many Ethernet cards have broken/missing 2429 * hardware multicast filters :-( As result the host on multicasting 2430 * network acquires a lot of useless route cache entries, sort of 2431 * SDR messages from all the world. Now we try to get rid of them. 2432 * Really, provided software IP multicast filter is organized 2433 * reasonably (at least, hashed), it does not result in a slowdown 2434 * comparing with route cache reject entries. 2435 * Note, that multicast routers are not affected, because 2436 * route cache entry is created eventually. 2437 */ 2438 if (ipv4_is_multicast(daddr)) { 2439 struct in_device *in_dev = __in_dev_get_rcu(dev); 2440 int our = 0; 2441 int err = -EINVAL; 2442 2443 if (!in_dev) 2444 return err; 2445 our = ip_check_mc_rcu(in_dev, daddr, saddr, 2446 ip_hdr(skb)->protocol); 2447 2448 /* check l3 master if no match yet */ 2449 if (!our && netif_is_l3_slave(dev)) { 2450 struct in_device *l3_in_dev; 2451 2452 l3_in_dev = __in_dev_get_rcu(skb->dev); 2453 if (l3_in_dev) 2454 our = ip_check_mc_rcu(l3_in_dev, daddr, saddr, 2455 ip_hdr(skb)->protocol); 2456 } 2457 2458 if (our 2459 #ifdef CONFIG_IP_MROUTE 2460 || 2461 (!ipv4_is_local_multicast(daddr) && 2462 IN_DEV_MFORWARD(in_dev)) 2463 #endif 2464 ) { 2465 err = ip_route_input_mc(skb, daddr, saddr, 2466 tos, dev, our); 2467 } 2468 return err; 2469 } 2470 2471 return ip_route_input_slow(skb, daddr, saddr, tos, dev, res); 2472 } 2473 2474 /* called with rcu_read_lock() */ 2475 static struct rtable *__mkroute_output(const struct fib_result *res, 2476 const struct flowi4 *fl4, int orig_oif, 2477 struct net_device *dev_out, 2478 unsigned int flags) 2479 { 2480 struct fib_info *fi = res->fi; 2481 struct fib_nh_exception *fnhe; 2482 struct in_device *in_dev; 2483 u16 type = res->type; 2484 struct rtable *rth; 2485 bool do_cache; 2486 2487 in_dev = __in_dev_get_rcu(dev_out); 2488 if (!in_dev) 2489 return ERR_PTR(-EINVAL); 2490 2491 if (likely(!IN_DEV_ROUTE_LOCALNET(in_dev))) 2492 if (ipv4_is_loopback(fl4->saddr) && 2493 !(dev_out->flags & IFF_LOOPBACK) && 2494 !netif_is_l3_master(dev_out)) 2495 return ERR_PTR(-EINVAL); 2496 2497 if (ipv4_is_lbcast(fl4->daddr)) 2498 type = RTN_BROADCAST; 2499 else if (ipv4_is_multicast(fl4->daddr)) 2500 type = RTN_MULTICAST; 2501 else if (ipv4_is_zeronet(fl4->daddr)) 2502 return ERR_PTR(-EINVAL); 2503 2504 if (dev_out->flags & IFF_LOOPBACK) 2505 flags |= RTCF_LOCAL; 2506 2507 do_cache = true; 2508 if (type == RTN_BROADCAST) { 2509 flags |= RTCF_BROADCAST | RTCF_LOCAL; 2510 fi = NULL; 2511 } else if (type == RTN_MULTICAST) { 2512 flags |= RTCF_MULTICAST | RTCF_LOCAL; 2513 if (!ip_check_mc_rcu(in_dev, fl4->daddr, fl4->saddr, 2514 fl4->flowi4_proto)) 2515 flags &= ~RTCF_LOCAL; 2516 else 2517 do_cache = false; 2518 /* If multicast route do not exist use 2519 * default one, but do not gateway in this case. 2520 * Yes, it is hack. 2521 */ 2522 if (fi && res->prefixlen < 4) 2523 fi = NULL; 2524 } else if ((type == RTN_LOCAL) && (orig_oif != 0) && 2525 (orig_oif != dev_out->ifindex)) { 2526 /* For local routes that require a particular output interface 2527 * we do not want to cache the result. Caching the result 2528 * causes incorrect behaviour when there are multiple source 2529 * addresses on the interface, the end result being that if the 2530 * intended recipient is waiting on that interface for the 2531 * packet he won't receive it because it will be delivered on 2532 * the loopback interface and the IP_PKTINFO ipi_ifindex will 2533 * be set to the loopback interface as well. 2534 */ 2535 do_cache = false; 2536 } 2537 2538 fnhe = NULL; 2539 do_cache &= fi != NULL; 2540 if (fi) { 2541 struct fib_nh_common *nhc = FIB_RES_NHC(*res); 2542 struct rtable __rcu **prth; 2543 2544 fnhe = find_exception(nhc, fl4->daddr); 2545 if (!do_cache) 2546 goto add; 2547 if (fnhe) { 2548 prth = &fnhe->fnhe_rth_output; 2549 } else { 2550 if (unlikely(fl4->flowi4_flags & 2551 FLOWI_FLAG_KNOWN_NH && 2552 !(nhc->nhc_gw_family && 2553 nhc->nhc_scope == RT_SCOPE_LINK))) { 2554 do_cache = false; 2555 goto add; 2556 } 2557 prth = raw_cpu_ptr(nhc->nhc_pcpu_rth_output); 2558 } 2559 rth = rcu_dereference(*prth); 2560 if (rt_cache_valid(rth) && dst_hold_safe(&rth->dst)) 2561 return rth; 2562 } 2563 2564 add: 2565 rth = rt_dst_alloc(dev_out, flags, type, 2566 IN_DEV_ORCONF(in_dev, NOPOLICY), 2567 IN_DEV_ORCONF(in_dev, NOXFRM)); 2568 if (!rth) 2569 return ERR_PTR(-ENOBUFS); 2570 2571 rth->rt_iif = orig_oif; 2572 2573 RT_CACHE_STAT_INC(out_slow_tot); 2574 2575 if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) { 2576 if (flags & RTCF_LOCAL && 2577 !(dev_out->flags & IFF_LOOPBACK)) { 2578 rth->dst.output = ip_mc_output; 2579 RT_CACHE_STAT_INC(out_slow_mc); 2580 } 2581 #ifdef CONFIG_IP_MROUTE 2582 if (type == RTN_MULTICAST) { 2583 if (IN_DEV_MFORWARD(in_dev) && 2584 !ipv4_is_local_multicast(fl4->daddr)) { 2585 rth->dst.input = ip_mr_input; 2586 rth->dst.output = ip_mc_output; 2587 } 2588 } 2589 #endif 2590 } 2591 2592 rt_set_nexthop(rth, fl4->daddr, res, fnhe, fi, type, 0, do_cache); 2593 lwtunnel_set_redirect(&rth->dst); 2594 2595 return rth; 2596 } 2597 2598 /* 2599 * Major route resolver routine. 2600 */ 2601 2602 struct rtable *ip_route_output_key_hash(struct net *net, struct flowi4 *fl4, 2603 const struct sk_buff *skb) 2604 { 2605 __u8 tos = RT_FL_TOS(fl4); 2606 struct fib_result res = { 2607 .type = RTN_UNSPEC, 2608 .fi = NULL, 2609 .table = NULL, 2610 .tclassid = 0, 2611 }; 2612 struct rtable *rth; 2613 2614 fl4->flowi4_iif = LOOPBACK_IFINDEX; 2615 fl4->flowi4_tos = tos & IPTOS_RT_MASK; 2616 fl4->flowi4_scope = ((tos & RTO_ONLINK) ? 2617 RT_SCOPE_LINK : RT_SCOPE_UNIVERSE); 2618 2619 rcu_read_lock(); 2620 rth = ip_route_output_key_hash_rcu(net, fl4, &res, skb); 2621 rcu_read_unlock(); 2622 2623 return rth; 2624 } 2625 EXPORT_SYMBOL_GPL(ip_route_output_key_hash); 2626 2627 struct rtable *ip_route_output_key_hash_rcu(struct net *net, struct flowi4 *fl4, 2628 struct fib_result *res, 2629 const struct sk_buff *skb) 2630 { 2631 struct net_device *dev_out = NULL; 2632 int orig_oif = fl4->flowi4_oif; 2633 unsigned int flags = 0; 2634 struct rtable *rth; 2635 int err; 2636 2637 if (fl4->saddr) { 2638 if (ipv4_is_multicast(fl4->saddr) || 2639 ipv4_is_lbcast(fl4->saddr) || 2640 ipv4_is_zeronet(fl4->saddr)) { 2641 rth = ERR_PTR(-EINVAL); 2642 goto out; 2643 } 2644 2645 rth = ERR_PTR(-ENETUNREACH); 2646 2647 /* I removed check for oif == dev_out->oif here. 2648 * It was wrong for two reasons: 2649 * 1. ip_dev_find(net, saddr) can return wrong iface, if saddr 2650 * is assigned to multiple interfaces. 2651 * 2. Moreover, we are allowed to send packets with saddr 2652 * of another iface. --ANK 2653 */ 2654 2655 if (fl4->flowi4_oif == 0 && 2656 (ipv4_is_multicast(fl4->daddr) || 2657 ipv4_is_lbcast(fl4->daddr))) { 2658 /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */ 2659 dev_out = __ip_dev_find(net, fl4->saddr, false); 2660 if (!dev_out) 2661 goto out; 2662 2663 /* Special hack: user can direct multicasts 2664 * and limited broadcast via necessary interface 2665 * without fiddling with IP_MULTICAST_IF or IP_PKTINFO. 2666 * This hack is not just for fun, it allows 2667 * vic,vat and friends to work. 2668 * They bind socket to loopback, set ttl to zero 2669 * and expect that it will work. 2670 * From the viewpoint of routing cache they are broken, 2671 * because we are not allowed to build multicast path 2672 * with loopback source addr (look, routing cache 2673 * cannot know, that ttl is zero, so that packet 2674 * will not leave this host and route is valid). 2675 * Luckily, this hack is good workaround. 2676 */ 2677 2678 fl4->flowi4_oif = dev_out->ifindex; 2679 goto make_route; 2680 } 2681 2682 if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC)) { 2683 /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */ 2684 if (!__ip_dev_find(net, fl4->saddr, false)) 2685 goto out; 2686 } 2687 } 2688 2689 2690 if (fl4->flowi4_oif) { 2691 dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif); 2692 rth = ERR_PTR(-ENODEV); 2693 if (!dev_out) 2694 goto out; 2695 2696 /* RACE: Check return value of inet_select_addr instead. */ 2697 if (!(dev_out->flags & IFF_UP) || !__in_dev_get_rcu(dev_out)) { 2698 rth = ERR_PTR(-ENETUNREACH); 2699 goto out; 2700 } 2701 if (ipv4_is_local_multicast(fl4->daddr) || 2702 ipv4_is_lbcast(fl4->daddr) || 2703 fl4->flowi4_proto == IPPROTO_IGMP) { 2704 if (!fl4->saddr) 2705 fl4->saddr = inet_select_addr(dev_out, 0, 2706 RT_SCOPE_LINK); 2707 goto make_route; 2708 } 2709 if (!fl4->saddr) { 2710 if (ipv4_is_multicast(fl4->daddr)) 2711 fl4->saddr = inet_select_addr(dev_out, 0, 2712 fl4->flowi4_scope); 2713 else if (!fl4->daddr) 2714 fl4->saddr = inet_select_addr(dev_out, 0, 2715 RT_SCOPE_HOST); 2716 } 2717 } 2718 2719 if (!fl4->daddr) { 2720 fl4->daddr = fl4->saddr; 2721 if (!fl4->daddr) 2722 fl4->daddr = fl4->saddr = htonl(INADDR_LOOPBACK); 2723 dev_out = net->loopback_dev; 2724 fl4->flowi4_oif = LOOPBACK_IFINDEX; 2725 res->type = RTN_LOCAL; 2726 flags |= RTCF_LOCAL; 2727 goto make_route; 2728 } 2729 2730 err = fib_lookup(net, fl4, res, 0); 2731 if (err) { 2732 res->fi = NULL; 2733 res->table = NULL; 2734 if (fl4->flowi4_oif && 2735 (ipv4_is_multicast(fl4->daddr) || 2736 !netif_index_is_l3_master(net, fl4->flowi4_oif))) { 2737 /* Apparently, routing tables are wrong. Assume, 2738 * that the destination is on link. 2739 * 2740 * WHY? DW. 2741 * Because we are allowed to send to iface 2742 * even if it has NO routes and NO assigned 2743 * addresses. When oif is specified, routing 2744 * tables are looked up with only one purpose: 2745 * to catch if destination is gatewayed, rather than 2746 * direct. Moreover, if MSG_DONTROUTE is set, 2747 * we send packet, ignoring both routing tables 2748 * and ifaddr state. --ANK 2749 * 2750 * 2751 * We could make it even if oif is unknown, 2752 * likely IPv6, but we do not. 2753 */ 2754 2755 if (fl4->saddr == 0) 2756 fl4->saddr = inet_select_addr(dev_out, 0, 2757 RT_SCOPE_LINK); 2758 res->type = RTN_UNICAST; 2759 goto make_route; 2760 } 2761 rth = ERR_PTR(err); 2762 goto out; 2763 } 2764 2765 if (res->type == RTN_LOCAL) { 2766 if (!fl4->saddr) { 2767 if (res->fi->fib_prefsrc) 2768 fl4->saddr = res->fi->fib_prefsrc; 2769 else 2770 fl4->saddr = fl4->daddr; 2771 } 2772 2773 /* L3 master device is the loopback for that domain */ 2774 dev_out = l3mdev_master_dev_rcu(FIB_RES_DEV(*res)) ? : 2775 net->loopback_dev; 2776 2777 /* make sure orig_oif points to fib result device even 2778 * though packet rx/tx happens over loopback or l3mdev 2779 */ 2780 orig_oif = FIB_RES_OIF(*res); 2781 2782 fl4->flowi4_oif = dev_out->ifindex; 2783 flags |= RTCF_LOCAL; 2784 goto make_route; 2785 } 2786 2787 fib_select_path(net, res, fl4, skb); 2788 2789 dev_out = FIB_RES_DEV(*res); 2790 2791 make_route: 2792 rth = __mkroute_output(res, fl4, orig_oif, dev_out, flags); 2793 2794 out: 2795 return rth; 2796 } 2797 2798 static struct dst_ops ipv4_dst_blackhole_ops = { 2799 .family = AF_INET, 2800 .default_advmss = ipv4_default_advmss, 2801 .neigh_lookup = ipv4_neigh_lookup, 2802 .check = dst_blackhole_check, 2803 .cow_metrics = dst_blackhole_cow_metrics, 2804 .update_pmtu = dst_blackhole_update_pmtu, 2805 .redirect = dst_blackhole_redirect, 2806 .mtu = dst_blackhole_mtu, 2807 }; 2808 2809 struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig) 2810 { 2811 struct rtable *ort = (struct rtable *) dst_orig; 2812 struct rtable *rt; 2813 2814 rt = dst_alloc(&ipv4_dst_blackhole_ops, NULL, 1, DST_OBSOLETE_DEAD, 0); 2815 if (rt) { 2816 struct dst_entry *new = &rt->dst; 2817 2818 new->__use = 1; 2819 new->input = dst_discard; 2820 new->output = dst_discard_out; 2821 2822 new->dev = net->loopback_dev; 2823 dev_hold_track(new->dev, &new->dev_tracker, GFP_ATOMIC); 2824 2825 rt->rt_is_input = ort->rt_is_input; 2826 rt->rt_iif = ort->rt_iif; 2827 rt->rt_pmtu = ort->rt_pmtu; 2828 rt->rt_mtu_locked = ort->rt_mtu_locked; 2829 2830 rt->rt_genid = rt_genid_ipv4(net); 2831 rt->rt_flags = ort->rt_flags; 2832 rt->rt_type = ort->rt_type; 2833 rt->rt_uses_gateway = ort->rt_uses_gateway; 2834 rt->rt_gw_family = ort->rt_gw_family; 2835 if (rt->rt_gw_family == AF_INET) 2836 rt->rt_gw4 = ort->rt_gw4; 2837 else if (rt->rt_gw_family == AF_INET6) 2838 rt->rt_gw6 = ort->rt_gw6; 2839 2840 INIT_LIST_HEAD(&rt->rt_uncached); 2841 } 2842 2843 dst_release(dst_orig); 2844 2845 return rt ? &rt->dst : ERR_PTR(-ENOMEM); 2846 } 2847 2848 struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4, 2849 const struct sock *sk) 2850 { 2851 struct rtable *rt = __ip_route_output_key(net, flp4); 2852 2853 if (IS_ERR(rt)) 2854 return rt; 2855 2856 if (flp4->flowi4_proto) { 2857 flp4->flowi4_oif = rt->dst.dev->ifindex; 2858 rt = (struct rtable *)xfrm_lookup_route(net, &rt->dst, 2859 flowi4_to_flowi(flp4), 2860 sk, 0); 2861 } 2862 2863 return rt; 2864 } 2865 EXPORT_SYMBOL_GPL(ip_route_output_flow); 2866 2867 struct rtable *ip_route_output_tunnel(struct sk_buff *skb, 2868 struct net_device *dev, 2869 struct net *net, __be32 *saddr, 2870 const struct ip_tunnel_info *info, 2871 u8 protocol, bool use_cache) 2872 { 2873 #ifdef CONFIG_DST_CACHE 2874 struct dst_cache *dst_cache; 2875 #endif 2876 struct rtable *rt = NULL; 2877 struct flowi4 fl4; 2878 __u8 tos; 2879 2880 #ifdef CONFIG_DST_CACHE 2881 dst_cache = (struct dst_cache *)&info->dst_cache; 2882 if (use_cache) { 2883 rt = dst_cache_get_ip4(dst_cache, saddr); 2884 if (rt) 2885 return rt; 2886 } 2887 #endif 2888 memset(&fl4, 0, sizeof(fl4)); 2889 fl4.flowi4_mark = skb->mark; 2890 fl4.flowi4_proto = protocol; 2891 fl4.daddr = info->key.u.ipv4.dst; 2892 fl4.saddr = info->key.u.ipv4.src; 2893 tos = info->key.tos; 2894 fl4.flowi4_tos = RT_TOS(tos); 2895 2896 rt = ip_route_output_key(net, &fl4); 2897 if (IS_ERR(rt)) { 2898 netdev_dbg(dev, "no route to %pI4\n", &fl4.daddr); 2899 return ERR_PTR(-ENETUNREACH); 2900 } 2901 if (rt->dst.dev == dev) { /* is this necessary? */ 2902 netdev_dbg(dev, "circular route to %pI4\n", &fl4.daddr); 2903 ip_rt_put(rt); 2904 return ERR_PTR(-ELOOP); 2905 } 2906 #ifdef CONFIG_DST_CACHE 2907 if (use_cache) 2908 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr); 2909 #endif 2910 *saddr = fl4.saddr; 2911 return rt; 2912 } 2913 EXPORT_SYMBOL_GPL(ip_route_output_tunnel); 2914 2915 /* called with rcu_read_lock held */ 2916 static int rt_fill_info(struct net *net, __be32 dst, __be32 src, 2917 struct rtable *rt, u32 table_id, struct flowi4 *fl4, 2918 struct sk_buff *skb, u32 portid, u32 seq, 2919 unsigned int flags) 2920 { 2921 struct rtmsg *r; 2922 struct nlmsghdr *nlh; 2923 unsigned long expires = 0; 2924 u32 error; 2925 u32 metrics[RTAX_MAX]; 2926 2927 nlh = nlmsg_put(skb, portid, seq, RTM_NEWROUTE, sizeof(*r), flags); 2928 if (!nlh) 2929 return -EMSGSIZE; 2930 2931 r = nlmsg_data(nlh); 2932 r->rtm_family = AF_INET; 2933 r->rtm_dst_len = 32; 2934 r->rtm_src_len = 0; 2935 r->rtm_tos = fl4 ? fl4->flowi4_tos : 0; 2936 r->rtm_table = table_id < 256 ? table_id : RT_TABLE_COMPAT; 2937 if (nla_put_u32(skb, RTA_TABLE, table_id)) 2938 goto nla_put_failure; 2939 r->rtm_type = rt->rt_type; 2940 r->rtm_scope = RT_SCOPE_UNIVERSE; 2941 r->rtm_protocol = RTPROT_UNSPEC; 2942 r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED; 2943 if (rt->rt_flags & RTCF_NOTIFY) 2944 r->rtm_flags |= RTM_F_NOTIFY; 2945 if (IPCB(skb)->flags & IPSKB_DOREDIRECT) 2946 r->rtm_flags |= RTCF_DOREDIRECT; 2947 2948 if (nla_put_in_addr(skb, RTA_DST, dst)) 2949 goto nla_put_failure; 2950 if (src) { 2951 r->rtm_src_len = 32; 2952 if (nla_put_in_addr(skb, RTA_SRC, src)) 2953 goto nla_put_failure; 2954 } 2955 if (rt->dst.dev && 2956 nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 2957 goto nla_put_failure; 2958 if (rt->dst.lwtstate && 2959 lwtunnel_fill_encap(skb, rt->dst.lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0) 2960 goto nla_put_failure; 2961 #ifdef CONFIG_IP_ROUTE_CLASSID 2962 if (rt->dst.tclassid && 2963 nla_put_u32(skb, RTA_FLOW, rt->dst.tclassid)) 2964 goto nla_put_failure; 2965 #endif 2966 if (fl4 && !rt_is_input_route(rt) && 2967 fl4->saddr != src) { 2968 if (nla_put_in_addr(skb, RTA_PREFSRC, fl4->saddr)) 2969 goto nla_put_failure; 2970 } 2971 if (rt->rt_uses_gateway) { 2972 if (rt->rt_gw_family == AF_INET && 2973 nla_put_in_addr(skb, RTA_GATEWAY, rt->rt_gw4)) { 2974 goto nla_put_failure; 2975 } else if (rt->rt_gw_family == AF_INET6) { 2976 int alen = sizeof(struct in6_addr); 2977 struct nlattr *nla; 2978 struct rtvia *via; 2979 2980 nla = nla_reserve(skb, RTA_VIA, alen + 2); 2981 if (!nla) 2982 goto nla_put_failure; 2983 2984 via = nla_data(nla); 2985 via->rtvia_family = AF_INET6; 2986 memcpy(via->rtvia_addr, &rt->rt_gw6, alen); 2987 } 2988 } 2989 2990 expires = rt->dst.expires; 2991 if (expires) { 2992 unsigned long now = jiffies; 2993 2994 if (time_before(now, expires)) 2995 expires -= now; 2996 else 2997 expires = 0; 2998 } 2999 3000 memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 3001 if (rt->rt_pmtu && expires) 3002 metrics[RTAX_MTU - 1] = rt->rt_pmtu; 3003 if (rt->rt_mtu_locked && expires) 3004 metrics[RTAX_LOCK - 1] |= BIT(RTAX_MTU); 3005 if (rtnetlink_put_metrics(skb, metrics) < 0) 3006 goto nla_put_failure; 3007 3008 if (fl4) { 3009 if (fl4->flowi4_mark && 3010 nla_put_u32(skb, RTA_MARK, fl4->flowi4_mark)) 3011 goto nla_put_failure; 3012 3013 if (!uid_eq(fl4->flowi4_uid, INVALID_UID) && 3014 nla_put_u32(skb, RTA_UID, 3015 from_kuid_munged(current_user_ns(), 3016 fl4->flowi4_uid))) 3017 goto nla_put_failure; 3018 3019 if (rt_is_input_route(rt)) { 3020 #ifdef CONFIG_IP_MROUTE 3021 if (ipv4_is_multicast(dst) && 3022 !ipv4_is_local_multicast(dst) && 3023 IPV4_DEVCONF_ALL(net, MC_FORWARDING)) { 3024 int err = ipmr_get_route(net, skb, 3025 fl4->saddr, fl4->daddr, 3026 r, portid); 3027 3028 if (err <= 0) { 3029 if (err == 0) 3030 return 0; 3031 goto nla_put_failure; 3032 } 3033 } else 3034 #endif 3035 if (nla_put_u32(skb, RTA_IIF, fl4->flowi4_iif)) 3036 goto nla_put_failure; 3037 } 3038 } 3039 3040 error = rt->dst.error; 3041 3042 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, error) < 0) 3043 goto nla_put_failure; 3044 3045 nlmsg_end(skb, nlh); 3046 return 0; 3047 3048 nla_put_failure: 3049 nlmsg_cancel(skb, nlh); 3050 return -EMSGSIZE; 3051 } 3052 3053 static int fnhe_dump_bucket(struct net *net, struct sk_buff *skb, 3054 struct netlink_callback *cb, u32 table_id, 3055 struct fnhe_hash_bucket *bucket, int genid, 3056 int *fa_index, int fa_start, unsigned int flags) 3057 { 3058 int i; 3059 3060 for (i = 0; i < FNHE_HASH_SIZE; i++) { 3061 struct fib_nh_exception *fnhe; 3062 3063 for (fnhe = rcu_dereference(bucket[i].chain); fnhe; 3064 fnhe = rcu_dereference(fnhe->fnhe_next)) { 3065 struct rtable *rt; 3066 int err; 3067 3068 if (*fa_index < fa_start) 3069 goto next; 3070 3071 if (fnhe->fnhe_genid != genid) 3072 goto next; 3073 3074 if (fnhe->fnhe_expires && 3075 time_after(jiffies, fnhe->fnhe_expires)) 3076 goto next; 3077 3078 rt = rcu_dereference(fnhe->fnhe_rth_input); 3079 if (!rt) 3080 rt = rcu_dereference(fnhe->fnhe_rth_output); 3081 if (!rt) 3082 goto next; 3083 3084 err = rt_fill_info(net, fnhe->fnhe_daddr, 0, rt, 3085 table_id, NULL, skb, 3086 NETLINK_CB(cb->skb).portid, 3087 cb->nlh->nlmsg_seq, flags); 3088 if (err) 3089 return err; 3090 next: 3091 (*fa_index)++; 3092 } 3093 } 3094 3095 return 0; 3096 } 3097 3098 int fib_dump_info_fnhe(struct sk_buff *skb, struct netlink_callback *cb, 3099 u32 table_id, struct fib_info *fi, 3100 int *fa_index, int fa_start, unsigned int flags) 3101 { 3102 struct net *net = sock_net(cb->skb->sk); 3103 int nhsel, genid = fnhe_genid(net); 3104 3105 for (nhsel = 0; nhsel < fib_info_num_path(fi); nhsel++) { 3106 struct fib_nh_common *nhc = fib_info_nhc(fi, nhsel); 3107 struct fnhe_hash_bucket *bucket; 3108 int err; 3109 3110 if (nhc->nhc_flags & RTNH_F_DEAD) 3111 continue; 3112 3113 rcu_read_lock(); 3114 bucket = rcu_dereference(nhc->nhc_exceptions); 3115 err = 0; 3116 if (bucket) 3117 err = fnhe_dump_bucket(net, skb, cb, table_id, bucket, 3118 genid, fa_index, fa_start, 3119 flags); 3120 rcu_read_unlock(); 3121 if (err) 3122 return err; 3123 } 3124 3125 return 0; 3126 } 3127 3128 static struct sk_buff *inet_rtm_getroute_build_skb(__be32 src, __be32 dst, 3129 u8 ip_proto, __be16 sport, 3130 __be16 dport) 3131 { 3132 struct sk_buff *skb; 3133 struct iphdr *iph; 3134 3135 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 3136 if (!skb) 3137 return NULL; 3138 3139 /* Reserve room for dummy headers, this skb can pass 3140 * through good chunk of routing engine. 3141 */ 3142 skb_reset_mac_header(skb); 3143 skb_reset_network_header(skb); 3144 skb->protocol = htons(ETH_P_IP); 3145 iph = skb_put(skb, sizeof(struct iphdr)); 3146 iph->protocol = ip_proto; 3147 iph->saddr = src; 3148 iph->daddr = dst; 3149 iph->version = 0x4; 3150 iph->frag_off = 0; 3151 iph->ihl = 0x5; 3152 skb_set_transport_header(skb, skb->len); 3153 3154 switch (iph->protocol) { 3155 case IPPROTO_UDP: { 3156 struct udphdr *udph; 3157 3158 udph = skb_put_zero(skb, sizeof(struct udphdr)); 3159 udph->source = sport; 3160 udph->dest = dport; 3161 udph->len = htons(sizeof(struct udphdr)); 3162 udph->check = 0; 3163 break; 3164 } 3165 case IPPROTO_TCP: { 3166 struct tcphdr *tcph; 3167 3168 tcph = skb_put_zero(skb, sizeof(struct tcphdr)); 3169 tcph->source = sport; 3170 tcph->dest = dport; 3171 tcph->doff = sizeof(struct tcphdr) / 4; 3172 tcph->rst = 1; 3173 tcph->check = ~tcp_v4_check(sizeof(struct tcphdr), 3174 src, dst, 0); 3175 break; 3176 } 3177 case IPPROTO_ICMP: { 3178 struct icmphdr *icmph; 3179 3180 icmph = skb_put_zero(skb, sizeof(struct icmphdr)); 3181 icmph->type = ICMP_ECHO; 3182 icmph->code = 0; 3183 } 3184 } 3185 3186 return skb; 3187 } 3188 3189 static int inet_rtm_valid_getroute_req(struct sk_buff *skb, 3190 const struct nlmsghdr *nlh, 3191 struct nlattr **tb, 3192 struct netlink_ext_ack *extack) 3193 { 3194 struct rtmsg *rtm; 3195 int i, err; 3196 3197 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { 3198 NL_SET_ERR_MSG(extack, 3199 "ipv4: Invalid header for route get request"); 3200 return -EINVAL; 3201 } 3202 3203 if (!netlink_strict_get_check(skb)) 3204 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 3205 rtm_ipv4_policy, extack); 3206 3207 rtm = nlmsg_data(nlh); 3208 if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) || 3209 (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) || 3210 rtm->rtm_table || rtm->rtm_protocol || 3211 rtm->rtm_scope || rtm->rtm_type) { 3212 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for route get request"); 3213 return -EINVAL; 3214 } 3215 3216 if (rtm->rtm_flags & ~(RTM_F_NOTIFY | 3217 RTM_F_LOOKUP_TABLE | 3218 RTM_F_FIB_MATCH)) { 3219 NL_SET_ERR_MSG(extack, "ipv4: Unsupported rtm_flags for route get request"); 3220 return -EINVAL; 3221 } 3222 3223 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, 3224 rtm_ipv4_policy, extack); 3225 if (err) 3226 return err; 3227 3228 if ((tb[RTA_SRC] && !rtm->rtm_src_len) || 3229 (tb[RTA_DST] && !rtm->rtm_dst_len)) { 3230 NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4"); 3231 return -EINVAL; 3232 } 3233 3234 for (i = 0; i <= RTA_MAX; i++) { 3235 if (!tb[i]) 3236 continue; 3237 3238 switch (i) { 3239 case RTA_IIF: 3240 case RTA_OIF: 3241 case RTA_SRC: 3242 case RTA_DST: 3243 case RTA_IP_PROTO: 3244 case RTA_SPORT: 3245 case RTA_DPORT: 3246 case RTA_MARK: 3247 case RTA_UID: 3248 break; 3249 default: 3250 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in route get request"); 3251 return -EINVAL; 3252 } 3253 } 3254 3255 return 0; 3256 } 3257 3258 static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 3259 struct netlink_ext_ack *extack) 3260 { 3261 struct net *net = sock_net(in_skb->sk); 3262 struct nlattr *tb[RTA_MAX+1]; 3263 u32 table_id = RT_TABLE_MAIN; 3264 __be16 sport = 0, dport = 0; 3265 struct fib_result res = {}; 3266 u8 ip_proto = IPPROTO_UDP; 3267 struct rtable *rt = NULL; 3268 struct sk_buff *skb; 3269 struct rtmsg *rtm; 3270 struct flowi4 fl4 = {}; 3271 __be32 dst = 0; 3272 __be32 src = 0; 3273 kuid_t uid; 3274 u32 iif; 3275 int err; 3276 int mark; 3277 3278 err = inet_rtm_valid_getroute_req(in_skb, nlh, tb, extack); 3279 if (err < 0) 3280 return err; 3281 3282 rtm = nlmsg_data(nlh); 3283 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0; 3284 dst = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0; 3285 iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0; 3286 mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0; 3287 if (tb[RTA_UID]) 3288 uid = make_kuid(current_user_ns(), nla_get_u32(tb[RTA_UID])); 3289 else 3290 uid = (iif ? INVALID_UID : current_uid()); 3291 3292 if (tb[RTA_IP_PROTO]) { 3293 err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO], 3294 &ip_proto, AF_INET, extack); 3295 if (err) 3296 return err; 3297 } 3298 3299 if (tb[RTA_SPORT]) 3300 sport = nla_get_be16(tb[RTA_SPORT]); 3301 3302 if (tb[RTA_DPORT]) 3303 dport = nla_get_be16(tb[RTA_DPORT]); 3304 3305 skb = inet_rtm_getroute_build_skb(src, dst, ip_proto, sport, dport); 3306 if (!skb) 3307 return -ENOBUFS; 3308 3309 fl4.daddr = dst; 3310 fl4.saddr = src; 3311 fl4.flowi4_tos = rtm->rtm_tos & IPTOS_RT_MASK; 3312 fl4.flowi4_oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0; 3313 fl4.flowi4_mark = mark; 3314 fl4.flowi4_uid = uid; 3315 if (sport) 3316 fl4.fl4_sport = sport; 3317 if (dport) 3318 fl4.fl4_dport = dport; 3319 fl4.flowi4_proto = ip_proto; 3320 3321 rcu_read_lock(); 3322 3323 if (iif) { 3324 struct net_device *dev; 3325 3326 dev = dev_get_by_index_rcu(net, iif); 3327 if (!dev) { 3328 err = -ENODEV; 3329 goto errout_rcu; 3330 } 3331 3332 fl4.flowi4_iif = iif; /* for rt_fill_info */ 3333 skb->dev = dev; 3334 skb->mark = mark; 3335 err = ip_route_input_rcu(skb, dst, src, 3336 rtm->rtm_tos & IPTOS_RT_MASK, dev, 3337 &res); 3338 3339 rt = skb_rtable(skb); 3340 if (err == 0 && rt->dst.error) 3341 err = -rt->dst.error; 3342 } else { 3343 fl4.flowi4_iif = LOOPBACK_IFINDEX; 3344 skb->dev = net->loopback_dev; 3345 rt = ip_route_output_key_hash_rcu(net, &fl4, &res, skb); 3346 err = 0; 3347 if (IS_ERR(rt)) 3348 err = PTR_ERR(rt); 3349 else 3350 skb_dst_set(skb, &rt->dst); 3351 } 3352 3353 if (err) 3354 goto errout_rcu; 3355 3356 if (rtm->rtm_flags & RTM_F_NOTIFY) 3357 rt->rt_flags |= RTCF_NOTIFY; 3358 3359 if (rtm->rtm_flags & RTM_F_LOOKUP_TABLE) 3360 table_id = res.table ? res.table->tb_id : 0; 3361 3362 /* reset skb for netlink reply msg */ 3363 skb_trim(skb, 0); 3364 skb_reset_network_header(skb); 3365 skb_reset_transport_header(skb); 3366 skb_reset_mac_header(skb); 3367 3368 if (rtm->rtm_flags & RTM_F_FIB_MATCH) { 3369 struct fib_rt_info fri; 3370 3371 if (!res.fi) { 3372 err = fib_props[res.type].error; 3373 if (!err) 3374 err = -EHOSTUNREACH; 3375 goto errout_rcu; 3376 } 3377 fri.fi = res.fi; 3378 fri.tb_id = table_id; 3379 fri.dst = res.prefix; 3380 fri.dst_len = res.prefixlen; 3381 fri.tos = fl4.flowi4_tos; 3382 fri.type = rt->rt_type; 3383 fri.offload = 0; 3384 fri.trap = 0; 3385 fri.offload_failed = 0; 3386 if (res.fa_head) { 3387 struct fib_alias *fa; 3388 3389 hlist_for_each_entry_rcu(fa, res.fa_head, fa_list) { 3390 u8 slen = 32 - fri.dst_len; 3391 3392 if (fa->fa_slen == slen && 3393 fa->tb_id == fri.tb_id && 3394 fa->fa_tos == fri.tos && 3395 fa->fa_info == res.fi && 3396 fa->fa_type == fri.type) { 3397 fri.offload = fa->offload; 3398 fri.trap = fa->trap; 3399 break; 3400 } 3401 } 3402 } 3403 err = fib_dump_info(skb, NETLINK_CB(in_skb).portid, 3404 nlh->nlmsg_seq, RTM_NEWROUTE, &fri, 0); 3405 } else { 3406 err = rt_fill_info(net, dst, src, rt, table_id, &fl4, skb, 3407 NETLINK_CB(in_skb).portid, 3408 nlh->nlmsg_seq, 0); 3409 } 3410 if (err < 0) 3411 goto errout_rcu; 3412 3413 rcu_read_unlock(); 3414 3415 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 3416 3417 errout_free: 3418 return err; 3419 errout_rcu: 3420 rcu_read_unlock(); 3421 kfree_skb(skb); 3422 goto errout_free; 3423 } 3424 3425 void ip_rt_multicast_event(struct in_device *in_dev) 3426 { 3427 rt_cache_flush(dev_net(in_dev->dev)); 3428 } 3429 3430 #ifdef CONFIG_SYSCTL 3431 static int ip_rt_gc_interval __read_mostly = 60 * HZ; 3432 static int ip_rt_gc_min_interval __read_mostly = HZ / 2; 3433 static int ip_rt_gc_elasticity __read_mostly = 8; 3434 static int ip_min_valid_pmtu __read_mostly = IPV4_MIN_MTU; 3435 3436 static int ipv4_sysctl_rtcache_flush(struct ctl_table *__ctl, int write, 3437 void *buffer, size_t *lenp, loff_t *ppos) 3438 { 3439 struct net *net = (struct net *)__ctl->extra1; 3440 3441 if (write) { 3442 rt_cache_flush(net); 3443 fnhe_genid_bump(net); 3444 return 0; 3445 } 3446 3447 return -EINVAL; 3448 } 3449 3450 static struct ctl_table ipv4_route_table[] = { 3451 { 3452 .procname = "gc_thresh", 3453 .data = &ipv4_dst_ops.gc_thresh, 3454 .maxlen = sizeof(int), 3455 .mode = 0644, 3456 .proc_handler = proc_dointvec, 3457 }, 3458 { 3459 .procname = "max_size", 3460 .data = &ip_rt_max_size, 3461 .maxlen = sizeof(int), 3462 .mode = 0644, 3463 .proc_handler = proc_dointvec, 3464 }, 3465 { 3466 /* Deprecated. Use gc_min_interval_ms */ 3467 3468 .procname = "gc_min_interval", 3469 .data = &ip_rt_gc_min_interval, 3470 .maxlen = sizeof(int), 3471 .mode = 0644, 3472 .proc_handler = proc_dointvec_jiffies, 3473 }, 3474 { 3475 .procname = "gc_min_interval_ms", 3476 .data = &ip_rt_gc_min_interval, 3477 .maxlen = sizeof(int), 3478 .mode = 0644, 3479 .proc_handler = proc_dointvec_ms_jiffies, 3480 }, 3481 { 3482 .procname = "gc_timeout", 3483 .data = &ip_rt_gc_timeout, 3484 .maxlen = sizeof(int), 3485 .mode = 0644, 3486 .proc_handler = proc_dointvec_jiffies, 3487 }, 3488 { 3489 .procname = "gc_interval", 3490 .data = &ip_rt_gc_interval, 3491 .maxlen = sizeof(int), 3492 .mode = 0644, 3493 .proc_handler = proc_dointvec_jiffies, 3494 }, 3495 { 3496 .procname = "redirect_load", 3497 .data = &ip_rt_redirect_load, 3498 .maxlen = sizeof(int), 3499 .mode = 0644, 3500 .proc_handler = proc_dointvec, 3501 }, 3502 { 3503 .procname = "redirect_number", 3504 .data = &ip_rt_redirect_number, 3505 .maxlen = sizeof(int), 3506 .mode = 0644, 3507 .proc_handler = proc_dointvec, 3508 }, 3509 { 3510 .procname = "redirect_silence", 3511 .data = &ip_rt_redirect_silence, 3512 .maxlen = sizeof(int), 3513 .mode = 0644, 3514 .proc_handler = proc_dointvec, 3515 }, 3516 { 3517 .procname = "error_cost", 3518 .data = &ip_rt_error_cost, 3519 .maxlen = sizeof(int), 3520 .mode = 0644, 3521 .proc_handler = proc_dointvec, 3522 }, 3523 { 3524 .procname = "error_burst", 3525 .data = &ip_rt_error_burst, 3526 .maxlen = sizeof(int), 3527 .mode = 0644, 3528 .proc_handler = proc_dointvec, 3529 }, 3530 { 3531 .procname = "gc_elasticity", 3532 .data = &ip_rt_gc_elasticity, 3533 .maxlen = sizeof(int), 3534 .mode = 0644, 3535 .proc_handler = proc_dointvec, 3536 }, 3537 { 3538 .procname = "mtu_expires", 3539 .data = &ip_rt_mtu_expires, 3540 .maxlen = sizeof(int), 3541 .mode = 0644, 3542 .proc_handler = proc_dointvec_jiffies, 3543 }, 3544 { 3545 .procname = "min_pmtu", 3546 .data = &ip_rt_min_pmtu, 3547 .maxlen = sizeof(int), 3548 .mode = 0644, 3549 .proc_handler = proc_dointvec_minmax, 3550 .extra1 = &ip_min_valid_pmtu, 3551 }, 3552 { 3553 .procname = "min_adv_mss", 3554 .data = &ip_rt_min_advmss, 3555 .maxlen = sizeof(int), 3556 .mode = 0644, 3557 .proc_handler = proc_dointvec, 3558 }, 3559 { } 3560 }; 3561 3562 static const char ipv4_route_flush_procname[] = "flush"; 3563 3564 static struct ctl_table ipv4_route_flush_table[] = { 3565 { 3566 .procname = ipv4_route_flush_procname, 3567 .maxlen = sizeof(int), 3568 .mode = 0200, 3569 .proc_handler = ipv4_sysctl_rtcache_flush, 3570 }, 3571 { }, 3572 }; 3573 3574 static __net_init int sysctl_route_net_init(struct net *net) 3575 { 3576 struct ctl_table *tbl; 3577 3578 tbl = ipv4_route_flush_table; 3579 if (!net_eq(net, &init_net)) { 3580 tbl = kmemdup(tbl, sizeof(ipv4_route_flush_table), GFP_KERNEL); 3581 if (!tbl) 3582 goto err_dup; 3583 3584 /* Don't export non-whitelisted sysctls to unprivileged users */ 3585 if (net->user_ns != &init_user_ns) { 3586 if (tbl[0].procname != ipv4_route_flush_procname) 3587 tbl[0].procname = NULL; 3588 } 3589 } 3590 tbl[0].extra1 = net; 3591 3592 net->ipv4.route_hdr = register_net_sysctl(net, "net/ipv4/route", tbl); 3593 if (!net->ipv4.route_hdr) 3594 goto err_reg; 3595 return 0; 3596 3597 err_reg: 3598 if (tbl != ipv4_route_flush_table) 3599 kfree(tbl); 3600 err_dup: 3601 return -ENOMEM; 3602 } 3603 3604 static __net_exit void sysctl_route_net_exit(struct net *net) 3605 { 3606 struct ctl_table *tbl; 3607 3608 tbl = net->ipv4.route_hdr->ctl_table_arg; 3609 unregister_net_sysctl_table(net->ipv4.route_hdr); 3610 BUG_ON(tbl == ipv4_route_flush_table); 3611 kfree(tbl); 3612 } 3613 3614 static __net_initdata struct pernet_operations sysctl_route_ops = { 3615 .init = sysctl_route_net_init, 3616 .exit = sysctl_route_net_exit, 3617 }; 3618 #endif 3619 3620 static __net_init int rt_genid_init(struct net *net) 3621 { 3622 atomic_set(&net->ipv4.rt_genid, 0); 3623 atomic_set(&net->fnhe_genid, 0); 3624 atomic_set(&net->ipv4.dev_addr_genid, get_random_int()); 3625 return 0; 3626 } 3627 3628 static __net_initdata struct pernet_operations rt_genid_ops = { 3629 .init = rt_genid_init, 3630 }; 3631 3632 static int __net_init ipv4_inetpeer_init(struct net *net) 3633 { 3634 struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 3635 3636 if (!bp) 3637 return -ENOMEM; 3638 inet_peer_base_init(bp); 3639 net->ipv4.peers = bp; 3640 return 0; 3641 } 3642 3643 static void __net_exit ipv4_inetpeer_exit(struct net *net) 3644 { 3645 struct inet_peer_base *bp = net->ipv4.peers; 3646 3647 net->ipv4.peers = NULL; 3648 inetpeer_invalidate_tree(bp); 3649 kfree(bp); 3650 } 3651 3652 static __net_initdata struct pernet_operations ipv4_inetpeer_ops = { 3653 .init = ipv4_inetpeer_init, 3654 .exit = ipv4_inetpeer_exit, 3655 }; 3656 3657 #ifdef CONFIG_IP_ROUTE_CLASSID 3658 struct ip_rt_acct __percpu *ip_rt_acct __read_mostly; 3659 #endif /* CONFIG_IP_ROUTE_CLASSID */ 3660 3661 int __init ip_rt_init(void) 3662 { 3663 void *idents_hash; 3664 int cpu; 3665 3666 /* For modern hosts, this will use 2 MB of memory */ 3667 idents_hash = alloc_large_system_hash("IP idents", 3668 sizeof(*ip_idents) + sizeof(*ip_tstamps), 3669 0, 3670 16, /* one bucket per 64 KB */ 3671 HASH_ZERO, 3672 NULL, 3673 &ip_idents_mask, 3674 2048, 3675 256*1024); 3676 3677 ip_idents = idents_hash; 3678 3679 prandom_bytes(ip_idents, (ip_idents_mask + 1) * sizeof(*ip_idents)); 3680 3681 ip_tstamps = idents_hash + (ip_idents_mask + 1) * sizeof(*ip_idents); 3682 3683 for_each_possible_cpu(cpu) { 3684 struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu); 3685 3686 INIT_LIST_HEAD(&ul->head); 3687 spin_lock_init(&ul->lock); 3688 } 3689 #ifdef CONFIG_IP_ROUTE_CLASSID 3690 ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct)); 3691 if (!ip_rt_acct) 3692 panic("IP: failed to allocate ip_rt_acct\n"); 3693 #endif 3694 3695 ipv4_dst_ops.kmem_cachep = 3696 kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0, 3697 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); 3698 3699 ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep; 3700 3701 if (dst_entries_init(&ipv4_dst_ops) < 0) 3702 panic("IP: failed to allocate ipv4_dst_ops counter\n"); 3703 3704 if (dst_entries_init(&ipv4_dst_blackhole_ops) < 0) 3705 panic("IP: failed to allocate ipv4_dst_blackhole_ops counter\n"); 3706 3707 ipv4_dst_ops.gc_thresh = ~0; 3708 ip_rt_max_size = INT_MAX; 3709 3710 devinet_init(); 3711 ip_fib_init(); 3712 3713 if (ip_rt_proc_init()) 3714 pr_err("Unable to create route proc files\n"); 3715 #ifdef CONFIG_XFRM 3716 xfrm_init(); 3717 xfrm4_init(); 3718 #endif 3719 rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL, 3720 RTNL_FLAG_DOIT_UNLOCKED); 3721 3722 #ifdef CONFIG_SYSCTL 3723 register_pernet_subsys(&sysctl_route_ops); 3724 #endif 3725 register_pernet_subsys(&rt_genid_ops); 3726 register_pernet_subsys(&ipv4_inetpeer_ops); 3727 return 0; 3728 } 3729 3730 #ifdef CONFIG_SYSCTL 3731 /* 3732 * We really need to sanitize the damn ipv4 init order, then all 3733 * this nonsense will go away. 3734 */ 3735 void __init ip_static_sysctl_init(void) 3736 { 3737 register_net_sysctl(&init_net, "net/ipv4/route", ipv4_route_table); 3738 } 3739 #endif 3740