]>
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 | ||
ddfeb486 DL |
23 | #ifdef HAVE_NETLINK |
24 | ||
1fdc9eae | 25 | #include "linklist.h" |
26 | #include "if.h" | |
27 | #include "log.h" | |
28 | #include "prefix.h" | |
29 | #include "connected.h" | |
30 | #include "table.h" | |
31 | #include "memory.h" | |
32 | #include "zebra_memory.h" | |
33 | #include "rib.h" | |
34 | #include "thread.h" | |
35 | #include "privs.h" | |
36 | #include "nexthop.h" | |
37 | #include "vrf.h" | |
38 | #include "mpls.h" | |
174482ef | 39 | #include "lib_errors.h" |
1fdc9eae | 40 | |
3801e764 DS |
41 | //#include "zebra/zserv.h" |
42 | #include "zebra/zebra_router.h" | |
1fdc9eae | 43 | #include "zebra/zebra_ns.h" |
44 | #include "zebra/zebra_vrf.h" | |
05f7f5db | 45 | #include "zebra/rt.h" |
1fdc9eae | 46 | #include "zebra/debug.h" |
47 | #include "zebra/kernel_netlink.h" | |
48 | #include "zebra/rt_netlink.h" | |
49 | #include "zebra/if_netlink.h" | |
942bf97b | 50 | #include "zebra/rule_netlink.h" |
43e52561 | 51 | #include "zebra/zebra_errors.h" |
1fdc9eae | 52 | |
53 | #ifndef SO_RCVBUFFORCE | |
54 | #define SO_RCVBUFFORCE (33) | |
55 | #endif | |
56 | ||
57 | /* Hack for GNU libc version 2. */ | |
58 | #ifndef MSG_TRUNC | |
59 | #define MSG_TRUNC 0x20 | |
60 | #endif /* MSG_TRUNC */ | |
61 | ||
62 | #ifndef NLMSG_TAIL | |
d62a17ae | 63 | #define NLMSG_TAIL(nmsg) \ |
d7c0a89a QY |
64 | ((struct rtattr *)(((uint8_t *)(nmsg)) \ |
65 | + NLMSG_ALIGN((nmsg)->nlmsg_len))) | |
1fdc9eae | 66 | #endif |
67 | ||
68 | #ifndef RTA_TAIL | |
d62a17ae | 69 | #define RTA_TAIL(rta) \ |
d7c0a89a | 70 | ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len))) |
1fdc9eae | 71 | #endif |
72 | ||
f909c673 DS |
73 | #ifndef RTNL_FAMILY_IP6MR |
74 | #define RTNL_FAMILY_IP6MR 129 | |
75 | #endif | |
76 | ||
77 | #ifndef RTPROT_MROUTED | |
78 | #define RTPROT_MROUTED 17 | |
79 | #endif | |
80 | ||
531c92b8 | 81 | #define NL_DEFAULT_BATCH_BUFSIZE (16 * NL_PKT_BUF_SIZE) |
e63c7622 JU |
82 | |
83 | /* | |
84 | * We limit the batch's size to a number smaller than the length of the | |
85 | * underlying buffer since the last message that wouldn't fit the batch would go | |
86 | * over the upper boundary and then it would have to be encoded again into a new | |
87 | * buffer. If the difference between the limit and the length of the buffer is | |
88 | * big enough (bigger than the biggest Netlink message) then this situation | |
89 | * won't occur. | |
90 | */ | |
531c92b8 JU |
91 | #define NL_DEFAULT_BATCH_SEND_THRESHOLD (15 * NL_PKT_BUF_SIZE) |
92 | ||
2f9dbd3a | 93 | #define NL_BATCH_RX_BUFSIZE NL_RCV_PKT_BUF_SIZE |
e63c7622 | 94 | |
d62a17ae | 95 | static const struct message nlmsg_str[] = {{RTM_NEWROUTE, "RTM_NEWROUTE"}, |
96 | {RTM_DELROUTE, "RTM_DELROUTE"}, | |
97 | {RTM_GETROUTE, "RTM_GETROUTE"}, | |
98 | {RTM_NEWLINK, "RTM_NEWLINK"}, | |
99 | {RTM_DELLINK, "RTM_DELLINK"}, | |
100 | {RTM_GETLINK, "RTM_GETLINK"}, | |
101 | {RTM_NEWADDR, "RTM_NEWADDR"}, | |
102 | {RTM_DELADDR, "RTM_DELADDR"}, | |
103 | {RTM_GETADDR, "RTM_GETADDR"}, | |
104 | {RTM_NEWNEIGH, "RTM_NEWNEIGH"}, | |
105 | {RTM_DELNEIGH, "RTM_DELNEIGH"}, | |
106 | {RTM_GETNEIGH, "RTM_GETNEIGH"}, | |
942bf97b | 107 | {RTM_NEWRULE, "RTM_NEWRULE"}, |
108 | {RTM_DELRULE, "RTM_DELRULE"}, | |
109 | {RTM_GETRULE, "RTM_GETRULE"}, | |
79580b5a SW |
110 | {RTM_NEWNEXTHOP, "RTM_NEWNEXTHOP"}, |
111 | {RTM_DELNEXTHOP, "RTM_DELNEXTHOP"}, | |
112 | {RTM_GETNEXTHOP, "RTM_GETNEXTHOP"}, | |
d62a17ae | 113 | {0}}; |
1fdc9eae | 114 | |
115 | static const struct message rtproto_str[] = { | |
d62a17ae | 116 | {RTPROT_REDIRECT, "redirect"}, |
117 | {RTPROT_KERNEL, "kernel"}, | |
118 | {RTPROT_BOOT, "boot"}, | |
119 | {RTPROT_STATIC, "static"}, | |
120 | {RTPROT_GATED, "GateD"}, | |
121 | {RTPROT_RA, "router advertisement"}, | |
122 | {RTPROT_MRT, "MRT"}, | |
123 | {RTPROT_ZEBRA, "Zebra"}, | |
1fdc9eae | 124 | #ifdef RTPROT_BIRD |
d62a17ae | 125 | {RTPROT_BIRD, "BIRD"}, |
1fdc9eae | 126 | #endif /* RTPROT_BIRD */ |
d62a17ae | 127 | {RTPROT_MROUTED, "mroute"}, |
128 | {RTPROT_BGP, "BGP"}, | |
129 | {RTPROT_OSPF, "OSPF"}, | |
130 | {RTPROT_ISIS, "IS-IS"}, | |
131 | {RTPROT_RIP, "RIP"}, | |
132 | {RTPROT_RIPNG, "RIPNG"}, | |
d4d71f11 | 133 | {RTPROT_ZSTATIC, "static"}, |
d62a17ae | 134 | {0}}; |
135 | ||
136 | static const struct message family_str[] = {{AF_INET, "ipv4"}, | |
137 | {AF_INET6, "ipv6"}, | |
138 | {AF_BRIDGE, "bridge"}, | |
139 | {RTNL_FAMILY_IPMR, "ipv4MR"}, | |
140 | {RTNL_FAMILY_IP6MR, "ipv6MR"}, | |
141 | {0}}; | |
142 | ||
8c8f250b DS |
143 | static const struct message rttype_str[] = {{RTN_UNSPEC, "none"}, |
144 | {RTN_UNICAST, "unicast"}, | |
145 | {RTN_LOCAL, "local"}, | |
146 | {RTN_BROADCAST, "broadcast"}, | |
147 | {RTN_ANYCAST, "anycast"}, | |
d62a17ae | 148 | {RTN_MULTICAST, "multicast"}, |
8c8f250b DS |
149 | {RTN_BLACKHOLE, "blackhole"}, |
150 | {RTN_UNREACHABLE, "unreachable"}, | |
151 | {RTN_PROHIBIT, "prohibited"}, | |
152 | {RTN_THROW, "throw"}, | |
153 | {RTN_NAT, "nat"}, | |
154 | {RTN_XRESOLVE, "resolver"}, | |
d62a17ae | 155 | {0}}; |
b339bde7 | 156 | |
1fdc9eae | 157 | extern struct thread_master *master; |
d7c0a89a | 158 | extern uint32_t nl_rcvbufsize; |
1fdc9eae | 159 | |
160 | extern struct zebra_privs_t zserv_privs; | |
161 | ||
531c92b8 JU |
162 | DEFINE_MTYPE_STATIC(ZEBRA, NL_BUF, "Zebra Netlink buffers") |
163 | ||
164 | size_t nl_batch_tx_bufsize; | |
165 | char *nl_batch_tx_buf; | |
166 | ||
e63c7622 JU |
167 | char nl_batch_rx_buf[NL_BATCH_RX_BUFSIZE]; |
168 | ||
531c92b8 JU |
169 | _Atomic uint32_t nl_batch_bufsize = NL_DEFAULT_BATCH_BUFSIZE; |
170 | _Atomic uint32_t nl_batch_send_threshold = NL_DEFAULT_BATCH_SEND_THRESHOLD; | |
171 | ||
e63c7622 JU |
172 | struct nl_batch { |
173 | void *buf; | |
174 | size_t bufsiz; | |
175 | size_t limit; | |
176 | ||
177 | void *buf_head; | |
178 | size_t curlen; | |
179 | size_t msgcnt; | |
180 | ||
181 | const struct zebra_dplane_info *zns; | |
e63c7622 | 182 | |
f6feb48b | 183 | struct dplane_ctx_q ctx_list; |
e63c7622 | 184 | |
f6feb48b JU |
185 | /* |
186 | * Pointer to the queue of completed contexts outbound back | |
187 | * towards the dataplane module. | |
188 | */ | |
189 | struct dplane_ctx_q *ctx_out_q; | |
e63c7622 JU |
190 | }; |
191 | ||
531c92b8 JU |
192 | int netlink_config_write_helper(struct vty *vty) |
193 | { | |
194 | uint32_t size = | |
195 | atomic_load_explicit(&nl_batch_bufsize, memory_order_relaxed); | |
196 | uint32_t threshold = atomic_load_explicit(&nl_batch_send_threshold, | |
197 | memory_order_relaxed); | |
198 | ||
199 | if (size != NL_DEFAULT_BATCH_BUFSIZE | |
200 | || threshold != NL_DEFAULT_BATCH_SEND_THRESHOLD) | |
201 | vty_out(vty, "zebra kernel netlink batch-tx-buf %u %u\n", size, | |
202 | threshold); | |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
207 | void netlink_set_batch_buffer_size(uint32_t size, uint32_t threshold, bool set) | |
208 | { | |
209 | if (!set) { | |
210 | size = NL_DEFAULT_BATCH_BUFSIZE; | |
211 | threshold = NL_DEFAULT_BATCH_SEND_THRESHOLD; | |
212 | } | |
213 | ||
214 | atomic_store_explicit(&nl_batch_bufsize, size, memory_order_relaxed); | |
215 | atomic_store_explicit(&nl_batch_send_threshold, threshold, | |
216 | memory_order_relaxed); | |
217 | } | |
218 | ||
2414abd3 | 219 | int netlink_talk_filter(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
1fdc9eae | 220 | { |
3575d9e8 DS |
221 | /* |
222 | * This is an error condition that must be handled during | |
223 | * development. | |
224 | * | |
225 | * The netlink_talk_filter function is used for communication | |
226 | * down the netlink_cmd pipe and we are expecting | |
227 | * an ack being received. So if we get here | |
228 | * then we did not receive the ack and instead | |
229 | * received some other message in an unexpected | |
230 | * way. | |
231 | */ | |
43e52561 QY |
232 | zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__, |
233 | h->nlmsg_type, nl_msg_type_to_str(h->nlmsg_type), ns_id); | |
d62a17ae | 234 | return 0; |
1fdc9eae | 235 | } |
236 | ||
d62a17ae | 237 | static int netlink_recvbuf(struct nlsock *nl, uint32_t newsize) |
1fdc9eae | 238 | { |
d7c0a89a | 239 | uint32_t oldsize; |
d62a17ae | 240 | socklen_t newlen = sizeof(newsize); |
241 | socklen_t oldlen = sizeof(oldsize); | |
242 | int ret; | |
243 | ||
244 | ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen); | |
245 | if (ret < 0) { | |
450971aa | 246 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
247 | "Can't get %s receive buffer size: %s", nl->name, |
248 | safe_strerror(errno)); | |
d62a17ae | 249 | return -1; |
250 | } | |
251 | ||
252 | /* Try force option (linux >= 2.6.14) and fall back to normal set */ | |
0cf6db21 | 253 | frr_with_privs(&zserv_privs) { |
01b9e3fd DL |
254 | ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE, |
255 | &nl_rcvbufsize, | |
256 | sizeof(nl_rcvbufsize)); | |
257 | } | |
d62a17ae | 258 | if (ret < 0) |
259 | ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, | |
260 | &nl_rcvbufsize, sizeof(nl_rcvbufsize)); | |
261 | if (ret < 0) { | |
450971aa | 262 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
263 | "Can't set %s receive buffer size: %s", nl->name, |
264 | safe_strerror(errno)); | |
d62a17ae | 265 | return -1; |
266 | } | |
267 | ||
268 | ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen); | |
269 | if (ret < 0) { | |
450971aa | 270 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
271 | "Can't get %s receive buffer size: %s", nl->name, |
272 | safe_strerror(errno)); | |
d62a17ae | 273 | return -1; |
274 | } | |
275 | ||
276 | zlog_info("Setting netlink socket receive buffer size: %u -> %u", | |
277 | oldsize, newsize); | |
278 | return 0; | |
1fdc9eae | 279 | } |
280 | ||
281 | /* Make socket for Linux netlink interface. */ | |
d62a17ae | 282 | static int netlink_socket(struct nlsock *nl, unsigned long groups, |
283 | ns_id_t ns_id) | |
1fdc9eae | 284 | { |
d62a17ae | 285 | int ret; |
286 | struct sockaddr_nl snl; | |
287 | int sock; | |
288 | int namelen; | |
d62a17ae | 289 | |
0cf6db21 | 290 | frr_with_privs(&zserv_privs) { |
6bb30c2c DL |
291 | sock = ns_socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE, ns_id); |
292 | if (sock < 0) { | |
293 | zlog_err("Can't open %s socket: %s", nl->name, | |
294 | safe_strerror(errno)); | |
295 | return -1; | |
296 | } | |
d62a17ae | 297 | |
0d6f7fd6 | 298 | memset(&snl, 0, sizeof(snl)); |
6bb30c2c DL |
299 | snl.nl_family = AF_NETLINK; |
300 | snl.nl_groups = groups; | |
d62a17ae | 301 | |
6bb30c2c | 302 | /* Bind the socket to the netlink structure for anything. */ |
0d6f7fd6 | 303 | ret = bind(sock, (struct sockaddr *)&snl, sizeof(snl)); |
6bb30c2c | 304 | } |
d62a17ae | 305 | |
306 | if (ret < 0) { | |
6bb30c2c DL |
307 | zlog_err("Can't bind %s socket to group 0x%x: %s", nl->name, |
308 | snl.nl_groups, safe_strerror(errno)); | |
d62a17ae | 309 | close(sock); |
310 | return -1; | |
311 | } | |
312 | ||
313 | /* multiple netlink sockets will have different nl_pid */ | |
0d6f7fd6 | 314 | namelen = sizeof(snl); |
d62a17ae | 315 | ret = getsockname(sock, (struct sockaddr *)&snl, (socklen_t *)&namelen); |
0d6f7fd6 | 316 | if (ret < 0 || namelen != sizeof(snl)) { |
450971aa | 317 | flog_err_sys(EC_LIB_SOCKET, "Can't get %s socket name: %s", |
09c866e3 | 318 | nl->name, safe_strerror(errno)); |
d62a17ae | 319 | close(sock); |
320 | return -1; | |
321 | } | |
322 | ||
323 | nl->snl = snl; | |
324 | nl->sock = sock; | |
325 | return ret; | |
1fdc9eae | 326 | } |
327 | ||
2414abd3 | 328 | static int netlink_information_fetch(struct nlmsghdr *h, ns_id_t ns_id, |
d62a17ae | 329 | int startup) |
1fdc9eae | 330 | { |
3575d9e8 DS |
331 | /* |
332 | * When we handle new message types here | |
333 | * because we are starting to install them | |
334 | * then lets check the netlink_install_filter | |
335 | * and see if we should add the corresponding | |
336 | * allow through entry there. | |
337 | * Probably not needed to do but please | |
338 | * think about it. | |
339 | */ | |
d62a17ae | 340 | switch (h->nlmsg_type) { |
341 | case RTM_NEWROUTE: | |
2414abd3 | 342 | return netlink_route_change(h, ns_id, startup); |
d62a17ae | 343 | case RTM_DELROUTE: |
2414abd3 | 344 | return netlink_route_change(h, ns_id, startup); |
d62a17ae | 345 | case RTM_NEWLINK: |
2414abd3 | 346 | return netlink_link_change(h, ns_id, startup); |
d62a17ae | 347 | case RTM_DELLINK: |
2414abd3 | 348 | return netlink_link_change(h, ns_id, startup); |
d62a17ae | 349 | case RTM_NEWADDR: |
2414abd3 | 350 | return netlink_interface_addr(h, ns_id, startup); |
d62a17ae | 351 | case RTM_DELADDR: |
2414abd3 | 352 | return netlink_interface_addr(h, ns_id, startup); |
d62a17ae | 353 | case RTM_NEWNEIGH: |
2414abd3 | 354 | return netlink_neigh_change(h, ns_id); |
d62a17ae | 355 | case RTM_DELNEIGH: |
2414abd3 | 356 | return netlink_neigh_change(h, ns_id); |
951f8bcb DS |
357 | case RTM_GETNEIGH: |
358 | /* | |
359 | * Kernel in some situations when it expects | |
360 | * user space to resolve arp entries, we will | |
361 | * receive this notification. As we don't | |
362 | * need this notification and as that | |
363 | * we don't want to spam the log file with | |
364 | * below messages, just ignore. | |
365 | */ | |
366 | if (IS_ZEBRA_DEBUG_KERNEL) | |
367 | zlog_debug("Received RTM_GETNEIGH, ignoring"); | |
368 | break; | |
942bf97b | 369 | case RTM_NEWRULE: |
2414abd3 | 370 | return netlink_rule_change(h, ns_id, startup); |
942bf97b | 371 | case RTM_DELRULE: |
2414abd3 | 372 | return netlink_rule_change(h, ns_id, startup); |
79580b5a | 373 | case RTM_NEWNEXTHOP: |
d9f5b2f5 | 374 | return netlink_nexthop_change(h, ns_id, startup); |
79580b5a | 375 | case RTM_DELNEXTHOP: |
d9f5b2f5 | 376 | return netlink_nexthop_change(h, ns_id, startup); |
d62a17ae | 377 | default: |
3575d9e8 DS |
378 | /* |
379 | * If we have received this message then | |
380 | * we have made a mistake during development | |
381 | * and we need to write some code to handle | |
382 | * this message type or not ask for | |
383 | * it to be sent up to us | |
384 | */ | |
e914ccbe | 385 | flog_err(EC_ZEBRA_UNKNOWN_NLMSG, |
1c50c1c0 QY |
386 | "Unknown netlink nlmsg_type %s(%d) vrf %u\n", |
387 | nl_msg_type_to_str(h->nlmsg_type), h->nlmsg_type, | |
388 | ns_id); | |
d62a17ae | 389 | break; |
390 | } | |
391 | return 0; | |
1fdc9eae | 392 | } |
393 | ||
d62a17ae | 394 | static int kernel_read(struct thread *thread) |
1fdc9eae | 395 | { |
d62a17ae | 396 | struct zebra_ns *zns = (struct zebra_ns *)THREAD_ARG(thread); |
85a75f1e MS |
397 | struct zebra_dplane_info dp_info; |
398 | ||
399 | /* Capture key info from ns struct */ | |
400 | zebra_dplane_info_from_zns(&dp_info, zns, false); | |
401 | ||
402 | netlink_parse_info(netlink_information_fetch, &zns->netlink, &dp_info, | |
403 | 5, 0); | |
d62a17ae | 404 | zns->t_netlink = NULL; |
3801e764 | 405 | thread_add_read(zrouter.master, kernel_read, zns, zns->netlink.sock, |
d62a17ae | 406 | &zns->t_netlink); |
1fdc9eae | 407 | |
d62a17ae | 408 | return 0; |
1fdc9eae | 409 | } |
410 | ||
3575d9e8 DS |
411 | /* |
412 | * Filter out messages from self that occur on listener socket, | |
62b8bb7a | 413 | * caused by our actions on the command socket(s) |
3575d9e8 DS |
414 | * |
415 | * When we add new Netlink message types we probably | |
416 | * do not need to add them here as that we are filtering | |
417 | * on the routes we actually care to receive( which is rarer | |
418 | * then the normal course of operations). We are intentionally | |
419 | * allowing some messages from ourselves through | |
420 | * ( I'm looking at you Interface based netlink messages ) | |
421 | * so that we only had to write one way to handle incoming | |
422 | * address add/delete changes. | |
1fdc9eae | 423 | */ |
62b8bb7a | 424 | static void netlink_install_filter(int sock, __u32 pid, __u32 dplane_pid) |
1fdc9eae | 425 | { |
3575d9e8 DS |
426 | /* |
427 | * BPF_JUMP instructions and where you jump to are based upon | |
428 | * 0 as being the next statement. So count from 0. Writing | |
429 | * this down because every time I look at this I have to | |
430 | * re-remember it. | |
431 | */ | |
d62a17ae | 432 | struct sock_filter filter[] = { |
3575d9e8 DS |
433 | /* |
434 | * Logic: | |
62b8bb7a MS |
435 | * if (nlmsg_pid == pid || |
436 | * nlmsg_pid == dplane_pid) { | |
3575d9e8 DS |
437 | * if (the incoming nlmsg_type == |
438 | * RTM_NEWADDR | RTM_DELADDR) | |
439 | * keep this message | |
440 | * else | |
441 | * skip this message | |
442 | * } else | |
443 | * keep this netlink message | |
444 | */ | |
445 | /* | |
446 | * 0: Load the nlmsg_pid into the BPF register | |
447 | */ | |
d62a17ae | 448 | BPF_STMT(BPF_LD | BPF_ABS | BPF_W, |
449 | offsetof(struct nlmsghdr, nlmsg_pid)), | |
3575d9e8 DS |
450 | /* |
451 | * 1: Compare to pid | |
452 | */ | |
62b8bb7a | 453 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(pid), 1, 0), |
3575d9e8 | 454 | /* |
62b8bb7a MS |
455 | * 2: Compare to dplane pid |
456 | */ | |
457 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(dplane_pid), 0, 4), | |
458 | /* | |
459 | * 3: Load the nlmsg_type into BPF register | |
3575d9e8 DS |
460 | */ |
461 | BPF_STMT(BPF_LD | BPF_ABS | BPF_H, | |
462 | offsetof(struct nlmsghdr, nlmsg_type)), | |
463 | /* | |
62b8bb7a | 464 | * 4: Compare to RTM_NEWADDR |
3575d9e8 DS |
465 | */ |
466 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_NEWADDR), 2, 0), | |
467 | /* | |
62b8bb7a | 468 | * 5: Compare to RTM_DELADDR |
3575d9e8 DS |
469 | */ |
470 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_DELADDR), 1, 0), | |
471 | /* | |
62b8bb7a | 472 | * 6: This is the end state of we want to skip the |
3575d9e8 DS |
473 | * message |
474 | */ | |
d62a17ae | 475 | BPF_STMT(BPF_RET | BPF_K, 0), |
62b8bb7a | 476 | /* 7: This is the end state of we want to keep |
3575d9e8 DS |
477 | * the message |
478 | */ | |
d62a17ae | 479 | BPF_STMT(BPF_RET | BPF_K, 0xffff), |
480 | }; | |
481 | ||
482 | struct sock_fprog prog = { | |
9d303b37 | 483 | .len = array_size(filter), .filter = filter, |
d62a17ae | 484 | }; |
485 | ||
486 | if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)) | |
487 | < 0) | |
1c50c1c0 | 488 | flog_err_sys(EC_LIB_SOCKET, "Can't install socket filter: %s\n", |
9df414fe | 489 | safe_strerror(errno)); |
1fdc9eae | 490 | } |
491 | ||
d62a17ae | 492 | void netlink_parse_rtattr(struct rtattr **tb, int max, struct rtattr *rta, |
493 | int len) | |
1fdc9eae | 494 | { |
d62a17ae | 495 | while (RTA_OK(rta, len)) { |
496 | if (rta->rta_type <= max) | |
497 | tb[rta->rta_type] = rta; | |
498 | rta = RTA_NEXT(rta, len); | |
499 | } | |
1fdc9eae | 500 | } |
501 | ||
87da6a60 SW |
502 | /** |
503 | * netlink_parse_rtattr_nested() - Parses a nested route attribute | |
504 | * @tb: Pointer to array for storing rtattr in. | |
505 | * @max: Max number to store. | |
506 | * @rta: Pointer to rtattr to look for nested items in. | |
507 | */ | |
508 | void netlink_parse_rtattr_nested(struct rtattr **tb, int max, | |
509 | struct rtattr *rta) | |
510 | { | |
511 | netlink_parse_rtattr(tb, max, RTA_DATA(rta), RTA_PAYLOAD(rta)); | |
512 | } | |
513 | ||
312a6bee JU |
514 | bool nl_attr_put(struct nlmsghdr *n, unsigned int maxlen, int type, |
515 | const void *data, unsigned int alen) | |
1fdc9eae | 516 | { |
d62a17ae | 517 | int len; |
518 | struct rtattr *rta; | |
1fdc9eae | 519 | |
d62a17ae | 520 | len = RTA_LENGTH(alen); |
1fdc9eae | 521 | |
d62a17ae | 522 | if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) |
312a6bee | 523 | return false; |
1fdc9eae | 524 | |
d62a17ae | 525 | rta = (struct rtattr *)(((char *)n) + NLMSG_ALIGN(n->nlmsg_len)); |
526 | rta->rta_type = type; | |
527 | rta->rta_len = len; | |
4b2792b5 | 528 | |
d62a17ae | 529 | if (data) |
530 | memcpy(RTA_DATA(rta), data, alen); | |
531 | else | |
532 | assert(alen == 0); | |
4b2792b5 | 533 | |
d62a17ae | 534 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len); |
1fdc9eae | 535 | |
312a6bee | 536 | return true; |
1fdc9eae | 537 | } |
538 | ||
312a6bee JU |
539 | bool nl_attr_put16(struct nlmsghdr *n, unsigned int maxlen, int type, |
540 | uint16_t data) | |
bbc16902 | 541 | { |
312a6bee | 542 | return nl_attr_put(n, maxlen, type, &data, sizeof(uint16_t)); |
bbc16902 | 543 | } |
544 | ||
312a6bee JU |
545 | bool nl_attr_put32(struct nlmsghdr *n, unsigned int maxlen, int type, |
546 | uint32_t data) | |
1fdc9eae | 547 | { |
312a6bee | 548 | return nl_attr_put(n, maxlen, type, &data, sizeof(uint32_t)); |
1fdc9eae | 549 | } |
550 | ||
312a6bee | 551 | struct rtattr *nl_attr_nest(struct nlmsghdr *n, unsigned int maxlen, int type) |
1fdc9eae | 552 | { |
d62a17ae | 553 | struct rtattr *nest = NLMSG_TAIL(n); |
1fdc9eae | 554 | |
312a6bee JU |
555 | if (!nl_attr_put(n, maxlen, type, NULL, 0)) |
556 | return NULL; | |
557 | ||
40d86eba | 558 | nest->rta_type |= NLA_F_NESTED; |
d62a17ae | 559 | return nest; |
1fdc9eae | 560 | } |
561 | ||
312a6bee | 562 | int nl_attr_nest_end(struct nlmsghdr *n, struct rtattr *nest) |
1fdc9eae | 563 | { |
d7c0a89a | 564 | nest->rta_len = (uint8_t *)NLMSG_TAIL(n) - (uint8_t *)nest; |
d62a17ae | 565 | return n->nlmsg_len; |
1fdc9eae | 566 | } |
567 | ||
312a6bee | 568 | struct rtnexthop *nl_attr_rtnh(struct nlmsghdr *n, unsigned int maxlen) |
1fdc9eae | 569 | { |
312a6bee | 570 | struct rtnexthop *rtnh = (struct rtnexthop *)NLMSG_TAIL(n); |
1fdc9eae | 571 | |
312a6bee JU |
572 | if (NLMSG_ALIGN(n->nlmsg_len) + RTNH_ALIGN(sizeof(struct rtnexthop)) |
573 | > maxlen) | |
574 | return NULL; | |
575 | ||
576 | memset(rtnh, 0, sizeof(struct rtnexthop)); | |
577 | n->nlmsg_len = | |
578 | NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(sizeof(struct rtnexthop)); | |
579 | ||
580 | return rtnh; | |
1fdc9eae | 581 | } |
582 | ||
312a6bee | 583 | void nl_attr_rtnh_end(struct nlmsghdr *n, struct rtnexthop *rtnh) |
1fdc9eae | 584 | { |
312a6bee | 585 | rtnh->rtnh_len = (uint8_t *)NLMSG_TAIL(n) - (uint8_t *)rtnh; |
1fdc9eae | 586 | } |
587 | ||
d62a17ae | 588 | const char *nl_msg_type_to_str(uint16_t msg_type) |
1fdc9eae | 589 | { |
d62a17ae | 590 | return lookup_msg(nlmsg_str, msg_type, ""); |
1fdc9eae | 591 | } |
592 | ||
d7c0a89a | 593 | const char *nl_rtproto_to_str(uint8_t rtproto) |
1fdc9eae | 594 | { |
d62a17ae | 595 | return lookup_msg(rtproto_str, rtproto, ""); |
1fdc9eae | 596 | } |
b339bde7 | 597 | |
d7c0a89a | 598 | const char *nl_family_to_str(uint8_t family) |
b339bde7 | 599 | { |
d62a17ae | 600 | return lookup_msg(family_str, family, ""); |
b339bde7 DS |
601 | } |
602 | ||
d7c0a89a | 603 | const char *nl_rttype_to_str(uint8_t rttype) |
b339bde7 | 604 | { |
d62a17ae | 605 | return lookup_msg(rttype_str, rttype, ""); |
b339bde7 DS |
606 | } |
607 | ||
4cebb2b6 | 608 | #define NLA_OK(nla, len) \ |
5d307d5d DS |
609 | ((len) >= (int)sizeof(struct nlattr) \ |
610 | && (nla)->nla_len >= sizeof(struct nlattr) \ | |
611 | && (nla)->nla_len <= (len)) | |
4cebb2b6 SW |
612 | #define NLA_NEXT(nla, attrlen) \ |
613 | ((attrlen) -= NLA_ALIGN((nla)->nla_len), \ | |
614 | (struct nlattr *)(((char *)(nla)) + NLA_ALIGN((nla)->nla_len))) | |
615 | #define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len)) | |
616 | #define NLA_DATA(nla) ((struct nlattr *)(((char *)(nla)) + NLA_LENGTH(0))) | |
617 | ||
618 | #define ERR_NLA(err, inner_len) \ | |
619 | ((struct nlattr *)(((char *)(err)) \ | |
620 | + NLMSG_ALIGN(sizeof(struct nlmsgerr)) \ | |
621 | + NLMSG_ALIGN((inner_len)))) | |
5d307d5d DS |
622 | |
623 | static void netlink_parse_nlattr(struct nlattr **tb, int max, | |
624 | struct nlattr *nla, int len) | |
625 | { | |
4cebb2b6 | 626 | while (NLA_OK(nla, len)) { |
5d307d5d DS |
627 | if (nla->nla_type <= max) |
628 | tb[nla->nla_type] = nla; | |
4cebb2b6 | 629 | nla = NLA_NEXT(nla, len); |
5d307d5d DS |
630 | } |
631 | } | |
632 | ||
633 | static void netlink_parse_extended_ack(struct nlmsghdr *h) | |
634 | { | |
4cebb2b6 SW |
635 | struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {}; |
636 | const struct nlmsgerr *err = (const struct nlmsgerr *)NLMSG_DATA(h); | |
5d307d5d | 637 | const struct nlmsghdr *err_nlh = NULL; |
4cebb2b6 SW |
638 | /* Length not including nlmsghdr */ |
639 | uint32_t len = 0; | |
640 | /* Inner error netlink message length */ | |
641 | uint32_t inner_len = 0; | |
5d307d5d DS |
642 | const char *msg = NULL; |
643 | uint32_t off = 0; | |
644 | ||
645 | if (!(h->nlmsg_flags & NLM_F_CAPPED)) | |
4cebb2b6 SW |
646 | inner_len = (uint32_t)NLMSG_PAYLOAD(&err->msg, 0); |
647 | ||
648 | len = (uint32_t)(NLMSG_PAYLOAD(h, sizeof(struct nlmsgerr)) - inner_len); | |
5d307d5d | 649 | |
4cebb2b6 SW |
650 | netlink_parse_nlattr(tb, NLMSGERR_ATTR_MAX, ERR_NLA(err, inner_len), |
651 | len); | |
5d307d5d DS |
652 | |
653 | if (tb[NLMSGERR_ATTR_MSG]) | |
4cebb2b6 | 654 | msg = (const char *)NLA_DATA(tb[NLMSGERR_ATTR_MSG]); |
5d307d5d DS |
655 | |
656 | if (tb[NLMSGERR_ATTR_OFFS]) { | |
4cebb2b6 | 657 | off = *(uint32_t *)NLA_DATA(tb[NLMSGERR_ATTR_OFFS]); |
5d307d5d DS |
658 | |
659 | if (off > h->nlmsg_len) { | |
9165c5f5 | 660 | zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS"); |
5d307d5d DS |
661 | } else if (!(h->nlmsg_flags & NLM_F_CAPPED)) { |
662 | /* | |
663 | * Header of failed message | |
664 | * we are not doing anything currently with it | |
665 | * but noticing it for later. | |
666 | */ | |
667 | err_nlh = &err->msg; | |
15569c58 | 668 | zlog_debug("%s: Received %s extended Ack", __func__, |
87b5d1b0 | 669 | nl_msg_type_to_str(err_nlh->nlmsg_type)); |
5d307d5d DS |
670 | } |
671 | } | |
672 | ||
673 | if (msg && *msg != '\0') { | |
674 | bool is_err = !!err->error; | |
675 | ||
676 | if (is_err) | |
677 | zlog_err("Extended Error: %s", msg); | |
678 | else | |
e914ccbe | 679 | flog_warn(EC_ZEBRA_NETLINK_EXTENDED_WARNING, |
9df414fe | 680 | "Extended Warning: %s", msg); |
5d307d5d DS |
681 | } |
682 | } | |
683 | ||
ae6138bf JU |
684 | /* |
685 | * netlink_send_msg - send a netlink message of a certain size. | |
686 | * | |
687 | * Returns -1 on error. Otherwise, it returns the number of bytes sent. | |
688 | */ | |
f8653393 JU |
689 | static ssize_t netlink_send_msg(const struct nlsock *nl, void *buf, |
690 | size_t buflen) | |
ae6138bf | 691 | { |
f8653393 JU |
692 | struct sockaddr_nl snl = {}; |
693 | struct iovec iov = {}; | |
694 | struct msghdr msg = {}; | |
695 | ssize_t status; | |
696 | int save_errno = 0; | |
ae6138bf JU |
697 | |
698 | iov.iov_base = buf; | |
699 | iov.iov_len = buflen; | |
f8653393 | 700 | msg.msg_name = &snl; |
ae6138bf JU |
701 | msg.msg_namelen = sizeof(snl); |
702 | msg.msg_iov = &iov; | |
703 | msg.msg_iovlen = 1; | |
704 | ||
705 | snl.nl_family = AF_NETLINK; | |
706 | ||
707 | /* Send message to netlink interface. */ | |
708 | frr_with_privs(&zserv_privs) { | |
709 | status = sendmsg(nl->sock, &msg, 0); | |
710 | save_errno = errno; | |
711 | } | |
712 | ||
713 | if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND) { | |
714 | zlog_debug("%s: >> netlink message dump [sent]", __func__); | |
715 | zlog_hexdump(buf, buflen); | |
716 | } | |
717 | ||
f8653393 | 718 | if (status == -1) { |
ae6138bf JU |
719 | flog_err_sys(EC_LIB_SOCKET, "%s error: %s", __func__, |
720 | safe_strerror(save_errno)); | |
721 | return -1; | |
722 | } | |
723 | ||
724 | return status; | |
725 | } | |
726 | ||
727 | /* | |
728 | * netlink_recv_msg - receive a netlink message. | |
729 | * | |
730 | * Returns -1 on error, 0 if read would block or the number of bytes received. | |
731 | */ | |
732 | static int netlink_recv_msg(const struct nlsock *nl, struct msghdr msg, | |
733 | void *buf, size_t buflen) | |
734 | { | |
735 | struct iovec iov; | |
736 | int status; | |
737 | ||
738 | iov.iov_base = buf; | |
739 | iov.iov_len = buflen; | |
740 | msg.msg_iov = &iov; | |
741 | msg.msg_iovlen = 1; | |
742 | ||
743 | do { | |
ae6138bf | 744 | status = recvmsg(nl->sock, &msg, 0); |
f8653393 | 745 | } while (status == -1 && errno == EINTR); |
ae6138bf | 746 | |
f8653393 | 747 | if (status == -1) { |
ae6138bf JU |
748 | if (errno == EWOULDBLOCK || errno == EAGAIN) |
749 | return 0; | |
750 | flog_err(EC_ZEBRA_RECVMSG_OVERRUN, "%s recvmsg overrun: %s", | |
751 | nl->name, safe_strerror(errno)); | |
752 | /* | |
753 | * In this case we are screwed. There is no good way to recover | |
754 | * zebra at this point. | |
755 | */ | |
756 | exit(-1); | |
757 | } | |
758 | ||
759 | if (status == 0) { | |
760 | flog_err_sys(EC_LIB_SOCKET, "%s EOF", nl->name); | |
761 | return -1; | |
762 | } | |
763 | ||
764 | if (msg.msg_namelen != sizeof(struct sockaddr_nl)) { | |
765 | flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR, | |
766 | "%s sender address length error: length %d", nl->name, | |
767 | msg.msg_namelen); | |
768 | return -1; | |
769 | } | |
770 | ||
771 | if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV) { | |
772 | zlog_debug("%s: << netlink message dump [recv]", __func__); | |
773 | zlog_hexdump(buf, status); | |
774 | } | |
775 | ||
ae6138bf JU |
776 | return status; |
777 | } | |
778 | ||
779 | /* | |
780 | * netlink_parse_error - parse a netlink error message | |
781 | * | |
782 | * Returns 1 if this message is acknowledgement, 0 if this error should be | |
783 | * ignored, -1 otherwise. | |
784 | */ | |
785 | static int netlink_parse_error(const struct nlsock *nl, struct nlmsghdr *h, | |
786 | const struct zebra_dplane_info *zns, | |
787 | bool startup) | |
788 | { | |
789 | struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); | |
790 | int errnum = err->error; | |
791 | int msg_type = err->msg.nlmsg_type; | |
792 | ||
793 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
794 | flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR, | |
795 | "%s error: message truncated", nl->name); | |
796 | return -1; | |
797 | } | |
798 | ||
799 | /* | |
800 | * Parse the extended information before we actually handle it. At this | |
801 | * point in time we do not do anything other than report the issue. | |
802 | */ | |
803 | if (h->nlmsg_flags & NLM_F_ACK_TLVS) | |
804 | netlink_parse_extended_ack(h); | |
805 | ||
806 | /* If the error field is zero, then this is an ACK. */ | |
807 | if (err->error == 0) { | |
808 | if (IS_ZEBRA_DEBUG_KERNEL) { | |
809 | zlog_debug("%s: %s ACK: type=%s(%u), seq=%u, pid=%u", | |
810 | __func__, nl->name, | |
811 | nl_msg_type_to_str(err->msg.nlmsg_type), | |
812 | err->msg.nlmsg_type, err->msg.nlmsg_seq, | |
813 | err->msg.nlmsg_pid); | |
814 | } | |
815 | ||
816 | return 1; | |
817 | } | |
818 | ||
819 | /* Deal with errors that occur because of races in link handling. */ | |
820 | if (zns->is_cmd | |
821 | && ((msg_type == RTM_DELROUTE | |
822 | && (-errnum == ENODEV || -errnum == ESRCH)) | |
823 | || (msg_type == RTM_NEWROUTE | |
824 | && (-errnum == ENETDOWN || -errnum == EEXIST)))) { | |
825 | if (IS_ZEBRA_DEBUG_KERNEL) | |
826 | zlog_debug("%s: error: %s type=%s(%u), seq=%u, pid=%u", | |
827 | nl->name, safe_strerror(-errnum), | |
828 | nl_msg_type_to_str(msg_type), msg_type, | |
829 | err->msg.nlmsg_seq, err->msg.nlmsg_pid); | |
830 | return 0; | |
831 | } | |
832 | ||
833 | /* | |
834 | * We see RTM_DELNEIGH when shutting down an interface with an IPv4 | |
835 | * link-local. The kernel should have already deleted the neighbor so | |
836 | * do not log these as an error. | |
837 | */ | |
838 | if (msg_type == RTM_DELNEIGH | |
839 | || (zns->is_cmd && msg_type == RTM_NEWROUTE | |
840 | && (-errnum == ESRCH || -errnum == ENETUNREACH))) { | |
841 | /* | |
842 | * This is known to happen in some situations, don't log as | |
843 | * error. | |
844 | */ | |
845 | if (IS_ZEBRA_DEBUG_KERNEL) | |
846 | zlog_debug("%s error: %s, type=%s(%u), seq=%u, pid=%u", | |
847 | nl->name, safe_strerror(-errnum), | |
848 | nl_msg_type_to_str(msg_type), msg_type, | |
849 | err->msg.nlmsg_seq, err->msg.nlmsg_pid); | |
850 | } else { | |
851 | if ((msg_type != RTM_GETNEXTHOP) || !startup) | |
852 | flog_err(EC_ZEBRA_UNEXPECTED_MESSAGE, | |
853 | "%s error: %s, type=%s(%u), seq=%u, pid=%u", | |
854 | nl->name, safe_strerror(-errnum), | |
855 | nl_msg_type_to_str(msg_type), msg_type, | |
856 | err->msg.nlmsg_seq, err->msg.nlmsg_pid); | |
857 | } | |
858 | ||
859 | return -1; | |
860 | } | |
861 | ||
936ebf0a DS |
862 | /* |
863 | * netlink_parse_info | |
864 | * | |
865 | * Receive message from netlink interface and pass those information | |
866 | * to the given function. | |
867 | * | |
868 | * filter -> Function to call to read the results | |
869 | * nl -> netlink socket information | |
870 | * zns -> The zebra namespace data | |
871 | * count -> How many we should read in, 0 means as much as possible | |
872 | * startup -> Are we reading in under startup conditions? passed to | |
873 | * the filter. | |
874 | */ | |
2414abd3 | 875 | int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int), |
7cdb1a84 MS |
876 | const struct nlsock *nl, |
877 | const struct zebra_dplane_info *zns, | |
85a75f1e | 878 | int count, int startup) |
1fdc9eae | 879 | { |
d62a17ae | 880 | int status; |
881 | int ret = 0; | |
882 | int error; | |
883 | int read_in = 0; | |
884 | ||
885 | while (1) { | |
9ed7517b | 886 | char buf[NL_RCV_PKT_BUF_SIZE]; |
d62a17ae | 887 | struct sockaddr_nl snl; |
888 | struct msghdr msg = {.msg_name = (void *)&snl, | |
ae6138bf | 889 | .msg_namelen = sizeof(snl)}; |
d62a17ae | 890 | struct nlmsghdr *h; |
891 | ||
892 | if (count && read_in >= count) | |
893 | return 0; | |
894 | ||
ae6138bf JU |
895 | status = netlink_recv_msg(nl, msg, buf, sizeof(buf)); |
896 | if (status == -1) | |
d62a17ae | 897 | return -1; |
ae6138bf JU |
898 | else if (status == 0) |
899 | break; | |
81a2f870 | 900 | |
d62a17ae | 901 | read_in++; |
902 | for (h = (struct nlmsghdr *)buf; | |
e6a0e0d1 | 903 | (status >= 0 && NLMSG_OK(h, (unsigned int)status)); |
d62a17ae | 904 | h = NLMSG_NEXT(h, status)) { |
905 | /* Finish of reading. */ | |
906 | if (h->nlmsg_type == NLMSG_DONE) | |
907 | return ret; | |
908 | ||
909 | /* Error handling. */ | |
910 | if (h->nlmsg_type == NLMSG_ERROR) { | |
ae6138bf JU |
911 | int err = netlink_parse_error(nl, h, zns, |
912 | startup); | |
913 | if (err == 1) { | |
d62a17ae | 914 | if (!(h->nlmsg_flags & NLM_F_MULTI)) |
915 | return 0; | |
916 | continue; | |
ae6138bf JU |
917 | } else |
918 | return err; | |
d62a17ae | 919 | } |
920 | ||
921 | /* OK we got netlink message. */ | |
922 | if (IS_ZEBRA_DEBUG_KERNEL) | |
923 | zlog_debug( | |
924 | "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u", | |
925 | nl->name, | |
926 | nl_msg_type_to_str(h->nlmsg_type), | |
927 | h->nlmsg_type, h->nlmsg_len, | |
928 | h->nlmsg_seq, h->nlmsg_pid); | |
929 | ||
783827ae DS |
930 | |
931 | /* | |
932 | * Ignore messages that maybe sent from | |
933 | * other actors besides the kernel | |
934 | */ | |
935 | if (snl.nl_pid != 0) { | |
43e52561 QY |
936 | zlog_debug("Ignoring message from pid %u", |
937 | snl.nl_pid); | |
d62a17ae | 938 | continue; |
939 | } | |
940 | ||
2414abd3 | 941 | error = (*filter)(h, zns->ns_id, startup); |
d62a17ae | 942 | if (error < 0) { |
9df414fe QY |
943 | zlog_debug("%s filter function error", |
944 | nl->name); | |
d62a17ae | 945 | ret = error; |
946 | } | |
947 | } | |
948 | ||
949 | /* After error care. */ | |
950 | if (msg.msg_flags & MSG_TRUNC) { | |
e914ccbe | 951 | flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR, |
1c50c1c0 | 952 | "%s error: message truncated", nl->name); |
d62a17ae | 953 | continue; |
954 | } | |
955 | if (status) { | |
e914ccbe | 956 | flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR, |
1c50c1c0 QY |
957 | "%s error: data remnant size %d", nl->name, |
958 | status); | |
d62a17ae | 959 | return -1; |
960 | } | |
961 | } | |
962 | return ret; | |
1fdc9eae | 963 | } |
964 | ||
936ebf0a | 965 | /* |
7cdb1a84 | 966 | * netlink_talk_info |
936ebf0a DS |
967 | * |
968 | * sendmsg() to netlink socket then recvmsg(). | |
969 | * Calls netlink_parse_info to parse returned data | |
970 | * | |
971 | * filter -> The filter to read final results from kernel | |
972 | * nlmsghdr -> The data to send to the kernel | |
8b962e77 | 973 | * dp_info -> The dataplane and netlink socket information |
936ebf0a DS |
974 | * startup -> Are we reading in under startup conditions |
975 | * This is passed through eventually to filter. | |
976 | */ | |
67e3369e JU |
977 | static int |
978 | netlink_talk_info(int (*filter)(struct nlmsghdr *, ns_id_t, int startup), | |
979 | struct nlmsghdr *n, const struct zebra_dplane_info *dp_info, | |
980 | int startup) | |
1fdc9eae | 981 | { |
7cdb1a84 | 982 | const struct nlsock *nl; |
d62a17ae | 983 | |
7cdb1a84 MS |
984 | nl = &(dp_info->nls); |
985 | n->nlmsg_seq = nl->seq; | |
d62a17ae | 986 | n->nlmsg_pid = nl->snl.nl_pid; |
987 | ||
d62a17ae | 988 | if (IS_ZEBRA_DEBUG_KERNEL) |
989 | zlog_debug( | |
990 | "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x", | |
991 | nl->name, nl_msg_type_to_str(n->nlmsg_type), | |
992 | n->nlmsg_type, n->nlmsg_len, n->nlmsg_seq, | |
993 | n->nlmsg_flags); | |
994 | ||
f8653393 | 995 | if (netlink_send_msg(nl, n, n->nlmsg_len) == -1) |
d62a17ae | 996 | return -1; |
d62a17ae | 997 | |
d62a17ae | 998 | /* |
999 | * Get reply from netlink socket. | |
1000 | * The reply should either be an acknowlegement or an error. | |
1001 | */ | |
7cdb1a84 MS |
1002 | return netlink_parse_info(filter, nl, dp_info, 0, startup); |
1003 | } | |
1004 | ||
1005 | /* | |
1006 | * Synchronous version of netlink_talk_info. Converts args to suit the | |
1007 | * common version, which is suitable for both sync and async use. | |
7cdb1a84 MS |
1008 | */ |
1009 | int netlink_talk(int (*filter)(struct nlmsghdr *, ns_id_t, int startup), | |
1010 | struct nlmsghdr *n, struct nlsock *nl, struct zebra_ns *zns, | |
1011 | int startup) | |
1012 | { | |
1013 | struct zebra_dplane_info dp_info; | |
1014 | ||
1015 | /* Increment sequence number before capturing snapshot of ns socket | |
1016 | * info. | |
1017 | */ | |
1018 | nl->seq++; | |
1019 | ||
1020 | /* Capture info in intermediate info struct */ | |
85a75f1e | 1021 | zebra_dplane_info_from_zns(&dp_info, zns, (nl == &(zns->netlink_cmd))); |
7cdb1a84 | 1022 | |
5709131c | 1023 | return netlink_talk_info(filter, n, &dp_info, startup); |
1fdc9eae | 1024 | } |
1025 | ||
289602d7 | 1026 | /* Issue request message to kernel via netlink socket. GET messages |
1027 | * are issued through this interface. | |
1028 | */ | |
fd3f8e52 | 1029 | int netlink_request(struct nlsock *nl, void *req) |
1fdc9eae | 1030 | { |
fd3f8e52 | 1031 | struct nlmsghdr *n = (struct nlmsghdr *)req; |
d62a17ae | 1032 | |
1033 | /* Check netlink socket. */ | |
1034 | if (nl->sock < 0) { | |
450971aa | 1035 | flog_err_sys(EC_LIB_SOCKET, "%s socket isn't active.", |
09c866e3 | 1036 | nl->name); |
d62a17ae | 1037 | return -1; |
1038 | } | |
1039 | ||
1040 | /* Fill common fields for all requests. */ | |
d62a17ae | 1041 | n->nlmsg_pid = nl->snl.nl_pid; |
1042 | n->nlmsg_seq = ++nl->seq; | |
1043 | ||
f8653393 | 1044 | if (netlink_send_msg(nl, req, n->nlmsg_len) == -1) |
d62a17ae | 1045 | return -1; |
d62a17ae | 1046 | |
1047 | return 0; | |
1fdc9eae | 1048 | } |
1049 | ||
e63c7622 JU |
1050 | static int nl_batch_read_resp(struct nl_batch *bth) |
1051 | { | |
1052 | struct nlmsghdr *h; | |
1053 | struct sockaddr_nl snl; | |
1054 | struct msghdr msg; | |
f6feb48b | 1055 | int status, seq; |
e63c7622 | 1056 | const struct nlsock *nl; |
f6feb48b JU |
1057 | struct zebra_dplane_ctx *ctx; |
1058 | bool ignore_msg; | |
e63c7622 JU |
1059 | |
1060 | nl = &(bth->zns->nls); | |
1061 | ||
1062 | msg.msg_name = (void *)&snl; | |
1063 | msg.msg_namelen = sizeof(snl); | |
1064 | ||
2f9dbd3a JU |
1065 | /* |
1066 | * The responses are not batched, so we need to read and process one | |
1067 | * message at a time. | |
1068 | */ | |
1069 | while (true) { | |
1070 | status = netlink_recv_msg(nl, msg, nl_batch_rx_buf, | |
1071 | sizeof(nl_batch_rx_buf)); | |
1072 | if (status == -1 || status == 0) | |
1073 | return status; | |
e63c7622 | 1074 | |
2f9dbd3a | 1075 | h = (struct nlmsghdr *)nl_batch_rx_buf; |
f6feb48b JU |
1076 | ignore_msg = false; |
1077 | seq = h->nlmsg_seq; | |
e63c7622 | 1078 | /* |
f6feb48b JU |
1079 | * Find the corresponding context object. Received responses are |
1080 | * in the same order as requests we sent, so we can simply | |
1081 | * iterate over the context list and match responses with | |
1082 | * requests at same time. | |
e63c7622 | 1083 | */ |
f6feb48b JU |
1084 | while (true) { |
1085 | ctx = dplane_ctx_dequeue(&(bth->ctx_list)); | |
1086 | if (ctx == NULL) | |
1087 | break; | |
1088 | ||
1089 | dplane_ctx_enqueue_tail(bth->ctx_out_q, ctx); | |
1090 | ||
1091 | /* We have found corresponding context object. */ | |
1092 | if (dplane_ctx_get_ns(ctx)->nls.seq == seq) | |
e63c7622 | 1093 | break; |
f6feb48b JU |
1094 | |
1095 | /* | |
1096 | * 'update' context objects take two consecutive | |
1097 | * sequence numbers. | |
1098 | */ | |
1099 | if (dplane_ctx_is_update(ctx) | |
1100 | && dplane_ctx_get_ns(ctx)->nls.seq + 1 == seq) { | |
1101 | /* | |
1102 | * This is the situation where we get a response | |
1103 | * to a message that should be ignored. | |
1104 | */ | |
1105 | ignore_msg = true; | |
1106 | break; | |
1107 | } | |
e63c7622 JU |
1108 | } |
1109 | ||
f6feb48b JU |
1110 | if (ignore_msg) |
1111 | continue; | |
1112 | ||
e63c7622 JU |
1113 | /* |
1114 | * We received a message with the sequence number that isn't | |
1115 | * associated with any dplane context object. | |
1116 | */ | |
f6feb48b | 1117 | if (ctx == NULL) { |
e63c7622 JU |
1118 | zlog_debug( |
1119 | "%s: skipping unassociated response, seq number %d NS %u", | |
1120 | __func__, h->nlmsg_seq, bth->zns->ns_id); | |
e63c7622 JU |
1121 | continue; |
1122 | } | |
1123 | ||
1124 | if (h->nlmsg_type == NLMSG_ERROR) { | |
1125 | int err = netlink_parse_error(nl, h, bth->zns, 0); | |
1126 | ||
1127 | if (err == -1) | |
f6feb48b JU |
1128 | dplane_ctx_set_status( |
1129 | ctx, ZEBRA_DPLANE_REQUEST_FAILURE); | |
e63c7622 JU |
1130 | |
1131 | zlog_debug("%s: netlink error message seq=%d ", | |
1132 | __func__, h->nlmsg_seq); | |
1133 | continue; | |
1134 | } | |
1135 | ||
1136 | /* | |
1137 | * If we get here then we did not receive neither the ack nor | |
1138 | * the error and instead received some other message in an | |
1139 | * unexpected way. | |
1140 | */ | |
1141 | zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", | |
1142 | __func__, h->nlmsg_type, | |
1143 | nl_msg_type_to_str(h->nlmsg_type), bth->zns->ns_id); | |
1144 | } | |
1145 | ||
1146 | return 0; | |
1147 | } | |
1148 | ||
1149 | static void nl_batch_reset(struct nl_batch *bth) | |
1150 | { | |
e63c7622 JU |
1151 | bth->buf_head = bth->buf; |
1152 | bth->curlen = 0; | |
1153 | bth->msgcnt = 0; | |
1154 | bth->zns = NULL; | |
1155 | ||
f6feb48b | 1156 | TAILQ_INIT(&(bth->ctx_list)); |
e63c7622 JU |
1157 | } |
1158 | ||
f6feb48b | 1159 | static void nl_batch_init(struct nl_batch *bth, struct dplane_ctx_q *ctx_out_q) |
e63c7622 | 1160 | { |
531c92b8 JU |
1161 | /* |
1162 | * If the size of the buffer has changed, free and then allocate a new | |
1163 | * one. | |
1164 | */ | |
1165 | size_t bufsize = | |
1166 | atomic_load_explicit(&nl_batch_bufsize, memory_order_relaxed); | |
1167 | if (bufsize != nl_batch_tx_bufsize) { | |
1168 | if (nl_batch_tx_buf) | |
1169 | XFREE(MTYPE_NL_BUF, nl_batch_tx_buf); | |
1170 | ||
1171 | nl_batch_tx_buf = XCALLOC(MTYPE_NL_BUF, bufsize); | |
1172 | nl_batch_tx_bufsize = bufsize; | |
1173 | } | |
1174 | ||
f6feb48b | 1175 | bth->buf = nl_batch_tx_buf; |
531c92b8 JU |
1176 | bth->bufsiz = bufsize; |
1177 | bth->limit = atomic_load_explicit(&nl_batch_send_threshold, | |
1178 | memory_order_relaxed); | |
e63c7622 | 1179 | |
f6feb48b | 1180 | bth->ctx_out_q = ctx_out_q; |
e63c7622 | 1181 | |
f6feb48b JU |
1182 | nl_batch_reset(bth); |
1183 | } | |
1184 | ||
1185 | static void nl_batch_send(struct nl_batch *bth) | |
1186 | { | |
1187 | struct zebra_dplane_ctx *ctx; | |
1188 | bool err = false; | |
e63c7622 | 1189 | |
f6feb48b JU |
1190 | if (bth->curlen != 0 && bth->zns != NULL) { |
1191 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1192 | zlog_debug("%s: %s, batch size=%zu, msg cnt=%zu", | |
1193 | __func__, bth->zns->nls.name, bth->curlen, | |
1194 | bth->msgcnt); | |
e63c7622 | 1195 | |
f6feb48b JU |
1196 | if (netlink_send_msg(&(bth->zns->nls), bth->buf, bth->curlen) |
1197 | == -1) | |
e63c7622 | 1198 | err = true; |
e63c7622 | 1199 | |
f6feb48b JU |
1200 | if (!err) { |
1201 | if (nl_batch_read_resp(bth) == -1) | |
1202 | err = true; | |
1203 | } | |
1204 | } | |
e63c7622 | 1205 | |
f6feb48b JU |
1206 | /* Move remaining contexts to the outbound queue. */ |
1207 | while (true) { | |
1208 | ctx = dplane_ctx_dequeue(&(bth->ctx_list)); | |
1209 | if (ctx == NULL) | |
1210 | break; | |
e63c7622 | 1211 | |
f6feb48b JU |
1212 | if (err) |
1213 | dplane_ctx_set_status(ctx, | |
1214 | ZEBRA_DPLANE_REQUEST_FAILURE); | |
e63c7622 | 1215 | |
f6feb48b | 1216 | dplane_ctx_enqueue_tail(bth->ctx_out_q, ctx); |
e63c7622 JU |
1217 | } |
1218 | ||
1219 | nl_batch_reset(bth); | |
1220 | } | |
1221 | ||
e63c7622 JU |
1222 | enum netlink_msg_status netlink_batch_add_msg( |
1223 | struct nl_batch *bth, struct zebra_dplane_ctx *ctx, | |
1224 | ssize_t (*msg_encoder)(struct zebra_dplane_ctx *, void *, size_t), | |
f6feb48b | 1225 | bool ignore_res) |
e63c7622 JU |
1226 | { |
1227 | int seq; | |
1228 | ssize_t size; | |
1229 | struct nlmsghdr *msgh; | |
1230 | ||
e63c7622 JU |
1231 | size = (*msg_encoder)(ctx, bth->buf_head, bth->bufsiz - bth->curlen); |
1232 | ||
1233 | /* | |
1234 | * If there was an error while encoding the message (other than buffer | |
1235 | * overflow) then return an error. | |
1236 | */ | |
1237 | if (size < 0) | |
1238 | return FRR_NETLINK_ERROR; | |
1239 | ||
1240 | /* | |
1241 | * If the message doesn't fit entirely in the buffer then send the batch | |
1242 | * and retry. | |
1243 | */ | |
1244 | if (size == 0) { | |
1245 | nl_batch_send(bth); | |
1246 | size = (*msg_encoder)(ctx, bth->buf_head, | |
1247 | bth->bufsiz - bth->curlen); | |
1248 | /* | |
1249 | * If the message doesn't fit in the empty buffer then just | |
1250 | * return an error. | |
1251 | */ | |
1252 | if (size <= 0) | |
1253 | return FRR_NETLINK_ERROR; | |
1254 | } | |
1255 | ||
1256 | seq = dplane_ctx_get_ns(ctx)->nls.seq; | |
f6feb48b | 1257 | if (ignore_res) |
e63c7622 JU |
1258 | seq++; |
1259 | ||
1260 | msgh = (struct nlmsghdr *)bth->buf_head; | |
1261 | msgh->nlmsg_seq = seq; | |
1262 | msgh->nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid; | |
1263 | ||
e63c7622 JU |
1264 | bth->zns = dplane_ctx_get_ns(ctx); |
1265 | bth->buf_head = ((char *)bth->buf_head) + size; | |
1266 | bth->curlen += size; | |
1267 | bth->msgcnt++; | |
1268 | ||
e63c7622 JU |
1269 | return FRR_NETLINK_QUEUED; |
1270 | } | |
1271 | ||
67e3369e JU |
1272 | static enum netlink_msg_status nl_put_msg(struct nl_batch *bth, |
1273 | struct zebra_dplane_ctx *ctx) | |
1274 | { | |
1275 | if (dplane_ctx_is_skip_kernel(ctx)) | |
1276 | return FRR_NETLINK_SUCCESS; | |
1277 | ||
1278 | switch (dplane_ctx_get_op(ctx)) { | |
1279 | ||
1280 | case DPLANE_OP_ROUTE_INSTALL: | |
1281 | case DPLANE_OP_ROUTE_UPDATE: | |
1282 | case DPLANE_OP_ROUTE_DELETE: | |
1283 | return netlink_put_route_update_msg(bth, ctx); | |
1284 | ||
1285 | case DPLANE_OP_NH_INSTALL: | |
1286 | case DPLANE_OP_NH_UPDATE: | |
1287 | case DPLANE_OP_NH_DELETE: | |
1288 | return netlink_put_nexthop_update_msg(bth, ctx); | |
1289 | ||
1290 | case DPLANE_OP_LSP_INSTALL: | |
1291 | case DPLANE_OP_LSP_UPDATE: | |
1292 | case DPLANE_OP_LSP_DELETE: | |
1293 | return netlink_put_lsp_update_msg(bth, ctx); | |
1294 | ||
1295 | case DPLANE_OP_PW_INSTALL: | |
1296 | case DPLANE_OP_PW_UNINSTALL: | |
1297 | return netlink_put_pw_update_msg(bth, ctx); | |
1298 | ||
1299 | case DPLANE_OP_ADDR_INSTALL: | |
1300 | case DPLANE_OP_ADDR_UNINSTALL: | |
1301 | return netlink_put_address_update_msg(bth, ctx); | |
1302 | ||
1303 | case DPLANE_OP_MAC_INSTALL: | |
1304 | case DPLANE_OP_MAC_DELETE: | |
1305 | return netlink_put_mac_update_msg(bth, ctx); | |
1306 | ||
1307 | case DPLANE_OP_NEIGH_INSTALL: | |
1308 | case DPLANE_OP_NEIGH_UPDATE: | |
1309 | case DPLANE_OP_NEIGH_DELETE: | |
1310 | case DPLANE_OP_VTEP_ADD: | |
1311 | case DPLANE_OP_VTEP_DELETE: | |
d68e74b4 | 1312 | case DPLANE_OP_NEIGH_DISCOVER: |
67e3369e JU |
1313 | return netlink_put_neigh_update_msg(bth, ctx); |
1314 | ||
1315 | case DPLANE_OP_RULE_ADD: | |
1316 | case DPLANE_OP_RULE_DELETE: | |
1317 | case DPLANE_OP_RULE_UPDATE: | |
1318 | return netlink_put_rule_update_msg(bth, ctx); | |
1319 | ||
1320 | case DPLANE_OP_SYS_ROUTE_ADD: | |
1321 | case DPLANE_OP_SYS_ROUTE_DELETE: | |
1322 | case DPLANE_OP_ROUTE_NOTIFY: | |
1323 | case DPLANE_OP_LSP_NOTIFY: | |
1324 | return FRR_NETLINK_SUCCESS; | |
1325 | ||
1326 | case DPLANE_OP_NONE: | |
1327 | return FRR_NETLINK_ERROR; | |
1328 | } | |
1329 | ||
1330 | return FRR_NETLINK_ERROR; | |
1331 | } | |
1332 | ||
fef24b03 JU |
1333 | void kernel_update_multi(struct dplane_ctx_q *ctx_list) |
1334 | { | |
67e3369e JU |
1335 | struct nl_batch batch; |
1336 | struct zebra_dplane_ctx *ctx; | |
1337 | struct dplane_ctx_q handled_list; | |
1338 | enum netlink_msg_status res; | |
1339 | ||
67e3369e | 1340 | TAILQ_INIT(&handled_list); |
f6feb48b | 1341 | nl_batch_init(&batch, &handled_list); |
67e3369e JU |
1342 | |
1343 | while (true) { | |
1344 | ctx = dplane_ctx_dequeue(ctx_list); | |
1345 | if (ctx == NULL) | |
1346 | break; | |
1347 | ||
f6feb48b JU |
1348 | if (batch.zns != NULL |
1349 | && batch.zns->ns_id != dplane_ctx_get_ns(ctx)->ns_id) | |
1350 | nl_batch_send(&batch); | |
67e3369e JU |
1351 | |
1352 | /* | |
f6feb48b JU |
1353 | * Assume all messages will succeed and then mark only the ones |
1354 | * that failed. | |
67e3369e | 1355 | */ |
f6feb48b JU |
1356 | dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS); |
1357 | ||
1358 | res = nl_put_msg(&batch, ctx); | |
1359 | ||
1360 | dplane_ctx_enqueue_tail(&(batch.ctx_list), ctx); | |
1361 | if (res == FRR_NETLINK_ERROR) | |
67e3369e JU |
1362 | dplane_ctx_set_status(ctx, |
1363 | ZEBRA_DPLANE_REQUEST_FAILURE); | |
1364 | ||
f6feb48b JU |
1365 | if (batch.curlen > batch.limit) |
1366 | nl_batch_send(&batch); | |
67e3369e JU |
1367 | } |
1368 | ||
1369 | nl_batch_send(&batch); | |
1370 | ||
1371 | TAILQ_INIT(ctx_list); | |
1372 | dplane_ctx_list_append(ctx_list, &handled_list); | |
fef24b03 JU |
1373 | } |
1374 | ||
1fdc9eae | 1375 | /* Exported interface function. This function simply calls |
1376 | netlink_socket (). */ | |
d62a17ae | 1377 | void kernel_init(struct zebra_ns *zns) |
1fdc9eae | 1378 | { |
67188ca2 | 1379 | uint32_t groups; |
5d307d5d DS |
1380 | #if defined SOL_NETLINK |
1381 | int one, ret; | |
1382 | #endif | |
d62a17ae | 1383 | |
026a316f DS |
1384 | /* |
1385 | * Initialize netlink sockets | |
1386 | * | |
1387 | * If RTMGRP_XXX exists use that, but at some point | |
1388 | * I think the kernel developers realized that | |
1389 | * keeping track of all the different values would | |
1390 | * lead to confusion, so we need to convert the | |
1391 | * RTNLGRP_XXX to a bit position for ourself | |
1392 | */ | |
1393 | groups = RTMGRP_LINK | | |
1394 | RTMGRP_IPV4_ROUTE | | |
1395 | RTMGRP_IPV4_IFADDR | | |
1396 | RTMGRP_IPV6_ROUTE | | |
1397 | RTMGRP_IPV6_IFADDR | | |
1398 | RTMGRP_IPV4_MROUTE | | |
1399 | RTMGRP_NEIGH | | |
67188ca2 QY |
1400 | ((uint32_t) 1 << (RTNLGRP_IPV4_RULE - 1)) | |
1401 | ((uint32_t) 1 << (RTNLGRP_IPV6_RULE - 1)) | | |
1402 | ((uint32_t) 1 << (RTNLGRP_NEXTHOP - 1)); | |
d62a17ae | 1403 | |
1404 | snprintf(zns->netlink.name, sizeof(zns->netlink.name), | |
1405 | "netlink-listen (NS %u)", zns->ns_id); | |
1406 | zns->netlink.sock = -1; | |
19d5a4fe DS |
1407 | if (netlink_socket(&zns->netlink, groups, zns->ns_id) < 0) { |
1408 | zlog_err("Failure to create %s socket", | |
1409 | zns->netlink.name); | |
1410 | exit(-1); | |
1411 | } | |
d62a17ae | 1412 | |
1413 | snprintf(zns->netlink_cmd.name, sizeof(zns->netlink_cmd.name), | |
1414 | "netlink-cmd (NS %u)", zns->ns_id); | |
1415 | zns->netlink_cmd.sock = -1; | |
19d5a4fe DS |
1416 | if (netlink_socket(&zns->netlink_cmd, 0, zns->ns_id) < 0) { |
1417 | zlog_err("Failure to create %s socket", | |
1418 | zns->netlink_cmd.name); | |
1419 | exit(-1); | |
1420 | } | |
d62a17ae | 1421 | |
62b8bb7a MS |
1422 | snprintf(zns->netlink_dplane.name, sizeof(zns->netlink_dplane.name), |
1423 | "netlink-dp (NS %u)", zns->ns_id); | |
1424 | zns->netlink_dplane.sock = -1; | |
1425 | if (netlink_socket(&zns->netlink_dplane, 0, zns->ns_id) < 0) { | |
1426 | zlog_err("Failure to create %s socket", | |
1427 | zns->netlink_dplane.name); | |
1428 | exit(-1); | |
1429 | } | |
1430 | ||
5d307d5d DS |
1431 | /* |
1432 | * SOL_NETLINK is not available on all platforms yet | |
1433 | * apparently. It's in bits/socket.h which I am not | |
1434 | * sure that we want to pull into our build system. | |
1435 | */ | |
1436 | #if defined SOL_NETLINK | |
1437 | /* | |
1438 | * Let's tell the kernel that we want to receive extended | |
62b8bb7a | 1439 | * ACKS over our command socket(s) |
5d307d5d DS |
1440 | */ |
1441 | one = 1; | |
1442 | ret = setsockopt(zns->netlink_cmd.sock, SOL_NETLINK, NETLINK_EXT_ACK, | |
1443 | &one, sizeof(one)); | |
1444 | ||
1445 | if (ret < 0) | |
62b8bb7a MS |
1446 | zlog_notice("Registration for extended cmd ACK failed : %d %s", |
1447 | errno, safe_strerror(errno)); | |
1448 | ||
1449 | one = 1; | |
1450 | ret = setsockopt(zns->netlink_dplane.sock, SOL_NETLINK, NETLINK_EXT_ACK, | |
1451 | &one, sizeof(one)); | |
1452 | ||
1453 | if (ret < 0) | |
1454 | zlog_notice("Registration for extended dp ACK failed : %d %s", | |
5d307d5d DS |
1455 | errno, safe_strerror(errno)); |
1456 | #endif | |
1457 | ||
d62a17ae | 1458 | /* Register kernel socket. */ |
19d5a4fe | 1459 | if (fcntl(zns->netlink.sock, F_SETFL, O_NONBLOCK) < 0) |
450971aa | 1460 | flog_err_sys(EC_LIB_SOCKET, "Can't set %s socket flags: %s", |
09c866e3 | 1461 | zns->netlink.name, safe_strerror(errno)); |
8c85e8ea DS |
1462 | |
1463 | if (fcntl(zns->netlink_cmd.sock, F_SETFL, O_NONBLOCK) < 0) | |
1464 | zlog_err("Can't set %s socket error: %s(%d)", | |
1465 | zns->netlink_cmd.name, safe_strerror(errno), errno); | |
19d5a4fe | 1466 | |
62b8bb7a MS |
1467 | if (fcntl(zns->netlink_dplane.sock, F_SETFL, O_NONBLOCK) < 0) |
1468 | zlog_err("Can't set %s socket error: %s(%d)", | |
1469 | zns->netlink_dplane.name, safe_strerror(errno), errno); | |
1470 | ||
19d5a4fe DS |
1471 | /* Set receive buffer size if it's set from command line */ |
1472 | if (nl_rcvbufsize) | |
1473 | netlink_recvbuf(&zns->netlink, nl_rcvbufsize); | |
1474 | ||
1475 | netlink_install_filter(zns->netlink.sock, | |
62b8bb7a MS |
1476 | zns->netlink_cmd.snl.nl_pid, |
1477 | zns->netlink_dplane.snl.nl_pid); | |
1478 | ||
19d5a4fe DS |
1479 | zns->t_netlink = NULL; |
1480 | ||
3801e764 | 1481 | thread_add_read(zrouter.master, kernel_read, zns, |
19d5a4fe | 1482 | zns->netlink.sock, &zns->t_netlink); |
d62a17ae | 1483 | |
1484 | rt_netlink_init(); | |
1fdc9eae | 1485 | } |
1486 | ||
62b8bb7a | 1487 | void kernel_terminate(struct zebra_ns *zns, bool complete) |
1fdc9eae | 1488 | { |
d62a17ae | 1489 | THREAD_READ_OFF(zns->t_netlink); |
1490 | ||
1491 | if (zns->netlink.sock >= 0) { | |
1492 | close(zns->netlink.sock); | |
1493 | zns->netlink.sock = -1; | |
1494 | } | |
1495 | ||
1496 | if (zns->netlink_cmd.sock >= 0) { | |
1497 | close(zns->netlink_cmd.sock); | |
1498 | zns->netlink_cmd.sock = -1; | |
1499 | } | |
ddfeb486 | 1500 | |
62b8bb7a MS |
1501 | /* During zebra shutdown, we need to leave the dataplane socket |
1502 | * around until all work is done. | |
1503 | */ | |
1504 | if (complete) { | |
1505 | if (zns->netlink_dplane.sock >= 0) { | |
1506 | close(zns->netlink_dplane.sock); | |
1507 | zns->netlink_dplane.sock = -1; | |
1508 | } | |
1509 | } | |
1510 | } | |
ddfeb486 | 1511 | #endif /* HAVE_NETLINK */ |