[XFRM] netlink: Use nlmsg_end() and nlmsg_cancel()
[linux-2.6-block.git] / net / xfrm / xfrm_user.c
index 61339e17a0f5dd52bb3ba69f24b47366833d5dc4..2c74db0c9233acb6522d6dd75390a287e4be3a73 100644 (file)
@@ -583,15 +583,14 @@ static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
        struct sk_buff *skb = sp->out_skb;
        struct xfrm_usersa_info *p;
        struct nlmsghdr *nlh;
-       unsigned char *b = skb_tail_pointer(skb);
 
        if (sp->this_idx < sp->start_idx)
                goto out;
 
-       nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
-                       sp->nlmsg_seq,
-                       XFRM_MSG_NEWSA, sizeof(*p));
-       nlh->nlmsg_flags = sp->nlmsg_flags;
+       nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
+                       XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
+       if (nlh == NULL)
+               return -EMSGSIZE;
 
        p = NLMSG_DATA(nlh);
        copy_to_user_state(x, p);
@@ -628,15 +627,14 @@ static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
        if (x->lastused)
                RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
 
-       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
+       nlmsg_end(skb, nlh);
 out:
        sp->this_idx++;
        return 0;
 
-nlmsg_failure:
 rtattr_failure:
-       nlmsg_trim(skb, b);
-       return -1;
+       nlmsg_cancel(skb, nlh);
+       return -EMSGSIZE;
 }
 
 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
@@ -1271,16 +1269,15 @@ 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;
-       unsigned char *b = skb_tail_pointer(skb);
 
        if (sp->this_idx < sp->start_idx)
                goto out;
 
-       nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
-                       sp->nlmsg_seq,
-                       XFRM_MSG_NEWPOLICY, sizeof(*p));
+       nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
+                       XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
+       if (nlh == NULL)
+               return -EMSGSIZE;
        p = NLMSG_DATA(nlh);
-       nlh->nlmsg_flags = sp->nlmsg_flags;
 
        copy_to_user_policy(xp, p, dir);
        if (copy_to_user_tmpl(xp, skb) < 0)
@@ -1290,14 +1287,14 @@ static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr
        if (copy_to_user_policy_type(xp->type, skb) < 0)
                goto nlmsg_failure;
 
-       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
+       nlmsg_end(skb, nlh);
 out:
        sp->this_idx++;
        return 0;
 
 nlmsg_failure:
-       nlmsg_trim(skb, b);
-       return -1;
+       nlmsg_cancel(skb, nlh);
+       return -EMSGSIZE;
 }
 
 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
@@ -1447,11 +1444,11 @@ static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_eve
        struct xfrm_aevent_id *id;
        struct nlmsghdr *nlh;
        struct xfrm_lifetime_cur ltime;
-       unsigned char *b = skb_tail_pointer(skb);
 
-       nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
+       nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
+       if (nlh == NULL)
+               return -EMSGSIZE;
        id = NLMSG_DATA(nlh);
-       nlh->nlmsg_flags = 0;
 
        memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
        id->sa_id.spi = x->id.spi;
@@ -1479,13 +1476,11 @@ static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_eve
                RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
        }
 
-       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
-       return skb->len;
+       return nlmsg_end(skb, nlh);
 
 rtattr_failure:
-nlmsg_failure:
-       nlmsg_trim(skb, b);
-       return -1;
+       nlmsg_cancel(skb, nlh);
+       return -EMSGSIZE;
 }
 
 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
@@ -1863,12 +1858,12 @@ static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
        struct xfrm_migrate *mp;
        struct xfrm_userpolicy_id *pol_id;
        struct nlmsghdr *nlh;
-       unsigned char *b = skb_tail_pointer(skb);
        int i;
 
-       nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id));
+       nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
+       if (nlh == NULL)
+               return -EMSGSIZE;
        pol_id = NLMSG_DATA(nlh);
