]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_pbr.c
Merge pull request #5706 from mjstapp/fix_nh_debug_show
[mirror_frr.git] / zebra / zebra_pbr.c
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
24 #include <jhash.h>
25 #include <hash.h>
26 #include <memory.h>
27 #include <hook.h>
28
29 #include "zebra/zebra_router.h"
30 #include "zebra/zebra_pbr.h"
31 #include "zebra/rt.h"
32 #include "zebra/zapi_msg.h"
33 #include "zebra/zebra_memory.h"
34 #include "zebra/zserv.h"
35
36 /* definitions */
37 DEFINE_MTYPE_STATIC(ZEBRA, PBR_IPTABLE_IFNAME, "PBR interface list")
38
39 /* definitions */
40 static 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 };
47
48 const 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
86 /* definitions */
87 static const struct message tcp_value_str[] = {
88 {TCP_HEADER_FIN, "FIN"},
89 {TCP_HEADER_SYN, "SYN"},
90 {TCP_HEADER_RST, "RST"},
91 {TCP_HEADER_PSH, "PSH"},
92 {TCP_HEADER_ACK, "ACK"},
93 {TCP_HEADER_URG, "URG"},
94 {0}
95 };
96
97 static const struct message fragment_value_str[] = {
98 {1, "dont-fragment"},
99 {2, "is-fragment"},
100 {4, "first-fragment"},
101 {8, "last-fragment"},
102 {0}
103 };
104
105 /* static function declarations */
106 DEFINE_HOOK(zebra_pbr_ipset_entry_get_stat,
107 (struct zebra_pbr_ipset_entry *ipset, uint64_t *pkts,
108 uint64_t *bytes),
109 (ipset, pkts, bytes))
110
111 DEFINE_HOOK(zebra_pbr_iptable_get_stat,
112 (struct zebra_pbr_iptable *iptable, uint64_t *pkts,
113 uint64_t *bytes),
114 (iptable, pkts, bytes))
115
116 DEFINE_HOOK(zebra_pbr_iptable_update,
117 (int cmd, struct zebra_pbr_iptable *iptable), (cmd, iptable));
118
119 DEFINE_HOOK(zebra_pbr_ipset_entry_update,
120 (int cmd, struct zebra_pbr_ipset_entry *ipset), (cmd, ipset));
121
122 DEFINE_HOOK(zebra_pbr_ipset_update,
123 (int cmd, struct zebra_pbr_ipset *ipset), (cmd, ipset));
124
125 /* Private functions */
126
127 /* Public functions */
128 void zebra_pbr_rules_free(void *arg)
129 {
130 struct zebra_pbr_rule *rule;
131
132 rule = (struct zebra_pbr_rule *)arg;
133
134 (void)kernel_del_pbr_rule(rule);
135 XFREE(MTYPE_TMP, rule);
136 }
137
138 uint32_t zebra_pbr_rules_hash_key(const void *arg)
139 {
140 const struct zebra_pbr_rule *rule;
141 uint32_t key;
142
143 rule = arg;
144 key = jhash_3words(rule->rule.seq, rule->rule.priority,
145 rule->rule.action.table,
146 prefix_hash_key(&rule->rule.filter.src_ip));
147
148 if (rule->rule.filter.fwmark)
149 key = jhash_3words(rule->rule.filter.fwmark, rule->vrf_id,
150 rule->rule.ifindex, key);
151 else
152 key = jhash_2words(rule->vrf_id, rule->rule.ifindex, key);
153
154 return jhash_3words(rule->rule.filter.src_port,
155 rule->rule.filter.dst_port,
156 prefix_hash_key(&rule->rule.filter.dst_ip),
157 jhash_1word(rule->rule.unique, key));
158 }
159
160 bool zebra_pbr_rules_hash_equal(const void *arg1, const void *arg2)
161 {
162 const struct zebra_pbr_rule *r1, *r2;
163
164 r1 = (const struct zebra_pbr_rule *)arg1;
165 r2 = (const struct zebra_pbr_rule *)arg2;
166
167 if (r1->rule.seq != r2->rule.seq)
168 return false;
169
170 if (r1->rule.priority != r2->rule.priority)
171 return false;
172
173 if (r1->rule.unique != r2->rule.unique)
174 return false;
175
176 if (r1->rule.action.table != r2->rule.action.table)
177 return false;
178
179 if (r1->rule.filter.src_port != r2->rule.filter.src_port)
180 return false;
181
182 if (r1->rule.filter.dst_port != r2->rule.filter.dst_port)
183 return false;
184
185 if (r1->rule.filter.fwmark != r2->rule.filter.fwmark)
186 return false;
187
188 if (!prefix_same(&r1->rule.filter.src_ip, &r2->rule.filter.src_ip))
189 return false;
190
191 if (!prefix_same(&r1->rule.filter.dst_ip, &r2->rule.filter.dst_ip))
192 return false;
193
194 if (r1->rule.ifindex != r2->rule.ifindex)
195 return false;
196
197 if (r1->vrf_id != r2->vrf_id)
198 return false;
199
200 return true;
201 }
202
203 struct pbr_rule_unique_lookup {
204 struct zebra_pbr_rule *rule;
205 uint32_t unique;
206 ifindex_t ifindex;
207 vrf_id_t vrf_id;
208 };
209
210 static int pbr_rule_lookup_unique_walker(struct hash_bucket *b, void *data)
211 {
212 struct pbr_rule_unique_lookup *pul = data;
213 struct zebra_pbr_rule *rule = b->data;
214
215 if (pul->unique == rule->rule.unique
216 && pul->ifindex == rule->rule.ifindex
217 && pul->vrf_id == rule->vrf_id) {
218 pul->rule = rule;
219 return HASHWALK_ABORT;
220 }
221
222 return HASHWALK_CONTINUE;
223 }
224
225 static struct zebra_pbr_rule *
226 pbr_rule_lookup_unique(struct zebra_pbr_rule *zrule)
227 {
228 struct pbr_rule_unique_lookup pul;
229
230 pul.unique = zrule->rule.unique;
231 pul.ifindex = zrule->rule.ifindex;
232 pul.rule = NULL;
233 pul.vrf_id = zrule->vrf_id;
234 hash_walk(zrouter.rules_hash, &pbr_rule_lookup_unique_walker, &pul);
235
236 return pul.rule;
237 }
238
239 void zebra_pbr_ipset_free(void *arg)
240 {
241 struct zebra_pbr_ipset *ipset;
242
243 ipset = (struct zebra_pbr_ipset *)arg;
244 hook_call(zebra_pbr_ipset_update, 0, ipset);
245 XFREE(MTYPE_TMP, ipset);
246 }
247
248 uint32_t zebra_pbr_ipset_hash_key(const void *arg)
249 {
250 const struct zebra_pbr_ipset *ipset = arg;
251 uint32_t *pnt = (uint32_t *)&ipset->ipset_name;
252 uint32_t key = jhash_1word(ipset->vrf_id, 0x63ab42de);
253
254 return jhash2(pnt, ZEBRA_IPSET_NAME_HASH_SIZE, key);
255 }
256
257 bool zebra_pbr_ipset_hash_equal(const void *arg1, const void *arg2)
258 {
259 const struct zebra_pbr_ipset *r1, *r2;
260
261 r1 = (const struct zebra_pbr_ipset *)arg1;
262 r2 = (const struct zebra_pbr_ipset *)arg2;
263
264 if (r1->type != r2->type)
265 return false;
266 if (r1->unique != r2->unique)
267 return false;
268 if (r1->vrf_id != r2->vrf_id)
269 return false;
270
271 if (strncmp(r1->ipset_name, r2->ipset_name,
272 ZEBRA_IPSET_NAME_SIZE))
273 return false;
274 return true;
275 }
276
277 void zebra_pbr_ipset_entry_free(void *arg)
278 {
279 struct zebra_pbr_ipset_entry *ipset;
280
281 ipset = (struct zebra_pbr_ipset_entry *)arg;
282
283 hook_call(zebra_pbr_ipset_entry_update, 0, ipset);
284
285 XFREE(MTYPE_TMP, ipset);
286 }
287
288 uint32_t zebra_pbr_ipset_entry_hash_key(const void *arg)
289 {
290 const struct zebra_pbr_ipset_entry *ipset;
291 uint32_t key;
292
293 ipset = arg;
294 key = prefix_hash_key(&ipset->src);
295 key = jhash_1word(ipset->unique, key);
296 key = jhash_1word(prefix_hash_key(&ipset->dst), key);
297 key = jhash(&ipset->dst_port_min, 2, key);
298 key = jhash(&ipset->dst_port_max, 2, key);
299 key = jhash(&ipset->src_port_min, 2, key);
300 key = jhash(&ipset->src_port_max, 2, key);
301 key = jhash(&ipset->proto, 1, key);
302
303 return key;
304 }
305
306 bool zebra_pbr_ipset_entry_hash_equal(const void *arg1, const void *arg2)
307 {
308 const struct zebra_pbr_ipset_entry *r1, *r2;
309
310 r1 = (const struct zebra_pbr_ipset_entry *)arg1;
311 r2 = (const struct zebra_pbr_ipset_entry *)arg2;
312
313 if (r1->unique != r2->unique)
314 return false;
315
316 if (!prefix_same(&r1->src, &r2->src))
317 return false;
318
319 if (!prefix_same(&r1->dst, &r2->dst))
320 return false;
321
322 if (r1->src_port_min != r2->src_port_min)
323 return false;
324
325 if (r1->src_port_max != r2->src_port_max)
326 return false;
327
328 if (r1->dst_port_min != r2->dst_port_min)
329 return false;
330
331 if (r1->dst_port_max != r2->dst_port_max)
332 return false;
333
334 if (r1->proto != r2->proto)
335 return false;
336 return true;
337 }
338
339 void zebra_pbr_iptable_free(void *arg)
340 {
341 struct zebra_pbr_iptable *iptable;
342 struct listnode *node, *nnode;
343 char *name;
344
345 iptable = (struct zebra_pbr_iptable *)arg;
346 hook_call(zebra_pbr_iptable_update, 0, iptable);
347
348 if (iptable->interface_name_list) {
349 for (ALL_LIST_ELEMENTS(iptable->interface_name_list, node,
350 nnode, name)) {
351 XFREE(MTYPE_PBR_IPTABLE_IFNAME, name);
352 list_delete_node(iptable->interface_name_list, node);
353 }
354 list_delete(&iptable->interface_name_list);
355 }
356 XFREE(MTYPE_TMP, iptable);
357 }
358
359 uint32_t zebra_pbr_iptable_hash_key(const void *arg)
360 {
361 const struct zebra_pbr_iptable *iptable = arg;
362 uint32_t *pnt = (uint32_t *)&(iptable->ipset_name);
363 uint32_t key;
364
365 key = jhash2(pnt, ZEBRA_IPSET_NAME_HASH_SIZE,
366 0x63ab42de);
367 key = jhash_1word(iptable->fwmark, key);
368 key = jhash_1word(iptable->pkt_len_min, key);
369 key = jhash_1word(iptable->pkt_len_max, key);
370 key = jhash_1word(iptable->tcp_flags, key);
371 key = jhash_1word(iptable->tcp_mask_flags, key);
372 key = jhash_1word(iptable->dscp_value, key);
373 key = jhash_1word(iptable->protocol, key);
374 key = jhash_1word(iptable->fragment, key);
375 key = jhash_1word(iptable->vrf_id, key);
376
377 return jhash_3words(iptable->filter_bm, iptable->type,
378 iptable->unique, key);
379 }
380
381 bool zebra_pbr_iptable_hash_equal(const void *arg1, const void *arg2)
382 {
383 const struct zebra_pbr_iptable *r1, *r2;
384
385 r1 = (const struct zebra_pbr_iptable *)arg1;
386 r2 = (const struct zebra_pbr_iptable *)arg2;
387
388 if (r1->vrf_id != r2->vrf_id)
389 return false;
390 if (r1->type != r2->type)
391 return false;
392 if (r1->unique != r2->unique)
393 return false;
394 if (r1->filter_bm != r2->filter_bm)
395 return false;
396 if (r1->fwmark != r2->fwmark)
397 return false;
398 if (r1->action != r2->action)
399 return false;
400 if (strncmp(r1->ipset_name, r2->ipset_name,
401 ZEBRA_IPSET_NAME_SIZE))
402 return false;
403 if (r1->pkt_len_min != r2->pkt_len_min)
404 return false;
405 if (r1->pkt_len_max != r2->pkt_len_max)
406 return false;
407 if (r1->tcp_flags != r2->tcp_flags)
408 return false;
409 if (r1->tcp_mask_flags != r2->tcp_mask_flags)
410 return false;
411 if (r1->dscp_value != r2->dscp_value)
412 return false;
413 if (r1->fragment != r2->fragment)
414 return false;
415 if (r1->protocol != r2->protocol)
416 return false;
417 return true;
418 }
419
420 static void *pbr_rule_alloc_intern(void *arg)
421 {
422 struct zebra_pbr_rule *zpr;
423 struct zebra_pbr_rule *new;
424
425 zpr = (struct zebra_pbr_rule *)arg;
426
427 new = XCALLOC(MTYPE_TMP, sizeof(*new));
428
429 memcpy(new, zpr, sizeof(*zpr));
430
431 return new;
432 }
433
434 void zebra_pbr_add_rule(struct zebra_pbr_rule *rule)
435 {
436 struct zebra_pbr_rule *unique =
437 pbr_rule_lookup_unique(rule);
438
439 (void)hash_get(zrouter.rules_hash, rule, pbr_rule_alloc_intern);
440 (void)kernel_add_pbr_rule(rule);
441 /*
442 * Rule Replace semantics, if we have an old, install the
443 * new rule, look above, and then delete the old
444 */
445 if (unique)
446 zebra_pbr_del_rule(unique);
447 }
448
449 void zebra_pbr_del_rule(struct zebra_pbr_rule *rule)
450 {
451 struct zebra_pbr_rule *lookup;
452
453 lookup = hash_lookup(zrouter.rules_hash, rule);
454 (void)kernel_del_pbr_rule(rule);
455
456 if (lookup) {
457 hash_release(zrouter.rules_hash, lookup);
458 XFREE(MTYPE_TMP, lookup);
459 } else
460 zlog_debug("%s: Rule being deleted we know nothing about",
461 __PRETTY_FUNCTION__);
462 }
463
464 static void zebra_pbr_cleanup_rules(struct hash_bucket *b, void *data)
465 {
466 struct zebra_pbr_rule *rule = b->data;
467 int *sock = data;
468
469 if (rule->sock == *sock) {
470 (void)kernel_del_pbr_rule(rule);
471 if (hash_release(zrouter.rules_hash, rule))
472 XFREE(MTYPE_TMP, rule);
473 else
474 zlog_debug(
475 "%s: Rule seq: %u is being cleaned but we can't find it in our tables",
476 __func__, rule->rule.seq);
477 }
478 }
479
480 static void zebra_pbr_cleanup_ipset(struct hash_bucket *b, void *data)
481 {
482 struct zebra_pbr_ipset *ipset = b->data;
483 int *sock = data;
484
485 if (ipset->sock == *sock) {
486 if (hash_release(zrouter.ipset_hash, ipset))
487 zebra_pbr_ipset_free(ipset);
488 else
489 hook_call(zebra_pbr_ipset_update, 0, ipset);
490 }
491 }
492
493 static void zebra_pbr_cleanup_ipset_entry(struct hash_bucket *b, void *data)
494 {
495 struct zebra_pbr_ipset_entry *ipset = b->data;
496 int *sock = data;
497
498 if (ipset->sock == *sock) {
499 if (hash_release(zrouter.ipset_entry_hash, ipset))
500 zebra_pbr_ipset_entry_free(ipset);
501 else
502 hook_call(zebra_pbr_ipset_entry_update, 0, ipset);
503 }
504 }
505
506 static void zebra_pbr_cleanup_iptable(struct hash_bucket *b, void *data)
507 {
508 struct zebra_pbr_iptable *iptable = b->data;
509 int *sock = data;
510
511 if (iptable->sock == *sock) {
512 if (hash_release(zrouter.iptable_hash, iptable))
513 zebra_pbr_iptable_free(iptable);
514 else
515 hook_call(zebra_pbr_iptable_update, 0, iptable);
516 }
517 }
518
519 static int zebra_pbr_client_close_cleanup(struct zserv *client)
520 {
521 int sock = client->sock;
522
523 if (!sock)
524 return 0;
525 hash_iterate(zrouter.rules_hash, zebra_pbr_cleanup_rules, &sock);
526 hash_iterate(zrouter.iptable_hash, zebra_pbr_cleanup_iptable, &sock);
527 hash_iterate(zrouter.ipset_entry_hash, zebra_pbr_cleanup_ipset_entry,
528 &sock);
529 hash_iterate(zrouter.ipset_hash, zebra_pbr_cleanup_ipset, &sock);
530 return 1;
531 }
532
533 void zebra_pbr_init(void)
534 {
535 hook_register(zserv_client_close, zebra_pbr_client_close_cleanup);
536 }
537
538 static void *pbr_ipset_alloc_intern(void *arg)
539 {
540 struct zebra_pbr_ipset *zpi;
541 struct zebra_pbr_ipset *new;
542
543 zpi = (struct zebra_pbr_ipset *)arg;
544
545 new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_ipset));
546
547 memcpy(new, zpi, sizeof(*zpi));
548
549 return new;
550 }
551
552 void zebra_pbr_create_ipset(struct zebra_pbr_ipset *ipset)
553 {
554 int ret;
555
556 (void)hash_get(zrouter.ipset_hash, ipset, pbr_ipset_alloc_intern);
557 ret = hook_call(zebra_pbr_ipset_update, 1, ipset);
558 kernel_pbr_ipset_add_del_status(ipset,
559 ret ? ZEBRA_DPLANE_INSTALL_SUCCESS
560 : ZEBRA_DPLANE_INSTALL_FAILURE);
561 }
562
563 void zebra_pbr_destroy_ipset(struct zebra_pbr_ipset *ipset)
564 {
565 struct zebra_pbr_ipset *lookup;
566
567 lookup = hash_lookup(zrouter.ipset_hash, ipset);
568 hook_call(zebra_pbr_ipset_update, 0, ipset);
569 if (lookup) {
570 hash_release(zrouter.ipset_hash, lookup);
571 XFREE(MTYPE_TMP, lookup);
572 } else
573 zlog_debug(
574 "%s: IPSet Entry being deleted we know nothing about",
575 __PRETTY_FUNCTION__);
576 }
577
578 struct pbr_ipset_name_lookup {
579 struct zebra_pbr_ipset *ipset;
580 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
581 };
582
583 const char *zebra_pbr_ipset_type2str(uint32_t type)
584 {
585 return lookup_msg(ipset_type_msg, type,
586 "Unrecognized IPset Type");
587 }
588
589 static int zebra_pbr_ipset_pername_walkcb(struct hash_bucket *bucket, void *arg)
590 {
591 struct pbr_ipset_name_lookup *pinl =
592 (struct pbr_ipset_name_lookup *)arg;
593 struct zebra_pbr_ipset *zpi = (struct zebra_pbr_ipset *)bucket->data;
594
595 if (!strncmp(pinl->ipset_name, zpi->ipset_name,
596 ZEBRA_IPSET_NAME_SIZE)) {
597 pinl->ipset = zpi;
598 return HASHWALK_ABORT;
599 }
600 return HASHWALK_CONTINUE;
601 }
602
603 struct zebra_pbr_ipset *zebra_pbr_lookup_ipset_pername(char *ipsetname)
604 {
605 struct pbr_ipset_name_lookup pinl;
606 struct pbr_ipset_name_lookup *ptr = &pinl;
607
608 if (!ipsetname)
609 return NULL;
610 memset(ptr, 0, sizeof(struct pbr_ipset_name_lookup));
611 snprintf((char *)ptr->ipset_name, ZEBRA_IPSET_NAME_SIZE, "%s",
612 ipsetname);
613 hash_walk(zrouter.ipset_hash, zebra_pbr_ipset_pername_walkcb, ptr);
614 return ptr->ipset;
615 }
616
617 static void *pbr_ipset_entry_alloc_intern(void *arg)
618 {
619 struct zebra_pbr_ipset_entry *zpi;
620 struct zebra_pbr_ipset_entry *new;
621
622 zpi = (struct zebra_pbr_ipset_entry *)arg;
623
624 new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_ipset_entry));
625
626 memcpy(new, zpi, sizeof(*zpi));
627
628 return new;
629 }
630
631 void zebra_pbr_add_ipset_entry(struct zebra_pbr_ipset_entry *ipset)
632 {
633 int ret;
634
635 (void)hash_get(zrouter.ipset_entry_hash, ipset,
636 pbr_ipset_entry_alloc_intern);
637 ret = hook_call(zebra_pbr_ipset_entry_update, 1, ipset);
638 kernel_pbr_ipset_entry_add_del_status(ipset,
639 ret ? ZEBRA_DPLANE_INSTALL_SUCCESS
640 : ZEBRA_DPLANE_INSTALL_FAILURE);
641 }
642
643 void zebra_pbr_del_ipset_entry(struct zebra_pbr_ipset_entry *ipset)
644 {
645 struct zebra_pbr_ipset_entry *lookup;
646
647 lookup = hash_lookup(zrouter.ipset_entry_hash, ipset);
648 hook_call(zebra_pbr_ipset_entry_update, 0, ipset);
649 if (lookup) {
650 hash_release(zrouter.ipset_entry_hash, lookup);
651 XFREE(MTYPE_TMP, lookup);
652 } else
653 zlog_debug("%s: IPSet being deleted we know nothing about",
654 __PRETTY_FUNCTION__);
655 }
656
657 static void *pbr_iptable_alloc_intern(void *arg)
658 {
659 struct zebra_pbr_iptable *zpi;
660 struct zebra_pbr_iptable *new;
661 struct listnode *ln;
662 char *ifname;
663
664 zpi = (struct zebra_pbr_iptable *)arg;
665
666 new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_iptable));
667
668 /* Deep structure copy */
669 memcpy(new, zpi, sizeof(*zpi));
670 new->interface_name_list = list_new();
671
672 if (zpi->interface_name_list) {
673 for (ALL_LIST_ELEMENTS_RO(zpi->interface_name_list, ln, ifname))
674 listnode_add(new->interface_name_list,
675 XSTRDUP(MTYPE_PBR_IPTABLE_IFNAME, ifname));
676 }
677
678 return new;
679 }
680
681 void zebra_pbr_add_iptable(struct zebra_pbr_iptable *iptable)
682 {
683 int ret;
684
685 (void)hash_get(zrouter.iptable_hash, iptable, pbr_iptable_alloc_intern);
686 ret = hook_call(zebra_pbr_iptable_update, 1, iptable);
687 kernel_pbr_iptable_add_del_status(iptable,
688 ret ? ZEBRA_DPLANE_INSTALL_SUCCESS
689 : ZEBRA_DPLANE_INSTALL_FAILURE);
690 }
691
692 void zebra_pbr_del_iptable(struct zebra_pbr_iptable *iptable)
693 {
694 struct zebra_pbr_iptable *lookup;
695
696 lookup = hash_lookup(zrouter.iptable_hash, iptable);
697 hook_call(zebra_pbr_iptable_update, 0, iptable);
698 if (lookup) {
699 struct listnode *node, *nnode;
700 char *name;
701
702 hash_release(zrouter.iptable_hash, lookup);
703 for (ALL_LIST_ELEMENTS(iptable->interface_name_list,
704 node, nnode, name)) {
705 XFREE(MTYPE_PBR_IPTABLE_IFNAME, name);
706 list_delete_node(iptable->interface_name_list,
707 node);
708 }
709 list_delete(&iptable->interface_name_list);
710 XFREE(MTYPE_TMP, lookup);
711 } else
712 zlog_debug("%s: IPTable being deleted we know nothing about",
713 __PRETTY_FUNCTION__);
714 }
715
716 /*
717 * Handle success or failure of rule (un)install in the kernel.
718 */
719 void kernel_pbr_rule_add_del_status(struct zebra_pbr_rule *rule,
720 enum zebra_dplane_status res)
721 {
722 switch (res) {
723 case ZEBRA_DPLANE_INSTALL_SUCCESS:
724 zsend_rule_notify_owner(rule, ZAPI_RULE_INSTALLED);
725 break;
726 case ZEBRA_DPLANE_INSTALL_FAILURE:
727 zsend_rule_notify_owner(rule, ZAPI_RULE_FAIL_INSTALL);
728 break;
729 case ZEBRA_DPLANE_DELETE_SUCCESS:
730 zsend_rule_notify_owner(rule, ZAPI_RULE_REMOVED);
731 break;
732 case ZEBRA_DPLANE_DELETE_FAILURE:
733 zsend_rule_notify_owner(rule, ZAPI_RULE_FAIL_REMOVE);
734 break;
735 case ZEBRA_DPLANE_STATUS_NONE:
736 break;
737 }
738 }
739
740 /*
741 * Handle success or failure of ipset (un)install in the kernel.
742 */
743 void kernel_pbr_ipset_add_del_status(struct zebra_pbr_ipset *ipset,
744 enum zebra_dplane_status res)
745 {
746 switch (res) {
747 case ZEBRA_DPLANE_INSTALL_SUCCESS:
748 zsend_ipset_notify_owner(ipset, ZAPI_IPSET_INSTALLED);
749 break;
750 case ZEBRA_DPLANE_INSTALL_FAILURE:
751 zsend_ipset_notify_owner(ipset, ZAPI_IPSET_FAIL_INSTALL);
752 break;
753 case ZEBRA_DPLANE_DELETE_SUCCESS:
754 zsend_ipset_notify_owner(ipset, ZAPI_IPSET_REMOVED);
755 break;
756 case ZEBRA_DPLANE_DELETE_FAILURE:
757 zsend_ipset_notify_owner(ipset, ZAPI_IPSET_FAIL_REMOVE);
758 break;
759 case ZEBRA_DPLANE_STATUS_NONE:
760 break;
761 }
762 }
763
764 /*
765 * Handle success or failure of ipset (un)install in the kernel.
766 */
767 void kernel_pbr_ipset_entry_add_del_status(
768 struct zebra_pbr_ipset_entry *ipset,
769 enum zebra_dplane_status res)
770 {
771 switch (res) {
772 case ZEBRA_DPLANE_INSTALL_SUCCESS:
773 zsend_ipset_entry_notify_owner(ipset,
774 ZAPI_IPSET_ENTRY_INSTALLED);
775 break;
776 case ZEBRA_DPLANE_INSTALL_FAILURE:
777 zsend_ipset_entry_notify_owner(ipset,
778 ZAPI_IPSET_ENTRY_FAIL_INSTALL);
779 break;
780 case ZEBRA_DPLANE_DELETE_SUCCESS:
781 zsend_ipset_entry_notify_owner(ipset,
782 ZAPI_IPSET_ENTRY_REMOVED);
783 break;
784 case ZEBRA_DPLANE_DELETE_FAILURE:
785 zsend_ipset_entry_notify_owner(ipset,
786 ZAPI_IPSET_ENTRY_FAIL_REMOVE);
787 break;
788 case ZEBRA_DPLANE_STATUS_NONE:
789 break;
790 }
791 }
792
793 /*
794 * Handle success or failure of ipset (un)install in the kernel.
795 */
796 void kernel_pbr_iptable_add_del_status(struct zebra_pbr_iptable *iptable,
797 enum zebra_dplane_status res)
798 {
799 switch (res) {
800 case ZEBRA_DPLANE_INSTALL_SUCCESS:
801 zsend_iptable_notify_owner(iptable, ZAPI_IPTABLE_INSTALLED);
802 break;
803 case ZEBRA_DPLANE_INSTALL_FAILURE:
804 zsend_iptable_notify_owner(iptable, ZAPI_IPTABLE_FAIL_INSTALL);
805 break;
806 case ZEBRA_DPLANE_DELETE_SUCCESS:
807 zsend_iptable_notify_owner(iptable,
808 ZAPI_IPTABLE_REMOVED);
809 break;
810 case ZEBRA_DPLANE_DELETE_FAILURE:
811 zsend_iptable_notify_owner(iptable,
812 ZAPI_IPTABLE_FAIL_REMOVE);
813 break;
814 case ZEBRA_DPLANE_STATUS_NONE:
815 break;
816 }
817 }
818
819 /*
820 * Handle rule delete notification from kernel.
821 */
822 int kernel_pbr_rule_del(struct zebra_pbr_rule *rule)
823 {
824 return 0;
825 }
826
827 struct zebra_pbr_ipset_entry_unique_display {
828 struct zebra_pbr_ipset *zpi;
829 struct vty *vty;
830 struct zebra_ns *zns;
831 };
832
833 struct zebra_pbr_env_display {
834 struct zebra_ns *zns;
835 struct vty *vty;
836 char *name;
837 };
838
839 static const char *zebra_pbr_prefix2str(union prefixconstptr pu,
840 char *str, int size)
841 {
842 const struct prefix *p = pu.p;
843 char buf[PREFIX2STR_BUFFER];
844
845 if (p->family == AF_INET && p->prefixlen == IPV4_MAX_PREFIXLEN) {
846 snprintf(str, size, "%s", inet_ntop(p->family, &p->u.prefix,
847 buf, PREFIX2STR_BUFFER));
848 return str;
849 }
850 return prefix2str(pu, str, size);
851 }
852
853 static void zebra_pbr_display_icmp(struct vty *vty,
854 struct zebra_pbr_ipset_entry *zpie)
855 {
856 char decoded_str[20];
857 uint16_t port;
858
859 /* range icmp type */
860 if (zpie->src_port_max || zpie->dst_port_max) {
861 vty_out(vty, ":icmp:[type <%d:%d>;code <%d:%d>",
862 zpie->src_port_min, zpie->src_port_max,
863 zpie->dst_port_min, zpie->dst_port_max);
864 } else {
865 port = ((zpie->src_port_min << 8) & 0xff00) +
866 (zpie->dst_port_min & 0xff);
867 memset(decoded_str, 0, sizeof(decoded_str));
868 sprintf(decoded_str, "%d/%d",
869 zpie->src_port_min,
870 zpie->dst_port_min);
871 vty_out(vty, ":icmp:%s",
872 lookup_msg(icmp_typecode_str,
873 port, decoded_str));
874 }
875 }
876
877 static void zebra_pbr_display_port(struct vty *vty, uint32_t filter_bm,
878 uint16_t port_min, uint16_t port_max,
879 uint8_t proto)
880 {
881 if (!(filter_bm & PBR_FILTER_PROTO)) {
882 if (port_max)
883 vty_out(vty, ":udp/tcp:%d-%d",
884 port_min, port_max);
885 else
886 vty_out(vty, ":udp/tcp:%d",
887 port_min);
888 } else {
889 if (port_max)
890 vty_out(vty, ":proto %d:%d-%d",
891 proto, port_min, port_max);
892 else
893 vty_out(vty, ":proto %d:%d",
894 proto, port_min);
895 }
896 }
897
898 static int zebra_pbr_show_ipset_entry_walkcb(struct hash_bucket *bucket,
899 void *arg)
900 {
901 struct zebra_pbr_ipset_entry_unique_display *unique =
902 (struct zebra_pbr_ipset_entry_unique_display *)arg;
903 struct zebra_pbr_ipset *zpi = unique->zpi;
904 struct vty *vty = unique->vty;
905 struct zebra_pbr_ipset_entry *zpie =
906 (struct zebra_pbr_ipset_entry *)bucket->data;
907 uint64_t pkts = 0, bytes = 0;
908 int ret = 0;
909
910 if (zpie->backpointer != zpi)
911 return HASHWALK_CONTINUE;
912
913 if ((zpi->type == IPSET_NET_NET) ||
914 (zpi->type == IPSET_NET_PORT_NET)) {
915 char buf[PREFIX_STRLEN];
916
917 zebra_pbr_prefix2str(&(zpie->src), buf, sizeof(buf));
918 vty_out(vty, "\tfrom %s", buf);
919 if (zpie->filter_bm & PBR_FILTER_SRC_PORT &&
920 zpie->proto != IPPROTO_ICMP)
921 zebra_pbr_display_port(vty, zpie->filter_bm,
922 zpie->src_port_min,
923 zpie->src_port_max,
924 zpie->proto);
925 vty_out(vty, " to ");
926 zebra_pbr_prefix2str(&(zpie->dst), buf, sizeof(buf));
927 vty_out(vty, "%s", buf);
928 if (zpie->filter_bm & PBR_FILTER_DST_PORT &&
929 zpie->proto != IPPROTO_ICMP)
930 zebra_pbr_display_port(vty, zpie->filter_bm,
931 zpie->dst_port_min,
932 zpie->dst_port_max,
933 zpie->proto);
934 if (zpie->proto == IPPROTO_ICMP)
935 zebra_pbr_display_icmp(vty, zpie);
936 } else if ((zpi->type == IPSET_NET) ||
937 (zpi->type == IPSET_NET_PORT)) {
938 char buf[PREFIX_STRLEN];
939
940 if (zpie->filter_bm & PBR_FILTER_SRC_IP) {
941 zebra_pbr_prefix2str(&(zpie->src), buf, sizeof(buf));
942 vty_out(vty, "\tfrom %s", buf);
943 }
944 if (zpie->filter_bm & PBR_FILTER_SRC_PORT &&
945 zpie->proto != IPPROTO_ICMP)
946 zebra_pbr_display_port(vty, zpie->filter_bm,
947 zpie->src_port_min,
948 zpie->src_port_max,
949 zpie->proto);
950 if (zpie->filter_bm & PBR_FILTER_DST_IP) {
951 zebra_pbr_prefix2str(&(zpie->dst), buf, sizeof(buf));
952 vty_out(vty, "\tto %s", buf);
953 }
954 if (zpie->filter_bm & PBR_FILTER_DST_PORT &&
955 zpie->proto != IPPROTO_ICMP)
956 zebra_pbr_display_port(vty, zpie->filter_bm,
957 zpie->dst_port_min,
958 zpie->dst_port_max,
959 zpie->proto);
960 if (zpie->proto == IPPROTO_ICMP)
961 zebra_pbr_display_icmp(vty, zpie);
962 }
963 vty_out(vty, " (%u)\n", zpie->unique);
964
965 ret = hook_call(zebra_pbr_ipset_entry_get_stat, zpie, &pkts,
966 &bytes);
967 if (ret && pkts > 0)
968 vty_out(vty, "\t pkts %" PRIu64 ", bytes %" PRIu64"\n",
969 pkts, bytes);
970 return HASHWALK_CONTINUE;
971 }
972
973 static int zebra_pbr_show_ipset_walkcb(struct hash_bucket *bucket, void *arg)
974 {
975 struct zebra_pbr_env_display *uniqueipset =
976 (struct zebra_pbr_env_display *)arg;
977 struct zebra_pbr_ipset *zpi = (struct zebra_pbr_ipset *)bucket->data;
978 struct zebra_pbr_ipset_entry_unique_display unique;
979 struct vty *vty = uniqueipset->vty;
980 struct zebra_ns *zns = uniqueipset->zns;
981
982 vty_out(vty, "IPset %s type %s\n", zpi->ipset_name,
983 zebra_pbr_ipset_type2str(zpi->type));
984 unique.vty = vty;
985 unique.zpi = zpi;
986 unique.zns = zns;
987 hash_walk(zrouter.ipset_entry_hash, zebra_pbr_show_ipset_entry_walkcb,
988 &unique);
989 vty_out(vty, "\n");
990 return HASHWALK_CONTINUE;
991 }
992
993 size_t zebra_pbr_tcpflags_snprintf(char *buffer, size_t len,
994 uint16_t tcp_val)
995 {
996 size_t len_written = 0;
997 static struct message nt = {0};
998 const struct message *pnt;
999 int incr = 0;
1000
1001 for (pnt = tcp_value_str;
1002 memcmp(pnt, &nt, sizeof(struct message)); pnt++)
1003 if (pnt->key & tcp_val) {
1004 len_written += snprintf(buffer + len_written,
1005 len - len_written,
1006 "%s%s", incr ?
1007 ",":"", pnt->str);
1008 incr++;
1009 }
1010 return len_written;
1011 }
1012
1013 /*
1014 */
1015 void zebra_pbr_show_ipset_list(struct vty *vty, char *ipsetname)
1016 {
1017 struct zebra_pbr_ipset *zpi;
1018 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1019 struct zebra_pbr_ipset_entry_unique_display unique;
1020 struct zebra_pbr_env_display uniqueipset;
1021
1022 if (ipsetname) {
1023 zpi = zebra_pbr_lookup_ipset_pername(ipsetname);
1024 if (!zpi) {
1025 vty_out(vty, "No IPset %s found\n", ipsetname);
1026 return;
1027 }
1028 vty_out(vty, "IPset %s type %s\n", ipsetname,
1029 zebra_pbr_ipset_type2str(zpi->type));
1030
1031 unique.vty = vty;
1032 unique.zpi = zpi;
1033 unique.zns = zns;
1034 hash_walk(zrouter.ipset_entry_hash,
1035 zebra_pbr_show_ipset_entry_walkcb, &unique);
1036 return;
1037 }
1038 uniqueipset.zns = zns;
1039 uniqueipset.vty = vty;
1040 uniqueipset.name = NULL;
1041 hash_walk(zrouter.ipset_hash, zebra_pbr_show_ipset_walkcb,
1042 &uniqueipset);
1043 }
1044
1045 struct pbr_rule_fwmark_lookup {
1046 struct zebra_pbr_rule *ptr;
1047 uint32_t fwmark;
1048 };
1049
1050 static int zebra_pbr_rule_lookup_fwmark_walkcb(struct hash_bucket *bucket,
1051 void *arg)
1052 {
1053 struct pbr_rule_fwmark_lookup *iprule =
1054 (struct pbr_rule_fwmark_lookup *)arg;
1055 struct zebra_pbr_rule *zpr = (struct zebra_pbr_rule *)bucket->data;
1056
1057 if (iprule->fwmark == zpr->rule.filter.fwmark) {
1058 iprule->ptr = zpr;
1059 return HASHWALK_ABORT;
1060 }
1061 return HASHWALK_CONTINUE;
1062 }
1063
1064 static void zebra_pbr_show_iptable_unit(struct zebra_pbr_iptable *iptable,
1065 struct vty *vty,
1066 struct zebra_ns *zns)
1067 {
1068 int ret;
1069 uint64_t pkts = 0, bytes = 0;
1070
1071 vty_out(vty, "IPtable %s action %s (%u)\n", iptable->ipset_name,
1072 iptable->action == ZEBRA_IPTABLES_DROP ? "drop" : "redirect",
1073 iptable->unique);
1074 if (iptable->type == IPSET_NET_PORT ||
1075 iptable->type == IPSET_NET_PORT_NET) {
1076 if (!(iptable->filter_bm & MATCH_ICMP_SET)) {
1077 if (iptable->filter_bm & PBR_FILTER_DST_PORT)
1078 vty_out(vty, "\t lookup dst port\n");
1079 else if (iptable->filter_bm & PBR_FILTER_SRC_PORT)
1080 vty_out(vty, "\t lookup src port\n");
1081 }
1082 }
1083 if (iptable->pkt_len_min || iptable->pkt_len_max) {
1084 if (!iptable->pkt_len_max)
1085 vty_out(vty, "\t pkt len %u\n",
1086 iptable->pkt_len_min);
1087 else
1088 vty_out(vty, "\t pkt len [%u;%u]\n",
1089 iptable->pkt_len_min,
1090 iptable->pkt_len_max);
1091 }
1092 if (iptable->tcp_flags || iptable->tcp_mask_flags) {
1093 char tcp_flag_str[64];
1094 char tcp_flag_mask_str[64];
1095
1096 zebra_pbr_tcpflags_snprintf(tcp_flag_str,
1097 sizeof(tcp_flag_str),
1098 iptable->tcp_flags);
1099 zebra_pbr_tcpflags_snprintf(tcp_flag_mask_str,
1100 sizeof(tcp_flag_mask_str),
1101 iptable->tcp_mask_flags);
1102 vty_out(vty, "\t tcpflags [%s/%s]\n",
1103 tcp_flag_str, tcp_flag_mask_str);
1104 }
1105 if (iptable->filter_bm & (MATCH_DSCP_SET | MATCH_DSCP_INVERSE_SET)) {
1106 vty_out(vty, "\t dscp %s %d\n",
1107 iptable->filter_bm & MATCH_DSCP_INVERSE_SET ?
1108 "not" : "", iptable->dscp_value);
1109 }
1110 if (iptable->fragment) {
1111 char val_str[10];
1112
1113 sprintf(val_str, "%d", iptable->fragment);
1114 vty_out(vty, "\t fragment%s %s\n",
1115 iptable->filter_bm & MATCH_FRAGMENT_INVERSE_SET ?
1116 " not" : "", lookup_msg(fragment_value_str,
1117 iptable->fragment, val_str));
1118 }
1119 if (iptable->protocol) {
1120 vty_out(vty, "\t protocol %d\n",
1121 iptable->protocol);
1122 }
1123 ret = hook_call(zebra_pbr_iptable_get_stat, iptable, &pkts,
1124 &bytes);
1125 if (ret && pkts > 0)
1126 vty_out(vty, "\t pkts %" PRIu64 ", bytes %" PRIu64"\n",
1127 pkts, bytes);
1128 if (iptable->action != ZEBRA_IPTABLES_DROP) {
1129 struct pbr_rule_fwmark_lookup prfl;
1130
1131 prfl.fwmark = iptable->fwmark;
1132 prfl.ptr = NULL;
1133 hash_walk(zrouter.rules_hash,
1134 &zebra_pbr_rule_lookup_fwmark_walkcb, &prfl);
1135 if (prfl.ptr) {
1136 struct zebra_pbr_rule *zpr = prfl.ptr;
1137
1138 vty_out(vty, "\t table %u, fwmark %u\n",
1139 zpr->rule.action.table,
1140 prfl.fwmark);
1141 }
1142 }
1143 }
1144
1145 static int zebra_pbr_show_iptable_walkcb(struct hash_bucket *bucket, void *arg)
1146 {
1147 struct zebra_pbr_iptable *iptable =
1148 (struct zebra_pbr_iptable *)bucket->data;
1149 struct zebra_pbr_env_display *env = (struct zebra_pbr_env_display *)arg;
1150 struct vty *vty = env->vty;
1151 struct zebra_ns *zns = env->zns;
1152 char *iptable_name = env->name;
1153
1154 if (!iptable_name)
1155 zebra_pbr_show_iptable_unit(iptable, vty, zns);
1156 else if (!strncmp(iptable_name,
1157 iptable->ipset_name,
1158 ZEBRA_IPSET_NAME_SIZE))
1159 zebra_pbr_show_iptable_unit(iptable, vty, zns);
1160 return HASHWALK_CONTINUE;
1161 }
1162
1163 void zebra_pbr_show_iptable(struct vty *vty, char *iptable_name)
1164 {
1165 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1166 struct zebra_pbr_env_display env;
1167
1168 env.vty = vty;
1169 env.zns = zns;
1170 env.name = iptable_name;
1171 hash_walk(zrouter.iptable_hash, zebra_pbr_show_iptable_walkcb, &env);
1172 }
1173
1174 void zebra_pbr_iptable_update_interfacelist(struct stream *s,
1175 struct zebra_pbr_iptable *zpi)
1176 {
1177 uint32_t i = 0, index;
1178 struct interface *ifp;
1179 char *name;
1180
1181 for (i = 0; i < zpi->nb_interface; i++) {
1182 STREAM_GETL(s, index);
1183 ifp = if_lookup_by_index(index, zpi->vrf_id);
1184 if (!ifp)
1185 continue;
1186 name = XSTRDUP(MTYPE_PBR_IPTABLE_IFNAME, ifp->name);
1187 listnode_add(zpi->interface_name_list, name);
1188 }
1189 stream_failure:
1190 return;
1191 }