#include "defs.h"
u_int update_seqno;
struct {
struct sockaddr_in to;
naddr to_mask;
naddr to_net;
naddr to_std_mask;
naddr to_std_net;
struct interface *ifp;
struct auth *a;
char metric;
int npackets;
int gen_limit;
u_int state;
#define WS_ST_FLASH 0x001
#define WS_ST_RIP2_ALL 0x002
#define WS_ST_AG 0x004
#define WS_ST_SUPER_AG 0x008
#define WS_ST_QUERY 0x010
#define WS_ST_TO_ON_NET 0x020
#define WS_ST_DEFAULT 0x040
} ws;
struct ws_buf v12buf;
union pkt_buf ripv12_buf;
struct ws_buf v2buf;
union pkt_buf rip_v2_buf;
void
bufinit(void)
{
ripv12_buf.rip.rip_cmd = RIPCMD_RESPONSE;
v12buf.buf = &ripv12_buf.rip;
v12buf.base = &v12buf.buf->rip_nets[0];
rip_v2_buf.rip.rip_cmd = RIPCMD_RESPONSE;
rip_v2_buf.rip.rip_vers = RIPv2;
v2buf.buf = &rip_v2_buf.rip;
v2buf.base = &v2buf.buf->rip_nets[0];
}
int
output(enum output_type type,
struct sockaddr_in *dst,
struct interface *ifp,
struct rip *buf,
int size)
{
struct sockaddr_in in;
int flags;
const char *msg;
int res;
naddr tgt_mcast;
int soc;
int serrno;
in = *dst;
if (in.sin_port == 0)
in.sin_port = htons(RIP_PORT);
if (in.sin_len == 0)
in.sin_len = sizeof(in);
soc = rip_sock;
flags = 0;
switch (type) {
case OUT_QUERY:
msg = "Answer Query";
if (soc < 0)
soc = ifp->int_rip_sock;
break;
case OUT_UNICAST:
msg = "Send";
if (soc < 0)
soc = ifp->int_rip_sock;
flags = MSG_DONTROUTE;
break;
case OUT_BROADCAST:
if (ifp->int_if_flags & IFF_POINTOPOINT) {
msg = "Send";
} else {
msg = "Send bcast";
}
flags = MSG_DONTROUTE;
break;
case OUT_MULTICAST:
if (ifp->int_if_flags & IFF_POINTOPOINT) {
msg = "Send pt-to-pt";
} else if (ifp->int_state & IS_DUP) {
trace_act("abort multicast output via %s"
" with duplicate address",
ifp->int_name);
return 0;
} else {
msg = "Send mcast";
if (rip_sock_mcast != ifp) {
#ifdef MCAST_PPP_BUG
if (ifp->int_addr == myaddr) {
tgt_mcast = 0;
} else
#endif
tgt_mcast = ifp->int_addr;
if (0 > setsockopt(rip_sock,
IPPROTO_IP, IP_MULTICAST_IF,
&tgt_mcast,
sizeof(tgt_mcast))) {
serrno = errno;
LOGERR("setsockopt(rip_sock,"
"IP_MULTICAST_IF)");
errno = serrno;
ifp = NULL;
return -1;
}
rip_sock_mcast = ifp;
}
in.sin_addr.s_addr = htonl(INADDR_RIP_GROUP);
}
break;
case NO_OUT_MULTICAST:
case NO_OUT_RIPV2:
default:
#ifdef DEBUG
abort();
#endif
return -1;
}
trace_rip(msg, "to", &in, ifp, buf, size);
res = sendto(soc, buf, size, flags,
(struct sockaddr *)&in, sizeof(in));
if (res < 0
&& (ifp == NULL || !(ifp->int_state & IS_BROKE))) {
serrno = errno;
msglog("%s sendto(%s%s%s.%d): %s", msg,
ifp != NULL ? ifp->int_name : "",
ifp != NULL ? ", " : "",
inet_ntoa(in.sin_addr),
ntohs(in.sin_port),
strerror(errno));
errno = serrno;
}
return res;
}
struct auth *
find_auth(struct interface *ifp)
{
struct auth *ap, *res;
int i;
if (ifp == NULL)
return 0;
res = NULL;
ap = ifp->int_auth;
for (i = 0; i < MAX_AUTH_KEYS; i++, ap++) {
if (ap->type == RIP_AUTH_NONE)
break;
if ((u_long)ap->start > (u_long)clk.tv_sec)
continue;
if ((u_long)ap->end < (u_long)clk.tv_sec) {
if (res == NULL || (u_long)ap->end > (u_long)res->end)
res = ap;
continue;
}
if (res == NULL || (u_long)res->end < (u_long)ap->end)
res = ap;
}
return res;
}
void
clr_ws_buf(struct ws_buf *wb,
struct auth *ap)
{
struct netauth *na;
wb->lim = wb->base + NETS_LEN;
wb->n = wb->base;
memset(wb->n, 0, NETS_LEN*sizeof(*wb->n));
if (ap == NULL)
return;
na = (struct netauth*)wb->n;
if (ap->type == RIP_AUTH_PW) {
na->a_family = RIP_AF_AUTH;
na->a_type = RIP_AUTH_PW;
memcpy(na->au.au_pw, ap->key, sizeof(na->au.au_pw));
wb->n++;
} else if (ap->type == RIP_AUTH_MD5) {
na->a_family = RIP_AF_AUTH;
na->a_type = RIP_AUTH_MD5;
na->au.a_md5.md5_keyid = ap->keyid;
na->au.a_md5.md5_auth_len = RIP_AUTH_MD5_LEN;
na->au.a_md5.md5_seqno = htonl(clk.tv_sec);
wb->n++;
wb->lim--;
}
}
void
end_md5_auth(struct ws_buf *wb,
struct auth *ap)
{
struct netauth *na, *na2;
MD5_CTX md5_ctx;
int len;
na = (struct netauth*)wb->base;
na2 = (struct netauth*)wb->n;
len = (char *)na2-(char *)wb->buf;
na2->a_family = RIP_AF_AUTH;
na2->a_type = htons(1);
na->au.a_md5.md5_pkt_len = htons(len);
MD5_Init(&md5_ctx);
MD5_Update(&md5_ctx, (u_char *)wb->buf, len);
MD5_Update(&md5_ctx, ap->key, RIP_AUTH_MD5_LEN);
MD5_Final(na2->au.au_pw, &md5_ctx);
wb->n++;
}
static void
supply_write(struct ws_buf *wb)
{
switch (wb->type) {
case NO_OUT_MULTICAST:
trace_pkt("skip multicast to %s because impossible",
naddr_ntoa(ws.to.sin_addr.s_addr));
break;
case NO_OUT_RIPV2:
break;
default:
if (ws.a != NULL && ws.a->type == RIP_AUTH_MD5)
end_md5_auth(wb,ws.a);
if (output(wb->type, &ws.to, ws.ifp, wb->buf,
((char *)wb->n - (char*)wb->buf)) < 0
&& ws.ifp != NULL)
if_sick(ws.ifp);
ws.npackets++;
break;
}
clr_ws_buf(wb,ws.a);
}
static void
supply_out(struct ag_info *ag)
{
int i;
naddr mask, v1_mask, dst_h, ddst_h = 0;
struct ws_buf *wb;
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 ((ws.state & WS_ST_RIP2_ALL)
|| ((ag->ag_state & AGS_RIPV2) && v1_mask != mask)) {
wb = &v2buf;
} else {
wb = &v12buf;
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) {
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);
wb->n->n_metric = ((stopint || ag->ag_metric < 1)
? HOPCNT_INFINITY
: ag->ag_metric);
wb->n->n_metric = htonl(wb->n->n_metric);
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);
}
static int
walk_supply(struct radix_node *rn, void *argp __unused)
{
#define RT ((struct rt_entry *)rn)
u_short ags;
char metric, pref;
naddr dst, nhop;
struct rt_spare *rts;
int i;
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 (!supplier && !(ws.state & WS_ST_QUERY)
&& !(RT->rt_state & RS_MHOME))
return 0;
dst = RT->rt_dst;
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) {
if (on_net(ws.to.sin_addr.s_addr,
ntohl(dst), RT->rt_mask))
return 0;
} else {
if (ws.state & WS_ST_RIP2_ALL)
return 0;
if (on_net(ws.to.sin_addr.s_addr,
ntohl(dst), RT->rt_mask))
return 0;
}
nhop = 0;
} else {
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) {
;
} else if (RT_ISHOST(RT)) {
if ((RT->rt_state & RS_LOCAL)
|| ridhosts
|| on_net(dst, ws.to_net, ws.to_mask))
ags |= AGS_SUPPRESS;
if ((ws.state & WS_ST_AG)
&& !(ws.state & WS_ST_RIP2_ALL))
ags |= AGS_AGGREGATE;
} else {
ags |= AGS_SUPPRESS;
if ((ws.state & WS_ST_AG)
&& ((RT->rt_state & RS_SUBNET)
|| (ws.state & WS_ST_SUPER_AG)))
ags |= AGS_AGGREGATE;
}
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;
if (ws.ifp != NULL
&& !(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 (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;
}
}
if (RT->rt_poison_metric >= metric
|| RT->rt_poison_time < now_expire) {
RT->rt_poison_time = now.tv_sec;
RT->rt_poison_metric = metric;
}
pref = RT->rt_poison_metric + ws.metric;
metric += ws.metric;
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
}
void
supply(struct sockaddr_in *dst,
struct interface *ifp,
enum output_type type,
int flash,
int vers,
int passwd_ok)
{
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 {
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;
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;
}
if (vers == RIPv2) {
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);
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 ((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++;
}
}
rhead->rnh_walktree(rhead, walk_supply, NULL);
ag_flush(0, 0, supply_out);
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 (ws.npackets == 0
&& (ws.state & WS_ST_QUERY))
supply_write(&v12buf);
}
void
rip_bcast(int flash)
{
static struct sockaddr_in dst = {sizeof(dst), AF_INET, 0, {0}, {0}};
struct interface *ifp;
enum output_type type;
int vers;
struct timeval rtime;
need_flash = 0;
intvl_random(&rtime, MIN_WAITTIME, MAX_WAITTIME);
no_flash = rtime;
timevaladd(&no_flash, &now);
if (rip_sock < 0)
return;
trace_act("send %s and inhibit dynamic updates for %.3f sec",
flash ? "dynamic update" : "all routes",
rtime.tv_sec + ((float)rtime.tv_usec)/1000000.0);
for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
if (IS_RIP_OUT_OFF(ifp->int_state))
continue;
if (!iff_up(ifp->int_if_flags))
continue;
vers = (ifp->int_state & IS_NO_RIPV1_OUT) ? RIPv2 : RIPv1;
if (ifp->int_if_flags & IFF_BROADCAST) {
dst.sin_addr.s_addr = ifp->int_brdaddr;
if (vers == RIPv2
&& !(ifp->int_state & IS_NO_RIP_MCAST)) {
type = OUT_MULTICAST;
} else {
type = OUT_BROADCAST;
}
} else if (ifp->int_if_flags & IFF_POINTOPOINT) {
dst.sin_addr.s_addr = ifp->int_dstaddr;
type = OUT_UNICAST;
} else if (ifp->int_state & IS_REMOTE) {
dst.sin_addr.s_addr = ifp->int_addr;
type = OUT_UNICAST;
} else {
continue;
}
supply(&dst, ifp, type, flash, vers, 1);
}
update_seqno++;
}
void
rip_query(void)
{
static struct sockaddr_in dst = {sizeof(dst), AF_INET, 0, {0}, {0}};
struct interface *ifp;
struct rip buf;
enum output_type type;
if (rip_sock < 0)
return;
memset(&buf, 0, sizeof(buf));
for (ifp = ifnet; ifp; ifp = ifp->int_next) {
if (IS_RIP_IN_OFF(ifp->int_state)
|| ifp->int_query_time != NEVER)
continue;
if (!iff_up(ifp->int_if_flags))
continue;
buf.rip_vers = (ifp->int_state&IS_NO_RIPV1_OUT) ? RIPv2:RIPv1;
buf.rip_cmd = RIPCMD_REQUEST;
buf.rip_nets[0].n_family = RIP_AF_UNSPEC;
buf.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
if ((ifp->int_state & IS_NO_RIPV1_OUT)
|| (ifp->int_state & IS_NO_RIPV1_IN)) {
buf.rip_vers = RIPv2;
} else {
buf.rip_vers = RIPv1;
}
if (ifp->int_if_flags & IFF_BROADCAST) {
dst.sin_addr.s_addr = ifp->int_brdaddr;
if (buf.rip_vers == RIPv2
&& (ifp->int_if_flags & IFF_MULTICAST)
&& !(ifp->int_state & IS_NO_RIP_MCAST)) {
type = OUT_MULTICAST;
} else {
type = OUT_BROADCAST;
}
} else if (ifp->int_if_flags & IFF_POINTOPOINT) {
dst.sin_addr.s_addr = ifp->int_dstaddr;
type = OUT_UNICAST;
} else if (ifp->int_state & IS_REMOTE) {
dst.sin_addr.s_addr = ifp->int_addr;
type = OUT_UNICAST;
} else {
continue;
}
ifp->int_query_time = now.tv_sec+SUPPLY_INTERVAL;
if (output(type, &dst, ifp, &buf, sizeof(buf)) < 0)
if_sick(ifp);
}
}