DHCP client setup for FreeBSD

(This document was borrowed from http://home.san.rr.com/freebsd/dhcp.html)
    Currently there is no streamlined way of setting up a DHCP client for FreeBSD. I have cobbled together the following procedure out of a lot of trial and error and I would like to save you the same aggravation. I hope that this text is of use to you. My experience is colored heavily by the fact that I am using the Road Runner cable modem service here in San Diego. If you have information that is pertinent to other uses of dhcp I would be interested in hearing it.

    If you need more assistance with the ISC DHCP client or server you should probably visit http://www.fugue.com/dhcp/lists and subscribe to one or more of the support mailing lists and post your question there. They have a lot of knowledgeable people there who are willing to help.

    If you have any questions comments or suggestions please send them to me by mailing Studded@dal.net.
 
Last modified 30 March 1998

1.  Make sure that you have BPF in your kernel

    The DHCP client depends on the existence of the Berkely Packet Filter (BPF) in your kernel. If you are using the GENERIC kernel that was originally installed with FreeBSD you do not have BPF. First, visit the kernel configuration page for the basic information on how to compile a custom kernel. While you are working on the configuration file make sure that you add the option for BPF:

pseudo-device    bpfilter    4

    After you have recompiled the kernel and rebooted  you will probably need to make your devices. As root type:

# cd /dev
# /bin/sh MAKEDEV bpf0 bpf1 bpf2 bpf3

2.  Download and install the source

    By far the easiest way to do this is to use the FreeBSD ports collection. This is a good general solution for adding software to any FreeBSD installation. The specific port you want is /usr/ports/net/isc-dhcp2. If you have the ports collection installed, click here to be taken straight to it. My limited experience with the Wide client in the ports was not good and I have found the person working on the ISC project to be very friendly and very much interested in cross-platform compatibility.

    If you do not wish to use the ports collection you can try installing it as a package instead. You could of course download the source from the ISC and compile it yourself, but if you are going to do that you might as well use the port. :)

3.  Configure the client

    Ok, now that you have the thing installed, you must set up the configuration file. You should find it in /etc, but if not you can copy the one that comes with the sources to /etc.

# cp /usr/ports/net/isc-dhcp2/work/dhcp-2.0b1pl0/client/dhclient.conf /etc

    A lot of the things in the conf file are examples and should be deleted. The only entries I have in mine are:

# Defaults
timeout 60;
reboot 10;

retry 60;
select-timeout 5;
initial-interval 2;

script "/etc/dhclient-script";

interface "ed0" {
 request subnet-mask, broadcast-address, routers, domain-name-servers,
         domain-name, host-name;
 require domain-name-servers;
 media "link2";
}

    Your configuration should be determined by your local needs of course. Your ISP should be able to give you the information you need (unless of course they are like Road Runner and prefer not to acknowledge that Unix exists :). The options I have in the request statement are pretty much normal and required. The man page for dhclient.conf also suggests that you can start with an empty conf file and in most normal server configurations it should come up by itself. Then you can check the /var/db/dhclient.leases file to see what information the server sends you.

    In general you should not need to make any modifications to the script, it will work out of the box. One option I need that is not currently supported by the dhclient-script is setting the hostname. I have a patch for the script that will set the hostname if you request it from the server. I also added an option to run a local script that in my case logs in to the Road Runner system when it obtains a new lease. I did not want to put this info in the client script itself for various reasons. If you have similar needs you can set up your own dhclient-script.local in /etc. If you do not, you can ignore that part of the patch (or delete it) since it will not do anything if there is no such script to run. :)

    The format of my dhclient-script.local file is as such. I use it to start Phil Karn's excellent rrlogin program that is needed by us Road Runner users. (Thanks Phil. :) Those who have an interest in Road Runner or cable modem service in general would be well served at his page. I do not like to keep rrlogin running, so I kill it off once I am logged in. It is enough to bring it up as needed when I boot or get a new lease.

    Once you have got the files situated the way you want them, fire it up and make sure it is working. As root I simply type

# dhclient ed0

which fires it up for my 3Com 3c509 card.

4.  Configuring FreeBSD To Use DHCP

    Now that dhclient is working, you want to set FreeBSD's configuration files up to use it during boot. There is no facility to do this directly, however you can use the hooks in the rc.network script to start it automatically and thereby use the network configuration options in rc.conf as they were intended. In /etc/rc.conf make the following changes. You should comment out the options as indicated so that dhcp can set them for you.

# If dhcp will be setting your hostname for you, comment this out.
# If you have a static hostname set it here as normal.
#hostname="my.domain.name" # Set this!
. . .
# Make sure you define your network interfaces even if they will
# be configured by dhcp (so that the start script catches them).
network_interfaces="ed0 lo0"    # List of network interfaces (lo0 is loopback).
ifconfig_lo0="inet 127.0.0.1"   # default loopback device configuration.
# But do not let rc.network configure it.
#ifconfig_ed0="inet 204.210.32.25  netmask 255.255.255.0"
. . .
# This is almost certainly going to be something you get from dhcp.
defaultrouter="NO"              # Set to default gateway (or NO).

    In /etc/rc.network you will see the following (no changes need to be made).

# Set up all the network interfaces, calling startup scripts if needed
    for ifn in ${network_interfaces}; do
            if [ -e /etc/start_if.${ifn} ]; then
                    . /etc/start_if.${ifn}
            fi
. . .

    You can take advantage of this by creating a script that starts dhcp. The script should have a suffix named after the interface you want to key it to. For instance mine is called /etc/start_if.ed0. Since all I need to do is start dhcp, it is very simple.

#!/bin/sh

/usr/local/sbin/dhclient ed0

5.  Reboot And Give It A Go

    At this point everything should be in place, you can reboot your system and it will use dhcp to configure your interface. If it does not work for some reason, double-check the configuration files and try running dhclient by itself and make sure that part of it is working. Also make sure that you do not have any conflicting instructions in /etc/rc.local or /usr/local/etc/rc.d.
 

Good luck! :)