Skip to content

Commit c0144be

Browse files
tgrafDavid S. Miller
authored andcommitted
[XFRM] netlink: Use nla_put()/NLA_PUT() variantes
Also makes use of copy_sec_ctx() in another place and removes duplicated code. Signed-off-by: Thomas Graf <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 082a1ad commit c0144be

File tree

1 file changed

+67
-89
lines changed

1 file changed

+67
-89
lines changed

net/xfrm/xfrm_user.c

Lines changed: 67 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,27 @@ struct xfrm_dump_info {
576576
int this_idx;
577577
};
578578

579+
static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
580+
{
581+
int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
582+
struct xfrm_user_sec_ctx *uctx;
583+
struct nlattr *attr;
584+
585+
attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
586+
if (attr == NULL)
587+
return -EMSGSIZE;
588+
589+
uctx = nla_data(attr);
590+
uctx->exttype = XFRMA_SEC_CTX;
591+
uctx->len = ctx_size;
592+
uctx->ctx_doi = s->ctx_doi;
593+
uctx->ctx_alg = s->ctx_alg;
594+
uctx->ctx_len = s->ctx_len;
595+
memcpy(uctx + 1, s->ctx_str, s->ctx_len);
596+
597+
return 0;
598+
}
599+
579600
static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
580601
{
581602
struct xfrm_dump_info *sp = ptr;
@@ -596,43 +617,32 @@ static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
596617
copy_to_user_state(x, p);
597618

598619
if (x->aalg)
599-
RTA_PUT(skb, XFRMA_ALG_AUTH,
620+
NLA_PUT(skb, XFRMA_ALG_AUTH,
600621
sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
601622
if (x->ealg)
602-
RTA_PUT(skb, XFRMA_ALG_CRYPT,
623+
NLA_PUT(skb, XFRMA_ALG_CRYPT,
603624
sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
604625
if (x->calg)
605-
RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
626+
NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
606627

607628
if (x->encap)
608-
RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
629+
NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
609630

610-
if (x->security) {
611-
int ctx_size = sizeof(struct xfrm_sec_ctx) +
612-
x->security->ctx_len;
613-
struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
614-
struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
615-
616-
uctx->exttype = XFRMA_SEC_CTX;
617-
uctx->len = ctx_size;
618-
uctx->ctx_doi = x->security->ctx_doi;
619-
uctx->ctx_alg = x->security->ctx_alg;
620-
uctx->ctx_len = x->security->ctx_len;
621-
memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
622-
}
631+
if (x->security && copy_sec_ctx(x->security, skb) < 0)
632+
goto nla_put_failure;
623633

624634
if (x->coaddr)
625-
RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
635+
NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
626636

627637
if (x->lastused)
628-
RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
638+
NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
629639

630640
nlmsg_end(skb, nlh);
631641
out:
632642
sp->this_idx++;
633643
return 0;
634644

635-
rtattr_failure:
645+
nla_put_failure:
636646
nlmsg_cancel(skb, nlh);
637647
return -EMSGSIZE;
638648
}
@@ -1193,32 +1203,9 @@ static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
11931203
up->ealgos = kp->ealgos;
11941204
up->calgos = kp->calgos;
11951205
}
1196-
RTA_PUT(skb, XFRMA_TMPL,
1197-
(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
1198-
vec);
1199-
1200-
return 0;
1201-
1202-
rtattr_failure:
1203-
return -1;
1204-
}
1205-
1206-
static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
1207-
{
1208-
int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
1209-
struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
1210-
struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1211-
1212-
uctx->exttype = XFRMA_SEC_CTX;
1213-
uctx->len = ctx_size;
1214-
uctx->ctx_doi = s->ctx_doi;
1215-
uctx->ctx_alg = s->ctx_alg;
1216-
uctx->ctx_len = s->ctx_len;
1217-
memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1218-
return 0;
12191206

1220-
rtattr_failure:
1221-
return -1;
1207+
return nla_put(skb, XFRMA_TMPL,
1208+
sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
12221209
}
12231210

12241211
static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
@@ -1240,17 +1227,11 @@ static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *s
12401227
#ifdef CONFIG_XFRM_SUB_POLICY
12411228
static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
12421229
{
1243-
struct xfrm_userpolicy_type upt;
1230+
struct xfrm_userpolicy_type upt = {
1231+
.type = type,
1232+
};
12441233

1245-
memset(&upt, 0, sizeof(upt));
1246-
upt.type = type;
1247-
1248-
RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1249-
1250-
return 0;
1251-
1252-
rtattr_failure:
1253-
return -1;
1234+
return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
12541235
}
12551236

12561237
#else
@@ -1440,7 +1421,6 @@ static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_eve
14401421
{
14411422
struct xfrm_aevent_id *id;
14421423
struct nlmsghdr *nlh;
1443-
struct xfrm_lifetime_cur ltime;
14441424

14451425
nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
14461426
if (nlh == NULL)
@@ -1455,27 +1435,19 @@ static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_eve
14551435
id->reqid = x->props.reqid;
14561436
id->flags = c->data.aevent;
14571437

1458-
RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1459-
1460-
ltime.bytes = x->curlft.bytes;
1461-
ltime.packets = x->curlft.packets;
1462-
ltime.add_time = x->curlft.add_time;
1463-
ltime.use_time = x->curlft.use_time;
1464-
1465-
RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1438+
NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1439+
NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
14661440

1467-
if (id->flags&XFRM_AE_RTHR) {
1468-
RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1469-
}
1441+
if (id->flags & XFRM_AE_RTHR)
1442+
NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
14701443

1471-
if (id->flags&XFRM_AE_ETHR) {
1472-
u32 etimer = x->replay_maxage*10/HZ;
1473-
RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1474-
}
1444+
if (id->flags & XFRM_AE_ETHR)
1445+
NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1446+
x->replay_maxage * 10 / HZ);
14751447

14761448
return nlmsg_end(skb, nlh);
14771449

1478-
rtattr_failure:
1450+
nla_put_failure:
14791451
nlmsg_cancel(skb, nlh);
14801452
return -EMSGSIZE;
14811453
}
@@ -1840,11 +1812,7 @@ static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
18401812
memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
18411813
memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
18421814

1843-
RTA_PUT(skb, XFRMA_MIGRATE, sizeof(um), &um);
1844-
return 0;
1845-
1846-
rtattr_failure:
1847-
return -1;
1815+
return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
18481816
}
18491817

18501818
static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
@@ -2137,39 +2105,44 @@ static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
21372105

21382106
nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
21392107
if (nlh == NULL)
2140-
goto nlmsg_failure;
2108+
goto nla_put_failure;
21412109

21422110
p = nlmsg_data(nlh);
21432111
if (c->event == XFRM_MSG_DELSA) {
2112+
struct nlattr *attr;
2113+
21442114
id = nlmsg_data(nlh);
21452115
memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
21462116
id->spi = x->id.spi;
21472117
id->family = x->props.family;
21482118
id->proto = x->id.proto;
21492119

2150-
p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
2120+
attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2121+
if (attr == NULL)
2122+
goto nla_put_failure;
2123+
2124+
p = nla_data(attr);
21512125
}
21522126

21532127
copy_to_user_state(x, p);
21542128

21552129
if (x->aalg)
2156-
RTA_PUT(skb, XFRMA_ALG_AUTH,
2130+
NLA_PUT(skb, XFRMA_ALG_AUTH,
21572131
sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
21582132
if (x->ealg)
2159-
RTA_PUT(skb, XFRMA_ALG_CRYPT,
2133+
NLA_PUT(skb, XFRMA_ALG_CRYPT,
21602134
sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
21612135
if (x->calg)
2162-
RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
2136+
NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
21632137

21642138
if (x->encap)
2165-
RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
2139+
NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
21662140

21672141
nlmsg_end(skb, nlh);
21682142

21692143
return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
21702144

2171-
nlmsg_failure:
2172-
rtattr_failure:
2145+
nla_put_failure:
21732146
kfree_skb(skb);
21742147
return -1;
21752148
}
@@ -2392,6 +2365,8 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
23922365

23932366
p = nlmsg_data(nlh);
23942367
if (c->event == XFRM_MSG_DELPOLICY) {
2368+
struct nlattr *attr;
2369+
23952370
id = nlmsg_data(nlh);
23962371
memset(id, 0, sizeof(*id));
23972372
id->dir = dir;
@@ -2400,7 +2375,11 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
24002375
else
24012376
memcpy(&id->sel, &xp->selector, sizeof(id->sel));
24022377

2403-
p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
2378+
attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2379+
if (attr == NULL)
2380+
goto nlmsg_failure;
2381+
2382+
p = nla_data(attr);
24042383
}
24052384

24062385
copy_to_user_policy(xp, p, dir);
@@ -2414,7 +2393,6 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
24142393
return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
24152394

24162395
nlmsg_failure:
2417-
rtattr_failure:
24182396
kfree_skb(skb);
24192397
return -1;
24202398
}
@@ -2483,11 +2461,11 @@ static int build_report(struct sk_buff *skb, u8 proto,
24832461
memcpy(&ur->sel, sel, sizeof(ur->sel));
24842462

24852463
if (addr)
2486-
RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2464+
NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
24872465

24882466
return nlmsg_end(skb, nlh);
24892467

2490-
rtattr_failure:
2468+
nla_put_failure:
24912469
nlmsg_cancel(skb, nlh);
24922470
return -EMSGSIZE;
24932471
}

0 commit comments

Comments
 (0)