]>
Commit | Line | Data |
---|---|---|
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> | |
26 | ||
942bf97b | 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 */ | |
43fe6a2a | 37 | void zebra_pbr_rules_free(void *arg) |
1fbfe5a5 | 38 | { |
43fe6a2a DS |
39 | struct zebra_pbr_rule *rule; |
40 | ||
41 | rule = (struct zebra_pbr_rule *)arg; | |
42 | ||
a0321978 | 43 | kernel_del_pbr_rule(rule); |
43fe6a2a DS |
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)); | |
a0321978 DS |
55 | if (rule->ifp) |
56 | key = jhash_1word(rule->ifp->ifindex, key); | |
57 | else | |
58 | key = jhash_1word(0, key); | |
59 | ||
43fe6a2a | 60 | return jhash_3words(rule->filter.src_port, rule->filter.dst_port, |
b6c5d343 DS |
61 | prefix_hash_key(&rule->filter.dst_ip), |
62 | jhash_1word(rule->unique, key)); | |
43fe6a2a DS |
63 | } |
64 | ||
65 | int zebra_pbr_rules_hash_equal(const void *arg1, const void *arg2) | |
66 | { | |
67 | const struct zebra_pbr_rule *r1, *r2; | |
68 | ||
69 | r1 = (const struct zebra_pbr_rule *)arg1; | |
70 | r2 = (const struct zebra_pbr_rule *)arg2; | |
71 | ||
72 | if (r1->seq != r2->seq) | |
73 | return 0; | |
74 | ||
75 | if (r1->priority != r2->priority) | |
76 | return 0; | |
77 | ||
b6c5d343 DS |
78 | if (r1->unique != r2->unique) |
79 | return 0; | |
80 | ||
43fe6a2a DS |
81 | if (r1->action.table != r2->action.table) |
82 | return 0; | |
83 | ||
84 | if (r1->filter.src_port != r2->filter.src_port) | |
85 | return 0; | |
86 | ||
87 | if (r1->filter.dst_port != r2->filter.dst_port) | |
88 | return 0; | |
89 | ||
90 | if (!prefix_same(&r1->filter.src_ip, &r2->filter.src_ip)) | |
91 | return 0; | |
92 | ||
93 | if (!prefix_same(&r1->filter.dst_ip, &r2->filter.dst_ip)) | |
94 | return 0; | |
95 | ||
a0321978 DS |
96 | if (r1->ifp != r2->ifp) |
97 | return 0; | |
98 | ||
43fe6a2a DS |
99 | return 1; |
100 | } | |
101 | ||
102 | static void *pbr_rule_alloc_intern(void *arg) | |
103 | { | |
104 | struct zebra_pbr_rule *zpr; | |
105 | struct zebra_pbr_rule *new; | |
106 | ||
107 | zpr = (struct zebra_pbr_rule *)arg; | |
108 | ||
109 | new = XCALLOC(MTYPE_TMP, sizeof(*new)); | |
110 | ||
111 | memcpy(new, zpr, sizeof(*zpr)); | |
112 | ||
113 | return new; | |
114 | } | |
115 | ||
a0321978 | 116 | void zebra_pbr_add_rule(struct zebra_ns *zns, struct zebra_pbr_rule *rule) |
43fe6a2a DS |
117 | { |
118 | (void)hash_get(zns->rules_hash, rule, pbr_rule_alloc_intern); | |
a0321978 | 119 | kernel_add_pbr_rule(rule); |
1fbfe5a5 DS |
120 | } |
121 | ||
a0321978 | 122 | void zebra_pbr_del_rule(struct zebra_ns *zns, struct zebra_pbr_rule *rule) |
1fbfe5a5 | 123 | { |
43fe6a2a DS |
124 | struct zebra_pbr_rule *lookup; |
125 | ||
126 | lookup = hash_lookup(zns->rules_hash, rule); | |
a0321978 | 127 | kernel_del_pbr_rule(rule); |
43fe6a2a DS |
128 | |
129 | if (lookup) | |
130 | XFREE(MTYPE_TMP, lookup); | |
131 | else | |
132 | zlog_warn("%s: Rule being deleted we know nothing about", | |
133 | __PRETTY_FUNCTION__); | |
1fbfe5a5 DS |
134 | } |
135 | ||
942bf97b | 136 | /* |
137 | * Handle success or failure of rule (un)install in the kernel. | |
138 | */ | |
139 | void kernel_pbr_rule_add_del_status(struct zebra_pbr_rule *rule, | |
942bf97b | 140 | enum southbound_results res) |
141 | { | |
b6c5d343 DS |
142 | switch (res) { |
143 | case SOUTHBOUND_INSTALL_SUCCESS: | |
144 | zsend_rule_notify_owner(rule, ZAPI_RULE_INSTALLED); | |
145 | break; | |
146 | case SOUTHBOUND_INSTALL_FAILURE: | |
147 | zsend_rule_notify_owner(rule, ZAPI_RULE_FAIL_INSTALL); | |
148 | break; | |
149 | case SOUTHBOUND_DELETE_SUCCESS: | |
150 | break; | |
151 | case SOUTHBOUND_DELETE_FAILURE: | |
152 | break; | |
153 | } | |
942bf97b | 154 | } |
155 | ||
156 | /* | |
157 | * Handle rule delete notification from kernel. | |
158 | */ | |
a0321978 | 159 | int kernel_pbr_rule_del(struct zebra_pbr_rule *rule) |
942bf97b | 160 | { |
161 | return 0; | |
162 | } |