xfrm: Report user triggered expirations against the users socket
[deliverable/linux.git] / net / xfrm / xfrm_user.c
index 44293b3fd6a1d1d4dc2e6ae47f0cdb91b2226834..5d6eb4b3c089f406ebf4397e034e26152bcb40dc 100644 (file)
@@ -603,7 +603,7 @@ static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
        }
 
        c.seq = nlh->nlmsg_seq;
-       c.pid = nlh->nlmsg_pid;
+       c.portid = nlh->nlmsg_pid;
        c.event = nlh->nlmsg_type;
 
        km_state_notify(x, &c);
@@ -676,7 +676,7 @@ static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
                goto out;
 
        c.seq = nlh->nlmsg_seq;
-       c.pid = nlh->nlmsg_pid;
+       c.portid = nlh->nlmsg_pid;
        c.event = nlh->nlmsg_type;
        km_state_notify(x, &c);
 
@@ -754,58 +754,67 @@ static int copy_to_user_state_extra(struct xfrm_state *x,
                                    struct xfrm_usersa_info *p,
                                    struct sk_buff *skb)
 {
-       copy_to_user_state(x, p);
-
-       if (x->coaddr &&
-           nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr))
-               goto nla_put_failure;
-
-       if (x->lastused &&
-           nla_put_u64(skb, XFRMA_LASTUSED, x->lastused))
-               goto nla_put_failure;
-
-       if (x->aead &&
-           nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead))
-               goto nla_put_failure;
-
-       if (x->aalg &&
-           (copy_to_user_auth(x->aalg, skb) ||
-            nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
-                    xfrm_alg_auth_len(x->aalg), x->aalg)))
-               goto nla_put_failure;
-
-       if (x->ealg &&
-           nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg))
-               goto nla_put_failure;
-
-       if (x->calg &&
-           nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg))
-               goto nla_put_failure;
-
-       if (x->encap &&
-           nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap))
-               goto nla_put_failure;
+       int ret = 0;
 
-       if (x->tfcpad &&
-           nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad))
-               goto nla_put_failure;
-
-       if (xfrm_mark_put(skb, &x->mark))
-               goto nla_put_failure;
-
-       if (x->replay_esn &&
-           nla_put(skb, XFRMA_REPLAY_ESN_VAL,
-                   xfrm_replay_state_esn_len(x->replay_esn),
-                   x->replay_esn))
-               goto nla_put_failure;
-
-       if (x->security && copy_sec_ctx(x->security, skb))
-               goto nla_put_failure;
-
-       return 0;
+       copy_to_user_state(x, p);
 
-nla_put_failure:
-       return -EMSGSIZE;
+       if (x->coaddr) {
+               ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
+               if (ret)
+                       goto out;
+       }
+       if (x->lastused) {
+               ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
+               if (ret)
+                       goto out;
+       }
+       if (x->aead) {
+               ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
+               if (ret)
+                       goto out;
+       }
+       if (x->aalg) {
+               ret = copy_to_user_auth(x->aalg, skb);
+               if (!ret)
+                       ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
+                                     xfrm_alg_auth_len(x->aalg), x->aalg);
+               if (ret)
+                       goto out;
+       }
+       if (x->ealg) {
+               ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
+               if (ret)
+                       goto out;
+       }
+       if (x->calg) {
+               ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
+               if (ret)
+                       goto out;
+       }
+       if (x->encap) {
+               ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
+               if (ret)
+                       goto out;
+       }
+       if (x->tfcpad) {
+               ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
+               if (ret)
+                       goto out;
+       }
+       ret = xfrm_mark_put(skb, &x->mark);
+       if (ret)
+               goto out;
+       if (x->replay_esn) {
+               ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
+                             xfrm_replay_state_esn_len(x->replay_esn),
+                             x->replay_esn);
+               if (ret)
+                       goto out;
+       }
+       if (x->security)
+               ret = copy_sec_ctx(x->security, skb);
+out:
+       return ret;
 }
 
 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
