GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/ipsecctl/parse.c Lines: 0 415 0.0 %
Date: 2017-11-13 Branches: 0 335 0.0 %

Line Branch Exec Source
1
#include <stdlib.h>
2
#include <string.h>
3
#define YYBYACC 1
4
#define YYMAJOR 1
5
#define YYMINOR 9
6
#define YYLEX yylex()
7
#define YYEMPTY -1
8
#define yyclearin (yychar=(YYEMPTY))
9
#define yyerrok (yyerrflag=0)
10
#define YYRECOVERING() (yyerrflag!=0)
11
#define YYPREFIX "yy"
12
#line 24 "parse.y"
13
#include <sys/types.h>
14
#include <sys/ioctl.h>
15
#include <sys/queue.h>
16
#include <sys/socket.h>
17
#include <sys/stat.h>
18
#include <net/if.h>
19
#include <netinet/in.h>
20
#include <netinet/ip_ipsp.h>
21
#include <arpa/inet.h>
22
23
#include <ctype.h>
24
#include <err.h>
25
#include <errno.h>
26
#include <fcntl.h>
27
#include <ifaddrs.h>
28
#include <limits.h>
29
#include <netdb.h>
30
#include <stdarg.h>
31
#include <stdio.h>
32
#include <string.h>
33
#include <syslog.h>
34
#include <unistd.h>
35
#include <netdb.h>
36
37
#include "ipsecctl.h"
38
39
TAILQ_HEAD(files, file)		 files = TAILQ_HEAD_INITIALIZER(files);
40
static struct file {
41
	TAILQ_ENTRY(file)	 entry;
42
	FILE			*stream;
43
	char			*name;
44
	int			 lineno;
45
	int			 errors;
46
} *file;
47
struct file	*pushfile(const char *, int);
48
int		 popfile(void);
49
int		 check_file_secrecy(int, const char *);
50
int		 yyparse(void);
51
int		 yylex(void);
52
int		 yyerror(const char *, ...)
53
    __attribute__((__format__ (printf, 1, 2)))
54
    __attribute__((__nonnull__ (1)));
55
int		 yywarn(const char *, ...)
56
    __attribute__((__format__ (printf, 1, 2)))
57
    __attribute__((__nonnull__ (1)));
58
int		 kw_cmp(const void *, const void *);
59
int		 lookup(char *);
60
int		 lgetc(int);
61
int		 lungetc(int);
62
int		 findeol(void);
63
64
TAILQ_HEAD(symhead, sym)	 symhead = TAILQ_HEAD_INITIALIZER(symhead);
65
struct sym {
66
	TAILQ_ENTRY(sym)	 entry;
67
	int			 used;
68
	int			 persist;
69
	char			*nam;
70
	char			*val;
71
};
72
int		 symset(const char *, const char *, int);
73
char		*symget(const char *);
74
int		 cmdline_symset(char *);
75
76
#define KEYSIZE_LIMIT	1024
77
78
static struct ipsecctl	*ipsec = NULL;
79
static int		 debug = 0;
80
81
const struct ipsec_xf authxfs[] = {
82
	{ "unknown",		AUTHXF_UNKNOWN,		0,	0 },
83
	{ "none",		AUTHXF_NONE,		0,	0 },
84
	{ "hmac-md5",		AUTHXF_HMAC_MD5,	16,	0 },
85
	{ "hmac-ripemd160",	AUTHXF_HMAC_RIPEMD160,	20,	0 },
86
	{ "hmac-sha1",		AUTHXF_HMAC_SHA1,	20,	0 },
87
	{ "hmac-sha2-256",	AUTHXF_HMAC_SHA2_256,	32,	0 },
88
	{ "hmac-sha2-384",	AUTHXF_HMAC_SHA2_384,	48,	0 },
89
	{ "hmac-sha2-512",	AUTHXF_HMAC_SHA2_512,	64,	0 },
90
	{ NULL,			0,			0,	0 },
91
};
92
93
const struct ipsec_xf encxfs[] = {
94
	{ "unknown",		ENCXF_UNKNOWN,		0,	0,	0, 0 },
95
	{ "none",		ENCXF_NONE,		0,	0,	0, 0 },
96
	{ "3des-cbc",		ENCXF_3DES_CBC,		24,	24,	0, 0 },
97
	{ "aes",		ENCXF_AES,		16,	32,	0, 0 },
98
	{ "aes-128",		ENCXF_AES_128,		16,	16,	0, 0 },
99
	{ "aes-192",		ENCXF_AES_192,		24,	24,	0, 0 },
100
	{ "aes-256",		ENCXF_AES_256,		32,	32,	0, 0 },
101
	{ "aesctr",		ENCXF_AESCTR,		16+4,	32+4,	0, 1 },
102
	{ "aes-128-ctr",	ENCXF_AES_128_CTR,	16+4,	16+4,	0, 1 },
103
	{ "aes-192-ctr",	ENCXF_AES_192_CTR,	24+4,	24+4,	0, 1 },
104
	{ "aes-256-ctr",	ENCXF_AES_256_CTR,	32+4,	32+4,	0, 1 },
105
	{ "aes-128-gcm",	ENCXF_AES_128_GCM,	16+4,	16+4,	1, 1 },
106
	{ "aes-192-gcm",	ENCXF_AES_192_GCM,	24+4,	24+4,	1, 1 },
107
	{ "aes-256-gcm",	ENCXF_AES_256_GCM,	32+4,	32+4,	1, 1 },
108
	{ "aes-128-gmac",	ENCXF_AES_128_GMAC,	16+4,	16+4,	1, 1 },
109
	{ "aes-192-gmac",	ENCXF_AES_192_GMAC,	24+4,	24+4,	1, 1 },
110
	{ "aes-256-gmac",	ENCXF_AES_256_GMAC,	32+4,	32+4,	1, 1 },
111
	{ "blowfish",		ENCXF_BLOWFISH,		5,	56,	0, 0 },
112
	{ "cast128",		ENCXF_CAST128,		5,	16,	0, 0 },
113
	{ "chacha20-poly1305",	ENCXF_CHACHA20_POLY1305, 32+4,	32+4,	1, 1 },
114
	{ "null",		ENCXF_NULL,		0,	0,	0, 0 },
115
	{ NULL,			0,			0,	0,	0, 0 },
116
};
117
118
const struct ipsec_xf compxfs[] = {
119
	{ "unknown",		COMPXF_UNKNOWN,		0,	0 },
120
	{ "deflate",		COMPXF_DEFLATE,		0,	0 },
121
	{ "lzs",		COMPXF_LZS,		0,	0 },
122
	{ NULL,			0,			0,	0 },
123
};
124
125
const struct ipsec_xf groupxfs[] = {
126
	{ "unknown",		GROUPXF_UNKNOWN,	0,	0 },
127
	{ "none",		GROUPXF_NONE,		0,	0 },
128
	{ "modp768",		GROUPXF_1,		768,	0 },
129
	{ "grp1",		GROUPXF_1,		768,	0 },
130
	{ "modp1024",		GROUPXF_2,		1024,	0 },
131
	{ "grp2",		GROUPXF_2,		1024,	0 },
132
	{ "modp1536",		GROUPXF_5,		1536,	0 },
133
	{ "grp5",		GROUPXF_5,		1536,	0 },
134
	{ "modp2048",		GROUPXF_14,		2048,	0 },
135
	{ "grp14",		GROUPXF_14,		2048,	0 },
136
	{ "modp3072",		GROUPXF_15,		3072,	0 },
137
	{ "grp15",		GROUPXF_15,		3072,	0 },
138
	{ "modp4096",		GROUPXF_16,		4096,	0 },
139
	{ "grp16",		GROUPXF_16,		4096,	0 },
140
	{ "modp6144",		GROUPXF_17,		6144,	0 },
141
	{ "grp17",		GROUPXF_17,		6144,	0 },
142
	{ "modp8192",		GROUPXF_18,		8192,	0 },
143
	{ "grp18",		GROUPXF_18,		8192,	0 },
144
	{ "ecp256",		GROUPXF_19,		256,	0 },
145
	{ "grp19",		GROUPXF_19,		256,	0 },
146
	{ "ecp384",		GROUPXF_20,		384,	0 },
147
	{ "grp20",		GROUPXF_20,		384,	0 },
148
	{ "ecp521",		GROUPXF_21,		521,	0 },
149
	{ "grp21",		GROUPXF_21,		521,	0 },
150
	{ "ecp192",		GROUPXF_25,		192,	0 },
151
	{ "grp25",		GROUPXF_25,		192,	0 },
152
	{ "ecp224",		GROUPXF_26,		224,	0 },
153
	{ "grp26",		GROUPXF_26,		224,	0 },
154
	{ "bp224",		GROUPXF_27,		224,	0 },
155
	{ "grp27",		GROUPXF_27,		224,	0 },
156
	{ "bp256",		GROUPXF_28,		256,	0 },
157
	{ "grp28",		GROUPXF_28,		256,	0 },
158
	{ "bp384",		GROUPXF_29,		384,	0 },
159
	{ "grp29",		GROUPXF_29,		384,	0 },
160
	{ "bp512",		GROUPXF_30,		512,	0 },
161
	{ "grp30",		GROUPXF_30,		512,	0 },
162
	{ NULL,			0,			0,	0 },
163
};
164
165
int			 atoul(char *, u_long *);
166
int			 atospi(char *, u_int32_t *);
167
u_int8_t		 x2i(unsigned char *);
168
struct ipsec_key	*parsekey(unsigned char *, size_t);
169
struct ipsec_key	*parsekeyfile(char *);
170
struct ipsec_addr_wrap	*host(const char *);
171
struct ipsec_addr_wrap	*host_v6(const char *, int);
172
struct ipsec_addr_wrap	*host_v4(const char *, int);
173
struct ipsec_addr_wrap	*host_dns(const char *, int);
174
struct ipsec_addr_wrap	*host_if(const char *, int);
175
struct ipsec_addr_wrap	*host_any(void);
176
void			 ifa_load(void);
177
int			 ifa_exists(const char *);
178
struct ipsec_addr_wrap	*ifa_lookup(const char *ifa_name);
179
struct ipsec_addr_wrap	*ifa_grouplookup(const char *);
180
void			 set_ipmask(struct ipsec_addr_wrap *, u_int8_t);
181
const struct ipsec_xf	*parse_xf(const char *, const struct ipsec_xf *);
182
struct ipsec_lifetime	*parse_life(const char *);
183
struct ipsec_transforms *copytransforms(const struct ipsec_transforms *);
184
struct ipsec_lifetime	*copylife(const struct ipsec_lifetime *);
185
struct ipsec_auth	*copyipsecauth(const struct ipsec_auth *);
186
struct ike_auth		*copyikeauth(const struct ike_auth *);
187
struct ipsec_key	*copykey(struct ipsec_key *);
188
struct ipsec_addr_wrap	*copyhost(const struct ipsec_addr_wrap *);
189
char			*copytag(const char *);
190
struct ipsec_rule	*copyrule(struct ipsec_rule *);
191
int			 validate_af(struct ipsec_addr_wrap *,
192
			     struct ipsec_addr_wrap *);
193
int			 validate_sa(u_int32_t, u_int8_t,
194
			     struct ipsec_transforms *, struct ipsec_key *,
195
			     struct ipsec_key *, u_int8_t);
196
struct ipsec_rule	*create_sa(u_int8_t, u_int8_t, struct ipsec_hosts *,
197
			     u_int32_t, struct ipsec_transforms *,
198
			     struct ipsec_key *, struct ipsec_key *);
199
struct ipsec_rule	*reverse_sa(struct ipsec_rule *, u_int32_t,
200
			     struct ipsec_key *, struct ipsec_key *);
201
struct ipsec_rule	*create_sabundle(struct ipsec_addr_wrap *, u_int8_t,
202
			     u_int32_t, struct ipsec_addr_wrap *, u_int8_t,
203
			     u_int32_t);
204
struct ipsec_rule	*create_flow(u_int8_t, u_int8_t, struct ipsec_hosts *,
205
			     u_int8_t, char *, char *, u_int8_t);
206
int			 set_rule_peers(struct ipsec_rule *r,
207
			     struct ipsec_hosts *peers);
208
void			 expand_any(struct ipsec_addr_wrap *);
209
int			 expand_rule(struct ipsec_rule *, struct ipsec_hosts *,
210
			     u_int8_t, u_int32_t, struct ipsec_key *,
211
			     struct ipsec_key *, char *);
212
struct ipsec_rule	*reverse_rule(struct ipsec_rule *);
213
struct ipsec_rule	*create_ike(u_int8_t, struct ipsec_hosts *,
214
			     struct ike_mode *, struct ike_mode *, u_int8_t,
215
			     u_int8_t, u_int8_t, char *, char *,
216
			     struct ike_auth *, char *);
217
int			 add_sabundle(struct ipsec_rule *, char *);
218
int			 get_id_type(char *);
219
220
struct ipsec_transforms *ipsec_transforms;
221
222
typedef struct {
223
	union {
224
		int64_t	 	 number;
225
		u_int8_t	 ikemode;
226
		u_int8_t	 dir;
227
		u_int8_t	 satype;	/* encapsulating prococol */
228
		u_int8_t	 proto;		/* encapsulated protocol */
229
		u_int8_t	 tmode;
230
		char		*string;
231
		u_int16_t	 port;
232
		struct ipsec_hosts hosts;
233
		struct ipsec_hosts peers;
234
		struct ipsec_addr_wrap *anyhost;
235
		struct ipsec_addr_wrap *singlehost;
236
		struct ipsec_addr_wrap *host;
237
		struct {
238
			char *srcid;
239
			char *dstid;
240
		} ids;
241
		char		*id;
242
		u_int8_t	 type;
243
		struct ike_auth	 ikeauth;
244
		struct {
245
			u_int32_t	spiout;
246
			u_int32_t	spiin;
247
		} spis;
248
		struct {
249
			struct ipsec_key *keyout;
250
			struct ipsec_key *keyin;
251
		} authkeys;
252
		struct {
253
			struct ipsec_key *keyout;
254
			struct ipsec_key *keyin;
255
		} enckeys;
256
		struct {
257
			struct ipsec_key *keyout;
258
			struct ipsec_key *keyin;
259
		} keys;
260
		struct ipsec_transforms *transforms;
261
		struct ipsec_lifetime	*life;
262
		struct ike_mode		*mode;
263
	} v;
264
	int lineno;
265
} YYSTYPE;
266
267
#line 268 "parse.c"
268
#define FLOW 257
269
#define FROM 258
270
#define ESP 259
271
#define AH 260
272
#define IN 261
273
#define PEER 262
274
#define ON 263
275
#define OUT 264
276
#define TO 265
277
#define SRCID 266
278
#define DSTID 267
279
#define RSA 268
280
#define PSK 269
281
#define TCPMD5 270
282
#define SPI 271
283
#define AUTHKEY 272
284
#define ENCKEY 273
285
#define FILENAME 274
286
#define AUTHXF 275
287
#define ENCXF 276
288
#define ERROR 277
289
#define IKE 278
290
#define MAIN 279
291
#define QUICK 280
292
#define AGGRESSIVE 281
293
#define PASSIVE 282
294
#define ACTIVE 283
295
#define ANY 284
296
#define IPIP 285
297
#define IPCOMP 286
298
#define COMPXF 287
299
#define TUNNEL 288
300
#define TRANSPORT 289
301
#define DYNAMIC 290
302
#define LIFETIME 291
303
#define TYPE 292
304
#define DENY 293
305
#define BYPASS 294
306
#define LOCAL 295
307
#define PROTO 296
308
#define USE 297
309
#define ACQUIRE 298
310
#define REQUIRE 299
311
#define DONTACQ 300
312
#define GROUP 301
313
#define PORT 302
314
#define TAG 303
315
#define INCLUDE 304
316
#define BUNDLE 305
317
#define STRING 306
318
#define NUMBER 307
319
#define YYERRCODE 256
320
const short yylhs[] =
321
	{                                        -1,
322
    0,    0,    0,    0,    0,    0,    0,    0,    0,   37,
323
   37,   31,   35,   34,   33,   32,    3,    3,    3,    3,
324
    3,    4,    4,    4,    4,    5,    5,    6,    6,    6,
325
    2,    2,    2,    7,    7,    8,    8,    9,    9,   10,
326
   10,   10,   10,   10,   11,   11,   12,   12,   14,   14,
327
   15,   15,   13,   13,   13,   13,   16,   16,   16,   16,
328
   26,   26,   26,   26,   26,   26,   26,   17,   18,   18,
329
   39,   23,   23,   38,   38,   40,   40,   40,   40,   28,
330
   28,   28,   29,   29,   27,   27,   27,   19,   19,   20,
331
   20,   21,   21,   22,   22,   24,   24,   24,   24,   25,
332
   25,   25,   30,   30,    1,    1,   36,
333
};
334
const short yylen[] =
335
	{                                         2,
336
    0,    3,    2,    3,    3,    3,    3,    3,    3,    1,
337
    0,    2,    4,    8,    8,   12,    0,    1,    1,    1,
338
    1,    0,    2,    2,    2,    1,    1,    0,    1,    1,
339
    0,    1,    1,    6,    6,    0,    2,    1,    1,    0,
340
    4,    4,    2,    2,    1,    1,    0,    1,    1,    3,
341
    1,    3,    1,    4,    1,    3,    0,    4,    2,    2,
342
    0,    2,    2,    2,    2,    2,    2,    1,    2,    2,
343
    0,    2,    0,    2,    1,    2,    2,    2,    2,    0,
344
    3,    3,    0,    3,    0,    2,    2,    0,    2,    0,
345
    2,    0,    2,    1,    2,    0,    1,    1,    1,    0,
346
    1,    2,    0,    2,    2,    1,    3,
347
};
348
const short yydefred[] =
349
	{                                      1,
350
    0,    0,    0,   18,   19,    0,    0,   21,   20,    0,
351
    0,    3,    0,    0,    0,    0,    0,    0,    0,    9,
352
    0,    0,    0,    0,   97,   99,   98,    0,   12,    0,
353
   29,   30,    0,    2,    4,    5,    6,    7,    8,   32,
354
   33,    0,   55,    0,    0,    0,    0,    0,    0,    0,
355
    0,  106,    0,    0,    0,    0,    0,   49,    0,    0,
356
    0,    0,    0,   69,   70,    0,   13,    0,  105,    0,
357
   24,   25,   26,   27,   23,    0,   52,   10,   56,    0,
358
   38,   39,   37,    0,    0,    0,    0,   94,   89,    0,
359
    0,    0,    0,    0,    0,   50,    0,   54,    0,   95,
360
    0,    0,    0,    0,    0,    0,    0,   75,   46,   48,
361
    0,   45,    0,    0,    0,    0,   34,   35,    0,    0,
362
    0,   76,   77,   78,   79,   74,    0,    0,   68,    0,
363
   60,    0,   15,    0,    0,    0,   91,    0,   14,   41,
364
   42,    0,   65,   66,   62,   63,   64,   67,    0,    0,
365
    0,    0,   93,   58,    0,   81,   82,    0,    0,   87,
366
   86,   84,  101,    0,    0,  102,    0,   16,  104,
367
};
368
const short yydgoto[] =
369
	{                                       1,
370
   53,   42,   13,   56,   75,   33,   24,   61,   83,   95,
371
  111,  112,   46,   59,   47,  116,  130,   50,   67,  121,
372
  139,   89,   91,   28,  165,  133,  156,  136,  152,  168,
373
   14,   15,   16,   17,   18,   19,   80,  107,   92,  108,
374
};
375
const short yysindex[] =
376
	{                                      0,
377
   41,   -1, -176,    0,    0, -167, -203,    0,    0, -279,
378
    2,    0, -181,   30,   82,   94,  102,  104,  106,    0,
379
 -161, -120, -120, -141,    0,    0,    0, -176,    0, -172,
380
    0,    0, -167,    0,    0,    0,    0,    0,    0,    0,
381
    0, -165,    0,   93, -120, -160,  101, -160, -186, -129,
382
 -181,    0, -162, -141, -245, -167, -159,    0,  -37, -180,
383
 -119, -157, -113,    0,    0, -228,    0, -165,    0,    0,
384
    0,    0,    0,    0,    0, -246,    0,    0,    0, -120,
385
    0,    0,    0, -120,  109, -120, -155,    0,    0, -167,
386
 -129, -190, -216, -154, -138,    0, -160,    0, -160,    0,
387
 -246, -126, -153, -152, -151, -150, -190,    0,    0,    0,
388
 -137,    0, -105, -147, -147, -132,    0,    0, -258, -228,
389
 -144,    0,    0,    0,    0,    0, -154, -216,    0, -102,
390
    0, -175,    0,    0,    0, -112,    0, -139,    0,    0,
391
    0, -147,    0,    0,    0,    0,    0,    0, -122, -122,
392
    0, -138,    0,    0, -170,    0,    0, -122, -130,    0,
393
    0,    0,    0, -135, -131,    0, -133,    0,    0,};
