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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
31 #include <ipxe/settings.h>
32 #include <ipxe/netdevice.h>
33 #include <ipxe/dhcppkt.h>
34 #include <ipxe/fakedhcp.h>
43 * Copy settings to DHCP packet
45 * @v dest Destination DHCP packet
46 * @v source Source settings block
47 * @v encapsulator Encapsulating setting tag number, or zero
48 * @ret rc Return status code
50 static int copy_encap_settings ( struct dhcp_packet
*dest
,
51 struct settings
*source
,
52 unsigned int encapsulator
) {
53 struct setting setting
= { .name
= "" };
60 for ( subtag
= DHCP_MIN_OPTION
; subtag
<= DHCP_MAX_OPTION
; subtag
++ ) {
61 tag
= DHCP_ENCAP_OPT ( encapsulator
, subtag
);
64 case DHCP_VENDOR_ENCAP
:
65 /* Process encapsulated settings */
66 if ( ( rc
= copy_encap_settings ( dest
, source
,
71 /* Copy setting, if present */
73 len
= fetch_raw_setting_copy ( source
, &setting
, &data
);
75 rc
= dhcppkt_store ( dest
, tag
, data
, len
);
88 * Copy settings to DHCP packet
90 * @v dest Destination DHCP packet
91 * @v source Source settings block
92 * @ret rc Return status code
94 static int copy_settings ( struct dhcp_packet
*dest
,
95 struct settings
*source
) {
96 return copy_encap_settings ( dest
, source
, 0 );
100 * Create fake DHCPDISCOVER packet
102 * @v netdev Network device
103 * @v data Buffer for DHCP packet
104 * @v max_len Size of DHCP packet buffer
105 * @ret rc Return status code
107 * Used by external code.
109 int create_fakedhcpdiscover ( struct net_device
*netdev
,
110 void *data
, size_t max_len
) {
111 struct dhcp_packet dhcppkt
;
112 struct in_addr ciaddr
= { 0 };
115 if ( ( rc
= dhcp_create_request ( &dhcppkt
, netdev
, DHCPDISCOVER
,
116 dhcp_last_xid
, ciaddr
, data
,
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
,
143 dhcp_last_xid
, NULL
, 0,
144 data
, max_len
) ) != 0 ) {
145 DBG ( "Could not create DHCPACK: %s\n", strerror ( rc
) );
149 /* Merge in globally-scoped settings, then netdev-specific
150 * settings. Do it in this order so that netdev-specific
151 * settings take precedence regardless of stated priorities.
153 if ( ( rc
= copy_settings ( &dhcppkt
, NULL
) ) != 0 ) {
154 DBG ( "Could not set DHCPACK global settings: %s\n",
158 if ( ( rc
= copy_settings ( &dhcppkt
,
159 netdev_settings ( netdev
) ) ) != 0 ) {
160 DBG ( "Could not set DHCPACK netdev settings: %s\n",
169 * Create fake PXE Boot Server ACK packet
171 * @v netdev Network device
172 * @v data Buffer for DHCP packet
173 * @v max_len Size of DHCP packet buffer
174 * @ret rc Return status code
176 * Used by external code.
178 int create_fakepxebsack ( struct net_device
*netdev
,
179 void *data
, size_t max_len
) {
180 struct dhcp_packet dhcppkt
;
181 struct settings
*proxy_settings
;
182 struct settings
*pxebs_settings
;
185 /* Identify available settings */
186 proxy_settings
= find_settings ( PROXYDHCP_SETTINGS_NAME
);
187 pxebs_settings
= find_settings ( PXEBS_SETTINGS_NAME
);
188 if ( ( ! proxy_settings
) && ( ! pxebs_settings
) ) {
189 /* No PXE boot server; return the regular DHCPACK */
190 return create_fakedhcpack ( netdev
, data
, max_len
);
193 /* Create base DHCPACK packet */
194 if ( ( rc
= dhcp_create_packet ( &dhcppkt
, netdev
, DHCPACK
,
195 dhcp_last_xid
, NULL
, 0,
196 data
, max_len
) ) != 0 ) {
197 DBG ( "Could not create PXE BS ACK: %s\n",
202 /* Merge in ProxyDHCP options */
203 if ( proxy_settings
&&
204 ( ( rc
= copy_settings ( &dhcppkt
, proxy_settings
) ) != 0 ) ) {
205 DBG ( "Could not copy ProxyDHCP settings: %s\n",
210 /* Merge in BootServerDHCP options, if present */
211 if ( pxebs_settings
&&
212 ( ( rc
= copy_settings ( &dhcppkt
, pxebs_settings
) ) != 0 ) ) {
213 DBG ( "Could not copy PXE BS settings: %s\n",