17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (C) 2003 by Darren Reed.
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate *
6f4b3ec61Sdh * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
77c478bd9Sstevel@tonic-gate * Use is subject to license terms.
894bdecd9SRob Gulewich *
994bdecd9SRob Gulewich * Copyright (c) 2014, Joyent, Inc. All rights reserved.
10*d7c57852SGary Mills * Copyright 2017 Gary Mills
117c478bd9Sstevel@tonic-gate */
127c478bd9Sstevel@tonic-gate
137c478bd9Sstevel@tonic-gate #include <sys/types.h>
147c478bd9Sstevel@tonic-gate #include <sys/time.h>
157c478bd9Sstevel@tonic-gate #include <sys/param.h>
167c478bd9Sstevel@tonic-gate #include <sys/socket.h>
177c478bd9Sstevel@tonic-gate #if defined(BSD) && (BSD >= 199306)
187c478bd9Sstevel@tonic-gate # include <sys/cdefs.h>
197c478bd9Sstevel@tonic-gate #endif
207c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate #include <net/if.h>
237c478bd9Sstevel@tonic-gate #if __FreeBSD_version >= 300000
247c478bd9Sstevel@tonic-gate # include <net/if_var.h>
257c478bd9Sstevel@tonic-gate #endif
267c478bd9Sstevel@tonic-gate #include <netinet/in.h>
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <netdb.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
37f4b3ec61Sdh #include <nlist.h>
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include "ipf.h"
40f4b3ec61Sdh #include "netinet/ipl.h"
417c478bd9Sstevel@tonic-gate #include "netinet/ip_lookup.h"
427c478bd9Sstevel@tonic-gate #include "netinet/ip_pool.h"
437c478bd9Sstevel@tonic-gate #include "netinet/ip_htable.h"
447c478bd9Sstevel@tonic-gate #include "kmem.h"
4594bdecd9SRob Gulewich #include "ipfzone.h"
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate extern int ippool_yyparse __P((void));
487c478bd9Sstevel@tonic-gate extern int ippool_yydebug;
497c478bd9Sstevel@tonic-gate extern FILE *ippool_yyin;
507c478bd9Sstevel@tonic-gate extern char *optarg;
517c478bd9Sstevel@tonic-gate extern int lineNum;
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate void showpools __P((ip_pool_stat_t *));
547c478bd9Sstevel@tonic-gate void usage __P((char *));
557c478bd9Sstevel@tonic-gate int main __P((int, char **));
567c478bd9Sstevel@tonic-gate int poolcommand __P((int, int, char *[]));
577c478bd9Sstevel@tonic-gate int poolnodecommand __P((int, int, char *[]));
587c478bd9Sstevel@tonic-gate int loadpoolfile __P((int, char *[], char *));
597c478bd9Sstevel@tonic-gate int poollist __P((int, char *[]));
607c478bd9Sstevel@tonic-gate int poolflush __P((int, char *[]));
617c478bd9Sstevel@tonic-gate int poolstats __P((int, char *[]));
627c478bd9Sstevel@tonic-gate int gettype __P((char *, u_int *));
637c478bd9Sstevel@tonic-gate int getrole __P((char *));
64f4b3ec61Sdh void poollist_dead __P((int, char *, int, char *, char *));
65f4b3ec61Sdh void showpools_live(int, int, ip_pool_stat_t *, char *, int);
66f4b3ec61Sdh void showhashs_live(int, int, iphtstat_t *, char *, int);
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate int opts = 0;
697c478bd9Sstevel@tonic-gate int fd = -1;
707c478bd9Sstevel@tonic-gate int use_inet6 = 0;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate
usage(prog)737c478bd9Sstevel@tonic-gate void usage(prog)
747c478bd9Sstevel@tonic-gate char *prog;
757c478bd9Sstevel@tonic-gate {
7694bdecd9SRob Gulewich const char *zoneopt = "[-G|-z zonename] ";
777c478bd9Sstevel@tonic-gate fprintf(stderr, "Usage:\t%s\n", prog);
7894bdecd9SRob Gulewich fprintf(stderr, "\t\t\t-a [-dnv] %s[-m <name>] [-o <role>] -i <ipaddr>[/netmask]\n",
7994bdecd9SRob Gulewich zoneopt);
8094bdecd9SRob Gulewich fprintf(stderr, "\t\t\t-A [-dnv] %s[-m <name>] [-o <role>] [-S <seed>] [-t <type>]\n",
8194bdecd9SRob Gulewich zoneopt);
8294bdecd9SRob Gulewich fprintf(stderr, "\t\t\t-f <file> %s[-dnuv]\n", zoneopt);
8394bdecd9SRob Gulewich fprintf(stderr, "\t\t\t-F [-dv] %s[-o <role>] [-t <type>]\n", zoneopt);
8494bdecd9SRob Gulewich fprintf(stderr, "\t\t\t-l [-dv] %s[-m <name>] [-t <type>]\n", zoneopt);
8594bdecd9SRob Gulewich fprintf(stderr, "\t\t\t-r [-dnv] %s[-m <name>] [-o <role>] -i <ipaddr>[/netmask]\n",
8694bdecd9SRob Gulewich zoneopt);
8794bdecd9SRob Gulewich fprintf(stderr, "\t\t\t-R [-dnv] %s[-m <name>] [-o <role>] [-t <type>]\n",
8894bdecd9SRob Gulewich zoneopt);
8994bdecd9SRob Gulewich fprintf(stderr, "\t\t\t-s [-dtv] %s[-M <core>] [-N <namelist>]\n",
9094bdecd9SRob Gulewich zoneopt);
917c478bd9Sstevel@tonic-gate exit(1);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate
main(argc,argv)957c478bd9Sstevel@tonic-gate int main(argc, argv)
967c478bd9Sstevel@tonic-gate int argc;
977c478bd9Sstevel@tonic-gate char *argv[];
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate int err;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate if (argc < 2)
1027c478bd9Sstevel@tonic-gate usage(argv[0]);
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate switch (getopt(argc, argv, "aAf:FlrRs"))
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate case 'a' :
1077c478bd9Sstevel@tonic-gate err = poolnodecommand(0, argc, argv);
1087c478bd9Sstevel@tonic-gate break;
1097c478bd9Sstevel@tonic-gate case 'A' :
1107c478bd9Sstevel@tonic-gate err = poolcommand(0, argc, argv);
1117c478bd9Sstevel@tonic-gate break;
1127c478bd9Sstevel@tonic-gate case 'f' :
1137c478bd9Sstevel@tonic-gate err = loadpoolfile(argc, argv, optarg);
1147c478bd9Sstevel@tonic-gate break;
1157c478bd9Sstevel@tonic-gate case 'F' :
1167c478bd9Sstevel@tonic-gate err = poolflush(argc, argv);
1177c478bd9Sstevel@tonic-gate break;
1187c478bd9Sstevel@tonic-gate case 'l' :
1197c478bd9Sstevel@tonic-gate err = poollist(argc, argv);
1207c478bd9Sstevel@tonic-gate break;
1217c478bd9Sstevel@tonic-gate case 'r' :
1227c478bd9Sstevel@tonic-gate err = poolnodecommand(1, argc, argv);
1237c478bd9Sstevel@tonic-gate break;
1247c478bd9Sstevel@tonic-gate case 'R' :
1257c478bd9Sstevel@tonic-gate err = poolcommand(1, argc, argv);
1267c478bd9Sstevel@tonic-gate break;
1277c478bd9Sstevel@tonic-gate case 's' :
1287c478bd9Sstevel@tonic-gate err = poolstats(argc, argv);
1297c478bd9Sstevel@tonic-gate break;
1307c478bd9Sstevel@tonic-gate default :
1317c478bd9Sstevel@tonic-gate exit(1);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate return err;
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate
poolnodecommand(remove,argc,argv)1387c478bd9Sstevel@tonic-gate int poolnodecommand(remove, argc, argv)
1397c478bd9Sstevel@tonic-gate int remove, argc;
1407c478bd9Sstevel@tonic-gate char *argv[];
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate char *poolname = NULL, *s;
1437c478bd9Sstevel@tonic-gate int err, c, ipset, role;
1447c478bd9Sstevel@tonic-gate ip_pool_node_t node;
1457c478bd9Sstevel@tonic-gate struct in_addr mask;
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate ipset = 0;
1487c478bd9Sstevel@tonic-gate role = IPL_LOGIPF;
1497c478bd9Sstevel@tonic-gate bzero((char *)&node, sizeof(node));
1507c478bd9Sstevel@tonic-gate
15194bdecd9SRob Gulewich while ((c = getopt(argc, argv, "di:G:m:no:Rvz:")) != -1)
1527c478bd9Sstevel@tonic-gate switch (c)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate case 'd' :
1557c478bd9Sstevel@tonic-gate opts |= OPT_DEBUG;
1567c478bd9Sstevel@tonic-gate ippool_yydebug++;
1577c478bd9Sstevel@tonic-gate break;
15894bdecd9SRob Gulewich case 'G' :
15994bdecd9SRob Gulewich setzonename_global(optarg);
16094bdecd9SRob Gulewich break;
1617c478bd9Sstevel@tonic-gate case 'i' :
1627c478bd9Sstevel@tonic-gate s = strchr(optarg, '/');
1637c478bd9Sstevel@tonic-gate if (s == NULL)
1647c478bd9Sstevel@tonic-gate mask.s_addr = 0xffffffff;
1657c478bd9Sstevel@tonic-gate else if (strchr(s, '.') == NULL) {
1667c478bd9Sstevel@tonic-gate if (ntomask(4, atoi(s + 1), &mask.s_addr) != 0)
1677c478bd9Sstevel@tonic-gate return -1;
1687c478bd9Sstevel@tonic-gate } else {
1697c478bd9Sstevel@tonic-gate mask.s_addr = inet_addr(s + 1);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate if (s != NULL)
1727c478bd9Sstevel@tonic-gate *s = '\0';
1737c478bd9Sstevel@tonic-gate ipset = 1;
174ab25eeb5Syz node.ipn_addr.adf_len = sizeof(node.ipn_addr);
1757c478bd9Sstevel@tonic-gate node.ipn_addr.adf_addr.in4.s_addr = inet_addr(optarg);
176ab25eeb5Syz node.ipn_mask.adf_len = sizeof(node.ipn_mask);
1777c478bd9Sstevel@tonic-gate node.ipn_mask.adf_addr.in4.s_addr = mask.s_addr;
1787c478bd9Sstevel@tonic-gate break;
1797c478bd9Sstevel@tonic-gate case 'm' :
1807c478bd9Sstevel@tonic-gate poolname = optarg;
1817c478bd9Sstevel@tonic-gate break;
1827c478bd9Sstevel@tonic-gate case 'n' :
1837c478bd9Sstevel@tonic-gate opts |= OPT_DONOTHING;
1847c478bd9Sstevel@tonic-gate break;
1857c478bd9Sstevel@tonic-gate case 'o' :
1867c478bd9Sstevel@tonic-gate role = getrole(optarg);
1877c478bd9Sstevel@tonic-gate if (role == IPL_LOGNONE)
1887c478bd9Sstevel@tonic-gate return -1;
1897c478bd9Sstevel@tonic-gate break;
190ab25eeb5Syz case 'R' :
191ab25eeb5Syz opts |= OPT_NORESOLVE;
192ab25eeb5Syz break;
1937c478bd9Sstevel@tonic-gate case 'v' :
1947c478bd9Sstevel@tonic-gate opts |= OPT_VERBOSE;
1957c478bd9Sstevel@tonic-gate break;
19694bdecd9SRob Gulewich case 'z' :
19794bdecd9SRob Gulewich setzonename(optarg);
19894bdecd9SRob Gulewich break;
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate
201ab25eeb5Syz if (opts & OPT_DEBUG)
202ab25eeb5Syz fprintf(stderr, "poolnodecommand: opts = %#x\n", opts);
203ab25eeb5Syz
2047c478bd9Sstevel@tonic-gate if (ipset == 0)
2057c478bd9Sstevel@tonic-gate return -1;
2067c478bd9Sstevel@tonic-gate if (poolname == NULL) {
2077c478bd9Sstevel@tonic-gate fprintf(stderr, "poolname not given with add/remove node\n");
2087c478bd9Sstevel@tonic-gate return -1;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate if (remove == 0)
2127c478bd9Sstevel@tonic-gate err = load_poolnode(0, poolname, &node, ioctl);
2137c478bd9Sstevel@tonic-gate else
2147c478bd9Sstevel@tonic-gate err = remove_poolnode(0, poolname, &node, ioctl);
2157c478bd9Sstevel@tonic-gate return err;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate
poolcommand(remove,argc,argv)2197c478bd9Sstevel@tonic-gate int poolcommand(remove, argc, argv)
2207c478bd9Sstevel@tonic-gate int remove, argc;
2217c478bd9Sstevel@tonic-gate char *argv[];
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate int type, role, c, err;
2247c478bd9Sstevel@tonic-gate char *poolname;
2257c478bd9Sstevel@tonic-gate iphtable_t iph;
2267c478bd9Sstevel@tonic-gate ip_pool_t pool;
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate err = 1;
2297c478bd9Sstevel@tonic-gate role = 0;
2307c478bd9Sstevel@tonic-gate type = 0;
2317c478bd9Sstevel@tonic-gate poolname = NULL;
2327c478bd9Sstevel@tonic-gate role = IPL_LOGIPF;
2337c478bd9Sstevel@tonic-gate bzero((char *)&iph, sizeof(iph));
2347c478bd9Sstevel@tonic-gate bzero((char *)&pool, sizeof(pool));
2357c478bd9Sstevel@tonic-gate
23694bdecd9SRob Gulewich while ((c = getopt(argc, argv, "dG:m:no:RS:t:vz:")) != -1)
2377c478bd9Sstevel@tonic-gate switch (c)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate case 'd' :
2407c478bd9Sstevel@tonic-gate opts |= OPT_DEBUG;
2417c478bd9Sstevel@tonic-gate ippool_yydebug++;
2427c478bd9Sstevel@tonic-gate break;
24394bdecd9SRob Gulewich case 'G' :
24494bdecd9SRob Gulewich setzonename_global(optarg);
24594bdecd9SRob Gulewich break;
2467c478bd9Sstevel@tonic-gate case 'm' :
2477c478bd9Sstevel@tonic-gate poolname = optarg;
2487c478bd9Sstevel@tonic-gate break;
2497c478bd9Sstevel@tonic-gate case 'n' :
2507c478bd9Sstevel@tonic-gate opts |= OPT_DONOTHING;
2517c478bd9Sstevel@tonic-gate break;
2527c478bd9Sstevel@tonic-gate case 'o' :
2537c478bd9Sstevel@tonic-gate role = getrole(optarg);
2547c478bd9Sstevel@tonic-gate if (role == IPL_LOGNONE) {
2557c478bd9Sstevel@tonic-gate fprintf(stderr, "unknown role '%s'\n", optarg);
2567c478bd9Sstevel@tonic-gate return -1;
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate break;
259ab25eeb5Syz case 'R' :
260ab25eeb5Syz opts |= OPT_NORESOLVE;
261ab25eeb5Syz break;
2627c478bd9Sstevel@tonic-gate case 'S' :
2637c478bd9Sstevel@tonic-gate iph.iph_seed = atoi(optarg);
2647c478bd9Sstevel@tonic-gate break;
2657c478bd9Sstevel@tonic-gate case 't' :
2667c478bd9Sstevel@tonic-gate type = gettype(optarg, &iph.iph_type);
2677c478bd9Sstevel@tonic-gate if (type == IPLT_NONE) {
2687c478bd9Sstevel@tonic-gate fprintf(stderr, "unknown type '%s'\n", optarg);
2697c478bd9Sstevel@tonic-gate return -1;
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate break;
2727c478bd9Sstevel@tonic-gate case 'v' :
2737c478bd9Sstevel@tonic-gate opts |= OPT_VERBOSE;
2747c478bd9Sstevel@tonic-gate break;
27594bdecd9SRob Gulewich case 'z' :
27694bdecd9SRob Gulewich setzonename(optarg);
27794bdecd9SRob Gulewich break;
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate
280ab25eeb5Syz if (opts & OPT_DEBUG)
281ab25eeb5Syz fprintf(stderr, "poolcommand: opts = %#x\n", opts);
282ab25eeb5Syz
2837c478bd9Sstevel@tonic-gate if (poolname == NULL) {
2847c478bd9Sstevel@tonic-gate fprintf(stderr, "poolname not given with add/remove pool\n");
2857c478bd9Sstevel@tonic-gate return -1;
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate if (type == IPLT_HASH) {
2897c478bd9Sstevel@tonic-gate strncpy(iph.iph_name, poolname, sizeof(iph.iph_name));
2907c478bd9Sstevel@tonic-gate iph.iph_name[sizeof(iph.iph_name) - 1] = '\0';
2917c478bd9Sstevel@tonic-gate iph.iph_unit = role;
2927c478bd9Sstevel@tonic-gate } else if (type == IPLT_POOL) {
2937c478bd9Sstevel@tonic-gate strncpy(pool.ipo_name, poolname, sizeof(pool.ipo_name));
2947c478bd9Sstevel@tonic-gate pool.ipo_name[sizeof(pool.ipo_name) - 1] = '\0';
2957c478bd9Sstevel@tonic-gate pool.ipo_unit = role;
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate if (remove == 0) {
2997c478bd9Sstevel@tonic-gate switch (type)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate case IPLT_HASH :
3027c478bd9Sstevel@tonic-gate err = load_hash(&iph, NULL, ioctl);
3037c478bd9Sstevel@tonic-gate break;
3047c478bd9Sstevel@tonic-gate case IPLT_POOL :
3057c478bd9Sstevel@tonic-gate err = load_pool(&pool, ioctl);
3067c478bd9Sstevel@tonic-gate break;
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate } else {
3097c478bd9Sstevel@tonic-gate switch (type)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate case IPLT_HASH :
3127c478bd9Sstevel@tonic-gate err = remove_hash(&iph, ioctl);
3137c478bd9Sstevel@tonic-gate break;
3147c478bd9Sstevel@tonic-gate case IPLT_POOL :
3157c478bd9Sstevel@tonic-gate err = remove_pool(&pool, ioctl);
3167c478bd9Sstevel@tonic-gate break;
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate return err;
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate
loadpoolfile(argc,argv,infile)3237c478bd9Sstevel@tonic-gate int loadpoolfile(argc, argv, infile)
3247c478bd9Sstevel@tonic-gate int argc;
3257c478bd9Sstevel@tonic-gate char *argv[], *infile;
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate int c;
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate infile = optarg;
3307c478bd9Sstevel@tonic-gate
33194bdecd9SRob Gulewich while ((c = getopt(argc, argv, "dG:nRuvz:")) != -1)
3327c478bd9Sstevel@tonic-gate switch (c)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate case 'd' :
3357c478bd9Sstevel@tonic-gate opts |= OPT_DEBUG;
3367c478bd9Sstevel@tonic-gate ippool_yydebug++;
3377c478bd9Sstevel@tonic-gate break;
33894bdecd9SRob Gulewich case 'G' :
33994bdecd9SRob Gulewich setzonename_global(optarg);
34094bdecd9SRob Gulewich break;
3417c478bd9Sstevel@tonic-gate case 'n' :
3427c478bd9Sstevel@tonic-gate opts |= OPT_DONOTHING;
3437c478bd9Sstevel@tonic-gate break;
344ab25eeb5Syz case 'R' :
345ab25eeb5Syz opts |= OPT_NORESOLVE;
346ab25eeb5Syz break;
347ab25eeb5Syz case 'u' :
3487c478bd9Sstevel@tonic-gate opts |= OPT_REMOVE;
3497c478bd9Sstevel@tonic-gate break;
3507c478bd9Sstevel@tonic-gate case 'v' :
3517c478bd9Sstevel@tonic-gate opts |= OPT_VERBOSE;
3527c478bd9Sstevel@tonic-gate break;
35394bdecd9SRob Gulewich case 'z' :
35494bdecd9SRob Gulewich setzonename(optarg);
35594bdecd9SRob Gulewich break;
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate
358ab25eeb5Syz if (opts & OPT_DEBUG)
359ab25eeb5Syz fprintf(stderr, "loadpoolfile: opts = %#x\n", opts);
360ab25eeb5Syz
3617c478bd9Sstevel@tonic-gate if (!(opts & OPT_DONOTHING) && (fd == -1)) {
3627c478bd9Sstevel@tonic-gate fd = open(IPLOOKUP_NAME, O_RDWR);
3637c478bd9Sstevel@tonic-gate if (fd == -1) {
3647c478bd9Sstevel@tonic-gate perror("open(IPLOOKUP_NAME)");
3657c478bd9Sstevel@tonic-gate exit(1);
3667c478bd9Sstevel@tonic-gate }
36794bdecd9SRob Gulewich
36894bdecd9SRob Gulewich if (setzone(fd) != 0) {
36994bdecd9SRob Gulewich close(fd);
37094bdecd9SRob Gulewich exit(1);
37194bdecd9SRob Gulewich }
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate if (ippool_parsefile(fd, infile, ioctl) != 0)
3757c478bd9Sstevel@tonic-gate return -1;
3767c478bd9Sstevel@tonic-gate return 0;
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate
poollist(argc,argv)3807c478bd9Sstevel@tonic-gate int poollist(argc, argv)
3817c478bd9Sstevel@tonic-gate int argc;
3827c478bd9Sstevel@tonic-gate char *argv[];
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate char *kernel, *core, *poolname;
3857c478bd9Sstevel@tonic-gate int c, role, type, live_kernel;
386*d7c57852SGary Mills ip_pool_stat_t plstat;
387*d7c57852SGary Mills iphtstat_t htstat;
3887663b816Sml iphtable_t *hptr;
3897c478bd9Sstevel@tonic-gate iplookupop_t op;
3907c478bd9Sstevel@tonic-gate ip_pool_t *ptr;
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate core = NULL;
3937c478bd9Sstevel@tonic-gate kernel = NULL;
3947c478bd9Sstevel@tonic-gate live_kernel = 1;
3957c478bd9Sstevel@tonic-gate type = IPLT_ALL;
3967c478bd9Sstevel@tonic-gate poolname = NULL;
3977c478bd9Sstevel@tonic-gate role = IPL_LOGALL;
3987c478bd9Sstevel@tonic-gate
39994bdecd9SRob Gulewich while ((c = getopt(argc, argv, "dG:m:M:N:o:Rt:vz:")) != -1)
4007c478bd9Sstevel@tonic-gate switch (c)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate case 'd' :
4037c478bd9Sstevel@tonic-gate opts |= OPT_DEBUG;
4047c478bd9Sstevel@tonic-gate break;
40594bdecd9SRob Gulewich case 'G' :
40694bdecd9SRob Gulewich setzonename_global(optarg);
40794bdecd9SRob Gulewich break;
4087c478bd9Sstevel@tonic-gate case 'm' :
4097c478bd9Sstevel@tonic-gate poolname = optarg;
4107c478bd9Sstevel@tonic-gate break;
4117c478bd9Sstevel@tonic-gate case 'M' :
4127c478bd9Sstevel@tonic-gate live_kernel = 0;
4137c478bd9Sstevel@tonic-gate core = optarg;
4147c478bd9Sstevel@tonic-gate break;
4157c478bd9Sstevel@tonic-gate case 'N' :
4167c478bd9Sstevel@tonic-gate live_kernel = 0;
4177c478bd9Sstevel@tonic-gate kernel = optarg;
4187c478bd9Sstevel@tonic-gate break;
4197c478bd9Sstevel@tonic-gate case 'o' :
4207c478bd9Sstevel@tonic-gate role = getrole(optarg);
4217c478bd9Sstevel@tonic-gate if (role == IPL_LOGNONE) {
4227c478bd9Sstevel@tonic-gate fprintf(stderr, "unknown role '%s'\n", optarg);
4237c478bd9Sstevel@tonic-gate return -1;
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate break;
426ab25eeb5Syz case 'R' :
427ab25eeb5Syz opts |= OPT_NORESOLVE;
428ab25eeb5Syz break;
4297c478bd9Sstevel@tonic-gate case 't' :
4307c478bd9Sstevel@tonic-gate type = gettype(optarg, NULL);
4317c478bd9Sstevel@tonic-gate if (type == IPLT_NONE) {
4327c478bd9Sstevel@tonic-gate fprintf(stderr, "unknown type '%s'\n", optarg);
4337c478bd9Sstevel@tonic-gate return -1;
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate break;
4367c478bd9Sstevel@tonic-gate case 'v' :
4377c478bd9Sstevel@tonic-gate opts |= OPT_VERBOSE;
4387c478bd9Sstevel@tonic-gate break;
43994bdecd9SRob Gulewich case 'z' :
44094bdecd9SRob Gulewich setzonename(optarg);
44194bdecd9SRob Gulewich break;
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate
4447663b816Sml if (opts & OPT_DEBUG)
4457663b816Sml fprintf(stderr, "poollist: opts = %#x\n", opts);
4467663b816Sml
4477c478bd9Sstevel@tonic-gate if (!(opts & OPT_DONOTHING) && (fd == -1)) {
4487c478bd9Sstevel@tonic-gate fd = open(IPLOOKUP_NAME, O_RDWR);
4497c478bd9Sstevel@tonic-gate if (fd == -1) {
4507c478bd9Sstevel@tonic-gate perror("open(IPLOOKUP_NAME)");
4517c478bd9Sstevel@tonic-gate exit(1);
4527c478bd9Sstevel@tonic-gate }
45394bdecd9SRob Gulewich
45494bdecd9SRob Gulewich if (setzone(fd) != 0) {
45594bdecd9SRob Gulewich close(fd);
45694bdecd9SRob Gulewich exit(1);
45794bdecd9SRob Gulewich }
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate bzero((char *)&op, sizeof(op));
4617c478bd9Sstevel@tonic-gate if (poolname != NULL) {
4627c478bd9Sstevel@tonic-gate strncpy(op.iplo_name, poolname, sizeof(op.iplo_name));
4637c478bd9Sstevel@tonic-gate op.iplo_name[sizeof(op.iplo_name) - 1] = '\0';
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate op.iplo_unit = role;
4667c478bd9Sstevel@tonic-gate
467f4b3ec61Sdh if (live_kernel == 0) {
468f4b3ec61Sdh poollist_dead(role, poolname, type, kernel, core);
469f4b3ec61Sdh return (0);
470f4b3ec61Sdh }
4717c478bd9Sstevel@tonic-gate
4727663b816Sml if (type == IPLT_ALL || type == IPLT_POOL) {
4737663b816Sml op.iplo_type = IPLT_POOL;
4747663b816Sml op.iplo_size = sizeof(plstat);
4757663b816Sml op.iplo_struct = &plstat;
476f4b3ec61Sdh op.iplo_name[0] = '\0';
477f4b3ec61Sdh op.iplo_arg = 0;
4787663b816Sml
4797663b816Sml if (role != IPL_LOGALL) {
480f4b3ec61Sdh op.iplo_unit = role;
481f4b3ec61Sdh
482f4b3ec61Sdh c = ioctl(fd, SIOCLOOKUPSTAT, &op);
483f4b3ec61Sdh if (c == -1) {
484f4b3ec61Sdh perror("ioctl(SIOCLOOKUPSTAT)");
485f4b3ec61Sdh return -1;
4867c478bd9Sstevel@tonic-gate }
487f4b3ec61Sdh
488f4b3ec61Sdh showpools_live(fd, role, &plstat, poolname, opts);
4897663b816Sml } else {
4907663b816Sml for (role = 0; role <= IPL_LOGMAX; role++) {
491f4b3ec61Sdh op.iplo_unit = role;
492f4b3ec61Sdh
493f4b3ec61Sdh c = ioctl(fd, SIOCLOOKUPSTAT, &op);
494f4b3ec61Sdh if (c == -1) {
495f4b3ec61Sdh perror("ioctl(SIOCLOOKUPSTAT)");
496f4b3ec61Sdh return -1;
4977663b816Sml }
498f4b3ec61Sdh
499f4b3ec61Sdh showpools_live(fd, role, &plstat, poolname, opts);
5007663b816Sml }
501f4b3ec61Sdh
5027663b816Sml role = IPL_LOGALL;
5037663b816Sml }
5047663b816Sml }
5057663b816Sml if (type == IPLT_ALL || type == IPLT_HASH) {
5067663b816Sml op.iplo_type = IPLT_HASH;
5077663b816Sml op.iplo_size = sizeof(htstat);
5087663b816Sml op.iplo_struct = &htstat;
509f4b3ec61Sdh op.iplo_name[0] = '\0';
510f4b3ec61Sdh op.iplo_arg = 0;
5117663b816Sml
5127663b816Sml if (role != IPL_LOGALL) {
513f4b3ec61Sdh op.iplo_unit = role;
514f4b3ec61Sdh
515f4b3ec61Sdh c = ioctl(fd, SIOCLOOKUPSTAT, &op);
516f4b3ec61Sdh if (c == -1) {
517f4b3ec61Sdh perror("ioctl(SIOCLOOKUPSTAT)");
518f4b3ec61Sdh return -1;
5197663b816Sml }
520f4b3ec61Sdh showhashs_live(fd, role, &htstat, poolname, opts);
5217663b816Sml } else {
5227663b816Sml for (role = 0; role <= IPL_LOGMAX; role++) {
5237663b816Sml
5247663b816Sml op.iplo_unit = role;
5257663b816Sml c = ioctl(fd, SIOCLOOKUPSTAT, &op);
5267663b816Sml if (c == -1) {
5277663b816Sml perror("ioctl(SIOCLOOKUPSTAT)");
5287663b816Sml return -1;
5297663b816Sml }
530f4b3ec61Sdh
531f4b3ec61Sdh showhashs_live(fd, role, &htstat, poolname, opts);
5327663b816Sml }
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate return 0;
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate
poollist_dead(role,poolname,type,kernel,core)538f4b3ec61Sdh void poollist_dead(role, poolname, type, kernel, core)
539f4b3ec61Sdh int role, type;
540f4b3ec61Sdh char *poolname, *kernel, *core;
541f4b3ec61Sdh {
542f4b3ec61Sdh iphtable_t *hptr;
543f4b3ec61Sdh ip_pool_t *ptr;
544f4b3ec61Sdh
545f4b3ec61Sdh if (openkmem(kernel, core) == -1)
546f4b3ec61Sdh exit(-1);
547f4b3ec61Sdh
548f4b3ec61Sdh if (type == IPLT_ALL || type == IPLT_POOL) {
549f4b3ec61Sdh ip_pool_t *pools[IPL_LOGSIZE];
550f4b3ec61Sdh struct nlist names[2] = { { "ip_pool_list" } , { "" } };
551f4b3ec61Sdh
552f4b3ec61Sdh if (nlist(kernel, names) != 1)
553f4b3ec61Sdh return;
554f4b3ec61Sdh
555f4b3ec61Sdh bzero(&pools, sizeof(pools));
556f4b3ec61Sdh if (kmemcpy((char *)&pools, names[0].n_value, sizeof(pools)))
557f4b3ec61Sdh return;
558f4b3ec61Sdh
559f4b3ec61Sdh if (role != IPL_LOGALL) {
560f4b3ec61Sdh ptr = pools[role];
561f4b3ec61Sdh while (ptr != NULL) {
562f4b3ec61Sdh ptr = printpool(ptr, kmemcpywrap,
563f4b3ec61Sdh poolname, opts);
564f4b3ec61Sdh }
565f4b3ec61Sdh } else {
566f4b3ec61Sdh for (role = 0; role <= IPL_LOGMAX; role++) {
567f4b3ec61Sdh ptr = pools[role];
568f4b3ec61Sdh while (ptr != NULL) {
569f4b3ec61Sdh ptr = printpool(ptr, kmemcpywrap,
570f4b3ec61Sdh poolname, opts);
571f4b3ec61Sdh }
572f4b3ec61Sdh }
573f4b3ec61Sdh role = IPL_LOGALL;
574f4b3ec61Sdh }
575f4b3ec61Sdh }
576f4b3ec61Sdh if (type == IPLT_ALL || type == IPLT_HASH) {
577f4b3ec61Sdh iphtable_t *tables[IPL_LOGSIZE];
578f4b3ec61Sdh struct nlist names[2] = { { "ipf_htables" } , { "" } };
579f4b3ec61Sdh
580f4b3ec61Sdh if (nlist(kernel, names) != 1)
581f4b3ec61Sdh return;
582f4b3ec61Sdh
583f4b3ec61Sdh bzero(&tables, sizeof(tables));
584f4b3ec61Sdh if (kmemcpy((char *)&tables, names[0].n_value, sizeof(tables)))
585f4b3ec61Sdh return;
586f4b3ec61Sdh
587f4b3ec61Sdh if (role != IPL_LOGALL) {
588f4b3ec61Sdh hptr = tables[role];
589f4b3ec61Sdh while (hptr != NULL) {
590f4b3ec61Sdh hptr = printhash(hptr, kmemcpywrap,
591f4b3ec61Sdh poolname, opts);
592