xref: /titanic_51/usr/src/cmd/ipf/lib/common/load_pool.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
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 
12 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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 
20 static int poolfd = -1;
21 
22 
23 int load_pool(plp, iocfunc)
24 ip_pool_t *plp;
25 ioctlfunc_t iocfunc;
26 {
27 	iplookupop_t op;
28 	ip_pool_node_t *a;
29 	ip_pool_t pool;
30 
31 	if ((poolfd == -1) && ((opts & OPT_DONOTHING) == 0))
32 		poolfd = open(IPLOOKUP_NAME, O_RDWR);
33 	if ((poolfd == -1) && ((opts & OPT_DONOTHING) == 0))
34 		return -1;
35 
36 	op.iplo_unit = plp->ipo_unit;
37 	op.iplo_type = IPLT_POOL;
38 	op.iplo_arg = 0;
39 	strncpy(op.iplo_name, plp->ipo_name, sizeof(op.iplo_name));
40 	op.iplo_size = sizeof(pool);
41 	op.iplo_struct = &pool;
42 	bzero((char *)&pool, sizeof(pool));
43 	strncpy(pool.ipo_name, plp->ipo_name, sizeof(pool.ipo_name));
44 	if (*plp->ipo_name == '\0')
45 		op.iplo_arg |= IPOOL_ANON;
46 
47 	if ((opts & OPT_REMOVE) == 0) {
48 		if ((*iocfunc)(poolfd, SIOCLOOKUPADDTABLE, &op))
49 			if ((opts & OPT_DONOTHING) == 0) {
50 				perror("load_pool:SIOCLOOKUPADDTABLE");
51 				return -1;
52 			}
53 	}
54 
55 	if ((opts & OPT_VERBOSE) != 0) {
56 		pool.ipo_list = plp->ipo_list;
57 		printpool(&pool, bcopywrap, pool.ipo_name, opts);
58 		pool.ipo_list = NULL;
59 	}
60 
61 	for (a = plp->ipo_list; a != NULL; a = a->ipn_next)
62 		load_poolnode(plp->ipo_unit, plp->ipo_name, a, iocfunc);
63 
64 	if ((opts & OPT_REMOVE) != 0) {
65 		if ((*iocfunc)(poolfd, SIOCLOOKUPDELTABLE, &op))
66 			if ((opts & OPT_DONOTHING) == 0) {
67 				perror("load_pool:SIOCLOOKUPDELTABLE");
68 				return -1;
69 			}
70 	}
71 	return 0;
72 }
73