diff --git a/usr.sbin/mrouted/cfparse.y b/usr.sbin/mrouted/cfparse.y index f244d9c2aaf6..2f743992c98a 100644 --- a/usr.sbin/mrouted/cfparse.y +++ b/usr.sbin/mrouted/cfparse.y @@ -1,932 +1,932 @@ %{ /* * Configuration file parser for mrouted. * * Written by Bill Fenner, NRL, 1994 * * $FreeBSD$ * cfparse.y,v 3.8.4.30 1998/03/01 01:48:58 fenner Exp */ #include #ifdef __STDC__ #include #else #include #endif #include "defs.h" #include /* * Local function declarations */ static void fatal __P((char *fmt, ...)); static void warn __P((char *fmt, ...)); static void yyerror __P((char *s)); static char * next_word __P((void)); static int yylex __P((void)); static u_int32 valid_if __P((char *s)); static struct ifreq * ifconfaddr __P((struct ifconf *ifcp, u_int32 a)); int yyparse __P((void)); static FILE *f; char *configfilename = _PATH_MROUTED_CONF; extern int cache_lifetime; extern int prune_lifetime; /* imported from config.c, with slight memory leak */ extern struct ifconf ifc; int allow_black_holes = 0; static int lineno; static struct uvif *v; static int order, state; static int noflood = 0; static int rexmit = VIFF_REXMIT_PRUNES; struct addrmask { u_int32 addr; int mask; }; struct boundnam { char *name; struct addrmask bound; }; #define MAXBOUNDS 20 struct boundnam boundlist[MAXBOUNDS]; /* Max. of 20 named boundaries */ int numbounds = 0; /* Number of named boundaries */ %} %union { int num; char *ptr; struct addrmask addrmask; u_int32 addr; struct vf_element *filterelem; }; %token CACHE_LIFETIME PRUNE_LIFETIME PRUNING BLACK_HOLE NOFLOOD %token PHYINT TUNNEL NAME %token DISABLE IGMPV1 SRCRT BESIDE %token METRIC THRESHOLD RATE_LIMIT BOUNDARY NETMASK ALTNET ADVERT_METRIC %token FILTER ACCEPT DENY EXACT BIDIR REXMIT_PRUNES REXMIT_PRUNES2 %token PASSIVE ALLOW_NONPRUNERS %token NOTRANSIT BLASTER FORCE_LEAF %token PRUNE_LIFETIME2 NOFLOOD2 %token SYSNAM SYSCONTACT SYSVERSION SYSLOCATION %token BOOLEAN %token NUMBER %token STRING %token ADDRMASK %token ADDR %type interface addrname %type bound boundary addrmask %type filter filtlist filtelement filtelem %start conf %% conf : stmts ; stmts : /* Empty */ | stmts stmt ; stmt : error | PHYINT interface { vifi_t vifi; state++; if (order) fatal("phyints must appear before tunnels"); for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) if (!(v->uv_flags & VIFF_TUNNEL) && $2 == v->uv_lcl_addr) break; if (vifi == numvifs) fatal("%s is not a configured interface", inet_fmt($2,s1)); } ifmods | TUNNEL interface addrname { struct ifreq *ifr; struct ifreq ffr; vifi_t vifi; order++; ifr = ifconfaddr(&ifc, $2); if (ifr == 0) fatal("Tunnel local address %s is not mine", inet_fmt($2, s1)); if (((ntohl($2) & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) fatal("Tunnel local address %s is a loopback address", inet_fmt($2, s1)); if (ifconfaddr(&ifc, $3) != 0) fatal("Tunnel remote address %s is one of mine", inet_fmt($3, s1)); for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) if (v->uv_flags & VIFF_TUNNEL) { if ($3 == v->uv_rmt_addr) fatal("Duplicate tunnel to %s", inet_fmt($3, s1)); } else if (!(v->uv_flags & VIFF_DISABLED)) { if (($3 & v->uv_subnetmask) == v->uv_subnet) fatal("Unnecessary tunnel to %s, same subnet as vif %d (%s)", inet_fmt($3,s1), vifi, v->uv_name); } if (numvifs == MAXVIFS) fatal("too many vifs"); strncpy(ffr.ifr_name, ifr->ifr_name, IFNAMSIZ); if (ioctl(udp_socket, SIOCGIFFLAGS, (char *)&ffr)<0) fatal("ioctl SIOCGIFFLAGS on %s", ffr.ifr_name); v = &uvifs[numvifs]; zero_vif(v, 1); v->uv_flags = VIFF_TUNNEL | rexmit | noflood; v->uv_flags |= VIFF_OTUNNEL; /*XXX*/ v->uv_lcl_addr = $2; v->uv_rmt_addr = $3; v->uv_dst_addr = $3; strncpy(v->uv_name, ffr.ifr_name, IFNAMSIZ); v->uv_name[IFNAMSIZ-1]='\0'; if (!(ffr.ifr_flags & IFF_UP)) { v->uv_flags |= VIFF_DOWN; vifs_down = TRUE; } } tunnelmods { if (!(v->uv_flags & VIFF_OTUNNEL)) { init_ipip_on_vif(v); } log(LOG_INFO, 0, "installing tunnel from %s to %s as vif #%u - rate=%d", inet_fmt($2, s1), inet_fmt($3, s2), numvifs, v->uv_rate_limit); ++numvifs; } | CACHE_LIFETIME NUMBER { if ($2 < MIN_CACHE_LIFETIME) { warn("cache_lifetime %d must be at least %d", $2, MIN_CACHE_LIFETIME); } else { cache_lifetime = $2; } } | PRUNE_LIFETIME NUMBER { if ($2 < MIN_PRUNE_LIFETIME) { warn("prune_lifetime %d must be at least %d", $2, MIN_PRUNE_LIFETIME); } else { prune_lifetime = $2; } } | PRUNING BOOLEAN { if ($2 != 1) { warn("Disabling pruning is no longer supported"); } } | BLACK_HOLE { #ifdef ALLOW_BLACK_HOLES allow_black_holes = 1; #endif } /* * Turn off initial flooding (until subordinateness is learned * via route exchange) on all phyints and set the default for * all further tunnels. */ | NOFLOOD { vifi_t vifi; noflood = VIFF_NOFLOOD; for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) v->uv_flags |= VIFF_NOFLOOD; } /* * Turn on prune retransmission on all interfaces. * Tunnels default to retransmitting, so this just * needs to turn on phyints. */ | REXMIT_PRUNES { vifi_t vifi; for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) v->uv_flags |= VIFF_REXMIT_PRUNES; } /* * If true, do as above. If false, no need to turn * it off for phyints since they default to not * rexmit; need to set flag to not rexmit on tunnels. */ | REXMIT_PRUNES BOOLEAN { if ($2) { vifi_t vifi; for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) v->uv_flags |= VIFF_REXMIT_PRUNES; } else { rexmit = 0; } } | NAME STRING boundary { if (numbounds >= MAXBOUNDS) { fatal("Too many named boundaries (max %d)", MAXBOUNDS); } boundlist[numbounds].name = malloc(strlen($2) + 1); strcpy(boundlist[numbounds].name, $2); boundlist[numbounds++].bound = $3; } | SYSNAM STRING { #ifdef SNMP set_sysName($2); #endif /* SNMP */ } | SYSCONTACT STRING { #ifdef SNMP set_sysContact($2); #endif /* SNMP */ } | SYSVERSION STRING { #ifdef SNMP set_sysVersion($2); #endif /* SNMP */ } | SYSLOCATION STRING { #ifdef SNMP set_sysLocation($2); #endif /* SNMP */ } ; tunnelmods : /* empty */ | tunnelmods tunnelmod ; tunnelmod : mod | BESIDE { v->uv_flags |= VIFF_OTUNNEL; } | BESIDE BOOLEAN { if ($2) { v->uv_flags |= VIFF_OTUNNEL; } else { v->uv_flags &= ~VIFF_OTUNNEL; } } | SRCRT { fatal("Source-route tunnels not supported"); } ; ifmods : /* empty */ | ifmods ifmod ; ifmod : mod | DISABLE { v->uv_flags |= VIFF_DISABLED; } | IGMPV1 { v->uv_flags |= VIFF_IGMPV1; } | NETMASK addrname { u_int32 subnet, mask; mask = $2; subnet = v->uv_lcl_addr & mask; if (!inet_valid_subnet(subnet, mask)) fatal("Invalid netmask"); v->uv_subnet = subnet; v->uv_subnetmask = mask; v->uv_subnetbcast = subnet | ~mask; } | NETMASK { warn("Expected address after netmask keyword, ignored"); } | ALTNET addrmask { struct phaddr *ph; ph = (struct phaddr *)malloc(sizeof(struct phaddr)); if (ph == NULL) fatal("out of memory"); if ($2.mask) { VAL_TO_MASK(ph->pa_subnetmask, $2.mask); } else ph->pa_subnetmask = v->uv_subnetmask; ph->pa_subnet = $2.addr & ph->pa_subnetmask; ph->pa_subnetbcast = ph->pa_subnet | ~ph->pa_subnetmask; if ($2.addr & ~ph->pa_subnetmask) warn("Extra subnet %s/%d has host bits set", inet_fmt($2.addr,s1), $2.mask); ph->pa_next = v->uv_addrs; v->uv_addrs = ph; } | ALTNET { warn("Expected address after altnet keyword, ignored"); } | FORCE_LEAF { v->uv_flags |= VIFF_FORCE_LEAF; } | FORCE_LEAF BOOLEAN { if ($2) { v->uv_flags |= VIFF_FORCE_LEAF; } else { v->uv_flags &= ~VIFF_FORCE_LEAF; } } ; mod : THRESHOLD NUMBER { if ($2 < 1 || $2 > 255) fatal("Invalid threshold %d",$2); v->uv_threshold = $2; } | THRESHOLD { warn("Expected number after threshold keyword, ignored"); } | METRIC NUMBER { if ($2 < 1 || $2 > UNREACHABLE) fatal("Invalid metric %d",$2); v->uv_metric = $2; } | METRIC { warn("Expected number after metric keyword, ignored"); } | ADVERT_METRIC NUMBER { if ($2 < 0 || $2 > UNREACHABLE - 1) fatal("Invalid advert_metric %d", $2); v->uv_admetric = $2; } | ADVERT_METRIC { warn("Expected number after advert_metric keyword, ignored"); } | RATE_LIMIT NUMBER { if ($2 > MAX_RATE_LIMIT) fatal("Invalid rate_limit %d",$2); v->uv_rate_limit = $2; } | RATE_LIMIT { warn("Expected number after rate_limit keyword, ignored"); } | BOUNDARY bound { struct vif_acl *v_acl; v_acl = (struct vif_acl *)malloc(sizeof(struct vif_acl)); if (v_acl == NULL) fatal("out of memory"); VAL_TO_MASK(v_acl->acl_mask, $2.mask); v_acl->acl_addr = $2.addr & v_acl->acl_mask; if ($2.addr & ~v_acl->acl_mask) warn("Boundary spec %s/%d has host bits set", inet_fmt($2.addr,s1),$2.mask); v_acl->acl_next = v->uv_acl; v->uv_acl = v_acl; } | BOUNDARY { warn("Expected boundary spec after boundary keyword, ignored"); } | REXMIT_PRUNES2 { v->uv_flags |= VIFF_REXMIT_PRUNES; } | REXMIT_PRUNES2 BOOLEAN { if ($2) { v->uv_flags |= VIFF_REXMIT_PRUNES; } else { v->uv_flags &= ~VIFF_REXMIT_PRUNES; } } | PASSIVE { v->uv_flags |= VIFF_PASSIVE; } | NOFLOOD2 { v->uv_flags |= VIFF_NOFLOOD; } | NOTRANSIT { v->uv_flags |= VIFF_NOTRANSIT; } | BLASTER { v->uv_flags |= VIFF_BLASTER; blaster_alloc(v - uvifs); } | ALLOW_NONPRUNERS { v->uv_flags |= VIFF_ALLOW_NONPRUNERS; } | PRUNE_LIFETIME2 NUMBER { if ($2 < MIN_PRUNE_LIFETIME) { warn("prune_lifetime %d must be at least %d", $2, MIN_PRUNE_LIFETIME); } else { v->uv_prune_lifetime = $2; } } | ACCEPT filter { if (v->uv_filter == NULL) { struct vif_filter *v_filter; v_filter = (struct vif_filter *)malloc(sizeof(struct vif_filter)); if (v_filter == NULL) fatal("out of memory"); v_filter->vf_flags = 0; v_filter->vf_type = VFT_ACCEPT; v_filter->vf_filter = $2; v->uv_filter = v_filter; } else if (v->uv_filter->vf_type != VFT_ACCEPT) { fatal("can't accept and deny"); } else { struct vf_element *p; p = v->uv_filter->vf_filter; while (p->vfe_next) p = p->vfe_next; p->vfe_next = $2; } } | ACCEPT { warn("Expected filter spec after accept keyword, ignored"); } | DENY filter { if (v->uv_filter == NULL) { struct vif_filter *v_filter; v_filter = (struct vif_filter *)malloc(sizeof(struct vif_filter)); if (v_filter == NULL) fatal("out of memory"); v_filter->vf_flags = 0; v_filter->vf_type = VFT_DENY; v_filter->vf_filter = $2; v->uv_filter = v_filter; } else if (v->uv_filter->vf_type != VFT_DENY) { fatal("can't accept and deny"); } else { struct vf_element *p; p = v->uv_filter->vf_filter; while (p->vfe_next) p = p->vfe_next; p->vfe_next = $2; } } | DENY { warn("Expected filter spec after deny keyword, ignored"); } | BIDIR { if (v->uv_filter == NULL) { fatal("bidir goes after filters"); } v->uv_filter->vf_flags |= VFF_BIDIR; } ; interface : ADDR { $$ = $1; } | STRING { $$ = valid_if($1); if ($$ == 0) fatal("Invalid interface name %s",$1); } ; addrname : ADDR { $$ = $1; } | STRING { struct hostent *hp; if ((hp = gethostbyname($1)) == NULL || hp->h_length != sizeof($$)) fatal("No such host %s", $1); if (hp->h_addr_list[1]) fatal("Hostname %s does not %s", $1, "map to a unique address"); bcopy(hp->h_addr_list[0], &$$, hp->h_length); } bound : boundary { $$ = $1; } | STRING { int i; for (i=0; i < numbounds; i++) { if (!strcmp(boundlist[i].name, $1)) { $$ = boundlist[i].bound; break; } } if (i == numbounds) { fatal("Invalid boundary name %s",$1); } } ; boundary : ADDRMASK { #ifdef ALLOW_BLACK_HOLES if (!allow_black_holes) #endif if ((ntohl($1.addr) & 0xff000000) != 0xef000000) { fatal("Boundaries must be 239.x.x.x, not %s/%d", inet_fmt($1.addr, s1), $1.mask); } $$ = $1; } ; addrmask : ADDRMASK { $$ = $1; } | ADDR { $$.addr = $1; $$.mask = 0; } ; filter : filtlist { $$ = $1; } | STRING { fatal("named filters no implemented yet"); } ; filtlist : filtelement { $$ = $1; } | filtelement filtlist { $1->vfe_next = $2; $$ = $1; } ; filtelement : filtelem { $$ = $1; } | filtelem EXACT { $1->vfe_flags |= VFEF_EXACT; $$ = $1; } ; filtelem : ADDRMASK { struct vf_element *vfe; vfe = (struct vf_element *)malloc(sizeof(struct vf_element)); if (vfe == NULL) fatal("out of memory"); vfe->vfe_addr = $1.addr; VAL_TO_MASK(vfe->vfe_mask, $1.mask); vfe->vfe_flags = 0; vfe->vfe_next = NULL; $$ = vfe; } %% #ifdef __STDC__ static void fatal(char *fmt, ...) { va_list ap; - char buf[200]; + char buf[MAXHOSTNAMELEN + 100]; va_start(ap, fmt); #else /*VARARGS1*/ static void fatal(fmt, va_alist) char *fmt; va_dcl { va_list ap; - char buf[200]; + char buf[MAXHOSTNAMELEN + 100]; va_start(ap); #endif - vsprintf(buf, fmt, ap); + vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); log(LOG_ERR,0,"%s: %s near line %d", configfilename, buf, lineno); } #ifdef __STDC__ static void warn(char *fmt, ...) { va_list ap; char buf[200]; va_start(ap, fmt); #else /*VARARGS1*/ static void warn(fmt, va_alist) char *fmt; va_dcl { va_list ap; char buf[200]; va_start(ap); #endif - vsprintf(buf, fmt, ap); + vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); log(LOG_WARNING,0,"%s: %s near line %d", configfilename, buf, lineno); } static void yyerror(s) char *s; { log(LOG_ERR, 0, "%s: %s near line %d", configfilename, s, lineno); } static char * next_word() { static char buf[1024]; static char *p=NULL; char *q; while (1) { if (!p || !*p) { lineno++; if (fgets(buf, sizeof(buf), f) == NULL) return NULL; p = buf; } while (*p && (*p == ' ' || *p == '\t')) /* skip whitespace */ p++; if (*p == '#') { p = NULL; /* skip comments */ continue; } q = p; #ifdef SNMP if (*p == '"') { p++; while (*p && *p != '"' && *p != '\n') p++; /* find next whitespace */ if (*p == '"') p++; } else #endif while (*p && *p != ' ' && *p != '\t' && *p != '\n') p++; /* find next whitespace */ *p++ = '\0'; /* null-terminate string */ if (!*q) { p = NULL; continue; /* if 0-length string, read another line */ } return q; } } /* * List of keywords. Must have an empty record at the end to terminate * list. If a second value is specified, the first is used at the beginning * of the file and the second is used while parsing interfaces (e.g. after * the first "phyint" or "tunnel" keyword). */ static struct keyword { char *word; int val1; int val2; } words[] = { { "cache_lifetime", CACHE_LIFETIME }, { "prune_lifetime", PRUNE_LIFETIME, PRUNE_LIFETIME2 }, { "pruning", PRUNING }, { "phyint", PHYINT }, { "tunnel", TUNNEL }, { "disable", DISABLE }, { "metric", METRIC }, { "advert_metric", ADVERT_METRIC }, { "threshold", THRESHOLD }, { "rate_limit", RATE_LIMIT }, { "force_leaf", FORCE_LEAF }, { "srcrt", SRCRT }, { "sourceroute", SRCRT }, { "boundary", BOUNDARY }, { "netmask", NETMASK }, { "igmpv1", IGMPV1 }, { "altnet", ALTNET }, { "name", NAME }, { "accept", ACCEPT }, { "deny", DENY }, { "exact", EXACT }, { "bidir", BIDIR }, { "allow_nonpruners", ALLOW_NONPRUNERS }, #ifdef ALLOW_BLACK_HOLES { "allow_black_holes", BLACK_HOLE }, #endif { "noflood", NOFLOOD, NOFLOOD2}, { "notransit", NOTRANSIT }, { "blaster", BLASTER }, { "rexmit_prunes", REXMIT_PRUNES, REXMIT_PRUNES2 }, { "passive", PASSIVE }, { "beside", BESIDE }, #ifdef SNMP { "sysName", SYSNAM }, { "sysContact", SYSCONTACT }, { "sysVersion", SYSVERSION }, { "sysLocation", SYSLOCATION }, #endif { NULL, 0 } }; static int yylex() { int n; u_int32 addr; char *q; struct keyword *w; if ((q = next_word()) == NULL) { return 0; } for (w = words; w->word; w++) if (!strcmp(q, w->word)) return (state && w->val2) ? w->val2 : w->val1; if (!strcmp(q,"on") || !strcmp(q,"yes")) { yylval.num = 1; return BOOLEAN; } if (!strcmp(q,"off") || !strcmp(q,"no")) { yylval.num = 0; return BOOLEAN; } if (!strcmp(q,"default")) { yylval.addrmask.mask = 0; yylval.addrmask.addr = 0; return ADDRMASK; } if (sscanf(q,"%[.0-9]/%d%c",s1,&n,s2) == 2) { if ((addr = inet_parse(s1,1)) != 0xffffffff) { yylval.addrmask.mask = n; yylval.addrmask.addr = addr; return ADDRMASK; } /* fall through to returning STRING */ } if (sscanf(q,"%[.0-9]%c",s1,s2) == 1) { if ((addr = inet_parse(s1,4)) != 0xffffffff && inet_valid_host(addr)) { yylval.addr = addr; return ADDR; } } if (sscanf(q,"0x%8x%c",&n,s1) == 1) { yylval.addr = n; return ADDR; } if (sscanf(q,"%d%c",&n,s1) == 1) { yylval.num = n; return NUMBER; } #ifdef SNMP if (*q=='"') { if (q[ strlen(q)-1 ]=='"') q[ strlen(q)-1 ]='\0'; /* trash trailing quote */ yylval.ptr = q+1; return STRING; } #endif yylval.ptr = q; return STRING; } void config_vifs_from_file() { order = 0; state = 0; numbounds = 0; lineno = 0; if ((f = fopen(configfilename, "r")) == NULL) { if (errno != ENOENT) log(LOG_ERR, errno, "can't open %s", configfilename); return; } yyparse(); fclose(f); } static u_int32 valid_if(s) char *s; { register vifi_t vifi; register struct uvif *v; for (vifi=0, v=uvifs; vifiuv_name, s)) return v->uv_lcl_addr; return 0; } static struct ifreq * ifconfaddr(ifcp, a) struct ifconf *ifcp; u_int32 a; { int n; struct ifreq *ifrp = (struct ifreq *)ifcp->ifc_buf; struct ifreq *ifend = (struct ifreq *)((char *)ifrp + ifcp->ifc_len); while (ifrp < ifend) { if (ifrp->ifr_addr.sa_family == AF_INET && ((struct sockaddr_in *)&ifrp->ifr_addr)->sin_addr.s_addr == a) return (ifrp); #ifdef HAVE_SA_LEN n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name); if (n < sizeof(*ifrp)) ++ifrp; else ifrp = (struct ifreq *)((char *)ifrp + n); #else ++ifrp; #endif } return (0); } diff --git a/usr.sbin/mrouted/main.c b/usr.sbin/mrouted/main.c index b3526d5934dc..f3fc676c3399 100644 --- a/usr.sbin/mrouted/main.c +++ b/usr.sbin/mrouted/main.c @@ -1,1061 +1,1059 @@ /* * The mrouted program is covered by the license in the accompanying file * named "LICENSE". Use of the mrouted program represents acceptance of * the terms and conditions listed in that file. * * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of * Leland Stanford Junior University. * * * main.c,v 3.8.4.29 1998/03/01 01:49:00 fenner Exp */ /* * Written by Steve Deering, Stanford University, February 1989. * * (An earlier version of DVMRP was implemented by David Waitzman of * BBN STC by extending Berkeley's routed program. Some of Waitzman's * extensions have been incorporated into mrouted, but none of the * original routed code has been adopted.) */ #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif #include #include "defs.h" #ifdef __STDC__ #include #else #include #endif #include #ifdef SNMP #include "snmp.h" #endif extern char *configfilename; char versionstring[100]; static char pidfilename[] = _PATH_MROUTED_PID; static char dumpfilename[] = _PATH_MROUTED_DUMP; static char cachefilename[] = _PATH_MROUTED_CACHE; static char genidfilename[] = _PATH_MROUTED_GENID; static int haveterminal = 1; int did_final_init = 0; static int sighandled = 0; #define GOT_SIGINT 0x01 #define GOT_SIGHUP 0x02 #define GOT_SIGUSR1 0x04 #define GOT_SIGUSR2 0x08 int cache_lifetime = DEFAULT_CACHE_LIFETIME; int prune_lifetime = AVERAGE_PRUNE_LIFETIME; int debug = 0; char *progname; time_t mrouted_init_time; #ifdef SNMP #define NHANDLERS 34 #else #define NHANDLERS 2 #endif static struct ihandler { int fd; /* File descriptor */ ihfunc_t func; /* Function to call with &fd_set */ } ihandlers[NHANDLERS]; static int nhandlers = 0; static struct debugname { char *name; int level; int nchars; } debugnames[] = { { "packet", DEBUG_PKT, 2 }, { "pkt", DEBUG_PKT, 3 }, { "pruning", DEBUG_PRUNE, 1 }, { "prunes", DEBUG_PRUNE, 1 }, { "routing", DEBUG_ROUTE, 1 }, { "routes", DEBUG_ROUTE, 1 }, { "route_detail", DEBUG_RTDETAIL, 6 }, { "rtdetail", DEBUG_RTDETAIL, 2 }, { "peers", DEBUG_PEER, 2 }, { "neighbors", DEBUG_PEER, 1 }, { "cache", DEBUG_CACHE, 1 }, { "timeout", DEBUG_TIMEOUT, 1 }, { "callout", DEBUG_TIMEOUT, 2 }, { "interface", DEBUG_IF, 2 }, { "vif", DEBUG_IF, 1 }, { "membership", DEBUG_MEMBER, 1 }, { "groups", DEBUG_MEMBER, 1 }, { "traceroute", DEBUG_TRACE, 2 }, { "mtrace", DEBUG_TRACE, 2 }, { "igmp", DEBUG_IGMP, 1 }, { "icmp", DEBUG_ICMP, 2 }, { "rsrr", DEBUG_RSRR, 2 }, { "3", 0xffffffff, 1 } /* compat. */ }; /* * Forward declarations. */ static void final_init __P((void *)); static void fasttimer __P((void *)); static void timer __P((void *)); static void dump __P((void)); static void dump_version __P((FILE *)); static void fdump __P((void)); static void cdump __P((void)); static void restart __P((void)); static void handler __P((int)); static void cleanup __P((void)); static void resetlogging __P((void *)); static void usage __P((void)); /* To shut up gcc -Wstrict-prototypes */ int main __P((int argc, char **argv)); int register_input_handler(fd, func) int fd; ihfunc_t func; { if (nhandlers >= NHANDLERS) return -1; ihandlers[nhandlers].fd = fd; ihandlers[nhandlers++].func = func; return 0; } int main(argc, argv) int argc; char *argv[]; { register int recvlen; int dummy; FILE *fp; struct timeval tv, difftime, curtime, lasttime, *timeout; u_int32 prev_genid; int vers; fd_set rfds, readers; int nfds, n, i, secs; extern char todaysversion[]; struct sigaction sa; #ifdef SNMP struct timeval timeout, *tvp = &timeout; struct timeval sched, *svp = &sched, now, *nvp = &now; int index, block; #endif setlinebuf(stderr); if (geteuid() != 0) errx(1, "must be root"); progname = strrchr(argv[0], '/'); if (progname) progname++; else progname = argv[0]; argv++, argc--; while (argc > 0 && *argv[0] == '-') { if (strcmp(*argv, "-d") == 0) { if (argc > 1 && *(argv + 1)[0] != '-') { char *p,*q; int i, len; struct debugname *d; argv++, argc--; debug = 0; p = *argv; q = NULL; while (p) { q = strchr(p, ','); if (q) *q++ = '\0'; len = strlen(p); for (i = 0, d = debugnames; i < sizeof(debugnames) / sizeof(debugnames[0]); i++, d++) if (len >= d->nchars && strncmp(d->name, p, len) == 0) break; if (i == sizeof(debugnames) / sizeof(debugnames[0])) { int j = 0xffffffff; int k = 0; fprintf(stderr, "Valid debug levels: "); for (i = 0, d = debugnames; i < sizeof(debugnames) / sizeof(debugnames[0]); i++, d++) { if ((j & d->level) == d->level) { if (k++) putc(',', stderr); fputs(d->name, stderr); j &= ~d->level; } } putc('\n', stderr); usage(); } debug |= d->level; p = q; } } else debug = DEFAULT_DEBUG; } else if (strcmp(*argv, "-c") == 0) { if (argc > 1) { argv++, argc--; configfilename = *argv; } else usage(); } else if (strcmp(*argv, "-p") == 0) { log(LOG_WARNING, 0, "disabling pruning is no longer supported"); #ifdef SNMP } else if (strcmp(*argv, "-P") == 0) { if (argc > 1 && isdigit(*(argv + 1)[0])) { argv++, argc--; dest_port = atoi(*argv); } else dest_port = DEFAULT_PORT; #endif } else usage(); argv++, argc--; } if (argc > 0) usage(); if (debug != 0) { struct debugname *d; char c; int tmpd = debug; fprintf(stderr, "debug level 0x%x ", debug); c = '('; for (d = debugnames; d < debugnames + sizeof(debugnames) / sizeof(debugnames[0]); d++) { if ((tmpd & d->level) == d->level) { tmpd &= ~d->level; fprintf(stderr, "%c%s", c, d->name); c = ','; } } fprintf(stderr, ")\n"); } #ifdef LOG_DAEMON (void)openlog("mrouted", LOG_PID, LOG_DAEMON); (void)setlogmask(LOG_UPTO(LOG_NOTICE)); #else (void)openlog("mrouted", LOG_PID); #endif sprintf(versionstring, "mrouted version %s", todaysversion); log(LOG_DEBUG, 0, "%s starting", versionstring); #ifdef SYSV srand48(time(NULL)); -#else - srandom(gethostid()); #endif /* * Get generation id */ gettimeofday(&tv, 0); dvmrp_genid = tv.tv_sec; fp = fopen(genidfilename, "r"); if (fp != NULL) { fscanf(fp, "%d", &prev_genid); if (prev_genid == dvmrp_genid) dvmrp_genid++; (void) fclose(fp); } fp = fopen(genidfilename, "w"); if (fp != NULL) { fprintf(fp, "%d", dvmrp_genid); (void) fclose(fp); } /* Start up the log rate-limiter */ resetlogging(NULL); callout_init(); init_igmp(); init_icmp(); init_ipip(); init_routes(); init_ktable(); #ifndef OLD_KERNEL /* * Unfortunately, you can't k_get_version() unless you've * k_init_dvmrp()'d. Now that we want to move the * k_init_dvmrp() to later in the initialization sequence, * we have to do the disgusting hack of initializing, * getting the version, then stopping the kernel multicast * forwarding. */ k_init_dvmrp(); vers = k_get_version(); k_stop_dvmrp(); /*XXX * This function must change whenever the kernel version changes */ if ((((vers >> 8) & 0xff) != 3) || ((vers & 0xff) != 5)) log(LOG_ERR, 0, "kernel (v%d.%d)/mrouted (v%d.%d) version mismatch", (vers >> 8) & 0xff, vers & 0xff, PROTOCOL_VERSION, MROUTED_VERSION); #endif #ifdef SNMP if (i = snmp_init()) return i; gettimeofday(nvp, 0); if (nvp->tv_usec < 500000L){ svp->tv_usec = nvp->tv_usec + 500000L; svp->tv_sec = nvp->tv_sec; } else { svp->tv_usec = nvp->tv_usec - 500000L; svp->tv_sec = nvp->tv_sec + 1; } #endif /* SNMP */ init_vifs(); #ifdef RSRR rsrr_init(); #endif /* RSRR */ sa.sa_handler = handler; sa.sa_flags = 0; /* Interrupt system calls */ sigemptyset(&sa.sa_mask); sigaction(SIGHUP, &sa, NULL); sigaction(SIGTERM, &sa, NULL); sigaction(SIGINT, &sa, NULL); sigaction(SIGUSR1, &sa, NULL); sigaction(SIGUSR2, &sa, NULL); FD_ZERO(&readers); FD_SET(igmp_socket, &readers); nfds = igmp_socket + 1; for (i = 0; i < nhandlers; i++) { FD_SET(ihandlers[i].fd, &readers); if (ihandlers[i].fd >= nfds) nfds = ihandlers[i].fd + 1; } IF_DEBUG(DEBUG_IF) dump_vifs(stderr); IF_DEBUG(DEBUG_ROUTE) dump_routes(stderr); /* schedule first timer interrupt */ timer_setTimer(1, fasttimer, NULL); timer_setTimer(TIMER_INTERVAL, timer, NULL); if (debug == 0) { /* * Detach from the terminal */ int t; haveterminal = 0; if (fork()) exit(0); (void)close(0); (void)close(1); (void)close(2); (void)open("/", 0); (void)dup2(0, 1); (void)dup2(0, 2); #if defined(SYSV) || defined(linux) (void)setpgrp(); #else #ifdef TIOCNOTTY t = open("/dev/tty", 2); if (t >= 0) { (void)ioctl(t, TIOCNOTTY, (char *)0); (void)close(t); } #else if (setsid() < 0) perror("setsid"); #endif #endif } fp = fopen(pidfilename, "w"); if (fp != NULL) { fprintf(fp, "%d\n", (int)getpid()); (void) fclose(fp); } /* XXX HACK * This will cause black holes for the first few seconds after startup, * since we are exchanging routes but not actually forwarding. * However, it eliminates much of the startup transient. * * It's possible that we can set a flag which says not to report any * routes (just accept reports) until this timer fires, and then * do a report_to_all_neighbors(ALL_ROUTES) immediately before * turning on DVMRP. */ timer_setTimer(10, final_init, NULL); /* * Main receive loop. */ dummy = 0; difftime.tv_usec = 0; gettimeofday(&curtime, NULL); lasttime = curtime; for(;;) { bcopy((char *)&readers, (char *)&rfds, sizeof(rfds)); secs = timer_nextTimer(); if (secs == -1) timeout = NULL; else { timeout = &tv; timeout->tv_sec = secs; timeout->tv_usec = 0; } #ifdef SNMP THIS IS BROKEN if (nvp->tv_sec > svp->tv_sec || (nvp->tv_sec == svp->tv_sec && nvp->tv_usec > svp->tv_usec)){ alarmTimer(nvp); eventTimer(nvp); if (nvp->tv_usec < 500000L){ svp->tv_usec = nvp->tv_usec + 500000L; svp->tv_sec = nvp->tv_sec; } else { svp->tv_usec = nvp->tv_usec - 500000L; svp->tv_sec = nvp->tv_sec + 1; } } tvp = &timeout; tvp->tv_sec = 0; tvp->tv_usec = 500000L; block = 0; snmp_select_info(&nfds, &rfds, tvp, &block); if (block == 1) tvp = NULL; /* block without timeout */ if ((n = select(nfds, &rfds, NULL, NULL, tvp)) < 0) #endif if (sighandled) { if (sighandled & GOT_SIGINT) { sighandled &= ~GOT_SIGINT; break; } if (sighandled & GOT_SIGHUP) { sighandled &= ~GOT_SIGHUP; restart(); } if (sighandled & GOT_SIGUSR1) { sighandled &= ~GOT_SIGUSR1; fdump(); } if (sighandled & GOT_SIGUSR2) { sighandled &= ~GOT_SIGUSR2; cdump(); } } if ((n = select(nfds, &rfds, NULL, NULL, timeout)) < 0) { if (errno != EINTR) log(LOG_WARNING, errno, "select failed"); continue; } if (n > 0) { if (FD_ISSET(igmp_socket, &rfds)) { recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE, 0, NULL, &dummy); if (recvlen < 0) { if (errno != EINTR) log(LOG_ERR, errno, "recvfrom"); continue; } accept_igmp(recvlen); } for (i = 0; i < nhandlers; i++) { if (FD_ISSET(ihandlers[i].fd, &rfds)) { (*ihandlers[i].func)(ihandlers[i].fd, &rfds); } } } #ifdef SNMP THIS IS BROKEN snmp_read(&rfds); snmp_timeout(); /* poll */ #endif /* * Handle timeout queue. * * If select + packet processing took more than 1 second, * or if there is a timeout pending, age the timeout queue. * * If not, collect usec in difftime to make sure that the * time doesn't drift too badly. * * If the timeout handlers took more than 1 second, * age the timeout queue again. XXX This introduces the * potential for infinite loops! */ do { /* * If the select timed out, then there's no other * activity to account for and we don't need to * call gettimeofday. */ if (n == 0) { curtime.tv_sec = lasttime.tv_sec + secs; curtime.tv_usec = lasttime.tv_usec; n = -1; /* don't do this next time through the loop */ } else gettimeofday(&curtime, NULL); difftime.tv_sec = curtime.tv_sec - lasttime.tv_sec; difftime.tv_usec += curtime.tv_usec - lasttime.tv_usec; while (difftime.tv_usec >= 1000000) { difftime.tv_sec++; difftime.tv_usec -= 1000000; } if (difftime.tv_usec < 0) { difftime.tv_sec--; difftime.tv_usec += 1000000; } lasttime = curtime; if (secs == 0 || difftime.tv_sec > 0) age_callout_queue(difftime.tv_sec); secs = -1; } while (difftime.tv_sec > 0); } log(LOG_NOTICE, 0, "%s exiting", versionstring); cleanup(); exit(0); } static void usage() { fprintf(stderr, "usage: mrouted [-p] [-c configfile] [-d [debug_level]]\n"); exit(1); } static void final_init(i) void *i; { char *s = (char *)i; log(LOG_NOTICE, 0, "%s%s", versionstring, s ? s : ""); if (s) free(s); k_init_dvmrp(); /* enable DVMRP routing in kernel */ /* * Install the vifs in the kernel as late as possible in the * initialization sequence. */ init_installvifs(); time(&mrouted_init_time); did_final_init = 1; } /* * routine invoked every second. Its main goal is to cycle through * the routing table and send partial updates to all neighbors at a * rate that will cause the entire table to be sent in ROUTE_REPORT_INTERVAL * seconds. Also, every TIMER_INTERVAL seconds it calls timer() to * do all the other time-based processing. */ static void fasttimer(i) void *i; { static unsigned int tlast; static unsigned int nsent; register unsigned int t = tlast + 1; register int n; /* * if we're in the last second, send everything that's left. * otherwise send at least the fraction we should have sent by now. */ if (t >= ROUTE_REPORT_INTERVAL) { register int nleft = nroutes - nsent; while (nleft > 0) { if ((n = report_next_chunk()) <= 0) break; nleft -= n; } tlast = 0; nsent = 0; } else { register unsigned int ncum = nroutes * t / ROUTE_REPORT_INTERVAL; while (nsent < ncum) { if ((n = report_next_chunk()) <= 0) break; nsent += n; } tlast = t; } timer_setTimer(1, fasttimer, NULL); } /* * The 'virtual_time' variable is initialized to a value that will cause the * first invocation of timer() to send a probe or route report to all vifs * and send group membership queries to all subnets for which this router is * querier. This first invocation occurs approximately TIMER_INTERVAL seconds * after the router starts up. Note that probes for neighbors and queries * for group memberships are also sent at start-up time, as part of initial- * ization. This repetition after a short interval is desirable for quickly * building up topology and membership information in the presence of possible * packet loss. * * 'virtual_time' advances at a rate that is only a crude approximation of * real time, because it does not take into account any time spent processing, * and because the timer intervals are sometimes shrunk by a random amount to * avoid unwanted synchronization with other routers. */ u_long virtual_time = 0; /* * Timer routine. Performs periodic neighbor probing, route reporting, and * group querying duties, and drives various timers in routing entries and * virtual interface data structures. */ static void timer(i) void *i; { age_routes(); /* Advance the timers in the route entries */ age_vifs(); /* Advance the timers for neighbors */ age_table_entry(); /* Advance the timers for the cache entries */ if (virtual_time % IGMP_QUERY_INTERVAL == 0) { /* * Time to query the local group memberships on all subnets * for which this router is the elected querier. */ query_groups(); } if (virtual_time % NEIGHBOR_PROBE_INTERVAL == 0) { /* * Time to send a probe on all vifs from which no neighbors have * been heard. Also, check if any inoperative interfaces have now * come up. (If they have, they will also be probed as part of * their initialization.) */ probe_for_neighbors(); if (vifs_down) check_vif_state(); } delay_change_reports = FALSE; if (routes_changed) { /* * Some routes have changed since the last timer interrupt, but * have not been reported yet. Report the changed routes to all * neighbors. */ report_to_all_neighbors(CHANGED_ROUTES); } #ifdef SNMP sync_timer(); #endif /* * Advance virtual time */ virtual_time += TIMER_INTERVAL; timer_setTimer(TIMER_INTERVAL, timer, NULL); } static void cleanup() { static in_cleanup = 0; if (!in_cleanup) { in_cleanup++; #ifdef RSRR rsrr_clean(); #endif /* RSRR */ expire_all_routes(); report_to_all_neighbors(ALL_ROUTES); if (did_final_init) k_stop_dvmrp(); } } /* * Signal handler. Take note of the fact that the signal arrived * so that the main loop can take care of it. */ static void handler(sig) int sig; { switch (sig) { case SIGINT: case SIGTERM: sighandled |= GOT_SIGINT; break; case SIGHUP: sighandled |= GOT_SIGHUP; break; case SIGUSR1: sighandled |= GOT_SIGUSR1; break; case SIGUSR2: sighandled |= GOT_SIGUSR2; break; } } /* * Dump internal data structures to stderr. */ static void dump() { dump_vifs(stderr); dump_routes(stderr); } static void dump_version(fp) FILE *fp; { time_t t; time(&t); fprintf(fp, "%s ", versionstring); if (did_final_init) fprintf(fp, "up %s", scaletime(t - mrouted_init_time)); else fprintf(fp, "(not yet initialized)"); fprintf(fp, " %s\n", ctime(&t)); } /* * Dump internal data structures to a file. */ static void fdump() { FILE *fp; fp = fopen(dumpfilename, "w"); if (fp != NULL) { dump_version(fp); dump_vifs(fp); dump_routes(fp); (void) fclose(fp); } } /* * Dump local cache contents to a file. */ static void cdump() { FILE *fp; fp = fopen(cachefilename, "w"); if (fp != NULL) { dump_version(fp); dump_cache(fp); (void) fclose(fp); } } /* * Restart mrouted */ static void restart() { char *s; s = (char *)malloc(sizeof(" restart")); if (s == NULL) log(LOG_ERR, 0, "out of memory"); strcpy(s, " restart"); /* * reset all the entries */ free_all_prunes(); free_all_routes(); free_all_callouts(); stop_all_vifs(); k_stop_dvmrp(); close(igmp_socket); close(udp_socket); did_final_init = 0; /* * start processing again */ dvmrp_genid++; init_igmp(); init_routes(); init_ktable(); init_vifs(); /*XXX Schedule final_init() as main does? */ final_init(s); /* schedule timer interrupts */ timer_setTimer(1, fasttimer, NULL); timer_setTimer(TIMER_INTERVAL, timer, NULL); } #define LOG_MAX_MSGS 20 /* if > 20/minute then shut up for a while */ #define LOG_SHUT_UP 600 /* shut up for 10 minutes */ static int log_nmsgs = 0; static void resetlogging(arg) void *arg; { int nxttime = 60; void *narg = NULL; if (arg == NULL && log_nmsgs > LOG_MAX_MSGS) { nxttime = LOG_SHUT_UP; narg = (void *)&log_nmsgs; /* just need some valid void * */ syslog(LOG_WARNING, "logging too fast, shutting up for %d minutes", LOG_SHUT_UP / 60); } else { log_nmsgs = 0; } timer_setTimer(nxttime, resetlogging, narg); } char * scaletime(t) u_long t; { #define SCALETIMEBUFLEN 20 static char buf1[20]; static char buf2[20]; static char *buf = buf1; char *p; p = buf; if (buf == buf1) buf = buf2; else buf = buf1; /* XXX snprintf */ sprintf(p, "%2ld:%02ld:%02ld", t / 3600, (t % 3600) / 60, t % 60); p[SCALETIMEBUFLEN - 1] = '\0'; return p; } #ifdef RINGBUFFER #define NLOGMSGS 10000 #define LOGMSGSIZE 200 char *logmsg[NLOGMSGS]; static int logmsgno = 0; void printringbuf() { FILE *f; int i; f = fopen("/var/tmp/mrouted.log", "a"); if (f == NULL) { log(LOG_ERR, errno, "can't open /var/tmp/mrouted.log"); /*NOTREACHED*/ } fprintf(f, "--------------------------------------------\n"); i = (logmsgno + 1) % NLOGMSGS; while (i != logmsgno) { if (*logmsg[i]) { fprintf(f, "%s\n", logmsg[i]); *logmsg[i] = '\0'; } i = (i + 1) % NLOGMSGS; } fclose(f); } #endif /* * Log errors and other messages to the system log daemon and to stderr, * according to the severity of the message and the current debug level. * For errors of severity LOG_ERR or worse, terminate the program. */ #ifdef __STDC__ void log(int severity, int syserr, char *format, ...) { va_list ap; static char fmt[211] = "warning - "; char *msg; struct timeval now; time_t now_sec; struct tm *thyme; #ifdef RINGBUFFER static int ringbufinit = 0; #endif va_start(ap, format); #else /*VARARGS3*/ void log(severity, syserr, format, va_alist) int severity, syserr; char *format; va_dcl { va_list ap; static char fmt[311] = "warning - "; char *msg; char tbuf[20]; struct timeval now; time_t now_sec; struct tm *thyme; #ifdef RINGBUFFER static int ringbufinit = 0; #endif va_start(ap); #endif - vsprintf(&fmt[10], format, ap); + vsnprintf(&fmt[10], sizeof(fmt) - 10, format, ap); va_end(ap); msg = (severity == LOG_WARNING) ? fmt : &fmt[10]; #ifdef RINGBUFFER if (!ringbufinit) { int i; for (i = 0; i < NLOGMSGS; i++) { logmsg[i] = malloc(LOGMSGSIZE); if (logmsg[i] == 0) { syslog(LOG_ERR, "out of memory"); exit(-1); } *logmsg[i] = 0; } ringbufinit = 1; } gettimeofday(&now,NULL); now_sec = now.tv_sec; thyme = localtime(&now_sec); - sprintf(logmsg[logmsgno++], "%02d:%02d:%02d.%03ld %s err %d", + snprintf(logmsg[logmsgno++], LOGMSGSIZE, "%02d:%02d:%02d.%03ld %s err %d", thyme->tm_hour, thyme->tm_min, thyme->tm_sec, now.tv_usec / 1000, msg, syserr); logmsgno %= NLOGMSGS; if (severity <= LOG_NOTICE) #endif /* * Log to stderr if we haven't forked yet and it's a warning or worse, * or if we're debugging. */ if (haveterminal && (debug || severity <= LOG_WARNING)) { gettimeofday(&now,NULL); now_sec = now.tv_sec; thyme = localtime(&now_sec); if (!debug) fprintf(stderr, "%s: ", progname); fprintf(stderr, "%02d:%02d:%02d.%03ld %s", thyme->tm_hour, thyme->tm_min, thyme->tm_sec, now.tv_usec / 1000, msg); if (syserr == 0) fprintf(stderr, "\n"); else if (syserr < sys_nerr) fprintf(stderr, ": %s\n", sys_errlist[syserr]); else fprintf(stderr, ": errno %d\n", syserr); } /* * Always log things that are worse than warnings, no matter what * the log_nmsgs rate limiter says. * Only count things worse than debugging in the rate limiter * (since if you put daemon.debug in syslog.conf you probably * actually want to log the debugging messages so they shouldn't * be rate-limited) */ if ((severity < LOG_WARNING) || (log_nmsgs < LOG_MAX_MSGS)) { if (severity < LOG_DEBUG) log_nmsgs++; if (syserr != 0) { errno = syserr; syslog(severity, "%s: %m", msg); } else syslog(severity, "%s", msg); } if (severity <= LOG_ERR) exit(-1); } #ifdef DEBUG_MFC void md_log(what, origin, mcastgrp) int what; u_int32 origin, mcastgrp; { static FILE *f = NULL; struct timeval tv; u_int32 buf[4]; if (!f) { if ((f = fopen("/tmp/mrouted.clog", "w")) == NULL) { log(LOG_ERR, errno, "open /tmp/mrouted.clog"); } } gettimeofday(&tv, NULL); buf[0] = tv.tv_sec; buf[1] = what; buf[2] = origin; buf[3] = mcastgrp; fwrite(buf, sizeof(u_int32), 4, f); } #endif diff --git a/usr.sbin/mrouted/mtrace.c b/usr.sbin/mrouted/mtrace.c index 66df8e284d32..86655af107e9 100644 --- a/usr.sbin/mrouted/mtrace.c +++ b/usr.sbin/mrouted/mtrace.c @@ -1,3177 +1,3175 @@ /* * mtrace.c * * This tool traces the branch of a multicast tree from a source to a * receiver for a particular multicast group and gives statistics * about packet rate and loss for each hop along the path. It can * usually be invoked just as * * mtrace source * * to trace the route from that source to the local host for a default * group when only the route is desired and not group-specific packet * counts. See the usage line for more complex forms. * * * Released 4 Apr 1995. This program was adapted by Steve Casner * (USC/ISI) from a prototype written by Ajit Thyagarajan (UDel and * Xerox PARC). It attempts to parallel in command syntax and output * format the unicast traceroute program written by Van Jacobson (LBL) * for the parts where that makes sense. * * Copyright (c) 1995 by the University of Southern California * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation in source and binary forms for any purposes and without * fee is hereby granted, provided that the above copyright notice * appear in all copies and that both the copyright notice and this * permission notice appear in supporting documentation, and that any * documentation, advertising materials, and other materials related to * such distribution and use acknowledge that the software was developed * by the University of Southern California, Information Sciences * Institute. The name of the University may not be used to endorse or * promote products derived from this software without specific prior * written permission. * * THE UNIVERSITY OF SOUTHERN CALIFORNIA makes no representations about * the suitability of this software for any purpose. THIS SOFTWARE IS * PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * Other copyrights might apply to parts of this software and are so * noted when applicable. * * Parts of this software are derived from mrouted, which has the * following license: * * The mrouted program is covered by the following license. Use of the * mrouted program represents acceptance of these terms and conditions. * * 1. STANFORD grants to LICENSEE a nonexclusive and nontransferable * license to use, copy and modify the computer software ``mrouted'' * (hereinafter called the ``Program''), upon the terms and conditions * hereinafter set out and until Licensee discontinues use of the Licensed * Program. * * 2. LICENSEE acknowledges that the Program is a research tool still in * the development state, that it is being supplied ``as is,'' without any * accompanying services from STANFORD, and that this license is entered * into in order to encourage scientific collaboration aimed at further * development and application of the Program. * * 3. LICENSEE may copy the Program and may sublicense others to use * object code copies of the Program or any derivative version of the * Program. All copies must contain all copyright and other proprietary * notices found in the Program as provided by STANFORD. Title to * copyright to the Program remains with STANFORD. * * 4. LICENSEE may create derivative versions of the Program. LICENSEE * hereby grants STANFORD a royalty-free license to use, copy, modify, * distribute and sublicense any such derivative works. At the time * LICENSEE provides a copy of a derivative version of the Program to a * third party, LICENSEE shall provide STANFORD with one copy of the * source code of the derivative version at no charge to STANFORD. * * 5. STANFORD MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR * IMPLIED. By way of example, but not limitation, STANFORD MAKES NO * REPRESENTATION OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY * PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED PROGRAM WILL NOT * INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. STANFORD * shall not be held liable for any liability nor for any direct, indirect * or consequential damages with respect to any claim by LICENSEE or any * third party on account of or arising from this Agreement or use of the * Program. * * 6. This agreement shall be construed, interpreted and applied in * accordance with the State of California and any legal action arising * out of this Agreement or use of the Program shall be filed in a court * in the State of California. * * 7. Nothing in this Agreement shall be construed as conferring rights to * use in advertising, publicity or otherwise any trademark or the name * of ``Stanford''. * * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of * Leland Stanford Junior University. * * * The mtrace program has been modified and improved by Xerox * Corporation. Xerox grants to LICENSEE a non-exclusive and * non-transferable license to use, copy, and modify the Xerox modified * and improved mrouted software on the same terms and conditions which * govern the license Stanford and ISI grant with respect to the mtrace * program. These terms and conditions are incorporated in this grant * by reference and shall be deemed to have been accepted by LICENSEE * to cover its relationship with Xerox Corporation with respect to any * use of the Xerox improved program. * * The mtrace program is COPYRIGHT 1998 by Xerox Corporation. * */ #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef SYSV #include #endif #include #ifdef __STDC__ #include #else #include #endif #ifdef SUNOS5 #include #endif typedef unsigned int u_int32; /* XXX */ #include "mtrace.h" #define DEFAULT_TIMEOUT 3 /* How long to wait before retrying requests */ #define DEFAULT_RETRIES 3 /* How many times to try */ #define DEFAULT_EXTRAHOPS 3 /* How many hops past a non-responding rtr */ #define MAXHOPS 60 /* Don't need more hops than this */ #define UNICAST_TTL 255 /* TTL for unicast response */ #define MULTICAST_TTL1 127 /* Default TTL for multicast query/response */ #define MULTICAST_TTL_INC 32 /* TTL increment for increase after timeout */ #define MULTICAST_TTL_MAX 192 /* Maximum TTL allowed (protect low-BW links */ #define TRUE 1 #define FALSE 0 #define DVMRP_ASK_NEIGHBORS2 5 /* DVMRP msg requesting neighbors */ #define DVMRP_NEIGHBORS2 6 /* reply to above */ #define DVMRP_NF_DOWN 0x10 /* kernel state of interface */ #define DVMRP_NF_DISABLED 0x20 /* administratively disabled */ #define MAX_IP_PACKET_LEN 576 #define MIN_IP_HEADER_LEN 20 #define MAX_IP_HEADER_LEN 60 #define MAX_DVMRP_DATA_LEN \ ( MAX_IP_PACKET_LEN - MAX_IP_HEADER_LEN - IGMP_MINLEN ) struct resp_buf { u_long qtime; /* Time query was issued */ u_long rtime; /* Time response was received */ int len; /* Number of reports or length of data */ struct igmp igmp; /* IGMP header */ union { struct { struct tr_query q; /* Query/response header */ struct tr_resp r[MAXHOPS]; /* Per-hop reports */ } t; char d[MAX_DVMRP_DATA_LEN]; /* Neighbor data */ } u; } base, incr[2]; #define qhdr u.t.q #define resps u.t.r #define ndata u.d char *names[MAXHOPS]; /* * In mrouted 3.3 and 3.4 (and in some Cisco IOS releases), * cache entries can get deleted even if there is traffic * flowing, which will reset the per-source/group counters. */ #define BUG_RESET 0x01 /* * Also in mrouted 3.3 and 3.4, there's a bug in neighbor * version processing which can cause them to believe that * the neighbor is constantly resetting. This causes them * to constantly delete all their state. */ #define BUG_RESET2X 0x02 /* * Pre-3.7 mrouted's forget to byte-swap their reports. */ #define BUG_SWAP 0x04 /* * Pre-3.9 mrouted's forgot a parenthesis in the htonl() * on the time calculation so supply bogus times. */ #define BUG_BOGUSTIME 0x08 #define BUG_NOPRINT (BUG_RESET | BUG_RESET2X) int bugs[MAXHOPS]; /* List of bugs noticed at each hop */ struct mtrace { struct mtrace *next; struct resp_buf base, incr[2]; struct resp_buf *new, *prev; int nresp; struct timeval last; int bugs[MAXHOPS]; char *names[MAXHOPS]; int lastqid; }; int timeout = DEFAULT_TIMEOUT; int nqueries = DEFAULT_RETRIES; int numeric = FALSE; int debug = 0; int passive = FALSE; int multicast = FALSE; int unicast = FALSE; int statint = 10; int verbose = FALSE; int tunstats = FALSE; int weak = FALSE; int extrahops = DEFAULT_EXTRAHOPS; int printstats = TRUE; int sendopts = TRUE; int lossthresh = 0; int fflag = FALSE; int staticqid = 0; u_int32 defgrp; /* Default group if not specified */ u_int32 query_cast; /* All routers multicast addr */ u_int32 resp_cast; /* Mtrace response multicast addr */ u_int32 lcl_addr = 0; /* This host address, in NET order */ u_int32 dst_netmask = 0; /* netmask to go with qdst */ /* * Query/response parameters, all initialized to zero and set later * to default values or from options. */ u_int32 qsrc = 0; /* Source address in the query */ u_int32 qgrp = 0; /* Group address in the query */ u_int32 qdst = 0; /* Destination (receiver) address in query */ u_char qno = 0; /* Max number of hops to query */ u_int32 raddr = 0; /* Address where response should be sent */ int qttl = 0; /* TTL for the query packet */ u_char rttl = 0; /* TTL for the response packet */ u_int32 gwy = 0; /* User-supplied last-hop router address */ u_int32 tdst = 0; /* Address where trace is sent (last-hop) */ char s1[19]; /* buffers to hold the string representations */ char s2[19]; /* of IP addresses, to be passed to inet_fmt() */ char s3[19]; /* or inet_fmts(). */ #if !(defined(BSD) && (BSD >= 199103)) extern int errno; extern int sys_nerr; extern char * sys_errlist[]; #endif #define RECV_BUF_SIZE 8192 char *send_buf, *recv_buf; int igmp_socket; u_int32 allrtrs_group; char router_alert[4]; /* Router Alert IP Option */ #ifndef IPOPT_RA #define IPOPT_RA 148 #endif #ifdef SUNOS5 char eol[4]; /* EOL IP Option */ int ip_addlen = 0; /* Workaround for Option bug #2 */ #endif /* * max macro, with weird case to avoid conflicts */ #define MaX(a,b) ((a) > (b) ? (a) : (b)) #ifndef __P #ifdef __STDC__ #define __P(x) x #else #define __P(x) () #endif #endif typedef int (*callback_t) __P((int, u_char *, int, struct igmp *, int, struct sockaddr *, int *, struct timeval *)); void init_igmp __P((void)); void send_igmp __P((u_int32 src, u_int32 dst, int type, int code, u_int32 group, int datalen)); int inet_cksum __P((u_short *addr, u_int len)); void k_set_rcvbuf __P((int bufsize)); void k_hdr_include __P((int bool)); void k_set_ttl __P((int t)); void k_set_loop __P((int l)); void k_set_if __P((u_int32 ifa)); void k_join __P((u_int32 grp, u_int32 ifa)); void k_leave __P((u_int32 grp, u_int32 ifa)); char * inet_fmt __P((u_int32 addr, char *s)); char * inet_fmts __P((u_int32 addr, u_int32 mask, char *s)); char * inet_name __P((u_int32 addr)); u_int32 host_addr __P((char *name)); /* u_int is promoted u_char */ char * proto_type __P((u_int type)); char * flag_type __P((u_int type)); u_int32 get_netmask __P((int s, u_int32 *dst)); int get_ttl __P((struct resp_buf *buf)); int t_diff __P((u_long a, u_long b)); u_long byteswap __P((u_long v)); int mtrace_callback __P((int, u_char *, int, struct igmp *, int, struct sockaddr *, int *, struct timeval *)); int send_recv __P((u_int32 dst, int type, int code, int tries, struct resp_buf *save, callback_t callback)); void passive_mode __P((void)); char * print_host __P((u_int32 addr)); char * print_host2 __P((u_int32 addr1, u_int32 addr2)); void print_trace __P((int idx, struct resp_buf *buf, char **names)); int what_kind __P((struct resp_buf *buf, char *why)); char * scale __P((int *hop)); void stat_line __P((struct tr_resp *r, struct tr_resp *s, int have_next, int *res)); void fixup_stats __P((struct resp_buf *base, struct resp_buf *prev, struct resp_buf *new, int *bugs)); int check_thresh __P((int thresh, struct resp_buf *base, struct resp_buf *prev, struct resp_buf *new)); int print_stats __P((struct resp_buf *base, struct resp_buf *prev, struct resp_buf *new, int *bugs, char **names)); int path_changed __P((struct resp_buf *base, struct resp_buf *new)); void check_vif_state __P((void)); int main __P((int argc, char *argv[])); void log __P((int, int, char *, ...)); static void usage __P((void)); /* * Open and initialize the igmp socket, and fill in the non-changing * IP header fields in the output packet buffer. */ void init_igmp() { struct ip *ip; recv_buf = (char *)malloc(RECV_BUF_SIZE); if (recv_buf == 0) log(LOG_ERR, 0, "Out of memory allocating recv_buf!"); send_buf = (char *)malloc(RECV_BUF_SIZE); if (send_buf == 0) log(LOG_ERR, 0, "Out of memory allocating send_buf!"); if ((igmp_socket = socket(AF_INET, SOCK_RAW, IPPROTO_IGMP)) < 0) log(LOG_ERR, errno, "IGMP socket"); k_hdr_include(TRUE); /* include IP header when sending */ k_set_rcvbuf(48*1024); /* lots of input buffering */ k_set_ttl(1); /* restrict multicasts to one hop */ k_set_loop(FALSE); /* disable multicast loopback */ ip = (struct ip *)send_buf; ip->ip_hl = sizeof(struct ip) >> 2; ip->ip_v = IPVERSION; ip->ip_tos = 0; ip->ip_off = 0; ip->ip_p = IPPROTO_IGMP; ip->ip_ttl = MAXTTL; /* applies to unicasts only */ #ifndef INADDR_ALLRTRS_GROUP #define INADDR_ALLRTRS_GROUP 0xe0000002 /* 224.0.0.2 */ #endif allrtrs_group = htonl(INADDR_ALLRTRS_GROUP); router_alert[0] = IPOPT_RA; /* Router Alert */ router_alert[1] = 4; /* 4 bytes */ router_alert[2] = 0; router_alert[3] = 0; } #ifdef SUNOS5 void checkforsolarisbug() { u_int32 localhost = htonl(0x7f000001); eol[0] = IPOPT_EOL; eol[1] = IPOPT_EOL; eol[2] = IPOPT_EOL; eol[3] = IPOPT_EOL; setsockopt(igmp_socket, IPPROTO_IP, IP_OPTIONS, eol, sizeof(eol)); /* * Check if the kernel adds the options length to the packet * length. Send myself an IGMP packet of type 0 (illegal), * with 4 IPOPT_EOL options, my PID (for collision detection) * and 4 bytes of zero (so that the checksum works whether * the 4 bytes of zero get truncated or not). */ bzero(send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN, 8); *(int *)(send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN) = getpid(); send_igmp(localhost, localhost, 0, 0, 0, 8); while (1) { int recvlen, dummy = 0; recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE, 0, NULL, &dummy); /* 8 == 4 bytes of options and 4 bytes of PID */ if (recvlen >= MIN_IP_HEADER_LEN + IGMP_MINLEN + 8) { struct ip *ip = (struct ip *)recv_buf; struct igmp *igmp; int *p; if (ip->ip_hl != 6 || ip->ip_p != IPPROTO_IGMP || ip->ip_src.s_addr != localhost || ip->ip_dst.s_addr != localhost) continue; igmp = (struct igmp *)(recv_buf + (ip->ip_hl << 2)); if (igmp->igmp_group.s_addr != 0) continue; if (igmp->igmp_type != 0 || igmp->igmp_code != 0) continue; p = (int *)((char *)igmp + IGMP_MINLEN); if (*p != getpid()) continue; #ifdef RAW_INPUT_IS_RAW ip->ip_len = ntohs(ip->ip_len); #endif if (ip->ip_len == IGMP_MINLEN + 4) ip_addlen = 4; else if (ip->ip_len == IGMP_MINLEN + 8) ip_addlen = 0; else log(LOG_ERR, 0, "while checking for Solaris bug: Sent %d bytes and got back %d!", IGMP_MINLEN + 8, ip->ip_len); break; } } } #endif /* * Construct an IGMP message in the output packet buffer. The caller may * have already placed data in that buffer, of length 'datalen'. Then send * the message from the interface with IP address 'src' to destination 'dst'. */ void send_igmp(src, dst, type, code, group, datalen) u_int32 src, dst; int type, code; u_int32 group; int datalen; { struct sockaddr_in sdst; struct ip *ip; struct igmp *igmp; int setloop = 0; static int raset = 0; int sendra = 0; int sendlen; ip = (struct ip *)send_buf; ip->ip_src.s_addr = src; ip->ip_dst.s_addr = dst; ip->ip_len = MIN_IP_HEADER_LEN + IGMP_MINLEN + datalen; sendlen = ip->ip_len; #ifdef SUNOS5 ip->ip_len += ip_addlen; #endif #ifdef RAW_OUTPUT_IS_RAW ip->ip_len = htons(ip->ip_len); #endif igmp = (struct igmp *)(send_buf + MIN_IP_HEADER_LEN); igmp->igmp_type = type; igmp->igmp_code = code; igmp->igmp_group.s_addr = group; igmp->igmp_cksum = 0; igmp->igmp_cksum = inet_cksum((u_short *)igmp, IGMP_MINLEN + datalen); if (IN_MULTICAST(ntohl(dst))) { k_set_if(src); setloop = 1; k_set_loop(TRUE); if (dst != allrtrs_group) sendra = 1; } if (sendopts && sendra && !raset) { setsockopt(igmp_socket, IPPROTO_IP, IP_OPTIONS, router_alert, sizeof(router_alert)); raset = 1; } else if (!sendra && raset) { #ifdef SUNOS5 /* * SunOS5 < 5.6 cannot properly reset the IP_OPTIONS "socket" * option. Instead, set up a string of 4 EOL's. */ setsockopt(igmp_socket, IPPROTO_IP, IP_OPTIONS, eol, sizeof(eol)); #else setsockopt(igmp_socket, IPPROTO_IP, IP_OPTIONS, NULL, 0); #endif raset = 0; } bzero(&sdst, sizeof(sdst)); sdst.sin_family = AF_INET; #if (defined(BSD) && (BSD >= 199103)) sdst.sin_len = sizeof(sdst); #endif sdst.sin_addr.s_addr = dst; if (sendto(igmp_socket, send_buf, sendlen, 0, (struct sockaddr *)&sdst, sizeof(sdst)) < 0) { log(LOG_WARNING, errno, "sendto to %s on %s", inet_fmt(dst, s1), inet_fmt(src, s2)); } if (setloop) k_set_loop(FALSE); log(LOG_DEBUG, 0, "SENT %s from %-15s to %s", type == IGMP_MTRACE ? "mtrace request" : "ask_neighbors", src == INADDR_ANY ? "INADDR_ANY" : inet_fmt(src, s1), inet_fmt(dst, s2)); } /* * inet_cksum extracted from: * P I N G . C * * Author - * Mike Muuss * U. S. Army Ballistic Research Laboratory * December, 1983 * Modified at Uc Berkeley * * (ping.c) Status - * Public Domain. Distribution Unlimited. * * I N _ C K S U M * * Checksum routine for Internet Protocol family headers (C Version) * */ int inet_cksum(addr, len) u_short *addr; u_int len; { register int nleft = (int)len; register u_short *w = addr; u_short answer = 0; register int sum = 0; /* * Our algorithm is simple, using a 32 bit accumulator (sum), * we add sequential 16 bit words to it, and at the end, fold * back all the carry bits from the top 16 bits into the lower * 16 bits. */ while (nleft > 1) { sum += *w++; nleft -= 2; } /* mop up an odd byte, if necessary */ if (nleft == 1) { *(u_char *) (&answer) = *(u_char *)w ; sum += answer; } /* * add back carry outs from top 16 bits to low 16 bits */ sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ sum += (sum >> 16); /* add carry */ answer = ~sum; /* truncate to 16 bits */ return (answer); } void k_set_rcvbuf(bufsize) int bufsize; { if (setsockopt(igmp_socket, SOL_SOCKET, SO_RCVBUF, (char *)&bufsize, sizeof(bufsize)) < 0) log(LOG_ERR, errno, "setsockopt SO_RCVBUF %u", bufsize); } void k_hdr_include(bool) int bool; { #ifdef IP_HDRINCL if (setsockopt(igmp_socket, IPPROTO_IP, IP_HDRINCL, (char *)&bool, sizeof(bool)) < 0) log(LOG_ERR, errno, "setsockopt IP_HDRINCL %u", bool); #endif } void k_set_ttl(t) int t; { u_char ttl; ttl = t; if (setsockopt(igmp_socket, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&ttl, sizeof(ttl)) < 0) log(LOG_ERR, errno, "setsockopt IP_MULTICAST_TTL %u", ttl); } void k_set_loop(l) int l; { u_char loop; loop = l; if (setsockopt(igmp_socket, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loop, sizeof(loop)) < 0) log(LOG_ERR, errno, "setsockopt IP_MULTICAST_LOOP %u", loop); } void k_set_if(ifa) u_int32 ifa; { struct in_addr adr; adr.s_addr = ifa; if (setsockopt(igmp_socket, IPPROTO_IP, IP_MULTICAST_IF, (char *)&adr, sizeof(adr)) < 0) log(LOG_ERR, errno, "setsockopt IP_MULTICAST_IF %s", inet_fmt(ifa, s1)); } void k_join(grp, ifa) u_int32 grp; u_int32 ifa; { struct ip_mreq mreq; mreq.imr_multiaddr.s_addr = grp; mreq.imr_interface.s_addr = ifa; if (setsockopt(igmp_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) < 0) log(LOG_WARNING, errno, "can't join group %s on interface %s", inet_fmt(grp, s1), inet_fmt(ifa, s2)); } void k_leave(grp, ifa) u_int32 grp; u_int32 ifa; { struct ip_mreq mreq; mreq.imr_multiaddr.s_addr = grp; mreq.imr_interface.s_addr = ifa; if (setsockopt(igmp_socket, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) < 0) log(LOG_WARNING, errno, "can't leave group %s on interface %s", inet_fmt(grp, s1), inet_fmt(ifa, s2)); } /* * Convert an IP address in u_long (network) format into a printable string. */ char * inet_fmt(addr, s) u_int32 addr; char *s; { register u_char *a; a = (u_char *)&addr; sprintf(s, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]); return (s); } /* * Convert an IP subnet number in u_long (network) format into a printable * string including the netmask as a number of bits. */ char * inet_fmts(addr, mask, s) u_int32 addr, mask; char *s; { register u_char *a, *m; int bits; if ((addr == 0) && (mask == 0)) { sprintf(s, "default"); return (s); } a = (u_char *)&addr; m = (u_char *)&mask; bits = 33 - ffs(ntohl(mask)); if (m[3] != 0) sprintf(s, "%u.%u.%u.%u/%d", a[0], a[1], a[2], a[3], bits); else if (m[2] != 0) sprintf(s, "%u.%u.%u/%d", a[0], a[1], a[2], bits); else if (m[1] != 0) sprintf(s, "%u.%u/%d", a[0], a[1], bits); else sprintf(s, "%u/%d", a[0], bits); return (s); } char * inet_name(addr) u_int32 addr; { struct hostent *e; e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET); return e ? e->h_name : "?"; } u_int32 host_addr(name) char *name; { struct hostent *e = (struct hostent *)0; u_int32 addr; int i, dots = 3; char buf[40]; char *ip = name; char *op = buf; /* * Undo BSD's favor -- take fewer than 4 octets as net/subnet address * if the name is all numeric. */ for (i = sizeof(buf) - 7; i > 0; --i) { if (*ip == '.') --dots; else if (*ip == '\0') break; else if (!isdigit(*ip)) dots = 0; /* Not numeric, don't add zeroes */ *op++ = *ip++; } for (i = 0; i < dots; ++i) { *op++ = '.'; *op++ = '0'; } *op = '\0'; if (dots <= 0) e = gethostbyname(name); if (e && (e->h_length == sizeof(addr))) { memcpy((char *)&addr, e->h_addr_list[0], e->h_length); if (e->h_addr_list[1]) fprintf(stderr, "Warning: %s has multiple addresses, using %s\n", name, inet_fmt(addr, s1)); } else { addr = inet_addr(buf); if (addr == -1 || (IN_MULTICAST(addr) && dots)) { addr = 0; printf("Could not parse %s as host name or address\n", name); } } return addr; } char * proto_type(type) u_int type; { static char buf[80]; switch (type) { case PROTO_DVMRP: return ("DVMRP"); case PROTO_MOSPF: return ("MOSPF"); case PROTO_PIM: return ("PIM"); case PROTO_CBT: return ("CBT"); case PROTO_PIM_SPECIAL: return ("PIM/Special"); case PROTO_PIM_STATIC: return ("PIM/Static"); case PROTO_DVMRP_STATIC: return ("DVMRP/Static"); case PROTO_PIM_BGP4PLUS: return ("PIM/BGP4+"); case PROTO_CBT_SPECIAL: return ("CBT/Special"); case PROTO_CBT_STATIC: return ("CBT/Static"); case PROTO_PIM_ASSERT: return ("PIM/Assert"); case 0: return ("None"); default: (void) sprintf(buf, "Unknown protocol code %d", type); return (buf); } } char * flag_type(type) u_int type; { static char buf[80]; switch (type) { case TR_NO_ERR: return (""); case TR_WRONG_IF: return ("Wrong interface"); case TR_PRUNED: return ("Prune sent upstream"); case TR_OPRUNED: return ("Output pruned"); case TR_SCOPED: return ("Hit scope boundary"); case TR_NO_RTE: return ("No route"); case TR_NO_FWD: return ("Not forwarding"); case TR_HIT_RP: return ("Reached RP/Core"); case TR_RPF_IF: return ("RPF Interface"); case TR_NO_MULTI: return ("Multicast disabled"); case TR_OLD_ROUTER: return ("Next router no mtrace"); case TR_NO_SPACE: return ("No space in packet"); case TR_ADMIN_PROHIB: return ("Admin. Prohibited"); default: (void) sprintf(buf, "Unknown error code %d", type); return (buf); } } /* * If destination is on a local net, get the netmask, else set the * netmask to all ones. There are two side effects: if the local * address was not explicitly set, and if the destination is on a * local net, use that one; in either case, verify that the local * address is valid. */ u_int32 get_netmask(s, dst) int s; u_int32 *dst; { unsigned int n; struct ifconf ifc; struct ifreq *ifrp, *ifend; u_int32 if_addr, if_mask; u_int32 retval = 0xFFFFFFFF; int found = FALSE; int num_ifreq = 32; ifc.ifc_len = num_ifreq * sizeof(struct ifreq); ifc.ifc_buf = malloc(ifc.ifc_len); while (ifc.ifc_buf) { if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { perror("ioctl SIOCGIFCONF"); return retval; } /* * If the buffer was large enough to hold all the addresses * then break out, otherwise increase the buffer size and * try again. * * The only way to know that we definitely had enough space * is to know that there was enough space for at least one * more struct ifreq. ??? */ if ((num_ifreq * sizeof(struct ifreq)) >= ifc.ifc_len + sizeof(struct ifreq)) break; num_ifreq *= 2; ifc.ifc_len = num_ifreq * sizeof(struct ifreq); ifc.ifc_buf = realloc(ifc.ifc_buf, ifc.ifc_len); } if (ifc.ifc_buf == NULL) { fprintf(stderr, "getting interface list: ran out of memory"); exit(1); } ifrp = (struct ifreq *)ifc.ifc_buf; ifend = (struct ifreq *)(ifc.ifc_buf + ifc.ifc_len); /* * Loop through all of the interfaces. */ for (; ifrp < ifend && !found; ifrp = (struct ifreq *)((char *)ifrp + n)) { #if BSD >= 199006 n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name); if (n < sizeof(*ifrp)) n = sizeof(*ifrp); #else n = sizeof(*ifrp); #endif /* * Ignore any interface for an address family other than IP. */ if (ifrp->ifr_addr.sa_family != AF_INET) continue; if_addr = ((struct sockaddr_in *)&(ifrp->ifr_addr))->sin_addr.s_addr; if (ioctl(s, SIOCGIFFLAGS, (char *)ifrp) < 0) { fprintf(stderr, "SIOCGIFFLAGS on "); perror(ifrp->ifr_name); continue; } if ((ifrp->ifr_flags & (IFF_MULTICAST|IFF_UP|IFF_LOOPBACK)) != (IFF_MULTICAST|IFF_UP)) continue; if (*dst == 0) *dst = if_addr; if (ioctl(s, SIOCGIFNETMASK, (char *)ifrp) >= 0) { if_mask = ((struct sockaddr_in *)&(ifrp->ifr_addr))->sin_addr.s_addr; if (if_mask != 0 && (*dst & if_mask) == (if_addr & if_mask)) { retval = if_mask; if (lcl_addr == 0) lcl_addr = if_addr; /* XXX what about aliases? */ } } if (lcl_addr == if_addr) found = TRUE; } if (!found && lcl_addr != 0) { printf("Interface address is not valid\n"); exit(1); } return (retval); } /* * Try to pick a TTL that will get past all the thresholds in the path. */ int get_ttl(buf) struct resp_buf *buf; { int rno; struct tr_resp *b; u_int ttl; if (buf && (rno = buf->len) > 0) { b = buf->resps + rno - 1; ttl = b->tr_fttl; while (--rno > 0) { --b; if (ttl < b->tr_fttl) ttl = b->tr_fttl; else ++ttl; } ttl += MULTICAST_TTL_INC; if (ttl < MULTICAST_TTL1) ttl = MULTICAST_TTL1; if (ttl > MULTICAST_TTL_MAX) ttl = MULTICAST_TTL_MAX; return (ttl); } else return(MULTICAST_TTL1); } /* * Calculate the difference between two 32-bit NTP timestamps and return * the result in milliseconds. */ int t_diff(a, b) u_long a, b; { int d = a - b; return ((d * 125) >> 13); } /* * Swap bytes for poor little-endian machines that don't byte-swap */ u_long byteswap(v) u_long v; { return ((v << 24) | ((v & 0xff00) << 8) | ((v >> 8) & 0xff00) | (v >> 24)); } #if 0 /* * XXX incomplete - need private callback data, too? * XXX since dst doesn't get passed through? */ int neighbors_callback(tmo, buf, buflen, igmp, igmplen, addr, addrlen, ts) int tmo; u_char *buf; int buflen; struct igmp *igmp; int igmplen; struct sockaddr *addr; int *addrlen; struct timeval *ts; { int len; u_int32 dst; struct ip *ip = (struct ip *)buf; if (tmo) return 0; if (igmp->igmp_code != DVMRP_NEIGHBORS2) return 0; len = igmplen; /* * Accept DVMRP_NEIGHBORS2 response if it comes from the * address queried or if that address is one of the local * addresses in the response. */ if (ip->ip_src.s_addr != dst) { u_int32 *p = (u_int32 *)(igmp + 1); u_int32 *ep = p + (len >> 2); while (p < ep) { u_int32 laddr = *p++; int n = ntohl(*p++) & 0xFF; if (laddr == dst) { ep = p + 1; /* ensure p < ep after loop */ break; } p += n; } if (p >= ep) return 0; } return buflen; } #endif int mtrace_callback(tmo, buf, buflen, igmp, igmplen, addr, addrlen, ts) int tmo; u_char *buf; int buflen; struct igmp *igmp; int igmplen; struct sockaddr *addr; int *addrlen; struct timeval *ts; { static u_char *savbuf = NULL; static int savbuflen; static struct sockaddr *savaddr; static int savaddrlen; static struct timeval savts; int len = (igmplen - QLEN) / RLEN; struct tr_resp *r = (struct tr_resp *)((struct tr_query *)(igmp + 1) + 1); if (tmo == 1) { /* * If we timed out with a packet saved, then return that packet. * send_recv won't send this same packet to the callback again. */ if (savbuf) { bcopy(savbuf, buf, savbuflen); free(savbuf); savbuf = NULL; bcopy(savaddr, addr, savaddrlen); free(savaddr); *addrlen = savaddrlen; bcopy(&savts, ts, sizeof(savts)); return savbuflen; } return 0; } if (savbuf) { free(savbuf); savbuf = NULL; free(savaddr); } /* * Check for IOS bug described in CSCdi68628, where a router that does * not have multicast enabled responds to an mtrace request with a 1-hop * error packet. * Heuristic is: * If there is only one hop reported in the packet, * And the protocol code is 0, * And there is no previous hop, * And the forwarding information is "Not Forwarding", * And the router is not on the same subnet as the destination of the * trace, * then drop this packet. The "#if 0"'d code saves it and returns * it on timeout, but timeouts are too common (e.g. routers with * limited unicast routing tables, etc). */ if (len == 1 && r->tr_rproto == 0 && r->tr_rmtaddr == 0 && r->tr_rflags == TR_NO_FWD) { u_int32 smask; VAL_TO_MASK(smask, r->tr_smask); if ((r->tr_outaddr & smask) != (qdst & smask)) { #if 0 /* XXX should do this silently? */ fprintf(stderr, "mtrace: probably IOS-buggy packet from %s\n", inet_fmt(((struct sockaddr_in *)addr)->sin_addr.s_addr, s1)); /* Save the packet to return if a timeout occurs. */ savbuf = (u_char *)malloc(buflen); if (savbuf != NULL) { bcopy(buf, savbuf, buflen); savbuflen = buflen; savaddr = (struct sockaddr *)malloc(*addrlen); if (savaddr != NULL) { bcopy(addr, savaddr, *addrlen); savaddrlen = *addrlen; bcopy(ts, &savts, sizeof(savts)); } else { free(savbuf); savbuf = NULL; } } #endif return 0; } } return buflen; } int send_recv(dst, type, code, tries, save, callback) u_int32 dst; int type, code, tries; struct resp_buf *save; callback_t callback; { fd_set fds; struct timeval tq, tr, tv; struct ip *ip; struct igmp *igmp; struct tr_query *query, *rquery; struct tr_resp *r; struct sockaddr_in recvaddr; u_int32 local, group; int ipdatalen, iphdrlen, igmpdatalen; int datalen; int count, recvlen, socklen = sizeof(recvaddr); int len; int i; if (type == IGMP_MTRACE) { group = qgrp; datalen = sizeof(struct tr_query); } else { group = htonl(0xff03); datalen = 0; } if (IN_MULTICAST(ntohl(dst))) local = lcl_addr; else local = INADDR_ANY; /* * If the reply address was not explictly specified, start off * with the standard multicast reply address, or the unicast * address of this host if the unicast flag was specified. * Then, if there is no response after trying half the tries * with multicast, switch to the unicast address of this host * if the multicast flag was not specified. If the TTL was * also not specified, set a multicast TTL and increase it * for every try. */ query = (struct tr_query *)(send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN); query->tr_raddr = raddr ? raddr : unicast ? lcl_addr : resp_cast; TR_SETTTL(query->tr_rttlqid, rttl ? rttl : IN_MULTICAST(ntohl(query->tr_raddr)) ? get_ttl(save) : UNICAST_TTL); query->tr_src = qsrc; query->tr_dst = qdst; for (i = tries ; i > 0; --i) { int oqid; if (tries == nqueries && raddr == 0) { if (i == (nqueries >> 1)) { if (multicast && unicast) { query->tr_raddr = resp_cast; if (!rttl) TR_SETTTL(query->tr_rttlqid, get_ttl(save)); } else if (!multicast) { query->tr_raddr = lcl_addr; TR_SETTTL(query->tr_rttlqid, UNICAST_TTL); } } if (i < tries && IN_MULTICAST(ntohl(query->tr_raddr)) && rttl == 0) { TR_SETTTL(query->tr_rttlqid, TR_GETTTL(query->tr_rttlqid) + MULTICAST_TTL_INC); if (TR_GETTTL(query->tr_rttlqid) > MULTICAST_TTL_MAX) TR_SETTTL(query->tr_rttlqid, MULTICAST_TTL_MAX); } } /* * Change the qid for each request sent to avoid being confused * by duplicate responses */ oqid = TR_GETQID(query->tr_rttlqid); if (staticqid) TR_SETQID(query->tr_rttlqid, staticqid); else #ifdef SYSV TR_SETQID(query->tr_rttlqid, ((u_int32)lrand48() >> 8)); #else - TR_SETQID(query->tr_rttlqid, ((u_int32)random() >> 8)); + TR_SETQID(query->tr_rttlqid, ((u_int32)arc4random() >> 8)); #endif /* * Set timer to calculate delays, then send query */ gettimeofday(&tq, 0); send_igmp(local, dst, type, code, group, datalen); /* * Wait for response, discarding false alarms */ while (TRUE) { FD_ZERO(&fds); FD_SET(igmp_socket, &fds); gettimeofday(&tv, 0); tv.tv_sec = tq.tv_sec + timeout - tv.tv_sec; tv.tv_usec = tq.tv_usec - tv.tv_usec; if (tv.tv_usec < 0) tv.tv_usec += 1000000L, --tv.tv_sec; if (tv.tv_sec < 0) tv.tv_sec = tv.tv_usec = 0; count = select(igmp_socket + 1, &fds, (fd_set *)0, (fd_set *)0, &tv); if (count < 0) { if (errno != EINTR) warn("select"); continue; } else if (count == 0) { /* * Timed out. Notify the callback. */ if (!callback || (recvlen = (callback)(1, recv_buf, 0, NULL, 0, (struct sockaddr *)&recvaddr, &socklen, &tr)) == 0) { printf("* "); fflush(stdout); break; } } else { /* * Data is available on the socket, so read it. */ gettimeofday(&tr, 0); recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE, 0, (struct sockaddr *)&recvaddr, &socklen); } if (recvlen <= 0) { if (recvlen && errno != EINTR) warn("recvfrom"); continue; } if (recvlen < sizeof(struct ip)) { warnx("packet too short (%u bytes) for IP header", recvlen); continue; } ip = (struct ip *) recv_buf; if (ip->ip_p == 0) /* ignore cache creation requests */ continue; iphdrlen = ip->ip_hl << 2; #ifdef RAW_INPUT_IS_RAW ipdatalen = ntohs(ip->ip_len); #else ipdatalen = ip->ip_len; #endif if (iphdrlen + ipdatalen != recvlen) { warnx("packet shorter (%u bytes) than hdr+data len (%u+%u)", recvlen, iphdrlen, ipdatalen); continue; } igmp = (struct igmp *) (recv_buf + iphdrlen); igmpdatalen = ipdatalen - IGMP_MINLEN; if (igmpdatalen < 0) { warnx("IP data field too short (%u bytes) for IGMP from %s", ipdatalen, inet_fmt(ip->ip_src.s_addr, s1)); continue; } switch (igmp->igmp_type) { case IGMP_DVMRP: if (type != IGMP_DVMRP || code != DVMRP_ASK_NEIGHBORS2) continue; if (igmp->igmp_code != DVMRP_NEIGHBORS2) continue; len = igmpdatalen; /* * Accept DVMRP_NEIGHBORS2 response if it comes from the * address queried or if that address is one of the local * addresses in the response. */ if (ip->ip_src.s_addr != dst) { u_int32 *p = (u_int32 *)(igmp + 1); u_int32 *ep = p + (len >> 2); while (p < ep) { u_int32 laddr = *p++; int n = ntohl(*p++) & 0xFF; if (laddr == dst) { ep = p + 1; /* ensure p < ep after loop */ break; } p += n; } if (p >= ep) continue; } break; case IGMP_MTRACE: /* For backward compatibility with 3.3 */ case IGMP_MTRACE_RESP: if (type != IGMP_MTRACE) continue; if (igmpdatalen <= QLEN) continue; if ((igmpdatalen - QLEN)%RLEN) { printf("packet with incomplete responses (%d bytes)\n", igmpdatalen); continue; } /* * Ignore responses that don't match query. */ rquery = (struct tr_query *)(igmp + 1); if (rquery->tr_src != qsrc || rquery->tr_dst != qdst) continue; if (TR_GETQID(rquery->tr_rttlqid) != TR_GETQID(query->tr_rttlqid)) { if (verbose && TR_GETQID(rquery->tr_rttlqid) == oqid) printf("[D]"); continue; } len = (igmpdatalen - QLEN)/RLEN; r = (struct tr_resp *)(rquery+1) + len - 1; /* * Ignore trace queries passing through this node when * mtrace is run on an mrouter that is in the path * (needed only because IGMP_MTRACE is accepted above * for backward compatibility with multicast release 3.3). */ if (igmp->igmp_type == IGMP_MTRACE) { u_int32 smask; VAL_TO_MASK(smask, r->tr_smask); if (len < code && (r->tr_inaddr & smask) != (qsrc & smask) && r->tr_rmtaddr != 0 && !(r->tr_rflags & 0x80)) continue; } /* * Some routers will return error messages without * filling in their addresses. We fill in the address * for them. */ if (r->tr_outaddr == 0) r->tr_outaddr = recvaddr.sin_addr.s_addr; /* * A match, we'll keep this one. */ if (len > code) { warnx("num hops received (%d) exceeds request (%d)", len, code); } rquery->tr_raddr = query->tr_raddr; /* Insure these are */ TR_SETTTL(rquery->tr_rttlqid, TR_GETTTL(query->tr_rttlqid)); /* as we sent them */ break; default: continue; } /* * We're pretty sure we want to use this packet now, * but if the caller gave a callback function, it might * want to handle it instead. Give the callback a chance, * unless the select timed out (in which case the only way * to get here is because the callback returned a packet). */ if (callback && (count != 0) && ((callback)(0, recv_buf, recvlen, igmp, igmpdatalen, (struct sockaddr*)&recvaddr, &socklen, &tr)) == 0) { /* * The callback function didn't like this packet. * Go try receiving another one. */ continue; } /* * Most of the sanity checking done at this point. * Return this packet we have been waiting for. */ if (save) { save->qtime = ((tq.tv_sec + JAN_1970) << 16) + (tq.tv_usec << 10) / 15625; save->rtime = ((tr.tv_sec + JAN_1970) << 16) + (tr.tv_usec << 10) / 15625; save->len = len; bcopy((char *)igmp, (char *)&save->igmp, ipdatalen); } return (recvlen); } } return (0); } /* * Most of this code is duplicated elsewhere. I'm not sure if * the duplication is absolutely required or not. * * Ideally, this would keep track of ongoing statistics * collection and print out statistics. (& keep track * of h-b-h traces and only print the longest) For now, * it just snoops on what traces it can. */ void passive_mode() { struct timeval tr; time_t tr_sec; struct ip *ip; struct igmp *igmp; struct tr_resp *r; struct sockaddr_in recvaddr; struct tm *now; char timebuf[32]; int socklen; int ipdatalen, iphdrlen, igmpdatalen; int len, recvlen; int qid; u_int32 smask; struct mtrace *remembered = NULL, *m, *n, **nn; int pc = 0; if (raddr) { if (IN_MULTICAST(ntohl(raddr))) k_join(raddr, lcl_addr); } else k_join(htonl(0xE0000120), lcl_addr); while (1) { fflush(stdout); /* make sure previous trace is flushed */ socklen = sizeof(recvaddr); recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE, 0, (struct sockaddr *)&recvaddr, &socklen); gettimeofday(&tr,0); if (recvlen <= 0) { if (recvlen && errno != EINTR) warn("recvfrom"); continue; } if (recvlen < sizeof(struct ip)) { warnx("packet too short (%u bytes) for IP header", recvlen); continue; } ip = (struct ip *) recv_buf; if (ip->ip_p == 0) /* ignore cache creation requests */ continue; iphdrlen = ip->ip_hl << 2; #ifdef RAW_INPUT_IS_RAW ipdatalen = ntohs(ip->ip_len); #else ipdatalen = ip->ip_len; #endif if (iphdrlen + ipdatalen != recvlen) { warnx("packet shorter (%u bytes) than hdr+data len (%u+%u)", recvlen, iphdrlen, ipdatalen); continue; } igmp = (struct igmp *) (recv_buf + iphdrlen); igmpdatalen = ipdatalen - IGMP_MINLEN; if (igmpdatalen < 0) { warnx("IP data field too short (%u bytes) for IGMP from %s", ipdatalen, inet_fmt(ip->ip_src.s_addr, s1)); continue; } switch (igmp->igmp_type) { case IGMP_MTRACE: /* For backward compatibility with 3.3 */ case IGMP_MTRACE_RESP: if (igmpdatalen < QLEN) continue; if ((igmpdatalen - QLEN)%RLEN) { printf("packet with incorrect datalen\n"); continue; } len = (igmpdatalen - QLEN)/RLEN; break; default: continue; } base.qtime = ((tr.tv_sec + JAN_1970) << 16) + (tr.tv_usec << 10) / 15625; base.rtime = ((tr.tv_sec + JAN_1970) << 16) + (tr.tv_usec << 10) / 15625; base.len = len; bcopy((char *)igmp, (char *)&base.igmp, ipdatalen); /* * If the user specified which traces to monitor, * only accept traces that correspond to the * request */ if ((qsrc != 0 && qsrc != base.qhdr.tr_src) || (qdst != 0 && qdst != base.qhdr.tr_dst) || (qgrp != 0 && qgrp != igmp->igmp_group.s_addr)) continue; /* XXX This should be a hash table */ /* XXX garbage-collection should be more efficient */ for (nn = &remembered, n = *nn, m = 0; n; n = *nn) { if ((n->base.qhdr.tr_src == base.qhdr.tr_src) && (n->base.qhdr.tr_dst == base.qhdr.tr_dst) && (n->base.igmp.igmp_group.s_addr == igmp->igmp_group.s_addr)) { m = n; m->last = tr; } if (tr.tv_sec - n->last.tv_sec > 500) { /* XXX don't hardcode */ *nn = n->next; free(n); } else { nn = &n->next; } } tr_sec = tr.tv_sec; now = localtime(&tr_sec); strftime(timebuf, sizeof(timebuf) - 1, "%b %e %k:%M:%S", now); printf("Mtrace %s at %s", len == 0 ? "query" : igmp->igmp_type == IGMP_MTRACE_RESP ? "response" : "in transit", timebuf); if (len == 0) printf(" by %s", inet_fmt(recvaddr.sin_addr.s_addr, s1)); if (!IN_MULTICAST(base.qhdr.tr_raddr)) printf(", resp to %s", (len == 0 && recvaddr.sin_addr.s_addr == base.qhdr.tr_raddr) ? "same" : inet_fmt(base.qhdr.tr_raddr, s1)); else printf(", respttl %d", TR_GETTTL(base.qhdr.tr_rttlqid)); printf(", qid %06x\n", qid = TR_GETQID(base.qhdr.tr_rttlqid)); printf("packet from %s to %s\n", inet_fmt(ip->ip_src.s_addr, s1), inet_fmt(ip->ip_dst.s_addr, s2)); printf("from %s to %s via group %s (mxhop=%d)\n", inet_fmt(base.qhdr.tr_dst, s1), inet_fmt(base.qhdr.tr_src, s2), inet_fmt(igmp->igmp_group.s_addr, s3), igmp->igmp_code); if (len == 0) { printf("\n"); continue; } r = base.resps + base.len - 1; /* * Some routers will return error messages without * filling in their addresses. We fill in the address * for them. */ if (r->tr_outaddr == 0) r->tr_outaddr = recvaddr.sin_addr.s_addr; /* * If there was a previous trace, it see if this is a * statistics candidate. */ if (m && base.len == m->base.len && !(pc = path_changed(&m->base, &base))) { /* * Some mtrace responders send multiple copies of the same * reply. Skip this packet if it's got the same query-id * as the last one. */ if (m->lastqid == qid) { printf("Skipping duplicate reply\n"); continue; } m->lastqid = qid; ++m->nresp; bcopy(&base, m->new, sizeof(base)); printf("Results after %d seconds:\n\n", (int)((m->new->qtime - m->base.qtime) >> 16)); fixup_stats(&m->base, m->prev, m->new, m->bugs); print_stats(&m->base, m->prev, m->new, m->bugs, m->names); m->prev = m->new; m->new = &m->incr[(m->nresp & 1)]; continue; } if (m == NULL) { m = (struct mtrace *)malloc(sizeof(struct mtrace)); if (m == NULL) { fprintf(stderr, "Out of memory!\n"); continue; } bzero(m, sizeof(struct mtrace)); m->next = remembered; remembered = m; bcopy(&tr, &m->last, sizeof(tr)); } /* Either it's a hop-by-hop in progress, or the path changed. */ if (pc) { printf("[Path Changed...]\n"); bzero(m->bugs, sizeof(m->bugs)); } bcopy(&base, &m->base, sizeof(base)); m->prev = &m->base; m->new = &m->incr[0]; m->nresp = 0; printf(" 0 "); print_host(base.qhdr.tr_dst); printf("\n"); print_trace(1, &base, m->names); VAL_TO_MASK(smask, r->tr_smask); if ((r->tr_inaddr & smask) == (base.qhdr.tr_src & smask)) { printf("%3d ", -(base.len+1)); print_host(base.qhdr.tr_src); printf("\n"); } else if (r->tr_rmtaddr != 0) { printf("%3d ", -(base.len+1)); print_host(r->tr_rmtaddr); printf(" %s\n", r->tr_rflags == TR_OLD_ROUTER ? "doesn't support mtrace" : "is the next hop"); } printf("\n"); } } char * print_host(addr) u_int32 addr; { return print_host2(addr, 0); } /* * On some routers, one interface has a name and the other doesn't. * We always print the address of the outgoing interface, but can * sometimes get the name from the incoming interface. This might be * confusing but should be slightly more helpful than just a "?". */ char * print_host2(addr1, addr2) u_int32 addr1, addr2; { char *name; if (numeric) { printf("%s", inet_fmt(addr1, s1)); return (""); } name = inet_name(addr1); if (*name == '?' && *(name + 1) == '\0' && addr2 != 0) name = inet_name(addr2); printf("%s (%s)", name, inet_fmt(addr1, s1)); return (name); } /* * Print responses as received (reverse path from dst to src) */ void print_trace(idx, buf, names) int idx; struct resp_buf *buf; char **names; { struct tr_resp *r; char *name; int i; int hop; char *ms; i = abs(idx); r = buf->resps + i - 1; for (; i <= buf->len; ++i, ++r) { if (idx > 0) printf("%3d ", -i); name = print_host2(r->tr_outaddr, r->tr_inaddr); if (r->tr_rflags != TR_NO_RTE) printf(" %s thresh^ %d", proto_type(r->tr_rproto), r->tr_fttl); if (verbose) { hop = t_diff(ntohl(r->tr_qarr), buf->qtime); ms = scale(&hop); printf(" %d%s", hop, ms); } printf(" %s", flag_type(r->tr_rflags)); if (i > 1 && r->tr_outaddr != (r-1)->tr_rmtaddr) { printf(" !RPF!"); print_host((r-1)->tr_rmtaddr); } if (r->tr_rflags != TR_NO_RTE) { if (r->tr_smask <= 1) /* MASK_TO_VAL() returns 1 for default */ printf(" [default]"); else if (verbose) { u_int32 smask; VAL_TO_MASK(smask, r->tr_smask); printf(" [%s]", inet_fmts(buf->qhdr.tr_src & smask, smask, s1)); } } printf("\n"); if (names[i-1]) free(names[i-1]); names[i-1]=malloc(strlen(name) + 1); strcpy(names[i-1], name); } } /* * See what kind of router is the next hop */ int what_kind(buf, why) struct resp_buf *buf; char *why; { u_int32 smask; int retval; int hops = buf->len; struct tr_resp *r = buf->resps + hops - 1; u_int32 next = r->tr_rmtaddr; retval = send_recv(next, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2, 1, &incr[0], NULL); print_host(next); if (retval) { u_int32 version = ntohl(incr[0].igmp.igmp_group.s_addr); u_int32 *p = (u_int32 *)incr[0].ndata; u_int32 *ep = p + (incr[0].len >> 2); char *type = "version "; retval = 0; switch (version & 0xFF) { case 1: type = "proteon/mrouted "; retval = 1; break; case 10: case 11: type = "cisco "; } printf(" [%s%d.%d] %s\n", type, version & 0xFF, (version >> 8) & 0xFF, why); VAL_TO_MASK(smask, r->tr_smask); while (p < ep) { u_int32 laddr = *p++; int flags = (ntohl(*p) & 0xFF00) >> 8; int n = ntohl(*p++) & 0xFF; if (!(flags & (DVMRP_NF_DOWN | DVMRP_NF_DISABLED)) && (laddr & smask) == (qsrc & smask)) { printf("%3d ", -(hops+2)); print_host(qsrc); printf("\n"); return 1; } p += n; } return retval; } printf(" %s\n", why); return 0; } char * scale(hop) int *hop; { if (*hop > -1000 && *hop < 10000) return (" ms"); *hop /= 1000; if (*hop > -1000 && *hop < 10000) return (" s "); return ("s "); } /* * Calculate and print one line of packet loss and packet rate statistics. * Checks for count of all ones from mrouted 2.3 that doesn't have counters. */ #define NEITHER 0 #define INS 1 #define OUTS 2 #define BOTH 3 void stat_line(r, s, have_next, rst) struct tr_resp *r, *s; int have_next; int *rst; { int timediff = (ntohl(s->tr_qarr) - ntohl(r->tr_qarr)) >> 16; int v_lost, v_pct; int g_lost, g_pct; int v_out = ntohl(s->tr_vifout) - ntohl(r->tr_vifout); int g_out = ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt); int v_pps, g_pps; char v_str[8], g_str[8]; int vhave = NEITHER; int ghave = NEITHER; int gmissing = NEITHER; char whochar; int badtime = 0; if (timediff == 0) { badtime = 1; /* Might be 32 bits of int seconds instead of 16int+16frac */ timediff = ntohl(s->tr_qarr) - ntohl(r->tr_qarr); if (timediff == 0 || abs(timediff - statint) > statint) timediff = 1; } v_pps = v_out / timediff; g_pps = g_out / timediff; #define STATS_MISSING(x) ((x) == 0xFFFFFFFF) if (!STATS_MISSING(s->tr_vifout) && !STATS_MISSING(r->tr_vifout)) vhave |= OUTS; if (STATS_MISSING(s->tr_pktcnt) || STATS_MISSING(r->tr_pktcnt)) gmissing |= OUTS; if (!(*rst & BUG_NOPRINT)) ghave |= OUTS; if (have_next) { --r, --s, --rst; if (!STATS_MISSING(s->tr_vifin) && !STATS_MISSING(r->tr_vifin)) vhave |= INS; if (STATS_MISSING(s->tr_pktcnt) || STATS_MISSING(r->tr_pktcnt)) gmissing |= INS; if (!(*rst & BUG_NOPRINT)) ghave |= INS; } /* * Stats can be missing for any number of reasons: * - The hop may not be capable of collecting stats * - Traffic may be getting dropped at the previous hop * and so this hop may not have any state * * We need a stronger heuristic to tell between these * two cases; in case 1 we don't want to print the stats * and in case 2 we want to print 100% loss. We used to * err on the side of not printing, which is less useful * than printing 100% loss and dealing with it. */ #if 0 /* * If both hops report as missing, then it's likely that there's just * no traffic flowing. * * If just one hop is missing, then we really don't have it. */ if (gmissing != BOTH) ghave &= ~gmissing; #endif whochar = have_next ? '^' : ' '; switch (vhave) { case BOTH: v_lost = v_out - (ntohl(s->tr_vifin) - ntohl(r->tr_vifin)); if (v_out) v_pct = v_lost * 100 / v_out; else v_pct = 0; if (-20 < v_pct && v_pct < 101 && v_out > 10) sprintf(v_str, "%3d%%", v_pct); else if (v_pct < -900 && v_out > 10) sprintf(v_str, "%3dx", (int)(-v_pct / 100. + 1.)); else if (v_pct <= -20 && v_out > 10) sprintf(v_str, "%1.1fx", -v_pct / 100. + 1.); else memcpy(v_str, " -- ", 5); if (tunstats) printf("%6d/%-5d=%s", v_lost, v_out, v_str); else printf(" "); printf("%4d pps", v_pps); if (v_pps && badtime) printf("?"); break; case INS: v_out = ntohl(s->tr_vifin) - ntohl(r->tr_vifin); v_pps = v_out / timediff; whochar = 'v'; /* FALLTHROUGH */ case OUTS: if (tunstats) printf(" %c%-5d ", whochar, v_out); else printf(" %c", whochar); printf("%4d pps", v_pps); if (v_pps && badtime) printf("?"); break; case NEITHER: if (ghave != NEITHER) if (tunstats) printf(" "); else printf(" "); break; } whochar = have_next ? '^' : ' '; switch (ghave) { case BOTH: g_lost = g_out - (ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt)); if (g_out) g_pct = g_lost * 100 / g_out; else g_pct = 0; if (-20 < g_pct && g_pct < 101 && g_out > 10) sprintf(g_str, "%3d%%", g_pct); else if (g_pct < -900 && g_out > 10) sprintf(g_str, "%3dx", (int)(-g_pct / 100. + 1.)); else if (g_pct <= -20 && g_out > 10) sprintf(g_str, "%1.1fx", -g_pct / 100. + 1.); else memcpy(g_str, " -- ", 5); printf("%s%6d/%-5d=%s%4d pps", tunstats ? "" : " ", g_lost, g_out, g_str, g_pps); if (g_pps && badtime) printf("?"); printf("\n"); break; #if 0 case INS: g_out = ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt); g_pps = g_out / timediff; whochar = 'v'; /* FALLTHROUGH */ #endif case OUTS: printf("%s ?/%-5d %4d pps", tunstats ? "" : " ", g_out, g_pps); if (badtime) printf("?"); printf("\n"); break; case INS: case NEITHER: printf("\n"); break; } if (debug > 2) { printf("\t\t\t\tv_in: %ld ", (long)ntohl(s->tr_vifin)); printf("v_out: %ld ", (long)ntohl(s->tr_vifout)); printf("pkts: %ld\n", (long)ntohl(s->tr_pktcnt)); printf("\t\t\t\tv_in: %ld ", (long)ntohl(r->tr_vifin)); printf("v_out: %ld ", (long)ntohl(r->tr_vifout)); printf("pkts: %ld\n", (long)ntohl(r->tr_pktcnt)); printf("\t\t\t\tv_in: %ld ", (long)(ntohl(s->tr_vifin) - ntohl(r->tr_vifin))); printf("v_out: %ld ", (long)(ntohl(s->tr_vifout) - ntohl(r->tr_vifout))); printf("pkts: %ld ", (long)(ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt))); printf("time: %d\n", timediff); printf("\t\t\t\treset: %x hoptime: %lx\n", *rst, ntohl(s->tr_qarr)); } } /* * A fixup to check if any pktcnt has been reset, and to fix the * byteorder bugs in mrouted 3.6 on little-endian machines. * * XXX Since periodic traffic sources are likely to have their * pktcnt periodically reset, should we save old values when * the reset occurs to keep slightly better statistics over * the long term? (e.g. SAP) */ void fixup_stats(base, prev, new, bugs) struct resp_buf *base, *prev, *new; int *bugs; { int rno = base->len; struct tr_resp *b = base->resps + rno; struct tr_resp *p = prev->resps + rno; struct tr_resp *n = new->resps + rno; int *r = bugs + rno; int res; int cleanup = 0; /* Check for byte-swappers. Only check on the first trace, * since long-running traces can wrap around and falsely trigger. */ while (--rno >= 0) { #ifdef TEST_ONLY u_int32 nvifout = ntohl(n->tr_vifout); u_int32 pvifout = ntohl(p->tr_vifout); #endif --n; --p; --b; #ifdef TEST_ONLY /*XXX this is still buggy, so disable it for release */ if ((*r & BUG_SWAP) || ((base == prev) && (nvifout - pvifout) > (byteswap(nvifout) - byteswap(pvifout)))) { if (1 || debug > 2) { printf("ip %s swaps; b %08x p %08x n %08x\n", inet_fmt(n->tr_inaddr, s1), ntohl(b->tr_vifout), pvifout, nvifout); } /* This host sends byteswapped reports; swap 'em */ if (!(*r & BUG_SWAP)) { *r |= BUG_SWAP; b->tr_qarr = byteswap(b->tr_qarr); b->tr_vifin = byteswap(b->tr_vifin); b->tr_vifout = byteswap(b->tr_vifout); b->tr_pktcnt = byteswap(b->tr_pktcnt); } n->tr_qarr = byteswap(n->tr_qarr); n->tr_vifin = byteswap(n->tr_vifin); n->tr_vifout = byteswap(n->tr_vifout); n->tr_pktcnt = byteswap(n->tr_pktcnt); } #endif /* * A missing parenthesis in mrouted 3.5-3.8's prune.c * causes extremely bogus time diff's. * One half of the time calculation was * inside an htonl() and one half wasn't. Therefore, on * a little-endian machine, both halves of the calculation * would get added together in the little end. Thus, the * low-order 2 bytes are either 0000 (no overflow) or * 0100 (overflow from the addition). * * Odds are against these particular bit patterns * happening in both prev and new for actual time values. */ if ((*r & BUG_BOGUSTIME) || (((ntohl(n->tr_qarr) & 0xfeff) == 0x0000) && ((ntohl(p->tr_qarr) & 0xfeff) == 0x0000))) { *r |= BUG_BOGUSTIME; n->tr_qarr = new->rtime; p->tr_qarr = prev->rtime; b->tr_qarr = base->rtime; } } rno = base->len; b = base->resps + rno; p = prev->resps + rno; n = new->resps + rno; r = bugs + rno; while (--rno >= 0) { --n; --p; --b; --r; /* * This hop has reset if: * - There were statistics in the base AND previous pass, AND * - There are less packets this time than the first time and * we didn't reset last time, OR * - There are less packets this time than last time, OR * - There are no statistics on this pass. * * The "and we didn't reset last time" is necessary in the * first branch of the OR because if the base is large and * we reset last time but the constant-resetter-avoidance * code kicked in so we delayed the copy of prev to base, * new could still be below base so we trigger the * constant-resetter code even though it was really only * a single reset. */ res = ((b->tr_pktcnt != 0xFFFFFFFF) && (p->tr_pktcnt != 0xFFFFFFFF) && ((!(*r & BUG_RESET) && ntohl(n->tr_pktcnt) < ntohl(b->tr_pktcnt)) || (ntohl(n->tr_pktcnt) < ntohl(p->tr_pktcnt)) || (n->tr_pktcnt == 0xFFFFFFFF))); if (debug > 2) { printf("\t\tip=%s, r=%d, res=%d\n", inet_fmt(b->tr_inaddr, s1), *r, res); if (res) printf("\t\tbase=%ld, prev=%ld, new=%ld\n", ntohl(b->tr_pktcnt), ntohl(p->tr_pktcnt), ntohl(n->tr_pktcnt)); } if (*r & BUG_RESET) { if (res || (*r & BUG_RESET2X)) { /* * This router appears to be a 3.4 with that nasty ol' * neighbor version bug, which causes it to constantly * reset. Just nuke the statistics for this node, and * don't even bother giving it the benefit of the * doubt from now on. */ p->tr_pktcnt = b->tr_pktcnt = n->tr_pktcnt; *r |= BUG_RESET2X; } else { /* * This is simply the situation that the original * fixup_stats was meant to deal with -- that a * 3.3 or 3.4 router deleted a cache entry while * traffic was still active. */ *r &= ~BUG_RESET; cleanup = 1; } } else if (res) *r |= BUG_RESET; } if (cleanup == 0) return; /* * If some hop reset its counters and didn't continue to * reset, then we pretend that the previous * trace was the first one. */ rno = base->len; b = base->resps + rno; p = prev->resps + rno; while (--rno >= 0) (--b)->tr_pktcnt = (--p)->tr_pktcnt; base->qtime = prev->qtime; base->rtime = prev->rtime; } /* * Check per-source losses along path and compare with threshold. */ int check_thresh(thresh, base, prev, new) int thresh; struct resp_buf *base, *prev, *new; { int rno = base->len - 1; struct tr_resp *b = base->resps + rno; struct tr_resp *p = prev->resps + rno; struct tr_resp *n = new->resps + rno; int g_out, g_lost; while (TRUE) { if ((n->tr_inaddr != b->tr_inaddr) || (n->tr_outaddr != b->tr_outaddr) || (n->tr_rmtaddr != b->tr_rmtaddr)) return 1; /* Route changed */ if (rno-- < 1) break; g_out = ntohl(n->tr_pktcnt) - ntohl(p->tr_pktcnt); b--; n--; p--; g_lost = g_out - (ntohl(n->tr_pktcnt) - ntohl(p->tr_pktcnt)); if (g_out && ((g_lost * 100 + (g_out >> 1))/ g_out) > thresh) { return TRUE; } } return FALSE; } /* * Print responses with statistics for forward path (from src to dst) */ int print_stats(base, prev, new, bugs, names) struct resp_buf *base, *prev, *new; int *bugs; char **names; { int rtt, hop; char *ms; u_int32 smask; int rno = base->len - 1; struct tr_resp *b = base->resps + rno; struct tr_resp *p = prev->resps + rno; struct tr_resp *n = new->resps + rno; int *r = bugs + rno; u_long resptime = new->rtime; u_long qarrtime = ntohl(n->tr_qarr); u_int ttl = MaX(1, n->tr_fttl) + 1; int first = (base == prev); VAL_TO_MASK(smask, b->tr_smask); printf(" Source Response Dest "); if (tunstats) printf("Packet Statistics For Only For Traffic\n"); else printf("Overall Packet Statistics For Traffic From\n"); (void)inet_fmt(base->qhdr.tr_src, s1); printf("%-15s %-15s ", ((b->tr_inaddr & smask) == (base->qhdr.tr_src & smask)) ? s1 : " * * * ", inet_fmt(base->qhdr.tr_raddr, s2)); (void)inet_fmt(base->igmp.igmp_group.s_addr, s2); if (tunstats) printf("All Multicast Traffic From %s\n", s1); else printf("Packet %s To %s\n", s1, s2); rtt = t_diff(resptime, new->qtime); ms = scale(&rtt); printf(" %c __/ rtt%5d%s ", (first && !verbose) ? 'v' : '|', rtt, ms); if (tunstats) printf("Lost/Sent = Pct Rate To %s\n", s2); else printf(" Rate Lost/Sent = Pct Rate\n"); if (!first || verbose) { hop = t_diff(resptime, qarrtime); ms = scale(&hop); printf(" v / hop%5d%s ", hop, ms); if (tunstats) printf("--------------------- --------------------\n"); else printf("------- ---------------------\n"); } if ((b->tr_inaddr & smask) != (base->qhdr.tr_src & smask) && b->tr_rmtaddr != 0) { printf("%-15s %-14s is the previous hop\n", inet_fmt(b->tr_rmtaddr, s1), inet_name(b->tr_rmtaddr)); printf(" v ^\n"); } if (debug > 2) { printf("\t\t\t\tv_in: %ld ", (long)ntohl(n->tr_vifin)); printf("v_out: %ld ", (long)ntohl(n->tr_vifout)); printf("pkts: %ld\n", (long)ntohl(n->tr_pktcnt)); printf("\t\t\t\tv_in: %ld ", (long)ntohl(b->tr_vifin)); printf("v_out: %ld ", (long)ntohl(b->tr_vifout)); printf("pkts: %ld\n", (long)ntohl(b->tr_pktcnt)); printf("\t\t\t\tv_in: %ld ", (long)(ntohl(n->tr_vifin) - ntohl(b->tr_vifin))); printf("v_out: %ld ", (long)(ntohl(n->tr_vifout) - ntohl(b->tr_vifout))); printf("pkts: %ld\n", (long)(ntohl(n->tr_pktcnt) - ntohl(b->tr_pktcnt))); printf("\t\t\t\treset: %x hoptime: %lx\n", *r, (long)ntohl(n->tr_qarr)); } while (TRUE) { if ((n->tr_inaddr != b->tr_inaddr) || (n->tr_outaddr != b->tr_outaddr) || (n->tr_rmtaddr != b->tr_rmtaddr)) return 1; /* Route changed */ if ((n->tr_inaddr != n->tr_outaddr) && n->tr_inaddr) printf("%-15s\n", inet_fmt(n->tr_inaddr, s1)); printf("%-15s %-14s %s%s\n", inet_fmt(n->tr_outaddr, s1), names[rno], flag_type(n->tr_rflags), (*r & BUG_NOPRINT) ? " [reset counters]" : ""); if (rno-- < 1) break; printf(" %c ^ ttl%5d ", (first && !verbose) ? 'v' : '|', ttl); stat_line(p, n, TRUE, r); if (!first || verbose) { resptime = qarrtime; qarrtime = ntohl((n-1)->tr_qarr); hop = t_diff(resptime, qarrtime); ms = scale(&hop); printf(" v | hop%5d%s", hop, ms); if (first) printf("\n"); else stat_line(b, n, TRUE, r); } --b, --p, --n, --r; ttl = MaX(ttl, MaX(1, n->tr_fttl) + base->len - rno); } printf(" %c \\__ ttl%5d ", (first && !verbose) ? 'v' : '|', ttl); stat_line(p, n, FALSE, r); if (!first || verbose) { hop = t_diff(qarrtime, new->qtime); ms = scale(&hop); printf(" v \\ hop%5d%s", hop, ms); if (first) printf("\n"); else stat_line(b, n, FALSE, r); } printf("%-15s %s\n", inet_fmt(base->qhdr.tr_dst, s1), !passive ? inet_fmt(lcl_addr, s2) : " * * * "); printf(" Receiver Query Source\n\n"); return 0; } /* * Determine whether or not the path has changed. */ int path_changed(base, new) struct resp_buf *base, *new; { int rno = base->len - 1; struct tr_resp *b = base->resps + rno; struct tr_resp *n = new->resps + rno; while (rno-- >= 0) { if ((n->tr_inaddr != b->tr_inaddr) || (n->tr_outaddr != b->tr_outaddr) || (n->tr_rmtaddr != b->tr_rmtaddr)) return 1; /* Route changed */ if ((b->tr_rflags == TR_NO_RTE) && (n->tr_rflags != TR_NO_RTE)) return 1; /* Route got longer? */ --n; --b; } return 0; } /*************************************************************************** * main ***************************************************************************/ int main(argc, argv) int argc; char *argv[]; { int udp; struct sockaddr_in addr; int addrlen = sizeof(addr); int recvlen; struct timeval tv; struct resp_buf *prev, *new; struct tr_resp *r; u_int32 smask; int rno; int hops, nexthop, tries; u_int32 lastout = 0; int numstats = 1; int waittime; int seed; int hopbyhop; int i; int printed = 1; if (geteuid() != 0) errx(1, "must be root"); /* * We might get spawned by vat with the audio device open. * Close everything but stdin, stdout, stderr. */ for (i = 3; i < 255; i++) close(i); init_igmp(); setuid(getuid()); argv++, argc--; if (argc == 0) usage(); while (argc > 0 && *argv[0] == '-') { char *p = *argv++; argc--; p++; do { char c = *p++; char *arg = (char *) 0; if (isdigit(*p)) { arg = p; p = ""; } else if (argc > 0) arg = argv[0]; switch (c) { case 'd': /* Unlisted debug print option */ if (arg && isdigit(*arg)) { debug = atoi(arg); if (debug < 0) debug = 0; if (debug > 3) debug = 3; if (arg == argv[0]) argv++, argc--; break; } else usage(); case 'M': /* Use multicast for reponse */ multicast = TRUE; break; case 'U': /* Use unicast for response */ unicast = TRUE; break; case 'L': /* Trace w/ loss threshold */ if (arg && isdigit(*arg)) { lossthresh = atoi(arg); if (lossthresh < 0) lossthresh = 0; numstats = 3153600; if (arg == argv[0]) argv++, argc--; break; } else usage(); break; case 'O': /* Don't use IP options */ sendopts = FALSE; break; case 'P': /* Just watch the path */ printstats = FALSE; numstats = 3153600; break; case 'Q': /* (undoc.) always use this QID */ if (arg && isdigit(*arg)) { staticqid = atoi(arg); if (staticqid < 0) staticqid = 0; if (arg == argv[0]) argv++, argc--; break; } else usage(); break; case 'T': /* Print confusing tunnel stats */ tunstats = TRUE; break; case 'W': /* Cisco's "weak" mtrace */ weak = TRUE; break; case 'V': /* Print version and exit */ /* * FreeBSD wants to have its own Id string, so * determination of the version number has to change. * XXX Note that this must be changed by hand on importing * XXX new versions! */ { char *r = strdup(rcsid); char *s = strchr(r, ','); while (s && *(s+1) != 'v') s = strchr(s + 1, ','); if (s) { char *q; s += 3; /* , v sp */ q = strchr(s, ' '); if (q) *q = '\0'; fprintf(stderr, "mtrace version 5.2/%s\n", s); } else { fprintf(stderr, "mtrace could not determine version number!?\n"); } exit(1); } break; case 'l': /* Loop updating stats indefinitely */ numstats = 3153600; break; case 'n': /* Don't reverse map host addresses */ numeric = TRUE; break; case 'p': /* Passive listen for traces */ passive = TRUE; break; case 'v': /* Verbosity */ verbose = TRUE; break; case 's': /* Short form, don't wait for stats */ numstats = 0; break; case 'w': /* Time to wait for packet arrival */ if (arg && isdigit(*arg)) { timeout = atoi(arg); if (timeout < 1) timeout = 1; if (arg == argv[0]) argv++, argc--; break; } else usage(); case 'f': /* first hop */ if (arg && isdigit(*arg)) { qno = atoi(arg); if (qno > MAXHOPS) qno = MAXHOPS; else if (qno < 1) qno = 0; if (arg == argv[0]) argv++, argc--; fflag++; break; } else usage(); case 'm': /* Max number of hops to trace */ if (arg && isdigit(*arg)) { qno = atoi(arg); if (qno > MAXHOPS) qno = MAXHOPS; else if (qno < 1) qno = 0; if (arg == argv[0]) argv++, argc--; break; } else usage(); case 'q': /* Number of query retries */ if (arg && isdigit(*arg)) { nqueries = atoi(arg); if (nqueries < 1) nqueries = 1; if (arg == argv[0]) argv++, argc--; break; } else usage(); case 'g': /* Last-hop gateway (dest of query) */ if (arg && (gwy = host_addr(arg))) { if (arg == argv[0]) argv++, argc--; break; } else usage(); case 't': /* TTL for query packet */ if (arg && isdigit(*arg)) { qttl = atoi(arg); if (qttl < 1) qttl = 1; rttl = qttl; if (arg == argv[0]) argv++, argc--; break; } else usage(); case 'e': /* Extra hops past non-responder */ if (arg && isdigit(*arg)) { extrahops = atoi(arg); if (extrahops < 0) extrahops = 0; if (arg == argv[0]) argv++, argc--; break; } else usage(); case 'r': /* Dest for response packet */ if (arg && (raddr = host_addr(arg))) { if (arg == argv[0]) argv++, argc--; break; } else usage(); case 'i': /* Local interface address */ if (arg && (lcl_addr = host_addr(arg))) { if (arg == argv[0]) argv++, argc--; break; } else usage(); case 'S': /* Stat accumulation interval */ if (arg && isdigit(*arg)) { statint = atoi(arg); if (statint < 1) statint = 1; if (arg == argv[0]) argv++, argc--; break; } else usage(); default: usage(); } } while (*p); } if (argc > 0 && (qsrc = host_addr(argv[0]))) { /* Source of path */ if (IN_MULTICAST(ntohl(qsrc))) { if (gwy) { /* Should probably rewrite arg parsing at some point, as * this makes "mtrace -g foo 224.1.2.3 224.2.3.4" valid!... */ qgrp = qsrc; qsrc = 0; } else { usage(); } } argv++, argc--; if (argc > 0 && (qdst = host_addr(argv[0]))) { /* Dest of path */ argv++, argc--; if (argc > 0 && (qgrp = host_addr(argv[0]))) { /* Path via group */ argv++, argc--; } if (IN_MULTICAST(ntohl(qdst))) { u_int32 temp = qdst; qdst = qgrp; qgrp = temp; if (IN_MULTICAST(ntohl(qdst))) usage(); } else if (qgrp && !IN_MULTICAST(ntohl(qgrp))) usage(); } } if (passive) { passive_mode(); return(0); } if (argc > 0) { usage(); } #ifdef SUNOS5 if (sendopts) checkforsolarisbug(); #endif /* * Set useful defaults for as many parameters as possible. */ defgrp = 0; /* Default to no group */ query_cast = htonl(0xE0000002); /* All routers multicast addr */ resp_cast = htonl(0xE0000120); /* Mtrace response multicast addr */ if (qgrp == 0) { if (!weak) qgrp = defgrp; if (printstats && numstats != 0 && !tunstats) { /* Stats are useless without a group */ warnx( "WARNING: no multicast group specified, so no statistics printed"); numstats = 0; } } else { if (weak) warnx( "WARNING: group was specified so not performing \"weak\" mtrace"); } /* * Get default local address for multicasts to use in setting defaults. */ addr.sin_family = AF_INET; #if (defined(BSD) && (BSD >= 199103)) addr.sin_len = sizeof(addr); #endif addr.sin_addr.s_addr = qgrp ? qgrp : query_cast; addr.sin_port = htons(2000); /* Any port above 1024 will do */ /* * Note that getsockname() can return 0 on some systems * (notably SunOS 5.x, x < 6). This is taken care of in * get_netmask(). If the default multicast interface (set * with the route for 224.0.0.0) is not the same as the * hostname, mtrace -i [if_addr] will have to be used. */ if (((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0) || (connect(udp, (struct sockaddr *) &addr, sizeof(addr)) < 0) || getsockname(udp, (struct sockaddr *) &addr, &addrlen) < 0) err(-1, "determining local address"); #ifdef SUNOS5 /* * SunOS 5.X prior to SunOS 2.6, getsockname returns 0 for udp socket. * This call to sysinfo will return the hostname. * If the default multicast interfface (set with the route * for 224.0.0.0) is not the same as the hostname, * mtrace -i [if_addr] will have to be used. */ if (addr.sin_addr.s_addr == 0) { char myhostname[MAXHOSTNAMELEN]; struct hostent *hp; int error; error = sysinfo(SI_HOSTNAME, myhostname, sizeof(myhostname)); if (error == -1) err(1, "getting my hostname"); hp = gethostbyname(myhostname); if (hp == NULL || hp->h_addrtype != AF_INET || hp->h_length != sizeof(addr.sin_addr)) err(1, "finding IP address for my hostname"); memcpy((char *)&addr.sin_addr.s_addr, hp->h_addr, hp->h_length); } #endif /* * Default destination for path to be queried is the local host. * When gateway specified, default destination is that gateway * and default source is local host. */ if (qdst == 0) { qdst = lcl_addr ? lcl_addr : addr.sin_addr.s_addr; dst_netmask = get_netmask(udp, &qdst); if (gwy && (gwy & dst_netmask) != (qdst & dst_netmask) && !IN_MULTICAST(ntohl(gwy))) qdst = gwy; } if (qsrc == 0 && gwy) qsrc = lcl_addr ? lcl_addr : addr.sin_addr.s_addr; if (qsrc == 0) usage(); if (!dst_netmask) dst_netmask = get_netmask(udp, &qdst); close(udp); if (lcl_addr == 0) lcl_addr = addr.sin_addr.s_addr; /* * Initialize the seed for random query identifiers. */ gettimeofday(&tv, 0); seed = tv.tv_usec ^ lcl_addr; #ifdef SYSV srand48(seed); -#else - srandom(seed); #endif /* * Protect against unicast queries to mrouted versions that might crash. * Also use the obsolete "can mtrace" neighbor bit to warn about * older implementations. */ if (gwy && !IN_MULTICAST(ntohl(gwy))) if (send_recv(gwy, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2, 1, &incr[0], NULL)) { int flags = ntohl(incr[0].igmp.igmp_group.s_addr); int version = flags & 0xFFFF; int info = (flags & 0xFF0000) >> 16; if (version == 0x0303 || version == 0x0503) { printf("Don't use -g to address an mrouted 3.%d, it might crash\n", (version >> 8) & 0xFF); exit(0); } if ((info & 0x08) == 0) { printf("mtrace: "); print_host(gwy); printf(" probably doesn't support mtrace, trying anyway...\n"); } } printf("Mtrace from %s to %s via group %s\n", inet_fmt(qsrc, s1), inet_fmt(qdst, s2), inet_fmt(qgrp, s3)); if ((qdst & dst_netmask) == (qsrc & dst_netmask)) fprintf(stderr, "mtrace: Source & receiver appear to be directly connected\n"); /* * If the response is to be a multicast address, make sure we * are listening on that multicast address. */ if (raddr) { if (IN_MULTICAST(ntohl(raddr))) k_join(raddr, lcl_addr); } else k_join(resp_cast, lcl_addr); memset(&base, 0, sizeof(base)); /* * If the destination is on the local net, the last-hop router can * be found by multicast to the all-routers multicast group. * Otherwise, use the group address that is the subject of the * query since by definition the last-hop router will be a member. * Set default TTLs for local remote multicasts. */ if (gwy == 0) if ((qdst & dst_netmask) == (lcl_addr & dst_netmask)) tdst = query_cast; else tdst = qgrp; else tdst = gwy; if (tdst == 0 && qgrp == 0) errx(1, "mtrace: weak mtrace requires -g if destination is not local.\n"); if (IN_MULTICAST(ntohl(tdst))) { k_set_loop(1); /* If I am running on a router, I need to hear this */ if (tdst == query_cast) k_set_ttl(qttl ? qttl : 1); else k_set_ttl(qttl ? qttl : MULTICAST_TTL1); } /* * Try a query at the requested number of hops or MAXHOPS if unspecified. */ if (qno == 0) { hops = MAXHOPS; tries = 1; printf("Querying full reverse path... "); fflush(stdout); } else { hops = qno; tries = nqueries; if (fflag) printf("Querying full reverse path, starting at hop %d...", qno); else printf("Querying reverse path, maximum %d hops... ", qno); fflush(stdout); } base.rtime = 0; base.len = 0; hopbyhop = FALSE; recvlen = send_recv(tdst, IGMP_MTRACE, hops, tries, &base, mtrace_callback); /* * If the initial query was successful, print it. Otherwise, if * the query max hop count is the default of zero, loop starting * from one until there is no response for extrahops more hops. The * extra hops allow getting past an mtrace-capable mrouter that can't * send multicast packets because all phyints are disabled. */ if (recvlen) { printf("\n 0 "); print_host(qdst); printf("\n"); print_trace(1, &base, names); r = base.resps + base.len - 1; if (r->tr_rflags == TR_OLD_ROUTER || r->tr_rflags == TR_NO_SPACE || (qno != 0 && r->tr_rmtaddr != 0 && !fflag)) { printf("%3d ", -(base.len+1)); what_kind(&base, r->tr_rflags == TR_OLD_ROUTER ? "doesn't support mtrace" : "is the next hop"); } else { if (fflag) { nexthop = hops = qno; goto continuehop; } VAL_TO_MASK(smask, r->tr_smask); if ((r->tr_inaddr & smask) == (qsrc & smask)) { printf("%3d ", -(base.len+1)); print_host(qsrc); printf("\n"); } } } else if (qno == 0) { hopbyhop = TRUE; printf("switching to hop-by-hop:\n 0 "); print_host(qdst); printf("\n"); for (hops = 1, nexthop = 1; hops <= MAXHOPS; ++hops) { printf("%3d ", -hops); fflush(stdout); /* * After a successful first hop, try switching to the unicast * address of the last-hop router instead of multicasting the * trace query. This should be safe for mrouted versions 3.3 * and 3.5 because there is a long route timeout with metric * infinity before a route disappears. Switching to unicast * reduces the amount of multicast traffic and avoids a bug * with duplicate suppression in mrouted 3.5. */ if (hops == 2 && gwy == 0 && lastout != 0 && (recvlen = send_recv(lastout, IGMP_MTRACE, hops, 1, &base, mtrace_callback))) tdst = lastout; else recvlen = send_recv(tdst, IGMP_MTRACE, hops, nqueries, &base, mtrace_callback); if (recvlen == 0) { /*if (hops == 1) break;*/ if (hops == nexthop) { if (hops == 1) { printf("\n"); } else if (what_kind(&base, "didn't respond")) { /* the ask_neighbors determined that the * not-responding router is the first-hop. */ break; } if (extrahops == 0) break; } else if (hops < nexthop + extrahops) { printf("\n"); } else { printf("...giving up\n"); break; } continue; } if (base.len == hops && (hops == 1 || (base.resps+nexthop-2)->tr_outaddr == lastout)) { if (hops == nexthop) { print_trace(-hops, &base, names); } else { printf("\nResuming...\n"); print_trace(nexthop, &base, names); } } else { if (base.len < hops) { /* * A shorter trace than requested means a fatal error * occurred along the path, or that the route changed * to a shorter one. * * If the trace is longer than the last one we received, * then we are resuming from a skipped router (but there * is still probably a problem). * * If the trace is shorter than the last one we * received, then the route must have changed (and * there is still probably a problem). */ if (nexthop <= base.len) { printf("\nResuming...\n"); print_trace(nexthop, &base, names); } else if (nexthop > base.len + 1) { hops = base.len; printf("\nRoute must have changed...\n"); print_trace(1, &base, names); } } else { /* * The last hop address is not the same as it was. * If we didn't know the last hop then we just * got the first response from a hop-by-hop trace; * if we did know the last hop then * the route probably changed underneath us. */ hops = base.len; if (lastout != 0) printf("\nRoute must have changed...\n"); else printf("\nResuming...\n"); print_trace(1, &base, names); } } continuehop: r = base.resps + base.len - 1; lastout = r->tr_outaddr; if (base.len < hops || r->tr_rmtaddr == 0 || (r->tr_rflags & 0x80)) { VAL_TO_MASK(smask, r->tr_smask); if (r->tr_rmtaddr) { if (hops != nexthop) { printf("\n%3d ", -(base.len+1)); } what_kind(&base, r->tr_rflags == TR_OLD_ROUTER ? "doesn't support mtrace" : "would be the next hop"); /* XXX could do segmented trace if TR_NO_SPACE */ } else if (r->tr_rflags == TR_NO_ERR && (r->tr_inaddr & smask) == (qsrc & smask)) { printf("%3d ", -(hops + 1)); print_host(qsrc); printf("\n"); } break; } nexthop = hops + 1; } } if (base.rtime == 0) { printf("Timed out receiving responses\n"); if (IN_MULTICAST(ntohl(tdst))) if (tdst == query_cast) printf("Perhaps no local router has a route for source %s\n", inet_fmt(qsrc, s1)); else printf("Perhaps receiver %s is not a member of group %s,\n\ or no router local to it has a route for source %s,\n\ or multicast at ttl %d doesn't reach its last-hop router for that source\n", inet_fmt(qdst, s2), inet_fmt(qgrp, s3), inet_fmt(qsrc, s1), qttl ? qttl : MULTICAST_TTL1); exit(1); } printf("Round trip time %d ms; ", t_diff(base.rtime, base.qtime)); { struct tr_resp *n = base.resps + base.len - 1; u_int ttl = n->tr_fttl + 1; rno = base.len - 1; while (--rno > 0) { --n; ttl = MaX(ttl, MaX(1, n->tr_fttl) + base.len - rno); } printf("total ttl of %d required.\n\n",ttl); } /* * Use the saved response which was the longest one received, * and make additional probes after delay to measure loss. */ raddr = base.qhdr.tr_raddr; rttl = TR_GETTTL(base.qhdr.tr_rttlqid); gettimeofday(&tv, 0); waittime = statint - (((tv.tv_sec + JAN_1970) & 0xFFFF) - (base.qtime >> 16)); prev = &base; new = &incr[numstats&1]; /* * Zero out bug-avoidance counters */ memset(bugs, 0, sizeof(bugs)); if (!printstats) printf("Monitoring path.."); while (numstats--) { if (waittime < 1) printf("\n"); else { if (printstats && (lossthresh == 0 || printed)) { printf("Waiting to accumulate statistics..."); } else { printf("."); } fflush(stdout); sleep((unsigned)waittime); } printed = 0; rno = hopbyhop ? base.len : qno ? qno : MAXHOPS; recvlen = send_recv(tdst, IGMP_MTRACE, rno, nqueries, new, mtrace_callback); if (recvlen == 0) { printf("Timed out.\n"); if (numstats) { numstats++; continue; } else exit(1); } if (base.len != new->len || path_changed(&base, new)) { printf("%s", base.len == new->len ? "Route changed" : "Trace length doesn't match"); if (!printstats) printf(" after %d seconds", (int)((new->qtime - base.qtime) >> 16)); printf(":\n"); printandcontinue: print_trace(1, new, names); numstats++; bcopy(new, &base, sizeof(base)); nexthop = hops = new->len; printf("Continuing with hop-by-hop...\n"); goto continuehop; } if (printstats) { if (new->igmp.igmp_group.s_addr != qgrp || new->qhdr.tr_src != qsrc || new->qhdr.tr_dst != qdst) printf("\nWARNING: trace modified en route; statistics may be incorrect\n"); fixup_stats(&base, prev, new, bugs); if ((lossthresh == 0) || check_thresh(lossthresh, &base, prev, new)) { printf("Results after %d seconds", (int)((new->qtime - base.qtime) >> 16)); if (lossthresh) printf(" (this trace %d seconds)", (int)((new->qtime - prev->qtime) >> 16)); if (verbose) { time_t t = time(0); struct tm *qr = localtime(&t); printf(" qid 0x%06x at %2d:%02d:%02d", TR_GETQID(base.qhdr.tr_rttlqid), qr->tm_hour, qr->tm_min, qr->tm_sec); } printf(":\n\n"); printed = 1; if (print_stats(&base, prev, new, bugs, names)) { printf("This should have been detected earlier, but "); printf("Route changed:\n"); goto printandcontinue; } } } prev = new; new = &incr[numstats&1]; waittime = statint; } /* * If the response was multicast back, leave the group */ if (raddr) { if (IN_MULTICAST(ntohl(raddr))) k_leave(raddr, lcl_addr); } else k_leave(resp_cast, lcl_addr); return (0); } static void usage() { fprintf(stderr, "%s\n%s\n%s\n", "usage: mtrace [-MUOPTWVlnpvs] [-e extra_hops] [-f first_hop] [-i if_addr]", " [-g gateway] [-m max_hops] [-q nqueries] [-r resp_dest]", " [-S statint] [-t ttl] [-w wait] source [receiver] [group]"); exit(1); } void check_vif_state() { log(LOG_WARNING, errno, "sendto"); } /* * Log errors and other messages to stderr, according to the severity * of the message and the current debug level. For errors of severity * LOG_ERR or worse, terminate the program. */ #ifdef __STDC__ void log(int severity, int syserr, char *format, ...) { va_list ap; char fmt[100]; va_start(ap, format); #else /*VARARGS3*/ void log(severity, syserr, format, va_alist) int severity, syserr; char *format; va_dcl { va_list ap; char fmt[100]; va_start(ap); #endif switch (debug) { case 0: if (severity > LOG_WARNING) return; case 1: if (severity > LOG_NOTICE) return; case 2: if (severity > LOG_INFO ) return; default: fmt[0] = '\0'; if (severity == LOG_WARNING) strcpy(fmt, "warning - "); strncat(fmt, format, sizeof(fmt)-strlen(fmt)); fmt[sizeof(fmt)-1]='\0'; vfprintf(stderr, fmt, ap); if (syserr == 0) fprintf(stderr, "\n"); else if (syserr < sys_nerr) fprintf(stderr, ": %s\n", sys_errlist[syserr]); else fprintf(stderr, ": errno %d\n", syserr); } if (severity <= LOG_ERR) exit(1); } diff --git a/usr.sbin/mrouted/prune.c b/usr.sbin/mrouted/prune.c index 7ffb34b23a7d..acefaf921f71 100644 --- a/usr.sbin/mrouted/prune.c +++ b/usr.sbin/mrouted/prune.c @@ -1,2619 +1,2619 @@ /* * The mrouted program is covered by the license in the accompanying file * named "LICENSE". Use of the mrouted program represents acceptance of * the terms and conditions listed in that file. * * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of * Leland Stanford Junior University. * * * prune.c,v 3.8.4.59 1998/03/01 02:06:32 fenner Exp */ #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include "defs.h" extern int cache_lifetime; extern int prune_lifetime; extern struct rtentry *routing_table; extern int phys_vif; extern int allow_black_holes; /* * randomize value to obtain a value between .5x and 1.5x * in order to prevent synchronization */ #ifdef SYSV #define JITTERED_VALUE(x) ((x)/2 + (lrand48() % (x))) #else -#define JITTERED_VALUE(x) ((x)/2 + (random() % (x))) +#define JITTERED_VALUE(x) ((x)/2 + (arc4random() % (x))) #endif #define CACHE_LIFETIME(x) JITTERED_VALUE(x) /* XXX */ struct gtable *kernel_table; /* ptr to list of kernel grp entries*/ static struct gtable *kernel_no_route; /* list of grp entries w/o routes */ struct gtable *gtp; /* pointer for kernel rt entries */ unsigned int kroutes; /* current number of cache entries */ /**************************************************************************** Functions that are local to prune.c ****************************************************************************/ static int scoped_addr __P((vifi_t vifi, u_int32 addr)); static void prun_add_ttls __P((struct gtable *gt)); static int pruning_neighbor __P((vifi_t vifi, u_int32 addr)); static int can_mtrace __P((vifi_t vifi, u_int32 addr)); static struct ptable * find_prune_entry __P((u_int32 vr, struct ptable *pt)); static void remove_sources __P((struct gtable *gt)); static void rexmit_prune __P((void *arg)); static void expire_prune __P((vifi_t vifi, struct gtable *gt)); static void send_prune __P((struct gtable *gt)); static void send_graft __P((struct gtable *gt)); static void send_graft_ack __P((u_int32 src, u_int32 dst, u_int32 origin, u_int32 grp, vifi_t vifi)); static void update_kernel __P((struct gtable *g)); /* * Updates the ttl values for each vif. */ static void prun_add_ttls(gt) struct gtable *gt; { struct uvif *v; vifi_t vifi; for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) { if (VIFM_ISSET(vifi, gt->gt_grpmems)) gt->gt_ttls[vifi] = v->uv_threshold; else gt->gt_ttls[vifi] = 0; } } /* * checks for scoped multicast addresses * XXX I want to make the check of allow_black_holes based on ALLOW_BLACK_HOLES * but macros are not functions. */ #define GET_SCOPE(gt) { \ register vifi_t _i; \ VIFM_CLRALL((gt)->gt_scope); \ if (allow_black_holes || \ (ntohl((gt)->gt_mcastgrp) & 0xff000000) == 0xef000000) \ for (_i = 0; _i < numvifs; _i++) \ if (scoped_addr(_i, (gt)->gt_mcastgrp)) \ VIFM_SET(_i, (gt)->gt_scope); \ } \ if ((gt)->gt_route == NULL || ((gt)->gt_route->rt_parent != NO_VIF && \ VIFM_ISSET((gt)->gt_route->rt_parent, (gt)->gt_scope))) \ VIFM_SETALL((gt)->gt_scope); #define APPLY_SCOPE(gt) VIFM_CLR_MASK((gt)->gt_grpmems, (gt)->gt_scope) #define GET_MEMBERSHIP(gt, vifi) { \ if ((gt)->gt_route && \ VIFM_ISSET((vifi), (gt)->gt_route->rt_children) && \ (!SUBS_ARE_PRUNED((gt)->gt_route->rt_subordinates, \ uvifs[vifi].uv_nbrmap, (gt)->gt_prunes) || \ grplst_mem((vifi), (gt)->gt_mcastgrp))) \ VIFM_SET((vifi), (gt)->gt_grpmems); \ } static int scoped_addr(vifi, addr) vifi_t vifi; u_int32 addr; { struct vif_acl *acl; for (acl = uvifs[vifi].uv_acl; acl; acl = acl->acl_next) if ((addr & acl->acl_mask) == acl->acl_addr) return 1; return 0; } /* * Determine the list of outgoing vifs, based upon * route subordinates, prunes received, and group * memberships. */ void determine_forwvifs(gt) struct gtable *gt; { vifi_t i; VIFM_CLRALL(gt->gt_grpmems); for (i = 0; i < numvifs; i++) { GET_MEMBERSHIP(gt, i); } GET_SCOPE(gt); APPLY_SCOPE(gt); } /* * Send a prune or a graft if necessary. */ void send_prune_or_graft(gt) struct gtable *gt; { if (VIFM_ISEMPTY(gt->gt_grpmems)) send_prune(gt); else if (gt->gt_prsent_timer) send_graft(gt); } /* * Determine if mcastgrp has a listener on vifi */ int grplst_mem(vifi, mcastgrp) vifi_t vifi; u_int32 mcastgrp; { register struct listaddr *g; register struct uvif *v; v = &uvifs[vifi]; for (g = v->uv_groups; g != NULL; g = g->al_next) if (mcastgrp == g->al_addr) return 1; return 0; } /* * Finds the group entry with the specified source and netmask. * If netmask is 0, it uses the route's netmask. * * Returns TRUE if found a match, and the global variable gtp is left * pointing to entry before the found entry. * Returns FALSE if no exact match found, gtp is left pointing to before * the entry in question belongs, or is NULL if the it belongs at the * head of the list. */ int find_src_grp(src, mask, grp) u_int32 src; u_int32 mask; u_int32 grp; { struct gtable *gt; gtp = NULL; gt = kernel_table; while (gt != NULL) { if (grp == gt->gt_mcastgrp && (mask ? (gt->gt_route->rt_origin == src && gt->gt_route->rt_originmask == mask) : ((src & gt->gt_route->rt_originmask) == gt->gt_route->rt_origin))) return TRUE; if (ntohl(grp) > ntohl(gt->gt_mcastgrp) || (grp == gt->gt_mcastgrp && (ntohl(mask) < ntohl(gt->gt_route->rt_originmask) || (mask == gt->gt_route->rt_originmask && (ntohl(src) > ntohl(gt->gt_route->rt_origin)))))) { gtp = gt; gt = gt->gt_gnext; } else break; } return FALSE; } /* * Check if the neighbor supports pruning */ static int pruning_neighbor(vifi, addr) vifi_t vifi; u_int32 addr; { struct listaddr *n = neighbor_info(vifi, addr); int vers; if (n == NULL) return 0; vers = NBR_VERS(n); return (vers >= 0x0300 && ((vers & 0xff00) != 0x0a00)); } /* * Can the neighbor in question handle multicast traceroute? */ static int can_mtrace(vifi, addr) vifi_t vifi; u_int32 addr; { struct listaddr *n = neighbor_info(vifi, addr); int vers; if (n == NULL) return 1; /* fail "safe" */ vers = NBR_VERS(n); return (vers >= 0x0303 && ((vers & 0xff00) != 0x0a00)); } /* * Returns the prune entry of the router, or NULL if none exists */ static struct ptable * find_prune_entry(vr, pt) u_int32 vr; struct ptable *pt; { while (pt) { if (pt->pt_router == vr) return pt; pt = pt->pt_next; } return NULL; } /* * Remove all the sources hanging off the group table entry from the kernel * cache. Remember the packet counts wherever possible, to keep the mtrace * counters consistent. This prepares for possible prune retransmission, * either on a multi-access network or when a prune that we sent upstream * has expired. */ static void remove_sources(gt) struct gtable *gt; { struct stable *st; struct sioc_sg_req sg_req; sg_req.grp.s_addr = gt->gt_mcastgrp; /* * call k_del_rg() on every one of the gt->gt_srctbl entries * but first save the packet count so that the mtrace packet * counters can remain approximately correct. There's a race * here but it's minor. */ for (st = gt->gt_srctbl; st; st = st->st_next) { if (st->st_ctime == 0) continue; IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "rexmit_prune deleting (%s %s) (next is %d sec)", inet_fmt(st->st_origin, s1), inet_fmt(gt->gt_mcastgrp, s2), gt->gt_prune_rexmit); sg_req.src.s_addr = st->st_origin; if (ioctl(udp_socket, SIOCGETSGCNT, (char *)&sg_req) < 0) { sg_req.pktcnt = 0; } k_del_rg(st->st_origin, gt); st->st_ctime = 0; /* flag that it's not in the kernel any more */ st->st_savpkt += sg_req.pktcnt; kroutes--; } /* * Now, add_table_entry will prune when asked to add a cache entry. */ } /* * Prepare for possible prune retransmission */ static void rexmit_prune(arg) void *arg; { struct gtable *gt = *(struct gtable **)arg; free(arg); gt->gt_rexmit_timer = 0; /* Make sure we're still not forwarding traffic */ if (!VIFM_ISEMPTY(gt->gt_grpmems)) { IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "rexmit_prune (%s %s): gm:%x", RT_FMT(gt->gt_route, s1), inet_fmt(gt->gt_mcastgrp, s2), gt->gt_grpmems); return; } remove_sources(gt); } /* * Send a prune message to the dominant router for * this source. * * Record an entry that a prune was sent for this group */ static void send_prune(gt) struct gtable *gt; { struct ptable *pt; char *p; int i; int datalen; u_int32 dst; u_int32 tmp; int rexmitting = 0; struct uvif *v; /* * Can't process a prune if we don't have an associated route * or if the route points to a local interface. */ if (gt->gt_route == NULL || gt->gt_route->rt_parent == NO_VIF || gt->gt_route->rt_gateway == 0) return; /* Don't send a prune to a non-pruning router */ if (!pruning_neighbor(gt->gt_route->rt_parent, gt->gt_route->rt_gateway)) return; v = &uvifs[gt->gt_route->rt_parent]; /* * sends a prune message to the router upstream. */ #if 0 dst = v->uv_flags & VIFF_TUNNEL ? dvmrp_group : gt->gt_route->rt_gateway; /*XXX*/ #else dst = gt->gt_route->rt_gateway; #endif p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN; datalen = 0; /* * determine prune lifetime, if this isn't a retransmission. * * Use interface-specified lifetime if there is one. */ if (gt->gt_prsent_timer == 0) { int l = prune_lifetime; if (v->uv_prune_lifetime != 0) l = v->uv_prune_lifetime; gt->gt_prsent_timer = JITTERED_VALUE(l); for (pt = gt->gt_pruntbl; pt; pt = pt->pt_next) if (pt->pt_timer < gt->gt_prsent_timer) gt->gt_prsent_timer = pt->pt_timer; } else if (gt->gt_prsent_timer < 0) { IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "asked to rexmit? (%s,%s)/%d on vif %d to %s with negative time", RT_FMT(gt->gt_route, s1), inet_fmt(gt->gt_mcastgrp, s2), gt->gt_prsent_timer, gt->gt_route->rt_parent, inet_fmt(gt->gt_route->rt_gateway, s3)); return; } else rexmitting = 1; if (rexmitting && !(v->uv_flags & VIFF_REXMIT_PRUNES)) { IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "not rexmitting prune for (%s %s)/%d on vif %d to %s", RT_FMT(gt->gt_route, s1), inet_fmt(gt->gt_mcastgrp, s2), gt->gt_prsent_timer, gt->gt_route->rt_parent, inet_fmt(gt->gt_route->rt_gateway, s3)); return; } if (gt->gt_prsent_timer <= MIN_PRUNE_LIFE) { IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "not bothering to send prune for (%s,%s)/%d on vif %d to %s because it's too short", RT_FMT(gt->gt_route, s1), inet_fmt(gt->gt_mcastgrp, s2), gt->gt_prsent_timer, gt->gt_route->rt_parent, inet_fmt(gt->gt_route->rt_gateway, s3)); return; } /* * If we have a graft pending, cancel graft retransmission */ gt->gt_grftsnt = 0; for (i = 0; i < 4; i++) *p++ = ((char *)&(gt->gt_route->rt_origin))[i]; for (i = 0; i < 4; i++) *p++ = ((char *)&(gt->gt_mcastgrp))[i]; tmp = htonl(gt->gt_prsent_timer); for (i = 0; i < 4; i++) *p++ = ((char *)&(tmp))[i]; datalen += 12; send_on_vif(v, dst, DVMRP_PRUNE, datalen); IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "%s prune for (%s %s)/%d on vif %d to %s", rexmitting ? "rexmitted" : "sent", RT_FMT(gt->gt_route, s1), inet_fmt(gt->gt_mcastgrp, s2), gt->gt_prsent_timer, gt->gt_route->rt_parent, inet_fmt(gt->gt_route->rt_gateway, s3)); if ((v->uv_flags & VIFF_REXMIT_PRUNES) && gt->gt_rexmit_timer == 0 && gt->gt_prsent_timer > gt->gt_prune_rexmit) { struct gtable **arg = (struct gtable **)malloc(sizeof (struct gtable **)); *arg = gt; gt->gt_rexmit_timer = timer_setTimer( JITTERED_VALUE(gt->gt_prune_rexmit), rexmit_prune, arg); gt->gt_prune_rexmit *= 2; } } /* * a prune was sent upstream * so, a graft has to be sent to annul the prune * set up a graft timer so that if an ack is not * heard within that time, another graft request * is sent out. */ static void send_graft(gt) struct gtable *gt; { register char *p; register int i; int datalen; u_int32 dst; /* Can't send a graft without an associated route */ if (gt->gt_route == NULL || gt->gt_route->rt_parent == NO_VIF) { gt->gt_grftsnt = 0; return; } gt->gt_prsent_timer = 0; gt->gt_prune_rexmit = PRUNE_REXMIT_VAL; if (gt->gt_rexmit_timer) timer_clearTimer(gt->gt_rexmit_timer); if (gt->gt_grftsnt == 0) gt->gt_grftsnt = 1; #if 0 dst = uvifs[gt->gt_route->rt_parent].uv_flags & VIFF_TUNNEL ? dvmrp_group : gt->gt_route->rt_gateway; /*XXX*/ #else dst = gt->gt_route->rt_gateway; #endif p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN; datalen = 0; for (i = 0; i < 4; i++) *p++ = ((char *)&(gt->gt_route->rt_origin))[i]; for (i = 0; i < 4; i++) *p++ = ((char *)&(gt->gt_mcastgrp))[i]; datalen += 8; send_on_vif(&uvifs[gt->gt_route->rt_parent], dst, DVMRP_GRAFT, datalen); IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "sent graft for (%s %s) to %s on vif %d", RT_FMT(gt->gt_route, s1), inet_fmt(gt->gt_mcastgrp, s2), inet_fmt(gt->gt_route->rt_gateway, s3), gt->gt_route->rt_parent); } /* * Send an ack that a graft was received */ static void send_graft_ack(src, dst, origin, grp, vifi) u_int32 src; u_int32 dst; u_int32 origin; u_int32 grp; vifi_t vifi; { register char *p; register int i; int datalen; p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN; datalen = 0; for (i = 0; i < 4; i++) *p++ = ((char *)&(origin))[i]; for (i = 0; i < 4; i++) *p++ = ((char *)&(grp))[i]; datalen += 8; if (vifi == NO_VIF) send_igmp(src, dst, IGMP_DVMRP, DVMRP_GRAFT_ACK, htonl(MROUTED_LEVEL), datalen); else { #if 0 if (uvifs[vifi].uv_flags & VIFF_TUNNEL) dst = dvmrp_group; /* XXX */ #endif send_on_vif(&uvifs[vifi], dst, DVMRP_GRAFT_ACK, datalen); } IF_DEBUG(DEBUG_PRUNE) if (vifi == NO_VIF) log(LOG_DEBUG, 0, "sent graft ack for (%s, %s) to %s", inet_fmt(origin, s1), inet_fmt(grp, s2), inet_fmt(dst, s3)); else log(LOG_DEBUG, 0, "sent graft ack for (%s, %s) to %s on vif %d", inet_fmt(origin, s1), inet_fmt(grp, s2), inet_fmt(dst, s3), vifi); } /* * Update the kernel cache with all the routes hanging off the group entry */ static void update_kernel(g) struct gtable *g; { struct stable *st; for (st = g->gt_srctbl; st; st = st->st_next) if (st->st_ctime != 0) k_add_rg(st->st_origin, g); } /**************************************************************************** Functions that are used externally ****************************************************************************/ #ifdef SNMP #include #include "snmp.h" /* * Find a specific group entry in the group table */ struct gtable * find_grp(grp) u_int32 grp; { struct gtable *gt; for (gt = kernel_table; gt; gt = gt->gt_gnext) { if (ntohl(grp) < ntohl(gt->gt_mcastgrp)) break; if (gt->gt_mcastgrp == grp) return gt; } return NULL; } /* * Given a group entry and source, find the corresponding source table * entry */ struct stable * find_grp_src(gt, src) struct gtable *gt; u_int32 src; { struct stable *st; u_long grp = gt->gt_mcastgrp; struct gtable *gtcurr; for (gtcurr = gt; gtcurr->gt_mcastgrp == grp; gtcurr = gtcurr->gt_gnext) { for (st = gtcurr->gt_srctbl; st; st = st->st_next) if (st->st_origin == src) return st; } return NULL; } /* * Find next entry > specification */ int next_grp_src_mask(gtpp, stpp, grp, src, mask) struct gtable **gtpp; /* ordered by group */ struct stable **stpp; /* ordered by source */ u_int32 grp; u_int32 src; u_int32 mask; { struct gtable *gt, *gbest = NULL; struct stable *st, *sbest = NULL; /* Find first group entry >= grp spec */ (*gtpp) = kernel_table; while ((*gtpp) && ntohl((*gtpp)->gt_mcastgrp) < ntohl(grp)) (*gtpp)=(*gtpp)->gt_gnext; if (!(*gtpp)) return 0; /* no more groups */ for (gt = kernel_table; gt; gt=gt->gt_gnext) { /* Since grps are ordered, we can stop when group changes from gbest */ if (gbest && gbest->gt_mcastgrp != gt->gt_mcastgrp) break; for (st = gt->gt_srctbl; st; st=st->st_next) { /* Among those entries > spec, find "lowest" one */ if (((ntohl(gt->gt_mcastgrp)> ntohl(grp)) || (ntohl(gt->gt_mcastgrp)==ntohl(grp) && ntohl(st->st_origin)> ntohl(src)) || (ntohl(gt->gt_mcastgrp)==ntohl(grp) && ntohl(st->st_origin)==src && 0xFFFFFFFF>ntohl(mask))) && (!gbest || (ntohl(gt->gt_mcastgrp)< ntohl(gbest->gt_mcastgrp)) || (ntohl(gt->gt_mcastgrp)==ntohl(gbest->gt_mcastgrp) && ntohl(st->st_origin)< ntohl(sbest->st_origin)))) { gbest = gt; sbest = st; } } } (*gtpp) = gbest; (*stpp) = sbest; return (*gtpp)!=0; } /* * Ensure that sg contains current information for the given group,source. * This is fetched from the kernel as a unit so that counts for the entry * are consistent, i.e. packet and byte counts for the same entry are * read at the same time. */ void refresh_sg(sg, gt, st) struct sioc_sg_req *sg; struct gtable *gt; struct stable *st; { static int lastq = -1; if (quantum != lastq || sg->src.s_addr!=st->st_origin || sg->grp.s_addr!=gt->gt_mcastgrp) { lastq = quantum; sg->src.s_addr = st->st_origin; sg->grp.s_addr = gt->gt_mcastgrp; ioctl(udp_socket, SIOCGETSGCNT, (char *)sg); } } /* * Given a routing table entry, and a vifi, find the next entry * equal to or greater than those */ int next_child(gtpp, stpp, grp, src, mask, vifi) struct gtable **gtpp; struct stable **stpp; u_int32 grp; u_int32 src; u_int32 mask; vifi_t *vifi; /* vif at which to start looking */ { /* Get (G,S,M) entry */ if (mask!=0xFFFFFFFF || !((*gtpp) = find_grp(grp)) || !((*stpp) = find_grp_src((*gtpp),src))) if (!next_grp_src_mask(gtpp, stpp, grp, src, mask)) return 0; /* Continue until we get one with a valid next vif */ do { for (; (*gtpp)->gt_route->rt_children && *vifigt_route->rt_children)) return 1; *vifi = 0; } while (next_grp_src_mask(gtpp, stpp, (*gtpp)->gt_mcastgrp, (*stpp)->st_origin, 0xFFFFFFFF) ); return 0; } #endif /* SNMP */ /* * Initialize the kernel table structure */ void init_ktable() { kernel_table = NULL; kernel_no_route = NULL; kroutes = 0; } /* * Add a new table entry for (origin, mcastgrp) */ void add_table_entry(origin, mcastgrp) u_int32 origin; u_int32 mcastgrp; { struct rtentry *r; struct gtable *gt,**gtnp,*prev_gt; struct stable *st,**stnp; /* * Since we have to enable mrouting to get the version number, * some cache creation requests can sneak through. Ignore them * since we're not going to do useful stuff until we've performed * final initialization. */ if (!did_final_init) return; #ifdef DEBUG_MFC md_log(MD_MISS, origin, mcastgrp); #endif r = determine_route(origin); prev_gt = NULL; if (r == NULL) { /* * Look for it on the no_route table; if it is found then * it will be detected as a duplicate below. */ for (gt = kernel_no_route; gt; gt = gt->gt_next) if (mcastgrp == gt->gt_mcastgrp && gt->gt_srctbl && gt->gt_srctbl->st_origin == origin) break; gtnp = &kernel_no_route; } else { gtnp = &r->rt_groups; while ((gt = *gtnp) != NULL) { if (gt->gt_mcastgrp >= mcastgrp) break; gtnp = >->gt_next; prev_gt = gt; } } if (gt == NULL || gt->gt_mcastgrp != mcastgrp) { gt = (struct gtable *)malloc(sizeof(struct gtable)); if (gt == NULL) log(LOG_ERR, 0, "ran out of memory"); gt->gt_mcastgrp = mcastgrp; gt->gt_timer = CACHE_LIFETIME(cache_lifetime); time(>->gt_ctime); gt->gt_prsent_timer = 0; gt->gt_grftsnt = 0; gt->gt_srctbl = NULL; gt->gt_pruntbl = NULL; gt->gt_route = r; gt->gt_rexmit_timer = 0; NBRM_CLRALL(gt->gt_prunes); gt->gt_prune_rexmit = PRUNE_REXMIT_VAL; #ifdef RSRR gt->gt_rsrr_cache = NULL; #endif /* Calculate forwarding vifs */ determine_forwvifs(gt); /* update ttls */ prun_add_ttls(gt); gt->gt_next = *gtnp; *gtnp = gt; if (gt->gt_next) gt->gt_next->gt_prev = gt; gt->gt_prev = prev_gt; if (r) { if (find_src_grp(r->rt_origin, r->rt_originmask, gt->gt_mcastgrp)) { struct gtable *g; g = gtp ? gtp->gt_gnext : kernel_table; log(LOG_WARNING, 0, "Entry for (%s %s) (rt:%x) exists (rt:%x)", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2), r, g->gt_route); } else { if (gtp) { gt->gt_gnext = gtp->gt_gnext; gt->gt_gprev = gtp; gtp->gt_gnext = gt; } else { gt->gt_gnext = kernel_table; gt->gt_gprev = NULL; kernel_table = gt; } if (gt->gt_gnext) gt->gt_gnext->gt_gprev = gt; } } else { gt->gt_gnext = gt->gt_gprev = NULL; } } stnp = >->gt_srctbl; while ((st = *stnp) != NULL) { if (ntohl(st->st_origin) >= ntohl(origin)) break; stnp = &st->st_next; } if (st == NULL || st->st_origin != origin) { st = (struct stable *)malloc(sizeof(struct stable)); if (st == NULL) log(LOG_ERR, 0, "ran out of memory"); st->st_origin = origin; st->st_pktcnt = 0; st->st_savpkt = 0; time(&st->st_ctime); st->st_next = *stnp; *stnp = st; } else { if (st->st_ctime == 0) { /* An old source which we're keeping around for statistics */ time(&st->st_ctime); } else { #ifdef DEBUG_MFC md_log(MD_DUPE, origin, mcastgrp); #endif /* Ignore kernel->mrouted retransmissions */ if (time(0) - st->st_ctime > 5) log(LOG_WARNING, 0, "kernel entry already exists for (%s %s)", inet_fmt(origin, s1), inet_fmt(mcastgrp, s2)); k_add_rg(origin, gt); return; } } kroutes++; k_add_rg(origin, gt); IF_DEBUG(DEBUG_CACHE) log(LOG_DEBUG, 0, "add cache entry (%s %s) gm:%x, parent-vif:%d", inet_fmt(origin, s1), inet_fmt(mcastgrp, s2), gt->gt_grpmems, r ? r->rt_parent : -1); /* * If there are no downstream routers that want traffic for * this group, send (or retransmit) a prune upstream. */ if (VIFM_ISEMPTY(gt->gt_grpmems)) send_prune(gt); } /* * A router has gone down. Remove prune state pertinent to that router. */ void reset_neighbor_state(vifi, addr) vifi_t vifi; u_int32 addr; { struct rtentry *r; struct gtable *g; struct ptable *pt, **ptnp; struct stable *st; for (g = kernel_table; g; g = g->gt_gnext) { r = g->gt_route; /* * If neighbor was the parent, remove the prune sent state * and all of the source cache info so that prunes get * regenerated. */ if (vifi == r->rt_parent) { if (addr == r->rt_gateway) { IF_DEBUG(DEBUG_PEER) log(LOG_DEBUG, 0, "reset_neighbor_state parent reset (%s %s)", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2)); g->gt_prsent_timer = 0; g->gt_grftsnt = 0; while ((st = g->gt_srctbl) != NULL) { g->gt_srctbl = st->st_next; if (st->st_ctime != 0) { k_del_rg(st->st_origin, g); kroutes--; } free(st); } } } else { /* * Remove any prunes that this router has sent us. */ ptnp = &g->gt_pruntbl; while ((pt = *ptnp) != NULL) { if (pt->pt_vifi == vifi && pt->pt_router == addr) { NBRM_CLR(pt->pt_index, g->gt_prunes); *ptnp = pt->pt_next; free(pt); } else ptnp = &pt->pt_next; } /* * And see if we want to forward again. */ if (!VIFM_ISSET(vifi, g->gt_grpmems)) { GET_MEMBERSHIP(g, vifi); APPLY_SCOPE(g); prun_add_ttls(g); /* Update kernel state */ update_kernel(g); #ifdef RSRR /* Send route change notification to reservation protocol. */ rsrr_cache_send(g,1); #endif /* RSRR */ /* * If removing this prune causes us to start forwarding * (e.g. the neighbor rebooted), and we sent a prune upstream, * send a graft to cancel the prune. */ if (!VIFM_ISEMPTY(g->gt_grpmems) && g->gt_prsent_timer) send_graft(g); IF_DEBUG(DEBUG_PEER) log(LOG_DEBUG, 0, "reset neighbor state (%s %s) gm:%x", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2), g->gt_grpmems); } } } } /* * Delete table entry from the kernel * del_flag determines how many entries to delete */ void del_table_entry(r, mcastgrp, del_flag) struct rtentry *r; u_int32 mcastgrp; u_int del_flag; { struct gtable *g, *prev_g; struct stable *st, *prev_st; struct ptable *pt, *prev_pt; if (del_flag == DEL_ALL_ROUTES) { g = r->rt_groups; while (g) { IF_DEBUG(DEBUG_CACHE) log(LOG_DEBUG, 0, "del_table_entry deleting (%s %s)", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2)); st = g->gt_srctbl; while (st) { if (st->st_ctime != 0) { if (k_del_rg(st->st_origin, g) < 0) { log(LOG_WARNING, errno, "del_table_entry trying to delete (%s, %s)", inet_fmt(st->st_origin, s1), inet_fmt(g->gt_mcastgrp, s2)); } kroutes--; } prev_st = st; st = st->st_next; free(prev_st); } g->gt_srctbl = NULL; pt = g->gt_pruntbl; while (pt) { prev_pt = pt; pt = pt->pt_next; free(prev_pt); } g->gt_pruntbl = NULL; if (g->gt_gnext) g->gt_gnext->gt_gprev = g->gt_gprev; if (g->gt_gprev) g->gt_gprev->gt_gnext = g->gt_gnext; else kernel_table = g->gt_gnext; #ifdef RSRR /* Send route change notification to reservation protocol. */ rsrr_cache_send(g,0); rsrr_cache_clean(g); #endif /* RSRR */ if (g->gt_rexmit_timer) timer_clearTimer(g->gt_rexmit_timer); prev_g = g; g = g->gt_next; free(prev_g); } r->rt_groups = NULL; } /* * Dummy routine - someday this may be needed, so it is just there */ if (del_flag == DEL_RTE_GROUP) { prev_g = (struct gtable *)&r->rt_groups; for (g = r->rt_groups; g; g = g->gt_next) { if (g->gt_mcastgrp == mcastgrp) { IF_DEBUG(DEBUG_CACHE) log(LOG_DEBUG, 0, "del_table_entry deleting (%s %s)", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2)); st = g->gt_srctbl; while (st) { if (st->st_ctime != 0) { if (k_del_rg(st->st_origin, g) < 0) { log(LOG_WARNING, errno, "del_table_entry trying to delete (%s, %s)", inet_fmt(st->st_origin, s1), inet_fmt(g->gt_mcastgrp, s2)); } kroutes--; } prev_st = st; st = st->st_next; free(prev_st); } g->gt_srctbl = NULL; pt = g->gt_pruntbl; while (pt) { prev_pt = pt; pt = pt->pt_next; free(prev_pt); } g->gt_pruntbl = NULL; if (g->gt_gnext) g->gt_gnext->gt_gprev = g->gt_gprev; if (g->gt_gprev) g->gt_gprev->gt_gnext = g->gt_gnext; else kernel_table = g->gt_gnext; if (prev_g != (struct gtable *)&r->rt_groups) g->gt_next->gt_prev = prev_g; else g->gt_next->gt_prev = NULL; prev_g->gt_next = g->gt_next; if (g->gt_rexmit_timer) timer_clearTimer(g->gt_rexmit_timer); #ifdef RSRR /* Send route change notification to reservation protocol. */ rsrr_cache_send(g,0); rsrr_cache_clean(g); #endif /* RSRR */ free(g); g = prev_g; } else { prev_g = g; } } } } /* * update kernel table entry when a route entry changes */ void update_table_entry(r, old_parent_gw) struct rtentry *r; u_int32 old_parent_gw; { struct gtable *g; struct ptable *pt, **ptnp; for (g = r->rt_groups; g; g = g->gt_next) { ptnp = &g->gt_pruntbl; /* * Delete prune entries from non-children, or non-subordinates. */ while ((pt = *ptnp)) { if (!VIFM_ISSET(pt->pt_vifi, r->rt_children) || !NBRM_ISSET(pt->pt_index, r->rt_subordinates)) { IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "update_table_entry deleting prune for (%s %s) from %s on vif %d -%s%s", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2), inet_fmt(pt->pt_router, s3), pt->pt_vifi, VIFM_ISSET(pt->pt_vifi, r->rt_children) ? "" : " not a child", NBRM_ISSET(pt->pt_index, r->rt_subordinates) ? "" : " not a subordinate"); if (!NBRM_ISSET(pt->pt_index, g->gt_prunes)) { log(LOG_WARNING, 0, "gt_prunes lost track of (%s %s) from %s on vif %d", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2), inet_fmt(pt->pt_router, s3), pt->pt_vifi); } NBRM_CLR(pt->pt_index, g->gt_prunes); *ptnp = pt->pt_next; free(pt); continue; } ptnp = &((*ptnp)->pt_next); } IF_DEBUG(DEBUG_CACHE) log(LOG_DEBUG, 0, "updating cache entries (%s %s) old gm:%x", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2), g->gt_grpmems); /* * Forget about a prune or graft that we sent previously if we * have a new parent router (since the new parent router will * know nothing about what I sent to the previous parent). The * old parent will forget any prune state it is keeping for us. */ if (old_parent_gw != r->rt_gateway) { g->gt_prsent_timer = 0; g->gt_grftsnt = 0; } /* Recalculate membership */ determine_forwvifs(g); /* send a prune or graft if needed. */ send_prune_or_graft(g); IF_DEBUG(DEBUG_CACHE) log(LOG_DEBUG, 0, "updating cache entries (%s %s) new gm:%x", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2), g->gt_grpmems); /* update ttls and add entry into kernel */ prun_add_ttls(g); update_kernel(g); #ifdef RSRR /* Send route change notification to reservation protocol. */ rsrr_cache_send(g,1); #endif /* RSRR */ } } /* * set the forwarding flag for all mcastgrps on this vifi */ void update_lclgrp(vifi, mcastgrp) vifi_t vifi; u_int32 mcastgrp; { struct rtentry *r; struct gtable *g; IF_DEBUG(DEBUG_MEMBER) log(LOG_DEBUG, 0, "group %s joined on vif %d", inet_fmt(mcastgrp, s1), vifi); for (g = kernel_table; g; g = g->gt_gnext) { if (ntohl(mcastgrp) < ntohl(g->gt_mcastgrp)) break; r = g->gt_route; if (g->gt_mcastgrp == mcastgrp && VIFM_ISSET(vifi, r->rt_children)) { VIFM_SET(vifi, g->gt_grpmems); APPLY_SCOPE(g); if (VIFM_ISEMPTY(g->gt_grpmems)) continue; prun_add_ttls(g); IF_DEBUG(DEBUG_CACHE) log(LOG_DEBUG, 0, "update lclgrp (%s %s) gm:%x", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2), g->gt_grpmems); update_kernel(g); #ifdef RSRR /* Send route change notification to reservation protocol. */ rsrr_cache_send(g,1); #endif /* RSRR */ } } } /* * reset forwarding flag for all mcastgrps on this vifi */ void delete_lclgrp(vifi, mcastgrp) vifi_t vifi; u_int32 mcastgrp; { struct gtable *g; IF_DEBUG(DEBUG_MEMBER) log(LOG_DEBUG, 0, "group %s left on vif %d", inet_fmt(mcastgrp, s1), vifi); for (g = kernel_table; g; g = g->gt_gnext) { if (ntohl(mcastgrp) < ntohl(g->gt_mcastgrp)) break; if (g->gt_mcastgrp == mcastgrp && VIFM_ISSET(vifi, g->gt_grpmems)) { if (g->gt_route == NULL || SUBS_ARE_PRUNED(g->gt_route->rt_subordinates, uvifs[vifi].uv_nbrmap, g->gt_prunes)) { VIFM_CLR(vifi, g->gt_grpmems); IF_DEBUG(DEBUG_CACHE) log(LOG_DEBUG, 0, "delete lclgrp (%s %s) gm:%x", RT_FMT(g->gt_route, s1), inet_fmt(g->gt_mcastgrp, s2), g->gt_grpmems); prun_add_ttls(g); update_kernel(g); #ifdef RSRR /* Send route change notification to reservation protocol. */ rsrr_cache_send(g,1); #endif /* RSRR */ /* * If there are no more members of this particular group, * send prune upstream */ if (VIFM_ISEMPTY(g->gt_grpmems) && g->gt_route->rt_gateway) send_prune(g); } } } } /* * Takes the prune message received and then strips it to * determine the (src, grp) pair to be pruned. * * Adds the router to the (src, grp) entry then. * * Determines if further packets have to be sent down that vif * * Determines if a corresponding prune message has to be generated */ void accept_prune(src, dst, p, datalen) u_int32 src; u_int32 dst; char *p; int datalen; { u_int32 prun_src; u_int32 prun_grp; u_int32 prun_tmr; vifi_t vifi; int i; struct rtentry *r; struct gtable *g; struct ptable *pt; if ((vifi = find_vif(src, dst)) == NO_VIF) { log(LOG_INFO, 0, "ignoring prune report from non-neighbor %s", inet_fmt(src, s1)); return; } /* Check if enough data is present */ if (datalen < 12) { log(LOG_WARNING, 0, "non-decipherable prune from %s", inet_fmt(src, s1)); return; } for (i = 0; i< 4; i++) ((char *)&prun_src)[i] = *p++; for (i = 0; i< 4; i++) ((char *)&prun_grp)[i] = *p++; for (i = 0; i< 4; i++) ((char *)&prun_tmr)[i] = *p++; prun_tmr = ntohl(prun_tmr); if (prun_tmr <= MIN_PRUNE_LIFE) { IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "ignoring prune from %s on vif %d for (%s %s)/%d because its lifetime is too short", inet_fmt(src, s1), vifi, inet_fmt(prun_src, s2), inet_fmt(prun_grp, s3), prun_tmr); return; } IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "%s on vif %d prunes (%s %s)/%d", inet_fmt(src, s1), vifi, inet_fmt(prun_src, s2), inet_fmt(prun_grp, s3), prun_tmr); /* * Find the subnet for the prune */ if (find_src_grp(prun_src, 0, prun_grp)) { g = gtp ? gtp->gt_gnext : kernel_table; r = g->gt_route; IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "found grp state, (%s %s), metric is %d, children are %x, subords are %08x%08x", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2), r->rt_metric, r->rt_children, r->rt_subordinates.hi, r->rt_subordinates.lo); if (!VIFM_ISSET(vifi, r->rt_children)) { IF_DEBUG(DEBUG_PRUNE) log(LOG_WARNING, 0, "prune received from non-child %s for (%s %s) (dominant on vif %d is %s)", inet_fmt(src, s1), inet_fmt(prun_src, s2), inet_fmt(prun_grp, s3), vifi, inet_fmt(r->rt_dominants[vifi], s4)); #ifdef RINGBUFFER printringbuf(); #endif return; } if (VIFM_ISSET(vifi, g->gt_scope)) { log(LOG_WARNING, 0, "prune received from %s on scoped grp (%s %s)", inet_fmt(src, s1), inet_fmt(prun_src, s2), inet_fmt(prun_grp, s3)); return; } if ((pt = find_prune_entry(src, g->gt_pruntbl)) != NULL) { IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "%s %d from %s for (%s %s)/%d %s %d %s %x", "duplicate prune received on vif", vifi, inet_fmt(src, s1), inet_fmt(prun_src, s2), inet_fmt(prun_grp, s3), prun_tmr, "old timer:", pt->pt_timer, "cur gm:", g->gt_grpmems); pt->pt_timer = prun_tmr; } else { struct listaddr *n = neighbor_info(vifi, src); if (!n) { log(LOG_WARNING, 0, "Prune from non-neighbor %s on vif %d!?", inet_fmt(src, s1), vifi); return; } /* allocate space for the prune structure */ pt = (struct ptable *)(malloc(sizeof(struct ptable))); if (pt == NULL) log(LOG_ERR, 0, "pt: ran out of memory"); pt->pt_vifi = vifi; pt->pt_router = src; pt->pt_timer = prun_tmr; pt->pt_next = g->gt_pruntbl; g->gt_pruntbl = pt; if (n) { pt->pt_index = n->al_index; NBRM_SET(n->al_index, g->gt_prunes); } } /* * check if any more packets need to be sent on the * vif which sent this message */ if (SUBS_ARE_PRUNED(r->rt_subordinates, uvifs[vifi].uv_nbrmap, g->gt_prunes) && !grplst_mem(vifi, prun_grp)) { nbrbitmap_t tmp; VIFM_CLR(vifi, g->gt_grpmems); IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "vifnbrs=0x%08x%08x, subord=0x%08x%08x prunes=0x%08x%08x", uvifs[vifi].uv_nbrmap.hi,uvifs[vifi].uv_nbrmap.lo, r->rt_subordinates.hi, r->rt_subordinates.lo, g->gt_prunes.hi, g->gt_prunes.lo); /* XXX debugging */ NBRM_COPY(r->rt_subordinates, tmp); NBRM_MASK(tmp, uvifs[vifi].uv_nbrmap); if (!NBRM_ISSETALLMASK(g->gt_prunes, tmp)) log(LOG_WARNING, 0, "subordinate error"); /* XXX end debugging */ IF_DEBUG(DEBUG_PRUNE|DEBUG_CACHE) log(LOG_DEBUG, 0, "prune (%s %s), stop sending on vif %d, gm:%x", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2), vifi, g->gt_grpmems); prun_add_ttls(g); update_kernel(g); #ifdef RSRR /* Send route change notification to reservation protocol. */ rsrr_cache_send(g,1); #endif /* RSRR */ } /* * check if all the child routers have expressed no interest * in this group and if this group does not exist in the * interface * Send a prune message then upstream */ if (VIFM_ISEMPTY(g->gt_grpmems) && r->rt_gateway) { send_prune(g); } } else { /* * There is no kernel entry for this group. Therefore, we can * simply ignore the prune, as we are not forwarding this traffic * downstream. */ IF_DEBUG(DEBUG_PRUNE|DEBUG_CACHE) log(LOG_DEBUG, 0, "%s (%s %s)/%d from %s", "prune message received with no kernel entry for", inet_fmt(prun_src, s1), inet_fmt(prun_grp, s2), prun_tmr, inet_fmt(src, s3)); return; } } /* * Checks if this mcastgrp is present in the kernel table * If so and if a prune was sent, it sends a graft upwards */ void chkgrp_graft(vifi, mcastgrp) vifi_t vifi; u_int32 mcastgrp; { struct rtentry *r; struct gtable *g; for (g = kernel_table; g; g = g->gt_gnext) { if (ntohl(mcastgrp) < ntohl(g->gt_mcastgrp)) break; r = g->gt_route; if (g->gt_mcastgrp == mcastgrp && VIFM_ISSET(vifi, r->rt_children)) if (g->gt_prsent_timer) { VIFM_SET(vifi, g->gt_grpmems); /* * If the vif that was joined was a scoped vif, * ignore it ; don't graft back */ APPLY_SCOPE(g); if (VIFM_ISEMPTY(g->gt_grpmems)) continue; /* send graft upwards */ send_graft(g); /* update cache timer*/ g->gt_timer = CACHE_LIFETIME(cache_lifetime); IF_DEBUG(DEBUG_PRUNE|DEBUG_CACHE) log(LOG_DEBUG, 0, "chkgrp graft (%s %s) gm:%x", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2), g->gt_grpmems); prun_add_ttls(g); update_kernel(g); #ifdef RSRR /* Send route change notification to reservation protocol. */ rsrr_cache_send(g,1); #endif /* RSRR */ } } } /* determine the multicast group and src * * if it does, then determine if a prune was sent * upstream. * if prune sent upstream, send graft upstream and send * ack downstream. * * if no prune sent upstream, change the forwarding bit * for this interface and send ack downstream. * * if no entry exists for this group send ack downstream. */ void accept_graft(src, dst, p, datalen) u_int32 src; u_int32 dst; char *p; int datalen; { vifi_t vifi; u_int32 graft_src; u_int32 graft_grp; int i; struct rtentry *r; struct gtable *g; struct ptable *pt, **ptnp; if (datalen < 8) { log(LOG_WARNING, 0, "received non-decipherable graft from %s", inet_fmt(src, s1)); return; } for (i = 0; i< 4; i++) ((char *)&graft_src)[i] = *p++; for (i = 0; i< 4; i++) ((char *)&graft_grp)[i] = *p++; vifi = find_vif(src, dst); send_graft_ack(dst, src, graft_src, graft_grp, vifi); if (vifi == NO_VIF) { log(LOG_INFO, 0, "ignoring graft for (%s %s) from non-neighbor %s", inet_fmt(graft_src, s2), inet_fmt(graft_grp, s3), inet_fmt(src, s1)); return; } IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "%s on vif %d grafts (%s %s)", inet_fmt(src, s1), vifi, inet_fmt(graft_src, s2), inet_fmt(graft_grp, s3)); /* * Find the subnet for the graft */ if (find_src_grp(graft_src, 0, graft_grp)) { g = gtp ? gtp->gt_gnext : kernel_table; r = g->gt_route; if (VIFM_ISSET(vifi, g->gt_scope)) { log(LOG_WARNING, 0, "graft received from %s on scoped grp (%s %s)", inet_fmt(src, s1), inet_fmt(graft_src, s2), inet_fmt(graft_grp, s3)); return; } ptnp = &g->gt_pruntbl; while ((pt = *ptnp) != NULL) { if ((pt->pt_vifi == vifi) && (pt->pt_router == src)) { NBRM_CLR(pt->pt_index, g->gt_prunes); *ptnp = pt->pt_next; free(pt); VIFM_SET(vifi, g->gt_grpmems); IF_DEBUG(DEBUG_PRUNE|DEBUG_CACHE) log(LOG_DEBUG, 0, "accept graft (%s %s) gm:%x", RT_FMT(r, s1), inet_fmt(g->gt_mcastgrp, s2), g->gt_grpmems); prun_add_ttls(g); update_kernel(g); #ifdef RSRR /* Send route change notification to reservation protocol. */ rsrr_cache_send(g,1); #endif /* RSRR */ break; } else { ptnp = &pt->pt_next; } } g->gt_timer = CACHE_LIFETIME(cache_lifetime); if (g->gt_prsent_timer) /* send graft upwards */ send_graft(g); } else { /* * We have no state for the source and group in question. * This is fine, since we know that we have no prune state, and * grafts are requests to remove prune state. */ IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "%s (%s %s) from %s", "graft received with no kernel entry for", inet_fmt(graft_src, s1), inet_fmt(graft_grp, s2), inet_fmt(src, s3)); return; } } /* * find out which group is involved first of all * then determine if a graft was sent. * if no graft sent, ignore the message * if graft was sent and the ack is from the right * source, remove the graft timer so that we don't * have send a graft again */ void accept_g_ack(src, dst, p, datalen) u_int32 src; u_int32 dst; char *p; int datalen; { struct gtable *g; vifi_t vifi; u_int32 grft_src; u_int32 grft_grp; int i; if ((vifi = find_vif(src, dst)) == NO_VIF) { log(LOG_INFO, 0, "ignoring graft ack from non-neighbor %s", inet_fmt(src, s1)); return; } if (datalen < 0 || datalen > 8) { log(LOG_WARNING, 0, "received non-decipherable graft ack from %s", inet_fmt(src, s1)); return; } for (i = 0; i< 4; i++) ((char *)&grft_src)[i] = *p++; for (i = 0; i< 4; i++) ((char *)&grft_grp)[i] = *p++; IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "%s on vif %d acks graft (%s, %s)", inet_fmt(src, s1), vifi, inet_fmt(grft_src, s2), inet_fmt(grft_grp, s3)); /* * Find the subnet for the graft ack */ if (find_src_grp(grft_src, 0, grft_grp)) { g = gtp ? gtp->gt_gnext : kernel_table; g->gt_grftsnt = 0; } else { log(LOG_WARNING, 0, "%s (%s, %s) from %s", "rcvd graft ack with no kernel entry for", inet_fmt(grft_src, s1), inet_fmt(grft_grp, s2), inet_fmt(src, s3)); #ifdef RINGBUFFER printringbuf(); #endif return; } } /* * free all prune entries and kernel routes * normally, this should inform the kernel that all of its routes * are going away, but this is only called by restart(), which is * about to call MRT_DONE which does that anyway. */ void free_all_prunes() { register struct rtentry *r; register struct gtable *g, *prev_g; register struct stable *s, *prev_s; register struct ptable *p, *prev_p; for (r = routing_table; r; r = r->rt_next) { g = r->rt_groups; while (g) { s = g->gt_srctbl; while (s) { prev_s = s; s = s->st_next; free(prev_s); } p = g->gt_pruntbl; while (p) { prev_p = p; p = p->pt_next; free(prev_p); } prev_g = g; g = g->gt_next; if (prev_g->gt_rexmit_timer) timer_clearTimer(prev_g->gt_rexmit_timer); free(prev_g); } r->rt_groups = NULL; } kernel_table = NULL; g = kernel_no_route; while (g) { if (g->gt_srctbl) free(g->gt_srctbl); prev_g = g; g = g->gt_next; if (prev_g->gt_rexmit_timer) timer_clearTimer(prev_g->gt_rexmit_timer); free(prev_g); } kernel_no_route = NULL; } /* * When a new route is created, search * a) The less-specific part of the routing table * b) The route-less kernel table * for sources that the new route might want to handle. * * "Inheriting" these sources might be cleanest, but simply deleting * them is easier, and letting the kernel re-request them. */ void steal_sources(rt) struct rtentry *rt; { register struct rtentry *rp; register struct gtable *gt, **gtnp; register struct stable *st, **stnp; for (rp = rt->rt_next; rp; rp = rp->rt_next) { if (rp->rt_groups == NULL) continue; if ((rt->rt_origin & rp->rt_originmask) == rp->rt_origin) { IF_DEBUG(DEBUG_ROUTE) log(LOG_DEBUG, 0, "Route for %s stealing sources from %s", RT_FMT(rt, s1), RT_FMT(rp, s2)); for (gt = rp->rt_groups; gt; gt = gt->gt_next) { stnp = >->gt_srctbl; while ((st = *stnp) != NULL) { if ((st->st_origin & rt->rt_originmask) == rt->rt_origin) { IF_DEBUG(DEBUG_ROUTE) log(LOG_DEBUG, 0, "%s stealing (%s %s) from %s", RT_FMT(rt, s1), inet_fmt(st->st_origin, s3), inet_fmt(gt->gt_mcastgrp, s4), RT_FMT(rp, s2)); if (st->st_ctime != 0) { if (k_del_rg(st->st_origin, gt) < 0) { log(LOG_WARNING, errno, "%s (%s, %s)", "steal_sources trying to delete", inet_fmt(st->st_origin, s1), inet_fmt(gt->gt_mcastgrp, s2)); } kroutes--; } *stnp = st->st_next; free(st); } else { stnp = &st->st_next; } } } } } gtnp = &kernel_no_route; while ((gt = *gtnp) != NULL) { if (gt->gt_srctbl && ((gt->gt_srctbl->st_origin & rt->rt_originmask) == rt->rt_origin)) { IF_DEBUG(DEBUG_ROUTE) log(LOG_DEBUG, 0, "%s stealing (%s %s) from %s", RT_FMT(rt, s1), inet_fmt(gt->gt_srctbl->st_origin, s3), inet_fmt(gt->gt_mcastgrp, s4), "no_route table"); if (gt->gt_srctbl->st_ctime != 0) { if (k_del_rg(gt->gt_srctbl->st_origin, gt) < 0) { log(LOG_WARNING, errno, "%s (%s %s)", "steal_sources trying to delete", inet_fmt(gt->gt_srctbl->st_origin, s1), inet_fmt(gt->gt_mcastgrp, s2)); } kroutes--; } free(gt->gt_srctbl); *gtnp = gt->gt_next; if (gt->gt_next) gt->gt_next->gt_prev = gt->gt_prev; if (gt->gt_rexmit_timer) timer_clearTimer(gt->gt_rexmit_timer); free(gt); } else { gtnp = >->gt_next; } } } /* * Advance the timers on all the cache entries. * If there are any entries whose timers have expired, * remove these entries from the kernel cache. */ void age_table_entry() { struct rtentry *r; struct gtable *gt, **gtnptr; struct stable *st, **stnp; struct ptable *pt, **ptnp; struct sioc_sg_req sg_req; IF_DEBUG(DEBUG_PRUNE|DEBUG_CACHE) log(LOG_DEBUG, 0, "aging forwarding cache entries"); gtnptr = &kernel_table; while ((gt = *gtnptr) != NULL) { vifi_t i; /* XXX Debugging */ int fixit = 0; /* XXX Debugging */ r = gt->gt_route; /* XXX Debugging... */ for (i = 0; i < numvifs; i++) { /* * If we're not sending on this vif, * And this group isn't scoped on this vif, * And I'm the parent for this route on this vif, * And there are subordinates on this vif, * And all of the subordinates haven't pruned, * YELL LOUDLY * and remember to fix it up later */ if (!VIFM_ISSET(i, gt->gt_grpmems) && !VIFM_ISSET(i, gt->gt_scope) && VIFM_ISSET(i, r->rt_children) && NBRM_ISSETMASK(uvifs[i].uv_nbrmap, r->rt_subordinates) && !SUBS_ARE_PRUNED(r->rt_subordinates, uvifs[i].uv_nbrmap, gt->gt_prunes)) { log(LOG_WARNING, 0, "(%s %s) is blackholing on vif %d", RT_FMT(r, s1), inet_fmt(gt->gt_mcastgrp, s2), i); fixit = 1; } } if (fixit) { log(LOG_WARNING, 0, "fixing membership for (%s %s) gm:%x", RT_FMT(r, s1), inet_fmt(gt->gt_mcastgrp, s2), gt->gt_grpmems); determine_forwvifs(gt); send_prune_or_graft(gt); log(LOG_WARNING, 0, "fixed membership for (%s %s) gm:%x", RT_FMT(r, s1), inet_fmt(gt->gt_mcastgrp, s2), gt->gt_grpmems); #ifdef RINGBUFFER printringbuf(); #endif } /*DEBUG2*/ /* If there are group members, * and there are recent sources, * and we have a route, * and it's not directly connected, * and we haven't sent a prune, * if there are any cache entries in the kernel * [if there aren't we're probably waiting to rexmit], * YELL LOUDLY * and send a prune */ if (VIFM_ISEMPTY(gt->gt_grpmems) && gt->gt_srctbl && r && r->rt_gateway && gt->gt_prsent_timer == 0) { for (st = gt->gt_srctbl; st; st = st->st_next) if (st->st_ctime != 0) break; if (st != NULL) { log(LOG_WARNING, 0, "grpmems for (%s %s) is empty but no prune state!", RT_FMT(r, s1), inet_fmt(gt->gt_mcastgrp, s2)); send_prune_or_graft(gt); #ifdef RINGBUFFER printringbuf(); #endif } } /* XXX ...Debugging */ /* advance the timer for the kernel entry */ gt->gt_timer -= TIMER_INTERVAL; /* decrement prune timer if need be */ if (gt->gt_prsent_timer > 0) { gt->gt_prsent_timer -= TIMER_INTERVAL; if (gt->gt_prsent_timer <= 0) { IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "upstream prune tmo (%s %s)", RT_FMT(r, s1), inet_fmt(gt->gt_mcastgrp, s2)); gt->gt_prsent_timer = -1; /* Reset the prune retransmission timer to its initial value */ gt->gt_prune_rexmit = PRUNE_REXMIT_VAL; } } /* retransmit graft with exponential backoff */ if (gt->gt_grftsnt) { register int y; y = ++gt->gt_grftsnt; while (y && !(y & 1)) y >>= 1; if (y == 1) send_graft(gt); } /* * Age prunes * * If a prune expires, forward again on that vif. */ ptnp = >->gt_pruntbl; while ((pt = *ptnp) != NULL) { if ((pt->pt_timer -= TIMER_INTERVAL) <= 0) { IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "expire prune (%s %s) from %s on vif %d", RT_FMT(r, s1), inet_fmt(gt->gt_mcastgrp, s2), inet_fmt(pt->pt_router, s3), pt->pt_vifi); if (gt->gt_prsent_timer > 0) { log(LOG_WARNING, 0, "prune (%s %s) from %s on vif %d expires with %d left on prsent timer", RT_FMT(r, s1), inet_fmt(gt->gt_mcastgrp, s2), inet_fmt(pt->pt_router, s3), pt->pt_vifi, gt->gt_prsent_timer); /* Send a graft to heal the tree. */ send_graft(gt); } NBRM_CLR(pt->pt_index, gt->gt_prunes); expire_prune(pt->pt_vifi, gt); /* remove the router's prune entry and await new one */ *ptnp = pt->pt_next; free(pt); } else { ptnp = &pt->pt_next; } } /* * If the cache entry has expired, delete source table entries for * silent sources. If there are no source entries left, and there * are no downstream prunes, then the entry is deleted. * Otherwise, the cache entry's timer is refreshed. */ if (gt->gt_timer <= 0) { IF_DEBUG(DEBUG_CACHE) log(LOG_DEBUG, 0, "(%s %s) timed out, checking for traffic", RT_FMT(gt->gt_route, s1), inet_fmt(gt->gt_mcastgrp, s2)); /* Check for traffic before deleting source entries */ sg_req.grp.s_addr = gt->gt_mcastgrp; stnp = >->gt_srctbl; while ((st = *stnp) != NULL) { /* * Source entries with no ctime are not actually in the * kernel; they have been removed by rexmit_prune() so * are safe to remove from the list at this point. */ if (st->st_ctime) { sg_req.src.s_addr = st->st_origin; if (ioctl(udp_socket, SIOCGETSGCNT, (char *)&sg_req) < 0) { log(LOG_WARNING, errno, "%s (%s %s)", "age_table_entry: SIOCGETSGCNT failing for", inet_fmt(st->st_origin, s1), inet_fmt(gt->gt_mcastgrp, s2)); /* Make sure it gets deleted below */ sg_req.pktcnt = st->st_pktcnt; } } else { sg_req.pktcnt = st->st_pktcnt; } if (sg_req.pktcnt == st->st_pktcnt) { *stnp = st->st_next; IF_DEBUG(DEBUG_CACHE) log(LOG_DEBUG, 0, "age_table_entry deleting (%s %s)", inet_fmt(st->st_origin, s1), inet_fmt(gt->gt_mcastgrp, s2)); if (st->st_ctime != 0) { if (k_del_rg(st->st_origin, gt) < 0) { log(LOG_WARNING, errno, "age_table_entry trying to delete (%s %s)", inet_fmt(st->st_origin, s1), inet_fmt(gt->gt_mcastgrp, s2)); } kroutes--; } free(st); } else { st->st_pktcnt = sg_req.pktcnt; stnp = &st->st_next; } } /* * Retain the group entry if we have downstream prunes or if * there is at least one source in the list that still has * traffic, or if our upstream prune timer or graft * retransmission timer is running. */ if (gt->gt_pruntbl != NULL || gt->gt_srctbl != NULL || gt->gt_prsent_timer > 0 || gt->gt_grftsnt > 0) { IF_DEBUG(DEBUG_CACHE) log(LOG_DEBUG, 0, "refresh lifetim of cache entry %s%s%s%s(%s, %s)", gt->gt_pruntbl ? "(dstrm prunes) " : "", gt->gt_srctbl ? "(trfc flow) " : "", gt->gt_prsent_timer > 0 ? "(upstrm prune) " : "", gt->gt_grftsnt > 0 ? "(grft rexmit) " : "", RT_FMT(r, s1), inet_fmt(gt->gt_mcastgrp, s2)); gt->gt_timer = CACHE_LIFETIME(cache_lifetime); if (gt->gt_prsent_timer == -1) { /* * The upstream prune timed out. Remove any kernel * state. */ gt->gt_prsent_timer = 0; if (gt->gt_pruntbl) { log(LOG_WARNING, 0, "upstream prune for (%s %s) expires with downstream prunes active", RT_FMT(r, s1), inet_fmt(gt->gt_mcastgrp, s2)); } remove_sources(gt); } gtnptr = >->gt_gnext; continue; } IF_DEBUG(DEBUG_CACHE) log(LOG_DEBUG, 0, "timeout cache entry (%s, %s)", RT_FMT(r, s1), inet_fmt(gt->gt_mcastgrp, s2)); if (gt->gt_prev) gt->gt_prev->gt_next = gt->gt_next; else gt->gt_route->rt_groups = gt->gt_next; if (gt->gt_next) gt->gt_next->gt_prev = gt->gt_prev; if (gt->gt_gprev) { gt->gt_gprev->gt_gnext = gt->gt_gnext; gtnptr = >->gt_gprev->gt_gnext; } else { kernel_table = gt->gt_gnext; gtnptr = &kernel_table; } if (gt->gt_gnext) gt->gt_gnext->gt_gprev = gt->gt_gprev; #ifdef RSRR /* Send route change notification to reservation protocol. */ rsrr_cache_send(gt,0); rsrr_cache_clean(gt); #endif /* RSRR */ if (gt->gt_rexmit_timer) timer_clearTimer(gt->gt_rexmit_timer); free((char *)gt); } else { if (gt->gt_prsent_timer == -1) { /* * The upstream prune timed out. Remove any kernel * state. */ gt->gt_prsent_timer = 0; if (gt->gt_pruntbl) { log(LOG_WARNING, 0, "upstream prune for (%s %s) expires with downstream prunes active", RT_FMT(r, s1), inet_fmt(gt->gt_mcastgrp, s2)); } remove_sources(gt); } gtnptr = >->gt_gnext; } } /* * When traversing the no_route table, the decision is much easier. * Just delete it if it has timed out. */ gtnptr = &kernel_no_route; while ((gt = *gtnptr) != NULL) { /* advance the timer for the kernel entry */ gt->gt_timer -= TIMER_INTERVAL; if (gt->gt_timer < 0) { if (gt->gt_srctbl) { if (gt->gt_srctbl->st_ctime != 0) { if (k_del_rg(gt->gt_srctbl->st_origin, gt) < 0) { log(LOG_WARNING, errno, "%s (%s %s)", "age_table_entry trying to delete no-route", inet_fmt(gt->gt_srctbl->st_origin, s1), inet_fmt(gt->gt_mcastgrp, s2)); } kroutes--; } free(gt->gt_srctbl); } *gtnptr = gt->gt_next; if (gt->gt_next) gt->gt_next->gt_prev = gt->gt_prev; if (gt->gt_rexmit_timer) timer_clearTimer(gt->gt_rexmit_timer); free((char *)gt); } else { gtnptr = >->gt_next; } } } /* * Modify the kernel to forward packets when one or multiple prunes that * were received on the vif given by vifi, for the group given by gt, * have expired. */ static void expire_prune(vifi, gt) vifi_t vifi; struct gtable *gt; { /* * No need to send a graft, any prunes that we sent * will expire before any prunes that we have received. * However, in the case that we did make a mistake, * send a graft to compensate. */ if (gt->gt_prsent_timer >= MIN_PRUNE_LIFE) { IF_DEBUG(DEBUG_PRUNE) log(LOG_DEBUG, 0, "prune expired with %d left on %s", gt->gt_prsent_timer, "prsent_timer"); gt->gt_prsent_timer = 0; send_graft(gt); } /* modify the kernel entry to forward packets */ if (!VIFM_ISSET(vifi, gt->gt_grpmems)) { struct rtentry *rt = gt->gt_route; VIFM_SET(vifi, gt->gt_grpmems); IF_DEBUG(DEBUG_CACHE) log(LOG_DEBUG, 0, "forw again (%s %s) gm:%x vif:%d", RT_FMT(rt, s1), inet_fmt(gt->gt_mcastgrp, s2), gt->gt_grpmems, vifi); prun_add_ttls(gt); update_kernel(gt); #ifdef RSRR /* Send route change notification to reservation protocol. */ rsrr_cache_send(gt,1); #endif /* RSRR */ } } /* * Print the contents of the cache table on file 'fp2'. */ void dump_cache(fp2) FILE *fp2; { register struct rtentry *r; register struct gtable *gt; register struct stable *st; register struct ptable *pt; register vifi_t i; char c; register time_t thyme = time(0); fprintf(fp2, "Multicast Routing Cache Table (%d entries)\n%s", kroutes, " Origin Mcast-group CTmr Age Ptmr Rx IVif Forwvifs\n"); fprintf(fp2, "<(prunesrc:vif[idx]/tmr) prunebitmap\n%s", ">Source Lifetime SavPkt Pkts Bytes RPFf\n"); for (gt = kernel_no_route; gt; gt = gt->gt_next) { if (gt->gt_srctbl) { fprintf(fp2, " %-18s %-15s %-8s %-8s - -1 (no route)\n", inet_fmts(gt->gt_srctbl->st_origin, 0xffffffff, s1), inet_fmt(gt->gt_mcastgrp, s2), scaletime(gt->gt_timer), scaletime(thyme - gt->gt_ctime)); fprintf(fp2, ">%s\n", inet_fmt(gt->gt_srctbl->st_origin, s1)); } } for (gt = kernel_table; gt; gt = gt->gt_gnext) { r = gt->gt_route; fprintf(fp2, " %-18s %-15s", RT_FMT(r, s1), inet_fmt(gt->gt_mcastgrp, s2)); fprintf(fp2, " %-8s", scaletime(gt->gt_timer)); fprintf(fp2, " %-8s %-8s ", scaletime(thyme - gt->gt_ctime), gt->gt_prsent_timer ? scaletime(gt->gt_prsent_timer) : " -"); if (gt->gt_prune_rexmit) { int i = gt->gt_prune_rexmit; int n = 0; while (i > PRUNE_REXMIT_VAL) { n++; i /= 2; } if (n == 0 && gt->gt_prsent_timer == 0) fprintf(fp2, " -"); else fprintf(fp2, "%2d", n); } else { fprintf(fp2, " -"); } fprintf(fp2, " %2u%c%c", r->rt_parent, gt->gt_prsent_timer ? 'P' : gt->gt_grftsnt ? 'G' : ' ', VIFM_ISSET(r->rt_parent, gt->gt_scope) ? 'B' : ' '); for (i = 0; i < numvifs; ++i) { if (VIFM_ISSET(i, gt->gt_grpmems)) fprintf(fp2, " %u ", i); else if (VIFM_ISSET(i, r->rt_children) && NBRM_ISSETMASK(uvifs[i].uv_nbrmap, r->rt_subordinates)) fprintf(fp2, " %u%c", i, VIFM_ISSET(i, gt->gt_scope) ? 'b' : SUBS_ARE_PRUNED(r->rt_subordinates, uvifs[i].uv_nbrmap, gt->gt_prunes) ? 'p' : '!'); } fprintf(fp2, "\n"); if (gt->gt_pruntbl) { fprintf(fp2, "<"); c = '('; for (pt = gt->gt_pruntbl; pt; pt = pt->pt_next) { fprintf(fp2, "%c%s:%d[%d]/%d", c, inet_fmt(pt->pt_router, s1), pt->pt_vifi, pt->pt_index, pt->pt_timer); c = ','; } fprintf(fp2, ")"); fprintf(fp2, " 0x%08lx%08lx\n",/*XXX*/ gt->gt_prunes.hi, gt->gt_prunes.lo); } for (st = gt->gt_srctbl; st; st = st->st_next) { fprintf(fp2, ">%-18s %-8s %6ld", inet_fmt(st->st_origin, s1), st->st_ctime ? scaletime(thyme - st->st_ctime) : "-", st->st_savpkt); if (st->st_ctime) { struct sioc_sg_req sg_req; sg_req.src.s_addr = st->st_origin; sg_req.grp.s_addr = gt->gt_mcastgrp; if (ioctl(udp_socket, SIOCGETSGCNT, (char *)&sg_req) < 0) { log(LOG_WARNING, errno, "SIOCGETSGCNT on (%s %s)", inet_fmt(st->st_origin, s1), inet_fmt(gt->gt_mcastgrp, s2)); } else { fprintf(fp2, " %8ld %8ld %4ld", sg_req.pktcnt, sg_req.bytecnt, sg_req.wrong_if); } } fprintf(fp2, "\n"); } } } /* * Traceroute function which returns traceroute replies to the requesting * router. Also forwards the request to downstream routers. */ void accept_mtrace(src, dst, group, data, no, datalen) u_int32 src; u_int32 dst; u_int32 group; char *data; u_int no; /* promoted u_char */ int datalen; { u_char type; struct rtentry *rt; struct gtable *gt; struct tr_query *qry; struct tr_resp *resp; int vifi; char *p; int rcount; int errcode = TR_NO_ERR; int resptype; struct timeval tp; struct sioc_vif_req v_req; struct sioc_sg_req sg_req; /* Remember qid across invocations */ static u_int32 oqid = 0; /* timestamp the request/response */ gettimeofday(&tp, 0); /* * Check if it is a query or a response */ if (datalen == QLEN) { type = QUERY; IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "Initial traceroute query rcvd from %s to %s", inet_fmt(src, s1), inet_fmt(dst, s2)); } else if ((datalen - QLEN) % RLEN == 0) { type = RESP; IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "In-transit traceroute query rcvd from %s to %s", inet_fmt(src, s1), inet_fmt(dst, s2)); if (IN_MULTICAST(ntohl(dst))) { IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "Dropping multicast response"); return; } } else { log(LOG_WARNING, 0, "%s from %s to %s", "Non decipherable traceroute request recieved", inet_fmt(src, s1), inet_fmt(dst, s2)); return; } qry = (struct tr_query *)data; /* * if it is a packet with all reports filled, drop it */ if ((rcount = (datalen - QLEN)/RLEN) == no) { IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "packet with all reports filled in"); return; } IF_DEBUG(DEBUG_TRACE) { log(LOG_DEBUG, 0, "s: %s g: %s d: %s ", inet_fmt(qry->tr_src, s1), inet_fmt(group, s2), inet_fmt(qry->tr_dst, s3)); log(LOG_DEBUG, 0, "rttl: %d rd: %s", qry->tr_rttl, inet_fmt(qry->tr_raddr, s1)); log(LOG_DEBUG, 0, "rcount:%d, qid:%06x", rcount, qry->tr_qid); } /* determine the routing table entry for this traceroute */ rt = determine_route(qry->tr_src); IF_DEBUG(DEBUG_TRACE) if (rt) { log(LOG_DEBUG, 0, "rt parent vif: %d rtr: %s metric: %d", rt->rt_parent, inet_fmt(rt->rt_gateway, s1), rt->rt_metric); log(LOG_DEBUG, 0, "rt origin %s", RT_FMT(rt, s1)); } else log(LOG_DEBUG, 0, "...no route"); /* * Query type packet - check if rte exists * Check if the query destination is a vif connected to me. * and if so, whether I should start response back */ if (type == QUERY) { if (oqid == qry->tr_qid) { /* * If the multicast router is a member of the group being * queried, and the query is multicasted, then the router can * recieve multiple copies of the same query. If we have already * replied to this traceroute, just ignore it this time. * * This is not a total solution, but since if this fails you * only get N copies, N <= the number of interfaces on the router, * it is not fatal. */ IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "ignoring duplicate traceroute packet"); return; } if (rt == NULL) { IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "Mcast traceroute: no route entry %s", inet_fmt(qry->tr_src, s1)); if (IN_MULTICAST(ntohl(dst))) return; } vifi = find_vif(qry->tr_dst, 0); if (vifi == NO_VIF) { /* The traceroute destination is not on one of my subnet vifs. */ IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "Destination %s not an interface", inet_fmt(qry->tr_dst, s1)); if (IN_MULTICAST(ntohl(dst))) return; errcode = TR_WRONG_IF; } else if (rt != NULL && !VIFM_ISSET(vifi, rt->rt_children)) { IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "Destination %s not on forwarding tree for src %s", inet_fmt(qry->tr_dst, s1), inet_fmt(qry->tr_src, s2)); if (IN_MULTICAST(ntohl(dst))) return; errcode = TR_WRONG_IF; } } else { /* * determine which interface the packet came in on * RESP packets travel hop-by-hop so this either traversed * a tunnel or came from a directly attached mrouter. */ if ((vifi = find_vif(src, dst)) == NO_VIF) { IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "Wrong interface for packet"); errcode = TR_WRONG_IF; } } /* Now that we've decided to send a response, save the qid */ oqid = qry->tr_qid; IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "Sending traceroute response"); /* copy the packet to the sending buffer */ p = send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN; bcopy(data, p, datalen); p += datalen; /* * If there is no room to insert our reply, coopt the previous hop * error indication to relay this fact. */ if (p + sizeof(struct tr_resp) > send_buf + RECV_BUF_SIZE) { resp = (struct tr_resp *)p - 1; resp->tr_rflags = TR_NO_SPACE; rt = NULL; goto sendit; } /* * fill in initial response fields */ resp = (struct tr_resp *)p; bzero(resp, sizeof(struct tr_resp)); datalen += RLEN; resp->tr_qarr = htonl(((tp.tv_sec + JAN_1970) << 16) + ((tp.tv_usec << 10) / 15625)); resp->tr_rproto = PROTO_DVMRP; resp->tr_outaddr = (vifi == NO_VIF) ? dst : uvifs[vifi].uv_lcl_addr; resp->tr_fttl = (vifi == NO_VIF) ? 0 : uvifs[vifi].uv_threshold; resp->tr_rflags = errcode; /* * obtain # of packets out on interface */ v_req.vifi = vifi; if (vifi != NO_VIF && ioctl(udp_socket, SIOCGETVIFCNT, (char *)&v_req) >= 0) resp->tr_vifout = htonl(v_req.ocount); else resp->tr_vifout = 0xffffffff; /* * fill in scoping & pruning information */ if (rt) for (gt = rt->rt_groups; gt; gt = gt->gt_next) { if (gt->gt_mcastgrp >= group) break; } else gt = NULL; if (gt && gt->gt_mcastgrp == group) { struct stable *st; for (st = gt->gt_srctbl; st; st = st->st_next) if (qry->tr_src == st->st_origin) break; sg_req.src.s_addr = qry->tr_src; sg_req.grp.s_addr = group; if (st && st->st_ctime != 0 && ioctl(udp_socket, SIOCGETSGCNT, (char *)&sg_req) >= 0) resp->tr_pktcnt = htonl(sg_req.pktcnt + st->st_savpkt); else resp->tr_pktcnt = htonl(st ? st->st_savpkt : 0xffffffff); if (VIFM_ISSET(vifi, gt->gt_scope)) resp->tr_rflags = TR_SCOPED; else if (gt->gt_prsent_timer) resp->tr_rflags = TR_PRUNED; else if (!VIFM_ISSET(vifi, gt->gt_grpmems)) if (!NBRM_ISEMPTY(uvifs[vifi].uv_nbrmap) && SUBS_ARE_PRUNED(rt->rt_subordinates, uvifs[vifi].uv_nbrmap, gt->gt_prunes)) resp->tr_rflags = TR_OPRUNED; else resp->tr_rflags = TR_NO_FWD; } else { if ((vifi != NO_VIF && scoped_addr(vifi, group)) || (rt && scoped_addr(rt->rt_parent, group))) resp->tr_rflags = TR_SCOPED; else if (rt && !VIFM_ISSET(vifi, rt->rt_children)) resp->tr_rflags = TR_NO_FWD; } /* * if no rte exists, set NO_RTE error */ if (rt == NULL) { src = dst; /* the dst address of resp. pkt */ resp->tr_inaddr = 0; resp->tr_rflags = TR_NO_RTE; resp->tr_rmtaddr = 0; } else { /* get # of packets in on interface */ v_req.vifi = rt->rt_parent; if (ioctl(udp_socket, SIOCGETVIFCNT, (char *)&v_req) >= 0) resp->tr_vifin = htonl(v_req.icount); else resp->tr_vifin = 0xffffffff; MASK_TO_VAL(rt->rt_originmask, resp->tr_smask); src = uvifs[rt->rt_parent].uv_lcl_addr; resp->tr_inaddr = src; resp->tr_rmtaddr = rt->rt_gateway; if (!VIFM_ISSET(vifi, rt->rt_children)) { IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "Destination %s not on forwarding tree for src %s", inet_fmt(qry->tr_dst, s1), inet_fmt(qry->tr_src, s2)); resp->tr_rflags = TR_WRONG_IF; } if (rt->rt_metric >= UNREACHABLE) { resp->tr_rflags = TR_NO_RTE; /* Hack to send reply directly */ rt = NULL; } } sendit: /* * if metric is 1 or no. of reports is 1, send response to requestor * else send to upstream router. If the upstream router can't handle * mtrace, set an error code and send to requestor anyway. */ IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "rcount:%d, no:%d", rcount, no); if ((rcount + 1 == no) || (rt == NULL) || (rt->rt_metric == 1)) { resptype = IGMP_MTRACE_RESP; dst = qry->tr_raddr; } else if (!can_mtrace(rt->rt_parent, rt->rt_gateway)) { dst = qry->tr_raddr; resp->tr_rflags = TR_OLD_ROUTER; resptype = IGMP_MTRACE_RESP; } else { dst = rt->rt_gateway; resptype = IGMP_MTRACE; } if (IN_MULTICAST(ntohl(dst))) { /* * Send the reply on a known multicast capable vif. * If we don't have one, we can't source any multicasts anyway. */ if (phys_vif != -1) { IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "Sending reply to %s from %s", inet_fmt(dst, s1), inet_fmt(uvifs[phys_vif].uv_lcl_addr, s2)); k_set_ttl(qry->tr_rttl); send_igmp(uvifs[phys_vif].uv_lcl_addr, dst, resptype, no, group, datalen); k_set_ttl(1); } else log(LOG_INFO, 0, "No enabled phyints -- %s", "dropping traceroute reply"); } else { IF_DEBUG(DEBUG_TRACE) log(LOG_DEBUG, 0, "Sending %s to %s from %s", resptype == IGMP_MTRACE_RESP ? "reply" : "request on", inet_fmt(dst, s1), inet_fmt(src, s2)); send_igmp(src, dst, resptype, no, group, datalen); } return; }