1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
4 #include <rte_common.h>
5 #include <rte_crypto.h>
6 #include <rte_string_fns.h>
8 #include <cmdline_parse_string.h>
9 #include <cmdline_parse_num.h>
10 #include <cmdline_parse_ipaddr.h>
11 #include <cmdline_socket.h>
17 #define PARSE_DELIMITER " \f\n\r\t\v"
19 parse_tokenize_string(char *string, char *tokens[], uint32_t *n_tokens)
23 if ((string == NULL) ||
28 for (i = 0; i < *n_tokens; i++) {
29 tokens[i] = strtok_r(string, PARSE_DELIMITER, &string);
30 if (tokens[i] == NULL)
34 if ((i == *n_tokens) &&
35 (NULL != strtok_r(string, PARSE_DELIMITER, &string)))
46 * inet_pton4(src, dst)
47 * like inet_aton() but without all the hexadecimal and shorthand.
49 * 1 if `src' is a valid dotted quad, else 0.
51 * does not touch `dst' unless it's returning 1.
56 inet_pton4(const char *src, unsigned char *dst)
58 static const char digits[] = "0123456789";
59 int saw_digit, octets, ch;
60 unsigned char tmp[INADDRSZ], *tp;
65 while ((ch = *src++) != '\0') {
68 pch = strchr(digits, ch);
70 unsigned int new = *tp * 10 + (pch - digits);
79 *tp = (unsigned char)new;
80 } else if (ch == '.' && saw_digit) {
91 memcpy(dst, tmp, INADDRSZ);
96 * inet_pton6(src, dst)
97 * convert presentation level address to network order binary form.
99 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
101 * (1) does not touch `dst' unless it's returning 1.
102 * (2) :: in a full address is silently ignored.
104 * inspired by Mark Andrews.
109 inet_pton6(const char *src, unsigned char *dst)
111 static const char xdigits_l[] = "0123456789abcdef",
112 xdigits_u[] = "0123456789ABCDEF";
113 unsigned char tmp[IN6ADDRSZ], *tp = 0, *endp = 0, *colonp = 0;
114 const char *xdigits = 0, *curtok = 0;
115 int ch = 0, saw_xdigit = 0, count_xdigit = 0;
116 unsigned int val = 0;
117 unsigned dbloct_count = 0;
119 memset((tp = tmp), '\0', IN6ADDRSZ);
120 endp = tp + IN6ADDRSZ;
122 /* Leading :: requires some special handling. */
127 saw_xdigit = count_xdigit = 0;
130 while ((ch = *src++) != '\0') {
133 pch = strchr((xdigits = xdigits_l), ch);
135 pch = strchr((xdigits = xdigits_u), ch);
137 if (count_xdigit >= 4)
140 val |= (pch - xdigits);
154 } else if (*src == '\0') {
157 if (tp + sizeof(int16_t) > endp)
159 *tp++ = (unsigned char) ((val >> 8) & 0xff);
160 *tp++ = (unsigned char) (val & 0xff);
167 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
168 inet_pton4(curtok, tp) > 0) {
172 break; /* '\0' was seen by inet_pton4(). */
177 if (tp + sizeof(int16_t) > endp)
179 *tp++ = (unsigned char) ((val >> 8) & 0xff);
180 *tp++ = (unsigned char) (val & 0xff);
183 if (colonp != NULL) {
184 /* if we already have 8 double octets, having a colon
186 if (dbloct_count == 8)
190 * Since some memmove()'s erroneously fail to handle
191 * overlapping regions, we'll do the shift by hand.
193 const int n = tp - colonp;
196 for (i = 1; i <= n; i++) {
197 endp[-i] = colonp[n - i];
204 memcpy(dst, tmp, IN6ADDRSZ);
209 parse_ipv4_addr(const char *token, struct in_addr *ipv4, uint32_t *mask)
211 char ip_str[INET_ADDRSTRLEN] = {0};
214 pch = strchr(token, '/');
216 strlcpy(ip_str, token,
217 RTE_MIN((unsigned int long)(pch - token + 1),
220 if (is_str_num(pch) != 0)
225 strlcpy(ip_str, token, sizeof(ip_str));
229 if (strlen(ip_str) >= INET_ADDRSTRLEN)
232 if (inet_pton4(ip_str, (unsigned char *)ipv4) != 1)
239 parse_ipv6_addr(const char *token, struct in6_addr *ipv6, uint32_t *mask)
241 char ip_str[256] = {0};
244 pch = strchr(token, '/');
246 strlcpy(ip_str, token,
247 RTE_MIN((unsigned int long)(pch - token + 1),
250 if (is_str_num(pch) != 0)
255 strlcpy(ip_str, token, sizeof(ip_str));
260 if (strlen(ip_str) >= INET6_ADDRSTRLEN)
263 if (inet_pton6(ip_str, (unsigned char *)ipv6) != 1)
270 parse_range(const char *token, uint16_t *low, uint16_t *high)
281 memset(num_str, 0, 20);
284 while ((ch = *token++) != '\0') {
289 } else if (ch == ':') {
292 range_low = atoi(num_str);
293 memset(num_str, 0, 20);
298 if (strlen(num_str) == 0)
301 range_high = atoi(num_str);
303 *low = (uint16_t)range_low;
304 *high = (uint16_t)range_high;
310 struct cfg_sp_add_cfg_item {
311 cmdline_fixed_string_t sp_keyword;
312 cmdline_multi_string_t multi_string;
316 cfg_sp_add_cfg_item_parsed(void *parsed_result,
317 __rte_unused struct cmdline *cl, void *data)
319 struct cfg_sp_add_cfg_item *params = parsed_result;
321 uint32_t n_tokens = RTE_DIM(tokens);
322 struct parse_status *status = (struct parse_status *)data;
324 APP_CHECK((parse_tokenize_string(params->multi_string, tokens,
325 &n_tokens) == 0), status, "too many arguments");
327 if (status->status < 0)
330 if (strcmp(tokens[0], "ipv4") == 0) {
331 parse_sp4_tokens(tokens, n_tokens, status);
332 if (status->status < 0)
334 } else if (strcmp(tokens[0], "ipv6") == 0) {
335 parse_sp6_tokens(tokens, n_tokens, status);
336 if (status->status < 0)
339 APP_CHECK(0, status, "unrecognizable input %s\n",
345 static cmdline_parse_token_string_t cfg_sp_add_sp_str =
346 TOKEN_STRING_INITIALIZER(struct cfg_sp_add_cfg_item,
349 static cmdline_parse_token_string_t cfg_sp_add_multi_str =
350 TOKEN_STRING_INITIALIZER(struct cfg_sp_add_cfg_item, multi_string,
353 cmdline_parse_inst_t cfg_sp_add_rule = {
354 .f = cfg_sp_add_cfg_item_parsed,
358 (void *) &cfg_sp_add_sp_str,
359 (void *) &cfg_sp_add_multi_str,
365 struct cfg_sa_add_cfg_item {
366 cmdline_fixed_string_t sa_keyword;
367 cmdline_multi_string_t multi_string;
371 cfg_sa_add_cfg_item_parsed(void *parsed_result,
372 __rte_unused struct cmdline *cl, void *data)
374 struct cfg_sa_add_cfg_item *params = parsed_result;
376 uint32_t n_tokens = RTE_DIM(tokens);
377 struct parse_status *status = (struct parse_status *)data;
379 APP_CHECK(parse_tokenize_string(params->multi_string, tokens,
380 &n_tokens) == 0, status, "too many arguments\n");
382 parse_sa_tokens(tokens, n_tokens, status);
385 static cmdline_parse_token_string_t cfg_sa_add_sa_str =
386 TOKEN_STRING_INITIALIZER(struct cfg_sa_add_cfg_item,
389 static cmdline_parse_token_string_t cfg_sa_add_multi_str =
390 TOKEN_STRING_INITIALIZER(struct cfg_sa_add_cfg_item, multi_string,
393 cmdline_parse_inst_t cfg_sa_add_rule = {
394 .f = cfg_sa_add_cfg_item_parsed,
398 (void *) &cfg_sa_add_sa_str,
399 (void *) &cfg_sa_add_multi_str,
405 struct cfg_rt_add_cfg_item {
406 cmdline_fixed_string_t rt_keyword;
407 cmdline_multi_string_t multi_string;
411 cfg_rt_add_cfg_item_parsed(void *parsed_result,
412 __rte_unused struct cmdline *cl, void *data)
414 struct cfg_rt_add_cfg_item *params = parsed_result;
416 uint32_t n_tokens = RTE_DIM(tokens);
417 struct parse_status *status = (struct parse_status *)data;
419 APP_CHECK(parse_tokenize_string(
420 params->multi_string, tokens, &n_tokens) == 0,
421 status, "too many arguments\n");
422 if (status->status < 0)
425 parse_rt_tokens(tokens, n_tokens, status);
428 static cmdline_parse_token_string_t cfg_rt_add_rt_str =
429 TOKEN_STRING_INITIALIZER(struct cfg_rt_add_cfg_item,
432 static cmdline_parse_token_string_t cfg_rt_add_multi_str =
433 TOKEN_STRING_INITIALIZER(struct cfg_rt_add_cfg_item, multi_string,
436 cmdline_parse_inst_t cfg_rt_add_rule = {
437 .f = cfg_rt_add_cfg_item_parsed,
441 (void *) &cfg_rt_add_rt_str,
442 (void *) &cfg_rt_add_multi_str,
447 /** set of cfg items */
448 cmdline_parse_ctx_t ipsec_ctx[] = {
449 (cmdline_parse_inst_t *)&cfg_sp_add_rule,
450 (cmdline_parse_inst_t *)&cfg_sa_add_rule,
451 (cmdline_parse_inst_t *)&cfg_rt_add_rule,
456 parse_cfg_file(const char *cfg_filename)
458 struct cmdline *cl = cmdline_stdin_new(ipsec_ctx, "");
459 FILE *f = fopen(cfg_filename, "r");
460 char str[1024] = {0}, *get_s = NULL;
461 uint32_t line_num = 0;
462 struct parse_status status = {0};
465 rte_panic("Error: invalid file descriptor %s\n", cfg_filename);
470 rte_panic("Error: cannot create cmdline instance\n");
474 cfg_sp_add_rule.data = &status;
475 cfg_sa_add_rule.data = &status;
476 cfg_rt_add_rule.data = &status;
481 get_s = fgets(oneline, 1024, f);
488 if (strlen(oneline) > 1022) {
489 rte_panic("%s:%u: error: "
490 "the line contains more characters the parser can handle\n",
491 cfg_filename, line_num);
495 /* process comment char '#' */
496 if (oneline[0] == '#')
499 pos = strchr(oneline, '#');
503 /* process line concatenator '\' */
504 pos = strchr(oneline, 92);
506 if (pos != oneline+strlen(oneline) - 2) {
507 rte_panic("%s:%u: error: "
508 "no character should exist after '\\'\n",
509 cfg_filename, line_num);
515 if (strlen(oneline) + strlen(str) > 1022) {
516 rte_panic("%s:%u: error: "
517 "the concatenated line contains more characters the parser can handle\n",
518 cfg_filename, line_num);
522 strcpy(str + strlen(str), oneline);
526 /* copy the line to str and process */
527 if (strlen(oneline) + strlen(str) > 1022) {
528 rte_panic("%s:%u: error: "
529 "the line contains more characters the parser can handle\n",
530 cfg_filename, line_num);
533 strcpy(str + strlen(str), oneline);
535 str[strlen(str)] = '\n';
536 if (cmdline_parse(cl, str) < 0) {
537 rte_panic("%s:%u: error: parsing \"%s\" failed\n",
538 cfg_filename, line_num, str);
542 if (status.status < 0) {
543 rte_panic("%s:%u: error: %s", cfg_filename,
544 line_num, status.parse_msg);
548 memset(str, 0, 1024);
551 cmdline_stdin_exit(cl);
558 cmdline_stdin_exit(cl);