1 /* $KAME: policy_parse.y,v 1.21 2003/12/12 08:01:26 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * IN/OUT bound policy configuration take place such below:
34 * in <priority> <policy>
35 * out <priority> <policy>
37 * <priority> is one of the following:
38 * priority <signed int> where the integer is an offset from the default
39 * priority, where negative numbers indicate lower
40 * priority (towards end of list) and positive numbers
41 * indicate higher priority (towards beginning of list)
43 * priority {low,def,high} {+,-} <unsigned int> where low and high are
44 * constants which are closer
45 * to the end of the list and
46 * beginning of the list,
49 * <policy> is one of following:
50 * "discard", "none", "ipsec <requests>", "entrust", "bypass",
52 * The following requests are accepted as <requests>:
54 * protocol/mode/src-dst/level
55 * protocol/mode/src-dst parsed as protocol/mode/src-dst/default
56 * protocol/mode/src-dst/ parsed as protocol/mode/src-dst/default
57 * protocol/transport parsed as protocol/mode/any-any/default
58 * protocol/transport//level parsed as protocol/mode/any-any/level
60 * You can concatenate these requests with either ' '(single space) or '\n'.
68 #include <sys/types.h>
69 #include <sys/param.h>
70 #include <sys/socket.h>
72 #include <netinet/in.h>
73 #ifdef HAVE_NETINET6_IPSEC
74 # include <netinet6/ipsec.h>
76 # include <netinet/ipsec.h>
88 #include "ipsec_strerror.h"
92 #define INT32_MAX (0xffffffff)
96 #define INT32_MIN (-INT32_MAX-1)
100 (isdigit(c) ? (c - '0') : (isupper(c) ? (c - 'A' + 10) : (c - 'a' + 10) ))
102 static u_int8_t *pbuf = NULL; /* sadb_x_policy buffer */
103 static int tlen = 0; /* total length of pbuf */
104 static int offset = 0; /* offset of pbuf */
105 static int p_dir, p_type, p_protocol, p_mode, p_level, p_reqid;
106 static u_int32_t p_priority = 0;
107 static long p_priority_offset = 0;
108 static struct sockaddr *p_src = NULL;
109 static struct sockaddr *p_dst = NULL;
112 extern void yyerror __P((char *msg));
113 static struct sockaddr *parse_sockaddr __P((struct _val *addrbuf,
114 struct _val *portbuf));
115 static int rule_check __P((void));
116 static int init_x_policy __P((void));
117 static int set_x_request __P((struct sockaddr *, struct sockaddr *));
118 static int set_sockaddr __P((struct sockaddr *));
119 static void policy_parse_request_init __P((void));
120 static void *policy_parse __P((const char *, int));
122 extern void __policy__strbuffer__init__ __P((const char *));
123 extern void __policy__strbuffer__free__ __P((void));
124 extern int yyparse __P((void));
125 extern int yylex __P((void));
127 extern char *__libipsectext; /*XXX*/
142 %token <num32> PRIO_BASE
143 %token <val> PRIO_OFFSET
144 %token ACTION PROTOCOL MODE LEVEL LEVEL_SPECIFY IPADDRESS PORT
147 %type <num> DIR PRIORITY ACTION PROTOCOL MODE LEVEL
148 %type <val> IPADDRESS LEVEL_SPECIFY PORT
157 #ifdef HAVE_PFKEY_POLICY_PRIORITY
158 p_priority = PRIORITY_DEFAULT;
167 | DIR PRIORITY PRIO_OFFSET ACTION
174 /* buffer big enough to hold a prepended negative sign */
175 offset_buf = malloc($3.len + 2);
176 if (offset_buf == NULL)
178 __ipsec_errcode = EIPSEC_NO_BUFS;
182 /* positive input value means higher priority, therefore lower
183 actual value so that is closer to the beginning of the list */
184 snprintf (offset_buf, $3.len + 2, "-%s", $3.buf);
187 p_priority_offset = atol(offset_buf);
191 if (errno != 0 || p_priority_offset < INT32_MIN)
193 __ipsec_errcode = EIPSEC_INVAL_PRIORITY_OFFSET;
197 p_priority = PRIORITY_DEFAULT + (u_int32_t) p_priority_offset;
203 | DIR PRIORITY HYPHEN PRIO_OFFSET ACTION
209 p_priority_offset = atol($4.buf);
211 if (errno != 0 || p_priority_offset > INT32_MAX)
213 __ipsec_errcode = EIPSEC_INVAL_PRIORITY_OFFSET;
217 /* negative input value means lower priority, therefore higher
218 actual value so that is closer to the end of the list */
219 p_priority = PRIORITY_DEFAULT + (u_int32_t) p_priority_offset;
225 | DIR PRIORITY PRIO_BASE ACTION
236 | DIR PRIORITY PRIO_BASE PLUS PRIO_OFFSET ACTION
242 p_priority_offset = atol($5.buf);
244 if (errno != 0 || p_priority_offset > PRIORITY_OFFSET_NEGATIVE_MAX)
246 __ipsec_errcode = EIPSEC_INVAL_PRIORITY_BASE_OFFSET;
250 /* adding value means higher priority, therefore lower
251 actual value so that is closer to the beginning of the list */
252 p_priority = $3 - (u_int32_t) p_priority_offset;
258 | DIR PRIORITY PRIO_BASE HYPHEN PRIO_OFFSET ACTION
264 p_priority_offset = atol($5.buf);
266 if (errno != 0 || p_priority_offset > PRIORITY_OFFSET_POSITIVE_MAX)
268 __ipsec_errcode = EIPSEC_INVAL_PRIORITY_BASE_OFFSET;
272 /* subtracting value means lower priority, therefore higher
273 actual value so that is closer to the end of the list */
274 p_priority = $3 + (u_int32_t) p_priority_offset;
283 p_type = 0; /* ignored it by kernel */
295 if (rule_check() < 0)
298 if (set_x_request(p_src, p_dst) < 0)
301 policy_parse_request_init();
306 : protocol SLASH mode SLASH addresses SLASH level
307 | protocol SLASH mode SLASH addresses SLASH
308 | protocol SLASH mode SLASH addresses
309 | protocol SLASH mode SLASH
310 | protocol SLASH mode SLASH SLASH level
311 | protocol SLASH mode
313 __ipsec_errcode = EIPSEC_FEW_ARGUMENTS;
317 __ipsec_errcode = EIPSEC_FEW_ARGUMENTS;
323 : PROTOCOL { p_protocol = $1; }
327 : MODE { p_mode = $1; }
336 p_level = IPSEC_LEVEL_UNIQUE;
337 p_reqid = atol($1.buf); /* atol() is good. */
343 p_src = parse_sockaddr(&$1, NULL);
349 p_dst = parse_sockaddr(&$4, NULL);
354 p_src = parse_sockaddr(&$1, &$2);
360 p_dst = parse_sockaddr(&$5, &$6);
365 if (p_dir != IPSEC_DIR_OUTBOUND) {
366 __ipsec_errcode = EIPSEC_INVAL_DIR;
371 if (p_dir != IPSEC_DIR_INBOUND) {
372 __ipsec_errcode = EIPSEC_INVAL_DIR;
387 fprintf(stderr, "libipsec: %s while parsing \"%s\"\n",
388 msg, __libipsectext);
393 static struct sockaddr *
394 parse_sockaddr(addrbuf, portbuf)
395 struct _val *addrbuf;
396 struct _val *portbuf;
398 struct addrinfo hints, *res;
402 struct sockaddr *newaddr = NULL;
404 if ((addr = malloc(addrbuf->len + 1)) == NULL) {
405 yyerror("malloc failed");
406 __ipsec_set_strerror(strerror(errno));
410 if (portbuf && ((serv = malloc(portbuf->len + 1)) == NULL)) {
412 yyerror("malloc failed");
413 __ipsec_set_strerror(strerror(errno));
417 strncpy(addr, addrbuf->buf, addrbuf->len);
418 addr[addrbuf->len] = '\0';
421 strncpy(serv, portbuf->buf, portbuf->len);
422 serv[portbuf->len] = '\0';
425 memset(&hints, 0, sizeof(hints));
426 hints.ai_family = PF_UNSPEC;
427 hints.ai_flags = AI_NUMERICHOST;
428 hints.ai_socktype = SOCK_DGRAM;
429 error = getaddrinfo(addr, serv, &hints, &res);
434 yyerror("invalid IP address");
435 __ipsec_set_strerror(gai_strerror(error));
439 if (res->ai_addr == NULL) {
440 yyerror("invalid IP address");
441 __ipsec_set_strerror(gai_strerror(error));
445 newaddr = malloc(res->ai_addrlen);
446 if (newaddr == NULL) {
447 __ipsec_errcode = EIPSEC_NO_BUFS;
451 memcpy(newaddr, res->ai_addr, res->ai_addrlen);
455 __ipsec_errcode = EIPSEC_NO_ERROR;
462 if (p_type == IPSEC_POLICY_IPSEC) {
463 if (p_protocol == IPPROTO_IP) {
464 __ipsec_errcode = EIPSEC_NO_PROTO;
468 if (p_mode != IPSEC_MODE_TRANSPORT
469 && p_mode != IPSEC_MODE_TUNNEL) {
470 __ipsec_errcode = EIPSEC_INVAL_MODE;
474 if (p_src == NULL && p_dst == NULL) {
475 if (p_mode != IPSEC_MODE_TRANSPORT) {
476 __ipsec_errcode = EIPSEC_INVAL_ADDRESS;
480 else if (p_src->sa_family != p_dst->sa_family) {
481 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
486 __ipsec_errcode = EIPSEC_NO_ERROR;
493 struct sadb_x_policy *p;
499 pbuf = malloc(sizeof(struct sadb_x_policy));
501 __ipsec_errcode = EIPSEC_NO_BUFS;
504 tlen = sizeof(struct sadb_x_policy);
506 memset(pbuf, 0, tlen);
507 p = (struct sadb_x_policy *)pbuf;
508 p->sadb_x_policy_len = 0; /* must update later */
509 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
510 p->sadb_x_policy_type = p_type;
511 p->sadb_x_policy_dir = p_dir;
512 p->sadb_x_policy_id = 0;
513 #ifdef HAVE_PFKEY_POLICY_PRIORITY
514 p->sadb_x_policy_priority = p_priority;
516 /* fail if given a priority and libipsec was not compiled with
520 __ipsec_errcode = EIPSEC_PRIORITY_NOT_COMPILED;
527 __ipsec_errcode = EIPSEC_NO_ERROR;
532 set_x_request(src, dst)
533 struct sockaddr *src, *dst;
535 struct sadb_x_ipsecrequest *p;
540 + (src ? sysdep_sa_len(src) : 0)
541 + (dst ? sysdep_sa_len(dst) : 0);
542 tlen += reqlen; /* increment to total length */
544 n = realloc(pbuf, tlen);
546 __ipsec_errcode = EIPSEC_NO_BUFS;
551 p = (struct sadb_x_ipsecrequest *)&pbuf[offset];
552 p->sadb_x_ipsecrequest_len = reqlen;
553 p->sadb_x_ipsecrequest_proto = p_protocol;
554 p->sadb_x_ipsecrequest_mode = p_mode;
555 p->sadb_x_ipsecrequest_level = p_level;
556 p->sadb_x_ipsecrequest_reqid = p_reqid;
557 offset += sizeof(*p);
559 if (set_sockaddr(src) || set_sockaddr(dst))
562 __ipsec_errcode = EIPSEC_NO_ERROR;
568 struct sockaddr *addr;
571 __ipsec_errcode = EIPSEC_NO_ERROR;
575 /* tlen has already incremented */
577 memcpy(&pbuf[offset], addr, sysdep_sa_len(addr));
579 offset += sysdep_sa_len(addr);
581 __ipsec_errcode = EIPSEC_NO_ERROR;
586 policy_parse_request_init()
588 p_protocol = IPPROTO_IP;
589 p_mode = IPSEC_MODE_ANY;
590 p_level = IPSEC_LEVEL_DEFAULT;
605 policy_parse(msg, msglen)
615 p_dir = IPSEC_DIR_INVALID;
616 p_type = IPSEC_POLICY_DISCARD;
617 policy_parse_request_init();
618 __policy__strbuffer__init__(msg);
620 error = yyparse(); /* it must be set errcode. */
621 __policy__strbuffer__free__();
629 /* update total length */
630 ((struct sadb_x_policy *)pbuf)->sadb_x_policy_len = PFKEY_UNIT64(tlen);
632 __ipsec_errcode = EIPSEC_NO_ERROR;
638 ipsec_set_policy(msg, msglen)
639 __ipsec_const char *msg;
644 policy = policy_parse(msg, msglen);
645 if (policy == NULL) {
646 if (__ipsec_errcode == EIPSEC_NO_ERROR)
647 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
651 __ipsec_errcode = EIPSEC_NO_ERROR;