]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - net/ipv4/devinet.c
ipv4: AF_INET link address family
[net-next-2.6.git] / net / ipv4 / devinet.c
index c2ff48fa18c723ff110b4ada4e49461225ee8098..71afc26c2df87996ce605d721f84cf3da47aaed2 100644 (file)
@@ -403,6 +403,9 @@ static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa)
        return inet_insert_ifa(ifa);
 }
 
+/* Caller must hold RCU or RTNL :
+ * We dont take a reference on found in_device
+ */
 struct in_device *inetdev_by_index(struct net *net, int ifindex)
 {
        struct net_device *dev;
@@ -411,7 +414,7 @@ struct in_device *inetdev_by_index(struct net *net, int ifindex)
        rcu_read_lock();
        dev = dev_get_by_index_rcu(net, ifindex);
        if (dev)
-               in_dev = in_dev_get(dev);
+               in_dev = rcu_dereference_rtnl(dev->ip_ptr);
        rcu_read_unlock();
        return in_dev;
 }
@@ -453,8 +456,6 @@ static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg
                goto errout;
        }
 
-       __in_dev_put(in_dev);
-
        for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
             ifap = &ifa->ifa_next) {
                if (tb[IFA_LOCAL] &&
@@ -1255,6 +1256,72 @@ errout:
                rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err);
 }
 
+static size_t inet_get_link_af_size(const struct net_device *dev)
+{
+       struct in_device *in_dev = __in_dev_get_rcu(dev);
+
+       if (!in_dev)
+               return 0;
+
+       return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */
+}
+
+static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
+{
+       struct in_device *in_dev = __in_dev_get_rcu(dev);
+       struct nlattr *nla;
+       int i;
+
+       if (!in_dev)
+               return -ENODATA;
+
+       nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
+       if (nla == NULL)
+               return -EMSGSIZE;
+
+       for (i = 0; i < IPV4_DEVCONF_MAX; i++)
+               ((u32 *) nla_data(nla))[i] = in_dev->cnf.data[i];
+
+       return 0;
+}
+
+static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
+       [IFLA_INET_CONF]        = { .type = NLA_NESTED },
+};
+
+static int inet_parse_link_af(struct net_device *dev, const struct nlattr *nla)
+{
+       struct in_device *in_dev = __in_dev_get_rcu(dev);
+       struct nlattr *a, *tb[IFLA_INET_MAX+1];
+       int err, rem;
+
+       if (!in_dev)
+               return -EOPNOTSUPP;
+
+       err = nla_parse_nested(tb, IFLA_INET_MAX, nla, inet_af_policy);
+       if (err < 0)
+               return err;
+
+       if (tb[IFLA_INET_CONF]) {
+               nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) {
+                       int cfgid = nla_type(a);
+
+                       if (nla_len(a) < 4)
+                               return -EINVAL;
+
+                       if (cfgid <= 0 || cfgid > IPV4_DEVCONF_MAX)
+                               return -EINVAL;
+               }
+       }
+
+       if (tb[IFLA_INET_CONF]) {
+               nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
+                       ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
+       }
+
+       return 0;
+}
+
 #ifdef CONFIG_SYSCTL
 
 static void devinet_copy_dflt_conf(struct net *net, int i)
@@ -1618,6 +1685,13 @@ static __net_initdata struct pernet_operations devinet_ops = {
        .exit = devinet_exit_net,
 };
 
+static struct rtnl_af_ops inet_af_ops = {
+       .family           = AF_INET,
+       .fill_link_af     = inet_fill_link_af,
+       .get_link_af_size = inet_get_link_af_size,
+       .parse_link_af    = inet_parse_link_af,
+};
+
 void __init devinet_init(void)
 {
        register_pernet_subsys(&devinet_ops);
@@ -1625,6 +1699,8 @@ void __init devinet_init(void)
        register_gifconf(PF_INET, inet_gifconf);
        register_netdevice_notifier(&ip_netdev_notifier);
 
+       rtnl_af_register(&inet_af_ops);
+
        rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL);
        rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL);
        rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr);