Compare commits

...

124 Commits

Author SHA1 Message Date
Bernhard Reutner-Fischer
41e206fba4 - add helptext for --retries 2006-05-10 16:11:25 +00:00
Mike Frysinger
865613f728 Giuseppe Ciotta writes: add -t, --retries=NUM to show_usage() 2006-04-27 02:01:16 +00:00
Mike Frysinger
f8f2b4b647 2006-04-20 Shaun Jackman <sjackman@gmail.com>
* networking/udhcp/socket.c (listen_socket): Use ifr_name instead
        of ifr_ifrn.ifrn_name.
2006-04-21 01:26:44 +00:00
Mike Frysinger
387050b1dc Shaun Jackman writes: udhcp uses some fun-fun __GLIBC__ tests. A _NEWLIB_VERSION test is required as well. 2006-04-21 01:24:58 +00:00
Mike Frysinger
80ea7ad686 re-order includes so prototypes get changed properly 2006-04-19 22:06:04 +00:00
Mike Frysinger
04edff8ba8 use memcmp() not bcmp() 2006-04-16 05:52:06 +00:00
Bernhard Reutner-Fischer
3da65ce484 - add remark about bb_xdaemon. Need to put a copy into libbb_udhcp.h for the
standalone version.
2006-04-12 18:17:00 +00:00
Mike Frysinger
1a9fb6c293 make sure we dont leak file descriptors to external scripts as pointed out by Roy Marples 2006-04-08 01:10:01 +00:00
Bernhard Reutner-Fischer
2aa7fab29a - typo: s/compatability/compatibility/g 2006-04-02 20:25:08 +00:00
Mike Frysinger
2c03a8b593 unify mostly common code 2006-04-01 03:23:03 +00:00
Mike Frysinger
78cb565ab0 misc touchups 2006-04-01 01:16:22 +00:00
Mike Frysinger
49458a69d0 update clean target 2006-03-23 23:47:23 +00:00
Mike Frysinger
c202003968 copy from busybox 2006-03-23 03:49:48 +00:00
Mike Frysinger
a40fc880c8 make the makefile a little more flexible so we can store in either location 2006-03-23 03:47:19 +00:00
Mike Frysinger
b37705b24e sync busybox and udhcp trees 2006-03-23 03:05:05 +00:00
Mike Frysinger
8df074e69a unneeded 2006-03-23 03:02:31 +00:00
Paul Fox
5b0249b8c6 syncing with busybox tree -- adds ability to set vendor class id option. 2005-10-14 03:07:32 +00:00
Paul Fox
2e4633ef70 syncing with busybox tree. adds ability to suppress default client-id,
to match kernel and uboot behavior.
2005-10-14 03:06:14 +00:00
Eric Andersen
86f13921ea Tobias Krawutschke found a bug where the DHCP client would accept packets
with the wrong ARP address, meaning we could easily get somebody else's IP.
That is a bad thing, and this is the minimal two-line fix.
2005-05-26 06:23:40 +00:00
Mike Frysinger
8acf2b25a3 In Bug 209, bernhardf writes:
struct named {
    named_field: init,
};
appear to be depraced by now; rather use C{99} format as in
struct named {
    .named_field = init,
};
2005-04-16 04:17:06 +00:00
Mike Frysinger
bb267dcce4 clean up ! support DESTDIR, fix permissions of installed files, and also install the .nak sample script 2004-12-17 04:21:06 +00:00
Mike Frysinger
c2fb7afe65 err forgot to add static_lease.o to the obj list 2004-12-17 04:06:17 +00:00
Mike Frysinger
082c7e9d6c add patch by Roy Marples to export nis env vars 2004-12-06 14:45:42 +00:00
Mike Frysinger
c7ecaab080 add patch by Roy Marples to support fully qualified doman names used by MS DHCP servers for example 2004-12-06 14:43:51 +00:00
Mike Frysinger
394a9621d1 tabs != 8 spaces, so use spaces ;) 2004-12-04 02:33:12 +00:00
Mike Frysinger
a5f069cf03 merge static lease code from busybox back here 2004-12-04 02:28:53 +00:00
Russ Dill
b58ee28241 backout unintented commit 2004-05-19 09:19:37 +00:00
Russ Dill
65243ca889 sync with busybox 2004-05-19 09:18:20 +00:00
Russ Dill
9a59393459 get rid of timewarp problems in client (server will be done later) 2004-05-19 08:26:01 +00:00
Russ Dill
7a77334126 switch over to sysinfo to go off uptime instead of system time 2004-05-19 08:07:54 +00:00
Russ Dill
c3aaf5692a get rid of some duplicate checks 2004-05-19 07:43:00 +00:00
Russ Dill
78ade985d9 head off possible packing problems 2004-05-19 07:36:42 +00:00
Russ Dill
ba260da42a update authors 2004-05-19 07:35:45 +00:00
Russ Dill
91260dd1f1 spelling fixes 2004-05-19 07:33:49 +00:00
Russ Dill
293312a69f fixup whitespace 2004-05-19 07:32:36 +00:00
Russ Dill
72f3712b60 fix memory leak caused by switching to vfork 2004-05-19 06:58:57 +00:00
Eric Andersen
d08f1cc610 Use standard C99 types 2004-01-30 23:45:53 +00:00
Russ Dill
642e15f264 fix a long standing underallocation bug 2003-12-24 19:57:13 +00:00
Russ Dill
18cc4adac2 move debugging to safe place (before vfork) 2003-12-24 19:31:08 +00:00
Russ Dill
9b98f2c1ab remerge with busybox 2003-12-18 22:35:01 +00:00
Russ Dill
4d1bfe0b76 forgotten include 2003-12-16 02:34:28 +00:00
Russ Dill
d47a7c9c39 get working on trying to merge to busybox 2003-12-16 02:25:39 +00:00
Russ Dill
09457926fa revert the const stuff 2003-12-16 01:40:37 +00:00
Russ Dill
f6e6a8c6b0 some cleanups, sanity stuff, etc 2003-12-16 01:38:51 +00:00
Russ Dill
195c5f1b8e updated changelog to reflect previous changes 2003-02-12 22:14:55 +00:00
Russ Dill
19e80ae128 byte order fix for mton 2003-02-12 22:05:28 +00:00
Russ Dill
7c12c431ed alignment patch from Rui He 2003-02-12 21:45:40 +00:00
Russ Dill
8f79eac98a fix potential problem (if sizeof(int) != sizeof(void *)) 2003-01-21 21:24:34 +00:00
Russ Dill
7cd05f4e61 udhcp mistakenly closes its own port (bug 1295) 2003-01-21 21:18:22 +00:00
Russ Dill
fe948782ed fix for continous backgrounding (bug 1294) 2003-01-21 21:09:42 +00:00
Russ Dill
9f3200d3aa small change for busybox 2002-12-11 22:30:44 +00:00
Russ Dill
27db173d0c make it a little easier on new users 2002-12-11 21:36:06 +00:00
Russ Dill
4d15ca6245 sanity check for max_leases (bug 1285) 2002-12-11 21:34:43 +00:00
Russ Dill
d8258a5173 patch from andersee 2002-12-11 21:05:27 +00:00
Russ Dill
d0d32ace90 removed trailing space from env vars 2002-12-11 21:01:23 +00:00
Russ Dill
bd709948ee added env var for mask bits 2002-12-11 20:53:58 +00:00
Russ Dill
fd015edaa9 add something to the TODO 2002-12-09 22:26:01 +00:00
Russ Dill
95e1718441 Documentation updates 2002-11-19 00:40:05 +00:00
Russ Dill
fc7c94e5d7 version number oops 2002-11-05 20:10:21 +00:00
Russ Dill
7d67eb5ab8 0.9.8 2002-10-31 18:46:43 +00:00
Russ Dill
0109516837 split up README 2002-10-31 18:02:09 +00:00
Russ Dill
fafdfbc9bd make udhcpc use /dev/urandom 2002-10-31 17:29:26 +00:00
Russ Dill
4060035575 compile, then commit... 2002-10-19 01:10:43 +00:00
Russ Dill
5ab1e30dde fixed renew behavior 2002-10-19 01:03:10 +00:00
Russ Dill
adf50b3afe udhcp now fits nicely into busybox 2002-09-20 20:36:15 +00:00
Russ Dill
6daadcc6af more script stuff 2002-09-18 18:09:39 +00:00
Russ Dill
d5b915377b socketpair signal handling 2002-09-17 22:44:01 +00:00
Russ Dill
ee3c1fb219 improved error message 2002-09-06 22:05:59 +00:00
Russ Dill
7d85f05318 improved signal handling behavior 2002-09-06 22:04:09 +00:00
Russ Dill
c26cd91421 Fix to config file parsing 2002-08-27 22:09:20 +00:00
Russ Dill
d5e8aa555d Fix load lease logic 2002-08-27 21:22:48 +00:00
Russ Dill
6fc8921432 fix clear lease logic 2002-08-27 21:22:01 +00:00
Russ Dill
397cad5a7e -h is now an alias for -H (udhcp bug #1253) 2002-08-27 20:44:57 +00:00
Russ Dill
d3f6108478 Shorter timeout on not receiving offers 2002-08-27 20:15:17 +00:00
Russ Dill
97a2d581b2 Improved signal behavior by client 2002-08-27 20:13:32 +00:00
Russ Dill
66c82dc0a0 Would never assign end address 2002-08-27 19:09:01 +00:00
Russ Dill
db89405506 Was improperly reporting yiaddr as siaddr 2002-08-27 19:00:31 +00:00
Russ Dill
2c42e666b6 fix reading of clientid 2002-08-27 18:50:07 +00:00
Russ Dill
0a2cf9f343 added background_if_no_lease option 2002-08-27 18:42:54 +00:00
Russ Dill
f31df6cd1f compute udp checksum correctly on big endian hosts 2002-08-27 18:23:14 +00:00
Russ Dill
407d84f7ab Improved NAK'ing behavior 2002-08-27 18:11:10 +00:00
Russ Dill
8febad96a0 fix possible packet loss in udhcpc 2002-08-27 17:16:47 +00:00
Russ Dill
3c9ed2b557 sys_errlist[]->strerror() 2002-08-27 17:11:58 +00:00
Russ Dill
9ee05bff02 daemon(0,0) was closing our pid_fd if stdin/out/err wasn't open 2002-06-03 19:47:35 +00:00
Russ Dill
1d35be5808 0.9.7 commits 2002-05-27 06:18:49 +00:00
Russ Dill
4fe980fa17 added nak script support 2002-05-23 19:29:25 +00:00
Russ Dill
393499ec8f added message to understood client options 2002-05-23 19:20:42 +00:00
Russ Dill
3eccddb359 signedness issues (or annoyances) 2002-04-02 22:49:33 +00:00
Russ Dill
fcab81a38f prototype mismatch 2002-04-02 22:26:18 +00:00
Russ Dill
93c4868651 little commenting in options.h 2002-03-23 00:34:03 +00:00
Russ Dill
58a95d7aed generate the paramater request list from the options list 2002-03-23 00:30:12 +00:00
Russ Dill
805978dda0 options code dealings should be unsigned 2002-03-22 23:55:55 +00:00
Russ Dill
15c831effd dhcp_options code should be unsigned 2002-03-22 23:51:06 +00:00
Russ Dill
fb5ff5ff9b small typo 2002-03-22 23:37:21 +00:00
Russ Dill
96eb61ee29 fix for bug 1230, don't reset server_addr on renew 2002-03-19 06:02:29 +00:00
Russ Dill
0e36d664e6 add request list for dhcp renew 2002-03-19 05:39:42 +00:00
Russ Dill
99e73baa46 responses should have BOOTREPLY 2002-03-19 01:01:33 +00:00
Russ Dill
b3acec08ad small fix to client id fix 2002-02-12 01:51:19 +00:00
Russ Dill
5d811d87af fixed clientid option 2002-02-12 01:19:48 +00:00
Russ Dill
5aa55f970a replace bzero with memset 2002-01-17 22:44:30 +00:00
Russ Dill
aa84e8eb9e todo additions 2002-01-17 08:17:52 +00:00
Russ Dill
3f812d49b1 remember to add the fd to select (client) 2002-01-17 00:13:19 +00:00
Russ Dill
5cff5e8663 added cvsignore 2002-01-17 00:12:00 +00:00
Russ Dill
66472ba42d only close and open ports on error 2001-12-29 04:06:02 +00:00
Russ Dill
09d6a685aa a few small fixes to client 2001-12-21 18:11:08 +00:00
Russ Dill
cf7bad6de8 fixed some problems in calculating the length of a option list 2001-12-21 00:41:12 +00:00
Russ Dill
0fb8544098 avoid traffic loop in client 2001-12-21 00:27:01 +00:00
Russ Dill
3095cf8605 fixed alignment problem 2001-12-21 00:17:19 +00:00
Russ Dill
de9a43164e added udhcpd config file option, switched to daemon 2001-12-21 00:06:15 +00:00
Russ Dill
4603d23876 moved pidfile aquire down 2001-12-20 23:55:42 +00:00
Russ Dill
d850ed22bb tried to trim some fat 2001-12-10 05:03:23 +00:00
Russ Dill
a31d890883 combined read_interface function 2001-12-09 02:40:55 +00:00
Russ Dill
06fbd1f3a2 combined read_interface function 2001-12-09 02:40:41 +00:00
Russ Dill
f9bd2018dc more error processing in files.c 2001-12-09 00:58:41 +00:00
Russ Dill
be27010b8e accept missing udp checksums and excess garbage 2001-12-08 00:10:57 +00:00
Russ Dill
1d61740f8a added todo for eth0:0 2001-11-16 20:15:13 +00:00
Russ Dill
9532b72869 documentation updates 2001-10-29 17:59:02 +00:00
Russ Dill
0d8db2a88e 0.9.6 release 2001-10-01 19:29:46 +00:00
Russ Dill
af3c128599 update changelog 2001-10-01 19:23:27 +00:00
Russ Dill
ddf4f3233c added man pages 2001-10-01 19:21:02 +00:00
Russ Dill
f8768addcb improved pidfile handling 2001-10-01 19:16:49 +00:00
Russ Dill
190f905e0d added vendor id option to client 2001-09-26 17:12:22 +00:00
Russ Dill
f567922680 added bootp paramater support to client 2001-09-21 20:14:07 +00:00
Russ Dill
4d17995d70 added bootp option support to server 2001-09-21 19:39:04 +00:00
59 changed files with 3263 additions and 1565 deletions

View File

@ -1,13 +1,13 @@
Moreton Bay DHCP Server
udhcp server/client package
-----------------------
Russ Dill <Russ.Dill@asu.edu>
Matthew Ramsay <matthewr@moreton.com.au>
Chris Trew <christ@moreton.com.au>
Russ Dill <Russ.Dill@asu.edu>
Other Credits:
--------------
Moreton Bay (http://www.moretonbay.com/)
Lineo (http://opensource.lineo.com)
Vladimir Oleynik <dzo@simtrea.ru> Size optimizations

View File

@ -1,3 +1,81 @@
0.9.9 (pending)
+ Various other size optimizations (Vladimir)
+ Change strerror(errno) to %m (Vladimir N. Oleynik <dzo@simtreas.ru>)
+ Fixed a little endian problem in mton (Bastian Blank <waldi@debian.org>)
+ Fixed a arpping alignment problem (Rui He <rhe@3eti.com>)
+ Added sanity check for max_leases (udhcp bug #1285) (me)
+ Finally got rid of the trailing space in enviromental vars (me)
+ added an new enviromental variable: $mask. It contains the number
of subnet bits for tools like ip route that require it.
(Bastian Blank <waldi@debian.org>, me)
0.9.8 (021031)
+ split up README files (me)
+ use /dev/urandom to seed xid's (instead of time(0)) (me)
+ fixed renew behavior (me)
+ udhcp now fits nicely into busybox
(Glenn McGrath <bug1@iinet.net.au> as well as myself)
+ updated client manpage (me)
+ both client and server now use sockets for signal handling,
hopefully, this will be the last needed change in signal
handling, I'm fairly certain all the possible races are now
closed. (me)
+ The server now restarts the auto_time timer when it receives
a SIGUSR1 (write out config file). (me)
+ Improve signal handling (David Poole)
+ Fix to config file parsing (Matt Kraai)
+ Fix load lease logic (me)
+ Fix clear_lease logic (me)
+ -h is now an alias for -H (udhcp bug #1253)
+ Shorter timeout on not receiving offers (me)
+ Improved signal behavior by client (me)
+ Would never assign end address (Keith Smith <keith@ksmith.com>)
+ Was improperly reporting yiaddr as siaddr (ben-udhcp@bdlow.net)
udhcp bug#1256
+ Fixed reading of client id (David Poole <davep@portsmith.com>)
+ change sys_errlist[] to strerror() as it aparently doesn't exist
(Erik Andersen <andersee@codepoet.org>)
+ fixed get_raw_packet so it returns -2 on non fatal errors
(Ted Lemon <Ted.Lemon@nominum.com>)
+ Improved (hopefully) NAKing behavior (me)
+ Added -b option (Jouni Malinen)
+ Compute checksums correctly on big endian hosts
(Jouni Malinen <jkmaline@cc.hut.fi>)
0.9.7 (020526)
+ Use add_lease in read_leases, sanitizes leases more, and clears out exprired
ones if there is no more room (me)
+ Moved udhcpd.leases to /var/lib/misc/udhcpd.leases (Debian bug #147747)
+ Change (obsolete) AF_INET in arping.c to PF_PACKET (Debian bug #127049)
+ Added script hook for DHCPNAK (nak), as well as providing the message option
(me)
+ Generate the paramaters request list by seeing what options in options.c are
ored with OPTION_REQ in options.c
+ Fix dhcp renew forgetfullness on client (bug #1230)
+ Fix dhcp release bug on client (bug #1231)
+ Set option request list for DHCP renew (bug #1233)
+ Set BOOTREQUEST/REPLY properly
+ Change client-identifier field to popularly expected behavior (me)
+ Only reopen port on errors (me)
+ Change fork/close/setsid structures to daemon() (me)
+ Allow user to specify udhcpd config file at run time (Steven, me)
+ Write pidfile after changing it (Steven CTR Carr <Steven.CTR.Carr@tc.faa.gov>)
+ Added env var docs to udhcpc man page (Matt)
+ Standardized lowercase udhcp in documentation (me)
+ Accept packets without a UDP checksum (me)
+ Accept packets with extra garbage (me)
+ Better error handling in files.c (me)
+ Combined read_interface function to reduce COMBINED_BINARY size (me)
+ Drop calc_length(), some servers choke on smaller packets (me)
+ Try to clean some fat out (me)
0.9.6 (011001)
+ Added bootp paramaters to server (me)
+ Added bootp paramaters to client (me)
+ Added vendor id to client (me)
+ Better pidfile handling in client and server (me)
+ Added man pages (Matt Kraai <kraai@alumni.carnegiemellon.edu>)
0.9.5 (010914)
+ Fixed $HOME and $PATH env passing (me)
+ Fixed client to only listen for raw packets on correct interface (me)

62
Config.in Normal file
View File

@ -0,0 +1,62 @@
#
# For a description of the syntax of this configuration file,
# see scripts/kbuild/config-language.txt.
#
menu "udhcp Server/Client"
config CONFIG_UDHCPD
bool "udhcp Server (udhcpd)"
default n
help
uDHCPd is a DHCP server geared primarily toward embedded systems,
while striving to be fully functional and RFC compliant.
See http://udhcp.busybox.net for further details.
config CONFIG_UDHCPC
bool "udhcp Client (udhcpc)"
default n
help
uDHCPc is a DHCP client geared primarily toward embedded systems,
while striving to be fully functional and RFC compliant.
The udhcp client negotiates a lease with the DHCP server and
notifies a set of scripts when a lease is obtained or lost.
See http://udhcp.busybox.net for further details.
config CONFIG_DUMPLEASES
bool "Lease display utility (dumpleases)"
default n
depends on CONFIG_UDHCPD
help
dumpleases displays the leases written out by the udhcpd server.
Lease times are stored in the file by time remaining in lease, or
by the absolute time that it expires in seconds from epoch.
See http://udhcp.busybox.net for further details.
config CONFIG_FEATURE_UDHCP_SYSLOG
bool " Log udhcp messages to syslog (instead of stdout)"
default n
depends on CONFIG_UDHCPD || CONFIG_UDHCPC
help
If selected, udhcpd will log all its messages to syslog, otherwise,
it will attempt to log them to stdout.
See http://udhcp.busybox.net for further details.
config CONFIG_FEATURE_UDHCP_DEBUG
bool " Compile udhcp with noisy debugging messages"
default n
depends on CONFIG_UDHCPD || CONFIG_UDHCPC
help
If selected, udhcpd will output extra debugging output. If using
this option, compile uDHCP with "-g", and do not fork the daemon to
the background.
See http://udhcp.busybox.net for further details.
endmenu

View File

@ -1,98 +1,5 @@
# udhcp makefile
prefix=/usr
SBINDIR=/sbin
USRSBINDIR=${prefix}/sbin
USRBINDIR=${prefix}/bin
USRSHAREDIR=${prefix}/share
# Uncomment this to get a shared binary. Call as udhcpd for the server,
# and udhcpc for the client
#COMBINED_BINARY=1
# Uncomment this for extra output and to compile with debugging symbols
#DEBUG=1
# Uncomment this to output messages to syslog, otherwise, messages go to stdout
CFLAGS += -DSYSLOG
#CROSS_COMPILE=arm-uclibc-
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)gcc
INSTALL = install
VER := 0.9.5
OBJS_SHARED = options.o socket.o packet.o
DHCPD_OBJS = dhcpd.o arpping.o files.o leases.o serverpacket.o
DHCPC_OBJS = dhcpc.o clientpacket.o script.o
ifdef COMBINED_BINARY
EXEC1 = udhcpd
OBJS1 = $(DHCPD_OBJS) $(DHCPC_OBJS) $(OBJS_SHARED) frontend.o
CFLAGS += -DCOMBINED_BINARY
ifneq ($(wildcard ../../networking/Config.in),)
include Makefile.busybox
else
EXEC1 = udhcpd
OBJS1 = $(DHCPD_OBJS) $(OBJS_SHARED)
EXEC2 = udhcpc
OBJS2 = $(DHCPC_OBJS) $(OBJS_SHARED)
include Makefile.udhcp
endif
EXEC3 = dumpleases
OBJS3 = dumpleases.o
BOOT_PROGRAMS = udhcpc
DAEMONS = udhcpd
COMMANDS = dumpleases
ifdef SYSLOG
CFLAGS += -DSYSLOG
endif
CFLAGS += -W -Wall -Wstrict-prototypes -DVERSION='"$(VER)"'
ifdef DEBUG
CFLAGS += -g -DDEBUG
else
CFLAGS += -Os -fomit-frame-pointer
STRIP=-s
endif
all: $(EXEC1) $(EXEC2) $(EXEC3)
$(OBJS1) $(OBJS2) $(OBJS3): *.h Makefile
$(EXEC1) $(EXEC2) $(EXEC3): Makefile
.c.o:
$(CC) -c $(CFLAGS) $<
$(EXEC1): $(OBJS1)
$(LD) $(LDFLAGS) $(OBJS1) -o $(EXEC1)
$(EXEC2): $(OBJS2)
$(LD) $(LDFLAGS) $(OBJS2) -o $(EXEC2)
$(EXEC3): $(OBJS3)
$(LD) $(LDFLAGS) $(OBJS3) -o $(EXEC3)
install: all
$(INSTALL) $(STRIP) $(DAEMONS) $(USRSBINDIR)
$(INSTALL) $(STRIP) $(COMMANDS) $(USRBINDIR)
ifdef COMBINED_BINARY
ln -sf $(USRSBINDIR)/$(DAEMONS) $(SBINDIR)/$(BOOT_PROGRAMS)
else
$(INSTALL) $(STRIP) $(BOOT_PROGRAMS) $(SBINDIR)
endif
mkdir -p $(USRSHAREDIR)/udhcpc
for name in bound deconfig renew script ; do \
$(INSTALL) samples/sample.$$name \
$(USRSHAREDIR)/udhcpc/default.$$name ; \
done
clean:
-rm -f udhcpd udhcpc dumpleases *.o core

36
Makefile.busybox Normal file
View File

@ -0,0 +1,36 @@
# Makefile for busybox
#
# Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
ifndef top_srcdir
top_srcdir=../..
endif
ifndef top_builddir
top_builddir=../..
endif
srcdir=$(top_srcdir)/networking/udhcp
UDHCP_DIR:=./
include $(top_builddir)/Rules.mak
include $(top_builddir)/.config
include Makefile.in
all: $(libraries-y)
-include $(top_builddir)/.depend
clean:
rm -f *.o *.a $(AR_TARGET)

53
Makefile.in Normal file
View File

@ -0,0 +1,53 @@
# Makefile for busybox
#
# Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
#
# Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
#
UDHCP_AR:=udhcp.a
ifndef $(UDHCP_DIR)
UDHCP_DIR:=$(top_builddir)/networking/udhcp/
endif
srcdir=$(top_srcdir)/networking/udhcp
#ok, so I forgot how to do an or, but this is a quick and dirty hack
ifeq ($(strip $(CONFIG_UDHCPC)),y)
CONFIG_UDHCP_SHARED=y
else
ifeq ($(strip $(CONFIG_UDHCPD)),y)
CONFIG_UDHCP_SHARED=y
else
CONFIG_UDHCP_SHARED=n
endif
endif
UDHCP-y:=
UDHCP-$(CONFIG_UDHCP_SHARED) += common.c options.c packet.c pidfile.c \
signalpipe.c socket.c
UDHCP-$(CONFIG_UDHCPC) += dhcpc.c clientpacket.c clientsocket.c \
script.c
UDHCP-$(CONFIG_UDHCPD) += dhcpd.c arpping.c files.c leases.c \
serverpacket.c static_leases.c
UDHCP-$(CONFIG_DUMPLEASES) += dumpleases.c
UDHCP_OBJS:=$(patsubst %.c,$(UDHCP_DIR)%.o, $(UDHCP-y))
ifneq ($(strip $(UDHCP-y)),)
libraries-y+=$(UDHCP_DIR)$(UDHCP_AR)
endif
UDHCP-y:=$(patsubst %,$(srcdir)/%,$(UDHCP-y))
UDHCP-a:=$(wildcard $(srcdir)/*.c)
APPLET_SRC-y+=$(UDHCP-y)
APPLET_SRC-a+=$(UDHCP-a)
UDHCP_INCLUDES:=$(srcdir)
APPLETS_DEFINE-y+=-DIN_BUSYBOX -I$(UDHCP_INCLUDES)
APPLETS_DEFINE-a+=-DIN_BUSYBOX -I$(UDHCP_INCLUDES)
$(UDHCP_DIR)$(UDHCP_AR): $(UDHCP_OBJS)
$(do_ar)
$(UDHCP_OBJS): $(UDHCP_DIR)%.o : $(srcdir)/%.c
$(compile.c) -DIN_BUSYBOX

110
Makefile.udhcp Normal file
View File

@ -0,0 +1,110 @@
# udhcp makefile
DESTDIR =
prefix = /usr
SBINDIR = /sbin
USRSBINDIR = $(DESTDIR)$(prefix)/sbin
USRBINDIR = $(DESTDIR)$(prefix)/bin
USRSHAREDIR = $(DESTDIR)$(prefix)/share
# Uncomment this to get a shared binary. Call as udhcpd for the server,
# and udhcpc for the client
#COMBINED_BINARY=1
# Uncomment this for extra output and to compile with debugging symbols
#UDHCP_DEBUG=1
# Uncomment this to output messages to syslog, otherwise, messages go to stdout
#UDHCP_SYSLOG=1
# Set to the prefix of your cross-compiler
#CROSS_COMPILE=arm-uclibc-
#---------------------------------------------------#
# You should not have to change anything below here #
#---------------------------------------------------#
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)gcc
INSTALL = install
OBJS_SHARED = common.o options.o packet.o pidfile.o signalpipe.o socket.o
DHCPD_OBJS = dhcpd.o arpping.o files.o leases.o serverpacket.o static_leases.o
DHCPC_OBJS = dhcpc.o clientpacket.o clientsocket.o script.o
ifdef COMBINED_BINARY
EXEC1 = udhcpd
OBJS1 = $(DHCPD_OBJS) $(DHCPC_OBJS) $(OBJS_SHARED) frontend.o
CFLAGS += -DCOMBINED_BINARY
else
EXEC1 = udhcpd
OBJS1 = $(DHCPD_OBJS) $(OBJS_SHARED)
EXEC2 = udhcpc
OBJS2 = $(DHCPC_OBJS) $(OBJS_SHARED)
endif
EXEC3 = dumpleases
OBJS3 = dumpleases.o
BOOT_PROGRAM = udhcpc
DAEMON = udhcpd
COMMAND = dumpleases
ifdef UDHCP_SYSLOG
CFLAGS += -DUDHCP_SYSLOG
endif
CFLAGS += -W -Wall -Wstrict-prototypes -D_GNU_SOURCE
ifdef UDHCP_DEBUG
CFLAGS += -g -DUDHCP_DEBUG
STRIP=true
else
CFLAGS += -Os -fomit-frame-pointer
STRIP=$(CROSS_COMPILE)strip
endif
all: $(EXEC1) $(EXEC2) $(EXEC3)
$(STRIP) --remove-section=.note --remove-section=.comment $(EXEC1) $(EXEC2) $(EXEC3)
$(OBJS1) $(OBJS2) $(OBJS3): *.h Makefile
$(EXEC1) $(EXEC2) $(EXEC3): Makefile
.c.o:
$(CC) -c $(CFLAGS) $<
$(EXEC1): $(OBJS1)
$(LD) $(CFLAGS) $(LDFLAGS) $(OBJS1) -o $(EXEC1)
$(EXEC2): $(OBJS2)
$(LD) $(CFLAGS) $(LDFLAGS) $(OBJS2) -o $(EXEC2)
$(EXEC3): $(OBJS3)
$(LD) $(CFLAGS) $(LDFLAGS) $(OBJS3) -o $(EXEC3)
install: all
mkdir -p $(USRSBINDIR) $(USRBINDIR)
$(INSTALL) -m 755 $(DAEMON) $(USRSBINDIR)
$(INSTALL) -m 755 $(COMMAND) $(USRBINDIR)
ifdef COMBINED_BINARY
ln -sf $(DAEMON) $(USRSBINDIR)/$(BOOT_PROGRAM)
else
$(INSTALL) -m 755 $(BOOT_PROGRAM) $(USRSBINDIR)
endif
mkdir -p $(USRSHAREDIR)/udhcpc
for name in bound deconfig nak renew script ; do \
$(INSTALL) -m 755 samples/sample.$$name \
$(USRSHAREDIR)/udhcpc/default.$$name ; \
done
mkdir -p $(USRSHAREDIR)/man/man1
$(INSTALL) -m 644 dumpleases.1 $(USRSHAREDIR)/man/man1
mkdir -p $(USRSHAREDIR)/man/man5
$(INSTALL) -m 644 udhcpd.conf.5 $(USRSHAREDIR)/man/man5
mkdir -p $(USRSHAREDIR)/man/man8
$(INSTALL) -m 644 udhcpc.8 udhcpd.8 $(USRSHAREDIR)/man/man8
clean:
-rm -f udhcpd udhcpc dumpleases *.o *.a core

172
README
View File

@ -5,151 +5,49 @@ The udhcp server/client package is primarily geared towards embedded
systems. It does however, strive to be fully functional, and RFC
compliant.
udhcp server (udhcpd)
--------------------
The udhcp server employs a number of simple config files:
udhcpd.leased
------------
The udhcpd.leases behavior is designed for an embedded system. The
file is written either every auto_time seconds, or when a SIGUSR1
is received. When the file is written, a script can be optionally
called to commit the file to flash. Lease times are stored in the
file by time remaining in lease (for systems without clock that works
when there is no power), or by the absolute time that it expires in
seconds from epoch. In the remainig format, expired leases are stored
as zero. The file is of the format:
16 byte MAC
4 byte ip address
u32 expire time
16 byte MAC
4 byte ip address
u32 expire time
.
etc.
example: hexdump udhcpd.leases
0000000 1000 c95a 27d9 0000 0000 0000 0000 0000
0000010 a8c0 150a 0d00 2d29 5000 23fc 8566 0000
0000020 0000 0000 0000 0000 a8c0 140a 0d00 4e29
0000030
udhcpd.conf
----------
The format is fairly simple, there is a sample file with all the
available options and comments describing them in samples/udhcpd.conf
udhcp client (udhcpc)
--------------------
The udhcp client negotiates a lease with the DHCP server and notifies
a set of scripts when a leases is obtained or lost. The command line
options for the udhcp client are:
-c, --clientid=CLIENTID Client identifier
-H, --hostname=HOSTNAME Client hostname
-f, --foreground Do not fork after getting lease
-i, --interface=INTERFACE Interface to use (default: eth0)
-n, --now Exit with failure if lease cannot be
immediately negotiated.
-p, --pidfile=file Store process ID of daemon in file
-q, --quit Quit after obtaining lease
-r, --request=IP IP address to request (default: none)
-s, --script=file Run file at dhcp events (default:
/usr/share/udhcpc/default.script)
-v, --version Display version
If the requested IP address cannot be obtained, the client accepts the
address that the server offers.
When an event occurs, udhcpc calls the action script. The script by
default is /usr/share/udhcpc/default.script but this can be changed via
the command line arguments. The three possible arguments to the script
are:
deconfig: This argument is used when udhcpc starts, and
when a leases is lost. The script should put the interface in an
up, but deconfigured state, ie: ifconfig $interface 0.0.0.0.
bound: This argument is used when udhcpc moves from an
unbound, to a bound state. All of the paramaters are set in
enviromental variables, The script should configure the interface,
and set any other relavent parameters (default gateway, dns server,
etc).
renew: This argument is used when a DHCP lease is renewed. All of
the paramaters are set in enviromental variables. This argument is
used when the interface is already configured, so the IP address,
will not change, however, the other DHCP paramaters, such as the
default gateway, subnet mask, and dns server may change.
The paramaters for enviromental variables are as follows:
$HOME - The set $HOME env or "/"
$PATH - the set $PATH env or "/bin:/usr/bin:/sbin:/usr/sbin"
$1 - What action the script should perform
interface - The interface this was obtained on
ip - The obtained IP
subnet - The assigend subnet mask
timezone - Offset in seconds from UTC
router - A list of routers
timesvr - A list of time servers
namesvr - A list of IEN 116 name servers
dns - A list of DNS server
logsvr - A list of MIT-LCS UDP log servers
cookiesvr - A list of RFC 865 cookie servers
lprsvr - A list of LPR servers
hostname - The assigned hostname
bootsize - The length in 512 octect blocks of the bootfile
domain - The domain name of the network
swapsvr - The IP address of the client's swap server
rootpath - The path name of the client's root disk
ipttl - The TTL to use for this network
mtu - The MTU to use for this network
broadcast - The broadcast address for this network
ntpsrv - A list of NTP servers
wins - A list of WINS servers
lease - The lease time, in seconds
dhcptype - DHCP message type (safely ignored)
serverid - The IP of the server
tftp - The TFTP server name
bootfile - The bootfile name
additional options are easily added in options.c.
udhcpc also responds to SIGUSR1 and SIGUSR2. SIGUSR1 will force a renew state,
and SIGUSR2 will force a release of the current lease, and cause udhcpc to
go into an inactive state (until it is killed, or receives a SIGUSR1).
compile time options
-------------------
The Makefile contains three of the compile time options:
DEBUG: If DEBUG is defined, udhcpd will output extra debugging
output, compile with -g, and not fork to the background when run.
SYSLOG: If SYSLOG is defined, udhcpd will log all its messages
syslog, otherwise, it will attempt to log them to stdout.
UDHCP_DEBUG: If UDHCP_DEBUG is defined, udhcpd will output extra
debugging output, compile with -g, and not fork to the background when
run.
UDHCP_SYSLOG: If UDHCP_SYSLOG is defined, udhcpd will log all its
messages syslog, otherwise, it will attempt to log them to stdout.
COMBINED_BINARY: If COMBINED_BINARY is define, one binary, udhcpd,
is created. If called as udhcpd, the dhcp server will be started.
If called as udhcpc, the dhcp client will be started.
dhcpd.h contains the other two compile time options:
dhcpd.h contains the other three compile time options:
LEASE_TIME: The default lease time if not specified in the config
file.
DHCPD_CONFIG_FILE: What config file to use.
LEASES_FILE: The default file for storing leases.
DHCPD_CONFIG_FILE: The defualt config file to use.
options.c contains a set of dhcp options for the client:
name[10]: The name of the option as it will appear in scripts
flags: The type of option, as well as if it will be requested
by the client (OPTION_REQ)
code: The DHCP code for this option
busybox drop-in
--------------
udhcp is now a drop-in component for busybox (http://busybox.net).
To update busybox to the latest revision, simply do a:
cp *.[ch] README AUTHORS COPYING ChangeLog TODO \
<busybox_source>/networking/udhcp
The only two files udhcp does not provide are config.in and
Makefile.in, so these may need to be updated from time to time.

17
README.dumpleases Normal file
View File

@ -0,0 +1,17 @@
udhcp lease dump (dumpleases)
----------------------------
dumpleases displays the leases written out by the udhcpd server. Lease
times are stored in the file by time remaining in lease (for systems
without clock that works when there is no power), or by the absolute
time that it expires in seconds from epoch. dumpleases accepts the
following command line options:
-a, --absolute Interpret lease times as expiration time.
-r, --remaining Interpret lease times as remaining time.
-f, --file=FILE Read lease information from FILE.
-h, --help Display help.
Note that if udhcpd has not written a leases file recently, the output
of may not be up to date.

145
README.udhcpc Normal file
View File

@ -0,0 +1,145 @@
udhcp client (udhcpc)
--------------------
The udhcp client negotiates a lease with the DHCP server and notifies
a set of scripts when a leases is obtained or lost.
command line options
-------------------
The command line options for the udhcp client are:
-c, --clientid=CLIENTID Set client identifier. Type is first char.
-C, --clientid-none Suppress default client identifier
-V, --vendorclass=CLASSID Set vendor class identifier
-H, --hostname=HOSTNAME Client hostname
-h, Alias for -H
-F, --fqdn=FQDN Client fully qualified domain name
-f, --foreground Do not fork after getting lease
-b, --background Fork to background if lease cannot be
immediately negotiated.
-i, --interface=INTERFACE Interface to use (default: eth0)
-n, --now Exit with failure if lease cannot be
immediately negotiated.
-p, --pidfile=file Store process ID of daemon in file
-q, --quit Quit after obtaining lease
-r, --request=IP IP address to request (default: none)
-s, --script=file Run file at dhcp events (default:
/usr/share/udhcpc/default.script)
-t, --retries=NUM Send up to NUM request packets
-v, --version Display version
If the requested IP address cannot be obtained, the client accepts the
address that the server offers.
udhcp client scripts
-------------------
When an event occurs, udhcpc calls the action script. udhcpc never does
any configuration of the network interface itself, but instead relies on
a set of scripts. The script by default is
/usr/share/udhcpc/default.script but this can be changed via the command
line arguments. The three possible arguments to the script are:
deconfig: This argument is used when udhcpc starts, and
when a leases is lost. The script must put the interface in an
up, but deconfigured state, ie: ifconfig $interface 0.0.0.0.
bound: This argument is used when udhcpc moves from an
unbound, to a bound state. All of the paramaters are set in
enviromental variables, The script should configure the interface,
and set any other relavent parameters (default gateway, dns server,
etc).
renew: This argument is used when a DHCP lease is renewed. All of
the paramaters are set in enviromental variables. This argument is
used when the interface is already configured, so the IP address,
will not change, however, the other DHCP paramaters, such as the
default gateway, subnet mask, and dns server may change.
nak: This argument is used with udhcpc receives a NAK message.
The script with the deconfig argument will be called directly
afterwards, so no changes to the network interface are neccessary.
This hook is provided for purely informational purposes (the
message option may contain a reason for the NAK).
The paramaters for enviromental variables are as follows:
$HOME - The set $HOME env or "/"
$PATH - the set $PATH env or "/bin:/usr/bin:/sbin:/usr/sbin"
$1 - What action the script should perform
interface - The interface this was obtained on
ip - The obtained IP
mask - The number of bits in the netmask (ie: 24)
siaddr - The bootp next server option
sname - The bootp server name option
boot_file - The bootp boot file option
subnet - The assigend subnet mask
timezone - Offset in seconds from UTC
router - A list of routers
timesvr - A list of time servers
namesvr - A list of IEN 116 name servers
dns - A list of DNS server
logsvr - A list of MIT-LCS UDP log servers
cookiesvr - A list of RFC 865 cookie servers
lprsvr - A list of LPR servers
hostname - The assigned hostname
bootsize - The length in 512 octect blocks of the bootfile
domain - The domain name of the network
swapsvr - The IP address of the client's swap server
rootpath - The path name of the client's root disk
ipttl - The TTL to use for this network
mtu - The MTU to use for this network
broadcast - The broadcast address for this network
ntpsrv - A list of NTP servers
wins - A list of WINS servers
lease - The lease time, in seconds
dhcptype - DHCP message type (safely ignored)
serverid - The IP of the server
message - Reason for a DHCPNAK
tftp - The TFTP server name
bootfile - The bootfile name
additional options are easily added in options.c.
note on udhcpc's random seed
---------------------------
udhcpc will seed its random number generator (used for generating xid's)
by reading /dev/urandom. If you have a lot of embedded systems on the same
network, with no entropy, you can either seed /dev/urandom by a method of
your own, or doing the following on startup:
ifconfig eth0 > /dev/urandom
in order to seed /dev/urandom with some data (mac address) unique to your
system. If reading /dev/urandom fails, udhcpc will fall back to its old
behavior of seeding with time(0).
signals accepted by udhcpc
-------------------------
udhcpc also responds to SIGUSR1 and SIGUSR2. SIGUSR1 will force a renew state,
and SIGUSR2 will force a release of the current lease, and cause udhcpc to
go into an inactive state (until it is killed, or receives a SIGUSR1). You do
not need to sleep between sending signals, as signals received are processed
sequencially in the order they are received.
compile time options
-------------------
options.c contains a set of dhcp options for the client:
name[10]: The name of the option as it will appear in scripts
flags: The type of option, as well as if it will be requested
by the client (OPTION_REQ)
code: The DHCP code for this option

59
README.udhcpd Normal file
View File

@ -0,0 +1,59 @@
udhcp server (udhcpd)
--------------------
The only command line argument to udhcpd is an optional specifed
config file. If no config file is specified, udhcpd uses the default
config file, /etc/udhcpd.conf. Ex:
udhcpd /etc/udhcpd.eth1.conf
The udhcp server employs a number of simple config files:
udhcpd.leases
------------
The udhcpd.leases behavior is designed for an embedded system. The
file is written either every auto_time seconds, or when a SIGUSR1
is received (the auto_time timer restarts if a SIGUSR1 is received).
If you send a SIGTERM to udhcpd directly after a SIGUSR1, udhcpd will
finish writing the leases file and wait for the aftermentioned script
to be executed and finish before quiting, so you do not need to sleep
between sending signals. When the file is written, a script can be
optionally called to commit the file to flash. Lease times are stored
in the file by time remaining in lease (for systems without clock
that works when there is no power), or by the absolute time that it
expires in seconds from epoch. In the remaining format, expired leases
are stored as zero. The file is of the format:
16 byte MAC
4 byte ip address
u32 expire time
16 byte MAC
4 byte ip address
u32 expire time
.
etc.
example: hexdump udhcpd.leases
0000000 1000 c95a 27d9 0000 0000 0000 0000 0000
0000010 a8c0 150a 0d00 2d29 5000 23fc 8566 0000
0000020 0000 0000 0000 0000 a8c0 140a 0d00 4e29
0000030
udhcpd.conf
----------
The format is fairly simple, there is a sample file with all the
available options and comments describing them in samples/udhcpd.conf
compile time options
-------------------
dhcpd.h contains the other two compile time options:
LEASE_TIME: The default lease time if not specified in the config
file.
DHCPD_CONFIG_FILE: The defualt config file to use.

18
TODO
View File

@ -1,12 +1,16 @@
TODO
----
+ Make sure get_raw_packet only accepts packets on the specified interface
+ FIXED: little/big endian issue probable cause for segmentation fault on i86 platform
+ N/A: makeiplist basic functionality works.. more user friendly interface required
+ REPLACED: browseiplist incomplete
+ Check for valid IP, netmask, hostname, paths, strings, etc
+ Integrade README.*'s with manpages
+ using time(0) breaks if the system clock changes, find a portable solution
+ make failure of reading functions revert to previous value, not the default
+ sanity code for option[OPT_LEN]
+ fix aliasing (ie: eth0:0)
+ better standard linux distro support
+ make config file a command line option
+ IMPLEMENTED: make forking a command line option
+ make sure packet generation works on a wide varitey of arches
+ Interoperability testing
+ Hooks within the DHCP server
+ Hooks within the DHCP server
* Server notification when a lease is added/removed
+ Additional bootp support in client/server
+ Make serverid option in server configurable
+ Possibly add failure message to DHCP NAK

149
arpping.c
View File

@ -5,127 +5,102 @@
* by Yoichi Hariguchi <yoichi@fore.com>
*/
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/if_ether.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "dhcpd.h"
#include "debug.h"
#include "arpping.h"
#include "common.h"
/* local prototypes */
int arpCheck(u_long inaddr, struct ifinfo *ifbuf, long timeout);
void mkArpMsg(int opcode, u_long tInaddr, u_char *tHaddr, u_long sInaddr, u_char *sHaddr, struct arpMsg *msg);
int openRawSocket (int *s, u_short type);
/* args: yiaddr - what IP to ping (eg. on the NETtel cb189701)
* retn: 1 addr free
/* args: yiaddr - what IP to ping
* ip - our ip
* mac - our arp address
* interface - interface to use
* retn: 1 addr free
* 0 addr used
* -1 error
*/
int arpping(u_int32_t yiaddr, u_int32_t ip, char *arp, char *interface) {
struct ifinfo ifbuf;
* -1 error
*/
strcpy(ifbuf.ifname, interface);
ifbuf.addr = ip;
ifbuf.mask = 0x0;
ifbuf.bcast = 0x0;
memcpy(ifbuf.haddr, arp, 6);
ifbuf.flags = 0;
return arpCheck(yiaddr, &ifbuf, 2);
}
/* FIXME: match response against chaddr */
int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface)
{
int arpCheck(u_long inaddr, struct ifinfo *ifbuf, long timeout) {
int s; /* socket */
int rv; /* return value */
int timeout = 2;
int optval = 1;
int s; /* socket */
int rv = 1; /* return value */
struct sockaddr addr; /* for interface name */
struct arpMsg arp;
fd_set fdset;
fd_set fdset;
struct timeval tm;
time_t prevTime;
time_t prevTime;
rv = 1;
openRawSocket(&s, ETH_P_ARP);
if ((s = socket (PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) {
#ifdef IN_BUSYBOX
LOG(LOG_ERR, bb_msg_can_not_create_raw_socket);
#else
LOG(LOG_ERR, "Could not open raw socket");
#endif
return -1;
}
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) == -1) {
LOG(LOG_ERR, "Could not setsocketopt on raw socket");
close(s);
return -1;
}
/* send arp request */
mkArpMsg(ARPOP_REQUEST, inaddr, NULL, ifbuf->addr, ifbuf->haddr, &arp);
bzero(&addr, sizeof(addr));
strcpy(addr.sa_data, ifbuf->ifname);
if ( sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0 ) rv = 0;
memset(&arp, 0, sizeof(arp));
memcpy(arp.h_dest, MAC_BCAST_ADDR, 6); /* MAC DA */
memcpy(arp.h_source, mac, 6); /* MAC SA */
arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
arp.htype = htons(ARPHRD_ETHER); /* hardware type */
arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
arp.hlen = 6; /* hardware address length */
arp.plen = 4; /* protocol address length */
arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
memcpy(arp.sInaddr, &ip, sizeof(ip)); /* source IP address */
memcpy(arp.sHaddr, mac, 6); /* source hardware address */
memcpy(arp.tInaddr, &yiaddr, sizeof(yiaddr)); /* target IP address */
memset(&addr, 0, sizeof(addr));
strcpy(addr.sa_data, interface);
if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0)
rv = 0;
/* wait arp reply, and check it */
tm.tv_usec = 0;
time(&prevTime);
while ( timeout > 0 ) {
prevTime = uptime();
while (timeout > 0) {
FD_ZERO(&fdset);
FD_SET(s, &fdset);
tm.tv_sec = timeout;
if ( select(s+1, &fdset, (fd_set *)NULL, (fd_set *)NULL, &tm) < 0 ) {
DEBUG(LOG_ERR, "Error on ARPING request: %s", sys_errlist[errno]);
tm.tv_sec = timeout;
if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) {
DEBUG(LOG_ERR, "Error on ARPING request: %m");
if (errno != EINTR) rv = 0;
} else if ( FD_ISSET(s, &fdset) ) {
} else if (FD_ISSET(s, &fdset)) {
if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0;
if(arp.operation == htons(ARPOP_REPLY) &&
bcmp(arp.tHaddr, ifbuf->haddr, 6) == 0 &&
*((u_int *)arp.sInaddr) == inaddr ) {
if (arp.operation == htons(ARPOP_REPLY) &&
memcmp(arp.tHaddr, mac, 6) == 0 &&
*((uint32_t *) arp.sInaddr) == yiaddr) {
DEBUG(LOG_INFO, "Valid arp reply receved for this address");
rv = 0;
break;
}
}
timeout -= time(NULL) - prevTime;
time(&prevTime);
timeout -= uptime() - prevTime;
prevTime = uptime();
}
close(s);
DEBUG(LOG_INFO, "%salid arp replies for this address", rv ? "No v" : "V");
DEBUG(LOG_INFO, "%salid arp replies for this address", rv ? "No v" : "V");
return rv;
}
void mkArpMsg(int opcode, u_long tInaddr, u_char *tHaddr,
u_long sInaddr, u_char *sHaddr, struct arpMsg *msg) {
bzero(msg, sizeof(*msg));
bcopy(MAC_BCAST_ADDR, msg->ethhdr.h_dest, 6); /* MAC DA */
bcopy(sHaddr, msg->ethhdr.h_source, 6); /* MAC SA */
msg->ethhdr.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
msg->htype = htons(ARPHRD_ETHER); /* hardware type */
msg->ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
msg->hlen = 6; /* hardware address length */
msg->plen = 4; /* protocol address length */
msg->operation = htons(opcode); /* ARP op code */
*((u_int *)msg->sInaddr) = sInaddr; /* source IP address */
bcopy(sHaddr, msg->sHaddr, 6); /* source hardware address */
*((u_int *)msg->tInaddr) = tInaddr; /* target IP address */
if ( opcode == ARPOP_REPLY )
bcopy(tHaddr, msg->tHaddr, 6); /* target hardware address */
}
int openRawSocket (int *s, u_short type) {
int optval = 1;
if((*s = socket (AF_INET, SOCK_PACKET, htons (type))) == -1) {
LOG(LOG_ERR, "Could not open raw socket");
return -1;
}
if(setsockopt (*s, SOL_SOCKET, SO_BROADCAST, &optval, sizeof (optval)) == -1) {
LOG(LOG_ERR, "Could not setsocketopt on raw socket");
return -1;
}
return 0;
}

View File

@ -5,39 +5,31 @@
#ifndef ARPPING_H
#define ARPPING_H
#define MAC_BCAST_ADDR "\xff\xff\xff\xff\xff\xff"
#include <netinet/if_ether.h>
#include <net/if_arp.h>
#include <net/if.h>
#include <netinet/in.h>
struct arpMsg {
struct ethhdr ethhdr; /* Ethernet header */
u_short htype; /* hardware type (must be ARPHRD_ETHER) */
u_short ptype; /* protocol type (must be ETH_P_IP) */
u_char hlen; /* hardware address length (must be 6) */
u_char plen; /* protocol address length (must be 4) */
u_short operation; /* ARP opcode */
u_char sHaddr[6]; /* sender's hardware address */
u_char sInaddr[4]; /* sender's IP address */
u_char tHaddr[6]; /* target's hardware address */
u_char tInaddr[4]; /* target's IP address */
u_char pad[18]; /* pad for min. Ethernet payload (60 bytes) */
};
struct ifinfo {
char ifname[IFNAMSIZ];
u_long addr; /* network byte order */
u_long mask; /* network byte order */
u_long bcast; /* network byte order */
u_char haddr[6];
short flags;
};
/* Ethernet header */
u_char h_dest[6]; /* destination ether addr */
u_char h_source[6]; /* source ether addr */
u_short h_proto; /* packet type ID field */
/* ARP packet */
uint16_t htype; /* hardware type (must be ARPHRD_ETHER) */
uint16_t ptype; /* protocol type (must be ETH_P_IP) */
uint8_t hlen; /* hardware address length (must be 6) */
uint8_t plen; /* protocol address length (must be 4) */
uint16_t operation; /* ARP opcode */
uint8_t sHaddr[6]; /* sender's hardware address */
uint8_t sInaddr[4]; /* sender's IP address */
uint8_t tHaddr[6]; /* target's hardware address */
uint8_t tInaddr[4]; /* target's IP address */
uint8_t pad[18]; /* pad for min. Ethernet payload (60 bytes) */
} ATTRIBUTE_PACKED;
/* function prototypes */
int arpping(u_int32_t yiaddr, u_int32_t ip, char *arp, char *interface);
int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *arp, char *interface);
#endif

View File

@ -18,11 +18,11 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <string.h>
#include <sys/socket.h>
#include <features.h>
#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
#if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION
#include <netpacket/packet.h>
#include <net/ethernet.h>
#else
@ -35,13 +35,14 @@
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include "dhcpd.h"
#include "packet.h"
#include "clientpacket.h"
#include "options.h"
#include "dhcpc.h"
#include "debug.h"
#include "common.h"
/* Create a random xid */
@ -49,7 +50,16 @@ unsigned long random_xid(void)
{
static int initialized;
if (!initialized) {
srand(time(0));
int fd;
unsigned long seed;
fd = open("/dev/urandom", 0);
if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) {
LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %m");
seed = time(0);
}
if (fd >= 0) close(fd);
srand(seed);
initialized++;
}
return rand();
@ -59,27 +69,31 @@ unsigned long random_xid(void)
/* initialize a packet with the proper defaults */
static void init_packet(struct dhcpMessage *packet, char type)
{
memset(packet, 0, sizeof(struct dhcpMessage));
packet->op = BOOTREQUEST;
packet->htype = ETH_10MB;
packet->hlen = ETH_10MB_LEN;
packet->cookie = htonl(DHCP_MAGIC);
packet->options[0] = DHCP_END;
init_header(packet, type);
memcpy(packet->chaddr, client_config.arp, 6);
add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
if (client_config.clientid) add_option_string(packet->options, client_config.clientid);
if (client_config.clientid)
add_option_string(packet->options, client_config.clientid);
if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
if (client_config.fqdn) add_option_string(packet->options, client_config.fqdn);
add_option_string(packet->options, client_config.vendorclass);
}
/* Add a paramater request list for stubborn DHCP servers */
/* Add a parameter request list for stubborn DHCP servers. Pull the data
* from the struct in options.c. Don't do bounds checking here because it
* goes towards the head of the packet. */
static void add_requests(struct dhcpMessage *packet)
{
char request_list[] = {DHCP_PARAM_REQ, 0, PARM_REQUESTS};
request_list[OPT_LEN] = sizeof(request_list) - 2;
add_option_string(packet->options, request_list);
int end = end_option(packet->options);
int i, len = 0;
packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
for (i = 0; dhcp_options[i].code; i++)
if (dhcp_options[i].flags & OPTION_REQ)
packet->options[end + OPT_DATA + len++] = dhcp_options[i].code;
packet->options[end + OPT_LEN] = len;
packet->options[end + OPT_DATA + len] = DHCP_END;
}
@ -91,11 +105,11 @@ int send_discover(unsigned long xid, unsigned long requested)
init_packet(&packet, DHCPDISCOVER);
packet.xid = xid;
if (requested)
add_simple_option(packet.options, DHCP_REQUESTED_IP, ntohl(requested));
add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
add_requests(&packet);
LOG(LOG_DEBUG, "Sending discover...");
return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
}
@ -109,16 +123,13 @@ int send_selecting(unsigned long xid, unsigned long server, unsigned long reques
init_packet(&packet, DHCPREQUEST);
packet.xid = xid;
/* expects host order */
add_simple_option(packet.options, DHCP_REQUESTED_IP, ntohl(requested));
add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
add_simple_option(packet.options, DHCP_SERVER_ID, server);
/* expects host order */
add_simple_option(packet.options, DHCP_SERVER_ID, ntohl(server));
add_requests(&packet);
addr.s_addr = requested;
LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr));
return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
}
@ -133,13 +144,14 @@ int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
packet.xid = xid;
packet.ciaddr = ciaddr;
add_requests(&packet);
LOG(LOG_DEBUG, "Sending renew...");
if (server)
if (server)
ret = kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
else ret = raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
return ret;
}
}
/* Unicasts a DHCP release message */
@ -150,22 +162,22 @@ int send_release(unsigned long server, unsigned long ciaddr)
init_packet(&packet, DHCPRELEASE);
packet.xid = random_xid();
packet.ciaddr = ciaddr;
/* expects host order */
add_simple_option(packet.options, DHCP_REQUESTED_IP, ntohl(ciaddr));
add_simple_option(packet.options, DHCP_SERVER_ID, ntohl(server));
add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
add_simple_option(packet.options, DHCP_SERVER_ID, server);
LOG(LOG_DEBUG, "Sending release...");
return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
}
/* return -1 on errors that are fatal for the socket, -2 for those that aren't */
int get_raw_packet(struct dhcpMessage *payload, int fd)
{
int bytes;
struct udp_dhcp_packet packet;
u_int32_t source, dest;
u_int16_t check;
uint32_t source, dest;
uint16_t check;
memset(&packet, 0, sizeof(struct udp_dhcp_packet));
bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
@ -174,19 +186,27 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
usleep(500000); /* possible down interface, looping condition */
return -1;
}
if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
DEBUG(LOG_INFO, "message too short, ignoring");
return -1;
return -2;
}
if (bytes < ntohs(packet.ip.tot_len)) {
DEBUG(LOG_INFO, "Truncated packet");
return -2;
}
/* ignore any extra garbage bytes */
bytes = ntohs(packet.ip.tot_len);
/* Make sure its the right packet for us, and that it passes sanity checks */
if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
ntohs(packet.ip.tot_len) != bytes || bytes > (int) sizeof(struct udp_dhcp_packet) ||
ntohs(packet.udp.len) != (short) (bytes - sizeof(packet.ip))) {
DEBUG(LOG_INFO, "unrelated/bogus packet");
return -1;
bytes > (int) sizeof(struct udp_dhcp_packet) ||
ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) {
DEBUG(LOG_INFO, "unrelated/bogus packet");
return -2;
}
/* check IP checksum */
@ -196,7 +216,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
DEBUG(LOG_INFO, "bad IP header checksum, ignoring");
return -1;
}
/* verify the UDP checksum by replacing the header with a psuedo header */
source = packet.ip.saddr;
dest = packet.ip.daddr;
@ -208,19 +228,18 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
packet.ip.saddr = source;
packet.ip.daddr = dest;
packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
if (check != checksum(&packet, bytes)) {
if (check && check != checksum(&packet, bytes)) {
DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring");
return -1;
return -2;
}
memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
if (ntohl(payload->cookie) != DHCP_MAGIC) {
LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring");
return -1;
return -2;
}
DEBUG(LOG_INFO, "oooooh!!! got some!");
return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
}
}

