2007-01-12 09:20:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* 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 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 <string.h>
|
2007-01-19 01:13:12 +00:00
|
|
|
#include <stdio.h>
|
2007-01-12 09:20:11 +00:00
|
|
|
#include <byteswap.h>
|
|
|
|
#include <gpxe/in.h>
|
2007-01-16 08:10:54 +00:00
|
|
|
#include <gpxe/ip.h>
|
2007-01-12 09:20:11 +00:00
|
|
|
#include <gpxe/dhcp.h>
|
|
|
|
#include <gpxe/async.h>
|
|
|
|
#include <gpxe/netdevice.h>
|
2007-01-18 22:38:13 +00:00
|
|
|
#include <gpxe/dns.h>
|
2007-01-15 08:49:10 +00:00
|
|
|
#include <usr/ifmgmt.h>
|
2007-01-12 09:20:11 +00:00
|
|
|
#include <usr/dhcpmgmt.h>
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* DHCP management
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-01-18 22:38:13 +00:00
|
|
|
/* Avoid dragging in dns.o */
|
2007-06-11 22:36:50 +00:00
|
|
|
struct sockaddr_tcpip nameserver;
|
2007-01-18 22:38:13 +00:00
|
|
|
|
2007-04-09 18:01:43 +00:00
|
|
|
/* Avoid dragging in syslog.o */
|
|
|
|
struct in_addr syslogserver;
|
|
|
|
|
2007-01-12 09:20:11 +00:00
|
|
|
/**
|
|
|
|
* Configure network device via DHCP
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int dhcp ( struct net_device *netdev ) {
|
|
|
|
static struct dhcp_option_block *dhcp_options = NULL;
|
|
|
|
struct dhcp_session dhcp;
|
2007-01-18 22:38:13 +00:00
|
|
|
struct in_addr address = { 0 };
|
|
|
|
struct in_addr netmask = { 0 };
|
2007-01-12 09:20:11 +00:00
|
|
|
struct in_addr gateway = { INADDR_NONE };
|
2007-06-11 22:36:50 +00:00
|
|
|
struct sockaddr_in *sin_nameserver;
|
2007-01-15 08:49:10 +00:00
|
|
|
struct async async;
|
2007-01-12 09:20:11 +00:00
|
|
|
int rc;
|
|
|
|
|
2007-01-12 09:53:28 +00:00
|
|
|
/* Check we can open the interface first */
|
|
|
|
if ( ( rc = ifopen ( netdev ) ) != 0 )
|
|
|
|
return rc;
|
|
|
|
|
2007-01-12 09:20:11 +00:00
|
|
|
/* Free up any previously-acquired options */
|
|
|
|
if ( dhcp_options ) {
|
|
|
|
unregister_dhcp_options ( dhcp_options );
|
|
|
|
free_dhcp_options ( dhcp_options );
|
|
|
|
dhcp_options = NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-16 04:09:13 +00:00
|
|
|
/* Clear any existing routing table entry */
|
|
|
|
del_ipv4_address ( netdev );
|
|
|
|
|
2007-01-12 09:20:11 +00:00
|
|
|
/* Issue DHCP request */
|
|
|
|
printf ( "DHCP (%s %s)...", netdev->name, netdev_hwaddr ( netdev ) );
|
|
|
|
memset ( &dhcp, 0, sizeof ( dhcp ) );
|
|
|
|
dhcp.netdev = netdev;
|
2007-01-16 08:10:54 +00:00
|
|
|
if ( ( rc = async_block ( &async,
|
|
|
|
start_dhcp ( &dhcp, &async ) ) ) != 0 ) {
|
2007-01-15 08:49:10 +00:00
|
|
|
printf ( "failed (%s)\n", strerror ( rc ) );
|
2007-01-12 09:20:11 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
printf ( "done\n" );
|
|
|
|
|
|
|
|
/* Store and register options */
|
|
|
|
dhcp_options = dhcp.options;
|
|
|
|
register_dhcp_options ( dhcp_options );
|
|
|
|
|
|
|
|
/* Retrieve IP address configuration */
|
|
|
|
find_dhcp_ipv4_option ( dhcp_options, DHCP_EB_YIADDR, &address );
|
|
|
|
find_dhcp_ipv4_option ( dhcp_options, DHCP_SUBNET_MASK, &netmask );
|
|
|
|
find_dhcp_ipv4_option ( dhcp_options, DHCP_ROUTERS, &gateway );
|
|
|
|
|
|
|
|
/* Set up new IP address configuration */
|
|
|
|
if ( ( rc = add_ipv4_address ( netdev, address, netmask,
|
|
|
|
gateway ) ) != 0 ) {
|
|
|
|
printf ( "Could not configure %s with DHCP results: %s\n",
|
|
|
|
netdev->name, strerror ( rc ) );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2007-01-18 22:38:13 +00:00
|
|
|
/* Retrieve other DHCP options that we care about */
|
2007-06-11 22:36:50 +00:00
|
|
|
sin_nameserver = ( struct sockaddr_in * ) &nameserver;
|
|
|
|
sin_nameserver->sin_family = AF_INET;
|
2007-01-18 22:38:13 +00:00
|
|
|
find_dhcp_ipv4_option ( dhcp_options, DHCP_DNS_SERVERS,
|
2007-06-11 22:36:50 +00:00
|
|
|
&sin_nameserver->sin_addr );
|
2007-04-09 18:01:43 +00:00
|
|
|
find_dhcp_ipv4_option ( dhcp_options, DHCP_LOG_SERVERS,
|
|
|
|
&syslogserver );
|
2007-01-18 22:38:13 +00:00
|
|
|
|
2007-01-12 09:20:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|