10Sstevel@tonic-gate /* 21676Sjpk * 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 906*2781Ssowmini if (ifp != NULL && ifp->int_phys != NULL) { 907*2781Ssowmini ifp = ifwithname(ifp->int_phys->phyi_name); 908*2781Ssowmini } 909*2781Ssowmini 9100Sstevel@tonic-gate for (pk = &KHASH(dst, mask); (k = *pk) != NULL; pk = &k->k_next) { 9110Sstevel@tonic-gate if (k->k_dst == dst && k->k_mask == mask && 9120Sstevel@tonic-gate (gate == 0 || k->k_gate == gate) && 9130Sstevel@tonic-gate (ifp == NULL || k->k_ifp == ifp)) { 9140Sstevel@tonic-gate break; 9150Sstevel@tonic-gate } 9160Sstevel@tonic-gate } 9170Sstevel@tonic-gate if (ppk != NULL) 9180Sstevel@tonic-gate *ppk = pk; 9190Sstevel@tonic-gate return (k); 9200Sstevel@tonic-gate } 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate /* 9240Sstevel@tonic-gate * Find out if there is an alternate route to a given destination 9250Sstevel@tonic-gate * off of a given interface. 9260Sstevel@tonic-gate */ 9270Sstevel@tonic-gate static struct khash * 9280Sstevel@tonic-gate kern_alternate(in_addr_t dst, in_addr_t mask, in_addr_t gate, 9290Sstevel@tonic-gate struct interface *ifp, struct khash ***ppk) 9300Sstevel@tonic-gate { 9310Sstevel@tonic-gate struct khash *k, **pk; 9320Sstevel@tonic-gate 933*2781Ssowmini if (ifp != NULL && ifp->int_phys != NULL) { 934*2781Ssowmini ifp = ifwithname(ifp->int_phys->phyi_name); 935*2781Ssowmini } 9360Sstevel@tonic-gate for (pk = &KHASH(dst, mask); (k = *pk) != NULL; pk = &k->k_next) { 9370Sstevel@tonic-gate if (k->k_dst == dst && k->k_mask == mask && 9380Sstevel@tonic-gate (k->k_gate != gate) && 9390Sstevel@tonic-gate (k->k_ifp == ifp)) { 9400Sstevel@tonic-gate break; 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate } 9430Sstevel@tonic-gate if (ppk != NULL) 9440Sstevel@tonic-gate *ppk = pk; 9450Sstevel@tonic-gate return (k); 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate static struct khash * 9490Sstevel@tonic-gate kern_add(in_addr_t dst, uint32_t mask, in_addr_t gate, struct interface *ifp) 9500Sstevel@tonic-gate { 9510Sstevel@tonic-gate struct khash *k, **pk; 9520Sstevel@tonic-gate 953*2781Ssowmini if (ifp != NULL && ifp->int_phys != NULL) { 954*2781Ssowmini ifp = ifwithname(ifp->int_phys->phyi_name); 955*2781Ssowmini } 9560Sstevel@tonic-gate k = kern_find(dst, mask, gate, ifp, &pk); 9570Sstevel@tonic-gate if (k != NULL) 9580Sstevel@tonic-gate return (k); 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate k = rtmalloc(sizeof (*k), "kern_add"); 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate (void) memset(k, 0, sizeof (*k)); 9630Sstevel@tonic-gate k->k_dst = dst; 9640Sstevel@tonic-gate k->k_mask = mask; 9650Sstevel@tonic-gate k->k_state = KS_NEW; 9660Sstevel@tonic-gate k->k_keep = now.tv_sec; 9670Sstevel@tonic-gate k->k_gate = gate; 9680Sstevel@tonic-gate k->k_ifp = ifp; 9690Sstevel@tonic-gate *pk = k; 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate return (k); 9720Sstevel@tonic-gate } 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate /* delete all khash entries that are wired through the interface ifp */ 9750Sstevel@tonic-gate void 9760Sstevel@tonic-gate kern_flush_ifp(struct interface *ifp) 9770Sstevel@tonic-gate { 9780Sstevel@tonic-gate struct khash *k, *kprev, *knext; 9790Sstevel@tonic-gate int i; 9800Sstevel@tonic-gate 981*2781Ssowmini if (ifp != NULL && ifp->int_phys != NULL) { 982*2781Ssowmini ifp = ifwithname(ifp->int_phys->phyi_name); 983*2781Ssowmini } 9840Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 9850Sstevel@tonic-gate kprev = NULL; 9860Sstevel@tonic-gate for (k = khash_bins[i]; k != NULL; k = knext) { 9870Sstevel@tonic-gate knext = k->k_next; 9880Sstevel@tonic-gate if (k->k_ifp == ifp) { 9890Sstevel@tonic-gate if (kprev != NULL) 9900Sstevel@tonic-gate kprev->k_next = k->k_next; 9910Sstevel@tonic-gate else 9920Sstevel@tonic-gate khash_bins[i] = k->k_next; 9930Sstevel@tonic-gate free(k); 9940Sstevel@tonic-gate continue; 9950Sstevel@tonic-gate } 9960Sstevel@tonic-gate kprev = k; 9970Sstevel@tonic-gate } 9980Sstevel@tonic-gate } 9990Sstevel@tonic-gate } 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate /* 10020Sstevel@tonic-gate * rewire khash entries that currently go through oldifp to 10030Sstevel@tonic-gate * go through newifp. 10040Sstevel@tonic-gate */ 10050Sstevel@tonic-gate void 10060Sstevel@tonic-gate kern_rewire_ifp(struct interface *oldifp, struct interface *newifp) 10070Sstevel@tonic-gate { 10080Sstevel@tonic-gate struct khash *k; 10090Sstevel@tonic-gate int i; 10100Sstevel@tonic-gate 1011*2781Ssowmini if (oldifp != NULL && oldifp->int_phys != NULL) { 1012*2781Ssowmini oldifp = ifwithname(oldifp->int_phys->phyi_name); 1013*2781Ssowmini } 1014*2781Ssowmini if (newifp != NULL && newifp->int_phys != NULL) { 1015*2781Ssowmini newifp = ifwithname(newifp->int_phys->phyi_name); 1016*2781Ssowmini } 10170Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 10180Sstevel@tonic-gate for (k = khash_bins[i]; k; k = k->k_next) { 10190Sstevel@tonic-gate if (k->k_ifp == oldifp) { 10200Sstevel@tonic-gate k->k_ifp = newifp; 10210Sstevel@tonic-gate trace_misc("kern_rewire_ifp k 0x%lx " 10220Sstevel@tonic-gate "from %s to %s", k, oldifp->int_name, 10230Sstevel@tonic-gate newifp->int_name); 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate } 10260Sstevel@tonic-gate } 10270Sstevel@tonic-gate } 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate /* 10310Sstevel@tonic-gate * Check that a static route it is still in the daemon table, and not 10320Sstevel@tonic-gate * deleted by interfaces coming and going. This is also the routine 10330Sstevel@tonic-gate * responsible for adding new static routes to the daemon table. 10340Sstevel@tonic-gate */ 10350Sstevel@tonic-gate static void 10360Sstevel@tonic-gate kern_check_static(struct khash *k, struct interface *ifp) 10370Sstevel@tonic-gate { 10380Sstevel@tonic-gate struct rt_entry *rt; 10390Sstevel@tonic-gate struct rt_spare new; 10400Sstevel@tonic-gate uint16_t rt_state = RS_STATIC; 10410Sstevel@tonic-gate 1042*2781Ssowmini if (ifp != NULL && ifp->int_phys != NULL) { 1043*2781Ssowmini ifp = ifwithname(ifp->int_phys->phyi_name); 1044*2781Ssowmini } 10450Sstevel@tonic-gate (void) memset(&new, 0, sizeof (new)); 10460Sstevel@tonic-gate new.rts_ifp = ifp; 10470Sstevel@tonic-gate new.rts_gate = k->k_gate; 10480Sstevel@tonic-gate new.rts_router = (ifp != NULL) ? ifp->int_addr : loopaddr; 10490Sstevel@tonic-gate new.rts_metric = k->k_metric; 10500Sstevel@tonic-gate new.rts_time = now.tv_sec; 10510Sstevel@tonic-gate new.rts_origin = RO_STATIC; 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate rt = rtget(k->k_dst, k->k_mask); 10540Sstevel@tonic-gate if ((ifp != NULL && !IS_IFF_ROUTING(ifp->int_if_flags)) || 10550Sstevel@tonic-gate (k->k_state & KS_PRIVATE)) 10560Sstevel@tonic-gate rt_state |= RS_NOPROPAGATE; 10570Sstevel@tonic-gate 10580Sstevel@tonic-gate if (rt != NULL) { 10590Sstevel@tonic-gate if ((rt->rt_state & RS_STATIC) == 0) { 10600Sstevel@tonic-gate /* 10610Sstevel@tonic-gate * We are already tracking this dest/mask 10620Sstevel@tonic-gate * via RIP/RDISC. Ignore the static route, 10630Sstevel@tonic-gate * because we don't currently have a good 10640Sstevel@tonic-gate * way to compare metrics on static routes 10650Sstevel@tonic-gate * with rip metrics, and therefore cannot 10660Sstevel@tonic-gate * mix and match the two. 10670Sstevel@tonic-gate */ 10680Sstevel@tonic-gate return; 10690Sstevel@tonic-gate } 10700Sstevel@tonic-gate rt_state |= rt->rt_state; 10710Sstevel@tonic-gate if (rt->rt_state != rt_state) 10720Sstevel@tonic-gate rtchange(rt, rt_state, &new, 0); 10730Sstevel@tonic-gate } else { 10740Sstevel@tonic-gate rtadd(k->k_dst, k->k_mask, rt_state, &new); 10750Sstevel@tonic-gate } 10760Sstevel@tonic-gate } 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate 10790Sstevel@tonic-gate /* operate on a kernel entry */ 10800Sstevel@tonic-gate static void 10810Sstevel@tonic-gate kern_ioctl(struct khash *k, 10820Sstevel@tonic-gate int action, /* RTM_DELETE, etc */ 10830Sstevel@tonic-gate int flags) 10840Sstevel@tonic-gate { 10850Sstevel@tonic-gate if (((k->k_state & (KS_IF|KS_PASSIVE)) == KS_IF) || 10860Sstevel@tonic-gate (k->k_state & KS_DEPRE_IF)) { 10870Sstevel@tonic-gate /* 10880Sstevel@tonic-gate * Prevent execution of RTM_DELETE, RTM_ADD or 10890Sstevel@tonic-gate * RTM_CHANGE of interface routes 10900Sstevel@tonic-gate */ 10910Sstevel@tonic-gate trace_act("Blocking execution of %s %s --> %s ", 10920Sstevel@tonic-gate rtm_type_name(action), 10930Sstevel@tonic-gate addrname(k->k_dst, k->k_mask, 0), naddr_ntoa(k->k_gate)); 10940Sstevel@tonic-gate return; 10950Sstevel@tonic-gate } 10960Sstevel@tonic-gate 10970Sstevel@tonic-gate switch (action) { 10980Sstevel@tonic-gate case RTM_DELETE: 10990Sstevel@tonic-gate k->k_state &= ~KS_DYNAMIC; 11000Sstevel@tonic-gate if (k->k_state & KS_DELETED) 11010Sstevel@tonic-gate return; 11020Sstevel@tonic-gate k->k_state |= KS_DELETED; 11030Sstevel@tonic-gate break; 11040Sstevel@tonic-gate case RTM_ADD: 11050Sstevel@tonic-gate k->k_state &= ~KS_DELETED; 11060Sstevel@tonic-gate break; 11070Sstevel@tonic-gate case RTM_CHANGE: 11080Sstevel@tonic-gate if (k->k_state & KS_DELETED) { 11090Sstevel@tonic-gate action = RTM_ADD; 11100Sstevel@tonic-gate k->k_state &= ~KS_DELETED; 11110Sstevel@tonic-gate } 11120Sstevel@tonic-gate break; 11130Sstevel@tonic-gate } 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate rtioctl(action, k->k_dst, k->k_gate, k->k_mask, k->k_ifp, 11160Sstevel@tonic-gate k->k_metric, flags); 11170Sstevel@tonic-gate } 11180Sstevel@tonic-gate 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate /* add a route the kernel told us */ 11210Sstevel@tonic-gate static void 11220Sstevel@tonic-gate rtm_add(struct rt_msghdr *rtm, 11230Sstevel@tonic-gate struct rt_addrinfo *info, 11240Sstevel@tonic-gate time_t keep, 11250Sstevel@tonic-gate boolean_t interf_route, 11260Sstevel@tonic-gate struct interface *ifptr) 11270Sstevel@tonic-gate { 11280Sstevel@tonic-gate struct khash *k; 11290Sstevel@tonic-gate struct interface *ifp = ifptr; 11300Sstevel@tonic-gate in_addr_t mask, gate = 0; 11310Sstevel@tonic-gate static struct msg_limit msg_no_ifp; 11320Sstevel@tonic-gate 11330Sstevel@tonic-gate if (rtm->rtm_flags & RTF_HOST) { 11340Sstevel@tonic-gate mask = HOST_MASK; 11350Sstevel@tonic-gate } else if (INFO_MASK(info) != 0) { 11360Sstevel@tonic-gate mask = ntohl(S_ADDR(INFO_MASK(info))); 11370Sstevel@tonic-gate } else { 11380Sstevel@tonic-gate writelog(LOG_WARNING, 11390Sstevel@tonic-gate "ignore %s without mask", rtm_type_name(rtm->rtm_type)); 11400Sstevel@tonic-gate return; 11410Sstevel@tonic-gate } 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate /* 11440Sstevel@tonic-gate * Find the interface toward the gateway. 11450Sstevel@tonic-gate */ 11460Sstevel@tonic-gate if (INFO_GATE(info) != NULL) 11470Sstevel@tonic-gate gate = S_ADDR(INFO_GATE(info)); 11480Sstevel@tonic-gate 11490Sstevel@tonic-gate if (ifp == NULL) { 11500Sstevel@tonic-gate if (INFO_GATE(info) != NULL) 11510Sstevel@tonic-gate ifp = iflookup(gate); 1152*2781Ssowmini if (ifp == NULL) { 11530Sstevel@tonic-gate msglim(&msg_no_ifp, gate, 11540Sstevel@tonic-gate "route %s --> %s nexthop is not directly connected", 11550Sstevel@tonic-gate addrname(S_ADDR(INFO_DST(info)), mask, 0), 11560Sstevel@tonic-gate naddr_ntoa(gate)); 1157*2781Ssowmini } else { 1158*2781Ssowmini if (ifp->int_phys != NULL) { 1159*2781Ssowmini ifp = ifwithname(ifp->int_phys->phyi_name); 1160*2781Ssowmini } 1161*2781Ssowmini } 11620Sstevel@tonic-gate } 11630Sstevel@tonic-gate 11640Sstevel@tonic-gate k = kern_add(S_ADDR(INFO_DST(info)), mask, gate, ifp); 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate if (k->k_state & KS_NEW) 11670Sstevel@tonic-gate k->k_keep = now.tv_sec+keep; 11680Sstevel@tonic-gate if (INFO_GATE(info) == 0) { 11690Sstevel@tonic-gate trace_act("note %s without gateway", 11700Sstevel@tonic-gate rtm_type_name(rtm->rtm_type)); 11710Sstevel@tonic-gate k->k_metric = HOPCNT_INFINITY; 11720Sstevel@tonic-gate } else if (INFO_GATE(info)->ss_family != AF_INET) { 11730Sstevel@tonic-gate trace_act("note %s with gateway AF=%d", 11740Sstevel@tonic-gate rtm_type_name(rtm->rtm_type), 11750Sstevel@tonic-gate INFO_GATE(info)->ss_family); 11760Sstevel@tonic-gate k->k_metric = HOPCNT_INFINITY; 11770Sstevel@tonic-gate } else { 11780Sstevel@tonic-gate k->k_gate = S_ADDR(INFO_GATE(info)); 11790Sstevel@tonic-gate k->k_metric = rtm->rtm_rmx.rmx_hopcount; 11800Sstevel@tonic-gate if (k->k_metric < 0) 11810Sstevel@tonic-gate k->k_metric = 0; 11820Sstevel@tonic-gate else if (k->k_metric > HOPCNT_INFINITY-1) 11830Sstevel@tonic-gate k->k_metric = HOPCNT_INFINITY-1; 11840Sstevel@tonic-gate } 11850Sstevel@tonic-gate 11860Sstevel@tonic-gate if ((k->k_state & KS_NEW) && interf_route) { 11870Sstevel@tonic-gate if (k->k_gate != 0 && findifaddr(k->k_gate) == NULL) 11880Sstevel@tonic-gate k->k_state |= KS_DEPRE_IF; 11890Sstevel@tonic-gate else 11900Sstevel@tonic-gate k->k_state |= KS_IF; 11910Sstevel@tonic-gate } 11920Sstevel@tonic-gate 11930Sstevel@tonic-gate k->k_state &= ~(KS_NEW | KS_DELETE | KS_ADD | KS_CHANGE | KS_DEL_ADD | 11940Sstevel@tonic-gate KS_STATIC | KS_GATEWAY | KS_DELETED | KS_PRIVATE | KS_CHECK); 11950Sstevel@tonic-gate if (rtm->rtm_flags & RTF_GATEWAY) 11960Sstevel@tonic-gate k->k_state |= KS_GATEWAY; 11970Sstevel@tonic-gate if (rtm->rtm_flags & RTF_STATIC) 11980Sstevel@tonic-gate k->k_state |= KS_STATIC; 11990Sstevel@tonic-gate if (rtm->rtm_flags & RTF_PRIVATE) 12000Sstevel@tonic-gate k->k_state |= KS_PRIVATE; 12010Sstevel@tonic-gate 12020Sstevel@tonic-gate 12030Sstevel@tonic-gate if (rtm->rtm_flags & (RTF_DYNAMIC | RTF_MODIFIED)) { 12040Sstevel@tonic-gate if (INFO_AUTHOR(info) != 0 && 12050Sstevel@tonic-gate INFO_AUTHOR(info)->ss_family == AF_INET) 12060Sstevel@tonic-gate ifp = iflookup(S_ADDR(INFO_AUTHOR(info))); 12070Sstevel@tonic-gate else 12080Sstevel@tonic-gate ifp = NULL; 12090Sstevel@tonic-gate if (should_supply(ifp) && (ifp == NULL || 12100Sstevel@tonic-gate !(ifp->int_state & IS_REDIRECT_OK))) { 12110Sstevel@tonic-gate /* 12120Sstevel@tonic-gate * Routers are not supposed to listen to redirects, 12130Sstevel@tonic-gate * so delete it if it came via an unknown interface 12140Sstevel@tonic-gate * or the interface does not have special permission. 12150Sstevel@tonic-gate */ 12160Sstevel@tonic-gate k->k_state &= ~KS_DYNAMIC; 12170Sstevel@tonic-gate k->k_state |= KS_DELETE; 12180Sstevel@tonic-gate LIM_SEC(need_kern, 0); 12190Sstevel@tonic-gate trace_act("mark for deletion redirected %s --> %s" 12200Sstevel@tonic-gate " via %s", 12210Sstevel@tonic-gate addrname(k->k_dst, k->k_mask, 0), 12220Sstevel@tonic-gate naddr_ntoa(k->k_gate), 12230Sstevel@tonic-gate ifp ? ifp->int_name : "unknown interface"); 12240Sstevel@tonic-gate } else { 12250Sstevel@tonic-gate k->k_state |= KS_DYNAMIC; 12260Sstevel@tonic-gate k->k_redirect_time = now.tv_sec; 12270Sstevel@tonic-gate trace_act("accept redirected %s --> %s via %s", 12280Sstevel@tonic-gate addrname(k->k_dst, k->k_mask, 0), 12290Sstevel@tonic-gate naddr_ntoa(k->k_gate), 12300Sstevel@tonic-gate ifp ? ifp->int_name : "unknown interface"); 12310Sstevel@tonic-gate } 12320Sstevel@tonic-gate return; 12330Sstevel@tonic-gate } 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate /* 12360Sstevel@tonic-gate * If it is not a static route, quit until the next comparison 12370Sstevel@tonic-gate * between the kernel and daemon tables, when it will be deleted. 12380Sstevel@tonic-gate */ 12390Sstevel@tonic-gate if (!(k->k_state & KS_STATIC)) { 12400Sstevel@tonic-gate if (!(k->k_state & (KS_IF|KS_DEPRE_IF|KS_FILE))) 12410Sstevel@tonic-gate k->k_state |= KS_DELETE; 12420Sstevel@tonic-gate LIM_SEC(need_kern, k->k_keep); 12430Sstevel@tonic-gate return; 12440Sstevel@tonic-gate } 12450Sstevel@tonic-gate 12460Sstevel@tonic-gate /* 12470Sstevel@tonic-gate * Put static routes with real metrics into the daemon table so 12480Sstevel@tonic-gate * they can be advertised. 12490Sstevel@tonic-gate */ 12500Sstevel@tonic-gate 12510Sstevel@tonic-gate kern_check_static(k, ifp); 12520Sstevel@tonic-gate } 12530Sstevel@tonic-gate 12540Sstevel@tonic-gate 12550Sstevel@tonic-gate /* deal with packet loss */ 12560Sstevel@tonic-gate static void 12570Sstevel@tonic-gate rtm_lose(struct rt_msghdr *rtm, struct rt_addrinfo *info) 12580Sstevel@tonic-gate { 12590Sstevel@tonic-gate if (INFO_GATE(info) == NULL || INFO_GATE(info)->ss_family != AF_INET) { 12600Sstevel@tonic-gate trace_act("ignore %s without gateway", 12610Sstevel@tonic-gate rtm_type_name(rtm->rtm_type)); 12620Sstevel@tonic-gate age(0); 12630Sstevel@tonic-gate return; 12640Sstevel@tonic-gate } 12650Sstevel@tonic-gate 12660Sstevel@tonic-gate if (rdisc_ok) 12670Sstevel@tonic-gate rdisc_age(S_ADDR(INFO_GATE(info))); 12680Sstevel@tonic-gate age(S_ADDR(INFO_GATE(info))); 12690Sstevel@tonic-gate } 12700Sstevel@tonic-gate 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate /* 12730Sstevel@tonic-gate * Make the gateway slot of an info structure point to something 12740Sstevel@tonic-gate * useful. If it is not already useful, but it specifies an interface, 12750Sstevel@tonic-gate * then fill in the sockaddr_in provided and point it there. 12760Sstevel@tonic-gate */ 12770Sstevel@tonic-gate static int 12780Sstevel@tonic-gate get_info_gate(struct sockaddr_storage **ssp, struct sockaddr_in *sin) 12790Sstevel@tonic-gate { 12800Sstevel@tonic-gate struct sockaddr_dl *sdl = (struct sockaddr_dl *)*ssp; 12810Sstevel@tonic-gate struct interface *ifp; 12820Sstevel@tonic-gate 12830Sstevel@tonic-gate if (sdl == NULL) 12840Sstevel@tonic-gate return (0); 12850Sstevel@tonic-gate if ((sdl)->sdl_family == AF_INET) 12860Sstevel@tonic-gate return (1); 12870Sstevel@tonic-gate if ((sdl)->sdl_family != AF_LINK) 12880Sstevel@tonic-gate return (0); 12890Sstevel@tonic-gate 12900Sstevel@tonic-gate ifp = ifwithindex(sdl->sdl_index, _B_TRUE); 12910Sstevel@tonic-gate if (ifp == NULL) 12920Sstevel@tonic-gate return (0); 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate sin->sin_addr.s_addr = ifp->int_addr; 12950Sstevel@tonic-gate sin->sin_family = AF_INET; 12960Sstevel@tonic-gate /* LINTED */ 12970Sstevel@tonic-gate *ssp = (struct sockaddr_storage *)sin; 12980Sstevel@tonic-gate 12990Sstevel@tonic-gate return (1); 13000Sstevel@tonic-gate } 13010Sstevel@tonic-gate 13020Sstevel@tonic-gate 13030Sstevel@tonic-gate /* 13040Sstevel@tonic-gate * Clean the kernel table by copying it to the daemon image. 13050Sstevel@tonic-gate * Eventually the daemon will delete any extra routes. 13060Sstevel@tonic-gate */ 13070Sstevel@tonic-gate void 13080Sstevel@tonic-gate sync_kern(void) 13090Sstevel@tonic-gate { 13100Sstevel@tonic-gate int i; 13110Sstevel@tonic-gate struct khash *k; 13120Sstevel@tonic-gate struct { 13130Sstevel@tonic-gate struct T_optmgmt_req req; 13140Sstevel@tonic-gate struct opthdr hdr; 13150Sstevel@tonic-gate } req; 13160Sstevel@tonic-gate union { 13170Sstevel@tonic-gate struct T_optmgmt_ack ack; 13180Sstevel@tonic-gate unsigned char space[64]; 13190Sstevel@tonic-gate } ack; 13200Sstevel@tonic-gate struct opthdr *rh; 13210Sstevel@tonic-gate struct strbuf cbuf, dbuf; 13220Sstevel@tonic-gate int ipfd, nroutes, flags, r; 13230Sstevel@tonic-gate mib2_ipRouteEntry_t routes[8]; 13240Sstevel@tonic-gate mib2_ipRouteEntry_t *rp; 13250Sstevel@tonic-gate struct rt_msghdr rtm; 13260Sstevel@tonic-gate struct rt_addrinfo info; 13270Sstevel@tonic-gate struct sockaddr_in sin_dst; 13280Sstevel@tonic-gate struct sockaddr_in sin_gate; 13290Sstevel@tonic-gate struct sockaddr_in sin_mask; 13300Sstevel@tonic-gate struct sockaddr_in sin_author; 13310Sstevel@tonic-gate struct interface *ifp; 13320Sstevel@tonic-gate char ifname[LIFNAMSIZ + 1]; 13330Sstevel@tonic-gate 13340Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 13350Sstevel@tonic-gate for (k = khash_bins[i]; k != NULL; k = k->k_next) { 13360Sstevel@tonic-gate if (!(k->k_state & (KS_IF|KS_DEPRE_IF))) 13370Sstevel@tonic-gate k->k_state |= KS_CHECK; 13380Sstevel@tonic-gate } 13390Sstevel@tonic-gate } 13400Sstevel@tonic-gate 13410Sstevel@tonic-gate ipfd = open(IP_DEV_NAME, O_RDWR); 13420Sstevel@tonic-gate if (ipfd == -1) { 13430Sstevel@tonic-gate msglog("open " IP_DEV_NAME ": %s", rip_strerror(errno)); 13440Sstevel@tonic-gate goto hash_clean; 13450Sstevel@tonic-gate } 13460Sstevel@tonic-gate 13470Sstevel@tonic-gate req.req.PRIM_type = T_OPTMGMT_REQ; 13480Sstevel@tonic-gate req.req.OPT_offset = (caddr_t)&req.hdr - (caddr_t)&req; 13490Sstevel@tonic-gate req.req.OPT_length = sizeof (req.hdr); 13500Sstevel@tonic-gate req.req.MGMT_flags = T_CURRENT; 13510Sstevel@tonic-gate 13520Sstevel@tonic-gate req.hdr.level = MIB2_IP; 13530Sstevel@tonic-gate req.hdr.name = 0; 13540Sstevel@tonic-gate req.hdr.len = 0; 13550Sstevel@tonic-gate 13560Sstevel@tonic-gate cbuf.buf = (caddr_t)&req; 13570Sstevel@tonic-gate cbuf.len = sizeof (req); 13580Sstevel@tonic-gate 13590Sstevel@tonic-gate if (putmsg(ipfd, &cbuf, NULL, 0) == -1) { 13600Sstevel@tonic-gate msglog("T_OPTMGMT_REQ putmsg: %s", rip_strerror(errno)); 13610Sstevel@tonic-gate goto hash_clean; 13620Sstevel@tonic-gate } 13630Sstevel@tonic-gate 13640Sstevel@tonic-gate for (;;) { 13650Sstevel@tonic-gate cbuf.buf = (caddr_t)&ack; 13660Sstevel@tonic-gate cbuf.maxlen = sizeof (ack); 13670Sstevel@tonic-gate dbuf.buf = (caddr_t)routes; 13680Sstevel@tonic-gate dbuf.maxlen = sizeof (routes); 13690Sstevel@tonic-gate flags = 0; 13700Sstevel@tonic-gate r = getmsg(ipfd, &cbuf, &dbuf, &flags); 13710Sstevel@tonic-gate if (r == -1) { 13720Sstevel@tonic-gate msglog("T_OPTMGMT_REQ getmsg: %s", rip_strerror(errno)); 13730Sstevel@tonic-gate goto hash_clean; 13740Sstevel@tonic-gate } 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate if (cbuf.len < sizeof (struct T_optmgmt_ack) || 13770Sstevel@tonic-gate ack.ack.PRIM_type != T_OPTMGMT_ACK || 13780Sstevel@tonic-gate ack.ack.MGMT_flags != T_SUCCESS || 13790Sstevel@tonic-gate ack.ack.OPT_length < sizeof (struct opthdr)) { 13800Sstevel@tonic-gate msglog("bad T_OPTMGMT response; len=%d prim=%d " 13810Sstevel@tonic-gate "flags=%d optlen=%d", cbuf.len, ack.ack.PRIM_type, 13820Sstevel@tonic-gate ack.ack.MGMT_flags, ack.ack.OPT_length); 13830Sstevel@tonic-gate goto hash_clean; 13840Sstevel@tonic-gate } 13850Sstevel@tonic-gate /* LINTED */ 13860Sstevel@tonic-gate rh = (struct opthdr *)((caddr_t)&ack + ack.ack.OPT_offset); 13870Sstevel@tonic-gate if (rh->level == 0 && rh->name == 0) { 13880Sstevel@tonic-gate break; 13890Sstevel@tonic-gate } 13900Sstevel@tonic-gate if (rh->level != MIB2_IP || rh->name != MIB2_IP_21) { 13910Sstevel@tonic-gate while (r == MOREDATA) { 13920Sstevel@tonic-gate r = getmsg(ipfd, NULL, &dbuf, &flags); 13930Sstevel@tonic-gate } 13940Sstevel@tonic-gate continue; 13950Sstevel@tonic-gate } 13960Sstevel@tonic-gate break; 13970Sstevel@tonic-gate } 13980Sstevel@tonic-gate 13990Sstevel@tonic-gate (void) memset(&rtm, 0, sizeof (rtm)); 14000Sstevel@tonic-gate (void) memset(&info, 0, sizeof (info)); 14010Sstevel@tonic-gate (void) memset(&sin_dst, 0, sizeof (sin_dst)); 14020Sstevel@tonic-gate (void) memset(&sin_gate, 0, sizeof (sin_gate)); 14030Sstevel@tonic-gate (void) memset(&sin_mask, 0, sizeof (sin_mask)); 14040Sstevel@tonic-gate (void) memset(&sin_author, 0, sizeof (sin_author)); 14050Sstevel@tonic-gate sin_dst.sin_family = AF_INET; 14060Sstevel@tonic-gate /* LINTED */ 14070Sstevel@tonic-gate info.rti_info[RTAX_DST] = (struct sockaddr_storage *)&sin_dst; 14080Sstevel@tonic-gate sin_gate.sin_family = AF_INET; 14090Sstevel@tonic-gate /* LINTED */ 14100Sstevel@tonic-gate info.rti_info[RTAX_GATEWAY] = (struct sockaddr_storage *)&sin_gate; 14110Sstevel@tonic-gate sin_mask.sin_family = AF_INET; 14120Sstevel@tonic-gate /* LINTED */ 14130Sstevel@tonic-gate info.rti_info[RTAX_NETMASK] = (struct sockaddr_storage *)&sin_mask; 14140Sstevel@tonic-gate sin_dst.sin_family = AF_INET; 14150Sstevel@tonic-gate /* LINTED */ 14160Sstevel@tonic-gate info.rti_info[RTAX_AUTHOR] = (struct sockaddr_storage *)&sin_author; 14170Sstevel@tonic-gate 14180Sstevel@tonic-gate for (;;) { 14190Sstevel@tonic-gate nroutes = dbuf.len / sizeof (mib2_ipRouteEntry_t); 14200Sstevel@tonic-gate for (rp = routes; nroutes > 0; ++rp, nroutes--) { 14210Sstevel@tonic-gate 14220Sstevel@tonic-gate /* 14230Sstevel@tonic-gate * Ignore IRE cache, broadcast, and local address 14240Sstevel@tonic-gate * entries; they're not subject to routing socket 14250Sstevel@tonic-gate * control. 14260Sstevel@tonic-gate */ 14270Sstevel@tonic-gate if (rp->ipRouteInfo.re_ire_type & 14280Sstevel@tonic-gate (IRE_BROADCAST | IRE_CACHE | IRE_LOCAL)) 14290Sstevel@tonic-gate continue; 14300Sstevel@tonic-gate 14310Sstevel@tonic-gate /* ignore multicast addresses */ 14320Sstevel@tonic-gate if (IN_MULTICAST(ntohl(rp->ipRouteDest))) 14330Sstevel@tonic-gate continue; 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate 14360Sstevel@tonic-gate #ifdef DEBUG_KERNEL_ROUTE_READ 14370Sstevel@tonic-gate (void) fprintf(stderr, "route type %d, ire type %08X, " 14380Sstevel@tonic-gate "flags %08X: %s", rp->ipRouteType, 14390Sstevel@tonic-gate rp->ipRouteInfo.re_ire_type, 14400Sstevel@tonic-gate rp->ipRouteInfo.re_flags, 14410Sstevel@tonic-gate naddr_ntoa(rp->ipRouteDest)); 14420Sstevel@tonic-gate (void) fprintf(stderr, " %s", 14430Sstevel@tonic-gate naddr_ntoa(rp->ipRouteMask)); 14440Sstevel@tonic-gate (void) fprintf(stderr, " %s\n", 14450Sstevel@tonic-gate naddr_ntoa(rp->ipRouteNextHop)); 14460Sstevel@tonic-gate #endif 14470Sstevel@tonic-gate 14480Sstevel@tonic-gate /* Fake up the needed entries */ 14490Sstevel@tonic-gate rtm.rtm_flags = rp->ipRouteInfo.re_flags; 14500Sstevel@tonic-gate rtm.rtm_type = RTM_GET; 14510Sstevel@tonic-gate rtm.rtm_rmx.rmx_hopcount = rp->ipRouteMetric1; 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate (void) memset(ifname, 0, sizeof (ifname)); 14540Sstevel@tonic-gate if (rp->ipRouteIfIndex.o_length < 14550Sstevel@tonic-gate sizeof (rp->ipRouteIfIndex.o_bytes)) 14560Sstevel@tonic-gate rp->ipRouteIfIndex.o_bytes[ 14570Sstevel@tonic-gate rp->ipRouteIfIndex.o_length] = '\0'; 14580Sstevel@tonic-gate (void) strncpy(ifname, 14590Sstevel@tonic-gate rp->ipRouteIfIndex.o_bytes, 14600Sstevel@tonic-gate sizeof (ifname)); 14610Sstevel@tonic-gate 14620Sstevel@tonic-gate /* 14630Sstevel@tonic-gate * First try to match up on gwkludge entries 14640Sstevel@tonic-gate * before trying to match ifp by name. 14650Sstevel@tonic-gate */ 14660Sstevel@tonic-gate if ((ifp = gwkludge_iflookup(rp->ipRouteDest, 1467551Sbw rp->ipRouteNextHop, 1468*2781Ssowmini ntohl(rp->ipRouteMask))) == NULL) { 14690Sstevel@tonic-gate ifp = ifwithname(ifname); 1470*2781Ssowmini if (ifp != NULL && ifp->int_phys != NULL) { 1471*2781Ssowmini ifp = ifwithname( 1472*2781Ssowmini ifp->int_phys->phyi_name); 1473*2781Ssowmini } 1474*2781Ssowmini } 14750Sstevel@tonic-gate 14760Sstevel@tonic-gate info.rti_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; 14770Sstevel@tonic-gate if (rp->ipRouteInfo.re_ire_type & IRE_HOST_REDIRECT) 14780Sstevel@tonic-gate info.rti_addrs |= RTA_AUTHOR; 14790Sstevel@tonic-gate sin_dst.sin_addr.s_addr = rp->ipRouteDest; 14800Sstevel@tonic-gate sin_gate.sin_addr.s_addr = rp->ipRouteNextHop; 14810Sstevel@tonic-gate sin_mask.sin_addr.s_addr = rp->ipRouteMask; 14820Sstevel@tonic-gate sin_author.sin_addr.s_addr = 14830Sstevel@tonic-gate rp->ipRouteInfo.re_src_addr; 14840Sstevel@tonic-gate 14850Sstevel@tonic-gate /* 14860Sstevel@tonic-gate * Note static routes and interface routes, and also 14870Sstevel@tonic-gate * preload the image of the kernel table so that 14880Sstevel@tonic-gate * we can later clean it, as well as avoid making 14890Sstevel@tonic-gate * unneeded changes. Keep the old kernel routes for a 14900Sstevel@tonic-gate * few seconds to allow a RIP or router-discovery 14910Sstevel@tonic-gate * response to be heard. 14920Sstevel@tonic-gate */ 14930Sstevel@tonic-gate rtm_add(&rtm, &info, MAX_WAITTIME, 14940Sstevel@tonic-gate ((rp->ipRouteInfo.re_ire_type & 14950Sstevel@tonic-gate (IRE_INTERFACE|IRE_LOOPBACK)) != 0), ifp); 14960Sstevel@tonic-gate } 14970Sstevel@tonic-gate if (r == 0) { 14980Sstevel@tonic-gate break; 14990Sstevel@tonic-gate } 15000Sstevel@tonic-gate r = getmsg(ipfd, NULL, &dbuf, &flags); 15010Sstevel@tonic-gate } 15020Sstevel@tonic-gate 15030Sstevel@tonic-gate hash_clean: 15040Sstevel@tonic-gate if (ipfd != -1) 15050Sstevel@tonic-gate (void) close(ipfd); 15060Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 15070Sstevel@tonic-gate for (k = khash_bins[i]; k != NULL; k = k->k_next) { 15080Sstevel@tonic-gate 15090Sstevel@tonic-gate /* 15100Sstevel@tonic-gate * KS_DELETED routes have been removed from the 15110Sstevel@tonic-gate * kernel, but we keep them around for reasons 15120Sstevel@tonic-gate * stated in del_static(), so we skip the check 15130Sstevel@tonic-gate * for KS_DELETED routes here. 15140Sstevel@tonic-gate */ 15150Sstevel@tonic-gate if ((k->k_state & (KS_CHECK|KS_DELETED)) == KS_CHECK) { 15160Sstevel@tonic-gate 15170Sstevel@tonic-gate if (!(k->k_state & KS_DYNAMIC)) 15180Sstevel@tonic-gate writelog(LOG_WARNING, 15190Sstevel@tonic-gate "%s --> %s disappeared from kernel", 15200Sstevel@tonic-gate addrname(k->k_dst, k->k_mask, 0), 15210Sstevel@tonic-gate naddr_ntoa(k->k_gate)); 15220Sstevel@tonic-gate del_static(k->k_dst, k->k_mask, k->k_gate, 15230Sstevel@tonic-gate k->k_ifp, 1); 15240Sstevel@tonic-gate 15250Sstevel@tonic-gate } 15260Sstevel@tonic-gate } 15270Sstevel@tonic-gate } 15280Sstevel@tonic-gate } 15290Sstevel@tonic-gate 15300Sstevel@tonic-gate 15310Sstevel@tonic-gate /* Listen to announcements from the kernel */ 15320Sstevel@tonic-gate void 15330Sstevel@tonic-gate read_rt(void) 15340Sstevel@tonic-gate { 15350Sstevel@tonic-gate long cc; 15360Sstevel@tonic-gate struct interface *ifp; 15370Sstevel@tonic-gate struct sockaddr_in gate_sin; 15380Sstevel@tonic-gate in_addr_t mask, gate; 15390Sstevel@tonic-gate union { 15400Sstevel@tonic-gate struct { 15410Sstevel@tonic-gate struct rt_msghdr rtm; 15420Sstevel@tonic-gate struct sockaddr_storage addrs[RTA_NUMBITS]; 15430Sstevel@tonic-gate } r; 15440Sstevel@tonic-gate struct if_msghdr ifm; 15450Sstevel@tonic-gate } m; 15460Sstevel@tonic-gate char str[100], *strp; 15470Sstevel@tonic-gate struct rt_addrinfo info; 15480Sstevel@tonic-gate 15490Sstevel@tonic-gate 15500Sstevel@tonic-gate for (;;) { 15510Sstevel@tonic-gate cc = read(rt_sock, &m, sizeof (m)); 15520Sstevel@tonic-gate if (cc <= 0) { 15530Sstevel@tonic-gate if (cc < 0 && errno != EWOULDBLOCK) 15540Sstevel@tonic-gate LOGERR("read(rt_sock)"); 15550Sstevel@tonic-gate return; 15560Sstevel@tonic-gate } 15570Sstevel@tonic-gate 15580Sstevel@tonic-gate if (TRACERTS) 15590Sstevel@tonic-gate dump_rt_msg("read", &m.r.rtm, cc); 15600Sstevel@tonic-gate 15610Sstevel@tonic-gate if (cc < m.r.rtm.rtm_msglen) { 15620Sstevel@tonic-gate msglog("routing message truncated (%d < %d)", 15630Sstevel@tonic-gate cc, m.r.rtm.rtm_msglen); 15640Sstevel@tonic-gate } 15650Sstevel@tonic-gate 15660Sstevel@tonic-gate if (m.r.rtm.rtm_version != RTM_VERSION) { 15670Sstevel@tonic-gate msglog("bogus routing message version %d", 15680Sstevel@tonic-gate m.r.rtm.rtm_version); 15690Sstevel@tonic-gate continue; 15700Sstevel@tonic-gate } 15710Sstevel@tonic-gate 15720Sstevel@tonic-gate ifp = NULL; 15730Sstevel@tonic-gate 15740Sstevel@tonic-gate if (m.r.rtm.rtm_type == RTM_IFINFO || 15750Sstevel@tonic-gate m.r.rtm.rtm_type == RTM_NEWADDR || 15760Sstevel@tonic-gate m.r.rtm.rtm_type == RTM_DELADDR) { 15770Sstevel@tonic-gate strp = if_bit_string(m.ifm.ifm_flags, _B_TRUE); 15780Sstevel@tonic-gate if (strp == NULL) { 15790Sstevel@tonic-gate strp = str; 15800Sstevel@tonic-gate (void) sprintf(str, "%#x", m.ifm.ifm_flags); 15810Sstevel@tonic-gate } 15820Sstevel@tonic-gate ifp = ifwithindex(m.ifm.ifm_index, 15830Sstevel@tonic-gate m.r.rtm.rtm_type != RTM_DELADDR); 15840Sstevel@tonic-gate if (ifp == NULL) { 15850Sstevel@tonic-gate char ifname[LIFNAMSIZ], *ifnamep; 15860Sstevel@tonic-gate 15870Sstevel@tonic-gate ifnamep = if_indextoname(m.ifm.ifm_index, 15880Sstevel@tonic-gate ifname); 15890Sstevel@tonic-gate if (ifnamep == NULL) { 15900Sstevel@tonic-gate trace_act("note %s with flags %s" 15910Sstevel@tonic-gate " for unknown interface index #%d", 15920Sstevel@tonic-gate rtm_type_name(m.r.rtm.rtm_type), 15930Sstevel@tonic-gate strp, m.ifm.ifm_index); 15940Sstevel@tonic-gate } else { 15950Sstevel@tonic-gate trace_act("note %s with flags %s" 15960Sstevel@tonic-gate " for unknown interface %s", 15970Sstevel@tonic-gate rtm_type_name(m.r.rtm.rtm_type), 15980Sstevel@tonic-gate strp, ifnamep); 15990Sstevel@tonic-gate } 16000Sstevel@tonic-gate } else { 16010Sstevel@tonic-gate trace_act("note %s with flags %s for %s", 16020Sstevel@tonic-gate rtm_type_name(m.r.rtm.rtm_type), 16030Sstevel@tonic-gate strp, ifp->int_name); 16040Sstevel@tonic-gate } 16050Sstevel@tonic-gate if (strp != str) 16060Sstevel@tonic-gate free(strp); 16070Sstevel@tonic-gate 16080Sstevel@tonic-gate /* 16090Sstevel@tonic-gate * After being informed of a change to an interface, 16100Sstevel@tonic-gate * check them all now if the check would otherwise 16110Sstevel@tonic-gate * be a long time from now, if the interface is 16120Sstevel@tonic-gate * not known, or if the interface has been turned 16130Sstevel@tonic-gate * off or on. 16140Sstevel@tonic-gate */ 16150Sstevel@tonic-gate if (ifscan_timer.tv_sec-now.tv_sec >= 16160Sstevel@tonic-gate CHECK_BAD_INTERVAL || ifp == NULL || 16170Sstevel@tonic-gate ((ifp->int_if_flags ^ m.ifm.ifm_flags) & 16180Sstevel@tonic-gate IFF_UP) != 0) 16190Sstevel@tonic-gate ifscan_timer.tv_sec = now.tv_sec; 16200Sstevel@tonic-gate continue; 16210Sstevel@tonic-gate } else { 16220Sstevel@tonic-gate if (m.r.rtm.rtm_index != 0) 16230Sstevel@tonic-gate ifp = ifwithindex(m.r.rtm.rtm_index, 1); 16240Sstevel@tonic-gate } 16250Sstevel@tonic-gate 16260Sstevel@tonic-gate (void) strlcpy(str, rtm_type_name(m.r.rtm.rtm_type), 16270Sstevel@tonic-gate sizeof (str)); 16280Sstevel@tonic-gate strp = &str[strlen(str)]; 16290Sstevel@tonic-gate if (m.r.rtm.rtm_type <= RTM_CHANGE) 16300Sstevel@tonic-gate strp += snprintf(strp, sizeof (str) - (strp - str), 16310Sstevel@tonic-gate " from pid %d", (int)m.r.rtm.rtm_pid); 16320Sstevel@tonic-gate 16330Sstevel@tonic-gate /* LINTED */ 16340Sstevel@tonic-gate (void) rt_xaddrs(&info, (struct sockaddr_storage *)(&m.r.rtm + 16350Sstevel@tonic-gate 1), (char *)&m + cc, m.r.rtm.rtm_addrs); 16360Sstevel@tonic-gate 16370Sstevel@tonic-gate if (INFO_DST(&info) == 0) { 16380Sstevel@tonic-gate trace_act("ignore %s without dst", str); 16390Sstevel@tonic-gate continue; 16400Sstevel@tonic-gate } 16410Sstevel@tonic-gate 16420Sstevel@tonic-gate if (INFO_DST(&info)->ss_family != AF_INET) { 16430Sstevel@tonic-gate trace_act("ignore %s for AF %d", str, 16440Sstevel@tonic-gate INFO_DST(&info)->ss_family); 16450Sstevel@tonic-gate continue; 16460Sstevel@tonic-gate } 16470Sstevel@tonic-gate 16480Sstevel@tonic-gate mask = ((INFO_MASK(&info) != 0) ? 16490Sstevel@tonic-gate ntohl(S_ADDR(INFO_MASK(&info))) : 16500Sstevel@tonic-gate (m.r.rtm.rtm_flags & RTF_HOST) ? 16510Sstevel@tonic-gate HOST_MASK : std_mask(S_ADDR(INFO_DST(&info)))); 16520Sstevel@tonic-gate 16530Sstevel@tonic-gate strp += snprintf(strp, sizeof (str) - (strp - str), ": %s", 16540Sstevel@tonic-gate addrname(S_ADDR(INFO_DST(&info)), mask, 0)); 16550Sstevel@tonic-gate 16560Sstevel@tonic-gate if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info))))) { 16570Sstevel@tonic-gate trace_act("ignore multicast %s", str); 16580Sstevel@tonic-gate continue; 16590Sstevel@tonic-gate } 16600Sstevel@tonic-gate 16610Sstevel@tonic-gate if (m.r.rtm.rtm_flags & RTF_LLINFO) { 16620Sstevel@tonic-gate trace_act("ignore ARP %s", str); 16630Sstevel@tonic-gate continue; 16640Sstevel@tonic-gate } 16650Sstevel@tonic-gate 16660Sstevel@tonic-gate if (get_info_gate(&INFO_GATE(&info), &gate_sin)) { 16670Sstevel@tonic-gate gate = S_ADDR(INFO_GATE(&info)); 16680Sstevel@tonic-gate strp += snprintf(strp, sizeof (str) - (strp - str), 16690Sstevel@tonic-gate " --> %s", naddr_ntoa(gate)); 16700Sstevel@tonic-gate } else { 16710Sstevel@tonic-gate gate = 0; 16720Sstevel@tonic-gate } 16730Sstevel@tonic-gate 16740Sstevel@tonic-gate if (INFO_AUTHOR(&info) != 0) 16750Sstevel@tonic-gate strp += snprintf(strp, sizeof (str) - (strp - str), 16760Sstevel@tonic-gate " by authority of %s", 16770Sstevel@tonic-gate saddr_ntoa(INFO_AUTHOR(&info))); 16780Sstevel@tonic-gate 16790Sstevel@tonic-gate switch (m.r.rtm.rtm_type) { 16800Sstevel@tonic-gate case RTM_ADD: 16810Sstevel@tonic-gate case RTM_CHANGE: 16820Sstevel@tonic-gate case RTM_REDIRECT: 16830Sstevel@tonic-gate if (m.r.rtm.rtm_errno != 0) { 16840Sstevel@tonic-gate trace_act("ignore %s with \"%s\" error", 16850Sstevel@tonic-gate str, rip_strerror(m.r.rtm.rtm_errno)); 16860Sstevel@tonic-gate } else { 16870Sstevel@tonic-gate trace_act("%s", str); 16880Sstevel@tonic-gate rtm_add(&m.r.rtm, &info, 0, 16890Sstevel@tonic-gate !(m.r.rtm.rtm_flags & RTF_GATEWAY) && 16900Sstevel@tonic-gate m.r.rtm.rtm_type != RTM_REDIRECT, ifp); 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate } 16930Sstevel@tonic-gate break; 16940Sstevel@tonic-gate 16950Sstevel@tonic-gate case RTM_DELETE: 16960Sstevel@tonic-gate if (m.r.rtm.rtm_errno != 0 && 16970Sstevel@tonic-gate m.r.rtm.rtm_errno != ESRCH) { 16980Sstevel@tonic-gate trace_act("ignore %s with \"%s\" error", 16990Sstevel@tonic-gate str, rip_strerror(m.r.rtm.rtm_errno)); 17000Sstevel@tonic-gate } else { 17010Sstevel@tonic-gate trace_act("%s", str); 17020Sstevel@tonic-gate del_static(S_ADDR(INFO_DST(&info)), mask, 17030Sstevel@tonic-gate gate, ifp, 1); 17040Sstevel@tonic-gate } 17050Sstevel@tonic-gate break; 17060Sstevel@tonic-gate 17070Sstevel@tonic-gate case RTM_LOSING: 17080Sstevel@tonic-gate trace_act("%s", str); 17090Sstevel@tonic-gate rtm_lose(&m.r.rtm, &info); 17100Sstevel@tonic-gate break; 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate default: 17130Sstevel@tonic-gate trace_act("ignore %s", str); 17140Sstevel@tonic-gate break; 17150Sstevel@tonic-gate } 17160Sstevel@tonic-gate } 17170Sstevel@tonic-gate } 17180Sstevel@tonic-gate 17190Sstevel@tonic-gate 17200Sstevel@tonic-gate /* 17210Sstevel@tonic-gate * Disassemble a routing message. The result is an array of pointers 17220Sstevel@tonic-gate * to sockaddr_storage structures stored in the info argument. 17230Sstevel@tonic-gate * 17240Sstevel@tonic-gate * ss is a pointer to the beginning of the data following the 17250Sstevel@tonic-gate * rt_msghdr contained in the routing socket message, which consists 17260Sstevel@tonic-gate * of a string of concatenated sockaddr structure of different types. 17271676Sjpk * 17281676Sjpk * Extended attributes can be appended at the end of the list. 17290Sstevel@tonic-gate */ 17300Sstevel@tonic-gate static int 17310Sstevel@tonic-gate rt_xaddrs(struct rt_addrinfo *info, 17320Sstevel@tonic-gate struct sockaddr_storage *ss, 17330Sstevel@tonic-gate char *lim, 17340Sstevel@tonic-gate int addrs) 17350Sstevel@tonic-gate { 17360Sstevel@tonic-gate int retv = 0; 17370Sstevel@tonic-gate int i; 17380Sstevel@tonic-gate int abit; 17390Sstevel@tonic-gate int complaints; 17400Sstevel@tonic-gate static int prev_complaints; 17410Sstevel@tonic-gate 17420Sstevel@tonic-gate #define XBAD_AF 0x1 17430Sstevel@tonic-gate #define XBAD_SHORT 0x2 17440Sstevel@tonic-gate #define XBAD_LONG 0x4 17450Sstevel@tonic-gate 17460Sstevel@tonic-gate (void) memset(info, 0, sizeof (*info)); 17470Sstevel@tonic-gate info->rti_addrs = addrs; 17480Sstevel@tonic-gate complaints = 0; 17490Sstevel@tonic-gate for (i = 0, abit = 1; i < RTAX_MAX && (char *)ss < lim; 17500Sstevel@tonic-gate i++, abit <<= 1) { 17510Sstevel@tonic-gate if ((addrs & abit) == 0) 17520Sstevel@tonic-gate continue; 17530Sstevel@tonic-gate info->rti_info[i] = ss; 17540Sstevel@tonic-gate /* Horrible interface here */ 17550Sstevel@tonic-gate switch (ss->ss_family) { 17560Sstevel@tonic-gate case AF_UNIX: 17570Sstevel@tonic-gate /* LINTED */ 17580Sstevel@tonic-gate ss = (struct sockaddr_storage *)( 17590Sstevel@tonic-gate (struct sockaddr_un *)ss + 1); 17600Sstevel@tonic-gate break; 17610Sstevel@tonic-gate case AF_INET: 17620Sstevel@tonic-gate /* LINTED */ 17630Sstevel@tonic-gate ss = (struct sockaddr_storage *)( 17640Sstevel@tonic-gate (struct sockaddr_in *)ss + 1); 17650Sstevel@tonic-gate break; 17660Sstevel@tonic-gate case AF_LINK: 17670Sstevel@tonic-gate /* LINTED */ 17680Sstevel@tonic-gate ss = (struct sockaddr_storage *)( 17690Sstevel@tonic-gate (struct sockaddr_dl *)ss + 1); 17700Sstevel@tonic-gate break; 17710Sstevel@tonic-gate case AF_INET6: 17720Sstevel@tonic-gate /* LINTED */ 17730Sstevel@tonic-gate ss = (struct sockaddr_storage *)( 17740Sstevel@tonic-gate (struct sockaddr_in6 *)ss + 1); 17750Sstevel@tonic-gate break; 17760Sstevel@tonic-gate default: 17770Sstevel@tonic-gate if (!(prev_complaints & XBAD_AF)) 17780Sstevel@tonic-gate writelog(LOG_WARNING, 17790Sstevel@tonic-gate "unknown address family %d " 17800Sstevel@tonic-gate "encountered", ss->ss_family); 17810Sstevel@tonic-gate if (complaints & XBAD_AF) 17820Sstevel@tonic-gate goto xaddr_done; 17830Sstevel@tonic-gate /* LINTED */ 17840Sstevel@tonic-gate ss = (struct sockaddr_storage *)( 17850Sstevel@tonic-gate (struct sockaddr *)ss + 1); 17860Sstevel@tonic-gate complaints |= XBAD_AF; 17870Sstevel@tonic-gate info->rti_addrs &= abit - 1; 17880Sstevel@tonic-gate addrs = info->rti_addrs; 17890Sstevel@tonic-gate retv = -1; 17900Sstevel@tonic-gate break; 17910Sstevel@tonic-gate } 17920Sstevel@tonic-gate if ((char *)ss > lim) { 17930Sstevel@tonic-gate if (!(prev_complaints & XBAD_SHORT)) 17940Sstevel@tonic-gate msglog("sockaddr %d too short by %d " 17950Sstevel@tonic-gate "bytes", i + 1, (char *)ss - lim); 17960Sstevel@tonic-gate complaints |= XBAD_SHORT; 17970Sstevel@tonic-gate info->rti_info[i] = NULL; 17980Sstevel@tonic-gate info->rti_addrs &= abit - 1; 17990Sstevel@tonic-gate retv = -1; 18000Sstevel@tonic-gate goto xaddr_done; 18010Sstevel@tonic-gate } 18020Sstevel@tonic-gate } 18031676Sjpk 18041676Sjpk while (((char *)ss + sizeof (rtm_ext_t)) <= lim) { 18051676Sjpk rtm_ext_t *tp; 18061676Sjpk char *nxt; 18071676Sjpk 18081676Sjpk /* LINTED: alignment */ 18091676Sjpk tp = (rtm_ext_t *)ss; 18101676Sjpk nxt = (char *)(tp + 1) + tp->rtmex_len; 18111676Sjpk 18121676Sjpk if (!IS_P2ALIGNED(tp->rtmex_len, sizeof (uint32_t)) || 18131676Sjpk nxt > lim) { 18141676Sjpk break; 18151676Sjpk } 18161676Sjpk 18171676Sjpk /* LINTED: alignment */ 18181676Sjpk ss = (struct sockaddr_storage *)nxt; 18191676Sjpk } 18201676Sjpk 18210Sstevel@tonic-gate if ((char *)ss != lim) { 18221676Sjpk if ((char *)ss > lim) { 18231676Sjpk if (!(prev_complaints & XBAD_SHORT)) 18241676Sjpk msglog("routing message too short by %d bytes", 18251676Sjpk (char *)ss - lim); 18261676Sjpk complaints |= XBAD_SHORT; 18271676Sjpk } else if (!(prev_complaints & XBAD_LONG)) { 18280Sstevel@tonic-gate msglog("%d bytes of routing message left over", 18290Sstevel@tonic-gate lim - (char *)ss); 18301676Sjpk complaints |= XBAD_LONG; 18311676Sjpk } 18320Sstevel@tonic-gate retv = -1; 18330Sstevel@tonic-gate } 18340Sstevel@tonic-gate xaddr_done: 18350Sstevel@tonic-gate prev_complaints = complaints; 18360Sstevel@tonic-gate return (retv); 18370Sstevel@tonic-gate } 18380Sstevel@tonic-gate 18390Sstevel@tonic-gate 18400Sstevel@tonic-gate /* after aggregating, note routes that belong in the kernel */ 18410Sstevel@tonic-gate static void 18420Sstevel@tonic-gate kern_out(struct ag_info *ag) 18430Sstevel@tonic-gate { 18440Sstevel@tonic-gate struct khash *k; 1845*2781Ssowmini struct interface *ifp; 1846*2781Ssowmini 1847*2781Ssowmini ifp = ag->ag_ifp; 1848*2781Ssowmini 1849*2781Ssowmini if (ifp != NULL && ifp->int_phys != NULL) { 1850*2781Ssowmini ifp = ifwithname(ifp->int_phys->phyi_name); 1851*2781Ssowmini } 18520Sstevel@tonic-gate 18530Sstevel@tonic-gate /* 18540Sstevel@tonic-gate * Do not install bad routes if they are not already present. 18550Sstevel@tonic-gate * This includes routes that had RS_NET_SYN for interfaces that 18560Sstevel@tonic-gate * recently died. 18570Sstevel@tonic-gate */ 18580Sstevel@tonic-gate if (ag->ag_metric == HOPCNT_INFINITY) { 18590Sstevel@tonic-gate k = kern_find(htonl(ag->ag_dst_h), ag->ag_mask, 18600Sstevel@tonic-gate ag->ag_nhop, ag->ag_ifp, NULL); 18610Sstevel@tonic-gate if (k == NULL) 18620Sstevel@tonic-gate return; 18630Sstevel@tonic-gate } else { 18640Sstevel@tonic-gate k = kern_add(htonl(ag->ag_dst_h), ag->ag_mask, ag->ag_nhop, 1865*2781Ssowmini ifp); 18660Sstevel@tonic-gate } 18670Sstevel@tonic-gate 18680Sstevel@tonic-gate if (k->k_state & KS_NEW) { 18690Sstevel@tonic-gate /* will need to add new entry to the kernel table */ 18700Sstevel@tonic-gate k->k_state = KS_ADD; 18710Sstevel@tonic-gate if (ag->ag_state & AGS_GATEWAY) 18720Sstevel@tonic-gate k->k_state |= KS_GATEWAY; 18730Sstevel@tonic-gate if (ag->ag_state & AGS_IF) 18740Sstevel@tonic-gate k->k_state |= KS_IF; 18750Sstevel@tonic-gate if (ag->ag_state & AGS_PASSIVE) 18760Sstevel@tonic-gate k->k_state |= KS_PASSIVE; 18770Sstevel@tonic-gate if (ag->ag_state & AGS_FILE) 18780Sstevel@tonic-gate k->k_state |= KS_FILE; 18790Sstevel@tonic-gate k->k_gate = ag->ag_nhop; 1880*2781Ssowmini k->k_ifp = ifp; 18810Sstevel@tonic-gate k->k_metric = ag->ag_metric; 18820Sstevel@tonic-gate return; 18830Sstevel@tonic-gate } 18840Sstevel@tonic-gate 18850Sstevel@tonic-gate if ((k->k_state & (KS_STATIC|KS_DEPRE_IF)) || 18860Sstevel@tonic-gate ((k->k_state & (KS_IF|KS_PASSIVE)) == KS_IF)) { 18870Sstevel@tonic-gate return; 18880Sstevel@tonic-gate } 18890Sstevel@tonic-gate 18900Sstevel@tonic-gate /* modify existing kernel entry if necessary */ 18910Sstevel@tonic-gate if (k->k_gate == ag->ag_nhop && k->k_ifp == ag->ag_ifp && 18920Sstevel@tonic-gate k->k_metric != ag->ag_metric) { 18930Sstevel@tonic-gate /* 18940Sstevel@tonic-gate * Must delete bad interface routes etc. 18950Sstevel@tonic-gate * to change them. 18960Sstevel@tonic-gate */ 18970Sstevel@tonic-gate if (k->k_metric == HOPCNT_INFINITY) 18980Sstevel@tonic-gate k->k_state |= KS_DEL_ADD; 18990Sstevel@tonic-gate k->k_gate = ag->ag_nhop; 19000Sstevel@tonic-gate k->k_metric = ag->ag_metric; 19010Sstevel@tonic-gate k->k_state |= KS_CHANGE; 19020Sstevel@tonic-gate } 19030Sstevel@tonic-gate 19040Sstevel@tonic-gate /* 19050Sstevel@tonic-gate * If the daemon thinks the route should exist, forget 19060Sstevel@tonic-gate * about any redirections. 19070Sstevel@tonic-gate * If the daemon thinks the route should exist, eventually 19080Sstevel@tonic-gate * override manual intervention by the operator. 19090Sstevel@tonic-gate */ 19100Sstevel@tonic-gate if ((k->k_state & (KS_DYNAMIC | KS_DELETED)) != 0) { 19110Sstevel@tonic-gate k->k_state &= ~KS_DYNAMIC; 19120Sstevel@tonic-gate k->k_state |= (KS_ADD | KS_DEL_ADD); 19130Sstevel@tonic-gate } 19140Sstevel@tonic-gate 19150Sstevel@tonic-gate if ((k->k_state & KS_GATEWAY) && !(ag->ag_state & AGS_GATEWAY)) { 19160Sstevel@tonic-gate k->k_state &= ~KS_GATEWAY; 19170Sstevel@tonic-gate k->k_state |= (KS_ADD | KS_DEL_ADD); 19180Sstevel@tonic-gate } else if (!(k->k_state & KS_GATEWAY) && (ag->ag_state & AGS_GATEWAY)) { 19190Sstevel@tonic-gate k->k_state |= KS_GATEWAY; 19200Sstevel@tonic-gate k->k_state |= (KS_ADD | KS_DEL_ADD); 19210Sstevel@tonic-gate } 19220Sstevel@tonic-gate 19230Sstevel@tonic-gate /* 19240Sstevel@tonic-gate * Deleting-and-adding is necessary to change aspects of a route. 19250Sstevel@tonic-gate * Just delete instead of deleting and then adding a bad route. 19260Sstevel@tonic-gate * Otherwise, we want to keep the route in the kernel. 19270Sstevel@tonic-gate */ 19280Sstevel@tonic-gate if (k->k_metric == HOPCNT_INFINITY && (k->k_state & KS_DEL_ADD)) 19290Sstevel@tonic-gate k->k_state |= KS_DELETE; 19300Sstevel@tonic-gate else 19310Sstevel@tonic-gate k->k_state &= ~KS_DELETE; 19320Sstevel@tonic-gate #undef RT 19330Sstevel@tonic-gate } 19340Sstevel@tonic-gate 19350Sstevel@tonic-gate /* 19360Sstevel@tonic-gate * Update our image of the kernel forwarding table using the given 19370Sstevel@tonic-gate * route from our internal routing table. 19380Sstevel@tonic-gate */ 19390Sstevel@tonic-gate 19400Sstevel@tonic-gate /*ARGSUSED1*/ 19410Sstevel@tonic-gate static int 19420Sstevel@tonic-gate walk_kern(struct radix_node *rn, void *argp) 19430Sstevel@tonic-gate { 19440Sstevel@tonic-gate #define RT ((struct rt_entry *)rn) 19450Sstevel@tonic-gate uint8_t metric, pref; 19460Sstevel@tonic-gate uint_t ags = 0; 19470Sstevel@tonic-gate int i; 19480Sstevel@tonic-gate struct rt_spare *rts; 19490Sstevel@tonic-gate 19500Sstevel@tonic-gate /* Do not install synthetic routes */ 19510Sstevel@tonic-gate if (RT->rt_state & RS_NET_SYN) 19520Sstevel@tonic-gate return (0); 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate /* 19550Sstevel@tonic-gate * Do not install static routes here. Only 19560Sstevel@tonic-gate * read_rt->rtm_add->kern_add should install those 19570Sstevel@tonic-gate */ 19580Sstevel@tonic-gate if ((RT->rt_state & RS_STATIC) && 19590Sstevel@tonic-gate (RT->rt_spares[0].rts_origin != RO_FILE)) 19600Sstevel@tonic-gate return (0); 19610Sstevel@tonic-gate 19620Sstevel@tonic-gate /* Do not clobber kernel if this is a route for a dead interface */ 19630Sstevel@tonic-gate if (RT->rt_state & RS_BADIF) 19640Sstevel@tonic-gate return (0); 19650Sstevel@tonic-gate 19660Sstevel@tonic-gate if (!(RT->rt_state & RS_IF)) { 19670Sstevel@tonic-gate /* This is an ordinary route, not for an interface. */ 19680Sstevel@tonic-gate 19690Sstevel@tonic-gate /* 19700Sstevel@tonic-gate * aggregate, ordinary good routes without regard to 19710Sstevel@tonic-gate * their metric 19720Sstevel@tonic-gate */ 19730Sstevel@tonic-gate pref = 1; 19740Sstevel@tonic-gate ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE); 19750Sstevel@tonic-gate 19760Sstevel@tonic-gate /* 19770Sstevel@tonic-gate * Do not install host routes directly to hosts, to avoid 19780Sstevel@tonic-gate * interfering with ARP entries in the kernel table. 19790Sstevel@tonic-gate */ 19800Sstevel@tonic-gate if (RT_ISHOST(RT) && ntohl(RT->rt_dst) == RT->rt_gate) 19810Sstevel@tonic-gate return (0); 19820Sstevel@tonic-gate 19830Sstevel@tonic-gate } else { 19840Sstevel@tonic-gate /* 19850Sstevel@tonic-gate * This is an interface route. 19860Sstevel@tonic-gate * Do not install routes for "external" remote interfaces. 19870Sstevel@tonic-gate */ 19880Sstevel@tonic-gate if (RT->rt_ifp != NULL && (RT->rt_ifp->int_state & IS_EXTERNAL)) 19890Sstevel@tonic-gate return (0); 19900Sstevel@tonic-gate 19910Sstevel@tonic-gate /* Interfaces should override received routes. */ 19920Sstevel@tonic-gate pref = 0; 19930Sstevel@tonic-gate ags |= (AGS_IF | AGS_CORS_GATE); 19940Sstevel@tonic-gate if (RT->rt_ifp != NULL && 19950Sstevel@tonic-gate !(RT->rt_ifp->int_if_flags & IFF_LOOPBACK) && 19960Sstevel@tonic-gate (RT->rt_ifp->int_state & (IS_PASSIVE|IS_ALIAS)) == 19970Sstevel@tonic-gate IS_PASSIVE) { 19980Sstevel@tonic-gate ags |= AGS_PASSIVE; 19990Sstevel@tonic-gate } 20000Sstevel@tonic-gate 20010Sstevel@tonic-gate /* 20020Sstevel@tonic-gate * If it is not an interface, or an alias for an interface, 20030Sstevel@tonic-gate * it must be a "gateway." 20040Sstevel@tonic-gate * 20050Sstevel@tonic-gate * If it is a "remote" interface, it is also a "gateway" to 20060Sstevel@tonic-gate * the kernel if is not a alias. 20070Sstevel@tonic-gate */ 2008879Sbw if (RT->rt_ifp == NULL || (RT->rt_ifp->int_state & IS_REMOTE)) { 2009879Sbw 2010879Sbw ags |= (AGS_GATEWAY | AGS_SUPPRESS); 2011879Sbw 2012879Sbw /* 2013879Sbw * Do not aggregate IS_PASSIVE routes. 2014879Sbw */ 2015879Sbw if (!(RT->rt_ifp->int_state & IS_PASSIVE)) 2016879Sbw ags |= AGS_AGGREGATE; 2017879Sbw } 20180Sstevel@tonic-gate } 20190Sstevel@tonic-gate 20200Sstevel@tonic-gate metric = RT->rt_metric; 20210Sstevel@tonic-gate if (metric == HOPCNT_INFINITY) { 20220Sstevel@tonic-gate /* If the route is dead, try hard to aggregate. */ 20230Sstevel@tonic-gate pref = HOPCNT_INFINITY; 20240Sstevel@tonic-gate ags |= (AGS_FINE_GATE | AGS_SUPPRESS); 20250Sstevel@tonic-gate ags &= ~(AGS_IF | AGS_CORS_GATE); 20260Sstevel@tonic-gate } 20270Sstevel@tonic-gate 20280Sstevel@tonic-gate /* 20290Sstevel@tonic-gate * dump all routes that have the same metric as rt_spares[0] 20300Sstevel@tonic-gate * into the kern_table, to be added to the kernel. 20310Sstevel@tonic-gate */ 20320Sstevel@tonic-gate for (i = 0; i < RT->rt_num_spares; i++) { 20330Sstevel@tonic-gate rts = &RT->rt_spares[i]; 20340Sstevel@tonic-gate 20350Sstevel@tonic-gate /* Do not install external routes */ 20360Sstevel@tonic-gate if (rts->rts_flags & RTS_EXTERNAL) 20370Sstevel@tonic-gate continue; 20380Sstevel@tonic-gate 20390Sstevel@tonic-gate if (rts->rts_metric == metric) { 20400Sstevel@tonic-gate ag_check(RT->rt_dst, RT->rt_mask, 20410Sstevel@tonic-gate rts->rts_router, rts->rts_ifp, rts->rts_gate, 20420Sstevel@tonic-gate metric, pref, 0, 0, 20430Sstevel@tonic-gate (rts->rts_origin & RO_FILE) ? (ags|AGS_FILE) : ags, 20440Sstevel@tonic-gate kern_out); 20450Sstevel@tonic-gate } 20460Sstevel@tonic-gate } 20470Sstevel@tonic-gate return (0); 20480Sstevel@tonic-gate #undef RT 20490Sstevel@tonic-gate } 20500Sstevel@tonic-gate 20510Sstevel@tonic-gate 20520Sstevel@tonic-gate /* Update the kernel table to match the daemon table. */ 20530Sstevel@tonic-gate static void 20540Sstevel@tonic-gate fix_kern(void) 20550Sstevel@tonic-gate { 20560Sstevel@tonic-gate int i; 20570Sstevel@tonic-gate struct khash *k, *pk, *knext; 20580Sstevel@tonic-gate 20590Sstevel@tonic-gate 20600Sstevel@tonic-gate need_kern = age_timer; 20610Sstevel@tonic-gate 20620Sstevel@tonic-gate /* Walk daemon table, updating the copy of the kernel table. */ 20630Sstevel@tonic-gate (void) rn_walktree(rhead, walk_kern, NULL); 20640Sstevel@tonic-gate ag_flush(0, 0, kern_out); 20650Sstevel@tonic-gate 20660Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 20670Sstevel@tonic-gate pk = NULL; 20680Sstevel@tonic-gate for (k = khash_bins[i]; k != NULL; k = knext) { 20690Sstevel@tonic-gate knext = k->k_next; 20700Sstevel@tonic-gate 20710Sstevel@tonic-gate /* Do not touch local interface routes */ 20720Sstevel@tonic-gate if ((k->k_state & KS_DEPRE_IF) || 20730Sstevel@tonic-gate (k->k_state & (KS_IF|KS_PASSIVE)) == KS_IF) { 20740Sstevel@tonic-gate pk = k; 20750Sstevel@tonic-gate continue; 20760Sstevel@tonic-gate } 20770Sstevel@tonic-gate 20780Sstevel@tonic-gate /* Do not touch static routes */ 20790Sstevel@tonic-gate if (k->k_state & KS_STATIC) { 20800Sstevel@tonic-gate kern_check_static(k, 0); 20810Sstevel@tonic-gate pk = k; 20820Sstevel@tonic-gate continue; 20830Sstevel@tonic-gate } 20840Sstevel@tonic-gate 20850Sstevel@tonic-gate /* check hold on routes deleted by the operator */ 20860Sstevel@tonic-gate if (k->k_keep > now.tv_sec) { 20870Sstevel@tonic-gate /* ensure we check when the hold is over */ 20880Sstevel@tonic-gate LIM_SEC(need_kern, k->k_keep); 20890Sstevel@tonic-gate pk = k; 20900Sstevel@tonic-gate continue; 20910Sstevel@tonic-gate } 20920Sstevel@tonic-gate 20930Sstevel@tonic-gate if ((k->k_state & KS_DELETE) && 20940Sstevel@tonic-gate !(k->k_state & KS_DYNAMIC)) { 20950Sstevel@tonic-gate if ((k->k_dst == RIP_DEFAULT) && 20960Sstevel@tonic-gate (k->k_ifp != NULL) && 20970Sstevel@tonic-gate (kern_alternate(RIP_DEFAULT, 20980Sstevel@tonic-gate k->k_mask, k->k_gate, k->k_ifp, 20990Sstevel@tonic-gate NULL) == NULL)) 21000Sstevel@tonic-gate rdisc_restore(k->k_ifp); 21010Sstevel@tonic-gate kern_ioctl(k, RTM_DELETE, 0); 21020Sstevel@tonic-gate if (pk != NULL) 21030Sstevel@tonic-gate pk->k_next = knext; 21040Sstevel@tonic-gate else 21050Sstevel@tonic-gate khash_bins[i] = knext; 21060Sstevel@tonic-gate free(k); 21070Sstevel@tonic-gate continue; 21080Sstevel@tonic-gate } 21090Sstevel@tonic-gate 21100Sstevel@tonic-gate if (k->k_state & KS_DEL_ADD) 21110Sstevel@tonic-gate kern_ioctl(k, RTM_DELETE, 0); 21120Sstevel@tonic-gate 21130Sstevel@tonic-gate if (k->k_state & KS_ADD) { 21140Sstevel@tonic-gate if ((k->k_dst == RIP_DEFAULT) && 21150Sstevel@tonic-gate (k->k_ifp != NULL)) 21160Sstevel@tonic-gate rdisc_suppress(k->k_ifp); 21170Sstevel@tonic-gate kern_ioctl(k, RTM_ADD, 21180Sstevel@tonic-gate ((0 != (k->k_state & (KS_GATEWAY | 21190Sstevel@tonic-gate KS_DYNAMIC))) ? RTF_GATEWAY : 0)); 21200Sstevel@tonic-gate } else if (k->k_state & KS_CHANGE) { 21210Sstevel@tonic-gate /* 21220Sstevel@tonic-gate * Should be using RTM_CHANGE here, but 21230Sstevel@tonic-gate * since RTM_CHANGE is currently 21240Sstevel@tonic-gate * not multipath-aware, and assumes 21250Sstevel@tonic-gate * that RTF_GATEWAY implies the gateway 21260Sstevel@tonic-gate * of the route for dst has to be 21270Sstevel@tonic-gate * changed, we play safe, and do a del + add. 21280Sstevel@tonic-gate */ 21290Sstevel@tonic-gate kern_ioctl(k, RTM_DELETE, 0); 21300Sstevel@tonic-gate kern_ioctl(k, RTM_ADD, 21310Sstevel@tonic-gate ((0 != (k->k_state & (KS_GATEWAY | 21320Sstevel@tonic-gate KS_DYNAMIC))) ? RTF_GATEWAY : 0)); 21330Sstevel@tonic-gate } 21340Sstevel@tonic-gate k->k_state &= ~(KS_ADD|KS_CHANGE|KS_DEL_ADD); 21350Sstevel@tonic-gate 21360Sstevel@tonic-gate /* 21370Sstevel@tonic-gate * Mark this route to be deleted in the next cycle. 21380Sstevel@tonic-gate * This deletes routes that disappear from the 21390Sstevel@tonic-gate * daemon table, since the normal aging code 21400Sstevel@tonic-gate * will clear the bit for routes that have not 21410Sstevel@tonic-gate * disappeared from the daemon table. 21420Sstevel@tonic-gate */ 21430Sstevel@tonic-gate k->k_state |= KS_DELETE; 21440Sstevel@tonic-gate pk = k; 21450Sstevel@tonic-gate } 21460Sstevel@tonic-gate } 21470Sstevel@tonic-gate } 21480Sstevel@tonic-gate 21490Sstevel@tonic-gate 21500Sstevel@tonic-gate /* Delete a static route in the image of the kernel table. */ 21510Sstevel@tonic-gate void 21520Sstevel@tonic-gate del_static(in_addr_t dst, in_addr_t mask, in_addr_t gate, 21530Sstevel@tonic-gate struct interface *ifp, int gone) 21540Sstevel@tonic-gate { 21550Sstevel@tonic-gate struct khash *k; 21560Sstevel@tonic-gate struct rt_entry *rt; 21570Sstevel@tonic-gate 21580Sstevel@tonic-gate /* 21590Sstevel@tonic-gate * Just mark it in the table to be deleted next time the kernel 21600Sstevel@tonic-gate * table is updated. 21610Sstevel@tonic-gate * If it has already been deleted, mark it as such, and set its 21620Sstevel@tonic-gate * keep-timer so that it will not be deleted again for a while. 21630Sstevel@tonic-gate * This lets the operator delete a route added by the daemon 21640Sstevel@tonic-gate * and add a replacement. 21650Sstevel@tonic-gate */ 21660Sstevel@tonic-gate k = kern_find(dst, mask, gate, ifp, NULL); 21670Sstevel@tonic-gate if (k != NULL && (gate == 0 || k->k_gate == gate)) { 21680Sstevel@tonic-gate k->k_state &= ~(KS_STATIC | KS_DYNAMIC | KS_CHECK); 21690Sstevel@tonic-gate k->k_state |= KS_DELETE; 21700Sstevel@tonic-gate if (gone) { 21710Sstevel@tonic-gate k->k_state |= KS_DELETED; 21720Sstevel@tonic-gate k->k_keep = now.tv_sec + K_KEEP_LIM; 21730Sstevel@tonic-gate } 21740Sstevel@tonic-gate } 21750Sstevel@tonic-gate 21760Sstevel@tonic-gate rt = rtget(dst, mask); 21770Sstevel@tonic-gate if (rt != NULL && (rt->rt_state & RS_STATIC)) 21780Sstevel@tonic-gate rtbad(rt, NULL); 21790Sstevel@tonic-gate } 21800Sstevel@tonic-gate 21810Sstevel@tonic-gate 21820Sstevel@tonic-gate /* 21830Sstevel@tonic-gate * Delete all routes generated from ICMP Redirects that use a given gateway, 21840Sstevel@tonic-gate * as well as old redirected routes. 21850Sstevel@tonic-gate */ 21860Sstevel@tonic-gate void 21870Sstevel@tonic-gate del_redirects(in_addr_t bad_gate, time_t old) 21880Sstevel@tonic-gate { 21890Sstevel@tonic-gate int i; 21900Sstevel@tonic-gate struct khash *k; 21910Sstevel@tonic-gate boolean_t dosupply = should_supply(NULL); 21920Sstevel@tonic-gate 21930Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 21940Sstevel@tonic-gate for (k = khash_bins[i]; k != NULL; k = k->k_next) { 21950Sstevel@tonic-gate if (!(k->k_state & KS_DYNAMIC) || 21960Sstevel@tonic-gate (k->k_state & (KS_STATIC|KS_IF|KS_DEPRE_IF))) 21970Sstevel@tonic-gate continue; 21980Sstevel@tonic-gate 21990Sstevel@tonic-gate if (k->k_gate != bad_gate && k->k_redirect_time > old && 22000Sstevel@tonic-gate !dosupply) 22010Sstevel@tonic-gate continue; 22020Sstevel@tonic-gate 22030Sstevel@tonic-gate k->k_state |= KS_DELETE; 22040Sstevel@tonic-gate k->k_state &= ~KS_DYNAMIC; 22050Sstevel@tonic-gate need_kern.tv_sec = now.tv_sec; 22060Sstevel@tonic-gate trace_act("mark redirected %s --> %s for deletion", 22070Sstevel@tonic-gate addrname(k->k_dst, k->k_mask, 0), 22080Sstevel@tonic-gate naddr_ntoa(k->k_gate)); 22090Sstevel@tonic-gate } 22100Sstevel@tonic-gate } 22110Sstevel@tonic-gate } 22120Sstevel@tonic-gate 22130Sstevel@tonic-gate /* Start the daemon tables. */ 22140Sstevel@tonic-gate void 22150Sstevel@tonic-gate rtinit(void) 22160Sstevel@tonic-gate { 22170Sstevel@tonic-gate int i; 22180Sstevel@tonic-gate struct ag_info *ag; 22190Sstevel@tonic-gate 22200Sstevel@tonic-gate /* Initialize the radix trees */ 22210Sstevel@tonic-gate rn_init(); 22220Sstevel@tonic-gate (void) rn_inithead((void**)&rhead, 32); 22230Sstevel@tonic-gate 22240Sstevel@tonic-gate /* mark all of the slots in the table free */ 22250Sstevel@tonic-gate ag_avail = ag_slots; 22260Sstevel@tonic-gate for (ag = ag_slots, i = 1; i < NUM_AG_SLOTS; i++) { 22270Sstevel@tonic-gate ag->ag_fine = ag+1; 22280Sstevel@tonic-gate ag++; 22290Sstevel@tonic-gate } 22300Sstevel@tonic-gate } 22310Sstevel@tonic-gate 22320Sstevel@tonic-gate 22330Sstevel@tonic-gate static struct sockaddr_in dst_sock = {AF_INET}; 22340Sstevel@tonic-gate static struct sockaddr_in mask_sock = {AF_INET}; 22350Sstevel@tonic-gate 22360Sstevel@tonic-gate 22370Sstevel@tonic-gate static void 22380Sstevel@tonic-gate set_need_flash(void) 22390Sstevel@tonic-gate { 22400Sstevel@tonic-gate if (!need_flash) { 22410Sstevel@tonic-gate need_flash = _B_TRUE; 22420Sstevel@tonic-gate /* 22430Sstevel@tonic-gate * Do not send the flash update immediately. Wait a little 22440Sstevel@tonic-gate * while to hear from other routers. 22450Sstevel@tonic-gate */ 22460Sstevel@tonic-gate no_flash.tv_sec = now.tv_sec + MIN_WAITTIME; 22470Sstevel@tonic-gate } 22480Sstevel@tonic-gate } 22490Sstevel@tonic-gate 22500Sstevel@tonic-gate 22510Sstevel@tonic-gate /* Get a particular routing table entry */ 22520Sstevel@tonic-gate struct rt_entry * 22530Sstevel@tonic-gate rtget(in_addr_t dst, in_addr_t mask) 22540Sstevel@tonic-gate { 22550Sstevel@tonic-gate struct rt_entry *rt; 22560Sstevel@tonic-gate 22570Sstevel@tonic-gate dst_sock.sin_addr.s_addr = dst; 22580Sstevel@tonic-gate mask_sock.sin_addr.s_addr = htonl(mask); 22590Sstevel@tonic-gate rt = (struct rt_entry *)rhead->rnh_lookup(&dst_sock, &mask_sock, rhead); 22600Sstevel@tonic-gate if (rt == NULL || rt->rt_dst != dst || rt->rt_mask != mask) 22610Sstevel@tonic-gate return (NULL); 22620Sstevel@tonic-gate 22630Sstevel@tonic-gate return (rt); 22640Sstevel@tonic-gate } 22650Sstevel@tonic-gate 22660Sstevel@tonic-gate 22670Sstevel@tonic-gate /* Find a route to dst as the kernel would. */ 22680Sstevel@tonic-gate struct rt_entry * 22690Sstevel@tonic-gate rtfind(in_addr_t dst) 22700Sstevel@tonic-gate { 22710Sstevel@tonic-gate dst_sock.sin_addr.s_addr = dst; 22720Sstevel@tonic-gate return ((struct rt_entry *)rhead->rnh_matchaddr(&dst_sock, rhead)); 22730Sstevel@tonic-gate } 22740Sstevel@tonic-gate 22750Sstevel@tonic-gate 22760Sstevel@tonic-gate /* add a route to the table */ 22770Sstevel@tonic-gate void 22780Sstevel@tonic-gate rtadd(in_addr_t dst, 22790Sstevel@tonic-gate in_addr_t mask, 22800Sstevel@tonic-gate uint16_t state, /* rt_state for the entry */ 22810Sstevel@tonic-gate struct rt_spare *new) 22820Sstevel@tonic-gate { 22830Sstevel@tonic-gate struct rt_entry *rt; 22840Sstevel@tonic-gate in_addr_t smask; 22850Sstevel@tonic-gate int i; 22860Sstevel@tonic-gate struct rt_spare *rts; 22870Sstevel@tonic-gate 22880Sstevel@tonic-gate /* This is the only function that increments total_routes. */ 22890Sstevel@tonic-gate if (total_routes == MAX_ROUTES) { 22900Sstevel@tonic-gate msglog("have maximum (%d) routes", total_routes); 22910Sstevel@tonic-gate return; 22920Sstevel@tonic-gate } 22930Sstevel@tonic-gate 22940Sstevel@tonic-gate rt = rtmalloc(sizeof (*rt), "rtadd"); 22950Sstevel@tonic-gate (void) memset(rt, 0, sizeof (*rt)); 22960Sstevel@tonic-gate rt->rt_spares = rtmalloc(SPARE_INC * sizeof (struct rt_spare), 22970Sstevel@tonic-gate "rtadd"); 22980Sstevel@tonic-gate rt->rt_num_spares = SPARE_INC; 22990Sstevel@tonic-gate (void) memset(rt->rt_spares, 0, SPARE_INC * sizeof (struct rt_spare)); 23000Sstevel@tonic-gate for (rts = rt->rt_spares, i = rt->rt_num_spares; i != 0; i--, rts++) 23010Sstevel@tonic-gate rts->rts_metric = HOPCNT_INFINITY; 23020Sstevel@tonic-gate 23030Sstevel@tonic-gate rt->rt_nodes->rn_key = (uint8_t *)&rt->rt_dst_sock; 23040Sstevel@tonic-gate rt->rt_dst = dst; 23050Sstevel@tonic-gate rt->rt_dst_sock.sin_family = AF_INET; 23060Sstevel@tonic-gate if (mask != HOST_MASK) { 23070Sstevel@tonic-gate smask = std_mask(dst); 23080Sstevel@tonic-gate if ((smask & ~mask) == 0 && mask > smask) 23090Sstevel@tonic-gate state |= RS_SUBNET; 23100Sstevel@tonic-gate } 23110Sstevel@tonic-gate mask_sock.sin_addr.s_addr = htonl(mask); 23120Sstevel@tonic-gate rt->rt_mask = mask; 23130Sstevel@tonic-gate rt->rt_spares[0] = *new; 23140Sstevel@tonic-gate rt->rt_state = state; 23150Sstevel@tonic-gate rt->rt_time = now.tv_sec; 23160Sstevel@tonic-gate rt->rt_poison_metric = HOPCNT_INFINITY; 23170Sstevel@tonic-gate rt->rt_seqno = update_seqno; 23180Sstevel@tonic-gate 23190Sstevel@tonic-gate if (TRACEACTIONS) 23200Sstevel@tonic-gate trace_add_del("Add", rt); 23210Sstevel@tonic-gate 23220Sstevel@tonic-gate need_kern.tv_sec = now.tv_sec; 23230Sstevel@tonic-gate set_need_flash(); 23240Sstevel@tonic-gate 23250Sstevel@tonic-gate if (NULL == rhead->rnh_addaddr(&rt->rt_dst_sock, &mask_sock, rhead, 23260Sstevel@tonic-gate rt->rt_nodes)) { 23270Sstevel@tonic-gate msglog("rnh_addaddr() failed for %s mask=%s", 23280Sstevel@tonic-gate naddr_ntoa(dst), naddr_ntoa(htonl(mask))); 23290Sstevel@tonic-gate free(rt); 23300Sstevel@tonic-gate } 23310Sstevel@tonic-gate 23320Sstevel@tonic-gate total_routes++; 23330Sstevel@tonic-gate } 23340Sstevel@tonic-gate 23350Sstevel@tonic-gate 23360Sstevel@tonic-gate /* notice a changed route */ 23370Sstevel@tonic-gate void 23380Sstevel@tonic-gate rtchange(struct rt_entry *rt, 23390Sstevel@tonic-gate uint16_t state, /* new state bits */ 23400Sstevel@tonic-gate struct rt_spare *new, 23410Sstevel@tonic-gate char *label) 23420Sstevel@tonic-gate { 23430Sstevel@tonic-gate if (rt->rt_metric != new->rts_metric) { 23440Sstevel@tonic-gate /* 23450Sstevel@tonic-gate * Fix the kernel immediately if it seems the route 23460Sstevel@tonic-gate * has gone bad, since there may be a working route that 23470Sstevel@tonic-gate * aggregates this route. 23480Sstevel@tonic-gate */ 23490Sstevel@tonic-gate if (new->rts_metric == HOPCNT_INFINITY) { 23500Sstevel@tonic-gate need_kern.tv_sec = now.tv_sec; 23510Sstevel@tonic-gate if (new->rts_time >= now.tv_sec - EXPIRE_TIME) 23520Sstevel@tonic-gate new->rts_time = now.tv_sec - EXPIRE_TIME; 23530Sstevel@tonic-gate } 23540Sstevel@tonic-gate rt->rt_seqno = update_seqno; 23550Sstevel@tonic-gate set_need_flash(); 23560Sstevel@tonic-gate } 23570Sstevel@tonic-gate 23580Sstevel@tonic-gate if (rt->rt_gate != new->rts_gate) { 23590Sstevel@tonic-gate need_kern.tv_sec = now.tv_sec; 23600Sstevel@tonic-gate rt->rt_seqno = update_seqno; 23610Sstevel@tonic-gate set_need_flash(); 23620Sstevel@tonic-gate } 23630Sstevel@tonic-gate 23640Sstevel@tonic-gate state |= (rt->rt_state & RS_SUBNET); 23650Sstevel@tonic-gate 23660Sstevel@tonic-gate /* Keep various things from deciding ageless routes are stale. */ 23670Sstevel@tonic-gate if (!AGE_RT(state, rt->rt_spares[0].rts_origin, new->rts_ifp)) 23680Sstevel@tonic-gate new->rts_time = now.tv_sec; 23690Sstevel@tonic-gate 23700Sstevel@tonic-gate if (TRACEACTIONS) 23710Sstevel@tonic-gate trace_change(rt, state, new, 23720Sstevel@tonic-gate label ? label : "Chg "); 23730Sstevel@tonic-gate 23740Sstevel@tonic-gate rt->rt_state = state; 23750Sstevel@tonic-gate /* 23760Sstevel@tonic-gate * If the interface state of the new primary route is good, 23770Sstevel@tonic-gate * turn off RS_BADIF flag 23780Sstevel@tonic-gate */ 23790Sstevel@tonic-gate if ((rt->rt_state & RS_BADIF) && 23800Sstevel@tonic-gate IS_IFF_UP(new->rts_ifp->int_if_flags) && 23810Sstevel@tonic-gate !(new->rts_ifp->int_state & (IS_BROKE | IS_SICK))) 23820Sstevel@tonic-gate rt->rt_state &= ~(RS_BADIF); 23830Sstevel@tonic-gate 23840Sstevel@tonic-gate rt->rt_spares[0] = *new; 23850Sstevel@tonic-gate } 23860Sstevel@tonic-gate 23870Sstevel@tonic-gate 23880Sstevel@tonic-gate /* check for a better route among the spares */ 23890Sstevel@tonic-gate static struct rt_spare * 23900Sstevel@tonic-gate rts_better(struct rt_entry *rt) 23910Sstevel@tonic-gate { 23920Sstevel@tonic-gate struct rt_spare *rts, *rts1; 23930Sstevel@tonic-gate int i; 23940Sstevel@tonic-gate 23950Sstevel@tonic-gate /* find the best alternative among the spares */ 23960Sstevel@tonic-gate rts = rt->rt_spares+1; 23970Sstevel@tonic-gate for (i = rt->rt_num_spares, rts1 = rts+1; i > 2; i--, rts1++) { 23980Sstevel@tonic-gate if (BETTER_LINK(rt, rts1, rts)) 23990Sstevel@tonic-gate rts = rts1; 24000Sstevel@tonic-gate } 24010Sstevel@tonic-gate 24020Sstevel@tonic-gate return (rts); 24030Sstevel@tonic-gate } 24040Sstevel@tonic-gate 24050Sstevel@tonic-gate 24060Sstevel@tonic-gate /* switch to a backup route */ 24070Sstevel@tonic-gate void 24080Sstevel@tonic-gate rtswitch(struct rt_entry *rt, 24090Sstevel@tonic-gate struct rt_spare *rts) 24100Sstevel@tonic-gate { 24110Sstevel@tonic-gate struct rt_spare swap; 24120Sstevel@tonic-gate char label[10]; 24130Sstevel@tonic-gate 24140Sstevel@tonic-gate /* Do not change permanent routes */ 24150Sstevel@tonic-gate if (0 != (rt->rt_state & (RS_MHOME | RS_STATIC | 24160Sstevel@tonic-gate RS_NET_SYN | RS_IF))) 24170Sstevel@tonic-gate return; 24180Sstevel@tonic-gate 24190Sstevel@tonic-gate /* find the best alternative among the spares */ 24200Sstevel@tonic-gate if (rts == NULL) 24210Sstevel@tonic-gate rts = rts_better(rt); 24220Sstevel@tonic-gate 24230Sstevel@tonic-gate /* Do not bother if it is not worthwhile. */ 24240Sstevel@tonic-gate if (!BETTER_LINK(rt, rts, rt->rt_spares)) 24250Sstevel@tonic-gate return; 24260Sstevel@tonic-gate 24270Sstevel@tonic-gate swap = rt->rt_spares[0]; 24280Sstevel@tonic-gate (void) snprintf(label, sizeof (label), "Use #%d", 24290Sstevel@tonic-gate (int)(rts - rt->rt_spares)); 24300Sstevel@tonic-gate rtchange(rt, rt->rt_state & ~(RS_NET_SYN), rts, label); 24310Sstevel@tonic-gate 24320Sstevel@tonic-gate if (swap.rts_metric == HOPCNT_INFINITY) { 24330Sstevel@tonic-gate *rts = rts_empty; 24340Sstevel@tonic-gate } else { 24350Sstevel@tonic-gate *rts = swap; 24360Sstevel@tonic-gate } 24370Sstevel@tonic-gate 24380Sstevel@tonic-gate } 24390Sstevel@tonic-gate 24400Sstevel@tonic-gate 24410Sstevel@tonic-gate void 24420Sstevel@tonic-gate rtdelete(struct rt_entry *rt) 24430Sstevel@tonic-gate { 24440Sstevel@tonic-gate struct rt_entry *deleted_rt; 24450Sstevel@tonic-gate struct rt_spare *rts; 24460Sstevel@tonic-gate int i; 24470Sstevel@tonic-gate in_addr_t gate = rt->rt_gate; /* for debugging */ 24480Sstevel@tonic-gate 24490Sstevel@tonic-gate if (TRACEACTIONS) 24500Sstevel@tonic-gate trace_add_del("Del", rt); 24510Sstevel@tonic-gate 24520Sstevel@tonic-gate for (i = 0; i < rt->rt_num_spares; i++) { 24530Sstevel@tonic-gate rts = &rt->rt_spares[i]; 24540Sstevel@tonic-gate rts_delete(rt, rts); 24550Sstevel@tonic-gate } 24560Sstevel@tonic-gate 24570Sstevel@tonic-gate dst_sock.sin_addr.s_addr = rt->rt_dst; 24580Sstevel@tonic-gate mask_sock.sin_addr.s_addr = htonl(rt->rt_mask); 24590Sstevel@tonic-gate if (rt != (deleted_rt = 24600Sstevel@tonic-gate ((struct rt_entry *)rhead->rnh_deladdr(&dst_sock, &mask_sock, 24610Sstevel@tonic-gate rhead)))) { 24620Sstevel@tonic-gate msglog("rnh_deladdr(%s) failed; found rt 0x%lx", 24630Sstevel@tonic-gate rtname(rt->rt_dst, rt->rt_mask, gate), deleted_rt); 24640Sstevel@tonic-gate if (deleted_rt != NULL) 24650Sstevel@tonic-gate free(deleted_rt); 24660Sstevel@tonic-gate } 24670Sstevel@tonic-gate total_routes--; 24680Sstevel@tonic-gate free(rt); 24690Sstevel@tonic-gate 24700Sstevel@tonic-gate if (dst_sock.sin_addr.s_addr == RIP_DEFAULT) { 24710Sstevel@tonic-gate /* 24720Sstevel@tonic-gate * we just deleted the default route. Trigger rdisc_sort 24730Sstevel@tonic-gate * so that we can recover from any rdisc information that 24740Sstevel@tonic-gate * is valid 24750Sstevel@tonic-gate */ 24760Sstevel@tonic-gate rdisc_timer.tv_sec = 0; 24770Sstevel@tonic-gate } 24780Sstevel@tonic-gate } 24790Sstevel@tonic-gate 24800Sstevel@tonic-gate void 24810Sstevel@tonic-gate rts_delete(struct rt_entry *rt, struct rt_spare *rts) 24820Sstevel@tonic-gate { 24830Sstevel@tonic-gate struct khash *k; 24840Sstevel@tonic-gate 24850Sstevel@tonic-gate trace_upslot(rt, rts, &rts_empty); 24860Sstevel@tonic-gate k = kern_find(rt->rt_dst, rt->rt_mask, 24870Sstevel@tonic-gate rts->rts_gate, rts->rts_ifp, NULL); 24880Sstevel@tonic-gate if (k != NULL && 24890Sstevel@tonic-gate !(k->k_state & KS_DEPRE_IF) && 24900Sstevel@tonic-gate ((k->k_state & (KS_IF|KS_PASSIVE)) != KS_IF)) { 24910Sstevel@tonic-gate k->k_state |= KS_DELETE; 24920Sstevel@tonic-gate need_kern.tv_sec = now.tv_sec; 24930Sstevel@tonic-gate } 24940Sstevel@tonic-gate 24950Sstevel@tonic-gate *rts = rts_empty; 24960Sstevel@tonic-gate } 24970Sstevel@tonic-gate 24980Sstevel@tonic-gate /* 24990Sstevel@tonic-gate * Get rid of a bad route, and try to switch to a replacement. 25000Sstevel@tonic-gate * If the route has gone bad because of a bad interface, 25010Sstevel@tonic-gate * the information about the dead interface is available in badifp 25020Sstevel@tonic-gate * for the purpose of sanity checks, if_flags checks etc. 25030Sstevel@tonic-gate */ 25040Sstevel@tonic-gate static void 25050Sstevel@tonic-gate rtbad(struct rt_entry *rt, struct interface *badifp) 25060Sstevel@tonic-gate { 25070Sstevel@tonic-gate struct rt_spare new; 25080Sstevel@tonic-gate uint16_t rt_state; 25090Sstevel@tonic-gate 25100Sstevel@tonic-gate 25110Sstevel@tonic-gate if (badifp == NULL || (rt->rt_spares[0].rts_ifp == badifp)) { 25120Sstevel@tonic-gate /* Poison the route */ 25130Sstevel@tonic-gate new = rt->rt_spares[0]; 25140Sstevel@tonic-gate new.rts_metric = HOPCNT_INFINITY; 25150Sstevel@tonic-gate rt_state = rt->rt_state & ~(RS_IF | RS_LOCAL | RS_STATIC); 25160Sstevel@tonic-gate } 25170Sstevel@tonic-gate 25180Sstevel@tonic-gate if (badifp != NULL) { 25190Sstevel@tonic-gate /* 25200Sstevel@tonic-gate * Dont mark the rtentry bad unless the ifp for the primary 25210Sstevel@tonic-gate * route is the bad ifp 25220Sstevel@tonic-gate */ 25230Sstevel@tonic-gate if (rt->rt_spares[0].rts_ifp != badifp) 25240Sstevel@tonic-gate return; 25250Sstevel@tonic-gate /* 25260Sstevel@tonic-gate * badifp has just gone bad. We want to keep this 25270Sstevel@tonic-gate * rt_entry around so that we tell our rip-neighbors 25280Sstevel@tonic-gate * about the bad route, but we can't do anything 25290Sstevel@tonic-gate * to the kernel itself, so mark it as RS_BADIF 25300Sstevel@tonic-gate */ 25310Sstevel@tonic-gate trace_misc("rtbad:Setting RS_BADIF (%s)", badifp->int_name); 25320Sstevel@tonic-gate rt_state |= RS_BADIF; 25330Sstevel@tonic-gate new.rts_ifp = &dummy_ifp; 25340Sstevel@tonic-gate } 25350Sstevel@tonic-gate rtchange(rt, rt_state, &new, 0); 25360Sstevel@tonic-gate rtswitch(rt, 0); 25370Sstevel@tonic-gate } 25380Sstevel@tonic-gate 25390Sstevel@tonic-gate 25400Sstevel@tonic-gate /* 25410Sstevel@tonic-gate * Junk a RS_NET_SYN or RS_LOCAL route, 25420Sstevel@tonic-gate * unless it is needed by another interface. 25430Sstevel@tonic-gate */ 25440Sstevel@tonic-gate void 25450Sstevel@tonic-gate rtbad_sub(struct rt_entry *rt, struct interface *badifp) 25460Sstevel@tonic-gate { 25470Sstevel@tonic-gate struct interface *ifp, *ifp1; 25480Sstevel@tonic-gate struct intnet *intnetp; 25490Sstevel@tonic-gate uint_t state; 25500Sstevel@tonic-gate 25510Sstevel@tonic-gate 25520Sstevel@tonic-gate ifp1 = NULL; 25530Sstevel@tonic-gate state = 0; 25540Sstevel@tonic-gate 25550Sstevel@tonic-gate if (rt->rt_state & RS_LOCAL) { 25560Sstevel@tonic-gate /* 25570Sstevel@tonic-gate * Is this the route through loopback for the interface? 25580Sstevel@tonic-gate * If so, see if it is used by any other interfaces, such 25590Sstevel@tonic-gate * as a point-to-point interface with the same local address. 25600Sstevel@tonic-gate */ 25610Sstevel@tonic-gate for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) { 25620Sstevel@tonic-gate /* Retain it if another interface needs it. */ 25630Sstevel@tonic-gate if (ifp->int_addr == rt->rt_ifp->int_addr) { 25640Sstevel@tonic-gate state |= RS_LOCAL; 25650Sstevel@tonic-gate ifp1 = ifp; 25660Sstevel@tonic-gate break; 25670Sstevel@tonic-gate } 25680Sstevel@tonic-gate } 25690Sstevel@tonic-gate 25700Sstevel@tonic-gate } 25710Sstevel@tonic-gate 25720Sstevel@tonic-gate if (!(state & RS_LOCAL)) { 25730Sstevel@tonic-gate /* 25740Sstevel@tonic-gate * Retain RIPv1 logical network route if there is another 25750Sstevel@tonic-gate * interface that justifies it. 25760Sstevel@tonic-gate */ 25770Sstevel@tonic-gate if (rt->rt_state & RS_NET_SYN) { 25780Sstevel@tonic-gate for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) { 25790Sstevel@tonic-gate if ((ifp->int_state & IS_NEED_NET_SYN) && 25800Sstevel@tonic-gate rt->rt_mask == ifp->int_std_mask && 25810Sstevel@tonic-gate rt->rt_dst == ifp->int_std_addr) { 25820Sstevel@tonic-gate state |= RS_NET_SYN; 25830Sstevel@tonic-gate ifp1 = ifp; 25840Sstevel@tonic-gate break; 25850Sstevel@tonic-gate } 25860Sstevel@tonic-gate } 25870Sstevel@tonic-gate } 25880Sstevel@tonic-gate 25890Sstevel@tonic-gate /* or if there is an authority route that needs it. */ 25900Sstevel@tonic-gate for (intnetp = intnets; intnetp != NULL; 25910Sstevel@tonic-gate intnetp = intnetp->intnet_next) { 25920Sstevel@tonic-gate if (intnetp->intnet_addr == rt->rt_dst && 25930Sstevel@tonic-gate intnetp->intnet_mask == rt->rt_mask) { 25940Sstevel@tonic-gate state |= (RS_NET_SYN | RS_NET_INT); 25950Sstevel@tonic-gate break; 25960Sstevel@tonic-gate } 25970Sstevel@tonic-gate } 25980Sstevel@tonic-gate } 25990Sstevel@tonic-gate 26000Sstevel@tonic-gate if (ifp1 != NULL || (state & RS_NET_SYN)) { 26010Sstevel@tonic-gate struct rt_spare new = rt->rt_spares[0]; 26020Sstevel@tonic-gate new.rts_ifp = ifp1; 26030Sstevel@tonic-gate rtchange(rt, ((rt->rt_state & ~(RS_NET_SYN|RS_LOCAL)) | state), 26040Sstevel@tonic-gate &new, 0); 26050Sstevel@tonic-gate } else { 26060Sstevel@tonic-gate rtbad(rt, badifp); 26070Sstevel@tonic-gate } 26080Sstevel@tonic-gate } 26090Sstevel@tonic-gate 26100Sstevel@tonic-gate /* 26110Sstevel@tonic-gate * Called while walking the table looking for sick interfaces 26120Sstevel@tonic-gate * or after a time change. 26130Sstevel@tonic-gate */ 26140Sstevel@tonic-gate int 26150Sstevel@tonic-gate walk_bad(struct radix_node *rn, 26160Sstevel@tonic-gate void *argp) 26170Sstevel@tonic-gate { 26180Sstevel@tonic-gate #define RT ((struct rt_entry *)rn) 26190Sstevel@tonic-gate struct rt_spare *rts; 26200Sstevel@tonic-gate int i, j = -1; 26210Sstevel@tonic-gate 26220Sstevel@tonic-gate /* fix any spare routes through the interface */ 26230Sstevel@tonic-gate for (i = 1; i < RT->rt_num_spares; i++) { 26240Sstevel@tonic-gate rts = &((struct rt_entry *)rn)->rt_spares[i]; 26250Sstevel@tonic-gate 26260Sstevel@tonic-gate if (rts->rts_metric < HOPCNT_INFINITY && 26270Sstevel@tonic-gate (rts->rts_ifp == NULL || 26280Sstevel@tonic-gate (rts->rts_ifp->int_state & IS_BROKE))) 26290Sstevel@tonic-gate rts_delete(RT, rts); 26300Sstevel@tonic-gate else { 26310Sstevel@tonic-gate if (rts->rts_origin != RO_NONE) 26320Sstevel@tonic-gate j = i; 26330Sstevel@tonic-gate } 26340Sstevel@tonic-gate } 26350Sstevel@tonic-gate 26360Sstevel@tonic-gate /* 26370Sstevel@tonic-gate * Deal with the main route 26380Sstevel@tonic-gate * finished if it has been handled before or if its interface is ok 26390Sstevel@tonic-gate */ 26400Sstevel@tonic-gate if (RT->rt_ifp == NULL || !(RT->rt_ifp->int_state & IS_BROKE)) 26410Sstevel@tonic-gate return (0); 26420Sstevel@tonic-gate 26430Sstevel@tonic-gate /* Bad routes for other than interfaces are easy. */ 26440Sstevel@tonic-gate if (!(RT->rt_state & (RS_IF | RS_NET_SYN | RS_LOCAL))) { 2645552Ssowmini if (j > 0) { 2646552Ssowmini RT->rt_spares[0].rts_metric = HOPCNT_INFINITY; 26470Sstevel@tonic-gate rtswitch(RT, NULL); 2648552Ssowmini } else { 26490Sstevel@tonic-gate rtbad(RT, (struct interface *)argp); 2650552Ssowmini } 26510Sstevel@tonic-gate return (0); 26520Sstevel@tonic-gate } 26530Sstevel@tonic-gate 26540Sstevel@tonic-gate rtbad_sub(RT, (struct interface *)argp); 26550Sstevel@tonic-gate return (0); 26560Sstevel@tonic-gate #undef RT 26570Sstevel@tonic-gate } 26580Sstevel@tonic-gate 26590Sstevel@tonic-gate /* 26600Sstevel@tonic-gate * Called while walking the table to replace a duplicate interface 26610Sstevel@tonic-gate * with a backup. 26620Sstevel@tonic-gate */ 26630Sstevel@tonic-gate int 26640Sstevel@tonic-gate walk_rewire(struct radix_node *rn, void *argp) 26650Sstevel@tonic-gate { 26660Sstevel@tonic-gate struct rt_entry *RT = (struct rt_entry *)rn; 26670Sstevel@tonic-gate struct rewire_data *wire = (struct rewire_data *)argp; 26680Sstevel@tonic-gate struct rt_spare *rts; 26690Sstevel@tonic-gate int i; 26700Sstevel@tonic-gate 26710Sstevel@tonic-gate /* fix any spare routes through the interface */ 26720Sstevel@tonic-gate rts = RT->rt_spares; 26730Sstevel@tonic-gate for (i = RT->rt_num_spares; i > 0; i--, rts++) { 26740Sstevel@tonic-gate if (rts->rts_ifp == wire->if_old) { 26750Sstevel@tonic-gate rts->rts_ifp = wire->if_new; 26760Sstevel@tonic-gate if ((RT->rt_dst == RIP_DEFAULT) && 26770Sstevel@tonic-gate (wire->if_old->int_state & IS_SUPPRESS_RDISC)) 26780Sstevel@tonic-gate rdisc_suppress(rts->rts_ifp); 26790Sstevel@tonic-gate if ((rts->rts_metric += wire->metric_delta) > 26800Sstevel@tonic-gate HOPCNT_INFINITY) 26810Sstevel@tonic-gate rts->rts_metric = HOPCNT_INFINITY; 26820Sstevel@tonic-gate 26830Sstevel@tonic-gate /* 26840Sstevel@tonic-gate * If the main route is getting a worse metric, 26850Sstevel@tonic-gate * then it may be time to switch to a backup. 26860Sstevel@tonic-gate */ 26870Sstevel@tonic-gate if (i == RT->rt_num_spares && wire->metric_delta > 0) { 26880Sstevel@tonic-gate rtswitch(RT, NULL); 26890Sstevel@tonic-gate } 26900Sstevel@tonic-gate } 26910Sstevel@tonic-gate } 26920Sstevel@tonic-gate 26930Sstevel@tonic-gate return (0); 26940Sstevel@tonic-gate } 26950Sstevel@tonic-gate 26960Sstevel@tonic-gate /* Check the age of an individual route. */ 26970Sstevel@tonic-gate static int 26980Sstevel@tonic-gate walk_age(struct radix_node *rn, void *argp) 26990Sstevel@tonic-gate { 27000Sstevel@tonic-gate #define RT ((struct rt_entry *)rn) 27010Sstevel@tonic-gate struct interface *ifp; 27020Sstevel@tonic-gate struct rt_spare *rts; 27030Sstevel@tonic-gate int i; 27040Sstevel@tonic-gate in_addr_t age_bad_gate = *(in_addr_t *)argp; 27050Sstevel@tonic-gate 27060Sstevel@tonic-gate 27070Sstevel@tonic-gate /* 27080Sstevel@tonic-gate * age all of the spare routes, including the primary route 27090Sstevel@tonic-gate * currently in use 27100Sstevel@tonic-gate */ 27110Sstevel@tonic-gate rts = RT->rt_spares; 27120Sstevel@tonic-gate for (i = RT->rt_num_spares; i != 0; i--, rts++) { 27130Sstevel@tonic-gate 27140Sstevel@tonic-gate ifp = rts->rts_ifp; 27150Sstevel@tonic-gate if (i == RT->rt_num_spares) { 27160Sstevel@tonic-gate if (!AGE_RT(RT->rt_state, rts->rts_origin, ifp)) { 27170Sstevel@tonic-gate /* 27180Sstevel@tonic-gate * Keep various things from deciding ageless 27190Sstevel@tonic-gate * routes are stale 27200Sstevel@tonic-gate */ 27210Sstevel@tonic-gate rts->rts_time = now.tv_sec; 27220Sstevel@tonic-gate continue; 27230Sstevel@tonic-gate } 27240Sstevel@tonic-gate 27250Sstevel@tonic-gate /* forget RIP routes after RIP has been turned off. */ 27260Sstevel@tonic-gate if (rip_sock < 0) { 27270Sstevel@tonic-gate rts->rts_time = now_stale + 1; 27280Sstevel@tonic-gate } 27290Sstevel@tonic-gate } 27300Sstevel@tonic-gate 27310Sstevel@tonic-gate /* age failing routes */ 27320Sstevel@tonic-gate if (age_bad_gate == rts->rts_gate && 27330Sstevel@tonic-gate rts->rts_time >= now_stale) { 27340Sstevel@tonic-gate rts->rts_time -= SUPPLY_INTERVAL; 27350Sstevel@tonic-gate } 27360Sstevel@tonic-gate 27370Sstevel@tonic-gate /* trash the spare routes when they go bad */ 27380Sstevel@tonic-gate if (rts->rts_origin == RO_RIP && 27390Sstevel@tonic-gate ((rip_sock < 0) || 27400Sstevel@tonic-gate (rts->rts_metric < HOPCNT_INFINITY && 27410Sstevel@tonic-gate now_garbage > rts->rts_time)) && 27420Sstevel@tonic-gate i != RT->rt_num_spares) { 27430Sstevel@tonic-gate rts_delete(RT, rts); 27440Sstevel@tonic-gate } 27450Sstevel@tonic-gate } 27460Sstevel@tonic-gate 27470Sstevel@tonic-gate 27480Sstevel@tonic-gate /* finished if the active route is still fresh */ 27490Sstevel@tonic-gate if (now_stale <= RT->rt_time) 27500Sstevel@tonic-gate return (0); 27510Sstevel@tonic-gate 27520Sstevel@tonic-gate /* try to switch to an alternative */ 27530Sstevel@tonic-gate rtswitch(RT, NULL); 27540Sstevel@tonic-gate 27550Sstevel@tonic-gate /* Delete a dead route after it has been publically mourned. */ 27560Sstevel@tonic-gate if (now_garbage > RT->rt_time) { 27570Sstevel@tonic-gate rtdelete(RT); 27580Sstevel@tonic-gate return (0); 27590Sstevel@tonic-gate } 27600Sstevel@tonic-gate 27610Sstevel@tonic-gate /* Start poisoning a bad route before deleting it. */ 27620Sstevel@tonic-gate if (now.tv_sec - RT->rt_time > EXPIRE_TIME) { 27630Sstevel@tonic-gate struct rt_spare new = RT->rt_spares[0]; 27640Sstevel@tonic-gate 27650Sstevel@tonic-gate new.rts_metric = HOPCNT_INFINITY; 27660Sstevel@tonic-gate rtchange(RT, RT->rt_state, &new, 0); 27670Sstevel@tonic-gate } 27680Sstevel@tonic-gate return (0); 27690Sstevel@tonic-gate } 27700Sstevel@tonic-gate 27710Sstevel@tonic-gate 27720Sstevel@tonic-gate /* Watch for dead routes and interfaces. */ 27730Sstevel@tonic-gate void 27740Sstevel@tonic-gate age(in_addr_t bad_gate) 27750Sstevel@tonic-gate { 27760Sstevel@tonic-gate struct interface *ifp; 27770Sstevel@tonic-gate int need_query = 0; 27780Sstevel@tonic-gate 27790Sstevel@tonic-gate /* 27800Sstevel@tonic-gate * If not listening to RIP, there is no need to age the routes in 27810Sstevel@tonic-gate * the table. 27820Sstevel@tonic-gate */ 27830Sstevel@tonic-gate age_timer.tv_sec = (now.tv_sec 27840Sstevel@tonic-gate + ((rip_sock < 0) ? NEVER : SUPPLY_INTERVAL)); 27850Sstevel@tonic-gate 27860Sstevel@tonic-gate /* 27870Sstevel@tonic-gate * Check for dead IS_REMOTE interfaces by timing their 27880Sstevel@tonic-gate * transmissions. 27890Sstevel@tonic-gate */ 27900Sstevel@tonic-gate for (ifp = ifnet; ifp; ifp = ifp->int_next) { 27910Sstevel@tonic-gate if (!(ifp->int_state & IS_REMOTE)) 27920Sstevel@tonic-gate continue; 27930Sstevel@tonic-gate 27940Sstevel@tonic-gate /* ignore unreachable remote interfaces */ 27950Sstevel@tonic-gate if (!check_remote(ifp)) 27960Sstevel@tonic-gate continue; 27970Sstevel@tonic-gate 27980Sstevel@tonic-gate /* Restore remote interface that has become reachable */ 27990Sstevel@tonic-gate if (ifp->int_state & IS_BROKE) 28000Sstevel@tonic-gate if_ok(ifp, "remote ", _B_FALSE); 28010Sstevel@tonic-gate 28020Sstevel@tonic-gate if (ifp->int_act_time != NEVER && 28030Sstevel@tonic-gate now.tv_sec - ifp->int_act_time > EXPIRE_TIME) { 28040Sstevel@tonic-gate writelog(LOG_NOTICE, 28050Sstevel@tonic-gate "remote interface %s to %s timed out after" 28060Sstevel@tonic-gate " %ld:%ld", 28070Sstevel@tonic-gate ifp->int_name, 28080Sstevel@tonic-gate naddr_ntoa(ifp->int_dstaddr), 28090Sstevel@tonic-gate (now.tv_sec - ifp->int_act_time)/60, 28100Sstevel@tonic-gate (now.tv_sec - ifp->int_act_time)%60); 28110Sstevel@tonic-gate if_sick(ifp, _B_FALSE); 28120Sstevel@tonic-gate } 28130Sstevel@tonic-gate 28140Sstevel@tonic-gate /* 28150Sstevel@tonic-gate * If we have not heard from the other router 28160Sstevel@tonic-gate * recently, ask it. 28170Sstevel@tonic-gate */ 28180Sstevel@tonic-gate if (now.tv_sec >= ifp->int_query_time) { 28190Sstevel@tonic-gate ifp->int_query_time = NEVER; 28200Sstevel@tonic-gate need_query = 1; 28210Sstevel@tonic-gate } 28220Sstevel@tonic-gate } 28230Sstevel@tonic-gate 28240Sstevel@tonic-gate /* Age routes. */ 28250Sstevel@tonic-gate (void) rn_walktree(rhead, walk_age, &bad_gate); 28260Sstevel@tonic-gate 28270Sstevel@tonic-gate /* 28280Sstevel@tonic-gate * delete old redirected routes to keep the kernel table small 28290Sstevel@tonic-gate * and prevent blackholes 28300Sstevel@tonic-gate */ 28310Sstevel@tonic-gate del_redirects(bad_gate, now.tv_sec-STALE_TIME); 28320Sstevel@tonic-gate 28330Sstevel@tonic-gate /* Update the kernel routing table. */ 28340Sstevel@tonic-gate fix_kern(); 28350Sstevel@tonic-gate 28360Sstevel@tonic-gate /* poke reticent remote gateways */ 28370Sstevel@tonic-gate if (need_query) 28380Sstevel@tonic-gate rip_query(); 28390Sstevel@tonic-gate } 28400Sstevel@tonic-gate 28410Sstevel@tonic-gate void 28420Sstevel@tonic-gate kern_dump(void) 28430Sstevel@tonic-gate { 28440Sstevel@tonic-gate int i; 28450Sstevel@tonic-gate struct khash *k; 28460Sstevel@tonic-gate 28470Sstevel@tonic-gate for (i = 0; i < KHASH_SIZE; i++) { 28480Sstevel@tonic-gate for (k = khash_bins[i]; k != NULL; k = k->k_next) 28490Sstevel@tonic-gate trace_khash(k); 28500Sstevel@tonic-gate } 28510Sstevel@tonic-gate } 28520Sstevel@tonic-gate 28530Sstevel@tonic-gate 28540Sstevel@tonic-gate static struct interface * 28550Sstevel@tonic-gate gwkludge_iflookup(in_addr_t dstaddr, in_addr_t addr, in_addr_t mask) 28560Sstevel@tonic-gate { 28570Sstevel@tonic-gate uint32_t int_state; 28580Sstevel@tonic-gate struct interface *ifp; 28590Sstevel@tonic-gate 28600Sstevel@tonic-gate for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) { 28610Sstevel@tonic-gate int_state = ifp->int_state; 28620Sstevel@tonic-gate 28630Sstevel@tonic-gate if (!(int_state & IS_REMOTE)) 28640Sstevel@tonic-gate continue; 28650Sstevel@tonic-gate 28660Sstevel@tonic-gate if (ifp->int_dstaddr == dstaddr && ifp->int_addr == addr && 28670Sstevel@tonic-gate ifp->int_mask == mask) 28680Sstevel@tonic-gate return (ifp); 28690Sstevel@tonic-gate } 28700Sstevel@tonic-gate return (NULL); 28710Sstevel@tonic-gate } 2872