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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_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 */
65  .flags = CACHEDHCP_RETAIN,
66 };
67 
68 /** Cached ProxyDHCPOFFER */
71 };
72 
73 /** Cached PXEBSACK */
76 };
77 
78 /** List of cached DHCP packets */
79 static struct cached_dhcp_packet *cached_packets[] = {
82  &cached_pxebs,
83 };
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  */
93 static 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  */
106 static 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;
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  */
203 int 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  */
268 static 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  */
283 static void cachedhcp_startup_late ( void ) {
284 
285  /* Clear retention flag */
287 
288  /* Free cached DHCPACK, if used by a network device */
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",
299  }
300 }
301 
302 /**
303  * Cached DHCP packet shutdown function
304  *
305  * @v booting System is shutting down for OS boot
306  */
307 static 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",
313  }
315 }
316 
317 /** Cached DHCP packet early startup function */
318 struct 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 */
324 struct 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  */
337 static 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 */
344 struct 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 colour
Colour for debug messages.
Definition: cachedhcp.c:86
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
A DHCP packet.
Definition: dhcppkt.h:21
uint8_t ll_addr_len
Link-layer address length.
Definition: netdevice.h:199
FILE_SECBOOT(PERMITTED)
void unregister_settings(struct settings *settings)
Unregister settings block.
Definition: settings.c:515
unsigned int vlan
VLAN tag (if applicable)
Definition: cachedhcp.c:51
Error codes.
#define EEXIST
File exists.
Definition: errno.h:389
static int cachedhcp_probe(struct net_device *netdev, void *priv __unused)
Apply cached DHCPACK to network device, if applicable.
Definition: cachedhcp.c:337
struct cached_dhcp_packet cached_proxydhcp
Cached ProxyDHCPOFFER.
Definition: cachedhcp.c:69
#define STARTUP_EARLY
Early startup.
Definition: init.h:64
#define DBGC(...)
Definition: compiler.h:505
static void cachedhcp_startup_early(void)
Cached DHCP packet early startup function.
Definition: cachedhcp.c:268
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct net_driver cachedhcp_driver __net_driver
Cached DHCP packet network device driver.
Definition: cachedhcp.c:344
const char * name
Definition: init.h:44
A network upper-layer driver.
Definition: netdevice.h:477
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition: netdevice.h:587
A link-layer protocol.
Definition: netdevice.h:115
#define STARTUP_LATE
Late startup.
Definition: init.h:66
static struct cached_dhcp_packet * cached_packets[]
List of cached DHCP packets.
Definition: cachedhcp.c:79
struct dhcp_packet * dhcppkt
DHCP packet (if any)
Definition: cachedhcp.c:49
void cachedhcp_recycle(struct net_device *netdev)
Recycle cached DHCPACK.
Definition: cachedhcp.c:355
static void cachedhcp_free(struct cached_dhcp_packet *cache)
Free cached DHCP packet.
Definition: cachedhcp.c:93
unsigned long tmp
Definition: linux_pci.h:65
A startup/shutdown function.
Definition: init.h:43
#define ENOMEM
Not enough space.
Definition: errno.h:535
void * memcpy(void *dest, const void *src, size_t len) __nonnull
const char * name
Name.
Definition: netdevice.h:479
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define PROXYDHCP_SETTINGS_NAME
Settings block name used for ProxyDHCP responses.
Definition: dhcp.h:714
Access to external ("user") memory.
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
static int cachedhcp_apply(struct cached_dhcp_packet *cache, struct net_device *netdev)
Apply cached DHCP packet settings.
Definition: cachedhcp.c:106
#define DHCP_SETTINGS_NAME
Settings block name used for DHCP responses.
Definition: dhcp.h:711
ring len
Length.
Definition: dwmac.h:231
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
Cached DHCP packet.
static struct net_device * netdev
Definition: gdbudp.c:52
A cached DHCP packet.
Definition: cachedhcp.c:45
struct startup_fn cachedhcp_early_fn __startup_fn(STARTUP_EARLY)
Cached DHCP packet early startup function.
#define PXEBS_SETTINGS_NAME
Setting block name used for BootServerDHCP responses.
Definition: dhcp.h:717
#define CACHEDHCP_RETAIN
Cached DHCP packet should be retained.
Definition: cachedhcp.c:57
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
uint8_t chaddr[16]
Client hardware address.
Definition: dhcp.h:662
A network device.
Definition: netdevice.h:353
A settings block.
Definition: settings.h:133
unsigned char uint8_t
Definition: stdint.h:10
static void cachedhcp_shutdown(int booting __unused)
Cached DHCP packet shutdown function.
Definition: cachedhcp.c:307
struct cached_dhcp_packet cached_dhcpack
Cached DHCPACK.
Definition: cachedhcp.c:63
unsigned int flags
Flags.
Definition: cachedhcp.c:53
struct settings settings
Settings interface.
Definition: dhcppkt.h:29
A DHCP header.
Definition: dhcp.h:616
Network device management.
void dhcppkt_init(struct dhcp_packet *dhcppkt, struct dhcphdr *data, size_t len)
Initialise DHCP packet.
Definition: dhcppkt.c:301
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition: netdevice.h:363
struct dhcphdr * dhcphdr
The DHCP packet contents.
Definition: dhcppkt.h:25
static struct tlan_private * priv
Definition: tlan.c:225
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
const char * name
Settings block name.
Definition: cachedhcp.c:47
uint8_t data[48]
Additional event data.
Definition: ena.h:22
Virtual LANs.
const char *(* ntoa)(const void *ll_addr)
Transcribe link-layer address.
Definition: netdevice.h:164
DHCP packets.
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:607
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition: settings.c:476
struct cached_dhcp_packet cached_pxebs
Cached PXEBSACK.
Definition: cachedhcp.c:74
uint8_t ll_addr[MAX_LL_ADDR_LEN]
Link-layer address.
Definition: netdevice.h:388
static unsigned int vlan_tag(struct net_device *netdev)
Get the VLAN tag.
Definition: vlan.h:74
struct settings * find_child_settings(struct settings *parent, const char *name)
Find child settings block.
Definition: settings.c:280
#define CACHEDHCP_USED
Cached DHCP packet has been used.
Definition: cachedhcp.c:60
static void cachedhcp_startup_late(void)
Cache DHCP packet late startup function.
Definition: cachedhcp.c:283
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:115
uint8_t hw_addr[MAX_HW_ADDR_LEN]
Hardware address.
Definition: netdevice.h:382
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
struct ll_protocol * ll_protocol
Link-layer protocol.
Definition: netdevice.h:373