@@ -817,7 +826,7 @@ static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
        struct nlmsghdr *nlh;
        int err;
 
-       nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
+       nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
                        XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
        if (nlh == NULL)
                return -EMSGSIZE;
@@ -825,15 +834,12 @@ static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
        p = nlmsg_data(nlh);
 
        err = copy_to_user_state_extra(x, p, skb);
-       if (err)
-               goto nla_put_failure;
-
+       if (err) {
+               nlmsg_cancel(skb, nlh);
+               return err;
+       }
        nlmsg_end(skb, nlh);
        return 0;
-
-nla_put_failure:
-       nlmsg_cancel(skb, nlh);
-       return err;
 }
 
 static int xfrm_dump_sa_done(struct netlink_callback *cb)
@@ -898,15 +904,16 @@ static inline size_t xfrm_spdinfo_msgsize(void)
 }
 
 static int build_spdinfo(struct sk_buff *skb, struct net *net,
-                        u32 pid, u32 seq, u32 flags)
+                        u32 portid, u32 seq, u32 flags)
 {
        struct xfrmk_spdinfo si;
        struct xfrmu_spdinfo spc;
        struct xfrmu_spdhinfo sph;
        struct nlmsghdr *nlh;
+       int err;
        u32 *f;
 
-       nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
+       nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
        if (nlh == NULL) /* shouldn't really happen ... */
                return -EMSGSIZE;
 
@@ -922,15 +929,15 @@ static int build_spdinfo(struct sk_buff *skb, struct net *net,
        sph.spdhcnt = si.spdhcnt;
        sph.spdhmcnt = si.spdhmcnt;
 
-       if (nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc) ||
-           nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph))
-               goto nla_put_failure;
+       err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
+       if (!err)
+               err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
+       if (err) {
+               nlmsg_cancel(skb, nlh);
+               return err;
+       }
 
        return nlmsg_end(skb, nlh);
-
-nla_put_failure:
-       nlmsg_cancel(skb, nlh);
-       return -EMSGSIZE;
 }
 
 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
@@ -939,17 +946,17 @@ static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
        struct net *net = sock_net(skb->sk);
        struct sk_buff *r_skb;
        u32 *flags = nlmsg_data(nlh);
-       u32 spid = NETLINK_CB(skb).pid;
+       u32 sportid = NETLINK_CB(skb).portid;
        u32 seq = nlh->nlmsg_seq;
 
        r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
        if (r_skb == NULL)
                return -ENOMEM;
 
-       if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
+       if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
                BUG();
 
-       return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
+       return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
 }
 
 static inline size_t xfrm_sadinfo_msgsize(void)
@@ -960,14 +967,15 @@ static inline size_t xfrm_sadinfo_msgsize(void)
 }
 
 static int build_sadinfo(struct sk_buff *skb, struct net *net,
-                        u32 pid, u32 seq, u32 flags)
+                        u32 portid, u32 seq, u32 flags)
 {
        struct xfrmk_sadinfo si;
        struct xfrmu_sadhinfo sh;
        struct nlmsghdr *nlh;
+       int err;
        u32 *f;
 
-       nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
+       nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
        if (nlh == NULL) /* shouldn't really happen ... */
                return -EMSGSIZE;
 
@@ -978,15 +986,15 @@ static int build_sadinfo(struct sk_buff *skb, struct net *net,
        sh.sadhmcnt = si.sadhmcnt;
        sh.sadhcnt = si.sadhcnt;
 
-       if (nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt) ||
-           nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh))
-               goto nla_put_failure;
+       err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
+       if (!err)
+               err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
+       if (err) {
+               nlmsg_cancel(skb, nlh);
+               return err;
+       }
 
        return nlmsg_end(skb, nlh);
-
-nla_put_failure:
-       nlmsg_cancel(skb, nlh);
-       return -EMSGSIZE;
 }
 
 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