View File

@ -1,6 +1,8 @@
#ifndef _CLIENTPACKET_H
#define _CLIENTPACKET_H
#include "packet.h"
unsigned long random_xid(void);
int send_discover(unsigned long xid, unsigned long requested);
int send_selecting(unsigned long xid, unsigned long server, unsigned long requested);

62
clientsocket.c Normal file
View File

@ -0,0 +1,62 @@
/*
* clientsocket.c -- DHCP client socket creation
*
* udhcp client
*
* Russ Dill <Russ.Dill@asu.edu> July 2001
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <features.h>
#if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION
#include <netpacket/packet.h>
#include <net/ethernet.h>
#else
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#endif
#include "clientsocket.h"
#include "common.h"
int raw_socket(int ifindex)
{
int fd;
struct sockaddr_ll sock;
DEBUG(LOG_INFO, "Opening raw socket on ifindex %d", ifindex);
if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
DEBUG(LOG_ERR, "socket call failed: %m");
return -1;
}
sock.sll_family = AF_PACKET;
sock.sll_protocol = htons(ETH_P_IP);
sock.sll_ifindex = ifindex;
if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
DEBUG(LOG_ERR, "bind call failed: %m");
close(fd);
return -1;
}
return fd;
}

7
clientsocket.h Normal file
View File

@ -0,0 +1,7 @@
/* clientsocket.h */
#ifndef _CLIENTSOCKET_H
#define _CLIENTSOCKET_H
int raw_socket(int ifindex);
#endif

