iPXE
cachedhcp.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3 *
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.
8 *
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.
13 *
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
17 * 02110-1301, USA.
18 *
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.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stdint.h>
28#include <stdlib.h>
29#include <string.h>
30#include <errno.h>
31#include <ipxe/dhcppkt.h>
32#include <ipxe/init.h>
33#include <ipxe/netdevice.h>
34#include <ipxe/vlan.h>
35#include <ipxe/uaccess.h>
36#include <ipxe/cachedhcp.h>
37
38/** @file
39 *
40 * Cached DHCP packet
41 *
42 */
43
44/** A cached DHCP packet */
46 /** Settings block name */
47 const char *name;
48 /** DHCP packet (if any) */
50 /** VLAN tag (if applicable) */
51 unsigned int vlan;
52 /** Flags */
53 unsigned int flags;
54};
55
56/** Cached DHCP packet should be retained */
57#define CACHEDHCP_RETAIN 0x0001
58
59/** Cached DHCP packet has been used */
60#define CACHEDHCP_USED 0x0002
61
62/** Cached DHCPACK */
67
68/** Cached ProxyDHCPOFFER */
72
73/** Cached PXEBSACK */
77
78/** List of cached DHCP packets */
84
85/** Colour for debug messages */
86#define colour &cached_dhcpack
87
88/**
89 * Free cached DHCP packet
90 *
91 * @v cache Cached DHCP packet
92 */
93static void cachedhcp_free ( struct cached_dhcp_packet *cache ) {
94
95 dhcppkt_put ( cache->dhcppkt );
96 cache->dhcppkt = NULL;
97}
98
99/**
100 * Apply cached DHCP packet settings
101 *
102 * @v cache Cached DHCP packet
103 * @v netdev Network device, or NULL
104 * @ret rc Return status code
105 */
106static int cachedhcp_apply ( struct cached_dhcp_packet *cache,
107 struct net_device *netdev ) {
108 struct settings *settings = NULL;
109 struct ll_protocol *ll_protocol;
110 const uint8_t *chaddr;
111 uint8_t *hw_addr;
112 uint8_t *ll_addr;
113 size_t ll_addr_len;
114 int rc;
115
116 /* Do nothing if cache is empty or already in use */
117 if ( ( ! cache->dhcppkt ) || ( cache->flags & CACHEDHCP_USED ) )
118 return 0;
119 chaddr = cache->dhcppkt->dhcphdr->chaddr;
120
121 /* Handle association with network device, if specified */
122 if ( netdev ) {
123 hw_addr = netdev->hw_addr;
124 ll_addr = netdev->ll_addr;
125 ll_protocol = netdev->ll_protocol;
127
128 /* If cached packet's MAC address matches the network
129 * device's permanent MAC address, then assume that
130 * the permanent MAC address ought to be the network
131 * device's current link-layer address.
132 *
133 * This situation can arise when the PXE ROM does not
134 * understand the system-specific mechanism for
135 * overriding the MAC address, and so uses the
136 * permanent MAC address instead. We choose to match
137 * this behaviour in order to minimise surprise.
138 */
139 if ( memcmp ( hw_addr, chaddr, ll_addr_len ) == 0 ) {
140 if ( memcmp ( hw_addr, ll_addr, ll_addr_len ) != 0 ) {
141 DBGC ( colour, "CACHEDHCP %s resetting %s MAC "
142 "%s ", cache->name, netdev->name,
143 ll_protocol->ntoa ( ll_addr ) );
144 DBGC ( colour, "-> %s\n",
145 ll_protocol->ntoa ( hw_addr ) );
146 }
147 memcpy ( ll_addr, hw_addr, ll_addr_len );
148 }
149
150 /* Do nothing unless cached packet's MAC address
151 * matches this network device.
152 */
153 if ( memcmp ( ll_addr, chaddr, ll_addr_len ) != 0 ) {
154 DBGC ( colour, "CACHEDHCP %s %s does not match %s\n",
155 cache->name, ll_protocol->ntoa ( chaddr ),
156 netdev->name );
157 return 0;
158 }
159
160 /* Do nothing unless cached packet's VLAN tag matches
161 * this network device.
162 */
163 if ( vlan_tag ( netdev ) != cache->vlan ) {
164 DBGC ( colour, "CACHEDHCP %s VLAN %d does not match "
165 "%s\n", cache->name, cache->vlan,
166 netdev->name );
167 return 0;
168 }
169
170 /* Use network device's settings block */
172 DBGC ( colour, "CACHEDHCP %s is for %s\n",
173 cache->name, netdev->name );
174 }
175
176 /* Register settings */
177 if ( ( rc = register_settings ( &cache->dhcppkt->settings, settings,
178 cache->name ) ) != 0 ) {
179 DBGC ( colour, "CACHEDHCP %s could not register settings: %s\n",
180 cache->name, strerror ( rc ) );
181 return rc;
182 }
183
184 /* Mark as used */
185 cache->flags |= CACHEDHCP_USED;
186
187 /* Free cached DHCP packet, if applicable */
188 if ( ! ( cache->flags & CACHEDHCP_RETAIN ) )
189 cachedhcp_free ( cache );
190
191 return 0;
192}
193
194/**
195 * Record cached DHCP packet
196 *
197 * @v cache Cached DHCP packet
198 * @v vlan VLAN tag, if any
199 * @v data DHCPACK packet buffer
200 * @v max_len Maximum possible length
201 * @ret rc Return status code
202 */
203int cachedhcp_record ( struct cached_dhcp_packet *cache, unsigned int vlan,
204 const void *data, size_t max_len ) {
205 struct dhcp_packet *dhcppkt;
206 struct dhcp_packet *tmp;
207 struct dhcphdr *dhcphdr;
208 unsigned int i;
209 size_t len;
210
211 /* Free any existing cached packet */
212 cachedhcp_free ( cache );
213
214 /* Allocate and populate DHCP packet */
215 dhcppkt = zalloc ( sizeof ( *dhcppkt ) + max_len );
216 if ( ! dhcppkt ) {
217 DBGC ( colour, "CACHEDHCP %s could not allocate copy\n",
218 cache->name );
219 return -ENOMEM;
220 }
221 dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
222 memcpy ( dhcphdr, data, max_len );
223 dhcppkt_init ( dhcppkt, dhcphdr, max_len );
224
225 /* Shrink packet to required length. If reallocation fails,
226 * just continue to use the original packet and waste the
227 * unused space.
228 */
229 len = dhcppkt_len ( dhcppkt );
230 assert ( len <= max_len );
231 tmp = realloc ( dhcppkt, ( sizeof ( *dhcppkt ) + len ) );
232 if ( tmp )
233 dhcppkt = tmp;
234
235 /* Reinitialise packet at new address */
236 dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
237 dhcppkt_init ( dhcppkt, dhcphdr, len );
238
239 /* Discard duplicate packets, since some PXE stacks (including
240 * iPXE itself) will report the DHCPACK packet as the PXEBSACK
241 * if no separate PXEBSACK exists.
242 */
243 for ( i = 0 ; i < ( sizeof ( cached_packets ) /
244 sizeof ( cached_packets[0] ) ) ; i++ ) {
245 tmp = cached_packets[i]->dhcppkt;
246 if ( tmp && ( dhcppkt_len ( tmp ) == len ) &&
247 ( memcmp ( tmp->dhcphdr, dhcppkt->dhcphdr, len ) == 0 ) ) {
248 DBGC ( colour, "CACHEDHCP %s duplicates %s\n",
249 cache->name, cached_packets[i]->name );
250 dhcppkt_put ( dhcppkt );
251 return -EEXIST;
252 }
253 }
254
255 /* Store as cached packet */
256 DBGC ( colour, "CACHEDHCP %s at %#08lx+%#zx/%#zx\n", cache->name,
257 virt_to_phys ( data ), len, max_len );
258 cache->dhcppkt = dhcppkt;
259 cache->vlan = vlan;
260
261 return 0;
262}
263
264/**
265 * Cached DHCP packet early startup function
266 *
267 */
268static void cachedhcp_startup_early ( void ) {
269
270 /* Apply cached ProxyDHCPOFFER, if any */
273
274 /* Apply cached PXEBSACK, if any */
277}
278
279/**
280 * Cache DHCP packet late startup function
281 *
282 */
283static void cachedhcp_startup_late ( void ) {
284
285 /* Clear retention flag */
287
288 /* Free cached DHCPACK, if used by a network device */
289 if ( cached_dhcpack.flags & CACHEDHCP_USED )
291
292 /* Report unclaimed DHCPACK, if any. Do not free yet, since
293 * it may still be claimed by a dynamically created device
294 * such as a VLAN device.
295 */
296 if ( cached_dhcpack.dhcppkt ) {
297 DBGC ( colour, "CACHEDHCP %s unclaimed\n",
298 cached_dhcpack.name );
299 }
300}
301
302/**
303 * Cached DHCP packet shutdown function
304 *
305 * @v booting System is shutting down for OS boot
306 */
307static void cachedhcp_shutdown ( int booting __unused ) {
308
309 /* Free cached DHCPACK, if any */
310 if ( cached_dhcpack.dhcppkt ) {
311 DBGC ( colour, "CACHEDHCP %s never claimed\n",
312 cached_dhcpack.name );
313 }
315}
316
317/** Cached DHCP packet early startup function */
318struct startup_fn cachedhcp_early_fn __startup_fn ( STARTUP_EARLY ) = {
319 .name = "cachedhcp1",
320 .startup = cachedhcp_startup_early,
321};
322
323/** Cached DHCP packet late startup function */
324struct startup_fn cachedhcp_late_fn __startup_fn ( STARTUP_LATE ) = {
325 .name = "cachedhcp2",
326 .startup = cachedhcp_startup_late,
327 .shutdown = cachedhcp_shutdown,
328};
329
330/**
331 * Apply cached DHCPACK to network device, if applicable
332 *
333 * @v netdev Network device
334 * @v priv Private data
335 * @ret rc Return status code
336 */
337static int cachedhcp_probe ( struct net_device *netdev, void *priv __unused ) {
338
339 /* Apply cached DHCPACK to network device, if applicable */
341}
342
343/** Cached DHCP packet network device driver */
344struct net_driver cachedhcp_driver __net_driver = {
345 .name = "cachedhcp",
346 .probe = cachedhcp_probe,
347};
348
349/**
350 * Recycle cached DHCPACK
351 *
352 * @v netdev Network device
353 * @v priv Private data
354 */
356 struct cached_dhcp_packet *cache = &cached_dhcpack;
357 struct settings *settings;
358
359 /* Return DHCPACK to cache, if applicable */
361 cache->name );
362 if ( cache->dhcppkt && ( settings == &cache->dhcppkt->settings ) ) {
363 DBGC ( colour, "CACHEDHCP %s recycled from %s\n",
364 cache->name, netdev->name );
365 assert ( cache->flags & CACHEDHCP_USED );
367 cache->flags &= ~CACHEDHCP_USED;
368 }
369}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
#define colour
Colour for debug messages.
Definition acpi.c:42
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
unsigned char uint8_t
Definition stdint.h:10
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
struct cached_dhcp_packet cached_proxydhcp
Cached ProxyDHCPOFFER.
Definition cachedhcp.c:69
struct cached_dhcp_packet cached_pxebs
Cached PXEBSACK.
Definition cachedhcp.c:74
#define CACHEDHCP_RETAIN
Cached DHCP packet should be retained.
Definition cachedhcp.c:57
static int cachedhcp_probe(struct net_device *netdev, void *priv __unused)
Apply cached DHCPACK to network device, if applicable.
Definition cachedhcp.c:337
static void cachedhcp_startup_early(void)
Cached DHCP packet early startup function.
Definition cachedhcp.c:268
static void cachedhcp_startup_late(void)
Cache DHCP packet late startup function.
Definition cachedhcp.c:283
#define CACHEDHCP_USED
Cached DHCP packet has been used.
Definition cachedhcp.c:60
void cachedhcp_recycle(struct net_device *netdev)
Recycle cached DHCPACK.
Definition cachedhcp.c:355
static struct cached_dhcp_packet * cached_packets[]
List of cached DHCP packets.
Definition cachedhcp.c:79
struct cached_dhcp_packet cached_dhcpack
Cached DHCPACK.
Definition cachedhcp.c:63
int cachedhcp_record(struct cached_dhcp_packet *cache, unsigned int vlan, const void *data, size_t max_len)
Record cached DHCP packet.
Definition cachedhcp.c:203
static void cachedhcp_free(struct cached_dhcp_packet *cache)
Free cached DHCP packet.
Definition cachedhcp.c:93
static int cachedhcp_apply(struct cached_dhcp_packet *cache, struct net_device *netdev)
Apply cached DHCP packet settings.
Definition cachedhcp.c:106
static void cachedhcp_shutdown(int booting __unused)
Cached DHCP packet shutdown function.
Definition cachedhcp.c:307
Cached DHCP packet.
void dhcppkt_init(struct dhcp_packet *dhcppkt, struct dhcphdr *data, size_t len)
Initialise DHCP packet.
Definition dhcppkt.c:301
DHCP packets.
static void dhcppkt_put(struct dhcp_packet *dhcppkt)
Decrement reference count on DHCP packet.
Definition dhcppkt.h:50
static size_t dhcppkt_len(struct dhcp_packet *dhcppkt)
Get used length of DHCP packet.
Definition dhcppkt.h:60
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
Error codes.
static struct net_device * netdev
Definition gdbudp.c:53
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:573
#define DBGC(...)
Definition compiler.h:505
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define EEXIST
File exists.
Definition errno.h:389
#define ENOMEM
Not enough space.
Definition errno.h:535
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define STARTUP_EARLY
Early startup.
Definition init.h:64
#define STARTUP_LATE
Late startup.
Definition init.h:66
#define PXEBS_SETTINGS_NAME
Setting block name used for BootServerDHCP responses.
Definition dhcp.h:717
#define PROXYDHCP_SETTINGS_NAME
Settings block name used for ProxyDHCP responses.
Definition dhcp.h:714
#define DHCP_SETTINGS_NAME
Settings block name used for DHCP responses.
Definition dhcp.h:711
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define __startup_fn(startup_order)
Declare a startup/shutdown function.
Definition init.h:53
Access to external ("user") memory.
unsigned long tmp
Definition linux_pci.h:65
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition malloc.c:607
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
Network device management.
#define __net_driver
Declare a network driver.
Definition netdevice.h:507
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition netdevice.h:587
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition settings.c:476
void unregister_settings(struct settings *settings)
Unregister settings block.
Definition settings.c:515
struct settings * find_child_settings(struct settings *parent, const char *name)
Find child settings block.
Definition settings.c:280
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
A cached DHCP packet.
Definition cachedhcp.c:45
struct dhcp_packet * dhcppkt
DHCP packet (if any)
Definition cachedhcp.c:49
unsigned int vlan
VLAN tag (if applicable)
Definition cachedhcp.c:51
unsigned int flags
Flags.
Definition cachedhcp.c:53
const char * name
Settings block name.
Definition cachedhcp.c:47
A DHCP packet.
Definition dhcppkt.h:21
struct settings settings
Settings interface.
Definition dhcppkt.h:29
struct dhcphdr * dhcphdr
The DHCP packet contents.
Definition dhcppkt.h:25
A DHCP header.
Definition dhcp.h:616
uint8_t chaddr[16]
Client hardware address.
Definition dhcp.h:662
A link-layer protocol.
Definition netdevice.h:115
const char *(* ntoa)(const void *ll_addr)
Transcribe link-layer address.
Definition netdevice.h:164
uint8_t ll_addr_len
Link-layer address length.
Definition netdevice.h:199
A network device.
Definition netdevice.h:353
A network upper-layer driver.
Definition netdevice.h:477
A settings block.
Definition settings.h:133
A startup/shutdown function.
Definition init.h:43
static struct tlan_private * priv
Definition tlan.c:225
Virtual LANs.
static unsigned int vlan_tag(struct net_device *netdev)
Get the VLAN tag.
Definition vlan.h:74