summaryrefslogtreecommitdiff
path: root/usr/src/cmd/ipf/tools/ipfzone.c
blob: d4e1bb84270baf58faa74aab35a07f62d0b9451e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright (c) 2014 Joyent, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * See the IPFILTER.LICENCE file for details on licensing.
 */


#include <errno.h>
#include <net/if.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <zone.h>

#include "netinet/ip_fil.h"
#include "ipfzone.h"

static ipfzoneobj_t	ipzo;
static boolean_t	do_setzone = 0;
static int		num_setzones = 0;

extern int	errno;
extern int	opterr;
extern int	optind;
extern char	*optarg;

/*
 * Get the zonename if it's the last argument and set the zonename
 * in ipfzo to it. This is used by ipf(1m) only - all of the other tools
 * specify the zone with the -z option, and therefore use getzoneopt() below.
 */
void
getzonearg(int argc, char *argv[], const char *optstr)
{
	int c;

	/*
	 * Don't warn about unknown options - let subsequent calls to
	 * getopt() handle this.
	 */
	opterr = 0;

	/*
	 * getopt is also used here to set optind so that we can
	 * determine if the last argument belongs to a flag or is
	 * actually a zonename.
	 */
	while ((c = getopt(argc, argv, optstr)) != -1) {
		if (c == 'G')
			ipzo.ipfz_gz = 1;
	}

	if (optind < argc)
		setzonename(argv[optind]);

	/*
	 * Reset optind and opterr so the next getopt call will go through all
	 * of argv again and warn about unknown options.
	 */
	optind = 1;
	opterr = 1;
}

/*
 * Get a -z option from argv and set the zonename in ipfzo accordingly
 */
void
getzoneopt(int argc, char *argv[], const char *optstr)
{
	int c;

	/*
	 * Don't warn about unknown options - let subsequent calls to
	 * getopt() handle this.
	 */
	opterr = 0;

	while ((c = getopt(argc, argv, optstr)) != -1) {
		if (c == 'G')
			setzonename_global(optarg);

		if (c == 'z')
			setzonename(optarg);
	}

	/*
	 * Reset optind and opterr so the next getopt call will go through all
	 * of argv again and warn about unknown options.
	 */
	optind = 1;
	opterr = 1;
}

/*
 * Set the zonename in ipfzo to the given string: this is the zone all further
 * ioctls will act on.
 */
void
setzonename(const char *zonename)
{
	memcpy(ipzo.ipfz_zonename, zonename, sizeof (ipzo.ipfz_zonename));
	do_setzone = B_TRUE;
	num_setzones++;
}

/*
 * Set the zonename in ipfo, and the gz flag. This indicates that we want all
 * further ioctls to act on the GZ-controlled stack for that zone.
 */
void
setzonename_global(const char *zonename)
{
	setzonename(zonename);
	ipzo.ipfz_gz = 1;
}

/*
 * Set the zone that all further ioctls will operate on. See the "GZ-controlled
 * and per-zone stacks" note at the top of ip_fil_solaris.c for further
 * explanation.
 */
int
setzone(int fd)
{
	if (!do_setzone)
		return (0);

	if (num_setzones > 1) {
		(void) fprintf(stderr,
		    "Only one of -G and -z may be set\n");
		return (-1);
	}

	if (ipzo.ipfz_gz == 1 &&
	    getzoneidbyname(ipzo.ipfz_zonename) == GLOBAL_ZONEID) {
		(void) fprintf(stderr,
		    "-G cannot be used with the global zone\n");
		return (-1);
	}

	if (ioctl(fd, SIOCIPFZONESET, &ipzo) == -1) {
		switch (errno) {
		case ENODEV:
			(void) fprintf(stderr,
			    "Could not find running zone: %s\n",
			    ipzo.ipfz_zonename);
			break;
		case EACCES:
			(void) fprintf(stderr,
			    "Permission denied setting zone: %s\n",
			    ipzo.ipfz_zonename);
			break;
		default:
			perror("Error setting zone");
		}
		return (-1);
	}

	return (0);
}