36 int keep_capabilities = 0;
39 #ifdef HAVE_LIBSYSTEMD 40 #include <systemd/sd-daemon.h> 89 isc_boolean_t use_if_id = ISC_FALSE;
108 struct sockaddr_in
to;
113 struct stream_list *
next;
115 struct sockaddr_in6 link;
117 } *downstreams, *upstreams;
119 static struct stream_list *parse_downstream(
char *);
120 static struct stream_list *parse_upstream(
char *);
121 static void setup_streams(
void);
129 char *dhcrelay_sub_id = NULL;
133 unsigned int,
unsigned int,
struct iaddr,
138 static int find_interface_by_agent_option(
struct dhcp_packet *,
144 static const char copyright[] =
145 "Copyright 2004-2014 Internet Systems Consortium.";
146 static const char arr[] =
"All rights reserved.";
147 static const char message[] =
148 "Internet Systems Consortium DHCP Relay Agent";
149 static const char url[] =
150 "For info, please visit https://www.isc.org/software/dhcp/";
153 #define DHCRELAY_USAGE \ 154 "Usage: dhcrelay [-4] [-d] [-q] [-a] [-D]\n"\ 155 " [-A <length>] [-c <hops>] [-p <port>]\n" \ 156 " [-pf <pid-file>] [--no-pid]\n"\ 157 " [-m append|replace|forward|discard]\n" \ 158 " [-i interface0 [ ... -i interfaceN]\n" \ 159 " server0 [ ... serverN]\n\n" \ 160 " dhcrelay -6 [-d] [-q] [-I] [-c <hops>] [-p <port>]\n" \ 161 " [-pf <pid-file>] [--no-pid]\n" \ 162 " [-s <subscriber-id>]\n" \ 163 " -l lower0 [ ... -l lowerN]\n" \ 164 " -u upper0 [ ... -u upperN]\n" \ 165 " lower (client link): [address%%]interface[#index]\n" \ 166 " upper (server link): [address%%]interface" 168 #define DHCRELAY_USAGE \ 169 "Usage: dhcrelay [-d] [-q] [-a] [-D] [-A <length>] [-c <hops>] [-p <port>]\n" \ 170 " [-pf <pid-file>] [--no-pid]\n" \ 171 " [-m append|replace|forward|discard]\n" \ 172 " [-i interface0 [ ... -i interfaceN]\n" \ 173 " server0 [ ... serverN]\n\n" 176 static void usage() {
186 char *service_local = NULL, *service_remote = NULL;
187 u_int16_t port_local = 0, port_remote = 0;
192 struct stream_list *sl = NULL;
193 int local_family_set = 0;
199 fd = open(
"/dev/null", O_RDWR | O_CLOEXEC);
201 fd = open(
"/dev/null", O_RDWR | O_CLOEXEC);
203 fd = open(
"/dev/null", O_RDWR | O_CLOEXEC);
209 openlog(
"dhcrelay", LOG_NDELAY, LOG_DAEMON);
212 setlogmask(LOG_UPTO(LOG_INFO));
218 if (status != ISC_R_SUCCESS)
219 log_fatal(
"Can't initialize context: %s",
220 isc_result_totext(status));
224 if (status != ISC_R_SUCCESS)
226 isc_result_totext(status));
231 for (i = 1; i < argc; i++) {
232 if (!strcmp(argv[i],
"-4")) {
237 local_family_set = 1;
239 }
else if (!strcmp(argv[i],
"-6")) {
243 local_family_set = 1;
246 }
else if (!strcmp(argv[i],
"-d")) {
248 }
else if (!strcmp(argv[i],
"-q")) {
251 }
else if (!strcmp(argv[i],
"-p")) {
255 log_debug(
"binding to user-specified port %d",
257 }
else if (!strcmp(argv[i],
"-c")) {
261 hcount = atoi(argv[i]);
266 }
else if (!strcmp(argv[i],
"-i")) {
271 local_family_set = 1;
277 if (strlen(argv[i]) >=
sizeof(tmp->
name)) {
280 argv[i], (
long)strlen(argv[i]));
282 status = interface_allocate(&tmp,
MDL);
283 if (status != ISC_R_SUCCESS) {
286 isc_result_totext(status));
288 strcpy(tmp->
name, argv[i]);
290 interface_dereference(&tmp,
MDL);
291 }
else if (!strcmp(argv[i],
"-a")) {
296 local_family_set = 1;
300 }
else if (!strcmp(argv[i],
"-A")) {
305 local_family_set = 1;
315 "longest possible MTU\n",
317 }
else if (!strcmp(argv[i],
"-m")) {
322 local_family_set = 1;
327 if (!strcasecmp(argv[i],
"append")) {
329 }
else if (!strcasecmp(argv[i],
"replace")) {
331 }
else if (!strcasecmp(argv[i],
"forward")) {
333 }
else if (!strcasecmp(argv[i],
"discard")) {
337 }
else if (!strcmp(argv[i],
"-D")) {
342 local_family_set = 1;
347 }
else if (!strcmp(argv[i],
"-I")) {
351 local_family_set = 1;
353 use_if_id = ISC_TRUE;
354 }
else if (!strcmp(argv[i],
"-l")) {
358 local_family_set = 1;
360 if (downstreams != NULL)
361 use_if_id = ISC_TRUE;
364 sl = parse_downstream(argv[i]);
365 sl->next = downstreams;
367 }
else if (!strcmp(argv[i],
"-u")) {
371 local_family_set = 1;
375 sl = parse_upstream(argv[i]);
376 sl->next = upstreams;
378 }
else if (!strcmp(argv[i],
"-s")) {
382 local_family_set = 1;
386 dhcrelay_sub_id = argv[i];
388 }
else if (!strcmp(argv[i],
"-nc")) {
389 #ifdef HAVE_LIBCAP_NG 390 keep_capabilities = 1;
392 }
else if (!strcmp(argv[i],
"-pf")) {
397 }
else if (!strcmp(argv[i],
"--no-pid")) {
399 }
else if (!strcmp(argv[i],
"--version")) {
402 }
else if (!strcmp(argv[i],
"--help") ||
403 !strcmp(argv[i],
"-h")) {
406 }
else if (argv[i][0] ==
'-') {
410 struct in_addr ia, *iap = NULL;
416 local_family_set = 1;
419 if (inet_aton(argv[i], &ia)) {
422 he = gethostbyname(argv[i]);
426 iap = ((
struct in_addr *)
438 memcpy(&sp->
to.sin_addr, iap,
sizeof *iap);
462 #ifdef HAVE_LIBCAP_NG 464 if (!keep_capabilities) {
465 capng_clear(CAPNG_SELECT_BOTH);
466 capng_updatev(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
467 CAP_NET_RAW, CAP_NET_BIND_SERVICE, -1);
468 capng_apply(CAPNG_SELECT_BOTH);
469 log_info (
"Dropped all unnecessary capabilities.");
483 service_local =
"bootps";
484 service_remote =
"bootpc";
485 port_local = htons(67);
486 port_remote = htons(68);
490 service_local =
"dhcpv6-server";
491 service_remote =
"dhcpv6-client";
492 port_local = htons(547);
493 port_remote = htons(546);
498 ent = getservbyname(service_local,
"udp");
504 ent = getservbyname(service_remote,
"udp");
523 sp->
to.sin_family = AF_INET;
525 sp->
to.sin_len =
sizeof sp->
to;
534 if (upstreams == NULL || downstreams == NULL) {
535 log_info(
"Must specify at least one lower " 536 "and one upper interface.\n");
545 if (!option_code_hash_lookup(&requested_opts[0],
548 log_fatal(
"Unable to find the RELAY_MSG " 549 "option definition.");
551 if (!option_code_hash_lookup(&requested_opts[1],
554 log_fatal(
"Unable to find the INTERFACE_ID " 555 "option definition.");
560 gettimeofday(&
cur_tv, NULL);
578 if ((pid = fork()) < 0)
585 O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
591 pf = fdopen(pfdesc,
"we");
596 fprintf(pf,
"%ld\n",(
long)getpid());
622 #ifdef HAVE_LIBCAP_NG 624 if (!keep_capabilities) {
625 capng_clear(CAPNG_SELECT_BOTH);
626 capng_apply(CAPNG_SELECT_BOTH);
627 log_info (
"Dropped all capabilities.");
631 #ifdef HAVE_LIBSYSTEMD 633 sd_notifyf(0,
"READY=1\n" 634 "STATUS=Dispatching packets...\n" 636 (
unsigned long) getpid());
648 unsigned int length,
unsigned int from_port,
struct iaddr from,
651 struct sockaddr_in to;
656 log_info(
"Discarding packet with invalid hlen, received on " 657 "%s interface.", ip->
name);
661 log_info(
"Discarding packet received on %s interface that " 662 "has no IPv4 address assigned.", ip->
name);
668 if (packet->
giaddr.s_addr) {
691 to.sin_addr = packet->
yiaddr;
697 to.sin_addr.s_addr = htonl(INADDR_BROADCAST);
703 to.sin_family = AF_INET;
705 to.sin_len =
sizeof to;
717 strip_relay_agent_options(ip, &out, packet, length)))
721 log_error(
"Packet to bogus giaddr %s.\n",
722 inet_ntoa(packet->
giaddr));
731 log_debug(
"Forwarded BOOTREPLY for %s to %s",
734 inet_ntoa(to.sin_addr));
748 if (!(length = add_relay_agent_options(ip, packet, length,
757 if (!packet->
giaddr.s_addr)
770 &sp->
to, NULL) < 0) {
773 log_debug(
"Forwarded BOOTREQUEST for %s to %s",
776 inet_ntoa(sp->
to.sin_addr));
793 u_int8_t *op, *nextop, *sp, *max;
794 int good_agent_option = 0;
807 max = ((u_int8_t *)packet) + length;
842 nextop = op + op[1] + 2;
846 status = find_interface_by_agent_option(packet,
852 good_agent_option = 1;
862 nextop = op + op[1] + 2;
867 memmove(sp, op, op[1] + 2);
887 if (!good_agent_option) {
895 length = sp -((u_int8_t *)packet);
920 find_interface_by_agent_option(
struct dhcp_packet *packet,
922 u_int8_t *buf,
int len) {
924 u_int8_t *circuit_id = 0;
925 unsigned circuit_id_len = 0;
932 i + buf[i + 1] + 2 > len) {
939 circuit_id = &buf[i + 2];
940 circuit_id_len = buf[i + 1];
941 i += circuit_id_len + 2;
963 !memcmp(ip->
circuit_id, circuit_id, circuit_id_len))
985 unsigned length,
struct in_addr
giaddr) {
986 int is_dhcp = 0, mms;
988 u_int8_t *op, *nextop, *sp, *max, *end_pad = NULL;
1003 sp = op = &packet->
options[4];
1018 if (end_pad == NULL)
1038 mms = ntohs(*(op + 2));
1041 max = ((u_int8_t *)packet) + mms;
1084 nextop = op + op[1] + 2;
1091 memmove(sp, op, op[1] + 2);
1109 if (end_pad != NULL)
1120 log_fatal(
"Circuit ID length %d out of range [1-255] on " 1126 log_fatal(
"Remote ID length %d out of range [1-255] " 1134 if ((optlen < 3) ||(optlen > 255))
1135 log_fatal(
"Total agent option length(%u) out of range " 1136 "[3 - 255] on %s\n", optlen, ip->
name);
1142 if (max - sp >= optlen + 3) {
1143 log_debug(
"Adding %d-byte relay agent option", optlen + 3);
1164 log_error(
"No room in packet (used %d of %d) " 1165 "for %d-byte relay agent option: omitted",
1166 (
int) (sp - ((u_int8_t *) packet)),
1167 (
int) (max - ((u_int8_t *) packet)),
1179 length = sp -((u_int8_t *)packet);
1194 static struct stream_list *
1195 parse_downstream(
char *arg) {
1196 struct stream_list *dp, *
up;
1198 char *ifname, *addr, *iid;
1199 isc_result_t status;
1202 (downstreams != NULL))
1203 log_fatal(
"No support for multiple interfaces.");
1206 ifname = strchr(arg,
'%');
1207 if (ifname == NULL) {
1214 iid = strchr(ifname,
'#');
1218 if (strlen(ifname) >=
sizeof(ifp->
name)) {
1219 log_error(
"Interface name '%s' too long", ifname);
1224 for (dp = downstreams; dp; dp = dp->next) {
1225 if (strcmp(ifname, dp->ifp->name) == 0)
1226 log_fatal(
"Down interface '%s' declared twice.",
1231 for (up = upstreams;
up; up = up->next) {
1232 if (strcmp(ifname, up->ifp->name) == 0) {
1233 log_info(
"Interface '%s' is both down and up.",
1242 status = interface_allocate(&ifp,
MDL);
1243 if (status != ISC_R_SUCCESS)
1245 arg, isc_result_totext(status));
1246 strcpy(ifp->
name, ifname);
1256 dp = (
struct stream_list *)
dmalloc(
sizeof(*dp),
MDL);
1266 if (addr && (inet_pton(AF_INET6, addr, &dp->link.sin6_addr) <= 0))
1267 log_fatal(
"Bad link address '%s'", addr);
1275 static struct stream_list *
1276 parse_upstream(
char *arg) {
1277 struct stream_list *
up, *dp;
1279 char *ifname, *addr;
1280 isc_result_t status;
1283 ifname = strchr(arg,
'%');
1284 if (ifname == NULL) {
1291 if (strlen(ifname) >=
sizeof(ifp->
name)) {
1292 log_fatal(
"Interface name '%s' too long", ifname);
1296 for (up = upstreams;
up; up = up->next) {
1297 if (strcmp(ifname, up->ifp->name) == 0) {
1302 for (dp = downstreams; dp; dp = dp->next) {
1303 if (strcmp(ifname, dp->ifp->name) == 0) {
1311 status = interface_allocate(&ifp,
MDL);
1312 if (status != ISC_R_SUCCESS)
1314 arg, isc_result_totext(status));
1315 strcpy(ifp->
name, ifname);
1325 up = (
struct stream_list *)
dmalloc(
sizeof(*up),
MDL);
1331 if (inet_pton(AF_INET6, addr, &up->link.sin6_addr) <= 0)
1341 setup_streams(
void) {
1342 struct stream_list *dp, *
up;
1344 isc_boolean_t link_is_set;
1346 for (dp = downstreams; dp; dp = dp->next) {
1348 if (dp->ifp->v6address_count == 0)
1349 log_fatal(
"Interface '%s' has no IPv6 addresses.",
1353 if (IN6_IS_ADDR_UNSPECIFIED(&dp->link.sin6_addr))
1354 link_is_set = ISC_FALSE;
1356 link_is_set = ISC_TRUE;
1357 for (i = 0; i < dp->ifp->v6address_count; i++) {
1358 if (IN6_IS_ADDR_LINKLOCAL(&dp->ifp->v6addresses[i]))
1362 if (!memcmp(&dp->ifp->v6addresses[i],
1363 &dp->link.sin6_addr,
1364 sizeof(dp->link.sin6_addr)))
1367 if (i == dp->ifp->v6address_count)
1368 log_fatal(
"Interface %s does not have global IPv6 " 1369 "address assigned.", dp->ifp->name);
1371 memcpy(&dp->link.sin6_addr,
1372 &dp->ifp->v6addresses[i],
1373 sizeof(dp->link.sin6_addr));
1377 dp->id = dp->ifp->index;
1380 for (up = upstreams;
up; up = up->next) {
1382 up->link.sin6_family = AF_INET6;
1384 up->link.sin6_len =
sizeof(up->link);
1387 if (up->ifp->v6address_count == 0)
1388 log_fatal(
"Interface '%s' has no IPv6 addresses.",
1396 static const int required_forw_opts[] = {
1407 process_up6(
struct packet *packet,
struct stream_list *dp) {
1408 char forw_data[65535];
1412 struct stream_list *
up;
1426 log_info(
"Relaying %s from %s port %d going up.",
1437 log_info(
"Discarding %s from %s port %d going up.",
1444 log_info(
"Unknown %d type from %s port %d going up.",
1465 if (!use_if_id && downstreams->next) {
1466 log_info(
"Shan't get back the interface.");
1482 log_fatal(
"No memory for upwards options.");
1491 }
else if (!downstreams->next) {
1492 if_id = downstreams->id;
1494 log_info(
"Don't know the interface.");
1500 NULL, (
unsigned char *) &if_id,
1511 if (dhcrelay_sub_id != NULL) {
1513 (
unsigned char *) dhcrelay_sub_id,
1514 strlen(dhcrelay_sub_id),
1525 NULL, (
unsigned char *) packet->
raw,
1535 sizeof(forw_data) - cursor,
1537 required_forw_opts, NULL);
1541 for (up = upstreams;
up; up = up->next) {
1543 (
size_t) cursor, &up->link);
1551 process_down6(
struct packet *packet) {
1552 struct stream_list *dp;
1557 struct sockaddr_in6 to;
1563 log_info(
"Discarding %s from %s port %d going down.",
1568 log_info(
"Unknown %d type from %s port %d going down.",
1576 memset(&relay_msg, 0,
sizeof(relay_msg));
1577 memset(&if_id, 0,
sizeof(if_id));
1578 memset(&to, 0,
sizeof(to));
1579 to.sin6_family = AF_INET6;
1581 to.sin6_len =
sizeof(
to);
1610 (if_id.
len !=
sizeof(
int))) {
1611 log_info(
"Can't evaluate interface-id.");
1614 memcpy(&if_index, if_id.
data,
sizeof(
int));
1615 for (dp = downstreams; dp; dp = dp->next) {
1616 if (dp->id == if_index)
1625 for (dp = downstreams; dp; dp = dp->next) {
1627 if (!memcmp(&dp->link.sin6_addr,
1629 sizeof(
struct in6_addr)))
1634 if (!dp && downstreams && !downstreams->next)
1637 log_info(
"Can't find the down interface.");
1655 log_info(
"Relaying %s to %s port %d down.",
1658 ntohs(to.sin6_port));
1670 log_info(
"Discarding %s to %s port %d down.",
1673 ntohs(to.sin6_port));
1677 log_info(
"Unknown %d type to %s port %d down.",
1680 ntohs(to.sin6_port));
1686 (
size_t) relay_msg.
len, &to);
1689 if (relay_msg.
data != NULL)
1691 if (if_id.
data != NULL)
1699 dhcpv6(
struct packet *packet) {
1700 struct stream_list *dp;
1704 process_down6(packet);
1708 for (dp = downstreams; dp; dp = dp->next) {
1711 process_up6(packet, dp);
1716 process_up6(packet, NULL);
1720 log_info(
"Can't process packet from interface '%s'.",
1747 find_class(
struct class **
class,
const char *c1,
const char *c2,
int i) {
1748 return ISC_R_NOTFOUND;
1760 return ISC_R_SUCCESS;
void do_packet6(struct interface_info *, const char *, int, int, const struct iaddr *, isc_boolean_t)
void(* dhcpv6_packet_handler)(struct interface_info *, const char *, int, int, const struct iaddr *, isc_boolean_t)
unsigned char peer_address[16]
#define DHO_DHCP_AGENT_OPTIONS
int drop_agent_mismatches
isc_boolean_t no_dhcrelay_pid
struct tree_cache * global_options[256]
struct binding_scope * global_scope
const char * piaddr(const struct iaddr addr)
void bootp(struct packet *packet)
unsigned char options[FLEXIBLE_ARRAY_MEMBER]
void(* bootp_packet_handler)(struct interface_info *, struct dhcp_packet *, unsigned, unsigned int, struct iaddr, struct hardware *)
int check_collection(struct packet *p, struct lease *l, struct collection *c)
void * dmalloc(unsigned, const char *, int)
char * print_hw_addr(int htype, const int hlen, const unsigned char *data) const
const char * dhcpv6_type_names[]
int int int log_debug(const char *,...) __attribute__((__format__(__printf__
#define DHCPV6_RECONFIGURE
const char * path_dhcrelay_pid
isc_result_t dhcp_set_control_state(control_object_state_t oldstate, control_object_state_t newstate)
#define DHCP_CONTEXT_PRE_DB
struct in_addr * addresses
void data_string_forget(struct data_string *data, const char *file, int line)
int log_error(const char *,...) __attribute__((__format__(__printf__
int dhcp_max_agent_option_packet_length
struct option_state * options
unsigned char dhcpv6_hop_count
unsigned char link_address[16]
unsigned char dhcpv6_msg_type
void log_fatal(const char *,...) __attribute__((__format__(__printf__
#define DHCPV6_RELAY_REPL
#define DHCPV6_LEASEQUERY
#define DHCP_CONTEXT_POST_DB
isc_boolean_t no_pid_file
struct option * requested_opts[2]
u_int16_t validate_port(char *port)
void dhcp_signal_handler(int signal)
struct server_list * next
struct interface_info * fallback_interface
int option_state_allocate(struct option_state **ptr, const char *file, int line)
isc_result_t dhcp_context_create(int flags, struct in_addr *local4, struct in6_addr *local6)
int evaluate_option_cache(struct data_string *result, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *cfg_options, struct binding_scope **scope, struct option_cache *oc, const char *file, int line)
enum @28 agent_relay_mode
#define _PATH_DHCRELAY6_PID
struct interface_info * interface
ssize_t send_packet6(struct interface_info *, const unsigned char *, size_t, struct sockaddr_in6 *)
#define D6O_SUBSCRIBER_ID
int parse_allow_deny(struct option_cache **oc, struct parse *p, int i)
ssize_t send_packet(struct interface_info *, struct packet *, struct dhcp_packet *, size_t, struct in_addr, struct sockaddr_in *, struct hardware *)
#define DHCPV6_RELAY_FORW
int save_option_buffer(struct universe *universe, struct option_state *options, struct buffer *bp, unsigned char *buffer, unsigned length, unsigned code, int terminatep)
struct option_cache * lookup_option(struct universe *universe, struct option_state *options, unsigned code)
int int log_info(const char *,...) __attribute__((__format__(__printf__
int server_packets_relayed
struct interface_info * interfaces
void interface_snorf(struct interface_info *tmp, int ir)
void dhcp(struct packet *packet)
#define DHO_DHCP_MAX_MESSAGE_SIZE
#define DHCPV6_LEASEQUERY_REPLY
int store_options6(char *buf, int buflen, struct option_state *opt_state, struct packet *packet, const int *required_opts, struct data_string *oro)
int quiet_interface_discovery
int option_state_dereference(struct option_state **ptr, const char *file, int line)
void initialize_common_option_spaces()
void dhcpv6(struct packet *)
const int dhcpv6_type_name_max
struct interface_info * next
struct universe dhcpv6_universe
void classify(struct packet *p, struct class *c)
int supports_multiple_interfaces(struct interface_info *)
isc_result_t interface_setup()
u_int8_t hbuf[HARDWARE_ADDR_LEN+1]
#define INTERFACE_UPSTREAM
struct server_list * servers
#define INTERFACE_DOWNSTREAM
option_code_hash_t * code_hash
#define _PATH_DHCRELAY_PID
struct in6_addr dhcpv6_peer_address
int can_unicast_without_arp(struct interface_info *)
const unsigned char * data
#define DHO_DHCP_MESSAGE_TYPE
int corrupt_agent_options
#define DHCPV6_INFORMATION_REQUEST
struct in6_addr dhcpv6_link_address
int main(int argc, char **argv)
void discover_interfaces(int state)
#define DHCP_OPTIONS_COOKIE
#define INTERFACE_REQUESTED
int client_packets_relayed
isc_result_t omapi_init(void)
unsigned char options[DHCP_MAX_OPTION_LEN]
isc_result_t find_class(struct class **class, const char *c1, const char *c2, int i)