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