135
common.c Normal file
View File

@ -0,0 +1,135 @@
/* vi: set sw=4 ts=4: */
/* common.c
*
* Functions for debugging and logging as well as some other
* simple helper functions.
*
* Russ Dill <Russ.Dill@asu.edu> 2001-2003
* Rewritten by Vladimir Oleynik <dzo@simtreas.ru> (C) 2003
*
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <paths.h>
#include <sys/socket.h>
#include <stdarg.h>
#include "common.h"
#include "pidfile.h"
static int daemonized;
long uptime(void)
{
struct sysinfo info;
sysinfo(&info);
return info.uptime;
}
/*
* This function makes sure our first socket calls
* aren't going to fd 1 (printf badness...) and are
* not later closed by daemon()
*/
static inline void sanitize_fds(void)
{
int zero;
if ((zero = open(_PATH_DEVNULL, O_RDWR, 0)) < 0) return;
while (zero < 3) zero = dup(zero);
close(zero);
}
void background(const char *pidfile)
{
#ifdef __uClinux__
LOG(LOG_ERR, "Cannot background in uclinux (yet)");
#else /* __uClinux__ */
int pid_fd;
/* hold lock during fork. */
pid_fd = pidfile_acquire(pidfile);
if (daemon(0, 0) == -1) { /* bb_xdaemon? */
perror("fork");
exit(1);
}
daemonized++;
pidfile_write_release(pid_fd);
#endif /* __uClinux__ */
}
#ifdef UDHCP_SYSLOG
void udhcp_logging(int level, const char *fmt, ...)
{
va_list p;
va_list p2;
va_start(p, fmt);
__va_copy(p2, p);
if(!daemonized) {
vprintf(fmt, p);
putchar('\n');
}
vsyslog(level, fmt, p2);
va_end(p);
}
#else
static char *syslog_level_msg[] = {
[LOG_EMERG] = "EMERGENCY!",
[LOG_ALERT] = "ALERT!",
[LOG_CRIT] = "critical!",
[LOG_WARNING] = "warning",
[LOG_ERR] = "error",
[LOG_INFO] = "info",
[LOG_DEBUG] = "debug"
};
void udhcp_logging(int level, const char *fmt, ...)
{
va_list p;
va_start(p, fmt);
if(!daemonized) {
printf("%s, ", syslog_level_msg[level]);
vprintf(fmt, p);
putchar('\n');
}
va_end(p);
}
#endif
void start_log_and_pid(const char *client_server, const char *pidfile)
{
int pid_fd;
/* Make sure our syslog fd isn't overwritten */
sanitize_fds();
/* do some other misc startup stuff while we are here to save bytes */
pid_fd = pidfile_acquire(pidfile);
pidfile_write_release(pid_fd);
/* equivelent of doing a fflush after every \n */
setlinebuf(stdout);
#ifdef UDHCP_SYSLOG
openlog(client_server, LOG_PID | LOG_CONS, LOG_LOCAL0);
#endif
udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
}

55
common.h Normal file
View File

