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