xref: /titanic_44/usr/src/uts/common/inet/ip/ip_rts.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
1 /*
2  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 1988, 1991, 1993
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)rtsock.c	8.6 (Berkeley) 2/11/95
39  */
40 
41 #pragma ident	"%Z%%M%	%I%	%E% SMI"
42 
43 /*
44  * This file contains routines that processes routing socket requests.
45  */
46 
47 #include <sys/types.h>
48 #include <sys/stream.h>
49 #include <sys/stropts.h>
50 #include <sys/ddi.h>
51 #include <sys/cmn_err.h>
52 #include <sys/debug.h>
53 #include <sys/policy.h>
54 #include <sys/zone.h>
55 
56 #include <sys/systm.h>
57 #include <sys/param.h>
58 #include <sys/socket.h>
59 #include <sys/strsun.h>
60 #include <net/if.h>
61 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <net/if_dl.h>
64 #include <netinet/ip6.h>
65 
66 #include <inet/common.h>
67 #include <inet/ip.h>
68 #include <inet/ip6.h>
69 #include <inet/ip_if.h>
70 #include <inet/ip_ire.h>
71 #include <inet/ip_ftable.h>
72 #include <inet/ip_rts.h>
73 
74 #include <inet/ipclassifier.h>
75 
76 #include <sys/tsol/tndb.h>
77 #include <sys/tsol/tnet.h>
78 
79 #define	RTS_MSG_SIZE(type, rtm_addrs, af, sacnt) \
80 	(rts_data_msg_size(rtm_addrs, af, sacnt) + rts_header_msg_size(type))
81 
82 static size_t	rts_copyfromsockaddr(struct sockaddr *sa, in6_addr_t *addrp);
83 static void	rts_fill_msg(int type, int rtm_addrs, ipaddr_t dst,
84     ipaddr_t mask, ipaddr_t gateway, ipaddr_t src_addr, ipaddr_t brd_addr,
85     ipaddr_t author, const ipif_t *ipif, mblk_t *mp, uint_t, const tsol_gc_t *);
86 static int	rts_getaddrs(rt_msghdr_t *rtm, in6_addr_t *dst_addrp,
87     in6_addr_t *gw_addrp, in6_addr_t *net_maskp, in6_addr_t *authorp,
88     in6_addr_t *if_addrp, in6_addr_t *src_addrp, ushort_t *indexp,
89     ushort_t *src_indexp, sa_family_t *afp, tsol_rtsecattr_t *rtsecattr,
90     int *error);
91 static void	rts_getifdata(if_data_t *if_data, const ipif_t *ipif);
92 static int	rts_getmetrics(ire_t *ire, rt_metrics_t *metrics);
93 static mblk_t	*rts_rtmget(mblk_t *mp, ire_t *ire, ire_t *sire,
94     sa_family_t af);
95 static void	rts_setmetrics(ire_t *ire, uint_t which, rt_metrics_t *metrics);
96 static void	ip_rts_request_retry(ipsq_t *, queue_t *q, mblk_t *mp, void *);
97 
98 /*
99  * Send the ack to all the routing queues.  In case of the originating queue,
100  * send it only if the loopback is set.
101  *
102  * Messages are sent upstream only on routing sockets that did not specify an
103  * address family when they were created or when the address family matches the
104  * one specified by the caller.
105  *
106  */
107 void
108 rts_queue_input(mblk_t *mp, queue_t *q, sa_family_t af)
109 {
110 	mblk_t	*mp1;
111 	int	checkqfull;
112 	conn_t 	*connp, *next_connp;
113 
114 	mutex_enter(&rts_clients.connf_lock);
115 	connp = rts_clients.connf_head;
116 
117 	while (connp != NULL) {
118 		/*
119 		 * If there was a family specified when this routing socket was
120 		 * created and it doesn't match the family of the message to
121 		 * copy, then continue.
122 		 */
123 		if ((connp->conn_proto != AF_UNSPEC) &&
124 		    (connp->conn_proto != af)) {
125 			connp = connp->conn_next;
126 			continue;
127 		}
128 		/*
129 		 * For the originating queue, we only copy the message upstream
130 		 * if loopback is set.  For others reading on the routing
131 		 * socket, we check if there is room upstream for a copy of the
132 		 * message.
133 		 */
134 		if ((q != NULL) && (CONNP_TO_RQ(connp) == RD(q))) {
135 			if (connp->conn_loopback == 0) {
136 				connp = connp->conn_next;
137 				continue;
138 			}
139 			checkqfull = B_FALSE;
140 		} else {
141 			checkqfull = B_TRUE;
142 		}
143 		CONN_INC_REF(connp);
144 		mutex_exit(&rts_clients.connf_lock);
145 		if (!checkqfull || canputnext(CONNP_TO_RQ(connp))) {
146 			mp1 = dupmsg(mp);
147 			if (mp1 == NULL)
148 				mp1 = copymsg(mp);
149 			if (mp1 != NULL)
150 				putnext(CONNP_TO_RQ(connp), mp1);
151 		}
152 
153 		mutex_enter(&rts_clients.connf_lock);
154 		/* Follow the next pointer before releasing the conn. */
155 		next_connp = connp->conn_next;
156 		CONN_DEC_REF(connp);
157 		connp = next_connp;
158 	}
159 	mutex_exit(&rts_clients.connf_lock);
160 	freemsg(mp);
161 }
162 
163 /*
164  * Takes an ire and sends an ack to all the routing sockets. This
165  * routine is used
166  * - when a route is created/deleted through the ioctl interface.
167  * - when ire_expire deletes a stale redirect
168  */
169 void
170 ip_rts_rtmsg(int type, ire_t *ire, int error)
171 {
172 	mblk_t		*mp;
173 	rt_msghdr_t	*rtm;
174 	int		rtm_addrs = (RTA_DST | RTA_NETMASK | RTA_GATEWAY);
175 	sa_family_t	af;
176 	in6_addr_t	gw_addr_v6;
177 
178 	if (ire == NULL)
179 		return;
180 	ASSERT(ire->ire_ipversion == IPV4_VERSION ||
181 	    ire->ire_ipversion == IPV6_VERSION);
182 
183 	if (ire->ire_flags & RTF_SETSRC)
184 		rtm_addrs |= RTA_SRC;
185 
186 	switch (ire->ire_ipversion) {
187 	case IPV4_VERSION:
188 		af = AF_INET;
189 		mp = rts_alloc_msg(type, rtm_addrs, af, 0);
190 		if (mp == NULL)
191 			return;
192 		rts_fill_msg(type, rtm_addrs, ire->ire_addr, ire->ire_mask,
193 		    ire->ire_gateway_addr, ire->ire_src_addr, 0, 0, NULL, mp,
194 		    0, NULL);
195 		break;
196 	case IPV6_VERSION:
197 		af = AF_INET6;
198 		mp = rts_alloc_msg(type, rtm_addrs, af, 0);
199 		if (mp == NULL)
200 			return;
201 		mutex_enter(&ire->ire_lock);
202 		gw_addr_v6 = ire->ire_gateway_addr_v6;
203 		mutex_exit(&ire->ire_lock);
204 		rts_fill_msg_v6(type, rtm_addrs, &ire->ire_addr_v6,
205 		    &ire->ire_mask_v6, &gw_addr_v6,
206 		    &ire->ire_src_addr_v6, &ipv6_all_zeros, &ipv6_all_zeros,
207 		    NULL, mp, 0, NULL);
208 		break;
209 	}
210 	rtm = (rt_msghdr_t *)mp->b_rptr;
211 	mp->b_wptr = (uchar_t *)&mp->b_rptr[rtm->rtm_msglen];
212 	rtm->rtm_addrs = rtm_addrs;
213 	rtm->rtm_flags = ire->ire_flags;
214 	if (error != 0)
215 		rtm->rtm_errno = error;
216 	else
217 		rtm->rtm_flags |= RTF_DONE;
218 	rts_queue_input(mp, NULL, af);
219 }
220 
221 /* ARGSUSED */
222 static void
223 ip_rts_request_retry(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, void *dummy)
224 {
225 	(void) ip_rts_request(q, mp, DB_CRED(mp));
226 }
227 
228 /*
229  * Processes requests received on a routing socket. It extracts all the
230  * arguments and calls the appropriate function to process the request.
231  *
232  * RTA_SRC bit flag requests are sent by mipagent and 'route -setsrc'.
233  * RTA_SRCIFP bit flag requests are sent by mipagent only.
234  *
235  * In general, this function does not consume the message supplied but rather
236  * sends the message upstream with an appropriate UNIX errno.
237  *
238  * We may need to restart this operation if the ipif cannot be looked up
239  * due to an exclusive operation that is currently in progress. The restart
240  * entry point is ip_rts_request_retry. While the request is enqueud in the
241  * ipsq the ioctl could be aborted and the conn close. To ensure that we don't
242  * have stale conn pointers, ip_wput_ioctl does a conn refhold. This is
243  * released at the completion of the rts ioctl at the end of this function
244  * by calling CONN_OPER_PENDING_DONE or when the ioctl is aborted and
245  * conn close occurs in conn_ioctl_cleanup.
246  */
247 int
248 ip_rts_request(queue_t *q, mblk_t *mp, cred_t *ioc_cr)
249 {
250 	rt_msghdr_t	*rtm = NULL;
251 	in6_addr_t	dst_addr_v6;
252 	in6_addr_t	src_addr_v6;
253 	in6_addr_t	gw_addr_v6;
254 	in6_addr_t	net_mask_v6;
255 	in6_addr_t	author_v6;
256 	in6_addr_t	if_addr_v6;
257 	mblk_t		*mp1, *ioc_mp = mp;
258 	ire_t		*ire = NULL;
259 	ire_t		*sire = NULL;
260 	int		error = 0;
261 	int		match_flags = MATCH_IRE_DSTONLY;
262 	int		match_flags_local = MATCH_IRE_TYPE | MATCH_IRE_GW;
263 	int		found_addrs;
264 	sa_family_t	af;
265 	ipaddr_t	dst_addr;
266 	ipaddr_t	gw_addr;
267 	ipaddr_t	src_addr;
268 	ipaddr_t	net_mask;
269 	ushort_t	index;
270 	ushort_t	src_index;
271 	ipif_t		*ipif = NULL;
272 	ipif_t		*src_ipif = NULL;
273 	ipif_t		*tmp_ipif = NULL;
274 	IOCP		iocp = (IOCP)mp->b_rptr;
275 	conn_t		*connp;
276 	boolean_t	gcgrp_xtraref = B_FALSE;
277 	tsol_gcgrp_addr_t ga;
278 	tsol_rtsecattr_t rtsecattr;
279 	struct rtsa_s	*rtsap = NULL;
280 	tsol_gcgrp_t	*gcgrp = NULL;
281 	tsol_gc_t	*gc = NULL;
282 	ts_label_t	*tsl = NULL;
283 	zoneid_t	zoneid;
284 
285 	ip1dbg(("ip_rts_request: mp is %x\n", DB_TYPE(mp)));
286 
287 	ASSERT(CONN_Q(q));
288 	connp = Q_TO_CONN(q);
289 	zoneid = connp->conn_zoneid;
290 
291 	ASSERT(mp->b_cont != NULL);
292 	/* ioc_mp holds mp */
293 	mp = mp->b_cont;
294 
295 	/*
296 	 * The Routing Socket data starts on
297 	 * next block. If there is no next block
298 	 * this is an indication from routing module
299 	 * that it is a routing socket stream queue.
300 	 */
301 	if (mp->b_cont != NULL) {
302 		mp1 = dupmsg(mp->b_cont);
303 		if (mp1 == NULL) {
304 			freemsg(mp);
305 			error  = ENOBUFS;
306 			goto done;
307 		}
308 		mp = mp1;
309 	} else {
310 		/*
311 		 * This is a message from RTS module
312 		 * indicating that this is a Routing Socket
313 		 * Stream. Insert this conn_t in routing
314 		 * socket client list.
315 		 */
316 
317 		connp->conn_loopback = 1;
318 		ipcl_hash_insert_wildcard(&rts_clients, connp);
319 
320 		goto done;
321 	}
322 	if (mp->b_cont != NULL && !pullupmsg(mp, -1)) {
323 		freemsg(mp);
324 		error =  EINVAL;
325 		goto done;
326 	}
327 	if ((mp->b_wptr - mp->b_rptr) < sizeof (rt_msghdr_t)) {
328 		freemsg(mp);
329 		error = EINVAL;
330 		goto done;
331 	}
332 
333 	/*
334 	 * Check the routing message for basic consistency including the
335 	 * version number and that the number of octets written is the same
336 	 * as specified by the rtm_msglen field.
337 	 *
338 	 * At this point, an error can be delivered back via rtm_errno.
339 	 */
340 	rtm = (rt_msghdr_t *)mp->b_rptr;
341 	if ((mp->b_wptr - mp->b_rptr) != rtm->rtm_msglen) {
342 		error = EINVAL;
343 		goto done;
344 	}
345 	if (rtm->rtm_version != RTM_VERSION) {
346 		error = EPROTONOSUPPORT;
347 		goto done;
348 	}
349 
350 	/* Only allow RTM_GET or RTM_RESOLVE for unprivileged process */
351 	if (rtm->rtm_type != RTM_GET &&
352 	    rtm->rtm_type != RTM_RESOLVE &&
353 	    (ioc_cr == NULL ||
354 	    secpolicy_net_config(ioc_cr, B_FALSE) != 0)) {
355 		error = EPERM;
356 		goto done;
357 	}
358 
359 	found_addrs = rts_getaddrs(rtm, &dst_addr_v6, &gw_addr_v6, &net_mask_v6,
360 	    &author_v6, &if_addr_v6, &src_addr_v6, &index, &src_index, &af,
361 	    &rtsecattr, &error);
362 
363 	if (error != 0)
364 		goto done;
365 
366 	if ((found_addrs & RTA_DST) == 0) {
367 		error = EINVAL;
368 		goto done;
369 	}
370 
371 	/*
372 	 * Based on the address family of the destination address, determine
373 	 * the destination, gateway and netmask and return the appropriate error
374 	 * if an unknown address family was specified (following the errno
375 	 * values that 4.4BSD-Lite2 returns.)
376 	 */
377 	switch (af) {
378 	case AF_INET:
379 		/*
380 		 * RTA_SRCIFP is supported for interface route only.
381 		 * Thus a gateway route with srcifindex is rejected,
382 		 * except if it's a request to add reverse tunnel
383 		 * route.
384 		 */
385 		if ((rtm->rtm_flags & RTF_GATEWAY) &&
386 		    (found_addrs & RTA_SRCIFP) &&
387 		    !(found_addrs & RTA_SRC)) {
388 			error = EINVAL;
389 			goto done;
390 		}
391 		IN6_V4MAPPED_TO_IPADDR(&dst_addr_v6, dst_addr);
392 		IN6_V4MAPPED_TO_IPADDR(&src_addr_v6, src_addr);
393 		IN6_V4MAPPED_TO_IPADDR(&gw_addr_v6, gw_addr);
394 		if (((found_addrs & RTA_NETMASK) == 0) ||
395 		    (rtm->rtm_flags & RTF_HOST))
396 			net_mask = IP_HOST_MASK;
397 		else
398 			IN6_V4MAPPED_TO_IPADDR(&net_mask_v6, net_mask);
399 		break;
400 	case AF_INET6:
401 		/*
402 		 * RTA_SRCIFP is not a valid flag for IPv6 routes.
403 		 */
404 		if (found_addrs & RTA_SRCIFP) {
405 			error = EINVAL;
406 			goto done;
407 		}
408 		if (((found_addrs & RTA_NETMASK) == 0) ||
409 		    (rtm->rtm_flags & RTF_HOST))
410 			net_mask_v6 = ipv6_all_ones;
411 		break;
412 	default:
413 		/*
414 		 * These errno values are meant to be compatible with
415 		 * 4.4BSD-Lite2 for the given message types.
416 		 */
417 		switch (rtm->rtm_type) {
418 		case RTM_ADD:
419 		case RTM_DELETE:
420 			error = ESRCH;
421 			goto done;
422 		case RTM_GET:
423 		case RTM_CHANGE:
424 			error = EAFNOSUPPORT;
425 			goto done;
426 		default:
427 			error = EOPNOTSUPP;
428 			goto done;
429 		}
430 	}
431 
432 	/*
433 	 * At this point, the address family must be something known.
434 	 */
435 	ASSERT(af == AF_INET || af == AF_INET6);
436 
437 	if (index != 0) {
438 		ill_t   *ill;
439 
440 		/*
441 		 * IPC must be refheld somewhere in ip_wput_nondata or
442 		 * ip_wput_ioctl etc... and cleaned up if ioctl is killed.
443 		 * If ILL_CHANGING the request is queued in the ipsq.
444 		 */
445 		ill = ill_lookup_on_ifindex(index, af == AF_INET6,
446 		    CONNP_TO_WQ(connp), ioc_mp, ip_rts_request_retry, &error);
447 		if (ill == NULL) {
448 			if (error != EINPROGRESS)
449 				error = EINVAL;
450 			goto done;
451 		}
452 
453 		ipif = ipif_get_next_ipif(NULL, ill);
454 		ill_refrele(ill);
455 		/*
456 		 * If this is replacement ipif, prevent a route from
457 		 * being added.
458 		 */
459 		if (ipif != NULL && ipif->ipif_replace_zero) {
460 			error = ENETDOWN;
461 			goto done;
462 		}
463 		match_flags |= MATCH_IRE_ILL;
464 	}
465 
466 	/* RTA_SRCIFP is unsupported on AF_INET6. */
467 	if (af == AF_INET && src_index != 0) {
468 		ill_t   *ill;
469 
470 		/* If ILL_CHANGING the request is queued in the ipsq. */
471 		ill = ill_lookup_on_ifindex(src_index, B_FALSE,
472 		    CONNP_TO_WQ(connp), ioc_mp, ip_rts_request_retry, &error);
473 		if (ill == NULL) {
474 			if (error != EINPROGRESS)
475 				error = EINVAL;
476 			goto done;
477 		}
478 
479 		src_ipif = ipif_get_next_ipif(NULL, ill);
480 		ill_refrele(ill);
481 	}
482 	/*
483 	 * If a netmask was supplied in the message, then subsequent route
484 	 * lookups will attempt to match on the netmask as well.
485 	 */
486 	if ((found_addrs & RTA_NETMASK) != 0)
487 		match_flags |= MATCH_IRE_MASK;
488 
489 	/*
490 	 * We only process any passed-in route security attributes for
491 	 * either RTM_ADD or RTM_CHANGE message; We overload them
492 	 * to do an RTM_GET as a different label; ignore otherwise.
493 	 */
494 	if (rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_CHANGE ||
495 	    rtm->rtm_type == RTM_GET) {
496 		ASSERT(rtsecattr.rtsa_cnt <= TSOL_RTSA_REQUEST_MAX);
497 		if (rtsecattr.rtsa_cnt > 0)
498 			rtsap = &rtsecattr.rtsa_attr[0];
499 	}
500 
501 	switch (rtm->rtm_type) {
502 	case RTM_ADD:
503 		/* if we are adding a route, gateway is a must */
504 		if ((found_addrs & RTA_GATEWAY) == 0) {
505 			error = EINVAL;
506 			goto done;
507 		}
508 
509 		/* Multirouting does not support net routes. */
510 		if ((rtm->rtm_flags & (RTF_MULTIRT | RTF_HOST)) ==
511 		    RTF_MULTIRT) {
512 			error = EADDRNOTAVAIL;
513 			goto done;
514 		}
515 
516 		/*
517 		 * Multirouting and user-specified source addresses
518 		 * do not support interface based routing.
519 		 * Assigning a source address to an interface based
520 		 * route is achievable by plumbing a new ipif and
521 		 * setting up the interface route via this ipif,
522 		 * though.
523 		 */
524 		if (rtm->rtm_flags & (RTF_MULTIRT | RTF_SETSRC)) {
525 			if ((rtm->rtm_flags & RTF_GATEWAY) == 0) {
526 				error = EADDRNOTAVAIL;
527 				goto done;
528 			}
529 		}
530 
531 		switch (af) {
532 		case AF_INET:
533 			if (src_addr != INADDR_ANY) {
534 				/*
535 				 * If there is a source address, but
536 				 * no RTF_SETSRC modifier, setup a MobileIP
537 				 * reverse tunnel.
538 				 */
539 				if ((rtm->rtm_flags & RTF_SETSRC) == 0) {
540 					error = ip_mrtun_rt_add(src_addr,
541 					    rtm->rtm_flags, ipif,
542 					    src_ipif, &ire, CONNP_TO_WQ(connp),
543 					    ioc_mp, ip_rts_request_retry);
544 					break;
545 				}
546 				/*
547 				 * The RTF_SETSRC flag is present, check that
548 				 * the supplied src address is not the loopback
549 				 * address. This would produce martian packets.
550 				 */
551 				if (src_addr == htonl(INADDR_LOOPBACK)) {
552 					error = EINVAL;
553 					goto done;
554 				}
555 				/*
556 				 * Also check that the supplied address is a
557 				 * valid, local one.
558 				 */
559 				tmp_ipif = ipif_lookup_addr(src_addr, NULL,
560 				    ALL_ZONES, CONNP_TO_WQ(connp), ioc_mp,
561 				    ip_rts_request_retry, &error);
562 				if (tmp_ipif == NULL) {
563 					if (error != EINPROGRESS)
564 						error = EADDRNOTAVAIL;
565 					goto done;
566 				}
567 				if (!(tmp_ipif->ipif_flags & IPIF_UP) ||
568 				    (tmp_ipif->ipif_flags &
569 				    (IPIF_NOLOCAL | IPIF_ANYCAST))) {
570 					error = EINVAL;
571 					goto done;
572 				}
573 			} else {
574 				/*
575 				 * The RTF_SETSRC modifier must be associated
576 				 * to a non-null source address.
577 				 */
578 				if (rtm->rtm_flags & RTF_SETSRC) {
579 					error = EINVAL;
580 					goto done;
581 				}
582 			}
583 
584 			error = ip_rt_add(dst_addr, net_mask, gw_addr, src_addr,
585 			    rtm->rtm_flags, ipif, src_ipif, &ire, B_FALSE,
586 			    CONNP_TO_WQ(connp), ioc_mp, ip_rts_request_retry,
587 			    rtsap);
588 			if (ipif != NULL)
589 				ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
590 			break;
591 		case AF_INET6:
592 			if (!IN6_IS_ADDR_UNSPECIFIED(&src_addr_v6)) {
593 				/*
594 				 * If there is a source address, but
595 				 * no RTF_SETSRC modifier, reject, as
596 				 * MobileIP IPv6 reverse tunnels are
597 				 * not supported.
598 				 */
599 				if ((rtm->rtm_flags & RTF_SETSRC) == 0) {
600 					error = EINVAL;
601 					goto done;
602 				}
603 				/*
604 				 * The RTF_SETSRC flag is present, check that
605 				 * the supplied src address is not the loopback
606 				 * address. This would produce martian packets.
607 				 */
608 				if (IN6_IS_ADDR_LOOPBACK(&src_addr_v6)) {
609 					error = EINVAL;
610 					goto done;
611 				}
612 				/*
613 				 * Also check that the supplied address is a
614 				 * valid, local one.
615 				 */
616 				tmp_ipif = ipif_lookup_addr_v6(&src_addr_v6,
617 				    NULL, ALL_ZONES, CONNP_TO_WQ(connp), ioc_mp,
618 				    ip_rts_request_retry, &error);
619 				if (tmp_ipif == NULL) {
620 					if (error != EINPROGRESS)
621 						error = EADDRNOTAVAIL;
622 					goto done;
623 				}
624 
625 				if (!(tmp_ipif->ipif_flags & IPIF_UP) ||
626 				    (tmp_ipif->ipif_flags &
627 				    (IPIF_NOLOCAL | IPIF_ANYCAST))) {
628 					error = EINVAL;
629 					goto done;
630 				}
631 
632 				error = ip_rt_add_v6(&dst_addr_v6, &net_mask_v6,
633 				    &gw_addr_v6, &src_addr_v6, rtm->rtm_flags,
634 				    ipif, &ire, CONNP_TO_WQ(connp), ioc_mp,
635 				    ip_rts_request_retry, rtsap);
636 				break;
637 			}
638 			/*
639 			 * The RTF_SETSRC modifier must be associated
640 			 * to a non-null source address.
641 			 */
642 			if (rtm->rtm_flags & RTF_SETSRC) {
643 				error = EINVAL;
644 				goto done;
645 			}
646 			error = ip_rt_add_v6(&dst_addr_v6, &net_mask_v6,
647 			    &gw_addr_v6, NULL, rtm->rtm_flags,
648 			    ipif, &ire, CONNP_TO_WQ(connp), ioc_mp,
649 			    ip_rts_request_retry, rtsap);
650 			if (ipif != NULL)
651 				ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
652 			break;
653 		}
654 		if (error != 0)
655 			goto done;
656 		ASSERT(ire != NULL);
657 		rts_setmetrics(ire, rtm->rtm_inits, &rtm->rtm_rmx);
658 		break;
659 	case RTM_DELETE:
660 		/* if we are deleting a route, gateway is a must */
661 		if ((found_addrs & RTA_GATEWAY) == 0) {
662 			error = EINVAL;
663 			goto done;
664 		}
665 		/*
666 		 * The RTF_SETSRC modifier does not make sense
667 		 * when deleting a route.
668 		 */
669 		if (rtm->rtm_flags & RTF_SETSRC) {
670 			error = EINVAL;
671 			goto done;
672 		}
673 
674 		switch (af) {
675 		case AF_INET:
676 			/*
677 			 * If there is a source address, delete
678 			 * a MobileIP reverse tunnel.
679 			 */
680 			if (src_addr != INADDR_ANY) {
681 				error = ip_mrtun_rt_delete(src_addr,
682 				    src_ipif);
683 				break;
684 			}
685 			error = ip_rt_delete(dst_addr, net_mask, gw_addr,
686 			    found_addrs, rtm->rtm_flags, ipif, src_ipif,
687 			    B_FALSE, CONNP_TO_WQ(connp), ioc_mp,
688 			    ip_rts_request_retry);
689 			break;
690 		case AF_INET6:
691 			error = ip_rt_delete_v6(&dst_addr_v6, &net_mask_v6,
692 			    &gw_addr_v6, found_addrs, rtm->rtm_flags, ipif,
693 			    CONNP_TO_WQ(connp), ioc_mp, ip_rts_request_retry);
694 			break;
695 		}
696 		break;
697 	case RTM_GET:
698 	case RTM_CHANGE:
699 		/*
700 		 * In the case of RTM_GET, the forwarding table should be
701 		 * searched recursively with default being matched if the
702 		 * specific route doesn't exist.  Also, if a gateway was
703 		 * specified then the gateway address must also be matched.
704 		 *
705 		 * In the case of RTM_CHANGE, the gateway address (if supplied)
706 		 * is the new gateway address so matching on the gateway address
707 		 * is not done.  This can lead to ambiguity when looking up the
708 		 * route to change as usually only the destination (and netmask,
709 		 * if supplied) is used for the lookup.  However if a RTA_IFP
710 		 * sockaddr is also supplied, it can disambiguate which route to
711 		 * change provided the ambigous routes are tied to distinct
712 		 * ill's (or interface indices).  If the routes are not tied to
713 		 * any particular interfaces (for example, with traditional
714 		 * gateway routes), then a RTA_IFP sockaddr will be of no use as
715 		 * it won't match any such routes.
716 		 * RTA_SRC is not supported for RTM_GET and RTM_CHANGE,
717 		 * except when RTM_CHANGE is combined to RTF_SETSRC.
718 		 */
719 		if (((found_addrs & RTA_SRC) != 0) &&
720 		    ((rtm->rtm_type == RTM_GET) ||
721 		    !(rtm->rtm_flags & RTF_SETSRC))) {
722 			error = EOPNOTSUPP;
723 			goto done;
724 		}
725 
726 		if (rtm->rtm_type == RTM_GET) {
727 			match_flags |=
728 			    (MATCH_IRE_DEFAULT | MATCH_IRE_RECURSIVE |
729 			    MATCH_IRE_SECATTR);
730 			match_flags_local |= MATCH_IRE_SECATTR;
731 			if ((found_addrs & RTA_GATEWAY) != 0)
732 				match_flags |= MATCH_IRE_GW;
733 			if (ioc_cr)
734 				tsl = crgetlabel(ioc_cr);
735 			if (rtsap != NULL) {
736 				if (rtsa_validate(rtsap) != 0) {
737 					error = EINVAL;
738 					goto done;
739 				}
740 				if (tsl != NULL &&
741 				    crgetzoneid(ioc_cr) != GLOBAL_ZONEID &&
742 				    (tsl->tsl_doi != rtsap->rtsa_doi ||
743 				    !bldominates(&tsl->tsl_label,
744 				    &rtsap->rtsa_slrange.lower_bound))) {
745 					error = EPERM;
746 					goto done;
747 				}
748 				tsl = labelalloc(
749 				    &rtsap->rtsa_slrange.lower_bound,
750 				    rtsap->rtsa_doi, KM_NOSLEEP);
751 			}
752 		}
753 		if (rtm->rtm_type == RTM_CHANGE) {
754 			if ((found_addrs & RTA_GATEWAY) &&
755 			    (rtm->rtm_flags & RTF_SETSRC)) {
756 				/*
757 				 * Do not want to change the gateway,
758 				 * but rather the source address.
759 				 */
760 				match_flags |= MATCH_IRE_GW;
761 			}
762 		}
763 
764 		/*
765 		 * If the netmask is all ones (either as supplied or as derived
766 		 * above), then first check for an IRE_LOOPBACK or
767 		 * IRE_LOCAL entry.
768 		 *
769 		 * If we didn't check for or find an IRE_LOOPBACK or IRE_LOCAL
770 		 * entry, then look in the forwarding table.
771 		 */
772 		switch (af) {
773 		case AF_INET:
774 			if (net_mask == IP_HOST_MASK) {
775 				ire = ire_ctable_lookup(dst_addr, gw_addr,
776 				    IRE_LOCAL | IRE_LOOPBACK, NULL, zoneid,
777 				    tsl, match_flags_local);
778 				/*
779 				 * If we found an IRE_LOCAL, make sure
780 				 * it is one that would be used by this
781 				 * zone to send packets.
782 				 */
783 				if (ire != NULL &&
784 				    ire->ire_type == IRE_LOCAL &&
785 				    ip_restrict_interzone_loopback &&
786 				    !ire_local_ok_across_zones(ire,
787 				    zoneid, &dst_addr, tsl)) {
788 					ire_refrele(ire);
789 					ire = NULL;
790 				}
791 			}
792 			if (ire == NULL) {
793 				ire = ire_ftable_lookup(dst_addr, net_mask,
794 				    gw_addr, 0, ipif, &sire, zoneid, 0,
795 				    tsl, match_flags);
796 			}
797 			break;
798 		case AF_INET6:
799 			if (IN6_ARE_ADDR_EQUAL(&net_mask_v6, &ipv6_all_ones)) {
800 				ire = ire_ctable_lookup_v6(&dst_addr_v6,
801 				    &gw_addr_v6, IRE_LOCAL | IRE_LOOPBACK, NULL,
802 				    zoneid, tsl, match_flags_local);
803 				/*
804 				 * If we found an IRE_LOCAL, make sure
805 				 * it is one that would be used by this
806 				 * zone to send packets.
807 				 */
808 				if (ire != NULL &&
809 				    ire->ire_type == IRE_LOCAL &&
810 				    ip_restrict_interzone_loopback &&
811 				    !ire_local_ok_across_zones(ire,
812 				    zoneid, (void *)&dst_addr_v6, tsl)) {
813 					ire_refrele(ire);
814 					ire = NULL;
815 				}
816 			}
817 			if (ire == NULL) {
818 				ire = ire_ftable_lookup_v6(&dst_addr_v6,
819 				    &net_mask_v6, &gw_addr_v6, 0, ipif, &sire,
820 				    zoneid, 0, tsl, match_flags);
821 			}
822 			break;
823 		}
824 		if (tsl != NULL && tsl != crgetlabel(ioc_cr))
825 			label_rele(tsl);
826 
827 		if (ire == NULL) {
828 			error = ESRCH;
829 			goto done;
830 		}
831 		/* we know the IRE before we come here */
832 		switch (rtm->rtm_type) {
833 		case RTM_GET:
834 			mp1 = rts_rtmget(mp, ire, sire, af);
835 			if (mp1 == NULL) {
836 				error = ENOBUFS;
837 				goto done;
838 			}
839 			freemsg(mp);
840 			mp = mp1;
841 			rtm = (rt_msghdr_t *)mp->b_rptr;
842 			break;
843 		case RTM_CHANGE:
844 			/*
845 			 * Do not allow to the multirouting state of a route
846 			 * to be changed. This aims to prevent undesirable
847 			 * stages where both multirt and non-multirt routes
848 			 * for the same destination are declared.
849 			 */
850 			if ((ire->ire_flags & RTF_MULTIRT) !=
851 			    (rtm->rtm_flags & RTF_MULTIRT)) {
852 				error = EINVAL;
853 				goto done;
854 			}
855 			/*
856 			 * Note that we do not need to do
857 			 * ire_flush_cache_*(IRE_FLUSH_ADD) as a change
858 			 * in metrics or gateway will not affect existing
859 			 * routes since it does not create a more specific
860 			 * route.
861 			 */
862 			switch (af) {
863 			case AF_INET:
864 				ire_flush_cache_v4(ire, IRE_FLUSH_DELETE);
865 				if ((found_addrs & RTA_GATEWAY) != 0 &&
866 				    (ire->ire_gateway_addr != gw_addr)) {
867 					ire->ire_gateway_addr = gw_addr;
868 				}
869 
870 				if (rtsap != NULL) {
871 					ga.ga_af = AF_INET;
872 					IN6_IPADDR_TO_V4MAPPED(
873 					    ire->ire_gateway_addr, &ga.ga_addr);
874 
875 					gcgrp = gcgrp_lookup(&ga, B_TRUE);
876 					if (gcgrp == NULL) {
877 						error = ENOMEM;
878 						goto done;
879 					}
880 				}
881 
882 				if ((found_addrs & RTA_SRC) != 0 &&
883 				    (rtm->rtm_flags & RTF_SETSRC) != 0 &&
884 				    (ire->ire_src_addr != src_addr)) {
885 
886 					if (src_addr != INADDR_ANY) {
887 						/*
888 						 * The RTF_SETSRC flag is
889 						 * present, check that the
890 						 * supplied src address is not
891 						 * the loopback address. This
892 						 * would produce martian
893 						 * packets.
894 						 */
895 						if (src_addr ==
896 						    htonl(INADDR_LOOPBACK)) {
897 							error = EINVAL;
898 							goto done;
899 						}
900 						/*
901 						 * Also check that the the
902 						 * supplied addr is a valid
903 						 * local address.
904 						 */
905 						tmp_ipif = ipif_lookup_addr(
906 						    src_addr, NULL, ALL_ZONES,
907 						    CONNP_TO_WQ(connp), ioc_mp,
908 						    ip_rts_request_retry,
909 						    &error);
910 						if (tmp_ipif == NULL) {
911 							error = (error ==
912 							    EINPROGRESS) ?
913 							    error :
914 							    EADDRNOTAVAIL;
915 							goto done;
916 						}
917 
918 						if (!(tmp_ipif->ipif_flags &
919 						    IPIF_UP) ||
920 						    (tmp_ipif->ipif_flags &
921 						    (IPIF_NOLOCAL |
922 						    IPIF_ANYCAST))) {
923 							error = EINVAL;
924 							goto done;
925 						}
926 						ire->ire_flags |= RTF_SETSRC;
927 					} else {
928 						ire->ire_flags &= ~RTF_SETSRC;
929 					}
930 					ire->ire_src_addr = src_addr;
931 				}
932 				break;
933 			case AF_INET6:
934 				ire_flush_cache_v6(ire, IRE_FLUSH_DELETE);
935 				mutex_enter(&ire->ire_lock);
936 				if ((found_addrs & RTA_GATEWAY) != 0 &&
937 				    !IN6_ARE_ADDR_EQUAL(
938 				    &ire->ire_gateway_addr_v6, &gw_addr_v6)) {
939 					ire->ire_gateway_addr_v6 = gw_addr_v6;
940 				}
941 
942 				if (rtsap != NULL) {
943 					ga.ga_af = AF_INET6;
944 					ga.ga_addr = ire->ire_gateway_addr_v6;
945 
946 					gcgrp = gcgrp_lookup(&ga, B_TRUE);
947 					if (gcgrp == NULL) {
948 						error = ENOMEM;
949 						goto done;
950 					}
951 				}
952 
953 				if ((found_addrs & RTA_SRC) != 0 &&
954 				    (rtm->rtm_flags & RTF_SETSRC) != 0 &&
955 				    !IN6_ARE_ADDR_EQUAL(
956 					&ire->ire_src_addr_v6, &src_addr_v6)) {
957 
958 					if (!IN6_IS_ADDR_UNSPECIFIED(
959 					    &src_addr_v6)) {
960 						/*
961 						 * The RTF_SETSRC flag is
962 						 * present, check that the
963 						 * supplied src address is not
964 						 * the loopback address. This
965 						 * would produce martian
966 						 * packets.
967 						 */
968 						if (IN6_IS_ADDR_LOOPBACK(
969 						    &src_addr_v6)) {
970 							mutex_exit(
971 							    &ire->ire_lock);
972 							error = EINVAL;
973 							goto done;
974 						}
975 						/*
976 						 * Also check that the the
977 						 * supplied addr is a valid
978 						 * local address.
979 						 */
980 						tmp_ipif = ipif_lookup_addr_v6(
981 						    &src_addr_v6, NULL,
982 						    ALL_ZONES,
983 						    CONNP_TO_WQ(connp), ioc_mp,
984 						    ip_rts_request_retry,
985 						    &error);
986 						if (tmp_ipif == NULL) {
987 							mutex_exit(
988 							    &ire->ire_lock);
989 							error = (error ==
990 							    EINPROGRESS) ?
991 							    error :
992 							    EADDRNOTAVAIL;
993 							goto done;
994 						}
995 						if (!(tmp_ipif->ipif_flags &
996 						    IPIF_UP) ||
997 						    (tmp_ipif->ipif_flags &
998 						    (IPIF_NOLOCAL |
999 						    IPIF_ANYCAST))) {
1000 							mutex_exit(
1001 							    &ire->ire_lock);
1002 							error = EINVAL;
1003 							goto done;
1004 						}
1005 						ire->ire_flags |= RTF_SETSRC;
1006 					} else {
1007 						ire->ire_flags &= ~RTF_SETSRC;
1008 					}
1009 					ire->ire_src_addr_v6 = src_addr_v6;
1010 				}
1011 				mutex_exit(&ire->ire_lock);
1012 				break;
1013 			}
1014 
1015 			if (rtsap != NULL) {
1016 				in_addr_t ga_addr4;
1017 
1018 				ASSERT(gcgrp != NULL);
1019 
1020 				/*
1021 				 * Create and add the security attribute to
1022 				 * prefix IRE; it will add a reference to the
1023 				 * group upon allocating a new entry.  If it
1024 				 * finds an already-existing entry for the
1025 				 * security attribute, it simply returns it
1026 				 * and no new group reference is made.
1027 				 */
1028 				gc = gc_create(rtsap, gcgrp, &gcgrp_xtraref);
1029 				if (gc == NULL ||
1030 				    (error = tsol_ire_init_gwattr(ire,
1031 				    ire->ire_ipversion, gc, NULL)) != 0) {
1032 					if (gc != NULL) {
1033 						GC_REFRELE(gc);
1034 					} else {
1035 						/* gc_create failed */
1036 						error = ENOMEM;
1037 					}
1038 					goto done;
1039 				}
1040 
1041 				/*
1042 				 * Now delete any existing gateway IRE caches
1043 				 * as well as all caches using the gateway,
1044 				 * and allow them to be created on demand
1045 				 * through ip_newroute{_v6}.
1046 				 */
1047 				IN6_V4MAPPED_TO_IPADDR(&ga.ga_addr, ga_addr4);
1048 				if (af == AF_INET) {
1049 					ire_clookup_delete_cache_gw(
1050 					    ga_addr4, ALL_ZONES);
1051 				} else {
1052 					ire_clookup_delete_cache_gw_v6(
1053 					    &ga.ga_addr, ALL_ZONES);
1054 				}
1055 			}
1056 			rts_setmetrics(ire, rtm->rtm_inits, &rtm->rtm_rmx);
1057 			break;
1058 		}
1059 		break;
1060 	default:
1061 		error = EOPNOTSUPP;
1062 		break;
1063 	}
1064 done:
1065 	if (ire != NULL)
1066 		ire_refrele(ire);
1067 	if (sire != NULL)
1068 		ire_refrele(sire);
1069 	if (ipif != NULL)
1070 		ipif_refrele(ipif);
1071 	if (src_ipif != NULL)
1072 		ipif_refrele(src_ipif);
1073 	if (tmp_ipif != NULL)
1074 		ipif_refrele(tmp_ipif);
1075 
1076 	if (gcgrp_xtraref)
1077 		GCGRP_REFRELE(gcgrp);
1078 
1079 	if (error == EINPROGRESS)
1080 		return (error);
1081 	if (rtm != NULL) {
1082 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
1083 		if (error != 0) {
1084 			rtm->rtm_errno = error;
1085 			/* Send error ACK */
1086 			ip1dbg(("ip_rts_request: error %d\n", error));
1087 		} else {
1088 			rtm->rtm_flags |= RTF_DONE;
1089 			/* OK ACK already set up by caller except this */
1090 			ip2dbg(("ip_rts_request: OK ACK\n"));
1091 		}
1092 		rts_queue_input(mp, q, af);
1093 	}
1094 	iocp->ioc_error = error;
1095 	ioc_mp->b_datap->db_type = M_IOCACK;
1096 	if (iocp->ioc_error != 0)
1097 		iocp->ioc_count = 0;
1098 	qreply(q, ioc_mp);
1099 	/* conn was refheld in ip_wput_ioctl. */
1100 	CONN_OPER_PENDING_DONE(connp);
1101 
1102 	return (error);
1103 }
1104 
1105 /*
1106  * Build a reply to the RTM_GET request contained in the given message block
1107  * using the retrieved IRE of the destination address, the parent IRE (if it
1108  * exists) and the address family.
1109  *
1110  * Returns a pointer to a message block containing the reply if successful,
1111  * otherwise NULL is returned.
1112  */
1113 static mblk_t *
1114 rts_rtmget(mblk_t *mp, ire_t *ire, ire_t *sire, sa_family_t af)
1115 {
1116 	rt_msghdr_t	*rtm;
1117 	rt_msghdr_t	*new_rtm;
1118 	mblk_t		*new_mp;
1119 	int		rtm_addrs;
1120 	int		rtm_flags;
1121 	in6_addr_t	gw_addr_v6;
1122 	tsol_ire_gw_secattr_t *attrp = NULL;
1123 	tsol_gc_t	*gc = NULL;
1124 	tsol_gcgrp_t	*gcgrp = NULL;
1125 	int		sacnt = 0;
1126 
1127 	ASSERT(ire->ire_ipif != NULL);
1128 	rtm = (rt_msghdr_t *)mp->b_rptr;
1129 
1130 	if (sire != NULL && sire->ire_gw_secattr != NULL)
1131 		attrp = sire->ire_gw_secattr;
1132 	else if (ire->ire_gw_secattr != NULL)
1133 		attrp = ire->ire_gw_secattr;
1134 
1135 	if (attrp != NULL) {
1136 		mutex_enter(&attrp->igsa_lock);
1137 		if ((gc = attrp->igsa_gc) != NULL) {
1138 			gcgrp = gc->gc_grp;
1139 			ASSERT(gcgrp != NULL);
1140 			rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
1141 			sacnt = 1;
1142 		} else if ((gcgrp = attrp->igsa_gcgrp) != NULL) {
1143 			rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
1144 			gc = gcgrp->gcgrp_head;
1145 			sacnt = gcgrp->gcgrp_count;
1146 		}
1147 		mutex_exit(&attrp->igsa_lock);
1148 
1149 		/* do nothing if there's no gc to report */
1150 		if (gc == NULL) {
1151 			ASSERT(sacnt == 0);
1152 			if (gcgrp != NULL) {
1153 				/* we might as well drop the lock now */
1154 				rw_exit(&gcgrp->gcgrp_rwlock);
1155 				gcgrp = NULL;
1156 			}
1157 			attrp = NULL;
1158 		}
1159 
1160 		ASSERT(gc == NULL || (gcgrp != NULL &&
1161 		    RW_LOCK_HELD(&gcgrp->gcgrp_rwlock)));
1162 	}
1163 	ASSERT(sacnt == 0 || gc != NULL);
1164 
1165 	/*
1166 	 * Always return RTA_DST, RTA_GATEWAY and RTA_NETMASK.
1167 	 *
1168 	 * The 4.4BSD-Lite2 code (net/rtsock.c) returns both
1169 	 * RTA_IFP and RTA_IFA if either is defined, and also
1170 	 * returns RTA_BRD if the appropriate interface is
1171 	 * point-to-point.
1172 	 */
1173 	rtm_addrs = (RTA_DST | RTA_GATEWAY | RTA_NETMASK);
1174 	if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
1175 		rtm_addrs |= (RTA_IFP | RTA_IFA);
1176 		if (ire->ire_ipif->ipif_flags & IPIF_POINTOPOINT)
1177 			rtm_addrs |= RTA_BRD;
1178 	}
1179 
1180 	new_mp = rts_alloc_msg(RTM_GET, rtm_addrs, af, sacnt);
1181 	if (new_mp == NULL) {
1182 		if (gcgrp != NULL)
1183 			rw_exit(&gcgrp->gcgrp_rwlock);
1184 		return (NULL);
1185 	}
1186 
1187 	/*
1188 	 * We set the destination address, gateway address,
1189 	 * netmask and flags in the RTM_GET response depending
1190 	 * on whether we found a parent IRE or not.
1191 	 * In particular, if we did find a parent IRE during the
1192 	 * recursive search, use that IRE's gateway address.
1193 	 * Otherwise, we use the IRE's source address for the
1194 	 * gateway address.
1195 	 */
1196 	ASSERT(af == AF_INET || af == AF_INET6);
1197 	switch (af) {
1198 	case AF_INET:
1199 		if (sire == NULL) {
1200 			rtm_flags = ire->ire_flags;
1201 			rts_fill_msg(RTM_GET, rtm_addrs, ire->ire_addr,
1202 			    ire->ire_mask, ire->ire_src_addr, ire->ire_src_addr,
1203 			    ire->ire_ipif->ipif_pp_dst_addr, 0, ire->ire_ipif,
1204 			    new_mp, sacnt, gc);
1205 		} else {
1206 			if (sire->ire_flags & RTF_SETSRC)
1207 				rtm_addrs |= RTA_SRC;
1208 
1209 			rtm_flags = sire->ire_flags;
1210 			rts_fill_msg(RTM_GET, rtm_addrs, sire->ire_addr,
1211 			    sire->ire_mask, sire->ire_gateway_addr,
1212 			    (sire->ire_flags & RTF_SETSRC) ?
1213 				sire->ire_src_addr : ire->ire_src_addr,
1214 			    ire->ire_ipif->ipif_pp_dst_addr,
1215 			    0, ire->ire_ipif, new_mp, sacnt, gc);
1216 		}
1217 		break;
1218 	case AF_INET6:
1219 		if (sire == NULL) {
1220 			rtm_flags = ire->ire_flags;
1221 			rts_fill_msg_v6(RTM_GET, rtm_addrs, &ire->ire_addr_v6,
1222 			    &ire->ire_mask_v6, &ire->ire_src_addr_v6,
1223 			    &ire->ire_src_addr_v6,
1224 			    &ire->ire_ipif->ipif_v6pp_dst_addr,
1225 			    &ipv6_all_zeros, ire->ire_ipif, new_mp,
1226 			    sacnt, gc);
1227 		} else {
1228 			if (sire->ire_flags & RTF_SETSRC)
1229 				rtm_addrs |= RTA_SRC;
1230 
1231 			rtm_flags = sire->ire_flags;
1232 			mutex_enter(&sire->ire_lock);
1233 			gw_addr_v6 = sire->ire_gateway_addr_v6;
1234 			mutex_exit(&sire->ire_lock);
1235 			rts_fill_msg_v6(RTM_GET, rtm_addrs, &sire->ire_addr_v6,
1236 			    &sire->ire_mask_v6, &gw_addr_v6,
1237 			    (sire->ire_flags & RTF_SETSRC) ?
1238 				&sire->ire_src_addr_v6 : &ire->ire_src_addr_v6,
1239 			    &ire->ire_ipif->ipif_v6pp_dst_addr, &ipv6_all_zeros,
1240 			    ire->ire_ipif, new_mp, sacnt, gc);
1241 		}
1242 		break;
1243 	}
1244 
1245 	if (gcgrp != NULL)
1246 		rw_exit(&gcgrp->gcgrp_rwlock);
1247 
1248 	new_rtm = (rt_msghdr_t *)new_mp->b_rptr;
1249 
1250 	/*
1251 	 * The rtm_msglen, rtm_version and rtm_type fields in
1252 	 * RTM_GET response are filled in by rts_fill_msg.
1253 	 *
1254 	 * rtm_addrs and rtm_flags are filled in based on what
1255 	 * was requested and the state of the IREs looked up
1256 	 * above.
1257 	 *
1258 	 * rtm_inits and rtm_rmx are filled in with metrics
1259 	 * based on whether a parent IRE was found or not.
1260 	 *
1261 	 * TODO: rtm_index and rtm_use should probably be
1262 	 * filled in with something resonable here and not just
1263 	 * copied from the request.
1264 	 */
1265 	new_rtm->rtm_index = rtm->rtm_index;
1266 	new_rtm->rtm_pid = rtm->rtm_pid;
1267 	new_rtm->rtm_seq = rtm->rtm_seq;
1268 	new_rtm->rtm_use = rtm->rtm_use;
1269 	new_rtm->rtm_addrs = rtm_addrs;
1270 	new_rtm->rtm_flags = rtm_flags;
1271 	if (sire == NULL)
1272 		new_rtm->rtm_inits = rts_getmetrics(ire, &new_rtm->rtm_rmx);
1273 	else
1274 		new_rtm->rtm_inits = rts_getmetrics(sire, &new_rtm->rtm_rmx);
1275 
1276 	return (new_mp);
1277 }
1278 
1279 /*
1280  * Fill the given if_data_t with interface statistics.
1281  */
1282 static void
1283 rts_getifdata(if_data_t *if_data, const ipif_t *ipif)
1284 {
1285 	if_data->ifi_type = ipif->ipif_type;	/* ethernet, tokenring, etc */
1286 	if_data->ifi_addrlen = 0;		/* media address length */
1287 	if_data->ifi_hdrlen = 0;		/* media header length */
1288 	if_data->ifi_mtu = ipif->ipif_mtu;	/* maximum transmission unit */
1289 	if_data->ifi_metric = ipif->ipif_metric; /* metric (external only) */
1290 	if_data->ifi_baudrate = 0;		/* linespeed */
1291 
1292 	if_data->ifi_ipackets = 0;		/* packets received on if */
1293 	if_data->ifi_ierrors = 0;		/* input errors on interface */
1294 	if_data->ifi_opackets = 0;		/* packets sent on interface */
1295 	if_data->ifi_oerrors = 0;		/* output errors on if */
1296 	if_data->ifi_collisions = 0;		/* collisions on csma if */
1297 	if_data->ifi_ibytes = 0;		/* total number received */
1298 	if_data->ifi_obytes = 0;		/* total number sent */
1299 	if_data->ifi_imcasts = 0;		/* multicast packets received */
1300 	if_data->ifi_omcasts = 0;		/* multicast packets sent */
1301 	if_data->ifi_iqdrops = 0;		/* dropped on input */
1302 	if_data->ifi_noproto = 0;		/* destined for unsupported */
1303 						/* protocol. */
1304 }
1305 
1306 /*
1307  * Set the metrics on a forwarding table route.
1308  */
1309 static void
1310 rts_setmetrics(ire_t *ire, uint_t which, rt_metrics_t *metrics)
1311 {
1312 	clock_t		rtt;
1313 	clock_t		rtt_sd;
1314 	ipif_t		*ipif;
1315 	ifrt_t		*ifrt;
1316 	mblk_t		*mp;
1317 	in6_addr_t	gw_addr_v6;
1318 
1319 	/*
1320 	 * Bypass obtaining the lock and searching ipif_saved_ire_mp in the
1321 	 * common case of no metrics.
1322 	 */
1323 	if (which == 0)
1324 		return;
1325 	ire->ire_uinfo.iulp_set = B_TRUE;
1326 
1327 	/*
1328 	 * iulp_rtt and iulp_rtt_sd are in milliseconds, but 4.4BSD-Lite2's
1329 	 * <net/route.h> says: rmx_rtt and rmx_rttvar are stored as
1330 	 * microseconds.
1331 	 */
1332 	if (which & RTV_RTT)
1333 		rtt = metrics->rmx_rtt / 1000;
1334 	if (which & RTV_RTTVAR)
1335 		rtt_sd = metrics->rmx_rttvar / 1000;
1336 
1337 	/*
1338 	 * Update the metrics in the IRE itself.
1339 	 */
1340 	mutex_enter(&ire->ire_lock);
1341 	if (which & RTV_MTU)
1342 		ire->ire_max_frag = metrics->rmx_mtu;
1343 	if (which & RTV_RTT)
1344 		ire->ire_uinfo.iulp_rtt = rtt;
1345 	if (which & RTV_SSTHRESH)
1346 		ire->ire_uinfo.iulp_ssthresh = metrics->rmx_ssthresh;
1347 	if (which & RTV_RTTVAR)
1348 		ire->ire_uinfo.iulp_rtt_sd = rtt_sd;
1349 	if (which & RTV_SPIPE)
1350 		ire->ire_uinfo.iulp_spipe = metrics->rmx_sendpipe;
1351 	if (which & RTV_RPIPE)
1352 		ire->ire_uinfo.iulp_rpipe = metrics->rmx_recvpipe;
1353 	mutex_exit(&ire->ire_lock);
1354 
1355 	/*
1356 	 * Search through the ifrt_t chain hanging off the IPIF in order to
1357 	 * reflect the metric change there.
1358 	 */
1359 	ipif = ire->ire_ipif;
1360 	if (ipif == NULL)
1361 		return;
1362 	ASSERT((ipif->ipif_isv6 && ire->ire_ipversion == IPV6_VERSION) ||
1363 	    ((!ipif->ipif_isv6 && ire->ire_ipversion == IPV4_VERSION)));
1364 	if (ipif->ipif_isv6) {
1365 		mutex_enter(&ire->ire_lock);
1366 		gw_addr_v6 = ire->ire_gateway_addr_v6;
1367 		mutex_exit(&ire->ire_lock);
1368 	}
1369 	mutex_enter(&ipif->ipif_saved_ire_lock);
1370 	for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
1371 		/*
1372 		 * On a given ipif, the triple of address, gateway and mask is
1373 		 * unique for each saved IRE (in the case of ordinary interface
1374 		 * routes, the gateway address is all-zeroes).
1375 		 */
1376 		ifrt = (ifrt_t *)mp->b_rptr;
1377 		if (ipif->ipif_isv6) {
1378 			if (!IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
1379 			    &ire->ire_addr_v6) ||
1380 			    !IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
1381 			    &gw_addr_v6) ||
1382 			    !IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
1383 			    &ire->ire_mask_v6))
1384 				continue;
1385 		} else {
1386 			if (ifrt->ifrt_addr != ire->ire_addr ||
1387 			    ifrt->ifrt_gateway_addr != ire->ire_gateway_addr ||
1388 			    ifrt->ifrt_mask != ire->ire_mask)
1389 				continue;
1390 		}
1391 		if (which & RTV_MTU)
1392 			ifrt->ifrt_max_frag = metrics->rmx_mtu;
1393 		if (which & RTV_RTT)
1394 			ifrt->ifrt_iulp_info.iulp_rtt = rtt;
1395 		if (which & RTV_SSTHRESH) {
1396 			ifrt->ifrt_iulp_info.iulp_ssthresh =
1397 			    metrics->rmx_ssthresh;
1398 		}
1399 		if (which & RTV_RTTVAR)
1400 			ifrt->ifrt_iulp_info.iulp_rtt_sd = metrics->rmx_rttvar;
1401 		if (which & RTV_SPIPE)
1402 			ifrt->ifrt_iulp_info.iulp_spipe = metrics->rmx_sendpipe;
1403 		if (which & RTV_RPIPE)
1404 			ifrt->ifrt_iulp_info.iulp_rpipe = metrics->rmx_recvpipe;
1405 		break;
1406 	}
1407 	mutex_exit(&ipif->ipif_saved_ire_lock);
1408 }
1409 
1410 /*
1411  * Get the metrics from a forwarding table route.
1412  */
1413 static int
1414 rts_getmetrics(ire_t *ire, rt_metrics_t *metrics)
1415 {
1416 	int	metrics_set = 0;
1417 
1418 	bzero(metrics, sizeof (rt_metrics_t));
1419 	/*
1420 	 * iulp_rtt and iulp_rtt_sd are in milliseconds, but 4.4BSD-Lite2's
1421 	 * <net/route.h> says: rmx_rtt and rmx_rttvar are stored as
1422 	 * microseconds.
1423 	 */
1424 	metrics->rmx_rtt = ire->ire_uinfo.iulp_rtt * 1000;
1425 	metrics_set |= RTV_RTT;
1426 	metrics->rmx_mtu = ire->ire_max_frag;
1427 	metrics_set |= RTV_MTU;
1428 	metrics->rmx_ssthresh = ire->ire_uinfo.iulp_ssthresh;
1429 	metrics_set |= RTV_SSTHRESH;
1430 	metrics->rmx_rttvar = ire->ire_uinfo.iulp_rtt_sd * 1000;
1431 	metrics_set |= RTV_RTTVAR;
1432 	metrics->rmx_sendpipe = ire->ire_uinfo.iulp_spipe;
1433 	metrics_set |= RTV_SPIPE;
1434 	metrics->rmx_recvpipe = ire->ire_uinfo.iulp_rpipe;
1435 	metrics_set |= RTV_RPIPE;
1436 	return (metrics_set);
1437 }
1438 
1439 /*
1440  * Takes a pointer to a routing message and extracts necessary info by looking
1441  * at the rtm->rtm_addrs bits and store the requested sockaddrs in the pointers
1442  * passed (all of which must be valid).
1443  *
1444  * The bitmask of sockaddrs actually found in the message is returned, or zero
1445  * is returned in the case of an error.
1446  */
1447 static int
1448 rts_getaddrs(rt_msghdr_t *rtm, in6_addr_t *dst_addrp, in6_addr_t *gw_addrp,
1449     in6_addr_t *net_maskp, in6_addr_t *authorp, in6_addr_t *if_addrp,
1450     in6_addr_t *in_src_addrp, ushort_t *indexp, ushort_t *src_indexp,
1451     sa_family_t *afp, tsol_rtsecattr_t *rtsecattr, int *error)
1452 {
1453 	struct sockaddr *sa;
1454 	int	i;
1455 	int	addr_bits;
1456 	int	length;
1457 	int	found_addrs = 0;
1458 	caddr_t	cp;
1459 	size_t	size;
1460 	struct sockaddr_dl *sdl;
1461 
1462 	*dst_addrp = ipv6_all_zeros;
1463 	*gw_addrp = ipv6_all_zeros;
1464 	*net_maskp = ipv6_all_zeros;
1465 	*authorp = ipv6_all_zeros;
1466 	*if_addrp = ipv6_all_zeros;
1467 	*in_src_addrp = ipv6_all_zeros;
1468 	*indexp = 0;
1469 	*src_indexp = 0;
1470 	*afp = AF_UNSPEC;
1471 	rtsecattr->rtsa_cnt = 0;
1472 	*error = 0;
1473 
1474 	/*
1475 	 * At present we handle only RTA_DST, RTA_GATEWAY, RTA_NETMASK, RTA_IFP,
1476 	 * RTA_IFA and RTA_AUTHOR.  The rest will be added as we need them.
1477 	 */
1478 	cp = (caddr_t)&rtm[1];
1479 	length = rtm->rtm_msglen;
1480 	for (i = 0; (i < RTA_NUMBITS) && ((cp - (caddr_t)rtm) < length); i++) {
1481 		/*
1482 		 * The address family we are working with starts out as
1483 		 * AF_UNSPEC, but is set to the one specified with the
1484 		 * destination address.
1485 		 *
1486 		 * If the "working" address family that has been set to
1487 		 * something other than AF_UNSPEC, then the address family of
1488 		 * subsequent sockaddrs must either be AF_UNSPEC (for
1489 		 * compatibility with older programs) or must be the same as our
1490 		 * "working" one.
1491 		 *
1492 		 * This code assumes that RTA_DST (1) comes first in the loop.
1493 		 */
1494 		sa = (struct sockaddr *)cp;
1495 		addr_bits = (rtm->rtm_addrs & (1 << i));
1496 		if (addr_bits == 0)
1497 			continue;
1498 		switch (addr_bits) {
1499 		case RTA_DST:
1500 			size = rts_copyfromsockaddr(sa, dst_addrp);
1501 			*afp = sa->sa_family;
1502 			break;
1503 		case RTA_GATEWAY:
1504 			if (sa->sa_family != *afp && sa->sa_family != AF_UNSPEC)
1505 				return (0);
1506 			size = rts_copyfromsockaddr(sa, gw_addrp);
1507 			break;
1508 		case RTA_NETMASK:
1509 			if (sa->sa_family != *afp && sa->sa_family != AF_UNSPEC)
1510 				return (0);
1511 			size = rts_copyfromsockaddr(sa, net_maskp);
1512 			break;
1513 		case RTA_IFP:
1514 			if (sa->sa_family != AF_LINK &&
1515 			    sa->sa_family != AF_UNSPEC)
1516 				return (0);
1517 			sdl = (struct sockaddr_dl *)cp;
1518 			*indexp = sdl->sdl_index;
1519 			size = sizeof (struct sockaddr_dl);
1520 			break;
1521 		case RTA_SRC:
1522 			/* Source address of the incoming packet */
1523 			size = rts_copyfromsockaddr(sa, in_src_addrp);
1524 			*afp = sa->sa_family;
1525 			break;
1526 		case RTA_SRCIFP:
1527 			/* Return incoming interface index pointer */
1528 			if (sa->sa_family != AF_LINK &&
1529 			    sa->sa_family != AF_UNSPEC)
1530 				return (0);
1531 			sdl = (struct sockaddr_dl *)cp;
1532 			*src_indexp = sdl->sdl_index;
1533 			size = sizeof (struct sockaddr_dl);
1534 			break;
1535 		case RTA_IFA:
1536 			if (sa->sa_family != *afp && sa->sa_family != AF_UNSPEC)
1537 				return (0);
1538 			size = rts_copyfromsockaddr(sa, if_addrp);
1539 			break;
1540 		case RTA_AUTHOR:
1541 			if (sa->sa_family != *afp && sa->sa_family != AF_UNSPEC)
1542 				return (0);
1543 			size = rts_copyfromsockaddr(sa, authorp);
1544 			break;
1545 		default:
1546 			return (0);
1547 		}
1548 		if (size == 0)
1549 			return (0);
1550 		cp += size;
1551 		found_addrs |= addr_bits;
1552 	}
1553 
1554 	/*
1555 	 * Parse the routing message and look for any security-
1556 	 * related attributes for the route.  For each valid
1557 	 * attribute, allocate/obtain the corresponding kernel
1558 	 * route security attributes.
1559 	 */
1560 	*error = tsol_rtsa_init(rtm, rtsecattr, cp);
1561 	ASSERT(rtsecattr->rtsa_cnt <= TSOL_RTSA_REQUEST_MAX);
1562 
1563 	return (found_addrs);
1564 }
1565 
1566 /*
1567  * Fills the message with the given info.
1568  */
1569 static void
1570 rts_fill_msg(int type, int rtm_addrs, ipaddr_t dst, ipaddr_t mask,
1571     ipaddr_t gateway, ipaddr_t src_addr, ipaddr_t brd_addr, ipaddr_t author,
1572     const ipif_t *ipif, mblk_t *mp, uint_t sacnt, const tsol_gc_t *gc)
1573 {
1574 	rt_msghdr_t	*rtm;
1575 	sin_t		*sin;
1576 	size_t		data_size, header_size;
1577 	uchar_t		*cp;
1578 	int		i;
1579 
1580 	ASSERT(mp != NULL);
1581 	ASSERT(sacnt == 0 || gc != NULL);
1582 	/*
1583 	 * First find the type of the message
1584 	 * and its length.
1585 	 */
1586 	header_size = rts_header_msg_size(type);
1587 	/*
1588 	 * Now find the size of the data
1589 	 * that follows the message header.
1590 	 */
1591 	data_size = rts_data_msg_size(rtm_addrs, AF_INET, sacnt);
1592 
1593 	rtm = (rt_msghdr_t *)mp->b_rptr;
1594 	mp->b_wptr = &mp->b_rptr[header_size];
1595 	cp = mp->b_wptr;
1596 	bzero(cp, data_size);
1597 	for (i = 0; i < RTA_NUMBITS; i++) {
1598 		sin = (sin_t *)cp;
1599 		switch (rtm_addrs & (1 << i)) {
1600 		case RTA_DST:
1601 			sin->sin_addr.s_addr = dst;
1602 			sin->sin_family = AF_INET;
1603 			cp += sizeof (sin_t);
1604 			break;
1605 		case RTA_GATEWAY:
1606 			sin->sin_addr.s_addr = gateway;
1607 			sin->sin_family = AF_INET;
1608 			cp += sizeof (sin_t);
1609 			break;
1610 		case RTA_NETMASK:
1611 			sin->sin_addr.s_addr = mask;
1612 			sin->sin_family = AF_INET;
1613 			cp += sizeof (sin_t);
1614 			break;
1615 		case RTA_IFP:
1616 			cp += ill_dls_info((struct sockaddr_dl *)cp, ipif);
1617 			break;
1618 		case RTA_SRCIFP:
1619 			/*
1620 			 * RTA_SRCIFP is not yet supported
1621 			 * for RTM_GET and RTM_CHANGE
1622 			 */
1623 			break;
1624 		case RTA_IFA:
1625 		case RTA_SRC:
1626 			sin->sin_addr.s_addr = src_addr;
1627 			sin->sin_family = AF_INET;
1628 			cp += sizeof (sin_t);
1629 			break;
1630 		case RTA_AUTHOR:
1631 			sin->sin_addr.s_addr = author;
1632 			sin->sin_family = AF_INET;
1633 			cp += sizeof (sin_t);
1634 			break;
1635 		case RTA_BRD:
1636 			/*
1637 			 * RTA_BRD is used typically to specify a point-to-point
1638 			 * destination address.
1639 			 */
1640 			sin->sin_addr.s_addr = brd_addr;
1641 			sin->sin_family = AF_INET;
1642 			cp += sizeof (sin_t);
1643 			break;
1644 		}
1645 	}
1646 
1647 	if (gc != NULL) {
1648 		rtm_ext_t *rtm_ext;
1649 		struct rtsa_s *rp_dst;
1650 		tsol_rtsecattr_t *rsap;
1651 		int i;
1652 
1653 		ASSERT(gc->gc_grp != NULL);
1654 		ASSERT(RW_LOCK_HELD(&gc->gc_grp->gcgrp_rwlock));
1655 		ASSERT(sacnt > 0);
1656 
1657 		rtm_ext = (rtm_ext_t *)cp;
1658 		rtm_ext->rtmex_type = RTMEX_GATEWAY_SECATTR;
1659 		rtm_ext->rtmex_len = TSOL_RTSECATTR_SIZE(sacnt);
1660 
1661 		rsap = (tsol_rtsecattr_t *)(rtm_ext + 1);
1662 		rsap->rtsa_cnt = sacnt;
1663 		rp_dst = rsap->rtsa_attr;
1664 
1665 		for (i = 0; i < sacnt; i++, gc = gc->gc_next, rp_dst++) {
1666 			ASSERT(gc->gc_db != NULL);
1667 			bcopy(&gc->gc_db->gcdb_attr, rp_dst, sizeof (*rp_dst));
1668 		}
1669 		cp = (uchar_t *)rp_dst;
1670 	}
1671 
1672 	mp->b_wptr = cp;
1673 	mp->b_cont = NULL;
1674 	/*
1675 	 * set the fields that are common to
1676 	 * to different messages.
1677 	 */
1678 	rtm->rtm_msglen = (short)(header_size + data_size);
1679 	rtm->rtm_version = RTM_VERSION;
1680 	rtm->rtm_type = (uchar_t)type;
1681 }
1682 
1683 /*
1684  * Allocates and initializes a routing socket message.
1685  */
1686 mblk_t *
1687 rts_alloc_msg(int type, int rtm_addrs, sa_family_t af, uint_t sacnt)
1688 {
1689 	size_t	length;
1690 	mblk_t	*mp;
1691 
1692 	length = RTS_MSG_SIZE(type, rtm_addrs, af, sacnt);
1693 	mp = allocb(length, BPRI_MED);
1694 	if (mp == NULL)
1695 		return (mp);
1696 	bzero(mp->b_rptr, length);
1697 	return (mp);
1698 }
1699 
1700 /*
1701  * Returns the size of the routing
1702  * socket message header size.
1703  */
1704 size_t
1705 rts_header_msg_size(int type)
1706 {
1707 	switch (type) {
1708 	case RTM_DELADDR:
1709 	case RTM_NEWADDR:
1710 		return (sizeof (ifa_msghdr_t));
1711 	case RTM_IFINFO:
1712 		return (sizeof (if_msghdr_t));
1713 	default:
1714 		return (sizeof (rt_msghdr_t));
1715 	}
1716 }
1717 
1718 /*
1719  * Returns the size of the message needed with the given rtm_addrs and family.
1720  *
1721  * It is assumed that all of the sockaddrs (with the exception of RTA_IFP) are
1722  * of the same family (currently either AF_INET or AF_INET6).
1723  */
1724 size_t
1725 rts_data_msg_size(int rtm_addrs, sa_family_t af, uint_t sacnt)
1726 {
1727 	int	i;
1728 	size_t	length = 0;
1729 
1730 	for (i = 0; i < RTA_NUMBITS; i++) {
1731 		switch (rtm_addrs & (1 << i)) {
1732 		case RTA_IFP:
1733 			length += sizeof (struct sockaddr_dl);
1734 			break;
1735 		case RTA_DST:
1736 		case RTA_GATEWAY:
1737 		case RTA_NETMASK:
1738 		case RTA_SRC:
1739 		case RTA_SRCIFP:
1740 		case RTA_IFA:
1741 		case RTA_AUTHOR:
1742 		case RTA_BRD:
1743 			ASSERT(af == AF_INET || af == AF_INET6);
1744 			switch (af) {
1745 			case AF_INET:
1746 				length += sizeof (sin_t);
1747 				break;
1748 			case AF_INET6:
1749 				length += sizeof (sin6_t);
1750 				break;
1751 			}
1752 			break;
1753 		}
1754 	}
1755 	if (sacnt > 0)
1756 		length += sizeof (rtm_ext_t) + TSOL_RTSECATTR_SIZE(sacnt);
1757 
1758 	return (length);
1759 }
1760 
1761 /*
1762  * This routine is called to generate a message to the routing
1763  * socket indicating that a redirect has occured, a routing lookup
1764  * has failed, or that a protocol has detected timeouts to a particular
1765  * destination. This routine is called for message types RTM_LOSING,
1766  * RTM_REDIRECT, and RTM_MISS.
1767  */
1768 void
1769 ip_rts_change(int type, ipaddr_t dst_addr, ipaddr_t gw_addr, ipaddr_t net_mask,
1770     ipaddr_t source, ipaddr_t author, int flags, int error, int rtm_addrs)
1771 {
1772 	rt_msghdr_t	*rtm;
1773 	mblk_t		*mp;
1774 
1775 	if (rtm_addrs == 0)
1776 		return;
1777 	mp = rts_alloc_msg(type, rtm_addrs, AF_INET, 0);
1778 	if (mp == NULL)
1779 		return;
1780 	rts_fill_msg(type, rtm_addrs, dst_addr, net_mask, gw_addr, source, 0,
1781 	    author, NULL, mp, 0, NULL);
1782 	rtm = (rt_msghdr_t *)mp->b_rptr;
1783 	rtm->rtm_flags = flags;
1784 	rtm->rtm_errno = error;
1785 	rtm->rtm_flags |= RTF_DONE;
1786 	rtm->rtm_addrs = rtm_addrs;
1787 	rts_queue_input(mp, NULL, AF_INET);
1788 }
1789 
1790 /*
1791  * This routine is called to generate a message to the routing
1792  * socket indicating that the status of a network interface has changed.
1793  * Message type generated RTM_IFINFO.
1794  */
1795 void
1796 ip_rts_ifmsg(const ipif_t *ipif)
1797 {
1798 	if_msghdr_t	*ifm;
1799 	mblk_t		*mp;
1800 	sa_family_t	af;
1801 
1802 	/*
1803 	 * This message should be generated only
1804 	 * when the physical device is changing
1805 	 * state.
1806 	 */
1807 	if (ipif->ipif_id != 0)
1808 		return;
1809 	if (ipif->ipif_isv6) {
1810 		af = AF_INET6;
1811 		mp = rts_alloc_msg(RTM_IFINFO, RTA_IFP, af, 0);
1812 		if (mp == NULL)
1813 			return;
1814 		rts_fill_msg_v6(RTM_IFINFO, RTA_IFP, &ipv6_all_zeros,
1815 		    &ipv6_all_zeros, &ipv6_all_zeros, &ipv6_all_zeros,
1816 		    &ipv6_all_zeros, &ipv6_all_zeros, ipif, mp, 0, NULL);
1817 	} else {
1818 		af = AF_INET;
1819 		mp = rts_alloc_msg(RTM_IFINFO, RTA_IFP, af, 0);
1820 		if (mp == NULL)
1821 			return;
1822 		rts_fill_msg(RTM_IFINFO, RTA_IFP, 0, 0, 0, 0, 0, 0, ipif, mp,
1823 		    0, NULL);
1824 	}
1825 	ifm = (if_msghdr_t *)mp->b_rptr;
1826 	ifm->ifm_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
1827 	ifm->ifm_flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags |
1828 	    ipif->ipif_ill->ill_phyint->phyint_flags;
1829 	rts_getifdata(&ifm->ifm_data, ipif);
1830 	ifm->ifm_addrs = RTA_IFP;
1831 	rts_queue_input(mp, NULL, af);
1832 }
1833 
1834 /*
1835  * This is called to generate messages to the routing socket
1836  * indicating a network interface has had addresses associated with it.
1837  * The structure of the code is based on the 4.4BSD-Lite2 <net/rtsock.c>.
1838  */
1839 void
1840 ip_rts_newaddrmsg(int cmd, int error, const ipif_t *ipif)
1841 {
1842 	int		pass;
1843 	int		ncmd;
1844 	int		rtm_addrs;
1845 	mblk_t		*mp;
1846 	ifa_msghdr_t	*ifam;
1847 	rt_msghdr_t	*rtm;
1848 	sa_family_t	af;
1849 
1850 	if (ipif->ipif_isv6)
1851 		af = AF_INET6;
1852 	else
1853 		af = AF_INET;
1854 	/*
1855 	 * If the request is DELETE, send RTM_DELETE and RTM_DELADDR.
1856 	 * if the request is ADD, send RTM_NEWADDR and RTM_ADD.
1857 	 */
1858 	for (pass = 1; pass < 3; pass++) {
1859 		if ((cmd == RTM_ADD && pass == 1) ||
1860 		    (cmd == RTM_DELETE && pass == 2)) {
1861 			ncmd = ((cmd == RTM_ADD) ? RTM_NEWADDR : RTM_DELADDR);
1862 
1863 			rtm_addrs = (RTA_IFA | RTA_NETMASK | RTA_BRD | RTA_IFP);
1864 			mp = rts_alloc_msg(ncmd, rtm_addrs, af, 0);
1865 			if (mp == NULL)
1866 				continue;
1867 			switch (af) {
1868 			case AF_INET:
1869 				rts_fill_msg(ncmd, rtm_addrs, 0,
1870 				    ipif->ipif_net_mask, 0, ipif->ipif_lcl_addr,
1871 				    ipif->ipif_pp_dst_addr, 0, ipif, mp,
1872 				    0, NULL);
1873 				break;
1874 			case AF_INET6:
1875 				rts_fill_msg_v6(ncmd, rtm_addrs,
1876 				    &ipv6_all_zeros, &ipif->ipif_v6net_mask,
1877 				    &ipv6_all_zeros, &ipif->ipif_v6lcl_addr,
1878 				    &ipif->ipif_v6pp_dst_addr, &ipv6_all_zeros,
1879 				    ipif, mp, 0, NULL);
1880 				break;
1881 			}
1882 			ifam = (ifa_msghdr_t *)mp->b_rptr;
1883 			ifam->ifam_index =
1884 			    ipif->ipif_ill->ill_phyint->phyint_ifindex;
1885 			ifam->ifam_metric = ipif->ipif_metric;
1886 			ifam->ifam_flags = ((cmd == RTM_ADD) ? RTF_UP : 0);
1887 			ifam->ifam_addrs = rtm_addrs;
1888 			rts_queue_input(mp, NULL, af);
1889 		}
1890 		if ((cmd == RTM_ADD && pass == 2) ||
1891 		    (cmd == RTM_DELETE && pass == 1)) {
1892 			rtm_addrs = (RTA_DST | RTA_NETMASK);
1893 			mp = rts_alloc_msg(cmd, rtm_addrs, af, 0);
1894 			if (mp == NULL)
1895 				continue;
1896 			switch (af) {
1897 			case AF_INET:
1898 				rts_fill_msg(cmd, rtm_addrs,
1899 				    ipif->ipif_lcl_addr, ipif->ipif_net_mask, 0,
1900 				    0, 0, 0, NULL, mp, 0, NULL);
1901 				break;
1902 			case AF_INET6:
1903 				rts_fill_msg_v6(cmd, rtm_addrs,
1904 				    &ipif->ipif_v6lcl_addr,
1905 				    &ipif->ipif_v6net_mask, &ipv6_all_zeros,
1906 				    &ipv6_all_zeros, &ipv6_all_zeros,
1907 				    &ipv6_all_zeros, NULL, mp, 0, NULL);
1908 				break;
1909 			}
1910 			rtm = (rt_msghdr_t *)mp->b_rptr;
1911 			rtm->rtm_index =
1912 			    ipif->ipif_ill->ill_phyint->phyint_ifindex;
1913 			rtm->rtm_flags = ((cmd == RTM_ADD) ? RTF_UP : 0);
1914 			rtm->rtm_errno = error;
1915 			if (error == 0)
1916 				rtm->rtm_flags |= RTF_DONE;
1917 			rtm->rtm_addrs = rtm_addrs;
1918 			rts_queue_input(mp, NULL, af);
1919 		}
1920 	}
1921 }
1922 
1923 /*
1924  * Based on the address family specified in a sockaddr, copy the address field
1925  * into an in6_addr_t.
1926  *
1927  * In the case of AF_UNSPEC, we assume the family is actually AF_INET for
1928  * compatibility with programs that leave the family cleared in the sockaddr.
1929  * Callers of rts_copyfromsockaddr should check the family themselves if they
1930  * wish to verify its value.
1931  *
1932  * In the case of AF_INET6, a check is made to ensure that address is not an
1933  * IPv4-mapped address.
1934  */
1935 size_t
1936 rts_copyfromsockaddr(struct sockaddr *sa, in6_addr_t *addrp)
1937 {
1938 	switch (sa->sa_family) {
1939 	case AF_INET:
1940 	case AF_UNSPEC:
1941 		IN6_IPADDR_TO_V4MAPPED(((sin_t *)sa)->sin_addr.s_addr, addrp);
1942 		return (sizeof (sin_t));
1943 	case AF_INET6:
1944 		*addrp = ((sin6_t *)sa)->sin6_addr;
1945 		if (IN6_IS_ADDR_V4MAPPED(addrp))
1946 			return (0);
1947 		return (sizeof (sin6_t));
1948 	default:
1949 		return (0);
1950 	}
1951 }
1952