@@ -995,17 +1003,17 @@ static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
        struct net *net = sock_net(skb->sk);
        struct sk_buff *r_skb;
        u32 *flags = nlmsg_data(nlh);
-       u32 spid = NETLINK_CB(skb).pid;
+       u32 sportid = NETLINK_CB(skb).portid;
        u32 seq = nlh->nlmsg_seq;
 
        r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
        if (r_skb == NULL)
                return -ENOMEM;
 
-       if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
+       if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
                BUG();
 
-       return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
+       return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
 }
 
 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
@@ -1025,7 +1033,7 @@ static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
        if (IS_ERR(resp_skb)) {
                err = PTR_ERR(resp_skb);
        } else {
-               err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
+               err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
        }
        xfrm_state_put(x);
 out_noput:
@@ -1106,7 +1114,7 @@ static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
                goto out;
        }
 
-       err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
+       err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
 
 out:
        xfrm_state_put(x);
@@ -1393,7 +1401,7 @@ static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
 
        c.event = nlh->nlmsg_type;
        c.seq = nlh->nlmsg_seq;
-       c.pid = nlh->nlmsg_pid;
+       c.portid = nlh->nlmsg_pid;
        km_policy_notify(xp, p->dir, &c);
 
        xfrm_pol_put(xp);
@@ -1439,9 +1447,8 @@ static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buf
 
 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
 {
-       if (xp->security) {
+       if (xp->security)
                return copy_sec_ctx(xp->security, skb);
-       }
        return 0;
 }
 static inline size_t userpolicy_type_attrsize(void)
@@ -1477,30 +1484,28 @@ static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr
        struct sk_buff *in_skb = sp->in_skb;
        struct sk_buff *skb = sp->out_skb;
        struct nlmsghdr *nlh;
+       int err;
 
-       nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
+       nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
                        XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
        if (nlh == NULL)
                return -EMSGSIZE;
 
        p = nlmsg_data(nlh);
        copy_to_user_policy(xp, p, dir);
-       if (copy_to_user_tmpl(xp, skb) < 0)
-               goto nlmsg_failure;
-       if (copy_to_user_sec_ctx(xp, skb))
-               goto nlmsg_failure;
-       if (copy_to_user_policy_type(xp->type, skb) < 0)
-               goto nlmsg_failure;
-       if (xfrm_mark_put(skb, &xp->mark))
-               goto nla_put_failure;
-
+       err = copy_to_user_tmpl(xp, skb);
+       if (!err)
+               err = copy_to_user_sec_ctx(xp, skb);
+       if (!err)
+               err = copy_to_user_policy_type(xp->type, skb);
+       if (!err)
+               err = xfrm_mark_put(skb, &xp->mark);
+       if (err) {
+               nlmsg_cancel(skb, nlh);
+               return err;
+       }
        nlmsg_end(skb, nlh);
        return 0;
-
-nla_put_failure:
-nlmsg_failure:
-       nlmsg_cancel(skb, nlh);
-       return -EMSGSIZE;
 }
 
 static int xfrm_dump_policy_done(struct netlink_callback *cb)
@@ -1616,7 +1621,7 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
                        err = PTR_ERR(resp_skb);
                } else {
                        err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
-                                           NETLINK_CB(skb).pid);
+                                           NETLINK_CB(skb).portid);
                }
        } else {
                uid_t loginuid = audit_get_loginuid(current);
@@ -1633,7 +1638,7 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
                c.data.byid = p->index;
                c.event = nlh->nlmsg_type;
                c.seq = nlh->nlmsg_seq;
-               c.pid = nlh->nlmsg_pid;
+               c.portid = nlh->nlmsg_pid;
                km_policy_notify(xp, p->dir, &c);
        }
 
@@ -1663,7 +1668,7 @@ static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
        c.data.proto = p->proto;
        c.event = nlh->nlmsg_type;
        c.seq = nlh->nlmsg_seq;
