]>
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 | ||
20 | #include "zebra.h" | |
21 | #include "prefix.h" | |
22 | #include "zclient.h" | |
f3d32faa | 23 | #include "jhash.h" |
2e1f721e | 24 | #include "pbr.h" |
bbe6ffd6 | 25 | |
f3d32faa | 26 | #include "bgpd/bgpd.h" |
bbe6ffd6 | 27 | #include "bgpd/bgp_pbr.h" |
b46b6f1a | 28 | #include "bgpd/bgp_debug.h" |
45918cfb PG |
29 | #include "bgpd/bgp_flowspec_util.h" |
30 | #include "bgpd/bgp_ecommunity.h" | |
31 | #include "bgpd/bgp_route.h" | |
32 | #include "bgpd/bgp_attr.h" | |
d114b0d7 | 33 | #include "bgpd/bgp_zebra.h" |
529efa23 | 34 | #include "bgpd/bgp_mplsvpn.h" |
35703998 | 35 | #include "bgpd/bgp_flowspec_private.h" |
4f3be667 | 36 | #include "bgpd/bgp_errors.h" |
d114b0d7 PG |
37 | |
38 | DEFINE_MTYPE_STATIC(BGPD, PBR_MATCH_ENTRY, "PBR match entry") | |
39 | DEFINE_MTYPE_STATIC(BGPD, PBR_MATCH, "PBR match") | |
40 | DEFINE_MTYPE_STATIC(BGPD, PBR_ACTION, "PBR action") | |
4762c213 | 41 | DEFINE_MTYPE_STATIC(BGPD, PBR, "BGP PBR Context") |
f7b2e630 | 42 | DEFINE_MTYPE_STATIC(BGPD, PBR_VALMASK, "BGP PBR Val Mask Value") |
4762c213 PG |
43 | |
44 | RB_GENERATE(bgp_pbr_interface_head, bgp_pbr_interface, | |
45 | id_entry, bgp_pbr_interface_compare); | |
46 | struct bgp_pbr_interface_head ifaces_by_name_ipv4 = | |
47 | RB_INITIALIZER(&ifaces_by_name_ipv4); | |
d114b0d7 PG |
48 | |
49 | static int bgp_pbr_match_counter_unique; | |
50 | static int bgp_pbr_match_entry_counter_unique; | |
51 | static int bgp_pbr_action_counter_unique; | |
52 | static int bgp_pbr_match_iptable_counter_unique; | |
b46b6f1a | 53 | |
1815c6fc PG |
54 | struct bgp_pbr_match_iptable_unique { |
55 | uint32_t unique; | |
56 | struct bgp_pbr_match *bpm_found; | |
57 | }; | |
58 | ||
c5d429e1 PG |
59 | struct bgp_pbr_match_entry_unique { |
60 | uint32_t unique; | |
61 | struct bgp_pbr_match_entry *bpme_found; | |
62 | }; | |
63 | ||
70eabd12 PG |
64 | struct bgp_pbr_action_unique { |
65 | uint32_t unique; | |
66 | struct bgp_pbr_action *bpa_found; | |
67 | }; | |
68 | ||
69 | static int bgp_pbr_action_walkcb(struct hash_backet *backet, void *arg) | |
70 | { | |
71 | struct bgp_pbr_action *bpa = (struct bgp_pbr_action *)backet->data; | |
72 | struct bgp_pbr_action_unique *bpau = (struct bgp_pbr_action_unique *) | |
73 | arg; | |
74 | uint32_t unique = bpau->unique; | |
75 | ||
76 | if (bpa->unique == unique) { | |
77 | bpau->bpa_found = bpa; | |
78 | return HASHWALK_ABORT; | |
79 | } | |
80 | return HASHWALK_CONTINUE; | |
81 | } | |
82 | ||
c5d429e1 PG |
83 | static int bgp_pbr_match_entry_walkcb(struct hash_backet *backet, void *arg) |
84 | { | |
85 | struct bgp_pbr_match_entry *bpme = | |
86 | (struct bgp_pbr_match_entry *)backet->data; | |
87 | struct bgp_pbr_match_entry_unique *bpmeu = | |
88 | (struct bgp_pbr_match_entry_unique *)arg; | |
89 | uint32_t unique = bpmeu->unique; | |
90 | ||
91 | if (bpme->unique == unique) { | |
92 | bpmeu->bpme_found = bpme; | |
93 | return HASHWALK_ABORT; | |
94 | } | |
95 | return HASHWALK_CONTINUE; | |
96 | } | |
97 | ||
98 | struct bgp_pbr_match_ipsetname { | |
99 | char *ipsetname; | |
100 | struct bgp_pbr_match *bpm_found; | |
101 | }; | |
102 | ||
103 | static int bgp_pbr_match_pername_walkcb(struct hash_backet *backet, void *arg) | |
104 | { | |
105 | struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)backet->data; | |
106 | struct bgp_pbr_match_ipsetname *bpmi = | |
107 | (struct bgp_pbr_match_ipsetname *)arg; | |
108 | char *ipset_name = bpmi->ipsetname; | |
109 | ||
110 | if (!strncmp(ipset_name, bpm->ipset_name, | |
111 | ZEBRA_IPSET_NAME_SIZE)) { | |
112 | bpmi->bpm_found = bpm; | |
113 | return HASHWALK_ABORT; | |
114 | } | |
115 | return HASHWALK_CONTINUE; | |
116 | } | |
117 | ||
1815c6fc PG |
118 | static int bgp_pbr_match_iptable_walkcb(struct hash_backet *backet, void *arg) |
119 | { | |
120 | struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)backet->data; | |
121 | struct bgp_pbr_match_iptable_unique *bpmiu = | |
122 | (struct bgp_pbr_match_iptable_unique *)arg; | |
123 | uint32_t unique = bpmiu->unique; | |
124 | ||
125 | if (bpm->unique2 == unique) { | |
126 | bpmiu->bpm_found = bpm; | |
127 | return HASHWALK_ABORT; | |
128 | } | |
129 | return HASHWALK_CONTINUE; | |
130 | } | |
131 | ||
c5d429e1 PG |
132 | struct bgp_pbr_match_unique { |
133 | uint32_t unique; | |
134 | struct bgp_pbr_match *bpm_found; | |
135 | }; | |
136 | ||
137 | static int bgp_pbr_match_walkcb(struct hash_backet *backet, void *arg) | |
138 | { | |
139 | struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)backet->data; | |
140 | struct bgp_pbr_match_unique *bpmu = (struct bgp_pbr_match_unique *) | |
141 | arg; | |
142 | uint32_t unique = bpmu->unique; | |
143 | ||
144 | if (bpm->unique == unique) { | |
145 | bpmu->bpm_found = bpm; | |
146 | return HASHWALK_ABORT; | |
147 | } | |
148 | return HASHWALK_CONTINUE; | |
149 | } | |
150 | ||
b46b6f1a PG |
151 | static int sprintf_bgp_pbr_match_val(char *str, struct bgp_pbr_match_val *mval, |
152 | const char *prepend) | |
153 | { | |
154 | char *ptr = str; | |
155 | ||
156 | if (prepend) | |
157 | ptr += sprintf(ptr, "%s", prepend); | |
158 | else { | |
159 | if (mval->unary_operator & OPERATOR_UNARY_OR) | |
160 | ptr += sprintf(ptr, ", or "); | |
161 | if (mval->unary_operator & OPERATOR_UNARY_AND) | |
162 | ptr += sprintf(ptr, ", and "); | |
163 | } | |
164 | if (mval->compare_operator & OPERATOR_COMPARE_LESS_THAN) | |
165 | ptr += sprintf(ptr, "<"); | |
166 | if (mval->compare_operator & OPERATOR_COMPARE_GREATER_THAN) | |
167 | ptr += sprintf(ptr, ">"); | |
168 | if (mval->compare_operator & OPERATOR_COMPARE_EQUAL_TO) | |
169 | ptr += sprintf(ptr, "="); | |
170 | if (mval->compare_operator & OPERATOR_COMPARE_EXACT_MATCH) | |
171 | ptr += sprintf(ptr, "match"); | |
172 | ptr += sprintf(ptr, " %u", mval->value); | |
173 | return (int)(ptr - str); | |
174 | } | |
175 | ||
176 | #define INCREMENT_DISPLAY(_ptr, _cnt) do { \ | |
177 | if (_cnt) \ | |
178 | (_ptr) += sprintf((_ptr), "; "); \ | |
179 | _cnt++; \ | |
180 | } while (0) | |
181 | ||
0e867886 PG |
182 | /* this structure can be used for port range, |
183 | * but also for other values range like packet length range | |
184 | */ | |
1de7dfff PG |
185 | struct bgp_pbr_range_port { |
186 | uint16_t min_port; | |
187 | uint16_t max_port; | |
188 | }; | |
189 | ||
2da7d62e PG |
190 | /* this structure can be used to filter with a mask |
191 | * for instance it supports not instructions like for | |
192 | * tcpflags | |
193 | */ | |
194 | struct bgp_pbr_val_mask { | |
195 | uint16_t val; | |
196 | uint16_t mask; | |
197 | }; | |
198 | ||
0e867886 PG |
199 | /* this structure is used to pass instructs |
200 | * so that BGP can create pbr instructions to ZEBRA | |
201 | */ | |
202 | struct bgp_pbr_filter { | |
203 | vrf_id_t vrf_id; | |
204 | struct prefix *src; | |
205 | struct prefix *dst; | |
206 | uint8_t protocol; | |
207 | struct bgp_pbr_range_port *pkt_len; | |
208 | struct bgp_pbr_range_port *src_port; | |
209 | struct bgp_pbr_range_port *dst_port; | |
2da7d62e | 210 | struct bgp_pbr_val_mask *tcp_flags; |
4977bd6c | 211 | struct bgp_pbr_val_mask *dscp; |
cfaf18ce | 212 | struct bgp_pbr_val_mask *pkt_len_val; |
6f5617d8 | 213 | struct bgp_pbr_val_mask *fragment; |
0e867886 PG |
214 | }; |
215 | ||
c5ee26cc PG |
216 | /* this structure is used to contain OR instructions |
217 | * so that BGP can create multiple pbr instructions | |
218 | * to ZEBRA | |
219 | */ | |
220 | struct bgp_pbr_or_filter { | |
221 | struct list *tcpflags; | |
56707a36 | 222 | struct list *dscp; |
cfaf18ce | 223 | struct list *pkt_len; |
6f5617d8 | 224 | struct list *fragment; |
da3fa383 PG |
225 | struct list *icmp_type; |
226 | struct list *icmp_code; | |
c5ee26cc PG |
227 | }; |
228 | ||
da3fa383 PG |
229 | static void bgp_pbr_policyroute_add_to_zebra_unit(struct bgp *bgp, |
230 | struct bgp_info *binfo, | |
231 | struct bgp_pbr_filter *bpf, | |
232 | struct nexthop *nh, | |
233 | float *rate); | |
6e288819 | 234 | |
064a9d52 PG |
235 | static void bgp_pbr_dump_entry(struct bgp_pbr_filter *bpf, bool add); |
236 | ||
6e288819 PG |
237 | static bool bgp_pbr_extract_enumerate_unary_opposite( |
238 | uint8_t unary_operator, | |
239 | struct bgp_pbr_val_mask *and_valmask, | |
240 | struct list *or_valmask, uint32_t value, | |
241 | uint8_t type_entry) | |
242 | { | |
243 | if (unary_operator == OPERATOR_UNARY_AND && and_valmask) { | |
244 | if (type_entry == FLOWSPEC_TCP_FLAGS) { | |
245 | and_valmask->mask |= | |
246 | TCP_HEADER_ALL_FLAGS & | |
247 | ~(value); | |
248 | } else if (type_entry == FLOWSPEC_DSCP || | |
249 | type_entry == FLOWSPEC_PKT_LEN || | |
250 | type_entry == FLOWSPEC_FRAGMENT) { | |
251 | and_valmask->val = value; | |
252 | and_valmask->mask = 1; /* inverse */ | |
253 | } | |
254 | } else if (unary_operator == OPERATOR_UNARY_OR && or_valmask) { | |
255 | and_valmask = XCALLOC(MTYPE_PBR_VALMASK, | |
256 | sizeof(struct bgp_pbr_val_mask)); | |
257 | if (type_entry == FLOWSPEC_TCP_FLAGS) { | |
258 | and_valmask->val = TCP_HEADER_ALL_FLAGS; | |
259 | and_valmask->mask |= | |
260 | TCP_HEADER_ALL_FLAGS & | |
261 | ~(value); | |
262 | } else if (type_entry == FLOWSPEC_DSCP || | |
263 | type_entry == FLOWSPEC_FRAGMENT || | |
264 | type_entry == FLOWSPEC_PKT_LEN) { | |
265 | and_valmask->val = value; | |
266 | and_valmask->mask = 1; /* inverse */ | |
267 | } | |
268 | listnode_add(or_valmask, and_valmask); | |
269 | } else if (type_entry == FLOWSPEC_ICMP_CODE || | |
270 | type_entry == FLOWSPEC_ICMP_TYPE) | |
271 | return false; | |
272 | return true; | |
273 | } | |
274 | ||
2da7d62e PG |
275 | /* TCP : FIN and SYN -> val = ALL; mask = 3 |
276 | * TCP : not (FIN and SYN) -> val = ALL; mask = ALL & ~(FIN|RST) | |
6f5617d8 PG |
277 | * other variables type: dscp, pkt len, fragment |
278 | * - value is copied in bgp_pbr_val_mask->val value | |
279 | * - if negate form is identifierd, bgp_pbr_val_mask->mask set to 1 | |
2da7d62e | 280 | */ |
f7b2e630 PG |
281 | static bool bgp_pbr_extract_enumerate_unary(struct bgp_pbr_match_val list[], |
282 | int num, uint8_t unary_operator, | |
35703998 | 283 | void *valmask, uint8_t type_entry) |
932404b7 PG |
284 | { |
285 | int i = 0; | |
f7b2e630 PG |
286 | struct bgp_pbr_val_mask *and_valmask = NULL; |
287 | struct list *or_valmask = NULL; | |
6e288819 | 288 | bool ret; |
932404b7 | 289 | |
f7b2e630 PG |
290 | if (valmask) { |
291 | if (unary_operator == OPERATOR_UNARY_AND) { | |
292 | and_valmask = (struct bgp_pbr_val_mask *)valmask; | |
293 | memset(and_valmask, 0, sizeof(struct bgp_pbr_val_mask)); | |
294 | } else if (unary_operator == OPERATOR_UNARY_OR) { | |
295 | or_valmask = (struct list *)valmask; | |
296 | } | |
297 | } | |
932404b7 | 298 | for (i = 0; i < num; i++) { |
2da7d62e PG |
299 | if (i != 0 && list[i].unary_operator != |
300 | unary_operator) | |
932404b7 | 301 | return false; |
2da7d62e PG |
302 | if (!(list[i].compare_operator & |
303 | OPERATOR_COMPARE_EQUAL_TO) && | |
304 | !(list[i].compare_operator & | |
305 | OPERATOR_COMPARE_EXACT_MATCH)) { | |
306 | if ((list[i].compare_operator & | |
307 | OPERATOR_COMPARE_LESS_THAN) && | |
308 | (list[i].compare_operator & | |
309 | OPERATOR_COMPARE_GREATER_THAN)) { | |
6e288819 PG |
310 | ret = bgp_pbr_extract_enumerate_unary_opposite( |
311 | unary_operator, and_valmask, | |
312 | or_valmask, list[i].value, | |
313 | type_entry); | |
314 | if (ret == false) | |
315 | return ret; | |
2da7d62e PG |
316 | continue; |
317 | } | |
318 | return false; | |
319 | } | |
35703998 PG |
320 | if (unary_operator == OPERATOR_UNARY_AND && and_valmask) { |
321 | if (type_entry == FLOWSPEC_TCP_FLAGS) | |
322 | and_valmask->mask |= | |
323 | TCP_HEADER_ALL_FLAGS & list[i].value; | |
324 | } else if (unary_operator == OPERATOR_UNARY_OR && or_valmask) { | |
f7b2e630 PG |
325 | and_valmask = XCALLOC(MTYPE_PBR_VALMASK, |
326 | sizeof(struct bgp_pbr_val_mask)); | |
35703998 PG |
327 | if (type_entry == FLOWSPEC_TCP_FLAGS) { |
328 | and_valmask->val = TCP_HEADER_ALL_FLAGS; | |
329 | and_valmask->mask |= | |
330 | TCP_HEADER_ALL_FLAGS & list[i].value; | |
cfaf18ce | 331 | } else if (type_entry == FLOWSPEC_DSCP || |
da3fa383 PG |
332 | type_entry == FLOWSPEC_ICMP_TYPE || |
333 | type_entry == FLOWSPEC_ICMP_CODE || | |
6f5617d8 | 334 | type_entry == FLOWSPEC_FRAGMENT || |
cfaf18ce | 335 | type_entry == FLOWSPEC_PKT_LEN) |
35703998 | 336 | and_valmask->val = list[i].value; |
f7b2e630 PG |
337 | listnode_add(or_valmask, and_valmask); |
338 | } | |
932404b7 | 339 | } |
35703998 PG |
340 | if (unary_operator == OPERATOR_UNARY_AND && and_valmask |
341 | && type_entry == FLOWSPEC_TCP_FLAGS) | |
342 | and_valmask->val = TCP_HEADER_ALL_FLAGS; | |
932404b7 PG |
343 | return true; |
344 | } | |
345 | ||
f7b2e630 PG |
346 | /* if unary operator can either be UNARY_OR/AND/OR-AND. |
347 | * in the latter case, combinationf of both is not handled | |
348 | */ | |
349 | static bool bgp_pbr_extract_enumerate(struct bgp_pbr_match_val list[], | |
350 | int num, uint8_t unary_operator, | |
35703998 | 351 | void *valmask, uint8_t type_entry) |
f7b2e630 PG |
352 | { |
353 | bool ret; | |
3f54c705 | 354 | uint8_t unary_operator_val; |
f7b2e630 PG |
355 | bool double_check = false; |
356 | ||
357 | if ((unary_operator & OPERATOR_UNARY_OR) && | |
358 | (unary_operator & OPERATOR_UNARY_AND)) { | |
359 | unary_operator_val = OPERATOR_UNARY_AND; | |
360 | double_check = true; | |
361 | } else | |
362 | unary_operator_val = unary_operator; | |
363 | ret = bgp_pbr_extract_enumerate_unary(list, num, unary_operator_val, | |
35703998 | 364 | valmask, type_entry); |
f7b2e630 PG |
365 | if (!ret && double_check) |
366 | ret = bgp_pbr_extract_enumerate_unary(list, num, | |
367 | OPERATOR_UNARY_OR, | |
35703998 PG |
368 | valmask, |
369 | type_entry); | |
f7b2e630 PG |
370 | return ret; |
371 | } | |
372 | ||
373 | /* returns the unary operator that is in the list | |
374 | * return 0 if both operators are used | |
375 | */ | |
376 | static uint8_t bgp_pbr_match_val_get_operator(struct bgp_pbr_match_val list[], | |
377 | int num) | |
378 | ||
379 | { | |
380 | int i; | |
381 | uint8_t unary_operator = OPERATOR_UNARY_AND; | |
382 | ||
383 | for (i = 0; i < num; i++) { | |
384 | if (i == 0) | |
385 | continue; | |
386 | if (list[i].unary_operator & OPERATOR_UNARY_OR) | |
387 | unary_operator = OPERATOR_UNARY_OR; | |
388 | if ((list[i].unary_operator & OPERATOR_UNARY_AND | |
389 | && unary_operator == OPERATOR_UNARY_OR) || | |
390 | (list[i].unary_operator & OPERATOR_UNARY_OR | |
391 | && unary_operator == OPERATOR_UNARY_AND)) | |
392 | return 0; | |
393 | } | |
394 | return unary_operator; | |
395 | } | |
396 | ||
397 | ||
1de7dfff PG |
398 | /* return true if extraction ok |
399 | */ | |
400 | static bool bgp_pbr_extract(struct bgp_pbr_match_val list[], | |
401 | int num, | |
402 | struct bgp_pbr_range_port *range) | |
403 | { | |
404 | int i = 0; | |
405 | bool exact_match = false; | |
406 | ||
407 | if (range) | |
408 | memset(range, 0, sizeof(struct bgp_pbr_range_port)); | |
409 | ||
410 | if (num > 2) | |
411 | return false; | |
412 | for (i = 0; i < num; i++) { | |
413 | if (i != 0 && (list[i].compare_operator == | |
414 | OPERATOR_COMPARE_EQUAL_TO)) | |
415 | return false; | |
416 | if (i == 0 && (list[i].compare_operator == | |
417 | OPERATOR_COMPARE_EQUAL_TO)) { | |
418 | if (range) | |
419 | range->min_port = list[i].value; | |
420 | exact_match = true; | |
421 | } | |
422 | if (exact_match == true && i > 0) | |
423 | return false; | |
424 | if (list[i].compare_operator == | |
425 | (OPERATOR_COMPARE_GREATER_THAN + | |
426 | OPERATOR_COMPARE_EQUAL_TO)) { | |
427 | if (range) | |
428 | range->min_port = list[i].value; | |
429 | } else if (list[i].compare_operator == | |
430 | (OPERATOR_COMPARE_LESS_THAN + | |
431 | OPERATOR_COMPARE_EQUAL_TO)) { | |
432 | if (range) | |
433 | range->max_port = list[i].value; | |
434 | } else if (list[i].compare_operator == | |
435 | OPERATOR_COMPARE_LESS_THAN) { | |
436 | if (range) | |
437 | range->max_port = list[i].value - 1; | |
438 | } else if (list[i].compare_operator == | |
439 | OPERATOR_COMPARE_GREATER_THAN) { | |
440 | if (range) | |
441 | range->min_port = list[i].value + 1; | |
442 | } | |
443 | } | |
444 | return true; | |
445 | } | |
446 | ||
b46b6f1a PG |
447 | static int bgp_pbr_validate_policy_route(struct bgp_pbr_entry_main *api) |
448 | { | |
932404b7 PG |
449 | bool enumerate_icmp = false; |
450 | ||
b46b6f1a PG |
451 | /* because bgp pbr entry may contain unsupported |
452 | * combinations, a message will be displayed here if | |
453 | * not supported. | |
454 | * for now, only match/set supported is | |
455 | * - combination src/dst => redirect nexthop [ + rate] | |
456 | * - combination src/dst => redirect VRF [ + rate] | |
457 | * - combination src/dst => drop | |
1de7dfff | 458 | * - combination srcport + @IP |
b46b6f1a | 459 | */ |
1de7dfff PG |
460 | if (api->match_protocol_num > 1) { |
461 | if (BGP_DEBUG(pbr, PBR)) | |
462 | zlog_debug("BGP: match protocol operations:" | |
463 | "multiple protocols ( %d). ignoring.", | |
464 | api->match_protocol_num); | |
465 | return 0; | |
466 | } | |
467 | if (api->match_protocol_num == 1 && | |
468 | api->protocol[0].value != PROTOCOL_UDP && | |
932404b7 | 469 | api->protocol[0].value != PROTOCOL_ICMP && |
1de7dfff PG |
470 | api->protocol[0].value != PROTOCOL_TCP) { |
471 | if (BGP_DEBUG(pbr, PBR)) | |
472 | zlog_debug("BGP: match protocol operations:" | |
473 | "protocol (%d) not supported. ignoring", | |
474 | api->match_protocol_num); | |
475 | return 0; | |
476 | } | |
477 | if (!bgp_pbr_extract(api->src_port, api->match_src_port_num, NULL)) { | |
478 | if (BGP_DEBUG(pbr, PBR)) | |
479 | zlog_debug("BGP: match src port operations:" | |
480 | "too complex. ignoring."); | |
481 | return 0; | |
482 | } | |
483 | if (!bgp_pbr_extract(api->dst_port, api->match_dst_port_num, NULL)) { | |
484 | if (BGP_DEBUG(pbr, PBR)) | |
485 | zlog_debug("BGP: match dst port operations:" | |
486 | "too complex. ignoring."); | |
487 | return 0; | |
488 | } | |
2da7d62e PG |
489 | if (!bgp_pbr_extract_enumerate(api->tcpflags, |
490 | api->match_tcpflags_num, | |
c5ee26cc | 491 | OPERATOR_UNARY_AND | |
35703998 PG |
492 | OPERATOR_UNARY_OR, NULL, |
493 | FLOWSPEC_TCP_FLAGS)) { | |
2da7d62e PG |
494 | if (BGP_DEBUG(pbr, PBR)) |
495 | zlog_debug("BGP: match tcp flags:" | |
496 | "too complex. ignoring."); | |
497 | return 0; | |
498 | } | |
932404b7 PG |
499 | if (!bgp_pbr_extract(api->icmp_type, api->match_icmp_type_num, NULL)) { |
500 | if (!bgp_pbr_extract_enumerate(api->icmp_type, | |
2da7d62e | 501 | api->match_icmp_type_num, |
35703998 PG |
502 | OPERATOR_UNARY_OR, NULL, |
503 | FLOWSPEC_ICMP_TYPE)) { | |
932404b7 PG |
504 | if (BGP_DEBUG(pbr, PBR)) |
505 | zlog_debug("BGP: match icmp type operations:" | |
506 | "too complex. ignoring."); | |
507 | return 0; | |
508 | } | |
509 | enumerate_icmp = true; | |
510 | } | |
511 | if (!bgp_pbr_extract(api->icmp_code, api->match_icmp_code_num, NULL)) { | |
512 | if (!bgp_pbr_extract_enumerate(api->icmp_code, | |
2da7d62e | 513 | api->match_icmp_code_num, |
35703998 PG |
514 | OPERATOR_UNARY_OR, NULL, |
515 | FLOWSPEC_ICMP_CODE)) { | |
932404b7 PG |
516 | if (BGP_DEBUG(pbr, PBR)) |
517 | zlog_debug("BGP: match icmp code operations:" | |
518 | "too complex. ignoring."); | |
519 | return 0; | |
520 | } else if (api->match_icmp_type_num > 1 && | |
521 | enumerate_icmp == false) { | |
522 | if (BGP_DEBUG(pbr, PBR)) | |
523 | zlog_debug("BGP: match icmp code is enumerate" | |
524 | ", and icmp type is not." | |
525 | " too complex. ignoring."); | |
526 | return 0; | |
527 | } | |
528 | } | |
1de7dfff PG |
529 | if (!bgp_pbr_extract(api->port, api->match_port_num, NULL)) { |
530 | if (BGP_DEBUG(pbr, PBR)) | |
531 | zlog_debug("BGP: match port operations:" | |
532 | "too complex. ignoring."); | |
533 | return 0; | |
534 | } | |
cfaf18ce PG |
535 | if (api->match_packet_length_num) { |
536 | bool ret; | |
537 | ||
538 | ret = bgp_pbr_extract(api->packet_length, | |
539 | api->match_packet_length_num, NULL); | |
540 | if (!ret) | |
541 | ret = bgp_pbr_extract_enumerate(api->packet_length, | |
542 | api->match_packet_length_num, | |
543 | OPERATOR_UNARY_OR | |
544 | | OPERATOR_UNARY_AND, | |
545 | NULL, FLOWSPEC_PKT_LEN); | |
546 | if (!ret) { | |
547 | if (BGP_DEBUG(pbr, PBR)) | |
548 | zlog_debug("BGP: match packet length operations:" | |
83360720 | 549 | "too complex. ignoring."); |
cfaf18ce PG |
550 | return 0; |
551 | } | |
83360720 | 552 | } |
35703998 PG |
553 | if (api->match_dscp_num) { |
554 | if (!bgp_pbr_extract_enumerate(api->dscp, api->match_dscp_num, | |
555 | OPERATOR_UNARY_OR | OPERATOR_UNARY_AND, | |
556 | NULL, FLOWSPEC_DSCP)) { | |
557 | if (BGP_DEBUG(pbr, PBR)) | |
558 | zlog_debug("BGP: match DSCP operations:" | |
559 | "too complex. ignoring."); | |
560 | return 0; | |
561 | } | |
4977bd6c | 562 | } |
6f5617d8 PG |
563 | if (api->match_fragment_num) { |
564 | char fail_str[64]; | |
565 | bool success; | |
566 | ||
567 | success = bgp_pbr_extract_enumerate(api->fragment, | |
568 | api->match_fragment_num, | |
569 | OPERATOR_UNARY_OR | |
570 | | OPERATOR_UNARY_AND, | |
571 | NULL, FLOWSPEC_FRAGMENT); | |
572 | if (success) { | |
573 | int i; | |
574 | ||
575 | for (i = 0; i < api->match_fragment_num; i++) { | |
576 | if (api->fragment[i].value != 1 && | |
577 | api->fragment[i].value != 2 && | |
578 | api->fragment[i].value != 4 && | |
579 | api->fragment[i].value != 8) { | |
580 | success = false; | |
581 | sprintf(fail_str, | |
582 | "Value not valid (%d) for this implementation", | |
583 | api->fragment[i].value); | |
584 | } | |
585 | } | |
586 | } else | |
587 | sprintf(fail_str, "too complex. ignoring"); | |
588 | if (!success) { | |
589 | if (BGP_DEBUG(pbr, PBR)) | |
590 | zlog_debug("BGP: match fragment operation (%d) %s", | |
591 | api->match_fragment_num, | |
592 | fail_str); | |
593 | return 0; | |
594 | } | |
595 | } | |
596 | ||
1de7dfff PG |
597 | /* no combinations with both src_port and dst_port |
598 | * or port with src_port and dst_port | |
599 | */ | |
600 | if (api->match_src_port_num + api->match_dst_port_num + | |
601 | api->match_port_num > 3) { | |
602 | if (BGP_DEBUG(pbr, PBR)) | |
603 | zlog_debug("BGP: match multiple port operations:" | |
604 | " too complex. ignoring."); | |
605 | return 0; | |
606 | } | |
932404b7 PG |
607 | if ((api->match_src_port_num || api->match_dst_port_num |
608 | || api->match_port_num) && (api->match_icmp_type_num | |
609 | || api->match_icmp_code_num)) { | |
610 | if (BGP_DEBUG(pbr, PBR)) | |
611 | zlog_debug("BGP: match multiple port/imcp operations:" | |
612 | " too complex. ignoring."); | |
613 | return 0; | |
614 | } | |
b46b6f1a PG |
615 | if (!(api->match_bitmask & PREFIX_SRC_PRESENT) && |
616 | !(api->match_bitmask & PREFIX_DST_PRESENT)) { | |
ac7c35f8 | 617 | if (BGP_DEBUG(pbr, PBR)) { |
b46b6f1a | 618 | bgp_pbr_print_policy_route(api); |
ac7c35f8 PG |
619 | zlog_debug("BGP: match actions without src" |
620 | " or dst address can not operate." | |
621 | " ignoring."); | |
622 | } | |
b46b6f1a PG |
623 | return 0; |
624 | } | |
625 | return 1; | |
626 | } | |
bbe6ffd6 | 627 | |
45918cfb PG |
628 | /* return -1 if build or validation failed */ |
629 | static int bgp_pbr_build_and_validate_entry(struct prefix *p, | |
630 | struct bgp_info *info, | |
631 | struct bgp_pbr_entry_main *api) | |
632 | { | |
633 | int ret; | |
634 | int i, action_count = 0; | |
635 | struct ecommunity *ecom; | |
636 | struct ecommunity_val *ecom_eval; | |
637 | struct bgp_pbr_entry_action *api_action; | |
638 | struct prefix *src = NULL, *dst = NULL; | |
639 | int valid_prefix = 0; | |
640 | afi_t afi = AFI_IP; | |
641 | ||
642 | /* extract match from flowspec entries */ | |
643 | ret = bgp_flowspec_match_rules_fill((uint8_t *)p->u.prefix_flowspec.ptr, | |
644 | p->u.prefix_flowspec.prefixlen, api); | |
645 | if (ret < 0) | |
646 | return -1; | |
647 | /* extract actiosn from flowspec ecom list */ | |
648 | if (info && info->attr && info->attr->ecommunity) { | |
649 | ecom = info->attr->ecommunity; | |
650 | for (i = 0; i < ecom->size; i++) { | |
651 | ecom_eval = (struct ecommunity_val *) | |
149d272b PG |
652 | (ecom->val + (i * ECOMMUNITY_SIZE)); |
653 | action_count++; | |
45918cfb | 654 | if (action_count > ACTIONS_MAX_NUM) { |
f146bb54 | 655 | if (BGP_DEBUG(pbr, PBR_ERROR)) |
1c50c1c0 QY |
656 | flog_err( |
657 | EC_BGP_FLOWSPEC_PACKET, | |
658 | "%s: flowspec actions exceeds limit (max %u)", | |
659 | __func__, action_count); | |
45918cfb PG |
660 | break; |
661 | } | |
149d272b | 662 | api_action = &api->actions[action_count - 1]; |
45918cfb PG |
663 | |
664 | if ((ecom_eval->val[1] == | |
665 | (char)ECOMMUNITY_REDIRECT_VRF) && | |
666 | (ecom_eval->val[0] == | |
667 | (char)ECOMMUNITY_ENCODE_TRANS_EXP || | |
668 | ecom_eval->val[0] == | |
669 | (char)ECOMMUNITY_EXTENDED_COMMUNITY_PART_2 || | |
670 | ecom_eval->val[0] == | |
671 | (char)ECOMMUNITY_EXTENDED_COMMUNITY_PART_3)) { | |
672 | struct ecommunity *eckey = ecommunity_new(); | |
673 | struct ecommunity_val ecom_copy; | |
674 | ||
675 | memcpy(&ecom_copy, ecom_eval, | |
676 | sizeof(struct ecommunity_val)); | |
677 | ecom_copy.val[0] &= | |
678 | ~ECOMMUNITY_ENCODE_TRANS_EXP; | |
679 | ecom_copy.val[1] = ECOMMUNITY_ROUTE_TARGET; | |
680 | ecommunity_add_val(eckey, &ecom_copy); | |
681 | ||
682 | api_action->action = ACTION_REDIRECT; | |
683 | api_action->u.redirect_vrf = | |
684 | get_first_vrf_for_redirect_with_rt( | |
685 | eckey); | |
686 | ecommunity_free(&eckey); | |
687 | } else if ((ecom_eval->val[0] == | |
688 | (char)ECOMMUNITY_ENCODE_REDIRECT_IP_NH) && | |
689 | (ecom_eval->val[1] == | |
690 | (char)ECOMMUNITY_REDIRECT_IP_NH)) { | |
691 | api_action->action = ACTION_REDIRECT_IP; | |
692 | api_action->u.zr.redirect_ip_v4.s_addr = | |
693 | info->attr->nexthop.s_addr; | |
694 | api_action->u.zr.duplicate = ecom_eval->val[7]; | |
695 | } else { | |
696 | if (ecom_eval->val[0] != | |
697 | (char)ECOMMUNITY_ENCODE_TRANS_EXP) | |
698 | continue; | |
699 | ret = ecommunity_fill_pbr_action(ecom_eval, | |
700 | api_action); | |
701 | if (ret != 0) | |
702 | continue; | |
703 | } | |
704 | api->action_num++; | |
705 | } | |
706 | } | |
707 | ||
708 | /* validate if incoming matc/action is compatible | |
709 | * with our policy routing engine | |
710 | */ | |
711 | if (!bgp_pbr_validate_policy_route(api)) | |
712 | return -1; | |
713 | ||
714 | /* check inconsistency in the match rule */ | |
715 | if (api->match_bitmask & PREFIX_SRC_PRESENT) { | |
716 | src = &api->src_prefix; | |
717 | afi = family2afi(src->family); | |
718 | valid_prefix = 1; | |
719 | } | |
720 | if (api->match_bitmask & PREFIX_DST_PRESENT) { | |
721 | dst = &api->dst_prefix; | |
722 | if (valid_prefix && afi != family2afi(dst->family)) { | |
ac7c35f8 | 723 | if (BGP_DEBUG(pbr, PBR)) { |
45918cfb | 724 | bgp_pbr_print_policy_route(api); |
ac7c35f8 PG |
725 | zlog_debug("%s: inconsistency:" |
726 | " no match for afi src and dst (%u/%u)", | |
727 | __func__, afi, family2afi(dst->family)); | |
728 | } | |
45918cfb PG |
729 | return -1; |
730 | } | |
731 | } | |
732 | return 0; | |
733 | } | |
734 | ||
a6b07429 PG |
735 | static void bgp_pbr_match_entry_free(void *arg) |
736 | { | |
737 | struct bgp_pbr_match_entry *bpme; | |
738 | ||
739 | bpme = (struct bgp_pbr_match_entry *)arg; | |
740 | ||
741 | if (bpme->installed) { | |
742 | bgp_send_pbr_ipset_entry_match(bpme, false); | |
743 | bpme->installed = false; | |
744 | bpme->backpointer = NULL; | |
745 | } | |
746 | XFREE(MTYPE_PBR_MATCH_ENTRY, bpme); | |
747 | } | |
748 | ||
749 | static void bgp_pbr_match_free(void *arg) | |
750 | { | |
751 | struct bgp_pbr_match *bpm; | |
752 | ||
753 | bpm = (struct bgp_pbr_match *)arg; | |
754 | ||
755 | hash_clean(bpm->entry_hash, bgp_pbr_match_entry_free); | |
756 | ||
757 | if (hashcount(bpm->entry_hash) == 0) { | |
758 | /* delete iptable entry first */ | |
759 | /* then delete ipset match */ | |
760 | if (bpm->installed) { | |
761 | if (bpm->installed_in_iptable) { | |
762 | bgp_send_pbr_iptable(bpm->action, | |
763 | bpm, false); | |
764 | bpm->installed_in_iptable = false; | |
765 | bpm->action->refcnt--; | |
766 | } | |
767 | bgp_send_pbr_ipset_match(bpm, false); | |
768 | bpm->installed = false; | |
769 | bpm->action = NULL; | |
770 | } | |
771 | } | |
772 | hash_free(bpm->entry_hash); | |
773 | ||
774 | XFREE(MTYPE_PBR_MATCH, bpm); | |
775 | } | |
776 | ||
d114b0d7 PG |
777 | static void *bgp_pbr_match_alloc_intern(void *arg) |
778 | { | |
779 | struct bgp_pbr_match *bpm, *new; | |
780 | ||
781 | bpm = (struct bgp_pbr_match *)arg; | |
782 | ||
783 | new = XCALLOC(MTYPE_PBR_MATCH, sizeof(*new)); | |
784 | memcpy(new, bpm, sizeof(*bpm)); | |
785 | ||
786 | return new; | |
787 | } | |
788 | ||
a6b07429 PG |
789 | static void bgp_pbr_action_free(void *arg) |
790 | { | |
791 | struct bgp_pbr_action *bpa; | |
792 | ||
793 | bpa = (struct bgp_pbr_action *)arg; | |
794 | ||
795 | if (bpa->refcnt == 0) { | |
796 | if (bpa->installed && bpa->table_id != 0) { | |
797 | bgp_send_pbr_rule_action(bpa, false); | |
798 | bgp_zebra_announce_default(bpa->bgp, &(bpa->nh), | |
799 | AFI_IP, | |
800 | bpa->table_id, | |
801 | false); | |
6ee20355 | 802 | bpa->installed = false; |
a6b07429 PG |
803 | } |
804 | } | |
805 | XFREE(MTYPE_PBR_ACTION, bpa); | |
806 | } | |
807 | ||
d114b0d7 PG |
808 | static void *bgp_pbr_action_alloc_intern(void *arg) |
809 | { | |
810 | struct bgp_pbr_action *bpa, *new; | |
811 | ||
812 | bpa = (struct bgp_pbr_action *)arg; | |
813 | ||
814 | new = XCALLOC(MTYPE_PBR_ACTION, sizeof(*new)); | |
815 | ||
816 | memcpy(new, bpa, sizeof(*bpa)); | |
817 | ||
818 | return new; | |
819 | } | |
820 | ||
821 | static void *bgp_pbr_match_entry_alloc_intern(void *arg) | |
822 | { | |
823 | struct bgp_pbr_match_entry *bpme, *new; | |
824 | ||
825 | bpme = (struct bgp_pbr_match_entry *)arg; | |
826 | ||
827 | new = XCALLOC(MTYPE_PBR_MATCH_ENTRY, sizeof(*new)); | |
828 | ||
829 | memcpy(new, bpme, sizeof(*bpme)); | |
830 | ||
831 | return new; | |
832 | } | |
833 | ||
f3d32faa PG |
834 | uint32_t bgp_pbr_match_hash_key(void *arg) |
835 | { | |
836 | struct bgp_pbr_match *pbm = (struct bgp_pbr_match *)arg; | |
837 | uint32_t key; | |
838 | ||
839 | key = jhash_1word(pbm->vrf_id, 0x4312abde); | |
840 | key = jhash_1word(pbm->flags, key); | |
9717d3e5 PG |
841 | key = jhash(&pbm->pkt_len_min, 2, key); |
842 | key = jhash(&pbm->pkt_len_max, 2, key); | |
843 | key = jhash(&pbm->tcp_flags, 2, key); | |
844 | key = jhash(&pbm->tcp_mask_flags, 2, key); | |
845 | key = jhash(&pbm->dscp_value, 1, key); | |
846 | key = jhash(&pbm->fragment, 1, key); | |
f3d32faa PG |
847 | return jhash_1word(pbm->type, key); |
848 | } | |
849 | ||
850 | int bgp_pbr_match_hash_equal(const void *arg1, const void *arg2) | |
851 | { | |
852 | const struct bgp_pbr_match *r1, *r2; | |
853 | ||
854 | r1 = (const struct bgp_pbr_match *)arg1; | |
855 | r2 = (const struct bgp_pbr_match *)arg2; | |
856 | ||
857 | if (r1->vrf_id != r2->vrf_id) | |
858 | return 0; | |
859 | ||
860 | if (r1->type != r2->type) | |
861 | return 0; | |
862 | ||
863 | if (r1->flags != r2->flags) | |
864 | return 0; | |
865 | ||
866 | if (r1->action != r2->action) | |
867 | return 0; | |
868 | ||
83360720 PG |
869 | if (r1->pkt_len_min != r2->pkt_len_min) |
870 | return 0; | |
871 | ||
872 | if (r1->pkt_len_max != r2->pkt_len_max) | |
873 | return 0; | |
874 | ||
2da7d62e PG |
875 | if (r1->tcp_flags != r2->tcp_flags) |
876 | return 0; | |
877 | ||
878 | if (r1->tcp_mask_flags != r2->tcp_mask_flags) | |
879 | return 0; | |
880 | ||
4977bd6c PG |
881 | if (r1->dscp_value != r2->dscp_value) |
882 | return 0; | |
6f5617d8 PG |
883 | |
884 | if (r1->fragment != r2->fragment) | |
885 | return 0; | |
f3d32faa PG |
886 | return 1; |
887 | } | |
888 | ||
889 | uint32_t bgp_pbr_match_entry_hash_key(void *arg) | |
890 | { | |
891 | struct bgp_pbr_match_entry *pbme; | |
892 | uint32_t key; | |
893 | ||
894 | pbme = (struct bgp_pbr_match_entry *)arg; | |
895 | key = prefix_hash_key(&pbme->src); | |
896 | key = jhash_1word(prefix_hash_key(&pbme->dst), key); | |
1de7dfff PG |
897 | key = jhash(&pbme->dst_port_min, 2, key); |
898 | key = jhash(&pbme->src_port_min, 2, key); | |
899 | key = jhash(&pbme->dst_port_max, 2, key); | |
900 | key = jhash(&pbme->src_port_max, 2, key); | |
901 | key = jhash(&pbme->proto, 1, key); | |
f3d32faa PG |
902 | |
903 | return key; | |
904 | } | |
905 | ||
906 | int bgp_pbr_match_entry_hash_equal(const void *arg1, const void *arg2) | |
907 | { | |
908 | const struct bgp_pbr_match_entry *r1, *r2; | |
909 | ||
910 | r1 = (const struct bgp_pbr_match_entry *)arg1; | |
911 | r2 = (const struct bgp_pbr_match_entry *)arg2; | |
912 | ||
913 | /* on updates, comparing | |
914 | * backpointer is not necessary | |
915 | */ | |
916 | ||
917 | /* unique value is self calculated | |
918 | */ | |
919 | ||
920 | /* rate is ignored for now | |
921 | */ | |
922 | ||
923 | if (!prefix_same(&r1->src, &r2->src)) | |
924 | return 0; | |
925 | ||
926 | if (!prefix_same(&r1->dst, &r2->dst)) | |
927 | return 0; | |
928 | ||
1de7dfff PG |
929 | if (r1->src_port_min != r2->src_port_min) |
930 | return 0; | |
931 | ||
932 | if (r1->dst_port_min != r2->dst_port_min) | |
933 | return 0; | |
934 | ||
935 | if (r1->src_port_max != r2->src_port_max) | |
936 | return 0; | |
937 | ||
938 | if (r1->dst_port_max != r2->dst_port_max) | |
939 | return 0; | |
940 | ||
941 | if (r1->proto != r2->proto) | |
942 | return 0; | |
943 | ||
f3d32faa PG |
944 | return 1; |
945 | } | |
946 | ||
947 | uint32_t bgp_pbr_action_hash_key(void *arg) | |
948 | { | |
949 | struct bgp_pbr_action *pbra; | |
950 | uint32_t key; | |
951 | ||
952 | pbra = (struct bgp_pbr_action *)arg; | |
953 | key = jhash_1word(pbra->table_id, 0x4312abde); | |
954 | key = jhash_1word(pbra->fwmark, key); | |
955 | return key; | |
956 | } | |
957 | ||
958 | int bgp_pbr_action_hash_equal(const void *arg1, const void *arg2) | |
959 | { | |
960 | const struct bgp_pbr_action *r1, *r2; | |
961 | ||
962 | r1 = (const struct bgp_pbr_action *)arg1; | |
963 | r2 = (const struct bgp_pbr_action *)arg2; | |
964 | ||
965 | /* unique value is self calculated | |
966 | * table and fwmark is self calculated | |
e414819e | 967 | * rate is ignored |
f3d32faa | 968 | */ |
f3d32faa PG |
969 | if (r1->vrf_id != r2->vrf_id) |
970 | return 0; | |
971 | ||
972 | if (memcmp(&r1->nh, &r2->nh, sizeof(struct nexthop))) | |
973 | return 0; | |
974 | return 1; | |
975 | } | |
bbe6ffd6 | 976 | |
70eabd12 PG |
977 | struct bgp_pbr_action *bgp_pbr_action_rule_lookup(vrf_id_t vrf_id, |
978 | uint32_t unique) | |
bbe6ffd6 | 979 | { |
70eabd12 PG |
980 | struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id); |
981 | struct bgp_pbr_action_unique bpau; | |
982 | ||
983 | if (!bgp || unique == 0) | |
984 | return NULL; | |
985 | bpau.unique = unique; | |
986 | bpau.bpa_found = NULL; | |
987 | hash_walk(bgp->pbr_action_hash, bgp_pbr_action_walkcb, &bpau); | |
988 | return bpau.bpa_found; | |
bbe6ffd6 PG |
989 | } |
990 | ||
991 | struct bgp_pbr_match *bgp_pbr_match_ipset_lookup(vrf_id_t vrf_id, | |
992 | uint32_t unique) | |
993 | { | |
c5d429e1 PG |
994 | struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id); |
995 | struct bgp_pbr_match_unique bpmu; | |
996 | ||
997 | if (!bgp || unique == 0) | |
998 | return NULL; | |
999 | bpmu.unique = unique; | |
1000 | bpmu.bpm_found = NULL; | |
1001 | hash_walk(bgp->pbr_match_hash, bgp_pbr_match_walkcb, &bpmu); | |
1002 | return bpmu.bpm_found; | |
bbe6ffd6 PG |
1003 | } |
1004 | ||
1005 | struct bgp_pbr_match_entry *bgp_pbr_match_ipset_entry_lookup(vrf_id_t vrf_id, | |
1006 | char *ipset_name, | |
1007 | uint32_t unique) | |
1008 | { | |
c5d429e1 PG |
1009 | struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id); |
1010 | struct bgp_pbr_match_entry_unique bpmeu; | |
1011 | struct bgp_pbr_match_ipsetname bpmi; | |
1012 | ||
1013 | if (!bgp || unique == 0) | |
1014 | return NULL; | |
1015 | bpmi.ipsetname = XCALLOC(MTYPE_TMP, ZEBRA_IPSET_NAME_SIZE); | |
1016 | snprintf(bpmi.ipsetname, ZEBRA_IPSET_NAME_SIZE, "%s", ipset_name); | |
1017 | bpmi.bpm_found = NULL; | |
1018 | hash_walk(bgp->pbr_match_hash, bgp_pbr_match_pername_walkcb, &bpmi); | |
1019 | XFREE(MTYPE_TMP, bpmi.ipsetname); | |
1020 | if (!bpmi.bpm_found) | |
1021 | return NULL; | |
1022 | bpmeu.bpme_found = NULL; | |
1023 | bpmeu.unique = unique; | |
1024 | hash_walk(bpmi.bpm_found->entry_hash, | |
1025 | bgp_pbr_match_entry_walkcb, &bpmeu); | |
1026 | return bpmeu.bpme_found; | |
bbe6ffd6 PG |
1027 | } |
1028 | ||
1815c6fc PG |
1029 | struct bgp_pbr_match *bgp_pbr_match_iptable_lookup(vrf_id_t vrf_id, |
1030 | uint32_t unique) | |
1031 | { | |
1032 | struct bgp *bgp = bgp_lookup_by_vrf_id(vrf_id); | |
1033 | struct bgp_pbr_match_iptable_unique bpmiu; | |
1034 | ||
1035 | if (!bgp || unique == 0) | |
1036 | return NULL; | |
1037 | bpmiu.unique = unique; | |
1038 | bpmiu.bpm_found = NULL; | |
1039 | hash_walk(bgp->pbr_match_hash, bgp_pbr_match_iptable_walkcb, &bpmiu); | |
1040 | return bpmiu.bpm_found; | |
1041 | } | |
1042 | ||
a6b07429 PG |
1043 | void bgp_pbr_cleanup(struct bgp *bgp) |
1044 | { | |
1045 | if (bgp->pbr_match_hash) { | |
1046 | hash_clean(bgp->pbr_match_hash, bgp_pbr_match_free); | |
1047 | hash_free(bgp->pbr_match_hash); | |
1048 | bgp->pbr_match_hash = NULL; | |
1049 | } | |
1050 | if (bgp->pbr_action_hash) { | |
1051 | hash_clean(bgp->pbr_action_hash, bgp_pbr_action_free); | |
1052 | hash_free(bgp->pbr_action_hash); | |
1053 | bgp->pbr_action_hash = NULL; | |
1054 | } | |
4762c213 PG |
1055 | if (bgp->bgp_pbr_cfg == NULL) |
1056 | return; | |
1057 | bgp_pbr_reset(bgp, AFI_IP); | |
1058 | XFREE(MTYPE_PBR, bgp->bgp_pbr_cfg); | |
1059 | bgp->bgp_pbr_cfg = NULL; | |
a6b07429 PG |
1060 | } |
1061 | ||
f3d32faa PG |
1062 | void bgp_pbr_init(struct bgp *bgp) |
1063 | { | |
1064 | bgp->pbr_match_hash = | |
1065 | hash_create_size(8, bgp_pbr_match_hash_key, | |
1066 | bgp_pbr_match_hash_equal, | |
1067 | "Match Hash"); | |
1068 | bgp->pbr_action_hash = | |
1069 | hash_create_size(8, bgp_pbr_action_hash_key, | |
1070 | bgp_pbr_action_hash_equal, | |
1071 | "Match Hash Entry"); | |
4762c213 PG |
1072 | |
1073 | bgp->bgp_pbr_cfg = XCALLOC(MTYPE_PBR, sizeof(struct bgp_pbr_config)); | |
1074 | bgp->bgp_pbr_cfg->pbr_interface_any_ipv4 = true; | |
f3d32faa | 1075 | } |
b46b6f1a PG |
1076 | |
1077 | void bgp_pbr_print_policy_route(struct bgp_pbr_entry_main *api) | |
1078 | { | |
1079 | int i = 0; | |
1080 | char return_string[512]; | |
1081 | char *ptr = return_string; | |
1082 | char buff[64]; | |
1083 | int nb_items = 0; | |
1084 | ||
1085 | ptr += sprintf(ptr, "MATCH : "); | |
1086 | if (api->match_bitmask & PREFIX_SRC_PRESENT) { | |
1087 | struct prefix *p = &(api->src_prefix); | |
1088 | ||
1089 | ptr += sprintf(ptr, "@src %s", prefix2str(p, buff, 64)); | |
1090 | INCREMENT_DISPLAY(ptr, nb_items); | |
1091 | } | |
1092 | if (api->match_bitmask & PREFIX_DST_PRESENT) { | |
1093 | struct prefix *p = &(api->dst_prefix); | |
1094 | ||
1095 | INCREMENT_DISPLAY(ptr, nb_items); | |
1096 | ptr += sprintf(ptr, "@dst %s", prefix2str(p, buff, 64)); | |
1097 | } | |
1098 | ||
1099 | if (api->match_protocol_num) | |
1100 | INCREMENT_DISPLAY(ptr, nb_items); | |
1101 | for (i = 0; i < api->match_protocol_num; i++) | |
1102 | ptr += sprintf_bgp_pbr_match_val(ptr, &api->protocol[i], | |
1103 | i > 0 ? NULL : "@proto "); | |
1104 | ||
1105 | if (api->match_src_port_num) | |
1106 | INCREMENT_DISPLAY(ptr, nb_items); | |
1107 | for (i = 0; i < api->match_src_port_num; i++) | |
1108 | ptr += sprintf_bgp_pbr_match_val(ptr, &api->src_port[i], | |
1109 | i > 0 ? NULL : "@srcport "); | |
1110 | ||
1111 | if (api->match_dst_port_num) | |
1112 | INCREMENT_DISPLAY(ptr, nb_items); | |
1113 | for (i = 0; i < api->match_dst_port_num; i++) | |
1114 | ptr += sprintf_bgp_pbr_match_val(ptr, &api->dst_port[i], | |
1115 | i > 0 ? NULL : "@dstport "); | |
1116 | ||
1117 | if (api->match_port_num) | |
1118 | INCREMENT_DISPLAY(ptr, nb_items); | |
1119 | for (i = 0; i < api->match_port_num; i++) | |
1120 | ptr += sprintf_bgp_pbr_match_val(ptr, &api->port[i], | |
1121 | i > 0 ? NULL : "@port "); | |
1122 | ||
1123 | if (api->match_icmp_type_num) | |
1124 | INCREMENT_DISPLAY(ptr, nb_items); | |
1125 | for (i = 0; i < api->match_icmp_type_num; i++) | |
1126 | ptr += sprintf_bgp_pbr_match_val(ptr, &api->icmp_type[i], | |
1127 | i > 0 ? NULL : "@icmptype "); | |
1128 | ||
1129 | if (api->match_icmp_code_num) | |
1130 | INCREMENT_DISPLAY(ptr, nb_items); | |
1131 | for (i = 0; i < api->match_icmp_code_num; i++) | |
1132 | ptr += sprintf_bgp_pbr_match_val(ptr, &api->icmp_code[i], | |
1133 | i > 0 ? NULL : "@icmpcode "); | |
1134 | ||
1135 | if (api->match_packet_length_num) | |
1136 | INCREMENT_DISPLAY(ptr, nb_items); | |
1137 | for (i = 0; i < api->match_packet_length_num; i++) | |
1138 | ptr += sprintf_bgp_pbr_match_val(ptr, &api->packet_length[i], | |
1139 | i > 0 ? NULL : "@plen "); | |
1140 | ||
1141 | if (api->match_dscp_num) | |
1142 | INCREMENT_DISPLAY(ptr, nb_items); | |
1143 | for (i = 0; i < api->match_dscp_num; i++) | |
1144 | ptr += sprintf_bgp_pbr_match_val(ptr, &api->dscp[i], | |
1145 | i > 0 ? NULL : "@dscp "); | |
1146 | ||
1147 | if (api->match_tcpflags_num) | |
1148 | INCREMENT_DISPLAY(ptr, nb_items); | |
1149 | for (i = 0; i < api->match_tcpflags_num; i++) | |
1150 | ptr += sprintf_bgp_pbr_match_val(ptr, &api->tcpflags[i], | |
1151 | i > 0 ? NULL : "@tcpflags "); | |
1152 | ||
588ec356 | 1153 | if (api->match_fragment_num) |
b46b6f1a | 1154 | INCREMENT_DISPLAY(ptr, nb_items); |
588ec356 PG |
1155 | for (i = 0; i < api->match_fragment_num; i++) |
1156 | ptr += sprintf_bgp_pbr_match_val(ptr, &api->fragment[i], | |
1157 | i > 0 ? NULL : "@fragment "); | |
b46b6f1a PG |
1158 | if (!nb_items) |
1159 | ptr = return_string; | |
1160 | else | |
1161 | ptr += sprintf(ptr, "; "); | |
1162 | if (api->action_num) | |
1163 | ptr += sprintf(ptr, "SET : "); | |
1164 | nb_items = 0; | |
1165 | for (i = 0; i < api->action_num; i++) { | |
1166 | switch (api->actions[i].action) { | |
1167 | case ACTION_TRAFFICRATE: | |
1168 | INCREMENT_DISPLAY(ptr, nb_items); | |
1169 | ptr += sprintf(ptr, "@set rate %f", | |
1170 | api->actions[i].u.r.rate); | |
1171 | break; | |
1172 | case ACTION_TRAFFIC_ACTION: | |
1173 | INCREMENT_DISPLAY(ptr, nb_items); | |
1174 | ptr += sprintf(ptr, "@action "); | |
1175 | if (api->actions[i].u.za.filter | |
1176 | & TRAFFIC_ACTION_TERMINATE) | |
1177 | ptr += sprintf(ptr, | |
1178 | " terminate (apply filter(s))"); | |
1179 | if (api->actions[i].u.za.filter | |
1180 | & TRAFFIC_ACTION_DISTRIBUTE) | |
1181 | ptr += sprintf(ptr, " distribute"); | |
1182 | if (api->actions[i].u.za.filter | |
1183 | & TRAFFIC_ACTION_SAMPLE) | |
1184 | ptr += sprintf(ptr, " sample"); | |
1185 | break; | |
1186 | case ACTION_REDIRECT_IP: | |
1187 | INCREMENT_DISPLAY(ptr, nb_items); | |
1188 | char local_buff[INET_ADDRSTRLEN]; | |
1189 | ||
1190 | if (inet_ntop(AF_INET, | |
1191 | &api->actions[i].u.zr.redirect_ip_v4, | |
1192 | local_buff, INET_ADDRSTRLEN) != NULL) | |
1193 | ptr += sprintf(ptr, | |
1194 | "@redirect ip nh %s", local_buff); | |
1195 | break; | |
1196 | case ACTION_REDIRECT: | |
1197 | INCREMENT_DISPLAY(ptr, nb_items); | |
1198 | ptr += sprintf(ptr, "@redirect vrf %u", | |
1199 | api->actions[i].u.redirect_vrf); | |
1200 | break; | |
1201 | case ACTION_MARKING: | |
1202 | INCREMENT_DISPLAY(ptr, nb_items); | |
1203 | ptr += sprintf(ptr, "@set dscp %u", | |
1204 | api->actions[i].u.marking_dscp); | |
1205 | break; | |
1206 | default: | |
1207 | break; | |
1208 | } | |
1209 | } | |
1210 | zlog_info("%s", return_string); | |
1211 | } | |
45918cfb | 1212 | |
d114b0d7 PG |
1213 | static void bgp_pbr_flush_entry(struct bgp *bgp, struct bgp_pbr_action *bpa, |
1214 | struct bgp_pbr_match *bpm, | |
1215 | struct bgp_pbr_match_entry *bpme) | |
1216 | { | |
1217 | /* if bpme is null, bpm is also null | |
1218 | */ | |
1219 | if (bpme == NULL) | |
1220 | return; | |
1221 | /* ipset del entry */ | |
1222 | if (bpme->installed) { | |
1223 | bgp_send_pbr_ipset_entry_match(bpme, false); | |
1224 | bpme->installed = false; | |
1225 | bpme->backpointer = NULL; | |
b588b642 PG |
1226 | if (bpme->bgp_info) { |
1227 | struct bgp_info *bgp_info; | |
1228 | struct bgp_info_extra *extra; | |
1229 | ||
1230 | /* unlink bgp_info to bpme */ | |
1231 | bgp_info = (struct bgp_info *)bpme->bgp_info; | |
1232 | extra = bgp_info_extra_get(bgp_info); | |
c26edcda PG |
1233 | if (extra->bgp_fs_pbr) |
1234 | listnode_delete(extra->bgp_fs_pbr, bpme); | |
b588b642 PG |
1235 | bpme->bgp_info = NULL; |
1236 | } | |
d114b0d7 PG |
1237 | } |
1238 | hash_release(bpm->entry_hash, bpme); | |
1239 | if (hashcount(bpm->entry_hash) == 0) { | |
1240 | /* delete iptable entry first */ | |
1241 | /* then delete ipset match */ | |
1242 | if (bpm->installed) { | |
1243 | if (bpm->installed_in_iptable) { | |
1244 | bgp_send_pbr_iptable(bpm->action, | |
1245 | bpm, false); | |
1246 | bpm->installed_in_iptable = false; | |
a6b07429 | 1247 | bpm->action->refcnt--; |
d114b0d7 PG |
1248 | } |
1249 | bgp_send_pbr_ipset_match(bpm, false); | |
1250 | bpm->installed = false; | |
1251 | bpm->action = NULL; | |
1252 | } | |
1253 | hash_release(bgp->pbr_match_hash, bpm); | |
1254 | /* XXX release pbr_match_action if not used | |
1255 | * note that drop does not need to call send_pbr_action | |
1256 | */ | |
1257 | } | |
a6b07429 PG |
1258 | if (bpa->refcnt == 0) { |
1259 | if (bpa->installed && bpa->table_id != 0) { | |
1260 | bgp_send_pbr_rule_action(bpa, false); | |
1261 | bgp_zebra_announce_default(bpa->bgp, &(bpa->nh), | |
1262 | AFI_IP, | |
1263 | bpa->table_id, | |
1264 | false); | |
6ee20355 | 1265 | bpa->installed = false; |
a6b07429 PG |
1266 | } |
1267 | } | |
d114b0d7 PG |
1268 | } |
1269 | ||
1270 | struct bgp_pbr_match_entry_remain { | |
1271 | struct bgp_pbr_match_entry *bpme_to_match; | |
1272 | struct bgp_pbr_match_entry *bpme_found; | |
1273 | }; | |
1274 | ||
1275 | static int bgp_pbr_get_remaining_entry(struct hash_backet *backet, void *arg) | |
1276 | { | |
1277 | struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)backet->data; | |
1278 | struct bgp_pbr_match_entry_remain *bpmer = | |
1279 | (struct bgp_pbr_match_entry_remain *)arg; | |
1280 | struct bgp_pbr_match *bpm_temp; | |
1281 | struct bgp_pbr_match_entry *bpme = bpmer->bpme_to_match; | |
1282 | ||
1283 | if (!bpme->backpointer || | |
1284 | bpm == bpme->backpointer || | |
1285 | bpme->backpointer->action == bpm->action) | |
1286 | return HASHWALK_CONTINUE; | |
1287 | /* ensure bpm other characteristics are equal */ | |
1288 | bpm_temp = bpme->backpointer; | |
1289 | if (bpm_temp->vrf_id != bpm->vrf_id || | |
1290 | bpm_temp->type != bpm->type || | |
836b6953 PG |
1291 | bpm_temp->flags != bpm->flags || |
1292 | bpm_temp->tcp_flags != bpm->tcp_flags || | |
1293 | bpm_temp->tcp_mask_flags != bpm->tcp_mask_flags || | |
1294 | bpm_temp->pkt_len_min != bpm->pkt_len_min || | |
1295 | bpm_temp->pkt_len_max != bpm->pkt_len_max || | |
1296 | bpm_temp->dscp_value != bpm->dscp_value || | |
1297 | bpm_temp->fragment != bpm->fragment) | |
d114b0d7 PG |
1298 | return HASHWALK_CONTINUE; |
1299 | ||
1300 | /* look for remaining bpme */ | |
1301 | bpmer->bpme_found = hash_lookup(bpm->entry_hash, bpme); | |
1302 | if (!bpmer->bpme_found) | |
1303 | return HASHWALK_CONTINUE; | |
1304 | return HASHWALK_ABORT; | |
1305 | } | |
1306 | ||
9bba1455 | 1307 | static void bgp_pbr_policyroute_remove_from_zebra_unit(struct bgp *bgp, |
0e867886 PG |
1308 | struct bgp_info *binfo, |
1309 | struct bgp_pbr_filter *bpf) | |
d114b0d7 PG |
1310 | { |
1311 | struct bgp_pbr_match temp; | |
1312 | struct bgp_pbr_match_entry temp2; | |
1313 | struct bgp_pbr_match *bpm; | |
1314 | struct bgp_pbr_match_entry *bpme; | |
1315 | struct bgp_pbr_match_entry_remain bpmer; | |
0e867886 PG |
1316 | struct bgp_pbr_range_port *src_port; |
1317 | struct bgp_pbr_range_port *dst_port; | |
1318 | struct bgp_pbr_range_port *pkt_len; | |
1319 | ||
1320 | if (!bpf) | |
1321 | return; | |
1322 | src_port = bpf->src_port; | |
1323 | dst_port = bpf->dst_port; | |
1324 | pkt_len = bpf->pkt_len; | |
d114b0d7 | 1325 | |
064a9d52 PG |
1326 | if (BGP_DEBUG(zebra, ZEBRA)) |
1327 | bgp_pbr_dump_entry(bpf, false); | |
1328 | ||
d114b0d7 PG |
1329 | /* as we don't know information from EC |
1330 | * look for bpm that have the bpm | |
1331 | * with vrf_id characteristics | |
1332 | */ | |
1333 | memset(&temp2, 0, sizeof(temp2)); | |
1334 | memset(&temp, 0, sizeof(temp)); | |
0e867886 | 1335 | if (bpf->src) { |
d114b0d7 | 1336 | temp.flags |= MATCH_IP_SRC_SET; |
0e867886 | 1337 | prefix_copy(&temp2.src, bpf->src); |
d114b0d7 PG |
1338 | } else |
1339 | temp2.src.family = AF_INET; | |
0e867886 | 1340 | if (bpf->dst) { |
d114b0d7 | 1341 | temp.flags |= MATCH_IP_DST_SET; |
0e867886 | 1342 | prefix_copy(&temp2.dst, bpf->dst); |
d114b0d7 PG |
1343 | } else |
1344 | temp2.dst.family = AF_INET; | |
3bed2363 PG |
1345 | if (src_port && (src_port->min_port || bpf->protocol == IPPROTO_ICMP)) { |
1346 | if (bpf->protocol == IPPROTO_ICMP) | |
1347 | temp.flags |= MATCH_ICMP_SET; | |
1de7dfff PG |
1348 | temp.flags |= MATCH_PORT_SRC_SET; |
1349 | temp2.src_port_min = src_port->min_port; | |
1350 | if (src_port->max_port) { | |
1351 | temp.flags |= MATCH_PORT_SRC_RANGE_SET; | |
1352 | temp2.src_port_max = src_port->max_port; | |
1353 | } | |
1354 | } | |
3bed2363 PG |
1355 | if (dst_port && (dst_port->min_port || bpf->protocol == IPPROTO_ICMP)) { |
1356 | if (bpf->protocol == IPPROTO_ICMP) | |
1357 | temp.flags |= MATCH_ICMP_SET; | |
1de7dfff PG |
1358 | temp.flags |= MATCH_PORT_DST_SET; |
1359 | temp2.dst_port_min = dst_port->min_port; | |
1360 | if (dst_port->max_port) { | |
1361 | temp.flags |= MATCH_PORT_DST_RANGE_SET; | |
1362 | temp2.dst_port_max = dst_port->max_port; | |
1363 | } | |
1364 | } | |
0e867886 | 1365 | temp2.proto = bpf->protocol; |
1de7dfff | 1366 | |
cfaf18ce | 1367 | if (pkt_len) { |
83360720 | 1368 | temp.pkt_len_min = pkt_len->min_port; |
cfaf18ce PG |
1369 | if (pkt_len->max_port) |
1370 | temp.pkt_len_max = pkt_len->max_port; | |
1371 | } else if (bpf->pkt_len_val) { | |
1372 | if (bpf->pkt_len_val->mask) | |
1373 | temp.flags |= MATCH_PKT_LEN_INVERSE_SET; | |
1374 | temp.pkt_len_min = bpf->pkt_len_val->val; | |
1375 | } | |
2da7d62e PG |
1376 | if (bpf->tcp_flags) { |
1377 | temp.tcp_flags = bpf->tcp_flags->val; | |
1378 | temp.tcp_mask_flags = bpf->tcp_flags->mask; | |
1379 | } | |
4977bd6c PG |
1380 | if (bpf->dscp) { |
1381 | if (bpf->dscp->mask) | |
1382 | temp.flags |= MATCH_DSCP_INVERSE_SET; | |
1383 | else | |
1384 | temp.flags |= MATCH_DSCP_SET; | |
1385 | temp.dscp_value = bpf->dscp->val; | |
1386 | } | |
6f5617d8 PG |
1387 | if (bpf->fragment) { |
1388 | if (bpf->fragment->mask) | |
1389 | temp.flags |= MATCH_FRAGMENT_INVERSE_SET; | |
1390 | temp.fragment = bpf->fragment->val; | |
1391 | } | |
83360720 | 1392 | |
0e867886 | 1393 | if (bpf->src == NULL || bpf->dst == NULL) { |
1de7dfff PG |
1394 | if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET)) |
1395 | temp.type = IPSET_NET_PORT; | |
1396 | else | |
1397 | temp.type = IPSET_NET; | |
1398 | } else { | |
1399 | if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET)) | |
1400 | temp.type = IPSET_NET_PORT_NET; | |
1401 | else | |
1402 | temp.type = IPSET_NET_NET; | |
1403 | } | |
0e867886 | 1404 | if (bpf->vrf_id == VRF_UNKNOWN) /* XXX case BGP destroy */ |
d114b0d7 PG |
1405 | temp.vrf_id = 0; |
1406 | else | |
0e867886 | 1407 | temp.vrf_id = bpf->vrf_id; |
d114b0d7 PG |
1408 | bpme = &temp2; |
1409 | bpm = &temp; | |
1410 | bpme->backpointer = bpm; | |
1411 | /* right now, a previous entry may already exist | |
1412 | * flush previous entry if necessary | |
1413 | */ | |
1414 | bpmer.bpme_to_match = bpme; | |
1415 | bpmer.bpme_found = NULL; | |
1416 | hash_walk(bgp->pbr_match_hash, bgp_pbr_get_remaining_entry, &bpmer); | |
1417 | if (bpmer.bpme_found) { | |
1418 | static struct bgp_pbr_match *local_bpm; | |
1419 | static struct bgp_pbr_action *local_bpa; | |
1420 | ||
1421 | local_bpm = bpmer.bpme_found->backpointer; | |
1422 | local_bpa = local_bpm->action; | |
1423 | bgp_pbr_flush_entry(bgp, local_bpa, | |
1424 | local_bpm, bpmer.bpme_found); | |
1425 | } | |
1426 | } | |
1427 | ||
e45aea59 PG |
1428 | static uint8_t bgp_pbr_next_type_entry(uint8_t type_entry) |
1429 | { | |
1430 | if (type_entry == FLOWSPEC_TCP_FLAGS) | |
1431 | return FLOWSPEC_DSCP; | |
1432 | if (type_entry == FLOWSPEC_DSCP) | |
1433 | return FLOWSPEC_PKT_LEN; | |
1434 | if (type_entry == FLOWSPEC_PKT_LEN) | |
1435 | return FLOWSPEC_FRAGMENT; | |
da3fa383 PG |
1436 | if (type_entry == FLOWSPEC_FRAGMENT) |
1437 | return FLOWSPEC_ICMP_TYPE; | |
e45aea59 PG |
1438 | return 0; |
1439 | } | |
1440 | ||
da3fa383 PG |
1441 | static void bgp_pbr_icmp_action(struct bgp *bgp, |
1442 | struct bgp_info *binfo, | |
1443 | struct bgp_pbr_filter *bpf, | |
1444 | struct bgp_pbr_or_filter *bpof, | |
1445 | bool add, | |
1446 | struct nexthop *nh, | |
1447 | float *rate) | |
1448 | { | |
1449 | struct bgp_pbr_range_port srcp, dstp; | |
1450 | struct bgp_pbr_val_mask *icmp_type, *icmp_code; | |
1451 | struct listnode *tnode, *cnode; | |
1452 | ||
1453 | if (!bpf) | |
1454 | return; | |
1455 | if (bpf->protocol != IPPROTO_ICMP) | |
1456 | return; | |
1457 | bpf->src_port = &srcp; | |
1458 | bpf->dst_port = &dstp; | |
1459 | /* parse icmp type and lookup appropriate icmp code | |
1460 | * if no icmp code found, create as many entryes as | |
1461 | * there are listed icmp codes for that icmp type | |
1462 | */ | |
1463 | if (!bpof->icmp_type) { | |
1464 | srcp.min_port = 0; | |
1465 | srcp.max_port = 255; | |
1466 | for (ALL_LIST_ELEMENTS_RO(bpof->icmp_code, cnode, icmp_code)) { | |
1467 | dstp.min_port = icmp_code->val; | |
1468 | if (add) | |
1469 | bgp_pbr_policyroute_add_to_zebra_unit(bgp, binfo, | |
1470 | bpf, nh, rate); | |
1471 | else | |
1472 | bgp_pbr_policyroute_remove_from_zebra_unit( | |
1473 | bgp, binfo, bpf); | |
1474 | } | |
1475 | return; | |
1476 | } | |
1477 | for (ALL_LIST_ELEMENTS_RO(bpof->icmp_type, tnode, icmp_type)) { | |
1478 | srcp.min_port = icmp_type->val; | |
1479 | srcp.max_port = 0; | |
1480 | dstp.max_port = 0; | |
1481 | /* only icmp type. create an entry only with icmp type */ | |
1482 | if (!bpof->icmp_code) { | |
1483 | /* icmp type is not one of the above | |
1484 | * forge an entry only based on the icmp type | |
1485 | */ | |
1486 | dstp.min_port = 0; | |
1487 | dstp.max_port = 255; | |
1488 | if (add) | |
1489 | bgp_pbr_policyroute_add_to_zebra_unit( | |
1490 | bgp, binfo, | |
1491 | bpf, nh, rate); | |
1492 | else | |
1493 | bgp_pbr_policyroute_remove_from_zebra_unit(bgp, | |
1494 | binfo, bpf); | |
1495 | continue; | |
1496 | } | |
1497 | for (ALL_LIST_ELEMENTS_RO(bpof->icmp_code, cnode, icmp_code)) { | |
1498 | dstp.min_port = icmp_code->val; | |
1499 | if (add) | |
1500 | bgp_pbr_policyroute_add_to_zebra_unit( | |
1501 | bgp, binfo, | |
1502 | bpf, nh, rate); | |
1503 | else | |
1504 | bgp_pbr_policyroute_remove_from_zebra_unit( | |
1505 | bgp, binfo, bpf); | |
1506 | } | |
1507 | } | |
1508 | } | |
1509 | ||
56707a36 PG |
1510 | static void bgp_pbr_policyroute_remove_from_zebra_recursive(struct bgp *bgp, |
1511 | struct bgp_info *binfo, | |
1512 | struct bgp_pbr_filter *bpf, | |
1513 | struct bgp_pbr_or_filter *bpof, | |
1514 | uint8_t type_entry) | |
1515 | { | |
1516 | struct listnode *node, *nnode; | |
1517 | struct bgp_pbr_val_mask *valmask; | |
1518 | uint8_t next_type_entry; | |
1519 | struct list *orig_list; | |
1520 | struct bgp_pbr_val_mask **target_val; | |
1521 | ||
1522 | if (type_entry == 0) | |
1523 | return bgp_pbr_policyroute_remove_from_zebra_unit(bgp, | |
1524 | binfo, bpf); | |
e45aea59 | 1525 | next_type_entry = bgp_pbr_next_type_entry(type_entry); |
56707a36 | 1526 | if (type_entry == FLOWSPEC_TCP_FLAGS && bpof->tcpflags) { |
56707a36 PG |
1527 | orig_list = bpof->tcpflags; |
1528 | target_val = &bpf->tcp_flags; | |
1529 | } else if (type_entry == FLOWSPEC_DSCP && bpof->dscp) { | |
56707a36 PG |
1530 | orig_list = bpof->dscp; |
1531 | target_val = &bpf->dscp; | |
cfaf18ce | 1532 | } else if (type_entry == FLOWSPEC_PKT_LEN && bpof->pkt_len) { |
cfaf18ce PG |
1533 | orig_list = bpof->pkt_len; |
1534 | target_val = &bpf->pkt_len_val; | |
6f5617d8 | 1535 | } else if (type_entry == FLOWSPEC_FRAGMENT && bpof->fragment) { |
6f5617d8 PG |
1536 | orig_list = bpof->fragment; |
1537 | target_val = &bpf->fragment; | |
da3fa383 PG |
1538 | } else if (type_entry == FLOWSPEC_ICMP_TYPE && |
1539 | (bpof->icmp_type || bpof->icmp_code)) { | |
1540 | /* enumerate list for icmp - must be last one */ | |
1541 | bgp_pbr_icmp_action(bgp, binfo, bpf, bpof, false, NULL, NULL); | |
1542 | return; | |
56707a36 | 1543 | } else { |
e45aea59 PG |
1544 | return bgp_pbr_policyroute_remove_from_zebra_recursive(bgp, |
1545 | binfo, | |
1546 | bpf, bpof, | |
1547 | next_type_entry); | |
56707a36 PG |
1548 | } |
1549 | for (ALL_LIST_ELEMENTS(orig_list, node, nnode, valmask)) { | |
1550 | *target_val = valmask; | |
1551 | bgp_pbr_policyroute_remove_from_zebra_recursive(bgp, binfo, | |
1552 | bpf, bpof, | |
1553 | next_type_entry); | |
1554 | } | |
1555 | } | |
1556 | ||
9bba1455 PG |
1557 | static void bgp_pbr_policyroute_remove_from_zebra(struct bgp *bgp, |
1558 | struct bgp_info *binfo, | |
1559 | struct bgp_pbr_filter *bpf, | |
1560 | struct bgp_pbr_or_filter *bpof) | |
1561 | { | |
56707a36 PG |
1562 | if (!bpof) |
1563 | return bgp_pbr_policyroute_remove_from_zebra_unit(bgp, | |
1564 | binfo, | |
1565 | bpf); | |
1566 | if (bpof->tcpflags) | |
1567 | bgp_pbr_policyroute_remove_from_zebra_recursive(bgp, binfo, | |
1568 | bpf, bpof, | |
1569 | FLOWSPEC_TCP_FLAGS); | |
1570 | else if (bpof->dscp) | |
1571 | bgp_pbr_policyroute_remove_from_zebra_recursive(bgp, binfo, | |
1572 | bpf, bpof, | |
1573 | FLOWSPEC_DSCP); | |
cfaf18ce PG |
1574 | else if (bpof->pkt_len) |
1575 | bgp_pbr_policyroute_remove_from_zebra_recursive(bgp, binfo, | |
1576 | bpf, bpof, | |
1577 | FLOWSPEC_PKT_LEN); | |
6f5617d8 PG |
1578 | else if (bpof->fragment) |
1579 | bgp_pbr_policyroute_remove_from_zebra_recursive(bgp, binfo, | |
1580 | bpf, bpof, | |
1581 | FLOWSPEC_FRAGMENT); | |
da3fa383 PG |
1582 | else if (bpof->icmp_type || bpof->icmp_code) |
1583 | bgp_pbr_policyroute_remove_from_zebra_recursive(bgp, binfo, | |
1584 | bpf, bpof, | |
1585 | FLOWSPEC_ICMP_TYPE); | |
56707a36 | 1586 | else |
9bba1455 | 1587 | bgp_pbr_policyroute_remove_from_zebra_unit(bgp, binfo, bpf); |
56707a36 PG |
1588 | /* flush bpof */ |
1589 | if (bpof->tcpflags) | |
1590 | list_delete_all_node(bpof->tcpflags); | |
1591 | if (bpof->dscp) | |
1592 | list_delete_all_node(bpof->dscp); | |
cfaf18ce PG |
1593 | if (bpof->pkt_len) |
1594 | list_delete_all_node(bpof->pkt_len); | |
6f5617d8 PG |
1595 | if (bpof->fragment) |
1596 | list_delete_all_node(bpof->fragment); | |
9bba1455 PG |
1597 | } |
1598 | ||
064a9d52 PG |
1599 | static void bgp_pbr_dump_entry(struct bgp_pbr_filter *bpf, bool add) |
1600 | { | |
1601 | struct bgp_pbr_range_port *src_port; | |
1602 | struct bgp_pbr_range_port *dst_port; | |
1603 | struct bgp_pbr_range_port *pkt_len; | |
1604 | char bufsrc[64], bufdst[64]; | |
1605 | char buffer[64]; | |
1606 | int remaining_len = 0; | |
1607 | char protocol_str[16]; | |
1608 | ||
1609 | if (!bpf) | |
1610 | return; | |
1611 | src_port = bpf->src_port; | |
1612 | dst_port = bpf->dst_port; | |
1613 | pkt_len = bpf->pkt_len; | |
1614 | ||
1615 | protocol_str[0] = '\0'; | |
1616 | if (bpf->tcp_flags && bpf->tcp_flags->mask) | |
1617 | bpf->protocol = IPPROTO_TCP; | |
1618 | if (bpf->protocol) | |
1619 | snprintf(protocol_str, sizeof(protocol_str), | |
1620 | "proto %d", bpf->protocol); | |
1621 | buffer[0] = '\0'; | |
1622 | if (bpf->protocol == IPPROTO_ICMP && src_port && dst_port) | |
1623 | remaining_len += snprintf(buffer, sizeof(buffer), | |
1624 | "type %d, code %d", | |
1625 | src_port->min_port, | |
1626 | dst_port->min_port); | |
1627 | else if (bpf->protocol == IPPROTO_UDP || | |
1628 | bpf->protocol == IPPROTO_TCP) { | |
1629 | ||
1630 | if (src_port && src_port->min_port) | |
1631 | remaining_len += snprintf(buffer, | |
1632 | sizeof(buffer), | |
1633 | "from [%u:%u]", | |
1634 | src_port->min_port, | |
1635 | src_port->max_port ? | |
1636 | src_port->max_port : | |
1637 | src_port->min_port); | |
1638 | if (dst_port && dst_port->min_port) | |
1639 | remaining_len += snprintf(buffer + | |
1640 | remaining_len, | |
1641 | sizeof(buffer) | |
1642 | - remaining_len, | |
1643 | "to [%u:%u]", | |
1644 | dst_port->min_port, | |
1645 | dst_port->max_port ? | |
1646 | dst_port->max_port : | |
1647 | dst_port->min_port); | |
1648 | } | |
1649 | if (pkt_len && (pkt_len->min_port || pkt_len->max_port)) { | |
1650 | remaining_len += snprintf(buffer + remaining_len, | |
1651 | sizeof(buffer) | |
1652 | - remaining_len, | |
1653 | " len [%u:%u]", | |
1654 | pkt_len->min_port, | |
1655 | pkt_len->max_port ? | |
1656 | pkt_len->max_port : | |
1657 | pkt_len->min_port); | |
1658 | } else if (bpf->pkt_len_val) { | |
1659 | remaining_len += snprintf(buffer + remaining_len, | |
1660 | sizeof(buffer) | |
1661 | - remaining_len, | |
1662 | " %s len %u", | |
1663 | bpf->pkt_len_val->mask | |
1664 | ? "!" : "", | |
1665 | bpf->pkt_len_val->val); | |
1666 | } | |
1667 | if (bpf->tcp_flags) { | |
1668 | remaining_len += snprintf(buffer + remaining_len, | |
1669 | sizeof(buffer) | |
1670 | - remaining_len, | |
1671 | "tcpflags %x/%x", | |
1672 | bpf->tcp_flags->val, | |
1673 | bpf->tcp_flags->mask); | |
1674 | } | |
1675 | if (bpf->dscp) { | |
1676 | snprintf(buffer + remaining_len, | |
1677 | sizeof(buffer) | |
1678 | - remaining_len, | |
1679 | "%s dscp %d", | |
1680 | bpf->dscp->mask | |
1681 | ? "!" : "", | |
1682 | bpf->dscp->val); | |
1683 | } | |
45837bc4 | 1684 | zlog_debug("BGP: %s FS PBR from %s to %s, %s %s", |
064a9d52 PG |
1685 | add ? "adding" : "removing", |
1686 | bpf->src == NULL ? "<all>" : | |
1687 | prefix2str(bpf->src, bufsrc, sizeof(bufsrc)), | |
1688 | bpf->dst == NULL ? "<all>" : | |
1689 | prefix2str(bpf->dst, bufdst, sizeof(bufdst)), | |
1690 | protocol_str, buffer); | |
1691 | ||
1692 | } | |
1693 | ||
9bba1455 | 1694 | static void bgp_pbr_policyroute_add_to_zebra_unit(struct bgp *bgp, |
1de7dfff | 1695 | struct bgp_info *binfo, |
0e867886 | 1696 | struct bgp_pbr_filter *bpf, |
1de7dfff | 1697 | struct nexthop *nh, |
0e867886 | 1698 | float *rate) |
d114b0d7 PG |
1699 | { |
1700 | struct bgp_pbr_match temp; | |
1701 | struct bgp_pbr_match_entry temp2; | |
1702 | struct bgp_pbr_match *bpm; | |
1703 | struct bgp_pbr_match_entry *bpme = NULL; | |
1704 | struct bgp_pbr_action temp3; | |
1705 | struct bgp_pbr_action *bpa = NULL; | |
1706 | struct bgp_pbr_match_entry_remain bpmer; | |
0e867886 PG |
1707 | struct bgp_pbr_range_port *src_port; |
1708 | struct bgp_pbr_range_port *dst_port; | |
1709 | struct bgp_pbr_range_port *pkt_len; | |
c26edcda | 1710 | bool bpme_found = false; |
0e867886 PG |
1711 | |
1712 | if (!bpf) | |
1713 | return; | |
1714 | src_port = bpf->src_port; | |
1715 | dst_port = bpf->dst_port; | |
1716 | pkt_len = bpf->pkt_len; | |
d114b0d7 | 1717 | |
064a9d52 PG |
1718 | if (BGP_DEBUG(zebra, ZEBRA)) |
1719 | bgp_pbr_dump_entry(bpf, true); | |
1720 | ||
d114b0d7 PG |
1721 | /* look for bpa first */ |
1722 | memset(&temp3, 0, sizeof(temp3)); | |
1723 | if (rate) | |
1724 | temp3.rate = *rate; | |
1725 | if (nh) | |
1726 | memcpy(&temp3.nh, nh, sizeof(struct nexthop)); | |
0e867886 | 1727 | temp3.vrf_id = bpf->vrf_id; |
d114b0d7 PG |
1728 | bpa = hash_get(bgp->pbr_action_hash, &temp3, |
1729 | bgp_pbr_action_alloc_intern); | |
1730 | ||
1731 | if (bpa->fwmark == 0) { | |
d114b0d7 PG |
1732 | /* drop is handled by iptable */ |
1733 | if (nh && nh->type == NEXTHOP_TYPE_BLACKHOLE) { | |
1734 | bpa->table_id = 0; | |
1735 | bpa->installed = true; | |
1736 | } else { | |
31c28cd7 PG |
1737 | bpa->fwmark = bgp_zebra_tm_get_id(); |
1738 | bpa->table_id = bpa->fwmark; | |
d114b0d7 PG |
1739 | bpa->installed = false; |
1740 | } | |
a6b07429 | 1741 | bpa->bgp = bgp; |
d114b0d7 PG |
1742 | bpa->unique = ++bgp_pbr_action_counter_unique; |
1743 | /* 0 value is forbidden */ | |
1744 | bpa->install_in_progress = false; | |
1745 | } | |
1746 | ||
1747 | /* then look for bpm */ | |
1748 | memset(&temp, 0, sizeof(temp)); | |
0e867886 PG |
1749 | temp.vrf_id = bpf->vrf_id; |
1750 | if (bpf->src) | |
d114b0d7 | 1751 | temp.flags |= MATCH_IP_SRC_SET; |
0e867886 | 1752 | if (bpf->dst) |
d114b0d7 | 1753 | temp.flags |= MATCH_IP_DST_SET; |
1de7dfff | 1754 | |
3bed2363 PG |
1755 | if (src_port && (src_port->min_port || bpf->protocol == IPPROTO_ICMP)) { |
1756 | if (bpf->protocol == IPPROTO_ICMP) | |
1757 | temp.flags |= MATCH_ICMP_SET; | |
1de7dfff | 1758 | temp.flags |= MATCH_PORT_SRC_SET; |
3bed2363 PG |
1759 | } |
1760 | if (dst_port && (dst_port->min_port || bpf->protocol == IPPROTO_ICMP)) { | |
1761 | if (bpf->protocol == IPPROTO_ICMP) | |
1762 | temp.flags |= MATCH_ICMP_SET; | |
1de7dfff | 1763 | temp.flags |= MATCH_PORT_DST_SET; |
3bed2363 | 1764 | } |
1de7dfff PG |
1765 | if (src_port && src_port->max_port) |
1766 | temp.flags |= MATCH_PORT_SRC_RANGE_SET; | |
1767 | if (dst_port && dst_port->max_port) | |
1768 | temp.flags |= MATCH_PORT_DST_RANGE_SET; | |
3bed2363 PG |
1769 | |
1770 | if (bpf->src == NULL || bpf->dst == NULL) { | |
1771 | if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET)) | |
1772 | temp.type = IPSET_NET_PORT; | |
1773 | else | |
1774 | temp.type = IPSET_NET; | |
1775 | } else { | |
1776 | if (temp.flags & (MATCH_PORT_DST_SET | MATCH_PORT_SRC_SET)) | |
1777 | temp.type = IPSET_NET_PORT_NET; | |
1778 | else | |
1779 | temp.type = IPSET_NET_NET; | |
1780 | } | |
cfaf18ce | 1781 | if (pkt_len) { |
83360720 | 1782 | temp.pkt_len_min = pkt_len->min_port; |
cfaf18ce PG |
1783 | if (pkt_len->max_port) |
1784 | temp.pkt_len_max = pkt_len->max_port; | |
1785 | } else if (bpf->pkt_len_val) { | |
1786 | if (bpf->pkt_len_val->mask) | |
1787 | temp.flags |= MATCH_PKT_LEN_INVERSE_SET; | |
1788 | temp.pkt_len_min = bpf->pkt_len_val->val; | |
1789 | } | |
2da7d62e PG |
1790 | if (bpf->tcp_flags) { |
1791 | temp.tcp_flags = bpf->tcp_flags->val; | |
1792 | temp.tcp_mask_flags = bpf->tcp_flags->mask; | |
1793 | } | |
4977bd6c PG |
1794 | if (bpf->dscp) { |
1795 | if (bpf->dscp->mask) | |
1796 | temp.flags |= MATCH_DSCP_INVERSE_SET; | |
1797 | else | |
1798 | temp.flags |= MATCH_DSCP_SET; | |
1799 | temp.dscp_value = bpf->dscp->val; | |
1800 | } | |
6f5617d8 PG |
1801 | if (bpf->fragment) { |
1802 | if (bpf->fragment->mask) | |
1803 | temp.flags |= MATCH_FRAGMENT_INVERSE_SET; | |
1804 | temp.fragment = bpf->fragment->val; | |
1805 | } | |
d114b0d7 PG |
1806 | temp.action = bpa; |
1807 | bpm = hash_get(bgp->pbr_match_hash, &temp, | |
1808 | bgp_pbr_match_alloc_intern); | |
1809 | ||
1810 | /* new, then self allocate ipset_name and unique */ | |
5a430eee | 1811 | if (bpm->unique == 0) { |
d114b0d7 PG |
1812 | bpm->unique = ++bgp_pbr_match_counter_unique; |
1813 | /* 0 value is forbidden */ | |
1814 | sprintf(bpm->ipset_name, "match%p", bpm); | |
1815 | bpm->entry_hash = hash_create_size(8, | |
1816 | bgp_pbr_match_entry_hash_key, | |
1817 | bgp_pbr_match_entry_hash_equal, | |
1818 | "Match Entry Hash"); | |
1819 | bpm->installed = false; | |
1820 | ||
1821 | /* unique2 should be updated too */ | |
1822 | bpm->unique2 = ++bgp_pbr_match_iptable_counter_unique; | |
1823 | bpm->installed_in_iptable = false; | |
1824 | bpm->install_in_progress = false; | |
1825 | bpm->install_iptable_in_progress = false; | |
1826 | } | |
1827 | ||
1828 | memset(&temp2, 0, sizeof(temp2)); | |
0e867886 PG |
1829 | if (bpf->src) |
1830 | prefix_copy(&temp2.src, bpf->src); | |
d114b0d7 PG |
1831 | else |
1832 | temp2.src.family = AF_INET; | |
0e867886 PG |
1833 | if (bpf->dst) |
1834 | prefix_copy(&temp2.dst, bpf->dst); | |
d114b0d7 PG |
1835 | else |
1836 | temp2.dst.family = AF_INET; | |
1de7dfff PG |
1837 | temp2.src_port_min = src_port ? src_port->min_port : 0; |
1838 | temp2.dst_port_min = dst_port ? dst_port->min_port : 0; | |
1839 | temp2.src_port_max = src_port ? src_port->max_port : 0; | |
1840 | temp2.dst_port_max = dst_port ? dst_port->max_port : 0; | |
0e867886 | 1841 | temp2.proto = bpf->protocol; |
5a430eee PG |
1842 | bpme = hash_get(bpm->entry_hash, &temp2, |
1843 | bgp_pbr_match_entry_alloc_intern); | |
1844 | if (bpme->unique == 0) { | |
d114b0d7 PG |
1845 | bpme->unique = ++bgp_pbr_match_entry_counter_unique; |
1846 | /* 0 value is forbidden */ | |
1847 | bpme->backpointer = bpm; | |
1848 | bpme->installed = false; | |
1849 | bpme->install_in_progress = false; | |
b588b642 PG |
1850 | /* link bgp info to bpme */ |
1851 | bpme->bgp_info = (void *)binfo; | |
c26edcda PG |
1852 | } else |
1853 | bpme_found = true; | |
d114b0d7 | 1854 | |
c26edcda | 1855 | /* already installed */ |
5a430eee | 1856 | if (bpme_found) { |
c26edcda PG |
1857 | struct bgp_info_extra *extra = bgp_info_extra_get(binfo); |
1858 | ||
1859 | if (extra && extra->bgp_fs_pbr && | |
1860 | listnode_lookup(extra->bgp_fs_pbr, bpme)) { | |
1861 | if (BGP_DEBUG(pbr, PBR_ERROR)) | |
1862 | zlog_err("%s: entry %p/%p already installed in bgp pbr", | |
1863 | __func__, binfo, bpme); | |
1864 | return; | |
1865 | } | |
1866 | } | |
d114b0d7 PG |
1867 | /* BGP FS: append entry to zebra |
1868 | * - policies are not routing entries and as such | |
1869 | * route replace semantics don't necessarily follow | |
1870 | * through to policy entries | |
1871 | * - because of that, not all policing information will be stored | |
1872 | * into zebra. and non selected policies will be suppressed from zebra | |
1873 | * - as consequence, in order to bring consistency | |
1874 | * a policy will be added, then ifan ecmp policy exists, | |
1875 | * it will be suppressed subsequently | |
1876 | */ | |
1877 | /* ip rule add */ | |
1a1f4a4c | 1878 | if (!bpa->installed && !bpa->install_in_progress) { |
d114b0d7 | 1879 | bgp_send_pbr_rule_action(bpa, true); |
f7df1907 PG |
1880 | bgp_zebra_announce_default(bgp, nh, |
1881 | AFI_IP, bpa->table_id, true); | |
1882 | } | |
d114b0d7 PG |
1883 | |
1884 | /* ipset create */ | |
6ea591c7 | 1885 | if (!bpm->installed) |
d114b0d7 PG |
1886 | bgp_send_pbr_ipset_match(bpm, true); |
1887 | /* ipset add */ | |
6ea591c7 | 1888 | if (!bpme->installed) |
d114b0d7 PG |
1889 | bgp_send_pbr_ipset_entry_match(bpme, true); |
1890 | ||
1891 | /* iptables */ | |
6ea591c7 | 1892 | if (!bpm->installed_in_iptable) |
d114b0d7 PG |
1893 | bgp_send_pbr_iptable(bpa, bpm, true); |
1894 | ||
1895 | /* A previous entry may already exist | |
1896 | * flush previous entry if necessary | |
1897 | */ | |
1898 | bpmer.bpme_to_match = bpme; | |
1899 | bpmer.bpme_found = NULL; | |
1900 | hash_walk(bgp->pbr_match_hash, bgp_pbr_get_remaining_entry, &bpmer); | |
1901 | if (bpmer.bpme_found) { | |
1902 | static struct bgp_pbr_match *local_bpm; | |
1903 | static struct bgp_pbr_action *local_bpa; | |
1904 | ||
1905 | local_bpm = bpmer.bpme_found->backpointer; | |
1906 | local_bpa = local_bpm->action; | |
1907 | bgp_pbr_flush_entry(bgp, local_bpa, | |
1908 | local_bpm, bpmer.bpme_found); | |
1909 | } | |
1910 | ||
1911 | ||
1912 | } | |
1913 | ||
56707a36 PG |
1914 | static void bgp_pbr_policyroute_add_to_zebra_recursive(struct bgp *bgp, |
1915 | struct bgp_info *binfo, | |
1916 | struct bgp_pbr_filter *bpf, | |
1917 | struct bgp_pbr_or_filter *bpof, | |
1918 | struct nexthop *nh, | |
1919 | float *rate, | |
1920 | uint8_t type_entry) | |
1921 | { | |
1922 | struct listnode *node, *nnode; | |
1923 | struct bgp_pbr_val_mask *valmask; | |
1924 | uint8_t next_type_entry; | |
1925 | struct list *orig_list; | |
1926 | struct bgp_pbr_val_mask **target_val; | |
1927 | ||
1928 | if (type_entry == 0) | |
1929 | return bgp_pbr_policyroute_add_to_zebra_unit(bgp, binfo, bpf, | |
1930 | nh, rate); | |
e45aea59 | 1931 | next_type_entry = bgp_pbr_next_type_entry(type_entry); |
56707a36 | 1932 | if (type_entry == FLOWSPEC_TCP_FLAGS && bpof->tcpflags) { |
56707a36 PG |
1933 | orig_list = bpof->tcpflags; |
1934 | target_val = &bpf->tcp_flags; | |
1935 | } else if (type_entry == FLOWSPEC_DSCP && bpof->dscp) { | |
56707a36 PG |
1936 | orig_list = bpof->dscp; |
1937 | target_val = &bpf->dscp; | |
cfaf18ce | 1938 | } else if (type_entry == FLOWSPEC_PKT_LEN && bpof->pkt_len) { |
cfaf18ce PG |
1939 | orig_list = bpof->pkt_len; |
1940 | target_val = &bpf->pkt_len_val; | |
6f5617d8 | 1941 | } else if (type_entry == FLOWSPEC_FRAGMENT && bpof->fragment) { |
6f5617d8 PG |
1942 | orig_list = bpof->fragment; |
1943 | target_val = &bpf->fragment; | |
da3fa383 PG |
1944 | } else if (type_entry == FLOWSPEC_ICMP_TYPE && |
1945 | (bpof->icmp_type || bpof->icmp_code)) { | |
1946 | /* enumerate list for icmp - must be last one */ | |
1947 | bgp_pbr_icmp_action(bgp, binfo, bpf, bpof, true, nh, rate); | |
1948 | return; | |
56707a36 PG |
1949 | } else { |
1950 | return bgp_pbr_policyroute_add_to_zebra_recursive(bgp, binfo, | |
e45aea59 PG |
1951 | bpf, bpof, nh, rate, |
1952 | next_type_entry); | |
56707a36 PG |
1953 | } |
1954 | for (ALL_LIST_ELEMENTS(orig_list, node, nnode, valmask)) { | |
1955 | *target_val = valmask; | |
1956 | bgp_pbr_policyroute_add_to_zebra_recursive(bgp, binfo, | |
1957 | bpf, bpof, | |
1958 | nh, rate, | |
1959 | next_type_entry); | |
1960 | } | |
1961 | } | |
1962 | ||
9bba1455 PG |
1963 | static void bgp_pbr_policyroute_add_to_zebra(struct bgp *bgp, |
1964 | struct bgp_info *binfo, | |
1965 | struct bgp_pbr_filter *bpf, | |
1966 | struct bgp_pbr_or_filter *bpof, | |
1967 | struct nexthop *nh, | |
1968 | float *rate) | |
1969 | { | |
56707a36 | 1970 | if (!bpof) |
9bba1455 PG |
1971 | return bgp_pbr_policyroute_add_to_zebra_unit(bgp, binfo, |
1972 | bpf, nh, rate); | |
56707a36 PG |
1973 | if (bpof->tcpflags) |
1974 | bgp_pbr_policyroute_add_to_zebra_recursive(bgp, binfo, | |
1975 | bpf, bpof, | |
1976 | nh, rate, | |
1977 | FLOWSPEC_TCP_FLAGS); | |
1978 | else if (bpof->dscp) | |
1979 | bgp_pbr_policyroute_add_to_zebra_recursive(bgp, binfo, | |
1980 | bpf, bpof, | |
1981 | nh, rate, | |
1982 | FLOWSPEC_DSCP); | |
cfaf18ce PG |
1983 | else if (bpof->pkt_len) |
1984 | bgp_pbr_policyroute_add_to_zebra_recursive(bgp, binfo, | |
1985 | bpf, bpof, | |
1986 | nh, rate, | |
1987 | FLOWSPEC_PKT_LEN); | |
6f5617d8 PG |
1988 | else if (bpof->fragment) |
1989 | bgp_pbr_policyroute_add_to_zebra_recursive(bgp, binfo, | |
1990 | bpf, bpof, | |
1991 | nh, rate, | |
1992 | FLOWSPEC_FRAGMENT); | |
da3fa383 PG |
1993 | else if (bpof->icmp_type || bpof->icmp_code) |
1994 | bgp_pbr_policyroute_add_to_zebra_recursive(bgp, binfo, | |
1995 | bpf, bpof, nh, rate, | |
1996 | FLOWSPEC_ICMP_TYPE); | |
56707a36 PG |
1997 | else |
1998 | bgp_pbr_policyroute_add_to_zebra_unit(bgp, binfo, bpf, | |
1999 | nh, rate); | |
2000 | /* flush bpof */ | |
2001 | if (bpof->tcpflags) | |
2002 | list_delete_all_node(bpof->tcpflags); | |
2003 | if (bpof->dscp) | |
2004 | list_delete_all_node(bpof->dscp); | |
cfaf18ce PG |
2005 | if (bpof->pkt_len) |
2006 | list_delete_all_node(bpof->pkt_len); | |
6f5617d8 PG |
2007 | if (bpof->fragment) |
2008 | list_delete_all_node(bpof->fragment); | |
da3fa383 PG |
2009 | if (bpof->icmp_type) |
2010 | list_delete_all_node(bpof->icmp_type); | |
2011 | if (bpof->icmp_code) | |
2012 | list_delete_all_node(bpof->icmp_code); | |
932404b7 PG |
2013 | } |
2014 | ||
d114b0d7 PG |
2015 | static void bgp_pbr_handle_entry(struct bgp *bgp, |
2016 | struct bgp_info *binfo, | |
2017 | struct bgp_pbr_entry_main *api, | |
2018 | bool add) | |
2019 | { | |
2020 | struct nexthop nh; | |
2021 | int i = 0; | |
2022 | int continue_loop = 1; | |
2023 | float rate = 0; | |
2024 | struct prefix *src = NULL, *dst = NULL; | |
1de7dfff PG |
2025 | uint8_t proto = 0; |
2026 | struct bgp_pbr_range_port *srcp = NULL, *dstp = NULL; | |
932404b7 | 2027 | struct bgp_pbr_range_port range, range_icmp_code; |
83360720 | 2028 | struct bgp_pbr_range_port pkt_len; |
0e867886 | 2029 | struct bgp_pbr_filter bpf; |
c5ee26cc PG |
2030 | uint8_t kind_enum; |
2031 | struct bgp_pbr_or_filter bpof; | |
9bba1455 | 2032 | struct bgp_pbr_val_mask bpvm; |
d114b0d7 | 2033 | |
8cda9106 | 2034 | memset(&nh, 0, sizeof(struct nexthop)); |
0e867886 | 2035 | memset(&bpf, 0, sizeof(struct bgp_pbr_filter)); |
c5ee26cc | 2036 | memset(&bpof, 0, sizeof(struct bgp_pbr_or_filter)); |
d114b0d7 PG |
2037 | if (api->match_bitmask & PREFIX_SRC_PRESENT) |
2038 | src = &api->src_prefix; | |
2039 | if (api->match_bitmask & PREFIX_DST_PRESENT) | |
2040 | dst = &api->dst_prefix; | |
2041 | memset(&nh, 0, sizeof(struct nexthop)); | |
2042 | nh.vrf_id = VRF_UNKNOWN; | |
1de7dfff PG |
2043 | if (api->match_protocol_num) |
2044 | proto = (uint8_t)api->protocol[0].value; | |
2045 | /* if match_port is selected, then either src or dst port will be parsed | |
2046 | * but not both at the same time | |
2047 | */ | |
2048 | if (api->match_port_num >= 1) { | |
2049 | bgp_pbr_extract(api->port, | |
2050 | api->match_port_num, | |
2051 | &range); | |
2052 | srcp = dstp = ⦥ | |
2053 | } else if (api->match_src_port_num >= 1) { | |
2054 | bgp_pbr_extract(api->src_port, | |
2055 | api->match_src_port_num, | |
2056 | &range); | |
2057 | srcp = ⦥ | |
2058 | dstp = NULL; | |
2059 | } else if (api->match_dst_port_num >= 1) { | |
2060 | bgp_pbr_extract(api->dst_port, | |
2061 | api->match_dst_port_num, | |
2062 | &range); | |
2063 | dstp = ⦥ | |
2064 | srcp = NULL; | |
2065 | } | |
932404b7 PG |
2066 | if (api->match_icmp_type_num >= 1) { |
2067 | proto = IPPROTO_ICMP; | |
da3fa383 PG |
2068 | if (bgp_pbr_extract(api->icmp_type, |
2069 | api->match_icmp_type_num, | |
2070 | &range)) | |
932404b7 | 2071 | srcp = ⦥ |
da3fa383 PG |
2072 | else { |
2073 | bpof.icmp_type = list_new(); | |
2074 | bgp_pbr_extract_enumerate(api->icmp_type, | |
2075 | api->match_icmp_type_num, | |
2076 | OPERATOR_UNARY_OR, | |
2077 | bpof.icmp_type, | |
2078 | FLOWSPEC_ICMP_TYPE); | |
932404b7 PG |
2079 | } |
2080 | } | |
2081 | if (api->match_icmp_code_num >= 1) { | |
2082 | proto = IPPROTO_ICMP; | |
da3fa383 PG |
2083 | if (bgp_pbr_extract(api->icmp_code, |
2084 | api->match_icmp_code_num, | |
2085 | &range_icmp_code)) | |
932404b7 | 2086 | dstp = &range_icmp_code; |
da3fa383 PG |
2087 | else { |
2088 | bpof.icmp_code = list_new(); | |
2089 | bgp_pbr_extract_enumerate(api->icmp_code, | |
2090 | api->match_icmp_code_num, | |
2091 | OPERATOR_UNARY_OR, | |
2092 | bpof.icmp_code, | |
2093 | FLOWSPEC_ICMP_CODE); | |
932404b7 PG |
2094 | } |
2095 | } | |
2da7d62e | 2096 | |
c5ee26cc PG |
2097 | if (api->match_tcpflags_num) { |
2098 | kind_enum = bgp_pbr_match_val_get_operator(api->tcpflags, | |
2099 | api->match_tcpflags_num); | |
2100 | if (kind_enum == OPERATOR_UNARY_AND) { | |
9bba1455 | 2101 | bpf.tcp_flags = &bpvm; |
c5ee26cc PG |
2102 | bgp_pbr_extract_enumerate(api->tcpflags, |
2103 | api->match_tcpflags_num, | |
35703998 PG |
2104 | OPERATOR_UNARY_AND, |
2105 | bpf.tcp_flags, | |
2106 | FLOWSPEC_TCP_FLAGS); | |
c5ee26cc PG |
2107 | } else if (kind_enum == OPERATOR_UNARY_OR) { |
2108 | bpof.tcpflags = list_new(); | |
2109 | bgp_pbr_extract_enumerate(api->tcpflags, | |
2110 | api->match_tcpflags_num, | |
35703998 PG |
2111 | OPERATOR_UNARY_OR, |
2112 | bpof.tcpflags, | |
2113 | FLOWSPEC_TCP_FLAGS); | |
c5ee26cc PG |
2114 | } |
2115 | } | |
cfaf18ce PG |
2116 | if (api->match_packet_length_num) { |
2117 | bool ret; | |
2118 | ||
2119 | ret = bgp_pbr_extract(api->packet_length, | |
2120 | api->match_packet_length_num, | |
2121 | &pkt_len); | |
2122 | if (ret) | |
2123 | bpf.pkt_len = &pkt_len; | |
2124 | else { | |
2125 | bpof.pkt_len = list_new(); | |
2126 | bgp_pbr_extract_enumerate(api->packet_length, | |
2127 | api->match_packet_length_num, | |
2128 | OPERATOR_UNARY_OR, | |
2129 | bpof.pkt_len, | |
2130 | FLOWSPEC_PKT_LEN); | |
2131 | } | |
2da7d62e | 2132 | } |
4977bd6c | 2133 | if (api->match_dscp_num >= 1) { |
35703998 PG |
2134 | bpof.dscp = list_new(); |
2135 | bgp_pbr_extract_enumerate(api->dscp, api->match_dscp_num, | |
2136 | OPERATOR_UNARY_OR, | |
2137 | bpof.dscp, FLOWSPEC_DSCP); | |
4977bd6c | 2138 | } |
6f5617d8 PG |
2139 | if (api->match_fragment_num) { |
2140 | bpof.fragment = list_new(); | |
2141 | bgp_pbr_extract_enumerate(api->fragment, | |
2142 | api->match_fragment_num, | |
2143 | OPERATOR_UNARY_OR, | |
2144 | bpof.fragment, | |
2145 | FLOWSPEC_FRAGMENT); | |
2146 | } | |
0e867886 PG |
2147 | bpf.vrf_id = api->vrf_id; |
2148 | bpf.src = src; | |
2149 | bpf.dst = dst; | |
2150 | bpf.protocol = proto; | |
0e867886 PG |
2151 | bpf.src_port = srcp; |
2152 | bpf.dst_port = dstp; | |
da3fa383 | 2153 | if (!add) |
932404b7 PG |
2154 | return bgp_pbr_policyroute_remove_from_zebra(bgp, |
2155 | binfo, | |
9bba1455 | 2156 | &bpf, &bpof); |
d114b0d7 PG |
2157 | /* no action for add = true */ |
2158 | for (i = 0; i < api->action_num; i++) { | |
2159 | switch (api->actions[i].action) { | |
2160 | case ACTION_TRAFFICRATE: | |
2161 | /* drop packet */ | |
2162 | if (api->actions[i].u.r.rate == 0) { | |
2163 | nh.vrf_id = api->vrf_id; | |
2164 | nh.type = NEXTHOP_TYPE_BLACKHOLE; | |
da3fa383 PG |
2165 | bgp_pbr_policyroute_add_to_zebra(bgp, binfo, |
2166 | &bpf, &bpof, | |
2167 | &nh, &rate); | |
d114b0d7 PG |
2168 | } else { |
2169 | /* update rate. can be reentrant */ | |
2170 | rate = api->actions[i].u.r.rate; | |
ac7c35f8 | 2171 | if (BGP_DEBUG(pbr, PBR)) { |
d114b0d7 | 2172 | bgp_pbr_print_policy_route(api); |
ac7c35f8 PG |
2173 | zlog_warn("PBR: ignoring Set action rate %f", |
2174 | api->actions[i].u.r.rate); | |
2175 | } | |
d114b0d7 PG |
2176 | } |
2177 | break; | |
2178 | case ACTION_TRAFFIC_ACTION: | |
2179 | if (api->actions[i].u.za.filter | |
2180 | & TRAFFIC_ACTION_SAMPLE) { | |
ac7c35f8 | 2181 | if (BGP_DEBUG(pbr, PBR)) { |
d114b0d7 | 2182 | bgp_pbr_print_policy_route(api); |
ac7c35f8 PG |
2183 | zlog_warn("PBR: Sample action Ignored"); |
2184 | } | |
d114b0d7 PG |
2185 | } |
2186 | #if 0 | |
2187 | if (api->actions[i].u.za.filter | |
2188 | & TRAFFIC_ACTION_DISTRIBUTE) { | |
ac7c35f8 | 2189 | if (BGP_DEBUG(pbr, PBR)) { |
d114b0d7 | 2190 | bgp_pbr_print_policy_route(api); |
ac7c35f8 PG |
2191 | zlog_warn("PBR: Distribute action Applies"); |
2192 | } | |
d114b0d7 PG |
2193 | continue_loop = 0; |
2194 | /* continue forwarding entry as before | |
2195 | * no action | |
2196 | */ | |
2197 | } | |
2198 | #endif /* XXX to confirm behaviour of traffic action. for now , ignore */ | |
2199 | /* terminate action: run other filters | |
2200 | */ | |
2201 | break; | |
2202 | case ACTION_REDIRECT_IP: | |
2203 | nh.type = NEXTHOP_TYPE_IPV4; | |
2204 | nh.gate.ipv4.s_addr = | |
2205 | api->actions[i].u.zr.redirect_ip_v4.s_addr; | |
2206 | nh.vrf_id = api->vrf_id; | |
da3fa383 PG |
2207 | bgp_pbr_policyroute_add_to_zebra(bgp, binfo, |
2208 | &bpf, &bpof, | |
2209 | &nh, &rate); | |
d114b0d7 PG |
2210 | /* XXX combination with REDIRECT_VRF |
2211 | * + REDIRECT_NH_IP not done | |
2212 | */ | |
2213 | continue_loop = 0; | |
2214 | break; | |
2215 | case ACTION_REDIRECT: | |
2216 | nh.vrf_id = api->actions[i].u.redirect_vrf; | |
2217 | nh.type = NEXTHOP_TYPE_IPV4; | |
da3fa383 PG |
2218 | bgp_pbr_policyroute_add_to_zebra(bgp, binfo, |
2219 | &bpf, &bpof, | |
2220 | &nh, &rate); | |
d114b0d7 PG |
2221 | continue_loop = 0; |
2222 | break; | |
2223 | case ACTION_MARKING: | |
ac7c35f8 | 2224 | if (BGP_DEBUG(pbr, PBR)) { |
d114b0d7 | 2225 | bgp_pbr_print_policy_route(api); |
ac7c35f8 PG |
2226 | zlog_warn("PBR: Set DSCP %u Ignored", |
2227 | api->actions[i].u.marking_dscp); | |
2228 | } | |
d114b0d7 PG |
2229 | break; |
2230 | default: | |
2231 | break; | |
2232 | } | |
2233 | if (continue_loop == 0) | |
2234 | break; | |
2235 | } | |
2236 | } | |
2237 | ||
45918cfb PG |
2238 | void bgp_pbr_update_entry(struct bgp *bgp, struct prefix *p, |
2239 | struct bgp_info *info, afi_t afi, safi_t safi, | |
2240 | bool nlri_update) | |
2241 | { | |
2242 | struct bgp_pbr_entry_main api; | |
2243 | ||
2244 | if (afi == AFI_IP6) | |
2245 | return; /* IPv6 not supported */ | |
2246 | if (safi != SAFI_FLOWSPEC) | |
2247 | return; /* not supported */ | |
2248 | /* Make Zebra API structure. */ | |
2249 | memset(&api, 0, sizeof(api)); | |
2250 | api.vrf_id = bgp->vrf_id; | |
2251 | api.afi = afi; | |
2252 | ||
6818e7e5 | 2253 | if (!bgp_zebra_tm_chunk_obtained()) { |
f146bb54 | 2254 | if (BGP_DEBUG(pbr, PBR_ERROR)) |
e50f7cfd | 2255 | flog_err(EC_BGP_TABLE_CHUNK, |
1c50c1c0 | 2256 | "%s: table chunk not obtained yet", __func__); |
6818e7e5 PG |
2257 | return; |
2258 | } | |
6818e7e5 PG |
2259 | |
2260 | if (bgp_pbr_build_and_validate_entry(p, info, &api) < 0) { | |
2261 | if (BGP_DEBUG(pbr, PBR_ERROR)) | |
e50f7cfd | 2262 | flog_err(EC_BGP_FLOWSPEC_INSTALLATION, |
1c50c1c0 QY |
2263 | "%s: cancel updating entry %p in bgp pbr", |
2264 | __func__, info); | |
45918cfb PG |
2265 | return; |
2266 | } | |
d114b0d7 | 2267 | bgp_pbr_handle_entry(bgp, info, &api, nlri_update); |
45918cfb | 2268 | } |
4762c213 PG |
2269 | |
2270 | int bgp_pbr_interface_compare(const struct bgp_pbr_interface *a, | |
2271 | const struct bgp_pbr_interface *b) | |
2272 | { | |
2273 | return strcmp(a->name, b->name); | |
2274 | } | |
2275 | ||
2276 | struct bgp_pbr_interface *bgp_pbr_interface_lookup(const char *name, | |
2277 | struct bgp_pbr_interface_head *head) | |
2278 | { | |
2279 | struct bgp_pbr_interface pbr_if; | |
2280 | ||
2281 | strlcpy(pbr_if.name, name, sizeof(pbr_if.name)); | |
2282 | return (RB_FIND(bgp_pbr_interface_head, | |
2283 | head, &pbr_if)); | |
2284 | } | |
2285 | ||
2286 | /* this function resets to the default policy routing | |
2287 | * go back to default status | |
2288 | */ | |
2289 | void bgp_pbr_reset(struct bgp *bgp, afi_t afi) | |
2290 | { | |
2291 | struct bgp_pbr_config *bgp_pbr_cfg = bgp->bgp_pbr_cfg; | |
2292 | struct bgp_pbr_interface_head *head; | |
2293 | struct bgp_pbr_interface *pbr_if; | |
2294 | ||
2295 | if (!bgp_pbr_cfg || afi != AFI_IP) | |
2296 | return; | |
2297 | head = &(bgp_pbr_cfg->ifaces_by_name_ipv4); | |
2298 | ||
2299 | while (!RB_EMPTY(bgp_pbr_interface_head, head)) { | |
2300 | pbr_if = RB_ROOT(bgp_pbr_interface_head, head); | |
2301 | RB_REMOVE(bgp_pbr_interface_head, head, pbr_if); | |
2302 | XFREE(MTYPE_TMP, pbr_if); | |
2303 | } | |
2304 | } |