From 6554f2c43d30c73b48939bf53b7f46122490af8d Mon Sep 17 00:00:00 2001 From: Jeffrey Hsu Date: Fri, 4 Mar 2005 03:34:47 +0000 Subject: [PATCH] Readability changes. --- sys/net/route.c | 232 ++++++++++++++++++++++++------------------------ 1 file changed, 117 insertions(+), 115 deletions(-) diff --git a/sys/net/route.c b/sys/net/route.c index d430e98f45..7b69a65764 100644 --- a/sys/net/route.c +++ b/sys/net/route.c @@ -82,7 +82,7 @@ * * @(#)route.c 8.3 (Berkeley) 1/9/95 * $FreeBSD: src/sys/net/route.c,v 1.59.2.10 2003/01/17 08:04:00 ru Exp $ - * $DragonFly: src/sys/net/route.c,v 1.18 2005/03/04 02:54:31 hsu Exp $ + * $DragonFly: src/sys/net/route.c,v 1.19 2005/03/04 03:34:47 hsu Exp $ */ #include "opt_inet.h" @@ -163,13 +163,13 @@ rtalloc_ign(struct route *ro, u_long ignoreflags) * Look up the route that matches the given "dst" address. * * Route lookup can have the side-effect of creating and returning - * a cloned route instead if "dst" matches a cloning route and the + * a cloned route instead when "dst" matches a cloning route and the * RTF_CLONING and RTF_PRCLONING flags are not being ignored. * * Any route returned has its reference count incremented. */ struct rtentry * -_rtlookup(struct sockaddr *dst, boolean_t report, u_long ignore) +_rtlookup(struct sockaddr *dst, boolean_t generate_report, u_long ignore) { struct radix_node_head *rnh = rt_tables[dst->sa_family]; struct rtentry *rt; @@ -195,12 +195,12 @@ _rtlookup(struct sockaddr *dst, boolean_t report, u_long ignore) error = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0, &clonedroute); /* clone the route */ if (error != 0) { /* cloning failed */ - if (report) + if (generate_report) rt_dstmsg(RTM_MISS, dst, error); rt->rt_refcnt++; return (rt); /* return the uncloned route */ } - if (report) { + if (generate_report) { if (clonedroute->rt_flags & RTF_XRESOLVE) rt_dstmsg(RTM_RESOLVE, dst, 0); else @@ -218,7 +218,7 @@ _rtlookup(struct sockaddr *dst, boolean_t report, u_long ignore) unreach: rtstat.rts_unreach++; - if (report) + if (generate_report) rt_dstmsg(RTM_MISS, dst, 0); return (NULL); } @@ -247,9 +247,8 @@ rtfree(struct rtentry *rt) } /* - * Force a routing table entry to the specified - * destination to go through the given gateway. - * Normally called as a result of a routing redirect + * Force a routing table entry to the specified destination to go through + * the given gateway. Normally called as a result of a routing redirect * message from the network layer. * * N.B.: must be called at splnet @@ -258,8 +257,8 @@ void rtredirect(struct sockaddr *dst, struct sockaddr *gateway, struct sockaddr *netmask, int flags, struct sockaddr *src) { - struct rtentry *rt; - struct rt_addrinfo info; + struct rtentry *rt = NULL; + struct rt_addrinfo rtinfo; struct ifaddr *ifa; short *stat = NULL; int error; @@ -271,86 +270,90 @@ rtredirect(struct sockaddr *dst, struct sockaddr *gateway, } /* - * If the redirect isn't from our current router for this dst, - * it's either old or wrong. If it redirects us to ourselves, - * we have a routing loop, perhaps as a result of an interface - * going down recently. + * If the redirect isn't from our current router for this destination, + * it's either old or wrong. */ - if (!(flags & RTF_DONE) && + if (!(flags & RTF_DONE) && /* XXX JH */ (rt = rtpurelookup(dst)) != NULL && (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) { error = EINVAL; goto done; - } else if (ifa_ifwithaddr(gateway)) { + } + + /* + * If it redirects us to ourselves, we have a routing loop, + * perhaps as a result of an interface going down recently. + */ + if (ifa_ifwithaddr(gateway)) { error = EHOSTUNREACH; goto done; } /* - * Create a new entry if we just got back a wildcard entry - * or the the lookup failed. This is necessary for hosts - * which use routing redirects generated by smart gateways - * to dynamically build the routing tables. + * Create a new entry if the lookup failed or if we got back + * a wildcard entry for the default route. This is necessary + * for hosts which use routing redirects generated by smart + * gateways to dynamically build the routing tables. */ - if (rt == NULL || (rt_mask(rt) != NULL && rt_mask(rt)->sa_len < 2)) + if (rt == NULL) + goto create; + if ((rt_mask(rt) != NULL && rt_mask(rt)->sa_len < 2)) { + rtfree(rt); goto create; + } - /* - * Don't listen to the redirect if it's for a route to an interface. - */ - if (rt->rt_flags & RTF_GATEWAY) { - if ((!(rt->rt_flags & RTF_HOST)) && (flags & RTF_HOST)) { - /* - * Changing from route to net => route to host. - * Create new route, rather than smashing route to net. - */ + /* Ignore redirects for directly connected hosts. */ + if (!(rt->rt_flags & RTF_GATEWAY)) { + error = EHOSTUNREACH; + goto done; + } + + if (!(rt->rt_flags & RTF_HOST) && (flags & RTF_HOST)) { + /* + * Changing from a network route to a host route. + * Create a new host route rather than smashing the + * network route. + */ create: - if (rt != NULL) - rtfree(rt); - flags |= RTF_GATEWAY | RTF_DYNAMIC; - bzero(&info, sizeof info); - info.rti_info[RTAX_DST] = dst; - info.rti_info[RTAX_GATEWAY] = gateway; - info.rti_info[RTAX_NETMASK] = netmask; - info.rti_ifa = ifa; - info.rti_flags = flags; - rt = NULL; - error = rtrequest1(RTM_ADD, &info, &rt); - if (rt != NULL) - flags = rt->rt_flags; - stat = &rtstat.rts_dynamic; - } else { - /* - * Smash the current notion of the gateway to - * this destination. Should check about netmask!!! - */ - rt->rt_flags |= RTF_MODIFIED; - flags |= RTF_MODIFIED; - stat = &rtstat.rts_newgateway; - /* Add the key and gateway (in one malloc'ed chunk). */ - rt_setgate(rt, rt_key(rt), gateway); - error = 0; - } + flags |= RTF_GATEWAY | RTF_DYNAMIC; + bzero(&rtinfo, sizeof(struct rt_addrinfo)); + rtinfo.rti_info[RTAX_DST] = dst; + rtinfo.rti_info[RTAX_GATEWAY] = gateway; + rtinfo.rti_info[RTAX_NETMASK] = netmask; + rtinfo.rti_flags = flags; + rtinfo.rti_ifa = ifa; + rt = NULL; /* copy-in/copy-out parameter */ + error = rtrequest1(RTM_ADD, &rtinfo, &rt); + if (rt != NULL) + flags = rt->rt_flags; + stat = &rtstat.rts_dynamic; } else { - error = EHOSTUNREACH; + /* + * Smash the current notion of the gateway to this destination. + * Should check about netmask!!! + */ + rt->rt_flags |= RTF_MODIFIED; + flags |= RTF_MODIFIED; + rt_setgate(rt, rt_key(rt), gateway); + error = 0; + stat = &rtstat.rts_newgateway; } done: if (rt != NULL) rtfree(rt); - out: if (error != 0) rtstat.rts_badredirect++; else if (stat != NULL) (*stat)++; - bzero(&info, sizeof info); - info.rti_info[RTAX_DST] = dst; - info.rti_info[RTAX_GATEWAY] = gateway; - info.rti_info[RTAX_NETMASK] = netmask; - info.rti_info[RTAX_AUTHOR] = src; - rt_missmsg(RTM_REDIRECT, &info, flags, error); + bzero(&rtinfo, sizeof(struct rt_addrinfo)); + rtinfo.rti_info[RTAX_DST] = dst; + rtinfo.rti_info[RTAX_GATEWAY] = gateway; + rtinfo.rti_info[RTAX_NETMASK] = netmask; + rtinfo.rti_info[RTAX_AUTHOR] = src; + rt_missmsg(RTM_REDIRECT, &rtinfo, flags, error); } /* @@ -407,11 +410,11 @@ ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway) return (NULL); } if (ifa->ifa_addr->sa_family != dst->sa_family) { - struct ifaddr *oifa = ifa; + struct ifaddr *oldifa = ifa; ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); if (ifa == NULL) - ifa = oifa; + ifa = oldifa; } return (ifa); } @@ -487,20 +490,20 @@ rtrequest( int flags, struct rtentry **ret_nrt) { - struct rt_addrinfo info; - - bzero(&info, sizeof info); - info.rti_flags = flags; - info.rti_info[RTAX_DST] = dst; - info.rti_info[RTAX_GATEWAY] = gateway; - info.rti_info[RTAX_NETMASK] = netmask; - return rtrequest1(req, &info, ret_nrt); + struct rt_addrinfo rtinfo; + + bzero(&rtinfo, sizeof(struct rt_addrinfo)); + rtinfo.rti_info[RTAX_DST] = dst; + rtinfo.rti_info[RTAX_GATEWAY] = gateway; + rtinfo.rti_info[RTAX_NETMASK] = netmask; + rtinfo.rti_flags = flags; + return rtrequest1(req, &rtinfo, ret_nrt); } int -rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt) +rtrequest1(int req, struct rt_addrinfo *rtinfo, struct rtentry **ret_nrt) { - struct sockaddr *dst = info->rti_info[RTAX_DST]; + struct sockaddr *dst = rtinfo->rti_info[RTAX_DST]; struct rtentry *rt; struct radix_node *rn; struct radix_node_head *rnh; @@ -522,16 +525,16 @@ rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt) * If we are adding a host route then we don't want to put * a netmask in the tree, nor do we want to clone it. */ - if (info->rti_flags & RTF_HOST) { - info->rti_info[RTAX_NETMASK] = NULL; - info->rti_flags &= ~(RTF_CLONING | RTF_PRCLONING); + if (rtinfo->rti_flags & RTF_HOST) { + rtinfo->rti_info[RTAX_NETMASK] = NULL; + rtinfo->rti_flags &= ~(RTF_CLONING | RTF_PRCLONING); } switch (req) { case RTM_DELETE: /* Remove the item from the tree. */ - rn = rnh->rnh_deladdr((char *)info->rti_info[RTAX_DST], - (char *)info->rti_info[RTAX_NETMASK], + rn = rnh->rnh_deladdr((char *)rtinfo->rti_info[RTAX_DST], + (char *)rtinfo->rti_info[RTAX_NETMASK], rnh); if (rn == NULL) gotoerr(ESRCH); @@ -561,7 +564,7 @@ rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt) /* Give the protocol a chance to keep things in sync. */ if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) - ifa->ifa_rtrequest(RTM_DELETE, rt, info); + ifa->ifa_rtrequest(RTM_DELETE, rt, rtinfo); /* * If the caller wants it, then it can have it, @@ -582,37 +585,38 @@ rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt) if (ret_nrt == NULL || (rt = *ret_nrt) == NULL) gotoerr(EINVAL); ifa = rt->rt_ifa; - info->rti_flags = + rtinfo->rti_flags = rt->rt_flags & ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC); - info->rti_flags |= RTF_WASCLONED; - info->rti_info[RTAX_GATEWAY] = rt->rt_gateway; - if ((info->rti_info[RTAX_NETMASK] = rt->rt_genmask) == NULL) - info->rti_flags |= RTF_HOST; + rtinfo->rti_flags |= RTF_WASCLONED; + rtinfo->rti_info[RTAX_GATEWAY] = rt->rt_gateway; + if ((rtinfo->rti_info[RTAX_NETMASK] = rt->rt_genmask) == NULL) + rtinfo->rti_flags |= RTF_HOST; goto makeroute; case RTM_ADD: - KASSERT(!(info->rti_flags & RTF_GATEWAY) || - info->rti_info[RTAX_GATEWAY] != NULL, + KASSERT(!(rtinfo->rti_flags & RTF_GATEWAY) || + rtinfo->rti_info[RTAX_GATEWAY] != NULL, ("rtrequest: GATEWAY but no gateway")); - if (info->rti_ifa == NULL && (error = rt_getifa(info))) + if (rtinfo->rti_ifa == NULL && (error = rt_getifa(rtinfo))) gotoerr(error); - ifa = info->rti_ifa; + ifa = rtinfo->rti_ifa; makeroute: - R_Malloc(rt, struct rtentry *, sizeof *rt); + R_Malloc(rt, struct rtentry *, sizeof(struct rtentry)); if (rt == NULL) gotoerr(ENOBUFS); - bzero(rt, sizeof *rt); - rt->rt_flags = RTF_UP | info->rti_flags; - error = rt_setgate(rt, dst, info->rti_info[RTAX_GATEWAY]); + bzero(rt, sizeof(struct rtentry)); + rt->rt_flags = RTF_UP | rtinfo->rti_flags; + error = rt_setgate(rt, dst, rtinfo->rti_info[RTAX_GATEWAY]); if (error != 0) { Free(rt); gotoerr(error); } ndst = rt_key(rt); - if (info->rti_info[RTAX_NETMASK] != NULL) - rt_maskedcopy(dst, ndst, info->rti_info[RTAX_NETMASK]); + if (rtinfo->rti_info[RTAX_NETMASK] != NULL) + rt_maskedcopy(dst, ndst, + rtinfo->rti_info[RTAX_NETMASK]); else bcopy(dst, ndst, dst->sa_len); @@ -627,7 +631,7 @@ makeroute: /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ rn = rnh->rnh_addaddr((char *)ndst, - (char *)info->rti_info[RTAX_NETMASK], + (char *)rtinfo->rti_info[RTAX_NETMASK], rnh, rt->rt_nodes); if (rn == NULL) { struct rtentry *oldrt; @@ -647,9 +651,9 @@ makeroute: rt_mask(oldrt), oldrt->rt_flags, NULL); rn = rnh->rnh_addaddr((char *)ndst, - (char *) - info->rti_info[RTAX_NETMASK], - rnh, rt->rt_nodes); + (char *) + rtinfo->rti_info[RTAX_NETMASK], + rnh, rt->rt_nodes); } } } @@ -687,7 +691,7 @@ makeroute: * allow it to do that as well. */ if (ifa->ifa_rtrequest != NULL) - ifa->ifa_rtrequest(req, rt, info); + ifa->ifa_rtrequest(req, rt, rtinfo); /* * We repeat the same procedure from rt_setgate() here because @@ -1005,19 +1009,17 @@ rt_llroute(struct sockaddr *dst, struct rtentry *rt0, struct rtentry **drt) } /* - * Set up a routing table entry, normally - * for an interface. + * Set up a routing table entry, normally for an interface. */ int rtinit(struct ifaddr *ifa, int cmd, int flags) { struct sockaddr *dst, *deldst, *netmask; struct rtentry *rt; - struct rtentry *nrt = NULL; struct mbuf *m = NULL; struct radix_node_head *rnh; struct radix_node *rn; - struct rt_addrinfo info; + struct rt_addrinfo rtinfo; int error; if (flags & RTF_HOST) { @@ -1074,14 +1076,14 @@ rtinit(struct ifaddr *ifa, int cmd, int flags) /* * Do the actual request */ - bzero(&info, sizeof info); - info.rti_ifa = ifa; - info.rti_flags = flags | ifa->ifa_flags; - info.rti_info[RTAX_DST] = dst; - info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; - info.rti_info[RTAX_NETMASK] = netmask; - error = rtrequest1(cmd, &info, &nrt); - if (error == 0 && (rt = nrt) != NULL) { + bzero(&rtinfo, sizeof(struct rt_addrinfo)); + rtinfo.rti_info[RTAX_DST] = dst; + rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; + rtinfo.rti_info[RTAX_NETMASK] = netmask; + rtinfo.rti_flags = flags | ifa->ifa_flags; + rtinfo.rti_ifa = ifa; + error = rtrequest1(cmd, &rtinfo, &rt); + if (error == 0 && rt != NULL) { /* * notify any listening routing agents of the change */ -- 2.41.0