-       c.pid = nlh->nlmsg_pid;
+       c.portid = nlh->nlmsg_pid;
        c.net = net;
        km_state_notify(NULL, &c);
 
@@ -1688,8 +1693,9 @@ static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct
 {
        struct xfrm_aevent_id *id;
        struct nlmsghdr *nlh;
+       int err;
 
-       nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
+       nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
        if (nlh == NULL)
                return -EMSGSIZE;
 
@@ -1703,35 +1709,39 @@ static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct
        id->flags = c->data.aevent;
 
        if (x->replay_esn) {
-               if (nla_put(skb, XFRMA_REPLAY_ESN_VAL,
-                           xfrm_replay_state_esn_len(x->replay_esn),
-                           x->replay_esn))
-                       goto nla_put_failure;
+               err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
+                             xfrm_replay_state_esn_len(x->replay_esn),
+                             x->replay_esn);
        } else {
-               if (nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
-                           &x->replay))
-                       goto nla_put_failure;
+               err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
+                             &x->replay);
        }
-       if (nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft))
-               goto nla_put_failure;
-
-       if ((id->flags & XFRM_AE_RTHR) &&
-           nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff))
-               goto nla_put_failure;
-
-       if ((id->flags & XFRM_AE_ETHR) &&
-           nla_put_u32(skb, XFRMA_ETIMER_THRESH,
-                       x->replay_maxage * 10 / HZ))
-               goto nla_put_failure;
+       if (err)
+               goto out_cancel;
+       err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
+       if (err)
+               goto out_cancel;
 
-       if (xfrm_mark_put(skb, &x->mark))
-               goto nla_put_failure;
+       if (id->flags & XFRM_AE_RTHR) {
+               err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
+               if (err)
+                       goto out_cancel;
+       }
+       if (id->flags & XFRM_AE_ETHR) {
+               err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
+                                 x->replay_maxage * 10 / HZ);
+               if (err)
+                       goto out_cancel;
+       }
+       err = xfrm_mark_put(skb, &x->mark);
+       if (err)
+               goto out_cancel;
 
        return nlmsg_end(skb, nlh);
 
-nla_put_failure:
+out_cancel:
        nlmsg_cancel(skb, nlh);
-       return -EMSGSIZE;
+       return err;
 }
 
 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
@@ -1767,11 +1777,11 @@ static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
        spin_lock_bh(&x->lock);
        c.data.aevent = p->flags;
        c.seq = nlh->nlmsg_seq;
-       c.pid = nlh->nlmsg_pid;
+       c.portid = nlh->nlmsg_pid;
 
        if (build_aevent(r_skb, x, &c) < 0)
                BUG();
-       err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
+       err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
        spin_unlock_bh(&x->lock);
        xfrm_state_put(x);
        return err;
@@ -1817,7 +1827,7 @@ static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
 
        c.event = nlh->nlmsg_type;
        c.seq = nlh->nlmsg_seq;
-       c.pid = nlh->nlmsg_pid;
+       c.portid = nlh->nlmsg_pid;
        c.data.aevent = XFRM_AE_CU;
        km_state_notify(x, &c);
        err = 0;
@@ -1852,7 +1862,7 @@ static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
        c.data.type = type;
        c.event = nlh->nlmsg_type;
        c.seq = nlh->nlmsg_seq;
-       c.pid = nlh->nlmsg_pid;
+       c.portid = nlh->nlmsg_pid;
        c.net = net;
        km_policy_notify(NULL, 0, &c);
        return 0;
@@ -1920,7 +1930,7 @@ static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
                // reset the timers here?
                WARN(1, "Dont know what to do with soft policy expire\n");
        }
-       km_policy_expired(xp, p->dir, up->hard, current->pid);
+       km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
 
 out:
        xfrm_pol_put(xp);
@@ -1948,7 +1958,7 @@ static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
        err = -EINVAL;
        if (x->km.state != XFRM_STATE_VALID)
                goto out;