@ -0,0 +1,55 @@
/* common.h
*
* Russ Dill <Russ.Dill@asu.edu> September 2001
* Rewritten by Vladimir Oleynik <dzo@simtreas.ru> (C) 2003
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef _COMMON_H
#define _COMMON_H
#include "version.h"
#include "libbb_udhcp.h"
#ifndef UDHCP_SYSLOG
enum syslog_levels {
LOG_EMERG = 0,
LOG_ALERT,
LOG_CRIT,
LOG_WARNING,
LOG_ERR,
LOG_INFO,
LOG_DEBUG
};
#else
#include <syslog.h>
#endif
long uptime(void);
void background(const char *pidfile);
void start_log_and_pid(const char *client_server, const char *pidfile);
void udhcp_logging(int level, const char *fmt, ...);
#define LOG(level, str, args...) udhcp_logging(level, str, ## args)
#ifdef UDHCP_DEBUG
# define DEBUG(level, str, args...) LOG(level, str, ## args)
#else
# define DEBUG(level, str, args...) do {;} while(0)
#endif
#endif

39
debug.h
View File

@ -1,39 +0,0 @@
#ifndef _DEBUG_H
#define _DEBUG_H
#include <stdio.h>
#ifdef SYSLOG
#include <syslog.h>
#endif
#ifdef SYSLOG
# define LOG(level, str, args...) do { printf(str, ## args); \
printf("\n"); \
syslog(level, str, ## args); } while(0)
# define OPEN_LOG(name) openlog(name, 0, 0)
#define CLOSE_LOG() closelog()
#else
# define LOG_EMERG "EMERGENCY!"
# define LOG_ALERT "ALERT!"
# define LOG_CRIT "critical!"
# define LOG_WARNING "warning"
# define LOG_ERR "error"
# define LOG_INFO "info"
# define LOG_DEBUG "debug"
# define LOG(level, str, args...) do { printf("%s, ", level); \
printf(str, ## args); \
printf("\n"); } while(0)
# define OPEN_LOG(name) do {;} while(0)
#define CLOSE_LOG() do {;} while(0)
#endif
#ifdef DEBUG
# undef DEBUG
# define DEBUG(level, str, args...) LOG(level, str, ## args)
# define DEBUGGING
#else
# define DEBUG(level, str, args...) do {;} while(0)
#endif
#endif

510
dhcpc.c
View File

@ -1,27 +1,13 @@
/* dhcpd.c
/* dhcpc.c
*
* udhcp DHCP client
*
* Russ Dill <Russ.Dill@asu.edu> July 2001
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
*/
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/file.h>
#include <unistd.h>
#include <getopt.h>
@ -36,50 +22,61 @@
#include <net/if.h>
#include <errno.h>
#include "common.h"
#include "dhcpd.h"
#include "dhcpc.h"
#include "options.h"
#include "clientpacket.h"
#include "packet.h"
#include "clientsocket.h"
#include "script.h"
#include "socket.h"
#include "debug.h"
#include "signalpipe.h"
static int state;
static unsigned long requested_ip; /* = 0 */
static unsigned long server_addr;
static unsigned long timeout;
static int packet_num; /* = 0 */
static int pid_fd;
static int fd = -1;
#define LISTEN_NONE 0
#define LISTEN_KERNEL 1
#define LISTEN_RAW 2
static int listen_mode = LISTEN_RAW;
#define DEFAULT_SCRIPT "/usr/share/udhcpc/default.script"
static int listen_mode;
struct client_config_t client_config = {
/* Default options. */
abort_if_no_lease: 0,
foreground: 0,
quit_after_lease: 0,
interface: "eth0",
pidfile: NULL,
script: DEFAULT_SCRIPT,
clientid: NULL,
hostname: NULL,
ifindex: 0,
arp: "\0\0\0\0\0\0", /* appease gcc-3.0 */
.abort_if_no_lease = 0,
.foreground = 0,
.quit_after_lease = 0,
.background_if_no_lease = 0,
.interface = "eth0",
.pidfile = NULL,
.script = DEFAULT_SCRIPT,
.clientid = NULL,
.vendorclass = NULL,
.hostname = NULL,
.fqdn = NULL,
.ifindex = 0,
.retries = 3,
.timeout = 3,
.arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */
};
static void print_usage(void)
#ifndef IN_BUSYBOX
static void ATTRIBUTE_NORETURN show_usage(void)
{
printf(
"Usage: udhcpcd [OPTIONS]\n\n"
" -c, --clientid=CLIENTID Client identifier\n"
"Usage: udhcpc [OPTIONS]\n\n"
" -c, --clientid=CLIENTID Set client identifier - type is first char\n"
" -C, --clientid-none Suppress default client identifier\n"
" -V, --vendorclass=CLASSID Set vendor class identifier\n"
" -H, --hostname=HOSTNAME Client hostname\n"
" -h Alias for -H\n"
" -F, --fqdn=FQDN Client fully qualified domain name\n"
" -f, --foreground Do not fork after getting lease\n"
" -b, --background Fork to background if lease cannot be\n"
" immediately negotiated.\n"
" -i, --interface=INTERFACE Interface to use (default: eth0)\n"
" -n, --now Exit with failure if lease cannot be\n"
" immediately negotiated.\n"
@ -88,168 +85,194 @@ static void print_usage(void)
" -r, --request=IP IP address to request (default: none)\n"
" -s, --script=file Run file at dhcp events (default:\n"
" " DEFAULT_SCRIPT ")\n"
" -T, --timeout=seconds Try to get the lease for the amount of\n"
" seconds (default: 3)\n"
" -t, --retries=NUM Send up to NUM request packets\n"
" -v, --version Display version\n"
);
exit(0);
}
#else
#define show_usage bb_show_usage
extern void show_usage(void) ATTRIBUTE_NORETURN;
#endif
/* just a little helper */
static void change_mode(int new_mode)
{
DEBUG(LOG_INFO, "entering %s listen mode",
new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
if (fd >= 0) close(fd);
fd = -1;
listen_mode = new_mode;
}
/* SIGUSR1 handler (renew) */
static void renew_requested(int sig)
/* perform a renew */
static void perform_renew(void)
{
sig = 0;
LOG(LOG_INFO, "Received SIGUSR1");
if (state == BOUND || state == RENEWING || state == REBINDING ||
state == RELEASED) {
listen_mode = LISTEN_KERNEL;
server_addr = 0;
packet_num = 0;
LOG(LOG_INFO, "Performing a DHCP renew");
switch (state) {
case BOUND:
change_mode(LISTEN_KERNEL);
case RENEWING:
case REBINDING:
state = RENEW_REQUESTED;
break;
case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
run_script(NULL, "deconfig");
case REQUESTING:
case RELEASED:
change_mode(LISTEN_RAW);
state = INIT_SELECTING;
break;
case INIT_SELECTING:
break;
}
if (state == RELEASED) {
listen_mode = LISTEN_RAW;
state = INIT_SELECTING;
}
/* start things over */
packet_num = 0;
/* Kill any timeouts because the user wants this to hurry along */
timeout = 0;
}
/* SIGUSR2 handler (release) */
static void release_requested(int sig)
/* perform a release */
static void perform_release(void)
{
sig = 0;
LOG(LOG_INFO, "Received SIGUSR2");
char buffer[16];
struct in_addr temp_addr;
/* send release packet */
if (state == BOUND || state == RENEWING || state == REBINDING) {
temp_addr.s_addr = server_addr;
sprintf(buffer, "%s", inet_ntoa(temp_addr));
temp_addr.s_addr = requested_ip;
LOG(LOG_INFO, "Unicasting a release of %s to %s",
inet_ntoa(temp_addr), buffer);
send_release(server_addr, requested_ip); /* unicast */
run_script(NULL, "deconfig");
}
LOG(LOG_INFO, "Entering released state");
listen_mode = 0;
change_mode(LISTEN_NONE);
state = RELEASED;
timeout = 0xffffffff;
timeout = 0x7fffffff;
}
static void pidfile_acquire(void)
static void client_background(void)
{
if (client_config.pidfile == NULL) return;
pid_fd = open(client_config.pidfile, O_CREAT | O_WRONLY, 0644);
if (pid_fd < 0) {
LOG(LOG_ERR, "Unable to open pidfile %s: %s\n",
client_config.pidfile, strerror(errno));
} else {
lockf(pid_fd, F_LOCK, 0);
}
}
static void pidfile_write_release(void)
{
FILE *out;
if (client_config.pidfile == NULL || pid_fd < 0) return;
if ((out = fdopen(pid_fd, "w")) != NULL) {
fprintf(out, "%d\n", getpid());
fclose(out);
}
lockf(pid_fd, F_UNLCK, 0);
close(pid_fd);
}
static void background(void)
{
if (client_config.quit_after_lease) {
exit(0);
} else if (!client_config.foreground) {
pidfile_acquire(); /* hold lock during fork. */
switch(fork()) {
case -1:
perror("fork");
exit(1);
/*NOTREACHED*/
case 0:
break; /* child continues */
default:
exit(0); /* parent exits */
/*NOTREACHED*/
}
close(0);
close(1);
close(2);
setsid();
client_config.foreground = 1; /* Do not fork again. */
pidfile_write_release();
}
background(client_config.pidfile);
client_config.foreground = 1; /* Do not fork again. */
client_config.background_if_no_lease = 0;
}
#ifdef COMBINED_BINARY
int udhcpc(int argc, char *argv[])
int udhcpc_main(int argc, char *argv[])
#else
int main(int argc, char *argv[])
#endif
{
char *temp, *message;
uint8_t *temp, *message;
unsigned long t1 = 0, t2 = 0, xid = 0;
unsigned long start = 0, lease;
fd_set rfds;
int fd, retval;
int retval;
struct timeval tv;
int c, len;
struct ifreq ifr;
struct dhcpMessage packet;
struct in_addr temp_addr;
long now;
int max_fd;
int sig;
int no_clientid = 0;
static struct option options[] = {
static const struct option arg_options[] = {
{"clientid", required_argument, 0, 'c'},
{"clientid-none", no_argument, 0, 'C'},
{"vendorclass", required_argument, 0, 'V'},
{"foreground", no_argument, 0, 'f'},
{"background", no_argument, 0, 'b'},
{"hostname", required_argument, 0, 'H'},
{"help", no_argument, 0, 'h'},
{"hostname", required_argument, 0, 'h'},
{"fqdn", required_argument, 0, 'F'},
{"interface", required_argument, 0, 'i'},
{"now", no_argument, 0, 'n'},
{"now", no_argument, 0, 'n'},
{"pidfile", required_argument, 0, 'p'},
{"quit", no_argument, 0, 'q'},
{"request", required_argument, 0, 'r'},
{"script", required_argument, 0, 's'},
{"timeout", required_argument, 0, 'T'},
{"version", no_argument, 0, 'v'},
{"retries", required_argument, 0, 't'},
{0, 0, 0, 0}
};
/* get options */
while (1) {
int option_index = 0;
c = getopt_long(argc, argv, "c:fH:hi:np:qr:s:v", options, &option_index);
c = getopt_long(argc, argv, "c:CV:fbH:h:F:i:np:qr:s:T:t:v", arg_options, &option_index);
if (c == -1) break;
switch (c) {
case 'c':
if (no_clientid) show_usage();
len = strlen(optarg) > 255 ? 255 : strlen(optarg);
if (client_config.clientid) free(client_config.clientid);
client_config.clientid = malloc(len + 2);
free(client_config.clientid);
client_config.clientid = xmalloc(len + 2);
client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
client_config.clientid[OPT_LEN] = len;
strncpy(client_config.clientid + 2, optarg, len);
client_config.clientid[OPT_DATA] = '\0';
strncpy((char*)client_config.clientid + OPT_DATA, optarg, len);
break;
case 'C':
if (client_config.clientid) show_usage();
no_clientid = 1;
break;
case 'V':
len = strlen(optarg) > 255 ? 255 : strlen(optarg);
free(client_config.vendorclass);
client_config.vendorclass = xmalloc(len + 2);
client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
client_config.vendorclass[OPT_LEN] = len;
strncpy((char*)client_config.vendorclass + OPT_DATA, optarg, len);
break;
case 'f':
client_config.foreground = 1;
break;
case 'H':
len = strlen(optarg) > 255 ? 255 : strlen(optarg);
if (client_config.hostname) free(client_config.hostname);
client_config.hostname = malloc(len + 2);
client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
client_config.hostname[OPT_LEN] = len;
strncpy(client_config.hostname + 2, optarg, len);
case 'b':
client_config.background_if_no_lease = 1;
break;
case 'h':
print_usage();
return 0;
case 'H':
len = strlen(optarg) > 255 ? 255 : strlen(optarg);
free(client_config.hostname);
client_config.hostname = xmalloc(len + 2);
client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
client_config.hostname[OPT_LEN] = len;
strncpy((char*)client_config.hostname + 2, optarg, len);
break;
case 'F':
len = strlen(optarg) > 255 ? 255 : strlen(optarg);
free(client_config.fqdn);
client_config.fqdn = xmalloc(len + 5);
client_config.fqdn[OPT_CODE] = DHCP_FQDN;
client_config.fqdn[OPT_LEN] = len + 3;
/* Flags: 0000NEOS
S: 1 => Client requests Server to update A RR in DNS as well as PTR
O: 1 => Server indicates to client that DNS has been updated regardless
E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
N: 1 => Client requests Server to not update DNS
*/
client_config.fqdn[OPT_LEN + 1] = 0x1;
client_config.fqdn[OPT_LEN + 2] = 0;
client_config.fqdn[OPT_LEN + 3] = 0;
strncpy((char*)client_config.fqdn + 5, optarg, len);
break;
case 'i':
client_config.interface = optarg;
break;
@ -268,124 +291,127 @@ int main(int argc, char *argv[])
case 's':
client_config.script = optarg;
break;
case 'T':
client_config.timeout = atoi(optarg);
break;
case 't':
client_config.retries = atoi(optarg);
break;
case 'v':
printf("udhcpcd, version %s\n\n", VERSION);
return 0;
break;
default:
show_usage();
}
}
OPEN_LOG("udhcpc");
LOG(LOG_INFO, "Moreton Bay DHCP Client (v%s) started", VERSION);
/* Start the log, sanitize fd's, and write a pid file */
start_log_and_pid("udhcpc", client_config.pidfile);
pidfile_acquire();
pidfile_write_release();
if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {
strcpy(ifr.ifr_name, client_config.interface);
if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {
DEBUG(LOG_INFO, "adapter index %d", ifr.ifr_ifindex);
client_config.ifindex = ifr.ifr_ifindex;
} else {
LOG(LOG_ERR, "SIOCGIFINDEX failed! %s", sys_errlist[errno]);
return 1;
}
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
memcpy(client_config.arp, ifr.ifr_hwaddr.sa_data, 6);
DEBUG(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
client_config.arp[0], client_config.arp[1], client_config.arp[2],
client_config.arp[3], client_config.arp[4], client_config.arp[5]);
} else {
LOG(LOG_ERR, "SIOCGIFHWADDR failed! %s", sys_errlist[errno]);
return 1;
}
} else {
LOG(LOG_ERR, "socket failed! %s", sys_errlist[errno]);
if (read_interface(client_config.interface, &client_config.ifindex,
NULL, client_config.arp) < 0)
return 1;
}
close(fd);
fd = -1;
/* setup signal handlers */
signal(SIGUSR1, renew_requested);
signal(SIGUSR2, release_requested);
/* if not set, and not suppressed, setup the default client ID */
if (!client_config.clientid && !no_clientid) {
client_config.clientid = xmalloc(6 + 3);
client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
client_config.clientid[OPT_LEN] = 7;
client_config.clientid[OPT_DATA] = 1;
memcpy(client_config.clientid + 3, client_config.arp, 6);
}
if (!client_config.vendorclass) {
client_config.vendorclass = xmalloc(sizeof("udhcp "VERSION) + 2);
client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
client_config.vendorclass[OPT_LEN] = sizeof("udhcp "VERSION) - 1;
client_config.vendorclass[OPT_DATA] = 1;
memcpy(&client_config.vendorclass[OPT_DATA],
"udhcp "VERSION, sizeof("udhcp "VERSION) - 1);
}
/* setup the signal pipe */
udhcp_sp_setup();
state = INIT_SELECTING;
run_script(NULL, "deconfig");
change_mode(LISTEN_RAW);
for (;;) {
if (fd > 0) {
close(fd);
fd = -1;
}
if (listen_mode == LISTEN_KERNEL) {
if ((fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface)) < 0) {
LOG(LOG_ERR, "couldn't create server socket -- au revoir");
exit(0);
}
} else if (listen_mode == LISTEN_RAW) {
if ((fd = raw_socket(client_config.ifindex)) < 0) {
LOG(LOG_ERR, "couldn't create raw socket -- au revoir");
exit(0);
}
} else fd = -1;
tv.tv_sec = timeout - time(0);
tv.tv_sec = timeout - uptime();
tv.tv_usec = 0;
FD_ZERO(&rfds);
if (listen_mode) FD_SET(fd, &rfds);
if (listen_mode != LISTEN_NONE && fd < 0) {
if (listen_mode == LISTEN_KERNEL)
fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
else
fd = raw_socket(client_config.ifindex);
if (fd < 0) {
LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
return 0;
}
}
max_fd = udhcp_sp_fd_set(&rfds, fd);
if (tv.tv_sec > 0) {
retval = select(fd + 1, &rfds, NULL, NULL, &tv);
DEBUG(LOG_INFO, "Waiting on select...");
retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
} else retval = 0; /* If we already timed out, fall through */
now = uptime();
if (retval == 0) {
/* timeout dropped to zero */
switch (state) {
case INIT_SELECTING:
if (packet_num < 3) {
if (packet_num < client_config.retries) {
if (packet_num == 0)
xid = random_xid();
/* send discover packet */
send_discover(xid, requested_ip); /* broadcast */
timeout = time(0) + ((packet_num == 2) ? 10 : 2);
timeout = now + client_config.timeout;
packet_num++;
} else {
if (client_config.abort_if_no_lease) {
LOG(LOG_INFO,
"No lease, failing.");
exit(1);
}
run_script(NULL, "leasefail");
if (client_config.background_if_no_lease) {
LOG(LOG_INFO, "No lease, forking to background.");
client_background();
} else if (client_config.abort_if_no_lease) {
LOG(LOG_INFO, "No lease, failing.");
return 1;
}
/* wait to try again */
packet_num = 0;
timeout = time(0) + 60;
timeout = now + 60;
}
break;
case RENEW_REQUESTED:
case REQUESTING:
if (packet_num < 3) {
if (packet_num < client_config.retries) {
/* send request packet */
if (state == RENEW_REQUESTED)
send_renew(xid, server_addr, requested_ip); /* unicast */
else send_selecting(xid, server_addr, requested_ip); /* broadcast */
timeout = time(0) + ((packet_num == 2) ? 10 : 2);
timeout = now + ((packet_num == 2) ? 10 : 2);
packet_num++;
} else {
/* timed out, go back to init state */
if (state == RENEW_REQUESTED) run_script(NULL, "deconfig");
state = INIT_SELECTING;
timeout = time(0);
timeout = now;
packet_num = 0;
listen_mode = LISTEN_RAW;
change_mode(LISTEN_RAW);
}
break;
case BOUND:
/* Lease is starting to run out, time to enter renewing state */
state = RENEWING;
listen_mode = LISTEN_KERNEL;
change_mode(LISTEN_KERNEL);
DEBUG(LOG_INFO, "Entering renew state");
/* fall right through */
case RENEWING:
@ -393,12 +419,12 @@ int main(int argc, char *argv[])
if ((t2 - t1) <= (lease / 14400 + 1)) {
/* timed out, enter rebinding state */
state = REBINDING;
timeout = time(0) + (t2 - t1);
timeout = now + (t2 - t1);
DEBUG(LOG_INFO, "Entering rebinding state");
} else {
/* send a request packet */
send_renew(xid, server_addr, requested_ip); /* unicast */
t1 = (t2 - t1) / 2 + t1;
timeout = t1 + start;
}
@ -410,9 +436,9 @@ int main(int argc, char *argv[])
state = INIT_SELECTING;
LOG(LOG_INFO, "Lease lost, entering init state");
run_script(NULL, "deconfig");
timeout = time(0);
timeout = now;
packet_num = 0;
listen_mode = LISTEN_RAW;
change_mode(LISTEN_RAW);
} else {
/* send a request packet */
send_renew(xid, 0, requested_ip); /* broadcast */
@ -423,29 +449,39 @@ int main(int argc, char *argv[])
break;
case RELEASED:
/* yah, I know, *you* say it would never happen */
timeout = 0xffffffff;
timeout = 0x7fffffff;
break;
}
} else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
/* a packet is ready, read it */
if (listen_mode == LISTEN_KERNEL) {
if (get_packet(&packet, fd) < 0) continue;
} else {
if (get_raw_packet(&packet, fd) < 0) continue;
}
if (listen_mode == LISTEN_KERNEL)
len = get_packet(&packet, fd);
else len = get_raw_packet(&packet, fd);
if (len == -1 && errno != EINTR) {
DEBUG(LOG_INFO, "error on read, %m, reopening socket");
change_mode(listen_mode); /* just close and reopen */
}
if (len < 0) continue;
if (packet.xid != xid) {
DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
(unsigned long) packet.xid, xid);
continue;
}
/* Ignore packets that aren't for us */
if (memcmp(packet.chaddr, client_config.arp, 6)) {
DEBUG(LOG_INFO, "packet does not have our chaddr -- ignoring");
continue;
}
if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
continue;
}
switch (state) {
case INIT_SELECTING:
/* Must be a DHCPOFFER to one of our xid's */
@ -454,10 +490,10 @@ int main(int argc, char *argv[])
memcpy(&server_addr, temp, 4);
xid = packet.xid;
requested_ip = packet.yiaddr;
/* enter requesting state */
state = REQUESTING;
timeout = time(0);
timeout = now;
packet_num = 0;
} else {
DEBUG(LOG_ERR, "No server ID in message");
@ -471,56 +507,68 @@ int main(int argc, char *argv[])
if (*message == DHCPACK) {
if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
lease = 60*60;
lease = 60 * 60;
} else {
memcpy(&lease, temp, 4);
lease = ntohl(lease);
}
/* enter bound state */
t1 = lease / 2;
/* little fixed point for n * .875 */
t2 = (lease * 0x7) >> 3;
temp_addr.s_addr = packet.yiaddr;
LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
inet_ntoa(temp_addr), lease);
start = time(0);
start = now;
timeout = t1 + start;
requested_ip = packet.yiaddr;
run_script(&packet,
((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
state = BOUND;
listen_mode = LISTEN_NONE;
background();
change_mode(LISTEN_NONE);
if (client_config.quit_after_lease)
return 0;
if (!client_config.foreground)
client_background();
} else if (*message == DHCPNAK) {
/* return to init state */
LOG(LOG_INFO, "Received DHCP NAK");
run_script(&packet, "nak");
if (state != REQUESTING)
run_script(NULL, "deconfig");
state = INIT_SELECTING;
timeout = time(0);
timeout = now;
requested_ip = 0;
packet_num = 0;
listen_mode = LISTEN_RAW;
change_mode(LISTEN_RAW);
sleep(3); /* avoid excessive network traffic */
}
break;
case BOUND:
case RELEASED:
/* ignore all packets */
/* case BOUND, RELEASED: - ignore all packets */
}
} else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
switch (sig) {
case SIGUSR1:
perform_renew();
break;
}
case SIGUSR2:
perform_release();
break;
case SIGTERM:
LOG(LOG_INFO, "Received SIGTERM");
return 0;
}
} else if (retval == -1 && errno == EINTR) {
/* a signal was caught */
} else {
/* An error occured */
DEBUG(LOG_ERR, "Error on select");
}
}
return 0;
}

25
dhcpc.h
View File

@ -1,7 +1,9 @@
/* dhcpd.h */
/* dhcpc.h */
#ifndef _DHCPC_H
#define _DHCPC_H
/* grab define DEFAULT_SCRIPT */
#include "libbb_udhcp.h"
#define INIT_SELECTING 0
#define REQUESTING 1
@ -13,27 +15,22 @@
#define RELEASED 7
/* Paramaters the client should request from the server */
#define PARM_REQUESTS \
DHCP_SUBNET, \
DHCP_ROUTER, \
DHCP_DNS_SERVER, \
DHCP_HOST_NAME, \
DHCP_DOMAIN_NAME, \
DHCP_BROADCAST
struct client_config_t {
char foreground; /* Do not fork */
char quit_after_lease; /* Quit after obtaining lease */
char abort_if_no_lease; /* Abort if no lease */
char background_if_no_lease; /* Fork to background if no lease */
char *interface; /* The name of the interface to use */
char *pidfile; /* Optionally store the process ID */
char *script; /* User script to run at dhcp events */
char *clientid; /* Optional client id to use */
char *hostname; /* Optional hostname to use */
uint8_t *clientid; /* Optional client id to use */
uint8_t *vendorclass; /* Optional vendor class-id to use */
uint8_t *hostname; /* Optional hostname to use */
uint8_t *fqdn; /* Optional fully qualified domain name to use */
int ifindex; /* Index number of the interface to use */
unsigned char arp[6]; /* Our arp address */
int retries; /* Max number of request packets */
int timeout; /* Number of seconds to try to get a lease */
uint8_t arp[6]; /* Our arp address */
};
extern struct client_config_t client_config;

272
dhcpd.c
View File

@ -1,6 +1,6 @@
/* dhcpd.c
*
* Moreton Bay DHCP Server
* udhcp Server
* Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
* Chris Trew <ctrew@moreton.com.au>
*
@ -25,12 +25,9 @@
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <signal.h>
@ -39,186 +36,177 @@
#include <time.h>
#include <sys/time.h>
#include "debug.h"
#include "dhcpd.h"
#include "arpping.h"
#include "socket.h"
#include "options.h"
#include "files.h"
#include "leases.h"
#include "packet.h"
#include "serverpacket.h"
/* prototypes */
static int log_pid(void);
#include "common.h"
#include "signalpipe.h"
#include "static_leases.h"
/* globals */
struct dhcpOfferedAddr *leases;
struct server_config_t server_config;
static void udhcpd_killed(int pid)
{
pid = 0;
if (server_config.pid_file) unlink(server_config.pid_file);
LOG(LOG_INFO, "Received SIGTERM");
CLOSE_LOG();
exit(0);
}
#ifdef COMBINED_BINARY
int udhcpd(int argc, char *argv[])
#ifdef COMBINED_BINARY
int udhcpd_main(int argc, char *argv[])
#else
int main(int argc, char *argv[])
#endif
{
{
fd_set rfds;
struct timeval tv;
int server_socket;
int server_socket = -1;
int bytes, retval;
struct dhcpMessage packet;
unsigned char *state;
char *server_id, *requested;
u_int32_t server_id_align, requested_align;
uint8_t *state;
uint8_t *server_id, *requested;
uint32_t server_id_align, requested_align;
unsigned long timeout_end;
struct option_set *option;
struct dhcpOfferedAddr *lease;
struct sockaddr_in *sin;
/* server ip addr */
int fd = -1;
struct ifreq ifr;
struct dhcpOfferedAddr static_lease;
int max_sock;
unsigned long num_ips;
uint32_t static_lease_ip;
argc = argv[0][0]; /* get rid of some warnings */
OPEN_LOG("udhcpd");
LOG(LOG_INFO, "Moreton Bay DHCP Server (v%s) started", VERSION);
memset(&server_config, 0, sizeof(struct server_config_t));
read_config(DHCPD_CONF_FILE);
read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]);
/* Start the log, sanitize fd's, and write a pid file */
start_log_and_pid("udhcpd", server_config.pidfile);
if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) {
memcpy(&server_config.lease, option->data + 2, 4);
server_config.lease = ntohl(server_config.lease);
}
else server_config.lease = LEASE_TIME;
leases = malloc(sizeof(struct dhcpOfferedAddr) * server_config.max_leases);
memset(leases, 0, sizeof(struct dhcpOfferedAddr) * server_config.max_leases);
/* Sanity check */
num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
if (server_config.max_leases > num_ips) {
LOG(LOG_ERR, "max_leases value (%lu) not sane, "
"setting to %lu instead",
server_config.max_leases, num_ips);
server_config.max_leases = num_ips;
}
leases = xcalloc(server_config.max_leases, sizeof(struct dhcpOfferedAddr));
read_leases(server_config.lease_file);
log_pid();
if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {
ifr.ifr_addr.sa_family = AF_INET;
strcpy(ifr.ifr_name, server_config.interface);
if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
sin = (struct sockaddr_in *) &ifr.ifr_addr;
server_config.server = sin->sin_addr.s_addr;
DEBUG(LOG_INFO, "%s (server_ip) = %s", ifr.ifr_name, inet_ntoa(sin->sin_addr));
} else {
LOG(LOG_ERR, "SIOCGIFADDR failed!");
return 1;
}
if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {
DEBUG(LOG_INFO, "adapter index %d", ifr.ifr_ifindex);
server_config.ifindex = ifr.ifr_ifindex;
} else {
LOG(LOG_ERR, "SIOCGIFINDEX failed!");
return 1;
}
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
memcpy(server_config.arp, ifr.ifr_hwaddr.sa_data, 6);
DEBUG(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
server_config.arp[0], server_config.arp[1], server_config.arp[2],
server_config.arp[3], server_config.arp[4], server_config.arp[5]);
} else {
LOG(LOG_ERR, "SIOCGIFHWADDR failed!");
return 1;
}
} else {
LOG(LOG_ERR, "socket failed!");
if (read_interface(server_config.interface, &server_config.ifindex,
&server_config.server, server_config.arp) < 0)
return 1;
}
close(fd);
#ifndef DEBUGGING
if (fork()) return 0;
else {
close(0);
setsid();
}
#ifndef UDHCP_DEBUG
background(server_config.pidfile); /* hold lock during fork. */
#endif
signal(SIGUSR1, write_leases);
signal(SIGTERM, udhcpd_killed);
/* Setup the signal pipe */
udhcp_sp_setup();
timeout_end = time(0) + server_config.auto_time;
while(1) { /* loop until universe collapses */
server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface);
if(server_socket == -1) {
LOG(LOG_ERR, "couldn't create server socket -- au revoir");
exit(0);
}
if (server_socket < 0)
if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) {
LOG(LOG_ERR, "FATAL: couldn't create server socket, %m");
return 2;
}
FD_ZERO(&rfds);
FD_SET(server_socket, &rfds);
max_sock = udhcp_sp_fd_set(&rfds, server_socket);
if (server_config.auto_time) {
tv.tv_sec = timeout_end - time(0);
if (tv.tv_sec <= 0) {
tv.tv_sec = server_config.auto_time;
timeout_end = time(0) + server_config.auto_time;
write_leases(0);
}
tv.tv_usec = 0;
}
retval = select(server_socket + 1, &rfds, NULL, NULL, server_config.auto_time ? &tv : NULL);
if (retval == 0) {
write_leases(0);
timeout_end = time(0) + server_config.auto_time;
close(server_socket);
continue;
} else if (retval < 0) {
DEBUG(LOG_INFO, "error on select");
close(server_socket);
continue;
}
bytes = get_packet(&packet, server_socket); /* this waits for a packet - idle */
close(server_socket);
if(bytes < 0)
continue;
if (!server_config.auto_time || tv.tv_sec > 0) {
retval = select(max_sock + 1, &rfds, NULL, NULL,
server_config.auto_time ? &tv : NULL);
} else retval = 0; /* If we already timed out, fall through */
if((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
if (retval == 0) {
write_leases();
timeout_end = time(0) + server_config.auto_time;
continue;
} else if (retval < 0 && errno != EINTR) {
DEBUG(LOG_INFO, "error on select");
continue;
}
switch (udhcp_sp_read(&rfds)) {
case SIGUSR1:
LOG(LOG_INFO, "Received a SIGUSR1");
write_leases();
/* why not just reset the timeout, eh */
timeout_end = time(0) + server_config.auto_time;
continue;
case SIGTERM:
LOG(LOG_INFO, "Received a SIGTERM");
return 0;
case 0: break; /* no signal */
default: continue; /* signal or error (probably EINTR) */
}
if ((bytes = get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */
if (bytes == -1 && errno != EINTR) {
DEBUG(LOG_INFO, "error on read, %m, reopening socket");
close(server_socket);
server_socket = -1;
}
continue;
}
if ((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
DEBUG(LOG_ERR, "couldn't get option from packet, ignoring");
continue;
}
/* Look for a static lease */
static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
if(static_lease_ip)
{
printf("Found static lease: %x\n", static_lease_ip);
memcpy(&static_lease.chaddr, &packet.chaddr, 16);
static_lease.yiaddr = static_lease_ip;
static_lease.expires = 0;
lease = &static_lease;
}
else
{
lease = find_lease_by_chaddr(packet.chaddr);
}
switch (state[0]) {
case DHCPDISCOVER:
DEBUG(LOG_INFO,"received DISCOVER");
if (sendOffer(&packet) < 0) {
LOG(LOG_ERR, "send OFFER failed -- ignoring");
LOG(LOG_ERR, "send OFFER failed");
}
break;
case DHCPREQUEST:
DEBUG(LOG_INFO,"received REQUEST");
break;
case DHCPREQUEST:
DEBUG(LOG_INFO, "received REQUEST");
requested = get_option(&packet, DHCP_REQUESTED_IP);
server_id = get_option(&packet, DHCP_SERVER_ID);
if (requested) memcpy(&requested_align, requested, 4);
if (server_id) memcpy(&server_id_align, server_id, 4);
if (lease) {
if (server_id) {
/* SELECTING State */
DEBUG(LOG_INFO, "server_id = %08x", ntohl(server_id_align));
if (server_id_align == server_config.server && requested &&
if (server_id_align == server_config.server && requested &&
requested_align == lease->yiaddr) {
sendACK(&packet, lease->yiaddr);
}
@ -236,16 +224,36 @@ int main(int argc, char *argv[])
/* don't know what to do!!!! */
sendNAK(&packet);
}
}
}
}
} /* else remain silent */
/* what to do if we have no record of the client */
} else if (server_id) {
/* SELECTING State */
} else if (requested) {
/* INIT-REBOOT State */
if ((lease = find_lease_by_yiaddr(requested_align))) {
if (lease_expired(lease)) {
/* probably best if we drop this lease */
memset(lease->chaddr, 0, 16);
/* make some contention for this address */
} else sendNAK(&packet);
} else if (requested_align < server_config.start ||
requested_align > server_config.end) {
sendNAK(&packet);
} /* else remain silent */
} else {
/* RENEWING or REBINDING State */
}
break;
case DHCPDECLINE:
DEBUG(LOG_INFO,"received DECLINE");
if (lease) {
memset(lease->chaddr, 0, 16);
lease->expires = time(0) + server_config.decline_time;
}
}
break;
case DHCPRELEASE:
DEBUG(LOG_INFO,"received RELEASE");
@ -254,7 +262,7 @@ int main(int argc, char *argv[])
case DHCPINFORM:
DEBUG(LOG_INFO,"received INFORM");
send_inform(&packet);
break;
break;
default:
LOG(LOG_WARNING, "unsupported DHCP message (%02x) -- ignoring", state[0]);
}
@ -263,19 +271,3 @@ int main(int argc, char *argv[])
return 0;
}
int log_pid(void)
{
int fd;
pid_t pid;
char *pidfile = server_config.pid_file;
pid = getpid();
if((fd = open(pidfile, O_WRONLY | O_CREAT, 0660)) < 0)
return -1;
write(fd, (void *) &pid, sizeof(pid));
close(fd);
return 0;
}

