blob: 5c774c14dcdff6d6b17dc23319f43b95a95aa052 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
5 *
6 * ROUTE - implementation of the IP router.
7 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Alan Cox, <gw4pts@gw4pts.ampr.org>
11 * Linus Torvalds, <Linus.Torvalds@helsinki.fi>
12 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13 *
14 * Fixes:
15 * Alan Cox : Verify area fixes.
16 * Alan Cox : cli() protects routing changes
17 * Rui Oliveira : ICMP routing table updates
18 * (rco@di.uminho.pt) Routing table insertion and update
19 * Linus Torvalds : Rewrote bits to be sensible
20 * Alan Cox : Added BSD route gw semantics
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090021 * Alan Cox : Super /proc >4K
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * Alan Cox : MTU in route table
23 * Alan Cox : MSS actually. Also added the window
24 * clamper.
25 * Sam Lantinga : Fixed route matching in rt_del()
26 * Alan Cox : Routing cache support.
27 * Alan Cox : Removed compatibility cruft.
28 * Alan Cox : RTF_REJECT support.
29 * Alan Cox : TCP irtt support.
30 * Jonathan Naylor : Added Metric support.
31 * Miquel van Smoorenburg : BSD API fixes.
32 * Miquel van Smoorenburg : Metrics.
33 * Alan Cox : Use __u32 properly
34 * Alan Cox : Aligned routing errors more closely with BSD
35 * our system is still very different.
36 * Alan Cox : Faster /proc handling
37 * Alexey Kuznetsov : Massive rework to support tree based routing,
38 * routing caches and better behaviour.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090039 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * Olaf Erb : irtt wasn't being copied right.
41 * Bjorn Ekwall : Kerneld route support.
42 * Alan Cox : Multicast fixed (I hope)
43 * Pavel Krauz : Limited broadcast fixed
44 * Mike McLagan : Routing by source
45 * Alexey Kuznetsov : End of old history. Split to fib.c and
46 * route.c and rewritten from scratch.
47 * Andi Kleen : Load-limit warning messages.
48 * Vitaly E. Lavrov : Transparent proxy revived after year coma.
49 * Vitaly E. Lavrov : Race condition in ip_route_input_slow.
50 * Tobias Ringstrom : Uninitialized res.type in ip_route_output_slow.
51 * Vladimir V. Ivanov : IP rule info (flowid) is really useful.
52 * Marc Boucher : routing by fwmark
53 * Robert Olsson : Added rt_cache statistics
54 * Arnaldo C. Melo : Convert proc stuff to seq_file
Eric Dumazetbb1d23b2005-07-05 15:00:32 -070055 * Eric Dumazet : hashed spinlocks and rt_check_expire() fixes.
Ilia Sotnikovcef26852006-03-25 01:38:55 -080056 * Ilia Sotnikov : Ignore TOS on PMTUD and Redirect
57 * Ilia Sotnikov : Removed TOS from hash calculations
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 *
59 * This program is free software; you can redistribute it and/or
60 * modify it under the terms of the GNU General Public License
61 * as published by the Free Software Foundation; either version
62 * 2 of the License, or (at your option) any later version.
63 */
64
Joe Perchesafd465032012-03-12 07:03:32 +000065#define pr_fmt(fmt) "IPv4: " fmt
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/module.h>
68#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/bitops.h>
70#include <linux/types.h>
71#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mm.h>
Eric Dumazet424c4b72005-07-05 14:58:19 -070073#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/string.h>
75#include <linux/socket.h>
76#include <linux/sockios.h>
77#include <linux/errno.h>
78#include <linux/in.h>
79#include <linux/inet.h>
80#include <linux/netdevice.h>
81#include <linux/proc_fs.h>
82#include <linux/init.h>
Eric Dumazet39c90ec2007-09-15 10:55:54 -070083#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/inetdevice.h>
86#include <linux/igmp.h>
87#include <linux/pkt_sched.h>
88#include <linux/mroute.h>
89#include <linux/netfilter_ipv4.h>
90#include <linux/random.h>
91#include <linux/jhash.h>
92#include <linux/rcupdate.h>
93#include <linux/times.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090094#include <linux/slab.h>
Stephen Rothwellb9eda062011-12-22 17:03:29 +110095#include <linux/prefetch.h>
Herbert Xu352e5122007-11-13 21:34:06 -080096#include <net/dst.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020097#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <net/protocol.h>
99#include <net/ip.h>
100#include <net/route.h>
101#include <net/inetpeer.h>
102#include <net/sock.h>
103#include <net/ip_fib.h>
104#include <net/arp.h>
105#include <net/tcp.h>
106#include <net/icmp.h>
107#include <net/xfrm.h>
Tom Tucker8d717402006-07-30 20:43:36 -0700108#include <net/netevent.h>
Thomas Graf63f34442007-03-22 11:55:17 -0700109#include <net/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#ifdef CONFIG_SYSCTL
111#include <linux/sysctl.h>
112#endif
David S. Miller6e5714e2011-08-03 20:50:44 -0700113#include <net/secure_seq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
David S. Miller68a5e3d2011-03-11 20:07:33 -0500115#define RT_FL_TOS(oldflp4) \
Julian Anastasovf61759e2011-12-02 11:39:42 +0000116 ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118#define IP_MAX_MTU 0xFFF0
119
120#define RT_GC_TIMEOUT (300*HZ)
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static int ip_rt_max_size;
Stephen Hemminger817bc4d2008-03-22 17:43:59 -0700123static int ip_rt_gc_timeout __read_mostly = RT_GC_TIMEOUT;
Eric Dumazet9f28a2f2011-12-21 15:47:16 -0500124static int ip_rt_gc_interval __read_mostly = 60 * HZ;
Stephen Hemminger817bc4d2008-03-22 17:43:59 -0700125static int ip_rt_gc_min_interval __read_mostly = HZ / 2;
126static int ip_rt_redirect_number __read_mostly = 9;
127static int ip_rt_redirect_load __read_mostly = HZ / 50;
128static int ip_rt_redirect_silence __read_mostly = ((HZ / 50) << (9 + 1));
129static int ip_rt_error_cost __read_mostly = HZ;
130static int ip_rt_error_burst __read_mostly = 5 * HZ;
131static int ip_rt_gc_elasticity __read_mostly = 8;
132static int ip_rt_mtu_expires __read_mostly = 10 * 60 * HZ;
133static int ip_rt_min_pmtu __read_mostly = 512 + 20 + 20;
134static int ip_rt_min_advmss __read_mostly = 256;
Neil Horman1080d702008-10-27 12:28:25 -0700135static int rt_chain_length_max __read_mostly = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Eric Dumazet9f28a2f2011-12-21 15:47:16 -0500137static struct delayed_work expires_work;
138static unsigned long expires_ljiffies;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
141 * Interface to generic destination cache.
142 */
143
144static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie);
David S. Miller0dbaee32010-12-13 12:52:14 -0800145static unsigned int ipv4_default_advmss(const struct dst_entry *dst);
Steffen Klassertebb762f2011-11-23 02:12:51 +0000146static unsigned int ipv4_mtu(const struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static void ipv4_dst_destroy(struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst);
149static void ipv4_link_failure(struct sk_buff *skb);
150static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
Daniel Lezcano569d3642008-01-18 03:56:57 -0800151static int rt_garbage_collect(struct dst_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Marcelo Ricardo Leitnerb6153ea2014-08-14 16:44:52 -0300153static void __rt_garbage_collect(struct work_struct *w);
154static DECLARE_WORK(rt_gc_worker, __rt_garbage_collect);
155
Eric Dumazet72cdd1d2010-11-11 07:14:07 +0000156static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
157 int how)
158{
159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
David S. Miller62fa8a82011-01-26 20:51:05 -0800161static u32 *ipv4_cow_metrics(struct dst_entry *dst, unsigned long old)
162{
David S. Miller06582542011-01-27 14:58:42 -0800163 struct rtable *rt = (struct rtable *) dst;
164 struct inet_peer *peer;
165 u32 *p = NULL;
David S. Miller62fa8a82011-01-26 20:51:05 -0800166
David S. Miller06582542011-01-27 14:58:42 -0800167 if (!rt->peer)
David S. Millera48eff12011-05-18 18:42:43 -0400168 rt_bind_peer(rt, rt->rt_dst, 1);
David S. Miller06582542011-01-27 14:58:42 -0800169
170 peer = rt->peer;
171 if (peer) {
David S. Miller62fa8a82011-01-26 20:51:05 -0800172 u32 *old_p = __DST_METRICS_PTR(old);
173 unsigned long prev, new;
174
David S. Miller06582542011-01-27 14:58:42 -0800175 p = peer->metrics;
176 if (inet_metrics_new(peer))
177 memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
David S. Miller62fa8a82011-01-26 20:51:05 -0800178
179 new = (unsigned long) p;
180 prev = cmpxchg(&dst->_metrics, old, new);
181
182 if (prev != old) {
David S. Miller62fa8a82011-01-26 20:51:05 -0800183 p = __DST_METRICS_PTR(prev);
184 if (prev & DST_METRICS_READ_ONLY)
185 p = NULL;
186 } else {
David S. Miller62fa8a82011-01-26 20:51:05 -0800187 if (rt->fi) {
188 fib_info_put(rt->fi);
189 rt->fi = NULL;
190 }
191 }
192 }
193 return p;
194}
195
David S. Millerd3aaeb32011-07-18 00:40:17 -0700196static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst, const void *daddr);
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static struct dst_ops ipv4_dst_ops = {
199 .family = AF_INET,
Harvey Harrison09640e62009-02-01 00:45:17 -0800200 .protocol = cpu_to_be16(ETH_P_IP),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .gc = rt_garbage_collect,
202 .check = ipv4_dst_check,
David S. Miller0dbaee32010-12-13 12:52:14 -0800203 .default_advmss = ipv4_default_advmss,
Steffen Klassertebb762f2011-11-23 02:12:51 +0000204 .mtu = ipv4_mtu,
David S. Miller62fa8a82011-01-26 20:51:05 -0800205 .cow_metrics = ipv4_cow_metrics,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 .destroy = ipv4_dst_destroy,
207 .ifdown = ipv4_dst_ifdown,
208 .negative_advice = ipv4_negative_advice,
209 .link_failure = ipv4_link_failure,
210 .update_pmtu = ip_rt_update_pmtu,
Herbert Xu1ac06e02008-05-20 14:32:14 -0700211 .local_out = __ip_local_out,
David S. Millerd3aaeb32011-07-18 00:40:17 -0700212 .neigh_lookup = ipv4_neigh_lookup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
215#define ECN_OR_COST(class) TC_PRIO_##class
216
Philippe De Muyter4839c522007-07-09 15:32:57 -0700217const __u8 ip_tos2prio[16] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 TC_PRIO_BESTEFFORT,
Dan Siemon4a2b9c32011-03-15 13:56:07 +0000219 ECN_OR_COST(BESTEFFORT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 TC_PRIO_BESTEFFORT,
221 ECN_OR_COST(BESTEFFORT),
222 TC_PRIO_BULK,
223 ECN_OR_COST(BULK),
224 TC_PRIO_BULK,
225 ECN_OR_COST(BULK),
226 TC_PRIO_INTERACTIVE,
227 ECN_OR_COST(INTERACTIVE),
228 TC_PRIO_INTERACTIVE,
229 ECN_OR_COST(INTERACTIVE),
230 TC_PRIO_INTERACTIVE_BULK,
231 ECN_OR_COST(INTERACTIVE_BULK),
232 TC_PRIO_INTERACTIVE_BULK,
233 ECN_OR_COST(INTERACTIVE_BULK)
234};
235
236
237/*
238 * Route cache.
239 */
240
241/* The locking scheme is rather straight forward:
242 *
243 * 1) Read-Copy Update protects the buckets of the central route hash.
244 * 2) Only writers remove entries, and they hold the lock
245 * as they look at rtable reference counts.
246 * 3) Only readers acquire references to rtable entries,
247 * they do so with atomic increments and with the
248 * lock held.
249 */
250
251struct rt_hash_bucket {
Eric Dumazet1c317202010-10-25 21:02:07 +0000252 struct rtable __rcu *chain;
Eric Dumazet22c047c2005-07-05 14:55:24 -0700253};
Neil Horman1080d702008-10-27 12:28:25 -0700254
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700255#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
256 defined(CONFIG_PROVE_LOCKING)
Eric Dumazet22c047c2005-07-05 14:55:24 -0700257/*
258 * Instead of using one spinlock for each rt_hash_bucket, we use a table of spinlocks
259 * The size of this table is a power of two and depends on the number of CPUS.
Ingo Molnar62051202006-07-03 00:24:59 -0700260 * (on lockdep we have a quite big spinlock_t, so keep the size down there)
Eric Dumazet22c047c2005-07-05 14:55:24 -0700261 */
Ingo Molnar62051202006-07-03 00:24:59 -0700262#ifdef CONFIG_LOCKDEP
263# define RT_HASH_LOCK_SZ 256
Eric Dumazet22c047c2005-07-05 14:55:24 -0700264#else
Ingo Molnar62051202006-07-03 00:24:59 -0700265# if NR_CPUS >= 32
266# define RT_HASH_LOCK_SZ 4096
267# elif NR_CPUS >= 16
268# define RT_HASH_LOCK_SZ 2048
269# elif NR_CPUS >= 8
270# define RT_HASH_LOCK_SZ 1024
271# elif NR_CPUS >= 4
272# define RT_HASH_LOCK_SZ 512
273# else
274# define RT_HASH_LOCK_SZ 256
275# endif
Eric Dumazet22c047c2005-07-05 14:55:24 -0700276#endif
277
278static spinlock_t *rt_hash_locks;
279# define rt_hash_lock_addr(slot) &rt_hash_locks[(slot) & (RT_HASH_LOCK_SZ - 1)]
Pavel Emelyanov1ff1cc22007-12-05 21:15:05 -0800280
281static __init void rt_hash_lock_init(void)
282{
283 int i;
284
285 rt_hash_locks = kmalloc(sizeof(spinlock_t) * RT_HASH_LOCK_SZ,
286 GFP_KERNEL);
287 if (!rt_hash_locks)
288 panic("IP: failed to allocate rt_hash_locks\n");
289
290 for (i = 0; i < RT_HASH_LOCK_SZ; i++)
291 spin_lock_init(&rt_hash_locks[i]);
292}
Eric Dumazet22c047c2005-07-05 14:55:24 -0700293#else
294# define rt_hash_lock_addr(slot) NULL
Pavel Emelyanov1ff1cc22007-12-05 21:15:05 -0800295
296static inline void rt_hash_lock_init(void)
297{
298}
Eric Dumazet22c047c2005-07-05 14:55:24 -0700299#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Stephen Hemminger817bc4d2008-03-22 17:43:59 -0700301static struct rt_hash_bucket *rt_hash_table __read_mostly;
302static unsigned rt_hash_mask __read_mostly;
303static unsigned int rt_hash_log __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Eric Dumazet2f970d82006-01-17 02:54:36 -0800305static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
Eric Dumazet27f39c72010-05-19 22:07:23 +0000306#define RT_CACHE_STAT_INC(field) __this_cpu_inc(rt_cache_stat.field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Denis V. Lunevb00180d2008-07-05 19:04:09 -0700308static inline unsigned int rt_hash(__be32 daddr, __be32 saddr, int idx,
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700309 int genid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700311 return jhash_3words((__force u32)daddr, (__force u32)saddr,
Denis V. Lunevb00180d2008-07-05 19:04:09 -0700312 idx, genid)
Eric Dumazet29e75252008-01-31 17:05:09 -0800313 & rt_hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Denis V. Luneve84f84f2008-07-05 19:04:32 -0700316static inline int rt_genid(struct net *net)
317{
318 return atomic_read(&net->ipv4.rt_genid);
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321#ifdef CONFIG_PROC_FS
322struct rt_cache_iter_state {
Denis V. Luneva75e9362008-02-28 20:50:55 -0800323 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 int bucket;
Eric Dumazet29e75252008-01-31 17:05:09 -0800325 int genid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326};
327
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900328static struct rtable *rt_cache_get_first(struct seq_file *seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900330 struct rt_cache_iter_state *st = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct rtable *r = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
Eric Dumazet33d480c2011-08-11 19:30:52 +0000334 if (!rcu_access_pointer(rt_hash_table[st->bucket].chain))
Eric Dumazeta6272662008-08-28 01:11:25 -0700335 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -0800337 r = rcu_dereference_bh(rt_hash_table[st->bucket].chain);
Eric Dumazet29e75252008-01-31 17:05:09 -0800338 while (r) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700339 if (dev_net(r->dst.dev) == seq_file_net(seq) &&
Denis V. Luneva75e9362008-02-28 20:50:55 -0800340 r->rt_genid == st->genid)
Eric Dumazet29e75252008-01-31 17:05:09 -0800341 return r;
Changli Gaod8d1f302010-06-10 23:31:35 -0700342 r = rcu_dereference_bh(r->dst.rt_next);
Eric Dumazet29e75252008-01-31 17:05:09 -0800343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 rcu_read_unlock_bh();
345 }
Eric Dumazet29e75252008-01-31 17:05:09 -0800346 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900349static struct rtable *__rt_cache_get_next(struct seq_file *seq,
Denis V. Lunev642d6312008-02-28 20:50:33 -0800350 struct rtable *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900352 struct rt_cache_iter_state *st = seq->private;
Eric Dumazeta6272662008-08-28 01:11:25 -0700353
Eric Dumazet1c317202010-10-25 21:02:07 +0000354 r = rcu_dereference_bh(r->dst.rt_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 while (!r) {
356 rcu_read_unlock_bh();
Eric Dumazeta6272662008-08-28 01:11:25 -0700357 do {
358 if (--st->bucket < 0)
359 return NULL;
Eric Dumazet33d480c2011-08-11 19:30:52 +0000360 } while (!rcu_access_pointer(rt_hash_table[st->bucket].chain));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 rcu_read_lock_bh();
Eric Dumazet1c317202010-10-25 21:02:07 +0000362 r = rcu_dereference_bh(rt_hash_table[st->bucket].chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Eric Dumazet1c317202010-10-25 21:02:07 +0000364 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900367static struct rtable *rt_cache_get_next(struct seq_file *seq,
Denis V. Lunev642d6312008-02-28 20:50:33 -0800368 struct rtable *r)
369{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900370 struct rt_cache_iter_state *st = seq->private;
371 while ((r = __rt_cache_get_next(seq, r)) != NULL) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700372 if (dev_net(r->dst.dev) != seq_file_net(seq))
Denis V. Luneva75e9362008-02-28 20:50:55 -0800373 continue;
Denis V. Lunev642d6312008-02-28 20:50:33 -0800374 if (r->rt_genid == st->genid)
375 break;
376 }
377 return r;
378}
379
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900380static struct rtable *rt_cache_get_idx(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900382 struct rtable *r = rt_cache_get_first(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (r)
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900385 while (pos && (r = rt_cache_get_next(seq, r)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 --pos;
387 return pos ? NULL : r;
388}
389
390static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
391{
Eric Dumazet29e75252008-01-31 17:05:09 -0800392 struct rt_cache_iter_state *st = seq->private;
Eric Dumazet29e75252008-01-31 17:05:09 -0800393 if (*pos)
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900394 return rt_cache_get_idx(seq, *pos - 1);
Denis V. Luneve84f84f2008-07-05 19:04:32 -0700395 st->genid = rt_genid(seq_file_net(seq));
Eric Dumazet29e75252008-01-31 17:05:09 -0800396 return SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
400{
Eric Dumazet29e75252008-01-31 17:05:09 -0800401 struct rtable *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900404 r = rt_cache_get_first(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 else
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900406 r = rt_cache_get_next(seq, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 ++*pos;
408 return r;
409}
410
411static void rt_cache_seq_stop(struct seq_file *seq, void *v)
412{
413 if (v && v != SEQ_START_TOKEN)
414 rcu_read_unlock_bh();
415}
416
417static int rt_cache_seq_show(struct seq_file *seq, void *v)
418{
419 if (v == SEQ_START_TOKEN)
420 seq_printf(seq, "%-127s\n",
421 "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
422 "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
423 "HHUptod\tSpecDst");
424 else {
425 struct rtable *r = v;
David S. Miller69cce1d2011-07-17 23:09:49 -0700426 struct neighbour *n;
Eric Dumazet218fa902011-11-29 20:05:55 +0000427 int len, HHUptod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Eric Dumazet218fa902011-11-29 20:05:55 +0000429 rcu_read_lock();
David Miller27217452011-12-02 16:52:08 +0000430 n = dst_get_neighbour_noref(&r->dst);
Eric Dumazet218fa902011-11-29 20:05:55 +0000431 HHUptod = (n && (n->nud_state & NUD_CONNECTED)) ? 1 : 0;
432 rcu_read_unlock();
433
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700434 seq_printf(seq, "%s\t%08X\t%08X\t%8X\t%d\t%u\t%d\t"
435 "%08X\t%d\t%u\t%u\t%02X\t%d\t%1d\t%08X%n",
Changli Gaod8d1f302010-06-10 23:31:35 -0700436 r->dst.dev ? r->dst.dev->name : "*",
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700437 (__force u32)r->rt_dst,
438 (__force u32)r->rt_gateway,
Changli Gaod8d1f302010-06-10 23:31:35 -0700439 r->rt_flags, atomic_read(&r->dst.__refcnt),
440 r->dst.__use, 0, (__force u32)r->rt_src,
David S. Miller0dbaee32010-12-13 12:52:14 -0800441 dst_metric_advmss(&r->dst) + 40,
Changli Gaod8d1f302010-06-10 23:31:35 -0700442 dst_metric(&r->dst, RTAX_WINDOW),
443 (int)((dst_metric(&r->dst, RTAX_RTT) >> 3) +
444 dst_metric(&r->dst, RTAX_RTTVAR)),
David S. Miller475949d2011-05-03 19:45:15 -0700445 r->rt_key_tos,
David S. Millerf6b72b62011-07-14 07:53:20 -0700446 -1,
Eric Dumazet218fa902011-11-29 20:05:55 +0000447 HHUptod,
Pavel Emelyanov5e659e42008-04-24 01:02:16 -0700448 r->rt_spec_dst, &len);
449
450 seq_printf(seq, "%*s\n", 127 - len, "");
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900451 }
452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Stephen Hemmingerf6908082007-03-12 14:34:29 -0700455static const struct seq_operations rt_cache_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 .start = rt_cache_seq_start,
457 .next = rt_cache_seq_next,
458 .stop = rt_cache_seq_stop,
459 .show = rt_cache_seq_show,
460};
461
462static int rt_cache_seq_open(struct inode *inode, struct file *file)
463{
Denis V. Luneva75e9362008-02-28 20:50:55 -0800464 return seq_open_net(inode, file, &rt_cache_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -0700465 sizeof(struct rt_cache_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Arjan van de Ven9a321442007-02-12 00:55:35 -0800468static const struct file_operations rt_cache_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 .owner = THIS_MODULE,
470 .open = rt_cache_seq_open,
471 .read = seq_read,
472 .llseek = seq_lseek,
Denis V. Luneva75e9362008-02-28 20:50:55 -0800473 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474};
475
476
477static void *rt_cpu_seq_start(struct seq_file *seq, loff_t *pos)
478{
479 int cpu;
480
481 if (*pos == 0)
482 return SEQ_START_TOKEN;
483
Rusty Russell0f231742008-12-29 12:23:42 +0000484 for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (!cpu_possible(cpu))
486 continue;
487 *pos = cpu+1;
Eric Dumazet2f970d82006-01-17 02:54:36 -0800488 return &per_cpu(rt_cache_stat, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 return NULL;
491}
492
493static void *rt_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
494{
495 int cpu;
496
Rusty Russell0f231742008-12-29 12:23:42 +0000497 for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!cpu_possible(cpu))
499 continue;
500 *pos = cpu+1;
Eric Dumazet2f970d82006-01-17 02:54:36 -0800501 return &per_cpu(rt_cache_stat, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 return NULL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507static void rt_cpu_seq_stop(struct seq_file *seq, void *v)
508{
509
510}
511
512static int rt_cpu_seq_show(struct seq_file *seq, void *v)
513{
514 struct rt_cache_stat *st = v;
515
516 if (v == SEQ_START_TOKEN) {
Olaf Rempel5bec0032005-04-28 12:16:08 -0700517 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return 0;
519 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 seq_printf(seq,"%08x %08x %08x %08x %08x %08x %08x %08x "
522 " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
Eric Dumazetfc66f952010-10-08 06:37:34 +0000523 dst_entries_get_slow(&ipv4_dst_ops),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 st->in_hit,
525 st->in_slow_tot,
526 st->in_slow_mc,
527 st->in_no_route,
528 st->in_brd,
529 st->in_martian_dst,
530 st->in_martian_src,
531
532 st->out_hit,
533 st->out_slow_tot,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900534 st->out_slow_mc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 st->gc_total,
537 st->gc_ignored,
538 st->gc_goal_miss,
539 st->gc_dst_overflow,
540 st->in_hlist_search,
541 st->out_hlist_search
542 );
543 return 0;
544}
545
Stephen Hemmingerf6908082007-03-12 14:34:29 -0700546static const struct seq_operations rt_cpu_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 .start = rt_cpu_seq_start,
548 .next = rt_cpu_seq_next,
549 .stop = rt_cpu_seq_stop,
550 .show = rt_cpu_seq_show,
551};
552
553
554static int rt_cpu_seq_open(struct inode *inode, struct file *file)
555{
556 return seq_open(file, &rt_cpu_seq_ops);
557}
558
Arjan van de Ven9a321442007-02-12 00:55:35 -0800559static const struct file_operations rt_cpu_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 .owner = THIS_MODULE,
561 .open = rt_cpu_seq_open,
562 .read = seq_read,
563 .llseek = seq_lseek,
564 .release = seq_release,
565};
566
Patrick McHardyc7066f72011-01-14 13:36:42 +0100567#ifdef CONFIG_IP_ROUTE_CLASSID
Alexey Dobriyana661c412009-11-25 15:40:35 -0800568static int rt_acct_proc_show(struct seq_file *m, void *v)
Pavel Emelyanov78c686e2007-12-05 21:13:48 -0800569{
Alexey Dobriyana661c412009-11-25 15:40:35 -0800570 struct ip_rt_acct *dst, *src;
571 unsigned int i, j;
Pavel Emelyanov78c686e2007-12-05 21:13:48 -0800572
Alexey Dobriyana661c412009-11-25 15:40:35 -0800573 dst = kcalloc(256, sizeof(struct ip_rt_acct), GFP_KERNEL);
574 if (!dst)
575 return -ENOMEM;
Pavel Emelyanov78c686e2007-12-05 21:13:48 -0800576
Alexey Dobriyana661c412009-11-25 15:40:35 -0800577 for_each_possible_cpu(i) {
578 src = (struct ip_rt_acct *)per_cpu_ptr(ip_rt_acct, i);
579 for (j = 0; j < 256; j++) {
580 dst[j].o_bytes += src[j].o_bytes;
581 dst[j].o_packets += src[j].o_packets;
582 dst[j].i_bytes += src[j].i_bytes;
583 dst[j].i_packets += src[j].i_packets;
Pavel Emelyanov78c686e2007-12-05 21:13:48 -0800584 }
585 }
Alexey Dobriyana661c412009-11-25 15:40:35 -0800586
587 seq_write(m, dst, 256 * sizeof(struct ip_rt_acct));
588 kfree(dst);
589 return 0;
Pavel Emelyanov78c686e2007-12-05 21:13:48 -0800590}
Alexey Dobriyana661c412009-11-25 15:40:35 -0800591
592static int rt_acct_proc_open(struct inode *inode, struct file *file)
593{
594 return single_open(file, rt_acct_proc_show, NULL);
595}
596
597static const struct file_operations rt_acct_proc_fops = {
598 .owner = THIS_MODULE,
599 .open = rt_acct_proc_open,
600 .read = seq_read,
601 .llseek = seq_lseek,
602 .release = single_release,
603};
Pavel Emelyanov78c686e2007-12-05 21:13:48 -0800604#endif
Pavel Emelyanov107f1632007-12-05 21:14:28 -0800605
Denis V. Lunev73b38712008-02-28 20:51:18 -0800606static int __net_init ip_rt_do_proc_init(struct net *net)
Pavel Emelyanov107f1632007-12-05 21:14:28 -0800607{
608 struct proc_dir_entry *pde;
609
610 pde = proc_net_fops_create(net, "rt_cache", S_IRUGO,
611 &rt_cache_seq_fops);
612 if (!pde)
613 goto err1;
614
Wang Chen77020722008-02-28 14:14:25 -0800615 pde = proc_create("rt_cache", S_IRUGO,
616 net->proc_net_stat, &rt_cpu_seq_fops);
Pavel Emelyanov107f1632007-12-05 21:14:28 -0800617 if (!pde)
618 goto err2;
619
Patrick McHardyc7066f72011-01-14 13:36:42 +0100620#ifdef CONFIG_IP_ROUTE_CLASSID
Alexey Dobriyana661c412009-11-25 15:40:35 -0800621 pde = proc_create("rt_acct", 0, net->proc_net, &rt_acct_proc_fops);
Pavel Emelyanov107f1632007-12-05 21:14:28 -0800622 if (!pde)
623 goto err3;
624#endif
625 return 0;
626
Patrick McHardyc7066f72011-01-14 13:36:42 +0100627#ifdef CONFIG_IP_ROUTE_CLASSID
Pavel Emelyanov107f1632007-12-05 21:14:28 -0800628err3:
629 remove_proc_entry("rt_cache", net->proc_net_stat);
630#endif
631err2:
632 remove_proc_entry("rt_cache", net->proc_net);
633err1:
634 return -ENOMEM;
635}
Denis V. Lunev73b38712008-02-28 20:51:18 -0800636
637static void __net_exit ip_rt_do_proc_exit(struct net *net)
638{
639 remove_proc_entry("rt_cache", net->proc_net_stat);
640 remove_proc_entry("rt_cache", net->proc_net);
Patrick McHardyc7066f72011-01-14 13:36:42 +0100641#ifdef CONFIG_IP_ROUTE_CLASSID
Denis V. Lunev73b38712008-02-28 20:51:18 -0800642 remove_proc_entry("rt_acct", net->proc_net);
Alexey Dobriyan0a931ac2010-01-17 03:32:50 +0000643#endif
Denis V. Lunev73b38712008-02-28 20:51:18 -0800644}
645
646static struct pernet_operations ip_rt_proc_ops __net_initdata = {
647 .init = ip_rt_do_proc_init,
648 .exit = ip_rt_do_proc_exit,
649};
650
651static int __init ip_rt_proc_init(void)
652{
653 return register_pernet_subsys(&ip_rt_proc_ops);
654}
655
Pavel Emelyanov107f1632007-12-05 21:14:28 -0800656#else
Denis V. Lunev73b38712008-02-28 20:51:18 -0800657static inline int ip_rt_proc_init(void)
Pavel Emelyanov107f1632007-12-05 21:14:28 -0800658{
659 return 0;
660}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661#endif /* CONFIG_PROC_FS */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900662
Stephen Hemminger5969f712008-04-10 01:52:09 -0700663static inline void rt_free(struct rtable *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Changli Gaod8d1f302010-06-10 23:31:35 -0700665 call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Stephen Hemminger5969f712008-04-10 01:52:09 -0700668static inline void rt_drop(struct rtable *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 ip_rt_put(rt);
Changli Gaod8d1f302010-06-10 23:31:35 -0700671 call_rcu_bh(&rt->dst.rcu_head, dst_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Stephen Hemminger5969f712008-04-10 01:52:09 -0700674static inline int rt_fast_clean(struct rtable *rth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 /* Kill broadcast/multicast entries very aggresively, if they
677 collide in hash table with more useful entries */
678 return (rth->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) &&
David S. Millerc7537962010-11-11 17:07:48 -0800679 rt_is_input_route(rth) && rth->dst.rt_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Stephen Hemminger5969f712008-04-10 01:52:09 -0700682static inline int rt_valuable(struct rtable *rth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 return (rth->rt_flags & (RTCF_REDIRECTED | RTCF_NOTIFY)) ||
David S. Miller2c8cec52011-02-09 20:42:07 -0800685 (rth->peer && rth->peer->pmtu_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688static int rt_may_expire(struct rtable *rth, unsigned long tmo1, unsigned long tmo2)
689{
690 unsigned long age;
691 int ret = 0;
692
Changli Gaod8d1f302010-06-10 23:31:35 -0700693 if (atomic_read(&rth->dst.__refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto out;
695
Changli Gaod8d1f302010-06-10 23:31:35 -0700696 age = jiffies - rth->dst.lastuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if ((age <= tmo1 && !rt_fast_clean(rth)) ||
698 (age <= tmo2 && rt_valuable(rth)))
699 goto out;
700 ret = 1;
701out: return ret;
702}
703
704/* Bits of score are:
705 * 31: very valuable
706 * 30: not quite useless
707 * 29..0: usage counter
708 */
709static inline u32 rt_score(struct rtable *rt)
710{
Changli Gaod8d1f302010-06-10 23:31:35 -0700711 u32 score = jiffies - rt->dst.lastuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 score = ~score & ~(3<<30);
714
715 if (rt_valuable(rt))
716 score |= (1<<31);
717
David S. Millerc7537962010-11-11 17:07:48 -0800718 if (rt_is_output_route(rt) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 !(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL)))
720 score |= (1<<30);
721
722 return score;
723}
724
Neil Horman1080d702008-10-27 12:28:25 -0700725static inline bool rt_caching(const struct net *net)
726{
727 return net->ipv4.current_rt_cache_rebuild_count <=
728 net->ipv4.sysctl_rt_cache_rebuild_count;
729}
730
David S. Miller5e2b61f2011-03-04 21:47:09 -0800731static inline bool compare_hash_inputs(const struct rtable *rt1,
732 const struct rtable *rt2)
Neil Horman1080d702008-10-27 12:28:25 -0700733{
David S. Miller5e2b61f2011-03-04 21:47:09 -0800734 return ((((__force u32)rt1->rt_key_dst ^ (__force u32)rt2->rt_key_dst) |
735 ((__force u32)rt1->rt_key_src ^ (__force u32)rt2->rt_key_src) |
Julian Anastasov97a80412011-08-09 04:01:16 +0000736 (rt1->rt_route_iif ^ rt2->rt_route_iif)) == 0);
Neil Horman1080d702008-10-27 12:28:25 -0700737}
738
David S. Miller5e2b61f2011-03-04 21:47:09 -0800739static inline int compare_keys(struct rtable *rt1, struct rtable *rt2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
David S. Miller5e2b61f2011-03-04 21:47:09 -0800741 return (((__force u32)rt1->rt_key_dst ^ (__force u32)rt2->rt_key_dst) |
742 ((__force u32)rt1->rt_key_src ^ (__force u32)rt2->rt_key_src) |
743 (rt1->rt_mark ^ rt2->rt_mark) |
David S. Miller475949d2011-05-03 19:45:15 -0700744 (rt1->rt_key_tos ^ rt2->rt_key_tos) |
Julian Anastasovd547f722011-08-07 22:20:20 -0700745 (rt1->rt_route_iif ^ rt2->rt_route_iif) |
Lorenzo Colitti462ce7c2014-03-31 16:23:51 +0900746 (rt1->rt_uid ^ rt2->rt_uid) |
Julian Anastasov97a80412011-08-09 04:01:16 +0000747 (rt1->rt_oif ^ rt2->rt_oif)) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Denis V. Lunevb5921912008-01-22 23:50:25 -0800750static inline int compare_netns(struct rtable *rt1, struct rtable *rt2)
751{
Changli Gaod8d1f302010-06-10 23:31:35 -0700752 return net_eq(dev_net(rt1->dst.dev), dev_net(rt2->dst.dev));
Denis V. Lunevb5921912008-01-22 23:50:25 -0800753}
754
Denis V. Luneve84f84f2008-07-05 19:04:32 -0700755static inline int rt_is_expired(struct rtable *rth)
756{
Changli Gaod8d1f302010-06-10 23:31:35 -0700757 return rth->rt_genid != rt_genid(dev_net(rth->dst.dev));
Denis V. Luneve84f84f2008-07-05 19:04:32 -0700758}
759
Eric Dumazetbeb659b2007-11-19 22:43:37 -0800760/*
761 * Perform a full scan of hash table and free all entries.
762 * Can be called by a softirq or a process.
763 * In the later case, we want to be reschedule if necessary
764 */
David S. Miller6561a3b2010-12-19 21:11:20 -0800765static void rt_do_flush(struct net *net, int process_context)
Eric Dumazetbeb659b2007-11-19 22:43:37 -0800766{
767 unsigned int i;
768 struct rtable *rth, *next;
769
770 for (i = 0; i <= rt_hash_mask; i++) {
David S. Miller6561a3b2010-12-19 21:11:20 -0800771 struct rtable __rcu **pprev;
772 struct rtable *list;
773
Eric Dumazetbeb659b2007-11-19 22:43:37 -0800774 if (process_context && need_resched())
775 cond_resched();
Eric Dumazet33d480c2011-08-11 19:30:52 +0000776 rth = rcu_access_pointer(rt_hash_table[i].chain);
Eric Dumazetbeb659b2007-11-19 22:43:37 -0800777 if (!rth)
778 continue;
779
780 spin_lock_bh(rt_hash_lock_addr(i));
Denis V. Lunev32cb5b42008-07-05 19:06:12 -0700781
David S. Miller6561a3b2010-12-19 21:11:20 -0800782 list = NULL;
783 pprev = &rt_hash_table[i].chain;
784 rth = rcu_dereference_protected(*pprev,
Eric Dumazet1c317202010-10-25 21:02:07 +0000785 lockdep_is_held(rt_hash_lock_addr(i)));
Denis V. Lunev32cb5b42008-07-05 19:06:12 -0700786
David S. Miller6561a3b2010-12-19 21:11:20 -0800787 while (rth) {
788 next = rcu_dereference_protected(rth->dst.rt_next,
789 lockdep_is_held(rt_hash_lock_addr(i)));
Denis V. Lunev32cb5b42008-07-05 19:06:12 -0700790
David S. Miller6561a3b2010-12-19 21:11:20 -0800791 if (!net ||
792 net_eq(dev_net(rth->dst.dev), net)) {
793 rcu_assign_pointer(*pprev, next);
794 rcu_assign_pointer(rth->dst.rt_next, list);
795 list = rth;
Denis V. Lunev32cb5b42008-07-05 19:06:12 -0700796 } else {
David S. Miller6561a3b2010-12-19 21:11:20 -0800797 pprev = &rth->dst.rt_next;
Denis V. Lunev32cb5b42008-07-05 19:06:12 -0700798 }
David S. Miller6561a3b2010-12-19 21:11:20 -0800799 rth = next;
Denis V. Lunev32cb5b42008-07-05 19:06:12 -0700800 }
David S. Miller6561a3b2010-12-19 21:11:20 -0800801
Eric Dumazetbeb659b2007-11-19 22:43:37 -0800802 spin_unlock_bh(rt_hash_lock_addr(i));
803
David S. Miller6561a3b2010-12-19 21:11:20 -0800804 for (; list; list = next) {
805 next = rcu_dereference_protected(list->dst.rt_next, 1);
806 rt_free(list);
Eric Dumazetbeb659b2007-11-19 22:43:37 -0800807 }
808 }
809}
810
Neil Horman1080d702008-10-27 12:28:25 -0700811/*
812 * While freeing expired entries, we compute average chain length
813 * and standard deviation, using fixed-point arithmetic.
814 * This to have an estimation of rt_chain_length_max
815 * rt_chain_length_max = max(elasticity, AVG + 4*SD)
816 * We use 3 bits for frational part, and 29 (or 61) for magnitude.
817 */
818
819#define FRACT_BITS 3
820#define ONE (1UL << FRACT_BITS)
821
Eric Dumazet98376382010-03-08 03:20:00 +0000822/*
823 * Given a hash chain and an item in this hash chain,
824 * find if a previous entry has the same hash_inputs
825 * (but differs on tos, mark or oif)
826 * Returns 0 if an alias is found.
827 * Returns ONE if rth has no alias before itself.
828 */
829static int has_noalias(const struct rtable *head, const struct rtable *rth)
830{
831 const struct rtable *aux = head;
832
833 while (aux != rth) {
David S. Miller5e2b61f2011-03-04 21:47:09 -0800834 if (compare_hash_inputs(aux, rth))
Eric Dumazet98376382010-03-08 03:20:00 +0000835 return 0;
Eric Dumazet1c317202010-10-25 21:02:07 +0000836 aux = rcu_dereference_protected(aux->dst.rt_next, 1);
Eric Dumazet98376382010-03-08 03:20:00 +0000837 }
838 return ONE;
839}
840
Eric Dumazet9f28a2f2011-12-21 15:47:16 -0500841static void rt_check_expire(void)
842{
843 static unsigned int rover;
844 unsigned int i = rover, goal;
845 struct rtable *rth;
846 struct rtable __rcu **rthp;
847 unsigned long samples = 0;
848 unsigned long sum = 0, sum2 = 0;
849 unsigned long delta;
850 u64 mult;
851
852 delta = jiffies - expires_ljiffies;
853 expires_ljiffies = jiffies;
854 mult = ((u64)delta) << rt_hash_log;
855 if (ip_rt_gc_timeout > 1)
856 do_div(mult, ip_rt_gc_timeout);
857 goal = (unsigned int)mult;
858 if (goal > rt_hash_mask)
859 goal = rt_hash_mask + 1;
860 for (; goal > 0; goal--) {
861 unsigned long tmo = ip_rt_gc_timeout;
862 unsigned long length;
863
864 i = (i + 1) & rt_hash_mask;
865 rthp = &rt_hash_table[i].chain;
866
867 if (need_resched())
868 cond_resched();
869
870 samples++;
871
872 if (rcu_dereference_raw(*rthp) == NULL)
873 continue;
874 length = 0;
875 spin_lock_bh(rt_hash_lock_addr(i));
876 while ((rth = rcu_dereference_protected(*rthp,
877 lockdep_is_held(rt_hash_lock_addr(i)))) != NULL) {
878 prefetch(rth->dst.rt_next);
879 if (rt_is_expired(rth)) {
880 *rthp = rth->dst.rt_next;
881 rt_free(rth);
882 continue;
883 }
884 if (rth->dst.expires) {
885 /* Entry is expired even if it is in use */
886 if (time_before_eq(jiffies, rth->dst.expires)) {
887nofree:
888 tmo >>= 1;
889 rthp = &rth->dst.rt_next;
890 /*
891 * We only count entries on
892 * a chain with equal hash inputs once
893 * so that entries for different QOS
894 * levels, and other non-hash input
895 * attributes don't unfairly skew
896 * the length computation
897 */
898 length += has_noalias(rt_hash_table[i].chain, rth);
899 continue;
900 }
901 } else if (!rt_may_expire(rth, tmo, ip_rt_gc_timeout))
902 goto nofree;
903
904 /* Cleanup aged off entries. */
905 *rthp = rth->dst.rt_next;
906 rt_free(rth);
907 }
908 spin_unlock_bh(rt_hash_lock_addr(i));
909 sum += length;
910 sum2 += length*length;
911 }
912 if (samples) {
913 unsigned long avg = sum / samples;
914 unsigned long sd = int_sqrt(sum2 / samples - avg*avg);
915 rt_chain_length_max = max_t(unsigned long,
916 ip_rt_gc_elasticity,
917 (avg + 4*sd) >> FRACT_BITS);
918 }
919 rover = i;
920}
921
922/*
923 * rt_worker_func() is run in process context.
924 * we call rt_check_expire() to scan part of the hash table
925 */
926static void rt_worker_func(struct work_struct *work)
927{
928 rt_check_expire();
929 schedule_delayed_work(&expires_work, ip_rt_gc_interval);
930}
931
Eric Dumazet29e75252008-01-31 17:05:09 -0800932/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300933 * Perturbation of rt_genid by a small quantity [1..256]
Eric Dumazet29e75252008-01-31 17:05:09 -0800934 * Using 8 bits of shuffling ensure we can call rt_cache_invalidate()
935 * many times (2^24) without giving recent rt_genid.
936 * Jenkins hash is strong enough that litle changes of rt_genid are OK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
Denis V. Lunev86c657f2008-07-05 19:03:31 -0700938static void rt_cache_invalidate(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Eric Dumazet29e75252008-01-31 17:05:09 -0800940 unsigned char shuffle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Eric Dumazet29e75252008-01-31 17:05:09 -0800942 get_random_bytes(&shuffle, sizeof(shuffle));
Denis V. Luneve84f84f2008-07-05 19:04:32 -0700943 atomic_add(shuffle + 1U, &net->ipv4.rt_genid);
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000944 inetpeer_invalidate_tree(AF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Eric Dumazetbeb659b2007-11-19 22:43:37 -0800947/*
Eric Dumazet29e75252008-01-31 17:05:09 -0800948 * delay < 0 : invalidate cache (fast : entries will be deleted later)
949 * delay >= 0 : invalidate & flush cache (can be long)
950 */
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -0700951void rt_cache_flush(struct net *net, int delay)
Eric Dumazet29e75252008-01-31 17:05:09 -0800952{
Denis V. Lunev86c657f2008-07-05 19:03:31 -0700953 rt_cache_invalidate(net);
Eric Dumazet29e75252008-01-31 17:05:09 -0800954 if (delay >= 0)
David S. Miller6561a3b2010-12-19 21:11:20 -0800955 rt_do_flush(net, !in_softirq());
Eric Dumazet29e75252008-01-31 17:05:09 -0800956}
957
Eric W. Biedermana5ee1552009-11-29 15:45:58 +0000958/* Flush previous cache invalidated entries from the cache */
David S. Miller6561a3b2010-12-19 21:11:20 -0800959void rt_cache_flush_batch(struct net *net)
Eric W. Biedermana5ee1552009-11-29 15:45:58 +0000960{
David S. Miller6561a3b2010-12-19 21:11:20 -0800961 rt_do_flush(net, !in_softirq());
Eric W. Biedermana5ee1552009-11-29 15:45:58 +0000962}
963
Neil Horman1080d702008-10-27 12:28:25 -0700964static void rt_emergency_hash_rebuild(struct net *net)
965{
Neil Horman3ee94372010-05-08 01:57:52 -0700966 if (net_ratelimit())
Joe Perches058bd4d2012-03-11 18:36:11 +0000967 pr_warn("Route hash chain too long!\n");
Neil Horman3ee94372010-05-08 01:57:52 -0700968 rt_cache_invalidate(net);
Neil Horman1080d702008-10-27 12:28:25 -0700969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/*
972 Short description of GC goals.
973
974 We want to build algorithm, which will keep routing cache
975 at some equilibrium point, when number of aged off entries
976 is kept approximately equal to newly generated ones.
977
978 Current expiration strength is variable "expire".
979 We try to adjust it dynamically, so that if networking
980 is idle expires is large enough to keep enough of warm entries,
981 and when load increases it reduces to limit cache size.
982 */
983
Marcelo Ricardo Leitnerb6153ea2014-08-14 16:44:52 -0300984static void __do_rt_garbage_collect(int elasticity, int min_interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 static unsigned long expire = RT_GC_TIMEOUT;
987 static unsigned long last_gc;
988 static int rover;
989 static int equilibrium;
Marcelo Ricardo Leitnerb54ca602014-08-14 16:44:53 -0300990 static DEFINE_SPINLOCK(rt_gc_lock);
Eric Dumazet1c317202010-10-25 21:02:07 +0000991 struct rtable *rth;
992 struct rtable __rcu **rthp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 unsigned long now = jiffies;
994 int goal;
Eric Dumazetfc66f952010-10-08 06:37:34 +0000995 int entries = dst_entries_get_fast(&ipv4_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 /*
998 * Garbage collection is pretty expensive,
999 * do not make it too frequently.
1000 */
1001
Marcelo Ricardo Leitner50f1b3d2014-10-13 14:03:30 -03001002 spin_lock_bh(&rt_gc_lock);
Marcelo Ricardo Leitnerb54ca602014-08-14 16:44:53 -03001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 RT_CACHE_STAT_INC(gc_total);
1005
Marcelo Ricardo Leitnerb6153ea2014-08-14 16:44:52 -03001006 if (now - last_gc < min_interval &&
Eric Dumazetfc66f952010-10-08 06:37:34 +00001007 entries < ip_rt_max_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 RT_CACHE_STAT_INC(gc_ignored);
1009 goto out;
1010 }
1011
Eric Dumazetfc66f952010-10-08 06:37:34 +00001012 entries = dst_entries_get_slow(&ipv4_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* Calculate number of entries, which we want to expire now. */
Marcelo Ricardo Leitnerb6153ea2014-08-14 16:44:52 -03001014 goal = entries - (elasticity << rt_hash_log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (goal <= 0) {
1016 if (equilibrium < ipv4_dst_ops.gc_thresh)
1017 equilibrium = ipv4_dst_ops.gc_thresh;
Eric Dumazetfc66f952010-10-08 06:37:34 +00001018 goal = entries - equilibrium;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (goal > 0) {
Eric Dumazetb790ced2007-12-21 01:49:07 -08001020 equilibrium += min_t(unsigned int, goal >> 1, rt_hash_mask + 1);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001021 goal = entries - equilibrium;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023 } else {
1024 /* We are in dangerous area. Try to reduce cache really
1025 * aggressively.
1026 */
Eric Dumazetb790ced2007-12-21 01:49:07 -08001027 goal = max_t(unsigned int, goal >> 1, rt_hash_mask + 1);
Eric Dumazetfc66f952010-10-08 06:37:34 +00001028 equilibrium = entries - goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030
Marcelo Ricardo Leitnerb6153ea2014-08-14 16:44:52 -03001031 if (now - last_gc >= min_interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 last_gc = now;
1033
1034 if (goal <= 0) {
1035 equilibrium += goal;
1036 goto work_done;
1037 }
1038
1039 do {
1040 int i, k;
1041
1042 for (i = rt_hash_mask, k = rover; i >= 0; i--) {
1043 unsigned long tmo = expire;
1044
1045 k = (k + 1) & rt_hash_mask;
1046 rthp = &rt_hash_table[k].chain;
Eric Dumazet22c047c2005-07-05 14:55:24 -07001047 spin_lock_bh(rt_hash_lock_addr(k));
Eric Dumazet1c317202010-10-25 21:02:07 +00001048 while ((rth = rcu_dereference_protected(*rthp,
1049 lockdep_is_held(rt_hash_lock_addr(k)))) != NULL) {
Denis V. Luneve84f84f2008-07-05 19:04:32 -07001050 if (!rt_is_expired(rth) &&
Eric Dumazet29e75252008-01-31 17:05:09 -08001051 !rt_may_expire(rth, tmo, expire)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 tmo >>= 1;
Changli Gaod8d1f302010-06-10 23:31:35 -07001053 rthp = &rth->dst.rt_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 continue;
1055 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001056 *rthp = rth->dst.rt_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 rt_free(rth);
1058 goal--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
Eric Dumazet22c047c2005-07-05 14:55:24 -07001060 spin_unlock_bh(rt_hash_lock_addr(k));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (goal <= 0)
1062 break;
1063 }
1064 rover = k;
1065
1066 if (goal <= 0)
1067 goto work_done;
1068
1069 /* Goal is not achieved. We stop process if:
1070
1071 - if expire reduced to zero. Otherwise, expire is halfed.
1072 - if table is not full.
1073 - if we are called from interrupt.
1074 - jiffies check is just fallback/debug loop breaker.
1075 We will not spin here for long time in any case.
1076 */
1077
1078 RT_CACHE_STAT_INC(gc_goal_miss);
1079
1080 if (expire == 0)
1081 break;
1082
1083 expire >>= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Eric Dumazetfc66f952010-10-08 06:37:34 +00001085 if (dst_entries_get_fast(&ipv4_dst_ops) < ip_rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 goto out;
1087 } while (!in_softirq() && time_before_eq(jiffies, now));
1088
Eric Dumazetfc66f952010-10-08 06:37:34 +00001089 if (dst_entries_get_fast(&ipv4_dst_ops) < ip_rt_max_size)
1090 goto out;
1091 if (dst_entries_get_slow(&ipv4_dst_ops) < ip_rt_max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 goto out;
1093 if (net_ratelimit())
Joe Perches058bd4d2012-03-11 18:36:11 +00001094 pr_warn("dst cache overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 RT_CACHE_STAT_INC(gc_dst_overflow);
Marcelo Ricardo Leitnerb54ca602014-08-14 16:44:53 -03001096 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098work_done:
Marcelo Ricardo Leitnerb6153ea2014-08-14 16:44:52 -03001099 expire += min_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (expire > ip_rt_gc_timeout ||
Eric Dumazetfc66f952010-10-08 06:37:34 +00001101 dst_entries_get_fast(&ipv4_dst_ops) < ipv4_dst_ops.gc_thresh ||
1102 dst_entries_get_slow(&ipv4_dst_ops) < ipv4_dst_ops.gc_thresh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 expire = ip_rt_gc_timeout;
Marcelo Ricardo Leitnerb54ca602014-08-14 16:44:53 -03001104out:
Marcelo Ricardo Leitner50f1b3d2014-10-13 14:03:30 -03001105 spin_unlock_bh(&rt_gc_lock);
Marcelo Ricardo Leitnerb6153ea2014-08-14 16:44:52 -03001106}
1107
1108static void __rt_garbage_collect(struct work_struct *w)
1109{
1110 __do_rt_garbage_collect(ip_rt_gc_elasticity, ip_rt_gc_min_interval);
1111}
1112
1113static int rt_garbage_collect(struct dst_ops *ops)
1114{
1115 if (!work_pending(&rt_gc_worker))
1116 schedule_work(&rt_gc_worker);
1117
1118 if (dst_entries_get_fast(&ipv4_dst_ops) >= ip_rt_max_size ||
1119 dst_entries_get_slow(&ipv4_dst_ops) >= ip_rt_max_size) {
1120 RT_CACHE_STAT_INC(gc_dst_overflow);
1121 return 1;
1122 }
1123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
Eric Dumazet98376382010-03-08 03:20:00 +00001126/*
1127 * Returns number of entries in a hash chain that have different hash_inputs
1128 */
1129static int slow_chain_length(const struct rtable *head)
1130{
1131 int length = 0;
1132 const struct rtable *rth = head;
1133
1134 while (rth) {
1135 length += has_noalias(head, rth);
Eric Dumazet1c317202010-10-25 21:02:07 +00001136 rth = rcu_dereference_protected(rth->dst.rt_next, 1);
Eric Dumazet98376382010-03-08 03:20:00 +00001137 }
1138 return length >> FRACT_BITS;
1139}
1140
David S. Millerd3aaeb32011-07-18 00:40:17 -07001141static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst, const void *daddr)
David Miller3769cff2011-07-11 22:44:24 +00001142{
David S. Millerd3aaeb32011-07-18 00:40:17 -07001143 static const __be32 inaddr_any = 0;
1144 struct net_device *dev = dst->dev;
1145 const __be32 *pkey = daddr;
David S. Miller39232972012-01-26 15:22:32 -05001146 const struct rtable *rt;
David Miller3769cff2011-07-11 22:44:24 +00001147 struct neighbour *n;
1148
David S. Miller39232972012-01-26 15:22:32 -05001149 rt = (const struct rtable *) dst;
1150
David Miller3769cff2011-07-11 22:44:24 +00001151 if (dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))
David S. Millerd3aaeb32011-07-18 00:40:17 -07001152 pkey = &inaddr_any;
David S. Miller39232972012-01-26 15:22:32 -05001153 else if (rt->rt_gateway)
1154 pkey = (const __be32 *) &rt->rt_gateway;
David S. Millerd3aaeb32011-07-18 00:40:17 -07001155
David S. Miller80703d22012-02-15 17:48:35 -05001156 n = __ipv4_neigh_lookup(dev, *(__force u32 *)pkey);
David S. Millerd3aaeb32011-07-18 00:40:17 -07001157 if (n)
1158 return n;
David Miller32092ec2011-07-25 00:01:41 +00001159 return neigh_create(&arp_tbl, pkey, dev);
David S. Millerd3aaeb32011-07-18 00:40:17 -07001160}
1161
1162static int rt_bind_neighbour(struct rtable *rt)
1163{
1164 struct neighbour *n = ipv4_neigh_lookup(&rt->dst, &rt->rt_gateway);
David Miller3769cff2011-07-11 22:44:24 +00001165 if (IS_ERR(n))
1166 return PTR_ERR(n);
David S. Miller69cce1d2011-07-17 23:09:49 -07001167 dst_set_neighbour(&rt->dst, n);
David Miller3769cff2011-07-11 22:44:24 +00001168
1169 return 0;
1170}
1171
David S. Millerb23dd4f2011-03-02 14:31:35 -08001172static struct rtable *rt_intern_hash(unsigned hash, struct rtable *rt,
1173 struct sk_buff *skb, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Eric Dumazet1c317202010-10-25 21:02:07 +00001175 struct rtable *rth, *cand;
1176 struct rtable __rcu **rthp, **candp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 u32 min_score;
1179 int chain_length;
Marcelo Ricardo Leitnerb54ca602014-08-14 16:44:53 -03001180 int attempts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182restart:
1183 chain_length = 0;
1184 min_score = ~(u32)0;
1185 cand = NULL;
1186 candp = NULL;
1187 now = jiffies;
1188
Changli Gaod8d1f302010-06-10 23:31:35 -07001189 if (!rt_caching(dev_net(rt->dst.dev))) {
Neil Horman73e42892009-06-20 01:15:16 -07001190 /*
1191 * If we're not caching, just tell the caller we
1192 * were successful and don't touch the route. The
1193 * caller hold the sole reference to the cache entry, and
1194 * it will be released when the caller is done with it.
1195 * If we drop it here, the callers have no way to resolve routes
1196 * when we're not caching. Instead, just point *rp at rt, so
1197 * the caller gets a single use out of the route
Neil Hormanb6280b42009-06-22 10:18:53 +00001198 * Note that we do rt_free on this new route entry, so that
1199 * once its refcount hits zero, we are still able to reap it
1200 * (Thanks Alexey)
Eric Dumazet27b75c92010-10-15 05:44:11 +00001201 * Note: To avoid expensive rcu stuff for this uncached dst,
1202 * we set DST_NOCACHE so that dst_release() can free dst without
1203 * waiting a grace period.
Neil Horman73e42892009-06-20 01:15:16 -07001204 */
Neil Hormanb6280b42009-06-22 10:18:53 +00001205
Eric Dumazetc7d44262010-10-03 22:17:54 -07001206 rt->dst.flags |= DST_NOCACHE;
David S. Millerc7537962010-11-11 17:07:48 -08001207 if (rt->rt_type == RTN_UNICAST || rt_is_output_route(rt)) {
David Miller3769cff2011-07-11 22:44:24 +00001208 int err = rt_bind_neighbour(rt);
Neil Hormanb6280b42009-06-22 10:18:53 +00001209 if (err) {
1210 if (net_ratelimit())
Joe Perches058bd4d2012-03-11 18:36:11 +00001211 pr_warn("Neighbour table failure & not caching routes\n");
Eric Dumazet27b75c92010-10-15 05:44:11 +00001212 ip_rt_put(rt);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001213 return ERR_PTR(err);
Neil Hormanb6280b42009-06-22 10:18:53 +00001214 }
1215 }
1216
Neil Hormanb6280b42009-06-22 10:18:53 +00001217 goto skip_hashing;
Neil Horman1080d702008-10-27 12:28:25 -07001218 }
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 rthp = &rt_hash_table[hash].chain;
1221
Eric Dumazet22c047c2005-07-05 14:55:24 -07001222 spin_lock_bh(rt_hash_lock_addr(hash));
Eric Dumazet1c317202010-10-25 21:02:07 +00001223 while ((rth = rcu_dereference_protected(*rthp,
1224 lockdep_is_held(rt_hash_lock_addr(hash)))) != NULL) {
Denis V. Luneve84f84f2008-07-05 19:04:32 -07001225 if (rt_is_expired(rth)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001226 *rthp = rth->dst.rt_next;
Eric Dumazet29e75252008-01-31 17:05:09 -08001227 rt_free(rth);
1228 continue;
1229 }
David S. Miller5e2b61f2011-03-04 21:47:09 -08001230 if (compare_keys(rth, rt) && compare_netns(rth, rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* Put it first */
Changli Gaod8d1f302010-06-10 23:31:35 -07001232 *rthp = rth->dst.rt_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /*
1234 * Since lookup is lockfree, the deletion
1235 * must be visible to another weakly ordered CPU before
1236 * the insertion at the start of the hash chain.
1237 */
Changli Gaod8d1f302010-06-10 23:31:35 -07001238 rcu_assign_pointer(rth->dst.rt_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 rt_hash_table[hash].chain);
1240 /*
1241 * Since lookup is lockfree, the update writes
1242 * must be ordered for consistency on SMP.
1243 */
1244 rcu_assign_pointer(rt_hash_table[hash].chain, rth);
1245
Changli Gaod8d1f302010-06-10 23:31:35 -07001246 dst_use(&rth->dst, now);
Eric Dumazet22c047c2005-07-05 14:55:24 -07001247 spin_unlock_bh(rt_hash_lock_addr(hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 rt_drop(rt);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001250 if (skb)
Changli Gaod8d1f302010-06-10 23:31:35 -07001251 skb_dst_set(skb, &rth->dst);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001252 return rth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
Changli Gaod8d1f302010-06-10 23:31:35 -07001255 if (!atomic_read(&rth->dst.__refcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 u32 score = rt_score(rth);
1257
1258 if (score <= min_score) {
1259 cand = rth;
1260 candp = rthp;
1261 min_score = score;
1262 }
1263 }
1264
1265 chain_length++;
1266
Changli Gaod8d1f302010-06-10 23:31:35 -07001267 rthp = &rth->dst.rt_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269
1270 if (cand) {
1271 /* ip_rt_gc_elasticity used to be average length of chain
1272 * length, when exceeded gc becomes really aggressive.
1273 *
1274 * The second limit is less certain. At the moment it allows
1275 * only 2 entries per bucket. We will see.
1276 */
1277 if (chain_length > ip_rt_gc_elasticity) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001278 *candp = cand->dst.rt_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 rt_free(cand);
1280 }
Neil Horman1080d702008-10-27 12:28:25 -07001281 } else {
Eric Dumazet98376382010-03-08 03:20:00 +00001282 if (chain_length > rt_chain_length_max &&
1283 slow_chain_length(rt_hash_table[hash].chain) > rt_chain_length_max) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001284 struct net *net = dev_net(rt->dst.dev);
Neil Horman1080d702008-10-27 12:28:25 -07001285 int num = ++net->ipv4.current_rt_cache_rebuild_count;
Pavel Emelyanovb35ecb52010-03-24 07:43:17 +00001286 if (!rt_caching(net)) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001287 pr_warn("%s: %d rebuilds is over limit, route caching disabled\n",
Changli Gaod8d1f302010-06-10 23:31:35 -07001288 rt->dst.dev->name, num);
Neil Horman1080d702008-10-27 12:28:25 -07001289 }
Pavel Emelyanovb35ecb52010-03-24 07:43:17 +00001290 rt_emergency_hash_rebuild(net);
Pavel Emelyanov6a2bad72010-03-24 21:51:22 +00001291 spin_unlock_bh(rt_hash_lock_addr(hash));
1292
David S. Miller5e2b61f2011-03-04 21:47:09 -08001293 hash = rt_hash(rt->rt_key_dst, rt->rt_key_src,
Pavel Emelyanov6a2bad72010-03-24 21:51:22 +00001294 ifindex, rt_genid(net));
1295 goto restart;
Neil Horman1080d702008-10-27 12:28:25 -07001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
1299 /* Try to bind route to arp only if it is output
1300 route or unicast forwarding path.
1301 */
David S. Millerc7537962010-11-11 17:07:48 -08001302 if (rt->rt_type == RTN_UNICAST || rt_is_output_route(rt)) {
David Miller3769cff2011-07-11 22:44:24 +00001303 int err = rt_bind_neighbour(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (err) {
Eric Dumazet22c047c2005-07-05 14:55:24 -07001305 spin_unlock_bh(rt_hash_lock_addr(hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 if (err != -ENOBUFS) {
1308 rt_drop(rt);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001309 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311
1312 /* Neighbour tables are full and nothing
1313 can be released. Try to shrink route cache,
1314 it is most likely it holds some neighbour records.
1315 */
Marcelo Ricardo Leitnerb54ca602014-08-14 16:44:53 -03001316 if (!in_softirq() && attempts-- > 0) {
1317 static DEFINE_SPINLOCK(lock);
1318
1319 if (spin_trylock(&lock)) {
1320 __do_rt_garbage_collect(1, 0);
1321 spin_unlock(&lock);
1322 } else {
1323 spin_unlock_wait(&lock);
1324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 goto restart;
1326 }
1327
1328 if (net_ratelimit())
Joe Perchesafd465032012-03-12 07:03:32 +00001329 pr_warn("Neighbour table overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 rt_drop(rt);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001331 return ERR_PTR(-ENOBUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333 }
1334
Changli Gaod8d1f302010-06-10 23:31:35 -07001335 rt->dst.rt_next = rt_hash_table[hash].chain;
Neil Horman1080d702008-10-27 12:28:25 -07001336
Eric Dumazet00269b52008-10-16 14:18:29 -07001337 /*
1338 * Since lookup is lockfree, we must make sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001339 * previous writes to rt are committed to memory
Eric Dumazet00269b52008-10-16 14:18:29 -07001340 * before making rt visible to other CPUS.
1341 */
Eric Dumazet1ddbcb02009-05-19 20:14:28 +00001342 rcu_assign_pointer(rt_hash_table[hash].chain, rt);
Neil Horman1080d702008-10-27 12:28:25 -07001343
Eric Dumazet22c047c2005-07-05 14:55:24 -07001344 spin_unlock_bh(rt_hash_lock_addr(hash));
Neil Horman73e42892009-06-20 01:15:16 -07001345
Neil Hormanb6280b42009-06-22 10:18:53 +00001346skip_hashing:
David S. Millerb23dd4f2011-03-02 14:31:35 -08001347 if (skb)
Changli Gaod8d1f302010-06-10 23:31:35 -07001348 skb_dst_set(skb, &rt->dst);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001349 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
David S. Miller6431cbc2011-02-07 20:38:06 -08001352static atomic_t __rt_peer_genid = ATOMIC_INIT(0);
1353
1354static u32 rt_peer_genid(void)
1355{
1356 return atomic_read(&__rt_peer_genid);
1357}
1358
David S. Millera48eff12011-05-18 18:42:43 -04001359void rt_bind_peer(struct rtable *rt, __be32 daddr, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 struct inet_peer *peer;
1362
David S. Millera48eff12011-05-18 18:42:43 -04001363 peer = inet_getpeer_v4(daddr, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Eric Dumazet49e8ab02010-08-19 06:10:45 +00001365 if (peer && cmpxchg(&rt->peer, NULL, peer) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 inet_putpeer(peer);
David S. Miller6431cbc2011-02-07 20:38:06 -08001367 else
1368 rt->rt_peer_genid = rt_peer_genid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
Eric Dumazet509a15a2014-07-26 08:58:10 +02001371#define IP_IDENTS_SZ 2048u
1372struct ip_ident_bucket {
1373 atomic_t id;
1374 u32 stamp32;
1375};
1376
1377static struct ip_ident_bucket *ip_idents __read_mostly;
1378
1379/* In order to protect privacy, we add a perturbation to identifiers
1380 * if one generator is seldom used. This makes hard for an attacker
1381 * to infer how many packets were sent between two points in time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 */
Eric Dumazet509a15a2014-07-26 08:58:10 +02001383u32 ip_idents_reserve(u32 hash, int segs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Eric Dumazet509a15a2014-07-26 08:58:10 +02001385 struct ip_ident_bucket *bucket = ip_idents + hash % IP_IDENTS_SZ;
1386 u32 old = ACCESS_ONCE(bucket->stamp32);
1387 u32 now = (u32)jiffies;
1388 u32 delta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Eric Dumazet509a15a2014-07-26 08:58:10 +02001390 if (old != now && cmpxchg(&bucket->stamp32, old, now) == old) {
1391 u64 x = random32();
1392
1393 x *= (now - old);
1394 delta = (u32)(x >> 32);
1395 }
1396
1397 return atomic_add_return(segs + delta, &bucket->id) - segs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
Eric Dumazet509a15a2014-07-26 08:58:10 +02001399EXPORT_SYMBOL(ip_idents_reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Eric Dumazetad52eef2014-06-02 05:26:03 -07001401void __ip_select_ident(struct iphdr *iph, int segs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Eric Dumazetad52eef2014-06-02 05:26:03 -07001403 static u32 ip_idents_hashrnd __read_mostly;
1404 static bool hashrnd_initialized = false;
1405 u32 hash, id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Eric Dumazetad52eef2014-06-02 05:26:03 -07001407 if (unlikely(!hashrnd_initialized)) {
1408 hashrnd_initialized = true;
1409 get_random_bytes(&ip_idents_hashrnd, sizeof(ip_idents_hashrnd));
1410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Eric Dumazet509a15a2014-07-26 08:58:10 +02001412 hash = jhash_3words((__force u32)iph->daddr,
1413 (__force u32)iph->saddr,
1414 iph->protocol,
1415 ip_idents_hashrnd);
Eric Dumazetad52eef2014-06-02 05:26:03 -07001416 id = ip_idents_reserve(hash, segs);
1417 iph->id = htons(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001419EXPORT_SYMBOL(__ip_select_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421static void rt_del(unsigned hash, struct rtable *rt)
1422{
Eric Dumazet1c317202010-10-25 21:02:07 +00001423 struct rtable __rcu **rthp;
1424 struct rtable *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Eric Dumazet29e75252008-01-31 17:05:09 -08001426 rthp = &rt_hash_table[hash].chain;
Eric Dumazet22c047c2005-07-05 14:55:24 -07001427 spin_lock_bh(rt_hash_lock_addr(hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 ip_rt_put(rt);
Eric Dumazet1c317202010-10-25 21:02:07 +00001429 while ((aux = rcu_dereference_protected(*rthp,
1430 lockdep_is_held(rt_hash_lock_addr(hash)))) != NULL) {
Denis V. Luneve84f84f2008-07-05 19:04:32 -07001431 if (aux == rt || rt_is_expired(aux)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001432 *rthp = aux->dst.rt_next;
Eric Dumazet29e75252008-01-31 17:05:09 -08001433 rt_free(aux);
1434 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001436 rthp = &aux->dst.rt_next;
Eric Dumazet29e75252008-01-31 17:05:09 -08001437 }
Eric Dumazet22c047c2005-07-05 14:55:24 -07001438 spin_unlock_bh(rt_hash_lock_addr(hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
David S. Millerde398fb2011-12-05 13:21:42 -05001441static void check_peer_redir(struct dst_entry *dst, struct inet_peer *peer)
Eric Dumazet9cc20b22011-11-18 15:24:32 -05001442{
1443 struct rtable *rt = (struct rtable *) dst;
1444 __be32 orig_gw = rt->rt_gateway;
1445 struct neighbour *n, *old_n;
1446
1447 dst_confirm(&rt->dst);
1448
1449 rt->rt_gateway = peer->redirect_learned.a4;
1450
1451 n = ipv4_neigh_lookup(&rt->dst, &rt->rt_gateway);
David S. Millerde398fb2011-12-05 13:21:42 -05001452 if (IS_ERR(n)) {
1453 rt->rt_gateway = orig_gw;
1454 return;
1455 }
Eric Dumazet9cc20b22011-11-18 15:24:32 -05001456 old_n = xchg(&rt->dst._neighbour, n);
1457 if (old_n)
1458 neigh_release(old_n);
David S. Millerde398fb2011-12-05 13:21:42 -05001459 if (!(n->nud_state & NUD_VALID)) {
1460 neigh_event_send(n, NULL);
Eric Dumazet9cc20b22011-11-18 15:24:32 -05001461 } else {
1462 rt->rt_flags |= RTCF_REDIRECTED;
1463 call_netevent_notifiers(NETEVENT_NEIGH_UPDATE, n);
1464 }
Eric Dumazet9cc20b22011-11-18 15:24:32 -05001465}
1466
Eric Dumazeted7865a42010-06-07 21:49:44 -07001467/* called in rcu_read_lock() section */
Al Virof7655222006-09-26 21:25:43 -07001468void ip_rt_redirect(__be32 old_gw, __be32 daddr, __be32 new_gw,
1469 __be32 saddr, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Flavio Leitner7cc91502011-10-24 02:56:38 -04001471 int s, i;
Eric Dumazeted7865a42010-06-07 21:49:44 -07001472 struct in_device *in_dev = __in_dev_get_rcu(dev);
Flavio Leitner7cc91502011-10-24 02:56:38 -04001473 __be32 skeys[2] = { saddr, 0 };
1474 int ikeys[2] = { dev->ifindex, 0 };
David S. Millerf39925d2011-02-09 22:00:16 -08001475 struct inet_peer *peer;
Denis V. Lunev317805b2008-02-28 20:50:06 -08001476 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (!in_dev)
1479 return;
1480
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001481 net = dev_net(dev);
Joe Perches9d4fb272009-11-23 10:41:23 -08001482 if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) ||
1483 ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) ||
1484 ipv4_is_zeronet(new_gw))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 goto reject_redirect;
1486
1487 if (!IN_DEV_SHARED_MEDIA(in_dev)) {
1488 if (!inet_addr_onlink(in_dev, new_gw, old_gw))
1489 goto reject_redirect;
1490 if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
1491 goto reject_redirect;
1492 } else {
Denis V. Lunev317805b2008-02-28 20:50:06 -08001493 if (inet_addr_type(net, new_gw) != RTN_UNICAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 goto reject_redirect;
1495 }
1496
Flavio Leitner7cc91502011-10-24 02:56:38 -04001497 for (s = 0; s < 2; s++) {
1498 for (i = 0; i < 2; i++) {
Eric Dumazet9cc20b22011-11-18 15:24:32 -05001499 unsigned int hash;
1500 struct rtable __rcu **rthp;
1501 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Eric Dumazet9cc20b22011-11-18 15:24:32 -05001503 hash = rt_hash(daddr, skeys[s], ikeys[i], rt_genid(net));
1504
1505 rthp = &rt_hash_table[hash].chain;
1506
1507 while ((rt = rcu_dereference(*rthp)) != NULL) {
1508 rthp = &rt->dst.rt_next;
1509
1510 if (rt->rt_key_dst != daddr ||
1511 rt->rt_key_src != skeys[s] ||
1512 rt->rt_oif != ikeys[i] ||
1513 rt_is_input_route(rt) ||
1514 rt_is_expired(rt) ||
1515 !net_eq(dev_net(rt->dst.dev), net) ||
1516 rt->dst.error ||
1517 rt->dst.dev != dev ||
1518 rt->rt_gateway != old_gw)
1519 continue;
1520
1521 if (!rt->peer)
1522 rt_bind_peer(rt, rt->rt_dst, 1);
1523
1524 peer = rt->peer;
1525 if (peer) {
Steffen Klassertac3f48d2012-03-06 21:21:10 +00001526 if (peer->redirect_learned.a4 != new_gw) {
Eric Dumazet9cc20b22011-11-18 15:24:32 -05001527 peer->redirect_learned.a4 = new_gw;
1528 atomic_inc(&__rt_peer_genid);
1529 }
1530 check_peer_redir(&rt->dst, peer);
1531 }
Flavio Leitner7cc91502011-10-24 02:56:38 -04001532 }
Flavio Leitner7cc91502011-10-24 02:56:38 -04001533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return;
1536
1537reject_redirect:
1538#ifdef CONFIG_IP_ROUTE_VERBOSE
1539 if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
Joe Perches058bd4d2012-03-11 18:36:11 +00001540 pr_info("Redirect from %pI4 on %s about %pI4 ignored\n"
Harvey Harrison673d57e2008-10-31 00:53:57 -07001541 " Advised path = %pI4 -> %pI4\n",
Joe Perches058bd4d2012-03-11 18:36:11 +00001542 &old_gw, dev->name, &new_gw,
1543 &saddr, &daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544#endif
Eric Dumazeted7865a42010-06-07 21:49:44 -07001545 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Eric Dumazetfe6fe792011-06-08 06:07:07 +00001548static bool peer_pmtu_expired(struct inet_peer *peer)
1549{
1550 unsigned long orig = ACCESS_ONCE(peer->pmtu_expires);
1551
1552 return orig &&
1553 time_after_eq(jiffies, orig) &&
1554 cmpxchg(&peer->pmtu_expires, orig, 0) == orig;
1555}
1556
1557static bool peer_pmtu_cleaned(struct inet_peer *peer)
1558{
1559 unsigned long orig = ACCESS_ONCE(peer->pmtu_expires);
1560
1561 return orig &&
1562 cmpxchg(&peer->pmtu_expires, orig, 0) == orig;
1563}
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
1566{
Eric Dumazetee6b9672008-03-05 18:30:47 -08001567 struct rtable *rt = (struct rtable *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 struct dst_entry *ret = dst;
1569
1570 if (rt) {
Timo Teräsd11a4dc2010-03-18 23:20:20 +00001571 if (dst->obsolete > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 ip_rt_put(rt);
1573 ret = NULL;
David S. Miller2c8cec52011-02-09 20:42:07 -08001574 } else if (rt->rt_flags & RTCF_REDIRECTED) {
David S. Miller5e2b61f2011-03-04 21:47:09 -08001575 unsigned hash = rt_hash(rt->rt_key_dst, rt->rt_key_src,
1576 rt->rt_oif,
Denis V. Luneve84f84f2008-07-05 19:04:32 -07001577 rt_genid(dev_net(dst->dev)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 rt_del(hash, rt);
1579 ret = NULL;
Eric Dumazetfe6fe792011-06-08 06:07:07 +00001580 } else if (rt->peer && peer_pmtu_expired(rt->peer)) {
1581 dst_metric_set(dst, RTAX_MTU, rt->peer->pmtu_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583 }
1584 return ret;
1585}
1586
1587/*
1588 * Algorithm:
1589 * 1. The first ip_rt_redirect_number redirects are sent
1590 * with exponential backoff, then we stop sending them at all,
1591 * assuming that the host ignores our redirects.
1592 * 2. If we did not see packets requiring redirects
1593 * during ip_rt_redirect_silence, we assume that the host
1594 * forgot redirected route and start to send redirects again.
1595 *
1596 * This algorithm is much cheaper and more intelligent than dumb load limiting
1597 * in icmp.c.
1598 *
1599 * NOTE. Do not forget to inhibit load limiting for redirects (redundant)
1600 * and "frag. need" (breaks PMTU discovery) in icmp.c.
1601 */
1602
1603void ip_rt_send_redirect(struct sk_buff *skb)
1604{
Eric Dumazet511c3f92009-06-02 05:14:27 +00001605 struct rtable *rt = skb_rtable(skb);
Eric Dumazet30038fc2009-08-28 23:52:01 -07001606 struct in_device *in_dev;
David S. Miller92d86822011-02-04 15:55:25 -08001607 struct inet_peer *peer;
Eric Dumazet30038fc2009-08-28 23:52:01 -07001608 int log_martians;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Eric Dumazet30038fc2009-08-28 23:52:01 -07001610 rcu_read_lock();
Changli Gaod8d1f302010-06-10 23:31:35 -07001611 in_dev = __in_dev_get_rcu(rt->dst.dev);
Eric Dumazet30038fc2009-08-28 23:52:01 -07001612 if (!in_dev || !IN_DEV_TX_REDIRECTS(in_dev)) {
1613 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return;
Eric Dumazet30038fc2009-08-28 23:52:01 -07001615 }
1616 log_martians = IN_DEV_LOG_MARTIANS(in_dev);
1617 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
David S. Miller92d86822011-02-04 15:55:25 -08001619 if (!rt->peer)
David S. Millera48eff12011-05-18 18:42:43 -04001620 rt_bind_peer(rt, rt->rt_dst, 1);
David S. Miller92d86822011-02-04 15:55:25 -08001621 peer = rt->peer;
1622 if (!peer) {
1623 icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway);
1624 return;
1625 }
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 /* No redirected packets during ip_rt_redirect_silence;
1628 * reset the algorithm.
1629 */
David S. Miller92d86822011-02-04 15:55:25 -08001630 if (time_after(jiffies, peer->rate_last + ip_rt_redirect_silence))
1631 peer->rate_tokens = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 /* Too many ignored redirects; do not send anything
Changli Gaod8d1f302010-06-10 23:31:35 -07001634 * set dst.rate_last to the last seen redirected packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 */
David S. Miller92d86822011-02-04 15:55:25 -08001636 if (peer->rate_tokens >= ip_rt_redirect_number) {
1637 peer->rate_last = jiffies;
Eric Dumazet30038fc2009-08-28 23:52:01 -07001638 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640
1641 /* Check for load limit; set rate_last to the latest sent
1642 * redirect.
1643 */
David S. Miller92d86822011-02-04 15:55:25 -08001644 if (peer->rate_tokens == 0 ||
Li Yewang14fb8a72006-12-18 00:26:35 -08001645 time_after(jiffies,
David S. Miller92d86822011-02-04 15:55:25 -08001646 (peer->rate_last +
1647 (ip_rt_redirect_load << peer->rate_tokens)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, rt->rt_gateway);
David S. Miller92d86822011-02-04 15:55:25 -08001649 peer->rate_last = jiffies;
1650 ++peer->rate_tokens;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651#ifdef CONFIG_IP_ROUTE_VERBOSE
Eric Dumazet30038fc2009-08-28 23:52:01 -07001652 if (log_martians &&
David S. Miller92d86822011-02-04 15:55:25 -08001653 peer->rate_tokens == ip_rt_redirect_number &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 net_ratelimit())
Joe Perches058bd4d2012-03-11 18:36:11 +00001655 pr_warn("host %pI4/if%d ignores redirects for %pI4 to %pI4\n",
1656 &ip_hdr(skb)->saddr, rt->rt_iif,
Harvey Harrison673d57e2008-10-31 00:53:57 -07001657 &rt->rt_dst, &rt->rt_gateway);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658#endif
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
1662static int ip_error(struct sk_buff *skb)
1663{
Eric Dumazet511c3f92009-06-02 05:14:27 +00001664 struct rtable *rt = skb_rtable(skb);
David S. Miller92d86822011-02-04 15:55:25 -08001665 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 unsigned long now;
David S. Miller92d86822011-02-04 15:55:25 -08001667 bool send;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 int code;
1669
Changli Gaod8d1f302010-06-10 23:31:35 -07001670 switch (rt->dst.error) {
Joe Perches4500ebf2011-07-01 09:43:07 +00001671 case EINVAL:
1672 default:
1673 goto out;
1674 case EHOSTUNREACH:
1675 code = ICMP_HOST_UNREACH;
1676 break;
1677 case ENETUNREACH:
1678 code = ICMP_NET_UNREACH;
1679 IP_INC_STATS_BH(dev_net(rt->dst.dev),
1680 IPSTATS_MIB_INNOROUTES);
1681 break;
1682 case EACCES:
1683 code = ICMP_PKT_FILTERED;
1684 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686
David S. Miller92d86822011-02-04 15:55:25 -08001687 if (!rt->peer)
David S. Millera48eff12011-05-18 18:42:43 -04001688 rt_bind_peer(rt, rt->rt_dst, 1);
David S. Miller92d86822011-02-04 15:55:25 -08001689 peer = rt->peer;
1690
1691 send = true;
1692 if (peer) {
1693 now = jiffies;
1694 peer->rate_tokens += now - peer->rate_last;
1695 if (peer->rate_tokens > ip_rt_error_burst)
1696 peer->rate_tokens = ip_rt_error_burst;
1697 peer->rate_last = now;
1698 if (peer->rate_tokens >= ip_rt_error_cost)
1699 peer->rate_tokens -= ip_rt_error_cost;
1700 else
1701 send = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
David S. Miller92d86822011-02-04 15:55:25 -08001703 if (send)
1704 icmp_send(skb, ICMP_DEST_UNREACH, code, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706out: kfree_skb(skb);
1707 return 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001708}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710/*
1711 * The last two values are not from the RFC but
1712 * are needed for AMPRnet AX.25 paths.
1713 */
1714
Arjan van de Ven9b5b5cf2005-11-29 16:21:38 -08001715static const unsigned short mtu_plateau[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{32000, 17914, 8166, 4352, 2002, 1492, 576, 296, 216, 128 };
1717
Stephen Hemminger5969f712008-04-10 01:52:09 -07001718static inline unsigned short guess_mtu(unsigned short old_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 for (i = 0; i < ARRAY_SIZE(mtu_plateau); i++)
1723 if (old_mtu > mtu_plateau[i])
1724 return mtu_plateau[i];
1725 return 68;
1726}
1727
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001728unsigned short ip_rt_frag_needed(struct net *net, const struct iphdr *iph,
Timo Teras0010e462008-04-29 03:32:25 -07001729 unsigned short new_mtu,
1730 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 unsigned short old_mtu = ntohs(iph->tot_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 unsigned short est_mtu = 0;
David S. Miller2c8cec52011-02-09 20:42:07 -08001734 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
David S. Miller2c8cec52011-02-09 20:42:07 -08001736 peer = inet_getpeer_v4(iph->daddr, 1);
1737 if (peer) {
1738 unsigned short mtu = new_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
David S. Miller2c8cec52011-02-09 20:42:07 -08001740 if (new_mtu < 68 || new_mtu >= old_mtu) {
1741 /* BSD 4.2 derived systems incorrectly adjust
1742 * tot_len by the IP header length, and report
1743 * a zero MTU in the ICMP message.
1744 */
1745 if (mtu == 0 &&
1746 old_mtu >= 68 + (iph->ihl << 2))
1747 old_mtu -= iph->ihl << 2;
1748 mtu = guess_mtu(old_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
David S. Miller2c8cec52011-02-09 20:42:07 -08001750
1751 if (mtu < ip_rt_min_pmtu)
1752 mtu = ip_rt_min_pmtu;
1753 if (!peer->pmtu_expires || mtu < peer->pmtu_learned) {
Hiroaki SHIMODA46af3182011-03-09 20:09:58 +00001754 unsigned long pmtu_expires;
1755
1756 pmtu_expires = jiffies + ip_rt_mtu_expires;
1757 if (!pmtu_expires)
1758 pmtu_expires = 1UL;
1759
David S. Miller2c8cec52011-02-09 20:42:07 -08001760 est_mtu = mtu;
1761 peer->pmtu_learned = mtu;
Hiroaki SHIMODA46af3182011-03-09 20:09:58 +00001762 peer->pmtu_expires = pmtu_expires;
Gao feng59445b62011-10-19 15:34:09 +00001763 atomic_inc(&__rt_peer_genid);
David S. Miller2c8cec52011-02-09 20:42:07 -08001764 }
1765
1766 inet_putpeer(peer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768 return est_mtu ? : new_mtu;
1769}
1770
David S. Miller2c8cec52011-02-09 20:42:07 -08001771static void check_peer_pmtu(struct dst_entry *dst, struct inet_peer *peer)
1772{
Eric Dumazetfe6fe792011-06-08 06:07:07 +00001773 unsigned long expires = ACCESS_ONCE(peer->pmtu_expires);
David S. Miller2c8cec52011-02-09 20:42:07 -08001774
Eric Dumazetfe6fe792011-06-08 06:07:07 +00001775 if (!expires)
1776 return;
Hiroaki SHIMODA46af3182011-03-09 20:09:58 +00001777 if (time_before(jiffies, expires)) {
David S. Miller2c8cec52011-02-09 20:42:07 -08001778 u32 orig_dst_mtu = dst_mtu(dst);
1779 if (peer->pmtu_learned < orig_dst_mtu) {
1780 if (!peer->pmtu_orig)
1781 peer->pmtu_orig = dst_metric_raw(dst, RTAX_MTU);
1782 dst_metric_set(dst, RTAX_MTU, peer->pmtu_learned);
1783 }
1784 } else if (cmpxchg(&peer->pmtu_expires, expires, 0) == expires)
1785 dst_metric_set(dst, RTAX_MTU, peer->pmtu_orig);
1786}
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788static void ip_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
1789{
David S. Miller2c8cec52011-02-09 20:42:07 -08001790 struct rtable *rt = (struct rtable *) dst;
1791 struct inet_peer *peer;
1792
1793 dst_confirm(dst);
1794
1795 if (!rt->peer)
David S. Millera48eff12011-05-18 18:42:43 -04001796 rt_bind_peer(rt, rt->rt_dst, 1);
David S. Miller2c8cec52011-02-09 20:42:07 -08001797 peer = rt->peer;
1798 if (peer) {
Eric Dumazetfe6fe792011-06-08 06:07:07 +00001799 unsigned long pmtu_expires = ACCESS_ONCE(peer->pmtu_expires);
1800
David S. Miller2c8cec52011-02-09 20:42:07 -08001801 if (mtu < ip_rt_min_pmtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 mtu = ip_rt_min_pmtu;
Eric Dumazetfe6fe792011-06-08 06:07:07 +00001803 if (!pmtu_expires || mtu < peer->pmtu_learned) {
Hiroaki SHIMODA46af3182011-03-09 20:09:58 +00001804
1805 pmtu_expires = jiffies + ip_rt_mtu_expires;
1806 if (!pmtu_expires)
1807 pmtu_expires = 1UL;
1808
David S. Miller2c8cec52011-02-09 20:42:07 -08001809 peer->pmtu_learned = mtu;
Hiroaki SHIMODA46af3182011-03-09 20:09:58 +00001810 peer->pmtu_expires = pmtu_expires;
David S. Miller2c8cec52011-02-09 20:42:07 -08001811
1812 atomic_inc(&__rt_peer_genid);
1813 rt->rt_peer_genid = rt_peer_genid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
Hiroaki SHIMODA46af3182011-03-09 20:09:58 +00001815 check_peer_pmtu(dst, peer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817}
1818
David S. Millerf39925d2011-02-09 22:00:16 -08001819
David S. Millerde398fb2011-12-05 13:21:42 -05001820static void ipv4_validate_peer(struct rtable *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
David S. Miller6431cbc2011-02-07 20:38:06 -08001822 if (rt->rt_peer_genid != rt_peer_genid()) {
David S. Miller2c8cec52011-02-09 20:42:07 -08001823 struct inet_peer *peer;
1824
David S. Miller6431cbc2011-02-07 20:38:06 -08001825 if (!rt->peer)
David S. Millera48eff12011-05-18 18:42:43 -04001826 rt_bind_peer(rt, rt->rt_dst, 0);
David S. Miller6431cbc2011-02-07 20:38:06 -08001827
David S. Miller2c8cec52011-02-09 20:42:07 -08001828 peer = rt->peer;
Eric Dumazetfe6fe792011-06-08 06:07:07 +00001829 if (peer) {
David S. Millerefbc3682011-12-01 13:38:59 -05001830 check_peer_pmtu(&rt->dst, peer);
David S. Miller2c8cec52011-02-09 20:42:07 -08001831
Eric Dumazetfe6fe792011-06-08 06:07:07 +00001832 if (peer->redirect_learned.a4 &&
David S. Millerde398fb2011-12-05 13:21:42 -05001833 peer->redirect_learned.a4 != rt->rt_gateway)
1834 check_peer_redir(&rt->dst, peer);
David S. Millerf39925d2011-02-09 22:00:16 -08001835 }
1836
David S. Miller6431cbc2011-02-07 20:38:06 -08001837 rt->rt_peer_genid = rt_peer_genid();
1838 }
David S. Millerefbc3682011-12-01 13:38:59 -05001839}
1840
1841static struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie)
1842{
1843 struct rtable *rt = (struct rtable *) dst;
1844
1845 if (rt_is_expired(rt))
1846 return NULL;
David S. Millerde398fb2011-12-05 13:21:42 -05001847 ipv4_validate_peer(rt);
Timo Teräsd11a4dc2010-03-18 23:20:20 +00001848 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
1851static void ipv4_dst_destroy(struct dst_entry *dst)
1852{
1853 struct rtable *rt = (struct rtable *) dst;
1854 struct inet_peer *peer = rt->peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
David S. Miller62fa8a82011-01-26 20:51:05 -08001856 if (rt->fi) {
1857 fib_info_put(rt->fi);
1858 rt->fi = NULL;
1859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (peer) {
1861 rt->peer = NULL;
1862 inet_putpeer(peer);
1863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867static void ipv4_link_failure(struct sk_buff *skb)
1868{
1869 struct rtable *rt;
1870
1871 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
1872
Eric Dumazet511c3f92009-06-02 05:14:27 +00001873 rt = skb_rtable(skb);
Eric Dumazetfe6fe792011-06-08 06:07:07 +00001874 if (rt && rt->peer && peer_pmtu_cleaned(rt->peer))
1875 dst_metric_set(&rt->dst, RTAX_MTU, rt->peer->pmtu_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
1878static int ip_rt_bug(struct sk_buff *skb)
1879{
Harvey Harrison673d57e2008-10-31 00:53:57 -07001880 printk(KERN_DEBUG "ip_rt_bug: %pI4 -> %pI4, %s\n",
1881 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 skb->dev ? skb->dev->name : "?");
1883 kfree_skb(skb);
Dave Jonesc378a9c2011-05-21 07:16:42 +00001884 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return 0;
1886}
1887
1888/*
1889 We do not cache source address of outgoing interface,
1890 because it is used only by IP RR, TS and SRR options,
1891 so that it out of fast path.
1892
1893 BTW remember: "addr" is allowed to be not aligned
1894 in IP options!
1895 */
1896
David S. Miller8e363602011-05-13 17:29:41 -04001897void ip_rt_get_source(u8 *addr, struct sk_buff *skb, struct rtable *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Al Viroa61ced52006-09-26 21:27:54 -07001899 __be32 src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
David S. Millerc7537962010-11-11 17:07:48 -08001901 if (rt_is_output_route(rt))
David S. Millerc5be24f2011-05-13 18:01:21 -04001902 src = ip_hdr(skb)->saddr;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001903 else {
David S. Miller8e363602011-05-13 17:29:41 -04001904 struct fib_result res;
1905 struct flowi4 fl4;
1906 struct iphdr *iph;
1907
1908 iph = ip_hdr(skb);
1909
1910 memset(&fl4, 0, sizeof(fl4));
1911 fl4.daddr = iph->daddr;
1912 fl4.saddr = iph->saddr;
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001913 fl4.flowi4_tos = RT_TOS(iph->tos);
David S. Miller8e363602011-05-13 17:29:41 -04001914 fl4.flowi4_oif = rt->dst.dev->ifindex;
1915 fl4.flowi4_iif = skb->dev->ifindex;
1916 fl4.flowi4_mark = skb->mark;
Lorenzo Colitti462ce7c2014-03-31 16:23:51 +09001917 fl4.flowi4_uid = skb->sk ? sock_i_uid(skb->sk) : 0;
David S. Miller5e2b61f2011-03-04 21:47:09 -08001918
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001919 rcu_read_lock();
David S. Miller68a5e3d2011-03-11 20:07:33 -05001920 if (fib_lookup(dev_net(rt->dst.dev), &fl4, &res) == 0)
David S. Miller436c3b62011-03-24 17:42:21 -07001921 src = FIB_RES_PREFSRC(dev_net(rt->dst.dev), res);
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001922 else
1923 src = inet_select_addr(rt->dst.dev, rt->rt_gateway,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 RT_SCOPE_UNIVERSE);
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00001925 rcu_read_unlock();
1926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 memcpy(addr, &src, 4);
1928}
1929
Patrick McHardyc7066f72011-01-14 13:36:42 +01001930#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931static void set_class_tag(struct rtable *rt, u32 tag)
1932{
Changli Gaod8d1f302010-06-10 23:31:35 -07001933 if (!(rt->dst.tclassid & 0xFFFF))
1934 rt->dst.tclassid |= tag & 0xFFFF;
1935 if (!(rt->dst.tclassid & 0xFFFF0000))
1936 rt->dst.tclassid |= tag & 0xFFFF0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938#endif
1939
David S. Miller0dbaee32010-12-13 12:52:14 -08001940static unsigned int ipv4_default_advmss(const struct dst_entry *dst)
1941{
1942 unsigned int advmss = dst_metric_raw(dst, RTAX_ADVMSS);
1943
1944 if (advmss == 0) {
1945 advmss = max_t(unsigned int, dst->dev->mtu - 40,
1946 ip_rt_min_advmss);
1947 if (advmss > 65535 - 40)
1948 advmss = 65535 - 40;
1949 }
1950 return advmss;
1951}
1952
Steffen Klassertebb762f2011-11-23 02:12:51 +00001953static unsigned int ipv4_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08001954{
Steffen Klassert261663b2011-11-23 02:14:50 +00001955 const struct rtable *rt = (const struct rtable *) dst;
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001956 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1957
Steffen Klassert261663b2011-11-23 02:14:50 +00001958 if (mtu && rt_is_output_route(rt))
Steffen Klassert618f9bc2011-11-23 02:13:31 +00001959 return mtu;
1960
1961 mtu = dst->dev->mtu;
David S. Millerd33e4552010-12-14 13:01:14 -08001962
1963 if (unlikely(dst_metric_locked(dst, RTAX_MTU))) {
David S. Millerd33e4552010-12-14 13:01:14 -08001964
1965 if (rt->rt_gateway != rt->rt_dst && mtu > 576)
1966 mtu = 576;
1967 }
1968
1969 if (mtu > IP_MAX_MTU)
1970 mtu = IP_MAX_MTU;
1971
1972 return mtu;
1973}
1974
David S. Miller813b3b52011-04-28 14:48:42 -07001975static void rt_init_metrics(struct rtable *rt, const struct flowi4 *fl4,
David S. Miller5e2b61f2011-03-04 21:47:09 -08001976 struct fib_info *fi)
David S. Millera4daad62011-01-27 22:01:53 -08001977{
David S. Miller0131ba42011-02-04 14:37:30 -08001978 struct inet_peer *peer;
1979 int create = 0;
1980
1981 /* If a peer entry exists for this destination, we must hook
1982 * it up in order to get at cached metrics.
1983 */
David S. Miller813b3b52011-04-28 14:48:42 -07001984 if (fl4 && (fl4->flowi4_flags & FLOWI_FLAG_PRECOW_METRICS))
David S. Miller0131ba42011-02-04 14:37:30 -08001985 create = 1;
1986
David S. Miller3c0afdc2011-03-04 21:26:07 -08001987 rt->peer = peer = inet_getpeer_v4(rt->rt_dst, create);
David S. Miller0131ba42011-02-04 14:37:30 -08001988 if (peer) {
David S. Miller3c0afdc2011-03-04 21:26:07 -08001989 rt->rt_peer_genid = rt_peer_genid();
David S. Miller0131ba42011-02-04 14:37:30 -08001990 if (inet_metrics_new(peer))
1991 memcpy(peer->metrics, fi->fib_metrics,
1992 sizeof(u32) * RTAX_MAX);
1993 dst_init_metrics(&rt->dst, peer->metrics, false);
David S. Miller2c8cec52011-02-09 20:42:07 -08001994
Eric Dumazetfe6fe792011-06-08 06:07:07 +00001995 check_peer_pmtu(&rt->dst, peer);
Steffen Klassertac3f48d2012-03-06 21:21:10 +00001996
David S. Millerf39925d2011-02-09 22:00:16 -08001997 if (peer->redirect_learned.a4 &&
1998 peer->redirect_learned.a4 != rt->rt_gateway) {
1999 rt->rt_gateway = peer->redirect_learned.a4;
2000 rt->rt_flags |= RTCF_REDIRECTED;
2001 }
David S. Miller0131ba42011-02-04 14:37:30 -08002002 } else {
David S. Millerb8dad612011-01-28 14:07:16 -08002003 if (fi->fib_metrics != (u32 *) dst_default_metrics) {
2004 rt->fi = fi;
2005 atomic_inc(&fi->fib_clntref);
2006 }
David S. Millera4daad62011-01-27 22:01:53 -08002007 dst_init_metrics(&rt->dst, fi->fib_metrics, true);
David S. Millera4daad62011-01-27 22:01:53 -08002008 }
2009}
2010
David S. Miller813b3b52011-04-28 14:48:42 -07002011static void rt_set_nexthop(struct rtable *rt, const struct flowi4 *fl4,
David S. Miller5e2b61f2011-03-04 21:47:09 -08002012 const struct fib_result *res,
David S. Miller982721f2011-02-16 21:44:24 -08002013 struct fib_info *fi, u16 type, u32 itag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
David S. Millerdefb3512010-12-08 21:16:57 -08002015 struct dst_entry *dst = &rt->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 if (fi) {
2018 if (FIB_RES_GW(*res) &&
2019 FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
2020 rt->rt_gateway = FIB_RES_GW(*res);
David S. Miller813b3b52011-04-28 14:48:42 -07002021 rt_init_metrics(rt, fl4, fi);
Patrick McHardyc7066f72011-01-14 13:36:42 +01002022#ifdef CONFIG_IP_ROUTE_CLASSID
David S. Millerdefb3512010-12-08 21:16:57 -08002023 dst->tclassid = FIB_RES_NH(*res).nh_tclassid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024#endif
David S. Millerd33e4552010-12-14 13:01:14 -08002025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
David S. Millerdefb3512010-12-08 21:16:57 -08002027 if (dst_mtu(dst) > IP_MAX_MTU)
2028 dst_metric_set(dst, RTAX_MTU, IP_MAX_MTU);
David S. Miller0dbaee32010-12-13 12:52:14 -08002029 if (dst_metric_raw(dst, RTAX_ADVMSS) > 65535 - 40)
David S. Millerdefb3512010-12-08 21:16:57 -08002030 dst_metric_set(dst, RTAX_ADVMSS, 65535 - 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Patrick McHardyc7066f72011-01-14 13:36:42 +01002032#ifdef CONFIG_IP_ROUTE_CLASSID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033#ifdef CONFIG_IP_MULTIPLE_TABLES
2034 set_class_tag(rt, fib_rules_tclass(res));
2035#endif
2036 set_class_tag(rt, itag);
2037#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002040static struct rtable *rt_dst_alloc(struct net_device *dev,
2041 bool nopolicy, bool noxfrm)
David S. Miller0c4dcd52011-02-17 15:42:37 -08002042{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002043 return dst_alloc(&ipv4_dst_ops, dev, 1, -1,
2044 DST_HOST |
2045 (nopolicy ? DST_NOPOLICY : 0) |
2046 (noxfrm ? DST_NOXFRM : 0));
David S. Miller0c4dcd52011-02-17 15:42:37 -08002047}
2048
Eric Dumazet96d36222010-06-02 19:21:31 +00002049/* called in rcu_read_lock() section */
Al Viro9e12bb22006-09-26 21:25:20 -07002050static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 u8 tos, struct net_device *dev, int our)
2052{
Eric Dumazet96d36222010-06-02 19:21:31 +00002053 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 struct rtable *rth;
Al Viroa61ced52006-09-26 21:27:54 -07002055 __be32 spec_dst;
Eric Dumazet96d36222010-06-02 19:21:31 +00002056 struct in_device *in_dev = __in_dev_get_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 u32 itag = 0;
Eric Dumazetb5f7e752010-06-02 12:05:27 +00002058 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 /* Primary sanity checks. */
2061
2062 if (in_dev == NULL)
2063 return -EINVAL;
2064
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002065 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
Joe Perchesf97c1e02007-12-16 13:45:43 -08002066 ipv4_is_loopback(saddr) || skb->protocol != htons(ETH_P_IP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 goto e_inval;
2068
Joe Perchesf97c1e02007-12-16 13:45:43 -08002069 if (ipv4_is_zeronet(saddr)) {
2070 if (!ipv4_is_local_multicast(daddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 goto e_inval;
2072 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
Eric Dumazetb5f7e752010-06-02 12:05:27 +00002073 } else {
Michael Smith5c04c812011-04-07 04:51:50 +00002074 err = fib_validate_source(skb, saddr, 0, tos, 0, dev, &spec_dst,
2075 &itag);
Eric Dumazetb5f7e752010-06-02 12:05:27 +00002076 if (err < 0)
2077 goto e_err;
2078 }
Benjamin LaHaise4e7b2f12012-03-27 15:55:32 +00002079 rth = rt_dst_alloc(dev_net(dev)->loopback_dev,
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002080 IN_DEV_CONF_GET(in_dev, NOPOLICY), false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (!rth)
2082 goto e_nobufs;
2083
Patrick McHardyc7066f72011-01-14 13:36:42 +01002084#ifdef CONFIG_IP_ROUTE_CLASSID
Changli Gaod8d1f302010-06-10 23:31:35 -07002085 rth->dst.tclassid = itag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086#endif
David S. Millercf911662011-04-28 14:31:47 -07002087 rth->dst.output = ip_rt_bug;
2088
2089 rth->rt_key_dst = daddr;
2090 rth->rt_key_src = saddr;
Denis V. Luneve84f84f2008-07-05 19:04:32 -07002091 rth->rt_genid = rt_genid(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 rth->rt_flags = RTCF_MULTICAST;
Eric Dumazet29e75252008-01-31 17:05:09 -08002093 rth->rt_type = RTN_MULTICAST;
David S. Miller475949d2011-05-03 19:45:15 -07002094 rth->rt_key_tos = tos;
David S. Millercf911662011-04-28 14:31:47 -07002095 rth->rt_dst = daddr;
2096 rth->rt_src = saddr;
2097 rth->rt_route_iif = dev->ifindex;
2098 rth->rt_iif = dev->ifindex;
2099 rth->rt_oif = 0;
2100 rth->rt_mark = skb->mark;
Lorenzo Colitti462ce7c2014-03-31 16:23:51 +09002101 rth->rt_uid = 0;
David S. Millercf911662011-04-28 14:31:47 -07002102 rth->rt_gateway = daddr;
2103 rth->rt_spec_dst= spec_dst;
2104 rth->rt_peer_genid = 0;
2105 rth->peer = NULL;
2106 rth->fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (our) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002108 rth->dst.input= ip_local_deliver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 rth->rt_flags |= RTCF_LOCAL;
2110 }
2111
2112#ifdef CONFIG_IP_MROUTE
Joe Perchesf97c1e02007-12-16 13:45:43 -08002113 if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev))
Changli Gaod8d1f302010-06-10 23:31:35 -07002114 rth->dst.input = ip_mr_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115#endif
2116 RT_CACHE_STAT_INC(in_slow_mc);
2117
Denis V. Luneve84f84f2008-07-05 19:04:32 -07002118 hash = rt_hash(daddr, saddr, dev->ifindex, rt_genid(dev_net(dev)));
David S. Millerb23dd4f2011-03-02 14:31:35 -08002119 rth = rt_intern_hash(hash, rth, skb, dev->ifindex);
Eric Dumazet9aa3c942011-06-18 11:59:18 -07002120 return IS_ERR(rth) ? PTR_ERR(rth) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122e_nobufs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124e_inval:
Eric Dumazet96d36222010-06-02 19:21:31 +00002125 return -EINVAL;
Eric Dumazetb5f7e752010-06-02 12:05:27 +00002126e_err:
Eric Dumazetb5f7e752010-06-02 12:05:27 +00002127 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
2130
2131static void ip_handle_martian_source(struct net_device *dev,
2132 struct in_device *in_dev,
2133 struct sk_buff *skb,
Al Viro9e12bb22006-09-26 21:25:20 -07002134 __be32 daddr,
2135 __be32 saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
2137 RT_CACHE_STAT_INC(in_martian_src);
2138#ifdef CONFIG_IP_ROUTE_VERBOSE
2139 if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit()) {
2140 /*
2141 * RFC1812 recommendation, if source is martian,
2142 * the only hint is MAC header.
2143 */
Joe Perches058bd4d2012-03-11 18:36:11 +00002144 pr_warn("martian source %pI4 from %pI4, on dev %s\n",
Harvey Harrison673d57e2008-10-31 00:53:57 -07002145 &daddr, &saddr, dev->name);
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002146 if (dev->hard_header_len && skb_mac_header_was_set(skb)) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002147 print_hex_dump(KERN_WARNING, "ll header: ",
2148 DUMP_PREFIX_OFFSET, 16, 1,
2149 skb_mac_header(skb),
2150 dev->hard_header_len, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
2152 }
2153#endif
2154}
2155
Eric Dumazet47360222010-06-03 04:13:21 +00002156/* called in rcu_read_lock() section */
Stephen Hemminger5969f712008-04-10 01:52:09 -07002157static int __mkroute_input(struct sk_buff *skb,
David S. Miller982721f2011-02-16 21:44:24 -08002158 const struct fib_result *res,
Stephen Hemminger5969f712008-04-10 01:52:09 -07002159 struct in_device *in_dev,
2160 __be32 daddr, __be32 saddr, u32 tos,
2161 struct rtable **result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 struct rtable *rth;
2164 int err;
2165 struct in_device *out_dev;
Eric Dumazet47360222010-06-03 04:13:21 +00002166 unsigned int flags = 0;
Al Virod9c9df82006-09-26 21:28:14 -07002167 __be32 spec_dst;
Li RongQing62e1a642014-05-22 16:36:55 +08002168 u32 itag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 /* get a working reference to the output device */
Eric Dumazet47360222010-06-03 04:13:21 +00002171 out_dev = __in_dev_get_rcu(FIB_RES_DEV(*res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (out_dev == NULL) {
2173 if (net_ratelimit())
Joe Perches058bd4d2012-03-11 18:36:11 +00002174 pr_crit("Bug in ip_route_input_slow(). Please report.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return -EINVAL;
2176 }
2177
2178
Michael Smith5c04c812011-04-07 04:51:50 +00002179 err = fib_validate_source(skb, saddr, daddr, tos, FIB_RES_OIF(*res),
2180 in_dev->dev, &spec_dst, &itag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002182 ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 saddr);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 goto cleanup;
2186 }
2187
2188 if (err)
2189 flags |= RTCF_DIRECTSRC;
2190
Thomas Graf51b77ca2008-06-03 16:36:01 -07002191 if (out_dev == in_dev && err &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 (IN_DEV_SHARED_MEDIA(out_dev) ||
2193 inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
2194 flags |= RTCF_DOREDIRECT;
2195
2196 if (skb->protocol != htons(ETH_P_IP)) {
2197 /* Not IP (i.e. ARP). Do not create route, if it is
2198 * invalid for proxy arp. DNAT routes are always valid.
Jesper Dangaard Brouer65324142010-01-05 05:50:47 +00002199 *
2200 * Proxy arp feature have been extended to allow, ARP
2201 * replies back to the same interface, to support
2202 * Private VLAN switch technologies. See arp.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 */
Jesper Dangaard Brouer65324142010-01-05 05:50:47 +00002204 if (out_dev == in_dev &&
2205 IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 err = -EINVAL;
2207 goto cleanup;
2208 }
2209 }
2210
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002211 rth = rt_dst_alloc(out_dev->dev,
2212 IN_DEV_CONF_GET(in_dev, NOPOLICY),
David S. Miller0c4dcd52011-02-17 15:42:37 -08002213 IN_DEV_CONF_GET(out_dev, NOXFRM));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (!rth) {
2215 err = -ENOBUFS;
2216 goto cleanup;
2217 }
2218
David S. Miller5e2b61f2011-03-04 21:47:09 -08002219 rth->rt_key_dst = daddr;
David S. Miller5e2b61f2011-03-04 21:47:09 -08002220 rth->rt_key_src = saddr;
David S. Millercf911662011-04-28 14:31:47 -07002221 rth->rt_genid = rt_genid(dev_net(rth->dst.dev));
2222 rth->rt_flags = flags;
2223 rth->rt_type = res->type;
David S. Miller475949d2011-05-03 19:45:15 -07002224 rth->rt_key_tos = tos;
David S. Millercf911662011-04-28 14:31:47 -07002225 rth->rt_dst = daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 rth->rt_src = saddr;
OGAWA Hirofumi1b86a582011-04-07 14:04:08 -07002227 rth->rt_route_iif = in_dev->dev->ifindex;
David S. Miller5e2b61f2011-03-04 21:47:09 -08002228 rth->rt_iif = in_dev->dev->ifindex;
David S. Miller5e2b61f2011-03-04 21:47:09 -08002229 rth->rt_oif = 0;
David S. Millercf911662011-04-28 14:31:47 -07002230 rth->rt_mark = skb->mark;
Lorenzo Colitti462ce7c2014-03-31 16:23:51 +09002231 rth->rt_uid = 0;
David S. Millercf911662011-04-28 14:31:47 -07002232 rth->rt_gateway = daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 rth->rt_spec_dst= spec_dst;
David S. Millercf911662011-04-28 14:31:47 -07002234 rth->rt_peer_genid = 0;
2235 rth->peer = NULL;
2236 rth->fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Changli Gaod8d1f302010-06-10 23:31:35 -07002238 rth->dst.input = ip_forward;
2239 rth->dst.output = ip_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
David S. Miller5e2b61f2011-03-04 21:47:09 -08002241 rt_set_nexthop(rth, NULL, res, res->fi, res->type, itag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 *result = rth;
2244 err = 0;
2245 cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return err;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Stephen Hemminger5969f712008-04-10 01:52:09 -07002249static int ip_mkroute_input(struct sk_buff *skb,
2250 struct fib_result *res,
David S. Miller68a5e3d2011-03-11 20:07:33 -05002251 const struct flowi4 *fl4,
Stephen Hemminger5969f712008-04-10 01:52:09 -07002252 struct in_device *in_dev,
2253 __be32 daddr, __be32 saddr, u32 tos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Chuck Short7abaa272005-06-22 22:10:23 -07002255 struct rtable* rth = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 int err;
2257 unsigned hash;
2258
2259#ifdef CONFIG_IP_ROUTE_MULTIPATH
David S. Millerff3fccb2011-03-10 16:23:24 -08002260 if (res->fi && res->fi->fib_nhs > 1)
David S. Miller1b7fe5932011-03-10 17:01:16 -08002261 fib_select_multipath(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262#endif
2263
2264 /* create a routing cache entry */
2265 err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth);
2266 if (err)
2267 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 /* put it into the cache */
David S. Miller68a5e3d2011-03-11 20:07:33 -05002270 hash = rt_hash(daddr, saddr, fl4->flowi4_iif,
Changli Gaod8d1f302010-06-10 23:31:35 -07002271 rt_genid(dev_net(rth->dst.dev)));
David S. Miller68a5e3d2011-03-11 20:07:33 -05002272 rth = rt_intern_hash(hash, rth, skb, fl4->flowi4_iif);
David S. Millerb23dd4f2011-03-02 14:31:35 -08002273 if (IS_ERR(rth))
2274 return PTR_ERR(rth);
2275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278/*
2279 * NOTE. We drop all the packets that has local source
2280 * addresses, because every properly looped back packet
2281 * must have correct destination already attached by output routine.
2282 *
2283 * Such approach solves two big problems:
2284 * 1. Not simplex devices are handled properly.
2285 * 2. IP spoofing attempts are filtered with 100% of guarantee.
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00002286 * called with rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 */
2288
Al Viro9e12bb22006-09-26 21:25:20 -07002289static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 u8 tos, struct net_device *dev)
2291{
2292 struct fib_result res;
Eric Dumazet96d36222010-06-02 19:21:31 +00002293 struct in_device *in_dev = __in_dev_get_rcu(dev);
David S. Miller68a5e3d2011-03-11 20:07:33 -05002294 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 unsigned flags = 0;
2296 u32 itag = 0;
2297 struct rtable * rth;
2298 unsigned hash;
Al Viro9e12bb22006-09-26 21:25:20 -07002299 __be32 spec_dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 int err = -EINVAL;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002301 struct net * net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 /* IP on this device is disabled. */
2304
2305 if (!in_dev)
2306 goto out;
2307
2308 /* Check for the most weird martians, which can be not detected
2309 by fib_lookup.
2310 */
2311
Jan Engelhardt1e637c72008-01-21 03:18:08 -08002312 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
Joe Perchesf97c1e02007-12-16 13:45:43 -08002313 ipv4_is_loopback(saddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 goto martian_source;
2315
Andy Walls27a954b2010-10-17 15:11:22 +00002316 if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 goto brd_input;
2318
2319 /* Accept zero addresses only to limited broadcast;
2320 * I even do not know to fix it or not. Waiting for complains :-)
2321 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08002322 if (ipv4_is_zeronet(saddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 goto martian_source;
2324
Andy Walls27a954b2010-10-17 15:11:22 +00002325 if (ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 goto martian_destination;
2327
2328 /*
2329 * Now we are ready to route packet.
2330 */
David S. Miller68a5e3d2011-03-11 20:07:33 -05002331 fl4.flowi4_oif = 0;
2332 fl4.flowi4_iif = dev->ifindex;
2333 fl4.flowi4_mark = skb->mark;
2334 fl4.flowi4_tos = tos;
2335 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
2336 fl4.daddr = daddr;
2337 fl4.saddr = saddr;
2338 err = fib_lookup(net, &fl4, &res);
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00002339 if (err != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (!IN_DEV_FORWARD(in_dev))
Dietmar Eggemann2c2910a2005-06-28 13:06:23 -07002341 goto e_hostunreach;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 goto no_route;
2343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 RT_CACHE_STAT_INC(in_slow_tot);
2346
2347 if (res.type == RTN_BROADCAST)
2348 goto brd_input;
2349
2350 if (res.type == RTN_LOCAL) {
Michael Smith5c04c812011-04-07 04:51:50 +00002351 err = fib_validate_source(skb, saddr, daddr, tos,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00002352 net->loopback_dev->ifindex,
Michael Smith5c04c812011-04-07 04:51:50 +00002353 dev, &spec_dst, &itag);
Eric Dumazetb5f7e752010-06-02 12:05:27 +00002354 if (err < 0)
2355 goto martian_source_keep_err;
2356 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 flags |= RTCF_DIRECTSRC;
2358 spec_dst = daddr;
2359 goto local_input;
2360 }
2361
2362 if (!IN_DEV_FORWARD(in_dev))
Dietmar Eggemann2c2910a2005-06-28 13:06:23 -07002363 goto e_hostunreach;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (res.type != RTN_UNICAST)
2365 goto martian_destination;
2366
David S. Miller68a5e3d2011-03-11 20:07:33 -05002367 err = ip_mkroute_input(skb, &res, &fl4, in_dev, daddr, saddr, tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368out: return err;
2369
2370brd_input:
2371 if (skb->protocol != htons(ETH_P_IP))
2372 goto e_inval;
2373
Joe Perchesf97c1e02007-12-16 13:45:43 -08002374 if (ipv4_is_zeronet(saddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
2376 else {
Michael Smith5c04c812011-04-07 04:51:50 +00002377 err = fib_validate_source(skb, saddr, 0, tos, 0, dev, &spec_dst,
2378 &itag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (err < 0)
Eric Dumazetb5f7e752010-06-02 12:05:27 +00002380 goto martian_source_keep_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (err)
2382 flags |= RTCF_DIRECTSRC;
2383 }
2384 flags |= RTCF_BROADCAST;
2385 res.type = RTN_BROADCAST;
2386 RT_CACHE_STAT_INC(in_brd);
2387
2388local_input:
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002389 rth = rt_dst_alloc(net->loopback_dev,
2390 IN_DEV_CONF_GET(in_dev, NOPOLICY), false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (!rth)
2392 goto e_nobufs;
2393
David S. Millercf911662011-04-28 14:31:47 -07002394 rth->dst.input= ip_local_deliver;
Changli Gaod8d1f302010-06-10 23:31:35 -07002395 rth->dst.output= ip_rt_bug;
David S. Millercf911662011-04-28 14:31:47 -07002396#ifdef CONFIG_IP_ROUTE_CLASSID
2397 rth->dst.tclassid = itag;
2398#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
David S. Miller5e2b61f2011-03-04 21:47:09 -08002400 rth->rt_key_dst = daddr;
David S. Miller5e2b61f2011-03-04 21:47:09 -08002401 rth->rt_key_src = saddr;
David S. Millercf911662011-04-28 14:31:47 -07002402 rth->rt_genid = rt_genid(net);
2403 rth->rt_flags = flags|RTCF_LOCAL;
2404 rth->rt_type = res.type;
David S. Miller475949d2011-05-03 19:45:15 -07002405 rth->rt_key_tos = tos;
David S. Millercf911662011-04-28 14:31:47 -07002406 rth->rt_dst = daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 rth->rt_src = saddr;
Patrick McHardyc7066f72011-01-14 13:36:42 +01002408#ifdef CONFIG_IP_ROUTE_CLASSID
Changli Gaod8d1f302010-06-10 23:31:35 -07002409 rth->dst.tclassid = itag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410#endif
OGAWA Hirofumi1b86a582011-04-07 14:04:08 -07002411 rth->rt_route_iif = dev->ifindex;
David S. Miller5e2b61f2011-03-04 21:47:09 -08002412 rth->rt_iif = dev->ifindex;
David S. Millercf911662011-04-28 14:31:47 -07002413 rth->rt_oif = 0;
2414 rth->rt_mark = skb->mark;
Lorenzo Colitti462ce7c2014-03-31 16:23:51 +09002415 rth->rt_uid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 rth->rt_gateway = daddr;
2417 rth->rt_spec_dst= spec_dst;
David S. Millercf911662011-04-28 14:31:47 -07002418 rth->rt_peer_genid = 0;
2419 rth->peer = NULL;
2420 rth->fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if (res.type == RTN_UNREACHABLE) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002422 rth->dst.input= ip_error;
2423 rth->dst.error= -err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 rth->rt_flags &= ~RTCF_LOCAL;
2425 }
David S. Miller68a5e3d2011-03-11 20:07:33 -05002426 hash = rt_hash(daddr, saddr, fl4.flowi4_iif, rt_genid(net));
2427 rth = rt_intern_hash(hash, rth, skb, fl4.flowi4_iif);
David S. Millerb23dd4f2011-03-02 14:31:35 -08002428 err = 0;
2429 if (IS_ERR(rth))
2430 err = PTR_ERR(rth);
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00002431 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433no_route:
2434 RT_CACHE_STAT_INC(in_no_route);
2435 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
2436 res.type = RTN_UNREACHABLE;
Mitsuru Chinen7f538782007-12-07 01:07:24 -08002437 if (err == -ESRCH)
2438 err = -ENETUNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 goto local_input;
2440
2441 /*
2442 * Do not cache martian addresses: they should be logged (RFC1812)
2443 */
2444martian_destination:
2445 RT_CACHE_STAT_INC(in_martian_dst);
2446#ifdef CONFIG_IP_ROUTE_VERBOSE
2447 if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
Joe Perches058bd4d2012-03-11 18:36:11 +00002448 pr_warn("martian destination %pI4 from %pI4, dev %s\n",
Harvey Harrison673d57e2008-10-31 00:53:57 -07002449 &daddr, &saddr, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450#endif
Dietmar Eggemann2c2910a2005-06-28 13:06:23 -07002451
2452e_hostunreach:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002453 err = -EHOSTUNREACH;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00002454 goto out;
Dietmar Eggemann2c2910a2005-06-28 13:06:23 -07002455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456e_inval:
2457 err = -EINVAL;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00002458 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460e_nobufs:
2461 err = -ENOBUFS;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00002462 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464martian_source:
Eric Dumazetb5f7e752010-06-02 12:05:27 +00002465 err = -EINVAL;
2466martian_source_keep_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00002468 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
2470
Eric Dumazet407eadd2010-05-10 11:32:55 +00002471int ip_route_input_common(struct sk_buff *skb, __be32 daddr, __be32 saddr,
2472 u8 tos, struct net_device *dev, bool noref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
2474 struct rtable * rth;
2475 unsigned hash;
2476 int iif = dev->ifindex;
Denis V. Lunevb5921912008-01-22 23:50:25 -08002477 struct net *net;
Eric Dumazet96d36222010-06-02 19:21:31 +00002478 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002480 net = dev_net(dev);
Neil Horman1080d702008-10-27 12:28:25 -07002481
Eric Dumazet96d36222010-06-02 19:21:31 +00002482 rcu_read_lock();
2483
Neil Horman1080d702008-10-27 12:28:25 -07002484 if (!rt_caching(net))
2485 goto skip_cache;
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 tos &= IPTOS_RT_MASK;
Denis V. Luneve84f84f2008-07-05 19:04:32 -07002488 hash = rt_hash(daddr, saddr, iif, rt_genid(net));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
Changli Gaod8d1f302010-06-10 23:31:35 -07002491 rth = rcu_dereference(rth->dst.rt_next)) {
David S. Miller5e2b61f2011-03-04 21:47:09 -08002492 if ((((__force u32)rth->rt_key_dst ^ (__force u32)daddr) |
2493 ((__force u32)rth->rt_key_src ^ (__force u32)saddr) |
Julian Anastasov97a80412011-08-09 04:01:16 +00002494 (rth->rt_route_iif ^ iif) |
David S. Miller475949d2011-05-03 19:45:15 -07002495 (rth->rt_key_tos ^ tos)) == 0 &&
David S. Miller5e2b61f2011-03-04 21:47:09 -08002496 rth->rt_mark == skb->mark &&
Changli Gaod8d1f302010-06-10 23:31:35 -07002497 net_eq(dev_net(rth->dst.dev), net) &&
Denis V. Luneve84f84f2008-07-05 19:04:32 -07002498 !rt_is_expired(rth)) {
David S. Millerde398fb2011-12-05 13:21:42 -05002499 ipv4_validate_peer(rth);
Eric Dumazet407eadd2010-05-10 11:32:55 +00002500 if (noref) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002501 dst_use_noref(&rth->dst, jiffies);
2502 skb_dst_set_noref(skb, &rth->dst);
Eric Dumazet407eadd2010-05-10 11:32:55 +00002503 } else {
Changli Gaod8d1f302010-06-10 23:31:35 -07002504 dst_use(&rth->dst, jiffies);
2505 skb_dst_set(skb, &rth->dst);
Eric Dumazet407eadd2010-05-10 11:32:55 +00002506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 RT_CACHE_STAT_INC(in_hit);
2508 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return 0;
2510 }
2511 RT_CACHE_STAT_INC(in_hlist_search);
2512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Neil Horman1080d702008-10-27 12:28:25 -07002514skip_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 /* Multicast recognition logic is moved from route cache to here.
2516 The problem was that too many Ethernet cards have broken/missing
2517 hardware multicast filters :-( As result the host on multicasting
2518 network acquires a lot of useless route cache entries, sort of
2519 SDR messages from all the world. Now we try to get rid of them.
2520 Really, provided software IP multicast filter is organized
2521 reasonably (at least, hashed), it does not result in a slowdown
2522 comparing with route cache reject entries.
2523 Note, that multicast routers are not affected, because
2524 route cache entry is created eventually.
2525 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08002526 if (ipv4_is_multicast(daddr)) {
Eric Dumazet96d36222010-06-02 19:21:31 +00002527 struct in_device *in_dev = __in_dev_get_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Eric Dumazet96d36222010-06-02 19:21:31 +00002529 if (in_dev) {
David S. Millerdbdd9a52011-03-10 16:34:38 -08002530 int our = ip_check_mc_rcu(in_dev, daddr, saddr,
2531 ip_hdr(skb)->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (our
2533#ifdef CONFIG_IP_MROUTE
Joe Perches9d4fb272009-11-23 10:41:23 -08002534 ||
2535 (!ipv4_is_local_multicast(daddr) &&
2536 IN_DEV_MFORWARD(in_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537#endif
Joe Perches9d4fb272009-11-23 10:41:23 -08002538 ) {
Eric Dumazet96d36222010-06-02 19:21:31 +00002539 int res = ip_route_input_mc(skb, daddr, saddr,
2540 tos, dev, our);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 rcu_read_unlock();
Eric Dumazet96d36222010-06-02 19:21:31 +00002542 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544 }
2545 rcu_read_unlock();
2546 return -EINVAL;
2547 }
Eric Dumazet96d36222010-06-02 19:21:31 +00002548 res = ip_route_input_slow(skb, daddr, saddr, tos, dev);
2549 rcu_read_unlock();
2550 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
Eric Dumazet407eadd2010-05-10 11:32:55 +00002552EXPORT_SYMBOL(ip_route_input_common);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00002554/* called with rcu_read_lock() */
David S. Miller982721f2011-02-16 21:44:24 -08002555static struct rtable *__mkroute_output(const struct fib_result *res,
David S. Miller68a5e3d2011-03-11 20:07:33 -05002556 const struct flowi4 *fl4,
David S. Miller813b3b52011-04-28 14:48:42 -07002557 __be32 orig_daddr, __be32 orig_saddr,
Julian Anastasovf61759e2011-12-02 11:39:42 +00002558 int orig_oif, __u8 orig_rtos,
2559 struct net_device *dev_out,
David S. Miller5ada5522011-02-17 15:29:00 -08002560 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
David S. Miller982721f2011-02-16 21:44:24 -08002562 struct fib_info *fi = res->fi;
David S. Miller5ada5522011-02-17 15:29:00 -08002563 struct in_device *in_dev;
David S. Miller982721f2011-02-16 21:44:24 -08002564 u16 type = res->type;
David S. Miller5ada5522011-02-17 15:29:00 -08002565 struct rtable *rth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
David S. Miller68a5e3d2011-03-11 20:07:33 -05002567 if (ipv4_is_loopback(fl4->saddr) && !(dev_out->flags & IFF_LOOPBACK))
David S. Miller5ada5522011-02-17 15:29:00 -08002568 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
David S. Miller68a5e3d2011-03-11 20:07:33 -05002570 if (ipv4_is_lbcast(fl4->daddr))
David S. Miller982721f2011-02-16 21:44:24 -08002571 type = RTN_BROADCAST;
David S. Miller68a5e3d2011-03-11 20:07:33 -05002572 else if (ipv4_is_multicast(fl4->daddr))
David S. Miller982721f2011-02-16 21:44:24 -08002573 type = RTN_MULTICAST;
David S. Miller68a5e3d2011-03-11 20:07:33 -05002574 else if (ipv4_is_zeronet(fl4->daddr))
David S. Miller5ada5522011-02-17 15:29:00 -08002575 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 if (dev_out->flags & IFF_LOOPBACK)
2578 flags |= RTCF_LOCAL;
2579
Eric Dumazetdd28d1a2010-09-29 11:53:50 +00002580 in_dev = __in_dev_get_rcu(dev_out);
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00002581 if (!in_dev)
David S. Miller5ada5522011-02-17 15:29:00 -08002582 return ERR_PTR(-EINVAL);
Eric Dumazetebc0ffa2010-10-05 10:41:36 +00002583
David S. Miller982721f2011-02-16 21:44:24 -08002584 if (type == RTN_BROADCAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 flags |= RTCF_BROADCAST | RTCF_LOCAL;
David S. Miller982721f2011-02-16 21:44:24 -08002586 fi = NULL;
2587 } else if (type == RTN_MULTICAST) {
Eric Dumazetdd28d1a2010-09-29 11:53:50 +00002588 flags |= RTCF_MULTICAST | RTCF_LOCAL;
David S. Miller813b3b52011-04-28 14:48:42 -07002589 if (!ip_check_mc_rcu(in_dev, fl4->daddr, fl4->saddr,
2590 fl4->flowi4_proto))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 flags &= ~RTCF_LOCAL;
2592 /* If multicast route do not exist use
Eric Dumazetdd28d1a2010-09-29 11:53:50 +00002593 * default one, but do not gateway in this case.
2594 * Yes, it is hack.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 */
David S. Miller982721f2011-02-16 21:44:24 -08002596 if (fi && res->prefixlen < 4)
2597 fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
2599
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002600 rth = rt_dst_alloc(dev_out,
2601 IN_DEV_CONF_GET(in_dev, NOPOLICY),
David S. Miller0c4dcd52011-02-17 15:42:37 -08002602 IN_DEV_CONF_GET(in_dev, NOXFRM));
Dimitris Michailidis8391d072010-10-07 14:48:38 +00002603 if (!rth)
David S. Miller5ada5522011-02-17 15:29:00 -08002604 return ERR_PTR(-ENOBUFS);
Dimitris Michailidis8391d072010-10-07 14:48:38 +00002605
David S. Millercf911662011-04-28 14:31:47 -07002606 rth->dst.output = ip_output;
2607
David S. Miller813b3b52011-04-28 14:48:42 -07002608 rth->rt_key_dst = orig_daddr;
2609 rth->rt_key_src = orig_saddr;
David S. Millercf911662011-04-28 14:31:47 -07002610 rth->rt_genid = rt_genid(dev_net(dev_out));
2611 rth->rt_flags = flags;
2612 rth->rt_type = type;
Julian Anastasovf61759e2011-12-02 11:39:42 +00002613 rth->rt_key_tos = orig_rtos;
David S. Miller68a5e3d2011-03-11 20:07:33 -05002614 rth->rt_dst = fl4->daddr;
2615 rth->rt_src = fl4->saddr;
OGAWA Hirofumi1b86a582011-04-07 14:04:08 -07002616 rth->rt_route_iif = 0;
David S. Miller813b3b52011-04-28 14:48:42 -07002617 rth->rt_iif = orig_oif ? : dev_out->ifindex;
2618 rth->rt_oif = orig_oif;
2619 rth->rt_mark = fl4->flowi4_mark;
Lorenzo Colitti462ce7c2014-03-31 16:23:51 +09002620 rth->rt_uid = fl4->flowi4_uid;
David S. Miller68a5e3d2011-03-11 20:07:33 -05002621 rth->rt_gateway = fl4->daddr;
2622 rth->rt_spec_dst= fl4->saddr;
David S. Millercf911662011-04-28 14:31:47 -07002623 rth->rt_peer_genid = 0;
2624 rth->peer = NULL;
2625 rth->fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627 RT_CACHE_STAT_INC(out_slow_tot);
2628
2629 if (flags & RTCF_LOCAL) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002630 rth->dst.input = ip_local_deliver;
David S. Miller68a5e3d2011-03-11 20:07:33 -05002631 rth->rt_spec_dst = fl4->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 }
2633 if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
David S. Miller68a5e3d2011-03-11 20:07:33 -05002634 rth->rt_spec_dst = fl4->saddr;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002635 if (flags & RTCF_LOCAL &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 !(dev_out->flags & IFF_LOOPBACK)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002637 rth->dst.output = ip_mc_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 RT_CACHE_STAT_INC(out_slow_mc);
2639 }
2640#ifdef CONFIG_IP_MROUTE
David S. Miller982721f2011-02-16 21:44:24 -08002641 if (type == RTN_MULTICAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (IN_DEV_MFORWARD(in_dev) &&
David S. Miller813b3b52011-04-28 14:48:42 -07002643 !ipv4_is_local_multicast(fl4->daddr)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002644 rth->dst.input = ip_mr_input;
2645 rth->dst.output = ip_mc_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 }
2647 }
2648#endif
2649 }
2650
David S. Miller813b3b52011-04-28 14:48:42 -07002651 rt_set_nexthop(rth, fl4, res, fi, type, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
David S. Miller5ada5522011-02-17 15:29:00 -08002653 return rth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654}
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656/*
2657 * Major route resolver routine.
Eric Dumazet0197aa32010-09-30 03:33:58 +00002658 * called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 */
2660
David S. Miller813b3b52011-04-28 14:48:42 -07002661static struct rtable *ip_route_output_slow(struct net *net, struct flowi4 *fl4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 struct net_device *dev_out = NULL;
Julian Anastasovf61759e2011-12-02 11:39:42 +00002664 __u8 tos = RT_FL_TOS(fl4);
David S. Miller813b3b52011-04-28 14:48:42 -07002665 unsigned int flags = 0;
2666 struct fib_result res;
David S. Miller5ada5522011-02-17 15:29:00 -08002667 struct rtable *rth;
David S. Miller813b3b52011-04-28 14:48:42 -07002668 __be32 orig_daddr;
2669 __be32 orig_saddr;
2670 int orig_oif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
2672 res.fi = NULL;
2673#ifdef CONFIG_IP_MULTIPLE_TABLES
2674 res.r = NULL;
2675#endif
2676
David S. Miller813b3b52011-04-28 14:48:42 -07002677 orig_daddr = fl4->daddr;
2678 orig_saddr = fl4->saddr;
2679 orig_oif = fl4->flowi4_oif;
2680
2681 fl4->flowi4_iif = net->loopback_dev->ifindex;
2682 fl4->flowi4_tos = tos & IPTOS_RT_MASK;
2683 fl4->flowi4_scope = ((tos & RTO_ONLINK) ?
2684 RT_SCOPE_LINK : RT_SCOPE_UNIVERSE);
David S. Miller44713b62011-03-04 21:24:47 -08002685
David S. Miller010c2702011-02-17 15:37:09 -08002686 rcu_read_lock();
David S. Miller813b3b52011-04-28 14:48:42 -07002687 if (fl4->saddr) {
David S. Millerb23dd4f2011-03-02 14:31:35 -08002688 rth = ERR_PTR(-EINVAL);
David S. Miller813b3b52011-04-28 14:48:42 -07002689 if (ipv4_is_multicast(fl4->saddr) ||
2690 ipv4_is_lbcast(fl4->saddr) ||
2691 ipv4_is_zeronet(fl4->saddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 goto out;
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 /* I removed check for oif == dev_out->oif here.
2695 It was wrong for two reasons:
Denis V. Lunev1ab35272008-01-22 22:04:30 -08002696 1. ip_dev_find(net, saddr) can return wrong iface, if saddr
2697 is assigned to multiple interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 2. Moreover, we are allowed to send packets with saddr
2699 of another iface. --ANK
2700 */
2701
David S. Miller813b3b52011-04-28 14:48:42 -07002702 if (fl4->flowi4_oif == 0 &&
2703 (ipv4_is_multicast(fl4->daddr) ||
2704 ipv4_is_lbcast(fl4->daddr))) {
Julian Anastasova210d012008-10-01 07:28:28 -07002705 /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
David S. Miller813b3b52011-04-28 14:48:42 -07002706 dev_out = __ip_dev_find(net, fl4->saddr, false);
Julian Anastasova210d012008-10-01 07:28:28 -07002707 if (dev_out == NULL)
2708 goto out;
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 /* Special hack: user can direct multicasts
2711 and limited broadcast via necessary interface
2712 without fiddling with IP_MULTICAST_IF or IP_PKTINFO.
2713 This hack is not just for fun, it allows
2714 vic,vat and friends to work.
2715 They bind socket to loopback, set ttl to zero
2716 and expect that it will work.
2717 From the viewpoint of routing cache they are broken,
2718 because we are not allowed to build multicast path
2719 with loopback source addr (look, routing cache
2720 cannot know, that ttl is zero, so that packet
2721 will not leave this host and route is valid).
2722 Luckily, this hack is good workaround.
2723 */
2724
David S. Miller813b3b52011-04-28 14:48:42 -07002725 fl4->flowi4_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 goto make_route;
2727 }
Julian Anastasova210d012008-10-01 07:28:28 -07002728
David S. Miller813b3b52011-04-28 14:48:42 -07002729 if (!(fl4->flowi4_flags & FLOWI_FLAG_ANYSRC)) {
Julian Anastasova210d012008-10-01 07:28:28 -07002730 /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
David S. Miller813b3b52011-04-28 14:48:42 -07002731 if (!__ip_dev_find(net, fl4->saddr, false))
Julian Anastasova210d012008-10-01 07:28:28 -07002732 goto out;
Julian Anastasova210d012008-10-01 07:28:28 -07002733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 }
2735
2736
David S. Miller813b3b52011-04-28 14:48:42 -07002737 if (fl4->flowi4_oif) {
2738 dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif);
David S. Millerb23dd4f2011-03-02 14:31:35 -08002739 rth = ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 if (dev_out == NULL)
2741 goto out;
Herbert Xue5ed6392005-10-03 14:35:55 -07002742
2743 /* RACE: Check return value of inet_select_addr instead. */
Eric Dumazetfc75fc82010-12-22 04:39:39 +00002744 if (!(dev_out->flags & IFF_UP) || !__in_dev_get_rcu(dev_out)) {
David S. Millerb23dd4f2011-03-02 14:31:35 -08002745 rth = ERR_PTR(-ENETUNREACH);
Eric Dumazetfc75fc82010-12-22 04:39:39 +00002746 goto out;
2747 }
David S. Miller813b3b52011-04-28 14:48:42 -07002748 if (ipv4_is_local_multicast(fl4->daddr) ||
2749 ipv4_is_lbcast(fl4->daddr)) {
2750 if (!fl4->saddr)
2751 fl4->saddr = inet_select_addr(dev_out, 0,
2752 RT_SCOPE_LINK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 goto make_route;
2754 }
Jiri Bencad61d4c2013-10-04 17:04:48 +02002755 if (!fl4->saddr) {
David S. Miller813b3b52011-04-28 14:48:42 -07002756 if (ipv4_is_multicast(fl4->daddr))
2757 fl4->saddr = inet_select_addr(dev_out, 0,
2758 fl4->flowi4_scope);
2759 else if (!fl4->daddr)
2760 fl4->saddr = inet_select_addr(dev_out, 0,
2761 RT_SCOPE_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
2763 }
2764
David S. Miller813b3b52011-04-28 14:48:42 -07002765 if (!fl4->daddr) {
2766 fl4->daddr = fl4->saddr;
2767 if (!fl4->daddr)
2768 fl4->daddr = fl4->saddr = htonl(INADDR_LOOPBACK);
Denis V. Lunevb40afd02008-01-22 22:06:19 -08002769 dev_out = net->loopback_dev;
David S. Miller813b3b52011-04-28 14:48:42 -07002770 fl4->flowi4_oif = net->loopback_dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 res.type = RTN_LOCAL;
2772 flags |= RTCF_LOCAL;
2773 goto make_route;
2774 }
2775
David S. Miller813b3b52011-04-28 14:48:42 -07002776 if (fib_lookup(net, fl4, &res)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 res.fi = NULL;
David S. Miller813b3b52011-04-28 14:48:42 -07002778 if (fl4->flowi4_oif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 /* Apparently, routing tables are wrong. Assume,
2780 that the destination is on link.
2781
2782 WHY? DW.
2783 Because we are allowed to send to iface
2784 even if it has NO routes and NO assigned
2785 addresses. When oif is specified, routing
2786 tables are looked up with only one purpose:
2787 to catch if destination is gatewayed, rather than
2788 direct. Moreover, if MSG_DONTROUTE is set,
2789 we send packet, ignoring both routing tables
2790 and ifaddr state. --ANK
2791
2792
2793 We could make it even if oif is unknown,
2794 likely IPv6, but we do not.
2795 */
2796
David S. Miller813b3b52011-04-28 14:48:42 -07002797 if (fl4->saddr == 0)
2798 fl4->saddr = inet_select_addr(dev_out, 0,
2799 RT_SCOPE_LINK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 res.type = RTN_UNICAST;
2801 goto make_route;
2802 }
David S. Millerb23dd4f2011-03-02 14:31:35 -08002803 rth = ERR_PTR(-ENETUNREACH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 goto out;
2805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 if (res.type == RTN_LOCAL) {
David S. Miller813b3b52011-04-28 14:48:42 -07002808 if (!fl4->saddr) {
Joel Sing9fc3bbb2011-01-03 20:24:20 +00002809 if (res.fi->fib_prefsrc)
David S. Miller813b3b52011-04-28 14:48:42 -07002810 fl4->saddr = res.fi->fib_prefsrc;
Joel Sing9fc3bbb2011-01-03 20:24:20 +00002811 else
David S. Miller813b3b52011-04-28 14:48:42 -07002812 fl4->saddr = fl4->daddr;
Joel Sing9fc3bbb2011-01-03 20:24:20 +00002813 }
Denis V. Lunevb40afd02008-01-22 22:06:19 -08002814 dev_out = net->loopback_dev;
David S. Miller813b3b52011-04-28 14:48:42 -07002815 fl4->flowi4_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 res.fi = NULL;
2817 flags |= RTCF_LOCAL;
2818 goto make_route;
2819 }
2820
2821#ifdef CONFIG_IP_ROUTE_MULTIPATH
David S. Miller813b3b52011-04-28 14:48:42 -07002822 if (res.fi->fib_nhs > 1 && fl4->flowi4_oif == 0)
David S. Miller1b7fe5932011-03-10 17:01:16 -08002823 fib_select_multipath(&res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 else
2825#endif
David S. Miller21d8c492011-04-14 14:49:37 -07002826 if (!res.prefixlen &&
2827 res.table->tb_num_default > 1 &&
David S. Miller813b3b52011-04-28 14:48:42 -07002828 res.type == RTN_UNICAST && !fl4->flowi4_oif)
David S. Miller0c838ff2011-01-31 16:16:50 -08002829 fib_select_default(&res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
David S. Miller813b3b52011-04-28 14:48:42 -07002831 if (!fl4->saddr)
2832 fl4->saddr = FIB_RES_PREFSRC(net, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 dev_out = FIB_RES_DEV(res);
David S. Miller813b3b52011-04-28 14:48:42 -07002835 fl4->flowi4_oif = dev_out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
2837
2838make_route:
David S. Miller813b3b52011-04-28 14:48:42 -07002839 rth = __mkroute_output(&res, fl4, orig_daddr, orig_saddr, orig_oif,
Julian Anastasovf61759e2011-12-02 11:39:42 +00002840 tos, dev_out, flags);
David S. Millerb23dd4f2011-03-02 14:31:35 -08002841 if (!IS_ERR(rth)) {
David S. Miller5ada5522011-02-17 15:29:00 -08002842 unsigned int hash;
2843
David S. Miller813b3b52011-04-28 14:48:42 -07002844 hash = rt_hash(orig_daddr, orig_saddr, orig_oif,
David S. Miller5ada5522011-02-17 15:29:00 -08002845 rt_genid(dev_net(dev_out)));
David S. Miller813b3b52011-04-28 14:48:42 -07002846 rth = rt_intern_hash(hash, rth, NULL, orig_oif);
David S. Miller5ada5522011-02-17 15:29:00 -08002847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
David S. Miller010c2702011-02-17 15:37:09 -08002849out:
2850 rcu_read_unlock();
David S. Millerb23dd4f2011-03-02 14:31:35 -08002851 return rth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
2853
David S. Miller813b3b52011-04-28 14:48:42 -07002854struct rtable *__ip_route_output_key(struct net *net, struct flowi4 *flp4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 struct rtable *rth;
David S. Miller010c2702011-02-17 15:37:09 -08002857 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Neil Horman1080d702008-10-27 12:28:25 -07002859 if (!rt_caching(net))
2860 goto slow_output;
2861
David S. Miller9d6ec932011-03-12 01:12:47 -05002862 hash = rt_hash(flp4->daddr, flp4->saddr, flp4->flowi4_oif, rt_genid(net));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
2864 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08002865 for (rth = rcu_dereference_bh(rt_hash_table[hash].chain); rth;
Changli Gaod8d1f302010-06-10 23:31:35 -07002866 rth = rcu_dereference_bh(rth->dst.rt_next)) {
David S. Miller9d6ec932011-03-12 01:12:47 -05002867 if (rth->rt_key_dst == flp4->daddr &&
2868 rth->rt_key_src == flp4->saddr &&
David S. Millerc7537962010-11-11 17:07:48 -08002869 rt_is_output_route(rth) &&
David S. Miller9d6ec932011-03-12 01:12:47 -05002870 rth->rt_oif == flp4->flowi4_oif &&
2871 rth->rt_mark == flp4->flowi4_mark &&
Lorenzo Colitti462ce7c2014-03-31 16:23:51 +09002872 rth->rt_uid == flp4->flowi4_uid &&
David S. Miller475949d2011-05-03 19:45:15 -07002873 !((rth->rt_key_tos ^ flp4->flowi4_tos) &
Denis V. Lunevb5921912008-01-22 23:50:25 -08002874 (IPTOS_RT_MASK | RTO_ONLINK)) &&
Changli Gaod8d1f302010-06-10 23:31:35 -07002875 net_eq(dev_net(rth->dst.dev), net) &&
Denis V. Luneve84f84f2008-07-05 19:04:32 -07002876 !rt_is_expired(rth)) {
David S. Millerde398fb2011-12-05 13:21:42 -05002877 ipv4_validate_peer(rth);
Changli Gaod8d1f302010-06-10 23:31:35 -07002878 dst_use(&rth->dst, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 RT_CACHE_STAT_INC(out_hit);
2880 rcu_read_unlock_bh();
David S. Miller56157872011-05-02 14:37:45 -07002881 if (!flp4->saddr)
2882 flp4->saddr = rth->rt_src;
2883 if (!flp4->daddr)
2884 flp4->daddr = rth->rt_dst;
David S. Millerb23dd4f2011-03-02 14:31:35 -08002885 return rth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
2887 RT_CACHE_STAT_INC(out_hlist_search);
2888 }
2889 rcu_read_unlock_bh();
2890
Neil Horman1080d702008-10-27 12:28:25 -07002891slow_output:
David S. Miller9d6ec932011-03-12 01:12:47 -05002892 return ip_route_output_slow(net, flp4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893}
Arnaldo Carvalho de Melod8c97a92005-08-09 20:12:12 -07002894EXPORT_SYMBOL_GPL(__ip_route_output_key);
2895
Jianzhao Wangae2688d2010-09-08 14:35:43 -07002896static struct dst_entry *ipv4_blackhole_dst_check(struct dst_entry *dst, u32 cookie)
2897{
2898 return NULL;
2899}
2900
Steffen Klassertebb762f2011-11-23 02:12:51 +00002901static unsigned int ipv4_blackhole_mtu(const struct dst_entry *dst)
Roland Dreierec831ea2011-01-31 13:16:00 -08002902{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002903 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2904
2905 return mtu ? : dst->dev->mtu;
Roland Dreierec831ea2011-01-31 13:16:00 -08002906}
2907
David S. Miller14e50e52007-05-24 18:17:54 -07002908static void ipv4_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
2909{
2910}
2911
Held Bernhard0972ddb2011-04-24 22:07:32 +00002912static u32 *ipv4_rt_blackhole_cow_metrics(struct dst_entry *dst,
2913 unsigned long old)
2914{
2915 return NULL;
2916}
2917
David S. Miller14e50e52007-05-24 18:17:54 -07002918static struct dst_ops ipv4_dst_blackhole_ops = {
2919 .family = AF_INET,
Harvey Harrison09640e62009-02-01 00:45:17 -08002920 .protocol = cpu_to_be16(ETH_P_IP),
David S. Miller14e50e52007-05-24 18:17:54 -07002921 .destroy = ipv4_dst_destroy,
Jianzhao Wangae2688d2010-09-08 14:35:43 -07002922 .check = ipv4_blackhole_dst_check,
Steffen Klassertebb762f2011-11-23 02:12:51 +00002923 .mtu = ipv4_blackhole_mtu,
Eric Dumazet214f45c2011-02-18 11:39:01 -08002924 .default_advmss = ipv4_default_advmss,
David S. Miller14e50e52007-05-24 18:17:54 -07002925 .update_pmtu = ipv4_rt_blackhole_update_pmtu,
Held Bernhard0972ddb2011-04-24 22:07:32 +00002926 .cow_metrics = ipv4_rt_blackhole_cow_metrics,
David S. Millerd3aaeb32011-07-18 00:40:17 -07002927 .neigh_lookup = ipv4_neigh_lookup,
David S. Miller14e50e52007-05-24 18:17:54 -07002928};
2929
David S. Miller2774c132011-03-01 14:59:04 -08002930struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig)
David S. Miller14e50e52007-05-24 18:17:54 -07002931{
David S. Miller5c1e6aa2011-04-28 14:13:38 -07002932 struct rtable *rt = dst_alloc(&ipv4_dst_blackhole_ops, NULL, 1, 0, 0);
David S. Miller2774c132011-03-01 14:59:04 -08002933 struct rtable *ort = (struct rtable *) dst_orig;
David S. Miller14e50e52007-05-24 18:17:54 -07002934
2935 if (rt) {
Changli Gaod8d1f302010-06-10 23:31:35 -07002936 struct dst_entry *new = &rt->dst;
David S. Miller14e50e52007-05-24 18:17:54 -07002937
David S. Miller14e50e52007-05-24 18:17:54 -07002938 new->__use = 1;
Herbert Xu352e5122007-11-13 21:34:06 -08002939 new->input = dst_discard;
2940 new->output = dst_discard;
David S. Millerdefb3512010-12-08 21:16:57 -08002941 dst_copy_metrics(new, &ort->dst);
David S. Miller14e50e52007-05-24 18:17:54 -07002942
Changli Gaod8d1f302010-06-10 23:31:35 -07002943 new->dev = ort->dst.dev;
David S. Miller14e50e52007-05-24 18:17:54 -07002944 if (new->dev)
2945 dev_hold(new->dev);
2946
David S. Miller5e2b61f2011-03-04 21:47:09 -08002947 rt->rt_key_dst = ort->rt_key_dst;
2948 rt->rt_key_src = ort->rt_key_src;
David S. Miller475949d2011-05-03 19:45:15 -07002949 rt->rt_key_tos = ort->rt_key_tos;
OGAWA Hirofumi1b86a582011-04-07 14:04:08 -07002950 rt->rt_route_iif = ort->rt_route_iif;
David S. Miller5e2b61f2011-03-04 21:47:09 -08002951 rt->rt_iif = ort->rt_iif;
2952 rt->rt_oif = ort->rt_oif;
2953 rt->rt_mark = ort->rt_mark;
Lorenzo Colitti462ce7c2014-03-31 16:23:51 +09002954 rt->rt_uid = ort->rt_uid;
David S. Miller14e50e52007-05-24 18:17:54 -07002955
Denis V. Luneve84f84f2008-07-05 19:04:32 -07002956 rt->rt_genid = rt_genid(net);
David S. Miller14e50e52007-05-24 18:17:54 -07002957 rt->rt_flags = ort->rt_flags;
2958 rt->rt_type = ort->rt_type;
2959 rt->rt_dst = ort->rt_dst;
2960 rt->rt_src = ort->rt_src;
David S. Miller14e50e52007-05-24 18:17:54 -07002961 rt->rt_gateway = ort->rt_gateway;
2962 rt->rt_spec_dst = ort->rt_spec_dst;
2963 rt->peer = ort->peer;
2964 if (rt->peer)
2965 atomic_inc(&rt->peer->refcnt);
David S. Miller62fa8a82011-01-26 20:51:05 -08002966 rt->fi = ort->fi;
2967 if (rt->fi)
2968 atomic_inc(&rt->fi->fib_clntref);
David S. Miller14e50e52007-05-24 18:17:54 -07002969
2970 dst_free(new);
2971 }
2972
David S. Miller2774c132011-03-01 14:59:04 -08002973 dst_release(dst_orig);
2974
2975 return rt ? &rt->dst : ERR_PTR(-ENOMEM);
David S. Miller14e50e52007-05-24 18:17:54 -07002976}
2977
David S. Miller9d6ec932011-03-12 01:12:47 -05002978struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4,
David S. Millerb23dd4f2011-03-02 14:31:35 -08002979 struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
David S. Miller9d6ec932011-03-12 01:12:47 -05002981 struct rtable *rt = __ip_route_output_key(net, flp4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
David S. Millerb23dd4f2011-03-02 14:31:35 -08002983 if (IS_ERR(rt))
2984 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
David S. Miller56157872011-05-02 14:37:45 -07002986 if (flp4->flowi4_proto)
David S. Miller9d6ec932011-03-12 01:12:47 -05002987 rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
2988 flowi4_to_flowi(flp4),
2989 sk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
David S. Millerb23dd4f2011-03-02 14:31:35 -08002991 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992}
Arnaldo Carvalho de Melod8c97a92005-08-09 20:12:12 -07002993EXPORT_SYMBOL_GPL(ip_route_output_flow);
2994
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002995static int rt_fill_info(struct net *net,
2996 struct sk_buff *skb, u32 pid, u32 seq, int event,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07002997 int nowait, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
Eric Dumazet511c3f92009-06-02 05:14:27 +00002999 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 struct rtmsg *r;
Thomas Grafbe403ea2006-08-17 18:15:17 -07003001 struct nlmsghdr *nlh;
Steffen Klassert2bc8ca42011-10-11 01:12:02 +00003002 unsigned long expires = 0;
Eric Dumazetfe6fe792011-06-08 06:07:07 +00003003 const struct inet_peer *peer = rt->peer;
Thomas Grafe3703b32006-11-27 09:27:07 -08003004 u32 id = 0, ts = 0, tsage = 0, error;
Thomas Grafbe403ea2006-08-17 18:15:17 -07003005
3006 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags);
3007 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003008 return -EMSGSIZE;
Thomas Grafbe403ea2006-08-17 18:15:17 -07003009
3010 r = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 r->rtm_family = AF_INET;
3012 r->rtm_dst_len = 32;
3013 r->rtm_src_len = 0;
David S. Miller475949d2011-05-03 19:45:15 -07003014 r->rtm_tos = rt->rt_key_tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 r->rtm_table = RT_TABLE_MAIN;
Thomas Grafbe403ea2006-08-17 18:15:17 -07003016 NLA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 r->rtm_type = rt->rt_type;
3018 r->rtm_scope = RT_SCOPE_UNIVERSE;
3019 r->rtm_protocol = RTPROT_UNSPEC;
3020 r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
3021 if (rt->rt_flags & RTCF_NOTIFY)
3022 r->rtm_flags |= RTM_F_NOTIFY;
Thomas Grafbe403ea2006-08-17 18:15:17 -07003023
Al Viro17fb2c62006-09-26 22:15:25 -07003024 NLA_PUT_BE32(skb, RTA_DST, rt->rt_dst);
Thomas Grafbe403ea2006-08-17 18:15:17 -07003025
David S. Miller5e2b61f2011-03-04 21:47:09 -08003026 if (rt->rt_key_src) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 r->rtm_src_len = 32;
David S. Miller5e2b61f2011-03-04 21:47:09 -08003028 NLA_PUT_BE32(skb, RTA_SRC, rt->rt_key_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
Changli Gaod8d1f302010-06-10 23:31:35 -07003030 if (rt->dst.dev)
3031 NLA_PUT_U32(skb, RTA_OIF, rt->dst.dev->ifindex);
Patrick McHardyc7066f72011-01-14 13:36:42 +01003032#ifdef CONFIG_IP_ROUTE_CLASSID
Changli Gaod8d1f302010-06-10 23:31:35 -07003033 if (rt->dst.tclassid)
3034 NLA_PUT_U32(skb, RTA_FLOW, rt->dst.tclassid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035#endif
David S. Millerc7537962010-11-11 17:07:48 -08003036 if (rt_is_input_route(rt))
Al Viro17fb2c62006-09-26 22:15:25 -07003037 NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_spec_dst);
David S. Miller5e2b61f2011-03-04 21:47:09 -08003038 else if (rt->rt_src != rt->rt_key_src)
Al Viro17fb2c62006-09-26 22:15:25 -07003039 NLA_PUT_BE32(skb, RTA_PREFSRC, rt->rt_src);
Thomas Grafbe403ea2006-08-17 18:15:17 -07003040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 if (rt->rt_dst != rt->rt_gateway)
Al Viro17fb2c62006-09-26 22:15:25 -07003042 NLA_PUT_BE32(skb, RTA_GATEWAY, rt->rt_gateway);
Thomas Grafbe403ea2006-08-17 18:15:17 -07003043
David S. Millerdefb3512010-12-08 21:16:57 -08003044 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
Thomas Grafbe403ea2006-08-17 18:15:17 -07003045 goto nla_put_failure;
3046
David S. Miller5e2b61f2011-03-04 21:47:09 -08003047 if (rt->rt_mark)
3048 NLA_PUT_BE32(skb, RTA_MARK, rt->rt_mark);
Eric Dumazet963bfee2010-07-20 22:03:14 +00003049
Lorenzo Colitti462ce7c2014-03-31 16:23:51 +09003050 if (rt->rt_uid != (uid_t) -1)
3051 NLA_PUT_BE32(skb, RTA_UID, rt->rt_uid);
3052
Changli Gaod8d1f302010-06-10 23:31:35 -07003053 error = rt->dst.error;
Eric Dumazetfe6fe792011-06-08 06:07:07 +00003054 if (peer) {
Eric Dumazet317fe0e2010-06-16 04:52:13 +00003055 inet_peer_refcheck(rt->peer);
Eric Dumazetfe6fe792011-06-08 06:07:07 +00003056 if (peer->tcp_ts_stamp) {
3057 ts = peer->tcp_ts;
3058 tsage = get_seconds() - peer->tcp_ts_stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
Eric Dumazetfe6fe792011-06-08 06:07:07 +00003060 expires = ACCESS_ONCE(peer->pmtu_expires);
Steffen Klassert2bc8ca42011-10-11 01:12:02 +00003061 if (expires) {
3062 if (time_before(jiffies, expires))
3063 expires -= jiffies;
3064 else
3065 expires = 0;
3066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 }
Thomas Grafbe403ea2006-08-17 18:15:17 -07003068
David S. Millerc7537962010-11-11 17:07:48 -08003069 if (rt_is_input_route(rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070#ifdef CONFIG_IP_MROUTE
Al Viroe4485152006-09-26 22:15:01 -07003071 __be32 dst = rt->rt_dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Joe Perchesf97c1e02007-12-16 13:45:43 -08003073 if (ipv4_is_multicast(dst) && !ipv4_is_local_multicast(dst) &&
Benjamin Thery4feb88e2009-01-22 04:56:23 +00003074 IPV4_DEVCONF_ALL(net, MC_FORWARDING)) {
David S. Miller9a1b9492011-05-04 12:18:54 -07003075 int err = ipmr_get_route(net, skb,
3076 rt->rt_src, rt->rt_dst,
3077 r, nowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 if (err <= 0) {
3079 if (!nowait) {
3080 if (err == 0)
3081 return 0;
Thomas Grafbe403ea2006-08-17 18:15:17 -07003082 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 } else {
3084 if (err == -EMSGSIZE)
Thomas Grafbe403ea2006-08-17 18:15:17 -07003085 goto nla_put_failure;
Thomas Grafe3703b32006-11-27 09:27:07 -08003086 error = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 }
3088 }
3089 } else
3090#endif
David S. Miller5e2b61f2011-03-04 21:47:09 -08003091 NLA_PUT_U32(skb, RTA_IIF, rt->rt_iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
3093
Changli Gaod8d1f302010-06-10 23:31:35 -07003094 if (rtnl_put_cacheinfo(skb, &rt->dst, id, ts, tsage,
Thomas Grafe3703b32006-11-27 09:27:07 -08003095 expires, error) < 0)
3096 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Thomas Grafbe403ea2006-08-17 18:15:17 -07003098 return nlmsg_end(skb, nlh);
3099
3100nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08003101 nlmsg_cancel(skb, nlh);
3102 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103}
3104
Thomas Graf63f34442007-03-22 11:55:17 -07003105static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003107 struct net *net = sock_net(in_skb->sk);
Thomas Grafd889ce32006-08-17 18:15:44 -07003108 struct rtmsg *rtm;
3109 struct nlattr *tb[RTA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 struct rtable *rt = NULL;
Al Viro9e12bb22006-09-26 21:25:20 -07003111 __be32 dst = 0;
3112 __be32 src = 0;
3113 u32 iif;
Thomas Grafd889ce32006-08-17 18:15:44 -07003114 int err;
Eric Dumazet963bfee2010-07-20 22:03:14 +00003115 int mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 struct sk_buff *skb;
3117
Thomas Grafd889ce32006-08-17 18:15:44 -07003118 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy);
3119 if (err < 0)
3120 goto errout;
3121
3122 rtm = nlmsg_data(nlh);
3123
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
Thomas Grafd889ce32006-08-17 18:15:44 -07003125 if (skb == NULL) {
3126 err = -ENOBUFS;
3127 goto errout;
3128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130 /* Reserve room for dummy headers, this skb can pass
3131 through good chunk of routing engine.
3132 */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07003133 skb_reset_mac_header(skb);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07003134 skb_reset_network_header(skb);
Stephen Hemmingerd2c962b2006-04-17 17:27:11 -07003135
3136 /* Bugfix: need to give ip_route_input enough of an IP header to not gag. */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003137 ip_hdr(skb)->protocol = IPPROTO_ICMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr));
3139
Al Viro17fb2c62006-09-26 22:15:25 -07003140 src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0;
3141 dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0;
Thomas Grafd889ce32006-08-17 18:15:44 -07003142 iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
Eric Dumazet963bfee2010-07-20 22:03:14 +00003143 mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
3145 if (iif) {
Thomas Grafd889ce32006-08-17 18:15:44 -07003146 struct net_device *dev;
3147
Denis V. Lunev19375042008-02-28 20:52:04 -08003148 dev = __dev_get_by_index(net, iif);
Thomas Grafd889ce32006-08-17 18:15:44 -07003149 if (dev == NULL) {
3150 err = -ENODEV;
3151 goto errout_free;
3152 }
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 skb->protocol = htons(ETH_P_IP);
3155 skb->dev = dev;
Eric Dumazet963bfee2010-07-20 22:03:14 +00003156 skb->mark = mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 local_bh_disable();
3158 err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev);
3159 local_bh_enable();
Thomas Grafd889ce32006-08-17 18:15:44 -07003160
Eric Dumazet511c3f92009-06-02 05:14:27 +00003161 rt = skb_rtable(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07003162 if (err == 0 && rt->dst.error)
3163 err = -rt->dst.error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 } else {
David S. Miller68a5e3d2011-03-11 20:07:33 -05003165 struct flowi4 fl4 = {
3166 .daddr = dst,
3167 .saddr = src,
3168 .flowi4_tos = rtm->rtm_tos,
3169 .flowi4_oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0,
3170 .flowi4_mark = mark,
Lorenzo Colitti462ce7c2014-03-31 16:23:51 +09003171 .flowi4_uid = tb[RTA_UID] ? nla_get_u32(tb[RTA_UID]) : current_uid(),
Thomas Grafd889ce32006-08-17 18:15:44 -07003172 };
David S. Miller9d6ec932011-03-12 01:12:47 -05003173 rt = ip_route_output_key(net, &fl4);
David S. Millerb23dd4f2011-03-02 14:31:35 -08003174
3175 err = 0;
3176 if (IS_ERR(rt))
3177 err = PTR_ERR(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 }
Thomas Grafd889ce32006-08-17 18:15:44 -07003179
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 if (err)
Thomas Grafd889ce32006-08-17 18:15:44 -07003181 goto errout_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Changli Gaod8d1f302010-06-10 23:31:35 -07003183 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 if (rtm->rtm_flags & RTM_F_NOTIFY)
3185 rt->rt_flags |= RTCF_NOTIFY;
3186
Benjamin Thery4feb88e2009-01-22 04:56:23 +00003187 err = rt_fill_info(net, skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
Denis V. Lunev19375042008-02-28 20:52:04 -08003188 RTM_NEWROUTE, 0, 0);
Thomas Grafd889ce32006-08-17 18:15:44 -07003189 if (err <= 0)
3190 goto errout_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Denis V. Lunev19375042008-02-28 20:52:04 -08003192 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Grafd889ce32006-08-17 18:15:44 -07003193errout:
Thomas Graf2942e902006-08-15 00:30:25 -07003194 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Thomas Grafd889ce32006-08-17 18:15:44 -07003196errout_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 kfree_skb(skb);
Thomas Grafd889ce32006-08-17 18:15:44 -07003198 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199}
3200
3201int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb)
3202{
3203 struct rtable *rt;
3204 int h, s_h;
3205 int idx, s_idx;
Denis V. Lunev19375042008-02-28 20:52:04 -08003206 struct net *net;
3207
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003208 net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210 s_h = cb->args[0];
Eric Dumazetd8c92832008-01-07 21:52:14 -08003211 if (s_h < 0)
3212 s_h = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 s_idx = idx = cb->args[1];
Eric Dumazeta6272662008-08-28 01:11:25 -07003214 for (h = s_h; h <= rt_hash_mask; h++, s_idx = 0) {
3215 if (!rt_hash_table[h].chain)
3216 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -08003218 for (rt = rcu_dereference_bh(rt_hash_table[h].chain), idx = 0; rt;
Changli Gaod8d1f302010-06-10 23:31:35 -07003219 rt = rcu_dereference_bh(rt->dst.rt_next), idx++) {
3220 if (!net_eq(dev_net(rt->dst.dev), net) || idx < s_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 continue;
Denis V. Luneve84f84f2008-07-05 19:04:32 -07003222 if (rt_is_expired(rt))
Eric Dumazet29e75252008-01-31 17:05:09 -08003223 continue;
Changli Gaod8d1f302010-06-10 23:31:35 -07003224 skb_dst_set_noref(skb, &rt->dst);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00003225 if (rt_fill_info(net, skb, NETLINK_CB(cb->skb).pid,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003226 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003227 1, NLM_F_MULTI) <= 0) {
Eric Dumazetadf30902009-06-02 05:19:30 +00003228 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 rcu_read_unlock_bh();
3230 goto done;
3231 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003232 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
3234 rcu_read_unlock_bh();
3235 }
3236
3237done:
3238 cb->args[0] = h;
3239 cb->args[1] = idx;
3240 return skb->len;
3241}
3242
3243void ip_rt_multicast_event(struct in_device *in_dev)
3244{
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -07003245 rt_cache_flush(dev_net(in_dev->dev), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246}
3247
3248#ifdef CONFIG_SYSCTL
Denis V. Lunev81c684d2008-07-08 03:05:28 -07003249static int ipv4_sysctl_rtcache_flush(ctl_table *__ctl, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003250 void __user *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 size_t *lenp, loff_t *ppos)
3252{
3253 if (write) {
Denis V. Lunev639e1042008-07-05 19:02:06 -07003254 int flush_delay;
Denis V. Lunev81c684d2008-07-08 03:05:28 -07003255 ctl_table ctl;
Denis V. Lunev39a23e72008-07-05 19:02:33 -07003256 struct net *net;
Denis V. Lunev639e1042008-07-05 19:02:06 -07003257
Denis V. Lunev81c684d2008-07-08 03:05:28 -07003258 memcpy(&ctl, __ctl, sizeof(ctl));
3259 ctl.data = &flush_delay;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003260 proc_dointvec(&ctl, write, buffer, lenp, ppos);
Denis V. Lunev639e1042008-07-05 19:02:06 -07003261
Denis V. Lunev81c684d2008-07-08 03:05:28 -07003262 net = (struct net *)__ctl->extra1;
Denis V. Lunev39a23e72008-07-05 19:02:33 -07003263 rt_cache_flush(net, flush_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 return 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
3267 return -EINVAL;
3268}
3269
Al Viroeeb61f72008-07-27 08:59:33 +01003270static ctl_table ipv4_route_table[] = {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003271 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 .procname = "gc_thresh",
3273 .data = &ipv4_dst_ops.gc_thresh,
3274 .maxlen = sizeof(int),
3275 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003276 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 },
3278 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 .procname = "max_size",
3280 .data = &ip_rt_max_size,
3281 .maxlen = sizeof(int),
3282 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003283 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 },
3285 {
3286 /* Deprecated. Use gc_min_interval_ms */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003287
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 .procname = "gc_min_interval",
3289 .data = &ip_rt_gc_min_interval,
3290 .maxlen = sizeof(int),
3291 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003292 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 },
3294 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 .procname = "gc_min_interval_ms",
3296 .data = &ip_rt_gc_min_interval,
3297 .maxlen = sizeof(int),
3298 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003299 .proc_handler = proc_dointvec_ms_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 },
3301 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 .procname = "gc_timeout",
3303 .data = &ip_rt_gc_timeout,
3304 .maxlen = sizeof(int),
3305 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003306 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 },
3308 {
Eric Dumazet9f28a2f2011-12-21 15:47:16 -05003309 .procname = "gc_interval",
3310 .data = &ip_rt_gc_interval,
3311 .maxlen = sizeof(int),
3312 .mode = 0644,
3313 .proc_handler = proc_dointvec_jiffies,
3314 },
3315 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 .procname = "redirect_load",
3317 .data = &ip_rt_redirect_load,
3318 .maxlen = sizeof(int),
3319 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003320 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 },
3322 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 .procname = "redirect_number",
3324 .data = &ip_rt_redirect_number,
3325 .maxlen = sizeof(int),
3326 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003327 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 },
3329 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 .procname = "redirect_silence",
3331 .data = &ip_rt_redirect_silence,
3332 .maxlen = sizeof(int),
3333 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003334 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 },
3336 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 .procname = "error_cost",
3338 .data = &ip_rt_error_cost,
3339 .maxlen = sizeof(int),
3340 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003341 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 },
3343 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 .procname = "error_burst",
3345 .data = &ip_rt_error_burst,
3346 .maxlen = sizeof(int),
3347 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003348 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 },
3350 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 .procname = "gc_elasticity",
3352 .data = &ip_rt_gc_elasticity,
3353 .maxlen = sizeof(int),
3354 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003355 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 },
3357 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 .procname = "mtu_expires",
3359 .data = &ip_rt_mtu_expires,
3360 .maxlen = sizeof(int),
3361 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003362 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 },
3364 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 .procname = "min_pmtu",
3366 .data = &ip_rt_min_pmtu,
3367 .maxlen = sizeof(int),
3368 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003369 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 },
3371 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 .procname = "min_adv_mss",
3373 .data = &ip_rt_min_advmss,
3374 .maxlen = sizeof(int),
3375 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003376 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003378 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379};
Denis V. Lunev39a23e72008-07-05 19:02:33 -07003380
Al Viro2f4520d2008-08-25 15:17:44 -07003381static struct ctl_table empty[1];
3382
3383static struct ctl_table ipv4_skeleton[] =
3384{
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003385 { .procname = "route",
Hugh Dickinsd994af02008-08-27 02:35:18 -07003386 .mode = 0555, .child = ipv4_route_table},
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003387 { .procname = "neigh",
Hugh Dickinsd994af02008-08-27 02:35:18 -07003388 .mode = 0555, .child = empty},
Al Viro2f4520d2008-08-25 15:17:44 -07003389 { }
Denis V. Lunev39a23e72008-07-05 19:02:33 -07003390};
3391
Al Viro2f4520d2008-08-25 15:17:44 -07003392static __net_initdata struct ctl_path ipv4_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003393 { .procname = "net", },
3394 { .procname = "ipv4", },
Al Viro2f4520d2008-08-25 15:17:44 -07003395 { },
3396};
Denis V. Lunev39a23e72008-07-05 19:02:33 -07003397
3398static struct ctl_table ipv4_route_flush_table[] = {
3399 {
Denis V. Lunev39a23e72008-07-05 19:02:33 -07003400 .procname = "flush",
3401 .maxlen = sizeof(int),
3402 .mode = 0200,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -08003403 .proc_handler = ipv4_sysctl_rtcache_flush,
Denis V. Lunev39a23e72008-07-05 19:02:33 -07003404 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003405 { },
Denis V. Lunev39a23e72008-07-05 19:02:33 -07003406};
3407
Al Viro2f4520d2008-08-25 15:17:44 -07003408static __net_initdata struct ctl_path ipv4_route_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08003409 { .procname = "net", },
3410 { .procname = "ipv4", },
3411 { .procname = "route", },
Al Viro2f4520d2008-08-25 15:17:44 -07003412 { },
3413};
3414
Denis V. Lunev39a23e72008-07-05 19:02:33 -07003415static __net_init int sysctl_route_net_init(struct net *net)
3416{
3417 struct ctl_table *tbl;
3418
3419 tbl = ipv4_route_flush_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08003420 if (!net_eq(net, &init_net)) {
Denis V. Lunev39a23e72008-07-05 19:02:33 -07003421 tbl = kmemdup(tbl, sizeof(ipv4_route_flush_table), GFP_KERNEL);
3422 if (tbl == NULL)
3423 goto err_dup;
3424 }
3425 tbl[0].extra1 = net;
3426
3427 net->ipv4.route_hdr =
3428 register_net_sysctl_table(net, ipv4_route_path, tbl);
3429 if (net->ipv4.route_hdr == NULL)
3430 goto err_reg;
3431 return 0;
3432
3433err_reg:
3434 if (tbl != ipv4_route_flush_table)
3435 kfree(tbl);
3436err_dup:
3437 return -ENOMEM;
3438}
3439
3440static __net_exit void sysctl_route_net_exit(struct net *net)
3441{
3442 struct ctl_table *tbl;
3443
3444 tbl = net->ipv4.route_hdr->ctl_table_arg;
3445 unregister_net_sysctl_table(net->ipv4.route_hdr);
3446 BUG_ON(tbl == ipv4_route_flush_table);
3447 kfree(tbl);
3448}
3449
3450static __net_initdata struct pernet_operations sysctl_route_ops = {
3451 .init = sysctl_route_net_init,
3452 .exit = sysctl_route_net_exit,
3453};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454#endif
3455
Neil Horman3ee94372010-05-08 01:57:52 -07003456static __net_init int rt_genid_init(struct net *net)
Denis V. Lunev9f5e97e2008-07-05 19:02:59 -07003457{
Neil Horman3ee94372010-05-08 01:57:52 -07003458 get_random_bytes(&net->ipv4.rt_genid,
3459 sizeof(net->ipv4.rt_genid));
David S. Miller436c3b62011-03-24 17:42:21 -07003460 get_random_bytes(&net->ipv4.dev_addr_genid,
3461 sizeof(net->ipv4.dev_addr_genid));
Denis V. Lunev9f5e97e2008-07-05 19:02:59 -07003462 return 0;
3463}
3464
Neil Horman3ee94372010-05-08 01:57:52 -07003465static __net_initdata struct pernet_operations rt_genid_ops = {
3466 .init = rt_genid_init,
Denis V. Lunev9f5e97e2008-07-05 19:02:59 -07003467};
3468
3469
Patrick McHardyc7066f72011-01-14 13:36:42 +01003470#ifdef CONFIG_IP_ROUTE_CLASSID
Tejun Heo7d720c32010-02-16 15:20:26 +00003471struct ip_rt_acct __percpu *ip_rt_acct __read_mostly;
Patrick McHardyc7066f72011-01-14 13:36:42 +01003472#endif /* CONFIG_IP_ROUTE_CLASSID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
3474static __initdata unsigned long rhash_entries;
3475static int __init set_rhash_entries(char *str)
3476{
3477 if (!str)
3478 return 0;
3479 rhash_entries = simple_strtoul(str, &str, 0);
3480 return 1;
3481}
3482__setup("rhash_entries=", set_rhash_entries);
3483
3484int __init ip_rt_init(void)
3485{
Eric Dumazet424c4b72005-07-05 14:58:19 -07003486 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Eric Dumazetad52eef2014-06-02 05:26:03 -07003488 ip_idents = kmalloc(IP_IDENTS_SZ * sizeof(*ip_idents), GFP_KERNEL);
3489 if (!ip_idents)
3490 panic("IP: failed to allocate ip_idents\n");
3491
3492 get_random_bytes(ip_idents, IP_IDENTS_SZ * sizeof(*ip_idents));
3493
Patrick McHardyc7066f72011-01-14 13:36:42 +01003494#ifdef CONFIG_IP_ROUTE_CLASSID
Ingo Molnar0dcec8c2009-02-25 14:07:33 +01003495 ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct), __alignof__(struct ip_rt_acct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 if (!ip_rt_acct)
3497 panic("IP: failed to allocate ip_rt_acct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498#endif
3499
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07003500 ipv4_dst_ops.kmem_cachep =
3501 kmem_cache_create("ip_dst_cache", sizeof(struct rtable), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09003502 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
David S. Miller14e50e52007-05-24 18:17:54 -07003504 ipv4_dst_blackhole_ops.kmem_cachep = ipv4_dst_ops.kmem_cachep;
3505
Eric Dumazetfc66f952010-10-08 06:37:34 +00003506 if (dst_entries_init(&ipv4_dst_ops) < 0)
3507 panic("IP: failed to allocate ipv4_dst_ops counter\n");
3508
3509 if (dst_entries_init(&ipv4_dst_blackhole_ops) < 0)
3510 panic("IP: failed to allocate ipv4_dst_blackhole_ops counter\n");
3511
Eric Dumazet424c4b72005-07-05 14:58:19 -07003512 rt_hash_table = (struct rt_hash_bucket *)
3513 alloc_large_system_hash("IP route cache",
3514 sizeof(struct rt_hash_bucket),
3515 rhash_entries,
Jan Beulich44813742009-09-21 17:03:05 -07003516 (totalram_pages >= 128 * 1024) ?
Mike Stroyan18955cf2005-11-29 16:12:55 -08003517 15 : 17,
Kirill Korotaev8d1502d2006-08-07 20:44:22 -07003518 0,
Eric Dumazet424c4b72005-07-05 14:58:19 -07003519 &rt_hash_log,
3520 &rt_hash_mask,
Anton Blanchardc9503e02009-04-27 05:42:24 -07003521 rhash_entries ? 0 : 512 * 1024);
Eric Dumazet22c047c2005-07-05 14:55:24 -07003522 memset(rt_hash_table, 0, (rt_hash_mask + 1) * sizeof(struct rt_hash_bucket));
3523 rt_hash_lock_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
3525 ipv4_dst_ops.gc_thresh = (rt_hash_mask + 1);
3526 ip_rt_max_size = (rt_hash_mask + 1) * 16;
3527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 devinet_init();
3529 ip_fib_init();
3530
Eric Dumazet9f28a2f2011-12-21 15:47:16 -05003531 INIT_DELAYED_WORK_DEFERRABLE(&expires_work, rt_worker_func);
3532 expires_ljiffies = jiffies;
3533 schedule_delayed_work(&expires_work,
3534 net_random() % ip_rt_gc_interval + ip_rt_gc_interval);
3535
Denis V. Lunev73b38712008-02-28 20:51:18 -08003536 if (ip_rt_proc_init())
Joe Perches058bd4d2012-03-11 18:36:11 +00003537 pr_err("Unable to create route proc files\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538#ifdef CONFIG_XFRM
3539 xfrm_init();
Neil Hormana33bc5c2009-07-30 18:52:15 -07003540 xfrm4_init(ip_rt_max_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003542 rtnl_register(PF_INET, RTM_GETROUTE, inet_rtm_getroute, NULL, NULL);
Thomas Graf63f34442007-03-22 11:55:17 -07003543
Denis V. Lunev39a23e72008-07-05 19:02:33 -07003544#ifdef CONFIG_SYSCTL
3545 register_pernet_subsys(&sysctl_route_ops);
3546#endif
Neil Horman3ee94372010-05-08 01:57:52 -07003547 register_pernet_subsys(&rt_genid_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 return rc;
3549}
3550
Al Viroa1bc6eb2008-07-30 06:32:52 -04003551#ifdef CONFIG_SYSCTL
Al Viroeeb61f72008-07-27 08:59:33 +01003552/*
3553 * We really need to sanitize the damn ipv4 init order, then all
3554 * this nonsense will go away.
3555 */
3556void __init ip_static_sysctl_init(void)
3557{
Al Viro2f4520d2008-08-25 15:17:44 -07003558 register_sysctl_paths(ipv4_path, ipv4_skeleton);
Al Viroeeb61f72008-07-27 08:59:33 +01003559}
Al Viroa1bc6eb2008-07-30 06:32:52 -04003560#endif