]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - net/core/rtnetlink.c
[RTNETLINK]: Add rtnl_put_cacheinfo() to unify some code
[net-next-2.6.git] / net / core / rtnetlink.c
index 93ba04fb84442c4d06717c538efd28d84eadf6eb..e76539a5eb5e09e79939fef2e4d96bd9449c1a7b 100644 (file)
@@ -58,6 +58,7 @@
 #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
 
 static DEFINE_MUTEX(rtnl_mutex);
+static struct sock *rtnl;
 
 void rtnl_lock(void)
 {
@@ -95,8 +96,6 @@ int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
        return 0;
 }
 
-struct sock *rtnl;
-
 struct rtnetlink_link * rtnetlink_links[NPROTO];
 
 static const int rtm_min[RTM_NR_FAMILIES] =
@@ -104,16 +103,13 @@ static const int rtm_min[RTM_NR_FAMILIES] =
        [RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
        [RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
        [RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
-       [RTM_FAM(RTM_NEWNEIGH)]     = NLMSG_LENGTH(sizeof(struct ndmsg)),
        [RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
        [RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
        [RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
        [RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
        [RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
-       [RTM_FAM(RTM_NEWPREFIX)]    = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
        [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
        [RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
-       [RTM_FAM(RTM_NEWNEIGHTBL)]  = NLMSG_LENGTH(sizeof(struct ndtmsg)),
 };
 
 static const int rta_max[RTM_NR_FAMILIES] =
@@ -121,13 +117,11 @@ static const int rta_max[RTM_NR_FAMILIES] =
        [RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
        [RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
        [RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
-       [RTM_FAM(RTM_NEWNEIGH)]     = NDA_MAX,
        [RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
        [RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
        [RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
        [RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
        [RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
-       [RTM_FAM(RTM_NEWNEIGHTBL)]  = NDTA_MAX,
 };
 
 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
@@ -170,26 +164,74 @@ int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
        return err;
 }
 
+int rtnl_unicast(struct sk_buff *skb, u32 pid)
+{
+       return nlmsg_unicast(rtnl, skb, pid);
+}
+
+int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
+               struct nlmsghdr *nlh, gfp_t flags)
+{
+       int report = 0;
+
+       if (nlh)
+               report = nlmsg_report(nlh);
+
+       return nlmsg_notify(rtnl, skb, pid, group, report, flags);
+}
+
+void rtnl_set_sk_err(u32 group, int error)
+{
+       netlink_set_err(rtnl, 0, group, error);
+}
+
 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
 {
-       struct rtattr *mx = (struct rtattr*)skb->tail;
-       int i;
+       struct nlattr *mx;
+       int i, valid = 0;
+
+       mx = nla_nest_start(skb, RTA_METRICS);
+       if (mx == NULL)
+               return -ENOBUFS;
 
-       RTA_PUT(skb, RTA_METRICS, 0, NULL);
-       for (i=0; i<RTAX_MAX; i++) {
-               if (metrics[i])
-                       RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
+       for (i = 0; i < RTAX_MAX; i++) {
+               if (metrics[i]) {
+                       valid++;
+                       NLA_PUT_U32(skb, i+1, metrics[i]);
+               }
        }
-       mx->rta_len = skb->tail - (u8*)mx;
-       if (mx->rta_len == RTA_LENGTH(0))
-               skb_trim(skb, (u8*)mx - skb->data);
-       return 0;
 
-rtattr_failure:
-       skb_trim(skb, (u8*)mx - skb->data);
-       return -1;
+       if (!valid) {
+               nla_nest_cancel(skb, mx);
+               return 0;
+       }
+
+       return nla_nest_end(skb, mx);
+
+nla_put_failure:
+       return nla_nest_cancel(skb, mx);
 }
 
+int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
+                      u32 ts, u32 tsage, long expires, u32 error)
+{
+       struct rta_cacheinfo ci = {
+               .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
+               .rta_used = dst->__use,
+               .rta_clntref = atomic_read(&(dst->__refcnt)),
+               .rta_error = error,
+               .rta_id =  id,
+               .rta_ts = ts,
+               .rta_tsage = tsage,
+       };
+
+       if (expires)
+               ci.rta_expires = jiffies_to_clock_t(expires);
+
+       return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
+}
+
+EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
 
 static void set_operstate(struct net_device *dev, unsigned char transition)
 {
@@ -250,6 +292,25 @@ static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
        a->tx_compressed = b->tx_compressed;
 };
 
+static inline size_t if_nlmsg_size(int iwbuflen)
+{
+       return NLMSG_ALIGN(sizeof(struct ifinfomsg))
+              + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
+              + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
+              + nla_total_size(sizeof(struct rtnl_link_ifmap))
+              + nla_total_size(sizeof(struct rtnl_link_stats))
+              + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
+              + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
+              + nla_total_size(4) /* IFLA_TXQLEN */
+              + nla_total_size(4) /* IFLA_WEIGHT */
+              + nla_total_size(4) /* IFLA_MTU */
+              + nla_total_size(4) /* IFLA_LINK */
+              + nla_total_size(4) /* IFLA_MASTER */
+              + nla_total_size(1) /* IFLA_OPERSTATE */
+              + nla_total_size(1) /* IFLA_LINKMODE */
+              + nla_total_size(iwbuflen);
+}
+
 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
                            void *iwbuf, int iwbuflen, int type, u32 pid,
                            u32 seq, u32 change, unsigned int flags)
@@ -348,8 +409,8 @@ static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
 }
 
 static struct nla_policy ifla_policy[IFLA_MAX+1] __read_mostly = {
-       [IFLA_IFNAME]           = { .type = NLA_STRING },
-       [IFLA_MAP]              = { .minlen = sizeof(struct rtnl_link_ifmap) },
+       [IFLA_IFNAME]           = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
+       [IFLA_MAP]              = { .len = sizeof(struct rtnl_link_ifmap) },
        [IFLA_MTU]              = { .type = NLA_U32 },
        [IFLA_TXQLEN]           = { .type = NLA_U32 },
        [IFLA_WEIGHT]           = { .type = NLA_U32 },
@@ -369,9 +430,10 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
        if (err < 0)
                goto errout;
 
-       if (tb[IFLA_IFNAME] &&
-           nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ) >= IFNAMSIZ)
-               return -EINVAL;
+       if (tb[IFLA_IFNAME])
+               nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
+       else
+               ifname[0] = '\0';
 
        err = -EINVAL;
        ifm = nlmsg_data(nlh);
@@ -534,11 +596,11 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
        struct sk_buff *nskb;
        char *iw_buf = NULL, *iw = NULL;
        int iw_buf_len = 0;
-       int err, payload;
+       int err;
 
        err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
        if (err < 0)
-               goto errout;
+               return err;
 
        ifm = nlmsg_data(nlh);
        if (ifm->ifi_index >= 0) {
@@ -563,9 +625,7 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
        }
 #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
 
-       payload = NLMSG_ALIGN(sizeof(struct ifinfomsg) +
-                             nla_total_size(iw_buf_len));
-       nskb = nlmsg_new(nlmsg_total_size(payload), GFP_KERNEL);
+       nskb = nlmsg_new(if_nlmsg_size(iw_buf_len), GFP_KERNEL);
        if (nskb == NULL) {
                err = -ENOBUFS;
                goto errout;
@@ -573,14 +633,10 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
 
        err = rtnl_fill_ifinfo(nskb, dev, iw, iw_buf_len, RTM_NEWLINK,
                               NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0, 0);
-       if (err <= 0) {
-               kfree_skb(skb);
-               goto errout;
-       }
+       /* failure impilies BUG in if_nlmsg_size or wireless_rtnetlink_get */
+       BUG_ON(err < 0);
 
-       err = netlink_unicast(rtnl, skb, NETLINK_CB(skb).pid, MSG_DONTWAIT);
-       if (err > 0)
-               err = 0;
+       err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
 errout:
        kfree(iw_buf);
        dev_put(dev);
@@ -615,20 +671,20 @@ static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
 {
        struct sk_buff *skb;
-       int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
-                              sizeof(struct rtnl_link_ifmap) +
-                              sizeof(struct rtnl_link_stats) + 128);
+       int err = -ENOBUFS;
 
-       skb = nlmsg_new(size, GFP_KERNEL);
-       if (!skb)
-               return;
+       skb = nlmsg_new(if_nlmsg_size(0), GFP_KERNEL);
+       if (skb == NULL)
+               goto errout;
 
-       if (rtnl_fill_ifinfo(skb, dev, NULL, 0, type, 0, 0, change, 0) < 0) {
-               kfree_skb(skb);
-               return;
-       }
-       NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
-       netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_KERNEL);
+       err = rtnl_fill_ifinfo(skb, dev, NULL, 0, type, 0, 0, change, 0);
+       /* failure implies BUG in if_nlmsg_size() */
+       BUG_ON(err < 0);
+
+       err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
+errout:
+       if (err < 0)
+               rtnl_set_sk_err(RTNLGRP_LINK, err);
 }
 
 /* Protected by RTNL sempahore.  */
@@ -825,7 +881,9 @@ EXPORT_SYMBOL(rtattr_strlcpy);
 EXPORT_SYMBOL(rtattr_parse);
 EXPORT_SYMBOL(rtnetlink_links);
 EXPORT_SYMBOL(rtnetlink_put_metrics);
-EXPORT_SYMBOL(rtnl);
 EXPORT_SYMBOL(rtnl_lock);
 EXPORT_SYMBOL(rtnl_trylock);
 EXPORT_SYMBOL(rtnl_unlock);
+EXPORT_SYMBOL(rtnl_unicast);
+EXPORT_SYMBOL(rtnl_notify);
+EXPORT_SYMBOL(rtnl_set_sk_err);