## modules/rpsl/import.y

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. yyparse
2. importerror

       1 %{
       2 /*
       3   filename: import.y
       4
       5   description:
       6     Defines the grammar for an RPSL import attribute.  It was mostly
       7     stolen from the IRRToolSet, simplified by removing ability to parse
       8     things defined by a dictionary (we use XML for extensibility rather
       9     than a dictionary).
      10
      11   notes:
      12     Defines tokens for the associated lexer, import.l.
      13 */
      14 %}
      15
      16 %union {
      17     char *sval;
      18 }
      19
      20 %token OP_OR OP_AND OP_NOT OP_MS OP_EQUAL OP_APPEND OP_COMPARE
      21 %token KEYW_ANY KEYW_PEERAS
      22 %token ASPATH_POSTFIX
      23 %token TKN_FLTRNAME TKN_ASNO TKN_RSNAME TKN_ASNAME TKN_PRFXV4 TKN_PRFXV4RNG
      24 %token TKN_IPV4 TKN_DNS TKN_RTRSNAME TKN_PRNGNAME
      25 %token KEYW_ACTION KEYW_EXCEPT
      26 %token TKN_PREF TKN_MED TKN_DPA TKN_ASPATH TKN_COMMUNITY TKN_NEXT_HOP TKN_COST
      27 %token TKN_COMM_NO
      28 %token KEYW_IGP_COST KEYW_SELF KEYW_PREPEND
      29 %token KEYW_APPEND KEYW_DELETE KEYW_CONTAINS KEYW_AT
      30 %token KEYW_INTERNET KEYW_NO_EXPORT KEYW_NO_ADVERTISE
      31 %token KEYW_PROTOCOL TKN_PROTOCOL
      32 %token KEYW_INTO KEYW_REFINE KEYW_ACCEPT KEYW_FROM
      33 %token <sval> TKN_INT TKN_DNS
      34 %type <sval> domain_name
      35
      36 %{
      37 #include <stdio.h>
      38 #include <stdarg.h>
      39 #include <stdlib.h>
      40
      41 int yyerror(const char *s);
      42 void syntax_error(char *fmt, ...);
      43
      44 %}
      45
      46 %%
         /* [<][>][^][v][top][bottom][index][help] */
      47
      48 import_attribute: opt_protocol_from opt_protocol_into import_expr
      49 | opt_protocol_from opt_protocol_into import_factor
      50 ;
      51
      52 opt_protocol_from:
      53 | KEYW_PROTOCOL TKN_PROTOCOL
      54 ;
      55
      56 opt_protocol_into:
      57 | KEYW_INTO TKN_PROTOCOL
      58 ;
      59
      60 import_expr: import_term
      61 | import_term KEYW_REFINE import_expr
      62 | import_term KEYW_EXCEPT import_expr
      63 ;
      64
      65 import_term: import_factor ';'
      66 | '{' import_factor_list '}'
      67 ;
      68
      69 import_factor_list: import_factor ';'
      70 | import_factor_list import_factor ';'
      71 ;
      72
      73 import_factor: import_peering_action_list KEYW_ACCEPT filter
      74 ;
      75
      76 import_peering_action_list: KEYW_FROM peering opt_action
      77 | import_peering_action_list KEYW_FROM peering opt_action
      78 ;
      79
      80 peering: as_expr opt_router_expr opt_router_expr_with_at
      81 | TKN_PRNGNAME
      82 ;
      83
      84 opt_action:
      85 | KEYW_ACTION action
      86 ;
      87
      88 as_expr: as_expr OP_OR as_expr_term
      89 | as_expr_term
      90 ;
      91
      92 as_expr_term: as_expr_term OP_AND as_expr_factor
      93 | as_expr_term KEYW_EXCEPT as_expr_factor
      94 | as_expr_factor
      95 ;
      96
      97 as_expr_factor: '(' as_expr ')'
      98 | as_expr_operand
      99 ;
     100
     101 as_expr_operand: TKN_ASNO
     102 | TKN_ASNAME
     103 ;
     104
     105 opt_router_expr:
     106 | router_expr
     107 ;
     108
     109 opt_router_expr_with_at:
     110 | KEYW_AT router_expr
     111 ;
     112
     113 router_expr: router_expr OP_OR router_expr_term
     114 | router_expr_term
     115 ;
     116
     117 router_expr_term: router_expr_term OP_AND router_expr_factor
     118 | router_expr_term KEYW_EXCEPT router_expr_factor
     119 | router_expr_factor
     120 ;
     121
     122 router_expr_factor: '(' router_expr ')'
     123 | router_expr_operand
     124 ;
     125
     126 router_expr_operand: TKN_IPV4
     127 | domain_name {
     128     if (strlen($1) > 255) {
     129         syntax_error("Domain name \"%s\" is longer than 255 characters", $1);
     130     }
     131 }
     132 | TKN_RTRSNAME
     133 ;
     134
     135 domain_name: TKN_DNS
     136 | domain_name '.' TKN_DNS
     137 ;
     138
     139 action: rp_attribute ';'
     140 | action rp_attribute ';'
     141 ;
     142
     143 rp_attribute: pref
     144 | med
     145 | dpa
     146 | aspath
     147 | community
     148 | next_hop
     149 | cost
     150 ;
     151
     152 pref: TKN_PREF OP_EQUAL TKN_INT {
     153       long int val;
     154       char *s, *p;
     155       p = $3;
     156       val = strtol(p, &s, 10);
     157       if ((val < 0) || (val > 65535)) {
     158           syntax_error("pref value \"%s\" is not between 0 and 65535", p);
     159       }
     160 }
     161 ;
     162
     163 med: TKN_MED OP_EQUAL TKN_INT {
     164       long int val;
     165       char *s, *p;
     166       p = $3;
     167       val = strtol(p, &s, 10);
     168       if ((val < 0) || (val > 65535)) {
     169           syntax_error("med value \"%s\" is not between 0 and 65535", p);
     170       }
     171 }
     172 | TKN_MED OP_EQUAL KEYW_IGP_COST
     173 ;
     174
     175 dpa: TKN_DPA OP_EQUAL TKN_INT {
     176       long int val;
     177       char *s, *p;
     178       p = $3;
     179       val = strtol(p, &s, 10);
     180       if ((val < 0) || (val > 65535)) {
     181           syntax_error("dpa value \"%s\" is not between 0 and 65535", p);
     182       }
     183 }
     184 ;
     185
     186 aspath: TKN_ASPATH '.' KEYW_PREPEND '(' asno_list ')'
     187 ;
     188
     189 asno_list: TKN_ASNO
     190 | asno_list ',' TKN_ASNO
     191 ;
     192
     193 community: TKN_COMMUNITY OP_EQUAL community_list
     194 | TKN_COMMUNITY OP_APPEND community_list
     195 | TKN_COMMUNITY '.' KEYW_APPEND '(' community_elm_list ')'
     196 | TKN_COMMUNITY '.' KEYW_DELETE '(' community_elm_list ')'
     197 | TKN_COMMUNITY '.' KEYW_CONTAINS '(' community_elm_list ')'
     198 | TKN_COMMUNITY '(' community_elm_list ')'
     199 | TKN_COMMUNITY OP_COMPARE community_list
     200 ;
     201
     202 community_list: '{' community_elm_list '}'
     203 ;
     204
     205 community_elm_list: community_elm
     206 | community_elm_list ',' community_elm
     207 ;
     208
     209 community_elm: KEYW_INTERNET
     210 | KEYW_NO_EXPORT
     211 | KEYW_NO_ADVERTISE
     212 | TKN_INT {
     213       unsigned long int val;
     214       char *s, *p;
     215       p = $1;
     216       val = strtoul(p, &s, 10);
     217       if ((val < 1) || (val > 4294967295UL) || (*s != '\0')) {
     218           syntax_error("community element \"%s\" is not between 1 and 4294967295",
     219                        p);
     220       }
     221 }
     222 | TKN_COMM_NO
     223 ;
     224
     225 next_hop: TKN_NEXT_HOP OP_EQUAL TKN_IPV4
     226 | TKN_NEXT_HOP OP_EQUAL KEYW_SELF
     227 ;
     228
     229 cost: TKN_COST OP_EQUAL TKN_INT {
     230       long int val;
     231       char *s, *p;
     232       p = $3;
     233       val = strtol(p, &s, 10);
     234       if ((val < 0) || (val > 65535)) {
     235           syntax_error("cost value \"%s\" is not between 0 and 65535", p);
     236       }
     237 }
     238 ;
     239
     240 filter: filter OP_OR filter_term
     241 | filter filter_term %prec OP_OR
     242 | filter_term
     243 ;
     244
     245 filter_term : filter_term OP_AND filter_factor
     246 | filter_factor
     247 ;
     248
     249 filter_factor :  OP_NOT filter_factor
     250 | '(' filter ')'
     251 | filter_operand
     252 ;
     253
     254 filter_operand: KEYW_ANY
     255 | '<' filter_aspath '>'
     256 | rp_attribute
     257 | TKN_FLTRNAME
     258 | filter_prefix
     259 ;
     260
     261 filter_prefix: filter_prefix_operand OP_MS
     262 |  filter_prefix_operand
     263 ;
     264
     265 filter_prefix_operand: TKN_ASNO
     266 | KEYW_PEERAS
     267 | TKN_ASNAME
     268 | TKN_RSNAME
     269 | '{' opt_filter_prefix_list '}'
     270 ;
     271
     272 opt_filter_prefix_list:
     273 | filter_prefix_list
     274 ;
     275
     276 filter_prefix_list: filter_prefix_list_prefix
     277 | filter_prefix_list ',' filter_prefix_list_prefix
     278 ;
     279
     280 filter_prefix_list_prefix: TKN_PRFXV4
     281 | TKN_PRFXV4RNG
     282 ;
     283
     284 filter_aspath: filter_aspath '|' filter_aspath_term
     285 | filter_aspath_term
     286 ;
     287
     288 filter_aspath_term: filter_aspath_term filter_aspath_closure
     289 | filter_aspath_closure
     290 ;
     291
     292 filter_aspath_closure: filter_aspath_closure '*'
     293 | filter_aspath_closure '?'
     294 | filter_aspath_closure '+'
     295 | filter_aspath_closure ASPATH_POSTFIX
     296 | filter_aspath_factor
     297 ;
     298
     299 filter_aspath_factor: '^'
     300 | '$'
     301 | '(' filter_aspath ')'
     302 | filter_aspath_no
     303 ;
     304
     305 filter_aspath_no: TKN_ASNO
     306 | KEYW_PEERAS
     307 | TKN_ASNAME
     308 | '.'
     309 | '[' filter_aspath_range ']'
     310 | '[' '^' filter_aspath_range ']'
     311 ;
     312
     313 filter_aspath_range:
     314 | filter_aspath_range TKN_ASNO
     315 | filter_aspath_range KEYW_PEERAS
     316 | filter_aspath_range '.'
     317 | filter_aspath_range TKN_ASNO '-' TKN_ASNO
     318 | filter_aspath_range TKN_ASNAME
     319 ;
     320
     321 %%
     322
     323 #undef importerror
     324 #undef yyerror
     325
     326 int
     327 importerror (const char *s)
         /* [<][>][^][v][top][bottom][index][help] */
     328 {
     329     yyerror(s);
     330     return 0;
     331 }
     332

/* [<][>][^][v][top][bottom][index][help] */