dhcprelay.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. #define SERVER_PORT 67
  14. #define SELECT_TIMEOUT 5 /* select timeout in sec. */
  15. #define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */
  16. /* This list holds information about clients. The xid_* functions manipulate this list. */
  17. struct xid_item {
  18. unsigned timestamp;
  19. int client;
  20. uint32_t xid;
  21. struct sockaddr_in ip;
  22. struct xid_item *next;
  23. };
  24. #define dhcprelay_xid_list (*(struct xid_item*)&bb_common_bufsiz1)
  25. static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
  26. {
  27. struct xid_item *item;
  28. /* create new xid entry */
  29. item = xmalloc(sizeof(struct xid_item));
  30. /* add xid entry */
  31. item->ip = *ip;
  32. item->xid = xid;
  33. item->client = client;
  34. item->timestamp = monotonic_sec();
  35. item->next = dhcprelay_xid_list.next;
  36. dhcprelay_xid_list.next = item;
  37. return item;
  38. }
  39. static void xid_expire(void)
  40. {
  41. struct xid_item *item = dhcprelay_xid_list.next;
  42. struct xid_item *last = &dhcprelay_xid_list;
  43. unsigned current_time = monotonic_sec();
  44. while (item != NULL) {
  45. if ((current_time - item->timestamp) > MAX_LIFETIME) {
  46. last->next = item->next;
  47. free(item);
  48. item = last->next;
  49. } else {
  50. last = item;
  51. item = item->next;
  52. }
  53. }
  54. }
  55. static struct xid_item *xid_find(uint32_t xid)
  56. {
  57. struct xid_item *item = dhcprelay_xid_list.next;
  58. while (item != NULL) {
  59. if (item->xid == xid) {
  60. return item;
  61. }
  62. item = item->next;
  63. }
  64. return NULL;
  65. }
  66. static void xid_del(uint32_t xid)
  67. {
  68. struct xid_item *item = dhcprelay_xid_list.next;
  69. struct xid_item *last = &dhcprelay_xid_list;
  70. while (item != NULL) {
  71. if (item->xid == xid) {
  72. last->next = item->next;
  73. free(item);
  74. item = last->next;
  75. } else {
  76. last = item;
  77. item = item->next;
  78. }
  79. }
  80. }
  81. /**
  82. * get_dhcp_packet_type - gets the message type of a dhcp packet
  83. * p - pointer to the dhcp packet
  84. * returns the message type on success, -1 otherwise
  85. */
  86. static int get_dhcp_packet_type(struct dhcp_packet *p)
  87. {
  88. uint8_t *op;
  89. /* it must be either a BOOTREQUEST or a BOOTREPLY */
  90. if (p->op != BOOTREQUEST && p->op != BOOTREPLY)
  91. return -1;
  92. /* get message type option */
  93. op = udhcp_get_option(p, DHCP_MESSAGE_TYPE);
  94. if (op != NULL)
  95. return op[0];
  96. return -1;
  97. }
  98. /**
  99. * get_client_devices - parses the devices list
  100. * dev_list - comma separated list of devices
  101. * returns array
  102. */
  103. static char **get_client_devices(char *dev_list, int *client_number)
  104. {
  105. char *s, **client_dev;
  106. int i, cn;
  107. /* copy list */
  108. dev_list = xstrdup(dev_list);
  109. /* get number of items, replace ',' with NULs */
  110. s = dev_list;
  111. cn = 1;
  112. while (*s) {
  113. if (*s == ',') {
  114. *s = '\0';
  115. cn++;
  116. }
  117. s++;
  118. }
  119. *client_number = cn;
  120. /* create vector of pointers */
  121. client_dev = xzalloc(cn * sizeof(*client_dev));
  122. client_dev[0] = dev_list;
  123. i = 1;
  124. while (i != cn) {
  125. client_dev[i] = client_dev[i - 1] + strlen(client_dev[i - 1]) + 1;
  126. i++;
  127. }
  128. return client_dev;
  129. }
  130. /* Creates listen sockets (in fds) bound to client and server ifaces,
  131. * and returns numerically max fd.
  132. */
  133. static int init_sockets(char **client_ifaces, int num_clients,
  134. char *server_iface, int *fds)
  135. {
  136. int i, n;
  137. /* talk to real server on bootps */
  138. fds[0] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, server_iface);
  139. n = fds[0];
  140. for (i = 1; i < num_clients; i++) {
  141. /* listen for clients on bootps */
  142. fds[i] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, client_ifaces[i-1]);
  143. if (fds[i] > n)
  144. n = fds[i];
  145. }
  146. return n;
  147. }
  148. /**
  149. * pass_to_server() - forwards dhcp packets from client to server
  150. * p - packet to send
  151. * client - number of the client
  152. */
  153. static void pass_to_server(struct dhcp_packet *p, int packet_len, int client, int *fds,
  154. struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
  155. {
  156. int res, type;
  157. struct xid_item *item;
  158. /* check packet_type */
  159. type = get_dhcp_packet_type(p);
  160. if (type != DHCPDISCOVER && type != DHCPREQUEST
  161. && type != DHCPDECLINE && type != DHCPRELEASE
  162. && type != DHCPINFORM
  163. ) {
  164. return;
  165. }
  166. /* create new xid entry */
  167. item = xid_add(p->xid, client_addr, client);
  168. /* forward request to LAN (server) */
  169. errno = 0;
  170. res = sendto(fds[0], p, packet_len, 0, (struct sockaddr*)server_addr,
  171. sizeof(struct sockaddr_in));
  172. if (res != packet_len) {
  173. bb_perror_msg("sendto");
  174. }
  175. }
  176. /**
  177. * pass_to_client() - forwards dhcp packets from server to client
  178. * p - packet to send
  179. */
  180. static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds)
  181. {
  182. int res, type;
  183. struct xid_item *item;
  184. /* check xid */
  185. item = xid_find(p->xid);
  186. if (!item) {
  187. return;
  188. }
  189. /* check packet type */
  190. type = get_dhcp_packet_type(p);
  191. if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
  192. return;
  193. }
  194. if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
  195. item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
  196. errno = 0;
  197. res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*) &(item->ip),
  198. sizeof(item->ip));
  199. if (res != packet_len) {
  200. bb_perror_msg("sendto");
  201. return;
  202. }
  203. /* remove xid entry */
  204. xid_del(p->xid);
  205. }
  206. int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  207. int dhcprelay_main(int argc, char **argv)
  208. {
  209. struct dhcp_packet dhcp_msg;
  210. struct sockaddr_in server_addr;
  211. struct sockaddr_in client_addr;
  212. fd_set rfds;
  213. char **client_ifaces;
  214. int *fds;
  215. int num_sockets, max_socket;
  216. uint32_t our_nip;
  217. server_addr.sin_family = AF_INET;
  218. server_addr.sin_port = htons(SERVER_PORT);
  219. /* dhcprelay client_iface1,client_iface2,... server_iface [server_IP] */
  220. if (argc == 4) {
  221. if (!inet_aton(argv[3], &server_addr.sin_addr))
  222. bb_perror_msg_and_die("bad server IP");
  223. } else if (argc == 3) {
  224. server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
  225. } else {
  226. bb_show_usage();
  227. }
  228. /* Produce list of client ifaces */
  229. client_ifaces = get_client_devices(argv[1], &num_sockets);
  230. num_sockets++; /* for server socket at fds[0] */
  231. fds = xmalloc(num_sockets * sizeof(fds[0]));
  232. /* Create sockets and bind one to every iface */
  233. max_socket = init_sockets(client_ifaces, num_sockets, argv[2], fds);
  234. /* Get our IP on server_iface */
  235. if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL))
  236. return 1;
  237. /* Main loop */
  238. while (1) {
  239. //reinit stuff from time to time? go back to get_client_devices
  240. //every N minutes?
  241. struct timeval tv;
  242. size_t packlen;
  243. socklen_t addr_size;
  244. int i;
  245. FD_ZERO(&rfds);
  246. for (i = 0; i < num_sockets; i++)
  247. FD_SET(fds[i], &rfds);
  248. tv.tv_sec = SELECT_TIMEOUT;
  249. tv.tv_usec = 0;
  250. if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
  251. /* server */
  252. if (FD_ISSET(fds[0], &rfds)) {
  253. packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
  254. if (packlen > 0) {
  255. pass_to_client(&dhcp_msg, packlen, fds);
  256. }
  257. }
  258. /* clients */
  259. for (i = 1; i < num_sockets; i++) {
  260. if (!FD_ISSET(fds[i], &rfds))
  261. continue;
  262. addr_size = sizeof(struct sockaddr_in);
  263. packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
  264. (struct sockaddr *)(&client_addr), &addr_size);
  265. if (packlen <= 0)
  266. continue;
  267. /* Get our IP on corresponding client_iface */
  268. //why? what if server can't route such IP?
  269. if (udhcp_read_interface(client_ifaces[i-1], NULL, &dhcp_msg.gateway_nip, NULL)) {
  270. /* Fall back to our server_iface's IP */
  271. //this makes more sense!
  272. dhcp_msg.gateway_nip = our_nip;
  273. }
  274. //maybe set dhcp_msg.flags |= BROADCAST_FLAG too?
  275. pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr);
  276. }
  277. }
  278. xid_expire();
  279. } /* while (1) */
  280. /* return 0; - not reached */
  281. }