xref: /illumos-gate/usr/src/cmd/ipf/lib/common/load_pool.c (revision 94bdecd9)
1 /*
2  * Copyright (C) 2002 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: load_pool.c,v 1.14.2.2 2005/02/01 02:44:06 darrenr Exp $
7  *
8  * Copyright 2007 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_pool.h"
19 #include "ipfzone.h"
20 
21 static int poolfd = -1;
22 
23 
load_pool(plp,iocfunc)24 int load_pool(plp, iocfunc)
25 ip_pool_t *plp;
26 ioctlfunc_t iocfunc;
27 {
28 	iplookupop_t op;
29 	ip_pool_node_t *a;
30 	ip_pool_t pool;
31 
32 	if ((poolfd == -1) && ((opts & OPT_DONOTHING) == 0))
33 		poolfd = open(IPLOOKUP_NAME, O_RDWR);
34 	if ((poolfd == -1) && ((opts & OPT_DONOTHING) == 0))
35 		return -1;
36 	if (setzone(poolfd) != 0) {
37 		close(poolfd);
38 		return -1;
39 	}
40 
41 	op.iplo_unit = plp->ipo_unit;
42 	op.iplo_type = IPLT_POOL;
43 	op.iplo_arg = 0;
44 	strncpy(op.iplo_name, plp->ipo_name, sizeof(op.iplo_name));
45 	op.iplo_size = sizeof(pool);
46 	op.iplo_struct = &pool;
47 	bzero((char *)&pool, sizeof(pool));
48 	strncpy(pool.ipo_name, plp->ipo_name, sizeof(pool.ipo_name));
49 	if (*plp->ipo_name == '\0')
50 		op.iplo_arg |= IPOOL_ANON;
51 
52 	if ((opts & OPT_REMOVE) == 0) {
53 		if ((*iocfunc)(poolfd, SIOCLOOKUPADDTABLE, &op))
54 			if ((opts & OPT_DONOTHING) == 0) {
55 				perror("load_pool:SIOCLOOKUPADDTABLE");
56 				return -1;
57 			}
58 	}
59 
60 	if ((opts & OPT_VERBOSE) != 0) {
61 		pool.ipo_list = plp->ipo_list;
62 		printpool(&pool, bcopywrap, pool.ipo_name, opts);
63 		pool.ipo_list = NULL;
64 	}
65 
66 	for (a = plp->ipo_list; a != NULL; a = a->ipn_next)
67 		load_poolnode(plp->ipo_unit, plp->ipo_name, a, iocfunc);
68 
69 	if ((opts & OPT_REMOVE) != 0) {
70 		if ((*iocfunc)(poolfd, SIOCLOOKUPDELTABLE, &op))
71 			if ((opts & OPT_DONOTHING) == 0) {
72 				perror("load_pool:SIOCLOOKUPDELTABLE");
73 				return -1;
74 			}
75 	}
76 	return 0;
77 }
78