-       nlh->nlmsg_flags = 0;
 
        /* copy data from selector, dir, and type to the pol_id */
        memset(pol_id, 0, sizeof(*pol_id));
@@ -1883,11 +1878,10 @@ static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
                        goto nlmsg_failure;
        }
 
-       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
-       return skb->len;
+       return nlmsg_end(skb, nlh);
 nlmsg_failure:
-       nlmsg_trim(skb, b);
-       return -1;
+       nlmsg_cancel(skb, nlh);
+       return -EMSGSIZE;
 }
 
 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
@@ -2043,22 +2037,16 @@ static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_eve
 {
        struct xfrm_user_expire *ue;
        struct nlmsghdr *nlh;
-       unsigned char *b = skb_tail_pointer(skb);
 
-       nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
-                       sizeof(*ue));
+       nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
+       if (nlh == NULL)
+               return -EMSGSIZE;
        ue = NLMSG_DATA(nlh);
-       nlh->nlmsg_flags = 0;
 
        copy_to_user_state(x, &ue->state);
        ue->hard = (c->data.hard != 0) ? 1 : 0;
 
-       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
-       return skb->len;
-
-nlmsg_failure:
-       nlmsg_trim(skb, b);
-       return -1;
+       return nlmsg_end(skb, nlh);
 }
 
 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
@@ -2100,29 +2088,25 @@ static int xfrm_notify_sa_flush(struct km_event *c)
        struct xfrm_usersa_flush *p;
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
-       sk_buff_data_t b;
        int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
 
        skb = alloc_skb(len, GFP_ATOMIC);
        if (skb == NULL)
                return -ENOMEM;
-       b = skb->tail;
 
-       nlh = NLMSG_PUT(skb, c->pid, c->seq,
-                       XFRM_MSG_FLUSHSA, sizeof(*p));
-       nlh->nlmsg_flags = 0;
+       nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
+       if (nlh == NULL) {
+               kfree_skb(skb);
+               return -EMSGSIZE;
+       }
 
        p = NLMSG_DATA(nlh);
        p->proto = c->data.proto;
 
-       nlh->nlmsg_len = skb->tail - b;
+       nlmsg_end(skb, nlh);
 
        NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
        return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
-
-nlmsg_failure:
-       kfree_skb(skb);
-       return -1;
 }
 
 static inline int xfrm_sa_len(struct xfrm_state *x)
@@ -2146,7 +2130,6 @@ static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
        struct xfrm_usersa_id *id;
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
-       sk_buff_data_t b;
        int len = xfrm_sa_len(x);
        int headlen;
 
@@ -2160,10 +2143,10 @@ static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
        skb = alloc_skb(len, GFP_ATOMIC);
        if (skb == NULL)
                return -ENOMEM;
-       b = skb->tail;
 
-       nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
-       nlh->nlmsg_flags = 0;
+       nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
+       if (nlh == NULL)
+               goto nlmsg_failure;
 
        p = NLMSG_DATA(nlh);
        if (c->event == XFRM_MSG_DELSA) {
@@ -2190,7 +2173,7 @@ static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
        if (x->encap)
                RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
 
-       nlh->nlmsg_len = skb->tail - b;
+       nlmsg_end(skb, nlh);
 
        NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
        return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
@@ -2230,13 +2213,12 @@ static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
 {
        struct xfrm_user_acquire *ua;
        struct nlmsghdr *nlh;
-       unsigned char *b = skb_tail_pointer(skb);
        __u32 seq = xfrm_get_acqseq();
 
-       nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
-                       sizeof(*ua));
+       nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
+       if (nlh == NULL)
+               return -EMSGSIZE;
        ua = NLMSG_DATA(nlh);
-       nlh->nlmsg_flags = 0;
 
        memcpy(&ua->id, &x->id, sizeof(ua->id));
        memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
@@ -2254,12 +2236,11 @@ static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
        if (copy_to_user_policy_type(xp->type, skb) < 0)
                goto nlmsg_failure;
 
-       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
-       return skb->len;
+       return nlmsg_end(skb, nlh);
 
 nlmsg_failure:
-       nlmsg_trim(skb, b);
-       return -1;
+       nlmsg_cancel(skb, nlh);
+       return -EMSGSIZE;
 }
 
 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
