141edb306SCy Schubert 241edb306SCy Schubert /* 341edb306SCy Schubert * Copyright (C) 2012 by Darren Reed. 441edb306SCy Schubert * 541edb306SCy Schubert * See the IPFILTER.LICENCE file for details on licencing. 641edb306SCy Schubert * 741edb306SCy Schubert * $Id$ 841edb306SCy Schubert */ 941edb306SCy Schubert 1041edb306SCy Schubert #include <fcntl.h> 1141edb306SCy Schubert #include <sys/ioctl.h> 1241edb306SCy Schubert #include "ipf.h" 1341edb306SCy Schubert #include "netinet/ip_lookup.h" 1441edb306SCy Schubert #include "netinet/ip_pool.h" 1541edb306SCy Schubert 1641edb306SCy Schubert 1741edb306SCy Schubert int 18efeb8bffSCy Schubert load_poolnode(int role, char *name, ip_pool_node_t *node, int ttl, 19efeb8bffSCy Schubert ioctlfunc_t iocfunc) 2041edb306SCy Schubert { 2141edb306SCy Schubert ip_pool_node_t pn; 2241edb306SCy Schubert iplookupop_t op; 2341edb306SCy Schubert char *what; 2441edb306SCy Schubert int err; 2541edb306SCy Schubert 2641edb306SCy Schubert if (pool_open() == -1) 272582ae57SCy Schubert return (-1); 2841edb306SCy Schubert 2941edb306SCy Schubert op.iplo_unit = role; 3041edb306SCy Schubert op.iplo_type = IPLT_POOL; 3141edb306SCy Schubert op.iplo_arg = 0; 3241edb306SCy Schubert op.iplo_struct = &pn; 3341edb306SCy Schubert op.iplo_size = sizeof(pn); 3441edb306SCy Schubert strncpy(op.iplo_name, name, sizeof(op.iplo_name)); 3541edb306SCy Schubert 3641edb306SCy Schubert bzero((char *)&pn, sizeof(pn)); 3741edb306SCy Schubert bcopy((char *)&node->ipn_addr, (char *)&pn.ipn_addr, 3841edb306SCy Schubert sizeof(pn.ipn_addr)); 3941edb306SCy Schubert bcopy((char *)&node->ipn_mask, (char *)&pn.ipn_mask, 4041edb306SCy Schubert sizeof(pn.ipn_mask)); 4141edb306SCy Schubert pn.ipn_info = node->ipn_info; 4241edb306SCy Schubert pn.ipn_die = ttl; 4341edb306SCy Schubert strncpy(pn.ipn_name, node->ipn_name, sizeof(pn.ipn_name)); 4441edb306SCy Schubert 4541edb306SCy Schubert if ((opts & OPT_REMOVE) == 0) { 4641edb306SCy Schubert what = "add"; 4741edb306SCy Schubert err = pool_ioctl(iocfunc, SIOCLOOKUPADDNODE, &op); 4841edb306SCy Schubert } else { 4941edb306SCy Schubert what = "delete"; 5041edb306SCy Schubert err = pool_ioctl(iocfunc, SIOCLOOKUPDELNODE, &op); 5141edb306SCy Schubert } 5241edb306SCy Schubert 5341edb306SCy Schubert if (err != 0) { 5441edb306SCy Schubert if ((opts & OPT_DONOTHING) == 0) { 5542935716SCy Schubert char msg[255]; 56*3a2cb65bSCy Schubert char ipaddr[80], mask_msg[10], mask[8]; 5741edb306SCy Schubert 58*3a2cb65bSCy Schubert inet_ntop(pn.ipn_addr.adf_family, 59*3a2cb65bSCy Schubert pn.ipn_addr.adf_addr.vptr, ipaddr, 60*3a2cb65bSCy Schubert sizeof(ipaddr)); 61*3a2cb65bSCy Schubert 62*3a2cb65bSCy Schubert #ifdef USE_INET6 63*3a2cb65bSCy Schubert if (pn.ipn_mask.adf_family == AF_INET) { 64*3a2cb65bSCy Schubert #endif 65*3a2cb65bSCy Schubert inet_ntop(pn.ipn_mask.adf_family, 66*3a2cb65bSCy Schubert pn.ipn_mask.adf_addr.vptr, mask, 67*3a2cb65bSCy Schubert sizeof(mask)); 68*3a2cb65bSCy Schubert mask_msg[0]='/'; 69*3a2cb65bSCy Schubert mask_msg[1]='\0'; 70*3a2cb65bSCy Schubert strlcat(mask_msg, mask, sizeof(mask_msg)); 71*3a2cb65bSCy Schubert #ifdef USE_INET6 72*3a2cb65bSCy Schubert } else { 73*3a2cb65bSCy Schubert mask_msg[0]='\0'; 74*3a2cb65bSCy Schubert } 75*3a2cb65bSCy Schubert #endif 76*3a2cb65bSCy Schubert 77*3a2cb65bSCy Schubert snprintf(msg, sizeof(msg), "%s pool(%s) node(%s%s)", 78*3a2cb65bSCy Schubert what, name, ipaddr, mask_msg); 792582ae57SCy Schubert return (ipf_perror_fd(pool_fd(), iocfunc, msg)); 8041edb306SCy Schubert } 8141edb306SCy Schubert } 8241edb306SCy Schubert 832582ae57SCy Schubert return (0); 8441edb306SCy Schubert } 85