53
dhcpd.h
View File

@ -5,7 +5,9 @@
#include <netinet/ip.h>
#include <netinet/udp.h>
#include "libbb_udhcp.h"
#include "leases.h"
#include "version.h"
/************************************/
/* Defaults _you_ may want to tweak */
@ -13,6 +15,7 @@
/* the period of time the client is allowed to use that address */
#define LEASE_TIME (60*60*24*10) /* 10 days of seconds */
#define LEASES_FILE "/var/lib/misc/udhcpd.leases"
/* where to find the DHCP server configuration file */
#define DHCPD_CONF_FILE "/etc/udhcpd.conf"
@ -60,6 +63,7 @@
#define DHCP_T2 0x3b
#define DHCP_VENDOR 0x3c
#define DHCP_CLIENT_ID 0x3d
#define DHCP_FQDN 0x51
#define DHCP_END 0xFF
@ -86,43 +90,52 @@
#define SNAME_FIELD 2
/* miscellaneous defines */
#define TRUE 1
#define FALSE 0
#define MAC_BCAST_ADDR "\xff\xff\xff\xff\xff\xff"
#define MAC_BCAST_ADDR (uint8_t *) "\xff\xff\xff\xff\xff\xff"
#define OPT_CODE 0
#define OPT_LEN 1
#define OPT_DATA 2
struct option_set {
unsigned char *data;
uint8_t *data;
struct option_set *next;
};
struct static_lease {
uint8_t *mac;
uint32_t *ip;
struct static_lease *next;
};
struct server_config_t {
u_int32_t server; /* Our IP, in network order */
u_int32_t start; /* Start address of leases, network order */
u_int32_t end; /* End of leases, network order */
uint32_t server; /* Our IP, in network order */
uint32_t start; /* Start address of leases, network order */
uint32_t end; /* End of leases, network order */
struct option_set *options; /* List of DHCP options loaded from the config file */
char *interface; /* The name of the interface to use */
int ifindex; /* Index number of the interface to use */
unsigned char arp[6]; /* Our arp address */
uint8_t arp[6]; /* Our arp address */
unsigned long lease; /* lease time in seconds (host order) */
unsigned long max_leases; /* maximum number of leases (including reserved address) */
char remaining; /* should the lease file be interpreted as lease time remaining, or
* as the time the lease expires */
unsigned long auto_time; /* how long should udhcpd wait before writing a config file.
unsigned long max_leases; /* maximum number of leases (including reserved address) */
char remaining; /* should the lease file be interpreted as lease time remaining, or
* as the time the lease expires */
unsigned long auto_time; /* how long should udhcpd wait before writing a config file.
* if this is zero, it will only write one on SIGUSR1 */
unsigned long decline_time; /* how long an address is reserved if a client returns a
* decline message */
unsigned long conflict_time; /* how long an arp conflict offender is leased for */
unsigned long offer_time; /* how long an offered address is reserved */
unsigned long min_lease; /* minimum lease a client can request*/
unsigned long decline_time; /* how long an address is reserved if a client returns a
* decline message */
unsigned long conflict_time; /* how long an arp conflict offender is leased for */
unsigned long offer_time; /* how long an offered address is reserved */
unsigned long min_lease; /* minimum lease a client can request*/
char *lease_file;
char *pid_file;
char *pidfile;
char *notify_file; /* What to run whenever leases are written */
};
uint32_t siaddr; /* next server bootp option */
char *sname; /* bootp server name */
char *boot_file; /* bootp boot file option */
struct static_lease *static_leases; /* List of ip/mac pairs to assign static leases */
};
extern struct server_config_t server_config;
extern struct dhcpOfferedAddr *leases;
#endif

30
dumpleases.1 Normal file
View File

@ -0,0 +1,30 @@
.TH DUMPLEASES 1 2001-09-27 GNU/Linux "GNU/Linux Administrator's Manual"
.SH NAME
dumpleases \- display leases granted by udhcp server
.SH SYNOPSIS
.B dumpleases
.RI [ OPTION ]...
.SH DESCRIPTION
Display the DHCP leases granted by
.BR udhcpd (8).
.SH OPTIONS
.TP
.BR \-a ,\ \-\-absolute
Interpret lease times as expiration time.
.TP
.BI \-f\ FILE,\ \-\-file= FILE
Read lease information from
.IR FILE .
.TP
.BR \-h ,\ \-\-help
Display help.
.TP
.BR \-r ,\ \-\-remaining
Interpret lease times as remaining time.
.SH FILES
.TP
.I /var/lib/misc/udhcpd.leases
Lease information file.
.SH SEE ALSO
.BR udhcpd (8),
.BR udhcpd.conf (5).

View File

