xref: /freebsd/sbin/ipf/libipf/load_hashnode.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 
2 /*
3  * Copyright (C) 2012 by Darren Reed.
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  *
7  * $Id$
8  */
9 
10 #include <fcntl.h>
11 #include <sys/ioctl.h>
12 #include "ipf.h"
13 #include "netinet/ip_lookup.h"
14 #include "netinet/ip_htable.h"
15 
16 
17 int
18 load_hashnode(int unit, char *name, iphtent_t *node, int ttl,
19 	ioctlfunc_t iocfunc)
20 {
21 	iplookupop_t op;
22 	iphtent_t ipe;
23 	char *what;
24 	int err;
25 
26 	if (pool_open() == -1)
27 		return (-1);
28 
29 	op.iplo_type = IPLT_HASH;
30 	op.iplo_unit = unit;
31 	op.iplo_arg = 0;
32 	op.iplo_size = sizeof(ipe);
33 	op.iplo_struct = &ipe;
34 	strncpy(op.iplo_name, name, sizeof(op.iplo_name));
35 
36 	bzero((char *)&ipe, sizeof(ipe));
37 	ipe.ipe_family = node->ipe_family;
38 	ipe.ipe_die = ttl;
39 	bcopy((char *)&node->ipe_addr, (char *)&ipe.ipe_addr,
40 	      sizeof(ipe.ipe_addr));
41 	bcopy((char *)&node->ipe_mask, (char *)&ipe.ipe_mask,
42 	      sizeof(ipe.ipe_mask));
43 	bcopy((char *)&node->ipe_group, (char *)&ipe.ipe_group,
44 	      sizeof(ipe.ipe_group));
45 
46 	if ((opts & OPT_REMOVE) == 0) {
47 		what = "add";
48 		err = pool_ioctl(iocfunc, SIOCLOOKUPADDNODE, &op);
49 	} else {
50 		what = "delete";
51 		err = pool_ioctl(iocfunc, SIOCLOOKUPDELNODE, &op);
52 	}
53 
54 	if (err != 0)
55 		if (!(opts & OPT_DONOTHING)) {
56 			char msg[80];
57 
58 			snprintf(msg, sizeof(msg), "%s node from lookup hash table", what);
59 			return (ipf_perror_fd(pool_fd(), iocfunc, msg));
60 		}
61 	return (0);
62 }
63