Home | History | Annotate | Line # | Download | only in net
route.c revision 1.191
      1  1.191     ozaki /*	$NetBSD: route.c,v 1.191 2017/02/17 04:31:34 ozaki-r Exp $	*/
      2   1.18       kml 
      3   1.18       kml /*-
      4  1.106        ad  * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
      5   1.18       kml  * All rights reserved.
      6   1.18       kml  *
      7   1.18       kml  * This code is derived from software contributed to The NetBSD Foundation
      8   1.18       kml  * by Kevin M. Lahey of the Numerical Aerospace Simulation Facility,
      9   1.18       kml  * NASA Ames Research Center.
     10   1.18       kml  *
     11   1.18       kml  * Redistribution and use in source and binary forms, with or without
     12   1.18       kml  * modification, are permitted provided that the following conditions
     13   1.18       kml  * are met:
     14   1.18       kml  * 1. Redistributions of source code must retain the above copyright
     15   1.18       kml  *    notice, this list of conditions and the following disclaimer.
     16   1.18       kml  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.18       kml  *    notice, this list of conditions and the following disclaimer in the
     18   1.18       kml  *    documentation and/or other materials provided with the distribution.
     19   1.18       kml  *
     20   1.18       kml  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.18       kml  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.18       kml  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.18       kml  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.18       kml  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.18       kml  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.18       kml  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.18       kml  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.18       kml  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.18       kml  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.18       kml  * POSSIBILITY OF SUCH DAMAGE.
     31   1.18       kml  */
     32   1.11       cgd 
     33    1.1       cgd /*
     34   1.25    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
     35   1.25    itojun  * All rights reserved.
     36   1.65     perry  *
     37   1.25    itojun  * Redistribution and use in source and binary forms, with or without
     38   1.25    itojun  * modification, are permitted provided that the following conditions
     39   1.25    itojun  * are met:
     40   1.25    itojun  * 1. Redistributions of source code must retain the above copyright
     41   1.25    itojun  *    notice, this list of conditions and the following disclaimer.
     42   1.25    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     43   1.25    itojun  *    notice, this list of conditions and the following disclaimer in the
     44   1.25    itojun  *    documentation and/or other materials provided with the distribution.
     45   1.25    itojun  * 3. Neither the name of the project nor the names of its contributors
     46   1.25    itojun  *    may be used to endorse or promote products derived from this software
     47   1.25    itojun  *    without specific prior written permission.
     48   1.65     perry  *
     49   1.25    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     50   1.25    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51   1.25    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52   1.25    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     53   1.25    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54   1.25    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55   1.25    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56   1.25    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57   1.25    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58   1.25    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59   1.25    itojun  * SUCH DAMAGE.
     60   1.25    itojun  */
     61   1.25    itojun 
     62   1.25    itojun /*
     63   1.10   mycroft  * Copyright (c) 1980, 1986, 1991, 1993
     64   1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
     65    1.1       cgd  *
     66    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     67    1.1       cgd  * modification, are permitted provided that the following conditions
     68    1.1       cgd  * are met:
     69    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     70    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     71    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     72    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     73    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     74   1.58       agc  * 3. Neither the name of the University nor the names of its contributors
     75    1.1       cgd  *    may be used to endorse or promote products derived from this software
     76    1.1       cgd  *    without specific prior written permission.
     77    1.1       cgd  *
     78    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     79    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     80    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     81    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     82    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     83    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     84    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     85    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     86    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     87    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     88    1.1       cgd  * SUCH DAMAGE.
     89    1.1       cgd  *
     90   1.17  christos  *	@(#)route.c	8.3 (Berkeley) 1/9/95
     91    1.1       cgd  */
     92   1.50     lukem 
     93  1.149     pooka #ifdef _KERNEL_OPT
     94  1.136       roy #include "opt_inet.h"
     95   1.90    dyoung #include "opt_route.h"
     96  1.175     ozaki #include "opt_net_mpsafe.h"
     97  1.149     pooka #endif
     98   1.90    dyoung 
     99   1.50     lukem #include <sys/cdefs.h>
    100  1.191     ozaki __KERNEL_RCSID(0, "$NetBSD: route.c,v 1.191 2017/02/17 04:31:34 ozaki-r Exp $");
    101    1.2       cgd 
    102    1.5   mycroft #include <sys/param.h>
    103  1.140     ozaki #ifdef RTFLUSH_DEBUG
    104   1.90    dyoung #include <sys/sysctl.h>
    105  1.140     ozaki #endif
    106    1.5   mycroft #include <sys/systm.h>
    107   1.35   thorpej #include <sys/callout.h>
    108    1.5   mycroft #include <sys/proc.h>
    109    1.5   mycroft #include <sys/mbuf.h>
    110    1.5   mycroft #include <sys/socket.h>
    111    1.5   mycroft #include <sys/socketvar.h>
    112    1.5   mycroft #include <sys/domain.h>
    113   1.18       kml #include <sys/kernel.h>
    114    1.5   mycroft #include <sys/ioctl.h>
    115   1.22   thorpej #include <sys/pool.h>
    116  1.119      elad #include <sys/kauth.h>
    117  1.170     ozaki #include <sys/workqueue.h>
    118  1.182     ozaki #include <sys/syslog.h>
    119  1.183     ozaki #include <sys/rwlock.h>
    120  1.183     ozaki #include <sys/mutex.h>
    121  1.183     ozaki #include <sys/cpu.h>
    122    1.1       cgd 
    123    1.5   mycroft #include <net/if.h>
    124  1.114    dyoung #include <net/if_dl.h>
    125    1.5   mycroft #include <net/route.h>
    126    1.1       cgd 
    127    1.5   mycroft #include <netinet/in.h>
    128    1.5   mycroft #include <netinet/in_var.h>
    129    1.1       cgd 
    130   1.90    dyoung #ifdef RTFLUSH_DEBUG
    131   1.90    dyoung #define	rtcache_debug() __predict_false(_rtcache_debug)
    132   1.90    dyoung #else /* RTFLUSH_DEBUG */
    133   1.90    dyoung #define	rtcache_debug() 0
    134   1.90    dyoung #endif /* RTFLUSH_DEBUG */
    135    1.5   mycroft 
    136  1.183     ozaki #ifdef RT_DEBUG
    137  1.183     ozaki #define RT_REFCNT_TRACE(rt)	printf("%s:%d: rt=%p refcnt=%d\n", \
    138  1.183     ozaki 				    __func__, __LINE__, (rt), (rt)->rt_refcnt)
    139  1.183     ozaki #else
    140  1.183     ozaki #define RT_REFCNT_TRACE(rt)	do {} while (0)
    141  1.183     ozaki #endif
    142  1.183     ozaki 
    143  1.183     ozaki #ifdef DEBUG
    144  1.183     ozaki #define dlog(level, fmt, args...)	log(level, fmt, ##args)
    145  1.183     ozaki #else
    146  1.183     ozaki #define dlog(level, fmt, args...)	do {} while (0)
    147  1.183     ozaki #endif
    148  1.183     ozaki 
    149  1.155     ozaki struct rtstat		rtstat;
    150    1.1       cgd 
    151  1.155     ozaki static int		rttrash;	/* routes not in table but not freed */
    152    1.1       cgd 
    153  1.155     ozaki static struct pool	rtentry_pool;
    154  1.155     ozaki static struct pool	rttimer_pool;
    155   1.22   thorpej 
    156  1.155     ozaki static struct callout	rt_timer_ch; /* callout for rt_timer_timer() */
    157  1.177     ozaki static struct workqueue	*rt_timer_wq;
    158  1.177     ozaki static struct work	rt_timer_wk;
    159  1.177     ozaki 
    160  1.177     ozaki static void	rt_timer_init(void);
    161  1.181     ozaki static void	rt_timer_queue_remove_all(struct rttimer_queue *);
    162  1.178     ozaki static void	rt_timer_remove_all(struct rtentry *);
    163  1.177     ozaki static void	rt_timer_timer(void *);
    164   1.35   thorpej 
    165  1.183     ozaki /*
    166  1.183     ozaki  * Locking notes:
    167  1.183     ozaki  * - The routing table is protected by a global rwlock
    168  1.183     ozaki  *   - API: RT_RLOCK and friends
    169  1.183     ozaki  * - rtcaches are protected by a global rwlock
    170  1.183     ozaki  *   - API: RTCACHE_RLOCK and friends
    171  1.183     ozaki  * - References to a rtentry is managed by reference counting and psref
    172  1.183     ozaki  *   - Reference couting is used for temporal reference when a rtentry
    173  1.183     ozaki  *     is fetched from the routing table
    174  1.183     ozaki  *   - psref is used for temporal reference when a rtentry is fetched
    175  1.183     ozaki  *     from a rtcache
    176  1.183     ozaki  *     - struct route (rtcache) has struct psref, so we cannot obtain
    177  1.183     ozaki  *       a reference twice on the same struct route
    178  1.183     ozaki  *   - Befere destroying or updating a rtentry, we have to wait for
    179  1.183     ozaki  *     all references left (see below for details)
    180  1.183     ozaki  *   - APIs
    181  1.183     ozaki  *     - An obtained rtentry via rtalloc1 or rtrequest* must be
    182  1.183     ozaki  *       unreferenced by rt_unref
    183  1.183     ozaki  *     - An obtained rtentry via rtcache_* must be unreferenced by
    184  1.183     ozaki  *       rtcache_unref
    185  1.183     ozaki  *   - TODO: once we get a lockless routing table, we should use only
    186  1.183     ozaki  *           psref for rtentries
    187  1.183     ozaki  * - rtentry destruction
    188  1.183     ozaki  *   - A rtentry is destroyed (freed) only when we call rtrequest(RTM_DELETE)
    189  1.183     ozaki  *   - If a caller of rtrequest grabs a reference of a rtentry, the caller
    190  1.183     ozaki  *     has a responsibility to destroy the rtentry by itself by calling
    191  1.183     ozaki  *     rt_free
    192  1.183     ozaki  *     - If not, rtrequest itself does that
    193  1.183     ozaki  *   - If rt_free is called in softint, the actual destruction routine is
    194  1.183     ozaki  *     deferred to a workqueue
    195  1.183     ozaki  * - rtentry update
    196  1.183     ozaki  *   - When updating a rtentry, RTF_UPDATING flag is set
    197  1.183     ozaki  *   - If a rtentry is set RTF_UPDATING, fetching the rtentry from
    198  1.183     ozaki  *     the routing table or a rtcache results in either of the following
    199  1.183     ozaki  *     cases:
    200  1.183     ozaki  *     - if the caller runs in softint, the caller fails to fetch
    201  1.183     ozaki  *     - otherwise, the caller waits for the update completed and retries
    202  1.183     ozaki  *       to fetch (probably succeed to fetch for the second time)
    203  1.183     ozaki  */
    204  1.183     ozaki 
    205  1.183     ozaki /*
    206  1.183     ozaki  * Global locks for the routing table and rtcaches.
    207  1.183     ozaki  * Locking order: rtcache_lock => rt_lock
    208  1.183     ozaki  */
    209  1.183     ozaki static krwlock_t		rt_lock __cacheline_aligned;
    210  1.183     ozaki #ifdef NET_MPSAFE
    211  1.183     ozaki #define RT_RLOCK()		rw_enter(&rt_lock, RW_READER)
    212  1.183     ozaki #define RT_WLOCK()		rw_enter(&rt_lock, RW_WRITER)
    213  1.183     ozaki #define RT_UNLOCK()		rw_exit(&rt_lock)
    214  1.183     ozaki #define RT_LOCKED()		rw_lock_held(&rt_lock)
    215  1.183     ozaki #define	RT_ASSERT_WLOCK()	KASSERT(rw_write_held(&rt_lock))
    216  1.183     ozaki #else
    217  1.183     ozaki #define RT_RLOCK()		do {} while (0)
    218  1.183     ozaki #define RT_WLOCK()		do {} while (0)
    219  1.183     ozaki #define RT_UNLOCK()		do {} while (0)
    220  1.183     ozaki #define RT_LOCKED()		false
    221  1.183     ozaki #define	RT_ASSERT_WLOCK()	do {} while (0)
    222  1.183     ozaki #endif
    223  1.183     ozaki 
    224  1.183     ozaki static krwlock_t		rtcache_lock __cacheline_aligned;
    225  1.183     ozaki #ifdef NET_MPSAFE
    226  1.183     ozaki #define RTCACHE_RLOCK()		rw_enter(&rtcache_lock, RW_READER)
    227  1.183     ozaki #define RTCACHE_WLOCK()		rw_enter(&rtcache_lock, RW_WRITER)
    228  1.183     ozaki #define RTCACHE_UNLOCK()	rw_exit(&rtcache_lock)
    229  1.183     ozaki #define	RTCACHE_ASSERT_WLOCK()	KASSERT(rw_write_held(&rtcache_lock))
    230  1.183     ozaki #define	RTCACHE_WLOCKED()	rw_write_held(&rtcache_lock)
    231  1.183     ozaki #else
    232  1.183     ozaki #define RTCACHE_RLOCK()		do {} while (0)
    233  1.183     ozaki #define RTCACHE_WLOCK()		do {} while (0)
    234  1.183     ozaki #define RTCACHE_UNLOCK()	do {} while (0)
    235  1.183     ozaki #define	RTCACHE_ASSERT_WLOCK()	do {} while (0)
    236  1.183     ozaki #define	RTCACHE_WLOCKED()	false
    237  1.183     ozaki #endif
    238  1.183     ozaki 
    239  1.183     ozaki /*
    240  1.183     ozaki  * mutex and cv that are used to wait for references to a rtentry left
    241  1.183     ozaki  * before updating the rtentry.
    242  1.183     ozaki  */
    243  1.183     ozaki static struct {
    244  1.183     ozaki 	kmutex_t		lock;
    245  1.183     ozaki 	kcondvar_t		cv;
    246  1.183     ozaki 	bool			ongoing;
    247  1.183     ozaki 	const struct lwp	*lwp;
    248  1.183     ozaki } rt_update_global __cacheline_aligned;
    249  1.183     ozaki 
    250  1.183     ozaki /*
    251  1.183     ozaki  * A workqueue and stuff that are used to defer the destruction routine
    252  1.183     ozaki  * of rtentries.
    253  1.183     ozaki  */
    254  1.183     ozaki static struct {
    255  1.183     ozaki 	struct workqueue	*wq;
    256  1.183     ozaki 	struct work		wk;
    257  1.183     ozaki 	kmutex_t		lock;
    258  1.183     ozaki 	struct rtentry		*queue[10];
    259  1.183     ozaki } rt_free_global __cacheline_aligned;
    260  1.183     ozaki 
    261  1.183     ozaki /* psref for rtentry */
    262  1.183     ozaki static struct psref_class *rt_psref_class __read_mostly;
    263  1.183     ozaki 
    264   1.90    dyoung #ifdef RTFLUSH_DEBUG
    265   1.90    dyoung static int _rtcache_debug = 0;
    266   1.90    dyoung #endif /* RTFLUSH_DEBUG */
    267   1.90    dyoung 
    268  1.119      elad static kauth_listener_t route_listener;
    269  1.119      elad 
    270   1.60      matt static int rtdeletemsg(struct rtentry *);
    271  1.144     ozaki static void rtflushall(int);
    272   1.40    itojun 
    273  1.141     ozaki static void rt_maskedcopy(const struct sockaddr *,
    274  1.141     ozaki     struct sockaddr *, const struct sockaddr *);
    275  1.141     ozaki 
    276  1.144     ozaki static void rtcache_clear(struct route *);
    277  1.168     ozaki static void rtcache_clear_rtentry(int, struct rtentry *);
    278  1.144     ozaki static void rtcache_invalidate(struct dom_rtlist *);
    279  1.144     ozaki 
    280  1.183     ozaki static void rt_ref(struct rtentry *);
    281  1.183     ozaki 
    282  1.183     ozaki static struct rtentry *
    283  1.183     ozaki     rtalloc1_locked(const struct sockaddr *, int, bool);
    284  1.183     ozaki static struct rtentry *
    285  1.183     ozaki     rtcache_validate_locked(struct route *);
    286  1.183     ozaki static void rtcache_free_locked(struct route *);
    287  1.183     ozaki static int rtcache_setdst_locked(struct route *, const struct sockaddr *);
    288  1.183     ozaki 
    289  1.183     ozaki static void rtcache_ref(struct rtentry *, struct route *);
    290  1.183     ozaki 
    291  1.188     ozaki #ifdef NET_MPSAFE
    292  1.183     ozaki static void rt_update_wait(void);
    293  1.188     ozaki #endif
    294  1.183     ozaki 
    295  1.183     ozaki static bool rt_wait_ok(void);
    296  1.183     ozaki static void rt_wait_refcnt(const char *, struct rtentry *, int);
    297  1.183     ozaki static void rt_wait_psref(struct rtentry *);
    298  1.183     ozaki 
    299  1.162     ozaki #ifdef DDB
    300  1.162     ozaki static void db_print_sa(const struct sockaddr *);
    301  1.162     ozaki static void db_print_ifa(struct ifaddr *);
    302  1.162     ozaki static int db_show_rtentry(struct rtentry *, void *);
    303  1.162     ozaki #endif
    304  1.162     ozaki 
    305   1.90    dyoung #ifdef RTFLUSH_DEBUG
    306  1.118     pooka static void sysctl_net_rtcache_setup(struct sysctllog **);
    307  1.118     pooka static void
    308  1.118     pooka sysctl_net_rtcache_setup(struct sysctllog **clog)
    309   1.90    dyoung {
    310   1.90    dyoung 	const struct sysctlnode *rnode;
    311   1.90    dyoung 
    312   1.90    dyoung 	if (sysctl_createv(clog, 0, NULL, &rnode, CTLFLAG_PERMANENT,
    313   1.90    dyoung 	    CTLTYPE_NODE,
    314   1.90    dyoung 	    "rtcache", SYSCTL_DESCR("Route cache related settings"),
    315  1.128     pooka 	    NULL, 0, NULL, 0, CTL_NET, CTL_CREATE, CTL_EOL) != 0)
    316   1.90    dyoung 		return;
    317   1.90    dyoung 	if (sysctl_createv(clog, 0, &rnode, &rnode,
    318   1.90    dyoung 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    319   1.90    dyoung 	    "debug", SYSCTL_DESCR("Debug route caches"),
    320   1.90    dyoung 	    NULL, 0, &_rtcache_debug, 0, CTL_CREATE, CTL_EOL) != 0)
    321   1.90    dyoung 		return;
    322   1.90    dyoung }
    323   1.90    dyoung #endif /* RTFLUSH_DEBUG */
    324   1.90    dyoung 
    325  1.144     ozaki static inline void
    326  1.144     ozaki rt_destroy(struct rtentry *rt)
    327  1.144     ozaki {
    328  1.144     ozaki 	if (rt->_rt_key != NULL)
    329  1.144     ozaki 		sockaddr_free(rt->_rt_key);
    330  1.144     ozaki 	if (rt->rt_gateway != NULL)
    331  1.144     ozaki 		sockaddr_free(rt->rt_gateway);
    332  1.144     ozaki 	if (rt_gettag(rt) != NULL)
    333  1.144     ozaki 		sockaddr_free(rt_gettag(rt));
    334  1.144     ozaki 	rt->_rt_key = rt->rt_gateway = rt->rt_tag = NULL;
    335  1.144     ozaki }
    336  1.144     ozaki 
    337  1.144     ozaki static inline const struct sockaddr *
    338  1.144     ozaki rt_setkey(struct rtentry *rt, const struct sockaddr *key, int flags)
    339  1.144     ozaki {
    340  1.144     ozaki 	if (rt->_rt_key == key)
    341  1.144     ozaki 		goto out;
    342  1.144     ozaki 
    343  1.144     ozaki 	if (rt->_rt_key != NULL)
    344  1.144     ozaki 		sockaddr_free(rt->_rt_key);
    345  1.144     ozaki 	rt->_rt_key = sockaddr_dup(key, flags);
    346  1.144     ozaki out:
    347  1.144     ozaki 	rt->rt_nodes->rn_key = (const char *)rt->_rt_key;
    348  1.144     ozaki 	return rt->_rt_key;
    349  1.144     ozaki }
    350  1.144     ozaki 
    351   1.81     joerg struct ifaddr *
    352   1.81     joerg rt_get_ifa(struct rtentry *rt)
    353   1.81     joerg {
    354   1.81     joerg 	struct ifaddr *ifa;
    355   1.81     joerg 
    356   1.81     joerg 	if ((ifa = rt->rt_ifa) == NULL)
    357   1.81     joerg 		return ifa;
    358   1.81     joerg 	else if (ifa->ifa_getifa == NULL)
    359   1.81     joerg 		return ifa;
    360   1.81     joerg #if 0
    361   1.81     joerg 	else if (ifa->ifa_seqno != NULL && *ifa->ifa_seqno == rt->rt_ifa_seqno)
    362   1.81     joerg 		return ifa;
    363   1.81     joerg #endif
    364   1.81     joerg 	else {
    365   1.94    dyoung 		ifa = (*ifa->ifa_getifa)(ifa, rt_getkey(rt));
    366  1.145       roy 		if (ifa == NULL)
    367  1.145       roy 			return NULL;
    368   1.81     joerg 		rt_replace_ifa(rt, ifa);
    369   1.81     joerg 		return ifa;
    370   1.81     joerg 	}
    371   1.81     joerg }
    372   1.81     joerg 
    373   1.80     joerg static void
    374   1.80     joerg rt_set_ifa1(struct rtentry *rt, struct ifaddr *ifa)
    375   1.80     joerg {
    376   1.80     joerg 	rt->rt_ifa = ifa;
    377   1.80     joerg 	if (ifa->ifa_seqno != NULL)
    378   1.80     joerg 		rt->rt_ifa_seqno = *ifa->ifa_seqno;
    379   1.80     joerg }
    380   1.80     joerg 
    381  1.116       roy /*
    382  1.116       roy  * Is this route the connected route for the ifa?
    383  1.116       roy  */
    384  1.116       roy static int
    385  1.116       roy rt_ifa_connected(const struct rtentry *rt, const struct ifaddr *ifa)
    386  1.116       roy {
    387  1.116       roy 	const struct sockaddr *key, *dst, *odst;
    388  1.116       roy 	struct sockaddr_storage maskeddst;
    389  1.116       roy 
    390  1.116       roy 	key = rt_getkey(rt);
    391  1.116       roy 	dst = rt->rt_flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
    392  1.116       roy 	if (dst == NULL ||
    393  1.116       roy 	    dst->sa_family != key->sa_family ||
    394  1.116       roy 	    dst->sa_len != key->sa_len)
    395  1.116       roy 		return 0;
    396  1.116       roy 	if ((rt->rt_flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
    397  1.116       roy 		odst = dst;
    398  1.116       roy 		dst = (struct sockaddr *)&maskeddst;
    399  1.116       roy 		rt_maskedcopy(odst, (struct sockaddr *)&maskeddst,
    400  1.116       roy 		    ifa->ifa_netmask);
    401  1.116       roy 	}
    402  1.116       roy 	return (memcmp(dst, key, dst->sa_len) == 0);
    403  1.116       roy }
    404  1.116       roy 
    405   1.80     joerg void
    406   1.80     joerg rt_replace_ifa(struct rtentry *rt, struct ifaddr *ifa)
    407   1.80     joerg {
    408  1.116       roy 	if (rt->rt_ifa &&
    409  1.116       roy 	    rt->rt_ifa != ifa &&
    410  1.116       roy 	    rt->rt_ifa->ifa_flags & IFA_ROUTE &&
    411  1.116       roy 	    rt_ifa_connected(rt, rt->rt_ifa))
    412  1.116       roy 	{
    413  1.116       roy 		RT_DPRINTF("rt->_rt_key = %p, ifa = %p, "
    414  1.116       roy 		    "replace deleted IFA_ROUTE\n",
    415  1.116       roy 		    (void *)rt->_rt_key, (void *)rt->rt_ifa);
    416  1.116       roy 		rt->rt_ifa->ifa_flags &= ~IFA_ROUTE;
    417  1.116       roy 		if (rt_ifa_connected(rt, ifa)) {
    418  1.116       roy 			RT_DPRINTF("rt->_rt_key = %p, ifa = %p, "
    419  1.116       roy 			    "replace added IFA_ROUTE\n",
    420  1.116       roy 			    (void *)rt->_rt_key, (void *)ifa);
    421  1.116       roy 			ifa->ifa_flags |= IFA_ROUTE;
    422  1.116       roy 		}
    423  1.116       roy 	}
    424  1.116       roy 
    425  1.133     rmind 	ifaref(ifa);
    426  1.133     rmind 	ifafree(rt->rt_ifa);
    427   1.80     joerg 	rt_set_ifa1(rt, ifa);
    428   1.80     joerg }
    429   1.80     joerg 
    430   1.80     joerg static void
    431   1.80     joerg rt_set_ifa(struct rtentry *rt, struct ifaddr *ifa)
    432   1.80     joerg {
    433  1.133     rmind 	ifaref(ifa);
    434   1.80     joerg 	rt_set_ifa1(rt, ifa);
    435   1.80     joerg }
    436   1.80     joerg 
    437  1.119      elad static int
    438  1.119      elad route_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    439  1.119      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    440  1.119      elad {
    441  1.119      elad 	struct rt_msghdr *rtm;
    442  1.119      elad 	int result;
    443  1.119      elad 
    444  1.119      elad 	result = KAUTH_RESULT_DEFER;
    445  1.119      elad 	rtm = arg1;
    446  1.119      elad 
    447  1.120      elad 	if (action != KAUTH_NETWORK_ROUTE)
    448  1.120      elad 		return result;
    449  1.120      elad 
    450  1.119      elad 	if (rtm->rtm_type == RTM_GET)
    451  1.119      elad 		result = KAUTH_RESULT_ALLOW;
    452  1.119      elad 
    453  1.119      elad 	return result;
    454  1.119      elad }
    455  1.119      elad 
    456  1.183     ozaki static void rt_free_work(struct work *, void *);
    457  1.183     ozaki 
    458    1.9   mycroft void
    459  1.124      matt rt_init(void)
    460    1.1       cgd {
    461  1.183     ozaki 	int error;
    462   1.22   thorpej 
    463  1.118     pooka #ifdef RTFLUSH_DEBUG
    464  1.118     pooka 	sysctl_net_rtcache_setup(NULL);
    465  1.118     pooka #endif
    466  1.118     pooka 
    467  1.183     ozaki 	mutex_init(&rt_free_global.lock, MUTEX_DEFAULT, IPL_SOFTNET);
    468  1.183     ozaki 	rt_psref_class = psref_class_create("rtentry", IPL_SOFTNET);
    469  1.183     ozaki 
    470  1.183     ozaki 	error = workqueue_create(&rt_free_global.wq, "rt_free",
    471  1.183     ozaki 	    rt_free_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
    472  1.183     ozaki 	if (error)
    473  1.183     ozaki 		panic("%s: workqueue_create failed (%d)\n", __func__, error);
    474  1.183     ozaki 
    475  1.183     ozaki 	mutex_init(&rt_update_global.lock, MUTEX_DEFAULT, IPL_SOFTNET);
    476  1.183     ozaki 	cv_init(&rt_update_global.cv, "rt_update");
    477  1.183     ozaki 
    478  1.113     pooka 	pool_init(&rtentry_pool, sizeof(struct rtentry), 0, 0, 0, "rtentpl",
    479  1.113     pooka 	    NULL, IPL_SOFTNET);
    480  1.113     pooka 	pool_init(&rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl",
    481  1.113     pooka 	    NULL, IPL_SOFTNET);
    482  1.113     pooka 
    483   1.10   mycroft 	rn_init();	/* initialize all zeroes, all ones, mask table */
    484  1.125    dyoung 	rtbl_init();
    485  1.119      elad 
    486  1.119      elad 	route_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
    487  1.119      elad 	    route_listener_cb, NULL);
    488    1.1       cgd }
    489    1.1       cgd 
    490  1.144     ozaki static void
    491   1.82    dyoung rtflushall(int family)
    492   1.82    dyoung {
    493   1.90    dyoung 	struct domain *dom;
    494   1.90    dyoung 
    495   1.90    dyoung 	if (rtcache_debug())
    496   1.90    dyoung 		printf("%s: enter\n", __func__);
    497   1.90    dyoung 
    498   1.90    dyoung 	if ((dom = pffinddomain(family)) == NULL)
    499   1.90    dyoung 		return;
    500   1.82    dyoung 
    501  1.183     ozaki 	RTCACHE_WLOCK();
    502  1.105    dyoung 	rtcache_invalidate(&dom->dom_rtcache);
    503  1.183     ozaki 	RTCACHE_UNLOCK();
    504   1.82    dyoung }
    505   1.82    dyoung 
    506  1.131     rmind static void
    507   1.82    dyoung rtcache(struct route *ro)
    508   1.82    dyoung {
    509   1.90    dyoung 	struct domain *dom;
    510   1.82    dyoung 
    511  1.183     ozaki 	RTCACHE_ASSERT_WLOCK();
    512  1.183     ozaki 
    513  1.114    dyoung 	rtcache_invariants(ro);
    514   1.99    dyoung 	KASSERT(ro->_ro_rt != NULL);
    515  1.105    dyoung 	KASSERT(ro->ro_invalid == false);
    516   1.90    dyoung 	KASSERT(rtcache_getdst(ro) != NULL);
    517   1.82    dyoung 
    518   1.90    dyoung 	if ((dom = pffinddomain(rtcache_getdst(ro)->sa_family)) == NULL)
    519   1.90    dyoung 		return;
    520   1.90    dyoung 
    521   1.90    dyoung 	LIST_INSERT_HEAD(&dom->dom_rtcache, ro, ro_rtcache_next);
    522  1.114    dyoung 	rtcache_invariants(ro);
    523   1.82    dyoung }
    524   1.82    dyoung 
    525  1.158     ozaki #ifdef RT_DEBUG
    526  1.158     ozaki static void
    527  1.158     ozaki dump_rt(const struct rtentry *rt)
    528  1.158     ozaki {
    529  1.158     ozaki 	char buf[512];
    530  1.158     ozaki 
    531  1.158     ozaki 	aprint_normal("rt: ");
    532  1.158     ozaki 	aprint_normal("p=%p ", rt);
    533  1.158     ozaki 	if (rt->_rt_key == NULL) {
    534  1.158     ozaki 		aprint_normal("dst=(NULL) ");
    535  1.158     ozaki 	} else {
    536  1.158     ozaki 		sockaddr_format(rt->_rt_key, buf, sizeof(buf));
    537  1.158     ozaki 		aprint_normal("dst=%s ", buf);
    538  1.158     ozaki 	}
    539  1.158     ozaki 	if (rt->rt_gateway == NULL) {
    540  1.158     ozaki 		aprint_normal("gw=(NULL) ");
    541  1.158     ozaki 	} else {
    542  1.158     ozaki 		sockaddr_format(rt->_rt_key, buf, sizeof(buf));
    543  1.158     ozaki 		aprint_normal("gw=%s ", buf);
    544  1.158     ozaki 	}
    545  1.158     ozaki 	aprint_normal("flags=%x ", rt->rt_flags);
    546  1.158     ozaki 	if (rt->rt_ifp == NULL) {
    547  1.158     ozaki 		aprint_normal("if=(NULL) ");
    548  1.158     ozaki 	} else {
    549  1.158     ozaki 		aprint_normal("if=%s ", rt->rt_ifp->if_xname);
    550  1.158     ozaki 	}
    551  1.158     ozaki 	aprint_normal("\n");
    552  1.158     ozaki }
    553  1.158     ozaki #endif /* RT_DEBUG */
    554  1.158     ozaki 
    555    1.1       cgd /*
    556  1.146     ozaki  * Packet routing routines. If success, refcnt of a returned rtentry
    557  1.146     ozaki  * will be incremented. The caller has to rtfree it by itself.
    558    1.1       cgd  */
    559    1.1       cgd struct rtentry *
    560  1.183     ozaki rtalloc1_locked(const struct sockaddr *dst, int report, bool wait_ok)
    561    1.1       cgd {
    562  1.158     ozaki 	rtbl_t *rtbl;
    563   1.36  augustss 	struct rtentry *rt;
    564  1.159  christos 	int s;
    565    1.1       cgd 
    566  1.188     ozaki #ifdef NET_MPSAFE
    567  1.183     ozaki retry:
    568  1.188     ozaki #endif
    569  1.159  christos 	s = splsoftnet();
    570  1.158     ozaki 	rtbl = rt_gettable(dst->sa_family);
    571  1.159  christos 	if (rtbl == NULL)
    572  1.158     ozaki 		goto miss;
    573  1.158     ozaki 
    574  1.158     ozaki 	rt = rt_matchaddr(rtbl, dst);
    575  1.159  christos 	if (rt == NULL)
    576  1.158     ozaki 		goto miss;
    577  1.159  christos 
    578  1.183     ozaki 	if (!ISSET(rt->rt_flags, RTF_UP))
    579  1.183     ozaki 		goto miss;
    580  1.183     ozaki 
    581  1.188     ozaki #ifdef NET_MPSAFE
    582  1.183     ozaki 	if (ISSET(rt->rt_flags, RTF_UPDATING) &&
    583  1.183     ozaki 	    /* XXX updater should be always able to acquire */
    584  1.183     ozaki 	    curlwp != rt_update_global.lwp) {
    585  1.183     ozaki 		bool need_lock = false;
    586  1.183     ozaki 		if (!wait_ok || !rt_wait_ok())
    587  1.183     ozaki 			goto miss;
    588  1.183     ozaki 		RT_UNLOCK();
    589  1.183     ozaki 		splx(s);
    590  1.183     ozaki 
    591  1.183     ozaki 		/* XXX need more proper solution */
    592  1.183     ozaki 		if (RTCACHE_WLOCKED()) {
    593  1.183     ozaki 			RTCACHE_UNLOCK();
    594  1.183     ozaki 			need_lock = true;
    595  1.183     ozaki 		}
    596  1.183     ozaki 
    597  1.183     ozaki 		/* We can wait until the update is complete */
    598  1.183     ozaki 		rt_update_wait();
    599  1.183     ozaki 
    600  1.183     ozaki 		if (need_lock)
    601  1.183     ozaki 			RTCACHE_WLOCK();
    602  1.183     ozaki 		goto retry;
    603  1.183     ozaki 	}
    604  1.188     ozaki #endif /* NET_MPSAFE */
    605  1.183     ozaki 
    606  1.183     ozaki 	rt_ref(rt);
    607  1.183     ozaki 	RT_REFCNT_TRACE(rt);
    608  1.158     ozaki 
    609  1.159  christos 	splx(s);
    610  1.159  christos 	return rt;
    611  1.158     ozaki miss:
    612  1.159  christos 	rtstat.rts_unreach++;
    613  1.158     ozaki 	if (report) {
    614  1.159  christos 		struct rt_addrinfo info;
    615  1.159  christos 
    616  1.160  christos 		memset(&info, 0, sizeof(info));
    617  1.158     ozaki 		info.rti_info[RTAX_DST] = dst;
    618  1.159  christos 		rt_missmsg(RTM_MISS, &info, 0, 0);
    619    1.1       cgd 	}
    620    1.1       cgd 	splx(s);
    621  1.159  christos 	return NULL;
    622    1.1       cgd }
    623    1.1       cgd 
    624  1.183     ozaki struct rtentry *
    625  1.183     ozaki rtalloc1(const struct sockaddr *dst, int report)
    626  1.183     ozaki {
    627  1.183     ozaki 	struct rtentry *rt;
    628  1.183     ozaki 
    629  1.183     ozaki 	RT_RLOCK();
    630  1.183     ozaki 	rt = rtalloc1_locked(dst, report, true);
    631  1.183     ozaki 	RT_UNLOCK();
    632  1.183     ozaki 
    633  1.183     ozaki 	return rt;
    634  1.183     ozaki }
    635  1.183     ozaki 
    636  1.151     ozaki static void
    637  1.183     ozaki rt_ref(struct rtentry *rt)
    638  1.183     ozaki {
    639  1.183     ozaki 
    640  1.183     ozaki 	KASSERT(rt->rt_refcnt >= 0);
    641  1.183     ozaki 	atomic_inc_uint(&rt->rt_refcnt);
    642  1.183     ozaki }
    643  1.183     ozaki 
    644  1.183     ozaki void
    645  1.183     ozaki rt_unref(struct rtentry *rt)
    646  1.183     ozaki {
    647  1.183     ozaki 
    648  1.183     ozaki 	KASSERT(rt != NULL);
    649  1.183     ozaki 	KASSERTMSG(rt->rt_refcnt > 0, "refcnt=%d", rt->rt_refcnt);
    650  1.183     ozaki 
    651  1.183     ozaki 	atomic_dec_uint(&rt->rt_refcnt);
    652  1.183     ozaki 	if (!ISSET(rt->rt_flags, RTF_UP) || ISSET(rt->rt_flags, RTF_UPDATING)) {
    653  1.183     ozaki 		mutex_enter(&rt_free_global.lock);
    654  1.183     ozaki 		cv_broadcast(&rt->rt_cv);
    655  1.183     ozaki 		mutex_exit(&rt_free_global.lock);
    656  1.183     ozaki 	}
    657  1.183     ozaki }
    658  1.183     ozaki 
    659  1.183     ozaki static bool
    660  1.183     ozaki rt_wait_ok(void)
    661  1.151     ozaki {
    662  1.151     ozaki 
    663  1.183     ozaki 	KASSERT(!cpu_intr_p());
    664  1.183     ozaki 	return !cpu_softintr_p();
    665  1.183     ozaki }
    666  1.151     ozaki 
    667  1.183     ozaki void
    668  1.183     ozaki rt_wait_refcnt(const char *title, struct rtentry *rt, int cnt)
    669  1.183     ozaki {
    670  1.183     ozaki 	mutex_enter(&rt_free_global.lock);
    671  1.183     ozaki 	while (rt->rt_refcnt > cnt) {
    672  1.183     ozaki 		dlog(LOG_DEBUG, "%s: %s waiting (refcnt=%d)\n",
    673  1.183     ozaki 		    __func__, title, rt->rt_refcnt);
    674  1.183     ozaki 		cv_wait(&rt->rt_cv, &rt_free_global.lock);
    675  1.183     ozaki 		dlog(LOG_DEBUG, "%s: %s waited (refcnt=%d)\n",
    676  1.183     ozaki 		    __func__, title, rt->rt_refcnt);
    677  1.183     ozaki 	}
    678  1.183     ozaki 	mutex_exit(&rt_free_global.lock);
    679  1.151     ozaki }
    680  1.151     ozaki 
    681    1.9   mycroft void
    682  1.183     ozaki rt_wait_psref(struct rtentry *rt)
    683  1.183     ozaki {
    684  1.183     ozaki 
    685  1.183     ozaki 	psref_target_destroy(&rt->rt_psref, rt_psref_class);
    686  1.183     ozaki 	psref_target_init(&rt->rt_psref, rt_psref_class);
    687  1.183     ozaki }
    688  1.183     ozaki 
    689  1.183     ozaki static void
    690  1.183     ozaki _rt_free(struct rtentry *rt)
    691    1.1       cgd {
    692   1.36  augustss 	struct ifaddr *ifa;
    693   1.10   mycroft 
    694  1.183     ozaki 	/*
    695  1.183     ozaki 	 * Need to avoid a deadlock on rt_wait_refcnt of update
    696  1.183     ozaki 	 * and a conflict on psref_target_destroy of update.
    697  1.183     ozaki 	 */
    698  1.188     ozaki #ifdef NET_MPSAFE
    699  1.183     ozaki 	rt_update_wait();
    700  1.188     ozaki #endif
    701  1.183     ozaki 
    702  1.183     ozaki 	RT_REFCNT_TRACE(rt);
    703  1.183     ozaki 	KASSERTMSG(rt->rt_refcnt >= 0, "refcnt=%d", rt->rt_refcnt);
    704  1.183     ozaki 	rt_wait_refcnt("free", rt, 0);
    705  1.185     ozaki #ifdef NET_MPSAFE
    706  1.183     ozaki 	psref_target_destroy(&rt->rt_psref, rt_psref_class);
    707  1.185     ozaki #endif
    708  1.183     ozaki 
    709  1.183     ozaki 	rt_assert_inactive(rt);
    710  1.183     ozaki 	rttrash--;
    711  1.183     ozaki 	ifa = rt->rt_ifa;
    712  1.183     ozaki 	rt->rt_ifa = NULL;
    713  1.183     ozaki 	ifafree(ifa);
    714  1.183     ozaki 	rt->rt_ifp = NULL;
    715  1.183     ozaki 	cv_destroy(&rt->rt_cv);
    716  1.183     ozaki 	rt_destroy(rt);
    717  1.183     ozaki 	pool_put(&rtentry_pool, rt);
    718  1.183     ozaki }
    719  1.183     ozaki 
    720  1.183     ozaki static void
    721  1.183     ozaki rt_free_work(struct work *wk, void *arg)
    722  1.183     ozaki {
    723  1.183     ozaki 	int i;
    724  1.183     ozaki 	struct rtentry *rt;
    725  1.183     ozaki 
    726  1.183     ozaki restart:
    727  1.183     ozaki 	mutex_enter(&rt_free_global.lock);
    728  1.183     ozaki 	for (i = 0; i < sizeof(rt_free_global.queue); i++) {
    729  1.183     ozaki 		if (rt_free_global.queue[i] == NULL)
    730  1.183     ozaki 			continue;
    731  1.183     ozaki 		rt = rt_free_global.queue[i];
    732  1.183     ozaki 		rt_free_global.queue[i] = NULL;
    733  1.183     ozaki 		mutex_exit(&rt_free_global.lock);
    734  1.183     ozaki 
    735  1.183     ozaki 		atomic_dec_uint(&rt->rt_refcnt);
    736  1.183     ozaki 		_rt_free(rt);
    737  1.183     ozaki 		goto restart;
    738  1.183     ozaki 	}
    739  1.183     ozaki 	mutex_exit(&rt_free_global.lock);
    740  1.183     ozaki }
    741  1.183     ozaki 
    742  1.183     ozaki void
    743  1.183     ozaki rt_free(struct rtentry *rt)
    744  1.183     ozaki {
    745  1.183     ozaki 
    746  1.132     rmind 	KASSERT(rt->rt_refcnt > 0);
    747  1.183     ozaki 	if (!rt_wait_ok()) {
    748  1.183     ozaki 		int i;
    749  1.183     ozaki 		mutex_enter(&rt_free_global.lock);
    750  1.183     ozaki 		for (i = 0; i < sizeof(rt_free_global.queue); i++) {
    751  1.183     ozaki 			if (rt_free_global.queue[i] == NULL) {
    752  1.183     ozaki 				rt_free_global.queue[i] = rt;
    753  1.183     ozaki 				break;
    754  1.183     ozaki 			}
    755  1.183     ozaki 		}
    756  1.183     ozaki 		KASSERT(i < sizeof(rt_free_global.queue));
    757  1.183     ozaki 		rt_ref(rt);
    758  1.183     ozaki 		mutex_exit(&rt_free_global.lock);
    759  1.183     ozaki 		workqueue_enqueue(rt_free_global.wq, &rt_free_global.wk, NULL);
    760  1.183     ozaki 	} else {
    761  1.183     ozaki 		atomic_dec_uint(&rt->rt_refcnt);
    762  1.183     ozaki 		_rt_free(rt);
    763  1.183     ozaki 	}
    764  1.183     ozaki }
    765  1.183     ozaki 
    766  1.188     ozaki #ifdef NET_MPSAFE
    767  1.183     ozaki static void
    768  1.183     ozaki rt_update_wait(void)
    769  1.183     ozaki {
    770  1.132     rmind 
    771  1.183     ozaki 	mutex_enter(&rt_update_global.lock);
    772  1.183     ozaki 	while (rt_update_global.ongoing) {
    773  1.183     ozaki 		dlog(LOG_DEBUG, "%s: waiting lwp=%p\n", __func__, curlwp);
    774  1.183     ozaki 		cv_wait(&rt_update_global.cv, &rt_update_global.lock);
    775  1.183     ozaki 		dlog(LOG_DEBUG, "%s: waited lwp=%p\n", __func__, curlwp);
    776    1.1       cgd 	}
    777  1.183     ozaki 	mutex_exit(&rt_update_global.lock);
    778  1.183     ozaki }
    779  1.188     ozaki #endif
    780  1.183     ozaki 
    781  1.183     ozaki int
    782  1.183     ozaki rt_update_prepare(struct rtentry *rt)
    783  1.183     ozaki {
    784  1.183     ozaki 
    785  1.183     ozaki 	dlog(LOG_DEBUG, "%s: updating rt=%p lwp=%p\n", __func__, rt, curlwp);
    786  1.183     ozaki 
    787  1.183     ozaki 	RTCACHE_WLOCK();
    788  1.183     ozaki 	RT_WLOCK();
    789  1.183     ozaki 	/* If the entry is being destroyed, don't proceed the update. */
    790  1.183     ozaki 	if (!ISSET(rt->rt_flags, RTF_UP)) {
    791  1.183     ozaki 		RT_UNLOCK();
    792  1.183     ozaki 		RTCACHE_UNLOCK();
    793  1.183     ozaki 		return -1;
    794  1.183     ozaki 	}
    795  1.183     ozaki 	rt->rt_flags |= RTF_UPDATING;
    796  1.183     ozaki 	RT_UNLOCK();
    797  1.183     ozaki 	RTCACHE_UNLOCK();
    798  1.183     ozaki 
    799  1.183     ozaki 	mutex_enter(&rt_update_global.lock);
    800  1.183     ozaki 	while (rt_update_global.ongoing) {
    801  1.183     ozaki 		dlog(LOG_DEBUG, "%s: waiting ongoing updating rt=%p lwp=%p\n",
    802  1.183     ozaki 		    __func__, rt, curlwp);
    803  1.183     ozaki 		cv_wait(&rt_update_global.cv, &rt_update_global.lock);
    804  1.183     ozaki 		dlog(LOG_DEBUG, "%s: waited ongoing updating rt=%p lwp=%p\n",
    805  1.183     ozaki 		    __func__, rt, curlwp);
    806  1.183     ozaki 	}
    807  1.183     ozaki 	rt_update_global.ongoing = true;
    808  1.183     ozaki 	/* XXX need it to avoid rt_update_wait by updater itself. */
    809  1.183     ozaki 	rt_update_global.lwp = curlwp;
    810  1.183     ozaki 	mutex_exit(&rt_update_global.lock);
    811  1.183     ozaki 
    812  1.183     ozaki 	rt_wait_refcnt("update", rt, 1);
    813  1.183     ozaki 	rt_wait_psref(rt);
    814  1.183     ozaki 
    815  1.183     ozaki 	return 0;
    816  1.183     ozaki }
    817  1.183     ozaki 
    818  1.183     ozaki void
    819  1.183     ozaki rt_update_finish(struct rtentry *rt)
    820  1.183     ozaki {
    821  1.183     ozaki 
    822  1.183     ozaki 	RTCACHE_WLOCK();
    823  1.183     ozaki 	RT_WLOCK();
    824  1.183     ozaki 	rt->rt_flags &= ~RTF_UPDATING;
    825  1.183     ozaki 	RT_UNLOCK();
    826  1.183     ozaki 	RTCACHE_UNLOCK();
    827  1.183     ozaki 
    828  1.183     ozaki 	mutex_enter(&rt_update_global.lock);
    829  1.183     ozaki 	rt_update_global.ongoing = false;
    830  1.183     ozaki 	rt_update_global.lwp = NULL;
    831  1.183     ozaki 	cv_broadcast(&rt_update_global.cv);
    832  1.183     ozaki 	mutex_exit(&rt_update_global.lock);
    833  1.183     ozaki 
    834  1.183     ozaki 	dlog(LOG_DEBUG, "%s: updated rt=%p lwp=%p\n", __func__, rt, curlwp);
    835    1.1       cgd }
    836    1.1       cgd 
    837    1.1       cgd /*
    838    1.1       cgd  * Force a routing table entry to the specified
    839    1.1       cgd  * destination to go through the given gateway.
    840    1.1       cgd  * Normally called as a result of a routing redirect
    841    1.1       cgd  * message from the network layer.
    842    1.1       cgd  *
    843   1.13   mycroft  * N.B.: must be called at splsoftnet
    844    1.1       cgd  */
    845   1.14  christos void
    846   1.60      matt rtredirect(const struct sockaddr *dst, const struct sockaddr *gateway,
    847   1.60      matt 	const struct sockaddr *netmask, int flags, const struct sockaddr *src,
    848   1.60      matt 	struct rtentry **rtp)
    849    1.1       cgd {
    850   1.36  augustss 	struct rtentry *rt;
    851    1.1       cgd 	int error = 0;
    852  1.121    dyoung 	uint64_t *stat = NULL;
    853   1.10   mycroft 	struct rt_addrinfo info;
    854   1.10   mycroft 	struct ifaddr *ifa;
    855  1.173     ozaki 	struct psref psref;
    856    1.1       cgd 
    857    1.1       cgd 	/* verify the gateway is directly reachable */
    858  1.173     ozaki 	if ((ifa = ifa_ifwithnet_psref(gateway, &psref)) == NULL) {
    859    1.1       cgd 		error = ENETUNREACH;
    860    1.8       cgd 		goto out;
    861    1.1       cgd 	}
    862    1.1       cgd 	rt = rtalloc1(dst, 0);
    863    1.1       cgd 	/*
    864    1.1       cgd 	 * If the redirect isn't from our current router for this dst,
    865    1.1       cgd 	 * it's either old or wrong.  If it redirects us to ourselves,
    866    1.1       cgd 	 * we have a routing loop, perhaps as a result of an interface
    867    1.1       cgd 	 * going down recently.
    868    1.1       cgd 	 */
    869   1.10   mycroft 	if (!(flags & RTF_DONE) && rt &&
    870  1.115      yamt 	     (sockaddr_cmp(src, rt->rt_gateway) != 0 || rt->rt_ifa != ifa))
    871    1.1       cgd 		error = EINVAL;
    872  1.173     ozaki 	else {
    873  1.173     ozaki 		int s = pserialize_read_enter();
    874  1.173     ozaki 		struct ifaddr *_ifa;
    875  1.173     ozaki 
    876  1.173     ozaki 		_ifa = ifa_ifwithaddr(gateway);
    877  1.173     ozaki 		if (_ifa != NULL)
    878  1.173     ozaki 			error = EHOSTUNREACH;
    879  1.173     ozaki 		pserialize_read_exit(s);
    880  1.173     ozaki 	}
    881    1.1       cgd 	if (error)
    882    1.1       cgd 		goto done;
    883    1.1       cgd 	/*
    884    1.1       cgd 	 * Create a new entry if we just got back a wildcard entry
    885   1.33     soren 	 * or the lookup failed.  This is necessary for hosts
    886    1.1       cgd 	 * which use routing redirects generated by smart gateways
    887    1.1       cgd 	 * to dynamically build the routing tables.
    888    1.1       cgd 	 */
    889   1.95    dyoung 	if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
    890    1.1       cgd 		goto create;
    891    1.1       cgd 	/*
    892    1.1       cgd 	 * Don't listen to the redirect if it's
    893   1.65     perry 	 * for a route to an interface.
    894    1.1       cgd 	 */
    895    1.1       cgd 	if (rt->rt_flags & RTF_GATEWAY) {
    896    1.1       cgd 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
    897    1.1       cgd 			/*
    898    1.1       cgd 			 * Changing from route to net => route to host.
    899    1.1       cgd 			 * Create new route, rather than smashing route to net.
    900    1.1       cgd 			 */
    901    1.1       cgd 		create:
    902   1.95    dyoung 			if (rt != NULL)
    903  1.183     ozaki 				rt_unref(rt);
    904    1.1       cgd 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
    905  1.122    kefren 			memset(&info, 0, sizeof(info));
    906   1.39    itojun 			info.rti_info[RTAX_DST] = dst;
    907   1.39    itojun 			info.rti_info[RTAX_GATEWAY] = gateway;
    908   1.39    itojun 			info.rti_info[RTAX_NETMASK] = netmask;
    909   1.39    itojun 			info.rti_ifa = ifa;
    910   1.39    itojun 			info.rti_flags = flags;
    911   1.39    itojun 			rt = NULL;
    912   1.39    itojun 			error = rtrequest1(RTM_ADD, &info, &rt);
    913   1.39    itojun 			if (rt != NULL)
    914   1.39    itojun 				flags = rt->rt_flags;
    915    1.1       cgd 			stat = &rtstat.rts_dynamic;
    916    1.1       cgd 		} else {
    917    1.1       cgd 			/*
    918    1.1       cgd 			 * Smash the current notion of the gateway to
    919    1.1       cgd 			 * this destination.  Should check about netmask!!!
    920    1.1       cgd 			 */
    921  1.190     ozaki #ifdef NET_MPSAFE
    922  1.190     ozaki 			KASSERT(!cpu_softintr_p());
    923  1.190     ozaki 
    924  1.190     ozaki 			error = rt_update_prepare(rt);
    925  1.164     ozaki 			if (error == 0) {
    926  1.190     ozaki #endif
    927  1.190     ozaki 				error = rt_setgate(rt, gateway);
    928  1.190     ozaki 				if (error == 0) {
    929  1.190     ozaki 					rt->rt_flags |= RTF_MODIFIED;
    930  1.190     ozaki 					flags |= RTF_MODIFIED;
    931  1.190     ozaki 				}
    932  1.190     ozaki #ifdef NET_MPSAFE
    933  1.190     ozaki 				rt_update_finish(rt);
    934  1.190     ozaki 			} else {
    935  1.190     ozaki 				/*
    936  1.190     ozaki 				 * If error != 0, the rtentry is being
    937  1.190     ozaki 				 * destroyed, so doing nothing doesn't
    938  1.190     ozaki 				 * matter.
    939  1.190     ozaki 				 */
    940  1.164     ozaki 			}
    941  1.190     ozaki #endif
    942   1.10   mycroft 			stat = &rtstat.rts_newgateway;
    943    1.1       cgd 		}
    944    1.1       cgd 	} else
    945    1.1       cgd 		error = EHOSTUNREACH;
    946    1.1       cgd done:
    947    1.1       cgd 	if (rt) {
    948   1.95    dyoung 		if (rtp != NULL && !error)
    949    1.1       cgd 			*rtp = rt;
    950    1.1       cgd 		else
    951  1.183     ozaki 			rt_unref(rt);
    952    1.1       cgd 	}
    953    1.8       cgd out:
    954    1.1       cgd 	if (error)
    955    1.1       cgd 		rtstat.rts_badredirect++;
    956    1.8       cgd 	else if (stat != NULL)
    957    1.8       cgd 		(*stat)++;
    958   1.95    dyoung 	memset(&info, 0, sizeof(info));
    959   1.10   mycroft 	info.rti_info[RTAX_DST] = dst;
    960   1.10   mycroft 	info.rti_info[RTAX_GATEWAY] = gateway;
    961   1.10   mycroft 	info.rti_info[RTAX_NETMASK] = netmask;
    962   1.10   mycroft 	info.rti_info[RTAX_AUTHOR] = src;
    963   1.10   mycroft 	rt_missmsg(RTM_REDIRECT, &info, flags, error);
    964  1.173     ozaki 	ifa_release(ifa, &psref);
    965    1.1       cgd }
    966    1.1       cgd 
    967    1.1       cgd /*
    968  1.146     ozaki  * Delete a route and generate a message.
    969  1.146     ozaki  * It doesn't free a passed rt.
    970   1.40    itojun  */
    971   1.40    itojun static int
    972   1.60      matt rtdeletemsg(struct rtentry *rt)
    973   1.40    itojun {
    974   1.40    itojun 	int error;
    975   1.40    itojun 	struct rt_addrinfo info;
    976  1.180     ozaki 	struct rtentry *retrt;
    977   1.40    itojun 
    978   1.40    itojun 	/*
    979   1.40    itojun 	 * Request the new route so that the entry is not actually
    980   1.40    itojun 	 * deleted.  That will allow the information being reported to
    981   1.40    itojun 	 * be accurate (and consistent with route_output()).
    982   1.40    itojun 	 */
    983   1.95    dyoung 	memset(&info, 0, sizeof(info));
    984   1.94    dyoung 	info.rti_info[RTAX_DST] = rt_getkey(rt);
    985   1.40    itojun 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    986   1.40    itojun 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    987   1.40    itojun 	info.rti_flags = rt->rt_flags;
    988  1.180     ozaki 	error = rtrequest1(RTM_DELETE, &info, &retrt);
    989   1.40    itojun 
    990   1.40    itojun 	rt_missmsg(RTM_DELETE, &info, info.rti_flags, error);
    991   1.40    itojun 
    992   1.95    dyoung 	return error;
    993   1.40    itojun }
    994   1.40    itojun 
    995    1.1       cgd struct ifaddr *
    996  1.173     ozaki ifa_ifwithroute_psref(int flags, const struct sockaddr *dst,
    997  1.173     ozaki 	const struct sockaddr *gateway, struct psref *psref)
    998    1.1       cgd {
    999  1.173     ozaki 	struct ifaddr *ifa = NULL;
   1000  1.173     ozaki 
   1001    1.1       cgd 	if ((flags & RTF_GATEWAY) == 0) {
   1002    1.1       cgd 		/*
   1003    1.1       cgd 		 * If we are adding a route to an interface,
   1004    1.1       cgd 		 * and the interface is a pt to pt link
   1005    1.1       cgd 		 * we should search for the destination
   1006    1.1       cgd 		 * as our clue to the interface.  Otherwise
   1007    1.1       cgd 		 * we can use the local address.
   1008    1.1       cgd 		 */
   1009  1.127  christos 		if ((flags & RTF_HOST) && gateway->sa_family != AF_LINK)
   1010  1.173     ozaki 			ifa = ifa_ifwithdstaddr_psref(dst, psref);
   1011   1.68  christos 		if (ifa == NULL)
   1012  1.173     ozaki 			ifa = ifa_ifwithaddr_psref(gateway, psref);
   1013    1.1       cgd 	} else {
   1014    1.1       cgd 		/*
   1015    1.1       cgd 		 * If we are adding a route to a remote net
   1016    1.1       cgd 		 * or host, the gateway may still be on the
   1017    1.1       cgd 		 * other end of a pt to pt link.
   1018    1.1       cgd 		 */
   1019  1.173     ozaki 		ifa = ifa_ifwithdstaddr_psref(gateway, psref);
   1020    1.1       cgd 	}
   1021   1.68  christos 	if (ifa == NULL)
   1022  1.173     ozaki 		ifa = ifa_ifwithnet_psref(gateway, psref);
   1023   1.68  christos 	if (ifa == NULL) {
   1024  1.173     ozaki 		int s;
   1025  1.173     ozaki 		struct rtentry *rt;
   1026  1.173     ozaki 
   1027  1.189     ozaki 		/* XXX we cannot call rtalloc1 if holding the rt lock */
   1028  1.189     ozaki 		if (RT_LOCKED())
   1029  1.189     ozaki 			rt = rtalloc1_locked(dst, 0, true);
   1030  1.189     ozaki 		else
   1031  1.189     ozaki 			rt = rtalloc1(dst, 0);
   1032   1.68  christos 		if (rt == NULL)
   1033   1.68  christos 			return NULL;
   1034  1.173     ozaki 		/*
   1035  1.173     ozaki 		 * Just in case. May not need to do this workaround.
   1036  1.173     ozaki 		 * Revisit when working on rtentry MP-ification.
   1037  1.173     ozaki 		 */
   1038  1.173     ozaki 		s = pserialize_read_enter();
   1039  1.173     ozaki 		IFADDR_READER_FOREACH(ifa, rt->rt_ifp) {
   1040  1.173     ozaki 			if (ifa == rt->rt_ifa)
   1041  1.173     ozaki 				break;
   1042  1.173     ozaki 		}
   1043  1.173     ozaki 		if (ifa != NULL)
   1044  1.173     ozaki 			ifa_acquire(ifa, psref);
   1045  1.173     ozaki 		pserialize_read_exit(s);
   1046  1.183     ozaki 		rt_unref(rt);
   1047  1.146     ozaki 		if (ifa == NULL)
   1048   1.68  christos 			return NULL;
   1049    1.1       cgd 	}
   1050    1.1       cgd 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
   1051  1.173     ozaki 		struct ifaddr *nifa;
   1052  1.173     ozaki 		int s;
   1053  1.173     ozaki 
   1054  1.173     ozaki 		s = pserialize_read_enter();
   1055  1.173     ozaki 		nifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
   1056  1.173     ozaki 		if (nifa != NULL) {
   1057  1.173     ozaki 			ifa_release(ifa, psref);
   1058  1.173     ozaki 			ifa_acquire(nifa, psref);
   1059  1.173     ozaki 			ifa = nifa;
   1060  1.173     ozaki 		}
   1061  1.173     ozaki 		pserialize_read_exit(s);
   1062    1.1       cgd 	}
   1063   1.95    dyoung 	return ifa;
   1064    1.1       cgd }
   1065    1.1       cgd 
   1066  1.146     ozaki /*
   1067  1.146     ozaki  * If it suceeds and ret_nrt isn't NULL, refcnt of ret_nrt is incremented.
   1068  1.146     ozaki  * The caller has to rtfree it by itself.
   1069  1.146     ozaki  */
   1070    1.9   mycroft int
   1071   1.60      matt rtrequest(int req, const struct sockaddr *dst, const struct sockaddr *gateway,
   1072   1.60      matt 	const struct sockaddr *netmask, int flags, struct rtentry **ret_nrt)
   1073    1.1       cgd {
   1074   1.39    itojun 	struct rt_addrinfo info;
   1075   1.39    itojun 
   1076   1.44   thorpej 	memset(&info, 0, sizeof(info));
   1077   1.39    itojun 	info.rti_flags = flags;
   1078   1.39    itojun 	info.rti_info[RTAX_DST] = dst;
   1079   1.39    itojun 	info.rti_info[RTAX_GATEWAY] = gateway;
   1080   1.39    itojun 	info.rti_info[RTAX_NETMASK] = netmask;
   1081   1.39    itojun 	return rtrequest1(req, &info, ret_nrt);
   1082   1.39    itojun }
   1083   1.39    itojun 
   1084  1.146     ozaki /*
   1085  1.146     ozaki  * It's a utility function to add/remove a route to/from the routing table
   1086  1.146     ozaki  * and tell user processes the addition/removal on success.
   1087  1.146     ozaki  */
   1088  1.146     ozaki int
   1089  1.146     ozaki rtrequest_newmsg(const int req, const struct sockaddr *dst,
   1090  1.146     ozaki 	const struct sockaddr *gateway, const struct sockaddr *netmask,
   1091  1.146     ozaki 	const int flags)
   1092  1.146     ozaki {
   1093  1.146     ozaki 	int error;
   1094  1.146     ozaki 	struct rtentry *ret_nrt = NULL;
   1095  1.146     ozaki 
   1096  1.146     ozaki 	KASSERT(req == RTM_ADD || req == RTM_DELETE);
   1097  1.146     ozaki 
   1098  1.146     ozaki 	error = rtrequest(req, dst, gateway, netmask, flags, &ret_nrt);
   1099  1.146     ozaki 	if (error != 0)
   1100  1.146     ozaki 		return error;
   1101  1.146     ozaki 
   1102  1.146     ozaki 	KASSERT(ret_nrt != NULL);
   1103  1.146     ozaki 
   1104  1.146     ozaki 	rt_newmsg(req, ret_nrt); /* tell user process */
   1105  1.183     ozaki 	if (req == RTM_DELETE)
   1106  1.183     ozaki 		rt_free(ret_nrt);
   1107  1.183     ozaki 	else
   1108  1.183     ozaki 		rt_unref(ret_nrt);
   1109  1.146     ozaki 
   1110  1.146     ozaki 	return 0;
   1111  1.146     ozaki }
   1112  1.146     ozaki 
   1113  1.173     ozaki struct ifnet *
   1114  1.173     ozaki rt_getifp(struct rt_addrinfo *info, struct psref *psref)
   1115   1.39    itojun {
   1116   1.68  christos 	const struct sockaddr *ifpaddr = info->rti_info[RTAX_IFP];
   1117   1.39    itojun 
   1118  1.173     ozaki 	if (info->rti_ifp != NULL)
   1119  1.173     ozaki 		return NULL;
   1120   1.39    itojun 	/*
   1121   1.39    itojun 	 * ifp may be specified by sockaddr_dl when protocol address
   1122   1.39    itojun 	 * is ambiguous
   1123   1.39    itojun 	 */
   1124  1.173     ozaki 	if (ifpaddr != NULL && ifpaddr->sa_family == AF_LINK) {
   1125  1.173     ozaki 		struct ifaddr *ifa;
   1126  1.173     ozaki 		int s = pserialize_read_enter();
   1127  1.173     ozaki 
   1128  1.173     ozaki 		ifa = ifa_ifwithnet(ifpaddr);
   1129  1.173     ozaki 		if (ifa != NULL)
   1130  1.173     ozaki 			info->rti_ifp = if_get_byindex(ifa->ifa_ifp->if_index,
   1131  1.173     ozaki 			    psref);
   1132  1.173     ozaki 		pserialize_read_exit(s);
   1133   1.39    itojun 	}
   1134  1.173     ozaki 
   1135  1.173     ozaki 	return info->rti_ifp;
   1136  1.173     ozaki }
   1137  1.173     ozaki 
   1138  1.173     ozaki struct ifaddr *
   1139  1.173     ozaki rt_getifa(struct rt_addrinfo *info, struct psref *psref)
   1140  1.173     ozaki {
   1141  1.174     ozaki 	struct ifaddr *ifa = NULL;
   1142  1.173     ozaki 	const struct sockaddr *dst = info->rti_info[RTAX_DST];
   1143  1.173     ozaki 	const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
   1144  1.173     ozaki 	const struct sockaddr *ifaaddr = info->rti_info[RTAX_IFA];
   1145  1.173     ozaki 	int flags = info->rti_flags;
   1146  1.173     ozaki 	const struct sockaddr *sa;
   1147  1.173     ozaki 
   1148  1.173     ozaki 	if (info->rti_ifa == NULL && ifaaddr != NULL) {
   1149  1.173     ozaki 		ifa = ifa_ifwithaddr_psref(ifaaddr, psref);
   1150  1.173     ozaki 		if (ifa != NULL)
   1151  1.173     ozaki 			goto got;
   1152  1.173     ozaki 	}
   1153  1.173     ozaki 
   1154  1.173     ozaki 	sa = ifaaddr != NULL ? ifaaddr :
   1155  1.173     ozaki 	    (gateway != NULL ? gateway : dst);
   1156  1.173     ozaki 	if (sa != NULL && info->rti_ifp != NULL)
   1157  1.173     ozaki 		ifa = ifaof_ifpforaddr_psref(sa, info->rti_ifp, psref);
   1158  1.173     ozaki 	else if (dst != NULL && gateway != NULL)
   1159  1.173     ozaki 		ifa = ifa_ifwithroute_psref(flags, dst, gateway, psref);
   1160  1.173     ozaki 	else if (sa != NULL)
   1161  1.173     ozaki 		ifa = ifa_ifwithroute_psref(flags, sa, sa, psref);
   1162  1.173     ozaki 	if (ifa == NULL)
   1163  1.173     ozaki 		return NULL;
   1164  1.173     ozaki got:
   1165  1.145       roy 	if (ifa->ifa_getifa != NULL) {
   1166  1.191     ozaki 		/* FIXME ifa_getifa is NOMPSAFE */
   1167  1.173     ozaki 		ifa = (*ifa->ifa_getifa)(ifa, dst);
   1168  1.145       roy 		if (ifa == NULL)
   1169  1.173     ozaki 			return NULL;
   1170  1.173     ozaki 		ifa_acquire(ifa, psref);
   1171  1.145       roy 	}
   1172  1.173     ozaki 	info->rti_ifa = ifa;
   1173   1.74    dyoung 	if (info->rti_ifp == NULL)
   1174   1.74    dyoung 		info->rti_ifp = ifa->ifa_ifp;
   1175  1.173     ozaki 	return ifa;
   1176   1.39    itojun }
   1177   1.39    itojun 
   1178  1.146     ozaki /*
   1179  1.146     ozaki  * If it suceeds and ret_nrt isn't NULL, refcnt of ret_nrt is incremented.
   1180  1.146     ozaki  * The caller has to rtfree it by itself.
   1181  1.146     ozaki  */
   1182   1.39    itojun int
   1183   1.60      matt rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
   1184   1.39    itojun {
   1185  1.173     ozaki 	int s = splsoftnet(), ss;
   1186  1.125    dyoung 	int error = 0, rc;
   1187  1.158     ozaki 	struct rtentry *rt;
   1188  1.125    dyoung 	rtbl_t *rtbl;
   1189  1.173     ozaki 	struct ifaddr *ifa = NULL, *ifa2 = NULL;
   1190   1.94    dyoung 	struct sockaddr_storage maskeddst;
   1191   1.68  christos 	const struct sockaddr *dst = info->rti_info[RTAX_DST];
   1192   1.68  christos 	const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
   1193   1.68  christos 	const struct sockaddr *netmask = info->rti_info[RTAX_NETMASK];
   1194   1.68  christos 	int flags = info->rti_flags;
   1195  1.173     ozaki 	struct psref psref_ifp, psref_ifa;
   1196  1.173     ozaki 	int bound = 0;
   1197  1.173     ozaki 	struct ifnet *ifp = NULL;
   1198  1.173     ozaki 	bool need_to_release_ifa = true;
   1199  1.183     ozaki 	bool need_unlock = true;
   1200    1.1       cgd #define senderr(x) { error = x ; goto bad; }
   1201    1.1       cgd 
   1202  1.183     ozaki 	RT_WLOCK();
   1203  1.183     ozaki 
   1204  1.173     ozaki 	bound = curlwp_bind();
   1205  1.125    dyoung 	if ((rtbl = rt_gettable(dst->sa_family)) == NULL)
   1206    1.1       cgd 		senderr(ESRCH);
   1207    1.1       cgd 	if (flags & RTF_HOST)
   1208   1.68  christos 		netmask = NULL;
   1209    1.1       cgd 	switch (req) {
   1210    1.1       cgd 	case RTM_DELETE:
   1211   1.63  christos 		if (netmask) {
   1212   1.94    dyoung 			rt_maskedcopy(dst, (struct sockaddr *)&maskeddst,
   1213   1.94    dyoung 			    netmask);
   1214   1.94    dyoung 			dst = (struct sockaddr *)&maskeddst;
   1215   1.63  christos 		}
   1216  1.125    dyoung 		if ((rt = rt_lookup(rtbl, dst, netmask)) == NULL)
   1217   1.41    itojun 			senderr(ESRCH);
   1218  1.125    dyoung 		if ((rt = rt_deladdr(rtbl, dst, netmask)) == NULL)
   1219    1.1       cgd 			senderr(ESRCH);
   1220   1.28       erh 		rt->rt_flags &= ~RTF_UP;
   1221  1.116       roy 		if ((ifa = rt->rt_ifa)) {
   1222  1.116       roy 			if (ifa->ifa_flags & IFA_ROUTE &&
   1223  1.116       roy 			    rt_ifa_connected(rt, ifa)) {
   1224  1.116       roy 				RT_DPRINTF("rt->_rt_key = %p, ifa = %p, "
   1225  1.116       roy 				    "deleted IFA_ROUTE\n",
   1226  1.116       roy 				    (void *)rt->_rt_key, (void *)ifa);
   1227  1.116       roy 				ifa->ifa_flags &= ~IFA_ROUTE;
   1228  1.116       roy 			}
   1229  1.116       roy 			if (ifa->ifa_rtrequest)
   1230  1.116       roy 				ifa->ifa_rtrequest(RTM_DELETE, rt, info);
   1231  1.173     ozaki 			ifa = NULL;
   1232  1.116       roy 		}
   1233    1.1       cgd 		rttrash++;
   1234  1.146     ozaki 		if (ret_nrt) {
   1235   1.10   mycroft 			*ret_nrt = rt;
   1236  1.183     ozaki 			rt_ref(rt);
   1237  1.183     ozaki 			RT_REFCNT_TRACE(rt);
   1238  1.183     ozaki 		}
   1239  1.183     ozaki 		RT_UNLOCK();
   1240  1.183     ozaki 		need_unlock = false;
   1241  1.183     ozaki 		rt_timer_remove_all(rt);
   1242  1.183     ozaki 		rtcache_clear_rtentry(dst->sa_family, rt);
   1243  1.183     ozaki 		if (ret_nrt == NULL) {
   1244  1.146     ozaki 			/* Adjust the refcount */
   1245  1.183     ozaki 			rt_ref(rt);
   1246  1.183     ozaki 			RT_REFCNT_TRACE(rt);
   1247  1.183     ozaki 			rt_free(rt);
   1248   1.10   mycroft 		}
   1249    1.1       cgd 		break;
   1250    1.1       cgd 
   1251    1.1       cgd 	case RTM_ADD:
   1252  1.173     ozaki 		if (info->rti_ifa == NULL) {
   1253  1.173     ozaki 			ifp = rt_getifp(info, &psref_ifp);
   1254  1.173     ozaki 			ifa = rt_getifa(info, &psref_ifa);
   1255  1.173     ozaki 			if (ifa == NULL)
   1256  1.173     ozaki 				senderr(ENETUNREACH);
   1257  1.173     ozaki 		} else {
   1258  1.173     ozaki 			/* Caller should have a reference of ifa */
   1259  1.173     ozaki 			ifa = info->rti_ifa;
   1260  1.173     ozaki 			need_to_release_ifa = false;
   1261  1.173     ozaki 		}
   1262   1.22   thorpej 		rt = pool_get(&rtentry_pool, PR_NOWAIT);
   1263   1.68  christos 		if (rt == NULL)
   1264    1.1       cgd 			senderr(ENOBUFS);
   1265  1.109    dyoung 		memset(rt, 0, sizeof(*rt));
   1266   1.10   mycroft 		rt->rt_flags = RTF_UP | flags;
   1267   1.18       kml 		LIST_INIT(&rt->rt_timer);
   1268  1.163     ozaki 
   1269  1.110    dyoung 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
   1270    1.1       cgd 		if (netmask) {
   1271   1.94    dyoung 			rt_maskedcopy(dst, (struct sockaddr *)&maskeddst,
   1272   1.94    dyoung 			    netmask);
   1273   1.96    dyoung 			rt_setkey(rt, (struct sockaddr *)&maskeddst, M_NOWAIT);
   1274   1.94    dyoung 		} else {
   1275   1.96    dyoung 			rt_setkey(rt, dst, M_NOWAIT);
   1276   1.94    dyoung 		}
   1277  1.163     ozaki 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
   1278  1.163     ozaki 		if (rt_getkey(rt) == NULL ||
   1279  1.163     ozaki 		    rt_setgate(rt, gateway) != 0) {
   1280  1.163     ozaki 			pool_put(&rtentry_pool, rt);
   1281  1.163     ozaki 			senderr(ENOBUFS);
   1282  1.163     ozaki 		}
   1283  1.163     ozaki 
   1284   1.74    dyoung 		rt_set_ifa(rt, ifa);
   1285  1.164     ozaki 		if (info->rti_info[RTAX_TAG] != NULL) {
   1286  1.164     ozaki 			const struct sockaddr *tag;
   1287  1.164     ozaki 			tag = rt_settag(rt, info->rti_info[RTAX_TAG]);
   1288  1.164     ozaki 			if (tag == NULL)
   1289  1.164     ozaki 				senderr(ENOBUFS);
   1290  1.164     ozaki 		}
   1291  1.110    dyoung 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
   1292  1.173     ozaki 
   1293  1.173     ozaki 		ss = pserialize_read_enter();
   1294  1.173     ozaki 		if (info->rti_info[RTAX_IFP] != NULL) {
   1295  1.173     ozaki 			ifa2 = ifa_ifwithnet(info->rti_info[RTAX_IFP]);
   1296  1.173     ozaki 			if (ifa2 != NULL)
   1297  1.173     ozaki 				rt->rt_ifp = ifa2->ifa_ifp;
   1298  1.173     ozaki 			else
   1299  1.173     ozaki 				rt->rt_ifp = ifa->ifa_ifp;
   1300  1.173     ozaki 		} else
   1301  1.122    kefren 			rt->rt_ifp = ifa->ifa_ifp;
   1302  1.173     ozaki 		pserialize_read_exit(ss);
   1303  1.183     ozaki 		cv_init(&rt->rt_cv, "rtentry");
   1304  1.183     ozaki 		psref_target_init(&rt->rt_psref, rt_psref_class);
   1305  1.173     ozaki 
   1306  1.110    dyoung 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
   1307  1.125    dyoung 		rc = rt_addaddr(rtbl, rt, netmask);
   1308  1.110    dyoung 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
   1309  1.125    dyoung 		if (rc != 0) {
   1310  1.173     ozaki 			ifafree(ifa); /* for rt_set_ifa above */
   1311  1.183     ozaki 			cv_destroy(&rt->rt_cv);
   1312   1.94    dyoung 			rt_destroy(rt);
   1313   1.40    itojun 			pool_put(&rtentry_pool, rt);
   1314  1.125    dyoung 			senderr(rc);
   1315   1.27      matt 		}
   1316  1.110    dyoung 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
   1317    1.1       cgd 		if (ifa->ifa_rtrequest)
   1318   1.39    itojun 			ifa->ifa_rtrequest(req, rt, info);
   1319  1.173     ozaki 		if (need_to_release_ifa)
   1320  1.173     ozaki 			ifa_release(ifa, &psref_ifa);
   1321  1.173     ozaki 		ifa = NULL;
   1322  1.173     ozaki 		if_put(ifp, &psref_ifp);
   1323  1.173     ozaki 		ifp = NULL;
   1324  1.110    dyoung 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
   1325    1.1       cgd 		if (ret_nrt) {
   1326    1.1       cgd 			*ret_nrt = rt;
   1327  1.183     ozaki 			rt_ref(rt);
   1328  1.183     ozaki 			RT_REFCNT_TRACE(rt);
   1329   1.41    itojun 		}
   1330  1.183     ozaki 		RT_UNLOCK();
   1331  1.183     ozaki 		need_unlock = false;
   1332   1.82    dyoung 		rtflushall(dst->sa_family);
   1333    1.1       cgd 		break;
   1334   1.92    dyoung 	case RTM_GET:
   1335   1.94    dyoung 		if (netmask != NULL) {
   1336   1.94    dyoung 			rt_maskedcopy(dst, (struct sockaddr *)&maskeddst,
   1337   1.94    dyoung 			    netmask);
   1338   1.94    dyoung 			dst = (struct sockaddr *)&maskeddst;
   1339   1.94    dyoung 		}
   1340  1.125    dyoung 		if ((rt = rt_lookup(rtbl, dst, netmask)) == NULL)
   1341   1.92    dyoung 			senderr(ESRCH);
   1342   1.92    dyoung 		if (ret_nrt != NULL) {
   1343   1.92    dyoung 			*ret_nrt = rt;
   1344  1.183     ozaki 			rt_ref(rt);
   1345  1.183     ozaki 			RT_REFCNT_TRACE(rt);
   1346   1.92    dyoung 		}
   1347   1.92    dyoung 		break;
   1348    1.1       cgd 	}
   1349    1.1       cgd bad:
   1350  1.173     ozaki 	if (need_to_release_ifa)
   1351  1.173     ozaki 		ifa_release(ifa, &psref_ifa);
   1352  1.173     ozaki 	if_put(ifp, &psref_ifp);
   1353  1.173     ozaki 	curlwp_bindx(bound);
   1354  1.183     ozaki 	if (need_unlock)
   1355  1.183     ozaki 		RT_UNLOCK();
   1356    1.1       cgd 	splx(s);
   1357   1.95    dyoung 	return error;
   1358    1.1       cgd }
   1359    1.1       cgd 
   1360   1.10   mycroft int
   1361   1.94    dyoung rt_setgate(struct rtentry *rt, const struct sockaddr *gate)
   1362   1.10   mycroft {
   1363  1.176     ozaki 	struct sockaddr *new, *old;
   1364   1.94    dyoung 
   1365   1.94    dyoung 	KASSERT(rt->_rt_key != NULL);
   1366  1.110    dyoung 	RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
   1367   1.94    dyoung 
   1368  1.176     ozaki 	new = sockaddr_dup(gate, M_ZERO | M_NOWAIT);
   1369  1.176     ozaki 	if (new == NULL)
   1370   1.94    dyoung 		return ENOMEM;
   1371  1.176     ozaki 
   1372  1.176     ozaki 	old = rt->rt_gateway;
   1373  1.176     ozaki 	rt->rt_gateway = new;
   1374  1.176     ozaki 	if (old != NULL)
   1375  1.176     ozaki 		sockaddr_free(old);
   1376  1.176     ozaki 
   1377   1.94    dyoung 	KASSERT(rt->_rt_key != NULL);
   1378  1.110    dyoung 	RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
   1379   1.94    dyoung 
   1380   1.10   mycroft 	if (rt->rt_flags & RTF_GATEWAY) {
   1381  1.183     ozaki 		struct rtentry *gwrt;
   1382  1.183     ozaki 
   1383  1.183     ozaki 		/* XXX we cannot call rtalloc1 if holding the rt lock */
   1384  1.183     ozaki 		if (RT_LOCKED())
   1385  1.183     ozaki 			gwrt = rtalloc1_locked(gate, 1, false);
   1386  1.183     ozaki 		else
   1387  1.183     ozaki 			gwrt = rtalloc1(gate, 1);
   1388   1.27      matt 		/*
   1389   1.27      matt 		 * If we switched gateways, grab the MTU from the new
   1390   1.47    itojun 		 * gateway route if the current MTU, if the current MTU is
   1391   1.47    itojun 		 * greater than the MTU of gateway.
   1392   1.47    itojun 		 * Note that, if the MTU of gateway is 0, we will reset the
   1393   1.47    itojun 		 * MTU of the route to run PMTUD again from scratch. XXX
   1394   1.27      matt 		 */
   1395  1.166     ozaki 		if (gwrt != NULL) {
   1396  1.166     ozaki 			KASSERT(gwrt->_rt_key != NULL);
   1397  1.166     ozaki 			RT_DPRINTF("gwrt->_rt_key = %p\n", gwrt->_rt_key);
   1398  1.166     ozaki 			if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
   1399  1.166     ozaki 			    rt->rt_rmx.rmx_mtu &&
   1400  1.166     ozaki 			    rt->rt_rmx.rmx_mtu > gwrt->rt_rmx.rmx_mtu) {
   1401  1.166     ozaki 				rt->rt_rmx.rmx_mtu = gwrt->rt_rmx.rmx_mtu;
   1402  1.166     ozaki 			}
   1403  1.183     ozaki 			rt_unref(gwrt);
   1404   1.27      matt 		}
   1405   1.10   mycroft 	}
   1406   1.94    dyoung 	KASSERT(rt->_rt_key != NULL);
   1407  1.110    dyoung 	RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
   1408   1.10   mycroft 	return 0;
   1409   1.10   mycroft }
   1410   1.10   mycroft 
   1411  1.141     ozaki static void
   1412   1.60      matt rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst,
   1413   1.60      matt 	const struct sockaddr *netmask)
   1414    1.1       cgd {
   1415   1.94    dyoung 	const char *netmaskp = &netmask->sa_data[0],
   1416   1.94    dyoung 	           *srcp = &src->sa_data[0];
   1417   1.94    dyoung 	char *dstp = &dst->sa_data[0];
   1418  1.126  christos 	const char *maskend = (char *)dst + MIN(netmask->sa_len, src->sa_len);
   1419  1.126  christos 	const char *srcend = (char *)dst + src->sa_len;
   1420   1.94    dyoung 
   1421   1.94    dyoung 	dst->sa_len = src->sa_len;
   1422   1.94    dyoung 	dst->sa_family = src->sa_family;
   1423   1.94    dyoung 
   1424   1.94    dyoung 	while (dstp < maskend)
   1425   1.94    dyoung 		*dstp++ = *srcp++ & *netmaskp++;
   1426   1.94    dyoung 	if (dstp < srcend)
   1427   1.94    dyoung 		memset(dstp, 0, (size_t)(srcend - dstp));
   1428    1.1       cgd }
   1429   1.10   mycroft 
   1430    1.1       cgd /*
   1431  1.135       roy  * Inform the routing socket of a route change.
   1432  1.135       roy  */
   1433  1.135       roy void
   1434  1.154     ozaki rt_newmsg(const int cmd, const struct rtentry *rt)
   1435  1.135       roy {
   1436  1.135       roy 	struct rt_addrinfo info;
   1437  1.135       roy 
   1438  1.135       roy 	memset((void *)&info, 0, sizeof(info));
   1439  1.135       roy 	info.rti_info[RTAX_DST] = rt_getkey(rt);
   1440  1.135       roy 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
   1441  1.135       roy 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
   1442  1.135       roy 	if (rt->rt_ifp) {
   1443  1.135       roy 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
   1444  1.135       roy 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
   1445  1.135       roy 	}
   1446  1.135       roy 
   1447  1.135       roy 	rt_missmsg(cmd, &info, rt->rt_flags, 0);
   1448  1.135       roy }
   1449  1.135       roy 
   1450  1.135       roy /*
   1451   1.29  sommerfe  * Set up or tear down a routing table entry, normally
   1452    1.1       cgd  * for an interface.
   1453    1.1       cgd  */
   1454    1.9   mycroft int
   1455   1.60      matt rtinit(struct ifaddr *ifa, int cmd, int flags)
   1456    1.1       cgd {
   1457   1.36  augustss 	struct rtentry *rt;
   1458   1.36  augustss 	struct sockaddr *dst, *odst;
   1459   1.94    dyoung 	struct sockaddr_storage maskeddst;
   1460   1.68  christos 	struct rtentry *nrt = NULL;
   1461    1.1       cgd 	int error;
   1462   1.39    itojun 	struct rt_addrinfo info;
   1463    1.1       cgd 
   1464    1.1       cgd 	dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
   1465    1.1       cgd 	if (cmd == RTM_DELETE) {
   1466    1.1       cgd 		if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
   1467   1.29  sommerfe 			/* Delete subnet route for this interface */
   1468   1.29  sommerfe 			odst = dst;
   1469   1.94    dyoung 			dst = (struct sockaddr *)&maskeddst;
   1470   1.29  sommerfe 			rt_maskedcopy(odst, dst, ifa->ifa_netmask);
   1471    1.1       cgd 		}
   1472   1.14  christos 		if ((rt = rtalloc1(dst, 0)) != NULL) {
   1473  1.146     ozaki 			if (rt->rt_ifa != ifa) {
   1474  1.183     ozaki 				rt_unref(rt);
   1475   1.85    dyoung 				return (flags & RTF_HOST) ? EHOSTUNREACH
   1476   1.85    dyoung 							: ENETUNREACH;
   1477  1.146     ozaki 			}
   1478  1.183     ozaki 			rt_unref(rt);
   1479    1.1       cgd 		}
   1480    1.1       cgd 	}
   1481   1.44   thorpej 	memset(&info, 0, sizeof(info));
   1482   1.39    itojun 	info.rti_ifa = ifa;
   1483   1.39    itojun 	info.rti_flags = flags | ifa->ifa_flags;
   1484   1.39    itojun 	info.rti_info[RTAX_DST] = dst;
   1485   1.39    itojun 	info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
   1486  1.158     ozaki 
   1487   1.39    itojun 	/*
   1488   1.39    itojun 	 * XXX here, it seems that we are assuming that ifa_netmask is NULL
   1489   1.39    itojun 	 * for RTF_HOST.  bsdi4 passes NULL explicitly (via intermediate
   1490   1.39    itojun 	 * variable) when RTF_HOST is 1.  still not sure if i can safely
   1491   1.39    itojun 	 * change it to meet bsdi4 behavior.
   1492   1.39    itojun 	 */
   1493  1.114    dyoung 	if (cmd != RTM_LLINFO_UPD)
   1494  1.114    dyoung 		info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
   1495  1.114    dyoung 	error = rtrequest1((cmd == RTM_LLINFO_UPD) ? RTM_GET : cmd, &info,
   1496  1.114    dyoung 	    &nrt);
   1497  1.153     ozaki 	if (error != 0)
   1498  1.146     ozaki 		return error;
   1499  1.146     ozaki 
   1500  1.153     ozaki 	rt = nrt;
   1501  1.183     ozaki 	RT_REFCNT_TRACE(rt);
   1502  1.146     ozaki 	switch (cmd) {
   1503  1.114    dyoung 	case RTM_DELETE:
   1504  1.146     ozaki 		rt_newmsg(cmd, rt);
   1505  1.183     ozaki 		rt_free(rt);
   1506  1.114    dyoung 		break;
   1507  1.114    dyoung 	case RTM_LLINFO_UPD:
   1508  1.114    dyoung 		if (cmd == RTM_LLINFO_UPD && ifa->ifa_rtrequest != NULL)
   1509  1.114    dyoung 			ifa->ifa_rtrequest(RTM_LLINFO_UPD, rt, &info);
   1510  1.146     ozaki 		rt_newmsg(RTM_CHANGE, rt);
   1511  1.183     ozaki 		rt_unref(rt);
   1512  1.114    dyoung 		break;
   1513  1.114    dyoung 	case RTM_ADD:
   1514  1.183     ozaki 		/*
   1515  1.187     ozaki 		 * FIXME NOMPSAFE: the rtentry is updated with the existence
   1516  1.183     ozaki 		 * of refeferences of it.
   1517  1.183     ozaki 		 */
   1518  1.183     ozaki 		/*
   1519  1.183     ozaki 		 * XXX it looks just reverting rt_ifa replaced by ifa_rtrequest
   1520  1.183     ozaki 		 * called via rtrequest1. Can we just prevent the replacement
   1521  1.183     ozaki 		 * somehow and remove the following code? And also doesn't
   1522  1.183     ozaki 		 * calling ifa_rtrequest(RTM_ADD) replace rt_ifa again?
   1523  1.183     ozaki 		 */
   1524   1.10   mycroft 		if (rt->rt_ifa != ifa) {
   1525   1.17  christos 			printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
   1526   1.17  christos 				rt->rt_ifa);
   1527  1.114    dyoung 			if (rt->rt_ifa->ifa_rtrequest != NULL) {
   1528  1.114    dyoung 				rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
   1529  1.114    dyoung 				    &info);
   1530  1.114    dyoung 			}
   1531   1.74    dyoung 			rt_replace_ifa(rt, ifa);
   1532   1.10   mycroft 			rt->rt_ifp = ifa->ifa_ifp;
   1533  1.114    dyoung 			if (ifa->ifa_rtrequest != NULL)
   1534  1.114    dyoung 				ifa->ifa_rtrequest(RTM_ADD, rt, &info);
   1535   1.10   mycroft 		}
   1536  1.146     ozaki 		rt_newmsg(cmd, rt);
   1537  1.183     ozaki 		rt_unref(rt);
   1538  1.183     ozaki 		RT_REFCNT_TRACE(rt);
   1539  1.114    dyoung 		break;
   1540    1.1       cgd 	}
   1541   1.85    dyoung 	return error;
   1542   1.18       kml }
   1543   1.18       kml 
   1544  1.136       roy /*
   1545  1.136       roy  * Create a local route entry for the address.
   1546  1.136       roy  * Announce the addition of the address and the route to the routing socket.
   1547  1.136       roy  */
   1548  1.136       roy int
   1549  1.136       roy rt_ifa_addlocal(struct ifaddr *ifa)
   1550  1.136       roy {
   1551  1.136       roy 	struct rtentry *rt;
   1552  1.136       roy 	int e;
   1553  1.136       roy 
   1554  1.136       roy 	/* If there is no loopback entry, allocate one. */
   1555  1.136       roy 	rt = rtalloc1(ifa->ifa_addr, 0);
   1556  1.158     ozaki #ifdef RT_DEBUG
   1557  1.158     ozaki 	if (rt != NULL)
   1558  1.158     ozaki 		dump_rt(rt);
   1559  1.158     ozaki #endif
   1560  1.136       roy 	if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
   1561  1.136       roy 	    (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
   1562  1.152       roy 	{
   1563  1.152       roy 		struct rt_addrinfo info;
   1564  1.152       roy 		struct rtentry *nrt;
   1565  1.152       roy 
   1566  1.152       roy 		memset(&info, 0, sizeof(info));
   1567  1.152       roy 		info.rti_flags = RTF_HOST | RTF_LOCAL;
   1568  1.152       roy 		if (!(ifa->ifa_ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)))
   1569  1.158     ozaki 			info.rti_flags |= RTF_LLDATA;
   1570  1.152       roy 		info.rti_info[RTAX_DST] = ifa->ifa_addr;
   1571  1.152       roy 		info.rti_info[RTAX_GATEWAY] =
   1572  1.152       roy 		    (const struct sockaddr *)ifa->ifa_ifp->if_sadl;
   1573  1.152       roy 		info.rti_ifa = ifa;
   1574  1.152       roy 		nrt = NULL;
   1575  1.152       roy 		e = rtrequest1(RTM_ADD, &info, &nrt);
   1576  1.152       roy 		if (nrt && ifa != nrt->rt_ifa)
   1577  1.152       roy 			rt_replace_ifa(nrt, ifa);
   1578  1.152       roy 		rt_newaddrmsg(RTM_ADD, ifa, e, nrt);
   1579  1.158     ozaki 		if (nrt != NULL) {
   1580  1.158     ozaki #ifdef RT_DEBUG
   1581  1.158     ozaki 			dump_rt(nrt);
   1582  1.158     ozaki #endif
   1583  1.183     ozaki 			rt_unref(nrt);
   1584  1.183     ozaki 			RT_REFCNT_TRACE(nrt);
   1585  1.158     ozaki 		}
   1586  1.152       roy 	} else {
   1587  1.136       roy 		e = 0;
   1588  1.136       roy 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1589  1.136       roy 	}
   1590  1.136       roy 	if (rt != NULL)
   1591  1.183     ozaki 		rt_unref(rt);
   1592  1.136       roy 	return e;
   1593  1.136       roy }
   1594  1.136       roy 
   1595  1.136       roy /*
   1596  1.136       roy  * Remove the local route entry for the address.
   1597  1.136       roy  * Announce the removal of the address and the route to the routing socket.
   1598  1.136       roy  */
   1599  1.136       roy int
   1600  1.136       roy rt_ifa_remlocal(struct ifaddr *ifa, struct ifaddr *alt_ifa)
   1601  1.136       roy {
   1602  1.136       roy 	struct rtentry *rt;
   1603  1.136       roy 	int e = 0;
   1604  1.136       roy 
   1605  1.136       roy 	rt = rtalloc1(ifa->ifa_addr, 0);
   1606  1.136       roy 
   1607  1.136       roy 	/*
   1608  1.136       roy 	 * Before deleting, check if a corresponding loopbacked
   1609  1.136       roy 	 * host route surely exists.  With this check, we can avoid
   1610  1.136       roy 	 * deleting an interface direct route whose destination is
   1611  1.136       roy 	 * the same as the address being removed.  This can happen
   1612  1.136       roy 	 * when removing a subnet-router anycast address on an
   1613  1.136       roy 	 * interface attached to a shared medium.
   1614  1.136       roy 	 */
   1615  1.136       roy 	if (rt != NULL &&
   1616  1.136       roy 	    (rt->rt_flags & RTF_HOST) &&
   1617  1.136       roy 	    (rt->rt_ifp->if_flags & IFF_LOOPBACK))
   1618  1.136       roy 	{
   1619  1.136       roy 		/* If we cannot replace the route's ifaddr with the equivalent
   1620  1.136       roy 		 * ifaddr of another interface, I believe it is safest to
   1621  1.136       roy 		 * delete the route.
   1622  1.136       roy 		 */
   1623  1.152       roy 		if (alt_ifa == NULL) {
   1624  1.152       roy 			e = rtdeletemsg(rt);
   1625  1.183     ozaki 			if (e == 0) {
   1626  1.183     ozaki 				rt_unref(rt);
   1627  1.183     ozaki 				rt_free(rt);
   1628  1.183     ozaki 				rt = NULL;
   1629  1.183     ozaki 			}
   1630  1.152       roy 			rt_newaddrmsg(RTM_DELADDR, ifa, 0, NULL);
   1631  1.152       roy 		} else {
   1632  1.136       roy 			rt_replace_ifa(rt, alt_ifa);
   1633  1.136       roy 			rt_newmsg(RTM_CHANGE, rt);
   1634  1.136       roy 		}
   1635  1.136       roy 	} else
   1636  1.136       roy 		rt_newaddrmsg(RTM_DELADDR, ifa, 0, NULL);
   1637  1.136       roy 	if (rt != NULL)
   1638  1.183     ozaki 		rt_unref(rt);
   1639  1.136       roy 	return e;
   1640  1.136       roy }
   1641  1.136       roy 
   1642   1.18       kml /*
   1643   1.18       kml  * Route timer routines.  These routes allow functions to be called
   1644   1.18       kml  * for various routes at any time.  This is useful in supporting
   1645   1.18       kml  * path MTU discovery and redirect route deletion.
   1646   1.18       kml  *
   1647   1.18       kml  * This is similar to some BSDI internal functions, but it provides
   1648   1.18       kml  * for multiple queues for efficiency's sake...
   1649   1.18       kml  */
   1650   1.18       kml 
   1651   1.18       kml LIST_HEAD(, rttimer_queue) rttimer_queue_head;
   1652   1.18       kml static int rt_init_done = 0;
   1653   1.18       kml 
   1654   1.65     perry /*
   1655   1.18       kml  * Some subtle order problems with domain initialization mean that
   1656   1.18       kml  * we cannot count on this being run from rt_init before various
   1657   1.18       kml  * protocol initializations are done.  Therefore, we make sure
   1658   1.18       kml  * that this is run when the first queue is added...
   1659   1.18       kml  */
   1660   1.18       kml 
   1661  1.170     ozaki static void rt_timer_work(struct work *, void *);
   1662  1.170     ozaki 
   1663  1.177     ozaki static void
   1664   1.60      matt rt_timer_init(void)
   1665   1.18       kml {
   1666  1.170     ozaki 	int error;
   1667  1.170     ozaki 
   1668   1.18       kml 	assert(rt_init_done == 0);
   1669   1.18       kml 
   1670  1.183     ozaki 	/* XXX should be in rt_init */
   1671  1.183     ozaki 	rw_init(&rt_lock);
   1672  1.183     ozaki 	rw_init(&rtcache_lock);
   1673  1.183     ozaki 
   1674   1.18       kml 	LIST_INIT(&rttimer_queue_head);
   1675  1.172    martin 	callout_init(&rt_timer_ch, CALLOUT_MPSAFE);
   1676  1.170     ozaki 	error = workqueue_create(&rt_timer_wq, "rt_timer",
   1677  1.170     ozaki 	    rt_timer_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
   1678  1.170     ozaki 	if (error)
   1679  1.170     ozaki 		panic("%s: workqueue_create failed (%d)\n", __func__, error);
   1680  1.172    martin 	callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
   1681   1.18       kml 	rt_init_done = 1;
   1682   1.18       kml }
   1683   1.18       kml 
   1684   1.18       kml struct rttimer_queue *
   1685   1.60      matt rt_timer_queue_create(u_int timeout)
   1686   1.18       kml {
   1687   1.18       kml 	struct rttimer_queue *rtq;
   1688   1.18       kml 
   1689   1.18       kml 	if (rt_init_done == 0)
   1690   1.18       kml 		rt_timer_init();
   1691   1.18       kml 
   1692   1.18       kml 	R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
   1693   1.18       kml 	if (rtq == NULL)
   1694   1.85    dyoung 		return NULL;
   1695  1.109    dyoung 	memset(rtq, 0, sizeof(*rtq));
   1696   1.18       kml 
   1697   1.18       kml 	rtq->rtq_timeout = timeout;
   1698   1.24   thorpej 	TAILQ_INIT(&rtq->rtq_head);
   1699  1.183     ozaki 	RT_WLOCK();
   1700   1.18       kml 	LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
   1701  1.183     ozaki 	RT_UNLOCK();
   1702   1.18       kml 
   1703   1.85    dyoung 	return rtq;
   1704   1.18       kml }
   1705   1.18       kml 
   1706   1.18       kml void
   1707   1.60      matt rt_timer_queue_change(struct rttimer_queue *rtq, long timeout)
   1708   1.18       kml {
   1709   1.24   thorpej 
   1710   1.18       kml 	rtq->rtq_timeout = timeout;
   1711   1.18       kml }
   1712   1.18       kml 
   1713  1.177     ozaki static void
   1714  1.181     ozaki rt_timer_queue_remove_all(struct rttimer_queue *rtq)
   1715   1.18       kml {
   1716   1.24   thorpej 	struct rttimer *r;
   1717   1.18       kml 
   1718  1.183     ozaki 	RT_ASSERT_WLOCK();
   1719  1.183     ozaki 
   1720   1.24   thorpej 	while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
   1721   1.18       kml 		LIST_REMOVE(r, rtt_link);
   1722   1.24   thorpej 		TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
   1723  1.183     ozaki 		rt_ref(r->rtt_rt); /* XXX */
   1724  1.183     ozaki 		RT_REFCNT_TRACE(r->rtt_rt);
   1725  1.183     ozaki 		RT_UNLOCK();
   1726  1.181     ozaki 		(*r->rtt_func)(r->rtt_rt, r);
   1727   1.22   thorpej 		pool_put(&rttimer_pool, r);
   1728  1.183     ozaki 		RT_WLOCK();
   1729   1.37    itojun 		if (rtq->rtq_count > 0)
   1730   1.37    itojun 			rtq->rtq_count--;
   1731   1.37    itojun 		else
   1732   1.55    itojun 			printf("rt_timer_queue_remove_all: "
   1733   1.55    itojun 			    "rtq_count reached 0\n");
   1734   1.18       kml 	}
   1735   1.55    itojun }
   1736   1.55    itojun 
   1737   1.55    itojun void
   1738  1.181     ozaki rt_timer_queue_destroy(struct rttimer_queue *rtq)
   1739   1.55    itojun {
   1740   1.55    itojun 
   1741  1.183     ozaki 	RT_WLOCK();
   1742  1.181     ozaki 	rt_timer_queue_remove_all(rtq);
   1743   1.18       kml 	LIST_REMOVE(rtq, rtq_link);
   1744  1.183     ozaki 	RT_UNLOCK();
   1745   1.22   thorpej 
   1746   1.22   thorpej 	/*
   1747   1.22   thorpej 	 * Caller is responsible for freeing the rttimer_queue structure.
   1748   1.22   thorpej 	 */
   1749   1.18       kml }
   1750   1.18       kml 
   1751   1.37    itojun unsigned long
   1752   1.60      matt rt_timer_count(struct rttimer_queue *rtq)
   1753   1.37    itojun {
   1754   1.37    itojun 	return rtq->rtq_count;
   1755   1.37    itojun }
   1756   1.37    itojun 
   1757  1.177     ozaki static void
   1758  1.178     ozaki rt_timer_remove_all(struct rtentry *rt)
   1759   1.18       kml {
   1760   1.24   thorpej 	struct rttimer *r;
   1761   1.18       kml 
   1762  1.183     ozaki 	RT_WLOCK();
   1763   1.24   thorpej 	while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
   1764   1.18       kml 		LIST_REMOVE(r, rtt_link);
   1765   1.24   thorpej 		TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
   1766   1.37    itojun 		if (r->rtt_queue->rtq_count > 0)
   1767   1.37    itojun 			r->rtt_queue->rtq_count--;
   1768   1.37    itojun 		else
   1769   1.37    itojun 			printf("rt_timer_remove_all: rtq_count reached 0\n");
   1770   1.38    itojun 		pool_put(&rttimer_pool, r);
   1771   1.18       kml 	}
   1772  1.183     ozaki 	RT_UNLOCK();
   1773   1.18       kml }
   1774   1.18       kml 
   1775   1.65     perry int
   1776   1.60      matt rt_timer_add(struct rtentry *rt,
   1777   1.60      matt 	void (*func)(struct rtentry *, struct rttimer *),
   1778   1.60      matt 	struct rttimer_queue *queue)
   1779   1.18       kml {
   1780   1.24   thorpej 	struct rttimer *r;
   1781   1.18       kml 
   1782  1.156     ozaki 	KASSERT(func != NULL);
   1783  1.183     ozaki 	RT_WLOCK();
   1784   1.24   thorpej 	/*
   1785   1.24   thorpej 	 * If there's already a timer with this action, destroy it before
   1786   1.24   thorpej 	 * we add a new one.
   1787   1.24   thorpej 	 */
   1788   1.85    dyoung 	LIST_FOREACH(r, &rt->rt_timer, rtt_link) {
   1789   1.85    dyoung 		if (r->rtt_func == func)
   1790   1.85    dyoung 			break;
   1791   1.85    dyoung 	}
   1792   1.85    dyoung 	if (r != NULL) {
   1793   1.85    dyoung 		LIST_REMOVE(r, rtt_link);
   1794   1.85    dyoung 		TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
   1795   1.85    dyoung 		if (r->rtt_queue->rtq_count > 0)
   1796   1.85    dyoung 			r->rtt_queue->rtq_count--;
   1797   1.85    dyoung 		else
   1798   1.85    dyoung 			printf("rt_timer_add: rtq_count reached 0\n");
   1799   1.85    dyoung 	} else {
   1800   1.85    dyoung 		r = pool_get(&rttimer_pool, PR_NOWAIT);
   1801  1.183     ozaki 		if (r == NULL) {
   1802  1.183     ozaki 			RT_UNLOCK();
   1803   1.85    dyoung 			return ENOBUFS;
   1804  1.183     ozaki 		}
   1805   1.18       kml 	}
   1806   1.18       kml 
   1807   1.85    dyoung 	memset(r, 0, sizeof(*r));
   1808   1.24   thorpej 
   1809   1.24   thorpej 	r->rtt_rt = rt;
   1810   1.70    kardel 	r->rtt_time = time_uptime;
   1811   1.24   thorpej 	r->rtt_func = func;
   1812   1.24   thorpej 	r->rtt_queue = queue;
   1813   1.24   thorpej 	LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
   1814   1.24   thorpej 	TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
   1815   1.37    itojun 	r->rtt_queue->rtq_count++;
   1816   1.65     perry 
   1817  1.183     ozaki 	RT_UNLOCK();
   1818  1.183     ozaki 
   1819   1.95    dyoung 	return 0;
   1820   1.18       kml }
   1821   1.18       kml 
   1822  1.170     ozaki static void
   1823  1.170     ozaki rt_timer_work(struct work *wk, void *arg)
   1824   1.18       kml {
   1825   1.24   thorpej 	struct rttimer_queue *rtq;
   1826   1.24   thorpej 	struct rttimer *r;
   1827   1.21       kml 
   1828  1.183     ozaki 	RT_WLOCK();
   1829   1.85    dyoung 	LIST_FOREACH(rtq, &rttimer_queue_head, rtq_link) {
   1830   1.24   thorpej 		while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
   1831   1.70    kardel 		    (r->rtt_time + rtq->rtq_timeout) < time_uptime) {
   1832   1.24   thorpej 			LIST_REMOVE(r, rtt_link);
   1833   1.24   thorpej 			TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
   1834  1.183     ozaki 			rt_ref(r->rtt_rt); /* XXX */
   1835  1.183     ozaki 			RT_REFCNT_TRACE(r->rtt_rt);
   1836  1.183     ozaki 			RT_UNLOCK();
   1837  1.156     ozaki 			(*r->rtt_func)(r->rtt_rt, r);
   1838   1.24   thorpej 			pool_put(&rttimer_pool, r);
   1839  1.183     ozaki 			RT_WLOCK();
   1840   1.37    itojun 			if (rtq->rtq_count > 0)
   1841   1.37    itojun 				rtq->rtq_count--;
   1842   1.37    itojun 			else
   1843   1.37    itojun 				printf("rt_timer_timer: rtq_count reached 0\n");
   1844   1.18       kml 		}
   1845   1.18       kml 	}
   1846  1.183     ozaki 	RT_UNLOCK();
   1847   1.18       kml 
   1848   1.35   thorpej 	callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
   1849    1.1       cgd }
   1850   1.83     joerg 
   1851  1.177     ozaki static void
   1852  1.170     ozaki rt_timer_timer(void *arg)
   1853  1.170     ozaki {
   1854  1.170     ozaki 
   1855  1.170     ozaki 	workqueue_enqueue(rt_timer_wq, &rt_timer_wk, NULL);
   1856  1.170     ozaki }
   1857  1.170     ozaki 
   1858  1.102    dyoung static struct rtentry *
   1859   1.84     joerg _rtcache_init(struct route *ro, int flag)
   1860   1.84     joerg {
   1861  1.183     ozaki 	struct rtentry *rt;
   1862  1.183     ozaki 
   1863  1.114    dyoung 	rtcache_invariants(ro);
   1864   1.99    dyoung 	KASSERT(ro->_ro_rt == NULL);
   1865  1.183     ozaki 	RTCACHE_ASSERT_WLOCK();
   1866   1.84     joerg 
   1867   1.90    dyoung 	if (rtcache_getdst(ro) == NULL)
   1868  1.102    dyoung 		return NULL;
   1869  1.105    dyoung 	ro->ro_invalid = false;
   1870  1.183     ozaki 	rt = rtalloc1(rtcache_getdst(ro), flag);
   1871  1.183     ozaki 	if (rt != NULL && ISSET(rt->rt_flags, RTF_UP)) {
   1872  1.183     ozaki 		ro->_ro_rt = rt;
   1873  1.183     ozaki 		KASSERT(!ISSET(rt->rt_flags, RTF_UPDATING));
   1874  1.183     ozaki 		rtcache_ref(rt, ro);
   1875  1.183     ozaki 		rt_unref(rt);
   1876  1.105    dyoung 		rtcache(ro);
   1877  1.183     ozaki 	} else if (rt != NULL)
   1878  1.183     ozaki 		rt_unref(rt);
   1879  1.103    dyoung 
   1880  1.114    dyoung 	rtcache_invariants(ro);
   1881  1.102    dyoung 	return ro->_ro_rt;
   1882   1.84     joerg }
   1883   1.84     joerg 
   1884  1.102    dyoung struct rtentry *
   1885   1.83     joerg rtcache_init(struct route *ro)
   1886   1.83     joerg {
   1887  1.183     ozaki 	struct rtentry *rt;
   1888  1.183     ozaki 	RTCACHE_WLOCK();
   1889  1.183     ozaki 	rt = _rtcache_init(ro, 1);
   1890  1.183     ozaki 	RTCACHE_UNLOCK();
   1891  1.183     ozaki 	return rt;
   1892   1.83     joerg }
   1893   1.83     joerg 
   1894  1.102    dyoung struct rtentry *
   1895   1.83     joerg rtcache_init_noclone(struct route *ro)
   1896   1.83     joerg {
   1897  1.183     ozaki 	struct rtentry *rt;
   1898  1.183     ozaki 	RTCACHE_WLOCK();
   1899  1.183     ozaki 	rt = _rtcache_init(ro, 0);
   1900  1.183     ozaki 	RTCACHE_UNLOCK();
   1901  1.183     ozaki 	return rt;
   1902   1.83     joerg }
   1903   1.90    dyoung 
   1904  1.102    dyoung struct rtentry *
   1905   1.90    dyoung rtcache_update(struct route *ro, int clone)
   1906   1.90    dyoung {
   1907  1.183     ozaki 	struct rtentry *rt;
   1908  1.183     ozaki 	RTCACHE_WLOCK();
   1909   1.90    dyoung 	rtcache_clear(ro);
   1910  1.183     ozaki 	rt = _rtcache_init(ro, clone);
   1911  1.183     ozaki 	RTCACHE_UNLOCK();
   1912  1.183     ozaki 	return rt;
   1913   1.90    dyoung }
   1914   1.83     joerg 
   1915   1.83     joerg void
   1916  1.183     ozaki rtcache_copy(struct route *new_ro, struct route *old_ro)
   1917   1.83     joerg {
   1918  1.103    dyoung 	struct rtentry *rt;
   1919  1.183     ozaki 	int ret;
   1920  1.103    dyoung 
   1921  1.103    dyoung 	KASSERT(new_ro != old_ro);
   1922  1.114    dyoung 	rtcache_invariants(new_ro);
   1923  1.114    dyoung 	rtcache_invariants(old_ro);
   1924  1.103    dyoung 
   1925  1.183     ozaki 	rt = rtcache_validate(old_ro);
   1926  1.103    dyoung 
   1927  1.183     ozaki 	if (rtcache_getdst(old_ro) == NULL)
   1928  1.183     ozaki 		goto out;
   1929  1.183     ozaki 	ret = rtcache_setdst(new_ro, rtcache_getdst(old_ro));
   1930  1.183     ozaki 	if (ret != 0)
   1931  1.183     ozaki 		goto out;
   1932  1.103    dyoung 
   1933  1.183     ozaki 	RTCACHE_WLOCK();
   1934  1.105    dyoung 	new_ro->ro_invalid = false;
   1935  1.103    dyoung 	if ((new_ro->_ro_rt = rt) != NULL)
   1936   1.86    dyoung 		rtcache(new_ro);
   1937  1.114    dyoung 	rtcache_invariants(new_ro);
   1938  1.183     ozaki 	RTCACHE_UNLOCK();
   1939  1.183     ozaki out:
   1940  1.183     ozaki 	rtcache_unref(rt, old_ro);
   1941  1.183     ozaki 	return;
   1942   1.83     joerg }
   1943   1.83     joerg 
   1944  1.105    dyoung static struct dom_rtlist invalid_routes = LIST_HEAD_INITIALIZER(dom_rtlist);
   1945  1.105    dyoung 
   1946  1.184     ozaki #if defined(RT_DEBUG) && defined(NET_MPSAFE)
   1947  1.183     ozaki static void
   1948  1.183     ozaki rtcache_trace(const char *func, struct rtentry *rt, struct route *ro)
   1949  1.183     ozaki {
   1950  1.183     ozaki 	char dst[64];
   1951  1.183     ozaki 
   1952  1.183     ozaki 	sockaddr_format(ro->ro_sa, dst, 64);
   1953  1.183     ozaki 	printf("trace: %s:\tdst=%s cpu=%d lwp=%p psref=%p target=%p\n", func, dst,
   1954  1.183     ozaki 	    cpu_index(curcpu()), curlwp, &ro->ro_psref, &rt->rt_psref);
   1955  1.183     ozaki }
   1956  1.183     ozaki #define RTCACHE_PSREF_TRACE(rt, ro)	rtcache_trace(__func__, (rt), (ro))
   1957  1.183     ozaki #else
   1958  1.183     ozaki #define RTCACHE_PSREF_TRACE(rt, ro)	do {} while (0)
   1959  1.183     ozaki #endif
   1960  1.183     ozaki 
   1961  1.183     ozaki static void
   1962  1.183     ozaki rtcache_ref(struct rtentry *rt, struct route *ro)
   1963  1.183     ozaki {
   1964  1.183     ozaki 
   1965  1.183     ozaki 	KASSERT(rt != NULL);
   1966  1.183     ozaki 
   1967  1.183     ozaki #ifdef NET_MPSAFE
   1968  1.183     ozaki 	RTCACHE_PSREF_TRACE(rt, ro);
   1969  1.183     ozaki 	ro->ro_bound = curlwp_bind();
   1970  1.183     ozaki 	psref_acquire(&ro->ro_psref, &rt->rt_psref, rt_psref_class);
   1971  1.183     ozaki #endif
   1972  1.183     ozaki }
   1973  1.183     ozaki 
   1974  1.183     ozaki void
   1975  1.183     ozaki rtcache_unref(struct rtentry *rt, struct route *ro)
   1976  1.183     ozaki {
   1977  1.183     ozaki 
   1978  1.183     ozaki 	if (rt == NULL)
   1979  1.183     ozaki 		return;
   1980  1.183     ozaki 
   1981  1.183     ozaki #ifdef NET_MPSAFE
   1982  1.183     ozaki 	psref_release(&ro->ro_psref, &rt->rt_psref, rt_psref_class);
   1983  1.183     ozaki 	curlwp_bindx(ro->ro_bound);
   1984  1.183     ozaki 	RTCACHE_PSREF_TRACE(rt, ro);
   1985  1.183     ozaki #endif
   1986  1.183     ozaki }
   1987  1.183     ozaki 
   1988  1.183     ozaki static struct rtentry *
   1989  1.183     ozaki rtcache_validate_locked(struct route *ro)
   1990  1.183     ozaki {
   1991  1.183     ozaki 	struct rtentry *rt = NULL;
   1992  1.183     ozaki 
   1993  1.188     ozaki #ifdef NET_MPSAFE
   1994  1.183     ozaki retry:
   1995  1.188     ozaki #endif
   1996  1.183     ozaki 	rt = ro->_ro_rt;
   1997  1.183     ozaki 	rtcache_invariants(ro);
   1998  1.183     ozaki 
   1999  1.183     ozaki 	if (ro->ro_invalid) {
   2000  1.183     ozaki 		rt = NULL;
   2001  1.183     ozaki 		goto out;
   2002  1.183     ozaki 	}
   2003  1.183     ozaki 
   2004  1.183     ozaki 	RT_RLOCK();
   2005  1.183     ozaki 	if (rt != NULL && (rt->rt_flags & RTF_UP) != 0 && rt->rt_ifp != NULL) {
   2006  1.188     ozaki #ifdef NET_MPSAFE
   2007  1.183     ozaki 		if (ISSET(rt->rt_flags, RTF_UPDATING)) {
   2008  1.183     ozaki 			if (rt_wait_ok()) {
   2009  1.183     ozaki 				RT_UNLOCK();
   2010  1.183     ozaki 				RTCACHE_UNLOCK();
   2011  1.183     ozaki 				/* We can wait until the update is complete */
   2012  1.183     ozaki 				rt_update_wait();
   2013  1.183     ozaki 				RTCACHE_RLOCK();
   2014  1.183     ozaki 				goto retry;
   2015  1.183     ozaki 			} else {
   2016  1.183     ozaki 				rt = NULL;
   2017  1.183     ozaki 			}
   2018  1.183     ozaki 		} else
   2019  1.188     ozaki #endif
   2020  1.183     ozaki 			rtcache_ref(rt, ro);
   2021  1.183     ozaki 	} else
   2022  1.183     ozaki 		rt = NULL;
   2023  1.183     ozaki 	RT_UNLOCK();
   2024  1.183     ozaki out:
   2025  1.183     ozaki 	return rt;
   2026  1.183     ozaki }
   2027  1.183     ozaki 
   2028  1.183     ozaki struct rtentry *
   2029  1.183     ozaki rtcache_validate(struct route *ro)
   2030  1.183     ozaki {
   2031  1.183     ozaki 	struct rtentry *rt;
   2032  1.183     ozaki 
   2033  1.183     ozaki 	RTCACHE_RLOCK();
   2034  1.183     ozaki 	rt = rtcache_validate_locked(ro);
   2035  1.183     ozaki 	RTCACHE_UNLOCK();
   2036  1.183     ozaki 	return rt;
   2037  1.183     ozaki }
   2038  1.183     ozaki 
   2039  1.144     ozaki static void
   2040  1.105    dyoung rtcache_invalidate(struct dom_rtlist *rtlist)
   2041   1.83     joerg {
   2042  1.105    dyoung 	struct route *ro;
   2043   1.99    dyoung 
   2044  1.183     ozaki 	RTCACHE_ASSERT_WLOCK();
   2045  1.183     ozaki 
   2046  1.105    dyoung 	while ((ro = LIST_FIRST(rtlist)) != NULL) {
   2047  1.114    dyoung 		rtcache_invariants(ro);
   2048  1.105    dyoung 		KASSERT(ro->_ro_rt != NULL);
   2049  1.105    dyoung 		ro->ro_invalid = true;
   2050   1.99    dyoung 		LIST_REMOVE(ro, ro_rtcache_next);
   2051  1.105    dyoung 		LIST_INSERT_HEAD(&invalid_routes, ro, ro_rtcache_next);
   2052  1.114    dyoung 		rtcache_invariants(ro);
   2053   1.84     joerg 	}
   2054  1.105    dyoung }
   2055  1.105    dyoung 
   2056  1.144     ozaki static void
   2057  1.168     ozaki rtcache_clear_rtentry(int family, struct rtentry *rt)
   2058  1.168     ozaki {
   2059  1.168     ozaki 	struct domain *dom;
   2060  1.171   hannken 	struct route *ro, *nro;
   2061  1.168     ozaki 
   2062  1.168     ozaki 	if ((dom = pffinddomain(family)) == NULL)
   2063  1.168     ozaki 		return;
   2064  1.168     ozaki 
   2065  1.183     ozaki 	RTCACHE_WLOCK();
   2066  1.171   hannken 	LIST_FOREACH_SAFE(ro, &dom->dom_rtcache, ro_rtcache_next, nro) {
   2067  1.168     ozaki 		if (ro->_ro_rt == rt)
   2068  1.168     ozaki 			rtcache_clear(ro);
   2069  1.168     ozaki 	}
   2070  1.183     ozaki 	RTCACHE_UNLOCK();
   2071  1.168     ozaki }
   2072  1.168     ozaki 
   2073  1.168     ozaki static void
   2074  1.105    dyoung rtcache_clear(struct route *ro)
   2075  1.105    dyoung {
   2076  1.183     ozaki 
   2077  1.183     ozaki 	RTCACHE_ASSERT_WLOCK();
   2078  1.183     ozaki 
   2079  1.114    dyoung 	rtcache_invariants(ro);
   2080  1.105    dyoung 	if (ro->_ro_rt == NULL)
   2081  1.105    dyoung 		return;
   2082  1.105    dyoung 
   2083  1.105    dyoung 	LIST_REMOVE(ro, ro_rtcache_next);
   2084  1.105    dyoung 
   2085  1.105    dyoung 	ro->_ro_rt = NULL;
   2086  1.114    dyoung 	ro->ro_invalid = false;
   2087  1.114    dyoung 	rtcache_invariants(ro);
   2088   1.83     joerg }
   2089   1.83     joerg 
   2090   1.90    dyoung struct rtentry *
   2091  1.183     ozaki rtcache_lookup2(struct route *ro, const struct sockaddr *dst,
   2092  1.183     ozaki     int clone, int *hitp)
   2093   1.90    dyoung {
   2094   1.90    dyoung 	const struct sockaddr *odst;
   2095  1.104    dyoung 	struct rtentry *rt = NULL;
   2096   1.90    dyoung 
   2097  1.183     ozaki 	RTCACHE_RLOCK();
   2098   1.90    dyoung 	odst = rtcache_getdst(ro);
   2099  1.183     ozaki 	if (odst == NULL) {
   2100  1.183     ozaki 		RTCACHE_UNLOCK();
   2101  1.183     ozaki 		RTCACHE_WLOCK();
   2102  1.138     ozaki 		goto miss;
   2103  1.183     ozaki 	}
   2104   1.90    dyoung 
   2105  1.138     ozaki 	if (sockaddr_cmp(odst, dst) != 0) {
   2106  1.183     ozaki 		RTCACHE_UNLOCK();
   2107  1.183     ozaki 		RTCACHE_WLOCK();
   2108  1.183     ozaki 		rtcache_free_locked(ro);
   2109  1.138     ozaki 		goto miss;
   2110  1.138     ozaki 	}
   2111  1.138     ozaki 
   2112  1.183     ozaki 	rt = rtcache_validate_locked(ro);
   2113  1.138     ozaki 	if (rt == NULL) {
   2114  1.183     ozaki 		RTCACHE_UNLOCK();
   2115  1.183     ozaki 		RTCACHE_WLOCK();
   2116   1.91    dyoung 		rtcache_clear(ro);
   2117  1.138     ozaki 		goto miss;
   2118  1.138     ozaki 	}
   2119  1.138     ozaki 
   2120  1.138     ozaki 	rtcache_invariants(ro);
   2121   1.90    dyoung 
   2122  1.183     ozaki 	RTCACHE_UNLOCK();
   2123  1.183     ozaki 	if (hitp != NULL)
   2124  1.183     ozaki 		*hitp = 1;
   2125  1.138     ozaki 	return rt;
   2126  1.138     ozaki miss:
   2127  1.183     ozaki 	if (hitp != NULL)
   2128  1.183     ozaki 		*hitp = 0;
   2129  1.183     ozaki 	if (rtcache_setdst_locked(ro, dst) == 0)
   2130  1.138     ozaki 		rt = _rtcache_init(ro, clone);
   2131   1.90    dyoung 
   2132  1.114    dyoung 	rtcache_invariants(ro);
   2133  1.114    dyoung 
   2134  1.183     ozaki 	RTCACHE_UNLOCK();
   2135  1.104    dyoung 	return rt;
   2136   1.90    dyoung }
   2137   1.90    dyoung 
   2138  1.183     ozaki static void
   2139  1.183     ozaki rtcache_free_locked(struct route *ro)
   2140   1.86    dyoung {
   2141  1.183     ozaki 
   2142  1.183     ozaki 	RTCACHE_ASSERT_WLOCK();
   2143   1.86    dyoung 	rtcache_clear(ro);
   2144   1.86    dyoung 	if (ro->ro_sa != NULL) {
   2145   1.86    dyoung 		sockaddr_free(ro->ro_sa);
   2146   1.86    dyoung 		ro->ro_sa = NULL;
   2147   1.86    dyoung 	}
   2148  1.114    dyoung 	rtcache_invariants(ro);
   2149   1.86    dyoung }
   2150   1.86    dyoung 
   2151  1.183     ozaki void
   2152  1.183     ozaki rtcache_free(struct route *ro)
   2153  1.183     ozaki {
   2154  1.183     ozaki 
   2155  1.183     ozaki 	RTCACHE_WLOCK();
   2156  1.183     ozaki 	rtcache_free_locked(ro);
   2157  1.183     ozaki 	RTCACHE_UNLOCK();
   2158  1.183     ozaki }
   2159  1.183     ozaki 
   2160  1.183     ozaki static int
   2161  1.183     ozaki rtcache_setdst_locked(struct route *ro, const struct sockaddr *sa)
   2162   1.83     joerg {
   2163   1.90    dyoung 	KASSERT(sa != NULL);
   2164   1.90    dyoung 
   2165  1.183     ozaki 	RTCACHE_ASSERT_WLOCK();
   2166  1.183     ozaki 
   2167  1.114    dyoung 	rtcache_invariants(ro);
   2168  1.142     ozaki 	if (ro->ro_sa != NULL) {
   2169  1.142     ozaki 		if (ro->ro_sa->sa_family == sa->sa_family) {
   2170  1.142     ozaki 			rtcache_clear(ro);
   2171  1.142     ozaki 			sockaddr_copy(ro->ro_sa, ro->ro_sa->sa_len, sa);
   2172  1.143     ozaki 			rtcache_invariants(ro);
   2173  1.143     ozaki 			return 0;
   2174  1.114    dyoung 		}
   2175  1.143     ozaki 		/* free ro_sa, wrong family */
   2176  1.183     ozaki 		rtcache_free_locked(ro);
   2177  1.142     ozaki 	}
   2178   1.90    dyoung 
   2179  1.107    dyoung 	KASSERT(ro->_ro_rt == NULL);
   2180  1.107    dyoung 
   2181  1.134  christos 	if ((ro->ro_sa = sockaddr_dup(sa, M_ZERO | M_NOWAIT)) == NULL) {
   2182  1.114    dyoung 		rtcache_invariants(ro);
   2183   1.90    dyoung 		return ENOMEM;
   2184  1.107    dyoung 	}
   2185  1.114    dyoung 	rtcache_invariants(ro);
   2186   1.90    dyoung 	return 0;
   2187   1.83     joerg }
   2188   1.92    dyoung 
   2189  1.183     ozaki int
   2190  1.183     ozaki rtcache_setdst(struct route *ro, const struct sockaddr *sa)
   2191  1.183     ozaki {
   2192  1.183     ozaki 	int error;
   2193  1.183     ozaki 
   2194  1.183     ozaki 	RTCACHE_WLOCK();
   2195  1.183     ozaki 	error = rtcache_setdst_locked(ro, sa);
   2196  1.183     ozaki 	RTCACHE_UNLOCK();
   2197  1.183     ozaki 
   2198  1.183     ozaki 	return error;
   2199  1.183     ozaki }
   2200  1.183     ozaki 
   2201  1.123    kefren const struct sockaddr *
   2202  1.123    kefren rt_settag(struct rtentry *rt, const struct sockaddr *tag)
   2203  1.123    kefren {
   2204  1.123    kefren 	if (rt->rt_tag != tag) {
   2205  1.123    kefren 		if (rt->rt_tag != NULL)
   2206  1.123    kefren 			sockaddr_free(rt->rt_tag);
   2207  1.134  christos 		rt->rt_tag = sockaddr_dup(tag, M_ZERO | M_NOWAIT);
   2208  1.123    kefren 	}
   2209  1.169   msaitoh 	return rt->rt_tag;
   2210  1.123    kefren }
   2211  1.123    kefren 
   2212  1.123    kefren struct sockaddr *
   2213  1.167     ozaki rt_gettag(const struct rtentry *rt)
   2214  1.123    kefren {
   2215  1.123    kefren 	return rt->rt_tag;
   2216  1.123    kefren }
   2217  1.162     ozaki 
   2218  1.165     ozaki int
   2219  1.167     ozaki rt_check_reject_route(const struct rtentry *rt, const struct ifnet *ifp)
   2220  1.165     ozaki {
   2221  1.165     ozaki 
   2222  1.165     ozaki 	if ((rt->rt_flags & RTF_REJECT) != 0) {
   2223  1.165     ozaki 		/* Mimic looutput */
   2224  1.165     ozaki 		if (ifp->if_flags & IFF_LOOPBACK)
   2225  1.165     ozaki 			return (rt->rt_flags & RTF_HOST) ?
   2226  1.165     ozaki 			    EHOSTUNREACH : ENETUNREACH;
   2227  1.165     ozaki 		else if (rt->rt_rmx.rmx_expire == 0 ||
   2228  1.165     ozaki 		    time_uptime < rt->rt_rmx.rmx_expire)
   2229  1.165     ozaki 			return (rt->rt_flags & RTF_GATEWAY) ?
   2230  1.165     ozaki 			    EHOSTUNREACH : EHOSTDOWN;
   2231  1.165     ozaki 	}
   2232  1.165     ozaki 
   2233  1.165     ozaki 	return 0;
   2234  1.165     ozaki }
   2235  1.165     ozaki 
   2236  1.182     ozaki void
   2237  1.182     ozaki rt_delete_matched_entries(sa_family_t family, int (*f)(struct rtentry *, void *),
   2238  1.182     ozaki     void *v)
   2239  1.182     ozaki {
   2240  1.182     ozaki 
   2241  1.182     ozaki 	for (;;) {
   2242  1.182     ozaki 		int s;
   2243  1.182     ozaki 		int error;
   2244  1.182     ozaki 		struct rtentry *rt, *retrt = NULL;
   2245  1.182     ozaki 
   2246  1.183     ozaki 		RT_RLOCK();
   2247  1.182     ozaki 		s = splsoftnet();
   2248  1.182     ozaki 		rt = rtbl_search_matched_entry(family, f, v);
   2249  1.182     ozaki 		if (rt == NULL) {
   2250  1.182     ozaki 			splx(s);
   2251  1.183     ozaki 			RT_UNLOCK();
   2252  1.182     ozaki 			return;
   2253  1.182     ozaki 		}
   2254  1.182     ozaki 		rt->rt_refcnt++;
   2255  1.182     ozaki 		splx(s);
   2256  1.183     ozaki 		RT_UNLOCK();
   2257  1.182     ozaki 
   2258  1.182     ozaki 		error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
   2259  1.182     ozaki 		    rt_mask(rt), rt->rt_flags, &retrt);
   2260  1.182     ozaki 		if (error == 0) {
   2261  1.182     ozaki 			KASSERT(retrt == rt);
   2262  1.182     ozaki 			KASSERT((retrt->rt_flags & RTF_UP) == 0);
   2263  1.182     ozaki 			retrt->rt_ifp = NULL;
   2264  1.183     ozaki 			rt_unref(rt);
   2265  1.183     ozaki 			rt_free(retrt);
   2266  1.182     ozaki 		} else if (error == ESRCH) {
   2267  1.182     ozaki 			/* Someone deleted the entry already. */
   2268  1.183     ozaki 			rt_unref(rt);
   2269  1.182     ozaki 		} else {
   2270  1.182     ozaki 			log(LOG_ERR, "%s: unable to delete rtentry @ %p, "
   2271  1.182     ozaki 			    "error = %d\n", rt->rt_ifp->if_xname, rt, error);
   2272  1.182     ozaki 			/* XXX how to treat this case? */
   2273  1.182     ozaki 		}
   2274  1.182     ozaki 	}
   2275  1.182     ozaki }
   2276  1.182     ozaki 
   2277  1.183     ozaki int
   2278  1.183     ozaki rt_walktree(sa_family_t family, int (*f)(struct rtentry *, void *), void *v)
   2279  1.183     ozaki {
   2280  1.183     ozaki 	int error;
   2281  1.183     ozaki 
   2282  1.183     ozaki 	RT_RLOCK();
   2283  1.183     ozaki 	error = rtbl_walktree(family, f, v);
   2284  1.183     ozaki 	RT_UNLOCK();
   2285  1.183     ozaki 
   2286  1.183     ozaki 	return error;
   2287  1.183     ozaki }
   2288  1.183     ozaki 
   2289  1.162     ozaki #ifdef DDB
   2290  1.162     ozaki 
   2291  1.162     ozaki #include <machine/db_machdep.h>
   2292  1.162     ozaki #include <ddb/db_interface.h>
   2293  1.162     ozaki #include <ddb/db_output.h>
   2294  1.162     ozaki 
   2295  1.162     ozaki #define	rt_expire rt_rmx.rmx_expire
   2296  1.162     ozaki 
   2297  1.162     ozaki static void
   2298  1.162     ozaki db_print_sa(const struct sockaddr *sa)
   2299  1.162     ozaki {
   2300  1.162     ozaki 	int len;
   2301  1.162     ozaki 	const u_char *p;
   2302  1.162     ozaki 
   2303  1.162     ozaki 	if (sa == NULL) {
   2304  1.162     ozaki 		db_printf("[NULL]");
   2305  1.162     ozaki 		return;
   2306  1.162     ozaki 	}
   2307  1.162     ozaki 
   2308  1.162     ozaki 	p = (const u_char *)sa;
   2309  1.162     ozaki 	len = sa->sa_len;
   2310  1.162     ozaki 	db_printf("[");
   2311  1.162     ozaki 	while (len > 0) {
   2312  1.162     ozaki 		db_printf("%d", *p);
   2313  1.162     ozaki 		p++; len--;
   2314  1.162     ozaki 		if (len) db_printf(",");
   2315  1.162     ozaki 	}
   2316  1.162     ozaki 	db_printf("]\n");
   2317  1.162     ozaki }
   2318  1.162     ozaki 
   2319  1.162     ozaki static void
   2320  1.162     ozaki db_print_ifa(struct ifaddr *ifa)
   2321  1.162     ozaki {
   2322  1.162     ozaki 	if (ifa == NULL)
   2323  1.162     ozaki 		return;
   2324  1.162     ozaki 	db_printf("  ifa_addr=");
   2325  1.162     ozaki 	db_print_sa(ifa->ifa_addr);
   2326  1.162     ozaki 	db_printf("  ifa_dsta=");
   2327  1.162     ozaki 	db_print_sa(ifa->ifa_dstaddr);
   2328  1.162     ozaki 	db_printf("  ifa_mask=");
   2329  1.162     ozaki 	db_print_sa(ifa->ifa_netmask);
   2330  1.162     ozaki 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
   2331  1.162     ozaki 			  ifa->ifa_flags,
   2332  1.162     ozaki 			  ifa->ifa_refcnt,
   2333  1.162     ozaki 			  ifa->ifa_metric);
   2334  1.162     ozaki }
   2335  1.162     ozaki 
   2336  1.162     ozaki /*
   2337  1.162     ozaki  * Function to pass to rt_walktree().
   2338  1.162     ozaki  * Return non-zero error to abort walk.
   2339  1.162     ozaki  */
   2340  1.162     ozaki static int
   2341  1.162     ozaki db_show_rtentry(struct rtentry *rt, void *w)
   2342  1.162     ozaki {
   2343  1.162     ozaki 	db_printf("rtentry=%p", rt);
   2344  1.162     ozaki 
   2345  1.162     ozaki 	db_printf(" flags=0x%x refcnt=%d use=%"PRId64" expire=%"PRId64"\n",
   2346  1.162     ozaki 			  rt->rt_flags, rt->rt_refcnt,
   2347  1.162     ozaki 			  rt->rt_use, (uint64_t)rt->rt_expire);
   2348  1.162     ozaki 
   2349  1.162     ozaki 	db_printf(" key="); db_print_sa(rt_getkey(rt));
   2350  1.162     ozaki 	db_printf(" mask="); db_print_sa(rt_mask(rt));
   2351  1.162     ozaki 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
   2352  1.162     ozaki 
   2353  1.162     ozaki 	db_printf(" ifp=%p ", rt->rt_ifp);
   2354  1.162     ozaki 	if (rt->rt_ifp)
   2355  1.162     ozaki 		db_printf("(%s)", rt->rt_ifp->if_xname);
   2356  1.162     ozaki 	else
   2357  1.162     ozaki 		db_printf("(NULL)");
   2358  1.162     ozaki 
   2359  1.162     ozaki 	db_printf(" ifa=%p\n", rt->rt_ifa);
   2360  1.162     ozaki 	db_print_ifa(rt->rt_ifa);
   2361  1.162     ozaki 
   2362  1.162     ozaki 	db_printf(" gwroute=%p llinfo=%p\n",
   2363  1.162     ozaki 			  rt->rt_gwroute, rt->rt_llinfo);
   2364  1.162     ozaki 
   2365  1.162     ozaki 	return 0;
   2366  1.162     ozaki }
   2367  1.162     ozaki 
   2368  1.162     ozaki /*
   2369  1.162     ozaki  * Function to print all the route trees.
   2370  1.162     ozaki  * Use this from ddb:  "show routes"
   2371  1.162     ozaki  */
   2372  1.162     ozaki void
   2373  1.162     ozaki db_show_routes(db_expr_t addr, bool have_addr,
   2374  1.162     ozaki     db_expr_t count, const char *modif)
   2375  1.162     ozaki {
   2376  1.162     ozaki 	rt_walktree(AF_INET, db_show_rtentry, NULL);
   2377  1.162     ozaki }
   2378  1.162     ozaki #endif
   2379