Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add a simple dhcp client for rump kernels. It uses code fro...



details:   https://anonhg.NetBSD.org/src/rev/892607c83638
branches:  trunk
changeset: 761210:892607c83638
user:      pooka <pooka%NetBSD.org@localhost>
date:      Thu Jan 20 18:47:19 2011 +0000

description:
Add a simple dhcp client for rump kernels.  It uses code from dhcpcd
for talking to the server and for setting the interface address
and route.  However, otherwise it is quite different, since we need
to be working under the assumptions that there is no stable storage
on a rump instance, and that there are n networking stacks on a
given host.

diffstat:

 distrib/sets/lists/base/mi                |    3 +-
 distrib/sets/lists/comp/mi                |    3 +-
 distrib/sets/lists/man/mi                 |    5 +-
 usr.bin/Makefile                          |    5 +-
 usr.bin/rump_dhcpclient/Makefile          |    9 +
 usr.bin/rump_dhcpclient/bpf-filter.h      |  101 +++
 usr.bin/rump_dhcpclient/bpf.c             |  205 +++++++
 usr.bin/rump_dhcpclient/common.c          |  195 +++++++
 usr.bin/rump_dhcpclient/common.h          |   76 ++
 usr.bin/rump_dhcpclient/configure.c       |  467 ++++++++++++++++++
 usr.bin/rump_dhcpclient/configure.h       |   38 +
 usr.bin/rump_dhcpclient/dhcp.c            |  773 ++++++++++++++++++++++++++++++
 usr.bin/rump_dhcpclient/dhcp.h            |  201 +++++++
 usr.bin/rump_dhcpclient/dhcpcd.h          |  122 ++++
 usr.bin/rump_dhcpclient/if-bsd.c          |  271 ++++++++++
 usr.bin/rump_dhcpclient/if-options.h      |  123 ++++
 usr.bin/rump_dhcpclient/main.c            |  272 ++++++++++
 usr.bin/rump_dhcpclient/net.c             |  503 +++++++++++++++++++
 usr.bin/rump_dhcpclient/net.h             |  156 ++++++
 usr.bin/rump_dhcpclient/rump.dhcpclient.1 |   68 ++
 20 files changed, 3591 insertions(+), 5 deletions(-)

diffs (truncated from 3730 to 300 lines):

diff -r 07c29bb1707b -r 892607c83638 distrib/sets/lists/base/mi
--- a/distrib/sets/lists/base/mi        Thu Jan 20 18:43:52 2011 +0000
+++ b/distrib/sets/lists/base/mi        Thu Jan 20 18:47:19 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.915 2011/01/13 22:28:36 haad Exp $
+# $NetBSD: mi,v 1.916 2011/01/20 18:47:20 pooka Exp $
 #
 # Note:        Don't delete entries from here - mark them as "obsolete" instead,
 #      unless otherwise stated below.
@@ -600,6 +600,7 @@
 ./usr/bin/rs                                   base-util-bin
 ./usr/bin/rsh                                  base-netutil-bin
 ./usr/bin/rump_allserver                       base-util-bin
+./usr/bin/rump.dhcpclient                      base-util-bin
 ./usr/bin/rump_server                  base-util-bin
 ./usr/bin/rump.halt                    base-util-bin
 ./usr/bin/rup                                  base-netutil-bin
diff -r 07c29bb1707b -r 892607c83638 distrib/sets/lists/comp/mi
--- a/distrib/sets/lists/comp/mi        Thu Jan 20 18:43:52 2011 +0000
+++ b/distrib/sets/lists/comp/mi        Thu Jan 20 18:47:19 2011 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: mi,v 1.1574 2011/01/19 21:07:14 he Exp $
+#      $NetBSD: mi,v 1.1575 2011/01/20 18:47:21 pooka Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -3345,6 +3345,7 @@
 ./usr/libdata/debug/usr/bin/rs.debug           comp-util-debug         debug
 ./usr/libdata/debug/usr/bin/rsh.debug          comp-netutil-debug      debug
 ./usr/libdata/debug/usr/bin/rump_allserver.debug               comp-util-debug         debug
+./usr/libdata/debug/usr/bin/rump.dhcpclient.debug              comp-util-debug         debug
 ./usr/libdata/debug/usr/bin/rump_server.debug          comp-util-debug         debug
 ./usr/libdata/debug/usr/bin/rump.halt.debug            comp-util-debug         debug
 ./usr/libdata/debug/usr/bin/rup.debug          comp-netutil-debug      debug
