Skip to content

Commit 4b3550e

Browse files
kaberdavem330
authored andcommitted
[NET_SCHED]: Use nla_nest_start/nla_nest_end
Use nla_nest_start/nla_nest_end for dumping nested attributes. Signed-off-by: Patrick McHardy <kaber@trash.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent cee6372 commit 4b3550e

16 files changed

Lines changed: 166 additions & 133 deletions

‎net/sched/act_api.c‎

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
6969
{
7070
struct tcf_common *p;
7171
int err = 0, index = -1,i = 0, s_i = 0, n_i = 0;
72-
struct nlattr *r ;
72+
struct nlattr *nest;
7373

7474
read_lock_bh(hinfo->lock);
7575

@@ -84,15 +84,17 @@ static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
8484
continue;
8585
a->priv = p;
8686
a->order = n_i;
87-
r = (struct nlattr *)skb_tail_pointer(skb);
88-
NLA_PUT(skb, a->order, 0, NULL);
87+
88+
nest = nla_nest_start(skb, a->order);
89+
if (nest == NULL)
90+
goto nla_put_failure;
8991
err = tcf_action_dump_1(skb, a, 0, 0);
9092
if (err < 0) {
9193
index--;
92-
nlmsg_trim(skb, r);
94+
nlmsg_trim(skb, nest);
9395
goto done;
9496
}
95-
r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
97+
nla_nest_end(skb, nest);
9698
n_i++;
9799
if (n_i >= TCA_ACT_MAX_PRIO)
98100
goto done;
@@ -105,19 +107,20 @@ static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
105107
return n_i;
106108

107109
nla_put_failure:
108-
nlmsg_trim(skb, r);
110+
nla_nest_cancel(skb, nest);
109111
goto done;
110112
}
111113

112114
static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
113115
struct tcf_hashinfo *hinfo)
114116
{
115117
struct tcf_common *p, *s_p;
116-
struct nlattr *r ;
118+
struct nlattr *nest;
117119
int i= 0, n_i = 0;
118120

119-
r = (struct nlattr *)skb_tail_pointer(skb);
120-
NLA_PUT(skb, a->order, 0, NULL);
121+
nest = nla_nest_start(skb, a->order);
122+
if (nest == NULL)
123+
goto nla_put_failure;
121124
NLA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
122125
for (i = 0; i < (hinfo->hmask + 1); i++) {
123126
p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
@@ -131,11 +134,11 @@ static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
131134
}
132135
}
133136
NLA_PUT(skb, TCA_FCNT, 4, &n_i);
134-
r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
137+
nla_nest_end(skb, nest);
135138

136139
return n_i;
137140
nla_put_failure:
138-
nlmsg_trim(skb, r);
141+
nla_nest_cancel(skb, nest);
139142
return -EINVAL;
140143
}
141144

@@ -415,18 +418,19 @@ tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
415418
{
416419
int err = -EINVAL;
417420
unsigned char *b = skb_tail_pointer(skb);
418-
struct nlattr *r;
421+
struct nlattr *nest;
419422

420423
if (a->ops == NULL || a->ops->dump == NULL)
421424
return err;
422425

423426
NLA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
424427
if (tcf_action_copy_stats(skb, a, 0))
425428
goto nla_put_failure;
426-
r = (struct nlattr *)skb_tail_pointer(skb);
427-
NLA_PUT(skb, TCA_OPTIONS, 0, NULL);
429+
nest = nla_nest_start(skb, TCA_OPTIONS);
430+
if (nest == NULL)
431+
goto nla_put_failure;
428432
if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
429-
r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
433+
nla_nest_end(skb, nest);
430434
return err;
431435
}
432436

@@ -441,25 +445,25 @@ tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
441445
{
442446
struct tc_action *a;
443447
int err = -EINVAL;
444-
unsigned char *b = skb_tail_pointer(skb);
445-
struct nlattr *r ;
448+
struct nlattr *nest;
446449

447450
while ((a = act) != NULL) {
448-
r = (struct nlattr *)skb_tail_pointer(skb);
449451
act = a->next;
450-
NLA_PUT(skb, a->order, 0, NULL);
452+
nest = nla_nest_start(skb, a->order);
453+
if (nest == NULL)
454+
goto nla_put_failure;
451455
err = tcf_action_dump_1(skb, a, bind, ref);
452456
if (err < 0)
453457
goto errout;
454-
r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
458+
nla_nest_end(skb, nest);
455459
}
456460

457461
return 0;
458462

459463
nla_put_failure:
460464
err = -EINVAL;
461465
errout:
462-
nlmsg_trim(skb, b);
466+
nla_nest_cancel(skb, nest);
463467
return err;
464468
}
465469

@@ -627,7 +631,7 @@ tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
627631
struct tcamsg *t;
628632
struct nlmsghdr *nlh;
629633
unsigned char *b = skb_tail_pointer(skb);
630-
struct nlattr *x;
634+
struct nlattr *nest;
631635

632636
nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
633637

