1 From a843f09a4d4428cf11ca02307e60058251b05743 Mon Sep 17 00:00:00 2001
2 From: Hauke Mehrtens <hauke@hauke-m.de>
3 Date: Fri, 16 Sep 2016 21:52:03 +0200
4 Subject: [PATCH] libnetlink: fix alignment of netlink messages
6 An padding to align a message should not only be added between
7 different attributes of a netlink message, but also at the end of the
8 message to pad it to the correct size.
10 Without this patch the following command does not work and returns an
12 ip link add type nlmon
14 Without this ip from busybox sends this:
15 sendmsg(3, {msg_name={sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, msg_namelen=12, msg_iov=[{iov_base={{len=45, type=0x10 /* NLMSG_??? */, flags=NLM_F_REQUEST|NLM_F_ACK|0x600, seq=1474057401, pid=0}, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\20\0\22\0\t\0\1nlmon"}, iov_len=45}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 45
18 The normal ip utile from iproute2 sends this:
19 sendmsg(3, {msg_name={sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, msg_namelen=12, msg_iov=[{iov_base={{len=48, type=0x10 /* NLMSG_??? */, flags=NLM_F_REQUEST|NLM_F_ACK|0x600, seq=1473716938, pid=0}, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\20\0\22\0\t\0\1nlmon\0\0\0"}, iov_len=48}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 48
22 With this patch ip from busybox sends this:
23 sendmsg(3, {msg_name={sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, msg_namelen=12, msg_iov=[{iov_base={{len=48, type=0x10 /* NLMSG_??? */, flags=NLM_F_REQUEST|NLM_F_ACK|0x600, seq=1473716908, pid=0}, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\20\0\22\0\t\0\1nlmon\0\0\0"}, iov_len=48}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 48
26 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
28 networking/libiproute/libnetlink.c | 16 ++++++++--------
29 1 file changed, 8 insertions(+), 8 deletions(-)
31 --- a/networking/libiproute/libnetlink.c
32 +++ b/networking/libiproute/libnetlink.c
33 @@ -338,14 +338,14 @@ int FAST_FUNC addattr32(struct nlmsghdr
34 int len = RTA_LENGTH(4);
37 - if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
38 + if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) {
41 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
44 move_to_unaligned32(RTA_DATA(rta), data);
45 - n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
46 + n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len);
50 @@ -354,14 +354,14 @@ int FAST_FUNC addattr_l(struct nlmsghdr
51 int len = RTA_LENGTH(alen);
54 - if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
55 + if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) {
58 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
61 memcpy(RTA_DATA(rta), data, alen);
62 - n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
63 + n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len);
67 @@ -370,14 +370,14 @@ int FAST_FUNC rta_addattr32(struct rtatt
68 int len = RTA_LENGTH(4);
69 struct rtattr *subrta;
71 - if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
72 + if (RTA_ALIGN(rta->rta_len + len) > maxlen) {
75 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
76 subrta->rta_type = type;
77 subrta->rta_len = len;
78 move_to_unaligned32(RTA_DATA(subrta), data);
79 - rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
80 + rta->rta_len = NLMSG_ALIGN(rta->rta_len + len);
84 @@ -386,14 +386,14 @@ int FAST_FUNC rta_addattr_l(struct rtatt
85 struct rtattr *subrta;
86 int len = RTA_LENGTH(alen);
88 - if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
89 + if (RTA_ALIGN(rta->rta_len + len) > maxlen) {
92 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
93 subrta->rta_type = type;
94 subrta->rta_len = len;
95 memcpy(RTA_DATA(subrta), data, alen);
96 - rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
97 + rta->rta_len = NLMSG_ALIGN(rta->rta_len + len);