1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
4 #include <rte_common.h>
5 #include <rte_crypto.h>
7 #include <cmdline_parse_string.h>
8 #include <cmdline_parse_num.h>
9 #include <cmdline_parse_ipaddr.h>
10 #include <cmdline_socket.h>
16 #define PARSE_DELIMITER " \f\n\r\t\v"
18 parse_tokenize_string(char *string, char *tokens[], uint32_t *n_tokens)
22 if ((string == NULL) ||
27 for (i = 0; i < *n_tokens; i++) {
28 tokens[i] = strtok_r(string, PARSE_DELIMITER, &string);
29 if (tokens[i] == NULL)
33 if ((i == *n_tokens) &&
34 (NULL != strtok_r(string, PARSE_DELIMITER, &string)))
45 * inet_pton4(src, dst)
46 * like inet_aton() but without all the hexadecimal and shorthand.
48 * 1 if `src' is a valid dotted quad, else 0.
50 * does not touch `dst' unless it's returning 1.
55 inet_pton4(const char *src, unsigned char *dst)
57 static const char digits[] = "0123456789";
58 int saw_digit, octets, ch;
59 unsigned char tmp[INADDRSZ], *tp;
64 while ((ch = *src++) != '\0') {
67 pch = strchr(digits, ch);
69 unsigned int new = *tp * 10 + (pch - digits);
78 *tp = (unsigned char)new;
79 } else if (ch == '.' && saw_digit) {
90 memcpy(dst, tmp, INADDRSZ);
95 * inet_pton6(src, dst)
96 * convert presentation level address to network order binary form.
98 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
100 * (1) does not touch `dst' unless it's returning 1.
101 * (2) :: in a full address is silently ignored.
103 * inspired by Mark Andrews.
108 inet_pton6(const char *src, unsigned char *dst)
110 static const char xdigits_l[] = "0123456789abcdef",
111 xdigits_u[] = "0123456789ABCDEF";
112 unsigned char tmp[IN6ADDRSZ], *tp = 0, *endp = 0, *colonp = 0;
113 const char *xdigits = 0, *curtok = 0;
114 int ch = 0, saw_xdigit = 0, count_xdigit = 0;
115 unsigned int val = 0;
116 unsigned dbloct_count = 0;
118 memset((tp = tmp), '\0', IN6ADDRSZ);
119 endp = tp + IN6ADDRSZ;
121 /* Leading :: requires some special handling. */
126 saw_xdigit = count_xdigit = 0;
129 while ((ch = *src++) != '\0') {
132 pch = strchr((xdigits = xdigits_l), ch);
134 pch = strchr((xdigits = xdigits_u), ch);
136 if (count_xdigit >= 4)
139 val |= (pch - xdigits);
153 } else if (*src == '\0') {
156 if (tp + sizeof(int16_t) > endp)
158 *tp++ = (unsigned char) ((val >> 8) & 0xff);
159 *tp++ = (unsigned char) (val & 0xff);
166 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
167 inet_pton4(curtok, tp) > 0) {
171 break; /* '\0' was seen by inet_pton4(). */
176 if (tp + sizeof(int16_t) > endp)
178 *tp++ = (unsigned char) ((val >> 8) & 0xff);
179 *tp++ = (unsigned char) (val & 0xff);
182 if (colonp != NULL) {
183 /* if we already have 8 double octets, having a colon
185 if (dbloct_count == 8)
189 * Since some memmove()'s erroneously fail to handle
190 * overlapping regions, we'll do the shift by hand.
192 const int n = tp - colonp;
195 for (i = 1; i <= n; i++) {
196 endp[-i] = colonp[n - i];
203 memcpy(dst, tmp, IN6ADDRSZ);
208 parse_ipv4_addr(const char *token, struct in_addr *ipv4, uint32_t *mask)
210 char ip_str[256] = {0};
213 pch = strchr(token, '/');
215 strncpy(ip_str, token, pch - token);
217 if (is_str_num(pch) != 0)
222 strncpy(ip_str, token, sizeof(ip_str) - 1);
227 if (strlen(ip_str) >= INET_ADDRSTRLEN)
230 if (inet_pton4(ip_str, (unsigned char *)ipv4) != 1)
237 parse_ipv6_addr(const char *token, struct in6_addr *ipv6, 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) >= INET6_ADDRSTRLEN)
259 if (inet_pton6(ip_str, (unsigned char *)ipv6) != 1)
266 parse_range(const char *token, uint16_t *low, uint16_t *high)
277 memset(num_str, 0, 20);
280 while ((ch = *token++) != '\0') {
285 } else if (ch == ':') {
288 range_low = atoi(num_str);
289 memset(num_str, 0, 20);
294 if (strlen(num_str) == 0)
297 range_high = atoi(num_str);
299 *low = (uint16_t)range_low;
300 *high = (uint16_t)range_high;
306 struct cfg_sp_add_cfg_item {
307 cmdline_fixed_string_t sp_keyword;
308 cmdline_multi_string_t multi_string;
312 cfg_sp_add_cfg_item_parsed(void *parsed_result,
313 __rte_unused struct cmdline *cl, void *data)
315 struct cfg_sp_add_cfg_item *params = parsed_result;
317 uint32_t n_tokens = RTE_DIM(tokens);
318 struct parse_status *status = (struct parse_status *)data;
320 APP_CHECK((parse_tokenize_string(params->multi_string, tokens,
321 &n_tokens) == 0), status, "too many arguments");
323 if (status->status < 0)
326 if (strcmp(tokens[0], "ipv4") == 0) {
327 parse_sp4_tokens(tokens, n_tokens, status);
328 if (status->status < 0)
330 } else if (strcmp(tokens[0], "ipv6") == 0) {
331 parse_sp6_tokens(tokens, n_tokens, status);
332 if (status->status < 0)
335 APP_CHECK(0, status, "unrecognizable input %s\n",
341 static cmdline_parse_token_string_t cfg_sp_add_sp_str =
342 TOKEN_STRING_INITIALIZER(struct cfg_sp_add_cfg_item,
345 static cmdline_parse_token_string_t cfg_sp_add_multi_str =
346 TOKEN_STRING_INITIALIZER(struct cfg_sp_add_cfg_item, multi_string,
349 cmdline_parse_inst_t cfg_sp_add_rule = {
350 .f = cfg_sp_add_cfg_item_parsed,
354 (void *) &cfg_sp_add_sp_str,
355 (void *) &cfg_sp_add_multi_str,
361 struct cfg_sa_add_cfg_item {
362 cmdline_fixed_string_t sa_keyword;
363 cmdline_multi_string_t multi_string;
367 cfg_sa_add_cfg_item_parsed(void *parsed_result,
368 __rte_unused struct cmdline *cl, void *data)
370 struct cfg_sa_add_cfg_item *params = parsed_result;
372 uint32_t n_tokens = RTE_DIM(tokens);
373 struct parse_status *status = (struct parse_status *)data;
375 APP_CHECK(parse_tokenize_string(params->multi_string, tokens,
376 &n_tokens) == 0, status, "too many arguments\n");
378 parse_sa_tokens(tokens, n_tokens, status);
381 static cmdline_parse_token_string_t cfg_sa_add_sa_str =
382 TOKEN_STRING_INITIALIZER(struct cfg_sa_add_cfg_item,
385 static cmdline_parse_token_string_t cfg_sa_add_multi_str =
386 TOKEN_STRING_INITIALIZER(struct cfg_sa_add_cfg_item, multi_string,
389 cmdline_parse_inst_t cfg_sa_add_rule = {
390 .f = cfg_sa_add_cfg_item_parsed,
394 (void *) &cfg_sa_add_sa_str,
395 (void *) &cfg_sa_add_multi_str,
401 struct cfg_rt_add_cfg_item {
402 cmdline_fixed_string_t rt_keyword;
403 cmdline_multi_string_t multi_string;
407 cfg_rt_add_cfg_item_parsed(void *parsed_result,
408 __rte_unused struct cmdline *cl, void *data)
410 struct cfg_rt_add_cfg_item *params = parsed_result;
412 uint32_t n_tokens = RTE_DIM(tokens);
413 struct parse_status *status = (struct parse_status *)data;
415 APP_CHECK(parse_tokenize_string(
416 params->multi_string, tokens, &n_tokens) == 0,
417 status, "too many arguments\n");
418 if (status->status < 0)
421 parse_rt_tokens(tokens, n_tokens, status);
424 static cmdline_parse_token_string_t cfg_rt_add_rt_str =
425 TOKEN_STRING_INITIALIZER(struct cfg_rt_add_cfg_item,
428 static cmdline_parse_token_string_t cfg_rt_add_multi_str =
429 TOKEN_STRING_INITIALIZER(struct cfg_rt_add_cfg_item, multi_string,
432 cmdline_parse_inst_t cfg_rt_add_rule = {
433 .f = cfg_rt_add_cfg_item_parsed,
437 (void *) &cfg_rt_add_rt_str,
438 (void *) &cfg_rt_add_multi_str,
443 /** set of cfg items */
444 cmdline_parse_ctx_t ipsec_ctx[] = {
445 (cmdline_parse_inst_t *)&cfg_sp_add_rule,
446 (cmdline_parse_inst_t *)&cfg_sa_add_rule,
447 (cmdline_parse_inst_t *)&cfg_rt_add_rule,
452 parse_cfg_file(const char *cfg_filename)
454 struct cmdline *cl = cmdline_stdin_new(ipsec_ctx, "");
455 FILE *f = fopen(cfg_filename, "r");
456 char str[1024] = {0}, *get_s = NULL;
457 uint32_t line_num = 0;
458 struct parse_status status = {0};
461 rte_panic("Error: invalid file descriptor %s\n", cfg_filename);
466 rte_panic("Error: cannot create cmdline instance\n");
470 cfg_sp_add_rule.data = &status;
471 cfg_sa_add_rule.data = &status;
472 cfg_rt_add_rule.data = &status;
477 get_s = fgets(oneline, 1024, f);
484 if (strlen(oneline) > 1022) {
485 rte_panic("%s:%u: error: "
486 "the line contains more characters the parser can handle\n",
487 cfg_filename, line_num);
491 /* process comment char '#' */
492 if (oneline[0] == '#')
495 pos = strchr(oneline, '#');
499 /* process line concatenator '\' */
500 pos = strchr(oneline, 92);
502 if (pos != oneline+strlen(oneline) - 2) {
503 rte_panic("%s:%u: error: "
504 "no character should exist after '\\'\n",
505 cfg_filename, line_num);
511 if (strlen(oneline) + strlen(str) > 1022) {
512 rte_panic("%s:%u: error: "
513 "the concatenated line contains more characters the parser can handle\n",
514 cfg_filename, line_num);
518 strncpy(str + strlen(str), oneline,
524 /* copy the line to str and process */
525 if (strlen(oneline) + strlen(str) > 1022) {
526 rte_panic("%s:%u: error: "
527 "the line contains more characters the parser can handle\n",
528 cfg_filename, line_num);
531 strncpy(str + strlen(str), oneline,
534 str[strlen(str)] = '\n';
535 if (cmdline_parse(cl, str) < 0) {
536 rte_panic("%s:%u: error: parsing \"%s\" failed\n",
537 cfg_filename, line_num, str);
541 if (status.status < 0) {
542 rte_panic("%s:%u: error: %s", cfg_filename,
543 line_num, status.parse_msg);
547 memset(str, 0, 1024);
550 cmdline_stdin_exit(cl);
557 cmdline_stdin_exit(cl);