1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
* Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <ipxe/settings.h>
#include <ipxe/netdevice.h>
#include <ipxe/dhcppkt.h>
#include <ipxe/fakedhcp.h>
/** @file
*
* Fake DHCP packets
*
*/
/**
* Copy settings to DHCP packet
*
* @v dest Destination DHCP packet
* @v source Source settings block
* @v encapsulator Encapsulating setting tag number, or zero
* @ret rc Return status code
*/
static int copy_encap_settings ( struct dhcp_packet *dest,
struct settings *source,
unsigned int encapsulator ) {
struct setting setting = { .name = "" };
unsigned int subtag;
unsigned int tag;
void *data;
int len;
int rc;
for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
switch ( tag ) {
case DHCP_EB_ENCAP:
case DHCP_VENDOR_ENCAP:
/* Process encapsulated settings */
if ( ( rc = copy_encap_settings ( dest, source,
tag ) ) != 0 )
return rc;
break;
default:
/* Copy setting, if present */
setting.tag = tag;
len = fetch_raw_setting_copy ( source, &setting, &data);
if ( len >= 0 ) {
rc = dhcppkt_store ( dest, tag, data, len );
free ( data );
if ( rc != 0 )
return rc;
}
break;
}
}
return 0;
}
/**
* Copy settings to DHCP packet
*
* @v dest Destination DHCP packet
* @v source Source settings block
* @ret rc Return status code
*/
static int copy_settings ( struct dhcp_packet *dest,
struct settings *source ) {
return copy_encap_settings ( dest, source, 0 );
}
/**
* Create fake DHCPDISCOVER packet
*
* @v netdev Network device
* @v data Buffer for DHCP packet
* @v max_len Size of DHCP packet buffer
* @ret rc Return status code
*
* Used by external code.
*/
int create_fakedhcpdiscover ( struct net_device *netdev,
void *data, size_t max_len ) {
struct dhcp_packet dhcppkt;
struct in_addr ciaddr = { 0 };
int rc;
if ( ( rc = dhcp_create_request ( &dhcppkt, netdev, DHCPDISCOVER,
dhcp_last_xid, ciaddr, data,
max_len ) ) != 0 ) {
DBG ( "Could not create DHCPDISCOVER: %s\n",
strerror ( rc ) );
return rc;
}
return 0;
}
/**
* Create fake DHCPACK packet
*
* @v netdev Network device
* @v data Buffer for DHCP packet
* @v max_len Size of DHCP packet buffer
* @ret rc Return status code
*
* Used by external code.
*/
int create_fakedhcpack ( struct net_device *netdev,
void *data, size_t max_len ) {
struct dhcp_packet dhcppkt;
int rc;
/* Create base DHCPACK packet */
if ( ( rc = dhcp_create_packet ( &dhcppkt, netdev, DHCPACK,
dhcp_last_xid, NULL, 0,
data, max_len ) ) != 0 ) {
DBG ( "Could not create DHCPACK: %s\n", strerror ( rc ) );
return rc;
}
/* Merge in globally-scoped settings, then netdev-specific
* settings. Do it in this order so that netdev-specific
* settings take precedence regardless of stated priorities.
*/
if ( ( rc = copy_settings ( &dhcppkt, NULL ) ) != 0 ) {
DBG ( "Could not set DHCPACK global settings: %s\n",
strerror ( rc ) );
return rc;
}
if ( ( rc = copy_settings ( &dhcppkt,
netdev_settings ( netdev ) ) ) != 0 ) {
DBG ( "Could not set DHCPACK netdev settings: %s\n",
strerror ( rc ) );
return rc;
}
return 0;
}
/**
* Create fake PXE Boot Server ACK packet
*
* @v netdev Network device
* @v data Buffer for DHCP packet
* @v max_len Size of DHCP packet buffer
* @ret rc Return status code
*
* Used by external code.
*/
int create_fakepxebsack ( struct net_device *netdev,
void *data, size_t max_len ) {
struct dhcp_packet dhcppkt;
struct settings *proxy_settings;
struct settings *pxebs_settings;
int rc;
/* Identify available settings */
proxy_settings = find_settings ( PROXYDHCP_SETTINGS_NAME );
pxebs_settings = find_settings ( PXEBS_SETTINGS_NAME );
if ( ( ! proxy_settings ) && ( ! pxebs_settings ) ) {
/* No PXE boot server; return the regular DHCPACK */
return create_fakedhcpack ( netdev, data, max_len );
}
/* Create base DHCPACK packet */
if ( ( rc = dhcp_create_packet ( &dhcppkt, netdev, DHCPACK,
dhcp_last_xid, NULL, 0,
data, max_len ) ) != 0 ) {
DBG ( "Could not create PXE BS ACK: %s\n",
strerror ( rc ) );
return rc;
}
/* Merge in ProxyDHCP options */
if ( proxy_settings &&
( ( rc = copy_settings ( &dhcppkt, proxy_settings ) ) != 0 ) ) {
DBG ( "Could not copy ProxyDHCP settings: %s\n",
strerror ( rc ) );
return rc;
}
/* Merge in BootServerDHCP options, if present */
if ( pxebs_settings &&
( ( rc = copy_settings ( &dhcppkt, pxebs_settings ) ) != 0 ) ) {
DBG ( "Could not copy PXE BS settings: %s\n",
strerror ( rc ) );
return rc;
}
return 0;
}
|