@ -1,74 +1,83 @@
/* vi: set sw=4 ts=4: */
/*
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
*/
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <syslog.h>
#include <signal.h>
#include <errno.h>
#include <getopt.h>
#include <time.h>
#include "dhcpd.h"
#include "leases.h"
#include "libbb_udhcp.h"
#define REMAINING 0
#define ABSOLUTE 1
struct lease_t {
unsigned char chaddr[16];
u_int32_t yiaddr;
u_int32_t expires;
};
int main (int argc, char *argv[]) {
#ifndef IN_BUSYBOX
static void ATTRIBUTE_NORETURN show_usage(void)
{
printf(
"Usage: dumpleases -f <file> -[r|a]\n\n"
" -f, --file=FILENAME Leases file to load\n"
" -r, --remaining Interepret lease times as time remaing\n"
" -a, --absolute Interepret lease times as expire time\n");
exit(0);
}
#else
#define show_usage bb_show_usage
#endif
#ifdef IN_BUSYBOX
int dumpleases_main(int argc, char *argv[])
#else
int main(int argc, char *argv[])
#endif
{
FILE *fp;
int i, c, mode = REMAINING;
unsigned long expires;
char file[255] = "/etc/udhcpd.leases";
struct lease_t lease;
long expires;
const char *file = LEASES_FILE;
struct dhcpOfferedAddr lease;
struct in_addr addr;
static struct option options[] = {
static const struct option options[] = {
{"absolute", 0, 0, 'a'},
{"remaining", 0, 0, 'r'},
{"file", 1, 0, 'f'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};
while (1) {
int option_index = 0;
c = getopt_long(argc, argv, "arf:h", options, &option_index);
c = getopt_long(argc, argv, "arf:", options, &option_index);
if (c == -1) break;
switch (c) {
case 'a': mode = ABSOLUTE; break;
case 'r': mode = REMAINING; break;
case 'f':
strncpy(file, optarg, 255);
file[254] = '\0';
break;
case 'h':
printf("Usage: dumpleases -f <file> -[ra]\n\n");
printf(" -f, --file=FILENAME Leases file to load\n");
printf(" -r, --remaining Interepret lease times as time reemaing\n");
printf(" -a, --absolute Interepret lease times as expire time\n");
file = optarg;
break;
default:
show_usage();
}
}
if (!(fp = fopen(file, "r"))) {
perror("could not open input file");
return 0;
}
printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at");
fp = xfopen(file, "r");
printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at");
/* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
while (fread(&lease, sizeof(lease), 1, fp)) {
@ -100,6 +109,6 @@ int main (int argc, char *argv[]) {
} else printf("%s", ctime(&expires));
}
fclose(fp);
return 0;
}

422
files.c
View File

@ -1,149 +1,256 @@
/*
/*
* files.c -- DHCP server file manipulation *
* Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
*/
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <netdb.h>
#include <netinet/ether.h>
#include "static_leases.h"
#include "debug.h"
#include "dhcpd.h"
#include "files.h"
#include "options.h"
#include "leases.h"
#include "files.h"
#include "common.h"
/*
* Domain names may have 254 chars, and string options can be 254
* chars long. However, 80 bytes will be enough for most, and won't
* hog up memory. If you have a special application, change it
*/
#define READ_CONFIG_BUF_SIZE 80
/* on these functions, make sure you datatype matches */
static int read_ip(char *line, void *arg)
static int read_ip(const char *line, void *arg)
{
struct in_addr *addr = arg;
inet_aton(line, addr);
return 1;
struct hostent *host;
int retval = 1;
if (!inet_aton(line, addr)) {
if ((host = gethostbyname(line)))
addr->s_addr = *((unsigned long *) host->h_addr_list[0]);
else retval = 0;
}
return retval;
}
static int read_mac(const char *line, void *arg)
{
uint8_t *mac_bytes = arg;
struct ether_addr *temp_ether_addr;
int retval = 1;
temp_ether_addr = ether_aton(line);
if(temp_ether_addr == NULL)
retval = 0;
else
memcpy(mac_bytes, temp_ether_addr, 6);
return retval;
}
static int read_str(char *line, void *arg)
static int read_str(const char *line, void *arg)
{
char **dest = arg;
int i;
if (*dest) free(*dest);
free(*dest);
*dest = strdup(line);
/* elimate trailing whitespace */
for (i = strlen(*dest) - 1; i > 0 && isspace((*dest)[i]); i--);
(*dest)[i > 0 ? i + 1 : 0] = '\0';
return 1;
}
static int read_u32(char *line, void *arg)
static int read_u32(const char *line, void *arg)
{
u_int32_t *dest = arg;
*dest = strtoul(line, NULL, 0);
return 1;
uint32_t *dest = arg;
char *endptr;
*dest = strtoul(line, &endptr, 0);
return endptr[0] == '\0';
}
static int read_yn(char *line, void *arg)
static int read_yn(const char *line, void *arg)
{
char *dest = arg;
if (!strcasecmp("yes", line) || !strcmp("1", line) || !strcasecmp("true", line))
int retval = 1;
if (!strcasecmp("yes", line))
*dest = 1;
else if (!strcasecmp("no", line) || !strcmp("0", line) || !strcasecmp("false", line))
else if (!strcasecmp("no", line))
*dest = 0;
else return 0;
return 1;
else retval = 0;
return retval;
}
/* find option 'code' in opt_list */
struct option_set *find_option(struct option_set *opt_list, char code)
{
while (opt_list && opt_list->data[OPT_CODE] < code)
opt_list = opt_list->next;
if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
else return NULL;
}
/* add an option to the opt_list */
static void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
{
struct option_set *existing, *new, **curr;
/* add it to an existing option */
if ((existing = find_option(*opt_list, option->code))) {
DEBUG(LOG_INFO, "Attaching option %s to existing member of list", option->name);
if (option->flags & OPTION_LIST) {
if (existing->data[OPT_LEN] + length <= 255) {
existing->data = realloc(existing->data,
existing->data[OPT_LEN] + length + 2);
memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
existing->data[OPT_LEN] += length;
} /* else, ignore the data, we could put this in a second option in the future */
} /* else, ignore the new data */
} else {
DEBUG(LOG_INFO, "Attaching option %s to list", option->name);
/* make a new option */
new = xmalloc(sizeof(struct option_set));
new->data = xmalloc(length + 2);
new->data[OPT_CODE] = option->code;
new->data[OPT_LEN] = length;
memcpy(new->data + 2, buffer, length);
curr = opt_list;
while (*curr && (*curr)->data[OPT_CODE] < option->code)
curr = &(*curr)->next;
new->next = *curr;
*curr = new;
}
}
/* read a dhcp option and add it to opt_list */
static int read_opt(char *line, void *arg)
static int read_opt(const char *const_line, void *arg)
{
struct option_set **opt_list = arg;
char *opt, *val;
char fail;
struct dhcp_option *option = NULL;
int length = 0;
char buffer[255];
u_int16_t result_u16;
int16_t result_s16;
u_int32_t result_u32;
int32_t result_s32;
int i;
char *opt, *val, *endptr;
struct dhcp_option *option;
int retval = 0, length;
char buffer[8];
char *line;
uint16_t *result_u16 = (uint16_t *) buffer;
uint32_t *result_u32 = (uint32_t *) buffer;
/* Cheat, the only const line we'll actually get is "" */
line = (char *) const_line;
if (!(opt = strtok(line, " \t="))) return 0;
for (i = 0; options[i].code; i++)
if (!strcmp(options[i].name, opt)) {
option = &(options[i]);
for (option = dhcp_options; option->code; option++)
if (!strcasecmp(option->name, opt))
break;
if (!option->code) return 0;
do {
if (!(val = strtok(NULL, ", \t"))) break;
length = option_lengths[option->flags & TYPE_MASK];
retval = 0;
opt = buffer; /* new meaning for variable opt */
switch (option->flags & TYPE_MASK) {
case OPTION_IP:
retval = read_ip(val, buffer);
break;
case OPTION_IP_PAIR:
retval = read_ip(val, buffer);
if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
if (retval) retval = read_ip(val, buffer + 4);
break;
case OPTION_STRING:
length = strlen(val);
if (length > 0) {
if (length > 254) length = 254;
opt = val;
retval = 1;
}
break;
case OPTION_BOOLEAN:
retval = read_yn(val, buffer);
break;
case OPTION_U8:
buffer[0] = strtoul(val, &endptr, 0);
retval = (endptr[0] == '\0');
break;
case OPTION_U16:
*result_u16 = htons(strtoul(val, &endptr, 0));
retval = (endptr[0] == '\0');
break;
case OPTION_S16:
*result_u16 = htons(strtol(val, &endptr, 0));
retval = (endptr[0] == '\0');
break;
case OPTION_U32:
*result_u32 = htonl(strtoul(val, &endptr, 0));
retval = (endptr[0] == '\0');
break;
case OPTION_S32:
*result_u32 = htonl(strtol(val, &endptr, 0));
retval = (endptr[0] == '\0');
break;
default:
break;
}
if (!option) return 0;
do {
val = strtok(NULL, ", \t");
if (val) {
fail = 0;
length = 0;
switch (option->flags & TYPE_MASK) {
case OPTION_IP:
read_ip(val, buffer);
break;
case OPTION_IP_PAIR:
read_ip(val, buffer);
if ((val = strtok(NULL, ", \t/-")))
read_ip(val, buffer + 4);
else fail = 1;
break;
case OPTION_STRING:
length = strlen(val);
if (length > 254) length = 254;
memcpy(buffer, val, length);
break;
case OPTION_BOOLEAN:
if (!read_yn(val, buffer)) fail = 1;
break;
case OPTION_U8:
buffer[0] = strtoul(val, NULL, 0);
break;
case OPTION_U16:
result_u16 = htons(strtoul(val, NULL, 0));
memcpy(buffer, &result_u16, 2);
break;
case OPTION_S16:
result_s16 = htons(strtol(val, NULL, 0));
memcpy(buffer, &result_s16, 2);
break;
case OPTION_U32:
result_u32 = htonl(strtoul(val, NULL, 0));
memcpy(buffer, &result_u32, 4);
break;
case OPTION_S32:
result_s32 = htonl(strtol(val, NULL, 0));
memcpy(buffer, &result_s32, 4);
break;
default:
break;
}
length += option_lengths[option->flags & TYPE_MASK];
if (!fail)
attach_option(opt_list, option, buffer, length);
} else fail = 1;
} while (!fail && option->flags & OPTION_LIST);
if (retval)
attach_option(opt_list, option, opt, length);
} while (retval && option->flags & OPTION_LIST);
return retval;
}
static int read_staticlease(const char *const_line, void *arg)
{
char *line;
char *mac_string;
char *ip_string;
uint8_t *mac_bytes;
uint32_t *ip;
/* Allocate memory for addresses */
mac_bytes = xmalloc(sizeof(unsigned char) * 8);
ip = xmalloc(sizeof(uint32_t));
/* Read mac */
line = (char *) const_line;
mac_string = strtok(line, " \t");
read_mac(mac_string, mac_bytes);
/* Read ip */
ip_string = strtok(NULL, " \t");
read_ip(ip_string, ip);
addStaticLease(arg, mac_bytes, ip);
#ifdef UDHCP_DEBUG
printStaticLeases(arg);
#endif
return 1;
}
static struct config_keyword keywords[] = {
static const struct config_keyword keywords[] = {
/* keyword handler variable address default */
{"start", read_ip, &(server_config.start), "192.168.0.20"},
{"end", read_ip, &(server_config.end), "192.168.0.254"},
@ -157,82 +264,100 @@ static struct config_keyword keywords[] = {
{"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
{"offer_time", read_u32, &(server_config.offer_time), "60"},
{"min_lease", read_u32, &(server_config.min_lease), "60"},
{"lease_file", read_str, &(server_config.lease_file), "/etc/udhcpd.leases"},
{"pid_file", read_str, &(server_config.pid_file), "/var/run/udhcpd.pid"},
{"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
{"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
{"notify_file", read_str, &(server_config.notify_file), ""},
{"", NULL, NULL, ""}
{"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
{"sname", read_str, &(server_config.sname), ""},
{"boot_file", read_str, &(server_config.boot_file), ""},
{"static_lease",read_staticlease, &(server_config.static_leases), ""},
/*ADDME: static lease */
{"", NULL, NULL, ""}
};
int read_config(char *file)
int read_config(const char *file)
{
FILE *in;
char buffer[80], *token, *line;
int i;
char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
#ifdef UDHCP_DEBUG
char orig[READ_CONFIG_BUF_SIZE];
#endif
int i, lm = 0;
for (i = 0; strlen(keywords[i].keyword); i++)
if (strlen(keywords[i].def))
for (i = 0; keywords[i].keyword[0]; i++)
if (keywords[i].def[0])
keywords[i].handler(keywords[i].def, keywords[i].var);
if (!(in = fopen(file, "r"))) {
LOG(LOG_ERR, "unable to open config file: %s", file);
return 0;
}
while (fgets(buffer, 80, in)) {
while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
lm++;
if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
#ifdef UDHCP_DEBUG
strcpy(orig, buffer);
#endif
if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
token = buffer + strspn(buffer, " \t");
if (*token == '\0') continue;
line = token + strcspn(token, " \t=");
if (*line == '\0') continue;
*line = '\0';
line++;
if (!(token = strtok(buffer, " \t"))) continue;
if (!(line = strtok(NULL, ""))) continue;
/* eat leading whitespace */
line = line + strspn(line, " \t=");
if (*line == '\0') continue;
for (i = 0; strlen(keywords[i].keyword); i++)
/* eat trailing whitespace */
for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
line[i] = '\0';
for (i = 0; keywords[i].keyword[0]; i++)
if (!strcasecmp(token, keywords[i].keyword))
keywords[i].handler(line, keywords[i].var);
if (!keywords[i].handler(line, keywords[i].var)) {
LOG(LOG_ERR, "Failure parsing line %d of %s", lm, file);
DEBUG(LOG_ERR, "unable to parse '%s'", orig);
/* reset back to the default value */
keywords[i].handler(keywords[i].def, keywords[i].var);
}
}
fclose(in);
return 1;
}
/* the dummy var is here so this can be a signal handler */
void write_leases(int dummy)
void write_leases(void)
{
FILE *fp;
unsigned int i;
char buf[255];
time_t curr = time(0);
unsigned long lease_time;
dummy = 0;
unsigned long tmp_time;
if (!(fp = fopen(server_config.lease_file, "w"))) {
LOG(LOG_ERR, "Unable to open %s for writing", server_config.lease_file);
return;
}
for (i = 0; i < server_config.max_leases; i++) {
if (leases[i].yiaddr != 0) {
/* screw with the time in the struct, for easier writing */
tmp_time = leases[i].expires;
if (server_config.remaining) {
if (lease_expired(&(leases[i])))
lease_time = 0;
else lease_time = leases[i].expires - curr;
} else lease_time = leases[i].expires;
lease_time = htonl(lease_time);
fwrite(leases[i].chaddr, 16, 1, fp);
fwrite(&(leases[i].yiaddr), 4, 1, fp);
fwrite(&lease_time, 4, 1, fp);
leases[i].expires = 0;
else leases[i].expires -= curr;
} /* else stick with the time we got */
leases[i].expires = htonl(leases[i].expires);
fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp);
/* Then restore it when done. */
leases[i].expires = tmp_time;
}
}
fclose(fp);
if (server_config.notify_file) {
sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file);
system(buf);
@ -240,36 +365,29 @@ void write_leases(int dummy)
}
void read_leases(char *file)
void read_leases(const char *file)
{
FILE *fp;
unsigned int i = 0;
time_t curr = time(0);
struct dhcpOfferedAddr lease;
if (!(fp = fopen(file, "r"))) {
LOG(LOG_ERR, "Unable to open %s for reading", file);
return;
}
while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) {
/* ADDME: is it a static lease */
if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
leases[i].yiaddr = lease.yiaddr;
leases[i].expires = ntohl(lease.expires);
if (server_config.remaining) leases[i].expires += curr;
memcpy(leases[i].chaddr, lease.chaddr, sizeof(lease.chaddr));
lease.expires = ntohl(lease.expires);
if (!server_config.remaining) lease.expires -= time(0);
if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
LOG(LOG_WARNING, "Too many leases while loading %s\n", file);
break;
}
i++;
}
}
DEBUG(LOG_INFO, "Read %d leases", i);
if (i == server_config.max_leases) {
if (fgetc(fp) == EOF)
/* might be helpfull to drop expired leases */
LOG(LOG_WARNING, "Too many leases while loading %s\n", file);
}
fclose(fp);
}

14
files.h
View File

@ -3,15 +3,17 @@
#define _FILES_H
struct config_keyword {
char keyword[20];
int (*handler)(char *line, void *var);
const char *keyword;
int (* const handler)(const char *line, void *var);
void *var;
char def[40];
const char *def;
};
int read_config(char *file);
void write_leases(int dummy);
void read_leases(char *file);
int read_config(const char *file);
void write_leases(void);
void read_leases(const char *file);
struct option_set *find_option(struct option_set *opt_list, char code);
#endif

View File

@ -1,16 +1,16 @@
#include <string.h>
extern int udhcpd(int argc, char *argv[]);
extern int udhcpc(int argc, char *argv[]);
extern int udhcpd_main(int argc, char *argv[]);
extern int udhcpc_main(int argc, char *argv[]);
int main(int argc, char *argv[])
{
int ret = 0;
char *base = strrchr(argv[0], '/');
if (strstr(base ? (base + 1) : argv[0], "dhcpd"))
ret = udhcpd(argc, argv);
else ret = udhcpc(argc, argv);
ret = udhcpd_main(argc, argv);
else ret = udhcpc_main(argc, argv);
return ret;
}

130
leases.c
View File

@ -1,5 +1,5 @@
/*
* leases.c -- tools to manage DHCP leases
/*
* leases.c -- tools to manage DHCP leases
* Russ Dill <Russ.Dill@asu.edu> July 2001
*/
@ -9,47 +9,49 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include "debug.h"
#include "dhcpd.h"
#include "files.h"
#include "options.h"
#include "leases.h"
#include "arpping.h"
#include "common.h"
#include "static_leases.h"
uint8_t blank_chaddr[] = {[0 ... 15] = 0};
/* clear every lease out that chaddr OR yiaddr matches and is nonzero */
void clear_lease(u_int8_t *chaddr, u_int32_t yiaddr)
void clear_lease(uint8_t *chaddr, uint32_t yiaddr)
{
unsigned int i, blank_chaddr = 0, blank_yiaddr = 0;
for (i = 0; i < 16 && !chaddr[i]; i++);
if (i == 16) blank_chaddr = 1;
blank_yiaddr = (yiaddr == 0);
unsigned int i, j;
for (j = 0; j < 16 && !chaddr[j]; j++);
for (i = 0; i < server_config.max_leases; i++)
if ((!blank_chaddr && !memcmp(leases[i].chaddr, chaddr, 16)) ||
(!blank_yiaddr && leases[i].yiaddr == yiaddr)) {
if ((j != 16 && !memcmp(leases[i].chaddr, chaddr, 16)) ||
(yiaddr && leases[i].yiaddr == yiaddr)) {
memset(&(leases[i]), 0, sizeof(struct dhcpOfferedAddr));
}
}
/* add a lease into the table, clearing out any old ones */
struct dhcpOfferedAddr *add_lease(u_int8_t *chaddr, u_int32_t yiaddr, unsigned long lease)
struct dhcpOfferedAddr *add_lease(uint8_t *chaddr, uint32_t yiaddr, unsigned long lease)
{
struct dhcpOfferedAddr *oldest;
/* clean out any old ones */
clear_lease(chaddr, yiaddr);
oldest = oldest_expired_lease();
if (oldest) {
memcpy(oldest->chaddr, chaddr, 16);
oldest->yiaddr = yiaddr;
oldest->expires = time(0) + lease;
}
return oldest;
}
@ -58,7 +60,7 @@ struct dhcpOfferedAddr *add_lease(u_int8_t *chaddr, u_int32_t yiaddr, unsigned l
int lease_expired(struct dhcpOfferedAddr *lease)
{
return (lease->expires < (unsigned long) time(0));
}
}
/* Find the oldest expired lease, NULL if there are no expired leases */
@ -68,85 +70,89 @@ struct dhcpOfferedAddr *oldest_expired_lease(void)
unsigned long oldest_lease = time(0);
unsigned int i;
for (i = 0; i < server_config.max_leases; i++)
if (oldest_lease > leases[i].expires) {
oldest_lease = leases[i].expires;
oldest = &(leases[i]);
}
return oldest;
}
/* Find the first lease that matches chaddr, NULL if no match */
struct dhcpOfferedAddr *find_lease_by_chaddr(u_int8_t *chaddr)
struct dhcpOfferedAddr *find_lease_by_chaddr(uint8_t *chaddr)
{
unsigned int i;
for (i = 0; i < server_config.max_leases; i++)
if (!memcmp(leases[i].chaddr, chaddr, 16)) return &(leases[i]);
return NULL;
}
/* Find the first lease that matches yiaddr, NULL is no match */
struct dhcpOfferedAddr *find_lease_by_yiaddr(u_int32_t yiaddr)
struct dhcpOfferedAddr *find_lease_by_yiaddr(uint32_t yiaddr)
{
unsigned int i;
for (i = 0; i < server_config.max_leases; i++)
if (leases[i].yiaddr == yiaddr) return &(leases[i]);
return NULL;
}
/* find an assignable address, it check_expired is true, we check all the expired leases as well.
* Maybe this should try expired leases by age... */
u_int32_t find_address(int check_expired)
{
u_int32_t addr, ret = 0;
struct dhcpOfferedAddr *lease = NULL;
addr = server_config.start;
for (;ntohl(addr) < ntohl(server_config.end) ;addr = htonl(ntohl(addr) + 1)) {
/* ie, 192.168.55.0 */
if (!(ntohl(addr) & 0xFF)) continue;
/* ie, 192.168.55.255 */
if ((ntohl(addr) & 0xFF) == 0xFF) continue;
/* lease is not taken */
if ((!(lease = find_lease_by_yiaddr(addr)) ||
/* or it expired and we are checking for expired leases */
(check_expired && lease_expired(lease))) &&
/* and it isn't on the network */
!check_ip(addr)) {
ret = addr;
break;
}
}
return ret;
}
/* check is an IP is taken, if it is, add it to the lease table */
int check_ip(u_int32_t addr)
static int check_ip(uint32_t addr)
{
char blank_chaddr[] = {[0 ... 15] = 0};
struct in_addr temp;
if (!arpping(addr, server_config.server, server_config.arp, server_config.interface)) {
if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) {
temp.s_addr = addr;
LOG(LOG_INFO, "%s belongs to someone, reserving it for %ld seconds",
inet_ntoa(temp), server_config.conflict_time);
LOG(LOG_INFO, "%s belongs to someone, reserving it for %ld seconds",
inet_ntoa(temp), server_config.conflict_time);
add_lease(blank_chaddr, addr, server_config.conflict_time);
return 1;
} else return 0;
}
/* find an assignable address, it check_expired is true, we check all the expired leases as well.
* Maybe this should try expired leases by age... */
uint32_t find_address(int check_expired)
{
uint32_t addr, ret;
struct dhcpOfferedAddr *lease = NULL;
addr = ntohl(server_config.start); /* addr is in host order here */
for (;addr <= ntohl(server_config.end); addr++) {
/* ie, 192.168.55.0 */
if (!(addr & 0xFF)) continue;
/* ie, 192.168.55.255 */
if ((addr & 0xFF) == 0xFF) continue;
/* Only do if it isn't an assigned as a static lease */
if(!reservedIp(server_config.static_leases, htonl(addr)))
{
/* lease is not taken */
ret = htonl(addr);
if ((!(lease = find_lease_by_yiaddr(ret)) ||
/* or it expired and we are checking for expired leases */
(check_expired && lease_expired(lease))) &&
/* and it isn't on the network */
!check_ip(ret)) {
return ret;
break;
}
}
}
return 0;
}

View File

@ -4,20 +4,20 @@
struct dhcpOfferedAddr {
u_int8_t chaddr[16];
u_int32_t yiaddr; /* network order */
u_int32_t expires; /* host order */
uint8_t chaddr[16];
uint32_t yiaddr; /* network order */
uint32_t expires; /* host order */
};
extern uint8_t blank_chaddr[];
void clear_lease(u_int8_t *chaddr, u_int32_t yiaddr);
struct dhcpOfferedAddr *add_lease(u_int8_t *chaddr, u_int32_t yiaddr, unsigned long lease);
void clear_lease(uint8_t *chaddr, uint32_t yiaddr);
struct dhcpOfferedAddr *add_lease(uint8_t *chaddr, uint32_t yiaddr, unsigned long lease);
int lease_expired(struct dhcpOfferedAddr *lease);
struct dhcpOfferedAddr *oldest_expired_lease(void);
struct dhcpOfferedAddr *find_lease_by_chaddr(u_int8_t *chaddr);
struct dhcpOfferedAddr *find_lease_by_yiaddr(u_int32_t yiaddr);
u_int32_t find_address(int check_expired);
int check_ip(u_int32_t addr);
struct dhcpOfferedAddr *find_lease_by_chaddr(uint8_t *chaddr);
struct dhcpOfferedAddr *find_lease_by_yiaddr(uint32_t yiaddr);
uint32_t find_address(int check_expired);
#endif

92
libbb_udhcp.h Normal file
View File

@ -0,0 +1,92 @@
/* libbb_udhcp.h - busybox compatibility wrapper */
/* bit of a hack, do this no matter what the order of the includes.
* (for busybox) */
#ifndef _LIBBB_UDHCP_H
#define _LIBBB_UDHCP_H
#ifdef CONFIG_INSTALL_NO_USR
# define DEFAULT_SCRIPT "/share/udhcpc/default.script"
#else
# define DEFAULT_SCRIPT "/usr/share/udhcpc/default.script"
#endif
#ifdef IN_BUSYBOX
#include "busybox.h"
#ifdef CONFIG_FEATURE_UDHCP_SYSLOG
#define UDHCP_SYSLOG
#endif
#ifdef CONFIG_FEATURE_UDHCP_DEBUG
#define UDHCP_DEBUG
#endif
#define COMBINED_BINARY
#include "version.h"
#define xfopen bb_xfopen
/* make safe the exported namespace */
/* from common.h */
#define background udhcp_background
#define start_log_and_pid udhcp_start_log_and_pid
/* from script.h */
#define run_script udhcp_run_script
/* from packet.h */
#define init_header udhcp_init_header
#define get_packet udhcp_get_packet
#define checksum udhcp_checksum
#define raw_packet udhcp_raw_packet
#define kernel_packet udhcp_kernel_packet
/* from pidfile.h */
#define pidfile_acquire udhcp_pidfile_acquire
#define pidfile_write_release udhcp_pidfile_write_release
/* from options.h */
#define get_option udhcp_get_option
#define end_option udhcp_end_option
#define add_option_string udhcp_add_option_string
#define add_simple_option udhcp_add_simple_option
#define option_lengths udhcp_option_lengths
/* from socket.h */
#define listen_socket udhcp_listen_socket
#define read_interface udhcp_read_interface
/* from dhcpc.h */
#define client_config udhcp_client_config
/* from dhcpd.h */
#define server_config udhcp_server_config
#else /* ! IN_BUSYBOX */
#include <stdlib.h>
#include <stdio.h>
#include <sys/sysinfo.h>
#ifndef ATTRIBUTE_NORETURN
#define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
#endif /* ATTRIBUTE_NORETURN */
#ifndef ATTRIBUTE_PACKED
#define ATTRIBUTE_PACKED __attribute__ ((__packed__))
#endif /* ATTRIBUTE_PACKED */
#define TRUE 1
#define FALSE 0
#define xmalloc malloc
#define xcalloc calloc
static inline FILE *xfopen(const char *file, const char *mode)
{
FILE *fp;
if (!(fp = fopen(file, mode))) {
perror("could not open input file");
exit(0);
}
return fp;
}
#endif /* IN_BUSYBOX */
#endif /* _LIBBB_UDHCP_H */

189
options.c
View File

@ -1,47 +1,48 @@
/*
* options.c -- DHCP server option packet tools
/*
* options.c -- DHCP server option packet tools
* Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "common.h"
#include "dhcpd.h"
#include "files.h"
#include "options.h"
#include "leases.h"
#include "files.h"
/* supported options are easily added here */
struct dhcp_option options[] = {
/* name[10] flags code */
{"subnet", OPTION_IP, 0x01},
{"timezone", OPTION_S32, 0x02},
{"router", OPTION_IP | OPTION_LIST, 0x03},
{"timesvr", OPTION_IP | OPTION_LIST, 0x04},
{"namesvr", OPTION_IP | OPTION_LIST, 0x05},
{"dns", OPTION_IP | OPTION_LIST, 0x06},
{"logsvr", OPTION_IP | OPTION_LIST, 0x07},
{"cookiesvr", OPTION_IP | OPTION_LIST, 0x08},
{"lprsvr", OPTION_IP | OPTION_LIST, 0x09},
{"hostname", OPTION_STRING, 0x0c},
{"bootsize", OPTION_U16, 0x0d},
{"domain", OPTION_STRING, 0x0f},
{"swapsvr", OPTION_IP, 0x10},
{"rootpath", OPTION_STRING, 0x11},
{"ipttl", OPTION_U8, 0x17},
{"mtu", OPTION_U16, 0x1a},
{"broadcast", OPTION_IP, 0x1c},
{"ntpsrv", OPTION_IP | OPTION_LIST, 0x2a},
{"wins", OPTION_IP | OPTION_LIST, 0x2c},
{"requestip", OPTION_IP, 0x32},
{"lease", OPTION_U32, 0x33},
{"dhcptype", OPTION_U8, 0x35},
{"serverid", OPTION_IP, 0x36},
{"tftp", OPTION_STRING, 0x42},
{"bootfile", OPTION_STRING, 0x43},
struct dhcp_option dhcp_options[] = {
/* name[10] flags code */
{"subnet", OPTION_IP | OPTION_REQ, 0x01},
{"timezone", OPTION_S32, 0x02},
{"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03},
{"timesvr", OPTION_IP | OPTION_LIST, 0x04},
{"namesvr", OPTION_IP | OPTION_LIST, 0x05},
{"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06},
{"logsvr", OPTION_IP | OPTION_LIST, 0x07},
{"cookiesvr", OPTION_IP | OPTION_LIST, 0x08},
{"lprsvr", OPTION_IP | OPTION_LIST, 0x09},
{"hostname", OPTION_STRING | OPTION_REQ, 0x0c},
{"bootsize", OPTION_U16, 0x0d},
{"domain", OPTION_STRING | OPTION_REQ, 0x0f},
{"swapsvr", OPTION_IP, 0x10},
{"rootpath", OPTION_STRING, 0x11},
{"ipttl", OPTION_U8, 0x17},
{"mtu", OPTION_U16, 0x1a},
{"broadcast", OPTION_IP | OPTION_REQ, 0x1c},
{"nisdomain", OPTION_STRING | OPTION_REQ, 0x28},
{"nissrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x29},
{"ntpsrv", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a},
{"wins", OPTION_IP | OPTION_LIST, 0x2c},
{"requestip", OPTION_IP, 0x32},
{"lease", OPTION_U32, 0x33},
{"dhcptype", OPTION_U8, 0x35},
{"serverid", OPTION_IP, 0x36},
{"message", OPTION_STRING, 0x38},
{"tftp", OPTION_STRING, 0x42},
{"bootfile", OPTION_STRING, 0x43},
{"", 0x00, 0x00}
};
@ -50,7 +51,7 @@ int option_lengths[] = {
[OPTION_IP] = 4,
[OPTION_IP_PAIR] = 8,
[OPTION_BOOLEAN] = 1,
[OPTION_STRING] = 0,
[OPTION_STRING] = 1,
[OPTION_U8] = 1,
[OPTION_U16] = 2,
[OPTION_S16] = 2,
@ -60,35 +61,34 @@ int option_lengths[] = {
/* get an option with bounds checking (warning, not aligned). */
unsigned char *get_option(struct dhcpMessage *packet, int code)
uint8_t *get_option(struct dhcpMessage *packet, int code)
{
int i, length;
static char err[] = "bogus packet, option fields too long."; /* save a few bytes */
unsigned char *optionptr;
uint8_t *optionptr;
int over = 0, done = 0, curr = OPTION_FIELD;
optionptr = packet->options;
i = 0;
length = 308;
while (!done) {
if (i >= length) {
LOG(LOG_WARNING, err);
LOG(LOG_WARNING, "bogus packet, option fields too long.");
return NULL;
}
if (optionptr[i + OPT_CODE] == code) {
if (i + 1 + optionptr[i + OPT_LEN] >= length) {
LOG(LOG_WARNING, err);
LOG(LOG_WARNING, "bogus packet, option fields too long.");
return NULL;
}
return optionptr + i + 2;
}
}
switch (optionptr[i + OPT_CODE]) {
case DHCP_PADDING:
i++;
break;
case DHCP_OPTION_OVER:
if (i + 1 + optionptr[i + OPT_LEN] >= length) {
LOG(LOG_WARNING, err);
LOG(LOG_WARNING, "bogus packet, option fields too long.");
return NULL;
}
over = optionptr[i + 3];
@ -116,10 +116,10 @@ unsigned char *get_option(struct dhcpMessage *packet, int code)
/* return the position of the 'end' option (no bounds checking) */
int end_option(unsigned char *optionptr)
int end_option(uint8_t *optionptr)
{
int i = 0;
while (optionptr[i] != DHCP_END) {
if (optionptr[i] == DHCP_PADDING) i++;
else i += optionptr[i + OPT_LEN] + 2;
@ -130,15 +130,13 @@ int end_option(unsigned char *optionptr)
/* add an option string to the options (an option string contains an option code,
* length, then data) */
int add_option_string(unsigned char *optionptr, unsigned char *string)
int add_option_string(uint8_t *optionptr, uint8_t *string)
{
int i, end = end_option(optionptr);
int end = end_option(optionptr);
/* end position + string length + option code/length + end option */
if (end + string[OPT_LEN] + 2 + 1 >= 308) {
for (i = 0; options[i].code && options[i].code != string[OPT_CODE]; i++);
LOG(LOG_ERR, "Option %s (0x%02x) did not fit into the packet!",
options[i].code ? options[i].name : "unknown", string[OPT_CODE]);
LOG(LOG_ERR, "Option 0x%02x did not fit into the packet!", string[OPT_CODE]);
return 0;
}
DEBUG(LOG_INFO, "adding option 0x%02x", string[OPT_CODE]);
@ -149,81 +147,26 @@ int add_option_string(unsigned char *optionptr, unsigned char *string)
/* add a one to four byte option to a packet */
int add_simple_option(unsigned char *optionptr, unsigned char code, u_int32_t data)
int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
{
char length = 0;
int i, end;
char buffer[4]; /* Cant copy straight to optionptr, it might not be aligned */
struct dhcp_option *dh;
for (i = 0; options[i].code; i++)
if (options[i].code == code) {
length = option_lengths[options[i].flags & TYPE_MASK];
break;
for (dh=dhcp_options; dh->code; dh++) {
if (dh->code == code) {
uint8_t option[6], len;
option[OPT_CODE] = code;
len = option_lengths[dh->flags & TYPE_MASK];
option[OPT_LEN] = len;
if (__BYTE_ORDER == __BIG_ENDIAN)
data <<= 8 * (4 - len);
/* This memcpy is for broken processors which can't
* handle a simple unaligned 32-bit assignment */
memcpy(&option[OPT_DATA], &data, 4);
return add_option_string(optionptr, option);
}
if (!length) {
DEBUG(LOG_ERR, "Could not add option 0x%02x", code);
return 0;
}
DEBUG(LOG_INFO, "adding option 0x%02x", code);
end = end_option(optionptr);
optionptr[end + OPT_CODE] = code;
optionptr[end + OPT_LEN] = length;
switch (length) {
case 1: buffer[0] = (char) data; break;
case 2: *((u_int16_t *) buffer) = htons(data); break;
case 4: *((u_int32_t *) buffer) = htonl(data); break;
}
memcpy(&optionptr[end + 2], buffer, length);
optionptr[end + length + 2] = DHCP_END;
return length;
}
/* find option 'code' in opt_list */
struct option_set *find_option(struct option_set *opt_list, char code)
{
while (opt_list && opt_list->data[OPT_CODE] < code)
opt_list = opt_list->next;
if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
else return NULL;
}
/* add an option to the opt_list */
void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length)
{
struct option_set *existing, *new, **curr;
/* add it to an existing option */
if ((existing = find_option(*opt_list, option->code))) {
DEBUG(LOG_INFO, "Attaching option %s to existing member of list", option->name);
if (option->flags & OPTION_LIST) {
if (existing->data[OPT_LEN] + length <= 255) {
existing->data = realloc(existing->data,
existing->data[OPT_LEN] + length + 2);
memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
existing->data[OPT_LEN] += length;
} /* else, ignore the data, we could put this in a second option in the future */
} /* else, ignore the new data */
} else {
DEBUG(LOG_INFO, "Attaching option %s to list", option->name);
/* make a new option */
new = malloc(sizeof(struct option_set));
new->data = malloc(length + 2);
new->data[OPT_CODE] = option->code;
new->data[OPT_LEN] = length;
memcpy(new->data + 2, buffer, length);
curr = opt_list;
while (*curr && (*curr)->data[OPT_CODE] < option->code)
curr = &(*curr)->next;
new->next = *curr;
*curr = new;
}
DEBUG(LOG_ERR, "Could not add option 0x%02x", code);
return 0;
}

View File

@ -18,22 +18,21 @@ enum {
OPTION_S32
};
#define OPTION_LIST 0x80
#define OPTION_REQ 0x10 /* have the client request this option */
#define OPTION_LIST 0x20 /* There can be a list of 1 or more of these */
struct dhcp_option {
char name[10];
char flags;
char code;
uint8_t code;
};
extern struct dhcp_option options[];
extern struct dhcp_option dhcp_options[];
extern int option_lengths[];
unsigned char *get_option(struct dhcpMessage *packet, int code);
int end_option(unsigned char *optionptr);
int add_option_string(unsigned char *optionptr, unsigned char *string);
int add_simple_option(unsigned char *optionptr, unsigned char code, u_int32_t data);
struct option_set *find_option(struct option_set *opt_list, char code);
void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length);
uint8_t *get_option(struct dhcpMessage *packet, int code);
int end_option(uint8_t *optionptr);
int add_option_string(uint8_t *optionptr, uint8_t *string);
int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data);
#endif

157
packet.c
View File

@ -4,7 +4,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <features.h>
#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
#if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION
#include <netpacket/packet.h>
#include <net/ethernet.h>
#else
@ -14,120 +14,128 @@
#endif
#include <errno.h>
#include "common.h"
#include "packet.h"
#include "debug.h"
#include "dhcpd.h"
#include "options.h"
/* read a packet from socket fd */
void init_header(struct dhcpMessage *packet, char type)
{
memset(packet, 0, sizeof(struct dhcpMessage));
switch (type) {
case DHCPDISCOVER:
case DHCPREQUEST:
case DHCPRELEASE:
case DHCPINFORM:
packet->op = BOOTREQUEST;
break;
case DHCPOFFER:
case DHCPACK:
case DHCPNAK:
packet->op = BOOTREPLY;
}
packet->htype = ETH_10MB;
packet->hlen = ETH_10MB_LEN;
packet->cookie = htonl(DHCP_MAGIC);
packet->options[0] = DHCP_END;
add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
}
/* read a packet from socket fd, return -1 on read error, -2 on packet error */
int get_packet(struct dhcpMessage *packet, int fd)
{
int bytes;
int i;
char broken_vendors[][8] = {
static const char broken_vendors[][8] = {
"MSFT 98",
""
};
char *vendor;
int bytes;
int i;
char unsigned *vendor;
memset(packet, 0, sizeof(struct dhcpMessage));
bytes = read(fd, packet, sizeof(struct dhcpMessage));
if (bytes < 0) {
DEBUG(LOG_INFO, "couldn't read on listening socket -- ignoring");
DEBUG(LOG_INFO, "couldn't read on listening socket, ignoring");
return -1;
}
if (ntohl(packet->cookie) != DHCP_MAGIC) {
LOG(LOG_ERR, "received bogus message -- ignoring");
return -1;
LOG(LOG_ERR, "received bogus message, ignoring");
return -2;
}
DEBUG(LOG_INFO, "oooooh!!! got some!");
DEBUG(LOG_INFO, "Received a packet");
if (packet->op == BOOTREQUEST && (vendor = get_option(packet, DHCP_VENDOR))) {
for (i = 0; broken_vendors[i][0]; i++) {
if (vendor[OPT_LEN - 2] == (signed) strlen(broken_vendors[i]) &&
!strncmp(vendor, broken_vendors[i], vendor[OPT_LEN - 2]) &&
!(ntohs(packet->flags) & BROADCAST_FLAG)) {
DEBUG(LOG_INFO, "broken client (%s), forcing broadcast",
broken_vendors[i]);
packet->flags |= htons(BROADCAST_FLAG);
if (vendor[OPT_LEN - 2] == (uint8_t) strlen(broken_vendors[i]) &&
!strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])) {
DEBUG(LOG_INFO, "broken client (%s), forcing broadcast",
broken_vendors[i]);
packet->flags |= htons(BROADCAST_FLAG);
}
}
}
return bytes;
}
/* Calculate the length of a packet, and make sure its a multiple of 2 */
static int calc_length(struct dhcpMessage *payload)
{
int payload_length;
payload_length = sizeof(struct dhcpMessage) - 308;
payload_length += end_option(payload->options) + 1;
if (payload_length % 2) {
payload_length++;
*((char *) payload + payload_length - 1) = '\0';
}
DEBUG(LOG_INFO, "payload length is %d bytes", payload_length);
return payload_length;
}
u_int16_t checksum(void *addr, int count)
uint16_t checksum(void *addr, int count)
{
/* Compute Internet Checksum for "count" bytes
* beginning at location "addr".
*/
register int32_t sum = 0;
u_int16_t *source = (u_int16_t *) addr;
uint16_t *source = (uint16_t *) addr;
while( count > 1 ) {
while (count > 1) {
/* This is the inner loop */
sum += *source++;
count -= 2;
}
/* Add left-over byte, if any */
if( count > 0 )
sum += * (unsigned char *) source;
if (count > 0) {
/* Make sure that the left-over byte is added correctly both
* with little and big endian hosts */
uint16_t tmp = 0;
*(uint8_t *) (&tmp) = * (uint8_t *) source;
sum += tmp;
}
/* Fold 32-bit sum to 16 bits */
while (sum>>16)
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
return ~sum;
}
/* Constuct a ip/udp header for a packet, and specify the source and dest hardware address */
int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port,
u_int32_t dest_ip, int dest_port, char *dest_arp, int ifindex)
/* Construct a ip/udp header for a packet, and specify the source and dest hardware address */
int raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
uint32_t dest_ip, int dest_port, uint8_t *dest_arp, int ifindex)
{
int fd;
int result, payload_length = calc_length(payload);
int result;
struct sockaddr_ll dest;
struct udp_dhcp_packet packet;
if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
DEBUG(LOG_ERR, "socket call failed: %s", sys_errlist[errno]);
DEBUG(LOG_ERR, "socket call failed: %m");
return -1;
}
memset(&dest, 0, sizeof(dest));
memset(&packet, 0, sizeof(packet));
dest.sll_family = AF_PACKET;
dest.sll_protocol = htons(ETH_P_IP);
dest.sll_ifindex = ifindex;
dest.sll_halen = 6;
memcpy(dest.sll_addr, dest_arp, 6);
if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
DEBUG(LOG_ERR, "bind call failed: %s", sys_errlist[errno]);
DEBUG(LOG_ERR, "bind call failed: %m");
close(fd);
return -1;
}
@ -135,22 +143,22 @@ int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port
packet.ip.protocol = IPPROTO_UDP;
packet.ip.saddr = source_ip;
packet.ip.daddr = dest_ip;
packet.ip.tot_len = htons(sizeof(packet.udp) + payload_length); /* cheat on the psuedo-header */
packet.udp.source = htons(source_port);
packet.udp.dest = htons(dest_port);
packet.udp.len = htons(sizeof(packet.udp) + payload_length);
memcpy(&(packet.data), payload, payload_length);
packet.udp.check = checksum(&packet, sizeof(packet.ip) + sizeof(packet.udp) + payload_length);
packet.ip.tot_len = htons(sizeof(packet.ip) + sizeof(packet.udp) + payload_length);
packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat on the psuedo-header */
packet.ip.tot_len = packet.udp.len;
memcpy(&(packet.data), payload, sizeof(struct dhcpMessage));
packet.udp.check = checksum(&packet, sizeof(struct udp_dhcp_packet));
packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet));
packet.ip.ihl = sizeof(packet.ip) >> 2;
packet.ip.version = IPVERSION;
packet.ip.ttl = IPDEFTTL;
packet.ip.check = checksum(&(packet.ip), sizeof(packet.ip));
result = sendto(fd, &packet, ntohs(packet.ip.tot_len), 0, (struct sockaddr *) &dest, sizeof(dest));
result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, (struct sockaddr *) &dest, sizeof(dest));
if (result <= 0) {
DEBUG(LOG_ERR, "write on socket failed: %s", sys_errlist[errno]);
DEBUG(LOG_ERR, "write on socket failed: %m");
}
close(fd);
return result;
@ -158,37 +166,42 @@ int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port
/* Let the kernel do all the work for packet generation */
int kernel_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port,
u_int32_t dest_ip, int dest_port)
int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
uint32_t dest_ip, int dest_port)
{
int n = 1;
int fd, result, payload_length = calc_length(payload);
int fd, result;
struct sockaddr_in client;
if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
return -1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1)
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) {
close(fd);
return -1;
}
memset(&client, 0, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons(source_port);
client.sin_addr.s_addr = source_ip;
if (bind(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1)
if (bind(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
close(fd);
return -1;
}
memset(&client, 0, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons(dest_port);
client.sin_addr.s_addr = dest_ip;
client.sin_addr.s_addr = dest_ip;
if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1)
if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
close(fd);
return -1;
}
result = write(fd, payload, payload_length);
result = write(fd, payload, sizeof(struct dhcpMessage));
close(fd);
return result;
}
}

View File

@ -5,22 +5,22 @@
#include <netinet/ip.h>
struct dhcpMessage {
u_int8_t op;
u_int8_t htype;
u_int8_t hlen;
u_int8_t hops;
u_int32_t xid;
u_int16_t secs;
u_int16_t flags;
u_int32_t ciaddr;
u_int32_t yiaddr;
u_int32_t siaddr;
u_int32_t giaddr;
u_int8_t chaddr[16];
u_int8_t sname[64];
u_int8_t file[128];
u_int32_t cookie;
u_int8_t options[308]; /* 312 - cookie */
uint8_t op;
uint8_t htype;
uint8_t hlen;
uint8_t hops;
uint32_t xid;
uint16_t secs;
uint16_t flags;
uint32_t ciaddr;
uint32_t yiaddr;
uint32_t siaddr;
uint32_t giaddr;
uint8_t chaddr[16];
uint8_t sname[64];
uint8_t file[128];
uint32_t cookie;
uint8_t options[308]; /* 312 - cookie */
};
struct udp_dhcp_packet {
@ -29,12 +29,13 @@ struct udp_dhcp_packet {
struct dhcpMessage data;
};
void init_header(struct dhcpMessage *packet, char type);
int get_packet(struct dhcpMessage *packet, int fd);
u_int16_t checksum(void *addr, int count);
int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port,
u_int32_t dest_ip, int dest_port, char *dest_arp, int ifindex);
int kernel_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port,
u_int32_t dest_ip, int dest_port);
uint16_t checksum(void *addr, int count);
int raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
uint32_t dest_ip, int dest_port, uint8_t *dest_arp, int ifindex);
int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
uint32_t dest_ip, int dest_port);
#endif

75
pidfile.c Normal file
View File

@ -0,0 +1,75 @@
/* pidfile.c
*
* Functions to assist in the writing and removing of pidfiles.
*
* Russ Dill <Russ.Dill@asu.edu> September 2001
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
#include "pidfile.h"
static char *saved_pidfile;
static void pidfile_delete(void)
{
if (saved_pidfile) unlink(saved_pidfile);
}
int pidfile_acquire(const char *pidfile)
{
int pid_fd;
if (!pidfile) return -1;
pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644);
if (pid_fd < 0) {
LOG(LOG_ERR, "Unable to open pidfile %s: %m\n", pidfile);
} else {
lockf(pid_fd, F_LOCK, 0);
if (!saved_pidfile)
atexit(pidfile_delete);
saved_pidfile = (char *) pidfile;
}
return pid_fd;
}
void pidfile_write_release(int pid_fd)
{
FILE *out;
if (pid_fd < 0) return;
if ((out = fdopen(pid_fd, "w")) != NULL) {
fprintf(out, "%d\n", getpid());
fclose(out);
}
lockf(pid_fd, F_UNLCK, 0);
close(pid_fd);
}

25
pidfile.h Normal file
View File

@ -0,0 +1,25 @@
/* pidfile.h
*
* Functions to assist in the writing and removing of pidfiles.
*
* Russ Dill <Russ.Dill@asu.edu> September 2001
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
int pidfile_acquire(const char *pidfile);
void pidfile_write_release(int pid_fd);

View File

@ -3,7 +3,9 @@ The files in this dir are:
udhcpd.conf: A sample udhpcd configuration file.
sample.script
sample.nak
sample.deconfig
sample.bound
sample.renew : sample scripts for the udhcpc client.
simple.script : a simple sample script for the client.

View File

@ -1,7 +1,7 @@
#!/bin/sh
# Sample udhcpc renew script
RESOLV_CONF="/etc/resolv.conf"
RESOLV_CONF="/etc/udhcpc/resolv.conf"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
[ -n "$subnet" ] && NETMASK="netmask $subnet"

4
samples/sample.nak Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
# Sample udhcpc nak script
echo Received a NAK: $message

View File

@ -4,4 +4,4 @@
# common initialization first, especially if more dhcp event notifications
# are added.
exec /usr/share/udhcpc/default.$1
exec /usr/share/udhcpc/sample.$1

39
samples/simple.script Normal file
View File

@ -0,0 +1,39 @@
#!/bin/sh
# udhcpc script edited by Tim Riker <Tim@Rikers.org>
[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1
RESOLV_CONF="/etc/resolv.conf"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
[ -n "$subnet" ] && NETMASK="netmask $subnet"
case "$1" in
deconfig)
/sbin/ifconfig $interface 0.0.0.0
;;
renew|bound)
/sbin/ifconfig $interface $ip $BROADCAST $NETMASK
if [ -n "$router" ] ; then
echo "deleting routers"
while route del default gw 0.0.0.0 dev $interface ; do
:
done
for i in $router ; do
route add default gw $i dev $interface
done
fi
echo -n > $RESOLV_CONF
[ -n "$domain" ] && echo search $domain >> $RESOLV_CONF
for i in $dns ; do
echo adding dns $i
echo nameserver $i >> $RESOLV_CONF
done
;;
esac
exit 0

View File

@ -33,7 +33,7 @@ interface eth0 #default: eth0
#auto_time 7200 #default: 7200 (2 hours)
# The amount of time that an IP will be reserved (leased) for if a
# The amount of time that an IP will be reserved (leased) for if a
# DHCP decline message is received (seconds).
#decline_time 3600 #default: 3600 (1 hour)
@ -57,10 +57,10 @@ interface eth0 #default: eth0
# The location of the leases file
#lease_file /etc/udhcpd.leases #defualt: /etc/udhcpd.leases
#lease_file /var/lib/misc/udhcpd.leases #defualt: /var/lib/misc/udhcpd.leases
# The location of the pid file
#pid_file /var/run/udhcpd.pid #default: /var/run/udhcpd.pid
#pidfile /var/run/udhcpd.pid #default: /var/run/udhcpd.pid
# Everytime udhcpd writes a leases file, the below script will be called.
# Useful for writing the lease file to flash every few hours.
@ -69,6 +69,14 @@ interface eth0 #default: eth0
#notify_file dumpleases # <--- usefull for debugging
# The following are bootp specific options, setable by udhcpd.
#siaddr 192.168.0.22 #default: 0.0.0.0
#sname zorak #default: (none)
#boot_file /var/nfs_root #default: (none)
# The remainer of options are DHCP options and can be specifed with the
# keyword 'opt' or 'option'. If an option can take multiple items, such
# as the dns option, they can be listed on the same line, or multiple
@ -85,24 +93,24 @@ option lease 864000 # 10 days of seconds
# Currently supported options, for more info, see options.c
#subnet
#timezone
#router
#timesvr
#namesvr
#dns
#logsvr
#cookiesvr
#lprsvr
#bootsize
#domain
#swapsvr
#rootpath
#ipttl
#mtu
#broadcast
#wins
#lease
#ntpsrv
#tftp
#bootfile
#opt subnet
#opt timezone
#opt router
#opt timesvr
#opt namesvr
#opt dns
#opt logsvr
#opt cookiesvr
#opt lprsvr
#opt bootsize
#opt domain
#opt swapsvr
#opt rootpath
#opt ipttl
#opt mtu
#opt broadcast
#opt wins
#opt lease
#opt ntpsrv
#opt tftp
#opt bootfile

213
script.c
View File

@ -1,6 +1,6 @@
/* script.c
*
* Functions to call the DHCP client notification scripts
* Functions to call the DHCP client notification scripts
*
* Russ Dill <Russ.Dill@asu.edu> July 2001
*
@ -28,59 +28,59 @@
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include "common.h"
#include "options.h"
#include "dhcpd.h"
#include "dhcpc.h"
#include "packet.h"
#include "options.h"
#include "debug.h"
#include "script.h"
/* get a rough idea of how long an option will be (rounding up...) */
static int max_option_length(char *option, struct dhcp_option *type)
static const int max_option_length[] = {
[OPTION_IP] = sizeof("255.255.255.255 "),
[OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2,
[OPTION_STRING] = 1,
[OPTION_BOOLEAN] = sizeof("yes "),
[OPTION_U8] = sizeof("255 "),
[OPTION_U16] = sizeof("65535 "),
[OPTION_S16] = sizeof("-32768 "),
[OPTION_U32] = sizeof("4294967295 "),
[OPTION_S32] = sizeof("-2147483684 "),
};
static inline int upper_length(int length, int opt_index)
{
int size = 0;
switch (type->flags & TYPE_MASK) {
case OPTION_IP:
case OPTION_IP_PAIR:
size = (option[OPT_LEN - 2] / 4) * sizeof("255.255.255.255 ");
break;
case OPTION_STRING:
size = option[OPT_LEN - 2] + 1;
break;
case OPTION_BOOLEAN:
size = option[OPT_LEN - 2] * sizeof("yes ");
break;
case OPTION_U8:
size = option[OPT_LEN - 2] * sizeof("255 ");
break;
case OPTION_U16:
size = (option[OPT_LEN - 2] / 2) * sizeof("65535 ");
break;
case OPTION_S16:
size = (option[OPT_LEN - 2] / 2) * sizeof("-32768 ");
break;
case OPTION_U32:
size = (option[OPT_LEN - 2] / 4) * sizeof("4294967295 ");
break;
case OPTION_S32:
size = (option[OPT_LEN - 2] / 4) * sizeof("-2147483684 ");
break;
}
return size;
return max_option_length[opt_index] *
(length / option_lengths[opt_index]);
}
static int sprintip(char *dest, char *pre, uint8_t *ip)
{
return sprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]);
}
/* really simple implementation, just count the bits */
static int mton(struct in_addr *mask)
{
int i;
unsigned long bits = ntohl(mask->s_addr);
/* too bad one can't check the carry bit, etc in c bit
* shifting */
for (i = 0; i < 32 && !((bits >> i) & 1); i++);
return 32 - i;
}
/* Fill dest with the text of option 'option'. */
static void fill_options(char *dest, unsigned char *option, struct dhcp_option *type_p)
static void fill_options(char *dest, uint8_t *option, struct dhcp_option *type_p)
{
int type, optlen;
u_int16_t val_u16;
uint16_t val_u16;
int16_t val_s16;
u_int32_t val_u32;
uint32_t val_u32;
int32_t val_s32;
int len = option[OPT_LEN - 2];
@ -90,17 +90,13 @@ static void fill_options(char *dest, unsigned char *option, struct dhcp_option *
optlen = option_lengths[type];
for(;;) {
switch (type) {
case OPTION_IP: /* Works regardless of host byte order. */
dest += sprintf(dest, "%d.%d.%d.%d",
option[0], option[1],
option[2], option[3]);
break;
case OPTION_IP_PAIR:
dest += sprintf(dest, "%d.%d.%d.%d, %d.%d.%d.%d",
option[0], option[1],
option[2], option[3],
option[4], option[5],
option[6], option[7]);
dest += sprintip(dest, "", option);
*(dest++) = '/';
option += 4;
optlen = 4;
case OPTION_IP: /* Works regardless of host byte order. */
dest += sprintip(dest, "", option);
break;
case OPTION_BOOLEAN:
dest += sprintf(dest, *option ? "yes" : "no");
@ -132,65 +128,77 @@ static void fill_options(char *dest, unsigned char *option, struct dhcp_option *
option += optlen;
len -= optlen;
if (len <= 0) break;
*(dest++) = ' ';
dest += sprintf(dest, " ");
}
}
static char *find_env(const char *prefix, char *defaultstr)
{
extern char **environ;
char **ptr;
const int len = strlen(prefix);
for (ptr = environ; *ptr != NULL; ptr++) {
if (strncmp(prefix, *ptr, len) == 0)
return *ptr;
}
return defaultstr;
}
/* put all the paramaters into an environment */
/* put all the parameters into an environment */
static char **fill_envp(struct dhcpMessage *packet)
{
int num_options = 0;
int i, j;
unsigned char *addr;
char **envp, *temp;
char **envp;
uint8_t *temp;
struct in_addr subnet;
char over = 0;
if (packet == NULL)
num_options = 0;
else {
for (i = 0; options[i].code; i++)
if (get_option(packet, options[i].code))
for (i = 0; dhcp_options[i].code; i++)
if (get_option(packet, dhcp_options[i].code)) {
num_options++;
}
envp = malloc((num_options + 5) * sizeof(char *));
envp[0] = malloc(strlen("interface=") + strlen(client_config.interface) + 1);
sprintf(envp[0], "interface=%s", client_config.interface);
envp[1] = malloc(sizeof("ip=255.255.255.255"));
envp[2] = find_env("PATH", "PATH=/bin:/usr/bin:/sbin:/usr/sbin");
envp[3] = find_env("HOME", "HOME=/");
if (packet == NULL) {
envp[4] = NULL;
return envp;
if (dhcp_options[i].code == DHCP_SUBNET)
num_options++; /* for mton */
}
if (packet->siaddr) num_options++;
if ((temp = get_option(packet, DHCP_OPTION_OVER)))
over = *temp;
if (!(over & FILE_FIELD) && packet->file[0]) num_options++;
if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++;
}
addr = (unsigned char*) &packet->yiaddr;
sprintf(envp[1], "ip=%d.%d.%d.%d",
addr[0], addr[1], addr[2], addr[3]);
for (i = 0, j = 4; options[i].code; i++) {
if ((temp = get_option(packet, options[i].code))) {
envp[j] = malloc(max_option_length(temp, &options[i]) +
strlen(options[i].name) + 2);
fill_options(envp[j], temp, &options[i]);
j++;
envp = xcalloc(sizeof(char *), num_options + 5);
j = 0;
asprintf(&envp[j++], "interface=%s", client_config.interface);
asprintf(&envp[j++], "%s=%s", "PATH",
getenv("PATH") ? : "/bin:/usr/bin:/sbin:/usr/sbin");
asprintf(&envp[j++], "%s=%s", "HOME", getenv("HOME") ? : "/");
if (packet == NULL) return envp;
envp[j] = xmalloc(sizeof("ip=255.255.255.255"));
sprintip(envp[j++], "ip=", (uint8_t *) &packet->yiaddr);
for (i = 0; dhcp_options[i].code; i++) {
if (!(temp = get_option(packet, dhcp_options[i].code)))
continue;
envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2],
dhcp_options[i].flags & TYPE_MASK) + strlen(dhcp_options[i].name) + 2);
fill_options(envp[j++], temp, &dhcp_options[i]);
/* Fill in a subnet bits option for things like /24 */
if (dhcp_options[i].code == DHCP_SUBNET) {
memcpy(&subnet, temp, 4);
asprintf(&envp[j++], "mask=%d", mton(&subnet));
}
}
envp[j] = NULL;
}
if (packet->siaddr) {
envp[j] = xmalloc(sizeof("siaddr=255.255.255.255"));
sprintip(envp[j++], "siaddr=", (uint8_t *) &packet->siaddr);
}
if (!(over & FILE_FIELD) && packet->file[0]) {
/* watch out for invalid packets */
packet->file[sizeof(packet->file) - 1] = '\0';
asprintf(&envp[j++], "boot_file=%s", packet->file);
}
if (!(over & SNAME_FIELD) && packet->sname[0]) {
/* watch out for invalid packets */
packet->sname[sizeof(packet->sname) - 1] = '\0';
asprintf(&envp[j++], "sname=%s", packet->sname);
}
return envp;
}
@ -199,27 +207,28 @@ static char **fill_envp(struct dhcpMessage *packet)
void run_script(struct dhcpMessage *packet, const char *name)
{
int pid;
char **envp;
char **envp, **curr;
if (client_config.script == NULL)
return;
DEBUG(LOG_INFO, "vforking and execle'ing %s", client_config.script);
envp = fill_envp(packet);
/* call script */
pid = fork();
pid = vfork();
if (pid) {
waitpid(pid, NULL, 0);
for (curr = envp; *curr; curr++) free(*curr);
free(envp);
return;
} else if (pid == 0) {
envp = fill_envp(packet);
/* close fd's? */
/* exec script */
DEBUG(LOG_INFO, "execle'ing %s", client_config.script);
execle(client_config.script, client_config.script,
name, NULL, envp);
LOG(LOG_ERR, "script %s failed: %s",
client_config.script, strerror(errno));
LOG(LOG_ERR, "script %s failed: %m", client_config.script);
exit(1);
}
}
}

View File

@ -1,6 +1,6 @@
#ifndef _SCRIPT_H
#define _SCRIPT_H
void run_script(struct dhcpMessage *packet, const char *name);
extern void run_script(struct dhcpMessage *packet, const char *name);
#endif

View File

@ -1,6 +1,6 @@
/* serverpacket.c
*
* Constuct and send DHCP server packets
* Construct and send DHCP server packets
*
* Russ Dill <Russ.Dill@asu.edu> July 2001
*
@ -25,11 +25,11 @@
#include <string.h>
#include <time.h>
#include "packet.h"
#include "debug.h"
#include "common.h"
#include "serverpacket.h"
#include "dhcpd.h"
#include "options.h"
#include "leases.h"
#include "static_leases.h"
/* send a packet to giaddr using the kernel ip stack */
static int send_packet_to_relay(struct dhcpMessage *payload)
@ -44,27 +44,27 @@ static int send_packet_to_relay(struct dhcpMessage *payload)
/* send a packet to a specific arp address and ip address by creating our own ip packet */
static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast)
{
u_int32_t ciaddr;
char chaddr[6];
uint8_t *chaddr;
uint32_t ciaddr;
if (force_broadcast) {
DEBUG(LOG_INFO, "broadcasting packet to client (NAK)");
ciaddr = INADDR_BROADCAST;
memcpy(chaddr, MAC_BCAST_ADDR, 6);
chaddr = MAC_BCAST_ADDR;
} else if (payload->ciaddr) {
DEBUG(LOG_INFO, "unicasting packet to client ciaddr");
ciaddr = payload->ciaddr;
memcpy(chaddr, payload->chaddr, 6);
chaddr = payload->chaddr;
} else if (ntohs(payload->flags) & BROADCAST_FLAG) {
DEBUG(LOG_INFO, "broadcasting packet to client (requested)");
ciaddr = INADDR_BROADCAST;
memcpy(chaddr, MAC_BCAST_ADDR, 6);
chaddr = MAC_BCAST_ADDR;
} else {
DEBUG(LOG_INFO, "unicasting packet to client yiaddr");
ciaddr = payload->yiaddr;
memcpy(chaddr, payload->chaddr, 6);
chaddr = payload->chaddr;
}
return raw_packet(payload, server_config.server, SERVER_PORT,
return raw_packet(payload, server_config.server, SERVER_PORT,
ciaddr, CLIENT_PORT, chaddr, server_config.ifindex);
}
@ -83,20 +83,24 @@ static int send_packet(struct dhcpMessage *payload, int force_broadcast)
static void init_packet(struct dhcpMessage *packet, struct dhcpMessage *oldpacket, char type)
{
memset(packet, 0, sizeof(struct dhcpMessage));
packet->op = BOOTREPLY;
packet->htype = ETH_10MB;
packet->hlen = ETH_10MB_LEN;
init_header(packet, type);
packet->xid = oldpacket->xid;
memcpy(packet->chaddr, oldpacket->chaddr, 16);
packet->cookie = htonl(DHCP_MAGIC);
packet->options[0] = DHCP_END;
packet->flags = oldpacket->flags;
packet->giaddr = oldpacket->giaddr;
packet->ciaddr = oldpacket->ciaddr;
add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
add_simple_option(packet->options, DHCP_SERVER_ID, ntohl(server_config.server)); /* expects host order */
add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server);
}
/* add in the bootp options */
static void add_bootp_options(struct dhcpMessage *packet)
{
packet->siaddr = server_config.siaddr;
if (server_config.sname)
strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
if (server_config.boot_file)
strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
}
@ -105,19 +109,26 @@ int sendOffer(struct dhcpMessage *oldpacket)
{
struct dhcpMessage packet;
struct dhcpOfferedAddr *lease = NULL;
u_int32_t req_align, lease_time_align = server_config.lease;
char *req, *lease_time;
uint32_t req_align, lease_time_align = server_config.lease;
uint8_t *req, *lease_time;
struct option_set *curr;
struct in_addr addr;
uint32_t static_lease_ip;
init_packet(&packet, oldpacket, DHCPOFFER);
static_lease_ip = getIpByMac(server_config.static_leases, oldpacket->chaddr);
/* ADDME: if static, short circuit */
if(!static_lease_ip)
{
/* the client is in our lease/offered table */
if ((lease = find_lease_by_chaddr(oldpacket->chaddr))) {
if (!lease_expired(lease))
if (!lease_expired(lease))
lease_time_align = lease->expires - time(0);
packet.yiaddr = lease->yiaddr;
/* Or the client has a requested ip */
} else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) &&
@ -127,44 +138,53 @@ int sendOffer(struct dhcpMessage *oldpacket)
/* and the ip is in the lease range */
ntohl(req_align) >= ntohl(server_config.start) &&
ntohl(req_align) <= ntohl(server_config.end) &&
/* and its not already taken/offered */
((!(lease = find_lease_by_yiaddr(req_align)) ||
/* or its taken, but expired */
lease_expired(lease)))) {
packet.yiaddr = req_align;
/* otherwise, find a free IP */
!static_lease_ip && /* Check that its not a static lease */
/* and is not already taken/offered */
((!(lease = find_lease_by_yiaddr(req_align)) ||
/* or its taken, but expired */ /* ADDME: or maybe in here */
lease_expired(lease)))) {
packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */
/* otherwise, find a free IP */
} else {
/* Is it a static lease? (No, because find_address skips static lease) */
packet.yiaddr = find_address(0);
/* try for an expired lease */
if (!packet.yiaddr) packet.yiaddr = find_address(1);
}
if(!packet.yiaddr) {
LOG(LOG_WARNING, "no IP addresses to give -- OFFER abandoned");
return -1;
}
if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) {
LOG(LOG_WARNING, "lease pool is full -- OFFER abandoned");
return -1;
}
}
if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {
memcpy(&lease_time_align, lease_time, 4);
lease_time_align = ntohl(lease_time_align);
if (lease_time_align > server_config.lease)
if (lease_time_align > server_config.lease)
lease_time_align = server_config.lease;
}
/* Make sure we aren't just using the lease time from the previous offer */
if (lease_time_align < server_config.min_lease)
if (lease_time_align < server_config.min_lease)
lease_time_align = server_config.lease;
add_simple_option(packet.options, DHCP_LEASE_TIME, lease_time_align);
}
/* ADDME: end of short circuit */
else
{
/* It is a static lease... use it */
packet.yiaddr = static_lease_ip;
}
add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
curr = server_config.options;
while (curr) {
@ -172,7 +192,9 @@ int sendOffer(struct dhcpMessage *oldpacket)
add_option_string(packet.options, curr->data);
curr = curr->next;
}
add_bootp_options(&packet);
addr.s_addr = packet.yiaddr;
LOG(LOG_INFO, "sending OFFER of %s", inet_ntoa(addr));
return send_packet(&packet, 0);
@ -184,45 +206,47 @@ int sendNAK(struct dhcpMessage *oldpacket)
struct dhcpMessage packet;
init_packet(&packet, oldpacket, DHCPNAK);
DEBUG(LOG_INFO, "sending NAK");
return send_packet(&packet, 1);
}
int sendACK(struct dhcpMessage *oldpacket, u_int32_t yiaddr)
int sendACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)
{
struct dhcpMessage packet;
struct option_set *curr;
char *lease_time;
u_int32_t lease_time_align = server_config.lease;
uint8_t *lease_time;
uint32_t lease_time_align = server_config.lease;
struct in_addr addr;
init_packet(&packet, oldpacket, DHCPACK);
packet.yiaddr = yiaddr;
if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {
memcpy(&lease_time_align, lease_time, 4);
lease_time_align = ntohl(lease_time_align);
if (lease_time_align > server_config.lease)
if (lease_time_align > server_config.lease)
lease_time_align = server_config.lease;
else if (lease_time_align < server_config.min_lease)
else if (lease_time_align < server_config.min_lease)
lease_time_align = server_config.lease;
}
add_simple_option(packet.options, DHCP_LEASE_TIME, lease_time_align);
add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
curr = server_config.options;
while (curr) {
if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
add_option_string(packet.options, curr->data);
curr = curr->next;
}
add_bootp_options(&packet);
addr.s_addr = packet.yiaddr;
LOG(LOG_INFO, "sending ACK to %s", inet_ntoa(addr));
if (send_packet(&packet, 0) < 0)
if (send_packet(&packet, 0) < 0)
return -1;
add_lease(packet.chaddr, packet.yiaddr, lease_time_align);
@ -237,7 +261,7 @@ int send_inform(struct dhcpMessage *oldpacket)
struct option_set *curr;
init_packet(&packet, oldpacket, DHCPACK);
curr = server_config.options;
while (curr) {
if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
@ -245,8 +269,7 @@ int send_inform(struct dhcpMessage *oldpacket)
curr = curr->next;
}
add_bootp_options(&packet);
return send_packet(&packet, 0);
}

