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