@@ -636,13 +640,14 @@ tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
636640
t->tca__pad1 = 0;
637641
t->tca__pad2 = 0;
638642

639-
x = (struct nlattr *)skb_tail_pointer(skb);
640-
NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
643+
nest = nla_nest_start(skb, TCA_ACT_TAB);
644+
if (nest == NULL)
645+
goto nla_put_failure;
641646

642647
if (tcf_action_dump(skb, a, bind, ref) < 0)
643648
goto nla_put_failure;
644649

645-
x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
650+
nla_nest_end(skb, nest);
646651

647652
nlh->nlmsg_len = skb_tail_pointer(skb) - b;
648653
return skb->len;
@@ -743,7 +748,7 @@ static int tca_action_flush(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
743748
struct nlmsghdr *nlh;
744749
struct tcamsg *t;
745750
struct netlink_callback dcb;
746-
struct nlattr *x;
751+
struct nlattr *nest;
747752
struct nlattr *tb[TCA_ACT_MAX+1];
748753
struct nlattr *kind;
749754
struct tc_action *a = create_a(0);
@@ -779,14 +784,15 @@ static int tca_action_flush(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
779784
t->tca__pad1 = 0;
780785
t->tca__pad2 = 0;
781786

782-
x = (struct nlattr *)skb_tail_pointer(skb);
783-
NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
787+
nest = nla_nest_start(skb, TCA_ACT_TAB);
788+
if (nest == NULL)
789+
goto nla_put_failure;
784790

785791
err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
786792
if (err < 0)
787793
goto nla_put_failure;
788794

789-
x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
795+
nla_nest_end(skb, nest);
790796

791797
nlh->nlmsg_len = skb_tail_pointer(skb) - b;
792798
nlh->nlmsg_flags |= NLM_F_ROOT;
@@ -875,7 +881,7 @@ static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
875881
struct tcamsg *t;
876882
struct nlmsghdr *nlh;
877883
struct sk_buff *skb;
878-
struct nlattr *x;
884+
struct nlattr *nest;
879885
unsigned char *b;
880886
int err = 0;
881887

@@ -891,13 +897,14 @@ static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
891897
t->tca__pad1 = 0;
892898
t->tca__pad2 = 0;
893899

894-
x = (struct nlattr *)skb_tail_pointer(skb);
895-
NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
900+
nest = nla_nest_start(skb, TCA_ACT_TAB);
901+
if (nest == NULL)
902+
goto nla_put_failure;
896903

897904
if (tcf_action_dump(skb, a, 0, 0) < 0)
898905
goto nla_put_failure;
899906

900-
x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
907+
nla_nest_end(skb, nest);
901908

902909
nlh->nlmsg_len = skb_tail_pointer(skb) - b;
903910
NETLINK_CB(skb).dst_group = RTNLGRP_TC;
@@ -1025,7 +1032,7 @@ tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
10251032
struct net *net = skb->sk->sk_net;
10261033
struct nlmsghdr *nlh;
10271034
unsigned char *b = skb_tail_pointer(skb);
1028-
struct nlattr *x;
1035+
struct nlattr *nest;
10291036
struct tc_action_ops *a_o;
10301037
struct tc_action a;
10311038
int ret = 0;
@@ -1060,18 +1067,19 @@ tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
10601067
t->tca__pad1 = 0;
10611068
t->tca__pad2 = 0;
10621069

1063-
x = (struct nlattr *)skb_tail_pointer(skb);
1064-
NLA_PUT(skb, TCA_ACT_TAB, 0, NULL);
1070+
nest = nla_nest_start(skb, TCA_ACT_TAB);
1071+
if (nest == NULL)
1072+
goto nla_put_failure;
10651073

10661074
ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
10671075
if (ret < 0)
10681076
goto nla_put_failure;
10691077

10701078
if (ret > 0) {
1071-
x->nla_len = skb_tail_pointer(skb) - (u8 *)x;
1079+
nla_nest_end(skb, nest);
10721080
ret = skb->len;
10731081
} else
1074-
nlmsg_trim(skb, x);
1082+
nla_nest_cancel(skb, nest);
10751083

10761084
nlh->nlmsg_len = skb_tail_pointer(skb) - b;
10771085
if (NETLINK_CB(cb->skb).pid && ret)

‎net/sched/act_police.c‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *c
5454
{
5555
struct tcf_common *p;
5656
int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
57-
struct nlattr *r;
57+
struct nlattr *nest;
5858

5959
read_lock_bh(&police_lock);
6060

@@ -69,18 +69,19 @@ static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *c
6969
continue;
7070
a->priv = p;
7171
a->order = index;
72-
r = (struct nlattr *)skb_tail_pointer(skb);
73-
NLA_PUT(skb, a->order, 0, NULL);
72+
nest = nla_nest_start(skb, a->order);
73+
if (nest == NULL)
74+
goto nla_put_failure;
7475
if (type == RTM_DELACTION)
7576
err = tcf_action_dump_1(skb, a, 0, 1);
7677
else
7778
err = tcf_action_dump_1(skb, a, 0, 0);
7879
if (err < 0) {
7980
index--;
80-
nlmsg_trim(skb, r);
81+
nla_nest_cancel(skb, nest);
8182
goto done;
8283
}
83-
r->nla_len = skb_tail_pointer(skb) - (u8 *)r;
84+
nla_nest_end(skb, nest);
8485
n_i++;
8586
}
8687
}
@@ -91,7 +92,7 @@ static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *c
9192
return n_i;
9293

9394
nla_put_failure:
94-
nlmsg_trim(skb, r);
95+
nla_nest_cancel(skb, nest);
9596
goto done;
9697
}
9798

