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
7 rtnl_send_check - 160 +160
8 xrtnl_wilddump_request 64 66 +2
9 ipneigh_list_or_flush 714 706 -8
11 ------------------------------------------------------------------------------
12 (add/remove: 1/1 grow/shrink: 1/1 up/down: 162/-77) Total: 85 bytes
14 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
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(-)
23 --- a/networking/libiproute/ipaddress.c
24 +++ b/networking/libiproute/ipaddress.c
29 + /* Flush cmd buf. If !NULL, print_addrinfo() constructs flush commands in it */
31 struct rtnl_handle *rth;
33 @@ -34,6 +35,8 @@ struct filter_t {
37 + /* Misnomer. Does not mean "flushed something" */
38 + /* More like "flush commands were constructed by print_addrinfo()" */
42 @@ -201,7 +204,7 @@ static NOINLINE int print_linkinfo(const
44 static int flush_update(void)
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");
51 @@ -507,7 +510,6 @@ int FAST_FUNC ipaddr_list_or_flush(char
52 xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
56 if (G_filter.family && G_filter.family != AF_PACKET) {
57 struct nlmsg_list **lp;
59 --- a/networking/libiproute/ipneigh.c
60 +++ b/networking/libiproute/ipneigh.c
61 @@ -32,7 +32,10 @@ struct filter_t {
65 + /* Misnomer. Does not mean "flushed N something" */
66 + /* More like "no_of_flush_commands_constructed_by_print_neigh()" */
68 + /* Flush cmd buf. If !NULL, print_neigh() constructs flush commands in it */
72 @@ -45,7 +48,7 @@ typedef struct filter_t filter_t;
74 static int flush_update(void)
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");
81 @@ -299,9 +302,7 @@ static int FAST_FUNC ipneigh_list_or_flu
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");
88 + xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETNEIGH);
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
98 + /* Misnomer. Does not mean "flushed something" */
99 + /* More like "flush commands were constructed by print_route()" */
101 + /* Flush cmd buf. If !NULL, print_route() constructs flush commands in it */
105 @@ -53,7 +56,7 @@ typedef struct filter_t filter_t;
107 static int flush_update(void)
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");
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);
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)
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;
129 - return rtnl_send(rth, (void*)&req, sizeof(req));
130 + rtnl_send(rth, (void*)&req, sizeof(req));
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
138 +int FAST_FUNC rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
140 - struct sockaddr_nl nladdr;
141 + struct nlmsghdr *h;
145 + status = write(rth->fd, buf, len);
149 + /* Check for immediate errors */
150 + status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
152 + if (errno == EAGAIN) /* if no error, this happens */
157 + for (h = (struct nlmsghdr *)resp;
158 + NLMSG_OK(h, status);
159 + h = NLMSG_NEXT(h, status)
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");
166 + errno = -err->error;
171 - memset(&nladdr, 0, sizeof(nladdr));
172 - nladdr.nl_family = AF_NETLINK;
174 - return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
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 {
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;
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)
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));
205 + // iproute2-4.2.0 simplified the above to:
206 + //return send(rth->fd, buf, len, 0);
208 + // We are using even shorter:
209 + xwrite(rth->fd, buf, len);
210 + // and convert to void, inline.
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;