View File

@ -1,10 +1,11 @@
#ifndef _SERVERPACKET_H
#define _SERVERPACKET_H
#include "packet.h"
int sendOffer(struct dhcpMessage *oldpacket);
int sendNAK(struct dhcpMessage *oldpacket);
int sendACK(struct dhcpMessage *oldpacket, u_int32_t yiaddr);
int sendACK(struct dhcpMessage *oldpacket, uint32_t yiaddr);
int send_inform(struct dhcpMessage *oldpacket);

84
signalpipe.c Normal file
View File

@ -0,0 +1,84 @@
/* signalpipe.c
*
* Signal pipe infrastructure. A reliable way of delivering signals.
*
* Russ Dill <Russ.Dill@asu.edu> December 2003
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include "signalpipe.h"
#include "common.h"
static int signal_pipe[2];
static void signal_handler(int sig)
{
if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0)
DEBUG(LOG_ERR, "Could not send signal: %m");
}
/* Call this before doing anything else. Sets up the socket pair
* and installs the signal handler */
void udhcp_sp_setup(void)
{
socketpair(AF_UNIX, SOCK_STREAM, 0, signal_pipe);
fcntl(signal_pipe[0], F_SETFD, FD_CLOEXEC);
fcntl(signal_pipe[1], F_SETFD, FD_CLOEXEC);
signal(SIGUSR1, signal_handler);
signal(SIGUSR2, signal_handler);
signal(SIGTERM, signal_handler);
}
/* Quick little function to setup the rfds. Will return the
* max_fd for use with select. Limited in that you can only pass
* one extra fd */
int udhcp_sp_fd_set(fd_set *rfds, int extra_fd)
{
FD_ZERO(rfds);
FD_SET(signal_pipe[0], rfds);
if (extra_fd >= 0) {
fcntl(extra_fd, F_SETFD, FD_CLOEXEC);
FD_SET(extra_fd, rfds);
}
return signal_pipe[0] > extra_fd ? signal_pipe[0] : extra_fd;
}
/* Read a signal from the signal pipe. Returns 0 if there is
* no signal, -1 on error (and sets errno appropriately), and
* your signal on success */
int udhcp_sp_read(fd_set *rfds)
{
int sig;
if (!FD_ISSET(signal_pipe[0], rfds))
return 0;
if (read(signal_pipe[0], &sig, sizeof(sig)) < 0)
return -1;
return sig;
}

