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