Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec...
[cascardo/linux.git] / net / tipc / bearer.c
index 2644743..463db5b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * net/tipc/bearer.c: TIPC bearer code
  *
- * Copyright (c) 1996-2006, 2013, Ericsson AB
+ * Copyright (c) 1996-2006, 2013-2014, Ericsson AB
  * Copyright (c) 2004-2006, 2010-2013, Wind River Systems
  * All rights reserved.
  *
@@ -37,6 +37,7 @@
 #include "core.h"
 #include "config.h"
 #include "bearer.h"
+#include "link.h"
 #include "discover.h"
 
 #define MAX_ADDR_STR 60
@@ -49,6 +50,23 @@ static struct tipc_media * const media_info_array[] = {
        NULL
 };
 
+static const struct nla_policy
+tipc_nl_bearer_policy[TIPC_NLA_BEARER_MAX + 1] = {
+       [TIPC_NLA_BEARER_UNSPEC]                = { .type = NLA_UNSPEC },
+       [TIPC_NLA_BEARER_NAME] = {
+               .type = NLA_STRING,
+               .len = TIPC_MAX_BEARER_NAME
+       },
+       [TIPC_NLA_BEARER_PROP]                  = { .type = NLA_NESTED },
+       [TIPC_NLA_BEARER_DOMAIN]                = { .type = NLA_U32 }
+};
+
+static const struct nla_policy tipc_nl_media_policy[TIPC_NLA_MEDIA_MAX + 1] = {
+       [TIPC_NLA_MEDIA_UNSPEC]         = { .type = NLA_UNSPEC },
+       [TIPC_NLA_MEDIA_NAME]           = { .type = NLA_STRING },
+       [TIPC_NLA_MEDIA_PROP]           = { .type = NLA_NESTED }
+};
+
 struct tipc_bearer __rcu *bearer_list[MAX_BEARERS + 1];
 
 static void bearer_disable(struct tipc_bearer *b_ptr, bool shutting_down);
@@ -627,3 +645,430 @@ void tipc_bearer_stop(void)
                }
        }
 }
