1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
*/
/*
* This is used to support the hidden __sin6_src_id in the sockaddr_in6
* structure which is there to ensure that applications (such as UDP apps)
* which get an address from recvfrom and use that address in a sendto
* or connect will by default use the same source address in the "response"
* as the destination address in the "request" they received.
*
* This is built using some new functions (in IP - doing their own locking
* so they can be called from the transports) to map between integer IDs
* and in6_addr_t.
* The use applies to sockaddr_in6 - whether or not mapped addresses are used.
*
* This file contains the functions used by both IP and the transports
* to implement __sin6_src_id.
* The routines do their own locking since they are called from
* the transports (to map between a source id and an address)
* and from IP proper when IP addresses are added and removed.
*
* The routines handle both IPv4 and IPv6 with the IPv4 addresses represented
* as IPv4-mapped addresses.
*/
#include <sys/types.h>
#include <sys/stream.h>
#include <sys/dlpi.h>
#include <sys/stropts.h>
#include <sys/sysmacros.h>
#include <sys/strsubr.h>
#include <sys/strlog.h>
#define _SUN_TPI_VERSION 2
#include <sys/tihdr.h>
#include <sys/xti_inet.h>
#include <sys/ddi.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/modctl.h>
#include <sys/atomic.h>
#include <sys/zone.h>
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/kmem.h>
#include <sys/callb.h>
#include <sys/socket.h>
#include <sys/vtrace.h>
#include <sys/isa_defs.h>
#include <sys/kmem.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/route.h>
#include <sys/sockio.h>
#include <netinet/in.h>
#include <net/if_dl.h>
#include <inet/common.h>
#include <inet/mi.h>
#include <inet/mib2.h>
#include <inet/nd.h>
#include <inet/arp.h>
#include <inet/snmpcom.h>
#include <netinet/igmp_var.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <inet/ip.h>
#include <inet/ip6.h>
#include <inet/tcp.h>
#include <inet/ip_multi.h>
#include <inet/ip_if.h>
#include <inet/ip_ire.h>
#include <inet/ip_rts.h>
#include <inet/optcom.h>
#include <inet/ip_ndp.h>
#include <netinet/igmp.h>
#include <netinet/ip_mroute.h>
#include <inet/ipclassifier.h>
#include <sys/kmem.h>
static uint_t srcid_nextid(ip_stack_t *);
static srcid_map_t **srcid_lookup_addr(const in6_addr_t *addr,
zoneid_t zoneid, ip_stack_t *);
static srcid_map_t **srcid_lookup_id(uint_t id, ip_stack_t *);
/*
* Insert/add a new address to the map.
* Returns zero if ok; otherwise errno (e.g. for memory allocation failure).
*/
int
ip_srcid_insert(const in6_addr_t *addr, zoneid_t zoneid, ip_stack_t *ipst)
{
srcid_map_t **smpp;
#ifdef DEBUG
char abuf[INET6_ADDRSTRLEN];
ip1dbg(("ip_srcid_insert(%s, %d)\n",
inet_ntop(AF_INET6, addr, abuf, sizeof (abuf)), zoneid));
#endif
rw_enter(&ipst->ips_srcid_lock, RW_WRITER);
smpp = srcid_lookup_addr(addr, zoneid, ipst);
if (*smpp != NULL) {
/* Already present - increment refcount */
(*smpp)->sm_refcnt++;
ASSERT((*smpp)->sm_refcnt != 0); /* wraparound */
rw_exit(&ipst->ips_srcid_lock);
return (0);
}
/* Insert new */
*smpp = kmem_alloc(sizeof (srcid_map_t), KM_NOSLEEP);
if (*smpp == NULL) {
rw_exit(&ipst->ips_srcid_lock);
return (ENOMEM);
}
(*smpp)->sm_next = NULL;
(*smpp)->sm_addr = *addr;
(*smpp)->sm_srcid = srcid_nextid(ipst);
(*smpp)->sm_refcnt = 1;
(*smpp)->sm_zoneid = zoneid;
rw_exit(&ipst->ips_srcid_lock);
return (0);
}
/*
* Remove an new address from the map.
* Returns zero if ok; otherwise errno (e.g. for nonexistent address).
*/
int
ip_srcid_remove(const in6_addr_t *addr, zoneid_t zoneid, ip_stack_t *ipst)
{
srcid_map_t **smpp;
srcid_map_t *smp;
#ifdef DEBUG
char abuf[INET6_ADDRSTRLEN];
ip1dbg(("ip_srcid_remove(%s, %d)\n",
inet_ntop(AF_INET6, addr, abuf, sizeof (abuf)), zoneid));
#endif
rw_enter(&ipst->ips_srcid_lock, RW_WRITER);
smpp = srcid_lookup_addr(addr, zoneid, ipst);
smp = *smpp;
if (smp == NULL) {
/* Not preset */
rw_exit(&ipst->ips_srcid_lock);
return (ENOENT);
}
/* Decrement refcount */
ASSERT(smp->sm_refcnt != 0);
smp->sm_refcnt--;
if (smp->sm_refcnt != 0) {
rw_exit(&ipst->ips_srcid_lock);
return (0);
}
/* Remove entry */
*smpp = smp->sm_next;
rw_exit(&ipst->ips_srcid_lock);
smp->sm_next = NULL;
kmem_free(smp, sizeof (srcid_map_t));
return (0);
}
/*
* Map from an address to a source id.
* If the address is unknown return the unknown id (zero).
*/
uint_t
ip_srcid_find_addr(const in6_addr_t *addr, zoneid_t zoneid,
netstack_t *ns)
{
srcid_map_t **smpp;
srcid_map_t *smp;
uint_t id;
ip_stack_t *ipst = ns->netstack_ip;
rw_enter(&ipst->ips_srcid_lock, RW_READER);
smpp = srcid_lookup_addr(addr, zoneid, ipst);
smp = *smpp;
if (smp == NULL) {
char abuf[INET6_ADDRSTRLEN];
/* Not present - could be broadcast or multicast address */
ip1dbg(("ip_srcid_find_addr: unknown %s in zone %d\n",
inet_ntop(AF_INET6, addr, abuf, sizeof (abuf)), zoneid));
id = 0;
} else {
ASSERT(smp->sm_refcnt != 0);
id = smp->sm_srcid;
}
rw_exit(&ipst->ips_srcid_lock);
return (id);
}
/*
* Map from a source id to an address.
* If the id is unknown return the unspecified address.
*
* For known IDs, check if the returned address is v4mapped or not, and
* return B_TRUE if it matches the desired v4mapped state or not. This
* prevents a broken app from requesting (via __sin6_src_id) a v4mapped
* address for a v6 destination, or vice versa.
*
* "addr" will not be set if we return B_FALSE.
*/
boolean_t
ip_srcid_find_id(uint_t id, in6_addr_t *addr, zoneid_t zoneid,
boolean_t v4mapped, netstack_t *ns)
{
srcid_map_t **smpp;
srcid_map_t *smp;
ip_stack_t *ipst = ns->netstack_ip;
boolean_t rc;
rw_enter(&ipst->ips_srcid_lock, RW_READER);
smpp = srcid_lookup_id(id, ipst);
smp = *smpp;
if (smp == NULL || (smp->sm_zoneid != zoneid && zoneid != ALL_ZONES)) {
/* Not preset */
ip1dbg(("ip_srcid_find_id: unknown %u or in wrong zone\n", id));
*addr = ipv6_all_zeros;
rc = B_TRUE;
} else {
ASSERT(smp->sm_refcnt != 0);
/*
* The caller tells us if it expects a v4mapped address.
* Use it, along with the property of "addr" to set the rc.
*/
if (IN6_IS_ADDR_V4MAPPED(&smp->sm_addr))
rc = v4mapped; /* We want a v4mapped address. */
else
rc = !v4mapped; /* We don't want a v4mapped address. */
if (rc)
*addr = smp->sm_addr;
}
rw_exit(&ipst->ips_srcid_lock);
return (rc);
}
/* Assign the next available ID */
static uint_t
srcid_nextid(ip_stack_t *ipst)
{
uint_t id;
srcid_map_t **smpp;
ASSERT(rw_owner(&ipst->ips_srcid_lock) == curthread);
if (!ipst->ips_srcid_wrapped) {
id = ipst->ips_ip_src_id++;
if (ipst->ips_ip_src_id == 0)
ipst->ips_srcid_wrapped = B_TRUE;
return (id);
}
/* Once it wraps we search for an unused ID. */
for (id = 0; id < 0xffffffff; id++) {
smpp = srcid_lookup_id(id, ipst);
if (*smpp == NULL)
return (id);
}
panic("srcid_nextid: No free identifiers!");
/* NOTREACHED */
}
/*
* Lookup based on address.
* Always returns a non-null pointer.
* If found then *ptr will be the found object.
* Otherwise *ptr will be NULL and can be used to insert a new object.
*/
static srcid_map_t **
srcid_lookup_addr(const in6_addr_t *addr, zoneid_t zoneid, ip_stack_t *ipst)
{
srcid_map_t **smpp;
ASSERT(RW_LOCK_HELD(&ipst->ips_srcid_lock));
smpp = &ipst->ips_srcid_head;
while (*smpp != NULL) {
if (IN6_ARE_ADDR_EQUAL(&(*smpp)->sm_addr, addr) &&
(zoneid == (*smpp)->sm_zoneid || zoneid == ALL_ZONES))
return (smpp);
smpp = &(*smpp)->sm_next;
}
return (smpp);
}
/*
* Lookup based on address.
* Always returns a non-null pointer.
* If found then *ptr will be the found object.
* Otherwise *ptr will be NULL and can be used to insert a new object.
*/
static srcid_map_t **
srcid_lookup_id(uint_t id, ip_stack_t *ipst)
{
srcid_map_t **smpp;
ASSERT(RW_LOCK_HELD(&ipst->ips_srcid_lock));
smpp = &ipst->ips_srcid_head;
while (*smpp != NULL) {
if ((*smpp)->sm_srcid == id)
return (smpp);
smpp = &(*smpp)->sm_next;
}
return (smpp);
}
|