]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_pbr.h
zebra: handling of policy routing iptable tcpflags
[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 struct zebra_pbr_rule {
36 int sock;
37
38 struct pbr_rule rule;
39
40 struct interface *ifp;
41 };
42
43 #define IS_RULE_FILTERING_ON_SRC_IP(r) \
44 (r->rule.filter.filter_bm & PBR_FILTER_SRC_IP)
45 #define IS_RULE_FILTERING_ON_DST_IP(r) \
46 (r->rule.filter.filter_bm & PBR_FILTER_DST_IP)
47 #define IS_RULE_FILTERING_ON_SRC_PORT(r) \
48 (r->rule.filter.filter_bm & PBR_FILTER_SRC_PORT)
49 #define IS_RULE_FILTERING_ON_DST_PORT(r) \
50 (r->rule.filter.filter_bm & PBR_FILTER_DST_PORT)
51 #define IS_RULE_FILTERING_ON_FWMARK(r) \
52 (r->rule.filter.filter_bm & PBR_FILTER_FWMARK)
53
54 /*
55 * An IPSet Entry Filter
56 *
57 * This is a filter mapped on ipset entries
58 */
59 struct zebra_pbr_ipset {
60 /*
61 * Originating zclient sock fd, so we can know who to send
62 * back to.
63 */
64 int sock;
65
66 vrf_id_t vrf_id;
67
68 uint32_t unique;
69
70 /* type is encoded as uint32_t
71 * but value is an enum ipset_type
72 */
73 uint32_t type;
74 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
75 };
76
77 /*
78 * An IPSet Entry Filter
79 *
80 * This is a filter mapped on ipset entries
81 */
82 struct zebra_pbr_ipset_entry {
83 /*
84 * Originating zclient sock fd, so we can know who to send
85 * back to.
86 */
87 int sock;
88
89 uint32_t unique;
90
91 struct prefix src;
92 struct prefix dst;
93
94 /* udp/tcp src port or icmp type */
95 uint16_t src_port_min;
96 uint16_t src_port_max;
97 /* udp/tcp dst port or icmp code */
98 uint16_t dst_port_min;
99 uint16_t dst_port_max;
100
101 uint8_t proto;
102
103 uint32_t filter_bm;
104
105 struct zebra_pbr_ipset *backpointer;
106 };
107
108 /*
109 * An IPTables Action
110 *
111 * This is a filter mapped on ipset entries
112 */
113 struct zebra_pbr_iptable {
114 /*
115 * Originating zclient sock fd, so we can know who to send
116 * back to.
117 */
118 int sock;
119
120 vrf_id_t vrf_id;
121
122 uint32_t unique;
123
124 /* include ipset type
125 */
126 uint32_t type;
127
128 /* include which IP is to be filtered
129 */
130 uint32_t filter_bm;
131
132 uint32_t fwmark;
133
134 uint32_t action;
135
136 uint16_t pkt_len_min;
137 uint16_t pkt_len_max;
138 uint16_t tcp_flags;
139 uint16_t tcp_mask_flags;
140
141 uint32_t nb_interface;
142
143 struct list *interface_name_list;
144
145 char ipset_name[ZEBRA_IPSET_NAME_SIZE];
146 };
147
148 extern const struct message icmp_typecode_str[];
149
150 const char *zebra_pbr_ipset_type2str(uint32_t type);
151
152 void zebra_pbr_add_rule(struct zebra_ns *zns, struct zebra_pbr_rule *rule);
153 void zebra_pbr_del_rule(struct zebra_ns *zns, struct zebra_pbr_rule *rule);
154 void zebra_pbr_create_ipset(struct zebra_ns *zns,
155 struct zebra_pbr_ipset *ipset);
156 void zebra_pbr_destroy_ipset(struct zebra_ns *zns,
157 struct zebra_pbr_ipset *ipset);
158 struct zebra_pbr_ipset *zebra_pbr_lookup_ipset_pername(struct zebra_ns *zns,
159 char *ipsetname);
160 void zebra_pbr_add_ipset_entry(struct zebra_ns *zns,
161 struct zebra_pbr_ipset_entry *ipset);
162 void zebra_pbr_del_ipset_entry(struct zebra_ns *zns,
163 struct zebra_pbr_ipset_entry *ipset);
164
165 void zebra_pbr_add_iptable(struct zebra_ns *zns,
166 struct zebra_pbr_iptable *iptable);
167 void zebra_pbr_del_iptable(struct zebra_ns *zns,
168 struct zebra_pbr_iptable *iptable);
169
170 /*
171 * Install specified rule for a specific interface.
172 * It is possible that the user-defined sequence number and the one in the
173 * forwarding plane may not coincide, hence the API requires a separate
174 * rule priority - maps to preference/FRA_PRIORITY on Linux.
175 */
176 extern enum dp_req_result kernel_add_pbr_rule(struct zebra_pbr_rule *rule);
177
178 /*
179 * Uninstall specified rule for a specific interface.
180 */
181 extern enum dp_req_result kernel_del_pbr_rule(struct zebra_pbr_rule *rule);
182
183 /*
184 * Get to know existing PBR rules in the kernel - typically called at startup.
185 */
186 extern void kernel_read_pbr_rules(struct zebra_ns *zns);
187
188 enum dp_results;
189 /*
190 * Handle success or failure of rule (un)install in the kernel.
191 */
192 extern void kernel_pbr_rule_add_del_status(struct zebra_pbr_rule *rule,
193 enum dp_results res);
194
195 /*
196 * Handle success or failure of ipset kinds (un)install in the kernel.
197 */
198 extern void kernel_pbr_ipset_add_del_status(struct zebra_pbr_ipset *ipset,
199 enum dp_results res);
200
201 extern void kernel_pbr_ipset_entry_add_del_status(
202 struct zebra_pbr_ipset_entry *ipset,
203 enum dp_results res);
204
205 extern void kernel_pbr_iptable_add_del_status(struct zebra_pbr_iptable *iptable,
206 enum dp_results res);
207
208 /*
209 * Handle rule delete notification from kernel.
210 */
211 extern int kernel_pbr_rule_del(struct zebra_pbr_rule *rule);
212
213 extern void zebra_pbr_rules_free(void *arg);
214 extern uint32_t zebra_pbr_rules_hash_key(void *arg);
215 extern int zebra_pbr_rules_hash_equal(const void *arg1, const void *arg2);
216
217 /* has operates on 32bit pointer
218 * and field is a string of 8bit
219 */
220 #define ZEBRA_IPSET_NAME_HASH_SIZE (ZEBRA_IPSET_NAME_SIZE / 4)
221
222 extern void zebra_pbr_ipset_free(void *arg);
223 extern uint32_t zebra_pbr_ipset_hash_key(void *arg);
224 extern int zebra_pbr_ipset_hash_equal(const void *arg1, const void *arg2);
225
226 extern void zebra_pbr_ipset_entry_free(void *arg);
227 extern uint32_t zebra_pbr_ipset_entry_hash_key(void *arg);
228 extern int zebra_pbr_ipset_entry_hash_equal(const void *arg1, const void *arg2);
229
230 extern void zebra_pbr_iptable_free(void *arg);
231 extern uint32_t zebra_pbr_iptable_hash_key(void *arg);
232 extern int zebra_pbr_iptable_hash_equal(const void *arg1, const void *arg2);
233
234 extern void zebra_pbr_init(void);
235 extern void zebra_pbr_show_ipset_list(struct vty *vty, char *ipsetname);
236 extern void zebra_pbr_show_iptable(struct vty *vty);
237 extern void zebra_pbr_iptable_update_interfacelist(struct stream *s,
238 struct zebra_pbr_iptable *zpi);
239 size_t zebra_pbr_tcpflags_snprintf(char *buffer, size_t len,
240 uint16_t tcp_val);
241
242 DECLARE_HOOK(zebra_pbr_ipset_entry_wrap_script_get_stat, (struct zebra_ns *zns,
243 struct zebra_pbr_ipset_entry *ipset,
244 uint64_t *pkts, uint64_t *bytes),
245 (zns, ipset, pkts, bytes))
246 DECLARE_HOOK(zebra_pbr_iptable_wrap_script_get_stat, (struct zebra_ns *zns,
247 struct zebra_pbr_iptable *iptable,
248 uint64_t *pkts, uint64_t *bytes),
249 (zns, iptable, pkts, bytes))
250 DECLARE_HOOK(zebra_pbr_iptable_wrap_script_update, (struct zebra_ns *zns,
251 int cmd,
252 struct zebra_pbr_iptable *iptable),
253 (zns, cmd, iptable));
254
255 DECLARE_HOOK(zebra_pbr_ipset_entry_wrap_script_update, (struct zebra_ns *zns,
256 int cmd,
257 struct zebra_pbr_ipset_entry *ipset),
258 (zns, cmd, ipset));
259 DECLARE_HOOK(zebra_pbr_ipset_wrap_script_update, (struct zebra_ns *zns,
260 int cmd,
261 struct zebra_pbr_ipset *ipset),
262 (zns, cmd, ipset));
263
264 #endif /* _ZEBRA_PBR_H */