iPXE
Functions | Variables
cachedhcp.h File Reference

Cached DHCP packet. More...

#include <stddef.h>
#include <ipxe/uaccess.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
int cachedhcp_record (struct cached_dhcp_packet *cache, userptr_t data, size_t max_len)
 Record cached DHCP packet. More...
 

Variables

struct cached_dhcp_packet cached_dhcpack
 Cached DHCPACK. More...
 
struct cached_dhcp_packet cached_proxydhcp
 Cached ProxyDHCPOFFER. More...
 
struct cached_dhcp_packet cached_pxebs
 Cached PXEBSACK. More...
 

Detailed Description

Cached DHCP packet.

Definition in file cachedhcp.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ cachedhcp_record()

int cachedhcp_record ( struct cached_dhcp_packet cache,
userptr_t  data,
size_t  max_len 
)

Record cached DHCP packet.

Parameters
cacheCached DHCP packet
dataDHCPACK packet buffer
max_lenMaximum possible length
Return values
rcReturn status code

Definition at line 172 of file cachedhcp.c.

173  {
174  struct dhcp_packet *dhcppkt;
175  struct dhcp_packet *tmp;
176  struct dhcphdr *dhcphdr;
177  unsigned int i;
178  size_t len;
179 
180  /* Free any existing cached packet */
181  cachedhcp_free ( cache );
182 
183  /* Allocate and populate DHCP packet */
184  dhcppkt = zalloc ( sizeof ( *dhcppkt ) + max_len );
185  if ( ! dhcppkt ) {
186  DBGC ( colour, "CACHEDHCP %s could not allocate copy\n",
187  cache->name );
188  return -ENOMEM;
189  }
190  dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
192  dhcppkt_init ( dhcppkt, dhcphdr, max_len );
193 
194  /* Shrink packet to required length. If reallocation fails,
195  * just continue to use the original packet and waste the
196  * unused space.
197  */
198  len = dhcppkt_len ( dhcppkt );
199  assert ( len <= max_len );
200  tmp = realloc ( dhcppkt, ( sizeof ( *dhcppkt ) + len ) );
201  if ( tmp )
202  dhcppkt = tmp;
203 
204  /* Reinitialise packet at new address */
205  dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
206  dhcppkt_init ( dhcppkt, dhcphdr, len );
207 
208  /* Discard duplicate packets, since some PXE stacks (including
209  * iPXE itself) will report the DHCPACK packet as the PXEBSACK
210  * if no separate PXEBSACK exists.
211  */
212  for ( i = 0 ; i < ( sizeof ( cached_packets ) /
213  sizeof ( cached_packets[0] ) ) ; i++ ) {
214  tmp = cached_packets[i]->dhcppkt;
215  if ( tmp && ( dhcppkt_len ( tmp ) == len ) &&
216  ( memcmp ( tmp->dhcphdr, dhcppkt->dhcphdr, len ) == 0 ) ) {
217  DBGC ( colour, "CACHEDHCP %s duplicates %s\n",
218  cache->name, cached_packets[i]->name );
219  dhcppkt_put ( dhcppkt );
220  return -EEXIST;
221  }
222  }
223 
224  /* Store as cached packet */
225  DBGC ( colour, "CACHEDHCP %s at %#08lx+%#zx/%#zx\n", cache->name,
226  user_to_phys ( data, 0 ), len, max_len );
227  cache->dhcppkt = dhcppkt;
228 
229  return 0;
230 }
#define colour
Colour for debug messages.
Definition: cachedhcp.c:71
A DHCP packet.
Definition: dhcppkt.h:20
static void size_t size_t max_len
Definition: entropy.h:153
#define EEXIST
File exists.
Definition: errno.h:388
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
unsigned long user_to_phys(userptr_t userptr, off_t offset)
Convert user pointer to physical address.
#define DBGC(...)
Definition: compiler.h:505
static struct cached_dhcp_packet * cached_packets[]
List of cached DHCP packets.
Definition: cachedhcp.c:64
struct dhcp_packet * dhcppkt
DHCP packet (if any)
Definition: cachedhcp.c:45
static void cachedhcp_free(struct cached_dhcp_packet *cache)
Free cached DHCP packet.
Definition: cachedhcp.c:78
#define ENOMEM
Not enough space.
Definition: errno.h:534
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
uint8_t * tmp
Definition: entropy.h:156
A DHCP header.
Definition: dhcp.h:591
void dhcppkt_init(struct dhcp_packet *dhcppkt, struct dhcphdr *data, size_t len)
Initialise DHCP packet.
Definition: dhcppkt.c:300
struct dhcphdr * dhcphdr
The DHCP packet contents.
Definition: dhcppkt.h:24
uint32_t len
Length.
Definition: ena.h:14
static void dhcppkt_put(struct dhcp_packet *dhcppkt)
Decrement reference count on DHCP packet.
Definition: dhcppkt.h:49
static size_t dhcppkt_len(struct dhcp_packet *dhcppkt)
Get used length of DHCP packet.
Definition: dhcppkt.h:59
const char * name
Settings block name.
Definition: cachedhcp.c:43
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:521
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114

References assert(), cached_packets, cachedhcp_free(), colour, copy_from_user(), data, DBGC, dhcp_packet::dhcphdr, cached_dhcp_packet::dhcppkt, dhcppkt_init(), dhcppkt_len(), dhcppkt_put(), EEXIST, ENOMEM, len, max_len, memcmp(), cached_dhcp_packet::name, realloc(), tmp, user_to_phys(), and zalloc().

Referenced by cachedhcp_init(), and efi_cachedhcp_record().

Variable Documentation

◆ cached_dhcpack

struct cached_dhcp_packet cached_dhcpack

Cached DHCPACK.

Definition at line 49 of file cachedhcp.c.

Referenced by cachedhcp_init(), cachedhcp_probe(), cachedhcp_startup(), and efi_cachedhcp_record().

◆ cached_proxydhcp

struct cached_dhcp_packet cached_proxydhcp

Cached ProxyDHCPOFFER.

Definition at line 54 of file cachedhcp.c.

Referenced by cachedhcp_startup(), and efi_cachedhcp_record().

◆ cached_pxebs

struct cached_dhcp_packet cached_pxebs

Cached PXEBSACK.

Definition at line 59 of file cachedhcp.c.

Referenced by cachedhcp_startup(), and efi_cachedhcp_record().