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