]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0-or-later | |
2 | /* Kernel communication using netlink interface. | |
3 | * Copyright (C) 1999 Kunihiro Ishiguro | |
4 | */ | |
5 | ||
6 | #include <zebra.h> | |
7 | ||
8 | #ifdef HAVE_NETLINK | |
9 | ||
10 | #include "linklist.h" | |
11 | #include "if.h" | |
12 | #include "log.h" | |
13 | #include "prefix.h" | |
14 | #include "connected.h" | |
15 | #include "table.h" | |
16 | #include "memory.h" | |
17 | #include "rib.h" | |
18 | #include "thread.h" | |
19 | #include "privs.h" | |
20 | #include "nexthop.h" | |
21 | #include "vrf.h" | |
22 | #include "mpls.h" | |
23 | #include "lib_errors.h" | |
24 | #include "hash.h" | |
25 | ||
26 | #include "zebra/zebra_router.h" | |
27 | #include "zebra/zebra_ns.h" | |
28 | #include "zebra/zebra_vrf.h" | |
29 | #include "zebra/rt.h" | |
30 | #include "zebra/debug.h" | |
31 | #include "zebra/kernel_netlink.h" | |
32 | #include "zebra/rt_netlink.h" | |
33 | #include "zebra/if_netlink.h" | |
34 | #include "zebra/rule_netlink.h" | |
35 | #include "zebra/tc_netlink.h" | |
36 | #include "zebra/netconf_netlink.h" | |
37 | #include "zebra/zebra_errors.h" | |
38 | ||
39 | #ifndef SO_RCVBUFFORCE | |
40 | #define SO_RCVBUFFORCE (33) | |
41 | #endif | |
42 | ||
43 | /* Hack for GNU libc version 2. */ | |
44 | #ifndef MSG_TRUNC | |
45 | #define MSG_TRUNC 0x20 | |
46 | #endif /* MSG_TRUNC */ | |
47 | ||
48 | #ifndef NLMSG_TAIL | |
49 | #define NLMSG_TAIL(nmsg) \ | |
50 | ((struct rtattr *)(((uint8_t *)(nmsg)) \ | |
51 | + NLMSG_ALIGN((nmsg)->nlmsg_len))) | |
52 | #endif | |
53 | ||
54 | #ifndef RTA_TAIL | |
55 | #define RTA_TAIL(rta) \ | |
56 | ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len))) | |
57 | #endif | |
58 | ||
59 | #ifndef RTNL_FAMILY_IP6MR | |
60 | #define RTNL_FAMILY_IP6MR 129 | |
61 | #endif | |
62 | ||
63 | #ifndef RTPROT_MROUTED | |
64 | #define RTPROT_MROUTED 17 | |
65 | #endif | |
66 | ||
67 | #define NL_DEFAULT_BATCH_BUFSIZE (16 * NL_PKT_BUF_SIZE) | |
68 | ||
69 | /* | |
70 | * We limit the batch's size to a number smaller than the length of the | |
71 | * underlying buffer since the last message that wouldn't fit the batch would go | |
72 | * over the upper boundary and then it would have to be encoded again into a new | |
73 | * buffer. If the difference between the limit and the length of the buffer is | |
74 | * big enough (bigger than the biggest Netlink message) then this situation | |
75 | * won't occur. | |
76 | */ | |
77 | #define NL_DEFAULT_BATCH_SEND_THRESHOLD (15 * NL_PKT_BUF_SIZE) | |
78 | ||
79 | static const struct message nlmsg_str[] = {{RTM_NEWROUTE, "RTM_NEWROUTE"}, | |
80 | {RTM_DELROUTE, "RTM_DELROUTE"}, | |
81 | {RTM_GETROUTE, "RTM_GETROUTE"}, | |
82 | {RTM_NEWLINK, "RTM_NEWLINK"}, | |
83 | {RTM_SETLINK, "RTM_SETLINK"}, | |
84 | {RTM_DELLINK, "RTM_DELLINK"}, | |
85 | {RTM_GETLINK, "RTM_GETLINK"}, | |
86 | {RTM_NEWADDR, "RTM_NEWADDR"}, | |
87 | {RTM_DELADDR, "RTM_DELADDR"}, | |
88 | {RTM_GETADDR, "RTM_GETADDR"}, | |
89 | {RTM_NEWNEIGH, "RTM_NEWNEIGH"}, | |
90 | {RTM_DELNEIGH, "RTM_DELNEIGH"}, | |
91 | {RTM_GETNEIGH, "RTM_GETNEIGH"}, | |
92 | {RTM_NEWRULE, "RTM_NEWRULE"}, | |
93 | {RTM_DELRULE, "RTM_DELRULE"}, | |
94 | {RTM_GETRULE, "RTM_GETRULE"}, | |
95 | {RTM_NEWNEXTHOP, "RTM_NEWNEXTHOP"}, | |
96 | {RTM_DELNEXTHOP, "RTM_DELNEXTHOP"}, | |
97 | {RTM_GETNEXTHOP, "RTM_GETNEXTHOP"}, | |
98 | {RTM_NEWNETCONF, "RTM_NEWNETCONF"}, | |
99 | {RTM_DELNETCONF, "RTM_DELNETCONF"}, | |
100 | {RTM_NEWTUNNEL, "RTM_NEWTUNNEL"}, | |
101 | {RTM_DELTUNNEL, "RTM_DELTUNNEL"}, | |
102 | {RTM_GETTUNNEL, "RTM_GETTUNNEL"}, | |
103 | {RTM_NEWQDISC, "RTM_NEWQDISC"}, | |
104 | {RTM_DELQDISC, "RTM_DELQDISC"}, | |
105 | {RTM_GETQDISC, "RTM_GETQDISC"}, | |
106 | {RTM_NEWTCLASS, "RTM_NEWTCLASS"}, | |
107 | {RTM_DELTCLASS, "RTM_DELTCLASS"}, | |
108 | {RTM_GETTCLASS, "RTM_GETTCLASS"}, | |
109 | {RTM_NEWTFILTER, "RTM_NEWTFILTER"}, | |
110 | {RTM_DELTFILTER, "RTM_DELTFILTER"}, | |
111 | {RTM_GETTFILTER, "RTM_GETTFILTER"}, | |
112 | {RTM_NEWVLAN, "RTM_NEWVLAN"}, | |
113 | {RTM_DELVLAN, "RTM_DELVLAN"}, | |
114 | {RTM_GETVLAN, "RTM_GETVLAN"}, | |
115 | {0}}; | |
116 | ||
117 | static const struct message rtproto_str[] = { | |
118 | {RTPROT_REDIRECT, "redirect"}, | |
119 | {RTPROT_KERNEL, "kernel"}, | |
120 | {RTPROT_BOOT, "boot"}, | |
121 | {RTPROT_STATIC, "static"}, | |
122 | {RTPROT_GATED, "GateD"}, | |
123 | {RTPROT_RA, "router advertisement"}, | |
124 | {RTPROT_MRT, "MRT"}, | |
125 | {RTPROT_ZEBRA, "Zebra"}, | |
126 | #ifdef RTPROT_BIRD | |
127 | {RTPROT_BIRD, "BIRD"}, | |
128 | #endif /* RTPROT_BIRD */ | |
129 | {RTPROT_MROUTED, "mroute"}, | |
130 | {RTPROT_BGP, "BGP"}, | |
131 | {RTPROT_OSPF, "OSPF"}, | |
132 | {RTPROT_ISIS, "IS-IS"}, | |
133 | {RTPROT_RIP, "RIP"}, | |
134 | {RTPROT_RIPNG, "RIPNG"}, | |
135 | {RTPROT_ZSTATIC, "static"}, | |
136 | {0}}; | |
137 | ||
138 | static const struct message family_str[] = {{AF_INET, "ipv4"}, | |
139 | {AF_INET6, "ipv6"}, | |
140 | {AF_BRIDGE, "bridge"}, | |
141 | {RTNL_FAMILY_IPMR, "ipv4MR"}, | |
142 | {RTNL_FAMILY_IP6MR, "ipv6MR"}, | |
143 | {0}}; | |
144 | ||
145 | static const struct message rttype_str[] = {{RTN_UNSPEC, "none"}, | |
146 | {RTN_UNICAST, "unicast"}, | |
147 | {RTN_LOCAL, "local"}, | |
148 | {RTN_BROADCAST, "broadcast"}, | |
149 | {RTN_ANYCAST, "anycast"}, | |
150 | {RTN_MULTICAST, "multicast"}, | |
151 | {RTN_BLACKHOLE, "blackhole"}, | |
152 | {RTN_UNREACHABLE, "unreachable"}, | |
153 | {RTN_PROHIBIT, "prohibited"}, | |
154 | {RTN_THROW, "throw"}, | |
155 | {RTN_NAT, "nat"}, | |
156 | {RTN_XRESOLVE, "resolver"}, | |
157 | {0}}; | |
158 | ||
159 | extern struct thread_master *master; | |
160 | ||
161 | extern struct zebra_privs_t zserv_privs; | |
162 | ||
163 | DEFINE_MTYPE_STATIC(ZEBRA, NL_BUF, "Zebra Netlink buffers"); | |
164 | ||
165 | /* Hashtable and mutex to allow lookup of nlsock structs by socket/fd value. | |
166 | * We have both the main and dplane pthreads using these structs, so we have | |
167 | * to protect the hash with a lock. | |
168 | */ | |
169 | static struct hash *nlsock_hash; | |
170 | pthread_mutex_t nlsock_mutex; | |
171 | ||
172 | /* Lock and unlock wrappers for nlsock hash */ | |
173 | #define NLSOCK_LOCK() pthread_mutex_lock(&nlsock_mutex) | |
174 | #define NLSOCK_UNLOCK() pthread_mutex_unlock(&nlsock_mutex) | |
175 | ||
176 | size_t nl_batch_tx_bufsize; | |
177 | char *nl_batch_tx_buf; | |
178 | ||
179 | _Atomic uint32_t nl_batch_bufsize = NL_DEFAULT_BATCH_BUFSIZE; | |
180 | _Atomic uint32_t nl_batch_send_threshold = NL_DEFAULT_BATCH_SEND_THRESHOLD; | |
181 | ||
182 | struct nl_batch { | |
183 | void *buf; | |
184 | size_t bufsiz; | |
185 | size_t limit; | |
186 | ||
187 | void *buf_head; | |
188 | size_t curlen; | |
189 | size_t msgcnt; | |
190 | ||
191 | const struct zebra_dplane_info *zns; | |
192 | ||
193 | struct dplane_ctx_list_head ctx_list; | |
194 | ||
195 | /* | |
196 | * Pointer to the queue of completed contexts outbound back | |
197 | * towards the dataplane module. | |
198 | */ | |
199 | struct dplane_ctx_list_head *ctx_out_q; | |
200 | }; | |
201 | ||
202 | int netlink_config_write_helper(struct vty *vty) | |
203 | { | |
204 | uint32_t size = | |
205 | atomic_load_explicit(&nl_batch_bufsize, memory_order_relaxed); | |
206 | uint32_t threshold = atomic_load_explicit(&nl_batch_send_threshold, | |
207 | memory_order_relaxed); | |
208 | ||
209 | if (size != NL_DEFAULT_BATCH_BUFSIZE | |
210 | || threshold != NL_DEFAULT_BATCH_SEND_THRESHOLD) | |
211 | vty_out(vty, "zebra kernel netlink batch-tx-buf %u %u\n", size, | |
212 | threshold); | |
213 | ||
214 | if (if_netlink_frr_protodown_r_bit_is_set()) | |
215 | vty_out(vty, "zebra protodown reason-bit %u\n", | |
216 | if_netlink_get_frr_protodown_r_bit()); | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
221 | void netlink_set_batch_buffer_size(uint32_t size, uint32_t threshold, bool set) | |
222 | { | |
223 | if (!set) { | |
224 | size = NL_DEFAULT_BATCH_BUFSIZE; | |
225 | threshold = NL_DEFAULT_BATCH_SEND_THRESHOLD; | |
226 | } | |
227 | ||
228 | atomic_store_explicit(&nl_batch_bufsize, size, memory_order_relaxed); | |
229 | atomic_store_explicit(&nl_batch_send_threshold, threshold, | |
230 | memory_order_relaxed); | |
231 | } | |
232 | ||
233 | int netlink_talk_filter(struct nlmsghdr *h, ns_id_t ns_id, int startup) | |
234 | { | |
235 | /* | |
236 | * This is an error condition that must be handled during | |
237 | * development. | |
238 | * | |
239 | * The netlink_talk_filter function is used for communication | |
240 | * down the netlink_cmd pipe and we are expecting | |
241 | * an ack being received. So if we get here | |
242 | * then we did not receive the ack and instead | |
243 | * received some other message in an unexpected | |
244 | * way. | |
245 | */ | |
246 | zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__, | |
247 | h->nlmsg_type, nl_msg_type_to_str(h->nlmsg_type), ns_id); | |
248 | return 0; | |
249 | } | |
250 | ||
251 | static int netlink_recvbuf(struct nlsock *nl, uint32_t newsize) | |
252 | { | |
253 | uint32_t oldsize; | |
254 | socklen_t newlen = sizeof(newsize); | |
255 | socklen_t oldlen = sizeof(oldsize); | |
256 | int ret; | |
257 | ||
258 | ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen); | |
259 | if (ret < 0) { | |
260 | flog_err_sys(EC_LIB_SOCKET, | |
261 | "Can't get %s receive buffer size: %s", nl->name, | |
262 | safe_strerror(errno)); | |
263 | return -1; | |
264 | } | |
265 | ||
266 | /* Try force option (linux >= 2.6.14) and fall back to normal set */ | |
267 | frr_with_privs(&zserv_privs) { | |
268 | ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE, | |
269 | &rcvbufsize, sizeof(rcvbufsize)); | |
270 | } | |
271 | if (ret < 0) | |
272 | ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsize, | |
273 | sizeof(rcvbufsize)); | |
274 | if (ret < 0) { | |
275 | flog_err_sys(EC_LIB_SOCKET, | |
276 | "Can't set %s receive buffer size: %s", nl->name, | |
277 | safe_strerror(errno)); | |
278 | return -1; | |
279 | } | |
280 | ||
281 | ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen); | |
282 | if (ret < 0) { | |
283 | flog_err_sys(EC_LIB_SOCKET, | |
284 | "Can't get %s receive buffer size: %s", nl->name, | |
285 | safe_strerror(errno)); | |
286 | return -1; | |
287 | } | |
288 | return 0; | |
289 | } | |
290 | ||
291 | static const char *group2str(uint32_t group) | |
292 | { | |
293 | switch (group) { | |
294 | case RTNLGRP_TUNNEL: | |
295 | return "RTNLGRP_TUNNEL"; | |
296 | default: | |
297 | return "UNKNOWN"; | |
298 | } | |
299 | } | |
300 | ||
301 | /* Make socket for Linux netlink interface. */ | |
302 | static int netlink_socket(struct nlsock *nl, unsigned long groups, | |
303 | uint32_t ext_groups[], uint8_t ext_group_size, | |
304 | ns_id_t ns_id) | |
305 | { | |
306 | int ret; | |
307 | struct sockaddr_nl snl; | |
308 | int sock; | |
309 | int namelen; | |
310 | ||
311 | frr_with_privs(&zserv_privs) { | |
312 | sock = ns_socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE, ns_id); | |
313 | if (sock < 0) { | |
314 | zlog_err("Can't open %s socket: %s", nl->name, | |
315 | safe_strerror(errno)); | |
316 | return -1; | |
317 | } | |
318 | ||
319 | memset(&snl, 0, sizeof(snl)); | |
320 | snl.nl_family = AF_NETLINK; | |
321 | snl.nl_groups = groups; | |
322 | ||
323 | if (ext_group_size) { | |
324 | uint8_t i; | |
325 | ||
326 | for (i = 0; i < ext_group_size; i++) { | |
327 | #if defined SOL_NETLINK | |
328 | ret = setsockopt(sock, SOL_NETLINK, | |
329 | NETLINK_ADD_MEMBERSHIP, | |
330 | &ext_groups[i], | |
331 | sizeof(ext_groups[i])); | |
332 | if (ret < 0) { | |
333 | zlog_notice( | |
334 | "can't setsockopt NETLINK_ADD_MEMBERSHIP for group %s(%u), this linux kernel does not support it: %s(%d)", | |
335 | group2str(ext_groups[i]), | |
336 | ext_groups[i], | |
337 | safe_strerror(errno), errno); | |
338 | } | |
339 | #else | |
340 | zlog_notice( | |
341 | "Unable to use NETLINK_ADD_MEMBERSHIP via SOL_NETLINK for %s(%u) since the linux kernel does not support the socket option", | |
342 | group2str(ext_groups[i]), | |
343 | ext_groups[i]); | |
344 | #endif | |
345 | } | |
346 | } | |
347 | ||
348 | /* Bind the socket to the netlink structure for anything. */ | |
349 | ret = bind(sock, (struct sockaddr *)&snl, sizeof(snl)); | |
350 | } | |
351 | ||
352 | if (ret < 0) { | |
353 | zlog_err("Can't bind %s socket to group 0x%x: %s", nl->name, | |
354 | snl.nl_groups, safe_strerror(errno)); | |
355 | close(sock); | |
356 | return -1; | |
357 | } | |
358 | ||
359 | /* multiple netlink sockets will have different nl_pid */ | |
360 | namelen = sizeof(snl); | |
361 | ret = getsockname(sock, (struct sockaddr *)&snl, (socklen_t *)&namelen); | |
362 | if (ret < 0 || namelen != sizeof(snl)) { | |
363 | flog_err_sys(EC_LIB_SOCKET, "Can't get %s socket name: %s", | |
364 | nl->name, safe_strerror(errno)); | |
365 | close(sock); | |
366 | return -1; | |
367 | } | |
368 | ||
369 | nl->snl = snl; | |
370 | nl->sock = sock; | |
371 | nl->buflen = NL_RCV_PKT_BUF_SIZE; | |
372 | nl->buf = XMALLOC(MTYPE_NL_BUF, nl->buflen); | |
373 | ||
374 | return ret; | |
375 | } | |
376 | ||
377 | /* | |
378 | * Dispatch an incoming netlink message; used by the zebra main pthread's | |
379 | * netlink event reader. | |
380 | */ | |
381 | static int netlink_information_fetch(struct nlmsghdr *h, ns_id_t ns_id, | |
382 | int startup) | |
383 | { | |
384 | /* | |
385 | * When we handle new message types here | |
386 | * because we are starting to install them | |
387 | * then lets check the netlink_install_filter | |
388 | * and see if we should add the corresponding | |
389 | * allow through entry there. | |
390 | * Probably not needed to do but please | |
391 | * think about it. | |
392 | */ | |
393 | switch (h->nlmsg_type) { | |
394 | case RTM_NEWROUTE: | |
395 | return netlink_route_change(h, ns_id, startup); | |
396 | case RTM_DELROUTE: | |
397 | return netlink_route_change(h, ns_id, startup); | |
398 | case RTM_NEWLINK: | |
399 | return netlink_link_change(h, ns_id, startup); | |
400 | case RTM_DELLINK: | |
401 | return netlink_link_change(h, ns_id, startup); | |
402 | case RTM_NEWNEIGH: | |
403 | case RTM_DELNEIGH: | |
404 | case RTM_GETNEIGH: | |
405 | return netlink_neigh_change(h, ns_id); | |
406 | case RTM_NEWRULE: | |
407 | return netlink_rule_change(h, ns_id, startup); | |
408 | case RTM_DELRULE: | |
409 | return netlink_rule_change(h, ns_id, startup); | |
410 | case RTM_NEWNEXTHOP: | |
411 | return netlink_nexthop_change(h, ns_id, startup); | |
412 | case RTM_DELNEXTHOP: | |
413 | return netlink_nexthop_change(h, ns_id, startup); | |
414 | case RTM_NEWQDISC: | |
415 | case RTM_DELQDISC: | |
416 | return netlink_qdisc_change(h, ns_id, startup); | |
417 | case RTM_NEWTCLASS: | |
418 | case RTM_DELTCLASS: | |
419 | return netlink_tclass_change(h, ns_id, startup); | |
420 | case RTM_NEWTFILTER: | |
421 | case RTM_DELTFILTER: | |
422 | return netlink_tfilter_change(h, ns_id, startup); | |
423 | case RTM_NEWVLAN: | |
424 | return netlink_vlan_change(h, ns_id, startup); | |
425 | case RTM_DELVLAN: | |
426 | return netlink_vlan_change(h, ns_id, startup); | |
427 | ||
428 | /* Messages handled in the dplane thread */ | |
429 | case RTM_NEWADDR: | |
430 | case RTM_DELADDR: | |
431 | case RTM_NEWNETCONF: | |
432 | case RTM_DELNETCONF: | |
433 | case RTM_NEWTUNNEL: | |
434 | case RTM_DELTUNNEL: | |
435 | case RTM_GETTUNNEL: | |
436 | return 0; | |
437 | default: | |
438 | /* | |
439 | * If we have received this message then | |
440 | * we have made a mistake during development | |
441 | * and we need to write some code to handle | |
442 | * this message type or not ask for | |
443 | * it to be sent up to us | |
444 | */ | |
445 | flog_err(EC_ZEBRA_UNKNOWN_NLMSG, | |
446 | "Unknown netlink nlmsg_type %s(%d) vrf %u", | |
447 | nl_msg_type_to_str(h->nlmsg_type), h->nlmsg_type, | |
448 | ns_id); | |
449 | break; | |
450 | } | |
451 | return 0; | |
452 | } | |
453 | ||
454 | /* | |
455 | * Dispatch an incoming netlink message; used by the dataplane pthread's | |
456 | * netlink event reader code. | |
457 | */ | |
458 | static int dplane_netlink_information_fetch(struct nlmsghdr *h, ns_id_t ns_id, | |
459 | int startup) | |
460 | { | |
461 | /* | |
462 | * Dispatch the incoming messages that the dplane pthread handles | |
463 | */ | |
464 | switch (h->nlmsg_type) { | |
465 | case RTM_NEWADDR: | |
466 | case RTM_DELADDR: | |
467 | return netlink_interface_addr_dplane(h, ns_id, startup); | |
468 | ||
469 | case RTM_NEWNETCONF: | |
470 | case RTM_DELNETCONF: | |
471 | return netlink_netconf_change(h, ns_id, startup); | |
472 | ||
473 | /* TODO -- other messages for the dplane socket and pthread */ | |
474 | ||
475 | case RTM_NEWLINK: | |
476 | case RTM_DELLINK: | |
477 | ||
478 | default: | |
479 | break; | |
480 | } | |
481 | ||
482 | return 0; | |
483 | } | |
484 | ||
485 | static void kernel_read(struct thread *thread) | |
486 | { | |
487 | struct zebra_ns *zns = (struct zebra_ns *)THREAD_ARG(thread); | |
488 | struct zebra_dplane_info dp_info; | |
489 | ||
490 | /* Capture key info from ns struct */ | |
491 | zebra_dplane_info_from_zns(&dp_info, zns, false); | |
492 | ||
493 | netlink_parse_info(netlink_information_fetch, &zns->netlink, &dp_info, | |
494 | 5, false); | |
495 | ||
496 | thread_add_read(zrouter.master, kernel_read, zns, zns->netlink.sock, | |
497 | &zns->t_netlink); | |
498 | } | |
499 | ||
500 | /* | |
501 | * Called by the dplane pthread to read incoming OS messages and dispatch them. | |
502 | */ | |
503 | int kernel_dplane_read(struct zebra_dplane_info *info) | |
504 | { | |
505 | struct nlsock *nl = kernel_netlink_nlsock_lookup(info->sock); | |
506 | ||
507 | netlink_parse_info(dplane_netlink_information_fetch, nl, info, 5, | |
508 | false); | |
509 | ||
510 | return 0; | |
511 | } | |
512 | ||
513 | /* | |
514 | * Filter out messages from self that occur on listener socket, | |
515 | * caused by our actions on the command socket(s) | |
516 | * | |
517 | * When we add new Netlink message types we probably | |
518 | * do not need to add them here as that we are filtering | |
519 | * on the routes we actually care to receive( which is rarer | |
520 | * then the normal course of operations). We are intentionally | |
521 | * allowing some messages from ourselves through | |
522 | * ( I'm looking at you Interface based netlink messages ) | |
523 | * so that we only have to write one way to handle incoming | |
524 | * address add/delete and xxxNETCONF changes. | |
525 | */ | |
526 | static void netlink_install_filter(int sock, uint32_t pid, uint32_t dplane_pid) | |
527 | { | |
528 | /* | |
529 | * BPF_JUMP instructions and where you jump to are based upon | |
530 | * 0 as being the next statement. So count from 0. Writing | |
531 | * this down because every time I look at this I have to | |
532 | * re-remember it. | |
533 | */ | |
534 | struct sock_filter filter[] = { | |
535 | /* | |
536 | * Logic: | |
537 | * if (nlmsg_pid == pid || | |
538 | * nlmsg_pid == dplane_pid) { | |
539 | * if (the incoming nlmsg_type == | |
540 | * RTM_NEWADDR || RTM_DELADDR || RTM_NEWNETCONF || | |
541 | * RTM_DELNETCONF) | |
542 | * keep this message | |
543 | * else | |
544 | * skip this message | |
545 | * } else | |
546 | * keep this netlink message | |
547 | */ | |
548 | /* | |
549 | * 0: Load the nlmsg_pid into the BPF register | |
550 | */ | |
551 | BPF_STMT(BPF_LD | BPF_ABS | BPF_W, | |
552 | offsetof(struct nlmsghdr, nlmsg_pid)), | |
553 | /* | |
554 | * 1: Compare to pid | |
555 | */ | |
556 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(pid), 1, 0), | |
557 | /* | |
558 | * 2: Compare to dplane pid | |
559 | */ | |
560 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(dplane_pid), 0, 6), | |
561 | /* | |
562 | * 3: Load the nlmsg_type into BPF register | |
563 | */ | |
564 | BPF_STMT(BPF_LD | BPF_ABS | BPF_H, | |
565 | offsetof(struct nlmsghdr, nlmsg_type)), | |
566 | /* | |
567 | * 4: Compare to RTM_NEWADDR | |
568 | */ | |
569 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_NEWADDR), 4, 0), | |
570 | /* | |
571 | * 5: Compare to RTM_DELADDR | |
572 | */ | |
573 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_DELADDR), 3, 0), | |
574 | /* | |
575 | * 6: Compare to RTM_NEWNETCONF | |
576 | */ | |
577 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_NEWNETCONF), 2, | |
578 | 0), | |
579 | /* | |
580 | * 7: Compare to RTM_DELNETCONF | |
581 | */ | |
582 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_DELNETCONF), 1, | |
583 | 0), | |
584 | /* | |
585 | * 8: This is the end state of we want to skip the | |
586 | * message | |
587 | */ | |
588 | BPF_STMT(BPF_RET | BPF_K, 0), | |
589 | /* 9: This is the end state of we want to keep | |
590 | * the message | |
591 | */ | |
592 | BPF_STMT(BPF_RET | BPF_K, 0xffff), | |
593 | }; | |
594 | ||
595 | struct sock_fprog prog = { | |
596 | .len = array_size(filter), .filter = filter, | |
597 | }; | |
598 | ||
599 | if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)) | |
600 | < 0) | |
601 | flog_err_sys(EC_LIB_SOCKET, "Can't install socket filter: %s", | |
602 | safe_strerror(errno)); | |
603 | } | |
604 | ||
605 | void netlink_parse_rtattr_flags(struct rtattr **tb, int max, struct rtattr *rta, | |
606 | int len, unsigned short flags) | |
607 | { | |
608 | unsigned short type; | |
609 | ||
610 | memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); | |
611 | while (RTA_OK(rta, len)) { | |
612 | type = rta->rta_type & ~flags; | |
613 | if ((type <= max) && (!tb[type])) | |
614 | tb[type] = rta; | |
615 | rta = RTA_NEXT(rta, len); | |
616 | } | |
617 | } | |
618 | ||
619 | void netlink_parse_rtattr(struct rtattr **tb, int max, struct rtattr *rta, | |
620 | int len) | |
621 | { | |
622 | memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); | |
623 | while (RTA_OK(rta, len)) { | |
624 | if (rta->rta_type <= max) | |
625 | tb[rta->rta_type] = rta; | |
626 | rta = RTA_NEXT(rta, len); | |
627 | } | |
628 | } | |
629 | ||
630 | /** | |
631 | * netlink_parse_rtattr_nested() - Parses a nested route attribute | |
632 | * @tb: Pointer to array for storing rtattr in. | |
633 | * @max: Max number to store. | |
634 | * @rta: Pointer to rtattr to look for nested items in. | |
635 | */ | |
636 | void netlink_parse_rtattr_nested(struct rtattr **tb, int max, | |
637 | struct rtattr *rta) | |
638 | { | |
639 | netlink_parse_rtattr(tb, max, RTA_DATA(rta), RTA_PAYLOAD(rta)); | |
640 | } | |
641 | ||
642 | bool nl_addraw_l(struct nlmsghdr *n, unsigned int maxlen, const void *data, | |
643 | unsigned int len) | |
644 | { | |
645 | if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) { | |
646 | zlog_err("ERROR message exceeded bound of %d", maxlen); | |
647 | return false; | |
648 | } | |
649 | ||
650 | memcpy(NLMSG_TAIL(n), data, len); | |
651 | memset((uint8_t *)NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len); | |
652 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len); | |
653 | ||
654 | return true; | |
655 | } | |
656 | ||
657 | bool nl_attr_put(struct nlmsghdr *n, unsigned int maxlen, int type, | |
658 | const void *data, unsigned int alen) | |
659 | { | |
660 | int len; | |
661 | struct rtattr *rta; | |
662 | ||
663 | len = RTA_LENGTH(alen); | |
664 | ||
665 | if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) | |
666 | return false; | |
667 | ||
668 | rta = (struct rtattr *)(((char *)n) + NLMSG_ALIGN(n->nlmsg_len)); | |
669 | rta->rta_type = type; | |
670 | rta->rta_len = len; | |
671 | ||
672 | if (data) | |
673 | memcpy(RTA_DATA(rta), data, alen); | |
674 | else | |
675 | assert(alen == 0); | |
676 | ||
677 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len); | |
678 | ||
679 | return true; | |
680 | } | |
681 | ||
682 | bool nl_attr_put8(struct nlmsghdr *n, unsigned int maxlen, int type, | |
683 | uint8_t data) | |
684 | { | |
685 | return nl_attr_put(n, maxlen, type, &data, sizeof(uint8_t)); | |
686 | } | |
687 | ||
688 | bool nl_attr_put16(struct nlmsghdr *n, unsigned int maxlen, int type, | |
689 | uint16_t data) | |
690 | { | |
691 | return nl_attr_put(n, maxlen, type, &data, sizeof(uint16_t)); | |
692 | } | |
693 | ||
694 | bool nl_attr_put32(struct nlmsghdr *n, unsigned int maxlen, int type, | |
695 | uint32_t data) | |
696 | { | |
697 | return nl_attr_put(n, maxlen, type, &data, sizeof(uint32_t)); | |
698 | } | |
699 | ||
700 | bool nl_attr_put64(struct nlmsghdr *n, unsigned int maxlen, int type, | |
701 | uint64_t data) | |
702 | { | |
703 | return nl_attr_put(n, maxlen, type, &data, sizeof(uint64_t)); | |
704 | } | |
705 | ||
706 | struct rtattr *nl_attr_nest(struct nlmsghdr *n, unsigned int maxlen, int type) | |
707 | { | |
708 | struct rtattr *nest = NLMSG_TAIL(n); | |
709 | ||
710 | if (!nl_attr_put(n, maxlen, type, NULL, 0)) | |
711 | return NULL; | |
712 | ||
713 | nest->rta_type |= NLA_F_NESTED; | |
714 | return nest; | |
715 | } | |
716 | ||
717 | int nl_attr_nest_end(struct nlmsghdr *n, struct rtattr *nest) | |
718 | { | |
719 | nest->rta_len = (uint8_t *)NLMSG_TAIL(n) - (uint8_t *)nest; | |
720 | return n->nlmsg_len; | |
721 | } | |
722 | ||
723 | struct rtnexthop *nl_attr_rtnh(struct nlmsghdr *n, unsigned int maxlen) | |
724 | { | |
725 | struct rtnexthop *rtnh = (struct rtnexthop *)NLMSG_TAIL(n); | |
726 | ||
727 | if (NLMSG_ALIGN(n->nlmsg_len) + RTNH_ALIGN(sizeof(struct rtnexthop)) | |
728 | > maxlen) | |
729 | return NULL; | |
730 | ||
731 | memset(rtnh, 0, sizeof(struct rtnexthop)); | |
732 | n->nlmsg_len = | |
733 | NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(sizeof(struct rtnexthop)); | |
734 | ||
735 | return rtnh; | |
736 | } | |
737 | ||
738 | void nl_attr_rtnh_end(struct nlmsghdr *n, struct rtnexthop *rtnh) | |
739 | { | |
740 | rtnh->rtnh_len = (uint8_t *)NLMSG_TAIL(n) - (uint8_t *)rtnh; | |
741 | } | |
742 | ||
743 | bool nl_rta_put(struct rtattr *rta, unsigned int maxlen, int type, | |
744 | const void *data, int alen) | |
745 | { | |
746 | struct rtattr *subrta; | |
747 | int len = RTA_LENGTH(alen); | |
748 | ||
749 | if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) { | |
750 | zlog_err("ERROR max allowed bound %d exceeded for rtattr", | |
751 | maxlen); | |
752 | return false; | |
753 | } | |
754 | subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len)); | |
755 | subrta->rta_type = type; | |
756 | subrta->rta_len = len; | |
757 | if (alen) | |
758 | memcpy(RTA_DATA(subrta), data, alen); | |
759 | rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len); | |
760 | ||
761 | return true; | |
762 | } | |
763 | ||
764 | bool nl_rta_put16(struct rtattr *rta, unsigned int maxlen, int type, | |
765 | uint16_t data) | |
766 | { | |
767 | return nl_rta_put(rta, maxlen, type, &data, sizeof(uint16_t)); | |
768 | } | |
769 | ||
770 | bool nl_rta_put64(struct rtattr *rta, unsigned int maxlen, int type, | |
771 | uint64_t data) | |
772 | { | |
773 | return nl_rta_put(rta, maxlen, type, &data, sizeof(uint64_t)); | |
774 | } | |
775 | ||
776 | struct rtattr *nl_rta_nest(struct rtattr *rta, unsigned int maxlen, int type) | |
777 | { | |
778 | struct rtattr *nest = RTA_TAIL(rta); | |
779 | ||
780 | if (nl_rta_put(rta, maxlen, type, NULL, 0)) | |
781 | return NULL; | |
782 | ||
783 | nest->rta_type |= NLA_F_NESTED; | |
784 | ||
785 | return nest; | |
786 | } | |
787 | ||
788 | int nl_rta_nest_end(struct rtattr *rta, struct rtattr *nest) | |
789 | { | |
790 | nest->rta_len = (uint8_t *)RTA_TAIL(rta) - (uint8_t *)nest; | |
791 | ||
792 | return rta->rta_len; | |
793 | } | |
794 | ||
795 | const char *nl_msg_type_to_str(uint16_t msg_type) | |
796 | { | |
797 | return lookup_msg(nlmsg_str, msg_type, ""); | |
798 | } | |
799 | ||
800 | const char *nl_rtproto_to_str(uint8_t rtproto) | |
801 | { | |
802 | return lookup_msg(rtproto_str, rtproto, ""); | |
803 | } | |
804 | ||
805 | const char *nl_family_to_str(uint8_t family) | |
806 | { | |
807 | return lookup_msg(family_str, family, ""); | |
808 | } | |
809 | ||
810 | const char *nl_rttype_to_str(uint8_t rttype) | |
811 | { | |
812 | return lookup_msg(rttype_str, rttype, ""); | |
813 | } | |
814 | ||
815 | #define NLA_OK(nla, len) \ | |
816 | ((len) >= (int)sizeof(struct nlattr) \ | |
817 | && (nla)->nla_len >= sizeof(struct nlattr) \ | |
818 | && (nla)->nla_len <= (len)) | |
819 | #define NLA_NEXT(nla, attrlen) \ | |
820 | ((attrlen) -= NLA_ALIGN((nla)->nla_len), \ | |
821 | (struct nlattr *)(((char *)(nla)) + NLA_ALIGN((nla)->nla_len))) | |
822 | #define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len)) | |
823 | #define NLA_DATA(nla) ((struct nlattr *)(((char *)(nla)) + NLA_LENGTH(0))) | |
824 | ||
825 | #define ERR_NLA(err, inner_len) \ | |
826 | ((struct nlattr *)(((char *)(err)) \ | |
827 | + NLMSG_ALIGN(sizeof(struct nlmsgerr)) \ | |
828 | + NLMSG_ALIGN((inner_len)))) | |
829 | ||
830 | static void netlink_parse_nlattr(struct nlattr **tb, int max, | |
831 | struct nlattr *nla, int len) | |
832 | { | |
833 | while (NLA_OK(nla, len)) { | |
834 | if (nla->nla_type <= max) | |
835 | tb[nla->nla_type] = nla; | |
836 | nla = NLA_NEXT(nla, len); | |
837 | } | |
838 | } | |
839 | ||
840 | static void netlink_parse_extended_ack(struct nlmsghdr *h) | |
841 | { | |
842 | struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {}; | |
843 | const struct nlmsgerr *err = (const struct nlmsgerr *)NLMSG_DATA(h); | |
844 | const struct nlmsghdr *err_nlh = NULL; | |
845 | /* Length not including nlmsghdr */ | |
846 | uint32_t len = 0; | |
847 | /* Inner error netlink message length */ | |
848 | uint32_t inner_len = 0; | |
849 | const char *msg = NULL; | |
850 | uint32_t off = 0; | |
851 | ||
852 | if (!(h->nlmsg_flags & NLM_F_CAPPED)) | |
853 | inner_len = (uint32_t)NLMSG_PAYLOAD(&err->msg, 0); | |
854 | ||
855 | len = (uint32_t)(NLMSG_PAYLOAD(h, sizeof(struct nlmsgerr)) - inner_len); | |
856 | ||
857 | netlink_parse_nlattr(tb, NLMSGERR_ATTR_MAX, ERR_NLA(err, inner_len), | |
858 | len); | |
859 | ||
860 | if (tb[NLMSGERR_ATTR_MSG]) | |
861 | msg = (const char *)NLA_DATA(tb[NLMSGERR_ATTR_MSG]); | |
862 | ||
863 | if (tb[NLMSGERR_ATTR_OFFS]) { | |
864 | off = *(uint32_t *)NLA_DATA(tb[NLMSGERR_ATTR_OFFS]); | |
865 | ||
866 | if (off > h->nlmsg_len) { | |
867 | zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS"); | |
868 | } else if (!(h->nlmsg_flags & NLM_F_CAPPED)) { | |
869 | /* | |
870 | * Header of failed message | |
871 | * we are not doing anything currently with it | |
872 | * but noticing it for later. | |
873 | */ | |
874 | err_nlh = &err->msg; | |
875 | zlog_debug("%s: Received %s extended Ack", __func__, | |
876 | nl_msg_type_to_str(err_nlh->nlmsg_type)); | |
877 | } | |
878 | } | |
879 | ||
880 | if (msg && *msg != '\0') { | |
881 | bool is_err = !!err->error; | |
882 | ||
883 | if (is_err) | |
884 | zlog_err("Extended Error: %s", msg); | |
885 | else | |
886 | flog_warn(EC_ZEBRA_NETLINK_EXTENDED_WARNING, | |
887 | "Extended Warning: %s", msg); | |
888 | } | |
889 | } | |
890 | ||
891 | /* | |
892 | * netlink_send_msg - send a netlink message of a certain size. | |
893 | * | |
894 | * Returns -1 on error. Otherwise, it returns the number of bytes sent. | |
895 | */ | |
896 | static ssize_t netlink_send_msg(const struct nlsock *nl, void *buf, | |
897 | size_t buflen) | |
898 | { | |
899 | struct sockaddr_nl snl = {}; | |
900 | struct iovec iov = {}; | |
901 | struct msghdr msg = {}; | |
902 | ssize_t status; | |
903 | int save_errno = 0; | |
904 | ||
905 | iov.iov_base = buf; | |
906 | iov.iov_len = buflen; | |
907 | msg.msg_name = &snl; | |
908 | msg.msg_namelen = sizeof(snl); | |
909 | msg.msg_iov = &iov; | |
910 | msg.msg_iovlen = 1; | |
911 | ||
912 | snl.nl_family = AF_NETLINK; | |
913 | ||
914 | /* Send message to netlink interface. */ | |
915 | frr_with_privs(&zserv_privs) { | |
916 | status = sendmsg(nl->sock, &msg, 0); | |
917 | save_errno = errno; | |
918 | } | |
919 | ||
920 | if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND) { | |
921 | zlog_debug("%s: >> netlink message dump [sent]", __func__); | |
922 | #ifdef NETLINK_DEBUG | |
923 | nl_dump(buf, buflen); | |
924 | #else | |
925 | zlog_hexdump(buf, buflen); | |
926 | #endif /* NETLINK_DEBUG */ | |
927 | } | |
928 | ||
929 | if (status == -1) { | |
930 | flog_err_sys(EC_LIB_SOCKET, "%s error: %s", __func__, | |
931 | safe_strerror(save_errno)); | |
932 | return -1; | |
933 | } | |
934 | ||
935 | return status; | |
936 | } | |
937 | ||
938 | /* | |
939 | * netlink_recv_msg - receive a netlink message. | |
940 | * | |
941 | * Returns -1 on error, 0 if read would block or the number of bytes received. | |
942 | */ | |
943 | static int netlink_recv_msg(struct nlsock *nl, struct msghdr *msg) | |
944 | { | |
945 | struct iovec iov; | |
946 | int status; | |
947 | ||
948 | iov.iov_base = nl->buf; | |
949 | iov.iov_len = nl->buflen; | |
950 | msg->msg_iov = &iov; | |
951 | msg->msg_iovlen = 1; | |
952 | ||
953 | do { | |
954 | int bytes; | |
955 | ||
956 | bytes = recv(nl->sock, NULL, 0, MSG_PEEK | MSG_TRUNC); | |
957 | ||
958 | if (bytes >= 0 && (size_t)bytes > nl->buflen) { | |
959 | nl->buf = XREALLOC(MTYPE_NL_BUF, nl->buf, bytes); | |
960 | nl->buflen = bytes; | |
961 | iov.iov_base = nl->buf; | |
962 | iov.iov_len = nl->buflen; | |
963 | } | |
964 | ||
965 | status = recvmsg(nl->sock, msg, 0); | |
966 | } while (status == -1 && errno == EINTR); | |
967 | ||
968 | if (status == -1) { | |
969 | if (errno == EWOULDBLOCK || errno == EAGAIN) | |
970 | return 0; | |
971 | flog_err(EC_ZEBRA_RECVMSG_OVERRUN, "%s recvmsg overrun: %s", | |
972 | nl->name, safe_strerror(errno)); | |
973 | /* | |
974 | * In this case we are screwed. There is no good way to recover | |
975 | * zebra at this point. | |
976 | */ | |
977 | exit(-1); | |
978 | } | |
979 | ||
980 | if (status == 0) { | |
981 | flog_err_sys(EC_LIB_SOCKET, "%s EOF", nl->name); | |
982 | return -1; | |
983 | } | |
984 | ||
985 | if (msg->msg_namelen != sizeof(struct sockaddr_nl)) { | |
986 | flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR, | |
987 | "%s sender address length error: length %d", nl->name, | |
988 | msg->msg_namelen); | |
989 | return -1; | |
990 | } | |
991 | ||
992 | if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV) { | |
993 | zlog_debug("%s: << netlink message dump [recv]", __func__); | |
994 | #ifdef NETLINK_DEBUG | |
995 | nl_dump(nl->buf, status); | |
996 | #else | |
997 | zlog_hexdump(nl->buf, status); | |
998 | #endif /* NETLINK_DEBUG */ | |
999 | } | |
1000 | ||
1001 | return status; | |
1002 | } | |
1003 | ||
1004 | /* | |
1005 | * netlink_parse_error - parse a netlink error message | |
1006 | * | |
1007 | * Returns 1 if this message is acknowledgement, 0 if this error should be | |
1008 | * ignored, -1 otherwise. | |
1009 | */ | |
1010 | static int netlink_parse_error(const struct nlsock *nl, struct nlmsghdr *h, | |
1011 | bool is_cmd, bool startup) | |
1012 | { | |
1013 | struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); | |
1014 | int errnum = err->error; | |
1015 | int msg_type = err->msg.nlmsg_type; | |
1016 | ||
1017 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
1018 | flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR, | |
1019 | "%s error: message truncated", nl->name); | |
1020 | return -1; | |
1021 | } | |
1022 | ||
1023 | /* | |
1024 | * Parse the extended information before we actually handle it. At this | |
1025 | * point in time we do not do anything other than report the issue. | |
1026 | */ | |
1027 | if (h->nlmsg_flags & NLM_F_ACK_TLVS) | |
1028 | netlink_parse_extended_ack(h); | |
1029 | ||
1030 | /* If the error field is zero, then this is an ACK. */ | |
1031 | if (err->error == 0) { | |
1032 | if (IS_ZEBRA_DEBUG_KERNEL) { | |
1033 | zlog_debug("%s: %s ACK: type=%s(%u), seq=%u, pid=%u", | |
1034 | __func__, nl->name, | |
1035 | nl_msg_type_to_str(err->msg.nlmsg_type), | |
1036 | err->msg.nlmsg_type, err->msg.nlmsg_seq, | |
1037 | err->msg.nlmsg_pid); | |
1038 | } | |
1039 | ||
1040 | return 1; | |
1041 | } | |
1042 | ||
1043 | /* | |
1044 | * Deal with errors that occur because of races in link handling | |
1045 | * or types are not supported in kernel. | |
1046 | */ | |
1047 | if (is_cmd && | |
1048 | ((msg_type == RTM_DELROUTE && | |
1049 | (-errnum == ENODEV || -errnum == ESRCH)) || | |
1050 | (msg_type == RTM_NEWROUTE && | |
1051 | (-errnum == ENETDOWN || -errnum == EEXIST)) || | |
1052 | ((msg_type == RTM_NEWTUNNEL || msg_type == RTM_DELTUNNEL || | |
1053 | msg_type == RTM_GETTUNNEL) && | |
1054 | (-errnum == EOPNOTSUPP)))) { | |
1055 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1056 | zlog_debug("%s: error: %s type=%s(%u), seq=%u, pid=%u", | |
1057 | nl->name, safe_strerror(-errnum), | |
1058 | nl_msg_type_to_str(msg_type), msg_type, | |
1059 | err->msg.nlmsg_seq, err->msg.nlmsg_pid); | |
1060 | return 0; | |
1061 | } | |
1062 | ||
1063 | /* | |
1064 | * We see RTM_DELNEIGH when shutting down an interface with an IPv4 | |
1065 | * link-local. The kernel should have already deleted the neighbor so | |
1066 | * do not log these as an error. | |
1067 | */ | |
1068 | if (msg_type == RTM_DELNEIGH | |
1069 | || (is_cmd && msg_type == RTM_NEWROUTE | |
1070 | && (-errnum == ESRCH || -errnum == ENETUNREACH))) { | |
1071 | /* | |
1072 | * This is known to happen in some situations, don't log as | |
1073 | * error. | |
1074 | */ | |
1075 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1076 | zlog_debug("%s error: %s, type=%s(%u), seq=%u, pid=%u", | |
1077 | nl->name, safe_strerror(-errnum), | |
1078 | nl_msg_type_to_str(msg_type), msg_type, | |
1079 | err->msg.nlmsg_seq, err->msg.nlmsg_pid); | |
1080 | } else { | |
1081 | if ((msg_type != RTM_GETNEXTHOP && msg_type != RTM_GETVLAN) || | |
1082 | !startup) | |
1083 | flog_err(EC_ZEBRA_UNEXPECTED_MESSAGE, | |
1084 | "%s error: %s, type=%s(%u), seq=%u, pid=%u", | |
1085 | nl->name, safe_strerror(-errnum), | |
1086 | nl_msg_type_to_str(msg_type), msg_type, | |
1087 | err->msg.nlmsg_seq, err->msg.nlmsg_pid); | |
1088 | } | |
1089 | ||
1090 | return -1; | |
1091 | } | |
1092 | ||
1093 | /* | |
1094 | * netlink_parse_info | |
1095 | * | |
1096 | * Receive message from netlink interface and pass those information | |
1097 | * to the given function. | |
1098 | * | |
1099 | * filter -> Function to call to read the results | |
1100 | * nl -> netlink socket information | |
1101 | * zns -> The zebra namespace data | |
1102 | * count -> How many we should read in, 0 means as much as possible | |
1103 | * startup -> Are we reading in under startup conditions? passed to | |
1104 | * the filter. | |
1105 | */ | |
1106 | int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int), | |
1107 | struct nlsock *nl, const struct zebra_dplane_info *zns, | |
1108 | int count, bool startup) | |
1109 | { | |
1110 | int status; | |
1111 | int ret = 0; | |
1112 | int error; | |
1113 | int read_in = 0; | |
1114 | ||
1115 | while (1) { | |
1116 | struct sockaddr_nl snl; | |
1117 | struct msghdr msg = {.msg_name = (void *)&snl, | |
1118 | .msg_namelen = sizeof(snl)}; | |
1119 | struct nlmsghdr *h; | |
1120 | ||
1121 | if (count && read_in >= count) | |
1122 | return 0; | |
1123 | ||
1124 | status = netlink_recv_msg(nl, &msg); | |
1125 | if (status == -1) | |
1126 | return -1; | |
1127 | else if (status == 0) | |
1128 | break; | |
1129 | ||
1130 | read_in++; | |
1131 | for (h = (struct nlmsghdr *)nl->buf; | |
1132 | (status >= 0 && NLMSG_OK(h, (unsigned int)status)); | |
1133 | h = NLMSG_NEXT(h, status)) { | |
1134 | /* Finish of reading. */ | |
1135 | if (h->nlmsg_type == NLMSG_DONE) | |
1136 | return ret; | |
1137 | ||
1138 | /* Error handling. */ | |
1139 | if (h->nlmsg_type == NLMSG_ERROR) { | |
1140 | int err = netlink_parse_error( | |
1141 | nl, h, zns->is_cmd, startup); | |
1142 | ||
1143 | if (err == 1) { | |
1144 | if (!(h->nlmsg_flags & NLM_F_MULTI)) | |
1145 | return 0; | |
1146 | continue; | |
1147 | } else | |
1148 | return err; | |
1149 | } | |
1150 | ||
1151 | /* | |
1152 | * What is the right thing to do? The kernel | |
1153 | * is telling us that the dump request was interrupted | |
1154 | * and we more than likely are out of luck and have | |
1155 | * missed data from the kernel. At this point in time | |
1156 | * lets just note that this is happening. | |
1157 | */ | |
1158 | if (h->nlmsg_flags & NLM_F_DUMP_INTR) | |
1159 | flog_err( | |
1160 | EC_ZEBRA_NETLINK_BAD_SEQUENCE, | |
1161 | "netlink recvmsg: The Dump request was interrupted"); | |
1162 | ||
1163 | /* OK we got netlink message. */ | |
1164 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1165 | zlog_debug( | |
1166 | "%s: %s type %s(%u), len=%d, seq=%u, pid=%u", | |
1167 | __func__, nl->name, | |
1168 | nl_msg_type_to_str(h->nlmsg_type), | |
1169 | h->nlmsg_type, h->nlmsg_len, | |
1170 | h->nlmsg_seq, h->nlmsg_pid); | |
1171 | ||
1172 | ||
1173 | /* | |
1174 | * Ignore messages that maybe sent from | |
1175 | * other actors besides the kernel | |
1176 | */ | |
1177 | if (snl.nl_pid != 0) { | |
1178 | zlog_debug("Ignoring message from pid %u", | |
1179 | snl.nl_pid); | |
1180 | continue; | |
1181 | } | |
1182 | ||
1183 | error = (*filter)(h, zns->ns_id, startup); | |
1184 | if (error < 0) { | |
1185 | zlog_debug("%s filter function error", | |
1186 | nl->name); | |
1187 | ret = error; | |
1188 | } | |
1189 | } | |
1190 | ||
1191 | /* After error care. */ | |
1192 | if (msg.msg_flags & MSG_TRUNC) { | |
1193 | flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR, | |
1194 | "%s error: message truncated", nl->name); | |
1195 | continue; | |
1196 | } | |
1197 | if (status) { | |
1198 | flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR, | |
1199 | "%s error: data remnant size %d", nl->name, | |
1200 | status); | |
1201 | return -1; | |
1202 | } | |
1203 | } | |
1204 | return ret; | |
1205 | } | |
1206 | ||
1207 | /* | |
1208 | * netlink_talk_info | |
1209 | * | |
1210 | * sendmsg() to netlink socket then recvmsg(). | |
1211 | * Calls netlink_parse_info to parse returned data | |
1212 | * | |
1213 | * filter -> The filter to read final results from kernel | |
1214 | * nlmsghdr -> The data to send to the kernel | |
1215 | * dp_info -> The dataplane and netlink socket information | |
1216 | * startup -> Are we reading in under startup conditions | |
1217 | * This is passed through eventually to filter. | |
1218 | */ | |
1219 | static int netlink_talk_info(int (*filter)(struct nlmsghdr *, ns_id_t, | |
1220 | int startup), | |
1221 | struct nlmsghdr *n, | |
1222 | struct zebra_dplane_info *dp_info, bool startup) | |
1223 | { | |
1224 | struct nlsock *nl; | |
1225 | ||
1226 | nl = kernel_netlink_nlsock_lookup(dp_info->sock); | |
1227 | n->nlmsg_seq = dp_info->seq; | |
1228 | n->nlmsg_pid = nl->snl.nl_pid; | |
1229 | ||
1230 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1231 | zlog_debug( | |
1232 | "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x", | |
1233 | nl->name, nl_msg_type_to_str(n->nlmsg_type), | |
1234 | n->nlmsg_type, n->nlmsg_len, n->nlmsg_seq, | |
1235 | n->nlmsg_flags); | |
1236 | ||
1237 | if (netlink_send_msg(nl, n, n->nlmsg_len) == -1) | |
1238 | return -1; | |
1239 | ||
1240 | /* | |
1241 | * Get reply from netlink socket. | |
1242 | * The reply should either be an acknowlegement or an error. | |
1243 | */ | |
1244 | return netlink_parse_info(filter, nl, dp_info, 0, startup); | |
1245 | } | |
1246 | ||
1247 | /* | |
1248 | * Synchronous version of netlink_talk_info. Converts args to suit the | |
1249 | * common version, which is suitable for both sync and async use. | |
1250 | */ | |
1251 | int netlink_talk(int (*filter)(struct nlmsghdr *, ns_id_t, int startup), | |
1252 | struct nlmsghdr *n, struct nlsock *nl, struct zebra_ns *zns, | |
1253 | bool startup) | |
1254 | { | |
1255 | struct zebra_dplane_info dp_info; | |
1256 | ||
1257 | /* Increment sequence number before capturing snapshot of ns socket | |
1258 | * info. | |
1259 | */ | |
1260 | nl->seq++; | |
1261 | ||
1262 | /* Capture info in intermediate info struct */ | |
1263 | zebra_dplane_info_from_zns(&dp_info, zns, (nl == &(zns->netlink_cmd))); | |
1264 | ||
1265 | return netlink_talk_info(filter, n, &dp_info, startup); | |
1266 | } | |
1267 | ||
1268 | /* Issue request message to kernel via netlink socket. GET messages | |
1269 | * are issued through this interface. | |
1270 | */ | |
1271 | int netlink_request(struct nlsock *nl, void *req) | |
1272 | { | |
1273 | struct nlmsghdr *n = (struct nlmsghdr *)req; | |
1274 | ||
1275 | /* Check netlink socket. */ | |
1276 | if (nl->sock < 0) { | |
1277 | flog_err_sys(EC_LIB_SOCKET, "%s socket isn't active.", | |
1278 | nl->name); | |
1279 | return -1; | |
1280 | } | |
1281 | ||
1282 | /* Fill common fields for all requests. */ | |
1283 | n->nlmsg_pid = nl->snl.nl_pid; | |
1284 | n->nlmsg_seq = ++nl->seq; | |
1285 | ||
1286 | if (netlink_send_msg(nl, req, n->nlmsg_len) == -1) | |
1287 | return -1; | |
1288 | ||
1289 | return 0; | |
1290 | } | |
1291 | ||
1292 | static int nl_batch_read_resp(struct nl_batch *bth) | |
1293 | { | |
1294 | struct nlmsghdr *h; | |
1295 | struct sockaddr_nl snl; | |
1296 | struct msghdr msg = {}; | |
1297 | int status, seq; | |
1298 | struct nlsock *nl; | |
1299 | struct zebra_dplane_ctx *ctx; | |
1300 | bool ignore_msg; | |
1301 | ||
1302 | nl = kernel_netlink_nlsock_lookup(bth->zns->sock); | |
1303 | ||
1304 | msg.msg_name = (void *)&snl; | |
1305 | msg.msg_namelen = sizeof(snl); | |
1306 | ||
1307 | /* | |
1308 | * The responses are not batched, so we need to read and process one | |
1309 | * message at a time. | |
1310 | */ | |
1311 | while (true) { | |
1312 | status = netlink_recv_msg(nl, &msg); | |
1313 | /* | |
1314 | * status == -1 is a full on failure somewhere | |
1315 | * since we don't know where the problem happened | |
1316 | * we must mark all as failed | |
1317 | * | |
1318 | * Else we mark everything as worked | |
1319 | * | |
1320 | */ | |
1321 | if (status == -1 || status == 0) { | |
1322 | while ((ctx = dplane_ctx_dequeue(&(bth->ctx_list))) != | |
1323 | NULL) { | |
1324 | if (status == -1) | |
1325 | dplane_ctx_set_status( | |
1326 | ctx, | |
1327 | ZEBRA_DPLANE_REQUEST_FAILURE); | |
1328 | dplane_ctx_enqueue_tail(bth->ctx_out_q, ctx); | |
1329 | } | |
1330 | return status; | |
1331 | } | |
1332 | ||
1333 | h = (struct nlmsghdr *)nl->buf; | |
1334 | ignore_msg = false; | |
1335 | seq = h->nlmsg_seq; | |
1336 | /* | |
1337 | * Find the corresponding context object. Received responses are | |
1338 | * in the same order as requests we sent, so we can simply | |
1339 | * iterate over the context list and match responses with | |
1340 | * requests at same time. | |
1341 | */ | |
1342 | while (true) { | |
1343 | ctx = dplane_ctx_get_head(&(bth->ctx_list)); | |
1344 | if (ctx == NULL) { | |
1345 | /* | |
1346 | * This is a situation where we have gotten | |
1347 | * into a bad spot. We need to know that | |
1348 | * this happens( does it? ) | |
1349 | */ | |
1350 | zlog_err( | |
1351 | "%s:WARNING Received netlink Response for an error and no Contexts to associate with it", | |
1352 | __func__); | |
1353 | break; | |
1354 | } | |
1355 | ||
1356 | /* | |
1357 | * 'update' context objects take two consecutive | |
1358 | * sequence numbers. | |
1359 | */ | |
1360 | if (dplane_ctx_is_update(ctx) && | |
1361 | dplane_ctx_get_ns(ctx)->seq + 1 == seq) { | |
1362 | /* | |
1363 | * This is the situation where we get a response | |
1364 | * to a message that should be ignored. | |
1365 | */ | |
1366 | ignore_msg = true; | |
1367 | break; | |
1368 | } | |
1369 | ||
1370 | ctx = dplane_ctx_dequeue(&(bth->ctx_list)); | |
1371 | dplane_ctx_enqueue_tail(bth->ctx_out_q, ctx); | |
1372 | ||
1373 | /* We have found corresponding context object. */ | |
1374 | if (dplane_ctx_get_ns(ctx)->seq == seq) | |
1375 | break; | |
1376 | ||
1377 | if (dplane_ctx_get_ns(ctx)->seq > seq) | |
1378 | zlog_warn( | |
1379 | "%s:WARNING Received %u is less than any context on the queue ctx->seq %u", | |
1380 | __func__, seq, | |
1381 | dplane_ctx_get_ns(ctx)->seq); | |
1382 | } | |
1383 | ||
1384 | if (ignore_msg) { | |
1385 | /* | |
1386 | * If we ignore the message due to an update | |
1387 | * above we should still fricking decode the | |
1388 | * message for our operator to understand | |
1389 | * what is going on | |
1390 | */ | |
1391 | int err = netlink_parse_error(nl, h, bth->zns->is_cmd, | |
1392 | false); | |
1393 | ||
1394 | zlog_debug("%s: netlink error message seq=%d %d", | |
1395 | __func__, h->nlmsg_seq, err); | |
1396 | continue; | |
1397 | } | |
1398 | ||
1399 | /* | |
1400 | * We received a message with the sequence number that isn't | |
1401 | * associated with any dplane context object. | |
1402 | */ | |
1403 | if (ctx == NULL) { | |
1404 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1405 | zlog_debug( | |
1406 | "%s: skipping unassociated response, seq number %d NS %u", | |
1407 | __func__, h->nlmsg_seq, | |
1408 | bth->zns->ns_id); | |
1409 | continue; | |
1410 | } | |
1411 | ||
1412 | if (h->nlmsg_type == NLMSG_ERROR) { | |
1413 | int err = netlink_parse_error(nl, h, bth->zns->is_cmd, | |
1414 | false); | |
1415 | ||
1416 | if (err == -1) | |
1417 | dplane_ctx_set_status( | |
1418 | ctx, ZEBRA_DPLANE_REQUEST_FAILURE); | |
1419 | ||
1420 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1421 | zlog_debug("%s: netlink error message seq=%d ", | |
1422 | __func__, h->nlmsg_seq); | |
1423 | continue; | |
1424 | } | |
1425 | ||
1426 | /* | |
1427 | * If we get here then we did not receive neither the ack nor | |
1428 | * the error and instead received some other message in an | |
1429 | * unexpected way. | |
1430 | */ | |
1431 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1432 | zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", | |
1433 | __func__, h->nlmsg_type, | |
1434 | nl_msg_type_to_str(h->nlmsg_type), | |
1435 | bth->zns->ns_id); | |
1436 | } | |
1437 | ||
1438 | return 0; | |
1439 | } | |
1440 | ||
1441 | static void nl_batch_reset(struct nl_batch *bth) | |
1442 | { | |
1443 | bth->buf_head = bth->buf; | |
1444 | bth->curlen = 0; | |
1445 | bth->msgcnt = 0; | |
1446 | bth->zns = NULL; | |
1447 | ||
1448 | dplane_ctx_q_init(&(bth->ctx_list)); | |
1449 | } | |
1450 | ||
1451 | static void nl_batch_init(struct nl_batch *bth, | |
1452 | struct dplane_ctx_list_head *ctx_out_q) | |
1453 | { | |
1454 | /* | |
1455 | * If the size of the buffer has changed, free and then allocate a new | |
1456 | * one. | |
1457 | */ | |
1458 | size_t bufsize = | |
1459 | atomic_load_explicit(&nl_batch_bufsize, memory_order_relaxed); | |
1460 | if (bufsize != nl_batch_tx_bufsize) { | |
1461 | if (nl_batch_tx_buf) | |
1462 | XFREE(MTYPE_NL_BUF, nl_batch_tx_buf); | |
1463 | ||
1464 | nl_batch_tx_buf = XCALLOC(MTYPE_NL_BUF, bufsize); | |
1465 | nl_batch_tx_bufsize = bufsize; | |
1466 | } | |
1467 | ||
1468 | bth->buf = nl_batch_tx_buf; | |
1469 | bth->bufsiz = bufsize; | |
1470 | bth->limit = atomic_load_explicit(&nl_batch_send_threshold, | |
1471 | memory_order_relaxed); | |
1472 | ||
1473 | bth->ctx_out_q = ctx_out_q; | |
1474 | ||
1475 | nl_batch_reset(bth); | |
1476 | } | |
1477 | ||
1478 | static void nl_batch_send(struct nl_batch *bth) | |
1479 | { | |
1480 | struct zebra_dplane_ctx *ctx; | |
1481 | bool err = false; | |
1482 | ||
1483 | if (bth->curlen != 0 && bth->zns != NULL) { | |
1484 | struct nlsock *nl = | |
1485 | kernel_netlink_nlsock_lookup(bth->zns->sock); | |
1486 | ||
1487 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1488 | zlog_debug("%s: %s, batch size=%zu, msg cnt=%zu", | |
1489 | __func__, nl->name, bth->curlen, | |
1490 | bth->msgcnt); | |
1491 | ||
1492 | if (netlink_send_msg(nl, bth->buf, bth->curlen) == -1) | |
1493 | err = true; | |
1494 | ||
1495 | if (!err) { | |
1496 | if (nl_batch_read_resp(bth) == -1) | |
1497 | err = true; | |
1498 | } | |
1499 | } | |
1500 | ||
1501 | /* Move remaining contexts to the outbound queue. */ | |
1502 | while (true) { | |
1503 | ctx = dplane_ctx_dequeue(&(bth->ctx_list)); | |
1504 | if (ctx == NULL) | |
1505 | break; | |
1506 | ||
1507 | if (err) | |
1508 | dplane_ctx_set_status(ctx, | |
1509 | ZEBRA_DPLANE_REQUEST_FAILURE); | |
1510 | ||
1511 | dplane_ctx_enqueue_tail(bth->ctx_out_q, ctx); | |
1512 | } | |
1513 | ||
1514 | nl_batch_reset(bth); | |
1515 | } | |
1516 | ||
1517 | enum netlink_msg_status netlink_batch_add_msg( | |
1518 | struct nl_batch *bth, struct zebra_dplane_ctx *ctx, | |
1519 | ssize_t (*msg_encoder)(struct zebra_dplane_ctx *, void *, size_t), | |
1520 | bool ignore_res) | |
1521 | { | |
1522 | int seq; | |
1523 | ssize_t size; | |
1524 | struct nlmsghdr *msgh; | |
1525 | struct nlsock *nl; | |
1526 | ||
1527 | size = (*msg_encoder)(ctx, bth->buf_head, bth->bufsiz - bth->curlen); | |
1528 | ||
1529 | /* | |
1530 | * If there was an error while encoding the message (other than buffer | |
1531 | * overflow) then return an error. | |
1532 | */ | |
1533 | if (size < 0) | |
1534 | return FRR_NETLINK_ERROR; | |
1535 | ||
1536 | /* | |
1537 | * If the message doesn't fit entirely in the buffer then send the batch | |
1538 | * and retry. | |
1539 | */ | |
1540 | if (size == 0) { | |
1541 | nl_batch_send(bth); | |
1542 | size = (*msg_encoder)(ctx, bth->buf_head, | |
1543 | bth->bufsiz - bth->curlen); | |
1544 | /* | |
1545 | * If the message doesn't fit in the empty buffer then just | |
1546 | * return an error. | |
1547 | */ | |
1548 | if (size <= 0) | |
1549 | return FRR_NETLINK_ERROR; | |
1550 | } | |
1551 | ||
1552 | seq = dplane_ctx_get_ns(ctx)->seq; | |
1553 | nl = kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx)); | |
1554 | ||
1555 | if (ignore_res) | |
1556 | seq++; | |
1557 | ||
1558 | msgh = (struct nlmsghdr *)bth->buf_head; | |
1559 | msgh->nlmsg_seq = seq; | |
1560 | msgh->nlmsg_pid = nl->snl.nl_pid; | |
1561 | ||
1562 | bth->zns = dplane_ctx_get_ns(ctx); | |
1563 | bth->buf_head = ((char *)bth->buf_head) + size; | |
1564 | bth->curlen += size; | |
1565 | bth->msgcnt++; | |
1566 | ||
1567 | return FRR_NETLINK_QUEUED; | |
1568 | } | |
1569 | ||
1570 | static enum netlink_msg_status nl_put_msg(struct nl_batch *bth, | |
1571 | struct zebra_dplane_ctx *ctx) | |
1572 | { | |
1573 | if (dplane_ctx_is_skip_kernel(ctx)) | |
1574 | return FRR_NETLINK_SUCCESS; | |
1575 | ||
1576 | switch (dplane_ctx_get_op(ctx)) { | |
1577 | ||
1578 | case DPLANE_OP_ROUTE_INSTALL: | |
1579 | case DPLANE_OP_ROUTE_UPDATE: | |
1580 | case DPLANE_OP_ROUTE_DELETE: | |
1581 | return netlink_put_route_update_msg(bth, ctx); | |
1582 | ||
1583 | case DPLANE_OP_NH_INSTALL: | |
1584 | case DPLANE_OP_NH_UPDATE: | |
1585 | case DPLANE_OP_NH_DELETE: | |
1586 | return netlink_put_nexthop_update_msg(bth, ctx); | |
1587 | ||
1588 | case DPLANE_OP_LSP_INSTALL: | |
1589 | case DPLANE_OP_LSP_UPDATE: | |
1590 | case DPLANE_OP_LSP_DELETE: | |
1591 | return netlink_put_lsp_update_msg(bth, ctx); | |
1592 | ||
1593 | case DPLANE_OP_PW_INSTALL: | |
1594 | case DPLANE_OP_PW_UNINSTALL: | |
1595 | return netlink_put_pw_update_msg(bth, ctx); | |
1596 | ||
1597 | case DPLANE_OP_ADDR_INSTALL: | |
1598 | case DPLANE_OP_ADDR_UNINSTALL: | |
1599 | return netlink_put_address_update_msg(bth, ctx); | |
1600 | ||
1601 | case DPLANE_OP_MAC_INSTALL: | |
1602 | case DPLANE_OP_MAC_DELETE: | |
1603 | return netlink_put_mac_update_msg(bth, ctx); | |
1604 | ||
1605 | case DPLANE_OP_NEIGH_INSTALL: | |
1606 | case DPLANE_OP_NEIGH_UPDATE: | |
1607 | case DPLANE_OP_NEIGH_DELETE: | |
1608 | case DPLANE_OP_VTEP_ADD: | |
1609 | case DPLANE_OP_VTEP_DELETE: | |
1610 | case DPLANE_OP_NEIGH_DISCOVER: | |
1611 | case DPLANE_OP_NEIGH_IP_INSTALL: | |
1612 | case DPLANE_OP_NEIGH_IP_DELETE: | |
1613 | case DPLANE_OP_NEIGH_TABLE_UPDATE: | |
1614 | return netlink_put_neigh_update_msg(bth, ctx); | |
1615 | ||
1616 | case DPLANE_OP_RULE_ADD: | |
1617 | case DPLANE_OP_RULE_DELETE: | |
1618 | case DPLANE_OP_RULE_UPDATE: | |
1619 | return netlink_put_rule_update_msg(bth, ctx); | |
1620 | ||
1621 | case DPLANE_OP_SYS_ROUTE_ADD: | |
1622 | case DPLANE_OP_SYS_ROUTE_DELETE: | |
1623 | case DPLANE_OP_ROUTE_NOTIFY: | |
1624 | case DPLANE_OP_LSP_NOTIFY: | |
1625 | case DPLANE_OP_BR_PORT_UPDATE: | |
1626 | return FRR_NETLINK_SUCCESS; | |
1627 | ||
1628 | case DPLANE_OP_IPTABLE_ADD: | |
1629 | case DPLANE_OP_IPTABLE_DELETE: | |
1630 | case DPLANE_OP_IPSET_ADD: | |
1631 | case DPLANE_OP_IPSET_DELETE: | |
1632 | case DPLANE_OP_IPSET_ENTRY_ADD: | |
1633 | case DPLANE_OP_IPSET_ENTRY_DELETE: | |
1634 | return FRR_NETLINK_ERROR; | |
1635 | ||
1636 | case DPLANE_OP_GRE_SET: | |
1637 | return netlink_put_gre_set_msg(bth, ctx); | |
1638 | ||
1639 | case DPLANE_OP_INTF_ADDR_ADD: | |
1640 | case DPLANE_OP_INTF_ADDR_DEL: | |
1641 | case DPLANE_OP_NONE: | |
1642 | return FRR_NETLINK_ERROR; | |
1643 | ||
1644 | case DPLANE_OP_INTF_NETCONFIG: | |
1645 | return netlink_put_intf_netconfig(bth, ctx); | |
1646 | ||
1647 | case DPLANE_OP_INTF_INSTALL: | |
1648 | case DPLANE_OP_INTF_UPDATE: | |
1649 | case DPLANE_OP_INTF_DELETE: | |
1650 | return netlink_put_intf_update_msg(bth, ctx); | |
1651 | ||
1652 | case DPLANE_OP_TC_QDISC_INSTALL: | |
1653 | case DPLANE_OP_TC_QDISC_UNINSTALL: | |
1654 | return netlink_put_tc_qdisc_update_msg(bth, ctx); | |
1655 | case DPLANE_OP_TC_CLASS_ADD: | |
1656 | case DPLANE_OP_TC_CLASS_DELETE: | |
1657 | case DPLANE_OP_TC_CLASS_UPDATE: | |
1658 | return netlink_put_tc_class_update_msg(bth, ctx); | |
1659 | case DPLANE_OP_TC_FILTER_ADD: | |
1660 | case DPLANE_OP_TC_FILTER_DELETE: | |
1661 | case DPLANE_OP_TC_FILTER_UPDATE: | |
1662 | return netlink_put_tc_filter_update_msg(bth, ctx); | |
1663 | } | |
1664 | ||
1665 | return FRR_NETLINK_ERROR; | |
1666 | } | |
1667 | ||
1668 | void kernel_update_multi(struct dplane_ctx_list_head *ctx_list) | |
1669 | { | |
1670 | struct nl_batch batch; | |
1671 | struct zebra_dplane_ctx *ctx; | |
1672 | struct dplane_ctx_list_head handled_list; | |
1673 | enum netlink_msg_status res; | |
1674 | ||
1675 | dplane_ctx_q_init(&handled_list); | |
1676 | nl_batch_init(&batch, &handled_list); | |
1677 | ||
1678 | while (true) { | |
1679 | ctx = dplane_ctx_dequeue(ctx_list); | |
1680 | if (ctx == NULL) | |
1681 | break; | |
1682 | ||
1683 | if (batch.zns != NULL | |
1684 | && batch.zns->ns_id != dplane_ctx_get_ns(ctx)->ns_id) | |
1685 | nl_batch_send(&batch); | |
1686 | ||
1687 | /* | |
1688 | * Assume all messages will succeed and then mark only the ones | |
1689 | * that failed. | |
1690 | */ | |
1691 | dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS); | |
1692 | ||
1693 | res = nl_put_msg(&batch, ctx); | |
1694 | ||
1695 | dplane_ctx_enqueue_tail(&(batch.ctx_list), ctx); | |
1696 | if (res == FRR_NETLINK_ERROR) | |
1697 | dplane_ctx_set_status(ctx, | |
1698 | ZEBRA_DPLANE_REQUEST_FAILURE); | |
1699 | ||
1700 | if (batch.curlen > batch.limit) | |
1701 | nl_batch_send(&batch); | |
1702 | } | |
1703 | ||
1704 | nl_batch_send(&batch); | |
1705 | ||
1706 | dplane_ctx_q_init(ctx_list); | |
1707 | dplane_ctx_list_append(ctx_list, &handled_list); | |
1708 | } | |
1709 | ||
1710 | struct nlsock *kernel_netlink_nlsock_lookup(int sock) | |
1711 | { | |
1712 | struct nlsock lookup, *retval; | |
1713 | ||
1714 | lookup.sock = sock; | |
1715 | ||
1716 | NLSOCK_LOCK(); | |
1717 | retval = hash_lookup(nlsock_hash, &lookup); | |
1718 | NLSOCK_UNLOCK(); | |
1719 | ||
1720 | return retval; | |
1721 | } | |
1722 | ||
1723 | /* Insert nlsock entry into hash */ | |
1724 | static void kernel_netlink_nlsock_insert(struct nlsock *nls) | |
1725 | { | |
1726 | NLSOCK_LOCK(); | |
1727 | (void)hash_get(nlsock_hash, nls, hash_alloc_intern); | |
1728 | NLSOCK_UNLOCK(); | |
1729 | } | |
1730 | ||
1731 | /* Remove nlsock entry from hash */ | |
1732 | static void kernel_netlink_nlsock_remove(struct nlsock *nls) | |
1733 | { | |
1734 | NLSOCK_LOCK(); | |
1735 | (void)hash_release(nlsock_hash, nls); | |
1736 | NLSOCK_UNLOCK(); | |
1737 | } | |
1738 | ||
1739 | static uint32_t kernel_netlink_nlsock_key(const void *arg) | |
1740 | { | |
1741 | const struct nlsock *nl = arg; | |
1742 | ||
1743 | return nl->sock; | |
1744 | } | |
1745 | ||
1746 | static bool kernel_netlink_nlsock_hash_equal(const void *arg1, const void *arg2) | |
1747 | { | |
1748 | const struct nlsock *nl1 = arg1; | |
1749 | const struct nlsock *nl2 = arg2; | |
1750 | ||
1751 | if (nl1->sock == nl2->sock) | |
1752 | return true; | |
1753 | ||
1754 | return false; | |
1755 | } | |
1756 | ||
1757 | /* Exported interface function. This function simply calls | |
1758 | netlink_socket (). */ | |
1759 | void kernel_init(struct zebra_ns *zns) | |
1760 | { | |
1761 | uint32_t groups, dplane_groups, ext_groups; | |
1762 | #if defined SOL_NETLINK | |
1763 | int one, ret, grp; | |
1764 | #endif | |
1765 | ||
1766 | /* | |
1767 | * Initialize netlink sockets | |
1768 | * | |
1769 | * If RTMGRP_XXX exists use that, but at some point | |
1770 | * I think the kernel developers realized that | |
1771 | * keeping track of all the different values would | |
1772 | * lead to confusion, so we need to convert the | |
1773 | * RTNLGRP_XXX to a bit position for ourself | |
1774 | * | |
1775 | * | |
1776 | * NOTE: If the bit is >= 32, you must use setsockopt(). Those | |
1777 | * groups are added further below after SOL_NETLINK is verified to | |
1778 | * exist. | |
1779 | */ | |
1780 | groups = RTMGRP_LINK | | |
1781 | RTMGRP_IPV4_ROUTE | | |
1782 | RTMGRP_IPV4_IFADDR | | |
1783 | RTMGRP_IPV6_ROUTE | | |
1784 | RTMGRP_IPV6_IFADDR | | |
1785 | RTMGRP_IPV4_MROUTE | | |
1786 | RTMGRP_NEIGH | | |
1787 | ((uint32_t) 1 << (RTNLGRP_IPV4_RULE - 1)) | | |
1788 | ((uint32_t) 1 << (RTNLGRP_IPV6_RULE - 1)) | | |
1789 | ((uint32_t) 1 << (RTNLGRP_NEXTHOP - 1)) | | |
1790 | ((uint32_t) 1 << (RTNLGRP_TC - 1)); | |
1791 | ||
1792 | dplane_groups = (RTMGRP_LINK | | |
1793 | RTMGRP_IPV4_IFADDR | | |
1794 | RTMGRP_IPV6_IFADDR | | |
1795 | ((uint32_t) 1 << (RTNLGRP_IPV4_NETCONF - 1)) | | |
1796 | ((uint32_t) 1 << (RTNLGRP_IPV6_NETCONF - 1)) | | |
1797 | ((uint32_t) 1 << (RTNLGRP_MPLS_NETCONF - 1))); | |
1798 | ||
1799 | /* Use setsockopt for > 31 group */ | |
1800 | ext_groups = RTNLGRP_TUNNEL; | |
1801 | ||
1802 | snprintf(zns->netlink.name, sizeof(zns->netlink.name), | |
1803 | "netlink-listen (NS %u)", zns->ns_id); | |
1804 | zns->netlink.sock = -1; | |
1805 | if (netlink_socket(&zns->netlink, groups, &ext_groups, 1, zns->ns_id) < | |
1806 | 0) { | |
1807 | zlog_err("Failure to create %s socket", | |
1808 | zns->netlink.name); | |
1809 | exit(-1); | |
1810 | } | |
1811 | ||
1812 | kernel_netlink_nlsock_insert(&zns->netlink); | |
1813 | ||
1814 | snprintf(zns->netlink_cmd.name, sizeof(zns->netlink_cmd.name), | |
1815 | "netlink-cmd (NS %u)", zns->ns_id); | |
1816 | zns->netlink_cmd.sock = -1; | |
1817 | if (netlink_socket(&zns->netlink_cmd, 0, 0, 0, zns->ns_id) < 0) { | |
1818 | zlog_err("Failure to create %s socket", | |
1819 | zns->netlink_cmd.name); | |
1820 | exit(-1); | |
1821 | } | |
1822 | ||
1823 | kernel_netlink_nlsock_insert(&zns->netlink_cmd); | |
1824 | ||
1825 | /* Outbound socket for dplane programming of the host OS. */ | |
1826 | snprintf(zns->netlink_dplane_out.name, | |
1827 | sizeof(zns->netlink_dplane_out.name), "netlink-dp (NS %u)", | |
1828 | zns->ns_id); | |
1829 | zns->netlink_dplane_out.sock = -1; | |
1830 | if (netlink_socket(&zns->netlink_dplane_out, 0, 0, 0, zns->ns_id) < 0) { | |
1831 | zlog_err("Failure to create %s socket", | |
1832 | zns->netlink_dplane_out.name); | |
1833 | exit(-1); | |
1834 | } | |
1835 | ||
1836 | kernel_netlink_nlsock_insert(&zns->netlink_dplane_out); | |
1837 | ||
1838 | /* Inbound socket for OS events coming to the dplane. */ | |
1839 | snprintf(zns->netlink_dplane_in.name, | |
1840 | sizeof(zns->netlink_dplane_in.name), "netlink-dp-in (NS %u)", | |
1841 | zns->ns_id); | |
1842 | zns->netlink_dplane_in.sock = -1; | |
1843 | if (netlink_socket(&zns->netlink_dplane_in, dplane_groups, 0, 0, | |
1844 | zns->ns_id) < 0) { | |
1845 | zlog_err("Failure to create %s socket", | |
1846 | zns->netlink_dplane_in.name); | |
1847 | exit(-1); | |
1848 | } | |
1849 | ||
1850 | kernel_netlink_nlsock_insert(&zns->netlink_dplane_in); | |
1851 | ||
1852 | /* | |
1853 | * SOL_NETLINK is not available on all platforms yet | |
1854 | * apparently. It's in bits/socket.h which I am not | |
1855 | * sure that we want to pull into our build system. | |
1856 | */ | |
1857 | #if defined SOL_NETLINK | |
1858 | ||
1859 | /* | |
1860 | * setsockopt multicast group subscriptions that don't fit in nl_groups | |
1861 | */ | |
1862 | grp = RTNLGRP_BRVLAN; | |
1863 | ret = setsockopt(zns->netlink.sock, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, | |
1864 | &grp, sizeof(grp)); | |
1865 | ||
1866 | if (ret < 0) | |
1867 | zlog_notice( | |
1868 | "Registration for RTNLGRP_BRVLAN Membership failed : %d %s", | |
1869 | errno, safe_strerror(errno)); | |
1870 | /* | |
1871 | * Let's tell the kernel that we want to receive extended | |
1872 | * ACKS over our command socket(s) | |
1873 | */ | |
1874 | one = 1; | |
1875 | ret = setsockopt(zns->netlink_cmd.sock, SOL_NETLINK, NETLINK_EXT_ACK, | |
1876 | &one, sizeof(one)); | |
1877 | ||
1878 | if (ret < 0) | |
1879 | zlog_notice("Registration for extended cmd ACK failed : %d %s", | |
1880 | errno, safe_strerror(errno)); | |
1881 | ||
1882 | one = 1; | |
1883 | ret = setsockopt(zns->netlink_dplane_out.sock, SOL_NETLINK, | |
1884 | NETLINK_EXT_ACK, &one, sizeof(one)); | |
1885 | ||
1886 | if (ret < 0) | |
1887 | zlog_notice("Registration for extended dp ACK failed : %d %s", | |
1888 | errno, safe_strerror(errno)); | |
1889 | ||
1890 | /* | |
1891 | * Trim off the payload of the original netlink message in the | |
1892 | * acknowledgment. This option is available since Linux 4.2, so if | |
1893 | * setsockopt fails, ignore the error. | |
1894 | */ | |
1895 | one = 1; | |
1896 | ret = setsockopt(zns->netlink_dplane_out.sock, SOL_NETLINK, | |
1897 | NETLINK_CAP_ACK, &one, sizeof(one)); | |
1898 | if (ret < 0) | |
1899 | zlog_notice( | |
1900 | "Registration for reduced ACK packet size failed, probably running an early kernel"); | |
1901 | #endif | |
1902 | ||
1903 | /* Register kernel socket. */ | |
1904 | if (fcntl(zns->netlink.sock, F_SETFL, O_NONBLOCK) < 0) | |
1905 | flog_err_sys(EC_LIB_SOCKET, "Can't set %s socket flags: %s", | |
1906 | zns->netlink.name, safe_strerror(errno)); | |
1907 | ||
1908 | if (fcntl(zns->netlink_cmd.sock, F_SETFL, O_NONBLOCK) < 0) | |
1909 | zlog_err("Can't set %s socket error: %s(%d)", | |
1910 | zns->netlink_cmd.name, safe_strerror(errno), errno); | |
1911 | ||
1912 | if (fcntl(zns->netlink_dplane_out.sock, F_SETFL, O_NONBLOCK) < 0) | |
1913 | zlog_err("Can't set %s socket error: %s(%d)", | |
1914 | zns->netlink_dplane_out.name, safe_strerror(errno), | |
1915 | errno); | |
1916 | ||
1917 | if (fcntl(zns->netlink_dplane_in.sock, F_SETFL, O_NONBLOCK) < 0) | |
1918 | zlog_err("Can't set %s socket error: %s(%d)", | |
1919 | zns->netlink_dplane_in.name, safe_strerror(errno), | |
1920 | errno); | |
1921 | ||
1922 | /* Set receive buffer size if it's set from command line */ | |
1923 | if (rcvbufsize) { | |
1924 | netlink_recvbuf(&zns->netlink, rcvbufsize); | |
1925 | netlink_recvbuf(&zns->netlink_cmd, rcvbufsize); | |
1926 | netlink_recvbuf(&zns->netlink_dplane_out, rcvbufsize); | |
1927 | netlink_recvbuf(&zns->netlink_dplane_in, rcvbufsize); | |
1928 | } | |
1929 | ||
1930 | /* Set filter for inbound sockets, to exclude events we've generated | |
1931 | * ourselves. | |
1932 | */ | |
1933 | netlink_install_filter(zns->netlink.sock, zns->netlink_cmd.snl.nl_pid, | |
1934 | zns->netlink_dplane_out.snl.nl_pid); | |
1935 | ||
1936 | netlink_install_filter(zns->netlink_dplane_in.sock, | |
1937 | zns->netlink_cmd.snl.nl_pid, | |
1938 | zns->netlink_dplane_out.snl.nl_pid); | |
1939 | ||
1940 | zns->t_netlink = NULL; | |
1941 | ||
1942 | thread_add_read(zrouter.master, kernel_read, zns, | |
1943 | zns->netlink.sock, &zns->t_netlink); | |
1944 | ||
1945 | rt_netlink_init(); | |
1946 | } | |
1947 | ||
1948 | /* Helper to clean up an nlsock */ | |
1949 | static void kernel_nlsock_fini(struct nlsock *nls) | |
1950 | { | |
1951 | if (nls && nls->sock >= 0) { | |
1952 | kernel_netlink_nlsock_remove(nls); | |
1953 | close(nls->sock); | |
1954 | nls->sock = -1; | |
1955 | XFREE(MTYPE_NL_BUF, nls->buf); | |
1956 | nls->buflen = 0; | |
1957 | } | |
1958 | } | |
1959 | ||
1960 | void kernel_terminate(struct zebra_ns *zns, bool complete) | |
1961 | { | |
1962 | THREAD_OFF(zns->t_netlink); | |
1963 | ||
1964 | kernel_nlsock_fini(&zns->netlink); | |
1965 | ||
1966 | kernel_nlsock_fini(&zns->netlink_cmd); | |
1967 | ||
1968 | kernel_nlsock_fini(&zns->netlink_dplane_in); | |
1969 | ||
1970 | /* During zebra shutdown, we need to leave the dataplane socket | |
1971 | * around until all work is done. | |
1972 | */ | |
1973 | if (complete) | |
1974 | kernel_nlsock_fini(&zns->netlink_dplane_out); | |
1975 | } | |
1976 | ||
1977 | /* | |
1978 | * Global init for platform-/OS-specific things | |
1979 | */ | |
1980 | void kernel_router_init(void) | |
1981 | { | |
1982 | /* Init nlsock hash and lock */ | |
1983 | pthread_mutex_init(&nlsock_mutex, NULL); | |
1984 | nlsock_hash = hash_create_size(8, kernel_netlink_nlsock_key, | |
1985 | kernel_netlink_nlsock_hash_equal, | |
1986 | "Netlink Socket Hash"); | |
1987 | } | |
1988 | ||
1989 | /* | |
1990 | * Global deinit for platform-/OS-specific things | |
1991 | */ | |
1992 | void kernel_router_terminate(void) | |
1993 | { | |
1994 | pthread_mutex_destroy(&nlsock_mutex); | |
1995 | ||
1996 | hash_free(nlsock_hash); | |
1997 | nlsock_hash = NULL; | |
1998 | } | |
1999 | ||
2000 | #endif /* HAVE_NETLINK */ |