-       km_state_expired(x, ue->hard, current->pid);
+       km_state_expired(x, ue->hard, nlh->nlmsg_pid);
 
        if (ue->hard) {
                uid_t loginuid = audit_get_loginuid(current);
@@ -2155,7 +2165,7 @@ static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
        const struct xfrm_migrate *mp;
        struct xfrm_userpolicy_id *pol_id;
        struct nlmsghdr *nlh;
-       int i;
+       int i, err;
 
        nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
        if (nlh == NULL)
@@ -2167,21 +2177,25 @@ static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
        memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
        pol_id->dir = dir;
 
-       if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
-                       goto nlmsg_failure;
-
-       if (copy_to_user_policy_type(type, skb) < 0)
-               goto nlmsg_failure;
-
+       if (k != NULL) {
+               err = copy_to_user_kmaddress(k, skb);
+               if (err)
+                       goto out_cancel;
+       }
+       err = copy_to_user_policy_type(type, skb);
+       if (err)
+               goto out_cancel;
        for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
-               if (copy_to_user_migrate(mp, skb) < 0)
-                       goto nlmsg_failure;
+               err = copy_to_user_migrate(mp, skb);
+               if (err)
+                       goto out_cancel;
        }
 
        return nlmsg_end(skb, nlh);
-nlmsg_failure:
+
+out_cancel:
        nlmsg_cancel(skb, nlh);
-       return -EMSGSIZE;
+       return err;
 }
 
 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
@@ -2354,8 +2368,9 @@ static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct
 {
        struct xfrm_user_expire *ue;
        struct nlmsghdr *nlh;
+       int err;
 
-       nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
+       nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
        if (nlh == NULL)
                return -EMSGSIZE;
 
@@ -2363,13 +2378,11 @@ static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct
        copy_to_user_state(x, &ue->state);
        ue->hard = (c->data.hard != 0) ? 1 : 0;
 
-       if (xfrm_mark_put(skb, &x->mark))
-               goto nla_put_failure;
+       err = xfrm_mark_put(skb, &x->mark);
+       if (err)
+               return err;
 
        return nlmsg_end(skb, nlh);
-
-nla_put_failure:
-       return -EMSGSIZE;
 }
 
 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
@@ -2416,7 +2429,7 @@ static int xfrm_notify_sa_flush(const struct km_event *c)
        if (skb == NULL)
                return -ENOMEM;
 
-       nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
+       nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
        if (nlh == NULL) {
                kfree_skb(skb);
                return -EMSGSIZE;
@@ -2470,7 +2483,7 @@ static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
        int len = xfrm_sa_len(x);
-       int headlen;
+       int headlen, err;
 
        headlen = sizeof(*p);
        if (c->event == XFRM_MSG_DELSA) {
@@ -2484,9 +2497,10 @@ static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
        if (skb == NULL)
                return -ENOMEM;
 
-       nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
+       nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
+       err = -EMSGSIZE;
        if (nlh == NULL)
-               goto nla_put_failure;
+               goto out_free_skb;
 
        p = nlmsg_data(nlh);
        if (c->event == XFRM_MSG_DELSA) {
@@ -2499,24 +2513,23 @@ static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
                id->proto = x->id.proto;
 
                attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
+               err = -EMSGSIZE;
                if (attr == NULL)
-                       goto nla_put_failure;
+                       goto out_free_skb;
 
                p = nla_data(attr);
        }
-
-       if (copy_to_user_state_extra(x, p, skb))
-               goto nla_put_failure;
+       err = copy_to_user_state_extra(x, p, skb);
+       if (err)
+               goto out_free_skb;
 
        nlmsg_end(skb, nlh);
 
        return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
 
-nla_put_failure:
-       /* Somebody screwed up with xfrm_sa_len! */
-       WARN_ON(1);
+out_free_skb:
        kfree_skb(skb);
-       return -1;
+       return err;
 }
 
 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
@@ -2554,12 +2567,12 @@ static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
 }
 
 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
