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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
26 #include <gpxe/settings.h>
27 #include <gpxe/netdevice.h>
28 #include <gpxe/dhcppkt.h>
29 #include <gpxe/fakedhcp.h>
38 * Copy settings to DHCP packet
40 * @v dest Destination DHCP packet
41 * @v source Source settings block
42 * @v encapsulator Encapsulating setting tag number, or zero
43 * @ret rc Return status code
45 static int copy_encap_settings ( struct dhcp_packet
*dest
,
46 struct settings
*source
,
47 unsigned int encapsulator
) {
48 struct setting setting
= { .name
= "" };
55 for ( subtag
= DHCP_MIN_OPTION
; subtag
<= DHCP_MAX_OPTION
; subtag
++ ) {
56 tag
= DHCP_ENCAP_OPT ( encapsulator
, subtag
);
59 case DHCP_VENDOR_ENCAP
:
60 /* Process encapsulated settings */
61 if ( ( rc
= copy_encap_settings ( dest
, source
,
66 /* Copy setting, if present */
68 len
= fetch_setting_len ( source
, &setting
);
74 check_len
= fetch_setting ( source
, &setting
,
76 assert ( check_len
== len
);
77 if ( ( rc
= dhcppkt_store ( dest
, tag
, buf
,
89 * Copy settings to DHCP packet
91 * @v dest Destination DHCP packet
92 * @v source Source settings block
93 * @ret rc Return status code
95 static int copy_settings ( struct dhcp_packet
*dest
,
96 struct settings
*source
) {
97 return copy_encap_settings ( dest
, source
, 0 );
101 * Create fake DHCPDISCOVER packet
103 * @v netdev Network device
104 * @v data Buffer for DHCP packet
105 * @v max_len Size of DHCP packet buffer
106 * @ret rc Return status code
108 * Used by external code.
110 int create_fakedhcpdiscover ( struct net_device
*netdev
,
111 void *data
, size_t max_len
) {
112 struct dhcp_packet dhcppkt
;
113 struct in_addr ciaddr
= { 0 };
116 if ( ( rc
= dhcp_create_request ( &dhcppkt
, netdev
, DHCPDISCOVER
,
117 ciaddr
, data
, max_len
) ) != 0 ) {
118 DBG ( "Could not create DHCPDISCOVER: %s\n",
127 * Create fake DHCPACK packet
129 * @v netdev Network device
130 * @v data Buffer for DHCP packet
131 * @v max_len Size of DHCP packet buffer
132 * @ret rc Return status code
134 * Used by external code.
136 int create_fakedhcpack ( struct net_device
*netdev
,
137 void *data
, size_t max_len
) {
138 struct dhcp_packet dhcppkt
;
141 /* Create base DHCPACK packet */
142 if ( ( rc
= dhcp_create_packet ( &dhcppkt
, netdev
, DHCPACK
, NULL
, 0,
143 data
, max_len
) ) != 0 ) {
144 DBG ( "Could not create DHCPACK: %s\n", strerror ( rc
) );
148 /* Merge in globally-scoped settings, then netdev-specific
149 * settings. Do it in this order so that netdev-specific
150 * settings take precedence regardless of stated priorities.
152 if ( ( rc
= copy_settings ( &dhcppkt
, NULL
) ) != 0 ) {
153 DBG ( "Could not set DHCPACK global settings: %s\n",
157 if ( ( rc
= copy_settings ( &dhcppkt
,
158 netdev_settings ( netdev
) ) ) != 0 ) {
159 DBG ( "Could not set DHCPACK netdev settings: %s\n",
168 * Create fake PXE Boot Server ACK packet
170 * @v netdev Network device
171 * @v data Buffer for DHCP packet
172 * @v max_len Size of DHCP packet buffer
173 * @ret rc Return status code
175 * Used by external code.
177 int create_fakepxebsack ( struct net_device
*netdev
,
178 void *data
, size_t max_len
) {
179 struct dhcp_packet dhcppkt
;
180 struct settings
*proxy_settings
;
181 struct settings
*pxebs_settings
;
184 /* Identify available settings */
185 proxy_settings
= find_settings ( PROXYDHCP_SETTINGS_NAME
);
186 pxebs_settings
= find_settings ( PXEBS_SETTINGS_NAME
);
187 if ( ( ! proxy_settings
) && ( ! pxebs_settings
) ) {
188 /* No PXE boot server; return the regular DHCPACK */
189 return create_fakedhcpack ( netdev
, data
, max_len
);
192 /* Create base DHCPACK packet */
193 if ( ( rc
= dhcp_create_packet ( &dhcppkt
, netdev
, DHCPACK
, NULL
, 0,
194 data
, max_len
) ) != 0 ) {
195 DBG ( "Could not create PXE BS ACK: %s\n",
200 /* Merge in ProxyDHCP options */
201 if ( proxy_settings
&&
202 ( ( rc
= copy_settings ( &dhcppkt
, proxy_settings
) ) != 0 ) ) {
203 DBG ( "Could not copy ProxyDHCP settings: %s\n",
208 /* Merge in BootServerDHCP options, if present */
209 if ( pxebs_settings
&&
210 ( ( rc
= copy_settings ( &dhcppkt
, pxebs_settings
) ) != 0 ) ) {
211 DBG ( "Could not copy PXE BS settings: %s\n",