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