]>
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" | |
bf094f69 QY |
27 | #include "lib/stream.h" |
28 | #include "lib/memory.h" | |
29 | #include "lib/table.h" | |
30 | #include "lib/network.h" | |
bf094f69 QY |
31 | #include "lib/log.h" |
32 | #include "lib/zclient.h" | |
33 | #include "lib/privs.h" | |
bf094f69 QY |
34 | #include "lib/nexthop.h" |
35 | #include "lib/vrf.h" | |
36 | #include "lib/libfrr.h" | |
5e7939a5 | 37 | #include "lib/lib_errors.h" |
bf094f69 | 38 | |
161e9ab7 | 39 | #include "zebra/zebra_router.h" |
bf094f69 | 40 | #include "zebra/rib.h" |
bf094f69 QY |
41 | #include "zebra/zebra_ns.h" |
42 | #include "zebra/zebra_vrf.h" | |
43 | #include "zebra/router-id.h" | |
44 | #include "zebra/redistribute.h" | |
45 | #include "zebra/debug.h" | |
46 | #include "zebra/zebra_rnh.h" | |
bf094f69 QY |
47 | #include "zebra/interface.h" |
48 | #include "zebra/zebra_ptm.h" | |
49 | #include "zebra/rtadv.h" | |
50 | #include "zebra/zebra_mpls.h" | |
51 | #include "zebra/zebra_mroute.h" | |
bf094f69 | 52 | #include "zebra/zebra_vxlan.h" |
ce5160c0 | 53 | #include "zebra/zebra_evpn_mh.h" |
bf094f69 QY |
54 | #include "zebra/rt.h" |
55 | #include "zebra/zebra_pbr.h" | |
56 | #include "zebra/table_manager.h" | |
57 | #include "zebra/zapi_msg.h" | |
364fed6b | 58 | #include "zebra/zebra_errors.h" |
02c0866d | 59 | #include "zebra/zebra_mlag.h" |
54bea4e5 | 60 | #include "zebra/connected.h" |
79b3664a | 61 | #include "zebra/zebra_opaque.h" |
31f937fb | 62 | #include "zebra/zebra_srte.h" |
6e68a084 | 63 | #include "zebra/zebra_srv6.h" |
bf094f69 | 64 | |
b86c1f4f | 65 | DEFINE_MTYPE_STATIC(ZEBRA, RE_OPAQUE, "Route Opaque Data"); |
224ccf29 | 66 | |
5898ce6f MS |
67 | static int zapi_nhg_decode(struct stream *s, int cmd, struct zapi_nhg *api_nhg); |
68 | ||
bf094f69 QY |
69 | /* Encoding helpers -------------------------------------------------------- */ |
70 | ||
71 | static void zserv_encode_interface(struct stream *s, struct interface *ifp) | |
72 | { | |
73 | /* Interface information. */ | |
53e60e5c | 74 | struct zebra_if *zif = ifp->info; |
19c38250 | 75 | |
bf094f69 QY |
76 | stream_put(s, ifp->name, INTERFACE_NAMSIZ); |
77 | stream_putl(s, ifp->ifindex); | |
78 | stream_putc(s, ifp->status); | |
79 | stream_putq(s, ifp->flags); | |
80 | stream_putc(s, ifp->ptm_enable); | |
81 | stream_putc(s, ifp->ptm_status); | |
82 | stream_putl(s, ifp->metric); | |
83 | stream_putl(s, ifp->speed); | |
84 | stream_putl(s, ifp->mtu); | |
85 | stream_putl(s, ifp->mtu6); | |
86 | stream_putl(s, ifp->bandwidth); | |
53e60e5c | 87 | stream_putl(s, zif->link_ifindex); |
bf094f69 QY |
88 | stream_putl(s, ifp->ll_type); |
89 | stream_putl(s, ifp->hw_addr_len); | |
90 | if (ifp->hw_addr_len) | |
91 | stream_put(s, ifp->hw_addr, ifp->hw_addr_len); | |
92 | ||
93 | /* Then, Traffic Engineering parameters if any */ | |
94 | if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params)) { | |
95 | stream_putc(s, 1); | |
96 | zebra_interface_link_params_write(s, ifp); | |
97 | } else | |
98 | stream_putc(s, 0); | |
99 | ||
100 | /* Write packet size. */ | |
101 | stream_putw_at(s, 0, stream_get_endp(s)); | |
102 | } | |
103 | ||
104 | static void zserv_encode_vrf(struct stream *s, struct zebra_vrf *zvrf) | |
105 | { | |
106 | struct vrf_data data; | |
107 | const char *netns_name = zvrf_ns_name(zvrf); | |
108 | ||
a013777a | 109 | memset(&data, 0, sizeof(data)); |
bf094f69 QY |
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 | ||
096f7609 | 188 | zclient_create_header(s, ZEBRA_INTERFACE_ADD, ifp->vrf->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 | ||
096f7609 | 200 | zclient_create_header(s, ZEBRA_INTERFACE_DELETE, ifp->vrf->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 | ||
096f7609 | 235 | zclient_create_header(s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf->vrf_id); |
bf094f69 QY |
236 | |
237 | /* Add Interface Index */ | |
238 | stream_putl(s, ifp->ifindex); | |
239 | ||
240 | /* Then TE Link Parameters */ | |
241 | if (zebra_interface_link_params_write(s, ifp) == 0) { | |
242 | stream_free(s); | |
243 | return 0; | |
244 | } | |
245 | ||
246 | /* Write packet size. */ | |
247 | stream_putw_at(s, 0, stream_get_endp(s)); | |
248 | ||
21ccc0cf | 249 | return zserv_send_message(client, s); |
bf094f69 QY |
250 | } |
251 | ||
252 | /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or | |
253 | * ZEBRA_INTERFACE_ADDRESS_DELETE to the client. | |
254 | * | |
255 | * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations: | |
256 | * - in response to a 3-byte ZEBRA_INTERFACE_ADD request | |
257 | * from the client, after the ZEBRA_INTERFACE_ADD has been | |
258 | * sent from zebra to the client | |
259 | * - redistribute new address info to all clients in the following situations | |
260 | * - at startup, when zebra figures out the available interfaces | |
261 | * - when an interface is added (where support for | |
262 | * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when | |
263 | * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is | |
264 | * received) | |
b1bd1015 | 265 | * - for the vty commands "ip address A.B.C.D/M [<label LINE>]" |
bf094f69 QY |
266 | * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M" |
267 | * - when an RTM_NEWADDR message is received from the kernel, | |
268 | * | |
269 | * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE: | |
270 | * | |
271 | * zsend_interface_address(DELETE) | |
272 | * ^ | |
273 | * | | |
274 | * zebra_interface_address_delete_update | |
275 | * ^ ^ ^ | |
276 | * | | if_delete_update | |
277 | * | | | |
278 | * ip_address_uninstall connected_delete_ipv4 | |
279 | * [ipv6_addresss_uninstall] [connected_delete_ipv6] | |
280 | * ^ ^ | |
281 | * | | | |
282 | * | RTM_NEWADDR on routing/netlink socket | |
283 | * | | |
284 | * vty commands: | |
285 | * "no ip address A.B.C.D/M [label LINE]" | |
b1bd1015 | 286 | * "no ip address A.B.C.D/M" |
bf094f69 QY |
287 | * ["no ipv6 address X:X::X:X/M"] |
288 | * | |
289 | */ | |
290 | int zsend_interface_address(int cmd, struct zserv *client, | |
291 | struct interface *ifp, struct connected *ifc) | |
292 | { | |
293 | int blen; | |
294 | struct prefix *p; | |
295 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
296 | ||
096f7609 | 297 | zclient_create_header(s, cmd, ifp->vrf->vrf_id); |
bf094f69 QY |
298 | stream_putl(s, ifp->ifindex); |
299 | ||
300 | /* Interface address flag. */ | |
301 | stream_putc(s, ifc->flags); | |
302 | ||
303 | /* Prefix information. */ | |
304 | p = ifc->address; | |
305 | stream_putc(s, p->family); | |
306 | blen = prefix_blen(p); | |
307 | stream_put(s, &p->u.prefix, blen); | |
308 | ||
309 | /* | |
310 | * XXX gnu version does not send prefixlen for | |
311 | * ZEBRA_INTERFACE_ADDRESS_DELETE | |
312 | * but zebra_interface_address_delete_read() in the gnu version | |
313 | * expects to find it | |
314 | */ | |
315 | stream_putc(s, p->prefixlen); | |
316 | ||
317 | /* Destination. */ | |
318 | p = ifc->destination; | |
319 | if (p) | |
320 | stream_put(s, &p->u.prefix, blen); | |
321 | else | |
322 | stream_put(s, NULL, blen); | |
323 | ||
324 | /* Write packet size. */ | |
325 | stream_putw_at(s, 0, stream_get_endp(s)); | |
326 | ||
327 | client->connected_rt_add_cnt++; | |
21ccc0cf | 328 | return zserv_send_message(client, s); |
bf094f69 QY |
329 | } |
330 | ||
331 | static int zsend_interface_nbr_address(int cmd, struct zserv *client, | |
332 | struct interface *ifp, | |
333 | struct nbr_connected *ifc) | |
334 | { | |
335 | int blen; | |
336 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
337 | struct prefix *p; | |
338 | ||
096f7609 | 339 | zclient_create_header(s, cmd, ifp->vrf->vrf_id); |
bf094f69 QY |
340 | stream_putl(s, ifp->ifindex); |
341 | ||
342 | /* Prefix information. */ | |
343 | p = ifc->address; | |
344 | stream_putc(s, p->family); | |
345 | blen = prefix_blen(p); | |
346 | stream_put(s, &p->u.prefix, blen); | |
347 | ||
348 | /* | |
349 | * XXX gnu version does not send prefixlen for | |
350 | * ZEBRA_INTERFACE_ADDRESS_DELETE | |
351 | * but zebra_interface_address_delete_read() in the gnu version | |
352 | * expects to find it | |
353 | */ | |
354 | stream_putc(s, p->prefixlen); | |
355 | ||
356 | /* Write packet size. */ | |
357 | stream_putw_at(s, 0, stream_get_endp(s)); | |
358 | ||
21ccc0cf | 359 | return zserv_send_message(client, s); |
bf094f69 QY |
360 | } |
361 | ||
362 | /* Interface address addition. */ | |
363 | static void zebra_interface_nbr_address_add_update(struct interface *ifp, | |
364 | struct nbr_connected *ifc) | |
365 | { | |
366 | struct listnode *node, *nnode; | |
367 | struct zserv *client; | |
368 | struct prefix *p; | |
369 | ||
370 | if (IS_ZEBRA_DEBUG_EVENT) { | |
371 | char buf[INET6_ADDRSTRLEN]; | |
372 | ||
373 | p = ifc->address; | |
374 | zlog_debug( | |
375 | "MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s", | |
376 | inet_ntop(p->family, &p->u.prefix, buf, | |
377 | INET6_ADDRSTRLEN), | |
378 | p->prefixlen, ifc->ifp->name); | |
379 | } | |
380 | ||
17da84a4 KS |
381 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) { |
382 | /* Do not send unsolicited messages to synchronous clients. */ | |
383 | if (client->synchronous) | |
384 | continue; | |
385 | ||
bf094f69 QY |
386 | zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD, |
387 | client, ifp, ifc); | |
17da84a4 | 388 | } |
bf094f69 QY |
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 | ||
17da84a4 KS |
410 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) { |
411 | /* Do not send unsolicited messages to synchronous clients. */ | |
412 | if (client->synchronous) | |
413 | continue; | |
414 | ||
bf094f69 QY |
415 | zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE, |
416 | client, ifp, ifc); | |
17da84a4 | 417 | } |
bf094f69 QY |
418 | } |
419 | ||
420 | /* Send addresses on interface to client */ | |
421 | int zsend_interface_addresses(struct zserv *client, struct interface *ifp) | |
422 | { | |
423 | struct listnode *cnode, *cnnode; | |
424 | struct connected *c; | |
425 | struct nbr_connected *nc; | |
426 | ||
427 | /* Send interface addresses. */ | |
428 | for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) { | |
429 | if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL)) | |
430 | continue; | |
431 | ||
432 | if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD, client, | |
433 | ifp, c) | |
434 | < 0) | |
435 | return -1; | |
436 | } | |
437 | ||
438 | /* Send interface neighbors. */ | |
439 | for (ALL_LIST_ELEMENTS(ifp->nbr_connected, cnode, cnnode, nc)) { | |
440 | if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD, | |
441 | client, ifp, nc) | |
442 | < 0) | |
443 | return -1; | |
444 | } | |
445 | ||
446 | return 0; | |
447 | } | |
448 | ||
449 | /* Notify client about interface moving from one VRF to another. | |
450 | * Whether client is interested in old and new VRF is checked by caller. | |
451 | */ | |
452 | int zsend_interface_vrf_update(struct zserv *client, struct interface *ifp, | |
453 | vrf_id_t vrf_id) | |
454 | { | |
455 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
456 | ||
096f7609 | 457 | zclient_create_header(s, ZEBRA_INTERFACE_VRF_UPDATE, ifp->vrf->vrf_id); |
bf094f69 | 458 | |
91d227b7 RW |
459 | /* Fill in the name of the interface and its new VRF (id) */ |
460 | stream_put(s, ifp->name, INTERFACE_NAMSIZ); | |
bf094f69 QY |
461 | stream_putl(s, vrf_id); |
462 | ||
463 | /* Write packet size. */ | |
464 | stream_putw_at(s, 0, stream_get_endp(s)); | |
465 | ||
466 | client->if_vrfchg_cnt++; | |
21ccc0cf | 467 | return zserv_send_message(client, s); |
bf094f69 QY |
468 | } |
469 | ||
470 | /* Add new nbr connected IPv6 address */ | |
471 | void nbr_connected_add_ipv6(struct interface *ifp, struct in6_addr *address) | |
472 | { | |
473 | struct nbr_connected *ifc; | |
474 | struct prefix p; | |
475 | ||
476 | p.family = AF_INET6; | |
a85297a7 | 477 | IPV6_ADDR_COPY(&p.u.prefix6, address); |
f4d81e55 | 478 | p.prefixlen = IPV6_MAX_BITLEN; |
bf094f69 | 479 | |
8b1766b1 QY |
480 | ifc = listnode_head(ifp->nbr_connected); |
481 | if (!ifc) { | |
bf094f69 QY |
482 | /* new addition */ |
483 | ifc = nbr_connected_new(); | |
484 | ifc->address = prefix_new(); | |
485 | ifc->ifp = ifp; | |
486 | listnode_add(ifp->nbr_connected, ifc); | |
487 | } | |
488 | ||
489 | prefix_copy(ifc->address, &p); | |
490 | ||
491 | zebra_interface_nbr_address_add_update(ifp, ifc); | |
492 | ||
493 | if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 1); | |
494 | } | |
495 | ||
496 | void nbr_connected_delete_ipv6(struct interface *ifp, struct in6_addr *address) | |
497 | { | |
498 | struct nbr_connected *ifc; | |
499 | struct prefix p; | |
500 | ||
501 | p.family = AF_INET6; | |
a85297a7 | 502 | IPV6_ADDR_COPY(&p.u.prefix6, address); |
f4d81e55 | 503 | p.prefixlen = IPV6_MAX_BITLEN; |
bf094f69 QY |
504 | |
505 | ifc = nbr_connected_check(ifp, &p); | |
506 | if (!ifc) | |
507 | return; | |
508 | ||
509 | listnode_delete(ifp->nbr_connected, ifc); | |
510 | ||
511 | zebra_interface_nbr_address_delete_update(ifp, ifc); | |
512 | ||
513 | if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 0); | |
514 | ||
515 | nbr_connected_free(ifc); | |
516 | } | |
517 | ||
518 | /* | |
519 | * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or | |
520 | * ZEBRA_INTERFACE_DOWN. | |
521 | * | |
522 | * The ZEBRA_INTERFACE_UP message is sent from the zebra server to | |
523 | * the clients in one of 2 situations: | |
524 | * - an if_up is detected e.g., as a result of an RTM_IFINFO message | |
525 | * - a vty command modifying the bandwidth of an interface is received. | |
526 | * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected. | |
527 | */ | |
528 | int zsend_interface_update(int cmd, struct zserv *client, struct interface *ifp) | |
529 | { | |
530 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
531 | ||
096f7609 | 532 | zclient_create_header(s, cmd, ifp->vrf->vrf_id); |
bf094f69 QY |
533 | zserv_encode_interface(s, ifp); |
534 | ||
535 | if (cmd == ZEBRA_INTERFACE_UP) | |
536 | client->ifup_cnt++; | |
537 | else | |
538 | client->ifdown_cnt++; | |
539 | ||
21ccc0cf | 540 | return zserv_send_message(client, s); |
bf094f69 QY |
541 | } |
542 | ||
86391e56 | 543 | int zsend_redistribute_route(int cmd, struct zserv *client, |
84faa420 | 544 | const struct route_node *rn, |
40f321c0 | 545 | const struct route_entry *re) |
bf094f69 QY |
546 | { |
547 | struct zapi_route api; | |
548 | struct zapi_nexthop *api_nh; | |
549 | struct nexthop *nexthop; | |
84faa420 | 550 | const struct prefix *p, *src_p; |
9a0d4dd3 | 551 | uint8_t count = 0; |
34fa0870 | 552 | afi_t afi; |
f3f45626 DS |
553 | size_t stream_size = |
554 | MAX(ZEBRA_MAX_PACKET_SIZ, sizeof(struct zapi_route)); | |
bf094f69 | 555 | |
84faa420 | 556 | srcdest_rnode_prefixes(rn, &p, &src_p); |
bf094f69 QY |
557 | memset(&api, 0, sizeof(api)); |
558 | api.vrf_id = re->vrf_id; | |
559 | api.type = re->type; | |
e4081c0e | 560 | api.safi = SAFI_UNICAST; |
bf094f69 QY |
561 | api.instance = re->instance; |
562 | api.flags = re->flags; | |
563 | ||
34fa0870 DS |
564 | afi = family2afi(p->family); |
565 | switch (afi) { | |
566 | case AFI_IP: | |
567 | if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) | |
568 | client->redist_v4_add_cnt++; | |
569 | else | |
570 | client->redist_v4_del_cnt++; | |
571 | break; | |
572 | case AFI_IP6: | |
573 | if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) | |
574 | client->redist_v6_add_cnt++; | |
575 | else | |
576 | client->redist_v6_del_cnt++; | |
577 | break; | |
578 | default: | |
579 | break; | |
580 | } | |
581 | ||
bf094f69 QY |
582 | /* Prefix. */ |
583 | api.prefix = *p; | |
584 | if (src_p) { | |
585 | SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX); | |
586 | memcpy(&api.src_prefix, src_p, sizeof(api.src_prefix)); | |
587 | } | |
588 | ||
c415d895 | 589 | for (nexthop = re->nhe->nhg.nexthop; |
0eb97b86 | 590 | nexthop; nexthop = nexthop->next) { |
bf094f69 QY |
591 | if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
592 | continue; | |
593 | ||
594 | api_nh = &api.nexthops[count]; | |
595 | api_nh->vrf_id = nexthop->vrf_id; | |
596 | api_nh->type = nexthop->type; | |
bd054c1a | 597 | api_nh->weight = nexthop->weight; |
bf094f69 QY |
598 | switch (nexthop->type) { |
599 | case NEXTHOP_TYPE_BLACKHOLE: | |
600 | api_nh->bh_type = nexthop->bh_type; | |
601 | break; | |
602 | case NEXTHOP_TYPE_IPV4: | |
bf094f69 QY |
603 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
604 | api_nh->gate.ipv4 = nexthop->gate.ipv4; | |
605 | api_nh->ifindex = nexthop->ifindex; | |
606 | break; | |
607 | case NEXTHOP_TYPE_IFINDEX: | |
608 | api_nh->ifindex = nexthop->ifindex; | |
609 | break; | |
610 | case NEXTHOP_TYPE_IPV6: | |
bf094f69 QY |
611 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
612 | api_nh->gate.ipv6 = nexthop->gate.ipv6; | |
613 | api_nh->ifindex = nexthop->ifindex; | |
614 | } | |
615 | count++; | |
616 | } | |
617 | ||
9a0d4dd3 DS |
618 | /* Nexthops. */ |
619 | if (count) { | |
620 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); | |
621 | api.nexthop_num = count; | |
622 | } | |
623 | ||
bf094f69 QY |
624 | /* Attributes. */ |
625 | SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE); | |
626 | api.distance = re->distance; | |
627 | SET_FLAG(api.message, ZAPI_MESSAGE_METRIC); | |
628 | api.metric = re->metric; | |
629 | if (re->tag) { | |
630 | SET_FLAG(api.message, ZAPI_MESSAGE_TAG); | |
631 | api.tag = re->tag; | |
632 | } | |
633 | SET_FLAG(api.message, ZAPI_MESSAGE_MTU); | |
634 | api.mtu = re->mtu; | |
635 | ||
f3f45626 | 636 | struct stream *s = stream_new(stream_size); |
bf094f69 QY |
637 | |
638 | /* Encode route and send. */ | |
639 | if (zapi_route_encode(cmd, s, &api) < 0) { | |
640 | stream_free(s); | |
641 | return -1; | |
642 | } | |
643 | ||
2dbe669b DA |
644 | if (IS_ZEBRA_DEBUG_SEND) |
645 | zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %pFX", | |
bf094f69 QY |
646 | __func__, zserv_command_string(cmd), |
647 | zebra_route_string(client->proto), | |
648 | zebra_route_string(api.type), api.vrf_id, | |
2dbe669b | 649 | &api.prefix); |
21ccc0cf | 650 | return zserv_send_message(client, s); |
bf094f69 QY |
651 | } |
652 | ||
653 | /* | |
654 | * Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if | |
655 | * nexthop is not found on mrib. Returns both route metric and protocol | |
656 | * distance. | |
34ee41c6 DL |
657 | * |
658 | * *XXX* this ZAPI call is slated to be removed at some point in the future | |
659 | * since MRIB support in PIM is hopelessly broken in its interactions with NHT. | |
660 | * The plan is to make pimd use NHT to receive URIB and MRIB in parallel and | |
661 | * make the decision there, which will obsolete this ZAPI op. | |
662 | * (Otherwise we would need to implement sending NHT updates for the result of | |
663 | * this "URIB-MRIB-combined" table, but we only decide that here on the fly, | |
664 | * so it'd be rather complex to do NHT for.) | |
bf094f69 | 665 | */ |
34ee41c6 DL |
666 | static int zsend_nexthop_lookup_mrib(struct zserv *client, struct ipaddr *addr, |
667 | struct route_entry *re, | |
668 | struct zebra_vrf *zvrf) | |
bf094f69 QY |
669 | { |
670 | struct stream *s; | |
671 | unsigned long nump; | |
672 | uint8_t num; | |
673 | struct nexthop *nexthop; | |
674 | ||
675 | /* Get output stream. */ | |
676 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
677 | stream_reset(s); | |
678 | ||
679 | /* Fill in result. */ | |
34ee41c6 DL |
680 | zclient_create_header(s, ZEBRA_NEXTHOP_LOOKUP_MRIB, zvrf_id(zvrf)); |
681 | stream_put_ipaddr(s, addr); | |
bf094f69 QY |
682 | |
683 | if (re) { | |
b59839af DS |
684 | struct nexthop_group *nhg; |
685 | ||
bf094f69 QY |
686 | stream_putc(s, re->distance); |
687 | stream_putl(s, re->metric); | |
688 | num = 0; | |
8b1766b1 QY |
689 | /* remember position for nexthop_num */ |
690 | nump = stream_get_endp(s); | |
691 | /* reserve room for nexthop_num */ | |
692 | stream_putc(s, 0); | |
b59839af DS |
693 | nhg = rib_get_fib_nhg(re); |
694 | for (ALL_NEXTHOPS_PTR(nhg, nexthop)) { | |
695 | if (rnh_nexthop_valid(re, nexthop)) | |
bf094f69 | 696 | num += zserv_encode_nexthop(s, nexthop); |
b59839af | 697 | } |
bf094f69 | 698 | |
8b1766b1 QY |
699 | /* store nexthop_num */ |
700 | stream_putc_at(s, nump, num); | |
bf094f69 QY |
701 | } else { |
702 | stream_putc(s, 0); /* distance */ | |
703 | stream_putl(s, 0); /* metric */ | |
704 | stream_putc(s, 0); /* nexthop_num */ | |
705 | } | |
706 | ||
707 | stream_putw_at(s, 0, stream_get_endp(s)); | |
708 | ||
21ccc0cf | 709 | return zserv_send_message(client, s); |
bf094f69 QY |
710 | } |
711 | ||
ee94437e MS |
712 | int zsend_nhg_notify(uint16_t type, uint16_t instance, uint32_t session_id, |
713 | uint32_t id, enum zapi_nhg_notify_owner note) | |
2f35a820 DS |
714 | { |
715 | struct zserv *client; | |
716 | struct stream *s; | |
717 | ||
ee94437e | 718 | client = zserv_find_client_session(type, instance, session_id); |
2f35a820 DS |
719 | if (!client) { |
720 | if (IS_ZEBRA_DEBUG_PACKET) { | |
721 | zlog_debug("Not Notifying Owner: %u(%u) about %u(%d)", | |
722 | type, instance, id, note); | |
723 | } | |
724 | return 0; | |
725 | } | |
726 | ||
ee94437e | 727 | if (IS_ZEBRA_DEBUG_SEND) |
04bec7b2 MS |
728 | zlog_debug("%s: type %d, id %d, note %s", |
729 | __func__, type, id, zapi_nhg_notify_owner2str(note)); | |
ee94437e | 730 | |
2f35a820 DS |
731 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); |
732 | stream_reset(s); | |
733 | ||
734 | zclient_create_header(s, ZEBRA_NHG_NOTIFY_OWNER, VRF_DEFAULT); | |
735 | ||
2f35a820 | 736 | stream_put(s, ¬e, sizeof(note)); |
2c7819b9 | 737 | stream_putl(s, id); |
2f35a820 DS |
738 | |
739 | stream_putw_at(s, 0, stream_get_endp(s)); | |
740 | ||
741 | return zserv_send_message(client, s); | |
742 | } | |
743 | ||
86391e56 MS |
744 | /* |
745 | * Common utility send route notification, called from a path using a | |
746 | * route_entry and from a path using a dataplane context. | |
747 | */ | |
a7704e1b | 748 | static int route_notify_internal(const struct route_node *rn, int type, |
86391e56 MS |
749 | uint16_t instance, vrf_id_t vrf_id, |
750 | uint32_t table_id, | |
a7704e1b DS |
751 | enum zapi_route_notify_owner note, afi_t afi, |
752 | safi_t safi) | |
bf094f69 QY |
753 | { |
754 | struct zserv *client; | |
755 | struct stream *s; | |
756 | uint8_t blen; | |
757 | ||
86391e56 | 758 | client = zserv_find_client(type, instance); |
bf094f69 | 759 | if (!client || !client->notify_owner) { |
2dbe669b | 760 | if (IS_ZEBRA_DEBUG_PACKET) |
bf094f69 | 761 | zlog_debug( |
a7704e1b DS |
762 | "Not Notifying Owner: %s about prefix %pRN(%u) %d vrf: %u", |
763 | zebra_route_string(type), rn, table_id, note, | |
2dbe669b | 764 | vrf_id); |
bf094f69 QY |
765 | return 0; |
766 | } | |
767 | ||
2dbe669b DA |
768 | if (IS_ZEBRA_DEBUG_PACKET) |
769 | zlog_debug( | |
a7704e1b DS |
770 | "Notifying Owner: %s about prefix %pRN(%u) %d vrf: %u", |
771 | zebra_route_string(type), rn, table_id, note, vrf_id); | |
bf094f69 | 772 | |
55e74ca9 MS |
773 | /* We're just allocating a small-ish buffer here, since we only |
774 | * encode a small amount of data. | |
775 | */ | |
776 | s = stream_new(ZEBRA_SMALL_PACKET_SIZE); | |
777 | ||
bf094f69 QY |
778 | stream_reset(s); |
779 | ||
86391e56 | 780 | zclient_create_header(s, ZEBRA_ROUTE_NOTIFY_OWNER, vrf_id); |
bf094f69 QY |
781 | |
782 | stream_put(s, ¬e, sizeof(note)); | |
783 | ||
a7704e1b | 784 | stream_putc(s, rn->p.family); |
bf094f69 | 785 | |
a7704e1b DS |
786 | blen = prefix_blen(&rn->p); |
787 | stream_putc(s, rn->p.prefixlen); | |
788 | stream_put(s, &rn->p.u.prefix, blen); | |
bf094f69 | 789 | |
86391e56 | 790 | stream_putl(s, table_id); |
bf094f69 | 791 | |
77b38a4a S |
792 | /* Encode AFI, SAFI in the message */ |
793 | stream_putc(s, afi); | |
794 | stream_putc(s, safi); | |
795 | ||
bf094f69 QY |
796 | stream_putw_at(s, 0, stream_get_endp(s)); |
797 | ||
21ccc0cf | 798 | return zserv_send_message(client, s); |
bf094f69 QY |
799 | } |
800 | ||
a7704e1b DS |
801 | int zsend_route_notify_owner(const struct route_node *rn, |
802 | struct route_entry *re, | |
803 | enum zapi_route_notify_owner note, afi_t afi, | |
804 | safi_t safi) | |
86391e56 | 805 | { |
a7704e1b | 806 | return (route_notify_internal(rn, re->type, re->instance, re->vrf_id, |
77b38a4a | 807 | re->table, note, afi, safi)); |
86391e56 MS |
808 | } |
809 | ||
7cdb1a84 MS |
810 | /* |
811 | * Route-owner notification using info from dataplane update context. | |
812 | */ | |
25779064 | 813 | int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx *ctx, |
7cdb1a84 MS |
814 | enum zapi_route_notify_owner note) |
815 | { | |
a7704e1b DS |
816 | return (route_notify_internal( |
817 | rib_find_rn_from_ctx(ctx), dplane_ctx_get_type(ctx), | |
818 | dplane_ctx_get_instance(ctx), dplane_ctx_get_vrf(ctx), | |
819 | dplane_ctx_get_table(ctx), note, dplane_ctx_get_afi(ctx), | |
820 | dplane_ctx_get_safi(ctx))); | |
77b38a4a S |
821 | } |
822 | ||
823 | static void zread_route_notify_request(ZAPI_HANDLER_ARGS) | |
824 | { | |
825 | uint8_t notify; | |
826 | ||
827 | STREAM_GETC(msg, notify); | |
828 | client->notify_owner = notify; | |
829 | stream_failure: | |
830 | return; | |
86391e56 MS |
831 | } |
832 | ||
f62e5480 | 833 | void zsend_rule_notify_owner(const struct zebra_dplane_ctx *ctx, |
bf094f69 QY |
834 | enum zapi_rule_notify_owner note) |
835 | { | |
836 | struct listnode *node; | |
837 | struct zserv *client; | |
838 | struct stream *s; | |
839 | ||
840 | if (IS_ZEBRA_DEBUG_PACKET) | |
f62e5480 JU |
841 | zlog_debug("%s: Notifying %u", __func__, |
842 | dplane_ctx_rule_get_unique(ctx)); | |
bf094f69 | 843 | |
161e9ab7 | 844 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) { |
f62e5480 | 845 | if (dplane_ctx_rule_get_sock(ctx) == client->sock) |
bf094f69 QY |
846 | break; |
847 | } | |
848 | ||
849 | if (!client) | |
850 | return; | |
851 | ||
852 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
853 | ||
854 | zclient_create_header(s, ZEBRA_RULE_NOTIFY_OWNER, VRF_DEFAULT); | |
855 | stream_put(s, ¬e, sizeof(note)); | |
f62e5480 JU |
856 | stream_putl(s, dplane_ctx_rule_get_seq(ctx)); |
857 | stream_putl(s, dplane_ctx_rule_get_priority(ctx)); | |
858 | stream_putl(s, dplane_ctx_rule_get_unique(ctx)); | |
58a1d249 | 859 | stream_put(s, dplane_ctx_rule_get_ifname(ctx), INTERFACE_NAMSIZ); |
bf094f69 QY |
860 | |
861 | stream_putw_at(s, 0, stream_get_endp(s)); | |
862 | ||
21ccc0cf | 863 | zserv_send_message(client, s); |
bf094f69 QY |
864 | } |
865 | ||
ef524230 | 866 | void zsend_iptable_notify_owner(const struct zebra_dplane_ctx *ctx, |
33c08518 | 867 | enum zapi_iptable_notify_owner note) |
bf094f69 QY |
868 | { |
869 | struct listnode *node; | |
870 | struct zserv *client; | |
871 | struct stream *s; | |
ef524230 PG |
872 | struct zebra_pbr_iptable ipt; |
873 | uint16_t cmd = ZEBRA_IPTABLE_NOTIFY_OWNER; | |
c9250e28 PG |
874 | struct zebra_pbr_iptable *ipt_hash; |
875 | enum dplane_op_e op = dplane_ctx_get_op(ctx); | |
ef524230 | 876 | |
8d78e148 | 877 | dplane_ctx_get_pbr_iptable(ctx, &ipt); |
bf094f69 | 878 | |
c9250e28 PG |
879 | ipt_hash = hash_lookup(zrouter.iptable_hash, &ipt); |
880 | if (ipt_hash) { | |
881 | if (op == DPLANE_OP_IPTABLE_ADD && | |
882 | CHECK_FLAG(ipt_hash->internal_flags, | |
883 | IPTABLE_INSTALL_QUEUED)) | |
884 | UNSET_FLAG(ipt_hash->internal_flags, | |
885 | IPTABLE_INSTALL_QUEUED); | |
886 | else if (op == DPLANE_OP_IPTABLE_DELETE && | |
887 | CHECK_FLAG(ipt_hash->internal_flags, | |
888 | IPTABLE_UNINSTALL_QUEUED)) | |
889 | UNSET_FLAG(ipt_hash->internal_flags, | |
890 | IPTABLE_UNINSTALL_QUEUED); | |
891 | } | |
bf094f69 | 892 | if (IS_ZEBRA_DEBUG_PACKET) |
ef524230 PG |
893 | zlog_debug("%s: Notifying %s id %u note %u", __func__, |
894 | zserv_command_string(cmd), ipt.unique, note); | |
bf094f69 | 895 | |
161e9ab7 | 896 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) { |
ef524230 | 897 | if (ipt.sock == client->sock) |
bf094f69 QY |
898 | break; |
899 | } | |
900 | ||
901 | if (!client) | |
902 | return; | |
903 | ||
904 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
905 | ||
5162e000 | 906 | zclient_create_header(s, cmd, VRF_DEFAULT); |
e108096b | 907 | stream_putw(s, note); |
ef524230 PG |
908 | stream_putl(s, ipt.unique); |
909 | stream_put(s, ipt.ipset_name, ZEBRA_IPSET_NAME_SIZE); | |
bf094f69 QY |
910 | stream_putw_at(s, 0, stream_get_endp(s)); |
911 | ||
21ccc0cf | 912 | zserv_send_message(client, s); |
bf094f69 QY |
913 | } |
914 | ||
33c08518 DS |
915 | void zsend_ipset_notify_owner(const struct zebra_dplane_ctx *ctx, |
916 | enum zapi_ipset_notify_owner note) | |
bf094f69 QY |
917 | { |
918 | struct listnode *node; | |
919 | struct zserv *client; | |
920 | struct stream *s; | |
ef524230 PG |
921 | struct zebra_pbr_ipset ipset; |
922 | uint16_t cmd = ZEBRA_IPSET_NOTIFY_OWNER; | |
923 | ||
8249f96a | 924 | dplane_ctx_get_pbr_ipset(ctx, &ipset); |
bf094f69 QY |
925 | |
926 | if (IS_ZEBRA_DEBUG_PACKET) | |
ef524230 PG |
927 | zlog_debug("%s: Notifying %s id %u note %u", __func__, |
928 | zserv_command_string(cmd), ipset.unique, note); | |
bf094f69 | 929 | |
161e9ab7 | 930 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) { |
ef524230 | 931 | if (ipset.sock == client->sock) |
bf094f69 QY |
932 | break; |
933 | } | |
934 | ||
935 | if (!client) | |
936 | return; | |
937 | ||
938 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
939 | ||
ef524230 | 940 | zclient_create_header(s, cmd, VRF_DEFAULT); |
e108096b | 941 | stream_putw(s, note); |
ef524230 PG |
942 | stream_putl(s, ipset.unique); |
943 | stream_put(s, ipset.ipset_name, ZEBRA_IPSET_NAME_SIZE); | |
bf094f69 QY |
944 | stream_putw_at(s, 0, stream_get_endp(s)); |
945 | ||
21ccc0cf | 946 | zserv_send_message(client, s); |
bf094f69 QY |
947 | } |
948 | ||
ef524230 | 949 | void zsend_ipset_entry_notify_owner(const struct zebra_dplane_ctx *ctx, |
33c08518 | 950 | enum zapi_ipset_entry_notify_owner note) |
bf094f69 QY |
951 | { |
952 | struct listnode *node; | |
953 | struct zserv *client; | |
954 | struct stream *s; | |
ef524230 PG |
955 | struct zebra_pbr_ipset_entry ipent; |
956 | struct zebra_pbr_ipset ipset; | |
957 | uint16_t cmd = ZEBRA_IPSET_ENTRY_NOTIFY_OWNER; | |
5162e000 | 958 | |
f284c132 | 959 | dplane_ctx_get_pbr_ipset_entry(ctx, &ipent); |
8249f96a | 960 | dplane_ctx_get_pbr_ipset(ctx, &ipset); |
bf094f69 QY |
961 | |
962 | if (IS_ZEBRA_DEBUG_PACKET) | |
5162e000 | 963 | zlog_debug("%s: Notifying %s id %u note %u", __func__, |
ef524230 | 964 | zserv_command_string(cmd), ipent.unique, note); |
bf094f69 | 965 | |
161e9ab7 | 966 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) { |
ef524230 | 967 | if (ipent.sock == client->sock) |
bf094f69 QY |
968 | break; |
969 | } | |
970 | ||
971 | if (!client) | |
972 | return; | |
973 | ||
974 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
975 | ||
5162e000 | 976 | zclient_create_header(s, cmd, VRF_DEFAULT); |
e108096b | 977 | stream_putw(s, note); |
ef524230 PG |
978 | stream_putl(s, ipent.unique); |
979 | stream_put(s, ipset.ipset_name, ZEBRA_IPSET_NAME_SIZE); | |
bf094f69 QY |
980 | stream_putw_at(s, 0, stream_get_endp(s)); |
981 | ||
21ccc0cf | 982 | zserv_send_message(client, s); |
bf094f69 QY |
983 | } |
984 | ||
7723e8d3 PG |
985 | void zsend_nhrp_neighbor_notify(int cmd, struct interface *ifp, |
986 | struct ipaddr *ipaddr, int ndm_state, | |
d603c077 | 987 | union sockunion *link_layer_ipv4) |
7723e8d3 PG |
988 | { |
989 | struct stream *s; | |
990 | struct listnode *node, *nnode; | |
991 | struct zserv *client; | |
992 | afi_t afi; | |
d603c077 | 993 | union sockunion ip; |
7723e8d3 PG |
994 | |
995 | if (IS_ZEBRA_DEBUG_PACKET) | |
7d7be47e | 996 | zlog_debug("%s: Notifying Neighbor entry (%u)", __func__, cmd); |
7723e8d3 | 997 | |
d603c077 PG |
998 | sockunion_family(&ip) = ipaddr_family(ipaddr); |
999 | afi = family2afi(sockunion_family(&ip)); | |
1000 | memcpy((char *)sockunion_get_addr(&ip), &ipaddr->ip.addr, | |
1001 | family2addrsize(sockunion_family(&ip))); | |
1002 | ||
7723e8d3 | 1003 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) { |
096f7609 IR |
1004 | if (!vrf_bitmap_check(client->nhrp_neighinfo[afi], |
1005 | ifp->vrf->vrf_id)) | |
7723e8d3 PG |
1006 | continue; |
1007 | ||
1008 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
c4e1fd52 PG |
1009 | zclient_neigh_ip_encode(s, cmd, &ip, link_layer_ipv4, ifp, |
1010 | ndm_state); | |
7723e8d3 | 1011 | stream_putw_at(s, 0, stream_get_endp(s)); |
7723e8d3 PG |
1012 | zserv_send_message(client, s); |
1013 | } | |
1014 | } | |
1015 | ||
1016 | ||
98a3fb0a SM |
1017 | /* Router-id is updated. Send ZEBRA_ROUTER_ID_UPDATE to client. */ |
1018 | int zsend_router_id_update(struct zserv *client, afi_t afi, struct prefix *p, | |
bf094f69 QY |
1019 | vrf_id_t vrf_id) |
1020 | { | |
1021 | int blen; | |
98a3fb0a | 1022 | struct stream *s; |
bf094f69 QY |
1023 | |
1024 | /* Check this client need interface information. */ | |
98a3fb0a | 1025 | if (!vrf_bitmap_check(client->ridinfo[afi], vrf_id)) |
bf094f69 QY |
1026 | return 0; |
1027 | ||
98a3fb0a | 1028 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); |
bf094f69 QY |
1029 | |
1030 | /* Message type. */ | |
1031 | zclient_create_header(s, ZEBRA_ROUTER_ID_UPDATE, vrf_id); | |
1032 | ||
1033 | /* Prefix information. */ | |
1034 | stream_putc(s, p->family); | |
1035 | blen = prefix_blen(p); | |
1036 | stream_put(s, &p->u.prefix, blen); | |
1037 | stream_putc(s, p->prefixlen); | |
1038 | ||
1039 | /* Write packet size. */ | |
1040 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1041 | ||
21ccc0cf | 1042 | return zserv_send_message(client, s); |
bf094f69 QY |
1043 | } |
1044 | ||
1045 | /* | |
1046 | * Function used by Zebra to send a PW status update to LDP daemon | |
1047 | */ | |
1048 | int zsend_pw_update(struct zserv *client, struct zebra_pw *pw) | |
1049 | { | |
1050 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
1051 | ||
1052 | zclient_create_header(s, ZEBRA_PW_STATUS_UPDATE, pw->vrf_id); | |
3682bd90 | 1053 | stream_write(s, pw->ifname, INTERFACE_NAMSIZ); |
bf094f69 QY |
1054 | stream_putl(s, pw->ifindex); |
1055 | stream_putl(s, pw->status); | |
1056 | ||
1057 | /* Put length at the first point of the stream. */ | |
1058 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1059 | ||
21ccc0cf | 1060 | return zserv_send_message(client, s); |
bf094f69 QY |
1061 | } |
1062 | ||
1063 | /* Send response to a get label chunk request to client */ | |
e11d7c96 | 1064 | int zsend_assign_label_chunk_response(struct zserv *client, vrf_id_t vrf_id, |
e11d7c96 | 1065 | struct label_manager_chunk *lmc) |
bf094f69 | 1066 | { |
bf094f69 QY |
1067 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); |
1068 | ||
1069 | zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id); | |
f004f7c3 | 1070 | /* proto */ |
4cebdb9b | 1071 | stream_putc(s, client->proto); |
f004f7c3 | 1072 | /* instance */ |
4cebdb9b | 1073 | stream_putw(s, client->instance); |
bf094f69 QY |
1074 | |
1075 | if (lmc) { | |
1076 | /* keep */ | |
1077 | stream_putc(s, lmc->keep); | |
1078 | /* start and end labels */ | |
1079 | stream_putl(s, lmc->start); | |
1080 | stream_putl(s, lmc->end); | |
1081 | } | |
1082 | ||
1083 | /* Write packet size. */ | |
1084 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1085 | ||
732d22cb | 1086 | return zserv_send_message(client, s); |
bf094f69 QY |
1087 | } |
1088 | ||
1089 | /* Send response to a label manager connect request to client */ | |
e11d7c96 EDP |
1090 | int zsend_label_manager_connect_response(struct zserv *client, vrf_id_t vrf_id, |
1091 | unsigned short result) | |
bf094f69 | 1092 | { |
bf094f69 QY |
1093 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); |
1094 | ||
1095 | zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id); | |
1096 | ||
5dffb0e9 FR |
1097 | /* proto */ |
1098 | stream_putc(s, client->proto); | |
1099 | ||
1100 | /* instance */ | |
1101 | stream_putw(s, client->instance); | |
1102 | ||
bf094f69 QY |
1103 | /* result */ |
1104 | stream_putc(s, result); | |
1105 | ||
1106 | /* Write packet size. */ | |
1107 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1108 | ||
732d22cb | 1109 | return zserv_send_message(client, s); |
bf094f69 QY |
1110 | } |
1111 | ||
1112 | /* Send response to a get table chunk request to client */ | |
1113 | static int zsend_assign_table_chunk_response(struct zserv *client, | |
1114 | vrf_id_t vrf_id, | |
1115 | struct table_manager_chunk *tmc) | |
1116 | { | |
1117 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
1118 | ||
1119 | zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, vrf_id); | |
1120 | ||
1121 | if (tmc) { | |
1122 | /* start and end labels */ | |
1123 | stream_putl(s, tmc->start); | |
1124 | stream_putl(s, tmc->end); | |
1125 | } | |
1126 | ||
1127 | /* Write packet size. */ | |
1128 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1129 | ||
21ccc0cf | 1130 | return zserv_send_message(client, s); |
bf094f69 QY |
1131 | } |
1132 | ||
1133 | static int zsend_table_manager_connect_response(struct zserv *client, | |
1134 | vrf_id_t vrf_id, | |
1135 | uint16_t result) | |
1136 | { | |
1137 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
1138 | ||
1139 | zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, vrf_id); | |
1140 | ||
1141 | /* result */ | |
1142 | stream_putc(s, result); | |
1143 | ||
1144 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1145 | ||
21ccc0cf | 1146 | return zserv_send_message(client, s); |
bf094f69 QY |
1147 | } |
1148 | ||
f5ca329b HS |
1149 | /* SRv6 locator add notification from zebra daemon. */ |
1150 | int zsend_zebra_srv6_locator_add(struct zserv *client, struct srv6_locator *loc) | |
1151 | { | |
1152 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
1153 | ||
1154 | zclient_create_header(s, ZEBRA_SRV6_LOCATOR_ADD, VRF_DEFAULT); | |
1155 | zapi_srv6_locator_encode(s, loc); | |
1156 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1157 | ||
1158 | return zserv_send_message(client, s); | |
1159 | } | |
1160 | ||
1161 | /* SRv6 locator delete notification from zebra daemon. */ | |
1162 | int zsend_zebra_srv6_locator_delete(struct zserv *client, | |
1163 | struct srv6_locator *loc) | |
1164 | { | |
1165 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
1166 | ||
1167 | zclient_create_header(s, ZEBRA_SRV6_LOCATOR_DELETE, VRF_DEFAULT); | |
1168 | zapi_srv6_locator_encode(s, loc); | |
1169 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1170 | ||
1171 | return zserv_send_message(client, s); | |
1172 | } | |
1173 | ||
bf094f69 QY |
1174 | /* Inbound message handling ------------------------------------------------ */ |
1175 | ||
bf094f69 QY |
1176 | /* Nexthop register */ |
1177 | static void zread_rnh_register(ZAPI_HANDLER_ARGS) | |
1178 | { | |
1179 | struct rnh *rnh; | |
1180 | struct stream *s; | |
1181 | struct prefix p; | |
1182 | unsigned short l = 0; | |
6cd9a93d | 1183 | uint8_t connected = 0; |
ed6cec97 | 1184 | uint8_t resolve_via_default; |
1d30d1f4 | 1185 | bool exist; |
906b54dd DS |
1186 | bool flag_changed = false; |
1187 | uint8_t orig_flags; | |
027db469 | 1188 | safi_t safi; |
bf094f69 QY |
1189 | |
1190 | if (IS_ZEBRA_DEBUG_NHT) | |
1191 | zlog_debug( | |
6071e5e9 | 1192 | "rnh_register msg from client %s: hdr->length=%d vrf=%u", |
bf094f69 | 1193 | zebra_route_string(client->proto), hdr->length, |
bf094f69 QY |
1194 | zvrf->vrf->vrf_id); |
1195 | ||
1196 | s = msg; | |
1197 | ||
469d6277 DS |
1198 | if (!client->nh_reg_time) |
1199 | client->nh_reg_time = monotime(NULL); | |
bf094f69 QY |
1200 | |
1201 | while (l < hdr->length) { | |
6cd9a93d | 1202 | STREAM_GETC(s, connected); |
ed6cec97 | 1203 | STREAM_GETC(s, resolve_via_default); |
027db469 | 1204 | STREAM_GETW(s, safi); |
bf094f69 QY |
1205 | STREAM_GETW(s, p.family); |
1206 | STREAM_GETC(s, p.prefixlen); | |
027db469 | 1207 | l += 7; |
bf094f69 | 1208 | if (p.family == AF_INET) { |
ab5990d8 | 1209 | client->v4_nh_watch_add_cnt++; |
bf094f69 | 1210 | if (p.prefixlen > IPV4_MAX_BITLEN) { |
9df414fe | 1211 | zlog_debug( |
bf094f69 | 1212 | "%s: Specified prefix hdr->length %d is too large for a v4 address", |
15569c58 | 1213 | __func__, p.prefixlen); |
bf094f69 QY |
1214 | return; |
1215 | } | |
1216 | STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN); | |
1217 | l += IPV4_MAX_BYTELEN; | |
1218 | } else if (p.family == AF_INET6) { | |
ab5990d8 | 1219 | client->v6_nh_watch_add_cnt++; |
bf094f69 | 1220 | if (p.prefixlen > IPV6_MAX_BITLEN) { |
9df414fe | 1221 | zlog_debug( |
bf094f69 | 1222 | "%s: Specified prefix hdr->length %d is to large for a v6 address", |
15569c58 | 1223 | __func__, p.prefixlen); |
bf094f69 QY |
1224 | return; |
1225 | } | |
1226 | STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN); | |
1227 | l += IPV6_MAX_BYTELEN; | |
1228 | } else { | |
af4c2728 | 1229 | flog_err( |
e914ccbe | 1230 | EC_ZEBRA_UNKNOWN_FAMILY, |
1d5453d6 | 1231 | "rnh_register: Received unknown family type %d", |
bf094f69 QY |
1232 | p.family); |
1233 | return; | |
1234 | } | |
e9ac2861 | 1235 | rnh = zebra_add_rnh(&p, zvrf_id(zvrf), safi, &exist); |
1d30d1f4 DS |
1236 | if (!rnh) |
1237 | return; | |
1238 | ||
906b54dd | 1239 | orig_flags = rnh->flags; |
6cd9a93d | 1240 | if (connected && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) |
6071e5e9 | 1241 | SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED); |
6cd9a93d DS |
1242 | else if (!connected |
1243 | && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) | |
6071e5e9 | 1244 | UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED); |
bf094f69 | 1245 | |
ed6cec97 DS |
1246 | if (resolve_via_default) |
1247 | SET_FLAG(rnh->flags, ZEBRA_NHT_RESOLVE_VIA_DEFAULT); | |
bf094f69 | 1248 | |
906b54dd DS |
1249 | if (orig_flags != rnh->flags) |
1250 | flag_changed = true; | |
1251 | ||
bf094f69 | 1252 | /* Anything not AF_INET/INET6 has been filtered out above */ |
906b54dd | 1253 | if (!exist || flag_changed) |
d597533a | 1254 | zebra_evaluate_rnh(zvrf, family2afi(p.family), 1, &p, |
027db469 | 1255 | safi); |
dd25a6b3 | 1256 | |
6071e5e9 | 1257 | zebra_add_rnh_client(rnh, client, zvrf_id(zvrf)); |
bf094f69 QY |
1258 | } |
1259 | ||
1260 | stream_failure: | |
1261 | return; | |
1262 | } | |
1263 | ||
1264 | /* Nexthop register */ | |
1265 | static void zread_rnh_unregister(ZAPI_HANDLER_ARGS) | |
1266 | { | |
1267 | struct rnh *rnh; | |
1268 | struct stream *s; | |
1269 | struct prefix p; | |
1270 | unsigned short l = 0; | |
027db469 | 1271 | safi_t safi; |
bf094f69 QY |
1272 | |
1273 | if (IS_ZEBRA_DEBUG_NHT) | |
1274 | zlog_debug( | |
63efca0e | 1275 | "rnh_unregister msg from client %s: hdr->length=%d vrf: %u", |
bf094f69 QY |
1276 | zebra_route_string(client->proto), hdr->length, |
1277 | zvrf->vrf->vrf_id); | |
1278 | ||
1279 | s = msg; | |
1280 | ||
1281 | while (l < hdr->length) { | |
6cd9a93d | 1282 | uint8_t ignore; |
bf094f69 | 1283 | |
6cd9a93d DS |
1284 | STREAM_GETC(s, ignore); |
1285 | if (ignore != 0) | |
ed6cec97 | 1286 | goto stream_failure; |
6cd9a93d DS |
1287 | STREAM_GETC(s, ignore); |
1288 | if (ignore != 0) | |
bf094f69 QY |
1289 | goto stream_failure; |
1290 | ||
027db469 | 1291 | STREAM_GETW(s, safi); |
bf094f69 QY |
1292 | STREAM_GETW(s, p.family); |
1293 | STREAM_GETC(s, p.prefixlen); | |
027db469 | 1294 | l += 7; |
bf094f69 | 1295 | if (p.family == AF_INET) { |
ab5990d8 | 1296 | client->v4_nh_watch_rem_cnt++; |
bf094f69 | 1297 | if (p.prefixlen > IPV4_MAX_BITLEN) { |
9df414fe | 1298 | zlog_debug( |
bf094f69 | 1299 | "%s: Specified prefix hdr->length %d is to large for a v4 address", |
15569c58 | 1300 | __func__, p.prefixlen); |
bf094f69 QY |
1301 | return; |
1302 | } | |
1303 | STREAM_GET(&p.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN); | |
1304 | l += IPV4_MAX_BYTELEN; | |
1305 | } else if (p.family == AF_INET6) { | |
ab5990d8 | 1306 | client->v6_nh_watch_rem_cnt++; |
bf094f69 | 1307 | if (p.prefixlen > IPV6_MAX_BITLEN) { |
9df414fe | 1308 | zlog_debug( |
bf094f69 | 1309 | "%s: Specified prefix hdr->length %d is to large for a v6 address", |
15569c58 | 1310 | __func__, p.prefixlen); |
bf094f69 QY |
1311 | return; |
1312 | } | |
1313 | STREAM_GET(&p.u.prefix6, s, IPV6_MAX_BYTELEN); | |
1314 | l += IPV6_MAX_BYTELEN; | |
1315 | } else { | |
af4c2728 | 1316 | flog_err( |
e914ccbe | 1317 | EC_ZEBRA_UNKNOWN_FAMILY, |
1d5453d6 | 1318 | "rnh_register: Received unknown family type %d", |
bf094f69 QY |
1319 | p.family); |
1320 | return; | |
1321 | } | |
027db469 | 1322 | rnh = zebra_lookup_rnh(&p, zvrf_id(zvrf), safi); |
bf094f69 QY |
1323 | if (rnh) { |
1324 | client->nh_dereg_time = monotime(NULL); | |
6071e5e9 | 1325 | zebra_remove_rnh_client(rnh, client); |
bf094f69 QY |
1326 | } |
1327 | } | |
1328 | stream_failure: | |
1329 | return; | |
1330 | } | |
1331 | ||
1332 | #define ZEBRA_MIN_FEC_LENGTH 5 | |
1333 | ||
1334 | /* FEC register */ | |
1335 | static void zread_fec_register(ZAPI_HANDLER_ARGS) | |
1336 | { | |
1337 | struct stream *s; | |
1338 | unsigned short l = 0; | |
1339 | struct prefix p; | |
1340 | uint16_t flags; | |
57592a53 | 1341 | uint32_t label = MPLS_INVALID_LABEL; |
bf094f69 QY |
1342 | uint32_t label_index = MPLS_INVALID_LABEL_INDEX; |
1343 | ||
1344 | s = msg; | |
1345 | zvrf = vrf_info_lookup(VRF_DEFAULT); | |
1346 | if (!zvrf) | |
8b1766b1 | 1347 | return; |
bf094f69 QY |
1348 | |
1349 | /* | |
1350 | * The minimum amount of data that can be sent for one fec | |
1351 | * registration | |
1352 | */ | |
1353 | if (hdr->length < ZEBRA_MIN_FEC_LENGTH) { | |
af4c2728 | 1354 | flog_err( |
e914ccbe | 1355 | EC_ZEBRA_IRDP_LEN_MISMATCH, |
bf094f69 QY |
1356 | "fec_register: Received a fec register of hdr->length %d, it is of insufficient size to properly decode", |
1357 | hdr->length); | |
1358 | return; | |
1359 | } | |
1360 | ||
1361 | while (l < hdr->length) { | |
1362 | STREAM_GETW(s, flags); | |
1363 | memset(&p, 0, sizeof(p)); | |
1364 | STREAM_GETW(s, p.family); | |
1365 | if (p.family != AF_INET && p.family != AF_INET6) { | |
af4c2728 | 1366 | flog_err( |
e914ccbe | 1367 | EC_ZEBRA_UNKNOWN_FAMILY, |
1d5453d6 | 1368 | "fec_register: Received unknown family type %d", |
bf094f69 QY |
1369 | p.family); |
1370 | return; | |
1371 | } | |
1372 | STREAM_GETC(s, p.prefixlen); | |
1373 | if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN) | |
1374 | || (p.family == AF_INET6 | |
1375 | && p.prefixlen > IPV6_MAX_BITLEN)) { | |
9df414fe | 1376 | zlog_debug( |
bf094f69 | 1377 | "%s: Specified prefix hdr->length: %d is to long for %d", |
15569c58 | 1378 | __func__, p.prefixlen, p.family); |
bf094f69 QY |
1379 | return; |
1380 | } | |
1381 | l += 5; | |
1382 | STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen)); | |
1383 | l += PSIZE(p.prefixlen); | |
57592a53 AD |
1384 | if (flags & ZEBRA_FEC_REGISTER_LABEL) { |
1385 | STREAM_GETL(s, label); | |
1386 | l += 4; | |
1387 | } else if (flags & ZEBRA_FEC_REGISTER_LABEL_INDEX) { | |
bf094f69 QY |
1388 | STREAM_GETL(s, label_index); |
1389 | l += 4; | |
57592a53 AD |
1390 | } |
1391 | ||
1392 | zebra_mpls_fec_register(zvrf, &p, label, label_index, client); | |
bf094f69 QY |
1393 | } |
1394 | ||
1395 | stream_failure: | |
1396 | return; | |
1397 | } | |
1398 | ||
1399 | /* FEC unregister */ | |
1400 | static void zread_fec_unregister(ZAPI_HANDLER_ARGS) | |
1401 | { | |
1402 | struct stream *s; | |
1403 | unsigned short l = 0; | |
1404 | struct prefix p; | |
1405 | uint16_t flags; | |
1406 | ||
1407 | s = msg; | |
1408 | zvrf = vrf_info_lookup(VRF_DEFAULT); | |
1409 | if (!zvrf) | |
8b1766b1 | 1410 | return; |
bf094f69 QY |
1411 | |
1412 | /* | |
1413 | * The minimum amount of data that can be sent for one | |
1414 | * fec unregistration | |
1415 | */ | |
1416 | if (hdr->length < ZEBRA_MIN_FEC_LENGTH) { | |
af4c2728 | 1417 | flog_err( |
e914ccbe | 1418 | EC_ZEBRA_IRDP_LEN_MISMATCH, |
bf094f69 QY |
1419 | "fec_unregister: Received a fec unregister of hdr->length %d, it is of insufficient size to properly decode", |
1420 | hdr->length); | |
1421 | return; | |
1422 | } | |
1423 | ||
1424 | while (l < hdr->length) { | |
1425 | STREAM_GETW(s, flags); | |
1426 | if (flags != 0) | |
1427 | goto stream_failure; | |
1428 | ||
1429 | memset(&p, 0, sizeof(p)); | |
1430 | STREAM_GETW(s, p.family); | |
1431 | if (p.family != AF_INET && p.family != AF_INET6) { | |
af4c2728 | 1432 | flog_err( |
e914ccbe | 1433 | EC_ZEBRA_UNKNOWN_FAMILY, |
1d5453d6 | 1434 | "fec_unregister: Received unknown family type %d", |
bf094f69 QY |
1435 | p.family); |
1436 | return; | |
1437 | } | |
1438 | STREAM_GETC(s, p.prefixlen); | |
1439 | if ((p.family == AF_INET && p.prefixlen > IPV4_MAX_BITLEN) | |
1440 | || (p.family == AF_INET6 | |
1441 | && p.prefixlen > IPV6_MAX_BITLEN)) { | |
9df414fe | 1442 | zlog_debug( |
bf094f69 | 1443 | "%s: Received prefix hdr->length %d which is greater than %d can support", |
15569c58 | 1444 | __func__, p.prefixlen, p.family); |
bf094f69 QY |
1445 | return; |
1446 | } | |
1447 | l += 5; | |
1448 | STREAM_GET(&p.u.prefix, s, PSIZE(p.prefixlen)); | |
1449 | l += PSIZE(p.prefixlen); | |
1450 | zebra_mpls_fec_unregister(zvrf, &p, client); | |
1451 | } | |
1452 | ||
1453 | stream_failure: | |
1454 | return; | |
1455 | } | |
1456 | ||
1457 | ||
1458 | /* | |
1459 | * Register zebra server interface information. | |
1460 | * Send current all interface and address information. | |
1461 | */ | |
1462 | static void zread_interface_add(ZAPI_HANDLER_ARGS) | |
1463 | { | |
1464 | struct vrf *vrf; | |
1465 | struct interface *ifp; | |
1466 | ||
9212d1e1 | 1467 | vrf_id_t vrf_id = zvrf_id(zvrf); |
1468 | if (vrf_id != VRF_DEFAULT && vrf_id != VRF_UNKNOWN) { | |
1469 | FOR_ALL_INTERFACES (zvrf->vrf, ifp) { | |
1470 | /* Skip pseudo interface. */ | |
1471 | if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) | |
1472 | continue; | |
1473 | ||
1474 | zsend_interface_add(client, ifp); | |
1475 | zsend_interface_link_params(client, ifp); | |
1476 | zsend_interface_addresses(client, ifp); | |
1477 | } | |
1478 | return; | |
1479 | } | |
1480 | ||
bf094f69 QY |
1481 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { |
1482 | FOR_ALL_INTERFACES (vrf, ifp) { | |
1483 | /* Skip pseudo interface. */ | |
1484 | if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) | |
1485 | continue; | |
1486 | ||
1487 | zsend_interface_add(client, ifp); | |
27ecbea3 | 1488 | zsend_interface_link_params(client, ifp); |
bf094f69 QY |
1489 | zsend_interface_addresses(client, ifp); |
1490 | } | |
1491 | } | |
1492 | } | |
1493 | ||
1494 | /* Unregister zebra server interface information. */ | |
1495 | static void zread_interface_delete(ZAPI_HANDLER_ARGS) | |
1496 | { | |
bf094f69 QY |
1497 | } |
1498 | ||
c3bd894e QY |
1499 | /* |
1500 | * Handle message requesting interface be set up or down. | |
1501 | */ | |
1502 | static void zread_interface_set_protodown(ZAPI_HANDLER_ARGS) | |
1503 | { | |
1504 | ifindex_t ifindex; | |
1505 | struct interface *ifp; | |
1506 | char down; | |
5d414138 | 1507 | enum protodown_reasons reason; |
c3bd894e QY |
1508 | |
1509 | STREAM_GETL(msg, ifindex); | |
1510 | STREAM_GETC(msg, down); | |
1511 | ||
1512 | /* set ifdown */ | |
1513 | ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT), ifindex); | |
c3bd894e | 1514 | |
5d414138 | 1515 | if (!ifp) { |
65dc7dd3 QY |
1516 | zlog_warn( |
1517 | "Cannot set protodown %s for interface %u; does not exist", | |
1518 | down ? "on" : "off", ifindex); | |
5d414138 SW |
1519 | |
1520 | return; | |
1521 | } | |
1522 | ||
1523 | switch (client->proto) { | |
1524 | case ZEBRA_ROUTE_VRRP: | |
1525 | reason = ZEBRA_PROTODOWN_VRRP; | |
1526 | break; | |
1527 | case ZEBRA_ROUTE_SHARP: | |
1528 | reason = ZEBRA_PROTODOWN_SHARP; | |
1529 | break; | |
1530 | default: | |
1531 | reason = 0; | |
1532 | break; | |
65dc7dd3 QY |
1533 | } |
1534 | ||
5d414138 | 1535 | zebra_if_set_protodown(ifp, down, reason); |
c3bd894e QY |
1536 | |
1537 | stream_failure: | |
1538 | return; | |
1539 | } | |
1540 | ||
786a9bd9 | 1541 | bool zserv_nexthop_num_warn(const char *caller, const struct prefix *p, |
bf094f69 QY |
1542 | const unsigned int nexthop_num) |
1543 | { | |
b3f2b590 | 1544 | if (nexthop_num > zrouter.multipath_num) { |
bf094f69 | 1545 | char buff[PREFIX2STR_BUFFER]; |
8b1766b1 | 1546 | |
e270f004 SW |
1547 | if (p) |
1548 | prefix2str(p, buff, sizeof(buff)); | |
1549 | ||
9df414fe | 1550 | flog_warn( |
e914ccbe | 1551 | EC_ZEBRA_MORE_NH_THAN_MULTIPATH, |
bf094f69 | 1552 | "%s: Prefix %s has %d nexthops, but we can only use the first %d", |
e270f004 SW |
1553 | caller, (p ? buff : "(NULL)"), nexthop_num, |
1554 | zrouter.multipath_num); | |
786a9bd9 | 1555 | return true; |
bf094f69 | 1556 | } |
786a9bd9 DS |
1557 | |
1558 | return false; | |
bf094f69 QY |
1559 | } |
1560 | ||
62e46303 MS |
1561 | /* |
1562 | * Create a new nexthop based on a zapi nexthop. | |
1563 | */ | |
2f35a820 DS |
1564 | static struct nexthop *nexthop_from_zapi(const struct zapi_nexthop *api_nh, |
1565 | uint32_t flags, struct prefix *p, | |
1566 | uint16_t backup_nexthop_num) | |
62e46303 MS |
1567 | { |
1568 | struct nexthop *nexthop = NULL; | |
1569 | struct ipaddr vtep_ip; | |
1570 | struct interface *ifp; | |
474aebd9 | 1571 | int i; |
62e46303 MS |
1572 | char nhbuf[INET6_ADDRSTRLEN] = ""; |
1573 | ||
62e46303 MS |
1574 | switch (api_nh->type) { |
1575 | case NEXTHOP_TYPE_IFINDEX: | |
1576 | nexthop = nexthop_from_ifindex(api_nh->ifindex, api_nh->vrf_id); | |
1577 | break; | |
1578 | case NEXTHOP_TYPE_IPV4: | |
1579 | if (IS_ZEBRA_DEBUG_RECV) { | |
1580 | inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf, | |
1581 | sizeof(nhbuf)); | |
1582 | zlog_debug("%s: nh=%s, vrf_id=%d", __func__, | |
1583 | nhbuf, api_nh->vrf_id); | |
1584 | } | |
1585 | nexthop = nexthop_from_ipv4(&api_nh->gate.ipv4, NULL, | |
1586 | api_nh->vrf_id); | |
1587 | break; | |
1588 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
1589 | if (IS_ZEBRA_DEBUG_RECV) { | |
1590 | inet_ntop(AF_INET, &api_nh->gate.ipv4, nhbuf, | |
1591 | sizeof(nhbuf)); | |
1592 | zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d", | |
1593 | __func__, nhbuf, api_nh->vrf_id, | |
1594 | api_nh->ifindex); | |
1595 | } | |
1596 | ||
1597 | nexthop = nexthop_from_ipv4_ifindex( | |
1598 | &api_nh->gate.ipv4, NULL, api_nh->ifindex, | |
1599 | api_nh->vrf_id); | |
1600 | ||
62e46303 MS |
1601 | /* Special handling for IPv4 routes sourced from EVPN: |
1602 | * the nexthop and associated MAC need to be installed. | |
1603 | */ | |
5609e70f | 1604 | if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN)) { |
6006b807 | 1605 | memset(&vtep_ip, 0, sizeof(vtep_ip)); |
62e46303 MS |
1606 | vtep_ip.ipa_type = IPADDR_V4; |
1607 | memcpy(&(vtep_ip.ipaddr_v4), &(api_nh->gate.ipv4), | |
1608 | sizeof(struct in_addr)); | |
8244ba34 | 1609 | zebra_rib_queue_evpn_route_add( |
ff9aca4f | 1610 | api_nh->vrf_id, &api_nh->rmac, &vtep_ip, p); |
5609e70f | 1611 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_EVPN); |
62e46303 MS |
1612 | } |
1613 | break; | |
1614 | case NEXTHOP_TYPE_IPV6: | |
1615 | if (IS_ZEBRA_DEBUG_RECV) { | |
1616 | inet_ntop(AF_INET6, &api_nh->gate.ipv6, nhbuf, | |
1617 | sizeof(nhbuf)); | |
1618 | zlog_debug("%s: nh=%s, vrf_id=%d", __func__, | |
1619 | nhbuf, api_nh->vrf_id); | |
1620 | } | |
1621 | nexthop = nexthop_from_ipv6(&api_nh->gate.ipv6, api_nh->vrf_id); | |
1622 | break; | |
1623 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
1624 | if (IS_ZEBRA_DEBUG_RECV) { | |
1625 | inet_ntop(AF_INET6, &api_nh->gate.ipv6, nhbuf, | |
1626 | sizeof(nhbuf)); | |
1627 | zlog_debug("%s: nh=%s, vrf_id=%d, ifindex=%d", | |
1628 | __func__, nhbuf, api_nh->vrf_id, | |
1629 | api_nh->ifindex); | |
1630 | } | |
1631 | nexthop = nexthop_from_ipv6_ifindex(&api_nh->gate.ipv6, | |
1632 | api_nh->ifindex, | |
1633 | api_nh->vrf_id); | |
1634 | ||
1635 | /* Special handling for IPv6 routes sourced from EVPN: | |
1636 | * the nexthop and associated MAC need to be installed. | |
1637 | */ | |
5609e70f | 1638 | if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_EVPN)) { |
6006b807 | 1639 | memset(&vtep_ip, 0, sizeof(vtep_ip)); |
62e46303 MS |
1640 | vtep_ip.ipa_type = IPADDR_V6; |
1641 | memcpy(&vtep_ip.ipaddr_v6, &(api_nh->gate.ipv6), | |
1642 | sizeof(struct in6_addr)); | |
8244ba34 | 1643 | zebra_rib_queue_evpn_route_add( |
ff9aca4f | 1644 | api_nh->vrf_id, &api_nh->rmac, &vtep_ip, p); |
5609e70f | 1645 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_EVPN); |
62e46303 MS |
1646 | } |
1647 | break; | |
1648 | case NEXTHOP_TYPE_BLACKHOLE: | |
1649 | if (IS_ZEBRA_DEBUG_RECV) | |
1650 | zlog_debug("%s: nh blackhole %d", | |
1651 | __func__, api_nh->bh_type); | |
1652 | ||
0789eb69 KM |
1653 | nexthop = |
1654 | nexthop_from_blackhole(api_nh->bh_type, api_nh->vrf_id); | |
62e46303 MS |
1655 | break; |
1656 | } | |
1657 | ||
1658 | /* Return early if we couldn't process the zapi nexthop */ | |
1659 | if (nexthop == NULL) { | |
1660 | goto done; | |
1661 | } | |
1662 | ||
12b4d77b | 1663 | /* Mark nexthop as onlink either if client has explicitly told us |
1664 | * to or if the nexthop is on an 'unnumbered' interface. | |
1665 | */ | |
62e46303 MS |
1666 | if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_ONLINK)) |
1667 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK); | |
12b4d77b | 1668 | else if (api_nh->type == NEXTHOP_TYPE_IPV4_IFINDEX) { |
1669 | ifp = if_lookup_by_index(api_nh->ifindex, api_nh->vrf_id); | |
1670 | if (ifp && connected_is_unnumbered(ifp)) | |
1671 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK); | |
1672 | } | |
62e46303 MS |
1673 | |
1674 | if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_WEIGHT)) | |
1675 | nexthop->weight = api_nh->weight; | |
1676 | ||
1d48702e | 1677 | if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP)) { |
474aebd9 MS |
1678 | /* Validate count */ |
1679 | if (api_nh->backup_num > NEXTHOP_MAX_BACKUPS) { | |
1d48702e | 1680 | if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_EVENT) |
474aebd9 MS |
1681 | zlog_debug("%s: invalid backup nh count %d", |
1682 | __func__, api_nh->backup_num); | |
1683 | nexthop_free(nexthop); | |
1684 | nexthop = NULL; | |
1685 | goto done; | |
1686 | } | |
1687 | ||
1688 | /* Copy backup info */ | |
1689 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP); | |
1690 | nexthop->backup_num = api_nh->backup_num; | |
1691 | ||
1692 | for (i = 0; i < api_nh->backup_num; i++) { | |
1693 | /* Validate backup index */ | |
2f35a820 | 1694 | if (api_nh->backup_idx[i] < backup_nexthop_num) { |
474aebd9 MS |
1695 | nexthop->backup_idx[i] = api_nh->backup_idx[i]; |
1696 | } else { | |
1697 | if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_EVENT) | |
1698 | zlog_debug("%s: invalid backup nh idx %d", | |
1699 | __func__, | |
1700 | api_nh->backup_idx[i]); | |
1701 | nexthop_free(nexthop); | |
1702 | nexthop = NULL; | |
1703 | goto done; | |
1704 | } | |
1d48702e MS |
1705 | } |
1706 | } | |
474aebd9 | 1707 | |
62e46303 MS |
1708 | done: |
1709 | return nexthop; | |
1710 | } | |
1711 | ||
2f35a820 DS |
1712 | static bool zapi_read_nexthops(struct zserv *client, struct prefix *p, |
1713 | struct zapi_nexthop *nhops, uint32_t flags, | |
1714 | uint32_t message, uint16_t nexthop_num, | |
1715 | uint16_t backup_nh_num, | |
f70da2a3 DS |
1716 | struct nexthop_group **png, |
1717 | struct nhg_backup_info **pbnhg) | |
bf094f69 | 1718 | { |
0eb97b86 | 1719 | struct nexthop_group *ng = NULL; |
1d48702e | 1720 | struct nhg_backup_info *bnhg = NULL; |
2f35a820 | 1721 | uint16_t i; |
f70da2a3 | 1722 | struct nexthop *last_nh = NULL; |
bf094f69 | 1723 | |
f70da2a3 | 1724 | assert(!(png && pbnhg)); |
c2c02b76 | 1725 | |
2f35a820 | 1726 | if (png) |
66c28560 | 1727 | ng = nexthop_group_new(); |
bf094f69 | 1728 | |
2f35a820 | 1729 | if (pbnhg && backup_nh_num > 0) { |
f70da2a3 | 1730 | if (IS_ZEBRA_DEBUG_RECV) |
2f35a820 DS |
1731 | zlog_debug("%s: adding %d backup nexthops", __func__, |
1732 | backup_nh_num); | |
7fcb24bb | 1733 | |
66c28560 | 1734 | bnhg = zebra_nhg_backup_alloc(); |
62e46303 MS |
1735 | } |
1736 | ||
bf094f69 QY |
1737 | /* |
1738 | * TBD should _all_ of the nexthop add operations use | |
1739 | * api_nh->vrf_id instead of re->vrf_id ? I only changed | |
1740 | * for cases NEXTHOP_TYPE_IPV4 and NEXTHOP_TYPE_IPV6. | |
1741 | */ | |
f70da2a3 DS |
1742 | for (i = 0; i < nexthop_num; i++) { |
1743 | struct nexthop *nexthop; | |
1744 | enum lsp_types_t label_type; | |
1745 | char nhbuf[NEXTHOP_STRLEN]; | |
1746 | char labelbuf[MPLS_LABEL_STRLEN]; | |
1747 | struct zapi_nexthop *api_nh = &nhops[i]; | |
7fcb24bb | 1748 | |
62e46303 | 1749 | /* Convert zapi nexthop */ |
2f35a820 | 1750 | nexthop = nexthop_from_zapi(api_nh, flags, p, backup_nh_num); |
62e46303 MS |
1751 | if (!nexthop) { |
1752 | flog_warn( | |
1753 | EC_ZEBRA_NEXTHOP_CREATION_FAILED, | |
f70da2a3 DS |
1754 | "%s: Nexthops Specified: %u(%u) but we failed to properly create one", |
1755 | __func__, nexthop_num, i); | |
1756 | if (ng) | |
1757 | nexthop_group_delete(&ng); | |
1758 | if (bnhg) | |
1759 | zebra_nhg_backup_free(&bnhg); | |
1760 | return false; | |
62e46303 | 1761 | } |
7fcb24bb | 1762 | |
ff9aca4f SW |
1763 | if (bnhg |
1764 | && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) { | |
f70da2a3 DS |
1765 | if (IS_ZEBRA_DEBUG_RECV) { |
1766 | nexthop2str(nexthop, nhbuf, sizeof(nhbuf)); | |
1767 | zlog_debug("%s: backup nh %s with BACKUP flag!", | |
1768 | __func__, nhbuf); | |
1769 | } | |
1770 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP); | |
1771 | nexthop->backup_num = 0; | |
1772 | } | |
1773 | ||
2f35a820 | 1774 | if (CHECK_FLAG(message, ZAPI_MESSAGE_SRTE)) { |
31f937fb SM |
1775 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_SRTE); |
1776 | nexthop->srte_color = api_nh->srte_color; | |
1777 | } | |
1778 | ||
62e46303 MS |
1779 | /* MPLS labels for BGP-LU or Segment Routing */ |
1780 | if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL) | |
1781 | && api_nh->type != NEXTHOP_TYPE_IFINDEX | |
1782 | && api_nh->type != NEXTHOP_TYPE_BLACKHOLE | |
1783 | && api_nh->label_num > 0) { | |
7fcb24bb | 1784 | |
62e46303 | 1785 | label_type = lsp_type_from_re_type(client->proto); |
62e46303 MS |
1786 | nexthop_add_labels(nexthop, label_type, |
1787 | api_nh->label_num, | |
1788 | &api_nh->labels[0]); | |
bf094f69 | 1789 | } |
bf094f69 | 1790 | |
c60c1ade | 1791 | if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6LOCAL) |
8689b25a HS |
1792 | && api_nh->type != NEXTHOP_TYPE_BLACKHOLE) { |
1793 | if (IS_ZEBRA_DEBUG_RECV) | |
1794 | zlog_debug("%s: adding seg6local action %s", | |
1795 | __func__, | |
1796 | seg6local_action2str( | |
1797 | api_nh->seg6local_action)); | |
1798 | ||
eab0f8f0 HS |
1799 | nexthop_add_srv6_seg6local(nexthop, |
1800 | api_nh->seg6local_action, | |
1801 | &api_nh->seg6local_ctx); | |
8689b25a HS |
1802 | } |
1803 | ||
c60c1ade | 1804 | if (CHECK_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_SEG6) |
76fb7ae4 HS |
1805 | && api_nh->type != NEXTHOP_TYPE_BLACKHOLE) { |
1806 | if (IS_ZEBRA_DEBUG_RECV) | |
1807 | zlog_debug("%s: adding seg6", __func__); | |
1808 | ||
eab0f8f0 | 1809 | nexthop_add_srv6_seg6(nexthop, &api_nh->seg6_segs); |
76fb7ae4 HS |
1810 | } |
1811 | ||
1d48702e MS |
1812 | if (IS_ZEBRA_DEBUG_RECV) { |
1813 | labelbuf[0] = '\0'; | |
1814 | nhbuf[0] = '\0'; | |
1815 | ||
1816 | nexthop2str(nexthop, nhbuf, sizeof(nhbuf)); | |
1817 | ||
1818 | if (nexthop->nh_label && | |
1819 | nexthop->nh_label->num_labels > 0) { | |
1820 | mpls_label2str(nexthop->nh_label->num_labels, | |
1821 | nexthop->nh_label->label, | |
1822 | labelbuf, sizeof(labelbuf), | |
1823 | false); | |
1824 | } | |
1825 | ||
1826 | zlog_debug("%s: nh=%s, vrf_id=%d %s", | |
1827 | __func__, nhbuf, api_nh->vrf_id, labelbuf); | |
1828 | } | |
1829 | ||
66c28560 | 1830 | if (ng) { |
f70da2a3 | 1831 | /* Add new nexthop to temporary list. This list is |
ff9aca4f SW |
1832 | * canonicalized - sorted - so that it can be hashed |
1833 | * later in route processing. We expect that the sender | |
1834 | * has sent the list sorted, and the zapi client api | |
1835 | * attempts to enforce that, so this should be | |
1836 | * inexpensive - but it is necessary to support shared | |
1837 | * nexthop-groups. | |
f70da2a3 DS |
1838 | */ |
1839 | nexthop_group_add_sorted(ng, nexthop); | |
1840 | } | |
1841 | if (bnhg) { | |
ff9aca4f SW |
1842 | /* Note that the order of the backup nexthops is |
1843 | * significant, so we don't sort this list as we do the | |
1844 | * primary nexthops, we just append. | |
f70da2a3 DS |
1845 | */ |
1846 | if (last_nh) | |
1847 | NEXTHOP_APPEND(last_nh, nexthop); | |
1848 | else | |
1849 | bnhg->nhe->nhg.nexthop = nexthop; | |
1850 | ||
1851 | last_nh = nexthop; | |
1852 | } | |
62e46303 MS |
1853 | } |
1854 | ||
66c28560 SW |
1855 | |
1856 | /* succesfully read, set caller pointers now */ | |
1857 | if (png) | |
1858 | *png = ng; | |
1859 | ||
1860 | if (pbnhg) | |
1861 | *pbnhg = bnhg; | |
1862 | ||
f70da2a3 DS |
1863 | return true; |
1864 | } | |
62e46303 | 1865 | |
5898ce6f | 1866 | static int zapi_nhg_decode(struct stream *s, int cmd, struct zapi_nhg *api_nhg) |
21735352 SW |
1867 | { |
1868 | uint16_t i; | |
1869 | struct zapi_nexthop *znh; | |
1870 | ||
1871 | STREAM_GETW(s, api_nhg->proto); | |
1872 | STREAM_GETL(s, api_nhg->id); | |
1873 | ||
1874 | if (cmd == ZEBRA_NHG_DEL) | |
1875 | goto done; | |
1876 | ||
1877 | /* Nexthops */ | |
1878 | STREAM_GETW(s, api_nhg->nexthop_num); | |
1879 | ||
1880 | if (zserv_nexthop_num_warn(__func__, NULL, api_nhg->nexthop_num)) | |
1881 | return -1; | |
1882 | ||
1883 | if (api_nhg->nexthop_num <= 0) { | |
1884 | flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED, | |
1885 | "%s: No nexthops sent", __func__); | |
1886 | return -1; | |
1887 | } | |
1888 | ||
1889 | for (i = 0; i < api_nhg->nexthop_num; i++) { | |
1890 | znh = &((api_nhg->nexthops)[i]); | |
1891 | ||
1892 | if (zapi_nexthop_decode(s, znh, 0, 0) != 0) { | |
1893 | flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED, | |
1894 | "%s: Nexthop creation failed", __func__); | |
1895 | return -1; | |
1896 | } | |
1897 | } | |
1898 | ||
1899 | /* Backup Nexthops */ | |
1900 | STREAM_GETW(s, api_nhg->backup_nexthop_num); | |
1901 | ||
1902 | if (zserv_nexthop_num_warn(__func__, NULL, api_nhg->backup_nexthop_num)) | |
1903 | return -1; | |
1904 | ||
1905 | for (i = 0; i < api_nhg->backup_nexthop_num; i++) { | |
1906 | znh = &((api_nhg->backup_nexthops)[i]); | |
1907 | ||
1908 | if (zapi_nexthop_decode(s, znh, 0, 0) != 0) { | |
1909 | flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED, | |
1910 | "%s: Backup Nexthop creation failed", | |
1911 | __func__); | |
1912 | return -1; | |
1913 | } | |
1914 | } | |
1915 | ||
1916 | done: | |
1917 | return 0; | |
1918 | ||
1919 | stream_failure: | |
1920 | flog_warn( | |
1921 | EC_ZEBRA_NEXTHOP_CREATION_FAILED, | |
1922 | "%s: Nexthop Group decode failed with some sort of stream read failure", | |
1923 | __func__); | |
1924 | return -1; | |
1925 | } | |
1926 | ||
2f35a820 DS |
1927 | static void zread_nhg_del(ZAPI_HANDLER_ARGS) |
1928 | { | |
21735352 SW |
1929 | struct stream *s; |
1930 | struct zapi_nhg api_nhg = {}; | |
0885b1e3 | 1931 | struct nhg_hash_entry *nhe; |
2f35a820 | 1932 | |
21735352 SW |
1933 | s = msg; |
1934 | if (zapi_nhg_decode(s, hdr->command, &api_nhg) < 0) { | |
1935 | if (IS_ZEBRA_DEBUG_RECV) | |
1936 | zlog_debug("%s: Unable to decode zapi_nhg sent", | |
1937 | __func__); | |
1938 | return; | |
1939 | } | |
2f35a820 DS |
1940 | |
1941 | /* | |
1942 | * Delete the received nhg id | |
2f35a820 | 1943 | */ |
21735352 | 1944 | nhe = zebra_nhg_proto_del(api_nhg.id, api_nhg.proto); |
0885b1e3 SW |
1945 | |
1946 | if (nhe) { | |
6fae63d2 | 1947 | zebra_nhg_decrement_ref(nhe); |
ee94437e MS |
1948 | zsend_nhg_notify(api_nhg.proto, client->instance, |
1949 | client->session_id, api_nhg.id, | |
1950 | ZAPI_NHG_REMOVED); | |
0885b1e3 | 1951 | } else |
ee94437e MS |
1952 | zsend_nhg_notify(api_nhg.proto, client->instance, |
1953 | client->session_id, api_nhg.id, | |
1954 | ZAPI_NHG_REMOVE_FAIL); | |
2f35a820 DS |
1955 | } |
1956 | ||
8b2d3a0f | 1957 | static void zread_nhg_add(ZAPI_HANDLER_ARGS) |
2f35a820 DS |
1958 | { |
1959 | struct stream *s; | |
21735352 | 1960 | struct zapi_nhg api_nhg = {}; |
2f35a820 | 1961 | struct nexthop_group *nhg = NULL; |
21735352 | 1962 | struct nhg_backup_info *bnhg = NULL; |
0885b1e3 | 1963 | struct nhg_hash_entry *nhe; |
2f35a820 | 1964 | |
2f35a820 | 1965 | s = msg; |
21735352 SW |
1966 | if (zapi_nhg_decode(s, hdr->command, &api_nhg) < 0) { |
1967 | if (IS_ZEBRA_DEBUG_RECV) | |
1968 | zlog_debug("%s: Unable to decode zapi_nhg sent", | |
1969 | __func__); | |
68671c74 SW |
1970 | return; |
1971 | } | |
1972 | ||
21735352 SW |
1973 | if ((!zapi_read_nexthops(client, NULL, api_nhg.nexthops, 0, 0, |
1974 | api_nhg.nexthop_num, | |
1975 | api_nhg.backup_nexthop_num, &nhg, NULL)) | |
1976 | || (!zapi_read_nexthops(client, NULL, api_nhg.backup_nexthops, 0, 0, | |
1977 | api_nhg.backup_nexthop_num, | |
1978 | api_nhg.backup_nexthop_num, NULL, &bnhg))) { | |
2f35a820 | 1979 | |
2f35a820 | 1980 | flog_warn(EC_ZEBRA_NEXTHOP_CREATION_FAILED, |
ff9aca4f | 1981 | "%s: Nexthop Group Creation failed", __func__); |
fd99142a MS |
1982 | |
1983 | /* Free any local allocations */ | |
1984 | nexthop_group_delete(&nhg); | |
1985 | zebra_nhg_backup_free(&bnhg); | |
1986 | ||
2f35a820 DS |
1987 | return; |
1988 | } | |
0885b1e3 | 1989 | |
04bec7b2 MS |
1990 | /* Create a temporary nhe */ |
1991 | nhe = zebra_nhg_alloc(); | |
1992 | nhe->id = api_nhg.id; | |
1993 | nhe->type = api_nhg.proto; | |
1994 | nhe->zapi_instance = client->instance; | |
1995 | nhe->zapi_session = client->session_id; | |
0885b1e3 | 1996 | |
04bec7b2 MS |
1997 | /* Take over the list(s) of nexthops */ |
1998 | nhe->nhg.nexthop = nhg->nexthop; | |
1999 | nhg->nexthop = NULL; | |
2000 | ||
2001 | if (bnhg) { | |
2002 | nhe->backup_info = bnhg; | |
2003 | bnhg = NULL; | |
2004 | } | |
0885b1e3 SW |
2005 | |
2006 | /* | |
2007 | * TODO: | |
2008 | * Assume fully resolved for now and install. | |
0885b1e3 SW |
2009 | * Resolution is going to need some more work. |
2010 | */ | |
ee94437e | 2011 | |
04bec7b2 MS |
2012 | /* Enqueue to workqueue for processing */ |
2013 | rib_queue_nhe_add(nhe); | |
2014 | ||
2015 | /* Free any local allocations */ | |
2016 | nexthop_group_delete(&nhg); | |
2017 | zebra_nhg_backup_free(&bnhg); | |
2018 | ||
2f35a820 DS |
2019 | } |
2020 | ||
f70da2a3 DS |
2021 | static void zread_route_add(ZAPI_HANDLER_ARGS) |
2022 | { | |
2023 | struct stream *s; | |
2024 | struct zapi_route api; | |
2025 | afi_t afi; | |
2026 | struct prefix_ipv6 *src_p = NULL; | |
2027 | struct route_entry *re; | |
2028 | struct nexthop_group *ng = NULL; | |
2029 | struct nhg_backup_info *bnhg = NULL; | |
2030 | int ret; | |
2031 | vrf_id_t vrf_id; | |
d7ac4c4d | 2032 | struct nhg_hash_entry nhe, *n = NULL; |
0eb97b86 | 2033 | |
f70da2a3 DS |
2034 | s = msg; |
2035 | if (zapi_route_decode(s, &api) < 0) { | |
2036 | if (IS_ZEBRA_DEBUG_RECV) | |
2037 | zlog_debug("%s: Unable to decode zapi_route sent", | |
2038 | __func__); | |
2039 | return; | |
2040 | } | |
1d48702e | 2041 | |
f70da2a3 | 2042 | vrf_id = zvrf_id(zvrf); |
31f937fb | 2043 | |
2dbe669b DA |
2044 | if (IS_ZEBRA_DEBUG_RECV) |
2045 | zlog_debug("%s: p=(%u:%u)%pFX, msg flags=0x%x, flags=0x%x", | |
2046 | __func__, vrf_id, api.tableid, &api.prefix, | |
f70da2a3 | 2047 | (int)api.message, api.flags); |
0eb97b86 | 2048 | |
f70da2a3 | 2049 | /* Allocate new route. */ |
b0385873 DS |
2050 | re = zebra_rib_route_entry_new( |
2051 | vrf_id, api.type, api.instance, api.flags, api.nhgid, | |
2052 | api.tableid ? api.tableid : zvrf->table_id, api.metric, api.mtu, | |
2053 | api.distance, api.tag); | |
1d48702e | 2054 | |
27141ea9 DS |
2055 | if (!CHECK_FLAG(api.message, ZAPI_MESSAGE_NHG) |
2056 | && (!CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP) | |
2057 | || api.nexthop_num == 0)) { | |
ff9aca4f SW |
2058 | flog_warn( |
2059 | EC_ZEBRA_RX_ROUTE_NO_NEXTHOPS, | |
2060 | "%s: received a route without nexthops for prefix %pFX from client %s", | |
2061 | __func__, &api.prefix, | |
2062 | zebra_route_string(client->proto)); | |
1d48702e | 2063 | |
f70da2a3 DS |
2064 | XFREE(MTYPE_RE, re); |
2065 | return; | |
2066 | } | |
1d48702e | 2067 | |
f70da2a3 | 2068 | /* Report misuse of the backup flag */ |
ff9aca4f SW |
2069 | if (CHECK_FLAG(api.message, ZAPI_MESSAGE_BACKUP_NEXTHOPS) |
2070 | && api.backup_nexthop_num == 0) { | |
f70da2a3 | 2071 | if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_EVENT) |
ff9aca4f SW |
2072 | zlog_debug( |
2073 | "%s: client %s: BACKUP flag set but no backup nexthops, prefix %pFX", | |
2074 | __func__, zebra_route_string(client->proto), | |
2075 | &api.prefix); | |
f70da2a3 | 2076 | } |
62e46303 | 2077 | |
50db3f2f | 2078 | if (!re->nhe_id |
2b5ecd4c SW |
2079 | && (!zapi_read_nexthops(client, &api.prefix, api.nexthops, |
2080 | api.flags, api.message, api.nexthop_num, | |
2081 | api.backup_nexthop_num, &ng, NULL) | |
2082 | || !zapi_read_nexthops(client, &api.prefix, api.backup_nexthops, | |
2083 | api.flags, api.message, | |
2084 | api.backup_nexthop_num, | |
2085 | api.backup_nexthop_num, NULL, &bnhg))) { | |
21735352 SW |
2086 | |
2087 | nexthop_group_delete(&ng); | |
2088 | zebra_nhg_backup_free(&bnhg); | |
f70da2a3 DS |
2089 | XFREE(MTYPE_RE, re); |
2090 | return; | |
7fcb24bb RW |
2091 | } |
2092 | ||
a29a6001 | 2093 | if (CHECK_FLAG(api.message, ZAPI_MESSAGE_OPAQUE)) { |
b86c1f4f MS |
2094 | re->opaque = |
2095 | XMALLOC(MTYPE_RE_OPAQUE, | |
2096 | sizeof(struct re_opaque) + api.opaque.length); | |
a29a6001 DS |
2097 | re->opaque->length = api.opaque.length; |
2098 | memcpy(re->opaque->data, api.opaque.data, re->opaque->length); | |
2099 | } | |
2100 | ||
bf094f69 QY |
2101 | afi = family2afi(api.prefix.family); |
2102 | if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) { | |
e914ccbe | 2103 | flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI, |
9df414fe | 2104 | "%s: Received SRC Prefix but afi is not v6", |
15569c58 | 2105 | __func__); |
0eb97b86 | 2106 | nexthop_group_delete(&ng); |
1d48702e | 2107 | zebra_nhg_backup_free(&bnhg); |
b86c1f4f | 2108 | XFREE(MTYPE_RE_OPAQUE, re->opaque); |
bf094f69 QY |
2109 | XFREE(MTYPE_RE, re); |
2110 | return; | |
2111 | } | |
2112 | if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) | |
2113 | src_p = &api.src_prefix; | |
2114 | ||
5e7939a5 DS |
2115 | if (api.safi != SAFI_UNICAST && api.safi != SAFI_MULTICAST) { |
2116 | flog_warn(EC_LIB_ZAPI_MISSMATCH, | |
2117 | "%s: Received safi: %d but we can only accept UNICAST or MULTICAST", | |
2118 | __func__, api.safi); | |
2119 | nexthop_group_delete(&ng); | |
1d48702e | 2120 | zebra_nhg_backup_free(&bnhg); |
b86c1f4f | 2121 | XFREE(MTYPE_RE_OPAQUE, re->opaque); |
5e7939a5 DS |
2122 | XFREE(MTYPE_RE, re); |
2123 | return; | |
2124 | } | |
1d48702e | 2125 | |
50db3f2f SW |
2126 | /* |
2127 | * If we have an ID, this proto owns the NHG it sent along with the | |
2128 | * route, so we just send the ID into rib code with it. | |
2129 | * | |
2130 | * Havent figured out how to handle backup NHs with this yet, so lets | |
2131 | * keep that separate. | |
2132 | * Include backup info with the route. We use a temporary nhe here; | |
377e29f7 MS |
2133 | * if this is a new/unknown nhe, a new copy will be allocated |
2134 | * and stored. | |
2135 | */ | |
50db3f2f SW |
2136 | if (!re->nhe_id) { |
2137 | zebra_nhe_init(&nhe, afi, ng->nexthop); | |
2138 | nhe.nhg.nexthop = ng->nexthop; | |
2139 | nhe.backup_info = bnhg; | |
d7ac4c4d | 2140 | n = zebra_nhe_copy(&nhe, 0); |
50db3f2f | 2141 | } |
d7ac4c4d DS |
2142 | ret = rib_add_multipath_nhe(afi, api.safi, &api.prefix, src_p, re, n, |
2143 | false); | |
bf094f69 | 2144 | |
f94a7703 DS |
2145 | /* |
2146 | * rib_add_multipath_nhe only fails in a couple spots | |
2147 | * and in those spots we have not freed memory | |
2148 | */ | |
38c764dd DS |
2149 | if (ret == -1) { |
2150 | client->error_cnt++; | |
b86c1f4f | 2151 | XFREE(MTYPE_RE_OPAQUE, re->opaque); |
f94a7703 | 2152 | XFREE(MTYPE_RE, re); |
38c764dd | 2153 | } |
f94a7703 | 2154 | |
377e29f7 MS |
2155 | /* At this point, these allocations are not needed: 're' has been |
2156 | * retained or freed, and if 're' still exists, it is using | |
2157 | * a reference to a shared group object. | |
2158 | */ | |
2159 | nexthop_group_delete(&ng); | |
2160 | if (bnhg) | |
2161 | zebra_nhg_backup_free(&bnhg); | |
2162 | ||
bf094f69 QY |
2163 | /* Stats */ |
2164 | switch (api.prefix.family) { | |
2165 | case AF_INET: | |
38c764dd | 2166 | if (ret == 0) |
bf094f69 | 2167 | client->v4_route_add_cnt++; |
38c764dd | 2168 | else if (ret == 1) |
bf094f69 QY |
2169 | client->v4_route_upd8_cnt++; |
2170 | break; | |
2171 | case AF_INET6: | |
38c764dd | 2172 | if (ret == 0) |
bf094f69 | 2173 | client->v6_route_add_cnt++; |
38c764dd | 2174 | else if (ret == 1) |
bf094f69 QY |
2175 | client->v6_route_upd8_cnt++; |
2176 | break; | |
2177 | } | |
2178 | } | |
2179 | ||
b86c1f4f | 2180 | void zapi_re_opaque_free(struct re_opaque *opaque) |
224ccf29 | 2181 | { |
b86c1f4f | 2182 | XFREE(MTYPE_RE_OPAQUE, opaque); |
224ccf29 DL |
2183 | } |
2184 | ||
bf094f69 QY |
2185 | static void zread_route_del(ZAPI_HANDLER_ARGS) |
2186 | { | |
2187 | struct stream *s; | |
2188 | struct zapi_route api; | |
2189 | afi_t afi; | |
2190 | struct prefix_ipv6 *src_p = NULL; | |
2191 | uint32_t table_id; | |
2192 | ||
2193 | s = msg; | |
2194 | if (zapi_route_decode(s, &api) < 0) | |
2195 | return; | |
2196 | ||
2197 | afi = family2afi(api.prefix.family); | |
2198 | if (afi != AFI_IP6 && CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) { | |
e914ccbe | 2199 | flog_warn(EC_ZEBRA_RX_SRCDEST_WRONG_AFI, |
9df414fe | 2200 | "%s: Received a src prefix while afi is not v6", |
15569c58 | 2201 | __func__); |
bf094f69 QY |
2202 | return; |
2203 | } | |
2204 | if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) | |
2205 | src_p = &api.src_prefix; | |
2206 | ||
60ca3cc2 | 2207 | if (api.tableid) |
bf094f69 QY |
2208 | table_id = api.tableid; |
2209 | else | |
2210 | table_id = zvrf->table_id; | |
2211 | ||
2dbe669b DA |
2212 | if (IS_ZEBRA_DEBUG_RECV) |
2213 | zlog_debug("%s: p=(%u:%u)%pFX, msg flags=0x%x, flags=0x%x", | |
2214 | __func__, zvrf_id(zvrf), table_id, &api.prefix, | |
c2c02b76 | 2215 | (int)api.message, api.flags); |
c2c02b76 | 2216 | |
bf094f69 | 2217 | rib_delete(afi, api.safi, zvrf_id(zvrf), api.type, api.instance, |
bc541126 | 2218 | api.flags, &api.prefix, src_p, NULL, 0, table_id, api.metric, |
3ceae22b | 2219 | api.distance, false); |
bf094f69 QY |
2220 | |
2221 | /* Stats */ | |
2222 | switch (api.prefix.family) { | |
2223 | case AF_INET: | |
2224 | client->v4_route_del_cnt++; | |
2225 | break; | |
2226 | case AF_INET6: | |
2227 | client->v6_route_del_cnt++; | |
2228 | break; | |
2229 | } | |
2230 | } | |
2231 | ||
bf094f69 | 2232 | /* MRIB Nexthop lookup for IPv4. */ |
34ee41c6 | 2233 | static void zread_nexthop_lookup_mrib(ZAPI_HANDLER_ARGS) |
bf094f69 | 2234 | { |
34ee41c6 DL |
2235 | struct ipaddr addr; |
2236 | struct route_entry *re = NULL; | |
2237 | ||
2238 | STREAM_GET_IPADDR(msg, &addr); | |
2239 | ||
2240 | switch (addr.ipa_type) { | |
2241 | case IPADDR_V4: | |
2242 | re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr.ipaddr_v4, | |
2243 | NULL); | |
2244 | break; | |
2245 | case IPADDR_V6: | |
2246 | re = rib_match_ipv6_multicast(zvrf_id(zvrf), addr.ipaddr_v6, | |
2247 | NULL); | |
2248 | break; | |
2249 | case IPADDR_NONE: | |
2250 | /* ??? */ | |
2251 | goto stream_failure; | |
2252 | } | |
bf094f69 | 2253 | |
34ee41c6 | 2254 | zsend_nexthop_lookup_mrib(client, &addr, re, zvrf); |
bf094f69 QY |
2255 | |
2256 | stream_failure: | |
2257 | return; | |
2258 | } | |
2259 | ||
bf094f69 QY |
2260 | /* Register zebra server router-id information. Send current router-id */ |
2261 | static void zread_router_id_add(ZAPI_HANDLER_ARGS) | |
2262 | { | |
98a3fb0a | 2263 | afi_t afi; |
bf094f69 | 2264 | struct prefix p; |
01141358 | 2265 | struct prefix zero; |
bf094f69 | 2266 | |
98a3fb0a SM |
2267 | STREAM_GETW(msg, afi); |
2268 | ||
2269 | if (afi <= AFI_UNSPEC || afi >= AFI_MAX) { | |
2270 | zlog_warn( | |
2271 | "Invalid AFI %u while registering for router ID notifications", | |
2272 | afi); | |
2273 | goto stream_failure; | |
2274 | } | |
2275 | ||
bf094f69 | 2276 | /* Router-id information is needed. */ |
98a3fb0a | 2277 | vrf_bitmap_set(client->ridinfo[afi], zvrf_id(zvrf)); |
bf094f69 | 2278 | |
98a3fb0a | 2279 | router_id_get(afi, &p, zvrf); |
bf094f69 | 2280 | |
01141358 DS |
2281 | /* |
2282 | * If we have not officially setup a router-id let's not | |
2283 | * tell the upper level protocol about it yet. | |
2284 | */ | |
2285 | memset(&zero, 0, sizeof(zero)); | |
2286 | if ((p.family == AF_INET && p.u.prefix4.s_addr == INADDR_ANY) | |
2287 | || (p.family == AF_INET6 | |
2288 | && memcmp(&p.u.prefix6, &zero.u.prefix6, | |
2289 | sizeof(struct in6_addr)) | |
2290 | == 0)) | |
2291 | return; | |
2292 | ||
98a3fb0a SM |
2293 | zsend_router_id_update(client, afi, &p, zvrf_id(zvrf)); |
2294 | ||
2295 | stream_failure: | |
2296 | return; | |
bf094f69 QY |
2297 | } |
2298 | ||
2299 | /* Unregister zebra server router-id information. */ | |
2300 | static void zread_router_id_delete(ZAPI_HANDLER_ARGS) | |
2301 | { | |
98a3fb0a SM |
2302 | afi_t afi; |
2303 | ||
2304 | STREAM_GETW(msg, afi); | |
2305 | ||
2306 | if (afi <= AFI_UNSPEC || afi >= AFI_MAX) { | |
2307 | zlog_warn( | |
2308 | "Invalid AFI %u while unregistering from router ID notifications", | |
2309 | afi); | |
2310 | goto stream_failure; | |
2311 | } | |
2312 | ||
2313 | vrf_bitmap_unset(client->ridinfo[afi], zvrf_id(zvrf)); | |
2314 | ||
2315 | stream_failure: | |
2316 | return; | |
bf094f69 QY |
2317 | } |
2318 | ||
2319 | static void zsend_capabilities(struct zserv *client, struct zebra_vrf *zvrf) | |
2320 | { | |
2321 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
2322 | ||
2323 | zclient_create_header(s, ZEBRA_CAPABILITIES, zvrf->vrf->vrf_id); | |
bb6b7f79 | 2324 | stream_putl(s, vrf_get_backend()); |
bf094f69 | 2325 | stream_putc(s, mpls_enabled); |
b3f2b590 | 2326 | stream_putl(s, zrouter.multipath_num); |
02c0866d | 2327 | stream_putc(s, zebra_mlag_get_role()); |
bf094f69 QY |
2328 | |
2329 | stream_putw_at(s, 0, stream_get_endp(s)); | |
21ccc0cf | 2330 | zserv_send_message(client, s); |
bf094f69 QY |
2331 | } |
2332 | ||
b120fe3b DS |
2333 | void zsend_capabilities_all_clients(void) |
2334 | { | |
2335 | struct listnode *node, *nnode; | |
2336 | struct zebra_vrf *zvrf; | |
2337 | struct zserv *client; | |
2338 | ||
2339 | zvrf = vrf_info_lookup(VRF_DEFAULT); | |
2340 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) { | |
17da84a4 KS |
2341 | /* Do not send unsolicited messages to synchronous clients. */ |
2342 | if (client->synchronous) | |
2343 | continue; | |
2344 | ||
b120fe3b DS |
2345 | zsend_capabilities(client, zvrf); |
2346 | } | |
2347 | } | |
2348 | ||
bf094f69 QY |
2349 | /* Tie up route-type and client->sock */ |
2350 | static void zread_hello(ZAPI_HANDLER_ARGS) | |
2351 | { | |
2352 | /* type of protocol (lib/zebra.h) */ | |
2353 | uint8_t proto; | |
2354 | unsigned short instance; | |
2355 | uint8_t notify; | |
17da84a4 | 2356 | uint8_t synchronous; |
4e0b5b31 | 2357 | uint32_t session_id; |
bf094f69 QY |
2358 | |
2359 | STREAM_GETC(msg, proto); | |
2360 | STREAM_GETW(msg, instance); | |
4e0b5b31 | 2361 | STREAM_GETL(msg, session_id); |
bf094f69 | 2362 | STREAM_GETC(msg, notify); |
17da84a4 | 2363 | STREAM_GETC(msg, synchronous); |
bf094f69 QY |
2364 | if (notify) |
2365 | client->notify_owner = true; | |
2366 | ||
17da84a4 KS |
2367 | if (synchronous) |
2368 | client->synchronous = true; | |
2369 | ||
bf094f69 | 2370 | /* accept only dynamic routing protocols */ |
f23cbcda | 2371 | if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_CONNECT)) { |
bf094f69 QY |
2372 | zlog_notice( |
2373 | "client %d says hello and bids fair to announce only %s routes vrf=%u", | |
2374 | client->sock, zebra_route_string(proto), | |
2375 | zvrf->vrf->vrf_id); | |
2376 | if (instance) | |
2377 | zlog_notice("client protocol instance %d", instance); | |
2378 | ||
2379 | client->proto = proto; | |
2380 | client->instance = instance; | |
4e0b5b31 | 2381 | client->session_id = session_id; |
6f4aee61 S |
2382 | |
2383 | /* Graceful restart processing for client connect */ | |
2384 | zebra_gr_client_reconnect(client); | |
bf094f69 QY |
2385 | } |
2386 | ||
17da84a4 KS |
2387 | if (!client->synchronous) { |
2388 | zsend_capabilities(client, zvrf); | |
2389 | zebra_vrf_update_all(client); | |
2390 | } | |
bf094f69 QY |
2391 | stream_failure: |
2392 | return; | |
2393 | } | |
2394 | ||
2395 | /* Unregister all information in a VRF. */ | |
2396 | static void zread_vrf_unregister(ZAPI_HANDLER_ARGS) | |
2397 | { | |
2398 | int i; | |
2399 | afi_t afi; | |
2400 | ||
49db7a7b | 2401 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { |
bf094f69 QY |
2402 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
2403 | vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf)); | |
49db7a7b | 2404 | vrf_bitmap_unset(client->redist_default[afi], zvrf_id(zvrf)); |
98a3fb0a | 2405 | vrf_bitmap_unset(client->ridinfo[afi], zvrf_id(zvrf)); |
7723e8d3 | 2406 | vrf_bitmap_unset(client->nhrp_neighinfo[afi], zvrf_id(zvrf)); |
49db7a7b | 2407 | } |
bf094f69 QY |
2408 | } |
2409 | ||
ff8d3c2d MS |
2410 | /* |
2411 | * Validate incoming zapi mpls lsp / labels message | |
2412 | */ | |
2413 | static int zapi_labels_validate(const struct zapi_labels *zl) | |
2414 | { | |
2415 | int ret = -1; | |
2416 | int i, j, idx; | |
2417 | uint32_t bits[8]; | |
2418 | uint32_t ival; | |
2419 | const struct zapi_nexthop *znh; | |
2420 | ||
2421 | /* Validate backup info: no duplicates for a single primary */ | |
2422 | if (zl->backup_nexthop_num == 0) { | |
2423 | ret = 0; | |
2424 | goto done; | |
2425 | } | |
2426 | ||
2427 | for (j = 0; j < zl->nexthop_num; j++) { | |
2428 | znh = &zl->nexthops[j]; | |
2429 | ||
2430 | memset(bits, 0, sizeof(bits)); | |
2431 | ||
2432 | for (i = 0; i < znh->backup_num; i++) { | |
2433 | idx = znh->backup_idx[i] / 32; | |
2434 | ||
2435 | ival = 1 << znh->backup_idx[i] % 32; | |
2436 | ||
2437 | /* Check whether value is already used */ | |
2438 | if (ival & bits[idx]) { | |
2439 | /* Fail */ | |
2440 | ||
2441 | if (IS_ZEBRA_DEBUG_RECV) | |
2442 | zlog_debug("%s: invalid zapi mpls message: duplicate backup nexthop index %d", | |
2443 | __func__, | |
2444 | znh->backup_idx[i]); | |
2445 | goto done; | |
2446 | } | |
2447 | ||
2448 | /* Mark index value */ | |
2449 | bits[idx] |= ival; | |
2450 | } | |
2451 | } | |
2452 | ||
2453 | ret = 0; | |
2454 | ||
2455 | done: | |
2456 | ||
2457 | return ret; | |
2458 | } | |
2459 | ||
ea6b290b RW |
2460 | /* |
2461 | * Handle request to create an MPLS LSP. | |
2462 | * | |
2463 | * A single message can fully specify an LSP with multiple nexthops. | |
2464 | * | |
2465 | * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is | |
2466 | * updated to use the received label(s). | |
2467 | */ | |
2468 | static void zread_mpls_labels_add(ZAPI_HANDLER_ARGS) | |
bf094f69 QY |
2469 | { |
2470 | struct stream *s; | |
bad6b0e7 | 2471 | struct zapi_labels zl; |
bf094f69 QY |
2472 | |
2473 | /* Get input stream. */ | |
2474 | s = msg; | |
bad6b0e7 RW |
2475 | if (zapi_labels_decode(s, &zl) < 0) { |
2476 | if (IS_ZEBRA_DEBUG_RECV) | |
2477 | zlog_debug("%s: Unable to decode zapi_labels sent", | |
15569c58 | 2478 | __func__); |
bf094f69 QY |
2479 | return; |
2480 | } | |
bf094f69 | 2481 | |
bf094f69 QY |
2482 | if (!mpls_enabled) |
2483 | return; | |
2484 | ||
ff8d3c2d MS |
2485 | /* Validate; will debug on failure */ |
2486 | if (zapi_labels_validate(&zl) < 0) | |
2487 | return; | |
2488 | ||
1548fbbc | 2489 | mpls_zapi_labels_process(true, zvrf, &zl); |
ea6b290b RW |
2490 | } |
2491 | ||
2492 | /* | |
2493 | * Handle request to delete an MPLS LSP. | |
2494 | * | |
2495 | * An LSP is identified by its type and local label. When the received message | |
2496 | * doesn't contain any nexthop, the whole LSP is deleted. Otherwise, only the | |
2497 | * listed LSP nexthops (aka NHLFEs) are deleted. | |
2498 | * | |
2499 | * When the optional ZAPI_LABELS_FTN flag is set, the labels of the specified | |
2500 | * FEC (route) nexthops are deleted. | |
2501 | */ | |
2502 | static void zread_mpls_labels_delete(ZAPI_HANDLER_ARGS) | |
2503 | { | |
2504 | struct stream *s; | |
2505 | struct zapi_labels zl; | |
2506 | ||
2507 | /* Get input stream. */ | |
2508 | s = msg; | |
2509 | if (zapi_labels_decode(s, &zl) < 0) { | |
2510 | if (IS_ZEBRA_DEBUG_RECV) | |
2511 | zlog_debug("%s: Unable to decode zapi_labels sent", | |
15569c58 | 2512 | __func__); |
ea6b290b RW |
2513 | return; |
2514 | } | |
2515 | ||
2516 | if (!mpls_enabled) | |
2517 | return; | |
2518 | ||
2519 | if (zl.nexthop_num > 0) { | |
1548fbbc | 2520 | mpls_zapi_labels_process(false /*delete*/, zvrf, &zl); |
ea6b290b RW |
2521 | } else { |
2522 | mpls_lsp_uninstall_all_vrf(zvrf, zl.type, zl.local_label); | |
2523 | ||
b3c49d0e | 2524 | if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN)) |
ea6b290b RW |
2525 | mpls_ftn_uninstall(zvrf, zl.type, &zl.route.prefix, |
2526 | zl.route.type, zl.route.instance); | |
2527 | } | |
2528 | } | |
2529 | ||
2530 | /* | |
2531 | * Handle request to add an MPLS LSP or change an existing one. | |
2532 | * | |
2533 | * A single message can fully specify an LSP with multiple nexthops. | |
2534 | * | |
2535 | * When the optional ZAPI_LABELS_FTN flag is set, the specified FEC (route) is | |
2536 | * updated to use the received label(s). | |
2537 | * | |
2538 | * NOTE: zebra will use route replace semantics (make-before-break) to update | |
2539 | * the LSP in the forwarding plane if that's supported by the underlying | |
2540 | * platform. | |
2541 | */ | |
2542 | static void zread_mpls_labels_replace(ZAPI_HANDLER_ARGS) | |
2543 | { | |
2544 | struct stream *s; | |
2545 | struct zapi_labels zl; | |
2546 | ||
2547 | /* Get input stream. */ | |
2548 | s = msg; | |
2549 | if (zapi_labels_decode(s, &zl) < 0) { | |
2550 | if (IS_ZEBRA_DEBUG_RECV) | |
2551 | zlog_debug("%s: Unable to decode zapi_labels sent", | |
15569c58 | 2552 | __func__); |
ea6b290b RW |
2553 | return; |
2554 | } | |
2555 | ||
2556 | if (!mpls_enabled) | |
2557 | return; | |
2558 | ||
ff8d3c2d MS |
2559 | /* Validate; will debug on failure */ |
2560 | if (zapi_labels_validate(&zl) < 0) | |
2561 | return; | |
2562 | ||
f2e7f4eb MS |
2563 | /* This removes everything, then re-adds from the client's |
2564 | * zapi message. Since the LSP will be processed later, on this | |
2565 | * this same pthread, all of the changes will 'appear' at once. | |
2566 | */ | |
ea6b290b RW |
2567 | mpls_lsp_uninstall_all_vrf(zvrf, zl.type, zl.local_label); |
2568 | if (CHECK_FLAG(zl.message, ZAPI_LABELS_FTN)) | |
2569 | mpls_ftn_uninstall(zvrf, zl.type, &zl.route.prefix, | |
2570 | zl.route.type, zl.route.instance); | |
2571 | ||
f2e7f4eb | 2572 | mpls_zapi_labels_process(true, zvrf, &zl); |
bf094f69 QY |
2573 | } |
2574 | ||
31f937fb SM |
2575 | static void zread_sr_policy_set(ZAPI_HANDLER_ARGS) |
2576 | { | |
2577 | struct stream *s; | |
2578 | struct zapi_sr_policy zp; | |
2579 | struct zapi_srte_tunnel *zt; | |
2580 | struct zebra_sr_policy *policy; | |
2581 | ||
2582 | /* Get input stream. */ | |
2583 | s = msg; | |
2584 | if (zapi_sr_policy_decode(s, &zp) < 0) { | |
2585 | if (IS_ZEBRA_DEBUG_RECV) | |
2586 | zlog_debug("%s: Unable to decode zapi_sr_policy sent", | |
7d7be47e | 2587 | __func__); |
31f937fb SM |
2588 | return; |
2589 | } | |
2590 | zt = &zp.segment_list; | |
2591 | if (zt->label_num < 1) { | |
2592 | if (IS_ZEBRA_DEBUG_RECV) | |
2593 | zlog_debug( | |
2594 | "%s: SR-TE tunnel must contain at least one label", | |
7d7be47e | 2595 | __func__); |
31f937fb SM |
2596 | return; |
2597 | } | |
2598 | ||
2599 | if (!mpls_enabled) | |
2600 | return; | |
2601 | ||
2602 | policy = zebra_sr_policy_find(zp.color, &zp.endpoint); | |
2603 | if (!policy) | |
2604 | policy = zebra_sr_policy_add(zp.color, &zp.endpoint, zp.name); | |
2605 | /* TODO: per-VRF list of SR-TE policies. */ | |
2606 | policy->zvrf = zvrf; | |
2607 | ||
2608 | zebra_sr_policy_validate(policy, &zp.segment_list); | |
2609 | } | |
2610 | ||
2611 | static void zread_sr_policy_delete(ZAPI_HANDLER_ARGS) | |
2612 | { | |
2613 | struct stream *s; | |
2614 | struct zapi_sr_policy zp; | |
2615 | struct zebra_sr_policy *policy; | |
2616 | ||
2617 | /* Get input stream. */ | |
2618 | s = msg; | |
2619 | if (zapi_sr_policy_decode(s, &zp) < 0) { | |
2620 | if (IS_ZEBRA_DEBUG_RECV) | |
2621 | zlog_debug("%s: Unable to decode zapi_sr_policy sent", | |
7d7be47e | 2622 | __func__); |
31f937fb SM |
2623 | return; |
2624 | } | |
2625 | ||
2626 | if (!mpls_enabled) | |
2627 | return; | |
2628 | ||
2629 | policy = zebra_sr_policy_find(zp.color, &zp.endpoint); | |
2630 | if (!policy) { | |
2631 | if (IS_ZEBRA_DEBUG_RECV) | |
7d7be47e | 2632 | zlog_debug("%s: Unable to find SR-TE policy", __func__); |
31f937fb SM |
2633 | return; |
2634 | } | |
2635 | ||
2636 | zebra_sr_policy_del(policy); | |
2637 | } | |
2638 | ||
2639 | int zsend_sr_policy_notify_status(uint32_t color, struct ipaddr *endpoint, | |
2640 | char *name, int status) | |
2641 | { | |
2642 | struct zserv *client; | |
2643 | struct stream *s; | |
2644 | ||
2645 | client = zserv_find_client(ZEBRA_ROUTE_SRTE, 0); | |
2646 | if (!client) { | |
2647 | if (IS_ZEBRA_DEBUG_PACKET) | |
2648 | zlog_debug( | |
2649 | "Not notifying pathd about policy %s" | |
2650 | " status change to %d", | |
2651 | name, status); | |
2652 | return 0; | |
2653 | } | |
2654 | ||
2655 | if (IS_ZEBRA_DEBUG_PACKET) | |
2656 | zlog_debug( | |
2657 | "Notifying pathd about policy %s status change" | |
2658 | " to %d", | |
2659 | name, status); | |
2660 | ||
2661 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
2662 | stream_reset(s); | |
2663 | ||
2664 | zclient_create_header(s, ZEBRA_SR_POLICY_NOTIFY_STATUS, VRF_DEFAULT); | |
2665 | stream_putl(s, color); | |
2666 | stream_put_ipaddr(s, endpoint); | |
2667 | stream_write(s, name, SRTE_POLICY_NAME_MAX_LENGTH); | |
2668 | stream_putl(s, status); | |
2669 | ||
2670 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2671 | ||
2672 | return zserv_send_message(client, s); | |
2673 | } | |
2674 | ||
581e797e KS |
2675 | /* Send client close notify to client */ |
2676 | int zsend_client_close_notify(struct zserv *client, struct zserv *closed_client) | |
2677 | { | |
2678 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
2679 | ||
2680 | zclient_create_header(s, ZEBRA_CLIENT_CLOSE_NOTIFY, VRF_DEFAULT); | |
2681 | ||
2682 | stream_putc(s, closed_client->proto); | |
2683 | stream_putw(s, closed_client->instance); | |
2684 | stream_putl(s, closed_client->session_id); | |
2685 | ||
2686 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2687 | ||
2688 | return zserv_send_message(client, s); | |
2689 | } | |
2690 | ||
6e68a084 HS |
2691 | int zsend_srv6_manager_get_locator_chunk_response(struct zserv *client, |
2692 | vrf_id_t vrf_id, | |
2693 | struct srv6_locator *loc) | |
2694 | { | |
1bda3e62 | 2695 | struct srv6_locator_chunk chunk = {}; |
6e68a084 HS |
2696 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); |
2697 | ||
ac6a9479 HS |
2698 | strlcpy(chunk.locator_name, loc->name, sizeof(chunk.locator_name)); |
2699 | chunk.prefix = loc->prefix; | |
2700 | chunk.block_bits_length = loc->block_bits_length; | |
2701 | chunk.node_bits_length = loc->node_bits_length; | |
2702 | chunk.function_bits_length = loc->function_bits_length; | |
2703 | chunk.argument_bits_length = loc->argument_bits_length; | |
2704 | chunk.keep = 0; | |
2705 | chunk.proto = client->proto; | |
2706 | chunk.instance = client->instance; | |
6e68a084 | 2707 | |
ac6a9479 HS |
2708 | zclient_create_header(s, ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK, vrf_id); |
2709 | zapi_srv6_locator_chunk_encode(s, &chunk); | |
6e68a084 | 2710 | stream_putw_at(s, 0, stream_get_endp(s)); |
6e68a084 HS |
2711 | return zserv_send_message(client, s); |
2712 | } | |
2713 | ||
bf094f69 QY |
2714 | /* Send response to a table manager connect request to client */ |
2715 | static void zread_table_manager_connect(struct zserv *client, | |
2716 | struct stream *msg, vrf_id_t vrf_id) | |
2717 | { | |
2718 | struct stream *s; | |
2719 | uint8_t proto; | |
2720 | uint16_t instance; | |
c479e756 | 2721 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
bf094f69 QY |
2722 | |
2723 | s = msg; | |
2724 | ||
2725 | /* Get data. */ | |
2726 | STREAM_GETC(s, proto); | |
2727 | STREAM_GETW(s, instance); | |
2728 | ||
2729 | /* accept only dynamic routing protocols */ | |
2730 | if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) { | |
e914ccbe | 2731 | flog_err(EC_ZEBRA_TM_WRONG_PROTO, |
1c50c1c0 QY |
2732 | "client %d has wrong protocol %s", client->sock, |
2733 | zebra_route_string(proto)); | |
bf094f69 QY |
2734 | zsend_table_manager_connect_response(client, vrf_id, 1); |
2735 | return; | |
2736 | } | |
c479e756 DS |
2737 | zlog_notice("client %d with vrf %s(%u) instance %u connected as %s", |
2738 | client->sock, VRF_LOGNAME(vrf), vrf_id, instance, | |
2739 | zebra_route_string(proto)); | |
bf094f69 QY |
2740 | client->proto = proto; |
2741 | client->instance = instance; | |
2742 | ||
2743 | /* | |
2744 | * Release previous labels of same protocol and instance. | |
2745 | * This is done in case it restarted from an unexpected shutdown. | |
2746 | */ | |
453844ab | 2747 | release_daemon_table_chunks(client); |
bf094f69 QY |
2748 | |
2749 | zsend_table_manager_connect_response(client, vrf_id, 0); | |
2750 | ||
2751 | stream_failure: | |
2752 | return; | |
2753 | } | |
2754 | ||
2755 | static void zread_label_manager_connect(struct zserv *client, | |
2756 | struct stream *msg, vrf_id_t vrf_id) | |
2757 | { | |
2758 | struct stream *s; | |
2759 | /* type of protocol (lib/zebra.h) */ | |
2760 | uint8_t proto; | |
2761 | unsigned short instance; | |
2762 | ||
2763 | /* Get input stream. */ | |
2764 | s = msg; | |
2765 | ||
2766 | /* Get data. */ | |
2767 | STREAM_GETC(s, proto); | |
2768 | STREAM_GETW(s, instance); | |
2769 | ||
2770 | /* accept only dynamic routing protocols */ | |
2771 | if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) { | |
e914ccbe | 2772 | flog_err(EC_ZEBRA_TM_WRONG_PROTO, |
1c50c1c0 QY |
2773 | "client %d has wrong protocol %s", client->sock, |
2774 | zebra_route_string(proto)); | |
e11d7c96 | 2775 | zsend_label_manager_connect_response(client, vrf_id, 1); |
bf094f69 QY |
2776 | return; |
2777 | } | |
e11d7c96 EDP |
2778 | |
2779 | /* recall proto and instance in this socket */ | |
bf094f69 QY |
2780 | client->proto = proto; |
2781 | client->instance = instance; | |
2782 | ||
e11d7c96 | 2783 | /* call hook for connection using wrapper */ |
4cebdb9b | 2784 | lm_client_connect_call(client, vrf_id); |
bf094f69 QY |
2785 | |
2786 | stream_failure: | |
2787 | return; | |
2788 | } | |
2789 | ||
2790 | static void zread_get_label_chunk(struct zserv *client, struct stream *msg, | |
2791 | vrf_id_t vrf_id) | |
2792 | { | |
2793 | struct stream *s; | |
2794 | uint8_t keep; | |
0e3b6a92 | 2795 | uint32_t size, base; |
e11d7c96 | 2796 | struct label_manager_chunk *lmc = NULL; |
5dffb0e9 FR |
2797 | uint8_t proto; |
2798 | unsigned short instance; | |
bf094f69 QY |
2799 | |
2800 | /* Get input stream. */ | |
2801 | s = msg; | |
2802 | ||
2803 | /* Get data. */ | |
5dffb0e9 FR |
2804 | STREAM_GETC(s, proto); |
2805 | STREAM_GETW(s, instance); | |
bf094f69 QY |
2806 | STREAM_GETC(s, keep); |
2807 | STREAM_GETL(s, size); | |
0e3b6a92 | 2808 | STREAM_GETL(s, base); |
bf094f69 | 2809 | |
4cebdb9b MS |
2810 | assert(proto == client->proto && instance == client->instance); |
2811 | ||
e11d7c96 | 2812 | /* call hook to get a chunk using wrapper */ |
4cebdb9b | 2813 | lm_get_chunk_call(&lmc, client, keep, size, base, vrf_id); |
bf094f69 | 2814 | |
bf094f69 QY |
2815 | stream_failure: |
2816 | return; | |
2817 | } | |
2818 | ||
2819 | static void zread_release_label_chunk(struct zserv *client, struct stream *msg) | |
2820 | { | |
2821 | struct stream *s; | |
2822 | uint32_t start, end; | |
5dffb0e9 FR |
2823 | uint8_t proto; |
2824 | unsigned short instance; | |
bf094f69 QY |
2825 | |
2826 | /* Get input stream. */ | |
2827 | s = msg; | |
2828 | ||
2829 | /* Get data. */ | |
5dffb0e9 FR |
2830 | STREAM_GETC(s, proto); |
2831 | STREAM_GETW(s, instance); | |
bf094f69 QY |
2832 | STREAM_GETL(s, start); |
2833 | STREAM_GETL(s, end); | |
2834 | ||
4cebdb9b MS |
2835 | assert(proto == client->proto && instance == client->instance); |
2836 | ||
e11d7c96 | 2837 | /* call hook to release a chunk using wrapper */ |
4cebdb9b | 2838 | lm_release_chunk_call(client, start, end); |
bf094f69 QY |
2839 | |
2840 | stream_failure: | |
2841 | return; | |
2842 | } | |
e11d7c96 | 2843 | |
bf094f69 QY |
2844 | static void zread_label_manager_request(ZAPI_HANDLER_ARGS) |
2845 | { | |
e11d7c96 EDP |
2846 | if (hdr->command == ZEBRA_LABEL_MANAGER_CONNECT |
2847 | || hdr->command == ZEBRA_LABEL_MANAGER_CONNECT_ASYNC) | |
2848 | zread_label_manager_connect(client, msg, zvrf_id(zvrf)); | |
bf094f69 | 2849 | else { |
e11d7c96 EDP |
2850 | if (hdr->command == ZEBRA_GET_LABEL_CHUNK) |
2851 | zread_get_label_chunk(client, msg, zvrf_id(zvrf)); | |
2852 | else if (hdr->command == ZEBRA_RELEASE_LABEL_CHUNK) | |
2853 | zread_release_label_chunk(client, msg); | |
bf094f69 QY |
2854 | } |
2855 | } | |
2856 | ||
2857 | static void zread_get_table_chunk(struct zserv *client, struct stream *msg, | |
42d4b30e | 2858 | struct zebra_vrf *zvrf) |
bf094f69 QY |
2859 | { |
2860 | struct stream *s; | |
2861 | uint32_t size; | |
2862 | struct table_manager_chunk *tmc; | |
2863 | ||
2864 | /* Get input stream. */ | |
2865 | s = msg; | |
2866 | ||
2867 | /* Get data. */ | |
2868 | STREAM_GETL(s, size); | |
2869 | ||
42d4b30e | 2870 | tmc = assign_table_chunk(client->proto, client->instance, size, zvrf); |
bf094f69 | 2871 | if (!tmc) |
e914ccbe | 2872 | flog_err(EC_ZEBRA_TM_CANNOT_ASSIGN_CHUNK, |
1c50c1c0 QY |
2873 | "%s: Unable to assign Table Chunk of size %u", |
2874 | __func__, size); | |
bf094f69 QY |
2875 | else |
2876 | zlog_debug("Assigned Table Chunk %u - %u", tmc->start, | |
2877 | tmc->end); | |
2878 | /* send response back */ | |
42d4b30e | 2879 | zsend_assign_table_chunk_response(client, zvrf_id(zvrf), tmc); |
bf094f69 QY |
2880 | |
2881 | stream_failure: | |
2882 | return; | |
2883 | } | |
2884 | ||
42d4b30e PG |
2885 | static void zread_release_table_chunk(struct zserv *client, struct stream *msg, |
2886 | struct zebra_vrf *zvrf) | |
bf094f69 QY |
2887 | { |
2888 | struct stream *s; | |
2889 | uint32_t start, end; | |
2890 | ||
2891 | /* Get input stream. */ | |
2892 | s = msg; | |
2893 | ||
2894 | /* Get data. */ | |
2895 | STREAM_GETL(s, start); | |
2896 | STREAM_GETL(s, end); | |
2897 | ||
42d4b30e | 2898 | release_table_chunk(client->proto, client->instance, start, end, zvrf); |
bf094f69 QY |
2899 | |
2900 | stream_failure: | |
2901 | return; | |
2902 | } | |
2903 | ||
2904 | static void zread_table_manager_request(ZAPI_HANDLER_ARGS) | |
2905 | { | |
16bd37d6 | 2906 | /* to avoid sending other messages like ZEBRA_INTERFACE_UP */ |
bf094f69 QY |
2907 | if (hdr->command == ZEBRA_TABLE_MANAGER_CONNECT) |
2908 | zread_table_manager_connect(client, msg, zvrf_id(zvrf)); | |
2909 | else { | |
2910 | /* Sanity: don't allow 'unidentified' requests */ | |
2911 | if (!client->proto) { | |
af4c2728 | 2912 | flog_err( |
e914ccbe | 2913 | EC_ZEBRA_TM_ALIENS, |
16bd37d6 | 2914 | "Got SRv6 request from an unidentified client"); |
bf094f69 QY |
2915 | return; |
2916 | } | |
2917 | if (hdr->command == ZEBRA_GET_TABLE_CHUNK) | |
42d4b30e | 2918 | zread_get_table_chunk(client, msg, zvrf); |
bf094f69 | 2919 | else if (hdr->command == ZEBRA_RELEASE_TABLE_CHUNK) |
42d4b30e | 2920 | zread_release_table_chunk(client, msg, zvrf); |
bf094f69 QY |
2921 | } |
2922 | } | |
2923 | ||
6e68a084 HS |
2924 | static void zread_srv6_manager_get_locator_chunk(struct zserv *client, |
2925 | struct stream *msg, | |
2926 | vrf_id_t vrf_id) | |
2927 | { | |
2928 | struct stream *s = msg; | |
6e68a084 HS |
2929 | uint16_t len; |
2930 | char locator_name[SRV6_LOCNAME_SIZE] = {0}; | |
2931 | ||
2932 | /* Get data. */ | |
6e68a084 HS |
2933 | STREAM_GETW(s, len); |
2934 | STREAM_GET(locator_name, s, len); | |
2935 | ||
6e68a084 HS |
2936 | /* call hook to get a chunk using wrapper */ |
2937 | struct srv6_locator *loc = NULL; | |
2938 | srv6_manager_get_locator_chunk_call(&loc, client, locator_name, vrf_id); | |
2939 | ||
2940 | stream_failure: | |
2941 | return; | |
2942 | } | |
2943 | ||
2944 | static void zread_srv6_manager_release_locator_chunk(struct zserv *client, | |
2945 | struct stream *msg, | |
2946 | vrf_id_t vrf_id) | |
2947 | { | |
2948 | struct stream *s = msg; | |
6e68a084 HS |
2949 | uint16_t len; |
2950 | char locator_name[SRV6_LOCNAME_SIZE] = {0}; | |
2951 | ||
2952 | /* Get data. */ | |
6e68a084 HS |
2953 | STREAM_GETW(s, len); |
2954 | STREAM_GET(locator_name, s, len); | |
2955 | ||
6e68a084 HS |
2956 | /* call hook to release a chunk using wrapper */ |
2957 | srv6_manager_release_locator_chunk_call(client, locator_name, vrf_id); | |
2958 | ||
2959 | stream_failure: | |
2960 | return; | |
2961 | } | |
2962 | ||
2963 | static void zread_srv6_manager_request(ZAPI_HANDLER_ARGS) | |
2964 | { | |
2965 | switch (hdr->command) { | |
6e68a084 HS |
2966 | case ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK: |
2967 | zread_srv6_manager_get_locator_chunk(client, msg, | |
2968 | zvrf_id(zvrf)); | |
2969 | break; | |
2970 | case ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK: | |
2971 | zread_srv6_manager_release_locator_chunk(client, msg, | |
2972 | zvrf_id(zvrf)); | |
2973 | break; | |
2974 | default: | |
9f900cda | 2975 | zlog_err("%s: unknown SRv6 Manager command", __func__); |
6e68a084 HS |
2976 | break; |
2977 | } | |
2978 | } | |
2979 | ||
bf094f69 QY |
2980 | static void zread_pseudowire(ZAPI_HANDLER_ARGS) |
2981 | { | |
2982 | struct stream *s; | |
3682bd90 | 2983 | char ifname[INTERFACE_NAMSIZ]; |
bf094f69 QY |
2984 | ifindex_t ifindex; |
2985 | int type; | |
2986 | int af; | |
2987 | union g_addr nexthop; | |
2988 | uint32_t local_label; | |
2989 | uint32_t remote_label; | |
2990 | uint8_t flags; | |
2991 | union pw_protocol_fields data; | |
2992 | uint8_t protocol; | |
2993 | struct zebra_pw *pw; | |
2994 | ||
2995 | /* Get input stream. */ | |
2996 | s = msg; | |
2997 | ||
2998 | /* Get data. */ | |
3682bd90 RZ |
2999 | STREAM_GET(ifname, s, INTERFACE_NAMSIZ); |
3000 | ifname[INTERFACE_NAMSIZ - 1] = '\0'; | |
bf094f69 QY |
3001 | STREAM_GETL(s, ifindex); |
3002 | STREAM_GETL(s, type); | |
3003 | STREAM_GETL(s, af); | |
3004 | switch (af) { | |
3005 | case AF_INET: | |
3006 | STREAM_GET(&nexthop.ipv4.s_addr, s, IPV4_MAX_BYTELEN); | |
3007 | break; | |
3008 | case AF_INET6: | |
3009 | STREAM_GET(&nexthop.ipv6, s, 16); | |
3010 | break; | |
3011 | default: | |
3012 | return; | |
3013 | } | |
3014 | STREAM_GETL(s, local_label); | |
3015 | STREAM_GETL(s, remote_label); | |
3016 | STREAM_GETC(s, flags); | |
3017 | STREAM_GET(&data, s, sizeof(data)); | |
3018 | protocol = client->proto; | |
3019 | ||
3020 | pw = zebra_pw_find(zvrf, ifname); | |
3021 | switch (hdr->command) { | |
3022 | case ZEBRA_PW_ADD: | |
3023 | if (pw) { | |
e914ccbe | 3024 | flog_warn(EC_ZEBRA_PSEUDOWIRE_EXISTS, |
9df414fe | 3025 | "%s: pseudowire %s already exists [%s]", |
bf094f69 QY |
3026 | __func__, ifname, |
3027 | zserv_command_string(hdr->command)); | |
3028 | return; | |
3029 | } | |
3030 | ||
3031 | zebra_pw_add(zvrf, ifname, protocol, client); | |
3032 | break; | |
3033 | case ZEBRA_PW_DELETE: | |
3034 | if (!pw) { | |
e914ccbe | 3035 | flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT, |
9df414fe | 3036 | "%s: pseudowire %s not found [%s]", __func__, |
bf094f69 QY |
3037 | ifname, zserv_command_string(hdr->command)); |
3038 | return; | |
3039 | } | |
3040 | ||
3041 | zebra_pw_del(zvrf, pw); | |
3042 | break; | |
3043 | case ZEBRA_PW_SET: | |
3044 | case ZEBRA_PW_UNSET: | |
3045 | if (!pw) { | |
e914ccbe | 3046 | flog_warn(EC_ZEBRA_PSEUDOWIRE_NONEXISTENT, |
9df414fe | 3047 | "%s: pseudowire %s not found [%s]", __func__, |
bf094f69 QY |
3048 | ifname, zserv_command_string(hdr->command)); |
3049 | return; | |
3050 | } | |
3051 | ||
3052 | switch (hdr->command) { | |
3053 | case ZEBRA_PW_SET: | |
3054 | pw->enabled = 1; | |
3055 | break; | |
3056 | case ZEBRA_PW_UNSET: | |
3057 | pw->enabled = 0; | |
3058 | break; | |
3059 | } | |
3060 | ||
3061 | zebra_pw_change(pw, ifindex, type, af, &nexthop, local_label, | |
3062 | remote_label, flags, &data); | |
3063 | break; | |
3064 | } | |
3065 | ||
3066 | stream_failure: | |
3067 | return; | |
3068 | } | |
3069 | ||
3070 | static void zread_interface_set_master(ZAPI_HANDLER_ARGS) | |
3071 | { | |
3072 | struct interface *master; | |
3073 | struct interface *slave; | |
3074 | struct stream *s = msg; | |
3075 | int ifindex; | |
3076 | vrf_id_t vrf_id; | |
3077 | ||
3078 | STREAM_GETL(s, vrf_id); | |
3079 | STREAM_GETL(s, ifindex); | |
3080 | master = if_lookup_by_index(ifindex, vrf_id); | |
3081 | ||
3082 | STREAM_GETL(s, vrf_id); | |
3083 | STREAM_GETL(s, ifindex); | |
3084 | slave = if_lookup_by_index(ifindex, vrf_id); | |
3085 | ||
3086 | if (!master || !slave) | |
3087 | return; | |
3088 | ||
3089 | kernel_interface_set_master(master, slave); | |
3090 | ||
3091 | stream_failure: | |
3092 | return; | |
3093 | } | |
3094 | ||
3095 | ||
3096 | static void zread_vrf_label(ZAPI_HANDLER_ARGS) | |
3097 | { | |
3098 | struct interface *ifp; | |
3099 | mpls_label_t nlabel; | |
3100 | afi_t afi; | |
3101 | struct stream *s; | |
3102 | struct zebra_vrf *def_zvrf; | |
3103 | enum lsp_types_t ltype; | |
3104 | ||
3105 | s = msg; | |
3106 | STREAM_GETL(s, nlabel); | |
3107 | STREAM_GETC(s, afi); | |
663d3a91 QY |
3108 | |
3109 | if (!(IS_VALID_AFI(afi))) { | |
3110 | zlog_warn("Invalid AFI for VRF label: %u", afi); | |
3111 | return; | |
3112 | } | |
3113 | ||
bf094f69 QY |
3114 | if (nlabel == zvrf->label[afi]) { |
3115 | /* | |
3116 | * Nothing to do here move along | |
3117 | */ | |
3118 | return; | |
3119 | } | |
3120 | ||
3121 | STREAM_GETC(s, ltype); | |
3122 | ||
3123 | if (zvrf->vrf->vrf_id != VRF_DEFAULT) | |
a36898e7 | 3124 | ifp = if_lookup_by_name(zvrf->vrf->name, zvrf->vrf->vrf_id); |
bf094f69 | 3125 | else |
a36898e7 | 3126 | ifp = if_lookup_by_name("lo", VRF_DEFAULT); |
bf094f69 QY |
3127 | |
3128 | if (!ifp) { | |
3129 | zlog_debug("Unable to find specified Interface for %s", | |
3130 | zvrf->vrf->name); | |
3131 | return; | |
3132 | } | |
3133 | ||
3134 | def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT); | |
3135 | ||
3136 | if (zvrf->label[afi] != MPLS_LABEL_NONE) { | |
3137 | afi_t scrubber; | |
3138 | bool really_remove; | |
3139 | ||
3140 | really_remove = true; | |
3141 | for (scrubber = AFI_IP; scrubber < AFI_MAX; scrubber++) { | |
3142 | if (scrubber == afi) | |
3143 | continue; | |
3144 | ||
3145 | if (zvrf->label[scrubber] == MPLS_LABEL_NONE) | |
3146 | continue; | |
3147 | ||
3148 | if (zvrf->label[afi] == zvrf->label[scrubber]) { | |
3149 | really_remove = false; | |
3150 | break; | |
3151 | } | |
3152 | } | |
3153 | ||
3154 | if (really_remove) | |
3155 | mpls_lsp_uninstall(def_zvrf, ltype, zvrf->label[afi], | |
3156 | NEXTHOP_TYPE_IFINDEX, NULL, | |
cc1b9746 | 3157 | ifp->ifindex, false /*backup*/); |
bf094f69 QY |
3158 | } |
3159 | ||
5065db0a RW |
3160 | if (nlabel != MPLS_LABEL_NONE) { |
3161 | mpls_label_t out_label = MPLS_LABEL_IMPLICIT_NULL; | |
3162 | mpls_lsp_install(def_zvrf, ltype, nlabel, 1, &out_label, | |
3163 | NEXTHOP_TYPE_IFINDEX, NULL, ifp->ifindex); | |
3164 | } | |
bf094f69 QY |
3165 | |
3166 | zvrf->label[afi] = nlabel; | |
06302ecb DS |
3167 | zvrf->label_proto[afi] = client->proto; |
3168 | ||
bf094f69 QY |
3169 | stream_failure: |
3170 | return; | |
3171 | } | |
3172 | ||
3173 | static inline void zread_rule(ZAPI_HANDLER_ARGS) | |
3174 | { | |
3175 | struct zebra_pbr_rule zpr; | |
3176 | struct stream *s; | |
3177 | uint32_t total, i; | |
58a1d249 | 3178 | char ifname[INTERFACE_NAMSIZ + 1] = {}; |
bf094f69 QY |
3179 | |
3180 | s = msg; | |
3181 | STREAM_GETL(s, total); | |
3182 | ||
3183 | for (i = 0; i < total; i++) { | |
3184 | memset(&zpr, 0, sizeof(zpr)); | |
3185 | ||
3186 | zpr.sock = client->sock; | |
3187 | zpr.rule.vrf_id = hdr->vrf_id; | |
3188 | STREAM_GETL(s, zpr.rule.seq); | |
3189 | STREAM_GETL(s, zpr.rule.priority); | |
3190 | STREAM_GETL(s, zpr.rule.unique); | |
f56697ef | 3191 | STREAM_GETC(s, zpr.rule.filter.ip_proto); |
bf094f69 QY |
3192 | STREAM_GETC(s, zpr.rule.filter.src_ip.family); |
3193 | STREAM_GETC(s, zpr.rule.filter.src_ip.prefixlen); | |
3194 | STREAM_GET(&zpr.rule.filter.src_ip.u.prefix, s, | |
3195 | prefix_blen(&zpr.rule.filter.src_ip)); | |
3196 | STREAM_GETW(s, zpr.rule.filter.src_port); | |
3197 | STREAM_GETC(s, zpr.rule.filter.dst_ip.family); | |
3198 | STREAM_GETC(s, zpr.rule.filter.dst_ip.prefixlen); | |
3199 | STREAM_GET(&zpr.rule.filter.dst_ip.u.prefix, s, | |
3200 | prefix_blen(&zpr.rule.filter.dst_ip)); | |
3201 | STREAM_GETW(s, zpr.rule.filter.dst_port); | |
01f23aff | 3202 | STREAM_GETC(s, zpr.rule.filter.dsfield); |
bf094f69 | 3203 | STREAM_GETL(s, zpr.rule.filter.fwmark); |
d70a31a3 EB |
3204 | |
3205 | STREAM_GETL(s, zpr.rule.action.queue_id); | |
3206 | STREAM_GETW(s, zpr.rule.action.vlan_id); | |
3207 | STREAM_GETW(s, zpr.rule.action.vlan_flags); | |
3208 | STREAM_GETW(s, zpr.rule.action.pcp); | |
3209 | ||
bf094f69 | 3210 | STREAM_GETL(s, zpr.rule.action.table); |
58a1d249 | 3211 | STREAM_GET(ifname, s, INTERFACE_NAMSIZ); |
bf094f69 | 3212 | |
58a1d249 DS |
3213 | strlcpy(zpr.ifname, ifname, sizeof(zpr.ifname)); |
3214 | strlcpy(zpr.rule.ifname, ifname, sizeof(zpr.rule.ifname)); | |
bf094f69 QY |
3215 | |
3216 | if (!is_default_prefix(&zpr.rule.filter.src_ip)) | |
3217 | zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_IP; | |
3218 | ||
3219 | if (!is_default_prefix(&zpr.rule.filter.dst_ip)) | |
3220 | zpr.rule.filter.filter_bm |= PBR_FILTER_DST_IP; | |
3221 | ||
3222 | if (zpr.rule.filter.src_port) | |
3223 | zpr.rule.filter.filter_bm |= PBR_FILTER_SRC_PORT; | |
3224 | ||
3225 | if (zpr.rule.filter.dst_port) | |
3226 | zpr.rule.filter.filter_bm |= PBR_FILTER_DST_PORT; | |
3227 | ||
01f23aff WC |
3228 | if (zpr.rule.filter.dsfield) |
3229 | zpr.rule.filter.filter_bm |= PBR_FILTER_DSFIELD; | |
3230 | ||
f56697ef DS |
3231 | if (zpr.rule.filter.ip_proto) |
3232 | zpr.rule.filter.filter_bm |= PBR_FILTER_IP_PROTOCOL; | |
3233 | ||
bf094f69 QY |
3234 | if (zpr.rule.filter.fwmark) |
3235 | zpr.rule.filter.filter_bm |= PBR_FILTER_FWMARK; | |
3236 | ||
4719fd76 QY |
3237 | if (!(zpr.rule.filter.src_ip.family == AF_INET |
3238 | || zpr.rule.filter.src_ip.family == AF_INET6)) { | |
cc815be7 | 3239 | zlog_warn( |
6cde4b45 | 3240 | "Unsupported PBR source IP family: %s (%hhu)", |
cc815be7 QY |
3241 | family2str(zpr.rule.filter.src_ip.family), |
3242 | zpr.rule.filter.src_ip.family); | |
4719fd76 QY |
3243 | return; |
3244 | } | |
3245 | if (!(zpr.rule.filter.dst_ip.family == AF_INET | |
3246 | || zpr.rule.filter.dst_ip.family == AF_INET6)) { | |
cec72d49 | 3247 | zlog_warn( |
6cde4b45 | 3248 | "Unsupported PBR destination IP family: %s (%hhu)", |
cec72d49 DA |
3249 | family2str(zpr.rule.filter.dst_ip.family), |
3250 | zpr.rule.filter.dst_ip.family); | |
4719fd76 QY |
3251 | return; |
3252 | } | |
3253 | ||
3254 | ||
7f0ea8a4 | 3255 | zpr.vrf_id = zvrf->vrf->vrf_id; |
bf094f69 | 3256 | if (hdr->command == ZEBRA_RULE_ADD) |
7f0ea8a4 | 3257 | zebra_pbr_add_rule(&zpr); |
bf094f69 | 3258 | else |
7f0ea8a4 | 3259 | zebra_pbr_del_rule(&zpr); |
bf094f69 QY |
3260 | } |
3261 | ||
3262 | stream_failure: | |
3263 | return; | |
3264 | } | |
3265 | ||
3266 | static inline void zread_ipset(ZAPI_HANDLER_ARGS) | |
3267 | { | |
3268 | struct zebra_pbr_ipset zpi; | |
3269 | struct stream *s; | |
3270 | uint32_t total, i; | |
3271 | ||
3272 | s = msg; | |
3273 | STREAM_GETL(s, total); | |
3274 | ||
3275 | for (i = 0; i < total; i++) { | |
3276 | memset(&zpi, 0, sizeof(zpi)); | |
3277 | ||
3278 | zpi.sock = client->sock; | |
be2028d1 | 3279 | zpi.vrf_id = zvrf->vrf->vrf_id; |
bf094f69 QY |
3280 | STREAM_GETL(s, zpi.unique); |
3281 | STREAM_GETL(s, zpi.type); | |
a60b7031 | 3282 | STREAM_GETC(s, zpi.family); |
bf094f69 QY |
3283 | STREAM_GET(&zpi.ipset_name, s, ZEBRA_IPSET_NAME_SIZE); |
3284 | ||
3285 | if (hdr->command == ZEBRA_IPSET_CREATE) | |
62f20a52 | 3286 | zebra_pbr_create_ipset(&zpi); |
bf094f69 | 3287 | else |
62f20a52 | 3288 | zebra_pbr_destroy_ipset(&zpi); |
bf094f69 QY |
3289 | } |
3290 | ||
3291 | stream_failure: | |
3292 | return; | |
3293 | } | |
3294 | ||
3295 | static inline void zread_ipset_entry(ZAPI_HANDLER_ARGS) | |
3296 | { | |
3297 | struct zebra_pbr_ipset_entry zpi; | |
3298 | struct zebra_pbr_ipset ipset; | |
3299 | struct stream *s; | |
3300 | uint32_t total, i; | |
3301 | ||
3302 | s = msg; | |
3303 | STREAM_GETL(s, total); | |
3304 | ||
3305 | for (i = 0; i < total; i++) { | |
3306 | memset(&zpi, 0, sizeof(zpi)); | |
3307 | memset(&ipset, 0, sizeof(ipset)); | |
3308 | ||
3309 | zpi.sock = client->sock; | |
3310 | STREAM_GETL(s, zpi.unique); | |
3311 | STREAM_GET(&ipset.ipset_name, s, ZEBRA_IPSET_NAME_SIZE); | |
261462c3 | 3312 | ipset.ipset_name[ZEBRA_IPSET_NAME_SIZE - 1] = '\0'; |
bf094f69 QY |
3313 | STREAM_GETC(s, zpi.src.family); |
3314 | STREAM_GETC(s, zpi.src.prefixlen); | |
3315 | STREAM_GET(&zpi.src.u.prefix, s, prefix_blen(&zpi.src)); | |
3316 | STREAM_GETC(s, zpi.dst.family); | |
3317 | STREAM_GETC(s, zpi.dst.prefixlen); | |
3318 | STREAM_GET(&zpi.dst.u.prefix, s, prefix_blen(&zpi.dst)); | |
3319 | ||
25d760c5 PG |
3320 | STREAM_GETW(s, zpi.src_port_min); |
3321 | STREAM_GETW(s, zpi.src_port_max); | |
3322 | STREAM_GETW(s, zpi.dst_port_min); | |
3323 | STREAM_GETW(s, zpi.dst_port_max); | |
3324 | STREAM_GETC(s, zpi.proto); | |
bf094f69 QY |
3325 | if (!is_default_prefix(&zpi.src)) |
3326 | zpi.filter_bm |= PBR_FILTER_SRC_IP; | |
3327 | ||
3328 | if (!is_default_prefix(&zpi.dst)) | |
3329 | zpi.filter_bm |= PBR_FILTER_DST_IP; | |
be729dd7 | 3330 | if (zpi.dst_port_min != 0 || zpi.proto == IPPROTO_ICMP) |
25d760c5 | 3331 | zpi.filter_bm |= PBR_FILTER_DST_PORT; |
be729dd7 | 3332 | if (zpi.src_port_min != 0 || zpi.proto == IPPROTO_ICMP) |
25d760c5 PG |
3333 | zpi.filter_bm |= PBR_FILTER_SRC_PORT; |
3334 | if (zpi.dst_port_max != 0) | |
3335 | zpi.filter_bm |= PBR_FILTER_DST_PORT_RANGE; | |
3336 | if (zpi.src_port_max != 0) | |
3337 | zpi.filter_bm |= PBR_FILTER_SRC_PORT_RANGE; | |
3338 | if (zpi.proto != 0) | |
3339 | zpi.filter_bm |= PBR_FILTER_PROTO; | |
bf094f69 | 3340 | |
9863725c QY |
3341 | if (!(zpi.dst.family == AF_INET |
3342 | || zpi.dst.family == AF_INET6)) { | |
cec72d49 | 3343 | zlog_warn( |
6cde4b45 | 3344 | "Unsupported PBR destination IP family: %s (%hhu)", |
cec72d49 | 3345 | family2str(zpi.dst.family), zpi.dst.family); |
9863725c QY |
3346 | goto stream_failure; |
3347 | } | |
3348 | if (!(zpi.src.family == AF_INET | |
3349 | || zpi.src.family == AF_INET6)) { | |
cec72d49 | 3350 | zlog_warn( |
6cde4b45 | 3351 | "Unsupported PBR source IP family: %s (%hhu)", |
cec72d49 | 3352 | family2str(zpi.src.family), zpi.src.family); |
9863725c QY |
3353 | goto stream_failure; |
3354 | } | |
3355 | ||
bf094f69 | 3356 | /* calculate backpointer */ |
62f20a52 DS |
3357 | zpi.backpointer = |
3358 | zebra_pbr_lookup_ipset_pername(ipset.ipset_name); | |
f096bae4 DS |
3359 | |
3360 | if (!zpi.backpointer) { | |
3361 | zlog_warn("ipset name specified: %s does not exist", | |
3362 | ipset.ipset_name); | |
3363 | goto stream_failure; | |
3364 | } | |
3365 | ||
bf094f69 | 3366 | if (hdr->command == ZEBRA_IPSET_ENTRY_ADD) |
62f20a52 | 3367 | zebra_pbr_add_ipset_entry(&zpi); |
bf094f69 | 3368 | else |
62f20a52 | 3369 | zebra_pbr_del_ipset_entry(&zpi); |
bf094f69 QY |
3370 | } |
3371 | ||
3372 | stream_failure: | |
3373 | return; | |
3374 | } | |
3375 | ||
05657ec2 | 3376 | |
7723e8d3 PG |
3377 | static inline void zebra_neigh_register(ZAPI_HANDLER_ARGS) |
3378 | { | |
3379 | afi_t afi; | |
3380 | ||
3381 | STREAM_GETW(msg, afi); | |
3382 | if (afi <= AFI_UNSPEC || afi >= AFI_MAX) { | |
3383 | zlog_warn( | |
3384 | "Invalid AFI %u while registering for neighbors notifications", | |
3385 | afi); | |
3386 | goto stream_failure; | |
3387 | } | |
3388 | vrf_bitmap_set(client->nhrp_neighinfo[afi], zvrf_id(zvrf)); | |
3389 | stream_failure: | |
3390 | return; | |
3391 | } | |
3392 | ||
3393 | static inline void zebra_neigh_unregister(ZAPI_HANDLER_ARGS) | |
3394 | { | |
3395 | afi_t afi; | |
3396 | ||
3397 | STREAM_GETW(msg, afi); | |
3398 | if (afi <= AFI_UNSPEC || afi >= AFI_MAX) { | |
3399 | zlog_warn( | |
3400 | "Invalid AFI %u while unregistering from neighbor notifications", | |
3401 | afi); | |
3402 | goto stream_failure; | |
3403 | } | |
3404 | vrf_bitmap_unset(client->nhrp_neighinfo[afi], zvrf_id(zvrf)); | |
3405 | stream_failure: | |
3406 | return; | |
3407 | } | |
3408 | ||
d17af8dd PG |
3409 | static inline void zebra_gre_get(ZAPI_HANDLER_ARGS) |
3410 | { | |
3411 | struct stream *s; | |
3412 | ifindex_t idx; | |
3413 | struct interface *ifp; | |
3414 | struct zebra_if *zebra_if = NULL; | |
3415 | struct zebra_l2info_gre *gre_info; | |
3416 | struct interface *ifp_link = NULL; | |
3417 | vrf_id_t vrf_id_link = VRF_UNKNOWN; | |
3418 | vrf_id_t vrf_id = zvrf->vrf->vrf_id; | |
3419 | ||
3420 | s = msg; | |
3421 | STREAM_GETL(s, idx); | |
3422 | ifp = if_lookup_by_index(idx, vrf_id); | |
3423 | ||
3424 | if (ifp) | |
3425 | zebra_if = ifp->info; | |
3426 | ||
3427 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
3428 | ||
3429 | zclient_create_header(s, ZEBRA_GRE_UPDATE, vrf_id); | |
3430 | ||
3431 | if (ifp && IS_ZEBRA_IF_GRE(ifp) && zebra_if) { | |
3432 | gre_info = &zebra_if->l2info.gre; | |
3433 | ||
3434 | stream_putl(s, idx); | |
3435 | stream_putl(s, gre_info->ikey); | |
3436 | stream_putl(s, gre_info->ikey); | |
3437 | stream_putl(s, gre_info->ifindex_link); | |
3438 | ||
3439 | ifp_link = if_lookup_by_index_per_ns( | |
3440 | zebra_ns_lookup(gre_info->link_nsid), | |
3441 | gre_info->ifindex_link); | |
3442 | if (ifp_link) | |
096f7609 | 3443 | vrf_id_link = ifp_link->vrf->vrf_id; |
d17af8dd PG |
3444 | stream_putl(s, vrf_id_link); |
3445 | stream_putl(s, gre_info->vtep_ip.s_addr); | |
3446 | stream_putl(s, gre_info->vtep_ip_remote.s_addr); | |
3447 | } else { | |
3448 | stream_putl(s, idx); | |
3449 | stream_putl(s, 0); | |
3450 | stream_putl(s, 0); | |
3451 | stream_putl(s, IFINDEX_INTERNAL); | |
3452 | stream_putl(s, VRF_UNKNOWN); | |
3453 | stream_putl(s, 0); | |
bcb68fe0 | 3454 | stream_putl(s, 0); |
d17af8dd PG |
3455 | } |
3456 | /* Write packet size. */ | |
3457 | stream_putw_at(s, 0, stream_get_endp(s)); | |
3458 | zserv_send_message(client, s); | |
3459 | ||
3460 | return; | |
3461 | stream_failure: | |
3462 | return; | |
3463 | } | |
3464 | ||
541025d6 PG |
3465 | static inline void zebra_configure_arp(ZAPI_HANDLER_ARGS) |
3466 | { | |
3467 | struct stream *s; | |
e18747a9 | 3468 | uint8_t fam; |
541025d6 PG |
3469 | ifindex_t idx; |
3470 | struct interface *ifp; | |
541025d6 PG |
3471 | |
3472 | s = msg; | |
3473 | STREAM_GETC(s, fam); | |
3474 | if (fam != AF_INET && fam != AF_INET6) | |
3475 | return; | |
3476 | STREAM_GETL(s, idx); | |
3477 | ifp = if_lookup_by_index_per_ns(zvrf->zns, idx); | |
3478 | if (!ifp) | |
3479 | return; | |
e18747a9 | 3480 | dplane_neigh_table_update(ifp, fam, 1, 0, 0); |
541025d6 PG |
3481 | stream_failure: |
3482 | return; | |
3483 | } | |
3484 | ||
d603c077 | 3485 | static inline void zebra_neigh_ip_add(ZAPI_HANDLER_ARGS) |
05657ec2 PG |
3486 | { |
3487 | struct stream *s; | |
d603c077 | 3488 | struct zapi_neigh_ip api = {}; |
05657ec2 | 3489 | int ret; |
0a27a2fe | 3490 | const struct interface *ifp; |
05657ec2 PG |
3491 | |
3492 | s = msg; | |
d603c077 | 3493 | ret = zclient_neigh_ip_decode(s, &api); |
05657ec2 PG |
3494 | if (ret < 0) |
3495 | return; | |
0a27a2fe PG |
3496 | ifp = if_lookup_by_index(api.index, zvrf_id(zvrf)); |
3497 | if (!ifp) | |
3498 | return; | |
3499 | dplane_neigh_ip_update(DPLANE_OP_NEIGH_IP_INSTALL, ifp, &api.ip_out, | |
d603c077 | 3500 | &api.ip_in, api.ndm_state, client->proto); |
05657ec2 PG |
3501 | } |
3502 | ||
0a27a2fe | 3503 | |
d603c077 | 3504 | static inline void zebra_neigh_ip_del(ZAPI_HANDLER_ARGS) |
05657ec2 PG |
3505 | { |
3506 | struct stream *s; | |
d603c077 | 3507 | struct zapi_neigh_ip api = {}; |
05657ec2 | 3508 | int ret; |
0a27a2fe | 3509 | struct interface *ifp; |
05657ec2 | 3510 | |
05657ec2 | 3511 | s = msg; |
d603c077 | 3512 | ret = zclient_neigh_ip_decode(s, &api); |
05657ec2 PG |
3513 | if (ret < 0) |
3514 | return; | |
0a27a2fe PG |
3515 | ifp = if_lookup_by_index(api.index, zvrf_id(zvrf)); |
3516 | if (!ifp) | |
3517 | return; | |
3518 | dplane_neigh_ip_update(DPLANE_OP_NEIGH_IP_DELETE, ifp, &api.ip_out, | |
d603c077 | 3519 | &api.ip_in, api.ndm_state, client->proto); |
05657ec2 PG |
3520 | } |
3521 | ||
3522 | ||
bf094f69 QY |
3523 | static inline void zread_iptable(ZAPI_HANDLER_ARGS) |
3524 | { | |
8b5c4dce QY |
3525 | struct zebra_pbr_iptable *zpi = |
3526 | XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_iptable)); | |
bf094f69 QY |
3527 | struct stream *s; |
3528 | ||
3529 | s = msg; | |
3530 | ||
8b5c4dce QY |
3531 | zpi->interface_name_list = list_new(); |
3532 | zpi->sock = client->sock; | |
3533 | zpi->vrf_id = zvrf->vrf->vrf_id; | |
3534 | STREAM_GETL(s, zpi->unique); | |
3535 | STREAM_GETL(s, zpi->type); | |
3536 | STREAM_GETL(s, zpi->filter_bm); | |
3537 | STREAM_GETL(s, zpi->action); | |
3538 | STREAM_GETL(s, zpi->fwmark); | |
3539 | STREAM_GET(&zpi->ipset_name, s, ZEBRA_IPSET_NAME_SIZE); | |
a60b7031 | 3540 | STREAM_GETC(s, zpi->family); |
8b5c4dce QY |
3541 | STREAM_GETW(s, zpi->pkt_len_min); |
3542 | STREAM_GETW(s, zpi->pkt_len_max); | |
3543 | STREAM_GETW(s, zpi->tcp_flags); | |
3544 | STREAM_GETW(s, zpi->tcp_mask_flags); | |
3545 | STREAM_GETC(s, zpi->dscp_value); | |
3546 | STREAM_GETC(s, zpi->fragment); | |
3547 | STREAM_GETC(s, zpi->protocol); | |
a60b7031 | 3548 | STREAM_GETW(s, zpi->flow_label); |
8b5c4dce QY |
3549 | STREAM_GETL(s, zpi->nb_interface); |
3550 | zebra_pbr_iptable_update_interfacelist(s, zpi); | |
bf094f69 QY |
3551 | |
3552 | if (hdr->command == ZEBRA_IPTABLE_ADD) | |
8b5c4dce | 3553 | zebra_pbr_add_iptable(zpi); |
bf094f69 | 3554 | else |
8b5c4dce QY |
3555 | zebra_pbr_del_iptable(zpi); |
3556 | ||
bf094f69 | 3557 | stream_failure: |
8b5c4dce QY |
3558 | zebra_pbr_iptable_free(zpi); |
3559 | zpi = NULL; | |
bf094f69 QY |
3560 | return; |
3561 | } | |
3562 | ||
d68e74b4 JU |
3563 | static inline void zread_neigh_discover(ZAPI_HANDLER_ARGS) |
3564 | { | |
3565 | struct stream *s; | |
3566 | ifindex_t ifindex; | |
3567 | struct interface *ifp; | |
3568 | struct prefix p; | |
3569 | struct ipaddr ip; | |
3570 | ||
3571 | s = msg; | |
3572 | ||
3573 | STREAM_GETL(s, ifindex); | |
3574 | ||
3575 | ifp = if_lookup_by_index_per_ns(zvrf->zns, ifindex); | |
3576 | if (!ifp) { | |
3577 | zlog_debug("Failed to lookup ifindex: %u", ifindex); | |
3578 | return; | |
3579 | } | |
3580 | ||
3581 | STREAM_GETC(s, p.family); | |
3582 | STREAM_GETC(s, p.prefixlen); | |
3583 | STREAM_GET(&p.u.prefix, s, prefix_blen(&p)); | |
3584 | ||
3585 | if (p.family == AF_INET) | |
3586 | SET_IPADDR_V4(&ip); | |
3587 | else | |
3588 | SET_IPADDR_V6(&ip); | |
3589 | ||
3590 | memcpy(&ip.ip.addr, &p.u.prefix, prefix_blen(&p)); | |
3591 | ||
3592 | dplane_neigh_discover(ifp, &ip); | |
3593 | ||
3594 | stream_failure: | |
3595 | return; | |
3596 | } | |
3597 | ||
b716ab61 PG |
3598 | static inline void zebra_gre_source_set(ZAPI_HANDLER_ARGS) |
3599 | { | |
3600 | struct stream *s; | |
3601 | ifindex_t idx, link_idx; | |
3602 | vrf_id_t link_vrf_id; | |
3603 | struct interface *ifp; | |
3604 | struct interface *ifp_link; | |
b716ab61 | 3605 | vrf_id_t vrf_id = zvrf->vrf->vrf_id; |
62b4b7e4 PG |
3606 | struct zebra_if *zif, *gre_zif; |
3607 | struct zebra_l2info_gre *gre_info; | |
db51f0cd | 3608 | unsigned int mtu; |
b716ab61 PG |
3609 | |
3610 | s = msg; | |
3611 | STREAM_GETL(s, idx); | |
3612 | ifp = if_lookup_by_index(idx, vrf_id); | |
3613 | STREAM_GETL(s, link_idx); | |
3614 | STREAM_GETL(s, link_vrf_id); | |
db51f0cd | 3615 | STREAM_GETL(s, mtu); |
62b4b7e4 | 3616 | |
b716ab61 PG |
3617 | ifp_link = if_lookup_by_index(link_idx, link_vrf_id); |
3618 | if (!ifp_link || !ifp) { | |
3619 | zlog_warn("GRE (index %u, VRF %u) or GRE link interface (index %u, VRF %u) not found, when setting GRE params", | |
3620 | idx, vrf_id, link_idx, link_vrf_id); | |
3621 | return; | |
3622 | } | |
62b4b7e4 PG |
3623 | |
3624 | if (!IS_ZEBRA_IF_GRE(ifp)) | |
3625 | return; | |
3626 | ||
3627 | gre_zif = (struct zebra_if *)ifp->info; | |
3628 | zif = (struct zebra_if *)ifp_link->info; | |
3629 | if (!zif || !gre_zif) | |
3630 | return; | |
3631 | ||
3632 | gre_info = &zif->l2info.gre; | |
3633 | if (!gre_info) | |
3634 | return; | |
3635 | ||
db51f0cd PG |
3636 | if (!mtu) |
3637 | mtu = ifp->mtu; | |
3638 | ||
3639 | /* if gre link already set or mtu did not change, do not set it */ | |
3640 | if (gre_zif->link && gre_zif->link == ifp_link && mtu == ifp->mtu) | |
62b4b7e4 PG |
3641 | return; |
3642 | ||
e3d3fa06 | 3643 | dplane_gre_set(ifp, ifp_link, mtu, gre_info); |
b716ab61 PG |
3644 | |
3645 | stream_failure: | |
3646 | return; | |
3647 | } | |
3648 | ||
9ab0b2a3 SW |
3649 | static void zsend_error_msg(struct zserv *client, enum zebra_error_types error, |
3650 | struct zmsghdr *bad_hdr) | |
3651 | { | |
3652 | ||
3653 | struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
3654 | ||
3655 | zclient_create_header(s, ZEBRA_ERROR, bad_hdr->vrf_id); | |
3656 | ||
3657 | zserv_encode_error(s, error); | |
3658 | ||
3659 | client->error_cnt++; | |
3660 | zserv_send_message(client, s); | |
3661 | } | |
3662 | ||
3663 | static void zserv_error_no_vrf(ZAPI_HANDLER_ARGS) | |
3664 | { | |
3665 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) | |
3666 | zlog_debug("ZAPI message specifies unknown VRF: %d", | |
3667 | hdr->vrf_id); | |
3668 | ||
5ee080f0 | 3669 | zsend_error_msg(client, ZEBRA_NO_VRF, hdr); |
9ab0b2a3 SW |
3670 | } |
3671 | ||
3672 | static void zserv_error_invalid_msg_type(ZAPI_HANDLER_ARGS) | |
3673 | { | |
3674 | zlog_info("Zebra received unknown command %d", hdr->command); | |
3675 | ||
5ee080f0 | 3676 | zsend_error_msg(client, ZEBRA_INVALID_MSG_TYPE, hdr); |
9ab0b2a3 SW |
3677 | } |
3678 | ||
2b64873d | 3679 | void (*const zserv_handlers[])(ZAPI_HANDLER_ARGS) = { |
bf094f69 QY |
3680 | [ZEBRA_ROUTER_ID_ADD] = zread_router_id_add, |
3681 | [ZEBRA_ROUTER_ID_DELETE] = zread_router_id_delete, | |
3682 | [ZEBRA_INTERFACE_ADD] = zread_interface_add, | |
3683 | [ZEBRA_INTERFACE_DELETE] = zread_interface_delete, | |
c3bd894e | 3684 | [ZEBRA_INTERFACE_SET_PROTODOWN] = zread_interface_set_protodown, |
bf094f69 QY |
3685 | [ZEBRA_ROUTE_ADD] = zread_route_add, |
3686 | [ZEBRA_ROUTE_DELETE] = zread_route_del, | |
bf094f69 QY |
3687 | [ZEBRA_REDISTRIBUTE_ADD] = zebra_redistribute_add, |
3688 | [ZEBRA_REDISTRIBUTE_DELETE] = zebra_redistribute_delete, | |
3689 | [ZEBRA_REDISTRIBUTE_DEFAULT_ADD] = zebra_redistribute_default_add, | |
3690 | [ZEBRA_REDISTRIBUTE_DEFAULT_DELETE] = zebra_redistribute_default_delete, | |
34ee41c6 | 3691 | [ZEBRA_NEXTHOP_LOOKUP_MRIB] = zread_nexthop_lookup_mrib, |
bf094f69 QY |
3692 | [ZEBRA_HELLO] = zread_hello, |
3693 | [ZEBRA_NEXTHOP_REGISTER] = zread_rnh_register, | |
3694 | [ZEBRA_NEXTHOP_UNREGISTER] = zread_rnh_unregister, | |
bf094f69 QY |
3695 | [ZEBRA_BFD_DEST_UPDATE] = zebra_ptm_bfd_dst_register, |
3696 | [ZEBRA_BFD_DEST_REGISTER] = zebra_ptm_bfd_dst_register, | |
3697 | [ZEBRA_BFD_DEST_DEREGISTER] = zebra_ptm_bfd_dst_deregister, | |
d3af6147 RZ |
3698 | #if HAVE_BFDD > 0 |
3699 | [ZEBRA_BFD_DEST_REPLAY] = zebra_ptm_bfd_dst_replay, | |
3700 | #endif /* HAVE_BFDD */ | |
bf094f69 QY |
3701 | [ZEBRA_VRF_UNREGISTER] = zread_vrf_unregister, |
3702 | [ZEBRA_VRF_LABEL] = zread_vrf_label, | |
3703 | [ZEBRA_BFD_CLIENT_REGISTER] = zebra_ptm_bfd_client_register, | |
bf094f69 QY |
3704 | [ZEBRA_INTERFACE_ENABLE_RADV] = zebra_interface_radv_enable, |
3705 | [ZEBRA_INTERFACE_DISABLE_RADV] = zebra_interface_radv_disable, | |
31f937fb SM |
3706 | [ZEBRA_SR_POLICY_SET] = zread_sr_policy_set, |
3707 | [ZEBRA_SR_POLICY_DELETE] = zread_sr_policy_delete, | |
ea6b290b RW |
3708 | [ZEBRA_MPLS_LABELS_ADD] = zread_mpls_labels_add, |
3709 | [ZEBRA_MPLS_LABELS_DELETE] = zread_mpls_labels_delete, | |
3710 | [ZEBRA_MPLS_LABELS_REPLACE] = zread_mpls_labels_replace, | |
bf094f69 QY |
3711 | [ZEBRA_IPMR_ROUTE_STATS] = zebra_ipmr_route_stats, |
3712 | [ZEBRA_LABEL_MANAGER_CONNECT] = zread_label_manager_request, | |
f533be73 | 3713 | [ZEBRA_LABEL_MANAGER_CONNECT_ASYNC] = zread_label_manager_request, |
bf094f69 QY |
3714 | [ZEBRA_GET_LABEL_CHUNK] = zread_label_manager_request, |
3715 | [ZEBRA_RELEASE_LABEL_CHUNK] = zread_label_manager_request, | |
3716 | [ZEBRA_FEC_REGISTER] = zread_fec_register, | |
3717 | [ZEBRA_FEC_UNREGISTER] = zread_fec_unregister, | |
3718 | [ZEBRA_ADVERTISE_DEFAULT_GW] = zebra_vxlan_advertise_gw_macip, | |
278e26de | 3719 | [ZEBRA_ADVERTISE_SVI_MACIP] = zebra_vxlan_advertise_svi_macip, |
bf094f69 QY |
3720 | [ZEBRA_ADVERTISE_SUBNET] = zebra_vxlan_advertise_subnet, |
3721 | [ZEBRA_ADVERTISE_ALL_VNI] = zebra_vxlan_advertise_all_vni, | |
ce5160c0 AK |
3722 | [ZEBRA_REMOTE_ES_VTEP_ADD] = zebra_evpn_proc_remote_es, |
3723 | [ZEBRA_REMOTE_ES_VTEP_DEL] = zebra_evpn_proc_remote_es, | |
7e5b0b2b MS |
3724 | [ZEBRA_REMOTE_VTEP_ADD] = zebra_vxlan_remote_vtep_add_zapi, |
3725 | [ZEBRA_REMOTE_VTEP_DEL] = zebra_vxlan_remote_vtep_del_zapi, | |
bf094f69 QY |
3726 | [ZEBRA_REMOTE_MACIP_ADD] = zebra_vxlan_remote_macip_add, |
3727 | [ZEBRA_REMOTE_MACIP_DEL] = zebra_vxlan_remote_macip_del, | |
3950b52c | 3728 | [ZEBRA_DUPLICATE_ADDR_DETECTION] = zebra_vxlan_dup_addr_detection, |
bf094f69 QY |
3729 | [ZEBRA_INTERFACE_SET_MASTER] = zread_interface_set_master, |
3730 | [ZEBRA_PW_ADD] = zread_pseudowire, | |
3731 | [ZEBRA_PW_DELETE] = zread_pseudowire, | |
3732 | [ZEBRA_PW_SET] = zread_pseudowire, | |
3733 | [ZEBRA_PW_UNSET] = zread_pseudowire, | |
3734 | [ZEBRA_RULE_ADD] = zread_rule, | |
3735 | [ZEBRA_RULE_DELETE] = zread_rule, | |
3736 | [ZEBRA_TABLE_MANAGER_CONNECT] = zread_table_manager_request, | |
3737 | [ZEBRA_GET_TABLE_CHUNK] = zread_table_manager_request, | |
3738 | [ZEBRA_RELEASE_TABLE_CHUNK] = zread_table_manager_request, | |
3739 | [ZEBRA_IPSET_CREATE] = zread_ipset, | |
3740 | [ZEBRA_IPSET_DESTROY] = zread_ipset, | |
3741 | [ZEBRA_IPSET_ENTRY_ADD] = zread_ipset_entry, | |
3742 | [ZEBRA_IPSET_ENTRY_DELETE] = zread_ipset_entry, | |
3743 | [ZEBRA_IPTABLE_ADD] = zread_iptable, | |
3744 | [ZEBRA_IPTABLE_DELETE] = zread_iptable, | |
fbac9605 | 3745 | [ZEBRA_VXLAN_FLOOD_CONTROL] = zebra_vxlan_flood_control, |
ecbbc3a7 | 3746 | [ZEBRA_VXLAN_SG_REPLAY] = zebra_vxlan_sg_replay, |
ee235396 SK |
3747 | [ZEBRA_MLAG_CLIENT_REGISTER] = zebra_mlag_client_register, |
3748 | [ZEBRA_MLAG_CLIENT_UNREGISTER] = zebra_mlag_client_unregister, | |
3749 | [ZEBRA_MLAG_FORWARD_MSG] = zebra_mlag_forward_client_msg, | |
6e68a084 HS |
3750 | [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK] = zread_srv6_manager_request, |
3751 | [ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK] = zread_srv6_manager_request, | |
d68e74b4 | 3752 | [ZEBRA_CLIENT_CAPABILITIES] = zread_client_capabilities, |
2f35a820 | 3753 | [ZEBRA_NEIGH_DISCOVER] = zread_neigh_discover, |
8b2d3a0f | 3754 | [ZEBRA_NHG_ADD] = zread_nhg_add, |
2f35a820 | 3755 | [ZEBRA_NHG_DEL] = zread_nhg_del, |
77b38a4a | 3756 | [ZEBRA_ROUTE_NOTIFY_REQUEST] = zread_route_notify_request, |
7bfa7d02 AK |
3757 | [ZEBRA_EVPN_REMOTE_NH_ADD] = zebra_evpn_proc_remote_nh, |
3758 | [ZEBRA_EVPN_REMOTE_NH_DEL] = zebra_evpn_proc_remote_nh, | |
d603c077 PG |
3759 | [ZEBRA_NEIGH_IP_ADD] = zebra_neigh_ip_add, |
3760 | [ZEBRA_NEIGH_IP_DEL] = zebra_neigh_ip_del, | |
7723e8d3 PG |
3761 | [ZEBRA_NHRP_NEIGH_REGISTER] = zebra_neigh_register, |
3762 | [ZEBRA_NHRP_NEIGH_UNREGISTER] = zebra_neigh_unregister, | |
541025d6 | 3763 | [ZEBRA_CONFIGURE_ARP] = zebra_configure_arp, |
d17af8dd | 3764 | [ZEBRA_GRE_GET] = zebra_gre_get, |
b716ab61 | 3765 | [ZEBRA_GRE_SOURCE_SET] = zebra_gre_source_set, |
2f35a820 | 3766 | }; |
bf094f69 | 3767 | |
79b3664a MS |
3768 | /* |
3769 | * Process a batch of zapi messages. | |
3770 | */ | |
3771 | void zserv_handle_commands(struct zserv *client, struct stream_fifo *fifo) | |
bf094f69 | 3772 | { |
904e0d88 QY |
3773 | struct zmsghdr hdr; |
3774 | struct zebra_vrf *zvrf; | |
79b3664a MS |
3775 | struct stream *msg; |
3776 | struct stream_fifo temp_fifo; | |
904e0d88 | 3777 | |
79b3664a | 3778 | stream_fifo_init(&temp_fifo); |
aa8cb964 | 3779 | |
79b3664a MS |
3780 | while (stream_fifo_head(fifo)) { |
3781 | msg = stream_fifo_pop(fifo); | |
bf094f69 | 3782 | |
79b3664a MS |
3783 | if (STREAM_READABLE(msg) > ZEBRA_MAX_PACKET_SIZ) { |
3784 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) | |
3785 | zlog_debug( | |
3786 | "ZAPI message is %zu bytes long but the maximum packet size is %u; dropping", | |
3787 | STREAM_READABLE(msg), | |
3788 | ZEBRA_MAX_PACKET_SIZ); | |
3789 | goto continue_loop; | |
3790 | } | |
3791 | ||
3792 | zapi_parse_header(msg, &hdr); | |
3793 | ||
ec64a77b DS |
3794 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV |
3795 | && IS_ZEBRA_DEBUG_DETAIL) | |
79b3664a | 3796 | zserv_log_message(NULL, msg, &hdr); |
49b3b01f | 3797 | |
79b3664a | 3798 | hdr.length -= ZEBRA_HEADER_SIZE; |
904e0d88 | 3799 | |
79b3664a MS |
3800 | /* Before checking for a handler function, check for |
3801 | * special messages that are handled in another module; | |
3802 | * we'll treat these as opaque. | |
3803 | */ | |
3804 | if (zebra_opaque_handles_msgid(hdr.command)) { | |
3805 | /* Reset message buffer */ | |
3806 | stream_set_getp(msg, 0); | |
3807 | ||
3808 | stream_fifo_push(&temp_fifo, msg); | |
3809 | ||
3810 | /* Continue without freeing the message */ | |
3811 | msg = NULL; | |
3812 | goto continue_loop; | |
3813 | } | |
3814 | ||
3815 | /* lookup vrf */ | |
3816 | zvrf = zebra_vrf_lookup_by_id(hdr.vrf_id); | |
3817 | if (!zvrf) { | |
3818 | zserv_error_no_vrf(client, &hdr, msg, zvrf); | |
3819 | goto continue_loop; | |
3820 | } | |
3821 | ||
3822 | if (hdr.command >= array_size(zserv_handlers) | |
3823 | || zserv_handlers[hdr.command] == NULL) { | |
3824 | zserv_error_invalid_msg_type(client, &hdr, msg, zvrf); | |
3825 | goto continue_loop; | |
3826 | } | |
3827 | ||
3828 | zserv_handlers[hdr.command](client, &hdr, msg, zvrf); | |
3829 | ||
3830 | continue_loop: | |
3831 | stream_free(msg); | |
3832 | } | |
904e0d88 | 3833 | |
79b3664a MS |
3834 | /* Dispatch any special messages from the temp fifo */ |
3835 | if (stream_fifo_head(&temp_fifo) != NULL) | |
3836 | zebra_opaque_enqueue_batch(&temp_fifo); | |
9ab0b2a3 | 3837 | |
79b3664a | 3838 | stream_fifo_deinit(&temp_fifo); |
bf094f69 | 3839 | } |