]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_pbr.c
Merge pull request #4941 from ton31337/fix/do_not_include_nexthop_dash_dash
[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 for (ALL_LIST_ELEMENTS(iptable->interface_name_list,
349 node, nnode, name)) {
350 XFREE(MTYPE_PBR_IPTABLE_IFNAME, name);
351 list_delete_node(iptable->interface_name_list,
352 node);
353 }
354 XFREE(MTYPE_TMP, iptable);
355 }
356
357 uint32_t zebra_pbr_iptable_hash_key(const void *arg)
358 {
359 const struct zebra_pbr_iptable *iptable = arg;
360 uint32_t *pnt = (uint32_t *)&(iptable->ipset_name);
361 uint32_t key;
362
363 key = jhash2(pnt, ZEBRA_IPSET_NAME_HASH_SIZE,
364 0x63ab42de);
365 key = jhash_1word(iptable->fwmark, key);
366 key = jhash_1word(iptable->pkt_len_min, key);
367 key = jhash_1word(iptable->pkt_len_max, key);
368 key = jhash_1word(iptable->tcp_flags, key);
369 key = jhash_1word(iptable->tcp_mask_flags, key);
370 key = jhash_1word(iptable->dscp_value, key);
371 key = jhash_1word(iptable->protocol, key);
372 key = jhash_1word(iptable->fragment, key);
373 key = jhash_1word(iptable->vrf_id, key);
374
375 return jhash_3words(iptable->filter_bm, iptable->type,
376 iptable->unique, key);
377 }
378
379 bool zebra_pbr_iptable_hash_equal(const void *arg1, const void *arg2)
380 {
381 const struct zebra_pbr_iptable *r1, *r2;
382
383 r1 = (const struct zebra_pbr_iptable *)arg1;
384 r2 = (const struct zebra_pbr_iptable *)arg2;
385
386 if (r1->vrf_id != r2->vrf_id)
387 return false;
388 if (r1->type != r2->type)
389 return false;
390 if (r1->unique != r2->unique)
391 return false;
392 if (r1->filter_bm != r2->filter_bm)
393 return false;
394 if (r1->fwmark != r2->fwmark)
395 return false;
396 if (r1->action != r2->action)
397 return false;
398 if (strncmp(r1->ipset_name, r2->ipset_name,
399 ZEBRA_IPSET_NAME_SIZE))
400 return false;
401 if (r1->pkt_len_min != r2->pkt_len_min)
402 return false;
403 if (r1->pkt_len_max != r2->pkt_len_max)
404 return false;
405 if (r1->tcp_flags != r2->tcp_flags)
406 return false;
407 if (r1->tcp_mask_flags != r2->tcp_mask_flags)
408 return false;
409 if (r1->dscp_value != r2->dscp_value)
410 return false;
411 if (r1->fragment != r2->fragment)
412 return false;
413 if (r1->protocol != r2->protocol)
414 return false;
415 return true;
416 }
417
418 static void *pbr_rule_alloc_intern(void *arg)
419 {
420 struct zebra_pbr_rule *zpr;
421 struct zebra_pbr_rule *new;
422
423 zpr = (struct zebra_pbr_rule *)arg;
424
425 new = XCALLOC(MTYPE_TMP, sizeof(*new));
426
427 memcpy(new, zpr, sizeof(*zpr));
428
429 return new;
430 }
431
432 void zebra_pbr_add_rule(struct zebra_pbr_rule *rule)
433 {
434 struct zebra_pbr_rule *unique =
435 pbr_rule_lookup_unique(rule);
436
437 (void)hash_get(zrouter.rules_hash, rule, pbr_rule_alloc_intern);
438 (void)kernel_add_pbr_rule(rule);
439 /*
440 * Rule Replace semantics, if we have an old, install the
441 * new rule, look above, and then delete the old
442 */
443 if (unique)
444 zebra_pbr_del_rule(unique);
445 }
446
447 void zebra_pbr_del_rule(struct zebra_pbr_rule *rule)
448 {
449 struct zebra_pbr_rule *lookup;
450
451 lookup = hash_lookup(zrouter.rules_hash, rule);
452 (void)kernel_del_pbr_rule(rule);
453
454 if (lookup) {
455 hash_release(zrouter.rules_hash, lookup);
456 XFREE(MTYPE_TMP, lookup);
457 } else
458 zlog_debug("%s: Rule being deleted we know nothing about",
459 __PRETTY_FUNCTION__);
460 }
461
462 static void zebra_pbr_cleanup_rules(struct hash_bucket *b, void *data)
463 {
464 struct zebra_pbr_rule *rule = b->data;
465 int *sock = data;
466
467 if (rule->sock == *sock) {
468 (void)kernel_del_pbr_rule(rule);
469 if (hash_release(zrouter.rules_hash, rule))
470 XFREE(MTYPE_TMP, rule);
471 else
472 zlog_debug(
473 "%s: Rule seq: %u is being cleaned but we can't find it in our tables",
474 __func__, rule->rule.seq);
475 }
476 }
477
478 static void zebra_pbr_cleanup_ipset(struct hash_bucket *b, void *data)
479 {
480 struct zebra_pbr_ipset *ipset = b->data;
481 int *sock = data;
482
483 if (ipset->sock == *sock) {
484 hook_call(zebra_pbr_ipset_update, 0, ipset);
485 hash_release(zrouter.ipset_hash, ipset);
486 }
487 }
488
489 static void zebra_pbr_cleanup_ipset_entry(struct hash_bucket *b, void *data)
490 {
491 struct zebra_pbr_ipset_entry *ipset = b->data;
492 int *sock = data;
493
494 if (ipset->sock == *sock) {
495 hook_call(zebra_pbr_ipset_entry_update, 0, ipset);
496 hash_release(zrouter.ipset_entry_hash, ipset);
497 }
498 }
499
500 static void zebra_pbr_cleanup_iptable(struct hash_bucket *b, void *data)
501 {
502 struct zebra_pbr_iptable *iptable = b->data;
503 int *sock = data;
504
505 if (iptable->sock == *sock) {
506 hook_call(zebra_pbr_iptable_update, 0, iptable);
507 hash_release(zrouter.iptable_hash, iptable);
508 }
509 }
510
511 static int zebra_pbr_client_close_cleanup(struct zserv *client)
512 {
513 int sock = client->sock;
514
515 if (!sock)
516 return 0;
517 hash_iterate(zrouter.rules_hash, zebra_pbr_cleanup_rules, &sock);
518 hash_iterate(zrouter.iptable_hash, zebra_pbr_cleanup_iptable, &sock);
519 hash_iterate(zrouter.ipset_entry_hash, zebra_pbr_cleanup_ipset_entry,
520 &sock);
521 hash_iterate(zrouter.ipset_hash, zebra_pbr_cleanup_ipset, &sock);
522 return 1;
523 }
524
525 void zebra_pbr_init(void)
526 {
527 hook_register(zserv_client_close, zebra_pbr_client_close_cleanup);
528 }
529
530 static void *pbr_ipset_alloc_intern(void *arg)
531 {
532 struct zebra_pbr_ipset *zpi;
533 struct zebra_pbr_ipset *new;
534
535 zpi = (struct zebra_pbr_ipset *)arg;
536
537 new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_ipset));
538
539 memcpy(new, zpi, sizeof(*zpi));
540
541 return new;
542 }
543
544 void zebra_pbr_create_ipset(struct zebra_pbr_ipset *ipset)
545 {
546 int ret;
547
548 (void)hash_get(zrouter.ipset_hash, ipset, pbr_ipset_alloc_intern);
549 ret = hook_call(zebra_pbr_ipset_update, 1, ipset);
550 kernel_pbr_ipset_add_del_status(ipset,
551 ret ? ZEBRA_DPLANE_INSTALL_SUCCESS
552 : ZEBRA_DPLANE_INSTALL_FAILURE);
553 }
554
555 void zebra_pbr_destroy_ipset(struct zebra_pbr_ipset *ipset)
556 {
557 struct zebra_pbr_ipset *lookup;
558
559 lookup = hash_lookup(zrouter.ipset_hash, ipset);
560 hook_call(zebra_pbr_ipset_update, 0, ipset);
561 if (lookup) {
562 hash_release(zrouter.ipset_hash, lookup);
563 XFREE(MTYPE_TMP, lookup);
564 } else
565 zlog_debug(
566 "%s: IPSet Entry being deleted we know nothing about",
567 __PRETTY_FUNCTION__);
568 }
569
570 struct pbr_ipset_name_lookup {
571 struct zebra_pbr_ipset *ipset;
572 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
573 };
574
575 const char *zebra_pbr_ipset_type2str(uint32_t type)
576 {
577 return lookup_msg(ipset_type_msg, type,
578 "Unrecognized IPset Type");
579 }
580
581 static int zebra_pbr_ipset_pername_walkcb(struct hash_bucket *bucket, void *arg)
582 {
583 struct pbr_ipset_name_lookup *pinl =
584 (struct pbr_ipset_name_lookup *)arg;
585 struct zebra_pbr_ipset *zpi = (struct zebra_pbr_ipset *)bucket->data;
586
587 if (!strncmp(pinl->ipset_name, zpi->ipset_name,
588 ZEBRA_IPSET_NAME_SIZE)) {
589 pinl->ipset = zpi;
590 return HASHWALK_ABORT;
591 }
592 return HASHWALK_CONTINUE;
593 }
594
595 struct zebra_pbr_ipset *zebra_pbr_lookup_ipset_pername(char *ipsetname)
596 {
597 struct pbr_ipset_name_lookup pinl;
598 struct pbr_ipset_name_lookup *ptr = &pinl;
599
600 if (!ipsetname)
601 return NULL;
602 memset(ptr, 0, sizeof(struct pbr_ipset_name_lookup));
603 snprintf((char *)ptr->ipset_name, ZEBRA_IPSET_NAME_SIZE, "%s",
604 ipsetname);
605 hash_walk(zrouter.ipset_hash, zebra_pbr_ipset_pername_walkcb, ptr);
606 return ptr->ipset;
607 }
608
609 static void *pbr_ipset_entry_alloc_intern(void *arg)
610 {
611 struct zebra_pbr_ipset_entry *zpi;
612 struct zebra_pbr_ipset_entry *new;
613
614 zpi = (struct zebra_pbr_ipset_entry *)arg;
615
616 new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_ipset_entry));
617
618 memcpy(new, zpi, sizeof(*zpi));
619
620 return new;
621 }
622
623 void zebra_pbr_add_ipset_entry(struct zebra_pbr_ipset_entry *ipset)
624 {
625 int ret;
626
627 (void)hash_get(zrouter.ipset_entry_hash, ipset,
628 pbr_ipset_entry_alloc_intern);
629 ret = hook_call(zebra_pbr_ipset_entry_update, 1, ipset);
630 kernel_pbr_ipset_entry_add_del_status(ipset,
631 ret ? ZEBRA_DPLANE_INSTALL_SUCCESS
632 : ZEBRA_DPLANE_INSTALL_FAILURE);
633 }
634
635 void zebra_pbr_del_ipset_entry(struct zebra_pbr_ipset_entry *ipset)
636 {
637 struct zebra_pbr_ipset_entry *lookup;
638
639 lookup = hash_lookup(zrouter.ipset_entry_hash, ipset);
640 hook_call(zebra_pbr_ipset_entry_update, 0, ipset);
641 if (lookup) {
642 hash_release(zrouter.ipset_entry_hash, lookup);
643 XFREE(MTYPE_TMP, lookup);
644 } else
645 zlog_debug("%s: IPSet being deleted we know nothing about",
646 __PRETTY_FUNCTION__);
647 }
648
649 static void *pbr_iptable_alloc_intern(void *arg)
650 {
651 struct zebra_pbr_iptable *zpi;
652 struct zebra_pbr_iptable *new;
653
654 zpi = (struct zebra_pbr_iptable *)arg;
655
656 new = XCALLOC(MTYPE_TMP, sizeof(struct zebra_pbr_iptable));
657
658 memcpy(new, zpi, sizeof(*zpi));
659
660 return new;
661 }
662
663 void zebra_pbr_add_iptable(struct zebra_pbr_iptable *iptable)
664 {
665 int ret;
666
667 (void)hash_get(zrouter.iptable_hash, iptable, pbr_iptable_alloc_intern);
668 ret = hook_call(zebra_pbr_iptable_update, 1, iptable);
669 kernel_pbr_iptable_add_del_status(iptable,
670 ret ? ZEBRA_DPLANE_INSTALL_SUCCESS
671 : ZEBRA_DPLANE_INSTALL_FAILURE);
672 }
673
674 void zebra_pbr_del_iptable(struct zebra_pbr_iptable *iptable)
675 {
676 struct zebra_pbr_iptable *lookup;
677
678 lookup = hash_lookup(zrouter.iptable_hash, iptable);
679 hook_call(zebra_pbr_iptable_update, 0, iptable);
680 if (lookup) {
681 struct listnode *node, *nnode;
682 char *name;
683
684 hash_release(zrouter.iptable_hash, lookup);
685 for (ALL_LIST_ELEMENTS(iptable->interface_name_list,
686 node, nnode, name)) {
687 XFREE(MTYPE_PBR_IPTABLE_IFNAME, name);
688 list_delete_node(iptable->interface_name_list,
689 node);
690 }
691 XFREE(MTYPE_TMP, lookup);
692 } else
693 zlog_debug("%s: IPTable being deleted we know nothing about",
694 __PRETTY_FUNCTION__);
695 }
696
697 /*
698 * Handle success or failure of rule (un)install in the kernel.
699 */
700 void kernel_pbr_rule_add_del_status(struct zebra_pbr_rule *rule,
701 enum zebra_dplane_status res)
702 {
703 switch (res) {
704 case ZEBRA_DPLANE_INSTALL_SUCCESS:
705 zsend_rule_notify_owner(rule, ZAPI_RULE_INSTALLED);
706 break;
707 case ZEBRA_DPLANE_INSTALL_FAILURE:
708 zsend_rule_notify_owner(rule, ZAPI_RULE_FAIL_INSTALL);
709 break;
710 case ZEBRA_DPLANE_DELETE_SUCCESS:
711 zsend_rule_notify_owner(rule, ZAPI_RULE_REMOVED);
712 break;
713 case ZEBRA_DPLANE_DELETE_FAILURE:
714 zsend_rule_notify_owner(rule, ZAPI_RULE_FAIL_REMOVE);
715 break;
716 case ZEBRA_DPLANE_STATUS_NONE:
717 break;
718 }
719 }
720
721 /*
722 * Handle success or failure of ipset (un)install in the kernel.
723 */
724 void kernel_pbr_ipset_add_del_status(struct zebra_pbr_ipset *ipset,
725 enum zebra_dplane_status res)
726 {
727 switch (res) {
728 case ZEBRA_DPLANE_INSTALL_SUCCESS:
729 zsend_ipset_notify_owner(ipset, ZAPI_IPSET_INSTALLED);
730 break;
731 case ZEBRA_DPLANE_INSTALL_FAILURE:
732 zsend_ipset_notify_owner(ipset, ZAPI_IPSET_FAIL_INSTALL);
733 break;
734 case ZEBRA_DPLANE_DELETE_SUCCESS:
735 zsend_ipset_notify_owner(ipset, ZAPI_IPSET_REMOVED);
736 break;
737 case ZEBRA_DPLANE_DELETE_FAILURE:
738 zsend_ipset_notify_owner(ipset, ZAPI_IPSET_FAIL_REMOVE);
739 break;
740 case ZEBRA_DPLANE_STATUS_NONE:
741 break;
742 }
743 }
744
745 /*
746 * Handle success or failure of ipset (un)install in the kernel.
747 */
748 void kernel_pbr_ipset_entry_add_del_status(
749 struct zebra_pbr_ipset_entry *ipset,
750 enum zebra_dplane_status res)
751 {
752 switch (res) {
753 case ZEBRA_DPLANE_INSTALL_SUCCESS:
754 zsend_ipset_entry_notify_owner(ipset,
755 ZAPI_IPSET_ENTRY_INSTALLED);
756 break;
757 case ZEBRA_DPLANE_INSTALL_FAILURE:
758 zsend_ipset_entry_notify_owner(ipset,
759 ZAPI_IPSET_ENTRY_FAIL_INSTALL);
760 break;
761 case ZEBRA_DPLANE_DELETE_SUCCESS:
762 zsend_ipset_entry_notify_owner(ipset,
763 ZAPI_IPSET_ENTRY_REMOVED);
764 break;
765 case ZEBRA_DPLANE_DELETE_FAILURE:
766 zsend_ipset_entry_notify_owner(ipset,
767 ZAPI_IPSET_ENTRY_FAIL_REMOVE);
768 break;
769 case ZEBRA_DPLANE_STATUS_NONE:
770 break;
771 }
772 }
773
774 /*
775 * Handle success or failure of ipset (un)install in the kernel.
776 */
777 void kernel_pbr_iptable_add_del_status(struct zebra_pbr_iptable *iptable,
778 enum zebra_dplane_status res)
779 {
780 switch (res) {
781 case ZEBRA_DPLANE_INSTALL_SUCCESS:
782 zsend_iptable_notify_owner(iptable, ZAPI_IPTABLE_INSTALLED);
783 break;
784 case ZEBRA_DPLANE_INSTALL_FAILURE:
785 zsend_iptable_notify_owner(iptable, ZAPI_IPTABLE_FAIL_INSTALL);
786 break;
787 case ZEBRA_DPLANE_DELETE_SUCCESS:
788 zsend_iptable_notify_owner(iptable,
789 ZAPI_IPTABLE_REMOVED);
790 break;
791 case ZEBRA_DPLANE_DELETE_FAILURE:
792 zsend_iptable_notify_owner(iptable,
793 ZAPI_IPTABLE_FAIL_REMOVE);
794 break;
795 case ZEBRA_DPLANE_STATUS_NONE:
796 break;
797 }
798 }
799
800 /*
801 * Handle rule delete notification from kernel.
802 */
803 int kernel_pbr_rule_del(struct zebra_pbr_rule *rule)
804 {
805 return 0;
806 }
807
808 struct zebra_pbr_ipset_entry_unique_display {
809 struct zebra_pbr_ipset *zpi;
810 struct vty *vty;
811 struct zebra_ns *zns;
812 };
813
814 struct zebra_pbr_env_display {
815 struct zebra_ns *zns;
816 struct vty *vty;
817 char *name;
818 };
819
820 static const char *zebra_pbr_prefix2str(union prefixconstptr pu,
821 char *str, int size)
822 {
823 const struct prefix *p = pu.p;
824 char buf[PREFIX2STR_BUFFER];
825
826 if (p->family == AF_INET && p->prefixlen == IPV4_MAX_PREFIXLEN) {
827 snprintf(str, size, "%s", inet_ntop(p->family, &p->u.prefix,
828 buf, PREFIX2STR_BUFFER));
829 return str;
830 }
831 return prefix2str(pu, str, size);
832 }
833
834 static void zebra_pbr_display_icmp(struct vty *vty,
835 struct zebra_pbr_ipset_entry *zpie)
836 {
837 char decoded_str[20];
838 uint16_t port;
839
840 /* range icmp type */
841 if (zpie->src_port_max || zpie->dst_port_max) {
842 vty_out(vty, ":icmp:[type <%d:%d>;code <%d:%d>",
843 zpie->src_port_min, zpie->src_port_max,
844 zpie->dst_port_min, zpie->dst_port_max);
845 } else {
846 port = ((zpie->src_port_min << 8) & 0xff00) +
847 (zpie->dst_port_min & 0xff);
848 memset(decoded_str, 0, sizeof(decoded_str));
849 sprintf(decoded_str, "%d/%d",
850 zpie->src_port_min,
851 zpie->dst_port_min);
852 vty_out(vty, ":icmp:%s",
853 lookup_msg(icmp_typecode_str,
854 port, decoded_str));
855 }
856 }
857
858 static void zebra_pbr_display_port(struct vty *vty, uint32_t filter_bm,
859 uint16_t port_min, uint16_t port_max,
860 uint8_t proto)
861 {
862 if (!(filter_bm & PBR_FILTER_PROTO)) {
863 if (port_max)
864 vty_out(vty, ":udp/tcp:%d-%d",
865 port_min, port_max);
866 else
867 vty_out(vty, ":udp/tcp:%d",
868 port_min);
869 } else {
870 if (port_max)
871 vty_out(vty, ":proto %d:%d-%d",
872 proto, port_min, port_max);
873 else
874 vty_out(vty, ":proto %d:%d",
875 proto, port_min);
876 }
877 }
878
879 static int zebra_pbr_show_ipset_entry_walkcb(struct hash_bucket *bucket,
880 void *arg)
881 {
882 struct zebra_pbr_ipset_entry_unique_display *unique =
883 (struct zebra_pbr_ipset_entry_unique_display *)arg;
884 struct zebra_pbr_ipset *zpi = unique->zpi;
885 struct vty *vty = unique->vty;
886 struct zebra_pbr_ipset_entry *zpie =
887 (struct zebra_pbr_ipset_entry *)bucket->data;
888 uint64_t pkts = 0, bytes = 0;
889 int ret = 0;
890
891 if (zpie->backpointer != zpi)
892 return HASHWALK_CONTINUE;
893
894 if ((zpi->type == IPSET_NET_NET) ||
895 (zpi->type == IPSET_NET_PORT_NET)) {
896 char buf[PREFIX_STRLEN];
897
898 zebra_pbr_prefix2str(&(zpie->src), buf, sizeof(buf));
899 vty_out(vty, "\tfrom %s", buf);
900 if (zpie->filter_bm & PBR_FILTER_SRC_PORT &&
901 zpie->proto != IPPROTO_ICMP)
902 zebra_pbr_display_port(vty, zpie->filter_bm,
903 zpie->src_port_min,
904 zpie->src_port_max,
905 zpie->proto);
906 vty_out(vty, " to ");
907 zebra_pbr_prefix2str(&(zpie->dst), buf, sizeof(buf));
908 vty_out(vty, "%s", buf);
909 if (zpie->filter_bm & PBR_FILTER_DST_PORT &&
910 zpie->proto != IPPROTO_ICMP)
911 zebra_pbr_display_port(vty, zpie->filter_bm,
912 zpie->dst_port_min,
913 zpie->dst_port_max,
914 zpie->proto);
915 if (zpie->proto == IPPROTO_ICMP)
916 zebra_pbr_display_icmp(vty, zpie);
917 } else if ((zpi->type == IPSET_NET) ||
918 (zpi->type == IPSET_NET_PORT)) {
919 char buf[PREFIX_STRLEN];
920
921 if (zpie->filter_bm & PBR_FILTER_SRC_IP) {
922 zebra_pbr_prefix2str(&(zpie->src), buf, sizeof(buf));
923 vty_out(vty, "\tfrom %s", buf);
924 }
925 if (zpie->filter_bm & PBR_FILTER_SRC_PORT &&
926 zpie->proto != IPPROTO_ICMP)
927 zebra_pbr_display_port(vty, zpie->filter_bm,
928 zpie->src_port_min,
929 zpie->src_port_max,
930 zpie->proto);
931 if (zpie->filter_bm & PBR_FILTER_DST_IP) {
932 zebra_pbr_prefix2str(&(zpie->dst), buf, sizeof(buf));
933 vty_out(vty, "\tto %s", buf);
934 }
935 if (zpie->filter_bm & PBR_FILTER_DST_PORT &&
936 zpie->proto != IPPROTO_ICMP)
937 zebra_pbr_display_port(vty, zpie->filter_bm,
938 zpie->dst_port_min,
939 zpie->dst_port_max,
940 zpie->proto);
941 if (zpie->proto == IPPROTO_ICMP)
942 zebra_pbr_display_icmp(vty, zpie);
943 }
944 vty_out(vty, " (%u)\n", zpie->unique);
945
946 ret = hook_call(zebra_pbr_ipset_entry_get_stat, zpie, &pkts,
947 &bytes);
948 if (ret && pkts > 0)
949 vty_out(vty, "\t pkts %" PRIu64 ", bytes %" PRIu64"\n",
950 pkts, bytes);
951 return HASHWALK_CONTINUE;
952 }
953
954 static int zebra_pbr_show_ipset_walkcb(struct hash_bucket *bucket, void *arg)
955 {
956 struct zebra_pbr_env_display *uniqueipset =
957 (struct zebra_pbr_env_display *)arg;
958 struct zebra_pbr_ipset *zpi = (struct zebra_pbr_ipset *)bucket->data;
959 struct zebra_pbr_ipset_entry_unique_display unique;
960 struct vty *vty = uniqueipset->vty;
961 struct zebra_ns *zns = uniqueipset->zns;
962
963 vty_out(vty, "IPset %s type %s\n", zpi->ipset_name,
964 zebra_pbr_ipset_type2str(zpi->type));
965 unique.vty = vty;
966 unique.zpi = zpi;
967 unique.zns = zns;
968 hash_walk(zrouter.ipset_entry_hash, zebra_pbr_show_ipset_entry_walkcb,
969 &unique);
970 vty_out(vty, "\n");
971 return HASHWALK_CONTINUE;
972 }
973
974 size_t zebra_pbr_tcpflags_snprintf(char *buffer, size_t len,
975 uint16_t tcp_val)
976 {
977 size_t len_written = 0;
978 static struct message nt = {0};
979 const struct message *pnt;
980 int incr = 0;
981
982 for (pnt = tcp_value_str;
983 memcmp(pnt, &nt, sizeof(struct message)); pnt++)
984 if (pnt->key & tcp_val) {
985 len_written += snprintf(buffer + len_written,
986 len - len_written,
987 "%s%s", incr ?
988 ",":"", pnt->str);
989 incr++;
990 }
991 return len_written;
992 }
993
994 /*
995 */
996 void zebra_pbr_show_ipset_list(struct vty *vty, char *ipsetname)
997 {
998 struct zebra_pbr_ipset *zpi;
999 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1000 struct zebra_pbr_ipset_entry_unique_display unique;
1001 struct zebra_pbr_env_display uniqueipset;
1002
1003 if (ipsetname) {
1004 zpi = zebra_pbr_lookup_ipset_pername(ipsetname);
1005 if (!zpi) {
1006 vty_out(vty, "No IPset %s found\n", ipsetname);
1007 return;
1008 }
1009 vty_out(vty, "IPset %s type %s\n", ipsetname,
1010 zebra_pbr_ipset_type2str(zpi->type));
1011
1012 unique.vty = vty;
1013 unique.zpi = zpi;
1014 unique.zns = zns;
1015 hash_walk(zrouter.ipset_entry_hash,
1016 zebra_pbr_show_ipset_entry_walkcb, &unique);
1017 return;
1018 }
1019 uniqueipset.zns = zns;
1020 uniqueipset.vty = vty;
1021 uniqueipset.name = NULL;
1022 hash_walk(zrouter.ipset_hash, zebra_pbr_show_ipset_walkcb,
1023 &uniqueipset);
1024 }
1025
1026 struct pbr_rule_fwmark_lookup {
1027 struct zebra_pbr_rule *ptr;
1028 uint32_t fwmark;
1029 };
1030
1031 static int zebra_pbr_rule_lookup_fwmark_walkcb(struct hash_bucket *bucket,
1032 void *arg)
1033 {
1034 struct pbr_rule_fwmark_lookup *iprule =
1035 (struct pbr_rule_fwmark_lookup *)arg;
1036 struct zebra_pbr_rule *zpr = (struct zebra_pbr_rule *)bucket->data;
1037
1038 if (iprule->fwmark == zpr->rule.filter.fwmark) {
1039 iprule->ptr = zpr;
1040 return HASHWALK_ABORT;
1041 }
1042 return HASHWALK_CONTINUE;
1043 }
1044
1045 static void zebra_pbr_show_iptable_unit(struct zebra_pbr_iptable *iptable,
1046 struct vty *vty,
1047 struct zebra_ns *zns)
1048 {
1049 int ret;
1050 uint64_t pkts = 0, bytes = 0;
1051
1052 vty_out(vty, "IPtable %s action %s (%u)\n", iptable->ipset_name,
1053 iptable->action == ZEBRA_IPTABLES_DROP ? "drop" : "redirect",
1054 iptable->unique);
1055 if (iptable->type == IPSET_NET_PORT ||
1056 iptable->type == IPSET_NET_PORT_NET) {
1057 if (!(iptable->filter_bm & MATCH_ICMP_SET)) {
1058 if (iptable->filter_bm & PBR_FILTER_DST_PORT)
1059 vty_out(vty, "\t lookup dst port\n");
1060 else if (iptable->filter_bm & PBR_FILTER_SRC_PORT)
1061 vty_out(vty, "\t lookup src port\n");
1062 }
1063 }
1064 if (iptable->pkt_len_min || iptable->pkt_len_max) {
1065 if (!iptable->pkt_len_max)
1066 vty_out(vty, "\t pkt len %u\n",
1067 iptable->pkt_len_min);
1068 else
1069 vty_out(vty, "\t pkt len [%u;%u]\n",
1070 iptable->pkt_len_min,
1071 iptable->pkt_len_max);
1072 }
1073 if (iptable->tcp_flags || iptable->tcp_mask_flags) {
1074 char tcp_flag_str[64];
1075 char tcp_flag_mask_str[64];
1076
1077 zebra_pbr_tcpflags_snprintf(tcp_flag_str,
1078 sizeof(tcp_flag_str),
1079 iptable->tcp_flags);
1080 zebra_pbr_tcpflags_snprintf(tcp_flag_mask_str,
1081 sizeof(tcp_flag_mask_str),
1082 iptable->tcp_mask_flags);
1083 vty_out(vty, "\t tcpflags [%s/%s]\n",
1084 tcp_flag_str, tcp_flag_mask_str);
1085 }
1086 if (iptable->filter_bm & (MATCH_DSCP_SET | MATCH_DSCP_INVERSE_SET)) {
1087 vty_out(vty, "\t dscp %s %d\n",
1088 iptable->filter_bm & MATCH_DSCP_INVERSE_SET ?
1089 "not" : "", iptable->dscp_value);
1090 }
1091 if (iptable->fragment) {
1092 char val_str[10];
1093
1094 sprintf(val_str, "%d", iptable->fragment);
1095 vty_out(vty, "\t fragment%s %s\n",
1096 iptable->filter_bm & MATCH_FRAGMENT_INVERSE_SET ?
1097 " not" : "", lookup_msg(fragment_value_str,
1098 iptable->fragment, val_str));
1099 }
1100 if (iptable->protocol) {
1101 vty_out(vty, "\t protocol %d\n",
1102 iptable->protocol);
1103 }
1104 ret = hook_call(zebra_pbr_iptable_get_stat, iptable, &pkts,
1105 &bytes);
1106 if (ret && pkts > 0)
1107 vty_out(vty, "\t pkts %" PRIu64 ", bytes %" PRIu64"\n",
1108 pkts, bytes);
1109 if (iptable->action != ZEBRA_IPTABLES_DROP) {
1110 struct pbr_rule_fwmark_lookup prfl;
1111
1112 prfl.fwmark = iptable->fwmark;
1113 prfl.ptr = NULL;
1114 hash_walk(zrouter.rules_hash,
1115 &zebra_pbr_rule_lookup_fwmark_walkcb, &prfl);
1116 if (prfl.ptr) {
1117 struct zebra_pbr_rule *zpr = prfl.ptr;
1118
1119 vty_out(vty, "\t table %u, fwmark %u\n",
1120 zpr->rule.action.table,
1121 prfl.fwmark);
1122 }
1123 }
1124 }
1125
1126 static int zebra_pbr_show_iptable_walkcb(struct hash_bucket *bucket, void *arg)
1127 {
1128 struct zebra_pbr_iptable *iptable =
1129 (struct zebra_pbr_iptable *)bucket->data;
1130 struct zebra_pbr_env_display *env = (struct zebra_pbr_env_display *)arg;
1131 struct vty *vty = env->vty;
1132 struct zebra_ns *zns = env->zns;
1133 char *iptable_name = env->name;
1134
1135 if (!iptable_name)
1136 zebra_pbr_show_iptable_unit(iptable, vty, zns);
1137 else if (!strncmp(iptable_name,
1138 iptable->ipset_name,
1139 ZEBRA_IPSET_NAME_SIZE))
1140 zebra_pbr_show_iptable_unit(iptable, vty, zns);
1141 return HASHWALK_CONTINUE;
1142 }
1143
1144 void zebra_pbr_show_iptable(struct vty *vty, char *iptable_name)
1145 {
1146 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1147 struct zebra_pbr_env_display env;
1148
1149 env.vty = vty;
1150 env.zns = zns;
1151 env.name = iptable_name;
1152 hash_walk(zrouter.iptable_hash, zebra_pbr_show_iptable_walkcb, &env);
1153 }
1154
1155 void zebra_pbr_iptable_update_interfacelist(struct stream *s,
1156 struct zebra_pbr_iptable *zpi)
1157 {
1158 uint32_t i = 0, index;
1159 struct interface *ifp;
1160 char *name;
1161
1162 for (i = 0; i < zpi->nb_interface; i++) {
1163 STREAM_GETL(s, index);
1164 ifp = if_lookup_by_index(index, zpi->vrf_id);
1165 if (!ifp)
1166 continue;
1167 name = XSTRDUP(MTYPE_PBR_IPTABLE_IFNAME, ifp->name);
1168 listnode_add(zpi->interface_name_list, name);
1169 }
1170 stream_failure:
1171 return;
1172 }