1
0

302-netlink-alignment.patch 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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
  5. An padding to align a message should not only be added between
  6. different attributes of a netlink message, but also at the end of the
  7. message to pad it to the correct size.
  8. Without this patch the following command does not work and returns an
  9. error code:
  10. ip link add type nlmon
  11. Without this ip from busybox sends this:
  12. 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
  13. return value: 2
  14. The normal ip utile from iproute2 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=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
  16. return value: 0
  17. With this patch ip from busybox sends this:
  18. 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
  19. return value: 0
  20. Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
  21. ---
  22. networking/libiproute/libnetlink.c | 16 ++++++++--------
  23. 1 file changed, 8 insertions(+), 8 deletions(-)
  24. --- a/networking/libiproute/libnetlink.c
  25. +++ b/networking/libiproute/libnetlink.c
  26. @@ -338,14 +338,14 @@ int FAST_FUNC addattr32(struct nlmsghdr
  27. int len = RTA_LENGTH(4);
  28. struct rtattr *rta;
  29. - if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
  30. + if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) {
  31. return -1;
  32. }
  33. rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
  34. rta->rta_type = type;
  35. rta->rta_len = len;
  36. move_to_unaligned32(RTA_DATA(rta), data);
  37. - n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
  38. + n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len);
  39. return 0;
  40. }
  41. @@ -354,14 +354,14 @@ int FAST_FUNC addattr_l(struct nlmsghdr
  42. int len = RTA_LENGTH(alen);
  43. struct rtattr *rta;
  44. - if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
  45. + if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) {
  46. return -1;
  47. }
  48. rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
  49. rta->rta_type = type;
  50. rta->rta_len = len;
  51. memcpy(RTA_DATA(rta), data, alen);
  52. - n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
  53. + n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len);
  54. return 0;
  55. }
  56. @@ -370,14 +370,14 @@ int FAST_FUNC rta_addattr32(struct rtatt
  57. int len = RTA_LENGTH(4);
  58. struct rtattr *subrta;
  59. - if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
  60. + if (RTA_ALIGN(rta->rta_len + len) > maxlen) {
  61. return -1;
  62. }
  63. subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
  64. subrta->rta_type = type;
  65. subrta->rta_len = len;
  66. move_to_unaligned32(RTA_DATA(subrta), data);
  67. - rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
  68. + rta->rta_len = NLMSG_ALIGN(rta->rta_len + len);
  69. return 0;
  70. }
  71. @@ -386,14 +386,14 @@ int FAST_FUNC rta_addattr_l(struct rtatt
  72. struct rtattr *subrta;
  73. int len = RTA_LENGTH(alen);
  74. - if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
  75. + if (RTA_ALIGN(rta->rta_len + len) > maxlen) {
  76. return -1;
  77. }
  78. subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
  79. subrta->rta_type = type;
  80. subrta->rta_len = len;
  81. memcpy(RTA_DATA(subrta), data, alen);
  82. - rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
  83. + rta->rta_len = NLMSG_ALIGN(rta->rta_len + len);
  84. return 0;
  85. }