]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_pbr.c
zebra: Make the ifp part of the rule structure
[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
27 #include "zebra/zebra_pbr.h"
28 #include "zebra/rt.h"
29
30 /* definitions */
31
32 /* static function declarations */
33
34 /* Private functions */
35
36 /* Public functions */
37 void zebra_pbr_rules_free(void *arg)
38 {
39 struct zebra_pbr_rule *rule;
40
41 rule = (struct zebra_pbr_rule *)arg;
42
43 kernel_del_pbr_rule(rule);
44 XFREE(MTYPE_TMP, rule);
45 }
46
47 uint32_t zebra_pbr_rules_hash_key(void *arg)
48 {
49 struct zebra_pbr_rule *rule;
50 uint32_t key;
51
52 rule = (struct zebra_pbr_rule *)arg;
53 key = jhash_3words(rule->seq, rule->priority, rule->action.table,
54 prefix_hash_key(&rule->filter.src_ip));
55 if (rule->ifp)
56 key = jhash_1word(rule->ifp->ifindex, key);
57 else
58 key = jhash_1word(0, key);
59
60 return jhash_3words(rule->filter.src_port, rule->filter.dst_port,
61 prefix_hash_key(&rule->filter.dst_ip), key);
62 }
63
64 int zebra_pbr_rules_hash_equal(const void *arg1, const void *arg2)
65 {
66 const struct zebra_pbr_rule *r1, *r2;
67
68 r1 = (const struct zebra_pbr_rule *)arg1;
69 r2 = (const struct zebra_pbr_rule *)arg2;
70
71 if (r1->seq != r2->seq)
72 return 0;
73
74 if (r1->priority != r2->priority)
75 return 0;
76
77 if (r1->action.table != r2->action.table)
78 return 0;
79
80 if (r1->filter.src_port != r2->filter.src_port)
81 return 0;
82
83 if (r1->filter.dst_port != r2->filter.dst_port)
84 return 0;
85
86 if (!prefix_same(&r1->filter.src_ip, &r2->filter.src_ip))
87 return 0;
88
89 if (!prefix_same(&r1->filter.dst_ip, &r2->filter.dst_ip))
90 return 0;
91
92 if (r1->ifp != r2->ifp)
93 return 0;
94
95 return 1;
96 }
97
98 static void *pbr_rule_alloc_intern(void *arg)
99 {
100 struct zebra_pbr_rule *zpr;
101 struct zebra_pbr_rule *new;
102
103 zpr = (struct zebra_pbr_rule *)arg;
104
105 new = XCALLOC(MTYPE_TMP, sizeof(*new));
106
107 memcpy(new, zpr, sizeof(*zpr));
108
109 return new;
110 }
111
112 void zebra_pbr_add_rule(struct zebra_ns *zns, struct zebra_pbr_rule *rule)
113 {
114 (void)hash_get(zns->rules_hash, rule, pbr_rule_alloc_intern);
115 kernel_add_pbr_rule(rule);
116 }
117
118 void zebra_pbr_del_rule(struct zebra_ns *zns, struct zebra_pbr_rule *rule)
119 {
120 struct zebra_pbr_rule *lookup;
121
122 lookup = hash_lookup(zns->rules_hash, rule);
123 kernel_del_pbr_rule(rule);
124
125 if (lookup)
126 XFREE(MTYPE_TMP, lookup);
127 else
128 zlog_warn("%s: Rule being deleted we know nothing about",
129 __PRETTY_FUNCTION__);
130 }
131
132 /*
133 * Handle success or failure of rule (un)install in the kernel.
134 */
135 void kernel_pbr_rule_add_del_status(struct zebra_pbr_rule *rule,
136 enum southbound_results res)
137 {
138 }
139
140 /*
141 * Handle rule delete notification from kernel.
142 */
143 int kernel_pbr_rule_del(struct zebra_pbr_rule *rule)
144 {
145 return 0;
146 }