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