2 * Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
27 #include <ipxe/settings.h>
28 #include <ipxe/netdevice.h>
29 #include <ipxe/dhcppkt.h>
30 #include <ipxe/fakedhcp.h>
39 * Copy settings to DHCP packet
41 * @v dest Destination DHCP packet
42 * @v source Source settings block
43 * @v encapsulator Encapsulating setting tag number, or zero
44 * @ret rc Return status code
46 static int copy_encap_settings ( struct dhcp_packet *dest,
47 struct settings *source,
48 unsigned int encapsulator ) {
49 struct setting setting = { .name = "" };
56 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
57 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
60 case DHCP_VENDOR_ENCAP:
61 /* Process encapsulated settings */
62 if ( ( rc = copy_encap_settings ( dest, source,
67 /* Copy setting, if present */
69 len = fetch_raw_setting_copy ( source, &setting, &data);
71 rc = dhcppkt_store ( dest, tag, data, len );
84 * Copy settings to DHCP packet
86 * @v dest Destination DHCP packet
87 * @v source Source settings block
88 * @ret rc Return status code
90 static int copy_settings ( struct dhcp_packet *dest,
91 struct settings *source ) {
92 return copy_encap_settings ( dest, source, 0 );
96 * Create fake DHCPDISCOVER packet
98 * @v netdev Network device
99 * @v data Buffer for DHCP packet
100 * @v max_len Size of DHCP packet buffer
101 * @ret rc Return status code
103 * Used by external code.
105 int create_fakedhcpdiscover ( struct net_device *netdev,
106 void *data, size_t max_len ) {
107 struct dhcp_packet dhcppkt;
108 struct in_addr ciaddr = { 0 };
111 if ( ( rc = dhcp_create_request ( &dhcppkt, netdev, DHCPDISCOVER,
112 dhcp_last_xid, ciaddr, data,
114 DBG ( "Could not create DHCPDISCOVER: %s\n",
123 * Create fake DHCPACK packet
125 * @v netdev Network device
126 * @v data Buffer for DHCP packet
127 * @v max_len Size of DHCP packet buffer
128 * @ret rc Return status code
130 * Used by external code.
132 int create_fakedhcpack ( struct net_device *netdev,
133 void *data, size_t max_len ) {
134 struct dhcp_packet dhcppkt;
137 /* Create base DHCPACK packet */
138 if ( ( rc = dhcp_create_packet ( &dhcppkt, netdev, DHCPACK,
139 dhcp_last_xid, NULL, 0,
140 data, max_len ) ) != 0 ) {
141 DBG ( "Could not create DHCPACK: %s\n", strerror ( rc ) );
145 /* Merge in globally-scoped settings, then netdev-specific
146 * settings. Do it in this order so that netdev-specific
147 * settings take precedence regardless of stated priorities.
149 if ( ( rc = copy_settings ( &dhcppkt, NULL ) ) != 0 ) {
150 DBG ( "Could not set DHCPACK global settings: %s\n",
154 if ( ( rc = copy_settings ( &dhcppkt,
155 netdev_settings ( netdev ) ) ) != 0 ) {
156 DBG ( "Could not set DHCPACK netdev settings: %s\n",
165 * Create fake PXE Boot Server ACK packet
167 * @v netdev Network device
168 * @v data Buffer for DHCP packet
169 * @v max_len Size of DHCP packet buffer
170 * @ret rc Return status code
172 * Used by external code.
174 int create_fakepxebsack ( struct net_device *netdev,
175 void *data, size_t max_len ) {
176 struct dhcp_packet dhcppkt;
177 struct settings *proxy_settings;
178 struct settings *pxebs_settings;
181 /* Identify available settings */
182 proxy_settings = find_settings ( PROXYDHCP_SETTINGS_NAME );
183 pxebs_settings = find_settings ( PXEBS_SETTINGS_NAME );
184 if ( ( ! proxy_settings ) && ( ! pxebs_settings ) ) {
185 /* No PXE boot server; return the regular DHCPACK */
186 return create_fakedhcpack ( netdev, data, max_len );
189 /* Create base DHCPACK packet */
190 if ( ( rc = dhcp_create_packet ( &dhcppkt, netdev, DHCPACK,
191 dhcp_last_xid, NULL, 0,
192 data, max_len ) ) != 0 ) {
193 DBG ( "Could not create PXE BS ACK: %s\n",
198 /* Merge in ProxyDHCP options */
199 if ( proxy_settings &&
200 ( ( rc = copy_settings ( &dhcppkt, proxy_settings ) ) != 0 ) ) {
201 DBG ( "Could not copy ProxyDHCP settings: %s\n",
206 /* Merge in BootServerDHCP options, if present */
207 if ( pxebs_settings &&
208 ( ( rc = copy_settings ( &dhcppkt, pxebs_settings ) ) != 0 ) ) {
209 DBG ( "Could not copy PXE BS settings: %s\n",