]>
Commit | Line | Data |
---|---|---|
e5c83d9b DS |
1 | /* |
2 | * Zebra connect code. | |
3 | * Copyright (C) 2018 Cumulus Networks, Inc. | |
4 | * Donald Sharp | |
5 | * | |
6 | * FRR is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * FRR is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | #include <zebra.h> | |
21 | ||
22 | #include "thread.h" | |
23 | #include "command.h" | |
24 | #include "network.h" | |
25 | #include "prefix.h" | |
26 | #include "routemap.h" | |
27 | #include "table.h" | |
28 | #include "stream.h" | |
29 | #include "memory.h" | |
30 | #include "zclient.h" | |
31 | #include "filter.h" | |
32 | #include "plist.h" | |
33 | #include "log.h" | |
34 | #include "nexthop.h" | |
35 | #include "nexthop_group.h" | |
36 | ||
37 | #include "pbr_nht.h" | |
38 | #include "pbr_map.h" | |
39 | #include "pbr_memory.h" | |
40 | #include "pbr_zebra.h" | |
41 | #include "pbr_debug.h" | |
be3b67b5 | 42 | #include "pbr_vrf.h" |
e5c83d9b DS |
43 | |
44 | DEFINE_MTYPE_STATIC(PBRD, PBR_INTERFACE, "PBR Interface") | |
45 | ||
46 | /* Zebra structure to hold current status. */ | |
d3765386 | 47 | struct zclient *zclient; |
e5c83d9b | 48 | |
b13e5ad6 | 49 | struct pbr_interface *pbr_if_new(struct interface *ifp) |
e5c83d9b DS |
50 | { |
51 | struct pbr_interface *pbr_ifp; | |
52 | ||
53 | zassert(ifp); | |
54 | zassert(!ifp->info); | |
55 | ||
56 | pbr_ifp = XCALLOC(MTYPE_PBR_INTERFACE, sizeof(*pbr_ifp)); | |
57 | ||
10a00758 DS |
58 | ifp->info = pbr_ifp; |
59 | return pbr_ifp; | |
e5c83d9b DS |
60 | } |
61 | ||
62 | /* Inteface addition message from zebra. */ | |
ef7bd2a3 | 63 | int pbr_ifp_create(struct interface *ifp) |
e5c83d9b | 64 | { |
15569c58 | 65 | DEBUGD(&pbr_dbg_zebra, "%s: %s", __func__, ifp->name); |
2f61710b | 66 | |
10a00758 DS |
67 | if (!ifp->info) |
68 | pbr_if_new(ifp); | |
e5c83d9b | 69 | |
be3b67b5 | 70 | /* Update nexthops tracked from a `set nexthop` command */ |
a106a408 RW |
71 | pbr_nht_nexthop_interface_update(ifp); |
72 | ||
be3b67b5 SW |
73 | pbr_map_policy_interface_update(ifp, true); |
74 | ||
e5c83d9b DS |
75 | return 0; |
76 | } | |
77 | ||
3c3c3252 | 78 | int pbr_ifp_destroy(struct interface *ifp) |
e5c83d9b | 79 | { |
15569c58 | 80 | DEBUGD(&pbr_dbg_zebra, "%s: %s", __func__, ifp->name); |
2f61710b | 81 | |
be3b67b5 SW |
82 | pbr_map_policy_interface_update(ifp, false); |
83 | ||
e5c83d9b DS |
84 | return 0; |
85 | } | |
86 | ||
121f9dee | 87 | static int interface_address_add(ZAPI_CALLBACK_ARGS) |
e5c83d9b | 88 | { |
2f61710b DS |
89 | struct connected *c; |
90 | char buf[PREFIX_STRLEN]; | |
91 | ||
121f9dee | 92 | c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); |
2f61710b | 93 | |
15569c58 | 94 | DEBUGD(&pbr_dbg_zebra, "%s: %s added %s", __func__, |
964c3dba DS |
95 | c ? c->ifp->name : "Unknown", |
96 | c ? prefix2str(c->address, buf, sizeof(buf)) : "Unknown"); | |
e5c83d9b DS |
97 | |
98 | return 0; | |
99 | } | |
100 | ||
121f9dee | 101 | static int interface_address_delete(ZAPI_CALLBACK_ARGS) |
e5c83d9b DS |
102 | { |
103 | struct connected *c; | |
2f61710b | 104 | char buf[PREFIX_STRLEN]; |
e5c83d9b | 105 | |
121f9dee | 106 | c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); |
e5c83d9b DS |
107 | |
108 | if (!c) | |
109 | return 0; | |
110 | ||
15569c58 | 111 | DEBUGD(&pbr_dbg_zebra, "%s: %s deleted %s", __func__, c->ifp->name, |
2f61710b DS |
112 | prefix2str(c->address, buf, sizeof(buf))); |
113 | ||
721c0857 | 114 | connected_free(&c); |
e5c83d9b DS |
115 | return 0; |
116 | } | |
117 | ||
ddbf3e60 | 118 | int pbr_ifp_up(struct interface *ifp) |
e5c83d9b | 119 | { |
15569c58 | 120 | DEBUGD(&pbr_dbg_zebra, "%s: %s is up", __func__, ifp->name); |
e5c83d9b | 121 | |
a106a408 RW |
122 | pbr_nht_nexthop_interface_update(ifp); |
123 | ||
e5c83d9b DS |
124 | return 0; |
125 | } | |
126 | ||
b0b69e59 | 127 | int pbr_ifp_down(struct interface *ifp) |
e5c83d9b | 128 | { |
15569c58 | 129 | DEBUGD(&pbr_dbg_zebra, "%s: %s is down", __func__, ifp->name); |
e5c83d9b | 130 | |
a106a408 RW |
131 | pbr_nht_nexthop_interface_update(ifp); |
132 | ||
e5c83d9b DS |
133 | return 0; |
134 | } | |
135 | ||
be3b67b5 SW |
136 | static int interface_vrf_update(ZAPI_CALLBACK_ARGS) |
137 | { | |
138 | struct interface *ifp; | |
139 | vrf_id_t new_vrf_id; | |
140 | ||
141 | ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id, | |
142 | &new_vrf_id); | |
143 | ||
144 | if (!ifp) { | |
145 | DEBUGD(&pbr_dbg_zebra, "%s: VRF change interface not found", | |
146 | __func__); | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
151 | DEBUGD(&pbr_dbg_zebra, "%s: %s VRF change %u -> %u", __func__, | |
152 | ifp->name, vrf_id, new_vrf_id); | |
153 | ||
154 | if_update_to_new_vrf(ifp, new_vrf_id); | |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
121f9dee | 159 | static int route_notify_owner(ZAPI_CALLBACK_ARGS) |
e5c83d9b DS |
160 | { |
161 | struct prefix p; | |
162 | enum zapi_route_notify_owner note; | |
163 | uint32_t table_id; | |
164 | char buf[PREFIX_STRLEN]; | |
165 | ||
e5c83d9b DS |
166 | if (!zapi_route_notify_decode(zclient->ibuf, &p, &table_id, ¬e)) |
167 | return -1; | |
168 | ||
2f61710b DS |
169 | prefix2str(&p, buf, sizeof(buf)); |
170 | ||
e5c83d9b DS |
171 | switch (note) { |
172 | case ZAPI_ROUTE_FAIL_INSTALL: | |
173 | DEBUGD(&pbr_dbg_zebra, | |
15569c58 DA |
174 | "%s: [%s] Route install failure for table: %u", __func__, |
175 | buf, table_id); | |
e5c83d9b DS |
176 | break; |
177 | case ZAPI_ROUTE_BETTER_ADMIN_WON: | |
178 | DEBUGD(&pbr_dbg_zebra, | |
179 | "%s: [%s] Route better admin distance won for table: %u", | |
15569c58 | 180 | __func__, buf, table_id); |
e5c83d9b DS |
181 | break; |
182 | case ZAPI_ROUTE_INSTALLED: | |
183 | DEBUGD(&pbr_dbg_zebra, | |
184 | "%s: [%s] Route installed succeeded for table: %u", | |
15569c58 | 185 | __func__, buf, table_id); |
e5c83d9b DS |
186 | pbr_nht_route_installed_for_table(table_id); |
187 | break; | |
188 | case ZAPI_ROUTE_REMOVED: | |
189 | DEBUGD(&pbr_dbg_zebra, | |
190 | "%s: [%s] Route Removed succeeded for table: %u", | |
15569c58 | 191 | __func__, buf, table_id); |
e5c83d9b DS |
192 | pbr_nht_route_removed_for_table(table_id); |
193 | break; | |
194 | case ZAPI_ROUTE_REMOVE_FAIL: | |
195 | DEBUGD(&pbr_dbg_zebra, | |
15569c58 DA |
196 | "%s: [%s] Route remove fail for table: %u", __func__, |
197 | buf, table_id); | |
e5c83d9b DS |
198 | break; |
199 | } | |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
121f9dee | 204 | static int rule_notify_owner(ZAPI_CALLBACK_ARGS) |
e5c83d9b DS |
205 | { |
206 | uint32_t seqno, priority, unique; | |
207 | enum zapi_rule_notify_owner note; | |
208 | struct pbr_map_sequence *pbrms; | |
37c606ff | 209 | struct pbr_map_interface *pmi; |
e5c83d9b | 210 | ifindex_t ifi; |
37c606ff | 211 | uint64_t installed; |
e5c83d9b DS |
212 | |
213 | if (!zapi_rule_notify_decode(zclient->ibuf, &seqno, &priority, &unique, | |
214 | &ifi, ¬e)) | |
215 | return -1; | |
216 | ||
37c606ff DS |
217 | pmi = NULL; |
218 | pbrms = pbrms_lookup_unique(unique, ifi, &pmi); | |
e5c83d9b DS |
219 | if (!pbrms) { |
220 | DEBUGD(&pbr_dbg_zebra, | |
15569c58 DA |
221 | "%s: Failure to lookup pbrms based upon %u", __func__, |
222 | unique); | |
e5c83d9b DS |
223 | return 0; |
224 | } | |
225 | ||
37c606ff DS |
226 | installed = 1 << pmi->install_bit; |
227 | ||
e5c83d9b DS |
228 | switch (note) { |
229 | case ZAPI_RULE_FAIL_INSTALL: | |
37c606ff | 230 | pbrms->installed &= ~installed; |
e5c83d9b DS |
231 | break; |
232 | case ZAPI_RULE_INSTALLED: | |
37c606ff | 233 | pbrms->installed |= installed; |
e5c83d9b | 234 | break; |
373dd3b5 | 235 | case ZAPI_RULE_FAIL_REMOVE: |
fde8af8d | 236 | /* Don't change state on rule removal failure */ |
fde8af8d | 237 | break; |
e5c83d9b | 238 | case ZAPI_RULE_REMOVED: |
0f03639d | 239 | pbrms->installed &= ~installed; |
e5c83d9b DS |
240 | break; |
241 | } | |
242 | ||
23e8679f SW |
243 | DEBUGD(&pbr_dbg_zebra, "%s: Received %s: %" PRIu64, __func__, |
244 | zapi_rule_notify_owner2str(note), pbrms->installed); | |
245 | ||
38e9ccde DS |
246 | pbr_map_final_interface_deletion(pbrms->parent, pmi); |
247 | ||
e5c83d9b DS |
248 | return 0; |
249 | } | |
250 | ||
251 | static void zebra_connected(struct zclient *zclient) | |
252 | { | |
15569c58 | 253 | DEBUGD(&pbr_dbg_zebra, "%s: Registering for fun and profit", __func__); |
e5c83d9b DS |
254 | zclient_send_reg_requests(zclient, VRF_DEFAULT); |
255 | } | |
256 | ||
257 | static void route_add_helper(struct zapi_route *api, struct nexthop_group nhg, | |
258 | uint8_t install_afi) | |
259 | { | |
260 | struct zapi_nexthop *api_nh; | |
2f61710b | 261 | char buf[PREFIX_STRLEN]; |
e5c83d9b DS |
262 | struct nexthop *nhop; |
263 | int i; | |
264 | ||
265 | api->prefix.family = install_afi; | |
266 | ||
2f61710b DS |
267 | DEBUGD(&pbr_dbg_zebra, "\tEncoding %s", |
268 | prefix2str(&api->prefix, buf, sizeof(buf))); | |
269 | ||
e5c83d9b DS |
270 | i = 0; |
271 | for (ALL_NEXTHOPS(nhg, nhop)) { | |
272 | api_nh = &api->nexthops[i]; | |
273 | api_nh->vrf_id = nhop->vrf_id; | |
274 | api_nh->type = nhop->type; | |
bd054c1a | 275 | api_nh->weight = nhop->weight; |
e5c83d9b DS |
276 | switch (nhop->type) { |
277 | case NEXTHOP_TYPE_IPV4: | |
278 | api_nh->gate.ipv4 = nhop->gate.ipv4; | |
279 | break; | |
280 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
281 | api_nh->gate.ipv4 = nhop->gate.ipv4; | |
282 | api_nh->ifindex = nhop->ifindex; | |
283 | break; | |
284 | case NEXTHOP_TYPE_IFINDEX: | |
285 | api_nh->ifindex = nhop->ifindex; | |
286 | break; | |
287 | case NEXTHOP_TYPE_IPV6: | |
288 | memcpy(&api_nh->gate.ipv6, &nhop->gate.ipv6, 16); | |
289 | break; | |
290 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
291 | api_nh->ifindex = nhop->ifindex; | |
292 | memcpy(&api_nh->gate.ipv6, &nhop->gate.ipv6, 16); | |
293 | break; | |
294 | case NEXTHOP_TYPE_BLACKHOLE: | |
295 | api_nh->bh_type = nhop->bh_type; | |
296 | break; | |
297 | } | |
298 | i++; | |
299 | } | |
300 | api->nexthop_num = i; | |
301 | ||
302 | zclient_route_send(ZEBRA_ROUTE_ADD, zclient, api); | |
303 | } | |
304 | ||
305 | /* | |
306 | * This function assumes a default route is being | |
307 | * installed into the appropriate tableid | |
308 | */ | |
309 | void route_add(struct pbr_nexthop_group_cache *pnhgc, struct nexthop_group nhg, | |
310 | afi_t install_afi) | |
311 | { | |
312 | struct zapi_route api; | |
313 | ||
15569c58 | 314 | DEBUGD(&pbr_dbg_zebra, "%s for Table: %d", __func__, pnhgc->table_id); |
2f61710b | 315 | |
e5c83d9b DS |
316 | memset(&api, 0, sizeof(api)); |
317 | ||
318 | api.vrf_id = VRF_DEFAULT; | |
319 | api.type = ZEBRA_ROUTE_PBR; | |
320 | api.safi = SAFI_UNICAST; | |
321 | /* | |
322 | * Sending a default route | |
323 | */ | |
324 | api.tableid = pnhgc->table_id; | |
325 | SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION); | |
326 | SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID); | |
327 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); | |
328 | switch (install_afi) { | |
329 | case AFI_MAX: | |
330 | route_add_helper(&api, nhg, AF_INET); | |
331 | route_add_helper(&api, nhg, AF_INET6); | |
332 | break; | |
333 | case AFI_IP: | |
334 | route_add_helper(&api, nhg, AF_INET); | |
335 | break; | |
336 | case AFI_IP6: | |
337 | route_add_helper(&api, nhg, AF_INET6); | |
338 | break; | |
339 | case AFI_L2VPN: | |
340 | DEBUGD(&pbr_dbg_zebra, | |
341 | "%s: Asked to install unsupported route type: L2VPN", | |
15569c58 | 342 | __func__); |
e5c83d9b | 343 | break; |
b26f891d SW |
344 | case AFI_UNSPEC: |
345 | DEBUGD(&pbr_dbg_zebra, | |
15569c58 | 346 | "%s: Asked to install unspecified route type", __func__); |
b26f891d | 347 | break; |
e5c83d9b DS |
348 | } |
349 | } | |
350 | ||
351 | /* | |
352 | * This function assumes a default route is being | |
353 | * removed from the appropriate tableid | |
354 | */ | |
355 | void route_delete(struct pbr_nexthop_group_cache *pnhgc, afi_t afi) | |
356 | { | |
357 | struct zapi_route api; | |
358 | ||
15569c58 | 359 | DEBUGD(&pbr_dbg_zebra, "%s for Table: %d", __func__, pnhgc->table_id); |
2f61710b | 360 | |
e5c83d9b DS |
361 | memset(&api, 0, sizeof(api)); |
362 | api.vrf_id = VRF_DEFAULT; | |
363 | api.type = ZEBRA_ROUTE_PBR; | |
364 | api.safi = SAFI_UNICAST; | |
365 | ||
366 | api.tableid = pnhgc->table_id; | |
367 | SET_FLAG(api.message, ZAPI_MESSAGE_TABLEID); | |
368 | ||
369 | switch (afi) { | |
370 | case AFI_IP: | |
371 | api.prefix.family = AF_INET; | |
372 | zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api); | |
373 | break; | |
374 | case AFI_IP6: | |
375 | api.prefix.family = AF_INET6; | |
376 | zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api); | |
377 | break; | |
378 | case AFI_MAX: | |
379 | api.prefix.family = AF_INET; | |
380 | zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api); | |
381 | api.prefix.family = AF_INET6; | |
382 | zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api); | |
383 | break; | |
384 | case AFI_L2VPN: | |
385 | DEBUGD(&pbr_dbg_zebra, | |
386 | "%s: Asked to delete unsupported route type: L2VPN", | |
15569c58 | 387 | __func__); |
e5c83d9b | 388 | break; |
b26f891d SW |
389 | case AFI_UNSPEC: |
390 | DEBUGD(&pbr_dbg_zebra, | |
15569c58 | 391 | "%s: Asked to delete unspecified route type", __func__); |
b26f891d | 392 | break; |
e5c83d9b | 393 | } |
e5c83d9b DS |
394 | } |
395 | ||
121f9dee | 396 | static int pbr_zebra_nexthop_update(ZAPI_CALLBACK_ARGS) |
e5c83d9b DS |
397 | { |
398 | struct zapi_route nhr; | |
399 | char buf[PREFIX2STR_BUFFER]; | |
400 | uint32_t i; | |
401 | ||
54317f2c A |
402 | if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) { |
403 | zlog_warn("Failure to decode Nexthop update message"); | |
404 | return 0; | |
405 | } | |
e5c83d9b DS |
406 | |
407 | if (DEBUG_MODE_CHECK(&pbr_dbg_zebra, DEBUG_MODE_ALL)) { | |
408 | ||
409 | DEBUGD(&pbr_dbg_zebra, "%s: Received Nexthop update: %s", | |
15569c58 | 410 | __func__, prefix2str(&nhr.prefix, buf, sizeof(buf))); |
e5c83d9b | 411 | |
15569c58 DA |
412 | DEBUGD(&pbr_dbg_zebra, "%s: (\tNexthops(%u)", __func__, |
413 | nhr.nexthop_num); | |
e5c83d9b DS |
414 | |
415 | for (i = 0; i < nhr.nexthop_num; i++) { | |
416 | DEBUGD(&pbr_dbg_zebra, | |
417 | "%s: \tType: %d: vrf: %d, ifindex: %d gate: %s", | |
15569c58 | 418 | __func__, nhr.nexthops[i].type, |
e5c83d9b DS |
419 | nhr.nexthops[i].vrf_id, nhr.nexthops[i].ifindex, |
420 | inet_ntoa(nhr.nexthops[i].gate.ipv4)); | |
421 | } | |
422 | } | |
423 | ||
424 | pbr_nht_nexthop_update(&nhr); | |
425 | return 1; | |
426 | } | |
427 | ||
428 | extern struct zebra_privs_t pbr_privs; | |
429 | ||
430 | void pbr_zebra_init(void) | |
431 | { | |
432 | struct zclient_options opt = { .receive_notify = true }; | |
433 | ||
26f63a1e | 434 | zclient = zclient_new(master, &opt); |
e5c83d9b DS |
435 | |
436 | zclient_init(zclient, ZEBRA_ROUTE_PBR, 0, &pbr_privs); | |
437 | zclient->zebra_connected = zebra_connected; | |
e5c83d9b DS |
438 | zclient->interface_address_add = interface_address_add; |
439 | zclient->interface_address_delete = interface_address_delete; | |
be3b67b5 | 440 | zclient->interface_vrf_update = interface_vrf_update; |
e5c83d9b DS |
441 | zclient->route_notify_owner = route_notify_owner; |
442 | zclient->rule_notify_owner = rule_notify_owner; | |
443 | zclient->nexthop_update = pbr_zebra_nexthop_update; | |
444 | } | |
445 | ||
446 | void pbr_send_rnh(struct nexthop *nhop, bool reg) | |
447 | { | |
448 | uint32_t command; | |
449 | struct prefix p; | |
450 | ||
451 | command = (reg) ? | |
452 | ZEBRA_NEXTHOP_REGISTER : ZEBRA_NEXTHOP_UNREGISTER; | |
453 | ||
454 | memset(&p, 0, sizeof(p)); | |
d3765386 | 455 | switch (nhop->type) { |
e5c83d9b DS |
456 | case NEXTHOP_TYPE_IFINDEX: |
457 | case NEXTHOP_TYPE_BLACKHOLE: | |
458 | return; | |
459 | case NEXTHOP_TYPE_IPV4: | |
460 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
461 | p.family = AF_INET; | |
462 | p.u.prefix4.s_addr = nhop->gate.ipv4.s_addr; | |
463 | p.prefixlen = 32; | |
464 | break; | |
465 | case NEXTHOP_TYPE_IPV6: | |
466 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
467 | p.family = AF_INET6; | |
468 | memcpy(&p.u.prefix6, &nhop->gate.ipv6, 16); | |
469 | p.prefixlen = 128; | |
cb254f41 SW |
470 | if (IN6_IS_ADDR_LINKLOCAL(&nhop->gate.ipv6)) |
471 | /* | |
472 | * Don't bother tracking link locals, just track their | |
473 | * interface state. | |
474 | */ | |
475 | return; | |
e5c83d9b DS |
476 | break; |
477 | } | |
478 | ||
479 | if (zclient_send_rnh(zclient, command, &p, | |
480 | false, nhop->vrf_id) < 0) { | |
15569c58 | 481 | zlog_warn("%s: Failure to send nexthop to zebra", __func__); |
e5c83d9b DS |
482 | } |
483 | } | |
484 | ||
485 | static void pbr_encode_pbr_map_sequence_prefix(struct stream *s, | |
486 | struct prefix *p, | |
49027ce8 | 487 | unsigned char family) |
e5c83d9b DS |
488 | { |
489 | struct prefix any; | |
490 | ||
491 | if (!p) { | |
492 | memset(&any, 0, sizeof(any)); | |
493 | any.family = family; | |
494 | p = &any; | |
495 | } | |
496 | ||
497 | stream_putc(s, p->family); | |
498 | stream_putc(s, p->prefixlen); | |
499 | stream_put(s, &p->u.prefix, prefix_blen(p)); | |
500 | } | |
501 | ||
be3b67b5 SW |
502 | static void |
503 | pbr_encode_pbr_map_sequence_vrf(struct stream *s, | |
504 | const struct pbr_map_sequence *pbrms, | |
505 | const struct interface *ifp) | |
506 | { | |
507 | struct pbr_vrf *pbr_vrf; | |
508 | ||
509 | if (pbrms->vrf_unchanged) | |
510 | pbr_vrf = pbr_vrf_lookup_by_id(ifp->vrf_id); | |
511 | else | |
512 | pbr_vrf = pbr_vrf_lookup_by_name(pbrms->vrf_name); | |
513 | ||
514 | if (!pbr_vrf) { | |
515 | DEBUGD(&pbr_dbg_zebra, "%s: VRF not found", __func__); | |
516 | return; | |
517 | } | |
518 | ||
519 | stream_putl(s, pbr_vrf->vrf->data.l.table_id); | |
520 | } | |
521 | ||
e5c83d9b DS |
522 | static void pbr_encode_pbr_map_sequence(struct stream *s, |
523 | struct pbr_map_sequence *pbrms, | |
524 | struct interface *ifp) | |
525 | { | |
49027ce8 | 526 | unsigned char family; |
e5c83d9b DS |
527 | |
528 | family = AF_INET; | |
49027ce8 DS |
529 | if (pbrms->family) |
530 | family = pbrms->family; | |
e5c83d9b DS |
531 | |
532 | stream_putl(s, pbrms->seqno); | |
533 | stream_putl(s, pbrms->ruleno); | |
534 | stream_putl(s, pbrms->unique); | |
535 | pbr_encode_pbr_map_sequence_prefix(s, pbrms->src, family); | |
536 | stream_putw(s, 0); /* src port */ | |
537 | pbr_encode_pbr_map_sequence_prefix(s, pbrms->dst, family); | |
538 | stream_putw(s, 0); /* dst port */ | |
95a9fe02 | 539 | stream_putl(s, pbrms->mark); |
be3b67b5 SW |
540 | |
541 | if (pbrms->vrf_unchanged || pbrms->vrf_lookup) | |
542 | pbr_encode_pbr_map_sequence_vrf(s, pbrms, ifp); | |
543 | else if (pbrms->nhgrp_name) | |
e5c83d9b DS |
544 | stream_putl(s, pbr_nht_get_table(pbrms->nhgrp_name)); |
545 | else if (pbrms->nhg) | |
546 | stream_putl(s, pbr_nht_get_table(pbrms->internal_nhg_name)); | |
547 | stream_putl(s, ifp->ifindex); | |
548 | } | |
549 | ||
b13e5ad6 | 550 | void pbr_send_pbr_map(struct pbr_map_sequence *pbrms, |
f143cffa | 551 | struct pbr_map_interface *pmi, bool install, bool changed) |
e5c83d9b | 552 | { |
b13e5ad6 | 553 | struct pbr_map *pbrm = pbrms->parent; |
e5c83d9b | 554 | struct stream *s; |
10a00758 | 555 | uint64_t is_installed = (uint64_t)1 << pmi->install_bit; |
37c606ff DS |
556 | |
557 | is_installed &= pbrms->installed; | |
e5c83d9b | 558 | |
15569c58 DA |
559 | DEBUGD(&pbr_dbg_zebra, "%s: for %s %d(%" PRIu64 ")", __func__, |
560 | pbrm->name, install, is_installed); | |
9b71ea4b DS |
561 | |
562 | /* | |
f143cffa SW |
563 | * If we are installed and asked to do so again and the config |
564 | * has not changed, just return. | |
565 | * | |
566 | * If we are not installed and asked | |
f732636d | 567 | * to delete just return. |
9b71ea4b | 568 | */ |
f143cffa | 569 | if (install && is_installed && !changed) |
9b71ea4b DS |
570 | return; |
571 | ||
37c606ff | 572 | if (!install && !is_installed) |
9b71ea4b | 573 | return; |
e5c83d9b DS |
574 | |
575 | s = zclient->obuf; | |
576 | stream_reset(s); | |
577 | ||
578 | zclient_create_header(s, | |
579 | install ? ZEBRA_RULE_ADD : ZEBRA_RULE_DELETE, | |
580 | VRF_DEFAULT); | |
581 | ||
b13e5ad6 DS |
582 | /* |
583 | * We are sending one item at a time at the moment | |
584 | */ | |
585 | stream_putl(s, 1); | |
e5c83d9b | 586 | |
b21d3042 SW |
587 | DEBUGD(&pbr_dbg_zebra, "%s: \t%s %s seq %u %d %s %u", __func__, |
588 | install ? "Installing" : "Deleting", pbrm->name, pbrms->seqno, | |
589 | install, pmi->ifp->name, pmi->delete); | |
e5c83d9b | 590 | |
b13e5ad6 | 591 | pbr_encode_pbr_map_sequence(s, pbrms, pmi->ifp); |
e5c83d9b | 592 | |
e5c83d9b DS |
593 | stream_putw_at(s, 0, stream_get_endp(s)); |
594 | ||
e5c83d9b DS |
595 | zclient_send_message(zclient); |
596 | } |