394
const short yyrindex[] =
395
	{                                      0,
396
 -183,    0, -222,    0,    0,    0, -241,    0,    0,    0,
397
    0,    0, -164,    0,    0,    0,    0,    0,    0,    0,
398
 -232,    0,    0,    0,    0,    0,    0, -236,    0,    0,
399
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
400
    0, -163,    0,  -10,    0, -103,   10,  -84,    0,  165,
401
 -200,    0,  166,    0,    0,    0,    0,    0, -118,    0,
402
    0,    0,    0,    0,    0,    0,    0, -163,    0,   31,
403
    0,    0,    0,    0,    0,   -4,    0,    0,    0,    0,
404
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
405
   -6,    0,  122,  153,   -9,    0,   89,    0,   89,    0,
406
  218,    3,    0,    0,    0,    0,   62,    0,    0,    0,
407
  173,    0,  191,    0,    0,  167,    0,    0,  222,    0,
408
  168,    0,    0,    0,    0,    0,  200,  200,    0,   18,
409
    0,    0,    0,  107,  107,   56,    0,    0,    0,    0,
410
    0,    0,    0,    0,    0,    0,    0,    0,  239,  239,
411
  160,   46,    0,    0,    0,    0,    0,  243,   -8,    0,
412
    0,    0,    0,    0,  169,    0,    0,    0,    0,};
413
const short yygindex[] =
414
	{                                      0,
415
    0,    0,   22,  112,    0,  130,  -21,  -40,    0,   81,
416
   57,  -57,  -13,    0,  125,   32, -104,  135,   99,    0,
417
    0,   71,  -58,    0,    0,    0,  -69,    0,    0,    0,
418
    0,    0,    0,    0,    0,    0,    0,    0,    0,   85,
419
};
420
#define YYTABLESIZE 546
421
const short yytable[] =
422
	{                                      51,
423
   57,  100,   45,   88,   11,   40,   78,   63,   20,   48,
424
  131,   54,   90,   71,   72,   93,   96,   96,   96,   53,
425
  134,   17,  135,   96,   21,   31,   29,   59,   17,   51,
426
   51,   58,   31,   51,   76,   17,  113,  154,   17,   34,
427
   73,   17,   17,   96,   96,   87,   96,   96,   94,   51,
428
   12,   17,   17,   53,   96,   57,  117,   28,  118,   17,
429
   73,   74,   30,   31,   28,   83,   96,  109,  101,  140,
430
   97,   72,   99,   17,   17,  149,  150,   88,   25,   26,
431
  157,   17,    4,    5,  103,  104,   27,   79,  162,  110,
432
   22,   35,  158,   28,   22,   28,  105,   23,   36,   40,
433
   28,   22,   41,   36,   17,   17,   31,   32,    8,    9,
434
  106,   37,   51,   38,   51,   39,   73,  143,  144,   64,
435
   65,  145,  146,  147,  148,   81,   82,  114,  115,   49,
436
   55,   47,   53,   52,   53,  160,  161,  163,  164,   57,
437
   62,   60,   66,   69,   86,   84,  120,   77,   44,   98,
438
  100,  110,  122,  123,  124,  125,  128,  127,  129,  132,
439
  138,   36,   47,   43,  142,   11,  153,  151,  155,   73,
440
  166,  167,  169,   36,   88,  107,   61,   92,  103,   90,
441
   68,  119,   43,  159,  141,   44,   85,   11,   70,  102,
442
  137,  126,    0,    0,    0,    0,    0,    0,    0,    0,
443
   44,    0,    0,    0,    0,    0,    0,    0,    0,   47,
444
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
445
    0,    0,    0,    0,    0,    0,    0,   40,    0,    0,
446
    0,   80,    0,    0,    0,    0,    0,    0,    0,    0,
447
    0,    0,    0,    0,    0,    0,    0,   51,   85,    0,
448
    0,   51,   85,    0,   51,   51,   51,   51,   51,    0,
449
   51,   40,   40,    0,    0,    0,   88,   53,   51,   51,
450
   51,   53,    0,   51,   53,   53,   53,   53,   53,    0,
451
   53,   51,   57,    0,   51,   59,   59,   40,   53,   53,
452
   53,   51,   51,   53,  100,   51,    2,    3,   88,    4,
453
    5,   53,   73,   73,   53,   71,   71,   90,    0,   59,
454
    6,   53,   53,   57,   57,   53,    0,   71,    7,    0,
455
   59,   83,   83,   83,   83,    8,    9,   72,   72,   72,
456
   72,   71,    0,   72,   72,   73,    0,    0,    0,    0,
457
    0,   72,    0,    0,   10,    0,   11,    0,   57,    0,
458
   36,    0,   72,    0,   36,   36,   36,   36,   83,   36,
459
    0,    0,    0,    0,   72,    0,   72,   36,   36,   36,
460
    0,    0,   73,   73,   73,   73,    0,    0,    0,    0,
461
   36,   71,   71,   36,    0,    0,   73,   47,   47,   47,
462
   47,   36,    0,   71,    0,    0,    0,   73,    0,    0,
463
   47,   47,   47,    0,    0,    0,    0,   71,    0,   73,
464
    0,    0,    0,   47,   47,    0,   47,    0,   47,   47,
465
   47,   47,    0,    0,   47,   73,   73,   73,   73,    0,
466
    0,   47,   47,   47,   71,   71,    0,    0,   43,   43,
467
   43,   43,    0,    0,   47,    0,   71,    0,    0,    0,
468
   73,   43,   43,   43,    0,   47,   44,   44,   44,   44,
469
   71,    0,   73,    0,   43,   47,   47,   47,   47,   44,
470
   44,   44,    0,    0,    0,   43,    0,    0,   47,   47,
471
   47,    0,   44,   40,   40,   40,   40,   80,   80,   80,
472
   80,   47,    0,   44,    0,    0,   40,   40,   40,    0,
473
    0,   80,   47,    0,   85,   85,   85,   85,   85,   85,
474
   85,   85,    0,    0,    0,    0,    0,    0,   85,    0,
475
   40,    0,    0,    0,   80,    0,    0,    0,    0,    0,
476
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
477
    0,   85,    0,    0,    0,   85,
478
};
479
const short yycheck[] =
480
	{                                      10,
481
   10,   10,  123,   10,  123,   10,   44,   48,   10,   23,
482
  115,   33,   10,  259,  260,  262,  258,  259,  260,   10,
483
  279,  258,  281,  265,    3,  258,  306,   10,  265,   40,
484
   41,   45,  265,   44,   56,  258,   94,  142,  261,   10,
485
   10,  264,  265,  285,  286,  274,  288,  289,  295,   28,
486
   10,  288,  289,   44,  296,   10,   97,  258,   99,  296,
487
  306,  307,   61,  296,  265,   10,   80,  284,   90,  127,
488
   84,   10,   86,  296,  258,  134,  135,  306,  282,  283,
489
  150,  265,  259,  260,  275,  276,  290,  125,  158,  306,
490
  258,   10,  151,  258,  258,  296,  287,  265,   10,  261,
491
  265,  265,  264,   10,  288,  289,  288,  289,  285,  286,
492
  301,   10,  123,   10,  125,   10,   10,  293,  294,  306,
493
  307,  297,  298,  299,  300,  306,  307,  266,  267,  271,
494
  296,   10,  123,  306,  125,  306,  307,  268,  269,   47,
495
   40,  302,  272,  306,  258,  265,  273,  307,  306,   41,
496
  306,  306,  306,  306,  306,  306,  262,  295,  306,  292,
497
  305,  265,   10,  284,  267,  284,  306,  280,  291,   10,
498
  306,  303,  306,  258,   10,   10,   10,   10,   10,   68,
499
   51,  101,   10,  152,  128,  306,   62,  306,   54,   91,
500
  120,  107,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
501
   10,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   10,
502
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
503
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   10,   -1,   -1,
504
   -1,   10,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
505
   -1,   -1,   -1,   -1,   -1,   -1,   -1,  258,   10,   -1,
506
   -1,  262,   10,   -1,  265,  266,  267,  268,  269,   -1,
507
  271,  266,  267,   -1,   -1,   -1,  273,  258,  279,  280,
508
  281,  262,   -1,  284,  265,  266,  267,  268,  269,   -1,
509
  271,  292,  292,   -1,  295,  268,  269,  292,  279,  280,
510
  281,  302,  303,  284,  303,  306,  256,  257,  305,  259,
511
  260,  292,  272,  273,  295,  275,  276,  305,   -1,  292,
512
  270,  302,  303,  268,  269,  306,   -1,  287,  278,   -1,
513
  303,  266,  267,  268,  269,  285,  286,  266,  267,  268,
514
  269,  301,   -1,  272,  273,  305,   -1,   -1,   -1,   -1,
515
   -1,  280,   -1,   -1,  304,   -1,  306,   -1,  303,   -1,
516
  262,   -1,  291,   -1,  266,  267,  268,  269,  303,  271,
517
   -1,   -1,   -1,   -1,  303,   -1,  305,  279,  280,  281,
518
   -1,   -1,  266,  267,  268,  269,   -1,   -1,   -1,   -1,
519
  292,  275,  276,  295,   -1,   -1,  280,  266,  267,  268,
520
  269,  303,   -1,  287,   -1,   -1,   -1,  291,   -1,   -1,
521
  279,  280,  281,   -1,   -1,   -1,   -1,  301,   -1,  303,
522
   -1,   -1,   -1,  292,  262,   -1,  295,   -1,  266,  267,
523
  268,  269,   -1,   -1,  303,  266,  267,  268,  269,   -1,
524
   -1,  279,  280,  281,  275,  276,   -1,   -1,  266,  267,
525
  268,  269,   -1,   -1,  292,   -1,  287,   -1,   -1,   -1,
526
  291,  279,  280,  281,   -1,  303,  266,  267,  268,  269,
527
  301,   -1,  303,   -1,  292,  266,  267,  268,  269,  279,
528
  280,  281,   -1,   -1,   -1,  303,   -1,   -1,  279,  280,
529
  281,   -1,  292,  266,  267,  268,  269,  266,  267,  268,
530
  269,  292,   -1,  303,   -1,   -1,  279,  280,  281,   -1,
531
   -1,  280,  303,   -1,  266,  267,  268,  269,  266,  267,
532
  268,  269,   -1,   -1,   -1,   -1,   -1,   -1,  280,   -1,
533
  303,   -1,   -1,   -1,  303,   -1,   -1,   -1,   -1,   -1,
534
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
535
   -1,  303,   -1,   -1,   -1,  303,
536
};
537
#define YYFINAL 1
538
#ifndef YYDEBUG
539
#define YYDEBUG 0
540
#endif
541
#define YYMAXTOKEN 307
542
#if YYDEBUG
543
const char * const yyname[] =
544
	{
545
"end-of-file",0,0,0,0,0,0,0,0,0,"'\\n'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
546
0,0,0,0,0,0,0,0,0,"'('","')'",0,0,"','",0,0,"'/'",0,0,0,0,0,0,0,0,0,0,0,0,0,
547
"'='",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
548
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"'{'",0,"'}'",0,0,0,0,0,0,0,0,0,
549
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
550
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
551
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
552
0,0,"FLOW","FROM","ESP","AH","IN","PEER","ON","OUT","TO","SRCID","DSTID","RSA",
553
"PSK","TCPMD5","SPI","AUTHKEY","ENCKEY","FILENAME","AUTHXF","ENCXF","ERROR",
554
"IKE","MAIN","QUICK","AGGRESSIVE","PASSIVE","ACTIVE","ANY","IPIP","IPCOMP",
555
"COMPXF","TUNNEL","TRANSPORT","DYNAMIC","LIFETIME","TYPE","DENY","BYPASS",
556
"LOCAL","PROTO","USE","ACQUIRE","REQUIRE","DONTACQ","GROUP","PORT","TAG",
557
"INCLUDE","BUNDLE","STRING","NUMBER",
558
};
559
const char * const yyrule[] =
560
	{"$accept : grammar",
561
"grammar :",
562
"grammar : grammar include '\\n'",
563
"grammar : grammar '\\n'",
564
"grammar : grammar ikerule '\\n'",
565
"grammar : grammar flowrule '\\n'",
566
"grammar : grammar sarule '\\n'",
567
"grammar : grammar tcpmd5rule '\\n'",
568
"grammar : grammar varset '\\n'",
569
"grammar : grammar error '\\n'",
570
"comma : ','",
571
"comma :",
572
"include : INCLUDE STRING",
573
"tcpmd5rule : TCPMD5 hosts spispec authkeyspec",
574
"sarule : satype tmode hosts spispec transforms authkeyspec enckeyspec bundlestring",
575
"flowrule : FLOW satype dir proto hosts peers ids type",
576
"ikerule : IKE ikemode satype tmode proto hosts peers phase1mode phase2mode ids ikeauth tag",
577
"satype :",
578
"satype : ESP",
579
"satype : AH",
580
"satype : IPCOMP",
581
"satype : IPIP",
582
"proto :",
583
"proto : PROTO protoval",
584
"proto : PROTO ESP",
585
"proto : PROTO AH",
586
"protoval : STRING",
587
"protoval : NUMBER",
588
"tmode :",
589
"tmode : TUNNEL",
590
"tmode : TRANSPORT",
591
"dir :",
592
"dir : IN",
593
"dir : OUT",
594
"hosts : FROM host port TO host port",
595
"hosts : TO host port FROM host port",
596
"port :",
597
"port : PORT portval",
598
"portval : STRING",
599
"portval : NUMBER",
600
"peers :",
601
"peers : PEER anyhost LOCAL singlehost",
602
"peers : LOCAL singlehost PEER anyhost",
603
"peers : PEER anyhost",
604
"peers : LOCAL singlehost",
605
"anyhost : singlehost",
606
"anyhost : ANY",
607
"singlehost :",
608
"singlehost : STRING",
609
"host_list : host",
610
"host_list : host_list comma host",
611
"host_spec : STRING",
612
"host_spec : STRING '/' NUMBER",
613
"host : host_spec",
614
"host : host_spec '(' host_spec ')'",
615
"host : ANY",
616
"host : '{' host_list '}'",
617
"ids :",
618
"ids : SRCID id DSTID id",
619
"ids : SRCID id",
620
"ids : DSTID id",
621
"type :",
622
"type : TYPE USE",
623
"type : TYPE ACQUIRE",
624
"type : TYPE REQUIRE",
625
"type : TYPE DENY",
626
"type : TYPE BYPASS",
627
"type : TYPE DONTACQ",
628
"id : STRING",
629
"spispec : SPI STRING",
630
"spispec : SPI NUMBER",
631
"$$1 :",
632
"transforms : $$1 transforms_l",
633
"transforms :",
634
"transforms_l : transforms_l transform",
635
"transforms_l : transform",
636
"transform : AUTHXF STRING",
637
"transform : ENCXF STRING",
638
"transform : COMPXF STRING",
639
"transform : GROUP STRING",
640
"phase1mode :",
641
"phase1mode : MAIN transforms lifetime",
642
"phase1mode : AGGRESSIVE transforms lifetime",
643
"phase2mode :",
644
"phase2mode : QUICK transforms lifetime",
645
"lifetime :",
646
"lifetime : LIFETIME NUMBER",
647
"lifetime : LIFETIME STRING",
648
"authkeyspec :",
649
"authkeyspec : AUTHKEY keyspec",
650
"enckeyspec :",
651
"enckeyspec : ENCKEY keyspec",
652
"bundlestring :",
653
"bundlestring : BUNDLE STRING",
654
"keyspec : STRING",
655
"keyspec : FILENAME STRING",
656
"ikemode :",
657
"ikemode : PASSIVE",
658
"ikemode : DYNAMIC",
659
"ikemode : ACTIVE",
660
"ikeauth :",
661
"ikeauth : RSA",
662
"ikeauth : PSK STRING",
663
"tag :",
664
"tag : TAG STRING",
665
"string : string STRING",
666
"string : STRING",
667
"varset : STRING '=' string",
668
};
669
#endif
670
#ifdef YYSTACKSIZE
671
#undef YYMAXDEPTH
672
#define YYMAXDEPTH YYSTACKSIZE
673
#else
674
#ifdef YYMAXDEPTH
675
#define YYSTACKSIZE YYMAXDEPTH
676
#else
677
#define YYSTACKSIZE 10000
678
#define YYMAXDEPTH 10000
679
#endif
680
#endif
681
#define YYINITSTACKSIZE 200
682
/* LINTUSED */
683
int yydebug;
684
int yynerrs;
685
int yyerrflag;
686
int yychar;
687
short *yyssp;
688
YYSTYPE *yyvsp;
689
YYSTYPE yyval;
690
YYSTYPE yylval;
691
short *yyss;
692
short *yysslim;
693
YYSTYPE *yyvs;
694
unsigned int yystacksize;
695
int yyparse(void);
696
#line 925 "parse.y"
697
698
struct keywords {
699
	const char	*k_name;
700
	int		 k_val;
701
};
702
703
int
704
yyerror(const char *fmt, ...)
705
{
706
	va_list		 ap;
707
708
	file->errors++;
709
	va_start(ap, fmt);
710
	fprintf(stderr, "%s: %d: ", file->name, yylval.lineno);
711
	vfprintf(stderr, fmt, ap);
712
	fprintf(stderr, "\n");
713
	va_end(ap);
714
	return (0);
715
}
716
717
int
718
yywarn(const char *fmt, ...)
719
{
720
	va_list		 ap;
721
722
	va_start(ap, fmt);
723
	fprintf(stderr, "%s: %d: ", file->name, yylval.lineno);
724
	vfprintf(stderr, fmt, ap);
725
	fprintf(stderr, "\n");
726
	va_end(ap);
727
	return (0);
728
}
729
730
int
731
kw_cmp(const void *k, const void *e)
732
{
733
	return (strcmp(k, ((const struct keywords *)e)->k_name));
734
}
735
736
int
737
lookup(char *s)
738
{
739
	/* this has to be sorted always */
740
	static const struct keywords keywords[] = {
741
		{ "acquire",		ACQUIRE },
742
		{ "active",		ACTIVE },
743
		{ "aggressive",		AGGRESSIVE },
744
		{ "ah",			AH },
745
		{ "any",		ANY },
746
		{ "auth",		AUTHXF },
747
		{ "authkey",		AUTHKEY },
748
		{ "bundle",		BUNDLE },
749
		{ "bypass",		BYPASS },
750
		{ "comp",		COMPXF },
751
		{ "deny",		DENY },
752
		{ "dontacq",		DONTACQ },
753
		{ "dstid",		DSTID },
754
		{ "dynamic",		DYNAMIC },
755
		{ "enc",		ENCXF },
756
		{ "enckey",		ENCKEY },
757
		{ "esp",		ESP },
758
		{ "file",		FILENAME },
759
		{ "flow",		FLOW },
760
		{ "from",		FROM },
761
		{ "group",		GROUP },
762
		{ "ike",		IKE },
763
		{ "in",			IN },
764
		{ "include",		INCLUDE },
765
		{ "ipcomp",		IPCOMP },
766
		{ "ipip",		IPIP },
767
		{ "lifetime",		LIFETIME },
768
		{ "local",		LOCAL },
769
		{ "main",		MAIN },
770
		{ "out",		OUT },
771
		{ "passive",		PASSIVE },
772
		{ "peer",		PEER },
773
		{ "port",		PORT },
774
		{ "proto",		PROTO },
775
		{ "psk",		PSK },
776
		{ "quick",		QUICK },
777
		{ "require",		REQUIRE },
778
		{ "rsa",		RSA },
779
		{ "spi",		SPI },
780
		{ "srcid",		SRCID },
781
		{ "tag",		TAG },
782
		{ "tcpmd5",		TCPMD5 },
783
		{ "to",			TO },
784
		{ "transport",		TRANSPORT },
785
		{ "tunnel",		TUNNEL },
786
		{ "type",		TYPE },
787
		{ "use",		USE }
788
	};
789
	const struct keywords	*p;
790
791
	p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
792
	    sizeof(keywords[0]), kw_cmp);
