xref: /illumos-gate/usr/src/cmd/ipf/lib/common/load_pool.c (revision 94bdecd9)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (C) 2002 by Darren Reed.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate  *
6ab25eeb5Syz  * $Id: load_pool.c,v 1.14.2.2 2005/02/01 02:44:06 darrenr Exp $
7f4b3ec61Sdh  *
8f4b3ec61Sdh  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
9f4b3ec61Sdh  * Use is subject to license terms.
10*94bdecd9SRob Gulewich  *
11*94bdecd9SRob Gulewich  * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #include <fcntl.h>
157c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
167c478bd9Sstevel@tonic-gate #include "ipf.h"
177c478bd9Sstevel@tonic-gate #include "netinet/ip_lookup.h"
187c478bd9Sstevel@tonic-gate #include "netinet/ip_pool.h"
19*94bdecd9SRob Gulewich #include "ipfzone.h"
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate static int poolfd = -1;
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate 
load_pool(plp,iocfunc)247c478bd9Sstevel@tonic-gate int load_pool(plp, iocfunc)
257c478bd9Sstevel@tonic-gate ip_pool_t *plp;
267c478bd9Sstevel@tonic-gate ioctlfunc_t iocfunc;
277c478bd9Sstevel@tonic-gate {
287c478bd9Sstevel@tonic-gate 	iplookupop_t op;
297c478bd9Sstevel@tonic-gate 	ip_pool_node_t *a;
307c478bd9Sstevel@tonic-gate 	ip_pool_t pool;
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate 	if ((poolfd == -1) && ((opts & OPT_DONOTHING) == 0))
337c478bd9Sstevel@tonic-gate 		poolfd = open(IPLOOKUP_NAME, O_RDWR);
347c478bd9Sstevel@tonic-gate 	if ((poolfd == -1) && ((opts & OPT_DONOTHING) == 0))
357c478bd9Sstevel@tonic-gate 		return -1;
36*94bdecd9SRob Gulewich 	if (setzone(poolfd) != 0) {
37*94bdecd9SRob Gulewich 		close(poolfd);
38*94bdecd9SRob Gulewich 		return -1;
39*94bdecd9SRob Gulewich 	}
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 	op.iplo_unit = plp->ipo_unit;
427c478bd9Sstevel@tonic-gate 	op.iplo_type = IPLT_POOL;
437c478bd9Sstevel@tonic-gate 	op.iplo_arg = 0;
447c478bd9Sstevel@tonic-gate 	strncpy(op.iplo_name, plp->ipo_name, sizeof(op.iplo_name));
457c478bd9Sstevel@tonic-gate 	op.iplo_size = sizeof(pool);
467c478bd9Sstevel@tonic-gate 	op.iplo_struct = &pool;
477c478bd9Sstevel@tonic-gate 	bzero((char *)&pool, sizeof(pool));
487c478bd9Sstevel@tonic-gate 	strncpy(pool.ipo_name, plp->ipo_name, sizeof(pool.ipo_name));
497c478bd9Sstevel@tonic-gate 	if (*plp->ipo_name == '\0')
507c478bd9Sstevel@tonic-gate 		op.iplo_arg |= IPOOL_ANON;
517c478bd9Sstevel@tonic-gate 
52ab25eeb5Syz 	if ((opts & OPT_REMOVE) == 0) {
53ab25eeb5Syz 		if ((*iocfunc)(poolfd, SIOCLOOKUPADDTABLE, &op))
54ab25eeb5Syz 			if ((opts & OPT_DONOTHING) == 0) {
55ab25eeb5Syz 				perror("load_pool:SIOCLOOKUPADDTABLE");
56ab25eeb5Syz 				return -1;
57ab25eeb5Syz 			}
58ab25eeb5Syz 	}
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if ((opts & OPT_VERBOSE) != 0) {
617c478bd9Sstevel@tonic-gate 		pool.ipo_list = plp->ipo_list;
62ab25eeb5Syz 		printpool(&pool, bcopywrap, pool.ipo_name, opts);
637c478bd9Sstevel@tonic-gate 		pool.ipo_list = NULL;
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	for (a = plp->ipo_list; a != NULL; a = a->ipn_next)
677c478bd9Sstevel@tonic-gate 		load_poolnode(plp->ipo_unit, plp->ipo_name, a, iocfunc);
687c478bd9Sstevel@tonic-gate 
69ab25eeb5Syz 	if ((opts & OPT_REMOVE) != 0) {
70ab25eeb5Syz 		if ((*iocfunc)(poolfd, SIOCLOOKUPDELTABLE, &op))
71ab25eeb5Syz 			if ((opts & OPT_DONOTHING) == 0) {
72ab25eeb5Syz 				perror("load_pool:SIOCLOOKUPDELTABLE");
73ab25eeb5Syz 				return -1;
74ab25eeb5Syz 			}
75ab25eeb5Syz 	}
767c478bd9Sstevel@tonic-gate 	return 0;
777c478bd9Sstevel@tonic-gate }
78