]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Kernel communication using netlink interface. |
2 | * Copyright (C) 1999 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 19 | */ |
1fdc9eae | 20 | |
21 | #include <zebra.h> | |
22 | ||
acfa8927 | 23 | #if defined(HANDLE_NETLINK_FUZZING) |
81a2f870 SW |
24 | #include <stdio.h> |
25 | #include <string.h> | |
acfa8927 | 26 | #endif /* HANDLE_NETLINK_FUZZING */ |
81a2f870 | 27 | |
ddfeb486 DL |
28 | #ifdef HAVE_NETLINK |
29 | ||
1fdc9eae | 30 | #include "linklist.h" |
31 | #include "if.h" | |
32 | #include "log.h" | |
33 | #include "prefix.h" | |
34 | #include "connected.h" | |
35 | #include "table.h" | |
36 | #include "memory.h" | |
37 | #include "zebra_memory.h" | |
38 | #include "rib.h" | |
39 | #include "thread.h" | |
40 | #include "privs.h" | |
41 | #include "nexthop.h" | |
42 | #include "vrf.h" | |
43 | #include "mpls.h" | |
174482ef | 44 | #include "lib_errors.h" |
1fdc9eae | 45 | |
46 | #include "zebra/zserv.h" | |
47 | #include "zebra/zebra_ns.h" | |
48 | #include "zebra/zebra_vrf.h" | |
05f7f5db | 49 | #include "zebra/rt.h" |
1fdc9eae | 50 | #include "zebra/debug.h" |
51 | #include "zebra/kernel_netlink.h" | |
52 | #include "zebra/rt_netlink.h" | |
53 | #include "zebra/if_netlink.h" | |
942bf97b | 54 | #include "zebra/rule_netlink.h" |
43e52561 | 55 | #include "zebra/zebra_errors.h" |
1fdc9eae | 56 | |
57 | #ifndef SO_RCVBUFFORCE | |
58 | #define SO_RCVBUFFORCE (33) | |
59 | #endif | |
60 | ||
61 | /* Hack for GNU libc version 2. */ | |
62 | #ifndef MSG_TRUNC | |
63 | #define MSG_TRUNC 0x20 | |
64 | #endif /* MSG_TRUNC */ | |
65 | ||
66 | #ifndef NLMSG_TAIL | |
d62a17ae | 67 | #define NLMSG_TAIL(nmsg) \ |
d7c0a89a QY |
68 | ((struct rtattr *)(((uint8_t *)(nmsg)) \ |
69 | + NLMSG_ALIGN((nmsg)->nlmsg_len))) | |
1fdc9eae | 70 | #endif |
71 | ||
72 | #ifndef RTA_TAIL | |
d62a17ae | 73 | #define RTA_TAIL(rta) \ |
d7c0a89a | 74 | ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len))) |
1fdc9eae | 75 | #endif |
76 | ||
f909c673 DS |
77 | #ifndef RTNL_FAMILY_IP6MR |
78 | #define RTNL_FAMILY_IP6MR 129 | |
79 | #endif | |
80 | ||
81 | #ifndef RTPROT_MROUTED | |
82 | #define RTPROT_MROUTED 17 | |
83 | #endif | |
84 | ||
d62a17ae | 85 | static const struct message nlmsg_str[] = {{RTM_NEWROUTE, "RTM_NEWROUTE"}, |
86 | {RTM_DELROUTE, "RTM_DELROUTE"}, | |
87 | {RTM_GETROUTE, "RTM_GETROUTE"}, | |
88 | {RTM_NEWLINK, "RTM_NEWLINK"}, | |
89 | {RTM_DELLINK, "RTM_DELLINK"}, | |
90 | {RTM_GETLINK, "RTM_GETLINK"}, | |
91 | {RTM_NEWADDR, "RTM_NEWADDR"}, | |
92 | {RTM_DELADDR, "RTM_DELADDR"}, | |
93 | {RTM_GETADDR, "RTM_GETADDR"}, | |
94 | {RTM_NEWNEIGH, "RTM_NEWNEIGH"}, | |
95 | {RTM_DELNEIGH, "RTM_DELNEIGH"}, | |
96 | {RTM_GETNEIGH, "RTM_GETNEIGH"}, | |
942bf97b | 97 | {RTM_NEWRULE, "RTM_NEWRULE"}, |
98 | {RTM_DELRULE, "RTM_DELRULE"}, | |
99 | {RTM_GETRULE, "RTM_GETRULE"}, | |
d62a17ae | 100 | {0}}; |
1fdc9eae | 101 | |
102 | static const struct message rtproto_str[] = { | |
d62a17ae | 103 | {RTPROT_REDIRECT, "redirect"}, |
104 | {RTPROT_KERNEL, "kernel"}, | |
105 | {RTPROT_BOOT, "boot"}, | |
106 | {RTPROT_STATIC, "static"}, | |
107 | {RTPROT_GATED, "GateD"}, | |
108 | {RTPROT_RA, "router advertisement"}, | |
109 | {RTPROT_MRT, "MRT"}, | |
110 | {RTPROT_ZEBRA, "Zebra"}, | |
1fdc9eae | 111 | #ifdef RTPROT_BIRD |
d62a17ae | 112 | {RTPROT_BIRD, "BIRD"}, |
1fdc9eae | 113 | #endif /* RTPROT_BIRD */ |
d62a17ae | 114 | {RTPROT_MROUTED, "mroute"}, |
115 | {RTPROT_BGP, "BGP"}, | |
116 | {RTPROT_OSPF, "OSPF"}, | |
117 | {RTPROT_ISIS, "IS-IS"}, | |
118 | {RTPROT_RIP, "RIP"}, | |
119 | {RTPROT_RIPNG, "RIPNG"}, | |
d4d71f11 | 120 | {RTPROT_ZSTATIC, "static"}, |
d62a17ae | 121 | {0}}; |
122 | ||
123 | static const struct message family_str[] = {{AF_INET, "ipv4"}, | |
124 | {AF_INET6, "ipv6"}, | |
125 | {AF_BRIDGE, "bridge"}, | |
126 | {RTNL_FAMILY_IPMR, "ipv4MR"}, | |
127 | {RTNL_FAMILY_IP6MR, "ipv6MR"}, | |
128 | {0}}; | |
129 | ||
130 | static const struct message rttype_str[] = {{RTN_UNICAST, "unicast"}, | |
131 | {RTN_MULTICAST, "multicast"}, | |
132 | {0}}; | |
b339bde7 | 133 | |
1fdc9eae | 134 | extern struct thread_master *master; |
d7c0a89a | 135 | extern uint32_t nl_rcvbufsize; |
1fdc9eae | 136 | |
137 | extern struct zebra_privs_t zserv_privs; | |
138 | ||
2414abd3 | 139 | int netlink_talk_filter(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
1fdc9eae | 140 | { |
3575d9e8 DS |
141 | /* |
142 | * This is an error condition that must be handled during | |
143 | * development. | |
144 | * | |
145 | * The netlink_talk_filter function is used for communication | |
146 | * down the netlink_cmd pipe and we are expecting | |
147 | * an ack being received. So if we get here | |
148 | * then we did not receive the ack and instead | |
149 | * received some other message in an unexpected | |
150 | * way. | |
151 | */ | |
43e52561 QY |
152 | zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__, |
153 | h->nlmsg_type, nl_msg_type_to_str(h->nlmsg_type), ns_id); | |
d62a17ae | 154 | return 0; |
1fdc9eae | 155 | } |
156 | ||
d62a17ae | 157 | static int netlink_recvbuf(struct nlsock *nl, uint32_t newsize) |
1fdc9eae | 158 | { |
d7c0a89a | 159 | uint32_t oldsize; |
d62a17ae | 160 | socklen_t newlen = sizeof(newsize); |
161 | socklen_t oldlen = sizeof(oldsize); | |
162 | int ret; | |
163 | ||
164 | ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen); | |
165 | if (ret < 0) { | |
09c866e3 QY |
166 | flog_err_sys(LIB_ERR_SOCKET, |
167 | "Can't get %s receive buffer size: %s", nl->name, | |
168 | safe_strerror(errno)); | |
d62a17ae | 169 | return -1; |
170 | } | |
171 | ||
172 | /* Try force option (linux >= 2.6.14) and fall back to normal set */ | |
173 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
af4c2728 | 174 | flog_err(LIB_ERR_PRIVILEGES, |
174482ef | 175 | "routing_socket: Can't raise privileges"); |
d62a17ae | 176 | ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE, &nl_rcvbufsize, |
177 | sizeof(nl_rcvbufsize)); | |
178 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
af4c2728 | 179 | flog_err(LIB_ERR_PRIVILEGES, |
174482ef | 180 | "routing_socket: Can't lower privileges"); |
d62a17ae | 181 | if (ret < 0) |
182 | ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, | |
183 | &nl_rcvbufsize, sizeof(nl_rcvbufsize)); | |
184 | if (ret < 0) { | |
09c866e3 QY |
185 | flog_err_sys(LIB_ERR_SOCKET, |
186 | "Can't set %s receive buffer size: %s", nl->name, | |
187 | safe_strerror(errno)); | |
d62a17ae | 188 | return -1; |
189 | } | |
190 | ||
191 | ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen); | |
192 | if (ret < 0) { | |
09c866e3 QY |
193 | flog_err_sys(LIB_ERR_SOCKET, |
194 | "Can't get %s receive buffer size: %s", nl->name, | |
195 | safe_strerror(errno)); | |
d62a17ae | 196 | return -1; |
197 | } | |
198 | ||
199 | zlog_info("Setting netlink socket receive buffer size: %u -> %u", | |
200 | oldsize, newsize); | |
201 | return 0; | |
1fdc9eae | 202 | } |
203 | ||
204 | /* Make socket for Linux netlink interface. */ | |
d62a17ae | 205 | static int netlink_socket(struct nlsock *nl, unsigned long groups, |
206 | ns_id_t ns_id) | |
1fdc9eae | 207 | { |
d62a17ae | 208 | int ret; |
209 | struct sockaddr_nl snl; | |
210 | int sock; | |
211 | int namelen; | |
212 | int save_errno; | |
213 | ||
214 | if (zserv_privs.change(ZPRIVS_RAISE)) { | |
af4c2728 | 215 | flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges"); |
d62a17ae | 216 | return -1; |
217 | } | |
218 | ||
fe533c56 | 219 | sock = ns_socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE, ns_id); |
d62a17ae | 220 | if (sock < 0) { |
09c866e3 QY |
221 | flog_err_sys(LIB_ERR_SOCKET, "Can't open %s socket: %s", |
222 | nl->name, safe_strerror(errno)); | |
d62a17ae | 223 | return -1; |
224 | } | |
225 | ||
226 | memset(&snl, 0, sizeof snl); | |
227 | snl.nl_family = AF_NETLINK; | |
228 | snl.nl_groups = groups; | |
229 | ||
230 | /* Bind the socket to the netlink structure for anything. */ | |
231 | ret = bind(sock, (struct sockaddr *)&snl, sizeof snl); | |
232 | save_errno = errno; | |
233 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
af4c2728 | 234 | flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges"); |
d62a17ae | 235 | |
236 | if (ret < 0) { | |
09c866e3 QY |
237 | flog_err_sys(LIB_ERR_SOCKET, |
238 | "Can't bind %s socket to group 0x%x: %s", nl->name, | |
239 | snl.nl_groups, safe_strerror(save_errno)); | |
d62a17ae | 240 | close(sock); |
241 | return -1; | |
242 | } | |
243 | ||
244 | /* multiple netlink sockets will have different nl_pid */ | |
245 | namelen = sizeof snl; | |
246 | ret = getsockname(sock, (struct sockaddr *)&snl, (socklen_t *)&namelen); | |
247 | if (ret < 0 || namelen != sizeof snl) { | |
09c866e3 QY |
248 | flog_err_sys(LIB_ERR_SOCKET, "Can't get %s socket name: %s", |
249 | nl->name, safe_strerror(errno)); | |
d62a17ae | 250 | close(sock); |
251 | return -1; | |
252 | } | |
253 | ||
254 | nl->snl = snl; | |
255 | nl->sock = sock; | |
256 | return ret; | |
1fdc9eae | 257 | } |
258 | ||
2414abd3 | 259 | static int netlink_information_fetch(struct nlmsghdr *h, ns_id_t ns_id, |
d62a17ae | 260 | int startup) |
1fdc9eae | 261 | { |
3575d9e8 DS |
262 | /* |
263 | * When we handle new message types here | |
264 | * because we are starting to install them | |
265 | * then lets check the netlink_install_filter | |
266 | * and see if we should add the corresponding | |
267 | * allow through entry there. | |
268 | * Probably not needed to do but please | |
269 | * think about it. | |
270 | */ | |
d62a17ae | 271 | switch (h->nlmsg_type) { |
272 | case RTM_NEWROUTE: | |
2414abd3 | 273 | return netlink_route_change(h, ns_id, startup); |
d62a17ae | 274 | case RTM_DELROUTE: |
2414abd3 | 275 | return netlink_route_change(h, ns_id, startup); |
d62a17ae | 276 | case RTM_NEWLINK: |
2414abd3 | 277 | return netlink_link_change(h, ns_id, startup); |
d62a17ae | 278 | case RTM_DELLINK: |
2414abd3 | 279 | return netlink_link_change(h, ns_id, startup); |
d62a17ae | 280 | case RTM_NEWADDR: |
2414abd3 | 281 | return netlink_interface_addr(h, ns_id, startup); |
d62a17ae | 282 | case RTM_DELADDR: |
2414abd3 | 283 | return netlink_interface_addr(h, ns_id, startup); |
d62a17ae | 284 | case RTM_NEWNEIGH: |
2414abd3 | 285 | return netlink_neigh_change(h, ns_id); |
d62a17ae | 286 | case RTM_DELNEIGH: |
2414abd3 | 287 | return netlink_neigh_change(h, ns_id); |
942bf97b | 288 | case RTM_NEWRULE: |
2414abd3 | 289 | return netlink_rule_change(h, ns_id, startup); |
942bf97b | 290 | case RTM_DELRULE: |
2414abd3 | 291 | return netlink_rule_change(h, ns_id, startup); |
d62a17ae | 292 | default: |
3575d9e8 DS |
293 | /* |
294 | * If we have received this message then | |
295 | * we have made a mistake during development | |
296 | * and we need to write some code to handle | |
297 | * this message type or not ask for | |
298 | * it to be sent up to us | |
299 | */ | |
af4c2728 | 300 | flog_err(ZEBRA_ERR_UNKNOWN_NLMSG, |
43e52561 QY |
301 | "Unknown netlink nlmsg_type %s(%d) vrf %u\n", |
302 | nl_msg_type_to_str(h->nlmsg_type), h->nlmsg_type, | |
303 | ns_id); | |
d62a17ae | 304 | break; |
305 | } | |
306 | return 0; | |
1fdc9eae | 307 | } |
308 | ||
acfa8927 | 309 | #if defined(HANDLE_NETLINK_FUZZING) |
81a2f870 SW |
310 | /* Using globals here to avoid adding function parameters */ |
311 | ||
312 | /* Keep distinct filenames for netlink fuzzy collection */ | |
313 | static unsigned int netlink_file_counter = 1; | |
314 | ||
315 | /* File name to read fuzzed netlink from */ | |
316 | static char netlink_fuzz_file[MAXPATHLEN] = ""; | |
317 | ||
318 | /* Flag for whether to read from file or not */ | |
29bf7b0b | 319 | bool netlink_read; |
81a2f870 SW |
320 | |
321 | /** | |
322 | * netlink_read_init() - Starts the message parser | |
323 | * @fname: Filename to read. | |
324 | */ | |
325 | void netlink_read_init(const char *fname) | |
326 | { | |
327 | snprintf(netlink_fuzz_file, MAXPATHLEN, "%s", fname); | |
328 | /* Creating this fake socket for testing purposes */ | |
ef593eff SW |
329 | struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT); |
330 | ||
331 | netlink_parse_info(netlink_information_fetch, &zns->netlink, zns, 1, 0); | |
81a2f870 SW |
332 | } |
333 | ||
334 | /** | |
335 | * netlink_write_incoming() - Writes all data received from netlink to a file | |
336 | * @buf: Data from netlink. | |
337 | * @size: Size of data. | |
338 | * @counter: Counter for keeping filenames distinct. | |
339 | */ | |
340 | static void netlink_write_incoming(const char *buf, const unsigned int size, | |
341 | unsigned int counter) | |
342 | { | |
343 | char fname[MAXPATHLEN]; | |
344 | FILE *f; | |
345 | ||
346 | zserv_privs.change(ZPRIVS_RAISE); | |
347 | snprintf(fname, MAXPATHLEN, "%s/%s_%u", DAEMON_VTY_DIR, "netlink", | |
348 | counter); | |
349 | f = fopen(fname, "w"); | |
350 | if (f) { | |
351 | fwrite(buf, 1, size, f); | |
352 | fclose(f); | |
353 | } | |
354 | zserv_privs.change(ZPRIVS_LOWER); | |
355 | } | |
356 | ||
357 | /** | |
358 | * netlink_read_file() - Reads netlink data from file | |
359 | * @buf: Netlink buffer being overwritten. | |
360 | * @fname: File name to read from. | |
361 | * | |
362 | * Return: Size of file. | |
363 | */ | |
364 | static long netlink_read_file(char *buf, const char *fname) | |
365 | { | |
366 | FILE *f; | |
367 | long file_bytes = -1; | |
bd7891fd | 368 | |
81a2f870 SW |
369 | zserv_privs.change(ZPRIVS_RAISE); |
370 | f = fopen(fname, "r"); | |
371 | if (f) { | |
372 | fseek(f, 0, SEEK_END); | |
373 | file_bytes = ftell(f); | |
374 | rewind(f); | |
ef593eff | 375 | fread(buf, NL_RCV_PKT_BUF_SIZE, 1, f); |
81a2f870 SW |
376 | fclose(f); |
377 | } | |
378 | zserv_privs.change(ZPRIVS_LOWER); | |
379 | return file_bytes; | |
380 | } | |
381 | ||
acfa8927 | 382 | #endif /* HANDLE_NETLINK_FUZZING */ |
81a2f870 | 383 | |
d62a17ae | 384 | static int kernel_read(struct thread *thread) |
1fdc9eae | 385 | { |
d62a17ae | 386 | struct zebra_ns *zns = (struct zebra_ns *)THREAD_ARG(thread); |
387 | netlink_parse_info(netlink_information_fetch, &zns->netlink, zns, 5, 0); | |
388 | zns->t_netlink = NULL; | |
389 | thread_add_read(zebrad.master, kernel_read, zns, zns->netlink.sock, | |
390 | &zns->t_netlink); | |
1fdc9eae | 391 | |
d62a17ae | 392 | return 0; |
1fdc9eae | 393 | } |
394 | ||
3575d9e8 DS |
395 | /* |
396 | * Filter out messages from self that occur on listener socket, | |
1fdc9eae | 397 | * caused by our actions on the command socket |
3575d9e8 DS |
398 | * |
399 | * When we add new Netlink message types we probably | |
400 | * do not need to add them here as that we are filtering | |
401 | * on the routes we actually care to receive( which is rarer | |
402 | * then the normal course of operations). We are intentionally | |
403 | * allowing some messages from ourselves through | |
404 | * ( I'm looking at you Interface based netlink messages ) | |
405 | * so that we only had to write one way to handle incoming | |
406 | * address add/delete changes. | |
1fdc9eae | 407 | */ |
d62a17ae | 408 | static void netlink_install_filter(int sock, __u32 pid) |
1fdc9eae | 409 | { |
3575d9e8 DS |
410 | /* |
411 | * BPF_JUMP instructions and where you jump to are based upon | |
412 | * 0 as being the next statement. So count from 0. Writing | |
413 | * this down because every time I look at this I have to | |
414 | * re-remember it. | |
415 | */ | |
d62a17ae | 416 | struct sock_filter filter[] = { |
3575d9e8 DS |
417 | /* |
418 | * Logic: | |
419 | * if (nlmsg_pid == pid) { | |
420 | * if (the incoming nlmsg_type == | |
421 | * RTM_NEWADDR | RTM_DELADDR) | |
422 | * keep this message | |
423 | * else | |
424 | * skip this message | |
425 | * } else | |
426 | * keep this netlink message | |
427 | */ | |
428 | /* | |
429 | * 0: Load the nlmsg_pid into the BPF register | |
430 | */ | |
d62a17ae | 431 | BPF_STMT(BPF_LD | BPF_ABS | BPF_W, |
432 | offsetof(struct nlmsghdr, nlmsg_pid)), | |
3575d9e8 DS |
433 | /* |
434 | * 1: Compare to pid | |
435 | */ | |
436 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(pid), 0, 4), | |
437 | /* | |
438 | * 2: Load the nlmsg_type into BPF register | |
439 | */ | |
440 | BPF_STMT(BPF_LD | BPF_ABS | BPF_H, | |
441 | offsetof(struct nlmsghdr, nlmsg_type)), | |
442 | /* | |
443 | * 3: Compare to RTM_NEWADDR | |
444 | */ | |
445 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_NEWADDR), 2, 0), | |
446 | /* | |
447 | * 4: Compare to RTM_DELADDR | |
448 | */ | |
449 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_DELADDR), 1, 0), | |
450 | /* | |
451 | * 5: This is the end state of we want to skip the | |
452 | * message | |
453 | */ | |
d62a17ae | 454 | BPF_STMT(BPF_RET | BPF_K, 0), |
3575d9e8 DS |
455 | /* 6: This is the end state of we want to keep |
456 | * the message | |
457 | */ | |
d62a17ae | 458 | BPF_STMT(BPF_RET | BPF_K, 0xffff), |
459 | }; | |
460 | ||
461 | struct sock_fprog prog = { | |
9d303b37 | 462 | .len = array_size(filter), .filter = filter, |
d62a17ae | 463 | }; |
464 | ||
465 | if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)) | |
466 | < 0) | |
467 | zlog_warn("Can't install socket filter: %s\n", | |
468 | safe_strerror(errno)); | |
1fdc9eae | 469 | } |
470 | ||
d62a17ae | 471 | void netlink_parse_rtattr(struct rtattr **tb, int max, struct rtattr *rta, |
472 | int len) | |
1fdc9eae | 473 | { |
d62a17ae | 474 | while (RTA_OK(rta, len)) { |
475 | if (rta->rta_type <= max) | |
476 | tb[rta->rta_type] = rta; | |
477 | rta = RTA_NEXT(rta, len); | |
478 | } | |
1fdc9eae | 479 | } |
480 | ||
86391e56 MS |
481 | int addattr_l(struct nlmsghdr *n, unsigned int maxlen, int type, |
482 | const void *data, unsigned int alen) | |
1fdc9eae | 483 | { |
d62a17ae | 484 | int len; |
485 | struct rtattr *rta; | |
1fdc9eae | 486 | |
d62a17ae | 487 | len = RTA_LENGTH(alen); |
1fdc9eae | 488 | |
d62a17ae | 489 | if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) |
490 | return -1; | |
1fdc9eae | 491 | |
d62a17ae | 492 | rta = (struct rtattr *)(((char *)n) + NLMSG_ALIGN(n->nlmsg_len)); |
493 | rta->rta_type = type; | |
494 | rta->rta_len = len; | |
4b2792b5 | 495 | |
d62a17ae | 496 | if (data) |
497 | memcpy(RTA_DATA(rta), data, alen); | |
498 | else | |
499 | assert(alen == 0); | |
4b2792b5 | 500 | |
d62a17ae | 501 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len); |
1fdc9eae | 502 | |
d62a17ae | 503 | return 0; |
1fdc9eae | 504 | } |
505 | ||
86391e56 MS |
506 | int rta_addattr_l(struct rtattr *rta, unsigned int maxlen, int type, |
507 | const void *data, unsigned int alen) | |
1fdc9eae | 508 | { |
d62a17ae | 509 | unsigned int len; |
510 | struct rtattr *subrta; | |
1fdc9eae | 511 | |
d62a17ae | 512 | len = RTA_LENGTH(alen); |
1fdc9eae | 513 | |
d62a17ae | 514 | if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) |
515 | return -1; | |
1fdc9eae | 516 | |
d62a17ae | 517 | subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len)); |
518 | subrta->rta_type = type; | |
519 | subrta->rta_len = len; | |
4b2792b5 | 520 | |
d62a17ae | 521 | if (data) |
522 | memcpy(RTA_DATA(subrta), data, alen); | |
523 | else | |
524 | assert(alen == 0); | |
4b2792b5 | 525 | |
d62a17ae | 526 | rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len); |
1fdc9eae | 527 | |
d62a17ae | 528 | return 0; |
1fdc9eae | 529 | } |
530 | ||
d7c0a89a | 531 | int addattr16(struct nlmsghdr *n, unsigned int maxlen, int type, uint16_t data) |
bbc16902 | 532 | { |
d7c0a89a | 533 | return addattr_l(n, maxlen, type, &data, sizeof(uint16_t)); |
bbc16902 | 534 | } |
535 | ||
d62a17ae | 536 | int addattr32(struct nlmsghdr *n, unsigned int maxlen, int type, int data) |
1fdc9eae | 537 | { |
d7c0a89a | 538 | return addattr_l(n, maxlen, type, &data, sizeof(uint32_t)); |
1fdc9eae | 539 | } |
540 | ||
d62a17ae | 541 | struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type) |
1fdc9eae | 542 | { |
d62a17ae | 543 | struct rtattr *nest = NLMSG_TAIL(n); |
1fdc9eae | 544 | |
d62a17ae | 545 | addattr_l(n, maxlen, type, NULL, 0); |
546 | return nest; | |
1fdc9eae | 547 | } |
548 | ||
d62a17ae | 549 | int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest) |
1fdc9eae | 550 | { |
d7c0a89a | 551 | nest->rta_len = (uint8_t *)NLMSG_TAIL(n) - (uint8_t *)nest; |
d62a17ae | 552 | return n->nlmsg_len; |
1fdc9eae | 553 | } |
554 | ||
d62a17ae | 555 | struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type) |
1fdc9eae | 556 | { |
d62a17ae | 557 | struct rtattr *nest = RTA_TAIL(rta); |
1fdc9eae | 558 | |
d62a17ae | 559 | rta_addattr_l(rta, maxlen, type, NULL, 0); |
560 | return nest; | |
1fdc9eae | 561 | } |
562 | ||
d62a17ae | 563 | int rta_nest_end(struct rtattr *rta, struct rtattr *nest) |
1fdc9eae | 564 | { |
d7c0a89a | 565 | nest->rta_len = (uint8_t *)RTA_TAIL(rta) - (uint8_t *)nest; |
d62a17ae | 566 | return rta->rta_len; |
1fdc9eae | 567 | } |
568 | ||
d62a17ae | 569 | const char *nl_msg_type_to_str(uint16_t msg_type) |
1fdc9eae | 570 | { |
d62a17ae | 571 | return lookup_msg(nlmsg_str, msg_type, ""); |
1fdc9eae | 572 | } |
573 | ||
d7c0a89a | 574 | const char *nl_rtproto_to_str(uint8_t rtproto) |
1fdc9eae | 575 | { |
d62a17ae | 576 | return lookup_msg(rtproto_str, rtproto, ""); |
1fdc9eae | 577 | } |
b339bde7 | 578 | |
d7c0a89a | 579 | const char *nl_family_to_str(uint8_t family) |
b339bde7 | 580 | { |
d62a17ae | 581 | return lookup_msg(family_str, family, ""); |
b339bde7 DS |
582 | } |
583 | ||
d7c0a89a | 584 | const char *nl_rttype_to_str(uint8_t rttype) |
b339bde7 | 585 | { |
d62a17ae | 586 | return lookup_msg(rttype_str, rttype, ""); |
b339bde7 DS |
587 | } |
588 | ||
5d307d5d DS |
589 | #define NL_OK(nla, len) \ |
590 | ((len) >= (int)sizeof(struct nlattr) \ | |
591 | && (nla)->nla_len >= sizeof(struct nlattr) \ | |
592 | && (nla)->nla_len <= (len)) | |
593 | #define NL_NEXT(nla, attrlen) \ | |
594 | ((attrlen) -= RTA_ALIGN((nla)->nla_len), \ | |
595 | (struct nlattr *)(((char *)(nla)) + RTA_ALIGN((nla)->nla_len))) | |
596 | #define NL_RTA(r) \ | |
597 | ((struct nlattr *)(((char *)(r)) \ | |
598 | + NLMSG_ALIGN(sizeof(struct nlmsgerr)))) | |
599 | ||
600 | static void netlink_parse_nlattr(struct nlattr **tb, int max, | |
601 | struct nlattr *nla, int len) | |
602 | { | |
603 | while (NL_OK(nla, len)) { | |
604 | if (nla->nla_type <= max) | |
605 | tb[nla->nla_type] = nla; | |
606 | nla = NL_NEXT(nla, len); | |
607 | } | |
608 | } | |
609 | ||
610 | static void netlink_parse_extended_ack(struct nlmsghdr *h) | |
611 | { | |
612 | struct nlattr *tb[NLMSGERR_ATTR_MAX + 1]; | |
613 | const struct nlmsgerr *err = | |
614 | (const struct nlmsgerr *)((uint8_t *)h | |
615 | + NLMSG_ALIGN( | |
616 | sizeof(struct nlmsghdr))); | |
617 | const struct nlmsghdr *err_nlh = NULL; | |
618 | uint32_t hlen = sizeof(*err); | |
619 | const char *msg = NULL; | |
620 | uint32_t off = 0; | |
621 | ||
622 | if (!(h->nlmsg_flags & NLM_F_CAPPED)) | |
623 | hlen += h->nlmsg_len - NLMSG_ALIGN(sizeof(struct nlmsghdr)); | |
624 | ||
625 | memset(tb, 0, sizeof(tb)); | |
626 | netlink_parse_nlattr(tb, NLMSGERR_ATTR_MAX, NL_RTA(h), hlen); | |
627 | ||
628 | if (tb[NLMSGERR_ATTR_MSG]) | |
629 | msg = (const char *)RTA_DATA(tb[NLMSGERR_ATTR_MSG]); | |
630 | ||
631 | if (tb[NLMSGERR_ATTR_OFFS]) { | |
632 | off = *(uint32_t *)RTA_DATA(tb[NLMSGERR_ATTR_OFFS]); | |
633 | ||
634 | if (off > h->nlmsg_len) { | |
635 | zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS\n"); | |
5d307d5d DS |
636 | } else if (!(h->nlmsg_flags & NLM_F_CAPPED)) { |
637 | /* | |
638 | * Header of failed message | |
639 | * we are not doing anything currently with it | |
640 | * but noticing it for later. | |
641 | */ | |
642 | err_nlh = &err->msg; | |
643 | zlog_warn("%s: Received %d extended Ack", | |
644 | __PRETTY_FUNCTION__, err_nlh->nlmsg_type); | |
645 | } | |
646 | } | |
647 | ||
648 | if (msg && *msg != '\0') { | |
649 | bool is_err = !!err->error; | |
650 | ||
651 | if (is_err) | |
652 | zlog_err("Extended Error: %s", msg); | |
653 | else | |
654 | zlog_warn("Extended Warning: %s", msg); | |
655 | } | |
656 | } | |
657 | ||
936ebf0a DS |
658 | /* |
659 | * netlink_parse_info | |
660 | * | |
661 | * Receive message from netlink interface and pass those information | |
662 | * to the given function. | |
663 | * | |
664 | * filter -> Function to call to read the results | |
665 | * nl -> netlink socket information | |
666 | * zns -> The zebra namespace data | |
667 | * count -> How many we should read in, 0 means as much as possible | |
668 | * startup -> Are we reading in under startup conditions? passed to | |
669 | * the filter. | |
670 | */ | |
2414abd3 | 671 | int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int), |
d62a17ae | 672 | struct nlsock *nl, struct zebra_ns *zns, int count, |
673 | int startup) | |
1fdc9eae | 674 | { |
d62a17ae | 675 | int status; |
676 | int ret = 0; | |
677 | int error; | |
678 | int read_in = 0; | |
679 | ||
680 | while (1) { | |
9ed7517b | 681 | char buf[NL_RCV_PKT_BUF_SIZE]; |
d62a17ae | 682 | struct iovec iov = {.iov_base = buf, .iov_len = sizeof buf}; |
683 | struct sockaddr_nl snl; | |
684 | struct msghdr msg = {.msg_name = (void *)&snl, | |
685 | .msg_namelen = sizeof snl, | |
686 | .msg_iov = &iov, | |
687 | .msg_iovlen = 1}; | |
688 | struct nlmsghdr *h; | |
689 | ||
690 | if (count && read_in >= count) | |
691 | return 0; | |
692 | ||
acfa8927 | 693 | #if defined(HANDLE_NETLINK_FUZZING) |
81a2f870 SW |
694 | /* Check if reading and filename is set */ |
695 | if (netlink_read && '\0' != netlink_fuzz_file[0]) { | |
696 | zlog_debug("Reading netlink fuzz file"); | |
697 | status = netlink_read_file(buf, netlink_fuzz_file); | |
698 | snl.nl_pid = 0; | |
699 | } else { | |
700 | status = recvmsg(nl->sock, &msg, 0); | |
701 | } | |
702 | #else | |
d62a17ae | 703 | status = recvmsg(nl->sock, &msg, 0); |
acfa8927 | 704 | #endif /* HANDLE_NETLINK_FUZZING */ |
d62a17ae | 705 | if (status < 0) { |
706 | if (errno == EINTR) | |
707 | continue; | |
708 | if (errno == EWOULDBLOCK || errno == EAGAIN) | |
709 | break; | |
af4c2728 | 710 | flog_err(ZEBRA_ERR_RECVMSG_OVERRUN, |
43e52561 QY |
711 | "%s recvmsg overrun: %s", nl->name, |
712 | safe_strerror(errno)); | |
d62a17ae | 713 | /* |
714 | * In this case we are screwed. | |
715 | * There is no good way to | |
716 | * recover zebra at this point. | |
717 | */ | |
718 | exit(-1); | |
719 | continue; | |
1fdc9eae | 720 | } |
721 | ||
d62a17ae | 722 | if (status == 0) { |
09c866e3 | 723 | flog_err_sys(LIB_ERR_SOCKET, "%s EOF", nl->name); |
d62a17ae | 724 | return -1; |
725 | } | |
726 | ||
727 | if (msg.msg_namelen != sizeof snl) { | |
af4c2728 | 728 | flog_err(ZEBRA_ERR_NETLINK_LENGTH_ERROR, |
43e52561 QY |
729 | "%s sender address length error: length %d", |
730 | nl->name, msg.msg_namelen); | |
d62a17ae | 731 | return -1; |
732 | } | |
733 | ||
734 | if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV) { | |
735 | zlog_debug("%s: << netlink message dump [recv]", | |
736 | __func__); | |
737 | zlog_hexdump(buf, status); | |
738 | } | |
739 | ||
acfa8927 | 740 | #if defined(HANDLE_NETLINK_FUZZING) |
81a2f870 SW |
741 | if (!netlink_read) { |
742 | zlog_debug("Writing incoming netlink message"); | |
743 | netlink_write_incoming(buf, status, | |
744 | netlink_file_counter++); | |
745 | } | |
acfa8927 | 746 | #endif /* HANDLE_NETLINK_FUZZING */ |
81a2f870 | 747 | |
d62a17ae | 748 | read_in++; |
749 | for (h = (struct nlmsghdr *)buf; | |
e6a0e0d1 | 750 | (status >= 0 && NLMSG_OK(h, (unsigned int)status)); |
d62a17ae | 751 | h = NLMSG_NEXT(h, status)) { |
752 | /* Finish of reading. */ | |
753 | if (h->nlmsg_type == NLMSG_DONE) | |
754 | return ret; | |
755 | ||
756 | /* Error handling. */ | |
757 | if (h->nlmsg_type == NLMSG_ERROR) { | |
758 | struct nlmsgerr *err = | |
759 | (struct nlmsgerr *)NLMSG_DATA(h); | |
760 | int errnum = err->error; | |
761 | int msg_type = err->msg.nlmsg_type; | |
762 | ||
5d307d5d DS |
763 | if (h->nlmsg_len |
764 | < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
765 | zlog_err("%s error: message truncated", | |
766 | nl->name); | |
767 | return -1; | |
768 | } | |
769 | ||
770 | /* | |
771 | * Parse the extended information before | |
772 | * we actually handle it. | |
773 | * At this point in time we do not | |
774 | * do anything other than report the | |
775 | * issue. | |
776 | */ | |
777 | if (h->nlmsg_flags & NLM_F_ACK_TLVS) | |
778 | netlink_parse_extended_ack(h); | |
779 | ||
d62a17ae | 780 | /* If the error field is zero, then this is an |
781 | * ACK */ | |
782 | if (err->error == 0) { | |
783 | if (IS_ZEBRA_DEBUG_KERNEL) { | |
784 | zlog_debug( | |
785 | "%s: %s ACK: type=%s(%u), seq=%u, pid=%u", | |
786 | __FUNCTION__, nl->name, | |
787 | nl_msg_type_to_str( | |
788 | err->msg.nlmsg_type), | |
789 | err->msg.nlmsg_type, | |
790 | err->msg.nlmsg_seq, | |
791 | err->msg.nlmsg_pid); | |
792 | } | |
793 | ||
794 | /* return if not a multipart message, | |
795 | * otherwise continue */ | |
796 | if (!(h->nlmsg_flags & NLM_F_MULTI)) | |
797 | return 0; | |
798 | continue; | |
799 | } | |
800 | ||
43e52561 QY |
801 | if (h->nlmsg_len |
802 | < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
af4c2728 | 803 | flog_err( |
43e52561 QY |
804 | ZEBRA_ERR_NETLINK_LENGTH_ERROR, |
805 | "%s error: message truncated", | |
806 | nl->name); | |
807 | return -1; | |
808 | } | |
809 | ||
d62a17ae | 810 | /* Deal with errors that occur because of races |
811 | * in link handling */ | |
812 | if (nl == &zns->netlink_cmd | |
813 | && ((msg_type == RTM_DELROUTE | |
814 | && (-errnum == ENODEV | |
815 | || -errnum == ESRCH)) | |
816 | || (msg_type == RTM_NEWROUTE | |
817 | && (-errnum == ENETDOWN | |
818 | || -errnum == EEXIST)))) { | |
819 | if (IS_ZEBRA_DEBUG_KERNEL) | |
820 | zlog_debug( | |
821 | "%s: error: %s type=%s(%u), seq=%u, pid=%u", | |
822 | nl->name, | |
823 | safe_strerror(-errnum), | |
824 | nl_msg_type_to_str( | |
825 | msg_type), | |
826 | msg_type, | |
827 | err->msg.nlmsg_seq, | |
828 | err->msg.nlmsg_pid); | |
829 | return 0; | |
830 | } | |
831 | ||
832 | /* We see RTM_DELNEIGH when shutting down an | |
833 | * interface with an IPv4 | |
834 | * link-local. The kernel should have already | |
835 | * deleted the neighbor | |
836 | * so do not log these as an error. | |
837 | */ | |
838 | if (msg_type == RTM_DELNEIGH | |
839 | || (nl == &zns->netlink_cmd | |
840 | && msg_type == RTM_NEWROUTE | |
841 | && (-errnum == ESRCH | |
842 | || -errnum == ENETUNREACH))) { | |
843 | /* This is known to happen in some | |
844 | * situations, don't log | |
845 | * as error. | |
846 | */ | |
847 | if (IS_ZEBRA_DEBUG_KERNEL) | |
848 | zlog_debug( | |
849 | "%s error: %s, type=%s(%u), seq=%u, pid=%u", | |
850 | nl->name, | |
851 | safe_strerror(-errnum), | |
852 | nl_msg_type_to_str( | |
853 | msg_type), | |
854 | msg_type, | |
855 | err->msg.nlmsg_seq, | |
856 | err->msg.nlmsg_pid); | |
857 | } else | |
af4c2728 | 858 | flog_err( |
43e52561 | 859 | ZEBRA_ERR_UNEXPECTED_MESSAGE, |
d62a17ae | 860 | "%s error: %s, type=%s(%u), seq=%u, pid=%u", |
861 | nl->name, | |
862 | safe_strerror(-errnum), | |
863 | nl_msg_type_to_str(msg_type), | |
864 | msg_type, err->msg.nlmsg_seq, | |
865 | err->msg.nlmsg_pid); | |
866 | ||
867 | return -1; | |
868 | } | |
869 | ||
870 | /* OK we got netlink message. */ | |
871 | if (IS_ZEBRA_DEBUG_KERNEL) | |
872 | zlog_debug( | |
873 | "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u", | |
874 | nl->name, | |
875 | nl_msg_type_to_str(h->nlmsg_type), | |
876 | h->nlmsg_type, h->nlmsg_len, | |
877 | h->nlmsg_seq, h->nlmsg_pid); | |
878 | ||
783827ae DS |
879 | |
880 | /* | |
881 | * Ignore messages that maybe sent from | |
882 | * other actors besides the kernel | |
883 | */ | |
884 | if (snl.nl_pid != 0) { | |
43e52561 QY |
885 | zlog_debug("Ignoring message from pid %u", |
886 | snl.nl_pid); | |
d62a17ae | 887 | continue; |
888 | } | |
889 | ||
2414abd3 | 890 | error = (*filter)(h, zns->ns_id, startup); |
d62a17ae | 891 | if (error < 0) { |
43e52561 | 892 | zlog_warn("%s filter function error", nl->name); |
d62a17ae | 893 | ret = error; |
894 | } | |
895 | } | |
896 | ||
897 | /* After error care. */ | |
898 | if (msg.msg_flags & MSG_TRUNC) { | |
af4c2728 | 899 | flog_err(ZEBRA_ERR_NETLINK_LENGTH_ERROR, |
43e52561 | 900 | "%s error: message truncated", nl->name); |
d62a17ae | 901 | continue; |
902 | } | |
903 | if (status) { | |
af4c2728 | 904 | flog_err(ZEBRA_ERR_NETLINK_LENGTH_ERROR, |
43e52561 QY |
905 | "%s error: data remnant size %d", nl->name, |
906 | status); | |
d62a17ae | 907 | return -1; |
908 | } | |
909 | } | |
910 | return ret; | |
1fdc9eae | 911 | } |
912 | ||
936ebf0a DS |
913 | /* |
914 | * netlink_talk | |
915 | * | |
916 | * sendmsg() to netlink socket then recvmsg(). | |
917 | * Calls netlink_parse_info to parse returned data | |
918 | * | |
919 | * filter -> The filter to read final results from kernel | |
920 | * nlmsghdr -> The data to send to the kernel | |
921 | * nl -> The netlink socket information | |
922 | * zns -> The zebra namespace information | |
923 | * startup -> Are we reading in under startup conditions | |
924 | * This is passed through eventually to filter. | |
925 | */ | |
2414abd3 | 926 | int netlink_talk(int (*filter)(struct nlmsghdr *, ns_id_t, int startup), |
d62a17ae | 927 | struct nlmsghdr *n, struct nlsock *nl, struct zebra_ns *zns, |
928 | int startup) | |
1fdc9eae | 929 | { |
d62a17ae | 930 | int status; |
931 | struct sockaddr_nl snl; | |
cbaca6a1 DS |
932 | struct iovec iov; |
933 | struct msghdr msg; | |
d62a17ae | 934 | int save_errno; |
935 | ||
936 | memset(&snl, 0, sizeof snl); | |
cbaca6a1 DS |
937 | memset(&iov, 0, sizeof iov); |
938 | memset(&msg, 0, sizeof msg); | |
939 | ||
940 | iov.iov_base = n; | |
941 | iov.iov_len = n->nlmsg_len; | |
942 | msg.msg_name = (void *)&snl; | |
943 | msg.msg_namelen = sizeof snl; | |
944 | msg.msg_iov = &iov; | |
945 | msg.msg_iovlen = 1; | |
946 | ||
d62a17ae | 947 | snl.nl_family = AF_NETLINK; |
948 | ||
949 | n->nlmsg_seq = ++nl->seq; | |
950 | n->nlmsg_pid = nl->snl.nl_pid; | |
951 | ||
d62a17ae | 952 | if (IS_ZEBRA_DEBUG_KERNEL) |
953 | zlog_debug( | |
954 | "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x", | |
955 | nl->name, nl_msg_type_to_str(n->nlmsg_type), | |
956 | n->nlmsg_type, n->nlmsg_len, n->nlmsg_seq, | |
957 | n->nlmsg_flags); | |
958 | ||
959 | /* Send message to netlink interface. */ | |
960 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
af4c2728 | 961 | flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges"); |
d62a17ae | 962 | status = sendmsg(nl->sock, &msg, 0); |
963 | save_errno = errno; | |
964 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
af4c2728 | 965 | flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges"); |
d62a17ae | 966 | |
967 | if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND) { | |
968 | zlog_debug("%s: >> netlink message dump [sent]", __func__); | |
969 | zlog_hexdump(n, n->nlmsg_len); | |
970 | } | |
971 | ||
972 | if (status < 0) { | |
09c866e3 QY |
973 | flog_err_sys(LIB_ERR_SOCKET, "netlink_talk sendmsg() error: %s", |
974 | safe_strerror(save_errno)); | |
d62a17ae | 975 | return -1; |
976 | } | |
977 | ||
978 | ||
979 | /* | |
980 | * Get reply from netlink socket. | |
981 | * The reply should either be an acknowlegement or an error. | |
982 | */ | |
983 | return netlink_parse_info(filter, nl, zns, 0, startup); | |
1fdc9eae | 984 | } |
985 | ||
289602d7 | 986 | /* Issue request message to kernel via netlink socket. GET messages |
987 | * are issued through this interface. | |
988 | */ | |
d62a17ae | 989 | int netlink_request(struct nlsock *nl, struct nlmsghdr *n) |
1fdc9eae | 990 | { |
d62a17ae | 991 | int ret; |
992 | struct sockaddr_nl snl; | |
993 | int save_errno; | |
994 | ||
995 | /* Check netlink socket. */ | |
996 | if (nl->sock < 0) { | |
09c866e3 QY |
997 | flog_err_sys(LIB_ERR_SOCKET, "%s socket isn't active.", |
998 | nl->name); | |
d62a17ae | 999 | return -1; |
1000 | } | |
1001 | ||
1002 | /* Fill common fields for all requests. */ | |
1003 | n->nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; | |
1004 | n->nlmsg_pid = nl->snl.nl_pid; | |
1005 | n->nlmsg_seq = ++nl->seq; | |
1006 | ||
1007 | memset(&snl, 0, sizeof snl); | |
1008 | snl.nl_family = AF_NETLINK; | |
1009 | ||
1010 | /* Raise capabilities and send message, then lower capabilities. */ | |
1011 | if (zserv_privs.change(ZPRIVS_RAISE)) { | |
af4c2728 | 1012 | flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges"); |
d62a17ae | 1013 | return -1; |
1014 | } | |
1015 | ||
1016 | ret = sendto(nl->sock, (void *)n, n->nlmsg_len, 0, | |
1017 | (struct sockaddr *)&snl, sizeof snl); | |
1018 | save_errno = errno; | |
1019 | ||
1020 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
af4c2728 | 1021 | flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges"); |
d62a17ae | 1022 | |
1023 | if (ret < 0) { | |
09c866e3 QY |
1024 | flog_err_sys(LIB_ERR_SOCKET, "%s sendto failed: %s", nl->name, |
1025 | safe_strerror(save_errno)); | |
d62a17ae | 1026 | return -1; |
1027 | } | |
1028 | ||
1029 | return 0; | |
1fdc9eae | 1030 | } |
1031 | ||
1032 | /* Exported interface function. This function simply calls | |
1033 | netlink_socket (). */ | |
d62a17ae | 1034 | void kernel_init(struct zebra_ns *zns) |
1fdc9eae | 1035 | { |
d62a17ae | 1036 | unsigned long groups; |
5d307d5d DS |
1037 | #if defined SOL_NETLINK |
1038 | int one, ret; | |
1039 | #endif | |
d62a17ae | 1040 | |
026a316f DS |
1041 | /* |
1042 | * Initialize netlink sockets | |
1043 | * | |
1044 | * If RTMGRP_XXX exists use that, but at some point | |
1045 | * I think the kernel developers realized that | |
1046 | * keeping track of all the different values would | |
1047 | * lead to confusion, so we need to convert the | |
1048 | * RTNLGRP_XXX to a bit position for ourself | |
1049 | */ | |
1050 | groups = RTMGRP_LINK | | |
1051 | RTMGRP_IPV4_ROUTE | | |
1052 | RTMGRP_IPV4_IFADDR | | |
1053 | RTMGRP_IPV6_ROUTE | | |
1054 | RTMGRP_IPV6_IFADDR | | |
1055 | RTMGRP_IPV4_MROUTE | | |
1056 | RTMGRP_NEIGH | | |
1057 | (1 << (RTNLGRP_IPV4_RULE - 1)) | | |
1058 | (1 << (RTNLGRP_IPV6_RULE - 1)); | |
d62a17ae | 1059 | |
1060 | snprintf(zns->netlink.name, sizeof(zns->netlink.name), | |
1061 | "netlink-listen (NS %u)", zns->ns_id); | |
1062 | zns->netlink.sock = -1; | |
19d5a4fe DS |
1063 | if (netlink_socket(&zns->netlink, groups, zns->ns_id) < 0) { |
1064 | zlog_err("Failure to create %s socket", | |
1065 | zns->netlink.name); | |
1066 | exit(-1); | |
1067 | } | |
d62a17ae | 1068 | |
1069 | snprintf(zns->netlink_cmd.name, sizeof(zns->netlink_cmd.name), | |
1070 | "netlink-cmd (NS %u)", zns->ns_id); | |
1071 | zns->netlink_cmd.sock = -1; | |
19d5a4fe DS |
1072 | if (netlink_socket(&zns->netlink_cmd, 0, zns->ns_id) < 0) { |
1073 | zlog_err("Failure to create %s socket", | |
1074 | zns->netlink_cmd.name); | |
1075 | exit(-1); | |
1076 | } | |
d62a17ae | 1077 | |
5d307d5d DS |
1078 | /* |
1079 | * SOL_NETLINK is not available on all platforms yet | |
1080 | * apparently. It's in bits/socket.h which I am not | |
1081 | * sure that we want to pull into our build system. | |
1082 | */ | |
1083 | #if defined SOL_NETLINK | |
1084 | /* | |
1085 | * Let's tell the kernel that we want to receive extended | |
1086 | * ACKS over our command socket | |
1087 | */ | |
1088 | one = 1; | |
1089 | ret = setsockopt(zns->netlink_cmd.sock, SOL_NETLINK, NETLINK_EXT_ACK, | |
1090 | &one, sizeof(one)); | |
1091 | ||
1092 | if (ret < 0) | |
1093 | zlog_notice("Registration for extended ACK failed : %d %s", | |
1094 | errno, safe_strerror(errno)); | |
1095 | #endif | |
1096 | ||
d62a17ae | 1097 | /* Register kernel socket. */ |
19d5a4fe | 1098 | if (fcntl(zns->netlink.sock, F_SETFL, O_NONBLOCK) < 0) |
09c866e3 QY |
1099 | flog_err_sys(LIB_ERR_SOCKET, "Can't set %s socket flags: %s", |
1100 | zns->netlink.name, safe_strerror(errno)); | |
8c85e8ea DS |
1101 | |
1102 | if (fcntl(zns->netlink_cmd.sock, F_SETFL, O_NONBLOCK) < 0) | |
1103 | zlog_err("Can't set %s socket error: %s(%d)", | |
1104 | zns->netlink_cmd.name, safe_strerror(errno), errno); | |
19d5a4fe DS |
1105 | |
1106 | /* Set receive buffer size if it's set from command line */ | |
1107 | if (nl_rcvbufsize) | |
1108 | netlink_recvbuf(&zns->netlink, nl_rcvbufsize); | |
1109 | ||
e940478c | 1110 | assert(zns->netlink.sock >= 0); |
19d5a4fe DS |
1111 | netlink_install_filter(zns->netlink.sock, |
1112 | zns->netlink_cmd.snl.nl_pid); | |
1113 | zns->t_netlink = NULL; | |
1114 | ||
1115 | thread_add_read(zebrad.master, kernel_read, zns, | |
1116 | zns->netlink.sock, &zns->t_netlink); | |
d62a17ae | 1117 | |
1118 | rt_netlink_init(); | |
1fdc9eae | 1119 | } |
1120 | ||
d62a17ae | 1121 | void kernel_terminate(struct zebra_ns *zns) |
1fdc9eae | 1122 | { |
d62a17ae | 1123 | THREAD_READ_OFF(zns->t_netlink); |
1124 | ||
1125 | if (zns->netlink.sock >= 0) { | |
1126 | close(zns->netlink.sock); | |
1127 | zns->netlink.sock = -1; | |
1128 | } | |
1129 | ||
1130 | if (zns->netlink_cmd.sock >= 0) { | |
1131 | close(zns->netlink_cmd.sock); | |
1132 | zns->netlink_cmd.sock = -1; | |
1133 | } | |
1fdc9eae | 1134 | } |
ddfeb486 DL |
1135 | |
1136 | #endif /* HAVE_NETLINK */ |