From: Roy Marples Date: Wed, 28 Mar 2018 10:00:18 +0000 (+0100) Subject: options: allow static routes to be configured on the command line X-Git-Tag: v7.0.3~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=77cdf11571cc21d6ec72987e1ed938da04cf8b02;p=thirdparty%2Fdhcpcd.git options: allow static routes to be configured on the command line --- diff --git a/src/if-options.c b/src/if-options.c index 40a7e7a5..bd051b3b 100644 --- a/src/if-options.c +++ b/src/if-options.c @@ -1086,14 +1086,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, strncmp(arg, "ms_classless_static_routes=", strlen("ms_classless_static_routes=")) == 0) { - struct interface *ifp; struct in_addr addr3; - ifp = if_find(ctx->ifaces, ifname); - if (ifp == NULL) { - logerrx("static routes require an interface"); - return -1; - } fp = np = strwhite(p); if (np == NULL) { logerrx("all routes need a gateway"); @@ -1107,7 +1101,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, *fp = ' '; return -1; } - if ((rt = rt_new(ifp)) == NULL) { + if ((rt = rt_new0(ctx)) == NULL) { *fp = ' '; return -1; } @@ -1117,16 +1111,9 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, TAILQ_INSERT_TAIL(&ifo->routes, rt, rt_next); *fp = ' '; } else if (strncmp(arg, "routers=", strlen("routers=")) == 0) { - struct interface *ifp; - - ifp = if_find(ctx->ifaces, ifname); - if (ifp == NULL) { - logerrx("static routes require an interface"); - return -1; - } if (parse_addr(&addr, NULL, p) == -1) return -1; - if ((rt = rt_new(ifp)) == NULL) + if ((rt = rt_new0(ctx)) == NULL) return -1; addr2.s_addr = INADDR_ANY; sa_in_init(&rt->rt_dest, &addr2); diff --git a/src/ipv4.c b/src/ipv4.c index d40b3f63..955f2020 100644 --- a/src/ipv4.c +++ b/src/ipv4.c @@ -289,10 +289,11 @@ inet_dhcproutes(struct rt_head *routes, struct interface *ifp) TAILQ_FOREACH(r, &ifp->options->routes, rt_next) { if (sa_is_unspecified(&r->rt_gateway)) break; - if ((rt = rt_new(ifp)) == NULL) + if ((rt = rt_new0(ifp->ctx)) == NULL) return -1; - rt->rt_dflags = RTDF_STATIC; memcpy(rt, r, sizeof(*rt)); + rt_setif(rt, ifp); + rt->rt_dflags = RTDF_STATIC; TAILQ_INSERT_TAIL(&nroutes, rt, rt_next); } } else { diff --git a/src/route.c b/src/route.c index 70d8868b..4ce073fc 100644 --- a/src/route.c +++ b/src/route.c @@ -146,13 +146,11 @@ rt_dispose(struct dhcpcd_ctx *ctx) } struct rt * -rt_new(struct interface *ifp) +rt_new0(struct dhcpcd_ctx *ctx) { struct rt *rt; - struct dhcpcd_ctx *ctx; - assert(ifp != NULL); - ctx = ifp->ctx; + assert(ctx != NULL); if ((rt = TAILQ_FIRST(&ctx->froutes)) != NULL) TAILQ_REMOVE(&ctx->froutes, rt, rt_next); else if ((rt = malloc(sizeof(*rt))) == NULL) { @@ -160,10 +158,30 @@ rt_new(struct interface *ifp) return NULL; } memset(rt, 0, sizeof(*rt)); + return rt; +} + +void +rt_setif(struct rt *rt, struct interface *ifp) +{ + + assert(rt != NULL); + assert(ifp != NULL); rt->rt_ifp = ifp; #ifdef HAVE_ROUTE_METRIC rt->rt_metric = ifp->metric; #endif +} + +struct rt * +rt_new(struct interface *ifp) +{ + struct rt *rt; + + assert(ifp != NULL); + if ((rt = rt_new0(ifp->ctx)) == NULL) + return NULL; + rt_setif(rt, ifp); return rt; } diff --git a/src/route.h b/src/route.h index 160f6ff9..db316e2e 100644 --- a/src/route.h +++ b/src/route.h @@ -88,6 +88,8 @@ void rt_free(struct rt *); void rt_freeif(struct interface *); void rt_headclear(struct rt_head *, int); void rt_headfreeif(struct rt_head *); +struct rt * rt_new0(struct dhcpcd_ctx *); +void rt_setif(struct rt *, struct interface *); struct rt * rt_new(struct interface *); void rt_recvrt(int, const struct rt *); void rt_build(struct dhcpcd_ctx *, int);