]> err.no Git - linux-2.6/blobdiff - net/netlink/attr.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / net / netlink / attr.c
index 136e529e5780cf4bec9a8523f07ce758028a83c2..2d106cfe1d276bf5e234195753eb091cba7b7f1e 100644 (file)
@@ -20,33 +20,78 @@ static u16 nla_attr_minlen[NLA_TYPE_MAX+1] __read_mostly = {
        [NLA_U16]       = sizeof(u16),
        [NLA_U32]       = sizeof(u32),
        [NLA_U64]       = sizeof(u64),
-       [NLA_STRING]    = 1,
        [NLA_NESTED]    = NLA_HDRLEN,
 };
 
 static int validate_nla(struct nlattr *nla, int maxtype,
-                       struct nla_policy *policy)
+                       const struct nla_policy *policy)
 {
-       struct nla_policy *pt;
-       int minlen = 0;
+       const struct nla_policy *pt;
+       int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
 
-       if (nla->nla_type <= 0 || nla->nla_type > maxtype)
+       if (type <= 0 || type > maxtype)
                return 0;
 
-       pt = &policy[nla->nla_type];
+       pt = &policy[type];
 
        BUG_ON(pt->type > NLA_TYPE_MAX);
 
-       if (pt->minlen)
-               minlen = pt->minlen;
-       else if (pt->type != NLA_UNSPEC)
-               minlen = nla_attr_minlen[pt->type];
+       switch (pt->type) {
+       case NLA_FLAG:
+               if (attrlen > 0)
+                       return -ERANGE;
+               break;
 
-       if (pt->type == NLA_FLAG && nla_len(nla) > 0)
-               return -ERANGE;
+       case NLA_NUL_STRING:
+               if (pt->len)
+                       minlen = min_t(int, attrlen, pt->len + 1);
+               else
+                       minlen = attrlen;
 
-       if (nla_len(nla) < minlen)
-               return -ERANGE;
+               if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL)
+                       return -EINVAL;
+               /* fall through */
+
+       case NLA_STRING:
+               if (attrlen < 1)
+                       return -ERANGE;
+
+               if (pt->len) {
+                       char *buf = nla_data(nla);
+
+                       if (buf[attrlen - 1] == '\0')
+                               attrlen--;
+
+                       if (attrlen > pt->len)
+                               return -ERANGE;
+               }
+               break;
+
+       case NLA_BINARY:
+               if (pt->len && attrlen > pt->len)
+                       return -ERANGE;
+               break;
+
+       case NLA_NESTED_COMPAT:
+               if (attrlen < pt->len)
+                       return -ERANGE;
+               if (attrlen < NLA_ALIGN(pt->len))
+                       break;
+               if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN)
+                       return -ERANGE;
+               nla = nla_data(nla) + NLA_ALIGN(pt->len);
+               if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla))
+                       return -ERANGE;
+               break;
+       default:
+               if (pt->len)
+                       minlen = pt->len;
+               else if (pt->type != NLA_UNSPEC)
+                       minlen = nla_attr_minlen[pt->type];
+
+               if (attrlen < minlen)
+                       return -ERANGE;
+       }
 
        return 0;
 }
@@ -65,7 +110,7 @@ static int validate_nla(struct nlattr *nla, int maxtype,
  * Returns 0 on success or a negative error code.
  */
 int nla_validate(struct nlattr *head, int len, int maxtype,
-                struct nla_policy *policy)
+                const struct nla_policy *policy)
 {
        struct nlattr *nla;
        int rem, err;
@@ -87,6 +132,7 @@ errout:
  * @maxtype: maximum attribute type to be expected
  * @head: head of attribute stream
  * @len: length of attribute stream
+ * @policy: validation policy
  *
  * Parses a stream of attributes and stores a pointer to each attribute in
  * the tb array accessable via the attribute type. Attributes with a type
@@ -96,7 +142,7 @@ errout:
  * Returns 0 on success or a negative error code.
  */
 int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
-             struct nla_policy *policy)
+             const struct nla_policy *policy)
 {
        struct nlattr *nla;
        int rem, err;
@@ -104,7 +150,7 @@ int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
        memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
 
        nla_for_each_attr(nla, head, len, rem) {
-               u16 type = nla->nla_type;
+               u16 type = nla_type(nla);
 
                if (type > 0 && type <= maxtype) {
                        if (policy) {
@@ -140,7 +186,7 @@ struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
        int rem;
 
        nla_for_each_attr(nla, head, len, rem)
-               if (nla->nla_type == attrtype)
+               if (nla_type(nla) == attrtype)
                        return nla;
 
        return NULL;
@@ -149,7 +195,7 @@ struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
 /**
  * nla_strlcpy - Copy string attribute payload into a sized buffer
  * @dst: where to copy the string to
- * @src: attribute to copy the string from
+ * @nla: attribute to copy the string from
  * @dstsize: size of destination buffer
  *
  * Copies at most dstsize - 1 bytes into the destination buffer.
@@ -295,9 +341,9 @@ struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
 }
 
 /**
- * nla_reserve - reserve room for attribute without header
+ * nla_reserve_nohdr - reserve room for attribute without header
  * @skb: socket buffer to reserve room on
- * @len: length of attribute payload
+ * @attrlen: length of attribute payload
  *
  * Reserves room for attribute payload without a header.
  *
@@ -355,13 +401,13 @@ void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
  * @attrlen: length of attribute payload
  * @data: head of attribute payload
  *
- * Returns -1 if the tailroom of the skb is insufficient to store
+ * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  * the attribute header and payload.
  */
 int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
 {
        if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
-               return -1;
+               return -EMSGSIZE;
 
        __nla_put(skb, attrtype, attrlen, data);
        return 0;
@@ -373,18 +419,36 @@ int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
  * @attrlen: length of attribute payload
  * @data: head of attribute payload
  *
- * Returns -1 if the tailroom of the skb is insufficient to store
+ * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  * the attribute payload.
  */
 int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
 {
        if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
-               return -1;
+               return -EMSGSIZE;
 
        __nla_put_nohdr(skb, attrlen, data);
        return 0;
 }
 
+/**
+ * nla_append - Add a netlink attribute without header or padding
+ * @skb: socket buffer to add attribute to
+ * @attrlen: length of attribute payload
+ * @data: head of attribute payload
+ *
+ * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
+ * the attribute payload.
+ */
+int nla_append(struct sk_buff *skb, int attrlen, const void *data)
+{
+       if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
+               return -EMSGSIZE;
+
+       memcpy(skb_put(skb, attrlen), data, attrlen);
+       return 0;
+}
+
 EXPORT_SYMBOL(nla_validate);
 EXPORT_SYMBOL(nla_parse);
 EXPORT_SYMBOL(nla_find);
@@ -400,3 +464,4 @@ EXPORT_SYMBOL(nla_put_nohdr);
 EXPORT_SYMBOL(nla_memcpy);
 EXPORT_SYMBOL(nla_memcmp);
 EXPORT_SYMBOL(nla_strcmp);
+EXPORT_SYMBOL(nla_append);