/* Supply dst with the contents of the routing tables. * If this won't fit in one packet, chop it up into several. */ void supply(struct sockaddr_in *dst, struct interface *ifp, /* output interface */ enum output_type type, int flash, /* 1=flash update */ int vers, /* RIP version */ int passwd_ok) /* OK to include cleartext password */ { struct rt_entry *rt; int def_metric; ws.state = 0; ws.gen_limit = 1024; ws.to = *dst; ws.to_std_mask = std_mask(ws.to.sin_addr.s_addr); ws.to_std_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_std_mask; if (ifp != NULL) { ws.to_mask = ifp->int_mask; ws.to_net = ifp->int_net; if (on_net(ws.to.sin_addr.s_addr, ws.to_net, ws.to_mask)) ws.state |= WS_ST_TO_ON_NET; } else { ws.to_mask = ripv1_mask_net(ws.to.sin_addr.s_addr, 0); ws.to_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_mask; rt = rtfind(dst->sin_addr.s_addr); if (rt) ifp = rt->rt_ifp; } ws.npackets = 0; if (flash) ws.state |= WS_ST_FLASH; if ((ws.ifp = ifp) == NULL) { ws.metric = 1; } else { /* Adjust the advertised metric by the outgoing interface * metric. */ ws.metric = ifp->int_metric+1; } ripv12_buf.rip.rip_vers = vers; switch (type) { case OUT_MULTICAST: if (ifp->int_if_flags & IFF_MULTICAST) v2buf.type = OUT_MULTICAST; else v2buf.type = NO_OUT_MULTICAST; v12buf.type = OUT_BROADCAST; break; case OUT_QUERY: ws.state |= WS_ST_QUERY; /* fall through */ case OUT_BROADCAST: case OUT_UNICAST: v2buf.type = (vers == RIPv2) ? type : NO_OUT_RIPV2; v12buf.type = type; break; case NO_OUT_MULTICAST: case NO_OUT_RIPV2: break; /* no output */ } if (vers == RIPv2) { /* full RIPv2 only if cannot be heard by RIPv1 listeners */ if (type != OUT_BROADCAST) ws.state |= WS_ST_RIP2_ALL; if ((ws.state & WS_ST_QUERY) || !(ws.state & WS_ST_TO_ON_NET)) { ws.state |= (WS_ST_AG | WS_ST_SUPER_AG); } else if (ifp == NULL || !(ifp->int_state & IS_NO_AG)) { ws.state |= WS_ST_AG; if (type != OUT_BROADCAST && (ifp == NULL || !(ifp->int_state & IS_NO_SUPER_AG))) ws.state |= WS_ST_SUPER_AG; } } ws.a = (vers == RIPv2) ? find_auth(ifp) : 0; if (!passwd_ok && ws.a != NULL && ws.a->type == RIP_AUTH_PW) ws.a = NULL; clr_ws_buf(&v12buf,ws.a); clr_ws_buf(&v2buf,ws.a); /* Fake a default route if asked and if there is not already * a better, real default route. */ if (supplier && (def_metric = ifp->int_d_metric) != 0) { if (NULL == (rt = rtget(RIP_DEFAULT, 0)) || rt->rt_metric+ws.metric >= def_metric) { ws.state |= WS_ST_DEFAULT; ag_check(0, 0, 0, 0, def_metric, def_metric, 0, 0, 0, supply_out); } else { def_metric = rt->rt_metric+ws.metric; } /* If both RIPv2 and the poor-man's router discovery * kludge are on, arrange to advertise an extra * default route via RIPv1. */ if ((ws.state & WS_ST_RIP2_ALL) && (ifp->int_state & IS_PM_RDISC)) { ripv12_buf.rip.rip_vers = RIPv1; v12buf.n->n_family = RIP_AF_INET; v12buf.n->n_dst = htonl(RIP_DEFAULT); v12buf.n->n_metric = htonl(def_metric); v12buf.n++; } } rn_walktree(rhead, walk_supply, 0); ag_flush(0,0,supply_out); /* Flush the packet buffers, provided they are not empty and * do not contain only the password. */ if (v12buf.n != v12buf.base && (v12buf.n > v12buf.base+1 || v12buf.base->n_family != RIP_AF_AUTH)) supply_write(&v12buf); if (v2buf.n != v2buf.base && (v2buf.n > v2buf.base+1 || v2buf.base->n_family != RIP_AF_AUTH)) supply_write(&v2buf); /* If we sent nothing and this is an answer to a query, send * an empty buffer. */ if (ws.npackets == 0 && (ws.state & WS_ST_QUERY)) supply_write(&v12buf); }
/* put an entry into the packet */ static void supply_out(struct ag_info *ag) { int i; naddr mask, v1_mask, dst_h, ddst_h = 0; struct ws_buf *wb; /* Skip this route if doing a flash update and it and the routes * it aggregates have not changed recently. */ if (ag->ag_seqno < update_seqno && (ws.state & WS_ST_FLASH)) return; dst_h = ag->ag_dst_h; mask = ag->ag_mask; v1_mask = ripv1_mask_host(htonl(dst_h), (ws.state & WS_ST_TO_ON_NET) ? ws.ifp : 0); i = 0; /* If we are sending RIPv2 packets that cannot (or must not) be * heard by RIPv1 listeners, do not worry about sub- or supernets. * Subnets (from other networks) can only be sent via multicast. * A pair of subnet routes might have been promoted so that they * are legal to send by RIPv1. * If RIPv1 is off, use the multicast buffer. */ if ((ws.state & WS_ST_RIP2_ALL) || ((ag->ag_state & AGS_RIPV2) && v1_mask != mask)) { /* use the RIPv2-only buffer */ wb = &v2buf; } else { /* use the RIPv1-or-RIPv2 buffer */ wb = &v12buf; /* Convert supernet route into corresponding set of network * routes for RIPv1, but leave non-contiguous netmasks * to ag_check(). */ if (v1_mask > mask && mask + (mask & -mask) == 0) { ddst_h = v1_mask & -v1_mask; i = (v1_mask & ~mask)/ddst_h; if (i > ws.gen_limit) { /* Punt if we would have to generate an * unreasonable number of routes. */ if (TRACECONTENTS) trace_misc("sending %s-->%s as 1" " instead of %d routes", addrname(htonl(dst_h), mask, 1), naddr_ntoa(ws.to.sin_addr .s_addr), i+1); i = 0; } else { mask = v1_mask; ws.gen_limit -= i; } } } do { wb->n->n_family = RIP_AF_INET; wb->n->n_dst = htonl(dst_h); /* If the route is from router-discovery or we are * shutting down, admit only a bad metric. */ wb->n->n_metric = ((stopint || ag->ag_metric < 1) ? HOPCNT_INFINITY : ag->ag_metric); wb->n->n_metric = htonl(wb->n->n_metric); /* Any non-zero bits in the supposedly unused RIPv1 fields * cause the old `routed` to ignore the route. * That means the mask and so forth cannot be sent * in the hybrid RIPv1/RIPv2 mode. */ if (ws.state & WS_ST_RIP2_ALL) { if (ag->ag_nhop != 0 && ((ws.state & WS_ST_QUERY) || (ag->ag_nhop != ws.ifp->int_addr && on_net(ag->ag_nhop, ws.ifp->int_net, ws.ifp->int_mask)))) wb->n->n_nhop = ag->ag_nhop; wb->n->n_mask = htonl(mask); wb->n->n_tag = ag->ag_tag; } dst_h += ddst_h; if (++wb->n >= wb->lim) supply_write(wb); } while (i-- != 0); }
/* ARGSUSED */ static int walk_supply(struct radix_node *rn, __unused struct walkarg *argp) { #define RT ((struct rt_entry *)rn) u_short ags; char metric, pref; naddr dst, nhop; struct rt_spare *rts; int i; /* Do not advertise external remote interfaces or passive interfaces. */ if ((RT->rt_state & RS_IF) && RT->rt_ifp != 0 && (RT->rt_ifp->int_state & IS_PASSIVE) && !(RT->rt_state & RS_MHOME)) return 0; /* If being quiet about our ability to forward, then * do not say anything unless responding to a query, * except about our main interface. */ if (!supplier && !(ws.state & WS_ST_QUERY) && !(RT->rt_state & RS_MHOME)) return 0; dst = RT->rt_dst; /* do not collide with the fake default route */ if (dst == RIP_DEFAULT && (ws.state & WS_ST_DEFAULT)) return 0; if (RT->rt_state & RS_NET_SYN) { if (RT->rt_state & RS_NET_INT) { /* Do not send manual synthetic network routes * into the subnet. */ if (on_net(ws.to.sin_addr.s_addr, ntohl(dst), RT->rt_mask)) return 0; } else { /* Do not send automatic synthetic network routes * if they are not needed because no RIPv1 listeners * can hear them. */ if (ws.state & WS_ST_RIP2_ALL) return 0; /* Do not send automatic synthetic network routes to * the real subnet. */ if (on_net(ws.to.sin_addr.s_addr, ntohl(dst), RT->rt_mask)) return 0; } nhop = 0; } else { /* Advertise the next hop if this is not a route for one * of our interfaces and the next hop is on the same * network as the target. * The final determination is made by supply_out(). */ if (!(RT->rt_state & RS_IF) && RT->rt_gate != myaddr && RT->rt_gate != loopaddr) nhop = RT->rt_gate; else nhop = 0; } metric = RT->rt_metric; ags = 0; if (RT->rt_state & RS_MHOME) { /* retain host route of multi-homed servers */ ; } else if (RT_ISHOST(RT)) { /* We should always suppress (into existing network routes) * the host routes for the local end of our point-to-point * links. * If we are suppressing host routes in general, then do so. * Avoid advertising host routes onto their own network, * where they should be handled by proxy-ARP. */ if ((RT->rt_state & RS_LOCAL) || ridhosts || on_net(dst, ws.to_net, ws.to_mask)) ags |= AGS_SUPPRESS; /* Aggregate stray host routes into network routes if allowed. * We cannot aggregate host routes into small network routes * without confusing RIPv1 listeners into thinking the * network routes are host routes. */ if ((ws.state & WS_ST_AG) && !(ws.state & WS_ST_RIP2_ALL)) ags |= AGS_AGGREGATE; } else { /* Always suppress network routes into other, existing * network routes */ ags |= AGS_SUPPRESS; /* Generate supernets if allowed. * If we can be heard by RIPv1 systems, we will * later convert back to ordinary nets. * This unifies dealing with received supernets. */ if ((ws.state & WS_ST_AG) && ((RT->rt_state & RS_SUBNET) || (ws.state & WS_ST_SUPER_AG))) ags |= AGS_AGGREGATE; } /* Do not send RIPv1 advertisements of subnets to other * networks. If possible, multicast them by RIPv2. */ if ((RT->rt_state & RS_SUBNET) && !(ws.state & WS_ST_RIP2_ALL) && !on_net(dst, ws.to_std_net, ws.to_std_mask)) ags |= AGS_RIPV2 | AGS_AGGREGATE; /* Do not send a route back to where it came from, except in * response to a query. This is "split-horizon". That means not * advertising back to the same network and so via the same interface. * * We want to suppress routes that might have been fragmented * from this route by a RIPv1 router and sent back to us, and so we * cannot forget this route here. Let the split-horizon route * suppress the fragmented routes and then itself be forgotten. * * Include the routes for both ends of point-to-point interfaces * among those suppressed by split-horizon, since the other side * should knows them as well as we do. * * Notice spare routes with the same metric that we are about to * advertise, to split the horizon on redundant, inactive paths. */ if (ws.ifp != 0 && !(ws.state & WS_ST_QUERY) && (ws.state & WS_ST_TO_ON_NET) && (!(RT->rt_state & RS_IF) || ws.ifp->int_if_flags & IFF_POINTOPOINT)) { for (rts = RT->rt_spares, i = NUM_SPARES; i != 0; i--, rts++) { if (rts->rts_metric > metric || rts->rts_ifp != ws.ifp) continue; /* If we do not mark the route with AGS_SPLIT_HZ here, * it will be poisoned-reverse, or advertised back * toward its source with an infinite metric. * If we have recently advertised the route with a * better metric than we now have, then we should * poison-reverse the route before suppressing it for * split-horizon. * * In almost all cases, if there is no spare for the * route then it is either old and dead or a brand * new route. If it is brand new, there is no need * for poison-reverse. If it is old and dead, it * is already poisoned. */ if (RT->rt_poison_time < now_expire || RT->rt_poison_metric >= metric || RT->rt_spares[1].rts_gate == 0) { ags |= AGS_SPLIT_HZ; ags &= ~AGS_SUPPRESS; } metric = HOPCNT_INFINITY; break; } } /* Keep track of the best metric with which the * route has been advertised recently. */ if (RT->rt_poison_metric >= metric || RT->rt_poison_time < now_expire) { RT->rt_poison_time = now.tv_sec; RT->rt_poison_metric = metric; } /* Adjust the outgoing metric by the cost of the link. * Avoid aggregation when a route is counting to infinity. */ pref = RT->rt_poison_metric + ws.metric; metric += ws.metric; /* Do not advertise stable routes that will be ignored, * unless we are answering a query. * If the route recently was advertised with a metric that * would have been less than infinity through this interface, * we need to continue to advertise it in order to poison it. */ if (metric >= HOPCNT_INFINITY) { if (!(ws.state & WS_ST_QUERY) && (pref >= HOPCNT_INFINITY || RT->rt_poison_time < now_garbage)) return 0; metric = HOPCNT_INFINITY; } ag_check(dst, RT->rt_mask, 0, nhop, metric, pref, RT->rt_seqno, RT->rt_tag, ags, supply_out); return 0; #undef RT }
/* use configured parameters */ void get_parms(struct interface *ifp) { static boolean_t warned_auth_in, warned_auth_out; struct parm *parmp; int i, num_passwds = 0; if (ifp == NULL) return; /* get all relevant parameters */ for (parmp = parms; parmp != NULL; parmp = parmp->parm_next) { if (parmp->parm_name[0] == '\0' || strcmp(ifp->int_name, parmp->parm_name) == 0 || (parmp->parm_name[0] == '\n' && on_net(ifp->int_addr, parmp->parm_net, parmp->parm_mask))) { /* * This group of parameters is relevant, * so get its settings */ ifp->int_state |= parmp->parm_int_state; for (i = 0; i < MAX_AUTH_KEYS; i++) { if (parmp->parm_auth[i].type == RIP_AUTH_NONE || num_passwds >= MAX_AUTH_KEYS) break; ifp->int_auth[num_passwds++] = parmp->parm_auth[i]; } if (parmp->parm_rdisc_pref != 0) ifp->int_rdisc_pref = parmp->parm_rdisc_pref; if (parmp->parm_rdisc_int != 0) ifp->int_rdisc_int = parmp->parm_rdisc_int; if (parmp->parm_d_metric != 0) ifp->int_d_metric = parmp->parm_d_metric; if (parmp->parm_ripout_addr != 0) ifp->int_ripout_addr = parmp->parm_ripout_addr; } } /* * Set general defaults. * * Default poor-man's router discovery to a metric that will * be heard by old versions of `routed`. They ignored received * routes with metric 15. */ if ((ifp->int_state & IS_PM_RDISC) && ifp->int_d_metric == 0) ifp->int_d_metric = FAKE_METRIC; if (ifp->int_rdisc_int == 0) ifp->int_rdisc_int = DEF_MAXADVERTISEINTERVAL; if (!(ifp->int_if_flags & IFF_MULTICAST) && !(ifp->int_state & IS_REMOTE)) ifp->int_state |= IS_BCAST_RDISC; if (ifp->int_if_flags & IFF_POINTOPOINT) { ifp->int_state |= IS_BCAST_RDISC; /* * By default, point-to-point links should be passive * about router-discovery for the sake of demand-dialing. */ if (!(ifp->int_state & GROUP_IS_SOL_OUT)) ifp->int_state |= IS_NO_SOL_OUT; if (!(ifp->int_state & GROUP_IS_ADV_OUT)) ifp->int_state |= IS_NO_ADV_OUT; } if (0 != (ifp->int_state & (IS_PASSIVE | IS_REMOTE))) ifp->int_state |= IS_NO_RDISC; if (ifp->int_state & IS_PASSIVE) ifp->int_state |= IS_NO_RIP; if (!IS_RIP_IN_OFF(ifp->int_state) && ifp->int_auth[0].type != RIP_AUTH_NONE && !(ifp->int_state & IS_NO_RIPV1_IN) && !warned_auth_in) { writelog(LOG_WARNING, "RIPv1 input via %s" " will be accepted without authentication", ifp->int_name); warned_auth_in = _B_TRUE; } if (!IS_RIP_OUT_OFF(ifp->int_state) && ifp->int_auth[0].type != RIP_AUTH_NONE && !(ifp->int_state & IS_NO_RIPV1_OUT)) { if (!warned_auth_out) { writelog(LOG_WARNING, "RIPv1 output via %s" " will be sent without authentication", ifp->int_name); warned_auth_out = _B_TRUE; } } /* * If not overriden by the rip_neighbor option, set the * default address to which RIP packets will be sent on * this interface. */ if (ifp->int_ripout_addr == 0) { if (ifp->int_state & IS_REMOTE) { /* * By definition we always send RIP packets to * the address assigned to a remote interface. */ ifp->int_ripout_addr = ifp->int_addr; } else if ((ifp->int_state & IS_NO_RIPV1_OUT) && (ifp->int_if_flags & IFF_MULTICAST) && !(ifp->int_state & IS_NO_RIP_MCAST)) { /* * If the interface is being used for RIPv2 * and it supports multicast, and if the user * has not explicitely turned off multicast * RIP output, send to the all RIP routers * multicast address. */ ifp->int_ripout_addr = htonl(INADDR_RIP_GROUP); } else if (ifp->int_if_flags & IFF_POINTOPOINT) { /* * For point-to-point interfaces which don't * fall into the two categories above, just * send to the destination address of the * interface. */ ifp->int_ripout_addr = ifp->int_dstaddr; } else { /* Otherwise, use the broadcast address. */ ifp->int_ripout_addr = ifp->int_brdaddr; } } }