dhcprelay.c 10 KB

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