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