iPXE
Macros | Functions | Variables
cachedhcp.c File Reference

Cached DHCP packet. More...

#include <stdint.h>
#include <stdlib.h>
#include <ipxe/dhcppkt.h>
#include <ipxe/init.h>
#include <ipxe/netdevice.h>
#include <realmode.h>
#include <pxe_api.h>

Go to the source code of this file.

Macros

#define cached_dhcpack_phys   __use_data16 ( cached_dhcpack_phys )
 
#define colour   &cached_dhcpack_phys
 Colour for debug messages. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
uint32_t __bss16 (cached_dhcpack_phys)
 Cached DHCPACK physical address. More...
 
static void cachedhcp_init (void)
 Cached DHCPACK startup function. More...
 
static void cachedhcp_startup (void)
 Cached DHCPACK startup function. More...
 
struct init_fn cachedhcp_init_fn __init_fn (INIT_NORMAL)
 Cached DHCPACK initialisation function. More...
 
struct startup_fn cachedhcp_startup_fn __startup_fn (STARTUP_LATE)
 Cached DHCPACK startup function. More...
 
static int cachedhcp_probe (struct net_device *netdev)
 Apply cached DHCPACK to network device, if applicable. More...
 

Variables

static struct dhcp_packetcached_dhcpack
 Cached DHCPACK. More...
 
struct net_driver cachedhcp_driver __net_driver
 Cached DHCP packet network device driver. More...
 

Detailed Description

Cached DHCP packet.

Definition in file cachedhcp.c.

Macro Definition Documentation

◆ cached_dhcpack_phys

#define cached_dhcpack_phys   __use_data16 ( cached_dhcpack_phys )

Definition at line 45 of file cachedhcp.c.

◆ colour

#define colour   &cached_dhcpack_phys

Colour for debug messages.

Definition at line 48 of file cachedhcp.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __bss16()

Cached DHCPACK physical address.

This can be set by the prefix.

◆ cachedhcp_init()

static void cachedhcp_init ( void  )
static

Cached DHCPACK startup function.

Definition at line 57 of file cachedhcp.c.

57  {
58  struct dhcp_packet *dhcppkt;
59  struct dhcp_packet *tmp;
60  struct dhcphdr *dhcphdr;
61  size_t max_len;
62  size_t len;
63 
64  /* Do nothing if no cached DHCPACK is present */
65  if ( ! cached_dhcpack_phys ) {
66  DBGC ( colour, "CACHEDHCP found no cached DHCPACK\n" );
67  return;
68  }
69 
70  /* No reliable way to determine length before parsing packet;
71  * start by assuming maximum length permitted by PXE.
72  */
73  max_len = sizeof ( BOOTPLAYER_t );
74 
75  /* Allocate and populate DHCP packet */
76  dhcppkt = zalloc ( sizeof ( *dhcppkt ) + max_len );
77  if ( ! dhcppkt ) {
78  DBGC ( colour, "CACHEDHCP could not allocate copy\n" );
79  return;
80  }
81  dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
83  max_len );
84  dhcppkt_init ( dhcppkt, dhcphdr, max_len );
85 
86  /* Shrink packet to required length. If reallocation fails,
87  * just continue to use the original packet and waste the
88  * unused space.
89  */
90  len = dhcppkt_len ( dhcppkt );
91  assert ( len <= max_len );
92  tmp = realloc ( dhcppkt, ( sizeof ( *dhcppkt ) + len ) );
93  if ( tmp )
94  dhcppkt = tmp;
95 
96  /* Reinitialise packet at new address */
97  dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
98  dhcppkt_init ( dhcppkt, dhcphdr, len );
99 
100  /* Store as cached DHCPACK, and mark original copy as consumed */
101  DBGC ( colour, "CACHEDHCP found cached DHCPACK at %08x+%zx\n",
103  cached_dhcpack = dhcppkt;
105 }
#define colour
Colour for debug messages.
Definition: cachedhcp.c:48
A DHCP packet.
Definition: dhcppkt.h:20
static struct dhcp_packet * cached_dhcpack
Cached DHCPACK.
Definition: cachedhcp.c:51
static void size_t size_t max_len
Definition: entropy.h:153
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
#define DBGC(...)
Definition: compiler.h:505
userptr_t phys_to_user(unsigned long phys_addr)
Convert physical address to user pointer.
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 bootph BOOTPLAYER_t
Definition: pxe_api.h:406
uint32_t len
Length.
Definition: ena.h:14
#define cached_dhcpack_phys
Definition: cachedhcp.c:45
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:521
static int dhcppkt_len(struct dhcp_packet *dhcppkt)
Get used length of DHCP packet.
Definition: dhcppkt.h:59

References assert(), cached_dhcpack, cached_dhcpack_phys, colour, copy_from_user(), DBGC, dhcppkt_init(), dhcppkt_len(), len, max_len, phys_to_user(), realloc(), tmp, and zalloc().

