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