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/uri.h>
37#include <ipxe/cachedhcp.h>
38
39/** @file
40 *
41 * Cached DHCP packet
42 *
43 */
44
45/** A cached DHCP packet */
47 /** Settings block name */
48 const char *name;
49 /** DHCP packet (if any) */
51 /** VLAN tag (if applicable) */
52 unsigned int vlan;
53 /** Flags */
54 unsigned int flags;
55};
56
57/** Cached DHCP packet should be retained */
58#define CACHEDHCP_RETAIN 0x0001
59
60/** Cached DHCP packet has been used */
61#define CACHEDHCP_USED 0x0002
62
63/** Cached DHCPACK */
68
69/** Cached ProxyDHCPOFFER */
73
74/** Cached PXEBSACK */
78
79/** List of cached DHCP packets */
85
86/** Colour for debug messages */
87#define colour &cached_dhcpack
88
89/**
90 * Free cached DHCP packet
91 *
92 * @v cache Cached DHCP packet
93 */
94static void cachedhcp_free ( struct cached_dhcp_packet *cache ) {
95
96 dhcppkt_put ( cache->dhcppkt );
97 cache->dhcppkt = NULL;
98}
99
100/**
101 * Apply cached DHCP packet settings
102 *
103 * @v cache Cached DHCP packet
104 * @v netdev Network device, or NULL
105 * @ret rc Return status code
106 */
107static int cachedhcp_apply ( struct cached_dhcp_packet *cache,
108 struct net_device *netdev ) {
109 struct settings *settings = NULL;
110 struct ll_protocol *ll_protocol;
111 const uint8_t *chaddr;
112 uint8_t *hw_addr;
113 uint8_t *ll_addr;
114 size_t ll_addr_len;
115 int rc;
116
117 /* Do nothing if cache is empty or already in use */
118 if ( ( ! cache->dhcppkt ) || ( cache->flags & CACHEDHCP_USED ) )
119 return 0;
120 chaddr = cache->dhcppkt->dhcphdr->chaddr;
121
122 /* Handle association with network device, if specified */
123 if ( netdev ) {
124 hw_addr = netdev->hw_addr;
125 ll_addr = netdev->ll_addr;
126 ll_protocol = netdev->ll_protocol;
128
129 /* If cached packet's MAC address matches the network
130 * device's permanent MAC address, then assume that
131 * the permanent MAC address ought to be the network
132 * device's current link-layer address.
133 *
134 * This situation can arise when the PXE ROM does not
135 * understand the system-specific mechanism for
136 * overriding the MAC address, and so uses the
137 * permanent MAC address instead. We choose to match
138 * this behaviour in order to minimise surprise.
139 */
140 if ( memcmp ( hw_addr, chaddr, ll_addr_len ) == 0 ) {
141 if ( memcmp ( hw_addr, ll_addr, ll_addr_len ) != 0 ) {
142 DBGC ( colour, "CACHEDHCP %s resetting %s MAC "
143 "%s ", cache->name, netdev->name,
144 ll_protocol->ntoa ( ll_addr ) );
145 DBGC ( colour, "-> %s\n",
146 ll_protocol->ntoa ( hw_addr ) );
147 }
148 memcpy ( ll_addr, hw_addr, ll_addr_len );
149 }
150
151 /* Do nothing unless cached packet's MAC address
152 * matches this network device.
153 */
154 if ( memcmp ( ll_addr, chaddr, ll_addr_len ) != 0 ) {
155 DBGC ( colour, "CACHEDHCP %s %s does not match %s\n",
156 cache->name, ll_protocol->ntoa ( chaddr ),
157 netdev->name );
158 return 0;
159 }
160
161 /* Do nothing unless cached packet's VLAN tag matches
162 * this network device.
163 */
164 if ( vlan_tag ( netdev ) != cache->vlan ) {
165 DBGC ( colour, "CACHEDHCP %s VLAN %d does not match "
166 "%s\n", cache->name, cache->vlan,
167 netdev->name );
168 return 0;
169 }
170
171 /* Use network device's settings block */
173 DBGC ( colour, "CACHEDHCP %s is for %s\n",
174 cache->name, netdev->name );
175
176 /* Mark network device to be opened automatically */
177 netdev->state |= NETDEV_AUTO_OPEN;
178 }
179
180 /* Register settings */
181 if ( ( rc = register_settings ( &cache->dhcppkt->settings, settings,
182 cache->name ) ) != 0 ) {
183 DBGC ( colour, "CACHEDHCP %s could not register settings: %s\n",
184 cache->name, strerror ( rc ) );
185 return rc;
186 }
187
188 /* Mark as used */
189 cache->flags |= CACHEDHCP_USED;
190
191 /* Free cached DHCP packet, if applicable */
192 if ( ! ( cache->flags & CACHEDHCP_RETAIN ) )
193 cachedhcp_free ( cache );
194
195 return 0;
196}
197
198/**
199 * Get URI from cached DHCP packet
200 *
201 * @v cache Cached DHCP packet
202 * @ret uri URI, or NULL if not defined
203 */
204static struct uri * cachedhcp_uri ( struct cached_dhcp_packet *cache ) {
205 struct dhcp_packet *dhcppkt = cache->dhcppkt;
206 struct settings *settings = &dhcppkt->settings;
207 struct uri *uri = NULL;
208 union {
209 struct sockaddr sa;
210 struct sockaddr_in sin;
211 } next_server;
212 char *filename;
213
214 /* Fetch next-server address */
215 memset ( &next_server, 0, sizeof ( next_server ) );
216 next_server.sin.sin_family = AF_INET;
217 fetch_ipv4_setting ( settings, &next_server_setting,
218 &next_server.sin.sin_addr );
219 if ( next_server.sin.sin_addr.s_addr ) {
220 DBGC ( colour, "CACHEDHCP %s has next-server %s\n",
221 cache->name, inet_ntoa ( next_server.sin.sin_addr ) );
222 }
223
224 /* Fetch filename */
225 fetch_string_setting_copy ( settings, &filename_setting, &filename );
226 if ( ! filename )
227 goto err_filename;
228 DBGC ( colour, "CACHEDHCP %s has filename %s\n",
229 cache->name, filename );
230
231 /* Construct URI */
232 uri = pxe_uri ( &next_server.sa, filename );
233 if ( ! uri )
234 goto err_uri;
235
236 err_uri:
237 free ( filename );
238 err_filename:
239 return uri;
240}
241
242/**
243 * Record cached DHCP packet
244 *
245 * @v cache Cached DHCP packet
246 * @v vlan VLAN tag, if any
247 * @v data DHCPACK packet buffer
248 * @v max_len Maximum possible length
249 * @ret rc Return status code
250 */
251int cachedhcp_record ( struct cached_dhcp_packet *cache, unsigned int vlan,
252 const void *data, size_t max_len ) {
253 struct dhcp_packet *dhcppkt;
254 struct dhcp_packet *tmp;
255 struct dhcphdr *dhcphdr;
256 struct uri *uri;
257 unsigned int i;
258 size_t len;
259
260 /* Free any existing cached packet */
261 cachedhcp_free ( cache );
262
263 /* Allocate and populate DHCP packet */
264 dhcppkt = zalloc ( sizeof ( *dhcppkt ) + max_len );
265 if ( ! dhcppkt ) {
266 DBGC ( colour, "CACHEDHCP %s could not allocate copy\n",
267 cache->name );
268 return -ENOMEM;
269 }
270 dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
271 memcpy ( dhcphdr, data, max_len );
272 dhcppkt_init ( dhcppkt, dhcphdr, max_len );
273
274 /* Shrink packet to required length. If reallocation fails,
275 * just continue to use the original packet and waste the
276 * unused space.
277 */
278 len = dhcppkt_len ( dhcppkt );
279 assert ( len <= max_len );
280 tmp = realloc ( dhcppkt, ( sizeof ( *dhcppkt ) + len ) );
281 if ( tmp )
282 dhcppkt = tmp;
283
284 /* Reinitialise packet at new address */
285 dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
286 dhcppkt_init ( dhcppkt, dhcphdr, len );
287
288 /* Discard duplicate packets, since some PXE stacks (including
289 * iPXE itself) will report the DHCPACK packet as the PXEBSACK
290 * if no separate PXEBSACK exists.
291 */
292 for ( i = 0 ; i < ( sizeof ( cached_packets ) /
293 sizeof ( cached_packets[0] ) ) ; i++ ) {
294 tmp = cached_packets[i]->dhcppkt;
295 if ( tmp && ( dhcppkt_len ( tmp ) == len ) &&
296 ( memcmp ( tmp->dhcphdr, dhcppkt->dhcphdr, len ) == 0 ) ) {
297 DBGC ( colour, "CACHEDHCP %s duplicates %s\n",
298 cache->name, cached_packets[i]->name );
299 dhcppkt_put ( dhcppkt );
300 return -EEXIST;
301 }
302 }
303
304 /* Store as cached packet */
305 DBGC ( colour, "CACHEDHCP %s at %#08lx+%#zx/%#zx\n", cache->name,
306 virt_to_phys ( data ), len, max_len );
307 cache->dhcppkt = dhcppkt;
308 cache->vlan = vlan;
309
310 /* Set current working URI, if defined in this packet */
311 uri = cachedhcp_uri ( cache );
312 if ( uri )
313 churi ( uri );
314 uri_put ( uri );
315
316 return 0;
317}
318
319/**
320 * Cached DHCP packet early startup function
321 *
322 */
323static void cachedhcp_startup_early ( void ) {
324
325 /* Apply cached ProxyDHCPOFFER, if any */
328
329 /* Apply cached PXEBSACK, if any */
332}
333
334/**
335 * Cache DHCP packet late startup function
336 *
337 */
338static void cachedhcp_startup_late ( void ) {
339
340 /* Clear retention flag */
342
343 /* Free cached DHCPACK, if used by a network device */
344 if ( cached_dhcpack.flags & CACHEDHCP_USED )
346
347 /* Report unclaimed DHCPACK, if any. Do not free yet, since
348 * it may still be claimed by a dynamically created device
349 * such as a VLAN device.
350 */
351 if ( cached_dhcpack.dhcppkt ) {
352 DBGC ( colour, "CACHEDHCP %s unclaimed\n",
353 cached_dhcpack.name );
354 }
355}
356
357/**
358 * Cached DHCP packet shutdown function
359 *
360 * @v booting System is shutting down for OS boot
361 */
362static void cachedhcp_shutdown ( int booting __unused ) {
363
364 /* Free cached DHCPACK, if any */
365 if ( cached_dhcpack.dhcppkt ) {
366 DBGC ( colour, "CACHEDHCP %s never claimed\n",
367 cached_dhcpack.name );
368 }
370}
371
372/** Cached DHCP packet early startup function */
373struct startup_fn cachedhcp_early_fn __startup_fn ( STARTUP_EARLY ) = {
374 .name = "cachedhcp1",
375 .startup = cachedhcp_startup_early,
376};
377
378/** Cached DHCP packet late startup function */
379struct startup_fn cachedhcp_late_fn __startup_fn ( STARTUP_LATE ) = {
380 .name = "cachedhcp2",
381 .startup = cachedhcp_startup_late,
382 .shutdown = cachedhcp_shutdown,
383};
384
385/**
386 * Apply cached DHCPACK to network device, if applicable
387 *
388 * @v netdev Network device
389 * @v priv Private data
390 * @ret rc Return status code
391 */
392static int cachedhcp_probe ( struct net_device *netdev, void *priv __unused ) {
393
394 /* Apply cached DHCPACK to network device, if applicable */
396}
397
398/** Cached DHCP packet network device driver */
399struct net_driver cachedhcp_driver __net_driver = {
400 .name = "cachedhcp",
401 .probe = cachedhcp_probe,
402};
403
404/**
405 * Recycle cached DHCPACK
406 *
407 * @v netdev Network device
408 * @v priv Private data
409 */
411 struct cached_dhcp_packet *cache = &cached_dhcpack;
412 struct settings *settings;
413
414 /* Return DHCPACK to cache, if applicable */
416 cache->name );
417 if ( cache->dhcppkt && ( settings == &cache->dhcppkt->settings ) ) {
418 DBGC ( colour, "CACHEDHCP %s recycled from %s\n",
419 cache->name, netdev->name );
420 assert ( cache->flags & CACHEDHCP_USED );
422 cache->flags &= ~CACHEDHCP_USED;
423 }
424}
#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:70
struct cached_dhcp_packet cached_pxebs
Cached PXEBSACK.
Definition cachedhcp.c:75
#define CACHEDHCP_RETAIN
Cached DHCP packet should be retained.
Definition cachedhcp.c:58
static int cachedhcp_probe(struct net_device *netdev, void *priv __unused)
Apply cached DHCPACK to network device, if applicable.
Definition cachedhcp.c:392
static void cachedhcp_startup_early(void)
Cached DHCP packet early startup function.
Definition cachedhcp.c:323
static void cachedhcp_startup_late(void)
Cache DHCP packet late startup function.
Definition cachedhcp.c:338
#define CACHEDHCP_USED
Cached DHCP packet has been used.
Definition cachedhcp.c:61
void cachedhcp_recycle(struct net_device *netdev)
Recycle cached DHCPACK.
Definition cachedhcp.c:410
static struct cached_dhcp_packet * cached_packets[]
List of cached DHCP packets.
Definition cachedhcp.c:80
struct cached_dhcp_packet cached_dhcpack
Cached DHCPACK.
Definition cachedhcp.c:64
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:251
static void cachedhcp_free(struct cached_dhcp_packet *cache)
Free cached DHCP packet.
Definition cachedhcp.c:94
static int cachedhcp_apply(struct cached_dhcp_packet *cache, struct net_device *netdev)
Apply cached DHCP packet settings.
Definition cachedhcp.c:107
static struct uri * cachedhcp_uri(struct cached_dhcp_packet *cache)
Get URI from cached DHCP packet.
Definition cachedhcp.c:204
static void cachedhcp_shutdown(int booting __unused)
Cached DHCP packet shutdown function.
Definition cachedhcp.c:362
Cached DHCP packet.
void churi(struct uri *uri)
Change working URI.
Definition cwuri.c:46
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 AF_INET
IPv4 Internet addresses.
Definition socket.h:64
#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
void * memset(void *dest, int character, size_t len) __nonnull
#define __startup_fn(startup_order)
Declare a startup/shutdown function.
Definition init.h:53
char * inet_ntoa(struct in_addr in)
Convert IPv4 address to dotted-quad notation.
Definition ipv4.c:814
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 NETDEV_AUTO_OPEN
Network device should be opened automatically.
Definition netdevice.h:465
#define __net_driver
Declare a network driver.
Definition netdevice.h:510
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition netdevice.h:590
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition settings.c:476
int fetch_string_setting_copy(struct settings *settings, const struct setting *setting, char **data)
Fetch value of string setting.
Definition settings.c:874
void unregister_settings(struct settings *settings)
Unregister settings block.
Definition settings.c:515
int fetch_ipv4_setting(struct settings *settings, const struct setting *setting, struct in_addr *inp)
Fetch value of IPv4 address setting.
Definition settings.c:913
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:46
struct dhcp_packet * dhcppkt
DHCP packet (if any)
Definition cachedhcp.c:50
unsigned int vlan
VLAN tag (if applicable)
Definition cachedhcp.c:52
unsigned int flags
Flags.
Definition cachedhcp.c:54
const char * name
Settings block name.
Definition cachedhcp.c:48
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:480
A settings block.
Definition settings.h:133
IPv4 socket address.
Definition in.h:85
Generalized socket address structure.
Definition socket.h:97
A startup/shutdown function.
Definition init.h:43
A Uniform Resource Identifier.
Definition uri.h:65
struct sockaddr sa
Definition syslog.c:57
struct sockaddr_in sin
Definition syslog.c:59
static struct tlan_private * priv
Definition tlan.c:225
struct uri * pxe_uri(struct sockaddr *sa_server, const char *filename)
Construct URI from server address and filename.
Definition uri.c:810
Uniform Resource Identifiers.
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition uri.h:206
Virtual LANs.
static unsigned int vlan_tag(struct net_device *netdev)
Get the VLAN tag.
Definition vlan.h:74