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_poolnode.c,v 1.3.2.1 2004/03/06 14:33:29 darrenr Exp $
7*94bdecd9SRob Gulewich  *
8*94bdecd9SRob Gulewich  * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
97c478bd9Sstevel@tonic-gate  */
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate #include <fcntl.h>
127c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
137c478bd9Sstevel@tonic-gate #include "ipf.h"
147c478bd9Sstevel@tonic-gate #include "netinet/ip_lookup.h"
157c478bd9Sstevel@tonic-gate #include "netinet/ip_pool.h"
16*94bdecd9SRob Gulewich #include "ipfzone.h"
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate static int poolfd = -1;
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate 
load_poolnode(role,name,node,iocfunc)217c478bd9Sstevel@tonic-gate int load_poolnode(role, name, node, iocfunc)
227c478bd9Sstevel@tonic-gate int role;
237c478bd9Sstevel@tonic-gate char *name;
247c478bd9Sstevel@tonic-gate ip_pool_node_t *node;
257c478bd9Sstevel@tonic-gate ioctlfunc_t iocfunc;
267c478bd9Sstevel@tonic-gate {
277c478bd9Sstevel@tonic-gate 	ip_pool_node_t pn;
287c478bd9Sstevel@tonic-gate 	iplookupop_t op;
29ab25eeb5Syz 	int err;
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 	if ((poolfd == -1) && ((opts & OPT_DONOTHING) == 0))
327c478bd9Sstevel@tonic-gate 		poolfd = open(IPLOOKUP_NAME, O_RDWR);
337c478bd9Sstevel@tonic-gate 	if ((poolfd == -1) && ((opts & OPT_DONOTHING) == 0))
347c478bd9Sstevel@tonic-gate 		return -1;
35*94bdecd9SRob Gulewich 	if (setzone(poolfd) != 0) {
36*94bdecd9SRob Gulewich 		close(poolfd);
37*94bdecd9SRob Gulewich 		return -1;
38*94bdecd9SRob Gulewich 	}
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 	op.iplo_unit = role;
417c478bd9Sstevel@tonic-gate 	op.iplo_type = IPLT_POOL;
427c478bd9Sstevel@tonic-gate 	op.iplo_arg = 0;
437c478bd9Sstevel@tonic-gate 	op.iplo_struct = &pn;
447c478bd9Sstevel@tonic-gate 	op.iplo_size = sizeof(pn);
457c478bd9Sstevel@tonic-gate 	strncpy(op.iplo_name, name, sizeof(op.iplo_name));
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	bzero((char *)&pn, sizeof(pn));
487c478bd9Sstevel@tonic-gate 	bcopy((char *)&node->ipn_addr, (char *)&pn.ipn_addr,
497c478bd9Sstevel@tonic-gate 	      sizeof(pn.ipn_addr));
507c478bd9Sstevel@tonic-gate 	bcopy((char *)&node->ipn_mask, (char *)&pn.ipn_mask,
517c478bd9Sstevel@tonic-gate 	      sizeof(pn.ipn_mask));
527c478bd9Sstevel@tonic-gate 	pn.ipn_info = node->ipn_info;
537c478bd9Sstevel@tonic-gate 	strncpy(pn.ipn_name, node->ipn_name, sizeof(pn.ipn_name));
547c478bd9Sstevel@tonic-gate 
55ab25eeb5Syz 	if ((opts & OPT_REMOVE) == 0)
56ab25eeb5Syz 		err = (*iocfunc)(poolfd, SIOCLOOKUPADDNODE, &op);
57ab25eeb5Syz 	else
58ab25eeb5Syz 		err = (*iocfunc)(poolfd, SIOCLOOKUPDELNODE, &op);
59ab25eeb5Syz 
60ab25eeb5Syz 	if (err != 0) {
617c478bd9Sstevel@tonic-gate 		if ((opts & OPT_DONOTHING) == 0) {
62ab25eeb5Syz 			perror("load_pool:SIOCLOOKUP*NODE");
637c478bd9Sstevel@tonic-gate 			return -1;
647c478bd9Sstevel@tonic-gate 		}
657c478bd9Sstevel@tonic-gate 	}
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	return 0;
687c478bd9Sstevel@tonic-gate }
69