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 * 6*7663b816Sml37995 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 77c478bd9Sstevel@tonic-gate * Use is subject to license terms. 87c478bd9Sstevel@tonic-gate */ 97c478bd9Sstevel@tonic-gate 107c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 117c478bd9Sstevel@tonic-gate 127c478bd9Sstevel@tonic-gate #include <sys/types.h> 137c478bd9Sstevel@tonic-gate #include <sys/time.h> 147c478bd9Sstevel@tonic-gate #include <sys/param.h> 157c478bd9Sstevel@tonic-gate #include <sys/socket.h> 167c478bd9Sstevel@tonic-gate #if defined(BSD) && (BSD >= 199306) 177c478bd9Sstevel@tonic-gate # include <sys/cdefs.h> 187c478bd9Sstevel@tonic-gate #endif 197c478bd9Sstevel@tonic-gate #include <sys/ioctl.h> 207c478bd9Sstevel@tonic-gate 217c478bd9Sstevel@tonic-gate #include <net/if.h> 227c478bd9Sstevel@tonic-gate #if __FreeBSD_version >= 300000 237c478bd9Sstevel@tonic-gate # include <net/if_var.h> 247c478bd9Sstevel@tonic-gate #endif 257c478bd9Sstevel@tonic-gate #include <netinet/in.h> 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <stdio.h> 307c478bd9Sstevel@tonic-gate #include <fcntl.h> 317c478bd9Sstevel@tonic-gate #include <stdlib.h> 327c478bd9Sstevel@tonic-gate #include <string.h> 337c478bd9Sstevel@tonic-gate #include <netdb.h> 347c478bd9Sstevel@tonic-gate #include <ctype.h> 357c478bd9Sstevel@tonic-gate #include <unistd.h> 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #include "ipf.h" 387c478bd9Sstevel@tonic-gate #if SOLARIS2 >= 10 397c478bd9Sstevel@tonic-gate #include "ip_lookup.h" 407c478bd9Sstevel@tonic-gate #include "ip_pool.h" 417c478bd9Sstevel@tonic-gate #include "ip_htable.h" 427c478bd9Sstevel@tonic-gate #else 437c478bd9Sstevel@tonic-gate #include "netinet/ip_lookup.h" 447c478bd9Sstevel@tonic-gate #include "netinet/ip_pool.h" 457c478bd9Sstevel@tonic-gate #include "netinet/ip_htable.h" 467c478bd9Sstevel@tonic-gate #endif 477c478bd9Sstevel@tonic-gate #include "kmem.h" 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate extern int ippool_yyparse __P((void)); 517c478bd9Sstevel@tonic-gate extern int ippool_yydebug; 527c478bd9Sstevel@tonic-gate extern FILE *ippool_yyin; 537c478bd9Sstevel@tonic-gate extern char *optarg; 547c478bd9Sstevel@tonic-gate extern int lineNum; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate void showpools __P((ip_pool_stat_t *)); 577c478bd9Sstevel@tonic-gate void usage __P((char *)); 587c478bd9Sstevel@tonic-gate int main __P((int, char **)); 597c478bd9Sstevel@tonic-gate int poolcommand __P((int, int, char *[])); 607c478bd9Sstevel@tonic-gate int poolnodecommand __P((int, int, char *[])); 617c478bd9Sstevel@tonic-gate int loadpoolfile __P((int, char *[], char *)); 627c478bd9Sstevel@tonic-gate int poollist __P((int, char *[])); 637c478bd9Sstevel@tonic-gate int poolflush __P((int, char *[])); 647c478bd9Sstevel@tonic-gate int poolstats __P((int, char *[])); 657c478bd9Sstevel@tonic-gate int gettype __P((char *, u_int *)); 667c478bd9Sstevel@tonic-gate int getrole __P((char *)); 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 737c478bd9Sstevel@tonic-gate void usage(prog) 747c478bd9Sstevel@tonic-gate char *prog; 757c478bd9Sstevel@tonic-gate { 767c478bd9Sstevel@tonic-gate fprintf(stderr, "Usage:\t%s\n", prog); 777c478bd9Sstevel@tonic-gate fprintf(stderr, "\t\t\t-a [-dnv] [-m <name>] [-o <role>] -i <ipaddr>[/netmask]\n"); 787c478bd9Sstevel@tonic-gate fprintf(stderr, "\t\t\t-A [-dnv] [-m <name>] [-o <role>] [-S <seed>] [-t <type>]\n"); 797c478bd9Sstevel@tonic-gate fprintf(stderr, "\t\t\t-f <file> [-dnuv]\n"); 807c478bd9Sstevel@tonic-gate fprintf(stderr, "\t\t\t-F [-dv] [-o <role>] [-t <type>]\n"); 817c478bd9Sstevel@tonic-gate fprintf(stderr, "\t\t\t-l [-dv] [-m <name>] [-t <type>]\n"); 827c478bd9Sstevel@tonic-gate fprintf(stderr, "\t\t\t-r [-dnv] [-m <name>] [-o <role>] -i <ipaddr>[/netmask]\n"); 837c478bd9Sstevel@tonic-gate fprintf(stderr, "\t\t\t-R [-dnv] [-m <name>] [-o <role>] [-t <type>]\n"); 847c478bd9Sstevel@tonic-gate fprintf(stderr, "\t\t\t-s [-dtv] [-M <core>] [-N <namelist>]\n"); 857c478bd9Sstevel@tonic-gate exit(1); 867c478bd9Sstevel@tonic-gate } 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate int main(argc, argv) 907c478bd9Sstevel@tonic-gate int argc; 917c478bd9Sstevel@tonic-gate char *argv[]; 927c478bd9Sstevel@tonic-gate { 937c478bd9Sstevel@tonic-gate int err; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate if (argc < 2) 967c478bd9Sstevel@tonic-gate usage(argv[0]); 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate switch (getopt(argc, argv, "aAf:FlrRs")) 997c478bd9Sstevel@tonic-gate { 1007c478bd9Sstevel@tonic-gate case 'a' : 1017c478bd9Sstevel@tonic-gate err = poolnodecommand(0, argc, argv); 1027c478bd9Sstevel@tonic-gate break; 1037c478bd9Sstevel@tonic-gate case 'A' : 1047c478bd9Sstevel@tonic-gate err = poolcommand(0, argc, argv); 1057c478bd9Sstevel@tonic-gate break; 1067c478bd9Sstevel@tonic-gate case 'f' : 1077c478bd9Sstevel@tonic-gate err = loadpoolfile(argc, argv, optarg); 1087c478bd9Sstevel@tonic-gate break; 1097c478bd9Sstevel@tonic-gate case 'F' : 1107c478bd9Sstevel@tonic-gate err = poolflush(argc, argv); 1117c478bd9Sstevel@tonic-gate break; 1127c478bd9Sstevel@tonic-gate case 'l' : 1137c478bd9Sstevel@tonic-gate err = poollist(argc, argv); 1147c478bd9Sstevel@tonic-gate break; 1157c478bd9Sstevel@tonic-gate case 'r' : 1167c478bd9Sstevel@tonic-gate err = poolnodecommand(1, argc, argv); 1177c478bd9Sstevel@tonic-gate break; 1187c478bd9Sstevel@tonic-gate case 'R' : 1197c478bd9Sstevel@tonic-gate err = poolcommand(1, argc, argv); 1207c478bd9Sstevel@tonic-gate break; 1217c478bd9Sstevel@tonic-gate case 's' : 1227c478bd9Sstevel@tonic-gate err = poolstats(argc, argv); 1237c478bd9Sstevel@tonic-gate break; 1247c478bd9Sstevel@tonic-gate default : 1257c478bd9Sstevel@tonic-gate exit(1); 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate return err; 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate int poolnodecommand(remove, argc, argv) 1337c478bd9Sstevel@tonic-gate int remove, argc; 1347c478bd9Sstevel@tonic-gate char *argv[]; 1357c478bd9Sstevel@tonic-gate { 1367c478bd9Sstevel@tonic-gate char *poolname = NULL, *s; 1377c478bd9Sstevel@tonic-gate int err, c, ipset, role; 1387c478bd9Sstevel@tonic-gate ip_pool_node_t node; 1397c478bd9Sstevel@tonic-gate struct in_addr mask; 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate ipset = 0; 1427c478bd9Sstevel@tonic-gate role = IPL_LOGIPF; 1437c478bd9Sstevel@tonic-gate bzero((char *)&node, sizeof(node)); 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "di:m:no:v")) != -1) 1467c478bd9Sstevel@tonic-gate switch (c) 1477c478bd9Sstevel@tonic-gate { 1487c478bd9Sstevel@tonic-gate case 'd' : 1497c478bd9Sstevel@tonic-gate opts |= OPT_DEBUG; 1507c478bd9Sstevel@tonic-gate ippool_yydebug++; 1517c478bd9Sstevel@tonic-gate break; 1527c478bd9Sstevel@tonic-gate case 'i' : 1537c478bd9Sstevel@tonic-gate s = strchr(optarg, '/'); 1547c478bd9Sstevel@tonic-gate if (s == NULL) 1557c478bd9Sstevel@tonic-gate mask.s_addr = 0xffffffff; 1567c478bd9Sstevel@tonic-gate else if (strchr(s, '.') == NULL) { 1577c478bd9Sstevel@tonic-gate if (ntomask(4, atoi(s + 1), &mask.s_addr) != 0) 1587c478bd9Sstevel@tonic-gate return -1; 1597c478bd9Sstevel@tonic-gate } else { 1607c478bd9Sstevel@tonic-gate mask.s_addr = inet_addr(s + 1); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate if (s != NULL) 1637c478bd9Sstevel@tonic-gate *s = '\0'; 1647c478bd9Sstevel@tonic-gate ipset = 1; 1657c478bd9Sstevel@tonic-gate node.ipn_addr.adf_addr.in4.s_addr = inet_addr(optarg); 1667c478bd9Sstevel@tonic-gate node.ipn_mask.adf_addr.in4.s_addr = mask.s_addr; 1677c478bd9Sstevel@tonic-gate break; 1687c478bd9Sstevel@tonic-gate case 'm' : 1697c478bd9Sstevel@tonic-gate poolname = optarg; 1707c478bd9Sstevel@tonic-gate break; 1717c478bd9Sstevel@tonic-gate case 'n' : 1727c478bd9Sstevel@tonic-gate opts |= OPT_DONOTHING; 1737c478bd9Sstevel@tonic-gate break; 1747c478bd9Sstevel@tonic-gate case 'o' : 1757c478bd9Sstevel@tonic-gate role = getrole(optarg); 1767c478bd9Sstevel@tonic-gate if (role == IPL_LOGNONE) 1777c478bd9Sstevel@tonic-gate return -1; 1787c478bd9Sstevel@tonic-gate break; 1797c478bd9Sstevel@tonic-gate case 'v' : 1807c478bd9Sstevel@tonic-gate opts |= OPT_VERBOSE; 1817c478bd9Sstevel@tonic-gate break; 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate if (ipset == 0) 1857c478bd9Sstevel@tonic-gate return -1; 1867c478bd9Sstevel@tonic-gate if (poolname == NULL) { 1877c478bd9Sstevel@tonic-gate fprintf(stderr, "poolname not given with add/remove node\n"); 1887c478bd9Sstevel@tonic-gate return -1; 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate if (remove == 0) 1927c478bd9Sstevel@tonic-gate err = load_poolnode(0, poolname, &node, ioctl); 1937c478bd9Sstevel@tonic-gate else 1947c478bd9Sstevel@tonic-gate err = remove_poolnode(0, poolname, &node, ioctl); 1957c478bd9Sstevel@tonic-gate return err; 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate int poolcommand(remove, argc, argv) 2007c478bd9Sstevel@tonic-gate int remove, argc; 2017c478bd9Sstevel@tonic-gate char *argv[]; 2027c478bd9Sstevel@tonic-gate { 2037c478bd9Sstevel@tonic-gate int type, role, c, err; 2047c478bd9Sstevel@tonic-gate char *poolname; 2057c478bd9Sstevel@tonic-gate iphtable_t iph; 2067c478bd9Sstevel@tonic-gate ip_pool_t pool; 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate err = 1; 2097c478bd9Sstevel@tonic-gate role = 0; 2107c478bd9Sstevel@tonic-gate type = 0; 2117c478bd9Sstevel@tonic-gate poolname = NULL; 2127c478bd9Sstevel@tonic-gate role = IPL_LOGIPF; 2137c478bd9Sstevel@tonic-gate bzero((char *)&iph, sizeof(iph)); 2147c478bd9Sstevel@tonic-gate bzero((char *)&pool, sizeof(pool)); 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "dm:no:S:t:v")) != -1) 2177c478bd9Sstevel@tonic-gate switch (c) 2187c478bd9Sstevel@tonic-gate { 2197c478bd9Sstevel@tonic-gate case 'd' : 2207c478bd9Sstevel@tonic-gate opts |= OPT_DEBUG; 2217c478bd9Sstevel@tonic-gate ippool_yydebug++; 2227c478bd9Sstevel@tonic-gate break; 2237c478bd9Sstevel@tonic-gate case 'm' : 2247c478bd9Sstevel@tonic-gate poolname = optarg; 2257c478bd9Sstevel@tonic-gate break; 2267c478bd9Sstevel@tonic-gate case 'n' : 2277c478bd9Sstevel@tonic-gate opts |= OPT_DONOTHING; 2287c478bd9Sstevel@tonic-gate break; 2297c478bd9Sstevel@tonic-gate case 'o' : 2307c478bd9Sstevel@tonic-gate role = getrole(optarg); 2317c478bd9Sstevel@tonic-gate if (role == IPL_LOGNONE) { 2327c478bd9Sstevel@tonic-gate fprintf(stderr, "unknown role '%s'\n", optarg); 2337c478bd9Sstevel@tonic-gate return -1; 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate break; 2367c478bd9Sstevel@tonic-gate case 'S' : 2377c478bd9Sstevel@tonic-gate iph.iph_seed = atoi(optarg); 2387c478bd9Sstevel@tonic-gate break; 2397c478bd9Sstevel@tonic-gate case 't' : 2407c478bd9Sstevel@tonic-gate type = gettype(optarg, &iph.iph_type); 2417c478bd9Sstevel@tonic-gate if (type == IPLT_NONE) { 2427c478bd9Sstevel@tonic-gate fprintf(stderr, "unknown type '%s'\n", optarg); 2437c478bd9Sstevel@tonic-gate return -1; 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate break; 2467c478bd9Sstevel@tonic-gate case 'v' : 2477c478bd9Sstevel@tonic-gate opts |= OPT_VERBOSE; 2487c478bd9Sstevel@tonic-gate break; 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate if (poolname == NULL) { 2527c478bd9Sstevel@tonic-gate fprintf(stderr, "poolname not given with add/remove pool\n"); 2537c478bd9Sstevel@tonic-gate return -1; 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate if (type == IPLT_HASH) { 2577c478bd9Sstevel@tonic-gate strncpy(iph.iph_name, poolname, sizeof(iph.iph_name)); 2587c478bd9Sstevel@tonic-gate iph.iph_name[sizeof(iph.iph_name) - 1] = '\0'; 2597c478bd9Sstevel@tonic-gate iph.iph_unit = role; 2607c478bd9Sstevel@tonic-gate } else if (type == IPLT_POOL) { 2617c478bd9Sstevel@tonic-gate strncpy(pool.ipo_name, poolname, sizeof(pool.ipo_name)); 2627c478bd9Sstevel@tonic-gate pool.ipo_name[sizeof(pool.ipo_name) - 1] = '\0'; 2637c478bd9Sstevel@tonic-gate pool.ipo_unit = role; 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate if (remove == 0) { 2677c478bd9Sstevel@tonic-gate switch (type) 2687c478bd9Sstevel@tonic-gate { 2697c478bd9Sstevel@tonic-gate case IPLT_HASH : 2707c478bd9Sstevel@tonic-gate err = load_hash(&iph, NULL, ioctl); 2717c478bd9Sstevel@tonic-gate break; 2727c478bd9Sstevel@tonic-gate case IPLT_POOL : 2737c478bd9Sstevel@tonic-gate err = load_pool(&pool, ioctl); 2747c478bd9Sstevel@tonic-gate break; 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate } else { 2777c478bd9Sstevel@tonic-gate switch (type) 2787c478bd9Sstevel@tonic-gate { 2797c478bd9Sstevel@tonic-gate case IPLT_HASH : 2807c478bd9Sstevel@tonic-gate err = remove_hash(&iph, ioctl); 2817c478bd9Sstevel@tonic-gate break; 2827c478bd9Sstevel@tonic-gate case IPLT_POOL : 2837c478bd9Sstevel@tonic-gate err = remove_pool(&pool, ioctl); 2847c478bd9Sstevel@tonic-gate break; 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate return err; 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate int loadpoolfile(argc, argv, infile) 2927c478bd9Sstevel@tonic-gate int argc; 2937c478bd9Sstevel@tonic-gate char *argv[], *infile; 2947c478bd9Sstevel@tonic-gate { 2957c478bd9Sstevel@tonic-gate int c; 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate infile = optarg; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "dnrv")) != -1) 3007c478bd9Sstevel@tonic-gate switch (c) 3017c478bd9Sstevel@tonic-gate { 3027c478bd9Sstevel@tonic-gate case 'd' : 3037c478bd9Sstevel@tonic-gate opts |= OPT_DEBUG; 3047c478bd9Sstevel@tonic-gate ippool_yydebug++; 3057c478bd9Sstevel@tonic-gate break; 3067c478bd9Sstevel@tonic-gate case 'n' : 3077c478bd9Sstevel@tonic-gate opts |= OPT_DONOTHING; 3087c478bd9Sstevel@tonic-gate break; 3097c478bd9Sstevel@tonic-gate case 'r' : 3107c478bd9Sstevel@tonic-gate opts |= OPT_REMOVE; 3117c478bd9Sstevel@tonic-gate break; 3127c478bd9Sstevel@tonic-gate case 'v' : 3137c478bd9Sstevel@tonic-gate opts |= OPT_VERBOSE; 3147c478bd9Sstevel@tonic-gate break; 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate if (!(opts & OPT_DONOTHING) && (fd == -1)) { 3187c478bd9Sstevel@tonic-gate fd = open(IPLOOKUP_NAME, O_RDWR); 3197c478bd9Sstevel@tonic-gate if (fd == -1) { 3207c478bd9Sstevel@tonic-gate perror("open(IPLOOKUP_NAME)"); 3217c478bd9Sstevel@tonic-gate exit(1); 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate if (ippool_parsefile(fd, infile, ioctl) != 0) 3267c478bd9Sstevel@tonic-gate return -1; 3277c478bd9Sstevel@tonic-gate return 0; 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate int poollist(argc, argv) 3327c478bd9Sstevel@tonic-gate int argc; 3337c478bd9Sstevel@tonic-gate char *argv[]; 3347c478bd9Sstevel@tonic-gate { 3357c478bd9Sstevel@tonic-gate char *kernel, *core, *poolname; 3367c478bd9Sstevel@tonic-gate int c, role, type, live_kernel; 3377c478bd9Sstevel@tonic-gate ip_pool_stat_t *plstp, plstat; 338*7663b816Sml37995 iphtstat_t *htstp, htstat; 339*7663b816Sml37995 iphtable_t *hptr; 3407c478bd9Sstevel@tonic-gate iplookupop_t op; 3417c478bd9Sstevel@tonic-gate ip_pool_t *ptr; 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate core = NULL; 3447c478bd9Sstevel@tonic-gate kernel = NULL; 3457c478bd9Sstevel@tonic-gate live_kernel = 1; 3467c478bd9Sstevel@tonic-gate type = IPLT_ALL; 3477c478bd9Sstevel@tonic-gate poolname = NULL; 3487c478bd9Sstevel@tonic-gate role = IPL_LOGALL; 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "dm:M:N:o:t:v")) != -1) 3517c478bd9Sstevel@tonic-gate switch (c) 3527c478bd9Sstevel@tonic-gate { 3537c478bd9Sstevel@tonic-gate case 'd' : 3547c478bd9Sstevel@tonic-gate opts |= OPT_DEBUG; 3557c478bd9Sstevel@tonic-gate break; 3567c478bd9Sstevel@tonic-gate case 'm' : 3577c478bd9Sstevel@tonic-gate poolname = optarg; 3587c478bd9Sstevel@tonic-gate break; 3597c478bd9Sstevel@tonic-gate case 'M' : 3607c478bd9Sstevel@tonic-gate live_kernel = 0; 3617c478bd9Sstevel@tonic-gate core = optarg; 3627c478bd9Sstevel@tonic-gate break; 3637c478bd9Sstevel@tonic-gate case 'N' : 3647c478bd9Sstevel@tonic-gate live_kernel = 0; 3657c478bd9Sstevel@tonic-gate kernel = optarg; 3667c478bd9Sstevel@tonic-gate break; 3677c478bd9Sstevel@tonic-gate case 'o' : 3687c478bd9Sstevel@tonic-gate role = getrole(optarg); 3697c478bd9Sstevel@tonic-gate if (role == IPL_LOGNONE) { 3707c478bd9Sstevel@tonic-gate fprintf(stderr, "unknown role '%s'\n", optarg); 3717c478bd9Sstevel@tonic-gate return -1; 3727c478bd9Sstevel@tonic-gate } 3737c478bd9Sstevel@tonic-gate break; 3747c478bd9Sstevel@tonic-gate case 't' : 3757c478bd9Sstevel@tonic-gate type = gettype(optarg, NULL); 3767c478bd9Sstevel@tonic-gate if (type == IPLT_NONE) { 3777c478bd9Sstevel@tonic-gate fprintf(stderr, "unknown type '%s'\n", optarg); 3787c478bd9Sstevel@tonic-gate return -1; 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate break; 3817c478bd9Sstevel@tonic-gate case 'v' : 3827c478bd9Sstevel@tonic-gate opts |= OPT_VERBOSE; 3837c478bd9Sstevel@tonic-gate break; 3847c478bd9Sstevel@tonic-gate } 3857c478bd9Sstevel@tonic-gate 386*7663b816Sml37995 if (opts & OPT_DEBUG) 387*7663b816Sml37995 fprintf(stderr, "poollist: opts = %#x\n", opts); 388*7663b816Sml37995 3897c478bd9Sstevel@tonic-gate if (!(opts & OPT_DONOTHING) && (fd == -1)) { 3907c478bd9Sstevel@tonic-gate fd = open(IPLOOKUP_NAME, O_RDWR); 3917c478bd9Sstevel@tonic-gate if (fd == -1) { 3927c478bd9Sstevel@tonic-gate perror("open(IPLOOKUP_NAME)"); 3937c478bd9Sstevel@tonic-gate exit(1); 3947c478bd9Sstevel@tonic-gate } 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate bzero((char *)&op, sizeof(op)); 3987c478bd9Sstevel@tonic-gate if (poolname != NULL) { 3997c478bd9Sstevel@tonic-gate strncpy(op.iplo_name, poolname, sizeof(op.iplo_name)); 4007c478bd9Sstevel@tonic-gate op.iplo_name[sizeof(op.iplo_name) - 1] = '\0'; 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate op.iplo_unit = role; 403*7663b816Sml37995 404*7663b816Sml37995 if (openkmem(kernel, core) == -1) 405*7663b816Sml37995 exit(-1); 406*7663b816Sml37995 407*7663b816Sml37995 if (type == IPLT_ALL || type == IPLT_POOL) { 408*7663b816Sml37995 plstp = &plstat; 409*7663b816Sml37995 op.iplo_type = IPLT_POOL; 4107c478bd9Sstevel@tonic-gate op.iplo_size = sizeof(plstat); 4117c478bd9Sstevel@tonic-gate op.iplo_struct = &plstat; 4127c478bd9Sstevel@tonic-gate c = ioctl(fd, SIOCLOOKUPSTAT, &op); 4137c478bd9Sstevel@tonic-gate if (c == -1) { 4147c478bd9Sstevel@tonic-gate perror("ioctl(SIOCLOOKUPSTAT)"); 4157c478bd9Sstevel@tonic-gate return -1; 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate if (role != IPL_LOGALL) { 4197c478bd9Sstevel@tonic-gate ptr = plstp->ipls_list[role]; 4207c478bd9Sstevel@tonic-gate while (ptr != NULL) { 4217c478bd9Sstevel@tonic-gate ptr = printpool(ptr, kmemcpywrap, opts); 4227c478bd9Sstevel@tonic-gate } 4237c478bd9Sstevel@tonic-gate } else { 4247c478bd9Sstevel@tonic-gate for (role = 0; role <= IPL_LOGMAX; role++) { 4257c478bd9Sstevel@tonic-gate ptr = plstp->ipls_list[role]; 4267c478bd9Sstevel@tonic-gate while (ptr != NULL) { 427*7663b816Sml37995 ptr = printpool(ptr, kmemcpywrap, 428*7663b816Sml37995 opts); 429*7663b816Sml37995 } 430*7663b816Sml37995 } 431*7663b816Sml37995 role = IPL_LOGALL; 432*7663b816Sml37995 } 433*7663b816Sml37995 } 434*7663b816Sml37995 if (type == IPLT_ALL || type == IPLT_HASH) { 435*7663b816Sml37995 htstp = &htstat; 436*7663b816Sml37995 op.iplo_type = IPLT_HASH; 437*7663b816Sml37995 op.iplo_size = sizeof(htstat); 438*7663b816Sml37995 op.iplo_struct = &htstat; 439*7663b816Sml37995 c = ioctl(fd, SIOCLOOKUPSTAT, &op); 440*7663b816Sml37995 if (c == -1) { 441*7663b816Sml37995 perror("ioctl(SIOCLOOKUPSTAT)"); 442*7663b816Sml37995 return -1; 443*7663b816Sml37995 } 444*7663b816Sml37995 445*7663b816Sml37995 if (role != IPL_LOGALL) { 446*7663b816Sml37995 hptr = htstp->iphs_tables; 447*7663b816Sml37995 while (hptr != NULL) { 448*7663b816Sml37995 hptr = printhash(hptr, kmemcpywrap, opts); 449*7663b816Sml37995 } 450*7663b816Sml37995 } else { 451*7663b816Sml37995 for (role = 0; role <= IPL_LOGMAX; role++) { 452*7663b816Sml37995 hptr = htstp->iphs_tables; 453*7663b816Sml37995 while (hptr != NULL) { 454*7663b816Sml37995 hptr = printhash(hptr, kmemcpywrap, 455*7663b816Sml37995 opts); 456*7663b816Sml37995 } 457*7663b816Sml37995 458*7663b816Sml37995 op.iplo_unit = role; 459*7663b816Sml37995 c = ioctl(fd, SIOCLOOKUPSTAT, &op); 460*7663b816Sml37995 if (c == -1) { 461*7663b816Sml37995 perror("ioctl(SIOCLOOKUPSTAT)"); 462*7663b816Sml37995 return -1; 463*7663b816Sml37995 } 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate } 4677c478bd9Sstevel@tonic-gate return 0; 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate int poolstats(argc, argv) 4727c478bd9Sstevel@tonic-gate int argc; 4737c478bd9Sstevel@tonic-gate char *argv[]; 4747c478bd9Sstevel@tonic-gate { 4757c478bd9Sstevel@tonic-gate int c, type, role, live_kernel; 4767c478bd9Sstevel@tonic-gate ip_pool_stat_t plstat; 4777c478bd9Sstevel@tonic-gate char *kernel, *core; 478*7663b816Sml37995 iphtstat_t htstat; 4797c478bd9Sstevel@tonic-gate iplookupop_t op; 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate core = NULL; 4827c478bd9Sstevel@tonic-gate kernel = NULL; 4837c478bd9Sstevel@tonic-gate live_kernel = 1; 4847c478bd9Sstevel@tonic-gate type = IPLT_ALL; 4857c478bd9Sstevel@tonic-gate role = IPL_LOGALL; 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate bzero((char *)&op, sizeof(op)); 4887c478bd9Sstevel@tonic-gate 4897c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "dM:N:o:t:v")) != -1) 4907c478bd9Sstevel@tonic-gate switch (c) 4917c478bd9Sstevel@tonic-gate { 4927c478bd9Sstevel@tonic-gate case 'd' : 4937c478bd9Sstevel@tonic-gate opts |= OPT_DEBUG; 4947c478bd9Sstevel@tonic-gate break; 4957c478bd9Sstevel@tonic-gate case 'M' : 4967c478bd9Sstevel@tonic-gate live_kernel = 0; 4977c478bd9Sstevel@tonic-gate core = optarg; 4987c478bd9Sstevel@tonic-gate break; 4997c478bd9Sstevel@tonic-gate case 'N' : 5007c478bd9Sstevel@tonic-gate live_kernel = 0; 5017c478bd9Sstevel@tonic-gate kernel = optarg; 5027c478bd9Sstevel@tonic-gate break; 5037c478bd9Sstevel@tonic-gate case 'o' : 5047c478bd9Sstevel@tonic-gate role = getrole(optarg); 5057c478bd9Sstevel@tonic-gate if (role == IPL_LOGNONE) { 5067c478bd9Sstevel@tonic-gate fprintf(stderr, "unknown role '%s'\n", optarg); 5077c478bd9Sstevel@tonic-gate return -1; 5087c478bd9Sstevel@tonic-gate } 5097c478bd9Sstevel@tonic-gate break; 5107c478bd9Sstevel@tonic-gate case 't' : 5117c478bd9Sstevel@tonic-gate type = gettype(optarg, NULL); 5127c478bd9Sstevel@tonic-gate if (type != IPLT_POOL) { 5137c478bd9Sstevel@tonic-gate fprintf(stderr, 5147c478bd9Sstevel@tonic-gate "-s not supported for this type yet\n"); 5157c478bd9Sstevel@tonic-gate return -1; 5167c478bd9Sstevel@tonic-gate } 5177c478bd9Sstevel@tonic-gate break; 5187c478bd9Sstevel@tonic-gate case 'v' : 5197c478bd9Sstevel@tonic-gate opts |= OPT_VERBOSE; 5207c478bd9Sstevel@tonic-gate break; 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate 523*7663b816Sml37995 if (opts & OPT_DEBUG) 524*7663b816Sml37995 fprintf(stderr, "poolstats: opts = %#x\n", opts); 525*7663b816Sml37995 5267c478bd9Sstevel@tonic-gate if (!(opts & OPT_DONOTHING) && (fd == -1)) { 5277c478bd9Sstevel@tonic-gate fd = open(IPLOOKUP_NAME, O_RDWR); 5287c478bd9Sstevel@tonic-gate if (fd == -1) { 5297c478bd9Sstevel@tonic-gate perror("open(IPLOOKUP_NAME)"); 5307c478bd9Sstevel@tonic-gate exit(1); 5317c478bd9Sstevel@tonic-gate } 5327c478bd9Sstevel@tonic-gate } 5337c478bd9Sstevel@tonic-gate 534*7663b816Sml37995 if (type == IPLT_ALL || type == IPLT_POOL) { 535*7663b816Sml37995 op.iplo_type = IPLT_POOL; 536*7663b816Sml37995 op.iplo_struct = &plstat; 537*7663b816Sml37995 op.iplo_size = sizeof(plstat); 5387c478bd9Sstevel@tonic-gate if (!(opts & OPT_DONOTHING)) { 5397c478bd9Sstevel@tonic-gate c = ioctl(fd, SIOCLOOKUPSTAT, &op); 5407c478bd9Sstevel@tonic-gate if (c == -1) { 5417c478bd9Sstevel@tonic-gate perror("ioctl(SIOCLOOKUPSTAT)"); 5427c478bd9Sstevel@tonic-gate return -1; 5437c478bd9Sstevel@tonic-gate } 5447c478bd9Sstevel@tonic-gate printf("Pools:\t%lu\n", plstat.ipls_pools); 5457c478bd9Sstevel@tonic-gate printf("Nodes:\t%lu\n", plstat.ipls_nodes); 5467c478bd9Sstevel@tonic-gate } 547*7663b816Sml37995 } 548*7663b816Sml37995 549*7663b816Sml37995 if (type == IPLT_ALL || type == IPLT_HASH) { 550*7663b816Sml37995 op.iplo_type = IPLT_HASH; 551*7663b816Sml37995 op.iplo_struct = &htstat; 552*7663b816Sml37995 op.iplo_size = sizeof(htstat); 553*7663b816Sml37995 if (!(opts & OPT_DONOTHING)) { 554*7663b816Sml37995 c = ioctl(fd, SIOCLOOKUPSTAT, &op); 555*7663b816Sml37995 if (c == -1) { 556*7663b816Sml37995 perror("ioctl(SIOCLOOKUPSTAT)"); 557*7663b816Sml37995 return -1; 558*7663b816Sml37995 } 559*7663b816Sml37995 printf("Hash Tables:\t%lu\n", htstat.iphs_numtables); 560*7663b816Sml37995 printf("Nodes:\t%lu\n", htstat.iphs_numnodes); 561*7663b816Sml37995 printf("Out of Memory:\t%lu\n", htstat.iphs_nomem); 562*7663b816Sml37995 } 563*7663b816Sml37995 } 5647c478bd9Sstevel@tonic-gate return 0; 5657c478bd9Sstevel@tonic-gate } 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate int poolflush(argc, argv) 5697c478bd9Sstevel@tonic-gate int argc; 5707c478bd9Sstevel@tonic-gate char *argv[]; 5717c478bd9Sstevel@tonic-gate { 5727c478bd9Sstevel@tonic-gate int c, role, type, arg; 5737c478bd9Sstevel@tonic-gate iplookupflush_t flush; 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate arg = IPLT_ALL; 5767c478bd9Sstevel@tonic-gate type = IPLT_ALL; 5777c478bd9Sstevel@tonic-gate role = IPL_LOGALL; 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "do:t:v")) != -1) 5807c478bd9Sstevel@tonic-gate switch (c) 5817c478bd9Sstevel@tonic-gate { 5827c478bd9Sstevel@tonic-gate case 'd' : 5837c478bd9Sstevel@tonic-gate opts |= OPT_DEBUG; 5847c478bd9Sstevel@tonic-gate break; 5857c478bd9Sstevel@tonic-gate case 'o' : 5867c478bd9Sstevel@tonic-gate role = getrole(optarg); 5877c478bd9Sstevel@tonic-gate if (role == IPL_LOGNONE) { 5887c478bd9Sstevel@tonic-gate fprintf(stderr, "unknown role '%s'\n", optarg); 5897c478bd9Sstevel@tonic-gate return -1; 5907c478bd9Sstevel@tonic-gate } 5917c478bd9Sstevel@tonic-gate break; 5927c478bd9Sstevel@tonic-gate case 't' : 5937c478bd9Sstevel@tonic-gate type = gettype(optarg, NULL); 5947c478bd9Sstevel@tonic-gate if (type == IPLT_NONE) { 5957c478bd9Sstevel@tonic-gate fprintf(stderr, "unknown type '%s'\n", optarg); 5967c478bd9Sstevel@tonic-gate return -1; 5977c478bd9Sstevel@tonic-gate } 5987c478bd9Sstevel@tonic-gate break; 5997c478bd9Sstevel@tonic-gate case 'v' : 6007c478bd9Sstevel@tonic-gate opts |= OPT_VERBOSE; 6017c478bd9Sstevel@tonic-gate break; 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate if (!(opts & OPT_DONOTHING) && (fd == -1)) { 6057c478bd9Sstevel@tonic-gate fd = open(IPLOOKUP_NAME, O_RDWR); 6067c478bd9Sstevel@tonic-gate if (fd == -1) { 6077c478bd9Sstevel@tonic-gate perror("open(IPLOOKUP_NAME)"); 6087c478bd9Sstevel@tonic-gate exit(1); 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate bzero((char *)&flush, sizeof(flush)); 6137c478bd9Sstevel@tonic-gate flush.iplf_type = type; 6147c478bd9Sstevel@tonic-gate flush.iplf_unit = role; 6157c478bd9Sstevel@tonic-gate flush.iplf_arg = arg; 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate if (!(opts & OPT_DONOTHING)) { 6187c478bd9Sstevel@tonic-gate if (ioctl(fd, SIOCLOOKUPFLUSH, &flush) == -1) { 6197c478bd9Sstevel@tonic-gate perror("ioctl(SIOCLOOKUPFLUSH)"); 6207c478bd9Sstevel@tonic-gate exit(1); 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate } 6247c478bd9Sstevel@tonic-gate printf("%u object%s flushed\n", flush.iplf_count, 6257c478bd9Sstevel@tonic-gate (flush.iplf_count == 1) ? "" : "s"); 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate return 0; 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate int getrole(rolename) 6327c478bd9Sstevel@tonic-gate char *rolename; 6337c478bd9Sstevel@tonic-gate { 6347c478bd9Sstevel@tonic-gate int role; 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate if (!strcasecmp(rolename, "ipf")) { 6377c478bd9Sstevel@tonic-gate role = IPL_LOGIPF; 6387c478bd9Sstevel@tonic-gate #if 0 6397c478bd9Sstevel@tonic-gate } else if (!strcasecmp(rolename, "nat")) { 6407c478bd9Sstevel@tonic-gate role = IPL_LOGNAT; 6417c478bd9Sstevel@tonic-gate } else if (!strcasecmp(rolename, "state")) { 6427c478bd9Sstevel@tonic-gate role = IPL_LOGSTATE; 6437c478bd9Sstevel@tonic-gate } else if (!strcasecmp(rolename, "auth")) { 6447c478bd9Sstevel@tonic-gate role = IPL_LOGAUTH; 6457c478bd9Sstevel@tonic-gate } else if (!strcasecmp(rolename, "sync")) { 6467c478bd9Sstevel@tonic-gate role = IPL_LOGSYNC; 6477c478bd9Sstevel@tonic-gate } else if (!strcasecmp(rolename, "scan")) { 6487c478bd9Sstevel@tonic-gate role = IPL_LOGSCAN; 6497c478bd9Sstevel@tonic-gate } else if (!strcasecmp(rolename, "pool")) { 6507c478bd9Sstevel@tonic-gate role = IPL_LOGLOOKUP; 6517c478bd9Sstevel@tonic-gate } else if (!strcasecmp(rolename, "count")) { 6527c478bd9Sstevel@tonic-gate role = IPL_LOGCOUNT; 6537c478bd9Sstevel@tonic-gate #endif 6547c478bd9Sstevel@tonic-gate } else { 6557c478bd9Sstevel@tonic-gate role = IPL_LOGNONE; 6567c478bd9Sstevel@tonic-gate } 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate return role; 6597c478bd9Sstevel@tonic-gate } 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate int gettype(typename, minor) 6637c478bd9Sstevel@tonic-gate char *typename; 6647c478bd9Sstevel@tonic-gate u_int *minor; 6657c478bd9Sstevel@tonic-gate { 6667c478bd9Sstevel@tonic-gate int type; 6677c478bd9Sstevel@tonic-gate 6687c478bd9Sstevel@tonic-gate if (!strcasecmp(optarg, "pool")) { 6697c478bd9Sstevel@tonic-gate type = IPLT_POOL; 6707c478bd9Sstevel@tonic-gate } else if (!strcasecmp(optarg, "hash")) { 6717c478bd9Sstevel@tonic-gate type = IPLT_HASH; 6727c478bd9Sstevel@tonic-gate if (minor != NULL) 6737c478bd9Sstevel@tonic-gate *minor = IPHASH_LOOKUP; 6747c478bd9Sstevel@tonic-gate } else if (!strcasecmp(optarg, "group-map")) { 6757c478bd9Sstevel@tonic-gate type = IPLT_HASH; 6767c478bd9Sstevel@tonic-gate if (minor != NULL) 6777c478bd9Sstevel@tonic-gate *minor = IPHASH_GROUPMAP; 6787c478bd9Sstevel@tonic-gate } else { 6797c478bd9Sstevel@tonic-gate type = IPLT_NONE; 6807c478bd9Sstevel@tonic-gate } 6817c478bd9Sstevel@tonic-gate return type; 6827c478bd9Sstevel@tonic-gate } 683