]>
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" | |
48 | ||
49 | #ifndef SO_RCVBUFFORCE | |
50 | #define SO_RCVBUFFORCE (33) | |
51 | #endif | |
52 | ||
53 | /* Hack for GNU libc version 2. */ | |
54 | #ifndef MSG_TRUNC | |
55 | #define MSG_TRUNC 0x20 | |
56 | #endif /* MSG_TRUNC */ | |
57 | ||
58 | #ifndef NLMSG_TAIL | |
d62a17ae | 59 | #define NLMSG_TAIL(nmsg) \ |
60 | ((struct rtattr *)(((u_char *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) | |
1fdc9eae | 61 | #endif |
62 | ||
63 | #ifndef RTA_TAIL | |
d62a17ae | 64 | #define RTA_TAIL(rta) \ |
65 | ((struct rtattr *)(((u_char *)(rta)) + RTA_ALIGN((rta)->rta_len))) | |
1fdc9eae | 66 | #endif |
67 | ||
f909c673 DS |
68 | #ifndef RTNL_FAMILY_IP6MR |
69 | #define RTNL_FAMILY_IP6MR 129 | |
70 | #endif | |
71 | ||
72 | #ifndef RTPROT_MROUTED | |
73 | #define RTPROT_MROUTED 17 | |
74 | #endif | |
75 | ||
d62a17ae | 76 | static const struct message nlmsg_str[] = {{RTM_NEWROUTE, "RTM_NEWROUTE"}, |
77 | {RTM_DELROUTE, "RTM_DELROUTE"}, | |
78 | {RTM_GETROUTE, "RTM_GETROUTE"}, | |
79 | {RTM_NEWLINK, "RTM_NEWLINK"}, | |
80 | {RTM_DELLINK, "RTM_DELLINK"}, | |
81 | {RTM_GETLINK, "RTM_GETLINK"}, | |
82 | {RTM_NEWADDR, "RTM_NEWADDR"}, | |
83 | {RTM_DELADDR, "RTM_DELADDR"}, | |
84 | {RTM_GETADDR, "RTM_GETADDR"}, | |
85 | {RTM_NEWNEIGH, "RTM_NEWNEIGH"}, | |
86 | {RTM_DELNEIGH, "RTM_DELNEIGH"}, | |
87 | {RTM_GETNEIGH, "RTM_GETNEIGH"}, | |
88 | {0}}; | |
1fdc9eae | 89 | |
90 | static const struct message rtproto_str[] = { | |
d62a17ae | 91 | {RTPROT_REDIRECT, "redirect"}, |
92 | {RTPROT_KERNEL, "kernel"}, | |
93 | {RTPROT_BOOT, "boot"}, | |
94 | {RTPROT_STATIC, "static"}, | |
95 | {RTPROT_GATED, "GateD"}, | |
96 | {RTPROT_RA, "router advertisement"}, | |
97 | {RTPROT_MRT, "MRT"}, | |
98 | {RTPROT_ZEBRA, "Zebra"}, | |
1fdc9eae | 99 | #ifdef RTPROT_BIRD |
d62a17ae | 100 | {RTPROT_BIRD, "BIRD"}, |
1fdc9eae | 101 | #endif /* RTPROT_BIRD */ |
d62a17ae | 102 | {RTPROT_MROUTED, "mroute"}, |
103 | {RTPROT_BGP, "BGP"}, | |
104 | {RTPROT_OSPF, "OSPF"}, | |
105 | {RTPROT_ISIS, "IS-IS"}, | |
106 | {RTPROT_RIP, "RIP"}, | |
107 | {RTPROT_RIPNG, "RIPNG"}, | |
108 | {0}}; | |
109 | ||
110 | static const struct message family_str[] = {{AF_INET, "ipv4"}, | |
111 | {AF_INET6, "ipv6"}, | |
112 | {AF_BRIDGE, "bridge"}, | |
113 | {RTNL_FAMILY_IPMR, "ipv4MR"}, | |
114 | {RTNL_FAMILY_IP6MR, "ipv6MR"}, | |
115 | {0}}; | |
116 | ||
117 | static const struct message rttype_str[] = {{RTN_UNICAST, "unicast"}, | |
118 | {RTN_MULTICAST, "multicast"}, | |
119 | {0}}; | |
b339bde7 | 120 | |
1fdc9eae | 121 | extern struct thread_master *master; |
122 | extern u_int32_t nl_rcvbufsize; | |
123 | ||
124 | extern struct zebra_privs_t zserv_privs; | |
125 | ||
d62a17ae | 126 | int netlink_talk_filter(struct sockaddr_nl *snl, struct nlmsghdr *h, |
127 | ns_id_t ns_id, int startup) | |
1fdc9eae | 128 | { |
d62a17ae | 129 | zlog_warn("netlink_talk: ignoring message type 0x%04x NS %u", |
130 | h->nlmsg_type, ns_id); | |
131 | return 0; | |
1fdc9eae | 132 | } |
133 | ||
d62a17ae | 134 | static int netlink_recvbuf(struct nlsock *nl, uint32_t newsize) |
1fdc9eae | 135 | { |
d62a17ae | 136 | u_int32_t oldsize; |
137 | socklen_t newlen = sizeof(newsize); | |
138 | socklen_t oldlen = sizeof(oldsize); | |
139 | int ret; | |
140 | ||
141 | ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen); | |
142 | if (ret < 0) { | |
143 | zlog_err("Can't get %s receive buffer size: %s", nl->name, | |
144 | safe_strerror(errno)); | |
145 | return -1; | |
146 | } | |
147 | ||
148 | /* Try force option (linux >= 2.6.14) and fall back to normal set */ | |
149 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
150 | zlog_err("routing_socket: Can't raise privileges"); | |
151 | ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE, &nl_rcvbufsize, | |
152 | sizeof(nl_rcvbufsize)); | |
153 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
154 | zlog_err("routing_socket: Can't lower privileges"); | |
155 | if (ret < 0) | |
156 | ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, | |
157 | &nl_rcvbufsize, sizeof(nl_rcvbufsize)); | |
158 | if (ret < 0) { | |
159 | zlog_err("Can't set %s receive buffer size: %s", nl->name, | |
160 | safe_strerror(errno)); | |
161 | return -1; | |
162 | } | |
163 | ||
164 | ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen); | |
165 | if (ret < 0) { | |
166 | zlog_err("Can't get %s receive buffer size: %s", nl->name, | |
167 | safe_strerror(errno)); | |
168 | return -1; | |
169 | } | |
170 | ||
171 | zlog_info("Setting netlink socket receive buffer size: %u -> %u", | |
172 | oldsize, newsize); | |
173 | return 0; | |
1fdc9eae | 174 | } |
175 | ||
176 | /* Make socket for Linux netlink interface. */ | |
d62a17ae | 177 | static int netlink_socket(struct nlsock *nl, unsigned long groups, |
178 | ns_id_t ns_id) | |
1fdc9eae | 179 | { |
d62a17ae | 180 | int ret; |
181 | struct sockaddr_nl snl; | |
182 | int sock; | |
183 | int namelen; | |
184 | int save_errno; | |
185 | ||
186 | if (zserv_privs.change(ZPRIVS_RAISE)) { | |
187 | zlog_err("Can't raise privileges"); | |
188 | return -1; | |
189 | } | |
190 | ||
191 | sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); | |
192 | if (sock < 0) { | |
193 | zlog_err("Can't open %s socket: %s", nl->name, | |
194 | safe_strerror(errno)); | |
195 | return -1; | |
196 | } | |
197 | ||
198 | memset(&snl, 0, sizeof snl); | |
199 | snl.nl_family = AF_NETLINK; | |
200 | snl.nl_groups = groups; | |
201 | ||
202 | /* Bind the socket to the netlink structure for anything. */ | |
203 | ret = bind(sock, (struct sockaddr *)&snl, sizeof snl); | |
204 | save_errno = errno; | |
205 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
206 | zlog_err("Can't lower privileges"); | |
207 | ||
208 | if (ret < 0) { | |
209 | zlog_err("Can't bind %s socket to group 0x%x: %s", nl->name, | |
210 | snl.nl_groups, safe_strerror(save_errno)); | |
211 | close(sock); | |
212 | return -1; | |
213 | } | |
214 | ||
215 | /* multiple netlink sockets will have different nl_pid */ | |
216 | namelen = sizeof snl; | |
217 | ret = getsockname(sock, (struct sockaddr *)&snl, (socklen_t *)&namelen); | |
218 | if (ret < 0 || namelen != sizeof snl) { | |
219 | zlog_err("Can't get %s socket name: %s", nl->name, | |
220 | safe_strerror(errno)); | |
221 | close(sock); | |
222 | return -1; | |
223 | } | |
224 | ||
225 | nl->snl = snl; | |
226 | nl->sock = sock; | |
227 | return ret; | |
1fdc9eae | 228 | } |
229 | ||
d62a17ae | 230 | static int netlink_information_fetch(struct sockaddr_nl *snl, |
231 | struct nlmsghdr *h, ns_id_t ns_id, | |
232 | int startup) | |
1fdc9eae | 233 | { |
d62a17ae | 234 | /* JF: Ignore messages that aren't from the kernel */ |
235 | if (snl->nl_pid != 0) { | |
236 | zlog_err("Ignoring message from pid %u", snl->nl_pid); | |
237 | return 0; | |
238 | } | |
239 | ||
240 | switch (h->nlmsg_type) { | |
241 | case RTM_NEWROUTE: | |
242 | return netlink_route_change(snl, h, ns_id, startup); | |
243 | break; | |
244 | case RTM_DELROUTE: | |
245 | return netlink_route_change(snl, h, ns_id, startup); | |
246 | break; | |
247 | case RTM_NEWLINK: | |
248 | return netlink_link_change(snl, h, ns_id, startup); | |
249 | break; | |
250 | case RTM_DELLINK: | |
251 | return netlink_link_change(snl, h, ns_id, startup); | |
252 | break; | |
253 | case RTM_NEWADDR: | |
254 | return netlink_interface_addr(snl, h, ns_id, startup); | |
255 | break; | |
256 | case RTM_DELADDR: | |
257 | return netlink_interface_addr(snl, h, ns_id, startup); | |
258 | break; | |
259 | case RTM_NEWNEIGH: | |
260 | return netlink_neigh_change(snl, h, ns_id); | |
261 | break; | |
262 | case RTM_DELNEIGH: | |
263 | return netlink_neigh_change(snl, h, ns_id); | |
264 | break; | |
265 | default: | |
109b90f5 JB |
266 | if (IS_ZEBRA_DEBUG_KERNEL) |
267 | zlog_debug("Unknown netlink nlmsg_type %d vrf %u\n", | |
268 | h->nlmsg_type, ns_id); | |
d62a17ae | 269 | break; |
270 | } | |
271 | return 0; | |
1fdc9eae | 272 | } |
273 | ||
d62a17ae | 274 | static int kernel_read(struct thread *thread) |
1fdc9eae | 275 | { |
d62a17ae | 276 | struct zebra_ns *zns = (struct zebra_ns *)THREAD_ARG(thread); |
277 | netlink_parse_info(netlink_information_fetch, &zns->netlink, zns, 5, 0); | |
278 | zns->t_netlink = NULL; | |
279 | thread_add_read(zebrad.master, kernel_read, zns, zns->netlink.sock, | |
280 | &zns->t_netlink); | |
1fdc9eae | 281 | |
d62a17ae | 282 | return 0; |
1fdc9eae | 283 | } |
284 | ||
285 | /* Filter out messages from self that occur on listener socket, | |
286 | * caused by our actions on the command socket | |
287 | */ | |
d62a17ae | 288 | static void netlink_install_filter(int sock, __u32 pid) |
1fdc9eae | 289 | { |
d62a17ae | 290 | struct sock_filter filter[] = { |
291 | /* 0: ldh [4] */ | |
292 | BPF_STMT(BPF_LD | BPF_ABS | BPF_H, | |
293 | offsetof(struct nlmsghdr, nlmsg_type)), | |
294 | /* 1: jeq 0x18 jt 5 jf next */ | |
295 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_NEWROUTE), 3, 0), | |
296 | /* 2: jeq 0x19 jt 5 jf next */ | |
297 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_DELROUTE), 2, 0), | |
298 | /* 3: jeq 0x19 jt 5 jf next */ | |
299 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_NEWNEIGH), 1, 0), | |
300 | /* 4: jeq 0x19 jt 5 jf 8 */ | |
301 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(RTM_DELNEIGH), 0, 3), | |
302 | /* 5: ldw [12] */ | |
303 | BPF_STMT(BPF_LD | BPF_ABS | BPF_W, | |
304 | offsetof(struct nlmsghdr, nlmsg_pid)), | |
305 | /* 6: jeq XX jt 7 jf 8 */ | |
306 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(pid), 0, 1), | |
307 | /* 7: ret 0 (skip) */ | |
308 | BPF_STMT(BPF_RET | BPF_K, 0), | |
309 | /* 8: ret 0xffff (keep) */ | |
310 | BPF_STMT(BPF_RET | BPF_K, 0xffff), | |
311 | }; | |
312 | ||
313 | struct sock_fprog prog = { | |
9d303b37 | 314 | .len = array_size(filter), .filter = filter, |
d62a17ae | 315 | }; |
316 | ||
317 | if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)) | |
318 | < 0) | |
319 | zlog_warn("Can't install socket filter: %s\n", | |
320 | safe_strerror(errno)); | |
1fdc9eae | 321 | } |
322 | ||
d62a17ae | 323 | void netlink_parse_rtattr(struct rtattr **tb, int max, struct rtattr *rta, |
324 | int len) | |
1fdc9eae | 325 | { |
d62a17ae | 326 | while (RTA_OK(rta, len)) { |
327 | if (rta->rta_type <= max) | |
328 | tb[rta->rta_type] = rta; | |
329 | rta = RTA_NEXT(rta, len); | |
330 | } | |
1fdc9eae | 331 | } |
332 | ||
d62a17ae | 333 | int addattr_l(struct nlmsghdr *n, unsigned int maxlen, int type, void *data, |
334 | unsigned int alen) | |
1fdc9eae | 335 | { |
d62a17ae | 336 | int len; |
337 | struct rtattr *rta; | |
1fdc9eae | 338 | |
d62a17ae | 339 | len = RTA_LENGTH(alen); |
1fdc9eae | 340 | |
d62a17ae | 341 | if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) |
342 | return -1; | |
1fdc9eae | 343 | |
d62a17ae | 344 | rta = (struct rtattr *)(((char *)n) + NLMSG_ALIGN(n->nlmsg_len)); |
345 | rta->rta_type = type; | |
346 | rta->rta_len = len; | |
4b2792b5 | 347 | |
d62a17ae | 348 | if (data) |
349 | memcpy(RTA_DATA(rta), data, alen); | |
350 | else | |
351 | assert(alen == 0); | |
4b2792b5 | 352 | |
d62a17ae | 353 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len); |
1fdc9eae | 354 | |
d62a17ae | 355 | return 0; |
1fdc9eae | 356 | } |
357 | ||
d62a17ae | 358 | int rta_addattr_l(struct rtattr *rta, unsigned int maxlen, int type, void *data, |
359 | unsigned int alen) | |
1fdc9eae | 360 | { |
d62a17ae | 361 | unsigned int len; |
362 | struct rtattr *subrta; | |
1fdc9eae | 363 | |
d62a17ae | 364 | len = RTA_LENGTH(alen); |
1fdc9eae | 365 | |
d62a17ae | 366 | if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) |
367 | return -1; | |
1fdc9eae | 368 | |
d62a17ae | 369 | subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len)); |
370 | subrta->rta_type = type; | |
371 | subrta->rta_len = len; | |
4b2792b5 | 372 | |
d62a17ae | 373 | if (data) |
374 | memcpy(RTA_DATA(subrta), data, alen); | |
375 | else | |
376 | assert(alen == 0); | |
4b2792b5 | 377 | |
d62a17ae | 378 | rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len); |
1fdc9eae | 379 | |
d62a17ae | 380 | return 0; |
1fdc9eae | 381 | } |
382 | ||
d62a17ae | 383 | int addattr16(struct nlmsghdr *n, unsigned int maxlen, int type, u_int16_t data) |
bbc16902 | 384 | { |
d62a17ae | 385 | return addattr_l(n, maxlen, type, &data, sizeof(u_int16_t)); |
bbc16902 | 386 | } |
387 | ||
d62a17ae | 388 | int addattr32(struct nlmsghdr *n, unsigned int maxlen, int type, int data) |
1fdc9eae | 389 | { |
d62a17ae | 390 | return addattr_l(n, maxlen, type, &data, sizeof(u_int32_t)); |
1fdc9eae | 391 | } |
392 | ||
d62a17ae | 393 | struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type) |
1fdc9eae | 394 | { |
d62a17ae | 395 | struct rtattr *nest = NLMSG_TAIL(n); |
1fdc9eae | 396 | |
d62a17ae | 397 | addattr_l(n, maxlen, type, NULL, 0); |
398 | return nest; | |
1fdc9eae | 399 | } |
400 | ||
d62a17ae | 401 | int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest) |
1fdc9eae | 402 | { |
d62a17ae | 403 | nest->rta_len = (u_char *)NLMSG_TAIL(n) - (u_char *)nest; |
404 | return n->nlmsg_len; | |
1fdc9eae | 405 | } |
406 | ||
d62a17ae | 407 | struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type) |
1fdc9eae | 408 | { |
d62a17ae | 409 | struct rtattr *nest = RTA_TAIL(rta); |
1fdc9eae | 410 | |
d62a17ae | 411 | rta_addattr_l(rta, maxlen, type, NULL, 0); |
412 | return nest; | |
1fdc9eae | 413 | } |
414 | ||
d62a17ae | 415 | int rta_nest_end(struct rtattr *rta, struct rtattr *nest) |
1fdc9eae | 416 | { |
d62a17ae | 417 | nest->rta_len = (u_char *)RTA_TAIL(rta) - (u_char *)nest; |
418 | return rta->rta_len; | |
1fdc9eae | 419 | } |
420 | ||
d62a17ae | 421 | const char *nl_msg_type_to_str(uint16_t msg_type) |
1fdc9eae | 422 | { |
d62a17ae | 423 | return lookup_msg(nlmsg_str, msg_type, ""); |
1fdc9eae | 424 | } |
425 | ||
d62a17ae | 426 | const char *nl_rtproto_to_str(u_char rtproto) |
1fdc9eae | 427 | { |
d62a17ae | 428 | return lookup_msg(rtproto_str, rtproto, ""); |
1fdc9eae | 429 | } |
b339bde7 | 430 | |
d62a17ae | 431 | const char *nl_family_to_str(u_char family) |
b339bde7 | 432 | { |
d62a17ae | 433 | return lookup_msg(family_str, family, ""); |
b339bde7 DS |
434 | } |
435 | ||
d62a17ae | 436 | const char *nl_rttype_to_str(u_char rttype) |
b339bde7 | 437 | { |
d62a17ae | 438 | return lookup_msg(rttype_str, rttype, ""); |
b339bde7 DS |
439 | } |
440 | ||
936ebf0a DS |
441 | /* |
442 | * netlink_parse_info | |
443 | * | |
444 | * Receive message from netlink interface and pass those information | |
445 | * to the given function. | |
446 | * | |
447 | * filter -> Function to call to read the results | |
448 | * nl -> netlink socket information | |
449 | * zns -> The zebra namespace data | |
450 | * count -> How many we should read in, 0 means as much as possible | |
451 | * startup -> Are we reading in under startup conditions? passed to | |
452 | * the filter. | |
453 | */ | |
d62a17ae | 454 | int netlink_parse_info(int (*filter)(struct sockaddr_nl *, struct nlmsghdr *, |
455 | ns_id_t, int), | |
456 | struct nlsock *nl, struct zebra_ns *zns, int count, | |
457 | int startup) | |
1fdc9eae | 458 | { |
d62a17ae | 459 | int status; |
460 | int ret = 0; | |
461 | int error; | |
462 | int read_in = 0; | |
463 | ||
464 | while (1) { | |
465 | char buf[NL_PKT_BUF_SIZE]; | |
466 | struct iovec iov = {.iov_base = buf, .iov_len = sizeof buf}; | |
467 | struct sockaddr_nl snl; | |
468 | struct msghdr msg = {.msg_name = (void *)&snl, | |
469 | .msg_namelen = sizeof snl, | |
470 | .msg_iov = &iov, | |
471 | .msg_iovlen = 1}; | |
472 | struct nlmsghdr *h; | |
473 | ||
474 | if (count && read_in >= count) | |
475 | return 0; | |
476 | ||
477 | status = recvmsg(nl->sock, &msg, 0); | |
478 | if (status < 0) { | |
479 | if (errno == EINTR) | |
480 | continue; | |
481 | if (errno == EWOULDBLOCK || errno == EAGAIN) | |
482 | break; | |
483 | zlog_err("%s recvmsg overrun: %s", nl->name, | |
484 | safe_strerror(errno)); | |
485 | /* | |
486 | * In this case we are screwed. | |
487 | * There is no good way to | |
488 | * recover zebra at this point. | |
489 | */ | |
490 | exit(-1); | |
491 | continue; | |
1fdc9eae | 492 | } |
493 | ||
d62a17ae | 494 | if (status == 0) { |
495 | zlog_err("%s EOF", nl->name); | |
496 | return -1; | |
497 | } | |
498 | ||
499 | if (msg.msg_namelen != sizeof snl) { | |
500 | zlog_err("%s sender address length error: length %d", | |
501 | nl->name, msg.msg_namelen); | |
502 | return -1; | |
503 | } | |
504 | ||
505 | if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV) { | |
506 | zlog_debug("%s: << netlink message dump [recv]", | |
507 | __func__); | |
508 | zlog_hexdump(buf, status); | |
509 | } | |
510 | ||
511 | read_in++; | |
512 | for (h = (struct nlmsghdr *)buf; | |
513 | NLMSG_OK(h, (unsigned int)status); | |
514 | h = NLMSG_NEXT(h, status)) { | |
515 | /* Finish of reading. */ | |
516 | if (h->nlmsg_type == NLMSG_DONE) | |
517 | return ret; | |
518 | ||
519 | /* Error handling. */ | |
520 | if (h->nlmsg_type == NLMSG_ERROR) { | |
521 | struct nlmsgerr *err = | |
522 | (struct nlmsgerr *)NLMSG_DATA(h); | |
523 | int errnum = err->error; | |
524 | int msg_type = err->msg.nlmsg_type; | |
525 | ||
526 | /* If the error field is zero, then this is an | |
527 | * ACK */ | |
528 | if (err->error == 0) { | |
529 | if (IS_ZEBRA_DEBUG_KERNEL) { | |
530 | zlog_debug( | |
531 | "%s: %s ACK: type=%s(%u), seq=%u, pid=%u", | |
532 | __FUNCTION__, nl->name, | |
533 | nl_msg_type_to_str( | |
534 | err->msg.nlmsg_type), | |
535 | err->msg.nlmsg_type, | |
536 | err->msg.nlmsg_seq, | |
537 | err->msg.nlmsg_pid); | |
538 | } | |
539 | ||
540 | /* return if not a multipart message, | |
541 | * otherwise continue */ | |
542 | if (!(h->nlmsg_flags & NLM_F_MULTI)) | |
543 | return 0; | |
544 | continue; | |
545 | } | |
546 | ||
547 | if (h->nlmsg_len | |
548 | < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
549 | zlog_err("%s error: message truncated", | |
550 | nl->name); | |
551 | return -1; | |
552 | } | |
553 | ||
554 | /* Deal with errors that occur because of races | |
555 | * in link handling */ | |
556 | if (nl == &zns->netlink_cmd | |
557 | && ((msg_type == RTM_DELROUTE | |
558 | && (-errnum == ENODEV | |
559 | || -errnum == ESRCH)) | |
560 | || (msg_type == RTM_NEWROUTE | |
561 | && (-errnum == ENETDOWN | |
562 | || -errnum == EEXIST)))) { | |
563 | if (IS_ZEBRA_DEBUG_KERNEL) | |
564 | zlog_debug( | |
565 | "%s: error: %s type=%s(%u), seq=%u, pid=%u", | |
566 | nl->name, | |
567 | safe_strerror(-errnum), | |
568 | nl_msg_type_to_str( | |
569 | msg_type), | |
570 | msg_type, | |
571 | err->msg.nlmsg_seq, | |
572 | err->msg.nlmsg_pid); | |
573 | return 0; | |
574 | } | |
575 | ||
576 | /* We see RTM_DELNEIGH when shutting down an | |
577 | * interface with an IPv4 | |
578 | * link-local. The kernel should have already | |
579 | * deleted the neighbor | |
580 | * so do not log these as an error. | |
581 | */ | |
582 | if (msg_type == RTM_DELNEIGH | |
583 | || (nl == &zns->netlink_cmd | |
584 | && msg_type == RTM_NEWROUTE | |
585 | && (-errnum == ESRCH | |
586 | || -errnum == ENETUNREACH))) { | |
587 | /* This is known to happen in some | |
588 | * situations, don't log | |
589 | * as error. | |
590 | */ | |
591 | if (IS_ZEBRA_DEBUG_KERNEL) | |
592 | zlog_debug( | |
593 | "%s error: %s, type=%s(%u), seq=%u, pid=%u", | |
594 | nl->name, | |
595 | safe_strerror(-errnum), | |
596 | nl_msg_type_to_str( | |
597 | msg_type), | |
598 | msg_type, | |
599 | err->msg.nlmsg_seq, | |
600 | err->msg.nlmsg_pid); | |
601 | } else | |
602 | zlog_err( | |
603 | "%s error: %s, type=%s(%u), seq=%u, pid=%u", | |
604 | nl->name, | |
605 | safe_strerror(-errnum), | |
606 | nl_msg_type_to_str(msg_type), | |
607 | msg_type, err->msg.nlmsg_seq, | |
608 | err->msg.nlmsg_pid); | |
609 | ||
610 | return -1; | |
611 | } | |
612 | ||
613 | /* OK we got netlink message. */ | |
614 | if (IS_ZEBRA_DEBUG_KERNEL) | |
615 | zlog_debug( | |
616 | "netlink_parse_info: %s type %s(%u), len=%d, seq=%u, pid=%u", | |
617 | nl->name, | |
618 | nl_msg_type_to_str(h->nlmsg_type), | |
619 | h->nlmsg_type, h->nlmsg_len, | |
620 | h->nlmsg_seq, h->nlmsg_pid); | |
621 | ||
622 | /* skip unsolicited messages originating from command | |
623 | * socket | |
624 | * linux sets the originators port-id for {NEW|DEL}ADDR | |
625 | * messages, | |
626 | * so this has to be checked here. */ | |
627 | if (nl != &zns->netlink_cmd | |
628 | && h->nlmsg_pid == zns->netlink_cmd.snl.nl_pid | |
629 | && (h->nlmsg_type != RTM_NEWADDR | |
630 | && h->nlmsg_type != RTM_DELADDR)) { | |
631 | if (IS_ZEBRA_DEBUG_KERNEL) | |
632 | zlog_debug( | |
633 | "netlink_parse_info: %s packet comes from %s", | |
634 | zns->netlink_cmd.name, | |
635 | nl->name); | |
636 | continue; | |
637 | } | |
638 | ||
639 | error = (*filter)(&snl, h, zns->ns_id, startup); | |
640 | if (error < 0) { | |
641 | zlog_err("%s filter function error", nl->name); | |
642 | ret = error; | |
643 | } | |
644 | } | |
645 | ||
646 | /* After error care. */ | |
647 | if (msg.msg_flags & MSG_TRUNC) { | |
648 | zlog_err("%s error: message truncated", nl->name); | |
649 | continue; | |
650 | } | |
651 | if (status) { | |
652 | zlog_err("%s error: data remnant size %d", nl->name, | |
653 | status); | |
654 | return -1; | |
655 | } | |
656 | } | |
657 | return ret; | |
1fdc9eae | 658 | } |
659 | ||
936ebf0a DS |
660 | /* |
661 | * netlink_talk | |
662 | * | |
663 | * sendmsg() to netlink socket then recvmsg(). | |
664 | * Calls netlink_parse_info to parse returned data | |
665 | * | |
666 | * filter -> The filter to read final results from kernel | |
667 | * nlmsghdr -> The data to send to the kernel | |
668 | * nl -> The netlink socket information | |
669 | * zns -> The zebra namespace information | |
670 | * startup -> Are we reading in under startup conditions | |
671 | * This is passed through eventually to filter. | |
672 | */ | |
d62a17ae | 673 | int netlink_talk(int (*filter)(struct sockaddr_nl *, struct nlmsghdr *, ns_id_t, |
674 | int startup), | |
675 | struct nlmsghdr *n, struct nlsock *nl, struct zebra_ns *zns, | |
676 | int startup) | |
1fdc9eae | 677 | { |
d62a17ae | 678 | int status; |
679 | struct sockaddr_nl snl; | |
cbaca6a1 DS |
680 | struct iovec iov; |
681 | struct msghdr msg; | |
d62a17ae | 682 | int save_errno; |
683 | ||
684 | memset(&snl, 0, sizeof snl); | |
cbaca6a1 DS |
685 | memset(&iov, 0, sizeof iov); |
686 | memset(&msg, 0, sizeof msg); | |
687 | ||
688 | iov.iov_base = n; | |
689 | iov.iov_len = n->nlmsg_len; | |
690 | msg.msg_name = (void *)&snl; | |
691 | msg.msg_namelen = sizeof snl; | |
692 | msg.msg_iov = &iov; | |
693 | msg.msg_iovlen = 1; | |
694 | ||
d62a17ae | 695 | snl.nl_family = AF_NETLINK; |
696 | ||
697 | n->nlmsg_seq = ++nl->seq; | |
698 | n->nlmsg_pid = nl->snl.nl_pid; | |
699 | ||
700 | /* Request an acknowledgement by setting NLM_F_ACK */ | |
701 | n->nlmsg_flags |= NLM_F_ACK; | |
702 | ||
703 | if (IS_ZEBRA_DEBUG_KERNEL) | |
704 | zlog_debug( | |
705 | "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x", | |
706 | nl->name, nl_msg_type_to_str(n->nlmsg_type), | |
707 | n->nlmsg_type, n->nlmsg_len, n->nlmsg_seq, | |
708 | n->nlmsg_flags); | |
709 | ||
710 | /* Send message to netlink interface. */ | |
711 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
712 | zlog_err("Can't raise privileges"); | |
713 | status = sendmsg(nl->sock, &msg, 0); | |
714 | save_errno = errno; | |
715 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
716 | zlog_err("Can't lower privileges"); | |
717 | ||
718 | if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND) { | |
719 | zlog_debug("%s: >> netlink message dump [sent]", __func__); | |
720 | zlog_hexdump(n, n->nlmsg_len); | |
721 | } | |
722 | ||
723 | if (status < 0) { | |
724 | zlog_err("netlink_talk sendmsg() error: %s", | |
725 | safe_strerror(save_errno)); | |
726 | return -1; | |
727 | } | |
728 | ||
729 | ||
730 | /* | |
731 | * Get reply from netlink socket. | |
732 | * The reply should either be an acknowlegement or an error. | |
733 | */ | |
734 | return netlink_parse_info(filter, nl, zns, 0, startup); | |
1fdc9eae | 735 | } |
736 | ||
289602d7 | 737 | /* Issue request message to kernel via netlink socket. GET messages |
738 | * are issued through this interface. | |
739 | */ | |
d62a17ae | 740 | int netlink_request(struct nlsock *nl, struct nlmsghdr *n) |
1fdc9eae | 741 | { |
d62a17ae | 742 | int ret; |
743 | struct sockaddr_nl snl; | |
744 | int save_errno; | |
745 | ||
746 | /* Check netlink socket. */ | |
747 | if (nl->sock < 0) { | |
748 | zlog_err("%s socket isn't active.", nl->name); | |
749 | return -1; | |
750 | } | |
751 | ||
752 | /* Fill common fields for all requests. */ | |
753 | n->nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; | |
754 | n->nlmsg_pid = nl->snl.nl_pid; | |
755 | n->nlmsg_seq = ++nl->seq; | |
756 | ||
757 | memset(&snl, 0, sizeof snl); | |
758 | snl.nl_family = AF_NETLINK; | |
759 | ||
760 | /* Raise capabilities and send message, then lower capabilities. */ | |
761 | if (zserv_privs.change(ZPRIVS_RAISE)) { | |
762 | zlog_err("Can't raise privileges"); | |
763 | return -1; | |
764 | } | |
765 | ||
766 | ret = sendto(nl->sock, (void *)n, n->nlmsg_len, 0, | |
767 | (struct sockaddr *)&snl, sizeof snl); | |
768 | save_errno = errno; | |
769 | ||
770 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
771 | zlog_err("Can't lower privileges"); | |
772 | ||
773 | if (ret < 0) { | |
774 | zlog_err("%s sendto failed: %s", nl->name, | |
775 | safe_strerror(save_errno)); | |
776 | return -1; | |
777 | } | |
778 | ||
779 | return 0; | |
1fdc9eae | 780 | } |
781 | ||
782 | /* Exported interface function. This function simply calls | |
783 | netlink_socket (). */ | |
d62a17ae | 784 | void kernel_init(struct zebra_ns *zns) |
1fdc9eae | 785 | { |
d62a17ae | 786 | unsigned long groups; |
787 | ||
788 | /* Initialize netlink sockets */ | |
789 | groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_IFADDR | |
790 | | RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_MROUTE | |
791 | | RTMGRP_NEIGH; | |
792 | ||
793 | snprintf(zns->netlink.name, sizeof(zns->netlink.name), | |
794 | "netlink-listen (NS %u)", zns->ns_id); | |
795 | zns->netlink.sock = -1; | |
796 | netlink_socket(&zns->netlink, groups, zns->ns_id); | |
797 | ||
798 | snprintf(zns->netlink_cmd.name, sizeof(zns->netlink_cmd.name), | |
799 | "netlink-cmd (NS %u)", zns->ns_id); | |
800 | zns->netlink_cmd.sock = -1; | |
801 | netlink_socket(&zns->netlink_cmd, 0, zns->ns_id); | |
802 | ||
803 | /* Register kernel socket. */ | |
804 | if (zns->netlink.sock > 0) { | |
805 | /* Only want non-blocking on the netlink event socket */ | |
806 | if (fcntl(zns->netlink.sock, F_SETFL, O_NONBLOCK) < 0) | |
807 | zlog_err("Can't set %s socket flags: %s", | |
808 | zns->netlink.name, safe_strerror(errno)); | |
809 | ||
810 | /* Set receive buffer size if it's set from command line */ | |
811 | if (nl_rcvbufsize) | |
812 | netlink_recvbuf(&zns->netlink, nl_rcvbufsize); | |
813 | ||
814 | netlink_install_filter(zns->netlink.sock, | |
815 | zns->netlink_cmd.snl.nl_pid); | |
816 | zns->t_netlink = NULL; | |
817 | thread_add_read(zebrad.master, kernel_read, zns, | |
818 | zns->netlink.sock, &zns->t_netlink); | |
819 | } | |
820 | ||
821 | rt_netlink_init(); | |
1fdc9eae | 822 | } |
823 | ||
d62a17ae | 824 | void kernel_terminate(struct zebra_ns *zns) |
1fdc9eae | 825 | { |
d62a17ae | 826 | THREAD_READ_OFF(zns->t_netlink); |
827 | ||
828 | if (zns->netlink.sock >= 0) { | |
829 | close(zns->netlink.sock); | |
830 | zns->netlink.sock = -1; | |
831 | } | |
832 | ||
833 | if (zns->netlink_cmd.sock >= 0) { | |
834 | close(zns->netlink_cmd.sock); | |
835 | zns->netlink_cmd.sock = -1; | |
836 | } | |
1fdc9eae | 837 | } |
ddfeb486 DL |
838 | |
839 | #endif /* HAVE_NETLINK */ |