dhcprelay.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* vi: set sw=4 ts=4: */
  2. /* Port to Busybox Copyright (C) 2006 Jesse Dutton <jessedutton@gmail.com>
  3. *
  4. * Licensed under GPL v2, see file LICENSE in this tarball for details.
  5. *
  6. * DHCP Relay for 'DHCPv4 Configuration of IPSec Tunnel Mode' support
  7. * Copyright (C) 2002 Mario Strasser <mast@gmx.net>,
  8. * Zuercher Hochschule Winterthur,
  9. * Netbeat AG
  10. * Upstream has GPL v2 or later
  11. */
  12. #include "common.h"
  13. #include "dhcpd.h"
  14. #include "options.h"
  15. /* constants */
  16. #define SELECT_TIMEOUT 5 /* select timeout in sec. */
  17. #define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */
  18. #define MAX_INTERFACES 9
  19. /* This list holds information about clients. The xid_* functions manipulate this list. */
  20. static struct xid_item {
  21. u_int32_t xid;
  22. struct sockaddr_in ip;
  23. int client;
  24. time_t timestamp;
  25. struct xid_item *next;
  26. } dhcprelay_xid_list = {0, {0}, 0, 0, NULL};
  27. static struct xid_item * xid_add(u_int32_t xid, struct sockaddr_in *ip, int client)
  28. {
  29. struct xid_item *item;
  30. /* create new xid entry */
  31. item = xmalloc(sizeof(struct xid_item));
  32. /* add xid entry */
  33. item->ip = *ip;
  34. item->xid = xid;
  35. item->client = client;
  36. item->timestamp = time(NULL);
  37. item->next = dhcprelay_xid_list.next;
  38. dhcprelay_xid_list.next = item;
  39. return item;
  40. }
  41. static void xid_expire(void)
  42. {
  43. struct xid_item *item = dhcprelay_xid_list.next;
  44. struct xid_item *last = &dhcprelay_xid_list;
  45. time_t current_time = time(NULL);
  46. while (item != NULL) {
  47. if ((current_time-item->timestamp) > MAX_LIFETIME) {
  48. last->next = item->next;
  49. free(item);
  50. item = last->next;
  51. } else {
  52. last = item;
  53. item = item->next;
  54. }
  55. }
  56. }
  57. static struct xid_item * xid_find(u_int32_t xid)
  58. {
  59. struct xid_item *item = dhcprelay_xid_list.next;
  60. while (item != NULL) {
  61. if (item->xid == xid) {
  62. return item;
  63. }
  64. item = item->next;
  65. }
  66. return NULL;
  67. }
  68. static void xid_del(u_int32_t xid)
  69. {
  70. struct xid_item *item = dhcprelay_xid_list.next;
  71. struct xid_item *last = &dhcprelay_xid_list;
  72. while (item != NULL) {
  73. if (item->xid == xid) {
  74. last->next = item->next;
  75. free(item);
  76. item = last->next;
  77. } else {
  78. last = item;
  79. item = item->next;
  80. }
  81. }
  82. }
  83. /**
  84. * get_dhcp_packet_type - gets the message type of a dhcp packet
  85. * p - pointer to the dhcp packet
  86. * returns the message type on success, -1 otherwise
  87. */
  88. static int get_dhcp_packet_type(struct dhcpMessage *p)
  89. {
  90. uint8_t *op;
  91. /* it must be either a BOOTREQUEST or a BOOTREPLY */
  92. if (p->op != BOOTREQUEST && p->op != BOOTREPLY)
  93. return -1;
  94. /* get message type option */
  95. op = get_option(p, DHCP_MESSAGE_TYPE);
  96. if (op != NULL)
  97. return op[0];
  98. return -1;
  99. }
  100. /**
  101. * signal_handler - handles signals ;-)
  102. * sig - sent signal
  103. */
  104. static int dhcprelay_stopflag;
  105. static void dhcprelay_signal_handler(int sig)
  106. {
  107. dhcprelay_stopflag = 1;
  108. }
  109. /**
  110. * get_client_devices - parses the devices list
  111. * dev_list - comma separated list of devices
  112. * returns array
  113. */
  114. static char ** get_client_devices(char *dev_list, int *client_number)
  115. {
  116. char *s, *list, **client_dev;
  117. int i, cn;
  118. /* copy list */
  119. list = xstrdup(dev_list);
  120. if (list == NULL) return NULL;
  121. /* get number of items */
  122. for (s = dev_list, cn = 1; *s; s++)
  123. if (*s == ',')
  124. cn++;
  125. client_dev = xzalloc(cn * sizeof(*client_dev));
  126. /* parse list */
  127. s = strtok(list, ",");
  128. i = 0;
  129. while (s != NULL) {
  130. client_dev[i++] = xstrdup(s);
  131. s = strtok(NULL, ",");
  132. }
  133. /* free copy and exit */
  134. free(list);
  135. *client_number = cn;
  136. return client_dev;
  137. }
  138. /* Creates listen sockets (in fds) and returns the number allocated. */
  139. static int init_sockets(char **client, int num_clients,
  140. char *server, int *fds, int *max_socket)
  141. {
  142. int i;
  143. /* talk to real server on bootps */
  144. fds[0] = listen_socket(htonl(INADDR_ANY), 67, server);
  145. *max_socket = fds[0];
  146. /* array starts at 1 since server is 0 */
  147. num_clients++;
  148. for (i=1; i < num_clients; i++) {
  149. /* listen for clients on bootps */
  150. fds[i] = listen_socket(htonl(INADDR_ANY), 67, client[i-1]);
  151. if (fds[i] > *max_socket) *max_socket = fds[i];
  152. }
  153. return i;
  154. }
  155. /**
  156. * pass_on() - forwards dhcp packets from client to server
  157. * p - packet to send
  158. * client - number of the client
  159. */
  160. static void pass_on(struct dhcpMessage *p, int packet_len, int client, int *fds,
  161. struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
  162. {
  163. int res, type;
  164. struct xid_item *item;
  165. /* check packet_type */
  166. type = get_dhcp_packet_type(p);
  167. if (type != DHCPDISCOVER && type != DHCPREQUEST
  168. && type != DHCPDECLINE && type != DHCPRELEASE
  169. && type != DHCPINFORM
  170. ) {
  171. return;
  172. }
  173. /* create new xid entry */
  174. item = xid_add(p->xid, client_addr, client);
  175. /* forward request to LAN (server) */
  176. res = sendto(fds[0], p, packet_len, 0, (struct sockaddr*)server_addr,
  177. sizeof(struct sockaddr_in));
  178. if (res != packet_len) {
  179. bb_perror_msg("pass_on");
  180. return;
  181. }
  182. }
  183. /**
  184. * pass_back() - forwards dhcp packets from server to client
  185. * p - packet to send
  186. */
  187. static void pass_back(struct dhcpMessage *p, int packet_len, int *fds)
  188. {
  189. int res, type;
  190. struct xid_item *item;
  191. /* check xid */
  192. item = xid_find(p->xid);
  193. if (!item) {
  194. return;
  195. }
  196. /* check packet type */
  197. type = get_dhcp_packet_type(p);
  198. if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
  199. return;
  200. }
  201. if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
  202. item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
  203. if (item->client > MAX_INTERFACES)
  204. return;
  205. res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*)(&item->ip),
  206. sizeof(item->ip));
  207. if (res != packet_len) {
  208. bb_perror_msg("pass_back");
  209. return;
  210. }
  211. /* remove xid entry */
  212. xid_del(p->xid);
  213. }
  214. static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients,
  215. struct sockaddr_in *server_addr, uint32_t gw_ip)
  216. {
  217. struct dhcpMessage dhcp_msg;
  218. fd_set rfds;
  219. size_t packlen;
  220. socklen_t addr_size;
  221. struct sockaddr_in client_addr;
  222. struct timeval tv;
  223. int i;
  224. while (!dhcprelay_stopflag) {
  225. FD_ZERO(&rfds);
  226. for (i = 0; i < num_sockets; i++)
  227. FD_SET(fds[i], &rfds);
  228. tv.tv_sec = SELECT_TIMEOUT;
  229. tv.tv_usec = 0;
  230. if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
  231. /* server */
  232. if (FD_ISSET(fds[0], &rfds)) {
  233. packlen = udhcp_get_packet(&dhcp_msg, fds[0]);
  234. if (packlen > 0) {
  235. pass_back(&dhcp_msg, packlen, fds);
  236. }
  237. }
  238. for (i = 1; i < num_sockets; i++) {
  239. /* clients */
  240. if (!FD_ISSET(fds[i], &rfds))
  241. continue;
  242. addr_size = sizeof(struct sockaddr_in);
  243. packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
  244. (struct sockaddr *)(&client_addr), &addr_size);
  245. if (packlen <= 0)
  246. continue;
  247. if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL) < 0)
  248. dhcp_msg.giaddr = gw_ip;
  249. pass_on(&dhcp_msg, packlen, i, fds, &client_addr, server_addr);
  250. }
  251. }
  252. xid_expire();
  253. }
  254. }
  255. int dhcprelay_main(int argc, char **argv)
  256. {
  257. int i, num_sockets, max_socket, fds[MAX_INTERFACES];
  258. uint32_t gw_ip;
  259. char **clients;
  260. struct sockaddr_in server_addr;
  261. server_addr.sin_family = AF_INET;
  262. server_addr.sin_port = htons(67);
  263. if (argc == 4) {
  264. if (!inet_aton(argv[3], &server_addr.sin_addr))
  265. bb_perror_msg_and_die("didn't grok server");
  266. } else if (argc == 3) {
  267. server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
  268. } else {
  269. bb_show_usage();
  270. }
  271. clients = get_client_devices(argv[1], &num_sockets);
  272. if (!clients) return 0;
  273. signal(SIGTERM, dhcprelay_signal_handler);
  274. signal(SIGQUIT, dhcprelay_signal_handler);
  275. signal(SIGINT, dhcprelay_signal_handler);
  276. num_sockets = init_sockets(clients, num_sockets, argv[2], fds, &max_socket);
  277. if (read_interface(argv[2], NULL, &gw_ip, NULL) == -1)
  278. return 1;
  279. dhcprelay_loop(fds, num_sockets, max_socket, clients, &server_addr, gw_ip);
  280. if (ENABLE_FEATURE_CLEAN_UP) {
  281. for (i = 0; i < num_sockets; i++) {
  282. close(fds[i]);
  283. free(clients[i]);
  284. }
  285. }
  286. return 0;
  287. }