4a919e7988eb20e7e43caf1f8cfd043926fc7f23
[vpp.git] / src / vnet / lisp-cp / lisp_types.h
1 /*
2  * Copyright (c) 2016 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 #ifndef VNET_LISP_GPE_LISP_TYPES_H_
17 #define VNET_LISP_GPE_LISP_TYPES_H_
18
19 #include <vnet/ip/ip.h>
20 #include <vnet/lisp-cp/lisp_cp_messages.h>
21
22 #define SHA1_AUTH_DATA_LEN                  20
23 #define SHA256_AUTH_DATA_LEN                32
24
25 typedef enum
26 {
27   HMAC_NO_KEY = 0,
28   HMAC_SHA_1_96,
29   HMAC_SHA_256_128
30 } lisp_key_type_t;
31
32 uword unformat_hmac_key_id (unformat_input_t * input, va_list * args);
33 u8 *format_hmac_key_id (u8 * s, va_list * args);
34
35 typedef enum
36 {
37   IP4,
38   IP6
39 } ip_address_type_t;
40
41 /* *INDENT-OFF* */
42 typedef CLIB_PACKED(struct ip_address
43 {
44   union
45   {
46     ip4_address_t v4;
47     ip6_address_t v6;
48   } ip;
49   u8 version;
50 }) ip_address_t;
51 /* *INDENT-ON* */
52
53 #define ip_addr_addr(_a) (_a)->ip
54 #define ip_addr_v4(_a) (_a)->ip.v4
55 #define ip_addr_v6(_a) (_a)->ip.v6
56 #define ip_addr_version(_a) (_a)->version
57
58 int ip_address_cmp (const ip_address_t * ip1, const ip_address_t * ip2);
59 void ip_address_copy (ip_address_t * dst, const ip_address_t * src);
60 void ip_address_copy_addr (void *dst, const ip_address_t * src);
61 void ip_address_set (ip_address_t * dst, const void *src, u8 version);
62
63 /* *INDENT-OFF* */
64 typedef CLIB_PACKED(struct ip_prefix
65 {
66   ip_address_t addr;
67   u8 len;
68 }) ip_prefix_t;
69 /* *INDENT-ON* */
70
71 #define ip_prefix_addr(_a) (_a)->addr
72 #define ip_prefix_version(_a) ip_addr_version(&ip_prefix_addr(_a))
73 #define ip_prefix_len(_a) (_a)->len
74 #define ip_prefix_v4(_a) ip_addr_v4(&ip_prefix_addr(_a))
75 #define ip_prefix_v6(_a) ip_addr_v6(&ip_prefix_addr(_a))
76
77 void ip_prefix_normalize (ip_prefix_t * a);
78
79 extern void ip_address_to_fib_prefix (const ip_address_t * addr,
80                                       fib_prefix_t * prefix);
81 extern void ip_prefix_to_fib_prefix (const ip_prefix_t * ipp,
82                                      fib_prefix_t * fibp);
83
84 typedef enum
85 {
86   /* NOTE: ip addresses are left out on purpose. Use max masked ip-prefixes
87    * instead */
88   GID_ADDR_IP_PREFIX,
89   GID_ADDR_LCAF,
90   GID_ADDR_MAC,
91   GID_ADDR_SRC_DST,
92   GID_ADDR_NSH,
93   GID_ADDR_ARP,
94   GID_ADDR_NDP,
95   GID_ADDR_NO_ADDRESS,
96   GID_ADDR_TYPES
97 } gid_address_type_t;
98
99 typedef enum
100 {
101   /* make sure that values corresponds with RFC */
102   LCAF_NULL_BODY = 0,
103   LCAF_AFI_LIST_TYPE,
104   LCAF_INSTANCE_ID,
105   LCAF_SOURCE_DEST = 12,
106   LCAF_NSH = 17,
107   LCAF_TYPES
108 } lcaf_type_t;
109
110 typedef enum fid_addr_type_t_
111 {
112   FID_ADDR_IP_PREF,
113   FID_ADDR_MAC,
114   FID_ADDR_NSH
115 } __attribute__ ((packed)) fid_addr_type_t;
116
117 /* flat address type */
118 typedef struct
119 {
120   union
121   {
122     ip_prefix_t ippref;
123     u8 mac[6];
124     u32 nsh;
125   };
126   fid_addr_type_t type;
127 } fid_address_t;
128
129 typedef fid_address_t dp_address_t;
130
131 #define fid_addr_ippref(_a) (_a)->ippref
132 #define fid_addr_prefix_length(_a) ip_prefix_len(&fid_addr_ippref(_a))
133 #define fid_addr_ip_version(_a) ip_prefix_version(&fid_addr_ippref(_a))
134 #define fid_addr_mac(_a) (_a)->mac
135 #define fid_addr_nsh(_a) (_a)->nsh
136 #define fid_addr_type(_a) (_a)->type
137 u8 *format_fid_address (u8 * s, va_list * args);
138
139 typedef struct
140 {
141   fid_address_t src;
142   fid_address_t dst;
143 } source_dest_t;
144
145 #define sd_dst(_a) (_a)->dst
146 #define sd_src(_a) (_a)->src
147 #define sd_src_ippref(_a) fid_addr_ippref(&sd_src(_a))
148 #define sd_dst_ippref(_a) fid_addr_ippref(&sd_dst(_a))
149 #define sd_src_mac(_a) fid_addr_mac(&sd_src(_a))
150 #define sd_dst_mac(_a) fid_addr_mac(&sd_dst(_a))
151 #define sd_src_type(_a) fid_addr_type(&sd_src(_a))
152 #define sd_dst_type(_a) fid_addr_type(&sd_dst(_a))
153
154 typedef struct
155 {
156   u8 vni_mask_len;
157   u32 vni;
158   struct _gid_address_t *gid_addr;
159 } vni_t;
160
161 #define vni_vni(_a) (_a)->vni
162 #define vni_mask_len(_a) (_a)->vni_mask_len
163 #define vni_gid(_a) (_a)->gid_addr
164
165 typedef struct
166 {
167   u32 spi;
168   u8 si;
169 } nsh_t;
170
171 #define nsh_spi(_a) (_a)->spi
172 #define nsh_si(_a) (_a)->si
173
174 typedef struct
175 {
176   ip_address_t addr;
177   u32 bd;
178 } lcaf_arp_ndp_t;
179
180 #define lcaf_arp_ndp_ip(_a) (_a)->addr
181 #define lcaf_arp_ndp_ip_ver(_a) ip_addr_version(&lcaf_arp_ndp_ip(_a))
182 #define lcaf_arp_ndp_ip4(_a) ip_addr_v4(&lcaf_arp_ndp_ip(_a))
183 #define lcaf_arp_ndp_ip6(_a) ip_addr_v6(&lcaf_arp_ndp_ip(_a))
184 #define lcaf_arp_ndp_bd(_a) (_a)->bd
185
186 typedef struct
187 {
188   /* the union needs to be at the beginning! */
189   union
190   {
191     source_dest_t sd;
192     lcaf_arp_ndp_t arp_ndp;
193     vni_t uni;
194   };
195   u8 type;
196 } lcaf_t;
197
198 #define lcaf_type(_a) (_a)->type
199 #define lcaf_vni(_a) vni_vni(& (_a)->uni)
200 #define lcaf_vni_len(_a) vni_mask_len(& (_a)->uni)
201
202 /* might want to expand this in the future :) */
203 typedef struct _gid_address_t
204 {
205   union
206   {
207     ip_prefix_t ippref;
208     lcaf_t lcaf;
209     u8 mac[6];
210     source_dest_t sd;
211     lcaf_arp_ndp_t arp_ndp;
212     nsh_t nsh;
213   };
214   u8 type;
215   u32 vni;
216   u8 vni_mask;
217 } gid_address_t;
218
219 u8 *format_ip_address (u8 * s, va_list * args);
220 uword unformat_ip_address (unformat_input_t * input, va_list * args);
221 u8 *format_ip_prefix (u8 * s, va_list * args);
222 uword unformat_ip_prefix (unformat_input_t * input, va_list * args);
223 u8 *format_mac_address (u8 * s, va_list * args);
224 uword unformat_mac_address (unformat_input_t * input, va_list * args);
225
226 u16 ip4_address_size_to_put ();
227 u16 ip6_address_size_to_put ();
228 u32 ip4_address_put (u8 * b, ip4_address_t * a);
229 u32 ip6_address_put (u8 * b, ip6_address_t * a);
230
231 u16 ip_address_size_to_write (ip_address_t * a);
232 u16 ip_address_iana_afi (ip_address_t * a);
233 u8 ip_address_max_len (u8 ver);
234 u32 ip_address_put (u8 * b, ip_address_t * a);
235 void ip_address_to_46 (const ip_address_t * addr,
236                        ip46_address_t * a, fib_protocol_t * proto);
237
238 /* LISP AFI codes  */
239 typedef enum
240 {
241   LISP_AFI_NO_ADDR,
242   LISP_AFI_IP,
243   LISP_AFI_IP6,
244   LISP_AFI_LCAF = 16387,
245   LISP_AFI_MAC = 16389
246 } lisp_afi_e;
247
248 u8 *format_gid_address (u8 * s, va_list * args);
249 uword unformat_gid_address (unformat_input_t * input, va_list * args);
250 int gid_address_cmp (gid_address_t * a1, gid_address_t * a2);
251 void gid_address_free (gid_address_t * a);
252
253 u16 gid_address_size_to_put (gid_address_t * a);
254 u16 gid_address_put (u8 * b, gid_address_t * gid);
255 u8 gid_address_len (gid_address_t * a);
256 void *gid_address_cast (gid_address_t * gid, gid_address_type_t type);
257 void gid_address_copy (gid_address_t * dst, gid_address_t * src);
258 u32 gid_address_parse (u8 * offset, gid_address_t * a);
259 void gid_address_ip_set (gid_address_t * dst, void *src, u8 version);
260
261 #define gid_address_type(_a) (_a)->type
262 #define gid_address_ippref(_a) (_a)->ippref
263 #define gid_address_ippref_len(_a) (_a)->ippref.len
264 #define gid_address_ip(_a) ip_prefix_addr(&gid_address_ippref(_a))
265 #define gid_address_ip_version(_a) ip_addr_version(&gid_address_ip(_a))
266 #define gid_address_lcaf(_a) (_a)->lcaf
267 #define gid_address_mac(_a) (_a)->mac
268 #define gid_address_nsh(_a) (_a)->nsh
269 #define gid_address_nsh_spi(_a) nsh_spi(&gid_address_nsh(_a))
270 #define gid_address_nsh_si(_a) nsh_si(&gid_address_nsh(_a))
271 #define gid_address_vni(_a) (_a)->vni
272 #define gid_address_vni_mask(_a) (_a)->vni_mask
273 #define gid_address_sd_dst_ippref(_a) sd_dst_ippref(&(_a)->sd)
274 #define gid_address_sd_src_ippref(_a) sd_src_ippref(&(_a)->sd)
275 #define gid_address_sd_dst_mac(_a) sd_dst_mac(&(_a)->sd)
276 #define gid_address_sd_src_mac(_a) sd_src_mac(&(_a)->sd)
277 #define gid_address_sd(_a) (_a)->sd
278 #define gid_address_sd_src(_a) sd_src(&gid_address_sd(_a))
279 #define gid_address_sd_dst(_a) sd_dst(&gid_address_sd(_a))
280 #define gid_address_sd_src_type(_a) sd_src_type(&gid_address_sd(_a))
281 #define gid_address_sd_dst_type(_a) sd_dst_type(&gid_address_sd(_a))
282 #define gid_address_arp_ndp(_a) (_a)->arp_ndp
283 #define gid_address_arp_ndp_bd(_a) lcaf_arp_ndp_bd(&gid_address_arp_ndp(_a))
284 #define gid_address_arp_ndp_ip(_a) lcaf_arp_ndp_ip(&gid_address_arp_ndp(_a))
285 #define gid_address_arp_ip4(_a) lcaf_arp_ndp_ip4(&gid_address_arp_ndp(_a))
286 #define gid_address_ndp_ip6(_a) lcaf_arp_ndp_ip6(&gid_address_arp_ndp(_a))
287 #define gid_address_ndp_bd gid_address_arp_ndp_bd
288 #define gid_address_arp_bd gid_address_arp_ndp_bd
289
290 /* 'sub'address functions */
291 #define foreach_gid_address_type_fcns  \
292   _(no_addr)                      \
293   _(ip_prefix)                    \
294   _(lcaf)                         \
295   _(mac)                          \
296   _(nsh)                          \
297   _(sd)
298
299 /* *INDENT-OFF* */
300 #define _(_n)                                 \
301 u16    _n ## _size_to_write (void * pref);    \
302 u16    _n ## _write (u8 * p, void * pref);    \
303 u8     _n ## _length (void *a);               \
304 void * _n ## _cast (gid_address_t * a);       \
305 void   _n ## _copy (void * dst , void * src);
306
307 foreach_gid_address_type_fcns
308 #undef _
309 /* *INDENT-ON* */
310
311 always_inline u64
312 mac_to_u64 (u8 * m)
313 {
314   return (*((u64 *) m) & 0xffffffffffff);
315 }
316
317 typedef struct
318 {
319   /* mark locator as local as opposed to remote */
320   u8 local;
321   u8 state;
322   union
323   {
324     u32 sw_if_index;
325     gid_address_t address;
326   };
327   u8 priority;
328   u8 weight;
329   u8 mpriority;
330   u8 mweight;
331   u8 probed;
332 } locator_t;
333
334 u32 locator_parse (void *ptr, locator_t * loc);
335 void locator_copy (locator_t * dst, locator_t * src);
336 u32 locator_cmp (locator_t * l1, locator_t * l2);
337 void locator_free (locator_t * l);
338
339 typedef struct
340 {
341   /* locator-set name */
342   u8 *name;
343
344   /* vector of locator indices */
345   u32 *locator_indices;
346   u8 local;
347 } locator_set_t;
348
349 typedef struct
350 {
351   gid_address_t eid;
352
353   /* index of local locator set */
354   union
355   {
356     u32 locator_set_index;
357     locator_t *locators;        /* used for map register message */
358   };
359
360   u32 ttl;
361   u8 action;
362
363   u8 authoritative:1;
364   u8 local:1;
365   /* valid only for remote mappings */
366   u8 is_static:1;
367   u8 pitr_set:1;
368   u8 nsh_set:1;
369   u8 almost_expired:1;
370   u8 delete_after_expiration:1;
371   u8 rsvd:1;
372
373   u8 *key;
374   lisp_key_type_t key_id;
375   u8 timer_set;
376   counter_t packets;
377 } mapping_t;
378
379 uword
380 unformat_negative_mapping_action (unformat_input_t * input, va_list * args);
381 u8 *format_negative_mapping_action (u8 *, va_list * args);
382
383 typedef struct locator_pair
384 {
385   /* local and remote locators (underlay attachment points) */
386   ip_address_t lcl_loc;
387   ip_address_t rmt_loc;
388
389   u8 priority;
390   u8 weight;
391 } locator_pair_t;
392
393 void
394 build_src_dst (gid_address_t * sd, gid_address_t * src, gid_address_t * dst);
395
396 void gid_address_from_ip (gid_address_t * g, ip_address_t * ip);
397 void gid_to_dp_address (gid_address_t * g, dp_address_t * d);
398
399 #endif /* VNET_LISP_GPE_LISP_TYPES_H_ */
400
401 /*
402  * fd.io coding-style-patch-verification: ON
403  *
404  * Local Variables:
405  * eval: (c-set-style "gnu")
406  * End:
407  */