-                        struct xfrm_tmpl *xt, struct xfrm_policy *xp,
-                        int dir)
+                        struct xfrm_tmpl *xt, struct xfrm_policy *xp)
 {
+       __u32 seq = xfrm_get_acqseq();
        struct xfrm_user_acquire *ua;
        struct nlmsghdr *nlh;
-       __u32 seq = xfrm_get_acqseq();
+       int err;
 
        nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
        if (nlh == NULL)
@@ -2569,31 +2582,29 @@ static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
        memcpy(&ua->id, &x->id, sizeof(ua->id));
        memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
        memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
-       copy_to_user_policy(xp, &ua->policy, dir);
+       copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
        ua->aalgos = xt->aalgos;
        ua->ealgos = xt->ealgos;
        ua->calgos = xt->calgos;
        ua->seq = x->km.seq = seq;
 
-       if (copy_to_user_tmpl(xp, skb) < 0)
-               goto nlmsg_failure;
-       if (copy_to_user_state_sec_ctx(x, skb))
-               goto nlmsg_failure;
-       if (copy_to_user_policy_type(xp->type, skb) < 0)
-               goto nlmsg_failure;
-       if (xfrm_mark_put(skb, &xp->mark))
-               goto nla_put_failure;
+       err = copy_to_user_tmpl(xp, skb);
+       if (!err)
+               err = copy_to_user_state_sec_ctx(x, skb);
+       if (!err)
+               err = copy_to_user_policy_type(xp->type, skb);
+       if (!err)
+               err = xfrm_mark_put(skb, &xp->mark);
+       if (err) {
+               nlmsg_cancel(skb, nlh);
+               return err;
+       }
 
        return nlmsg_end(skb, nlh);
-
-nla_put_failure:
-nlmsg_failure:
-       nlmsg_cancel(skb, nlh);
-       return -EMSGSIZE;
 }
 
 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
-                            struct xfrm_policy *xp, int dir)
+                            struct xfrm_policy *xp)
 {
        struct net *net = xs_net(x);
        struct sk_buff *skb;
@@ -2602,7 +2613,7 @@ static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
        if (skb == NULL)
                return -ENOMEM;
 
-       if (build_acquire(skb, x, xt, xp, dir) < 0)
+       if (build_acquire(skb, x, xt, xp) < 0)
                BUG();
 
        return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
@@ -2681,31 +2692,30 @@ static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
                           int dir, const struct km_event *c)
 {
        struct xfrm_user_polexpire *upe;
-       struct nlmsghdr *nlh;
        int hard = c->data.hard;
+       struct nlmsghdr *nlh;
+       int err;
 
-       nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
+       nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
        if (nlh == NULL)
                return -EMSGSIZE;
 
        upe = nlmsg_data(nlh);
        copy_to_user_policy(xp, &upe->pol, dir);
-       if (copy_to_user_tmpl(xp, skb) < 0)
-               goto nlmsg_failure;
-       if (copy_to_user_sec_ctx(xp, skb))
-               goto nlmsg_failure;
-       if (copy_to_user_policy_type(xp->type, skb) < 0)
-               goto nlmsg_failure;
-       if (xfrm_mark_put(skb, &xp->mark))
-               goto nla_put_failure;
+       err = copy_to_user_tmpl(xp, skb);
+       if (!err)
+               err = copy_to_user_sec_ctx(xp, skb);
+       if (!err)
+               err = copy_to_user_policy_type(xp->type, skb);
+       if (!err)
+               err = xfrm_mark_put(skb, &xp->mark);
+       if (err) {
+               nlmsg_cancel(skb, nlh);
+               return err;
+       }
        upe->hard = !!hard;
 
        return nlmsg_end(skb, nlh);
-
-nla_put_failure:
-nlmsg_failure:
-       nlmsg_cancel(skb, nlh);
-       return -EMSGSIZE;
 }
 
 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
