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