1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2020 Mariusz Zaborski <oshogbo@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #ifndef _CAP_NETWORK_H_
29 #define _CAP_NETWORK_H_
30
31 #ifdef HAVE_CASPER
32 #define WITH_CASPER
33 #endif
34
35 #include <sys/dnv.h>
36 #include <sys/nv.h>
37
38 #include <sys/socket.h>
39
40 struct addrinfo;
41 struct hostent;
42
43 struct cap_net_limit;
44 typedef struct cap_net_limit cap_net_limit_t;
45
46 #define CAPNET_ADDR2NAME (0x01)
47 #define CAPNET_NAME2ADDR (0x02)
48 #define CAPNET_DEPRECATED_ADDR2NAME (0x04)
49 #define CAPNET_DEPRECATED_NAME2ADDR (0x08)
50 #define CAPNET_CONNECT (0x10)
51 #define CAPNET_BIND (0x20)
52 #define CAPNET_CONNECTDNS (0x40)
53
54 #ifdef WITH_CASPER
55 /* Capability functions. */
56 int cap_bind(cap_channel_t *chan, int s, const struct sockaddr *addr,
57 socklen_t addrlen);
58 int cap_connect(cap_channel_t *chan, int s, const struct sockaddr *name,
59 socklen_t namelen);
60
61 int cap_getaddrinfo(cap_channel_t *chan, const char *hostname,
62 const char *servname, const struct addrinfo *hints, struct addrinfo **res);
63 int cap_getnameinfo(cap_channel_t *chan, const struct sockaddr *sa,
64 socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen,
65 int flags);
66
67 /* Limit functions. */
68 cap_net_limit_t *cap_net_limit_init(cap_channel_t *chan, uint64_t mode);
69 int cap_net_limit(cap_net_limit_t *limit);
70 void cap_net_free(cap_net_limit_t *limit);
71
72 cap_net_limit_t *cap_net_limit_addr2name_family(cap_net_limit_t *limit,
73 int *family, size_t size);
74 cap_net_limit_t *cap_net_limit_addr2name(cap_net_limit_t *limit,
75 const struct sockaddr *sa, socklen_t salen);
76
77 cap_net_limit_t *cap_net_limit_name2addr_family(cap_net_limit_t *limit,
78 int *family, size_t size);
79 cap_net_limit_t *cap_net_limit_name2addr(cap_net_limit_t *limit,
80 const char *name, const char *serv);
81
82 cap_net_limit_t *cap_net_limit_connect(cap_net_limit_t *limit,
83 const struct sockaddr *sa, socklen_t salen);
84
85 cap_net_limit_t *cap_net_limit_bind(cap_net_limit_t *limit,
86 const struct sockaddr *sa, socklen_t salen);
87
88 /* Deprecated functions. */
89 struct hostent *cap_gethostbyname(cap_channel_t *chan, const char *name);
90 struct hostent *cap_gethostbyname2(cap_channel_t *chan, const char *name,
91 int af);
92 struct hostent *cap_gethostbyaddr(cap_channel_t *chan, const void *addr,
93 socklen_t len, int af);
94 #else
95 /* Capability functions. */
96 #define cap_bind(chan, s, addr, addrlen) \
97 bind(s, addr, addrlen)
98 #define cap_connect(chan, s, name, namelen) \
99 connect(s, name, namelen)
100 #define cap_getaddrinfo(chan, hostname, servname, hints, res) \
101 getaddrinfo(hostname, servname, hints, res)
102 #define cap_getnameinfo(chan, sa, salen, host, hostlen, serv, servlen, flags) \
103 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
104
105 /* Limit functions. */
106 #define cap_net_limit_init(chan, mode) ((cap_net_limit_t *)malloc(8))
107 #define cap_net_free(limit) free(limit)
108 static inline int
cap_net_limit(cap_net_limit_t * limit)109 cap_net_limit(cap_net_limit_t *limit)
110 {
111 free(limit);
112 return (0);
113 }
114
115 static inline cap_net_limit_t *
cap_net_limit_addr2name_family(cap_net_limit_t * limit,int * family __unused,size_t size __unused)116 cap_net_limit_addr2name_family(cap_net_limit_t *limit,
117 int *family __unused, size_t size __unused)
118 {
119 return (limit);
120 }
121
122 static inline cap_net_limit_t *
cap_net_limit_addr2name(cap_net_limit_t * limit,const struct sockaddr * sa __unused,socklen_t salen __unused)123 cap_net_limit_addr2name(cap_net_limit_t *limit,
124 const struct sockaddr *sa __unused, socklen_t salen __unused)
125 {
126 return (limit);
127 }
128
129 static inline cap_net_limit_t *
cap_net_limit_name2addr_family(cap_net_limit_t * limit,int * family __unused,size_t size __unused)130 cap_net_limit_name2addr_family(cap_net_limit_t *limit,
131 int *family __unused, size_t size __unused)
132 {
133 return (limit);
134 }
135
136 static inline cap_net_limit_t *
cap_net_limit_name2addr(cap_net_limit_t * limit,const char * name __unused,const char * serv __unused)137 cap_net_limit_name2addr(cap_net_limit_t *limit,
138 const char *name __unused, const char *serv __unused)
139 {
140 return (limit);
141 }
142
143 static inline cap_net_limit_t *
cap_net_limit_connect(cap_net_limit_t * limit,const struct sockaddr * sa __unused,socklen_t salen __unused)144 cap_net_limit_connect(cap_net_limit_t *limit,
145 const struct sockaddr *sa __unused, socklen_t salen __unused)
146 {
147 return (limit);
148 }
149
150 static inline cap_net_limit_t *
cap_net_limit_bind(cap_net_limit_t * limit,const struct sockaddr * sa __unused,socklen_t salen __unused)151 cap_net_limit_bind(cap_net_limit_t *limit,
152 const struct sockaddr *sa __unused, socklen_t salen __unused)
153 {
154 return (limit);
155 }
156
157 /* Deprecated functions. */
158 #define cap_gethostbyname(chan, name) gethostbyname(name)
159 #define cap_gethostbyname2(chan, name, type) gethostbyname2(name, type)
160 #define cap_gethostbyaddr(chan, addr, len, type) gethostbyaddr(addr, len, type)
161 #endif
162
163 #endif /* !_CAP_NETWORK_H_ */
164