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