dhcprelay.c 9.8 KB

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