1*ab25eeb5Syz /*
2*ab25eeb5Syz  * Copyright (C) 1993-2005  by Darren Reed.
3*ab25eeb5Syz  * See the IPFILTER.LICENCE file for details on licencing.
4*ab25eeb5Syz  */
5*ab25eeb5Syz 
6*ab25eeb5Syz #include "ipf.h"
7*ab25eeb5Syz #include "netinet/ipl.h"
8*ab25eeb5Syz #include <sys/ioctl.h>
9*ab25eeb5Syz 
ipf_dotuning(fd,tuneargs,iocfn)10*ab25eeb5Syz void ipf_dotuning(fd, tuneargs, iocfn)
11*ab25eeb5Syz int fd;
12*ab25eeb5Syz char *tuneargs;
13*ab25eeb5Syz ioctlfunc_t iocfn;
14*ab25eeb5Syz {
15*ab25eeb5Syz 	ipfobj_t obj;
16*ab25eeb5Syz 	ipftune_t tu;
17*ab25eeb5Syz 	char *s, *t;
18*ab25eeb5Syz 
19*ab25eeb5Syz 	bzero((char *)&tu, sizeof(tu));
20*ab25eeb5Syz 	obj.ipfo_rev = IPFILTER_VERSION;
21*ab25eeb5Syz 	obj.ipfo_size = sizeof(tu);;
22*ab25eeb5Syz 	obj.ipfo_ptr = (void *)&tu;
23*ab25eeb5Syz 	obj.ipfo_type = IPFOBJ_TUNEABLE;
24*ab25eeb5Syz 
25*ab25eeb5Syz 	for (s = strtok(tuneargs, ","); s != NULL; s = strtok(NULL, ",")) {
26*ab25eeb5Syz 		if (!strcmp(s, "list")) {
27*ab25eeb5Syz 			while (1) {
28*ab25eeb5Syz 				if ((*iocfn)(fd, SIOCIPFGETNEXT, &obj) == -1) {
29*ab25eeb5Syz 					perror("ioctl(SIOCIPFGETNEXT)");
30*ab25eeb5Syz 					break;
31*ab25eeb5Syz 				}
32*ab25eeb5Syz 				if (tu.ipft_cookie == NULL)
33*ab25eeb5Syz 					break;
34*ab25eeb5Syz 
35*ab25eeb5Syz 				tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0';
36*ab25eeb5Syz 				printtunable(&tu);
37*ab25eeb5Syz 			}
38*ab25eeb5Syz 		} else if ((t = strchr(s, '=')) != NULL) {
39*ab25eeb5Syz 			tu.ipft_cookie = NULL;
40*ab25eeb5Syz 			*t++ = '\0';
41*ab25eeb5Syz 			strncpy(tu.ipft_name, s, sizeof(tu.ipft_name));
42*ab25eeb5Syz 			if (sscanf(t, "%lu", &tu.ipft_vlong) == 1) {
43*ab25eeb5Syz 				if ((*iocfn)(fd, SIOCIPFSET, &obj) == -1) {
44*ab25eeb5Syz 					perror("ioctl(SIOCIPFSET)");
45*ab25eeb5Syz 					return;
46*ab25eeb5Syz 				}
47*ab25eeb5Syz 			} else {
48*ab25eeb5Syz 				fprintf(stderr, "invalid value '%s'\n", s);
49*ab25eeb5Syz 				return;
50*ab25eeb5Syz 			}
51*ab25eeb5Syz 		} else {
52*ab25eeb5Syz 			tu.ipft_cookie = NULL;
53*ab25eeb5Syz 			strncpy(tu.ipft_name, s, sizeof(tu.ipft_name));
54*ab25eeb5Syz 			if ((*iocfn)(fd, SIOCIPFGET, &obj) == -1) {
55*ab25eeb5Syz 				perror("ioctl(SIOCIPFGET)");
56*ab25eeb5Syz 				return;
57*ab25eeb5Syz 			}
58*ab25eeb5Syz 			if (tu.ipft_cookie == NULL) {
59*ab25eeb5Syz 				fprintf(stderr, "Null cookie for %s\n", s);
60*ab25eeb5Syz 				return;
61*ab25eeb5Syz 			}
62*ab25eeb5Syz 
63*ab25eeb5Syz 			tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0';
64*ab25eeb5Syz 			printtunable(&tu);
65*ab25eeb5Syz 		}
66*ab25eeb5Syz 	}
67*ab25eeb5Syz }
68