diff -r 07c29bb1707b -r 892607c83638 distrib/sets/lists/man/mi
--- a/distrib/sets/lists/man/mi Thu Jan 20 18:43:52 2011 +0000
+++ b/distrib/sets/lists/man/mi Thu Jan 20 18:47:19 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1278 2011/01/17 16:08:19 jmcneill Exp $
+# $NetBSD: mi,v 1.1279 2011/01/20 18:47:22 pooka Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -445,6 +445,7 @@
 ./usr/share/man/cat1/rsh.0                     man-netutil-catman      .cat
 ./usr/share/man/cat1/rtld.0                    man-shlib-catman        .cat
 ./usr/share/man/cat1/rump.halt.0                       man-netutil-catman      .cat
+./usr/share/man/cat1/rump.dhcpclient.0                 man-netutil-catman      .cat
 ./usr/share/man/cat1/rump_allserver.0                  man-netutil-catman      .cat
 ./usr/share/man/cat1/rump_server.0                     man-netutil-catman      .cat
 ./usr/share/man/cat1/rup.0                     man-netutil-catman      .cat
@@ -3243,6 +3244,7 @@
 ./usr/share/man/html1/rsh.html                 man-netutil-htmlman     html
 ./usr/share/man/html1/rtld.html                        man-shlib-htmlman       html
 ./usr/share/man/html1/rump.halt.html                   man-netutil-htmlman     html
+./usr/share/man/html1/rump.dhcpclient.html                     man-netutil-htmlman     html
 ./usr/share/man/html1/rump_allserver.html                      man-netutil-htmlman     html
 ./usr/share/man/html1/rump_server.html                 man-netutil-htmlman     html
 ./usr/share/man/html1/rup.html                 man-netutil-htmlman     html
@@ -5699,6 +5701,7 @@
 ./usr/share/man/man1/rsh.1                     man-netutil-man         .man
 ./usr/share/man/man1/rtld.1                    man-shlib-man           .man
 ./usr/share/man/man1/rump.halt.1                       man-netutil-man         .man
+./usr/share/man/man1/rump.dhcpclient.1                 man-netutil-man         .man
 ./usr/share/man/man1/rump_allserver.1                  man-netutil-man         .man
 ./usr/share/man/man1/rump_server.1                     man-netutil-man         .man
 ./usr/share/man/man1/rup.1                     man-netutil-man         .man
diff -r 07c29bb1707b -r 892607c83638 usr.bin/Makefile
--- a/usr.bin/Makefile  Thu Jan 20 18:43:52 2011 +0000
+++ b/usr.bin/Makefile  Thu Jan 20 18:47:19 2011 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.198 2011/01/12 16:14:24 pooka Exp $
+#      $NetBSD: Makefile,v 1.199 2011/01/20 18:47:19 pooka Exp $
 #      from: @(#)Makefile      8.3 (Berkeley) 1/7/94
 
 .include <bsd.own.mk>
@@ -22,7 +22,8 @@
        pagesize passwd paste patch pathchk pkill pmap pmc pr \
        printenv printf progress pwhash qsubst quota radioctl rdist \
        renice rev revoke rfcomm_sppd rlogin rpcgen rpcinfo rs rsh \
-       rump_allserver rump_halt rump_server rup ruptime rusers rwall rwho \
+       rump_allserver rump_dhcpclient rump_halt rump_server rup ruptime \
+       rusers rwall rwho \
        script sdiff sdpquery sed seq shar shlock shmif_dumpbus \
        showmount shuffle sockstat soelim sort spell split stat su systat \
        tabs tail talk tcopy tee telnet tftp tic time tip touch tput \
