X-Git-Url: https://git.decadent.org.uk/gitweb/?p=odhcp6c.git;a=blobdiff_plain;f=src%2Fscript.c;h=644f61f8a697921a2a21da98c1b67ccc045ab4a1;hp=847e4d7ec3a30a3b216186c7d376b104433f4802;hb=6373af63310f32e1408a81f4e4e069a5f0d3e59e;hpb=76c4797e82ec820d488e41ffe37adc107b5d0218 diff --git a/src/script.c b/src/script.c index 847e4d7..644f61f 100644 --- a/src/script.c +++ b/src/script.c @@ -41,8 +41,6 @@ static const int8_t hexvals[] = { static char *argv[4] = {NULL, NULL, NULL, NULL}; -static volatile char *delayed_call = NULL; -static bool dont_delay = false; int script_init(const char *path, const char *ifname) @@ -209,7 +207,7 @@ static void entry_to_env(const char *name, const void *data, size_t len, enum en putenv(buf); } -#ifdef EXT_S46 + static void s46_to_env_portparams(const uint8_t *data, size_t len, FILE *fp) { uint8_t *odata; @@ -223,7 +221,7 @@ static void s46_to_env_portparams(const uint8_t *data, size_t len, FILE *fp) } } } -#endif + static void s46_to_env(enum odhcp6c_state state, const uint8_t *data, size_t len) { @@ -237,7 +235,9 @@ static void s46_to_env(enum odhcp6c_state state, const uint8_t *data, size_t len fputs(name, fp); fputc('=', fp); -#ifdef EXT_S46 + const char *type = (state == STATE_S46_MAPE) ? "map-e" : + (state == STATE_S46_MAPT) ? "map-t" : "lw4o6"; + uint8_t *odata; uint16_t otype, olen; dhcpv6_for_each_option(data, &data[len], otype, olen, odata) { @@ -246,12 +246,18 @@ static void s46_to_env(enum odhcp6c_state state, const uint8_t *data, size_t len struct dhcpv6_s46_v4v6bind *bind = (struct dhcpv6_s46_v4v6bind*)odata; if (state != STATE_S46_LW && otype == DHCPV6_OPT_S46_RULE && - olen >= sizeof(struct dhcpv6_s46_rule) && olen >= - sizeof(struct dhcpv6_s46_rule) + rule->prefix6_len) { + olen >= sizeof(struct dhcpv6_s46_rule)) { char buf4[INET_ADDRSTRLEN]; char buf6[INET6_ADDRSTRLEN]; struct in6_addr in6 = IN6ADDR_ANY_INIT; - memcpy(&in6, rule->ipv6_prefix, rule->prefix6_len); + + size_t prefix6len = rule->prefix6_len; + prefix6len = (prefix6len % 8 == 0) ? prefix6len / 8 : prefix6len / 8 + 1; + + if (olen < sizeof(struct dhcpv6_s46_rule) + prefix6len) + continue; + + memcpy(&in6, rule->ipv6_prefix, prefix6len); inet_ntop(AF_INET, &rule->ipv4_prefix, buf4, sizeof(buf4)); inet_ntop(AF_INET6, &in6, buf6, sizeof(buf6)); @@ -259,84 +265,80 @@ static void s46_to_env(enum odhcp6c_state state, const uint8_t *data, size_t len if (rule->flags & 1) fputs("fmr,", fp); - fprintf(fp, "ealen=%d,prefix4len=%d,prefix6len=%d,ipv4prefix=%s,ipv6prefix=%s,", - rule->ea_len, rule->prefix4_len, rule->prefix6_len, buf4, buf6); - - s46_to_env_portparams(&rule->ipv6_prefix[rule->prefix6_len], - olen - sizeof(*rule) - rule->prefix6_len, fp); + fprintf(fp, "type=%s,ealen=%d,prefix4len=%d,prefix6len=%d,ipv4prefix=%s,ipv6prefix=%s,", + type, rule->ea_len, rule->prefix4_len, rule->prefix6_len, buf4, buf6); + + s46_to_env_portparams(&rule->ipv6_prefix[prefix6len], + olen - sizeof(*rule) - prefix6len, fp); + + dhcpv6_for_each_option(data, &data[len], otype, olen, odata) { + if (state != STATE_S46_MAPT && otype == DHCPV6_OPT_S46_BR && + olen == sizeof(struct in6_addr)) { + inet_ntop(AF_INET6, odata, buf6, sizeof(buf6)); + fprintf(fp, "br=%s,", buf6); + } else if (state == STATE_S46_MAPT && otype == DHCPV6_OPT_S46_DMR && + olen >= sizeof(struct dhcpv6_s46_dmr)) { + dmr = (struct dhcpv6_s46_dmr*)odata; + memset(&in6, 0, sizeof(in6)); + size_t prefix6len = dmr->dmr_prefix6_len; + prefix6len = (prefix6len % 8 == 0) ? prefix6len / 8 : prefix6len / 8 + 1; + + if (olen < sizeof(struct dhcpv6_s46_dmr) + prefix6len) + continue; + + memcpy(&in6, dmr->dmr_ipv6_prefix, prefix6len); + inet_ntop(AF_INET6, &in6, buf6, sizeof(buf6)); + fprintf(fp, "dmr=%s/%d,", buf6, dmr->dmr_prefix6_len); + } + } } else if (state == STATE_S46_LW && otype == DHCPV6_OPT_S46_V4V6BIND && - olen >= sizeof(struct dhcpv6_s46_v4v6bind) && olen >= - sizeof(struct dhcpv6_s46_v4v6bind) + bind->bindprefix6_len) { + olen >= sizeof(struct dhcpv6_s46_v4v6bind)) { char buf4[INET_ADDRSTRLEN]; char buf6[INET6_ADDRSTRLEN]; struct in6_addr in6 = IN6ADDR_ANY_INIT; - memcpy(&in6, bind->bind_ipv6_prefix, bind->bindprefix6_len); + + size_t prefix6len = bind->bindprefix6_len; + prefix6len = (prefix6len % 8 == 0) ? prefix6len / 8 : prefix6len / 8 + 1; + + if (olen < sizeof(struct dhcpv6_s46_v4v6bind) + prefix6len) + continue; + + memcpy(&in6, bind->bind_ipv6_prefix, prefix6len); inet_ntop(AF_INET, &bind->ipv4_address, buf4, sizeof(buf4)); inet_ntop(AF_INET6, &in6, buf6, sizeof(buf6)); - fprintf(fp, "ipv4address=%s,prefix6len=%d,ipv6prefix=%s,", - buf4, bind->bindprefix6_len, buf6); + fprintf(fp, "type=%s,ipv4address=%s,prefix6len=%d,ipv6prefix=%s,", + type, buf4, bind->bindprefix6_len, buf6); - s46_to_env_portparams(&bind->bind_ipv6_prefix[bind->bindprefix6_len], - olen - sizeof(*bind) - bind->bindprefix6_len, fp); - } else if (state != STATE_S46_MAPT && otype == DHCPV6_OPT_S46_BR - && olen == sizeof(struct in6_addr)) { - char buf6[INET6_ADDRSTRLEN]; - inet_ntop(AF_INET6, odata, buf6, sizeof(buf6)); - fprintf(fp, "br=%s,", buf6); - } else if (state == STATE_S46_MAPT && otype == DHCPV6_OPT_S46_DMR && - olen >= sizeof(struct dhcpv6_s46_dmr) && olen >= - sizeof(struct dhcpv6_s46_dmr) + dmr->dmr_prefix6_len) { - struct in6_addr in6 = IN6ADDR_ANY_INIT; - memcpy(&in6, dmr->dmr_ipv6_prefix, dmr->dmr_prefix6_len); - char buf6[INET6_ADDRSTRLEN]; - inet_ntop(AF_INET6, &in6, buf6, sizeof(buf6)); - fprintf(fp, "dmr=%s/%d,", buf6, dmr->dmr_prefix6_len); + s46_to_env_portparams(&bind->bind_ipv6_prefix[prefix6len], + olen - sizeof(*bind) - prefix6len, fp); + + dhcpv6_for_each_option(data, &data[len], otype, olen, odata) { + if (otype == DHCPV6_OPT_S46_BR && olen == sizeof(struct in6_addr)) { + inet_ntop(AF_INET6, odata, buf6, sizeof(buf6)); + fprintf(fp, "br=%s,", buf6); + } + } } fputc(' ', fp); } -#else - if (data && len) {} -#endif fclose(fp); putenv(str); } -static void script_call_delayed(int signal __attribute__((unused))) -{ - if (delayed_call) - script_call((char*)delayed_call); -} - - -void script_delay_call(const char *status, int timeout) -{ - if (dont_delay) { - script_call(status); - } else if (!delayed_call) { - delayed_call = strdup(status); - signal(SIGALRM, script_call_delayed); - alarm(timeout); - } -} - - void script_call(const char *status) { size_t dns_len, search_len, custom_len, sntp_ip_len, ntp_ip_len, ntp_dns_len; - size_t sip_ip_len, sip_fqdn_len, aftr_name_len, cer_len; - size_t s46_mapt_len, s46_mape_len, s46_lw_len; + size_t sip_ip_len, sip_fqdn_len, aftr_name_len, cer_len, addr_len; + size_t s46_mapt_len, s46_mape_len, s46_lw_len, passthru_len; odhcp6c_expire(); - if (delayed_call) { - alarm(0); - dont_delay = true; - } + struct in6_addr *addr = odhcp6c_get_state(STATE_SERVER_ADDR, &addr_len); struct in6_addr *dns = odhcp6c_get_state(STATE_DNS, &dns_len); uint8_t *search = odhcp6c_get_state(STATE_SEARCH, &search_len); uint8_t *custom = odhcp6c_get_state(STATE_CUSTOM_OPTS, &custom_len); @@ -350,6 +352,7 @@ void script_call(const char *status) uint8_t *s46_mapt = odhcp6c_get_state(STATE_S46_MAPT, &s46_mapt_len); uint8_t *s46_mape = odhcp6c_get_state(STATE_S46_MAPE, &s46_mape_len); uint8_t *s46_lw = odhcp6c_get_state(STATE_S46_LW, &s46_lw_len); + uint8_t *passthru = odhcp6c_get_state(STATE_PASSTHRU, &passthru_len); size_t prefix_len, address_len, ra_pref_len, ra_route_len, ra_dns_len; uint8_t *prefix = odhcp6c_get_state(STATE_IA_PD, &prefix_len); @@ -360,6 +363,7 @@ void script_call(const char *status) // Don't set environment before forking, because env is leaky. if (fork() == 0) { + ipv6_to_env("SERVER", addr, addr_len / sizeof(*addr)); ipv6_to_env("RDNSS", dns, dns_len / sizeof(*dns)); ipv6_to_env("SNTP_IP", sntp, sntp_ip_len / sizeof(*sntp)); ipv6_to_env("NTP_IP", ntp, ntp_ip_len / sizeof(*ntp)); @@ -380,6 +384,11 @@ void script_call(const char *status) entry_to_env("RA_ROUTES", ra_route, ra_route_len, ENTRY_ROUTE); entry_to_env("RA_DNS", ra_dns, ra_dns_len, ENTRY_HOST); + char *buf = malloc(10 + passthru_len * 2); + strncpy(buf, "PASSTHRU=", 10); + script_hexlify(&buf[9], passthru, passthru_len); + putenv(buf); + argv[2] = (char*)status; execv(argv[0], argv); _exit(128);