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