]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_pbr.h
Merge pull request #11843 from ARShreenidhi/ecmp_testcase
[mirror_frr.git] / zebra / zebra_pbr.h
1 /*
2 * Zebra Policy Based Routing (PBR) Data structures and definitions
3 * These are public definitions referenced by multiple files.
4 * Copyright (C) 2018 Cumulus Networks, Inc.
5 *
6 * This file is part of FRR.
7 *
8 * FRR is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * FRR is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with FRR; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
22 */
23
24 #ifndef _ZEBRA_PBR_H
25 #define _ZEBRA_PBR_H
26
27 #include <zebra.h>
28
29 #include "prefix.h"
30 #include "if.h"
31
32 #include "rt.h"
33 #include "pbr.h"
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 struct zebra_pbr_action {
40 afi_t afi;
41
42 /* currently only one nexthop is supported */
43 union g_addr gate;
44
45 /* dest-interface */
46 ifindex_t ifindex;
47
48 /* dataplane info */
49 intptr_t dp_flow_ptr;
50
51 /* neigh */
52 struct zebra_neigh_ent *neigh;
53 /* zebra_pbr_rule is linked to neigh via neigh_listnode */
54 struct listnode neigh_listnode;
55 };
56
57 struct zebra_pbr_rule {
58 int sock;
59
60 struct pbr_rule rule;
61
62 char ifname[INTERFACE_NAMSIZ];
63
64 struct zebra_pbr_action action;
65
66 vrf_id_t vrf_id;
67 };
68
69 #define IS_RULE_FILTERING_ON_SRC_IP(r) \
70 (r->rule.filter.filter_bm & PBR_FILTER_SRC_IP)
71 #define IS_RULE_FILTERING_ON_DST_IP(r) \
72 (r->rule.filter.filter_bm & PBR_FILTER_DST_IP)
73 #define IS_RULE_FILTERING_ON_SRC_PORT(r) \
74 (r->rule.filter.filter_bm & PBR_FILTER_SRC_PORT)
75 #define IS_RULE_FILTERING_ON_DST_PORT(r) \
76 (r->rule.filter.filter_bm & PBR_FILTER_DST_PORT)
77 #define IS_RULE_FILTERING_ON_DSFIELD(r) \
78 (r->rule.filter.filter_bm & PBR_FILTER_DSFIELD)
79 #define IS_RULE_FILTERING_ON_FWMARK(r) \
80 (r->rule.filter.filter_bm & PBR_FILTER_FWMARK)
81
82 /*
83 * An IPSet Entry Filter
84 *
85 * This is a filter mapped on ipset entries
86 */
87 struct zebra_pbr_ipset_info {
88 /* type is encoded as uint32_t
89 * but value is an enum ipset_type
90 */
91 uint32_t type;
92
93 uint8_t family;
94
95 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
96 };
97
98 struct zebra_pbr_ipset {
99 /*
100 * Originating zclient sock fd, so we can know who to send
101 * back to.
102 */
103 int sock;
104
105 vrf_id_t vrf_id;
106
107 uint32_t unique;
108
109 /* type is encoded as uint32_t
110 * but value is an enum ipset_type
111 */
112 uint32_t type;
113
114 uint8_t family;
115
116 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
117 };
118
119
120 /*
121 * An IPSet Entry Filter
122 *
123 * This is a filter mapped on ipset entries
124 */
125 struct zebra_pbr_ipset_entry {
126 /*
127 * Originating zclient sock fd, so we can know who to send
128 * back to.
129 */
130 int sock;
131
132 uint32_t unique;
133
134 struct prefix src;
135 struct prefix dst;
136
137 /* udp/tcp src port or icmp type */
138 uint16_t src_port_min;
139 uint16_t src_port_max;
140 /* udp/tcp dst port or icmp code */
141 uint16_t dst_port_min;
142 uint16_t dst_port_max;
143
144 uint8_t proto;
145
146 uint32_t filter_bm;
147
148 struct zebra_pbr_ipset *backpointer;
149 };
150
151 /*
152 * An IPTables Action
153 *
154 * This is a filter mapped on ipset entries
155 */
156 struct zebra_pbr_iptable {
157 /*
158 * Originating zclient sock fd, so we can know who to send
159 * back to.
160 */
161 int sock;
162
163 vrf_id_t vrf_id;
164
165 uint32_t unique;
166
167 /* include ipset type
168 */
169 uint32_t type;
170
171 /* include which IP is to be filtered
172 */
173 uint32_t filter_bm;
174
175 uint32_t fwmark;
176
177 uint32_t action;
178
179 uint16_t pkt_len_min;
180 uint16_t pkt_len_max;
181 uint16_t tcp_flags;
182 uint16_t tcp_mask_flags;
183 uint8_t dscp_value;
184 uint8_t fragment;
185 uint8_t protocol;
186
187 uint32_t nb_interface;
188 uint16_t flow_label;
189
190 uint8_t family;
191
192 struct list *interface_name_list;
193
194 #define IPTABLE_INSTALL_QUEUED 1 << 1
195 #define IPTABLE_UNINSTALL_QUEUED 1 << 2
196 uint8_t internal_flags;
197 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
198 };
199
200 extern const struct message icmp_typecode_str[];
201 extern const struct message icmpv6_typecode_str[];
202
203 const char *zebra_pbr_ipset_type2str(uint32_t type);
204
205 void zebra_pbr_add_rule(struct zebra_pbr_rule *rule);
206 void zebra_pbr_del_rule(struct zebra_pbr_rule *rule);
207 void zebra_pbr_create_ipset(struct zebra_pbr_ipset *ipset);
208 void zebra_pbr_destroy_ipset(struct zebra_pbr_ipset *ipset);
209 struct zebra_pbr_ipset *zebra_pbr_lookup_ipset_pername(char *ipsetname);
210 void zebra_pbr_add_ipset_entry(struct zebra_pbr_ipset_entry *ipset);
211 void zebra_pbr_del_ipset_entry(struct zebra_pbr_ipset_entry *ipset);
212
213 void zebra_pbr_add_iptable(struct zebra_pbr_iptable *iptable);
214 void zebra_pbr_del_iptable(struct zebra_pbr_iptable *iptable);
215 void zebra_pbr_process_iptable(struct zebra_dplane_ctx *ctx);
216 void zebra_pbr_process_ipset(struct zebra_dplane_ctx *ctx);
217 void zebra_pbr_process_ipset_entry(struct zebra_dplane_ctx *ctx);
218
219 /*
220 * Get to know existing PBR rules in the kernel - typically called at startup.
221 */
222 extern void kernel_read_pbr_rules(struct zebra_ns *zns);
223
224 /*
225 * Handle success or failure of rule (un)install in the kernel.
226 */
227 extern void zebra_pbr_dplane_result(struct zebra_dplane_ctx *ctx);
228
229 /*
230 * Handle success or failure of ipset kinds (un)install in the kernel.
231 */
232 extern void kernel_pbr_ipset_add_del_status(struct zebra_pbr_ipset *ipset,
233 enum zebra_dplane_status res);
234
235 extern void kernel_pbr_ipset_entry_add_del_status(
236 struct zebra_pbr_ipset_entry *ipset,
237 enum zebra_dplane_status res);
238
239 /*
240 * Handle rule delete notification from kernel.
241 */
242 extern int kernel_pbr_rule_del(struct zebra_pbr_rule *rule);
243
244 extern void zebra_pbr_rules_free(void *arg);
245 extern uint32_t zebra_pbr_rules_hash_key(const void *arg);
246 extern bool zebra_pbr_rules_hash_equal(const void *arg1, const void *arg2);
247
248 /* has operates on 32bit pointer
249 * and field is a string of 8bit
250 */
251 #define ZEBRA_IPSET_NAME_HASH_SIZE (ZEBRA_IPSET_NAME_SIZE / 4)
252
253 extern void zebra_pbr_ipset_free(void *arg);
254 extern uint32_t zebra_pbr_ipset_hash_key(const void *arg);
255 extern bool zebra_pbr_ipset_hash_equal(const void *arg1, const void *arg2);
256
257 extern void zebra_pbr_ipset_entry_free(void *arg);
258 extern uint32_t zebra_pbr_ipset_entry_hash_key(const void *arg);
259 extern bool zebra_pbr_ipset_entry_hash_equal(const void *arg1,
260 const void *arg2);
261
262 extern void zebra_pbr_iptable_free(void *arg);
263 extern uint32_t zebra_pbr_iptable_hash_key(const void *arg);
264 extern bool zebra_pbr_iptable_hash_equal(const void *arg1, const void *arg2);
265
266 extern void zebra_pbr_config_write(struct vty *vty);
267 extern void zebra_pbr_expand_action_update(bool enable);
268 extern void zebra_pbr_init(void);
269 extern void zebra_pbr_show_ipset_list(struct vty *vty, char *ipsetname);
270 extern void zebra_pbr_show_iptable(struct vty *vty, char *iptable);
271 extern void zebra_pbr_iptable_update_interfacelist(struct stream *s,
272 struct zebra_pbr_iptable *zpi);
273 size_t zebra_pbr_tcpflags_snprintf(char *buffer, size_t len,
274 uint16_t tcp_val);
275 extern void zebra_pbr_show_rule(struct vty *vty);
276 extern void zebra_pbr_show_rule_unit(struct zebra_pbr_rule *rule,
277 struct vty *vty);
278
279 DECLARE_HOOK(zebra_pbr_ipset_entry_get_stat,
280 (struct zebra_pbr_ipset_entry *ipset, uint64_t *pkts,
281 uint64_t *bytes),
282 (ipset, pkts, bytes));
283 DECLARE_HOOK(zebra_pbr_iptable_get_stat,
284 (struct zebra_pbr_iptable *iptable, uint64_t *pkts,
285 uint64_t *bytes),
286 (iptable, pkts, bytes));
287 DECLARE_HOOK(zebra_pbr_iptable_update,
288 (int cmd, struct zebra_pbr_iptable *iptable), (cmd, iptable));
289
290 DECLARE_HOOK(zebra_pbr_ipset_entry_update,
291 (int cmd, struct zebra_pbr_ipset_entry *ipset), (cmd, ipset));
292 DECLARE_HOOK(zebra_pbr_ipset_update,
293 (int cmd, struct zebra_pbr_ipset *ipset), (cmd, ipset));
294
295 #ifdef __cplusplus
296 }
297 #endif
298
299 #endif /* _ZEBRA_PBR_H */