diff -r 07c29bb1707b -r 892607c83638 usr.bin/rump_dhcpclient/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/rump_dhcpclient/Makefile  Thu Jan 20 18:47:19 2011 +0000
@@ -0,0 +1,9 @@
+#      $NetBSD: Makefile,v 1.1 2011/01/20 18:47:19 pooka Exp $
+#
+
+PROG=  rump.dhcpclient
+SRCS=  main.c bpf.c common.c configure.c dhcp.c if-bsd.c net.c
+
+LDADD+=        -lrumpclient
+
+.include <bsd.prog.mk>
diff -r 07c29bb1707b -r 892607c83638 usr.bin/rump_dhcpclient/bpf-filter.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/rump_dhcpclient/bpf-filter.h      Thu Jan 20 18:47:19 2011 +0000
@@ -0,0 +1,101 @@
+/*
+ * dhcpcd - DHCP client daemon
+ * Copyright (c) 2006-2008 Roy Marples <roy%marples.name@localhost>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef BPF_ETHCOOK
+# define BPF_ETHCOOK 0
+#endif
+#ifndef BPF_WHOLEPACKET
+# define BPF_WHOLEPACKET ~0U
+#endif
+static const struct bpf_insn const arp_bpf_filter [] = {
+#ifndef BPF_SKIPTYPE
+       /* Make sure this is an ARP packet... */
+       BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
+       BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_ARP, 0, 3),
+#endif
+       /* Make sure this is an ARP REQUEST... */
+       BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20 + BPF_ETHCOOK),
+       BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REQUEST, 2, 0),
+       /* or ARP REPLY... */
+       BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20 + BPF_ETHCOOK),
+       BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REPLY, 0, 1),
+       /* If we passed all the tests, ask for the whole packet. */
+       BPF_STMT(BPF_RET + BPF_K, BPF_WHOLEPACKET),
+       /* Otherwise, drop it. */
+       BPF_STMT(BPF_RET + BPF_K, 0),
+};
+static const size_t arp_bpf_filter_len =
+    sizeof(arp_bpf_filter) / sizeof(arp_bpf_filter[0]);
+
+
+/* dhcp_bpf_filter taken from bpf.c in dhcp-3.1.0
+ *
+ * Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1996-2003 by Internet Software Consortium
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ *   Internet Systems Consortium, Inc.
+ *   950 Charter Street
+ *   Redwood City, CA 94063
+ *   <info%isc.org@localhost>
+ *   http://www.isc.org/
+ */
+
+static const struct bpf_insn const dhcp_bpf_filter [] = {
+#ifndef BPF_SKIPTYPE
+       /* Make sure this is an IP packet... */
+       BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
+       BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
+#endif
+       /* Make sure it's a UDP packet... */
+       BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23 + BPF_ETHCOOK),
+       BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
+       /* Make sure this isn't a fragment... */
+       BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20 + BPF_ETHCOOK),
+       BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
+       /* Get the IP header length... */
+       BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14 + BPF_ETHCOOK),
+       /* Make sure it's to the right port... */
+       BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16 + BPF_ETHCOOK),
+       BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DHCP_CLIENT_PORT, 0, 1),
+       /* If we passed all the tests, ask for the whole packet. */
+       BPF_STMT(BPF_RET + BPF_K, BPF_WHOLEPACKET),
+       /* Otherwise, drop it. */
+       BPF_STMT(BPF_RET + BPF_K, 0),
+};
+static const size_t dhcp_bpf_filter_len =
+    sizeof(dhcp_bpf_filter) / sizeof(dhcp_bpf_filter[0]);
diff -r 07c29bb1707b -r 892607c83638 usr.bin/rump_dhcpclient/bpf.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/rump_dhcpclient/bpf.c     Thu Jan 20 18:47:19 2011 +0000
@@ -0,0 +1,205 @@
+/*
+ * dhcpcd - DHCP client daemon
+ * Copyright (c) 2006-2008 Roy Marples <roy%marples.name@localhost>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/uio.h>
+
+#include <net/bpf.h>
+#include <net/if.h>
+#include <arpa/inet.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <paths.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "common.h"
+#include "dhcp.h"
+#include "net.h"
+#include "bpf-filter.h"
+
+#include <rump/rump.h>
+#include <rump/rump_syscalls.h>
+
+int
+open_socket(struct interface *iface, int protocol)
+{
+       int fd = -1;
+       int *fdp = NULL;
+       struct ifreq ifr;
+       int buf_len = 0;
+       struct bpf_version pv;
+       struct bpf_program pf;
+#ifdef BIOCIMMEDIATE
+       int flags;
+#endif
+#ifdef _PATH_BPF
+       fd = rump_sys_open(_PATH_BPF, O_RDWR | O_NONBLOCK);
+#else
+       char *device;
+       int n = 0;
+
+       device = xmalloc(sizeof(char) * PATH_MAX);
+       do {
+               snprintf(device, PATH_MAX, "/dev/bpf%d", n++);
+               fd = rump_sys_open(device, O_RDWR | O_NONBLOCK);
+       } while (fd == -1 && errno == EBUSY);
+       free(device);
+#endif
+
+       if (fd == -1)
+               return -1;
+
+       if (rump_sys_ioctl(fd, BIOCVERSION, &pv) == -1)
+               goto eexit;
+       if (pv.bv_major != BPF_MAJOR_VERSION ||
+           pv.bv_minor < BPF_MINOR_VERSION) {
+               fprintf(stderr, "BPF version mismatch - recompile");
+               goto eexit;
+       }
+
+       memset(&ifr, 0, sizeof(ifr));
+       strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
+       if (rump_sys_ioctl(fd, BIOCSETIF, &ifr) == -1)
+               goto eexit;



Home | Main Index | Thread Index | Old Index