Skip to content

Commit 6b60978

Browse files
tgrafdavem330
authored andcommitted
decnet: Do not use RTA_PUT() macros
Also, no need to trim on nlmsg_put() failure, nothing has been added yet. We also want to use nlmsg_end(), nlmsg_new() and nlmsg_free(). Signed-off-by: Thomas Graf <tgraf@suug.ch> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 74a0bd7 commit 6b60978

2 files changed

Lines changed: 79 additions & 51 deletions

File tree

‎net/decnet/dn_route.c‎

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,56 +1515,68 @@ static int dn_rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
15151515
struct dn_route *rt = (struct dn_route *)skb_dst(skb);
15161516
struct rtmsg *r;
15171517
struct nlmsghdr *nlh;
1518-
unsigned char *b = skb_tail_pointer(skb);
15191518
long expires;
15201519

15211520
nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags);
15221521
if (!nlh)
1523-
goto out_nlmsg_trim;
1522+
return -EMSGSIZE;
1523+
15241524
r = nlmsg_data(nlh);
15251525
r->rtm_family = AF_DECnet;
15261526
r->rtm_dst_len = 16;
15271527
r->rtm_src_len = 0;
15281528
r->rtm_tos = 0;
15291529
r->rtm_table = RT_TABLE_MAIN;
1530-
RTA_PUT_U32(skb, RTA_TABLE, RT_TABLE_MAIN);
15311530
r->rtm_type = rt->rt_type;
15321531
r->rtm_flags = (rt->rt_flags & ~0xFFFF) | RTM_F_CLONED;
15331532
r->rtm_scope = RT_SCOPE_UNIVERSE;
15341533
r->rtm_protocol = RTPROT_UNSPEC;
1534+
15351535
if (rt->rt_flags & RTCF_NOTIFY)
15361536
r->rtm_flags |= RTM_F_NOTIFY;
1537-
RTA_PUT(skb, RTA_DST, 2, &rt->rt_daddr);
1537+
1538+
if (nla_put_u32(skb, RTA_TABLE, RT_TABLE_MAIN) < 0 ||
1539+
nla_put_le16(skb, RTA_DST, rt->rt_daddr) < 0)
1540+
goto errout;
1541+
15381542
if (rt->fld.saddr) {
15391543
r->rtm_src_len = 16;
1540-
RTA_PUT(skb, RTA_SRC, 2, &rt->fld.saddr);
1544+
if (nla_put_le16(skb, RTA_SRC, rt->fld.saddr) < 0)
1545+
goto errout;
15411546
}
1542-
if (rt->dst.dev)
1543-
RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->dst.dev->ifindex);
1547+
if (rt->dst.dev &&
1548+
nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex) < 0)
1549+
goto errout;
1550+
15441551
/*
15451552
* Note to self - change this if input routes reverse direction when
15461553
* they deal only with inputs and not with replies like they do
15471554
* currently.
15481555
*/
1549-
RTA_PUT(skb, RTA_PREFSRC, 2, &rt->rt_local_src);
1550-
if (rt->rt_daddr != rt->rt_gateway)
1551-
RTA_PUT(skb, RTA_GATEWAY, 2, &rt->rt_gateway);
1556+
if (nla_put_le16(skb, RTA_PREFSRC, rt->rt_local_src) < 0)
1557+
goto errout;
1558+
1559+
if (rt->rt_daddr != rt->rt_gateway &&
1560+
nla_put_le16(skb, RTA_GATEWAY, rt->rt_gateway) < 0)
1561+
goto errout;
1562+
15521563
if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
1553-
goto rtattr_failure;
1564+
goto errout;
1565+
15541566
expires = rt->dst.expires ? rt->dst.expires - jiffies : 0;
15551567
if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, expires,
15561568
rt->dst.error) < 0)
1557-
goto rtattr_failure;
1558-
if (dn_is_input_route(rt))
1559-
RTA_PUT(skb, RTA_IIF, sizeof(int), &rt->fld.flowidn_iif);
1569+
goto errout;
15601570

1561-
nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1562-
return skb->len;
1571+
if (dn_is_input_route(rt) &&
1572+
nla_put_u32(skb, RTA_IIF, rt->fld.flowidn_iif) < 0)
1573+
goto errout;
15631574

1564-
out_nlmsg_trim:
1565-
rtattr_failure:
1566-
nlmsg_trim(skb, b);
1567-
return -1;
1575+
return nlmsg_end(skb, nlh);
1576+
1577+
errout:
1578+
nlmsg_cancel(skb, nlh);
1579+
return -EMSGSIZE;
15681580
}
15691581