◆ cachedhcp_startup()

static void cachedhcp_startup ( void  )
static

Cached DHCPACK startup function.

Definition at line 111 of file cachedhcp.c.

111  {
112 
113  /* If cached DHCP packet was not claimed by any network device
114  * during startup, then free it.
115  */
116  if ( cached_dhcpack ) {
117  DBGC ( colour, "CACHEDHCP freeing unclaimed cached DHCPACK\n" );
120  }
121 }
#define colour
Colour for debug messages.
Definition: cachedhcp.c:48
static struct dhcp_packet * cached_dhcpack
Cached DHCPACK.
Definition: cachedhcp.c:51
#define DBGC(...)
Definition: compiler.h:505
static void dhcppkt_put(struct dhcp_packet *dhcppkt)
Decrement reference count on DHCP packet.
Definition: dhcppkt.h:49
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References cached_dhcpack, colour, DBGC, dhcppkt_put(), and NULL.

◆ __init_fn()

struct init_fn cachedhcp_init_fn __init_fn ( INIT_NORMAL  )

Cached DHCPACK initialisation function.

◆ __startup_fn()

struct startup_fn cachedhcp_startup_fn __startup_fn ( STARTUP_LATE  )

Cached DHCPACK startup function.

◆ cachedhcp_probe()

static int cachedhcp_probe ( struct net_device netdev)
static

Apply cached DHCPACK to network device, if applicable.

Parameters
netdevNetwork device
Return values
rcReturn status code

Definition at line 140 of file cachedhcp.c.

140  {
142  int rc;
143 
144  /* Do nothing unless we have a cached DHCPACK */
145  if ( ! cached_dhcpack )
146  return 0;
147 
148  /* Do nothing unless cached DHCPACK's MAC address matches this
149  * network device.
150  */
152  ll_protocol->ll_addr_len ) != 0 ) {
153  DBGC ( colour, "CACHEDHCP cached DHCPACK does not match %s\n",
154  netdev->name );
155  return 0;
156  }
157  DBGC ( colour, "CACHEDHCP cached DHCPACK is for %s\n", netdev->name );
158 
159  /* Register as DHCP settings for this network device */
162  DHCP_SETTINGS_NAME ) ) != 0 ) {
163  DBGC ( colour, "CACHEDHCP could not register settings: %s\n",
164  strerror ( rc ) );
165  return rc;
166  }
167 
168  /* Claim cached DHCPACK */
171 
172  return 0;
173 }
#define colour
Colour for debug messages.
Definition: cachedhcp.c:48
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static struct dhcp_packet * cached_dhcpack
Cached DHCPACK.
Definition: cachedhcp.c:51
uint8_t ll_addr_len
Link-layer address length.
Definition: netdevice.h:198
#define DBGC(...)
Definition: compiler.h:505
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition: netdevice.h:577
A link-layer protocol.
Definition: netdevice.h:114
#define DHCP_SETTINGS_NAME
Settings block name used for DHCP responses.
Definition: dhcp.h:686
static struct net_device * netdev
Definition: gdbudp.c:52
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
uint8_t chaddr[16]
Client hardware address.
Definition: dhcp.h:637
struct settings settings
Settings interface.
Definition: dhcppkt.h:28
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition: netdevice.h:358
struct dhcphdr * dhcphdr
The DHCP packet contents.
Definition: dhcppkt.h:24
static void dhcppkt_put(struct dhcp_packet *dhcppkt)
Decrement reference count on DHCP packet.
Definition: dhcppkt.h:49
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition: settings.c:473
uint8_t ll_addr[MAX_LL_ADDR_LEN]
Link-layer address.
Definition: netdevice.h:381
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:98
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct ll_protocol * ll_protocol
Link-layer protocol.
Definition: netdevice.h:366

References cached_dhcpack, dhcphdr::chaddr, colour, DBGC, DHCP_SETTINGS_NAME, dhcp_packet::dhcphdr, dhcppkt_put(), net_device::ll_addr, ll_protocol::ll_addr_len, net_device::ll_protocol, memcmp(), net_device::name, netdev, netdev_settings(), NULL, rc, register_settings(), dhcp_packet::settings, and strerror().

Variable Documentation

◆ cached_dhcpack

struct dhcp_packet* cached_dhcpack
static

Cached DHCPACK.

Definition at line 51 of file cachedhcp.c.

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

◆ __net_driver

struct net_driver cachedhcp_driver __net_driver
Initial value:
= {
.name = "cachedhcp",
.probe = cachedhcp_probe,
}
static int cachedhcp_probe(struct net_device *netdev)
Apply cached DHCPACK to network device, if applicable.
Definition: cachedhcp.c:140

Cached DHCP packet network device driver.

Definition at line 176 of file cachedhcp.c.