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,boolean_t listener,struct ns_flow_info * nfi)47 flow_namespace_create(union sockaddr_in_4_6 *laddr, uint8_t protocol,
48 netns_token *token, boolean_t listener, 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 (listener) {
65 netns_rsv_flags = NETNS_LISTENER;
66 } else {
67 netns_rsv_flags = NETNS_SKYWALK;
68 }
69
70 /* validate protocol */
71 switch (protocol) {
72 case IPPROTO_UDP:
73 so_type = SOCK_DGRAM;
74 break;
75
76 case IPPROTO_TCP:
77 so_type = SOCK_STREAM;
78 break;
79
80 default:
81 SK_ERR("invalid protocol (%d)", protocol);
82 return EINVAL;
83 }
84
85 /* set up addresses */
86 switch (af) {
87 case AF_INET:
88 addr = (uint32_t *)&laddr->sin.sin_addr;
89 addr_len = 4;
90 break;
91
92 case AF_INET6:
93 addr = (uint32_t *)&laddr->sin6.sin6_addr;
94 addr_len = 16;
95 break;
96
97 default:
98 SK_ERR("invalid src address family (%d)", laddr->sa.sa_family);
99 return EINVAL;
100 }
101
102 /* Assign an ephemeral port, if no port was specified */
103 if (laddr->sin.sin_port == 0) {
104 err = netns_reserve_ephemeral(token, addr, addr_len, protocol,
105 &laddr->sin.sin_port, netns_rsv_flags, nfi);
106 } else {
107 err = netns_reserve(token, addr, addr_len, protocol,
108 laddr->sin.sin_port, netns_rsv_flags, nfi);
109 }
110
111 SK_DF(SK_VERB_FLOW, "token (%s port %d) BIND",
112 (protocol == IPPROTO_TCP) ? "tcp" : "udp",
113 ntohs(laddr->sin.sin_port));
114
115 return err;
116 }
117
118 void
flow_namespace_destroy(netns_token * token)119 flow_namespace_destroy(netns_token *token)
120 {
121 netns_release(token);
122 }
123
124 void
flow_namespace_half_close(netns_token * token)125 flow_namespace_half_close(netns_token *token)
126 {
127 if (NETNS_TOKEN_VALID(token)) {
128 netns_half_close(token);
129 }
130 }
131
132 void
flow_namespace_withdraw(netns_token * token)133 flow_namespace_withdraw(netns_token *token)
134 {
135 if (NETNS_TOKEN_VALID(token)) {
136 netns_withdraw(token);
137 }
138 }
139