15701582
/*
@@ -1587,7 +1599,7 @@ static int dn_cache_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, void
15871599
memset(&fld, 0, sizeof(fld));
15881600
fld.flowidn_proto = DNPROTO_NSP;
15891601

1590-
skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1602+
skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15911603
if (skb == NULL)
15921604
return -ENOBUFS;
15931605
skb_reset_mac_header(skb);
@@ -1665,13 +1677,16 @@ int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb)
16651677
struct dn_route *rt;
16661678
int h, s_h;
16671679
int idx, s_idx;
1680+
struct rtmsg *rtm;
16681681

16691682
if (!net_eq(net, &init_net))
16701683
return 0;
16711684

1672-
if (NLMSG_PAYLOAD(cb->nlh, 0) < sizeof(struct rtmsg))
1685+
if (nlmsg_len(cb->nlh) < sizeof(struct rtmsg))
16731686
return -EINVAL;
1674-
if (!(((struct rtmsg *)nlmsg_data(cb->nlh))->rtm_flags&RTM_F_CLONED))
1687+
1688+
rtm = nlmsg_data(cb->nlh);
1689+
if (!(rtm->rtm_flags & RTM_F_CLONED))
16751690
return 0;
16761691

16771692
s_h = cb->args[0];

‎net/decnet/dn_table.c‎

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -297,62 +297,75 @@ static int dn_fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
297297
{
298298
struct rtmsg *rtm;
299299
struct nlmsghdr *nlh;
300-
unsigned char *b = skb_tail_pointer(skb);
301300

302301
nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
303302
if (!nlh)
304-
goto out_nlmsg_trim;
303+
return -EMSGSIZE;
304+
305305
rtm = nlmsg_data(nlh);
306306
rtm->rtm_family = AF_DECnet;
307307
rtm->rtm_dst_len = dst_len;
308308
rtm->rtm_src_len = 0;
309309
rtm->rtm_tos = 0;
310310
rtm->rtm_table = tb_id;
311-
RTA_PUT_U32(skb, RTA_TABLE, tb_id);
312311
rtm->rtm_flags = fi->fib_flags;
313312
rtm->rtm_scope = scope;
314313
rtm->rtm_type = type;
315-
if (rtm->rtm_dst_len)
316-
RTA_PUT(skb, RTA_DST, 2, dst);
317314
rtm->rtm_protocol = fi->fib_protocol;
318-
if (fi->fib_priority)
319-
RTA_PUT(skb, RTA_PRIORITY, 4, &fi->fib_priority);
315+
316+
if (nla_put_u32(skb, RTA_TABLE, tb_id) < 0)
317+
goto errout;
318+
319+
if (rtm->rtm_dst_len &&
320+
nla_put(skb, RTA_DST, 2, dst) < 0)
321+
goto errout;
322+
323+
if (fi->fib_priority &&
324+
nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority) < 0)
325+
goto errout;
326+
320327
if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
321-
goto rtattr_failure;
328+
goto errout;
329+
322330
if (fi->fib_nhs == 1) {
323-
if (fi->fib_nh->nh_gw)
324-
RTA_PUT(skb, RTA_GATEWAY, 2, &fi->fib_nh->nh_gw);
325-
if (fi->fib_nh->nh_oif)
326-
RTA_PUT(skb, RTA_OIF, sizeof(int), &fi->fib_nh->nh_oif);
331+
if (fi->fib_nh->nh_gw &&
332+
nla_put_le16(skb, RTA_GATEWAY, fi->fib_nh->nh_gw) < 0)
333+
goto errout;
334+
335+
if (fi->fib_nh->nh_oif &&
336+
nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif) < 0)
337+
goto errout;
327338
}
339+
328340
if (fi->fib_nhs > 1) {
329341
struct rtnexthop *nhp;
330-
struct rtattr *mp_head;
331-
if (skb_tailroom(skb) <= RTA_SPACE(0))
332-
goto rtattr_failure;
333-
mp_head = (struct rtattr *)skb_put(skb, RTA_SPACE(0));
342+
struct nlattr *mp_head;
343+
344+
if (!(mp_head = nla_nest_start(skb, RTA_MULTIPATH)))
345+
goto errout;
334346

335347
for_nexthops(fi) {
336-
if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
337-
goto rtattr_failure;
338-
nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
348+
if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp))))
349+
goto errout;
350+
339351
nhp->rtnh_flags = nh->nh_flags & 0xFF;
340352
nhp->rtnh_hops = nh->nh_weight - 1;
341353
nhp->rtnh_ifindex = nh->nh_oif;
342-
if (nh->nh_gw)
343-
RTA_PUT(skb, RTA_GATEWAY, 2, &nh->nh_gw);
354+
355+
if (nh->nh_gw &&
356+
nla_put_le16(skb, RTA_GATEWAY, nh->nh_gw) < 0)
357+
goto errout;
358+
344359
nhp->rtnh_len = skb_tail_pointer(skb) - (unsigned char *)nhp;
345360
} endfor_nexthops(fi);
346-
mp_head->rta_type = RTA_MULTIPATH;
347-
mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
361+
362+
nla_nest_end(skb, mp_head);
348363
}
349364

350-
nlh->nlmsg_len = skb_tail_pointer(skb) - b;
351-
return skb->len;
365+
return nlmsg_end(skb, nlh);
352366

353-
out_nlmsg_trim:
354-
rtattr_failure:
355-
nlmsg_trim(skb, b);
367+
errout:
368+
nlmsg_cancel(skb, nlh);
356369
return -EMSGSIZE;
357370
}
358371

0 commit comments

Comments
 (0)