ndhc/ndhc/ifchange.c

332 lines
11 KiB
C
Raw Normal View History

/* ifchange.c - functions to call the interface change daemon
2010-11-12 14:32:18 +05:30
*
* Copyright (c) 2004-2014 Nicholas J. Kain <njkain at gmail dot com>
* All rights reserved.
2010-11-12 14:32:18 +05:30
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
2010-11-12 14:32:18 +05:30
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
2010-11-12 14:32:18 +05:30
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
2010-11-12 14:32:18 +05:30
*/
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <errno.h>
#include <limits.h>
2010-11-12 14:32:18 +05:30
#include "options.h"
#include "ndhc.h"
2011-07-02 13:21:44 +05:30
#include "dhcp.h"
2010-11-12 14:32:18 +05:30
#include "options.h"
#include "arp.h"
2010-11-12 14:32:18 +05:30
#include "log.h"
#include "io.h"
#include "strl.h"
2010-12-24 20:42:41 +05:30
#include "ifchange.h"
2010-11-12 14:32:18 +05:30
static struct dhcpmsg cfg_packet; // Copy of the current configuration packet.
static int ifcmd_raw(char *buf, size_t buflen, char *optname,
char *optdata, ssize_t optlen)
{
if (!optdata)
return -1;
if (buflen < strlen(optname) + optlen + 3)
return -1;
if (optlen > INT_MAX || optlen < 0)
return -1;
int ioptlen = (int)optlen;
ssize_t olen = snprintf(buf, buflen, "%s:%.*s;",
optname, ioptlen, optdata);
if (olen < 0 || (size_t)olen >= buflen) {
log_warning("%s: (%s) '%s' option would truncate, so it was dropped.",
client_config.interface, __func__, optname);
memset(buf, 0, buflen);
return -1;
}
return olen;
}
2014-03-19 16:04:29 +05:30
static int ifcmd_bytes(char *buf, size_t buflen, char *optname,
uint8_t *optdata, ssize_t optlen)
{
return ifcmd_raw(buf, buflen, optname, (char *)optdata, optlen);
}
2014-03-19 16:04:29 +05:30
static int ifcmd_u8(char *buf, size_t buflen, char *optname,
uint8_t *optdata, ssize_t optlen)
2010-11-12 14:32:18 +05:30
{
if (!optdata || optlen < 1)
return -1;
char numbuf[16];
uint8_t c = optdata[0];
ssize_t olen = snprintf(numbuf, sizeof numbuf, "%c", c);
if (olen < 0 || (size_t)olen >= sizeof numbuf)
return -1;
return ifcmd_raw(buf, buflen, optname, numbuf, strlen(numbuf));
}
2014-03-19 16:04:29 +05:30
static int ifcmd_u16(char *buf, size_t buflen, char *optname,
uint8_t *optdata, ssize_t optlen)
{
if (!optdata || optlen < 2)
return -1;
char numbuf[16];
uint16_t v;
memcpy(&v, optdata, 2);
v = ntohs(v);
ssize_t olen = snprintf(numbuf, sizeof numbuf, "%hu", v);
if (olen < 0 || (size_t)olen >= sizeof numbuf)
return -1;
return ifcmd_raw(buf, buflen, optname, numbuf, strlen(numbuf));
}
2014-03-19 16:04:29 +05:30
static int ifcmd_s32(char *buf, size_t buflen, char *optname,
uint8_t *optdata, ssize_t optlen)
{
if (!optdata || optlen < 4)
return -1;
char numbuf[16];
int32_t v;
memcpy(&v, optdata, 4);
v = ntohl(v);
ssize_t olen = snprintf(numbuf, sizeof numbuf, "%d", v);
if (olen < 0 || (size_t)olen >= sizeof numbuf)
return -1;
return ifcmd_raw(buf, buflen, optname, numbuf, strlen(numbuf));
}
2014-03-19 16:04:29 +05:30
static int ifcmd_ip(char *buf, size_t buflen, char *optname,
uint8_t *optdata, ssize_t optlen)
{
if (!optdata || optlen < 4)
return -1;
char ipbuf[INET_ADDRSTRLEN];
if (!inet_ntop(AF_INET, optdata, ipbuf, sizeof ipbuf)) {
log_warning("%s: (%s) inet_ntop failed: %s",
client_config.interface, __func__, strerror(errno));
return -1;
}
return ifcmd_raw(buf, buflen, optname, ipbuf, strlen(ipbuf));
}
2014-03-19 16:04:29 +05:30
static int ifcmd_iplist(char *out, size_t outlen, char *optname,
uint8_t *optdata, ssize_t optlen)
{
char buf[2048];
char ipbuf[INET_ADDRSTRLEN];
size_t bufoff = 0;
size_t optoff = 0;
if (!optdata || optlen < 4)
return -1;
if (!inet_ntop(AF_INET, optdata + optoff, ipbuf, sizeof ipbuf)) {
log_warning("%s: (%s) inet_ntop failed: %s",
client_config.interface, __func__, strerror(errno));
return -1;
}
ssize_t wc = snprintf(buf + bufoff, sizeof buf, "%s:%s", optname, ipbuf);
if (wc < 0 || (size_t)wc >= sizeof buf)
return -1;
optoff += 4;
bufoff += wc;
while (optlen - optoff >= 4) {
if (!inet_ntop(AF_INET, optdata + optoff, ipbuf, sizeof ipbuf)) {
log_warning("%s: (%s) inet_ntop failed: %s",
client_config.interface, __func__, strerror(errno));
return -1;
}
wc = snprintf(buf + bufoff, sizeof buf, ",%s", ipbuf);
if (wc < 0 || (size_t)wc >= sizeof buf)
return -1;
optoff += 4;
bufoff += wc;
}
wc = snprintf(buf + bufoff, sizeof buf, ";");
if (wc < 0 || (size_t)wc >= sizeof buf)
return -1;
return ifcmd_raw(out, outlen, optname, buf, strlen(buf));
}
static int ifchd_cmd(char *b, size_t bl, uint8_t *od, ssize_t ol, uint8_t code)
{
switch (code) {
case DCODE_ROUTER: return ifcmd_ip(b, bl, "routr", od, ol);
case DCODE_DNS: return ifcmd_iplist(b, bl, "dns", od, ol);
case DCODE_LPRSVR: return ifcmd_iplist(b, bl, "lpr", od, ol);
case DCODE_NTPSVR: return ifcmd_iplist(b, bl, "ntp", od, ol);
case DCODE_WINS: return ifcmd_iplist(b, bl, "wins", od, ol);
case DCODE_HOSTNAME: return ifcmd_bytes(b, bl, "host", od, ol);
case DCODE_DOMAIN: return ifcmd_bytes(b, bl, "dom", od, ol);
case DCODE_TIMEZONE: return ifcmd_s32(b, bl, "tzone", od, ol);
case DCODE_MTU: return ifcmd_u16(b, bl, "mtu", od, ol);
case DCODE_IPTTL: return ifcmd_u8(b, bl, "ipttl", od, ol);
default: break;
}
log_line("Invalid option code (%c) for ifchd cmd.", code);
return -1;
2010-11-12 14:32:18 +05:30
}
static void pipewrite(struct client_state_t *cs, const char *buf, size_t count)
2010-11-12 14:32:18 +05:30
{
cs->ifchWorking = 1;
if (safe_write(pToIfchW, buf, count) == -1) {
log_error("pipewrite: write failed: %s", strerror(errno));
return;
}
log_line("Sent to ifchd: %s", buf);
}
2010-11-12 14:32:18 +05:30
void ifchange_deconfig(struct client_state_t *cs)
2010-11-12 14:32:18 +05:30
{
char buf[256];
if (cs->ifDeconfig)
return;
cs->ifDeconfig = 1;
snprintf(buf, sizeof buf, "ip4:0.0.0.0,255.255.255.255;");
log_line("Resetting %s IP configuration.", client_config.interface);
pipewrite(cs, buf, strlen(buf));
memset(&cfg_packet, 0, sizeof cfg_packet);
2010-11-12 14:32:18 +05:30
}
static size_t send_client_ip(char *out, size_t olen, struct dhcpmsg *packet)
{
uint8_t optdata[MAX_DOPT_SIZE], olddata[MAX_DOPT_SIZE];
char ip[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN], bc[INET_ADDRSTRLEN];
ssize_t optlen, oldlen;
bool change_ipaddr = false;
bool have_subnet = false;
bool change_subnet = false;
bool have_bcast = false;
bool change_bcast = false;
if (memcmp(&packet->yiaddr, &cfg_packet.yiaddr, sizeof packet->yiaddr))
change_ipaddr = true;
if (!inet_ntop(AF_INET, &packet->yiaddr, ip, sizeof ip)) {
log_warning("%s: (%s) inet_ntop for ip failed: %s",
client_config.interface, __func__, strerror(errno));
return 0;
}
optlen = get_dhcp_opt(packet, DCODE_SUBNET, optdata, sizeof optdata);
if (optlen >= 4) {
have_subnet = true;
if (!inet_ntop(AF_INET, optdata, sn, sizeof sn)) {
log_warning("%s: (%s) inet_ntop for subnet failed: %s",
client_config.interface, __func__, strerror(errno));
return 0;
}
oldlen = get_dhcp_opt(&cfg_packet, DCODE_SUBNET, olddata,
sizeof olddata);
if (oldlen != optlen || memcmp(optdata, olddata, optlen))
change_subnet = true;
}
optlen = get_dhcp_opt(packet, DCODE_BROADCAST, optdata, sizeof optdata);
if (optlen >= 4) {
have_bcast = true;
if (!inet_ntop(AF_INET, optdata, bc, sizeof bc)) {
log_warning("%s: (%s) inet_ntop for broadcast failed: %s",
client_config.interface, __func__, strerror(errno));
return 0;
}
oldlen = get_dhcp_opt(&cfg_packet, DCODE_BROADCAST, olddata,
sizeof olddata);
if (oldlen != optlen || memcmp(optdata, olddata, optlen))
change_bcast = true;
}
// Nothing to change.
if (!change_ipaddr && !change_subnet && !change_bcast)
return 0;
if (!have_subnet) {
static char snClassC[] = "255.255.255.0";
log_line("Server did not send a subnet mask. Assuming class C (255.255.255.0).");
memcpy(sn, snClassC, sizeof snClassC);
}
int snlen;
if (have_bcast) {
snlen = snprintf(out, olen, "ip4:%s,%s,%s;", ip, sn, bc);
} else {
snlen = snprintf(out, olen, "ip4:%s,%s;", ip, sn);
}
if (snlen < 0 || (size_t)snlen >= olen) {
log_warning("%s: (%s) ip4 command would truncate so it was dropped.",
client_config.interface, __func__);
memset(out, 0, olen);
return 0;
}
return snlen;
}
static size_t send_cmd(char *out, size_t olen, struct dhcpmsg *packet,
uint8_t code)
{
uint8_t optdata[MAX_DOPT_SIZE], olddata[MAX_DOPT_SIZE];
ssize_t optlen, oldlen;
if (!packet)
return 0;
optlen = get_dhcp_opt(packet, code, optdata, sizeof optdata);
if (!optlen)
return 0;
oldlen = get_dhcp_opt(&cfg_packet, code, olddata, sizeof olddata);
if (oldlen == optlen && !memcmp(optdata, olddata, optlen))
return 0;
int r = ifchd_cmd(out, olen, optdata, optlen, code);
return r > 0 ? r : 0;
2010-11-12 14:32:18 +05:30
}
void ifchange_bind(struct client_state_t *cs, struct dhcpmsg *packet)
2010-11-12 14:32:18 +05:30
{
char buf[2048];
size_t bo;
if (!packet)
return;
memset(buf, 0, sizeof buf);
bo = send_client_ip(buf, sizeof buf, packet);
bo += send_cmd(buf + bo, sizeof buf - bo, packet, DCODE_ROUTER);
bo += send_cmd(buf + bo, sizeof buf - bo, packet, DCODE_DNS);
bo += send_cmd(buf + bo, sizeof buf - bo, packet, DCODE_HOSTNAME);
bo += send_cmd(buf + bo, sizeof buf - bo, packet, DCODE_DOMAIN);
bo += send_cmd(buf + bo, sizeof buf - bo, packet, DCODE_MTU);
bo += send_cmd(buf + bo, sizeof buf - bo, packet, DCODE_WINS);
if (bo)
pipewrite(cs, buf, bo);
cs->ifDeconfig = 0;
memcpy(&cfg_packet, packet, sizeof cfg_packet);
2010-11-12 14:32:18 +05:30
}