ipfw2.c revision 248505
1173095Srwatson/* 2189797Srwatson * Copyright (c) 2002-2003 Luigi Rizzo 3173095Srwatson * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp 4173095Srwatson * Copyright (c) 1994 Ugen J.S.Antsilevich 5173095Srwatson * 6173095Srwatson * Idea and grammar partially left from: 7189797Srwatson * Copyright (c) 1993 Daniel Boulet 8189797Srwatson * 9189797Srwatson * Redistribution and use in source forms, with and without modification, 10173095Srwatson * are permitted provided that this entire comment appears intact. 11173095Srwatson * 12173095Srwatson * Redistribution in binary form may occur without any restrictions. 13173095Srwatson * Obviously, it would be nice if you gave credit where credit is due 14173095Srwatson * but requiring it would be too onerous. 15173095Srwatson * 16173095Srwatson * This software is provided ``AS IS'' without any warranties of any kind. 17173095Srwatson * 18173095Srwatson * NEW command line interface for IP firewall facility 19173095Srwatson * 20173095Srwatson * $FreeBSD: stable/9/sbin/ipfw/ipfw2.c 248505 2013-03-19 13:29:01Z melifaro $ 21173095Srwatson */ 22173095Srwatson 23173095Srwatson#include <sys/types.h> 24173095Srwatson#include <sys/param.h> 25173095Srwatson#include <sys/socket.h> 26173095Srwatson#include <sys/sockio.h> 27173095Srwatson#include <sys/sysctl.h> 28173095Srwatson 29173095Srwatson#include "ipfw2.h" 30173095Srwatson 31173095Srwatson#include <ctype.h> 32173095Srwatson#include <err.h> 33173095Srwatson#include <errno.h> 34173095Srwatson#include <grp.h> 35173095Srwatson#include <netdb.h> 36173095Srwatson#include <pwd.h> 37173095Srwatson#include <stdio.h> 38173095Srwatson#include <stdlib.h> 39173095Srwatson#include <string.h> 40173095Srwatson#include <sysexits.h> 41173095Srwatson#include <time.h> /* ctime */ 42173095Srwatson#include <timeconv.h> /* _long_to_time */ 43173095Srwatson#include <unistd.h> 44173095Srwatson#include <fcntl.h> 45173095Srwatson#include <stddef.h> /* offsetof */ 46173095Srwatson 47173095Srwatson#include <net/ethernet.h> 48173095Srwatson#include <net/if.h> /* only IFNAMSIZ */ 49173095Srwatson#include <netinet/in.h> 50173095Srwatson#include <netinet/in_systm.h> /* only n_short, n_long */ 51173095Srwatson#include <netinet/ip.h> 52173095Srwatson#include <netinet/ip_icmp.h> 53173095Srwatson#include <netinet/ip_fw.h> 54173095Srwatson#include <netinet/tcp.h> 55184307Srwatson#include <arpa/inet.h> 56184307Srwatson 57184307Srwatsonstruct cmdline_opts co; /* global options */ 58184307Srwatson 59173095Srwatsonint resvd_set_number = RESVD_SET; 60173095Srwatson 61173095Srwatsonint ipfw_socket = -1; 62173095Srwatson 63184307Srwatson#ifndef s6_addr32 64184307Srwatson#define s6_addr32 __u6_addr.__u6_addr32 65184307Srwatson#endif 66184307Srwatson 67184307Srwatson#define CHECK_LENGTH(v, len) do { \ 68184307Srwatson if ((v) < (len)) \ 69184307Srwatson errx(EX_DATAERR, "Rule too long"); \ 70184307Srwatson } while (0) 71184307Srwatson/* 72184307Srwatson * Check if we have enough space in cmd buffer. Note that since 73189797Srwatson * first 8? u32 words are reserved by reserved header, full cmd 74191731Srwatson * buffer can't be used, so we need to protect from buffer overrun 75189797Srwatson * only. At the beginnig, cblen is less than actual buffer size by 76191731Srwatson * size of ipfw_insn_u32 instruction + 1 u32 work. This eliminates need 77184307Srwatson * for checking small instructions fitting in given range. 78191731Srwatson * We also (ab)use the fact that ipfw_insn is always the first field 79184307Srwatson * for any custom instruction. 80184307Srwatson */ 81184307Srwatson#define CHECK_CMDLEN CHECK_LENGTH(cblen, F_LEN((ipfw_insn *)cmd)) 82184307Srwatson 83184307Srwatson#define GET_UINT_ARG(arg, min, max, tok, s_x) do { \ 84184307Srwatson if (!av[0]) \ 85184307Srwatson errx(EX_USAGE, "%s: missing argument", match_value(s_x, tok)); \ 86184307Srwatson if (_substrcmp(*av, "tablearg") == 0) { \ 87184307Srwatson arg = IP_FW_TABLEARG; \ 88184307Srwatson break; \ 89187014Srwatson } \ 90184307Srwatson \ 91184307Srwatson { \ 92184307Srwatson long _xval; \ 93184307Srwatson char *end; \ 94184307Srwatson \ 95184307Srwatson _xval = strtol(*av, &end, 10); \ 96184307Srwatson \ 97184307Srwatson if (!isdigit(**av) || *end != '\0' || (_xval == 0 && errno == EINVAL)) \ 98184307Srwatson errx(EX_DATAERR, "%s: invalid argument: %s", \ 99184307Srwatson match_value(s_x, tok), *av); \ 100184307Srwatson \ 101184307Srwatson if (errno == ERANGE || _xval < min || _xval > max) \ 102191731Srwatson errx(EX_DATAERR, "%s: argument is out of range (%u..%u): %s", \ 103184307Srwatson match_value(s_x, tok), min, max, *av); \ 104184307Srwatson \ 105184307Srwatson if (_xval == IP_FW_TABLEARG) \ 106173095Srwatson errx(EX_DATAERR, "%s: illegal argument value: %s", \ 107184307Srwatson match_value(s_x, tok), *av); \ 108184307Srwatson arg = _xval; \ 109184307Srwatson } \ 110184307Srwatson} while (0) 111184307Srwatson 112184307Srwatsonstatic void 113184307SrwatsonPRINT_UINT_ARG(const char *str, uint32_t arg) 114184307Srwatson{ 115184307Srwatson if (str != NULL) 116184307Srwatson printf("%s",str); 117184307Srwatson if (arg == IP_FW_TABLEARG) 118184307Srwatson printf("tablearg"); 119184307Srwatson else 120184307Srwatson printf("%u", arg); 121193391Srwatson} 122193391Srwatson 123193391Srwatsonstatic struct _s_x f_tcpflags[] = { 124184307Srwatson { "syn", TH_SYN }, 125184307Srwatson { "fin", TH_FIN }, 126191731Srwatson { "ack", TH_ACK }, 127191731Srwatson { "psh", TH_PUSH }, 128184307Srwatson { "rst", TH_RST }, 129184307Srwatson { "urg", TH_URG }, 130184307Srwatson { "tcp flag", 0 }, 131184307Srwatson { NULL, 0 } 132184307Srwatson}; 133184307Srwatson 134184307Srwatsonstatic struct _s_x f_tcpopts[] = { 135193391Srwatson { "mss", IP_FW_TCPOPT_MSS }, 136193391Srwatson { "maxseg", IP_FW_TCPOPT_MSS }, 137193391Srwatson { "window", IP_FW_TCPOPT_WINDOW }, 138184307Srwatson { "sack", IP_FW_TCPOPT_SACK }, 139184307Srwatson { "ts", IP_FW_TCPOPT_TS }, 140191731Srwatson { "timestamp", IP_FW_TCPOPT_TS }, 141191731Srwatson { "cc", IP_FW_TCPOPT_CC }, 142184307Srwatson { "tcp option", 0 }, 143184307Srwatson { NULL, 0 } 144184307Srwatson}; 145184307Srwatson 146184307Srwatson/* 147184307Srwatson * IP options span the range 0 to 255 so we need to remap them 148184307Srwatson * (though in fact only the low 5 bits are significant). 149184307Srwatson */ 150193391Srwatsonstatic struct _s_x f_ipopts[] = { 151193391Srwatson { "ssrr", IP_FW_IPOPT_SSRR}, 152193391Srwatson { "lsrr", IP_FW_IPOPT_LSRR}, 153184307Srwatson { "rr", IP_FW_IPOPT_RR}, 154184307Srwatson { "ts", IP_FW_IPOPT_TS}, 155184307Srwatson { "ip option", 0 }, 156191731Srwatson { NULL, 0 } 157191731Srwatson}; 158184307Srwatson 159184307Srwatsonstatic struct _s_x f_iptos[] = { 160184307Srwatson { "lowdelay", IPTOS_LOWDELAY}, 161184307Srwatson { "throughput", IPTOS_THROUGHPUT}, 162184307Srwatson { "reliability", IPTOS_RELIABILITY}, 163184307Srwatson { "mincost", IPTOS_MINCOST}, 164184307Srwatson { "congestion", IPTOS_ECN_CE}, 165184307Srwatson { "ecntransport", IPTOS_ECN_ECT0}, 166184307Srwatson { "ip tos option", 0}, 167193391Srwatson { NULL, 0 } 168193391Srwatson}; 169193391Srwatson 170184307Srwatsonstatic struct _s_x limit_masks[] = { 171184307Srwatson {"all", DYN_SRC_ADDR|DYN_SRC_PORT|DYN_DST_ADDR|DYN_DST_PORT}, 172191731Srwatson {"src-addr", DYN_SRC_ADDR}, 173191731Srwatson {"src-port", DYN_SRC_PORT}, 174184307Srwatson {"dst-addr", DYN_DST_ADDR}, 175184307Srwatson {"dst-port", DYN_DST_PORT}, 176184307Srwatson {NULL, 0} 177173095Srwatson}; 178173095Srwatson 179173095Srwatson/* 180173095Srwatson * we use IPPROTO_ETHERTYPE as a fake protocol id to call the print routines 181193391Srwatson * This is only used in this code. 182193391Srwatson */ 183193391Srwatson#define IPPROTO_ETHERTYPE 0x1000 184173095Srwatsonstatic struct _s_x ether_types[] = { 185173095Srwatson /* 186191731Srwatson * Note, we cannot use "-:&/" in the names because they are field 187189797Srwatson * separators in the type specifications. Also, we use s = NULL as 188173095Srwatson * end-delimiter, because a type of 0 can be legal. 189 */ 190 { "ip", 0x0800 }, 191 { "ipv4", 0x0800 }, 192 { "ipv6", 0x86dd }, 193 { "arp", 0x0806 }, 194 { "rarp", 0x8035 }, 195 { "vlan", 0x8100 }, 196 { "loop", 0x9000 }, 197 { "trail", 0x1000 }, 198 { "at", 0x809b }, 199 { "atalk", 0x809b }, 200 { "aarp", 0x80f3 }, 201 { "pppoe_disc", 0x8863 }, 202 { "pppoe_sess", 0x8864 }, 203 { "ipx_8022", 0x00E0 }, 204 { "ipx_8023", 0x0000 }, 205 { "ipx_ii", 0x8137 }, 206 { "ipx_snap", 0x8137 }, 207 { "ipx", 0x8137 }, 208 { "ns", 0x0600 }, 209 { NULL, 0 } 210}; 211 212 213static struct _s_x rule_actions[] = { 214 { "accept", TOK_ACCEPT }, 215 { "pass", TOK_ACCEPT }, 216 { "allow", TOK_ACCEPT }, 217 { "permit", TOK_ACCEPT }, 218 { "count", TOK_COUNT }, 219 { "pipe", TOK_PIPE }, 220 { "queue", TOK_QUEUE }, 221 { "divert", TOK_DIVERT }, 222 { "tee", TOK_TEE }, 223 { "netgraph", TOK_NETGRAPH }, 224 { "ngtee", TOK_NGTEE }, 225 { "fwd", TOK_FORWARD }, 226 { "forward", TOK_FORWARD }, 227 { "skipto", TOK_SKIPTO }, 228 { "deny", TOK_DENY }, 229 { "drop", TOK_DENY }, 230 { "reject", TOK_REJECT }, 231 { "reset6", TOK_RESET6 }, 232 { "reset", TOK_RESET }, 233 { "unreach6", TOK_UNREACH6 }, 234 { "unreach", TOK_UNREACH }, 235 { "check-state", TOK_CHECKSTATE }, 236 { "//", TOK_COMMENT }, 237 { "nat", TOK_NAT }, 238 { "reass", TOK_REASS }, 239 { "setfib", TOK_SETFIB }, 240 { "call", TOK_CALL }, 241 { "return", TOK_RETURN }, 242 { NULL, 0 } /* terminator */ 243}; 244 245static struct _s_x rule_action_params[] = { 246 { "altq", TOK_ALTQ }, 247 { "log", TOK_LOG }, 248 { "tag", TOK_TAG }, 249 { "untag", TOK_UNTAG }, 250 { NULL, 0 } /* terminator */ 251}; 252 253/* 254 * The 'lookup' instruction accepts one of the following arguments. 255 * -1 is a terminator for the list. 256 * Arguments are passed as v[1] in O_DST_LOOKUP options. 257 */ 258static int lookup_key[] = { 259 TOK_DSTIP, TOK_SRCIP, TOK_DSTPORT, TOK_SRCPORT, 260 TOK_UID, TOK_JAIL, TOK_DSCP, -1 }; 261 262static struct _s_x rule_options[] = { 263 { "tagged", TOK_TAGGED }, 264 { "uid", TOK_UID }, 265 { "gid", TOK_GID }, 266 { "jail", TOK_JAIL }, 267 { "in", TOK_IN }, 268 { "limit", TOK_LIMIT }, 269 { "keep-state", TOK_KEEPSTATE }, 270 { "bridged", TOK_LAYER2 }, 271 { "layer2", TOK_LAYER2 }, 272 { "out", TOK_OUT }, 273 { "diverted", TOK_DIVERTED }, 274 { "diverted-loopback", TOK_DIVERTEDLOOPBACK }, 275 { "diverted-output", TOK_DIVERTEDOUTPUT }, 276 { "xmit", TOK_XMIT }, 277 { "recv", TOK_RECV }, 278 { "via", TOK_VIA }, 279 { "fragment", TOK_FRAG }, 280 { "frag", TOK_FRAG }, 281 { "fib", TOK_FIB }, 282 { "ipoptions", TOK_IPOPTS }, 283 { "ipopts", TOK_IPOPTS }, 284 { "iplen", TOK_IPLEN }, 285 { "ipid", TOK_IPID }, 286 { "ipprecedence", TOK_IPPRECEDENCE }, 287 { "dscp", TOK_DSCP }, 288 { "iptos", TOK_IPTOS }, 289 { "ipttl", TOK_IPTTL }, 290 { "ipversion", TOK_IPVER }, 291 { "ipver", TOK_IPVER }, 292 { "estab", TOK_ESTAB }, 293 { "established", TOK_ESTAB }, 294 { "setup", TOK_SETUP }, 295 { "sockarg", TOK_SOCKARG }, 296 { "tcpdatalen", TOK_TCPDATALEN }, 297 { "tcpflags", TOK_TCPFLAGS }, 298 { "tcpflgs", TOK_TCPFLAGS }, 299 { "tcpoptions", TOK_TCPOPTS }, 300 { "tcpopts", TOK_TCPOPTS }, 301 { "tcpseq", TOK_TCPSEQ }, 302 { "tcpack", TOK_TCPACK }, 303 { "tcpwin", TOK_TCPWIN }, 304 { "icmptype", TOK_ICMPTYPES }, 305 { "icmptypes", TOK_ICMPTYPES }, 306 { "dst-ip", TOK_DSTIP }, 307 { "src-ip", TOK_SRCIP }, 308 { "dst-port", TOK_DSTPORT }, 309 { "src-port", TOK_SRCPORT }, 310 { "proto", TOK_PROTO }, 311 { "MAC", TOK_MAC }, 312 { "mac", TOK_MAC }, 313 { "mac-type", TOK_MACTYPE }, 314 { "verrevpath", TOK_VERREVPATH }, 315 { "versrcreach", TOK_VERSRCREACH }, 316 { "antispoof", TOK_ANTISPOOF }, 317 { "ipsec", TOK_IPSEC }, 318 { "icmp6type", TOK_ICMP6TYPES }, 319 { "icmp6types", TOK_ICMP6TYPES }, 320 { "ext6hdr", TOK_EXT6HDR}, 321 { "flow-id", TOK_FLOWID}, 322 { "ipv6", TOK_IPV6}, 323 { "ip6", TOK_IPV6}, 324 { "ipv4", TOK_IPV4}, 325 { "ip4", TOK_IPV4}, 326 { "dst-ipv6", TOK_DSTIP6}, 327 { "dst-ip6", TOK_DSTIP6}, 328 { "src-ipv6", TOK_SRCIP6}, 329 { "src-ip6", TOK_SRCIP6}, 330 { "lookup", TOK_LOOKUP}, 331 { "//", TOK_COMMENT }, 332 333 { "not", TOK_NOT }, /* pseudo option */ 334 { "!", /* escape ? */ TOK_NOT }, /* pseudo option */ 335 { "or", TOK_OR }, /* pseudo option */ 336 { "|", /* escape */ TOK_OR }, /* pseudo option */ 337 { "{", TOK_STARTBRACE }, /* pseudo option */ 338 { "(", TOK_STARTBRACE }, /* pseudo option */ 339 { "}", TOK_ENDBRACE }, /* pseudo option */ 340 { ")", TOK_ENDBRACE }, /* pseudo option */ 341 { NULL, 0 } /* terminator */ 342}; 343 344/* 345 * Helper routine to print a possibly unaligned uint64_t on 346 * various platform. If width > 0, print the value with 347 * the desired width, followed by a space; 348 * otherwise, return the required width. 349 */ 350int 351pr_u64(uint64_t *pd, int width) 352{ 353#ifdef TCC 354#define U64_FMT "I64" 355#else 356#define U64_FMT "llu" 357#endif 358 uint64_t u; 359 unsigned long long d; 360 361 bcopy (pd, &u, sizeof(u)); 362 d = u; 363 return (width > 0) ? 364 printf("%*" U64_FMT " ", width, d) : 365 snprintf(NULL, 0, "%" U64_FMT, d) ; 366#undef U64_FMT 367} 368 369void * 370safe_calloc(size_t number, size_t size) 371{ 372 void *ret = calloc(number, size); 373 374 if (ret == NULL) 375 err(EX_OSERR, "calloc"); 376 return ret; 377} 378 379void * 380safe_realloc(void *ptr, size_t size) 381{ 382 void *ret = realloc(ptr, size); 383 384 if (ret == NULL) 385 err(EX_OSERR, "realloc"); 386 return ret; 387} 388 389/* 390 * conditionally runs the command. 391 * Selected options or negative -> getsockopt 392 */ 393int 394do_cmd(int optname, void *optval, uintptr_t optlen) 395{ 396 int i; 397 398 if (co.test_only) 399 return 0; 400 401 if (ipfw_socket == -1) 402 ipfw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); 403 if (ipfw_socket < 0) 404 err(EX_UNAVAILABLE, "socket"); 405 406 if (optname == IP_FW_GET || optname == IP_DUMMYNET_GET || 407 optname == IP_FW_ADD || optname == IP_FW3 || 408 optname == IP_FW_NAT_GET_CONFIG || 409 optname < 0 || 410 optname == IP_FW_NAT_GET_LOG) { 411 if (optname < 0) 412 optname = -optname; 413 i = getsockopt(ipfw_socket, IPPROTO_IP, optname, optval, 414 (socklen_t *)optlen); 415 } else { 416 i = setsockopt(ipfw_socket, IPPROTO_IP, optname, optval, optlen); 417 } 418 return i; 419} 420 421/* 422 * do_setcmd3 - pass ipfw control cmd to kernel 423 * @optname: option name 424 * @optval: pointer to option data 425 * @optlen: option length 426 * 427 * Function encapsulates option value in IP_FW3 socket option 428 * and calls setsockopt(). 429 * Function returns 0 on success or -1 otherwise. 430 */ 431int 432do_setcmd3(int optname, void *optval, socklen_t optlen) 433{ 434 socklen_t len; 435 ip_fw3_opheader *op3; 436 437 if (co.test_only) 438 return (0); 439 440 if (ipfw_socket == -1) 441 ipfw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); 442 if (ipfw_socket < 0) 443 err(EX_UNAVAILABLE, "socket"); 444 445 len = sizeof(ip_fw3_opheader) + optlen; 446 op3 = alloca(len); 447 /* Zero reserved fields */ 448 memset(op3, 0, sizeof(ip_fw3_opheader)); 449 memcpy(op3 + 1, optval, optlen); 450 op3->opcode = optname; 451 452 return setsockopt(ipfw_socket, IPPROTO_IP, IP_FW3, op3, len); 453} 454 455/** 456 * match_token takes a table and a string, returns the value associated 457 * with the string (-1 in case of failure). 458 */ 459int 460match_token(struct _s_x *table, char *string) 461{ 462 struct _s_x *pt; 463 uint i = strlen(string); 464 465 for (pt = table ; i && pt->s != NULL ; pt++) 466 if (strlen(pt->s) == i && !bcmp(string, pt->s, i)) 467 return pt->x; 468 return -1; 469} 470 471/** 472 * match_value takes a table and a value, returns the string associated 473 * with the value (NULL in case of failure). 474 */ 475char const * 476match_value(struct _s_x *p, int value) 477{ 478 for (; p->s != NULL; p++) 479 if (p->x == value) 480 return p->s; 481 return NULL; 482} 483 484/* 485 * _substrcmp takes two strings and returns 1 if they do not match, 486 * and 0 if they match exactly or the first string is a sub-string 487 * of the second. A warning is printed to stderr in the case that the 488 * first string is a sub-string of the second. 489 * 490 * This function will be removed in the future through the usual 491 * deprecation process. 492 */ 493int 494_substrcmp(const char *str1, const char* str2) 495{ 496 497 if (strncmp(str1, str2, strlen(str1)) != 0) 498 return 1; 499 500 if (strlen(str1) != strlen(str2)) 501 warnx("DEPRECATED: '%s' matched '%s' as a sub-string", 502 str1, str2); 503 return 0; 504} 505 506/* 507 * _substrcmp2 takes three strings and returns 1 if the first two do not match, 508 * and 0 if they match exactly or the second string is a sub-string 509 * of the first. A warning is printed to stderr in the case that the 510 * first string does not match the third. 511 * 512 * This function exists to warn about the bizzare construction 513 * strncmp(str, "by", 2) which is used to allow people to use a shotcut 514 * for "bytes". The problem is that in addition to accepting "by", 515 * "byt", "byte", and "bytes", it also excepts "by_rabid_dogs" and any 516 * other string beginning with "by". 517 * 518 * This function will be removed in the future through the usual 519 * deprecation process. 520 */ 521int 522_substrcmp2(const char *str1, const char* str2, const char* str3) 523{ 524 525 if (strncmp(str1, str2, strlen(str2)) != 0) 526 return 1; 527 528 if (strcmp(str1, str3) != 0) 529 warnx("DEPRECATED: '%s' matched '%s'", 530 str1, str3); 531 return 0; 532} 533 534/* 535 * prints one port, symbolic or numeric 536 */ 537static void 538print_port(int proto, uint16_t port) 539{ 540 541 if (proto == IPPROTO_ETHERTYPE) { 542 char const *s; 543 544 if (co.do_resolv && (s = match_value(ether_types, port)) ) 545 printf("%s", s); 546 else 547 printf("0x%04x", port); 548 } else { 549 struct servent *se = NULL; 550 if (co.do_resolv) { 551 struct protoent *pe = getprotobynumber(proto); 552 553 se = getservbyport(htons(port), pe ? pe->p_name : NULL); 554 } 555 if (se) 556 printf("%s", se->s_name); 557 else 558 printf("%d", port); 559 } 560} 561 562static struct _s_x _port_name[] = { 563 {"dst-port", O_IP_DSTPORT}, 564 {"src-port", O_IP_SRCPORT}, 565 {"ipid", O_IPID}, 566 {"iplen", O_IPLEN}, 567 {"ipttl", O_IPTTL}, 568 {"mac-type", O_MAC_TYPE}, 569 {"tcpdatalen", O_TCPDATALEN}, 570 {"tcpwin", O_TCPWIN}, 571 {"tagged", O_TAGGED}, 572 {NULL, 0} 573}; 574 575/* 576 * Print the values in a list 16-bit items of the types above. 577 * XXX todo: add support for mask. 578 */ 579static void 580print_newports(ipfw_insn_u16 *cmd, int proto, int opcode) 581{ 582 uint16_t *p = cmd->ports; 583 int i; 584 char const *sep; 585 586 if (opcode != 0) { 587 sep = match_value(_port_name, opcode); 588 if (sep == NULL) 589 sep = "???"; 590 printf (" %s", sep); 591 } 592 sep = " "; 593 for (i = F_LEN((ipfw_insn *)cmd) - 1; i > 0; i--, p += 2) { 594 printf("%s", sep); 595 print_port(proto, p[0]); 596 if (p[0] != p[1]) { 597 printf("-"); 598 print_port(proto, p[1]); 599 } 600 sep = ","; 601 } 602} 603 604/* 605 * Like strtol, but also translates service names into port numbers 606 * for some protocols. 607 * In particular: 608 * proto == -1 disables the protocol check; 609 * proto == IPPROTO_ETHERTYPE looks up an internal table 610 * proto == <some value in /etc/protocols> matches the values there. 611 * Returns *end == s in case the parameter is not found. 612 */ 613static int 614strtoport(char *s, char **end, int base, int proto) 615{ 616 char *p, *buf; 617 char *s1; 618 int i; 619 620 *end = s; /* default - not found */ 621 if (*s == '\0') 622 return 0; /* not found */ 623 624 if (isdigit(*s)) 625 return strtol(s, end, base); 626 627 /* 628 * find separator. '\\' escapes the next char. 629 */ 630 for (s1 = s; *s1 && (isalnum(*s1) || *s1 == '\\') ; s1++) 631 if (*s1 == '\\' && s1[1] != '\0') 632 s1++; 633 634 buf = safe_calloc(s1 - s + 1, 1); 635 636 /* 637 * copy into a buffer skipping backslashes 638 */ 639 for (p = s, i = 0; p != s1 ; p++) 640 if (*p != '\\') 641 buf[i++] = *p; 642 buf[i++] = '\0'; 643 644 if (proto == IPPROTO_ETHERTYPE) { 645 i = match_token(ether_types, buf); 646 free(buf); 647 if (i != -1) { /* found */ 648 *end = s1; 649 return i; 650 } 651 } else { 652 struct protoent *pe = NULL; 653 struct servent *se; 654 655 if (proto != 0) 656 pe = getprotobynumber(proto); 657 setservent(1); 658 se = getservbyname(buf, pe ? pe->p_name : NULL); 659 free(buf); 660 if (se != NULL) { 661 *end = s1; 662 return ntohs(se->s_port); 663 } 664 } 665 return 0; /* not found */ 666} 667 668/* 669 * Fill the body of the command with the list of port ranges. 670 */ 671static int 672fill_newports(ipfw_insn_u16 *cmd, char *av, int proto, int cblen) 673{ 674 uint16_t a, b, *p = cmd->ports; 675 int i = 0; 676 char *s = av; 677 678 while (*s) { 679 a = strtoport(av, &s, 0, proto); 680 if (s == av) /* empty or invalid argument */ 681 return (0); 682 683 CHECK_LENGTH(cblen, i + 2); 684 685 switch (*s) { 686 case '-': /* a range */ 687 av = s + 1; 688 b = strtoport(av, &s, 0, proto); 689 /* Reject expressions like '1-abc' or '1-2-3'. */ 690 if (s == av || (*s != ',' && *s != '\0')) 691 return (0); 692 p[0] = a; 693 p[1] = b; 694 break; 695 case ',': /* comma separated list */ 696 case '\0': 697 p[0] = p[1] = a; 698 break; 699 default: 700 warnx("port list: invalid separator <%c> in <%s>", 701 *s, av); 702 return (0); 703 } 704 705 i++; 706 p += 2; 707 av = s + 1; 708 } 709 if (i > 0) { 710 if (i + 1 > F_LEN_MASK) 711 errx(EX_DATAERR, "too many ports/ranges\n"); 712 cmd->o.len |= i + 1; /* leave F_NOT and F_OR untouched */ 713 } 714 return (i); 715} 716 717static struct _s_x icmpcodes[] = { 718 { "net", ICMP_UNREACH_NET }, 719 { "host", ICMP_UNREACH_HOST }, 720 { "protocol", ICMP_UNREACH_PROTOCOL }, 721 { "port", ICMP_UNREACH_PORT }, 722 { "needfrag", ICMP_UNREACH_NEEDFRAG }, 723 { "srcfail", ICMP_UNREACH_SRCFAIL }, 724 { "net-unknown", ICMP_UNREACH_NET_UNKNOWN }, 725 { "host-unknown", ICMP_UNREACH_HOST_UNKNOWN }, 726 { "isolated", ICMP_UNREACH_ISOLATED }, 727 { "net-prohib", ICMP_UNREACH_NET_PROHIB }, 728 { "host-prohib", ICMP_UNREACH_HOST_PROHIB }, 729 { "tosnet", ICMP_UNREACH_TOSNET }, 730 { "toshost", ICMP_UNREACH_TOSHOST }, 731 { "filter-prohib", ICMP_UNREACH_FILTER_PROHIB }, 732 { "host-precedence", ICMP_UNREACH_HOST_PRECEDENCE }, 733 { "precedence-cutoff", ICMP_UNREACH_PRECEDENCE_CUTOFF }, 734 { NULL, 0 } 735}; 736 737static void 738fill_reject_code(u_short *codep, char *str) 739{ 740 int val; 741 char *s; 742 743 val = strtoul(str, &s, 0); 744 if (s == str || *s != '\0' || val >= 0x100) 745 val = match_token(icmpcodes, str); 746 if (val < 0) 747 errx(EX_DATAERR, "unknown ICMP unreachable code ``%s''", str); 748 *codep = val; 749 return; 750} 751 752static void 753print_reject_code(uint16_t code) 754{ 755 char const *s = match_value(icmpcodes, code); 756 757 if (s != NULL) 758 printf("unreach %s", s); 759 else 760 printf("unreach %u", code); 761} 762 763/* 764 * Returns the number of bits set (from left) in a contiguous bitmask, 765 * or -1 if the mask is not contiguous. 766 * XXX this needs a proper fix. 767 * This effectively works on masks in big-endian (network) format. 768 * when compiled on little endian architectures. 769 * 770 * First bit is bit 7 of the first byte -- note, for MAC addresses, 771 * the first bit on the wire is bit 0 of the first byte. 772 * len is the max length in bits. 773 */ 774int 775contigmask(uint8_t *p, int len) 776{ 777 int i, n; 778 779 for (i=0; i<len ; i++) 780 if ( (p[i/8] & (1 << (7 - (i%8)))) == 0) /* first bit unset */ 781 break; 782 for (n=i+1; n < len; n++) 783 if ( (p[n/8] & (1 << (7 - (n%8)))) != 0) 784 return -1; /* mask not contiguous */ 785 return i; 786} 787 788/* 789 * print flags set/clear in the two bitmasks passed as parameters. 790 * There is a specialized check for f_tcpflags. 791 */ 792static void 793print_flags(char const *name, ipfw_insn *cmd, struct _s_x *list) 794{ 795 char const *comma = ""; 796 int i; 797 uint8_t set = cmd->arg1 & 0xff; 798 uint8_t clear = (cmd->arg1 >> 8) & 0xff; 799 800 if (list == f_tcpflags && set == TH_SYN && clear == TH_ACK) { 801 printf(" setup"); 802 return; 803 } 804 805 printf(" %s ", name); 806 for (i=0; list[i].x != 0; i++) { 807 if (set & list[i].x) { 808 set &= ~list[i].x; 809 printf("%s%s", comma, list[i].s); 810 comma = ","; 811 } 812 if (clear & list[i].x) { 813 clear &= ~list[i].x; 814 printf("%s!%s", comma, list[i].s); 815 comma = ","; 816 } 817 } 818} 819 820/* 821 * Print the ip address contained in a command. 822 */ 823static void 824print_ip(ipfw_insn_ip *cmd, char const *s) 825{ 826 struct hostent *he = NULL; 827 uint32_t len = F_LEN((ipfw_insn *)cmd); 828 uint32_t *a = ((ipfw_insn_u32 *)cmd)->d; 829 830 if (cmd->o.opcode == O_IP_DST_LOOKUP && len > F_INSN_SIZE(ipfw_insn_u32)) { 831 uint32_t d = a[1]; 832 const char *arg = "<invalid>"; 833 834 if (d < sizeof(lookup_key)/sizeof(lookup_key[0])) 835 arg = match_value(rule_options, lookup_key[d]); 836 printf("%s lookup %s %d", cmd->o.len & F_NOT ? " not": "", 837 arg, cmd->o.arg1); 838 return; 839 } 840 printf("%s%s ", cmd->o.len & F_NOT ? " not": "", s); 841 842 if (cmd->o.opcode == O_IP_SRC_ME || cmd->o.opcode == O_IP_DST_ME) { 843 printf("me"); 844 return; 845 } 846 if (cmd->o.opcode == O_IP_SRC_LOOKUP || 847 cmd->o.opcode == O_IP_DST_LOOKUP) { 848 printf("table(%u", ((ipfw_insn *)cmd)->arg1); 849 if (len == F_INSN_SIZE(ipfw_insn_u32)) 850 printf(",%u", *a); 851 printf(")"); 852 return; 853 } 854 if (cmd->o.opcode == O_IP_SRC_SET || cmd->o.opcode == O_IP_DST_SET) { 855 uint32_t x, *map = (uint32_t *)&(cmd->mask); 856 int i, j; 857 char comma = '{'; 858 859 x = cmd->o.arg1 - 1; 860 x = htonl( ~x ); 861 cmd->addr.s_addr = htonl(cmd->addr.s_addr); 862 printf("%s/%d", inet_ntoa(cmd->addr), 863 contigmask((uint8_t *)&x, 32)); 864 x = cmd->addr.s_addr = htonl(cmd->addr.s_addr); 865 x &= 0xff; /* base */ 866 /* 867 * Print bits and ranges. 868 * Locate first bit set (i), then locate first bit unset (j). 869 * If we have 3+ consecutive bits set, then print them as a 870 * range, otherwise only print the initial bit and rescan. 871 */ 872 for (i=0; i < cmd->o.arg1; i++) 873 if (map[i/32] & (1<<(i & 31))) { 874 for (j=i+1; j < cmd->o.arg1; j++) 875 if (!(map[ j/32] & (1<<(j & 31)))) 876 break; 877 printf("%c%d", comma, i+x); 878 if (j>i+2) { /* range has at least 3 elements */ 879 printf("-%d", j-1+x); 880 i = j-1; 881 } 882 comma = ','; 883 } 884 printf("}"); 885 return; 886 } 887 /* 888 * len == 2 indicates a single IP, whereas lists of 1 or more 889 * addr/mask pairs have len = (2n+1). We convert len to n so we 890 * use that to count the number of entries. 891 */ 892 for (len = len / 2; len > 0; len--, a += 2) { 893 int mb = /* mask length */ 894 (cmd->o.opcode == O_IP_SRC || cmd->o.opcode == O_IP_DST) ? 895 32 : contigmask((uint8_t *)&(a[1]), 32); 896 if (mb == 32 && co.do_resolv) 897 he = gethostbyaddr((char *)&(a[0]), sizeof(u_long), AF_INET); 898 if (he != NULL) /* resolved to name */ 899 printf("%s", he->h_name); 900 else if (mb == 0) /* any */ 901 printf("any"); 902 else { /* numeric IP followed by some kind of mask */ 903 printf("%s", inet_ntoa( *((struct in_addr *)&a[0]) ) ); 904 if (mb < 0) 905 printf(":%s", inet_ntoa( *((struct in_addr *)&a[1]) ) ); 906 else if (mb < 32) 907 printf("/%d", mb); 908 } 909 if (len > 1) 910 printf(","); 911 } 912} 913 914/* 915 * prints a MAC address/mask pair 916 */ 917static void 918print_mac(uint8_t *addr, uint8_t *mask) 919{ 920 int l = contigmask(mask, 48); 921 922 if (l == 0) 923 printf(" any"); 924 else { 925 printf(" %02x:%02x:%02x:%02x:%02x:%02x", 926 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 927 if (l == -1) 928 printf("&%02x:%02x:%02x:%02x:%02x:%02x", 929 mask[0], mask[1], mask[2], 930 mask[3], mask[4], mask[5]); 931 else if (l < 48) 932 printf("/%d", l); 933 } 934} 935 936static void 937fill_icmptypes(ipfw_insn_u32 *cmd, char *av) 938{ 939 uint8_t type; 940 941 cmd->d[0] = 0; 942 while (*av) { 943 if (*av == ',') 944 av++; 945 946 type = strtoul(av, &av, 0); 947 948 if (*av != ',' && *av != '\0') 949 errx(EX_DATAERR, "invalid ICMP type"); 950 951 if (type > 31) 952 errx(EX_DATAERR, "ICMP type out of range"); 953 954 cmd->d[0] |= 1 << type; 955 } 956 cmd->o.opcode = O_ICMPTYPE; 957 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 958} 959 960static void 961print_icmptypes(ipfw_insn_u32 *cmd) 962{ 963 int i; 964 char sep= ' '; 965 966 printf(" icmptypes"); 967 for (i = 0; i < 32; i++) { 968 if ( (cmd->d[0] & (1 << (i))) == 0) 969 continue; 970 printf("%c%d", sep, i); 971 sep = ','; 972 } 973} 974 975/* 976 * show_ipfw() prints the body of an ipfw rule. 977 * Because the standard rule has at least proto src_ip dst_ip, we use 978 * a helper function to produce these entries if not provided explicitly. 979 * The first argument is the list of fields we have, the second is 980 * the list of fields we want to be printed. 981 * 982 * Special cases if we have provided a MAC header: 983 * + if the rule does not contain IP addresses/ports, do not print them; 984 * + if the rule does not contain an IP proto, print "all" instead of "ip"; 985 * 986 * Once we have 'have_options', IP header fields are printed as options. 987 */ 988#define HAVE_PROTO 0x0001 989#define HAVE_SRCIP 0x0002 990#define HAVE_DSTIP 0x0004 991#define HAVE_PROTO4 0x0008 992#define HAVE_PROTO6 0x0010 993#define HAVE_IP 0x0100 994#define HAVE_OPTIONS 0x8000 995 996static void 997show_prerequisites(int *flags, int want, int cmd __unused) 998{ 999 if (co.comment_only) 1000 return; 1001 if ( (*flags & HAVE_IP) == HAVE_IP) 1002 *flags |= HAVE_OPTIONS; 1003 1004 if ( !(*flags & HAVE_OPTIONS)) { 1005 if ( !(*flags & HAVE_PROTO) && (want & HAVE_PROTO)) { 1006 if ( (*flags & HAVE_PROTO4)) 1007 printf(" ip4"); 1008 else if ( (*flags & HAVE_PROTO6)) 1009 printf(" ip6"); 1010 else 1011 printf(" ip"); 1012 } 1013 if ( !(*flags & HAVE_SRCIP) && (want & HAVE_SRCIP)) 1014 printf(" from any"); 1015 if ( !(*flags & HAVE_DSTIP) && (want & HAVE_DSTIP)) 1016 printf(" to any"); 1017 } 1018 *flags |= want; 1019} 1020 1021static void 1022show_ipfw(struct ip_fw *rule, int pcwidth, int bcwidth) 1023{ 1024 static int twidth = 0; 1025 int l; 1026 ipfw_insn *cmd, *tagptr = NULL; 1027 const char *comment = NULL; /* ptr to comment if we have one */ 1028 int proto = 0; /* default */ 1029 int flags = 0; /* prerequisites */ 1030 ipfw_insn_log *logptr = NULL; /* set if we find an O_LOG */ 1031 ipfw_insn_altq *altqptr = NULL; /* set if we find an O_ALTQ */ 1032 int or_block = 0; /* we are in an or block */ 1033 uint32_t set_disable; 1034 1035 bcopy(&rule->next_rule, &set_disable, sizeof(set_disable)); 1036 1037 if (set_disable & (1 << rule->set)) { /* disabled */ 1038 if (!co.show_sets) 1039 return; 1040 else 1041 printf("# DISABLED "); 1042 } 1043 printf("%05u ", rule->rulenum); 1044 1045 if (pcwidth > 0 || bcwidth > 0) { 1046 pr_u64(&rule->pcnt, pcwidth); 1047 pr_u64(&rule->bcnt, bcwidth); 1048 } 1049 1050 if (co.do_time == 2) 1051 printf("%10u ", rule->timestamp); 1052 else if (co.do_time == 1) { 1053 char timestr[30]; 1054 time_t t = (time_t)0; 1055 1056 if (twidth == 0) { 1057 strcpy(timestr, ctime(&t)); 1058 *strchr(timestr, '\n') = '\0'; 1059 twidth = strlen(timestr); 1060 } 1061 if (rule->timestamp) { 1062 t = _long_to_time(rule->timestamp); 1063 1064 strcpy(timestr, ctime(&t)); 1065 *strchr(timestr, '\n') = '\0'; 1066 printf("%s ", timestr); 1067 } else { 1068 printf("%*s", twidth, " "); 1069 } 1070 } 1071 1072 if (co.show_sets) 1073 printf("set %d ", rule->set); 1074 1075 /* 1076 * print the optional "match probability" 1077 */ 1078 if (rule->cmd_len > 0) { 1079 cmd = rule->cmd ; 1080 if (cmd->opcode == O_PROB) { 1081 ipfw_insn_u32 *p = (ipfw_insn_u32 *)cmd; 1082 double d = 1.0 * p->d[0]; 1083 1084 d = (d / 0x7fffffff); 1085 printf("prob %f ", d); 1086 } 1087 } 1088 1089 /* 1090 * first print actions 1091 */ 1092 for (l = rule->cmd_len - rule->act_ofs, cmd = ACTION_PTR(rule); 1093 l > 0 ; l -= F_LEN(cmd), cmd += F_LEN(cmd)) { 1094 switch(cmd->opcode) { 1095 case O_CHECK_STATE: 1096 printf("check-state"); 1097 /* avoid printing anything else */ 1098 flags = HAVE_PROTO | HAVE_SRCIP | 1099 HAVE_DSTIP | HAVE_IP; 1100 break; 1101 1102 case O_ACCEPT: 1103 printf("allow"); 1104 break; 1105 1106 case O_COUNT: 1107 printf("count"); 1108 break; 1109 1110 case O_DENY: 1111 printf("deny"); 1112 break; 1113 1114 case O_REJECT: 1115 if (cmd->arg1 == ICMP_REJECT_RST) 1116 printf("reset"); 1117 else if (cmd->arg1 == ICMP_UNREACH_HOST) 1118 printf("reject"); 1119 else 1120 print_reject_code(cmd->arg1); 1121 break; 1122 1123 case O_UNREACH6: 1124 if (cmd->arg1 == ICMP6_UNREACH_RST) 1125 printf("reset6"); 1126 else 1127 print_unreach6_code(cmd->arg1); 1128 break; 1129 1130 case O_SKIPTO: 1131 PRINT_UINT_ARG("skipto ", cmd->arg1); 1132 break; 1133 1134 case O_PIPE: 1135 PRINT_UINT_ARG("pipe ", cmd->arg1); 1136 break; 1137 1138 case O_QUEUE: 1139 PRINT_UINT_ARG("queue ", cmd->arg1); 1140 break; 1141 1142 case O_DIVERT: 1143 PRINT_UINT_ARG("divert ", cmd->arg1); 1144 break; 1145 1146 case O_TEE: 1147 PRINT_UINT_ARG("tee ", cmd->arg1); 1148 break; 1149 1150 case O_NETGRAPH: 1151 PRINT_UINT_ARG("netgraph ", cmd->arg1); 1152 break; 1153 1154 case O_NGTEE: 1155 PRINT_UINT_ARG("ngtee ", cmd->arg1); 1156 break; 1157 1158 case O_FORWARD_IP: 1159 { 1160 ipfw_insn_sa *s = (ipfw_insn_sa *)cmd; 1161 1162 if (s->sa.sin_addr.s_addr == INADDR_ANY) { 1163 printf("fwd tablearg"); 1164 } else { 1165 printf("fwd %s", inet_ntoa(s->sa.sin_addr)); 1166 } 1167 if (s->sa.sin_port) 1168 printf(",%d", s->sa.sin_port); 1169 } 1170 break; 1171 1172 case O_FORWARD_IP6: 1173 { 1174 char buf[4 + INET6_ADDRSTRLEN + 1]; 1175 ipfw_insn_sa6 *s = (ipfw_insn_sa6 *)cmd; 1176 1177 printf("fwd %s", inet_ntop(AF_INET6, &s->sa.sin6_addr, 1178 buf, sizeof(buf))); 1179 if (s->sa.sin6_port) 1180 printf(",%d", s->sa.sin6_port); 1181 } 1182 break; 1183 1184 case O_LOG: /* O_LOG is printed last */ 1185 logptr = (ipfw_insn_log *)cmd; 1186 break; 1187 1188 case O_ALTQ: /* O_ALTQ is printed after O_LOG */ 1189 altqptr = (ipfw_insn_altq *)cmd; 1190 break; 1191 1192 case O_TAG: 1193 tagptr = cmd; 1194 break; 1195 1196 case O_NAT: 1197 if (cmd->arg1 != 0) 1198 PRINT_UINT_ARG("nat ", cmd->arg1); 1199 else 1200 printf("nat global"); 1201 break; 1202 1203 case O_SETFIB: 1204 PRINT_UINT_ARG("setfib ", cmd->arg1); 1205 break; 1206 1207 case O_REASS: 1208 printf("reass"); 1209 break; 1210 1211 case O_CALLRETURN: 1212 if (cmd->len & F_NOT) 1213 printf("return"); 1214 else 1215 PRINT_UINT_ARG("call ", cmd->arg1); 1216 break; 1217 1218 default: 1219 printf("** unrecognized action %d len %d ", 1220 cmd->opcode, cmd->len); 1221 } 1222 } 1223 if (logptr) { 1224 if (logptr->max_log > 0) 1225 printf(" log logamount %d", logptr->max_log); 1226 else 1227 printf(" log"); 1228 } 1229#ifndef NO_ALTQ 1230 if (altqptr) { 1231 print_altq_cmd(altqptr); 1232 } 1233#endif 1234 if (tagptr) { 1235 if (tagptr->len & F_NOT) 1236 PRINT_UINT_ARG(" untag ", tagptr->arg1); 1237 else 1238 PRINT_UINT_ARG(" tag ", tagptr->arg1); 1239 } 1240 1241 /* 1242 * then print the body. 1243 */ 1244 for (l = rule->act_ofs, cmd = rule->cmd ; 1245 l > 0 ; l -= F_LEN(cmd) , cmd += F_LEN(cmd)) { 1246 if ((cmd->len & F_OR) || (cmd->len & F_NOT)) 1247 continue; 1248 if (cmd->opcode == O_IP4) { 1249 flags |= HAVE_PROTO4; 1250 break; 1251 } else if (cmd->opcode == O_IP6) { 1252 flags |= HAVE_PROTO6; 1253 break; 1254 } 1255 } 1256 if (rule->_pad & 1) { /* empty rules before options */ 1257 if (!co.do_compact) { 1258 show_prerequisites(&flags, HAVE_PROTO, 0); 1259 printf(" from any to any"); 1260 } 1261 flags |= HAVE_IP | HAVE_OPTIONS | HAVE_PROTO | 1262 HAVE_SRCIP | HAVE_DSTIP; 1263 } 1264 1265 if (co.comment_only) 1266 comment = "..."; 1267 1268 for (l = rule->act_ofs, cmd = rule->cmd ; 1269 l > 0 ; l -= F_LEN(cmd) , cmd += F_LEN(cmd)) { 1270 /* useful alias */ 1271 ipfw_insn_u32 *cmd32 = (ipfw_insn_u32 *)cmd; 1272 1273 if (co.comment_only) { 1274 if (cmd->opcode != O_NOP) 1275 continue; 1276 printf(" // %s\n", (char *)(cmd + 1)); 1277 return; 1278 } 1279 1280 show_prerequisites(&flags, 0, cmd->opcode); 1281 1282 switch(cmd->opcode) { 1283 case O_PROB: 1284 break; /* done already */ 1285 1286 case O_PROBE_STATE: 1287 break; /* no need to print anything here */ 1288 1289 case O_IP_SRC: 1290 case O_IP_SRC_LOOKUP: 1291 case O_IP_SRC_MASK: 1292 case O_IP_SRC_ME: 1293 case O_IP_SRC_SET: 1294 show_prerequisites(&flags, HAVE_PROTO, 0); 1295 if (!(flags & HAVE_SRCIP)) 1296 printf(" from"); 1297 if ((cmd->len & F_OR) && !or_block) 1298 printf(" {"); 1299 print_ip((ipfw_insn_ip *)cmd, 1300 (flags & HAVE_OPTIONS) ? " src-ip" : ""); 1301 flags |= HAVE_SRCIP; 1302 break; 1303 1304 case O_IP_DST: 1305 case O_IP_DST_LOOKUP: 1306 case O_IP_DST_MASK: 1307 case O_IP_DST_ME: 1308 case O_IP_DST_SET: 1309 show_prerequisites(&flags, HAVE_PROTO|HAVE_SRCIP, 0); 1310 if (!(flags & HAVE_DSTIP)) 1311 printf(" to"); 1312 if ((cmd->len & F_OR) && !or_block) 1313 printf(" {"); 1314 print_ip((ipfw_insn_ip *)cmd, 1315 (flags & HAVE_OPTIONS) ? " dst-ip" : ""); 1316 flags |= HAVE_DSTIP; 1317 break; 1318 1319 case O_IP6_SRC: 1320 case O_IP6_SRC_MASK: 1321 case O_IP6_SRC_ME: 1322 show_prerequisites(&flags, HAVE_PROTO, 0); 1323 if (!(flags & HAVE_SRCIP)) 1324 printf(" from"); 1325 if ((cmd->len & F_OR) && !or_block) 1326 printf(" {"); 1327 print_ip6((ipfw_insn_ip6 *)cmd, 1328 (flags & HAVE_OPTIONS) ? " src-ip6" : ""); 1329 flags |= HAVE_SRCIP | HAVE_PROTO; 1330 break; 1331 1332 case O_IP6_DST: 1333 case O_IP6_DST_MASK: 1334 case O_IP6_DST_ME: 1335 show_prerequisites(&flags, HAVE_PROTO|HAVE_SRCIP, 0); 1336 if (!(flags & HAVE_DSTIP)) 1337 printf(" to"); 1338 if ((cmd->len & F_OR) && !or_block) 1339 printf(" {"); 1340 print_ip6((ipfw_insn_ip6 *)cmd, 1341 (flags & HAVE_OPTIONS) ? " dst-ip6" : ""); 1342 flags |= HAVE_DSTIP; 1343 break; 1344 1345 case O_FLOW6ID: 1346 print_flow6id( (ipfw_insn_u32 *) cmd ); 1347 flags |= HAVE_OPTIONS; 1348 break; 1349 1350 case O_IP_DSTPORT: 1351 show_prerequisites(&flags, 1352 HAVE_PROTO | HAVE_SRCIP | 1353 HAVE_DSTIP | HAVE_IP, 0); 1354 case O_IP_SRCPORT: 1355 if (flags & HAVE_DSTIP) 1356 flags |= HAVE_IP; 1357 show_prerequisites(&flags, 1358 HAVE_PROTO | HAVE_SRCIP, 0); 1359 if ((cmd->len & F_OR) && !or_block) 1360 printf(" {"); 1361 if (cmd->len & F_NOT) 1362 printf(" not"); 1363 print_newports((ipfw_insn_u16 *)cmd, proto, 1364 (flags & HAVE_OPTIONS) ? cmd->opcode : 0); 1365 break; 1366 1367 case O_PROTO: { 1368 struct protoent *pe = NULL; 1369 1370 if ((cmd->len & F_OR) && !or_block) 1371 printf(" {"); 1372 if (cmd->len & F_NOT) 1373 printf(" not"); 1374 proto = cmd->arg1; 1375 pe = getprotobynumber(cmd->arg1); 1376 if ((flags & (HAVE_PROTO4 | HAVE_PROTO6)) && 1377 !(flags & HAVE_PROTO)) 1378 show_prerequisites(&flags, 1379 HAVE_PROTO | HAVE_IP | HAVE_SRCIP | 1380 HAVE_DSTIP | HAVE_OPTIONS, 0); 1381 if (flags & HAVE_OPTIONS) 1382 printf(" proto"); 1383 if (pe) 1384 printf(" %s", pe->p_name); 1385 else 1386 printf(" %u", cmd->arg1); 1387 } 1388 flags |= HAVE_PROTO; 1389 break; 1390 1391 default: /*options ... */ 1392 if (!(cmd->len & (F_OR|F_NOT))) 1393 if (((cmd->opcode == O_IP6) && 1394 (flags & HAVE_PROTO6)) || 1395 ((cmd->opcode == O_IP4) && 1396 (flags & HAVE_PROTO4))) 1397 break; 1398 show_prerequisites(&flags, HAVE_PROTO | HAVE_SRCIP | 1399 HAVE_DSTIP | HAVE_IP | HAVE_OPTIONS, 0); 1400 if ((cmd->len & F_OR) && !or_block) 1401 printf(" {"); 1402 if (cmd->len & F_NOT && cmd->opcode != O_IN) 1403 printf(" not"); 1404 switch(cmd->opcode) { 1405 case O_MACADDR2: { 1406 ipfw_insn_mac *m = (ipfw_insn_mac *)cmd; 1407 1408 printf(" MAC"); 1409 print_mac(m->addr, m->mask); 1410 print_mac(m->addr + 6, m->mask + 6); 1411 } 1412 break; 1413 1414 case O_MAC_TYPE: 1415 print_newports((ipfw_insn_u16 *)cmd, 1416 IPPROTO_ETHERTYPE, cmd->opcode); 1417 break; 1418 1419 1420 case O_FRAG: 1421 printf(" frag"); 1422 break; 1423 1424 case O_FIB: 1425 printf(" fib %u", cmd->arg1 ); 1426 break; 1427 case O_SOCKARG: 1428 printf(" sockarg"); 1429 break; 1430 1431 case O_IN: 1432 printf(cmd->len & F_NOT ? " out" : " in"); 1433 break; 1434 1435 case O_DIVERTED: 1436 switch (cmd->arg1) { 1437 case 3: 1438 printf(" diverted"); 1439 break; 1440 case 1: 1441 printf(" diverted-loopback"); 1442 break; 1443 case 2: 1444 printf(" diverted-output"); 1445 break; 1446 default: 1447 printf(" diverted-?<%u>", cmd->arg1); 1448 break; 1449 } 1450 break; 1451 1452 case O_LAYER2: 1453 printf(" layer2"); 1454 break; 1455 case O_XMIT: 1456 case O_RECV: 1457 case O_VIA: 1458 { 1459 char const *s; 1460 ipfw_insn_if *cmdif = (ipfw_insn_if *)cmd; 1461 1462 if (cmd->opcode == O_XMIT) 1463 s = "xmit"; 1464 else if (cmd->opcode == O_RECV) 1465 s = "recv"; 1466 else /* if (cmd->opcode == O_VIA) */ 1467 s = "via"; 1468 if (cmdif->name[0] == '\0') 1469 printf(" %s %s", s, 1470 inet_ntoa(cmdif->p.ip)); 1471 else if (cmdif->name[0] == '\1') /* interface table */ 1472 printf(" %s table(%d)", s, cmdif->p.glob); 1473 else 1474 printf(" %s %s", s, cmdif->name); 1475 1476 break; 1477 } 1478 case O_IPID: 1479 if (F_LEN(cmd) == 1) 1480 printf(" ipid %u", cmd->arg1 ); 1481 else 1482 print_newports((ipfw_insn_u16 *)cmd, 0, 1483 O_IPID); 1484 break; 1485 1486 case O_IPTTL: 1487 if (F_LEN(cmd) == 1) 1488 printf(" ipttl %u", cmd->arg1 ); 1489 else 1490 print_newports((ipfw_insn_u16 *)cmd, 0, 1491 O_IPTTL); 1492 break; 1493 1494 case O_IPVER: 1495 printf(" ipver %u", cmd->arg1 ); 1496 break; 1497 1498 case O_IPPRECEDENCE: 1499 printf(" ipprecedence %u", (cmd->arg1) >> 5 ); 1500 break; 1501 1502 case O_IPLEN: 1503 if (F_LEN(cmd) == 1) 1504 printf(" iplen %u", cmd->arg1 ); 1505 else 1506 print_newports((ipfw_insn_u16 *)cmd, 0, 1507 O_IPLEN); 1508 break; 1509 1510 case O_IPOPT: 1511 print_flags("ipoptions", cmd, f_ipopts); 1512 break; 1513 1514 case O_IPTOS: 1515 print_flags("iptos", cmd, f_iptos); 1516 break; 1517 1518 case O_ICMPTYPE: 1519 print_icmptypes((ipfw_insn_u32 *)cmd); 1520 break; 1521 1522 case O_ESTAB: 1523 printf(" established"); 1524 break; 1525 1526 case O_TCPDATALEN: 1527 if (F_LEN(cmd) == 1) 1528 printf(" tcpdatalen %u", cmd->arg1 ); 1529 else 1530 print_newports((ipfw_insn_u16 *)cmd, 0, 1531 O_TCPDATALEN); 1532 break; 1533 1534 case O_TCPFLAGS: 1535 print_flags("tcpflags", cmd, f_tcpflags); 1536 break; 1537 1538 case O_TCPOPTS: 1539 print_flags("tcpoptions", cmd, f_tcpopts); 1540 break; 1541 1542 case O_TCPWIN: 1543 if (F_LEN(cmd) == 1) 1544 printf(" tcpwin %u", cmd->arg1); 1545 else 1546 print_newports((ipfw_insn_u16 *)cmd, 0, 1547 O_TCPWIN); 1548 break; 1549 1550 case O_TCPACK: 1551 printf(" tcpack %d", ntohl(cmd32->d[0])); 1552 break; 1553 1554 case O_TCPSEQ: 1555 printf(" tcpseq %d", ntohl(cmd32->d[0])); 1556 break; 1557 1558 case O_UID: 1559 { 1560 struct passwd *pwd = getpwuid(cmd32->d[0]); 1561 1562 if (pwd) 1563 printf(" uid %s", pwd->pw_name); 1564 else 1565 printf(" uid %u", cmd32->d[0]); 1566 } 1567 break; 1568 1569 case O_GID: 1570 { 1571 struct group *grp = getgrgid(cmd32->d[0]); 1572 1573 if (grp) 1574 printf(" gid %s", grp->gr_name); 1575 else 1576 printf(" gid %u", cmd32->d[0]); 1577 } 1578 break; 1579 1580 case O_JAIL: 1581 printf(" jail %d", cmd32->d[0]); 1582 break; 1583 1584 case O_VERREVPATH: 1585 printf(" verrevpath"); 1586 break; 1587 1588 case O_VERSRCREACH: 1589 printf(" versrcreach"); 1590 break; 1591 1592 case O_ANTISPOOF: 1593 printf(" antispoof"); 1594 break; 1595 1596 case O_IPSEC: 1597 printf(" ipsec"); 1598 break; 1599 1600 case O_NOP: 1601 comment = (char *)(cmd + 1); 1602 break; 1603 1604 case O_KEEP_STATE: 1605 printf(" keep-state"); 1606 break; 1607 1608 case O_LIMIT: { 1609 struct _s_x *p = limit_masks; 1610 ipfw_insn_limit *c = (ipfw_insn_limit *)cmd; 1611 uint8_t x = c->limit_mask; 1612 char const *comma = " "; 1613 1614 printf(" limit"); 1615 for (; p->x != 0 ; p++) 1616 if ((x & p->x) == p->x) { 1617 x &= ~p->x; 1618 printf("%s%s", comma, p->s); 1619 comma = ","; 1620 } 1621 PRINT_UINT_ARG(" ", c->conn_limit); 1622 break; 1623 } 1624 1625 case O_IP6: 1626 printf(" ip6"); 1627 break; 1628 1629 case O_IP4: 1630 printf(" ip4"); 1631 break; 1632 1633 case O_ICMP6TYPE: 1634 print_icmp6types((ipfw_insn_u32 *)cmd); 1635 break; 1636 1637 case O_EXT_HDR: 1638 print_ext6hdr( (ipfw_insn *) cmd ); 1639 break; 1640 1641 case O_TAGGED: 1642 if (F_LEN(cmd) == 1) 1643 PRINT_UINT_ARG(" tagged ", cmd->arg1); 1644 else 1645 print_newports((ipfw_insn_u16 *)cmd, 0, 1646 O_TAGGED); 1647 break; 1648 1649 default: 1650 printf(" [opcode %d len %d]", 1651 cmd->opcode, cmd->len); 1652 } 1653 } 1654 if (cmd->len & F_OR) { 1655 printf(" or"); 1656 or_block = 1; 1657 } else if (or_block) { 1658 printf(" }"); 1659 or_block = 0; 1660 } 1661 } 1662 show_prerequisites(&flags, HAVE_PROTO | HAVE_SRCIP | HAVE_DSTIP 1663 | HAVE_IP, 0); 1664 if (comment) 1665 printf(" // %s", comment); 1666 printf("\n"); 1667} 1668 1669static void 1670show_dyn_ipfw(ipfw_dyn_rule *d, int pcwidth, int bcwidth) 1671{ 1672 struct protoent *pe; 1673 struct in_addr a; 1674 uint16_t rulenum; 1675 char buf[INET6_ADDRSTRLEN]; 1676 1677 if (!co.do_expired) { 1678 if (!d->expire && !(d->dyn_type == O_LIMIT_PARENT)) 1679 return; 1680 } 1681 bcopy(&d->rule, &rulenum, sizeof(rulenum)); 1682 printf("%05d", rulenum); 1683 if (pcwidth > 0 || bcwidth > 0) { 1684 printf(" "); 1685 pr_u64(&d->pcnt, pcwidth); 1686 pr_u64(&d->bcnt, bcwidth); 1687 printf("(%ds)", d->expire); 1688 } 1689 switch (d->dyn_type) { 1690 case O_LIMIT_PARENT: 1691 printf(" PARENT %d", d->count); 1692 break; 1693 case O_LIMIT: 1694 printf(" LIMIT"); 1695 break; 1696 case O_KEEP_STATE: /* bidir, no mask */ 1697 printf(" STATE"); 1698 break; 1699 } 1700 1701 if ((pe = getprotobynumber(d->id.proto)) != NULL) 1702 printf(" %s", pe->p_name); 1703 else 1704 printf(" proto %u", d->id.proto); 1705 1706 if (d->id.addr_type == 4) { 1707 a.s_addr = htonl(d->id.src_ip); 1708 printf(" %s %d", inet_ntoa(a), d->id.src_port); 1709 1710 a.s_addr = htonl(d->id.dst_ip); 1711 printf(" <-> %s %d", inet_ntoa(a), d->id.dst_port); 1712 } else if (d->id.addr_type == 6) { 1713 printf(" %s %d", inet_ntop(AF_INET6, &d->id.src_ip6, buf, 1714 sizeof(buf)), d->id.src_port); 1715 printf(" <-> %s %d", inet_ntop(AF_INET6, &d->id.dst_ip6, buf, 1716 sizeof(buf)), d->id.dst_port); 1717 } else 1718 printf(" UNKNOWN <-> UNKNOWN\n"); 1719 1720 printf("\n"); 1721} 1722 1723/* 1724 * This one handles all set-related commands 1725 * ipfw set { show | enable | disable } 1726 * ipfw set swap X Y 1727 * ipfw set move X to Y 1728 * ipfw set move rule X to Y 1729 */ 1730void 1731ipfw_sets_handler(char *av[]) 1732{ 1733 uint32_t set_disable, masks[2]; 1734 int i, nbytes; 1735 uint16_t rulenum; 1736 uint8_t cmd, new_set; 1737 1738 av++; 1739 1740 if (av[0] == NULL) 1741 errx(EX_USAGE, "set needs command"); 1742 if (_substrcmp(*av, "show") == 0) { 1743 void *data = NULL; 1744 char const *msg; 1745 int nalloc; 1746 1747 nalloc = nbytes = sizeof(struct ip_fw); 1748 while (nbytes >= nalloc) { 1749 if (data) 1750 free(data); 1751 nalloc = nalloc * 2 + 200; 1752 nbytes = nalloc; 1753 data = safe_calloc(1, nbytes); 1754 if (do_cmd(IP_FW_GET, data, (uintptr_t)&nbytes) < 0) 1755 err(EX_OSERR, "getsockopt(IP_FW_GET)"); 1756 } 1757 1758 bcopy(&((struct ip_fw *)data)->next_rule, 1759 &set_disable, sizeof(set_disable)); 1760 1761 for (i = 0, msg = "disable" ; i < RESVD_SET; i++) 1762 if ((set_disable & (1<<i))) { 1763 printf("%s %d", msg, i); 1764 msg = ""; 1765 } 1766 msg = (set_disable) ? " enable" : "enable"; 1767 for (i = 0; i < RESVD_SET; i++) 1768 if (!(set_disable & (1<<i))) { 1769 printf("%s %d", msg, i); 1770 msg = ""; 1771 } 1772 printf("\n"); 1773 } else if (_substrcmp(*av, "swap") == 0) { 1774 av++; 1775 if ( av[0] == NULL || av[1] == NULL ) 1776 errx(EX_USAGE, "set swap needs 2 set numbers\n"); 1777 rulenum = atoi(av[0]); 1778 new_set = atoi(av[1]); 1779 if (!isdigit(*(av[0])) || rulenum > RESVD_SET) 1780 errx(EX_DATAERR, "invalid set number %s\n", av[0]); 1781 if (!isdigit(*(av[1])) || new_set > RESVD_SET) 1782 errx(EX_DATAERR, "invalid set number %s\n", av[1]); 1783 masks[0] = (4 << 24) | (new_set << 16) | (rulenum); 1784 i = do_cmd(IP_FW_DEL, masks, sizeof(uint32_t)); 1785 } else if (_substrcmp(*av, "move") == 0) { 1786 av++; 1787 if (av[0] && _substrcmp(*av, "rule") == 0) { 1788 cmd = 2; 1789 av++; 1790 } else 1791 cmd = 3; 1792 if (av[0] == NULL || av[1] == NULL || av[2] == NULL || 1793 av[3] != NULL || _substrcmp(av[1], "to") != 0) 1794 errx(EX_USAGE, "syntax: set move [rule] X to Y\n"); 1795 rulenum = atoi(av[0]); 1796 new_set = atoi(av[2]); 1797 if (!isdigit(*(av[0])) || (cmd == 3 && rulenum > RESVD_SET) || 1798 (cmd == 2 && rulenum == IPFW_DEFAULT_RULE) ) 1799 errx(EX_DATAERR, "invalid source number %s\n", av[0]); 1800 if (!isdigit(*(av[2])) || new_set > RESVD_SET) 1801 errx(EX_DATAERR, "invalid dest. set %s\n", av[1]); 1802 masks[0] = (cmd << 24) | (new_set << 16) | (rulenum); 1803 i = do_cmd(IP_FW_DEL, masks, sizeof(uint32_t)); 1804 } else if (_substrcmp(*av, "disable") == 0 || 1805 _substrcmp(*av, "enable") == 0 ) { 1806 int which = _substrcmp(*av, "enable") == 0 ? 1 : 0; 1807 1808 av++; 1809 masks[0] = masks[1] = 0; 1810 1811 while (av[0]) { 1812 if (isdigit(**av)) { 1813 i = atoi(*av); 1814 if (i < 0 || i > RESVD_SET) 1815 errx(EX_DATAERR, 1816 "invalid set number %d\n", i); 1817 masks[which] |= (1<<i); 1818 } else if (_substrcmp(*av, "disable") == 0) 1819 which = 0; 1820 else if (_substrcmp(*av, "enable") == 0) 1821 which = 1; 1822 else 1823 errx(EX_DATAERR, 1824 "invalid set command %s\n", *av); 1825 av++; 1826 } 1827 if ( (masks[0] & masks[1]) != 0 ) 1828 errx(EX_DATAERR, 1829 "cannot enable and disable the same set\n"); 1830 1831 i = do_cmd(IP_FW_DEL, masks, sizeof(masks)); 1832 if (i) 1833 warn("set enable/disable: setsockopt(IP_FW_DEL)"); 1834 } else 1835 errx(EX_USAGE, "invalid set command %s\n", *av); 1836} 1837 1838void 1839ipfw_sysctl_handler(char *av[], int which) 1840{ 1841 av++; 1842 1843 if (av[0] == NULL) { 1844 warnx("missing keyword to enable/disable\n"); 1845 } else if (_substrcmp(*av, "firewall") == 0) { 1846 sysctlbyname("net.inet.ip.fw.enable", NULL, 0, 1847 &which, sizeof(which)); 1848 sysctlbyname("net.inet6.ip6.fw.enable", NULL, 0, 1849 &which, sizeof(which)); 1850 } else if (_substrcmp(*av, "one_pass") == 0) { 1851 sysctlbyname("net.inet.ip.fw.one_pass", NULL, 0, 1852 &which, sizeof(which)); 1853 } else if (_substrcmp(*av, "debug") == 0) { 1854 sysctlbyname("net.inet.ip.fw.debug", NULL, 0, 1855 &which, sizeof(which)); 1856 } else if (_substrcmp(*av, "verbose") == 0) { 1857 sysctlbyname("net.inet.ip.fw.verbose", NULL, 0, 1858 &which, sizeof(which)); 1859 } else if (_substrcmp(*av, "dyn_keepalive") == 0) { 1860 sysctlbyname("net.inet.ip.fw.dyn_keepalive", NULL, 0, 1861 &which, sizeof(which)); 1862#ifndef NO_ALTQ 1863 } else if (_substrcmp(*av, "altq") == 0) { 1864 altq_set_enabled(which); 1865#endif 1866 } else { 1867 warnx("unrecognize enable/disable keyword: %s\n", *av); 1868 } 1869} 1870 1871void 1872ipfw_list(int ac, char *av[], int show_counters) 1873{ 1874 struct ip_fw *r; 1875 ipfw_dyn_rule *dynrules, *d; 1876 1877#define NEXT(r) ((struct ip_fw *)((char *)r + RULESIZE(r))) 1878 char *lim; 1879 void *data = NULL; 1880 int bcwidth, n, nbytes, nstat, ndyn, pcwidth, width; 1881 int exitval = EX_OK; 1882 int lac; 1883 char **lav; 1884 u_long rnum, last; 1885 char *endptr; 1886 int seen = 0; 1887 uint8_t set; 1888 1889 const int ocmd = co.do_pipe ? IP_DUMMYNET_GET : IP_FW_GET; 1890 int nalloc = 1024; /* start somewhere... */ 1891 1892 last = 0; 1893 1894 if (co.test_only) { 1895 fprintf(stderr, "Testing only, list disabled\n"); 1896 return; 1897 } 1898 if (co.do_pipe) { 1899 dummynet_list(ac, av, show_counters); 1900 return; 1901 } 1902 1903 ac--; 1904 av++; 1905 1906 /* get rules or pipes from kernel, resizing array as necessary */ 1907 nbytes = nalloc; 1908 1909 while (nbytes >= nalloc) { 1910 nalloc = nalloc * 2 + 200; 1911 nbytes = nalloc; 1912 data = safe_realloc(data, nbytes); 1913 if (do_cmd(ocmd, data, (uintptr_t)&nbytes) < 0) 1914 err(EX_OSERR, "getsockopt(IP_%s_GET)", 1915 co.do_pipe ? "DUMMYNET" : "FW"); 1916 } 1917 1918 /* 1919 * Count static rules. They have variable size so we 1920 * need to scan the list to count them. 1921 */ 1922 for (nstat = 1, r = data, lim = (char *)data + nbytes; 1923 r->rulenum < IPFW_DEFAULT_RULE && (char *)r < lim; 1924 ++nstat, r = NEXT(r) ) 1925 ; /* nothing */ 1926 1927 /* 1928 * Count dynamic rules. This is easier as they have 1929 * fixed size. 1930 */ 1931 r = NEXT(r); 1932 dynrules = (ipfw_dyn_rule *)r ; 1933 n = (char *)r - (char *)data; 1934 ndyn = (nbytes - n) / sizeof *dynrules; 1935 1936 /* if showing stats, figure out column widths ahead of time */ 1937 bcwidth = pcwidth = 0; 1938 if (show_counters) { 1939 for (n = 0, r = data; n < nstat; n++, r = NEXT(r)) { 1940 /* skip rules from another set */ 1941 if (co.use_set && r->set != co.use_set - 1) 1942 continue; 1943 1944 /* packet counter */ 1945 width = pr_u64(&r->pcnt, 0); 1946 if (width > pcwidth) 1947 pcwidth = width; 1948 1949 /* byte counter */ 1950 width = pr_u64(&r->bcnt, 0); 1951 if (width > bcwidth) 1952 bcwidth = width; 1953 } 1954 } 1955 if (co.do_dynamic && ndyn) { 1956 for (n = 0, d = dynrules; n < ndyn; n++, d++) { 1957 if (co.use_set) { 1958 /* skip rules from another set */ 1959 bcopy((char *)&d->rule + sizeof(uint16_t), 1960 &set, sizeof(uint8_t)); 1961 if (set != co.use_set - 1) 1962 continue; 1963 } 1964 width = pr_u64(&d->pcnt, 0); 1965 if (width > pcwidth) 1966 pcwidth = width; 1967 1968 width = pr_u64(&d->bcnt, 0); 1969 if (width > bcwidth) 1970 bcwidth = width; 1971 } 1972 } 1973 /* if no rule numbers were specified, list all rules */ 1974 if (ac == 0) { 1975 for (n = 0, r = data; n < nstat; n++, r = NEXT(r)) { 1976 if (co.use_set && r->set != co.use_set - 1) 1977 continue; 1978 show_ipfw(r, pcwidth, bcwidth); 1979 } 1980 1981 if (co.do_dynamic && ndyn) { 1982 printf("## Dynamic rules (%d):\n", ndyn); 1983 for (n = 0, d = dynrules; n < ndyn; n++, d++) { 1984 if (co.use_set) { 1985 bcopy((char *)&d->rule + sizeof(uint16_t), 1986 &set, sizeof(uint8_t)); 1987 if (set != co.use_set - 1) 1988 continue; 1989 } 1990 show_dyn_ipfw(d, pcwidth, bcwidth); 1991 } 1992 } 1993 goto done; 1994 } 1995 1996 /* display specific rules requested on command line */ 1997 1998 for (lac = ac, lav = av; lac != 0; lac--) { 1999 /* convert command line rule # */ 2000 last = rnum = strtoul(*lav++, &endptr, 10); 2001 if (*endptr == '-') 2002 last = strtoul(endptr+1, &endptr, 10); 2003 if (*endptr) { 2004 exitval = EX_USAGE; 2005 warnx("invalid rule number: %s", *(lav - 1)); 2006 continue; 2007 } 2008 for (n = seen = 0, r = data; n < nstat; n++, r = NEXT(r) ) { 2009 if (r->rulenum > last) 2010 break; 2011 if (co.use_set && r->set != co.use_set - 1) 2012 continue; 2013 if (r->rulenum >= rnum && r->rulenum <= last) { 2014 show_ipfw(r, pcwidth, bcwidth); 2015 seen = 1; 2016 } 2017 } 2018 if (!seen) { 2019 /* give precedence to other error(s) */ 2020 if (exitval == EX_OK) 2021 exitval = EX_UNAVAILABLE; 2022 warnx("rule %lu does not exist", rnum); 2023 } 2024 } 2025 2026 if (co.do_dynamic && ndyn) { 2027 printf("## Dynamic rules:\n"); 2028 for (lac = ac, lav = av; lac != 0; lac--) { 2029 last = rnum = strtoul(*lav++, &endptr, 10); 2030 if (*endptr == '-') 2031 last = strtoul(endptr+1, &endptr, 10); 2032 if (*endptr) 2033 /* already warned */ 2034 continue; 2035 for (n = 0, d = dynrules; n < ndyn; n++, d++) { 2036 uint16_t rulenum; 2037 2038 bcopy(&d->rule, &rulenum, sizeof(rulenum)); 2039 if (rulenum > rnum) 2040 break; 2041 if (co.use_set) { 2042 bcopy((char *)&d->rule + sizeof(uint16_t), 2043 &set, sizeof(uint8_t)); 2044 if (set != co.use_set - 1) 2045 continue; 2046 } 2047 if (r->rulenum >= rnum && r->rulenum <= last) 2048 show_dyn_ipfw(d, pcwidth, bcwidth); 2049 } 2050 } 2051 } 2052 2053 ac = 0; 2054 2055done: 2056 free(data); 2057 2058 if (exitval != EX_OK) 2059 exit(exitval); 2060#undef NEXT 2061} 2062 2063static int 2064lookup_host (char *host, struct in_addr *ipaddr) 2065{ 2066 struct hostent *he; 2067 2068 if (!inet_aton(host, ipaddr)) { 2069 if ((he = gethostbyname(host)) == NULL) 2070 return(-1); 2071 *ipaddr = *(struct in_addr *)he->h_addr_list[0]; 2072 } 2073 return(0); 2074} 2075 2076/* 2077 * fills the addr and mask fields in the instruction as appropriate from av. 2078 * Update length as appropriate. 2079 * The following formats are allowed: 2080 * me returns O_IP_*_ME 2081 * 1.2.3.4 single IP address 2082 * 1.2.3.4:5.6.7.8 address:mask 2083 * 1.2.3.4/24 address/mask 2084 * 1.2.3.4/26{1,6,5,4,23} set of addresses in a subnet 2085 * We can have multiple comma-separated address/mask entries. 2086 */ 2087static void 2088fill_ip(ipfw_insn_ip *cmd, char *av, int cblen) 2089{ 2090 int len = 0; 2091 uint32_t *d = ((ipfw_insn_u32 *)cmd)->d; 2092 2093 cmd->o.len &= ~F_LEN_MASK; /* zero len */ 2094 2095 if (_substrcmp(av, "any") == 0) 2096 return; 2097 2098 if (_substrcmp(av, "me") == 0) { 2099 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2100 return; 2101 } 2102 2103 if (strncmp(av, "table(", 6) == 0) { 2104 char *p = strchr(av + 6, ','); 2105 2106 if (p) 2107 *p++ = '\0'; 2108 cmd->o.opcode = O_IP_DST_LOOKUP; 2109 cmd->o.arg1 = strtoul(av + 6, NULL, 0); 2110 if (p) { 2111 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 2112 d[0] = strtoul(p, NULL, 0); 2113 } else 2114 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2115 return; 2116 } 2117 2118 while (av) { 2119 /* 2120 * After the address we can have '/' or ':' indicating a mask, 2121 * ',' indicating another address follows, '{' indicating a 2122 * set of addresses of unspecified size. 2123 */ 2124 char *t = NULL, *p = strpbrk(av, "/:,{"); 2125 int masklen; 2126 char md, nd = '\0'; 2127 2128 CHECK_LENGTH(cblen, F_INSN_SIZE(ipfw_insn) + 2 + len); 2129 2130 if (p) { 2131 md = *p; 2132 *p++ = '\0'; 2133 if ((t = strpbrk(p, ",{")) != NULL) { 2134 nd = *t; 2135 *t = '\0'; 2136 } 2137 } else 2138 md = '\0'; 2139 2140 if (lookup_host(av, (struct in_addr *)&d[0]) != 0) 2141 errx(EX_NOHOST, "hostname ``%s'' unknown", av); 2142 switch (md) { 2143 case ':': 2144 if (!inet_aton(p, (struct in_addr *)&d[1])) 2145 errx(EX_DATAERR, "bad netmask ``%s''", p); 2146 break; 2147 case '/': 2148 masklen = atoi(p); 2149 if (masklen == 0) 2150 d[1] = htonl(0); /* mask */ 2151 else if (masklen > 32) 2152 errx(EX_DATAERR, "bad width ``%s''", p); 2153 else 2154 d[1] = htonl(~0 << (32 - masklen)); 2155 break; 2156 case '{': /* no mask, assume /24 and put back the '{' */ 2157 d[1] = htonl(~0 << (32 - 24)); 2158 *(--p) = md; 2159 break; 2160 2161 case ',': /* single address plus continuation */ 2162 *(--p) = md; 2163 /* FALLTHROUGH */ 2164 case 0: /* initialization value */ 2165 default: 2166 d[1] = htonl(~0); /* force /32 */ 2167 break; 2168 } 2169 d[0] &= d[1]; /* mask base address with mask */ 2170 if (t) 2171 *t = nd; 2172 /* find next separator */ 2173 if (p) 2174 p = strpbrk(p, ",{"); 2175 if (p && *p == '{') { 2176 /* 2177 * We have a set of addresses. They are stored as follows: 2178 * arg1 is the set size (powers of 2, 2..256) 2179 * addr is the base address IN HOST FORMAT 2180 * mask.. is an array of arg1 bits (rounded up to 2181 * the next multiple of 32) with bits set 2182 * for each host in the map. 2183 */ 2184 uint32_t *map = (uint32_t *)&cmd->mask; 2185 int low, high; 2186 int i = contigmask((uint8_t *)&(d[1]), 32); 2187 2188 if (len > 0) 2189 errx(EX_DATAERR, "address set cannot be in a list"); 2190 if (i < 24 || i > 31) 2191 errx(EX_DATAERR, "invalid set with mask %d\n", i); 2192 cmd->o.arg1 = 1<<(32-i); /* map length */ 2193 d[0] = ntohl(d[0]); /* base addr in host format */ 2194 cmd->o.opcode = O_IP_DST_SET; /* default */ 2195 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + (cmd->o.arg1+31)/32; 2196 for (i = 0; i < (cmd->o.arg1+31)/32 ; i++) 2197 map[i] = 0; /* clear map */ 2198 2199 av = p + 1; 2200 low = d[0] & 0xff; 2201 high = low + cmd->o.arg1 - 1; 2202 /* 2203 * Here, i stores the previous value when we specify a range 2204 * of addresses within a mask, e.g. 45-63. i = -1 means we 2205 * have no previous value. 2206 */ 2207 i = -1; /* previous value in a range */ 2208 while (isdigit(*av)) { 2209 char *s; 2210 int a = strtol(av, &s, 0); 2211 2212 if (s == av) { /* no parameter */ 2213 if (*av != '}') 2214 errx(EX_DATAERR, "set not closed\n"); 2215 if (i != -1) 2216 errx(EX_DATAERR, "incomplete range %d-", i); 2217 break; 2218 } 2219 if (a < low || a > high) 2220 errx(EX_DATAERR, "addr %d out of range [%d-%d]\n", 2221 a, low, high); 2222 a -= low; 2223 if (i == -1) /* no previous in range */ 2224 i = a; 2225 else { /* check that range is valid */ 2226 if (i > a) 2227 errx(EX_DATAERR, "invalid range %d-%d", 2228 i+low, a+low); 2229 if (*s == '-') 2230 errx(EX_DATAERR, "double '-' in range"); 2231 } 2232 for (; i <= a; i++) 2233 map[i/32] |= 1<<(i & 31); 2234 i = -1; 2235 if (*s == '-') 2236 i = a; 2237 else if (*s == '}') 2238 break; 2239 av = s+1; 2240 } 2241 return; 2242 } 2243 av = p; 2244 if (av) /* then *av must be a ',' */ 2245 av++; 2246 2247 /* Check this entry */ 2248 if (d[1] == 0) { /* "any", specified as x.x.x.x/0 */ 2249 /* 2250 * 'any' turns the entire list into a NOP. 2251 * 'not any' never matches, so it is removed from the 2252 * list unless it is the only item, in which case we 2253 * report an error. 2254 */ 2255 if (cmd->o.len & F_NOT) { /* "not any" never matches */ 2256 if (av == NULL && len == 0) /* only this entry */ 2257 errx(EX_DATAERR, "not any never matches"); 2258 } 2259 /* else do nothing and skip this entry */ 2260 return; 2261 } 2262 /* A single IP can be stored in an optimized format */ 2263 if (d[1] == (uint32_t)~0 && av == NULL && len == 0) { 2264 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 2265 return; 2266 } 2267 len += 2; /* two words... */ 2268 d += 2; 2269 } /* end while */ 2270 if (len + 1 > F_LEN_MASK) 2271 errx(EX_DATAERR, "address list too long"); 2272 cmd->o.len |= len+1; 2273} 2274 2275 2276/* n2mask sets n bits of the mask */ 2277void 2278n2mask(struct in6_addr *mask, int n) 2279{ 2280 static int minimask[9] = 2281 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; 2282 u_char *p; 2283 2284 memset(mask, 0, sizeof(struct in6_addr)); 2285 p = (u_char *) mask; 2286 for (; n > 0; p++, n -= 8) { 2287 if (n >= 8) 2288 *p = 0xff; 2289 else 2290 *p = minimask[n]; 2291 } 2292 return; 2293} 2294 2295/* 2296 * helper function to process a set of flags and set bits in the 2297 * appropriate masks. 2298 */ 2299static void 2300fill_flags(ipfw_insn *cmd, enum ipfw_opcodes opcode, 2301 struct _s_x *flags, char *p) 2302{ 2303 uint8_t set=0, clear=0; 2304 2305 while (p && *p) { 2306 char *q; /* points to the separator */ 2307 int val; 2308 uint8_t *which; /* mask we are working on */ 2309 2310 if (*p == '!') { 2311 p++; 2312 which = &clear; 2313 } else 2314 which = &set; 2315 q = strchr(p, ','); 2316 if (q) 2317 *q++ = '\0'; 2318 val = match_token(flags, p); 2319 if (val <= 0) 2320 errx(EX_DATAERR, "invalid flag %s", p); 2321 *which |= (uint8_t)val; 2322 p = q; 2323 } 2324 cmd->opcode = opcode; 2325 cmd->len = (cmd->len & (F_NOT | F_OR)) | 1; 2326 cmd->arg1 = (set & 0xff) | ( (clear & 0xff) << 8); 2327} 2328 2329 2330void 2331ipfw_delete(char *av[]) 2332{ 2333 uint32_t rulenum; 2334 int i; 2335 int exitval = EX_OK; 2336 int do_set = 0; 2337 2338 av++; 2339 NEED1("missing rule specification"); 2340 if ( *av && _substrcmp(*av, "set") == 0) { 2341 /* Do not allow using the following syntax: 2342 * ipfw set N delete set M 2343 */ 2344 if (co.use_set) 2345 errx(EX_DATAERR, "invalid syntax"); 2346 do_set = 1; /* delete set */ 2347 av++; 2348 } 2349 2350 /* Rule number */ 2351 while (*av && isdigit(**av)) { 2352 i = atoi(*av); av++; 2353 if (co.do_nat) { 2354 exitval = do_cmd(IP_FW_NAT_DEL, &i, sizeof i); 2355 if (exitval) { 2356 exitval = EX_UNAVAILABLE; 2357 warn("rule %u not available", i); 2358 } 2359 } else if (co.do_pipe) { 2360 exitval = ipfw_delete_pipe(co.do_pipe, i); 2361 } else { 2362 if (co.use_set) 2363 rulenum = (i & 0xffff) | (5 << 24) | 2364 ((co.use_set - 1) << 16); 2365 else 2366 rulenum = (i & 0xffff) | (do_set << 24); 2367 i = do_cmd(IP_FW_DEL, &rulenum, sizeof rulenum); 2368 if (i) { 2369 exitval = EX_UNAVAILABLE; 2370 warn("rule %u: setsockopt(IP_FW_DEL)", 2371 rulenum); 2372 } 2373 } 2374 } 2375 if (exitval != EX_OK) 2376 exit(exitval); 2377} 2378 2379 2380/* 2381 * fill the interface structure. We do not check the name as we can 2382 * create interfaces dynamically, so checking them at insert time 2383 * makes relatively little sense. 2384 * Interface names containing '*', '?', or '[' are assumed to be shell 2385 * patterns which match interfaces. 2386 */ 2387static void 2388fill_iface(ipfw_insn_if *cmd, char *arg, int cblen) 2389{ 2390 cmd->name[0] = '\0'; 2391 cmd->o.len |= F_INSN_SIZE(ipfw_insn_if); 2392 2393 CHECK_CMDLEN; 2394 2395 /* Parse the interface or address */ 2396 if (strcmp(arg, "any") == 0) 2397 cmd->o.len = 0; /* effectively ignore this command */ 2398 else if (strncmp(arg, "table(", 6) == 0) { 2399 char *p = strchr(arg + 6, ','); 2400 if (p) 2401 *p++ = '\0'; 2402 cmd->name[0] = '\1'; /* Special value indicating table */ 2403 cmd->p.glob = strtoul(arg + 6, NULL, 0); 2404 } else if (!isdigit(*arg)) { 2405 strlcpy(cmd->name, arg, sizeof(cmd->name)); 2406 cmd->p.glob = strpbrk(arg, "*?[") != NULL ? 1 : 0; 2407 } else if (!inet_aton(arg, &cmd->p.ip)) 2408 errx(EX_DATAERR, "bad ip address ``%s''", arg); 2409} 2410 2411static void 2412get_mac_addr_mask(const char *p, uint8_t *addr, uint8_t *mask) 2413{ 2414 int i; 2415 size_t l; 2416 char *ap, *ptr, *optr; 2417 struct ether_addr *mac; 2418 const char *macset = "0123456789abcdefABCDEF:"; 2419 2420 if (strcmp(p, "any") == 0) { 2421 for (i = 0; i < ETHER_ADDR_LEN; i++) 2422 addr[i] = mask[i] = 0; 2423 return; 2424 } 2425 2426 optr = ptr = strdup(p); 2427 if ((ap = strsep(&ptr, "&/")) != NULL && *ap != 0) { 2428 l = strlen(ap); 2429 if (strspn(ap, macset) != l || (mac = ether_aton(ap)) == NULL) 2430 errx(EX_DATAERR, "Incorrect MAC address"); 2431 bcopy(mac, addr, ETHER_ADDR_LEN); 2432 } else 2433 errx(EX_DATAERR, "Incorrect MAC address"); 2434 2435 if (ptr != NULL) { /* we have mask? */ 2436 if (p[ptr - optr - 1] == '/') { /* mask len */ 2437 long ml = strtol(ptr, &ap, 10); 2438 if (*ap != 0 || ml > ETHER_ADDR_LEN * 8 || ml < 0) 2439 errx(EX_DATAERR, "Incorrect mask length"); 2440 for (i = 0; ml > 0 && i < ETHER_ADDR_LEN; ml -= 8, i++) 2441 mask[i] = (ml >= 8) ? 0xff: (~0) << (8 - ml); 2442 } else { /* mask */ 2443 l = strlen(ptr); 2444 if (strspn(ptr, macset) != l || 2445 (mac = ether_aton(ptr)) == NULL) 2446 errx(EX_DATAERR, "Incorrect mask"); 2447 bcopy(mac, mask, ETHER_ADDR_LEN); 2448 } 2449 } else { /* default mask: ff:ff:ff:ff:ff:ff */ 2450 for (i = 0; i < ETHER_ADDR_LEN; i++) 2451 mask[i] = 0xff; 2452 } 2453 for (i = 0; i < ETHER_ADDR_LEN; i++) 2454 addr[i] &= mask[i]; 2455 2456 free(optr); 2457} 2458 2459/* 2460 * helper function, updates the pointer to cmd with the length 2461 * of the current command, and also cleans up the first word of 2462 * the new command in case it has been clobbered before. 2463 */ 2464static ipfw_insn * 2465next_cmd(ipfw_insn *cmd, int *len) 2466{ 2467 *len -= F_LEN(cmd); 2468 CHECK_LENGTH(*len, 0); 2469 cmd += F_LEN(cmd); 2470 bzero(cmd, sizeof(*cmd)); 2471 return cmd; 2472} 2473 2474/* 2475 * Takes arguments and copies them into a comment 2476 */ 2477static void 2478fill_comment(ipfw_insn *cmd, char **av, int cblen) 2479{ 2480 int i, l; 2481 char *p = (char *)(cmd + 1); 2482 2483 cmd->opcode = O_NOP; 2484 cmd->len = (cmd->len & (F_NOT | F_OR)); 2485 2486 /* Compute length of comment string. */ 2487 for (i = 0, l = 0; av[i] != NULL; i++) 2488 l += strlen(av[i]) + 1; 2489 if (l == 0) 2490 return; 2491 if (l > 84) 2492 errx(EX_DATAERR, 2493 "comment too long (max 80 chars)"); 2494 l = 1 + (l+3)/4; 2495 cmd->len = (cmd->len & (F_NOT | F_OR)) | l; 2496 CHECK_CMDLEN; 2497 2498 for (i = 0; av[i] != NULL; i++) { 2499 strcpy(p, av[i]); 2500 p += strlen(av[i]); 2501 *p++ = ' '; 2502 } 2503 *(--p) = '\0'; 2504} 2505 2506/* 2507 * A function to fill simple commands of size 1. 2508 * Existing flags are preserved. 2509 */ 2510static void 2511fill_cmd(ipfw_insn *cmd, enum ipfw_opcodes opcode, int flags, uint16_t arg) 2512{ 2513 cmd->opcode = opcode; 2514 cmd->len = ((cmd->len | flags) & (F_NOT | F_OR)) | 1; 2515 cmd->arg1 = arg; 2516} 2517 2518/* 2519 * Fetch and add the MAC address and type, with masks. This generates one or 2520 * two microinstructions, and returns the pointer to the last one. 2521 */ 2522static ipfw_insn * 2523add_mac(ipfw_insn *cmd, char *av[], int cblen) 2524{ 2525 ipfw_insn_mac *mac; 2526 2527 if ( ( av[0] == NULL ) || ( av[1] == NULL ) ) 2528 errx(EX_DATAERR, "MAC dst src"); 2529 2530 cmd->opcode = O_MACADDR2; 2531 cmd->len = (cmd->len & (F_NOT | F_OR)) | F_INSN_SIZE(ipfw_insn_mac); 2532 CHECK_CMDLEN; 2533 2534 mac = (ipfw_insn_mac *)cmd; 2535 get_mac_addr_mask(av[0], mac->addr, mac->mask); /* dst */ 2536 get_mac_addr_mask(av[1], &(mac->addr[ETHER_ADDR_LEN]), 2537 &(mac->mask[ETHER_ADDR_LEN])); /* src */ 2538 return cmd; 2539} 2540 2541static ipfw_insn * 2542add_mactype(ipfw_insn *cmd, char *av, int cblen) 2543{ 2544 if (!av) 2545 errx(EX_DATAERR, "missing MAC type"); 2546 if (strcmp(av, "any") != 0) { /* we have a non-null type */ 2547 fill_newports((ipfw_insn_u16 *)cmd, av, IPPROTO_ETHERTYPE, 2548 cblen); 2549 cmd->opcode = O_MAC_TYPE; 2550 return cmd; 2551 } else 2552 return NULL; 2553} 2554 2555static ipfw_insn * 2556add_proto0(ipfw_insn *cmd, char *av, u_char *protop) 2557{ 2558 struct protoent *pe; 2559 char *ep; 2560 int proto; 2561 2562 proto = strtol(av, &ep, 10); 2563 if (*ep != '\0' || proto <= 0) { 2564 if ((pe = getprotobyname(av)) == NULL) 2565 return NULL; 2566 proto = pe->p_proto; 2567 } 2568 2569 fill_cmd(cmd, O_PROTO, 0, proto); 2570 *protop = proto; 2571 return cmd; 2572} 2573 2574static ipfw_insn * 2575add_proto(ipfw_insn *cmd, char *av, u_char *protop) 2576{ 2577 u_char proto = IPPROTO_IP; 2578 2579 if (_substrcmp(av, "all") == 0 || strcmp(av, "ip") == 0) 2580 ; /* do not set O_IP4 nor O_IP6 */ 2581 else if (strcmp(av, "ip4") == 0) 2582 /* explicit "just IPv4" rule */ 2583 fill_cmd(cmd, O_IP4, 0, 0); 2584 else if (strcmp(av, "ip6") == 0) { 2585 /* explicit "just IPv6" rule */ 2586 proto = IPPROTO_IPV6; 2587 fill_cmd(cmd, O_IP6, 0, 0); 2588 } else 2589 return add_proto0(cmd, av, protop); 2590 2591 *protop = proto; 2592 return cmd; 2593} 2594 2595static ipfw_insn * 2596add_proto_compat(ipfw_insn *cmd, char *av, u_char *protop) 2597{ 2598 u_char proto = IPPROTO_IP; 2599 2600 if (_substrcmp(av, "all") == 0 || strcmp(av, "ip") == 0) 2601 ; /* do not set O_IP4 nor O_IP6 */ 2602 else if (strcmp(av, "ipv4") == 0 || strcmp(av, "ip4") == 0) 2603 /* explicit "just IPv4" rule */ 2604 fill_cmd(cmd, O_IP4, 0, 0); 2605 else if (strcmp(av, "ipv6") == 0 || strcmp(av, "ip6") == 0) { 2606 /* explicit "just IPv6" rule */ 2607 proto = IPPROTO_IPV6; 2608 fill_cmd(cmd, O_IP6, 0, 0); 2609 } else 2610 return add_proto0(cmd, av, protop); 2611 2612 *protop = proto; 2613 return cmd; 2614} 2615 2616static ipfw_insn * 2617add_srcip(ipfw_insn *cmd, char *av, int cblen) 2618{ 2619 fill_ip((ipfw_insn_ip *)cmd, av, cblen); 2620 if (cmd->opcode == O_IP_DST_SET) /* set */ 2621 cmd->opcode = O_IP_SRC_SET; 2622 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */ 2623 cmd->opcode = O_IP_SRC_LOOKUP; 2624 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */ 2625 cmd->opcode = O_IP_SRC_ME; 2626 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */ 2627 cmd->opcode = O_IP_SRC; 2628 else /* addr/mask */ 2629 cmd->opcode = O_IP_SRC_MASK; 2630 return cmd; 2631} 2632 2633static ipfw_insn * 2634add_dstip(ipfw_insn *cmd, char *av, int cblen) 2635{ 2636 fill_ip((ipfw_insn_ip *)cmd, av, cblen); 2637 if (cmd->opcode == O_IP_DST_SET) /* set */ 2638 ; 2639 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */ 2640 ; 2641 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */ 2642 cmd->opcode = O_IP_DST_ME; 2643 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */ 2644 cmd->opcode = O_IP_DST; 2645 else /* addr/mask */ 2646 cmd->opcode = O_IP_DST_MASK; 2647 return cmd; 2648} 2649 2650static ipfw_insn * 2651add_ports(ipfw_insn *cmd, char *av, u_char proto, int opcode, int cblen) 2652{ 2653 /* XXX "any" is trapped before. Perhaps "to" */ 2654 if (_substrcmp(av, "any") == 0) { 2655 return NULL; 2656 } else if (fill_newports((ipfw_insn_u16 *)cmd, av, proto, cblen)) { 2657 /* XXX todo: check that we have a protocol with ports */ 2658 cmd->opcode = opcode; 2659 return cmd; 2660 } 2661 return NULL; 2662} 2663 2664static ipfw_insn * 2665add_src(ipfw_insn *cmd, char *av, u_char proto, int cblen) 2666{ 2667 struct in6_addr a; 2668 char *host, *ch; 2669 ipfw_insn *ret = NULL; 2670 2671 if ((host = strdup(av)) == NULL) 2672 return NULL; 2673 if ((ch = strrchr(host, '/')) != NULL) 2674 *ch = '\0'; 2675 2676 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 || 2677 inet_pton(AF_INET6, host, &a) == 1) 2678 ret = add_srcip6(cmd, av, cblen); 2679 /* XXX: should check for IPv4, not !IPv6 */ 2680 if (ret == NULL && (proto == IPPROTO_IP || strcmp(av, "me") == 0 || 2681 inet_pton(AF_INET6, host, &a) != 1)) 2682 ret = add_srcip(cmd, av, cblen); 2683 if (ret == NULL && strcmp(av, "any") != 0) 2684 ret = cmd; 2685 2686 free(host); 2687 return ret; 2688} 2689 2690static ipfw_insn * 2691add_dst(ipfw_insn *cmd, char *av, u_char proto, int cblen) 2692{ 2693 struct in6_addr a; 2694 char *host, *ch; 2695 ipfw_insn *ret = NULL; 2696 2697 if ((host = strdup(av)) == NULL) 2698 return NULL; 2699 if ((ch = strrchr(host, '/')) != NULL) 2700 *ch = '\0'; 2701 2702 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 || 2703 inet_pton(AF_INET6, host, &a) == 1) 2704 ret = add_dstip6(cmd, av, cblen); 2705 /* XXX: should check for IPv4, not !IPv6 */ 2706 if (ret == NULL && (proto == IPPROTO_IP || strcmp(av, "me") == 0 || 2707 inet_pton(AF_INET6, host, &a) != 1)) 2708 ret = add_dstip(cmd, av, cblen); 2709 if (ret == NULL && strcmp(av, "any") != 0) 2710 ret = cmd; 2711 2712 free(host); 2713 return ret; 2714} 2715 2716/* 2717 * Parse arguments and assemble the microinstructions which make up a rule. 2718 * Rules are added into the 'rulebuf' and then copied in the correct order 2719 * into the actual rule. 2720 * 2721 * The syntax for a rule starts with the action, followed by 2722 * optional action parameters, and the various match patterns. 2723 * In the assembled microcode, the first opcode must be an O_PROBE_STATE 2724 * (generated if the rule includes a keep-state option), then the 2725 * various match patterns, log/altq actions, and the actual action. 2726 * 2727 */ 2728void 2729ipfw_add(char *av[]) 2730{ 2731 /* 2732 * rules are added into the 'rulebuf' and then copied in 2733 * the correct order into the actual rule. 2734 * Some things that need to go out of order (prob, action etc.) 2735 * go into actbuf[]. 2736 */ 2737 static uint32_t rulebuf[255], actbuf[255], cmdbuf[255]; 2738 int rblen, ablen, cblen; 2739 2740 ipfw_insn *src, *dst, *cmd, *action, *prev=NULL; 2741 ipfw_insn *first_cmd; /* first match pattern */ 2742 2743 struct ip_fw *rule; 2744 2745 /* 2746 * various flags used to record that we entered some fields. 2747 */ 2748 ipfw_insn *have_state = NULL; /* check-state or keep-state */ 2749 ipfw_insn *have_log = NULL, *have_altq = NULL, *have_tag = NULL; 2750 size_t len; 2751 2752 int i; 2753 2754 int open_par = 0; /* open parenthesis ( */ 2755 2756 /* proto is here because it is used to fetch ports */ 2757 u_char proto = IPPROTO_IP; /* default protocol */ 2758 2759 double match_prob = 1; /* match probability, default is always match */ 2760 2761 bzero(actbuf, sizeof(actbuf)); /* actions go here */ 2762 bzero(cmdbuf, sizeof(cmdbuf)); 2763 bzero(rulebuf, sizeof(rulebuf)); 2764 2765 rule = (struct ip_fw *)rulebuf; 2766 cmd = (ipfw_insn *)cmdbuf; 2767 action = (ipfw_insn *)actbuf; 2768 2769 rblen = sizeof(rulebuf) / sizeof(rulebuf[0]); 2770 rblen -= offsetof(struct ip_fw, cmd) / sizeof(rulebuf[0]); 2771 ablen = sizeof(actbuf) / sizeof(actbuf[0]); 2772 cblen = sizeof(cmdbuf) / sizeof(cmdbuf[0]); 2773 cblen -= F_INSN_SIZE(ipfw_insn_u32) + 1; 2774 2775#define CHECK_RBUFLEN(len) { CHECK_LENGTH(rblen, len); rblen -= len; } 2776#define CHECK_ACTLEN CHECK_LENGTH(ablen, action->len) 2777 2778 av++; 2779 2780 /* [rule N] -- Rule number optional */ 2781 if (av[0] && isdigit(**av)) { 2782 rule->rulenum = atoi(*av); 2783 av++; 2784 } 2785 2786 /* [set N] -- set number (0..RESVD_SET), optional */ 2787 if (av[0] && av[1] && _substrcmp(*av, "set") == 0) { 2788 int set = strtoul(av[1], NULL, 10); 2789 if (set < 0 || set > RESVD_SET) 2790 errx(EX_DATAERR, "illegal set %s", av[1]); 2791 rule->set = set; 2792 av += 2; 2793 } 2794 2795 /* [prob D] -- match probability, optional */ 2796 if (av[0] && av[1] && _substrcmp(*av, "prob") == 0) { 2797 match_prob = strtod(av[1], NULL); 2798 2799 if (match_prob <= 0 || match_prob > 1) 2800 errx(EX_DATAERR, "illegal match prob. %s", av[1]); 2801 av += 2; 2802 } 2803 2804 /* action -- mandatory */ 2805 NEED1("missing action"); 2806 i = match_token(rule_actions, *av); 2807 av++; 2808 action->len = 1; /* default */ 2809 CHECK_ACTLEN; 2810 switch(i) { 2811 case TOK_CHECKSTATE: 2812 have_state = action; 2813 action->opcode = O_CHECK_STATE; 2814 break; 2815 2816 case TOK_ACCEPT: 2817 action->opcode = O_ACCEPT; 2818 break; 2819 2820 case TOK_DENY: 2821 action->opcode = O_DENY; 2822 action->arg1 = 0; 2823 break; 2824 2825 case TOK_REJECT: 2826 action->opcode = O_REJECT; 2827 action->arg1 = ICMP_UNREACH_HOST; 2828 break; 2829 2830 case TOK_RESET: 2831 action->opcode = O_REJECT; 2832 action->arg1 = ICMP_REJECT_RST; 2833 break; 2834 2835 case TOK_RESET6: 2836 action->opcode = O_UNREACH6; 2837 action->arg1 = ICMP6_UNREACH_RST; 2838 break; 2839 2840 case TOK_UNREACH: 2841 action->opcode = O_REJECT; 2842 NEED1("missing reject code"); 2843 fill_reject_code(&action->arg1, *av); 2844 av++; 2845 break; 2846 2847 case TOK_UNREACH6: 2848 action->opcode = O_UNREACH6; 2849 NEED1("missing unreach code"); 2850 fill_unreach6_code(&action->arg1, *av); 2851 av++; 2852 break; 2853 2854 case TOK_COUNT: 2855 action->opcode = O_COUNT; 2856 break; 2857 2858 case TOK_NAT: 2859 action->opcode = O_NAT; 2860 action->len = F_INSN_SIZE(ipfw_insn_nat); 2861 CHECK_ACTLEN; 2862 if (_substrcmp(*av, "global") == 0) { 2863 action->arg1 = 0; 2864 av++; 2865 break; 2866 } else 2867 goto chkarg; 2868 2869 case TOK_QUEUE: 2870 action->opcode = O_QUEUE; 2871 goto chkarg; 2872 case TOK_PIPE: 2873 action->opcode = O_PIPE; 2874 goto chkarg; 2875 case TOK_SKIPTO: 2876 action->opcode = O_SKIPTO; 2877 goto chkarg; 2878 case TOK_NETGRAPH: 2879 action->opcode = O_NETGRAPH; 2880 goto chkarg; 2881 case TOK_NGTEE: 2882 action->opcode = O_NGTEE; 2883 goto chkarg; 2884 case TOK_DIVERT: 2885 action->opcode = O_DIVERT; 2886 goto chkarg; 2887 case TOK_TEE: 2888 action->opcode = O_TEE; 2889 goto chkarg; 2890 case TOK_CALL: 2891 action->opcode = O_CALLRETURN; 2892chkarg: 2893 if (!av[0]) 2894 errx(EX_USAGE, "missing argument for %s", *(av - 1)); 2895 if (isdigit(**av)) { 2896 action->arg1 = strtoul(*av, NULL, 10); 2897 if (action->arg1 <= 0 || action->arg1 >= IP_FW_TABLEARG) 2898 errx(EX_DATAERR, "illegal argument for %s", 2899 *(av - 1)); 2900 } else if (_substrcmp(*av, "tablearg") == 0) { 2901 action->arg1 = IP_FW_TABLEARG; 2902 } else if (i == TOK_DIVERT || i == TOK_TEE) { 2903 struct servent *s; 2904 setservent(1); 2905 s = getservbyname(av[0], "divert"); 2906 if (s != NULL) 2907 action->arg1 = ntohs(s->s_port); 2908 else 2909 errx(EX_DATAERR, "illegal divert/tee port"); 2910 } else 2911 errx(EX_DATAERR, "illegal argument for %s", *(av - 1)); 2912 av++; 2913 break; 2914 2915 case TOK_FORWARD: { 2916 /* 2917 * Locate the address-port separator (':' or ','). 2918 * Could be one of the following: 2919 * hostname:port 2920 * IPv4 a.b.c.d,port 2921 * IPv4 a.b.c.d:port 2922 * IPv6 w:x:y::z,port 2923 * The ':' can only be used with hostname and IPv4 address. 2924 * XXX-BZ Should we also support [w:x:y::z]:port? 2925 */ 2926 struct sockaddr_storage result; 2927 struct addrinfo *res; 2928 char *s, *end; 2929 int family; 2930 u_short port_number; 2931 2932 NEED1("missing forward address[:port]"); 2933 2934 /* 2935 * locate the address-port separator (':' or ',') 2936 */ 2937 s = strchr(*av, ','); 2938 if (s == NULL) { 2939 /* Distinguish between IPv4:port and IPv6 cases. */ 2940 s = strchr(*av, ':'); 2941 if (s && strchr(s+1, ':')) 2942 s = NULL; /* no port */ 2943 } 2944 2945 port_number = 0; 2946 if (s != NULL) { 2947 /* Terminate host portion and set s to start of port. */ 2948 *(s++) = '\0'; 2949 i = strtoport(s, &end, 0 /* base */, 0 /* proto */); 2950 if (s == end) 2951 errx(EX_DATAERR, 2952 "illegal forwarding port ``%s''", s); 2953 port_number = (u_short)i; 2954 } 2955 2956 if (_substrcmp(*av, "tablearg") == 0) { 2957 family = PF_INET; 2958 ((struct sockaddr_in*)&result)->sin_addr.s_addr = 2959 INADDR_ANY; 2960 } else { 2961 /* 2962 * Resolve the host name or address to a family and a 2963 * network representation of the addres. 2964 */ 2965 if (getaddrinfo(*av, NULL, NULL, &res)) 2966 errx(EX_DATAERR, NULL); 2967 /* Just use the first host in the answer. */ 2968 family = res->ai_family; 2969 memcpy(&result, res->ai_addr, res->ai_addrlen); 2970 freeaddrinfo(res); 2971 } 2972 2973 if (family == PF_INET) { 2974 ipfw_insn_sa *p = (ipfw_insn_sa *)action; 2975 2976 action->opcode = O_FORWARD_IP; 2977 action->len = F_INSN_SIZE(ipfw_insn_sa); 2978 CHECK_ACTLEN; 2979 2980 /* 2981 * In the kernel we assume AF_INET and use only 2982 * sin_port and sin_addr. Remember to set sin_len as 2983 * the routing code seems to use it too. 2984 */ 2985 p->sa.sin_len = sizeof(struct sockaddr_in); 2986 p->sa.sin_family = AF_INET; 2987 p->sa.sin_port = port_number; 2988 p->sa.sin_addr.s_addr = 2989 ((struct sockaddr_in *)&result)->sin_addr.s_addr; 2990 } else if (family == PF_INET6) { 2991 ipfw_insn_sa6 *p = (ipfw_insn_sa6 *)action; 2992 2993 action->opcode = O_FORWARD_IP6; 2994 action->len = F_INSN_SIZE(ipfw_insn_sa6); 2995 CHECK_ACTLEN; 2996 2997 p->sa.sin6_len = sizeof(struct sockaddr_in6); 2998 p->sa.sin6_family = AF_INET6; 2999 p->sa.sin6_port = port_number; 3000 p->sa.sin6_flowinfo = 0; 3001 p->sa.sin6_scope_id = 0; 3002 /* No table support for v6 yet. */ 3003 bcopy(&((struct sockaddr_in6*)&result)->sin6_addr, 3004 &p->sa.sin6_addr, sizeof(p->sa.sin6_addr)); 3005 } else { 3006 errx(EX_DATAERR, "Invalid address family in forward action"); 3007 } 3008 av++; 3009 break; 3010 } 3011 case TOK_COMMENT: 3012 /* pretend it is a 'count' rule followed by the comment */ 3013 action->opcode = O_COUNT; 3014 av--; /* go back... */ 3015 break; 3016 3017 case TOK_SETFIB: 3018 { 3019 int numfibs; 3020 size_t intsize = sizeof(int); 3021 3022 action->opcode = O_SETFIB; 3023 NEED1("missing fib number"); 3024 if (_substrcmp(*av, "tablearg") == 0) { 3025 action->arg1 = IP_FW_TABLEARG; 3026 } else { 3027 action->arg1 = strtoul(*av, NULL, 10); 3028 if (sysctlbyname("net.fibs", &numfibs, &intsize, 3029 NULL, 0) == -1) 3030 errx(EX_DATAERR, "fibs not suported.\n"); 3031 if (action->arg1 >= numfibs) /* Temporary */ 3032 errx(EX_DATAERR, "fib too large.\n"); 3033 } 3034 av++; 3035 break; 3036 } 3037 3038 case TOK_REASS: 3039 action->opcode = O_REASS; 3040 break; 3041 3042 case TOK_RETURN: 3043 fill_cmd(action, O_CALLRETURN, F_NOT, 0); 3044 break; 3045 3046 default: 3047 errx(EX_DATAERR, "invalid action %s\n", av[-1]); 3048 } 3049 action = next_cmd(action, &ablen); 3050 3051 /* 3052 * [altq queuename] -- altq tag, optional 3053 * [log [logamount N]] -- log, optional 3054 * 3055 * If they exist, it go first in the cmdbuf, but then it is 3056 * skipped in the copy section to the end of the buffer. 3057 */ 3058 while (av[0] != NULL && (i = match_token(rule_action_params, *av)) != -1) { 3059 av++; 3060 switch (i) { 3061 case TOK_LOG: 3062 { 3063 ipfw_insn_log *c = (ipfw_insn_log *)cmd; 3064 int l; 3065 3066 if (have_log) 3067 errx(EX_DATAERR, 3068 "log cannot be specified more than once"); 3069 have_log = (ipfw_insn *)c; 3070 cmd->len = F_INSN_SIZE(ipfw_insn_log); 3071 CHECK_CMDLEN; 3072 cmd->opcode = O_LOG; 3073 if (av[0] && _substrcmp(*av, "logamount") == 0) { 3074 av++; 3075 NEED1("logamount requires argument"); 3076 l = atoi(*av); 3077 if (l < 0) 3078 errx(EX_DATAERR, 3079 "logamount must be positive"); 3080 c->max_log = l; 3081 av++; 3082 } else { 3083 len = sizeof(c->max_log); 3084 if (sysctlbyname("net.inet.ip.fw.verbose_limit", 3085 &c->max_log, &len, NULL, 0) == -1) { 3086 if (co.test_only) { 3087 c->max_log = 0; 3088 break; 3089 } 3090 errx(1, "sysctlbyname(\"%s\")", 3091 "net.inet.ip.fw.verbose_limit"); 3092 } 3093 } 3094 } 3095 break; 3096 3097#ifndef NO_ALTQ 3098 case TOK_ALTQ: 3099 { 3100 ipfw_insn_altq *a = (ipfw_insn_altq *)cmd; 3101 3102 NEED1("missing altq queue name"); 3103 if (have_altq) 3104 errx(EX_DATAERR, 3105 "altq cannot be specified more than once"); 3106 have_altq = (ipfw_insn *)a; 3107 cmd->len = F_INSN_SIZE(ipfw_insn_altq); 3108 CHECK_CMDLEN; 3109 cmd->opcode = O_ALTQ; 3110 a->qid = altq_name_to_qid(*av); 3111 av++; 3112 } 3113 break; 3114#endif 3115 3116 case TOK_TAG: 3117 case TOK_UNTAG: { 3118 uint16_t tag; 3119 3120 if (have_tag) 3121 errx(EX_USAGE, "tag and untag cannot be " 3122 "specified more than once"); 3123 GET_UINT_ARG(tag, IPFW_ARG_MIN, IPFW_ARG_MAX, i, 3124 rule_action_params); 3125 have_tag = cmd; 3126 fill_cmd(cmd, O_TAG, (i == TOK_TAG) ? 0: F_NOT, tag); 3127 av++; 3128 break; 3129 } 3130 3131 default: 3132 abort(); 3133 } 3134 cmd = next_cmd(cmd, &cblen); 3135 } 3136 3137 if (have_state) /* must be a check-state, we are done */ 3138 goto done; 3139 3140#define OR_START(target) \ 3141 if (av[0] && (*av[0] == '(' || *av[0] == '{')) { \ 3142 if (open_par) \ 3143 errx(EX_USAGE, "nested \"(\" not allowed\n"); \ 3144 prev = NULL; \ 3145 open_par = 1; \ 3146 if ( (av[0])[1] == '\0') { \ 3147 av++; \ 3148 } else \ 3149 (*av)++; \ 3150 } \ 3151 target: \ 3152 3153 3154#define CLOSE_PAR \ 3155 if (open_par) { \ 3156 if (av[0] && ( \ 3157 strcmp(*av, ")") == 0 || \ 3158 strcmp(*av, "}") == 0)) { \ 3159 prev = NULL; \ 3160 open_par = 0; \ 3161 av++; \ 3162 } else \ 3163 errx(EX_USAGE, "missing \")\"\n"); \ 3164 } 3165 3166#define NOT_BLOCK \ 3167 if (av[0] && _substrcmp(*av, "not") == 0) { \ 3168 if (cmd->len & F_NOT) \ 3169 errx(EX_USAGE, "double \"not\" not allowed\n"); \ 3170 cmd->len |= F_NOT; \ 3171 av++; \ 3172 } 3173 3174#define OR_BLOCK(target) \ 3175 if (av[0] && _substrcmp(*av, "or") == 0) { \ 3176 if (prev == NULL || open_par == 0) \ 3177 errx(EX_DATAERR, "invalid OR block"); \ 3178 prev->len |= F_OR; \ 3179 av++; \ 3180 goto target; \ 3181 } \ 3182 CLOSE_PAR; 3183 3184 first_cmd = cmd; 3185 3186#if 0 3187 /* 3188 * MAC addresses, optional. 3189 * If we have this, we skip the part "proto from src to dst" 3190 * and jump straight to the option parsing. 3191 */ 3192 NOT_BLOCK; 3193 NEED1("missing protocol"); 3194 if (_substrcmp(*av, "MAC") == 0 || 3195 _substrcmp(*av, "mac") == 0) { 3196 av++; /* the "MAC" keyword */ 3197 add_mac(cmd, av); /* exits in case of errors */ 3198 cmd = next_cmd(cmd); 3199 av += 2; /* dst-mac and src-mac */ 3200 NOT_BLOCK; 3201 NEED1("missing mac type"); 3202 if (add_mactype(cmd, av[0])) 3203 cmd = next_cmd(cmd); 3204 av++; /* any or mac-type */ 3205 goto read_options; 3206 } 3207#endif 3208 3209 /* 3210 * protocol, mandatory 3211 */ 3212 OR_START(get_proto); 3213 NOT_BLOCK; 3214 NEED1("missing protocol"); 3215 if (add_proto_compat(cmd, *av, &proto)) { 3216 av++; 3217 if (F_LEN(cmd) != 0) { 3218 prev = cmd; 3219 cmd = next_cmd(cmd, &cblen); 3220 } 3221 } else if (first_cmd != cmd) { 3222 errx(EX_DATAERR, "invalid protocol ``%s''", *av); 3223 } else 3224 goto read_options; 3225 OR_BLOCK(get_proto); 3226 3227 /* 3228 * "from", mandatory 3229 */ 3230 if ((av[0] == NULL) || _substrcmp(*av, "from") != 0) 3231 errx(EX_USAGE, "missing ``from''"); 3232 av++; 3233 3234 /* 3235 * source IP, mandatory 3236 */ 3237 OR_START(source_ip); 3238 NOT_BLOCK; /* optional "not" */ 3239 NEED1("missing source address"); 3240 if (add_src(cmd, *av, proto, cblen)) { 3241 av++; 3242 if (F_LEN(cmd) != 0) { /* ! any */ 3243 prev = cmd; 3244 cmd = next_cmd(cmd, &cblen); 3245 } 3246 } else 3247 errx(EX_USAGE, "bad source address %s", *av); 3248 OR_BLOCK(source_ip); 3249 3250 /* 3251 * source ports, optional 3252 */ 3253 NOT_BLOCK; /* optional "not" */ 3254 if ( av[0] != NULL ) { 3255 if (_substrcmp(*av, "any") == 0 || 3256 add_ports(cmd, *av, proto, O_IP_SRCPORT, cblen)) { 3257 av++; 3258 if (F_LEN(cmd) != 0) 3259 cmd = next_cmd(cmd, &cblen); 3260 } 3261 } 3262 3263 /* 3264 * "to", mandatory 3265 */ 3266 if ( (av[0] == NULL) || _substrcmp(*av, "to") != 0 ) 3267 errx(EX_USAGE, "missing ``to''"); 3268 av++; 3269 3270 /* 3271 * destination, mandatory 3272 */ 3273 OR_START(dest_ip); 3274 NOT_BLOCK; /* optional "not" */ 3275 NEED1("missing dst address"); 3276 if (add_dst(cmd, *av, proto, cblen)) { 3277 av++; 3278 if (F_LEN(cmd) != 0) { /* ! any */ 3279 prev = cmd; 3280 cmd = next_cmd(cmd, &cblen); 3281 } 3282 } else 3283 errx( EX_USAGE, "bad destination address %s", *av); 3284 OR_BLOCK(dest_ip); 3285 3286 /* 3287 * dest. ports, optional 3288 */ 3289 NOT_BLOCK; /* optional "not" */ 3290 if (av[0]) { 3291 if (_substrcmp(*av, "any") == 0 || 3292 add_ports(cmd, *av, proto, O_IP_DSTPORT, cblen)) { 3293 av++; 3294 if (F_LEN(cmd) != 0) 3295 cmd = next_cmd(cmd, &cblen); 3296 } 3297 } 3298 3299read_options: 3300 if (av[0] && first_cmd == cmd) { 3301 /* 3302 * nothing specified so far, store in the rule to ease 3303 * printout later. 3304 */ 3305 rule->_pad = 1; 3306 } 3307 prev = NULL; 3308 while ( av[0] != NULL ) { 3309 char *s; 3310 ipfw_insn_u32 *cmd32; /* alias for cmd */ 3311 3312 s = *av; 3313 cmd32 = (ipfw_insn_u32 *)cmd; 3314 3315 if (*s == '!') { /* alternate syntax for NOT */ 3316 if (cmd->len & F_NOT) 3317 errx(EX_USAGE, "double \"not\" not allowed\n"); 3318 cmd->len = F_NOT; 3319 s++; 3320 } 3321 i = match_token(rule_options, s); 3322 av++; 3323 switch(i) { 3324 case TOK_NOT: 3325 if (cmd->len & F_NOT) 3326 errx(EX_USAGE, "double \"not\" not allowed\n"); 3327 cmd->len = F_NOT; 3328 break; 3329 3330 case TOK_OR: 3331 if (open_par == 0 || prev == NULL) 3332 errx(EX_USAGE, "invalid \"or\" block\n"); 3333 prev->len |= F_OR; 3334 break; 3335 3336 case TOK_STARTBRACE: 3337 if (open_par) 3338 errx(EX_USAGE, "+nested \"(\" not allowed\n"); 3339 open_par = 1; 3340 break; 3341 3342 case TOK_ENDBRACE: 3343 if (!open_par) 3344 errx(EX_USAGE, "+missing \")\"\n"); 3345 open_par = 0; 3346 prev = NULL; 3347 break; 3348 3349 case TOK_IN: 3350 fill_cmd(cmd, O_IN, 0, 0); 3351 break; 3352 3353 case TOK_OUT: 3354 cmd->len ^= F_NOT; /* toggle F_NOT */ 3355 fill_cmd(cmd, O_IN, 0, 0); 3356 break; 3357 3358 case TOK_DIVERTED: 3359 fill_cmd(cmd, O_DIVERTED, 0, 3); 3360 break; 3361 3362 case TOK_DIVERTEDLOOPBACK: 3363 fill_cmd(cmd, O_DIVERTED, 0, 1); 3364 break; 3365 3366 case TOK_DIVERTEDOUTPUT: 3367 fill_cmd(cmd, O_DIVERTED, 0, 2); 3368 break; 3369 3370 case TOK_FRAG: 3371 fill_cmd(cmd, O_FRAG, 0, 0); 3372 break; 3373 3374 case TOK_LAYER2: 3375 fill_cmd(cmd, O_LAYER2, 0, 0); 3376 break; 3377 3378 case TOK_XMIT: 3379 case TOK_RECV: 3380 case TOK_VIA: 3381 NEED1("recv, xmit, via require interface name" 3382 " or address"); 3383 fill_iface((ipfw_insn_if *)cmd, av[0], cblen); 3384 av++; 3385 if (F_LEN(cmd) == 0) /* not a valid address */ 3386 break; 3387 if (i == TOK_XMIT) 3388 cmd->opcode = O_XMIT; 3389 else if (i == TOK_RECV) 3390 cmd->opcode = O_RECV; 3391 else if (i == TOK_VIA) 3392 cmd->opcode = O_VIA; 3393 break; 3394 3395 case TOK_ICMPTYPES: 3396 NEED1("icmptypes requires list of types"); 3397 fill_icmptypes((ipfw_insn_u32 *)cmd, *av); 3398 av++; 3399 break; 3400 3401 case TOK_ICMP6TYPES: 3402 NEED1("icmptypes requires list of types"); 3403 fill_icmp6types((ipfw_insn_icmp6 *)cmd, *av, cblen); 3404 av++; 3405 break; 3406 3407 case TOK_IPTTL: 3408 NEED1("ipttl requires TTL"); 3409 if (strpbrk(*av, "-,")) { 3410 if (!add_ports(cmd, *av, 0, O_IPTTL, cblen)) 3411 errx(EX_DATAERR, "invalid ipttl %s", *av); 3412 } else 3413 fill_cmd(cmd, O_IPTTL, 0, strtoul(*av, NULL, 0)); 3414 av++; 3415 break; 3416 3417 case TOK_IPID: 3418 NEED1("ipid requires id"); 3419 if (strpbrk(*av, "-,")) { 3420 if (!add_ports(cmd, *av, 0, O_IPID, cblen)) 3421 errx(EX_DATAERR, "invalid ipid %s", *av); 3422 } else 3423 fill_cmd(cmd, O_IPID, 0, strtoul(*av, NULL, 0)); 3424 av++; 3425 break; 3426 3427 case TOK_IPLEN: 3428 NEED1("iplen requires length"); 3429 if (strpbrk(*av, "-,")) { 3430 if (!add_ports(cmd, *av, 0, O_IPLEN, cblen)) 3431 errx(EX_DATAERR, "invalid ip len %s", *av); 3432 } else 3433 fill_cmd(cmd, O_IPLEN, 0, strtoul(*av, NULL, 0)); 3434 av++; 3435 break; 3436 3437 case TOK_IPVER: 3438 NEED1("ipver requires version"); 3439 fill_cmd(cmd, O_IPVER, 0, strtoul(*av, NULL, 0)); 3440 av++; 3441 break; 3442 3443 case TOK_IPPRECEDENCE: 3444 NEED1("ipprecedence requires value"); 3445 fill_cmd(cmd, O_IPPRECEDENCE, 0, 3446 (strtoul(*av, NULL, 0) & 7) << 5); 3447 av++; 3448 break; 3449 3450 case TOK_IPOPTS: 3451 NEED1("missing argument for ipoptions"); 3452 fill_flags(cmd, O_IPOPT, f_ipopts, *av); 3453 av++; 3454 break; 3455 3456 case TOK_IPTOS: 3457 NEED1("missing argument for iptos"); 3458 fill_flags(cmd, O_IPTOS, f_iptos, *av); 3459 av++; 3460 break; 3461 3462 case TOK_UID: 3463 NEED1("uid requires argument"); 3464 { 3465 char *end; 3466 uid_t uid; 3467 struct passwd *pwd; 3468 3469 cmd->opcode = O_UID; 3470 uid = strtoul(*av, &end, 0); 3471 pwd = (*end == '\0') ? getpwuid(uid) : getpwnam(*av); 3472 if (pwd == NULL) 3473 errx(EX_DATAERR, "uid \"%s\" nonexistent", *av); 3474 cmd32->d[0] = pwd->pw_uid; 3475 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 3476 av++; 3477 } 3478 break; 3479 3480 case TOK_GID: 3481 NEED1("gid requires argument"); 3482 { 3483 char *end; 3484 gid_t gid; 3485 struct group *grp; 3486 3487 cmd->opcode = O_GID; 3488 gid = strtoul(*av, &end, 0); 3489 grp = (*end == '\0') ? getgrgid(gid) : getgrnam(*av); 3490 if (grp == NULL) 3491 errx(EX_DATAERR, "gid \"%s\" nonexistent", *av); 3492 cmd32->d[0] = grp->gr_gid; 3493 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 3494 av++; 3495 } 3496 break; 3497 3498 case TOK_JAIL: 3499 NEED1("jail requires argument"); 3500 { 3501 char *end; 3502 int jid; 3503 3504 cmd->opcode = O_JAIL; 3505 jid = (int)strtol(*av, &end, 0); 3506 if (jid < 0 || *end != '\0') 3507 errx(EX_DATAERR, "jail requires prison ID"); 3508 cmd32->d[0] = (uint32_t)jid; 3509 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 3510 av++; 3511 } 3512 break; 3513 3514 case TOK_ESTAB: 3515 fill_cmd(cmd, O_ESTAB, 0, 0); 3516 break; 3517 3518 case TOK_SETUP: 3519 fill_cmd(cmd, O_TCPFLAGS, 0, 3520 (TH_SYN) | ( (TH_ACK) & 0xff) <<8 ); 3521 break; 3522 3523 case TOK_TCPDATALEN: 3524 NEED1("tcpdatalen requires length"); 3525 if (strpbrk(*av, "-,")) { 3526 if (!add_ports(cmd, *av, 0, O_TCPDATALEN, cblen)) 3527 errx(EX_DATAERR, "invalid tcpdata len %s", *av); 3528 } else 3529 fill_cmd(cmd, O_TCPDATALEN, 0, 3530 strtoul(*av, NULL, 0)); 3531 av++; 3532 break; 3533 3534 case TOK_TCPOPTS: 3535 NEED1("missing argument for tcpoptions"); 3536 fill_flags(cmd, O_TCPOPTS, f_tcpopts, *av); 3537 av++; 3538 break; 3539 3540 case TOK_TCPSEQ: 3541 case TOK_TCPACK: 3542 NEED1("tcpseq/tcpack requires argument"); 3543 cmd->len = F_INSN_SIZE(ipfw_insn_u32); 3544 cmd->opcode = (i == TOK_TCPSEQ) ? O_TCPSEQ : O_TCPACK; 3545 cmd32->d[0] = htonl(strtoul(*av, NULL, 0)); 3546 av++; 3547 break; 3548 3549 case TOK_TCPWIN: 3550 NEED1("tcpwin requires length"); 3551 if (strpbrk(*av, "-,")) { 3552 if (!add_ports(cmd, *av, 0, O_TCPWIN, cblen)) 3553 errx(EX_DATAERR, "invalid tcpwin len %s", *av); 3554 } else 3555 fill_cmd(cmd, O_TCPWIN, 0, 3556 strtoul(*av, NULL, 0)); 3557 av++; 3558 break; 3559 3560 case TOK_TCPFLAGS: 3561 NEED1("missing argument for tcpflags"); 3562 cmd->opcode = O_TCPFLAGS; 3563 fill_flags(cmd, O_TCPFLAGS, f_tcpflags, *av); 3564 av++; 3565 break; 3566 3567 case TOK_KEEPSTATE: 3568 if (open_par) 3569 errx(EX_USAGE, "keep-state cannot be part " 3570 "of an or block"); 3571 if (have_state) 3572 errx(EX_USAGE, "only one of keep-state " 3573 "and limit is allowed"); 3574 have_state = cmd; 3575 fill_cmd(cmd, O_KEEP_STATE, 0, 0); 3576 break; 3577 3578 case TOK_LIMIT: { 3579 ipfw_insn_limit *c = (ipfw_insn_limit *)cmd; 3580 int val; 3581 3582 if (open_par) 3583 errx(EX_USAGE, 3584 "limit cannot be part of an or block"); 3585 if (have_state) 3586 errx(EX_USAGE, "only one of keep-state and " 3587 "limit is allowed"); 3588 have_state = cmd; 3589 3590 cmd->len = F_INSN_SIZE(ipfw_insn_limit); 3591 CHECK_CMDLEN; 3592 cmd->opcode = O_LIMIT; 3593 c->limit_mask = c->conn_limit = 0; 3594 3595 while ( av[0] != NULL ) { 3596 if ((val = match_token(limit_masks, *av)) <= 0) 3597 break; 3598 c->limit_mask |= val; 3599 av++; 3600 } 3601 3602 if (c->limit_mask == 0) 3603 errx(EX_USAGE, "limit: missing limit mask"); 3604 3605 GET_UINT_ARG(c->conn_limit, IPFW_ARG_MIN, IPFW_ARG_MAX, 3606 TOK_LIMIT, rule_options); 3607 3608 av++; 3609 break; 3610 } 3611 3612 case TOK_PROTO: 3613 NEED1("missing protocol"); 3614 if (add_proto(cmd, *av, &proto)) { 3615 av++; 3616 } else 3617 errx(EX_DATAERR, "invalid protocol ``%s''", 3618 *av); 3619 break; 3620 3621 case TOK_SRCIP: 3622 NEED1("missing source IP"); 3623 if (add_srcip(cmd, *av, cblen)) { 3624 av++; 3625 } 3626 break; 3627 3628 case TOK_DSTIP: 3629 NEED1("missing destination IP"); 3630 if (add_dstip(cmd, *av, cblen)) { 3631 av++; 3632 } 3633 break; 3634 3635 case TOK_SRCIP6: 3636 NEED1("missing source IP6"); 3637 if (add_srcip6(cmd, *av, cblen)) { 3638 av++; 3639 } 3640 break; 3641 3642 case TOK_DSTIP6: 3643 NEED1("missing destination IP6"); 3644 if (add_dstip6(cmd, *av, cblen)) { 3645 av++; 3646 } 3647 break; 3648 3649 case TOK_SRCPORT: 3650 NEED1("missing source port"); 3651 if (_substrcmp(*av, "any") == 0 || 3652 add_ports(cmd, *av, proto, O_IP_SRCPORT, cblen)) { 3653 av++; 3654 } else 3655 errx(EX_DATAERR, "invalid source port %s", *av); 3656 break; 3657 3658 case TOK_DSTPORT: 3659 NEED1("missing destination port"); 3660 if (_substrcmp(*av, "any") == 0 || 3661 add_ports(cmd, *av, proto, O_IP_DSTPORT, cblen)) { 3662 av++; 3663 } else 3664 errx(EX_DATAERR, "invalid destination port %s", 3665 *av); 3666 break; 3667 3668 case TOK_MAC: 3669 if (add_mac(cmd, av, cblen)) 3670 av += 2; 3671 break; 3672 3673 case TOK_MACTYPE: 3674 NEED1("missing mac type"); 3675 if (!add_mactype(cmd, *av, cblen)) 3676 errx(EX_DATAERR, "invalid mac type %s", *av); 3677 av++; 3678 break; 3679 3680 case TOK_VERREVPATH: 3681 fill_cmd(cmd, O_VERREVPATH, 0, 0); 3682 break; 3683 3684 case TOK_VERSRCREACH: 3685 fill_cmd(cmd, O_VERSRCREACH, 0, 0); 3686 break; 3687 3688 case TOK_ANTISPOOF: 3689 fill_cmd(cmd, O_ANTISPOOF, 0, 0); 3690 break; 3691 3692 case TOK_IPSEC: 3693 fill_cmd(cmd, O_IPSEC, 0, 0); 3694 break; 3695 3696 case TOK_IPV6: 3697 fill_cmd(cmd, O_IP6, 0, 0); 3698 break; 3699 3700 case TOK_IPV4: 3701 fill_cmd(cmd, O_IP4, 0, 0); 3702 break; 3703 3704 case TOK_EXT6HDR: 3705 fill_ext6hdr( cmd, *av ); 3706 av++; 3707 break; 3708 3709 case TOK_FLOWID: 3710 if (proto != IPPROTO_IPV6 ) 3711 errx( EX_USAGE, "flow-id filter is active " 3712 "only for ipv6 protocol\n"); 3713 fill_flow6( (ipfw_insn_u32 *) cmd, *av, cblen); 3714 av++; 3715 break; 3716 3717 case TOK_COMMENT: 3718 fill_comment(cmd, av, cblen); 3719 av[0]=NULL; 3720 break; 3721 3722 case TOK_TAGGED: 3723 if (av[0] && strpbrk(*av, "-,")) { 3724 if (!add_ports(cmd, *av, 0, O_TAGGED, cblen)) 3725 errx(EX_DATAERR, "tagged: invalid tag" 3726 " list: %s", *av); 3727 } 3728 else { 3729 uint16_t tag; 3730 3731 GET_UINT_ARG(tag, IPFW_ARG_MIN, IPFW_ARG_MAX, 3732 TOK_TAGGED, rule_options); 3733 fill_cmd(cmd, O_TAGGED, 0, tag); 3734 } 3735 av++; 3736 break; 3737 3738 case TOK_FIB: 3739 NEED1("fib requires fib number"); 3740 fill_cmd(cmd, O_FIB, 0, strtoul(*av, NULL, 0)); 3741 av++; 3742 break; 3743 case TOK_SOCKARG: 3744 fill_cmd(cmd, O_SOCKARG, 0, 0); 3745 break; 3746 3747 case TOK_LOOKUP: { 3748 ipfw_insn_u32 *c = (ipfw_insn_u32 *)cmd; 3749 char *p; 3750 int j; 3751 3752 if (!av[0] || !av[1]) 3753 errx(EX_USAGE, "format: lookup argument tablenum"); 3754 cmd->opcode = O_IP_DST_LOOKUP; 3755 cmd->len |= F_INSN_SIZE(ipfw_insn) + 2; 3756 i = match_token(rule_options, *av); 3757 for (j = 0; lookup_key[j] >= 0 ; j++) { 3758 if (i == lookup_key[j]) 3759 break; 3760 } 3761 if (lookup_key[j] <= 0) 3762 errx(EX_USAGE, "format: cannot lookup on %s", *av); 3763 __PAST_END(c->d, 1) = j; // i converted to option 3764 av++; 3765 cmd->arg1 = strtoul(*av, &p, 0); 3766 if (p && *p) 3767 errx(EX_USAGE, "format: lookup argument tablenum"); 3768 av++; 3769 } 3770 break; 3771 3772 default: 3773 errx(EX_USAGE, "unrecognised option [%d] %s\n", i, s); 3774 } 3775 if (F_LEN(cmd) > 0) { /* prepare to advance */ 3776 prev = cmd; 3777 cmd = next_cmd(cmd, &cblen); 3778 } 3779 } 3780 3781done: 3782 /* 3783 * Now copy stuff into the rule. 3784 * If we have a keep-state option, the first instruction 3785 * must be a PROBE_STATE (which is generated here). 3786 * If we have a LOG option, it was stored as the first command, 3787 * and now must be moved to the top of the action part. 3788 */ 3789 dst = (ipfw_insn *)rule->cmd; 3790 3791 /* 3792 * First thing to write into the command stream is the match probability. 3793 */ 3794 if (match_prob != 1) { /* 1 means always match */ 3795 dst->opcode = O_PROB; 3796 dst->len = 2; 3797 *((int32_t *)(dst+1)) = (int32_t)(match_prob * 0x7fffffff); 3798 dst += dst->len; 3799 } 3800 3801 /* 3802 * generate O_PROBE_STATE if necessary 3803 */ 3804 if (have_state && have_state->opcode != O_CHECK_STATE) { 3805 fill_cmd(dst, O_PROBE_STATE, 0, 0); 3806 dst = next_cmd(dst, &rblen); 3807 } 3808 3809 /* copy all commands but O_LOG, O_KEEP_STATE, O_LIMIT, O_ALTQ, O_TAG */ 3810 for (src = (ipfw_insn *)cmdbuf; src != cmd; src += i) { 3811 i = F_LEN(src); 3812 CHECK_RBUFLEN(i); 3813 3814 switch (src->opcode) { 3815 case O_LOG: 3816 case O_KEEP_STATE: 3817 case O_LIMIT: 3818 case O_ALTQ: 3819 case O_TAG: 3820 break; 3821 default: 3822 bcopy(src, dst, i * sizeof(uint32_t)); 3823 dst += i; 3824 } 3825 } 3826 3827 /* 3828 * put back the have_state command as last opcode 3829 */ 3830 if (have_state && have_state->opcode != O_CHECK_STATE) { 3831 i = F_LEN(have_state); 3832 CHECK_RBUFLEN(i); 3833 bcopy(have_state, dst, i * sizeof(uint32_t)); 3834 dst += i; 3835 } 3836 /* 3837 * start action section 3838 */ 3839 rule->act_ofs = dst - rule->cmd; 3840 3841 /* put back O_LOG, O_ALTQ, O_TAG if necessary */ 3842 if (have_log) { 3843 i = F_LEN(have_log); 3844 CHECK_RBUFLEN(i); 3845 bcopy(have_log, dst, i * sizeof(uint32_t)); 3846 dst += i; 3847 } 3848 if (have_altq) { 3849 i = F_LEN(have_altq); 3850 CHECK_RBUFLEN(i); 3851 bcopy(have_altq, dst, i * sizeof(uint32_t)); 3852 dst += i; 3853 } 3854 if (have_tag) { 3855 i = F_LEN(have_tag); 3856 CHECK_RBUFLEN(i); 3857 bcopy(have_tag, dst, i * sizeof(uint32_t)); 3858 dst += i; 3859 } 3860 3861 /* 3862 * copy all other actions 3863 */ 3864 for (src = (ipfw_insn *)actbuf; src != action; src += i) { 3865 i = F_LEN(src); 3866 CHECK_RBUFLEN(i); 3867 bcopy(src, dst, i * sizeof(uint32_t)); 3868 dst += i; 3869 } 3870 3871 rule->cmd_len = (uint32_t *)dst - (uint32_t *)(rule->cmd); 3872 i = (char *)dst - (char *)rule; 3873 if (do_cmd(IP_FW_ADD, rule, (uintptr_t)&i) == -1) 3874 err(EX_UNAVAILABLE, "getsockopt(%s)", "IP_FW_ADD"); 3875 if (!co.do_quiet) 3876 show_ipfw(rule, 0, 0); 3877} 3878 3879/* 3880 * clear the counters or the log counters. 3881 */ 3882void 3883ipfw_zero(int ac, char *av[], int optname /* 0 = IP_FW_ZERO, 1 = IP_FW_RESETLOG */) 3884{ 3885 uint32_t arg, saved_arg; 3886 int failed = EX_OK; 3887 char const *errstr; 3888 char const *name = optname ? "RESETLOG" : "ZERO"; 3889 3890 optname = optname ? IP_FW_RESETLOG : IP_FW_ZERO; 3891 3892 av++; ac--; 3893 3894 if (!ac) { 3895 /* clear all entries */ 3896 if (do_cmd(optname, NULL, 0) < 0) 3897 err(EX_UNAVAILABLE, "setsockopt(IP_FW_%s)", name); 3898 if (!co.do_quiet) 3899 printf("%s.\n", optname == IP_FW_ZERO ? 3900 "Accounting cleared":"Logging counts reset"); 3901 3902 return; 3903 } 3904 3905 while (ac) { 3906 /* Rule number */ 3907 if (isdigit(**av)) { 3908 arg = strtonum(*av, 0, 0xffff, &errstr); 3909 if (errstr) 3910 errx(EX_DATAERR, 3911 "invalid rule number %s\n", *av); 3912 saved_arg = arg; 3913 if (co.use_set) 3914 arg |= (1 << 24) | ((co.use_set - 1) << 16); 3915 av++; 3916 ac--; 3917 if (do_cmd(optname, &arg, sizeof(arg))) { 3918 warn("rule %u: setsockopt(IP_FW_%s)", 3919 saved_arg, name); 3920 failed = EX_UNAVAILABLE; 3921 } else if (!co.do_quiet) 3922 printf("Entry %d %s.\n", saved_arg, 3923 optname == IP_FW_ZERO ? 3924 "cleared" : "logging count reset"); 3925 } else { 3926 errx(EX_USAGE, "invalid rule number ``%s''", *av); 3927 } 3928 } 3929 if (failed != EX_OK) 3930 exit(failed); 3931} 3932 3933void 3934ipfw_flush(int force) 3935{ 3936 int cmd = co.do_pipe ? IP_DUMMYNET_FLUSH : IP_FW_FLUSH; 3937 3938 if (!force && !co.do_quiet) { /* need to ask user */ 3939 int c; 3940 3941 printf("Are you sure? [yn] "); 3942 fflush(stdout); 3943 do { 3944 c = toupper(getc(stdin)); 3945 while (c != '\n' && getc(stdin) != '\n') 3946 if (feof(stdin)) 3947 return; /* and do not flush */ 3948 } while (c != 'Y' && c != 'N'); 3949 printf("\n"); 3950 if (c == 'N') /* user said no */ 3951 return; 3952 } 3953 if (co.do_pipe) { 3954 dummynet_flush(); 3955 return; 3956 } 3957 /* `ipfw set N flush` - is the same that `ipfw delete set N` */ 3958 if (co.use_set) { 3959 uint32_t arg = ((co.use_set - 1) & 0xffff) | (1 << 24); 3960 if (do_cmd(IP_FW_DEL, &arg, sizeof(arg)) < 0) 3961 err(EX_UNAVAILABLE, "setsockopt(IP_FW_DEL)"); 3962 } else if (do_cmd(cmd, NULL, 0) < 0) 3963 err(EX_UNAVAILABLE, "setsockopt(IP_%s_FLUSH)", 3964 co.do_pipe ? "DUMMYNET" : "FW"); 3965 if (!co.do_quiet) 3966 printf("Flushed all %s.\n", co.do_pipe ? "pipes" : "rules"); 3967} 3968 3969 3970static void table_list(uint16_t num, int need_header); 3971static void table_fill_xentry(char *arg, ipfw_table_xentry *xent); 3972 3973/* 3974 * This one handles all table-related commands 3975 * ipfw table N add addr[/masklen] [value] 3976 * ipfw table N delete addr[/masklen] 3977 * ipfw table {N | all} flush 3978 * ipfw table {N | all} list 3979 */ 3980void 3981ipfw_table_handler(int ac, char *av[]) 3982{ 3983 ipfw_table_xentry xent; 3984 int do_add; 3985 int is_all; 3986 size_t len; 3987 uint32_t a; 3988 uint32_t tables_max; 3989 3990 len = sizeof(tables_max); 3991 if (sysctlbyname("net.inet.ip.fw.tables_max", &tables_max, &len, 3992 NULL, 0) == -1) { 3993 if (co.test_only) 3994 tables_max = 128; /* Old conservative default */ 3995 else 3996 errx(1, "Can't determine maximum number of ipfw tables." 3997 " Perhaps you forgot to load ipfw module?"); 3998 } 3999 4000 memset(&xent, 0, sizeof(xent)); 4001 4002 ac--; av++; 4003 if (ac && isdigit(**av)) { 4004 xent.tbl = atoi(*av); 4005 is_all = 0; 4006 ac--; av++; 4007 } else if (ac && _substrcmp(*av, "all") == 0) { 4008 xent.tbl = 0; 4009 is_all = 1; 4010 ac--; av++; 4011 } else 4012 errx(EX_USAGE, "table number or 'all' keyword required"); 4013 if (xent.tbl >= tables_max) 4014 errx(EX_USAGE, "The table number exceeds the maximum allowed " 4015 "value (%d)", tables_max - 1); 4016 NEED1("table needs command"); 4017 if (is_all && _substrcmp(*av, "list") != 0 4018 && _substrcmp(*av, "flush") != 0) 4019 errx(EX_USAGE, "table number required"); 4020 4021 if (_substrcmp(*av, "add") == 0 || 4022 _substrcmp(*av, "delete") == 0) { 4023 do_add = **av == 'a'; 4024 ac--; av++; 4025 if (!ac) 4026 errx(EX_USAGE, "address required"); 4027 4028 table_fill_xentry(*av, &xent); 4029 4030 ac--; av++; 4031 if (do_add && ac) { 4032 unsigned int tval; 4033 /* isdigit is a bit of a hack here.. */ 4034 if (strchr(*av, (int)'.') == NULL && isdigit(**av)) { 4035 xent.value = strtoul(*av, NULL, 0); 4036 } else { 4037 if (lookup_host(*av, (struct in_addr *)&tval) == 0) { 4038 /* The value must be stored in host order * 4039 * so that the values < 65k can be distinguished */ 4040 xent.value = ntohl(tval); 4041 } else { 4042 errx(EX_NOHOST, "hostname ``%s'' unknown", *av); 4043 } 4044 } 4045 } else 4046 xent.value = 0; 4047 if (do_setcmd3(do_add ? IP_FW_TABLE_XADD : IP_FW_TABLE_XDEL, 4048 &xent, xent.len) < 0) { 4049 /* If running silent, don't bomb out on these errors. */ 4050 if (!(co.do_quiet && (errno == (do_add ? EEXIST : ESRCH)))) 4051 err(EX_OSERR, "setsockopt(IP_FW_TABLE_%s)", 4052 do_add ? "XADD" : "XDEL"); 4053 /* In silent mode, react to a failed add by deleting */ 4054 if (do_add) { 4055 do_setcmd3(IP_FW_TABLE_XDEL, &xent, xent.len); 4056 if (do_setcmd3(IP_FW_TABLE_XADD, &xent, xent.len) < 0) 4057 err(EX_OSERR, 4058 "setsockopt(IP_FW_TABLE_XADD)"); 4059 } 4060 } 4061 } else if (_substrcmp(*av, "flush") == 0) { 4062 a = is_all ? tables_max : (uint32_t)(xent.tbl + 1); 4063 do { 4064 if (do_cmd(IP_FW_TABLE_FLUSH, &xent.tbl, 4065 sizeof(xent.tbl)) < 0) 4066 err(EX_OSERR, "setsockopt(IP_FW_TABLE_FLUSH)"); 4067 } while (++xent.tbl < a); 4068 } else if (_substrcmp(*av, "list") == 0) { 4069 a = is_all ? tables_max : (uint32_t)(xent.tbl + 1); 4070 do { 4071 table_list(xent.tbl, is_all); 4072 } while (++xent.tbl < a); 4073 } else 4074 errx(EX_USAGE, "invalid table command %s", *av); 4075} 4076 4077static void 4078table_fill_xentry(char *arg, ipfw_table_xentry *xent) 4079{ 4080 int addrlen, mask, masklen, type; 4081 struct in6_addr *paddr; 4082 uint32_t *pkey; 4083 char *p; 4084 uint32_t key; 4085 4086 mask = 0; 4087 type = 0; 4088 addrlen = 0; 4089 masklen = 0; 4090 4091 /* 4092 * Let's try to guess type by agrument. 4093 * Possible types: 4094 * 1) IPv4[/mask] 4095 * 2) IPv6[/mask] 4096 * 3) interface name 4097 * 4) port, uid/gid or other u32 key (base 10 format) 4098 * 5) hostname 4099 */ 4100 paddr = &xent->k.addr6; 4101 if (ishexnumber(*arg) != 0 || *arg == ':') { 4102 /* Remove / if exists */ 4103 if ((p = strchr(arg, '/')) != NULL) { 4104 *p = '\0'; 4105 mask = atoi(p + 1); 4106 } 4107 4108 if (inet_pton(AF_INET, arg, paddr) == 1) { 4109 if (p != NULL && mask > 32) 4110 errx(EX_DATAERR, "bad IPv4 mask width: %s", 4111 p + 1); 4112 4113 type = IPFW_TABLE_CIDR; 4114 masklen = p ? mask : 32; 4115 addrlen = sizeof(struct in_addr); 4116 } else if (inet_pton(AF_INET6, arg, paddr) == 1) { 4117 if (IN6_IS_ADDR_V4COMPAT(paddr)) 4118 errx(EX_DATAERR, 4119 "Use IPv4 instead of v4-compatible"); 4120 if (p != NULL && mask > 128) 4121 errx(EX_DATAERR, "bad IPv6 mask width: %s", 4122 p + 1); 4123 4124 type = IPFW_TABLE_CIDR; 4125 masklen = p ? mask : 128; 4126 addrlen = sizeof(struct in6_addr); 4127 } else { 4128 /* Port or any other key */ 4129 key = strtol(arg, &p, 10); 4130 /* Skip non-base 10 entries like 'fa1' */ 4131 if (p != arg) { 4132 pkey = (uint32_t *)paddr; 4133 *pkey = htonl(key); 4134 type = IPFW_TABLE_CIDR; 4135 addrlen = sizeof(uint32_t); 4136 } 4137 } 4138 } 4139 4140 if (type == 0 && strchr(arg, '.') == NULL) { 4141 /* Assume interface name. Copy significant data only */ 4142 mask = MIN(strlen(arg), IF_NAMESIZE - 1); 4143 memcpy(xent->k.iface, arg, mask); 4144 /* Set mask to exact match */ 4145 masklen = 8 * IF_NAMESIZE; 4146 type = IPFW_TABLE_INTERFACE; 4147 addrlen = IF_NAMESIZE; 4148 } 4149 4150 if (type == 0) { 4151 if (lookup_host(arg, (struct in_addr *)paddr) != 0) 4152 errx(EX_NOHOST, "hostname ``%s'' unknown", arg); 4153 4154 masklen = 32; 4155 type = IPFW_TABLE_CIDR; 4156 addrlen = sizeof(struct in_addr); 4157 } 4158 4159 xent->type = type; 4160 xent->masklen = masklen; 4161 xent->len = offsetof(ipfw_table_xentry, k) + addrlen; 4162} 4163 4164static void 4165table_list(uint16_t num, int need_header) 4166{ 4167 ipfw_xtable *tbl; 4168 ipfw_table_xentry *xent; 4169 socklen_t l; 4170 uint32_t *a, sz, tval; 4171 char tbuf[128]; 4172 struct in6_addr *addr6; 4173 ip_fw3_opheader *op3; 4174 4175 /* Prepend value with IP_FW3 header */ 4176 l = sizeof(ip_fw3_opheader) + sizeof(uint32_t); 4177 op3 = alloca(l); 4178 /* Zero reserved fields */ 4179 memset(op3, 0, sizeof(ip_fw3_opheader)); 4180 a = (uint32_t *)(op3 + 1); 4181 *a = num; 4182 op3->opcode = IP_FW_TABLE_XGETSIZE; 4183 if (do_cmd(IP_FW3, op3, (uintptr_t)&l) < 0) 4184 err(EX_OSERR, "getsockopt(IP_FW_TABLE_XGETSIZE)"); 4185 4186 /* If a is zero we have nothing to do, the table is empty. */ 4187 if (*a == 0) 4188 return; 4189 4190 l = *a; 4191 tbl = safe_calloc(1, l); 4192 tbl->opheader.opcode = IP_FW_TABLE_XLIST; 4193 tbl->tbl = num; 4194 if (do_cmd(IP_FW3, tbl, (uintptr_t)&l) < 0) 4195 err(EX_OSERR, "getsockopt(IP_FW_TABLE_XLIST)"); 4196 if (tbl->cnt && need_header) 4197 printf("---table(%d)---\n", tbl->tbl); 4198 sz = tbl->size - sizeof(ipfw_xtable); 4199 xent = &tbl->xent[0]; 4200 while (sz > 0) { 4201 switch (tbl->type) { 4202 case IPFW_TABLE_CIDR: 4203 /* IPv4 or IPv6 prefixes */ 4204 tval = xent->value; 4205 addr6 = &xent->k.addr6; 4206 4207 4208 if (IN6_IS_ADDR_V4COMPAT(addr6)) { 4209 /* IPv4 address */ 4210 inet_ntop(AF_INET, &addr6->s6_addr32[3], tbuf, sizeof(tbuf)); 4211 } else { 4212 /* IPv6 address */ 4213 inet_ntop(AF_INET6, addr6, tbuf, sizeof(tbuf)); 4214 } 4215 4216 if (co.do_value_as_ip) { 4217 tval = htonl(tval); 4218 printf("%s/%u %s\n", tbuf, xent->masklen, 4219 inet_ntoa(*(struct in_addr *)&tval)); 4220 } else 4221 printf("%s/%u %u\n", tbuf, xent->masklen, tval); 4222 break; 4223 case IPFW_TABLE_INTERFACE: 4224 /* Interface names */ 4225 tval = xent->value; 4226 if (co.do_value_as_ip) { 4227 tval = htonl(tval); 4228 printf("%s %s\n", xent->k.iface, 4229 inet_ntoa(*(struct in_addr *)&tval)); 4230 } else 4231 printf("%s %u\n", xent->k.iface, tval); 4232 } 4233 4234 if (sz < xent->len) 4235 break; 4236 sz -= xent->len; 4237 xent = (void *)xent + xent->len; 4238 } 4239 4240 free(tbl); 4241} 4242