4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <rte_common.h>
34 #include <rte_crypto.h>
36 #include <cmdline_parse_string.h>
37 #include <cmdline_parse_num.h>
38 #include <cmdline_parse_ipaddr.h>
39 #include <cmdline_socket.h>
45 #define PARSE_DELIMITER " \f\n\r\t\v"
47 parse_tokenize_string(char *string, char *tokens[], uint32_t *n_tokens)
51 if ((string == NULL) ||
56 for (i = 0; i < *n_tokens; i++) {
57 tokens[i] = strtok_r(string, PARSE_DELIMITER, &string);
58 if (tokens[i] == NULL)
62 if ((i == *n_tokens) &&
63 (NULL != strtok_r(string, PARSE_DELIMITER, &string)))
74 * inet_pton4(src, dst)
75 * like inet_aton() but without all the hexadecimal and shorthand.
77 * 1 if `src' is a valid dotted quad, else 0.
79 * does not touch `dst' unless it's returning 1.
84 inet_pton4(const char *src, unsigned char *dst)
86 static const char digits[] = "0123456789";
87 int saw_digit, octets, ch;
88 unsigned char tmp[INADDRSZ], *tp;
93 while ((ch = *src++) != '\0') {
96 pch = strchr(digits, ch);
98 unsigned int new = *tp * 10 + (pch - digits);
107 *tp = (unsigned char)new;
108 } else if (ch == '.' && saw_digit) {
119 memcpy(dst, tmp, INADDRSZ);
124 * inet_pton6(src, dst)
125 * convert presentation level address to network order binary form.
127 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
129 * (1) does not touch `dst' unless it's returning 1.
130 * (2) :: in a full address is silently ignored.
132 * inspired by Mark Andrews.
137 inet_pton6(const char *src, unsigned char *dst)
139 static const char xdigits_l[] = "0123456789abcdef",
140 xdigits_u[] = "0123456789ABCDEF";
141 unsigned char tmp[IN6ADDRSZ], *tp = 0, *endp = 0, *colonp = 0;
142 const char *xdigits = 0, *curtok = 0;
143 int ch = 0, saw_xdigit = 0, count_xdigit = 0;
144 unsigned int val = 0;
145 unsigned dbloct_count = 0;
147 memset((tp = tmp), '\0', IN6ADDRSZ);
148 endp = tp + IN6ADDRSZ;
150 /* Leading :: requires some special handling. */
155 saw_xdigit = count_xdigit = 0;
158 while ((ch = *src++) != '\0') {
161 pch = strchr((xdigits = xdigits_l), ch);
163 pch = strchr((xdigits = xdigits_u), ch);
165 if (count_xdigit >= 4)
168 val |= (pch - xdigits);
182 } else if (*src == '\0') {
185 if (tp + sizeof(int16_t) > endp)
187 *tp++ = (unsigned char) ((val >> 8) & 0xff);
188 *tp++ = (unsigned char) (val & 0xff);
195 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
196 inet_pton4(curtok, tp) > 0) {
200 break; /* '\0' was seen by inet_pton4(). */
205 if (tp + sizeof(int16_t) > endp)
207 *tp++ = (unsigned char) ((val >> 8) & 0xff);
208 *tp++ = (unsigned char) (val & 0xff);
211 if (colonp != NULL) {
212 /* if we already have 8 double octets, having a colon
214 if (dbloct_count == 8)
218 * Since some memmove()'s erroneously fail to handle
219 * overlapping regions, we'll do the shift by hand.
221 const int n = tp - colonp;
224 for (i = 1; i <= n; i++) {
225 endp[-i] = colonp[n - i];
232 memcpy(dst, tmp, IN6ADDRSZ);
237 parse_ipv4_addr(const char *token, struct in_addr *ipv4, uint32_t *mask)
239 char ip_str[256] = {0};
242 pch = strchr(token, '/');
244 strncpy(ip_str, token, pch - token);
246 if (is_str_num(pch) != 0)
251 strncpy(ip_str, token, sizeof(ip_str) - 1);
256 if (strlen(ip_str) >= INET_ADDRSTRLEN)
259 if (inet_pton4(ip_str, (unsigned char *)ipv4) != 1)
266 parse_ipv6_addr(const char *token, struct in6_addr *ipv6, uint32_t *mask)
268 char ip_str[256] = {0};
271 pch = strchr(token, '/');
273 strncpy(ip_str, token, pch - token);
275 if (is_str_num(pch) != 0)
280 strncpy(ip_str, token, sizeof(ip_str) - 1);
285 if (strlen(ip_str) >= INET6_ADDRSTRLEN)
288 if (inet_pton6(ip_str, (unsigned char *)ipv6) != 1)
295 parse_range(const char *token, uint16_t *low, uint16_t *high)
306 memset(num_str, 0, 20);
309 while ((ch = *token++) != '\0') {
314 } else if (ch == ':') {
317 range_low = atoi(num_str);
318 memset(num_str, 0, 20);
323 if (strlen(num_str) == 0)
326 range_high = atoi(num_str);
328 *low = (uint16_t)range_low;
329 *high = (uint16_t)range_high;
335 struct cfg_sp_add_cfg_item {
336 cmdline_fixed_string_t sp_keyword;
337 cmdline_multi_string_t multi_string;
341 cfg_sp_add_cfg_item_parsed(void *parsed_result,
342 __rte_unused struct cmdline *cl, void *data)
344 struct cfg_sp_add_cfg_item *params = parsed_result;
346 uint32_t n_tokens = RTE_DIM(tokens);
347 struct parse_status *status = (struct parse_status *)data;
349 APP_CHECK((parse_tokenize_string(params->multi_string, tokens,
350 &n_tokens) == 0), status, "too many arguments");
352 if (status->status < 0)
355 if (strcmp(tokens[0], "ipv4") == 0) {
356 parse_sp4_tokens(tokens, n_tokens, status);
357 if (status->status < 0)
359 } else if (strcmp(tokens[0], "ipv6") == 0) {
360 parse_sp6_tokens(tokens, n_tokens, status);
361 if (status->status < 0)
364 APP_CHECK(0, status, "unrecognizable input %s\n",
370 static cmdline_parse_token_string_t cfg_sp_add_sp_str =
371 TOKEN_STRING_INITIALIZER(struct cfg_sp_add_cfg_item,
374 static cmdline_parse_token_string_t cfg_sp_add_multi_str =
375 TOKEN_STRING_INITIALIZER(struct cfg_sp_add_cfg_item, multi_string,
378 cmdline_parse_inst_t cfg_sp_add_rule = {
379 .f = cfg_sp_add_cfg_item_parsed,
383 (void *) &cfg_sp_add_sp_str,
384 (void *) &cfg_sp_add_multi_str,
390 struct cfg_sa_add_cfg_item {
391 cmdline_fixed_string_t sa_keyword;
392 cmdline_multi_string_t multi_string;
396 cfg_sa_add_cfg_item_parsed(void *parsed_result,
397 __rte_unused struct cmdline *cl, void *data)
399 struct cfg_sa_add_cfg_item *params = parsed_result;
401 uint32_t n_tokens = RTE_DIM(tokens);
402 struct parse_status *status = (struct parse_status *)data;
404 APP_CHECK(parse_tokenize_string(params->multi_string, tokens,
405 &n_tokens) == 0, status, "too many arguments\n");
407 parse_sa_tokens(tokens, n_tokens, status);
410 static cmdline_parse_token_string_t cfg_sa_add_sa_str =
411 TOKEN_STRING_INITIALIZER(struct cfg_sa_add_cfg_item,
414 static cmdline_parse_token_string_t cfg_sa_add_multi_str =
415 TOKEN_STRING_INITIALIZER(struct cfg_sa_add_cfg_item, multi_string,
418 cmdline_parse_inst_t cfg_sa_add_rule = {
419 .f = cfg_sa_add_cfg_item_parsed,
423 (void *) &cfg_sa_add_sa_str,
424 (void *) &cfg_sa_add_multi_str,
430 struct cfg_rt_add_cfg_item {
431 cmdline_fixed_string_t rt_keyword;
432 cmdline_multi_string_t multi_string;
436 cfg_rt_add_cfg_item_parsed(void *parsed_result,
437 __rte_unused struct cmdline *cl, void *data)
439 struct cfg_rt_add_cfg_item *params = parsed_result;
441 uint32_t n_tokens = RTE_DIM(tokens);
442 struct parse_status *status = (struct parse_status *)data;
444 APP_CHECK(parse_tokenize_string(
445 params->multi_string, tokens, &n_tokens) == 0,
446 status, "too many arguments\n");
447 if (status->status < 0)
450 parse_rt_tokens(tokens, n_tokens, status);
453 static cmdline_parse_token_string_t cfg_rt_add_rt_str =
454 TOKEN_STRING_INITIALIZER(struct cfg_rt_add_cfg_item,
457 static cmdline_parse_token_string_t cfg_rt_add_multi_str =
458 TOKEN_STRING_INITIALIZER(struct cfg_rt_add_cfg_item, multi_string,
461 cmdline_parse_inst_t cfg_rt_add_rule = {
462 .f = cfg_rt_add_cfg_item_parsed,
466 (void *) &cfg_rt_add_rt_str,
467 (void *) &cfg_rt_add_multi_str,
472 /** set of cfg items */
473 cmdline_parse_ctx_t ipsec_ctx[] = {
474 (cmdline_parse_inst_t *)&cfg_sp_add_rule,
475 (cmdline_parse_inst_t *)&cfg_sa_add_rule,
476 (cmdline_parse_inst_t *)&cfg_rt_add_rule,
481 parse_cfg_file(const char *cfg_filename)
483 struct cmdline *cl = cmdline_stdin_new(ipsec_ctx, "");
484 FILE *f = fopen(cfg_filename, "r");
485 char str[1024] = {0}, *get_s = NULL;
486 uint32_t line_num = 0;
487 struct parse_status status = {0};
490 rte_panic("Error: invalid file descriptor %s\n", cfg_filename);
495 rte_panic("Error: cannot create cmdline instance\n");
499 cfg_sp_add_rule.data = &status;
500 cfg_sa_add_rule.data = &status;
501 cfg_rt_add_rule.data = &status;
506 get_s = fgets(oneline, 1024, f);
513 if (strlen(oneline) > 1022) {
514 rte_panic("%s:%u: error: "
515 "the line contains more characters the parser can handle\n",
516 cfg_filename, line_num);
520 /* process comment char '#' */
521 if (oneline[0] == '#')
524 pos = strchr(oneline, '#');
528 /* process line concatenator '\' */
529 pos = strchr(oneline, 92);
531 if (pos != oneline+strlen(oneline) - 2) {
532 rte_panic("%s:%u: error: "
533 "no character should exist after '\\'\n",
534 cfg_filename, line_num);
540 if (strlen(oneline) + strlen(str) > 1022) {
541 rte_panic("%s:%u: error: "
542 "the concatenated line contains more characters the parser can handle\n",
543 cfg_filename, line_num);
547 strncpy(str + strlen(str), oneline,
553 /* copy the line to str and process */
554 if (strlen(oneline) + strlen(str) > 1022) {
555 rte_panic("%s:%u: error: "
556 "the line contains more characters the parser can handle\n",
557 cfg_filename, line_num);
560 strncpy(str + strlen(str), oneline,
563 str[strlen(str)] = '\n';
564 if (cmdline_parse(cl, str) < 0) {
565 rte_panic("%s:%u: error: parsing \"%s\" failed\n",
566 cfg_filename, line_num, str);
570 if (status.status < 0) {
571 rte_panic("%s:%u: error: %s", cfg_filename,
572 line_num, status.parse_msg);
576 memset(str, 0, 1024);
579 cmdline_stdin_exit(cl);
586 cmdline_stdin_exit(cl);