Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | // SPDX-License-Identifier: GPL-2.0-or-later /* Taken & modified from iproute2's libnetlink.c * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <time.h> #include <sys/socket.h> #include "netlink_helpers.h" static int rcvbuf = 1024 * 1024; void rtnl_close(struct rtnl_handle *rth) { if (rth->fd >= 0) { close(rth->fd); rth->fd = -1; } } int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions, int protocol) { socklen_t addr_len; int sndbuf = 32768; int one = 1; memset(rth, 0, sizeof(*rth)); rth->proto = protocol; rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol); if (rth->fd < 0) { perror("Cannot open netlink socket"); return -1; } if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)) < 0) { perror("SO_SNDBUF"); goto err; } if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)) < 0) { perror("SO_RCVBUF"); goto err; } /* Older kernels may no support extended ACK reporting */ setsockopt(rth->fd, SOL_NETLINK, NETLINK_EXT_ACK, &one, sizeof(one)); memset(&rth->local, 0, sizeof(rth->local)); rth->local.nl_family = AF_NETLINK; rth->local.nl_groups = subscriptions; if (bind(rth->fd, (struct sockaddr *)&rth->local, sizeof(rth->local)) < 0) { perror("Cannot bind netlink socket"); goto err; } addr_len = sizeof(rth->local); if (getsockname(rth->fd, (struct sockaddr *)&rth->local, &addr_len) < 0) { perror("Cannot getsockname"); goto err; } if (addr_len != sizeof(rth->local)) { fprintf(stderr, "Wrong address length %d\n", addr_len); goto err; } if (rth->local.nl_family != AF_NETLINK) { fprintf(stderr, "Wrong address family %d\n", rth->local.nl_family); goto err; } rth->seq = time(NULL); return 0; err: rtnl_close(rth); return -1; } int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions) { return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE); } static int __rtnl_recvmsg(int fd, struct msghdr *msg, int flags) { int len; do { len = recvmsg(fd, msg, flags); } while (len < 0 && (errno == EINTR || errno == EAGAIN)); if (len < 0) { fprintf(stderr, "netlink receive error %s (%d)\n", strerror(errno), errno); return -errno; } if (len == 0) { fprintf(stderr, "EOF on netlink\n"); return -ENODATA; } return len; } static int rtnl_recvmsg(int fd, struct msghdr *msg, char **answer) { struct iovec *iov = msg->msg_iov; char *buf; int len; iov->iov_base = NULL; iov->iov_len = 0; len = __rtnl_recvmsg(fd, msg, MSG_PEEK | MSG_TRUNC); if (len < 0) return len; if (len < 32768) len = 32768; buf = malloc(len); if (!buf) { fprintf(stderr, "malloc error: not enough buffer\n"); return -ENOMEM; } iov->iov_base = buf; iov->iov_len = len; len = __rtnl_recvmsg(fd, msg, 0); if (len < 0) { free(buf); return len; } if (answer) *answer = buf; else free(buf); return len; } static void rtnl_talk_error(struct nlmsghdr *h, struct nlmsgerr *err, nl_ext_ack_fn_t errfn) { fprintf(stderr, "RTNETLINK answers: %s\n", strerror(-err->error)); } static int __rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iov, size_t iovlen, struct nlmsghdr **answer, bool show_rtnl_err, nl_ext_ack_fn_t errfn) { struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; struct iovec riov; struct msghdr msg = { .msg_name = &nladdr, .msg_namelen = sizeof(nladdr), .msg_iov = iov, .msg_iovlen = iovlen, }; unsigned int seq = 0; struct nlmsghdr *h; int i, status; char *buf; for (i = 0; i < iovlen; i++) { h = iov[i].iov_base; h->nlmsg_seq = seq = ++rtnl->seq; if (answer == NULL) h->nlmsg_flags |= NLM_F_ACK; } status = sendmsg(rtnl->fd, &msg, 0); if (status < 0) { perror("Cannot talk to rtnetlink"); return -1; } /* change msg to use the response iov */ msg.msg_iov = &riov; msg.msg_iovlen = 1; i = 0; while (1) { next: status = rtnl_recvmsg(rtnl->fd, &msg, &buf); ++i; if (status < 0) return status; if (msg.msg_namelen != sizeof(nladdr)) { fprintf(stderr, "Sender address length == %d!\n", msg.msg_namelen); exit(1); } for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) { int len = h->nlmsg_len; int l = len - sizeof(*h); if (l < 0 || len > status) { if (msg.msg_flags & MSG_TRUNC) { fprintf(stderr, "Truncated message!\n"); free(buf); return -1; } fprintf(stderr, "Malformed message: len=%d!\n", len); exit(1); } if (nladdr.nl_pid != 0 || h->nlmsg_pid != rtnl->local.nl_pid || h->nlmsg_seq > seq || h->nlmsg_seq < seq - iovlen) { /* Don't forget to skip that message. */ status -= NLMSG_ALIGN(len); h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); continue; } if (h->nlmsg_type == NLMSG_ERROR) { struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); int error = err->error; if (l < sizeof(struct nlmsgerr)) { fprintf(stderr, "ERROR truncated\n"); free(buf); return -1; } if (error) { errno = -error; if (rtnl->proto != NETLINK_SOCK_DIAG && show_rtnl_err) rtnl_talk_error(h, err, errfn); } if (i < iovlen) { free(buf); goto next; } if (error) { free(buf); return -i; } if (answer) *answer = (struct nlmsghdr *)buf; else free(buf); return 0; } if (answer) { *answer = (struct nlmsghdr *)buf; return 0; } fprintf(stderr, "Unexpected reply!\n"); status -= NLMSG_ALIGN(len); h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); } free(buf); if (msg.msg_flags & MSG_TRUNC) { fprintf(stderr, "Message truncated!\n"); continue; } if (status) { fprintf(stderr, "Remnant of size %d!\n", status); exit(1); } } } static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, struct nlmsghdr **answer, bool show_rtnl_err, nl_ext_ack_fn_t errfn) { struct iovec iov = { .iov_base = n, .iov_len = n->nlmsg_len, }; return __rtnl_talk_iov(rtnl, &iov, 1, answer, show_rtnl_err, errfn); } int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, struct nlmsghdr **answer) { return __rtnl_talk(rtnl, n, answer, true, NULL); } int addattr(struct nlmsghdr *n, int maxlen, int type) { return addattr_l(n, maxlen, type, NULL, 0); } int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data) { return addattr_l(n, maxlen, type, &data, sizeof(__u8)); } int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data) { return addattr_l(n, maxlen, type, &data, sizeof(__u16)); } int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data) { return addattr_l(n, maxlen, type, &data, sizeof(__u32)); } int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data) { return addattr_l(n, maxlen, type, &data, sizeof(__u64)); } int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str) { return addattr_l(n, maxlen, type, str, strlen(str)+1); } int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, int alen) { int len = RTA_LENGTH(alen); struct rtattr *rta; if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) { fprintf(stderr, "%s: Message exceeded bound of %d\n", __func__, maxlen); return -1; } rta = NLMSG_TAIL(n); rta->rta_type = type; rta->rta_len = len; if (alen) memcpy(RTA_DATA(rta), data, alen); n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len); return 0; } int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len) { if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) { fprintf(stderr, "%s: Message exceeded bound of %d\n", __func__, maxlen); return -1; } memcpy(NLMSG_TAIL(n), data, len); memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len); n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len); return 0; } struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type) { struct rtattr *nest = NLMSG_TAIL(n); addattr_l(n, maxlen, type, NULL, 0); return nest; } int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest) { nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest; return n->nlmsg_len; } |