22
signalpipe.h Normal file
View File

@ -0,0 +1,22 @@
/* signalpipe.h
*
* Russ Dill <Russ.Dill@asu.edu> December 2003
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
void udhcp_sp_setup(void);
int udhcp_sp_fd_set(fd_set *rfds, int extra_fd);
int udhcp_sp_read(fd_set *rfds);

View File

@ -1,7 +1,7 @@
/*
* socket.c -- DHCP server client/server socket creation
*
* Moreton Bay DHCP Server
* udhcp client/server
* Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
* Chris Trew <ctrew@moreton.com.au>
*
@ -24,13 +24,15 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <errno.h>
#include <features.h>
#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
#if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION
#include <netpacket/packet.h>
#include <net/ethernet.h>
#else
@ -39,21 +41,71 @@
#include <linux/if_ether.h>
#endif
#include "common.h"
#include "socket.h"
#include "debug.h"
int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp)
{
int fd;
struct ifreq ifr;
struct sockaddr_in *our_ip;
int listen_socket(unsigned int ip, int port, char *inf)
memset(&ifr, 0, sizeof(struct ifreq));
if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) {
ifr.ifr_addr.sa_family = AF_INET;
strcpy(ifr.ifr_name, interface);
if (addr) {
if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
*addr = our_ip->sin_addr.s_addr;
DEBUG(LOG_INFO, "%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr));
} else {
LOG(LOG_ERR, "SIOCGIFADDR failed, is the interface up and configured?: %m");
close(fd);
return -1;
}
}
if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {
DEBUG(LOG_INFO, "adapter index %d", ifr.ifr_ifindex);
*ifindex = ifr.ifr_ifindex;
} else {
LOG(LOG_ERR, "SIOCGIFINDEX failed!: %m");
close(fd);
return -1;
}
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);
DEBUG(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
} else {
LOG(LOG_ERR, "SIOCGIFHWADDR failed!: %m");
close(fd);
return -1;
}
} else {
LOG(LOG_ERR, "socket failed!: %m");
return -1;
}
close(fd);
return 0;
}
int listen_socket(uint32_t ip, int port, char *inf)
{
struct ifreq interface;
int fd;
struct sockaddr_in addr;
int n = 1;
DEBUG(LOG_INFO, "Opening listen socket on 0x%08x:%d %s", ip, port, inf);
if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
DEBUG(LOG_ERR, "socket call failed: %s", sys_errlist[errno]);
DEBUG(LOG_ERR, "socket call failed: %m");
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
@ -68,7 +120,7 @@ int listen_socket(unsigned int ip, int port, char *inf)
return -1;
}
strncpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ);
strncpy(interface.ifr_name, inf, IFNAMSIZ);
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) {
close(fd);
return -1;
@ -78,31 +130,6 @@ int listen_socket(unsigned int ip, int port, char *inf)
close(fd);
return -1;
}
return fd;
}
int raw_socket(int ifindex)
{
int fd;
struct sockaddr_ll sock;
if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
DEBUG(LOG_ERR, "socket call failed: %s", sys_errlist[errno]);
return -1;
}
sock.sll_family = AF_PACKET;
sock.sll_protocol = htons(ETH_P_IP);
sock.sll_ifindex = ifindex;
if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
DEBUG(LOG_ERR, "bind call failed: %s", sys_errlist[errno]);
close(fd);
return -1;
}
return fd;
}

View File

@ -2,7 +2,7 @@
#ifndef _SOCKET_H
#define _SOCKET_H
int listen_socket(unsigned int ip, int port, char *inf);
int raw_socket(int ifindex);
int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp);
int listen_socket(uint32_t ip, int port, char *inf);
#endif

119
static_leases.c Normal file
View File

@ -0,0 +1,119 @@
/*
* static_leases.c -- Couple of functions to assist with storing and
* retrieving data for static leases
*
* Wade Berrier <wberrier@myrealbox.com> September 2004
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "static_leases.h"
#include "dhcpd.h"
/* Takes the address of the pointer to the static_leases linked list,
* Address to a 6 byte mac address
* Address to a 4 byte ip address */
int addStaticLease(struct static_lease **lease_struct, uint8_t *mac, uint32_t *ip)
{
struct static_lease *cur;
struct static_lease *new_static_lease;
/* Build new node */
new_static_lease = xmalloc(sizeof(struct static_lease));
new_static_lease->mac = mac;
new_static_lease->ip = ip;
new_static_lease->next = NULL;
/* If it's the first node to be added... */
if(*lease_struct == NULL)
{
*lease_struct = new_static_lease;
}
else
{
cur = *lease_struct;
while(cur->next != NULL)
{
cur = cur->next;
}
cur->next = new_static_lease;
}
return 1;
}
/* Check to see if a mac has an associated static lease */
uint32_t getIpByMac(struct static_lease *lease_struct, void *arg)
{
uint32_t return_ip;
struct static_lease *cur = lease_struct;
uint8_t *mac = arg;
return_ip = 0;
while(cur != NULL)
{
/* If the client has the correct mac */
if(memcmp(cur->mac, mac, 6) == 0)
{
return_ip = *(cur->ip);
}
cur = cur->next;
}
return return_ip;
}
/* Check to see if an ip is reserved as a static ip */
uint32_t reservedIp(struct static_lease *lease_struct, uint32_t ip)
{
struct static_lease *cur = lease_struct;
uint32_t return_val = 0;
while(cur != NULL)
{
/* If the client has the correct ip */
if(*cur->ip == ip)
return_val = 1;
cur = cur->next;
}
return return_val;
}
#ifdef UDHCP_DEBUG
/* Print out static leases just to check what's going on */
/* Takes the address of the pointer to the static_leases linked list */
void printStaticLeases(struct static_lease **arg)
{
/* Get a pointer to the linked list */
struct static_lease *cur = *arg;
while(cur != NULL)
{
/* printf("PrintStaticLeases: Lease mac Address: %x\n", cur->mac); */
printf("PrintStaticLeases: Lease mac Value: %x\n", *(cur->mac));
/* printf("PrintStaticLeases: Lease ip Address: %x\n", cur->ip); */
printf("PrintStaticLeases: Lease ip Value: %x\n", *(cur->ip));
cur = cur->next;
}
}
#endif

25
static_leases.h Normal file
View File

@ -0,0 +1,25 @@
/* static_leases.h */
#ifndef _STATIC_LEASES_H
#define _STATIC_LEASES_H
#include "dhcpd.h"
/* Config file will pass static lease info to this function which will add it
* to a data structure that can be searched later */
int addStaticLease(struct static_lease **lease_struct, uint8_t *mac, uint32_t *ip);
/* Check to see if a mac has an associated static lease */
uint32_t getIpByMac(struct static_lease *lease_struct, void *arg);
/* Check to see if an ip is reserved as a static ip */
uint32_t reservedIp(struct static_lease *lease_struct, uint32_t ip);
#ifdef UDHCP_DEBUG
/* Print out static leases just to check what's going on */
void printStaticLeases(struct static_lease **lease_struct);
#endif
#endif

208
udhcpc.8 Normal file
View File

@ -0,0 +1,208 @@
.TH UDHCPC 8 2001-09-26 GNU/Linux "GNU/Linux Administrator's Manual"
.SH NAME
udhcpc \- very small DHCP client
.SH SYNOPSIS
.B udhcpc
.RI [ OPTION ]...
.SH DESCRIPTION
The udhcp client negotiates a lease with the DHCP server and
executes a script when it is obtained or lost.
.SH OPTIONS
.TP
.BI \-c\ CLIENTID ,\ \-\-clientid= CLIENTID
Send the client identifier
.IR CLIENTID .
.TP
.BR -f ,\ \-\-foreground
Do not fork after obtaining a lease.
.TP
.BI \-H\ HOSTNAME ,\ \-\-hostname= HOSTNAME
Send the client hostname
.IR HOSTNAME .
.TP
.BI \-h\ HOSTNAME
Alias for -H
.IR HOSTNAME .
.TP
.BI \-i\ INTERFACE ,\ \-\-interface= INTERFACE
Configure
.IR INTERFACE .
.TP
.BR -n ,\ \-\-now
Exit with failure if a lease cannot be obtained.
.TP
.BI \-p\ FILE ,\ \-\-pidfile= FILE
Write the process ID of the daemon to
.IR FILE .
.TP
.BR -q ,\ \-\-quit
Exit after obtaining a lease.
.TP
.BI \-r\ ADDRESS ,\ \-\-request= ADDRESS
Request IP address
.IR ADDRESS .
.TP
.BI \-s\ FILE ,\ \-\-script= FILE
Use script
.IR FILE .
.TP
.BR -v ,\ \-\-version
Display version.
.SH USAGE
When an event occurs,
.B udhcpc
executes a script. There are four possible arguments to this
script:
.TP
.B deconfig
.B deconfig
is used when
.B udhcpc
starts, and when a lease is lost. The script should put the
interface in an up, but deconfigured, state.
.TP
.B bound
.B bound
is used when
.B udhcpc
moves from an unbound to a bound state. The script should
configure the interface and set any other relevant parameters
(e.g., default gateway, dns server, etc.).
.TP
.B renew
.B renew
is used when
.B udhcpc
when a lease is renewed. The interface is already
configured, so the IP address will not change. Other parameters
(e.g., default gateway, subnet mask, dns server) may.
.TP
.B nak
.B nak
is used when
.B udhcpc
receieves a NAK packet from the server. The
enviromental variable
.B $message
will contain the reason for the
NAK message if the server included one. Processing this message
is optional, as the script will also be called with deconfig if
need be.
.PP
Parameters are passed to the script via the following environment
variables:
.TP
.B HOME
The inherited HOME, or "/" if it is unset.
.TP
.B PATH
The inherited PATH, or "/bin:/usr/bin:/sbin:/usr/sbin" if it is
unset.
.TP
.B interface
The interface.
.TP
.B ip
The client IP address.
.TP
.B siaddr
The BOOTP next server option.
.TP
.B sname
The BOOTP server name option.
.TP
.B boot_file
The BOOTP boot file option.
.TP
.B subnet
The subnet mask.
.TP
.B timezone
The timezone offset from UTC in seconds.
.TP
.B router
The list of routers.
.TP
.B timesvr
The list of time servers.
.TP
.B namesvr
The list of IEN 116 name servers.
.TP
.B dns
The list of DNS servers.
.TP
.B logsvr
The list of MIT-LCS UDP log servers.
.TP
.B cookiesvr
The list of RFC 865 cookie servers.
.TP
.B lprsvr
The list of LPR servers.
.TP
.B hostname
The host name.
.TP
.B bootsize
The length in 512-octet blocks of the bootfile.
.TP
.B domain
The domain name of the network.
.TP
.B swapsvr
The client's swap server.
.TP
.B rootpath
The path of the client's root dist.
.TP
.B ipttl
The TTL.
.TP
.B mtu
The MTU.
.TP
.B broadcast
The broadcast address.
.TP
.B ntpsrv
The list of NTP servers.
.TP
.B wins
The list of WINS servers.
.TP
.B lease
The lease time in seconds.
.TP
.B dhcptype
The DHCP message type (safely ignored).
.TP
.B serverid
The server IP address.
.TP
.B message
Reason for a DHCPNAK.
.TP
.B tftp
The TFTP server name.
.TP
.B bootfile
The bootfile name.
.SH FILES
.TP
.I /usr/share/udhcpc/default.script
Script run when leases are obtained or lost.
.SH NOTES
.B udhcpc
responds to the following signals:
.TP
.B SIGUSR1
This signal causes
.B udhcpc
to renew the current lease or, if it does not have one, obtain a
new lease.
.TP
.B SIGUSR2
This signal caused
.B udhcpc
to release the current lease.

17
udhcpd.8 Normal file
View File

@ -0,0 +1,17 @@
.TH UDHCPD 8 2001-09-27 GNU/Linux "GNU/Linux Administrator's Manual"
.SH NAME
udhcpd \- very small DHCP server
.SH SYNOPSIS
.B udhcpd
.SH DESCRIPTION
The udhcp server negotiates leases with DHCP clients.
.SH FILES
.TP
.I /etc/udhcpd.conf
Configuration file.
.TP
.I /var/lib/misc/udhcpd.leases
Lease information file.
.SH SEE ALSO
.BR dumpleases (1),
.BR udhcpd.conf (8).

164
udhcpd.conf.5 Normal file
View File

@ -0,0 +1,164 @@
.TH UDHCPD.CONF 5 2001-09-26 GNU/Linux "GNU/Linux Administrator's Manual"
.SH NAME
udhcpd.conf \- udhcp server configuration file
.SH DESCRIPTION
The file
.I /etc/udhcpd.conf
contains configuration information specific to the udhcp server.
It should contain one configuration keyword per line, followed by
appropriate configuration information.
.SH OPTIONS
.TP
.BI start\ ADDRESS
The starting address of the IP lease block is
.IR ADDRESS .
The default is
.BR 192.168.0.20 .
.TP
.BI end\ ADDRESS
The ending address of the IP lease block is
.IR ADDRESS .
The default is
.BR 192.168.0.254 .
.TP
.BI interface\ INTERFACE
The udhcp server should listen on
.IR INTERFACE .
The default is
.BR eth0 .
.TP
.BI max_leases\ LEASES
Offer at most
.I LEASES
leases (including those reserved by OFFERs, DECLINEs, and ARP
conflicts). The default is
.BR 254 .
.TP
.BI remaining\ REMAINING
If
.I REMAINING
is
.BR yes ,
store the time remaining for each lease. If it is
.BR no ,
store the expiration time for each lease. The default is
.BR yes .
.TP
.BI auto_time\ SECONDS
Write the lease information to a file every
.I SECONDS
seconds. The default is
.BR 7200 .
.TP
.BI decline_time\ SECONDS
Reserve an IP for
.I SECONDS
seconds if a DHCP decline message is received. The default is
.BR 3600 .
.TP
.BI conflict_time\ SECONDS
Reserve an IP for
.I SECONDS
seconds if an ARP conflict occurs. The default is
.BR 3600 .
.TP
.BI offer_time\ SECONDS
Reserve an IP for
.I SECONDS
seconds if it is offered. The default is
.BR 60 .
.TP
.BI min_lease\ SECONDS
Reserve an IP for the full lease time if the lease to be given is less than
.I SECONDS
seconds. The default is
.BR 60 .
.TP
.BI lease_file\ FILE
Write the lease information to
.IR FILE .
The default is
.BR /var/lib/misc/udhcpd.leases .
.TP
.BI pidfile\ FILE
Write the process ID to
.IR FILE .
The default is
.BR /var/run/udhcpd.pid .
.TP
.BI notify_file\ FILE
Execute
.I FILE
after the lease information is written. By default, no file is executed.
.TP
.BI siaddr\ ADDRESS
BOOTP specific option. The default is
.BR 0.0.0.0 .
.TP
.BI sname\ NAME
BOOTP specific option. There is no default.
.TP
.BI boot_file\ FILE
BOOTP specific option. There is no default.
.TP
.BI option\ OPTION
DHCP specific option.
.RS
.TP
.BI subnet\ ADDRESS
.TP
.BI timezone\ OFFSET
.TP
.BI router\ ADDRESS...
.TP
.BI timesvr\ ADDRESS...
.TP
.BI namesvr\ ADDRESS...
.TP
.BI dns\ ADDRESS...
.TP
.BI logsvr\ ADDRESS...
.TP
.BI cookiesvr\ ADDRESS...
.TP
.BI lprsvr\ ADDRESS...
.TP
.BI hostname\ HOSTNAME
.TP
.BI bootsize\ SIZE
.TP
.BI domain\ DOMAIN
.TP
.BI swapsvr\ ADDRESS
.TP
.BI rootpath\ PATH
.TP
.BI ipttl\ TTL
.TP
.BI mtu\ MTU
.TP
.BI broadcast\ ADDRESS
.TP
.BI ntpsrv\ ADDRESS...
.TP
.BI wins\ ADDRESS...
.TP
.BI requestip\ ADDRESS
.TP
.BI lease\ SECONDS
.TP
.BI dhcptype\ TYPE
.TP
.BI serverid\ ADDRESS
.TP
.BI tftp\ FILE
.TP
.BI bootfile\ FILE
The default for
.B lease
is
.BR 864000 .
There are no defaults for the other options.
.RE
.SH SEE ALSO
.BR udhcpd (8).

6
version.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _UDHCP_VERSION_H
#define _UDHCP_VERSION_H
#define VERSION "0.9.9-pre"
#endif