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