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