OSDN Git Service

ipv6 addrconf: Fix route lifetime setting in corner case.
[uclinux-h8/linux.git] / net / ipv6 / route.c
index a4b5aee..98aa50c 100644 (file)
@@ -36,6 +36,7 @@
 #include <linux/route.h>
 #include <linux/netdevice.h>
 #include <linux/in6.h>
+#include <linux/mroute6.h>
 #include <linux/init.h>
 #include <linux/if_arp.h>
 #include <linux/proc_fs.h>
@@ -149,7 +150,7 @@ static struct rt6_info ip6_null_entry_template = {
 static int ip6_pkt_prohibit(struct sk_buff *skb);
 static int ip6_pkt_prohibit_out(struct sk_buff *skb);
 
-struct rt6_info ip6_prohibit_entry_template = {
+static struct rt6_info ip6_prohibit_entry_template = {
        .u = {
                .dst = {
                        .__refcnt       = ATOMIC_INIT(1),
@@ -208,7 +209,7 @@ static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
        struct rt6_info *rt = (struct rt6_info *)dst;
        struct inet6_dev *idev = rt->rt6i_idev;
        struct net_device *loopback_dev =
-               dev->nd_net->loopback_dev;
+               dev_net(dev)->loopback_dev;
 
        if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
                struct inet6_dev *loopback_idev =
@@ -433,7 +434,7 @@ static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
        RT6_TRACE("%s() => %p\n",
                  __func__, match);
 
-       net = rt0->rt6i_dev->nd_net;
+       net = dev_net(rt0->rt6i_dev);
        return (match ? match : net->ipv6.ip6_null_entry);
 }
 
@@ -441,7 +442,7 @@ static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
                  struct in6_addr *gwaddr)
 {
-       struct net *net = dev->nd_net;
+       struct net *net = dev_net(dev);
        struct route_info *rinfo = (struct route_info *) opt;
        struct in6_addr prefix_buf, *prefix;
        unsigned int pref;
@@ -474,7 +475,7 @@ int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
        lifetime = ntohl(rinfo->lifetime);
        if (lifetime == 0xffffffff) {
                /* infinity */
-       } else if (lifetime > 0x7fffffff/HZ) {
+       } else if (lifetime > 0x7fffffff/HZ - 1) {
                /* Avoid arithmetic overflow */
                lifetime = 0x7fffffff/HZ - 1;
        }
@@ -555,8 +556,8 @@ out:
 
 }
 
-struct rt6_info *rt6_lookup(struct net *net, struct in6_addr *daddr,
-                           struct in6_addr *saddr, int oif, int strict)
+struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
+                           const struct in6_addr *saddr, int oif, int strict)
 {
        struct flowi fl = {
                .oif = oif,
@@ -607,7 +608,7 @@ static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
 int ip6_ins_rt(struct rt6_info *rt)
 {
        struct nl_info info = {
-               .nl_net = rt->rt6i_dev->nd_net,
+               .nl_net = dev_net(rt->rt6i_dev),
        };
        return __ip6_ins_rt(rt, &info);
 }
@@ -745,7 +746,7 @@ static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *
 void ip6_route_input(struct sk_buff *skb)
 {
        struct ipv6hdr *iph = ipv6_hdr(skb);
-       struct net *net = skb->dev->nd_net;
+       struct net *net = dev_net(skb->dev);
        int flags = RT6_LOOKUP_F_HAS_SADDR;
        struct flowi fl = {
                .iif = skb->dev->ifindex,
@@ -782,6 +783,15 @@ struct dst_entry * ip6_route_output(struct net *net, struct sock *sk,
 
        if (!ipv6_addr_any(&fl->fl6_src))
                flags |= RT6_LOOKUP_F_HAS_SADDR;
+       else if (sk) {
+               unsigned int prefs = inet6_sk(sk)->srcprefs;
+               if (prefs & IPV6_PREFER_SRC_TMP)
+                       flags |= RT6_LOOKUP_F_SRCPREF_TMP;
+               if (prefs & IPV6_PREFER_SRC_PUBLIC)
+                       flags |= RT6_LOOKUP_F_SRCPREF_PUBLIC;
+               if (prefs & IPV6_PREFER_SRC_COA)
+                       flags |= RT6_LOOKUP_F_SRCPREF_COA;
+       }
 
        return fib6_rule_lookup(net, fl, flags, ip6_pol_route_output);
 }
@@ -915,11 +925,11 @@ static DEFINE_SPINLOCK(icmp6_dst_lock);
 
 struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
                                  struct neighbour *neigh,
-                                 struct in6_addr *addr)
+                                 const struct in6_addr *addr)
 {
        struct rt6_info *rt;
        struct inet6_dev *idev = in6_dev_get(dev);
-       struct net *net = dev->nd_net;
+       struct net *net = dev_net(dev);
 
        if (unlikely(idev == NULL))
                return NULL;
@@ -1034,15 +1044,17 @@ static int ipv6_get_mtu(struct net_device *dev)
        return mtu;
 }
 
-int ipv6_get_hoplimit(struct net_device *dev)
+int ip6_dst_hoplimit(struct dst_entry *dst)
 {
-       int hoplimit = ipv6_devconf.hop_limit;
-       struct inet6_dev *idev;
-
-       idev = in6_dev_get(dev);
-       if (idev) {
-               hoplimit = idev->cnf.hop_limit;
-               in6_dev_put(idev);
+       int hoplimit = dst_metric(dst, RTAX_HOPLIMIT);
+       if (hoplimit < 0) {
+               struct net_device *dev = dst->dev;
+               struct inet6_dev *idev = in6_dev_get(dev);
+               if (idev) {
+                       hoplimit = idev->cnf.hop_limit;
+                       in6_dev_put(idev);
+               } else
+                       hoplimit = ipv6_devconf.hop_limit;
        }
        return hoplimit;
 }
@@ -1231,17 +1243,17 @@ install_route:
                }
        }
 
-       if (rt->u.dst.metrics[RTAX_HOPLIMIT-1] == 0)
+       if (dst_metric(&rt->u.dst, RTAX_HOPLIMIT) == 0)
                rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1;
-       if (!rt->u.dst.metrics[RTAX_MTU-1])
+       if (!dst_metric(&rt->u.dst, RTAX_MTU))
                rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(dev);
-       if (!rt->u.dst.metrics[RTAX_ADVMSS-1])
+       if (!dst_metric(&rt->u.dst, RTAX_ADVMSS))
                rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(net, dst_mtu(&rt->u.dst));
        rt->u.dst.dev = dev;
        rt->rt6i_idev = idev;
        rt->rt6i_table = table;
 
-       cfg->fc_nlinfo.nl_net = dev->nd_net;
+       cfg->fc_nlinfo.nl_net = dev_net(dev);
 
        return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
 
@@ -1259,7 +1271,7 @@ static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
 {
        int err;
        struct fib6_table *table;
-       struct net *net = rt->rt6i_dev->nd_net;
+       struct net *net = dev_net(rt->rt6i_dev);
 
        if (rt == net->ipv6.ip6_null_entry)
                return -ENOENT;
@@ -1278,7 +1290,7 @@ static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
 int ip6_del_rt(struct rt6_info *rt)
 {
        struct nl_info info = {
-               .nl_net = rt->rt6i_dev->nd_net,
+               .nl_net = dev_net(rt->rt6i_dev),
        };
        return __ip6_del_rt(rt, &info);
 }
@@ -1390,7 +1402,7 @@ static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
                                           struct net_device *dev)
 {
        int flags = RT6_LOOKUP_F_HAS_SADDR;
-       struct net *net = dev->nd_net;
+       struct net *net = dev_net(dev);
        struct ip6rd_flowi rdfl = {
                .fl = {
                        .oif = dev->ifindex,
@@ -1417,7 +1429,7 @@ void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
 {
        struct rt6_info *rt, *nrt = NULL;
        struct netevent_redirect netevent;
-       struct net *net = neigh->dev->nd_net;
+       struct net *net = dev_net(neigh->dev);
 
        rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
 
@@ -1466,7 +1478,7 @@ void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
        nrt->rt6i_nexthop = neigh_clone(neigh);
        /* Reset pmtu, it may be better */
        nrt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(neigh->dev);
-       nrt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(neigh->dev->nd_net,
+       nrt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dev_net(neigh->dev),
                                                        dst_mtu(&nrt->u.dst));
 
        if (ip6_ins_rt(nrt))
@@ -1495,7 +1507,7 @@ void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
                        struct net_device *dev, u32 pmtu)
 {
        struct rt6_info *rt, *nrt;
-       struct net *net = dev->nd_net;
+       struct net *net = dev_net(dev);
        int allfrag = 0;
 
        rt = rt6_lookup(net, daddr, saddr, dev->ifindex, 0);
@@ -1572,7 +1584,7 @@ out:
 
 static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
 {
-       struct net *net = ort->rt6i_dev->nd_net;
+       struct net *net = dev_net(ort->rt6i_dev);
        struct rt6_info *rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
 
        if (rt) {
@@ -1671,7 +1683,7 @@ struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *d
        struct rt6_info *rt;
        struct fib6_table *table;
 
-       table = fib6_get_table(dev->nd_net, RT6_TABLE_DFLT);
+       table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
        if (table == NULL)
                return NULL;
 
@@ -1688,8 +1700,6 @@ struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *d
        return rt;
 }
 
-EXPORT_SYMBOL(rt6_get_dflt_router);
-
 struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
                                     struct net_device *dev,
                                     unsigned int pref)
@@ -1702,7 +1712,7 @@ struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
                                  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
                .fc_nlinfo.pid = 0,
                .fc_nlinfo.nlh = NULL,
-               .fc_nlinfo.nl_net = dev->nd_net,
+               .fc_nlinfo.nl_net = dev_net(dev),
        };
 
        ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
@@ -1851,7 +1861,7 @@ struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
                                    const struct in6_addr *addr,
                                    int anycast)
 {
-       struct net *net = idev->dev->nd_net;
+       struct net *net = dev_net(idev->dev);
        struct rt6_info *rt = ip6_dst_alloc(net->ipv6.ip6_dst_ops);
 
        if (rt == NULL)
@@ -1928,7 +1938,7 @@ static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
 {
        struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
        struct inet6_dev *idev;
-       struct net *net = arg->dev->nd_net;
+       struct net *net = dev_net(arg->dev);
 
        /* In IPv6 pmtu discovery is not optional,
           so that RTAX_MTU lock cannot disable it.
@@ -1972,7 +1982,7 @@ void rt6_mtu_change(struct net_device *dev, unsigned mtu)
                .mtu = mtu,
        };
 
-       fib6_clean_all(dev->nd_net, rt6_mtu_change_route, 0, &arg);
+       fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
 }
 
 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
@@ -2009,7 +2019,7 @@ static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
 
        cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
        cfg->fc_nlinfo.nlh = nlh;
-       cfg->fc_nlinfo.nl_net = skb->sk->sk_net;
+       cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
 
        if (tb[RTA_GATEWAY]) {
                nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
@@ -2095,7 +2105,7 @@ static inline size_t rt6_nlmsg_size(void)
 static int rt6_fill_node(struct sk_buff *skb, struct rt6_info *rt,
                         struct in6_addr *dst, struct in6_addr *src,
                         int iif, int type, u32 pid, u32 seq,
-                        int prefix, unsigned int flags)
+                        int prefix, int nowait, unsigned int flags)
 {
        struct rtmsg *rtm;
        struct nlmsghdr *nlh;
@@ -2155,12 +2165,27 @@ static int rt6_fill_node(struct sk_buff *skb, struct rt6_info *rt,
        } else if (rtm->rtm_src_len)
                NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
 #endif
-       if (iif)
-               NLA_PUT_U32(skb, RTA_IIF, iif);
-       else if (dst) {
+       if (iif) {
+#ifdef CONFIG_IPV6_MROUTE
+               if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
+                       int err = ip6mr_get_route(skb, rtm, nowait);
+                       if (err <= 0) {
+                               if (!nowait) {
+                                       if (err == 0)
+                                               return 0;
+                                       goto nla_put_failure;
+                               } else {
+                                       if (err == -EMSGSIZE)
+                                               goto nla_put_failure;
+                               }
+                       }
+               } else
+#endif
+                       NLA_PUT_U32(skb, RTA_IIF, iif);
+       } else if (dst) {
                struct in6_addr saddr_buf;
                if (ipv6_dev_get_saddr(ip6_dst_idev(&rt->u.dst)->dev,
-                                      dst, &saddr_buf) == 0)
+                                      dst, 0, &saddr_buf) == 0)
                        NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
        }
 
@@ -2200,12 +2225,12 @@ int rt6_dump_route(struct rt6_info *rt, void *p_arg)
 
        return rt6_fill_node(arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
                     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
-                    prefix, NLM_F_MULTI);
+                    prefix, 0, NLM_F_MULTI);
 }
 
 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
 {
-       struct net *net = in_skb->sk->sk_net;
+       struct net *net = sock_net(in_skb->sk);
        struct nlattr *tb[RTA_MAX+1];
        struct rt6_info *rt;
        struct sk_buff *skb;
@@ -2266,7 +2291,7 @@ static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void
 
        err = rt6_fill_node(skb, rt, &fl.fl6_dst, &fl.fl6_src, iif,
                            RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
-                           nlh->nlmsg_seq, 0, 0);
+                           nlh->nlmsg_seq, 0, 0, 0);
        if (err < 0) {
                kfree_skb(skb);
                goto errout;
@@ -2292,7 +2317,7 @@ void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
                goto errout;
 
        err = rt6_fill_node(skb, rt, NULL, NULL, 0,
-                               event, info->pid, seq, 0, 0);
+                               event, info->pid, seq, 0, 0, 0);
        if (err < 0) {
                /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
                WARN_ON(err == -EMSGSIZE);
@@ -2310,7 +2335,7 @@ static int ip6_route_dev_notify(struct notifier_block *this,
                                unsigned long event, void *data)
 {
        struct net_device *dev = (struct net_device *)data;
-       struct net *net = dev->nd_net;
+       struct net *net = dev_net(dev);
 
        if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
                net->ipv6.ip6_null_entry->u.dst.dev = dev;
@@ -2379,10 +2404,18 @@ static int ipv6_route_show(struct seq_file *m, void *v)
 
 static int ipv6_route_open(struct inode *inode, struct file *file)
 {
+       int err;
        struct net *net = get_proc_net(inode);
        if (!net)
                return -ENXIO;
-       return single_open(file, ipv6_route_show, net);
+
+       err = single_open(file, ipv6_route_show, net);
+       if (err < 0) {
+               put_net(net);
+               return err;
+       }
+
+       return 0;
 }
 
 static int ipv6_route_release(struct inode *inode, struct file *file)
@@ -2418,8 +2451,18 @@ static int rt6_stats_seq_show(struct seq_file *seq, void *v)
 
 static int rt6_stats_seq_open(struct inode *inode, struct file *file)
 {
+       int err;
        struct net *net = get_proc_net(inode);
-       return single_open(file, rt6_stats_seq_show, net);
+       if (!net)
+               return -ENXIO;
+
+       err = single_open(file, rt6_stats_seq_show, net);
+       if (err < 0) {
+               put_net(net);
+               return err;
+       }
+
+       return 0;
 }
 
 static int rt6_stats_seq_release(struct inode *inode, struct file *file)
@@ -2571,15 +2614,14 @@ struct ctl_table *ipv6_route_sysctl_init(struct net *net)
 
 static int ip6_route_net_init(struct net *net)
 {
-       int ret = 0;
+       int ret = -ENOMEM;
 
-       ret = -ENOMEM;
        net->ipv6.ip6_dst_ops = kmemdup(&ip6_dst_ops_template,
                                        sizeof(*net->ipv6.ip6_dst_ops),
                                        GFP_KERNEL);
        if (!net->ipv6.ip6_dst_ops)
                goto out;
-       net->ipv6.ip6_dst_ops->dst_net = net;
+       net->ipv6.ip6_dst_ops->dst_net = hold_net(net);
 
        net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
                                           sizeof(*net->ipv6.ip6_null_entry),
@@ -2626,6 +2668,7 @@ out:
        return ret;
 
 out_ip6_dst_ops:
+       release_net(net->ipv6.ip6_dst_ops->dst_net);
        kfree(net->ipv6.ip6_dst_ops);
        goto out;
 }
@@ -2641,6 +2684,7 @@ static void ip6_route_net_exit(struct net *net)
        kfree(net->ipv6.ip6_prohibit_entry);
        kfree(net->ipv6.ip6_blk_hole_entry);
 #endif
+       release_net(net->ipv6.ip6_dst_ops->dst_net);
        kfree(net->ipv6.ip6_dst_ops);
 }