]>
Commit | Line | Data |
---|---|---|
bf094f69 | 1 | /* |
d8647095 | 2 | * Zebra API message creation & consumption. |
bf094f69 QY |
3 | * Portions: |
4 | * Copyright (C) 1997-1999 Kunihiro Ishiguro | |
5 | * Copyright (C) 2015-2018 Cumulus Networks, Inc. | |
6 | * et al. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the Free | |
10 | * Software Foundation; either version 2 of the License, or (at your option) | |
11 | * any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
16 | * more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; see the file COPYING; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
23 | #include <zebra.h> | |
24 | #include <libgen.h> | |
25 | ||
26 | #include "lib/prefix.h" | |
27 | #include "lib/command.h" | |
28 | #include "lib/if.h" | |
29 | #include "lib/thread.h" | |
30 | #include "lib/stream.h" | |
31 | #include "lib/memory.h" | |
32 | #include "lib/table.h" | |
33 | #include "lib/network.h" | |
34 | #include "lib/sockunion.h" | |
35 | #include "lib/log.h" | |
36 | #include "lib/zclient.h" | |
37 | #include "lib/privs.h" | |
38 | #include "lib/network.h" | |
39 | #include "lib/buffer.h" | |
40 | #include "lib/nexthop.h" | |
41 | #include "lib/vrf.h" | |
42 | #include "lib/libfrr.h" | |
43 | #include "lib/sockopt.h" | |
5e7939a5 | 44 | #include "lib/lib_errors.h" |
bf094f69 | 45 | |
161e9ab7 | 46 | #include "zebra/zebra_router.h" |
bf094f69 QY |
47 | #include "zebra/rib.h" |
48 | #include "zebra/zebra_memory.h" | |
49 | #include "zebra/zebra_ns.h" | |
50 | #include "zebra/zebra_vrf.h" | |
51 | #include "zebra/router-id.h" | |
52 | #include "zebra/redistribute.h" | |
53 | #include "zebra/debug.h" | |
54 | #include "zebra/zebra_rnh.h" | |
55 | #include "zebra/rt_netlink.h" | |
56 | #include "zebra/interface.h" | |
57 | #include "zebra/zebra_ptm.h" | |
58 | #include "zebra/rtadv.h" | |
59 | #include "zebra/zebra_mpls.h" | |
60 | #include "zebra/zebra_mroute.h" | |
bf094f69 QY |
61 | #include "zebra/zebra_vxlan.h" |
62 | #include "zebra/rt.h" | |
63 | #include "zebra/zebra_pbr.h" | |
64 | #include "zebra/table_manager.h" | |
65 | #include "zebra/zapi_msg.h" | |
364fed6b | 66 | #include "zebra/zebra_errors.h" |
02c0866d | 67 | #include "zebra/zebra_mlag.h" |
54bea4e5 | 68 | #include "zebra/connected.h" |
bf094f69 QY |
69 | |
70 | /* Encoding helpers -------------------------------------------------------- */ | |
71 | ||
72 | static void zserv_encode_interface(struct stream *s, struct interface *ifp) | |
73 | { | |
74 | /* Interface information. */ | |
53e60e5c | 75 | struct zebra_if *zif = ifp->info; |
19c38250 | 76 | |
bf094f69 QY |
77 | stream_put(s, ifp->name, INTERFACE_NAMSIZ); |
78 | stream_putl(s, ifp->ifindex); | |
79 | stream_putc(s, ifp->status); | |
80 | stream_putq(s, ifp->flags); | |
81 | stream_putc(s, ifp->ptm_enable); | |
82 | stream_putc(s, ifp->ptm_status); | |
83 | stream_putl(s, ifp->metric); | |
84 | stream_putl(s, ifp->speed); | |
85 | stream_putl(s, ifp->mtu); | |
86 | stream_putl(s, ifp->mtu6); | |
87 | stream_putl(s, ifp->bandwidth); | |
53e60e5c | 88 | stream_putl(s, zif->link_ifindex); |
bf094f69 QY |
89 | stream_putl(s, ifp->ll_type); |
90 | stream_putl(s, ifp->hw_addr_len); | |
91 | if (ifp->hw_addr_len) | |
92 | stream_put(s, ifp->hw_addr, ifp->hw_addr_len); | |
93 | ||
94 | /* Then, Traffic Engineering parameters if any */ | |
95 | if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params)) { | |
96 | stream_putc(s, 1); | |
97 | zebra_interface_link_params_write(s, ifp); | |
98 | } else | |
99 | stream_putc(s, 0); | |
100 | ||
101 | /* Write packet size. */ | |
102 | stream_putw_at(s, 0, stream_get_endp(s)); | |
103 | } | |
104 | ||
105 | static void zserv_encode_vrf(struct stream *s, struct zebra_vrf *zvrf) | |
106 | { | |
107 | struct vrf_data data; | |
108 | const char *netns_name = zvrf_ns_name(zvrf); | |
109 | ||
110 | data.l.table_id = zvrf->table_id; | |
111 | ||
112 | if (netns_name) | |
113 | strlcpy(data.l.netns_name, basename((char *)netns_name), | |
114 | NS_NAMSIZ); | |
115 | else | |
116 | memset(data.l.netns_name, 0, NS_NAMSIZ); | |
117 | /* Pass the tableid and the netns NAME */ | |
118 | stream_put(s, &data, sizeof(struct vrf_data)); | |
119 | /* Interface information. */ | |
120 | stream_put(s, zvrf_name(zvrf), VRF_NAMSIZ); | |
121 | /* Write packet size. */ | |
122 | stream_putw_at(s, 0, stream_get_endp(s)); | |
123 | } | |
124 | ||
125 | static int zserv_encode_nexthop(struct stream *s, struct nexthop *nexthop) | |
126 | { | |
a756969d | 127 | stream_putl(s, nexthop->vrf_id); |
bf094f69 QY |
128 | stream_putc(s, nexthop->type); |
129 | switch (nexthop->type) { | |
130 | case NEXTHOP_TYPE_IPV4: | |
131 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
132 | stream_put_in_addr(s, &nexthop->gate.ipv4); | |
133 | stream_putl(s, nexthop->ifindex); | |
134 | break; | |
135 | case NEXTHOP_TYPE_IPV6: | |
136 | stream_put(s, &nexthop->gate.ipv6, 16); | |
137 | break; | |
138 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
139 | stream_put(s, &nexthop->gate.ipv6, 16); | |
140 | stream_putl(s, nexthop->ifindex); | |
141 | break; | |
142 | case NEXTHOP_TYPE_IFINDEX: | |
143 | stream_putl(s, nexthop->ifindex); | |
144 | break; | |
145 | default: | |
146 | /* do nothing */ | |
147 | break; | |
148 | } | |
149 | return 1; | |
150 | } | |
151 | ||
9ab0b2a3 SW |
152 | /* |
153 | * Zebra error addition adds error type. | |
154 | * | |
155 | * | |
156 | * 0 1 | |
157 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 | |
158 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
159 | * | enum zebra_error_types | | |
160 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
161 | * | |
162 | */ | |
163 | static void zserv_encode_error(struct stream *s, enum zebra_error_types error) | |
164 | { | |
165 | stream_put(s, &error, sizeof(error)); | |
166 | ||
167 | /* Write packet size. */ | |
168 | stream_putw_at(s, 0, stream_get_endp(s)); | |
169 | } | |
170 | ||
bf094f69 QY |
171 | /* Send handlers ----------------------------------------------------------- */ |
172 | ||
173 | /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */ | |
174 | /* | |
175 | * This function is called in the following situations: | |
176 | * - in response to a 3-byte ZEBRA_INTERFACE_ADD request | |
177 | * from the client. | |
178 | * - at startup, when zebra figures out the available interfaces | |
179 | * - when an interface is added (where support for | |
180 | * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when | |
181 | * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is | |
182 | * received) | |
183 | */ | |
184 | int zsend_interface_add(struct zserv *client, struct interface *ifp) | |
185 | { | |
186 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
187 | ||
a36898e7 | 188 | zclient_create_header(s, ZEBRA_INTERFACE_ADD, ifp->vrf_id); |
bf094f69 QY |
189 | zserv_encode_interface(s, ifp); |
190 | ||
191 | client->ifadd_cnt++; | |
21ccc0cf | 192 | return zserv_send_message(client, s); |
bf094f69 QY |
193 | } |
194 | ||
195 | /* Interface deletion from zebra daemon. */ | |
196 | int zsend_interface_delete(struct zserv *client, struct interface *ifp) | |
197 | { | |
198 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
199 | ||
a36898e7 | 200 | zclient_create_header(s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id); |
bf094f69 QY |
201 | zserv_encode_interface(s, ifp); |
202 | ||
203 | client->ifdel_cnt++; | |
21ccc0cf | 204 | return zserv_send_message(client, s); |
bf094f69 QY |
205 | } |
206 | ||
207 | int zsend_vrf_add(struct zserv *client, struct zebra_vrf *zvrf) | |
208 | { | |
209 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
210 | ||
211 | zclient_create_header(s, ZEBRA_VRF_ADD, zvrf_id(zvrf)); | |
212 | zserv_encode_vrf(s, zvrf); | |
213 | ||
214 | client->vrfadd_cnt++; | |
21ccc0cf | 215 | return zserv_send_message(client, s); |
bf094f69 QY |
216 | } |
217 | ||
218 | /* VRF deletion from zebra daemon. */ | |
219 | int zsend_vrf_delete(struct zserv *client, struct zebra_vrf *zvrf) | |
220 | ||
221 | { | |
222 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
223 | ||
224 | zclient_create_header(s, ZEBRA_VRF_DELETE, zvrf_id(zvrf)); | |
225 | zserv_encode_vrf(s, zvrf); | |
226 | ||
227 | client->vrfdel_cnt++; | |
21ccc0cf | 228 | return zserv_send_message(client, s); |
bf094f69 QY |
229 | } |
230 | ||
231 | int zsend_interface_link_params(struct zserv *client, struct interface *ifp) | |
232 | { | |
233 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
234 | ||
bf094f69 QY |
235 | if (!ifp->link_params) { |
236 | stream_free(s); | |
237 | return 0; | |
238 | } | |
239 | ||
a36898e7 | 240 | zclient_create_header(s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id); |
bf094f69 QY |
241 | |
242 | /* Add Interface Index */ | |
243 | stream_putl(s, ifp->ifindex); | |
244 | ||
245 | /* Then TE Link Parameters */ | |
246 | if (zebra_interface_link_params_write(s, ifp) == 0) { | |
247 | stream_free(s); | |
248 | return 0; | |
249 | } | |
250 | ||
251 | /* Write packet size. */ | |
252 | stream_putw_at(s, 0, stream_get_endp(s)); | |
253 | ||
21ccc0cf | 254 | return zserv_send_message(client, s); |
bf094f69 QY |
255 | } |
256 | ||
257 | /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or | |
258 | * ZEBRA_INTERFACE_ADDRESS_DELETE to the client. | |
259 | * | |
260 | * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations: | |
261 | * - in response to a 3-byte ZEBRA_INTERFACE_ADD request | |
262 | * from the client, after the ZEBRA_INTERFACE_ADD has been | |
263 | * sent from zebra to the client | |
264 | * - redistribute new address info to all clients in the following situations | |
265 | * - at startup, when zebra figures out the available interfaces | |
266 | * - when an interface is added (where support for | |
267 | * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when | |
268 | * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is | |
269 | * received) | |
b1bd1015 | 270 | * - for the vty commands "ip address A.B.C.D/M [<label LINE>]" |
bf094f69 QY |
271 | * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M" |
272 | * - when an RTM_NEWADDR message is received from the kernel, | |
273 | * | |
274 | * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE: | |
275 | * | |
276 | * zsend_interface_address(DELETE) | |
277 | * ^ | |
278 | * | | |
279 | * zebra_interface_address_delete_update | |
280 | * ^ ^ ^ | |
281 | * | | if_delete_update | |
282 | * | | | |
283 | * ip_address_uninstall connected_delete_ipv4 | |
284 | * [ipv6_addresss_uninstall] [connected_delete_ipv6] | |
285 | * ^ ^ | |
286 | * | | | |
287 | * | RTM_NEWADDR on routing/netlink socket | |
288 | * | | |
289 | * vty commands: | |
290 | * "no ip address A.B.C.D/M [label LINE]" | |
b1bd1015 | 291 | * "no ip address A.B.C.D/M" |
bf094f69 QY |
292 | * ["no ipv6 address X:X::X:X/M"] |
293 | * | |
294 | */ | |
295 | int zsend_interface_address(int cmd, struct zserv *client, | |
296 | struct interface *ifp, struct connected *ifc) | |
297 | { | |
298 | int blen; | |
299 | struct prefix *p; | |
300 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
301 | ||
a36898e7 | 302 | zclient_create_header(s, cmd, ifp->vrf_id); |
bf094f69 QY |
303 | stream_putl(s, ifp->ifindex); |
304 | ||
305 | /* Interface address flag. */ | |
306 | stream_putc(s, ifc->flags); | |
307 | ||
308 | /* Prefix information. */ | |
309 | p = ifc->address; | |
310 | stream_putc(s, p->family); | |
311 | blen = prefix_blen(p); | |
312 | stream_put(s, &p->u.prefix, blen); | |
313 | ||
314 | /* | |
315 | * XXX gnu version does not send prefixlen for | |
316 | * ZEBRA_INTERFACE_ADDRESS_DELETE | |
317 | * but zebra_interface_address_delete_read() in the gnu version | |
318 | * expects to find it | |
319 | */ | |
320 | stream_putc(s, p->prefixlen); | |
321 | ||
322 | /* Destination. */ | |
323 | p = ifc->destination; | |
324 | if (p) | |
325 | stream_put(s, &p->u.prefix, blen); | |
326 | else | |
327 | stream_put(s, NULL, blen); | |
328 | ||
329 | /* Write packet size. */ | |
330 | stream_putw_at(s, 0, stream_get_endp(s)); | |
331 | ||
332 | client->connected_rt_add_cnt++; | |
21ccc0cf | 333 | return zserv_send_message(client, s); |
bf094f69 QY |
334 | } |
335 | ||
336 | static int zsend_interface_nbr_address(int cmd, struct zserv *client, | |
337 | struct interface *ifp, | |
338 | struct nbr_connected *ifc) | |
339 | { | |
340 | int blen; | |
341 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
342 | struct prefix *p; | |
343 | ||
a36898e7 | 344 | zclient_create_header(s, cmd, ifp->vrf_id); |
bf094f69 QY |
345 | stream_putl(s, ifp->ifindex); |
346 | ||
347 | /* Prefix information. */ | |
348 | p = ifc->address; | |
349 | stream_putc(s, p->family); | |
350 | blen = prefix_blen(p); | |
351 | stream_put(s, &p->u.prefix, blen); | |
352 | ||
353 | /* | |
354 | * XXX gnu version does not send prefixlen for | |
355 | * ZEBRA_INTERFACE_ADDRESS_DELETE | |
356 | * but zebra_interface_address_delete_read() in the gnu version | |
357 | * expects to find it | |
358 | */ | |
359 | stream_putc(s, p->prefixlen); | |
360 | ||
361 | /* Write packet size. */ | |
362 | stream_putw_at(s, 0, stream_get_endp(s)); | |
363 | ||
21ccc0cf | 364 | return zserv_send_message(client, s); |
bf094f69 QY |
365 | } |
366 | ||
367 | /* Interface address addition. */ | |
368 | static void zebra_interface_nbr_address_add_update(struct interface *ifp, | |
369 | struct nbr_connected *ifc) | |
370 | { | |
371 | struct listnode *node, *nnode; | |
372 | struct zserv *client; | |
373 | struct prefix *p; | |
374 | ||
375 | if (IS_ZEBRA_DEBUG_EVENT) { | |
376 | char buf[INET6_ADDRSTRLEN]; | |
377 | ||
378 | p = ifc->address; | |
379 | zlog_debug( | |
380 | "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s", | |
381 | inet_ntop(p->family, &p->u.prefix, buf, | |
382 | INET6_ADDRSTRLEN), | |
383 | p->prefixlen, ifc->ifp->name); | |
384 | } | |
385 | ||
161e9ab7 | 386 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) |
bf094f69 QY |
387 | zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD, |
388 | client, ifp, ifc); | |
389 | } | |
390 | ||
391 | /* Interface address deletion. */ | |
392 | static void zebra_interface_nbr_address_delete_update(struct interface *ifp, | |
393 | struct nbr_connected *ifc) | |
394 | { | |
395 | struct listnode *node, *nnode; | |
396 | struct zserv *client; | |
397 | struct prefix *p; | |
398 | ||
399 | if (IS_ZEBRA_DEBUG_EVENT) { | |
400 | char buf[INET6_ADDRSTRLEN]; | |
401 | ||
402 | p = ifc->address; | |
403 | zlog_debug( | |
404 | "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s", | |
405 | inet_ntop(p->family, &p->u.prefix, buf, | |
406 | INET6_ADDRSTRLEN), | |
407 | p->prefixlen, ifc->ifp->name); | |
408 | } | |
409 | ||
161e9ab7 | 410 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) |
bf094f69 QY |
411 | zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE, |
412 | client, ifp, ifc); | |
413 | } | |
414 | ||
415 | /* Send addresses on interface to client */ | |
416 | int zsend_interface_addresses(struct zserv *client, struct interface *ifp) | |
417 | { | |
418 | struct listnode *cnode, *cnnode; | |
419 | struct connected *c; | |
420 | struct nbr_connected *nc; | |
421 | ||
422 | /* Send interface addresses. */ | |
423 | for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) { | |
424 | if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL)) | |
425 | continue; | |
426 | ||
427 | if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD, client, | |
428 | ifp, c) | |
429 | < 0) | |
430 | return -1; | |
431 | } | |
432 | ||
433 | /* Send interface neighbors. */ | |
434 | for (ALL_LIST_ELEMENTS(ifp->nbr_connected, cnode, cnnode, nc)) { | |
435 | if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD, | |
436 | client, ifp, nc) | |
437 | < 0) | |
438 | return -1; | |
439 | } | |
440 | ||
441 | return 0; | |
442 | } | |
443 | ||
444 | /* Notify client about interface moving from one VRF to another. | |
445 | * Whether client is interested in old and new VRF is checked by caller. | |
446 | */ | |
447 | int zsend_interface_vrf_update(struct zserv *client, struct interface *ifp, | |
448 | vrf_id_t vrf_id) | |
449 | { | |
450 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
451 | ||
a36898e7 | 452 | zclient_create_header(s, ZEBRA_INTERFACE_VRF_UPDATE, ifp->vrf_id); |
bf094f69 | 453 | |
91d227b7 RW |
454 | /* Fill in the name of the interface and its new VRF (id) */ |
455 | stream_put(s, ifp->name, INTERFACE_NAMSIZ); | |
bf094f69 QY |
456 | stream_putl(s, vrf_id); |
457 | ||
458 | /* Write packet size. */ | |
459 | stream_putw_at(s, 0, stream_get_endp(s)); | |
460 | ||
461 | client->if_vrfchg_cnt++; | |
21ccc0cf | 462 | return zserv_send_message(client, s); |
bf094f69 QY |
463 | } |
464 | ||
465 | /* Add new nbr connected IPv6 address */ | |
466 | void nbr_connected_add_ipv6(struct interface *ifp, struct in6_addr *address) | |
467 | { | |
468 | struct nbr_connected *ifc; | |
469 | struct prefix p; | |
470 | ||
471 | p.family = AF_INET6; | |
a85297a7 | 472 | IPV6_ADDR_COPY(&p.u.prefix6, address); |
bf094f69 QY |
473 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
474 | ||
8b1766b1 QY |
475 | ifc = listnode_head(ifp->nbr_connected); |
476 | if (!ifc) { | |
bf094f69 QY |
477 | /* new addition */ |
478 | ifc = nbr_connected_new(); | |
479 | ifc->address = prefix_new(); | |
480 | ifc->ifp = ifp; | |
481 | listnode_add(ifp->nbr_connected, ifc); | |
482 | } | |
483 | ||
484 | prefix_copy(ifc->address, &p); | |
485 | ||
486 | zebra_interface_nbr_address_add_update(ifp, ifc); | |
487 | ||
488 | if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 1); | |
489 | } | |
490 | ||
491 | void nbr_connected_delete_ipv6(struct interface *ifp, struct in6_addr *address) | |
492 | { | |
493 | struct nbr_connected *ifc; | |
494 | struct prefix p; | |
495 | ||
496 | p.family = AF_INET6; | |
a85297a7 | 497 | IPV6_ADDR_COPY(&p.u.prefix6, address); |
bf094f69 QY |
498 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
499 | ||
500 | ifc = nbr_connected_check(ifp, &p); | |
501 | if (!ifc) | |
502 | return; | |
503 | ||
504 | listnode_delete(ifp->nbr_connected, ifc); | |
505 | ||
506 | zebra_interface_nbr_address_delete_update(ifp, ifc); | |
507 | ||
508 | if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 0); | |
509 | ||
510 | nbr_connected_free(ifc); | |
511 | } | |
512 | ||
513 | /* | |
514 | * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or | |
515 | * ZEBRA_INTERFACE_DOWN. | |
516 | * | |
517 | * The ZEBRA_INTERFACE_UP message is sent from the zebra server to | |
518 | * the clients in one of 2 situations: | |
519 | * - an if_up is detected e.g., as a result of an RTM_IFINFO message | |
520 | * - a vty command modifying the bandwidth of an interface is received. | |
521 | * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected. | |
522 | */ | |
523 | int zsend_interface_update(int cmd, struct zserv *client, struct interface *ifp) | |
524 | { | |
525 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
526 | ||
a36898e7 | 527 | zclient_create_header(s, cmd, ifp->vrf_id); |
bf094f69 QY |
528 | zserv_encode_interface(s, ifp); |
529 | ||
530 | if (cmd == ZEBRA_INTERFACE_UP) | |
531 | client->ifup_cnt++; | |
532 | else | |
533 | client->ifdown_cnt++; | |
534 | ||
21ccc0cf | 535 | return zserv_send_message(client, s); |
bf094f69 QY |
536 | } |
537 | ||
86391e56 MS |
538 | int zsend_redistribute_route(int cmd, struct zserv *client, |
539 | const struct prefix *p, | |
40f321c0 MS |
540 | const struct prefix *src_p, |
541 | const struct route_entry *re) | |
bf094f69 QY |
542 | { |
543 | struct zapi_route api; | |
544 | struct zapi_nexthop *api_nh; | |
545 | struct nexthop *nexthop; | |
9a0d4dd3 | 546 | uint8_t count = 0; |
34fa0870 | 547 | afi_t afi; |
f3f45626 DS |
548 | size_t stream_size = |
549 | MAX(ZEBRA_MAX_PACKET_SIZ, sizeof(struct zapi_route)); | |
bf094f69 QY |
550 | |
551 | memset(&api, 0, sizeof(api)); | |
552 | api.vrf_id = re->vrf_id; | |
553 | api.type = re->type; | |
e4081c0e | 554 | api.safi = SAFI_UNICAST; |
bf094f69 QY |
555 | api.instance = re->instance; |
556 | api.flags = re->flags; | |
557 | ||
34fa0870 DS |
558 | afi = family2afi(p->family); |
559 | switch (afi) { | |
560 | case AFI_IP: | |
561 | if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) | |
562 | client->redist_v4_add_cnt++; | |
563 | else | |
564 | client->redist_v4_del_cnt++; | |
565 | break; | |
566 | case AFI_IP6: | |
567 | if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) | |
568 | client->redist_v6_add_cnt++; | |
569 | else | |
570 | client->redist_v6_del_cnt++; | |
571 | break; | |
572 | default: | |
573 | break; | |
574 | } | |
575 | ||
bf094f69 QY |
576 | /* Prefix. */ |
577 | api.prefix = *p; | |
578 | if (src_p) { | |
579 | SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX); | |
580 | memcpy(&api.src_prefix, src_p, sizeof(api.src_prefix)); | |
581 | } | |
582 | ||
0eb97b86 MS |
583 | for (nexthop = re->nhe->nhg->nexthop; |
584 | nexthop; nexthop = nexthop->next) { | |
bf094f69 QY |
585 | if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
586 | continue; | |
587 | ||
588 | api_nh = &api.nexthops[count]; | |
589 | api_nh->vrf_id = nexthop->vrf_id; | |
590 | api_nh->type = nexthop->type; | |
bd054c1a | 591 | api_nh->weight = nexthop->weight; |
bf094f69 QY |
592 | switch (nexthop->type) { |
593 | case NEXTHOP_TYPE_BLACKHOLE: | |
594 | api_nh->bh_type = nexthop->bh_type; | |
595 | break; | |
596 | case NEXTHOP_TYPE_IPV4: | |
597 | api_nh->gate.ipv4 = nexthop->gate.ipv4; | |
598 | break; | |
599 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
600 | api_nh->gate.ipv4 = nexthop->gate.ipv4; | |
601 | api_nh->ifindex = nexthop->ifindex; | |
602 | break; | |
603 | case NEXTHOP_TYPE_IFINDEX: | |
604 | api_nh->ifindex = nexthop->ifindex; | |
605 | break; | |
606 | case NEXTHOP_TYPE_IPV6: | |
607 | api_nh->gate.ipv6 = nexthop->gate.ipv6; | |
608 | break; | |
609 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
610 | api_nh->gate.ipv6 = nexthop->gate.ipv6; | |
611 | api_nh->ifindex = nexthop->ifindex; | |
612 | } | |
613 | count++; | |
614 | } | |
615 | ||
9a0d4dd3 DS |
616 | /* Nexthops. */ |
617 | if (count) { | |
618 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); | |
619 | api.nexthop_num = count; | |
620 | } | |
621 | ||
bf094f69 QY |
622 | /* Attributes. */ |
623 | SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE); | |
624 | api.distance = re->distance; | |
625 | SET_FLAG(api.message, ZAPI_MESSAGE_METRIC); | |
626 | api.metric = re->metric; | |
627 | if (re->tag) { | |
628 | SET_FLAG(api.message, ZAPI_MESSAGE_TAG); | |
629 | api.tag = re->tag; | |
630 | } | |
631 | SET_FLAG(api.message, ZAPI_MESSAGE_MTU); | |
632 | api.mtu = re->mtu; | |
633 | ||
f3f45626 | 634 | struct stream *s = stream_new(stream_size); |
bf094f69 QY |
635 | |
636 | /* Encode route and send. */ | |
637 | if (zapi_route_encode(cmd, s, &api) < 0) { | |
638 | stream_free(s); | |
639 | return -1; | |
640 | } | |
641 | ||
642 | if (IS_ZEBRA_DEBUG_SEND) { | |
643 | char buf_prefix[PREFIX_STRLEN]; | |
8b1766b1 | 644 | |
bf094f69 QY |
645 | prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix)); |
646 | ||
647 | zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s", | |
648 | __func__, zserv_command_string(cmd), | |
649 | zebra_route_string(client->proto), | |
650 | zebra_route_string(api.type), api.vrf_id, | |
651 | buf_prefix); | |
652 | } | |
21ccc0cf | 653 | return zserv_send_message(client, s); |
bf094f69 QY |
654 | } |
655 | ||
656 | /* | |
657 | * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if | |
658 | * nexthop is not found on mrib. Returns both route metric and protocol | |
659 | * distance. | |
660 | */ | |
661 | static int zsend_ipv4_nexthop_lookup_mrib(struct zserv *client, | |
662 | struct in_addr addr, | |
663 | struct route_entry *re, | |
664 | struct zebra_vrf *zvrf) | |
665 | { | |
666 | struct stream *s; | |
667 | unsigned long nump; | |
668 | uint8_t num; | |
669 | struct nexthop *nexthop; | |
670 | ||
671 | /* Get output stream. */ | |
672 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
673 | stream_reset(s); | |
674 | ||
675 | /* Fill in result. */ | |
676 | zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf_id(zvrf)); | |
677 | stream_put_in_addr(s, &addr); | |
678 | ||
679 | if (re) { | |
680 | stream_putc(s, re->distance); | |
681 | stream_putl(s, re->metric); | |
682 | num = 0; | |
8b1766b1 QY |
683 | /* remember position for nexthop_num */ |
684 | nump = stream_get_endp(s); | |
685 | /* reserve room for nexthop_num */ | |
686 | stream_putc(s, 0); | |
687 | /* | |
688 | * Only non-recursive routes are elegible to resolve the | |
689 | * nexthop we are looking up. Therefore, we will just iterate | |
690 | * over the top chain of nexthops. | |
691 | */ | |
0eb97b86 | 692 | for (nexthop = re->nhe->nhg->nexthop; nexthop; |
6b468511 | 693 | nexthop = nexthop->next) |
bf094f69 QY |
694 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
695 | num += zserv_encode_nexthop(s, nexthop); | |
696 | ||
8b1766b1 QY |
697 | /* store nexthop_num */ |
698 | stream_putc_at(s, nump, num); | |
bf094f69 QY |
699 | } else { |
700 | stream_putc(s, 0); /* distance */ | |
701 | stream_putl(s, 0); /* metric */ | |
702 | stream_putc(s, 0); /* nexthop_num */ | |
703 | } | |
704 | ||
705 | stream_putw_at(s, 0, stream_get_endp(s)); | |
706 | ||
21ccc0cf | 707 | return zserv_send_message(client, s); |
bf094f69 QY |
708 | } |
709 | ||
86391e56 MS |
710 | /* |
711 | * Common utility send route notification, called from a path using a | |
712 | * route_entry and from a path using a dataplane context. | |
713 | */ | |
714 | static int route_notify_internal(const struct prefix *p, int type, | |
715 | uint16_t instance, vrf_id_t vrf_id, | |
716 | uint32_t table_id, | |
717 | enum zapi_route_notify_owner note) | |
bf094f69 QY |
718 | { |
719 | struct zserv *client; | |
720 | struct stream *s; | |
721 | uint8_t blen; | |
722 | ||
86391e56 | 723 | client = zserv_find_client(type, instance); |
bf094f69 QY |
724 | if (!client || !client->notify_owner) { |
725 | if (IS_ZEBRA_DEBUG_PACKET) { | |
726 | char buff[PREFIX_STRLEN]; | |
727 | ||
728 | zlog_debug( | |
729 | "Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u", | |
86391e56 MS |
730 | type, prefix2str(p, buff, sizeof(buff)), |
731 | table_id, note, vrf_id); | |
bf094f69 QY |
732 | } |
733 | return 0; | |
734 | } | |
735 | ||
736 | if (IS_ZEBRA_DEBUG_PACKET) { | |
737 | char buff[PREFIX_STRLEN]; | |
738 | ||
739 | zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u", | |
86391e56 MS |
740 | type, prefix2str(p, buff, sizeof(buff)), |
741 | table_id, note, vrf_id); | |
bf094f69 QY |
742 | } |
743 | ||
744 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
745 | stream_reset(s); | |
746 | ||
86391e56 | 747 | zclient_create_header(s, ZEBRA_ROUTE_NOTIFY_OWNER, vrf_id); |
bf094f69 QY |
748 | |
749 | stream_put(s, ¬e, sizeof(note)); | |
750 | ||
751 | stream_putc(s, p->family); | |
752 | ||
753 | blen = prefix_blen(p); | |
754 | stream_putc(s, p->prefixlen); | |
755 | stream_put(s, &p->u.prefix, blen); | |
756 | ||
86391e56 | 757 | stream_putl(s, table_id); |
bf094f69 QY |
758 | |
759 | stream_putw_at(s, 0, stream_get_endp(s)); | |
760 | ||
21ccc0cf | 761 | return zserv_send_message(client, s); |
bf094f69 QY |
762 | } |
763 | ||
86391e56 MS |
764 | int zsend_route_notify_owner(struct route_entry *re, const struct prefix *p, |
765 | enum zapi_route_notify_owner note) | |
766 | { | |
767 | return (route_notify_internal(p, re->type, re->instance, re->vrf_id, | |
768 | re->table, note)); | |
769 | } | |
770 | ||
7cdb1a84 MS |
771 | /* |
772 | * Route-owner notification using info from dataplane update context. | |
773 | */ | |
25779064 | 774 | int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx *ctx, |
7cdb1a84 MS |
775 | enum zapi_route_notify_owner note) |
776 | { | |
777 | return (route_notify_internal(dplane_ctx_get_dest(ctx), | |
778 | dplane_ctx_get_type(ctx), | |
779 | dplane_ctx_get_instance(ctx), | |
780 | dplane_ctx_get_vrf(ctx), | |
781 | dplane_ctx_get_table(ctx), | |
782 | note)); | |
86391e56 MS |
783 | } |
784 | ||
bf094f69 QY |
785 | void zsend_rule_notify_owner(struct zebra_pbr_rule *rule, |
786 | enum zapi_rule_notify_owner note) | |
787 | { | |
788 | struct listnode *node; | |
789 | struct zserv *client; | |
790 | struct stream *s; | |
791 | ||
792 | if (IS_ZEBRA_DEBUG_PACKET) | |
793 | zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__, | |
794 | rule->rule.unique); | |
795 | ||
161e9ab7 | 796 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) { |
bf094f69 QY |
797 | if (rule->sock == client->sock) |
798 | break; | |
799 | } | |
800 | ||
801 | if (!client) | |
802 | return; | |
803 | ||
804 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
805 | ||
806 | zclient_create_header(s, ZEBRA_RULE_NOTIFY_OWNER, VRF_DEFAULT); | |
807 | stream_put(s, ¬e, sizeof(note)); | |
808 | stream_putl(s, rule->rule.seq); | |
809 | stream_putl(s, rule->rule.priority); | |
810 | stream_putl(s, rule->rule.unique); | |
b19d55d0 | 811 | stream_putl(s, rule->rule.ifindex); |
bf094f69 QY |
812 | |
813 | stream_putw_at(s, 0, stream_get_endp(s)); | |
814 | ||
21ccc0cf | 815 | zserv_send_message(client, s); |
bf094f69 QY |
816 | } |
817 | ||
818 | void zsend_ipset_notify_owner(struct zebra_pbr_ipset *ipset, | |
819 | enum zapi_ipset_notify_owner note) | |
820 | { | |
821 | struct listnode *node; | |
822 | struct zserv *client; | |
823 | struct stream *s; | |
824 | ||
825 | if (IS_ZEBRA_DEBUG_PACKET) | |
826 | zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__, | |
827 | ipset->unique); | |
828 | ||
161e9ab7 | 829 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) { |
bf094f69 QY |
830 | if (ipset->sock == client->sock) |
831 | break; | |
832 | } | |
833 | ||
834 | if (!client) | |
835 | return; | |
836 | ||
837 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
838 | ||
839 | zclient_create_header(s, ZEBRA_IPSET_NOTIFY_OWNER, VRF_DEFAULT); | |
840 | stream_put(s, ¬e, sizeof(note)); | |
841 | stream_putl(s, ipset->unique); | |
842 | stream_put(s, ipset->ipset_name, ZEBRA_IPSET_NAME_SIZE); | |
843 | stream_putw_at(s, 0, stream_get_endp(s)); | |
844 | ||
21ccc0cf | 845 | zserv_send_message(client, s); |
bf094f69 QY |
846 | } |
847 | ||
848 | void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry *ipset, | |
849 | enum zapi_ipset_entry_notify_owner note) | |
850 | { | |
851 | struct listnode *node; | |
852 | struct zserv *client; | |
853 | struct stream *s; | |
854 | ||
855 | if (IS_ZEBRA_DEBUG_PACKET) | |
856 | zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__, | |
857 | ipset->unique); | |
858 | ||
161e9ab7 | 859 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) { |
bf094f69 QY |
860 | if (ipset->sock == client->sock) |
861 | break; | |
862 | } | |
863 | ||
864 | if (!client) | |
865 | return; | |
866 | ||
867 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
868 | ||
869 | zclient_create_header(s, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER, VRF_DEFAULT); | |
870 | stream_put(s, ¬e, sizeof(note)); | |
871 | stream_putl(s, ipset->unique); | |
872 | stream_put(s, ipset->backpointer->ipset_name, ZEBRA_IPSET_NAME_SIZE); | |
873 | stream_putw_at(s, 0, stream_get_endp(s)); | |
874 | ||
21ccc0cf | 875 | zserv_send_message(client, s); |
bf094f69 QY |
876 | } |
877 | ||
878 | void zsend_iptable_notify_owner(struct zebra_pbr_iptable *iptable, | |
879 | enum zapi_iptable_notify_owner note) | |
880 | { | |
881 | struct listnode *node; | |
882 | struct zserv *client; | |
883 | struct stream *s; | |
884 | ||
885 | if (IS_ZEBRA_DEBUG_PACKET) | |
886 | zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__, | |
887 | iptable->unique); | |
888 | ||
161e9ab7 | 889 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) { |
bf094f69 QY |
890 | if (iptable->sock == client->sock) |
891 | break; | |
892 | } | |
893 | ||
894 | if (!client) | |
895 | return; | |
896 | ||
897 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
898 | ||
899 | zclient_create_header(s, ZEBRA_IPTABLE_NOTIFY_OWNER, VRF_DEFAULT); | |
900 | stream_put(s, ¬e, sizeof(note)); | |
901 | stream_putl(s, iptable->unique); | |
902 | stream_putw_at(s, 0, stream_get_endp(s)); | |
903 | ||
21ccc0cf | 904 | zserv_send_message(client, s); |
bf094f69 QY |
905 | } |
906 | ||
907 | /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */ | |
908 | int zsend_router_id_update(struct zserv *client, struct prefix *p, | |
909 | vrf_id_t vrf_id) | |
910 | { | |
911 | int blen; | |
912 | ||
913 | /* Check this client need interface information. */ | |
914 | if (!vrf_bitmap_check(client->ridinfo, vrf_id)) | |
915 | return 0; | |
916 | ||
917 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
918 | ||
919 | /* Message type. */ | |
920 | zclient_create_header(s, ZEBRA_ROUTER_ID_UPDATE, vrf_id); | |
921 | ||
922 | /* Prefix information. */ | |
923 | stream_putc(s, p->family); | |
924 | blen = prefix_blen(p); | |
925 | stream_put(s, &p->u.prefix, blen); | |
926 | stream_putc(s, p->prefixlen); | |
927 | ||
928 | /* Write packet size. */ | |
929 | stream_putw_at(s, 0, stream_get_endp(s)); | |
930 | ||
21ccc0cf | 931 | return zserv_send_message(client, s); |
bf094f69 QY |
932 | } |
933 | ||
934 | /* | |
935 | * Function used by Zebra to send a PW status update to LDP daemon | |
936 | */ | |
937 | int zsend_pw_update(struct zserv *client, struct zebra_pw *pw) | |
938 | { | |
939 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
940 | ||
941 | zclient_create_header(s, ZEBRA_PW_STATUS_UPDATE, pw->vrf_id); | |
942 | stream_write(s, pw->ifname, IF_NAMESIZE); | |
943 | stream_putl(s, pw->ifindex); | |
944 | stream_putl(s, pw->status); | |
945 | ||
946 | /* Put length at the first point of the stream. */ | |
947 | stream_putw_at(s, 0, stream_get_endp(s)); | |
948 | ||
21ccc0cf | 949 | return zserv_send_message(client, s); |
bf094f69 QY |
950 | } |
951 | ||
952 | /* Send response to a get label chunk request to client */ | |
e11d7c96 EDP |
953 | int zsend_assign_label_chunk_response(struct zserv *client, vrf_id_t vrf_id, |
954 | uint8_t proto, uint16_t instance, | |
955 | struct label_manager_chunk *lmc) | |
bf094f69 QY |
956 | { |
957 | int ret; | |
958 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
959 | ||
960 | zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id); | |
f004f7c3 EDP |
961 | /* proto */ |
962 | stream_putc(s, proto); | |
963 | /* instance */ | |
964 | stream_putw(s, instance); | |
bf094f69 QY |
965 | |
966 | if (lmc) { | |
967 | /* keep */ | |
968 | stream_putc(s, lmc->keep); | |
969 | /* start and end labels */ | |
970 | stream_putl(s, lmc->start); | |
971 | stream_putl(s, lmc->end); | |
972 | } | |
973 | ||
974 | /* Write packet size. */ | |
975 | stream_putw_at(s, 0, stream_get_endp(s)); | |
976 | ||
977 | ret = writen(client->sock, s->data, stream_get_endp(s)); | |
978 | stream_free(s); | |
979 | return ret; | |
980 | } | |
981 | ||
982 | /* Send response to a label manager connect request to client */ | |
e11d7c96 EDP |
983 | int zsend_label_manager_connect_response(struct zserv *client, vrf_id_t vrf_id, |
984 | unsigned short result) | |
bf094f69 QY |
985 | { |
986 | int ret; | |
987 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
988 | ||
989 | zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id); | |
990 | ||
5dffb0e9 FR |
991 | /* proto */ |
992 | stream_putc(s, client->proto); | |
993 | ||
994 | /* instance */ | |
995 | stream_putw(s, client->instance); | |
996 | ||
bf094f69 QY |
997 | /* result */ |
998 | stream_putc(s, result); | |
999 | ||
1000 | /* Write packet size. */ | |
1001 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1002 | ||
1003 | ret = writen(client->sock, s->data, stream_get_endp(s)); | |
1004 | stream_free(s); | |
1005 | ||
1006 | return ret; | |
1007 | } | |
1008 | ||
1009 | /* Send response to a get table chunk request to client */ | |
1010 | static int zsend_assign_table_chunk_response(struct zserv *client, | |
1011 | vrf_id_t vrf_id, | |
1012 | struct table_manager_chunk *tmc) | |
1013 | { | |
1014 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
1015 | ||
1016 | zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, vrf_id); | |
1017 | ||
1018 | if (tmc) { | |
1019 | /* start and end labels */ | |
1020 | stream_putl(s, tmc->start); | |
1021 | stream_putl(s, tmc->end); | |
1022 | } | |
1023 | ||
1024 | /* Write packet size. */ | |
1025 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1026 | ||
21ccc0cf | 1027 | return zserv_send_message(client, s); |
bf094f69 QY |
1028 | } |
1029 | ||
1030 | static int zsend_table_manager_connect_response(struct zserv *client, | |
1031 | vrf_id_t vrf_id, | |
1032 | uint16_t result) | |
1033 | { | |
1034 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
1035 | ||
1036 | zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, vrf_id); | |
1037 | ||
1038 | /* result */ | |
1039 | stream_putc(s, result); | |
1040 | ||
1041 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1042 | ||
21ccc0cf | 1043 | return zserv_send_message(client, s); |
bf094f69 QY |
1044 | } |
1045 | ||
1046 | /* Inbound message handling ------------------------------------------------ */ | |
1047 | ||
2b64873d | 1048 | const int cmd2type[] = { |
bf094f69 QY |
1049 | [ZEBRA_NEXTHOP_REGISTER] = RNH_NEXTHOP_TYPE, |
1050 | [ZEBRA_NEXTHOP_UNREGISTER] = RNH_NEXTHOP_TYPE, | |
1051 | [ZEBRA_IMPORT_ROUTE_REGISTER] = RNH_IMPORT_CHECK_TYPE, | |
1052 | [ZEBRA_IMPORT_ROUTE_UNREGISTER] = RNH_IMPORT_CHECK_TYPE, | |
1053 | }; | |
1054 | ||
1055 | /* Nexthop register */ | |
1056 | static void zread_rnh_register(ZAPI_HANDLER_ARGS) | |
1057 | { | |
1058 | struct rnh *rnh; | |
1059 | struct stream *s; | |
1060 | struct prefix p; | |
1061 | unsigned short l = 0; | |
1062 | uint8_t flags = 0; | |
1063 | uint16_t type = cmd2type[hdr->command]; | |
1d30d1f4 | 1064 | bool exist; |
906b54dd DS |
1065 | bool flag_changed = false; |
1066 | uint8_t orig_flags; | |
bf094f69 QY |
1067 | |
1068 | if (IS_ZEBRA_DEBUG_NHT) | |
1069 | zlog_debug( | |
1070 | "rnh_register msg from client %s: hdr->length=%d, type=%s vrf=%u\n", | |
1071 | zebra_route_string(client->proto), hdr->length, | |
1072 | (type == RNH_NEXTHOP_TYPE) ? "nexthop" : "route", | |
1073 | zvrf->vrf->vrf_id); | |
1074 | ||
1075 | s = msg; | |
1076 | ||
1077 | client->nh_reg_time = monotime(NULL); | |
1078 | ||
1079 | while (l < hdr->length) { | |
1080 | STREAM_GETC(s, flags); | |
1081 | STREAM_GETW(s, p.family); | |
1082 | STREAM_GETC(s, p.prefixlen); | |
1083 | l += 4; | |
1084 | if (p.family == AF_INET) { | |
ab5990d8 | 1085 | client->v4_nh_watch_add_cnt++; |
bf094f69 | 1086 | if (p.prefixlen > IPV4_MAX_BITLEN) { |
9df414fe | 1087 | zlog_debug( |
bf094f69 QY |
1088 | "%s: Specified prefix hdr->length %d is too large for a v4 address", |
1089 | __PRETTY_FUNCTION__, p.prefixlen); | |
1090 | return; | |
1091 | } | |
1092 | STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN); | |
1093 | l += IPV4_MAX_BYTELEN; | |
1094 | } else if (p.family == AF_INET6) { | |
ab5990d8 | 1095 | client->v6_nh_watch_add_cnt++; |
bf094f69 | 1096 | if (p.prefixlen > IPV6_MAX_BITLEN) { |
9df414fe | 1097 | zlog_debug( |
bf094f69 QY |
1098 | "%s: Specified prefix hdr->length %d is to large for a v6 address", |
1099 | __PRETTY_FUNCTION__, p.prefixlen); | |
1100 | return; | |
1101 | } | |
1102 | STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN); | |
1103 | l += IPV6_MAX_BYTELEN; | |
1104 | } else { | |
af4c2728 | 1105 | flog_err( |
e914ccbe | 1106 | EC_ZEBRA_UNKNOWN_FAMILY, |
bf094f69 QY |
1107 | "rnh_register: Received unknown family type %d\n", |
1108 | p.family); | |
1109 | return; | |
1110 | } | |
1d30d1f4 DS |
1111 | rnh = zebra_add_rnh(&p, zvrf_id(zvrf), type, &exist); |
1112 | if (!rnh) | |
1113 | return; | |
1114 | ||
906b54dd | 1115 | orig_flags = rnh->flags; |
bf094f69 QY |
1116 | if (type == RNH_NEXTHOP_TYPE) { |
1117 | if (flags | |
1118 | && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) | |
1119 | SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED); | |
1120 | else if (!flags | |
1121 | && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) | |
1122 | UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED); | |
1123 | } else if (type == RNH_IMPORT_CHECK_TYPE) { | |
1124 | if (flags | |
1125 | && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH)) | |
1126 | SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH); | |
1127 | else if (!flags | |
1128 | && CHECK_FLAG(rnh->flags, | |
1129 | ZEBRA_NHT_EXACT_MATCH)) | |
1130 | UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH); | |
1131 | } | |
1132 | ||
906b54dd DS |
1133 | if (orig_flags != rnh->flags) |
1134 | flag_changed = true; | |
1135 | ||
bf094f69 | 1136 | /* Anything not AF_INET/INET6 has been filtered out above */ |
906b54dd | 1137 | if (!exist || flag_changed) |
73bf60a0 RW |
1138 | zebra_evaluate_rnh(zvrf, family2afi(p.family), 1, type, |
1139 | &p); | |
dd25a6b3 DS |
1140 | |
1141 | zebra_add_rnh_client(rnh, client, type, zvrf_id(zvrf)); | |
bf094f69 QY |
1142 | } |
1143 | ||
1144 | stream_failure: | |
1145 | return; | |
1146 | } | |
1147 | ||
1148 | /* Nexthop register */ | |
1149 | static void zread_rnh_unregister(ZAPI_HANDLER_ARGS) | |
1150 | { | |
1151 | struct rnh *rnh; | |
1152 | struct stream *s; | |
1153 | struct prefix p; | |
1154 | unsigned short l = 0; | |
1155 | uint16_t type = cmd2type[hdr->command]; | |
1156 | ||
1157 | if (IS_ZEBRA_DEBUG_NHT) | |
1158 | zlog_debug( | |
1159 | "rnh_unregister msg from client %s: hdr->length=%d vrf: %u\n", | |
1160 | zebra_route_string(client->proto), hdr->length, | |
1161 | zvrf->vrf->vrf_id); | |
1162 | ||
1163 | s = msg; | |
1164 | ||
1165 | while (l < hdr->length) { | |
1166 | uint8_t flags; | |
1167 | ||
1168 | STREAM_GETC(s, flags); | |
1169 | if (flags != 0) | |
1170 | goto stream_failure; | |
1171 | ||
1172 | STREAM_GETW(s, p.family); | |
1173 | STREAM_GETC(s, p.prefixlen); | |
1174 | l += 4; | |
1175 | if (p.family == AF_INET) { | |
ab5990d8 | 1176 | client->v4_nh_watch_rem_cnt++; |
bf094f69 | 1177 | if (p.prefixlen > IPV4_MAX_BITLEN) { |
9df414fe | 1178 | zlog_debug( |
bf094f69 QY |
1179 | "%s: Specified prefix hdr->length %d is to large for a v4 address", |
1180 | __PRETTY_FUNCTION__, p.prefixlen); | |
1181 | return; | |
1182 | } | |
1183 | STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN); | |
1184 | l += IPV4_MAX_BYTELEN; | |
1185 | } else if (p.family == AF_INET6) { | |
ab5990d8 | 1186 | client->v6_nh_watch_rem_cnt++; |
bf094f69 | 1187 | if (p.prefixlen > IPV6_MAX_BITLEN) { |
9df414fe | 1188 | zlog_debug( |
bf094f69 QY |
1189 | "%s: Specified prefix hdr->length %d is to large for a v6 address", |
1190 | __PRETTY_FUNCTION__, p.prefixlen); | |
1191 | return; | |
1192 | } | |
1193 | STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN); | |
1194 | l += IPV6_MAX_BYTELEN; | |
1195 | } else { | |
af4c2728 | 1196 | flog_err( |
e914ccbe | 1197 | EC_ZEBRA_UNKNOWN_FAMILY, |
bf094f69 QY |
1198 | "rnh_register: Received unknown family type %d\n", |
1199 | p.family); | |
1200 | return; | |
1201 | } | |
1202 | rnh = zebra_lookup_rnh(&p, zvrf_id(zvrf), type); | |
1203 | if (rnh) { | |
1204 | client->nh_dereg_time = monotime(NULL); | |
1205 | zebra_remove_rnh_client(rnh, client, type); | |
1206 | } | |
1207 | } | |
1208 | stream_failure: | |
1209 | return; | |
1210 | } | |
1211 | ||
1212 | #define ZEBRA_MIN_FEC_LENGTH 5 | |
1213 | ||
1214 | /* FEC register */ | |
1215 | static void zread_fec_register(ZAPI_HANDLER_ARGS) | |
1216 | { | |
1217 | struct stream *s; | |
1218 | unsigned short l = 0; | |
1219 | struct prefix p; | |
1220 | uint16_t flags; | |
57592a53 | 1221 | uint32_t label = MPLS_INVALID_LABEL; |
bf094f69 QY |
1222 | uint32_t label_index = MPLS_INVALID_LABEL_INDEX; |
1223 | ||
1224 | s = msg; | |
1225 | zvrf = vrf_info_lookup(VRF_DEFAULT); | |
1226 | if (!zvrf) | |
8b1766b1 | 1227 | return; |
bf094f69 QY |
1228 | |
1229 | /* | |
1230 | * The minimum amount of data that can be sent for one fec | |
1231 | * registration | |
1232 | */ | |
1233 | if (hdr->length < ZEBRA_MIN_FEC_LENGTH) { | |
af4c2728 | 1234 | flog_err( |
e914ccbe | 1235 | EC_ZEBRA_IRDP_LEN_MISMATCH, |
bf094f69 QY |
1236 | "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode", |
1237 | hdr->length); | |
1238 | return; | |
1239 | } | |
1240 | ||
1241 | while (l < hdr->length) { | |
1242 | STREAM_GETW(s, flags); | |
1243 | memset(&p, 0, sizeof(p)); | |
1244 | STREAM_GETW(s, p.family); | |
1245 | if (p.family != AF_INET && p.family != AF_INET6) { | |
af4c2728 | 1246 | flog_err( |
e914ccbe | 1247 | EC_ZEBRA_UNKNOWN_FAMILY, |
bf094f69 QY |
1248 | "fec_register: Received unknown family type %d\n", |
1249 | p.family); | |
1250 | return; | |
1251 | } | |
1252 | STREAM_GETC(s, p.prefixlen); | |
1253 | if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN) | |
1254 | || (p.family == AF_INET6 | |
1255 | && p.prefixlen > IPV6_MAX_BITLEN)) { | |
9df414fe | 1256 | zlog_debug( |
bf094f69 QY |
1257 | "%s: Specified prefix hdr->length: %d is to long for %d", |
1258 | __PRETTY_FUNCTION__, p.prefixlen, p.family); | |
1259 | return; | |
1260 | } | |
1261 | l += 5; | |
1262 | STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen)); | |
1263 | l += PSIZE(p.prefixlen); | |
57592a53 AD |
1264 | if (flags & ZEBRA_FEC_REGISTER_LABEL) { |
1265 | STREAM_GETL(s, label); | |
1266 | l += 4; | |
1267 | } else if (flags & ZEBRA_FEC_REGISTER_LABEL_INDEX) { | |
bf094f69 QY |
1268 | STREAM_GETL(s, label_index); |
1269 | l += 4; | |
57592a53 AD |
1270 | } |
1271 | ||
1272 | zebra_mpls_fec_register(zvrf, &p, label, label_index, client); | |
bf094f69 QY |
1273 | } |
1274 | ||
1275 | stream_failure: | |
1276 | return; | |
1277 | } | |
1278 | ||
1279 | /* FEC unregister */ | |
1280 | static void zread_fec_unregister(ZAPI_HANDLER_ARGS) | |
1281 | { | |
1282 | struct stream *s; | |
1283 | unsigned short l = 0; | |
1284 | struct prefix p; | |
1285 | uint16_t flags; | |
1286 | ||
1287 | s = msg; | |
1288 | zvrf = vrf_info_lookup(VRF_DEFAULT); | |
1289 | if (!zvrf) | |
8b1766b1 | 1290 | return; |
bf094f69 QY |
1291 | |
1292 | /* | |
1293 | * The minimum amount of data that can be sent for one | |
1294 | * fec unregistration | |
1295 | */ | |
1296 | if (hdr->length < ZEBRA_MIN_FEC_LENGTH) { | |
af4c2728 | 1297 | flog_err( |
e914ccbe | 1298 | EC_ZEBRA_IRDP_LEN_MISMATCH, |
bf094f69 QY |
1299 | "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode", |
1300 | hdr->length); | |
1301 | return; | |
1302 | } | |
1303 | ||
1304 | while (l < hdr->length) { | |
1305 | STREAM_GETW(s, flags); | |
1306 | if (flags != 0) | |
1307 | goto stream_failure; | |
1308 | ||
1309 | memset(&p, 0, sizeof(p)); | |
1310 | STREAM_GETW(s, p.family); | |
1311 | if (p.family != AF_INET && p.family != AF_INET6) { | |
af4c2728 | 1312 | flog_err( |
e914ccbe | 1313 | EC_ZEBRA_UNKNOWN_FAMILY, |
bf094f69 QY |
1314 | "fec_unregister: Received unknown family type %d\n", |
1315 | p.family); | |
1316 | return; | |
1317 | } | |
1318 | STREAM_GETC(s, p.prefixlen); | |
1319 | if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN) | |
1320 | || (p.family == AF_INET6 | |
1321 | && p.prefixlen > IPV6_MAX_BITLEN)) { | |
9df414fe | 1322 | zlog_debug( |
bf094f69 QY |
1323 | "%s: Received prefix hdr->length %d which is greater than %d can support", |
1324 | __PRETTY_FUNCTION__, p.prefixlen, p.family); | |
1325 | return; | |
1326 | } | |
1327 | l += 5; | |
1328 | STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen)); | |
1329 | l += PSIZE(p.prefixlen); | |
1330 | zebra_mpls_fec_unregister(zvrf, &p, client); | |
1331 | } | |
1332 | ||
1333 | stream_failure: | |
1334 | return; | |
1335 | } | |
1336 | ||
1337 | ||
1338 | /* | |
1339 | * Register zebra server interface information. | |
1340 | * Send current all interface and address information. | |
1341 | */ | |
1342 | static void zread_interface_add(ZAPI_HANDLER_ARGS) | |
1343 | { | |
1344 | struct vrf *vrf; | |
1345 | struct interface *ifp; | |
1346 | ||
bf094f69 QY |
1347 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { |
1348 | FOR_ALL_INTERFACES (vrf, ifp) { | |
1349 | /* Skip pseudo interface. */ | |
1350 | if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) | |
1351 | continue; | |
1352 | ||
1353 | zsend_interface_add(client, ifp); | |
27ecbea3 | 1354 | zsend_interface_link_params(client, ifp); |
bf094f69 QY |
1355 | zsend_interface_addresses(client, ifp); |
1356 | } | |
1357 | } | |
1358 | } | |
1359 | ||
1360 | /* Unregister zebra server interface information. */ | |
1361 | static void zread_interface_delete(ZAPI_HANDLER_ARGS) | |
1362 | { | |
bf094f69 QY |
1363 | } |
1364 | ||
c3bd894e QY |
1365 | /* |
1366 | * Handle message requesting interface be set up or down. | |
1367 | */ | |
1368 | static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS) | |
1369 | { | |
1370 | ifindex_t ifindex; | |
1371 | struct interface *ifp; | |
1372 | char down; | |
1373 | ||
1374 | STREAM_GETL(msg, ifindex); | |
1375 | STREAM_GETC(msg, down); | |
1376 | ||
1377 | /* set ifdown */ | |
1378 | ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT), ifindex); | |
c3bd894e | 1379 | |
65dc7dd3 QY |
1380 | if (ifp) { |
1381 | zlog_info("Setting interface %s (%u): protodown %s", ifp->name, | |
1382 | ifindex, down ? "on" : "off"); | |
1383 | zebra_if_set_protodown(ifp, down); | |
1384 | } else { | |
1385 | zlog_warn( | |
1386 | "Cannot set protodown %s for interface %u; does not exist", | |
1387 | down ? "on" : "off", ifindex); | |
1388 | } | |
1389 | ||
c3bd894e QY |
1390 | |
1391 | stream_failure: | |
1392 | return; | |
1393 | } | |
1394 | ||
1395 | ||
bf094f69 QY |
1396 | void zserv_nexthop_num_warn(const char *caller, const struct prefix *p, |
1397 | const unsigned int nexthop_num) | |
1398 | { | |
b3f2b590 | 1399 | if (nexthop_num > zrouter.multipath_num) { |
bf094f69 | 1400 | char buff[PREFIX2STR_BUFFER]; |
8b1766b1 | 1401 | |
bf094f69 | 1402 | prefix2str(p, buff, sizeof(buff)); |
9df414fe | 1403 | flog_warn( |
e914ccbe | 1404 | EC_ZEBRA_MORE_NH_THAN_MULTIPATH, |
bf094f69 | 1405 | "%s: Prefix %s has %d nexthops, but we can only use the first %d", |
b3f2b590 | 1406 | caller, buff, nexthop_num, zrouter.multipath_num); |
bf094f69 QY |
1407 | } |
1408 | } | |
1409 | ||
1410 | static void zread_route_add(ZAPI_HANDLER_ARGS) | |
1411 | { | |
1412 | struct stream *s; | |
1413 | struct zapi_route api; | |
1414 | struct zapi_nexthop *api_nh; | |
1415 | afi_t afi; | |
1416 | struct prefix_ipv6 *src_p = NULL; | |
1417 | struct route_entry *re; | |
1418 | struct nexthop *nexthop = NULL; | |
0eb97b86 | 1419 | struct nexthop_group *ng = NULL; |
bf094f69 | 1420 | int i, ret; |
946de1b9 | 1421 | vrf_id_t vrf_id; |
bf094f69 | 1422 | struct ipaddr vtep_ip; |
54bea4e5 | 1423 | struct interface *ifp; |
bf094f69 QY |
1424 | |
1425 | s = msg; | |
1426 | if (zapi_route_decode(s, &api) < 0) { | |
1427 | if (IS_ZEBRA_DEBUG_RECV) | |
1428 | zlog_debug("%s: Unable to decode zapi_route sent", | |
1429 | __PRETTY_FUNCTION__); | |
1430 | return; | |
1431 | } | |
1432 | ||
1433 | if (IS_ZEBRA_DEBUG_RECV) { | |
1434 | char buf_prefix[PREFIX_STRLEN]; | |
8b1766b1 | 1435 | |
bf094f69 | 1436 | prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix)); |
68a02e06 MS |
1437 | zlog_debug("%s: p=%s, flags=0x%x", |
1438 | __func__, buf_prefix, api.flags); | |
bf094f69 QY |
1439 | } |
1440 | ||
1441 | /* Allocate new route. */ | |
1442 | vrf_id = zvrf_id(zvrf); | |
1443 | re = XCALLOC(MTYPE_RE, sizeof(struct route_entry)); | |
1444 | re->type = api.type; | |
1445 | re->instance = api.instance; | |
1446 | re->flags = api.flags; | |
98572489 | 1447 | re->uptime = monotime(NULL); |
bf094f69 | 1448 | re->vrf_id = vrf_id; |
6b468511 | 1449 | |
60ca3cc2 | 1450 | if (api.tableid) |
bf094f69 QY |
1451 | re->table = api.tableid; |
1452 | else | |
1453 | re->table = zvrf->table_id; | |
1454 | ||
7fcb24bb RW |
1455 | if (!CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP) |
1456 | || api.nexthop_num == 0) { | |
7fcb24bb | 1457 | flog_warn(EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS, |
8ba70c4e DS |
1458 | "%s: received a route without nexthops for prefix %pFX from client %s", |
1459 | __func__, &api.prefix, | |
1460 | zebra_route_string(client->proto)); | |
6b468511 | 1461 | |
7fcb24bb RW |
1462 | XFREE(MTYPE_RE, re); |
1463 | return; | |
1464 | } | |
1465 | ||
0eb97b86 MS |
1466 | /* Use temporary list of nexthops */ |
1467 | ng = nexthop_group_new(); | |
1468 | ||
bf094f69 QY |
1469 | /* |
1470 | * TBD should _all_ of the nexthop add operations use | |
1471 | * api_nh->vrf_id instead of re->vrf_id ? I only changed | |
1472 | * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6. | |
1473 | */ | |
7fcb24bb RW |
1474 | for (i = 0; i < api.nexthop_num; i++) { |
1475 | api_nh = &api.nexthops[i]; | |
1476 | ifindex_t ifindex = 0; | |
1477 | ||
c8b891b4 SW |
1478 | nexthop = NULL; |
1479 | ||
7fcb24bb RW |
1480 | if (IS_ZEBRA_DEBUG_RECV) |
1481 | zlog_debug("nh type %d", api_nh->type); | |
1482 | ||
1483 | switch (api_nh->type) { | |
1484 | case NEXTHOP_TYPE_IFINDEX: | |
0eb97b86 MS |
1485 | nexthop = nexthop_from_ifindex(api_nh->ifindex, |
1486 | api_nh->vrf_id); | |
7fcb24bb RW |
1487 | break; |
1488 | case NEXTHOP_TYPE_IPV4: | |
1489 | if (IS_ZEBRA_DEBUG_RECV) { | |
1490 | char nhbuf[INET6_ADDRSTRLEN] = {0}; | |
1491 | ||
1492 | inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf, | |
1493 | INET6_ADDRSTRLEN); | |
1494 | zlog_debug("%s: nh=%s, vrf_id=%d", __func__, | |
1495 | nhbuf, api_nh->vrf_id); | |
1496 | } | |
0eb97b86 MS |
1497 | nexthop = nexthop_from_ipv4(&api_nh->gate.ipv4, |
1498 | NULL, api_nh->vrf_id); | |
7fcb24bb RW |
1499 | break; |
1500 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
1501 | ||
1502 | memset(&vtep_ip, 0, sizeof(struct ipaddr)); | |
e1e71450 | 1503 | ifindex = api_nh->ifindex; |
7fcb24bb RW |
1504 | if (IS_ZEBRA_DEBUG_RECV) { |
1505 | char nhbuf[INET6_ADDRSTRLEN] = {0}; | |
1506 | ||
1507 | inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf, | |
1508 | INET6_ADDRSTRLEN); | |
1509 | zlog_debug( | |
1510 | "%s: nh=%s, vrf_id=%d (re->vrf_id=%d), ifindex=%d", | |
1511 | __func__, nhbuf, api_nh->vrf_id, | |
1512 | re->vrf_id, ifindex); | |
bf094f69 | 1513 | } |
0eb97b86 MS |
1514 | nexthop = nexthop_from_ipv4_ifindex( |
1515 | &api_nh->gate.ipv4, NULL, ifindex, | |
7fcb24bb RW |
1516 | api_nh->vrf_id); |
1517 | ||
54bea4e5 DS |
1518 | ifp = if_lookup_by_index(ifindex, api_nh->vrf_id); |
1519 | if (ifp && connected_is_unnumbered(ifp)) | |
1520 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK); | |
2b83602b | 1521 | /* Special handling for IPv4 routes sourced from EVPN: |
1522 | * the nexthop and associated MAC need to be installed. | |
7fcb24bb RW |
1523 | */ |
1524 | if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) { | |
7fcb24bb RW |
1525 | vtep_ip.ipa_type = IPADDR_V4; |
1526 | memcpy(&(vtep_ip.ipaddr_v4), | |
1527 | &(api_nh->gate.ipv4), | |
1528 | sizeof(struct in_addr)); | |
1529 | zebra_vxlan_evpn_vrf_route_add( | |
744c63be | 1530 | api_nh->vrf_id, &api_nh->rmac, |
1531 | &vtep_ip, &api.prefix); | |
bf094f69 | 1532 | } |
7fcb24bb RW |
1533 | break; |
1534 | case NEXTHOP_TYPE_IPV6: | |
0eb97b86 MS |
1535 | nexthop = nexthop_from_ipv6(&api_nh->gate.ipv6, |
1536 | api_nh->vrf_id); | |
7fcb24bb RW |
1537 | break; |
1538 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
1539 | memset(&vtep_ip, 0, sizeof(struct ipaddr)); | |
e1e71450 | 1540 | ifindex = api_nh->ifindex; |
0eb97b86 MS |
1541 | nexthop = nexthop_from_ipv6_ifindex(&api_nh->gate.ipv6, |
1542 | ifindex, | |
1543 | api_nh->vrf_id); | |
7fcb24bb | 1544 | |
2b83602b | 1545 | /* Special handling for IPv6 routes sourced from EVPN: |
1546 | * the nexthop and associated MAC need to be installed. | |
7fcb24bb RW |
1547 | */ |
1548 | if (CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) { | |
7fcb24bb RW |
1549 | vtep_ip.ipa_type = IPADDR_V6; |
1550 | memcpy(&vtep_ip.ipaddr_v6, &(api_nh->gate.ipv6), | |
1551 | sizeof(struct in6_addr)); | |
1552 | zebra_vxlan_evpn_vrf_route_add( | |
744c63be | 1553 | api_nh->vrf_id, &api_nh->rmac, |
1554 | &vtep_ip, &api.prefix); | |
7fcb24bb RW |
1555 | } |
1556 | break; | |
1557 | case NEXTHOP_TYPE_BLACKHOLE: | |
0eb97b86 | 1558 | nexthop = nexthop_from_blackhole(api_nh->bh_type); |
7fcb24bb | 1559 | break; |
bf094f69 | 1560 | } |
bf094f69 | 1561 | |
7fcb24bb RW |
1562 | if (!nexthop) { |
1563 | flog_warn( | |
1564 | EC_ZEBRA_NEXTHOP_CREATION_FAILED, | |
1565 | "%s: Nexthops Specified: %d but we failed to properly create one", | |
1566 | __PRETTY_FUNCTION__, api.nexthop_num); | |
0eb97b86 | 1567 | nexthop_group_delete(&ng); |
7fcb24bb RW |
1568 | XFREE(MTYPE_RE, re); |
1569 | return; | |
1570 | } | |
0eb97b86 | 1571 | |
68a02e06 | 1572 | if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK)) |
fe85601c DS |
1573 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK); |
1574 | ||
bd054c1a DS |
1575 | if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_WEIGHT)) |
1576 | nexthop->weight = api_nh->weight; | |
1577 | ||
7fcb24bb | 1578 | /* MPLS labels for BGP-LU or Segment Routing */ |
68a02e06 | 1579 | if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL) |
7fcb24bb RW |
1580 | && api_nh->type != NEXTHOP_TYPE_IFINDEX |
1581 | && api_nh->type != NEXTHOP_TYPE_BLACKHOLE) { | |
1582 | enum lsp_types_t label_type; | |
1583 | ||
1584 | label_type = lsp_type_from_re_type(client->proto); | |
1585 | ||
1586 | if (IS_ZEBRA_DEBUG_RECV) { | |
1587 | zlog_debug( | |
1588 | "%s: adding %d labels of type %d (1st=%u)", | |
1589 | __func__, api_nh->label_num, label_type, | |
1590 | api_nh->labels[0]); | |
1591 | } | |
1592 | ||
1593 | nexthop_add_labels(nexthop, label_type, | |
1594 | api_nh->label_num, | |
1595 | &api_nh->labels[0]); | |
1596 | } | |
0eb97b86 MS |
1597 | |
1598 | /* Add new nexthop to temporary list */ | |
1599 | nexthop_group_add_sorted(ng, nexthop); | |
7fcb24bb RW |
1600 | } |
1601 | ||
bf094f69 QY |
1602 | if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE)) |
1603 | re->distance = api.distance; | |
1604 | if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC)) | |
1605 | re->metric = api.metric; | |
1606 | if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG)) | |
1607 | re->tag = api.tag; | |
1608 | if (CHECK_FLAG(api.message, ZAPI_MESSAGE_MTU)) | |
1609 | re->mtu = api.mtu; | |
1610 | ||
1611 | afi = family2afi(api.prefix.family); | |
1612 | if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) { | |
e914ccbe | 1613 | flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI, |
9df414fe | 1614 | "%s: Received SRC Prefix but afi is not v6", |
bf094f69 | 1615 | __PRETTY_FUNCTION__); |
0eb97b86 | 1616 | nexthop_group_delete(&ng); |
bf094f69 QY |
1617 | XFREE(MTYPE_RE, re); |
1618 | return; | |
1619 | } | |
1620 | if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) | |
1621 | src_p = &api.src_prefix; | |
1622 | ||
5e7939a5 DS |
1623 | if (api.safi != SAFI_UNICAST && api.safi != SAFI_MULTICAST) { |
1624 | flog_warn(EC_LIB_ZAPI_MISSMATCH, | |
1625 | "%s: Received safi: %d but we can only accept UNICAST or MULTICAST", | |
1626 | __func__, api.safi); | |
1627 | nexthop_group_delete(&ng); | |
1628 | XFREE(MTYPE_RE, re); | |
1629 | return; | |
1630 | } | |
0eb97b86 | 1631 | ret = rib_add_multipath(afi, api.safi, &api.prefix, src_p, re, ng); |
bf094f69 QY |
1632 | |
1633 | /* Stats */ | |
1634 | switch (api.prefix.family) { | |
1635 | case AF_INET: | |
1636 | if (ret > 0) | |
1637 | client->v4_route_add_cnt++; | |
1638 | else if (ret < 0) | |
1639 | client->v4_route_upd8_cnt++; | |
1640 | break; | |
1641 | case AF_INET6: | |
1642 | if (ret > 0) | |
1643 | client->v6_route_add_cnt++; | |
1644 | else if (ret < 0) | |
1645 | client->v6_route_upd8_cnt++; | |
1646 | break; | |
1647 | } | |
1648 | } | |
1649 | ||
1650 | static void zread_route_del(ZAPI_HANDLER_ARGS) | |
1651 | { | |
1652 | struct stream *s; | |
1653 | struct zapi_route api; | |
1654 | afi_t afi; | |
1655 | struct prefix_ipv6 *src_p = NULL; | |
1656 | uint32_t table_id; | |
1657 | ||
1658 | s = msg; | |
1659 | if (zapi_route_decode(s, &api) < 0) | |
1660 | return; | |
1661 | ||
1662 | afi = family2afi(api.prefix.family); | |
1663 | if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) { | |
e914ccbe | 1664 | flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI, |
9df414fe | 1665 | "%s: Received a src prefix while afi is not v6", |
bf094f69 QY |
1666 | __PRETTY_FUNCTION__); |
1667 | return; | |
1668 | } | |
1669 | if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) | |
1670 | src_p = &api.src_prefix; | |
1671 | ||
60ca3cc2 | 1672 | if (api.tableid) |
bf094f69 QY |
1673 | table_id = api.tableid; |
1674 | else | |
1675 | table_id = zvrf->table_id; | |
1676 | ||
1677 | rib_delete(afi, api.safi, zvrf_id(zvrf), api.type, api.instance, | |
bc541126 | 1678 | api.flags, &api.prefix, src_p, NULL, 0, table_id, api.metric, |
40ecd8e4 | 1679 | api.distance, false); |
bf094f69 QY |
1680 | |
1681 | /* Stats */ | |
1682 | switch (api.prefix.family) { | |
1683 | case AF_INET: | |
1684 | client->v4_route_del_cnt++; | |
1685 | break; | |
1686 | case AF_INET6: | |
1687 | client->v6_route_del_cnt++; | |
1688 | break; | |
1689 | } | |
1690 | } | |
1691 | ||
bf094f69 QY |
1692 | /* MRIB Nexthop lookup for IPv4. */ |
1693 | static void zread_ipv4_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS) | |
1694 | { | |
1695 | struct in_addr addr; | |
1696 | struct route_entry *re; | |
1697 | ||
1698 | STREAM_GET(&addr.s_addr, msg, IPV4_MAX_BYTELEN); | |
1699 | re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr, NULL); | |
1700 | zsend_ipv4_nexthop_lookup_mrib(client, addr, re, zvrf); | |
1701 | ||
1702 | stream_failure: | |
1703 | return; | |
1704 | } | |
1705 | ||
bf094f69 QY |
1706 | /* Register zebra server router-id information. Send current router-id */ |
1707 | static void zread_router_id_add(ZAPI_HANDLER_ARGS) | |
1708 | { | |
1709 | struct prefix p; | |
1710 | ||
1711 | /* Router-id information is needed. */ | |
1712 | vrf_bitmap_set(client->ridinfo, zvrf_id(zvrf)); | |
1713 | ||
1714 | router_id_get(&p, zvrf_id(zvrf)); | |
1715 | ||
1716 | zsend_router_id_update(client, &p, zvrf_id(zvrf)); | |
1717 | } | |
1718 | ||
1719 | /* Unregister zebra server router-id information. */ | |
1720 | static void zread_router_id_delete(ZAPI_HANDLER_ARGS) | |
1721 | { | |
1722 | vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf)); | |
1723 | } | |
1724 | ||
1725 | static void zsend_capabilities(struct zserv *client, struct zebra_vrf *zvrf) | |
1726 | { | |
1727 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
1728 | ||
1729 | zclient_create_header(s, ZEBRA_CAPABILITIES, zvrf->vrf->vrf_id); | |
bb6b7f79 | 1730 | stream_putl(s, vrf_get_backend()); |
bf094f69 | 1731 | stream_putc(s, mpls_enabled); |
b3f2b590 | 1732 | stream_putl(s, zrouter.multipath_num); |
02c0866d | 1733 | stream_putc(s, zebra_mlag_get_role()); |
bf094f69 QY |
1734 | |
1735 | stream_putw_at(s, 0, stream_get_endp(s)); | |
21ccc0cf | 1736 | zserv_send_message(client, s); |
bf094f69 QY |
1737 | } |
1738 | ||
b120fe3b DS |
1739 | void zsend_capabilities_all_clients(void) |
1740 | { | |
1741 | struct listnode *node, *nnode; | |
1742 | struct zebra_vrf *zvrf; | |
1743 | struct zserv *client; | |
1744 | ||
1745 | zvrf = vrf_info_lookup(VRF_DEFAULT); | |
1746 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) { | |
1747 | zsend_capabilities(client, zvrf); | |
1748 | } | |
1749 | } | |
1750 | ||
bf094f69 QY |
1751 | /* Tie up route-type and client->sock */ |
1752 | static void zread_hello(ZAPI_HANDLER_ARGS) | |
1753 | { | |
1754 | /* type of protocol (lib/zebra.h) */ | |
1755 | uint8_t proto; | |
1756 | unsigned short instance; | |
1757 | uint8_t notify; | |
1758 | ||
1759 | STREAM_GETC(msg, proto); | |
1760 | STREAM_GETW(msg, instance); | |
1761 | STREAM_GETC(msg, notify); | |
1762 | if (notify) | |
1763 | client->notify_owner = true; | |
1764 | ||
1765 | /* accept only dynamic routing protocols */ | |
f23cbcda | 1766 | if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_CONNECT)) { |
bf094f69 QY |
1767 | zlog_notice( |
1768 | "client %d says hello and bids fair to announce only %s routes vrf=%u", | |
1769 | client->sock, zebra_route_string(proto), | |
1770 | zvrf->vrf->vrf_id); | |
1771 | if (instance) | |
1772 | zlog_notice("client protocol instance %d", instance); | |
1773 | ||
1774 | client->proto = proto; | |
1775 | client->instance = instance; | |
1776 | } | |
1777 | ||
851140a7 S |
1778 | /* Graceful restart processing for client connect */ |
1779 | zebra_gr_client_reconnect(client); | |
bf094f69 | 1780 | zsend_capabilities(client, zvrf); |
bb6b7f79 | 1781 | zebra_vrf_update_all(client); |
bf094f69 QY |
1782 | stream_failure: |
1783 | return; | |
1784 | } | |
1785 | ||
1786 | /* Unregister all information in a VRF. */ | |
1787 | static void zread_vrf_unregister(ZAPI_HANDLER_ARGS) | |
1788 | { | |
1789 | int i; | |
1790 | afi_t afi; | |
1791 | ||
49db7a7b | 1792 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { |
bf094f69 QY |
1793 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
1794 | vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf)); | |
49db7a7b RW |
1795 | vrf_bitmap_unset(client->redist_default[afi], zvrf_id(zvrf)); |
1796 | } | |
bf094f69 QY |
1797 | vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf)); |
1798 | } | |
1799 | ||
ea6b290b RW |
1800 | /* |
1801 | * Handle request to create an MPLS LSP. | |
1802 | * | |
1803 | * A single message can fully specify an LSP with multiple nexthops. | |
1804 | * | |
1805 | * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is | |
1806 | * updated to use the received label(s). | |
1807 | */ | |
1808 | static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS) | |
bf094f69 QY |
1809 | { |
1810 | struct stream *s; | |
bad6b0e7 | 1811 | struct zapi_labels zl; |
bf094f69 QY |
1812 | |
1813 | /* Get input stream. */ | |
1814 | s = msg; | |
bad6b0e7 RW |
1815 | if (zapi_labels_decode(s, &zl) < 0) { |
1816 | if (IS_ZEBRA_DEBUG_RECV) | |
1817 | zlog_debug("%s: Unable to decode zapi_labels sent", | |
1818 | __PRETTY_FUNCTION__); | |
bf094f69 QY |
1819 | return; |
1820 | } | |
bf094f69 | 1821 | |
bf094f69 QY |
1822 | if (!mpls_enabled) |
1823 | return; | |
1824 | ||
ea6b290b | 1825 | for (int i = 0; i < zl.nexthop_num; i++) { |
4945002d | 1826 | struct zapi_nexthop *znh; |
ea6b290b RW |
1827 | |
1828 | znh = &zl.nexthops[i]; | |
4945002d MS |
1829 | mpls_lsp_install(zvrf, zl.type, zl.local_label, 1, znh->labels, |
1830 | znh->type, &znh->gate, znh->ifindex); | |
ea6b290b | 1831 | |
b3c49d0e RW |
1832 | if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN)) |
1833 | mpls_ftn_update(1, zvrf, zl.type, &zl.route.prefix, | |
4945002d | 1834 | znh->type, &znh->gate, znh->ifindex, |
ea6b290b | 1835 | zl.route.type, zl.route.instance, |
4945002d | 1836 | znh->labels[0]); |
ea6b290b RW |
1837 | } |
1838 | } | |
1839 | ||
1840 | /* | |
1841 | * Handle request to delete an MPLS LSP. | |
1842 | * | |
1843 | * An LSP is identified by its type and local label. When the received message | |
1844 | * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the | |
1845 | * listed LSP nexthops (aka NHLFEs) are deleted. | |
1846 | * | |
1847 | * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified | |
1848 | * FEC (route) nexthops are deleted. | |
1849 | */ | |
1850 | static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS) | |
1851 | { | |
1852 | struct stream *s; | |
1853 | struct zapi_labels zl; | |
1854 | ||
1855 | /* Get input stream. */ | |
1856 | s = msg; | |
1857 | if (zapi_labels_decode(s, &zl) < 0) { | |
1858 | if (IS_ZEBRA_DEBUG_RECV) | |
1859 | zlog_debug("%s: Unable to decode zapi_labels sent", | |
1860 | __PRETTY_FUNCTION__); | |
1861 | return; | |
1862 | } | |
1863 | ||
1864 | if (!mpls_enabled) | |
1865 | return; | |
1866 | ||
1867 | if (zl.nexthop_num > 0) { | |
1868 | for (int i = 0; i < zl.nexthop_num; i++) { | |
4945002d | 1869 | struct zapi_nexthop *znh; |
ea6b290b RW |
1870 | |
1871 | znh = &zl.nexthops[i]; | |
1872 | mpls_lsp_uninstall(zvrf, zl.type, zl.local_label, | |
4945002d | 1873 | znh->type, &znh->gate, |
ea6b290b RW |
1874 | znh->ifindex); |
1875 | ||
1876 | if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN)) | |
1877 | mpls_ftn_update(0, zvrf, zl.type, | |
1878 | &zl.route.prefix, znh->type, | |
4945002d | 1879 | &znh->gate, znh->ifindex, |
ea6b290b | 1880 | zl.route.type, |
4945002d MS |
1881 | zl.route.instance, |
1882 | znh->labels[0]); | |
ea6b290b RW |
1883 | } |
1884 | } else { | |
1885 | mpls_lsp_uninstall_all_vrf(zvrf, zl.type, zl.local_label); | |
1886 | ||
b3c49d0e | 1887 | if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN)) |
ea6b290b RW |
1888 | mpls_ftn_uninstall(zvrf, zl.type, &zl.route.prefix, |
1889 | zl.route.type, zl.route.instance); | |
1890 | } | |
1891 | } | |
1892 | ||
1893 | /* | |
1894 | * Handle request to add an MPLS LSP or change an existing one. | |
1895 | * | |
1896 | * A single message can fully specify an LSP with multiple nexthops. | |
1897 | * | |
1898 | * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is | |
1899 | * updated to use the received label(s). | |
1900 | * | |
1901 | * NOTE: zebra will use route replace semantics (make-before-break) to update | |
1902 | * the LSP in the forwarding plane if that's supported by the underlying | |
1903 | * platform. | |
1904 | */ | |
1905 | static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS) | |
1906 | { | |
1907 | struct stream *s; | |
1908 | struct zapi_labels zl; | |
1909 | ||
1910 | /* Get input stream. */ | |
1911 | s = msg; | |
1912 | if (zapi_labels_decode(s, &zl) < 0) { | |
1913 | if (IS_ZEBRA_DEBUG_RECV) | |
1914 | zlog_debug("%s: Unable to decode zapi_labels sent", | |
1915 | __PRETTY_FUNCTION__); | |
1916 | return; | |
1917 | } | |
1918 | ||
1919 | if (!mpls_enabled) | |
1920 | return; | |
1921 | ||
1922 | mpls_lsp_uninstall_all_vrf(zvrf, zl.type, zl.local_label); | |
1923 | if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN)) | |
1924 | mpls_ftn_uninstall(zvrf, zl.type, &zl.route.prefix, | |
1925 | zl.route.type, zl.route.instance); | |
1926 | ||
1927 | for (int i = 0; i < zl.nexthop_num; i++) { | |
4945002d | 1928 | struct zapi_nexthop *znh; |
ea6b290b RW |
1929 | |
1930 | znh = &zl.nexthops[i]; | |
4945002d MS |
1931 | mpls_lsp_install(zvrf, zl.type, zl.local_label, |
1932 | 1, znh->labels, znh->type, | |
1933 | &znh->gate, znh->ifindex); | |
ea6b290b RW |
1934 | |
1935 | if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN)) { | |
1936 | mpls_ftn_update(1, zvrf, zl.type, &zl.route.prefix, | |
4945002d | 1937 | znh->type, &znh->gate, znh->ifindex, |
ea6b290b | 1938 | zl.route.type, zl.route.instance, |
4945002d | 1939 | znh->labels[0]); |
ea6b290b | 1940 | } |
bf094f69 | 1941 | } |
bf094f69 QY |
1942 | } |
1943 | ||
1944 | /* Send response to a table manager connect request to client */ | |
1945 | static void zread_table_manager_connect(struct zserv *client, | |
1946 | struct stream *msg, vrf_id_t vrf_id) | |
1947 | { | |
1948 | struct stream *s; | |
1949 | uint8_t proto; | |
1950 | uint16_t instance; | |
c479e756 | 1951 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
bf094f69 QY |
1952 | |
1953 | s = msg; | |
1954 | ||
1955 | /* Get data. */ | |
1956 | STREAM_GETC(s, proto); | |
1957 | STREAM_GETW(s, instance); | |
1958 | ||
1959 | /* accept only dynamic routing protocols */ | |
1960 | if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) { | |
e914ccbe | 1961 | flog_err(EC_ZEBRA_TM_WRONG_PROTO, |
1c50c1c0 QY |
1962 | "client %d has wrong protocol %s", client->sock, |
1963 | zebra_route_string(proto)); | |
bf094f69 QY |
1964 | zsend_table_manager_connect_response(client, vrf_id, 1); |
1965 | return; | |
1966 | } | |
c479e756 DS |
1967 | zlog_notice("client %d with vrf %s(%u) instance %u connected as %s", |
1968 | client->sock, VRF_LOGNAME(vrf), vrf_id, instance, | |
1969 | zebra_route_string(proto)); | |
bf094f69 QY |
1970 | client->proto = proto; |
1971 | client->instance = instance; | |
1972 | ||
1973 | /* | |
1974 | * Release previous labels of same protocol and instance. | |
1975 | * This is done in case it restarted from an unexpected shutdown. | |
1976 | */ | |
453844ab | 1977 | release_daemon_table_chunks(client); |
bf094f69 QY |
1978 | |
1979 | zsend_table_manager_connect_response(client, vrf_id, 0); | |
1980 | ||
1981 | stream_failure: | |
1982 | return; | |
1983 | } | |
1984 | ||
1985 | static void zread_label_manager_connect(struct zserv *client, | |
1986 | struct stream *msg, vrf_id_t vrf_id) | |
1987 | { | |
1988 | struct stream *s; | |
1989 | /* type of protocol (lib/zebra.h) */ | |
1990 | uint8_t proto; | |
1991 | unsigned short instance; | |
1992 | ||
1993 | /* Get input stream. */ | |
1994 | s = msg; | |
1995 | ||
1996 | /* Get data. */ | |
1997 | STREAM_GETC(s, proto); | |
1998 | STREAM_GETW(s, instance); | |
1999 | ||
2000 | /* accept only dynamic routing protocols */ | |
2001 | if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) { | |
e914ccbe | 2002 | flog_err(EC_ZEBRA_TM_WRONG_PROTO, |
1c50c1c0 QY |
2003 | "client %d has wrong protocol %s", client->sock, |
2004 | zebra_route_string(proto)); | |
e11d7c96 | 2005 | zsend_label_manager_connect_response(client, vrf_id, 1); |
bf094f69 QY |
2006 | return; |
2007 | } | |
e11d7c96 EDP |
2008 | |
2009 | /* recall proto and instance in this socket */ | |
bf094f69 QY |
2010 | client->proto = proto; |
2011 | client->instance = instance; | |
2012 | ||
e11d7c96 EDP |
2013 | /* call hook for connection using wrapper */ |
2014 | lm_client_connect_call(proto, instance, vrf_id); | |
bf094f69 QY |
2015 | |
2016 | stream_failure: | |
2017 | return; | |
2018 | } | |
2019 | ||
2020 | static void zread_get_label_chunk(struct zserv *client, struct stream *msg, | |
2021 | vrf_id_t vrf_id) | |
2022 | { | |
2023 | struct stream *s; | |
2024 | uint8_t keep; | |
0e3b6a92 | 2025 | uint32_t size, base; |
e11d7c96 | 2026 | struct label_manager_chunk *lmc = NULL; |
5dffb0e9 FR |
2027 | uint8_t proto; |
2028 | unsigned short instance; | |
bf094f69 QY |
2029 | |
2030 | /* Get input stream. */ | |
2031 | s = msg; | |
2032 | ||
2033 | /* Get data. */ | |
5dffb0e9 FR |
2034 | STREAM_GETC(s, proto); |
2035 | STREAM_GETW(s, instance); | |
bf094f69 QY |
2036 | STREAM_GETC(s, keep); |
2037 | STREAM_GETL(s, size); | |
0e3b6a92 | 2038 | STREAM_GETL(s, base); |
bf094f69 | 2039 | |
e11d7c96 EDP |
2040 | /* call hook to get a chunk using wrapper */ |
2041 | lm_get_chunk_call(&lmc, proto, instance, keep, size, base, vrf_id); | |
bf094f69 | 2042 | |
bf094f69 | 2043 | if (!lmc) |
af4c2728 | 2044 | flog_err( |
e914ccbe | 2045 | EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK, |
0313523d | 2046 | "Unable to assign Label Chunk of size %u to %s instance %u", |
f533be73 | 2047 | size, zebra_route_string(proto), instance); |
bf094f69 | 2048 | else |
8f86bb06 DS |
2049 | if (IS_ZEBRA_DEBUG_PACKET) |
2050 | zlog_debug("Assigned Label Chunk %u - %u to %s instance %u", | |
2051 | lmc->start, lmc->end, | |
2052 | zebra_route_string(proto), instance); | |
bf094f69 QY |
2053 | |
2054 | stream_failure: | |
2055 | return; | |
2056 | } | |
2057 | ||
2058 | static void zread_release_label_chunk(struct zserv *client, struct stream *msg) | |
2059 | { | |
2060 | struct stream *s; | |
2061 | uint32_t start, end; | |
5dffb0e9 FR |
2062 | uint8_t proto; |
2063 | unsigned short instance; | |
bf094f69 QY |
2064 | |
2065 | /* Get input stream. */ | |
2066 | s = msg; | |
2067 | ||
2068 | /* Get data. */ | |
5dffb0e9 FR |
2069 | STREAM_GETC(s, proto); |
2070 | STREAM_GETW(s, instance); | |
bf094f69 QY |
2071 | STREAM_GETL(s, start); |
2072 | STREAM_GETL(s, end); | |
2073 | ||
e11d7c96 EDP |
2074 | /* call hook to release a chunk using wrapper */ |
2075 | lm_release_chunk_call(proto, instance, start, end); | |
bf094f69 QY |
2076 | |
2077 | stream_failure: | |
2078 | return; | |
2079 | } | |
e11d7c96 | 2080 | |
bf094f69 QY |
2081 | static void zread_label_manager_request(ZAPI_HANDLER_ARGS) |
2082 | { | |
e11d7c96 EDP |
2083 | if (hdr->command == ZEBRA_LABEL_MANAGER_CONNECT |
2084 | || hdr->command == ZEBRA_LABEL_MANAGER_CONNECT_ASYNC) | |
2085 | zread_label_manager_connect(client, msg, zvrf_id(zvrf)); | |
bf094f69 | 2086 | else { |
e11d7c96 EDP |
2087 | if (hdr->command == ZEBRA_GET_LABEL_CHUNK) |
2088 | zread_get_label_chunk(client, msg, zvrf_id(zvrf)); | |
2089 | else if (hdr->command == ZEBRA_RELEASE_LABEL_CHUNK) | |
2090 | zread_release_label_chunk(client, msg); | |
bf094f69 QY |
2091 | } |
2092 | } | |
2093 | ||
2094 | static void zread_get_table_chunk(struct zserv *client, struct stream *msg, | |
2095 | vrf_id_t vrf_id) | |
2096 | { | |
2097 | struct stream *s; | |
2098 | uint32_t size; | |
2099 | struct table_manager_chunk *tmc; | |
2100 | ||
2101 | /* Get input stream. */ | |
2102 | s = msg; | |
2103 | ||
2104 | /* Get data. */ | |
2105 | STREAM_GETL(s, size); | |
2106 | ||
2107 | tmc = assign_table_chunk(client->proto, client->instance, size); | |
2108 | if (!tmc) | |
e914ccbe | 2109 | flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK, |
1c50c1c0 QY |
2110 | "%s: Unable to assign Table Chunk of size %u", |
2111 | __func__, size); | |
bf094f69 QY |
2112 | else |
2113 | zlog_debug("Assigned Table Chunk %u - %u", tmc->start, | |
2114 | tmc->end); | |
2115 | /* send response back */ | |
2116 | zsend_assign_table_chunk_response(client, vrf_id, tmc); | |
2117 | ||
2118 | stream_failure: | |
2119 | return; | |
2120 | } | |
2121 | ||
2122 | static void zread_release_table_chunk(struct zserv *client, struct stream *msg) | |
2123 | { | |
2124 | struct stream *s; | |
2125 | uint32_t start, end; | |
2126 | ||
2127 | /* Get input stream. */ | |
2128 | s = msg; | |
2129 | ||
2130 | /* Get data. */ | |
2131 | STREAM_GETL(s, start); | |
2132 | STREAM_GETL(s, end); | |
2133 | ||
2134 | release_table_chunk(client->proto, client->instance, start, end); | |
2135 | ||
2136 | stream_failure: | |
2137 | return; | |
2138 | } | |
2139 | ||
2140 | static void zread_table_manager_request(ZAPI_HANDLER_ARGS) | |
2141 | { | |
2142 | /* to avoid sending other messages like ZERBA_INTERFACE_UP */ | |
2143 | if (hdr->command == ZEBRA_TABLE_MANAGER_CONNECT) | |
2144 | zread_table_manager_connect(client, msg, zvrf_id(zvrf)); | |
2145 | else { | |
2146 | /* Sanity: don't allow 'unidentified' requests */ | |
2147 | if (!client->proto) { | |
af4c2728 | 2148 | flog_err( |
e914ccbe | 2149 | EC_ZEBRA_TM_ALIENS, |
bf094f69 QY |
2150 | "Got table request from an unidentified client"); |
2151 | return; | |
2152 | } | |
2153 | if (hdr->command == ZEBRA_GET_TABLE_CHUNK) | |
2154 | zread_get_table_chunk(client, msg, zvrf_id(zvrf)); | |
2155 | else if (hdr->command == ZEBRA_RELEASE_TABLE_CHUNK) | |
2156 | zread_release_table_chunk(client, msg); | |
2157 | } | |
2158 | } | |
2159 | ||
2160 | static void zread_pseudowire(ZAPI_HANDLER_ARGS) | |
2161 | { | |
2162 | struct stream *s; | |
2163 | char ifname[IF_NAMESIZE]; | |
2164 | ifindex_t ifindex; | |
2165 | int type; | |
2166 | int af; | |
2167 | union g_addr nexthop; | |
2168 | uint32_t local_label; | |
2169 | uint32_t remote_label; | |
2170 | uint8_t flags; | |
2171 | union pw_protocol_fields data; | |
2172 | uint8_t protocol; | |
2173 | struct zebra_pw *pw; | |
2174 | ||
2175 | /* Get input stream. */ | |
2176 | s = msg; | |
2177 | ||
2178 | /* Get data. */ | |
2179 | STREAM_GET(ifname, s, IF_NAMESIZE); | |
f223b3d1 | 2180 | ifname[IF_NAMESIZE - 1] = '\0'; |
bf094f69 QY |
2181 | STREAM_GETL(s, ifindex); |
2182 | STREAM_GETL(s, type); | |
2183 | STREAM_GETL(s, af); | |
2184 | switch (af) { | |
2185 | case AF_INET: | |
2186 | STREAM_GET(&nexthop.ipv4.s_addr, s, IPV4_MAX_BYTELEN); | |
2187 | break; | |
2188 | case AF_INET6: | |
2189 | STREAM_GET(&nexthop.ipv6, s, 16); | |
2190 | break; | |
2191 | default: | |
2192 | return; | |
2193 | } | |
2194 | STREAM_GETL(s, local_label); | |
2195 | STREAM_GETL(s, remote_label); | |
2196 | STREAM_GETC(s, flags); | |
2197 | STREAM_GET(&data, s, sizeof(data)); | |
2198 | protocol = client->proto; | |
2199 | ||
2200 | pw = zebra_pw_find(zvrf, ifname); | |
2201 | switch (hdr->command) { | |
2202 | case ZEBRA_PW_ADD: | |
2203 | if (pw) { | |
e914ccbe | 2204 | flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS, |
9df414fe | 2205 | "%s: pseudowire %s already exists [%s]", |
bf094f69 QY |
2206 | __func__, ifname, |
2207 | zserv_command_string(hdr->command)); | |
2208 | return; | |
2209 | } | |
2210 | ||
2211 | zebra_pw_add(zvrf, ifname, protocol, client); | |
2212 | break; | |
2213 | case ZEBRA_PW_DELETE: | |
2214 | if (!pw) { | |
e914ccbe | 2215 | flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT, |
9df414fe | 2216 | "%s: pseudowire %s not found [%s]", __func__, |
bf094f69 QY |
2217 | ifname, zserv_command_string(hdr->command)); |
2218 | return; | |
2219 | } | |
2220 | ||
2221 | zebra_pw_del(zvrf, pw); | |
2222 | break; | |
2223 | case ZEBRA_PW_SET: | |
2224 | case ZEBRA_PW_UNSET: | |
2225 | if (!pw) { | |
e914ccbe | 2226 | flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT, |
9df414fe | 2227 | "%s: pseudowire %s not found [%s]", __func__, |
bf094f69 QY |
2228 | ifname, zserv_command_string(hdr->command)); |
2229 | return; | |
2230 | } | |
2231 | ||
2232 | switch (hdr->command) { | |
2233 | case ZEBRA_PW_SET: | |
2234 | pw->enabled = 1; | |
2235 | break; | |
2236 | case ZEBRA_PW_UNSET: | |
2237 | pw->enabled = 0; | |
2238 | break; | |
2239 | } | |
2240 | ||
2241 | zebra_pw_change(pw, ifindex, type, af, &nexthop, local_label, | |
2242 | remote_label, flags, &data); | |
2243 | break; | |
2244 | } | |
2245 | ||
2246 | stream_failure: | |
2247 | return; | |
2248 | } | |
2249 | ||
2250 | static void zread_interface_set_master(ZAPI_HANDLER_ARGS) | |
2251 | { | |
2252 | struct interface *master; | |
2253 | struct interface *slave; | |
2254 | struct stream *s = msg; | |
2255 | int ifindex; | |
2256 | vrf_id_t vrf_id; | |
2257 | ||
2258 | STREAM_GETL(s, vrf_id); | |
2259 | STREAM_GETL(s, ifindex); | |
2260 | master = if_lookup_by_index(ifindex, vrf_id); | |
2261 | ||
2262 | STREAM_GETL(s, vrf_id); | |
2263 | STREAM_GETL(s, ifindex); | |
2264 | slave = if_lookup_by_index(ifindex, vrf_id); | |
2265 | ||
2266 | if (!master || !slave) | |
2267 | return; | |
2268 | ||
2269 | kernel_interface_set_master(master, slave); | |
2270 | ||
2271 | stream_failure: | |
2272 | return; | |
2273 | } | |
2274 | ||
2275 | ||
2276 | static void zread_vrf_label(ZAPI_HANDLER_ARGS) | |
2277 | { | |
2278 | struct interface *ifp; | |
2279 | mpls_label_t nlabel; | |
2280 | afi_t afi; | |
2281 | struct stream *s; | |
2282 | struct zebra_vrf *def_zvrf; | |
2283 | enum lsp_types_t ltype; | |
2284 | ||
2285 | s = msg; | |
2286 | STREAM_GETL(s, nlabel); | |
2287 | STREAM_GETC(s, afi); | |
663d3a91 QY |
2288 | |
2289 | if (!(IS_VALID_AFI(afi))) { | |
2290 | zlog_warn("Invalid AFI for VRF label: %u", afi); | |
2291 | return; | |
2292 | } | |
2293 | ||
bf094f69 QY |
2294 | if (nlabel == zvrf->label[afi]) { |
2295 | /* | |
2296 | * Nothing to do here move along | |
2297 | */ | |
2298 | return; | |
2299 | } | |
2300 | ||
2301 | STREAM_GETC(s, ltype); | |
2302 | ||
2303 | if (zvrf->vrf->vrf_id != VRF_DEFAULT) | |
a36898e7 | 2304 | ifp = if_lookup_by_name(zvrf->vrf->name, zvrf->vrf->vrf_id); |
bf094f69 | 2305 | else |
a36898e7 | 2306 | ifp = if_lookup_by_name("lo", VRF_DEFAULT); |
bf094f69 QY |
2307 | |
2308 | if (!ifp) { | |
2309 | zlog_debug("Unable to find specified Interface for %s", | |
2310 | zvrf->vrf->name); | |
2311 | return; | |
2312 | } | |
2313 | ||
2314 | def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT); | |
2315 | ||
2316 | if (zvrf->label[afi] != MPLS_LABEL_NONE) { | |
2317 | afi_t scrubber; | |
2318 | bool really_remove; | |
2319 | ||
2320 | really_remove = true; | |
2321 | for (scrubber = AFI_IP; scrubber < AFI_MAX; scrubber++) { | |
2322 | if (scrubber == afi) | |
2323 | continue; | |
2324 | ||
2325 | if (zvrf->label[scrubber] == MPLS_LABEL_NONE) | |
2326 | continue; | |
2327 | ||
2328 | if (zvrf->label[afi] == zvrf->label[scrubber]) { | |
2329 | really_remove = false; | |
2330 | break; | |
2331 | } | |
2332 | } | |
2333 | ||
2334 | if (really_remove) | |
2335 | mpls_lsp_uninstall(def_zvrf, ltype, zvrf->label[afi], | |
2336 | NEXTHOP_TYPE_IFINDEX, NULL, | |
2337 | ifp->ifindex); | |
2338 | } | |
2339 | ||
5065db0a RW |
2340 | if (nlabel != MPLS_LABEL_NONE) { |
2341 | mpls_label_t out_label = MPLS_LABEL_IMPLICIT_NULL; | |
2342 | mpls_lsp_install(def_zvrf, ltype, nlabel, 1, &out_label, | |
2343 | NEXTHOP_TYPE_IFINDEX, NULL, ifp->ifindex); | |
2344 | } | |
bf094f69 QY |
2345 | |
2346 | zvrf->label[afi] = nlabel; | |
2347 | stream_failure: | |
2348 | return; | |
2349 | } | |
2350 | ||
2351 | static inline void zread_rule(ZAPI_HANDLER_ARGS) | |
2352 | { | |
2353 | struct zebra_pbr_rule zpr; | |
2354 | struct stream *s; | |
2355 | uint32_t total, i; | |
bf094f69 QY |
2356 | |
2357 | s = msg; | |
2358 | STREAM_GETL(s, total); | |
2359 | ||
2360 | for (i = 0; i < total; i++) { | |
2361 | memset(&zpr, 0, sizeof(zpr)); | |
2362 | ||
2363 | zpr.sock = client->sock; | |
2364 | zpr.rule.vrf_id = hdr->vrf_id; | |
2365 | STREAM_GETL(s, zpr.rule.seq); | |
2366 | STREAM_GETL(s, zpr.rule.priority); | |
2367 | STREAM_GETL(s, zpr.rule.unique); | |
2368 | STREAM_GETC(s, zpr.rule.filter.src_ip.family); | |
2369 | STREAM_GETC(s, zpr.rule.filter.src_ip.prefixlen); | |
2370 | STREAM_GET(&zpr.rule.filter.src_ip.u.prefix, s, | |
2371 | prefix_blen(&zpr.rule.filter.src_ip)); | |
2372 | STREAM_GETW(s, zpr.rule.filter.src_port); | |
2373 | STREAM_GETC(s, zpr.rule.filter.dst_ip.family); | |
2374 | STREAM_GETC(s, zpr.rule.filter.dst_ip.prefixlen); | |
2375 | STREAM_GET(&zpr.rule.filter.dst_ip.u.prefix, s, | |
2376 | prefix_blen(&zpr.rule.filter.dst_ip)); | |
2377 | STREAM_GETW(s, zpr.rule.filter.dst_port); | |
2378 | STREAM_GETL(s, zpr.rule.filter.fwmark); | |
2379 | STREAM_GETL(s, zpr.rule.action.table); | |
b77a69bd | 2380 | STREAM_GETL(s, zpr.rule.ifindex); |
bf094f69 | 2381 | |
b77a69bd | 2382 | if (zpr.rule.ifindex) { |
b19d55d0 SW |
2383 | struct interface *ifp; |
2384 | ||
2385 | ifp = if_lookup_by_index_per_ns(zvrf->zns, | |
2386 | zpr.rule.ifindex); | |
2387 | if (!ifp) { | |
bf094f69 | 2388 | zlog_debug("Failed to lookup ifindex: %u", |
b77a69bd | 2389 | zpr.rule.ifindex); |
bf094f69 QY |
2390 | return; |
2391 | } | |
b19d55d0 SW |
2392 | |
2393 | strlcpy(zpr.ifname, ifp->name, sizeof(zpr.ifname)); | |
bf094f69 QY |
2394 | } |
2395 | ||
2396 | if (!is_default_prefix(&zpr.rule.filter.src_ip)) | |
2397 | zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_IP; | |
2398 | ||
2399 | if (!is_default_prefix(&zpr.rule.filter.dst_ip)) | |
2400 | zpr.rule.filter.filter_bm |= PBR_FILTER_DST_IP; | |
2401 | ||
2402 | if (zpr.rule.filter.src_port) | |
2403 | zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_PORT; | |
2404 | ||
2405 | if (zpr.rule.filter.dst_port) | |
2406 | zpr.rule.filter.filter_bm |= PBR_FILTER_DST_PORT; | |
2407 | ||
2408 | if (zpr.rule.filter.fwmark) | |
2409 | zpr.rule.filter.filter_bm |= PBR_FILTER_FWMARK; | |
2410 | ||
4719fd76 QY |
2411 | if (!(zpr.rule.filter.src_ip.family == AF_INET |
2412 | || zpr.rule.filter.src_ip.family == AF_INET6)) { | |
cc815be7 QY |
2413 | zlog_warn( |
2414 | "Unsupported PBR source IP family: %s (%" PRIu8 | |
b7b05b46 | 2415 | ")", |
cc815be7 QY |
2416 | family2str(zpr.rule.filter.src_ip.family), |
2417 | zpr.rule.filter.src_ip.family); | |
4719fd76 QY |
2418 | return; |
2419 | } | |
2420 | if (!(zpr.rule.filter.dst_ip.family == AF_INET | |
2421 | || zpr.rule.filter.dst_ip.family == AF_INET6)) { | |
b7b05b46 | 2422 | zlog_warn("Unsupported PBR IP family: %s (%" PRIu8 ")", |
cc815be7 QY |
2423 | family2str(zpr.rule.filter.dst_ip.family), |
2424 | zpr.rule.filter.dst_ip.family); | |
4719fd76 QY |
2425 | return; |
2426 | } | |
2427 | ||
2428 | ||
7f0ea8a4 | 2429 | zpr.vrf_id = zvrf->vrf->vrf_id; |
bf094f69 | 2430 | if (hdr->command == ZEBRA_RULE_ADD) |
7f0ea8a4 | 2431 | zebra_pbr_add_rule(&zpr); |
bf094f69 | 2432 | else |
7f0ea8a4 | 2433 | zebra_pbr_del_rule(&zpr); |
bf094f69 QY |
2434 | } |
2435 | ||
2436 | stream_failure: | |
2437 | return; | |
2438 | } | |
2439 | ||
2440 | static inline void zread_ipset(ZAPI_HANDLER_ARGS) | |
2441 | { | |
2442 | struct zebra_pbr_ipset zpi; | |
2443 | struct stream *s; | |
2444 | uint32_t total, i; | |
2445 | ||
2446 | s = msg; | |
2447 | STREAM_GETL(s, total); | |
2448 | ||
2449 | for (i = 0; i < total; i++) { | |
2450 | memset(&zpi, 0, sizeof(zpi)); | |
2451 | ||
2452 | zpi.sock = client->sock; | |
be2028d1 | 2453 | zpi.vrf_id = zvrf->vrf->vrf_id; |
bf094f69 QY |
2454 | STREAM_GETL(s, zpi.unique); |
2455 | STREAM_GETL(s, zpi.type); | |
2456 | STREAM_GET(&zpi.ipset_name, s, ZEBRA_IPSET_NAME_SIZE); | |
2457 | ||
2458 | if (hdr->command == ZEBRA_IPSET_CREATE) | |
62f20a52 | 2459 | zebra_pbr_create_ipset(&zpi); |
bf094f69 | 2460 | else |
62f20a52 | 2461 | zebra_pbr_destroy_ipset(&zpi); |
bf094f69 QY |
2462 | } |
2463 | ||
2464 | stream_failure: | |
2465 | return; | |
2466 | } | |
2467 | ||
2468 | static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS) | |
2469 | { | |
2470 | struct zebra_pbr_ipset_entry zpi; | |
2471 | struct zebra_pbr_ipset ipset; | |
2472 | struct stream *s; | |
2473 | uint32_t total, i; | |
2474 | ||
2475 | s = msg; | |
2476 | STREAM_GETL(s, total); | |
2477 | ||
2478 | for (i = 0; i < total; i++) { | |
2479 | memset(&zpi, 0, sizeof(zpi)); | |
2480 | memset(&ipset, 0, sizeof(ipset)); | |
2481 | ||
2482 | zpi.sock = client->sock; | |
2483 | STREAM_GETL(s, zpi.unique); | |
2484 | STREAM_GET(&ipset.ipset_name, s, ZEBRA_IPSET_NAME_SIZE); | |
261462c3 | 2485 | ipset.ipset_name[ZEBRA_IPSET_NAME_SIZE - 1] = '\0'; |
bf094f69 QY |
2486 | STREAM_GETC(s, zpi.src.family); |
2487 | STREAM_GETC(s, zpi.src.prefixlen); | |
2488 | STREAM_GET(&zpi.src.u.prefix, s, prefix_blen(&zpi.src)); | |
2489 | STREAM_GETC(s, zpi.dst.family); | |
2490 | STREAM_GETC(s, zpi.dst.prefixlen); | |
2491 | STREAM_GET(&zpi.dst.u.prefix, s, prefix_blen(&zpi.dst)); | |
2492 | ||
25d760c5 PG |
2493 | STREAM_GETW(s, zpi.src_port_min); |
2494 | STREAM_GETW(s, zpi.src_port_max); | |
2495 | STREAM_GETW(s, zpi.dst_port_min); | |
2496 | STREAM_GETW(s, zpi.dst_port_max); | |
2497 | STREAM_GETC(s, zpi.proto); | |
bf094f69 QY |
2498 | if (!is_default_prefix(&zpi.src)) |
2499 | zpi.filter_bm |= PBR_FILTER_SRC_IP; | |
2500 | ||
2501 | if (!is_default_prefix(&zpi.dst)) | |
2502 | zpi.filter_bm |= PBR_FILTER_DST_IP; | |
be729dd7 | 2503 | if (zpi.dst_port_min != 0 || zpi.proto == IPPROTO_ICMP) |
25d760c5 | 2504 | zpi.filter_bm |= PBR_FILTER_DST_PORT; |
be729dd7 | 2505 | if (zpi.src_port_min != 0 || zpi.proto == IPPROTO_ICMP) |
25d760c5 PG |
2506 | zpi.filter_bm |= PBR_FILTER_SRC_PORT; |
2507 | if (zpi.dst_port_max != 0) | |
2508 | zpi.filter_bm |= PBR_FILTER_DST_PORT_RANGE; | |
2509 | if (zpi.src_port_max != 0) | |
2510 | zpi.filter_bm |= PBR_FILTER_SRC_PORT_RANGE; | |
2511 | if (zpi.proto != 0) | |
2512 | zpi.filter_bm |= PBR_FILTER_PROTO; | |
bf094f69 QY |
2513 | |
2514 | /* calculate backpointer */ | |
62f20a52 DS |
2515 | zpi.backpointer = |
2516 | zebra_pbr_lookup_ipset_pername(ipset.ipset_name); | |
f096bae4 DS |
2517 | |
2518 | if (!zpi.backpointer) { | |
2519 | zlog_warn("ipset name specified: %s does not exist", | |
2520 | ipset.ipset_name); | |
2521 | goto stream_failure; | |
2522 | } | |
2523 | ||
bf094f69 | 2524 | if (hdr->command == ZEBRA_IPSET_ENTRY_ADD) |
62f20a52 | 2525 | zebra_pbr_add_ipset_entry(&zpi); |
bf094f69 | 2526 | else |
62f20a52 | 2527 | zebra_pbr_del_ipset_entry(&zpi); |
bf094f69 QY |
2528 | } |
2529 | ||
2530 | stream_failure: | |
2531 | return; | |
2532 | } | |
2533 | ||
2534 | static inline void zread_iptable(ZAPI_HANDLER_ARGS) | |
2535 | { | |
8b5c4dce QY |
2536 | struct zebra_pbr_iptable *zpi = |
2537 | XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_iptable)); | |
bf094f69 QY |
2538 | struct stream *s; |
2539 | ||
2540 | s = msg; | |
2541 | ||
8b5c4dce QY |
2542 | zpi->interface_name_list = list_new(); |
2543 | zpi->sock = client->sock; | |
2544 | zpi->vrf_id = zvrf->vrf->vrf_id; | |
2545 | STREAM_GETL(s, zpi->unique); | |
2546 | STREAM_GETL(s, zpi->type); | |
2547 | STREAM_GETL(s, zpi->filter_bm); | |
2548 | STREAM_GETL(s, zpi->action); | |
2549 | STREAM_GETL(s, zpi->fwmark); | |
2550 | STREAM_GET(&zpi->ipset_name, s, ZEBRA_IPSET_NAME_SIZE); | |
2551 | STREAM_GETW(s, zpi->pkt_len_min); | |
2552 | STREAM_GETW(s, zpi->pkt_len_max); | |
2553 | STREAM_GETW(s, zpi->tcp_flags); | |
2554 | STREAM_GETW(s, zpi->tcp_mask_flags); | |
2555 | STREAM_GETC(s, zpi->dscp_value); | |
2556 | STREAM_GETC(s, zpi->fragment); | |
2557 | STREAM_GETC(s, zpi->protocol); | |
2558 | STREAM_GETL(s, zpi->nb_interface); | |
2559 | zebra_pbr_iptable_update_interfacelist(s, zpi); | |
bf094f69 QY |
2560 | |
2561 | if (hdr->command == ZEBRA_IPTABLE_ADD) | |
8b5c4dce | 2562 | zebra_pbr_add_iptable(zpi); |
bf094f69 | 2563 | else |
8b5c4dce QY |
2564 | zebra_pbr_del_iptable(zpi); |
2565 | ||
bf094f69 | 2566 | stream_failure: |
8b5c4dce QY |
2567 | zebra_pbr_iptable_free(zpi); |
2568 | zpi = NULL; | |
bf094f69 QY |
2569 | return; |
2570 | } | |
2571 | ||
9ab0b2a3 SW |
2572 | static void zsend_error_msg(struct zserv *client, enum zebra_error_types error, |
2573 | struct zmsghdr *bad_hdr) | |
2574 | { | |
2575 | ||
2576 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
2577 | ||
2578 | zclient_create_header(s, ZEBRA_ERROR, bad_hdr->vrf_id); | |
2579 | ||
2580 | zserv_encode_error(s, error); | |
2581 | ||
2582 | client->error_cnt++; | |
2583 | zserv_send_message(client, s); | |
2584 | } | |
2585 | ||
2586 | static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS) | |
2587 | { | |
2588 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) | |
2589 | zlog_debug("ZAPI message specifies unknown VRF: %d", | |
2590 | hdr->vrf_id); | |
2591 | ||
5ee080f0 | 2592 | zsend_error_msg(client, ZEBRA_NO_VRF, hdr); |
9ab0b2a3 SW |
2593 | } |
2594 | ||
2595 | static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS) | |
2596 | { | |
2597 | zlog_info("Zebra received unknown command %d", hdr->command); | |
2598 | ||
5ee080f0 | 2599 | zsend_error_msg(client, ZEBRA_INVALID_MSG_TYPE, hdr); |
9ab0b2a3 SW |
2600 | } |
2601 | ||
2b64873d | 2602 | void (*const zserv_handlers[])(ZAPI_HANDLER_ARGS) = { |
bf094f69 QY |
2603 | [ZEBRA_ROUTER_ID_ADD] = zread_router_id_add, |
2604 | [ZEBRA_ROUTER_ID_DELETE] = zread_router_id_delete, | |
2605 | [ZEBRA_INTERFACE_ADD] = zread_interface_add, | |
2606 | [ZEBRA_INTERFACE_DELETE] = zread_interface_delete, | |
c3bd894e | 2607 | [ZEBRA_INTERFACE_SET_PROTODOWN] = zread_interface_set_protodown, |
bf094f69 QY |
2608 | [ZEBRA_ROUTE_ADD] = zread_route_add, |
2609 | [ZEBRA_ROUTE_DELETE] = zread_route_del, | |
bf094f69 QY |
2610 | [ZEBRA_REDISTRIBUTE_ADD] = zebra_redistribute_add, |
2611 | [ZEBRA_REDISTRIBUTE_DELETE] = zebra_redistribute_delete, | |
2612 | [ZEBRA_REDISTRIBUTE_DEFAULT_ADD] = zebra_redistribute_default_add, | |
2613 | [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE] = zebra_redistribute_default_delete, | |
2614 | [ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB] = zread_ipv4_nexthop_lookup_mrib, | |
2615 | [ZEBRA_HELLO] = zread_hello, | |
2616 | [ZEBRA_NEXTHOP_REGISTER] = zread_rnh_register, | |
2617 | [ZEBRA_NEXTHOP_UNREGISTER] = zread_rnh_unregister, | |
2618 | [ZEBRA_IMPORT_ROUTE_REGISTER] = zread_rnh_register, | |
2619 | [ZEBRA_IMPORT_ROUTE_UNREGISTER] = zread_rnh_unregister, | |
2620 | [ZEBRA_BFD_DEST_UPDATE] = zebra_ptm_bfd_dst_register, | |
2621 | [ZEBRA_BFD_DEST_REGISTER] = zebra_ptm_bfd_dst_register, | |
2622 | [ZEBRA_BFD_DEST_DEREGISTER] = zebra_ptm_bfd_dst_deregister, | |
d3af6147 RZ |
2623 | #if HAVE_BFDD > 0 |
2624 | [ZEBRA_BFD_DEST_REPLAY] = zebra_ptm_bfd_dst_replay, | |
2625 | #endif /* HAVE_BFDD */ | |
bf094f69 QY |
2626 | [ZEBRA_VRF_UNREGISTER] = zread_vrf_unregister, |
2627 | [ZEBRA_VRF_LABEL] = zread_vrf_label, | |
2628 | [ZEBRA_BFD_CLIENT_REGISTER] = zebra_ptm_bfd_client_register, | |
2629 | #if defined(HAVE_RTADV) | |
2630 | [ZEBRA_INTERFACE_ENABLE_RADV] = zebra_interface_radv_enable, | |
2631 | [ZEBRA_INTERFACE_DISABLE_RADV] = zebra_interface_radv_disable, | |
2632 | #else | |
2633 | [ZEBRA_INTERFACE_ENABLE_RADV] = NULL, | |
2634 | [ZEBRA_INTERFACE_DISABLE_RADV] = NULL, | |
2635 | #endif | |
ea6b290b RW |
2636 | [ZEBRA_MPLS_LABELS_ADD] = zread_mpls_labels_add, |
2637 | [ZEBRA_MPLS_LABELS_DELETE] = zread_mpls_labels_delete, | |
2638 | [ZEBRA_MPLS_LABELS_REPLACE] = zread_mpls_labels_replace, | |
bf094f69 QY |
2639 | [ZEBRA_IPMR_ROUTE_STATS] = zebra_ipmr_route_stats, |
2640 | [ZEBRA_LABEL_MANAGER_CONNECT] = zread_label_manager_request, | |
f533be73 | 2641 | [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC] = zread_label_manager_request, |
bf094f69 QY |
2642 | [ZEBRA_GET_LABEL_CHUNK] = zread_label_manager_request, |
2643 | [ZEBRA_RELEASE_LABEL_CHUNK] = zread_label_manager_request, | |
2644 | [ZEBRA_FEC_REGISTER] = zread_fec_register, | |
2645 | [ZEBRA_FEC_UNREGISTER] = zread_fec_unregister, | |
2646 | [ZEBRA_ADVERTISE_DEFAULT_GW] = zebra_vxlan_advertise_gw_macip, | |
278e26de | 2647 | [ZEBRA_ADVERTISE_SVI_MACIP] = zebra_vxlan_advertise_svi_macip, |
bf094f69 QY |
2648 | [ZEBRA_ADVERTISE_SUBNET] = zebra_vxlan_advertise_subnet, |
2649 | [ZEBRA_ADVERTISE_ALL_VNI] = zebra_vxlan_advertise_all_vni, | |
2650 | [ZEBRA_REMOTE_VTEP_ADD] = zebra_vxlan_remote_vtep_add, | |
2651 | [ZEBRA_REMOTE_VTEP_DEL] = zebra_vxlan_remote_vtep_del, | |
2652 | [ZEBRA_REMOTE_MACIP_ADD] = zebra_vxlan_remote_macip_add, | |
2653 | [ZEBRA_REMOTE_MACIP_DEL] = zebra_vxlan_remote_macip_del, | |
3950b52c | 2654 | [ZEBRA_DUPLICATE_ADDR_DETECTION] = zebra_vxlan_dup_addr_detection, |
bf094f69 QY |
2655 | [ZEBRA_INTERFACE_SET_MASTER] = zread_interface_set_master, |
2656 | [ZEBRA_PW_ADD] = zread_pseudowire, | |
2657 | [ZEBRA_PW_DELETE] = zread_pseudowire, | |
2658 | [ZEBRA_PW_SET] = zread_pseudowire, | |
2659 | [ZEBRA_PW_UNSET] = zread_pseudowire, | |
2660 | [ZEBRA_RULE_ADD] = zread_rule, | |
2661 | [ZEBRA_RULE_DELETE] = zread_rule, | |
2662 | [ZEBRA_TABLE_MANAGER_CONNECT] = zread_table_manager_request, | |
2663 | [ZEBRA_GET_TABLE_CHUNK] = zread_table_manager_request, | |
2664 | [ZEBRA_RELEASE_TABLE_CHUNK] = zread_table_manager_request, | |
2665 | [ZEBRA_IPSET_CREATE] = zread_ipset, | |
2666 | [ZEBRA_IPSET_DESTROY] = zread_ipset, | |
2667 | [ZEBRA_IPSET_ENTRY_ADD] = zread_ipset_entry, | |
2668 | [ZEBRA_IPSET_ENTRY_DELETE] = zread_ipset_entry, | |
2669 | [ZEBRA_IPTABLE_ADD] = zread_iptable, | |
2670 | [ZEBRA_IPTABLE_DELETE] = zread_iptable, | |
fbac9605 | 2671 | [ZEBRA_VXLAN_FLOOD_CONTROL] = zebra_vxlan_flood_control, |
ecbbc3a7 | 2672 | [ZEBRA_VXLAN_SG_REPLAY] = zebra_vxlan_sg_replay, |
ee235396 SK |
2673 | [ZEBRA_MLAG_CLIENT_REGISTER] = zebra_mlag_client_register, |
2674 | [ZEBRA_MLAG_CLIENT_UNREGISTER] = zebra_mlag_client_unregister, | |
2675 | [ZEBRA_MLAG_FORWARD_MSG] = zebra_mlag_forward_client_msg, | |
b9e6727a | 2676 | [ZEBRA_CLIENT_CAPABILITIES] = zread_client_capabilities |
bf094f69 QY |
2677 | }; |
2678 | ||
727c9b99 QY |
2679 | #if defined(HANDLE_ZAPI_FUZZING) |
2680 | extern struct zebra_privs_t zserv_privs; | |
2681 | ||
2682 | static void zserv_write_incoming(struct stream *orig, uint16_t command) | |
2683 | { | |
2684 | char fname[MAXPATHLEN]; | |
2685 | struct stream *copy; | |
2686 | int fd = -1; | |
2687 | ||
2688 | copy = stream_dup(orig); | |
2689 | stream_set_getp(copy, 0); | |
2690 | ||
3c649c71 | 2691 | snprintf(fname, MAXPATHLEN, "%s/%u", frr_vtydir, command); |
6bb30c2c | 2692 | |
0cf6db21 | 2693 | frr_with_privs(&zserv_privs) { |
6bb30c2c DL |
2694 | fd = open(fname, O_CREAT | O_WRONLY | O_EXCL, 0644); |
2695 | } | |
727c9b99 QY |
2696 | stream_flush(copy, fd); |
2697 | close(fd); | |
727c9b99 QY |
2698 | stream_free(copy); |
2699 | } | |
2700 | #endif | |
2701 | ||
904e0d88 | 2702 | void zserv_handle_commands(struct zserv *client, struct stream *msg) |
bf094f69 | 2703 | { |
904e0d88 QY |
2704 | struct zmsghdr hdr; |
2705 | struct zebra_vrf *zvrf; | |
2706 | ||
aa8cb964 QY |
2707 | if (STREAM_READABLE(msg) > ZEBRA_MAX_PACKET_SIZ) { |
2708 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) | |
2709 | zlog_debug( | |
2710 | "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping", | |
2711 | STREAM_READABLE(msg), ZEBRA_MAX_PACKET_SIZ); | |
2712 | return; | |
2713 | } | |
2714 | ||
904e0d88 | 2715 | zapi_parse_header(msg, &hdr); |
bf094f69 | 2716 | |
49b3b01f QY |
2717 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
2718 | zserv_log_message(NULL, msg, &hdr); | |
2719 | ||
727c9b99 QY |
2720 | #if defined(HANDLE_ZAPI_FUZZING) |
2721 | zserv_write_incoming(msg, hdr.command); | |
2722 | #endif | |
2723 | ||
904e0d88 QY |
2724 | hdr.length -= ZEBRA_HEADER_SIZE; |
2725 | ||
2726 | /* lookup vrf */ | |
2727 | zvrf = zebra_vrf_lookup_by_id(hdr.vrf_id); | |
9ab0b2a3 SW |
2728 | if (!zvrf) |
2729 | return zserv_error_no_vrf(client, &hdr, msg, zvrf); | |
904e0d88 | 2730 | |
aa360de7 | 2731 | if (hdr.command >= array_size(zserv_handlers) |
904e0d88 | 2732 | || zserv_handlers[hdr.command] == NULL) |
9ab0b2a3 SW |
2733 | return zserv_error_invalid_msg_type(client, &hdr, msg, zvrf); |
2734 | ||
2735 | zserv_handlers[hdr.command](client, &hdr, msg, zvrf); | |
bf094f69 | 2736 | } |