1 /* $FreeBSD: src/usr.sbin/setkey/parse.y,v 1.1.2.2 2001/07/03 11:02:17 ume Exp $ */
2 /* $DragonFly: src/usr.sbin/setkey/parse.y,v 1.4 2004/03/24 18:23:46 cpressey Exp $ */
3 /* $KAME: kame/kame/kame/setkey/parse.y,v 1.36 2001/06/07 15:53:12 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/socket.h>
39 #include <net/route.h>
40 #include <netinet/in.h>
41 #include <net/pfkeyv2.h>
42 #include <netkey/key_var.h>
43 #include <netinet6/ipsec.h>
44 #include <arpa/inet.h>
57 (isdigit(c) ? (c - '0') : (isupper(c) ? (c - 'A' + 10) : (c - 'a' + 10) ))
62 struct sockaddr *p_src, *p_dst;
63 u_int p_prefs, p_prefd, p_upper;
64 u_int p_satype, p_ext, p_alg_enc, p_alg_auth, p_replay, p_mode;
66 u_int p_key_enc_len, p_key_auth_len;
67 caddr_t p_key_enc, p_key_auth;
68 time_t p_lt_hard, p_lt_soft;
73 /* temporary buffer */
74 static struct sockaddr *pp_addr;
75 static u_int pp_prefix;
77 static caddr_t pp_key;
79 extern u_char m_buf[BUFSIZ];
81 extern char cmdarg[8192];
84 static struct addrinfo *parse_addr(char *, char *, int);
85 static int setvarbuf(int *, struct sadb_ext *, int, caddr_t, int);
86 void parse_init(void);
87 void free_buffer(void);
89 extern int setkeymsg(void);
90 extern int sendkeymsg(void);
92 extern int yylex(void);
93 extern void yyfatal(const char *);
94 extern void yyerror(const char *);
103 %token ADD GET DELETE DELETEALL FLUSH DUMP
104 %token ADDRESS PREFIX PORT PORTANY
105 %token UP_PROTO PR_ESP PR_AH PR_IPCOMP PR_TCP
106 %token F_PROTOCOL F_AUTH F_ENC F_REPLAY F_COMP F_RAWCPI
107 %token F_MODE MODE F_REQID
108 %token F_EXT EXTENSION NOCYCLICSEQ
109 %token ALG_AUTH ALG_ENC ALG_ENC_DESDERIV ALG_ENC_DES32IV ALG_COMP
110 %token F_LIFETIME_HARD F_LIFETIME_SOFT
111 %token DECSTRING QUOTEDSTRING HEXSTRING STRING ANY
113 %token SPDADD SPDDELETE SPDDUMP SPDFLUSH
114 %token F_POLICY PL_REQUESTS
116 %type <num> PORT PREFIX EXTENSION MODE
117 %type <num> UP_PROTO PR_ESP PR_AH PR_IPCOMP PR_TCP
118 %type <num> ALG_AUTH ALG_ENC ALG_ENC_DESDERIV ALG_ENC_DES32IV ALG_COMP
119 %type <num> DECSTRING
120 %type <val> ADDRESS PL_REQUESTS
121 %type <val> key_string policy_requests
122 %type <val> QUOTEDSTRING HEXSTRING STRING
130 printf("cmdarg:\n%s\n", cmdarg);
152 /* commands concerned with management, there is in tail of this file. */
156 : ADD { p_type = SADB_ADD; }
157 sa_selector_spec extension_spec algorithm_spec EOT
162 : DELETE { p_type = SADB_DELETE; }
163 sa_selector_spec extension_spec
165 if (p_mode != IPSEC_MODE_ANY)
166 yyerror("WARNING: mode is obsoleted.");
171 /* deleteall command */
173 : DELETEALL { p_type = SADB_DELETE; }
174 ipaddress { p_src = pp_addr; }
175 ipaddress { p_dst = pp_addr; }
183 : GET { p_type = SADB_GET; }
184 sa_selector_spec extension_spec
186 if (p_mode != IPSEC_MODE_ANY)
187 yyerror("WARNING: mode is obsoleted.");
194 : FLUSH { p_type = SADB_FLUSH; }
200 : DUMP { p_type = SADB_DUMP; }
204 /* sa_selector_spec */
206 : ipaddress { p_src = pp_addr; }
207 ipaddress { p_dst = pp_addr; }
212 : /*NOTHING*/ { p_satype = SADB_SATYPE_UNSPEC; }
215 p_satype = SADB_SATYPE_ESP;
217 p_ext |= SADB_X_EXT_OLD;
219 p_ext &= ~SADB_X_EXT_OLD;
223 p_satype = SADB_SATYPE_AH;
225 p_ext |= SADB_X_EXT_OLD;
227 p_ext &= ~SADB_X_EXT_OLD;
231 p_satype = SADB_X_SATYPE_IPCOMP;
235 p_satype = SADB_X_SATYPE_TCPSIGNATURE;
240 : DECSTRING { p_spi = $1; }
245 char buf0[4], buf[4];
250 yyerror("SPI too big.");
257 *bp = (ATOX(yp[0]) << 4) | ATOX(yp[1]);
262 for (i = 0; i < 4; i++) buf[i] = 0;
264 for (j = $1.len - 1, i = 3; j >= 0; j--, i--)
268 p_spi = ntohl(*(u_int32_t *)buf);
281 : F_ENC enc_alg enc_key F_AUTH auth_alg auth_key
282 | F_ENC enc_alg enc_key
286 : F_AUTH auth_alg auth_key
290 : F_COMP ALG_COMP { p_alg_enc = $2; }
291 | F_COMP ALG_COMP { p_alg_enc = $2; }
292 F_RAWCPI { p_ext |= SADB_X_EXT_RAWCPI; }
296 : ALG_ENC { p_alg_enc = $1; }
300 if (p_ext & SADB_X_EXT_OLD) {
301 yyerror("algorithm mismatched.");
304 p_ext |= SADB_X_EXT_DERIV;
309 if (!(p_ext & SADB_X_EXT_OLD)) {
310 yyerror("algorithm mismatched.");
313 p_ext |= SADB_X_EXT_IV4B;
320 if (p_alg_enc != SADB_EALG_NULL) {
321 yyerror("no key found.");
327 p_key_enc_len = $1.len;
330 if (ipsec_check_keylen(SADB_EXT_SUPPORTED_ENCRYPT,
332 PFKEY_UNUNIT64(p_key_enc_len)) < 0) {
333 yyerror(ipsec_strerror());
340 : ALG_AUTH { p_alg_auth = $1; }
346 if (p_alg_auth != SADB_X_AALG_NULL) {
347 yyerror("no key found.");
353 p_key_auth_len = $1.len;
356 if (p_alg_auth == SADB_X_AALG_TCP_MD5) {
357 if ((p_key_auth_len < 1) || (p_key_auth_len >
360 } else if (ipsec_check_keylen(SADB_EXT_SUPPORTED_AUTH,
362 PFKEY_UNUNIT64(p_key_auth_len)) < 0) {
363 yyerror(ipsec_strerror());
373 /* free pp_key later */
380 if ((pp_key = malloc($1.len)) == 0) {
382 yyerror("not enough core");
385 memset(pp_key, 0, $1.len);
389 *bp = (ATOX(yp[0]) << 4) | ATOX(yp[1]);
399 | extension_spec extension
403 : F_EXT EXTENSION { p_ext |= $2; }
404 | F_EXT NOCYCLICSEQ { p_ext &= ~SADB_X_EXT_CYCSEQ; }
405 | F_MODE MODE { p_mode = $2; }
406 | F_MODE ANY { p_mode = IPSEC_MODE_ANY; }
407 | F_REQID DECSTRING { p_reqid = $2; }
410 if (p_ext & SADB_X_EXT_OLD) {
411 yyerror("replay prevention "
412 "only use on new spec.");
417 | F_LIFETIME_HARD DECSTRING { p_lt_hard = $2; }
418 | F_LIFETIME_SOFT DECSTRING { p_lt_soft = $2; }
421 /* definition about command for SPD management */
426 p_type = SADB_X_SPDADD;
427 p_satype = SADB_SATYPE_UNSPEC;
429 sp_selector_spec policy_spec EOT
435 p_type = SADB_X_SPDDELETE;
436 p_satype = SADB_SATYPE_UNSPEC;
438 sp_selector_spec policy_spec EOT
444 p_type = SADB_X_SPDDUMP;
445 p_satype = SADB_SATYPE_UNSPEC;
453 p_type = SADB_X_SPDFLUSH;
454 p_satype = SADB_SATYPE_UNSPEC;
459 /* sp_selector_spec */
461 : ipaddress { p_src = pp_addr; }
462 prefix { p_prefs = pp_prefix; }
465 switch (p_src->sa_family) {
467 ((struct sockaddr_in *)p_src)->sin_port =
472 ((struct sockaddr_in6 *)p_src)->sin6_port =
480 ipaddress { p_dst = pp_addr; }
481 prefix { p_prefd = pp_prefix; }
484 switch (p_dst->sa_family) {
486 ((struct sockaddr_in *)p_dst)->sin_port =
491 ((struct sockaddr_in6 *)p_dst)->sin6_port =
501 /* XXX is it something userland should check? */
506 if (_INPORTBYSA(p_src) != IPSEC_PORT_ANY
507 || _INPORTBYSA(p_dst) != IPSEC_PORT_ANY) {
508 yyerror("port number must be \"any\".");
511 if ((pp_addr->sa_family == AF_INET6
512 && p_upper == IPPROTO_ICMP)
513 || (pp_addr->sa_family == AF_INET
514 && p_upper == IPPROTO_ICMPV6)) {
515 yyerror("upper layer protocol "
530 struct addrinfo *res;
532 res = parse_addr($1.buf, NULL, AI_NUMERICHOST);
537 pp_addr = (struct sockaddr *)malloc(res->ai_addrlen);
539 yyerror("not enough core");
543 memcpy(pp_addr, res->ai_addr, res->ai_addrlen);
551 : /*NOTHING*/ { pp_prefix = ~0; }
552 | PREFIX { pp_prefix = $1; }
556 : /*NOTHING*/ { pp_port = IPSEC_PORT_ANY; }
557 | PORT { pp_port = $1; }
558 | PORTANY { pp_port = IPSEC_PORT_ANY; }
562 : DECSTRING { p_upper = $1; }
563 | UP_PROTO { p_upper = $1; }
564 | ANY { p_upper = IPSEC_ULPROTO_ANY; }
567 struct protoent *ent;
569 ent = getprotobyname($1.buf);
571 p_upper = ent->p_proto;
573 if (strcmp("icmp6", $1.buf) == 0) {
574 p_upper = IPPROTO_ICMPV6;
575 } else if(strcmp("ip4", $1.buf) == 0) {
576 p_upper = IPPROTO_IPV4;
578 yyerror("invalid upper layer protocol");
588 : F_POLICY policy_requests
590 p_policy = ipsec_set_policy($2.buf, $2.len);
591 if (p_policy == NULL) {
594 yyerror(ipsec_strerror());
598 p_policy_len = ipsec_get_policylen(p_policy);
605 : PL_REQUESTS { $$ = $1; }
613 struct sadb_msg m_msg;
615 m_msg.sadb_msg_version = PF_KEY_V2;
616 m_msg.sadb_msg_type = p_type;
617 m_msg.sadb_msg_errno = 0;
618 m_msg.sadb_msg_satype = p_satype;
619 m_msg.sadb_msg_reserved = 0;
620 m_msg.sadb_msg_seq = 0;
621 m_msg.sadb_msg_pid = getpid();
623 m_len = sizeof(struct sadb_msg);
624 memcpy(m_buf, &m_msg, m_len);
632 /* set encryption algorithm, if present. */
633 if (p_satype != SADB_X_SATYPE_IPCOMP && p_alg_enc != SADB_EALG_NONE) {
634 struct sadb_key m_key;
637 PFKEY_UNIT64(sizeof(m_key)
638 + PFKEY_ALIGN8(p_key_enc_len));
639 m_key.sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
640 m_key.sadb_key_bits = p_key_enc_len * 8;
641 m_key.sadb_key_reserved = 0;
644 (struct sadb_ext *)&m_key, sizeof(m_key),
645 (caddr_t)p_key_enc, p_key_enc_len);
648 /* set authentication algorithm, if present. */
649 if (p_alg_auth != SADB_AALG_NONE) {
650 struct sadb_key m_key;
653 PFKEY_UNIT64(sizeof(m_key)
654 + PFKEY_ALIGN8(p_key_auth_len));
655 m_key.sadb_key_exttype = SADB_EXT_KEY_AUTH;
656 m_key.sadb_key_bits = p_key_auth_len * 8;
657 m_key.sadb_key_reserved = 0;
660 (struct sadb_ext *)&m_key, sizeof(m_key),
661 (caddr_t)p_key_auth, p_key_auth_len);
664 /* set lifetime for HARD */
665 if (p_lt_hard != 0) {
666 struct sadb_lifetime m_lt;
667 u_int len = sizeof(struct sadb_lifetime);
669 m_lt.sadb_lifetime_len = PFKEY_UNIT64(len);
670 m_lt.sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
671 m_lt.sadb_lifetime_allocations = 0;
672 m_lt.sadb_lifetime_bytes = 0;
673 m_lt.sadb_lifetime_addtime = p_lt_hard;
674 m_lt.sadb_lifetime_usetime = 0;
676 memcpy(m_buf + m_len, &m_lt, len);
680 /* set lifetime for SOFT */
681 if (p_lt_soft != 0) {
682 struct sadb_lifetime m_lt;
683 u_int len = sizeof(struct sadb_lifetime);
685 m_lt.sadb_lifetime_len = PFKEY_UNIT64(len);
686 m_lt.sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
687 m_lt.sadb_lifetime_allocations = 0;
688 m_lt.sadb_lifetime_bytes = 0;
689 m_lt.sadb_lifetime_addtime = p_lt_soft;
690 m_lt.sadb_lifetime_usetime = 0;
692 memcpy(m_buf + m_len, &m_lt, len);
701 struct sadb_x_sa2 m_sa2;
702 struct sadb_address m_addr;
706 len = sizeof(struct sadb_sa);
707 m_sa.sadb_sa_len = PFKEY_UNIT64(len);
708 m_sa.sadb_sa_exttype = SADB_EXT_SA;
709 m_sa.sadb_sa_spi = htonl(p_spi);
710 m_sa.sadb_sa_replay = p_replay;
711 m_sa.sadb_sa_state = 0;
712 m_sa.sadb_sa_auth = p_alg_auth;
713 m_sa.sadb_sa_encrypt = p_alg_enc;
714 m_sa.sadb_sa_flags = p_ext;
716 memcpy(m_buf + m_len, &m_sa, len);
719 len = sizeof(struct sadb_x_sa2);
720 m_sa2.sadb_x_sa2_len = PFKEY_UNIT64(len);
721 m_sa2.sadb_x_sa2_exttype = SADB_X_EXT_SA2;
722 m_sa2.sadb_x_sa2_mode = p_mode;
723 m_sa2.sadb_x_sa2_reqid = p_reqid;
725 memcpy(m_buf + m_len, &m_sa2, len);
730 m_addr.sadb_address_len =
731 PFKEY_UNIT64(sizeof(m_addr)
732 + PFKEY_ALIGN8(p_src->sa_len));
733 m_addr.sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
734 m_addr.sadb_address_proto = IPSEC_ULPROTO_ANY;
735 switch (p_src->sa_family) {
737 m_addr.sadb_address_prefixlen =
738 sizeof(struct in_addr) << 3;
742 m_addr.sadb_address_prefixlen =
743 sizeof(struct in6_addr) << 3;
747 yyerror("unsupported address family");
750 m_addr.sadb_address_reserved = 0;
753 (struct sadb_ext *)&m_addr, sizeof(m_addr),
754 (caddr_t)p_src, p_src->sa_len);
757 m_addr.sadb_address_len =
758 PFKEY_UNIT64(sizeof(m_addr)
759 + PFKEY_ALIGN8(p_dst->sa_len));
760 m_addr.sadb_address_exttype = SADB_EXT_ADDRESS_DST;
761 m_addr.sadb_address_proto = IPSEC_ULPROTO_ANY;
762 switch (p_dst->sa_family) {
764 m_addr.sadb_address_prefixlen =
765 sizeof(struct in_addr) << 3;
769 m_addr.sadb_address_prefixlen =
770 sizeof(struct in6_addr) << 3;
774 yyerror("unsupported address family");
777 m_addr.sadb_address_reserved = 0;
780 (struct sadb_ext *)&m_addr, sizeof(m_addr),
781 (caddr_t)p_dst, p_dst->sa_len);
785 /* for SPD management */
786 case SADB_X_SPDFLUSH:
791 case SADB_X_SPDDELETE:
793 struct sadb_address m_addr;
796 memcpy(m_buf + m_len, p_policy, p_policy_len);
797 m_len += p_policy_len;
802 m_addr.sadb_address_len =
803 PFKEY_UNIT64(sizeof(m_addr)
804 + PFKEY_ALIGN8(p_src->sa_len));
805 m_addr.sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
806 m_addr.sadb_address_proto = p_upper;
807 switch (p_src->sa_family) {
809 plen = sizeof(struct in_addr) << 3;
813 plen = sizeof(struct in6_addr) << 3;
817 yyerror("unsupported address family");
820 m_addr.sadb_address_prefixlen =
821 (p_prefs != ~0 ? p_prefs : plen);
822 m_addr.sadb_address_reserved = 0;
825 (struct sadb_ext *)&m_addr, sizeof(m_addr),
826 (caddr_t)p_src, p_src->sa_len);
829 m_addr.sadb_address_len =
830 PFKEY_UNIT64(sizeof(m_addr)
831 + PFKEY_ALIGN8(p_dst->sa_len));
832 m_addr.sadb_address_exttype = SADB_EXT_ADDRESS_DST;
833 m_addr.sadb_address_proto = p_upper;
834 switch (p_dst->sa_family) {
836 plen = sizeof(struct in_addr) << 3;
840 plen = sizeof(struct in6_addr) << 3;
844 yyerror("unsupported address family");
847 m_addr.sadb_address_prefixlen =
848 (p_prefd != ~0 ? p_prefd : plen);
849 m_addr.sadb_address_reserved = 0;
852 (struct sadb_ext *)&m_addr, sizeof(m_addr),
853 (caddr_t)p_dst, p_dst->sa_len);
858 ((struct sadb_msg *)m_buf)->sadb_msg_len = PFKEY_UNIT64(m_len);
863 static struct addrinfo *
864 parse_addr(char *host, char *port, int flag)
866 struct addrinfo hints, *res = NULL;
869 memset(&hints, 0, sizeof(hints));
870 hints.ai_family = PF_UNSPEC;
871 hints.ai_socktype = SOCK_DGRAM;
872 hints.ai_flags = flag;
873 error = getaddrinfo(host, port, &hints, &res);
875 yyerror(gai_strerror(error));
878 if (res->ai_next != NULL) {
879 yyerror(gai_strerror(error));
885 setvarbuf(int *off, struct sadb_ext *ebuf, int elen, caddr_t vbuf, int vlen)
887 memset(m_buf + *off, 0, PFKEY_UNUNIT64(ebuf->sadb_ext_len));
888 memcpy(m_buf + *off, (caddr_t)ebuf, elen);
889 memcpy(m_buf + *off + elen, vbuf, vlen);
890 (*off) += PFKEY_ALIGN8(elen + vlen);
902 p_src = 0, p_dst = 0;
903 pp_prefix = p_prefs = p_prefd = ~0;
904 pp_port = IPSEC_PORT_ANY;
908 p_ext = SADB_X_EXT_CYCSEQ;
909 p_alg_enc = SADB_EALG_NONE;
910 p_alg_auth = SADB_AALG_NONE;
911 p_mode = IPSEC_MODE_ANY;
914 p_key_enc_len = p_key_auth_len = 0;
915 p_key_enc = p_key_auth = 0;
916 p_lt_hard = p_lt_soft = 0;
921 memset(cmdarg, 0, sizeof(cmdarg));
929 if (p_src) free(p_src);
930 if (p_dst) free(p_dst);
931 if (p_key_enc) free(p_key_enc);
932 if (p_key_auth) free(p_key_auth);