dhcprelay.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* vi: set sw=4 ts=4: */
  2. /* Port to Busybox Copyright (C) 2006 Jesse Dutton <jessedutton@gmail.com>
  3. *
  4. * Licensed under GPLv2, see file LICENSE in this source tree.
  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. //applet:IF_DHCPRELAY(APPLET(dhcprelay, BB_DIR_USR_SBIN, BB_SUID_DROP))
  13. //kbuild:lib-$(CONFIG_DHCPRELAY) += dhcprelay.o
  14. //usage:#define dhcprelay_trivial_usage
  15. //usage: "CLIENT_IFACE[,CLIENT_IFACE2]... SERVER_IFACE [SERVER_IP]"
  16. //usage:#define dhcprelay_full_usage "\n\n"
  17. //usage: "Relay DHCP requests between clients and server"
  18. #include "common.h"
  19. #define SERVER_PORT 67
  20. /* lifetime of an xid entry in sec. */
  21. #define MAX_LIFETIME 2*60
  22. /* select timeout in sec. */
  23. #define SELECT_TIMEOUT (MAX_LIFETIME / 8)
  24. /* This list holds information about clients. The xid_* functions manipulate this list. */
  25. struct xid_item {
  26. unsigned timestamp;
  27. int client;
  28. uint32_t xid;
  29. struct sockaddr_in ip;
  30. struct xid_item *next;
  31. } FIX_ALIASING;
  32. #define dhcprelay_xid_list (*(struct xid_item*)bb_common_bufsiz1)
  33. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  34. static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
  35. {
  36. struct xid_item *item;
  37. /* create new xid entry */
  38. item = xmalloc(sizeof(struct xid_item));
  39. /* add xid entry */
  40. item->ip = *ip;
  41. item->xid = xid;
  42. item->client = client;
  43. item->timestamp = monotonic_sec();
  44. item->next = dhcprelay_xid_list.next;
  45. dhcprelay_xid_list.next = item;
  46. return item;
  47. }
  48. static void xid_expire(void)
  49. {
  50. struct xid_item *item = dhcprelay_xid_list.next;
  51. struct xid_item *last = &dhcprelay_xid_list;
  52. unsigned current_time = monotonic_sec();
  53. while (item != NULL) {
  54. if ((current_time - item->timestamp) > MAX_LIFETIME) {
  55. last->next = item->next;
  56. free(item);
  57. item = last->next;
  58. } else {
  59. last = item;
  60. item = item->next;
  61. }
  62. }
  63. }
  64. static struct xid_item *xid_find(uint32_t xid)
  65. {
  66. struct xid_item *item = dhcprelay_xid_list.next;
  67. while (item != NULL) {
  68. if (item->xid == xid) {
  69. break;
  70. }
  71. item = item->next;
  72. }
  73. return item;
  74. }
  75. static void xid_del(uint32_t xid)
  76. {
  77. struct xid_item *item = dhcprelay_xid_list.next;
  78. struct xid_item *last = &dhcprelay_xid_list;
  79. while (item != NULL) {
  80. if (item->xid == xid) {
  81. last->next = item->next;
  82. free(item);
  83. item = last->next;
  84. } else {
  85. last = item;
  86. item = item->next;
  87. }
  88. }
  89. }
  90. /**
  91. * get_dhcp_packet_type - gets the message type of a dhcp packet
  92. * p - pointer to the dhcp packet
  93. * returns the message type on success, -1 otherwise
  94. */
  95. static int get_dhcp_packet_type(struct dhcp_packet *p)
  96. {
  97. uint8_t *op;
  98. /* it must be either a BOOTREQUEST or a BOOTREPLY */
  99. if (p->op != BOOTREQUEST && p->op != BOOTREPLY)
  100. return -1;
  101. /* get message type option */
  102. op = udhcp_get_option(p, DHCP_MESSAGE_TYPE);
  103. if (op != NULL)
  104. return op[0];
  105. return -1;
  106. }
  107. /**
  108. * make_iface_list - parses client/server interface names
  109. * returns array
  110. */
  111. static char **make_iface_list(char **client_and_server_ifaces, int *client_number)
  112. {
  113. char *s, **iface_list;
  114. int i, cn;
  115. /* get number of items */
  116. cn = 2; /* 1 server iface + at least 1 client one */
  117. s = client_and_server_ifaces[0]; /* list of client ifaces */
  118. while (*s) {
  119. if (*s == ',')
  120. cn++;
  121. s++;
  122. }
  123. *client_number = cn;
  124. /* create vector of pointers */
  125. iface_list = xzalloc(cn * sizeof(iface_list[0]));
  126. iface_list[0] = client_and_server_ifaces[1]; /* server iface */
  127. i = 1;
  128. s = xstrdup(client_and_server_ifaces[0]); /* list of client ifaces */
  129. goto store_client_iface_name;
  130. while (i < cn) {
  131. if (*s++ == ',') {
  132. s[-1] = '\0';
  133. store_client_iface_name:
  134. iface_list[i++] = s;
  135. }
  136. }
  137. return iface_list;
  138. }
  139. /* Creates listen sockets (in fds) bound to client and server ifaces,
  140. * and returns numerically max fd.
  141. */
  142. static int init_sockets(char **iface_list, int num_clients, int *fds)
  143. {
  144. int i, n;
  145. n = 0;
  146. for (i = 0; i < num_clients; i++) {
  147. fds[i] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, iface_list[i]);
  148. if (n < fds[i])
  149. n = fds[i];
  150. }
  151. return n;
  152. }
  153. static int sendto_ip4(int sock, const void *msg, int msg_len, struct sockaddr_in *to)
  154. {
  155. int err;
  156. errno = 0;
  157. err = sendto(sock, msg, msg_len, 0, (struct sockaddr*) to, sizeof(*to));
  158. err -= msg_len;
  159. if (err)
  160. bb_perror_msg("sendto");
  161. return err;
  162. }
  163. /**
  164. * pass_to_server() - forwards dhcp packets from client to server
  165. * p - packet to send
  166. * client - number of the client
  167. */
  168. static void pass_to_server(struct dhcp_packet *p, int packet_len, int client, int *fds,
  169. struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
  170. {
  171. int type;
  172. /* check packet_type */
  173. type = get_dhcp_packet_type(p);
  174. if (type != DHCPDISCOVER && type != DHCPREQUEST
  175. && type != DHCPDECLINE && type != DHCPRELEASE
  176. && type != DHCPINFORM
  177. ) {
  178. return;
  179. }
  180. /* create new xid entry */
  181. xid_add(p->xid, client_addr, client);
  182. /* forward request to server */
  183. /* note that we send from fds[0] which is bound to SERVER_PORT (67).
  184. * IOW: we send _from_ SERVER_PORT! Although this may look strange,
  185. * RFC 1542 not only allows, but prescribes this for BOOTP relays.
  186. */
  187. sendto_ip4(fds[0], p, packet_len, server_addr);
  188. }
  189. /**
  190. * pass_to_client() - forwards dhcp packets from server to client
  191. * p - packet to send
  192. */
  193. static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds)
  194. {
  195. int type;
  196. struct xid_item *item;
  197. /* check xid */
  198. item = xid_find(p->xid);
  199. if (!item) {
  200. return;
  201. }
  202. /* check packet type */
  203. type = get_dhcp_packet_type(p);
  204. if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
  205. return;
  206. }
  207. //TODO: also do it if (p->flags & htons(BROADCAST_FLAG)) is set!
  208. if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
  209. item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
  210. if (sendto_ip4(fds[item->client], p, packet_len, &item->ip) != 0) {
  211. return; /* send error occurred */
  212. }
  213. /* remove xid entry */
  214. xid_del(p->xid);
  215. }
  216. int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  217. int dhcprelay_main(int argc, char **argv)
  218. {
  219. struct sockaddr_in server_addr;
  220. char **iface_list;
  221. int *fds;
  222. int num_sockets, max_socket;
  223. uint32_t our_nip;
  224. INIT_G();
  225. server_addr.sin_family = AF_INET;
  226. server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
  227. server_addr.sin_port = htons(SERVER_PORT);
  228. /* dhcprelay CLIENT_IFACE1[,CLIENT_IFACE2...] SERVER_IFACE [SERVER_IP] */
  229. if (argc == 4) {
  230. if (!inet_aton(argv[3], &server_addr.sin_addr))
  231. bb_perror_msg_and_die("bad server IP");
  232. } else if (argc != 3) {
  233. bb_show_usage();
  234. }
  235. iface_list = make_iface_list(argv + 1, &num_sockets);
  236. fds = xmalloc(num_sockets * sizeof(fds[0]));
  237. /* Create sockets and bind one to every iface */
  238. max_socket = init_sockets(iface_list, num_sockets, fds);
  239. /* Get our IP on server_iface */
  240. if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL))
  241. return 1;
  242. /* Main loop */
  243. while (1) {
  244. // reinit stuff from time to time? go back to make_iface_list
  245. // every N minutes?
  246. fd_set rfds;
  247. struct timeval tv;
  248. int i;
  249. FD_ZERO(&rfds);
  250. for (i = 0; i < num_sockets; i++)
  251. FD_SET(fds[i], &rfds);
  252. tv.tv_sec = SELECT_TIMEOUT;
  253. tv.tv_usec = 0;
  254. if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
  255. int packlen;
  256. struct dhcp_packet dhcp_msg;
  257. /* server */
  258. if (FD_ISSET(fds[0], &rfds)) {
  259. packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
  260. if (packlen > 0) {
  261. pass_to_client(&dhcp_msg, packlen, fds);
  262. }
  263. }
  264. /* clients */
  265. for (i = 1; i < num_sockets; i++) {
  266. struct sockaddr_in client_addr;
  267. socklen_t addr_size;
  268. if (!FD_ISSET(fds[i], &rfds))
  269. continue;
  270. addr_size = sizeof(client_addr);
  271. packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
  272. (struct sockaddr *)(&client_addr), &addr_size);
  273. if (packlen <= 0)
  274. continue;
  275. /* Get our IP on corresponding client_iface */
  276. // RFC 1542
  277. // 4.1 General BOOTP Processing for Relay Agents
  278. // 4.1.1 BOOTREQUEST Messages
  279. // If the relay agent does decide to relay the request, it MUST examine
  280. // the 'giaddr' ("gateway" IP address) field. If this field is zero,
  281. // the relay agent MUST fill this field with the IP address of the
  282. // interface on which the request was received. If the interface has
  283. // more than one IP address logically associated with it, the relay
  284. // agent SHOULD choose one IP address associated with that interface and
  285. // use it consistently for all BOOTP messages it relays. If the
  286. // 'giaddr' field contains some non-zero value, the 'giaddr' field MUST
  287. // NOT be modified. The relay agent MUST NOT, under any circumstances,
  288. // fill the 'giaddr' field with a broadcast address as is suggested in
  289. // [1] (Section 8, sixth paragraph).
  290. // but why? what if server can't route such IP? Client ifaces may be, say, NATed!
  291. // 4.1.2 BOOTREPLY Messages
  292. // BOOTP relay agents relay BOOTREPLY messages only to BOOTP clients.
  293. // It is the responsibility of BOOTP servers to send BOOTREPLY messages
  294. // directly to the relay agent identified in the 'giaddr' field.
  295. // (yeah right, unless it is impossible... see comment above)
  296. // Therefore, a relay agent may assume that all BOOTREPLY messages it
  297. // receives are intended for BOOTP clients on its directly-connected
  298. // networks.
  299. //
  300. // When a relay agent receives a BOOTREPLY message, it should examine
  301. // the BOOTP 'giaddr', 'yiaddr', 'chaddr', 'htype', and 'hlen' fields.
  302. // These fields should provide adequate information for the relay agent
  303. // to deliver the BOOTREPLY message to the client.
  304. //
  305. // The 'giaddr' field can be used to identify the logical interface from
  306. // which the reply must be sent (i.e., the host or router interface
  307. // connected to the same network as the BOOTP client). If the content
  308. // of the 'giaddr' field does not match one of the relay agent's
  309. // directly-connected logical interfaces, the BOOTREPLY message MUST be
  310. // silently discarded.
  311. if (udhcp_read_interface(iface_list[i], NULL, &dhcp_msg.gateway_nip, NULL)) {
  312. /* Fall back to our IP on server iface */
  313. // this makes more sense!
  314. dhcp_msg.gateway_nip = our_nip;
  315. }
  316. // maybe dhcp_msg.hops++? drop packets with too many hops (RFC 1542 says 4 or 16)?
  317. pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr);
  318. }
  319. }
  320. xid_expire();
  321. } /* while (1) */
  322. /* return 0; - not reached */
  323. }