793
794
	if (p) {
795
		if (debug > 1)
796
			fprintf(stderr, "%s: %d\n", s, p->k_val);
797
		return (p->k_val);
798
	} else {
799
		if (debug > 1)
800
			fprintf(stderr, "string: %s\n", s);
801
		return (STRING);
802
	}
803
}
804
805
#define MAXPUSHBACK	128
806
807
u_char	*parsebuf;
808
int	 parseindex;
809
u_char	 pushback_buffer[MAXPUSHBACK];
810
int	 pushback_index = 0;
811
812
int
813
lgetc(int quotec)
814
{
815
	int		c, next;
816
817
	if (parsebuf) {
818
		/* Read character from the parsebuffer instead of input. */
819
		if (parseindex >= 0) {
820
			c = parsebuf[parseindex++];
821
			if (c != '\0')
822
				return (c);
823
			parsebuf = NULL;
824
		} else
825
			parseindex++;
826
	}
827
828
	if (pushback_index)
829
		return (pushback_buffer[--pushback_index]);
830
831
	if (quotec) {
832
		if ((c = getc(file->stream)) == EOF) {
833
			yyerror("reached end of file while parsing quoted string");
834
			if (popfile() == EOF)
835
				return (EOF);
836
			return (quotec);
837
		}
838
		return (c);
839
	}
840
841
	while ((c = getc(file->stream)) == '\\') {
842
		next = getc(file->stream);
843
		if (next != '\n') {
844
			c = next;
845
			break;
846
		}
847
		yylval.lineno = file->lineno;
848
		file->lineno++;
849
	}
850
851
	while (c == EOF) {
852
		if (popfile() == EOF)
853
			return (EOF);
854
		c = getc(file->stream);
855
	}
856
	return (c);
857
}
858
859
int
860
lungetc(int c)
861
{
862
	if (c == EOF)
863
		return (EOF);
864
	if (parsebuf) {
865
		parseindex--;
866
		if (parseindex >= 0)
867
			return (c);
868
	}
869
	if (pushback_index < MAXPUSHBACK-1)
870
		return (pushback_buffer[pushback_index++] = c);
871
	else
872
		return (EOF);
873
}
874
875
int
876
findeol(void)
877
{
878
	int	c;
879
880
	parsebuf = NULL;
881
882
	/* skip to either EOF or the first real EOL */
883
	while (1) {
884
		if (pushback_index)
885
			c = pushback_buffer[--pushback_index];
886
		else
887
			c = lgetc(0);
888
		if (c == '\n') {
889
			file->lineno++;
890
			break;
891
		}
892
		if (c == EOF)
893
			break;
894
	}
895
	return (ERROR);
896
}
897
898
int
899
yylex(void)
900
{
901
	u_char	 buf[8096];
902
	u_char	*p, *val;
903
	int	 quotec, next, c;
904
	int	 token;
905
906
top:
907
	p = buf;
908
	while ((c = lgetc(0)) == ' ' || c == '\t')
909
		; /* nothing */
910
911
	yylval.lineno = file->lineno;
912
	if (c == '#')
913
		while ((c = lgetc(0)) != '\n' && c != EOF)
914
			; /* nothing */
915
	if (c == '$' && parsebuf == NULL) {
916
		while (1) {
917
			if ((c = lgetc(0)) == EOF)
918
				return (0);
919
920
			if (p + 1 >= buf + sizeof(buf) - 1) {
921
				yyerror("string too long");
922
				return (findeol());
923
			}
924
			if (isalnum(c) || c == '_') {
925
				*p++ = c;
926
				continue;
927
			}
928
			*p = '\0';
929
			lungetc(c);
930
			break;
931
		}
932
		val = symget(buf);
933
		if (val == NULL) {
934
			yyerror("macro '%s' not defined", buf);
935
			return (findeol());
936
		}
937
		parsebuf = val;
938
		parseindex = 0;
939
		goto top;
940
	}
941
942
	switch (c) {
943
	case '\'':
944
	case '"':
945
		quotec = c;
946
		while (1) {
947
			if ((c = lgetc(quotec)) == EOF)
948
				return (0);
949
			if (c == '\n') {
950
				file->lineno++;
951
				continue;
952
			} else if (c == '\\') {
953
				if ((next = lgetc(quotec)) == EOF)
954
					return (0);
955
				if (next == quotec || c == ' ' || c == '\t')
956
					c = next;
957
				else if (next == '\n') {
958
					file->lineno++;
959
					continue;
960
				} else
961
					lungetc(next);
962
			} else if (c == quotec) {
963
				*p = '\0';
964
				break;
965
			} else if (c == '\0') {
966
				yyerror("syntax error");
967
				return (findeol());
968
			}
969
			if (p + 1 >= buf + sizeof(buf) - 1) {
970
				yyerror("string too long");
971
				return (findeol());
972
			}
973
			*p++ = c;
974
		}
975
		yylval.v.string = strdup(buf);
976
		if (yylval.v.string == NULL)
977
			err(1, "yylex: strdup");
978
		return (STRING);
979
	}
980
981
#define allowed_to_end_number(x) \
982
	(isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
983
984
	if (c == '-' || isdigit(c)) {
985
		do {
986
			*p++ = c;
987
			if ((unsigned)(p-buf) >= sizeof(buf)) {
988
				yyerror("string too long");
989
				return (findeol());
990
			}
991
		} while ((c = lgetc(0)) != EOF && isdigit(c));
992
		lungetc(c);
993
		if (p == buf + 1 && buf[0] == '-')
994
			goto nodigits;
995
		if (c == EOF || allowed_to_end_number(c)) {
996
			const char *errstr = NULL;
997
998
			*p = '\0';
999
			yylval.v.number = strtonum(buf, LLONG_MIN,
1000
			    LLONG_MAX, &errstr);
1001
			if (errstr) {
1002
				yyerror("\"%s\" invalid number: %s",
1003
				    buf, errstr);
1004
				return (findeol());
1005
			}
1006
			return (NUMBER);
1007
		} else {
1008
nodigits:
1009
			while (p > buf + 1)
1010
				lungetc(*--p);
1011
			c = *--p;
1012
			if (c == '-')
1013
				return (c);
1014
		}
1015
	}
1016
1017
#define allowed_in_string(x) \
1018
	(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
1019
	x != '{' && x != '}' && x != '<' && x != '>' && \
1020
	x != '!' && x != '=' && x != '/' && x != '#' && \
1021
	x != ','))
1022
1023
	if (isalnum(c) || c == ':' || c == '_' || c == '*') {
1024
		do {
1025
			*p++ = c;
1026
			if ((unsigned)(p-buf) >= sizeof(buf)) {
1027
				yyerror("string too long");
1028
				return (findeol());
1029
			}
1030
		} while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
1031
		lungetc(c);
1032
		*p = '\0';
1033
		if ((token = lookup(buf)) == STRING)
1034
			if ((yylval.v.string = strdup(buf)) == NULL)
1035
				err(1, "yylex: strdup");
1036
		return (token);
1037
	}
1038
	if (c == '\n') {
1039
		yylval.lineno = file->lineno;
1040
		file->lineno++;
1041
	}
1042
	if (c == EOF)
1043
		return (0);
1044
	return (c);
1045
}
1046
1047
int
1048
check_file_secrecy(int fd, const char *fname)
1049
{
1050
	struct stat	st;
1051
1052
	if (fstat(fd, &st)) {
1053
		warn("cannot stat %s", fname);
1054
		return (-1);
1055
	}
1056
	if (st.st_uid != 0 && st.st_uid != getuid()) {
1057
		warnx("%s: owner not root or current user", fname);
1058
		return (-1);
1059
	}
1060
	if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
1061
		warnx("%s: group writable or world read/writable", fname);
1062
		return (-1);
1063
	}
1064
	return (0);
1065
}
1066
1067
struct file *
1068
pushfile(const char *name, int secret)
1069
{
1070
	struct file	*nfile;
1071
1072
	if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
1073
		warn("malloc");
1074
		return (NULL);
1075
	}
1076
	if ((nfile->name = strdup(name)) == NULL) {
1077
		warn("malloc");
1078
		free(nfile);
1079
		return (NULL);
1080
	}
1081
	if (TAILQ_FIRST(&files) == NULL && strcmp(nfile->name, "-") == 0) {
1082
		nfile->stream = stdin;
1083
		free(nfile->name);
1084
		if ((nfile->name = strdup("stdin")) == NULL) {
1085
			warn("strdup");
1086
			free(nfile);
1087
			return (NULL);
1088
		}
1089
	} else if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
1090
		warn("%s", nfile->name);
1091
		free(nfile->name);
1092
		free(nfile);
1093
		return (NULL);
1094
	} else if (secret &&
1095
	    check_file_secrecy(fileno(nfile->stream), nfile->name)) {
1096
		fclose(nfile->stream);
1097
		free(nfile->name);
1098
		free(nfile);
1099
		return (NULL);
1100
	}
1101
	nfile->lineno = 1;
1102
	TAILQ_INSERT_TAIL(&files, nfile, entry);
1103
	return (nfile);
