xref: /titanic_50/usr/src/cmd/ipf/lib/common/load_hashnode.c (revision 174bc6499d233e329ecd3d98a880a7b07df16bfa)
1 /*
2  * Copyright (C) 2002 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: load_hashnode.c,v 1.2.4.1 2004/03/06 14:33:28 darrenr Exp $
7  *
8  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
9  * Use is subject to license terms.
10  *
11  * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
12  */
13 
14 #include <fcntl.h>
15 #include <sys/ioctl.h>
16 #include "ipf.h"
17 #include "netinet/ip_lookup.h"
18 #include "netinet/ip_htable.h"
19 #include "ipfzone.h"
20 
21 static int hashfd = -1;
22 
23 
24 int load_hashnode(unit, name, node, iocfunc)
25 int unit;
26 char *name;
27 iphtent_t *node;
28 ioctlfunc_t iocfunc;
29 {
30 	iplookupop_t op;
31 	iphtent_t ipe;
32 	int err;
33 
34 	if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
35 		hashfd = open(IPLOOKUP_NAME, O_RDWR);
36 	if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
37 		return -1;
38 	if (setzone(hashfd) != 0) {
39 		close(hashfd);
40 		return -1;
41 	}
42 
43 	op.iplo_type = IPLT_HASH;
44 	op.iplo_unit = unit;
45 	op.iplo_arg = 0;
46 	op.iplo_size = sizeof(ipe);
47 	op.iplo_struct = &ipe;
48 	strncpy(op.iplo_name, name, sizeof(op.iplo_name));
49 
50 	bzero((char *)&ipe, sizeof(ipe));
51 	ipe.ipe_family = node->ipe_family;
52 	bcopy((char *)&node->ipe_addr, (char *)&ipe.ipe_addr,
53 	      sizeof(ipe.ipe_addr));
54 	bcopy((char *)&node->ipe_mask, (char *)&ipe.ipe_mask,
55 	      sizeof(ipe.ipe_mask));
56 	bcopy((char *)&node->ipe_group, (char *)&ipe.ipe_group,
57 	      sizeof(ipe.ipe_group));
58 
59 	if ((opts & OPT_REMOVE) == 0)
60 		err = (*iocfunc)(hashfd, SIOCLOOKUPADDNODE, &op);
61 	else
62 		err = (*iocfunc)(hashfd, SIOCLOOKUPDELNODE, &op);
63 
64 	if (err != 0)
65 		if (!(opts & OPT_DONOTHING)) {
66 			perror("load_hash:SIOCLOOKUP*NODE");
67 			return -1;
68 		}
69 	return 0;
70 }
71