1 // SPDX-License-Identifier: GPL-2.0-or-later
12 /* flowspec case: 0 to 3 actions maximum:
17 #define ACTIONS_MAX_NUM 4
18 enum bgp_pbr_action_enum
{
19 ACTION_TRAFFICRATE
= 1,
20 ACTION_TRAFFIC_ACTION
= 2,
23 ACTION_REDIRECT_IP
= 5
26 #define TRAFFIC_ACTION_SAMPLE (1 << 0)
27 #define TRAFFIC_ACTION_TERMINATE (1 << 1)
28 #define TRAFFIC_ACTION_DISTRIBUTE (1 << 2)
30 #define OPERATOR_COMPARE_LESS_THAN (1<<1)
31 #define OPERATOR_COMPARE_GREATER_THAN (1<<2)
32 #define OPERATOR_COMPARE_EQUAL_TO (1<<3)
33 #define OPERATOR_COMPARE_EXACT_MATCH (1<<4)
35 #define OPERATOR_UNARY_OR (1<<1)
36 #define OPERATOR_UNARY_AND (1<<2)
38 /* struct used to store values [0;65535]
39 * this can be used for port number of protocol
41 #define BGP_PBR_MATCH_VAL_MAX 5
43 struct bgp_pbr_match_val
{
45 uint8_t compare_operator
;
46 uint8_t unary_operator
;
49 #define FRAGMENT_DONT 1
51 #define FRAGMENT_FIRST 4
52 #define FRAGMENT_LAST 8
54 struct bgp_pbr_entry_action
{
55 /* used to store enum bgp_pbr_action_enum enumerate */
59 uint8_t rate_info
[4]; /* IEEE.754.1985 */
61 } r
__attribute__((aligned(8)));
66 vrf_id_t redirect_vrf
;
67 struct _pbr_redirect_ip
{
68 struct in_addr redirect_ip_v4
;
69 struct in6_addr redirect_ip_v6
;
73 } u
__attribute__((aligned(8)));
76 /* BGP Policy Route structure */
77 struct bgp_pbr_entry_main
{
78 #define BGP_PBR_UNDEFINED 0
79 #define BGP_PBR_IPSET 1
80 #define BGP_PBR_IPRULE 2
84 * This is an enum but we are going to treat it as a uint8_t
85 * for purpose of encoding/decoding
90 #define PREFIX_SRC_PRESENT (1 << 0)
91 #define PREFIX_DST_PRESENT (1 << 1)
92 uint8_t match_bitmask_iprule
;
93 uint8_t match_bitmask
;
95 uint8_t match_src_port_num
;
96 uint8_t match_dst_port_num
;
97 uint8_t match_port_num
;
98 uint8_t match_protocol_num
;
99 uint8_t match_icmp_type_num
;
100 uint8_t match_icmp_code_num
;
101 uint8_t match_packet_length_num
;
102 uint8_t match_dscp_num
;
103 uint8_t match_tcpflags_num
;
104 uint8_t match_fragment_num
;
105 uint8_t match_flowlabel_num
;
107 struct prefix src_prefix
;
108 struct prefix dst_prefix
;
109 uint8_t src_prefix_offset
;
110 uint8_t dst_prefix_offset
;
112 #define PROTOCOL_UDP 17
113 #define PROTOCOL_TCP 6
114 #define PROTOCOL_ICMP 1
115 #define PROTOCOL_ICMPV6 58
116 struct bgp_pbr_match_val protocol
[BGP_PBR_MATCH_VAL_MAX
];
117 struct bgp_pbr_match_val src_port
[BGP_PBR_MATCH_VAL_MAX
];
118 struct bgp_pbr_match_val dst_port
[BGP_PBR_MATCH_VAL_MAX
];
119 struct bgp_pbr_match_val port
[BGP_PBR_MATCH_VAL_MAX
];
120 struct bgp_pbr_match_val icmp_type
[BGP_PBR_MATCH_VAL_MAX
];
121 struct bgp_pbr_match_val icmp_code
[BGP_PBR_MATCH_VAL_MAX
];
122 struct bgp_pbr_match_val packet_length
[BGP_PBR_MATCH_VAL_MAX
];
123 struct bgp_pbr_match_val dscp
[BGP_PBR_MATCH_VAL_MAX
];
124 struct bgp_pbr_match_val flow_label
[BGP_PBR_MATCH_VAL_MAX
];
126 struct bgp_pbr_match_val tcpflags
[BGP_PBR_MATCH_VAL_MAX
];
127 struct bgp_pbr_match_val fragment
[BGP_PBR_MATCH_VAL_MAX
];
130 struct bgp_pbr_entry_action actions
[ACTIONS_MAX_NUM
];
135 struct bgp_pbr_interface
{
136 RB_ENTRY(bgp_pbr_interface
) id_entry
;
137 char name
[INTERFACE_NAMSIZ
];
140 RB_HEAD(bgp_pbr_interface_head
, bgp_pbr_interface
);
141 RB_PROTOTYPE(bgp_pbr_interface_head
, bgp_pbr_interface
, id_entry
,
142 bgp_pbr_interface_compare
);
144 extern int bgp_pbr_interface_compare(const struct bgp_pbr_interface
*a
,
145 const struct bgp_pbr_interface
*b
);
147 struct bgp_pbr_config
{
148 struct bgp_pbr_interface_head ifaces_by_name_ipv4
;
149 bool pbr_interface_any_ipv4
;
150 struct bgp_pbr_interface_head ifaces_by_name_ipv6
;
151 bool pbr_interface_any_ipv6
;
154 extern struct bgp_pbr_config
*bgp_pbr_cfg
;
156 struct bgp_pbr_rule
{
160 struct bgp_pbr_action
*action
;
165 bool install_in_progress
;
169 struct bgp_pbr_match
{
170 char ipset_name
[ZEBRA_IPSET_NAME_SIZE
];
172 /* mapped on enum ipset_type
179 uint16_t pkt_len_min
;
180 uint16_t pkt_len_max
;
182 uint16_t tcp_mask_flags
;
190 /* unique identifier for ipset create transaction
194 /* unique identifier for iptable add transaction
199 bool install_in_progress
;
201 bool installed_in_iptable
;
202 bool install_iptable_in_progress
;
204 struct hash
*entry_hash
;
206 struct bgp_pbr_action
*action
;
210 struct bgp_pbr_match_entry
{
211 struct bgp_pbr_match
*backpointer
;
218 uint16_t src_port_min
;
219 uint16_t src_port_max
;
220 uint16_t dst_port_min
;
221 uint16_t dst_port_max
;
227 bool install_in_progress
;
230 struct bgp_pbr_action
{
233 * The Unique identifier of this specific pbrms
244 * nexthop information, or drop information
245 * contains src vrf_id and nh contains dest vrf_id
251 bool install_in_progress
;
257 extern struct bgp_pbr_rule
*bgp_pbr_rule_lookup(vrf_id_t vrf_id
,
260 extern struct bgp_pbr_action
*bgp_pbr_action_rule_lookup(vrf_id_t vrf_id
,
263 extern struct bgp_pbr_match
*bgp_pbr_match_ipset_lookup(vrf_id_t vrf_id
,
266 extern struct bgp_pbr_match_entry
*bgp_pbr_match_ipset_entry_lookup(
267 vrf_id_t vrf_id
, char *name
,
269 extern struct bgp_pbr_match
*bgp_pbr_match_iptable_lookup(vrf_id_t vrf_id
,
272 extern void bgp_pbr_cleanup(struct bgp
*bgp
);
273 extern void bgp_pbr_init(struct bgp
*bgp
);
275 extern uint32_t bgp_pbr_rule_hash_key(const void *arg
);
276 extern bool bgp_pbr_rule_hash_equal(const void *arg1
,
278 extern uint32_t bgp_pbr_action_hash_key(const void *arg
);
279 extern bool bgp_pbr_action_hash_equal(const void *arg1
,
281 extern uint32_t bgp_pbr_match_entry_hash_key(const void *arg
);
282 extern bool bgp_pbr_match_entry_hash_equal(const void *arg1
,
284 extern uint32_t bgp_pbr_match_hash_key(const void *arg
);
285 extern bool bgp_pbr_match_hash_equal(const void *arg1
,
288 void bgp_pbr_print_policy_route(struct bgp_pbr_entry_main
*api
);
290 struct bgp_path_info
;
291 extern void bgp_pbr_update_entry(struct bgp
*bgp
, const struct prefix
*p
,
292 struct bgp_path_info
*new_select
, afi_t afi
,
293 safi_t safi
, bool nlri_update
);
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
);
301 extern int bgp_pbr_build_and_validate_entry(const struct prefix
*p
,
302 struct bgp_path_info
*path
,
303 struct bgp_pbr_entry_main
*api
);
304 #endif /* __BGP_PBR_H__ */