1 /* $KAME: policy_parse.y,v 1.14 2003/06/27 03:39:20 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:
37 * <policy> is one of following:
38 * "discard", "none", "ipsec <requests>", "entrust", "bypass",
40 * The following requests are accepted as <requests>:
42 * protocol/mode/src-dst/level
43 * protocol/mode/src-dst parsed as protocol/mode/src-dst/default
44 * protocol/mode/src-dst/ parsed as protocol/mode/src-dst/default
45 * protocol/transport parsed as protocol/mode/any-any/default
46 * protocol/transport//level parsed as protocol/mode/any-any/level
48 * You can concatenate these requests with either ' '(single space) or '\n'.
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
55 #include <sys/types.h>
56 #include <sys/param.h>
57 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <netipsec/ipsec.h>
67 #include "ipsec_strerror.h"
70 (isdigit(c) ? (c - '0') : (isupper(c) ? (c - 'A' + 10) : (c - 'a' + 10) ))
72 static caddr_t pbuf = NULL; /* sadb_x_policy buffer */
73 static int tlen = 0; /* total length of pbuf */
74 static int offset = 0; /* offset of pbuf */
75 static int p_dir, p_type, p_protocol, p_mode, p_level, p_reqid;
76 static struct sockaddr *p_src = NULL;
77 static struct sockaddr *p_dst = NULL;
80 extern void yyerror(char *msg);
81 static struct sockaddr *parse_sockaddr(struct _val *buf);
82 static int rule_check(void);
83 static int init_x_policy(void);
84 static int set_x_request(struct sockaddr *src, struct sockaddr *dst);
85 static int set_sockaddr(struct sockaddr *addr);
86 static void policy_parse_request_init(void);
87 static caddr_t policy_parse(char *msg, int msglen);
89 extern void __policy__strbuffer__init__(char *msg);
90 extern void __policy__strbuffer__free__(void);
91 extern int yyparse(void);
92 extern int yylex(void);
94 extern char *__libipsecyytext; /*XXX*/
106 %token DIR ACTION PROTOCOL MODE LEVEL LEVEL_SPECIFY
110 %type <num> DIR ACTION PROTOCOL MODE LEVEL
111 %type <val> IPADDRESS LEVEL_SPECIFY
127 p_type = 0; /* ignored it by kernel */
137 if (rule_check() < 0)
140 if (set_x_request(p_src, p_dst) < 0)
143 policy_parse_request_init();
148 : protocol SLASH mode SLASH addresses SLASH level
149 | protocol SLASH mode SLASH addresses SLASH
150 | protocol SLASH mode SLASH addresses
151 | protocol SLASH mode SLASH
152 | protocol SLASH mode SLASH SLASH level
153 | protocol SLASH mode
155 __ipsec_errcode = EIPSEC_FEW_ARGUMENTS;
159 __ipsec_errcode = EIPSEC_FEW_ARGUMENTS;
165 : PROTOCOL { p_protocol = $1; }
169 : MODE { p_mode = $1; }
178 p_level = IPSEC_LEVEL_UNIQUE;
179 p_reqid = atol($1.buf); /* atol() is good. */
185 p_src = parse_sockaddr(&$1);
191 p_dst = parse_sockaddr(&$4);
196 if (p_dir != IPSEC_DIR_OUTBOUND) {
197 __ipsec_errcode = EIPSEC_INVAL_DIR;
202 if (p_dir != IPSEC_DIR_INBOUND) {
203 __ipsec_errcode = EIPSEC_INVAL_DIR;
218 fprintf(stderr, "libipsec: %s while parsing \"%s\"\n",
219 msg, __libipsecyytext);
224 static struct sockaddr *
228 struct addrinfo hints, *res;
231 struct sockaddr *newaddr = NULL;
233 memset(&hints, 0, sizeof(hints));
234 hints.ai_family = PF_UNSPEC;
235 hints.ai_flags = AI_NUMERICHOST;
236 error = getaddrinfo(buf->buf, serv, &hints, &res);
238 yyerror("invalid IP address");
239 __ipsec_set_strerror(gai_strerror(error));
243 if (res->ai_addr == NULL) {
244 yyerror("invalid IP address");
245 __ipsec_set_strerror(gai_strerror(error));
249 newaddr = malloc(res->ai_addr->sa_len);
250 if (newaddr == NULL) {
251 __ipsec_errcode = EIPSEC_NO_BUFS;
255 memcpy(newaddr, res->ai_addr, res->ai_addr->sa_len);
259 __ipsec_errcode = EIPSEC_NO_ERROR;
266 if (p_type == IPSEC_POLICY_IPSEC) {
267 if (p_protocol == IPPROTO_IP) {
268 __ipsec_errcode = EIPSEC_NO_PROTO;
272 if (p_mode != IPSEC_MODE_TRANSPORT
273 && p_mode != IPSEC_MODE_TUNNEL) {
274 __ipsec_errcode = EIPSEC_INVAL_MODE;
278 if (p_src == NULL && p_dst == NULL) {
279 if (p_mode != IPSEC_MODE_TRANSPORT) {
280 __ipsec_errcode = EIPSEC_INVAL_ADDRESS;
284 else if (p_src->sa_family != p_dst->sa_family) {
285 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
290 __ipsec_errcode = EIPSEC_NO_ERROR;
297 struct sadb_x_policy *p;
299 tlen = sizeof(struct sadb_x_policy);
303 __ipsec_errcode = EIPSEC_NO_BUFS;
306 memset(pbuf, 0, tlen);
307 p = (struct sadb_x_policy *)pbuf;
308 p->sadb_x_policy_len = 0; /* must update later */
309 p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
310 p->sadb_x_policy_type = p_type;
311 p->sadb_x_policy_dir = p_dir;
312 p->sadb_x_policy_id = 0;
316 __ipsec_errcode = EIPSEC_NO_ERROR;
321 set_x_request(src, dst)
322 struct sockaddr *src, *dst;
324 struct sadb_x_ipsecrequest *p;
328 + (src ? src->sa_len : 0)
329 + (dst ? dst->sa_len : 0);
330 tlen += reqlen; /* increment to total length */
332 pbuf = realloc(pbuf, tlen);
334 __ipsec_errcode = EIPSEC_NO_BUFS;
337 p = (struct sadb_x_ipsecrequest *)&pbuf[offset];
338 p->sadb_x_ipsecrequest_len = reqlen;
339 p->sadb_x_ipsecrequest_proto = p_protocol;
340 p->sadb_x_ipsecrequest_mode = p_mode;
341 p->sadb_x_ipsecrequest_level = p_level;
342 p->sadb_x_ipsecrequest_reqid = p_reqid;
343 offset += sizeof(*p);
345 if (set_sockaddr(src) || set_sockaddr(dst))
348 __ipsec_errcode = EIPSEC_NO_ERROR;
354 struct sockaddr *addr;
357 __ipsec_errcode = EIPSEC_NO_ERROR;
361 /* tlen has already incremented */
363 memcpy(&pbuf[offset], addr, addr->sa_len);
365 offset += addr->sa_len;
367 __ipsec_errcode = EIPSEC_NO_ERROR;
372 policy_parse_request_init()
374 p_protocol = IPPROTO_IP;
375 p_mode = IPSEC_MODE_ANY;
376 p_level = IPSEC_LEVEL_DEFAULT;
391 policy_parse(msg, msglen)
400 p_dir = IPSEC_DIR_INVALID;
401 p_type = IPSEC_POLICY_DISCARD;
402 policy_parse_request_init();
403 __policy__strbuffer__init__(msg);
405 error = yyparse(); /* it must be set errcode. */
406 __policy__strbuffer__free__();
414 /* update total length */
415 ((struct sadb_x_policy *)pbuf)->sadb_x_policy_len = PFKEY_UNIT64(tlen);
417 __ipsec_errcode = EIPSEC_NO_ERROR;
423 ipsec_set_policy(msg, msglen)
429 policy = policy_parse(msg, msglen);
430 if (policy == NULL) {
431 if (__ipsec_errcode == EIPSEC_NO_ERROR)
432 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
436 __ipsec_errcode = EIPSEC_NO_ERROR;