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