]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_pbr.c
zebra: Use __func__ instead of __PRETTY_FUNCTION__
[mirror_frr.git] / zebra / zebra_pbr.c
CommitLineData
942bf97b 1/* Zebra Policy Based Routing (PBR) main handling.
2 * Copyright (C) 2018 Cumulus Networks, Inc.
3 *
4 * This file is part of FRR.
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
17 * along with FRR; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
43fe6a2a
DS
24#include <jhash.h>
25#include <hash.h>
f80ec7e3 26#include <memory.h>
73a829f7 27#include <hook.h>
43fe6a2a 28
7f0ea8a4 29#include "zebra/zebra_router.h"
942bf97b 30#include "zebra/zebra_pbr.h"
31#include "zebra/rt.h"
bf094f69 32#include "zebra/zapi_msg.h"
c0ea1ae7 33#include "zebra/zserv.h"
8eeca5a2 34#include "zebra/debug.h"
f80ec7e3
PG
35
36/* definitions */
bf8d3d6a 37DEFINE_MTYPE_STATIC(ZEBRA, PBR_IPTABLE_IFNAME, "PBR interface list");
942bf97b 38
39/* definitions */
586f4ccf
PG
40static const struct message ipset_type_msg[] = {
41 {IPSET_NET_PORT_NET, "net,port,net"},
42 {IPSET_NET_PORT, "net,port"},
43 {IPSET_NET_NET, "net,net"},
44 {IPSET_NET, "net"},
45 {0}
46};
942bf97b 47
be729dd7
PG
48const struct message icmp_typecode_str[] = {
49 { 0 << 8, "echo-reply"},
50 { 0 << 8, "pong"},
51 { 3 << 8, "network-unreachable"},
52 { (3 << 8) + 1, "host-unreachable"},
53 { (3 << 8) + 2, "protocol-unreachable"},
54 { (3 << 8) + 3, "port-unreachable"},
55 { (3 << 8) + 4, "fragmentation-needed"},
56 { (3 << 8) + 5, "source-route-failed"},
57 { (3 << 8) + 6, "network-unknown"},
58 { (3 << 8) + 7, "host-unknown"},
59 { (3 << 8) + 9, "network-prohibited"},
60 { (3 << 8) + 10, "host-prohibited"},
61 { (3 << 8) + 11, "TOS-network-unreachable"},
62 { (3 << 8) + 12, "TOS-host-unreachable"},
63 { (3 << 8) + 13, "communication-prohibited"},
64 { (3 << 8) + 14, "host-precedence-violation"},
65 { (3 << 8) + 15, "precedence-cutoff"},
66 { 4 << 8, "source-quench"},
67 { 5 << 8, "network-redirect"},
68 { (5 << 8) + 1, "host-redirect"},
69 { (5 << 8) + 2, "TOS-network-redirect"},
70 { (5 << 8) + 3, "TOS-host-redirect"},
71 { 8 << 8, "echo-request"},
72 { 8 << 8, "ping"},
73 { 9 << 8, "router-advertisement"},
74 { 10 << 8, "router-solicitation"},
75 { 11 << 8, "ttl-zero-during-transit"},
76 { (11 << 8) + 1, "ttl-zero-during-reassembly"},
77 { 12 << 8, "ip-header-bad"},
78 { (12 << 8) + 1, "required-option-missing"},
79 { 13 << 8, "timestamp-request"},
80 { 14 << 8, "timestamp-reply"},
81 { 17 << 8, "address-mask-request"},
82 { 18 << 8, "address-mask-reply"},
83 {0}
84};
85
c9b1139a
PG
86const struct message icmpv6_typecode_str[] = {
87 { 128 << 8, "echo-request"},
88 { 129 << 8, "echo-reply"},
89 { 1 << 8, "no-route"},
90 { (1 << 8) + 1, "communication-prohibited"},
91 { (1 << 8) + 3, "address-unreachable"},
92 { (1 << 8) + 4, "port-unreachable"},
93 { (2 << 8), "packet-too-big"},
94 { 3 << 0, "ttl-zero-during-transit"},
95 { (3 << 8) + 1, "ttl-zero-during-reassembly"},
96 { 4 << 0, "bad-header"},
97 { (4 << 0) + 1, "unknown-header-type"},
98 { (4 << 0) + 2, "unknown-option"},
99 { 133 << 8, "router-solicitation"},
100 { 134 << 8, "router-advertisement"},
101 { 135 << 8, "neighbor-solicitation"},
102 { 136 << 8, "neighbor-advertisement"},
103 { 137 << 8, "redirect"},
104 {0}
105};
106
dc993e76
PG
107/* definitions */
108static const struct message tcp_value_str[] = {
109 {TCP_HEADER_FIN, "FIN"},
110 {TCP_HEADER_SYN, "SYN"},
111 {TCP_HEADER_RST, "RST"},
112 {TCP_HEADER_PSH, "PSH"},
113 {TCP_HEADER_ACK, "ACK"},
114 {TCP_HEADER_URG, "URG"},
115 {0}
116};
117
5ac5b7cc
PG
118static const struct message fragment_value_str[] = {
119 {1, "dont-fragment"},
120 {2, "is-fragment"},
121 {4, "first-fragment"},
122 {8, "last-fragment"},
123 {0}
124};
125
942bf97b 126/* static function declarations */
1c6fca1f 127DEFINE_HOOK(zebra_pbr_ipset_entry_get_stat,
62f20a52
DS
128 (struct zebra_pbr_ipset_entry *ipset, uint64_t *pkts,
129 uint64_t *bytes),
8451921b 130 (ipset, pkts, bytes));
62f20a52 131
1c6fca1f 132DEFINE_HOOK(zebra_pbr_iptable_get_stat,
62f20a52
DS
133 (struct zebra_pbr_iptable *iptable, uint64_t *pkts,
134 uint64_t *bytes),
8451921b 135 (iptable, pkts, bytes));
62f20a52 136
1c6fca1f 137DEFINE_HOOK(zebra_pbr_iptable_update,
62f20a52
DS
138 (int cmd, struct zebra_pbr_iptable *iptable), (cmd, iptable));
139
1c6fca1f 140DEFINE_HOOK(zebra_pbr_ipset_entry_update,
62f20a52
DS
141 (int cmd, struct zebra_pbr_ipset_entry *ipset), (cmd, ipset));
142
1c6fca1f 143DEFINE_HOOK(zebra_pbr_ipset_update,
62f20a52 144 (int cmd, struct zebra_pbr_ipset *ipset), (cmd, ipset));
942bf97b 145
146/* Private functions */
147
148/* Public functions */
43fe6a2a 149void zebra_pbr_rules_free(void *arg)
1fbfe5a5 150{
43fe6a2a
DS
151 struct zebra_pbr_rule *rule;
152
153 rule = (struct zebra_pbr_rule *)arg;
154
f62e5480 155 (void)dplane_pbr_rule_delete(rule);
43fe6a2a
DS
156 XFREE(MTYPE_TMP, rule);
157}
158
d8b87afe 159uint32_t zebra_pbr_rules_hash_key(const void *arg)
43fe6a2a 160{
d8b87afe 161 const struct zebra_pbr_rule *rule;
43fe6a2a
DS
162 uint32_t key;
163
d8b87afe 164 rule = arg;
5dd0722d
PG
165 key = jhash_3words(rule->rule.seq, rule->rule.priority,
166 rule->rule.action.table,
167 prefix_hash_key(&rule->rule.filter.src_ip));
a0321978 168
5dd0722d 169 if (rule->rule.filter.fwmark)
58a1d249 170 key = jhash_2words(rule->rule.filter.fwmark, rule->vrf_id, key);
1907e4b8 171 else
58a1d249
DS
172 key = jhash_1word(rule->vrf_id, key);
173
174 key = jhash(rule->ifname, strlen(rule->ifname), key);
7f0ea8a4 175
5dd0722d
PG
176 return jhash_3words(rule->rule.filter.src_port,
177 rule->rule.filter.dst_port,
178 prefix_hash_key(&rule->rule.filter.dst_ip),
179 jhash_1word(rule->rule.unique, key));
43fe6a2a
DS
180}
181
74df8d6d 182bool zebra_pbr_rules_hash_equal(const void *arg1, const void *arg2)
43fe6a2a
DS
183{
184 const struct zebra_pbr_rule *r1, *r2;
185
186 r1 = (const struct zebra_pbr_rule *)arg1;
187 r2 = (const struct zebra_pbr_rule *)arg2;
188
5dd0722d 189 if (r1->rule.seq != r2->rule.seq)
74df8d6d 190 return false;
43fe6a2a 191
5dd0722d 192 if (r1->rule.priority != r2->rule.priority)
74df8d6d 193 return false;
43fe6a2a 194
5dd0722d 195 if (r1->rule.unique != r2->rule.unique)
74df8d6d 196 return false;
b6c5d343 197
5dd0722d 198 if (r1->rule.action.table != r2->rule.action.table)
74df8d6d 199 return false;
43fe6a2a 200
5dd0722d 201 if (r1->rule.filter.src_port != r2->rule.filter.src_port)
74df8d6d 202 return false;
43fe6a2a 203
5dd0722d 204 if (r1->rule.filter.dst_port != r2->rule.filter.dst_port)
74df8d6d 205 return false;
43fe6a2a 206
5dd0722d 207 if (r1->rule.filter.fwmark != r2->rule.filter.fwmark)
74df8d6d 208 return false;
1907e4b8 209
5dd0722d 210 if (!prefix_same(&r1->rule.filter.src_ip, &r2->rule.filter.src_ip))
74df8d6d 211 return false;
43fe6a2a 212
5dd0722d 213 if (!prefix_same(&r1->rule.filter.dst_ip, &r2->rule.filter.dst_ip))
74df8d6d 214 return false;
43fe6a2a 215
58a1d249 216 if (strcmp(r1->rule.ifname, r2->rule.ifname) != 0)
74df8d6d 217 return false;
a0321978 218
7f0ea8a4
DS
219 if (r1->vrf_id != r2->vrf_id)
220 return false;
221
74df8d6d 222 return true;
43fe6a2a
DS
223}
224
f46bbab4 225struct pbr_rule_unique_lookup {
8c3cd6c6
DS
226 struct zebra_pbr_rule *rule;
227 uint32_t unique;
58a1d249 228 char ifname[INTERFACE_NAMSIZ + 1];
7f0ea8a4 229 vrf_id_t vrf_id;
8c3cd6c6
DS
230};
231
e3b78da8 232static int pbr_rule_lookup_unique_walker(struct hash_bucket *b, void *data)
8c3cd6c6 233{
f46bbab4 234 struct pbr_rule_unique_lookup *pul = data;
8c3cd6c6
DS
235 struct zebra_pbr_rule *rule = b->data;
236
7f0ea8a4 237 if (pul->unique == rule->rule.unique
58a1d249 238 && strncmp(pul->ifname, rule->rule.ifname, INTERFACE_NAMSIZ) == 0
7f0ea8a4 239 && pul->vrf_id == rule->vrf_id) {
8c3cd6c6
DS
240 pul->rule = rule;
241 return HASHWALK_ABORT;
242 }
243
244 return HASHWALK_CONTINUE;
245}
246
62f20a52
DS
247static struct zebra_pbr_rule *
248pbr_rule_lookup_unique(struct zebra_pbr_rule *zrule)
8c3cd6c6 249{
f46bbab4 250 struct pbr_rule_unique_lookup pul;
8c3cd6c6 251
7f0ea8a4 252 pul.unique = zrule->rule.unique;
58a1d249 253 strlcpy(pul.ifname, zrule->rule.ifname, INTERFACE_NAMSIZ);
8c3cd6c6 254 pul.rule = NULL;
7f0ea8a4
DS
255 pul.vrf_id = zrule->vrf_id;
256 hash_walk(zrouter.rules_hash, &pbr_rule_lookup_unique_walker, &pul);
8c3cd6c6
DS
257
258 return pul.rule;
259}
260
7661461a
PG
261void zebra_pbr_ipset_free(void *arg)
262{
263 struct zebra_pbr_ipset *ipset;
264
265 ipset = (struct zebra_pbr_ipset *)arg;
1c6fca1f 266 hook_call(zebra_pbr_ipset_update, 0, ipset);
7661461a
PG
267 XFREE(MTYPE_TMP, ipset);
268}
269
d8b87afe 270uint32_t zebra_pbr_ipset_hash_key(const void *arg)
7661461a 271{
d8b87afe 272 const struct zebra_pbr_ipset *ipset = arg;
425bdd6b 273 uint32_t *pnt = (uint32_t *)&ipset->ipset_name;
62f20a52 274 uint32_t key = jhash_1word(ipset->vrf_id, 0x63ab42de);
7661461a 275
a60b7031
PG
276 key = jhash_1word(ipset->family, key);
277
62f20a52 278 return jhash2(pnt, ZEBRA_IPSET_NAME_HASH_SIZE, key);
7661461a
PG
279}
280
74df8d6d 281bool zebra_pbr_ipset_hash_equal(const void *arg1, const void *arg2)
7661461a
PG
282{
283 const struct zebra_pbr_ipset *r1, *r2;
284
285 r1 = (const struct zebra_pbr_ipset *)arg1;
286 r2 = (const struct zebra_pbr_ipset *)arg2;
287
288 if (r1->type != r2->type)
74df8d6d 289 return false;
7661461a 290 if (r1->unique != r2->unique)
74df8d6d 291 return false;
62f20a52
DS
292 if (r1->vrf_id != r2->vrf_id)
293 return false;
a60b7031
PG
294 if (r1->family != r2->family)
295 return false;
62f20a52 296
7661461a
PG
297 if (strncmp(r1->ipset_name, r2->ipset_name,
298 ZEBRA_IPSET_NAME_SIZE))
74df8d6d
DS
299 return false;
300 return true;
7661461a
PG
301}
302
303void zebra_pbr_ipset_entry_free(void *arg)
304{
305 struct zebra_pbr_ipset_entry *ipset;
306
307 ipset = (struct zebra_pbr_ipset_entry *)arg;
73a829f7 308
1c6fca1f 309 hook_call(zebra_pbr_ipset_entry_update, 0, ipset);
7661461a
PG
310
311 XFREE(MTYPE_TMP, ipset);
312}
313
d8b87afe 314uint32_t zebra_pbr_ipset_entry_hash_key(const void *arg)
7661461a 315{
d8b87afe 316 const struct zebra_pbr_ipset_entry *ipset;
7661461a
PG
317 uint32_t key;
318
d8b87afe 319 ipset = arg;
7661461a
PG
320 key = prefix_hash_key(&ipset->src);
321 key = jhash_1word(ipset->unique, key);
322 key = jhash_1word(prefix_hash_key(&ipset->dst), key);
25d760c5
PG
323 key = jhash(&ipset->dst_port_min, 2, key);
324 key = jhash(&ipset->dst_port_max, 2, key);
325 key = jhash(&ipset->src_port_min, 2, key);
326 key = jhash(&ipset->src_port_max, 2, key);
327 key = jhash(&ipset->proto, 1, key);
7661461a
PG
328
329 return key;
330}
331
74df8d6d 332bool zebra_pbr_ipset_entry_hash_equal(const void *arg1, const void *arg2)
7661461a
PG
333{
334 const struct zebra_pbr_ipset_entry *r1, *r2;
335
336 r1 = (const struct zebra_pbr_ipset_entry *)arg1;
337 r2 = (const struct zebra_pbr_ipset_entry *)arg2;
338
339 if (r1->unique != r2->unique)
74df8d6d 340 return false;
7661461a
PG
341
342 if (!prefix_same(&r1->src, &r2->src))
74df8d6d 343 return false;
7661461a
PG
344
345 if (!prefix_same(&r1->dst, &r2->dst))
74df8d6d 346 return false;
7661461a 347
25d760c5 348 if (r1->src_port_min != r2->src_port_min)
74df8d6d 349 return false;
25d760c5
PG
350
351 if (r1->src_port_max != r2->src_port_max)
74df8d6d 352 return false;
25d760c5
PG
353
354 if (r1->dst_port_min != r2->dst_port_min)
74df8d6d 355 return false;
25d760c5
PG
356
357 if (r1->dst_port_max != r2->dst_port_max)
74df8d6d 358 return false;
25d760c5
PG
359
360 if (r1->proto != r2->proto)
74df8d6d
DS
361 return false;
362 return true;
7661461a
PG
363}
364
3b1de7b8
PG
365/* this function gives option to flush plugin memory contexts
366 * with all parameter. set it to true to flush all
367 * set it to false to flush only passed arg argument
368 */
369static void _zebra_pbr_iptable_free_all(void *arg, bool all)
7abd6c4f
PG
370{
371 struct zebra_pbr_iptable *iptable;
f80ec7e3
PG
372 struct listnode *node, *nnode;
373 char *name;
7abd6c4f
PG
374
375 iptable = (struct zebra_pbr_iptable *)arg;
3b1de7b8
PG
376
377 if (all)
378 hook_call(zebra_pbr_iptable_update, 0, iptable);
7abd6c4f 379
8b5c4dce
QY
380 if (iptable->interface_name_list) {
381 for (ALL_LIST_ELEMENTS(iptable->interface_name_list, node,
382 nnode, name)) {
383 XFREE(MTYPE_PBR_IPTABLE_IFNAME, name);
384 list_delete_node(iptable->interface_name_list, node);
385 }
386 list_delete(&iptable->interface_name_list);
f80ec7e3 387 }
7abd6c4f
PG
388 XFREE(MTYPE_TMP, iptable);
389}
390
3b1de7b8
PG
391void zebra_pbr_iptable_free(void *arg)
392{
393 _zebra_pbr_iptable_free_all(arg, false);
394}
395
d8b87afe 396uint32_t zebra_pbr_iptable_hash_key(const void *arg)
7abd6c4f 397{
d8b87afe 398 const struct zebra_pbr_iptable *iptable = arg;
7abd6c4f
PG
399 uint32_t *pnt = (uint32_t *)&(iptable->ipset_name);
400 uint32_t key;
401
402 key = jhash2(pnt, ZEBRA_IPSET_NAME_HASH_SIZE,
403 0x63ab42de);
404 key = jhash_1word(iptable->fwmark, key);
a60b7031
PG
405 key = jhash_1word(iptable->family, key);
406 key = jhash_1word(iptable->flow_label, key);
e7f7dad4
PG
407 key = jhash_1word(iptable->pkt_len_min, key);
408 key = jhash_1word(iptable->pkt_len_max, key);
dc993e76
PG
409 key = jhash_1word(iptable->tcp_flags, key);
410 key = jhash_1word(iptable->tcp_mask_flags, key);
4977bd6c 411 key = jhash_1word(iptable->dscp_value, key);
f449d223 412 key = jhash_1word(iptable->protocol, key);
5ac5b7cc 413 key = jhash_1word(iptable->fragment, key);
62f20a52
DS
414 key = jhash_1word(iptable->vrf_id, key);
415
7abd6c4f
PG
416 return jhash_3words(iptable->filter_bm, iptable->type,
417 iptable->unique, key);
418}
419
74df8d6d 420bool zebra_pbr_iptable_hash_equal(const void *arg1, const void *arg2)
7abd6c4f
PG
421{
422 const struct zebra_pbr_iptable *r1, *r2;
423
424 r1 = (const struct zebra_pbr_iptable *)arg1;
425 r2 = (const struct zebra_pbr_iptable *)arg2;
426
62f20a52 427 if (r1->vrf_id != r2->vrf_id)
b08047f8 428 return false;
7abd6c4f 429 if (r1->type != r2->type)
74df8d6d 430 return false;
7abd6c4f 431 if (r1->unique != r2->unique)
74df8d6d 432 return false;
7abd6c4f 433 if (r1->filter_bm != r2->filter_bm)
74df8d6d 434 return false;
7abd6c4f 435 if (r1->fwmark != r2->fwmark)
74df8d6d 436 return false;
7abd6c4f 437 if (r1->action != r2->action)
74df8d6d 438 return false;
7abd6c4f
PG
439 if (strncmp(r1->ipset_name, r2->ipset_name,
440 ZEBRA_IPSET_NAME_SIZE))
74df8d6d 441 return false;
a60b7031
PG
442 if (r1->family != r2->family)
443 return false;
444 if (r1->flow_label != r2->flow_label)
445 return false;
e7f7dad4 446 if (r1->pkt_len_min != r2->pkt_len_min)
74df8d6d 447 return false;
e7f7dad4 448 if (r1->pkt_len_max != r2->pkt_len_max)
74df8d6d 449 return false;
dc993e76 450 if (r1->tcp_flags != r2->tcp_flags)
74df8d6d 451 return false;
dc993e76 452 if (r1->tcp_mask_flags != r2->tcp_mask_flags)
74df8d6d 453 return false;
4977bd6c 454 if (r1->dscp_value != r2->dscp_value)
74df8d6d 455 return false;
5ac5b7cc 456 if (r1->fragment != r2->fragment)
74df8d6d 457 return false;
f449d223
PG
458 if (r1->protocol != r2->protocol)
459 return false;
74df8d6d 460 return true;
7abd6c4f
PG
461}
462
43fe6a2a
DS
463static void *pbr_rule_alloc_intern(void *arg)
464{
465 struct zebra_pbr_rule *zpr;
466 struct zebra_pbr_rule *new;
467
468 zpr = (struct zebra_pbr_rule *)arg;
469
470 new = XCALLOC(MTYPE_TMP, sizeof(*new));
471
472 memcpy(new, zpr, sizeof(*zpr));
473
474 return new;
475}
476
3ae327cb
SW
477static int pbr_rule_release(struct zebra_pbr_rule *rule)
478{
479 struct zebra_pbr_rule *lookup;
480
481 lookup = hash_lookup(zrouter.rules_hash, rule);
482
483 if (!lookup)
484 return -ENOENT;
485
486 hash_release(zrouter.rules_hash, lookup);
487 XFREE(MTYPE_TMP, lookup);
488
489 return 0;
490}
491
7f0ea8a4 492void zebra_pbr_add_rule(struct zebra_pbr_rule *rule)
43fe6a2a 493{
3ae327cb 494 struct zebra_pbr_rule *found;
8c3cd6c6 495
3ae327cb
SW
496 /**
497 * Check if we already have it (this checks via a unique ID, walking
498 * over the hash table, not via a hash operation).
8c3cd6c6 499 */
3ae327cb
SW
500 found = pbr_rule_lookup_unique(rule);
501
3ae327cb
SW
502 /* If found, this is an update */
503 if (found) {
8eeca5a2
SW
504 if (IS_ZEBRA_DEBUG_PBR)
505 zlog_debug(
506 "%s: seq: %d, prior: %d, unique: %d, ifname: %s -- update",
507 __func__, rule->rule.seq, rule->rule.priority,
508 rule->rule.unique, rule->rule.ifname);
509
f62e5480 510 (void)dplane_pbr_rule_update(found, rule);
3ae327cb
SW
511
512 if (pbr_rule_release(found))
513 zlog_debug(
514 "%s: Rule being updated we know nothing about",
7d7be47e 515 __func__);
3ae327cb 516
8eeca5a2
SW
517 } else {
518 if (IS_ZEBRA_DEBUG_PBR)
519 zlog_debug(
520 "%s: seq: %d, prior: %d, unique: %d, ifname: %s -- new",
521 __func__, rule->rule.seq, rule->rule.priority,
522 rule->rule.unique, rule->rule.ifname);
523
f62e5480 524 (void)dplane_pbr_rule_add(rule);
8eeca5a2 525 }
f7692085
SW
526
527 (void)hash_get(zrouter.rules_hash, rule, pbr_rule_alloc_intern);
1fbfe5a5
DS
528}
529
7f0ea8a4 530void zebra_pbr_del_rule(struct zebra_pbr_rule *rule)
1fbfe5a5 531{
8eeca5a2
SW
532 if (IS_ZEBRA_DEBUG_PBR)
533 zlog_debug("%s: seq: %d, prior: %d, unique: %d, ifname: %s",
534 __func__, rule->rule.seq, rule->rule.priority,
535 rule->rule.unique, rule->rule.ifname);
536
f62e5480 537 (void)dplane_pbr_rule_delete(rule);
43fe6a2a 538
3ae327cb 539 if (pbr_rule_release(rule))
9df414fe 540 zlog_debug("%s: Rule being deleted we know nothing about",
15569c58 541 __func__);
1fbfe5a5
DS
542}
543
5162e000
PG
544void zebra_pbr_process_iptable(struct zebra_dplane_ctx *ctx)
545{
546 int mode, ret = 0;
547 struct zebra_pbr_iptable ipt;
548
549 if (dplane_ctx_get_op(ctx) == DPLANE_OP_IPTABLE_ADD)
550 mode = 1;
551 else
552 mode = 0;
553
554 if (dplane_ctx_get_pbr_iptable(ctx, &ipt)) {
555 ret = hook_call(zebra_pbr_iptable_update, mode, &ipt);
556 if (ret)
ef524230
PG
557 dplane_ctx_set_status(ctx,
558 ZEBRA_DPLANE_REQUEST_SUCCESS);
5162e000
PG
559 }
560 if (!ret)
561 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_FAILURE);
562}
563
ef524230
PG
564void zebra_pbr_process_ipset(struct zebra_dplane_ctx *ctx)
565{
566 int mode, ret = 0;
567 struct zebra_pbr_ipset ipset;
568
569 if (dplane_ctx_get_op(ctx) == DPLANE_OP_IPSET_ADD)
570 mode = 1;
571 else
572 mode = 0;
573 if (dplane_ctx_get_pbr_ipset(ctx, &ipset)) {
574 ret = hook_call(zebra_pbr_ipset_update, mode, &ipset);
575 if (ret)
576 dplane_ctx_set_status(ctx,
577 ZEBRA_DPLANE_REQUEST_SUCCESS);
578 }
579 if (!ret)
580 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_FAILURE);
581}
582
583void zebra_pbr_process_ipset_entry(struct zebra_dplane_ctx *ctx)
584{
585 int mode, ret = 0;
586 struct zebra_pbr_ipset_entry ipset_entry;
587 struct zebra_pbr_ipset ipset;
588
589 if (dplane_ctx_get_op(ctx) == DPLANE_OP_IPSET_ENTRY_ADD)
590 mode = 1;
591 else
592 mode = 0;
593
594 if (!dplane_ctx_get_pbr_ipset_entry(ctx, &ipset_entry))
595 return;
596 if (!dplane_ctx_get_pbr_ipset(ctx, &ipset))
597 return;
598 ipset_entry.backpointer = &ipset;
599
600 ret = hook_call(zebra_pbr_ipset_entry_update, mode, &ipset_entry);
601 if (ret)
602 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS);
603 else
604 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_FAILURE);
605}
606
e3b78da8 607static void zebra_pbr_cleanup_rules(struct hash_bucket *b, void *data)
e69aa084 608{
e69aa084
DS
609 struct zebra_pbr_rule *rule = b->data;
610 int *sock = data;
611
612 if (rule->sock == *sock) {
f62e5480 613 (void)dplane_pbr_rule_delete(rule);
c4097b75
SW
614 if (hash_release(zrouter.rules_hash, rule))
615 XFREE(MTYPE_TMP, rule);
616 else
617 zlog_debug(
618 "%s: Rule seq: %u is being cleaned but we can't find it in our tables",
619 __func__, rule->rule.seq);
e69aa084
DS
620 }
621}
622
e3b78da8 623static void zebra_pbr_cleanup_ipset(struct hash_bucket *b, void *data)
c2ef5232 624{
c2ef5232
PG
625 struct zebra_pbr_ipset *ipset = b->data;
626 int *sock = data;
627
73a829f7 628 if (ipset->sock == *sock) {
b147e204
QY
629 if (hash_release(zrouter.ipset_hash, ipset))
630 zebra_pbr_ipset_free(ipset);
631 else
632 hook_call(zebra_pbr_ipset_update, 0, ipset);
73a829f7 633 }
c2ef5232
PG
634}
635
e3b78da8 636static void zebra_pbr_cleanup_ipset_entry(struct hash_bucket *b, void *data)
c2ef5232 637{
c2ef5232
PG
638 struct zebra_pbr_ipset_entry *ipset = b->data;
639 int *sock = data;
640
73a829f7 641 if (ipset->sock == *sock) {
b147e204
QY
642 if (hash_release(zrouter.ipset_entry_hash, ipset))
643 zebra_pbr_ipset_entry_free(ipset);
644 else
645 hook_call(zebra_pbr_ipset_entry_update, 0, ipset);
73a829f7 646 }
c2ef5232
PG
647}
648
e3b78da8 649static void zebra_pbr_cleanup_iptable(struct hash_bucket *b, void *data)
c2ef5232 650{
c2ef5232
PG
651 struct zebra_pbr_iptable *iptable = b->data;
652 int *sock = data;
653
73a829f7 654 if (iptable->sock == *sock) {
b147e204 655 if (hash_release(zrouter.iptable_hash, iptable))
3b1de7b8 656 _zebra_pbr_iptable_free_all(iptable, true);
b147e204
QY
657 else
658 hook_call(zebra_pbr_iptable_update, 0, iptable);
73a829f7 659 }
c2ef5232
PG
660}
661
4c0ec639 662static int zebra_pbr_client_close_cleanup(struct zserv *client)
e69aa084 663{
4c0ec639 664 int sock = client->sock;
e69aa084 665
4c0ec639
PG
666 if (!sock)
667 return 0;
7f0ea8a4 668 hash_iterate(zrouter.rules_hash, zebra_pbr_cleanup_rules, &sock);
62f20a52
DS
669 hash_iterate(zrouter.iptable_hash, zebra_pbr_cleanup_iptable, &sock);
670 hash_iterate(zrouter.ipset_entry_hash, zebra_pbr_cleanup_ipset_entry,
671 &sock);
672 hash_iterate(zrouter.ipset_hash, zebra_pbr_cleanup_ipset, &sock);
4c0ec639
PG
673 return 1;
674}
675
676void zebra_pbr_init(void)
677{
c0ea1ae7 678 hook_register(zserv_client_close, zebra_pbr_client_close_cleanup);
e69aa084
DS
679}
680
7661461a
PG
681static void *pbr_ipset_alloc_intern(void *arg)
682{
683 struct zebra_pbr_ipset *zpi;
684 struct zebra_pbr_ipset *new;
685
686 zpi = (struct zebra_pbr_ipset *)arg;
687
688 new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_ipset));
689
690 memcpy(new, zpi, sizeof(*zpi));
691
692 return new;
693}
694
62f20a52 695void zebra_pbr_create_ipset(struct zebra_pbr_ipset *ipset)
7661461a 696{
62f20a52 697 (void)hash_get(zrouter.ipset_hash, ipset, pbr_ipset_alloc_intern);
ef524230 698 (void)dplane_pbr_ipset_add(ipset);
7661461a
PG
699}
700
62f20a52 701void zebra_pbr_destroy_ipset(struct zebra_pbr_ipset *ipset)
7661461a
PG
702{
703 struct zebra_pbr_ipset *lookup;
704
62f20a52 705 lookup = hash_lookup(zrouter.ipset_hash, ipset);
ef524230 706 (void)dplane_pbr_ipset_delete(ipset);
de67547d 707 if (lookup) {
62f20a52 708 hash_release(zrouter.ipset_hash, lookup);
7661461a 709 XFREE(MTYPE_TMP, lookup);
de67547d 710 } else
9df414fe
QY
711 zlog_debug(
712 "%s: IPSet Entry being deleted we know nothing about",
15569c58 713 __func__);
7661461a
PG
714}
715
ed78b7c8
PG
716struct pbr_ipset_name_lookup {
717 struct zebra_pbr_ipset *ipset;
718 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
719};
720
5b0d92b8 721const char *zebra_pbr_ipset_type2str(uint32_t type)
586f4ccf
PG
722{
723 return lookup_msg(ipset_type_msg, type,
724 "Unrecognized IPset Type");
725}
726
e3b78da8 727static int zebra_pbr_ipset_pername_walkcb(struct hash_bucket *bucket, void *arg)
ed78b7c8
PG
728{
729 struct pbr_ipset_name_lookup *pinl =
730 (struct pbr_ipset_name_lookup *)arg;
e3b78da8 731 struct zebra_pbr_ipset *zpi = (struct zebra_pbr_ipset *)bucket->data;
ed78b7c8
PG
732
733 if (!strncmp(pinl->ipset_name, zpi->ipset_name,
734 ZEBRA_IPSET_NAME_SIZE)) {
735 pinl->ipset = zpi;
736 return HASHWALK_ABORT;
737 }
738 return HASHWALK_CONTINUE;
739}
740
62f20a52 741struct zebra_pbr_ipset *zebra_pbr_lookup_ipset_pername(char *ipsetname)
d59c13af 742{
ed78b7c8
PG
743 struct pbr_ipset_name_lookup pinl;
744 struct pbr_ipset_name_lookup *ptr = &pinl;
745
d59c13af
PG
746 if (!ipsetname)
747 return NULL;
ed78b7c8
PG
748 memset(ptr, 0, sizeof(struct pbr_ipset_name_lookup));
749 snprintf((char *)ptr->ipset_name, ZEBRA_IPSET_NAME_SIZE, "%s",
750 ipsetname);
62f20a52 751 hash_walk(zrouter.ipset_hash, zebra_pbr_ipset_pername_walkcb, ptr);
ed78b7c8 752 return ptr->ipset;
d59c13af
PG
753}
754
7661461a
PG
755static void *pbr_ipset_entry_alloc_intern(void *arg)
756{
757 struct zebra_pbr_ipset_entry *zpi;
758 struct zebra_pbr_ipset_entry *new;
759
760 zpi = (struct zebra_pbr_ipset_entry *)arg;
761
762 new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_ipset_entry));
763
764 memcpy(new, zpi, sizeof(*zpi));
765
766 return new;
767}
768
62f20a52 769void zebra_pbr_add_ipset_entry(struct zebra_pbr_ipset_entry *ipset)
7661461a 770{
62f20a52 771 (void)hash_get(zrouter.ipset_entry_hash, ipset,
7661461a 772 pbr_ipset_entry_alloc_intern);
ef524230 773 (void)dplane_pbr_ipset_entry_add(ipset);
7661461a
PG
774}
775
62f20a52 776void zebra_pbr_del_ipset_entry(struct zebra_pbr_ipset_entry *ipset)
7661461a
PG
777{
778 struct zebra_pbr_ipset_entry *lookup;
779
62f20a52 780 lookup = hash_lookup(zrouter.ipset_entry_hash, ipset);
ef524230 781 (void)dplane_pbr_ipset_entry_delete(ipset);
de67547d 782 if (lookup) {
62f20a52 783 hash_release(zrouter.ipset_entry_hash, lookup);
7661461a 784 XFREE(MTYPE_TMP, lookup);
de67547d 785 } else
9df414fe 786 zlog_debug("%s: IPSet being deleted we know nothing about",
15569c58 787 __func__);
7661461a
PG
788}
789
7abd6c4f
PG
790static void *pbr_iptable_alloc_intern(void *arg)
791{
792 struct zebra_pbr_iptable *zpi;
793 struct zebra_pbr_iptable *new;
592af4cc
QY
794 struct listnode *ln;
795 char *ifname;
7abd6c4f
PG
796
797 zpi = (struct zebra_pbr_iptable *)arg;
798
799 new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_iptable));
800
592af4cc 801 /* Deep structure copy */
7abd6c4f 802 memcpy(new, zpi, sizeof(*zpi));
592af4cc
QY
803 new->interface_name_list = list_new();
804
805 if (zpi->interface_name_list) {
806 for (ALL_LIST_ELEMENTS_RO(zpi->interface_name_list, ln, ifname))
807 listnode_add(new->interface_name_list,
808 XSTRDUP(MTYPE_PBR_IPTABLE_IFNAME, ifname));
809 }
7abd6c4f
PG
810
811 return new;
812}
813
62f20a52 814void zebra_pbr_add_iptable(struct zebra_pbr_iptable *iptable)
7abd6c4f 815{
62f20a52 816 (void)hash_get(zrouter.iptable_hash, iptable, pbr_iptable_alloc_intern);
5162e000 817 (void)dplane_pbr_iptable_add(iptable);
7abd6c4f
PG
818}
819
62f20a52 820void zebra_pbr_del_iptable(struct zebra_pbr_iptable *iptable)
7abd6c4f 821{
f80ec7e3 822 struct zebra_pbr_iptable *lookup;
7abd6c4f 823
62f20a52 824 lookup = hash_lookup(zrouter.iptable_hash, iptable);
5162e000 825 (void)dplane_pbr_iptable_delete(iptable);
f80ec7e3
PG
826 if (lookup) {
827 struct listnode *node, *nnode;
828 char *name;
829
62f20a52 830 hash_release(zrouter.iptable_hash, lookup);
f80ec7e3
PG
831 for (ALL_LIST_ELEMENTS(iptable->interface_name_list,
832 node, nnode, name)) {
833 XFREE(MTYPE_PBR_IPTABLE_IFNAME, name);
834 list_delete_node(iptable->interface_name_list,
835 node);
836 }
8b5c4dce 837 list_delete(&iptable->interface_name_list);
7abd6c4f 838 XFREE(MTYPE_TMP, lookup);
f80ec7e3 839 } else
9df414fe 840 zlog_debug("%s: IPTable being deleted we know nothing about",
5e81f5dd 841 __func__);
7abd6c4f
PG
842}
843
942bf97b 844/*
845 * Handle success or failure of rule (un)install in the kernel.
846 */
f62e5480
JU
847void zebra_pbr_dplane_result(struct zebra_dplane_ctx *ctx)
848{
849 enum zebra_dplane_result res;
850 enum dplane_op_e op;
851
852 res = dplane_ctx_get_status(ctx);
853 op = dplane_ctx_get_op(ctx);
854 if (op == DPLANE_OP_RULE_ADD || op == DPLANE_OP_RULE_UPDATE)
855 zsend_rule_notify_owner(ctx, res == ZEBRA_DPLANE_REQUEST_SUCCESS
856 ? ZAPI_RULE_INSTALLED
857 : ZAPI_RULE_FAIL_INSTALL);
858 else if (op == DPLANE_OP_RULE_DELETE)
859 zsend_rule_notify_owner(ctx, res == ZEBRA_DPLANE_REQUEST_SUCCESS
860 ? ZAPI_RULE_REMOVED
861 : ZAPI_RULE_FAIL_REMOVE);
5162e000
PG
862 else if (op == DPLANE_OP_IPTABLE_ADD)
863 zsend_iptable_notify_owner(ctx,
864 res == ZEBRA_DPLANE_REQUEST_SUCCESS
865 ? ZAPI_IPTABLE_INSTALLED
866 : ZAPI_IPTABLE_FAIL_INSTALL);
867 else if (op == DPLANE_OP_IPTABLE_DELETE)
868 zsend_iptable_notify_owner(ctx,
869 res == ZEBRA_DPLANE_REQUEST_SUCCESS
870 ? ZAPI_IPTABLE_REMOVED
871 : ZAPI_IPTABLE_FAIL_REMOVE);
ef524230
PG
872 else if (op == DPLANE_OP_IPSET_ADD)
873 zsend_ipset_notify_owner(ctx,
874 res == ZEBRA_DPLANE_REQUEST_SUCCESS
875 ? ZAPI_IPSET_INSTALLED
876 : ZAPI_IPSET_FAIL_INSTALL);
877 else if (op == DPLANE_OP_IPSET_DELETE)
878 zsend_ipset_notify_owner(ctx,
879 res == ZEBRA_DPLANE_REQUEST_SUCCESS
880 ? ZAPI_IPSET_REMOVED
881 : ZAPI_IPSET_FAIL_REMOVE);
882 else if (op == DPLANE_OP_IPSET_ENTRY_ADD)
883 zsend_ipset_entry_notify_owner(
884 ctx, res == ZEBRA_DPLANE_REQUEST_SUCCESS
885 ? ZAPI_IPSET_ENTRY_INSTALLED
886 : ZAPI_IPSET_ENTRY_FAIL_INSTALL);
887 else if (op == DPLANE_OP_IPSET_ENTRY_DELETE)
888 zsend_ipset_entry_notify_owner(
889 ctx, res == ZEBRA_DPLANE_REQUEST_SUCCESS
890 ? ZAPI_IPSET_ENTRY_REMOVED
891 : ZAPI_IPSET_ENTRY_FAIL_REMOVE);
f62e5480
JU
892 else
893 flog_err(
894 EC_ZEBRA_PBR_RULE_UPDATE,
895 "Context received in pbr rule dplane result handler with incorrect OP code (%u)",
896 op);
897
898
899 dplane_ctx_fini(&ctx);
942bf97b 900}
901
902/*
903 * Handle rule delete notification from kernel.
904 */
a0321978 905int kernel_pbr_rule_del(struct zebra_pbr_rule *rule)
942bf97b 906{
907 return 0;
908}
586f4ccf
PG
909
910struct zebra_pbr_ipset_entry_unique_display {
911 struct zebra_pbr_ipset *zpi;
912 struct vty *vty;
73a829f7 913 struct zebra_ns *zns;
586f4ccf
PG
914};
915
916struct zebra_pbr_env_display {
917 struct zebra_ns *zns;
918 struct vty *vty;
7929821a 919 char *name;
586f4ccf
PG
920};
921
922static const char *zebra_pbr_prefix2str(union prefixconstptr pu,
923 char *str, int size)
924{
925 const struct prefix *p = pu.p;
926 char buf[PREFIX2STR_BUFFER];
927
a60b7031
PG
928 if ((p->family == AF_INET && p->prefixlen == IPV4_MAX_PREFIXLEN) ||
929 (p->family == AF_INET6 && p->prefixlen == IPV6_MAX_PREFIXLEN)) {
586f4ccf
PG
930 snprintf(str, size, "%s", inet_ntop(p->family, &p->u.prefix,
931 buf, PREFIX2STR_BUFFER));
932 return str;
933 }
934 return prefix2str(pu, str, size);
935}
936
be729dd7
PG
937static void zebra_pbr_display_icmp(struct vty *vty,
938 struct zebra_pbr_ipset_entry *zpie)
939{
940 char decoded_str[20];
941 uint16_t port;
c9b1139a
PG
942 struct zebra_pbr_ipset *zpi;
943
944 zpi = zpie->backpointer;
be729dd7
PG
945
946 /* range icmp type */
947 if (zpie->src_port_max || zpie->dst_port_max) {
2b7165e7 948 vty_out(vty, ":icmp:[type <%u:%u>;code <%u:%u>",
be729dd7
PG
949 zpie->src_port_min, zpie->src_port_max,
950 zpie->dst_port_min, zpie->dst_port_max);
951 } else {
952 port = ((zpie->src_port_min << 8) & 0xff00) +
953 (zpie->dst_port_min & 0xff);
954 memset(decoded_str, 0, sizeof(decoded_str));
2b7165e7 955 snprintf(decoded_str, sizeof(decoded_str), "%u/%u",
772270f3 956 zpie->src_port_min, zpie->dst_port_min);
c9b1139a
PG
957 vty_out(vty, ":%s:%s",
958 zpi->family == AF_INET6 ? "ipv6-icmp" : "icmp",
959 lookup_msg(zpi->family == AF_INET6 ?
960 icmpv6_typecode_str : icmp_typecode_str,
be729dd7
PG
961 port, decoded_str));
962 }
963}
964
25d760c5
PG
965static void zebra_pbr_display_port(struct vty *vty, uint32_t filter_bm,
966 uint16_t port_min, uint16_t port_max,
967 uint8_t proto)
968{
969 if (!(filter_bm & PBR_FILTER_PROTO)) {
970 if (port_max)
971 vty_out(vty, ":udp/tcp:%d-%d",
972 port_min, port_max);
973 else
974 vty_out(vty, ":udp/tcp:%d",
975 port_min);
976 } else {
977 if (port_max)
978 vty_out(vty, ":proto %d:%d-%d",
979 proto, port_min, port_max);
980 else
981 vty_out(vty, ":proto %d:%d",
982 proto, port_min);
983 }
984}
985
e3b78da8 986static int zebra_pbr_show_ipset_entry_walkcb(struct hash_bucket *bucket,
586f4ccf
PG
987 void *arg)
988{
989 struct zebra_pbr_ipset_entry_unique_display *unique =
990 (struct zebra_pbr_ipset_entry_unique_display *)arg;
991 struct zebra_pbr_ipset *zpi = unique->zpi;
992 struct vty *vty = unique->vty;
993 struct zebra_pbr_ipset_entry *zpie =
e3b78da8 994 (struct zebra_pbr_ipset_entry *)bucket->data;
73a829f7 995 uint64_t pkts = 0, bytes = 0;
73a829f7 996 int ret = 0;
586f4ccf
PG
997
998 if (zpie->backpointer != zpi)
999 return HASHWALK_CONTINUE;
1000
25d760c5
PG
1001 if ((zpi->type == IPSET_NET_NET) ||
1002 (zpi->type == IPSET_NET_PORT_NET)) {
586f4ccf
PG
1003 char buf[PREFIX_STRLEN];
1004
1005 zebra_pbr_prefix2str(&(zpie->src), buf, sizeof(buf));
1006 vty_out(vty, "\tfrom %s", buf);
be729dd7
PG
1007 if (zpie->filter_bm & PBR_FILTER_SRC_PORT &&
1008 zpie->proto != IPPROTO_ICMP)
25d760c5
PG
1009 zebra_pbr_display_port(vty, zpie->filter_bm,
1010 zpie->src_port_min,
1011 zpie->src_port_max,
1012 zpie->proto);
586f4ccf
PG
1013 vty_out(vty, " to ");
1014 zebra_pbr_prefix2str(&(zpie->dst), buf, sizeof(buf));
1015 vty_out(vty, "%s", buf);
be729dd7
PG
1016 if (zpie->filter_bm & PBR_FILTER_DST_PORT &&
1017 zpie->proto != IPPROTO_ICMP)
25d760c5
PG
1018 zebra_pbr_display_port(vty, zpie->filter_bm,
1019 zpie->dst_port_min,
1020 zpie->dst_port_max,
1021 zpie->proto);
be729dd7
PG
1022 if (zpie->proto == IPPROTO_ICMP)
1023 zebra_pbr_display_icmp(vty, zpie);
25d760c5
PG
1024 } else if ((zpi->type == IPSET_NET) ||
1025 (zpi->type == IPSET_NET_PORT)) {
586f4ccf
PG
1026 char buf[PREFIX_STRLEN];
1027
1028 if (zpie->filter_bm & PBR_FILTER_SRC_IP) {
1029 zebra_pbr_prefix2str(&(zpie->src), buf, sizeof(buf));
1030 vty_out(vty, "\tfrom %s", buf);
1031 }
be729dd7
PG
1032 if (zpie->filter_bm & PBR_FILTER_SRC_PORT &&
1033 zpie->proto != IPPROTO_ICMP)
25d760c5
PG
1034 zebra_pbr_display_port(vty, zpie->filter_bm,
1035 zpie->src_port_min,
1036 zpie->src_port_max,
1037 zpie->proto);
586f4ccf
PG
1038 if (zpie->filter_bm & PBR_FILTER_DST_IP) {
1039 zebra_pbr_prefix2str(&(zpie->dst), buf, sizeof(buf));
1040 vty_out(vty, "\tto %s", buf);
1041 }
be729dd7
PG
1042 if (zpie->filter_bm & PBR_FILTER_DST_PORT &&
1043 zpie->proto != IPPROTO_ICMP)
25d760c5
PG
1044 zebra_pbr_display_port(vty, zpie->filter_bm,
1045 zpie->dst_port_min,
1046 zpie->dst_port_max,
1047 zpie->proto);
be729dd7
PG
1048 if (zpie->proto == IPPROTO_ICMP)
1049 zebra_pbr_display_icmp(vty, zpie);
586f4ccf
PG
1050 }
1051 vty_out(vty, " (%u)\n", zpie->unique);
1052
1c6fca1f 1053 ret = hook_call(zebra_pbr_ipset_entry_get_stat, zpie, &pkts,
62f20a52 1054 &bytes);
73a829f7
PG
1055 if (ret && pkts > 0)
1056 vty_out(vty, "\t pkts %" PRIu64 ", bytes %" PRIu64"\n",
1057 pkts, bytes);
586f4ccf
PG
1058 return HASHWALK_CONTINUE;
1059}
1060
e3b78da8 1061static int zebra_pbr_show_ipset_walkcb(struct hash_bucket *bucket, void *arg)
586f4ccf
PG
1062{
1063 struct zebra_pbr_env_display *uniqueipset =
1064 (struct zebra_pbr_env_display *)arg;
e3b78da8 1065 struct zebra_pbr_ipset *zpi = (struct zebra_pbr_ipset *)bucket->data;
586f4ccf
PG
1066 struct zebra_pbr_ipset_entry_unique_display unique;
1067 struct vty *vty = uniqueipset->vty;
1068 struct zebra_ns *zns = uniqueipset->zns;
1069
a60b7031
PG
1070 vty_out(vty, "IPset %s type %s family %s\n", zpi->ipset_name,
1071 zebra_pbr_ipset_type2str(zpi->type),
c6423c31 1072 family2str(zpi->family));
586f4ccf
PG
1073 unique.vty = vty;
1074 unique.zpi = zpi;
73a829f7 1075 unique.zns = zns;
62f20a52 1076 hash_walk(zrouter.ipset_entry_hash, zebra_pbr_show_ipset_entry_walkcb,
586f4ccf
PG
1077 &unique);
1078 vty_out(vty, "\n");
1079 return HASHWALK_CONTINUE;
1080}
1081
dc993e76
PG
1082size_t zebra_pbr_tcpflags_snprintf(char *buffer, size_t len,
1083 uint16_t tcp_val)
1084{
1085 size_t len_written = 0;
1086 static struct message nt = {0};
1087 const struct message *pnt;
1088 int incr = 0;
1089
1090 for (pnt = tcp_value_str;
1091 memcmp(pnt, &nt, sizeof(struct message)); pnt++)
1092 if (pnt->key & tcp_val) {
1093 len_written += snprintf(buffer + len_written,
1094 len - len_written,
1095 "%s%s", incr ?
1096 ",":"", pnt->str);
1097 incr++;
1098 }
1099 return len_written;
1100}
1101
586f4ccf
PG
1102/*
1103 */
1104void zebra_pbr_show_ipset_list(struct vty *vty, char *ipsetname)
1105{
1106 struct zebra_pbr_ipset *zpi;
1107 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1108 struct zebra_pbr_ipset_entry_unique_display unique;
1109 struct zebra_pbr_env_display uniqueipset;
1110
1111 if (ipsetname) {
62f20a52 1112 zpi = zebra_pbr_lookup_ipset_pername(ipsetname);
586f4ccf
PG
1113 if (!zpi) {
1114 vty_out(vty, "No IPset %s found\n", ipsetname);
1115 return;
1116 }
a60b7031
PG
1117 vty_out(vty, "IPset %s type %s family %s\n", ipsetname,
1118 zebra_pbr_ipset_type2str(zpi->type),
c6423c31 1119 family2str(zpi->family));
586f4ccf
PG
1120 unique.vty = vty;
1121 unique.zpi = zpi;
73a829f7 1122 unique.zns = zns;
62f20a52
DS
1123 hash_walk(zrouter.ipset_entry_hash,
1124 zebra_pbr_show_ipset_entry_walkcb, &unique);
586f4ccf
PG
1125 return;
1126 }
1127 uniqueipset.zns = zns;
1128 uniqueipset.vty = vty;
7929821a 1129 uniqueipset.name = NULL;
62f20a52 1130 hash_walk(zrouter.ipset_hash, zebra_pbr_show_ipset_walkcb,
586f4ccf
PG
1131 &uniqueipset);
1132}
1133
1134struct pbr_rule_fwmark_lookup {
1135 struct zebra_pbr_rule *ptr;
1136 uint32_t fwmark;
1137};
1138
e3b78da8 1139static int zebra_pbr_rule_lookup_fwmark_walkcb(struct hash_bucket *bucket,
586f4ccf
PG
1140 void *arg)
1141{
1142 struct pbr_rule_fwmark_lookup *iprule =
1143 (struct pbr_rule_fwmark_lookup *)arg;
e3b78da8 1144 struct zebra_pbr_rule *zpr = (struct zebra_pbr_rule *)bucket->data;
586f4ccf
PG
1145
1146 if (iprule->fwmark == zpr->rule.filter.fwmark) {
1147 iprule->ptr = zpr;
1148 return HASHWALK_ABORT;
1149 }
1150 return HASHWALK_CONTINUE;
1151}
1152
7929821a
PG
1153static void zebra_pbr_show_iptable_unit(struct zebra_pbr_iptable *iptable,
1154 struct vty *vty,
1155 struct zebra_ns *zns)
586f4ccf 1156{
73a829f7
PG
1157 int ret;
1158 uint64_t pkts = 0, bytes = 0;
586f4ccf 1159
a60b7031
PG
1160 vty_out(vty, "IPtable %s family %s action %s (%u)\n",
1161 iptable->ipset_name,
c9b1139a 1162 family2str(iptable->family),
586f4ccf
PG
1163 iptable->action == ZEBRA_IPTABLES_DROP ? "drop" : "redirect",
1164 iptable->unique);
0b328d3f
PG
1165 if (iptable->type == IPSET_NET_PORT ||
1166 iptable->type == IPSET_NET_PORT_NET) {
1167 if (!(iptable->filter_bm & MATCH_ICMP_SET)) {
1168 if (iptable->filter_bm & PBR_FILTER_DST_PORT)
1169 vty_out(vty, "\t lookup dst port\n");
1170 else if (iptable->filter_bm & PBR_FILTER_SRC_PORT)
1171 vty_out(vty, "\t lookup src port\n");
1172 }
1173 }
e7f7dad4
PG
1174 if (iptable->pkt_len_min || iptable->pkt_len_max) {
1175 if (!iptable->pkt_len_max)
1176 vty_out(vty, "\t pkt len %u\n",
1177 iptable->pkt_len_min);
1178 else
1179 vty_out(vty, "\t pkt len [%u;%u]\n",
1180 iptable->pkt_len_min,
1181 iptable->pkt_len_max);
1182 }
dc993e76
PG
1183 if (iptable->tcp_flags || iptable->tcp_mask_flags) {
1184 char tcp_flag_str[64];
1185 char tcp_flag_mask_str[64];
1186
1187 zebra_pbr_tcpflags_snprintf(tcp_flag_str,
1188 sizeof(tcp_flag_str),
1189 iptable->tcp_flags);
1190 zebra_pbr_tcpflags_snprintf(tcp_flag_mask_str,
1191 sizeof(tcp_flag_mask_str),
1192 iptable->tcp_mask_flags);
1193 vty_out(vty, "\t tcpflags [%s/%s]\n",
1194 tcp_flag_str, tcp_flag_mask_str);
1195 }
69214c57
PG
1196 if (iptable->filter_bm & (MATCH_DSCP_SET | MATCH_DSCP_INVERSE_SET)) {
1197 vty_out(vty, "\t dscp %s %d\n",
1198 iptable->filter_bm & MATCH_DSCP_INVERSE_SET ?
1199 "not" : "", iptable->dscp_value);
1200 }
a60b7031
PG
1201 if (iptable->filter_bm & (MATCH_FLOW_LABEL_SET |
1202 MATCH_FLOW_LABEL_INVERSE_SET)) {
1203 vty_out(vty, "\t flowlabel %s %d\n",
1204 iptable->filter_bm & MATCH_FLOW_LABEL_INVERSE_SET ?
1205 "not" : "", iptable->flow_label);
1206 }
5ac5b7cc
PG
1207 if (iptable->fragment) {
1208 char val_str[10];
1209
772270f3 1210 snprintf(val_str, sizeof(val_str), "%d", iptable->fragment);
5ac5b7cc
PG
1211 vty_out(vty, "\t fragment%s %s\n",
1212 iptable->filter_bm & MATCH_FRAGMENT_INVERSE_SET ?
1213 " not" : "", lookup_msg(fragment_value_str,
1214 iptable->fragment, val_str));
1215 }
f449d223
PG
1216 if (iptable->protocol) {
1217 vty_out(vty, "\t protocol %d\n",
1218 iptable->protocol);
1219 }
1c6fca1f 1220 ret = hook_call(zebra_pbr_iptable_get_stat, iptable, &pkts,
62f20a52 1221 &bytes);
73a829f7
PG
1222 if (ret && pkts > 0)
1223 vty_out(vty, "\t pkts %" PRIu64 ", bytes %" PRIu64"\n",
1224 pkts, bytes);
586f4ccf
PG
1225 if (iptable->action != ZEBRA_IPTABLES_DROP) {
1226 struct pbr_rule_fwmark_lookup prfl;
1227
1228 prfl.fwmark = iptable->fwmark;
1229 prfl.ptr = NULL;
7f0ea8a4 1230 hash_walk(zrouter.rules_hash,
586f4ccf
PG
1231 &zebra_pbr_rule_lookup_fwmark_walkcb, &prfl);
1232 if (prfl.ptr) {
1233 struct zebra_pbr_rule *zpr = prfl.ptr;
1234
1235 vty_out(vty, "\t table %u, fwmark %u\n",
1236 zpr->rule.action.table,
1237 prfl.fwmark);
1238 }
1239 }
7929821a
PG
1240}
1241
e3b78da8 1242static int zebra_pbr_show_iptable_walkcb(struct hash_bucket *bucket, void *arg)
7929821a
PG
1243{
1244 struct zebra_pbr_iptable *iptable =
e3b78da8 1245 (struct zebra_pbr_iptable *)bucket->data;
7929821a
PG
1246 struct zebra_pbr_env_display *env = (struct zebra_pbr_env_display *)arg;
1247 struct vty *vty = env->vty;
1248 struct zebra_ns *zns = env->zns;
1249 char *iptable_name = env->name;
1250
1251 if (!iptable_name)
1252 zebra_pbr_show_iptable_unit(iptable, vty, zns);
1253 else if (!strncmp(iptable_name,
1254 iptable->ipset_name,
1255 ZEBRA_IPSET_NAME_SIZE))
1256 zebra_pbr_show_iptable_unit(iptable, vty, zns);
586f4ccf
PG
1257 return HASHWALK_CONTINUE;
1258}
1259
7929821a 1260void zebra_pbr_show_iptable(struct vty *vty, char *iptable_name)
586f4ccf
PG
1261{
1262 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1263 struct zebra_pbr_env_display env;
1264
1265 env.vty = vty;
1266 env.zns = zns;
7929821a 1267 env.name = iptable_name;
62f20a52 1268 hash_walk(zrouter.iptable_hash, zebra_pbr_show_iptable_walkcb, &env);
586f4ccf 1269}
f80ec7e3
PG
1270
1271void zebra_pbr_iptable_update_interfacelist(struct stream *s,
1272 struct zebra_pbr_iptable *zpi)
1273{
1274 uint32_t i = 0, index;
1275 struct interface *ifp;
1276 char *name;
1277
1278 for (i = 0; i < zpi->nb_interface; i++) {
1279 STREAM_GETL(s, index);
1280 ifp = if_lookup_by_index(index, zpi->vrf_id);
1281 if (!ifp)
1282 continue;
1283 name = XSTRDUP(MTYPE_PBR_IPTABLE_IFNAME, ifp->name);
1284 listnode_add(zpi->interface_name_list, name);
1285 }
1286stream_failure:
1287 return;
1288}