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