1104
}
1105
1106
int
1107
popfile(void)
1108
{
1109
	struct file	*prev;
1110
1111
	if ((prev = TAILQ_PREV(file, files, entry)) != NULL) {
1112
		prev->errors += file->errors;
1113
		TAILQ_REMOVE(&files, file, entry);
1114
		fclose(file->stream);
1115
		free(file->name);
1116
		free(file);
1117
		file = prev;
1118
		return (0);
1119
	}
1120
	return (EOF);
1121
}
1122
1123
int
1124
parse_rules(const char *filename, struct ipsecctl *ipsecx)
1125
{
1126
	struct sym	*sym;
1127
	int		 errors = 0;
1128
1129
	ipsec = ipsecx;
1130
1131
	if ((file = pushfile(filename, 1)) == NULL) {
1132
		return (-1);
1133
	}
1134
1135
	yyparse();
1136
	errors = file->errors;
1137
	popfile();
1138
1139
	/* Free macros and check which have not been used. */
1140
	while ((sym = TAILQ_FIRST(&symhead))) {
1141
		if ((ipsec->opts & IPSECCTL_OPT_VERBOSE2) && !sym->used)
1142
			fprintf(stderr, "warning: macro '%s' not "
1143
			    "used\n", sym->nam);
1144
		free(sym->nam);
1145
		free(sym->val);
1146
		TAILQ_REMOVE(&symhead, sym, entry);
1147
		free(sym);
1148
	}
1149
1150
	return (errors ? -1 : 0);
1151
}
1152
1153
int
1154
symset(const char *nam, const char *val, int persist)
1155
{
1156
	struct sym	*sym;
1157
1158
	TAILQ_FOREACH(sym, &symhead, entry) {
1159
		if (strcmp(nam, sym->nam) == 0)
1160
			break;
1161
	}
1162
1163
	if (sym != NULL) {
1164
		if (sym->persist == 1)
1165
			return (0);
1166
		else {
1167
			free(sym->nam);
1168
			free(sym->val);
1169
			TAILQ_REMOVE(&symhead, sym, entry);
1170
			free(sym);
1171
		}
1172
	}
1173
	if ((sym = calloc(1, sizeof(*sym))) == NULL)
1174
		return (-1);
1175
1176
	sym->nam = strdup(nam);
1177
	if (sym->nam == NULL) {
1178
		free(sym);
1179
		return (-1);
1180
	}
1181
	sym->val = strdup(val);
1182
	if (sym->val == NULL) {
1183
		free(sym->nam);
1184
		free(sym);
1185
		return (-1);
1186
	}
1187
	sym->used = 0;
1188
	sym->persist = persist;
1189
	TAILQ_INSERT_TAIL(&symhead, sym, entry);
1190
	return (0);
1191
}
1192
1193
int
1194
cmdline_symset(char *s)
1195
{
1196
	char	*sym, *val;
1197
	int	ret;
1198
	size_t	len;
1199
1200
	if ((val = strrchr(s, '=')) == NULL)
1201
		return (-1);
1202
1203
	len = strlen(s) - strlen(val) + 1;
1204
	if ((sym = malloc(len)) == NULL)
1205
		err(1, "cmdline_symset: malloc");
1206
1207
	strlcpy(sym, s, len);
1208
1209
	ret = symset(sym, val + 1, 1);
1210
	free(sym);
1211
1212
	return (ret);
1213
}
1214
1215
char *
1216
symget(const char *nam)
1217
{
1218
	struct sym	*sym;
1219
1220
	TAILQ_FOREACH(sym, &symhead, entry) {
1221
		if (strcmp(nam, sym->nam) == 0) {
1222
			sym->used = 1;
1223
			return (sym->val);
1224
		}
1225
	}
1226
	return (NULL);
1227
}
1228
1229
int
1230
atoul(char *s, u_long *ulvalp)
1231
{
1232
	u_long	 ulval;
1233
	char	*ep;
1234
1235
	errno = 0;
1236
	ulval = strtoul(s, &ep, 0);
1237
	if (s[0] == '\0' || *ep != '\0')
1238
		return (-1);
1239
	if (errno == ERANGE && ulval == ULONG_MAX)
1240
		return (-1);
1241
	*ulvalp = ulval;
1242
	return (0);
1243
}
1244
1245
int
1246
atospi(char *s, u_int32_t *spivalp)
1247
{
1248
	unsigned long	ulval;
1249
1250
	if (atoul(s, &ulval) == -1)
1251
		return (-1);
1252
	if (ulval > UINT_MAX) {
1253
		yyerror("%lu not a valid spi", ulval);
1254
		return (-1);
1255
	}
1256
	if (ulval >= SPI_RESERVED_MIN && ulval <= SPI_RESERVED_MAX) {
1257
		yyerror("%lu within reserved spi range", ulval);
1258
		return (-1);
1259
	}
1260
	*spivalp = ulval;
1261
	return (0);
1262
}
1263
1264
u_int8_t
1265
x2i(unsigned char *s)
1266
{
1267
	char	ss[3];
1268
1269
	ss[0] = s[0];
1270
	ss[1] = s[1];
1271
	ss[2] = 0;
1272
1273
	if (!isxdigit(s[0]) || !isxdigit(s[1])) {
1274
		yyerror("keys need to be specified in hex digits");
1275
		return (-1);
1276
	}
1277
	return ((u_int8_t)strtoul(ss, NULL, 16));
1278
}
1279
1280
struct ipsec_key *
1281
parsekey(unsigned char *hexkey, size_t len)
1282
{
1283
	struct ipsec_key *key;
1284
	int		  i;
1285
1286
	key = calloc(1, sizeof(struct ipsec_key));
1287
	if (key == NULL)
1288
		err(1, "parsekey: calloc");
1289
1290
	key->len = len / 2;
1291
	key->data = calloc(key->len, sizeof(u_int8_t));
1292
	if (key->data == NULL)
1293
		err(1, "parsekey: calloc");
1294
1295
	for (i = 0; i < (int)key->len; i++)
1296
		key->data[i] = x2i(hexkey + 2 * i);
1297
1298
	return (key);
1299
}
1300
1301
struct ipsec_key *
1302
parsekeyfile(char *filename)
1303
{
1304
	struct stat	 sb;
1305
	int		 fd;
1306
	unsigned char	*hex;
1307
1308
	if ((fd = open(filename, O_RDONLY)) < 0)
1309
		err(1, "open %s", filename);
1310
	if (fstat(fd, &sb) < 0)
1311
		err(1, "parsekeyfile: stat %s", filename);
1312
	if ((sb.st_size > KEYSIZE_LIMIT) || (sb.st_size == 0))
1313
		errx(1, "%s: key too %s", filename, sb.st_size ? "large" :
1314
		    "small");
1315
	if ((hex = calloc(sb.st_size, sizeof(unsigned char))) == NULL)
1316
		err(1, "parsekeyfile: calloc");
1317
	if (read(fd, hex, sb.st_size) < sb.st_size)
1318
		err(1, "parsekeyfile: read");
1319
	close(fd);
1320
	return (parsekey(hex, sb.st_size));
1321
}
1322
1323
int
1324
get_id_type(char *string)
1325
{
1326
	struct in6_addr ia;
1327
1328
	if (string == NULL)
1329
		return (ID_UNKNOWN);
1330
1331
	if (inet_pton(AF_INET, string, &ia) == 1)
1332
		return (ID_IPV4);
1333
	else if (inet_pton(AF_INET6, string, &ia) == 1)
1334
		return (ID_IPV6);
1335
	else if (strchr(string, '@'))
1336
		return (ID_UFQDN);
1337
	else
1338
		return (ID_FQDN);
1339
}
1340
1341
struct ipsec_addr_wrap *
1342
host(const char *s)
1343
{
1344
	struct ipsec_addr_wrap	*ipa = NULL;
1345
	int			 mask, cont = 1;
1346
	char			*p, *q, *ps;
1347
1348
	if ((p = strrchr(s, '/')) != NULL) {
1349
		errno = 0;
1350
		mask = strtol(p + 1, &q, 0);
1351
		if (errno == ERANGE || !q || *q || mask > 128 || q == (p + 1))
1352
			errx(1, "host: invalid netmask '%s'", p);
1353
		if ((ps = malloc(strlen(s) - strlen(p) + 1)) == NULL)
1354
			err(1, "host: calloc");
1355
		strlcpy(ps, s, strlen(s) - strlen(p) + 1);
1356
	} else {
1357
		if ((ps = strdup(s)) == NULL)
1358
			err(1, "host: strdup");
1359
		mask = -1;
1360
	}
1361
1362
	/* Does interface with this name exist? */
1363
	if (cont && (ipa = host_if(ps, mask)) != NULL)
1364
		cont = 0;
1365
1366
	/* IPv4 address? */
1367
	if (cont && (ipa = host_v4(s, mask == -1 ? 32 : mask)) != NULL)
1368
		cont = 0;
1369
1370
	/* IPv6 address? */
1371
	if (cont && (ipa = host_v6(ps, mask == -1 ? 128 : mask)) != NULL)
1372
		cont = 0;
1373
1374
	/* dns lookup */
1375
	if (cont && mask == -1 && (ipa = host_dns(s, mask)) != NULL)
1376
		cont = 0;
1377
	free(ps);
1378
1379
	if (ipa == NULL || cont == 1) {
1380
		fprintf(stderr, "no IP address found for %s\n", s);
1381
		return (NULL);
1382
	}
1383
	return (ipa);
1384
}
1385
1386
struct ipsec_addr_wrap *
1387
host_v6(const char *s, int prefixlen)
1388
{
1389
	struct ipsec_addr_wrap	*ipa = NULL;
1390
	struct addrinfo		 hints, *res;
1391
	char			 hbuf[NI_MAXHOST];
1392
1393
	bzero(&hints, sizeof(struct addrinfo));
1394
	hints.ai_family = AF_INET6;
1395
	hints.ai_socktype = SOCK_STREAM;
1396
	hints.ai_flags = AI_NUMERICHOST;
1397
	if (getaddrinfo(s, NULL, &hints, &res))
1398
		return (NULL);
1399
	if (res->ai_next)
1400
		err(1, "host_v6: numeric hostname expanded to multiple item");
1401
1402
	ipa = calloc(1, sizeof(struct ipsec_addr_wrap));
1403
	if (ipa == NULL)
1404
		err(1, "host_v6: calloc");
1405
	ipa->af = res->ai_family;
1406
	memcpy(&ipa->address.v6,
1407
	    &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1408
	    sizeof(struct in6_addr));
1409
	if (prefixlen > 128)
1410
		prefixlen = 128;
1411
	ipa->next = NULL;
1412
	ipa->tail = ipa;
1413
1414
	set_ipmask(ipa, prefixlen);
1415
	if (getnameinfo(res->ai_addr, res->ai_addrlen,
1416
	    hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST)) {
1417
		errx(1, "could not get a numeric hostname");
1418
	}
1419
1420
	if (prefixlen != 128) {
1421
		ipa->netaddress = 1;
1422
		if (asprintf(&ipa->name, "%s/%d", hbuf, prefixlen) == -1)
1423
			err(1, "host_v6: asprintf");
1424
	} else {
1425
		if ((ipa->name = strdup(hbuf)) == NULL)
1426
			err(1, "host_v6: strdup");
1427
	}
1428
1429
	freeaddrinfo(res);
1430
1431
	return (ipa);
1432
}
1433
1434
struct ipsec_addr_wrap *
1435
host_v4(const char *s, int mask)
1436
{
1437
	struct ipsec_addr_wrap	*ipa = NULL;
1438
	struct in_addr		 ina;
1439
	int			 bits = 32;
1440
1441
	bzero(&ina, sizeof(struct in_addr));
1442
	if (strrchr(s, '/') != NULL) {
1443
		if ((bits = inet_net_pton(AF_INET, s, &ina, sizeof(ina))) == -1)
1444
			return (NULL);
1445
	} else {
1446
		if (inet_pton(AF_INET, s, &ina) != 1)
1447
			return (NULL);
1448
	}
1449
1450
	ipa = calloc(1, sizeof(struct ipsec_addr_wrap));
1451
	if (ipa == NULL)
1452
		err(1, "host_v4: calloc");
1453
1454
	ipa->address.v4 = ina;
1455
	ipa->name = strdup(s);
1456
	if (ipa->name == NULL)
1457
		err(1, "host_v4: strdup");
1458
	ipa->af = AF_INET;
1459
	ipa->next = NULL;
1460
	ipa->tail = ipa;
1461
1462
	set_ipmask(ipa, bits);
1463
	if (strrchr(s, '/') != NULL)
1464
		ipa->netaddress = 1;
1465
1466
	return (ipa);
1467
}
1468
1469
struct ipsec_addr_wrap *
1470
host_dns(const char *s, int mask)
1471
{
1472
	struct ipsec_addr_wrap	*ipa = NULL, *head = NULL;
1473
	struct addrinfo		 hints, *res0, *res;
1474
	int			 error;
1475
	char			 hbuf[NI_MAXHOST];
1476
1477
	bzero(&hints, sizeof(struct addrinfo));
1478
	hints.ai_family = PF_UNSPEC;
1479
	hints.ai_socktype = SOCK_STREAM;
1480
	error = getaddrinfo(s, NULL, &hints, &res0);
1481
	if (error)
1482
		return (NULL);
1483
1484
	for (res = res0; res; res = res->ai_next) {
1485
		if (res->ai_family != AF_INET && res->ai_family != AF_INET6)
1486
			continue;
1487
1488
		ipa = calloc(1, sizeof(struct ipsec_addr_wrap));
1489
		if (ipa == NULL)
1490
			err(1, "host_dns: calloc");
1491
		switch (res->ai_family) {
1492
		case AF_INET:
1493
			memcpy(&ipa->address.v4,
1494
			    &((struct sockaddr_in *)res->ai_addr)->sin_addr,
1495
			    sizeof(struct in_addr));
1496
			break;
1497
		case AF_INET6:
1498
			/* XXX we do not support scoped IPv6 address yet */
1499
			if (((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id) {
1500
				free(ipa);
1501
				continue;
1502
			}
1503
			memcpy(&ipa->address.v6,
1504
			    &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1505
			    sizeof(struct in6_addr));
1506
			break;
1507
		}
1508
		error = getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
1509
		    sizeof(hbuf), NULL, 0, NI_NUMERICHOST);
1510
		if (error)
1511
			err(1, "host_dns: getnameinfo");
1512
		ipa->name = strdup(hbuf);
1513
		if (ipa->name == NULL)
1514
			err(1, "host_dns: strdup");
1515
		ipa->af = res->ai_family;
1516
		ipa->next = NULL;
1517
		ipa->tail = ipa;
1518
		if (head == NULL)
1519
			head = ipa;
1520
		else {
1521
			head->tail->next = ipa;
1522
			head->tail = ipa;
1523
		}
1524
1525
		/*
1526
		 * XXX for now, no netmask support for IPv6.
1527
		 * but since there's no way to specify address family, once you
1528
		 * have IPv6 address on a host, you cannot use dns/netmask
1529
		 * syntax.
1530
		 */
1531
		if (ipa->af == AF_INET)
1532
			set_ipmask(ipa, mask == -1 ? 32 : mask);
1533
		else
1534
			if (mask != -1)
1535
				err(1, "host_dns: cannot apply netmask "
1536
				    "on non-IPv4 address");
1537
	}
1538
	freeaddrinfo(res0);
1539
1540
	return (head);
1541
}
1542
1543
struct ipsec_addr_wrap *
1544
host_if(const char *s, int mask)
1545
{
1546
	struct ipsec_addr_wrap *ipa = NULL;
1547
1548
	if (ifa_exists(s))
1549
		ipa = ifa_lookup(s);
1550
1551
	return (ipa);
1552
}
1553
1554
struct ipsec_addr_wrap *
1555
host_any(void)
1556
{
1557
	struct ipsec_addr_wrap	*ipa;
1558
1559
	ipa = calloc(1, sizeof(struct ipsec_addr_wrap));
1560
	if (ipa == NULL)
1561
		err(1, "host_any: calloc");
1562
	ipa->af = AF_UNSPEC;
1563
	ipa->netaddress = 1;
1564
	ipa->tail = ipa;
1565
	return (ipa);
1566
}
1567
1568
/* interface lookup routintes */
1569
1570
struct ipsec_addr_wrap	*iftab;
1571
1572
void
1573
ifa_load(void)
1574
{
1575
	struct ifaddrs		*ifap, *ifa;
1576
	struct ipsec_addr_wrap	*n = NULL, *h = NULL;
1577
1578
	if (getifaddrs(&ifap) < 0)
1579
		err(1, "ifa_load: getifaddrs");
1580
1581
	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1582
		if (!(ifa->ifa_addr->sa_family == AF_INET ||
1583
		    ifa->ifa_addr->sa_family == AF_INET6 ||
1584
		    ifa->ifa_addr->sa_family == AF_LINK))
1585
			continue;
1586
		n = calloc(1, sizeof(struct ipsec_addr_wrap));
1587
		if (n == NULL)
1588
			err(1, "ifa_load: calloc");
1589
		n->af = ifa->ifa_addr->sa_family;
1590
		if ((n->name = strdup(ifa->ifa_name)) == NULL)
1591
			err(1, "ifa_load: strdup");
1592
		if (n->af == AF_INET) {
1593
			n->af = AF_INET;
1594
			memcpy(&n->address.v4, &((struct sockaddr_in *)
1595
			    ifa->ifa_addr)->sin_addr,
1596
			    sizeof(struct in_addr));
1597
			memcpy(&n->mask.v4, &((struct sockaddr_in *)
1598
			    ifa->ifa_netmask)->sin_addr,
1599
			    sizeof(struct in_addr));
1600
		} else if (n->af == AF_INET6) {
1601
			n->af = AF_INET6;
1602
			memcpy(&n->address.v6, &((struct sockaddr_in6 *)
1603
			    ifa->ifa_addr)->sin6_addr,
1604
			    sizeof(struct in6_addr));
1605
			memcpy(&n->mask.v6, &((struct sockaddr_in6 *)
1606
			    ifa->ifa_netmask)->sin6_addr,
1607
			    sizeof(struct in6_addr));
1608
		}
1609
		n->next = NULL;
1610
		n->tail = n;
1611
		if (h == NULL)
1612
			h = n;
1613
		else {
1614
			h->tail->next = n;
1615
			h->tail = n;
1616
		}
1617
	}
1618
1619
	iftab = h;
1620
	freeifaddrs(ifap);
1621
}
1622
1623
int
1624
ifa_exists(const char *ifa_name)
1625
{
1626
	struct ipsec_addr_wrap	*n;
1627
	struct ifgroupreq	 ifgr;
1628
	int			 s;
1629
1630
	if (iftab == NULL)
1631
		ifa_load();
1632
1633
	/* check whether this is a group */
1634
	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1635
		err(1, "ifa_exists: socket");
1636
	bzero(&ifgr, sizeof(ifgr));
1637
	strlcpy(ifgr.ifgr_name, ifa_name, sizeof(ifgr.ifgr_name));
1638
	if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == 0) {
1639
		close(s);
1640
		return (1);
1641
	}
1642
	close(s);
1643
1644
	for (n = iftab; n; n = n->next) {
1645
		if (n->af == AF_LINK && !strncmp(n->name, ifa_name,
1646
		    IFNAMSIZ))
1647
			return (1);
1648
	}
1649
1650
	return (0);
1651
}
1652
1653
struct ipsec_addr_wrap *
1654
ifa_grouplookup(const char *ifa_name)
1655
{
1656
	struct ifg_req		*ifg;
1657
	struct ifgroupreq	 ifgr;
1658
	int			 s;
1659
	size_t			 len;
1660
	struct ipsec_addr_wrap	*n, *h = NULL, *hn;
1661
1662
	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1663
		err(1, "socket");
1664
	bzero(&ifgr, sizeof(ifgr));
1665
	strlcpy(ifgr.ifgr_name, ifa_name, sizeof(ifgr.ifgr_name));
1666
	if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
1667
		close(s);
1668
		return (NULL);
1669
	}
1670
1671
	len = ifgr.ifgr_len;
1672
	if ((ifgr.ifgr_groups = calloc(1, len)) == NULL)
1673
		err(1, "calloc");
1674
	if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1)
1675
		err(1, "ioctl");
1676
1677
	for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req);
1678
	    ifg++) {
1679
		len -= sizeof(struct ifg_req);
1680
		if ((n = ifa_lookup(ifg->ifgrq_member)) == NULL)
1681
			continue;
1682
		if (h == NULL)
1683
			h = n;
1684
		else {
1685
			for (hn = h; hn->next != NULL; hn = hn->next)
1686
				;	/* nothing */
1687
			hn->next = n;
1688
			n->tail = hn;
1689
		}
1690
	}
1691
	free(ifgr.ifgr_groups);
1692
	close(s);
1693
1694
	return (h);
