in6_jail.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #include <sys/cdefs.h>
  29. #include "opt_ddb.h"
  30. #include "opt_inet.h"
  31. #include "opt_inet6.h"
  32. #include <sys/param.h>
  33. #include <sys/types.h>
  34. #include <sys/kernel.h>
  35. #include <sys/systm.h>
  36. #include <sys/errno.h>
  37. #include <sys/sysproto.h>
  38. #include <sys/malloc.h>
  39. #include <sys/osd.h>
  40. #include <sys/priv.h>
  41. #include <sys/proc.h>
  42. #include <sys/taskqueue.h>
  43. #include <sys/fcntl.h>
  44. #include <sys/jail.h>
  45. #include <sys/lock.h>
  46. #include <sys/mutex.h>
  47. #include <sys/racct.h>
  48. #include <sys/refcount.h>
  49. #include <sys/sx.h>
  50. #include <sys/namei.h>
  51. #include <sys/mount.h>
  52. #include <sys/queue.h>
  53. #include <sys/socket.h>
  54. #include <sys/syscallsubr.h>
  55. #include <sys/sysctl.h>
  56. #include <sys/vnode.h>
  57. #include <net/if.h>
  58. #include <net/vnet.h>
  59. #include <netinet/in.h>
  60. static void
  61. prison_bcopy_primary_ip6(const struct prison *pr, struct in6_addr *ia6)
  62. {
  63. bcopy(prison_ip_get0(pr, PR_INET6), ia6, sizeof(struct in6_addr));
  64. }
  65. int
  66. prison_qcmp_v6(const void *ip1, const void *ip2)
  67. {
  68. const struct in6_addr *ia6a, *ia6b;
  69. int i, rc;
  70. ia6a = (const struct in6_addr *)ip1;
  71. ia6b = (const struct in6_addr *)ip2;
  72. rc = 0;
  73. for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
  74. if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
  75. rc = 1;
  76. else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
  77. rc = -1;
  78. }
  79. return (rc);
  80. }
  81. bool
  82. prison_valid_v6(const void *ip)
  83. {
  84. const struct in6_addr *ia = ip;
  85. return (!IN6_IS_ADDR_UNSPECIFIED(ia));
  86. }
  87. /*
  88. * Pass back primary IPv6 address for this jail.
  89. *
  90. * If not restricted return success but do not alter the address. Caller has
  91. * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
  92. *
  93. * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
  94. */
  95. int
  96. prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
  97. {
  98. struct prison *pr;
  99. KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
  100. KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
  101. pr = cred->cr_prison;
  102. if (!(pr->pr_flags & PR_IP6))
  103. return (0);
  104. mtx_lock(&pr->pr_mtx);
  105. if (!(pr->pr_flags & PR_IP6)) {
  106. mtx_unlock(&pr->pr_mtx);
  107. return (0);
  108. }
  109. if (pr->pr_addrs[PR_INET6] == NULL) {
  110. mtx_unlock(&pr->pr_mtx);
  111. return (EAFNOSUPPORT);
  112. }
  113. prison_bcopy_primary_ip6(pr, ia6);
  114. mtx_unlock(&pr->pr_mtx);
  115. return (0);
  116. }
  117. /*
  118. * Return true if we should do proper source address selection or are not jailed.
  119. * We will return false if we should bypass source address selection in favour
  120. * of the primary jail IPv6 address. Only in this case *ia will be updated and
  121. * returned in NBO.
  122. * Return true, even in case this jail does not allow IPv6.
  123. */
  124. bool
  125. prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
  126. {
  127. struct prison *pr;
  128. struct in6_addr lia6;
  129. KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
  130. KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
  131. if (!jailed(cred))
  132. return (true);
  133. pr = cred->cr_prison;
  134. if (pr->pr_flags & PR_IP6_SADDRSEL)
  135. return (true);
  136. lia6 = in6addr_any;
  137. if (prison_get_ip6(cred, &lia6) != 0)
  138. return (true);
  139. if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
  140. return (true);
  141. bcopy(&lia6, ia6, sizeof(struct in6_addr));
  142. return (false);
  143. }
  144. /*
  145. * Return true if pr1 and pr2 have the same IPv6 address restrictions.
  146. */
  147. bool
  148. prison_equal_ip6(struct prison *pr1, struct prison *pr2)
  149. {
  150. if (pr1 == pr2)
  151. return (true);
  152. while (pr1 != &prison0 &&
  153. #ifdef VIMAGE
  154. !(pr1->pr_flags & PR_VNET) &&
  155. #endif
  156. !(pr1->pr_flags & PR_IP6_USER))
  157. pr1 = pr1->pr_parent;
  158. while (pr2 != &prison0 &&
  159. #ifdef VIMAGE
  160. !(pr2->pr_flags & PR_VNET) &&
  161. #endif
  162. !(pr2->pr_flags & PR_IP6_USER))
  163. pr2 = pr2->pr_parent;
  164. return (pr1 == pr2);
  165. }
  166. /*
  167. * Make sure our (source) address is set to something meaningful to this jail.
  168. *
  169. * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
  170. * when needed while binding.
  171. *
  172. * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
  173. * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
  174. * doesn't allow IPv6.
  175. */
  176. int
  177. prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
  178. {
  179. struct prison *pr;
  180. int error;
  181. KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
  182. KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
  183. pr = cred->cr_prison;
  184. if (!(pr->pr_flags & PR_IP6))
  185. return (0);
  186. mtx_lock(&pr->pr_mtx);
  187. if (!(pr->pr_flags & PR_IP6)) {
  188. mtx_unlock(&pr->pr_mtx);
  189. return (0);
  190. }
  191. if (pr->pr_addrs[PR_INET6] == NULL) {
  192. mtx_unlock(&pr->pr_mtx);
  193. return (EAFNOSUPPORT);
  194. }
  195. if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
  196. /*
  197. * In case there is only 1 IPv6 address, and v6only is true,
  198. * then bind directly.
  199. */
  200. if (v6only != 0 && prison_ip_cnt(pr, PR_INET6) == 1)
  201. prison_bcopy_primary_ip6(pr, ia6);
  202. mtx_unlock(&pr->pr_mtx);
  203. return (0);
  204. }
  205. error = prison_check_ip6_locked(pr, ia6);
  206. if (error == EADDRNOTAVAIL && IN6_IS_ADDR_LOOPBACK(ia6)) {
  207. prison_bcopy_primary_ip6(pr, ia6);
  208. error = 0;
  209. }
  210. mtx_unlock(&pr->pr_mtx);
  211. return (error);
  212. }
  213. /*
  214. * Rewrite destination address in case we will connect to loopback address.
  215. *
  216. * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
  217. */
  218. int
  219. prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
  220. {
  221. struct prison *pr;
  222. KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
  223. KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
  224. pr = cred->cr_prison;
  225. if (!(pr->pr_flags & PR_IP6))
  226. return (0);
  227. mtx_lock(&pr->pr_mtx);
  228. if (!(pr->pr_flags & PR_IP6)) {
  229. mtx_unlock(&pr->pr_mtx);
  230. return (0);
  231. }
  232. if (pr->pr_addrs[PR_INET6] == NULL) {
  233. mtx_unlock(&pr->pr_mtx);
  234. return (EAFNOSUPPORT);
  235. }
  236. if (IN6_IS_ADDR_LOOPBACK(ia6) &&
  237. prison_check_ip6_locked(pr, ia6) == EADDRNOTAVAIL) {
  238. prison_bcopy_primary_ip6(pr, ia6);
  239. mtx_unlock(&pr->pr_mtx);
  240. return (0);
  241. }
  242. /*
  243. * Return success because nothing had to be changed.
  244. */
  245. mtx_unlock(&pr->pr_mtx);
  246. return (0);
  247. }
  248. /*
  249. * Check if given address belongs to the jail referenced by cred/prison.
  250. *
  251. * Returns 0 if address belongs to jail,
  252. * EADDRNOTAVAIL if the address doesn't belong to the jail.
  253. */
  254. int
  255. prison_check_ip6_locked(const struct prison *pr, const struct in6_addr *ia6)
  256. {
  257. if (!(pr->pr_flags & PR_IP6))
  258. return (0);
  259. return (prison_ip_check(pr, PR_INET6, ia6));
  260. }
  261. int
  262. prison_check_ip6(const struct ucred *cred, const struct in6_addr *ia6)
  263. {
  264. struct prison *pr;
  265. int error;
  266. KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
  267. KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
  268. pr = cred->cr_prison;
  269. if (!(pr->pr_flags & PR_IP6))
  270. return (0);
  271. mtx_lock(&pr->pr_mtx);
  272. if (!(pr->pr_flags & PR_IP6)) {
  273. mtx_unlock(&pr->pr_mtx);
  274. return (0);
  275. }
  276. if (pr->pr_addrs[PR_INET6] == NULL) {
  277. mtx_unlock(&pr->pr_mtx);
  278. return (EAFNOSUPPORT);
  279. }
  280. error = prison_check_ip6_locked(pr, ia6);
  281. mtx_unlock(&pr->pr_mtx);
  282. return (error);
  283. }