source: MondoRescue/branches/stable/mindi-busybox/networking/udhcp/dhcprelay.c

Last change on this file was 1770, checked in by Bruno Cornec, 16 years ago
  • Better output for mindi-busybox revision
  • Remove dummy file created on NFS - report from Arnaud Tiger <arnaud.tiger_at_hp.com>
  • strace useful for debug
  • fix new versions for pb (2.0.0 for mindi and 1.7.2 for mindi-busybox)
  • fix build process for mindi-busybox + options used in that version (dd for label-partitions-as-necessary)
  • fix typo in label-partitions-as-necessary which doesn't seem to work
  • Update to busybox 1.7.2
  • perl is now required at restore time to support uuid swap partitions (and will be used for many other thigs

in the future for sure)

  • next mindi version will be 2.0.0 due to all the changes made in it (udev may break working distros)
  • small optimization in mindi on keyboard handling (one single find instead of multiple)
  • better interaction for USB device when launching mindi manually
  • attempt to automatically guess block disk size for ramdisk
  • fix typos in bkphw
  • Fix the remaining problem with UUID support for swap partitions
  • Updates mondoarchive man page for USB support
  • Adds preliminary Hardware support to mindi (Proliant SSSTK)
  • Tries to add udev support also for rhel4
  • Fix UUID support which was still broken.
  • Be conservative in test for the start-nfs script
  • Update config file for mindi-busybox for 1.7.2 migration
  • Try to run around a busybox bug (1.2.2 pb on inexistant links)
  • Add build content for mindi-busybox in pb
  • Remove distributions content for mindi-busybox
  • Fix a warning on inexistant raidtab
  • Solve problem on tmpfs in restore init (Problem of inexistant symlink and busybox)
  • Create MONDO_CACHE and use it everywhere + creation at start
  • Really never try to eject a USB device
  • Fix a issue with &> usage (replaced with 1> and 2>)
  • Adds magic file to depllist in order to have file working + ldd which helps for debugging issues
  • tty modes correct to avoid sh error messages
  • Use ext3 normally and not ext2 instead
  • USB device should be corrected after reading (take 1st part)
  • Adds a mount_USB_here function derived from mount_CDROM_here
  • usb detection place before /dev detection in device name at restore time
  • Fix when restoring from USB: media is asked in interactive mode
  • Adds USB support for mondorestore
  • mount_cdrom => mount_media
  • elilo.efi is now searched throughout /boot/efi and not in a fixed place as there is no standard
  • untar-and-softlink => untar (+ interface change)
  • suppress useless softlinks creation/removal in boot process
  • avoids udevd messages on groups
  • Increase # of disks to 99 as in mindi at restore time (should be a conf file parameter)
  • skip existing big file creation
  • seems to work correctly for USB mindi boot
  • Adds group and tty link to udev conf
  • Always load usb-torage (even 2.6) to initiate USB bus discovery
  • Better printing of messages
  • Attempt to fix a bug in supporting OpenSusE 10.3 kernel for initramfs (mindi may now use multiple regex for kernel initrd detection)
  • Links were not correctly done as non relative for modules in mindi
  • exclusion of modules denied now works
  • Also create modules in their ordinary place, so that classical modprobe works + copy modules.dep
  • Fix bugs for DENY_MODS handling
  • Add device /dev/console for udev
  • ide-generic should now really be excluded
  • Fix a bug in major number for tty
  • If udev then adds modprobe/insmod to rootfs
  • tty0 is also cretaed with udev
  • ide-generic put rather in DENY_MODS
  • udevd remove from deplist s handled in mindi directly
  • better default for mindi when using --usb
  • Handles dynamically linked busybox (in case we want to use it soon ;-)
  • Adds fixed devices to create for udev
  • ide-generic should not be part of the initrd when using libata v2
  • support a dynamically linked udev (case on Ubuntu 7.10 and Mandriva 2008.0 so should be quite generic) This will give incitation to move to dyn. linked binaries in the initrd which will help for other tasks (ia6 4)
  • Improvement in udev support (do not use cl options not available in busybox)
  • Udev in mindi
    • auto creation of the right links at boot time with udev-links.conf(from Mandriva 2008.0)
    • rework startup of udev as current makes kernel crash (from Mandriva 2008.0)
    • add support for 64 bits udev
  • Try to render MyInsmod silent at boot time
  • Adds udev support (mandatory for newest distributions to avoid remapping of devices in a different way as on the original system)
  • We also need vaft format support for USB boot
  • Adds libusual support (Ubuntu 7.10 needs it for USB)
  • Improve Ubuntu/Debian keyboard detection and support
  • pbinit adapted to new pb (0.8.10). Filtering of docs done in it
  • Suppress some mondo warnings and errors on USB again
  • Tries to fix lack of files in deb mindi package
  • Verify should now work for USB devices
  • More log/mesages improvement for USB support
  • - Supress g_erase_tmpdir_and_scratchdir
  • Improve some log messages for USB support
  • Try to improve install in mindi to avoid issues with isolinux.cfg not installed vene if in the pkg :-(
  • Improve mindi-busybox build
  • In conformity with pb 0.8.9
  • Add support for Ubuntu 7.10 in build process
  • Add USB Key button to Menu UI (CD streamer removed)
  • Attempt to fix error messages on tmp/scratch files at the end by removing those dir at the latest possible.
  • Fix a bug linked to the size of the -E param which could be used (Arnaud Tiger/René Ribaud).
  • Integrate ~/.pbrc content into mondorescue.pb (required project-builder >= 0.8.7)
  • Put mondorescue in conformity with new pb filtering rules
  • Add USB support at restore time (no test done yet). New start-usb script PB varibale added where useful
  • Unmounting USB device before removal of temporary scratchdir
  • Stil refining USB copy back to mondo (one command was not executed)
  • No need to have the image subdor in the csratchdir when USB.
  • umount the USB partition before attempting to use it
  • Remove useless copy from mindi to mondo at end of USB handling

(risky merge, we are raising the limits of 2 diverging branches. The status of stable is not completely sure as such. Will need lots of tests, but it's not yet done :-()
(merge -r1692:1769 $SVN_M/branches/2.2.5)

  • Property svn:eol-style set to native
File size: 7.7 KB
Line 
1/* vi: set sw=4 ts=4: */
2/* Port to Busybox Copyright (C) 2006 Jesse Dutton <jessedutton@gmail.com>
3 *
4 * Licensed under GPL v2, see file LICENSE in this tarball for details.
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
13#include "common.h"
14#include "dhcpd.h"
15#include "options.h"
16
17/* constants */
18#define SELECT_TIMEOUT 5 /* select timeout in sec. */
19#define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */
20#define MAX_INTERFACES 9
21
22
23/* This list holds information about clients. The xid_* functions manipulate this list. */
24static struct xid_item {
25 uint32_t xid;
26 struct sockaddr_in ip;
27 int client;
28 time_t timestamp;
29 struct xid_item *next;
30} dhcprelay_xid_list = {0, {0}, 0, 0, NULL};
31
32
33static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
34{
35 struct xid_item *item;
36
37 /* create new xid entry */
38 item = xmalloc(sizeof(struct xid_item));
39
40 /* add xid entry */
41 item->ip = *ip;
42 item->xid = xid;
43 item->client = client;
44 item->timestamp = time(NULL);
45 item->next = dhcprelay_xid_list.next;
46 dhcprelay_xid_list.next = item;
47
48 return item;
49}
50
51static void xid_expire(void)
52{
53 struct xid_item *item = dhcprelay_xid_list.next;
54 struct xid_item *last = &dhcprelay_xid_list;
55 time_t current_time = time(NULL);
56
57 while (item != NULL) {
58 if ((current_time - item->timestamp) > MAX_LIFETIME) {
59 last->next = item->next;
60 free(item);
61 item = last->next;
62 } else {
63 last = item;
64 item = item->next;
65 }
66 }
67}
68
69static struct xid_item *xid_find(uint32_t xid)
70{
71 struct xid_item *item = dhcprelay_xid_list.next;
72 while (item != NULL) {
73 if (item->xid == xid) {
74 return item;
75 }
76 item = item->next;
77 }
78 return NULL;
79}
80
81static void xid_del(uint32_t xid)
82{
83 struct xid_item *item = dhcprelay_xid_list.next;
84 struct xid_item *last = &dhcprelay_xid_list;
85 while (item != NULL) {
86 if (item->xid == xid) {
87 last->next = item->next;
88 free(item);
89 item = last->next;
90 } else {
91 last = item;
92 item = item->next;
93 }
94 }
95}
96
97/**
98 * get_dhcp_packet_type - gets the message type of a dhcp packet
99 * p - pointer to the dhcp packet
100 * returns the message type on success, -1 otherwise
101 */
102static int get_dhcp_packet_type(struct dhcpMessage *p)
103{
104 uint8_t *op;
105
106 /* it must be either a BOOTREQUEST or a BOOTREPLY */
107 if (p->op != BOOTREQUEST && p->op != BOOTREPLY)
108 return -1;
109 /* get message type option */
110 op = get_option(p, DHCP_MESSAGE_TYPE);
111 if (op != NULL)
112 return op[0];
113 return -1;
114}
115
116/**
117 * signal_handler - handles signals ;-)
118 * sig - sent signal
119 */
120static smallint dhcprelay_stopflag;
121
122static void dhcprelay_signal_handler(int sig)
123{
124 dhcprelay_stopflag = 1;
125}
126
127/**
128 * get_client_devices - parses the devices list
129 * dev_list - comma separated list of devices
130 * returns array
131 */
132static char **get_client_devices(char *dev_list, int *client_number)
133{
134 char *s, *list, **client_dev;
135 int i, cn;
136
137 /* copy list */
138 list = xstrdup(dev_list);
139 if (list == NULL) return NULL;
140
141 /* get number of items */
142 for (s = dev_list, cn = 1; *s; s++)
143 if (*s == ',')
144 cn++;
145
146 client_dev = xzalloc(cn * sizeof(*client_dev));
147
148 /* parse list */
149 s = strtok(list, ",");
150 i = 0;
151 while (s != NULL) {
152 client_dev[i++] = xstrdup(s);
153 s = strtok(NULL, ",");
154 }
155
156 /* free copy and exit */
157 free(list);
158 *client_number = cn;
159 return client_dev;
160}
161
162
163/* Creates listen sockets (in fds) and returns the number allocated. */
164static int init_sockets(char **client, int num_clients,
165 char *server, int *fds, int *max_socket)
166{
167 int i;
168
169 /* talk to real server on bootps */
170 fds[0] = listen_socket(/*INADDR_ANY,*/ 67, server);
171 *max_socket = fds[0];
172
173 /* array starts at 1 since server is 0 */
174 num_clients++;
175
176 for (i = 1; i < num_clients; i++) {
177 /* listen for clients on bootps */
178 fds[i] = listen_socket(/*NADDR_ANY,*/ 67, client[i-1]);
179 if (fds[i] > *max_socket)
180 *max_socket = fds[i];
181 }
182
183 return i;
184}
185
186
187/**
188 * pass_on() - forwards dhcp packets from client to server
189 * p - packet to send
190 * client - number of the client
191 */
192static void pass_on(struct dhcpMessage *p, int packet_len, int client, int *fds,
193 struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
194{
195 int res, type;
196 struct xid_item *item;
197
198 /* check packet_type */
199 type = get_dhcp_packet_type(p);
200 if (type != DHCPDISCOVER && type != DHCPREQUEST
201 && type != DHCPDECLINE && type != DHCPRELEASE
202 && type != DHCPINFORM
203 ) {
204 return;
205 }
206
207 /* create new xid entry */
208 item = xid_add(p->xid, client_addr, client);
209
210 /* forward request to LAN (server) */
211 res = sendto(fds[0], p, packet_len, 0, (struct sockaddr*)server_addr,
212 sizeof(struct sockaddr_in));
213 if (res != packet_len) {
214 bb_perror_msg("pass_on");
215 return;
216 }
217}
218
219/**
220 * pass_back() - forwards dhcp packets from server to client
221 * p - packet to send
222 */
223static void pass_back(struct dhcpMessage *p, int packet_len, int *fds)
224{
225 int res, type;
226 struct xid_item *item;
227
228 /* check xid */
229 item = xid_find(p->xid);
230 if (!item) {
231 return;
232 }
233
234 /* check packet type */
235 type = get_dhcp_packet_type(p);
236 if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
237 return;
238 }
239
240 if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
241 item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
242 if (item->client > MAX_INTERFACES)
243 return;
244 res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*)(&item->ip),
245 sizeof(item->ip));
246 if (res != packet_len) {
247 bb_perror_msg("pass_back");
248 return;
249 }
250
251 /* remove xid entry */
252 xid_del(p->xid);
253}
254
255static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients,
256 struct sockaddr_in *server_addr, uint32_t gw_ip)
257{
258 struct dhcpMessage dhcp_msg;
259 fd_set rfds;
260 size_t packlen;
261 socklen_t addr_size;
262 struct sockaddr_in client_addr;
263 struct timeval tv;
264 int i;
265
266 while (!dhcprelay_stopflag) {
267 FD_ZERO(&rfds);
268 for (i = 0; i < num_sockets; i++)
269 FD_SET(fds[i], &rfds);
270 tv.tv_sec = SELECT_TIMEOUT;
271 tv.tv_usec = 0;
272 if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
273 /* server */
274 if (FD_ISSET(fds[0], &rfds)) {
275 packlen = udhcp_get_packet(&dhcp_msg, fds[0]);
276 if (packlen > 0) {
277 pass_back(&dhcp_msg, packlen, fds);
278 }
279 }
280 for (i = 1; i < num_sockets; i++) {
281 /* clients */
282 if (!FD_ISSET(fds[i], &rfds))
283 continue;
284 addr_size = sizeof(struct sockaddr_in);
285 packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
286 (struct sockaddr *)(&client_addr), &addr_size);
287 if (packlen <= 0)
288 continue;
289 if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL))
290 dhcp_msg.giaddr = gw_ip;
291 pass_on(&dhcp_msg, packlen, i, fds, &client_addr, server_addr);
292 }
293 }
294 xid_expire();
295 }
296}
297
298int dhcprelay_main(int argc, char **argv);
299int dhcprelay_main(int argc, char **argv)
300{
301 int i, num_sockets, max_socket, fds[MAX_INTERFACES];
302 uint32_t gw_ip;
303 char **clients;
304 struct sockaddr_in server_addr;
305
306 server_addr.sin_family = AF_INET;
307 server_addr.sin_port = htons(67);
308 if (argc == 4) {
309 if (!inet_aton(argv[3], &server_addr.sin_addr))
310 bb_perror_msg_and_die("didn't grok server");
311 } else if (argc == 3) {
312 server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
313 } else {
314 bb_show_usage();
315 }
316 clients = get_client_devices(argv[1], &num_sockets);
317 if (!clients) return 0;
318
319 signal(SIGTERM, dhcprelay_signal_handler);
320 signal(SIGQUIT, dhcprelay_signal_handler);
321 signal(SIGINT, dhcprelay_signal_handler);
322
323 num_sockets = init_sockets(clients, num_sockets, argv[2], fds, &max_socket);
324
325 if (read_interface(argv[2], NULL, &gw_ip, NULL))
326 return 1;
327
328 dhcprelay_loop(fds, num_sockets, max_socket, clients, &server_addr, gw_ip);
329
330 if (ENABLE_FEATURE_CLEAN_UP) {
331 for (i = 0; i < num_sockets; i++) {
332 close(fds[i]);
333 free(clients[i]);
334 }
335 }
336
337 return 0;
338}
Note: See TracBrowser for help on using the repository browser.