1695
}
1696
1697
struct ipsec_addr_wrap *
1698
ifa_lookup(const char *ifa_name)
1699
{
1700
	struct ipsec_addr_wrap	*p = NULL, *h = NULL, *n = NULL;
1701
1702
	if (iftab == NULL)
1703
		ifa_load();
1704
1705
	if ((n = ifa_grouplookup(ifa_name)) != NULL)
1706
		return (n);
1707
1708
	for (p = iftab; p; p = p->next) {
1709
		if (p->af != AF_INET && p->af != AF_INET6)
1710
			continue;
1711
		if (strncmp(p->name, ifa_name, IFNAMSIZ))
1712
			continue;
1713
		n = calloc(1, sizeof(struct ipsec_addr_wrap));
1714
		if (n == NULL)
1715
			err(1, "ifa_lookup: calloc");
1716
		memcpy(n, p, sizeof(struct ipsec_addr_wrap));
1717
		if ((n->name = strdup(p->name)) == NULL)
1718
			err(1, "ifa_lookup: strdup");
1719
		switch (n->af) {
1720
		case AF_INET:
1721
			set_ipmask(n, 32);
1722
			break;
1723
		case AF_INET6:
1724
			/* route/show.c and bgpd/util.c give KAME credit */
1725
			if (IN6_IS_ADDR_LINKLOCAL(&n->address.v6)) {
1726
				u_int16_t tmp16;
1727
				/* for now we can not handle link local,
1728
				 * therefore bail for now
1729
				 */
1730
				free(n);
1731
				continue;
1732
1733
				memcpy(&tmp16, &n->address.v6.s6_addr[2],
1734
				    sizeof(tmp16));
1735
				/* use this when we support link-local
1736
				 * n->??.scopeid = ntohs(tmp16);
1737
				 */
1738
				n->address.v6.s6_addr[2] = 0;
1739
				n->address.v6.s6_addr[3] = 0;
1740
			}
1741
			set_ipmask(n, 128);
1742
			break;
1743
		}
1744
1745
		n->next = NULL;
1746
		n->tail = n;
1747
		if (h == NULL)
1748
			h = n;
1749
		else {
1750
			h->tail->next = n;
1751
			h->tail = n;
1752
		}
1753
	}
1754
1755
	return (h);
1756
}
1757
1758
void
1759
set_ipmask(struct ipsec_addr_wrap *address, u_int8_t b)
1760
{
1761
	struct ipsec_addr	*ipa;
1762
	int			 i, j = 0;
1763
1764
	ipa = &address->mask;
1765
	bzero(ipa, sizeof(struct ipsec_addr));
1766
1767
	while (b >= 32) {
1768
		ipa->addr32[j++] = 0xffffffff;
1769
		b -= 32;
1770
	}
1771
	for (i = 31; i > 31 - b; --i)
1772
		ipa->addr32[j] |= (1 << i);
1773
	if (b)
1774
		ipa->addr32[j] = htonl(ipa->addr32[j]);
1775
}
1776
1777
const struct ipsec_xf *
1778
parse_xf(const char *name, const struct ipsec_xf xfs[])
1779
{
1780
	int		i;
1781
1782
	for (i = 0; xfs[i].name != NULL; i++) {
1783
		if (strncmp(name, xfs[i].name, strlen(name)))
1784
			continue;
1785
		return &xfs[i];
1786
	}
1787
	return (NULL);
1788
}
1789
1790
struct ipsec_lifetime *
1791
parse_life(const char *value)
1792
{
1793
	struct ipsec_lifetime	*life;
1794
	int			ret;
1795
	int			seconds = 0;
1796
	char			unit = 0;
1797
1798
	ret = sscanf(value, "%d%c", &seconds, &unit);
1799
	if (ret == 2) {
1800
		switch (tolower((unsigned char)unit)) {
1801
		case 'm':
1802
			seconds *= 60;
1803
			break;
1804
		case 'h':
1805
			seconds *= 60 * 60;
1806
			break;
1807
		default:
1808
			err(1, "invalid time unit");
1809
		}
1810
	} else if (ret != 1)
1811
		err(1, "invalid time specification: %s", value);
1812
1813
	life = calloc(1, sizeof(struct ipsec_lifetime));
1814
	if (life == NULL)
1815
		err(1, "calloc");
1816
1817
	life->lt_seconds = seconds;
1818
	life->lt_bytes = -1;
1819
1820
	return (life);
1821
}
1822
1823
struct ipsec_transforms *
1824
copytransforms(const struct ipsec_transforms *xfs)
1825
{
1826
	struct ipsec_transforms *newxfs;
1827
1828
	if (xfs == NULL)
1829
		return (NULL);
1830
1831
	newxfs = calloc(1, sizeof(struct ipsec_transforms));
1832
	if (newxfs == NULL)
1833
		err(1, "copytransforms: calloc");
1834
1835
	memcpy(newxfs, xfs, sizeof(struct ipsec_transforms));
1836
	return (newxfs);
1837
}
1838
1839
struct ipsec_lifetime *
1840
copylife(const struct ipsec_lifetime *life)
1841
{
1842
	struct ipsec_lifetime *newlife;
1843
1844
	if (life == NULL)
1845
		return (NULL);
1846
1847
	newlife = calloc(1, sizeof(struct ipsec_lifetime));
1848
	if (newlife == NULL)
1849
		err(1, "copylife: calloc");
1850
1851
	memcpy(newlife, life, sizeof(struct ipsec_lifetime));
1852
	return (newlife);
1853
}
1854
1855
struct ipsec_auth *
1856
copyipsecauth(const struct ipsec_auth *auth)
1857
{
1858
	struct ipsec_auth	*newauth;
1859
1860
	if (auth == NULL)
1861
		return (NULL);
1862
1863
	if ((newauth = calloc(1, sizeof(struct ipsec_auth))) == NULL)
1864
		err(1, "calloc");
1865
	if (auth->srcid &&
1866
	    asprintf(&newauth->srcid, "%s", auth->srcid) == -1)
1867
		err(1, "asprintf");
1868
	if (auth->dstid &&
1869
	    asprintf(&newauth->dstid, "%s", auth->dstid) == -1)
1870
		err(1, "asprintf");
1871
1872
	newauth->srcid_type = auth->srcid_type;
1873
	newauth->dstid_type = auth->dstid_type;
1874
	newauth->type = auth->type;
1875
1876
	return (newauth);
1877
}
1878
1879
struct ike_auth *
1880
copyikeauth(const struct ike_auth *auth)
1881
{
1882
	struct ike_auth	*newauth;
1883
1884
	if (auth == NULL)
1885
		return (NULL);
1886
1887
	if ((newauth = calloc(1, sizeof(struct ike_auth))) == NULL)
1888
		err(1, "calloc");
1889
	if (auth->string &&
1890
	    asprintf(&newauth->string, "%s", auth->string) == -1)
1891
		err(1, "asprintf");
1892
1893
	newauth->type = auth->type;
1894
1895
	return (newauth);
1896
}
1897
1898
struct ipsec_key *
1899
copykey(struct ipsec_key *key)
1900
{
1901
	struct ipsec_key	*newkey;
1902
1903
	if (key == NULL)
1904
		return (NULL);
1905
1906
	if ((newkey = calloc(1, sizeof(struct ipsec_key))) == NULL)
1907
		err(1, "calloc");
1908
	if ((newkey->data = calloc(key->len, sizeof(u_int8_t))) == NULL)
1909
		err(1, "calloc");
1910
	memcpy(newkey->data, key->data, key->len);
1911
	newkey->len = key->len;
1912
1913
	return (newkey);
1914
}
1915
1916
struct ipsec_addr_wrap *
1917
copyhost(const struct ipsec_addr_wrap *src)
1918
{
1919
	struct ipsec_addr_wrap *dst;
1920
1921
	if (src == NULL)
1922
		return (NULL);
1923
1924
	dst = calloc(1, sizeof(struct ipsec_addr_wrap));
1925
	if (dst == NULL)
1926
		err(1, "copyhost: calloc");
1927
1928
	memcpy(dst, src, sizeof(struct ipsec_addr_wrap));
1929
1930
	if (src->name != NULL && (dst->name = strdup(src->name)) == NULL)
1931
		err(1, "copyhost: strdup");
1932
1933
	return dst;
1934
}
1935
1936
char *
1937
copytag(const char *src)
1938
{
1939
	char *tag;
1940
1941
	if (src == NULL)
1942
		return (NULL);
1943
	if ((tag = strdup(src)) == NULL)
1944
		err(1, "copytag: strdup");
1945
1946
	return (tag);
1947
}
1948
1949
struct ipsec_rule *
1950
copyrule(struct ipsec_rule *rule)
1951
{
1952
	struct ipsec_rule	*r;
1953
1954
	if ((r = calloc(1, sizeof(struct ipsec_rule))) == NULL)
1955
		err(1, "calloc");
1956
1957
	r->src = copyhost(rule->src);
1958
	r->dst = copyhost(rule->dst);
1959
	r->local = copyhost(rule->local);
1960
	r->peer = copyhost(rule->peer);
1961
	r->auth = copyipsecauth(rule->auth);
1962
	r->ikeauth = copyikeauth(rule->ikeauth);
1963
	r->xfs = copytransforms(rule->xfs);
1964
	r->p1xfs = copytransforms(rule->p1xfs);
1965
	r->p2xfs = copytransforms(rule->p2xfs);
1966
	r->p1life = copylife(rule->p1life);
1967
	r->p2life = copylife(rule->p2life);
1968
	r->authkey = copykey(rule->authkey);
1969
	r->enckey = copykey(rule->enckey);
1970
	r->tag = copytag(rule->tag);
1971
1972
	r->p1ie = rule->p1ie;
1973
	r->p2ie = rule->p2ie;
1974
	r->type = rule->type;
1975
	r->satype = rule->satype;
1976
	r->proto = rule->proto;
1977
	r->tmode = rule->tmode;
1978
	r->direction = rule->direction;
1979
	r->flowtype = rule->flowtype;
1980
	r->sport = rule->sport;
1981
	r->dport = rule->dport;
1982
	r->ikemode = rule->ikemode;
1983
	r->spi = rule->spi;
1984
	r->nr = rule->nr;
1985
1986
	return (r);
1987
}
1988
1989
int
1990
validate_af(struct ipsec_addr_wrap *src, struct ipsec_addr_wrap *dst)
1991
{
1992
	struct ipsec_addr_wrap *ta;
1993
	u_int8_t src_v4 = 0;
1994
	u_int8_t dst_v4 = 0;
1995
	u_int8_t src_v6 = 0;
1996
	u_int8_t dst_v6 = 0;
1997
1998
	for (ta = src; ta; ta = ta->next) {
1999
		if (ta->af == AF_INET)
2000
			src_v4 = 1;
2001
		if (ta->af == AF_INET6)
2002
			src_v6 = 1;
2003
		if (ta->af == AF_UNSPEC)
2004
			return 0;
2005
		if (src_v4 && src_v6)
2006
			break;
2007
	}
2008
	for (ta = dst; ta; ta = ta->next) {
2009
		if (ta->af == AF_INET)
2010
			dst_v4 = 1;
2011
		if (ta->af == AF_INET6)
2012
			dst_v6 = 1;
2013
		if (ta->af == AF_UNSPEC)
2014
			return 0;
2015
		if (dst_v4 && dst_v6)
2016
			break;
2017
	}
2018
	if (src_v4 != dst_v4 && src_v6 != dst_v6)
2019
		return (1);
2020
2021
	return (0);
2022
}
2023
2024
2025
int
2026
validate_sa(u_int32_t spi, u_int8_t satype, struct ipsec_transforms *xfs,
2027
    struct ipsec_key *authkey, struct ipsec_key *enckey, u_int8_t tmode)
2028
{
2029
	/* Sanity checks */
2030
	if (spi == 0) {
2031
		yyerror("no SPI specified");
2032
		return (0);
2033
	}
2034
	if (satype == IPSEC_AH) {
2035
		if (!xfs) {
2036
			yyerror("no transforms specified");
2037
			return (0);
2038
		}
2039
		if (!xfs->authxf)
2040
			xfs->authxf = &authxfs[AUTHXF_HMAC_SHA2_256];
2041
		if (xfs->encxf) {
2042
			yyerror("ah does not provide encryption");
2043
			return (0);
2044
		}
2045
		if (xfs->compxf) {
2046
			yyerror("ah does not provide compression");
2047
			return (0);
2048
		}
2049
	}
2050
	if (satype == IPSEC_ESP) {
2051
		if (!xfs) {
2052
			yyerror("no transforms specified");
2053
			return (0);
2054
		}
2055
		if (xfs->compxf) {
2056
			yyerror("esp does not provide compression");
2057
			return (0);
2058
		}
2059
		if (!xfs->encxf)
2060
			xfs->encxf = &encxfs[ENCXF_AES];
2061
		if (xfs->encxf->nostatic) {
2062
			yyerror("%s is disallowed with static keys",
2063
			    xfs->encxf->name);
2064
			return 0;
2065
		}
2066
		if (xfs->encxf->noauth && xfs->authxf) {
2067
			yyerror("authentication is implicit for %s",
2068
			    xfs->encxf->name);
2069
			return (0);
2070
		} else if (!xfs->encxf->noauth && !xfs->authxf)
2071
			xfs->authxf = &authxfs[AUTHXF_HMAC_SHA2_256];
2072
	}
2073
	if (satype == IPSEC_IPCOMP) {
2074
		if (!xfs) {
2075
			yyerror("no transform specified");
2076
			return (0);
2077
		}
2078
		if (xfs->authxf || xfs->encxf) {
2079
			yyerror("no encryption or authentication with ipcomp");
2080
			return (0);
2081
		}
2082
		if (!xfs->compxf)
2083
			xfs->compxf = &compxfs[COMPXF_DEFLATE];
2084
	}
2085
	if (satype == IPSEC_IPIP) {
2086
		if (!xfs) {
2087
			yyerror("no transform specified");
2088
			return (0);
2089
		}
2090
		if (xfs->authxf || xfs->encxf || xfs->compxf) {
2091
			yyerror("no encryption, authentication or compression"
2092
			    " with ipip");
2093
			return (0);
2094
		}
2095
	}
2096
	if (satype == IPSEC_TCPMD5 && authkey == NULL && tmode !=
2097
	    IPSEC_TRANSPORT) {
2098
		yyerror("authentication key needed for tcpmd5");
2099
		return (0);
2100
	}
2101
	if (xfs && xfs->authxf) {
2102
		if (!authkey && xfs->authxf != &authxfs[AUTHXF_NONE]) {
2103
			yyerror("no authentication key specified");
2104
			return (0);
2105
		}
2106
		if (authkey && authkey->len != xfs->authxf->keymin) {
2107
			yyerror("wrong authentication key length, needs to be "
2108
			    "%zu bits", xfs->authxf->keymin * 8);
2109
			return (0);
2110
		}
2111
	}
2112
	if (xfs && xfs->encxf) {
2113
		if (!enckey && xfs->encxf != &encxfs[ENCXF_NULL]) {
2114
			yyerror("no encryption key specified");
2115
			return (0);
2116
		}
2117
		if (enckey) {
2118
			if (enckey->len < xfs->encxf->keymin) {
2119
				yyerror("encryption key too short (%zu bits), "
2120
				    "minimum %zu bits", enckey->len * 8,
2121
				    xfs->encxf->keymin * 8);
2122
				return (0);
2123
			}
2124
			if (xfs->encxf->keymax < enckey->len) {
2125
				yyerror("encryption key too long (%zu bits), "
2126
				    "maximum %zu bits", enckey->len * 8,
2127
				    xfs->encxf->keymax * 8);
2128
				return (0);
2129
			}
2130
		}
2131
	}
2132
2133
	return 1;
2134
}
2135
2136
int
2137
add_sabundle(struct ipsec_rule *r, char *bundle)
2138
{
2139
	struct ipsec_rule	*rp, *last, *sabundle;
2140
	int			 found = 0;
2141
2142
	TAILQ_FOREACH(rp, &ipsec->bundle_queue, bundle_entry) {
2143
		if ((strcmp(rp->src->name, r->src->name) == 0) &&
2144
		    (strcmp(rp->dst->name, r->dst->name) == 0) &&
2145
		    (strcmp(rp->bundle, bundle) == 0)) {
2146
			found = 1;
2147
			break;
2148
		}
2149
	}
2150
	if (found) {
2151
		last = TAILQ_LAST(&rp->dst_bundle_queue, dst_bundle_queue);
2152
		TAILQ_INSERT_TAIL(&rp->dst_bundle_queue, r, dst_bundle_entry);
2153
2154
		sabundle = create_sabundle(last->dst, last->satype, last->spi,
2155
		    r->dst, r->satype, r->spi);
2156
		if (sabundle == NULL)
2157
			return (1);
2158
		sabundle->nr = ipsec->rule_nr++;
2159
		if (ipsecctl_add_rule(ipsec, sabundle))
2160
			return (1);
2161
	} else {
2162
		TAILQ_INSERT_TAIL(&ipsec->bundle_queue, r, bundle_entry);
2163
		TAILQ_INIT(&r->dst_bundle_queue);
2164
		TAILQ_INSERT_TAIL(&r->dst_bundle_queue, r, dst_bundle_entry);
2165
		r->bundle = bundle;
2166
	}
2167
2168
	return (0);
2169
}
2170
2171
struct ipsec_rule *
2172
create_sa(u_int8_t satype, u_int8_t tmode, struct ipsec_hosts *hosts,
2173
    u_int32_t spi, struct ipsec_transforms *xfs, struct ipsec_key *authkey,
2174
    struct ipsec_key *enckey)
2175
{
2176
	struct ipsec_rule *r;
2177
2178
	if (validate_sa(spi, satype, xfs, authkey, enckey, tmode) == 0)
2179
		return (NULL);
2180
2181
	r = calloc(1, sizeof(struct ipsec_rule));
2182
	if (r == NULL)
2183
		err(1, "create_sa: calloc");
2184
2185
	r->type |= RULE_SA;
2186
	r->satype = satype;
2187
	r->tmode = tmode;
2188
	r->src = hosts->src;
2189
	r->dst = hosts->dst;
2190
	r->spi = spi;
2191
	r->xfs = xfs;
2192
	r->authkey = authkey;
2193
	r->enckey = enckey;
2194
2195
	return r;
2196
}
2197
2198
struct ipsec_rule *
2199
reverse_sa(struct ipsec_rule *rule, u_int32_t spi, struct ipsec_key *authkey,
2200
    struct ipsec_key *enckey)
2201
{
2202
	struct ipsec_rule *reverse;
2203
2204
	if (validate_sa(spi, rule->satype, rule->xfs, authkey, enckey,
2205
	    rule->tmode) == 0)
2206
		return (NULL);
2207
2208
	reverse = calloc(1, sizeof(struct ipsec_rule));
2209
	if (reverse == NULL)
2210
		err(1, "reverse_sa: calloc");
2211
2212
	reverse->type |= RULE_SA;
2213
	reverse->satype = rule->satype;
2214
	reverse->tmode = rule->tmode;
2215
	reverse->src = copyhost(rule->dst);
2216
	reverse->dst = copyhost(rule->src);
2217
	reverse->spi = spi;
2218
	reverse->xfs = copytransforms(rule->xfs);
2219
	reverse->authkey = authkey;
2220
	reverse->enckey = enckey;
2221
2222
	return (reverse);
2223
}
2224
2225
struct ipsec_rule *
2226
create_sabundle(struct ipsec_addr_wrap *dst, u_int8_t proto, u_int32_t spi,
2227
    struct ipsec_addr_wrap *dst2, u_int8_t proto2, u_int32_t spi2)
