dhcprelay.c 10 KB

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