struct iphdr iph;
} __packed;
+
+enum {
+ DHCP_OPTION_ROUTER = 0x03,
+ DHCP_OPTION_ROUTES = 0x79,
+ DHCP_OPTION_END = 0xff,
+};
+
+struct dhcp_option {
+ uint8_t code;
+ uint8_t len;
+ uint8_t data[];
+};
+
struct dhcp_header {
uint8_t op, htype, hlen, hops;
uint32_t xit;
unsigned char chaddr[16];
unsigned char sname[64];
unsigned char file[128];
+ uint32_t cookie;
+ uint8_t option_data[];
} __packed;
static uint16_t
return sum;
}
+static void
+parse_dhcp_options(struct relayd_host *host, struct dhcp_header *dhcp, int len)
+{
+ uint8_t *end = (uint8_t *) dhcp + len;
+ struct dhcp_option *opt = (void *)dhcp->option_data;
+ static const uint8_t dest[4] = { 0, 0, 0, 0 };
+
+ while((uint8_t *) opt < end) {
+ if ((uint8_t *) opt + opt->len > end)
+ break;
+
+ opt = (void *) &opt->data[opt->len];
+ switch(opt->code) {
+ case DHCP_OPTION_ROUTER:
+ DPRINTF(2, "Found a DHCP router option, len=%d\n", opt->len);
+ if (!memcmp(opt->data, host->ipaddr, 4))
+ relayd_add_host_route(host, dest, 0);
+ else
+ relayd_add_pending_route(opt->data, dest, 0, 10000);
+ break;
+ case DHCP_OPTION_ROUTES:
+ DPRINTF(2, "Found a DHCP static routes option, len=%d\n", opt->len);
+ break;
+ case DHCP_OPTION_END:
+ opt = (void *) end;
+ continue;
+ default:
+ DPRINTF(3, "Skipping unknown DHCP option %02x\n", opt->code);
+ continue;
+ }
+
+ }
+}
+
bool relayd_handle_dhcp_packet(struct relayd_interface *rif, void *data, int len, bool forward)
{
struct ip_packet *pkt = data;
struct udphdr *udp;
struct dhcp_header *dhcp;
+ struct relayd_host *host;
int udplen;
uint16_t sum;
if (!forward)
return true;
- if (dhcp->op == 2)
- relayd_refresh_host(rif, pkt->eth.ether_shost, (void *) &pkt->iph.saddr);
+ if (dhcp->op == 2) {
+ host = relayd_refresh_host(rif, pkt->eth.ether_shost, (void *) &pkt->iph.saddr);
+ if (host)
+ parse_dhcp_options(host, dhcp, udplen - sizeof(struct udphdr));
+ }
DPRINTF(2, "%s: handling DHCP %s\n", rif->ifname, (dhcp->op == 1 ? "request" : "response"));
#include "relayd.h"
+static LIST_HEAD(pending_routes);
LIST_HEAD(interfaces);
int debug;
static int forward_bcast;
static int forward_dhcp;
+struct relayd_pending_route {
+ struct relayd_route rt;
+ struct uloop_timeout timeout;
+ uint8_t gateway[4];
+};
+
static struct relayd_host *find_host_by_ipaddr(struct relayd_interface *rif, const uint8_t *ipaddr)
{
struct relayd_host *host;
ioctl(inet_sock, SIOCSARP, &arp);
}
+static void timeout_host_route(struct uloop_timeout *timeout)
+{
+ struct relayd_pending_route *rt;
+
+ rt = container_of(timeout, struct relayd_pending_route, timeout);
+ list_del(&rt->rt.list);
+ free(rt);
+}
+
+void relayd_add_host_route(struct relayd_host *host, const uint8_t *dest, uint8_t mask)
+{
+ struct relayd_route *rt;
+
+ list_for_each_entry(rt, &host->routes, list) {
+ if (!memcmp(rt->dest, dest, sizeof(rt->dest)) && rt->mask == mask)
+ return;
+ }
+
+ rt = calloc(1, sizeof(*rt));
+ if (!rt)
+ return;
+
+ list_add(&rt->list, &host->routes);
+ memcpy(rt->dest, dest, sizeof(rt->dest));
+ rt->mask = mask;
+ relayd_add_route(host, rt);
+}
+
static void del_host(struct relayd_host *host)
{
+ struct relayd_route *route, *tmp;
+
DPRINTF(1, "%s: deleting host "IP_FMT" ("MAC_FMT")\n", host->rif->ifname,
IP_BUF(host->ipaddr), MAC_BUF(host->lladdr));
+ list_for_each_entry_safe(route, tmp, &host->routes, list) {
+ relayd_del_route(host, route);
+ list_del(&route->list);
+ free(route);
+ }
if (host->rif->managed)
- relayd_del_route(host);
+ relayd_del_route(host, NULL);
uloop_timeout_cancel(&host->timeout);
list_del(&host->list);
free(host);
}
static void fill_arp_request(struct arp_packet *pkt, struct relayd_interface *rif,
- uint8_t spa[4], uint8_t tpa[4])
+ const uint8_t spa[4], const uint8_t tpa[4])
{
memset(pkt, 0, sizeof(*pkt));
pkt->arp.arp_pln = 4;
}
-static void send_arp_request(struct relayd_host *host)
+static void send_arp_request(struct relayd_interface *rif, const uint8_t *ipaddr)
{
- struct relayd_interface *rif = host->rif;
struct arp_packet pkt;
- fill_arp_request(&pkt, host->rif, host->rif->src_ip, host->ipaddr);
+ fill_arp_request(&pkt, rif, rif->src_ip, ipaddr);
pkt.arp.arp_op = htons(ARPOP_REQUEST);
memcpy(pkt.arp.arp_spa, rif->src_ip, ETH_ALEN);
(struct sockaddr *) &rif->sll, sizeof(rif->sll));
}
+void relayd_add_pending_route(const uint8_t *gateway, const uint8_t *dest, uint8_t mask, int timeout)
+{
+ struct relayd_pending_route *rt;
+ struct relayd_interface *rif;
+ struct relayd_host *host;
+
+ host = find_host_by_ipaddr(NULL, gateway);
+ if (host) {
+ relayd_add_host_route(host, dest, mask);
+ return;
+ }
+
+ rt = calloc(1, sizeof(*rt));
+ if (!rt)
+ return;
+
+ memcpy(rt->gateway, gateway, sizeof(rt->gateway));
+ memcpy(rt->rt.dest, dest, sizeof(rt->rt.dest));
+ rt->rt.mask = mask;
+ list_add(&rt->rt.list, &pending_routes);
+ if (timeout <= 0)
+ return;
+
+ rt->timeout.cb = timeout_host_route;
+ uloop_timeout_set(&rt->timeout, 10000);
+ list_for_each_entry(rif, &interfaces, list) {
+ send_arp_request(rif, gateway);
+ }
+}
+
static void send_arp_reply(struct relayd_interface *rif, uint8_t spa[4],
uint8_t tha[ETH_ALEN], uint8_t tpa[4])
{
* giving up on it.
*/
if (host->rif->managed && host->cleanup_pending < 2) {
- send_arp_request(host);
+ send_arp_request(host->rif, host->ipaddr);
host->cleanup_pending++;
uloop_timeout_set(&host->timeout, 1000);
return;
static struct relayd_host *add_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr)
{
struct relayd_host *host;
+ struct relayd_pending_route *route, *rtmp;
DPRINTF(1, "%s: adding host "IP_FMT" ("MAC_FMT")\n", rif->ifname,
IP_BUF(ipaddr), MAC_BUF(lladdr));
host = calloc(1, sizeof(*host));
+ INIT_LIST_HEAD(&host->routes);
host->rif = rif;
memcpy(host->ipaddr, ipaddr, sizeof(host->ipaddr));
memcpy(host->lladdr, lladdr, sizeof(host->lladdr));
add_arp(host);
if (rif->managed)
- relayd_add_route(host);
+ relayd_add_route(host, NULL);
+
+ list_for_each_entry_safe(route, rtmp, &pending_routes, rt.list) {
+ if (memcmp(route->gateway, ipaddr, 4) != 0)
+ continue;
+
+ relayd_add_host_route(host, route->rt.dest, route->rt.mask);
+ if (!route->timeout.pending)
+ continue;
+
+ uloop_timeout_cancel(&route->timeout);
+ list_del(&route->rt.list);
+ free(route);
+ }
return host;
}
* If the old entry is behind a managed interface, it will be pinged
* before we expire it
*/
- if (host && !host->cleanup_pending)
+ if (host && !host->cleanup_pending) {
uloop_timeout_set(&host->timeout, 1);
+ return NULL;
+ }
host = add_host(rif, lladdr, ipaddr);
} else {
MAC_BUF(pkt->eth.ether_shost),
IP_BUF(pkt->arp.arp_tpa));
- relayd_refresh_host(rif, pkt->arp.arp_sha, pkt->arp.arp_spa);
+ if (memcmp(pkt->arp.arp_sha, rif->sll.sll_addr, ETH_ALEN) != 0)
+ relayd_refresh_host(rif, pkt->arp.arp_sha, pkt->arp.arp_spa);
if (!memcmp(pkt->arp.arp_tpa, rif->src_ip, 4))
return;
return 0;
}
+static void ping_static_routes(void)
+{
+ struct relayd_pending_route *rt;
+ struct relayd_interface *rif;
+
+ list_for_each_entry(rt, &pending_routes, rt.list)
+ list_for_each_entry(rif, &interfaces, list)
+ send_arp_request(rif, rt->gateway);
+}
+
static int init_interfaces(void)
{
struct relayd_interface *rif;
}
}
-static int alloc_interface(const char *ifname, bool managed)
+static struct relayd_interface *alloc_interface(const char *ifname, bool managed)
{
struct relayd_interface *rif;
if (strlen(ifname) >= IFNAMSIZ)
- return -1;
+ return NULL;
rif = calloc(1, sizeof(*rif));
if (!rif)
- return -1;
+ return NULL;
- INIT_LIST_HEAD(&rif->list);
INIT_LIST_HEAD(&rif->hosts);
strcpy(rif->ifname, ifname);
list_add(&rif->list, &interfaces);
rif->managed = managed;
- return 0;
+ return rif;
}
static void die(int signo)
" -i <ifname> Add an interface for relaying\n"
" -I <ifname> Same as -i, except with ARP cache and host route management\n"
" You need to specify at least two interfaces\n"
+ " -G <ip> Set a gateway IP for clients\n"
" -t <timeout> Host entry expiry timeout\n"
" -T <table> Set routing table number for automatically added routes\n"
" -B Enable broadcast forwarding\n"
int main(int argc, char **argv)
{
+ struct relayd_interface *rif = NULL;
+ struct in_addr addr;
bool managed;
int ifnum = 0;
int ch;
forward_bcast = 0;
uloop_init();
- while ((ch = getopt(argc, argv, "I:i:t:BDdT:")) != -1) {
+ while ((ch = getopt(argc, argv, "I:i:t:BDdT:G:")) != -1) {
switch(ch) {
case 'I':
managed = true;
/* fall through */
case 'i':
ifnum++;
- if (alloc_interface(optarg, managed) < 0)
+ rif = alloc_interface(optarg, managed);
+ if (!rif)
return 1;
managed = false;
if (route_table <= 0)
return usage(argv[0]);
break;
+ case 'G':
+ if (!inet_aton(optarg, &addr)) {
+ fprintf(stderr, "Address '%s' not found\n", optarg);
+ return 1;
+ }
+ relayd_add_pending_route((uint8_t *) &addr.s_addr, (const uint8_t *) "\x00\x00\x00\x00", 0, 0);
+ break;
case '?':
default:
return usage(argv[0]);
if (init_interfaces() < 0)
return 1;
+ ping_static_routes();
+
uloop_run();
uloop_done();
close(fd);
}
-static void rtnl_route_request(struct relayd_interface *rif, struct relayd_host *host, bool add)
+static void
+rtnl_route_request(struct relayd_interface *rif, struct relayd_host *host,
+ struct relayd_route *route, bool add)
{
static struct {
struct nlmsghdr nl;
struct rtattr rta;
int ifindex;
} __packed dev;
+ struct {
+ struct rtattr rta;
+ uint8_t ipaddr[4];
+ } __packed gw;
} __packed req = {
- .nl = {
- .nlmsg_len = sizeof(req),
- },
.rt = {
.rtm_family = AF_INET,
.rtm_dst_len = 32,
.rta_len = sizeof(req.table),
},
.dst.rta = {
- .rta_type = RTA_DST,
.rta_len = sizeof(req.dst),
},
.dev.rta = {
.rta_type = RTA_OIF,
.rta_len = sizeof(req.dev),
},
+ .gw.rta = {
+ .rta_type = RTA_GATEWAY,
+ .rta_len = sizeof(req.gw),
+ },
};
+ int pktlen = sizeof(req);
- memcpy(req.dst.ipaddr, host->ipaddr, sizeof(req.dst.ipaddr));
req.dev.ifindex = host->rif->sll.sll_ifindex;
req.table.table = rif->rt_table;
req.nl.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
req.rt.rtm_protocol = RTPROT_BOOT;
- req.rt.rtm_scope = RT_SCOPE_LINK;
+ if (route) {
+ req.rt.rtm_scope = RT_SCOPE_UNIVERSE;
+ } else {
+ req.rt.rtm_scope = RT_SCOPE_LINK;
+ }
req.rt.rtm_type = RTN_UNICAST;
} else {
req.nl.nlmsg_type = RTM_DELROUTE;
req.rt.rtm_scope = RT_SCOPE_NOWHERE;
}
- send(rtnl_sock.fd, &req, sizeof(req), 0);
+ req.dst.rta.rta_type = RTA_DST;
+ if (route) {
+ DPRINTF(2, "%s: add route to "IP_FMT"/%d via "IP_FMT"\n",
+ host->rif->ifname, IP_BUF(route->dest), route->mask, IP_BUF(host->ipaddr));
+ if (!route->mask) {
+ /* No DST -> reuse the DST attribute as the gateway attribute */
+ memcpy(req.dst.ipaddr, host->ipaddr, sizeof(req.dst.ipaddr));
+ req.dst.rta.rta_type = RTA_GATEWAY;
+ pktlen -= sizeof(req.gw);
+ req.rt.rtm_dst_len = 0;
+ } else {
+ memcpy(req.gw.ipaddr, host->ipaddr, sizeof(req.gw.ipaddr));
+ memcpy(req.dst.ipaddr, route->dest, sizeof(req.dst.ipaddr));
+ req.rt.rtm_dst_len = route->mask;
+ }
+ } else {
+ pktlen -= sizeof(req.gw);
+ req.rt.rtm_dst_len = 32;
+ memcpy(req.dst.ipaddr, host->ipaddr, sizeof(req.dst.ipaddr));
+ }
+
+ req.nl.nlmsg_len = pktlen;
+ send(rtnl_sock.fd, &req, pktlen, 0);
rtnl_flush();
}
rtnl_flush();
}
-void rtnl_route_set(struct relayd_host *host, bool add)
+void
+rtnl_route_set(struct relayd_host *host, struct relayd_route *route, bool add)
{
struct relayd_interface *rif;
if (rif == host->rif)
continue;
- rtnl_route_request(rif, host, add);
+ rtnl_route_request(rif, host, route, add);
}
}