3
0

dhcprelay.c 10 KB

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