• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBCONNECTIVITY_CONNECTIVITY_NATIVE_H_
18 #define LIBCONNECTIVITY_CONNECTIVITY_NATIVE_H_
19 
20 #include <sys/cdefs.h>
21 #include <netinet/in.h>
22 
23 __BEGIN_DECLS
24 
25 /**
26  * Blocks a port from being assigned during bind(). The caller is responsible for updating
27  * /proc/sys/net/ipv4/ip_local_port_range with the port being blocked so that calls to connect()
28  * will not automatically assign one of the blocked ports.
29  * Will return success even if port was already blocked.
30  *
31  * Returns 0 on success, or a POSIX error code (see errno.h) on failure:
32  *  - EINVAL for invalid port number
33  *  - EPERM if the UID of the client doesn't have network stack permission
34  *  - Other errors as per https://man7.org/linux/man-pages/man2/bpf.2.html
35  *
36  * @param port Int corresponding to port number.
37  */
38 int AConnectivityNative_blockPortForBind(in_port_t port) __INTRODUCED_IN(34);
39 
40 /**
41  * Unblocks a port that has previously been blocked.
42  * Will return success even if port was already unblocked.
43  *
44  * Returns 0 on success, or a POSIX error code (see errno.h) on failure:
45  *  - EINVAL for invalid port number
46  *  - EPERM if the UID of the client doesn't have network stack permission
47  *  - Other errors as per https://man7.org/linux/man-pages/man2/bpf.2.html
48  *
49  * @param port Int corresponding to port number.
50  */
51 int AConnectivityNative_unblockPortForBind(in_port_t port) __INTRODUCED_IN(34);
52 
53 /**
54  * Unblocks all ports that have previously been blocked.
55  *
56  * Returns 0 on success, or a POSIX error code (see errno.h) on failure:
57  *  - EINVAL for invalid port number
58  *  - EPERM if the UID of the client doesn't have network stack permission
59  *  - Other errors as per https://man7.org/linux/man-pages/man2/bpf.2.html
60  */
61 int AConnectivityNative_unblockAllPortsForBind() __INTRODUCED_IN(34);
62 
63 /**
64  * Gets the list of ports that have been blocked.
65  *
66  * Returns 0 on success, or a POSIX error code (see errno.h) on failure:
67  *  - EINVAL for invalid port number
68  *  - EPERM if the UID of the client doesn't have network stack permission
69  *  - Other errors as per https://man7.org/linux/man-pages/man2/bpf.2.html
70  *
71  * @param ports Array of ports that will be filled with the port numbers.
72  * @param count Pointer to the size of the ports array; the value will be set to the total number of
73  *              blocked ports, which may be larger than the ports array that was filled.
74  */
75 int AConnectivityNative_getPortsBlockedForBind(in_port_t* _Nonnull ports, size_t* _Nonnull count)
76     __INTRODUCED_IN(34);
77 
78 __END_DECLS
79 
80 
81 #endif
82