]>
Commit | Line | Data |
---|---|---|
bbe6ffd6 PG |
1 | /* |
2 | * BGP pbr | |
3 | * Copyright (C) 6WIND | |
4 | * | |
5 | * FRR is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the | |
7 | * Free Software Foundation; either version 2, or (at your option) any | |
8 | * later version. | |
9 | * | |
10 | * FRR is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | #ifndef __BGP_PBR_H__ | |
20 | #define __BGP_PBR_H__ | |
21 | ||
22 | #include "nexthop.h" | |
23 | #include "zclient.h" | |
24 | ||
b46b6f1a PG |
25 | /* flowspec case: 0 to 3 actions maximum: |
26 | * 1 redirect | |
27 | * 1 set dscp | |
28 | * 1 set traffic rate | |
29 | */ | |
30 | #define ACTIONS_MAX_NUM 4 | |
31 | enum bgp_pbr_action_enum { | |
32 | ACTION_TRAFFICRATE = 1, | |
33 | ACTION_TRAFFIC_ACTION = 2, | |
34 | ACTION_REDIRECT = 3, | |
35 | ACTION_MARKING = 4, | |
36 | ACTION_REDIRECT_IP = 5 | |
37 | }; | |
38 | ||
39 | #define TRAFFIC_ACTION_SAMPLE (1 << 0) | |
40 | #define TRAFFIC_ACTION_TERMINATE (1 << 1) | |
41 | #define TRAFFIC_ACTION_DISTRIBUTE (1 << 2) | |
42 | ||
43 | #define OPERATOR_COMPARE_LESS_THAN (1<<1) | |
44 | #define OPERATOR_COMPARE_GREATER_THAN (1<<2) | |
45 | #define OPERATOR_COMPARE_EQUAL_TO (1<<3) | |
46 | #define OPERATOR_COMPARE_EXACT_MATCH (1<<4) | |
47 | ||
48 | #define OPERATOR_UNARY_OR (1<<1) | |
49 | #define OPERATOR_UNARY_AND (1<<2) | |
50 | ||
51 | /* struct used to store values [0;65535] | |
52 | * this can be used for port number of protocol | |
53 | */ | |
54 | #define BGP_PBR_MATCH_VAL_MAX 5 | |
55 | ||
56 | struct bgp_pbr_match_val { | |
57 | uint16_t value; | |
58 | uint8_t compare_operator; | |
59 | uint8_t unary_operator; | |
3f54c705 | 60 | }; |
b46b6f1a PG |
61 | |
62 | #define FRAGMENT_DONT 1 | |
63 | #define FRAGMENT_IS 2 | |
64 | #define FRAGMENT_FIRST 4 | |
65 | #define FRAGMENT_LAST 8 | |
66 | ||
b46b6f1a PG |
67 | struct bgp_pbr_entry_action { |
68 | /* used to store enum bgp_pbr_action_enum enumerate */ | |
69 | uint8_t action; | |
70 | union { | |
71 | union { | |
72 | uint8_t rate_info[4]; /* IEEE.754.1985 */ | |
73 | float rate; | |
74 | } r __attribute__((aligned(8))); | |
75 | struct _pbr_action { | |
76 | uint8_t do_sample; | |
77 | uint8_t filter; | |
78 | } za; | |
79 | vrf_id_t redirect_vrf; | |
80 | struct _pbr_redirect_ip { | |
81 | struct in_addr redirect_ip_v4; | |
82 | uint8_t duplicate; | |
83 | } zr; | |
84 | uint8_t marking_dscp; | |
85 | } u __attribute__((aligned(8))); | |
86 | }; | |
87 | ||
88 | /* BGP Policy Route structure */ | |
89 | struct bgp_pbr_entry_main { | |
5fa779c9 PG |
90 | #define BGP_PBR_UNDEFINED 0 |
91 | #define BGP_PBR_IPSET 1 | |
92 | #define BGP_PBR_IPRULE 2 | |
b46b6f1a | 93 | uint8_t type; |
b46b6f1a PG |
94 | |
95 | /* | |
96 | * This is an enum but we are going to treat it as a uint8_t | |
97 | * for purpose of encoding/decoding | |
98 | */ | |
99 | afi_t afi; | |
100 | safi_t safi; | |
101 | ||
102 | #define PREFIX_SRC_PRESENT (1 << 0) | |
103 | #define PREFIX_DST_PRESENT (1 << 1) | |
5fa779c9 | 104 | uint8_t match_bitmask_iprule; |
b46b6f1a PG |
105 | uint8_t match_bitmask; |
106 | ||
107 | uint8_t match_src_port_num; | |
108 | uint8_t match_dst_port_num; | |
109 | uint8_t match_port_num; | |
110 | uint8_t match_protocol_num; | |
111 | uint8_t match_icmp_type_num; | |
112 | uint8_t match_icmp_code_num; | |
113 | uint8_t match_packet_length_num; | |
114 | uint8_t match_dscp_num; | |
115 | uint8_t match_tcpflags_num; | |
588ec356 | 116 | uint8_t match_fragment_num; |
b46b6f1a PG |
117 | |
118 | struct prefix src_prefix; | |
119 | struct prefix dst_prefix; | |
120 | ||
1de7dfff PG |
121 | #define PROTOCOL_UDP 17 |
122 | #define PROTOCOL_TCP 6 | |
932404b7 | 123 | #define PROTOCOL_ICMP 1 |
b46b6f1a PG |
124 | struct bgp_pbr_match_val protocol[BGP_PBR_MATCH_VAL_MAX]; |
125 | struct bgp_pbr_match_val src_port[BGP_PBR_MATCH_VAL_MAX]; | |
126 | struct bgp_pbr_match_val dst_port[BGP_PBR_MATCH_VAL_MAX]; | |
127 | struct bgp_pbr_match_val port[BGP_PBR_MATCH_VAL_MAX]; | |
128 | struct bgp_pbr_match_val icmp_type[BGP_PBR_MATCH_VAL_MAX]; | |
129 | struct bgp_pbr_match_val icmp_code[BGP_PBR_MATCH_VAL_MAX]; | |
130 | struct bgp_pbr_match_val packet_length[BGP_PBR_MATCH_VAL_MAX]; | |
131 | struct bgp_pbr_match_val dscp[BGP_PBR_MATCH_VAL_MAX]; | |
2da7d62e | 132 | |
b46b6f1a | 133 | struct bgp_pbr_match_val tcpflags[BGP_PBR_MATCH_VAL_MAX]; |
588ec356 | 134 | struct bgp_pbr_match_val fragment[BGP_PBR_MATCH_VAL_MAX]; |
b46b6f1a PG |
135 | |
136 | uint16_t action_num; | |
137 | struct bgp_pbr_entry_action actions[ACTIONS_MAX_NUM]; | |
138 | ||
b46b6f1a PG |
139 | vrf_id_t vrf_id; |
140 | }; | |
141 | ||
4762c213 PG |
142 | struct bgp_pbr_interface { |
143 | RB_ENTRY(bgp_pbr_interface) id_entry; | |
144 | char name[INTERFACE_NAMSIZ]; | |
145 | }; | |
146 | ||
147 | RB_HEAD(bgp_pbr_interface_head, bgp_pbr_interface); | |
148 | RB_PROTOTYPE(bgp_pbr_interface_head, bgp_pbr_interface, id_entry, | |
149 | bgp_pbr_interface_compare); | |
150 | ||
151 | extern int bgp_pbr_interface_compare(const struct bgp_pbr_interface *a, | |
152 | const struct bgp_pbr_interface *b); | |
153 | ||
154 | struct bgp_pbr_config { | |
155 | struct bgp_pbr_interface_head ifaces_by_name_ipv4; | |
156 | bool pbr_interface_any_ipv4; | |
157 | }; | |
158 | ||
159 | extern struct bgp_pbr_config *bgp_pbr_cfg; | |
160 | ||
27e376d4 PG |
161 | struct bgp_pbr_rule { |
162 | uint32_t flags; | |
163 | struct prefix src; | |
164 | struct prefix dst; | |
165 | struct bgp_pbr_action *action; | |
166 | vrf_id_t vrf_id; | |
167 | uint32_t unique; | |
168 | bool installed; | |
169 | bool install_in_progress; | |
170 | }; | |
171 | ||
bbe6ffd6 PG |
172 | struct bgp_pbr_match { |
173 | char ipset_name[ZEBRA_IPSET_NAME_SIZE]; | |
174 | ||
175 | /* mapped on enum ipset_type | |
176 | */ | |
177 | uint32_t type; | |
178 | ||
f3d32faa PG |
179 | uint32_t flags; |
180 | ||
83360720 PG |
181 | uint16_t pkt_len_min; |
182 | uint16_t pkt_len_max; | |
2da7d62e PG |
183 | uint16_t tcp_flags; |
184 | uint16_t tcp_mask_flags; | |
4977bd6c | 185 | uint8_t dscp_value; |
6f5617d8 | 186 | uint8_t fragment; |
83360720 | 187 | |
f3d32faa PG |
188 | vrf_id_t vrf_id; |
189 | ||
190 | /* unique identifier for ipset create transaction | |
191 | */ | |
bbe6ffd6 PG |
192 | uint32_t unique; |
193 | ||
f3d32faa PG |
194 | /* unique identifier for iptable add transaction |
195 | */ | |
196 | uint32_t unique2; | |
197 | ||
bbe6ffd6 | 198 | bool installed; |
30d50e6d | 199 | bool install_in_progress; |
f3d32faa PG |
200 | |
201 | bool installed_in_iptable; | |
202 | bool install_iptable_in_progress; | |
203 | ||
204 | struct hash *entry_hash; | |
205 | ||
206 | struct bgp_pbr_action *action; | |
207 | ||
bbe6ffd6 PG |
208 | }; |
209 | ||
210 | struct bgp_pbr_match_entry { | |
211 | struct bgp_pbr_match *backpointer; | |
212 | ||
213 | uint32_t unique; | |
214 | ||
215 | struct prefix src; | |
216 | struct prefix dst; | |
217 | ||
f730e566 PG |
218 | uint16_t src_port_min; |
219 | uint16_t src_port_max; | |
220 | uint16_t dst_port_min; | |
221 | uint16_t dst_port_max; | |
222 | uint8_t proto; | |
223 | ||
9b6d8fcf | 224 | void *path; |
b588b642 | 225 | |
bbe6ffd6 PG |
226 | bool installed; |
227 | bool install_in_progress; | |
228 | }; | |
229 | ||
230 | struct bgp_pbr_action { | |
231 | ||
232 | /* | |
233 | * The Unique identifier of this specific pbrms | |
234 | */ | |
235 | uint32_t unique; | |
236 | ||
237 | uint32_t fwmark; | |
238 | ||
239 | uint32_t table_id; | |
240 | ||
f3d32faa PG |
241 | float rate; |
242 | ||
bbe6ffd6 PG |
243 | /* |
244 | * nexthop information, or drop information | |
245 | * contains src vrf_id and nh contains dest vrf_id | |
246 | */ | |
247 | vrf_id_t vrf_id; | |
248 | struct nexthop nh; | |
249 | ||
250 | bool installed; | |
251 | bool install_in_progress; | |
a6b07429 PG |
252 | uint32_t refcnt; |
253 | struct bgp *bgp; | |
bbe6ffd6 PG |
254 | }; |
255 | ||
ffee150e PG |
256 | extern struct bgp_pbr_rule *bgp_pbr_rule_lookup(vrf_id_t vrf_id, |
257 | uint32_t unique); | |
258 | ||
70eabd12 PG |
259 | extern struct bgp_pbr_action *bgp_pbr_action_rule_lookup(vrf_id_t vrf_id, |
260 | uint32_t unique); | |
bbe6ffd6 PG |
261 | |
262 | extern struct bgp_pbr_match *bgp_pbr_match_ipset_lookup(vrf_id_t vrf_id, | |
263 | uint32_t unique); | |
264 | ||
265 | extern struct bgp_pbr_match_entry *bgp_pbr_match_ipset_entry_lookup( | |
266 | vrf_id_t vrf_id, char *name, | |
267 | uint32_t unique); | |
1815c6fc PG |
268 | extern struct bgp_pbr_match *bgp_pbr_match_iptable_lookup(vrf_id_t vrf_id, |
269 | uint32_t unique); | |
f3d32faa | 270 | |
a6b07429 | 271 | extern void bgp_pbr_cleanup(struct bgp *bgp); |
f3d32faa PG |
272 | extern void bgp_pbr_init(struct bgp *bgp); |
273 | ||
27e376d4 PG |
274 | extern uint32_t bgp_pbr_rule_hash_key(void *arg); |
275 | extern bool bgp_pbr_rule_hash_equal(const void *arg1, | |
276 | const void *arg2); | |
f3d32faa | 277 | extern uint32_t bgp_pbr_action_hash_key(void *arg); |
74df8d6d | 278 | extern bool bgp_pbr_action_hash_equal(const void *arg1, |
f3d32faa PG |
279 | const void *arg2); |
280 | extern uint32_t bgp_pbr_match_entry_hash_key(void *arg); | |
74df8d6d | 281 | extern bool bgp_pbr_match_entry_hash_equal(const void *arg1, |
f3d32faa PG |
282 | const void *arg2); |
283 | extern uint32_t bgp_pbr_match_hash_key(void *arg); | |
74df8d6d | 284 | extern bool bgp_pbr_match_hash_equal(const void *arg1, |
f3d32faa PG |
285 | const void *arg2); |
286 | ||
b46b6f1a PG |
287 | void bgp_pbr_print_policy_route(struct bgp_pbr_entry_main *api); |
288 | ||
45918cfb | 289 | struct bgp_node; |
4b7e6066 | 290 | struct bgp_path_info; |
45918cfb | 291 | extern void bgp_pbr_update_entry(struct bgp *bgp, struct prefix *p, |
4b7e6066 DS |
292 | struct bgp_path_info *new_select, afi_t afi, |
293 | safi_t safi, bool nlri_update); | |
45918cfb | 294 | |
4762c213 PG |
295 | /* bgp pbr utilities */ |
296 | extern struct bgp_pbr_interface *pbr_interface_lookup(const char *name); | |
297 | extern void bgp_pbr_reset(struct bgp *bgp, afi_t afi); | |
298 | extern struct bgp_pbr_interface *bgp_pbr_interface_lookup(const char *name, | |
299 | struct bgp_pbr_interface_head *head); | |
300 | ||
0378bcaa PG |
301 | extern int bgp_pbr_build_and_validate_entry(struct prefix *p, |
302 | struct bgp_path_info *path, | |
303 | struct bgp_pbr_entry_main *api); | |
bbe6ffd6 | 304 | #endif /* __BGP_PBR_H__ */ |