Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-or-later
2 : /*
3 : * SRv6 definitions
4 : * Copyright (C) 2020 Hiroki Shirokura, LINE Corporation
5 : */
6 :
7 : #ifndef _FRR_SRV6_H
8 : #define _FRR_SRV6_H
9 :
10 : #include <zebra.h>
11 : #include "prefix.h"
12 : #include "json.h"
13 :
14 : #include <arpa/inet.h>
15 : #include <netinet/in.h>
16 :
17 : #define SRV6_MAX_SIDS 16
18 : #define SRV6_MAX_SEGS 8
19 : #define SRV6_LOCNAME_SIZE 256
20 : #define SRH_BASE_HEADER_LENGTH 8
21 : #define SRH_SEGMENT_LENGTH 16
22 :
23 : #ifdef __cplusplus
24 : extern "C" {
25 : #endif
26 :
27 : #define sid2str(sid, str, size) \
28 : inet_ntop(AF_INET6, sid, str, size)
29 :
30 : /* SRv6 flavors manipulation macros */
31 : #define CHECK_SRV6_FLV_OP(OPS,OP) ((OPS) & (1 << OP))
32 : #define SET_SRV6_FLV_OP(OPS,OP) (OPS) |= (1 << OP)
33 : #define UNSET_SRV6_FLV_OP(OPS,OP) (OPS) &= ~(1 << OP)
34 : #define RESET_SRV6_FLV_OP(OPS) (OPS) = 0
35 :
36 : /* SRv6 Flavors default values */
37 : #define ZEBRA_DEFAULT_SEG6_LOCAL_FLV_LCBLOCK_LEN 32
38 : #define ZEBRA_DEFAULT_SEG6_LOCAL_FLV_LCNODE_FN_LEN 16
39 :
40 : enum seg6_mode_t {
41 : INLINE,
42 : ENCAP,
43 : L2ENCAP,
44 : };
45 :
46 : enum seg6local_action_t {
47 : ZEBRA_SEG6_LOCAL_ACTION_UNSPEC = 0,
48 : ZEBRA_SEG6_LOCAL_ACTION_END = 1,
49 : ZEBRA_SEG6_LOCAL_ACTION_END_X = 2,
50 : ZEBRA_SEG6_LOCAL_ACTION_END_T = 3,
51 : ZEBRA_SEG6_LOCAL_ACTION_END_DX2 = 4,
52 : ZEBRA_SEG6_LOCAL_ACTION_END_DX6 = 5,
53 : ZEBRA_SEG6_LOCAL_ACTION_END_DX4 = 6,
54 : ZEBRA_SEG6_LOCAL_ACTION_END_DT6 = 7,
55 : ZEBRA_SEG6_LOCAL_ACTION_END_DT4 = 8,
56 : ZEBRA_SEG6_LOCAL_ACTION_END_B6 = 9,
57 : ZEBRA_SEG6_LOCAL_ACTION_END_B6_ENCAP = 10,
58 : ZEBRA_SEG6_LOCAL_ACTION_END_BM = 11,
59 : ZEBRA_SEG6_LOCAL_ACTION_END_S = 12,
60 : ZEBRA_SEG6_LOCAL_ACTION_END_AS = 13,
61 : ZEBRA_SEG6_LOCAL_ACTION_END_AM = 14,
62 : ZEBRA_SEG6_LOCAL_ACTION_END_BPF = 15,
63 : ZEBRA_SEG6_LOCAL_ACTION_END_DT46 = 16,
64 : };
65 :
66 : /* Flavor operations for SRv6 End* Behaviors */
67 : enum seg6local_flavor_op {
68 : ZEBRA_SEG6_LOCAL_FLV_OP_UNSPEC = 0,
69 : /* PSP Flavor as per RFC 8986 section #4.16.1 */
70 : ZEBRA_SEG6_LOCAL_FLV_OP_PSP = 1,
71 : /* USP Flavor as per RFC 8986 section #4.16.2 */
72 : ZEBRA_SEG6_LOCAL_FLV_OP_USP = 2,
73 : /* USD Flavor as per RFC 8986 section #4.16.3 */
74 : ZEBRA_SEG6_LOCAL_FLV_OP_USD = 3,
75 : /* NEXT-C-SID Flavor as per draft-ietf-spring-srv6-srh-compression-03
76 : section 4.1 */
77 : ZEBRA_SEG6_LOCAL_FLV_OP_NEXT_CSID = 4,
78 : };
79 :
80 : #define SRV6_SEG_STRLEN 1024
81 :
82 : struct seg6_segs {
83 : size_t num_segs;
84 : struct in6_addr segs[256];
85 : };
86 :
87 : struct seg6local_flavors_info {
88 : /* Flavor operations */
89 : uint32_t flv_ops;
90 :
91 : /* Locator-Block length, expressed in bits */
92 : uint8_t lcblock_len;
93 : /* Locator-Node Function length, expressed in bits */
94 : uint8_t lcnode_func_len;
95 : };
96 :
97 : struct seg6_seg_stack {
98 : uint8_t num_segs;
99 : struct in6_addr seg[0]; /* 1 or more segs */
100 : };
101 :
102 : struct seg6local_context {
103 : struct in_addr nh4;
104 : struct in6_addr nh6;
105 : uint32_t table;
106 : struct seg6local_flavors_info flv;
107 : };
108 :
109 : struct srv6_locator {
110 : char name[SRV6_LOCNAME_SIZE];
111 : struct prefix_ipv6 prefix;
112 :
113 : /*
114 : * Bit length of SRv6 locator described in
115 : * draft-ietf-bess-srv6-services-05#section-3.2.1
116 : */
117 : uint8_t block_bits_length;
118 : uint8_t node_bits_length;
119 : uint8_t function_bits_length;
120 : uint8_t argument_bits_length;
121 :
122 : int algonum;
123 : uint64_t current;
124 : bool status_up;
125 : struct list *chunks;
126 :
127 : uint8_t flags;
128 : #define SRV6_LOCATOR_USID (1 << 0) /* The SRv6 Locator is a uSID Locator */
129 :
130 : QOBJ_FIELDS;
131 : };
132 : DECLARE_QOBJ_TYPE(srv6_locator);
133 :
134 : struct srv6_locator_chunk {
135 : char locator_name[SRV6_LOCNAME_SIZE];
136 : struct prefix_ipv6 prefix;
137 :
138 : /*
139 : * Bit length of SRv6 locator described in
140 : * draft-ietf-bess-srv6-services-05#section-3.2.1
141 : */
142 : uint8_t block_bits_length;
143 : uint8_t node_bits_length;
144 : uint8_t function_bits_length;
145 : uint8_t argument_bits_length;
146 :
147 : /*
148 : * For Zclient communication values
149 : */
150 : uint8_t keep;
151 : uint8_t proto;
152 : uint16_t instance;
153 : uint32_t session_id;
154 :
155 : uint8_t flags;
156 : };
157 :
158 : /*
159 : * SRv6 Endpoint Behavior codepoints, as defined by IANA in
160 : * https://www.iana.org/assignments/segment-routing/segment-routing.xhtml
161 : */
162 : enum srv6_endpoint_behavior_codepoint {
163 : SRV6_ENDPOINT_BEHAVIOR_RESERVED = 0x0000,
164 : SRV6_ENDPOINT_BEHAVIOR_END = 0x0001,
165 : SRV6_ENDPOINT_BEHAVIOR_END_X = 0x0005,
166 : SRV6_ENDPOINT_BEHAVIOR_END_DT6 = 0x0012,
167 : SRV6_ENDPOINT_BEHAVIOR_END_DT4 = 0x0013,
168 : SRV6_ENDPOINT_BEHAVIOR_END_DT46 = 0x0014,
169 : SRV6_ENDPOINT_BEHAVIOR_END_NEXT_CSID = 0x002B,
170 : SRV6_ENDPOINT_BEHAVIOR_END_X_NEXT_CSID = 0x002C,
171 : SRV6_ENDPOINT_BEHAVIOR_END_DT6_USID = 0x003E,
172 : SRV6_ENDPOINT_BEHAVIOR_END_DT4_USID = 0x003F,
173 : SRV6_ENDPOINT_BEHAVIOR_END_DT46_USID = 0x0040,
174 : SRV6_ENDPOINT_BEHAVIOR_OPAQUE = 0xFFFF,
175 : };
176 :
177 : struct nexthop_srv6 {
178 : /* SRv6 localsid info for Endpoint-behaviour */
179 : enum seg6local_action_t seg6local_action;
180 : struct seg6local_context seg6local_ctx;
181 :
182 : /* SRv6 Headend-behaviour */
183 : struct seg6_seg_stack *seg6_segs;
184 : };
185 :
186 : static inline const char *seg6_mode2str(enum seg6_mode_t mode)
187 : {
188 : switch (mode) {
189 : case INLINE:
190 : return "INLINE";
191 : case ENCAP:
192 : return "ENCAP";
193 : case L2ENCAP:
194 : return "L2ENCAP";
195 : default:
196 : return "unknown";
197 : }
198 : }
199 :
200 0 : static inline bool sid_same(
201 : const struct in6_addr *a,
202 : const struct in6_addr *b)
203 : {
204 0 : if (!a && !b)
205 : return true;
206 0 : else if (!(a && b))
207 : return false;
208 : else
209 0 : return memcmp(a, b, sizeof(struct in6_addr)) == 0;
210 : }
211 :
212 0 : static inline bool sid_diff(
213 : const struct in6_addr *a,
214 : const struct in6_addr *b)
215 : {
216 0 : return !sid_same(a, b);
217 : }
218 :
219 :
220 0 : static inline bool sid_zero(const struct seg6_seg_stack *a)
221 : {
222 0 : struct in6_addr zero = {};
223 :
224 0 : assert(a);
225 :
226 0 : return sid_same(&a->seg[0], &zero);
227 : }
228 :
229 0 : static inline bool sid_zero_ipv6(const struct in6_addr *a)
230 : {
231 0 : struct in6_addr zero = {};
232 :
233 0 : return sid_same(&a[0], &zero);
234 : }
235 :
236 0 : static inline void *sid_copy(struct in6_addr *dst,
237 : const struct in6_addr *src)
238 : {
239 0 : return memcpy(dst, src, sizeof(struct in6_addr));
240 : }
241 :
242 : const char *
243 : seg6local_action2str(uint32_t action);
244 :
245 : const char *seg6local_context2str(char *str, size_t size,
246 : const struct seg6local_context *ctx,
247 : uint32_t action);
248 :
249 : int snprintf_seg6_segs(char *str,
250 : size_t size, const struct seg6_segs *segs);
251 :
252 : extern struct srv6_locator *srv6_locator_alloc(const char *name);
253 : extern struct srv6_locator_chunk *srv6_locator_chunk_alloc(void);
254 : extern void srv6_locator_free(struct srv6_locator *locator);
255 : extern void srv6_locator_chunk_free(struct srv6_locator_chunk **chunk);
256 : json_object *srv6_locator_chunk_json(const struct srv6_locator_chunk *chunk);
257 : json_object *srv6_locator_json(const struct srv6_locator *loc);
258 : json_object *srv6_locator_detailed_json(const struct srv6_locator *loc);
259 : json_object *
260 : srv6_locator_chunk_detailed_json(const struct srv6_locator_chunk *chunk);
261 :
262 : #ifdef __cplusplus
263 : }
264 : #endif
265 :
266 : #endif
|