27 #define pr_fmt(fmt) "IPv6: " fmt
29 #include <linux/capability.h>
30 #include <linux/errno.h>
31 #include <linux/export.h>
32 #include <linux/types.h>
34 #include <linux/socket.h>
36 #include <linux/net.h>
38 #include <linux/netdevice.h>
39 #include <linux/in6.h>
40 #include <linux/mroute6.h>
42 #include <linux/if_arp.h>
46 #include <linux/slab.h>
55 #include <linux/rtnetlink.h>
61 #include <asm/uaccess.h>
64 #include <linux/sysctl.h>
70 static unsigned int ip6_default_advmss(
const struct dst_entry *
dst);
71 static unsigned int ip6_mtu(
const struct dst_entry *
dst);
73 static void ip6_dst_destroy(
struct dst_entry *);
74 static void ip6_dst_ifdown(
struct dst_entry *,
78 static int ip6_pkt_discard(
struct sk_buff *
skb);
79 static int ip6_pkt_discard_out(
struct sk_buff *
skb);
80 static void ip6_link_failure(
struct sk_buff *
skb);
86 #ifdef CONFIG_IPV6_ROUTE_INFO
89 const struct in6_addr *gwaddr,
int ifindex,
93 const struct in6_addr *gwaddr,
int ifindex);
105 peer = rt6_get_peer_create(rt);
108 unsigned long prev,
new;
111 if (inet_metrics_new(peer))
114 new = (
unsigned long) p;
126 static inline const void *choose_neigh_daddr(
struct rt6_info *rt,
132 if (!ipv6_addr_any(p))
133 return (
const void *)
p;
135 return &ipv6_hdr(skb)->daddr;
146 daddr = choose_neigh_daddr(rt, skb, daddr);
147 n = __ipv6_neigh_lookup(&
nd_tbl, dst->
dev, daddr);
150 return neigh_create(&
nd_tbl, daddr, dst->
dev);
166 static struct dst_ops ip6_dst_ops_template = {
171 .check = ip6_dst_check,
172 .default_advmss = ip6_default_advmss,
174 .cow_metrics = ipv6_cow_metrics,
175 .destroy = ip6_dst_destroy,
176 .ifdown = ip6_dst_ifdown,
177 .negative_advice = ip6_negative_advice,
178 .link_failure = ip6_link_failure,
179 .update_pmtu = ip6_rt_update_pmtu,
180 .redirect = rt6_do_redirect,
182 .neigh_lookup = ip6_neigh_lookup,
185 static unsigned int ip6_blackhole_mtu(
const struct dst_entry *dst)
189 return mtu ? : dst->
dev->mtu;
192 static void ip6_rt_blackhole_update_pmtu(
struct dst_entry *dst,
struct sock *
sk,
197 static void ip6_rt_blackhole_redirect(
struct dst_entry *dst,
struct sock *
sk,
202 static u32 *ip6_rt_blackhole_cow_metrics(
struct dst_entry *dst,
208 static struct dst_ops ip6_dst_blackhole_ops = {
211 .destroy = ip6_dst_destroy,
212 .check = ip6_dst_check,
213 .mtu = ip6_blackhole_mtu,
214 .default_advmss = ip6_default_advmss,
215 .update_pmtu = ip6_rt_blackhole_update_pmtu,
216 .redirect = ip6_rt_blackhole_redirect,
217 .cow_metrics = ip6_rt_blackhole_cow_metrics,
218 .neigh_lookup = ip6_neigh_lookup,
221 static const u32 ip6_template_metrics[
RTAX_MAX] = {
225 static const struct rt6_info ip6_null_entry_template = {
231 .input = ip6_pkt_discard,
232 .output = ip6_pkt_discard_out,
240 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
242 static int ip6_pkt_prohibit(
struct sk_buff *skb);
243 static int ip6_pkt_prohibit_out(
struct sk_buff *skb);
245 static const struct rt6_info ip6_prohibit_entry_template = {
251 .input = ip6_pkt_prohibit,
252 .output = ip6_pkt_prohibit_out,
260 static const struct rt6_info ip6_blk_hole_entry_template = {
289 memset(dst + 1, 0,
sizeof(*rt) -
sizeof(*dst));
290 rt6_init_peer(rt, table ? &table->
tb6_peers : net->ipv6.peers);
296 static void ip6_dst_destroy(
struct dst_entry *dst)
302 neigh_release(rt->
n);
305 dst_destroy_metrics_generic(dst);
315 if (rt6_has_peer(rt)) {
316 struct inet_peer *peer = rt6_peer_ptr(rt);
323 static u32 rt6_peer_genid(
void)
337 peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
339 if (!rt6_set_peer(rt, peer))
352 dev_net(dev)->loopback_dev;
354 if (dev != loopback_dev) {
355 if (idev && idev->
dev == dev) {
357 in6_dev_get(loopback_dev);
363 if (rt->
n && rt->
n->dev == dev) {
364 rt->
n->dev = loopback_dev;
365 dev_hold(loopback_dev);
371 static bool rt6_check_expired(
const struct rt6_info *rt)
376 }
else if (rt->
dst.from) {
377 return rt6_check_expired((
struct rt6_info *) rt->
dst.from);
382 static bool rt6_need_strict(
const struct in6_addr *daddr)
384 return ipv6_addr_type(daddr) &
392 static inline struct rt6_info *rt6_device_match(
struct net *net,
401 if (!oif && ipv6_addr_any(saddr))
404 for (sprt = rt; sprt; sprt = sprt->
dst.rt6_next) {
415 if (local && (!oif ||
433 return net->ipv6.ip6_null_entry;
439 #ifdef CONFIG_IPV6_ROUTER_PREF
440 static void rt6_probe(
struct rt6_info *rt)
451 neigh = rt ? rt->
n :
NULL;
464 addrconf_addr_solict_mult(target, &mcaddr);
471 static inline void rt6_probe(
struct rt6_info *rt)
479 static inline int rt6_check_dev(
struct rt6_info *rt,
int oif)
482 if (!oif || dev->
ifindex == oif)
490 static inline int rt6_check_neigh(
struct rt6_info *rt)
503 #ifdef CONFIG_IPV6_ROUTER_PREF
515 static int rt6_score_route(
struct rt6_info *rt,
int oif,
520 m = rt6_check_dev(rt, oif);
523 #ifdef CONFIG_IPV6_ROUTER_PREF
526 n = rt6_check_neigh(rt);
532 static struct rt6_info *find_match(
struct rt6_info *rt,
int oif,
int strict,
537 if (rt6_check_expired(rt))
540 m = rt6_score_route(rt, oif, strict);
559 u32 metric,
int oif,
int strict)
565 for (rt = rr_head; rt && rt->
rt6i_metric == metric;
566 rt = rt->
dst.rt6_next)
567 match = find_match(rt, oif, strict, &mpri, match);
569 rt = rt->
dst.rt6_next)
570 match = find_match(rt, oif, strict, &mpri, match);
584 match = find_rr_leaf(fn, rt0, rt0->
rt6i_metric, oif, strict);
598 net = dev_net(rt0->
dst.dev);
599 return match ? match : net->ipv6.ip6_null_entry;
602 #ifdef CONFIG_IPV6_ROUTE_INFO
606 struct net *net = dev_net(dev);
610 unsigned long lifetime;
632 pref = rinfo->route_pref;
642 ipv6_addr_prefix(&prefix_buf,
645 prefix = &prefix_buf;
648 rt = rt6_get_route_info(net, prefix, rinfo->
prefix_len, gwaddr,
651 if (rt && !lifetime) {
664 if (!addrconf_finite_timeout(lifetime))
665 rt6_clean_expires(rt);
667 rt6_set_expires(rt, jiffies +
HZ * lifetime);
675 #define BACKTRACK(__net, saddr) \
677 if (rt == __net->ipv6.ip6_null_entry) { \
678 struct fib6_node *pn; \
680 if (fn->fn_flags & RTN_TL_ROOT) \
683 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
684 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
687 if (fn->fn_flags & RTN_RTINFO) \
693 static struct rt6_info *ip6_pol_route_lookup(
struct net *net,
695 struct flowi6 *fl6,
int flags)
704 rt = rt6_device_match(net, rt, &fl6->
saddr, fl6->flowi6_oif, flags);
721 const struct in6_addr *saddr,
int oif,
int strict)
728 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
770 return __ip6_ins_rt(rt, &info);
783 rt = ip6_rt_copy(ort, daddr);
789 if (ort->rt6i_dst.plen != 128 &&
790 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
797 #ifdef CONFIG_IPV6_SUBTREES
805 if (rt6_bind_neighbour(rt, rt->
dst.dev)) {
806 struct net *net = dev_net(rt->
dst.dev);
807 int saved_rt_min_interval =
808 net->ipv6.sysctl.ip6_rt_gc_min_interval;
809 int saved_rt_elasticity =
810 net->ipv6.sysctl.ip6_rt_gc_elasticity;
812 if (attempts-- > 0) {
813 net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
814 net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
816 ip6_dst_gc(&net->ipv6.ip6_dst_ops);
818 net->ipv6.sysctl.ip6_rt_gc_elasticity =
820 net->ipv6.sysctl.ip6_rt_gc_min_interval =
821 saved_rt_min_interval;
837 struct rt6_info *rt = ip6_rt_copy(ort, daddr);
841 rt->
n = neigh_clone(ort->
n);
846 static struct rt6_info *ip6_pol_route(
struct net *net,
struct fib6_table *table,
int oif,
847 struct flowi6 *fl6,
int flags)
865 rt = rt6_select(fn, oif, strict | reachable);
868 if (rt == net->ipv6.ip6_null_entry ||
876 nrt = rt6_alloc_cow(rt, &fl6->
daddr, &fl6->
saddr);
878 nrt = rt6_alloc_clone(rt, &fl6->
daddr);
883 rt = nrt ? : net->ipv6.ip6_null_entry;
916 static struct rt6_info *ip6_pol_route_input(
struct net *net,
struct fib6_table *table,
917 struct flowi6 *fl6,
int flags)
919 return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
922 static struct dst_entry *ip6_route_input_lookup(
struct net *net,
924 struct flowi6 *fl6,
int flags)
934 const struct ipv6hdr *iph = ipv6_hdr(skb);
935 struct net *net = dev_net(skb->
dev);
938 .flowi6_iif = skb->
dev->ifindex,
942 .flowi6_mark = skb->
mark,
946 skb_dst_set(skb, ip6_route_input_lookup(net, skb->
dev, &fl6, flags));
949 static struct rt6_info *ip6_pol_route_output(
struct net *net,
struct fib6_table *table,
950 struct flowi6 *fl6,
int flags)
952 return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
962 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->
daddr))
965 if (!ipv6_addr_any(&fl6->
saddr))
968 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
984 memset(
new + 1, 0,
sizeof(*rt) -
sizeof(*
new));
985 rt6_init_peer(rt, net->ipv6.peers);
991 if (dst_metrics_read_only(&ort->
dst))
992 new->_metrics = ort->
dst._metrics;
994 dst_copy_metrics(
new, &ort->
dst);
1001 rt6_clean_expires(rt);
1004 memcpy(&rt->rt6i_dst, &ort->rt6i_dst,
sizeof(
struct rt6key));
1005 #ifdef CONFIG_IPV6_SUBTREES
1013 return new ?
new : ERR_PTR(-
ENOMEM);
1035 if (!rt6_has_peer(rt))
1050 if (rt6_check_expired(rt)) {
1062 static void ip6_link_failure(
struct sk_buff *skb)
1068 rt = (
struct rt6_info *) skb_dst(skb);
1071 rt6_update_expires(rt, 0);
1077 static void ip6_rt_update_pmtu(
struct dst_entry *dst,
struct sock *
sk,
1083 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
1084 struct net *net = dev_net(dst->
dev);
1093 dst_metric_set(dst,
RTAX_MTU, mtu);
1094 rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
1105 memset(&fl6, 0,
sizeof(fl6));
1106 fl6.flowi6_oif = oif;
1107 fl6.flowi6_mark =
mark;
1108 fl6.flowi6_flags = 0;
1115 ip6_rt_update_pmtu(dst,
NULL, skb,
ntohl(mtu));
1123 sk->sk_bound_dev_if, sk->
sk_mark);
1133 memset(&fl6, 0,
sizeof(fl6));
1134 fl6.flowi6_oif = oif;
1135 fl6.flowi6_mark =
mark;
1136 fl6.flowi6_flags = 0;
1143 rt6_do_redirect(dst,
NULL, skb);
1154 static unsigned int ip6_default_advmss(
const struct dst_entry *dst)
1157 unsigned int mtu = dst_mtu(dst);
1158 struct net *net = dev_net(dev);
1162 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
1163 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
1176 static unsigned int ip6_mtu(
const struct dst_entry *dst)
1179 unsigned int mtu = dst_metric_raw(dst,
RTAX_MTU);
1187 idev = __in6_dev_get(dst->
dev);
1189 mtu = idev->
cnf.mtu6;
1195 static struct dst_entry *icmp6_dst_gc_list;
1204 struct inet6_dev *idev = in6_dev_get(dev);
1205 struct net *net = dev_net(dev);
1210 rt = ip6_dst_alloc(net, dev, 0,
NULL);
1221 if (IS_ERR(neigh)) {
1224 return ERR_CAST(neigh);
1232 rt->rt6i_dst.addr = fl6->
daddr;
1233 rt->rt6i_dst.plen = 128;
1237 spin_lock_bh(&icmp6_dst_lock);
1238 rt->
dst.next = icmp6_dst_gc_list;
1239 icmp6_dst_gc_list = &rt->
dst;
1240 spin_unlock_bh(&icmp6_dst_lock);
1255 spin_lock_bh(&icmp6_dst_lock);
1256 pprev = &icmp6_dst_gc_list;
1258 while ((dst = *pprev) !=
NULL) {
1268 spin_unlock_bh(&icmp6_dst_lock);
1273 static void icmp6_clean_all(
int (*
func)(
struct rt6_info *rt,
void *
arg),
1278 spin_lock_bh(&icmp6_dst_lock);
1279 pprev = &icmp6_dst_gc_list;
1280 while ((dst = *pprev) !=
NULL) {
1282 if (
func(rt, arg)) {
1289 spin_unlock_bh(&icmp6_dst_lock);
1292 static int ip6_dst_gc(
struct dst_ops *ops)
1295 struct net *net =
container_of(ops,
struct net, ipv6.ip6_dst_ops);
1296 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1297 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1298 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1299 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1300 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1303 entries = dst_entries_get_fast(ops);
1304 if (
time_after(rt_last_gc + rt_min_interval, now) &&
1305 entries <= rt_max_size)
1308 net->ipv6.ip6_rt_gc_expire++;
1310 net->ipv6.ip6_rt_last_gc = now;
1311 entries = dst_entries_get_slow(ops);
1312 if (entries < ops->gc_thresh)
1313 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
1315 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1316 return entries > rt_max_size;
1328 if (hoplimit == 0) {
1333 idev = __in6_dev_get(dev);
1335 hoplimit = idev->
cnf.hop_limit;
1337 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1351 struct net *net = cfg->
fc_nlinfo.nl_net;
1360 #ifndef CONFIG_IPV6_SUBTREES
1369 idev = in6_dev_get(dev);
1382 pr_warn(
"NLM_F_CREATE should be specified when creating new route\n");
1403 rt6_clean_expires(rt);
1409 addr_type = ipv6_addr_type(&cfg->
fc_dst);
1422 if (rt->rt6i_dst.plen == 128)
1431 dst_init_metrics(&rt->
dst, metrics, 0);
1433 #ifdef CONFIG_IPV6_SUBTREES
1455 idev = in6_dev_get(dev);
1461 rt->
dst.output = ip6_pkt_discard_out;
1462 rt->
dst.input = ip6_pkt_discard;
1487 gwa_type = ipv6_addr_type(gw_addr);
1509 if (dev != grt->
dst.dev) {
1546 err = rt6_bind_neighbour(rt, dev);
1567 dst_metric_set(&rt->
dst, type, nla_get_u32(nla));
1578 return __ip6_ins_rt(rt, &cfg->
fc_nlinfo);
1594 struct net *net = dev_net(rt->
dst.dev);
1596 if (rt == net->ipv6.ip6_null_entry) {
1616 return __ip6_del_rt(rt, &info);
1637 for (rt = fn->
leaf; rt; rt = rt->
dst.rt6_next) {
1650 return __ip6_del_rt(rt, &cfg->
fc_nlinfo);
1660 struct net *net = dev_net(skb->
dev);
1670 int optlen, on_link;
1681 icmph = icmp6_hdr(skb);
1682 target = (
const struct in6_addr *) (icmph + 1);
1685 if (ipv6_addr_is_multicast(dest)) {
1691 if (ipv6_addr_equal(dest, target)) {
1693 }
else if (ipv6_addr_type(target) !=
1699 in6_dev = __in6_dev_get(skb->
dev);
1702 if (in6_dev->
cnf.forwarding || !in6_dev->
cnf.accept_redirects)
1716 if (ndopts.nd_opts_tgt_lladdr) {
1717 lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1726 if (rt == net->ipv6.ip6_null_entry) {
1735 dst_confirm(&rt->
dst);
1737 neigh = __neigh_lookup(&
nd_tbl, target, skb->
dev, 1);
1743 if (neigh == old_neigh)
1757 nrt = ip6_rt_copy(rt, dest);
1766 nrt->
n = neigh_clone(neigh);
1771 netevent.old = &rt->
dst;
1772 netevent.old_neigh = old_neigh;
1773 netevent.new = &nrt->
dst;
1774 netevent.new_neigh = neigh;
1775 netevent.daddr =
dest;
1784 neigh_release(neigh);
1794 struct net *net = dev_net(ort->
dst.dev);
1795 struct rt6_info *rt = ip6_dst_alloc(net, ort->
dst.dev, 0,
1799 rt->
dst.input = ort->
dst.input;
1800 rt->
dst.output = ort->
dst.output;
1803 rt->rt6i_dst.addr = *
dest;
1804 rt->rt6i_dst.plen = 128;
1805 dst_copy_metrics(&rt->
dst, &ort->
dst);
1806 rt->
dst.error = ort->
dst.error;
1816 rt6_set_from(rt, ort);
1818 rt6_clean_expires(rt);
1821 #ifdef CONFIG_IPV6_SUBTREES
1830 #ifdef CONFIG_IPV6_ROUTE_INFO
1831 static struct rt6_info *rt6_get_route_info(
struct net *net,
1832 const struct in6_addr *prefix,
int prefixlen,
1833 const struct in6_addr *gwaddr,
int ifindex)
1848 for (rt = fn->
leaf; rt; rt = rt->
dst.rt6_next) {
1849 if (rt->
dst.dev->ifindex != ifindex)
1863 static struct rt6_info *rt6_add_route_info(
struct net *net,
1864 const struct in6_addr *prefix,
int prefixlen,
1865 const struct in6_addr *gwaddr,
int ifindex,
1871 .fc_ifindex = ifindex,
1872 .fc_dst_len = prefixlen,
1875 .fc_nlinfo.portid = 0,
1876 .fc_nlinfo.nlh =
NULL,
1877 .fc_nlinfo.nl_net = net,
1889 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
1903 for (rt = table->
tb6_root.leaf; rt; rt=rt->
dst.rt6_next) {
1904 if (dev == rt->
dst.dev &&
1925 .fc_nlinfo.portid = 0,
1926 .fc_nlinfo.nlh =
NULL,
1927 .fc_nlinfo.nl_net = dev_net(dev),
1949 for (rt = table->
tb6_root.leaf; rt; rt = rt->
dst.rt6_next) {
1960 static void rtmsg_to_fib6_config(
struct net *net,
1964 memset(cfg, 0,
sizeof(*cfg));
1997 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
2005 err = ip6_route_del(&cfg);
2022 static int ip6_pkt_drop(
struct sk_buff *skb,
u8 code,
int ipstats_mib_noroutes)
2026 switch (ipstats_mib_noroutes) {
2028 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
2037 ipstats_mib_noroutes);
2045 static int ip6_pkt_discard(
struct sk_buff *skb)
2050 static int ip6_pkt_discard_out(
struct sk_buff *skb)
2052 skb->
dev = skb_dst(skb)->dev;
2056 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2058 static int ip6_pkt_prohibit(
struct sk_buff *skb)
2063 static int ip6_pkt_prohibit_out(
struct sk_buff *skb)
2065 skb->
dev = skb_dst(skb)->dev;
2079 struct net *net = dev_net(idev->
dev);
2084 net_warn_ratelimited(
"Maximum number of routes reached, consider increasing route/max_size\n");
2100 err = rt6_bind_neighbour(rt, rt->
dst.dev);
2103 return ERR_PTR(err);
2106 rt->rt6i_dst.addr = *
addr;
2107 rt->rt6i_dst.plen = 128;
2127 daddr, prefs, saddr);
2138 static int fib6_remove_prefsrc(
struct rt6_info *rt,
void *arg)
2144 if (((
void *)rt->
dst.dev == dev || !
dev) &&
2145 rt != net->ipv6.ip6_null_entry &&
2155 struct net *net = dev_net(ifp->
idev->dev);
2169 static int fib6_ifdown(
struct rt6_info *rt,
void *arg)
2174 if ((rt->
dst.dev == dev || !dev) &&
2175 rt != adn->
net->ipv6.ip6_null_entry)
2189 icmp6_clean_all(fib6_ifdown, &adn);
2197 static int rt6_mtu_change_route(
struct rt6_info *rt,
void *p_arg)
2208 idev = __in6_dev_get(arg->
dev);
2226 if (rt->
dst.dev == arg->
dev &&
2228 (dst_mtu(&rt->
dst) >= arg->
mtu ||
2229 (dst_mtu(&rt->
dst) < arg->
mtu &&
2230 dst_mtu(&rt->
dst) == idev->
cnf.mtu6))) {
2254 static int rtm_to_fib6_config(
struct sk_buff *skb,
struct nlmsghdr *nlh,
2261 err = nlmsg_parse(nlh,
sizeof(*rtm), tb,
RTA_MAX, rtm_ipv6_policy);
2266 rtm = nlmsg_data(nlh);
2267 memset(cfg, 0,
sizeof(*cfg));
2319 cfg->
fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2322 cfg->
fc_mx = nla_data(tb[RTA_METRICS]);
2327 cfg->
fc_table = nla_get_u32(tb[RTA_TABLE]);
2334 static int inet6_rtm_delroute(
struct sk_buff *skb,
struct nlmsghdr* nlh,
void *arg)
2339 err = rtm_to_fib6_config(skb, nlh, &cfg);
2343 return ip6_route_del(&cfg);
2346 static int inet6_rtm_newroute(
struct sk_buff *skb,
struct nlmsghdr* nlh,
void *arg)
2351 err = rtm_to_fib6_config(skb, nlh, &cfg);
2358 static inline size_t rt6_nlmsg_size(
void)
2361 + nla_total_size(16)
2362 + nla_total_size(16)
2363 + nla_total_size(16)
2364 + nla_total_size(16)
2373 static int rt6_fill_node(
struct net *net,
2376 int iif,
int type,
u32 portid,
u32 seq,
2377 int prefix,
int nowait,
unsigned int flags)
2392 nlh = nlmsg_put(skb, portid, seq, type,
sizeof(*rtm), flags);
2396 rtm = nlmsg_data(nlh);
2407 goto nla_put_failure;
2409 switch (rt->
dst.error) {
2446 if (
nla_put(skb, RTA_DST, 16, dst))
2447 goto nla_put_failure;
2450 if (
nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2451 goto nla_put_failure;
2452 #ifdef CONFIG_IPV6_SUBTREES
2454 if (
nla_put(skb, RTA_SRC, 16, src))
2455 goto nla_put_failure;
2459 goto nla_put_failure;
2462 #ifdef CONFIG_IPV6_MROUTE
2463 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
2469 goto nla_put_failure;
2472 goto nla_put_failure;
2477 if (nla_put_u32(skb,
RTA_IIF, iif))
2478 goto nla_put_failure;
2482 nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2483 goto nla_put_failure;
2489 if (
nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2490 goto nla_put_failure;
2494 goto nla_put_failure;
2499 goto nla_put_failure;
2503 nla_put_u32(skb, RTA_OIF, rt->
dst.dev->ifindex))
2504 goto nla_put_failure;
2505 if (nla_put_u32(skb, RTA_PRIORITY, rt->
rt6i_metric))
2506 goto nla_put_failure;
2511 goto nla_put_failure;
2513 return nlmsg_end(skb, nlh);
2516 nlmsg_cancel(skb, nlh);
2525 if (nlmsg_len(arg->
cb->nlh) >=
sizeof(
struct rtmsg)) {
2526 struct rtmsg *rtm = nlmsg_data(arg->
cb->nlh);
2531 return rt6_fill_node(arg->
net,
2537 static int inet6_rtm_getroute(
struct sk_buff *in_skb,
struct nlmsghdr* nlh,
void *arg)
2539 struct net *net = sock_net(in_skb->
sk);
2545 int err, iif = 0, oif = 0;
2547 err = nlmsg_parse(nlh,
sizeof(*rtm), tb,
RTA_MAX, rtm_ipv6_policy);
2552 memset(&fl6, 0,
sizeof(fl6));
2555 if (nla_len(tb[RTA_SRC]) <
sizeof(
struct in6_addr))
2562 if (nla_len(tb[RTA_DST]) <
sizeof(
struct in6_addr))
2569 iif = nla_get_u32(tb[RTA_IIF]);
2572 oif = nla_get_u32(tb[RTA_OIF]);
2584 fl6.flowi6_iif = iif;
2586 if (!ipv6_addr_any(&fl6.
saddr))
2589 rt = (
struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
2592 fl6.flowi6_oif = oif;
2607 skb_reset_mac_header(skb);
2610 skb_dst_set(skb, &rt->
dst);
2612 err = rt6_fill_node(net, skb, rt, &fl6.
daddr, &fl6.
saddr, iif,
2628 struct net *net = info->
nl_net;
2633 seq = info->
nlh ? info->
nlh->nlmsg_seq : 0;
2635 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
2639 err = rt6_fill_node(net, skb, rt,
NULL,
NULL, 0,
2640 event, info->
portid, seq, 0, 0, 0);
2648 info->
nlh, gfp_any());
2659 struct net *net = dev_net(dev);
2662 net->ipv6.ip6_null_entry->dst.dev =
dev;
2663 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2664 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2665 net->ipv6.ip6_prohibit_entry->dst.dev =
dev;
2666 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2667 net->ipv6.ip6_blk_hole_entry->dst.dev =
dev;
2668 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2679 #ifdef CONFIG_PROC_FS
2690 static int rt6_info_route(
struct rt6_info *rt,
void *p_arg)
2695 seq_printf(m,
"%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2697 #ifdef CONFIG_IPV6_SUBTREES
2700 seq_puts(m,
"00000000000000000000000000000000 00 ");
2706 seq_puts(m,
"00000000000000000000000000000000");
2711 rt->
dst.dev ? rt->
dst.dev->name :
"");
2715 static int ipv6_route_show(
struct seq_file *m,
void *
v)
2717 struct net *net = (
struct net *)m->
private;
2729 .open = ipv6_route_open,
2735 static int rt6_stats_seq_show(
struct seq_file *seq,
void *v)
2737 struct net *net = (
struct net *)seq->
private;
2738 seq_printf(seq,
"%04x %04x %04x %04x %04x %04x %04x\n",
2739 net->ipv6.rt6_stats->fib_nodes,
2740 net->ipv6.rt6_stats->fib_route_nodes,
2741 net->ipv6.rt6_stats->fib_rt_alloc,
2742 net->ipv6.rt6_stats->fib_rt_entries,
2743 net->ipv6.rt6_stats->fib_rt_cache,
2744 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
2745 net->ipv6.rt6_stats->fib_discarded_routes);
2757 .open = rt6_stats_seq_open,
2764 #ifdef CONFIG_SYSCTL
2768 void __user *
buffer,
size_t *lenp, loff_t *ppos)
2775 net = (
struct net *)ctl->
extra1;
2776 delay = net->ipv6.sysctl.flush_delay;
2782 ctl_table ipv6_route_table_template[] = {
2785 .data = &
init_net.ipv6.sysctl.flush_delay,
2786 .maxlen =
sizeof(
int),
2791 .procname =
"gc_thresh",
2792 .data = &ip6_dst_ops_template.
gc_thresh,
2793 .maxlen =
sizeof(
int),
2798 .procname =
"max_size",
2799 .data = &
init_net.ipv6.sysctl.ip6_rt_max_size,
2800 .maxlen =
sizeof(
int),
2805 .procname =
"gc_min_interval",
2806 .data = &
init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
2807 .maxlen =
sizeof(
int),
2812 .procname =
"gc_timeout",
2813 .data = &
init_net.ipv6.sysctl.ip6_rt_gc_timeout,
2814 .maxlen =
sizeof(
int),
2819 .procname =
"gc_interval",
2820 .data = &
init_net.ipv6.sysctl.ip6_rt_gc_interval,
2821 .maxlen =
sizeof(
int),
2826 .procname =
"gc_elasticity",
2827 .data = &
init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
2828 .maxlen =
sizeof(
int),
2833 .procname =
"mtu_expires",
2834 .data = &
init_net.ipv6.sysctl.ip6_rt_mtu_expires,
2835 .maxlen =
sizeof(
int),
2840 .procname =
"min_adv_mss",
2841 .data = &
init_net.ipv6.sysctl.ip6_rt_min_advmss,
2842 .maxlen =
sizeof(
int),
2847 .procname =
"gc_min_interval_ms",
2848 .data = &
init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
2849 .maxlen =
sizeof(
int),
2860 table =
kmemdup(ipv6_route_table_template,
2861 sizeof(ipv6_route_table_template),
2865 table[0].
data = &net->ipv6.sysctl.flush_delay;
2867 table[1].
data = &net->ipv6.ip6_dst_ops.gc_thresh;
2868 table[2].
data = &net->ipv6.sysctl.ip6_rt_max_size;
2869 table[3].
data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2870 table[4].
data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2871 table[5].
data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2872 table[6].
data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2873 table[7].
data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2874 table[8].
data = &net->ipv6.sysctl.ip6_rt_min_advmss;
2875 table[9].
data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2882 static int __net_init ip6_route_net_init(
struct net *net)
2886 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2887 sizeof(net->ipv6.ip6_dst_ops));
2889 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2890 goto out_ip6_dst_ops;
2892 net->ipv6.ip6_null_entry =
kmemdup(&ip6_null_entry_template,
2893 sizeof(*net->ipv6.ip6_null_entry),
2895 if (!net->ipv6.ip6_null_entry)
2896 goto out_ip6_dst_entries;
2897 net->ipv6.ip6_null_entry->dst.path =
2898 (
struct dst_entry *)net->ipv6.ip6_null_entry;
2899 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
2900 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2901 ip6_template_metrics,
true);
2903 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2904 net->ipv6.ip6_prohibit_entry =
kmemdup(&ip6_prohibit_entry_template,
2905 sizeof(*net->ipv6.ip6_prohibit_entry),
2907 if (!net->ipv6.ip6_prohibit_entry)
2908 goto out_ip6_null_entry;
2909 net->ipv6.ip6_prohibit_entry->dst.path =
2910 (
struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2911 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
2912 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2913 ip6_template_metrics,
true);
2915 net->ipv6.ip6_blk_hole_entry =
kmemdup(&ip6_blk_hole_entry_template,
2916 sizeof(*net->ipv6.ip6_blk_hole_entry),
2918 if (!net->ipv6.ip6_blk_hole_entry)
2919 goto out_ip6_prohibit_entry;
2920 net->ipv6.ip6_blk_hole_entry->dst.path =
2921 (
struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2922 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
2923 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2924 ip6_template_metrics,
true);
2927 net->ipv6.sysctl.flush_delay = 0;
2928 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2929 net->ipv6.sysctl.ip6_rt_gc_min_interval =
HZ / 2;
2930 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*
HZ;
2931 net->ipv6.sysctl.ip6_rt_gc_interval = 30*
HZ;
2932 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2933 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*
HZ;
2934 net->ipv6.sysctl.ip6_rt_min_advmss =
IPV6_MIN_MTU - 20 - 40;
2936 net->ipv6.ip6_rt_gc_expire = 30*
HZ;
2942 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2943 out_ip6_prohibit_entry:
2944 kfree(net->ipv6.ip6_prohibit_entry);
2946 kfree(net->ipv6.ip6_null_entry);
2948 out_ip6_dst_entries:
2949 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2954 static void __net_exit ip6_route_net_exit(
struct net *net)
2956 kfree(net->ipv6.ip6_null_entry);
2957 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2958 kfree(net->ipv6.ip6_prohibit_entry);
2959 kfree(net->ipv6.ip6_blk_hole_entry);
2961 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2964 static int __net_init ip6_route_net_init_late(
struct net *net)
2966 #ifdef CONFIG_PROC_FS
2973 static void __net_exit ip6_route_net_exit_late(
struct net *net)
2975 #ifdef CONFIG_PROC_FS
2982 .init = ip6_route_net_init,
2983 .exit = ip6_route_net_exit,
2986 static int __net_init ipv6_inetpeer_init(
struct net *net)
2993 net->ipv6.peers = bp;
2997 static void __net_exit ipv6_inetpeer_exit(
struct net *net)
3001 net->ipv6.peers =
NULL;
3007 .init = ipv6_inetpeer_init,
3008 .exit = ipv6_inetpeer_exit,
3012 .init = ip6_route_net_init_late,
3013 .exit = ip6_route_net_exit_late,
3017 .notifier_call = ip6_route_dev_notify,
3032 ret = dst_entries_init(&ip6_dst_blackhole_ops);
3034 goto out_kmem_cache;
3038 goto out_dst_entries;
3042 goto out_register_inetpeer;
3050 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(
init_net.loopback_dev);
3051 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3053 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(
init_net.loopback_dev);
3055 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(
init_net.loopback_dev);
3059 goto out_register_subsys;
3077 goto out_register_late_subsys;
3081 goto out_register_late_subsys;
3086 out_register_late_subsys:
3094 out_register_subsys:
3096 out_register_inetpeer:
3099 dst_entries_destroy(&ip6_dst_blackhole_ops);
3114 dst_entries_destroy(&ip6_dst_blackhole_ops);