2228
{
2229
	struct ipsec_rule *r;
2230
2231
	r = calloc(1, sizeof(struct ipsec_rule));
2232
	if (r == NULL)
2233
		err(1, "create_sabundle: calloc");
2234
2235
	r->type |= RULE_BUNDLE;
2236
2237
	r->dst = copyhost(dst);
2238
	r->dst2 = copyhost(dst2);
2239
	r->proto = proto;
2240
	r->proto2 = proto2;
2241
	r->spi = spi;
2242
	r->spi2 = spi2;
2243
	r->satype = proto;
2244
2245
	return (r);
2246
}
2247
2248
struct ipsec_rule *
2249
create_flow(u_int8_t dir, u_int8_t proto, struct ipsec_hosts *hosts,
2250
    u_int8_t satype, char *srcid, char *dstid, u_int8_t type)
2251
{
2252
	struct ipsec_rule *r;
2253
2254
	r = calloc(1, sizeof(struct ipsec_rule));
2255
	if (r == NULL)
2256
		err(1, "create_flow: calloc");
2257
2258
	r->type |= RULE_FLOW;
2259
2260
	if (dir == IPSEC_INOUT)
2261
		r->direction = IPSEC_OUT;
2262
	else
2263
		r->direction = dir;
2264
2265
	r->satype = satype;
2266
	r->proto = proto;
2267
	r->src = hosts->src;
2268
	r->sport = hosts->sport;
2269
	r->dst = hosts->dst;
2270
	r->dport = hosts->dport;
2271
	if ((hosts->sport != 0 || hosts->dport != 0) &&
2272
	    (proto != IPPROTO_TCP && proto != IPPROTO_UDP)) {
2273
		yyerror("no protocol supplied with source/destination ports");
2274
		goto errout;
2275
	}
2276
2277
	switch (satype) {
2278
	case IPSEC_IPCOMP:
2279
	case IPSEC_IPIP:
2280
		if (type == TYPE_UNKNOWN)
2281
			type = TYPE_USE;
2282
		break;
2283
	default:
2284
		if (type == TYPE_UNKNOWN)
2285
			type = TYPE_REQUIRE;
2286
		break;
2287
	}
2288
2289
	r->flowtype = type;
2290
	if (type == TYPE_DENY || type == TYPE_BYPASS)
2291
		return (r);
2292
2293
	r->auth = calloc(1, sizeof(struct ipsec_auth));
2294
	if (r->auth == NULL)
2295
		err(1, "create_flow: calloc");
2296
	r->auth->srcid = srcid;
2297
	r->auth->dstid = dstid;
2298
	r->auth->srcid_type = get_id_type(srcid);
2299
	r->auth->dstid_type = get_id_type(dstid);
2300
	return r;
2301
2302
errout:
2303
	free(r);
2304
	if (srcid)
2305
		free(srcid);
2306
	if (dstid)
2307
		free(dstid);
2308
	free(hosts->src);
2309
	hosts->src = NULL;
2310
	free(hosts->dst);
2311
	hosts->dst = NULL;
2312
2313
	return NULL;
2314
}
2315
2316
void
2317
expand_any(struct ipsec_addr_wrap *ipa_in)
2318
{
2319
	struct ipsec_addr_wrap *oldnext, *ipa;
2320
2321
	for (ipa = ipa_in; ipa; ipa = ipa->next) {
2322
		if (ipa->af != AF_UNSPEC)
2323
			continue;
2324
		oldnext = ipa->next;
2325
2326
		ipa->af = AF_INET;
2327
		ipa->netaddress = 1;
2328
		if ((ipa->name = strdup("0.0.0.0/0")) == NULL)
2329
			err(1, "expand_any: strdup");
2330
2331
		ipa->next = calloc(1, sizeof(struct ipsec_addr_wrap));
2332
		if (ipa->next == NULL)
2333
			err(1, "expand_any: calloc");
2334
		ipa->next->af = AF_INET6;
2335
		ipa->next->netaddress = 1;
2336
		if ((ipa->next->name = strdup("::/0")) == NULL)
2337
			err(1, "expand_any: strdup");
2338
2339
		ipa->next->next = oldnext;
2340
	}
2341
}
2342
2343
int
2344
set_rule_peers(struct ipsec_rule *r, struct ipsec_hosts *peers)
2345
{
2346
	if (r->type == RULE_FLOW &&
2347
	    (r->flowtype == TYPE_DENY || r->flowtype == TYPE_BYPASS))
2348
		return (0);
2349
2350
	r->local = copyhost(peers->src);
2351
	r->peer = copyhost(peers->dst);
2352
	if (r->peer == NULL) {
2353
		/* Set peer to remote host.  Must be a host address. */
2354
		if (r->direction == IPSEC_IN) {
2355
			if (!r->src->netaddress)
2356
				r->peer = copyhost(r->src);
2357
		} else {
2358
			if (!r->dst->netaddress)
2359
				r->peer = copyhost(r->dst);
2360
		}
2361
	}
2362
	if (r->type == RULE_FLOW && r->peer == NULL) {
2363
		yyerror("no peer specified for destination %s",
2364
		    r->dst->name);
2365
		return (1);
2366
	}
2367
	if (r->peer != NULL && r->peer->af == AF_UNSPEC) {
2368
		/* If peer has been specified as any, use the default peer. */
2369
		free(r->peer);
2370
		r->peer = NULL;
2371
	}
2372
	if (r->type == RULE_IKE && r->peer == NULL) {
2373
		/*
2374
                 * Check if the default peer is consistent for all
2375
                 * rules.  Only warn to avoid breaking existing configs.
2376
		 */
2377
		static struct ipsec_rule *pdr = NULL;
2378
2379
		if (pdr == NULL) {
2380
			/* Remember first default peer rule for comparison. */
2381
			pdr = r;
2382
		} else {
2383
			/* The new default peer must create the same config. */
2384
			if ((pdr->local == NULL && r->local != NULL) ||
2385
			    (pdr->local != NULL && r->local == NULL) ||
2386
			    (pdr->local != NULL && r->local != NULL &&
2387
			    strcmp(pdr->local->name, r->local->name)))
2388
				yywarn("default peer local mismatch");
2389
			if (pdr->ikeauth->type != r->ikeauth->type)
2390
				yywarn("default peer phase 1 auth mismatch");
2391
			if (pdr->ikeauth->type == IKE_AUTH_PSK &&
2392
			    r->ikeauth->type == IKE_AUTH_PSK &&
2393
			    strcmp(pdr->ikeauth->string, r->ikeauth->string))
2394
				yywarn("default peer psk mismatch");
2395
			if (pdr->p1ie != r->p1ie)
2396
				yywarn("default peer phase 1 mode mismatch");
2397
			/*
2398
			 * Transforms have ADD insted of SET so they may be
2399
			 * different and are not checked here.
2400
			 */
2401
			if ((pdr->auth->srcid == NULL &&
2402
			    r->auth->srcid != NULL) ||
2403
			    (pdr->auth->srcid != NULL &&
2404
			    r->auth->srcid == NULL) ||
2405
			    (pdr->auth->srcid != NULL &&
2406
			    r->auth->srcid != NULL &&
2407
			    strcmp(pdr->auth->srcid, r->auth->srcid)))
2408
				yywarn("default peer srcid mismatch");
2409
			if ((pdr->auth->dstid == NULL &&
2410
			    r->auth->dstid != NULL) ||
2411
			    (pdr->auth->dstid != NULL &&
2412
			    r->auth->dstid == NULL) ||
2413
			    (pdr->auth->dstid != NULL &&
2414
			    r->auth->dstid != NULL &&
2415
			    strcmp(pdr->auth->dstid, r->auth->dstid)))
2416
				yywarn("default peer dstid mismatch");
2417
		}
2418
	}
2419
	return (0);
2420
}
2421
2422
int
2423
expand_rule(struct ipsec_rule *rule, struct ipsec_hosts *peers,
2424
    u_int8_t direction, u_int32_t spi, struct ipsec_key *authkey,
2425
    struct ipsec_key *enckey, char *bundle)
2426
{
2427
	struct ipsec_rule	*r, *revr;
2428
	struct ipsec_addr_wrap	*src, *dst;
2429
	int added = 0, ret = 1;
2430
2431
	if (validate_af(rule->src, rule->dst)) {
2432
		yyerror("source/destination address families do not match");
2433
		goto errout;
2434
	}
2435
	expand_any(rule->src);
2436
	expand_any(rule->dst);
2437
	for (src = rule->src; src; src = src->next) {
2438
		for (dst = rule->dst; dst; dst = dst->next) {
2439
			if (src->af != dst->af)
2440
				continue;
2441
			r = copyrule(rule);
2442
2443
			r->src = copyhost(src);
2444
			r->dst = copyhost(dst);
2445
2446
			if (peers && set_rule_peers(r, peers)) {
2447
				ipsecctl_free_rule(r);
2448
				goto errout;
2449
			}
2450
2451
			r->nr = ipsec->rule_nr++;
2452
			if (ipsecctl_add_rule(ipsec, r))
2453
				goto out;
2454
			if (bundle && add_sabundle(r, bundle))
2455
				goto out;
2456
2457
			if (direction == IPSEC_INOUT) {
2458
				/* Create and add reverse flow rule. */
2459
				revr = reverse_rule(r);
2460
				if (revr == NULL)
2461
					goto out;
2462
2463
				revr->nr = ipsec->rule_nr++;
2464
				if (ipsecctl_add_rule(ipsec, revr))
2465
					goto out;
2466
				if (bundle && add_sabundle(revr, bundle))
2467
					goto out;
2468
			} else if (spi != 0 || authkey || enckey) {
2469
				/* Create and add reverse sa rule. */
2470
				revr = reverse_sa(r, spi, authkey, enckey);
2471
				if (revr == NULL)
2472
					goto out;
2473
2474
				revr->nr = ipsec->rule_nr++;
2475
				if (ipsecctl_add_rule(ipsec, revr))
2476
					goto out;
2477
				if (bundle && add_sabundle(revr, bundle))
2478
					goto out;
2479
			}
2480
			added++;
2481
		}
2482
	}
2483
	if (!added)
2484
		yyerror("rule expands to no valid combination");
2485
 errout:
2486
	ret = 0;
2487
	ipsecctl_free_rule(rule);
2488
 out:
2489
	if (peers) {
2490
		if (peers->src)
2491
			free(peers->src);
2492
		if (peers->dst)
2493
			free(peers->dst);
2494
	}
2495
	return (ret);
2496
}
2497
2498
struct ipsec_rule *
2499
reverse_rule(struct ipsec_rule *rule)
2500
{
2501
	struct ipsec_rule *reverse;
2502
2503
	reverse = calloc(1, sizeof(struct ipsec_rule));
2504
	if (reverse == NULL)
2505
		err(1, "reverse_rule: calloc");
2506
2507
	reverse->type |= RULE_FLOW;
2508
2509
	/* Reverse direction */
2510
	if (rule->direction == (u_int8_t)IPSEC_OUT)
2511
		reverse->direction = (u_int8_t)IPSEC_IN;
2512
	else
2513
		reverse->direction = (u_int8_t)IPSEC_OUT;
2514
2515
	reverse->flowtype = rule->flowtype;
2516
	reverse->src = copyhost(rule->dst);
2517
	reverse->dst = copyhost(rule->src);
2518
	reverse->sport = rule->dport;
2519
	reverse->dport = rule->sport;
2520
	if (rule->local)
2521
		reverse->local = copyhost(rule->local);
2522
	if (rule->peer)
2523
		reverse->peer = copyhost(rule->peer);
2524
	reverse->satype = rule->satype;
2525
	reverse->proto = rule->proto;
2526
2527
	if (rule->auth) {
2528
		reverse->auth = calloc(1, sizeof(struct ipsec_auth));
2529
		if (reverse->auth == NULL)
2530
			err(1, "reverse_rule: calloc");
2531
		if (rule->auth->dstid && (reverse->auth->dstid =
2532
		    strdup(rule->auth->dstid)) == NULL)
2533
			err(1, "reverse_rule: strdup");
2534
		if (rule->auth->srcid && (reverse->auth->srcid =
2535
		    strdup(rule->auth->srcid)) == NULL)
2536
			err(1, "reverse_rule: strdup");
2537
		reverse->auth->srcid_type = rule->auth->srcid_type;
2538
		reverse->auth->dstid_type = rule->auth->dstid_type;
2539
		reverse->auth->type = rule->auth->type;
2540
	}
2541
2542
	return reverse;
2543
}
2544
2545
struct ipsec_rule *
2546
create_ike(u_int8_t proto, struct ipsec_hosts *hosts,
2547
    struct ike_mode *phase1mode, struct ike_mode *phase2mode, u_int8_t satype,
2548
    u_int8_t tmode, u_int8_t mode, char *srcid, char *dstid,
2549
    struct ike_auth *authtype, char *tag)
