network/config/xfrm: add host-dependency for xfrm interface parent
[openwrt/staging/rmilecki.git] / package / utils / busybox / patches / 530-ip-use-rtnl_send_check-on-flush-commands.patch
1 From 028c5aa18b5273c029f0278232d922ee1a164de6 Mon Sep 17 00:00:00 2001
2 From: Denys Vlasenko <vda.linux@googlemail.com>
3 Date: Wed, 22 May 2019 13:54:46 +0200
4 Subject: ip: use rtnl_send_check() on flush commands, closes 6962
5
6 function old new delta
7 rtnl_send_check - 160 +160
8 xrtnl_wilddump_request 64 66 +2
9 ipneigh_list_or_flush 714 706 -8
10 rtnl_send 69 - -69
11 ------------------------------------------------------------------------------
12 (add/remove: 1/1 grow/shrink: 1/1 up/down: 162/-77) Total: 85 bytes
13
14 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
15 ---
16 networking/libiproute/ipaddress.c | 6 ++++--
17 networking/libiproute/ipneigh.c | 9 ++++----
18 networking/libiproute/iproute.c | 5 ++++-
19 networking/libiproute/libnetlink.c | 43 +++++++++++++++++++++++++++++++-------
20 networking/libiproute/libnetlink.h | 19 +++++++++++++++--
21 5 files changed, 65 insertions(+), 17 deletions(-)
22
23 --- a/networking/libiproute/ipaddress.c
24 +++ b/networking/libiproute/ipaddress.c
25 @@ -23,6 +23,7 @@
26
27 struct filter_t {
28 char *label;
29 + /* Flush cmd buf. If !NULL, print_addrinfo() constructs flush commands in it */
30 char *flushb;
31 struct rtnl_handle *rth;
32 int scope, scopemask;
33 @@ -34,6 +35,8 @@ struct filter_t {
34 smallint showqueue;
35 smallint oneline;
36 smallint up;
37 + /* Misnomer. Does not mean "flushed something" */
38 + /* More like "flush commands were constructed by print_addrinfo()" */
39 smallint flushed;
40 inet_prefix pfx;
41 } FIX_ALIASING;
42 @@ -201,7 +204,7 @@ static NOINLINE int print_linkinfo(const
43
44 static int flush_update(void)
45 {
46 - if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
47 + if (rtnl_send_check(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
48 bb_perror_msg("can't send flush request");
49 return -1;
50 }
51 @@ -507,7 +510,6 @@ int FAST_FUNC ipaddr_list_or_flush(char
52 xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
53 }
54
55 -
56 if (G_filter.family && G_filter.family != AF_PACKET) {
57 struct nlmsg_list **lp;
58 lp = &linfo;
59 --- a/networking/libiproute/ipneigh.c
60 +++ b/networking/libiproute/ipneigh.c
61 @@ -32,7 +32,10 @@ struct filter_t {
62 int state;
63 int unused_only;
64 inet_prefix pfx;
65 + /* Misnomer. Does not mean "flushed N something" */
66 + /* More like "no_of_flush_commands_constructed_by_print_neigh()" */
67 int flushed;
68 + /* Flush cmd buf. If !NULL, print_neigh() constructs flush commands in it */
69 char *flushb;
70 int flushp;
71 int flushe;
72 @@ -45,7 +48,7 @@ typedef struct filter_t filter_t;
73
74 static int flush_update(void)
75 {
76 - if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
77 + if (rtnl_send_check(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
78 bb_perror_msg("can't send flush request");
79 return -1;
80 }
81 @@ -299,9 +302,7 @@ static int FAST_FUNC ipneigh_list_or_flu
82 G_filter.rth = &rth;
83
84 while (round < MAX_ROUNDS) {
85 - if (xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETNEIGH) < 0) {
86 - bb_perror_msg_and_die("can't send dump request");
87 - }
88 + xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETNEIGH);
89 G_filter.flushed = 0;
90 if (xrtnl_dump_filter(&rth, print_neigh, NULL) < 0) {
91 bb_perror_msg_and_die("flush terminated");
92 --- a/networking/libiproute/iproute.c
93 +++ b/networking/libiproute/iproute.c
94 @@ -26,7 +26,10 @@
95
96 struct filter_t {
97 int tb;
98 + /* Misnomer. Does not mean "flushed something" */
99 + /* More like "flush commands were constructed by print_route()" */
100 smallint flushed;
101 + /* Flush cmd buf. If !NULL, print_route() constructs flush commands in it */
102 char *flushb;
103 int flushp;
104 int flushe;
105 @@ -53,7 +56,7 @@ typedef struct filter_t filter_t;
106
107 static int flush_update(void)
108 {
109 - if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
110 + if (rtnl_send_check(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
111 bb_perror_msg("can't send flush request");
112 return -1;
113 }
114 --- a/networking/libiproute/libnetlink.c
115 +++ b/networking/libiproute/libnetlink.c
116 @@ -34,7 +34,7 @@ void FAST_FUNC xrtnl_open(struct rtnl_ha
117 rth->seq = time(NULL);
118 }
119
120 -int FAST_FUNC xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
121 +void FAST_FUNC xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
122 {
123 struct {
124 struct nlmsghdr nlh;
125 @@ -48,18 +48,45 @@ int FAST_FUNC xrtnl_wilddump_request(str
126 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
127 req.g.rtgen_family = family;
128
129 - return rtnl_send(rth, (void*)&req, sizeof(req));
130 + rtnl_send(rth, (void*)&req, sizeof(req));
131 }
132
133 -//TODO: pass rth->fd instead of full rth?
134 -int FAST_FUNC rtnl_send(struct rtnl_handle *rth, char *buf, int len)
135 +/* A version which checks for e.g. EPERM errors.
136 + * Try: setuidgid 1:1 ip addr flush dev eth0
137 + */
138 +int FAST_FUNC rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
139 {
140 - struct sockaddr_nl nladdr;
141 + struct nlmsghdr *h;
142 + int status;
143 + char resp[1024];
144 +
145 + status = write(rth->fd, buf, len);
146 + if (status < 0)
147 + return status;
148 +
149 + /* Check for immediate errors */
150 + status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
151 + if (status < 0) {
152 + if (errno == EAGAIN) /* if no error, this happens */
153 + return 0;
154 + return -1;
155 + }
156 +
157 + for (h = (struct nlmsghdr *)resp;
158 + NLMSG_OK(h, status);
159 + h = NLMSG_NEXT(h, status)
160 + ) {
161 + if (h->nlmsg_type == NLMSG_ERROR) {
162 + struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
163 + if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
164 + bb_error_msg("ERROR truncated");
165 + else
166 + errno = -err->error;
167 + return -1;
168 + }
169 + }
170
171 - memset(&nladdr, 0, sizeof(nladdr));
172 - nladdr.nl_family = AF_NETLINK;
173 -
174 - return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
175 + return 0;
176 }
177
178 int FAST_FUNC rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
179 --- a/networking/libiproute/libnetlink.h
180 +++ b/networking/libiproute/libnetlink.h
181 @@ -20,7 +20,7 @@ struct rtnl_handle {
182
183 extern void xrtnl_open(struct rtnl_handle *rth) FAST_FUNC;
184 #define rtnl_close(rth) (close((rth)->fd))
185 -extern int xrtnl_wilddump_request(struct rtnl_handle *rth, int fam, int type) FAST_FUNC;
186 +extern void xrtnl_wilddump_request(struct rtnl_handle *rth, int fam, int type) FAST_FUNC;
187 extern int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len) FAST_FUNC;
188 extern int xrtnl_dump_filter(struct rtnl_handle *rth,
189 int (*filter)(const struct sockaddr_nl*, struct nlmsghdr *n, void*) FAST_FUNC,
190 @@ -34,8 +34,23 @@ extern int rtnl_talk(struct rtnl_handle
191 int (*junk)(struct sockaddr_nl *,struct nlmsghdr *n, void *),
192 void *jarg) FAST_FUNC;
193
194 -extern int rtnl_send(struct rtnl_handle *rth, char *buf, int) FAST_FUNC;
195 +int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len) FAST_FUNC;
196 +//TODO: pass rth->fd instead of full rth?
197 +static ALWAYS_INLINE void rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
198 +{
199 + // Used to be:
200 + //struct sockaddr_nl nladdr;
201 + //memset(&nladdr, 0, sizeof(nladdr));
202 + //nladdr.nl_family = AF_NETLINK;
203 + //return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
204
205 + // iproute2-4.2.0 simplified the above to:
206 + //return send(rth->fd, buf, len, 0);
207 +
208 + // We are using even shorter:
209 + xwrite(rth->fd, buf, len);
210 + // and convert to void, inline.
211 +}
212
213 extern int addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data) FAST_FUNC;
214 extern int addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen) FAST_FUNC;