]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/iplang/iplang_y.y
This commit was generated by cvs2svn to compensate for changes in r153816,
[FreeBSD/FreeBSD.git] / contrib / ipfilter / iplang / iplang_y.y
1 /*      $FreeBSD$       */
2
3 %{
4 /*
5  * Copyright (C) 1997-1998 by Darren Reed.
6  *
7  * See the IPFILTER.LICENCE file for details on licencing.
8  *
9  * Id: iplang_y.y,v 2.9.2.2 2004/12/09 19:41:10 darrenr Exp
10  * $FreeBSD$
11  */
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <fcntl.h>
16 #if !defined(__SVR4) && !defined(__svr4__)
17 #include <strings.h>
18 #else
19 #include <sys/byteorder.h>
20 #endif
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/param.h>
24 #include <sys/time.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stddef.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netinet/in_systm.h>
31 #include <netinet/ip.h>
32 #ifndef linux
33 #include <netinet/ip_var.h>
34 #endif
35 #include <net/if.h>
36 #ifndef linux
37 #include <netinet/if_ether.h>
38 #endif
39 #include <netdb.h>
40 #include <arpa/nameser.h>
41 #include <arpa/inet.h>
42 #include <resolv.h>
43 #include <ctype.h>
44 #include "ipsend.h"
45 #include "ip_compat.h"
46 #include "ipf.h"
47 #include "iplang.h"
48
49 #if !defined(__NetBSD__) && (!defined(__FreeBSD_version) && \
50     __FreeBSD_version < 400020) && (!SOLARIS || SOLARIS2 < 10)
51 extern  struct ether_addr *ether_aton __P((char *));
52 #endif
53
54 extern  int     opts;
55 extern  struct ipopt_names ionames[];
56 extern  int     state, state, lineNum, token;
57 extern  int     yylineno;
58 extern  char    yytext[];
59 extern  FILE    *yyin;
60 int     yylex   __P((void));
61 #define YYDEBUG 1
62 #if !defined(ultrix) && !defined(hpux)
63 int     yydebug = 1;
64 #else
65 extern  int     yydebug;
66 #endif
67
68 iface_t *iflist = NULL, **iftail = &iflist;
69 iface_t *cifp = NULL;
70 arp_t *arplist = NULL, **arptail = &arplist, *carp = NULL;
71 struct in_addr defrouter;
72 send_t  sending;
73 char    *sclass = NULL;
74 u_short c_chksum __P((u_short *, u_int, u_long));
75 u_long  p_chksum __P((u_short *, u_int));
76
77 u_long  ipbuffer[67584/sizeof(u_long)];         /* 66K */
78 aniphdr_t       *aniphead = NULL, *canip = NULL, **aniptail = &aniphead;
79 ip_t            *ip = NULL;
80 udphdr_t        *udp = NULL;
81 tcphdr_t        *tcp = NULL;
82 icmphdr_t       *icmp = NULL;
83
84 struct statetoopt {
85         int     sto_st;
86         int     sto_op;
87 };
88
89 struct  in_addr getipv4addr __P((char *arg));
90 u_short getportnum __P((char *, char *));
91 struct  ether_addr *geteaddr __P((char *, struct ether_addr *));
92 void    *new_header __P((int));
93 void    free_aniplist __P((void));
94 void    inc_anipheaders __P((int));
95 void    new_data __P((void));
96 void    set_datalen __P((char **));
97 void    set_datafile __P((char **));
98 void    set_data __P((char **));
99 void    new_packet __P((void));
100 void    set_ipv4proto __P((char **));
101 void    set_ipv4src __P((char **));
102 void    set_ipv4dst __P((char **));
103 void    set_ipv4off __P((char **));
104 void    set_ipv4v __P((char **));
105 void    set_ipv4hl __P((char **));
106 void    set_ipv4ttl __P((char **));
107 void    set_ipv4tos __P((char **));
108 void    set_ipv4id __P((char **));
109 void    set_ipv4sum __P((char **));
110 void    set_ipv4len __P((char **));
111 void    new_tcpheader __P((void));
112 void    set_tcpsport __P((char **));
113 void    set_tcpdport __P((char **));
114 void    set_tcpseq __P((char **));
115 void    set_tcpack __P((char **));
116 void    set_tcpoff __P((char **));
117 void    set_tcpurp __P((char **));
118 void    set_tcpwin __P((char **));
119 void    set_tcpsum __P((char **));
120 void    set_tcpflags __P((char **));
121 void    set_tcpopt __P((int, char **));
122 void    end_tcpopt __P((void));
123 void    new_udpheader __P((void));
124 void    set_udplen __P((char **));
125 void    set_udpsum __P((char **));
126 void    prep_packet __P((void));
127 void    packet_done __P((void));
128 void    new_interface __P((void));
129 void    check_interface __P((void));
130 void    set_ifname __P((char **));
131 void    set_ifmtu __P((int));
132 void    set_ifv4addr __P((char **));
133 void    set_ifeaddr __P((char **));
134 void    new_arp __P((void));
135 void    set_arpeaddr __P((char **));
136 void    set_arpv4addr __P((char **));
137 void    reset_send __P((void));
138 void    set_sendif __P((char **));
139 void    set_sendvia __P((char **));
140 void    set_defaultrouter __P((char **));
141 void    new_icmpheader __P((void));
142 void    set_icmpcode __P((int));
143 void    set_icmptype __P((int));
144 void    set_icmpcodetok __P((char **));
145 void    set_icmptypetok __P((char **));
146 void    set_icmpid __P((int));
147 void    set_icmpseq __P((int));
148 void    set_icmpotime __P((int));
149 void    set_icmprtime __P((int));
150 void    set_icmpttime __P((int));
151 void    set_icmpmtu __P((int));
152 void    set_redir __P((int, char **));
153 void    new_ipv4opt __P((void));
154 void    set_icmppprob __P((int));
155 void    add_ipopt __P((int, void *));
156 void    end_ipopt __P((void));
157 void    set_secclass __P((char **));
158 void    free_anipheader __P((void));
159 void    end_ipv4 __P((void));
160 void    end_icmp __P((void));
161 void    end_udp __P((void));
162 void    end_tcp __P((void));
163 void    end_data __P((void));
164 void    yyerror __P((char *));
165 void    iplang __P((FILE *));
166 int     arp_getipv4 __P((char *, char *));
167 int     yyparse __P((void));
168 %}
169 %union {
170         char    *str;
171         int     num;
172 }
173 %token  <num> IL_NUMBER
174 %type   <num> number digits optnumber
175 %token  <str> IL_TOKEN
176 %type   <str> token optoken
177 %token  IL_HEXDIGIT IL_COLON IL_DOT IL_EOF IL_COMMENT
178 %token  IL_INTERFACE IL_IFNAME IL_MTU IL_EADDR
179 %token  IL_IPV4 IL_V4PROTO IL_V4SRC IL_V4DST IL_V4OFF IL_V4V IL_V4HL IL_V4TTL
180 %token  IL_V4TOS IL_V4SUM IL_V4LEN IL_V4OPT IL_V4ID
181 %token  IL_TCP IL_SPORT IL_DPORT IL_TCPFL IL_TCPSEQ IL_TCPACK IL_TCPOFF
182 %token  IL_TCPWIN IL_TCPSUM IL_TCPURP IL_TCPOPT IL_TCPO_NOP IL_TCPO_EOL
183 %token  IL_TCPO_MSS IL_TCPO_WSCALE IL_TCPO_TS
184 %token  IL_UDP IL_UDPLEN IL_UDPSUM
185 %token  IL_ICMP IL_ICMPTYPE IL_ICMPCODE
186 %token  IL_SEND IL_VIA
187 %token  IL_ARP
188 %token  IL_DEFROUTER
189 %token  IL_SUM IL_OFF IL_LEN IL_V4ADDR IL_OPT
190 %token  IL_DATA IL_DLEN IL_DVALUE IL_DFILE
191 %token  IL_IPO_NOP IL_IPO_RR IL_IPO_ZSU IL_IPO_MTUP IL_IPO_MTUR IL_IPO_EOL
192 %token  IL_IPO_TS IL_IPO_TR IL_IPO_SEC IL_IPO_LSRR IL_IPO_ESEC
193 %token  IL_IPO_SATID IL_IPO_SSRR IL_IPO_ADDEXT IL_IPO_VISA IL_IPO_IMITD
194 %token  IL_IPO_EIP IL_IPO_FINN IL_IPO_SECCLASS IL_IPO_CIPSO IL_IPO_ENCODE
195 %token  <str> IL_IPS_RESERV4 IL_IPS_TOPSECRET IL_IPS_SECRET IL_IPS_RESERV3
196 %token  <str> IL_IPS_CONFID IL_IPS_UNCLASS IL_IPS_RESERV2 IL_IPS_RESERV1
197 %token  IL_ICMP_ECHOREPLY IL_ICMP_UNREACH IL_ICMP_UNREACH_NET
198 %token  IL_ICMP_UNREACH_HOST IL_ICMP_UNREACH_PROTOCOL IL_ICMP_UNREACH_PORT
199 %token  IL_ICMP_UNREACH_NEEDFRAG IL_ICMP_UNREACH_SRCFAIL
200 %token  IL_ICMP_UNREACH_NET_UNKNOWN IL_ICMP_UNREACH_HOST_UNKNOWN
201 %token  IL_ICMP_UNREACH_ISOLATED IL_ICMP_UNREACH_NET_PROHIB
202 %token  IL_ICMP_UNREACH_HOST_PROHIB IL_ICMP_UNREACH_TOSNET
203 %token  IL_ICMP_UNREACH_TOSHOST IL_ICMP_UNREACH_FILTER_PROHIB
204 %token  IL_ICMP_UNREACH_HOST_PRECEDENCE IL_ICMP_UNREACH_PRECEDENCE_CUTOFF
205 %token  IL_ICMP_SOURCEQUENCH IL_ICMP_REDIRECT IL_ICMP_REDIRECT_NET
206 %token  IL_ICMP_REDIRECT_HOST IL_ICMP_REDIRECT_TOSNET
207 %token  IL_ICMP_REDIRECT_TOSHOST IL_ICMP_ECHO IL_ICMP_ROUTERADVERT
208 %token  IL_ICMP_ROUTERSOLICIT IL_ICMP_TIMXCEED IL_ICMP_TIMXCEED_INTRANS
209 %token  IL_ICMP_TIMXCEED_REASS IL_ICMP_PARAMPROB IL_ICMP_PARAMPROB_OPTABSENT
210 %token  IL_ICMP_TSTAMP IL_ICMP_TSTAMPREPLY IL_ICMP_IREQ IL_ICMP_IREQREPLY
211 %token  IL_ICMP_MASKREQ IL_ICMP_MASKREPLY IL_ICMP_SEQ IL_ICMP_ID
212 %token  IL_ICMP_OTIME IL_ICMP_RTIME IL_ICMP_TTIME
213
214 %%
215 file:   line
216         | line file
217         | IL_COMMENT
218         | IL_COMMENT file
219         ;
220
221 line:   iface
222         | arp
223         | send
224         | defrouter
225         | ipline
226         ;
227
228 iface:  ifhdr '{' ifaceopts '}' ';'     { check_interface(); }
229         ;
230
231 ifhdr:  IL_INTERFACE                    { new_interface(); }
232         ;
233
234 ifaceopts:
235         ifaceopt
236         | ifaceopt ifaceopts
237         ;
238
239 ifaceopt:
240         IL_IFNAME token                 { set_ifname(&$2); }
241         | IL_MTU number                 { set_ifmtu($2); }
242         | IL_V4ADDR token               { set_ifv4addr(&$2); }
243         | IL_EADDR token                { set_ifeaddr(&$2); }
244         ;
245
246 send:   sendhdr '{' sendbody '}' ';'    { packet_done(); }
247         | sendhdr ';'                   { packet_done(); }
248         ;
249
250 sendhdr:
251         IL_SEND                         { reset_send(); }
252         ;
253
254 sendbody:
255         sendopt
256         | sendbody sendopt
257         ;
258
259 sendopt:
260         IL_IFNAME token                 { set_sendif(&$2); }
261         | IL_VIA token                  { set_sendvia(&$2); }
262         ;
263
264 arp:    arphdr '{' arpbody '}' ';'
265         ;
266
267 arphdr: IL_ARP                          { new_arp(); }
268         ;
269
270 arpbody:
271         arpopt
272         | arpbody arpopt
273         ;
274
275 arpopt: IL_V4ADDR token                 { set_arpv4addr(&$2); }
276         | IL_EADDR token                { set_arpeaddr(&$2); }
277         ;
278
279 defrouter:
280         IL_DEFROUTER token              { set_defaultrouter(&$2); }
281         ;
282
283 bodyline:
284         ipline
285         | tcp tcpline
286         | udp udpline
287         | icmp icmpline
288         | data dataline
289         ;
290
291 ipline: ipv4 '{' ipv4body '}' ';'       { end_ipv4(); }
292         ;
293
294 ipv4:   IL_IPV4                         { new_packet(); }
295
296 ipv4body:
297         ipv4type
298         | ipv4type ipv4body
299         | bodyline
300         ;
301
302 ipv4type:
303         IL_V4PROTO token                { set_ipv4proto(&$2); }
304         | IL_V4SRC token                { set_ipv4src(&$2); }
305         | IL_V4DST token                { set_ipv4dst(&$2); }
306         | IL_V4OFF token                { set_ipv4off(&$2); }
307         | IL_V4V token                  { set_ipv4v(&$2); }
308         | IL_V4HL token                 { set_ipv4hl(&$2); }
309         | IL_V4ID token                 { set_ipv4id(&$2); }
310         | IL_V4TTL token                { set_ipv4ttl(&$2); }
311         | IL_V4TOS token                { set_ipv4tos(&$2); }
312         | IL_V4SUM token                { set_ipv4sum(&$2); }
313         | IL_V4LEN token                { set_ipv4len(&$2); }
314         | ipv4opt '{' ipv4optlist '}' ';'       { end_ipopt(); }
315         ;
316
317 tcp:    IL_TCP                          { new_tcpheader(); }
318         ;
319
320 tcpline:
321         '{' tcpheader '}' ';'           { end_tcp(); }
322         ;
323
324 tcpheader:
325         tcpbody
326         | tcpbody tcpheader
327         | bodyline
328         ;
329
330 tcpbody:
331         IL_SPORT token                  { set_tcpsport(&$2); }
332         | IL_DPORT token                { set_tcpdport(&$2); }
333         | IL_TCPSEQ token               { set_tcpseq(&$2); }
334         | IL_TCPACK token               { set_tcpack(&$2); }
335         | IL_TCPOFF token               { set_tcpoff(&$2); }
336         | IL_TCPURP token               { set_tcpurp(&$2); }
337         | IL_TCPWIN token               { set_tcpwin(&$2); }
338         | IL_TCPSUM token               { set_tcpsum(&$2); }
339         | IL_TCPFL token                { set_tcpflags(&$2); }
340         | IL_TCPOPT '{' tcpopts '}' ';' { end_tcpopt(); }
341         ;
342
343 tcpopts:
344         | tcpopt tcpopts
345         ;
346
347 tcpopt: IL_TCPO_NOP ';'                 { set_tcpopt(IL_TCPO_NOP, NULL); }
348         | IL_TCPO_EOL ';'               { set_tcpopt(IL_TCPO_EOL, NULL); }
349         | IL_TCPO_MSS optoken           { set_tcpopt(IL_TCPO_MSS,&$2);}
350         | IL_TCPO_WSCALE optoken        { set_tcpopt(IL_TCPO_WSCALE,&$2);}
351         | IL_TCPO_TS optoken            { set_tcpopt(IL_TCPO_TS, &$2);}
352         ;
353
354 udp:    IL_UDP                          { new_udpheader(); }
355         ;
356
357 udpline:
358         '{' udpheader '}' ';'           { end_udp(); }
359         ;
360
361
362 udpheader:
363         udpbody
364         | udpbody udpheader
365         | bodyline
366         ;
367
368 udpbody:
369         IL_SPORT token                  { set_tcpsport(&$2); }
370         | IL_DPORT token                { set_tcpdport(&$2); }
371         | IL_UDPLEN token               { set_udplen(&$2); }
372         | IL_UDPSUM token               { set_udpsum(&$2); }
373         ;
374
375 icmp:   IL_ICMP                         { new_icmpheader(); }
376         ;
377
378 icmpline:
379         '{' icmpbody '}' ';'            { end_icmp(); }
380         ;
381
382 icmpbody:
383         icmpheader
384         | icmpheader bodyline
385         ;
386
387 icmpheader:
388         IL_ICMPTYPE icmptype
389         | IL_ICMPTYPE icmptype icmpcode
390         ;
391
392 icmpcode:
393         IL_ICMPCODE token               { set_icmpcodetok(&$2); }
394         ;
395
396 icmptype:
397         IL_ICMP_ECHOREPLY ';'           { set_icmptype(ICMP_ECHOREPLY); }
398         | IL_ICMP_ECHOREPLY '{' icmpechoopts '}' ';'
399         | unreach
400         | IL_ICMP_SOURCEQUENCH ';'      { set_icmptype(ICMP_SOURCEQUENCH); }
401         | redirect
402         | IL_ICMP_ROUTERADVERT ';'      { set_icmptype(ICMP_ROUTERADVERT); }
403         | IL_ICMP_ROUTERSOLICIT ';'     { set_icmptype(ICMP_ROUTERSOLICIT); }
404         | IL_ICMP_ECHO ';'              { set_icmptype(ICMP_ECHO); }
405         | IL_ICMP_ECHO '{' icmpechoopts '}' ';'
406         | IL_ICMP_TIMXCEED ';'          { set_icmptype(ICMP_TIMXCEED); }
407         | IL_ICMP_TIMXCEED '{' exceed '}' ';'
408         | IL_ICMP_TSTAMP ';'            { set_icmptype(ICMP_TSTAMP); }
409         | IL_ICMP_TSTAMPREPLY ';'       { set_icmptype(ICMP_TSTAMPREPLY); }
410         | IL_ICMP_TSTAMPREPLY '{' icmptsopts '}' ';'
411         | IL_ICMP_IREQ ';'              { set_icmptype(ICMP_IREQ); }
412         | IL_ICMP_IREQREPLY ';'         { set_icmptype(ICMP_IREQREPLY); }
413         | IL_ICMP_IREQREPLY '{' data dataline '}' ';'
414         | IL_ICMP_MASKREQ ';'           { set_icmptype(ICMP_MASKREQ); }
415         | IL_ICMP_MASKREPLY ';'         { set_icmptype(ICMP_MASKREPLY); }
416         | IL_ICMP_MASKREPLY '{' token '}' ';'
417         | IL_ICMP_PARAMPROB ';'         { set_icmptype(ICMP_PARAMPROB); }
418         | IL_ICMP_PARAMPROB '{' paramprob '}' ';'
419         | IL_TOKEN ';'                  { set_icmptypetok(&$1); }
420         ;
421
422 icmpechoopts:
423         | icmpechoopts icmpecho
424         ;
425
426 icmpecho:
427         IL_ICMP_SEQ number              { set_icmpseq($2); }
428         | IL_ICMP_ID number             { set_icmpid($2); }
429         ;
430
431 icmptsopts:
432         | icmptsopts icmpts ';'
433         ;
434
435 icmpts: IL_ICMP_OTIME number            { set_icmpotime($2); }
436         | IL_ICMP_RTIME number          { set_icmprtime($2); }
437         | IL_ICMP_TTIME number          { set_icmpttime($2); }
438         ;
439
440 unreach:
441         IL_ICMP_UNREACH
442         | IL_ICMP_UNREACH '{' unreachopts '}' ';'
443         ;
444
445 unreachopts:
446         IL_ICMP_UNREACH_NET line
447         | IL_ICMP_UNREACH_HOST line
448         | IL_ICMP_UNREACH_PROTOCOL line
449         | IL_ICMP_UNREACH_PORT line
450         | IL_ICMP_UNREACH_NEEDFRAG number ';'   { set_icmpmtu($2); }
451         | IL_ICMP_UNREACH_SRCFAIL line
452         | IL_ICMP_UNREACH_NET_UNKNOWN line
453         | IL_ICMP_UNREACH_HOST_UNKNOWN line
454         | IL_ICMP_UNREACH_ISOLATED line
455         | IL_ICMP_UNREACH_NET_PROHIB line
456         | IL_ICMP_UNREACH_HOST_PROHIB line
457         | IL_ICMP_UNREACH_TOSNET line
458         | IL_ICMP_UNREACH_TOSHOST line
459         | IL_ICMP_UNREACH_FILTER_PROHIB line
460         | IL_ICMP_UNREACH_HOST_PRECEDENCE line
461         | IL_ICMP_UNREACH_PRECEDENCE_CUTOFF line
462         ;
463
464 redirect:
465         IL_ICMP_REDIRECT
466         | IL_ICMP_REDIRECT '{' redirectopts '}' ';'
467         ;
468
469 redirectopts:
470         | IL_ICMP_REDIRECT_NET token            { set_redir(0, &$2); }
471         | IL_ICMP_REDIRECT_HOST token           { set_redir(1, &$2); }
472         | IL_ICMP_REDIRECT_TOSNET token         { set_redir(2, &$2); }
473         | IL_ICMP_REDIRECT_TOSHOST token        { set_redir(3, &$2); }
474         ;
475
476 exceed:
477         IL_ICMP_TIMXCEED_INTRANS line
478         | IL_ICMP_TIMXCEED_REASS line
479         ;
480
481 paramprob:
482         IL_ICMP_PARAMPROB_OPTABSENT
483         | IL_ICMP_PARAMPROB_OPTABSENT paraprobarg
484
485 paraprobarg:
486         '{' number '}' ';'              { set_icmppprob($2); }
487         ;
488
489 ipv4opt:        IL_V4OPT                { new_ipv4opt(); }
490         ;
491
492 ipv4optlist:
493         | ipv4opts ipv4optlist
494         ;
495
496 ipv4opts:
497         IL_IPO_NOP ';'                  { add_ipopt(IL_IPO_NOP, NULL); }
498         | IL_IPO_RR optnumber           { add_ipopt(IL_IPO_RR, &$2); }
499         | IL_IPO_ZSU ';'                { add_ipopt(IL_IPO_ZSU, NULL); }
500         | IL_IPO_MTUP ';'               { add_ipopt(IL_IPO_MTUP, NULL); }
501         | IL_IPO_MTUR ';'               { add_ipopt(IL_IPO_MTUR, NULL); }
502         | IL_IPO_ENCODE ';'             { add_ipopt(IL_IPO_ENCODE, NULL); }
503         | IL_IPO_TS ';'                 { add_ipopt(IL_IPO_TS, NULL); }
504         | IL_IPO_TR ';'                 { add_ipopt(IL_IPO_TR, NULL); }
505         | IL_IPO_SEC ';'                { add_ipopt(IL_IPO_SEC, NULL); }
506         | IL_IPO_SECCLASS secclass      { add_ipopt(IL_IPO_SECCLASS, sclass); }
507         | IL_IPO_LSRR token             { add_ipopt(IL_IPO_LSRR,&$2); }
508         | IL_IPO_ESEC ';'               { add_ipopt(IL_IPO_ESEC, NULL); }
509         | IL_IPO_CIPSO ';'              { add_ipopt(IL_IPO_CIPSO, NULL); }
510         | IL_IPO_SATID optnumber        { add_ipopt(IL_IPO_SATID,&$2);}
511         | IL_IPO_SSRR token             { add_ipopt(IL_IPO_SSRR,&$2); }
512         | IL_IPO_ADDEXT ';'             { add_ipopt(IL_IPO_ADDEXT, NULL); }
513         | IL_IPO_VISA ';'               { add_ipopt(IL_IPO_VISA, NULL); }
514         | IL_IPO_IMITD ';'              { add_ipopt(IL_IPO_IMITD, NULL); }
515         | IL_IPO_EIP ';'                { add_ipopt(IL_IPO_EIP, NULL); }
516         | IL_IPO_FINN ';'               { add_ipopt(IL_IPO_FINN, NULL); }
517         ;
518
519 secclass:
520         IL_IPS_RESERV4 ';'              { set_secclass(&$1); }
521         | IL_IPS_TOPSECRET ';'          { set_secclass(&$1); }
522         | IL_IPS_SECRET ';'             { set_secclass(&$1); }
523         | IL_IPS_RESERV3 ';'            { set_secclass(&$1); }
524         | IL_IPS_CONFID ';'             { set_secclass(&$1); }
525         | IL_IPS_UNCLASS ';'            { set_secclass(&$1); }
526         | IL_IPS_RESERV2 ';'            { set_secclass(&$1); }
527         | IL_IPS_RESERV1 ';'            { set_secclass(&$1); }
528         ;
529
530 data:   IL_DATA                         { new_data(); }
531         ;
532
533 dataline:
534         '{' databody '}' ';'            { end_data(); }
535         ;
536
537 databody: dataopts
538         | dataopts databody
539         ;
540
541 dataopts:
542         IL_DLEN token                   { set_datalen(&$2); }
543         | IL_DVALUE token               { set_data(&$2); }
544         | IL_DFILE token                { set_datafile(&$2); }
545         ;
546
547 token: IL_TOKEN ';'
548         ;
549
550 optoken: ';'                            { $$ = ""; }
551         | token
552         ;
553
554 number: digits ';'
555         ;
556
557 optnumber: ';'                          { $$ = 0; }
558         | number
559         ;
560
561 digits: IL_NUMBER
562         | digits IL_NUMBER
563         ;
564 %%
565
566 struct  statetoopt      toipopts[] = {
567         { IL_IPO_NOP,           IPOPT_NOP },
568         { IL_IPO_RR,            IPOPT_RR },
569         { IL_IPO_ZSU,           IPOPT_ZSU },
570         { IL_IPO_MTUP,          IPOPT_MTUP },
571         { IL_IPO_MTUR,          IPOPT_MTUR },
572         { IL_IPO_ENCODE,        IPOPT_ENCODE },
573         { IL_IPO_TS,            IPOPT_TS },
574         { IL_IPO_TR,            IPOPT_TR },
575         { IL_IPO_SEC,           IPOPT_SECURITY },
576         { IL_IPO_SECCLASS,      IPOPT_SECURITY },
577         { IL_IPO_LSRR,          IPOPT_LSRR },
578         { IL_IPO_ESEC,          IPOPT_E_SEC },
579         { IL_IPO_CIPSO,         IPOPT_CIPSO },
580         { IL_IPO_SATID,         IPOPT_SATID },
581         { IL_IPO_SSRR,          IPOPT_SSRR },
582         { IL_IPO_ADDEXT,        IPOPT_ADDEXT },
583         { IL_IPO_VISA,          IPOPT_VISA },
584         { IL_IPO_IMITD,         IPOPT_IMITD },
585         { IL_IPO_EIP,           IPOPT_EIP },
586         { IL_IPO_FINN,          IPOPT_FINN },
587         { 0, 0 }
588 };
589
590 struct  statetoopt      tosecopts[] = {
591         { IL_IPS_RESERV4,       IPSO_CLASS_RES4 },
592         { IL_IPS_TOPSECRET,     IPSO_CLASS_TOPS },
593         { IL_IPS_SECRET,        IPSO_CLASS_SECR },
594         { IL_IPS_RESERV3,       IPSO_CLASS_RES3 },
595         { IL_IPS_CONFID,        IPSO_CLASS_CONF },
596         { IL_IPS_UNCLASS,       IPSO_CLASS_UNCL },
597         { IL_IPS_RESERV2,       IPSO_CLASS_RES2 },
598         { IL_IPS_RESERV1,       IPSO_CLASS_RES1 },
599         { 0, 0 }
600 };
601
602 #ifdef  bsdi
603 struct ether_addr *
604 ether_aton(s)
605         char *s;   
606 {
607         static struct ether_addr n;
608         u_int i[6];
609
610         if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
611             &i[2], &i[3], &i[4], &i[5]) == 6) {
612                 n.ether_addr_octet[0] = (u_char)i[0];
613                 n.ether_addr_octet[1] = (u_char)i[1];
614                 n.ether_addr_octet[2] = (u_char)i[2];
615                 n.ether_addr_octet[3] = (u_char)i[3];
616                 n.ether_addr_octet[4] = (u_char)i[4];
617                 n.ether_addr_octet[5] = (u_char)i[5];
618                 return &n;
619         }
620         return NULL;
621 }
622 #endif
623
624
625 struct in_addr getipv4addr(arg)
626 char *arg;
627 {
628         struct hostent *hp;
629         struct in_addr in;
630
631         in.s_addr = 0xffffffff;
632
633         if ((hp = gethostbyname(arg)))
634                 bcopy(hp->h_addr, &in.s_addr, sizeof(struct in_addr));
635         else
636                 in.s_addr = inet_addr(arg);
637         return in;
638 }
639
640
641 u_short getportnum(pr, name)
642 char *pr, *name;
643 {
644         struct servent *sp;
645
646         if (!(sp = getservbyname(name, pr)))
647                 return htons(atoi(name));
648         return sp->s_port;
649 }
650
651
652 struct ether_addr *geteaddr(arg, buf)
653 char *arg;
654 struct ether_addr *buf;
655 {
656         struct ether_addr *e;
657
658 #if !defined(hpux) && !defined(linux)
659         e = ether_aton(arg);
660         if (!e)
661                 fprintf(stderr, "Invalid ethernet address: %s\n", arg);
662         else
663 # ifdef __FreeBSD__
664                 bcopy(e->octet, buf->octet, sizeof(e->octet));
665 # else
666                 bcopy(e->ether_addr_octet, buf->ether_addr_octet,
667                       sizeof(e->ether_addr_octet));
668 # endif
669         return e;
670 #else
671         return NULL;
672 #endif
673 }
674
675
676 void *new_header(type)
677 int type;
678 {
679         aniphdr_t *aip, *oip = canip;
680         int     sz = 0;
681
682         aip = (aniphdr_t *)calloc(1, sizeof(*aip));
683         *aniptail = aip;
684         aniptail = &aip->ah_next;
685         aip->ah_p = type;
686         aip->ah_prev = oip;
687         canip = aip;
688
689         if (type == IPPROTO_UDP)
690                 sz = sizeof(udphdr_t);
691         else if (type == IPPROTO_TCP)
692                 sz = sizeof(tcphdr_t);
693         else if (type == IPPROTO_ICMP)
694                 sz = sizeof(icmphdr_t);
695         else if (type == IPPROTO_IP)
696                 sz = sizeof(ip_t);
697
698         if (oip)
699                 canip->ah_data = oip->ah_data + oip->ah_len;
700         else
701                 canip->ah_data = (char *)ipbuffer;
702
703         /*
704          * Increase the size fields in all wrapping headers.
705          */
706         for (aip = aniphead; aip; aip = aip->ah_next) {
707                 aip->ah_len += sz;
708                 if (aip->ah_p == IPPROTO_IP)
709                         aip->ah_ip->ip_len += sz;
710                 else if (aip->ah_p == IPPROTO_UDP)
711                         aip->ah_udp->uh_ulen += sz;
712         }
713         return (void *)canip->ah_data;
714 }
715
716
717 void free_aniplist()
718 {
719         aniphdr_t *aip, **aipp = &aniphead;
720
721         while ((aip = *aipp)) {
722                 *aipp = aip->ah_next;
723                 free(aip);
724         }
725         aniptail = &aniphead;
726 }
727
728
729 void inc_anipheaders(inc)
730 int inc;
731 {
732         aniphdr_t *aip;
733
734         for (aip = aniphead; aip; aip = aip->ah_next) {
735                 aip->ah_len += inc;
736                 if (aip->ah_p == IPPROTO_IP)
737                         aip->ah_ip->ip_len += inc;
738                 else if (aip->ah_p == IPPROTO_UDP)
739                         aip->ah_udp->uh_ulen += inc;
740         }
741 }
742
743
744 void new_data()
745 {
746         (void) new_header(-1);
747         canip->ah_len = 0;
748 }
749
750
751 void set_datalen(arg)
752 char **arg;
753 {
754         int     len;
755
756         len = strtol(*arg, NULL, 0);
757         inc_anipheaders(len);
758         free(*arg);
759         *arg = NULL;
760 }
761
762
763 void set_data(arg)
764 char **arg;
765 {
766         u_char *s = (u_char *)*arg, *t = (u_char *)canip->ah_data, c;
767         int len = 0, todo = 0, quote = 0, val = 0;
768
769         while ((c = *s++)) {
770                 if (todo) {
771                         if (ISDIGIT(c)) {
772                                 todo--;
773                                 if (c > '7') {
774                                         fprintf(stderr, "octal with %c!\n", c);
775                                         break;
776                                 }
777                                 val <<= 3;
778                                 val |= (c - '0');
779                         }
780                         if (!ISDIGIT(c) || !todo) {
781                                 *t++ = (u_char)(val & 0xff);
782                                 todo = 0;
783                         }
784                         if (todo)
785                                 continue;
786                 }
787                 if (quote) {
788                         if (ISDIGIT(c)) {
789                                 todo = 2;
790                                 if (c > '7') {
791                                         fprintf(stderr, "octal with %c!\n", c);
792                                         break;
793                                 }
794                                 val = (c - '0');
795                         } else {
796                                 switch (c)
797                                 {
798                                 case '\"' :
799                                         *t++ = '\"';
800                                         break;
801                                 case '\\' :
802                                         *t++ = '\\';
803                                         break;
804                                 case 'n' :
805                                         *t++ = '\n';
806                                         break;
807                                 case 'r' :
808                                         *t++ = '\r';
809                                         break;
810                                 case 't' :
811                                         *t++ = '\t';
812                                         break;
813                                 }
814                         }
815                         quote = 0;
816                         continue;
817                 }
818
819                 if (c == '\\')
820                         quote = 1;
821                 else
822                         *t++ = c;
823         }
824         if (todo)
825                 *t++ = (u_char)(val & 0xff);
826         if (quote)
827                 *t++ = '\\';
828         len = t - (u_char *)canip->ah_data;
829         inc_anipheaders(len - canip->ah_len);
830         canip->ah_len = len;
831 }
832
833
834 void set_datafile(arg)
835 char **arg;
836 {
837         struct stat sb;
838         char *file = *arg;
839         int fd, len;
840
841         if ((fd = open(file, O_RDONLY)) == -1) {
842                 perror("open");
843                 exit(-1);
844         }
845
846         if (fstat(fd, &sb) == -1) {
847                 perror("fstat");
848                 exit(-1);
849         }
850
851         if ((sb.st_size + aniphead->ah_len ) > 65535) {
852                 fprintf(stderr, "data file %s too big to include.\n", file);
853                 close(fd);
854                 return;
855         }
856         if ((len = read(fd, canip->ah_data, sb.st_size)) == -1) {
857                 perror("read");
858                 close(fd);
859                 return;
860         }
861         inc_anipheaders(len);
862         canip->ah_len += len;
863         close(fd);
864 }
865
866
867 void new_packet()
868 {
869         static  u_short id = 0;
870
871         if (!aniphead)
872                 bzero((char *)ipbuffer, sizeof(ipbuffer));
873
874         ip = (ip_t *)new_header(IPPROTO_IP);
875         ip->ip_v = IPVERSION;
876         ip->ip_hl = sizeof(ip_t) >> 2;
877         ip->ip_len = sizeof(ip_t);
878         ip->ip_ttl = 63;
879         ip->ip_id = htons(id++);
880 }
881
882
883 void set_ipv4proto(arg)
884 char **arg;
885 {
886         struct protoent *pr;
887
888         if ((pr = getprotobyname(*arg)))
889                 ip->ip_p = pr->p_proto;
890         else
891                 if (!(ip->ip_p = atoi(*arg)))
892                         fprintf(stderr, "unknown protocol %s\n", *arg);
893         free(*arg);
894         *arg = NULL;
895 }
896
897
898 void set_ipv4src(arg)
899 char **arg;
900 {
901         ip->ip_src = getipv4addr(*arg);
902         free(*arg);
903         *arg = NULL;
904 }
905
906
907 void set_ipv4dst(arg)
908 char **arg;
909 {
910         ip->ip_dst = getipv4addr(*arg);
911         free(*arg);
912         *arg = NULL;
913 }
914
915
916 void set_ipv4off(arg)
917 char **arg;
918 {
919         ip->ip_off = htons(strtol(*arg, NULL, 0));
920         free(*arg);
921         *arg = NULL;
922 }
923
924
925 void set_ipv4v(arg)
926 char **arg;
927 {
928         ip->ip_v = strtol(*arg, NULL, 0);
929         free(*arg);
930         *arg = NULL;
931 }
932
933
934 void set_ipv4hl(arg)
935 char **arg;
936 {
937         int newhl, inc;
938
939         newhl = strtol(*arg, NULL, 0);
940         inc = (newhl - ip->ip_hl) << 2;
941         ip->ip_len += inc;
942         ip->ip_hl = newhl;
943         canip->ah_len += inc;
944         free(*arg);
945         *arg = NULL;
946 }
947
948
949 void set_ipv4ttl(arg)
950 char **arg;
951 {
952         ip->ip_ttl = strtol(*arg, NULL, 0);
953         free(*arg);
954         *arg = NULL;
955 }
956
957
958 void set_ipv4tos(arg)
959 char **arg;
960 {
961         ip->ip_tos = strtol(*arg, NULL, 0);
962         free(*arg);
963         *arg = NULL;
964 }
965
966
967 void set_ipv4id(arg)
968 char **arg;
969 {
970         ip->ip_id = htons(strtol(*arg, NULL, 0));
971         free(*arg);
972         *arg = NULL;
973 }
974
975
976 void set_ipv4sum(arg)
977 char **arg;
978 {
979         ip->ip_sum = strtol(*arg, NULL, 0);
980         free(*arg);
981         *arg = NULL;
982 }
983
984
985 void set_ipv4len(arg)
986 char **arg;
987 {
988         int len;
989
990         len = strtol(*arg, NULL, 0);
991         inc_anipheaders(len - ip->ip_len);
992         ip->ip_len = len;
993         free(*arg);
994         *arg = NULL;
995 }
996
997
998 void new_tcpheader()
999 {
1000
1001         if ((ip->ip_p) && (ip->ip_p != IPPROTO_TCP)) {
1002                 fprintf(stderr, "protocol %d specified with TCP!\n", ip->ip_p);
1003                 return;
1004         }
1005         ip->ip_p = IPPROTO_TCP;
1006
1007         tcp = (tcphdr_t *)new_header(IPPROTO_TCP);
1008         tcp->th_win = htons(4096);
1009         tcp->th_off = sizeof(*tcp) >> 2;
1010 }
1011
1012
1013 void set_tcpsport(arg)
1014 char **arg;
1015 {
1016         u_short *port;
1017         char *pr;
1018
1019         if (ip->ip_p == IPPROTO_UDP) {
1020                 port = &udp->uh_sport;
1021                 pr = "udp";
1022         } else {
1023                 port = &tcp->th_sport;
1024                 pr = "udp";
1025         }
1026
1027         *port = getportnum(pr, *arg);
1028         free(*arg);
1029         *arg = NULL;
1030 }
1031
1032
1033 void set_tcpdport(arg)
1034 char **arg;
1035 {
1036         u_short *port;
1037         char *pr;
1038
1039         if (ip->ip_p == IPPROTO_UDP) {
1040                 port = &udp->uh_dport;
1041                 pr = "udp";
1042         } else {
1043                 port = &tcp->th_dport;
1044                 pr = "udp";
1045         }
1046
1047         *port = getportnum(pr, *arg);
1048         free(*arg);
1049         *arg = NULL;
1050 }
1051
1052
1053 void set_tcpseq(arg)
1054 char **arg;
1055 {
1056         tcp->th_seq = htonl(strtol(*arg, NULL, 0));
1057         free(*arg);
1058         *arg = NULL;
1059 }
1060
1061
1062 void set_tcpack(arg)
1063 char **arg;
1064 {
1065         tcp->th_ack = htonl(strtol(*arg, NULL, 0));
1066         free(*arg);
1067         *arg = NULL;
1068 }
1069
1070
1071 void set_tcpoff(arg)
1072 char **arg;
1073 {
1074         int     off;
1075
1076         off = strtol(*arg, NULL, 0);
1077         inc_anipheaders((off - tcp->th_off) << 2);
1078         tcp->th_off = off;
1079         free(*arg);
1080         *arg = NULL;
1081 }
1082
1083
1084 void set_tcpurp(arg)
1085 char **arg;
1086 {
1087         tcp->th_urp = htons(strtol(*arg, NULL, 0));
1088         free(*arg);
1089         *arg = NULL;
1090 }
1091
1092
1093 void set_tcpwin(arg)
1094 char **arg;
1095 {
1096         tcp->th_win = htons(strtol(*arg, NULL, 0));
1097         free(*arg);
1098         *arg = NULL;
1099 }
1100
1101
1102 void set_tcpsum(arg)
1103 char **arg;
1104 {
1105         tcp->th_sum = strtol(*arg, NULL, 0);
1106         free(*arg);
1107         *arg = NULL;
1108 }
1109
1110
1111 void set_tcpflags(arg)
1112 char **arg;
1113 {
1114         static  char    flags[] = "ASURPF";
1115         static  int     flagv[] = { TH_ACK, TH_SYN, TH_URG, TH_RST, TH_PUSH,
1116                                     TH_FIN } ;
1117         char *s, *t;
1118
1119         for (s = *arg; *s; s++)
1120                 if (!(t = strchr(flags, *s))) {
1121                         if (s - *arg) {
1122                                 fprintf(stderr, "unknown TCP flag %c\n", *s);
1123                                 break;
1124                         }
1125                         tcp->th_flags = strtol(*arg, NULL, 0);
1126                         break;
1127                 } else
1128                         tcp->th_flags |= flagv[t - flags];
1129         free(*arg);
1130         *arg = NULL;
1131 }
1132
1133
1134 void set_tcpopt(state, arg)
1135 int state;
1136 char **arg;
1137 {
1138         u_char *s;
1139         int val, len, val2, pad, optval;
1140
1141         if (arg && *arg)
1142                 val = atoi(*arg);
1143         else
1144                 val = 0;
1145
1146         s = (u_char *)tcp + sizeof(*tcp) + canip->ah_optlen;
1147         switch (state)
1148         {
1149         case IL_TCPO_EOL :
1150                 optval = 0;
1151                 len = 1;
1152                 break;
1153         case IL_TCPO_NOP :
1154                 optval = 1;
1155                 len = 1;
1156                 break;
1157         case IL_TCPO_MSS :
1158                 optval = 2;
1159                 len = 4;
1160                 break;
1161         case IL_TCPO_WSCALE :
1162                 optval = 3;
1163                 len = 3;
1164                 break;
1165         case IL_TCPO_TS :
1166                 optval = 8;
1167                 len = 10;
1168                 break;
1169         default :
1170                 optval = 0;
1171                 len = 0;
1172                 break;
1173         }
1174
1175         if (len > 1) {
1176                 /*
1177                  * prepend padding - if required.
1178                  */
1179                 if (len & 3)
1180                         for (pad = 4 - (len & 3); pad; pad--) {
1181                                 *s++ = 1;
1182                                 canip->ah_optlen++;
1183                         }
1184                 /*
1185                  * build tcp option
1186                  */
1187                 *s++ = (u_char)optval;
1188                 *s++ = (u_char)len;
1189                 if (len > 2) {
1190                         if (len == 3) {         /* 1 byte - char */
1191                                 *s++ = (u_char)val;
1192                         } else if (len == 4) {  /* 2 bytes - short */
1193                                 *s++ = (u_char)((val >> 8) & 0xff);
1194                                 *s++ = (u_char)(val & 0xff);
1195                         } else if (len >= 6) {  /* 4 bytes - long */
1196                                 val2 = htonl(val);
1197                                 bcopy((char *)&val2, s, 4);
1198                         }
1199                         s += (len - 2);
1200                 }
1201         } else
1202                 *s++ = (u_char)optval;
1203
1204         canip->ah_lastopt = optval;
1205         canip->ah_optlen += len;
1206
1207         if (arg && *arg) {
1208                 free(*arg);
1209                 *arg = NULL;
1210         }
1211 }
1212
1213
1214 void end_tcpopt()
1215 {
1216         int pad;
1217         char *s = (char *)tcp;
1218
1219         s += sizeof(*tcp) + canip->ah_optlen;
1220         /*
1221          * pad out so that we have a multiple of 4 bytes in size fo the
1222          * options.  make sure last byte is EOL.
1223          */
1224         if (canip->ah_optlen & 3) {
1225                 if (canip->ah_lastopt != 1) {
1226                         for (pad = 3 - (canip->ah_optlen & 3); pad; pad--) {
1227                                 *s++ = 1;
1228                                 canip->ah_optlen++;
1229                         }
1230                         canip->ah_optlen++;
1231                 } else {
1232                         s -= 1;
1233
1234                         for (pad = 3 - (canip->ah_optlen & 3); pad; pad--) {
1235                                 *s++ = 1;
1236                                 canip->ah_optlen++;
1237                         }
1238                 }
1239                 *s++ = 0;
1240         }
1241         tcp->th_off = (sizeof(*tcp) + canip->ah_optlen) >> 2;
1242         inc_anipheaders(canip->ah_optlen);
1243 }
1244
1245
1246 void new_udpheader()
1247 {
1248         if ((ip->ip_p) && (ip->ip_p != IPPROTO_UDP)) {
1249                 fprintf(stderr, "protocol %d specified with UDP!\n", ip->ip_p);
1250                 return;
1251         }
1252         ip->ip_p = IPPROTO_UDP;
1253
1254         udp = (udphdr_t *)new_header(IPPROTO_UDP);
1255         udp->uh_ulen = sizeof(*udp);
1256 }
1257
1258
1259 void set_udplen(arg)
1260 char **arg;
1261 {
1262         int len;
1263
1264         len = strtol(*arg, NULL, 0);
1265         inc_anipheaders(len - udp->uh_ulen);
1266         udp->uh_ulen = len;
1267         free(*arg);
1268         *arg = NULL;
1269 }
1270
1271
1272 void set_udpsum(arg)
1273 char **arg;
1274 {
1275         udp->uh_sum = strtol(*arg, NULL, 0);
1276         free(*arg);
1277         *arg = NULL;
1278 }
1279
1280
1281 void prep_packet()
1282 {
1283         iface_t *ifp;
1284         struct in_addr gwip;
1285
1286         ifp = sending.snd_if;
1287         if (!ifp) {
1288                 fprintf(stderr, "no interface defined for sending!\n");
1289                 return;
1290         }
1291         if (ifp->if_fd == -1)
1292                 ifp->if_fd = initdevice(ifp->if_name, 5);
1293         gwip = sending.snd_gw;
1294         if (!gwip.s_addr)
1295                 gwip = aniphead->ah_ip->ip_dst;
1296         (void) send_ip(ifp->if_fd, ifp->if_MTU, (ip_t *)ipbuffer, gwip, 2);
1297 }
1298
1299
1300 void packet_done()
1301 {
1302         char    outline[80];
1303         int     i, j, k;
1304         u_char  *s = (u_char *)ipbuffer, *t = (u_char *)outline;
1305
1306         if (opts & OPT_VERBOSE) {
1307                 ip->ip_len = htons(ip->ip_len);
1308                 for (i = ntohs(ip->ip_len), j = 0; i; i--, j++, s++) {
1309                         if (j && !(j & 0xf)) {
1310                                 *t++ = '\n';
1311                                 *t = '\0';
1312                                 fputs(outline, stdout);
1313                                 fflush(stdout);
1314                                 t = (u_char *)outline;
1315                                 *t = '\0';
1316                         }
1317                         sprintf((char *)t, "%02x", *s & 0xff);
1318                         t += 2;
1319                         if (!((j + 1) & 0xf)) {
1320                                 s -= 15;
1321                                 sprintf((char *)t, "    ");
1322                                 t += 8;
1323                                 for (k = 16; k; k--, s++)
1324                                         *t++ = (ISPRINT(*s) ? *s : '.');
1325                                 s--;
1326                         }
1327
1328                         if ((j + 1) & 0xf)
1329                                 *t++ = ' ';;
1330                 }
1331
1332                 if (j & 0xf) {
1333                         for (k = 16 - (j & 0xf); k; k--) {
1334                                 *t++ = ' ';
1335                                 *t++ = ' ';
1336                                 *t++ = ' ';
1337                         }
1338                         sprintf((char *)t, "       ");
1339                         t += 7;
1340                         s -= j & 0xf;
1341                         for (k = j & 0xf; k; k--, s++)
1342                                 *t++ = (ISPRINT(*s) ? *s : '.');
1343                         *t++ = '\n';
1344                         *t = '\0';
1345                 }
1346                 fputs(outline, stdout);
1347                 fflush(stdout);
1348                 ip->ip_len = ntohs(ip->ip_len);
1349         }
1350
1351         prep_packet();
1352         free_aniplist();
1353 }
1354
1355
1356 void new_interface()
1357 {
1358         cifp = (iface_t *)calloc(1, sizeof(iface_t));
1359         *iftail = cifp;
1360         iftail = &cifp->if_next;
1361         cifp->if_fd = -1;
1362 }
1363
1364
1365 void check_interface()
1366 {
1367         if (!cifp->if_name || !*cifp->if_name)
1368                 fprintf(stderr, "No interface name given!\n");
1369         if (!cifp->if_MTU || !*cifp->if_name)
1370                 fprintf(stderr, "Interface %s has an MTU of 0!\n",
1371                         cifp->if_name);
1372 }
1373
1374
1375 void set_ifname(arg)
1376 char **arg;
1377 {
1378         cifp->if_name = *arg;
1379         *arg = NULL;
1380 }
1381
1382
1383 void set_ifmtu(arg)
1384 int arg;
1385 {
1386         cifp->if_MTU = arg;
1387 }
1388
1389
1390 void set_ifv4addr(arg)
1391 char **arg;
1392 {
1393         cifp->if_addr = getipv4addr(*arg);
1394         free(*arg);
1395         *arg = NULL;
1396 }
1397
1398
1399 void set_ifeaddr(arg)
1400 char **arg;
1401 {
1402         (void) geteaddr(*arg, &cifp->if_eaddr);
1403         free(*arg);
1404         *arg = NULL;
1405 }
1406
1407
1408 void new_arp()
1409 {
1410         carp = (arp_t *)calloc(1, sizeof(arp_t));
1411         *arptail = carp;
1412         arptail = &carp->arp_next;
1413 }
1414
1415
1416 void set_arpeaddr(arg)
1417 char **arg;
1418 {
1419         (void) geteaddr(*arg, &carp->arp_eaddr);
1420         free(*arg);
1421         *arg = NULL;
1422 }
1423
1424
1425 void set_arpv4addr(arg)
1426 char **arg;
1427 {
1428         carp->arp_addr = getipv4addr(*arg);
1429         free(*arg);
1430         *arg = NULL;
1431 }
1432
1433
1434 int arp_getipv4(ip, addr)
1435 char *ip;
1436 char *addr;
1437 {
1438         arp_t *a;
1439
1440         for (a = arplist; a; a = a->arp_next)
1441                 if (!bcmp(ip, (char *)&a->arp_addr, 4)) {
1442                         bcopy((char *)&a->arp_eaddr, addr, 6);
1443                         return 0;
1444                 }
1445         return -1;
1446 }
1447
1448
1449 void reset_send()
1450 {
1451         sending.snd_if = iflist;
1452         sending.snd_gw = defrouter;
1453 }
1454
1455
1456 void set_sendif(arg)
1457 char **arg;
1458 {
1459         iface_t *ifp;
1460
1461         for (ifp = iflist; ifp; ifp = ifp->if_next)
1462                 if (ifp->if_name && !strcmp(ifp->if_name, *arg))
1463                         break;
1464         sending.snd_if = ifp;
1465         if (!ifp)
1466                 fprintf(stderr, "couldn't find interface %s\n", *arg);
1467         free(*arg);
1468         *arg = NULL;
1469 }
1470
1471
1472 void set_sendvia(arg)
1473 char **arg;
1474 {
1475         sending.snd_gw = getipv4addr(*arg);
1476         free(*arg);
1477         *arg = NULL;
1478 }
1479
1480
1481 void set_defaultrouter(arg)
1482 char **arg;
1483 {
1484         defrouter = getipv4addr(*arg);
1485         free(*arg);
1486         *arg = NULL;
1487 }
1488
1489
1490 void new_icmpheader()
1491 {
1492         if ((ip->ip_p) && (ip->ip_p != IPPROTO_ICMP)) {
1493                 fprintf(stderr, "protocol %d specified with ICMP!\n",
1494                         ip->ip_p);
1495                 return;
1496         }
1497         ip->ip_p = IPPROTO_ICMP;
1498         icmp = (icmphdr_t *)new_header(IPPROTO_ICMP);
1499 }
1500
1501
1502 void set_icmpcode(code)
1503 int code;
1504 {
1505         icmp->icmp_code = code;
1506 }
1507
1508
1509 void set_icmptype(type)
1510 int type;
1511 {
1512         icmp->icmp_type = type;
1513 }
1514
1515
1516 void set_icmpcodetok(code)
1517 char **code;
1518 {
1519         char    *s;
1520         int     i;
1521
1522         for (i = 0; (s = icmpcodes[i]); i++)
1523                 if (!strcmp(s, *code)) {
1524                         icmp->icmp_code = i;
1525                         break;
1526                 }
1527         if (!s)
1528                 fprintf(stderr, "unknown ICMP code %s\n", *code);
1529         free(*code);
1530         *code = NULL;
1531 }
1532
1533
1534 void set_icmptypetok(type)
1535 char **type;
1536 {
1537         char    *s;
1538         int     i, done = 0;
1539
1540         for (i = 0; !(s = icmptypes[i]) || strcmp(s, "END"); i++)
1541                 if (s && !strcmp(s, *type)) {
1542                         icmp->icmp_type = i;
1543                         done = 1;
1544                         break;
1545                 }
1546         if (!done)
1547                 fprintf(stderr, "unknown ICMP type %s\n", *type);
1548         free(*type);
1549         *type = NULL;
1550 }
1551
1552
1553 void set_icmpid(arg)
1554 int arg;
1555 {
1556         icmp->icmp_id = htons(arg);
1557 }
1558
1559
1560 void set_icmpseq(arg)
1561 int arg;
1562 {
1563         icmp->icmp_seq = htons(arg);
1564 }
1565
1566
1567 void set_icmpotime(arg)
1568 int arg;
1569 {
1570         icmp->icmp_otime = htonl(arg);
1571 }
1572
1573
1574 void set_icmprtime(arg)
1575 int arg;
1576 {
1577         icmp->icmp_rtime = htonl(arg);
1578 }
1579
1580
1581 void set_icmpttime(arg)
1582 int arg;
1583 {
1584         icmp->icmp_ttime = htonl(arg);
1585 }
1586
1587
1588 void set_icmpmtu(arg)
1589 int arg;
1590 {
1591 #if     BSD >= 199306
1592         icmp->icmp_nextmtu = htons(arg);
1593 #endif
1594 }
1595
1596
1597 void set_redir(redir, arg)
1598 int redir;
1599 char **arg;
1600 {
1601         icmp->icmp_code = redir;
1602         icmp->icmp_gwaddr = getipv4addr(*arg);
1603         free(*arg);
1604         *arg = NULL;
1605 }
1606
1607
1608 void set_icmppprob(num)
1609 int num;
1610 {
1611         icmp->icmp_pptr = num;
1612 }
1613
1614
1615 void new_ipv4opt()
1616 {
1617         new_header(-2);
1618 }
1619
1620
1621 void add_ipopt(state, ptr)
1622 int state;
1623 void *ptr;
1624 {
1625         struct ipopt_names *io;
1626         struct statetoopt *sto;
1627         char numbuf[16], *arg, **param = ptr;
1628         int inc, hlen;
1629
1630         if (state == IL_IPO_RR || state == IL_IPO_SATID) {
1631                 if (param)
1632                         sprintf(numbuf, "%d", *(int *)param);
1633                 else
1634                         strcpy(numbuf, "0");
1635                 arg = numbuf;
1636         } else
1637                 arg = param ? *param : NULL;
1638
1639         if (canip->ah_next) {
1640                 fprintf(stderr, "cannot specify options after data body\n");
1641                 return;
1642         }
1643         for (sto = toipopts; sto->sto_st; sto++)
1644                 if (sto->sto_st == state)
1645                         break;
1646         if (!sto || !sto->sto_st) {
1647                 fprintf(stderr, "No mapping for state %d to IP option\n",
1648                         state);
1649                 return;
1650         }
1651
1652         hlen = sizeof(ip_t) + canip->ah_optlen;
1653         for (io = ionames; io->on_name; io++)
1654                 if (io->on_value == sto->sto_op)
1655                         break;
1656         canip->ah_lastopt = io->on_value;
1657
1658         if (io->on_name) {
1659                 inc = addipopt((char *)ip + hlen, io, hlen - sizeof(ip_t),arg);
1660                 if (inc > 0) {
1661                         while (inc & 3) {
1662                                 ((char *)ip)[sizeof(*ip) + inc] = IPOPT_NOP;
1663                                 canip->ah_lastopt = IPOPT_NOP;
1664                                 inc++;
1665                         }
1666                         hlen += inc;
1667                 }
1668         }
1669
1670         canip->ah_optlen = hlen - sizeof(ip_t);
1671
1672         if (state != IL_IPO_RR && state != IL_IPO_SATID)
1673                 if (param && *param) {
1674                         free(*param);
1675                         *param = NULL;
1676                 }
1677         sclass = NULL;
1678 }
1679
1680
1681 void end_ipopt()
1682 {
1683         int pad;
1684         char *s, *buf = (char *)ip;
1685
1686         /*
1687          * pad out so that we have a multiple of 4 bytes in size fo the
1688          * options.  make sure last byte is EOL.
1689          */
1690         if (canip->ah_lastopt == IPOPT_NOP) {
1691                 buf[sizeof(*ip) + canip->ah_optlen - 1] = IPOPT_EOL;
1692         } else if (canip->ah_lastopt != IPOPT_EOL) {
1693                 s = buf + sizeof(*ip) + canip->ah_optlen;
1694
1695                 for (pad = 3 - (canip->ah_optlen & 3); pad; pad--) {
1696                         *s++ = IPOPT_NOP;
1697                         *s = IPOPT_EOL;
1698                         canip->ah_optlen++;
1699                 }
1700                 canip->ah_optlen++;
1701         } else {
1702                 s = buf + sizeof(*ip) + canip->ah_optlen - 1;
1703
1704                 for (pad = 3 - (canip->ah_optlen & 3); pad; pad--) {
1705                         *s++ = IPOPT_NOP;
1706                         *s = IPOPT_EOL;
1707                         canip->ah_optlen++;
1708                 }
1709         }
1710         ip->ip_hl = (sizeof(*ip) + canip->ah_optlen) >> 2;
1711         inc_anipheaders(canip->ah_optlen);
1712         free_anipheader();
1713 }
1714
1715
1716 void set_secclass(arg)
1717 char **arg;
1718 {
1719         sclass = *arg;
1720         *arg = NULL;
1721 }
1722
1723
1724 void free_anipheader()
1725 {
1726         aniphdr_t *aip;
1727
1728         aip = canip;
1729         if ((canip = aip->ah_prev)) {
1730                 canip->ah_next = NULL;
1731                 aniptail = &canip->ah_next;
1732         }
1733
1734         if (canip)
1735                 free(aip);
1736 }
1737
1738
1739 void end_ipv4()
1740 {
1741         aniphdr_t *aip;
1742
1743         ip->ip_sum = 0;
1744         ip->ip_len = htons(ip->ip_len);
1745         ip->ip_sum = chksum((u_short *)ip, ip->ip_hl << 2);
1746         ip->ip_len = ntohs(ip->ip_len);
1747         free_anipheader();
1748         for (aip = aniphead, ip = NULL; aip; aip = aip->ah_next)
1749                 if (aip->ah_p == IPPROTO_IP)
1750                         ip = aip->ah_ip;
1751 }
1752
1753
1754 void end_icmp()
1755 {
1756         aniphdr_t *aip;
1757
1758         icmp->icmp_cksum = 0;
1759         icmp->icmp_cksum = chksum((u_short *)icmp, canip->ah_len);
1760         free_anipheader();
1761         for (aip = aniphead, icmp = NULL; aip; aip = aip->ah_next)
1762                 if (aip->ah_p == IPPROTO_ICMP)
1763                         icmp = aip->ah_icmp;
1764 }
1765
1766
1767 void end_udp()
1768 {
1769         u_long  sum;
1770         aniphdr_t *aip;
1771         ip_t    iptmp;
1772
1773         bzero((char *)&iptmp, sizeof(iptmp));
1774         iptmp.ip_p = ip->ip_p;
1775         iptmp.ip_src = ip->ip_src;
1776         iptmp.ip_dst = ip->ip_dst;
1777         iptmp.ip_len = htons(ip->ip_len - (ip->ip_hl << 2));
1778         sum = p_chksum((u_short *)&iptmp, (u_int)sizeof(iptmp));
1779         udp->uh_ulen = htons(udp->uh_ulen);
1780         udp->uh_sum = c_chksum((u_short *)udp, (u_int)ntohs(iptmp.ip_len), sum);
1781         free_anipheader();
1782         for (aip = aniphead, udp = NULL; aip; aip = aip->ah_next)
1783                 if (aip->ah_p == IPPROTO_UDP)
1784                         udp = aip->ah_udp;
1785 }
1786
1787
1788 void end_tcp()
1789 {
1790         u_long  sum;
1791         aniphdr_t *aip;
1792         ip_t    iptmp;
1793
1794         bzero((char *)&iptmp, sizeof(iptmp));
1795         iptmp.ip_p = ip->ip_p;
1796         iptmp.ip_src = ip->ip_src;
1797         iptmp.ip_dst = ip->ip_dst;
1798         iptmp.ip_len = htons(ip->ip_len - (ip->ip_hl << 2));
1799         sum = p_chksum((u_short *)&iptmp, (u_int)sizeof(iptmp));
1800         tcp->th_sum = 0;
1801         tcp->th_sum = c_chksum((u_short *)tcp, (u_int)ntohs(iptmp.ip_len), sum);
1802         free_anipheader();
1803         for (aip = aniphead, tcp = NULL; aip; aip = aip->ah_next)
1804                 if (aip->ah_p == IPPROTO_TCP)
1805                         tcp = aip->ah_tcp;
1806 }
1807
1808
1809 void end_data()
1810 {
1811         free_anipheader();
1812 }
1813
1814
1815 void iplang(fp)
1816 FILE *fp;
1817 {
1818         yyin = fp;
1819
1820         yydebug = (opts & OPT_DEBUG) ? 1 : 0;
1821
1822         while (!feof(fp))
1823                 yyparse();
1824 }
1825
1826
1827 u_short c_chksum(buf, len, init)
1828 u_short *buf;
1829 u_int   len;
1830 u_long  init;
1831 {
1832         u_long  sum = init;
1833         int     nwords = len >> 1;
1834  
1835         for(; nwords > 0; nwords--)
1836                 sum += *buf++;
1837         sum = (sum>>16) + (sum & 0xffff);
1838         sum += (sum >>16);
1839         return (~sum);
1840 }
1841
1842
1843 u_long  p_chksum(buf,len)
1844 u_short *buf;
1845 u_int   len;
1846 {
1847         u_long  sum = 0;
1848         int     nwords = len >> 1;
1849  
1850         for(; nwords > 0; nwords--)
1851                 sum += *buf++;
1852         return sum;
1853 }