10Sstevel@tonic-gate /* 2*1676Sjpk * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 30Sstevel@tonic-gate * Use is subject to license terms. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * Copyright (c) 1983, 1988, 1993 60Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 90Sstevel@tonic-gate * modification, are permitted provided that the following conditions 100Sstevel@tonic-gate * are met: 110Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 120Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 130Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 140Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 150Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 160Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 170Sstevel@tonic-gate * must display the following acknowledgment: 180Sstevel@tonic-gate * This product includes software developed by the University of 190Sstevel@tonic-gate * California, Berkeley and its contributors. 200Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 210Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 220Sstevel@tonic-gate * without specific prior written permission. 230Sstevel@tonic-gate * 240Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 250Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 260Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 270Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 280Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 290Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 300Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 310Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 320Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 330Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 340Sstevel@tonic-gate * SUCH DAMAGE. 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * $FreeBSD: src/sbin/routed/table.c,v 1.15 2000/08/11 08:24:38 sheldonh Exp $ 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 400Sstevel@tonic-gate 410Sstevel@tonic-gate #include "defs.h" 420Sstevel@tonic-gate #include <fcntl.h> 430Sstevel@tonic-gate #include <stropts.h> 440Sstevel@tonic-gate #include <sys/tihdr.h> 450Sstevel@tonic-gate #include <inet/mib2.h> 460Sstevel@tonic-gate #include <inet/ip.h> 470Sstevel@tonic-gate 480Sstevel@tonic-gate /* This structure is used to store a disassembled routing socket message. */ 490Sstevel@tonic-gate struct rt_addrinfo { 500Sstevel@tonic-gate int rti_addrs; 510Sstevel@tonic-gate struct sockaddr_storage *rti_info[RTAX_MAX]; 520Sstevel@tonic-gate }; 530Sstevel@tonic-gate 540Sstevel@tonic-gate static struct rt_spare *rts_better(struct rt_entry *); 550Sstevel@tonic-gate static struct rt_spare rts_empty = EMPTY_RT_SPARE; 560Sstevel@tonic-gate static void set_need_flash(void); 570Sstevel@tonic-gate static void rtbad(struct rt_entry *, struct interface *); 580Sstevel@tonic-gate static int rt_xaddrs(struct rt_addrinfo *, struct sockaddr_storage *, 590Sstevel@tonic-gate char *, int); 600Sstevel@tonic-gate static struct interface *gwkludge_iflookup(in_addr_t, in_addr_t, in_addr_t); 610Sstevel@tonic-gate 620Sstevel@tonic-gate struct radix_node_head *rhead; /* root of the radix tree */ 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* Flash update needed. _B_TRUE to suppress the 1st. */ 650Sstevel@tonic-gate boolean_t need_flash = _B_TRUE; 660Sstevel@tonic-gate 670Sstevel@tonic-gate struct timeval age_timer; /* next check of old routes */ 680Sstevel@tonic-gate struct timeval need_kern = { /* need to update kernel table */ 690Sstevel@tonic-gate EPOCH+MIN_WAITTIME-1, 0 700Sstevel@tonic-gate }; 710Sstevel@tonic-gate 720Sstevel@tonic-gate static uint32_t total_routes; 730Sstevel@tonic-gate 740Sstevel@tonic-gate #define ROUNDUP_LONG(a) \ 750Sstevel@tonic-gate ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long)) 760Sstevel@tonic-gate 770Sstevel@tonic-gate /* 780Sstevel@tonic-gate * It is desirable to "aggregate" routes, to combine differing routes of 790Sstevel@tonic-gate * the same metric and next hop into a common route with a smaller netmask 800Sstevel@tonic-gate * or to suppress redundant routes, routes that add no information to 810Sstevel@tonic-gate * routes with smaller netmasks. 820Sstevel@tonic-gate * 830Sstevel@tonic-gate * A route is redundant if and only if any and all routes with smaller 840Sstevel@tonic-gate * but matching netmasks and nets are the same. Since routes are 850Sstevel@tonic-gate * kept sorted in the radix tree, redundant routes always come second. 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * There are two kinds of aggregations. First, two routes of the same bit 880Sstevel@tonic-gate * mask and differing only in the least significant bit of the network 890Sstevel@tonic-gate * number can be combined into a single route with a coarser mask. 900Sstevel@tonic-gate * 910Sstevel@tonic-gate * Second, a route can be suppressed in favor of another route with a more 920Sstevel@tonic-gate * coarse mask provided no incompatible routes with intermediate masks 930Sstevel@tonic-gate * are present. The second kind of aggregation involves suppressing routes. 940Sstevel@tonic-gate * A route must not be suppressed if an incompatible route exists with 950Sstevel@tonic-gate * an intermediate mask, since the suppressed route would be covered 960Sstevel@tonic-gate * by the intermediate. 970Sstevel@tonic-gate * 980Sstevel@tonic-gate * This code relies on the radix tree walk encountering routes 990Sstevel@tonic-gate * sorted first by address, with the smallest address first. 1000Sstevel@tonic-gate */ 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate static struct ag_info ag_slots[NUM_AG_SLOTS], *ag_avail, *ag_corsest, 1030Sstevel@tonic-gate *ag_finest; 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate #ifdef DEBUG_AG 1060Sstevel@tonic-gate #define CHECK_AG() do { int acnt = 0; struct ag_info *cag; \ 1070Sstevel@tonic-gate for (cag = ag_avail; cag != NULL; cag = cag->ag_fine) \ 1080Sstevel@tonic-gate acnt++; \ 1090Sstevel@tonic-gate for (cag = ag_corsest; cag != NULL; cag = cag->ag_fine) \ 1100Sstevel@tonic-gate acnt++; \ 1110Sstevel@tonic-gate if (acnt != NUM_AG_SLOTS) \ 1120Sstevel@tonic-gate abort(); \ 1130Sstevel@tonic-gate } while (_B_FALSE) 1140Sstevel@tonic-gate #else 1150Sstevel@tonic-gate #define CHECK_AG() (void)0 1160Sstevel@tonic-gate #endif 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* 1200Sstevel@tonic-gate * Output the contents of an aggregation table slot. 1210Sstevel@tonic-gate * This function must always be immediately followed with the deletion 1220Sstevel@tonic-gate * of the target slot. 1230Sstevel@tonic-gate */ 1240Sstevel@tonic-gate static void 1250Sstevel@tonic-gate ag_out(struct ag_info *ag, void (*out)(struct ag_info *)) 1260Sstevel@tonic-gate { 1270Sstevel@tonic-gate struct ag_info *ag_cors; 1280Sstevel@tonic-gate uint32_t bit; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate /* Forget it if this route should not be output for split-horizon. */ 1320Sstevel@tonic-gate if (ag->ag_state & AGS_SPLIT_HZ) 1330Sstevel@tonic-gate return; 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate /* 1360Sstevel@tonic-gate * If we output both the even and odd twins, then the immediate parent, 1370Sstevel@tonic-gate * if it is present, is redundant, unless the parent manages to 1380Sstevel@tonic-gate * aggregate into something coarser. 1390Sstevel@tonic-gate * On successive calls, this code detects the even and odd twins, 1400Sstevel@tonic-gate * and marks the parent. 1410Sstevel@tonic-gate * 1420Sstevel@tonic-gate * Note that the order in which the radix tree code emits routes 1430Sstevel@tonic-gate * ensures that the twins are seen before the parent is emitted. 1440Sstevel@tonic-gate */ 1450Sstevel@tonic-gate ag_cors = ag->ag_cors; 1460Sstevel@tonic-gate if (ag_cors != NULL && 1470Sstevel@tonic-gate ag_cors->ag_mask == (ag->ag_mask << 1) && 1480Sstevel@tonic-gate ag_cors->ag_dst_h == (ag->ag_dst_h & ag_cors->ag_mask)) { 1490Sstevel@tonic-gate ag_cors->ag_state |= ((ag_cors->ag_dst_h == ag->ag_dst_h) ? 1500Sstevel@tonic-gate AGS_REDUN0 : AGS_REDUN1); 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate /* 1540Sstevel@tonic-gate * Skip it if this route is itself redundant. 1550Sstevel@tonic-gate * 1560Sstevel@tonic-gate * It is ok to change the contents of the slot here, since it is 1570Sstevel@tonic-gate * always deleted next. 1580Sstevel@tonic-gate */ 1590Sstevel@tonic-gate if (ag->ag_state & AGS_REDUN0) { 1600Sstevel@tonic-gate if (ag->ag_state & AGS_REDUN1) 1610Sstevel@tonic-gate return; /* quit if fully redundant */ 1620Sstevel@tonic-gate /* make it finer if it is half-redundant */ 1630Sstevel@tonic-gate bit = (-ag->ag_mask) >> 1; 1640Sstevel@tonic-gate ag->ag_dst_h |= bit; 1650Sstevel@tonic-gate ag->ag_mask |= bit; 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate } else if (ag->ag_state & AGS_REDUN1) { 1680Sstevel@tonic-gate /* make it finer if it is half-redundant */ 1690Sstevel@tonic-gate bit = (-ag->ag_mask) >> 1; 1700Sstevel@tonic-gate ag->ag_mask |= bit; 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate out(ag); 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate static void 1770Sstevel@tonic-gate ag_del(struct ag_info *ag) 1780Sstevel@tonic-gate { 1790Sstevel@tonic-gate CHECK_AG(); 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate if (ag->ag_cors == NULL) 1820Sstevel@tonic-gate ag_corsest = ag->ag_fine; 1830Sstevel@tonic-gate else 1840Sstevel@tonic-gate ag->ag_cors->ag_fine = ag->ag_fine; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate if (ag->ag_fine == NULL) 1870Sstevel@tonic-gate ag_finest = ag->ag_cors; 1880Sstevel@tonic-gate else 1890Sstevel@tonic-gate ag->ag_fine->ag_cors = ag->ag_cors; 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate ag->ag_fine = ag_avail; 1920Sstevel@tonic-gate ag_avail = ag; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate CHECK_AG(); 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate /* Look for a route that can suppress the given route. */ 1990Sstevel@tonic-gate static struct ag_info * 2000Sstevel@tonic-gate ag_find_suppressor(struct ag_info *ag) 2010Sstevel@tonic-gate { 2020Sstevel@tonic-gate struct ag_info *ag_cors; 2030Sstevel@tonic-gate in_addr_t dst_h = ag->ag_dst_h; 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate for (ag_cors = ag->ag_cors; ag_cors != NULL; 2060Sstevel@tonic-gate ag_cors = ag_cors->ag_cors) { 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate if ((dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h) { 2090Sstevel@tonic-gate /* 2100Sstevel@tonic-gate * We found a route with a coarser mask that covers 2110Sstevel@tonic-gate * the given target. It can suppress the target 2120Sstevel@tonic-gate * only if it has a good enough metric and it 2130Sstevel@tonic-gate * either has the same (gateway, ifp), or if its state 2140Sstevel@tonic-gate * includes AGS_CORS_GATE or the target's state 2150Sstevel@tonic-gate * includes AGS_FINE_GATE. 2160Sstevel@tonic-gate */ 2170Sstevel@tonic-gate if (ag_cors->ag_pref <= ag->ag_pref && 2180Sstevel@tonic-gate (((ag->ag_nhop == ag_cors->ag_nhop) && 2190Sstevel@tonic-gate (ag->ag_ifp == ag_cors->ag_ifp)) || 2200Sstevel@tonic-gate ag_cors->ag_state & AGS_CORS_GATE || 2210Sstevel@tonic-gate ag->ag_state & AGS_FINE_GATE)) { 2220Sstevel@tonic-gate return (ag_cors); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate return (NULL); 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* 2320Sstevel@tonic-gate * Flush routes waiting for aggregation. 2330Sstevel@tonic-gate * This must not suppress a route unless it is known that among all routes 2340Sstevel@tonic-gate * with coarser masks that match it, the one with the longest mask is 2350Sstevel@tonic-gate * appropriate. This is ensured by scanning the routes in lexical order, 2360Sstevel@tonic-gate * and with the most restrictive mask first among routes to the same 2370Sstevel@tonic-gate * destination. 2380Sstevel@tonic-gate */ 2390Sstevel@tonic-gate void 2400Sstevel@tonic-gate ag_flush(in_addr_t lim_dst_h, /* flush routes to here */ 2410Sstevel@tonic-gate in_addr_t lim_mask, /* matching this mask */ 2420Sstevel@tonic-gate void (*out)(struct ag_info *)) 2430Sstevel@tonic-gate { 2440Sstevel@tonic-gate struct ag_info *ag, *ag_cors, *ag_supr; 2450Sstevel@tonic-gate in_addr_t dst_h; 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate for (ag = ag_finest; ag != NULL && ag->ag_mask >= lim_mask; 2490Sstevel@tonic-gate ag = ag_cors) { 2500Sstevel@tonic-gate /* Get the next route now, before we delete ag. */ 2510Sstevel@tonic-gate ag_cors = ag->ag_cors; 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate /* Work on only the specified routes. */ 2540Sstevel@tonic-gate dst_h = ag->ag_dst_h; 2550Sstevel@tonic-gate if ((dst_h & lim_mask) != lim_dst_h) 2560Sstevel@tonic-gate continue; 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate /* 2590Sstevel@tonic-gate * Don't try to suppress the route if its state doesn't 2600Sstevel@tonic-gate * include AGS_SUPPRESS. 2610Sstevel@tonic-gate */ 2620Sstevel@tonic-gate if (!(ag->ag_state & AGS_SUPPRESS)) { 2630Sstevel@tonic-gate ag_out(ag, out); 2640Sstevel@tonic-gate ag_del(ag); 2650Sstevel@tonic-gate continue; 2660Sstevel@tonic-gate } 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate ag_supr = ag_find_suppressor(ag); 2690Sstevel@tonic-gate if (ag_supr == NULL) { 2700Sstevel@tonic-gate /* 2710Sstevel@tonic-gate * We didn't find a route which suppresses the 2720Sstevel@tonic-gate * target, so the target can go out. 2730Sstevel@tonic-gate */ 2740Sstevel@tonic-gate ag_out(ag, out); 2750Sstevel@tonic-gate } else { 2760Sstevel@tonic-gate /* 2770Sstevel@tonic-gate * We found a route which suppresses the target, so 2780Sstevel@tonic-gate * don't output the target. 2790Sstevel@tonic-gate */ 2800Sstevel@tonic-gate if (TRACEACTIONS) { 2810Sstevel@tonic-gate trace_misc("aggregated away %s", 2820Sstevel@tonic-gate rtname(htonl(ag->ag_dst_h), ag->ag_mask, 2830Sstevel@tonic-gate ag->ag_nhop)); 2840Sstevel@tonic-gate trace_misc("on coarser route %s", 2850Sstevel@tonic-gate rtname(htonl(ag_supr->ag_dst_h), 2860Sstevel@tonic-gate ag_supr->ag_mask, ag_supr->ag_nhop)); 2870Sstevel@tonic-gate } 2880Sstevel@tonic-gate /* 2890Sstevel@tonic-gate * If the suppressed target was redundant, then 2900Sstevel@tonic-gate * mark the suppressor as redundant. 2910Sstevel@tonic-gate */ 2920Sstevel@tonic-gate if (AG_IS_REDUN(ag->ag_state) && 2930Sstevel@tonic-gate ag_supr->ag_mask == (ag->ag_mask<<1)) { 2940Sstevel@tonic-gate if (ag_supr->ag_dst_h == dst_h) 2950Sstevel@tonic-gate ag_supr->ag_state |= AGS_REDUN0; 2960Sstevel@tonic-gate else 2970Sstevel@tonic-gate ag_supr->ag_state |= AGS_REDUN1; 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate if (ag->ag_tag != ag_supr->ag_tag) 3000Sstevel@tonic-gate ag_supr->ag_tag = 0; 3010Sstevel@tonic-gate if (ag->ag_nhop != ag_supr->ag_nhop) 3020Sstevel@tonic-gate ag_supr->ag_nhop = 0; 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate /* The route has either been output or suppressed */ 3060Sstevel@tonic-gate ag_del(ag); 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate CHECK_AG(); 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate /* Try to aggregate a route with previous routes. */ 3140Sstevel@tonic-gate void 3150Sstevel@tonic-gate ag_check(in_addr_t dst, 3160Sstevel@tonic-gate in_addr_t mask, 3170Sstevel@tonic-gate in_addr_t gate, 3180Sstevel@tonic-gate struct interface *ifp, 3190Sstevel@tonic-gate in_addr_t nhop, 3200Sstevel@tonic-gate uint8_t metric, 3210Sstevel@tonic-gate uint8_t pref, 3220Sstevel@tonic-gate uint32_t seqno, 3230Sstevel@tonic-gate uint16_t tag, 3240Sstevel@tonic-gate uint16_t state, 3250Sstevel@tonic-gate void (*out)(struct ag_info *)) /* output using this */ 3260Sstevel@tonic-gate { 3270Sstevel@tonic-gate struct ag_info *ag, *nag, *ag_cors; 3280Sstevel@tonic-gate in_addr_t xaddr; 3290Sstevel@tonic-gate int tmp; 3300Sstevel@tonic-gate struct interface *xifp; 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate dst = ntohl(dst); 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate /* 3350Sstevel@tonic-gate * Don't bother trying to aggregate routes with non-contiguous 3360Sstevel@tonic-gate * subnet masks. 3370Sstevel@tonic-gate * 3380Sstevel@tonic-gate * (X & -X) contains a single bit if and only if X is a power of 2. 3390Sstevel@tonic-gate * (X + (X & -X)) == 0 if and only if X is a power of 2. 3400Sstevel@tonic-gate */ 3410Sstevel@tonic-gate if ((mask & -mask) + mask != 0) { 3420Sstevel@tonic-gate struct ag_info nc_ag; 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate nc_ag.ag_dst_h = dst; 3450Sstevel@tonic-gate nc_ag.ag_mask = mask; 3460Sstevel@tonic-gate nc_ag.ag_gate = gate; 3470Sstevel@tonic-gate nc_ag.ag_ifp = ifp; 3480Sstevel@tonic-gate nc_ag.ag_nhop = nhop; 3490Sstevel@tonic-gate nc_ag.ag_metric = metric; 3500Sstevel@tonic-gate nc_ag.ag_pref = pref; 3510Sstevel@tonic-gate nc_ag.ag_tag = tag; 3520Sstevel@tonic-gate nc_ag.ag_state = state; 3530Sstevel@tonic-gate nc_ag.ag_seqno = seqno; 3540Sstevel@tonic-gate out(&nc_ag); 3550Sstevel@tonic-gate return; 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate /* Search for the right slot in the aggregation table. */ 3590Sstevel@tonic-gate ag_cors = NULL; 3600Sstevel@tonic-gate ag = ag_corsest; 3610Sstevel@tonic-gate while (ag != NULL) { 3620Sstevel@tonic-gate if (ag->ag_mask >= mask) 3630Sstevel@tonic-gate break; 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate /* 3660Sstevel@tonic-gate * Suppress old routes (i.e. combine with compatible routes 3670Sstevel@tonic-gate * with coarser masks) as we look for the right slot in the 3680Sstevel@tonic-gate * aggregation table for the new route. 3690Sstevel@tonic-gate * A route to an address less than the current destination 3700Sstevel@tonic-gate * will not be affected by the current route or any route 3710Sstevel@tonic-gate * seen hereafter. That means it is safe to suppress it. 3720Sstevel@tonic-gate * This check keeps poor routes (e.g. with large hop counts) 3730Sstevel@tonic-gate * from preventing suppression of finer routes. 3740Sstevel@tonic-gate */ 3750Sstevel@tonic-gate if (ag_cors != NULL && ag->ag_dst_h < dst && 3760Sstevel@tonic-gate (ag->ag_state & AGS_SUPPRESS) && 3770Sstevel@tonic-gate ag_cors->ag_pref <= ag->ag_pref && 3780Sstevel@tonic-gate (ag->ag_dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h && 3790Sstevel@tonic-gate ((ag_cors->ag_nhop == ag->ag_nhop && 3800Sstevel@tonic-gate (ag_cors->ag_ifp == ag->ag_ifp))|| 3810Sstevel@tonic-gate (ag->ag_state & AGS_FINE_GATE) || 3820Sstevel@tonic-gate (ag_cors->ag_state & AGS_CORS_GATE))) { 3830Sstevel@tonic-gate /* 3840Sstevel@tonic-gate * If the suppressed target was redundant, 3850Sstevel@tonic-gate * then mark the suppressor redundant. 3860Sstevel@tonic-gate */ 3870Sstevel@tonic-gate if (AG_IS_REDUN(ag->ag_state) && 3880Sstevel@tonic-gate ag_cors->ag_mask == (ag->ag_mask << 1)) { 3890Sstevel@tonic-gate if (ag_cors->ag_dst_h == dst) 3900Sstevel@tonic-gate ag_cors->ag_state |= AGS_REDUN0; 3910Sstevel@tonic-gate else 3920Sstevel@tonic-gate ag_cors->ag_state |= AGS_REDUN1; 3930Sstevel@tonic-gate } 3940Sstevel@tonic-gate if (ag->ag_tag != ag_cors->ag_tag) 3950Sstevel@tonic-gate ag_cors->ag_tag = 0; 3960Sstevel@tonic-gate if (ag->ag_nhop != ag_cors->ag_nhop) 3970Sstevel@tonic-gate ag_cors->ag_nhop = 0; 3980Sstevel@tonic-gate ag_del(ag); 3990Sstevel@tonic-gate CHECK_AG(); 4000Sstevel@tonic-gate } else { 4010Sstevel@tonic-gate ag_cors = ag; 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate ag = ag_cors->ag_fine; 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate /* 4070Sstevel@tonic-gate * If we find the even/odd twin of the new route, and if the 4080Sstevel@tonic-gate * masks and so forth are equal, we can aggregate them. 4090Sstevel@tonic-gate * We can probably promote one of the pair. 4100Sstevel@tonic-gate * 4110Sstevel@tonic-gate * Since the routes are encountered in lexical order, 4120Sstevel@tonic-gate * the new route must be odd. However, the second or later 4130Sstevel@tonic-gate * times around this loop, it could be the even twin promoted 4140Sstevel@tonic-gate * from the even/odd pair of twins of the finer route. 4150Sstevel@tonic-gate */ 4160Sstevel@tonic-gate while (ag != NULL && ag->ag_mask == mask && 4170Sstevel@tonic-gate ((ag->ag_dst_h ^ dst) & (mask<<1)) == 0) { 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate /* 4200Sstevel@tonic-gate * Here we know the target route and the route in the current 4210Sstevel@tonic-gate * slot have the same netmasks and differ by at most the 4220Sstevel@tonic-gate * last bit. They are either for the same destination, or 4230Sstevel@tonic-gate * for an even/odd pair of destinations. 4240Sstevel@tonic-gate */ 4250Sstevel@tonic-gate if (ag->ag_dst_h == dst) { 4260Sstevel@tonic-gate if (ag->ag_nhop == nhop && ag->ag_ifp == ifp) { 4270Sstevel@tonic-gate /* 4280Sstevel@tonic-gate * We have two routes to the same destination, 4290Sstevel@tonic-gate * with the same nexthop and interface. 4300Sstevel@tonic-gate * Routes are encountered in lexical order, 4310Sstevel@tonic-gate * so a route is never promoted until the 4320Sstevel@tonic-gate * parent route is already present. So we 4330Sstevel@tonic-gate * know that the new route is a promoted (or 4340Sstevel@tonic-gate * aggregated) pair and the route already in 4350Sstevel@tonic-gate * the slot is the explicit route. 4360Sstevel@tonic-gate * 4370Sstevel@tonic-gate * Prefer the best route if their metrics 4380Sstevel@tonic-gate * differ, or the aggregated one if not, 4390Sstevel@tonic-gate * following a sort of longest-match rule. 4400Sstevel@tonic-gate */ 4410Sstevel@tonic-gate if (pref <= ag->ag_pref) { 4420Sstevel@tonic-gate ag->ag_gate = gate; 4430Sstevel@tonic-gate ag->ag_ifp = ifp; 4440Sstevel@tonic-gate ag->ag_nhop = nhop; 4450Sstevel@tonic-gate ag->ag_tag = tag; 4460Sstevel@tonic-gate ag->ag_metric = metric; 4470Sstevel@tonic-gate ag->ag_pref = pref; 4480Sstevel@tonic-gate if (seqno > ag->ag_seqno) 4490Sstevel@tonic-gate ag->ag_seqno = seqno; 4500Sstevel@tonic-gate tmp = ag->ag_state; 4510Sstevel@tonic-gate ag->ag_state = state; 4520Sstevel@tonic-gate state = tmp; 4530Sstevel@tonic-gate } 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate /* 4560Sstevel@tonic-gate * Some bits are set if they are set on 4570Sstevel@tonic-gate * either route, except when the route is 4580Sstevel@tonic-gate * for an interface. 4590Sstevel@tonic-gate */ 4600Sstevel@tonic-gate if (!(ag->ag_state & AGS_IF)) 4610Sstevel@tonic-gate ag->ag_state |= 4620Sstevel@tonic-gate (state & (AGS_AGGREGATE_EITHER | 4630Sstevel@tonic-gate AGS_REDUN0 | AGS_REDUN1)); 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate return; 4660Sstevel@tonic-gate } else { 4670Sstevel@tonic-gate /* 4680Sstevel@tonic-gate * multiple routes to same dest/mask with 4690Sstevel@tonic-gate * differing gate nexthop/or ifp. Flush 4700Sstevel@tonic-gate * both out. 4710Sstevel@tonic-gate */ 4720Sstevel@tonic-gate break; 4730Sstevel@tonic-gate } 4740Sstevel@tonic-gate } 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate /* 4770Sstevel@tonic-gate * If one of the routes can be promoted and the other can 4780Sstevel@tonic-gate * be suppressed, it may be possible to combine them or 4790Sstevel@tonic-gate * worthwhile to promote one. 4800Sstevel@tonic-gate * 4810Sstevel@tonic-gate * Any route that can be promoted is always 4820Sstevel@tonic-gate * marked to be eligible to be suppressed. 4830Sstevel@tonic-gate */ 4840Sstevel@tonic-gate if (!((state & AGS_AGGREGATE) && 4850Sstevel@tonic-gate (ag->ag_state & AGS_SUPPRESS)) && 4860Sstevel@tonic-gate !((ag->ag_state & AGS_AGGREGATE) && (state & AGS_SUPPRESS))) 4870Sstevel@tonic-gate break; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate /* 4900Sstevel@tonic-gate * A pair of even/odd twin routes can be combined 4910Sstevel@tonic-gate * if either is redundant, or if they are via the 4920Sstevel@tonic-gate * same gateway and have the same metric. 4930Sstevel@tonic-gate */ 4940Sstevel@tonic-gate if (AG_IS_REDUN(ag->ag_state) || AG_IS_REDUN(state) || 4950Sstevel@tonic-gate (ag->ag_nhop == nhop && ag->ag_ifp == ifp && 4960Sstevel@tonic-gate ag->ag_pref == pref && 4970Sstevel@tonic-gate (state & ag->ag_state & AGS_AGGREGATE) != 0)) { 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate /* 5000Sstevel@tonic-gate * We have both the even and odd pairs. 5010Sstevel@tonic-gate * Since the routes are encountered in order, 5020Sstevel@tonic-gate * the route in the slot must be the even twin. 5030Sstevel@tonic-gate * 5040Sstevel@tonic-gate * Combine and promote (aggregate) the pair of routes. 5050Sstevel@tonic-gate */ 5060Sstevel@tonic-gate if (seqno < ag->ag_seqno) 5070Sstevel@tonic-gate seqno = ag->ag_seqno; 5080Sstevel@tonic-gate if (!AG_IS_REDUN(state)) 5090Sstevel@tonic-gate state &= ~AGS_REDUN1; 5100Sstevel@tonic-gate if (AG_IS_REDUN(ag->ag_state)) 5110Sstevel@tonic-gate state |= AGS_REDUN0; 5120Sstevel@tonic-gate else 5130Sstevel@tonic-gate state &= ~AGS_REDUN0; 5140Sstevel@tonic-gate state |= (ag->ag_state & AGS_AGGREGATE_EITHER); 5150Sstevel@tonic-gate if (ag->ag_tag != tag) 5160Sstevel@tonic-gate tag = 0; 5170Sstevel@tonic-gate if (ag->ag_nhop != nhop) 5180Sstevel@tonic-gate nhop = 0; 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate /* 5210Sstevel@tonic-gate * Get rid of the even twin that was already 5220Sstevel@tonic-gate * in the slot. 5230Sstevel@tonic-gate */ 5240Sstevel@tonic-gate ag_del(ag); 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate } else if (ag->ag_pref >= pref && 5270Sstevel@tonic-gate (ag->ag_state & AGS_AGGREGATE)) { 5280Sstevel@tonic-gate /* 5290Sstevel@tonic-gate * If we cannot combine the pair, maybe the route 5300Sstevel@tonic-gate * with the worse metric can be promoted. 5310Sstevel@tonic-gate * 5320Sstevel@tonic-gate * Promote the old, even twin, by giving its slot 5330Sstevel@tonic-gate * in the table to the new, odd twin. 5340Sstevel@tonic-gate */ 5350Sstevel@tonic-gate ag->ag_dst_h = dst; 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate xaddr = ag->ag_gate; 5380Sstevel@tonic-gate ag->ag_gate = gate; 5390Sstevel@tonic-gate gate = xaddr; 5400Sstevel@tonic-gate 5410Sstevel@tonic-gate xifp = ag->ag_ifp; 5420Sstevel@tonic-gate ag->ag_ifp = ifp; 5430Sstevel@tonic-gate ifp = xifp; 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate xaddr = ag->ag_nhop; 5460Sstevel@tonic-gate ag->ag_nhop = nhop; 5470Sstevel@tonic-gate nhop = xaddr; 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate tmp = ag->ag_tag; 5500Sstevel@tonic-gate ag->ag_tag = tag; 5510Sstevel@tonic-gate tag = tmp; 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate /* 5540Sstevel@tonic-gate * The promoted route is even-redundant only if the 5550Sstevel@tonic-gate * even twin was fully redundant. It is not 5560Sstevel@tonic-gate * odd-redundant because the odd-twin will still be 5570Sstevel@tonic-gate * in the table. 5580Sstevel@tonic-gate */ 5590Sstevel@tonic-gate tmp = ag->ag_state; 5600Sstevel@tonic-gate if (!AG_IS_REDUN(tmp)) 5610Sstevel@tonic-gate tmp &= ~AGS_REDUN0; 5620Sstevel@tonic-gate tmp &= ~AGS_REDUN1; 5630Sstevel@tonic-gate ag->ag_state = state; 5640Sstevel@tonic-gate state = tmp; 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate tmp = ag->ag_metric; 5670Sstevel@tonic-gate ag->ag_metric = metric; 5680Sstevel@tonic-gate metric = tmp; 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate tmp = ag->ag_pref; 5710Sstevel@tonic-gate ag->ag_pref = pref; 5720Sstevel@tonic-gate pref = tmp; 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate /* take the newest sequence number */ 5750Sstevel@tonic-gate if (seqno <= ag->ag_seqno) 5760Sstevel@tonic-gate seqno = ag->ag_seqno; 5770Sstevel@tonic-gate else 5780Sstevel@tonic-gate ag->ag_seqno = seqno; 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate } else { 5810Sstevel@tonic-gate if (!(state & AGS_AGGREGATE)) 5820Sstevel@tonic-gate break; /* cannot promote either twin */ 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate /* 5850Sstevel@tonic-gate * Promote the new, odd twin by shaving its 5860Sstevel@tonic-gate * mask and address. 5870Sstevel@tonic-gate * The promoted route is odd-redundant only if the 5880Sstevel@tonic-gate * odd twin was fully redundant. It is not 5890Sstevel@tonic-gate * even-redundant because the even twin is still in 5900Sstevel@tonic-gate * the table. 5910Sstevel@tonic-gate */ 5920Sstevel@tonic-gate if (!AG_IS_REDUN(state)) 5930Sstevel@tonic-gate state &= ~AGS_REDUN1; 5940Sstevel@tonic-gate state &= ~AGS_REDUN0; 5950Sstevel@tonic-gate if (seqno < ag->ag_seqno) 5960Sstevel@tonic-gate seqno = ag->ag_seqno; 5970Sstevel@tonic-gate else 5980Sstevel@tonic-gate ag->ag_seqno = seqno; 5990Sstevel@tonic-gate } 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate mask <<= 1; 6020Sstevel@tonic-gate dst &= mask; 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate if (ag_cors == NULL) { 6050Sstevel@tonic-gate ag = ag_corsest; 6060Sstevel@tonic-gate break; 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate ag = ag_cors; 6090Sstevel@tonic-gate ag_cors = ag->ag_cors; 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate /* 6130Sstevel@tonic-gate * When we can no longer promote and combine routes, 6140Sstevel@tonic-gate * flush the old route in the target slot. Also flush 6150Sstevel@tonic-gate * any finer routes that we know will never be aggregated by 6160Sstevel@tonic-gate * the new route. 6170Sstevel@tonic-gate * 6180Sstevel@tonic-gate * In case we moved toward coarser masks, 6190Sstevel@tonic-gate * get back where we belong 6200Sstevel@tonic-gate */ 6210Sstevel@tonic-gate if (ag != NULL && ag->ag_mask < mask) { 6220Sstevel@tonic-gate ag_cors = ag; 6230Sstevel@tonic-gate ag = ag->ag_fine; 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate /* Empty the target slot */ 6270Sstevel@tonic-gate if (ag != NULL && ag->ag_mask == mask) { 6280Sstevel@tonic-gate ag_flush(ag->ag_dst_h, ag->ag_mask, out); 6290Sstevel@tonic-gate ag = (ag_cors == NULL) ? ag_corsest : ag_cors->ag_fine; 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate #ifdef DEBUG_AG 6330Sstevel@tonic-gate if (ag == NULL && ag_cors != ag_finest) 6340Sstevel@tonic-gate abort(); 6350Sstevel@tonic-gate if (ag_cors == NULL && ag != ag_corsest) 6360Sstevel@tonic-gate abort(); 6370Sstevel@tonic-gate if (ag != NULL && ag->ag_cors != ag_cors) 6380Sstevel@tonic-gate abort(); 6390Sstevel@tonic-gate if (ag_cors != NULL && ag_cors->ag_fine != ag) 6400Sstevel@tonic-gate abort(); 6410Sstevel@tonic-gate CHECK_AG(); 6420Sstevel@tonic-gate #endif 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate /* Save the new route on the end of the table. */ 6450Sstevel@tonic-gate nag = ag_avail; 6460Sstevel@tonic-gate ag_avail = nag->ag_fine; 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate nag->ag_dst_h = dst; 6490Sstevel@tonic-gate nag->ag_mask = mask; 6500Sstevel@tonic-gate nag->ag_ifp = ifp; 6510Sstevel@tonic-gate nag->ag_gate = gate; 6520Sstevel@tonic-gate nag->ag_nhop = nhop; 6530Sstevel@tonic-gate nag->ag_metric = metric; 6540Sstevel@tonic-gate nag->ag_pref = pref; 6550Sstevel@tonic-gate nag->ag_tag = tag; 6560Sstevel@tonic-gate nag->ag_state = state; 6570Sstevel@tonic-gate nag->ag_seqno = seqno; 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate nag->ag_fine = ag; 6600Sstevel@tonic-gate if (ag != NULL) 6610Sstevel@tonic-gate ag->ag_cors = nag; 6620Sstevel@tonic-gate else 6630Sstevel@tonic-gate ag_finest = nag; 6640Sstevel@tonic-gate nag->ag_cors = ag_cors; 6650Sstevel@tonic-gate if (ag_cors == NULL) 6660Sstevel@tonic-gate ag_corsest = nag; 6670Sstevel@tonic-gate else 6680Sstevel@tonic-gate ag_cors->ag_fine = nag; 6690Sstevel@tonic-gate CHECK_AG(); 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate static const char * 6740Sstevel@tonic-gate rtm_type_name(uchar_t type) 6750Sstevel@tonic-gate { 6760Sstevel@tonic-gate static const char *rtm_types[] = { 6770Sstevel@tonic-gate "RTM_ADD", 6780Sstevel@tonic-gate "RTM_DELETE", 6790Sstevel@tonic-gate "RTM_CHANGE", 6800Sstevel@tonic-gate "RTM_GET", 6810Sstevel@tonic-gate "RTM_LOSING", 6820Sstevel@tonic-gate "RTM_REDIRECT", 6830Sstevel@tonic-gate "RTM_MISS", 6840Sstevel@tonic-gate "RTM_LOCK", 6850Sstevel@tonic-gate "RTM_OLDADD", 6860Sstevel@tonic-gate "RTM_OLDDEL", 6870Sstevel@tonic-gate "RTM_RESOLVE", 6880Sstevel@tonic-gate "RTM_NEWADDR", 6890Sstevel@tonic-gate "RTM_DELADDR", 6900Sstevel@tonic-gate "RTM_IFINFO", 6910Sstevel@tonic-gate "RTM_NEWMADDR", 6920Sstevel@tonic-gate "RTM_DELMADDR" 6930Sstevel@tonic-gate }; 6940Sstevel@tonic-gate #define NEW_RTM_PAT "RTM type %#x" 6950Sstevel@tonic-gate static char name0[sizeof (NEW_RTM_PAT) + 2]; 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate if (type > sizeof (rtm_types) / sizeof (rtm_types[0]) || type == 0) { 6980Sstevel@tonic-gate (void) snprintf(name0, sizeof (name0), NEW_RTM_PAT, type); 6990Sstevel@tonic-gate return (name0); 7000Sstevel@tonic-gate } else { 7010Sstevel@tonic-gate return (rtm_types[type-1]); 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate #undef NEW_RTM_PAT 7040Sstevel@tonic-gate } 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate static void 7080Sstevel@tonic-gate dump_rt_msg(const char *act, struct rt_msghdr *rtm, int mlen) 7090Sstevel@tonic-gate { 7100Sstevel@tonic-gate const char *mtype; 7110Sstevel@tonic-gate uchar_t *cp; 7120Sstevel@tonic-gate int i, j; 7130Sstevel@tonic-gate char buffer[16*3 + 1], *ibs; 7140Sstevel@tonic-gate struct ifa_msghdr *ifam; 7150Sstevel@tonic-gate struct if_msghdr *ifm; 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate switch (rtm->rtm_type) { 7180Sstevel@tonic-gate case RTM_NEWADDR: 7190Sstevel@tonic-gate case RTM_DELADDR: 7200Sstevel@tonic-gate mtype = "ifam"; 7210Sstevel@tonic-gate break; 7220Sstevel@tonic-gate case RTM_IFINFO: 7230Sstevel@tonic-gate mtype = "ifm"; 7240Sstevel@tonic-gate break; 7250Sstevel@tonic-gate default: 7260Sstevel@tonic-gate mtype = "rtm"; 7270Sstevel@tonic-gate break; 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate trace_misc("%s %s %d bytes", act, mtype, mlen); 7300Sstevel@tonic-gate if (mlen > rtm->rtm_msglen) { 7310Sstevel@tonic-gate trace_misc("%s: extra %d bytes ignored", mtype, 7320Sstevel@tonic-gate mlen - rtm->rtm_msglen); 7330Sstevel@tonic-gate mlen = rtm->rtm_msglen; 7340Sstevel@tonic-gate } else if (mlen < rtm->rtm_msglen) { 7350Sstevel@tonic-gate trace_misc("%s: truncated by %d bytes", mtype, 7360Sstevel@tonic-gate rtm->rtm_msglen - mlen); 7370Sstevel@tonic-gate } 7380Sstevel@tonic-gate switch (rtm->rtm_type) { 7390Sstevel@tonic-gate case RTM_NEWADDR: 7400Sstevel@tonic-gate case RTM_DELADDR: 7410Sstevel@tonic-gate ifam = (struct ifa_msghdr *)rtm; 7420Sstevel@tonic-gate trace_misc("ifam: msglen %d version %d type %d addrs %X", 7430Sstevel@tonic-gate ifam->ifam_msglen, ifam->ifam_version, ifam->ifam_type, 7440Sstevel@tonic-gate ifam->ifam_addrs); 7450Sstevel@tonic-gate trace_misc("ifam: flags %X index %d metric %d", 7460Sstevel@tonic-gate ifam->ifam_flags, ifam->ifam_index, ifam->ifam_metric); 7470Sstevel@tonic-gate cp = (uchar_t *)(ifam + 1); 7480Sstevel@tonic-gate break; 7490Sstevel@tonic-gate case RTM_IFINFO: 7500Sstevel@tonic-gate ifm = (struct if_msghdr *)rtm; 7510Sstevel@tonic-gate trace_misc("ifm: msglen %d version %d type %d addrs %X", 7520Sstevel@tonic-gate ifm->ifm_msglen, ifm->ifm_version, ifm->ifm_type, 7530Sstevel@tonic-gate ifm->ifm_addrs); 7540Sstevel@tonic-gate ibs = if_bit_string(ifm->ifm_flags, _B_TRUE); 7550Sstevel@tonic-gate if (ibs == NULL) { 7560Sstevel@tonic-gate trace_misc("ifm: flags %#x index %d", ifm->ifm_flags, 7570Sstevel@tonic-gate ifm->ifm_index); 7580Sstevel@tonic-gate } else { 7590Sstevel@tonic-gate trace_misc("ifm: flags %s index %d", ibs, 7600Sstevel@tonic-gate ifm->ifm_index); 7610Sstevel@tonic-gate free(ibs); 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate cp = (uchar_t *)(ifm + 1); 7640Sstevel@tonic-gate break; 7650Sstevel@tonic-gate default: 7660Sstevel@tonic-gate trace_misc("rtm: msglen %d version %d type %d index %d", 7670Sstevel@tonic-gate rtm->rtm_msglen, rtm->rtm_version, rtm->rtm_type, 7680Sstevel@tonic-gate rtm->rtm_index); 7690Sstevel@tonic-gate trace_misc("rtm: flags %X addrs %X pid %d seq %d", 7700Sstevel@tonic-gate rtm->rtm_flags, rtm->rtm_addrs, rtm->rtm_pid, rtm->rtm_seq); 7710Sstevel@tonic-gate trace_misc("rtm: errno %d use %d inits %X", rtm->rtm_errno, 7720Sstevel@tonic-gate rtm->rtm_use, rtm->rtm_inits); 7730Sstevel@tonic-gate cp = (uchar_t *)(rtm + 1); 7740Sstevel@tonic-gate break; 7750Sstevel@tonic-gate } 7760Sstevel@tonic-gate i = mlen - (cp - (uint8_t *)rtm); 7770Sstevel@tonic-gate while (i > 0) { 7780Sstevel@tonic-gate buffer[0] = '\0'; 7790Sstevel@tonic-gate ibs = buffer; 7800Sstevel@tonic-gate for (j = 0; j < 16 && i > 0; j++, i--) 7810Sstevel@tonic-gate ibs += sprintf(ibs, " %02X", *cp++); 7820Sstevel@tonic-gate trace_misc("addr%s", buffer); 7830Sstevel@tonic-gate } 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate 7860Sstevel@tonic-gate /* 7870Sstevel@tonic-gate * Tell the kernel to add, delete or change a route 7880Sstevel@tonic-gate * Pass k_state from khash in for diagnostic info. 7890Sstevel@tonic-gate */ 7900Sstevel@tonic-gate static void 7910Sstevel@tonic-gate rtioctl(int action, /* RTM_DELETE, etc */ 7920Sstevel@tonic-gate in_addr_t dst, 7930Sstevel@tonic-gate in_addr_t gate, 7940Sstevel@tonic-gate in_addr_t mask, 7950Sstevel@tonic-gate struct interface *ifp, 7960Sstevel@tonic-gate uint8_t metric, 7970Sstevel@tonic-gate int flags) 7980Sstevel@tonic-gate { 7990Sstevel@tonic-gate static int rt_sock_seqno = 0; 8000Sstevel@tonic-gate struct { 8010Sstevel@tonic-gate struct rt_msghdr w_rtm; 8020Sstevel@tonic-gate struct sockaddr_in w_dst; 8030Sstevel@tonic-gate struct sockaddr_in w_gate; 8040Sstevel@tonic-gate uint8_t w_space[512]; 8050Sstevel@tonic-gate } w; 8060Sstevel@tonic-gate struct sockaddr_in w_mask; 8070Sstevel@tonic-gate struct sockaddr_dl w_ifp; 8080Sstevel@tonic-gate uint8_t *cp; 8090Sstevel@tonic-gate long cc; 8100Sstevel@tonic-gate #define PAT " %-10s %s metric=%d flags=%#x" 8110Sstevel@tonic-gate #define ARGS rtm_type_name(action), rtname(dst, mask, gate), metric, flags 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate again: 8140Sstevel@tonic-gate (void) memset(&w, 0, sizeof (w)); 8150Sstevel@tonic-gate (void) memset(&w_mask, 0, sizeof (w_mask)); 8160Sstevel@tonic-gate (void) memset(&w_ifp, 0, sizeof (w_ifp)); 8170Sstevel@tonic-gate cp = w.w_space; 8180Sstevel@tonic-gate w.w_rtm.rtm_msglen = sizeof (struct rt_msghdr) + 8190Sstevel@tonic-gate 2 * ROUNDUP_LONG(sizeof (struct sockaddr_in)); 8200Sstevel@tonic-gate w.w_rtm.rtm_version = RTM_VERSION; 8210Sstevel@tonic-gate w.w_rtm.rtm_type = action; 8220Sstevel@tonic-gate w.w_rtm.rtm_flags = flags; 8230Sstevel@tonic-gate w.w_rtm.rtm_seq = ++rt_sock_seqno; 8240Sstevel@tonic-gate w.w_rtm.rtm_addrs = RTA_DST|RTA_GATEWAY; 8250Sstevel@tonic-gate if (metric != 0 || action == RTM_CHANGE) { 8260Sstevel@tonic-gate w.w_rtm.rtm_rmx.rmx_hopcount = metric; 8270Sstevel@tonic-gate w.w_rtm.rtm_inits |= RTV_HOPCOUNT; 8280Sstevel@tonic-gate } 8290Sstevel@tonic-gate w.w_dst.sin_family = AF_INET; 8300Sstevel@tonic-gate w.w_dst.sin_addr.s_addr = dst; 8310Sstevel@tonic-gate w.w_gate.sin_family = AF_INET; 8320Sstevel@tonic-gate w.w_gate.sin_addr.s_addr = gate; 8330Sstevel@tonic-gate if (mask == HOST_MASK) { 8340Sstevel@tonic-gate w.w_rtm.rtm_flags |= RTF_HOST; 8350Sstevel@tonic-gate } else { 8360Sstevel@tonic-gate w.w_rtm.rtm_addrs |= RTA_NETMASK; 8370Sstevel@tonic-gate w_mask.sin_family = AF_INET; 8380Sstevel@tonic-gate w_mask.sin_addr.s_addr = htonl(mask); 8390Sstevel@tonic-gate (void) memmove(cp, &w_mask, sizeof (w_mask)); 8400Sstevel@tonic-gate cp += ROUNDUP_LONG(sizeof (struct sockaddr_in)); 8410Sstevel@tonic-gate w.w_rtm.rtm_msglen += ROUNDUP_LONG(sizeof (struct sockaddr_in)); 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate if (ifp == NULL) 8440Sstevel@tonic-gate ifp = iflookup(gate); 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate if ((ifp == NULL) || (ifp->int_phys == NULL)) { 8470Sstevel@tonic-gate trace_misc("no ifp for" PAT, ARGS); 8480Sstevel@tonic-gate } else { 8490Sstevel@tonic-gate if (ifp->int_phys->phyi_index > UINT16_MAX) { 8500Sstevel@tonic-gate trace_misc("ifindex %d is too big for sdl_index", 8510Sstevel@tonic-gate ifp->int_phys->phyi_index); 8520Sstevel@tonic-gate } else { 8530Sstevel@tonic-gate w_ifp.sdl_family = AF_LINK; 8540Sstevel@tonic-gate w.w_rtm.rtm_addrs |= RTA_IFP; 8550Sstevel@tonic-gate w_ifp.sdl_index = ifp->int_phys->phyi_index; 8560Sstevel@tonic-gate (void) memmove(cp, &w_ifp, sizeof (w_ifp)); 8570Sstevel@tonic-gate w.w_rtm.rtm_msglen += 8580Sstevel@tonic-gate ROUNDUP_LONG(sizeof (struct sockaddr_dl)); 8590Sstevel@tonic-gate } 8600Sstevel@tonic-gate } 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate if (!no_install) { 8640Sstevel@tonic-gate if (TRACERTS) 8650Sstevel@tonic-gate dump_rt_msg("write", &w.w_rtm, w.w_rtm.rtm_msglen); 8660Sstevel@tonic-gate cc = write(rt_sock, &w, w.w_rtm.rtm_msglen); 8670Sstevel@tonic-gate if (cc < 0) { 8680Sstevel@tonic-gate if (errno == ESRCH && (action == RTM_CHANGE || 8690Sstevel@tonic-gate action == RTM_DELETE)) { 8700Sstevel@tonic-gate trace_act("route disappeared before" PAT, ARGS); 8710Sstevel@tonic-gate if (action == RTM_CHANGE) { 8720Sstevel@tonic-gate action = RTM_ADD; 8730Sstevel@tonic-gate goto again; 8740Sstevel@tonic-gate } 8750Sstevel@tonic-gate return; 8760Sstevel@tonic-gate } 8770Sstevel@tonic-gate writelog(LOG_WARNING, "write(rt_sock)" PAT ": %s ", 8780Sstevel@tonic-gate ARGS, rip_strerror(errno)); 8790Sstevel@tonic-gate return; 8800Sstevel@tonic-gate } else if (cc != w.w_rtm.rtm_msglen) { 8810Sstevel@tonic-gate msglog("write(rt_sock) wrote %ld instead of %d for" PAT, 8820Sstevel@tonic-gate cc, w.w_rtm.rtm_msglen, ARGS); 8830Sstevel@tonic-gate return; 8840Sstevel@tonic-gate } 8850Sstevel@tonic-gate } 8860Sstevel@tonic-gate if (TRACEKERNEL) 8870Sstevel@tonic-gate trace_misc("write kernel" PAT, ARGS); 8880Sstevel@tonic-gate #undef PAT 8890Sstevel@tonic-gate #undef ARGS 8900Sstevel@tonic-gate } 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate /* Hash table containing our image of the kernel forwarding table. */ 8940Sstevel@tonic-gate #define KHASH_SIZE 71 /* should be prime */ 8950Sstevel@tonic-gate #define KHASH(a, m) khash_bins[((a) ^ (m)) % KHASH_SIZE] 8960Sstevel@tonic-gate static struct khash *khash_bins[KHASH_SIZE]; 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate #define K_KEEP_LIM 30 /* k_keep */ 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate static struct khash * 9010Sstevel@tonic-gate kern_find(in_addr_t dst, in_addr_t mask, in_addr_t gate, 9020Sstevel@tonic-gate struct interface *ifp, struct khash ***ppk) 9030Sstevel@tonic-gate { 9040Sstevel@tonic-gate struct khash *k, **pk; 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate for (pk = &KHASH(dst, mask); (k = *pk) != NULL; pk = &k->k_next) { 9070Sstevel@tonic-gate if (k->k_dst == dst && k->k_mask == mask && 9080Sstevel@tonic-gate (gate == 0 || k->k_gate == gate) && 9090Sstevel@tonic-gate (ifp == NULL || k->k_ifp == ifp)) { 9100Sstevel@tonic-gate break; 9110Sstevel@tonic-gate } 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate if (ppk != NULL) 9140Sstevel@tonic-gate *ppk = pk; 9150Sstevel@tonic-gate return (k); 9160Sstevel@tonic-gate } 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate 9190Sstevel@tonic-gate /* 9200Sstevel@tonic-gate * Find out if there is an alternate route to a given destination 9210Sstevel@tonic-gate * off of a given interface. 9220Sstevel@tonic-gate */ 9230Sstevel@tonic-gate static struct khash * 9240Sstevel@tonic-gate kern_alternate(in_addr_t dst, in_addr_t mask, in_addr_t gate, 9250Sstevel@tonic-gate struct interface *ifp, struct khash ***ppk) 9260Sstevel@tonic-gate { 9270Sstevel@tonic-gate struct khash *k, **pk; 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate for (pk = &KHASH(dst, mask); (k = *pk) != NULL; pk = &k->k_next) { 9300Sstevel@tonic-gate if (k->k_dst == dst && k->k_mask == mask && 9310Sstevel@tonic-gate (k->k_gate != gate) && 9320Sstevel@tonic-gate (k->k_ifp == ifp)) { 9330Sstevel@tonic-gate break; 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate } 9360Sstevel@tonic-gate if (ppk != NULL) 9370Sstevel@tonic-gate *ppk = pk; 9380Sstevel@tonic-gate return (k); 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate static struct khash * 9420Sstevel@tonic-gate kern_add(in_addr_t dst, uint32_t mask, in_addr_t gate, struct interface *ifp) 9430Sstevel@tonic-gate { 9440Sstevel@tonic-gate struct khash *k, **pk; 9450Sstevel@tonic-gate 9460Sstevel@tonic-gate k = kern_find(dst, mask, gate, ifp, &pk); 9470Sstevel@tonic-gate if (k != NULL) 9480Sstevel@tonic-gate return (k); 9490Sstevel@tonic-gate 9500Sstevel@tonic-gate k = rtmalloc(sizeof (*k), "kern_add"); 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate (void) memset(k, 0, sizeof (*k)); 9530Sstevel@tonic-gate k->k_dst = dst; 9540Sstevel@tonic-gate k->k_mask = mask; 9550Sstevel@tonic-gate k->k_state = KS_NEW; 9560Sstevel@tonic-gate k->k_keep = now.tv_sec; 9570Sstevel@tonic-gate k->k_gate = gate; 9580Sstevel@tonic-gate k->k_ifp = ifp; 9590Sstevel@tonic-gate *pk = k; 9600Sstevel@tonic-gate 9610Sstevel@tonic-gate return (k); 9620Sstevel@tonic-gate } 9630Sstevel@tonic-gate 9640Sstevel@tonic-gate /* delete all khash entries that are wired through the interface ifp */ 9650Sstevel@tonic-gate void 9660Sstevel@tonic-gate kern_flush_ifp(struct interface *ifp) 9670Sstevel@tonic-gate { 9680Sstevel@tonic-gate struct khash *k, *kprev, *knext; 9690Sstevel@tonic-gate int i; 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 9720Sstevel@tonic-gate kprev = NULL; 9730Sstevel@tonic-gate for (k = khash_bins[i]; k != NULL; k = knext) { 9740Sstevel@tonic-gate knext = k->k_next; 9750Sstevel@tonic-gate if (k->k_ifp == ifp) { 9760Sstevel@tonic-gate if (kprev != NULL) 9770Sstevel@tonic-gate kprev->k_next = k->k_next; 9780Sstevel@tonic-gate else 9790Sstevel@tonic-gate khash_bins[i] = k->k_next; 9800Sstevel@tonic-gate free(k); 9810Sstevel@tonic-gate continue; 9820Sstevel@tonic-gate } 9830Sstevel@tonic-gate kprev = k; 9840Sstevel@tonic-gate } 9850Sstevel@tonic-gate } 9860Sstevel@tonic-gate } 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate /* 9890Sstevel@tonic-gate * rewire khash entries that currently go through oldifp to 9900Sstevel@tonic-gate * go through newifp. 9910Sstevel@tonic-gate */ 9920Sstevel@tonic-gate void 9930Sstevel@tonic-gate kern_rewire_ifp(struct interface *oldifp, struct interface *newifp) 9940Sstevel@tonic-gate { 9950Sstevel@tonic-gate struct khash *k; 9960Sstevel@tonic-gate int i; 9970Sstevel@tonic-gate 9980Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 9990Sstevel@tonic-gate for (k = khash_bins[i]; k; k = k->k_next) { 10000Sstevel@tonic-gate if (k->k_ifp == oldifp) { 10010Sstevel@tonic-gate k->k_ifp = newifp; 10020Sstevel@tonic-gate trace_misc("kern_rewire_ifp k 0x%lx " 10030Sstevel@tonic-gate "from %s to %s", k, oldifp->int_name, 10040Sstevel@tonic-gate newifp->int_name); 10050Sstevel@tonic-gate } 10060Sstevel@tonic-gate } 10070Sstevel@tonic-gate } 10080Sstevel@tonic-gate } 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate /* 10120Sstevel@tonic-gate * Check that a static route it is still in the daemon table, and not 10130Sstevel@tonic-gate * deleted by interfaces coming and going. This is also the routine 10140Sstevel@tonic-gate * responsible for adding new static routes to the daemon table. 10150Sstevel@tonic-gate */ 10160Sstevel@tonic-gate static void 10170Sstevel@tonic-gate kern_check_static(struct khash *k, struct interface *ifp) 10180Sstevel@tonic-gate { 10190Sstevel@tonic-gate struct rt_entry *rt; 10200Sstevel@tonic-gate struct rt_spare new; 10210Sstevel@tonic-gate uint16_t rt_state = RS_STATIC; 10220Sstevel@tonic-gate 10230Sstevel@tonic-gate (void) memset(&new, 0, sizeof (new)); 10240Sstevel@tonic-gate new.rts_ifp = ifp; 10250Sstevel@tonic-gate new.rts_gate = k->k_gate; 10260Sstevel@tonic-gate new.rts_router = (ifp != NULL) ? ifp->int_addr : loopaddr; 10270Sstevel@tonic-gate new.rts_metric = k->k_metric; 10280Sstevel@tonic-gate new.rts_time = now.tv_sec; 10290Sstevel@tonic-gate new.rts_origin = RO_STATIC; 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate rt = rtget(k->k_dst, k->k_mask); 10320Sstevel@tonic-gate if ((ifp != NULL && !IS_IFF_ROUTING(ifp->int_if_flags)) || 10330Sstevel@tonic-gate (k->k_state & KS_PRIVATE)) 10340Sstevel@tonic-gate rt_state |= RS_NOPROPAGATE; 10350Sstevel@tonic-gate 10360Sstevel@tonic-gate if (rt != NULL) { 10370Sstevel@tonic-gate if ((rt->rt_state & RS_STATIC) == 0) { 10380Sstevel@tonic-gate /* 10390Sstevel@tonic-gate * We are already tracking this dest/mask 10400Sstevel@tonic-gate * via RIP/RDISC. Ignore the static route, 10410Sstevel@tonic-gate * because we don't currently have a good 10420Sstevel@tonic-gate * way to compare metrics on static routes 10430Sstevel@tonic-gate * with rip metrics, and therefore cannot 10440Sstevel@tonic-gate * mix and match the two. 10450Sstevel@tonic-gate */ 10460Sstevel@tonic-gate return; 10470Sstevel@tonic-gate } 10480Sstevel@tonic-gate rt_state |= rt->rt_state; 10490Sstevel@tonic-gate if (rt->rt_state != rt_state) 10500Sstevel@tonic-gate rtchange(rt, rt_state, &new, 0); 10510Sstevel@tonic-gate } else { 10520Sstevel@tonic-gate rtadd(k->k_dst, k->k_mask, rt_state, &new); 10530Sstevel@tonic-gate } 10540Sstevel@tonic-gate } 10550Sstevel@tonic-gate 10560Sstevel@tonic-gate 10570Sstevel@tonic-gate /* operate on a kernel entry */ 10580Sstevel@tonic-gate static void 10590Sstevel@tonic-gate kern_ioctl(struct khash *k, 10600Sstevel@tonic-gate int action, /* RTM_DELETE, etc */ 10610Sstevel@tonic-gate int flags) 10620Sstevel@tonic-gate { 10630Sstevel@tonic-gate if (((k->k_state & (KS_IF|KS_PASSIVE)) == KS_IF) || 10640Sstevel@tonic-gate (k->k_state & KS_DEPRE_IF)) { 10650Sstevel@tonic-gate /* 10660Sstevel@tonic-gate * Prevent execution of RTM_DELETE, RTM_ADD or 10670Sstevel@tonic-gate * RTM_CHANGE of interface routes 10680Sstevel@tonic-gate */ 10690Sstevel@tonic-gate trace_act("Blocking execution of %s %s --> %s ", 10700Sstevel@tonic-gate rtm_type_name(action), 10710Sstevel@tonic-gate addrname(k->k_dst, k->k_mask, 0), naddr_ntoa(k->k_gate)); 10720Sstevel@tonic-gate return; 10730Sstevel@tonic-gate } 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate switch (action) { 10760Sstevel@tonic-gate case RTM_DELETE: 10770Sstevel@tonic-gate k->k_state &= ~KS_DYNAMIC; 10780Sstevel@tonic-gate if (k->k_state & KS_DELETED) 10790Sstevel@tonic-gate return; 10800Sstevel@tonic-gate k->k_state |= KS_DELETED; 10810Sstevel@tonic-gate break; 10820Sstevel@tonic-gate case RTM_ADD: 10830Sstevel@tonic-gate k->k_state &= ~KS_DELETED; 10840Sstevel@tonic-gate break; 10850Sstevel@tonic-gate case RTM_CHANGE: 10860Sstevel@tonic-gate if (k->k_state & KS_DELETED) { 10870Sstevel@tonic-gate action = RTM_ADD; 10880Sstevel@tonic-gate k->k_state &= ~KS_DELETED; 10890Sstevel@tonic-gate } 10900Sstevel@tonic-gate break; 10910Sstevel@tonic-gate } 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate rtioctl(action, k->k_dst, k->k_gate, k->k_mask, k->k_ifp, 10940Sstevel@tonic-gate k->k_metric, flags); 10950Sstevel@tonic-gate } 10960Sstevel@tonic-gate 10970Sstevel@tonic-gate 10980Sstevel@tonic-gate /* add a route the kernel told us */ 10990Sstevel@tonic-gate static void 11000Sstevel@tonic-gate rtm_add(struct rt_msghdr *rtm, 11010Sstevel@tonic-gate struct rt_addrinfo *info, 11020Sstevel@tonic-gate time_t keep, 11030Sstevel@tonic-gate boolean_t interf_route, 11040Sstevel@tonic-gate struct interface *ifptr) 11050Sstevel@tonic-gate { 11060Sstevel@tonic-gate struct khash *k; 11070Sstevel@tonic-gate struct interface *ifp = ifptr; 11080Sstevel@tonic-gate in_addr_t mask, gate = 0; 11090Sstevel@tonic-gate static struct msg_limit msg_no_ifp; 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate if (rtm->rtm_flags & RTF_HOST) { 11120Sstevel@tonic-gate mask = HOST_MASK; 11130Sstevel@tonic-gate } else if (INFO_MASK(info) != 0) { 11140Sstevel@tonic-gate mask = ntohl(S_ADDR(INFO_MASK(info))); 11150Sstevel@tonic-gate } else { 11160Sstevel@tonic-gate writelog(LOG_WARNING, 11170Sstevel@tonic-gate "ignore %s without mask", rtm_type_name(rtm->rtm_type)); 11180Sstevel@tonic-gate return; 11190Sstevel@tonic-gate } 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate /* 11220Sstevel@tonic-gate * Find the interface toward the gateway. 11230Sstevel@tonic-gate */ 11240Sstevel@tonic-gate if (INFO_GATE(info) != NULL) 11250Sstevel@tonic-gate gate = S_ADDR(INFO_GATE(info)); 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate if (ifp == NULL) { 11280Sstevel@tonic-gate if (INFO_GATE(info) != NULL) 11290Sstevel@tonic-gate ifp = iflookup(gate); 11300Sstevel@tonic-gate if (ifp == NULL) 11310Sstevel@tonic-gate msglim(&msg_no_ifp, gate, 11320Sstevel@tonic-gate "route %s --> %s nexthop is not directly connected", 11330Sstevel@tonic-gate addrname(S_ADDR(INFO_DST(info)), mask, 0), 11340Sstevel@tonic-gate naddr_ntoa(gate)); 11350Sstevel@tonic-gate } 11360Sstevel@tonic-gate 11370Sstevel@tonic-gate k = kern_add(S_ADDR(INFO_DST(info)), mask, gate, ifp); 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate if (k->k_state & KS_NEW) 11400Sstevel@tonic-gate k->k_keep = now.tv_sec+keep; 11410Sstevel@tonic-gate if (INFO_GATE(info) == 0) { 11420Sstevel@tonic-gate trace_act("note %s without gateway", 11430Sstevel@tonic-gate rtm_type_name(rtm->rtm_type)); 11440Sstevel@tonic-gate k->k_metric = HOPCNT_INFINITY; 11450Sstevel@tonic-gate } else if (INFO_GATE(info)->ss_family != AF_INET) { 11460Sstevel@tonic-gate trace_act("note %s with gateway AF=%d", 11470Sstevel@tonic-gate rtm_type_name(rtm->rtm_type), 11480Sstevel@tonic-gate INFO_GATE(info)->ss_family); 11490Sstevel@tonic-gate k->k_metric = HOPCNT_INFINITY; 11500Sstevel@tonic-gate } else { 11510Sstevel@tonic-gate k->k_gate = S_ADDR(INFO_GATE(info)); 11520Sstevel@tonic-gate k->k_metric = rtm->rtm_rmx.rmx_hopcount; 11530Sstevel@tonic-gate if (k->k_metric < 0) 11540Sstevel@tonic-gate k->k_metric = 0; 11550Sstevel@tonic-gate else if (k->k_metric > HOPCNT_INFINITY-1) 11560Sstevel@tonic-gate k->k_metric = HOPCNT_INFINITY-1; 11570Sstevel@tonic-gate } 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate if ((k->k_state & KS_NEW) && interf_route) { 11600Sstevel@tonic-gate if (k->k_gate != 0 && findifaddr(k->k_gate) == NULL) 11610Sstevel@tonic-gate k->k_state |= KS_DEPRE_IF; 11620Sstevel@tonic-gate else 11630Sstevel@tonic-gate k->k_state |= KS_IF; 11640Sstevel@tonic-gate } 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate k->k_state &= ~(KS_NEW | KS_DELETE | KS_ADD | KS_CHANGE | KS_DEL_ADD | 11670Sstevel@tonic-gate KS_STATIC | KS_GATEWAY | KS_DELETED | KS_PRIVATE | KS_CHECK); 11680Sstevel@tonic-gate if (rtm->rtm_flags & RTF_GATEWAY) 11690Sstevel@tonic-gate k->k_state |= KS_GATEWAY; 11700Sstevel@tonic-gate if (rtm->rtm_flags & RTF_STATIC) 11710Sstevel@tonic-gate k->k_state |= KS_STATIC; 11720Sstevel@tonic-gate if (rtm->rtm_flags & RTF_PRIVATE) 11730Sstevel@tonic-gate k->k_state |= KS_PRIVATE; 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate 11760Sstevel@tonic-gate if (rtm->rtm_flags & (RTF_DYNAMIC | RTF_MODIFIED)) { 11770Sstevel@tonic-gate if (INFO_AUTHOR(info) != 0 && 11780Sstevel@tonic-gate INFO_AUTHOR(info)->ss_family == AF_INET) 11790Sstevel@tonic-gate ifp = iflookup(S_ADDR(INFO_AUTHOR(info))); 11800Sstevel@tonic-gate else 11810Sstevel@tonic-gate ifp = NULL; 11820Sstevel@tonic-gate if (should_supply(ifp) && (ifp == NULL || 11830Sstevel@tonic-gate !(ifp->int_state & IS_REDIRECT_OK))) { 11840Sstevel@tonic-gate /* 11850Sstevel@tonic-gate * Routers are not supposed to listen to redirects, 11860Sstevel@tonic-gate * so delete it if it came via an unknown interface 11870Sstevel@tonic-gate * or the interface does not have special permission. 11880Sstevel@tonic-gate */ 11890Sstevel@tonic-gate k->k_state &= ~KS_DYNAMIC; 11900Sstevel@tonic-gate k->k_state |= KS_DELETE; 11910Sstevel@tonic-gate LIM_SEC(need_kern, 0); 11920Sstevel@tonic-gate trace_act("mark for deletion redirected %s --> %s" 11930Sstevel@tonic-gate " via %s", 11940Sstevel@tonic-gate addrname(k->k_dst, k->k_mask, 0), 11950Sstevel@tonic-gate naddr_ntoa(k->k_gate), 11960Sstevel@tonic-gate ifp ? ifp->int_name : "unknown interface"); 11970Sstevel@tonic-gate } else { 11980Sstevel@tonic-gate k->k_state |= KS_DYNAMIC; 11990Sstevel@tonic-gate k->k_redirect_time = now.tv_sec; 12000Sstevel@tonic-gate trace_act("accept redirected %s --> %s via %s", 12010Sstevel@tonic-gate addrname(k->k_dst, k->k_mask, 0), 12020Sstevel@tonic-gate naddr_ntoa(k->k_gate), 12030Sstevel@tonic-gate ifp ? ifp->int_name : "unknown interface"); 12040Sstevel@tonic-gate } 12050Sstevel@tonic-gate return; 12060Sstevel@tonic-gate } 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate /* 12090Sstevel@tonic-gate * If it is not a static route, quit until the next comparison 12100Sstevel@tonic-gate * between the kernel and daemon tables, when it will be deleted. 12110Sstevel@tonic-gate */ 12120Sstevel@tonic-gate if (!(k->k_state & KS_STATIC)) { 12130Sstevel@tonic-gate if (!(k->k_state & (KS_IF|KS_DEPRE_IF|KS_FILE))) 12140Sstevel@tonic-gate k->k_state |= KS_DELETE; 12150Sstevel@tonic-gate LIM_SEC(need_kern, k->k_keep); 12160Sstevel@tonic-gate return; 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate 12190Sstevel@tonic-gate /* 12200Sstevel@tonic-gate * Put static routes with real metrics into the daemon table so 12210Sstevel@tonic-gate * they can be advertised. 12220Sstevel@tonic-gate */ 12230Sstevel@tonic-gate 12240Sstevel@tonic-gate kern_check_static(k, ifp); 12250Sstevel@tonic-gate } 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate 12280Sstevel@tonic-gate /* deal with packet loss */ 12290Sstevel@tonic-gate static void 12300Sstevel@tonic-gate rtm_lose(struct rt_msghdr *rtm, struct rt_addrinfo *info) 12310Sstevel@tonic-gate { 12320Sstevel@tonic-gate if (INFO_GATE(info) == NULL || INFO_GATE(info)->ss_family != AF_INET) { 12330Sstevel@tonic-gate trace_act("ignore %s without gateway", 12340Sstevel@tonic-gate rtm_type_name(rtm->rtm_type)); 12350Sstevel@tonic-gate age(0); 12360Sstevel@tonic-gate return; 12370Sstevel@tonic-gate } 12380Sstevel@tonic-gate 12390Sstevel@tonic-gate if (rdisc_ok) 12400Sstevel@tonic-gate rdisc_age(S_ADDR(INFO_GATE(info))); 12410Sstevel@tonic-gate age(S_ADDR(INFO_GATE(info))); 12420Sstevel@tonic-gate } 12430Sstevel@tonic-gate 12440Sstevel@tonic-gate 12450Sstevel@tonic-gate /* 12460Sstevel@tonic-gate * Make the gateway slot of an info structure point to something 12470Sstevel@tonic-gate * useful. If it is not already useful, but it specifies an interface, 12480Sstevel@tonic-gate * then fill in the sockaddr_in provided and point it there. 12490Sstevel@tonic-gate */ 12500Sstevel@tonic-gate static int 12510Sstevel@tonic-gate get_info_gate(struct sockaddr_storage **ssp, struct sockaddr_in *sin) 12520Sstevel@tonic-gate { 12530Sstevel@tonic-gate struct sockaddr_dl *sdl = (struct sockaddr_dl *)*ssp; 12540Sstevel@tonic-gate struct interface *ifp; 12550Sstevel@tonic-gate 12560Sstevel@tonic-gate if (sdl == NULL) 12570Sstevel@tonic-gate return (0); 12580Sstevel@tonic-gate if ((sdl)->sdl_family == AF_INET) 12590Sstevel@tonic-gate return (1); 12600Sstevel@tonic-gate if ((sdl)->sdl_family != AF_LINK) 12610Sstevel@tonic-gate return (0); 12620Sstevel@tonic-gate 12630Sstevel@tonic-gate ifp = ifwithindex(sdl->sdl_index, _B_TRUE); 12640Sstevel@tonic-gate if (ifp == NULL) 12650Sstevel@tonic-gate return (0); 12660Sstevel@tonic-gate 12670Sstevel@tonic-gate sin->sin_addr.s_addr = ifp->int_addr; 12680Sstevel@tonic-gate sin->sin_family = AF_INET; 12690Sstevel@tonic-gate /* LINTED */ 12700Sstevel@tonic-gate *ssp = (struct sockaddr_storage *)sin; 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate return (1); 12730Sstevel@tonic-gate } 12740Sstevel@tonic-gate 12750Sstevel@tonic-gate 12760Sstevel@tonic-gate /* 12770Sstevel@tonic-gate * Clean the kernel table by copying it to the daemon image. 12780Sstevel@tonic-gate * Eventually the daemon will delete any extra routes. 12790Sstevel@tonic-gate */ 12800Sstevel@tonic-gate void 12810Sstevel@tonic-gate sync_kern(void) 12820Sstevel@tonic-gate { 12830Sstevel@tonic-gate int i; 12840Sstevel@tonic-gate struct khash *k; 12850Sstevel@tonic-gate struct { 12860Sstevel@tonic-gate struct T_optmgmt_req req; 12870Sstevel@tonic-gate struct opthdr hdr; 12880Sstevel@tonic-gate } req; 12890Sstevel@tonic-gate union { 12900Sstevel@tonic-gate struct T_optmgmt_ack ack; 12910Sstevel@tonic-gate unsigned char space[64]; 12920Sstevel@tonic-gate } ack; 12930Sstevel@tonic-gate struct opthdr *rh; 12940Sstevel@tonic-gate struct strbuf cbuf, dbuf; 12950Sstevel@tonic-gate int ipfd, nroutes, flags, r; 12960Sstevel@tonic-gate mib2_ipRouteEntry_t routes[8]; 12970Sstevel@tonic-gate mib2_ipRouteEntry_t *rp; 12980Sstevel@tonic-gate struct rt_msghdr rtm; 12990Sstevel@tonic-gate struct rt_addrinfo info; 13000Sstevel@tonic-gate struct sockaddr_in sin_dst; 13010Sstevel@tonic-gate struct sockaddr_in sin_gate; 13020Sstevel@tonic-gate struct sockaddr_in sin_mask; 13030Sstevel@tonic-gate struct sockaddr_in sin_author; 13040Sstevel@tonic-gate struct interface *ifp; 13050Sstevel@tonic-gate char ifname[LIFNAMSIZ + 1]; 13060Sstevel@tonic-gate 13070Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 13080Sstevel@tonic-gate for (k = khash_bins[i]; k != NULL; k = k->k_next) { 13090Sstevel@tonic-gate if (!(k->k_state & (KS_IF|KS_DEPRE_IF))) 13100Sstevel@tonic-gate k->k_state |= KS_CHECK; 13110Sstevel@tonic-gate } 13120Sstevel@tonic-gate } 13130Sstevel@tonic-gate 13140Sstevel@tonic-gate ipfd = open(IP_DEV_NAME, O_RDWR); 13150Sstevel@tonic-gate if (ipfd == -1) { 13160Sstevel@tonic-gate msglog("open " IP_DEV_NAME ": %s", rip_strerror(errno)); 13170Sstevel@tonic-gate goto hash_clean; 13180Sstevel@tonic-gate } 13190Sstevel@tonic-gate 13200Sstevel@tonic-gate req.req.PRIM_type = T_OPTMGMT_REQ; 13210Sstevel@tonic-gate req.req.OPT_offset = (caddr_t)&req.hdr - (caddr_t)&req; 13220Sstevel@tonic-gate req.req.OPT_length = sizeof (req.hdr); 13230Sstevel@tonic-gate req.req.MGMT_flags = T_CURRENT; 13240Sstevel@tonic-gate 13250Sstevel@tonic-gate req.hdr.level = MIB2_IP; 13260Sstevel@tonic-gate req.hdr.name = 0; 13270Sstevel@tonic-gate req.hdr.len = 0; 13280Sstevel@tonic-gate 13290Sstevel@tonic-gate cbuf.buf = (caddr_t)&req; 13300Sstevel@tonic-gate cbuf.len = sizeof (req); 13310Sstevel@tonic-gate 13320Sstevel@tonic-gate if (putmsg(ipfd, &cbuf, NULL, 0) == -1) { 13330Sstevel@tonic-gate msglog("T_OPTMGMT_REQ putmsg: %s", rip_strerror(errno)); 13340Sstevel@tonic-gate goto hash_clean; 13350Sstevel@tonic-gate } 13360Sstevel@tonic-gate 13370Sstevel@tonic-gate for (;;) { 13380Sstevel@tonic-gate cbuf.buf = (caddr_t)&ack; 13390Sstevel@tonic-gate cbuf.maxlen = sizeof (ack); 13400Sstevel@tonic-gate dbuf.buf = (caddr_t)routes; 13410Sstevel@tonic-gate dbuf.maxlen = sizeof (routes); 13420Sstevel@tonic-gate flags = 0; 13430Sstevel@tonic-gate r = getmsg(ipfd, &cbuf, &dbuf, &flags); 13440Sstevel@tonic-gate if (r == -1) { 13450Sstevel@tonic-gate msglog("T_OPTMGMT_REQ getmsg: %s", rip_strerror(errno)); 13460Sstevel@tonic-gate goto hash_clean; 13470Sstevel@tonic-gate } 13480Sstevel@tonic-gate 13490Sstevel@tonic-gate if (cbuf.len < sizeof (struct T_optmgmt_ack) || 13500Sstevel@tonic-gate ack.ack.PRIM_type != T_OPTMGMT_ACK || 13510Sstevel@tonic-gate ack.ack.MGMT_flags != T_SUCCESS || 13520Sstevel@tonic-gate ack.ack.OPT_length < sizeof (struct opthdr)) { 13530Sstevel@tonic-gate msglog("bad T_OPTMGMT response; len=%d prim=%d " 13540Sstevel@tonic-gate "flags=%d optlen=%d", cbuf.len, ack.ack.PRIM_type, 13550Sstevel@tonic-gate ack.ack.MGMT_flags, ack.ack.OPT_length); 13560Sstevel@tonic-gate goto hash_clean; 13570Sstevel@tonic-gate } 13580Sstevel@tonic-gate /* LINTED */ 13590Sstevel@tonic-gate rh = (struct opthdr *)((caddr_t)&ack + ack.ack.OPT_offset); 13600Sstevel@tonic-gate if (rh->level == 0 && rh->name == 0) { 13610Sstevel@tonic-gate break; 13620Sstevel@tonic-gate } 13630Sstevel@tonic-gate if (rh->level != MIB2_IP || rh->name != MIB2_IP_21) { 13640Sstevel@tonic-gate while (r == MOREDATA) { 13650Sstevel@tonic-gate r = getmsg(ipfd, NULL, &dbuf, &flags); 13660Sstevel@tonic-gate } 13670Sstevel@tonic-gate continue; 13680Sstevel@tonic-gate } 13690Sstevel@tonic-gate break; 13700Sstevel@tonic-gate } 13710Sstevel@tonic-gate 13720Sstevel@tonic-gate (void) memset(&rtm, 0, sizeof (rtm)); 13730Sstevel@tonic-gate (void) memset(&info, 0, sizeof (info)); 13740Sstevel@tonic-gate (void) memset(&sin_dst, 0, sizeof (sin_dst)); 13750Sstevel@tonic-gate (void) memset(&sin_gate, 0, sizeof (sin_gate)); 13760Sstevel@tonic-gate (void) memset(&sin_mask, 0, sizeof (sin_mask)); 13770Sstevel@tonic-gate (void) memset(&sin_author, 0, sizeof (sin_author)); 13780Sstevel@tonic-gate sin_dst.sin_family = AF_INET; 13790Sstevel@tonic-gate /* LINTED */ 13800Sstevel@tonic-gate info.rti_info[RTAX_DST] = (struct sockaddr_storage *)&sin_dst; 13810Sstevel@tonic-gate sin_gate.sin_family = AF_INET; 13820Sstevel@tonic-gate /* LINTED */ 13830Sstevel@tonic-gate info.rti_info[RTAX_GATEWAY] = (struct sockaddr_storage *)&sin_gate; 13840Sstevel@tonic-gate sin_mask.sin_family = AF_INET; 13850Sstevel@tonic-gate /* LINTED */ 13860Sstevel@tonic-gate info.rti_info[RTAX_NETMASK] = (struct sockaddr_storage *)&sin_mask; 13870Sstevel@tonic-gate sin_dst.sin_family = AF_INET; 13880Sstevel@tonic-gate /* LINTED */ 13890Sstevel@tonic-gate info.rti_info[RTAX_AUTHOR] = (struct sockaddr_storage *)&sin_author; 13900Sstevel@tonic-gate 13910Sstevel@tonic-gate for (;;) { 13920Sstevel@tonic-gate nroutes = dbuf.len / sizeof (mib2_ipRouteEntry_t); 13930Sstevel@tonic-gate for (rp = routes; nroutes > 0; ++rp, nroutes--) { 13940Sstevel@tonic-gate 13950Sstevel@tonic-gate /* 13960Sstevel@tonic-gate * Ignore IRE cache, broadcast, and local address 13970Sstevel@tonic-gate * entries; they're not subject to routing socket 13980Sstevel@tonic-gate * control. 13990Sstevel@tonic-gate */ 14000Sstevel@tonic-gate if (rp->ipRouteInfo.re_ire_type & 14010Sstevel@tonic-gate (IRE_BROADCAST | IRE_CACHE | IRE_LOCAL)) 14020Sstevel@tonic-gate continue; 14030Sstevel@tonic-gate 14040Sstevel@tonic-gate /* ignore multicast addresses */ 14050Sstevel@tonic-gate if (IN_MULTICAST(ntohl(rp->ipRouteDest))) 14060Sstevel@tonic-gate continue; 14070Sstevel@tonic-gate 14080Sstevel@tonic-gate 14090Sstevel@tonic-gate #ifdef DEBUG_KERNEL_ROUTE_READ 14100Sstevel@tonic-gate (void) fprintf(stderr, "route type %d, ire type %08X, " 14110Sstevel@tonic-gate "flags %08X: %s", rp->ipRouteType, 14120Sstevel@tonic-gate rp->ipRouteInfo.re_ire_type, 14130Sstevel@tonic-gate rp->ipRouteInfo.re_flags, 14140Sstevel@tonic-gate naddr_ntoa(rp->ipRouteDest)); 14150Sstevel@tonic-gate (void) fprintf(stderr, " %s", 14160Sstevel@tonic-gate naddr_ntoa(rp->ipRouteMask)); 14170Sstevel@tonic-gate (void) fprintf(stderr, " %s\n", 14180Sstevel@tonic-gate naddr_ntoa(rp->ipRouteNextHop)); 14190Sstevel@tonic-gate #endif 14200Sstevel@tonic-gate 14210Sstevel@tonic-gate /* Fake up the needed entries */ 14220Sstevel@tonic-gate rtm.rtm_flags = rp->ipRouteInfo.re_flags; 14230Sstevel@tonic-gate rtm.rtm_type = RTM_GET; 14240Sstevel@tonic-gate rtm.rtm_rmx.rmx_hopcount = rp->ipRouteMetric1; 14250Sstevel@tonic-gate 14260Sstevel@tonic-gate (void) memset(ifname, 0, sizeof (ifname)); 14270Sstevel@tonic-gate if (rp->ipRouteIfIndex.o_length < 14280Sstevel@tonic-gate sizeof (rp->ipRouteIfIndex.o_bytes)) 14290Sstevel@tonic-gate rp->ipRouteIfIndex.o_bytes[ 14300Sstevel@tonic-gate rp->ipRouteIfIndex.o_length] = '\0'; 14310Sstevel@tonic-gate (void) strncpy(ifname, 14320Sstevel@tonic-gate rp->ipRouteIfIndex.o_bytes, 14330Sstevel@tonic-gate sizeof (ifname)); 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate /* 14360Sstevel@tonic-gate * First try to match up on gwkludge entries 14370Sstevel@tonic-gate * before trying to match ifp by name. 14380Sstevel@tonic-gate */ 14390Sstevel@tonic-gate if ((ifp = gwkludge_iflookup(rp->ipRouteDest, 1440551Sbw rp->ipRouteNextHop, 1441551Sbw ntohl(rp->ipRouteMask))) == NULL) 14420Sstevel@tonic-gate ifp = ifwithname(ifname); 14430Sstevel@tonic-gate 14440Sstevel@tonic-gate info.rti_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; 14450Sstevel@tonic-gate if (rp->ipRouteInfo.re_ire_type & IRE_HOST_REDIRECT) 14460Sstevel@tonic-gate info.rti_addrs |= RTA_AUTHOR; 14470Sstevel@tonic-gate sin_dst.sin_addr.s_addr = rp->ipRouteDest; 14480Sstevel@tonic-gate sin_gate.sin_addr.s_addr = rp->ipRouteNextHop; 14490Sstevel@tonic-gate sin_mask.sin_addr.s_addr = rp->ipRouteMask; 14500Sstevel@tonic-gate sin_author.sin_addr.s_addr = 14510Sstevel@tonic-gate rp->ipRouteInfo.re_src_addr; 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate /* 14540Sstevel@tonic-gate * Note static routes and interface routes, and also 14550Sstevel@tonic-gate * preload the image of the kernel table so that 14560Sstevel@tonic-gate * we can later clean it, as well as avoid making 14570Sstevel@tonic-gate * unneeded changes. Keep the old kernel routes for a 14580Sstevel@tonic-gate * few seconds to allow a RIP or router-discovery 14590Sstevel@tonic-gate * response to be heard. 14600Sstevel@tonic-gate */ 14610Sstevel@tonic-gate rtm_add(&rtm, &info, MAX_WAITTIME, 14620Sstevel@tonic-gate ((rp->ipRouteInfo.re_ire_type & 14630Sstevel@tonic-gate (IRE_INTERFACE|IRE_LOOPBACK)) != 0), ifp); 14640Sstevel@tonic-gate } 14650Sstevel@tonic-gate if (r == 0) { 14660Sstevel@tonic-gate break; 14670Sstevel@tonic-gate } 14680Sstevel@tonic-gate r = getmsg(ipfd, NULL, &dbuf, &flags); 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate hash_clean: 14720Sstevel@tonic-gate if (ipfd != -1) 14730Sstevel@tonic-gate (void) close(ipfd); 14740Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 14750Sstevel@tonic-gate for (k = khash_bins[i]; k != NULL; k = k->k_next) { 14760Sstevel@tonic-gate 14770Sstevel@tonic-gate /* 14780Sstevel@tonic-gate * KS_DELETED routes have been removed from the 14790Sstevel@tonic-gate * kernel, but we keep them around for reasons 14800Sstevel@tonic-gate * stated in del_static(), so we skip the check 14810Sstevel@tonic-gate * for KS_DELETED routes here. 14820Sstevel@tonic-gate */ 14830Sstevel@tonic-gate if ((k->k_state & (KS_CHECK|KS_DELETED)) == KS_CHECK) { 14840Sstevel@tonic-gate 14850Sstevel@tonic-gate if (!(k->k_state & KS_DYNAMIC)) 14860Sstevel@tonic-gate writelog(LOG_WARNING, 14870Sstevel@tonic-gate "%s --> %s disappeared from kernel", 14880Sstevel@tonic-gate addrname(k->k_dst, k->k_mask, 0), 14890Sstevel@tonic-gate naddr_ntoa(k->k_gate)); 14900Sstevel@tonic-gate del_static(k->k_dst, k->k_mask, k->k_gate, 14910Sstevel@tonic-gate k->k_ifp, 1); 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate } 14940Sstevel@tonic-gate } 14950Sstevel@tonic-gate } 14960Sstevel@tonic-gate } 14970Sstevel@tonic-gate 14980Sstevel@tonic-gate 14990Sstevel@tonic-gate /* Listen to announcements from the kernel */ 15000Sstevel@tonic-gate void 15010Sstevel@tonic-gate read_rt(void) 15020Sstevel@tonic-gate { 15030Sstevel@tonic-gate long cc; 15040Sstevel@tonic-gate struct interface *ifp; 15050Sstevel@tonic-gate struct sockaddr_in gate_sin; 15060Sstevel@tonic-gate in_addr_t mask, gate; 15070Sstevel@tonic-gate union { 15080Sstevel@tonic-gate struct { 15090Sstevel@tonic-gate struct rt_msghdr rtm; 15100Sstevel@tonic-gate struct sockaddr_storage addrs[RTA_NUMBITS]; 15110Sstevel@tonic-gate } r; 15120Sstevel@tonic-gate struct if_msghdr ifm; 15130Sstevel@tonic-gate } m; 15140Sstevel@tonic-gate char str[100], *strp; 15150Sstevel@tonic-gate struct rt_addrinfo info; 15160Sstevel@tonic-gate 15170Sstevel@tonic-gate 15180Sstevel@tonic-gate for (;;) { 15190Sstevel@tonic-gate cc = read(rt_sock, &m, sizeof (m)); 15200Sstevel@tonic-gate if (cc <= 0) { 15210Sstevel@tonic-gate if (cc < 0 && errno != EWOULDBLOCK) 15220Sstevel@tonic-gate LOGERR("read(rt_sock)"); 15230Sstevel@tonic-gate return; 15240Sstevel@tonic-gate } 15250Sstevel@tonic-gate 15260Sstevel@tonic-gate if (TRACERTS) 15270Sstevel@tonic-gate dump_rt_msg("read", &m.r.rtm, cc); 15280Sstevel@tonic-gate 15290Sstevel@tonic-gate if (cc < m.r.rtm.rtm_msglen) { 15300Sstevel@tonic-gate msglog("routing message truncated (%d < %d)", 15310Sstevel@tonic-gate cc, m.r.rtm.rtm_msglen); 15320Sstevel@tonic-gate } 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate if (m.r.rtm.rtm_version != RTM_VERSION) { 15350Sstevel@tonic-gate msglog("bogus routing message version %d", 15360Sstevel@tonic-gate m.r.rtm.rtm_version); 15370Sstevel@tonic-gate continue; 15380Sstevel@tonic-gate } 15390Sstevel@tonic-gate 15400Sstevel@tonic-gate ifp = NULL; 15410Sstevel@tonic-gate 15420Sstevel@tonic-gate if (m.r.rtm.rtm_type == RTM_IFINFO || 15430Sstevel@tonic-gate m.r.rtm.rtm_type == RTM_NEWADDR || 15440Sstevel@tonic-gate m.r.rtm.rtm_type == RTM_DELADDR) { 15450Sstevel@tonic-gate strp = if_bit_string(m.ifm.ifm_flags, _B_TRUE); 15460Sstevel@tonic-gate if (strp == NULL) { 15470Sstevel@tonic-gate strp = str; 15480Sstevel@tonic-gate (void) sprintf(str, "%#x", m.ifm.ifm_flags); 15490Sstevel@tonic-gate } 15500Sstevel@tonic-gate ifp = ifwithindex(m.ifm.ifm_index, 15510Sstevel@tonic-gate m.r.rtm.rtm_type != RTM_DELADDR); 15520Sstevel@tonic-gate if (ifp == NULL) { 15530Sstevel@tonic-gate char ifname[LIFNAMSIZ], *ifnamep; 15540Sstevel@tonic-gate 15550Sstevel@tonic-gate ifnamep = if_indextoname(m.ifm.ifm_index, 15560Sstevel@tonic-gate ifname); 15570Sstevel@tonic-gate if (ifnamep == NULL) { 15580Sstevel@tonic-gate trace_act("note %s with flags %s" 15590Sstevel@tonic-gate " for unknown interface index #%d", 15600Sstevel@tonic-gate rtm_type_name(m.r.rtm.rtm_type), 15610Sstevel@tonic-gate strp, m.ifm.ifm_index); 15620Sstevel@tonic-gate } else { 15630Sstevel@tonic-gate trace_act("note %s with flags %s" 15640Sstevel@tonic-gate " for unknown interface %s", 15650Sstevel@tonic-gate rtm_type_name(m.r.rtm.rtm_type), 15660Sstevel@tonic-gate strp, ifnamep); 15670Sstevel@tonic-gate } 15680Sstevel@tonic-gate } else { 15690Sstevel@tonic-gate trace_act("note %s with flags %s for %s", 15700Sstevel@tonic-gate rtm_type_name(m.r.rtm.rtm_type), 15710Sstevel@tonic-gate strp, ifp->int_name); 15720Sstevel@tonic-gate } 15730Sstevel@tonic-gate if (strp != str) 15740Sstevel@tonic-gate free(strp); 15750Sstevel@tonic-gate 15760Sstevel@tonic-gate /* 15770Sstevel@tonic-gate * After being informed of a change to an interface, 15780Sstevel@tonic-gate * check them all now if the check would otherwise 15790Sstevel@tonic-gate * be a long time from now, if the interface is 15800Sstevel@tonic-gate * not known, or if the interface has been turned 15810Sstevel@tonic-gate * off or on. 15820Sstevel@tonic-gate */ 15830Sstevel@tonic-gate if (ifscan_timer.tv_sec-now.tv_sec >= 15840Sstevel@tonic-gate CHECK_BAD_INTERVAL || ifp == NULL || 15850Sstevel@tonic-gate ((ifp->int_if_flags ^ m.ifm.ifm_flags) & 15860Sstevel@tonic-gate IFF_UP) != 0) 15870Sstevel@tonic-gate ifscan_timer.tv_sec = now.tv_sec; 15880Sstevel@tonic-gate continue; 15890Sstevel@tonic-gate } else { 15900Sstevel@tonic-gate if (m.r.rtm.rtm_index != 0) 15910Sstevel@tonic-gate ifp = ifwithindex(m.r.rtm.rtm_index, 1); 15920Sstevel@tonic-gate } 15930Sstevel@tonic-gate 15940Sstevel@tonic-gate (void) strlcpy(str, rtm_type_name(m.r.rtm.rtm_type), 15950Sstevel@tonic-gate sizeof (str)); 15960Sstevel@tonic-gate strp = &str[strlen(str)]; 15970Sstevel@tonic-gate if (m.r.rtm.rtm_type <= RTM_CHANGE) 15980Sstevel@tonic-gate strp += snprintf(strp, sizeof (str) - (strp - str), 15990Sstevel@tonic-gate " from pid %d", (int)m.r.rtm.rtm_pid); 16000Sstevel@tonic-gate 16010Sstevel@tonic-gate /* LINTED */ 16020Sstevel@tonic-gate (void) rt_xaddrs(&info, (struct sockaddr_storage *)(&m.r.rtm + 16030Sstevel@tonic-gate 1), (char *)&m + cc, m.r.rtm.rtm_addrs); 16040Sstevel@tonic-gate 16050Sstevel@tonic-gate if (INFO_DST(&info) == 0) { 16060Sstevel@tonic-gate trace_act("ignore %s without dst", str); 16070Sstevel@tonic-gate continue; 16080Sstevel@tonic-gate } 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate if (INFO_DST(&info)->ss_family != AF_INET) { 16110Sstevel@tonic-gate trace_act("ignore %s for AF %d", str, 16120Sstevel@tonic-gate INFO_DST(&info)->ss_family); 16130Sstevel@tonic-gate continue; 16140Sstevel@tonic-gate } 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate mask = ((INFO_MASK(&info) != 0) ? 16170Sstevel@tonic-gate ntohl(S_ADDR(INFO_MASK(&info))) : 16180Sstevel@tonic-gate (m.r.rtm.rtm_flags & RTF_HOST) ? 16190Sstevel@tonic-gate HOST_MASK : std_mask(S_ADDR(INFO_DST(&info)))); 16200Sstevel@tonic-gate 16210Sstevel@tonic-gate strp += snprintf(strp, sizeof (str) - (strp - str), ": %s", 16220Sstevel@tonic-gate addrname(S_ADDR(INFO_DST(&info)), mask, 0)); 16230Sstevel@tonic-gate 16240Sstevel@tonic-gate if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info))))) { 16250Sstevel@tonic-gate trace_act("ignore multicast %s", str); 16260Sstevel@tonic-gate continue; 16270Sstevel@tonic-gate } 16280Sstevel@tonic-gate 16290Sstevel@tonic-gate if (m.r.rtm.rtm_flags & RTF_LLINFO) { 16300Sstevel@tonic-gate trace_act("ignore ARP %s", str); 16310Sstevel@tonic-gate continue; 16320Sstevel@tonic-gate } 16330Sstevel@tonic-gate 16340Sstevel@tonic-gate if (get_info_gate(&INFO_GATE(&info), &gate_sin)) { 16350Sstevel@tonic-gate gate = S_ADDR(INFO_GATE(&info)); 16360Sstevel@tonic-gate strp += snprintf(strp, sizeof (str) - (strp - str), 16370Sstevel@tonic-gate " --> %s", naddr_ntoa(gate)); 16380Sstevel@tonic-gate } else { 16390Sstevel@tonic-gate gate = 0; 16400Sstevel@tonic-gate } 16410Sstevel@tonic-gate 16420Sstevel@tonic-gate if (INFO_AUTHOR(&info) != 0) 16430Sstevel@tonic-gate strp += snprintf(strp, sizeof (str) - (strp - str), 16440Sstevel@tonic-gate " by authority of %s", 16450Sstevel@tonic-gate saddr_ntoa(INFO_AUTHOR(&info))); 16460Sstevel@tonic-gate 16470Sstevel@tonic-gate switch (m.r.rtm.rtm_type) { 16480Sstevel@tonic-gate case RTM_ADD: 16490Sstevel@tonic-gate case RTM_CHANGE: 16500Sstevel@tonic-gate case RTM_REDIRECT: 16510Sstevel@tonic-gate if (m.r.rtm.rtm_errno != 0) { 16520Sstevel@tonic-gate trace_act("ignore %s with \"%s\" error", 16530Sstevel@tonic-gate str, rip_strerror(m.r.rtm.rtm_errno)); 16540Sstevel@tonic-gate } else { 16550Sstevel@tonic-gate trace_act("%s", str); 16560Sstevel@tonic-gate rtm_add(&m.r.rtm, &info, 0, 16570Sstevel@tonic-gate !(m.r.rtm.rtm_flags & RTF_GATEWAY) && 16580Sstevel@tonic-gate m.r.rtm.rtm_type != RTM_REDIRECT, ifp); 16590Sstevel@tonic-gate 16600Sstevel@tonic-gate } 16610Sstevel@tonic-gate break; 16620Sstevel@tonic-gate 16630Sstevel@tonic-gate case RTM_DELETE: 16640Sstevel@tonic-gate if (m.r.rtm.rtm_errno != 0 && 16650Sstevel@tonic-gate m.r.rtm.rtm_errno != ESRCH) { 16660Sstevel@tonic-gate trace_act("ignore %s with \"%s\" error", 16670Sstevel@tonic-gate str, rip_strerror(m.r.rtm.rtm_errno)); 16680Sstevel@tonic-gate } else { 16690Sstevel@tonic-gate trace_act("%s", str); 16700Sstevel@tonic-gate del_static(S_ADDR(INFO_DST(&info)), mask, 16710Sstevel@tonic-gate gate, ifp, 1); 16720Sstevel@tonic-gate } 16730Sstevel@tonic-gate break; 16740Sstevel@tonic-gate 16750Sstevel@tonic-gate case RTM_LOSING: 16760Sstevel@tonic-gate trace_act("%s", str); 16770Sstevel@tonic-gate rtm_lose(&m.r.rtm, &info); 16780Sstevel@tonic-gate break; 16790Sstevel@tonic-gate 16800Sstevel@tonic-gate default: 16810Sstevel@tonic-gate trace_act("ignore %s", str); 16820Sstevel@tonic-gate break; 16830Sstevel@tonic-gate } 16840Sstevel@tonic-gate } 16850Sstevel@tonic-gate } 16860Sstevel@tonic-gate 16870Sstevel@tonic-gate 16880Sstevel@tonic-gate /* 16890Sstevel@tonic-gate * Disassemble a routing message. The result is an array of pointers 16900Sstevel@tonic-gate * to sockaddr_storage structures stored in the info argument. 16910Sstevel@tonic-gate * 16920Sstevel@tonic-gate * ss is a pointer to the beginning of the data following the 16930Sstevel@tonic-gate * rt_msghdr contained in the routing socket message, which consists 16940Sstevel@tonic-gate * of a string of concatenated sockaddr structure of different types. 1695*1676Sjpk * 1696*1676Sjpk * Extended attributes can be appended at the end of the list. 16970Sstevel@tonic-gate */ 16980Sstevel@tonic-gate static int 16990Sstevel@tonic-gate rt_xaddrs(struct rt_addrinfo *info, 17000Sstevel@tonic-gate struct sockaddr_storage *ss, 17010Sstevel@tonic-gate char *lim, 17020Sstevel@tonic-gate int addrs) 17030Sstevel@tonic-gate { 17040Sstevel@tonic-gate int retv = 0; 17050Sstevel@tonic-gate int i; 17060Sstevel@tonic-gate int abit; 17070Sstevel@tonic-gate int complaints; 17080Sstevel@tonic-gate static int prev_complaints; 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate #define XBAD_AF 0x1 17110Sstevel@tonic-gate #define XBAD_SHORT 0x2 17120Sstevel@tonic-gate #define XBAD_LONG 0x4 17130Sstevel@tonic-gate 17140Sstevel@tonic-gate (void) memset(info, 0, sizeof (*info)); 17150Sstevel@tonic-gate info->rti_addrs = addrs; 17160Sstevel@tonic-gate complaints = 0; 17170Sstevel@tonic-gate for (i = 0, abit = 1; i < RTAX_MAX && (char *)ss < lim; 17180Sstevel@tonic-gate i++, abit <<= 1) { 17190Sstevel@tonic-gate if ((addrs & abit) == 0) 17200Sstevel@tonic-gate continue; 17210Sstevel@tonic-gate info->rti_info[i] = ss; 17220Sstevel@tonic-gate /* Horrible interface here */ 17230Sstevel@tonic-gate switch (ss->ss_family) { 17240Sstevel@tonic-gate case AF_UNIX: 17250Sstevel@tonic-gate /* LINTED */ 17260Sstevel@tonic-gate ss = (struct sockaddr_storage *)( 17270Sstevel@tonic-gate (struct sockaddr_un *)ss + 1); 17280Sstevel@tonic-gate break; 17290Sstevel@tonic-gate case AF_INET: 17300Sstevel@tonic-gate /* LINTED */ 17310Sstevel@tonic-gate ss = (struct sockaddr_storage *)( 17320Sstevel@tonic-gate (struct sockaddr_in *)ss + 1); 17330Sstevel@tonic-gate break; 17340Sstevel@tonic-gate case AF_LINK: 17350Sstevel@tonic-gate /* LINTED */ 17360Sstevel@tonic-gate ss = (struct sockaddr_storage *)( 17370Sstevel@tonic-gate (struct sockaddr_dl *)ss + 1); 17380Sstevel@tonic-gate break; 17390Sstevel@tonic-gate case AF_INET6: 17400Sstevel@tonic-gate /* LINTED */ 17410Sstevel@tonic-gate ss = (struct sockaddr_storage *)( 17420Sstevel@tonic-gate (struct sockaddr_in6 *)ss + 1); 17430Sstevel@tonic-gate break; 17440Sstevel@tonic-gate default: 17450Sstevel@tonic-gate if (!(prev_complaints & XBAD_AF)) 17460Sstevel@tonic-gate writelog(LOG_WARNING, 17470Sstevel@tonic-gate "unknown address family %d " 17480Sstevel@tonic-gate "encountered", ss->ss_family); 17490Sstevel@tonic-gate if (complaints & XBAD_AF) 17500Sstevel@tonic-gate goto xaddr_done; 17510Sstevel@tonic-gate /* LINTED */ 17520Sstevel@tonic-gate ss = (struct sockaddr_storage *)( 17530Sstevel@tonic-gate (struct sockaddr *)ss + 1); 17540Sstevel@tonic-gate complaints |= XBAD_AF; 17550Sstevel@tonic-gate info->rti_addrs &= abit - 1; 17560Sstevel@tonic-gate addrs = info->rti_addrs; 17570Sstevel@tonic-gate retv = -1; 17580Sstevel@tonic-gate break; 17590Sstevel@tonic-gate } 17600Sstevel@tonic-gate if ((char *)ss > lim) { 17610Sstevel@tonic-gate if (!(prev_complaints & XBAD_SHORT)) 17620Sstevel@tonic-gate msglog("sockaddr %d too short by %d " 17630Sstevel@tonic-gate "bytes", i + 1, (char *)ss - lim); 17640Sstevel@tonic-gate complaints |= XBAD_SHORT; 17650Sstevel@tonic-gate info->rti_info[i] = NULL; 17660Sstevel@tonic-gate info->rti_addrs &= abit - 1; 17670Sstevel@tonic-gate retv = -1; 17680Sstevel@tonic-gate goto xaddr_done; 17690Sstevel@tonic-gate } 17700Sstevel@tonic-gate } 1771*1676Sjpk 1772*1676Sjpk while (((char *)ss + sizeof (rtm_ext_t)) <= lim) { 1773*1676Sjpk rtm_ext_t *tp; 1774*1676Sjpk char *nxt; 1775*1676Sjpk 1776*1676Sjpk /* LINTED: alignment */ 1777*1676Sjpk tp = (rtm_ext_t *)ss; 1778*1676Sjpk nxt = (char *)(tp + 1) + tp->rtmex_len; 1779*1676Sjpk 1780*1676Sjpk if (!IS_P2ALIGNED(tp->rtmex_len, sizeof (uint32_t)) || 1781*1676Sjpk nxt > lim) { 1782*1676Sjpk break; 1783*1676Sjpk } 1784*1676Sjpk 1785*1676Sjpk /* LINTED: alignment */ 1786*1676Sjpk ss = (struct sockaddr_storage *)nxt; 1787*1676Sjpk } 1788*1676Sjpk 17890Sstevel@tonic-gate if ((char *)ss != lim) { 1790*1676Sjpk if ((char *)ss > lim) { 1791*1676Sjpk if (!(prev_complaints & XBAD_SHORT)) 1792*1676Sjpk msglog("routing message too short by %d bytes", 1793*1676Sjpk (char *)ss - lim); 1794*1676Sjpk complaints |= XBAD_SHORT; 1795*1676Sjpk } else if (!(prev_complaints & XBAD_LONG)) { 17960Sstevel@tonic-gate msglog("%d bytes of routing message left over", 17970Sstevel@tonic-gate lim - (char *)ss); 1798*1676Sjpk complaints |= XBAD_LONG; 1799*1676Sjpk } 18000Sstevel@tonic-gate retv = -1; 18010Sstevel@tonic-gate } 18020Sstevel@tonic-gate xaddr_done: 18030Sstevel@tonic-gate prev_complaints = complaints; 18040Sstevel@tonic-gate return (retv); 18050Sstevel@tonic-gate } 18060Sstevel@tonic-gate 18070Sstevel@tonic-gate 18080Sstevel@tonic-gate /* after aggregating, note routes that belong in the kernel */ 18090Sstevel@tonic-gate static void 18100Sstevel@tonic-gate kern_out(struct ag_info *ag) 18110Sstevel@tonic-gate { 18120Sstevel@tonic-gate struct khash *k; 18130Sstevel@tonic-gate 18140Sstevel@tonic-gate /* 18150Sstevel@tonic-gate * Do not install bad routes if they are not already present. 18160Sstevel@tonic-gate * This includes routes that had RS_NET_SYN for interfaces that 18170Sstevel@tonic-gate * recently died. 18180Sstevel@tonic-gate */ 18190Sstevel@tonic-gate if (ag->ag_metric == HOPCNT_INFINITY) { 18200Sstevel@tonic-gate k = kern_find(htonl(ag->ag_dst_h), ag->ag_mask, 18210Sstevel@tonic-gate ag->ag_nhop, ag->ag_ifp, NULL); 18220Sstevel@tonic-gate if (k == NULL) 18230Sstevel@tonic-gate return; 18240Sstevel@tonic-gate } else { 18250Sstevel@tonic-gate k = kern_add(htonl(ag->ag_dst_h), ag->ag_mask, ag->ag_nhop, 18260Sstevel@tonic-gate ag->ag_ifp); 18270Sstevel@tonic-gate } 18280Sstevel@tonic-gate 18290Sstevel@tonic-gate if (k->k_state & KS_NEW) { 18300Sstevel@tonic-gate /* will need to add new entry to the kernel table */ 18310Sstevel@tonic-gate k->k_state = KS_ADD; 18320Sstevel@tonic-gate if (ag->ag_state & AGS_GATEWAY) 18330Sstevel@tonic-gate k->k_state |= KS_GATEWAY; 18340Sstevel@tonic-gate if (ag->ag_state & AGS_IF) 18350Sstevel@tonic-gate k->k_state |= KS_IF; 18360Sstevel@tonic-gate if (ag->ag_state & AGS_PASSIVE) 18370Sstevel@tonic-gate k->k_state |= KS_PASSIVE; 18380Sstevel@tonic-gate if (ag->ag_state & AGS_FILE) 18390Sstevel@tonic-gate k->k_state |= KS_FILE; 18400Sstevel@tonic-gate k->k_gate = ag->ag_nhop; 18410Sstevel@tonic-gate k->k_ifp = ag->ag_ifp; 18420Sstevel@tonic-gate k->k_metric = ag->ag_metric; 18430Sstevel@tonic-gate return; 18440Sstevel@tonic-gate } 18450Sstevel@tonic-gate 18460Sstevel@tonic-gate if ((k->k_state & (KS_STATIC|KS_DEPRE_IF)) || 18470Sstevel@tonic-gate ((k->k_state & (KS_IF|KS_PASSIVE)) == KS_IF)) { 18480Sstevel@tonic-gate return; 18490Sstevel@tonic-gate } 18500Sstevel@tonic-gate 18510Sstevel@tonic-gate /* modify existing kernel entry if necessary */ 18520Sstevel@tonic-gate if (k->k_gate == ag->ag_nhop && k->k_ifp == ag->ag_ifp && 18530Sstevel@tonic-gate k->k_metric != ag->ag_metric) { 18540Sstevel@tonic-gate /* 18550Sstevel@tonic-gate * Must delete bad interface routes etc. 18560Sstevel@tonic-gate * to change them. 18570Sstevel@tonic-gate */ 18580Sstevel@tonic-gate if (k->k_metric == HOPCNT_INFINITY) 18590Sstevel@tonic-gate k->k_state |= KS_DEL_ADD; 18600Sstevel@tonic-gate k->k_gate = ag->ag_nhop; 18610Sstevel@tonic-gate k->k_metric = ag->ag_metric; 18620Sstevel@tonic-gate k->k_state |= KS_CHANGE; 18630Sstevel@tonic-gate } 18640Sstevel@tonic-gate 18650Sstevel@tonic-gate /* 18660Sstevel@tonic-gate * If the daemon thinks the route should exist, forget 18670Sstevel@tonic-gate * about any redirections. 18680Sstevel@tonic-gate * If the daemon thinks the route should exist, eventually 18690Sstevel@tonic-gate * override manual intervention by the operator. 18700Sstevel@tonic-gate */ 18710Sstevel@tonic-gate if ((k->k_state & (KS_DYNAMIC | KS_DELETED)) != 0) { 18720Sstevel@tonic-gate k->k_state &= ~KS_DYNAMIC; 18730Sstevel@tonic-gate k->k_state |= (KS_ADD | KS_DEL_ADD); 18740Sstevel@tonic-gate } 18750Sstevel@tonic-gate 18760Sstevel@tonic-gate if ((k->k_state & KS_GATEWAY) && !(ag->ag_state & AGS_GATEWAY)) { 18770Sstevel@tonic-gate k->k_state &= ~KS_GATEWAY; 18780Sstevel@tonic-gate k->k_state |= (KS_ADD | KS_DEL_ADD); 18790Sstevel@tonic-gate } else if (!(k->k_state & KS_GATEWAY) && (ag->ag_state & AGS_GATEWAY)) { 18800Sstevel@tonic-gate k->k_state |= KS_GATEWAY; 18810Sstevel@tonic-gate k->k_state |= (KS_ADD | KS_DEL_ADD); 18820Sstevel@tonic-gate } 18830Sstevel@tonic-gate 18840Sstevel@tonic-gate /* 18850Sstevel@tonic-gate * Deleting-and-adding is necessary to change aspects of a route. 18860Sstevel@tonic-gate * Just delete instead of deleting and then adding a bad route. 18870Sstevel@tonic-gate * Otherwise, we want to keep the route in the kernel. 18880Sstevel@tonic-gate */ 18890Sstevel@tonic-gate if (k->k_metric == HOPCNT_INFINITY && (k->k_state & KS_DEL_ADD)) 18900Sstevel@tonic-gate k->k_state |= KS_DELETE; 18910Sstevel@tonic-gate else 18920Sstevel@tonic-gate k->k_state &= ~KS_DELETE; 18930Sstevel@tonic-gate #undef RT 18940Sstevel@tonic-gate } 18950Sstevel@tonic-gate 18960Sstevel@tonic-gate /* 18970Sstevel@tonic-gate * Update our image of the kernel forwarding table using the given 18980Sstevel@tonic-gate * route from our internal routing table. 18990Sstevel@tonic-gate */ 19000Sstevel@tonic-gate 19010Sstevel@tonic-gate /*ARGSUSED1*/ 19020Sstevel@tonic-gate static int 19030Sstevel@tonic-gate walk_kern(struct radix_node *rn, void *argp) 19040Sstevel@tonic-gate { 19050Sstevel@tonic-gate #define RT ((struct rt_entry *)rn) 19060Sstevel@tonic-gate uint8_t metric, pref; 19070Sstevel@tonic-gate uint_t ags = 0; 19080Sstevel@tonic-gate int i; 19090Sstevel@tonic-gate struct rt_spare *rts; 19100Sstevel@tonic-gate 19110Sstevel@tonic-gate /* Do not install synthetic routes */ 19120Sstevel@tonic-gate if (RT->rt_state & RS_NET_SYN) 19130Sstevel@tonic-gate return (0); 19140Sstevel@tonic-gate 19150Sstevel@tonic-gate /* 19160Sstevel@tonic-gate * Do not install static routes here. Only 19170Sstevel@tonic-gate * read_rt->rtm_add->kern_add should install those 19180Sstevel@tonic-gate */ 19190Sstevel@tonic-gate if ((RT->rt_state & RS_STATIC) && 19200Sstevel@tonic-gate (RT->rt_spares[0].rts_origin != RO_FILE)) 19210Sstevel@tonic-gate return (0); 19220Sstevel@tonic-gate 19230Sstevel@tonic-gate /* Do not clobber kernel if this is a route for a dead interface */ 19240Sstevel@tonic-gate if (RT->rt_state & RS_BADIF) 19250Sstevel@tonic-gate return (0); 19260Sstevel@tonic-gate 19270Sstevel@tonic-gate if (!(RT->rt_state & RS_IF)) { 19280Sstevel@tonic-gate /* This is an ordinary route, not for an interface. */ 19290Sstevel@tonic-gate 19300Sstevel@tonic-gate /* 19310Sstevel@tonic-gate * aggregate, ordinary good routes without regard to 19320Sstevel@tonic-gate * their metric 19330Sstevel@tonic-gate */ 19340Sstevel@tonic-gate pref = 1; 19350Sstevel@tonic-gate ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE); 19360Sstevel@tonic-gate 19370Sstevel@tonic-gate /* 19380Sstevel@tonic-gate * Do not install host routes directly to hosts, to avoid 19390Sstevel@tonic-gate * interfering with ARP entries in the kernel table. 19400Sstevel@tonic-gate */ 19410Sstevel@tonic-gate if (RT_ISHOST(RT) && ntohl(RT->rt_dst) == RT->rt_gate) 19420Sstevel@tonic-gate return (0); 19430Sstevel@tonic-gate 19440Sstevel@tonic-gate } else { 19450Sstevel@tonic-gate /* 19460Sstevel@tonic-gate * This is an interface route. 19470Sstevel@tonic-gate * Do not install routes for "external" remote interfaces. 19480Sstevel@tonic-gate */ 19490Sstevel@tonic-gate if (RT->rt_ifp != NULL && (RT->rt_ifp->int_state & IS_EXTERNAL)) 19500Sstevel@tonic-gate return (0); 19510Sstevel@tonic-gate 19520Sstevel@tonic-gate /* Interfaces should override received routes. */ 19530Sstevel@tonic-gate pref = 0; 19540Sstevel@tonic-gate ags |= (AGS_IF | AGS_CORS_GATE); 19550Sstevel@tonic-gate if (RT->rt_ifp != NULL && 19560Sstevel@tonic-gate !(RT->rt_ifp->int_if_flags & IFF_LOOPBACK) && 19570Sstevel@tonic-gate (RT->rt_ifp->int_state & (IS_PASSIVE|IS_ALIAS)) == 19580Sstevel@tonic-gate IS_PASSIVE) { 19590Sstevel@tonic-gate ags |= AGS_PASSIVE; 19600Sstevel@tonic-gate } 19610Sstevel@tonic-gate 19620Sstevel@tonic-gate /* 19630Sstevel@tonic-gate * If it is not an interface, or an alias for an interface, 19640Sstevel@tonic-gate * it must be a "gateway." 19650Sstevel@tonic-gate * 19660Sstevel@tonic-gate * If it is a "remote" interface, it is also a "gateway" to 19670Sstevel@tonic-gate * the kernel if is not a alias. 19680Sstevel@tonic-gate */ 1969879Sbw if (RT->rt_ifp == NULL || (RT->rt_ifp->int_state & IS_REMOTE)) { 1970879Sbw 1971879Sbw ags |= (AGS_GATEWAY | AGS_SUPPRESS); 1972879Sbw 1973879Sbw /* 1974879Sbw * Do not aggregate IS_PASSIVE routes. 1975879Sbw */ 1976879Sbw if (!(RT->rt_ifp->int_state & IS_PASSIVE)) 1977879Sbw ags |= AGS_AGGREGATE; 1978879Sbw } 19790Sstevel@tonic-gate } 19800Sstevel@tonic-gate 19810Sstevel@tonic-gate metric = RT->rt_metric; 19820Sstevel@tonic-gate if (metric == HOPCNT_INFINITY) { 19830Sstevel@tonic-gate /* If the route is dead, try hard to aggregate. */ 19840Sstevel@tonic-gate pref = HOPCNT_INFINITY; 19850Sstevel@tonic-gate ags |= (AGS_FINE_GATE | AGS_SUPPRESS); 19860Sstevel@tonic-gate ags &= ~(AGS_IF | AGS_CORS_GATE); 19870Sstevel@tonic-gate } 19880Sstevel@tonic-gate 19890Sstevel@tonic-gate /* 19900Sstevel@tonic-gate * dump all routes that have the same metric as rt_spares[0] 19910Sstevel@tonic-gate * into the kern_table, to be added to the kernel. 19920Sstevel@tonic-gate */ 19930Sstevel@tonic-gate for (i = 0; i < RT->rt_num_spares; i++) { 19940Sstevel@tonic-gate rts = &RT->rt_spares[i]; 19950Sstevel@tonic-gate 19960Sstevel@tonic-gate /* Do not install external routes */ 19970Sstevel@tonic-gate if (rts->rts_flags & RTS_EXTERNAL) 19980Sstevel@tonic-gate continue; 19990Sstevel@tonic-gate 20000Sstevel@tonic-gate if (rts->rts_metric == metric) { 20010Sstevel@tonic-gate ag_check(RT->rt_dst, RT->rt_mask, 20020Sstevel@tonic-gate rts->rts_router, rts->rts_ifp, rts->rts_gate, 20030Sstevel@tonic-gate metric, pref, 0, 0, 20040Sstevel@tonic-gate (rts->rts_origin & RO_FILE) ? (ags|AGS_FILE) : ags, 20050Sstevel@tonic-gate kern_out); 20060Sstevel@tonic-gate } 20070Sstevel@tonic-gate } 20080Sstevel@tonic-gate return (0); 20090Sstevel@tonic-gate #undef RT 20100Sstevel@tonic-gate } 20110Sstevel@tonic-gate 20120Sstevel@tonic-gate 20130Sstevel@tonic-gate /* Update the kernel table to match the daemon table. */ 20140Sstevel@tonic-gate static void 20150Sstevel@tonic-gate fix_kern(void) 20160Sstevel@tonic-gate { 20170Sstevel@tonic-gate int i; 20180Sstevel@tonic-gate struct khash *k, *pk, *knext; 20190Sstevel@tonic-gate 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate need_kern = age_timer; 20220Sstevel@tonic-gate 20230Sstevel@tonic-gate /* Walk daemon table, updating the copy of the kernel table. */ 20240Sstevel@tonic-gate (void) rn_walktree(rhead, walk_kern, NULL); 20250Sstevel@tonic-gate ag_flush(0, 0, kern_out); 20260Sstevel@tonic-gate 20270Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 20280Sstevel@tonic-gate pk = NULL; 20290Sstevel@tonic-gate for (k = khash_bins[i]; k != NULL; k = knext) { 20300Sstevel@tonic-gate knext = k->k_next; 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate /* Do not touch local interface routes */ 20330Sstevel@tonic-gate if ((k->k_state & KS_DEPRE_IF) || 20340Sstevel@tonic-gate (k->k_state & (KS_IF|KS_PASSIVE)) == KS_IF) { 20350Sstevel@tonic-gate pk = k; 20360Sstevel@tonic-gate continue; 20370Sstevel@tonic-gate } 20380Sstevel@tonic-gate 20390Sstevel@tonic-gate /* Do not touch static routes */ 20400Sstevel@tonic-gate if (k->k_state & KS_STATIC) { 20410Sstevel@tonic-gate kern_check_static(k, 0); 20420Sstevel@tonic-gate pk = k; 20430Sstevel@tonic-gate continue; 20440Sstevel@tonic-gate } 20450Sstevel@tonic-gate 20460Sstevel@tonic-gate /* check hold on routes deleted by the operator */ 20470Sstevel@tonic-gate if (k->k_keep > now.tv_sec) { 20480Sstevel@tonic-gate /* ensure we check when the hold is over */ 20490Sstevel@tonic-gate LIM_SEC(need_kern, k->k_keep); 20500Sstevel@tonic-gate pk = k; 20510Sstevel@tonic-gate continue; 20520Sstevel@tonic-gate } 20530Sstevel@tonic-gate 20540Sstevel@tonic-gate if ((k->k_state & KS_DELETE) && 20550Sstevel@tonic-gate !(k->k_state & KS_DYNAMIC)) { 20560Sstevel@tonic-gate if ((k->k_dst == RIP_DEFAULT) && 20570Sstevel@tonic-gate (k->k_ifp != NULL) && 20580Sstevel@tonic-gate (kern_alternate(RIP_DEFAULT, 20590Sstevel@tonic-gate k->k_mask, k->k_gate, k->k_ifp, 20600Sstevel@tonic-gate NULL) == NULL)) 20610Sstevel@tonic-gate rdisc_restore(k->k_ifp); 20620Sstevel@tonic-gate kern_ioctl(k, RTM_DELETE, 0); 20630Sstevel@tonic-gate if (pk != NULL) 20640Sstevel@tonic-gate pk->k_next = knext; 20650Sstevel@tonic-gate else 20660Sstevel@tonic-gate khash_bins[i] = knext; 20670Sstevel@tonic-gate free(k); 20680Sstevel@tonic-gate continue; 20690Sstevel@tonic-gate } 20700Sstevel@tonic-gate 20710Sstevel@tonic-gate if (k->k_state & KS_DEL_ADD) 20720Sstevel@tonic-gate kern_ioctl(k, RTM_DELETE, 0); 20730Sstevel@tonic-gate 20740Sstevel@tonic-gate if (k->k_state & KS_ADD) { 20750Sstevel@tonic-gate if ((k->k_dst == RIP_DEFAULT) && 20760Sstevel@tonic-gate (k->k_ifp != NULL)) 20770Sstevel@tonic-gate rdisc_suppress(k->k_ifp); 20780Sstevel@tonic-gate kern_ioctl(k, RTM_ADD, 20790Sstevel@tonic-gate ((0 != (k->k_state & (KS_GATEWAY | 20800Sstevel@tonic-gate KS_DYNAMIC))) ? RTF_GATEWAY : 0)); 20810Sstevel@tonic-gate } else if (k->k_state & KS_CHANGE) { 20820Sstevel@tonic-gate /* 20830Sstevel@tonic-gate * Should be using RTM_CHANGE here, but 20840Sstevel@tonic-gate * since RTM_CHANGE is currently 20850Sstevel@tonic-gate * not multipath-aware, and assumes 20860Sstevel@tonic-gate * that RTF_GATEWAY implies the gateway 20870Sstevel@tonic-gate * of the route for dst has to be 20880Sstevel@tonic-gate * changed, we play safe, and do a del + add. 20890Sstevel@tonic-gate */ 20900Sstevel@tonic-gate kern_ioctl(k, RTM_DELETE, 0); 20910Sstevel@tonic-gate kern_ioctl(k, RTM_ADD, 20920Sstevel@tonic-gate ((0 != (k->k_state & (KS_GATEWAY | 20930Sstevel@tonic-gate KS_DYNAMIC))) ? RTF_GATEWAY : 0)); 20940Sstevel@tonic-gate } 20950Sstevel@tonic-gate k->k_state &= ~(KS_ADD|KS_CHANGE|KS_DEL_ADD); 20960Sstevel@tonic-gate 20970Sstevel@tonic-gate /* 20980Sstevel@tonic-gate * Mark this route to be deleted in the next cycle. 20990Sstevel@tonic-gate * This deletes routes that disappear from the 21000Sstevel@tonic-gate * daemon table, since the normal aging code 21010Sstevel@tonic-gate * will clear the bit for routes that have not 21020Sstevel@tonic-gate * disappeared from the daemon table. 21030Sstevel@tonic-gate */ 21040Sstevel@tonic-gate k->k_state |= KS_DELETE; 21050Sstevel@tonic-gate pk = k; 21060Sstevel@tonic-gate } 21070Sstevel@tonic-gate } 21080Sstevel@tonic-gate } 21090Sstevel@tonic-gate 21100Sstevel@tonic-gate 21110Sstevel@tonic-gate /* Delete a static route in the image of the kernel table. */ 21120Sstevel@tonic-gate void 21130Sstevel@tonic-gate del_static(in_addr_t dst, in_addr_t mask, in_addr_t gate, 21140Sstevel@tonic-gate struct interface *ifp, int gone) 21150Sstevel@tonic-gate { 21160Sstevel@tonic-gate struct khash *k; 21170Sstevel@tonic-gate struct rt_entry *rt; 21180Sstevel@tonic-gate 21190Sstevel@tonic-gate /* 21200Sstevel@tonic-gate * Just mark it in the table to be deleted next time the kernel 21210Sstevel@tonic-gate * table is updated. 21220Sstevel@tonic-gate * If it has already been deleted, mark it as such, and set its 21230Sstevel@tonic-gate * keep-timer so that it will not be deleted again for a while. 21240Sstevel@tonic-gate * This lets the operator delete a route added by the daemon 21250Sstevel@tonic-gate * and add a replacement. 21260Sstevel@tonic-gate */ 21270Sstevel@tonic-gate k = kern_find(dst, mask, gate, ifp, NULL); 21280Sstevel@tonic-gate if (k != NULL && (gate == 0 || k->k_gate == gate)) { 21290Sstevel@tonic-gate k->k_state &= ~(KS_STATIC | KS_DYNAMIC | KS_CHECK); 21300Sstevel@tonic-gate k->k_state |= KS_DELETE; 21310Sstevel@tonic-gate if (gone) { 21320Sstevel@tonic-gate k->k_state |= KS_DELETED; 21330Sstevel@tonic-gate k->k_keep = now.tv_sec + K_KEEP_LIM; 21340Sstevel@tonic-gate } 21350Sstevel@tonic-gate } 21360Sstevel@tonic-gate 21370Sstevel@tonic-gate rt = rtget(dst, mask); 21380Sstevel@tonic-gate if (rt != NULL && (rt->rt_state & RS_STATIC)) 21390Sstevel@tonic-gate rtbad(rt, NULL); 21400Sstevel@tonic-gate } 21410Sstevel@tonic-gate 21420Sstevel@tonic-gate 21430Sstevel@tonic-gate /* 21440Sstevel@tonic-gate * Delete all routes generated from ICMP Redirects that use a given gateway, 21450Sstevel@tonic-gate * as well as old redirected routes. 21460Sstevel@tonic-gate */ 21470Sstevel@tonic-gate void 21480Sstevel@tonic-gate del_redirects(in_addr_t bad_gate, time_t old) 21490Sstevel@tonic-gate { 21500Sstevel@tonic-gate int i; 21510Sstevel@tonic-gate struct khash *k; 21520Sstevel@tonic-gate boolean_t dosupply = should_supply(NULL); 21530Sstevel@tonic-gate 21540Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 21550Sstevel@tonic-gate for (k = khash_bins[i]; k != NULL; k = k->k_next) { 21560Sstevel@tonic-gate if (!(k->k_state & KS_DYNAMIC) || 21570Sstevel@tonic-gate (k->k_state & (KS_STATIC|KS_IF|KS_DEPRE_IF))) 21580Sstevel@tonic-gate continue; 21590Sstevel@tonic-gate 21600Sstevel@tonic-gate if (k->k_gate != bad_gate && k->k_redirect_time > old && 21610Sstevel@tonic-gate !dosupply) 21620Sstevel@tonic-gate continue; 21630Sstevel@tonic-gate 21640Sstevel@tonic-gate k->k_state |= KS_DELETE; 21650Sstevel@tonic-gate k->k_state &= ~KS_DYNAMIC; 21660Sstevel@tonic-gate need_kern.tv_sec = now.tv_sec; 21670Sstevel@tonic-gate trace_act("mark redirected %s --> %s for deletion", 21680Sstevel@tonic-gate addrname(k->k_dst, k->k_mask, 0), 21690Sstevel@tonic-gate naddr_ntoa(k->k_gate)); 21700Sstevel@tonic-gate } 21710Sstevel@tonic-gate } 21720Sstevel@tonic-gate } 21730Sstevel@tonic-gate 21740Sstevel@tonic-gate /* Start the daemon tables. */ 21750Sstevel@tonic-gate void 21760Sstevel@tonic-gate rtinit(void) 21770Sstevel@tonic-gate { 21780Sstevel@tonic-gate int i; 21790Sstevel@tonic-gate struct ag_info *ag; 21800Sstevel@tonic-gate 21810Sstevel@tonic-gate /* Initialize the radix trees */ 21820Sstevel@tonic-gate rn_init(); 21830Sstevel@tonic-gate (void) rn_inithead((void**)&rhead, 32); 21840Sstevel@tonic-gate 21850Sstevel@tonic-gate /* mark all of the slots in the table free */ 21860Sstevel@tonic-gate ag_avail = ag_slots; 21870Sstevel@tonic-gate for (ag = ag_slots, i = 1; i < NUM_AG_SLOTS; i++) { 21880Sstevel@tonic-gate ag->ag_fine = ag+1; 21890Sstevel@tonic-gate ag++; 21900Sstevel@tonic-gate } 21910Sstevel@tonic-gate } 21920Sstevel@tonic-gate 21930Sstevel@tonic-gate 21940Sstevel@tonic-gate static struct sockaddr_in dst_sock = {AF_INET}; 21950Sstevel@tonic-gate static struct sockaddr_in mask_sock = {AF_INET}; 21960Sstevel@tonic-gate 21970Sstevel@tonic-gate 21980Sstevel@tonic-gate static void 21990Sstevel@tonic-gate set_need_flash(void) 22000Sstevel@tonic-gate { 22010Sstevel@tonic-gate if (!need_flash) { 22020Sstevel@tonic-gate need_flash = _B_TRUE; 22030Sstevel@tonic-gate /* 22040Sstevel@tonic-gate * Do not send the flash update immediately. Wait a little 22050Sstevel@tonic-gate * while to hear from other routers. 22060Sstevel@tonic-gate */ 22070Sstevel@tonic-gate no_flash.tv_sec = now.tv_sec + MIN_WAITTIME; 22080Sstevel@tonic-gate } 22090Sstevel@tonic-gate } 22100Sstevel@tonic-gate 22110Sstevel@tonic-gate 22120Sstevel@tonic-gate /* Get a particular routing table entry */ 22130Sstevel@tonic-gate struct rt_entry * 22140Sstevel@tonic-gate rtget(in_addr_t dst, in_addr_t mask) 22150Sstevel@tonic-gate { 22160Sstevel@tonic-gate struct rt_entry *rt; 22170Sstevel@tonic-gate 22180Sstevel@tonic-gate dst_sock.sin_addr.s_addr = dst; 22190Sstevel@tonic-gate mask_sock.sin_addr.s_addr = htonl(mask); 22200Sstevel@tonic-gate rt = (struct rt_entry *)rhead->rnh_lookup(&dst_sock, &mask_sock, rhead); 22210Sstevel@tonic-gate if (rt == NULL || rt->rt_dst != dst || rt->rt_mask != mask) 22220Sstevel@tonic-gate return (NULL); 22230Sstevel@tonic-gate 22240Sstevel@tonic-gate return (rt); 22250Sstevel@tonic-gate } 22260Sstevel@tonic-gate 22270Sstevel@tonic-gate 22280Sstevel@tonic-gate /* Find a route to dst as the kernel would. */ 22290Sstevel@tonic-gate struct rt_entry * 22300Sstevel@tonic-gate rtfind(in_addr_t dst) 22310Sstevel@tonic-gate { 22320Sstevel@tonic-gate dst_sock.sin_addr.s_addr = dst; 22330Sstevel@tonic-gate return ((struct rt_entry *)rhead->rnh_matchaddr(&dst_sock, rhead)); 22340Sstevel@tonic-gate } 22350Sstevel@tonic-gate 22360Sstevel@tonic-gate 22370Sstevel@tonic-gate /* add a route to the table */ 22380Sstevel@tonic-gate void 22390Sstevel@tonic-gate rtadd(in_addr_t dst, 22400Sstevel@tonic-gate in_addr_t mask, 22410Sstevel@tonic-gate uint16_t state, /* rt_state for the entry */ 22420Sstevel@tonic-gate struct rt_spare *new) 22430Sstevel@tonic-gate { 22440Sstevel@tonic-gate struct rt_entry *rt; 22450Sstevel@tonic-gate in_addr_t smask; 22460Sstevel@tonic-gate int i; 22470Sstevel@tonic-gate struct rt_spare *rts; 22480Sstevel@tonic-gate 22490Sstevel@tonic-gate /* This is the only function that increments total_routes. */ 22500Sstevel@tonic-gate if (total_routes == MAX_ROUTES) { 22510Sstevel@tonic-gate msglog("have maximum (%d) routes", total_routes); 22520Sstevel@tonic-gate return; 22530Sstevel@tonic-gate } 22540Sstevel@tonic-gate 22550Sstevel@tonic-gate rt = rtmalloc(sizeof (*rt), "rtadd"); 22560Sstevel@tonic-gate (void) memset(rt, 0, sizeof (*rt)); 22570Sstevel@tonic-gate rt->rt_spares = rtmalloc(SPARE_INC * sizeof (struct rt_spare), 22580Sstevel@tonic-gate "rtadd"); 22590Sstevel@tonic-gate rt->rt_num_spares = SPARE_INC; 22600Sstevel@tonic-gate (void) memset(rt->rt_spares, 0, SPARE_INC * sizeof (struct rt_spare)); 22610Sstevel@tonic-gate for (rts = rt->rt_spares, i = rt->rt_num_spares; i != 0; i--, rts++) 22620Sstevel@tonic-gate rts->rts_metric = HOPCNT_INFINITY; 22630Sstevel@tonic-gate 22640Sstevel@tonic-gate rt->rt_nodes->rn_key = (uint8_t *)&rt->rt_dst_sock; 22650Sstevel@tonic-gate rt->rt_dst = dst; 22660Sstevel@tonic-gate rt->rt_dst_sock.sin_family = AF_INET; 22670Sstevel@tonic-gate if (mask != HOST_MASK) { 22680Sstevel@tonic-gate smask = std_mask(dst); 22690Sstevel@tonic-gate if ((smask & ~mask) == 0 && mask > smask) 22700Sstevel@tonic-gate state |= RS_SUBNET; 22710Sstevel@tonic-gate } 22720Sstevel@tonic-gate mask_sock.sin_addr.s_addr = htonl(mask); 22730Sstevel@tonic-gate rt->rt_mask = mask; 22740Sstevel@tonic-gate rt->rt_spares[0] = *new; 22750Sstevel@tonic-gate rt->rt_state = state; 22760Sstevel@tonic-gate rt->rt_time = now.tv_sec; 22770Sstevel@tonic-gate rt->rt_poison_metric = HOPCNT_INFINITY; 22780Sstevel@tonic-gate rt->rt_seqno = update_seqno; 22790Sstevel@tonic-gate 22800Sstevel@tonic-gate if (TRACEACTIONS) 22810Sstevel@tonic-gate trace_add_del("Add", rt); 22820Sstevel@tonic-gate 22830Sstevel@tonic-gate need_kern.tv_sec = now.tv_sec; 22840Sstevel@tonic-gate set_need_flash(); 22850Sstevel@tonic-gate 22860Sstevel@tonic-gate if (NULL == rhead->rnh_addaddr(&rt->rt_dst_sock, &mask_sock, rhead, 22870Sstevel@tonic-gate rt->rt_nodes)) { 22880Sstevel@tonic-gate msglog("rnh_addaddr() failed for %s mask=%s", 22890Sstevel@tonic-gate naddr_ntoa(dst), naddr_ntoa(htonl(mask))); 22900Sstevel@tonic-gate free(rt); 22910Sstevel@tonic-gate } 22920Sstevel@tonic-gate 22930Sstevel@tonic-gate total_routes++; 22940Sstevel@tonic-gate } 22950Sstevel@tonic-gate 22960Sstevel@tonic-gate 22970Sstevel@tonic-gate /* notice a changed route */ 22980Sstevel@tonic-gate void 22990Sstevel@tonic-gate rtchange(struct rt_entry *rt, 23000Sstevel@tonic-gate uint16_t state, /* new state bits */ 23010Sstevel@tonic-gate struct rt_spare *new, 23020Sstevel@tonic-gate char *label) 23030Sstevel@tonic-gate { 23040Sstevel@tonic-gate if (rt->rt_metric != new->rts_metric) { 23050Sstevel@tonic-gate /* 23060Sstevel@tonic-gate * Fix the kernel immediately if it seems the route 23070Sstevel@tonic-gate * has gone bad, since there may be a working route that 23080Sstevel@tonic-gate * aggregates this route. 23090Sstevel@tonic-gate */ 23100Sstevel@tonic-gate if (new->rts_metric == HOPCNT_INFINITY) { 23110Sstevel@tonic-gate need_kern.tv_sec = now.tv_sec; 23120Sstevel@tonic-gate if (new->rts_time >= now.tv_sec - EXPIRE_TIME) 23130Sstevel@tonic-gate new->rts_time = now.tv_sec - EXPIRE_TIME; 23140Sstevel@tonic-gate } 23150Sstevel@tonic-gate rt->rt_seqno = update_seqno; 23160Sstevel@tonic-gate set_need_flash(); 23170Sstevel@tonic-gate } 23180Sstevel@tonic-gate 23190Sstevel@tonic-gate if (rt->rt_gate != new->rts_gate) { 23200Sstevel@tonic-gate need_kern.tv_sec = now.tv_sec; 23210Sstevel@tonic-gate rt->rt_seqno = update_seqno; 23220Sstevel@tonic-gate set_need_flash(); 23230Sstevel@tonic-gate } 23240Sstevel@tonic-gate 23250Sstevel@tonic-gate state |= (rt->rt_state & RS_SUBNET); 23260Sstevel@tonic-gate 23270Sstevel@tonic-gate /* Keep various things from deciding ageless routes are stale. */ 23280Sstevel@tonic-gate if (!AGE_RT(state, rt->rt_spares[0].rts_origin, new->rts_ifp)) 23290Sstevel@tonic-gate new->rts_time = now.tv_sec; 23300Sstevel@tonic-gate 23310Sstevel@tonic-gate if (TRACEACTIONS) 23320Sstevel@tonic-gate trace_change(rt, state, new, 23330Sstevel@tonic-gate label ? label : "Chg "); 23340Sstevel@tonic-gate 23350Sstevel@tonic-gate rt->rt_state = state; 23360Sstevel@tonic-gate /* 23370Sstevel@tonic-gate * If the interface state of the new primary route is good, 23380Sstevel@tonic-gate * turn off RS_BADIF flag 23390Sstevel@tonic-gate */ 23400Sstevel@tonic-gate if ((rt->rt_state & RS_BADIF) && 23410Sstevel@tonic-gate IS_IFF_UP(new->rts_ifp->int_if_flags) && 23420Sstevel@tonic-gate !(new->rts_ifp->int_state & (IS_BROKE | IS_SICK))) 23430Sstevel@tonic-gate rt->rt_state &= ~(RS_BADIF); 23440Sstevel@tonic-gate 23450Sstevel@tonic-gate rt->rt_spares[0] = *new; 23460Sstevel@tonic-gate } 23470Sstevel@tonic-gate 23480Sstevel@tonic-gate 23490Sstevel@tonic-gate /* check for a better route among the spares */ 23500Sstevel@tonic-gate static struct rt_spare * 23510Sstevel@tonic-gate rts_better(struct rt_entry *rt) 23520Sstevel@tonic-gate { 23530Sstevel@tonic-gate struct rt_spare *rts, *rts1; 23540Sstevel@tonic-gate int i; 23550Sstevel@tonic-gate 23560Sstevel@tonic-gate /* find the best alternative among the spares */ 23570Sstevel@tonic-gate rts = rt->rt_spares+1; 23580Sstevel@tonic-gate for (i = rt->rt_num_spares, rts1 = rts+1; i > 2; i--, rts1++) { 23590Sstevel@tonic-gate if (BETTER_LINK(rt, rts1, rts)) 23600Sstevel@tonic-gate rts = rts1; 23610Sstevel@tonic-gate } 23620Sstevel@tonic-gate 23630Sstevel@tonic-gate return (rts); 23640Sstevel@tonic-gate } 23650Sstevel@tonic-gate 23660Sstevel@tonic-gate 23670Sstevel@tonic-gate /* switch to a backup route */ 23680Sstevel@tonic-gate void 23690Sstevel@tonic-gate rtswitch(struct rt_entry *rt, 23700Sstevel@tonic-gate struct rt_spare *rts) 23710Sstevel@tonic-gate { 23720Sstevel@tonic-gate struct rt_spare swap; 23730Sstevel@tonic-gate char label[10]; 23740Sstevel@tonic-gate 23750Sstevel@tonic-gate /* Do not change permanent routes */ 23760Sstevel@tonic-gate if (0 != (rt->rt_state & (RS_MHOME | RS_STATIC | 23770Sstevel@tonic-gate RS_NET_SYN | RS_IF))) 23780Sstevel@tonic-gate return; 23790Sstevel@tonic-gate 23800Sstevel@tonic-gate /* find the best alternative among the spares */ 23810Sstevel@tonic-gate if (rts == NULL) 23820Sstevel@tonic-gate rts = rts_better(rt); 23830Sstevel@tonic-gate 23840Sstevel@tonic-gate /* Do not bother if it is not worthwhile. */ 23850Sstevel@tonic-gate if (!BETTER_LINK(rt, rts, rt->rt_spares)) 23860Sstevel@tonic-gate return; 23870Sstevel@tonic-gate 23880Sstevel@tonic-gate swap = rt->rt_spares[0]; 23890Sstevel@tonic-gate (void) snprintf(label, sizeof (label), "Use #%d", 23900Sstevel@tonic-gate (int)(rts - rt->rt_spares)); 23910Sstevel@tonic-gate rtchange(rt, rt->rt_state & ~(RS_NET_SYN), rts, label); 23920Sstevel@tonic-gate 23930Sstevel@tonic-gate if (swap.rts_metric == HOPCNT_INFINITY) { 23940Sstevel@tonic-gate *rts = rts_empty; 23950Sstevel@tonic-gate } else { 23960Sstevel@tonic-gate *rts = swap; 23970Sstevel@tonic-gate } 23980Sstevel@tonic-gate 23990Sstevel@tonic-gate } 24000Sstevel@tonic-gate 24010Sstevel@tonic-gate 24020Sstevel@tonic-gate void 24030Sstevel@tonic-gate rtdelete(struct rt_entry *rt) 24040Sstevel@tonic-gate { 24050Sstevel@tonic-gate struct rt_entry *deleted_rt; 24060Sstevel@tonic-gate struct rt_spare *rts; 24070Sstevel@tonic-gate int i; 24080Sstevel@tonic-gate in_addr_t gate = rt->rt_gate; /* for debugging */ 24090Sstevel@tonic-gate 24100Sstevel@tonic-gate if (TRACEACTIONS) 24110Sstevel@tonic-gate trace_add_del("Del", rt); 24120Sstevel@tonic-gate 24130Sstevel@tonic-gate for (i = 0; i < rt->rt_num_spares; i++) { 24140Sstevel@tonic-gate rts = &rt->rt_spares[i]; 24150Sstevel@tonic-gate rts_delete(rt, rts); 24160Sstevel@tonic-gate } 24170Sstevel@tonic-gate 24180Sstevel@tonic-gate dst_sock.sin_addr.s_addr = rt->rt_dst; 24190Sstevel@tonic-gate mask_sock.sin_addr.s_addr = htonl(rt->rt_mask); 24200Sstevel@tonic-gate if (rt != (deleted_rt = 24210Sstevel@tonic-gate ((struct rt_entry *)rhead->rnh_deladdr(&dst_sock, &mask_sock, 24220Sstevel@tonic-gate rhead)))) { 24230Sstevel@tonic-gate msglog("rnh_deladdr(%s) failed; found rt 0x%lx", 24240Sstevel@tonic-gate rtname(rt->rt_dst, rt->rt_mask, gate), deleted_rt); 24250Sstevel@tonic-gate if (deleted_rt != NULL) 24260Sstevel@tonic-gate free(deleted_rt); 24270Sstevel@tonic-gate } 24280Sstevel@tonic-gate total_routes--; 24290Sstevel@tonic-gate free(rt); 24300Sstevel@tonic-gate 24310Sstevel@tonic-gate if (dst_sock.sin_addr.s_addr == RIP_DEFAULT) { 24320Sstevel@tonic-gate /* 24330Sstevel@tonic-gate * we just deleted the default route. Trigger rdisc_sort 24340Sstevel@tonic-gate * so that we can recover from any rdisc information that 24350Sstevel@tonic-gate * is valid 24360Sstevel@tonic-gate */ 24370Sstevel@tonic-gate rdisc_timer.tv_sec = 0; 24380Sstevel@tonic-gate } 24390Sstevel@tonic-gate } 24400Sstevel@tonic-gate 24410Sstevel@tonic-gate void 24420Sstevel@tonic-gate rts_delete(struct rt_entry *rt, struct rt_spare *rts) 24430Sstevel@tonic-gate { 24440Sstevel@tonic-gate struct khash *k; 24450Sstevel@tonic-gate 24460Sstevel@tonic-gate trace_upslot(rt, rts, &rts_empty); 24470Sstevel@tonic-gate k = kern_find(rt->rt_dst, rt->rt_mask, 24480Sstevel@tonic-gate rts->rts_gate, rts->rts_ifp, NULL); 24490Sstevel@tonic-gate if (k != NULL && 24500Sstevel@tonic-gate !(k->k_state & KS_DEPRE_IF) && 24510Sstevel@tonic-gate ((k->k_state & (KS_IF|KS_PASSIVE)) != KS_IF)) { 24520Sstevel@tonic-gate k->k_state |= KS_DELETE; 24530Sstevel@tonic-gate need_kern.tv_sec = now.tv_sec; 24540Sstevel@tonic-gate } 24550Sstevel@tonic-gate 24560Sstevel@tonic-gate *rts = rts_empty; 24570Sstevel@tonic-gate } 24580Sstevel@tonic-gate 24590Sstevel@tonic-gate /* 24600Sstevel@tonic-gate * Get rid of a bad route, and try to switch to a replacement. 24610Sstevel@tonic-gate * If the route has gone bad because of a bad interface, 24620Sstevel@tonic-gate * the information about the dead interface is available in badifp 24630Sstevel@tonic-gate * for the purpose of sanity checks, if_flags checks etc. 24640Sstevel@tonic-gate */ 24650Sstevel@tonic-gate static void 24660Sstevel@tonic-gate rtbad(struct rt_entry *rt, struct interface *badifp) 24670Sstevel@tonic-gate { 24680Sstevel@tonic-gate struct rt_spare new; 24690Sstevel@tonic-gate uint16_t rt_state; 24700Sstevel@tonic-gate 24710Sstevel@tonic-gate 24720Sstevel@tonic-gate if (badifp == NULL || (rt->rt_spares[0].rts_ifp == badifp)) { 24730Sstevel@tonic-gate /* Poison the route */ 24740Sstevel@tonic-gate new = rt->rt_spares[0]; 24750Sstevel@tonic-gate new.rts_metric = HOPCNT_INFINITY; 24760Sstevel@tonic-gate rt_state = rt->rt_state & ~(RS_IF | RS_LOCAL | RS_STATIC); 24770Sstevel@tonic-gate } 24780Sstevel@tonic-gate 24790Sstevel@tonic-gate if (badifp != NULL) { 24800Sstevel@tonic-gate /* 24810Sstevel@tonic-gate * Dont mark the rtentry bad unless the ifp for the primary 24820Sstevel@tonic-gate * route is the bad ifp 24830Sstevel@tonic-gate */ 24840Sstevel@tonic-gate if (rt->rt_spares[0].rts_ifp != badifp) 24850Sstevel@tonic-gate return; 24860Sstevel@tonic-gate /* 24870Sstevel@tonic-gate * badifp has just gone bad. We want to keep this 24880Sstevel@tonic-gate * rt_entry around so that we tell our rip-neighbors 24890Sstevel@tonic-gate * about the bad route, but we can't do anything 24900Sstevel@tonic-gate * to the kernel itself, so mark it as RS_BADIF 24910Sstevel@tonic-gate */ 24920Sstevel@tonic-gate trace_misc("rtbad:Setting RS_BADIF (%s)", badifp->int_name); 24930Sstevel@tonic-gate rt_state |= RS_BADIF; 24940Sstevel@tonic-gate new.rts_ifp = &dummy_ifp; 24950Sstevel@tonic-gate } 24960Sstevel@tonic-gate rtchange(rt, rt_state, &new, 0); 24970Sstevel@tonic-gate rtswitch(rt, 0); 24980Sstevel@tonic-gate } 24990Sstevel@tonic-gate 25000Sstevel@tonic-gate 25010Sstevel@tonic-gate /* 25020Sstevel@tonic-gate * Junk a RS_NET_SYN or RS_LOCAL route, 25030Sstevel@tonic-gate * unless it is needed by another interface. 25040Sstevel@tonic-gate */ 25050Sstevel@tonic-gate void 25060Sstevel@tonic-gate rtbad_sub(struct rt_entry *rt, struct interface *badifp) 25070Sstevel@tonic-gate { 25080Sstevel@tonic-gate struct interface *ifp, *ifp1; 25090Sstevel@tonic-gate struct intnet *intnetp; 25100Sstevel@tonic-gate uint_t state; 25110Sstevel@tonic-gate 25120Sstevel@tonic-gate 25130Sstevel@tonic-gate ifp1 = NULL; 25140Sstevel@tonic-gate state = 0; 25150Sstevel@tonic-gate 25160Sstevel@tonic-gate if (rt->rt_state & RS_LOCAL) { 25170Sstevel@tonic-gate /* 25180Sstevel@tonic-gate * Is this the route through loopback for the interface? 25190Sstevel@tonic-gate * If so, see if it is used by any other interfaces, such 25200Sstevel@tonic-gate * as a point-to-point interface with the same local address. 25210Sstevel@tonic-gate */ 25220Sstevel@tonic-gate for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) { 25230Sstevel@tonic-gate /* Retain it if another interface needs it. */ 25240Sstevel@tonic-gate if (ifp->int_addr == rt->rt_ifp->int_addr) { 25250Sstevel@tonic-gate state |= RS_LOCAL; 25260Sstevel@tonic-gate ifp1 = ifp; 25270Sstevel@tonic-gate break; 25280Sstevel@tonic-gate } 25290Sstevel@tonic-gate } 25300Sstevel@tonic-gate 25310Sstevel@tonic-gate } 25320Sstevel@tonic-gate 25330Sstevel@tonic-gate if (!(state & RS_LOCAL)) { 25340Sstevel@tonic-gate /* 25350Sstevel@tonic-gate * Retain RIPv1 logical network route if there is another 25360Sstevel@tonic-gate * interface that justifies it. 25370Sstevel@tonic-gate */ 25380Sstevel@tonic-gate if (rt->rt_state & RS_NET_SYN) { 25390Sstevel@tonic-gate for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) { 25400Sstevel@tonic-gate if ((ifp->int_state & IS_NEED_NET_SYN) && 25410Sstevel@tonic-gate rt->rt_mask == ifp->int_std_mask && 25420Sstevel@tonic-gate rt->rt_dst == ifp->int_std_addr) { 25430Sstevel@tonic-gate state |= RS_NET_SYN; 25440Sstevel@tonic-gate ifp1 = ifp; 25450Sstevel@tonic-gate break; 25460Sstevel@tonic-gate } 25470Sstevel@tonic-gate } 25480Sstevel@tonic-gate } 25490Sstevel@tonic-gate 25500Sstevel@tonic-gate /* or if there is an authority route that needs it. */ 25510Sstevel@tonic-gate for (intnetp = intnets; intnetp != NULL; 25520Sstevel@tonic-gate intnetp = intnetp->intnet_next) { 25530Sstevel@tonic-gate if (intnetp->intnet_addr == rt->rt_dst && 25540Sstevel@tonic-gate intnetp->intnet_mask == rt->rt_mask) { 25550Sstevel@tonic-gate state |= (RS_NET_SYN | RS_NET_INT); 25560Sstevel@tonic-gate break; 25570Sstevel@tonic-gate } 25580Sstevel@tonic-gate } 25590Sstevel@tonic-gate } 25600Sstevel@tonic-gate 25610Sstevel@tonic-gate if (ifp1 != NULL || (state & RS_NET_SYN)) { 25620Sstevel@tonic-gate struct rt_spare new = rt->rt_spares[0]; 25630Sstevel@tonic-gate new.rts_ifp = ifp1; 25640Sstevel@tonic-gate rtchange(rt, ((rt->rt_state & ~(RS_NET_SYN|RS_LOCAL)) | state), 25650Sstevel@tonic-gate &new, 0); 25660Sstevel@tonic-gate } else { 25670Sstevel@tonic-gate rtbad(rt, badifp); 25680Sstevel@tonic-gate } 25690Sstevel@tonic-gate } 25700Sstevel@tonic-gate 25710Sstevel@tonic-gate /* 25720Sstevel@tonic-gate * Called while walking the table looking for sick interfaces 25730Sstevel@tonic-gate * or after a time change. 25740Sstevel@tonic-gate */ 25750Sstevel@tonic-gate int 25760Sstevel@tonic-gate walk_bad(struct radix_node *rn, 25770Sstevel@tonic-gate void *argp) 25780Sstevel@tonic-gate { 25790Sstevel@tonic-gate #define RT ((struct rt_entry *)rn) 25800Sstevel@tonic-gate struct rt_spare *rts; 25810Sstevel@tonic-gate int i, j = -1; 25820Sstevel@tonic-gate 25830Sstevel@tonic-gate /* fix any spare routes through the interface */ 25840Sstevel@tonic-gate for (i = 1; i < RT->rt_num_spares; i++) { 25850Sstevel@tonic-gate rts = &((struct rt_entry *)rn)->rt_spares[i]; 25860Sstevel@tonic-gate 25870Sstevel@tonic-gate if (rts->rts_metric < HOPCNT_INFINITY && 25880Sstevel@tonic-gate (rts->rts_ifp == NULL || 25890Sstevel@tonic-gate (rts->rts_ifp->int_state & IS_BROKE))) 25900Sstevel@tonic-gate rts_delete(RT, rts); 25910Sstevel@tonic-gate else { 25920Sstevel@tonic-gate if (rts->rts_origin != RO_NONE) 25930Sstevel@tonic-gate j = i; 25940Sstevel@tonic-gate } 25950Sstevel@tonic-gate } 25960Sstevel@tonic-gate 25970Sstevel@tonic-gate /* 25980Sstevel@tonic-gate * Deal with the main route 25990Sstevel@tonic-gate * finished if it has been handled before or if its interface is ok 26000Sstevel@tonic-gate */ 26010Sstevel@tonic-gate if (RT->rt_ifp == NULL || !(RT->rt_ifp->int_state & IS_BROKE)) 26020Sstevel@tonic-gate return (0); 26030Sstevel@tonic-gate 26040Sstevel@tonic-gate /* Bad routes for other than interfaces are easy. */ 26050Sstevel@tonic-gate if (!(RT->rt_state & (RS_IF | RS_NET_SYN | RS_LOCAL))) { 2606552Ssowmini if (j > 0) { 2607552Ssowmini RT->rt_spares[0].rts_metric = HOPCNT_INFINITY; 26080Sstevel@tonic-gate rtswitch(RT, NULL); 2609552Ssowmini } else { 26100Sstevel@tonic-gate rtbad(RT, (struct interface *)argp); 2611552Ssowmini } 26120Sstevel@tonic-gate return (0); 26130Sstevel@tonic-gate } 26140Sstevel@tonic-gate 26150Sstevel@tonic-gate rtbad_sub(RT, (struct interface *)argp); 26160Sstevel@tonic-gate return (0); 26170Sstevel@tonic-gate #undef RT 26180Sstevel@tonic-gate } 26190Sstevel@tonic-gate 26200Sstevel@tonic-gate /* 26210Sstevel@tonic-gate * Called while walking the table to replace a duplicate interface 26220Sstevel@tonic-gate * with a backup. 26230Sstevel@tonic-gate */ 26240Sstevel@tonic-gate int 26250Sstevel@tonic-gate walk_rewire(struct radix_node *rn, void *argp) 26260Sstevel@tonic-gate { 26270Sstevel@tonic-gate struct rt_entry *RT = (struct rt_entry *)rn; 26280Sstevel@tonic-gate struct rewire_data *wire = (struct rewire_data *)argp; 26290Sstevel@tonic-gate struct rt_spare *rts; 26300Sstevel@tonic-gate int i; 26310Sstevel@tonic-gate 26320Sstevel@tonic-gate /* fix any spare routes through the interface */ 26330Sstevel@tonic-gate rts = RT->rt_spares; 26340Sstevel@tonic-gate for (i = RT->rt_num_spares; i > 0; i--, rts++) { 26350Sstevel@tonic-gate if (rts->rts_ifp == wire->if_old) { 26360Sstevel@tonic-gate rts->rts_ifp = wire->if_new; 26370Sstevel@tonic-gate if ((RT->rt_dst == RIP_DEFAULT) && 26380Sstevel@tonic-gate (wire->if_old->int_state & IS_SUPPRESS_RDISC)) 26390Sstevel@tonic-gate rdisc_suppress(rts->rts_ifp); 26400Sstevel@tonic-gate if ((rts->rts_metric += wire->metric_delta) > 26410Sstevel@tonic-gate HOPCNT_INFINITY) 26420Sstevel@tonic-gate rts->rts_metric = HOPCNT_INFINITY; 26430Sstevel@tonic-gate 26440Sstevel@tonic-gate /* 26450Sstevel@tonic-gate * If the main route is getting a worse metric, 26460Sstevel@tonic-gate * then it may be time to switch to a backup. 26470Sstevel@tonic-gate */ 26480Sstevel@tonic-gate if (i == RT->rt_num_spares && wire->metric_delta > 0) { 26490Sstevel@tonic-gate rtswitch(RT, NULL); 26500Sstevel@tonic-gate } 26510Sstevel@tonic-gate } 26520Sstevel@tonic-gate } 26530Sstevel@tonic-gate 26540Sstevel@tonic-gate return (0); 26550Sstevel@tonic-gate } 26560Sstevel@tonic-gate 26570Sstevel@tonic-gate /* Check the age of an individual route. */ 26580Sstevel@tonic-gate static int 26590Sstevel@tonic-gate walk_age(struct radix_node *rn, void *argp) 26600Sstevel@tonic-gate { 26610Sstevel@tonic-gate #define RT ((struct rt_entry *)rn) 26620Sstevel@tonic-gate struct interface *ifp; 26630Sstevel@tonic-gate struct rt_spare *rts; 26640Sstevel@tonic-gate int i; 26650Sstevel@tonic-gate in_addr_t age_bad_gate = *(in_addr_t *)argp; 26660Sstevel@tonic-gate 26670Sstevel@tonic-gate 26680Sstevel@tonic-gate /* 26690Sstevel@tonic-gate * age all of the spare routes, including the primary route 26700Sstevel@tonic-gate * currently in use 26710Sstevel@tonic-gate */ 26720Sstevel@tonic-gate rts = RT->rt_spares; 26730Sstevel@tonic-gate for (i = RT->rt_num_spares; i != 0; i--, rts++) { 26740Sstevel@tonic-gate 26750Sstevel@tonic-gate ifp = rts->rts_ifp; 26760Sstevel@tonic-gate if (i == RT->rt_num_spares) { 26770Sstevel@tonic-gate if (!AGE_RT(RT->rt_state, rts->rts_origin, ifp)) { 26780Sstevel@tonic-gate /* 26790Sstevel@tonic-gate * Keep various things from deciding ageless 26800Sstevel@tonic-gate * routes are stale 26810Sstevel@tonic-gate */ 26820Sstevel@tonic-gate rts->rts_time = now.tv_sec; 26830Sstevel@tonic-gate continue; 26840Sstevel@tonic-gate } 26850Sstevel@tonic-gate 26860Sstevel@tonic-gate /* forget RIP routes after RIP has been turned off. */ 26870Sstevel@tonic-gate if (rip_sock < 0) { 26880Sstevel@tonic-gate rts->rts_time = now_stale + 1; 26890Sstevel@tonic-gate } 26900Sstevel@tonic-gate } 26910Sstevel@tonic-gate 26920Sstevel@tonic-gate /* age failing routes */ 26930Sstevel@tonic-gate if (age_bad_gate == rts->rts_gate && 26940Sstevel@tonic-gate rts->rts_time >= now_stale) { 26950Sstevel@tonic-gate rts->rts_time -= SUPPLY_INTERVAL; 26960Sstevel@tonic-gate } 26970Sstevel@tonic-gate 26980Sstevel@tonic-gate /* trash the spare routes when they go bad */ 26990Sstevel@tonic-gate if (rts->rts_origin == RO_RIP && 27000Sstevel@tonic-gate ((rip_sock < 0) || 27010Sstevel@tonic-gate (rts->rts_metric < HOPCNT_INFINITY && 27020Sstevel@tonic-gate now_garbage > rts->rts_time)) && 27030Sstevel@tonic-gate i != RT->rt_num_spares) { 27040Sstevel@tonic-gate rts_delete(RT, rts); 27050Sstevel@tonic-gate } 27060Sstevel@tonic-gate } 27070Sstevel@tonic-gate 27080Sstevel@tonic-gate 27090Sstevel@tonic-gate /* finished if the active route is still fresh */ 27100Sstevel@tonic-gate if (now_stale <= RT->rt_time) 27110Sstevel@tonic-gate return (0); 27120Sstevel@tonic-gate 27130Sstevel@tonic-gate /* try to switch to an alternative */ 27140Sstevel@tonic-gate rtswitch(RT, NULL); 27150Sstevel@tonic-gate 27160Sstevel@tonic-gate /* Delete a dead route after it has been publically mourned. */ 27170Sstevel@tonic-gate if (now_garbage > RT->rt_time) { 27180Sstevel@tonic-gate rtdelete(RT); 27190Sstevel@tonic-gate return (0); 27200Sstevel@tonic-gate } 27210Sstevel@tonic-gate 27220Sstevel@tonic-gate /* Start poisoning a bad route before deleting it. */ 27230Sstevel@tonic-gate if (now.tv_sec - RT->rt_time > EXPIRE_TIME) { 27240Sstevel@tonic-gate struct rt_spare new = RT->rt_spares[0]; 27250Sstevel@tonic-gate 27260Sstevel@tonic-gate new.rts_metric = HOPCNT_INFINITY; 27270Sstevel@tonic-gate rtchange(RT, RT->rt_state, &new, 0); 27280Sstevel@tonic-gate } 27290Sstevel@tonic-gate return (0); 27300Sstevel@tonic-gate } 27310Sstevel@tonic-gate 27320Sstevel@tonic-gate 27330Sstevel@tonic-gate /* Watch for dead routes and interfaces. */ 27340Sstevel@tonic-gate void 27350Sstevel@tonic-gate age(in_addr_t bad_gate) 27360Sstevel@tonic-gate { 27370Sstevel@tonic-gate struct interface *ifp; 27380Sstevel@tonic-gate int need_query = 0; 27390Sstevel@tonic-gate 27400Sstevel@tonic-gate /* 27410Sstevel@tonic-gate * If not listening to RIP, there is no need to age the routes in 27420Sstevel@tonic-gate * the table. 27430Sstevel@tonic-gate */ 27440Sstevel@tonic-gate age_timer.tv_sec = (now.tv_sec 27450Sstevel@tonic-gate + ((rip_sock < 0) ? NEVER : SUPPLY_INTERVAL)); 27460Sstevel@tonic-gate 27470Sstevel@tonic-gate /* 27480Sstevel@tonic-gate * Check for dead IS_REMOTE interfaces by timing their 27490Sstevel@tonic-gate * transmissions. 27500Sstevel@tonic-gate */ 27510Sstevel@tonic-gate for (ifp = ifnet; ifp; ifp = ifp->int_next) { 27520Sstevel@tonic-gate if (!(ifp->int_state & IS_REMOTE)) 27530Sstevel@tonic-gate continue; 27540Sstevel@tonic-gate 27550Sstevel@tonic-gate /* ignore unreachable remote interfaces */ 27560Sstevel@tonic-gate if (!check_remote(ifp)) 27570Sstevel@tonic-gate continue; 27580Sstevel@tonic-gate 27590Sstevel@tonic-gate /* Restore remote interface that has become reachable */ 27600Sstevel@tonic-gate if (ifp->int_state & IS_BROKE) 27610Sstevel@tonic-gate if_ok(ifp, "remote ", _B_FALSE); 27620Sstevel@tonic-gate 27630Sstevel@tonic-gate if (ifp->int_act_time != NEVER && 27640Sstevel@tonic-gate now.tv_sec - ifp->int_act_time > EXPIRE_TIME) { 27650Sstevel@tonic-gate writelog(LOG_NOTICE, 27660Sstevel@tonic-gate "remote interface %s to %s timed out after" 27670Sstevel@tonic-gate " %ld:%ld", 27680Sstevel@tonic-gate ifp->int_name, 27690Sstevel@tonic-gate naddr_ntoa(ifp->int_dstaddr), 27700Sstevel@tonic-gate (now.tv_sec - ifp->int_act_time)/60, 27710Sstevel@tonic-gate (now.tv_sec - ifp->int_act_time)%60); 27720Sstevel@tonic-gate if_sick(ifp, _B_FALSE); 27730Sstevel@tonic-gate } 27740Sstevel@tonic-gate 27750Sstevel@tonic-gate /* 27760Sstevel@tonic-gate * If we have not heard from the other router 27770Sstevel@tonic-gate * recently, ask it. 27780Sstevel@tonic-gate */ 27790Sstevel@tonic-gate if (now.tv_sec >= ifp->int_query_time) { 27800Sstevel@tonic-gate ifp->int_query_time = NEVER; 27810Sstevel@tonic-gate need_query = 1; 27820Sstevel@tonic-gate } 27830Sstevel@tonic-gate } 27840Sstevel@tonic-gate 27850Sstevel@tonic-gate /* Age routes. */ 27860Sstevel@tonic-gate (void) rn_walktree(rhead, walk_age, &bad_gate); 27870Sstevel@tonic-gate 27880Sstevel@tonic-gate /* 27890Sstevel@tonic-gate * delete old redirected routes to keep the kernel table small 27900Sstevel@tonic-gate * and prevent blackholes 27910Sstevel@tonic-gate */ 27920Sstevel@tonic-gate del_redirects(bad_gate, now.tv_sec-STALE_TIME); 27930Sstevel@tonic-gate 27940Sstevel@tonic-gate /* Update the kernel routing table. */ 27950Sstevel@tonic-gate fix_kern(); 27960Sstevel@tonic-gate 27970Sstevel@tonic-gate /* poke reticent remote gateways */ 27980Sstevel@tonic-gate if (need_query) 27990Sstevel@tonic-gate rip_query(); 28000Sstevel@tonic-gate } 28010Sstevel@tonic-gate 28020Sstevel@tonic-gate void 28030Sstevel@tonic-gate kern_dump(void) 28040Sstevel@tonic-gate { 28050Sstevel@tonic-gate int i; 28060Sstevel@tonic-gate struct khash *k; 28070Sstevel@tonic-gate 28080Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 28090Sstevel@tonic-gate for (k = khash_bins[i]; k != NULL; k = k->k_next) 28100Sstevel@tonic-gate trace_khash(k); 28110Sstevel@tonic-gate } 28120Sstevel@tonic-gate } 28130Sstevel@tonic-gate 28140Sstevel@tonic-gate 28150Sstevel@tonic-gate static struct interface * 28160Sstevel@tonic-gate gwkludge_iflookup(in_addr_t dstaddr, in_addr_t addr, in_addr_t mask) 28170Sstevel@tonic-gate { 28180Sstevel@tonic-gate uint32_t int_state; 28190Sstevel@tonic-gate struct interface *ifp; 28200Sstevel@tonic-gate 28210Sstevel@tonic-gate for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) { 28220Sstevel@tonic-gate int_state = ifp->int_state; 28230Sstevel@tonic-gate 28240Sstevel@tonic-gate if (!(int_state & IS_REMOTE)) 28250Sstevel@tonic-gate continue; 28260Sstevel@tonic-gate 28270Sstevel@tonic-gate if (ifp->int_dstaddr == dstaddr && ifp->int_addr == addr && 28280Sstevel@tonic-gate ifp->int_mask == mask) 28290Sstevel@tonic-gate return (ifp); 28300Sstevel@tonic-gate } 28310Sstevel@tonic-gate return (NULL); 28320Sstevel@tonic-gate } 2833