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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
|
/*
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* cicn_parser.h: Fast-path, vpp-aware ICN packet parser, used in cicn forwarder.
*/
#ifndef _CICN_PARSER_H_
#define _CICN_PARSER_H_ 1
#if !CICN_VPP_PLUGIN
#error "cicn-internal file included externally"
#endif
#include <ctype.h>
#include <vlib/vlib.h>
#include <cicn/cicn_types.h>
#include <cicn/cicn_std.h>
#ifndef AOK
# define AOK 0
#endif
/*
* Useful macros for working with integers in possibly-unaligned
* buffers and possibly-byteswapped architectures
*/
#define C_GETINT16(n, p) do { \
register uint8_t *t_p = (uint8_t *)(p); \
(n) = ((uint16_t)t_p[0] << 8) \
| ((uint16_t)t_p[1]); \
} while (0)
#define C_GETINT32(n, p) do { \
register uint8_t *t_p = (uint8_t *)(p); \
(n) = ((uint32_t)t_p[0] << 24) \
| ((uint32_t)t_p[1] << 16) \
| ((uint32_t)t_p[2] << 8) \
| ((uint32_t)t_p[3]); \
} while (0)
#define C_GETINT64(n, p) do { \
register uint8_t *t_p = (uint8_t *)(p); \
(n) = ((uint64_t)t_p[0] << 56) \
| ((uint64_t)t_p[1] << 48) \
| ((uint64_t)t_p[2] << 40) \
| ((uint64_t)t_p[3] << 32) \
| ((uint64_t)t_p[4] << 24) \
| ((uint64_t)t_p[5] << 16) \
| ((uint64_t)t_p[6] << 8) \
| ((uint64_t)t_p[7]); \
} while (0)
#define C_PUTINT16(p, n) do { \
register uint16_t t_n = (uint16_t)(n); \
register uint8_t *t_p = (uint8_t *)(p); \
t_p[0] = (uint8_t)(t_n >> 8); \
t_p[1] = (uint8_t)(t_n); \
} while (0)
#define C_PUTINT32(p, n) do { \
register uint32_t t_n = (uint32_t)(n); \
register uint8_t *t_p = (uint8_t *)(p); \
t_p[0] = (uint8_t)(t_n >> 24); \
t_p[1] = (uint8_t)(t_n >> 16); \
t_p[2] = (uint8_t)(t_n >> 8); \
t_p[3] = (uint8_t)t_n; \
} while (0)
#define C_PUTINT64(p, n) do { \
register uint64_t t_n = (uint64_t)(n); \
register uint8_t *t_p = (uint8_t *)(p); \
t_p[0] = (uint8_t)(t_n >> 56); \
t_p[1] = (uint8_t)(t_n >> 48); \
t_p[2] = (uint8_t)(t_n >> 40); \
t_p[3] = (uint8_t)(t_n >> 32); \
t_p[4] = (uint8_t)(t_n >> 24); \
t_p[5] = (uint8_t)(t_n >> 16); \
t_p[6] = (uint8_t)(t_n >> 8); \
t_p[7] = (uint8_t)t_n; \
} while (0)
/*
* Key type codes for header, header tlvs, body tlvs, and child tlvs
*/
enum cicn_pkt_type_e
{
CICN_PKT_TYPE_INTEREST = 0,
CICN_PKT_TYPE_CONTENT = 1,
CICN_PKT_TYPE_NAK = 2,
CICN_PKT_TYPE_CONTROL = 0xA4,
CICN_PKT_TYPE_CONTROL_REQUEST = CICN_PKT_TYPE_CONTROL + 1,
CICN_PKT_TYPE_CONTROL_REPLY
};
enum cicn_msg_type_e
{
CICN_MSG_TYPE_INTEREST = 1,
CICN_MSG_TYPE_CONTENT = 2,
CICN_MSG_TYPE_CONTROL = 0xBEEF,
CICN_MSG_TYPE_ECHO_REQUEST = CICN_MSG_TYPE_CONTROL + 1,
CICN_MSG_TYPE_ECHO_REPLY,
CICN_MSG_TYPE_TRACEROUTE_REQUEST,
CICN_MSG_TYPE_TRACEROUTE_REPLY,
};
enum cicn_hdr_tlv_e
{
CICN_HDR_TLV_INT_LIFETIME = 1,
CICN_HDR_TLV_CACHE_TIME = 2,
};
enum cicn_tlv_e
{
CICN_TLV_NAME = 0,
CICN_TLV_PAYLOAD = 1,
CICN_TLV_PAYLOAD_TYPE = 5,
CICN_TLV_MSG_EXPIRY = 6,
};
enum cicn_name_comp_e
{
CICN_NAME_COMP = 1,
CICN_NAME_COMP_CHUNK = 16
};
enum cicn_msg_err_e
{
CICN_MSG_ERR_NOROUTE = 1,
CICN_MSG_ERR_HOPLIM = 2,
CICN_MSG_ERR_RESOURCES = 3,
CICN_MSG_ERR_CONGESTION = 6,
CICN_MSG_ERR_MTU = 7
};
/*
* Fixed packet header
*/
typedef struct __attribute__ ((__packed__)) cicn_packet_hdr_s
{
uint8_t pkt_ver;
uint8_t pkt_type;
uint16_t pkt_len;
uint8_t pkt_hop_limit;
uint8_t pkt_reserved;
#define pkt_nack_code pkt_reserved
uint8_t pkt_flags;
uint8_t pkt_hdr_len;
} cicn_packet_hdr_t;
typedef struct cicn_pkt_hdr_desc_s
{
int16_t ph_lifetime_idx;
} cicn_pkt_hdr_desc_t;
/* Simple min packet len */
#define CICN_PACKET_MIN_LEN \
(sizeof(cicn_packet_hdr_t) + /*msg tlv*/ 4 + \
/*name tlv*/ 4 + /*name comp*/2 + 1)
/* Protocol versions */
#define CICN_PROTO_VERSION_1 0x01
#define CICN_PROTO_VERSION_CURRENT CICN_PROTO_VERSION_1
/* The versions we can deal with */
#define CICN_PROTO_VERSION_MIN CICN_PROTO_VERSION_CURRENT
#define CICN_PROTO_VERSION_MAX CICN_PROTO_VERSION_CURRENT
/* Default initial hop limit */
#define CICN_DEFAULT_HOP_LIMIT 128
/* Current encoding restricts TLV 'T' and 'L' to two bytes */
#define CICN_TLV_TYPE_LEN 2
#define CICN_TLV_LEN_LEN 2
#define CICN_TLV_HDR_LEN (CICN_TLV_TYPE_LEN + CICN_TLV_LEN_LEN)
#define CICN_TLV_MAX_LEN 0xffff
#define cicn_parse_tlvtype(p) ((((uint8_t*)(p))[0]<<8) | ((uint8_t*)(p))[1])
#define cicn_parse_tlvlength(p) ((((uint8_t*)(p))[2]<<8) | ((uint8_t*)(p))[3])
static inline uint64_t
cicn_parse_getvlv (uint8_t * p, uint8_t * e)
{
u64 v;
/*
* Should check
* if(e <= p || e - p > 8)
* - it's an error.
*/
v = *p++;
while (p < e)
{
v = (v << 8) | *p++;
}
return v;
}
/*
* Wrapper to fill in tlv hdr (type and len) for tlv under constructions.
* Byte-swapps if needed for this CPU.
*/
static inline void
cicn_parse_tlv_hdr_build (uint8_t * tlv, uint16_t type, uint16_t len)
{
C_PUTINT16 (&tlv[0], type);
C_PUTINT16 (&tlv[CICN_TLV_TYPE_LEN], len);
}
/*
* Wrapper to build tlv given the type, the length, and the pre-constructed
* value
*/
static inline void
cicn_parse_tlv_build (uint8_t * tlv, uint16_t type, uint16_t len,
const uint8_t * v)
{
cicn_parse_tlv_hdr_build (tlv, type, len);
clib_memcpy (&tlv[CICN_TLV_HDR_LEN], v, len);
}
/*
* Quickie packet sanity check: check lengths, locate name
*/
static inline int
cicn_parse_pkt (uint8_t * pkt, uint32_t pktlen, uint8_t * type_p,
uint16_t * msg_type_p, uint8_t ** name_p,
uint32_t * namelen_p, cicn_pkt_hdr_desc_t * pkt_hdr_desc)
{
int ret = EINVAL;
uint8_t *startptr;
const uint8_t *endptr;
uint8_t type;
uint16_t sval;
uint8_t hdr_len;
if (pkt == NULL || pktlen < CICN_PACKET_MIN_LEN)
{
goto error;
}
startptr = pkt;
endptr = pkt + pktlen;
if ((*pkt < CICN_PROTO_VERSION_MIN) || (*pkt > CICN_PROTO_VERSION_MAX))
{
goto error;
}
pkt++;
/* TODO -- validate packet type or make the caller do it? */
type = *pkt;
if (type_p)
{
*type_p = type;
}
/* Advance to and check header's packet len */
pkt++;
C_GETINT16 (sval, pkt);
if (startptr + sval > endptr)
{
goto error;
}
/* TODO -- check hop limit here, or let caller do it? */
/* Advance past hop limit and reserved bits */
pkt += 4;
/* TODO -- any 'flags' to check? */
/* Advance to header-len field */
pkt++;
hdr_len = *pkt;
/* Check header-len; must be enough room for at least a message tlv and
* a name tlv.
*/
if ((startptr + hdr_len) > (endptr - 4 /*msg */ - 4 /*name */ ))
{
goto error;
}
/* header options we care about */
pkt_hdr_desc->ph_lifetime_idx = -1;
uint8_t *hdr_tlv = pkt + 1;
pkt = startptr + hdr_len;
while (hdr_tlv < pkt)
{
uint16_t hdr_tlv_type = cicn_parse_tlvtype (hdr_tlv);
uint16_t hdr_tlv_len =
CICN_TLV_HDR_LEN + cicn_parse_tlvlength (hdr_tlv);
if (hdr_tlv + hdr_tlv_len > pkt)
{
goto error;
}
switch (hdr_tlv_type)
{
case CICN_HDR_TLV_INT_LIFETIME:
if (type == CICN_PKT_TYPE_INTEREST)
{
pkt_hdr_desc->ph_lifetime_idx = hdr_tlv - startptr;
}
case CICN_HDR_TLV_CACHE_TIME:
if (type == CICN_PKT_TYPE_CONTENT)
{
pkt_hdr_desc->ph_lifetime_idx = hdr_tlv - startptr;
}
break;
default:
break;
}
hdr_tlv += hdr_tlv_len;
}
/* Capture message type. TODO -- validate/enforce msg types. */
C_GETINT16 (sval, pkt);
if (msg_type_p)
{
*msg_type_p = sval;
}
pkt += 2;
/* Check len of message tlv. TODO -- not checking for other per-msg tlvs */
C_GETINT16 (sval, pkt);
if (((pkt + sval + CICN_TLV_LEN_LEN) > endptr) || (sval < 4 /*name */ ))
{
goto error;
}
pkt += 2;
/* Must find name first in the 'message' */
C_GETINT16 (sval, pkt);
if (sval != CICN_TLV_NAME)
{
goto error;
}
/* Capture start of name */
if (name_p)
{
*name_p = pkt;
}
pkt += 2;
/* Validate len of name tlv
* - zero _is_ a validate name len
* - TODO should compare embedded name len with containing message tlv len
*/
C_GETINT16 (sval, pkt);
if ((pkt + sval + CICN_TLV_LEN_LEN) > endptr)
{
goto error;
}
if (namelen_p)
{
/* Return the whole length from the start of the Name tlv,
* including the T and L octets.
*/
*namelen_p = sval + CICN_TLV_TYPE_LEN + CICN_TLV_LEN_LEN;
}
/* Looks ok so far... */
ret = AOK;
return (ret);
error:
if (type_p)
{
*type_p = 0; // compiler warning
}
if (msg_type_p)
{
*msg_type_p = 0; // compiler warning
}
return (ret);
}
/*
* Process optional time-based Hop-by-hop headers.
* Packet already verified for sanity by cicn_parse_pkt().
* An Interest Lifetime TLV will affect the PIT timeout
* value, or whether the interest should be put in the PIT
* at all (if the value is 0 then no content is expected).
* Caching will use the Recommended Cache Time TLV.
*/
static inline int
cicn_parse_hdr_time_ms (uint8_t * pkt, cicn_pkt_hdr_desc_t * pkt_hdr_desc,
uint16_t type, uint64_t * time_res)
{
uint8_t *p;
uint16_t len;
uint64_t v;
if (pkt_hdr_desc->ph_lifetime_idx == -1)
{
return (ENOENT);
}
p = pkt + pkt_hdr_desc->ph_lifetime_idx;
len = cicn_parse_tlvlength (p);
switch (type)
{
case CICN_HDR_TLV_INT_LIFETIME:
if (len > 8)
{
return (ENOENT);
}
v =
cicn_parse_getvlv (p + CICN_TLV_HDR_LEN, p + CICN_TLV_HDR_LEN + len);
break;
case CICN_HDR_TLV_CACHE_TIME:
if (len != 8)
{
return (ENOENT);
}
C_GETINT64 (v, p + CICN_TLV_HDR_LEN);
break;
default:
return (ENOENT);
break;
}
*time_res = v;
return AOK;
}
/*
* skip over pkt_hdr to msg.
* pkt_hdr must have already been verified by cicn_parse_pkt()
*/
static inline uint8_t *
cicn_parse_pkt2msg (cicn_packet_hdr_t * pkt_hdr)
{
uint8_t *pkt_hdr_ptr = (uint8_t *) pkt_hdr;
return (&pkt_hdr_ptr[pkt_hdr->pkt_hdr_len]);
}
/*
* Utility to convert a string into a series of name-components. We use this
* in cli handling, for example. We write into 'buf', and we return the
* number of octets used, or an error < 0. This only creates name-comps: it's
* up to the caller to create a complete name tlv if that's needed.
* - obuf holds result
* - obuflen is size of result buffer
* - str is name in "/"-separated ascii
* - chunk_name specifies whether the name's last component should be
* chunk name component rather than generic name component.
*
* This is pretty basic right now:
* - the '/' character is the separator
* - binary octets (via escapes) not supported
* - instring component type specification not supported
* - not much validation of the input string.
*/
static inline int
cicn_parse_name_comps_from_str_inline (uint8_t * obuf, int obuflen,
const char *str, int chunk_name)
{
int ret = -EINVAL;
int i, used, start;
uint8_t *obufp;
uint8_t *last_comp = NULL; // chunk component, if chunk_name != 0
if (obuf == NULL || str == NULL)
{
goto done;
}
/* Special case empty string, which means a valid name with no components.
*/
if (str[0] == '\000')
{
ret = 0;
goto done;
}
/* Let's see how many slashes there are, so we can pre-check the
* buffer space we'll need. There is the special-case of a single
* '/', which means a single empty name-component.
*/
used = (str[0] == '/' || str[0] == '\000') ? 0 : 1;
for (i = 0; str[i] != 0; i++)
{
if (str[i] == '/')
{
used++;
}
}
/* Estimate safe buf len required */
if (obuflen < (i + (4 * used)))
{
ret = -ENOSPC;
goto done;
}
/* Convert to name-comp encoding */
start = (str[0] == '/') ? 1 : 0;
for (i = start, obufp = obuf;; i++)
{
if ((str[i] == '/') || ((str[i] == '\000') && (i > start)))
{
last_comp = obufp;
C_PUTINT16 (obufp, CICN_NAME_COMP);
obufp += CICN_TLV_TYPE_LEN;
C_PUTINT16 (obufp, (i - start));
obufp += CICN_TLV_LEN_LEN;
memcpy (obufp, str + start, i - start);
obufp += (i - start);
start = i + 1;
}
if (str[i] == 0)
{
ret = obufp - obuf;
break;
}
}
if (chunk_name && (last_comp != NULL))
{
C_PUTINT16 (last_comp, CICN_NAME_COMP_CHUNK);
}
done:
return (ret);
}
/*
* Utility to convert from tlv-encoded prefix to string (no leading name tlv),
* for cli output e.g. See also cicn_parse_name_to_str()
*
* For resultant buf, return strlen(buf) in *str_len_res
*/
static inline int
cicn_parse_prefix_to_str (char *buf, int bufsize, const uint8_t * prefix,
int pfxlen, int *str_len_res)
{
int i, str_len, ret = EINVAL;
uint16_t sval;
str_len = 0;
if ((buf == NULL) || (bufsize < 1) || (prefix == NULL) || (pfxlen < 0))
{
goto done;
}
/* Special-case empty prefix */
if (pfxlen == 0)
{
*buf = '\0';
ret = AOK;
goto done;
}
for (i = 0;; i++)
{
if (i >= pfxlen)
{
break;
}
/* Must have at least T + L */
if ((pfxlen - i) < 4)
{
break;
}
/* Skip 'T' */
i += 2;
C_GETINT16 (sval, (prefix + i));
/* Must not overrun 'prefix': error */
if ((i + 2 + sval) > pfxlen)
{
goto done;
}
/* Advance past 'L' */
i += 2;
if (str_len >= bufsize)
{
ret = ENOSPC;
goto done;
}
else
{
buf[str_len++] = '/';
}
while (sval > 0)
{
if (prefix[i] == '\\' || !isprint (prefix[i]))
{
int len;
if (prefix[i] == '\\')
{
len =
snprintf (&buf[str_len], bufsize - str_len, "%s", "\\\\");
}
else
{
len =
snprintf (&buf[str_len], bufsize - str_len, "\\%03o",
prefix[i]);
}
if (len < 0)
{
goto done;
}
str_len += len;
if (str_len >= bufsize)
{
ret = ENOSPC;
goto done;
}
}
else
{
if (str_len >= bufsize)
{
ret = ENOSPC;
goto done;
}
else
{
buf[str_len++] = prefix[i];
}
}
sval--;
if (sval > 0)
{
i++;
}
}
} /* End for... */
if (str_len >= bufsize)
{
ret = ENOSPC; // no space for terminal \0, added below
goto done;
}
ret = AOK; // success
done:
if (bufsize <= 0)
{
str_len = 0;
}
else
{
if (str_len >= bufsize)
{
str_len = bufsize - 1;
}
buf[str_len] = '\000';
}
if (str_len_res)
{
*str_len_res = str_len;
}
return (ret);
}
/*
* Convert name (including name tlv header) to printable buffer
* For resultant buf, return strlen(buf) in *str_len_res
*/
static inline int
cicn_parse_name_to_str (char *buf, int bufsize, const uint8_t * name,
int namelen, int *str_len_res)
{
int ret;
uint16_t sval;
if (namelen < CICN_TLV_HDR_LEN)
{
return (EINVAL);
}
C_GETINT16 (sval, &name[0]);
if (sval != CICN_TLV_NAME)
{
return (EINVAL);
}
C_GETINT16 (sval, &name[CICN_TLV_TYPE_LEN]);
if (sval != namelen - CICN_TLV_HDR_LEN)
{ /* Must not overrun name */
return (EINVAL);
}
ret =
cicn_parse_prefix_to_str (buf, bufsize, name + CICN_TLV_HDR_LEN,
namelen - CICN_TLV_HDR_LEN, str_len_res);
return (ret);
}
int
cicn_parse_name_comps_from_str (uint8_t * obuf, int obuflen, const char *str,
cicn_rd_t * cicn_rd);
int cicn_parse_name_from_str (uint8_t * obuf, int obuflen, const char *str,
int is_chunk_name, cicn_rd_t * cicn_rd);
#endif /* _CICN_PARSER_H_ */
|