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>
18 #define PARSE_DELIMITER " \f\n\r\t\v"
20 parse_tokenize_string(char *string, char *tokens[], uint32_t *n_tokens)
24 if ((string == NULL) ||
29 for (i = 0; i < *n_tokens; i++) {
30 tokens[i] = strtok_r(string, PARSE_DELIMITER, &string);
31 if (tokens[i] == NULL)
35 if ((i == *n_tokens) &&
36 (NULL != strtok_r(string, PARSE_DELIMITER, &string)))
47 * inet_pton4(src, dst)
48 * like inet_aton() but without all the hexadecimal and shorthand.
50 * 1 if `src' is a valid dotted quad, else 0.
52 * does not touch `dst' unless it's returning 1.
57 inet_pton4(const char *src, unsigned char *dst)
59 static const char digits[] = "0123456789";
60 int saw_digit, octets, ch;
61 unsigned char tmp[INADDRSZ], *tp;
66 while ((ch = *src++) != '\0') {
69 pch = strchr(digits, ch);
71 unsigned int new = *tp * 10 + (pch - digits);
80 *tp = (unsigned char)new;
81 } else if (ch == '.' && saw_digit) {
92 memcpy(dst, tmp, INADDRSZ);
97 * inet_pton6(src, dst)
98 * convert presentation level address to network order binary form.
100 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
102 * (1) does not touch `dst' unless it's returning 1.
103 * (2) :: in a full address is silently ignored.
105 * inspired by Mark Andrews.
110 inet_pton6(const char *src, unsigned char *dst)
112 static const char xdigits_l[] = "0123456789abcdef",
113 xdigits_u[] = "0123456789ABCDEF";
114 unsigned char tmp[IN6ADDRSZ], *tp = 0, *endp = 0, *colonp = 0;
115 const char *xdigits = 0, *curtok = 0;
116 int ch = 0, saw_xdigit = 0, count_xdigit = 0;
117 unsigned int val = 0;
118 unsigned dbloct_count = 0;
120 memset((tp = tmp), '\0', IN6ADDRSZ);
121 endp = tp + IN6ADDRSZ;
123 /* Leading :: requires some special handling. */
128 saw_xdigit = count_xdigit = 0;
131 while ((ch = *src++) != '\0') {
134 pch = strchr((xdigits = xdigits_l), ch);
136 pch = strchr((xdigits = xdigits_u), ch);
138 if (count_xdigit >= 4)
141 val |= (pch - xdigits);
155 } else if (*src == '\0') {
158 if (tp + sizeof(int16_t) > endp)
160 *tp++ = (unsigned char) ((val >> 8) & 0xff);
161 *tp++ = (unsigned char) (val & 0xff);
168 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
169 inet_pton4(curtok, tp) > 0) {
173 break; /* '\0' was seen by inet_pton4(). */
178 if (tp + sizeof(int16_t) > endp)
180 *tp++ = (unsigned char) ((val >> 8) & 0xff);
181 *tp++ = (unsigned char) (val & 0xff);
184 if (colonp != NULL) {
185 /* if we already have 8 double octets, having a colon
187 if (dbloct_count == 8)
191 * Since some memmove()'s erroneously fail to handle
192 * overlapping regions, we'll do the shift by hand.
194 const int n = tp - colonp;
197 for (i = 1; i <= n; i++) {
198 endp[-i] = colonp[n - i];
205 memcpy(dst, tmp, IN6ADDRSZ);
210 parse_ipv4_addr(const char *token, struct in_addr *ipv4, uint32_t *mask)
212 char ip_str[INET_ADDRSTRLEN] = {0};
215 pch = strchr(token, '/');
217 strlcpy(ip_str, token,
218 RTE_MIN((unsigned int long)(pch - token + 1),
221 if (is_str_num(pch) != 0)
226 strlcpy(ip_str, token, sizeof(ip_str));
230 if (strlen(ip_str) >= INET_ADDRSTRLEN)
233 if (inet_pton4(ip_str, (unsigned char *)ipv4) != 1)
240 parse_ipv6_addr(const char *token, struct in6_addr *ipv6, uint32_t *mask)
242 char ip_str[256] = {0};
245 pch = strchr(token, '/');
247 strlcpy(ip_str, token,
248 RTE_MIN((unsigned int long)(pch - token + 1),
251 if (is_str_num(pch) != 0)
256 strlcpy(ip_str, token, sizeof(ip_str));
261 if (strlen(ip_str) >= INET6_ADDRSTRLEN)
264 if (inet_pton6(ip_str, (unsigned char *)ipv6) != 1)
271 parse_range(const char *token, uint16_t *low, uint16_t *high)
282 memset(num_str, 0, 20);
285 while ((ch = *token++) != '\0') {
290 } else if (ch == ':') {
293 range_low = atoi(num_str);
294 memset(num_str, 0, 20);
299 if (strlen(num_str) == 0)
302 range_high = atoi(num_str);
304 *low = (uint16_t)range_low;
305 *high = (uint16_t)range_high;
311 * helper function for parse_mac, parse one section of the ether addr.
314 parse_uint8x16(const char *s, uint8_t *v, uint8_t ls)
320 t = strtoul(s, &end, 16);
321 if (errno != 0 || end[0] != ls || t > UINT8_MAX)
328 parse_mac(const char *str, struct rte_ether_addr *addr)
332 static const uint8_t stop_sym[RTE_DIM(addr->addr_bytes)] = {
341 for (i = 0; i != RTE_DIM(addr->addr_bytes); i++) {
342 str = parse_uint8x16(str, addr->addr_bytes + i, stop_sym[i]);
351 struct cfg_sp_add_cfg_item {
352 cmdline_fixed_string_t sp_keyword;
353 cmdline_multi_string_t multi_string;
357 cfg_sp_add_cfg_item_parsed(void *parsed_result,
358 __rte_unused struct cmdline *cl, void *data)
360 struct cfg_sp_add_cfg_item *params = parsed_result;
362 uint32_t n_tokens = RTE_DIM(tokens);
363 struct parse_status *status = (struct parse_status *)data;
365 APP_CHECK((parse_tokenize_string(params->multi_string, tokens,
366 &n_tokens) == 0), status, "too many arguments");
368 if (status->status < 0)
371 if (strcmp(tokens[0], "ipv4") == 0) {
372 parse_sp4_tokens(tokens, n_tokens, status);
373 if (status->status < 0)
375 } else if (strcmp(tokens[0], "ipv6") == 0) {
376 parse_sp6_tokens(tokens, n_tokens, status);
377 if (status->status < 0)
380 APP_CHECK(0, status, "unrecognizable input %s\n",
386 static cmdline_parse_token_string_t cfg_sp_add_sp_str =
387 TOKEN_STRING_INITIALIZER(struct cfg_sp_add_cfg_item,
390 static cmdline_parse_token_string_t cfg_sp_add_multi_str =
391 TOKEN_STRING_INITIALIZER(struct cfg_sp_add_cfg_item, multi_string,
394 cmdline_parse_inst_t cfg_sp_add_rule = {
395 .f = cfg_sp_add_cfg_item_parsed,
399 (void *) &cfg_sp_add_sp_str,
400 (void *) &cfg_sp_add_multi_str,
406 struct cfg_sa_add_cfg_item {
407 cmdline_fixed_string_t sa_keyword;
408 cmdline_multi_string_t multi_string;
412 cfg_sa_add_cfg_item_parsed(void *parsed_result,
413 __rte_unused struct cmdline *cl, void *data)
415 struct cfg_sa_add_cfg_item *params = parsed_result;
417 uint32_t n_tokens = RTE_DIM(tokens);
418 struct parse_status *status = (struct parse_status *)data;
420 APP_CHECK(parse_tokenize_string(params->multi_string, tokens,
421 &n_tokens) == 0, status, "too many arguments\n");
423 parse_sa_tokens(tokens, n_tokens, status);
426 static cmdline_parse_token_string_t cfg_sa_add_sa_str =
427 TOKEN_STRING_INITIALIZER(struct cfg_sa_add_cfg_item,
430 static cmdline_parse_token_string_t cfg_sa_add_multi_str =
431 TOKEN_STRING_INITIALIZER(struct cfg_sa_add_cfg_item, multi_string,
434 cmdline_parse_inst_t cfg_sa_add_rule = {
435 .f = cfg_sa_add_cfg_item_parsed,
439 (void *) &cfg_sa_add_sa_str,
440 (void *) &cfg_sa_add_multi_str,
446 struct cfg_rt_add_cfg_item {
447 cmdline_fixed_string_t rt_keyword;
448 cmdline_multi_string_t multi_string;
452 cfg_rt_add_cfg_item_parsed(void *parsed_result,
453 __rte_unused struct cmdline *cl, void *data)
455 struct cfg_rt_add_cfg_item *params = parsed_result;
457 uint32_t n_tokens = RTE_DIM(tokens);
458 struct parse_status *status = (struct parse_status *)data;
460 APP_CHECK(parse_tokenize_string(
461 params->multi_string, tokens, &n_tokens) == 0,
462 status, "too many arguments\n");
463 if (status->status < 0)
466 parse_rt_tokens(tokens, n_tokens, status);
469 static cmdline_parse_token_string_t cfg_rt_add_rt_str =
470 TOKEN_STRING_INITIALIZER(struct cfg_rt_add_cfg_item,
473 static cmdline_parse_token_string_t cfg_rt_add_multi_str =
474 TOKEN_STRING_INITIALIZER(struct cfg_rt_add_cfg_item, multi_string,
477 cmdline_parse_inst_t cfg_rt_add_rule = {
478 .f = cfg_rt_add_cfg_item_parsed,
482 (void *) &cfg_rt_add_rt_str,
483 (void *) &cfg_rt_add_multi_str,
489 struct cfg_flow_add_cfg_item {
490 cmdline_fixed_string_t flow_keyword;
491 cmdline_multi_string_t multi_string;
495 cfg_flow_add_cfg_item_parsed(void *parsed_result,
496 __rte_unused struct cmdline *cl, void *data)
498 struct cfg_flow_add_cfg_item *params = parsed_result;
500 uint32_t n_tokens = RTE_DIM(tokens);
501 struct parse_status *status = (struct parse_status *)data;
503 APP_CHECK(parse_tokenize_string(
504 params->multi_string, tokens, &n_tokens) == 0,
505 status, "too many arguments\n");
506 if (status->status < 0)
509 parse_flow_tokens(tokens, n_tokens, status);
512 static cmdline_parse_token_string_t cfg_flow_add_flow_str =
513 TOKEN_STRING_INITIALIZER(struct cfg_flow_add_cfg_item,
514 flow_keyword, "flow");
516 static cmdline_parse_token_string_t cfg_flow_add_multi_str =
517 TOKEN_STRING_INITIALIZER(struct cfg_flow_add_cfg_item, multi_string,
520 cmdline_parse_inst_t cfg_flow_add_rule = {
521 .f = cfg_flow_add_cfg_item_parsed,
525 (void *) &cfg_flow_add_flow_str,
526 (void *) &cfg_flow_add_multi_str,
531 /* neigh add parse */
532 struct cfg_neigh_add_item {
533 cmdline_fixed_string_t neigh;
534 cmdline_fixed_string_t pstr;
536 cmdline_fixed_string_t mac;
540 cfg_parse_neigh(void *parsed_result, __rte_unused struct cmdline *cl,
544 struct cfg_neigh_add_item *res;
545 struct parse_status *st;
546 struct rte_ether_addr mac;
550 rc = parse_mac(res->mac, &mac);
551 APP_CHECK(rc == 0, st, "invalid ether addr:%s", res->mac);
552 rc = add_dst_ethaddr(res->port, &mac);
553 APP_CHECK(rc == 0, st, "invalid port numer:%hu", res->port);
558 cmdline_parse_token_string_t cfg_add_neigh_start =
559 TOKEN_STRING_INITIALIZER(struct cfg_neigh_add_item, neigh, "neigh");
560 cmdline_parse_token_string_t cfg_add_neigh_pstr =
561 TOKEN_STRING_INITIALIZER(struct cfg_neigh_add_item, pstr, "port");
562 cmdline_parse_token_num_t cfg_add_neigh_port =
563 TOKEN_NUM_INITIALIZER(struct cfg_neigh_add_item, port, UINT16);
564 cmdline_parse_token_string_t cfg_add_neigh_mac =
565 TOKEN_STRING_INITIALIZER(struct cfg_neigh_add_item, mac, NULL);
567 cmdline_parse_inst_t cfg_neigh_add_rule = {
568 .f = cfg_parse_neigh,
572 (void *)&cfg_add_neigh_start,
573 (void *)&cfg_add_neigh_pstr,
574 (void *)&cfg_add_neigh_port,
575 (void *)&cfg_add_neigh_mac,
580 /** set of cfg items */
581 cmdline_parse_ctx_t ipsec_ctx[] = {
582 (cmdline_parse_inst_t *)&cfg_sp_add_rule,
583 (cmdline_parse_inst_t *)&cfg_sa_add_rule,
584 (cmdline_parse_inst_t *)&cfg_rt_add_rule,
585 (cmdline_parse_inst_t *)&cfg_flow_add_rule,
586 (cmdline_parse_inst_t *)&cfg_neigh_add_rule,
591 parse_cfg_file(const char *cfg_filename)
593 struct cmdline *cl = cmdline_stdin_new(ipsec_ctx, "");
594 FILE *f = fopen(cfg_filename, "r");
595 char str[1024] = {0}, *get_s = NULL;
596 uint32_t line_num = 0;
597 struct parse_status status = {0};
600 rte_panic("Error: invalid file descriptor %s\n", cfg_filename);
605 rte_panic("Error: cannot create cmdline instance\n");
609 cfg_sp_add_rule.data = &status;
610 cfg_sa_add_rule.data = &status;
611 cfg_rt_add_rule.data = &status;
612 cfg_flow_add_rule.data = &status;
613 cfg_neigh_add_rule.data = &status;
618 get_s = fgets(oneline, 1024, f);
625 if (strlen(oneline) > 1022) {
626 rte_panic("%s:%u: error: "
627 "the line contains more characters the parser can handle\n",
628 cfg_filename, line_num);
632 /* process comment char '#' */
633 if (oneline[0] == '#')
636 pos = strchr(oneline, '#');
640 /* process line concatenator '\' */
641 pos = strchr(oneline, 92);
643 if (pos != oneline+strlen(oneline) - 2) {
644 rte_panic("%s:%u: error: "
645 "no character should exist after '\\'\n",
646 cfg_filename, line_num);
652 if (strlen(oneline) + strlen(str) > 1022) {
653 rte_panic("%s:%u: error: "
654 "the concatenated line contains more characters the parser can handle\n",
655 cfg_filename, line_num);
659 strcpy(str + strlen(str), oneline);
663 /* copy the line to str and process */
664 if (strlen(oneline) + strlen(str) > 1022) {
665 rte_panic("%s:%u: error: "
666 "the line contains more characters the parser can handle\n",
667 cfg_filename, line_num);
670 strcpy(str + strlen(str), oneline);
672 str[strlen(str)] = '\n';
673 if (cmdline_parse(cl, str) < 0) {
674 rte_panic("%s:%u: error: parsing \"%s\" failed\n",
675 cfg_filename, line_num, str);
679 if (status.status < 0) {
680 rte_panic("%s:%u: error: %s", cfg_filename,
681 line_num, status.parse_msg);
685 memset(str, 0, 1024);
688 cmdline_stdin_exit(cl);
699 cmdline_stdin_exit(cl);