65 #define pr_fmt(fmt) "IPv4: " fmt
67 #include <linux/module.h>
68 #include <asm/uaccess.h>
69 #include <linux/bitops.h>
70 #include <linux/types.h>
71 #include <linux/kernel.h>
73 #include <linux/string.h>
74 #include <linux/socket.h>
76 #include <linux/errno.h>
79 #include <linux/netdevice.h>
84 #include <linux/igmp.h>
86 #include <linux/mroute.h>
87 #include <linux/netfilter_ipv4.h>
88 #include <linux/random.h>
91 #include <linux/slab.h>
107 #include <linux/sysctl.h>
112 #define RT_FL_TOS(oldflp4) \
113 ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
115 #define IP_MAX_MTU 0xFFF0
117 #define RT_GC_TIMEOUT (300*HZ)
119 static int ip_rt_max_size;
125 static int ip_rt_redirect_silence
__read_mostly = ((
HZ / 50) << (9 + 1));
127 static int ip_rt_error_burst __read_mostly = 5 *
HZ;
128 static int ip_rt_gc_elasticity __read_mostly = 8;
129 static int ip_rt_mtu_expires __read_mostly = 10 * 60 *
HZ;
130 static int ip_rt_min_pmtu __read_mostly = 512 + 20 + 20;
131 static int ip_rt_min_advmss __read_mostly = 256;
138 static unsigned int ipv4_default_advmss(
const struct dst_entry *
dst);
139 static unsigned int ipv4_mtu(
const struct dst_entry *
dst);
141 static void ipv4_link_failure(
struct sk_buff *
skb);
163 static struct dst_ops ipv4_dst_ops = {
166 .check = ipv4_dst_check,
167 .default_advmss = ipv4_default_advmss,
169 .cow_metrics = ipv4_cow_metrics,
170 .destroy = ipv4_dst_destroy,
171 .ifdown = ipv4_dst_ifdown,
172 .negative_advice = ipv4_negative_advice,
173 .link_failure = ipv4_link_failure,
174 .update_pmtu = ip_rt_update_pmtu,
175 .redirect = ip_do_redirect,
177 .neigh_lookup = ipv4_neigh_lookup,
180 #define ECN_OR_COST(class) TC_PRIO_##class
203 #define RT_CACHE_STAT_INC(field) __this_cpu_inc(rt_cache_stat.field)
205 #ifdef CONFIG_PROC_FS
206 static void *rt_cache_seq_start(
struct seq_file *seq, loff_t *
pos)
213 static void *rt_cache_seq_next(
struct seq_file *seq,
void *
v, loff_t *
pos)
219 static void rt_cache_seq_stop(
struct seq_file *seq,
void *
v)
223 static int rt_cache_seq_show(
struct seq_file *seq,
void *
v)
227 "Iface\tDestination\tGateway \tFlags\t\tRefCnt\tUse\t"
228 "Metric\tSource\t\tMTU\tWindow\tIRTT\tTOS\tHHRef\t"
234 .
start = rt_cache_seq_start,
235 .next = rt_cache_seq_next,
236 .stop = rt_cache_seq_stop,
237 .show = rt_cache_seq_show,
242 return seq_open(file, &rt_cache_seq_ops);
247 .open = rt_cache_seq_open,
254 static void *rt_cpu_seq_start(
struct seq_file *seq, loff_t *
pos)
270 static void *rt_cpu_seq_next(
struct seq_file *seq,
void *v, loff_t *pos)
274 for (cpu = *pos; cpu < nr_cpu_ids; ++
cpu) {
284 static void rt_cpu_seq_stop(
struct seq_file *seq,
void *v)
289 static int rt_cpu_seq_show(
struct seq_file *seq,
void *v)
294 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");
298 seq_printf(seq,
"%08x %08x %08x %08x %08x %08x %08x %08x "
299 " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
300 dst_entries_get_slow(&ipv4_dst_ops),
324 .
start = rt_cpu_seq_start,
325 .next = rt_cpu_seq_next,
326 .stop = rt_cpu_seq_stop,
327 .show = rt_cpu_seq_show,
333 return seq_open(file, &rt_cpu_seq_ops);
338 .open = rt_cpu_seq_open,
344 #ifdef CONFIG_IP_ROUTE_CLASSID
345 static int rt_acct_proc_show(
struct seq_file *
m,
void *v)
356 for (j = 0; j < 256; j++) {
376 .open = rt_acct_proc_open,
392 pde = proc_create(
"rt_cache",
S_IRUGO,
397 #ifdef CONFIG_IP_ROUTE_CLASSID
398 pde = proc_create(
"rt_acct", 0, net->
proc_net, &rt_acct_proc_fops);
404 #ifdef CONFIG_IP_ROUTE_CLASSID
414 static void __net_exit ip_rt_do_proc_exit(
struct net *net)
418 #ifdef CONFIG_IP_ROUTE_CLASSID
424 .init = ip_rt_do_proc_init,
425 .exit = ip_rt_do_proc_exit,
428 static int __init ip_rt_proc_init(
void)
434 static inline int ip_rt_proc_init(
void)
440 static inline bool rt_is_expired(
const struct rtable *rth)
442 return rth->
rt_genid != rt_genid(dev_net(rth->
dst.dev));
459 rt = (
const struct rtable *) dst;
463 pkey = &ip_hdr(skb)->daddr;
465 n = __ipv4_neigh_lookup(dev, *(
__force u32 *)pkey);
468 return neigh_create(&
arp_tbl, pkey, dev);
478 static void ip_select_fb_ident(
struct iphdr *iph)
481 static u32 ip_fallback_id;
484 spin_lock_bh(&ip_fb_id_lock);
486 iph->
id =
htons(salt & 0xFFFF);
487 ip_fallback_id = salt;
488 spin_unlock_bh(&ip_fb_id_lock);
493 struct net *net = dev_net(dst->
dev);
496 peer = inet_getpeer_v4(net->
ipv4.peers, iph->
daddr, 1);
498 iph->
id =
htons(inet_getid(peer, more));
503 ip_select_fb_ident(iph);
507 static void __build_flow_key(
struct flowi4 *fl4,
const struct sock *
sk,
508 const struct iphdr *iph,
513 const struct inet_sock *inet = inet_sk(sk);
515 oif = sk->sk_bound_dev_if;
520 flowi4_init_output(fl4, oif, mark, tos,
526 static void build_skb_flow_key(
struct flowi4 *fl4,
const struct sk_buff *skb,
527 const struct sock *sk)
529 const struct iphdr *iph = ip_hdr(skb);
530 int oif = skb->
dev->ifindex;
535 __build_flow_key(fl4, sk, iph, oif, tos, prot, mark, 0);
538 static void build_sk_flow_key(
struct flowi4 *fl4,
const struct sock *sk)
540 const struct inet_sock *inet = inet_sk(sk);
542 __be32 daddr = inet->inet_daddr;
546 if (inet_opt && inet_opt->
opt.srr)
547 daddr = inet_opt->
opt.faddr;
548 flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->
sk_mark,
551 inet_sk_flowi_flags(sk),
556 static void ip_rt_build_flow_key(
struct flowi4 *fl4,
const struct sock *sk,
560 build_skb_flow_key(fl4, skb, sk);
562 build_sk_flow_key(fl4, sk);
565 static inline void rt_free(
struct rtable *rt)
591 static inline u32 fnhe_hashfun(
__be32 daddr)
596 hval ^= (hval >> 11) ^ (hval >> 22);
602 u32 pmtu,
unsigned long expires)
607 u32 hval = fnhe_hashfun(daddr);
609 spin_lock_bh(&fnhe_lock);
638 fnhe = fnhe_oldest(hash);
656 spin_unlock_bh(&fnhe_lock);
663 __be32 new_gw = icmp_hdr(skb)->un.gateway;
664 __be32 old_gw = ip_hdr(skb)->saddr;
666 struct in_device *in_dev;
671 switch (icmp_hdr(skb)->
code & 7) {
685 in_dev = __in_dev_get_rcu(dev);
690 if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev) ||
691 ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw) ||
692 ipv4_is_zeronet(new_gw))
693 goto reject_redirect;
695 if (!IN_DEV_SHARED_MEDIA(in_dev)) {
697 goto reject_redirect;
699 goto reject_redirect;
702 goto reject_redirect;
705 n = ipv4_neigh_lookup(&rt->
dst,
NULL, &new_gw);
708 neigh_event_send(n,
NULL);
710 if (fib_lookup(net, fl4, &
res) == 0) {
713 update_or_create_fnhe(nh, fl4->
daddr, new_gw,
725 #ifdef CONFIG_IP_ROUTE_VERBOSE
726 if (IN_DEV_LOG_MARTIANS(in_dev)) {
732 " Advised path = %pI4 -> %pI4\n",
733 &old_gw, dev->
name, &new_gw,
745 rt = (
struct rtable *) dst;
747 ip_rt_build_flow_key(&fl4, sk, skb);
748 __ip_do_redirect(rt, skb, &fl4,
true);
787 struct rtable *rt = skb_rtable(skb);
788 struct in_device *in_dev;
794 in_dev = __in_dev_get_rcu(rt->
dst.dev);
795 if (!in_dev || !IN_DEV_TX_REDIRECTS(in_dev)) {
799 log_martians = IN_DEV_LOG_MARTIANS(in_dev);
802 net = dev_net(rt->
dst.dev);
803 peer = inet_getpeer_v4(net->
ipv4.peers, ip_hdr(skb)->
saddr, 1);
806 rt_nexthop(rt, ip_hdr(skb)->daddr));
831 __be32 gw = rt_nexthop(rt, ip_hdr(skb)->daddr);
836 #ifdef CONFIG_IP_ROUTE_VERBOSE
840 &ip_hdr(skb)->
saddr, inet_iif(skb),
841 &ip_hdr(skb)->daddr, &gw);
848 static int ip_error(
struct sk_buff *skb)
850 struct in_device *in_dev = __in_dev_get_rcu(skb->
dev);
851 struct rtable *rt = skb_rtable(skb);
858 net = dev_net(rt->
dst.dev);
859 if (!IN_DEV_FORWARD(in_dev)) {
860 switch (rt->
dst.error) {
872 switch (rt->
dst.error) {
888 peer = inet_getpeer_v4(net->
ipv4.peers, ip_hdr(skb)->
saddr, 1);
910 static void __ip_rt_update_pmtu(
struct rtable *rt,
struct flowi4 *fl4,
u32 mtu)
915 if (dst->
dev->mtu < mtu)
918 if (mtu < ip_rt_min_pmtu)
919 mtu = ip_rt_min_pmtu;
929 if (fib_lookup(dev_net(dst->
dev), fl4, &
res) == 0) {
932 update_or_create_fnhe(nh, fl4->
daddr, 0, mtu,
933 jiffies + ip_rt_mtu_expires);
938 static void ip_rt_update_pmtu(
struct dst_entry *dst,
struct sock *sk,
944 ip_rt_build_flow_key(&fl4, sk, skb);
945 __ip_rt_update_pmtu(rt, &fl4, mtu);
955 __build_flow_key(&fl4,
NULL, iph, oif,
959 __ip_rt_update_pmtu(rt, &fl4, mtu);
971 __build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0);
974 __ip_rt_update_pmtu(rt, &fl4, mtu);
987 __build_flow_key(&fl4,
NULL, iph, oif,
991 __ip_do_redirect(rt, skb, &fl4,
false);
1003 __build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0);
1006 __ip_do_redirect(rt, skb, &fl4,
false);
1029 static void ipv4_link_failure(
struct sk_buff *skb)
1035 rt = skb_rtable(skb);
1037 dst_set_expires(&rt->
dst, 0);
1040 static int ip_rt_bug(
struct sk_buff *skb)
1043 __func__, &ip_hdr(skb)->
saddr, &ip_hdr(skb)->daddr,
1044 skb->
dev ? skb->
dev->name :
"?");
1063 if (rt_is_output_route(rt))
1064 src = ip_hdr(skb)->saddr;
1072 memset(&fl4, 0,
sizeof(fl4));
1076 fl4.flowi4_oif = rt->
dst.dev->ifindex;
1077 fl4.flowi4_iif = skb->
dev->ifindex;
1078 fl4.flowi4_mark = skb->
mark;
1081 if (fib_lookup(dev_net(rt->
dst.dev), &fl4, &res) == 0)
1085 rt_nexthop(rt, iph->
daddr),
1092 #ifdef CONFIG_IP_ROUTE_CLASSID
1095 if (!(rt->
dst.tclassid & 0xFFFF))
1096 rt->
dst.tclassid |= tag & 0xFFFF;
1097 if (!(rt->
dst.tclassid & 0xFFFF0000))
1098 rt->
dst.tclassid |= tag & 0xFFFF0000;
1102 static unsigned int ipv4_default_advmss(
const struct dst_entry *dst)
1104 unsigned int advmss = dst_metric_raw(dst,
RTAX_ADVMSS);
1107 advmss =
max_t(
unsigned int, dst->
dev->mtu - 40,
1109 if (advmss > 65535 - 40)
1110 advmss = 65535 - 40;
1115 static unsigned int ipv4_mtu(
const struct dst_entry *dst)
1118 unsigned int mtu = rt->
rt_pmtu;
1121 mtu = dst_metric_raw(dst,
RTAX_MTU);
1123 if (mtu && rt_is_output_route(rt))
1126 mtu = dst->
dev->mtu;
1148 hval = fnhe_hashfun(daddr);
1163 spin_lock_bh(&fnhe_lock);
1167 if (orig && rt_is_expired(orig)) {
1174 unsigned long diff = expires -
jiffies;
1178 dst_set_expires(&rt->
dst, diff);
1195 spin_unlock_bh(&fnhe_lock);
1200 static bool rt_cache_route(
struct fib_nh *nh,
struct rtable *rt)
1205 if (rt_is_input_route(rt)) {
1225 static void rt_add_uncached_list(
struct rtable *rt)
1227 spin_lock_bh(&rt_uncached_lock);
1229 spin_unlock_bh(&rt_uncached_lock);
1232 static void ipv4_dst_destroy(
struct dst_entry *dst)
1237 spin_lock_bh(&rt_uncached_lock);
1239 spin_unlock_bh(&rt_uncached_lock);
1245 if (!list_empty(&rt_uncached_list)) {
1246 struct net *net = dev_net(dev);
1249 spin_lock_bh(&rt_uncached_lock);
1251 if (rt->
dst.dev != dev)
1254 dev_hold(rt->
dst.dev);
1257 spin_unlock_bh(&rt_uncached_lock);
1261 static bool rt_cache_valid(
const struct rtable *rt)
1268 static void rt_set_nexthop(
struct rtable *rt,
__be32 daddr,
1283 #ifdef CONFIG_IP_ROUTE_CLASSID
1284 rt->
dst.tclassid = nh->nh_tclassid;
1287 cached = rt_bind_exception(rt, fnhe, daddr);
1289 cached = rt_cache_route(nh, rt);
1299 rt_add_uncached_list(rt);
1302 rt_add_uncached_list(rt);
1304 #ifdef CONFIG_IP_ROUTE_CLASSID
1305 #ifdef CONFIG_IP_MULTIPLE_TABLES
1308 set_class_tag(rt, itag);
1313 bool nopolicy,
bool noxfrm,
bool will_cache)
1326 struct in_device *in_dev = __in_dev_get_rcu(dev);
1335 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1339 if (
likely(!IN_DEV_ROUTE_LOCALNET(in_dev)))
1340 if (ipv4_is_loopback(saddr))
1343 if (ipv4_is_zeronet(saddr)) {
1344 if (!ipv4_is_local_multicast(daddr))
1353 IN_DEV_CONF_GET(in_dev, NOPOLICY),
false,
false);
1357 #ifdef CONFIG_IP_ROUTE_CLASSID
1358 rth->
dst.tclassid = itag;
1360 rth->
dst.output = ip_rt_bug;
1362 rth->
rt_genid = rt_genid(dev_net(dev));
1376 #ifdef CONFIG_IP_MROUTE
1377 if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev))
1382 skb_dst_set(skb, &rth->
dst);
1394 static void ip_handle_martian_source(
struct net_device *dev,
1395 struct in_device *in_dev,
1401 #ifdef CONFIG_IP_ROUTE_VERBOSE
1407 pr_warn(
"martian source %pI4 from %pI4, on dev %s\n",
1408 &daddr, &saddr, dev->
name);
1412 skb_mac_header(skb),
1420 static int __mkroute_input(
struct sk_buff *skb,
1422 struct in_device *in_dev,
1427 struct in_device *out_dev;
1428 unsigned int flags = 0;
1434 if (out_dev ==
NULL) {
1440 in_dev->dev, in_dev, &itag);
1442 ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
1448 do_cache = res->
fi && !itag;
1449 if (out_dev == in_dev && err && IN_DEV_TX_REDIRECTS(out_dev) &&
1450 (IN_DEV_SHARED_MEDIA(out_dev) ||
1464 if (out_dev == in_dev &&
1465 IN_DEV_PROXY_ARP_PVLAN(in_dev) == 0) {
1473 if (rt_cache_valid(rth)) {
1479 rth = rt_dst_alloc(out_dev->dev,
1480 IN_DEV_CONF_GET(in_dev, NOPOLICY),
1481 IN_DEV_CONF_GET(out_dev, NOXFRM), do_cache);
1500 rt_set_nexthop(rth, daddr, res,
NULL, res->
fi, res->
type, itag);
1501 skb_dst_set(skb, &rth->
dst);
1508 static int ip_mkroute_input(
struct sk_buff *skb,
1510 const struct flowi4 *fl4,
1511 struct in_device *in_dev,
1514 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1515 if (res->
fi && res->
fi->fib_nhs > 1)
1520 return __mkroute_input(skb, res, in_dev, daddr, saddr, tos);
1538 struct in_device *in_dev = __in_dev_get_rcu(dev);
1540 unsigned int flags = 0;
1544 struct net *net = dev_net(dev);
1556 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr))
1557 goto martian_source;
1560 if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0))
1566 if (ipv4_is_zeronet(saddr))
1567 goto martian_source;
1569 if (ipv4_is_zeronet(daddr))
1570 goto martian_destination;
1575 if (ipv4_is_loopback(daddr)) {
1576 if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
1577 goto martian_destination;
1578 }
else if (ipv4_is_loopback(saddr)) {
1579 if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
1580 goto martian_source;
1587 fl4.flowi4_iif = dev->
ifindex;
1588 fl4.flowi4_mark = skb->
mark;
1589 fl4.flowi4_tos = tos;
1593 err = fib_lookup(net, &fl4, &res);
1605 dev, in_dev, &itag);
1607 goto martian_source_keep_err;
1611 if (!IN_DEV_FORWARD(in_dev))
1614 goto martian_destination;
1616 err = ip_mkroute_input(skb, &res, &fl4, in_dev, daddr, saddr, tos);
1623 if (!ipv4_is_zeronet(saddr)) {
1627 goto martian_source_keep_err;
1638 if (rt_cache_valid(rth)) {
1648 IN_DEV_CONF_GET(in_dev, NOPOLICY),
false, do_cache);
1653 rth->
dst.output= ip_rt_bug;
1654 #ifdef CONFIG_IP_ROUTE_CLASSID
1655 rth->
dst.tclassid = itag;
1668 rth->
dst.input= ip_error;
1674 skb_dst_set(skb, &rth->
dst);
1688 martian_destination:
1690 #ifdef CONFIG_IP_ROUTE_VERBOSE
1691 if (IN_DEV_LOG_MARTIANS(in_dev))
1693 &daddr, &saddr, dev->
name);
1706 martian_source_keep_err:
1707 ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
1729 if (ipv4_is_multicast(daddr)) {
1730 struct in_device *in_dev = __in_dev_get_rcu(dev);
1736 #ifdef CONFIG_IP_MROUTE
1738 (!ipv4_is_local_multicast(daddr) &&
1739 IN_DEV_MFORWARD(in_dev))
1742 int res = ip_route_input_mc(skb, daddr, saddr,
1751 res = ip_route_input_slow(skb, daddr, saddr, tos, dev);
1759 const struct flowi4 *fl4,
int orig_oif,
1765 struct in_device *in_dev;
1770 in_dev = __in_dev_get_rcu(dev_out);
1774 if (
likely(!IN_DEV_ROUTE_LOCALNET(in_dev)))
1778 if (ipv4_is_lbcast(fl4->
daddr))
1780 else if (ipv4_is_multicast(fl4->
daddr))
1782 else if (ipv4_is_zeronet(fl4->
daddr))
1808 do_cache &= fi !=
NULL;
1813 fnhe = find_exception(nh, fl4->
daddr);
1827 if (rt_cache_valid(rth)) {
1828 dst_hold(&rth->
dst);
1834 rth = rt_dst_alloc(dev_out,
1835 IN_DEV_CONF_GET(in_dev, NOPOLICY),
1836 IN_DEV_CONF_GET(in_dev, NOXFRM),
1843 rth->
rt_genid = rt_genid(dev_net(dev_out));
1847 rth->
rt_iif = orig_oif ? : 0;
1858 if (flags & RTCF_LOCAL &&
1863 #ifdef CONFIG_IP_MROUTE
1865 if (IN_DEV_MFORWARD(in_dev) &&
1866 !ipv4_is_local_multicast(fl4->
daddr)) {
1874 rt_set_nexthop(rth, fl4->
daddr, res, fnhe, fi, type, 0);
1887 unsigned int flags = 0;
1896 orig_oif = fl4->flowi4_oif;
1906 if (ipv4_is_multicast(fl4->
saddr) ||
1907 ipv4_is_lbcast(fl4->
saddr) ||
1908 ipv4_is_zeronet(fl4->
saddr))
1919 if (fl4->flowi4_oif == 0 &&
1920 (ipv4_is_multicast(fl4->
daddr) ||
1921 ipv4_is_lbcast(fl4->
daddr))) {
1924 if (dev_out ==
NULL)
1942 fl4->flowi4_oif = dev_out->
ifindex;
1954 if (fl4->flowi4_oif) {
1957 if (dev_out ==
NULL)
1961 if (!(dev_out->
flags &
IFF_UP) || !__in_dev_get_rcu(dev_out)) {
1965 if (ipv4_is_local_multicast(fl4->
daddr) ||
1966 ipv4_is_lbcast(fl4->
daddr)) {
1973 if (ipv4_is_multicast(fl4->
daddr))
1976 else if (!fl4->
daddr)
1993 if (fib_lookup(net, fl4, &res)) {
1996 if (fl4->flowi4_oif) {
2015 if (fl4->
saddr == 0)
2027 if (res.
fi->fib_prefsrc)
2028 fl4->
saddr = res.
fi->fib_prefsrc;
2033 fl4->flowi4_oif = dev_out->
ifindex;
2038 #ifdef CONFIG_IP_ROUTE_MULTIPATH
2039 if (res.
fi->fib_nhs > 1 && fl4->flowi4_oif == 0)
2044 res.
table->tb_num_default > 1 &&
2052 fl4->flowi4_oif = dev_out->
ifindex;
2056 rth = __mkroute_output(&res, fl4, orig_oif, dev_out, flags);
2069 static unsigned int ipv4_blackhole_mtu(
const struct dst_entry *dst)
2071 unsigned int mtu = dst_metric_raw(dst,
RTAX_MTU);
2073 return mtu ? : dst->
dev->mtu;
2076 static void ipv4_rt_blackhole_update_pmtu(
struct dst_entry *dst,
struct sock *sk,
2081 static void ipv4_rt_blackhole_redirect(
struct dst_entry *dst,
struct sock *sk,
2086 static u32 *ipv4_rt_blackhole_cow_metrics(
struct dst_entry *dst,
2092 static struct dst_ops ipv4_dst_blackhole_ops = {
2095 .check = ipv4_blackhole_dst_check,
2096 .mtu = ipv4_blackhole_mtu,
2097 .default_advmss = ipv4_default_advmss,
2098 .update_pmtu = ipv4_rt_blackhole_update_pmtu,
2099 .redirect = ipv4_rt_blackhole_redirect,
2100 .cow_metrics = ipv4_rt_blackhole_cow_metrics,
2101 .neigh_lookup = ipv4_neigh_lookup,
2117 new->dev = ort->
dst.dev;
2138 return rt ? &rt->
dst : ERR_PTR(-
ENOMEM);
2149 if (flp4->flowi4_proto)
2151 flowi4_to_flowi(flp4),
2158 static int rt_fill_info(
struct net *net,
__be32 dst,
__be32 src,
2160 u32 seq,
int event,
int nowait,
unsigned int flags)
2162 struct rtable *rt = skb_rtable(skb);
2165 unsigned long expires = 0;
2169 nlh = nlmsg_put(skb, portid, seq, event,
sizeof(*r), flags);
2173 r = nlmsg_data(nlh);
2180 goto nla_put_failure;
2188 if (nla_put_be32(skb,
RTA_DST, dst))
2189 goto nla_put_failure;
2192 if (nla_put_be32(skb,
RTA_SRC, src))
2193 goto nla_put_failure;
2196 nla_put_u32(skb,
RTA_OIF, rt->
dst.dev->ifindex))
2197 goto nla_put_failure;
2198 #ifdef CONFIG_IP_ROUTE_CLASSID
2199 if (rt->
dst.tclassid &&
2201 goto nla_put_failure;
2203 if (!rt_is_input_route(rt) &&
2206 goto nla_put_failure;
2210 goto nla_put_failure;
2212 expires = rt->
dst.expires;
2214 unsigned long now = jiffies;
2222 memcpy(metrics, dst_metrics_ptr(&rt->
dst),
sizeof(metrics));
2226 goto nla_put_failure;
2228 if (fl4->flowi4_mark &&
2229 nla_put_u32(skb,
RTA_MARK, fl4->flowi4_mark))
2230 goto nla_put_failure;
2232 error = rt->
dst.error;
2234 if (rt_is_input_route(rt)) {
2236 goto nla_put_failure;
2240 goto nla_put_failure;
2242 return nlmsg_end(skb, nlh);
2245 nlmsg_cancel(skb, nlh);
2249 static int inet_rtm_getroute(
struct sk_buff *in_skb,
struct nlmsghdr *nlh,
void *
arg)
2251 struct net *net = sock_net(in_skb->
sk);
2267 rtm = nlmsg_data(nlh);
2278 skb_reset_mac_header(skb);
2279 skb_reset_network_header(skb);
2290 memset(&fl4, 0,
sizeof(fl4));
2293 fl4.flowi4_tos = rtm->
rtm_tos;
2295 fl4.flowi4_mark =
mark;
2310 err = ip_route_input(skb, dst, src, rtm->
rtm_tos, dev);
2313 rt = skb_rtable(skb);
2314 if (err == 0 && rt->
dst.error)
2315 err = -rt->
dst.error;
2317 rt = ip_route_output_key(net, &fl4);
2327 skb_dst_set(skb, &rt->
dst);
2331 err = rt_fill_info(net, dst, src, &fl4, skb,
2356 #ifdef CONFIG_SYSCTL
2357 static int ipv4_sysctl_rtcache_flush(
ctl_table *__ctl,
int write,
2359 size_t *lenp, loff_t *ppos)
2373 .maxlen =
sizeof(
int),
2378 .procname =
"max_size",
2379 .data = &ip_rt_max_size,
2380 .maxlen =
sizeof(
int),
2387 .procname =
"gc_min_interval",
2388 .data = &ip_rt_gc_min_interval,
2389 .maxlen =
sizeof(
int),
2394 .procname =
"gc_min_interval_ms",
2395 .data = &ip_rt_gc_min_interval,
2396 .maxlen =
sizeof(
int),
2401 .procname =
"gc_timeout",
2402 .data = &ip_rt_gc_timeout,
2403 .maxlen =
sizeof(
int),
2408 .procname =
"gc_interval",
2409 .data = &ip_rt_gc_interval,
2410 .maxlen =
sizeof(
int),
2415 .procname =
"redirect_load",
2416 .data = &ip_rt_redirect_load,
2417 .maxlen =
sizeof(
int),
2422 .procname =
"redirect_number",
2423 .data = &ip_rt_redirect_number,
2424 .maxlen =
sizeof(
int),
2429 .procname =
"redirect_silence",
2430 .data = &ip_rt_redirect_silence,
2431 .maxlen =
sizeof(
int),
2436 .procname =
"error_cost",
2437 .data = &ip_rt_error_cost,
2438 .maxlen =
sizeof(
int),
2443 .procname =
"error_burst",
2444 .data = &ip_rt_error_burst,
2445 .maxlen =
sizeof(
int),
2450 .procname =
"gc_elasticity",
2451 .data = &ip_rt_gc_elasticity,
2452 .maxlen =
sizeof(
int),
2457 .procname =
"mtu_expires",
2458 .data = &ip_rt_mtu_expires,
2459 .maxlen =
sizeof(
int),
2464 .procname =
"min_pmtu",
2465 .data = &ip_rt_min_pmtu,
2466 .maxlen =
sizeof(
int),
2471 .procname =
"min_adv_mss",
2472 .data = &ip_rt_min_advmss,
2473 .maxlen =
sizeof(
int),
2480 static struct ctl_table ipv4_route_flush_table[] = {
2483 .maxlen =
sizeof(
int),
2490 static __net_init int sysctl_route_net_init(
struct net *net)
2494 tbl = ipv4_route_flush_table;
2508 if (tbl != ipv4_route_flush_table)
2514 static __net_exit void sysctl_route_net_exit(
struct net *net)
2518 tbl = net->
ipv4.route_hdr->ctl_table_arg;
2520 BUG_ON(tbl == ipv4_route_flush_table);
2525 .
init = sysctl_route_net_init,
2526 .exit = sysctl_route_net_exit,
2530 static __net_init int rt_genid_init(
struct net *net)
2534 sizeof(net->
ipv4.dev_addr_genid));
2539 .init = rt_genid_init,
2542 static int __net_init ipv4_inetpeer_init(
struct net *net)
2549 net->
ipv4.peers = bp;
2553 static void __net_exit ipv4_inetpeer_exit(
struct net *net)
2563 .init = ipv4_inetpeer_init,
2564 .exit = ipv4_inetpeer_exit,
2567 #ifdef CONFIG_IP_ROUTE_CLASSID
2575 #ifdef CONFIG_IP_ROUTE_CLASSID
2578 panic(
"IP: failed to allocate ip_rt_acct\n");
2587 if (dst_entries_init(&ipv4_dst_ops) < 0)
2588 panic(
"IP: failed to allocate ipv4_dst_ops counter\n");
2590 if (dst_entries_init(&ipv4_dst_blackhole_ops) < 0)
2591 panic(
"IP: failed to allocate ipv4_dst_blackhole_ops counter\n");
2599 if (ip_rt_proc_init())
2600 pr_err(
"Unable to create route proc files\n");
2607 #ifdef CONFIG_SYSCTL
2615 #ifdef CONFIG_SYSCTL