Home | History | Annotate | Line # | Download | only in pfctl
pfctl_radix.c revision 1.4.28.1
      1  1.4.28.1  wrstuden /*	$NetBSD: pfctl_radix.c,v 1.4.28.1 2008/06/23 04:28:53 wrstuden Exp $	*/
      2  1.4.28.1  wrstuden /*	$OpenBSD: pfctl_radix.c,v 1.27 2005/05/21 21:03:58 henning Exp $ */
      3       1.1    itojun 
      4       1.1    itojun /*
      5       1.1    itojun  * Copyright (c) 2002 Cedric Berger
      6       1.1    itojun  * All rights reserved.
      7       1.1    itojun  *
      8       1.1    itojun  * Redistribution and use in source and binary forms, with or without
      9       1.1    itojun  * modification, are permitted provided that the following conditions
     10       1.1    itojun  * are met:
     11       1.1    itojun  *
     12       1.1    itojun  *    - Redistributions of source code must retain the above copyright
     13       1.1    itojun  *      notice, this list of conditions and the following disclaimer.
     14       1.1    itojun  *    - Redistributions in binary form must reproduce the above
     15       1.1    itojun  *      copyright notice, this list of conditions and the following
     16       1.1    itojun  *      disclaimer in the documentation and/or other materials provided
     17       1.1    itojun  *      with the distribution.
     18       1.1    itojun  *
     19       1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20       1.1    itojun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21       1.1    itojun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     22       1.1    itojun  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     23       1.1    itojun  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     24       1.1    itojun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     25       1.1    itojun  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     26       1.1    itojun  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     27       1.1    itojun  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28       1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     29       1.1    itojun  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1    itojun  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1    itojun  *
     32       1.1    itojun  */
     33       1.1    itojun 
     34       1.1    itojun #include <sys/types.h>
     35       1.1    itojun #include <sys/ioctl.h>
     36       1.1    itojun #include <sys/socket.h>
     37       1.1    itojun 
     38       1.1    itojun #include <net/if.h>
     39       1.1    itojun #include <net/pfvar.h>
     40       1.1    itojun 
     41       1.1    itojun #include <errno.h>
     42       1.1    itojun #include <string.h>
     43       1.1    itojun #include <ctype.h>
     44       1.1    itojun #include <stdio.h>
     45       1.1    itojun #include <stdlib.h>
     46       1.1    itojun #include <limits.h>
     47       1.1    itojun #include <err.h>
     48       1.1    itojun 
     49       1.1    itojun #include "pfctl.h"
     50       1.1    itojun 
     51       1.1    itojun #define BUF_SIZE 256
     52       1.1    itojun 
     53       1.1    itojun extern int dev;
     54       1.1    itojun 
     55       1.1    itojun static int	 pfr_next_token(char buf[], FILE *);
     56       1.1    itojun 
     57       1.1    itojun 
     58       1.1    itojun int
     59       1.1    itojun pfr_clr_tables(struct pfr_table *filter, int *ndel, int flags)
     60       1.1    itojun {
     61       1.1    itojun 	struct pfioc_table io;
     62       1.1    itojun 
     63       1.1    itojun 	bzero(&io, sizeof io);
     64       1.1    itojun 	io.pfrio_flags = flags;
     65       1.1    itojun 	if (filter != NULL)
     66       1.1    itojun 		io.pfrio_table = *filter;
     67       1.1    itojun 	if (ioctl(dev, DIOCRCLRTABLES, &io))
     68       1.1    itojun 		return (-1);
     69       1.1    itojun 	if (ndel != NULL)
     70       1.1    itojun 		*ndel = io.pfrio_ndel;
     71       1.1    itojun 	return (0);
     72       1.1    itojun }
     73       1.1    itojun 
     74       1.1    itojun int
     75       1.1    itojun pfr_add_tables(struct pfr_table *tbl, int size, int *nadd, int flags)
     76       1.1    itojun {
     77       1.1    itojun 	struct pfioc_table io;
     78       1.1    itojun 
     79       1.1    itojun 	if (size < 0 || (size && tbl == NULL)) {
     80       1.1    itojun 		errno = EINVAL;
     81       1.1    itojun 		return (-1);
     82       1.1    itojun 	}
     83       1.1    itojun 	bzero(&io, sizeof io);
     84       1.1    itojun 	io.pfrio_flags = flags;
     85       1.1    itojun 	io.pfrio_buffer = tbl;
     86       1.1    itojun 	io.pfrio_esize = sizeof(*tbl);
     87       1.1    itojun 	io.pfrio_size = size;
     88       1.1    itojun 	if (ioctl(dev, DIOCRADDTABLES, &io))
     89       1.1    itojun 		return (-1);
     90       1.1    itojun 	if (nadd != NULL)
     91       1.1    itojun 		*nadd = io.pfrio_nadd;
     92       1.1    itojun 	return (0);
     93       1.1    itojun }
     94       1.1    itojun 
     95       1.1    itojun int
     96       1.1    itojun pfr_del_tables(struct pfr_table *tbl, int size, int *ndel, int flags)
     97       1.1    itojun {
     98       1.1    itojun 	struct pfioc_table io;
     99       1.1    itojun 
    100       1.1    itojun 	if (size < 0 || (size && tbl == NULL)) {
    101       1.1    itojun 		errno = EINVAL;
    102       1.1    itojun 		return (-1);
    103       1.1    itojun 	}
    104       1.1    itojun 	bzero(&io, sizeof io);
    105       1.1    itojun 	io.pfrio_flags = flags;
    106       1.1    itojun 	io.pfrio_buffer = tbl;
    107       1.1    itojun 	io.pfrio_esize = sizeof(*tbl);
    108       1.1    itojun 	io.pfrio_size = size;
    109       1.1    itojun 	if (ioctl(dev, DIOCRDELTABLES, &io))
    110       1.1    itojun 		return (-1);
    111       1.1    itojun 	if (ndel != NULL)
    112       1.1    itojun 		*ndel = io.pfrio_ndel;
    113       1.1    itojun 	return (0);
    114       1.1    itojun }
    115       1.1    itojun 
    116       1.1    itojun int
    117       1.1    itojun pfr_get_tables(struct pfr_table *filter, struct pfr_table *tbl, int *size,
    118       1.1    itojun 	int flags)
    119       1.1    itojun {
    120       1.1    itojun 	struct pfioc_table io;
    121       1.1    itojun 
    122       1.1    itojun 	if (size == NULL || *size < 0 || (*size && tbl == NULL)) {
    123       1.1    itojun 		errno = EINVAL;
    124       1.1    itojun 		return (-1);
    125       1.1    itojun 	}
    126       1.1    itojun 	bzero(&io, sizeof io);
    127       1.1    itojun 	io.pfrio_flags = flags;
    128       1.1    itojun 	if (filter != NULL)
    129       1.1    itojun 		io.pfrio_table = *filter;
    130       1.1    itojun 	io.pfrio_buffer = tbl;
    131       1.1    itojun 	io.pfrio_esize = sizeof(*tbl);
    132       1.1    itojun 	io.pfrio_size = *size;
    133       1.1    itojun 	if (ioctl(dev, DIOCRGETTABLES, &io))
    134       1.1    itojun 		return (-1);
    135       1.1    itojun 	*size = io.pfrio_size;
    136       1.1    itojun 	return (0);
    137       1.1    itojun }
    138       1.1    itojun 
    139       1.1    itojun int
    140       1.1    itojun pfr_get_tstats(struct pfr_table *filter, struct pfr_tstats *tbl, int *size,
    141       1.1    itojun 	int flags)
    142       1.1    itojun {
    143       1.1    itojun 	struct pfioc_table io;
    144       1.1    itojun 
    145       1.1    itojun 	if (size == NULL || *size < 0 || (*size && tbl == NULL)) {
    146       1.1    itojun 		errno = EINVAL;
    147       1.1    itojun 		return (-1);
    148       1.1    itojun 	}
    149       1.1    itojun 	bzero(&io, sizeof io);
    150       1.1    itojun 	io.pfrio_flags = flags;
    151       1.1    itojun 	if (filter != NULL)
    152       1.1    itojun 		io.pfrio_table = *filter;
    153       1.1    itojun 	io.pfrio_buffer = tbl;
    154       1.1    itojun 	io.pfrio_esize = sizeof(*tbl);
    155       1.1    itojun 	io.pfrio_size = *size;
    156       1.1    itojun 	if (ioctl(dev, DIOCRGETTSTATS, &io))
    157       1.1    itojun 		return (-1);
    158       1.1    itojun 	*size = io.pfrio_size;
    159       1.1    itojun 	return (0);
    160       1.1    itojun }
    161       1.1    itojun 
    162       1.1    itojun int
    163       1.1    itojun pfr_clr_addrs(struct pfr_table *tbl, int *ndel, int flags)
    164       1.1    itojun {
    165       1.1    itojun 	struct pfioc_table io;
    166       1.1    itojun 
    167       1.1    itojun 	if (tbl == NULL) {
    168       1.1    itojun 		errno = EINVAL;
    169       1.1    itojun 		return (-1);
    170       1.1    itojun 	}
    171       1.1    itojun 	bzero(&io, sizeof io);
    172       1.1    itojun 	io.pfrio_flags = flags;
    173       1.1    itojun 	io.pfrio_table = *tbl;
    174       1.1    itojun 	if (ioctl(dev, DIOCRCLRADDRS, &io))
    175       1.1    itojun 		return (-1);
    176       1.1    itojun 	if (ndel != NULL)
    177       1.1    itojun 		*ndel = io.pfrio_ndel;
    178       1.1    itojun 	return (0);
    179       1.1    itojun }
    180       1.1    itojun 
    181       1.1    itojun int
    182       1.1    itojun pfr_add_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
    183       1.1    itojun     int *nadd, int flags)
    184       1.1    itojun {
    185       1.1    itojun 	struct pfioc_table io;
    186       1.1    itojun 
    187       1.1    itojun 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
    188       1.1    itojun 		errno = EINVAL;
    189       1.1    itojun 		return (-1);
    190       1.1    itojun 	}
    191       1.1    itojun 	bzero(&io, sizeof io);
    192       1.1    itojun 	io.pfrio_flags = flags;
    193       1.1    itojun 	io.pfrio_table = *tbl;
    194       1.1    itojun 	io.pfrio_buffer = addr;
    195       1.1    itojun 	io.pfrio_esize = sizeof(*addr);
    196       1.1    itojun 	io.pfrio_size = size;
    197       1.1    itojun 	if (ioctl(dev, DIOCRADDADDRS, &io))
    198       1.1    itojun 		return (-1);
    199       1.1    itojun 	if (nadd != NULL)
    200       1.1    itojun 		*nadd = io.pfrio_nadd;
    201       1.1    itojun 	return (0);
    202       1.1    itojun }
    203       1.1    itojun 
    204       1.1    itojun int
    205       1.1    itojun pfr_del_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
    206       1.1    itojun     int *ndel, int flags)
    207       1.1    itojun {
    208       1.1    itojun 	struct pfioc_table io;
    209       1.1    itojun 
    210       1.1    itojun 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
    211       1.1    itojun 		errno = EINVAL;
    212       1.1    itojun 		return (-1);
    213       1.1    itojun 	}
    214       1.1    itojun 	bzero(&io, sizeof io);
    215       1.1    itojun 	io.pfrio_flags = flags;
    216       1.1    itojun 	io.pfrio_table = *tbl;
    217       1.1    itojun 	io.pfrio_buffer = addr;
    218       1.1    itojun 	io.pfrio_esize = sizeof(*addr);
    219       1.1    itojun 	io.pfrio_size = size;
    220       1.1    itojun 	if (ioctl(dev, DIOCRDELADDRS, &io))
    221       1.1    itojun 		return (-1);
    222       1.1    itojun 	if (ndel != NULL)
    223       1.1    itojun 		*ndel = io.pfrio_ndel;
    224       1.1    itojun 	return (0);
    225       1.1    itojun }
    226       1.1    itojun 
    227       1.1    itojun int
    228       1.1    itojun pfr_set_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
    229       1.1    itojun     int *size2, int *nadd, int *ndel, int *nchange, int flags)
    230       1.1    itojun {
    231       1.1    itojun 	struct pfioc_table io;
    232       1.1    itojun 
    233       1.1    itojun 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
    234       1.1    itojun 		errno = EINVAL;
    235       1.1    itojun 		return (-1);
    236       1.1    itojun 	}
    237       1.1    itojun 	bzero(&io, sizeof io);
    238       1.1    itojun 	io.pfrio_flags = flags;
    239       1.1    itojun 	io.pfrio_table = *tbl;
    240       1.1    itojun 	io.pfrio_buffer = addr;
    241       1.1    itojun 	io.pfrio_esize = sizeof(*addr);
    242       1.1    itojun 	io.pfrio_size = size;
    243       1.1    itojun 	io.pfrio_size2 = (size2 != NULL) ? *size2 : 0;
    244       1.1    itojun 	if (ioctl(dev, DIOCRSETADDRS, &io))
    245       1.1    itojun 		return (-1);
    246       1.1    itojun 	if (nadd != NULL)
    247       1.1    itojun 		*nadd = io.pfrio_nadd;
    248       1.1    itojun 	if (ndel != NULL)
    249       1.1    itojun 		*ndel = io.pfrio_ndel;
    250       1.1    itojun 	if (nchange != NULL)
    251       1.1    itojun 		*nchange = io.pfrio_nchange;
    252       1.1    itojun 	if (size2 != NULL)
    253       1.1    itojun 		*size2 = io.pfrio_size2;
    254       1.1    itojun 	return (0);
    255       1.1    itojun }
    256       1.1    itojun 
    257       1.1    itojun int
    258       1.1    itojun pfr_get_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int *size,
    259       1.1    itojun     int flags)
    260       1.1    itojun {
    261       1.1    itojun 	struct pfioc_table io;
    262       1.1    itojun 
    263       1.1    itojun 	if (tbl == NULL || size == NULL || *size < 0 ||
    264       1.1    itojun 	    (*size && addr == NULL)) {
    265       1.1    itojun 		errno = EINVAL;
    266       1.1    itojun 		return (-1);
    267       1.1    itojun 	}
    268       1.1    itojun 	bzero(&io, sizeof io);
    269       1.1    itojun 	io.pfrio_flags = flags;
    270       1.1    itojun 	io.pfrio_table = *tbl;
    271       1.1    itojun 	io.pfrio_buffer = addr;
    272       1.1    itojun 	io.pfrio_esize = sizeof(*addr);
    273       1.1    itojun 	io.pfrio_size = *size;
    274       1.1    itojun 	if (ioctl(dev, DIOCRGETADDRS, &io))
    275       1.1    itojun 		return (-1);
    276       1.1    itojun 	*size = io.pfrio_size;
    277       1.1    itojun 	return (0);
    278       1.1    itojun }
    279       1.1    itojun 
    280       1.1    itojun int
    281       1.1    itojun pfr_get_astats(struct pfr_table *tbl, struct pfr_astats *addr, int *size,
    282       1.1    itojun     int flags)
    283       1.1    itojun {
    284       1.1    itojun 	struct pfioc_table io;
    285       1.1    itojun 
    286       1.1    itojun 	if (tbl == NULL || size == NULL || *size < 0 ||
    287       1.1    itojun 	    (*size && addr == NULL)) {
    288       1.1    itojun 		errno = EINVAL;
    289       1.1    itojun 		return (-1);
    290       1.1    itojun 	}
    291       1.1    itojun 	bzero(&io, sizeof io);
    292       1.1    itojun 	io.pfrio_flags = flags;
    293       1.1    itojun 	io.pfrio_table = *tbl;
    294       1.1    itojun 	io.pfrio_buffer = addr;
    295       1.1    itojun 	io.pfrio_esize = sizeof(*addr);
    296       1.1    itojun 	io.pfrio_size = *size;
    297       1.1    itojun 	if (ioctl(dev, DIOCRGETASTATS, &io))
    298       1.1    itojun 		return (-1);
    299       1.1    itojun 	*size = io.pfrio_size;
    300       1.1    itojun 	return (0);
    301       1.1    itojun }
    302       1.1    itojun 
    303       1.1    itojun int
    304       1.1    itojun pfr_clr_astats(struct pfr_table *tbl, struct pfr_addr *addr, int size,
    305       1.1    itojun     int *nzero, int flags)
    306       1.1    itojun {
    307       1.1    itojun 	struct pfioc_table io;
    308       1.1    itojun 
    309       1.1    itojun 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
    310       1.1    itojun 		errno = EINVAL;
    311       1.1    itojun 		return (-1);
    312       1.1    itojun 	}
    313       1.1    itojun 	bzero(&io, sizeof io);
    314       1.1    itojun 	io.pfrio_flags = flags;
    315       1.1    itojun 	io.pfrio_table = *tbl;
    316       1.1    itojun 	io.pfrio_buffer = addr;
    317       1.1    itojun 	io.pfrio_esize = sizeof(*addr);
    318       1.1    itojun 	io.pfrio_size = size;
    319       1.1    itojun 	if (ioctl(dev, DIOCRCLRASTATS, &io))
    320       1.1    itojun 		return (-1);
    321       1.1    itojun 	if (nzero != NULL)
    322       1.1    itojun 		*nzero = io.pfrio_nzero;
    323       1.1    itojun 	return (0);
    324       1.1    itojun }
    325       1.1    itojun 
    326       1.1    itojun int
    327       1.1    itojun pfr_clr_tstats(struct pfr_table *tbl, int size, int *nzero, int flags)
    328       1.1    itojun {
    329       1.1    itojun 	struct pfioc_table io;
    330       1.1    itojun 
    331       1.1    itojun 	if (size < 0 || (size && !tbl)) {
    332       1.1    itojun 		errno = EINVAL;
    333       1.1    itojun 		return (-1);
    334       1.1    itojun 	}
    335       1.1    itojun 	bzero(&io, sizeof io);
    336       1.1    itojun 	io.pfrio_flags = flags;
    337       1.1    itojun 	io.pfrio_buffer = tbl;
    338       1.1    itojun 	io.pfrio_esize = sizeof(*tbl);
    339       1.1    itojun 	io.pfrio_size = size;
    340       1.1    itojun 	if (ioctl(dev, DIOCRCLRTSTATS, &io))
    341       1.1    itojun 		return (-1);
    342       1.1    itojun 	if (nzero)
    343       1.1    itojun 		*nzero = io.pfrio_nzero;
    344       1.1    itojun 	return (0);
    345       1.1    itojun }
    346       1.1    itojun 
    347       1.1    itojun int
    348       1.1    itojun pfr_set_tflags(struct pfr_table *tbl, int size, int setflag, int clrflag,
    349       1.1    itojun     int *nchange, int *ndel, int flags)
    350       1.1    itojun {
    351       1.1    itojun 	struct pfioc_table io;
    352       1.1    itojun 
    353       1.1    itojun 	if (size < 0 || (size && !tbl)) {
    354       1.1    itojun 		errno = EINVAL;
    355       1.1    itojun 		return (-1);
    356       1.1    itojun 	}
    357       1.1    itojun 	bzero(&io, sizeof io);
    358       1.1    itojun 	io.pfrio_flags = flags;
    359       1.1    itojun 	io.pfrio_buffer = tbl;
    360       1.1    itojun 	io.pfrio_esize = sizeof(*tbl);
    361       1.1    itojun 	io.pfrio_size = size;
    362       1.1    itojun 	io.pfrio_setflag = setflag;
    363       1.1    itojun 	io.pfrio_clrflag = clrflag;
    364       1.1    itojun 	if (ioctl(dev, DIOCRSETTFLAGS, &io))
    365       1.1    itojun 		return (-1);
    366       1.1    itojun 	if (nchange)
    367       1.1    itojun 		*nchange = io.pfrio_nchange;
    368       1.1    itojun 	if (ndel)
    369       1.1    itojun 		*ndel = io.pfrio_ndel;
    370       1.1    itojun 	return (0);
    371       1.1    itojun }
    372       1.1    itojun 
    373       1.1    itojun int
    374       1.1    itojun pfr_tst_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
    375       1.1    itojun     int *nmatch, int flags)
    376       1.1    itojun {
    377       1.1    itojun 	struct pfioc_table io;
    378       1.1    itojun 
    379       1.1    itojun 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
    380       1.1    itojun 		errno = EINVAL;
    381       1.1    itojun 		return (-1);
    382       1.1    itojun 	}
    383       1.1    itojun 	bzero(&io, sizeof io);
    384       1.1    itojun 	io.pfrio_flags = flags;
    385       1.1    itojun 	io.pfrio_table = *tbl;
    386       1.1    itojun 	io.pfrio_buffer = addr;
    387       1.1    itojun 	io.pfrio_esize = sizeof(*addr);
    388       1.1    itojun 	io.pfrio_size = size;
    389       1.1    itojun 	if (ioctl(dev, DIOCRTSTADDRS, &io))
    390       1.1    itojun 		return (-1);
    391       1.1    itojun 	if (nmatch)
    392       1.1    itojun 		*nmatch = io.pfrio_nmatch;
    393       1.1    itojun 	return (0);
    394       1.1    itojun }
    395       1.1    itojun 
    396       1.1    itojun int
    397       1.1    itojun pfr_ina_define(struct pfr_table *tbl, struct pfr_addr *addr, int size,
    398       1.1    itojun     int *nadd, int *naddr, int ticket, int flags)
    399       1.1    itojun {
    400       1.1    itojun 	struct pfioc_table io;
    401       1.1    itojun 
    402       1.1    itojun 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
    403       1.1    itojun 		errno = EINVAL;
    404       1.1    itojun 		return (-1);
    405       1.1    itojun 	}
    406       1.1    itojun 	bzero(&io, sizeof io);
    407       1.1    itojun 	io.pfrio_flags = flags;
    408       1.1    itojun 	io.pfrio_table = *tbl;
    409       1.1    itojun 	io.pfrio_buffer = addr;
    410       1.1    itojun 	io.pfrio_esize = sizeof(*addr);
    411       1.1    itojun 	io.pfrio_size = size;
    412       1.1    itojun 	io.pfrio_ticket = ticket;
    413       1.1    itojun 	if (ioctl(dev, DIOCRINADEFINE, &io))
    414       1.1    itojun 		return (-1);
    415       1.1    itojun 	if (nadd != NULL)
    416       1.1    itojun 		*nadd = io.pfrio_nadd;
    417       1.1    itojun 	if (naddr != NULL)
    418       1.1    itojun 		*naddr = io.pfrio_naddr;
    419       1.1    itojun 	return (0);
    420       1.1    itojun }
    421       1.1    itojun 
    422       1.1    itojun /* interface management code */
    423       1.1    itojun 
    424       1.1    itojun int
    425  1.4.28.1  wrstuden pfi_get_ifaces(const char *filter, struct pfi_kif *buf, int *size)
    426       1.1    itojun {
    427       1.1    itojun 	struct pfioc_iface io;
    428       1.1    itojun 
    429       1.1    itojun 	if (size == NULL || *size < 0 || (*size && buf == NULL)) {
    430       1.1    itojun 		errno = EINVAL;
    431       1.1    itojun 		return (-1);
    432       1.1    itojun 	}
    433       1.1    itojun 	bzero(&io, sizeof io);
    434       1.1    itojun 	if (filter != NULL)
    435       1.1    itojun 		if (strlcpy(io.pfiio_name, filter, sizeof(io.pfiio_name)) >=
    436       1.1    itojun 		    sizeof(io.pfiio_name)) {
    437       1.1    itojun 			errno = EINVAL;
    438       1.1    itojun 			return (-1);
    439       1.1    itojun 		}
    440       1.1    itojun 	io.pfiio_buffer = buf;
    441       1.1    itojun 	io.pfiio_esize = sizeof(*buf);
    442       1.1    itojun 	io.pfiio_size = *size;
    443       1.1    itojun 	if (ioctl(dev, DIOCIGETIFACES, &io))
    444       1.1    itojun 		return (-1);
    445       1.1    itojun 	*size = io.pfiio_size;
    446       1.1    itojun 	return (0);
    447       1.1    itojun }
    448       1.1    itojun 
    449       1.1    itojun /* buffer management code */
    450       1.1    itojun 
    451       1.1    itojun size_t buf_esize[PFRB_MAX] = { 0,
    452       1.1    itojun 	sizeof(struct pfr_table), sizeof(struct pfr_tstats),
    453       1.1    itojun 	sizeof(struct pfr_addr), sizeof(struct pfr_astats),
    454  1.4.28.1  wrstuden 	sizeof(struct pfi_kif), sizeof(struct pfioc_trans_e)
    455       1.1    itojun };
    456       1.1    itojun 
    457       1.1    itojun /*
    458       1.1    itojun  * add one element to the buffer
    459       1.1    itojun  */
    460       1.1    itojun int
    461       1.1    itojun pfr_buf_add(struct pfr_buffer *b, const void *e)
    462       1.1    itojun {
    463       1.1    itojun 	size_t bs;
    464       1.1    itojun 
    465       1.1    itojun 	if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX ||
    466       1.1    itojun 	    e == NULL) {
    467       1.1    itojun 		errno = EINVAL;
    468       1.1    itojun 		return (-1);
    469       1.1    itojun 	}
    470       1.1    itojun 	bs = buf_esize[b->pfrb_type];
    471       1.1    itojun 	if (b->pfrb_size == b->pfrb_msize)
    472       1.1    itojun 		if (pfr_buf_grow(b, 0))
    473       1.1    itojun 			return (-1);
    474       1.1    itojun 	memcpy(((caddr_t)b->pfrb_caddr) + bs * b->pfrb_size, e, bs);
    475       1.1    itojun 	b->pfrb_size++;
    476       1.1    itojun 	return (0);
    477       1.1    itojun }
    478       1.1    itojun 
    479       1.1    itojun /*
    480       1.1    itojun  * return next element of the buffer (or first one if prev is NULL)
    481       1.1    itojun  * see PFRB_FOREACH macro
    482       1.1    itojun  */
    483       1.1    itojun void *
    484       1.1    itojun pfr_buf_next(struct pfr_buffer *b, const void *prev)
    485       1.1    itojun {
    486       1.1    itojun 	size_t bs;
    487       1.1    itojun 
    488       1.1    itojun 	if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX)
    489       1.1    itojun 		return (NULL);
    490       1.1    itojun 	if (b->pfrb_size == 0)
    491       1.1    itojun 		return (NULL);
    492       1.1    itojun 	if (prev == NULL)
    493       1.1    itojun 		return (b->pfrb_caddr);
    494       1.1    itojun 	bs = buf_esize[b->pfrb_type];
    495       1.1    itojun 	if ((((caddr_t)prev)-((caddr_t)b->pfrb_caddr)) / bs >= b->pfrb_size-1)
    496       1.1    itojun 		return (NULL);
    497       1.1    itojun 	return (((caddr_t)prev) + bs);
    498       1.1    itojun }
    499       1.1    itojun 
    500       1.1    itojun /*
    501       1.1    itojun  * minsize:
    502       1.1    itojun  *    0: make the buffer somewhat bigger
    503       1.1    itojun  *    n: make room for "n" entries in the buffer
    504       1.1    itojun  */
    505       1.1    itojun int
    506       1.1    itojun pfr_buf_grow(struct pfr_buffer *b, int minsize)
    507       1.1    itojun {
    508       1.1    itojun 	caddr_t p;
    509       1.1    itojun 	size_t bs;
    510       1.1    itojun 
    511       1.1    itojun 	if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX) {
    512       1.1    itojun 		errno = EINVAL;
    513       1.1    itojun 		return (-1);
    514       1.1    itojun 	}
    515       1.1    itojun 	if (minsize != 0 && minsize <= b->pfrb_msize)
    516       1.1    itojun 		return (0);
    517       1.1    itojun 	bs = buf_esize[b->pfrb_type];
    518       1.1    itojun 	if (!b->pfrb_msize) {
    519       1.1    itojun 		if (minsize < 64)
    520       1.1    itojun 			minsize = 64;
    521       1.1    itojun 		b->pfrb_caddr = calloc(bs, minsize);
    522       1.1    itojun 		if (b->pfrb_caddr == NULL)
    523       1.1    itojun 			return (-1);
    524       1.1    itojun 		b->pfrb_msize = minsize;
    525       1.1    itojun 	} else {
    526       1.1    itojun 		if (minsize == 0)
    527       1.1    itojun 			minsize = b->pfrb_msize * 2;
    528       1.1    itojun 		if (minsize < 0 || minsize >= SIZE_T_MAX / bs) {
    529       1.1    itojun 			/* msize overflow */
    530       1.1    itojun 			errno = ENOMEM;
    531       1.1    itojun 			return (-1);
    532       1.1    itojun 		}
    533       1.1    itojun 		p = realloc(b->pfrb_caddr, minsize * bs);
    534       1.1    itojun 		if (p == NULL)
    535       1.1    itojun 			return (-1);
    536       1.1    itojun 		bzero(p + b->pfrb_msize * bs, (minsize - b->pfrb_msize) * bs);
    537       1.1    itojun 		b->pfrb_caddr = p;
    538       1.1    itojun 		b->pfrb_msize = minsize;
    539       1.1    itojun 	}
    540       1.1    itojun 	return (0);
    541       1.1    itojun }
    542       1.1    itojun 
    543       1.1    itojun /*
    544       1.1    itojun  * reset buffer and free memory.
    545       1.1    itojun  */
    546       1.1    itojun void
    547       1.1    itojun pfr_buf_clear(struct pfr_buffer *b)
    548       1.1    itojun {
    549       1.1    itojun 	if (b == NULL)
    550       1.1    itojun 		return;
    551       1.1    itojun 	if (b->pfrb_caddr != NULL)
    552       1.1    itojun 		free(b->pfrb_caddr);
    553       1.1    itojun 	b->pfrb_caddr = NULL;
    554       1.1    itojun 	b->pfrb_size = b->pfrb_msize = 0;
    555       1.1    itojun }
    556       1.1    itojun 
    557       1.1    itojun int
    558       1.1    itojun pfr_buf_load(struct pfr_buffer *b, char *file, int nonetwork,
    559       1.1    itojun     int (*append_addr)(struct pfr_buffer *, char *, int))
    560       1.1    itojun {
    561       1.1    itojun 	FILE	*fp;
    562       1.1    itojun 	char	 buf[BUF_SIZE];
    563       1.1    itojun 	int	 rv;
    564       1.1    itojun 
    565       1.1    itojun 	if (file == NULL)
    566       1.1    itojun 		return (0);
    567       1.1    itojun 	if (!strcmp(file, "-"))
    568       1.1    itojun 		fp = stdin;
    569       1.1    itojun 	else {
    570       1.4      yamt 		fp = pfctl_fopen(file, "r");
    571       1.1    itojun 		if (fp == NULL)
    572       1.1    itojun 			return (-1);
    573       1.1    itojun 	}
    574       1.1    itojun 	while ((rv = pfr_next_token(buf, fp)) == 1)
    575       1.1    itojun 		if (append_addr(b, buf, nonetwork)) {
    576       1.1    itojun 			rv = -1;
    577       1.1    itojun 			break;
    578       1.1    itojun 		}
    579       1.1    itojun 	if (fp != stdin)
    580       1.1    itojun 		fclose(fp);
    581       1.1    itojun 	return (rv);
    582       1.1    itojun }
    583       1.1    itojun 
    584       1.1    itojun int
    585       1.1    itojun pfr_next_token(char buf[BUF_SIZE], FILE *fp)
    586       1.1    itojun {
    587       1.1    itojun 	static char	next_ch = ' ';
    588       1.1    itojun 	int		i = 0;
    589       1.1    itojun 
    590       1.1    itojun 	for (;;) {
    591       1.1    itojun 		/* skip spaces */
    592       1.3       dsl 		while (isspace((unsigned char)next_ch) && !feof(fp))
    593       1.1    itojun 			next_ch = fgetc(fp);
    594       1.1    itojun 		/* remove from '#' until end of line */
    595       1.1    itojun 		if (next_ch == '#')
    596       1.1    itojun 			while (!feof(fp)) {
    597       1.1    itojun 				next_ch = fgetc(fp);
    598       1.1    itojun 				if (next_ch == '\n')
    599       1.1    itojun 					break;
    600       1.1    itojun 			}
    601       1.1    itojun 		else
    602       1.1    itojun 			break;
    603       1.1    itojun 	}
    604       1.1    itojun 	if (feof(fp)) {
    605       1.1    itojun 		next_ch = ' ';
    606       1.1    itojun 		return (0);
    607       1.1    itojun 	}
    608       1.1    itojun 	do {
    609       1.1    itojun 		if (i < BUF_SIZE)
    610       1.1    itojun 			buf[i++] = next_ch;
    611       1.1    itojun 		next_ch = fgetc(fp);
    612       1.3       dsl 	} while (!feof(fp) && !isspace((unsigned char)next_ch));
    613       1.1    itojun 	if (i >= BUF_SIZE) {
    614       1.1    itojun 		errno = EINVAL;
    615       1.1    itojun 		return (-1);
    616       1.1    itojun 	}
    617       1.1    itojun 	buf[i] = '\0';
    618       1.1    itojun 	return (1);
    619       1.1    itojun }
    620       1.1    itojun 
    621       1.1    itojun char *
    622       1.1    itojun pfr_strerror(int errnum)
    623       1.1    itojun {
    624       1.1    itojun 	switch (errnum) {
    625       1.1    itojun 	case ESRCH:
    626       1.1    itojun 		return "Table does not exist";
    627       1.1    itojun 	case ENOENT:
    628       1.1    itojun 		return "Anchor or Ruleset does not exist";
    629       1.1    itojun 	default:
    630       1.1    itojun 		return strerror(errnum);
    631       1.1    itojun 	}
    632       1.1    itojun }
    633