3 * DHCP Server library with GLib integration
5 * Copyright (C) 2009-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
32 #include <arpa/inet.h>
34 #include <netpacket/packet.h>
35 #include <net/ethernet.h>
36 #include <net/if_arp.h>
39 #include <linux/filter.h>
46 #define DEFAULT_DHCP_LEASE_SEC (8*60*60)
49 #define OFFER_TIME (5*60)
59 uint32_t server_nip; /* our address in network byte order */
60 uint32_t lease_seconds;
63 GIOChannel *listener_channel;
65 GHashTable *nip_lease_hash;
66 GHashTable *option_hash; /* Options send to client */
67 GDHCPSaveLeaseFunc save_lease_func;
68 GDHCPLeaseAddedCb lease_added_cb;
69 GDHCPDebugFunc debug_func;
76 uint8_t lease_mac[ETH_ALEN];
79 static inline void debug(GDHCPServer *server, const char *format, ...)
84 if (!server->debug_func)
89 if (vsnprintf(str, sizeof(str), format, ap) > 0)
90 server->debug_func(str, server->debug_data);
95 static struct dhcp_lease *find_lease_by_mac(GDHCPServer *dhcp_server,
100 for (list = dhcp_server->lease_list; list; list = list->next) {
101 struct dhcp_lease *lease = list->data;
103 if (memcmp(lease->lease_mac, mac, ETH_ALEN) == 0)
110 static void remove_lease(GDHCPServer *dhcp_server, struct dhcp_lease *lease)
112 dhcp_server->lease_list =
113 g_list_remove(dhcp_server->lease_list, lease);
115 g_hash_table_remove(dhcp_server->nip_lease_hash,
116 GINT_TO_POINTER((int) lease->lease_nip));
120 /* Clear the old lease and create the new one */
121 static int get_lease(GDHCPServer *dhcp_server, uint32_t yiaddr,
122 const uint8_t *mac, struct dhcp_lease **lease)
124 struct dhcp_lease *lease_nip, *lease_mac;
129 if (ntohl(yiaddr) < dhcp_server->start_ip)
132 if (ntohl(yiaddr) > dhcp_server->end_ip)
135 if (memcmp(mac, MAC_BCAST_ADDR, ETH_ALEN) == 0)
138 if (memcmp(mac, MAC_ANY_ADDR, ETH_ALEN) == 0)
141 lease_mac = find_lease_by_mac(dhcp_server, mac);
143 lease_nip = g_hash_table_lookup(dhcp_server->nip_lease_hash,
144 GINT_TO_POINTER((int) ntohl(yiaddr)));
145 debug(dhcp_server, "lease_mac %p lease_nip %p", lease_mac, lease_nip);
148 dhcp_server->lease_list =
149 g_list_remove(dhcp_server->lease_list,
151 g_hash_table_remove(dhcp_server->nip_lease_hash,
152 GINT_TO_POINTER((int) ntohl(yiaddr)));
156 else if (lease_nip != lease_mac) {
157 remove_lease(dhcp_server, lease_mac);
166 dhcp_server->lease_list =
167 g_list_remove(dhcp_server->lease_list,
169 g_hash_table_remove(dhcp_server->nip_lease_hash,
170 GINT_TO_POINTER((int) lease_mac->lease_nip));
176 *lease = g_try_new0(struct dhcp_lease, 1);
183 static gint compare_expire(gconstpointer a, gconstpointer b)
185 const struct dhcp_lease *lease1 = a;
186 const struct dhcp_lease *lease2 = b;
188 return lease2->expire - lease1->expire;
191 static struct dhcp_lease *add_lease(GDHCPServer *dhcp_server, uint32_t expire,
192 const uint8_t *chaddr, uint32_t yiaddr)
194 struct dhcp_lease *lease = NULL;
197 ret = get_lease(dhcp_server, yiaddr, chaddr, &lease);
201 memset(lease, 0, sizeof(*lease));
203 memcpy(lease->lease_mac, chaddr, ETH_ALEN);
204 lease->lease_nip = ntohl(yiaddr);
207 lease->expire = time(NULL) + dhcp_server->lease_seconds;
209 lease->expire = expire;
211 dhcp_server->lease_list = g_list_insert_sorted(dhcp_server->lease_list,
212 lease, compare_expire);
214 g_hash_table_insert(dhcp_server->nip_lease_hash,
215 GINT_TO_POINTER((int) lease->lease_nip), lease);
220 static struct dhcp_lease *find_lease_by_nip(GDHCPServer *dhcp_server,
223 return g_hash_table_lookup(dhcp_server->nip_lease_hash,
224 GINT_TO_POINTER((int) nip));
227 /* Check if the IP is taken; if it is, add it to the lease table */
228 static bool arp_check(uint32_t nip, const uint8_t *safe_mac)
230 /* TODO: Add ARP checking */
234 static bool is_expired_lease(struct dhcp_lease *lease)
236 if (lease->expire < time(NULL))
242 static uint32_t find_free_or_expired_nip(GDHCPServer *dhcp_server,
243 const uint8_t *safe_mac)
246 struct dhcp_lease *lease;
248 ip_addr = dhcp_server->start_ip;
249 for (; ip_addr <= dhcp_server->end_ip; ip_addr++) {
250 /* e.g. 192.168.55.0 */
251 if ((ip_addr & 0xff) == 0)
254 /* e.g. 192.168.55.255 */
255 if ((ip_addr & 0xff) == 0xff)
258 lease = find_lease_by_nip(dhcp_server, ip_addr);
262 if (arp_check(htonl(ip_addr), safe_mac))
266 /* The last lease is the oldest one */
267 list = g_list_last(dhcp_server->lease_list);
275 if (!is_expired_lease(lease))
278 if (!arp_check(lease->lease_nip, safe_mac))
281 return lease->lease_nip;
284 static void lease_set_expire(GDHCPServer *dhcp_server,
285 struct dhcp_lease *lease, uint32_t expire)
287 dhcp_server->lease_list = g_list_remove(dhcp_server->lease_list, lease);
289 lease->expire = expire;
291 dhcp_server->lease_list = g_list_insert_sorted(dhcp_server->lease_list,
292 lease, compare_expire);
295 static void destroy_lease_table(GDHCPServer *dhcp_server)
299 g_hash_table_destroy(dhcp_server->nip_lease_hash);
301 dhcp_server->nip_lease_hash = NULL;
303 for (list = dhcp_server->lease_list; list; list = list->next) {
304 struct dhcp_lease *lease = list->data;
309 g_list_free(dhcp_server->lease_list);
311 dhcp_server->lease_list = NULL;
313 static uint32_t get_interface_address(int index)
317 struct sockaddr_in *server_ip;
320 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
322 perror("Open socket error");
326 memset(&ifr, 0, sizeof(ifr));
327 ifr.ifr_ifindex = index;
329 err = ioctl(sk, SIOCGIFNAME, &ifr);
331 perror("Get interface name error");
335 err = ioctl(sk, SIOCGIFADDR, &ifr);
337 perror("Get ip address error");
341 server_ip = (struct sockaddr_in *) &ifr.ifr_addr;
342 ret = server_ip->sin_addr.s_addr;
350 GDHCPServer *g_dhcp_server_new(GDHCPType type,
351 int ifindex, GDHCPServerError *error)
353 GDHCPServer *dhcp_server = NULL;
356 *error = G_DHCP_SERVER_ERROR_INVALID_INDEX;
360 dhcp_server = g_try_new0(GDHCPServer, 1);
362 *error = G_DHCP_SERVER_ERROR_NOMEM;
366 dhcp_server->interface = get_interface_name(ifindex);
367 if (!dhcp_server->interface) {
368 *error = G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE;
372 if (!interface_is_up(ifindex)) {
373 *error = G_DHCP_SERVER_ERROR_INTERFACE_DOWN;
377 dhcp_server->server_nip = get_interface_address(ifindex);
378 if (dhcp_server->server_nip == 0) {
379 *error = G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID;
383 dhcp_server->nip_lease_hash = g_hash_table_new_full(g_direct_hash,
384 g_direct_equal, NULL, NULL);
385 dhcp_server->option_hash = g_hash_table_new_full(g_direct_hash,
386 g_direct_equal, NULL, NULL);
388 dhcp_server->started = FALSE;
390 /* All the leases have the same fixed lease time,
391 * do not support DHCP_LEASE_TIME option from client.
393 dhcp_server->lease_seconds = DEFAULT_DHCP_LEASE_SEC;
395 dhcp_server->type = type;
396 dhcp_server->ref_count = 1;
397 dhcp_server->ifindex = ifindex;
398 dhcp_server->listener_sockfd = -1;
399 dhcp_server->listener_watch = 0;
400 dhcp_server->listener_channel = NULL;
401 dhcp_server->save_lease_func = NULL;
402 dhcp_server->debug_func = NULL;
403 dhcp_server->debug_data = NULL;
405 *error = G_DHCP_SERVER_ERROR_NONE;
410 g_free(dhcp_server->interface);
416 static uint8_t check_packet_type(struct dhcp_packet *packet)
420 if (packet->hlen != ETH_ALEN)
423 if (packet->op != BOOTREQUEST)
426 type = dhcp_get_option(packet, DHCP_MESSAGE_TYPE);
431 if (*type < DHCP_MINTYPE)
434 if (*type > DHCP_MAXTYPE)
440 static void init_packet(GDHCPServer *dhcp_server, struct dhcp_packet *packet,
441 struct dhcp_packet *client_packet, char type)
443 /* Sets op, htype, hlen, cookie fields
444 * and adds DHCP_MESSAGE_TYPE option */
445 dhcp_init_header(packet, type);
447 packet->xid = client_packet->xid;
448 memcpy(packet->chaddr, client_packet->chaddr,
449 sizeof(client_packet->chaddr));
450 packet->flags = client_packet->flags;
451 packet->gateway_nip = client_packet->gateway_nip;
452 packet->ciaddr = client_packet->ciaddr;
453 dhcp_add_option_uint32(packet, DHCP_SERVER_ID,
454 ntohl(dhcp_server->server_nip));
457 static void add_option(gpointer key, gpointer value, gpointer user_data)
459 const char *option_value = value;
460 uint8_t option_code = GPOINTER_TO_INT(key);
462 struct dhcp_packet *packet = user_data;
467 switch (option_code) {
470 case G_DHCP_DNS_SERVER:
471 if (inet_aton(option_value, &nip) == 0)
474 dhcp_add_option_uint32(packet, (uint8_t) option_code,
482 static void add_server_options(GDHCPServer *dhcp_server,
483 struct dhcp_packet *packet)
485 g_hash_table_foreach(dhcp_server->option_hash,
489 static bool check_requested_nip(GDHCPServer *dhcp_server,
490 uint32_t requested_nip)
492 struct dhcp_lease *lease;
494 if (requested_nip == 0)
497 if (requested_nip < dhcp_server->start_ip)
500 if (requested_nip > dhcp_server->end_ip)
503 lease = find_lease_by_nip(dhcp_server, requested_nip);
507 if (!is_expired_lease(lease))
513 static void send_packet_to_client(GDHCPServer *dhcp_server,
514 struct dhcp_packet *dhcp_pkt)
516 const uint8_t *chaddr;
519 if ((dhcp_pkt->flags & htons(BROADCAST_FLAG))
520 || dhcp_pkt->ciaddr == 0) {
521 debug(dhcp_server, "Broadcasting packet to client");
522 ciaddr = INADDR_BROADCAST;
523 chaddr = MAC_BCAST_ADDR;
525 debug(dhcp_server, "Unicasting packet to client ciaddr");
526 ciaddr = dhcp_pkt->ciaddr;
527 chaddr = dhcp_pkt->chaddr;
530 dhcp_send_raw_packet(dhcp_pkt,
531 dhcp_server->server_nip, SERVER_PORT,
532 ciaddr, CLIENT_PORT, chaddr,
533 dhcp_server->ifindex, false);
536 static void send_offer(GDHCPServer *dhcp_server,
537 struct dhcp_packet *client_packet,
538 struct dhcp_lease *lease,
539 uint32_t requested_nip)
541 struct dhcp_packet packet;
544 init_packet(dhcp_server, &packet, client_packet, DHCPOFFER);
547 packet.yiaddr = htonl(lease->lease_nip);
548 else if (check_requested_nip(dhcp_server, requested_nip))
549 packet.yiaddr = htonl(requested_nip);
551 packet.yiaddr = htonl(find_free_or_expired_nip(
552 dhcp_server, client_packet->chaddr));
554 debug(dhcp_server, "find yiaddr %u", packet.yiaddr);
556 if (!packet.yiaddr) {
557 debug(dhcp_server, "Err: Can not found lease and send offer");
561 lease = add_lease(dhcp_server, OFFER_TIME,
562 packet.chaddr, packet.yiaddr);
565 "Err: No free IP addresses. OFFER abandoned");
569 dhcp_add_option_uint32(&packet, DHCP_LEASE_TIME,
570 dhcp_server->lease_seconds);
571 add_server_options(dhcp_server, &packet);
573 addr.s_addr = packet.yiaddr;
575 debug(dhcp_server, "Sending OFFER of %s", inet_ntoa(addr));
576 send_packet_to_client(dhcp_server, &packet);
579 static void save_lease(GDHCPServer *dhcp_server)
583 if (!dhcp_server->save_lease_func)
586 for (list = dhcp_server->lease_list; list; list = list->next) {
587 struct dhcp_lease *lease = list->data;
588 dhcp_server->save_lease_func(lease->lease_mac,
589 lease->lease_nip, lease->expire);
593 static void send_ACK(GDHCPServer *dhcp_server,
594 struct dhcp_packet *client_packet, uint32_t dest)
596 struct dhcp_packet packet;
597 uint32_t lease_time_sec;
600 init_packet(dhcp_server, &packet, client_packet, DHCPACK);
601 packet.yiaddr = htonl(dest);
603 lease_time_sec = dhcp_server->lease_seconds;
605 dhcp_add_option_uint32(&packet, DHCP_LEASE_TIME, lease_time_sec);
607 add_server_options(dhcp_server, &packet);
609 addr.s_addr = htonl(dest);
611 debug(dhcp_server, "Sending ACK to %s", inet_ntoa(addr));
613 send_packet_to_client(dhcp_server, &packet);
615 add_lease(dhcp_server, 0, packet.chaddr, packet.yiaddr);
617 if (dhcp_server->lease_added_cb)
618 dhcp_server->lease_added_cb(packet.chaddr, packet.yiaddr);
621 static void send_NAK(GDHCPServer *dhcp_server,
622 struct dhcp_packet *client_packet)
624 struct dhcp_packet packet;
626 init_packet(dhcp_server, &packet, client_packet, DHCPNAK);
628 debug(dhcp_server, "Sending NAK");
630 dhcp_send_raw_packet(&packet,
631 dhcp_server->server_nip, SERVER_PORT,
632 INADDR_BROADCAST, CLIENT_PORT, MAC_BCAST_ADDR,
633 dhcp_server->ifindex, false);
636 static void send_inform(GDHCPServer *dhcp_server,
637 struct dhcp_packet *client_packet)
639 struct dhcp_packet packet;
641 init_packet(dhcp_server, &packet, client_packet, DHCPACK);
642 add_server_options(dhcp_server, &packet);
643 send_packet_to_client(dhcp_server, &packet);
646 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
649 GDHCPServer *dhcp_server = user_data;
650 struct dhcp_packet packet;
651 struct dhcp_lease *lease;
652 uint32_t requested_nip = 0;
653 uint8_t type, *server_id_option, *request_ip_option;
656 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
657 dhcp_server->listener_watch = 0;
661 re = dhcp_recv_l3_packet(&packet, dhcp_server->listener_sockfd);
665 type = check_packet_type(&packet);
669 server_id_option = dhcp_get_option(&packet, DHCP_SERVER_ID);
670 if (server_id_option) {
671 uint32_t server_nid =
672 get_unaligned((const uint32_t *) server_id_option);
674 if (server_nid != dhcp_server->server_nip)
678 request_ip_option = dhcp_get_option(&packet, DHCP_REQUESTED_IP);
679 if (request_ip_option)
680 requested_nip = get_be32(request_ip_option);
682 lease = find_lease_by_mac(dhcp_server, packet.chaddr);
686 debug(dhcp_server, "Received DISCOVER");
688 send_offer(dhcp_server, &packet, lease, requested_nip);
691 debug(dhcp_server, "Received REQUEST NIP %d",
693 if (requested_nip == 0) {
694 requested_nip = ntohl(packet.ciaddr);
695 if (requested_nip == 0)
699 if (lease && requested_nip == lease->lease_nip) {
700 debug(dhcp_server, "Sending ACK");
701 send_ACK(dhcp_server, &packet,
706 if (server_id_option || !lease) {
707 debug(dhcp_server, "Sending NAK");
708 send_NAK(dhcp_server, &packet);
713 debug(dhcp_server, "Received DECLINE");
715 if (!server_id_option)
718 if (!request_ip_option)
724 if (requested_nip == lease->lease_nip)
725 remove_lease(dhcp_server, lease);
729 debug(dhcp_server, "Received RELEASE");
731 if (!server_id_option)
737 if (packet.ciaddr == lease->lease_nip)
738 lease_set_expire(dhcp_server, lease,
742 debug(dhcp_server, "Received INFORM");
743 send_inform(dhcp_server, &packet);
750 /* Caller need to load leases before call it */
751 int g_dhcp_server_start(GDHCPServer *dhcp_server)
753 GIOChannel *listener_channel;
756 if (dhcp_server->started)
759 listener_sockfd = dhcp_l3_socket(SERVER_PORT,
760 dhcp_server->interface, AF_INET);
761 if (listener_sockfd < 0)
764 listener_channel = g_io_channel_unix_new(listener_sockfd);
765 if (!listener_channel) {
766 close(listener_sockfd);
770 dhcp_server->listener_sockfd = listener_sockfd;
771 dhcp_server->listener_channel = listener_channel;
773 g_io_channel_set_close_on_unref(listener_channel, TRUE);
774 dhcp_server->listener_watch =
775 g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
776 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
777 listener_event, dhcp_server,
779 g_io_channel_unref(dhcp_server->listener_channel);
781 dhcp_server->started = TRUE;
786 int g_dhcp_server_set_option(GDHCPServer *dhcp_server,
787 unsigned char option_code, const char *option_value)
794 debug(dhcp_server, "option_code %d option_value %s",
795 option_code, option_value);
796 switch (option_code) {
799 case G_DHCP_DNS_SERVER:
800 if (inet_aton(option_value, &nip) == 0)
807 g_hash_table_replace(dhcp_server->option_hash,
808 GINT_TO_POINTER((int) option_code),
809 (gpointer) option_value);
813 void g_dhcp_server_set_save_lease(GDHCPServer *dhcp_server,
814 GDHCPSaveLeaseFunc func, gpointer user_data)
819 dhcp_server->save_lease_func = func;
822 void g_dhcp_server_set_lease_added_cb(GDHCPServer *dhcp_server,
823 GDHCPLeaseAddedCb cb)
828 dhcp_server->lease_added_cb = cb;
831 GDHCPServer *g_dhcp_server_ref(GDHCPServer *dhcp_server)
836 __sync_fetch_and_add(&dhcp_server->ref_count, 1);
841 void g_dhcp_server_stop(GDHCPServer *dhcp_server)
843 /* Save leases, before stop; load them before start */
844 save_lease(dhcp_server);
846 if (dhcp_server->listener_watch > 0) {
847 g_source_remove(dhcp_server->listener_watch);
848 dhcp_server->listener_watch = 0;
851 dhcp_server->listener_channel = NULL;
853 dhcp_server->started = FALSE;
856 void g_dhcp_server_unref(GDHCPServer *dhcp_server)
861 if (__sync_fetch_and_sub(&dhcp_server->ref_count, 1) != 1)
864 g_dhcp_server_stop(dhcp_server);
866 g_hash_table_destroy(dhcp_server->option_hash);
868 destroy_lease_table(dhcp_server);
870 g_free(dhcp_server->interface);
875 int g_dhcp_server_set_ip_range(GDHCPServer *dhcp_server,
876 const char *start_ip, const char *end_ip)
878 struct in_addr _host_addr;
880 if (inet_aton(start_ip, &_host_addr) == 0)
883 dhcp_server->start_ip = ntohl(_host_addr.s_addr);
885 if (inet_aton(end_ip, &_host_addr) == 0)
888 dhcp_server->end_ip = ntohl(_host_addr.s_addr);
893 void g_dhcp_server_set_lease_time(GDHCPServer *dhcp_server,
894 unsigned int lease_time)
899 dhcp_server->lease_seconds = lease_time;
902 void g_dhcp_server_set_debug(GDHCPServer *dhcp_server,
903 GDHCPDebugFunc func, gpointer user_data)
908 dhcp_server->debug_func = func;
909 dhcp_server->debug_data = user_data;