@@ -2350,11 +2331,11 @@ static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
        struct xfrm_user_polexpire *upe;
        struct nlmsghdr *nlh;
        int hard = c->data.hard;
-       unsigned char *b = skb_tail_pointer(skb);
 
-       nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
+       nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
+       if (nlh == NULL)
+               return -EMSGSIZE;
        upe = NLMSG_DATA(nlh);
-       nlh->nlmsg_flags = 0;
 
        copy_to_user_policy(xp, &upe->pol, dir);
        if (copy_to_user_tmpl(xp, skb) < 0)
@@ -2365,12 +2346,11 @@ static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
                goto nlmsg_failure;
        upe->hard = !!hard;
 
-       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
-       return skb->len;
+       return nlmsg_end(skb, nlh);
 
 nlmsg_failure:
-       nlmsg_trim(skb, b);
-       return -1;
+       nlmsg_cancel(skb, nlh);
+       return -EMSGSIZE;
 }
 
 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
@@ -2401,7 +2381,6 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
        struct xfrm_userpolicy_id *id;
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
-       sk_buff_data_t b;
        int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
        int headlen;
 
@@ -2418,9 +2397,10 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
        skb = alloc_skb(len, GFP_ATOMIC);
        if (skb == NULL)
                return -ENOMEM;
-       b = skb->tail;
 
-       nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
+       nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
+       if (nlh == NULL)
+               goto nlmsg_failure;
 
        p = NLMSG_DATA(nlh);
        if (c->event == XFRM_MSG_DELPOLICY) {
@@ -2435,15 +2415,13 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
                p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
        }
 
-       nlh->nlmsg_flags = 0;
-
        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;
 
-       nlh->nlmsg_len = skb->tail - b;
+       nlmsg_end(skb, nlh);
 
        NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
        return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
@@ -2458,7 +2436,6 @@ static int xfrm_notify_policy_flush(struct km_event *c)
 {
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
-       sk_buff_data_t b;
        int len = 0;
 #ifdef CONFIG_XFRM_SUB_POLICY
        len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
@@ -2468,15 +2445,14 @@ static int xfrm_notify_policy_flush(struct km_event *c)
        skb = alloc_skb(len, GFP_ATOMIC);
        if (skb == NULL)
                return -ENOMEM;
-       b = skb->tail;
-
 
-       nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
-       nlh->nlmsg_flags = 0;
+       nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
+       if (nlh == NULL)
+               goto nlmsg_failure;
        if (copy_to_user_policy_type(c->data.type, skb) < 0)
                goto nlmsg_failure;
 
-       nlh->nlmsg_len = skb->tail - b;
+       nlmsg_end(skb, nlh);
 
        NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
        return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
@@ -2511,11 +2487,11 @@ static int build_report(struct sk_buff *skb, u8 proto,
 {
        struct xfrm_user_report *ur;
        struct nlmsghdr *nlh;
-       unsigned char *b = skb_tail_pointer(skb);
 
-       nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
+       nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
+       if (nlh == NULL)
+               return -EMSGSIZE;
        ur = NLMSG_DATA(nlh);
-       nlh->nlmsg_flags = 0;
 
        ur->proto = proto;
        memcpy(&ur->sel, sel, sizeof(ur->sel));
@@ -2523,13 +2499,11 @@ static int build_report(struct sk_buff *skb, u8 proto,
        if (addr)
                RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
 
-       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
-       return skb->len;
+       return nlmsg_end(skb, nlh);
 
-nlmsg_failure:
 rtattr_failure:
-       nlmsg_trim(skb, b);
-       return -1;
+       nlmsg_cancel(skb, nlh);
+       return -EMSGSIZE;
 }
 
 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,