and added files
[bcm963xx.git] / userapps / opensource / ipsec-tools / src / libipsec / policy_parse.c
1 /* A Bison parser, made by GNU Bison 1.875.  */
2
3 /* Skeleton parser for Yacc-like parsing with Bison,
4    Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002 Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 /* As a special exception, when this file is copied by Bison into a
22    Bison output file, you may use that output file without restriction.
23    This special exception was added by the Free Software Foundation
24    in version 1.24 of Bison.  */
25
26 /* Written by Richard Stallman by simplifying the original so called
27    ``semantic'' parser.  */
28
29 /* All symbols defined below should begin with yy or YY, to avoid
30    infringing on user name space.  This should be done even for local
31    variables, as they might otherwise be expanded by user macros.
32    There are some unavoidable exceptions within include files to
33    define necessary library symbols; they are noted "INFRINGES ON
34    USER NAME SPACE" below.  */
35
36 /* Identify Bison output.  */
37 #define YYBISON 1
38
39 /* Skeleton name.  */
40 #define YYSKELETON_NAME "yacc.c"
41
42 /* Pure parsers.  */
43 #define YYPURE 0
44
45 /* Using locations.  */
46 #define YYLSP_NEEDED 0
47
48 /* If NAME_PREFIX is specified substitute the variables and functions
49    names.  */
50 #define yyparse __libipsecparse
51 #define yylex   __libipseclex
52 #define yyerror __libipsecerror
53 #define yylval  __libipseclval
54 #define yychar  __libipsecchar
55 #define yydebug __libipsecdebug
56 #define yynerrs __libipsecnerrs
57
58
59 /* Tokens.  */
60 #ifndef YYTOKENTYPE
61 # define YYTOKENTYPE
62    /* Put the tokens into the symbol table, so that GDB and other debuggers
63       know about them.  */
64    enum yytokentype {
65      DIR = 258,
66      PRIORITY = 259,
67      PLUS = 260,
68      PRIO_BASE = 261,
69      PRIO_OFFSET = 262,
70      ACTION = 263,
71      PROTOCOL = 264,
72      MODE = 265,
73      LEVEL = 266,
74      LEVEL_SPECIFY = 267,
75      IPADDRESS = 268,
76      ME = 269,
77      ANY = 270,
78      SLASH = 271,
79      HYPHEN = 272
80    };
81 #endif
82 #define DIR 258
83 #define PRIORITY 259
84 #define PLUS 260
85 #define PRIO_BASE 261
86 #define PRIO_OFFSET 262
87 #define ACTION 263
88 #define PROTOCOL 264
89 #define MODE 265
90 #define LEVEL 266
91 #define LEVEL_SPECIFY 267
92 #define IPADDRESS 268
93 #define ME 269
94 #define ANY 270
95 #define SLASH 271
96 #define HYPHEN 272
97
98
99
100
101 /* Copy the first part of user declarations.  */
102 #line 63 "policy_parse.y"
103
104 #ifdef HAVE_CONFIG_H
105 #include "config.h"
106 #endif
107
108 #include <sys/types.h>
109 #include <sys/param.h>
110 #include <sys/socket.h>
111
112 #include <netinet/in.h>
113 #ifdef HAVE_NETINET6_IPSEC
114 #  include <netinet6/ipsec.h>
115 #else
116 #  include <netinet/ipsec.h>
117 #endif
118
119 #include <stdlib.h>
120 #include <stdio.h>
121 #include <string.h>
122 #include <netdb.h>
123
124 #include <errno.h>
125
126 #include "config.h"
127
128 #include "ipsec_strerror.h"
129 #include "libpfkey.h"
130
131 #ifndef INT32_MAX
132 #define INT32_MAX       (0xffffffff)
133 #endif
134
135 #ifndef INT32_MIN
136 #define INT32_MIN       (-INT32_MAX-1)
137 #endif
138
139 #define ATOX(c) \
140   (isdigit(c) ? (c - '0') : (isupper(c) ? (c - 'A' + 10) : (c - 'a' + 10) ))
141
142 static u_int8_t *pbuf = NULL;           /* sadb_x_policy buffer */
143 static int tlen = 0;                    /* total length of pbuf */
144 static int offset = 0;                  /* offset of pbuf */
145 static int p_dir, p_type, p_protocol, p_mode, p_level, p_reqid;
146 static u_int32_t p_priority = 0;
147 static long p_priority_offset = 0;
148 static struct sockaddr *p_src = NULL;
149 static struct sockaddr *p_dst = NULL;
150
151 struct _val;
152 extern void yyerror __P((char *msg));
153 static struct sockaddr *parse_sockaddr __P((struct _val *buf));
154 static int rule_check __P((void));
155 static int init_x_policy __P((void));
156 static int set_x_request __P((struct sockaddr *src, struct sockaddr *dst));
157 static int set_sockaddr __P((struct sockaddr *addr));
158 static void policy_parse_request_init __P((void));
159 static caddr_t policy_parse __P((char *msg, int msglen));
160
161 extern void __policy__strbuffer__init__ __P((char *msg));
162 extern void __policy__strbuffer__free__ __P((void));
163 extern int yyparse __P((void));
164 extern int yylex __P((void));
165
166 extern char *__libipsectext;    /*XXX*/
167
168
169
170 /* Enabling traces.  */
171 #ifndef YYDEBUG
172 # define YYDEBUG 0
173 #endif
174
175 /* Enabling verbose error messages.  */
176 #ifdef YYERROR_VERBOSE
177 # undef YYERROR_VERBOSE
178 # define YYERROR_VERBOSE 1
179 #else
180 # define YYERROR_VERBOSE 0
181 #endif
182
183 #if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED)
184 #line 130 "policy_parse.y"
185 typedef union YYSTYPE {
186         u_int num;
187         u_int32_t num32;
188         struct _val {
189                 int len;
190                 char *buf;
191         } val;
192 } YYSTYPE;
193 /* Line 191 of yacc.c.  */
194 #line 194 "policy_parse.c"
195 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
196 # define YYSTYPE_IS_DECLARED 1
197 # define YYSTYPE_IS_TRIVIAL 1
198 #endif
199
200
201
202 /* Copy the second part of user declarations.  */
203
204
205 /* Line 214 of yacc.c.  */
206 #line 206 "policy_parse.c"
207
208 #if ! defined (yyoverflow) || YYERROR_VERBOSE
209
210 /* The parser invokes alloca or malloc; define the necessary symbols.  */
211
212 # if YYSTACK_USE_ALLOCA
213 #  define YYSTACK_ALLOC alloca
214 # else
215 #  ifndef YYSTACK_USE_ALLOCA
216 #   if defined (alloca) || defined (_ALLOCA_H)
217 #    define YYSTACK_ALLOC alloca
218 #   else
219 #    ifdef __GNUC__
220 #     define YYSTACK_ALLOC __builtin_alloca
221 #    endif
222 #   endif
223 #  endif
224 # endif
225
226 # ifdef YYSTACK_ALLOC
227    /* Pacify GCC's `empty if-body' warning. */
228 #  define YYSTACK_FREE(Ptr) do { /* empty */; } while (0)
229 # else
230 #  if defined (__STDC__) || defined (__cplusplus)
231 #   include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
232 #   define YYSIZE_T size_t
233 #  endif
234 #  define YYSTACK_ALLOC malloc
235 #  define YYSTACK_FREE free
236 # endif
237 #endif /* ! defined (yyoverflow) || YYERROR_VERBOSE */
238
239
240 #if (! defined (yyoverflow) \
241      && (! defined (__cplusplus) \
242          || (YYSTYPE_IS_TRIVIAL)))
243
244 /* A type that is properly aligned for any stack member.  */
245 union yyalloc
246 {
247   short yyss;
248   YYSTYPE yyvs;
249   };
250
251 /* The size of the maximum gap between one aligned stack and the next.  */
252 # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)
253
254 /* The size of an array large to enough to hold all stacks, each with
255    N elements.  */
256 # define YYSTACK_BYTES(N) \
257      ((N) * (sizeof (short) + sizeof (YYSTYPE))                         \
258       + YYSTACK_GAP_MAXIMUM)
259
260 /* Copy COUNT objects from FROM to TO.  The source and destination do
261    not overlap.  */
262 # ifndef YYCOPY
263 #  if 1 < __GNUC__
264 #   define YYCOPY(To, From, Count) \
265       __builtin_memcpy (To, From, (Count) * sizeof (*(From)))
266 #  else
267 #   define YYCOPY(To, From, Count)              \
268       do                                        \
269         {                                       \
270           register YYSIZE_T yyi;                \
271           for (yyi = 0; yyi < (Count); yyi++)   \
272             (To)[yyi] = (From)[yyi];            \
273         }                                       \
274       while (0)
275 #  endif
276 # endif
277
278 /* Relocate STACK from its old location to the new one.  The
279    local variables YYSIZE and YYSTACKSIZE give the old and new number of
280    elements in the stack, and YYPTR gives the new location of the
281    stack.  Advance YYPTR to a properly aligned location for the next
282    stack.  */
283 # define YYSTACK_RELOCATE(Stack)                                        \
284     do                                                                  \
285       {                                                                 \
286         YYSIZE_T yynewbytes;                                            \
287         YYCOPY (&yyptr->Stack, Stack, yysize);                          \
288         Stack = &yyptr->Stack;                                          \
289         yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \
290         yyptr += yynewbytes / sizeof (*yyptr);                          \
291       }                                                                 \
292     while (0)
293
294 #endif
295
296 #if defined (__STDC__) || defined (__cplusplus)
297    typedef signed char yysigned_char;
298 #else
299    typedef short yysigned_char;
300 #endif
301
302 /* YYFINAL -- State number of the termination state. */
303 #define YYFINAL  5
304 /* YYLAST -- Last index in YYTABLE.  */
305 #define YYLAST   44
306
307 /* YYNTOKENS -- Number of terminals. */
308 #define YYNTOKENS  18
309 /* YYNNTS -- Number of nonterminals. */
310 #define YYNNTS  15
311 /* YYNRULES -- Number of rules. */
312 #define YYNRULES  32
313 /* YYNRULES -- Number of states. */
314 #define YYNSTATES  55
315
316 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX.  */
317 #define YYUNDEFTOK  2
318 #define YYMAXUTOK   272
319
320 #define YYTRANSLATE(YYX)                                                \
321   ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
322
323 /* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX.  */
324 static const unsigned char yytranslate[] =
325 {
326        0,     2,     2,     2,     2,     2,     2,     2,     2,     2,
327        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
328        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
329        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
330        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
331        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
332        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
333        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
334        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
335        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
336        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
337        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
338        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
339        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
340        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
341        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
342        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
343        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
344        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
345        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
346        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
347        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
348        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
349        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
350        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
351        2,     2,     2,     2,     2,     2,     1,     2,     3,     4,
352        5,     6,     7,     8,     9,    10,    11,    12,    13,    14,
353       15,    16,    17
354 };
355
356 #if YYDEBUG
357 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
358    YYRHS.  */
359 static const unsigned char yyprhs[] =
360 {
361        0,     0,     3,     4,     9,    10,    17,    18,    26,    27,
362       34,    35,    44,    45,    54,    56,    57,    60,    68,    75,
363       81,    86,    93,    97,   100,   102,   104,   106,   108,   110,
364      111,   116,   120
365 };
366
367 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
368 static const yysigned_char yyrhs[] =
369 {
370       19,     0,    -1,    -1,     3,     8,    20,    26,    -1,    -1,
371        3,     4,     7,     8,    21,    26,    -1,    -1,     3,     4,
372       17,     7,     8,    22,    26,    -1,    -1,     3,     4,     6,
373        8,    23,    26,    -1,    -1,     3,     4,     6,     5,     7,
374        8,    24,    26,    -1,    -1,     3,     4,     6,    17,     7,
375        8,    25,    26,    -1,     3,    -1,    -1,    26,    27,    -1,
376       28,    16,    29,    16,    31,    16,    30,    -1,    28,    16,
377       29,    16,    31,    16,    -1,    28,    16,    29,    16,    31,
378       -1,    28,    16,    29,    16,    -1,    28,    16,    29,    16,
379       16,    30,    -1,    28,    16,    29,    -1,    28,    16,    -1,
380       28,    -1,     9,    -1,    10,    -1,    11,    -1,    12,    -1,
381       -1,    13,    32,    17,    13,    -1,    14,    17,    15,    -1,
382       15,    17,    14,    -1
383 };
384
385 /* YYRLINE[YYN] -- source line where rule number YYN was defined.  */
386 static const unsigned short yyrline[] =
387 {
388        0,   152,   152,   151,   167,   166,   203,   202,   225,   224,
389      236,   235,   258,   257,   279,   291,   293,   305,   306,   307,
390      308,   309,   310,   311,   315,   322,   326,   330,   334,   341,
391      341,   352,   358
392 };
393 #endif
394
395 #if YYDEBUG || YYERROR_VERBOSE
396 /* YYTNME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
397    First, the terminals, then, starting at YYNTOKENS, nonterminals. */
398 static const char *const yytname[] =
399 {
400   "$end", "error", "$undefined", "DIR", "PRIORITY", "PLUS", "PRIO_BASE", 
401   "PRIO_OFFSET", "ACTION", "PROTOCOL", "MODE", "LEVEL", "LEVEL_SPECIFY", 
402   "IPADDRESS", "ME", "ANY", "SLASH", "HYPHEN", "$accept", "policy_spec", 
403   "@1", "@2", "@3", "@4", "@5", "@6", "rules", "rule", "protocol", "mode", 
404   "level", "addresses", "@7", 0
405 };
406 #endif
407
408 # ifdef YYPRINT
409 /* YYTOKNUM[YYLEX-NUM] -- Internal token number corresponding to
410    token YYLEX-NUM.  */
411 static const unsigned short yytoknum[] =
412 {
413        0,   256,   257,   258,   259,   260,   261,   262,   263,   264,
414      265,   266,   267,   268,   269,   270,   271,   272
415 };
416 # endif
417
418 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
419 static const unsigned char yyr1[] =
420 {
421        0,    18,    20,    19,    21,    19,    22,    19,    23,    19,
422       24,    19,    25,    19,    19,    26,    26,    27,    27,    27,
423       27,    27,    27,    27,    27,    28,    29,    30,    30,    32,
424       31,    31,    31
425 };
426
427 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN.  */
428 static const unsigned char yyr2[] =
429 {
430        0,     2,     0,     4,     0,     6,     0,     7,     0,     6,
431        0,     8,     0,     8,     1,     0,     2,     7,     6,     5,
432        4,     6,     3,     2,     1,     1,     1,     1,     1,     0,
433        4,     3,     3
434 };
435
436 /* YYDEFACT[STATE-NAME] -- Default rule to reduce with in state
437    STATE-NUM when YYTABLE doesn't specify something else to do.  Zero
438    means the default is an error.  */
439 static const unsigned char yydefact[] =
440 {
441        0,    14,     0,     0,     2,     1,     0,     0,     0,    15,
442        0,     8,     0,     4,     0,     3,     0,    15,     0,    15,
443        6,    25,    16,    24,    10,     9,    12,     5,    15,    23,
444       15,    15,     7,    26,    22,    11,    13,    20,    29,     0,
445        0,     0,    19,     0,     0,     0,    27,    28,    21,    18,
446        0,    31,    32,    17,    30
447 };
448
449 /* YYDEFGOTO[NTERM-NUM]. */
450 static const yysigned_char yydefgoto[] =
451 {
452       -1,     2,     9,    19,    28,    17,    30,    31,    15,    22,
453       23,    34,    48,    42,    43
454 };
455
456 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
457    STATE-NUM.  */
458 #define YYPACT_NINF -19
459 static const yysigned_char yypact[] =
460 {
461        7,    -3,     4,     1,   -19,   -19,    -2,    15,     2,   -19,
462       17,   -19,    18,   -19,    19,     3,    20,   -19,    21,   -19,
463      -19,   -19,   -19,    10,   -19,     3,   -19,     3,   -19,    22,
464      -19,   -19,     3,   -19,    14,     3,     3,     6,   -19,    16,
465       23,     5,    25,    26,    24,    28,   -19,   -19,   -19,     5,
466       31,   -19,   -19,   -19,   -19
467 };
468
469 /* YYPGOTO[NTERM-NUM].  */
470 static const yysigned_char yypgoto[] =
471 {
472      -19,   -19,   -19,   -19,   -19,   -19,   -19,   -19,   -17,   -19,
473      -19,   -19,   -18,   -19,   -19
474 };
475
476 /* YYTABLE[YYPACT[STATE-NUM]].  What to do in state STATE-NUM.  If
477    positive, shift that token.  If negative, reduce the rule which
478    number is the opposite.  If zero, do what YYDEFACT says.
479    If YYTABLE_NINF, syntax error.  */
480 #define YYTABLE_NINF -1
481 static const unsigned char yytable[] =
482 {
483       25,     3,    27,    10,     5,     4,    11,     6,     7,    14,
484        1,    32,    21,    35,    36,    12,    46,    47,     8,    38,
485       39,    40,    41,    13,    16,    18,    29,    20,    24,    26,
486       37,    53,    33,    44,     0,     0,     0,     0,     0,    51,
487       45,    49,    52,    50,    54
488 };
489
490 static const yysigned_char yycheck[] =
491 {
492       17,     4,    19,     5,     0,     8,     8,     6,     7,     7,
493        3,    28,     9,    30,    31,    17,    11,    12,    17,    13,
494       14,    15,    16,     8,     7,     7,    16,     8,     8,     8,
495       16,    49,    10,    17,    -1,    -1,    -1,    -1,    -1,    15,
496       17,    16,    14,    17,    13
497 };
498
499 /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
500    symbol of state STATE-NUM.  */
501 static const unsigned char yystos[] =
502 {
503        0,     3,    19,     4,     8,     0,     6,     7,    17,    20,
504        5,     8,    17,     8,     7,    26,     7,    23,     7,    21,
505        8,     9,    27,    28,     8,    26,     8,    26,    22,    16,
506       24,    25,    26,    10,    29,    26,    26,    16,    13,    14,
507       15,    16,    31,    32,    17,    17,    11,    12,    30,    16,
508       17,    15,    14,    30,    13
509 };
510
511 #if ! defined (YYSIZE_T) && defined (__SIZE_TYPE__)
512 # define YYSIZE_T __SIZE_TYPE__
513 #endif
514 #if ! defined (YYSIZE_T) && defined (size_t)
515 # define YYSIZE_T size_t
516 #endif
517 #if ! defined (YYSIZE_T)
518 # if defined (__STDC__) || defined (__cplusplus)
519 #  include <stddef.h> /* INFRINGES ON USER NAME SPACE */
520 #  define YYSIZE_T size_t
521 # endif
522 #endif
523 #if ! defined (YYSIZE_T)
524 # define YYSIZE_T unsigned int
525 #endif
526
527 #define yyerrok         (yyerrstatus = 0)
528 #define yyclearin       (yychar = YYEMPTY)
529 #define YYEMPTY         (-2)
530 #define YYEOF           0
531
532 #define YYACCEPT        goto yyacceptlab
533 #define YYABORT         goto yyabortlab
534 #define YYERROR         goto yyerrlab1
535
536 /* Like YYERROR except do call yyerror.  This remains here temporarily
537    to ease the transition to the new meaning of YYERROR, for GCC.
538    Once GCC version 2 has supplanted version 1, this can go.  */
539
540 #define YYFAIL          goto yyerrlab
541
542 #define YYRECOVERING()  (!!yyerrstatus)
543
544 #define YYBACKUP(Token, Value)                                  \
545 do                                                              \
546   if (yychar == YYEMPTY && yylen == 1)                          \
547     {                                                           \
548       yychar = (Token);                                         \
549       yylval = (Value);                                         \
550       yytoken = YYTRANSLATE (yychar);                           \
551       YYPOPSTACK;                                               \
552       goto yybackup;                                            \
553     }                                                           \
554   else                                                          \
555     {                                                           \
556       yyerror ("syntax error: cannot back up");\
557       YYERROR;                                                  \
558     }                                                           \
559 while (0)
560
561 #define YYTERROR        1
562 #define YYERRCODE       256
563
564 /* YYLLOC_DEFAULT -- Compute the default location (before the actions
565    are run).  */
566
567 #ifndef YYLLOC_DEFAULT
568 # define YYLLOC_DEFAULT(Current, Rhs, N)         \
569   Current.first_line   = Rhs[1].first_line;      \
570   Current.first_column = Rhs[1].first_column;    \
571   Current.last_line    = Rhs[N].last_line;       \
572   Current.last_column  = Rhs[N].last_column;
573 #endif
574
575 /* YYLEX -- calling `yylex' with the right arguments.  */
576
577 #ifdef YYLEX_PARAM
578 # define YYLEX yylex (YYLEX_PARAM)
579 #else
580 # define YYLEX yylex ()
581 #endif
582
583 /* Enable debugging if requested.  */
584 #if YYDEBUG
585
586 # ifndef YYFPRINTF
587 #  include <stdio.h> /* INFRINGES ON USER NAME SPACE */
588 #  define YYFPRINTF fprintf
589 # endif
590
591 # define YYDPRINTF(Args)                        \
592 do {                                            \
593   if (yydebug)                                  \
594     YYFPRINTF Args;                             \
595 } while (0)
596
597 # define YYDSYMPRINT(Args)                      \
598 do {                                            \
599   if (yydebug)                                  \
600     yysymprint Args;                            \
601 } while (0)
602
603 # define YYDSYMPRINTF(Title, Token, Value, Location)            \
604 do {                                                            \
605   if (yydebug)                                                  \
606     {                                                           \
607       YYFPRINTF (stderr, "%s ", Title);                         \
608       yysymprint (stderr,                                       \
609                   Token, Value);        \
610       YYFPRINTF (stderr, "\n");                                 \
611     }                                                           \
612 } while (0)
613
614 /*------------------------------------------------------------------.
615 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
616 | TOP (cinluded).                                                   |
617 `------------------------------------------------------------------*/
618
619 #if defined (__STDC__) || defined (__cplusplus)
620 static void
621 yy_stack_print (short *bottom, short *top)
622 #else
623 static void
624 yy_stack_print (bottom, top)
625     short *bottom;
626     short *top;
627 #endif
628 {
629   YYFPRINTF (stderr, "Stack now");
630   for (/* Nothing. */; bottom <= top; ++bottom)
631     YYFPRINTF (stderr, " %d", *bottom);
632   YYFPRINTF (stderr, "\n");
633 }
634
635 # define YY_STACK_PRINT(Bottom, Top)                            \
636 do {                                                            \
637   if (yydebug)                                                  \
638     yy_stack_print ((Bottom), (Top));                           \
639 } while (0)
640
641
642 /*------------------------------------------------.
643 | Report that the YYRULE is going to be reduced.  |
644 `------------------------------------------------*/
645
646 #if defined (__STDC__) || defined (__cplusplus)
647 static void
648 yy_reduce_print (int yyrule)
649 #else
650 static void
651 yy_reduce_print (yyrule)
652     int yyrule;
653 #endif
654 {
655   int yyi;
656   unsigned int yylineno = yyrline[yyrule];
657   YYFPRINTF (stderr, "Reducing stack by rule %d (line %u), ",
658              yyrule - 1, yylineno);
659   /* Print the symbols being reduced, and their result.  */
660   for (yyi = yyprhs[yyrule]; 0 <= yyrhs[yyi]; yyi++)
661     YYFPRINTF (stderr, "%s ", yytname [yyrhs[yyi]]);
662   YYFPRINTF (stderr, "-> %s\n", yytname [yyr1[yyrule]]);
663 }
664
665 # define YY_REDUCE_PRINT(Rule)          \
666 do {                                    \
667   if (yydebug)                          \
668     yy_reduce_print (Rule);             \
669 } while (0)
670
671 /* Nonzero means print parse trace.  It is left uninitialized so that
672    multiple parsers can coexist.  */
673 int yydebug;
674 #else /* !YYDEBUG */
675 # define YYDPRINTF(Args)
676 # define YYDSYMPRINT(Args)
677 # define YYDSYMPRINTF(Title, Token, Value, Location)
678 # define YY_STACK_PRINT(Bottom, Top)
679 # define YY_REDUCE_PRINT(Rule)
680 #endif /* !YYDEBUG */
681
682
683 /* YYINITDEPTH -- initial size of the parser's stacks.  */
684 #ifndef YYINITDEPTH
685 # define YYINITDEPTH 200
686 #endif
687
688 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
689    if the built-in stack extension method is used).
690
691    Do not make this value too large; the results are undefined if
692    SIZE_MAX < YYSTACK_BYTES (YYMAXDEPTH)
693    evaluated with infinite-precision integer arithmetic.  */
694
695 #if YYMAXDEPTH == 0
696 # undef YYMAXDEPTH
697 #endif
698
699 #ifndef YYMAXDEPTH
700 # define YYMAXDEPTH 10000
701 #endif
702
703 \f
704
705 #if YYERROR_VERBOSE
706
707 # ifndef yystrlen
708 #  if defined (__GLIBC__) && defined (_STRING_H)
709 #   define yystrlen strlen
710 #  else
711 /* Return the length of YYSTR.  */
712 static YYSIZE_T
713 #   if defined (__STDC__) || defined (__cplusplus)
714 yystrlen (const char *yystr)
715 #   else
716 yystrlen (yystr)
717      const char *yystr;
718 #   endif
719 {
720   register const char *yys = yystr;
721
722   while (*yys++ != '\0')
723     continue;
724
725   return yys - yystr - 1;
726 }
727 #  endif
728 # endif
729
730 # ifndef yystpcpy
731 #  if defined (__GLIBC__) && defined (_STRING_H) && defined (_GNU_SOURCE)
732 #   define yystpcpy stpcpy
733 #  else
734 /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
735    YYDEST.  */
736 static char *
737 #   if defined (__STDC__) || defined (__cplusplus)
738 yystpcpy (char *yydest, const char *yysrc)
739 #   else
740 yystpcpy (yydest, yysrc)
741      char *yydest;
742      const char *yysrc;
743 #   endif
744 {
745   register char *yyd = yydest;
746   register const char *yys = yysrc;
747
748   while ((*yyd++ = *yys++) != '\0')
749     continue;
750
751   return yyd - 1;
752 }
753 #  endif
754 # endif
755
756 #endif /* !YYERROR_VERBOSE */
757
758 \f
759
760 #if YYDEBUG
761 /*--------------------------------.
762 | Print this symbol on YYOUTPUT.  |
763 `--------------------------------*/
764
765 #if defined (__STDC__) || defined (__cplusplus)
766 static void
767 yysymprint (FILE *yyoutput, int yytype, YYSTYPE *yyvaluep)
768 #else
769 static void
770 yysymprint (yyoutput, yytype, yyvaluep)
771     FILE *yyoutput;
772     int yytype;
773     YYSTYPE *yyvaluep;
774 #endif
775 {
776   /* Pacify ``unused variable'' warnings.  */
777   (void) yyvaluep;
778
779   if (yytype < YYNTOKENS)
780     {
781       YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
782 # ifdef YYPRINT
783       YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
784 # endif
785     }
786   else
787     YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
788
789   switch (yytype)
790     {
791       default:
792         break;
793     }
794   YYFPRINTF (yyoutput, ")");
795 }
796
797 #endif /* ! YYDEBUG */
798 /*-----------------------------------------------.
799 | Release the memory associated to this symbol.  |
800 `-----------------------------------------------*/
801
802 #if defined (__STDC__) || defined (__cplusplus)
803 static void
804 yydestruct (int yytype, YYSTYPE *yyvaluep)
805 #else
806 static void
807 yydestruct (yytype, yyvaluep)
808     int yytype;
809     YYSTYPE *yyvaluep;
810 #endif
811 {
812   /* Pacify ``unused variable'' warnings.  */
813   (void) yyvaluep;
814
815   switch (yytype)
816     {
817
818       default:
819         break;
820     }
821 }
822 \f
823
824 /* Prevent warnings from -Wmissing-prototypes.  */
825
826 #ifdef YYPARSE_PARAM
827 # if defined (__STDC__) || defined (__cplusplus)
828 int yyparse (void *YYPARSE_PARAM);
829 # else
830 int yyparse ();
831 # endif
832 #else /* ! YYPARSE_PARAM */
833 #if defined (__STDC__) || defined (__cplusplus)
834 int yyparse (void);
835 #else
836 int yyparse ();
837 #endif
838 #endif /* ! YYPARSE_PARAM */
839
840
841
842 /* The lookahead symbol.  */
843 int yychar;
844
845 /* The semantic value of the lookahead symbol.  */
846 YYSTYPE yylval;
847
848 /* Number of syntax errors so far.  */
849 int yynerrs;
850
851
852
853 /*----------.
854 | yyparse.  |
855 `----------*/
856
857 #ifdef YYPARSE_PARAM
858 # if defined (__STDC__) || defined (__cplusplus)
859 int yyparse (void *YYPARSE_PARAM)
860 # else
861 int yyparse (YYPARSE_PARAM)
862   void *YYPARSE_PARAM;
863 # endif
864 #else /* ! YYPARSE_PARAM */
865 #if defined (__STDC__) || defined (__cplusplus)
866 int
867 yyparse (void)
868 #else
869 int
870 yyparse ()
871
872 #endif
873 #endif
874 {
875   
876   register int yystate;
877   register int yyn;
878   int yyresult;
879   /* Number of tokens to shift before error messages enabled.  */
880   int yyerrstatus;
881   /* Lookahead token as an internal (translated) token number.  */
882   int yytoken = 0;
883
884   /* Three stacks and their tools:
885      `yyss': related to states,
886      `yyvs': related to semantic values,
887      `yyls': related to locations.
888
889      Refer to the stacks thru separate pointers, to allow yyoverflow
890      to reallocate them elsewhere.  */
891
892   /* The state stack.  */
893   short yyssa[YYINITDEPTH];
894   short *yyss = yyssa;
895   register short *yyssp;
896
897   /* The semantic value stack.  */
898   YYSTYPE yyvsa[YYINITDEPTH];
899   YYSTYPE *yyvs = yyvsa;
900   register YYSTYPE *yyvsp;
901
902
903
904 #define YYPOPSTACK   (yyvsp--, yyssp--)
905
906   YYSIZE_T yystacksize = YYINITDEPTH;
907
908   /* The variables used to return semantic value and location from the
909      action routines.  */
910   YYSTYPE yyval;
911
912
913   /* When reducing, the number of symbols on the RHS of the reduced
914      rule.  */
915   int yylen;
916
917   YYDPRINTF ((stderr, "Starting parse\n"));
918
919   yystate = 0;
920   yyerrstatus = 0;
921   yynerrs = 0;
922   yychar = YYEMPTY;             /* Cause a token to be read.  */
923
924   /* Initialize stack pointers.
925      Waste one element of value and location stack
926      so that they stay on the same level as the state stack.
927      The wasted elements are never initialized.  */
928
929   yyssp = yyss;
930   yyvsp = yyvs;
931
932   goto yysetstate;
933
934 /*------------------------------------------------------------.
935 | yynewstate -- Push a new state, which is found in yystate.  |
936 `------------------------------------------------------------*/
937  yynewstate:
938   /* In all cases, when you get here, the value and location stacks
939      have just been pushed. so pushing a state here evens the stacks.
940      */
941   yyssp++;
942
943  yysetstate:
944   *yyssp = yystate;
945
946   if (yyss + yystacksize - 1 <= yyssp)
947     {
948       /* Get the current used size of the three stacks, in elements.  */
949       YYSIZE_T yysize = yyssp - yyss + 1;
950
951 #ifdef yyoverflow
952       {
953         /* Give user a chance to reallocate the stack. Use copies of
954            these so that the &'s don't force the real ones into
955            memory.  */
956         YYSTYPE *yyvs1 = yyvs;
957         short *yyss1 = yyss;
958
959
960         /* Each stack pointer address is followed by the size of the
961            data in use in that stack, in bytes.  This used to be a
962            conditional around just the two extra args, but that might
963            be undefined if yyoverflow is a macro.  */
964         yyoverflow ("parser stack overflow",
965                     &yyss1, yysize * sizeof (*yyssp),
966                     &yyvs1, yysize * sizeof (*yyvsp),
967
968                     &yystacksize);
969
970         yyss = yyss1;
971         yyvs = yyvs1;
972       }
973 #else /* no yyoverflow */
974 # ifndef YYSTACK_RELOCATE
975       goto yyoverflowlab;
976 # else
977       /* Extend the stack our own way.  */
978       if (YYMAXDEPTH <= yystacksize)
979         goto yyoverflowlab;
980       yystacksize *= 2;
981       if (YYMAXDEPTH < yystacksize)
982         yystacksize = YYMAXDEPTH;
983
984       {
985         short *yyss1 = yyss;
986         union yyalloc *yyptr =
987           (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
988         if (! yyptr)
989           goto yyoverflowlab;
990         YYSTACK_RELOCATE (yyss);
991         YYSTACK_RELOCATE (yyvs);
992
993 #  undef YYSTACK_RELOCATE
994         if (yyss1 != yyssa)
995           YYSTACK_FREE (yyss1);
996       }
997 # endif
998 #endif /* no yyoverflow */
999
1000       yyssp = yyss + yysize - 1;
1001       yyvsp = yyvs + yysize - 1;
1002
1003
1004       YYDPRINTF ((stderr, "Stack size increased to %lu\n",
1005                   (unsigned long int) yystacksize));
1006
1007       if (yyss + yystacksize - 1 <= yyssp)
1008         YYABORT;
1009     }
1010
1011   YYDPRINTF ((stderr, "Entering state %d\n", yystate));
1012
1013   goto yybackup;
1014
1015 /*-----------.
1016 | yybackup.  |
1017 `-----------*/
1018 yybackup:
1019
1020 /* Do appropriate processing given the current state.  */
1021 /* Read a lookahead token if we need one and don't already have one.  */
1022 /* yyresume: */
1023
1024   /* First try to decide what to do without reference to lookahead token.  */
1025
1026   yyn = yypact[yystate];
1027   if (yyn == YYPACT_NINF)
1028     goto yydefault;
1029
1030   /* Not known => get a lookahead token if don't already have one.  */
1031
1032   /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol.  */
1033   if (yychar == YYEMPTY)
1034     {
1035       YYDPRINTF ((stderr, "Reading a token: "));
1036       yychar = YYLEX;
1037     }
1038
1039   if (yychar <= YYEOF)
1040     {
1041       yychar = yytoken = YYEOF;
1042       YYDPRINTF ((stderr, "Now at end of input.\n"));
1043     }
1044   else
1045     {
1046       yytoken = YYTRANSLATE (yychar);
1047       YYDSYMPRINTF ("Next token is", yytoken, &yylval, &yylloc);
1048     }
1049
1050   /* If the proper action on seeing token YYTOKEN is to reduce or to
1051      detect an error, take that action.  */
1052   yyn += yytoken;
1053   if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
1054     goto yydefault;
1055   yyn = yytable[yyn];
1056   if (yyn <= 0)
1057     {
1058       if (yyn == 0 || yyn == YYTABLE_NINF)
1059         goto yyerrlab;
1060       yyn = -yyn;
1061       goto yyreduce;
1062     }
1063
1064   if (yyn == YYFINAL)
1065     YYACCEPT;
1066
1067   /* Shift the lookahead token.  */
1068   YYDPRINTF ((stderr, "Shifting token %s, ", yytname[yytoken]));
1069
1070   /* Discard the token being shifted unless it is eof.  */
1071   if (yychar != YYEOF)
1072     yychar = YYEMPTY;
1073
1074   *++yyvsp = yylval;
1075
1076
1077   /* Count tokens shifted since error; after three, turn off error
1078      status.  */
1079   if (yyerrstatus)
1080     yyerrstatus--;
1081
1082   yystate = yyn;
1083   goto yynewstate;
1084
1085
1086 /*-----------------------------------------------------------.
1087 | yydefault -- do the default action for the current state.  |
1088 `-----------------------------------------------------------*/
1089 yydefault:
1090   yyn = yydefact[yystate];
1091   if (yyn == 0)
1092     goto yyerrlab;
1093   goto yyreduce;
1094
1095
1096 /*-----------------------------.
1097 | yyreduce -- Do a reduction.  |
1098 `-----------------------------*/
1099 yyreduce:
1100   /* yyn is the number of a rule to reduce with.  */
1101   yylen = yyr2[yyn];
1102
1103   /* If YYLEN is nonzero, implement the default value of the action:
1104      `$$ = $1'.
1105
1106      Otherwise, the following line sets YYVAL to garbage.
1107      This behavior is undocumented and Bison
1108      users should not rely upon it.  Assigning to YYVAL
1109      unconditionally makes the parser a bit smaller, and it avoids a
1110      GCC warning that YYVAL may be used uninitialized.  */
1111   yyval = yyvsp[1-yylen];
1112
1113
1114   YY_REDUCE_PRINT (yyn);
1115   switch (yyn)
1116     {
1117         case 2:
1118 #line 152 "policy_parse.y"
1119     {
1120                         p_dir = yyvsp[-1].num;
1121                         p_type = yyvsp[0].num;
1122
1123 #ifdef HAVE_PFKEY_POLICY_PRIORITY
1124                         p_priority = PRIORITY_DEFAULT;
1125 #else
1126                         p_priority = 0;
1127 #endif
1128
1129                         if (init_x_policy())
1130                                 return -1;
1131                 }
1132     break;
1133
1134   case 4:
1135 #line 167 "policy_parse.y"
1136     {
1137                         char *offset_buf;
1138
1139                         p_dir = yyvsp[-3].num;
1140                         p_type = yyvsp[0].num;
1141
1142                         /* buffer big enough to hold a prepended negative sign */
1143                         offset_buf = malloc(yyvsp[-1].val.len + 2);
1144                         if (offset_buf == NULL) 
1145                         {
1146                                 __ipsec_errcode = EIPSEC_NO_BUFS;
1147                                 return -1;
1148                         }
1149
1150                         /* positive input value means higher priority, therefore lower
1151                            actual value so that is closer to the beginning of the list */
1152                         sprintf (offset_buf, "-%s", yyvsp[-1].val.buf);
1153
1154                         errno = 0;
1155                         p_priority_offset = atol(offset_buf);
1156
1157                         free(offset_buf);
1158
1159                         if (errno != 0 || p_priority_offset < INT32_MIN)
1160                         {
1161                                 __ipsec_errcode = EIPSEC_INVAL_PRIORITY_OFFSET;
1162                                 return -1;
1163                         }
1164
1165                         p_priority = PRIORITY_DEFAULT + (u_int32_t) p_priority_offset;
1166
1167                         if (init_x_policy())
1168                                 return -1;
1169                 }
1170     break;
1171
1172   case 6:
1173 #line 203 "policy_parse.y"
1174     {
1175                         p_dir = yyvsp[-4].num;
1176                         p_type = yyvsp[0].num;
1177
1178                         errno = 0;
1179                         p_priority_offset = atol(yyvsp[-1].val.buf);
1180
1181                         if (errno != 0 || p_priority_offset > INT32_MAX)
1182                         {
1183                                 __ipsec_errcode = EIPSEC_INVAL_PRIORITY_OFFSET;
1184                                 return -1;
1185                         }
1186
1187                         /* negative input value means lower priority, therefore higher
1188                            actual value so that is closer to the end of the list */
1189                         p_priority = PRIORITY_DEFAULT + (u_int32_t) p_priority_offset;
1190
1191                         if (init_x_policy())
1192                                 return -1;
1193                 }
1194     break;
1195
1196   case 8:
1197 #line 225 "policy_parse.y"
1198     {
1199                         p_dir = yyvsp[-3].num;
1200                         p_type = yyvsp[0].num;
1201
1202                         p_priority = yyvsp[-1].num32;
1203
1204                         if (init_x_policy())
1205                                 return -1;
1206                 }
1207     break;
1208
1209   case 10:
1210 #line 236 "policy_parse.y"
1211     {
1212                         p_dir = yyvsp[-5].num;
1213                         p_type = yyvsp[0].num;
1214
1215                         errno = 0;
1216                         p_priority_offset = atol(yyvsp[-1].val.buf);
1217
1218                         if (errno != 0 || p_priority_offset > PRIORITY_OFFSET_NEGATIVE_MAX)
1219                         {
1220                                 __ipsec_errcode = EIPSEC_INVAL_PRIORITY_BASE_OFFSET;
1221                                 return -1;
1222                         }
1223
1224                         /* adding value means higher priority, therefore lower
1225                            actual value so that is closer to the beginning of the list */
1226                         p_priority = yyvsp[-3].num32 - (u_int32_t) p_priority_offset;
1227
1228                         if (init_x_policy())
1229                                 return -1;
1230                 }
1231     break;
1232
1233   case 12:
1234 #line 258 "policy_parse.y"
1235     {
1236                         p_dir = yyvsp[-5].num;
1237                         p_type = yyvsp[0].num;
1238
1239                         errno = 0;
1240                         p_priority_offset = atol(yyvsp[-1].val.buf);
1241
1242                         if (errno != 0 || p_priority_offset > PRIORITY_OFFSET_POSITIVE_MAX)
1243                         {
1244                                 __ipsec_errcode = EIPSEC_INVAL_PRIORITY_BASE_OFFSET;
1245                                 return -1;
1246                         }
1247
1248                         /* subtracting value means lower priority, therefore higher
1249                            actual value so that is closer to the end of the list */
1250                         p_priority = yyvsp[-3].num32 + (u_int32_t) p_priority_offset;
1251
1252                         if (init_x_policy())
1253                                 return -1;
1254                 }
1255     break;
1256
1257   case 14:
1258 #line 280 "policy_parse.y"
1259     {
1260                         p_dir = yyvsp[0].num;
1261                         p_type = 0;     /* ignored it by kernel */
1262
1263                         p_priority = 0;
1264
1265                         if (init_x_policy())
1266                                 return -1;
1267                 }
1268     break;
1269
1270   case 16:
1271 #line 293 "policy_parse.y"
1272     {
1273                         if (rule_check() < 0)
1274                                 return -1;
1275
1276                         if (set_x_request(p_src, p_dst) < 0)
1277                                 return -1;
1278
1279                         policy_parse_request_init();
1280                 }
1281     break;
1282
1283   case 23:
1284 #line 311 "policy_parse.y"
1285     {
1286                         __ipsec_errcode = EIPSEC_FEW_ARGUMENTS;
1287                         return -1;
1288                 }
1289     break;
1290
1291   case 24:
1292 #line 315 "policy_parse.y"
1293     {
1294                         __ipsec_errcode = EIPSEC_FEW_ARGUMENTS;
1295                         return -1;
1296                 }
1297     break;
1298
1299   case 25:
1300 #line 322 "policy_parse.y"
1301     { p_protocol = yyvsp[0].num; }
1302     break;
1303
1304   case 26:
1305 #line 326 "policy_parse.y"
1306     { p_mode = yyvsp[0].num; }
1307     break;
1308
1309   case 27:
1310 #line 330 "policy_parse.y"
1311     {
1312                         p_level = yyvsp[0].num;
1313                         p_reqid = 0;
1314                 }
1315     break;
1316
1317   case 28:
1318 #line 334 "policy_parse.y"
1319     {
1320                         p_level = IPSEC_LEVEL_UNIQUE;
1321                         p_reqid = atol(yyvsp[0].val.buf);       /* atol() is good. */
1322                 }
1323     break;
1324
1325   case 29:
1326 #line 341 "policy_parse.y"
1327     {
1328                         p_src = parse_sockaddr(&yyvsp[0].val);
1329                         if (p_src == NULL)
1330                                 return -1;
1331                 }
1332     break;
1333
1334   case 30:
1335 #line 347 "policy_parse.y"
1336     {
1337                         p_dst = parse_sockaddr(&yyvsp[0].val);
1338                         if (p_dst == NULL)
1339                                 return -1;
1340                 }
1341     break;
1342
1343   case 31:
1344 #line 352 "policy_parse.y"
1345     {
1346                         if (p_dir != IPSEC_DIR_OUTBOUND) {
1347                                 __ipsec_errcode = EIPSEC_INVAL_DIR;
1348                                 return -1;
1349                         }
1350                 }
1351     break;
1352
1353   case 32:
1354 #line 358 "policy_parse.y"
1355     {
1356                         if (p_dir != IPSEC_DIR_INBOUND) {
1357                                 __ipsec_errcode = EIPSEC_INVAL_DIR;
1358                                 return -1;
1359                         }
1360                 }
1361     break;
1362
1363
1364     }
1365
1366 /* Line 991 of yacc.c.  */
1367 #line 1367 "policy_parse.c"
1368 \f
1369   yyvsp -= yylen;
1370   yyssp -= yylen;
1371
1372
1373   YY_STACK_PRINT (yyss, yyssp);
1374
1375   *++yyvsp = yyval;
1376
1377
1378   /* Now `shift' the result of the reduction.  Determine what state
1379      that goes to, based on the state we popped back to and the rule
1380      number reduced by.  */
1381
1382   yyn = yyr1[yyn];
1383
1384   yystate = yypgoto[yyn - YYNTOKENS] + *yyssp;
1385   if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp)
1386     yystate = yytable[yystate];
1387   else
1388     yystate = yydefgoto[yyn - YYNTOKENS];
1389
1390   goto yynewstate;
1391
1392
1393 /*------------------------------------.
1394 | yyerrlab -- here on detecting error |
1395 `------------------------------------*/
1396 yyerrlab:
1397   /* If not already recovering from an error, report this error.  */
1398   if (!yyerrstatus)
1399     {
1400       ++yynerrs;
1401 #if YYERROR_VERBOSE
1402       yyn = yypact[yystate];
1403
1404       if (YYPACT_NINF < yyn && yyn < YYLAST)
1405         {
1406           YYSIZE_T yysize = 0;
1407           int yytype = YYTRANSLATE (yychar);
1408           char *yymsg;
1409           int yyx, yycount;
1410
1411           yycount = 0;
1412           /* Start YYX at -YYN if negative to avoid negative indexes in
1413              YYCHECK.  */
1414           for (yyx = yyn < 0 ? -yyn : 0;
1415                yyx < (int) (sizeof (yytname) / sizeof (char *)); yyx++)
1416             if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
1417               yysize += yystrlen (yytname[yyx]) + 15, yycount++;
1418           yysize += yystrlen ("syntax error, unexpected ") + 1;
1419           yysize += yystrlen (yytname[yytype]);
1420           yymsg = (char *) YYSTACK_ALLOC (yysize);
1421           if (yymsg != 0)
1422             {
1423               char *yyp = yystpcpy (yymsg, "syntax error, unexpected ");
1424               yyp = yystpcpy (yyp, yytname[yytype]);
1425
1426               if (yycount < 5)
1427                 {
1428                   yycount = 0;
1429                   for (yyx = yyn < 0 ? -yyn : 0;
1430                        yyx < (int) (sizeof (yytname) / sizeof (char *));
1431                        yyx++)
1432                     if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
1433                       {
1434                         const char *yyq = ! yycount ? ", expecting " : " or ";
1435                         yyp = yystpcpy (yyp, yyq);
1436                         yyp = yystpcpy (yyp, yytname[yyx]);
1437                         yycount++;
1438                       }
1439                 }
1440               yyerror (yymsg);
1441               YYSTACK_FREE (yymsg);
1442             }
1443           else
1444             yyerror ("syntax error; also virtual memory exhausted");
1445         }
1446       else
1447 #endif /* YYERROR_VERBOSE */
1448         yyerror ("syntax error");
1449     }
1450
1451
1452
1453   if (yyerrstatus == 3)
1454     {
1455       /* If just tried and failed to reuse lookahead token after an
1456          error, discard it.  */
1457
1458       /* Return failure if at end of input.  */
1459       if (yychar == YYEOF)
1460         {
1461           /* Pop the error token.  */
1462           YYPOPSTACK;
1463           /* Pop the rest of the stack.  */
1464           while (yyss < yyssp)
1465             {
1466               YYDSYMPRINTF ("Error: popping", yystos[*yyssp], yyvsp, yylsp);
1467               yydestruct (yystos[*yyssp], yyvsp);
1468               YYPOPSTACK;
1469             }
1470           YYABORT;
1471         }
1472
1473       YYDSYMPRINTF ("Error: discarding", yytoken, &yylval, &yylloc);
1474       yydestruct (yytoken, &yylval);
1475       yychar = YYEMPTY;
1476
1477     }
1478
1479   /* Else will try to reuse lookahead token after shifting the error
1480      token.  */
1481   goto yyerrlab2;
1482
1483
1484 /*----------------------------------------------------.
1485 | yyerrlab1 -- error raised explicitly by an action.  |
1486 `----------------------------------------------------*/
1487 yyerrlab1:
1488
1489   /* Suppress GCC warning that yyerrlab1 is unused when no action
1490      invokes YYERROR.  */
1491 #if defined (__GNUC_MINOR__) && 2093 <= (__GNUC__ * 1000 + __GNUC_MINOR__) \
1492     && !defined __cplusplus
1493   __attribute__ ((__unused__))
1494 #endif
1495
1496
1497   goto yyerrlab2;
1498
1499
1500 /*---------------------------------------------------------------.
1501 | yyerrlab2 -- pop states until the error token can be shifted.  |
1502 `---------------------------------------------------------------*/
1503 yyerrlab2:
1504   yyerrstatus = 3;      /* Each real token shifted decrements this.  */
1505
1506   for (;;)
1507     {
1508       yyn = yypact[yystate];
1509       if (yyn != YYPACT_NINF)
1510         {
1511           yyn += YYTERROR;
1512           if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
1513             {
1514               yyn = yytable[yyn];
1515               if (0 < yyn)
1516                 break;
1517             }
1518         }
1519
1520       /* Pop the current state because it cannot handle the error token.  */
1521       if (yyssp == yyss)
1522         YYABORT;
1523
1524       YYDSYMPRINTF ("Error: popping", yystos[*yyssp], yyvsp, yylsp);
1525       yydestruct (yystos[yystate], yyvsp);
1526       yyvsp--;
1527       yystate = *--yyssp;
1528
1529       YY_STACK_PRINT (yyss, yyssp);
1530     }
1531
1532   if (yyn == YYFINAL)
1533     YYACCEPT;
1534
1535   YYDPRINTF ((stderr, "Shifting error token, "));
1536
1537   *++yyvsp = yylval;
1538
1539
1540   yystate = yyn;
1541   goto yynewstate;
1542
1543
1544 /*-------------------------------------.
1545 | yyacceptlab -- YYACCEPT comes here.  |
1546 `-------------------------------------*/
1547 yyacceptlab:
1548   yyresult = 0;
1549   goto yyreturn;
1550
1551 /*-----------------------------------.
1552 | yyabortlab -- YYABORT comes here.  |
1553 `-----------------------------------*/
1554 yyabortlab:
1555   yyresult = 1;
1556   goto yyreturn;
1557
1558 #ifndef yyoverflow
1559 /*----------------------------------------------.
1560 | yyoverflowlab -- parser overflow comes here.  |
1561 `----------------------------------------------*/
1562 yyoverflowlab:
1563   yyerror ("parser stack overflow");
1564   yyresult = 2;
1565   /* Fall through.  */
1566 #endif
1567
1568 yyreturn:
1569 #ifndef yyoverflow
1570   if (yyss != yyssa)
1571     YYSTACK_FREE (yyss);
1572 #endif
1573   return yyresult;
1574 }
1575
1576
1577 #line 369 "policy_parse.y"
1578
1579
1580 void
1581 yyerror(msg)
1582         char *msg;
1583 {
1584         fprintf(stderr, "libipsec: %s while parsing \"%s\"\n",
1585                 msg, __libipsectext);
1586
1587         return;
1588 }
1589
1590 static struct sockaddr *
1591 parse_sockaddr(buf)
1592         struct _val *buf;
1593 {
1594         struct addrinfo hints, *res;
1595         char *serv = NULL;
1596         int error;
1597         struct sockaddr *newaddr = NULL;
1598
1599         memset(&hints, 0, sizeof(hints));
1600         hints.ai_family = PF_UNSPEC;
1601         hints.ai_flags = AI_NUMERICHOST;
1602         error = getaddrinfo(buf->buf, serv, &hints, &res);
1603         if (error != 0) {
1604                 yyerror("invalid IP address");
1605                 __ipsec_set_strerror(gai_strerror(error));
1606                 return NULL;
1607         }
1608
1609         if (res->ai_addr == NULL) {
1610                 yyerror("invalid IP address");
1611                 __ipsec_set_strerror(gai_strerror(error));
1612                 return NULL;
1613         }
1614
1615         newaddr = malloc(res->ai_addrlen);
1616         if (newaddr == NULL) {
1617                 __ipsec_errcode = EIPSEC_NO_BUFS;
1618                 freeaddrinfo(res);
1619                 return NULL;
1620         }
1621         memcpy(newaddr, res->ai_addr, res->ai_addrlen);
1622
1623         freeaddrinfo(res);
1624
1625         __ipsec_errcode = EIPSEC_NO_ERROR;
1626         return newaddr;
1627 }
1628
1629 static int
1630 rule_check()
1631 {
1632         if (p_type == IPSEC_POLICY_IPSEC) {
1633                 if (p_protocol == IPPROTO_IP) {
1634                         __ipsec_errcode = EIPSEC_NO_PROTO;
1635                         return -1;
1636                 }
1637
1638                 if (p_mode != IPSEC_MODE_TRANSPORT
1639                  && p_mode != IPSEC_MODE_TUNNEL) {
1640                         __ipsec_errcode = EIPSEC_INVAL_MODE;
1641                         return -1;
1642                 }
1643
1644                 if (p_src == NULL && p_dst == NULL) {
1645                          if (p_mode != IPSEC_MODE_TRANSPORT) {
1646                                 __ipsec_errcode = EIPSEC_INVAL_ADDRESS;
1647                                 return -1;
1648                         }
1649                 }
1650                 else if (p_src->sa_family != p_dst->sa_family) {
1651                         __ipsec_errcode = EIPSEC_FAMILY_MISMATCH;
1652                         return -1;
1653                 }
1654         }
1655
1656         __ipsec_errcode = EIPSEC_NO_ERROR;
1657         return 0;
1658 }
1659
1660 static int
1661 init_x_policy()
1662 {
1663         struct sadb_x_policy *p;
1664
1665         if (pbuf) {
1666                 free(pbuf);
1667                 tlen = 0;
1668         }
1669         pbuf = malloc(sizeof(struct sadb_x_policy));
1670         if (pbuf == NULL) {
1671                 __ipsec_errcode = EIPSEC_NO_BUFS;
1672                 return -1;
1673         }
1674         tlen = sizeof(struct sadb_x_policy);
1675
1676         memset(pbuf, 0, tlen);
1677         p = (struct sadb_x_policy *)pbuf;
1678         p->sadb_x_policy_len = 0;       /* must update later */
1679         p->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1680         p->sadb_x_policy_type = p_type;
1681         p->sadb_x_policy_dir = p_dir;
1682         p->sadb_x_policy_id = 0;
1683 #ifdef HAVE_PFKEY_POLICY_PRIORITY
1684         p->sadb_x_policy_priority = p_priority;
1685 #else
1686     /* fail if given a priority and libipsec was not compiled with 
1687            priority support */
1688         if (p_priority != 0)
1689         {
1690                 __ipsec_errcode = EIPSEC_PRIORITY_NOT_COMPILED;
1691                 return -1;
1692         }
1693 #endif
1694
1695         offset = tlen;
1696
1697         __ipsec_errcode = EIPSEC_NO_ERROR;
1698         return 0;
1699 }
1700
1701 static int
1702 set_x_request(src, dst)
1703         struct sockaddr *src, *dst;
1704 {
1705         struct sadb_x_ipsecrequest *p;
1706         int reqlen;
1707         caddr_t n;
1708
1709         reqlen = sizeof(*p)
1710                 + (src ? sysdep_sa_len(src) : 0)
1711                 + (dst ? sysdep_sa_len(dst) : 0);
1712         tlen += reqlen;         /* increment to total length */
1713
1714         n = realloc(pbuf, tlen);
1715         if (n == NULL) {
1716                 __ipsec_errcode = EIPSEC_NO_BUFS;
1717                 return -1;
1718         }
1719         pbuf = n;
1720         p = (struct sadb_x_ipsecrequest *)&pbuf[offset];
1721         p->sadb_x_ipsecrequest_len = reqlen;
1722         p->sadb_x_ipsecrequest_proto = p_protocol;
1723         p->sadb_x_ipsecrequest_mode = p_mode;
1724         p->sadb_x_ipsecrequest_level = p_level;
1725         p->sadb_x_ipsecrequest_reqid = p_reqid;
1726         offset += sizeof(*p);
1727
1728         if (set_sockaddr(src) || set_sockaddr(dst))
1729                 return -1;
1730
1731         __ipsec_errcode = EIPSEC_NO_ERROR;
1732         return 0;
1733 }
1734
1735 static int
1736 set_sockaddr(addr)
1737         struct sockaddr *addr;
1738 {
1739         if (addr == NULL) {
1740                 __ipsec_errcode = EIPSEC_NO_ERROR;
1741                 return 0;
1742         }
1743
1744         /* tlen has already incremented */
1745
1746         memcpy(&pbuf[offset], addr, sysdep_sa_len(addr));
1747
1748         offset += sysdep_sa_len(addr);
1749
1750         __ipsec_errcode = EIPSEC_NO_ERROR;
1751         return 0;
1752 }
1753
1754 static void
1755 policy_parse_request_init()
1756 {
1757         p_protocol = IPPROTO_IP;
1758         p_mode = IPSEC_MODE_ANY;
1759         p_level = IPSEC_LEVEL_DEFAULT;
1760         p_reqid = 0;
1761         if (p_src != NULL) {
1762                 free(p_src);
1763                 p_src = NULL;
1764         }
1765         if (p_dst != NULL) {
1766                 free(p_dst);
1767                 p_dst = NULL;
1768         }
1769
1770         return;
1771 }
1772
1773 static caddr_t
1774 policy_parse(msg, msglen)
1775         char *msg;
1776         int msglen;
1777 {
1778         int error;
1779
1780         pbuf = NULL;
1781         tlen = 0;
1782
1783         /* initialize */
1784         p_dir = IPSEC_DIR_INVALID;
1785         p_type = IPSEC_POLICY_DISCARD;
1786         policy_parse_request_init();
1787         __policy__strbuffer__init__(msg);
1788
1789         error = yyparse();      /* it must be set errcode. */
1790         __policy__strbuffer__free__();
1791
1792         if (error) {
1793                 if (pbuf != NULL)
1794                         free(pbuf);
1795                 return NULL;
1796         }
1797
1798         /* update total length */
1799         ((struct sadb_x_policy *)pbuf)->sadb_x_policy_len = PFKEY_UNIT64(tlen);
1800
1801         __ipsec_errcode = EIPSEC_NO_ERROR;
1802
1803         return pbuf;
1804 }
1805
1806 caddr_t
1807 ipsec_set_policy(msg, msglen)
1808         char *msg;
1809         int msglen;
1810 {
1811         caddr_t policy;
1812
1813         policy = policy_parse(msg, msglen);
1814         if (policy == NULL) {
1815                 if (__ipsec_errcode == EIPSEC_NO_ERROR)
1816                         __ipsec_errcode = EIPSEC_INVAL_ARGUMENT;
1817                 return NULL;
1818         }
1819
1820         __ipsec_errcode = EIPSEC_NO_ERROR;
1821         return policy;
1822 }
1823
1824