2 * Copyright (c) 2002-2003 Luigi Rizzo
3 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
4 * Copyright (c) 1994 Ugen J.S.Antsilevich
6 * Idea and grammar partially left from:
7 * Copyright (c) 1993 Daniel Boulet
9 * Redistribution and use in source forms, with and without modification,
10 * are permitted provided that this entire comment appears intact.
12 * Redistribution in binary form may occur without any restrictions.
13 * Obviously, it would be nice if you gave credit where credit is due
14 * but requiring it would be too onerous.
16 * This software is provided ``AS IS'' without any warranties of any kind.
18 * NEW command line interface for IP firewall facility
25 #include <sys/types.h>
26 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <netinet/in_systm.h>
40 #include <netinet/ip.h>
41 #include <netinet/icmp6.h>
42 #include <netinet/ip_fw.h>
43 #include <arpa/inet.h>
45 #define CHECK_LENGTH(v, len) do { \
47 errx(EX_DATAERR, "Rule too long"); \
50 static struct _s_x icmp6codes[] = {
51 { "no-route", ICMP6_DST_UNREACH_NOROUTE },
52 { "admin-prohib", ICMP6_DST_UNREACH_ADMIN },
53 { "address", ICMP6_DST_UNREACH_ADDR },
54 { "port", ICMP6_DST_UNREACH_NOPORT },
59 fill_unreach6_code(u_short *codep, char *str)
64 val = strtoul(str, &s, 0);
65 if (s == str || *s != '\0' || val >= 0x100)
66 val = match_token(icmp6codes, str);
68 errx(EX_DATAERR, "unknown ICMPv6 unreachable code ``%s''", str);
74 print_unreach6_code(struct buf_pr *bp, uint16_t code)
76 char const *s = match_value(icmp6codes, code);
79 bprintf(bp, "unreach6 %s", s);
81 bprintf(bp, "unreach6 %u", code);
85 * Print the ip address contained in a command.
88 print_ip6(struct buf_pr *bp, ipfw_insn_ip6 *cmd)
91 struct hostent *he = NULL;
92 struct in6_addr *a = &(cmd->addr6);
95 len = F_LEN((ipfw_insn *) cmd) - 1;
96 if (cmd->o.opcode == O_IP6_SRC_ME || cmd->o.opcode == O_IP6_DST_ME) {
100 if (cmd->o.opcode == O_IP6) {
106 * len == 4 indicates a single IP, whereas lists of 1 or more
107 * addr/mask pairs have len = (2n+1). We convert len to n so we
108 * use that to count the number of entries.
111 for (len = len / 4; len > 0; len -= 2, a += 2) {
113 mb = (cmd->o.opcode == O_IP6_SRC ||
114 cmd->o.opcode == O_IP6_DST) ? 128:
115 contigmask((uint8_t *)&(a[1]), 128);
117 if (mb == 128 && co.do_resolv)
118 he = gethostbyaddr((char *)a, sizeof(*a), AF_INET6);
120 if (he != NULL) /* resolved to name */
121 bprintf(bp, "%s", he->h_name);
122 else if (mb == 0) /* any */
124 else { /* numeric IP followed by some kind of mask */
125 if (inet_ntop(AF_INET6, a, trad,
126 sizeof(trad)) == NULL)
127 bprintf(bp, "Error ntop in print_ip6\n");
128 bprintf(bp, "%s", trad );
129 if (mb < 0) /* mask not contiguous */
130 bprintf(bp, "/%s", inet_ntop(AF_INET6, &a[1],
131 trad, sizeof(trad)));
133 bprintf(bp, "/%d", mb);
141 fill_icmp6types(ipfw_insn_icmp6 *cmd, char *av, int cblen)
145 CHECK_LENGTH(cblen, F_INSN_SIZE(ipfw_insn_icmp6));
149 type = strtoul(av, &av, 0);
150 if (*av != ',' && *av != '\0')
151 errx(EX_DATAERR, "invalid ICMP6 type");
153 * XXX: shouldn't this be 0xFF? I can't see any reason why
154 * we shouldn't be able to filter all possiable values
155 * regardless of the ability of the rest of the kernel to do
156 * anything useful with them.
158 if (type > ICMP6_MAXTYPE)
159 errx(EX_DATAERR, "ICMP6 type out of range");
160 cmd->d[type / 32] |= ( 1 << (type % 32));
162 cmd->o.opcode = O_ICMP6TYPE;
163 cmd->o.len |= F_INSN_SIZE(ipfw_insn_icmp6);
167 print_icmp6types(struct buf_pr *bp, ipfw_insn_u32 *cmd)
172 bprintf(bp, " icmp6types");
173 for (i = 0; i < 7; i++)
174 for (j=0; j < 32; ++j) {
175 if ( (cmd->d[i] & (1 << (j))) == 0)
177 bprintf(bp, "%c%d", sep, (i*32 + j));
183 print_flow6id(struct buf_pr *bp, ipfw_insn_u32 *cmd)
185 uint16_t i, limit = cmd->o.arg1;
188 bprintf(bp, " flow-id ");
189 for( i=0; i < limit; ++i) {
192 bprintf(bp, "%d%c", cmd->d[i], sep);
196 /* structure and define for the extension header in ipv6 */
197 static struct _s_x ext6hdrcodes[] = {
198 { "frag", EXT_FRAGMENT },
199 { "hopopt", EXT_HOPOPTS },
200 { "route", EXT_ROUTING },
201 { "dstopt", EXT_DSTOPTS },
204 { "rthdr0", EXT_RTHDR0 },
205 { "rthdr2", EXT_RTHDR2 },
209 /* fills command for the extension header filtering */
211 fill_ext6hdr( ipfw_insn *cmd, char *av)
218 av = strsep( &s, ",") ;
219 tok = match_token(ext6hdrcodes, av);
222 cmd->arg1 |= EXT_FRAGMENT;
225 cmd->arg1 |= EXT_HOPOPTS;
228 cmd->arg1 |= EXT_ROUTING;
231 cmd->arg1 |= EXT_DSTOPTS;
237 cmd->arg1 |= EXT_ESP;
240 cmd->arg1 |= EXT_RTHDR0;
243 cmd->arg1 |= EXT_RTHDR2;
247 "invalid option for ipv6 exten header");
253 cmd->opcode = O_EXT_HDR;
254 cmd->len |= F_INSN_SIZE(ipfw_insn);
259 print_ext6hdr(struct buf_pr *bp, ipfw_insn *cmd )
263 bprintf(bp, " extension header:");
264 if (cmd->arg1 & EXT_FRAGMENT) {
265 bprintf(bp, "%cfragmentation", sep);
268 if (cmd->arg1 & EXT_HOPOPTS) {
269 bprintf(bp, "%chop options", sep);
272 if (cmd->arg1 & EXT_ROUTING) {
273 bprintf(bp, "%crouting options", sep);
276 if (cmd->arg1 & EXT_RTHDR0) {
277 bprintf(bp, "%crthdr0", sep);
280 if (cmd->arg1 & EXT_RTHDR2) {
281 bprintf(bp, "%crthdr2", sep);
284 if (cmd->arg1 & EXT_DSTOPTS) {
285 bprintf(bp, "%cdestination options", sep);
288 if (cmd->arg1 & EXT_AH) {
289 bprintf(bp, "%cauthentication header", sep);
292 if (cmd->arg1 & EXT_ESP) {
293 bprintf(bp, "%cencapsulated security payload", sep);
297 /* Try to find ipv6 address by hostname */
299 lookup_host6 (char *host, struct in6_addr *ip6addr)
303 if (!inet_pton(AF_INET6, host, ip6addr)) {
304 if ((he = gethostbyname2(host, AF_INET6)) == NULL)
306 memcpy(ip6addr, he->h_addr_list[0], sizeof( struct in6_addr));
313 * fill the addr and mask fields in the instruction as appropriate from av.
314 * Update length as appropriate.
315 * The following formats are allowed:
316 * any matches any IP6. Actually returns an empty instruction.
317 * me returns O_IP6_*_ME
319 * 03f1::234:123:0342 single IP6 address
320 * 03f1::234:123:0342/24 address/masklen
321 * 03f1::234:123:0342/ffff::ffff:ffff address/mask
322 * 03f1::234:123:0342/24,03f1::234:123:0343/ List of address
324 * Set of address (as in ipv6) not supported because ipv6 address
325 * are typically random past the initial prefix.
326 * Return 1 on success, 0 on failure.
329 fill_ip6(ipfw_insn_ip6 *cmd, char *av, int cblen, struct tidx *tstate)
332 struct in6_addr *d = &(cmd->addr6);
335 * Needed for multiple address.
336 * Note d[1] points to struct in6_add r mask6 of cmd
339 cmd->o.len &= ~F_LEN_MASK; /* zero len */
341 if (strcmp(av, "any") == 0)
345 if (strcmp(av, "me") == 0) { /* Set the data for "me" opt*/
346 cmd->o.len |= F_INSN_SIZE(ipfw_insn);
350 if (strcmp(av, "me6") == 0) { /* Set the data for "me" opt*/
351 cmd->o.len |= F_INSN_SIZE(ipfw_insn);
355 if (strncmp(av, "table(", 6) == 0) {
356 fill_table(&cmd->o, av, O_IP_DST_LOOKUP, tstate);
360 oav = av = strdup(av);
363 * After the address we can have '/' indicating a mask,
364 * or ',' indicating another address follows.
371 CHECK_LENGTH(cblen, 1 + len + 2 * F_INSN_SIZE(struct in6_addr));
373 if ((q = strchr(av, ',')) ) {
378 if ((p = strchr(av, '/')) ) {
379 md = *p; /* save the separator */
380 *p = '\0'; /* terminate address string */
381 p++; /* and skip past it */
383 /* now p points to NULL, mask or next entry */
385 /* lookup stores address in *d as a side effect */
386 if (lookup_host6(av, d) != 0) {
387 /* XXX: failed. Free memory and go */
388 errx(EX_DATAERR, "bad address \"%s\"", av);
390 /* next, look at the mask, if any */
391 if (md == '/' && strchr(p, ':')) {
392 if (!inet_pton(AF_INET6, p, &d[1]))
393 errx(EX_DATAERR, "bad mask \"%s\"", p);
395 masklen = contigmask((uint8_t *)&(d[1]), 128);
397 masklen = (md == '/') ? atoi(p) : 128;
398 if (masklen > 128 || masklen < 0)
399 errx(EX_DATAERR, "bad width \"%s\''", p);
401 n2mask(&d[1], masklen);
404 APPLY_MASK(d, &d[1]) /* mask base address with mask */
408 /* Check this entry */
411 * 'any' turns the entire list into a NOP.
412 * 'not any' never matches, so it is removed from the
413 * list unless it is the only item, in which case we
416 if (cmd->o.len & F_NOT && av == NULL && len == 0)
417 errx(EX_DATAERR, "not any never matches");
422 * A single IP can be stored alone
424 if (masklen == 128 && av == NULL && len == 0) {
425 len = F_INSN_SIZE(struct in6_addr);
429 /* Update length and pointer to arguments */
430 len += F_INSN_SIZE(struct in6_addr)*2;
435 * Total length of the command, remember that 1 is the size of
438 if (len + 1 > F_LEN_MASK)
439 errx(EX_DATAERR, "address list too long");
446 * fills command for ipv6 flow-id filtering
447 * note that the 20 bit flow number is stored in a array of u_int32_t
448 * it's supported lists of flow-id, so in the o.arg1 we store how many
449 * additional flow-id we want to filter, the basic is 1
452 fill_flow6( ipfw_insn_u32 *cmd, char *av, int cblen)
454 u_int32_t type; /* Current flow number */
455 u_int16_t nflow = 0; /* Current flow index */
457 cmd->d[0] = 0; /* Initializing the base number*/
460 CHECK_LENGTH(cblen, F_INSN_SIZE(ipfw_insn_u32) + nflow + 1);
462 av = strsep( &s, ",") ;
463 type = strtoul(av, &av, 0);
464 if (*av != ',' && *av != '\0')
465 errx(EX_DATAERR, "invalid ipv6 flow number %s", av);
467 errx(EX_DATAERR, "flow number out of range %s", av);
468 cmd->d[nflow] |= type;
472 cmd->o.opcode = O_FLOW6ID;
473 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + nflow;
477 errx(EX_DATAERR, "invalid ipv6 flow number %s", av);
482 add_srcip6(ipfw_insn *cmd, char *av, int cblen, struct tidx *tstate)
485 fill_ip6((ipfw_insn_ip6 *)cmd, av, cblen, tstate);
486 if (cmd->opcode == O_IP_DST_SET) /* set */
487 cmd->opcode = O_IP_SRC_SET;
488 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */
489 cmd->opcode = O_IP_SRC_LOOKUP;
490 else if (F_LEN(cmd) == 0) { /* any */
491 } else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) { /* "me" */
492 cmd->opcode = O_IP6_SRC_ME;
493 } else if (F_LEN(cmd) ==
494 (F_INSN_SIZE(struct in6_addr) + F_INSN_SIZE(ipfw_insn))) {
495 /* single IP, no mask*/
496 cmd->opcode = O_IP6_SRC;
497 } else { /* addr/mask opt */
498 cmd->opcode = O_IP6_SRC_MASK;
504 add_dstip6(ipfw_insn *cmd, char *av, int cblen, struct tidx *tstate)
507 fill_ip6((ipfw_insn_ip6 *)cmd, av, cblen, tstate);
508 if (cmd->opcode == O_IP_DST_SET) /* set */
510 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */
512 else if (F_LEN(cmd) == 0) { /* any */
513 } else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) { /* "me" */
514 cmd->opcode = O_IP6_DST_ME;
515 } else if (F_LEN(cmd) ==
516 (F_INSN_SIZE(struct in6_addr) + F_INSN_SIZE(ipfw_insn))) {
517 /* single IP, no mask*/
518 cmd->opcode = O_IP6_DST;
519 } else { /* addr/mask opt */
520 cmd->opcode = O_IP6_DST_MASK;