‎net/sched/cls_api.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,18 +544,22 @@ int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
544544
* to work with both old and new modes of entering
545545
* tc data even if iproute2 was newer - jhs
546546
*/
547-
struct nlattr *p_rta = (struct nlattr *)skb_tail_pointer(skb);
547+
struct nlattr *nest;
548548

549549
if (exts->action->type != TCA_OLD_COMPAT) {
550-
NLA_PUT(skb, map->action, 0, NULL);
550+
nest = nla_nest_start(skb, map->action);
551+
if (nest == NULL)
552+
goto nla_put_failure;
551553
if (tcf_action_dump(skb, exts->action, 0, 0) < 0)
552554
goto nla_put_failure;
553-
p_rta->nla_len = skb_tail_pointer(skb) - (u8 *)p_rta;
555+
nla_nest_end(skb, nest);
554556
} else if (map->police) {
555-
NLA_PUT(skb, map->police, 0, NULL);
557+
nest = nla_nest_start(skb, map->police);
558+
if (nest == NULL)
559+
goto nla_put_failure;
556560
if (tcf_action_dump_old(skb, exts->action, 0, 0) < 0)
557561
goto nla_put_failure;
558-
p_rta->nla_len = skb_tail_pointer(skb) - (u8 *)p_rta;
562+
nla_nest_end(skb, nest);
559563
}
560564
}
561565
#endif

‎net/sched/cls_basic.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,16 @@ static int basic_dump(struct tcf_proto *tp, unsigned long fh,
246246
struct sk_buff *skb, struct tcmsg *t)
247247
{
248248
struct basic_filter *f = (struct basic_filter *) fh;
249-
unsigned char *b = skb_tail_pointer(skb);
250-
struct nlattr *nla;
249+
struct nlattr *nest;
251250

252251
if (f == NULL)
253252
return skb->len;
254253

255254
t->tcm_handle = f->handle;
256255

257-
nla = (struct nlattr *) b;
258-
NLA_PUT(skb, TCA_OPTIONS, 0, NULL);
256+
nest = nla_nest_start(skb, TCA_OPTIONS);
257+
if (nest == NULL)
258+
goto nla_put_failure;
259259

260260
if (f->res.classid)
261261
NLA_PUT(skb, TCA_BASIC_CLASSID, sizeof(u32), &f->res.classid);
@@ -264,11 +264,11 @@ static int basic_dump(struct tcf_proto *tp, unsigned long fh,
264264
tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
265265
goto nla_put_failure;
266266

267-
nla->nla_len = skb_tail_pointer(skb) - b;
267+
nla_nest_end(skb, nest);
268268
return skb->len;
269269

270270
nla_put_failure:
271-
nlmsg_trim(skb, b);
271+
nla_nest_cancel(skb, nest);
272272
return -1;
273273
}
274274

‎net/sched/cls_fw.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ static int fw_dump(struct tcf_proto *tp, unsigned long fh,
334334
struct fw_head *head = (struct fw_head *)tp->root;
335335
struct fw_filter *f = (struct fw_filter*)fh;
336336
unsigned char *b = skb_tail_pointer(skb);
337-
struct nlattr *nla;
337+
struct nlattr *nest;
338338

339339
if (f == NULL)
340340
return skb->len;
@@ -344,8 +344,9 @@ static int fw_dump(struct tcf_proto *tp, unsigned long fh,
344344
if (!f->res.classid && !tcf_exts_is_available(&f->exts))
345345
return skb->len;
346346

347-
nla = (struct nlattr*)b;
348-
NLA_PUT(skb, TCA_OPTIONS, 0, NULL);
347+
nest = nla_nest_start(skb, TCA_OPTIONS);
348+
if (nest == NULL)
349+
goto nla_put_failure;
349350

350351
if (f->res.classid)
351352
NLA_PUT(skb, TCA_FW_CLASSID, 4, &f->res.classid);
@@ -359,7 +360,7 @@ static int fw_dump(struct tcf_proto *tp, unsigned long fh,
359360
if (tcf_exts_dump(skb, &f->exts, &fw_ext_map) < 0)
360361
goto nla_put_failure;
361362

362-
nla->nla_len = skb_tail_pointer(skb) - b;
363+
nla_nest_end(skb, nest);
363364

364365
if (tcf_exts_dump_stats(skb, &f->exts, &fw_ext_map) < 0)
365366
goto nla_put_failure;

0 commit comments

Comments
 (0)