+
+/* Caller should hold rtnl_lock to protect the bearer */
+static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
+                               struct tipc_bearer *bearer)
+{
+       void *hdr;
+       struct nlattr *attrs;
+       struct nlattr *prop;
+
+       hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
+                         NLM_F_MULTI, TIPC_NL_BEARER_GET);
+       if (!hdr)
+               return -EMSGSIZE;
+
+       attrs = nla_nest_start(msg->skb, TIPC_NLA_BEARER);
+       if (!attrs)
+               goto msg_full;
+
+       if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
+               goto attr_msg_full;
+
+       prop = nla_nest_start(msg->skb, TIPC_NLA_BEARER_PROP);
+       if (!prop)
+               goto prop_msg_full;
+       if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
+               goto prop_msg_full;
+       if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
+               goto prop_msg_full;
+       if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->window))
+               goto prop_msg_full;
+
+       nla_nest_end(msg->skb, prop);
+       nla_nest_end(msg->skb, attrs);
+       genlmsg_end(msg->skb, hdr);
+
+       return 0;
+
+prop_msg_full:
+       nla_nest_cancel(msg->skb, prop);
+attr_msg_full:
+       nla_nest_cancel(msg->skb, attrs);
+msg_full:
+       genlmsg_cancel(msg->skb, hdr);
+
+       return -EMSGSIZE;
+}
+
+int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
+{
+       int err;
+       int i = cb->args[0];
+       struct tipc_bearer *bearer;
+       struct tipc_nl_msg msg;
+
+       if (i == MAX_BEARERS)
+               return 0;
+
+       msg.skb = skb;
+       msg.portid = NETLINK_CB(cb->skb).portid;
+       msg.seq = cb->nlh->nlmsg_seq;
+
+       rtnl_lock();
+       for (i = 0; i < MAX_BEARERS; i++) {
+               bearer = rtnl_dereference(bearer_list[i]);
+               if (!bearer)
+                       continue;
+
+               err = __tipc_nl_add_bearer(&msg, bearer);
+               if (err)
+                       break;
+       }
+       rtnl_unlock();
+
+       cb->args[0] = i;
+       return skb->len;
+}
+
+int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
+{
+       int err;
+       char *name;
+       struct sk_buff *rep;
+       struct tipc_bearer *bearer;
+       struct tipc_nl_msg msg;
+       struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
+
+       if (!info->attrs[TIPC_NLA_BEARER])
+               return -EINVAL;
+
+       err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
+                              info->attrs[TIPC_NLA_BEARER],
+                              tipc_nl_bearer_policy);
+       if (err)
+               return err;
+
+       if (!attrs[TIPC_NLA_BEARER_NAME])
+               return -EINVAL;
+       name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
+
+       rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+       if (!rep)
+               return -ENOMEM;
+
+       msg.skb = rep;
+       msg.portid = info->snd_portid;
+       msg.seq = info->snd_seq;
+
+       rtnl_lock();
+       bearer = tipc_bearer_find(name);
+       if (!bearer) {
+               err = -EINVAL;
+               goto err_out;
+       }
+
+       err = __tipc_nl_add_bearer(&msg, bearer);
+       if (err)
+               goto err_out;
+       rtnl_unlock();
+
+       return genlmsg_reply(rep, info);
+err_out:
+       rtnl_unlock();
+       nlmsg_free(rep);
+
+       return err;
+}
+
+int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
+{
+       int err;
+       char *name;
+       struct tipc_bearer *bearer;
+       struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
+
+       if (!info->attrs[TIPC_NLA_BEARER])
+               return -EINVAL;
+
+       err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
+                              info->attrs[TIPC_NLA_BEARER],
+                              tipc_nl_bearer_policy);
+       if (err)
+               return err;
+
+       if (!attrs[TIPC_NLA_BEARER_NAME])
+               return -EINVAL;
+
+       name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
+
+       rtnl_lock();
+       bearer = tipc_bearer_find(name);
+       if (!bearer) {
+               rtnl_unlock();
+               return -EINVAL;
+       }
+
+       bearer_disable(bearer, false);
+       rtnl_unlock();
+
+       return 0;
+}
+
+int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
+{
+       int err;
+       char *bearer;
+       struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
+       u32 domain;
+       u32 prio;
+
+       prio = TIPC_MEDIA_LINK_PRI;
+       domain = tipc_own_addr & TIPC_CLUSTER_MASK;
+
+       if (!info->attrs[TIPC_NLA_BEARER])
+               return -EINVAL;
+
+       err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
+                              info->attrs[TIPC_NLA_BEARER],
+                              tipc_nl_bearer_policy);
+       if (err)
+               return err;
+
+       if (!attrs[TIPC_NLA_BEARER_NAME])
+               return -EINVAL;
+
+       bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
+
+       if (attrs[TIPC_NLA_BEARER_DOMAIN])
+               domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
+
+       if (attrs[TIPC_NLA_BEARER_PROP]) {
+               struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
+
+               err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
+                                             props);
+               if (err)
+                       return err;
+
+               if (props[TIPC_NLA_PROP_PRIO])
+                       prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
+       }
+
+       rtnl_lock();
+       err = tipc_enable_bearer(bearer, domain, prio);
+       if (err) {
+               rtnl_unlock();
+               return err;
+       }
+       rtnl_unlock();
+
+       return 0;
+}
+
+int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
+{
+       int err;
+       char *name;
+       struct tipc_bearer *b;
+       struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
+
+       if (!info->attrs[TIPC_NLA_BEARER])
+               return -EINVAL;
+
+       err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
+                              info->attrs[TIPC_NLA_BEARER],
+                              tipc_nl_bearer_policy);
+       if (err)
+               return err;
+
+       if (!attrs[TIPC_NLA_BEARER_NAME])
+               return -EINVAL;
+       name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
+
+       rtnl_lock();
+       b = tipc_bearer_find(name);
+       if (!b) {
+               rtnl_unlock();
+               return -EINVAL;
+       }
+
+       if (attrs[TIPC_NLA_BEARER_PROP]) {
+               struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
+
+               err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
+                                             props);
+               if (err) {
+                       rtnl_unlock();
+                       return err;
+               }
+
+               if (props[TIPC_NLA_PROP_TOL])
+                       b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
+               if (props[TIPC_NLA_PROP_PRIO])
+                       b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
+               if (props[TIPC_NLA_PROP_WIN])
+                       b->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
+       }
+       rtnl_unlock();
+
+       return 0;
+}
+
+static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
+                              struct tipc_media *media)
+{
+       void *hdr;
+       struct nlattr *attrs;
+       struct nlattr *prop;
+
+       hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
+                         NLM_F_MULTI, TIPC_NL_MEDIA_GET);
+       if (!hdr)
+               return -EMSGSIZE;
+
+       attrs = nla_nest_start(msg->skb, TIPC_NLA_MEDIA);
+       if (!attrs)
+               goto msg_full;
+
+       if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
+               goto attr_msg_full;
+
+       prop = nla_nest_start(msg->skb, TIPC_NLA_MEDIA_PROP);
+       if (!prop)
+               goto prop_msg_full;
+       if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
+               goto prop_msg_full;
+       if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
+               goto prop_msg_full;
+       if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->window))
+               goto prop_msg_full;
+
+       nla_nest_end(msg->skb, prop);
+       nla_nest_end(msg->skb, attrs);
+       genlmsg_end(msg->skb, hdr);
+
+       return 0;
+
+prop_msg_full:
+       nla_nest_cancel(msg->skb, prop);
+attr_msg_full:
+       nla_nest_cancel(msg->skb, attrs);
+msg_full:
+       genlmsg_cancel(msg->skb, hdr);
+
+       return -EMSGSIZE;
+}
+
+int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
+{
+       int err;
+       int i = cb->args[0];
+       struct tipc_nl_msg msg;
+
+       if (i == MAX_MEDIA)
+               return 0;
+
+       msg.skb = skb;
+       msg.portid = NETLINK_CB(cb->skb).portid;
+       msg.seq = cb->nlh->nlmsg_seq;
+
+       rtnl_lock();
+       for (; media_info_array[i] != NULL; i++) {
+               err = __tipc_nl_add_media(&msg, media_info_array[i]);
+               if (err)
+                       break;
+       }
+       rtnl_unlock();
+
+       cb->args[0] = i;
+       return skb->len;
+}
+
+int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
+{
+       int err;
+       char *name;
+       struct tipc_nl_msg msg;
+       struct tipc_media *media;
+       struct sk_buff *rep;
+       struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
+
+       if (!info->attrs[TIPC_NLA_MEDIA])
+               return -EINVAL;
+
+       err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
+                              info->attrs[TIPC_NLA_MEDIA],
+                              tipc_nl_media_policy);
+       if (err)
+               return err;
+
+       if (!attrs[TIPC_NLA_MEDIA_NAME])
+               return -EINVAL;
+       name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
+
+       rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+       if (!rep)
+               return -ENOMEM;
+
+       msg.skb = rep;
+       msg.portid = info->snd_portid;
+       msg.seq = info->snd_seq;
+
+       rtnl_lock();
+       media = tipc_media_find(name);
+       if (!media) {
+               err = -EINVAL;
+               goto err_out;
+       }
+
+       err = __tipc_nl_add_media(&msg, media);
+       if (err)
+               goto err_out;
+       rtnl_unlock();
+
+       return genlmsg_reply(rep, info);
+err_out:
+       rtnl_unlock();
+       nlmsg_free(rep);
+
+       return err;
+}
+
+int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
+{
+       int err;
+       char *name;
+       struct tipc_media *m;
+       struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
+
+       if (!info->attrs[TIPC_NLA_MEDIA])
+               return -EINVAL;
+
+       err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
+                              info->attrs[TIPC_NLA_MEDIA],
+                              tipc_nl_media_policy);
+
+       if (!attrs[TIPC_NLA_MEDIA_NAME])
+               return -EINVAL;
+       name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
+
+       rtnl_lock();
+       m = tipc_media_find(name);
+       if (!m) {
+               rtnl_unlock();
+               return -EINVAL;
+       }
+
+       if (attrs[TIPC_NLA_MEDIA_PROP]) {
+               struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
+
+               err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
+                                             props);
+               if (err) {
+                       rtnl_unlock();
+                       return err;
+               }
+
+               if (props[TIPC_NLA_PROP_TOL])
+                       m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
+               if (props[TIPC_NLA_PROP_PRIO])
+                       m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
+               if (props[TIPC_NLA_PROP_WIN])
+                       m->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
+       }
+       rtnl_unlock();
+
+       return 0;
+}