portalgo.c revision 1.1 1 1.1 christos /* $NetBSD: portalgo.c,v 1.1 2012/06/25 15:28:39 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright 2011 Vlad Balan
5 1.1 christos *
6 1.1 christos * Written by Vlad Balan for the NetBSD Foundation.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos * 1. Redistributions of source code must retain the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer.
13 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer in the
15 1.1 christos * documentation and/or other materials provided with the distribution.
16 1.1 christos *
17 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 christos * SUCH DAMAGE.
28 1.1 christos *
29 1.1 christos */
30 1.1 christos
31 1.1 christos /*
32 1.1 christos * see:
33 1.1 christos * RFC 6056 Recommendations for Transport-Protocol Port Randomization
34 1.1 christos */
35 1.1 christos
36 1.1 christos #include <sys/cdefs.h>
37 1.1 christos __KERNEL_RCSID(0, "$NetBSD: portalgo.c,v 1.1 2012/06/25 15:28:39 christos Exp $");
38 1.1 christos
39 1.1 christos #include "opt_inet.h"
40 1.1 christos
41 1.1 christos #include <sys/param.h>
42 1.1 christos #include <sys/errno.h>
43 1.1 christos #include <sys/kauth.h>
44 1.1 christos #include <sys/uidinfo.h>
45 1.1 christos #include <sys/domain.h>
46 1.1 christos #include <sys/md5.h>
47 1.1 christos #include <sys/cprng.h>
48 1.1 christos
49 1.1 christos #include <net/if.h>
50 1.1 christos #include <net/route.h>
51 1.1 christos
52 1.1 christos #include <netinet/in.h>
53 1.1 christos #include <netinet/in_systm.h>
54 1.1 christos #include <netinet/ip.h>
55 1.1 christos #include <netinet/in_pcb.h>
56 1.1 christos #include <netinet/in_var.h>
57 1.1 christos #include <netinet/ip_var.h>
58 1.1 christos
59 1.1 christos #ifdef INET6
60 1.1 christos #include <netinet/ip6.h>
61 1.1 christos #include <netinet6/ip6_var.h>
62 1.1 christos #include <netinet6/in6_pcb.h>
63 1.1 christos #endif
64 1.1 christos
65 1.1 christos #include <netinet/tcp_vtw.h>
66 1.1 christos
67 1.1 christos #include "portalgo.h"
68 1.1 christos
69 1.1 christos #define NPROTO 2
70 1.1 christos #define PORTALGO_TCP 0
71 1.1 christos #define PORTALGO_UDP 1
72 1.1 christos
73 1.1 christos #define NAF 2
74 1.1 christos #define PORTALGO_IPV4 0
75 1.1 christos #define PORTALGO_IPV6 1
76 1.1 christos
77 1.1 christos #define NRANGES 2
78 1.1 christos #define PORTALGO_LOWPORT 0
79 1.1 christos #define PORTALGO_HIGHPORT 1
80 1.1 christos
81 1.1 christos #if PORTALGO_DEBUG
82 1.1 christos static bool portalgo_debug = true;
83 1.1 christos #define DPRINTF if (portalgo_debug) printf
84 1.1 christos #else
85 1.1 christos #define DPRINTF while (/*CONSTCOND*/0) printf
86 1.1 christos #endif
87 1.1 christos
88 1.1 christos #ifdef INET
89 1.1 christos static int inet4_portalgo = PORTALGO_BSD;
90 1.1 christos #endif
91 1.1 christos #ifdef INET6
92 1.1 christos static int inet6_portalgo = PORTALGO_BSD;
93 1.1 christos #endif
94 1.1 christos
95 1.1 christos typedef struct {
96 1.1 christos const char *name;
97 1.1 christos int (*func)(int, uint16_t *, struct inpcb_hdr *, kauth_cred_t);
98 1.1 christos } portalgo_algorithm_t;
99 1.1 christos
100 1.1 christos static int algo_bsd(int, uint16_t *, struct inpcb_hdr *, kauth_cred_t);
101 1.1 christos static int algo_random_start(int, uint16_t *, struct inpcb_hdr *, kauth_cred_t);
102 1.1 christos static int algo_random_pick(int, uint16_t *, struct inpcb_hdr *, kauth_cred_t);
103 1.1 christos static int algo_hash(int, uint16_t *, struct inpcb_hdr *, kauth_cred_t);
104 1.1 christos static int algo_doublehash(int, uint16_t *, struct inpcb_hdr *, kauth_cred_t);
105 1.1 christos static int algo_randinc(int, uint16_t *, struct inpcb_hdr *, kauth_cred_t);
106 1.1 christos
107 1.1 christos static const portalgo_algorithm_t algos[] = {
108 1.1 christos {
109 1.1 christos .name = "bsd",
110 1.1 christos .func = algo_bsd
111 1.1 christos },
112 1.1 christos {
113 1.1 christos .name = "random_start",
114 1.1 christos .func = algo_random_start
115 1.1 christos },
116 1.1 christos {
117 1.1 christos .name = "random_pick",
118 1.1 christos .func = algo_random_pick
119 1.1 christos },
120 1.1 christos {
121 1.1 christos .name = "hash",
122 1.1 christos .func = algo_hash
123 1.1 christos },
124 1.1 christos {
125 1.1 christos .name = "doublehash",
126 1.1 christos .func = algo_doublehash
127 1.1 christos },
128 1.1 christos {
129 1.1 christos .name = "randinc",
130 1.1 christos .func = algo_randinc
131 1.1 christos }
132 1.1 christos };
133 1.1 christos
134 1.1 christos #define NALGOS __arraycount(algos)
135 1.1 christos
136 1.1 christos static uint16_t portalgo_next_ephemeral[NPROTO][NAF][NRANGES][NALGOS];
137 1.1 christos
138 1.1 christos /*
139 1.1 christos * Access the pcb and copy the values of the last port and the ends of
140 1.1 christos * the port range.
141 1.1 christos */
142 1.1 christos static int
143 1.1 christos pcb_getports(struct inpcb_hdr *inp_hdr, uint16_t *lastport,
144 1.1 christos uint16_t *mymin, uint16_t *mymax, uint16_t **pnext_ephemeral, int algo)
145 1.1 christos {
146 1.1 christos struct inpcbtable * const table = inp_hdr->inph_table;
147 1.1 christos struct socket *so;
148 1.1 christos int portalgo_proto;
149 1.1 christos int portalgo_af;
150 1.1 christos int portalgo_range;
151 1.1 christos
152 1.1 christos so = inp_hdr->inph_socket;
153 1.1 christos switch (so->so_type) {
154 1.1 christos case SOCK_DGRAM: /* UDP or DCCP */
155 1.1 christos portalgo_proto = PORTALGO_UDP;
156 1.1 christos break;
157 1.1 christos case SOCK_STREAM: /* TCP or SCTP */
158 1.1 christos portalgo_proto = PORTALGO_TCP;
159 1.1 christos break;
160 1.1 christos default:
161 1.1 christos return EPFNOSUPPORT;
162 1.1 christos }
163 1.1 christos
164 1.1 christos switch (inp_hdr->inph_af) {
165 1.1 christos #ifdef INET
166 1.1 christos case AF_INET: {
167 1.1 christos struct inpcb *inp = (struct inpcb *)(void *)inp_hdr;
168 1.1 christos
169 1.1 christos portalgo_af = PORTALGO_IPV4;
170 1.1 christos if (inp->inp_flags & INP_LOWPORT) {
171 1.1 christos *mymin = lowportmin;
172 1.1 christos *mymax = lowportmax;
173 1.1 christos *lastport = table->inpt_lastlow;
174 1.1 christos portalgo_range = PORTALGO_LOWPORT;
175 1.1 christos } else {
176 1.1 christos *mymin = anonportmin;
177 1.1 christos *mymax = anonportmax;
178 1.1 christos *lastport = table->inpt_lastport;
179 1.1 christos portalgo_range = PORTALGO_HIGHPORT;
180 1.1 christos }
181 1.1 christos break;
182 1.1 christos }
183 1.1 christos #endif
184 1.1 christos #ifdef INET6
185 1.1 christos case AF_INET6: {
186 1.1 christos struct in6pcb *in6p = (struct in6pcb *)(void *)inp_hdr;
187 1.1 christos
188 1.1 christos portalgo_af = PORTALGO_IPV6;
189 1.1 christos if (in6p->in6p_flags & IN6P_LOWPORT) {
190 1.1 christos *mymin = ip6_lowportmin;
191 1.1 christos *mymax = ip6_lowportmax;
192 1.1 christos *lastport = table->inpt_lastlow;
193 1.1 christos portalgo_range = PORTALGO_LOWPORT;
194 1.1 christos } else {
195 1.1 christos *mymin = ip6_anonportmin;
196 1.1 christos *mymax = ip6_anonportmax;
197 1.1 christos *lastport = table->inpt_lastport;
198 1.1 christos portalgo_range = PORTALGO_HIGHPORT;
199 1.1 christos }
200 1.1 christos break;
201 1.1 christos }
202 1.1 christos #endif
203 1.1 christos default:
204 1.1 christos return EAFNOSUPPORT;
205 1.1 christos }
206 1.1 christos
207 1.1 christos if (*mymin > *mymax) { /* sanity check */
208 1.1 christos u_int16_t swp;
209 1.1 christos
210 1.1 christos swp = *mymin;
211 1.1 christos *mymin = *mymax;
212 1.1 christos *mymax = swp;
213 1.1 christos }
214 1.1 christos
215 1.1 christos DPRINTF("%s mymin:%d mymax:%d lastport:%d\n", __func__,
216 1.1 christos *mymin, *mymax, *lastport);
217 1.1 christos
218 1.1 christos *pnext_ephemeral = &portalgo_next_ephemeral[portalgo_proto]
219 1.1 christos [portalgo_af][portalgo_range][algo];
220 1.1 christos
221 1.1 christos DPRINTF("%s portalgo_proto:%d portalgo_af:%d portalgo_range:%d\n",
222 1.1 christos __func__, portalgo_proto, portalgo_af, portalgo_range);
223 1.1 christos return 0;
224 1.1 christos }
225 1.1 christos
226 1.1 christos /*
227 1.1 christos * Check whether the port picked by the port randomizer is available
228 1.1 christos * and whether KAUTH approves of our choice. This part of the code
229 1.1 christos * shamelessly copied from in_pcb.c.
230 1.1 christos */
231 1.1 christos static bool
232 1.1 christos check_suitable_port(uint16_t port, struct inpcb_hdr *inp_hdr, kauth_cred_t cred)
233 1.1 christos {
234 1.1 christos struct inpcbtable * const table = inp_hdr->inph_table;
235 1.1 christos #ifdef INET
236 1.1 christos vestigial_inpcb_t vestigial;
237 1.1 christos #endif
238 1.1 christos int error;
239 1.1 christos #ifdef INET6
240 1.1 christos struct socket *so;
241 1.1 christos int wild = 0;
242 1.1 christos #endif
243 1.1 christos
244 1.1 christos DPRINTF("%s called for argument %d\n", __func__, port);
245 1.1 christos
246 1.1 christos switch (inp_hdr->inph_af) {
247 1.1 christos #ifdef INET
248 1.1 christos case AF_INET: { /* IPv4 */
249 1.1 christos struct inpcb *inp = (struct inpcb *)(void *)inp_hdr;
250 1.1 christos struct inpcb *pcb;
251 1.1 christos struct sockaddr_in sin;
252 1.1 christos
253 1.1 christos sin.sin_addr = inp->inp_laddr;
254 1.1 christos pcb = in_pcblookup_port(table, sin.sin_addr, htons(port), 1,
255 1.1 christos &vestigial);
256 1.1 christos
257 1.1 christos DPRINTF("%s in_pcblookup_port returned %p and "
258 1.1 christos "vestigial.valid %d\n",
259 1.1 christos __func__, pcb, vestigial.valid);
260 1.1 christos
261 1.1 christos if ((!pcb) && (!vestigial.valid)) {
262 1.1 christos enum kauth_network_req req;
263 1.1 christos
264 1.1 christos /* We have a free port. Check with the secmodel. */
265 1.1 christos if (inp->inp_flags & INP_LOWPORT) {
266 1.1 christos #ifndef IPNOPRIVPORTS
267 1.1 christos req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
268 1.1 christos #else
269 1.1 christos req = KAUTH_REQ_NETWORK_BIND_PORT;
270 1.1 christos #endif
271 1.1 christos } else
272 1.1 christos req = KAUTH_REQ_NETWORK_BIND_PORT;
273 1.1 christos
274 1.1 christos sin.sin_port = port;
275 1.1 christos error = kauth_authorize_network(cred,
276 1.1 christos KAUTH_NETWORK_BIND,
277 1.1 christos req, inp->inp_socket, &sin, NULL);
278 1.1 christos DPRINTF("%s kauth_authorize_network returned %d\n",
279 1.1 christos __func__, error);
280 1.1 christos
281 1.1 christos if (error == 0) {
282 1.1 christos DPRINTF("%s port approved\n", __func__);
283 1.1 christos return true; /* KAUTH agrees */
284 1.1 christos }
285 1.1 christos }
286 1.1 christos break;
287 1.1 christos }
288 1.1 christos #endif
289 1.1 christos #ifdef INET6
290 1.1 christos case AF_INET6: { /* IPv6 */
291 1.1 christos struct in6pcb *in6p = (struct in6pcb *)(void *)inp_hdr;
292 1.1 christos struct sockaddr_in6 sin6;
293 1.1 christos void *t;
294 1.1 christos
295 1.1 christos sin6.sin6_addr = in6p->in6p_laddr;
296 1.1 christos so = in6p->in6p_socket;
297 1.1 christos
298 1.1 christos /* XXX: this is redundant when called from in6_pcbbind */
299 1.1 christos if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
300 1.1 christos ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
301 1.1 christos (so->so_options & SO_ACCEPTCONN) == 0))
302 1.1 christos wild = 1;
303 1.1 christos
304 1.1 christos #ifdef INET
305 1.1 christos if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
306 1.1 christos t = in_pcblookup_port(table,
307 1.1 christos *(struct in_addr *)&sin6.sin6_addr.s6_addr32[3],
308 1.1 christos htons(port), wild, &vestigial);
309 1.1 christos if (!t && vestigial.valid) {
310 1.1 christos DPRINTF("%s in_pcblookup_port returned "
311 1.1 christos "a result\n", __func__);
312 1.1 christos return false;
313 1.1 christos }
314 1.1 christos } else
315 1.1 christos #endif
316 1.1 christos {
317 1.1 christos t = in6_pcblookup_port(table, &sin6.sin6_addr,
318 1.1 christos htons(port), wild, &vestigial);
319 1.1 christos if (!t && vestigial.valid) {
320 1.1 christos DPRINTF("%s in6_pcblookup_port returned "
321 1.1 christos "a result\n", __func__);
322 1.1 christos return false;
323 1.1 christos }
324 1.1 christos }
325 1.1 christos if (t == NULL) {
326 1.1 christos enum kauth_network_req req;
327 1.1 christos
328 1.1 christos /* We have a free port. Check with the secmodel. */
329 1.1 christos if (in6p->in6p_flags & IN6P_LOWPORT) {
330 1.1 christos #ifndef IPNOPRIVPORTS
331 1.1 christos req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
332 1.1 christos #else
333 1.1 christos req = KAUTH_REQ_NETWORK_BIND_PORT;
334 1.1 christos #endif
335 1.1 christos } else {
336 1.1 christos req = KAUTH_REQ_NETWORK_BIND_PORT;
337 1.1 christos }
338 1.1 christos
339 1.1 christos sin6.sin6_port = port;
340 1.1 christos error = kauth_authorize_network(cred,
341 1.1 christos KAUTH_NETWORK_BIND, req, so, &sin6, NULL);
342 1.1 christos if (error) {
343 1.1 christos /* Secmodel says no. Keep looking. */
344 1.1 christos DPRINTF("%s secmodel says no\n", __func__);
345 1.1 christos return false;
346 1.1 christos }
347 1.1 christos DPRINTF("%s port approved\n", __func__);
348 1.1 christos return true;
349 1.1 christos }
350 1.1 christos break;
351 1.1 christos }
352 1.1 christos #endif
353 1.1 christos default:
354 1.1 christos DPRINTF("%s unknown address family\n", __func__);
355 1.1 christos return false;
356 1.1 christos }
357 1.1 christos return false;
358 1.1 christos }
359 1.1 christos
360 1.1 christos /* This is the default BSD algorithm, as described in RFC 6056 */
361 1.1 christos static int
362 1.1 christos algo_bsd(int algo, uint16_t *port, struct inpcb_hdr *inp_hdr, kauth_cred_t cred)
363 1.1 christos {
364 1.1 christos uint16_t count;
365 1.1 christos uint16_t mymin, mymax, lastport;
366 1.1 christos uint16_t *next_ephemeral;
367 1.1 christos int error;
368 1.1 christos
369 1.1 christos DPRINTF("%s called\n", __func__);
370 1.1 christos error = pcb_getports(inp_hdr, &lastport, &mymin, &mymax,
371 1.1 christos &next_ephemeral, algo);
372 1.1 christos if (error)
373 1.1 christos return error;
374 1.1 christos count = mymax - mymin + 1;
375 1.1 christos do {
376 1.1 christos uint16_t myport = *next_ephemeral;
377 1.1 christos
378 1.1 christos if (myport < mymin || mymax < myport)
379 1.1 christos myport = mymax;
380 1.1 christos *next_ephemeral = myport - 1;
381 1.1 christos if (check_suitable_port(myport, inp_hdr, cred)) {
382 1.1 christos *port = myport;
383 1.1 christos DPRINTF("%s returning port %d\n", __func__, *port);
384 1.1 christos return 0;
385 1.1 christos }
386 1.1 christos count--;
387 1.1 christos } while (count > 0);
388 1.1 christos
389 1.1 christos DPRINTF("%s returning EAGAIN\n", __func__);
390 1.1 christos return EAGAIN;
391 1.1 christos }
392 1.1 christos
393 1.1 christos /*
394 1.1 christos * The straightforward algorithm that calls random() in order to
395 1.1 christos * compute the increment to the next port number.
396 1.1 christos */
397 1.1 christos static int
398 1.1 christos algo_random_start(int algo, uint16_t *port, struct inpcb_hdr *inp_hdr,
399 1.1 christos kauth_cred_t cred)
400 1.1 christos {
401 1.1 christos uint16_t count, num_ephemeral;
402 1.1 christos uint16_t mymin, mymax, lastport;
403 1.1 christos uint16_t *next_ephemeral;
404 1.1 christos int error;
405 1.1 christos
406 1.1 christos DPRINTF("%s called\n", __func__);
407 1.1 christos
408 1.1 christos error = pcb_getports(inp_hdr, &lastport, &mymin, &mymax,
409 1.1 christos &next_ephemeral, algo);
410 1.1 christos if (error)
411 1.1 christos return error;
412 1.1 christos
413 1.1 christos num_ephemeral = mymax - mymin + 1;
414 1.1 christos
415 1.1 christos DPRINTF("num_ephemeral: %u\n", num_ephemeral);
416 1.1 christos
417 1.1 christos *next_ephemeral = mymin + (cprng_fast32() % num_ephemeral);
418 1.1 christos
419 1.1 christos DPRINTF("next_ephemeral initially: %u\n", *next_ephemeral);
420 1.1 christos
421 1.1 christos count = num_ephemeral;
422 1.1 christos
423 1.1 christos do {
424 1.1 christos if (check_suitable_port(*next_ephemeral, inp_hdr, cred)) {
425 1.1 christos *port = *next_ephemeral;
426 1.1 christos DPRINTF("%s returning port %d\n", __func__, *port);
427 1.1 christos return 0;
428 1.1 christos }
429 1.1 christos if (*next_ephemeral == mymax) {
430 1.1 christos *next_ephemeral = mymin;
431 1.1 christos } else
432 1.1 christos (*next_ephemeral)++;
433 1.1 christos
434 1.1 christos count--;
435 1.1 christos
436 1.1 christos
437 1.1 christos DPRINTF("next_ephemeral: %u count: %u\n", *next_ephemeral,
438 1.1 christos count);
439 1.1 christos
440 1.1 christos } while (count > 0);
441 1.1 christos
442 1.1 christos DPRINTF("%s returning EINVAL\n", __func__);
443 1.1 christos
444 1.1 christos return EINVAL;
445 1.1 christos }
446 1.1 christos
447 1.1 christos /*
448 1.1 christos * Since there is no state kept on the ports tried, we might actually
449 1.1 christos * give up before exhausting the free ports.
450 1.1 christos */
451 1.1 christos static int
452 1.1 christos algo_random_pick(int algo, uint16_t *port, struct inpcb_hdr *inp_hdr,
453 1.1 christos kauth_cred_t cred)
454 1.1 christos {
455 1.1 christos uint16_t count, num_ephemeral;
456 1.1 christos uint16_t mymin, mymax, lastport;
457 1.1 christos uint16_t *next_ephemeral;
458 1.1 christos int error;
459 1.1 christos
460 1.1 christos DPRINTF("%s called\n", __func__);
461 1.1 christos
462 1.1 christos error = pcb_getports(inp_hdr, &lastport, &mymin, &mymax,
463 1.1 christos &next_ephemeral, algo);
464 1.1 christos if (error)
465 1.1 christos return error;
466 1.1 christos
467 1.1 christos num_ephemeral = mymax - mymin + 1;
468 1.1 christos
469 1.1 christos DPRINTF("num_ephemeral: %u\n", num_ephemeral);
470 1.1 christos *next_ephemeral = mymin + (cprng_fast32() % num_ephemeral);
471 1.1 christos
472 1.1 christos DPRINTF("next_ephemeral initially: %u\n", *next_ephemeral);
473 1.1 christos
474 1.1 christos count = num_ephemeral;
475 1.1 christos
476 1.1 christos do {
477 1.1 christos if (check_suitable_port(*next_ephemeral, inp_hdr, cred)) {
478 1.1 christos *port = *next_ephemeral;
479 1.1 christos DPRINTF("%s returning port %d\n", __func__, *port);
480 1.1 christos return 0;
481 1.1 christos }
482 1.1 christos *next_ephemeral = mymin +
483 1.1 christos (cprng_fast32() % num_ephemeral);
484 1.1 christos
485 1.1 christos count--;
486 1.1 christos
487 1.1 christos DPRINTF("next_ephemeral: %u count: %u\n",
488 1.1 christos *next_ephemeral, count);
489 1.1 christos } while (count > 0);
490 1.1 christos
491 1.1 christos DPRINTF("%s returning EINVAL\n", __func__);
492 1.1 christos
493 1.1 christos return EINVAL;
494 1.1 christos }
495 1.1 christos
496 1.1 christos /* This is the implementation from FreeBSD, with tweaks */
497 1.1 christos static uint16_t
498 1.1 christos Fhash(const struct inpcb_hdr *inp_hdr)
499 1.1 christos {
500 1.1 christos MD5_CTX f_ctx;
501 1.1 christos uint32_t Ff[4];
502 1.1 christos uint32_t secret_f[4];
503 1.1 christos uint32_t offset;
504 1.1 christos uint16_t soffset[2];
505 1.1 christos
506 1.1 christos cprng_fast(secret_f, sizeof(secret_f));
507 1.1 christos
508 1.1 christos MD5Init(&f_ctx);
509 1.1 christos switch (inp_hdr->inph_af) {
510 1.1 christos #ifdef INET
511 1.1 christos case AF_INET: {
512 1.1 christos const struct inpcb *inp =
513 1.1 christos (const struct inpcb *)(const void *)inp_hdr;
514 1.1 christos MD5Update(&f_ctx, (const u_char *)&inp->inp_laddr,
515 1.1 christos sizeof(inp->inp_laddr));
516 1.1 christos MD5Update(&f_ctx, (const u_char *)&inp->inp_faddr,
517 1.1 christos sizeof(inp->inp_faddr));
518 1.1 christos MD5Update(&f_ctx, (const u_char *)&inp->inp_fport,
519 1.1 christos sizeof(inp->inp_fport));
520 1.1 christos break;
521 1.1 christos }
522 1.1 christos #endif
523 1.1 christos #ifdef INET6
524 1.1 christos case AF_INET6: {
525 1.1 christos const struct in6pcb *in6p =
526 1.1 christos (const struct in6pcb *)(const void *)inp_hdr;
527 1.1 christos MD5Update(&f_ctx, (const u_char *)&in6p->in6p_laddr,
528 1.1 christos sizeof(in6p->in6p_laddr));
529 1.1 christos MD5Update(&f_ctx, (const u_char *)&in6p->in6p_faddr,
530 1.1 christos sizeof(in6p->in6p_faddr));
531 1.1 christos MD5Update(&f_ctx, (const u_char *)&in6p->in6p_fport,
532 1.1 christos sizeof(in6p->in6p_fport));
533 1.1 christos break;
534 1.1 christos }
535 1.1 christos #endif
536 1.1 christos default:
537 1.1 christos break;
538 1.1 christos }
539 1.1 christos MD5Update(&f_ctx, (const u_char *)secret_f, sizeof(secret_f));
540 1.1 christos MD5Final((u_char *)&Ff, &f_ctx);
541 1.1 christos
542 1.1 christos offset = (Ff[0] ^ Ff[1]) ^ (Ff[2] ^ Ff[3]);
543 1.1 christos
544 1.1 christos memcpy(&soffset, &offset, sizeof(soffset));
545 1.1 christos
546 1.1 christos return soffset[0] ^ soffset[1];
547 1.1 christos }
548 1.1 christos
549 1.1 christos /*
550 1.1 christos * Checks whether the tuple is complete. If not, marks the pcb for
551 1.1 christos * late binding.
552 1.1 christos */
553 1.1 christos static bool
554 1.1 christos iscompletetuple(struct inpcb_hdr *inp_hdr)
555 1.1 christos {
556 1.1 christos #ifdef INET6
557 1.1 christos struct in6pcb *in6p;
558 1.1 christos #endif
559 1.1 christos
560 1.1 christos switch (inp_hdr->inph_af) {
561 1.1 christos #ifdef INET
562 1.1 christos case AF_INET: {
563 1.1 christos struct inpcb *inp = (struct inpcb *)(void *)inp_hdr;
564 1.1 christos if (inp->inp_fport == 0 || in_nullhost(inp->inp_faddr)) {
565 1.1 christos DPRINTF("%s fport or faddr missing, delaying port "
566 1.1 christos "to connect/send\n", __func__);
567 1.1 christos inp->inp_bindportonsend = true;
568 1.1 christos return false;
569 1.1 christos } else {
570 1.1 christos inp->inp_bindportonsend = false;
571 1.1 christos }
572 1.1 christos break;
573 1.1 christos }
574 1.1 christos #endif
575 1.1 christos #ifdef INET6
576 1.1 christos case AF_INET6: {
577 1.1 christos in6p = (struct in6pcb *)(void *)inp_hdr;
578 1.1 christos if (in6p->in6p_fport == 0 || memcmp(&in6p->in6p_faddr,
579 1.1 christos &in6addr_any, sizeof(in6p->in6p_faddr)) == 0) {
580 1.1 christos DPRINTF("%s fport or faddr missing, delaying port "
581 1.1 christos "to connect/send\n", __func__);
582 1.1 christos in6p->in6p_bindportonsend = true;
583 1.1 christos return false;
584 1.1 christos } else {
585 1.1 christos in6p->in6p_bindportonsend = false;
586 1.1 christos }
587 1.1 christos break;
588 1.1 christos }
589 1.1 christos #endif
590 1.1 christos default:
591 1.1 christos DPRINTF("%s incorrect address family\n", __func__);
592 1.1 christos return false;
593 1.1 christos }
594 1.1 christos
595 1.1 christos return true;
596 1.1 christos }
597 1.1 christos
598 1.1 christos static int
599 1.1 christos algo_hash(int algo, uint16_t *port, struct inpcb_hdr *inp_hdr,
600 1.1 christos kauth_cred_t cred)
601 1.1 christos {
602 1.1 christos uint16_t count, num_ephemeral;
603 1.1 christos uint16_t mymin, mymax, lastport;
604 1.1 christos uint16_t *next_ephemeral;
605 1.1 christos uint16_t offset, myport;
606 1.1 christos int error;
607 1.1 christos
608 1.1 christos DPRINTF("%s called\n", __func__);
609 1.1 christos
610 1.1 christos error = pcb_getports(inp_hdr, &lastport, &mymin, &mymax,
611 1.1 christos &next_ephemeral, algo);
612 1.1 christos if (error)
613 1.1 christos return error;
614 1.1 christos
615 1.1 christos if (!iscompletetuple(inp_hdr)) {
616 1.1 christos *port = 0;
617 1.1 christos return 0;
618 1.1 christos }
619 1.1 christos
620 1.1 christos /* Ephemeral port selection function */
621 1.1 christos num_ephemeral = mymax - mymin + 1;
622 1.1 christos
623 1.1 christos DPRINTF("num_ephemeral: %d\n", num_ephemeral);
624 1.1 christos
625 1.1 christos offset = Fhash(inp_hdr);
626 1.1 christos
627 1.1 christos count = num_ephemeral;
628 1.1 christos do {
629 1.1 christos myport = mymin + (*next_ephemeral + offset)
630 1.1 christos % num_ephemeral;
631 1.1 christos
632 1.1 christos (*next_ephemeral)++;
633 1.1 christos
634 1.1 christos if (check_suitable_port(myport, inp_hdr, cred)) {
635 1.1 christos *port = myport;
636 1.1 christos DPRINTF("%s returning port %d\n", __func__, *port);
637 1.1 christos return 0;
638 1.1 christos }
639 1.1 christos count--;
640 1.1 christos } while (count > 0);
641 1.1 christos
642 1.1 christos DPRINTF("%s returning EINVAL\n", __func__);
643 1.1 christos
644 1.1 christos return EINVAL;
645 1.1 christos }
646 1.1 christos
647 1.1 christos static int
648 1.1 christos algo_doublehash(int algo, uint16_t *port, struct inpcb_hdr *inp_hdr,
649 1.1 christos kauth_cred_t cred)
650 1.1 christos {
651 1.1 christos uint16_t count, num_ephemeral;
652 1.1 christos uint16_t mymin, mymax, lastport;
653 1.1 christos uint16_t *next_ephemeral;
654 1.1 christos uint16_t offset, myport;
655 1.1 christos static uint16_t dhtable[8];
656 1.1 christos size_t idx;
657 1.1 christos int error;
658 1.1 christos
659 1.1 christos DPRINTF("%s called\n", __func__);
660 1.1 christos
661 1.1 christos error = pcb_getports(inp_hdr, &lastport, &mymin, &mymax,
662 1.1 christos &next_ephemeral, algo);
663 1.1 christos if (error)
664 1.1 christos return error;
665 1.1 christos
666 1.1 christos if (!iscompletetuple(inp_hdr)) {
667 1.1 christos *port = 0;
668 1.1 christos return 0;
669 1.1 christos }
670 1.1 christos /* first time initialization */
671 1.1 christos if (dhtable[0] == 0)
672 1.1 christos for (size_t i = 0; i < __arraycount(dhtable); i++)
673 1.1 christos dhtable[i] = random() & 0xffff;
674 1.1 christos
675 1.1 christos /* Ephemeral port selection function */
676 1.1 christos num_ephemeral = mymax - mymin + 1;
677 1.1 christos offset = Fhash(inp_hdr);
678 1.1 christos idx = Fhash(inp_hdr) % __arraycount(dhtable); /* G */
679 1.1 christos count = num_ephemeral;
680 1.1 christos
681 1.1 christos do {
682 1.1 christos myport = mymin + (offset + dhtable[idx])
683 1.1 christos % num_ephemeral;
684 1.1 christos dhtable[idx]++;
685 1.1 christos
686 1.1 christos if (check_suitable_port(myport, inp_hdr, cred)) {
687 1.1 christos *port = myport;
688 1.1 christos DPRINTF("%s returning port %d\n", __func__, *port);
689 1.1 christos return 0;
690 1.1 christos }
691 1.1 christos count--;
692 1.1 christos
693 1.1 christos } while (count > 0);
694 1.1 christos
695 1.1 christos DPRINTF("%s returning EINVAL\n", __func__);
696 1.1 christos
697 1.1 christos return EINVAL;
698 1.1 christos }
699 1.1 christos
700 1.1 christos static int
701 1.1 christos algo_randinc(int algo, uint16_t *port, struct inpcb_hdr *inp_hdr,
702 1.1 christos kauth_cred_t cred)
703 1.1 christos {
704 1.1 christos static const uint16_t N = 500; /* Determines the trade-off */
705 1.1 christos uint16_t count, num_ephemeral;
706 1.1 christos uint16_t mymin, mymax, lastport;
707 1.1 christos uint16_t *next_ephemeral;
708 1.1 christos uint16_t myport;
709 1.1 christos int error;
710 1.1 christos
711 1.1 christos DPRINTF("%s called\n", __func__);
712 1.1 christos
713 1.1 christos error = pcb_getports(inp_hdr, &lastport, &mymin, &mymax,
714 1.1 christos &next_ephemeral, algo);
715 1.1 christos if (error)
716 1.1 christos return error;
717 1.1 christos
718 1.1 christos if (*next_ephemeral == 0)
719 1.1 christos *next_ephemeral = cprng_fast32() & 0xffff;
720 1.1 christos
721 1.1 christos /* Ephemeral port selection function */
722 1.1 christos num_ephemeral = mymax - mymin + 1;
723 1.1 christos
724 1.1 christos count = num_ephemeral;
725 1.1 christos do {
726 1.1 christos *next_ephemeral = *next_ephemeral +
727 1.1 christos (cprng_fast32() % N) + 1;
728 1.1 christos myport = mymin +
729 1.1 christos (*next_ephemeral % num_ephemeral);
730 1.1 christos
731 1.1 christos if (check_suitable_port(myport, inp_hdr, cred)) {
732 1.1 christos *port = myport;
733 1.1 christos DPRINTF("%s returning port %d\n", __func__, *port);
734 1.1 christos return 0;
735 1.1 christos }
736 1.1 christos count--;
737 1.1 christos } while (count > 0);
738 1.1 christos
739 1.1 christos return EINVAL;
740 1.1 christos }
741 1.1 christos
742 1.1 christos /* The generic function called in order to pick a port. */
743 1.1 christos int
744 1.1 christos portalgo_randport(uint16_t *port, struct inpcb_hdr *inp_hdr, kauth_cred_t cred)
745 1.1 christos {
746 1.1 christos int algo, error;
747 1.1 christos uint16_t lport;
748 1.1 christos int default_algo;
749 1.1 christos
750 1.1 christos DPRINTF("%s called\n", __func__);
751 1.1 christos
752 1.1 christos if (inp_hdr->inph_portalgo == PORTALGO_DEFAULT) {
753 1.1 christos switch (inp_hdr->inph_af) {
754 1.1 christos #ifdef INET
755 1.1 christos case AF_INET:
756 1.1 christos default_algo = inet4_portalgo;
757 1.1 christos break;
758 1.1 christos #endif
759 1.1 christos #ifdef INET6
760 1.1 christos case AF_INET6:
761 1.1 christos default_algo = inet6_portalgo;
762 1.1 christos break;
763 1.1 christos #endif
764 1.1 christos default:
765 1.1 christos return EINVAL;
766 1.1 christos }
767 1.1 christos
768 1.1 christos if (default_algo == PORTALGO_DEFAULT)
769 1.1 christos algo = PORTALGO_BSD;
770 1.1 christos else
771 1.1 christos algo = default_algo;
772 1.1 christos }
773 1.1 christos else /* socket specifies the algorithm */
774 1.1 christos algo = inp_hdr->inph_portalgo;
775 1.1 christos
776 1.1 christos KASSERT(algo >= 0);
777 1.1 christos KASSERT(algo < NALGOS);
778 1.1 christos
779 1.1 christos switch (inp_hdr->inph_af) {
780 1.1 christos #ifdef INET
781 1.1 christos case AF_INET: {
782 1.1 christos struct inpcb *inp = (struct inpcb *)(void *)inp_hdr;
783 1.1 christos DPRINTF("local addr: %s\n", inet_ntoa(inp->inp_laddr));
784 1.1 christos DPRINTF("local port: %d\n", inp->inp_lport);
785 1.1 christos DPRINTF("foreign addr: %s\n", inet_ntoa(inp->inp_faddr));
786 1.1 christos DPRINTF("foreign port: %d\n", inp->inp_fport);
787 1.1 christos break;
788 1.1 christos }
789 1.1 christos #endif
790 1.1 christos #ifdef INET6
791 1.1 christos case AF_INET6: {
792 1.1 christos struct in6pcb *in6p = (struct in6pcb *)(void *)inp_hdr;
793 1.1 christos
794 1.1 christos DPRINTF("local addr: %s\n", ip6_sprintf(&in6p->in6p_laddr));
795 1.1 christos DPRINTF("local port: %d\n", in6p->in6p_lport);
796 1.1 christos DPRINTF("foreign addr: %s\n", ip6_sprintf(&in6p->in6p_faddr));
797 1.1 christos DPRINTF("foreign port: %d\n", in6p->in6p_fport);
798 1.1 christos break;
799 1.1 christos }
800 1.1 christos #endif
801 1.1 christos default:
802 1.1 christos break;
803 1.1 christos }
804 1.1 christos
805 1.1 christos DPRINTF("%s portalgo = %d\n", __func__, algo);
806 1.1 christos
807 1.1 christos error = (*algos[algo].func)(algo, &lport, inp_hdr, cred);
808 1.1 christos if (error == 0) {
809 1.1 christos *port = lport;
810 1.1 christos } else if (error != EAGAIN) {
811 1.1 christos uint16_t lastport, mymin, mymax, *pnext_ephemeral;
812 1.1 christos
813 1.1 christos error = pcb_getports(inp_hdr, &lastport, &mymin,
814 1.1 christos &mymax, &pnext_ephemeral, algo);
815 1.1 christos if (error)
816 1.1 christos return error;
817 1.1 christos *port = lastport - 1;
818 1.1 christos }
819 1.1 christos return error;
820 1.1 christos }
821 1.1 christos
822 1.1 christos /* Sets the algorithm to be used globally */
823 1.1 christos static int
824 1.1 christos portalgo_algo_name_select(const char *name, int *algo)
825 1.1 christos {
826 1.1 christos size_t ai;
827 1.1 christos
828 1.1 christos DPRINTF("%s called\n", __func__);
829 1.1 christos
830 1.1 christos for (ai = 0; ai < NALGOS; ai++)
831 1.1 christos if (strcmp(algos[ai].name, name) == 0) {
832 1.1 christos DPRINTF("%s: found idx %zu\n", __func__, ai);
833 1.1 christos *algo = ai;
834 1.1 christos return 0;
835 1.1 christos }
836 1.1 christos return EINVAL;
837 1.1 christos }
838 1.1 christos
839 1.1 christos /* Sets the algorithm to be used by the pcb inp. */
840 1.1 christos int
841 1.1 christos portalgo_algo_index_select(struct inpcb_hdr *inp, int algo)
842 1.1 christos {
843 1.1 christos
844 1.1 christos DPRINTF("%s called with algo %d for pcb %p\n", __func__, algo, inp );
845 1.1 christos
846 1.1 christos if ((algo < 0 || algo >= NALGOS) &&
847 1.1 christos (algo != PORTALGO_DEFAULT))
848 1.1 christos return EINVAL;
849 1.1 christos
850 1.1 christos inp->inph_portalgo = algo;
851 1.1 christos return 0;
852 1.1 christos }
853 1.1 christos
854 1.1 christos /*
855 1.1 christos * The sysctl hook that is supposed to check that we are picking one
856 1.1 christos * of the valid algorithms. IPv4.
857 1.1 christos */
858 1.1 christos static int
859 1.1 christos sysctl_portalgo_helper(SYSCTLFN_ARGS, int *algo)
860 1.1 christos {
861 1.1 christos struct sysctlnode node;
862 1.1 christos int error;
863 1.1 christos char newalgo[PORTALGO_MAXLEN];
864 1.1 christos
865 1.1 christos DPRINTF("%s called\n", __func__);
866 1.1 christos
867 1.1 christos strlcpy(newalgo, algos[*algo].name, sizeof(newalgo));
868 1.1 christos
869 1.1 christos node = *rnode;
870 1.1 christos node.sysctl_data = newalgo;
871 1.1 christos node.sysctl_size = sizeof(newalgo);
872 1.1 christos
873 1.1 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
874 1.1 christos
875 1.1 christos DPRINTF("newalgo: %s\n", newalgo);
876 1.1 christos
877 1.1 christos if (error || newp == NULL ||
878 1.1 christos strncmp(newalgo, algos[*algo].name, sizeof(newalgo)) == 0)
879 1.1 christos return error;
880 1.1 christos
881 1.1 christos #ifdef KAUTH_NETWORK_SOCKET_PORT_RANDOMIZE
882 1.1 christos if (l != NULL && (error = kauth_authorize_system(l->l_cred,
883 1.1 christos KAUTH_NETWORK_SOCKET, KAUTH_NETWORK_SOCKET_PORT_RANDOMIZE, newname,
884 1.1 christos NULL, NULL)) != 0)
885 1.1 christos return error;
886 1.1 christos #endif
887 1.1 christos
888 1.1 christos mutex_enter(softnet_lock);
889 1.1 christos error = portalgo_algo_name_select(newalgo, algo);
890 1.1 christos mutex_exit(softnet_lock);
891 1.1 christos return error;
892 1.1 christos }
893 1.1 christos
894 1.1 christos /*
895 1.1 christos * The sysctl hook that is supposed to check that we are picking one
896 1.1 christos * of the valid algorithms.
897 1.1 christos */
898 1.1 christos int
899 1.1 christos sysctl_portalgo_selected(SYSCTLFN_ARGS)
900 1.1 christos {
901 1.1 christos
902 1.1 christos return sysctl_portalgo_helper(SYSCTLFN_CALL(rnode), &inet4_portalgo);
903 1.1 christos }
904 1.1 christos
905 1.1 christos #ifdef INET6
906 1.1 christos int
907 1.1 christos sysctl_portalgo_selected6(SYSCTLFN_ARGS)
908 1.1 christos {
909 1.1 christos
910 1.1 christos return sysctl_portalgo_helper(SYSCTLFN_CALL(rnode), &inet6_portalgo);
911 1.1 christos }
912 1.1 christos #endif
913 1.1 christos
914 1.1 christos /*
915 1.1 christos * The sysctl hook that returns the available
916 1.1 christos * algorithms.
917 1.1 christos */
918 1.1 christos int
919 1.1 christos sysctl_portalgo_available(SYSCTLFN_ARGS)
920 1.1 christos {
921 1.1 christos size_t ai, len = 0;
922 1.1 christos struct sysctlnode node;
923 1.1 christos char availalgo[NALGOS * PORTALGO_MAXLEN];
924 1.1 christos
925 1.1 christos DPRINTF("%s called\n", __func__);
926 1.1 christos
927 1.1 christos availalgo[0] = '\0';
928 1.1 christos
929 1.1 christos for (ai = 0; ai < NALGOS; ai++) {
930 1.1 christos len = strlcat(availalgo, algos[ai].name, sizeof(availalgo));
931 1.1 christos if (ai < NALGOS - 1)
932 1.1 christos strlcat(availalgo, " ", sizeof(availalgo));
933 1.1 christos }
934 1.1 christos
935 1.1 christos DPRINTF("available algos: %s\n", availalgo);
936 1.1 christos
937 1.1 christos node = *rnode;
938 1.1 christos node.sysctl_data = availalgo;
939 1.1 christos node.sysctl_size = len;
940 1.1 christos
941 1.1 christos return sysctl_lookup(SYSCTLFN_CALL(&node));
942 1.1 christos }
943