1/*	$OpenBSD: bindresvport.c,v 1.19 2019/06/28 13:32:42 deraadt Exp $	*/
2
3/*
4 * Copyright 1996, Jason Downs.  All rights reserved.
5 * Copyright 1998, Theo de Raadt.  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 ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <errno.h>
29#include <string.h>
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <netinet/in.h>
33
34/*
35 * Bind a socket to a privileged IP port
36 */
37int
38bindresvport(int sd, struct sockaddr_in *sin)
39{
40	return bindresvport_sa(sd, (struct sockaddr *)sin);
41}
42DEF_WEAK(bindresvport);
43
44/*
45 * Bind a socket to a privileged port for whatever protocol.
46 */
47int
48bindresvport_sa(int sd, struct sockaddr *sa)
49{
50	int old, error, af;
51	struct sockaddr_storage myaddr;
52	struct sockaddr_in *sin;
53	struct sockaddr_in6 *sin6;
54	int proto, portrange, portlow;
55	u_int16_t port;
56	socklen_t salen;
57
58	if (sa == NULL) {
59		salen = sizeof(myaddr);
60		sa = (struct sockaddr *)&myaddr;
61
62		if (getsockname(sd, sa, &salen) == -1)
63			return -1;	/* errno is correctly set */
64
65		af = sa->sa_family;
66		memset(&myaddr, 0, salen);
67	} else
68		af = sa->sa_family;
69
70	if (af == AF_INET) {
71		proto = IPPROTO_IP;
72		portrange = IP_PORTRANGE;
73		portlow = IP_PORTRANGE_LOW;
74		sin = (struct sockaddr_in *)sa;
75		salen = sizeof(struct sockaddr_in);
76		port = sin->sin_port;
77	} else if (af == AF_INET6) {
78		proto = IPPROTO_IPV6;
79		portrange = IPV6_PORTRANGE;
80		portlow = IPV6_PORTRANGE_LOW;
81		sin6 = (struct sockaddr_in6 *)sa;
82		salen = sizeof(struct sockaddr_in6);
83		port = sin6->sin6_port;
84	} else {
85		errno = EPFNOSUPPORT;
86		return (-1);
87	}
88	sa->sa_family = af;
89	sa->sa_len = salen;
90
91	if (port == 0) {
92		socklen_t oldlen = sizeof(old);
93
94		error = getsockopt(sd, proto, portrange, &old, &oldlen);
95		if (error == -1)
96			return (error);
97
98		error = setsockopt(sd, proto, portrange, &portlow,
99		    sizeof(portlow));
100		if (error == -1)
101			return (error);
102	}
103
104	error = bind(sd, sa, salen);
105
106	if (port == 0) {
107		int saved_errno = errno;
108
109		if (error) {
110			if (setsockopt(sd, proto, portrange, &old,
111			    sizeof(old)) == -1)
112				errno = saved_errno;
113			return (error);
114		}
115
116		if (sa != (struct sockaddr *)&myaddr) {
117			/* Hmm, what did the kernel assign... */
118			if (getsockname(sd, sa, &salen) == -1)
119				errno = saved_errno;
120			return (error);
121		}
122	}
123	return (error);
124}
125DEF_WEAK(bindresvport_sa);
126