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>
27 #include <sys/queue.h>
28 /* XXX there are several sysctl leftover here */
29 #include <sys/sysctl.h>
42 #include <netinet/in.h>
43 #include <netinet/ip_fw.h>
44 #include <netinet/ip_dummynet.h>
45 #include <arpa/inet.h> /* inet_ntoa */
47 static struct _s_x dummynet_params[] = {
49 { "noerror", TOK_NOERROR },
50 { "buckets", TOK_BUCKETS },
51 { "dst-ip", TOK_DSTIP },
52 { "src-ip", TOK_SRCIP },
53 { "dst-port", TOK_DSTPORT },
54 { "src-port", TOK_SRCPORT },
55 { "proto", TOK_PROTO },
56 { "weight", TOK_WEIGHT },
59 { "droptail", TOK_DROPTAIL },
63 { "bandwidth", TOK_BW },
64 { "delay", TOK_DELAY },
66 { "queue", TOK_QUEUE },
67 { "flow-id", TOK_FLOWID},
68 { "dst-ipv6", TOK_DSTIP6},
69 { "dst-ip6", TOK_DSTIP6},
70 { "src-ipv6", TOK_SRCIP6},
71 { "src-ip6", TOK_SRCIP6},
72 { "dummynet-params", TOK_NULL },
73 { NULL, 0 } /* terminator */
77 sort_q(const void *pa, const void *pb)
79 int rev = (co.do_sort < 0);
80 int field = rev ? -co.do_sort : co.do_sort;
82 const struct dn_flow_queue *a = pa;
83 const struct dn_flow_queue *b = pb;
87 res = a->len - b->len;
90 res = a->len_bytes - b->len_bytes;
93 case 3: /* tot pkts */
94 res = a->tot_pkts - b->tot_pkts;
97 case 4: /* tot bytes */
98 res = a->tot_bytes - b->tot_bytes;
105 return (int)(rev ? res : -res);
109 list_queues(struct dn_flow_set *fs, struct dn_flow_queue *q)
112 int index_printed, indexes = 0;
116 if (fs->rq_elements == 0)
120 heapsort(q, fs->rq_elements, sizeof *q, sort_q);
122 /* Print IPv4 flows */
124 for (l = 0; l < fs->rq_elements; l++) {
127 /* XXX: Should check for IPv4 flows */
128 if (IS_IP6_FLOW_ID(&(q[l].id)))
131 if (!index_printed) {
133 if (indexes > 0) /* currently a no-op */
137 "mask: 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
139 fs->flow_mask.src_ip, fs->flow_mask.src_port,
140 fs->flow_mask.dst_ip, fs->flow_mask.dst_port);
142 printf("BKT Prot ___Source IP/port____ "
143 "____Dest. IP/port____ "
144 "Tot_pkt/bytes Pkt/Byte Drp\n");
147 printf("%3d ", q[l].hash_slot);
148 pe = getprotobynumber(q[l].id.proto);
150 printf("%-4s ", pe->p_name);
152 printf("%4u ", q[l].id.proto);
153 ina.s_addr = htonl(q[l].id.src_ip);
155 inet_ntoa(ina), q[l].id.src_port);
156 ina.s_addr = htonl(q[l].id.dst_ip);
158 inet_ntoa(ina), q[l].id.dst_port);
159 printf("%4llu %8llu %2u %4u %3u\n",
160 align_uint64(&q[l].tot_pkts),
161 align_uint64(&q[l].tot_bytes),
162 q[l].len, q[l].len_bytes, q[l].drops);
164 printf(" S %20llu F %20llu\n",
165 align_uint64(&q[l].S), align_uint64(&q[l].F));
168 /* Print IPv6 flows */
170 for (l = 0; l < fs->rq_elements; l++) {
171 if (!IS_IP6_FLOW_ID(&(q[l].id)))
174 if (!index_printed) {
179 printf("\n mask: proto: 0x%02x, flow_id: 0x%08x, ",
180 fs->flow_mask.proto, fs->flow_mask.flow_id6);
181 inet_ntop(AF_INET6, &(fs->flow_mask.src_ip6),
183 printf("%s/0x%04x -> ", buff, fs->flow_mask.src_port);
184 inet_ntop( AF_INET6, &(fs->flow_mask.dst_ip6),
185 buff, sizeof(buff) );
186 printf("%s/0x%04x\n", buff, fs->flow_mask.dst_port);
188 printf("BKT ___Prot___ _flow-id_ "
189 "______________Source IPv6/port_______________ "
190 "_______________Dest. IPv6/port_______________ "
191 "Tot_pkt/bytes Pkt/Byte Drp\n");
193 printf("%3d ", q[l].hash_slot);
194 pe = getprotobynumber(q[l].id.proto);
196 printf("%9s ", pe->p_name);
198 printf("%9u ", q[l].id.proto);
199 printf("%7d %39s/%-5d ", q[l].id.flow_id6,
200 inet_ntop(AF_INET6, &(q[l].id.src_ip6), buff, sizeof(buff)),
202 printf(" %39s/%-5d ",
203 inet_ntop(AF_INET6, &(q[l].id.dst_ip6), buff, sizeof(buff)),
205 printf(" %4llu %8llu %2u %4u %3u\n",
206 align_uint64(&q[l].tot_pkts),
207 align_uint64(&q[l].tot_bytes),
208 q[l].len, q[l].len_bytes, q[l].drops);
210 printf(" S %20llu F %20llu\n",
211 align_uint64(&q[l].S),
212 align_uint64(&q[l].F));
217 print_flowset_parms(struct dn_flow_set *fs, char *prefix)
222 char red[90]; /* Display RED parameters */
225 if (fs->flags_fs & DN_QSIZE_IS_BYTES) {
227 sprintf(qs, "%d KB", l / 1024);
229 sprintf(qs, "%d B", l);
231 sprintf(qs, "%3d sl.", l);
233 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
236 if (fs->flags_fs & DN_IS_RED) /* RED parameters */
238 "\n\t %cRED w_q %f min_th %d max_th %d max_p %f",
239 (fs->flags_fs & DN_IS_GENTLE_RED) ? 'G' : ' ',
240 1.0 * fs->w_q / (double)(1 << SCALE_RED),
241 SCALE_VAL(fs->min_th),
242 SCALE_VAL(fs->max_th),
243 1.0 * fs->max_p / (double)(1 << SCALE_RED));
245 sprintf(red, "droptail");
247 printf("%s %s%s %d queues (%d buckets) %s\n",
248 prefix, qs, plr, fs->rq_elements, fs->rq_size, red);
252 ipfw_list_pipes(void *data, uint nbytes, int ac, char *av[])
256 struct dn_pipe *p = (struct dn_pipe *) data;
257 struct dn_flow_set *fs;
258 struct dn_flow_queue *q;
262 rulenum = strtoul(*av++, NULL, 10);
265 for (; nbytes >= sizeof *p; p = (struct dn_pipe *)next) {
266 double b = p->bandwidth;
270 if (SLIST_NEXT(p, next) != (struct dn_pipe *)DN_IS_PIPE)
271 break; /* done with pipes, now queues */
274 * compute length, as pipe have variable size
276 l = sizeof(*p) + p->fs.rq_elements * sizeof(*q);
277 next = (char *)p + l;
280 if ((rulenum != 0 && rulenum != p->pipe_nr) || co.do_pipe == 2)
284 * Print rate (or clocking interface)
286 if (p->if_name[0] != '\0')
287 sprintf(buf, "%s", p->if_name);
289 sprintf(buf, "unlimited");
290 else if (b >= 1000000)
291 sprintf(buf, "%7.3f Mbit/s", b/1000000);
293 sprintf(buf, "%7.3f Kbit/s", b/1000);
295 sprintf(buf, "%7.3f bit/s ", b);
297 sprintf(prefix, "%05d: %s %4d ms ",
298 p->pipe_nr, buf, p->delay);
299 print_flowset_parms(&(p->fs), prefix);
301 printf(" V %20llu\n", align_uint64(&p->V) >> MY_M);
303 q = (struct dn_flow_queue *)(p+1);
304 list_queues(&(p->fs), q);
306 for (fs = next; nbytes >= sizeof *fs; fs = next) {
309 if (SLIST_NEXT(fs, next) != (struct dn_flow_set *)DN_IS_QUEUE)
311 l = sizeof(*fs) + fs->rq_elements * sizeof(*q);
312 next = (char *)fs + l;
315 if (rulenum != 0 && ((rulenum != fs->fs_nr && co.do_pipe == 2) ||
316 (rulenum != fs->parent_nr && co.do_pipe == 1))) {
320 q = (struct dn_flow_queue *)(fs+1);
321 sprintf(prefix, "q%05d: weight %d pipe %d ",
322 fs->fs_nr, fs->weight, fs->parent_nr);
323 print_flowset_parms(fs, prefix);
329 * Delete pipe or queue i
332 ipfw_delete_pipe(int pipe_or_queue, int i)
336 memset(&p, 0, sizeof p);
337 if (pipe_or_queue == 1)
338 p.pipe_nr = i; /* pipe */
340 p.fs.fs_nr = i; /* queue */
341 i = do_cmd(IP_DUMMYNET_DEL, &p, sizeof p);
344 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
350 ipfw_config_pipe(int ac, char **av)
357 memset(&p, 0, sizeof p);
361 if (ac && isdigit(**av)) {
362 i = atoi(*av); av++; ac--;
370 int tok = match_token(dummynet_params, *av);
375 p.fs.flags_fs |= DN_NOERROR;
379 NEED1("plr needs argument 0..1\n");
380 d = strtod(av[0], NULL);
385 p.fs.plr = (int)(d*0x7fffffff);
390 NEED1("queue needs queue size\n");
392 p.fs.qsize = strtoul(av[0], &end, 0);
393 if (*end == 'K' || *end == 'k') {
394 p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
396 } else if (*end == 'B' ||
397 _substrcmp2(end, "by", "bytes") == 0) {
398 p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
404 NEED1("buckets needs argument\n");
405 p.fs.rq_size = strtoul(av[0], NULL, 0);
410 NEED1("mask needs mask specifier\n");
412 * per-flow queue, mask is dst_ip, dst_port,
413 * src_ip, src_port, proto measured in bits
417 bzero(&p.fs.flow_mask, sizeof(p.fs.flow_mask));
421 uint32_t *p32 = NULL;
422 uint16_t *p16 = NULL;
423 uint32_t *p20 = NULL;
424 struct in6_addr *pa6 = NULL;
427 tok = match_token(dummynet_params, *av);
432 * special case, all bits significant
434 p.fs.flow_mask.dst_ip = ~0;
435 p.fs.flow_mask.src_ip = ~0;
436 p.fs.flow_mask.dst_port = ~0;
437 p.fs.flow_mask.src_port = ~0;
438 p.fs.flow_mask.proto = ~0;
439 n2mask(&(p.fs.flow_mask.dst_ip6), 128);
440 n2mask(&(p.fs.flow_mask.src_ip6), 128);
441 p.fs.flow_mask.flow_id6 = ~0;
442 p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
446 p32 = &p.fs.flow_mask.dst_ip;
450 p32 = &p.fs.flow_mask.src_ip;
454 pa6 = &(p.fs.flow_mask.dst_ip6);
458 pa6 = &(p.fs.flow_mask.src_ip6);
462 p20 = &p.fs.flow_mask.flow_id6;
466 p16 = &p.fs.flow_mask.dst_port;
470 p16 = &p.fs.flow_mask.src_port;
477 ac++; av--; /* backtrack */
481 errx(EX_USAGE, "mask: value missing");
483 a = strtoul(av[0]+1, &end, 0);
485 a = (a == 32) ? ~0 : (1 << a) - 1;
487 a = strtoul(av[0], &end, 0);
490 else if (p16 != NULL) {
493 "port mask must be 16 bit");
495 } else if (p20 != NULL) {
498 "flow_id mask must be 20 bit");
500 } else if (pa6 != NULL) {
503 "in6addr invalid mask len");
509 "proto mask must be 8 bit");
510 p.fs.flow_mask.proto = (uint8_t)a;
513 p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
515 } /* end while, config masks */
521 NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
522 p.fs.flags_fs |= DN_IS_RED;
524 p.fs.flags_fs |= DN_IS_GENTLE_RED;
526 * the format for parameters is w_q/min_th/max_th/max_p
528 if ((end = strsep(&av[0], "/"))) {
529 double w_q = strtod(end, NULL);
530 if (w_q > 1 || w_q <= 0)
531 errx(EX_DATAERR, "0 < w_q <= 1");
532 p.fs.w_q = (int) (w_q * (1 << SCALE_RED));
534 if ((end = strsep(&av[0], "/"))) {
535 p.fs.min_th = strtoul(end, &end, 0);
536 if (*end == 'K' || *end == 'k')
539 if ((end = strsep(&av[0], "/"))) {
540 p.fs.max_th = strtoul(end, &end, 0);
541 if (*end == 'K' || *end == 'k')
544 if ((end = strsep(&av[0], "/"))) {
545 double max_p = strtod(end, NULL);
546 if (max_p > 1 || max_p <= 0)
547 errx(EX_DATAERR, "0 < max_p <= 1");
548 p.fs.max_p = (int)(max_p * (1 << SCALE_RED));
554 p.fs.flags_fs &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
558 NEED1("bw needs bandwidth or interface\n");
560 errx(EX_DATAERR, "bandwidth only valid for pipes");
562 * set clocking interface or bandwidth value
564 if (av[0][0] >= 'a' && av[0][0] <= 'z') {
565 int l = sizeof(p.if_name)-1;
567 strncpy(p.if_name, av[0], l);
572 p.bandwidth = strtoul(av[0], &end, 0);
573 if (*end == 'K' || *end == 'k') {
576 } else if (*end == 'M') {
578 p.bandwidth *= 1000000;
581 _substrcmp2(end, "Bi", "Bit/s") != 0) ||
582 _substrcmp2(end, "by", "bytes") == 0)
585 errx(EX_DATAERR, "bandwidth too large");
592 errx(EX_DATAERR, "delay only valid for pipes");
593 NEED1("delay needs argument 0..10000ms\n");
594 p.delay = strtoul(av[0], NULL, 0);
600 errx(EX_DATAERR,"weight only valid for queues");
601 NEED1("weight needs argument 0..100\n");
602 p.fs.weight = strtoul(av[0], &end, 0);
608 errx(EX_DATAERR,"pipe only valid for queues");
609 NEED1("pipe needs pipe_number\n");
610 p.fs.parent_nr = strtoul(av[0], &end, 0);
615 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
618 if (co.do_pipe == 1) {
620 errx(EX_DATAERR, "pipe_nr must be > 0");
622 errx(EX_DATAERR, "delay must be < 10000");
623 } else { /* co.do_pipe == 2, queue */
624 if (p.fs.parent_nr == 0)
625 errx(EX_DATAERR, "pipe must be > 0");
626 if (p.fs.weight >100)
627 errx(EX_DATAERR, "weight must be <= 100");
629 if (p.fs.flags_fs & DN_QSIZE_IS_BYTES) {
634 if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
635 &limit, &len, NULL, 0) == -1)
637 if (p.fs.qsize > limit)
638 errx(EX_DATAERR, "queue size must be < %ldB", limit);
644 if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
645 &limit, &len, NULL, 0) == -1)
647 if (p.fs.qsize > limit)
648 errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
650 if (p.fs.flags_fs & DN_IS_RED) {
652 int lookup_depth, avg_pkt_size;
653 double s, idle, weight, w_q;
657 if (p.fs.min_th >= p.fs.max_th)
658 errx(EX_DATAERR, "min_th %d must be < than max_th %d",
659 p.fs.min_th, p.fs.max_th);
660 if (p.fs.max_th == 0)
661 errx(EX_DATAERR, "max_th must be > 0");
664 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
665 &lookup_depth, &len, NULL, 0) == -1)
666 errx(1, "sysctlbyname(\"%s\")",
667 "net.inet.ip.dummynet.red_lookup_depth");
668 if (lookup_depth == 0)
669 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
670 " must be greater than zero");
673 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
674 &avg_pkt_size, &len, NULL, 0) == -1)
676 errx(1, "sysctlbyname(\"%s\")",
677 "net.inet.ip.dummynet.red_avg_pkt_size");
678 if (avg_pkt_size == 0)
680 "net.inet.ip.dummynet.red_avg_pkt_size must"
681 " be greater than zero");
683 len = sizeof(struct clockinfo);
684 if (sysctlbyname("kern.clockrate", &ck, &len, NULL, 0) == -1)
685 errx(1, "sysctlbyname(\"%s\")", "kern.clockrate");
688 * Ticks needed for sending a medium-sized packet.
689 * Unfortunately, when we are configuring a WF2Q+ queue, we
690 * do not have bandwidth information, because that is stored
691 * in the parent pipe, and also we have multiple queues
692 * competing for it. So we set s=0, which is not very
693 * correct. But on the other hand, why do we want RED with
696 if (p.bandwidth==0) /* this is a WF2Q+ queue */
699 s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
702 * max idle time (in ticks) before avg queue size becomes 0.
703 * NOTA: (3/w_q) is approx the value x so that
706 w_q = ((double)p.fs.w_q) / (1 << SCALE_RED);
708 p.fs.lookup_step = (int)idle / lookup_depth;
709 if (!p.fs.lookup_step)
710 p.fs.lookup_step = 1;
712 for (t = p.fs.lookup_step; t > 1; --t)
714 p.fs.lookup_weight = (int)(weight * (1 << SCALE_RED));
716 i = do_cmd(IP_DUMMYNET_CONFIGURE, &p, sizeof p);
718 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");