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