2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * ROUTE - implementation of the IP router.
8 * Version: $Id: route.c,v 1.103 2002/01/12 07:44:09 davem Exp $
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Alan Cox, <gw4pts@gw4pts.ampr.org>
13 * Linus Torvalds, <Linus.Torvalds@helsinki.fi>
14 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
17 * Alan Cox : Verify area fixes.
18 * Alan Cox : cli() protects routing changes
19 * Rui Oliveira : ICMP routing table updates
20 * (rco@di.uminho.pt) Routing table insertion and update
21 * Linus Torvalds : Rewrote bits to be sensible
22 * Alan Cox : Added BSD route gw semantics
23 * Alan Cox : Super /proc >4K
24 * Alan Cox : MTU in route table
25 * Alan Cox : MSS actually. Also added the window
27 * Sam Lantinga : Fixed route matching in rt_del()
28 * Alan Cox : Routing cache support.
29 * Alan Cox : Removed compatibility cruft.
30 * Alan Cox : RTF_REJECT support.
31 * Alan Cox : TCP irtt support.
32 * Jonathan Naylor : Added Metric support.
33 * Miquel van Smoorenburg : BSD API fixes.
34 * Miquel van Smoorenburg : Metrics.
35 * Alan Cox : Use __u32 properly
36 * Alan Cox : Aligned routing errors more closely with BSD
37 * our system is still very different.
38 * Alan Cox : Faster /proc handling
39 * Alexey Kuznetsov : Massive rework to support tree based routing,
40 * routing caches and better behaviour.
42 * Olaf Erb : irtt wasn't being copied right.
43 * Bjorn Ekwall : Kerneld route support.
44 * Alan Cox : Multicast fixed (I hope)
45 * Pavel Krauz : Limited broadcast fixed
46 * Mike McLagan : Routing by source
47 * Alexey Kuznetsov : End of old history. Split to fib.c and
48 * route.c and rewritten from scratch.
49 * Andi Kleen : Load-limit warning messages.
50 * Vitaly E. Lavrov : Transparent proxy revived after year coma.
51 * Vitaly E. Lavrov : Race condition in ip_route_input_slow.
52 * Tobias Ringstrom : Uninitialized res.type in ip_route_output_slow.
53 * Vladimir V. Ivanov : IP rule info (flowid) is really useful.
54 * Marc Boucher : routing by fwmark
55 * Robert Olsson : Added rt_cache statistics
56 * Arnaldo C. Melo : Convert proc stuff to seq_file
57 * Eric Dumazet : hashed spinlocks and rt_check_expire() fixes.
58 * Ilia Sotnikov : Ignore TOS on PMTUD and Redirect
59 * Ilia Sotnikov : Removed TOS from hash calculations
61 * This program is free software; you can redistribute it and/or
62 * modify it under the terms of the GNU General Public License
63 * as published by the Free Software Foundation; either version
64 * 2 of the License, or (at your option) any later version.
67 #include <linux/module.h>
68 #include <asm/uaccess.h>
69 #include <asm/system.h>
70 #include <linux/bitops.h>
71 #include <linux/types.h>
72 #include <linux/kernel.h>
74 #include <linux/bootmem.h>
75 #include <linux/string.h>
76 #include <linux/socket.h>
77 #include <linux/sockios.h>
78 #include <linux/errno.h>
80 #include <linux/inet.h>
81 #include <linux/netdevice.h>
82 #include <linux/proc_fs.h>
83 #include <linux/init.h>
84 #include <linux/workqueue.h>
85 #include <linux/skbuff.h>
86 #include <linux/inetdevice.h>
87 #include <linux/igmp.h>
88 #include <linux/pkt_sched.h>
89 #include <linux/mroute.h>
90 #include <linux/netfilter_ipv4.h>
91 #include <linux/random.h>
92 #include <linux/jhash.h>
93 #include <linux/rcupdate.h>
94 #include <linux/times.h>
96 #include <net/net_namespace.h>
97 #include <net/protocol.h>
99 #include <net/route.h>
100 #include <net/inetpeer.h>
101 #include <net/sock.h>
102 #include <net/ip_fib.h>
105 #include <net/icmp.h>
106 #include <net/xfrm.h>
107 #include <net/netevent.h>
108 #include <net/rtnetlink.h>
110 #include <linux/sysctl.h>
113 #define RT_FL_TOS(oldflp) \
114 ((u32)(oldflp->fl4_tos & (IPTOS_RT_MASK | RTO_ONLINK)))
116 #define IP_MAX_MTU 0xFFF0
118 #define RT_GC_TIMEOUT (300*HZ)
120 static int ip_rt_min_delay = 2 * HZ;
121 static int ip_rt_max_delay = 10 * HZ;
122 static int ip_rt_max_size;
123 static int ip_rt_gc_timeout = RT_GC_TIMEOUT;
124 static int ip_rt_gc_interval = 60 * HZ;
125 static int ip_rt_gc_min_interval = HZ / 2;
126 static int ip_rt_redirect_number = 9;
127 static int ip_rt_redirect_load = HZ / 50;
128 static int ip_rt_redirect_silence = ((HZ / 50) << (9 + 1));
129 static int ip_rt_error_cost = HZ;
130 static int ip_rt_error_burst = 5 * HZ;
131 static int ip_rt_gc_elasticity = 8;
132 static int ip_rt_mtu_expires = 10 * 60 * HZ;
133 static int ip_rt_min_pmtu = 512 + 20 + 20;
134 static int ip_rt_min_advmss = 256;
135 static int ip_rt_secret_interval = 10 * 60 * HZ;
136 static unsigned long rt_deadline;
138 #define RTprint(a...) printk(KERN_DEBUG a)
140 static struct timer_list rt_flush_timer;
141 static void rt_check_expire(struct work_struct *work);
142 static DECLARE_DELAYED_WORK(expires_work, rt_check_expire);
143 static struct timer_list rt_secret_timer;
146 * Interface to generic destination cache.
149 static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
150 static void ipv4_dst_destroy(struct dst_entry *dst);
151 static void ipv4_dst_ifdown(struct dst_entry *dst,
152 struct net_device *dev, int how);
153 static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst);
154 static void ipv4_link_failure(struct sk_buff *skb);
155 static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
156 static int rt_garbage_collect(void);
159 static struct dst_ops ipv4_dst_ops = {
161 .protocol = __constant_htons(ETH_P_IP),
162 .gc = rt_garbage_collect,
163 .check = ipv4_dst_check,
164 .destroy = ipv4_dst_destroy,
165 .ifdown = ipv4_dst_ifdown,
166 .negative_advice = ipv4_negative_advice,
167 .link_failure = ipv4_link_failure,
168 .update_pmtu = ip_rt_update_pmtu,
169 .entry_size = sizeof(struct rtable),
172 #define ECN_OR_COST(class) TC_PRIO_##class
174 const __u8 ip_tos2prio[16] = {
178 ECN_OR_COST(BESTEFFORT),
184 ECN_OR_COST(INTERACTIVE),
186 ECN_OR_COST(INTERACTIVE),
187 TC_PRIO_INTERACTIVE_BULK,
188 ECN_OR_COST(INTERACTIVE_BULK),
189 TC_PRIO_INTERACTIVE_BULK,
190 ECN_OR_COST(INTERACTIVE_BULK)
198 /* The locking scheme is rather straight forward:
200 * 1) Read-Copy Update protects the buckets of the central route hash.
201 * 2) Only writers remove entries, and they hold the lock
202 * as they look at rtable reference counts.
203 * 3) Only readers acquire references to rtable entries,
204 * they do so with atomic increments and with the
208 struct rt_hash_bucket {
209 struct rtable *chain;
211 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
212 defined(CONFIG_PROVE_LOCKING)
214 * Instead of using one spinlock for each rt_hash_bucket, we use a table of spinlocks
215 * The size of this table is a power of two and depends on the number of CPUS.
216 * (on lockdep we have a quite big spinlock_t, so keep the size down there)
218 #ifdef CONFIG_LOCKDEP
219 # define RT_HASH_LOCK_SZ 256
222 # define RT_HASH_LOCK_SZ 4096
224 # define RT_HASH_LOCK_SZ 2048
226 # define RT_HASH_LOCK_SZ 1024
228 # define RT_HASH_LOCK_SZ 512
230 # define RT_HASH_LOCK_SZ 256
234 static spinlock_t *rt_hash_locks;
235 # define rt_hash_lock_addr(slot) &rt_hash_locks[(slot) & (RT_HASH_LOCK_SZ - 1)]
236 # define rt_hash_lock_init() { \
238 rt_hash_locks = kmalloc(sizeof(spinlock_t) * RT_HASH_LOCK_SZ, GFP_KERNEL); \
239 if (!rt_hash_locks) panic("IP: failed to allocate rt_hash_locks\n"); \
240 for (i = 0; i < RT_HASH_LOCK_SZ; i++) \
241 spin_lock_init(&rt_hash_locks[i]); \
244 # define rt_hash_lock_addr(slot) NULL
245 # define rt_hash_lock_init()
248 static struct rt_hash_bucket *rt_hash_table;
249 static unsigned rt_hash_mask;
250 static unsigned int rt_hash_log;
251 static unsigned int rt_hash_rnd;
253 static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
254 #define RT_CACHE_STAT_INC(field) \
255 (__raw_get_cpu_var(rt_cache_stat).field++)
257 static int rt_intern_hash(unsigned hash, struct rtable *rth,
258 struct rtable **res);
260 static unsigned int rt_hash_code(u32 daddr, u32 saddr)
262 return (jhash_2words(daddr, saddr, rt_hash_rnd)
266 #define rt_hash(daddr, saddr, idx) \
267 rt_hash_code((__force u32)(__be32)(daddr),\
268 (__force u32)(__be32)(saddr) ^ ((idx) << 5))
270 #ifdef CONFIG_PROC_FS
271 struct rt_cache_iter_state {
275 static struct rtable *rt_cache_get_first(struct seq_file *seq)
277 struct rtable *r = NULL;
278 struct rt_cache_iter_state *st = seq->private;
280 for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
282 r = rt_hash_table[st->bucket].chain;
285 rcu_read_unlock_bh();
287 return rcu_dereference(r);
290 static struct rtable *rt_cache_get_next(struct seq_file *seq, struct rtable *r)
292 struct rt_cache_iter_state *st = seq->private;
294 r = r->u.dst.rt_next;
296 rcu_read_unlock_bh();
297 if (--st->bucket < 0)
300 r = rt_hash_table[st->bucket].chain;
302 return rcu_dereference(r);
305 static struct rtable *rt_cache_get_idx(struct seq_file *seq, loff_t pos)
307 struct rtable *r = rt_cache_get_first(seq);
310 while (pos && (r = rt_cache_get_next(seq, r)))
312 return pos ? NULL : r;
315 static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
317 return *pos ? rt_cache_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
320 static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
322 struct rtable *r = NULL;
324 if (v == SEQ_START_TOKEN)
325 r = rt_cache_get_first(seq);
327 r = rt_cache_get_next(seq, v);
332 static void rt_cache_seq_stop(struct seq_file *seq, void *v)
334 if (v && v != SEQ_START_TOKEN)
335 rcu_read_unlock_bh();
338 static int rt_cache_seq_show(struct seq_file *seq, void *v)
340 if (v == SEQ_START_TOKEN)
341 seq_printf(seq, "%-127s\n",
342 "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
343 "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
346 struct rtable *r = v;
349 sprintf(temp, "%s\t%08lX\t%08lX\t%8X\t%d\t%u\t%d\t"
350 "%08lX\t%d\t%u\t%u\t%02X\t%d\t%1d\t%08X",
351 r->u.dst.dev ? r->u.dst.dev->name : "*",
352 (unsigned long)r->rt_dst, (unsigned long)r->rt_gateway,
353 r->rt_flags, atomic_read(&r->u.dst.__refcnt),
354 r->u.dst.__use, 0, (unsigned long)r->rt_src,
355 (dst_metric(&r->u.dst, RTAX_ADVMSS) ?
356 (int)dst_metric(&r->u.dst, RTAX_ADVMSS) + 40 : 0),
357 dst_metric(&r->u.dst, RTAX_WINDOW),
358 (int)((dst_metric(&r->u.dst, RTAX_RTT) >> 3) +
359 dst_metric(&r->u.dst, RTAX_RTTVAR)),
361 r->u.dst.hh ? atomic_read(&r->u.dst.hh->hh_refcnt) : -1,
362 r->u.dst.hh ? (r->u.dst.hh->hh_output ==
365 seq_printf(seq, "%-127s\n", temp);
370 static const struct seq_operations rt_cache_seq_ops = {
371 .start = rt_cache_seq_start,
372 .next = rt_cache_seq_next,
373 .stop = rt_cache_seq_stop,
374 .show = rt_cache_seq_show,
377 static int rt_cache_seq_open(struct inode *inode, struct file *file)
379 return seq_open_private(file, &rt_cache_seq_ops,
380 sizeof(struct rt_cache_iter_state));
383 static const struct file_operations rt_cache_seq_fops = {
384 .owner = THIS_MODULE,
385 .open = rt_cache_seq_open,
388 .release = seq_release_private,
392 static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
397 return SEQ_START_TOKEN;
399 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
400 if (!cpu_possible(cpu))
403 return &per_cpu(rt_cache_stat, cpu);
408 static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
412 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
413 if (!cpu_possible(cpu))
416 return &per_cpu(rt_cache_stat, cpu);
422 static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
427 static int rt_cpu_seq_show(struct seq_file *seq, void *v)
429 struct rt_cache_stat *st = v;
431 if (v == SEQ_START_TOKEN) {
432 seq_printf(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");
436 seq_printf(seq,"%08x %08x %08x %08x %08x %08x %08x %08x "
437 " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
438 atomic_read(&ipv4_dst_ops.entries),
461 static const struct seq_operations rt_cpu_seq_ops = {
462 .start = rt_cpu_seq_start,
463 .next = rt_cpu_seq_next,
464 .stop = rt_cpu_seq_stop,
465 .show = rt_cpu_seq_show,
469 static int rt_cpu_seq_open(struct inode *inode, struct file *file)
471 return seq_open(file, &rt_cpu_seq_ops);
474 static const struct file_operations rt_cpu_seq_fops = {
475 .owner = THIS_MODULE,
476 .open = rt_cpu_seq_open,
479 .release = seq_release,
482 #endif /* CONFIG_PROC_FS */
484 static __inline__ void rt_free(struct rtable *rt)
486 call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
489 static __inline__ void rt_drop(struct rtable *rt)
492 call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
495 static __inline__ int rt_fast_clean(struct rtable *rth)
497 /* Kill broadcast/multicast entries very aggresively, if they
498 collide in hash table with more useful entries */
499 return (rth->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) &&
500 rth->fl.iif && rth->u.dst.rt_next;
503 static __inline__ int rt_valuable(struct rtable *rth)
505 return (rth->rt_flags & (RTCF_REDIRECTED | RTCF_NOTIFY)) ||
509 static int rt_may_expire(struct rtable *rth, unsigned long tmo1, unsigned long tmo2)
514 if (atomic_read(&rth->u.dst.__refcnt))
518 if (rth->u.dst.expires &&
519 time_after_eq(jiffies, rth->u.dst.expires))
522 age = jiffies - rth->u.dst.lastuse;
524 if ((age <= tmo1 && !rt_fast_clean(rth)) ||
525 (age <= tmo2 && rt_valuable(rth)))
531 /* Bits of score are:
533 * 30: not quite useless
534 * 29..0: usage counter
536 static inline u32 rt_score(struct rtable *rt)
538 u32 score = jiffies - rt->u.dst.lastuse;
540 score = ~score & ~(3<<30);
546 !(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL)))
552 static inline int compare_keys(struct flowi *fl1, struct flowi *fl2)
554 return ((__force u32)((fl1->nl_u.ip4_u.daddr ^ fl2->nl_u.ip4_u.daddr) |
555 (fl1->nl_u.ip4_u.saddr ^ fl2->nl_u.ip4_u.saddr)) |
556 (fl1->mark ^ fl2->mark) |
557 (*(u16 *)&fl1->nl_u.ip4_u.tos ^
558 *(u16 *)&fl2->nl_u.ip4_u.tos) |
559 (fl1->oif ^ fl2->oif) |
560 (fl1->iif ^ fl2->iif)) == 0;
563 static void rt_check_expire(struct work_struct *work)
565 static unsigned int rover;
566 unsigned int i = rover, goal;
567 struct rtable *rth, **rthp;
570 mult = ((u64)ip_rt_gc_interval) << rt_hash_log;
571 if (ip_rt_gc_timeout > 1)
572 do_div(mult, ip_rt_gc_timeout);
573 goal = (unsigned int)mult;
574 if (goal > rt_hash_mask)
575 goal = rt_hash_mask + 1;
576 for (; goal > 0; goal--) {
577 unsigned long tmo = ip_rt_gc_timeout;
579 i = (i + 1) & rt_hash_mask;
580 rthp = &rt_hash_table[i].chain;
587 spin_lock_bh(rt_hash_lock_addr(i));
588 while ((rth = *rthp) != NULL) {
589 if (rth->u.dst.expires) {
590 /* Entry is expired even if it is in use */
591 if (time_before_eq(jiffies, rth->u.dst.expires)) {
593 rthp = &rth->u.dst.rt_next;
596 } else if (!rt_may_expire(rth, tmo, ip_rt_gc_timeout)) {
598 rthp = &rth->u.dst.rt_next;
602 /* Cleanup aged off entries. */
603 *rthp = rth->u.dst.rt_next;
606 spin_unlock_bh(rt_hash_lock_addr(i));
609 schedule_delayed_work(&expires_work, ip_rt_gc_interval);
612 /* This can run from both BH and non-BH contexts, the latter
613 * in the case of a forced flush event.
615 static void rt_run_flush(unsigned long dummy)
618 struct rtable *rth, *next;
622 get_random_bytes(&rt_hash_rnd, 4);
624 for (i = rt_hash_mask; i >= 0; i--) {
625 spin_lock_bh(rt_hash_lock_addr(i));
626 rth = rt_hash_table[i].chain;
628 rt_hash_table[i].chain = NULL;
629 spin_unlock_bh(rt_hash_lock_addr(i));
631 for (; rth; rth = next) {
632 next = rth->u.dst.rt_next;
638 static DEFINE_SPINLOCK(rt_flush_lock);
640 void rt_cache_flush(int delay)
642 unsigned long now = jiffies;
643 int user_mode = !in_softirq();
646 delay = ip_rt_min_delay;
648 spin_lock_bh(&rt_flush_lock);
650 if (del_timer(&rt_flush_timer) && delay > 0 && rt_deadline) {
651 long tmo = (long)(rt_deadline - now);
653 /* If flush timer is already running
654 and flush request is not immediate (delay > 0):
656 if deadline is not achieved, prolongate timer to "delay",
657 otherwise fire it at deadline time.
660 if (user_mode && tmo < ip_rt_max_delay-ip_rt_min_delay)
668 spin_unlock_bh(&rt_flush_lock);
673 if (rt_deadline == 0)
674 rt_deadline = now + ip_rt_max_delay;
676 mod_timer(&rt_flush_timer, now+delay);
677 spin_unlock_bh(&rt_flush_lock);
680 static void rt_secret_rebuild(unsigned long dummy)
682 unsigned long now = jiffies;
685 mod_timer(&rt_secret_timer, now + ip_rt_secret_interval);
689 Short description of GC goals.
691 We want to build algorithm, which will keep routing cache
692 at some equilibrium point, when number of aged off entries
693 is kept approximately equal to newly generated ones.
695 Current expiration strength is variable "expire".
696 We try to adjust it dynamically, so that if networking
697 is idle expires is large enough to keep enough of warm entries,
698 and when load increases it reduces to limit cache size.
701 static int rt_garbage_collect(void)
703 static unsigned long expire = RT_GC_TIMEOUT;
704 static unsigned long last_gc;
706 static int equilibrium;
707 struct rtable *rth, **rthp;
708 unsigned long now = jiffies;
712 * Garbage collection is pretty expensive,
713 * do not make it too frequently.
716 RT_CACHE_STAT_INC(gc_total);
718 if (now - last_gc < ip_rt_gc_min_interval &&
719 atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size) {
720 RT_CACHE_STAT_INC(gc_ignored);
724 /* Calculate number of entries, which we want to expire now. */
725 goal = atomic_read(&ipv4_dst_ops.entries) -
726 (ip_rt_gc_elasticity << rt_hash_log);
728 if (equilibrium < ipv4_dst_ops.gc_thresh)
729 equilibrium = ipv4_dst_ops.gc_thresh;
730 goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
732 equilibrium += min_t(unsigned int, goal / 2, rt_hash_mask + 1);
733 goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
736 /* We are in dangerous area. Try to reduce cache really
739 goal = max_t(unsigned int, goal / 2, rt_hash_mask + 1);
740 equilibrium = atomic_read(&ipv4_dst_ops.entries) - goal;
743 if (now - last_gc >= ip_rt_gc_min_interval)
754 for (i = rt_hash_mask, k = rover; i >= 0; i--) {
755 unsigned long tmo = expire;
757 k = (k + 1) & rt_hash_mask;
758 rthp = &rt_hash_table[k].chain;
759 spin_lock_bh(rt_hash_lock_addr(k));
760 while ((rth = *rthp) != NULL) {
761 if (!rt_may_expire(rth, tmo, expire)) {
763 rthp = &rth->u.dst.rt_next;
766 *rthp = rth->u.dst.rt_next;
770 spin_unlock_bh(rt_hash_lock_addr(k));
779 /* Goal is not achieved. We stop process if:
781 - if expire reduced to zero. Otherwise, expire is halfed.
782 - if table is not full.
783 - if we are called from interrupt.
784 - jiffies check is just fallback/debug loop breaker.
785 We will not spin here for long time in any case.
788 RT_CACHE_STAT_INC(gc_goal_miss);
794 #if RT_CACHE_DEBUG >= 2
795 printk(KERN_DEBUG "expire>> %u %d %d %d\n", expire,
796 atomic_read(&ipv4_dst_ops.entries), goal, i);
799 if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
801 } while (!in_softirq() && time_before_eq(jiffies, now));
803 if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
806 printk(KERN_WARNING "dst cache overflow\n");
807 RT_CACHE_STAT_INC(gc_dst_overflow);
811 expire += ip_rt_gc_min_interval;
812 if (expire > ip_rt_gc_timeout ||
813 atomic_read(&ipv4_dst_ops.entries) < ipv4_dst_ops.gc_thresh)
814 expire = ip_rt_gc_timeout;
815 #if RT_CACHE_DEBUG >= 2
816 printk(KERN_DEBUG "expire++ %u %d %d %d\n", expire,
817 atomic_read(&ipv4_dst_ops.entries), goal, rover);
822 static int rt_intern_hash(unsigned hash, struct rtable *rt, struct rtable **rp)
824 struct rtable *rth, **rthp;
826 struct rtable *cand, **candp;
829 int attempts = !in_softirq();
838 rthp = &rt_hash_table[hash].chain;
840 spin_lock_bh(rt_hash_lock_addr(hash));
841 while ((rth = *rthp) != NULL) {
842 if (compare_keys(&rth->fl, &rt->fl)) {
844 *rthp = rth->u.dst.rt_next;
846 * Since lookup is lockfree, the deletion
847 * must be visible to another weakly ordered CPU before
848 * the insertion at the start of the hash chain.
850 rcu_assign_pointer(rth->u.dst.rt_next,
851 rt_hash_table[hash].chain);
853 * Since lookup is lockfree, the update writes
854 * must be ordered for consistency on SMP.
856 rcu_assign_pointer(rt_hash_table[hash].chain, rth);
858 dst_use(&rth->u.dst, now);
859 spin_unlock_bh(rt_hash_lock_addr(hash));
866 if (!atomic_read(&rth->u.dst.__refcnt)) {
867 u32 score = rt_score(rth);
869 if (score <= min_score) {
878 rthp = &rth->u.dst.rt_next;
882 /* ip_rt_gc_elasticity used to be average length of chain
883 * length, when exceeded gc becomes really aggressive.
885 * The second limit is less certain. At the moment it allows
886 * only 2 entries per bucket. We will see.
888 if (chain_length > ip_rt_gc_elasticity) {
889 *candp = cand->u.dst.rt_next;
894 /* Try to bind route to arp only if it is output
895 route or unicast forwarding path.
897 if (rt->rt_type == RTN_UNICAST || rt->fl.iif == 0) {
898 int err = arp_bind_neighbour(&rt->u.dst);
900 spin_unlock_bh(rt_hash_lock_addr(hash));
902 if (err != -ENOBUFS) {
907 /* Neighbour tables are full and nothing
908 can be released. Try to shrink route cache,
909 it is most likely it holds some neighbour records.
911 if (attempts-- > 0) {
912 int saved_elasticity = ip_rt_gc_elasticity;
913 int saved_int = ip_rt_gc_min_interval;
914 ip_rt_gc_elasticity = 1;
915 ip_rt_gc_min_interval = 0;
916 rt_garbage_collect();
917 ip_rt_gc_min_interval = saved_int;
918 ip_rt_gc_elasticity = saved_elasticity;
923 printk(KERN_WARNING "Neighbour table overflow.\n");
929 rt->u.dst.rt_next = rt_hash_table[hash].chain;
930 #if RT_CACHE_DEBUG >= 2
931 if (rt->u.dst.rt_next) {
933 printk(KERN_DEBUG "rt_cache @%02x: %u.%u.%u.%u", hash,
934 NIPQUAD(rt->rt_dst));
935 for (trt = rt->u.dst.rt_next; trt; trt = trt->u.dst.rt_next)
936 printk(" . %u.%u.%u.%u", NIPQUAD(trt->rt_dst));
940 rt_hash_table[hash].chain = rt;
941 spin_unlock_bh(rt_hash_lock_addr(hash));
946 void rt_bind_peer(struct rtable *rt, int create)
948 static DEFINE_SPINLOCK(rt_peer_lock);
949 struct inet_peer *peer;
951 peer = inet_getpeer(rt->rt_dst, create);
953 spin_lock_bh(&rt_peer_lock);
954 if (rt->peer == NULL) {
958 spin_unlock_bh(&rt_peer_lock);
964 * Peer allocation may fail only in serious out-of-memory conditions. However
965 * we still can generate some output.
966 * Random ID selection looks a bit dangerous because we have no chances to
967 * select ID being unique in a reasonable period of time.
968 * But broken packet identifier may be better than no packet at all.
970 static void ip_select_fb_ident(struct iphdr *iph)
972 static DEFINE_SPINLOCK(ip_fb_id_lock);
973 static u32 ip_fallback_id;
976 spin_lock_bh(&ip_fb_id_lock);
977 salt = secure_ip_id((__force __be32)ip_fallback_id ^ iph->daddr);
978 iph->id = htons(salt & 0xFFFF);
979 ip_fallback_id = salt;
980 spin_unlock_bh(&ip_fb_id_lock);
983 void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more)
985 struct rtable *rt = (struct rtable *) dst;
988 if (rt->peer == NULL)
991 /* If peer is attached to destination, it is never detached,
992 so that we need not to grab a lock to dereference it.
995 iph->id = htons(inet_getid(rt->peer, more));
999 printk(KERN_DEBUG "rt_bind_peer(0) @%p\n",
1000 __builtin_return_address(0));
1002 ip_select_fb_ident(iph);
1005 static void rt_del(unsigned hash, struct rtable *rt)
1007 struct rtable **rthp;
1009 spin_lock_bh(rt_hash_lock_addr(hash));
1011 for (rthp = &rt_hash_table[hash].chain; *rthp;
1012 rthp = &(*rthp)->u.dst.rt_next)
1014 *rthp = rt->u.dst.rt_next;
1018 spin_unlock_bh(rt_hash_lock_addr(hash));
1021 void ip_rt_redirect(__be32 old_gw, __be32 daddr, __be32 new_gw,
1022 __be32 saddr, struct net_device *dev)
1025 struct in_device *in_dev = in_dev_get(dev);
1026 struct rtable *rth, **rthp;
1027 __be32 skeys[2] = { saddr, 0 };
1028 int ikeys[2] = { dev->ifindex, 0 };
1029 struct netevent_redirect netevent;
1034 if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev)
1035 || MULTICAST(new_gw) || BADCLASS(new_gw) || ZERONET(new_gw))
1036 goto reject_redirect;
1038 if (!IN_DEV_SHARED_MEDIA(in_dev)) {
1039 if (!inet_addr_onlink(in_dev, new_gw, old_gw))
1040 goto reject_redirect;
1041 if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
1042 goto reject_redirect;
1044 if (inet_addr_type(new_gw) != RTN_UNICAST)
1045 goto reject_redirect;
1048 for (i = 0; i < 2; i++) {
1049 for (k = 0; k < 2; k++) {
1050 unsigned hash = rt_hash(daddr, skeys[i], ikeys[k]);
1052 rthp=&rt_hash_table[hash].chain;
1055 while ((rth = rcu_dereference(*rthp)) != NULL) {
1058 if (rth->fl.fl4_dst != daddr ||
1059 rth->fl.fl4_src != skeys[i] ||
1060 rth->fl.oif != ikeys[k] ||
1062 rthp = &rth->u.dst.rt_next;
1066 if (rth->rt_dst != daddr ||
1067 rth->rt_src != saddr ||
1069 rth->rt_gateway != old_gw ||
1070 rth->u.dst.dev != dev)
1073 dst_hold(&rth->u.dst);
1076 rt = dst_alloc(&ipv4_dst_ops);
1083 /* Copy all the information. */
1085 INIT_RCU_HEAD(&rt->u.dst.rcu_head);
1086 rt->u.dst.__use = 1;
1087 atomic_set(&rt->u.dst.__refcnt, 1);
1088 rt->u.dst.child = NULL;
1090 dev_hold(rt->u.dst.dev);
1092 in_dev_hold(rt->idev);
1093 rt->u.dst.obsolete = 0;
1094 rt->u.dst.lastuse = jiffies;
1095 rt->u.dst.path = &rt->u.dst;
1096 rt->u.dst.neighbour = NULL;
1097 rt->u.dst.hh = NULL;
1098 rt->u.dst.xfrm = NULL;
1100 rt->rt_flags |= RTCF_REDIRECTED;
1102 /* Gateway is different ... */
1103 rt->rt_gateway = new_gw;
1105 /* Redirect received -> path was valid */
1106 dst_confirm(&rth->u.dst);
1109 atomic_inc(&rt->peer->refcnt);
1111 if (arp_bind_neighbour(&rt->u.dst) ||
1112 !(rt->u.dst.neighbour->nud_state &
1114 if (rt->u.dst.neighbour)
1115 neigh_event_send(rt->u.dst.neighbour, NULL);
1121 netevent.old = &rth->u.dst;
1122 netevent.new = &rt->u.dst;
1123 call_netevent_notifiers(NETEVENT_REDIRECT,
1127 if (!rt_intern_hash(hash, rt, &rt))
1140 #ifdef CONFIG_IP_ROUTE_VERBOSE
1141 if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
1142 printk(KERN_INFO "Redirect from %u.%u.%u.%u on %s about "
1143 "%u.%u.%u.%u ignored.\n"
1144 " Advised path = %u.%u.%u.%u -> %u.%u.%u.%u\n",
1145 NIPQUAD(old_gw), dev->name, NIPQUAD(new_gw),
1146 NIPQUAD(saddr), NIPQUAD(daddr));
1151 static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
1153 struct rtable *rt = (struct rtable*)dst;
1154 struct dst_entry *ret = dst;
1157 if (dst->obsolete) {
1160 } else if ((rt->rt_flags & RTCF_REDIRECTED) ||
1161 rt->u.dst.expires) {
1162 unsigned hash = rt_hash(rt->fl.fl4_dst, rt->fl.fl4_src,
1164 #if RT_CACHE_DEBUG >= 1
1165 printk(KERN_DEBUG "ipv4_negative_advice: redirect to "
1166 "%u.%u.%u.%u/%02x dropped\n",
1167 NIPQUAD(rt->rt_dst), rt->fl.fl4_tos);
1178 * 1. The first ip_rt_redirect_number redirects are sent
1179 * with exponential backoff, then we stop sending them at all,
1180 * assuming that the host ignores our redirects.
1181 * 2. If we did not see packets requiring redirects
1182 * during ip_rt_redirect_silence, we assume that the host
1183 * forgot redirected route and start to send redirects again.
1185 * This algorithm is much cheaper and more intelligent than dumb load limiting
1188 * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
1189 * and "frag. need" (breaks PMTU discovery) in icmp.c.
1192 void ip_rt_send_redirect(struct sk_buff *skb)
1194 struct rtable *rt = (struct rtable*)skb->dst;
1195 struct in_device *in_dev = in_dev_get(rt->u.dst.dev);
1200 if (!IN_DEV_TX_REDIRECTS(in_dev))
1203 /* No redirected packets during ip_rt_redirect_silence;
1204 * reset the algorithm.
1206 if (time_after(jiffies, rt->u.dst.rate_last + ip_rt_redirect_silence))
1207 rt->u.dst.rate_tokens = 0;
1209 /* Too many ignored redirects; do not send anything
1210 * set u.dst.rate_last to the last seen redirected packet.
1212 if (rt->u.dst.rate_tokens >= ip_rt_redirect_number) {
1213 rt->u.dst.rate_last = jiffies;
1217 /* Check for load limit; set rate_last to the latest sent
1220 if (rt->u.dst.rate_tokens == 0 ||
1222 (rt->u.dst.rate_last +
1223 (ip_rt_redirect_load << rt->u.dst.rate_tokens)))) {
1224 icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway);
1225 rt->u.dst.rate_last = jiffies;
1226 ++rt->u.dst.rate_tokens;
1227 #ifdef CONFIG_IP_ROUTE_VERBOSE
1228 if (IN_DEV_LOG_MARTIANS(in_dev) &&
1229 rt->u.dst.rate_tokens == ip_rt_redirect_number &&
1231 printk(KERN_WARNING "host %u.%u.%u.%u/if%d ignores "
1232 "redirects for %u.%u.%u.%u to %u.%u.%u.%u.\n",
1233 NIPQUAD(rt->rt_src), rt->rt_iif,
1234 NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_gateway));
1241 static int ip_error(struct sk_buff *skb)
1243 struct rtable *rt = (struct rtable*)skb->dst;
1247 switch (rt->u.dst.error) {
1252 code = ICMP_HOST_UNREACH;
1255 code = ICMP_NET_UNREACH;
1256 IP_INC_STATS_BH(IPSTATS_MIB_INNOROUTES);
1259 code = ICMP_PKT_FILTERED;
1264 rt->u.dst.rate_tokens += now - rt->u.dst.rate_last;
1265 if (rt->u.dst.rate_tokens > ip_rt_error_burst)
1266 rt->u.dst.rate_tokens = ip_rt_error_burst;
1267 rt->u.dst.rate_last = now;
1268 if (rt->u.dst.rate_tokens >= ip_rt_error_cost) {
1269 rt->u.dst.rate_tokens -= ip_rt_error_cost;
1270 icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
1273 out: kfree_skb(skb);
1278 * The last two values are not from the RFC but
1279 * are needed for AMPRnet AX.25 paths.
1282 static const unsigned short mtu_plateau[] =
1283 {32000, 17914, 8166, 4352, 2002, 1492, 576, 296, 216, 128 };
1285 static __inline__ unsigned short guess_mtu(unsigned short old_mtu)
1289 for (i = 0; i < ARRAY_SIZE(mtu_plateau); i++)
1290 if (old_mtu > mtu_plateau[i])
1291 return mtu_plateau[i];
1295 unsigned short ip_rt_frag_needed(struct iphdr *iph, unsigned short new_mtu)
1298 unsigned short old_mtu = ntohs(iph->tot_len);
1300 __be32 skeys[2] = { iph->saddr, 0, };
1301 __be32 daddr = iph->daddr;
1302 unsigned short est_mtu = 0;
1304 if (ipv4_config.no_pmtu_disc)
1307 for (i = 0; i < 2; i++) {
1308 unsigned hash = rt_hash(daddr, skeys[i], 0);
1311 for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
1312 rth = rcu_dereference(rth->u.dst.rt_next)) {
1313 if (rth->fl.fl4_dst == daddr &&
1314 rth->fl.fl4_src == skeys[i] &&
1315 rth->rt_dst == daddr &&
1316 rth->rt_src == iph->saddr &&
1318 !(dst_metric_locked(&rth->u.dst, RTAX_MTU))) {
1319 unsigned short mtu = new_mtu;
1321 if (new_mtu < 68 || new_mtu >= old_mtu) {
1323 /* BSD 4.2 compatibility hack :-( */
1325 old_mtu >= rth->u.dst.metrics[RTAX_MTU-1] &&
1326 old_mtu >= 68 + (iph->ihl << 2))
1327 old_mtu -= iph->ihl << 2;
1329 mtu = guess_mtu(old_mtu);
1331 if (mtu <= rth->u.dst.metrics[RTAX_MTU-1]) {
1332 if (mtu < rth->u.dst.metrics[RTAX_MTU-1]) {
1333 dst_confirm(&rth->u.dst);
1334 if (mtu < ip_rt_min_pmtu) {
1335 mtu = ip_rt_min_pmtu;
1336 rth->u.dst.metrics[RTAX_LOCK-1] |=
1339 rth->u.dst.metrics[RTAX_MTU-1] = mtu;
1340 dst_set_expires(&rth->u.dst,
1349 return est_mtu ? : new_mtu;
1352 static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
1354 if (dst->metrics[RTAX_MTU-1] > mtu && mtu >= 68 &&
1355 !(dst_metric_locked(dst, RTAX_MTU))) {
1356 if (mtu < ip_rt_min_pmtu) {
1357 mtu = ip_rt_min_pmtu;
1358 dst->metrics[RTAX_LOCK-1] |= (1 << RTAX_MTU);
1360 dst->metrics[RTAX_MTU-1] = mtu;
1361 dst_set_expires(dst, ip_rt_mtu_expires);
1362 call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
1366 static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie)
1371 static void ipv4_dst_destroy(struct dst_entry *dst)
1373 struct rtable *rt = (struct rtable *) dst;
1374 struct inet_peer *peer = rt->peer;
1375 struct in_device *idev = rt->idev;
1388 static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
1391 struct rtable *rt = (struct rtable *) dst;
1392 struct in_device *idev = rt->idev;
1393 if (dev != init_net.loopback_dev && idev && idev->dev == dev) {
1394 struct in_device *loopback_idev = in_dev_get(init_net.loopback_dev);
1395 if (loopback_idev) {
1396 rt->idev = loopback_idev;
1402 static void ipv4_link_failure(struct sk_buff *skb)
1406 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
1408 rt = (struct rtable *) skb->dst;
1410 dst_set_expires(&rt->u.dst, 0);
1413 static int ip_rt_bug(struct sk_buff *skb)
1415 printk(KERN_DEBUG "ip_rt_bug: %u.%u.%u.%u -> %u.%u.%u.%u, %s\n",
1416 NIPQUAD(ip_hdr(skb)->saddr), NIPQUAD(ip_hdr(skb)->daddr),
1417 skb->dev ? skb->dev->name : "?");
1423 We do not cache source address of outgoing interface,
1424 because it is used only by IP RR, TS and SRR options,
1425 so that it out of fast path.
1427 BTW remember: "addr" is allowed to be not aligned
1431 void ip_rt_get_source(u8 *addr, struct rtable *rt)
1434 struct fib_result res;
1436 if (rt->fl.iif == 0)
1438 else if (fib_lookup(&rt->fl, &res) == 0) {
1439 src = FIB_RES_PREFSRC(res);
1442 src = inet_select_addr(rt->u.dst.dev, rt->rt_gateway,
1444 memcpy(addr, &src, 4);
1447 #ifdef CONFIG_NET_CLS_ROUTE
1448 static void set_class_tag(struct rtable *rt, u32 tag)
1450 if (!(rt->u.dst.tclassid & 0xFFFF))
1451 rt->u.dst.tclassid |= tag & 0xFFFF;
1452 if (!(rt->u.dst.tclassid & 0xFFFF0000))
1453 rt->u.dst.tclassid |= tag & 0xFFFF0000;
1457 static void rt_set_nexthop(struct rtable *rt, struct fib_result *res, u32 itag)
1459 struct fib_info *fi = res->fi;
1462 if (FIB_RES_GW(*res) &&
1463 FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
1464 rt->rt_gateway = FIB_RES_GW(*res);
1465 memcpy(rt->u.dst.metrics, fi->fib_metrics,
1466 sizeof(rt->u.dst.metrics));
1467 if (fi->fib_mtu == 0) {
1468 rt->u.dst.metrics[RTAX_MTU-1] = rt->u.dst.dev->mtu;
1469 if (rt->u.dst.metrics[RTAX_LOCK-1] & (1 << RTAX_MTU) &&
1470 rt->rt_gateway != rt->rt_dst &&
1471 rt->u.dst.dev->mtu > 576)
1472 rt->u.dst.metrics[RTAX_MTU-1] = 576;
1474 #ifdef CONFIG_NET_CLS_ROUTE
1475 rt->u.dst.tclassid = FIB_RES_NH(*res).nh_tclassid;
1478 rt->u.dst.metrics[RTAX_MTU-1]= rt->u.dst.dev->mtu;
1480 if (rt->u.dst.metrics[RTAX_HOPLIMIT-1] == 0)
1481 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = sysctl_ip_default_ttl;
1482 if (rt->u.dst.metrics[RTAX_MTU-1] > IP_MAX_MTU)
1483 rt->u.dst.metrics[RTAX_MTU-1] = IP_MAX_MTU;
1484 if (rt->u.dst.metrics[RTAX_ADVMSS-1] == 0)
1485 rt->u.dst.metrics[RTAX_ADVMSS-1] = max_t(unsigned int, rt->u.dst.dev->mtu - 40,
1487 if (rt->u.dst.metrics[RTAX_ADVMSS-1] > 65535 - 40)
1488 rt->u.dst.metrics[RTAX_ADVMSS-1] = 65535 - 40;
1490 #ifdef CONFIG_NET_CLS_ROUTE
1491 #ifdef CONFIG_IP_MULTIPLE_TABLES
1492 set_class_tag(rt, fib_rules_tclass(res));
1494 set_class_tag(rt, itag);
1496 rt->rt_type = res->type;
1499 static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
1500 u8 tos, struct net_device *dev, int our)
1505 struct in_device *in_dev = in_dev_get(dev);
1508 /* Primary sanity checks. */
1513 if (MULTICAST(saddr) || BADCLASS(saddr) || LOOPBACK(saddr) ||
1514 skb->protocol != htons(ETH_P_IP))
1517 if (ZERONET(saddr)) {
1518 if (!LOCAL_MCAST(daddr))
1520 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
1521 } else if (fib_validate_source(saddr, 0, tos, 0,
1522 dev, &spec_dst, &itag) < 0)
1525 rth = dst_alloc(&ipv4_dst_ops);
1529 rth->u.dst.output= ip_rt_bug;
1531 atomic_set(&rth->u.dst.__refcnt, 1);
1532 rth->u.dst.flags= DST_HOST;
1533 if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
1534 rth->u.dst.flags |= DST_NOPOLICY;
1535 rth->fl.fl4_dst = daddr;
1536 rth->rt_dst = daddr;
1537 rth->fl.fl4_tos = tos;
1538 rth->fl.mark = skb->mark;
1539 rth->fl.fl4_src = saddr;
1540 rth->rt_src = saddr;
1541 #ifdef CONFIG_NET_CLS_ROUTE
1542 rth->u.dst.tclassid = itag;
1545 rth->fl.iif = dev->ifindex;
1546 rth->u.dst.dev = init_net.loopback_dev;
1547 dev_hold(rth->u.dst.dev);
1548 rth->idev = in_dev_get(rth->u.dst.dev);
1550 rth->rt_gateway = daddr;
1551 rth->rt_spec_dst= spec_dst;
1552 rth->rt_type = RTN_MULTICAST;
1553 rth->rt_flags = RTCF_MULTICAST;
1555 rth->u.dst.input= ip_local_deliver;
1556 rth->rt_flags |= RTCF_LOCAL;
1559 #ifdef CONFIG_IP_MROUTE
1560 if (!LOCAL_MCAST(daddr) && IN_DEV_MFORWARD(in_dev))
1561 rth->u.dst.input = ip_mr_input;
1563 RT_CACHE_STAT_INC(in_slow_mc);
1566 hash = rt_hash(daddr, saddr, dev->ifindex);
1567 return rt_intern_hash(hash, rth, (struct rtable**) &skb->dst);
1579 static void ip_handle_martian_source(struct net_device *dev,
1580 struct in_device *in_dev,
1581 struct sk_buff *skb,
1585 RT_CACHE_STAT_INC(in_martian_src);
1586 #ifdef CONFIG_IP_ROUTE_VERBOSE
1587 if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
1589 * RFC1812 recommendation, if source is martian,
1590 * the only hint is MAC header.
1592 printk(KERN_WARNING "martian source %u.%u.%u.%u from "
1593 "%u.%u.%u.%u, on dev %s\n",
1594 NIPQUAD(daddr), NIPQUAD(saddr), dev->name);
1595 if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
1597 const unsigned char *p = skb_mac_header(skb);
1598 printk(KERN_WARNING "ll header: ");
1599 for (i = 0; i < dev->hard_header_len; i++, p++) {
1601 if (i < (dev->hard_header_len - 1))
1610 static inline int __mkroute_input(struct sk_buff *skb,
1611 struct fib_result* res,
1612 struct in_device *in_dev,
1613 __be32 daddr, __be32 saddr, u32 tos,
1614 struct rtable **result)
1619 struct in_device *out_dev;
1624 /* get a working reference to the output device */
1625 out_dev = in_dev_get(FIB_RES_DEV(*res));
1626 if (out_dev == NULL) {
1627 if (net_ratelimit())
1628 printk(KERN_CRIT "Bug in ip_route_input" \
1629 "_slow(). Please, report\n");
1634 err = fib_validate_source(saddr, daddr, tos, FIB_RES_OIF(*res),
1635 in_dev->dev, &spec_dst, &itag);
1637 ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
1645 flags |= RTCF_DIRECTSRC;
1647 if (out_dev == in_dev && err && !(flags & (RTCF_NAT | RTCF_MASQ)) &&
1648 (IN_DEV_SHARED_MEDIA(out_dev) ||
1649 inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
1650 flags |= RTCF_DOREDIRECT;
1652 if (skb->protocol != htons(ETH_P_IP)) {
1653 /* Not IP (i.e. ARP). Do not create route, if it is
1654 * invalid for proxy arp. DNAT routes are always valid.
1656 if (out_dev == in_dev && !(flags & RTCF_DNAT)) {
1663 rth = dst_alloc(&ipv4_dst_ops);
1669 atomic_set(&rth->u.dst.__refcnt, 1);
1670 rth->u.dst.flags= DST_HOST;
1671 if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
1672 rth->u.dst.flags |= DST_NOPOLICY;
1673 if (IN_DEV_CONF_GET(out_dev, NOXFRM))
1674 rth->u.dst.flags |= DST_NOXFRM;
1675 rth->fl.fl4_dst = daddr;
1676 rth->rt_dst = daddr;
1677 rth->fl.fl4_tos = tos;
1678 rth->fl.mark = skb->mark;
1679 rth->fl.fl4_src = saddr;
1680 rth->rt_src = saddr;
1681 rth->rt_gateway = daddr;
1683 rth->fl.iif = in_dev->dev->ifindex;
1684 rth->u.dst.dev = (out_dev)->dev;
1685 dev_hold(rth->u.dst.dev);
1686 rth->idev = in_dev_get(rth->u.dst.dev);
1688 rth->rt_spec_dst= spec_dst;
1690 rth->u.dst.input = ip_forward;
1691 rth->u.dst.output = ip_output;
1693 rt_set_nexthop(rth, res, itag);
1695 rth->rt_flags = flags;
1700 /* release the working reference to the output device */
1701 in_dev_put(out_dev);
1705 static inline int ip_mkroute_input(struct sk_buff *skb,
1706 struct fib_result* res,
1707 const struct flowi *fl,
1708 struct in_device *in_dev,
1709 __be32 daddr, __be32 saddr, u32 tos)
1711 struct rtable* rth = NULL;
1715 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1716 if (res->fi && res->fi->fib_nhs > 1 && fl->oif == 0)
1717 fib_select_multipath(fl, res);
1720 /* create a routing cache entry */
1721 err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth);
1725 /* put it into the cache */
1726 hash = rt_hash(daddr, saddr, fl->iif);
1727 return rt_intern_hash(hash, rth, (struct rtable**)&skb->dst);
1731 * NOTE. We drop all the packets that has local source
1732 * addresses, because every properly looped back packet
1733 * must have correct destination already attached by output routine.
1735 * Such approach solves two big problems:
1736 * 1. Not simplex devices are handled properly.
1737 * 2. IP spoofing attempts are filtered with 100% of guarantee.
1740 static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
1741 u8 tos, struct net_device *dev)
1743 struct fib_result res;
1744 struct in_device *in_dev = in_dev_get(dev);
1745 struct flowi fl = { .nl_u = { .ip4_u =
1749 .scope = RT_SCOPE_UNIVERSE,
1752 .iif = dev->ifindex };
1755 struct rtable * rth;
1761 /* IP on this device is disabled. */
1766 /* Check for the most weird martians, which can be not detected
1770 if (MULTICAST(saddr) || BADCLASS(saddr) || LOOPBACK(saddr))
1771 goto martian_source;
1773 if (daddr == htonl(0xFFFFFFFF) || (saddr == 0 && daddr == 0))
1776 /* Accept zero addresses only to limited broadcast;
1777 * I even do not know to fix it or not. Waiting for complains :-)
1780 goto martian_source;
1782 if (BADCLASS(daddr) || ZERONET(daddr) || LOOPBACK(daddr))
1783 goto martian_destination;
1786 * Now we are ready to route packet.
1788 if ((err = fib_lookup(&fl, &res)) != 0) {
1789 if (!IN_DEV_FORWARD(in_dev))
1795 RT_CACHE_STAT_INC(in_slow_tot);
1797 if (res.type == RTN_BROADCAST)
1800 if (res.type == RTN_LOCAL) {
1802 result = fib_validate_source(saddr, daddr, tos,
1803 init_net.loopback_dev->ifindex,
1804 dev, &spec_dst, &itag);
1806 goto martian_source;
1808 flags |= RTCF_DIRECTSRC;
1813 if (!IN_DEV_FORWARD(in_dev))
1815 if (res.type != RTN_UNICAST)
1816 goto martian_destination;
1818 err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos);
1826 if (skb->protocol != htons(ETH_P_IP))
1830 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
1832 err = fib_validate_source(saddr, 0, tos, 0, dev, &spec_dst,
1835 goto martian_source;
1837 flags |= RTCF_DIRECTSRC;
1839 flags |= RTCF_BROADCAST;
1840 res.type = RTN_BROADCAST;
1841 RT_CACHE_STAT_INC(in_brd);
1844 rth = dst_alloc(&ipv4_dst_ops);
1848 rth->u.dst.output= ip_rt_bug;
1850 atomic_set(&rth->u.dst.__refcnt, 1);
1851 rth->u.dst.flags= DST_HOST;
1852 if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
1853 rth->u.dst.flags |= DST_NOPOLICY;
1854 rth->fl.fl4_dst = daddr;
1855 rth->rt_dst = daddr;
1856 rth->fl.fl4_tos = tos;
1857 rth->fl.mark = skb->mark;
1858 rth->fl.fl4_src = saddr;
1859 rth->rt_src = saddr;
1860 #ifdef CONFIG_NET_CLS_ROUTE
1861 rth->u.dst.tclassid = itag;
1864 rth->fl.iif = dev->ifindex;
1865 rth->u.dst.dev = init_net.loopback_dev;
1866 dev_hold(rth->u.dst.dev);
1867 rth->idev = in_dev_get(rth->u.dst.dev);
1868 rth->rt_gateway = daddr;
1869 rth->rt_spec_dst= spec_dst;
1870 rth->u.dst.input= ip_local_deliver;
1871 rth->rt_flags = flags|RTCF_LOCAL;
1872 if (res.type == RTN_UNREACHABLE) {
1873 rth->u.dst.input= ip_error;
1874 rth->u.dst.error= -err;
1875 rth->rt_flags &= ~RTCF_LOCAL;
1877 rth->rt_type = res.type;
1878 hash = rt_hash(daddr, saddr, fl.iif);
1879 err = rt_intern_hash(hash, rth, (struct rtable**)&skb->dst);
1883 RT_CACHE_STAT_INC(in_no_route);
1884 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
1885 res.type = RTN_UNREACHABLE;
1891 * Do not cache martian addresses: they should be logged (RFC1812)
1893 martian_destination:
1894 RT_CACHE_STAT_INC(in_martian_dst);
1895 #ifdef CONFIG_IP_ROUTE_VERBOSE
1896 if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
1897 printk(KERN_WARNING "martian destination %u.%u.%u.%u from "
1898 "%u.%u.%u.%u, dev %s\n",
1899 NIPQUAD(daddr), NIPQUAD(saddr), dev->name);
1903 err = -EHOSTUNREACH;
1915 ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
1919 int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr,
1920 u8 tos, struct net_device *dev)
1922 struct rtable * rth;
1924 int iif = dev->ifindex;
1926 tos &= IPTOS_RT_MASK;
1927 hash = rt_hash(daddr, saddr, iif);
1930 for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
1931 rth = rcu_dereference(rth->u.dst.rt_next)) {
1932 if (rth->fl.fl4_dst == daddr &&
1933 rth->fl.fl4_src == saddr &&
1934 rth->fl.iif == iif &&
1936 rth->fl.mark == skb->mark &&
1937 rth->fl.fl4_tos == tos) {
1938 dst_use(&rth->u.dst, jiffies);
1939 RT_CACHE_STAT_INC(in_hit);
1941 skb->dst = (struct dst_entry*)rth;
1944 RT_CACHE_STAT_INC(in_hlist_search);
1948 /* Multicast recognition logic is moved from route cache to here.
1949 The problem was that too many Ethernet cards have broken/missing
1950 hardware multicast filters :-( As result the host on multicasting
1951 network acquires a lot of useless route cache entries, sort of
1952 SDR messages from all the world. Now we try to get rid of them.
1953 Really, provided software IP multicast filter is organized
1954 reasonably (at least, hashed), it does not result in a slowdown
1955 comparing with route cache reject entries.
1956 Note, that multicast routers are not affected, because
1957 route cache entry is created eventually.
1959 if (MULTICAST(daddr)) {
1960 struct in_device *in_dev;
1963 if ((in_dev = __in_dev_get_rcu(dev)) != NULL) {
1964 int our = ip_check_mc(in_dev, daddr, saddr,
1965 ip_hdr(skb)->protocol);
1967 #ifdef CONFIG_IP_MROUTE
1968 || (!LOCAL_MCAST(daddr) && IN_DEV_MFORWARD(in_dev))
1972 return ip_route_input_mc(skb, daddr, saddr,
1979 return ip_route_input_slow(skb, daddr, saddr, tos, dev);
1982 static inline int __mkroute_output(struct rtable **result,
1983 struct fib_result* res,
1984 const struct flowi *fl,
1985 const struct flowi *oldflp,
1986 struct net_device *dev_out,
1990 struct in_device *in_dev;
1991 u32 tos = RT_FL_TOS(oldflp);
1994 if (LOOPBACK(fl->fl4_src) && !(dev_out->flags&IFF_LOOPBACK))
1997 if (fl->fl4_dst == htonl(0xFFFFFFFF))
1998 res->type = RTN_BROADCAST;
1999 else if (MULTICAST(fl->fl4_dst))
2000 res->type = RTN_MULTICAST;
2001 else if (BADCLASS(fl->fl4_dst) || ZERONET(fl->fl4_dst))
2004 if (dev_out->flags & IFF_LOOPBACK)
2005 flags |= RTCF_LOCAL;
2007 /* get work reference to inet device */
2008 in_dev = in_dev_get(dev_out);
2012 if (res->type == RTN_BROADCAST) {
2013 flags |= RTCF_BROADCAST | RTCF_LOCAL;
2015 fib_info_put(res->fi);
2018 } else if (res->type == RTN_MULTICAST) {
2019 flags |= RTCF_MULTICAST|RTCF_LOCAL;
2020 if (!ip_check_mc(in_dev, oldflp->fl4_dst, oldflp->fl4_src,
2022 flags &= ~RTCF_LOCAL;
2023 /* If multicast route do not exist use
2024 default one, but do not gateway in this case.
2027 if (res->fi && res->prefixlen < 4) {
2028 fib_info_put(res->fi);
2034 rth = dst_alloc(&ipv4_dst_ops);
2040 atomic_set(&rth->u.dst.__refcnt, 1);
2041 rth->u.dst.flags= DST_HOST;
2042 if (IN_DEV_CONF_GET(in_dev, NOXFRM))
2043 rth->u.dst.flags |= DST_NOXFRM;
2044 if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
2045 rth->u.dst.flags |= DST_NOPOLICY;
2047 rth->fl.fl4_dst = oldflp->fl4_dst;
2048 rth->fl.fl4_tos = tos;
2049 rth->fl.fl4_src = oldflp->fl4_src;
2050 rth->fl.oif = oldflp->oif;
2051 rth->fl.mark = oldflp->mark;
2052 rth->rt_dst = fl->fl4_dst;
2053 rth->rt_src = fl->fl4_src;
2054 rth->rt_iif = oldflp->oif ? : dev_out->ifindex;
2055 /* get references to the devices that are to be hold by the routing
2057 rth->u.dst.dev = dev_out;
2059 rth->idev = in_dev_get(dev_out);
2060 rth->rt_gateway = fl->fl4_dst;
2061 rth->rt_spec_dst= fl->fl4_src;
2063 rth->u.dst.output=ip_output;
2065 RT_CACHE_STAT_INC(out_slow_tot);
2067 if (flags & RTCF_LOCAL) {
2068 rth->u.dst.input = ip_local_deliver;
2069 rth->rt_spec_dst = fl->fl4_dst;
2071 if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
2072 rth->rt_spec_dst = fl->fl4_src;
2073 if (flags & RTCF_LOCAL &&
2074 !(dev_out->flags & IFF_LOOPBACK)) {
2075 rth->u.dst.output = ip_mc_output;
2076 RT_CACHE_STAT_INC(out_slow_mc);
2078 #ifdef CONFIG_IP_MROUTE
2079 if (res->type == RTN_MULTICAST) {
2080 if (IN_DEV_MFORWARD(in_dev) &&
2081 !LOCAL_MCAST(oldflp->fl4_dst)) {
2082 rth->u.dst.input = ip_mr_input;
2083 rth->u.dst.output = ip_mc_output;
2089 rt_set_nexthop(rth, res, 0);
2091 rth->rt_flags = flags;
2095 /* release work reference to inet device */
2101 static inline int ip_mkroute_output(struct rtable **rp,
2102 struct fib_result* res,
2103 const struct flowi *fl,
2104 const struct flowi *oldflp,
2105 struct net_device *dev_out,
2108 struct rtable *rth = NULL;
2109 int err = __mkroute_output(&rth, res, fl, oldflp, dev_out, flags);
2112 hash = rt_hash(oldflp->fl4_dst, oldflp->fl4_src, oldflp->oif);
2113 err = rt_intern_hash(hash, rth, rp);
2120 * Major route resolver routine.
2123 static int ip_route_output_slow(struct rtable **rp, const struct flowi *oldflp)
2125 u32 tos = RT_FL_TOS(oldflp);
2126 struct flowi fl = { .nl_u = { .ip4_u =
2127 { .daddr = oldflp->fl4_dst,
2128 .saddr = oldflp->fl4_src,
2129 .tos = tos & IPTOS_RT_MASK,
2130 .scope = ((tos & RTO_ONLINK) ?
2134 .mark = oldflp->mark,
2135 .iif = init_net.loopback_dev->ifindex,
2136 .oif = oldflp->oif };
2137 struct fib_result res;
2139 struct net_device *dev_out = NULL;
2145 #ifdef CONFIG_IP_MULTIPLE_TABLES
2149 if (oldflp->fl4_src) {
2151 if (MULTICAST(oldflp->fl4_src) ||
2152 BADCLASS(oldflp->fl4_src) ||
2153 ZERONET(oldflp->fl4_src))
2156 /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
2157 dev_out = ip_dev_find(oldflp->fl4_src);
2158 if (dev_out == NULL)
2161 /* I removed check for oif == dev_out->oif here.
2162 It was wrong for two reasons:
2163 1. ip_dev_find(saddr) can return wrong iface, if saddr is
2164 assigned to multiple interfaces.
2165 2. Moreover, we are allowed to send packets with saddr
2166 of another iface. --ANK
2169 if (oldflp->oif == 0
2170 && (MULTICAST(oldflp->fl4_dst) || oldflp->fl4_dst == htonl(0xFFFFFFFF))) {
2171 /* Special hack: user can direct multicasts
2172 and limited broadcast via necessary interface
2173 without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
2174 This hack is not just for fun, it allows
2175 vic,vat and friends to work.
2176 They bind socket to loopback, set ttl to zero
2177 and expect that it will work.
2178 From the viewpoint of routing cache they are broken,
2179 because we are not allowed to build multicast path
2180 with loopback source addr (look, routing cache
2181 cannot know, that ttl is zero, so that packet
2182 will not leave this host and route is valid).
2183 Luckily, this hack is good workaround.
2186 fl.oif = dev_out->ifindex;
2196 dev_out = dev_get_by_index(&init_net, oldflp->oif);
2198 if (dev_out == NULL)
2201 /* RACE: Check return value of inet_select_addr instead. */
2202 if (__in_dev_get_rtnl(dev_out) == NULL) {
2204 goto out; /* Wrong error code */
2207 if (LOCAL_MCAST(oldflp->fl4_dst) || oldflp->fl4_dst == htonl(0xFFFFFFFF)) {
2209 fl.fl4_src = inet_select_addr(dev_out, 0,
2214 if (MULTICAST(oldflp->fl4_dst))
2215 fl.fl4_src = inet_select_addr(dev_out, 0,
2217 else if (!oldflp->fl4_dst)
2218 fl.fl4_src = inet_select_addr(dev_out, 0,
2224 fl.fl4_dst = fl.fl4_src;
2226 fl.fl4_dst = fl.fl4_src = htonl(INADDR_LOOPBACK);
2229 dev_out = init_net.loopback_dev;
2231 fl.oif = init_net.loopback_dev->ifindex;
2232 res.type = RTN_LOCAL;
2233 flags |= RTCF_LOCAL;
2237 if (fib_lookup(&fl, &res)) {
2240 /* Apparently, routing tables are wrong. Assume,
2241 that the destination is on link.
2244 Because we are allowed to send to iface
2245 even if it has NO routes and NO assigned
2246 addresses. When oif is specified, routing
2247 tables are looked up with only one purpose:
2248 to catch if destination is gatewayed, rather than
2249 direct. Moreover, if MSG_DONTROUTE is set,
2250 we send packet, ignoring both routing tables
2251 and ifaddr state. --ANK
2254 We could make it even if oif is unknown,
2255 likely IPv6, but we do not.
2258 if (fl.fl4_src == 0)
2259 fl.fl4_src = inet_select_addr(dev_out, 0,
2261 res.type = RTN_UNICAST;
2271 if (res.type == RTN_LOCAL) {
2273 fl.fl4_src = fl.fl4_dst;
2276 dev_out = init_net.loopback_dev;
2278 fl.oif = dev_out->ifindex;
2280 fib_info_put(res.fi);
2282 flags |= RTCF_LOCAL;
2286 #ifdef CONFIG_IP_ROUTE_MULTIPATH
2287 if (res.fi->fib_nhs > 1 && fl.oif == 0)
2288 fib_select_multipath(&fl, &res);
2291 if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
2292 fib_select_default(&fl, &res);
2295 fl.fl4_src = FIB_RES_PREFSRC(res);
2299 dev_out = FIB_RES_DEV(res);
2301 fl.oif = dev_out->ifindex;
2305 err = ip_mkroute_output(rp, &res, &fl, oldflp, dev_out, flags);
2315 int __ip_route_output_key(struct rtable **rp, const struct flowi *flp)
2320 hash = rt_hash(flp->fl4_dst, flp->fl4_src, flp->oif);
2323 for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
2324 rth = rcu_dereference(rth->u.dst.rt_next)) {
2325 if (rth->fl.fl4_dst == flp->fl4_dst &&
2326 rth->fl.fl4_src == flp->fl4_src &&
2328 rth->fl.oif == flp->oif &&
2329 rth->fl.mark == flp->mark &&
2330 !((rth->fl.fl4_tos ^ flp->fl4_tos) &
2331 (IPTOS_RT_MASK | RTO_ONLINK))) {
2332 dst_use(&rth->u.dst, jiffies);
2333 RT_CACHE_STAT_INC(out_hit);
2334 rcu_read_unlock_bh();
2338 RT_CACHE_STAT_INC(out_hlist_search);
2340 rcu_read_unlock_bh();
2342 return ip_route_output_slow(rp, flp);
2345 EXPORT_SYMBOL_GPL(__ip_route_output_key);
2347 static void ipv4_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
2351 static struct dst_ops ipv4_dst_blackhole_ops = {
2353 .protocol = __constant_htons(ETH_P_IP),
2354 .destroy = ipv4_dst_destroy,
2355 .check = ipv4_dst_check,
2356 .update_pmtu = ipv4_rt_blackhole_update_pmtu,
2357 .entry_size = sizeof(struct rtable),
2361 static int ipv4_dst_blackhole(struct rtable **rp, struct flowi *flp, struct sock *sk)
2363 struct rtable *ort = *rp;
2364 struct rtable *rt = (struct rtable *)
2365 dst_alloc(&ipv4_dst_blackhole_ops);
2368 struct dst_entry *new = &rt->u.dst;
2370 atomic_set(&new->__refcnt, 1);
2372 new->input = dst_discard;
2373 new->output = dst_discard;
2374 memcpy(new->metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
2376 new->dev = ort->u.dst.dev;
2382 rt->idev = ort->idev;
2384 in_dev_hold(rt->idev);
2385 rt->rt_flags = ort->rt_flags;
2386 rt->rt_type = ort->rt_type;
2387 rt->rt_dst = ort->rt_dst;
2388 rt->rt_src = ort->rt_src;
2389 rt->rt_iif = ort->rt_iif;
2390 rt->rt_gateway = ort->rt_gateway;
2391 rt->rt_spec_dst = ort->rt_spec_dst;
2392 rt->peer = ort->peer;
2394 atomic_inc(&rt->peer->refcnt);
2399 dst_release(&(*rp)->u.dst);
2401 return (rt ? 0 : -ENOMEM);
2404 int ip_route_output_flow(struct rtable **rp, struct flowi *flp, struct sock *sk, int flags)
2408 if ((err = __ip_route_output_key(rp, flp)) != 0)
2413 flp->fl4_src = (*rp)->rt_src;
2415 flp->fl4_dst = (*rp)->rt_dst;
2416 err = __xfrm_lookup((struct dst_entry **)rp, flp, sk, flags);
2417 if (err == -EREMOTE)
2418 err = ipv4_dst_blackhole(rp, flp, sk);
2426 EXPORT_SYMBOL_GPL(ip_route_output_flow);
2428 int ip_route_output_key(struct rtable **rp, struct flowi *flp)
2430 return ip_route_output_flow(rp, flp, NULL, 0);
2433 static int rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
2434 int nowait, unsigned int flags)
2436 struct rtable *rt = (struct rtable*)skb->dst;
2438 struct nlmsghdr *nlh;
2440 u32 id = 0, ts = 0, tsage = 0, error;
2442 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags);
2446 r = nlmsg_data(nlh);
2447 r->rtm_family = AF_INET;
2448 r->rtm_dst_len = 32;
2450 r->rtm_tos = rt->fl.fl4_tos;
2451 r->rtm_table = RT_TABLE_MAIN;
2452 NLA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
2453 r->rtm_type = rt->rt_type;
2454 r->rtm_scope = RT_SCOPE_UNIVERSE;
2455 r->rtm_protocol = RTPROT_UNSPEC;
2456 r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
2457 if (rt->rt_flags & RTCF_NOTIFY)
2458 r->rtm_flags |= RTM_F_NOTIFY;
2460 NLA_PUT_BE32(skb, RTA_DST, rt->rt_dst);
2462 if (rt->fl.fl4_src) {
2463 r->rtm_src_len = 32;
2464 NLA_PUT_BE32(skb, RTA_SRC, rt->fl.fl4_src);
2467 NLA_PUT_U32(skb, RTA_OIF, rt->u.dst.dev->ifindex);
2468 #ifdef CONFIG_NET_CLS_ROUTE
2469 if (rt->u.dst.tclassid)
2470 NLA_PUT_U32(skb, RTA_FLOW, rt->u.dst.tclassid);
2473 NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_spec_dst);
2474 else if (rt->rt_src != rt->fl.fl4_src)
2475 NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_src);
2477 if (rt->rt_dst != rt->rt_gateway)
2478 NLA_PUT_BE32(skb, RTA_GATEWAY, rt->rt_gateway);
2480 if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
2481 goto nla_put_failure;
2483 error = rt->u.dst.error;
2484 expires = rt->u.dst.expires ? rt->u.dst.expires - jiffies : 0;
2486 id = rt->peer->ip_id_count;
2487 if (rt->peer->tcp_ts_stamp) {
2488 ts = rt->peer->tcp_ts;
2489 tsage = get_seconds() - rt->peer->tcp_ts_stamp;
2494 #ifdef CONFIG_IP_MROUTE
2495 __be32 dst = rt->rt_dst;
2497 if (MULTICAST(dst) && !LOCAL_MCAST(dst) &&
2498 IPV4_DEVCONF_ALL(MC_FORWARDING)) {
2499 int err = ipmr_get_route(skb, r, nowait);
2504 goto nla_put_failure;
2506 if (err == -EMSGSIZE)
2507 goto nla_put_failure;
2513 NLA_PUT_U32(skb, RTA_IIF, rt->fl.iif);
2516 if (rtnl_put_cacheinfo(skb, &rt->u.dst, id, ts, tsage,
2517 expires, error) < 0)
2518 goto nla_put_failure;
2520 return nlmsg_end(skb, nlh);
2523 nlmsg_cancel(skb, nlh);
2527 static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
2530 struct nlattr *tb[RTA_MAX+1];
2531 struct rtable *rt = NULL;
2536 struct sk_buff *skb;
2538 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy);
2542 rtm = nlmsg_data(nlh);
2544 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2550 /* Reserve room for dummy headers, this skb can pass
2551 through good chunk of routing engine.
2553 skb_reset_mac_header(skb);
2554 skb_reset_network_header(skb);
2556 /* Bugfix: need to give ip_route_input enough of an IP header to not gag. */
2557 ip_hdr(skb)->protocol = IPPROTO_ICMP;
2558 skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr));
2560 src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0;
2561 dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0;
2562 iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
2565 struct net_device *dev;
2567 dev = __dev_get_by_index(&init_net, iif);
2573 skb->protocol = htons(ETH_P_IP);
2576 err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev);
2579 rt = (struct rtable*) skb->dst;
2580 if (err == 0 && rt->u.dst.error)
2581 err = -rt->u.dst.error;
2588 .tos = rtm->rtm_tos,
2591 .oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0,
2593 err = ip_route_output_key(&rt, &fl);
2599 skb->dst = &rt->u.dst;
2600 if (rtm->rtm_flags & RTM_F_NOTIFY)
2601 rt->rt_flags |= RTCF_NOTIFY;
2603 err = rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
2604 RTM_NEWROUTE, 0, 0);
2608 err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
2617 int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb)
2626 s_idx = idx = cb->args[1];
2627 for (h = s_h; h <= rt_hash_mask; h++) {
2629 for (rt = rcu_dereference(rt_hash_table[h].chain), idx = 0; rt;
2630 rt = rcu_dereference(rt->u.dst.rt_next), idx++) {
2633 skb->dst = dst_clone(&rt->u.dst);
2634 if (rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
2635 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
2636 1, NLM_F_MULTI) <= 0) {
2637 dst_release(xchg(&skb->dst, NULL));
2638 rcu_read_unlock_bh();
2641 dst_release(xchg(&skb->dst, NULL));
2643 rcu_read_unlock_bh();
2653 void ip_rt_multicast_event(struct in_device *in_dev)
2658 #ifdef CONFIG_SYSCTL
2659 static int flush_delay;
2661 static int ipv4_sysctl_rtcache_flush(ctl_table *ctl, int write,
2662 struct file *filp, void __user *buffer,
2663 size_t *lenp, loff_t *ppos)
2666 proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
2667 rt_cache_flush(flush_delay);
2674 static int ipv4_sysctl_rtcache_flush_strategy(ctl_table *table,
2677 void __user *oldval,
2678 size_t __user *oldlenp,
2679 void __user *newval,
2683 if (newlen != sizeof(int))
2685 if (get_user(delay, (int __user *)newval))
2687 rt_cache_flush(delay);
2691 ctl_table ipv4_route_table[] = {
2693 .ctl_name = NET_IPV4_ROUTE_FLUSH,
2694 .procname = "flush",
2695 .data = &flush_delay,
2696 .maxlen = sizeof(int),
2698 .proc_handler = &ipv4_sysctl_rtcache_flush,
2699 .strategy = &ipv4_sysctl_rtcache_flush_strategy,
2702 .ctl_name = NET_IPV4_ROUTE_MIN_DELAY,
2703 .procname = "min_delay",
2704 .data = &ip_rt_min_delay,
2705 .maxlen = sizeof(int),
2707 .proc_handler = &proc_dointvec_jiffies,
2708 .strategy = &sysctl_jiffies,
2711 .ctl_name = NET_IPV4_ROUTE_MAX_DELAY,
2712 .procname = "max_delay",
2713 .data = &ip_rt_max_delay,
2714 .maxlen = sizeof(int),
2716 .proc_handler = &proc_dointvec_jiffies,
2717 .strategy = &sysctl_jiffies,
2720 .ctl_name = NET_IPV4_ROUTE_GC_THRESH,
2721 .procname = "gc_thresh",
2722 .data = &ipv4_dst_ops.gc_thresh,
2723 .maxlen = sizeof(int),
2725 .proc_handler = &proc_dointvec,
2728 .ctl_name = NET_IPV4_ROUTE_MAX_SIZE,
2729 .procname = "max_size",
2730 .data = &ip_rt_max_size,
2731 .maxlen = sizeof(int),
2733 .proc_handler = &proc_dointvec,
2736 /* Deprecated. Use gc_min_interval_ms */
2738 .ctl_name = NET_IPV4_ROUTE_GC_MIN_INTERVAL,
2739 .procname = "gc_min_interval",
2740 .data = &ip_rt_gc_min_interval,
2741 .maxlen = sizeof(int),
2743 .proc_handler = &proc_dointvec_jiffies,
2744 .strategy = &sysctl_jiffies,
2747 .ctl_name = NET_IPV4_ROUTE_GC_MIN_INTERVAL_MS,
2748 .procname = "gc_min_interval_ms",
2749 .data = &ip_rt_gc_min_interval,
2750 .maxlen = sizeof(int),
2752 .proc_handler = &proc_dointvec_ms_jiffies,
2753 .strategy = &sysctl_ms_jiffies,
2756 .ctl_name = NET_IPV4_ROUTE_GC_TIMEOUT,
2757 .procname = "gc_timeout",
2758 .data = &ip_rt_gc_timeout,
2759 .maxlen = sizeof(int),
2761 .proc_handler = &proc_dointvec_jiffies,
2762 .strategy = &sysctl_jiffies,
2765 .ctl_name = NET_IPV4_ROUTE_GC_INTERVAL,
2766 .procname = "gc_interval",
2767 .data = &ip_rt_gc_interval,
2768 .maxlen = sizeof(int),
2770 .proc_handler = &proc_dointvec_jiffies,
2771 .strategy = &sysctl_jiffies,
2774 .ctl_name = NET_IPV4_ROUTE_REDIRECT_LOAD,
2775 .procname = "redirect_load",
2776 .data = &ip_rt_redirect_load,
2777 .maxlen = sizeof(int),
2779 .proc_handler = &proc_dointvec,
2782 .ctl_name = NET_IPV4_ROUTE_REDIRECT_NUMBER,
2783 .procname = "redirect_number",
2784 .data = &ip_rt_redirect_number,
2785 .maxlen = sizeof(int),
2787 .proc_handler = &proc_dointvec,
2790 .ctl_name = NET_IPV4_ROUTE_REDIRECT_SILENCE,
2791 .procname = "redirect_silence",
2792 .data = &ip_rt_redirect_silence,
2793 .maxlen = sizeof(int),
2795 .proc_handler = &proc_dointvec,
2798 .ctl_name = NET_IPV4_ROUTE_ERROR_COST,
2799 .procname = "error_cost",
2800 .data = &ip_rt_error_cost,
2801 .maxlen = sizeof(int),
2803 .proc_handler = &proc_dointvec,
2806 .ctl_name = NET_IPV4_ROUTE_ERROR_BURST,
2807 .procname = "error_burst",
2808 .data = &ip_rt_error_burst,
2809 .maxlen = sizeof(int),
2811 .proc_handler = &proc_dointvec,
2814 .ctl_name = NET_IPV4_ROUTE_GC_ELASTICITY,
2815 .procname = "gc_elasticity",
2816 .data = &ip_rt_gc_elasticity,
2817 .maxlen = sizeof(int),
2819 .proc_handler = &proc_dointvec,
2822 .ctl_name = NET_IPV4_ROUTE_MTU_EXPIRES,
2823 .procname = "mtu_expires",
2824 .data = &ip_rt_mtu_expires,
2825 .maxlen = sizeof(int),
2827 .proc_handler = &proc_dointvec_jiffies,
2828 .strategy = &sysctl_jiffies,
2831 .ctl_name = NET_IPV4_ROUTE_MIN_PMTU,
2832 .procname = "min_pmtu",
2833 .data = &ip_rt_min_pmtu,
2834 .maxlen = sizeof(int),
2836 .proc_handler = &proc_dointvec,
2839 .ctl_name = NET_IPV4_ROUTE_MIN_ADVMSS,
2840 .procname = "min_adv_mss",
2841 .data = &ip_rt_min_advmss,
2842 .maxlen = sizeof(int),
2844 .proc_handler = &proc_dointvec,
2847 .ctl_name = NET_IPV4_ROUTE_SECRET_INTERVAL,
2848 .procname = "secret_interval",
2849 .data = &ip_rt_secret_interval,
2850 .maxlen = sizeof(int),
2852 .proc_handler = &proc_dointvec_jiffies,
2853 .strategy = &sysctl_jiffies,
2859 #ifdef CONFIG_NET_CLS_ROUTE
2860 struct ip_rt_acct *ip_rt_acct;
2862 /* This code sucks. But you should have seen it before! --RR */
2864 /* IP route accounting ptr for this logical cpu number. */
2865 #define IP_RT_ACCT_CPU(i) (ip_rt_acct + i * 256)
2867 #ifdef CONFIG_PROC_FS
2868 static int ip_rt_acct_read(char *buffer, char **start, off_t offset,
2869 int length, int *eof, void *data)
2873 if ((offset & 3) || (length & 3))
2876 if (offset >= sizeof(struct ip_rt_acct) * 256) {
2881 if (offset + length >= sizeof(struct ip_rt_acct) * 256) {
2882 length = sizeof(struct ip_rt_acct) * 256 - offset;
2886 offset /= sizeof(u32);
2889 u32 *dst = (u32 *) buffer;
2892 memset(dst, 0, length);
2894 for_each_possible_cpu(i) {
2896 u32 *src = ((u32 *) IP_RT_ACCT_CPU(i)) + offset;
2898 for (j = 0; j < length/4; j++)
2904 #endif /* CONFIG_PROC_FS */
2905 #endif /* CONFIG_NET_CLS_ROUTE */
2907 static __initdata unsigned long rhash_entries;
2908 static int __init set_rhash_entries(char *str)
2912 rhash_entries = simple_strtoul(str, &str, 0);
2915 __setup("rhash_entries=", set_rhash_entries);
2917 int __init ip_rt_init(void)
2921 rt_hash_rnd = (int) ((num_physpages ^ (num_physpages>>8)) ^
2922 (jiffies ^ (jiffies >> 7)));
2924 #ifdef CONFIG_NET_CLS_ROUTE
2928 (PAGE_SIZE << order) < 256 * sizeof(struct ip_rt_acct) * NR_CPUS; order++)
2930 ip_rt_acct = (struct ip_rt_acct *)__get_free_pages(GFP_KERNEL, order);
2932 panic("IP: failed to allocate ip_rt_acct\n");
2933 memset(ip_rt_acct, 0, PAGE_SIZE << order);
2937 ipv4_dst_ops.kmem_cachep =
2938 kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
2939 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2941 ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
2943 rt_hash_table = (struct rt_hash_bucket *)
2944 alloc_large_system_hash("IP route cache",
2945 sizeof(struct rt_hash_bucket),
2947 (num_physpages >= 128 * 1024) ?
2953 memset(rt_hash_table, 0, (rt_hash_mask + 1) * sizeof(struct rt_hash_bucket));
2954 rt_hash_lock_init();
2956 ipv4_dst_ops.gc_thresh = (rt_hash_mask + 1);
2957 ip_rt_max_size = (rt_hash_mask + 1) * 16;
2962 setup_timer(&rt_flush_timer, rt_run_flush, 0);
2963 setup_timer(&rt_secret_timer, rt_secret_rebuild, 0);
2965 /* All the timers, started at system startup tend
2966 to synchronize. Perturb it a bit.
2968 schedule_delayed_work(&expires_work,
2969 net_random() % ip_rt_gc_interval + ip_rt_gc_interval);
2971 rt_secret_timer.expires = jiffies + net_random() % ip_rt_secret_interval +
2972 ip_rt_secret_interval;
2973 add_timer(&rt_secret_timer);
2975 #ifdef CONFIG_PROC_FS
2977 struct proc_dir_entry *rtstat_pde = NULL; /* keep gcc happy */
2978 if (!proc_net_fops_create(&init_net, "rt_cache", S_IRUGO, &rt_cache_seq_fops) ||
2979 !(rtstat_pde = create_proc_entry("rt_cache", S_IRUGO,
2980 init_net.proc_net_stat))) {
2983 rtstat_pde->proc_fops = &rt_cpu_seq_fops;
2985 #ifdef CONFIG_NET_CLS_ROUTE
2986 create_proc_read_entry("rt_acct", 0, init_net.proc_net, ip_rt_acct_read, NULL);
2993 rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL);
2998 EXPORT_SYMBOL(__ip_select_ident);
2999 EXPORT_SYMBOL(ip_route_input);
3000 EXPORT_SYMBOL(ip_route_output_key);