xref: /xnu-8020.121.3/bsd/skywalk/nexus/flowswitch/flow/flow_namespace.c (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1 /*
2  * Copyright (c) 2015-2017 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <sys/systm.h>
30 #include <sys/types.h>
31 #include <sys/random.h>
32 
33 #include <skywalk/os_skywalk_private.h>
34 
35 #include <netinet/in.h>
36 #include <netinet/in_var.h>
37 #include <netinet/in_pcb.h> /* for ipport_firstauto and ipport_lastauto */
38 
39 #include <skywalk/nexus/flowswitch/flow/flow_var.h>
40 
41 /*
42  * caller needs to do local addr resolution (e.g. hostos_sk_source_addr_select)
43  * before calling this if a specific laddr is to be reserved. Otherwise it
44  * would bind to ADDR_ANY.
45  */
46 int
flow_namespace_create(union sockaddr_in_4_6 * laddr,uint8_t protocol,netns_token * token,uint16_t nfr_flags,struct ns_flow_info * nfi)47 flow_namespace_create(union sockaddr_in_4_6 *laddr, uint8_t protocol,
48     netns_token *token, uint16_t nfr_flags, struct ns_flow_info *nfi)
49 {
50 	sa_family_t af = laddr->sa.sa_family;
51 	uint32_t *addr;
52 	uint32_t netns_rsv_flags = NETNS_SKYWALK;
53 	uint8_t addr_len;
54 	int err;
55 	int so_type = 0;
56 
57 	*token = NULL;
58 
59 	if (__improbable(!netns_is_enabled())) {
60 		SK_ERR("netns is not enabled");
61 		return ENOTSUP;
62 	}
63 
64 	if (nfr_flags & NXFLOWREQF_LISTENER) {
65 		netns_rsv_flags = NETNS_LISTENER;
66 	}
67 	if (nfr_flags & NXFLOWREQF_NOWAKEFROMSLEEP) {
68 		netns_rsv_flags |= NETNS_NOWAKEFROMSLEEP;
69 	}
70 
71 	/* validate protocol */
72 	switch (protocol) {
73 	case IPPROTO_UDP:
74 		so_type = SOCK_DGRAM;
75 		break;
76 
77 	case IPPROTO_TCP:
78 		so_type = SOCK_STREAM;
79 		break;
80 
81 	default:
82 		SK_ERR("invalid protocol (%d)", protocol);
83 		return EINVAL;
84 	}
85 
86 	/* set up addresses */
87 	switch (af) {
88 	case AF_INET:
89 		addr = (uint32_t *)&laddr->sin.sin_addr;
90 		addr_len = 4;
91 		break;
92 
93 	case AF_INET6:
94 		addr = (uint32_t *)&laddr->sin6.sin6_addr;
95 		addr_len = 16;
96 		break;
97 
98 	default:
99 		SK_ERR("invalid src address family (%d)", laddr->sa.sa_family);
100 		return EINVAL;
101 	}
102 
103 	/* Assign an ephemeral port, if no port was specified */
104 	if (laddr->sin.sin_port == 0) {
105 		err = netns_reserve_ephemeral(token, addr, addr_len, protocol,
106 		    &laddr->sin.sin_port, netns_rsv_flags, nfi);
107 	} else {
108 		err = netns_reserve(token, addr, addr_len, protocol,
109 		    laddr->sin.sin_port, netns_rsv_flags, nfi);
110 	}
111 
112 	SK_DF(SK_VERB_FLOW, "token (%s port %d) BIND",
113 	    (protocol == IPPROTO_TCP) ? "tcp" : "udp",
114 	    ntohs(laddr->sin.sin_port));
115 
116 	return err;
117 }
118 
119 void
flow_namespace_destroy(netns_token * token)120 flow_namespace_destroy(netns_token *token)
121 {
122 	netns_release(token);
123 }
124 
125 void
flow_namespace_half_close(netns_token * token)126 flow_namespace_half_close(netns_token *token)
127 {
128 	if (NETNS_TOKEN_VALID(token)) {
129 		netns_half_close(token);
130 	}
131 }
132 
133 void
flow_namespace_withdraw(netns_token * token)134 flow_namespace_withdraw(netns_token *token)
135 {
136 	if (NETNS_TOKEN_VALID(token)) {
137 		netns_withdraw(token);
138 	}
139 }
140