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