@@ -2725,13 +2735,13 @@ static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct
 
 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
 {
+       int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
        struct net *net = xp_net(xp);
        struct xfrm_userpolicy_info *p;
        struct xfrm_userpolicy_id *id;
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
-       int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
-       int headlen;
+       int headlen, err;
 
        headlen = sizeof(*p);
        if (c->event == XFRM_MSG_DELPOLICY) {
@@ -2746,9 +2756,10 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_e
        if (skb == NULL)
                return -ENOMEM;
 
-       nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
+       nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
+       err = -EMSGSIZE;
        if (nlh == NULL)
-               goto nlmsg_failure;
+               goto out_free_skb;
 
        p = nlmsg_data(nlh);
        if (c->event == XFRM_MSG_DELPOLICY) {
@@ -2763,29 +2774,29 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_e
                        memcpy(&id->sel, &xp->selector, sizeof(id->sel));
 
                attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
+               err = -EMSGSIZE;
                if (attr == NULL)
-                       goto nlmsg_failure;
+                       goto out_free_skb;
 
                p = nla_data(attr);
        }
 
        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->type, skb) < 0)
-               goto nlmsg_failure;
-
-       if (xfrm_mark_put(skb, &xp->mark))
-               goto nla_put_failure;
+       err = copy_to_user_tmpl(xp, skb);
+       if (!err)
+               err = copy_to_user_policy_type(xp->type, skb);
+       if (!err)
+               err = xfrm_mark_put(skb, &xp->mark);
+       if (err)
+               goto out_free_skb;
 
        nlmsg_end(skb, nlh);
 
        return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
 
-nla_put_failure:
-nlmsg_failure:
+out_free_skb:
        kfree_skb(skb);
-       return -1;
+       return err;
 }
 
 static int xfrm_notify_policy_flush(const struct km_event *c)
@@ -2793,24 +2804,27 @@ static int xfrm_notify_policy_flush(const struct km_event *c)
        struct net *net = c->net;
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
+       int err;
 
        skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
        if (skb == NULL)
                return -ENOMEM;
 
-       nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
+       nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
+       err = -EMSGSIZE;
        if (nlh == NULL)
-               goto nlmsg_failure;
-       if (copy_to_user_policy_type(c->data.type, skb) < 0)
-               goto nlmsg_failure;
+               goto out_free_skb;
+       err = copy_to_user_policy_type(c->data.type, skb);
+       if (err)
+               goto out_free_skb;
 
        nlmsg_end(skb, nlh);
 
        return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
 
-nlmsg_failure:
+out_free_skb:
        kfree_skb(skb);
-       return -1;
+       return err;
 }
 
 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
@@ -2853,15 +2867,14 @@ static int build_report(struct sk_buff *skb, u8 proto,
        ur->proto = proto;
        memcpy(&ur->sel, sel, sizeof(ur->sel));
 
-       if (addr &&
-           nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr))
-               goto nla_put_failure;
-
+       if (addr) {
+               int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
+               if (err) {
+                       nlmsg_cancel(skb, nlh);
+                       return err;
+               }
+       }
        return nlmsg_end(skb, nlh);
-
-nla_put_failure:
-       nlmsg_cancel(skb, nlh);
-       return -EMSGSIZE;
 }
 
 static int xfrm_send_report(struct net *net, u8 proto,
@@ -2945,9 +2958,12 @@ static struct xfrm_mgr netlink_mgr = {
 static int __net_init xfrm_user_net_init(struct net *net)
 {
        struct sock *nlsk;
+       struct netlink_kernel_cfg cfg = {
+               .groups = XFRMNLGRP_MAX,
+               .input  = xfrm_netlink_rcv,
+       };
 
-       nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
-                                    xfrm_netlink_rcv, NULL, THIS_MODULE);
+       nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
        if (nlsk == NULL)
                return -ENOMEM;
        net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
This page took 0.04692 seconds and 5 git commands to generate.