2550
{
2551
	struct ipsec_rule *r;
2552
2553
	r = calloc(1, sizeof(struct ipsec_rule));
2554
	if (r == NULL)
2555
		err(1, "create_ike: calloc");
2556
2557
	r->type = RULE_IKE;
2558
2559
	r->proto = proto;
2560
	r->src = hosts->src;
2561
	r->sport = hosts->sport;
2562
	r->dst = hosts->dst;
2563
	r->dport = hosts->dport;
2564
	if ((hosts->sport != 0 || hosts->dport != 0) &&
2565
	    (proto != IPPROTO_TCP && proto != IPPROTO_UDP)) {
2566
		yyerror("no protocol supplied with source/destination ports");
2567
		goto errout;
2568
	}
2569
2570
	r->satype = satype;
2571
	r->tmode = tmode;
2572
	r->ikemode = mode;
2573
	if (phase1mode) {
2574
		r->p1xfs = phase1mode->xfs;
2575
		r->p1life = phase1mode->life;
2576
		r->p1ie = phase1mode->ike_exch;
2577
	} else {
2578
		r->p1ie = IKE_MM;
2579
	}
2580
	if (phase2mode) {
2581
		if (phase2mode->xfs && phase2mode->xfs->encxf &&
2582
		    phase2mode->xfs->encxf->noauth &&
2583
		    phase2mode->xfs->authxf) {
2584
			yyerror("authentication is implicit for %s",
2585
			    phase2mode->xfs->encxf->name);
2586
			goto errout;
2587
		}
2588
		r->p2xfs = phase2mode->xfs;
2589
		r->p2life = phase2mode->life;
2590
		r->p2ie = phase2mode->ike_exch;
2591
	} else {
2592
		r->p2ie = IKE_QM;
2593
	}
2594
2595
	r->auth = calloc(1, sizeof(struct ipsec_auth));
2596
	if (r->auth == NULL)
2597
		err(1, "create_ike: calloc");
2598
	r->auth->srcid = srcid;
2599
	r->auth->dstid = dstid;
2600
	r->auth->srcid_type = get_id_type(srcid);
2601
	r->auth->dstid_type = get_id_type(dstid);
2602
	r->ikeauth = calloc(1, sizeof(struct ike_auth));
2603
	if (r->ikeauth == NULL)
2604
		err(1, "create_ike: calloc");
2605
	r->ikeauth->type = authtype->type;
2606
	r->ikeauth->string = authtype->string;
2607
	r->tag = tag;
2608
2609
	return (r);
2610
2611
errout:
2612
	free(r);
2613
	free(hosts->src);
2614
	hosts->src = NULL;
2615
	free(hosts->dst);
2616
	hosts->dst = NULL;
2617
	if (phase1mode) {
2618
		free(phase1mode->xfs);
2619
		phase1mode->xfs = NULL;
2620
		free(phase1mode->life);
2621
		phase1mode->life = NULL;
2622
	}
2623
	if (phase2mode) {
2624
		free(phase2mode->xfs);
2625
		phase2mode->xfs = NULL;
2626
		free(phase2mode->life);
2627
		phase2mode->life = NULL;
2628
	}
2629
	if (srcid)
2630
		free(srcid);
2631
	if (dstid)
2632
		free(dstid);
2633
	return NULL;
2634
}
2635
#line 2628 "parse.c"
2636
/* allocate initial stack or double stack size, up to YYMAXDEPTH */
2637
static int yygrowstack(void)
2638
{
2639
    unsigned int newsize;
2640
    long sslen;
2641
    short *newss;
2642
    YYSTYPE *newvs;
2643
2644
    if ((newsize = yystacksize) == 0)
2645
        newsize = YYINITSTACKSIZE;
2646
    else if (newsize >= YYMAXDEPTH)
2647
        return -1;
2648
    else if ((newsize *= 2) > YYMAXDEPTH)
2649
        newsize = YYMAXDEPTH;
2650
    sslen = yyssp - yyss;
2651
#ifdef SIZE_MAX
2652
#define YY_SIZE_MAX SIZE_MAX
2653
#else
2654
#define YY_SIZE_MAX 0xffffffffU
2655
#endif
2656
    if (newsize && YY_SIZE_MAX / newsize < sizeof *newss)
2657
        goto bail;
2658
    newss = yyss ? (short *)realloc(yyss, newsize * sizeof *newss) :
2659
      (short *)malloc(newsize * sizeof *newss); /* overflow check above */
2660
    if (newss == NULL)
2661
        goto bail;
2662
    yyss = newss;
2663
    yyssp = newss + sslen;
2664
    if (newsize && YY_SIZE_MAX / newsize < sizeof *newvs)
2665
        goto bail;
2666
    newvs = yyvs ? (YYSTYPE *)realloc(yyvs, newsize * sizeof *newvs) :
2667
      (YYSTYPE *)malloc(newsize * sizeof *newvs); /* overflow check above */
2668
    if (newvs == NULL)
2669
        goto bail;
2670
    yyvs = newvs;
2671
    yyvsp = newvs + sslen;
2672
    yystacksize = newsize;
2673
    yysslim = yyss + newsize - 1;
2674
    return 0;
2675
bail:
2676
    if (yyss)
2677
            free(yyss);
2678
    if (yyvs)
2679
            free(yyvs);
2680
    yyss = yyssp = NULL;
2681
    yyvs = yyvsp = NULL;
2682
    yystacksize = 0;
2683
    return -1;
2684
}
2685
2686
#define YYABORT goto yyabort
2687
#define YYREJECT goto yyabort
2688
#define YYACCEPT goto yyaccept
2689
#define YYERROR goto yyerrlab
2690
int
2691
yyparse(void)
2692
{
2693
    int yym, yyn, yystate;
2694
#if YYDEBUG
2695
    const char *yys;
2696
2697
    if ((yys = getenv("YYDEBUG")))
2698
    {
2699
        yyn = *yys;
2700
        if (yyn >= '0' && yyn <= '9')
2701
            yydebug = yyn - '0';
2702
    }
2703
#endif /* YYDEBUG */
2704
2705
    yynerrs = 0;
2706
    yyerrflag = 0;
2707
    yychar = (-1);
2708
2709
    if (yyss == NULL && yygrowstack()) goto yyoverflow;
2710
    yyssp = yyss;
2711
    yyvsp = yyvs;
2712
    *yyssp = yystate = 0;
2713
2714
yyloop:
2715
    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
2716
    if (yychar < 0)
2717
    {
2718
        if ((yychar = yylex()) < 0) yychar = 0;
2719
#if YYDEBUG
2720
        if (yydebug)
2721
        {
2722
            yys = 0;
2723
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
2724
            if (!yys) yys = "illegal-symbol";
2725
            printf("%sdebug: state %d, reading %d (%s)\n",
2726
                    YYPREFIX, yystate, yychar, yys);
2727
        }
2728
#endif
2729
    }
2730
    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
2731
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
2732
    {
2733
#if YYDEBUG
2734
        if (yydebug)
2735
            printf("%sdebug: state %d, shifting to state %d\n",
2736
                    YYPREFIX, yystate, yytable[yyn]);
2737
#endif
2738
        if (yyssp >= yysslim && yygrowstack())
2739
        {
2740
            goto yyoverflow;
2741
        }
2742
        *++yyssp = yystate = yytable[yyn];
2743
        *++yyvsp = yylval;
2744
        yychar = (-1);
2745
        if (yyerrflag > 0)  --yyerrflag;
2746
        goto yyloop;
2747
    }
2748
    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
2749
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
2750
    {
2751
        yyn = yytable[yyn];
2752
        goto yyreduce;
2753
    }
2754
    if (yyerrflag) goto yyinrecovery;
2755
#if defined(__GNUC__)
2756
    goto yynewerror;
2757
#endif
2758
yynewerror:
2759
    yyerror("syntax error");
2760
#if defined(__GNUC__)
2761
    goto yyerrlab;
2762
#endif
2763
yyerrlab:
2764
    ++yynerrs;
2765
yyinrecovery:
2766
    if (yyerrflag < 3)
2767
    {
2768
        yyerrflag = 3;
2769
        for (;;)
2770
        {
2771
            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&
2772
                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
2773
            {
2774
#if YYDEBUG
2775
                if (yydebug)
2776
                    printf("%sdebug: state %d, error recovery shifting\
2777
 to state %d\n", YYPREFIX, *yyssp, yytable[yyn]);
2778
#endif
2779
                if (yyssp >= yysslim && yygrowstack())
2780
                {
2781
                    goto yyoverflow;
2782
                }
2783
                *++yyssp = yystate = yytable[yyn];
2784
                *++yyvsp = yylval;
2785
                goto yyloop;
2786
            }
2787
            else
2788
            {
2789
#if YYDEBUG
2790
                if (yydebug)
2791
                    printf("%sdebug: error recovery discarding state %d\n",
2792
                            YYPREFIX, *yyssp);
2793
#endif
2794
                if (yyssp <= yyss) goto yyabort;
2795
                --yyssp;
2796
                --yyvsp;
2797
            }
2798
        }
2799
    }
2800
    else
2801
    {
2802
        if (yychar == 0) goto yyabort;
2803
#if YYDEBUG
2804
        if (yydebug)
2805
        {
2806
            yys = 0;
2807
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
2808
            if (!yys) yys = "illegal-symbol";
2809
            printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
2810
                    YYPREFIX, yystate, yychar, yys);
2811
        }
2812
#endif
2813
        yychar = (-1);
2814
        goto yyloop;
2815
    }
2816
yyreduce:
2817
#if YYDEBUG
2818
    if (yydebug)
2819
        printf("%sdebug: state %d, reducing by rule %d (%s)\n",
2820
                YYPREFIX, yystate, yyn, yyrule[yyn]);
2821
#endif
2822
    yym = yylen[yyn];
2823
    if (yym)
2824
        yyval = yyvsp[1-yym];
2825
    else
2826
        memset(&yyval, 0, sizeof yyval);
2827
    switch (yyn)
2828
    {
2829
case 9:
2830
#line 324 "parse.y"
2831
{ file->errors++; }
2832
break;
2833
case 12:
2834
#line 331 "parse.y"
2835
{
2836
			struct file	*nfile;
2837
2838
			if ((nfile = pushfile(yyvsp[0].v.string, 0)) == NULL) {
2839
				yyerror("failed to include file %s", yyvsp[0].v.string);
2840
				free(yyvsp[0].v.string);
2841
				YYERROR;
2842
			}
2843
			free(yyvsp[0].v.string);
2844
2845
			file = nfile;
2846
			lungetc('\n');
2847
		}
2848
break;
2849
case 13:
2850
#line 346 "parse.y"
2851
{
2852
			struct ipsec_rule	*r;
2853
2854
			r = create_sa(IPSEC_TCPMD5, IPSEC_TRANSPORT, &yyvsp[-2].v.hosts,
2855
			    yyvsp[-1].v.spis.spiout, NULL, yyvsp[0].v.authkeys.keyout, NULL);
2856
			if (r == NULL)
2857
				YYERROR;
2858
2859
			if (expand_rule(r, NULL, 0, yyvsp[-1].v.spis.spiin, yyvsp[0].v.authkeys.keyin, NULL,
2860
			    NULL))
2861
				errx(1, "tcpmd5rule: expand_rule");
2862
		}
2863
break;
2864
case 14:
2865
#line 361 "parse.y"
2866
{
2867
			struct ipsec_rule	*r;
2868
2869
			r = create_sa(yyvsp[-7].v.satype, yyvsp[-6].v.tmode, &yyvsp[-5].v.hosts, yyvsp[-4].v.spis.spiout, yyvsp[-3].v.transforms, yyvsp[-2].v.authkeys.keyout,
2870
			    yyvsp[-1].v.enckeys.keyout);
2871
			if (r == NULL)
2872
				YYERROR;
2873
2874
			if (expand_rule(r, NULL, 0, yyvsp[-4].v.spis.spiin, yyvsp[-2].v.authkeys.keyin,
2875
			    yyvsp[-1].v.enckeys.keyin, yyvsp[0].v.string))
2876
				errx(1, "sarule: expand_rule");
2877
		}
2878
break;
2879
case 15:
2880
#line 375 "parse.y"
2881
{
2882
			struct ipsec_rule	*r;
2883
2884
			r = create_flow(yyvsp[-5].v.dir, yyvsp[-4].v.proto, &yyvsp[-3].v.hosts, yyvsp[-6].v.satype, yyvsp[-1].v.ids.srcid,
2885
			    yyvsp[-1].v.ids.dstid, yyvsp[0].v.type);
2886
			if (r == NULL)
2887
				YYERROR;
2888
2889
			if (expand_rule(r, &yyvsp[-2].v.peers, yyvsp[-5].v.dir, 0, NULL, NULL, NULL))
2890
				errx(1, "flowrule: expand_rule");
2891
		}
2892
break;
2893
case 16:
2894
#line 389 "parse.y"
2895
{
2896
			struct ipsec_rule	*r;
2897
2898
			r = create_ike(yyvsp[-7].v.proto, &yyvsp[-6].v.hosts, yyvsp[-4].v.mode, yyvsp[-3].v.mode, yyvsp[-9].v.satype, yyvsp[-8].v.tmode, yyvsp[-10].v.ikemode,
2899
			    yyvsp[-2].v.ids.srcid, yyvsp[-2].v.ids.dstid, &yyvsp[-1].v.ikeauth, yyvsp[0].v.string);
2900
			if (r == NULL)
2901
				YYERROR;
2902
2903
			if (expand_rule(r, &yyvsp[-5].v.peers, 0, 0, NULL, NULL, NULL))
2904
				errx(1, "ikerule: expand_rule");
2905
		}
2906
break;
2907
case 17:
2908
#line 402 "parse.y"
2909
{ yyval.v.satype = IPSEC_ESP; }
2910
break;
2911
case 18:
2912
#line 403 "parse.y"
2913
{ yyval.v.satype = IPSEC_ESP; }
2914
break;
2915
case 19:
2916
#line 404 "parse.y"
2917
{ yyval.v.satype = IPSEC_AH; }
2918
break;
2919
case 20:
2920
#line 405 "parse.y"
2921
{ yyval.v.satype = IPSEC_IPCOMP; }
2922
break;
2923
case 21:
2924
#line 406 "parse.y"
2925
{ yyval.v.satype = IPSEC_IPIP; }
2926
break;
2927
case 22:
2928
#line 409 "parse.y"
2929
{ yyval.v.proto = 0; }
2930
break;
2931
case 23:
2932
#line 410 "parse.y"
2933
{ yyval.v.proto = yyvsp[0].v.number; }
2934
break;
2935
case 24:
2936
#line 411 "parse.y"
2937
{ yyval.v.proto = IPPROTO_ESP; }
2938
break;
2939
case 25:
2940
#line 412 "parse.y"
2941
{ yyval.v.proto = IPPROTO_AH; }
2942
break;
2943
case 26:
2944
#line 415 "parse.y"
2945
{
2946
			struct protoent *p;
2947
2948
			p = getprotobyname(yyvsp[0].v.string);
2949
			if (p == NULL) {
2950
				yyerror("unknown protocol: %s", yyvsp[0].v.string);
2951
				YYERROR;
2952
			}
2953
			yyval.v.number = p->p_proto;
2954
			free(yyvsp[0].v.string);
2955
		}
2956
break;
2957
case 27:
2958
#line 426 "parse.y"
2959
{
2960
			if (yyvsp[0].v.number > 255 || yyvsp[0].v.number < 0) {
2961
				yyerror("protocol outside range");
2962
				YYERROR;
2963
			}
2964
		}
2965
break;
2966
case 28:
2967
#line 434 "parse.y"
2968
{ yyval.v.tmode = IPSEC_TUNNEL; }
2969
break;
2970
case 29:
2971
#line 435 "parse.y"
2972
{ yyval.v.tmode = IPSEC_TUNNEL; }
2973
break;
2974
case 30:
2975
#line 436 "parse.y"
2976
{ yyval.v.tmode = IPSEC_TRANSPORT; }
2977
break;
2978
case 31:
2979
#line 439 "parse.y"
2980
{ yyval.v.dir = IPSEC_INOUT; }
2981
break;
2982
case 32:
2983
#line 440 "parse.y"
2984
{ yyval.v.dir = IPSEC_IN; }
2985
break;
2986
case 33:
2987
#line 441 "parse.y"
2988
{ yyval.v.dir = IPSEC_OUT; }
2989
break;
2990
case 34:
2991
#line 444 "parse.y"
2992
{
2993
			struct ipsec_addr_wrap *ipa;
2994
			for (ipa = yyvsp[-1].v.host; ipa; ipa = ipa->next) {
2995
				if (ipa->srcnat) {
2996
					yyerror("no flow NAT support for"
2997
					    " destination network: %s", ipa->name);
2998
					YYERROR;
2999
				}
3000
			}
3001
			yyval.v.hosts.src = yyvsp[-4].v.host;
3002
			yyval.v.hosts.sport = yyvsp[-3].v.port;
3003
			yyval.v.hosts.dst = yyvsp[-1].v.host;
3004
			yyval.v.hosts.dport = yyvsp[0].v.port;
3005
		}
3006
break;
3007
case 35:
3008
#line 458 "parse.y"
3009
{
3010
			struct ipsec_addr_wrap *ipa;
3011
			for (ipa = yyvsp[-4].v.host; ipa; ipa = ipa->next) {
3012
				if (ipa->srcnat) {
3013
					yyerror("no flow NAT support for"
3014
					    " destination network: %s", ipa->name);
3015
					YYERROR;
3016
				}
3017
			}
3018
			yyval.v.hosts.src = yyvsp[-1].v.host;
3019
			yyval.v.hosts.sport = yyvsp[0].v.port;
3020
			yyval.v.hosts.dst = yyvsp[-4].v.host;
3021
			yyval.v.hosts.dport = yyvsp[-3].v.port;
3022
		}
3023
break;
3024
case 36:
3025
#line 474 "parse.y"
3026
{ yyval.v.port = 0; }
3027
break;
3028
case 37:
3029
#line 475 "parse.y"
3030
{ yyval.v.port = yyvsp[0].v.number; }
3031
break;
3032
case 38:
3033
#line 478 "parse.y"
3034
{
3035
			struct servent *s;
3036
3037
			if ((s = getservbyname(yyvsp[0].v.string, "tcp")) != NULL ||
3038
			    (s = getservbyname(yyvsp[0].v.string, "udp")) != NULL) {
3039
				yyval.v.number = s->s_port;
3040
			} else {
3041
				yyerror("unknown port: %s", yyvsp[0].v.string);
3042
				YYERROR;
3043
			}
3044
		}
3045
break;
3046
case 39:
3047
#line 489 "parse.y"
3048
{
3049
			if (yyvsp[0].v.number > USHRT_MAX || yyvsp[0].v.number < 0) {
3050
				yyerror("port outside range");
3051
				YYERROR;
3052
			}
3053
			yyval.v.number = htons(yyvsp[0].v.number);
3054
		}
3055
break;
3056
case 40:
3057
#line 498 "parse.y"
3058
{
3059
			yyval.v.peers.dst = NULL;
3060
			yyval.v.peers.src = NULL;
3061
		}
3062
break;
3063
case 41:
3064
#line 502 "parse.y"
3065
{
3066
			yyval.v.peers.dst = yyvsp[-2].v.anyhost;
3067
			yyval.v.peers.src = yyvsp[0].v.singlehost;
3068
		}
3069
break;
3070
case 42:
3071
#line 506 "parse.y"
3072
{
3073
			yyval.v.peers.dst = yyvsp[0].v.anyhost;
3074
			yyval.v.peers.src = yyvsp[-2].v.singlehost;
3075
		}
3076
break;
3077
case 43:
3078
#line 510 "parse.y"
3079
{
3080
			yyval.v.peers.dst = yyvsp[0].v.anyhost;
3081
			yyval.v.peers.src = NULL;
3082
		}
3083
break;
3084
case 44:
3085
#line 514 "parse.y"
3086
{
3087
			yyval.v.peers.dst = NULL;
3088
			yyval.v.peers.src = yyvsp[0].v.singlehost;
3089
		}
3090
break;
3091
case 45:
3092
#line 520 "parse.y"
3093
{ yyval.v.anyhost = yyvsp[0].v.singlehost; }
3094
break;
3095
case 46:
3096
#line 521 "parse.y"
3097
{
3098
			yyval.v.anyhost = host_any();
3099
		}
3100
break;
3101
case 47:
3102
#line 525 "parse.y"
3103
{ yyval.v.singlehost = NULL; }
3104
break;
3105
case 48:
3106
#line 526 "parse.y"
3107
{
3108
			if ((yyval.v.singlehost = host(yyvsp[0].v.string)) == NULL) {
3109
				free(yyvsp[0].v.string);
3110
				yyerror("could not parse host specification");
3111
				YYERROR;
3112
			}
3113
			free(yyvsp[0].v.string);
3114
		}
3115
break;
3116
case 49:
3117
#line 536 "parse.y"
3118
{ yyval.v.host = yyvsp[0].v.host; }
3119
break;
3120
case 50:
3121
#line 537 "parse.y"
3122
{
3123
			if (yyvsp[0].v.host == NULL)
3124
				yyval.v.host = yyvsp[-2].v.host;
3125
			else if (yyvsp[-2].v.host == NULL)
3126
				yyval.v.host = yyvsp[0].v.host;
3127
			else {
3128
				yyvsp[-2].v.host->tail->next = yyvsp[0].v.host;
3129
				yyvsp[-2].v.host->tail = yyvsp[0].v.host->tail;
3130
				yyval.v.host = yyvsp[-2].v.host;
3131
			}
3132
		}
3133
break;
3134
case 51:
3135
#line 550 "parse.y"
3136
{
3137
			if ((yyval.v.host = host(yyvsp[0].v.string)) == NULL) {
3138
				free(yyvsp[0].v.string);
3139
				yyerror("could not parse host specification");
3140
				YYERROR;
3141
			}
3142
			free(yyvsp[0].v.string);
3143
		}
3144
break;
3145
case 52:
3146
#line 558 "parse.y"
3147
{
3148
			char	*buf;
3149
3150
			if (asprintf(&buf, "%s/%lld", yyvsp[-2].v.string, yyvsp[0].v.number) == -1)
3151
				err(1, "host: asprintf");
3152
			free(yyvsp[-2].v.string);
3153
			if ((yyval.v.host = host(buf)) == NULL)	{
3154
				free(buf);
3155
				yyerror("could not parse host specification");
3156
				YYERROR;
3157
			}
3158
			free(buf);
3159
		}
3160
break;
3161
case 53:
3162
#line 573 "parse.y"
3163
{ yyval.v.host = yyvsp[0].v.host; }
3164
break;
3165
case 54:
3166
#line 574 "parse.y"
3167
{
3168
			if (yyvsp[-1].v.host->af != yyvsp[-3].v.host->af) {
3169
				yyerror("Flow NAT address family mismatch");
3170
				YYERROR;
3171
			}
3172
			yyval.v.host = yyvsp[-3].v.host;
3173
			yyval.v.host->srcnat = yyvsp[-1].v.host;
3174
		}
3175
break;
3176
case 55:
3177
#line 582 "parse.y"
3178
{
3179
			yyval.v.host = host_any();
3180
		}
3181
break;
3182
case 56:
3183
#line 585 "parse.y"
3184
{ yyval.v.host = yyvsp[-1].v.host; }
3185
break;
3186
case 57:
3187
#line 588 "parse.y"
3188
{
3189
			yyval.v.ids.srcid = NULL;
3190
			yyval.v.ids.dstid = NULL;
3191
		}
3192
break;
3193
case 58:
3194
#line 592 "parse.y"
3195
{
3196
			yyval.v.ids.srcid = yyvsp[-2].v.id;
3197
			yyval.v.ids.dstid = yyvsp[0].v.id;
3198
		}
3199
break;
3200
case 59:
3201
#line 596 "parse.y"
3202
{
3203
			yyval.v.ids.srcid = yyvsp[0].v.id;
3204
			yyval.v.ids.dstid = NULL;
3205
		}
3206
break;
3207
case 60:
3208
#line 600 "parse.y"
3209
{
3210
			yyval.v.ids.srcid = NULL;
3211
			yyval.v.ids.dstid = yyvsp[0].v.id;
3212
		}
3213
break;
3214
case 61:
3215
#line 606 "parse.y"
3216
{
3217
			yyval.v.type = TYPE_UNKNOWN;
3218
		}
3219
break;
3220
case 62:
3221
#line 609 "parse.y"
3222
{
3223
			yyval.v.type = TYPE_USE;
3224
		}
3225
break;
3226
case 63:
3227
#line 612 "parse.y"
3228
{
3229
			yyval.v.type = TYPE_ACQUIRE;
3230
		}
3231
break;
3232
case 64:
3233
#line 615 "parse.y"
3234
{
3235
			yyval.v.type = TYPE_REQUIRE;
3236
		}
3237
break;
3238
case 65:
3239
#line 618 "parse.y"
3240
{
3241
			yyval.v.type = TYPE_DENY;
3242
		}
3243
break;
3244
case 66:
3245
#line 621 "parse.y"
3246
{
3247
			yyval.v.type = TYPE_BYPASS;
3248
		}
3249
break;
3250
case 67:
3251
#line 624 "parse.y"
3252
{
3253
			yyval.v.type = TYPE_DONTACQ;
3254
		}
3255
break;
3256
case 68:
3257
#line 629 "parse.y"
3258
{ yyval.v.id = yyvsp[0].v.string; }
3259
break;
3260
case 69:
3261
#line 632 "parse.y"
3262
{
3263
			u_int32_t	 spi;
3264
			char		*p = strchr(yyvsp[0].v.string, ':');
3265
3266
			if (p != NULL) {
3267
				*p++ = 0;
3268
3269
				if (atospi(p, &spi) == -1) {
3270
					free(yyvsp[0].v.string);
3271
					YYERROR;
3272
				}
3273
				yyval.v.spis.spiin = spi;
3274
			} else
3275
				yyval.v.spis.spiin = 0;
3276
3277
			if (atospi(yyvsp[0].v.string, &spi) == -1) {
3278
				free(yyvsp[0].v.string);
3279
				YYERROR;
3280
			}
3281
			yyval.v.spis.spiout = spi;
3282
3283
3284
			free(yyvsp[0].v.string);
3285
		}
3286
break;
3287
case 70:
3288
#line 656 "parse.y"
3289
{
3290
			if (yyvsp[0].v.number > UINT_MAX || yyvsp[0].v.number < 0) {
3291
				yyerror("%lld not a valid spi", yyvsp[0].v.number);
3292
				YYERROR;
3293
			}
3294
			if (yyvsp[0].v.number >= SPI_RESERVED_MIN && yyvsp[0].v.number <= SPI_RESERVED_MAX) {
3295
				yyerror("%lld within reserved spi range", yyvsp[0].v.number);
3296
				YYERROR;
3297
			}
3298
3299
			yyval.v.spis.spiin = 0;
3300
			yyval.v.spis.spiout = yyvsp[0].v.number;
3301
		}
3302
break;
3303
case 71:
3304
#line 671 "parse.y"
3305
{
3306
			if ((ipsec_transforms = calloc(1,
3307
			    sizeof(struct ipsec_transforms))) == NULL)
3308
				err(1, "transforms: calloc");
3309
		}
3310
break;
3311
case 72:
3312
#line 677 "parse.y"
3313
{ yyval.v.transforms = ipsec_transforms; }
3314
break;
3315
case 73:
3316
#line 678 "parse.y"
3317
{
3318
			if ((yyval.v.transforms = calloc(1,
3319
			    sizeof(struct ipsec_transforms))) == NULL)
3320
				err(1, "transforms: calloc");
3321
		}
3322
break;
3323
case 76:
3324
#line 689 "parse.y"
3325
{
3326
			if (ipsec_transforms->authxf)
3327
				yyerror("auth already set");
3328
			else {
3329
				ipsec_transforms->authxf = parse_xf(yyvsp[0].v.string,
3330
				    authxfs);
3331
				if (!ipsec_transforms->authxf)
3332
					yyerror("%s not a valid transform", yyvsp[0].v.string);
3333
			}
3334
		}
3335
break;
3336
case 77:
3337
#line 699 "parse.y"
3338
{
3339
			if (ipsec_transforms->encxf)
3340
				yyerror("enc already set");
3341
			else {
3342
				ipsec_transforms->encxf = parse_xf(yyvsp[0].v.string, encxfs);
3343
				if (!ipsec_transforms->encxf)
3344
					yyerror("%s not a valid transform", yyvsp[0].v.string);
3345
			}
3346
		}
3347
break;
3348
case 78:
3349
#line 708 "parse.y"
3350
{
3351
			if (ipsec_transforms->compxf)
3352
				yyerror("comp already set");
3353
			else {
3354
				ipsec_transforms->compxf = parse_xf(yyvsp[0].v.string,
3355
				    compxfs);
3356
				if (!ipsec_transforms->compxf)
3357
					yyerror("%s not a valid transform", yyvsp[0].v.string);
3358
			}
3359
		}
3360
break;
3361
case 79:
3362
#line 718 "parse.y"
3363
{
3364
			if (ipsec_transforms->groupxf)
3365
				yyerror("group already set");
3366
			else {
3367
				ipsec_transforms->groupxf = parse_xf(yyvsp[0].v.string,
3368
				    groupxfs);
3369
				if (!ipsec_transforms->groupxf)
3370
					yyerror("%s not a valid transform", yyvsp[0].v.string);
3371
			}
3372
		}
3373
break;
3374
case 80:
3375
#line 730 "parse.y"
3376
{
3377
			struct ike_mode		*p1;
3378
3379
			/* We create just an empty main mode */
3380
			if ((p1 = calloc(1, sizeof(struct ike_mode))) == NULL)
3381
				err(1, "phase1mode: calloc");
3382
			p1->ike_exch = IKE_MM;
3383
			yyval.v.mode = p1;
3384
		}
3385
break;
3386
case 81:
3387
#line 739 "parse.y"
3388
{
3389
			struct ike_mode *p1;
3390
3391
			if ((p1 = calloc(1, sizeof(struct ike_mode))) == NULL)
3392
				err(1, "phase1mode: calloc");
3393
			p1->xfs = yyvsp[-1].v.transforms;
3394
			p1->life = yyvsp[0].v.life;
3395
			p1->ike_exch = IKE_MM;
3396
			yyval.v.mode = p1;
3397
		}
3398
break;
3399
case 82:
3400
#line 749 "parse.y"
3401
{
3402
			struct ike_mode	*p1;
3403
3404
			if ((p1 = calloc(1, sizeof(struct ike_mode))) == NULL)
3405
				err(1, "phase1mode: calloc");
3406
			p1->xfs = yyvsp[-1].v.transforms;
3407
			p1->life = yyvsp[0].v.life;
3408
			p1->ike_exch = IKE_AM;
3409
			yyval.v.mode = p1;
3410
		}
3411
break;
3412
case 83:
3413
#line 761 "parse.y"
3414
{
3415
			struct ike_mode		*p2;
3416
3417
			/* We create just an empty quick mode */
3418
			if ((p2 = calloc(1, sizeof(struct ike_mode))) == NULL)
3419
				err(1, "phase2mode: calloc");
3420
			p2->ike_exch = IKE_QM;
3421
			yyval.v.mode = p2;
3422
		}
3423
break;
3424
case 84:
3425
#line 770 "parse.y"
3426
{
3427
			struct ike_mode	*p2;
3428
3429
			if ((p2 = calloc(1, sizeof(struct ike_mode))) == NULL)
3430
				err(1, "phase2mode: calloc");
3431
			p2->xfs = yyvsp[-1].v.transforms;
3432
			p2->life = yyvsp[0].v.life;
3433
			p2->ike_exch = IKE_QM;
3434
			yyval.v.mode = p2;
3435
		}
3436
break;
3437
case 85:
3438
#line 782 "parse.y"
3439
{
3440
			struct ipsec_lifetime *life;
3441
3442
			/* We create just an empty transform */
3443
			if ((life = calloc(1, sizeof(struct ipsec_lifetime)))
3444
			    == NULL)
3445
				err(1, "life: calloc");
3446
			life->lt_seconds = -1;
3447
			life->lt_bytes = -1;
3448
			yyval.v.life = life;
3449
		}
3450
break;
3451
case 86:
3452
#line 793 "parse.y"
3453
{
3454
			struct ipsec_lifetime *life;
3455
3456
			if ((life = calloc(1, sizeof(struct ipsec_lifetime)))
3457
			    == NULL)
3458
				err(1, "life: calloc");
3459
			life->lt_seconds = yyvsp[0].v.number;
3460
			life->lt_bytes = -1;
3461
			yyval.v.life = life;
3462
		}
3463
break;
3464
case 87:
3465
#line 803 "parse.y"
3466
{
3467
			yyval.v.life = parse_life(yyvsp[0].v.string);
3468
		}
3469
break;
3470
case 88:
3471
#line 808 "parse.y"
3472
{
3473
			yyval.v.authkeys.keyout = NULL;
3474
			yyval.v.authkeys.keyin = NULL;
3475
		}
3476
break;
3477
case 89:
3478
#line 812 "parse.y"
3479
{
3480
			yyval.v.authkeys.keyout = yyvsp[0].v.keys.keyout;
3481
			yyval.v.authkeys.keyin = yyvsp[0].v.keys.keyin;
3482
		}
3483
break;
3484
case 90:
3485
#line 818 "parse.y"
3486
{
3487
			yyval.v.enckeys.keyout = NULL;
3488
			yyval.v.enckeys.keyin = NULL;
3489
		}
3490
break;
3491
case 91:
3492
#line 822 "parse.y"
3493
{
3494
			yyval.v.enckeys.keyout = yyvsp[0].v.keys.keyout;
3495
			yyval.v.enckeys.keyin = yyvsp[0].v.keys.keyin;
3496
		}
3497
break;
3498
case 92:
3499
#line 828 "parse.y"
3500
{ yyval.v.string = NULL; }
3501
break;
3502
case 93:
3503
#line 829 "parse.y"
3504
{ yyval.v.string = yyvsp[0].v.string; }
3505
break;
3506
case 94:
3507
#line 832 "parse.y"
3508
{
3509
			unsigned char	*hex;
3510
			unsigned char	*p = strchr(yyvsp[0].v.string, ':');
3511
3512
			if (p != NULL ) {
3513
				*p++ = 0;
3514
3515
				if (!strncmp(p, "0x", 2))
3516
					p += 2;
3517
				yyval.v.keys.keyin = parsekey(p, strlen(p));
3518
			} else
3519
				yyval.v.keys.keyin = NULL;
3520
3521
			hex = yyvsp[0].v.string;
3522
			if (!strncmp(hex, "0x", 2))
3523
				hex += 2;
3524
			yyval.v.keys.keyout = parsekey(hex, strlen(hex));
3525
3526
			free(yyvsp[0].v.string);
3527
		}
3528
break;
3529
case 95:
3530
#line 852 "parse.y"
3531
{
3532
			unsigned char	*p = strchr(yyvsp[0].v.string, ':');
3533
3534
			if (p != NULL) {
3535
				*p++ = 0;
3536
				yyval.v.keys.keyin = parsekeyfile(p);
3537
			}
3538
			yyval.v.keys.keyout = parsekeyfile(yyvsp[0].v.string);
3539
			free(yyvsp[0].v.string);
3540
		}
3541
break;
3542
case 96:
3543
#line 864 "parse.y"
3544
{ yyval.v.ikemode = IKE_ACTIVE; }
3545
break;
3546
case 97:
3547
#line 865 "parse.y"
3548
{ yyval.v.ikemode = IKE_PASSIVE; }
3549
break;
3550
case 98:
3551
#line 866 "parse.y"
3552
{ yyval.v.ikemode = IKE_DYNAMIC; }
3553
break;
3554
case 99:
3555
#line 867 "parse.y"
3556
{ yyval.v.ikemode = IKE_ACTIVE; }
3557
break;
3558
case 100:
3559
#line 870 "parse.y"
3560
{
3561
			yyval.v.ikeauth.type = IKE_AUTH_RSA;
3562
			yyval.v.ikeauth.string = NULL;
3563
		}
3564
break;
3565
case 101:
3566
#line 874 "parse.y"
3567
{
3568
			yyval.v.ikeauth.type = IKE_AUTH_RSA;
3569
			yyval.v.ikeauth.string = NULL;
3570
		}
3571
break;
3572
case 102:
3573
#line 878 "parse.y"
3574
{
3575
			yyval.v.ikeauth.type = IKE_AUTH_PSK;
3576
			if ((yyval.v.ikeauth.string = strdup(yyvsp[0].v.string)) == NULL)
3577
				err(1, "ikeauth: strdup");
3578
		}
3579
break;
3580
case 103:
3581
#line 886 "parse.y"
3582
{
3583
			yyval.v.string = NULL;
3584
		}
3585
break;
3586
case 104:
3587
#line 890 "parse.y"
3588
{
3589
			yyval.v.string = yyvsp[0].v.string;
3590
		}
3591
break;
3592
case 105:
3593
#line 896 "parse.y"
3594
{
3595
			if (asprintf(&yyval.v.string, "%s %s", yyvsp[-1].v.string, yyvsp[0].v.string) == -1)
3596
				err(1, "string: asprintf");
3597
			free(yyvsp[-1].v.string);
3598
			free(yyvsp[0].v.string);
3599
		}
3600
break;
3601
case 107:
3602
#line 906 "parse.y"
3603
{
3604
			char *s = yyvsp[-2].v.string;
3605
			if (ipsec->opts & IPSECCTL_OPT_VERBOSE)
3606
				printf("%s = \"%s\"\n", yyvsp[-2].v.string, yyvsp[0].v.string);
3607
			while (*s++) {
3608
				if (isspace((unsigned char)*s)) {
3609
					yyerror("macro name cannot contain "
3610
					    "whitespace");
3611
					YYERROR;
3612
				}
3613
			}
3614
			if (symset(yyvsp[-2].v.string, yyvsp[0].v.string, 0) == -1)
3615
				err(1, "cannot store variable");
3616
			free(yyvsp[-2].v.string);
3617
			free(yyvsp[0].v.string);
3618
		}
3619
break;
3620
#line 3613 "parse.c"
3621
    }
3622
    yyssp -= yym;
3623
    yystate = *yyssp;
3624
    yyvsp -= yym;
3625
    yym = yylhs[yyn];
3626
    if (yystate == 0 && yym == 0)
3627
    {
3628
#if YYDEBUG
3629
        if (yydebug)
3630
            printf("%sdebug: after reduction, shifting from state 0 to\
3631
 state %d\n", YYPREFIX, YYFINAL);
3632
#endif
3633
        yystate = YYFINAL;
3634
        *++yyssp = YYFINAL;
3635
        *++yyvsp = yyval;
3636
        if (yychar < 0)
3637
        {
3638
            if ((yychar = yylex()) < 0) yychar = 0;
3639
#if YYDEBUG
3640
            if (yydebug)
3641
            {
3642
                yys = 0;
3643
                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
3644
                if (!yys) yys = "illegal-symbol";
3645
                printf("%sdebug: state %d, reading %d (%s)\n",
3646
                        YYPREFIX, YYFINAL, yychar, yys);
3647
            }
3648
#endif
3649
        }
3650
        if (yychar == 0) goto yyaccept;
3651
        goto yyloop;
3652
    }
3653
    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
3654
            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
3655
        yystate = yytable[yyn];
3656
    else
3657
        yystate = yydgoto[yym];
3658
#if YYDEBUG
3659
    if (yydebug)
3660
        printf("%sdebug: after reduction, shifting from state %d \
3661
to state %d\n", YYPREFIX, *yyssp, yystate);
3662
#endif
3663
    if (yyssp >= yysslim && yygrowstack())
3664
    {
3665
        goto yyoverflow;
3666
    }
3667
    *++yyssp = yystate;
3668
    *++yyvsp = yyval;
3669
    goto yyloop;
3670
yyoverflow:
3671
    yyerror("yacc stack overflow");
3672
yyabort:
3673
    if (yyss)
3674
            free(yyss);
3675
    if (yyvs)
3676
            free(yyvs);
3677
    yyss = yyssp = NULL;
3678
    yyvs = yyvsp = NULL;
3679
    yystacksize = 0;
3680
    return (1);
3681
yyaccept:
3682
    if (yyss)
3683
            free(yyss);
3684
    if (yyvs)
3685
            free(yyvs);
3686
    yyss = yyssp = NULL;
3687
    yyvs = yyvsp = NULL;
3688
    yystacksize = 0;
3689
    return (0);
3690
}