dhcprelay.c 11 KB

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