summaryrefslogtreecommitdiffstats
path: root/lib/pbr.h
blob: d8c06e75bd2b429ea4031de30f9b7a95fd99dd20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// SPDX-License-Identifier: GPL-2.0-or-later
/* Policy Based Routing (PBR) main header
 * Copyright (C) 2018 6WIND
 * Portions:
 *      Copyright (c) 2021 The MITRE Corporation.
 *      Copyright (c) 2023 LabN Consulting, L.L.C.
 */

#ifndef _PBR_H
#define _PBR_H

#include <zebra.h>
#include "stream.h"
#include "prefix.h"

#ifdef __cplusplus
extern "C" {
#endif

#define PBR_STR "Policy Based Routing\n"

/*
 * A PBR filter
 *
 * The filter or match criteria in a PBR rule.
 * For simplicity, all supported filters are grouped into a structure rather
 * than delineating further. A bitmask denotes which filters are actually
 * specified.
 */
struct pbr_filter {
	uint32_t filter_bm;
#define PBR_FILTER_SRC_IP         (1 << 0)
#define PBR_FILTER_DST_IP         (1 << 1)
#define PBR_FILTER_SRC_PORT       (1 << 2)
#define PBR_FILTER_DST_PORT       (1 << 3)
#define PBR_FILTER_FWMARK         (1 << 4)
#define PBR_FILTER_IP_PROTOCOL	  (1 << 5)
#define PBR_FILTER_SRC_PORT_RANGE (1 << 6)
#define PBR_FILTER_DST_PORT_RANGE (1 << 7)
#define PBR_FILTER_DSCP		  (1 << 8)
#define PBR_FILTER_ECN		  (1 << 9)
#define PBR_FILTER_PCP            (1 << 10)
#define PBR_FILTER_VLAN_FLAGS     (1 << 11)
#define PBR_FILTER_VLAN_ID        (1 << 12)

#define PBR_DSFIELD_DSCP (0xfc) /* Upper 6 bits of DS field: DSCP */
#define PBR_DSFIELD_ECN (0x03)	/* Lower 2 bits of DS field: BCN */

#define PBR_PCP (0x07)		/* 3-bit value 0..7 for prioritization*/

#define PBR_VLAN_FLAGS_NO_WILD	  0
#define PBR_VLAN_FLAGS_TAGGED	  (1 << 0)
#define PBR_VLAN_FLAGS_UNTAGGED	  (1 << 1)
#define PBR_VLAN_FLAGS_UNTAGGED_0 (1 << 2)

	/* Source and Destination IP address with masks */
	struct prefix src_ip;
	struct prefix dst_ip;

	/* Source and Destination layer 4 (TCP/UDP/etc.) port numbers */
	uint16_t src_port;
	uint16_t dst_port;

	/* Filter by VLAN and prioritization */
	uint8_t pcp;
	uint16_t vlan_id;
	uint16_t vlan_flags;

	/* Filter by Differentiated Services field  */
	uint8_t dsfield; /* DSCP (6 bits) & ECN (2 bits) */

	/* Filter with fwmark */
	uint32_t fwmark;

	/* Filter with the ip protocol */
	uint8_t ip_proto;
};

/*
 * A PBR action
 *
 * The action corresponding to a PBR rule.
 * While the user specifies the action in a particular way, the forwarding
 * plane implementation (Linux only) requires that to be encoded into a
 * route table and the rule then point to that route table; in some cases,
 * the user criteria may directly point to a table too.
 */
struct pbr_action {
	uint32_t flags;

#define PBR_ACTION_TABLE		(1 << 0)
#define PBR_ACTION_QUEUE_ID		(1 << 1)
#define PBR_ACTION_PCP			(1 << 2)
#define PBR_ACTION_VLAN_ID		(1 << 3)
#define PBR_ACTION_VLAN_STRIP_INNER_ANY (1 << 4)
#define PBR_ACTION_SRC_IP		(1 << 5)
#define PBR_ACTION_DST_IP		(1 << 6)
#define PBR_ACTION_SRC_PORT		(1 << 7)
#define PBR_ACTION_DST_PORT		(1 << 8)
#define PBR_ACTION_DSCP			(1 << 9)
#define PBR_ACTION_ECN			(1 << 10)
#define PBR_ACTION_DROP			(1 << 11) /* nexthop == blackhole */

	uint32_t table;
	uint32_t queue_id;

	/* VLAN */
	uint8_t pcp;
	uint16_t vlan_id;

	/* Source and Destination IP addresses */
	union sockunion src_ip;
	union sockunion dst_ip;

	/* Source and Destination layer 4 (TCP/UDP/etc.) port numbers */
	uint32_t src_port;
	uint32_t dst_port;

	/* Differentiated Services field  */
	uint8_t dscp; /* stored here already shifted to upper 6 bits */
	uint8_t ecn;  /* stored here as lower 2 bits */
};

/*
 * A PBR rule
 *
 * This is a combination of the filter criteria and corresponding action.
 * Rules also have a user-defined sequence number which defines the relative
 * order amongst rules.
 */
struct pbr_rule {
	vrf_id_t vrf_id;
	uint8_t family; /* netlink: select which rule database */

	uint32_t seq;
	uint32_t priority;
	uint32_t unique;
	struct pbr_filter filter;
	struct pbr_action action;

	char ifname[INTERFACE_NAMSIZ + 1];
};

/* TCP flags value shared
 * those are values of byte 13 of TCP header
 * as mentioned in rfc793
 */
#define TCP_HEADER_FIN (0x01)
#define TCP_HEADER_SYN (0x02)
#define TCP_HEADER_RST (0x04)
#define TCP_HEADER_PSH (0x08)
#define TCP_HEADER_ACK (0x10)
#define TCP_HEADER_URG (0x20)
#define TCP_HEADER_ALL_FLAGS (TCP_HEADER_FIN | TCP_HEADER_SYN \
			      | TCP_HEADER_RST | TCP_HEADER_PSH \
			      | TCP_HEADER_ACK | TCP_HEADER_URG)

/* Pbr IPTable defines
 * those are common flags shared between BGP and Zebra
 */
#define MATCH_IP_SRC_SET		(1 << 0)
#define MATCH_IP_DST_SET		(1 << 1)
#define MATCH_PORT_SRC_SET		(1 << 2)
#define MATCH_PORT_DST_SET		(1 << 3)
#define MATCH_PORT_SRC_RANGE_SET	(1 << 4)
#define MATCH_PORT_DST_RANGE_SET	(1 << 5)
#define MATCH_DSCP_SET			(1 << 6)
#define MATCH_DSCP_INVERSE_SET		(1 << 7)
#define MATCH_PKT_LEN_INVERSE_SET	(1 << 8)
#define MATCH_FRAGMENT_INVERSE_SET	(1 << 9)
#define MATCH_ICMP_SET			(1 << 10)
#define MATCH_PROTOCOL_SET		(1 << 11)
#define MATCH_FLOW_LABEL_SET		(1 << 12)
#define MATCH_FLOW_LABEL_INVERSE_SET	(1 << 13)

extern int zapi_pbr_rule_encode(struct stream *s, struct pbr_rule *r);
extern bool zapi_pbr_rule_decode(struct stream *s, struct pbr_rule *r);

#ifdef __cplusplus
}
#endif

#endif /* _PBR_H */