3c50b7358a1ee3d6f754c17f5cd9e4cf11b8289d
[vpp.git] / src / vnet / sr / sr.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file
17  * @brief Segment Routing header
18  */
19 #ifndef included_vnet_sr_h
20 #define included_vnet_sr_h
21
22 #include <vnet/vnet.h>
23 #include <vnet/sr/sr_packet.h>
24 #include <vnet/ip/ip6_packet.h>
25
26 #include <openssl/opensslconf.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <openssl/crypto.h>
31 #include <openssl/sha.h>
32 #include <openssl/opensslv.h>
33 #include <openssl/hmac.h>
34
35 /**
36  *    @brief Segment Route tunnel key
37  */
38 typedef struct
39 {
40   ip6_address_t src;
41   ip6_address_t dst;
42 } ip6_sr_tunnel_key_t;
43
44 /**
45  * @brief Segment Route tunnel
46  */
47 typedef struct
48 {
49   /** src, dst address */
50   ip6_sr_tunnel_key_t key;
51
52   /** Pptional tunnel name */
53   u8 *name;
54
55   /** Mask width for FIB entry */
56   u32 dst_mask_width;
57
58   /** First hop, to save 1 elt in the segment list */
59   ip6_address_t first_hop;
60
61   /** RX Fib index */
62   u32 rx_fib_index;
63   /** TX Fib index */
64   u32 tx_fib_index;
65
66   /** The actual ip6 SR header */
67   u8 *rewrite;
68
69   /** Indicates that this tunnel is part of a policy comprising
70      of multiple tunnels. If == ~0 tunnel is not part of a policy */
71   u32 policy_index;
72
73   /**
74    * The FIB node graph linkage
75    */
76   fib_node_t node;
77
78   /**
79    * The FIB entry index for the first hop. We track this so we
80    * don't need an extra lookup for it in the data plane
81    */
82   fib_node_index_t fib_entry_index;
83
84   /**
85    * This tunnel's sibling index in the children of the FIB entry
86    */
87   u32 sibling_index;
88
89   /**
90    * The DPO contributed by the first-hop FIB entry.
91    */
92   dpo_id_t first_hop_dpo;
93 } ip6_sr_tunnel_t;
94
95 /**
96  * @brief Shared secret for keyed-hash message authentication code (HMAC).
97  */
98 typedef struct
99 {
100   u8 *shared_secret;
101 } ip6_sr_hmac_key_t;
102
103 /**
104  * @brief Args required for add/del tunnel.
105  *
106  * Else we end up passing a LOT of parameters around.
107  */
108 typedef struct
109 {
110   /** Key (header imposition case) */
111   ip6_address_t *src_address;
112   ip6_address_t *dst_address;
113   u32 dst_mask_width;
114   u32 rx_table_id;
115   u32 tx_table_id;
116
117   /** optional name argument - for referencing SR tunnel/policy by name */
118   u8 *name;
119
120   /** optional policy name */
121   u8 *policy_name;
122
123   /** segment list, when inserting an ip6 SR header */
124   ip6_address_t *segments;
125
126   /**
127    * "Tag" list, aka segments inserted at the end of the list,
128    * past last_seg
129    */
130   ip6_address_t *tags;
131
132   /** Shared secret => generate SHA-256 HMAC security fields */
133   u8 *shared_secret;
134
135   /** Flags, e.g. cleanup, policy-list flags */
136   u16 flags_net_byte_order;
137
138   /** Delete the tunnnel? */
139   u8 is_del;
140 } ip6_sr_add_del_tunnel_args_t;
141
142 /**
143  * @brief Args for creating a policy.
144  *
145  * Typically used for multicast replication.
146  * ie a multicast address can be associated with a policy,
147  * then replicated across a number of unicast SR tunnels.
148  */
149 typedef struct
150 {
151   /** policy name */
152   u8 *name;
153
154   /** tunnel names */
155   u8 **tunnel_names;
156
157   /** Delete the policy? */
158   u8 is_del;
159 } ip6_sr_add_del_policy_args_t;
160
161 /**
162  * @brief Segment Routing policy.
163  *
164  * Typically used for multicast replication.
165  * ie a multicast address can be associated with a policy,
166  * then replicated across a number of unicast SR tunnels.
167  */
168 typedef struct
169 {
170   /** name of policy */
171   u8 *name;
172
173   /** vector to SR tunnel index */
174   u32 *tunnel_indices;
175
176 } ip6_sr_policy_t;
177
178 /**
179  * @brief Args for mapping of multicast address to policy name.
180  *
181  * Typically used for multicast replication.
182  * ie a multicast address can be associated with a policy,
183  * then replicated across a number of unicast SR tunnels.
184  */
185 typedef struct
186 {
187   /** multicast IP6 address */
188   ip6_address_t *multicast_address;
189
190   /** name of policy to map to */
191   u8 *policy_name;
192
193   /** Delete the mapping */
194   u8 is_del;
195
196 } ip6_sr_add_del_multicastmap_args_t;
197
198 /**
199  * @brief Segment Routing state.
200  */
201 typedef struct
202 {
203   /** pool of tunnel instances, sr entry only */
204   ip6_sr_tunnel_t *tunnels;
205
206   /** find an sr "tunnel" by its outer-IP src/dst */
207   uword *tunnel_index_by_key;
208
209   /** find an sr "tunnel" by its name */
210   uword *tunnel_index_by_name;
211
212   /** policy pool */
213   ip6_sr_policy_t *policies;
214
215   /** find a policy by name */
216   uword *policy_index_by_policy_name;
217
218   /** multicast address to policy mapping */
219   uword *policy_index_by_multicast_address;
220
221   /** hmac key id by shared secret */
222   uword *hmac_key_by_shared_secret;
223
224   /** ip6-rewrite next index for reinstalling the original dst address */
225   u32 ip6_rewrite_sr_next_index;
226
227   /** application API callback */
228   void *sr_local_cb;
229
230   /** validate hmac keys */
231   u8 validate_hmac;
232
233   /** pool of hmac keys */
234   ip6_sr_hmac_key_t *hmac_keys;
235
236   /** Openssl var */
237   EVP_MD *md;
238   /** Openssl var */
239   HMAC_CTX *hmac_ctx;
240
241   /** enable debug spew */
242   u8 is_debug;
243
244   /** convenience */
245   vlib_main_t *vlib_main;
246   /** convenience */
247   vnet_main_t *vnet_main;
248 } ip6_sr_main_t;
249
250 ip6_sr_main_t sr_main;
251
252 format_function_t format_ip6_sr_header;
253 format_function_t format_ip6_sr_header_with_length;
254
255 vlib_node_registration_t ip6_sr_input_node;
256
257 int ip6_sr_add_del_tunnel (ip6_sr_add_del_tunnel_args_t * a);
258 int ip6_sr_add_del_policy (ip6_sr_add_del_policy_args_t * a);
259 int ip6_sr_add_del_multicastmap (ip6_sr_add_del_multicastmap_args_t * a);
260
261 void vnet_register_sr_app_callback (void *cb);
262
263 void sr_fix_hmac (ip6_sr_main_t * sm, ip6_header_t * ip,
264                   ip6_sr_header_t * sr);
265
266 #endif /* included_vnet_sr_h */
267
268 /*
269  * fd.io coding-style-patch-verification: ON
270  *
271  * Local Variables:
272  * eval: (c-set-style "gnu")
273  * End:
274  */