ACPI: ibm-acpi: extend fan status functions
[sfrench/cifs-2.6.git] / net / xfrm / xfrm_user.c
index f643063a1cbd2bff60150ea3625f34fda796b0ff..2b2e59d8ffbc8ca197c8b84b98248c92511ce365 100644 (file)
@@ -28,6 +28,9 @@
 #include <net/xfrm.h>
 #include <net/netlink.h>
 #include <asm/uaccess.h>
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+#include <linux/in6.h>
+#endif
 
 static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
 {
@@ -173,6 +176,19 @@ static int verify_newsa_info(struct xfrm_usersa_info *p,
                        goto out;
                break;
 
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+       case IPPROTO_DSTOPTS:
+       case IPPROTO_ROUTING:
+               if (xfrma[XFRMA_ALG_COMP-1]     ||
+                   xfrma[XFRMA_ALG_AUTH-1]     ||
+                   xfrma[XFRMA_ALG_CRYPT-1]    ||
+                   xfrma[XFRMA_ENCAP-1]        ||
+                   xfrma[XFRMA_SEC_CTX-1]      ||
+                   !xfrma[XFRMA_COADDR-1])
+                       goto out;
+               break;
+#endif
+
        default:
                goto out;
        };
@@ -195,6 +211,7 @@ static int verify_newsa_info(struct xfrm_usersa_info *p,
        case XFRM_MODE_TRANSPORT:
        case XFRM_MODE_TUNNEL:
        case XFRM_MODE_ROUTEOPTIMIZATION:
+       case XFRM_MODE_BEET:
                break;
 
        default:
@@ -770,6 +787,22 @@ static int verify_policy_dir(__u8 dir)
        return 0;
 }
 
+static int verify_policy_type(__u8 type)
+{
+       switch (type) {
+       case XFRM_POLICY_TYPE_MAIN:
+#ifdef CONFIG_XFRM_SUB_POLICY
+       case XFRM_POLICY_TYPE_SUB:
+#endif
+               break;
+
+       default:
+               return -EINVAL;
+       };
+
+       return 0;
+}
+
 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
 {
        switch (p->share) {
@@ -863,6 +896,29 @@ static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
        return 0;
 }
 
+static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
+{
+       struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
+       struct xfrm_userpolicy_type *upt;
+       __u8 type = XFRM_POLICY_TYPE_MAIN;
+       int err;
+
+       if (rt) {
+               if (rt->rta_len < sizeof(*upt))
+                       return -EINVAL;
+
+               upt = RTA_DATA(rt);
+               type = upt->type;
+       }
+
+       err = verify_policy_type(type);
+       if (err)
+               return err;
+
+       *tp = type;
+       return 0;
+}
+
 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
 {
        xp->priority = p->priority;
@@ -901,16 +957,20 @@ static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p,
 
        copy_from_user_policy(xp, p);
 
+       err = copy_from_user_policy_type(&xp->type, xfrma);
+       if (err)
+               goto error;
+
        if (!(err = copy_from_user_tmpl(xp, xfrma)))
                err = copy_from_user_sec_ctx(xp, xfrma);
-
-       if (err) {
-               *errp = err;
-               kfree(xp);
-               xp = NULL;
-       }
+       if (err)
+               goto error;
 
        return xp;
+ error:
+       *errp = err;
+       kfree(xp);
+       return NULL;
 }
 
 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
@@ -1021,6 +1081,29 @@ static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *s
        return 0;
 }
 
+#ifdef CONFIG_XFRM_SUB_POLICY
+static int copy_to_user_policy_type(struct xfrm_policy *xp, struct sk_buff *skb)
+{
+       struct xfrm_userpolicy_type upt;
+
+       memset(&upt, 0, sizeof(upt));
+       upt.type = xp->type;
+
+       RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
+
+       return 0;
+
+rtattr_failure:
+       return -1;
+}
+
+#else
+static inline int copy_to_user_policy_type(struct xfrm_policy *xp, struct sk_buff *skb)
+{
+       return 0;
+}
+#endif
+
 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
 {
        struct xfrm_dump_info *sp = ptr;
@@ -1044,6 +1127,8 @@ static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr
                goto nlmsg_failure;
        if (copy_to_user_sec_ctx(xp, skb))
                goto nlmsg_failure;
+       if (copy_to_user_policy_type(xp, skb) < 0)
+               goto nlmsg_failure;
 
        nlh->nlmsg_len = skb->tail - b;
 out:
@@ -1065,7 +1150,10 @@ static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
        info.nlmsg_flags = NLM_F_MULTI;
        info.this_idx = 0;
        info.start_idx = cb->args[0];
-       (void) xfrm_policy_walk(dump_one_policy, &info);
+       (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
+#ifdef CONFIG_XFRM_SUB_POLICY
+       (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
+#endif
        cb->args[0] = info.this_idx;
 
        return skb->len;
@@ -1101,6 +1189,7 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
 {
        struct xfrm_policy *xp;
        struct xfrm_userpolicy_id *p;
+       __u8 type = XFRM_POLICY_TYPE_MAIN;
        int err;
        struct km_event c;
        int delete;
@@ -1108,12 +1197,16 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
        p = NLMSG_DATA(nlh);
        delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
 
+       err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
+       if (err)
+               return err;
+
        err = verify_policy_dir(p->dir);
        if (err)
                return err;
 
        if (p->index)
-               xp = xfrm_policy_byid(p->dir, p->index, delete);
+               xp = xfrm_policy_byid(type, p->dir, p->index, delete);
        else {
                struct rtattr **rtattrs = (struct rtattr **)xfrma;
                struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
@@ -1130,7 +1223,7 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
                        if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
                                return err;
                }
-               xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
+               xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, delete);
                security_xfrm_policy_free(&tmp);
        }
        if (xp == NULL)
@@ -1313,9 +1406,16 @@ out:
 
 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
 {
-struct km_event c;
+       struct km_event c;
+       __u8 type = XFRM_POLICY_TYPE_MAIN;
+       int err;
 
-       xfrm_policy_flush();
+       err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
+       if (err)
+               return err;
+
+       xfrm_policy_flush(type);
+       c.data.type = type;
        c.event = nlh->nlmsg_type;
        c.seq = nlh->nlmsg_seq;
        c.pid = nlh->nlmsg_pid;
@@ -1328,10 +1428,15 @@ static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void *
        struct xfrm_policy *xp;
        struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
        struct xfrm_userpolicy_info *p = &up->pol;
+       __u8 type = XFRM_POLICY_TYPE_MAIN;
        int err = -ENOENT;
 
+       err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
+       if (err)
+               return err;
+
        if (p->index)
-               xp = xfrm_policy_byid(p->dir, p->index, 0);
+               xp = xfrm_policy_byid(type, p->dir, p->index, 0);
        else {
                struct rtattr **rtattrs = (struct rtattr **)xfrma;
                struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
@@ -1348,7 +1453,7 @@ static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void *
                        if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
                                return err;
                }
-               xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, 0);
+               xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, 0);
                security_xfrm_policy_free(&tmp);
        }
 
@@ -1475,6 +1580,7 @@ static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
        [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
        [XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
        [XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
+       [XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
 };
 
 #undef XMSGSIZE
@@ -1801,6 +1907,8 @@ static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
                goto nlmsg_failure;
        if (copy_to_user_state_sec_ctx(x, skb))
                goto nlmsg_failure;
+       if (copy_to_user_policy_type(xp, skb) < 0)
+               goto nlmsg_failure;
 
        nlh->nlmsg_len = skb->tail - b;
        return skb->len;
@@ -1881,17 +1989,9 @@ static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
        }
 
        copy_from_user_policy(xp, p);
+       xp->type = XFRM_POLICY_TYPE_MAIN;
        copy_templates(xp, ut, nr);
 
-       if (!xp->security) {
-               int err = security_xfrm_sock_policy_alloc(xp, sk);
-               if (err) {
-                       kfree(xp);
-                       *dir = err;
-                       return NULL;
-               }
-       }
-
        *dir = p->dir;
 
        return xp;
@@ -1914,6 +2014,8 @@ static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
                goto nlmsg_failure;
        if (copy_to_user_sec_ctx(xp, skb))
                goto nlmsg_failure;
+       if (copy_to_user_policy_type(xp, skb) < 0)
+               goto nlmsg_failure;
        upe->hard = !!hard;
 
        nlh->nlmsg_len = skb->tail - b;
@@ -1985,6 +2087,8 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
        copy_to_user_policy(xp, p, dir);
        if (copy_to_user_tmpl(xp, skb) < 0)
                goto nlmsg_failure;
+       if (copy_to_user_policy_type(xp, skb) < 0)
+               goto nlmsg_failure;
 
        nlh->nlmsg_len = skb->tail - b;
 
@@ -2002,6 +2106,9 @@ static int xfrm_notify_policy_flush(struct km_event *c)
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
        unsigned char *b;
+#ifdef CONFIG_XFRM_SUB_POLICY
+       struct xfrm_userpolicy_type upt;
+#endif
        int len = NLMSG_LENGTH(0);
 
        skb = alloc_skb(len, GFP_ATOMIC);
@@ -2011,6 +2118,13 @@ static int xfrm_notify_policy_flush(struct km_event *c)
 
 
        nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
+       nlh->nlmsg_flags = 0;
+
+#ifdef CONFIG_XFRM_SUB_POLICY
+       memset(&upt, 0, sizeof(upt));
+       upt.type = c->data.type;
+       RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
+#endif
 
        nlh->nlmsg_len = skb->tail - b;
 
@@ -2018,6 +2132,9 @@ static int xfrm_notify_policy_flush(struct km_event *c)
        return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
 
 nlmsg_failure:
+#ifdef CONFIG_XFRM_SUB_POLICY
+rtattr_failure:
+#endif
        kfree_skb(skb);
        return -1;
 }
@@ -2042,19 +2159,64 @@ static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_ev
 
 }
 
+static int build_report(struct sk_buff *skb, u8 proto,
+                       struct xfrm_selector *sel, xfrm_address_t *addr)
+{
+       struct xfrm_user_report *ur;
+       struct nlmsghdr *nlh;
+       unsigned char *b = skb->tail;
+
+       nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
+       ur = NLMSG_DATA(nlh);
+       nlh->nlmsg_flags = 0;
+
+       ur->proto = proto;
+       memcpy(&ur->sel, sel, sizeof(ur->sel));
+
+       if (addr)
+               RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
+
+       nlh->nlmsg_len = skb->tail - b;
+       return skb->len;
+
+nlmsg_failure:
+rtattr_failure:
+       skb_trim(skb, b - skb->data);
+       return -1;
+}
+
+static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
+                           xfrm_address_t *addr)
+{
+       struct sk_buff *skb;
+       size_t len;
+
+       len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
+       skb = alloc_skb(len, GFP_ATOMIC);
+       if (skb == NULL)
+               return -ENOMEM;
+
+       if (build_report(skb, proto, sel, addr) < 0)
+               BUG();
+
+       NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
+       return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
+}
+
 static struct xfrm_mgr netlink_mgr = {
        .id             = "netlink",
        .notify         = xfrm_send_state_notify,
        .acquire        = xfrm_send_acquire,
        .compile_policy = xfrm_compile_policy,
        .notify_policy  = xfrm_send_policy_notify,
+       .report         = xfrm_send_report,
 };
 
 static int __init xfrm_user_init(void)
 {
        struct sock *nlsk;
 
-       printk(KERN_INFO "Initializing IPsec netlink socket\n");
+       printk(KERN_INFO "Initializing XFRM netlink socket\n");
 
        nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
                                     xfrm_netlink_rcv, THIS_MODULE);