LISP GPE: initial CP commit and DP improvements
[vpp.git] / vnet / vnet / lisp-cp / lisp_cp_messages.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_CP_MESSAGES_H_
17 #define VNET_LISP_GPE_LISP_CP_MESSAGES_H_
18
19 #include <vnet/vnet.h>
20
21 #define MAX_IP_PKT_LEN 4096
22 #define MAX_IP_HDR_LEN 40  /* without options or IPv6 hdr extensions */
23 #define UDP_HDR_LEN 8
24 #define LISP_DATA_HDR_LEN 8
25 #define LISP_ECM_HDR_LEN 4
26 #define MAX_LISP_MSG_ENCAP_LEN  2*(MAX_IP_HDR_LEN + UDP_HDR_LEN)+ LISP_ECM_HDR_LEN
27 #define MAX_LISP_PKT_ENCAP_LEN  MAX_IP_HDR_LEN + UDP_HDR_LEN + LISP_DATA_HDR_LEN
28
29 #define LISP_CONTROL_PORT 4342
30
31 /*
32  * EID RECORD FIELD
33  */
34
35 /*
36  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37  *    / |   Reserved    | EID mask-len  |        EID-prefix-AFI         |
38  *  Rec +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39  *    \ |                       EID-prefix  ...                         |
40  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41  */
42
43
44 typedef struct _eid_prefix_record_hdr {
45     u8 reserved;
46     u8 eid_prefix_length;
47 } __attribute__ ((__packed__)) eid_record_hdr_t;
48
49 void eid_rec_hdr_init(eid_record_hdr_t *ptr);
50
51 #define EID_REC_CAST(h_) ((eid_record_hdr_t *)(h_))
52 #define EID_REC_MLEN(h_) EID_REC_CAST((h_))->eid_prefix_length
53 #define EID_REC_ADDR(h) (u8 *)(h) + sizeof(eid_record_hdr_t)
54
55 /* LISP Types */
56 typedef enum
57 {
58   NOT_LISP_MSG,
59   LISP_MAP_REQUEST = 1,
60   LISP_MAP_REPLY,
61   LISP_MAP_REGISTER,
62   LISP_MAP_NOTIFY,
63   LISP_INFO_NAT = 7,
64   LISP_ENCAP_CONTROL_TYPE = 8,
65   LISP_MSG_TYPES
66 } lisp_msg_type_e;
67
68 /*
69  * ENCAPSULATED CONTROL MESSAGE
70  */
71
72 /*
73  *     0                   1                   2                   3
74  *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
75  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76  *   / |                       IPv4 or IPv6 Header                     |
77  * OH  |                      (uses RLOC addresses)                    |
78  *   \ |                                                               |
79  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80  *   / |       Source Port = xxxx      |       Dest Port = 4342        |
81  * UDP +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
82  *   \ |           UDP Length          |        UDP Checksum           |
83  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
84  * LH  |Type=8 |S|                  Reserved                           |
85  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86  *   / |                       IPv4 or IPv6 Header                     |
87  * IH  |                  (uses RLOC or EID addresses)                 |
88  *   \ |                                                               |
89  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90  *   / |       Source Port = xxxx      |       Dest Port = yyyy        |
91  * UDP +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92  *   \ |           UDP Length          |        UDP Checksum           |
93  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94  * LCM |                      LISP Control Message                     |
95  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96  */
97
98
99 /*
100  * Encapsulated control message header. This is followed by the IP
101  * header of the encapsulated LISP control message.
102  *
103  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104  *    |Type=8 |S|                 Reserved                            |
105  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106  */
107
108 typedef struct
109 {
110 #if CLIB_ARCH_IS_LITTLE_ENDIAN
111   u8 reserved:3;
112   u8 s_bit:1;
113   u8 type:4;
114 #else
115   u8 type:4;
116   u8 s_bit:1;
117   u8 reserved:3;
118 #endif
119   u8 reserved2[3];
120 } ecm_hdr_t;
121
122 char *ecm_hdr_to_char(ecm_hdr_t *h);
123
124 #define ECM_TYPE(h_) ((ecm_hdr_t *)(h_))->type
125
126 /*
127  * MAP-REQUEST MESSAGE
128  */
129
130 /*
131  * Map-Request Message Format
132  *
133  *       0                   1                   2                   3
134  *       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
135  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136  *      |Type=1 |A|M|P|S|p|s|    Reserved     |   IRC   | Record Count  |
137  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
138  *      |                         Nonce . . .                           |
139  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
140  *      |                         . . . Nonce                           |
141  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142  *      |         Source-EID-AFI        |   Source EID Address  ...     |
143  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144  *      |         ITR-RLOC-AFI 1        |    ITR-RLOC Address 1  ...    |
145  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
146  *      |                              ...                              |
147  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
148  *      |         ITR-RLOC-AFI n        |    ITR-RLOC Address n  ...    |
149  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
150  *    / |   Reserved    | EID mask-len  |        EID-prefix-AFI         |
151  *  Rec +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
152  *    \ |                       EID-prefix  ...                         |
153  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
154  *      |                   Map-Reply Record  ...                       |
155  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
156  *      |                     Mapping Protocol Data                     |
157  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
158  */
159
160
161 /*
162  * Fixed size portion of the map request. Variable size source EID
163  * address, originating ITR RLOC AFIs and addresses and then map
164  * request records follow.
165  */
166 typedef struct
167 {
168 #if CLIB_ARCH_IS_LITTLE_ENDIAN
169     u8 solicit_map_request:1;
170     u8 rloc_probe:1;
171     u8 map_data_present:1;
172     u8 authoritative:1;
173     u8 type:4;
174 #else
175     u8 type:4;
176     u8 authoritative:1;
177     u8 map_data_present:1;
178     u8 rloc_probe:1;
179     u8 solicit_map_request:1;
180 #endif
181 #if CLIB_ARCH_IS_LITTLE_ENDIAN
182     u8 reserved1:6;
183     u8 smr_invoked:1;
184     u8 pitr:1;
185 #else
186     u8 pitr:1;
187     u8 smr_invoked:1;
188     u8 reserved1:6;
189 #endif
190 #if CLIB_ARCH_IS_LITTLE_ENDIAN
191     u8 additional_itr_rloc_count:5;
192     u8 reserved2:3;
193 #else
194     u8 reserved2:3;
195     u8 additional_itr_rloc_count:5;
196 #endif
197     u8 record_count;
198     u64 nonce;
199 }__attribute__ ((__packed__)) map_request_hdr_t;
200
201 void map_request_hdr_init(void *ptr);
202 char *map_request_hdr_to_char(map_request_hdr_t *h);
203
204 #define MREQ_TYPE(h_) (h_)->type
205 #define MREQ_HDR_CAST(h_) ((map_request_hdr_t *)(h_))
206 #define MREQ_REC_COUNT(h_) (MREQ_HDR_CAST(h_))->record_count
207 #define MREQ_RLOC_PROBE(h_) (MREQ_HDR_CAST(h_))->rloc_probe
208 #define MREQ_ITR_RLOC_COUNT(h_) (MREQ_HDR_CAST(h_))->additional_itr_rloc_count
209 #define MREQ_NONCE(h_) (MREQ_HDR_CAST(h_))->nonce
210 #define MREQ_SMR(h_) (MREQ_HDR_CAST(h_))->solicit_map_request
211 #define MREQ_SMR_INVOKED(h_) (MREQ_HDR_CAST(h_))->smr_invoked
212
213 /*
214  * MAP-REPLY MESSAGE
215  */
216
217  /*  Map Reply action codes */
218  #define LISP_ACTION_NO_ACTION           0
219  #define LISP_ACTION_FORWARD             1
220  #define LISP_ACTION_DROP                2
221  #define LISP_ACTION_SEND_MAP_REQUEST    3
222
223  /*
224   * Map-Reply Message Format
225   *
226   *       0                   1                   2                   3
227   *       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
228   *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
229   *      |Type=2 |P|E|S|         Reserved                | Record Count  |
230   *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
231   *      |                         Nonce . . .                           |
232   *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
233   *      |                         . . . Nonce                           |
234   *  +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
235   *  |   |                          Record  TTL                          |
236   *  |   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
237   *  R   | Locator Count | EID mask-len  | ACT |A|      Reserved         |
238   *  e   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
239   *  c   | Rsvd  |  Map-Version Number   |            EID-AFI            |
240   *  o   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
241   *  r   |                          EID-prefix                           |
242   *  d   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
243   *  |  /|    Priority   |    Weight     |  M Priority   |   M Weight    |
244   *  | L +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
245   *  | o |        Unused Flags     |L|p|R|           Loc-AFI             |
246   *  | c +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
247   *  |  \|                            Locator                            |
248   *  +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
249   *      |                     Mapping Protocol Data                     |
250   *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
251   */
252
253  /*
254   * Fixed size portion of the map reply.
255   */
256 typedef struct
257 {
258 #if CLIB_ARCH_IS_LITTLE_ENDIAN
259     u8 reserved1:1;
260     u8 security:1;
261     u8 echo_nonce:1;
262     u8 rloc_probe:1;
263     u8 type:4;
264 #else
265     u8 type:4;
266     u8 rloc_probe:1;
267     u8 echo_nonce:1;
268     u8 security:1;
269     u8 reserved1:1;
270 #endif
271     u8 reserved2;
272     u8 reserved3;
273     u8 record_count;
274     u64 nonce;
275 } __attribute__ ((__packed__)) map_reply_hdr_t;
276
277  void map_reply_hdr_init(void *ptr);
278  char *map_reply_hdr_to_char(map_reply_hdr_t *h);
279
280 #define MREP_HDR_CAST(h_) ((map_reply_hdr_t *)(h_))
281 #define MREP_REC_COUNT(h_) MREP_HDR_CAST(h_)->record_count
282 #define MREP_RLOC_PROBE(h_) MREP_HDR_CAST(h_)->rloc_probe
283 #define MREP_NONCE(h_) MREP_HDR_CAST(h_)->nonce
284
285
286 always_inline lisp_msg_type_e
287 lisp_msg_type (void * b)
288 {
289   ecm_hdr_t * hdr = b;
290   if (!hdr)
291     {
292       return (NOT_LISP_MSG);
293     }
294   return (hdr->type);
295 }
296
297 always_inline void
298 increment_record_count (void * b)
299 {
300   switch (lisp_msg_type (b))
301     {
302     case LISP_MAP_REQUEST:
303       MREQ_REC_COUNT(b) += 1;
304       break;
305     case LISP_MAP_REPLY:
306       MREP_REC_COUNT(b) += 1;
307       break;
308     default:
309       return;
310     }
311 }
312
313
314 /*
315  * LOCATOR FIELD
316  *
317  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
318  *    /|    Priority   |    Weight     |  M Priority   |   M Weight    |
319  *   L +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
320  *   o |        Unused Flags     |L|p|R|           Loc-AFI             |
321  *   c +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
322  *    \|                            Locator                            |
323  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
324  *
325  * Fixed portion of the mapping record locator. Variable length
326  * locator address follows.
327  */
328 typedef struct _locator_hdr {
329     u8 priority;
330     u8 weight;
331     u8 mpriority;
332     u8 mweight;
333     u8 unused1;
334 #ifdef CLIB_ARCH_IS_LITTLE_ENDIAN
335     u8 reachable:1;
336     u8 probed:1;
337     u8 local:1;
338     u8 unused2:5;
339 #else
340     u8 unused2:5;
341     u8 local:1;
342     u8 probed:1;
343     u8 reachable:1;
344 #endif
345 } __attribute__ ((__packed__)) locator_hdr_t;
346
347 #define LOC_CAST(h_) ((locator_hdr_t *)(h_))
348 #define LOC_PROBED(h_) LOC_CAST(h_)->probed
349 #define LOC_PRIORITY(h_) LOC_CAST(h_)->priority
350 #define LOC_WEIGHT(h_) LOC_CAST(h_)->weight
351 #define LOC_MPRIORITY(h_) LOC_CAST(h_)->mpriority
352 #define LOC_MWEIGHT(h_) LOC_CAST(h_)->mweight
353 #define LOC_REACHABLE(h_) LOC_CAST(h_)->reachable
354 #define LOC_LOCAL(h_) LOC_CAST(h_)->local
355 #define LOC_ADDR(h_) ((u8 *)(h_)  + sizeof(locator_hdr_t))
356
357 /*
358  * MAPPING RECORD
359  *
360  * Mapping record used in all LISP control messages.
361  *
362  *  +--->  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
363  *  |      |                          Record  TTL                          |
364  *  |      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
365  *  R      | Locator Count | EID mask-len  | ACT |A|       Reserved        |
366  *  e      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
367  *  c      | Rsvd  |  Map-Version Number   |            EID-AFI            |
368  *  o      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
369  *  r      |                          EID-prefix                           |
370  *  d      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
371  *  |     /|    Priority   |    Weight     |  M Priority   |   M Weight    |
372  *  |    / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
373  *  |  Loc |         Unused Flags    |L|p|R|           Loc-AFI             |
374  *  |    \ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
375  *  |     \|                             Locator                           |
376  *  +--->  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
377  */
378
379 /*
380  * Fixed portion of the mapping record. EID prefix address and
381  * locators follow.
382  */
383
384 typedef struct _mapping_record_hdr_t
385 {
386   u32 ttl;
387   u8 locator_count;
388   u8 eid_prefix_length;
389 #ifdef CLIB_ARCH_IS_LITTLE_ENDIAN
390   u8 reserved1:4;
391   u8 authoritative:1;
392   u8 action:3;
393 #else
394   u8 action :3;
395   u8 authoritative :1;
396   u8 reserved1 :4;
397 #endif
398   u8 reserved2;
399 #ifdef CLIB_ARCH_IS_LITTLE_ENDIAN
400   u8 version_hi:4;
401   u8 reserved3:4;
402 #else
403   u8 reserved3 :4;
404   u8 version_hi :4;
405 #endif
406   u8 version_low;
407 }__attribute__ ((__packed__)) mapping_record_hdr_t;
408
409 void mapping_record_init_hdr(mapping_record_hdr_t *h);
410
411 #define MAP_REC_EID_PLEN(h) ((mapping_record_hdr_t *)(h))->eid_prefix_length
412 #define MAP_REC_LOC_COUNT(h) ((mapping_record_hdr_t *)(h))->locator_count
413 #define MAP_REC_ACTION(h) ((mapping_record_hdr_t *)(h))->action
414 #define MAP_REC_AUTH(h) ((mapping_record_hdr_t *)(h))->authoritative
415 #define MAP_REC_TTL(h) ((mapping_record_hdr_t *)(h))->ttl
416 #define MAP_REC_EID(h) (u8 *)(h)+sizeof(mapping_record_hdr_t)
417 #define MAP_REC_VERSION(h) (h)->version_hi << 8 | (h)->version_low
418
419 typedef enum lisp_actions
420 {
421   ACT_NO_ACTION = 0,
422   ACT_NATIVE_FWD,
423   ACT_SEND_MREQ,
424   ACT_DROP
425 } lisp_action_e;
426
427 typedef enum lisp_authoritative
428 {
429   A_NO_AUTHORITATIVE = 0,
430   A_AUTHORITATIVE
431 } lisp_authoritative_e;
432
433 #endif /* VNET_LISP_GPE_LISP_CP_MESSAGES_H_ */