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;
406 addr_len = addrbuf->len + 1;
407 if ((addr = malloc(addr_len)) == NULL) {
408 yyerror("malloc failed");
409 __ipsec_set_strerror(strerror(errno));
414 serv_len = portbuf->len + 1;
415 if ((serv = malloc(serv_len)) == NULL) {
417 yyerror("malloc failed");
418 __ipsec_set_strerror(strerror(errno));
423 strlcpy(addr, addrbuf->buf, addr_len);
426 strlcpy(serv, portbuf->buf, serv_len);
429 memset(&hints, 0, sizeof(hints));
430 hints.ai_family = PF_UNSPEC;
431 hints.ai_flags = AI_NUMERICHOST;
432 hints.ai_socktype = SOCK_DGRAM;
433 error = getaddrinfo(addr, serv, &hints, &res);
438 yyerror("invalid IP address");
439 __ipsec_set_strerror(gai_strerror(error));
443 if (res->ai_addr == NULL) {
444 yyerror("invalid IP address");
445 __ipsec_set_strerror(gai_strerror(error));
449 newaddr = malloc(res->ai_addrlen);
450 if (newaddr == NULL) {
451 __ipsec_errcode = EIPSEC_NO_BUFS;
455 memcpy(newaddr, res->ai_addr, res->ai_addrlen);
459 __ipsec_errcode = EIPSEC_NO_ERROR;
466 if (p_type == IPSEC_POLICY_IPSEC) {
467 if (p_protocol == IPPROTO_IP) {
468 __ipsec_errcode = EIPSEC_NO_PROTO;
472 if (p_mode != IPSEC_MODE_TRANSPORT
473 && p_mode != IPSEC_MODE_TUNNEL) {
474 __ipsec_errcode = EIPSEC_INVAL_MODE;
478 if (p_src == NULL && p_dst == NULL) {
479 if (p_mode != IPSEC_MODE_TRANSPORT) {
480 __ipsec_errcode = EIPSEC_INVAL_ADDRESS;
484 else if (p_src->sa_family != p_dst->sa_family) {
485 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
490 __ipsec_errcode = EIPSEC_NO_ERROR;
497 struct sadb_x_policy *p;
503 pbuf = malloc(sizeof(struct sadb_x_policy));
505 __ipsec_errcode = EIPSEC_NO_BUFS;
508 tlen = sizeof(struct sadb_x_policy);
510 memset(pbuf, 0, tlen);
511 p = (struct sadb_x_policy *)pbuf;
512 p->sadb_x_policy_len = 0; /* must update later */
513 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
514 p->sadb_x_policy_type = p_type;
515 p->sadb_x_policy_dir = p_dir;
516 p->sadb_x_policy_id = 0;
517 #ifdef HAVE_PFKEY_POLICY_PRIORITY
518 p->sadb_x_policy_priority = p_priority;
520 /* fail if given a priority and libipsec was not compiled with
524 __ipsec_errcode = EIPSEC_PRIORITY_NOT_COMPILED;
531 __ipsec_errcode = EIPSEC_NO_ERROR;
536 set_x_request(src, dst)
537 struct sockaddr *src, *dst;
539 struct sadb_x_ipsecrequest *p;
544 + (src ? sysdep_sa_len(src) : 0)
545 + (dst ? sysdep_sa_len(dst) : 0);
546 tlen += reqlen; /* increment to total length */
548 n = realloc(pbuf, tlen);
550 __ipsec_errcode = EIPSEC_NO_BUFS;
555 p = (struct sadb_x_ipsecrequest *)&pbuf[offset];
556 p->sadb_x_ipsecrequest_len = reqlen;
557 p->sadb_x_ipsecrequest_proto = p_protocol;
558 p->sadb_x_ipsecrequest_mode = p_mode;
559 p->sadb_x_ipsecrequest_level = p_level;
560 p->sadb_x_ipsecrequest_reqid = p_reqid;
561 offset += sizeof(*p);
563 if (set_sockaddr(src) || set_sockaddr(dst))
566 __ipsec_errcode = EIPSEC_NO_ERROR;
572 struct sockaddr *addr;
575 __ipsec_errcode = EIPSEC_NO_ERROR;
579 /* tlen has already incremented */
581 memcpy(&pbuf[offset], addr, sysdep_sa_len(addr));
583 offset += sysdep_sa_len(addr);
585 __ipsec_errcode = EIPSEC_NO_ERROR;
590 policy_parse_request_init()
592 p_protocol = IPPROTO_IP;
593 p_mode = IPSEC_MODE_ANY;
594 p_level = IPSEC_LEVEL_DEFAULT;
609 policy_parse(msg, msglen)
619 p_dir = IPSEC_DIR_INVALID;
620 p_type = IPSEC_POLICY_DISCARD;
621 policy_parse_request_init();
622 __policy__strbuffer__init__(msg);
624 error = yyparse(); /* it must be set errcode. */
625 __policy__strbuffer__free__();
633 /* update total length */
634 ((struct sadb_x_policy *)pbuf)->sadb_x_policy_len = PFKEY_UNIT64(tlen);
636 __ipsec_errcode = EIPSEC_NO_ERROR;
642 ipsec_set_policy(msg, msglen)
643 __ipsec_const char *msg;
648 policy = policy_parse(msg, msglen);
649 if (policy == NULL) {
650 if (__ipsec_errcode == EIPSEC_NO_ERROR)
651 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
655 __ipsec_errcode = EIPSEC_NO_ERROR;