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