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 int ip_rt_flush_expected;
137 static unsigned long rt_deadline;
139 #define RTprint(a...) printk(KERN_DEBUG a)
141 static struct timer_list rt_flush_timer;
142 static void rt_worker_func(struct work_struct *work);
143 static DECLARE_DELAYED_WORK(expires_work, rt_worker_func);
144 static struct timer_list rt_secret_timer;
147 * Interface to generic destination cache.
150 static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
151 static void ipv4_dst_destroy(struct dst_entry *dst);
152 static void ipv4_dst_ifdown(struct dst_entry *dst,
153 struct net_device *dev, int how);
154 static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst);
155 static void ipv4_link_failure(struct sk_buff *skb);
156 static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
157 static int rt_garbage_collect(void);
160 static struct dst_ops ipv4_dst_ops = {
162 .protocol = __constant_htons(ETH_P_IP),
163 .gc = rt_garbage_collect,
164 .check = ipv4_dst_check,
165 .destroy = ipv4_dst_destroy,
166 .ifdown = ipv4_dst_ifdown,
167 .negative_advice = ipv4_negative_advice,
168 .link_failure = ipv4_link_failure,
169 .update_pmtu = ip_rt_update_pmtu,
170 .local_out = ip_local_out,
171 .entry_size = sizeof(struct rtable),
174 #define ECN_OR_COST(class) TC_PRIO_##class
176 const __u8 ip_tos2prio[16] = {
180 ECN_OR_COST(BESTEFFORT),
186 ECN_OR_COST(INTERACTIVE),
188 ECN_OR_COST(INTERACTIVE),
189 TC_PRIO_INTERACTIVE_BULK,
190 ECN_OR_COST(INTERACTIVE_BULK),
191 TC_PRIO_INTERACTIVE_BULK,
192 ECN_OR_COST(INTERACTIVE_BULK)
200 /* The locking scheme is rather straight forward:
202 * 1) Read-Copy Update protects the buckets of the central route hash.
203 * 2) Only writers remove entries, and they hold the lock
204 * as they look at rtable reference counts.
205 * 3) Only readers acquire references to rtable entries,
206 * they do so with atomic increments and with the
210 struct rt_hash_bucket {
211 struct rtable *chain;
213 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
214 defined(CONFIG_PROVE_LOCKING)
216 * Instead of using one spinlock for each rt_hash_bucket, we use a table of spinlocks
217 * The size of this table is a power of two and depends on the number of CPUS.
218 * (on lockdep we have a quite big spinlock_t, so keep the size down there)
220 #ifdef CONFIG_LOCKDEP
221 # define RT_HASH_LOCK_SZ 256
224 # define RT_HASH_LOCK_SZ 4096
226 # define RT_HASH_LOCK_SZ 2048
228 # define RT_HASH_LOCK_SZ 1024
230 # define RT_HASH_LOCK_SZ 512
232 # define RT_HASH_LOCK_SZ 256
236 static spinlock_t *rt_hash_locks;
237 # define rt_hash_lock_addr(slot) &rt_hash_locks[(slot) & (RT_HASH_LOCK_SZ - 1)]
238 # define rt_hash_lock_init() { \
240 rt_hash_locks = kmalloc(sizeof(spinlock_t) * RT_HASH_LOCK_SZ, GFP_KERNEL); \
241 if (!rt_hash_locks) panic("IP: failed to allocate rt_hash_locks\n"); \
242 for (i = 0; i < RT_HASH_LOCK_SZ; i++) \
243 spin_lock_init(&rt_hash_locks[i]); \
246 # define rt_hash_lock_addr(slot) NULL
247 # define rt_hash_lock_init()
250 static struct rt_hash_bucket *rt_hash_table;
251 static unsigned rt_hash_mask;
252 static unsigned int rt_hash_log;
253 static unsigned int rt_hash_rnd;
255 static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
256 #define RT_CACHE_STAT_INC(field) \
257 (__raw_get_cpu_var(rt_cache_stat).field++)
259 static int rt_intern_hash(unsigned hash, struct rtable *rth,
260 struct rtable **res);
262 static unsigned int rt_hash_code(u32 daddr, u32 saddr)
264 return (jhash_2words(daddr, saddr, rt_hash_rnd)
268 #define rt_hash(daddr, saddr, idx) \
269 rt_hash_code((__force u32)(__be32)(daddr),\
270 (__force u32)(__be32)(saddr) ^ ((idx) << 5))
272 #ifdef CONFIG_PROC_FS
273 struct rt_cache_iter_state {
277 static struct rtable *rt_cache_get_first(struct seq_file *seq)
279 struct rtable *r = NULL;
280 struct rt_cache_iter_state *st = seq->private;
282 for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
284 r = rt_hash_table[st->bucket].chain;
287 rcu_read_unlock_bh();
289 return rcu_dereference(r);
292 static struct rtable *rt_cache_get_next(struct seq_file *seq, struct rtable *r)
294 struct rt_cache_iter_state *st = seq->private;
296 r = r->u.dst.rt_next;
298 rcu_read_unlock_bh();
299 if (--st->bucket < 0)
302 r = rt_hash_table[st->bucket].chain;
304 return rcu_dereference(r);
307 static struct rtable *rt_cache_get_idx(struct seq_file *seq, loff_t pos)
309 struct rtable *r = rt_cache_get_first(seq);
312 while (pos && (r = rt_cache_get_next(seq, r)))
314 return pos ? NULL : r;
317 static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
319 return *pos ? rt_cache_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
322 static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
324 struct rtable *r = NULL;
326 if (v == SEQ_START_TOKEN)
327 r = rt_cache_get_first(seq);
329 r = rt_cache_get_next(seq, v);
334 static void rt_cache_seq_stop(struct seq_file *seq, void *v)
336 if (v && v != SEQ_START_TOKEN)
337 rcu_read_unlock_bh();
340 static int rt_cache_seq_show(struct seq_file *seq, void *v)
342 if (v == SEQ_START_TOKEN)
343 seq_printf(seq, "%-127s\n",
344 "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
345 "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
348 struct rtable *r = v;
351 sprintf(temp, "%s\t%08lX\t%08lX\t%8X\t%d\t%u\t%d\t"
352 "%08lX\t%d\t%u\t%u\t%02X\t%d\t%1d\t%08X",
353 r->u.dst.dev ? r->u.dst.dev->name : "*",
354 (unsigned long)r->rt_dst, (unsigned long)r->rt_gateway,
355 r->rt_flags, atomic_read(&r->u.dst.__refcnt),
356 r->u.dst.__use, 0, (unsigned long)r->rt_src,
357 (dst_metric(&r->u.dst, RTAX_ADVMSS) ?
358 (int)dst_metric(&r->u.dst, RTAX_ADVMSS) + 40 : 0),
359 dst_metric(&r->u.dst, RTAX_WINDOW),
360 (int)((dst_metric(&r->u.dst, RTAX_RTT) >> 3) +
361 dst_metric(&r->u.dst, RTAX_RTTVAR)),
363 r->u.dst.hh ? atomic_read(&r->u.dst.hh->hh_refcnt) : -1,
364 r->u.dst.hh ? (r->u.dst.hh->hh_output ==
367 seq_printf(seq, "%-127s\n", temp);
372 static const struct seq_operations rt_cache_seq_ops = {
373 .start = rt_cache_seq_start,
374 .next = rt_cache_seq_next,
375 .stop = rt_cache_seq_stop,
376 .show = rt_cache_seq_show,
379 static int rt_cache_seq_open(struct inode *inode, struct file *file)
381 return seq_open_private(file, &rt_cache_seq_ops,
382 sizeof(struct rt_cache_iter_state));
385 static const struct file_operations rt_cache_seq_fops = {
386 .owner = THIS_MODULE,
387 .open = rt_cache_seq_open,
390 .release = seq_release_private,
394 static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
399 return SEQ_START_TOKEN;
401 for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
402 if (!cpu_possible(cpu))
405 return &per_cpu(rt_cache_stat, cpu);
410 static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
414 for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
415 if (!cpu_possible(cpu))
418 return &per_cpu(rt_cache_stat, cpu);
424 static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
429 static int rt_cpu_seq_show(struct seq_file *seq, void *v)
431 struct rt_cache_stat *st = v;
433 if (v == SEQ_START_TOKEN) {
434 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");
438 seq_printf(seq,"%08x %08x %08x %08x %08x %08x %08x %08x "
439 " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
440 atomic_read(&ipv4_dst_ops.entries),
463 static const struct seq_operations rt_cpu_seq_ops = {
464 .start = rt_cpu_seq_start,
465 .next = rt_cpu_seq_next,
466 .stop = rt_cpu_seq_stop,
467 .show = rt_cpu_seq_show,
471 static int rt_cpu_seq_open(struct inode *inode, struct file *file)
473 return seq_open(file, &rt_cpu_seq_ops);
476 static const struct file_operations rt_cpu_seq_fops = {
477 .owner = THIS_MODULE,
478 .open = rt_cpu_seq_open,
481 .release = seq_release,
484 #ifdef CONFIG_NET_CLS_ROUTE
485 static int ip_rt_acct_read(char *buffer, char **start, off_t offset,
486 int length, int *eof, void *data)
490 if ((offset & 3) || (length & 3))
493 if (offset >= sizeof(struct ip_rt_acct) * 256) {
498 if (offset + length >= sizeof(struct ip_rt_acct) * 256) {
499 length = sizeof(struct ip_rt_acct) * 256 - offset;
503 offset /= sizeof(u32);
506 u32 *dst = (u32 *) buffer;
509 memset(dst, 0, length);
511 for_each_possible_cpu(i) {
515 src = ((u32 *) per_cpu_ptr(ip_rt_acct, i)) + offset;
516 for (j = 0; j < length/4; j++)
523 #endif /* CONFIG_PROC_FS */
525 static __inline__ void rt_free(struct rtable *rt)
527 call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
530 static __inline__ void rt_drop(struct rtable *rt)
533 call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
536 static __inline__ int rt_fast_clean(struct rtable *rth)
538 /* Kill broadcast/multicast entries very aggresively, if they
539 collide in hash table with more useful entries */
540 return (rth->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) &&
541 rth->fl.iif && rth->u.dst.rt_next;
544 static __inline__ int rt_valuable(struct rtable *rth)
546 return (rth->rt_flags & (RTCF_REDIRECTED | RTCF_NOTIFY)) ||
550 static int rt_may_expire(struct rtable *rth, unsigned long tmo1, unsigned long tmo2)
555 if (atomic_read(&rth->u.dst.__refcnt))
559 if (rth->u.dst.expires &&
560 time_after_eq(jiffies, rth->u.dst.expires))
563 age = jiffies - rth->u.dst.lastuse;
565 if ((age <= tmo1 && !rt_fast_clean(rth)) ||
566 (age <= tmo2 && rt_valuable(rth)))
572 /* Bits of score are:
574 * 30: not quite useless
575 * 29..0: usage counter
577 static inline u32 rt_score(struct rtable *rt)
579 u32 score = jiffies - rt->u.dst.lastuse;
581 score = ~score & ~(3<<30);
587 !(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL)))
593 static inline int compare_keys(struct flowi *fl1, struct flowi *fl2)
595 return ((__force u32)((fl1->nl_u.ip4_u.daddr ^ fl2->nl_u.ip4_u.daddr) |
596 (fl1->nl_u.ip4_u.saddr ^ fl2->nl_u.ip4_u.saddr)) |
597 (fl1->mark ^ fl2->mark) |
598 (*(u16 *)&fl1->nl_u.ip4_u.tos ^
599 *(u16 *)&fl2->nl_u.ip4_u.tos) |
600 (fl1->oif ^ fl2->oif) |
601 (fl1->iif ^ fl2->iif)) == 0;
605 * Perform a full scan of hash table and free all entries.
606 * Can be called by a softirq or a process.
607 * In the later case, we want to be reschedule if necessary
609 static void rt_do_flush(int process_context)
612 struct rtable *rth, *next;
614 for (i = 0; i <= rt_hash_mask; i++) {
615 if (process_context && need_resched())
617 rth = rt_hash_table[i].chain;
621 spin_lock_bh(rt_hash_lock_addr(i));
622 rth = rt_hash_table[i].chain;
623 rt_hash_table[i].chain = NULL;
624 spin_unlock_bh(rt_hash_lock_addr(i));
626 for (; rth; rth = next) {
627 next = rth->u.dst.rt_next;
633 static void rt_check_expire(void)
635 static unsigned int rover;
636 unsigned int i = rover, goal;
637 struct rtable *rth, **rthp;
640 mult = ((u64)ip_rt_gc_interval) << rt_hash_log;
641 if (ip_rt_gc_timeout > 1)
642 do_div(mult, ip_rt_gc_timeout);
643 goal = (unsigned int)mult;
644 if (goal > rt_hash_mask)
645 goal = rt_hash_mask + 1;
646 for (; goal > 0; goal--) {
647 unsigned long tmo = ip_rt_gc_timeout;
649 i = (i + 1) & rt_hash_mask;
650 rthp = &rt_hash_table[i].chain;
657 spin_lock_bh(rt_hash_lock_addr(i));
658 while ((rth = *rthp) != NULL) {
659 if (rth->u.dst.expires) {
660 /* Entry is expired even if it is in use */
661 if (time_before_eq(jiffies, rth->u.dst.expires)) {
663 rthp = &rth->u.dst.rt_next;
666 } else if (!rt_may_expire(rth, tmo, ip_rt_gc_timeout)) {
668 rthp = &rth->u.dst.rt_next;
672 /* Cleanup aged off entries. */
673 *rthp = rth->u.dst.rt_next;
676 spin_unlock_bh(rt_hash_lock_addr(i));
682 * rt_worker_func() is run in process context.
683 * If a whole flush was scheduled, it is done.
684 * Else, we call rt_check_expire() to scan part of the hash table
686 static void rt_worker_func(struct work_struct *work)
688 if (ip_rt_flush_expected) {
689 ip_rt_flush_expected = 0;
693 schedule_delayed_work(&expires_work, ip_rt_gc_interval);
696 /* This can run from both BH and non-BH contexts, the latter
697 * in the case of a forced flush event.
699 static void rt_run_flush(unsigned long process_context)
703 get_random_bytes(&rt_hash_rnd, 4);
705 rt_do_flush(process_context);
708 static DEFINE_SPINLOCK(rt_flush_lock);
710 void rt_cache_flush(int delay)
712 unsigned long now = jiffies;
713 int user_mode = !in_softirq();
716 delay = ip_rt_min_delay;
718 spin_lock_bh(&rt_flush_lock);
720 if (del_timer(&rt_flush_timer) && delay > 0 && rt_deadline) {
721 long tmo = (long)(rt_deadline - now);
723 /* If flush timer is already running
724 and flush request is not immediate (delay > 0):
726 if deadline is not achieved, prolongate timer to "delay",
727 otherwise fire it at deadline time.
730 if (user_mode && tmo < ip_rt_max_delay-ip_rt_min_delay)
738 spin_unlock_bh(&rt_flush_lock);
739 rt_run_flush(user_mode);
743 if (rt_deadline == 0)
744 rt_deadline = now + ip_rt_max_delay;
746 mod_timer(&rt_flush_timer, now+delay);
747 spin_unlock_bh(&rt_flush_lock);
751 * We change rt_hash_rnd and ask next rt_worker_func() invocation
752 * to perform a flush in process context
754 static void rt_secret_rebuild(unsigned long dummy)
756 get_random_bytes(&rt_hash_rnd, 4);
757 ip_rt_flush_expected = 1;
758 cancel_delayed_work(&expires_work);
759 schedule_delayed_work(&expires_work, HZ/10);
760 mod_timer(&rt_secret_timer, jiffies + ip_rt_secret_interval);
764 Short description of GC goals.
766 We want to build algorithm, which will keep routing cache
767 at some equilibrium point, when number of aged off entries
768 is kept approximately equal to newly generated ones.
770 Current expiration strength is variable "expire".
771 We try to adjust it dynamically, so that if networking
772 is idle expires is large enough to keep enough of warm entries,
773 and when load increases it reduces to limit cache size.
776 static int rt_garbage_collect(void)
778 static unsigned long expire = RT_GC_TIMEOUT;
779 static unsigned long last_gc;
781 static int equilibrium;
782 struct rtable *rth, **rthp;
783 unsigned long now = jiffies;
787 * Garbage collection is pretty expensive,
788 * do not make it too frequently.
791 RT_CACHE_STAT_INC(gc_total);
793 if (now - last_gc < ip_rt_gc_min_interval &&
794 atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size) {
795 RT_CACHE_STAT_INC(gc_ignored);
799 /* Calculate number of entries, which we want to expire now. */
800 goal = atomic_read(&ipv4_dst_ops.entries) -
801 (ip_rt_gc_elasticity << rt_hash_log);
803 if (equilibrium < ipv4_dst_ops.gc_thresh)
804 equilibrium = ipv4_dst_ops.gc_thresh;
805 goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
807 equilibrium += min_t(unsigned int, goal / 2, rt_hash_mask + 1);
808 goal = atomic_read(&ipv4_dst_ops.entries) - equilibrium;
811 /* We are in dangerous area. Try to reduce cache really
814 goal = max_t(unsigned int, goal / 2, rt_hash_mask + 1);
815 equilibrium = atomic_read(&ipv4_dst_ops.entries) - goal;
818 if (now - last_gc >= ip_rt_gc_min_interval)
829 for (i = rt_hash_mask, k = rover; i >= 0; i--) {
830 unsigned long tmo = expire;
832 k = (k + 1) & rt_hash_mask;
833 rthp = &rt_hash_table[k].chain;
834 spin_lock_bh(rt_hash_lock_addr(k));
835 while ((rth = *rthp) != NULL) {
836 if (!rt_may_expire(rth, tmo, expire)) {
838 rthp = &rth->u.dst.rt_next;
841 *rthp = rth->u.dst.rt_next;
845 spin_unlock_bh(rt_hash_lock_addr(k));
854 /* Goal is not achieved. We stop process if:
856 - if expire reduced to zero. Otherwise, expire is halfed.
857 - if table is not full.
858 - if we are called from interrupt.
859 - jiffies check is just fallback/debug loop breaker.
860 We will not spin here for long time in any case.
863 RT_CACHE_STAT_INC(gc_goal_miss);
869 #if RT_CACHE_DEBUG >= 2
870 printk(KERN_DEBUG "expire>> %u %d %d %d\n", expire,
871 atomic_read(&ipv4_dst_ops.entries), goal, i);
874 if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
876 } while (!in_softirq() && time_before_eq(jiffies, now));
878 if (atomic_read(&ipv4_dst_ops.entries) < ip_rt_max_size)
881 printk(KERN_WARNING "dst cache overflow\n");
882 RT_CACHE_STAT_INC(gc_dst_overflow);
886 expire += ip_rt_gc_min_interval;
887 if (expire > ip_rt_gc_timeout ||
888 atomic_read(&ipv4_dst_ops.entries) < ipv4_dst_ops.gc_thresh)
889 expire = ip_rt_gc_timeout;
890 #if RT_CACHE_DEBUG >= 2
891 printk(KERN_DEBUG "expire++ %u %d %d %d\n", expire,
892 atomic_read(&ipv4_dst_ops.entries), goal, rover);
897 static int rt_intern_hash(unsigned hash, struct rtable *rt, struct rtable **rp)
899 struct rtable *rth, **rthp;
901 struct rtable *cand, **candp;
904 int attempts = !in_softirq();
913 rthp = &rt_hash_table[hash].chain;
915 spin_lock_bh(rt_hash_lock_addr(hash));
916 while ((rth = *rthp) != NULL) {
917 if (compare_keys(&rth->fl, &rt->fl)) {
919 *rthp = rth->u.dst.rt_next;
921 * Since lookup is lockfree, the deletion
922 * must be visible to another weakly ordered CPU before
923 * the insertion at the start of the hash chain.
925 rcu_assign_pointer(rth->u.dst.rt_next,
926 rt_hash_table[hash].chain);
928 * Since lookup is lockfree, the update writes
929 * must be ordered for consistency on SMP.
931 rcu_assign_pointer(rt_hash_table[hash].chain, rth);
933 dst_use(&rth->u.dst, now);
934 spin_unlock_bh(rt_hash_lock_addr(hash));
941 if (!atomic_read(&rth->u.dst.__refcnt)) {
942 u32 score = rt_score(rth);
944 if (score <= min_score) {
953 rthp = &rth->u.dst.rt_next;
957 /* ip_rt_gc_elasticity used to be average length of chain
958 * length, when exceeded gc becomes really aggressive.
960 * The second limit is less certain. At the moment it allows
961 * only 2 entries per bucket. We will see.
963 if (chain_length > ip_rt_gc_elasticity) {
964 *candp = cand->u.dst.rt_next;
969 /* Try to bind route to arp only if it is output
970 route or unicast forwarding path.
972 if (rt->rt_type == RTN_UNICAST || rt->fl.iif == 0) {
973 int err = arp_bind_neighbour(&rt->u.dst);
975 spin_unlock_bh(rt_hash_lock_addr(hash));
977 if (err != -ENOBUFS) {
982 /* Neighbour tables are full and nothing
983 can be released. Try to shrink route cache,
984 it is most likely it holds some neighbour records.
986 if (attempts-- > 0) {
987 int saved_elasticity = ip_rt_gc_elasticity;
988 int saved_int = ip_rt_gc_min_interval;
989 ip_rt_gc_elasticity = 1;
990 ip_rt_gc_min_interval = 0;
991 rt_garbage_collect();
992 ip_rt_gc_min_interval = saved_int;
993 ip_rt_gc_elasticity = saved_elasticity;
998 printk(KERN_WARNING "Neighbour table overflow.\n");
1004 rt->u.dst.rt_next = rt_hash_table[hash].chain;
1005 #if RT_CACHE_DEBUG >= 2
1006 if (rt->u.dst.rt_next) {
1008 printk(KERN_DEBUG "rt_cache @%02x: %u.%u.%u.%u", hash,
1009 NIPQUAD(rt->rt_dst));
1010 for (trt = rt->u.dst.rt_next; trt; trt = trt->u.dst.rt_next)
1011 printk(" . %u.%u.%u.%u", NIPQUAD(trt->rt_dst));
1015 rt_hash_table[hash].chain = rt;
1016 spin_unlock_bh(rt_hash_lock_addr(hash));
1021 void rt_bind_peer(struct rtable *rt, int create)
1023 static DEFINE_SPINLOCK(rt_peer_lock);
1024 struct inet_peer *peer;
1026 peer = inet_getpeer(rt->rt_dst, create);
1028 spin_lock_bh(&rt_peer_lock);
1029 if (rt->peer == NULL) {
1033 spin_unlock_bh(&rt_peer_lock);
1039 * Peer allocation may fail only in serious out-of-memory conditions. However
1040 * we still can generate some output.
1041 * Random ID selection looks a bit dangerous because we have no chances to
1042 * select ID being unique in a reasonable period of time.
1043 * But broken packet identifier may be better than no packet at all.
1045 static void ip_select_fb_ident(struct iphdr *iph)
1047 static DEFINE_SPINLOCK(ip_fb_id_lock);
1048 static u32 ip_fallback_id;
1051 spin_lock_bh(&ip_fb_id_lock);
1052 salt = secure_ip_id((__force __be32)ip_fallback_id ^ iph->daddr);
1053 iph->id = htons(salt & 0xFFFF);
1054 ip_fallback_id = salt;
1055 spin_unlock_bh(&ip_fb_id_lock);
1058 void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more)
1060 struct rtable *rt = (struct rtable *) dst;
1063 if (rt->peer == NULL)
1064 rt_bind_peer(rt, 1);
1066 /* If peer is attached to destination, it is never detached,
1067 so that we need not to grab a lock to dereference it.
1070 iph->id = htons(inet_getid(rt->peer, more));
1074 printk(KERN_DEBUG "rt_bind_peer(0) @%p\n",
1075 __builtin_return_address(0));
1077 ip_select_fb_ident(iph);
1080 static void rt_del(unsigned hash, struct rtable *rt)
1082 struct rtable **rthp;
1084 spin_lock_bh(rt_hash_lock_addr(hash));
1086 for (rthp = &rt_hash_table[hash].chain; *rthp;
1087 rthp = &(*rthp)->u.dst.rt_next)
1089 *rthp = rt->u.dst.rt_next;
1093 spin_unlock_bh(rt_hash_lock_addr(hash));
1096 void ip_rt_redirect(__be32 old_gw, __be32 daddr, __be32 new_gw,
1097 __be32 saddr, struct net_device *dev)
1100 struct in_device *in_dev = in_dev_get(dev);
1101 struct rtable *rth, **rthp;
1102 __be32 skeys[2] = { saddr, 0 };
1103 int ikeys[2] = { dev->ifindex, 0 };
1104 struct netevent_redirect netevent;
1109 if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev)
1110 || MULTICAST(new_gw) || BADCLASS(new_gw) || ZERONET(new_gw))
1111 goto reject_redirect;
1113 if (!IN_DEV_SHARED_MEDIA(in_dev)) {
1114 if (!inet_addr_onlink(in_dev, new_gw, old_gw))
1115 goto reject_redirect;
1116 if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
1117 goto reject_redirect;
1119 if (inet_addr_type(new_gw) != RTN_UNICAST)
1120 goto reject_redirect;
1123 for (i = 0; i < 2; i++) {
1124 for (k = 0; k < 2; k++) {
1125 unsigned hash = rt_hash(daddr, skeys[i], ikeys[k]);
1127 rthp=&rt_hash_table[hash].chain;
1130 while ((rth = rcu_dereference(*rthp)) != NULL) {
1133 if (rth->fl.fl4_dst != daddr ||
1134 rth->fl.fl4_src != skeys[i] ||
1135 rth->fl.oif != ikeys[k] ||
1137 rthp = &rth->u.dst.rt_next;
1141 if (rth->rt_dst != daddr ||
1142 rth->rt_src != saddr ||
1144 rth->rt_gateway != old_gw ||
1145 rth->u.dst.dev != dev)
1148 dst_hold(&rth->u.dst);
1151 rt = dst_alloc(&ipv4_dst_ops);
1158 /* Copy all the information. */
1160 INIT_RCU_HEAD(&rt->u.dst.rcu_head);
1161 rt->u.dst.__use = 1;
1162 atomic_set(&rt->u.dst.__refcnt, 1);
1163 rt->u.dst.child = NULL;
1165 dev_hold(rt->u.dst.dev);
1167 in_dev_hold(rt->idev);
1168 rt->u.dst.obsolete = 0;
1169 rt->u.dst.lastuse = jiffies;
1170 rt->u.dst.path = &rt->u.dst;
1171 rt->u.dst.neighbour = NULL;
1172 rt->u.dst.hh = NULL;
1173 rt->u.dst.xfrm = NULL;
1175 rt->rt_flags |= RTCF_REDIRECTED;
1177 /* Gateway is different ... */
1178 rt->rt_gateway = new_gw;
1180 /* Redirect received -> path was valid */
1181 dst_confirm(&rth->u.dst);
1184 atomic_inc(&rt->peer->refcnt);
1186 if (arp_bind_neighbour(&rt->u.dst) ||
1187 !(rt->u.dst.neighbour->nud_state &
1189 if (rt->u.dst.neighbour)
1190 neigh_event_send(rt->u.dst.neighbour, NULL);
1196 netevent.old = &rth->u.dst;
1197 netevent.new = &rt->u.dst;
1198 call_netevent_notifiers(NETEVENT_REDIRECT,
1202 if (!rt_intern_hash(hash, rt, &rt))
1215 #ifdef CONFIG_IP_ROUTE_VERBOSE
1216 if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
1217 printk(KERN_INFO "Redirect from %u.%u.%u.%u on %s about "
1218 "%u.%u.%u.%u ignored.\n"
1219 " Advised path = %u.%u.%u.%u -> %u.%u.%u.%u\n",
1220 NIPQUAD(old_gw), dev->name, NIPQUAD(new_gw),
1221 NIPQUAD(saddr), NIPQUAD(daddr));
1226 static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
1228 struct rtable *rt = (struct rtable*)dst;
1229 struct dst_entry *ret = dst;
1232 if (dst->obsolete) {
1235 } else if ((rt->rt_flags & RTCF_REDIRECTED) ||
1236 rt->u.dst.expires) {
1237 unsigned hash = rt_hash(rt->fl.fl4_dst, rt->fl.fl4_src,
1239 #if RT_CACHE_DEBUG >= 1
1240 printk(KERN_DEBUG "ipv4_negative_advice: redirect to "
1241 "%u.%u.%u.%u/%02x dropped\n",
1242 NIPQUAD(rt->rt_dst), rt->fl.fl4_tos);
1253 * 1. The first ip_rt_redirect_number redirects are sent
1254 * with exponential backoff, then we stop sending them at all,
1255 * assuming that the host ignores our redirects.
1256 * 2. If we did not see packets requiring redirects
1257 * during ip_rt_redirect_silence, we assume that the host
1258 * forgot redirected route and start to send redirects again.
1260 * This algorithm is much cheaper and more intelligent than dumb load limiting
1263 * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
1264 * and "frag. need" (breaks PMTU discovery) in icmp.c.
1267 void ip_rt_send_redirect(struct sk_buff *skb)
1269 struct rtable *rt = (struct rtable*)skb->dst;
1270 struct in_device *in_dev = in_dev_get(rt->u.dst.dev);
1275 if (!IN_DEV_TX_REDIRECTS(in_dev))
1278 /* No redirected packets during ip_rt_redirect_silence;
1279 * reset the algorithm.
1281 if (time_after(jiffies, rt->u.dst.rate_last + ip_rt_redirect_silence))
1282 rt->u.dst.rate_tokens = 0;
1284 /* Too many ignored redirects; do not send anything
1285 * set u.dst.rate_last to the last seen redirected packet.
1287 if (rt->u.dst.rate_tokens >= ip_rt_redirect_number) {
1288 rt->u.dst.rate_last = jiffies;
1292 /* Check for load limit; set rate_last to the latest sent
1295 if (rt->u.dst.rate_tokens == 0 ||
1297 (rt->u.dst.rate_last +
1298 (ip_rt_redirect_load << rt->u.dst.rate_tokens)))) {
1299 icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway);
1300 rt->u.dst.rate_last = jiffies;
1301 ++rt->u.dst.rate_tokens;
1302 #ifdef CONFIG_IP_ROUTE_VERBOSE
1303 if (IN_DEV_LOG_MARTIANS(in_dev) &&
1304 rt->u.dst.rate_tokens == ip_rt_redirect_number &&
1306 printk(KERN_WARNING "host %u.%u.%u.%u/if%d ignores "
1307 "redirects for %u.%u.%u.%u to %u.%u.%u.%u.\n",
1308 NIPQUAD(rt->rt_src), rt->rt_iif,
1309 NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_gateway));
1316 static int ip_error(struct sk_buff *skb)
1318 struct rtable *rt = (struct rtable*)skb->dst;
1322 switch (rt->u.dst.error) {
1327 code = ICMP_HOST_UNREACH;
1330 code = ICMP_NET_UNREACH;
1331 IP_INC_STATS_BH(IPSTATS_MIB_INNOROUTES);
1334 code = ICMP_PKT_FILTERED;
1339 rt->u.dst.rate_tokens += now - rt->u.dst.rate_last;
1340 if (rt->u.dst.rate_tokens > ip_rt_error_burst)
1341 rt->u.dst.rate_tokens = ip_rt_error_burst;
1342 rt->u.dst.rate_last = now;
1343 if (rt->u.dst.rate_tokens >= ip_rt_error_cost) {
1344 rt->u.dst.rate_tokens -= ip_rt_error_cost;
1345 icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
1348 out: kfree_skb(skb);
1353 * The last two values are not from the RFC but
1354 * are needed for AMPRnet AX.25 paths.
1357 static const unsigned short mtu_plateau[] =
1358 {32000, 17914, 8166, 4352, 2002, 1492, 576, 296, 216, 128 };
1360 static __inline__ unsigned short guess_mtu(unsigned short old_mtu)
1364 for (i = 0; i < ARRAY_SIZE(mtu_plateau); i++)
1365 if (old_mtu > mtu_plateau[i])
1366 return mtu_plateau[i];
1370 unsigned short ip_rt_frag_needed(struct iphdr *iph, unsigned short new_mtu)
1373 unsigned short old_mtu = ntohs(iph->tot_len);
1375 __be32 skeys[2] = { iph->saddr, 0, };
1376 __be32 daddr = iph->daddr;
1377 unsigned short est_mtu = 0;
1379 if (ipv4_config.no_pmtu_disc)
1382 for (i = 0; i < 2; i++) {
1383 unsigned hash = rt_hash(daddr, skeys[i], 0);
1386 for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
1387 rth = rcu_dereference(rth->u.dst.rt_next)) {
1388 if (rth->fl.fl4_dst == daddr &&
1389 rth->fl.fl4_src == skeys[i] &&
1390 rth->rt_dst == daddr &&
1391 rth->rt_src == iph->saddr &&
1393 !(dst_metric_locked(&rth->u.dst, RTAX_MTU))) {
1394 unsigned short mtu = new_mtu;
1396 if (new_mtu < 68 || new_mtu >= old_mtu) {
1398 /* BSD 4.2 compatibility hack :-( */
1400 old_mtu >= rth->u.dst.metrics[RTAX_MTU-1] &&
1401 old_mtu >= 68 + (iph->ihl << 2))
1402 old_mtu -= iph->ihl << 2;
1404 mtu = guess_mtu(old_mtu);
1406 if (mtu <= rth->u.dst.metrics[RTAX_MTU-1]) {
1407 if (mtu < rth->u.dst.metrics[RTAX_MTU-1]) {
1408 dst_confirm(&rth->u.dst);
1409 if (mtu < ip_rt_min_pmtu) {
1410 mtu = ip_rt_min_pmtu;
1411 rth->u.dst.metrics[RTAX_LOCK-1] |=
1414 rth->u.dst.metrics[RTAX_MTU-1] = mtu;
1415 dst_set_expires(&rth->u.dst,
1424 return est_mtu ? : new_mtu;
1427 static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
1429 if (dst->metrics[RTAX_MTU-1] > mtu && mtu >= 68 &&
1430 !(dst_metric_locked(dst, RTAX_MTU))) {
1431 if (mtu < ip_rt_min_pmtu) {
1432 mtu = ip_rt_min_pmtu;
1433 dst->metrics[RTAX_LOCK-1] |= (1 << RTAX_MTU);
1435 dst->metrics[RTAX_MTU-1] = mtu;
1436 dst_set_expires(dst, ip_rt_mtu_expires);
1437 call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst);
1441 static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie)
1446 static void ipv4_dst_destroy(struct dst_entry *dst)
1448 struct rtable *rt = (struct rtable *) dst;
1449 struct inet_peer *peer = rt->peer;
1450 struct in_device *idev = rt->idev;
1463 static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
1466 struct rtable *rt = (struct rtable *) dst;
1467 struct in_device *idev = rt->idev;
1468 if (dev != init_net.loopback_dev && idev && idev->dev == dev) {
1469 struct in_device *loopback_idev = in_dev_get(init_net.loopback_dev);
1470 if (loopback_idev) {
1471 rt->idev = loopback_idev;
1477 static void ipv4_link_failure(struct sk_buff *skb)
1481 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
1483 rt = (struct rtable *) skb->dst;
1485 dst_set_expires(&rt->u.dst, 0);
1488 static int ip_rt_bug(struct sk_buff *skb)
1490 printk(KERN_DEBUG "ip_rt_bug: %u.%u.%u.%u -> %u.%u.%u.%u, %s\n",
1491 NIPQUAD(ip_hdr(skb)->saddr), NIPQUAD(ip_hdr(skb)->daddr),
1492 skb->dev ? skb->dev->name : "?");
1498 We do not cache source address of outgoing interface,
1499 because it is used only by IP RR, TS and SRR options,
1500 so that it out of fast path.
1502 BTW remember: "addr" is allowed to be not aligned
1506 void ip_rt_get_source(u8 *addr, struct rtable *rt)
1509 struct fib_result res;
1511 if (rt->fl.iif == 0)
1513 else if (fib_lookup(&rt->fl, &res) == 0) {
1514 src = FIB_RES_PREFSRC(res);
1517 src = inet_select_addr(rt->u.dst.dev, rt->rt_gateway,
1519 memcpy(addr, &src, 4);
1522 #ifdef CONFIG_NET_CLS_ROUTE
1523 static void set_class_tag(struct rtable *rt, u32 tag)
1525 if (!(rt->u.dst.tclassid & 0xFFFF))
1526 rt->u.dst.tclassid |= tag & 0xFFFF;
1527 if (!(rt->u.dst.tclassid & 0xFFFF0000))
1528 rt->u.dst.tclassid |= tag & 0xFFFF0000;
1532 static void rt_set_nexthop(struct rtable *rt, struct fib_result *res, u32 itag)
1534 struct fib_info *fi = res->fi;
1537 if (FIB_RES_GW(*res) &&
1538 FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
1539 rt->rt_gateway = FIB_RES_GW(*res);
1540 memcpy(rt->u.dst.metrics, fi->fib_metrics,
1541 sizeof(rt->u.dst.metrics));
1542 if (fi->fib_mtu == 0) {
1543 rt->u.dst.metrics[RTAX_MTU-1] = rt->u.dst.dev->mtu;
1544 if (rt->u.dst.metrics[RTAX_LOCK-1] & (1 << RTAX_MTU) &&
1545 rt->rt_gateway != rt->rt_dst &&
1546 rt->u.dst.dev->mtu > 576)
1547 rt->u.dst.metrics[RTAX_MTU-1] = 576;
1549 #ifdef CONFIG_NET_CLS_ROUTE
1550 rt->u.dst.tclassid = FIB_RES_NH(*res).nh_tclassid;
1553 rt->u.dst.metrics[RTAX_MTU-1]= rt->u.dst.dev->mtu;
1555 if (rt->u.dst.metrics[RTAX_HOPLIMIT-1] == 0)
1556 rt->u.dst.metrics[RTAX_HOPLIMIT-1] = sysctl_ip_default_ttl;
1557 if (rt->u.dst.metrics[RTAX_MTU-1] > IP_MAX_MTU)
1558 rt->u.dst.metrics[RTAX_MTU-1] = IP_MAX_MTU;
1559 if (rt->u.dst.metrics[RTAX_ADVMSS-1] == 0)
1560 rt->u.dst.metrics[RTAX_ADVMSS-1] = max_t(unsigned int, rt->u.dst.dev->mtu - 40,
1562 if (rt->u.dst.metrics[RTAX_ADVMSS-1] > 65535 - 40)
1563 rt->u.dst.metrics[RTAX_ADVMSS-1] = 65535 - 40;
1565 #ifdef CONFIG_NET_CLS_ROUTE
1566 #ifdef CONFIG_IP_MULTIPLE_TABLES
1567 set_class_tag(rt, fib_rules_tclass(res));
1569 set_class_tag(rt, itag);
1571 rt->rt_type = res->type;
1574 static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
1575 u8 tos, struct net_device *dev, int our)
1580 struct in_device *in_dev = in_dev_get(dev);
1583 /* Primary sanity checks. */
1588 if (MULTICAST(saddr) || BADCLASS(saddr) || LOOPBACK(saddr) ||
1589 skb->protocol != htons(ETH_P_IP))
1592 if (ZERONET(saddr)) {
1593 if (!LOCAL_MCAST(daddr))
1595 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
1596 } else if (fib_validate_source(saddr, 0, tos, 0,
1597 dev, &spec_dst, &itag) < 0)
1600 rth = dst_alloc(&ipv4_dst_ops);
1604 rth->u.dst.output= ip_rt_bug;
1606 atomic_set(&rth->u.dst.__refcnt, 1);
1607 rth->u.dst.flags= DST_HOST;
1608 if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
1609 rth->u.dst.flags |= DST_NOPOLICY;
1610 rth->fl.fl4_dst = daddr;
1611 rth->rt_dst = daddr;
1612 rth->fl.fl4_tos = tos;
1613 rth->fl.mark = skb->mark;
1614 rth->fl.fl4_src = saddr;
1615 rth->rt_src = saddr;
1616 #ifdef CONFIG_NET_CLS_ROUTE
1617 rth->u.dst.tclassid = itag;
1620 rth->fl.iif = dev->ifindex;
1621 rth->u.dst.dev = init_net.loopback_dev;
1622 dev_hold(rth->u.dst.dev);
1623 rth->idev = in_dev_get(rth->u.dst.dev);
1625 rth->rt_gateway = daddr;
1626 rth->rt_spec_dst= spec_dst;
1627 rth->rt_type = RTN_MULTICAST;
1628 rth->rt_flags = RTCF_MULTICAST;
1630 rth->u.dst.input= ip_local_deliver;
1631 rth->rt_flags |= RTCF_LOCAL;
1634 #ifdef CONFIG_IP_MROUTE
1635 if (!LOCAL_MCAST(daddr) && IN_DEV_MFORWARD(in_dev))
1636 rth->u.dst.input = ip_mr_input;
1638 RT_CACHE_STAT_INC(in_slow_mc);
1641 hash = rt_hash(daddr, saddr, dev->ifindex);
1642 return rt_intern_hash(hash, rth, (struct rtable**) &skb->dst);
1654 static void ip_handle_martian_source(struct net_device *dev,
1655 struct in_device *in_dev,
1656 struct sk_buff *skb,
1660 RT_CACHE_STAT_INC(in_martian_src);
1661 #ifdef CONFIG_IP_ROUTE_VERBOSE
1662 if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
1664 * RFC1812 recommendation, if source is martian,
1665 * the only hint is MAC header.
1667 printk(KERN_WARNING "martian source %u.%u.%u.%u from "
1668 "%u.%u.%u.%u, on dev %s\n",
1669 NIPQUAD(daddr), NIPQUAD(saddr), dev->name);
1670 if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
1672 const unsigned char *p = skb_mac_header(skb);
1673 printk(KERN_WARNING "ll header: ");
1674 for (i = 0; i < dev->hard_header_len; i++, p++) {
1676 if (i < (dev->hard_header_len - 1))
1685 static inline int __mkroute_input(struct sk_buff *skb,
1686 struct fib_result* res,
1687 struct in_device *in_dev,
1688 __be32 daddr, __be32 saddr, u32 tos,
1689 struct rtable **result)
1694 struct in_device *out_dev;
1699 /* get a working reference to the output device */
1700 out_dev = in_dev_get(FIB_RES_DEV(*res));
1701 if (out_dev == NULL) {
1702 if (net_ratelimit())
1703 printk(KERN_CRIT "Bug in ip_route_input" \
1704 "_slow(). Please, report\n");
1709 err = fib_validate_source(saddr, daddr, tos, FIB_RES_OIF(*res),
1710 in_dev->dev, &spec_dst, &itag);
1712 ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
1720 flags |= RTCF_DIRECTSRC;
1722 if (out_dev == in_dev && err && !(flags & (RTCF_NAT | RTCF_MASQ)) &&
1723 (IN_DEV_SHARED_MEDIA(out_dev) ||
1724 inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
1725 flags |= RTCF_DOREDIRECT;
1727 if (skb->protocol != htons(ETH_P_IP)) {
1728 /* Not IP (i.e. ARP). Do not create route, if it is
1729 * invalid for proxy arp. DNAT routes are always valid.
1731 if (out_dev == in_dev && !(flags & RTCF_DNAT)) {
1738 rth = dst_alloc(&ipv4_dst_ops);
1744 atomic_set(&rth->u.dst.__refcnt, 1);
1745 rth->u.dst.flags= DST_HOST;
1746 if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
1747 rth->u.dst.flags |= DST_NOPOLICY;
1748 if (IN_DEV_CONF_GET(out_dev, NOXFRM))
1749 rth->u.dst.flags |= DST_NOXFRM;
1750 rth->fl.fl4_dst = daddr;
1751 rth->rt_dst = daddr;
1752 rth->fl.fl4_tos = tos;
1753 rth->fl.mark = skb->mark;
1754 rth->fl.fl4_src = saddr;
1755 rth->rt_src = saddr;
1756 rth->rt_gateway = daddr;
1758 rth->fl.iif = in_dev->dev->ifindex;
1759 rth->u.dst.dev = (out_dev)->dev;
1760 dev_hold(rth->u.dst.dev);
1761 rth->idev = in_dev_get(rth->u.dst.dev);
1763 rth->rt_spec_dst= spec_dst;
1765 rth->u.dst.input = ip_forward;
1766 rth->u.dst.output = ip_output;
1768 rt_set_nexthop(rth, res, itag);
1770 rth->rt_flags = flags;
1775 /* release the working reference to the output device */
1776 in_dev_put(out_dev);
1780 static inline int ip_mkroute_input(struct sk_buff *skb,
1781 struct fib_result* res,
1782 const struct flowi *fl,
1783 struct in_device *in_dev,
1784 __be32 daddr, __be32 saddr, u32 tos)
1786 struct rtable* rth = NULL;
1790 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1791 if (res->fi && res->fi->fib_nhs > 1 && fl->oif == 0)
1792 fib_select_multipath(fl, res);
1795 /* create a routing cache entry */
1796 err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth);
1800 /* put it into the cache */
1801 hash = rt_hash(daddr, saddr, fl->iif);
1802 return rt_intern_hash(hash, rth, (struct rtable**)&skb->dst);
1806 * NOTE. We drop all the packets that has local source
1807 * addresses, because every properly looped back packet
1808 * must have correct destination already attached by output routine.
1810 * Such approach solves two big problems:
1811 * 1. Not simplex devices are handled properly.
1812 * 2. IP spoofing attempts are filtered with 100% of guarantee.
1815 static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
1816 u8 tos, struct net_device *dev)
1818 struct fib_result res;
1819 struct in_device *in_dev = in_dev_get(dev);
1820 struct flowi fl = { .nl_u = { .ip4_u =
1824 .scope = RT_SCOPE_UNIVERSE,
1827 .iif = dev->ifindex };
1830 struct rtable * rth;
1836 /* IP on this device is disabled. */
1841 /* Check for the most weird martians, which can be not detected
1845 if (MULTICAST(saddr) || BADCLASS(saddr) || LOOPBACK(saddr))
1846 goto martian_source;
1848 if (daddr == htonl(0xFFFFFFFF) || (saddr == 0 && daddr == 0))
1851 /* Accept zero addresses only to limited broadcast;
1852 * I even do not know to fix it or not. Waiting for complains :-)
1855 goto martian_source;
1857 if (BADCLASS(daddr) || ZERONET(daddr) || LOOPBACK(daddr))
1858 goto martian_destination;
1861 * Now we are ready to route packet.
1863 if ((err = fib_lookup(&fl, &res)) != 0) {
1864 if (!IN_DEV_FORWARD(in_dev))
1870 RT_CACHE_STAT_INC(in_slow_tot);
1872 if (res.type == RTN_BROADCAST)
1875 if (res.type == RTN_LOCAL) {
1877 result = fib_validate_source(saddr, daddr, tos,
1878 init_net.loopback_dev->ifindex,
1879 dev, &spec_dst, &itag);
1881 goto martian_source;
1883 flags |= RTCF_DIRECTSRC;
1888 if (!IN_DEV_FORWARD(in_dev))
1890 if (res.type != RTN_UNICAST)
1891 goto martian_destination;
1893 err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos);
1901 if (skb->protocol != htons(ETH_P_IP))
1905 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
1907 err = fib_validate_source(saddr, 0, tos, 0, dev, &spec_dst,
1910 goto martian_source;
1912 flags |= RTCF_DIRECTSRC;
1914 flags |= RTCF_BROADCAST;
1915 res.type = RTN_BROADCAST;
1916 RT_CACHE_STAT_INC(in_brd);
1919 rth = dst_alloc(&ipv4_dst_ops);
1923 rth->u.dst.output= ip_rt_bug;
1925 atomic_set(&rth->u.dst.__refcnt, 1);
1926 rth->u.dst.flags= DST_HOST;
1927 if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
1928 rth->u.dst.flags |= DST_NOPOLICY;
1929 rth->fl.fl4_dst = daddr;
1930 rth->rt_dst = daddr;
1931 rth->fl.fl4_tos = tos;
1932 rth->fl.mark = skb->mark;
1933 rth->fl.fl4_src = saddr;
1934 rth->rt_src = saddr;
1935 #ifdef CONFIG_NET_CLS_ROUTE
1936 rth->u.dst.tclassid = itag;
1939 rth->fl.iif = dev->ifindex;
1940 rth->u.dst.dev = init_net.loopback_dev;
1941 dev_hold(rth->u.dst.dev);
1942 rth->idev = in_dev_get(rth->u.dst.dev);
1943 rth->rt_gateway = daddr;
1944 rth->rt_spec_dst= spec_dst;
1945 rth->u.dst.input= ip_local_deliver;
1946 rth->rt_flags = flags|RTCF_LOCAL;
1947 if (res.type == RTN_UNREACHABLE) {
1948 rth->u.dst.input= ip_error;
1949 rth->u.dst.error= -err;
1950 rth->rt_flags &= ~RTCF_LOCAL;
1952 rth->rt_type = res.type;
1953 hash = rt_hash(daddr, saddr, fl.iif);
1954 err = rt_intern_hash(hash, rth, (struct rtable**)&skb->dst);
1958 RT_CACHE_STAT_INC(in_no_route);
1959 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
1960 res.type = RTN_UNREACHABLE;
1966 * Do not cache martian addresses: they should be logged (RFC1812)
1968 martian_destination:
1969 RT_CACHE_STAT_INC(in_martian_dst);
1970 #ifdef CONFIG_IP_ROUTE_VERBOSE
1971 if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
1972 printk(KERN_WARNING "martian destination %u.%u.%u.%u from "
1973 "%u.%u.%u.%u, dev %s\n",
1974 NIPQUAD(daddr), NIPQUAD(saddr), dev->name);
1978 err = -EHOSTUNREACH;
1990 ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
1994 int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr,
1995 u8 tos, struct net_device *dev)
1997 struct rtable * rth;
1999 int iif = dev->ifindex;
2001 tos &= IPTOS_RT_MASK;
2002 hash = rt_hash(daddr, saddr, iif);
2005 for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
2006 rth = rcu_dereference(rth->u.dst.rt_next)) {
2007 if (rth->fl.fl4_dst == daddr &&
2008 rth->fl.fl4_src == saddr &&
2009 rth->fl.iif == iif &&
2011 rth->fl.mark == skb->mark &&
2012 rth->fl.fl4_tos == tos) {
2013 dst_use(&rth->u.dst, jiffies);
2014 RT_CACHE_STAT_INC(in_hit);
2016 skb->dst = (struct dst_entry*)rth;
2019 RT_CACHE_STAT_INC(in_hlist_search);
2023 /* Multicast recognition logic is moved from route cache to here.
2024 The problem was that too many Ethernet cards have broken/missing
2025 hardware multicast filters :-( As result the host on multicasting
2026 network acquires a lot of useless route cache entries, sort of
2027 SDR messages from all the world. Now we try to get rid of them.
2028 Really, provided software IP multicast filter is organized
2029 reasonably (at least, hashed), it does not result in a slowdown
2030 comparing with route cache reject entries.
2031 Note, that multicast routers are not affected, because
2032 route cache entry is created eventually.
2034 if (MULTICAST(daddr)) {
2035 struct in_device *in_dev;
2038 if ((in_dev = __in_dev_get_rcu(dev)) != NULL) {
2039 int our = ip_check_mc(in_dev, daddr, saddr,
2040 ip_hdr(skb)->protocol);
2042 #ifdef CONFIG_IP_MROUTE
2043 || (!LOCAL_MCAST(daddr) && IN_DEV_MFORWARD(in_dev))
2047 return ip_route_input_mc(skb, daddr, saddr,
2054 return ip_route_input_slow(skb, daddr, saddr, tos, dev);
2057 static inline int __mkroute_output(struct rtable **result,
2058 struct fib_result* res,
2059 const struct flowi *fl,
2060 const struct flowi *oldflp,
2061 struct net_device *dev_out,
2065 struct in_device *in_dev;
2066 u32 tos = RT_FL_TOS(oldflp);
2069 if (LOOPBACK(fl->fl4_src) && !(dev_out->flags&IFF_LOOPBACK))
2072 if (fl->fl4_dst == htonl(0xFFFFFFFF))
2073 res->type = RTN_BROADCAST;
2074 else if (MULTICAST(fl->fl4_dst))
2075 res->type = RTN_MULTICAST;
2076 else if (BADCLASS(fl->fl4_dst) || ZERONET(fl->fl4_dst))
2079 if (dev_out->flags & IFF_LOOPBACK)
2080 flags |= RTCF_LOCAL;
2082 /* get work reference to inet device */
2083 in_dev = in_dev_get(dev_out);
2087 if (res->type == RTN_BROADCAST) {
2088 flags |= RTCF_BROADCAST | RTCF_LOCAL;
2090 fib_info_put(res->fi);
2093 } else if (res->type == RTN_MULTICAST) {
2094 flags |= RTCF_MULTICAST|RTCF_LOCAL;
2095 if (!ip_check_mc(in_dev, oldflp->fl4_dst, oldflp->fl4_src,
2097 flags &= ~RTCF_LOCAL;
2098 /* If multicast route do not exist use
2099 default one, but do not gateway in this case.
2102 if (res->fi && res->prefixlen < 4) {
2103 fib_info_put(res->fi);
2109 rth = dst_alloc(&ipv4_dst_ops);
2115 atomic_set(&rth->u.dst.__refcnt, 1);
2116 rth->u.dst.flags= DST_HOST;
2117 if (IN_DEV_CONF_GET(in_dev, NOXFRM))
2118 rth->u.dst.flags |= DST_NOXFRM;
2119 if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
2120 rth->u.dst.flags |= DST_NOPOLICY;
2122 rth->fl.fl4_dst = oldflp->fl4_dst;
2123 rth->fl.fl4_tos = tos;
2124 rth->fl.fl4_src = oldflp->fl4_src;
2125 rth->fl.oif = oldflp->oif;
2126 rth->fl.mark = oldflp->mark;
2127 rth->rt_dst = fl->fl4_dst;
2128 rth->rt_src = fl->fl4_src;
2129 rth->rt_iif = oldflp->oif ? : dev_out->ifindex;
2130 /* get references to the devices that are to be hold by the routing
2132 rth->u.dst.dev = dev_out;
2134 rth->idev = in_dev_get(dev_out);
2135 rth->rt_gateway = fl->fl4_dst;
2136 rth->rt_spec_dst= fl->fl4_src;
2138 rth->u.dst.output=ip_output;
2140 RT_CACHE_STAT_INC(out_slow_tot);
2142 if (flags & RTCF_LOCAL) {
2143 rth->u.dst.input = ip_local_deliver;
2144 rth->rt_spec_dst = fl->fl4_dst;
2146 if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
2147 rth->rt_spec_dst = fl->fl4_src;
2148 if (flags & RTCF_LOCAL &&
2149 !(dev_out->flags & IFF_LOOPBACK)) {
2150 rth->u.dst.output = ip_mc_output;
2151 RT_CACHE_STAT_INC(out_slow_mc);
2153 #ifdef CONFIG_IP_MROUTE
2154 if (res->type == RTN_MULTICAST) {
2155 if (IN_DEV_MFORWARD(in_dev) &&
2156 !LOCAL_MCAST(oldflp->fl4_dst)) {
2157 rth->u.dst.input = ip_mr_input;
2158 rth->u.dst.output = ip_mc_output;
2164 rt_set_nexthop(rth, res, 0);
2166 rth->rt_flags = flags;
2170 /* release work reference to inet device */
2176 static inline int ip_mkroute_output(struct rtable **rp,
2177 struct fib_result* res,
2178 const struct flowi *fl,
2179 const struct flowi *oldflp,
2180 struct net_device *dev_out,
2183 struct rtable *rth = NULL;
2184 int err = __mkroute_output(&rth, res, fl, oldflp, dev_out, flags);
2187 hash = rt_hash(oldflp->fl4_dst, oldflp->fl4_src, oldflp->oif);
2188 err = rt_intern_hash(hash, rth, rp);
2195 * Major route resolver routine.
2198 static int ip_route_output_slow(struct rtable **rp, const struct flowi *oldflp)
2200 u32 tos = RT_FL_TOS(oldflp);
2201 struct flowi fl = { .nl_u = { .ip4_u =
2202 { .daddr = oldflp->fl4_dst,
2203 .saddr = oldflp->fl4_src,
2204 .tos = tos & IPTOS_RT_MASK,
2205 .scope = ((tos & RTO_ONLINK) ?
2209 .mark = oldflp->mark,
2210 .iif = init_net.loopback_dev->ifindex,
2211 .oif = oldflp->oif };
2212 struct fib_result res;
2214 struct net_device *dev_out = NULL;
2220 #ifdef CONFIG_IP_MULTIPLE_TABLES
2224 if (oldflp->fl4_src) {
2226 if (MULTICAST(oldflp->fl4_src) ||
2227 BADCLASS(oldflp->fl4_src) ||
2228 ZERONET(oldflp->fl4_src))
2231 /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
2232 dev_out = ip_dev_find(oldflp->fl4_src);
2233 if (dev_out == NULL)
2236 /* I removed check for oif == dev_out->oif here.
2237 It was wrong for two reasons:
2238 1. ip_dev_find(saddr) can return wrong iface, if saddr is
2239 assigned to multiple interfaces.
2240 2. Moreover, we are allowed to send packets with saddr
2241 of another iface. --ANK
2244 if (oldflp->oif == 0
2245 && (MULTICAST(oldflp->fl4_dst) || oldflp->fl4_dst == htonl(0xFFFFFFFF))) {
2246 /* Special hack: user can direct multicasts
2247 and limited broadcast via necessary interface
2248 without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
2249 This hack is not just for fun, it allows
2250 vic,vat and friends to work.
2251 They bind socket to loopback, set ttl to zero
2252 and expect that it will work.
2253 From the viewpoint of routing cache they are broken,
2254 because we are not allowed to build multicast path
2255 with loopback source addr (look, routing cache
2256 cannot know, that ttl is zero, so that packet
2257 will not leave this host and route is valid).
2258 Luckily, this hack is good workaround.
2261 fl.oif = dev_out->ifindex;
2271 dev_out = dev_get_by_index(&init_net, oldflp->oif);
2273 if (dev_out == NULL)
2276 /* RACE: Check return value of inet_select_addr instead. */
2277 if (__in_dev_get_rtnl(dev_out) == NULL) {
2279 goto out; /* Wrong error code */
2282 if (LOCAL_MCAST(oldflp->fl4_dst) || oldflp->fl4_dst == htonl(0xFFFFFFFF)) {
2284 fl.fl4_src = inet_select_addr(dev_out, 0,
2289 if (MULTICAST(oldflp->fl4_dst))
2290 fl.fl4_src = inet_select_addr(dev_out, 0,
2292 else if (!oldflp->fl4_dst)
2293 fl.fl4_src = inet_select_addr(dev_out, 0,
2299 fl.fl4_dst = fl.fl4_src;
2301 fl.fl4_dst = fl.fl4_src = htonl(INADDR_LOOPBACK);
2304 dev_out = init_net.loopback_dev;
2306 fl.oif = init_net.loopback_dev->ifindex;
2307 res.type = RTN_LOCAL;
2308 flags |= RTCF_LOCAL;
2312 if (fib_lookup(&fl, &res)) {
2315 /* Apparently, routing tables are wrong. Assume,
2316 that the destination is on link.
2319 Because we are allowed to send to iface
2320 even if it has NO routes and NO assigned
2321 addresses. When oif is specified, routing
2322 tables are looked up with only one purpose:
2323 to catch if destination is gatewayed, rather than
2324 direct. Moreover, if MSG_DONTROUTE is set,
2325 we send packet, ignoring both routing tables
2326 and ifaddr state. --ANK
2329 We could make it even if oif is unknown,
2330 likely IPv6, but we do not.
2333 if (fl.fl4_src == 0)
2334 fl.fl4_src = inet_select_addr(dev_out, 0,
2336 res.type = RTN_UNICAST;
2346 if (res.type == RTN_LOCAL) {
2348 fl.fl4_src = fl.fl4_dst;
2351 dev_out = init_net.loopback_dev;
2353 fl.oif = dev_out->ifindex;
2355 fib_info_put(res.fi);
2357 flags |= RTCF_LOCAL;
2361 #ifdef CONFIG_IP_ROUTE_MULTIPATH
2362 if (res.fi->fib_nhs > 1 && fl.oif == 0)
2363 fib_select_multipath(&fl, &res);
2366 if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
2367 fib_select_default(&fl, &res);
2370 fl.fl4_src = FIB_RES_PREFSRC(res);
2374 dev_out = FIB_RES_DEV(res);
2376 fl.oif = dev_out->ifindex;
2380 err = ip_mkroute_output(rp, &res, &fl, oldflp, dev_out, flags);
2390 int __ip_route_output_key(struct rtable **rp, const struct flowi *flp)
2395 hash = rt_hash(flp->fl4_dst, flp->fl4_src, flp->oif);
2398 for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
2399 rth = rcu_dereference(rth->u.dst.rt_next)) {
2400 if (rth->fl.fl4_dst == flp->fl4_dst &&
2401 rth->fl.fl4_src == flp->fl4_src &&
2403 rth->fl.oif == flp->oif &&
2404 rth->fl.mark == flp->mark &&
2405 !((rth->fl.fl4_tos ^ flp->fl4_tos) &
2406 (IPTOS_RT_MASK | RTO_ONLINK))) {
2407 dst_use(&rth->u.dst, jiffies);
2408 RT_CACHE_STAT_INC(out_hit);
2409 rcu_read_unlock_bh();
2413 RT_CACHE_STAT_INC(out_hlist_search);
2415 rcu_read_unlock_bh();
2417 return ip_route_output_slow(rp, flp);
2420 EXPORT_SYMBOL_GPL(__ip_route_output_key);
2422 static void ipv4_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
2426 static struct dst_ops ipv4_dst_blackhole_ops = {
2428 .protocol = __constant_htons(ETH_P_IP),
2429 .destroy = ipv4_dst_destroy,
2430 .check = ipv4_dst_check,
2431 .update_pmtu = ipv4_rt_blackhole_update_pmtu,
2432 .entry_size = sizeof(struct rtable),
2436 static int ipv4_dst_blackhole(struct rtable **rp, struct flowi *flp, struct sock *sk)
2438 struct rtable *ort = *rp;
2439 struct rtable *rt = (struct rtable *)
2440 dst_alloc(&ipv4_dst_blackhole_ops);
2443 struct dst_entry *new = &rt->u.dst;
2445 atomic_set(&new->__refcnt, 1);
2447 new->input = dst_discard;
2448 new->output = dst_discard;
2449 memcpy(new->metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32));
2451 new->dev = ort->u.dst.dev;
2457 rt->idev = ort->idev;
2459 in_dev_hold(rt->idev);
2460 rt->rt_flags = ort->rt_flags;
2461 rt->rt_type = ort->rt_type;
2462 rt->rt_dst = ort->rt_dst;
2463 rt->rt_src = ort->rt_src;
2464 rt->rt_iif = ort->rt_iif;
2465 rt->rt_gateway = ort->rt_gateway;
2466 rt->rt_spec_dst = ort->rt_spec_dst;
2467 rt->peer = ort->peer;
2469 atomic_inc(&rt->peer->refcnt);
2474 dst_release(&(*rp)->u.dst);
2476 return (rt ? 0 : -ENOMEM);
2479 int ip_route_output_flow(struct rtable **rp, struct flowi *flp, struct sock *sk, int flags)
2483 if ((err = __ip_route_output_key(rp, flp)) != 0)
2488 flp->fl4_src = (*rp)->rt_src;
2490 flp->fl4_dst = (*rp)->rt_dst;
2491 err = __xfrm_lookup((struct dst_entry **)rp, flp, sk, flags);
2492 if (err == -EREMOTE)
2493 err = ipv4_dst_blackhole(rp, flp, sk);
2501 EXPORT_SYMBOL_GPL(ip_route_output_flow);
2503 int ip_route_output_key(struct rtable **rp, struct flowi *flp)
2505 return ip_route_output_flow(rp, flp, NULL, 0);
2508 static int rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
2509 int nowait, unsigned int flags)
2511 struct rtable *rt = (struct rtable*)skb->dst;
2513 struct nlmsghdr *nlh;
2515 u32 id = 0, ts = 0, tsage = 0, error;
2517 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags);
2521 r = nlmsg_data(nlh);
2522 r->rtm_family = AF_INET;
2523 r->rtm_dst_len = 32;
2525 r->rtm_tos = rt->fl.fl4_tos;
2526 r->rtm_table = RT_TABLE_MAIN;
2527 NLA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
2528 r->rtm_type = rt->rt_type;
2529 r->rtm_scope = RT_SCOPE_UNIVERSE;
2530 r->rtm_protocol = RTPROT_UNSPEC;
2531 r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
2532 if (rt->rt_flags & RTCF_NOTIFY)
2533 r->rtm_flags |= RTM_F_NOTIFY;
2535 NLA_PUT_BE32(skb, RTA_DST, rt->rt_dst);
2537 if (rt->fl.fl4_src) {
2538 r->rtm_src_len = 32;
2539 NLA_PUT_BE32(skb, RTA_SRC, rt->fl.fl4_src);
2542 NLA_PUT_U32(skb, RTA_OIF, rt->u.dst.dev->ifindex);
2543 #ifdef CONFIG_NET_CLS_ROUTE
2544 if (rt->u.dst.tclassid)
2545 NLA_PUT_U32(skb, RTA_FLOW, rt->u.dst.tclassid);
2548 NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_spec_dst);
2549 else if (rt->rt_src != rt->fl.fl4_src)
2550 NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_src);
2552 if (rt->rt_dst != rt->rt_gateway)
2553 NLA_PUT_BE32(skb, RTA_GATEWAY, rt->rt_gateway);
2555 if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0)
2556 goto nla_put_failure;
2558 error = rt->u.dst.error;
2559 expires = rt->u.dst.expires ? rt->u.dst.expires - jiffies : 0;
2561 id = rt->peer->ip_id_count;
2562 if (rt->peer->tcp_ts_stamp) {
2563 ts = rt->peer->tcp_ts;
2564 tsage = get_seconds() - rt->peer->tcp_ts_stamp;
2569 #ifdef CONFIG_IP_MROUTE
2570 __be32 dst = rt->rt_dst;
2572 if (MULTICAST(dst) && !LOCAL_MCAST(dst) &&
2573 IPV4_DEVCONF_ALL(MC_FORWARDING)) {
2574 int err = ipmr_get_route(skb, r, nowait);
2579 goto nla_put_failure;
2581 if (err == -EMSGSIZE)
2582 goto nla_put_failure;
2588 NLA_PUT_U32(skb, RTA_IIF, rt->fl.iif);
2591 if (rtnl_put_cacheinfo(skb, &rt->u.dst, id, ts, tsage,
2592 expires, error) < 0)
2593 goto nla_put_failure;
2595 return nlmsg_end(skb, nlh);
2598 nlmsg_cancel(skb, nlh);
2602 static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
2604 struct net *net = in_skb->sk->sk_net;
2606 struct nlattr *tb[RTA_MAX+1];
2607 struct rtable *rt = NULL;
2612 struct sk_buff *skb;
2614 if (net != &init_net)
2617 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy);
2621 rtm = nlmsg_data(nlh);
2623 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2629 /* Reserve room for dummy headers, this skb can pass
2630 through good chunk of routing engine.
2632 skb_reset_mac_header(skb);
2633 skb_reset_network_header(skb);
2635 /* Bugfix: need to give ip_route_input enough of an IP header to not gag. */
2636 ip_hdr(skb)->protocol = IPPROTO_ICMP;
2637 skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr));
2639 src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0;
2640 dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0;
2641 iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
2644 struct net_device *dev;
2646 dev = __dev_get_by_index(&init_net, iif);
2652 skb->protocol = htons(ETH_P_IP);
2655 err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev);
2658 rt = (struct rtable*) skb->dst;
2659 if (err == 0 && rt->u.dst.error)
2660 err = -rt->u.dst.error;
2667 .tos = rtm->rtm_tos,
2670 .oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0,
2672 err = ip_route_output_key(&rt, &fl);
2678 skb->dst = &rt->u.dst;
2679 if (rtm->rtm_flags & RTM_F_NOTIFY)
2680 rt->rt_flags |= RTCF_NOTIFY;
2682 err = rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
2683 RTM_NEWROUTE, 0, 0);
2687 err = rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
2696 int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb)
2705 s_idx = idx = cb->args[1];
2706 for (h = s_h; h <= rt_hash_mask; h++) {
2708 for (rt = rcu_dereference(rt_hash_table[h].chain), idx = 0; rt;
2709 rt = rcu_dereference(rt->u.dst.rt_next), idx++) {
2712 skb->dst = dst_clone(&rt->u.dst);
2713 if (rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
2714 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
2715 1, NLM_F_MULTI) <= 0) {
2716 dst_release(xchg(&skb->dst, NULL));
2717 rcu_read_unlock_bh();
2720 dst_release(xchg(&skb->dst, NULL));
2722 rcu_read_unlock_bh();
2732 void ip_rt_multicast_event(struct in_device *in_dev)
2737 #ifdef CONFIG_SYSCTL
2738 static int flush_delay;
2740 static int ipv4_sysctl_rtcache_flush(ctl_table *ctl, int write,
2741 struct file *filp, void __user *buffer,
2742 size_t *lenp, loff_t *ppos)
2745 proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
2746 rt_cache_flush(flush_delay);
2753 static int ipv4_sysctl_rtcache_flush_strategy(ctl_table *table,
2756 void __user *oldval,
2757 size_t __user *oldlenp,
2758 void __user *newval,
2762 if (newlen != sizeof(int))
2764 if (get_user(delay, (int __user *)newval))
2766 rt_cache_flush(delay);
2770 ctl_table ipv4_route_table[] = {
2772 .ctl_name = NET_IPV4_ROUTE_FLUSH,
2773 .procname = "flush",
2774 .data = &flush_delay,
2775 .maxlen = sizeof(int),
2777 .proc_handler = &ipv4_sysctl_rtcache_flush,
2778 .strategy = &ipv4_sysctl_rtcache_flush_strategy,
2781 .ctl_name = NET_IPV4_ROUTE_MIN_DELAY,
2782 .procname = "min_delay",
2783 .data = &ip_rt_min_delay,
2784 .maxlen = sizeof(int),
2786 .proc_handler = &proc_dointvec_jiffies,
2787 .strategy = &sysctl_jiffies,
2790 .ctl_name = NET_IPV4_ROUTE_MAX_DELAY,
2791 .procname = "max_delay",
2792 .data = &ip_rt_max_delay,
2793 .maxlen = sizeof(int),
2795 .proc_handler = &proc_dointvec_jiffies,
2796 .strategy = &sysctl_jiffies,
2799 .ctl_name = NET_IPV4_ROUTE_GC_THRESH,
2800 .procname = "gc_thresh",
2801 .data = &ipv4_dst_ops.gc_thresh,
2802 .maxlen = sizeof(int),
2804 .proc_handler = &proc_dointvec,
2807 .ctl_name = NET_IPV4_ROUTE_MAX_SIZE,
2808 .procname = "max_size",
2809 .data = &ip_rt_max_size,
2810 .maxlen = sizeof(int),
2812 .proc_handler = &proc_dointvec,
2815 /* Deprecated. Use gc_min_interval_ms */
2817 .ctl_name = NET_IPV4_ROUTE_GC_MIN_INTERVAL,
2818 .procname = "gc_min_interval",
2819 .data = &ip_rt_gc_min_interval,
2820 .maxlen = sizeof(int),
2822 .proc_handler = &proc_dointvec_jiffies,
2823 .strategy = &sysctl_jiffies,
2826 .ctl_name = NET_IPV4_ROUTE_GC_MIN_INTERVAL_MS,
2827 .procname = "gc_min_interval_ms",
2828 .data = &ip_rt_gc_min_interval,
2829 .maxlen = sizeof(int),
2831 .proc_handler = &proc_dointvec_ms_jiffies,
2832 .strategy = &sysctl_ms_jiffies,
2835 .ctl_name = NET_IPV4_ROUTE_GC_TIMEOUT,
2836 .procname = "gc_timeout",
2837 .data = &ip_rt_gc_timeout,
2838 .maxlen = sizeof(int),
2840 .proc_handler = &proc_dointvec_jiffies,
2841 .strategy = &sysctl_jiffies,
2844 .ctl_name = NET_IPV4_ROUTE_GC_INTERVAL,
2845 .procname = "gc_interval",
2846 .data = &ip_rt_gc_interval,
2847 .maxlen = sizeof(int),
2849 .proc_handler = &proc_dointvec_jiffies,
2850 .strategy = &sysctl_jiffies,
2853 .ctl_name = NET_IPV4_ROUTE_REDIRECT_LOAD,
2854 .procname = "redirect_load",
2855 .data = &ip_rt_redirect_load,
2856 .maxlen = sizeof(int),
2858 .proc_handler = &proc_dointvec,
2861 .ctl_name = NET_IPV4_ROUTE_REDIRECT_NUMBER,
2862 .procname = "redirect_number",
2863 .data = &ip_rt_redirect_number,
2864 .maxlen = sizeof(int),
2866 .proc_handler = &proc_dointvec,
2869 .ctl_name = NET_IPV4_ROUTE_REDIRECT_SILENCE,
2870 .procname = "redirect_silence",
2871 .data = &ip_rt_redirect_silence,
2872 .maxlen = sizeof(int),
2874 .proc_handler = &proc_dointvec,
2877 .ctl_name = NET_IPV4_ROUTE_ERROR_COST,
2878 .procname = "error_cost",
2879 .data = &ip_rt_error_cost,
2880 .maxlen = sizeof(int),
2882 .proc_handler = &proc_dointvec,
2885 .ctl_name = NET_IPV4_ROUTE_ERROR_BURST,
2886 .procname = "error_burst",
2887 .data = &ip_rt_error_burst,
2888 .maxlen = sizeof(int),
2890 .proc_handler = &proc_dointvec,
2893 .ctl_name = NET_IPV4_ROUTE_GC_ELASTICITY,
2894 .procname = "gc_elasticity",
2895 .data = &ip_rt_gc_elasticity,
2896 .maxlen = sizeof(int),
2898 .proc_handler = &proc_dointvec,
2901 .ctl_name = NET_IPV4_ROUTE_MTU_EXPIRES,
2902 .procname = "mtu_expires",
2903 .data = &ip_rt_mtu_expires,
2904 .maxlen = sizeof(int),
2906 .proc_handler = &proc_dointvec_jiffies,
2907 .strategy = &sysctl_jiffies,
2910 .ctl_name = NET_IPV4_ROUTE_MIN_PMTU,
2911 .procname = "min_pmtu",
2912 .data = &ip_rt_min_pmtu,
2913 .maxlen = sizeof(int),
2915 .proc_handler = &proc_dointvec,
2918 .ctl_name = NET_IPV4_ROUTE_MIN_ADVMSS,
2919 .procname = "min_adv_mss",
2920 .data = &ip_rt_min_advmss,
2921 .maxlen = sizeof(int),
2923 .proc_handler = &proc_dointvec,
2926 .ctl_name = NET_IPV4_ROUTE_SECRET_INTERVAL,
2927 .procname = "secret_interval",
2928 .data = &ip_rt_secret_interval,
2929 .maxlen = sizeof(int),
2931 .proc_handler = &proc_dointvec_jiffies,
2932 .strategy = &sysctl_jiffies,
2938 #ifdef CONFIG_NET_CLS_ROUTE
2939 struct ip_rt_acct *ip_rt_acct __read_mostly;
2940 #endif /* CONFIG_NET_CLS_ROUTE */
2942 static __initdata unsigned long rhash_entries;
2943 static int __init set_rhash_entries(char *str)
2947 rhash_entries = simple_strtoul(str, &str, 0);
2950 __setup("rhash_entries=", set_rhash_entries);
2952 int __init ip_rt_init(void)
2956 rt_hash_rnd = (int) ((num_physpages ^ (num_physpages>>8)) ^
2957 (jiffies ^ (jiffies >> 7)));
2959 #ifdef CONFIG_NET_CLS_ROUTE
2960 ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct));
2962 panic("IP: failed to allocate ip_rt_acct\n");
2965 ipv4_dst_ops.kmem_cachep =
2966 kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
2967 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2969 ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
2971 rt_hash_table = (struct rt_hash_bucket *)
2972 alloc_large_system_hash("IP route cache",
2973 sizeof(struct rt_hash_bucket),
2975 (num_physpages >= 128 * 1024) ?
2981 memset(rt_hash_table, 0, (rt_hash_mask + 1) * sizeof(struct rt_hash_bucket));
2982 rt_hash_lock_init();
2984 ipv4_dst_ops.gc_thresh = (rt_hash_mask + 1);
2985 ip_rt_max_size = (rt_hash_mask + 1) * 16;
2990 setup_timer(&rt_flush_timer, rt_run_flush, 0);
2991 setup_timer(&rt_secret_timer, rt_secret_rebuild, 0);
2993 /* All the timers, started at system startup tend
2994 to synchronize. Perturb it a bit.
2996 schedule_delayed_work(&expires_work,
2997 net_random() % ip_rt_gc_interval + ip_rt_gc_interval);
2999 rt_secret_timer.expires = jiffies + net_random() % ip_rt_secret_interval +
3000 ip_rt_secret_interval;
3001 add_timer(&rt_secret_timer);
3003 #ifdef CONFIG_PROC_FS
3005 struct proc_dir_entry *rtstat_pde = NULL; /* keep gcc happy */
3006 if (!proc_net_fops_create(&init_net, "rt_cache", S_IRUGO, &rt_cache_seq_fops) ||
3007 !(rtstat_pde = create_proc_entry("rt_cache", S_IRUGO,
3008 init_net.proc_net_stat))) {
3011 rtstat_pde->proc_fops = &rt_cpu_seq_fops;
3013 #ifdef CONFIG_NET_CLS_ROUTE
3014 create_proc_read_entry("rt_acct", 0, init_net.proc_net, ip_rt_acct_read, NULL);
3021 rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL);
3026 EXPORT_SYMBOL(__ip_select_ident);
3027 EXPORT_SYMBOL(ip_route_input);
3028 EXPORT_SYMBOL(ip_route_output_key);