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