2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <gpxe/dhcp.h>
25 #include <gpxe/async.h>
26 #include <gpxe/netdevice.h>
28 #include <usr/ifmgmt.h>
29 #include <usr/dhcpmgmt.h>
37 /* Avoid dragging in dns.o */
38 struct in_addr nameserver;
41 * Configure network device via DHCP
43 * @v netdev Network device
44 * @ret rc Return status code
46 int dhcp ( struct net_device *netdev ) {
47 static struct dhcp_option_block *dhcp_options = NULL;
48 struct dhcp_session dhcp;
49 struct in_addr address = { 0 };
50 struct in_addr netmask = { 0 };
51 struct in_addr gateway = { INADDR_NONE };
55 /* Check we can open the interface first */
56 if ( ( rc = ifopen ( netdev ) ) != 0 )
59 /* Free up any previously-acquired options */
61 unregister_dhcp_options ( dhcp_options );
62 free_dhcp_options ( dhcp_options );
66 /* Clear any existing routing table entry */
67 del_ipv4_address ( netdev );
69 /* Issue DHCP request */
70 printf ( "DHCP (%s %s)...", netdev->name, netdev_hwaddr ( netdev ) );
71 memset ( &dhcp, 0, sizeof ( dhcp ) );
73 if ( ( rc = async_block ( &async,
74 start_dhcp ( &dhcp, &async ) ) ) != 0 ) {
75 printf ( "failed (%s)\n", strerror ( rc ) );
80 /* Store and register options */
81 dhcp_options = dhcp.options;
82 register_dhcp_options ( dhcp_options );
84 /* Retrieve IP address configuration */
85 find_dhcp_ipv4_option ( dhcp_options, DHCP_EB_YIADDR, &address );
86 find_dhcp_ipv4_option ( dhcp_options, DHCP_SUBNET_MASK, &netmask );
87 find_dhcp_ipv4_option ( dhcp_options, DHCP_ROUTERS, &gateway );
89 /* Set up new IP address configuration */
90 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
92 printf ( "Could not configure %s with DHCP results: %s\n",
93 netdev->name, strerror ( rc ) );
97 /* Retrieve other DHCP options that we care about */
98 find_dhcp_ipv4_option ( dhcp_options, DHCP_DNS_SERVERS,