1 /*-
2 * Copyright (c) 1999 Poul-Henning Kamp.
3 * Copyright (c) 2008 Bjoern A. Zeeb.
4 * Copyright (c) 2009 James Gritton.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "opt_ddb.h"
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/errno.h>
38 #include <sys/sysproto.h>
39 #include <sys/malloc.h>
40 #include <sys/osd.h>
41 #include <sys/priv.h>
42 #include <sys/proc.h>
43 #include <sys/taskqueue.h>
44 #include <sys/fcntl.h>
45 #include <sys/jail.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/racct.h>
49 #include <sys/refcount.h>
50 #include <sys/sx.h>
51 #include <sys/namei.h>
52 #include <sys/mount.h>
53 #include <sys/queue.h>
54 #include <sys/socket.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysctl.h>
57 #include <sys/vnode.h>
58
59 #include <net/if.h>
60 #include <net/vnet.h>
61
62 #include <netinet/in.h>
63
64 static void
prison_bcopy_primary_ip6(const struct prison * pr,struct in6_addr * ia6)65 prison_bcopy_primary_ip6(const struct prison *pr, struct in6_addr *ia6)
66 {
67
68 bcopy(prison_ip_get0(pr, PR_INET6), ia6, sizeof(struct in6_addr));
69 }
70
71 int
prison_qcmp_v6(const void * ip1,const void * ip2)72 prison_qcmp_v6(const void *ip1, const void *ip2)
73 {
74 const struct in6_addr *ia6a, *ia6b;
75 int i, rc;
76
77 ia6a = (const struct in6_addr *)ip1;
78 ia6b = (const struct in6_addr *)ip2;
79
80 rc = 0;
81 for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
82 if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
83 rc = 1;
84 else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
85 rc = -1;
86 }
87 return (rc);
88 }
89
90 bool
prison_valid_v6(const void * ip)91 prison_valid_v6(const void *ip)
92 {
93 const struct in6_addr *ia = ip;
94
95 return (!IN6_IS_ADDR_UNSPECIFIED(ia));
96 }
97
98 /*
99 * Pass back primary IPv6 address for this jail.
100 *
101 * If not restricted return success but do not alter the address. Caller has
102 * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
103 *
104 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
105 */
106 int
prison_get_ip6(struct ucred * cred,struct in6_addr * ia6)107 prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
108 {
109 struct prison *pr;
110
111 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
112 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
113
114 pr = cred->cr_prison;
115 if (!(pr->pr_flags & PR_IP6))
116 return (0);
117 mtx_lock(&pr->pr_mtx);
118 if (!(pr->pr_flags & PR_IP6)) {
119 mtx_unlock(&pr->pr_mtx);
120 return (0);
121 }
122 if (pr->pr_addrs[PR_INET6] == NULL) {
123 mtx_unlock(&pr->pr_mtx);
124 return (EAFNOSUPPORT);
125 }
126
127 prison_bcopy_primary_ip6(pr, ia6);
128 mtx_unlock(&pr->pr_mtx);
129 return (0);
130 }
131
132 /*
133 * Return true if we should do proper source address selection or are not jailed.
134 * We will return false if we should bypass source address selection in favour
135 * of the primary jail IPv6 address. Only in this case *ia will be updated and
136 * returned in NBO.
137 * Return true, even in case this jail does not allow IPv6.
138 */
139 bool
prison_saddrsel_ip6(struct ucred * cred,struct in6_addr * ia6)140 prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
141 {
142 struct prison *pr;
143 struct in6_addr lia6;
144
145 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
146 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
147
148 if (!jailed(cred))
149 return (true);
150
151 pr = cred->cr_prison;
152 if (pr->pr_flags & PR_IP6_SADDRSEL)
153 return (true);
154
155 lia6 = in6addr_any;
156 if (prison_get_ip6(cred, &lia6) != 0)
157 return (true);
158 if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
159 return (true);
160
161 bcopy(&lia6, ia6, sizeof(struct in6_addr));
162 return (false);
163 }
164
165 /*
166 * Return true if pr1 and pr2 have the same IPv6 address restrictions.
167 */
168 bool
prison_equal_ip6(struct prison * pr1,struct prison * pr2)169 prison_equal_ip6(struct prison *pr1, struct prison *pr2)
170 {
171
172 if (pr1 == pr2)
173 return (true);
174
175 while (pr1 != &prison0 &&
176 #ifdef VIMAGE
177 !(pr1->pr_flags & PR_VNET) &&
178 #endif
179 !(pr1->pr_flags & PR_IP6_USER))
180 pr1 = pr1->pr_parent;
181 while (pr2 != &prison0 &&
182 #ifdef VIMAGE
183 !(pr2->pr_flags & PR_VNET) &&
184 #endif
185 !(pr2->pr_flags & PR_IP6_USER))
186 pr2 = pr2->pr_parent;
187 return (pr1 == pr2);
188 }
189
190 /*
191 * Make sure our (source) address is set to something meaningful to this jail.
192 *
193 * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
194 * when needed while binding.
195 *
196 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
197 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
198 * doesn't allow IPv6.
199 */
200 int
prison_local_ip6(struct ucred * cred,struct in6_addr * ia6,int v6only)201 prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
202 {
203 struct prison *pr;
204 int error;
205
206 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
207 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
208
209 pr = cred->cr_prison;
210 if (!(pr->pr_flags & PR_IP6))
211 return (0);
212 mtx_lock(&pr->pr_mtx);
213 if (!(pr->pr_flags & PR_IP6)) {
214 mtx_unlock(&pr->pr_mtx);
215 return (0);
216 }
217 if (pr->pr_addrs[PR_INET6] == NULL) {
218 mtx_unlock(&pr->pr_mtx);
219 return (EAFNOSUPPORT);
220 }
221
222 if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
223 /*
224 * In case there is only 1 IPv6 address, and v6only is true,
225 * then bind directly.
226 */
227 if (v6only != 0 && prison_ip_cnt(pr, PR_INET6) == 1)
228 prison_bcopy_primary_ip6(pr, ia6);
229 mtx_unlock(&pr->pr_mtx);
230 return (0);
231 }
232
233 error = prison_check_ip6_locked(pr, ia6);
234 if (error == EADDRNOTAVAIL && IN6_IS_ADDR_LOOPBACK(ia6)) {
235 prison_bcopy_primary_ip6(pr, ia6);
236 error = 0;
237 }
238
239 mtx_unlock(&pr->pr_mtx);
240 return (error);
241 }
242
243 /*
244 * Rewrite destination address in case we will connect to loopback address.
245 *
246 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
247 */
248 int
prison_remote_ip6(struct ucred * cred,struct in6_addr * ia6)249 prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
250 {
251 struct prison *pr;
252
253 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
254 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
255
256 pr = cred->cr_prison;
257 if (!(pr->pr_flags & PR_IP6))
258 return (0);
259 mtx_lock(&pr->pr_mtx);
260 if (!(pr->pr_flags & PR_IP6)) {
261 mtx_unlock(&pr->pr_mtx);
262 return (0);
263 }
264 if (pr->pr_addrs[PR_INET6] == NULL) {
265 mtx_unlock(&pr->pr_mtx);
266 return (EAFNOSUPPORT);
267 }
268
269 if (IN6_IS_ADDR_LOOPBACK(ia6) &&
270 prison_check_ip6_locked(pr, ia6) == EADDRNOTAVAIL) {
271 prison_bcopy_primary_ip6(pr, ia6);
272 mtx_unlock(&pr->pr_mtx);
273 return (0);
274 }
275
276 /*
277 * Return success because nothing had to be changed.
278 */
279 mtx_unlock(&pr->pr_mtx);
280 return (0);
281 }
282
283 /*
284 * Check if given address belongs to the jail referenced by cred/prison.
285 *
286 * Returns 0 if address belongs to jail,
287 * EADDRNOTAVAIL if the address doesn't belong to the jail.
288 */
289 int
prison_check_ip6_locked(const struct prison * pr,const struct in6_addr * ia6)290 prison_check_ip6_locked(const struct prison *pr, const struct in6_addr *ia6)
291 {
292
293 if (!(pr->pr_flags & PR_IP6))
294 return (0);
295
296 return (prison_ip_check(pr, PR_INET6, ia6));
297 }
298
299 int
prison_check_ip6(const struct ucred * cred,const struct in6_addr * ia6)300 prison_check_ip6(const struct ucred *cred, const struct in6_addr *ia6)
301 {
302 struct prison *pr;
303 int error;
304
305 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
306 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
307
308 pr = cred->cr_prison;
309 if (!(pr->pr_flags & PR_IP6))
310 return (0);
311 mtx_lock(&pr->pr_mtx);
312 if (!(pr->pr_flags & PR_IP6)) {
313 mtx_unlock(&pr->pr_mtx);
314 return (0);
315 }
316 if (pr->pr_addrs[PR_INET6] == NULL) {
317 mtx_unlock(&pr->pr_mtx);
318 return (EAFNOSUPPORT);
319 }
320
321 error = prison_check_ip6_locked(pr, ia6);
322 mtx_unlock(&pr->pr_mtx);
323 return (error);
324 }
325