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 
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <ipxe/dhcppkt.h>
30 #include <ipxe/init.h>
31 #include <ipxe/netdevice.h>
32 #include <ipxe/vlan.h>
33 #include <ipxe/cachedhcp.h>
34 
35 /** @file
36  *
37  * Cached DHCP packet
38  *
39  */
40 
41 /** A cached DHCP packet */
43  /** Settings block name */
44  const char *name;
45  /** DHCP packet (if any) */
47  /** VLAN tag (if applicable) */
48  unsigned int vlan;
49 };
50 
51 /** Cached DHCPACK */
54 };
55 
56 /** Cached ProxyDHCPOFFER */
59 };
60 
61 /** Cached PXEBSACK */
64 };
65 
66 /** List of cached DHCP packets */
67 static struct cached_dhcp_packet *cached_packets[] = {
70  &cached_pxebs,
71 };
72 
73 /** Colour for debug messages */
74 #define colour &cached_dhcpack
75 
76 /**
77  * Free cached DHCP packet
78  *
79  * @v cache Cached DHCP packet
80  */
81 static void cachedhcp_free ( struct cached_dhcp_packet *cache ) {
82 
83  dhcppkt_put ( cache->dhcppkt );
84  cache->dhcppkt = NULL;
85 }
86 
87 /**
88  * Apply cached DHCP packet settings
89  *
90  * @v cache Cached DHCP packet
91  * @v netdev Network device, or NULL
92  * @ret rc Return status code
93  */
94 static int cachedhcp_apply ( struct cached_dhcp_packet *cache,
95  struct net_device *netdev ) {
96  struct settings *settings = NULL;
97  struct ll_protocol *ll_protocol;
98  const uint8_t *chaddr;
99  uint8_t *hw_addr;
100  uint8_t *ll_addr;
101  size_t ll_addr_len;
102  int rc;
103 
104  /* Do nothing if cache is empty */
105  if ( ! cache->dhcppkt )
106  return 0;
107  chaddr = cache->dhcppkt->dhcphdr->chaddr;
108 
109  /* Handle association with network device, if specified */
110  if ( netdev ) {
111  hw_addr = netdev->hw_addr;
112  ll_addr = netdev->ll_addr;
115 
116  /* If cached packet's MAC address matches the network
117  * device's permanent MAC address, then assume that
118  * the permanent MAC address ought to be the network
119  * device's current link-layer address.
120  *
121  * This situation can arise when the PXE ROM does not
122  * understand the system-specific mechanism for
123  * overriding the MAC address, and so uses the
124  * permanent MAC address instead. We choose to match
125  * this behaviour in order to minimise surprise.
126  */
127  if ( memcmp ( hw_addr, chaddr, ll_addr_len ) == 0 ) {
128  if ( memcmp ( hw_addr, ll_addr, ll_addr_len ) != 0 ) {
129  DBGC ( colour, "CACHEDHCP %s resetting %s MAC "
130  "%s ", cache->name, netdev->name,
131  ll_protocol->ntoa ( ll_addr ) );
132  DBGC ( colour, "-> %s\n",
133  ll_protocol->ntoa ( hw_addr ) );
134  }
135  memcpy ( ll_addr, hw_addr, ll_addr_len );
136  }
137 
138  /* Do nothing unless cached packet's MAC address
139  * matches this network device.
140  */
141  if ( memcmp ( ll_addr, chaddr, ll_addr_len ) != 0 ) {
142  DBGC ( colour, "CACHEDHCP %s %s does not match %s\n",
143  cache->name, ll_protocol->ntoa ( chaddr ),
144  netdev->name );
145  return 0;
146  }
147 
148  /* Do nothing unless cached packet's VLAN tag matches
149  * this network device.
150  */
151  if ( vlan_tag ( netdev ) != cache->vlan ) {
152  DBGC ( colour, "CACHEDHCP %s VLAN %d does not match "
153  "%s\n", cache->name, cache->vlan,
154  netdev->name );
155  return 0;
156  }
157 
158  /* Use network device's settings block */
160  DBGC ( colour, "CACHEDHCP %s is for %s\n",
161  cache->name, netdev->name );
162  }
163 
164  /* Register settings */
165  if ( ( rc = register_settings ( &cache->dhcppkt->settings, settings,
166  cache->name ) ) != 0 ) {
167  DBGC ( colour, "CACHEDHCP %s could not register settings: %s\n",
168  cache->name, strerror ( rc ) );
169  return rc;
170  }
171 
172  /* Free cached DHCP packet */
173  cachedhcp_free ( cache );
174 
175  return 0;
176 }
177 
178 /**
179  * Record cached DHCP packet
180  *
181  * @v cache Cached DHCP packet
182  * @v vlan VLAN tag, if any
183  * @v data DHCPACK packet buffer
184  * @v max_len Maximum possible length
185  * @ret rc Return status code
186  */
187 int cachedhcp_record ( struct cached_dhcp_packet *cache, unsigned int vlan,
188  userptr_t data, size_t max_len ) {
189  struct dhcp_packet *dhcppkt;
190  struct dhcp_packet *tmp;
191  struct dhcphdr *dhcphdr;
192  unsigned int i;
193  size_t len;
194 
195  /* Free any existing cached packet */
196  cachedhcp_free ( cache );
197 
198  /* Allocate and populate DHCP packet */
199  dhcppkt = zalloc ( sizeof ( *dhcppkt ) + max_len );
200  if ( ! dhcppkt ) {
201  DBGC ( colour, "CACHEDHCP %s could not allocate copy\n",
202  cache->name );
203  return -ENOMEM;
204  }
205  dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
207  dhcppkt_init ( dhcppkt, dhcphdr, max_len );
208 
209  /* Shrink packet to required length. If reallocation fails,
210  * just continue to use the original packet and waste the
211  * unused space.
212  */
213  len = dhcppkt_len ( dhcppkt );
214  assert ( len <= max_len );
215  tmp = realloc ( dhcppkt, ( sizeof ( *dhcppkt ) + len ) );
216  if ( tmp )
217  dhcppkt = tmp;
218 
219  /* Reinitialise packet at new address */
220  dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
221  dhcppkt_init ( dhcppkt, dhcphdr, len );
222 
223  /* Discard duplicate packets, since some PXE stacks (including
224  * iPXE itself) will report the DHCPACK packet as the PXEBSACK
225  * if no separate PXEBSACK exists.
226  */
227  for ( i = 0 ; i < ( sizeof ( cached_packets ) /
228  sizeof ( cached_packets[0] ) ) ; i++ ) {
229  tmp = cached_packets[i]->dhcppkt;
230  if ( tmp && ( dhcppkt_len ( tmp ) == len ) &&
231  ( memcmp ( tmp->dhcphdr, dhcppkt->dhcphdr, len ) == 0 ) ) {
232  DBGC ( colour, "CACHEDHCP %s duplicates %s\n",
233  cache->name, cached_packets[i]->name );
234  dhcppkt_put ( dhcppkt );
235  return -EEXIST;
236  }
237  }
238 
239  /* Store as cached packet */
240  DBGC ( colour, "CACHEDHCP %s at %#08lx+%#zx/%#zx\n", cache->name,
241  user_to_phys ( data, 0 ), len, max_len );
242  cache->dhcppkt = dhcppkt;
243  cache->vlan = vlan;
244 
245  return 0;
246 }
247 
248 /**
249  * Cached DHCP packet startup function
250  *
251  */
252 static void cachedhcp_startup ( void ) {
253 
254  /* Apply cached ProxyDHCPOFFER, if any */
257 
258  /* Apply cached PXEBSACK, if any */
261 
262  /* Report unclaimed DHCPACK, if any. Do not free yet, since
263  * it may still be claimed by a dynamically created device
264  * such as a VLAN device.
265  */
266  if ( cached_dhcpack.dhcppkt ) {
267  DBGC ( colour, "CACHEDHCP %s unclaimed\n",
269  }
270 }
271 
272 /**
273  * Cached DHCP packet shutdown function
274  *
275  * @v booting System is shutting down for OS boot
276  */
277 static void cachedhcp_shutdown ( int booting __unused ) {
278 
279  /* Free cached DHCPACK, if any */
280  if ( cached_dhcpack.dhcppkt ) {
281  DBGC ( colour, "CACHEDHCP %s never claimed\n",
283  }
285 }
286 
287 /** Cached DHCPACK startup function */
288 struct startup_fn cachedhcp_startup_fn __startup_fn ( STARTUP_LATE ) = {
289  .name = "cachedhcp",
290  .startup = cachedhcp_startup,
291  .shutdown = cachedhcp_shutdown,
292 };
293 
294 /**
295  * Apply cached DHCPACK to network device, if applicable
296  *
297  * @v netdev Network device
298  * @ret rc Return status code
299  */
300 static int cachedhcp_probe ( struct net_device *netdev ) {
301 
302  /* Apply cached DHCPACK to network device, if applicable */
304 }
305 
306 /** Cached DHCP packet network device driver */
307 struct net_driver cachedhcp_driver __net_driver = {
308  .name = "cachedhcp",
309  .probe = cachedhcp_probe,
310 };
#define colour
Colour for debug messages.
Definition: cachedhcp.c:74
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
A DHCP packet.
Definition: dhcppkt.h:20
uint8_t ll_addr_len
Link-layer address length.
Definition: netdevice.h:198
unsigned int vlan
VLAN tag (if applicable)
Definition: cachedhcp.c:48
static void size_t size_t max_len
Definition: entropy.h:153
Error codes.
#define EEXIST
File exists.
Definition: errno.h:388
struct cached_dhcp_packet cached_proxydhcp
Cached ProxyDHCPOFFER.
Definition: cachedhcp.c:57
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 void cachedhcp_startup(void)
Cached DHCP packet startup function.
Definition: cachedhcp.c:252
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct net_driver cachedhcp_driver __net_driver
Cached DHCP packet network device driver.
Definition: cachedhcp.c:307
const char * name
Definition: init.h:42
A network upper-layer driver.
Definition: netdevice.h:473
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition: netdevice.h:589
A link-layer protocol.
Definition: netdevice.h:114
#define STARTUP_LATE
Late startup.
Definition: init.h:64
static struct cached_dhcp_packet * cached_packets[]
List of cached DHCP packets.
Definition: cachedhcp.c:67
struct dhcp_packet * dhcppkt
DHCP packet (if any)
Definition: cachedhcp.c:46
static void cachedhcp_free(struct cached_dhcp_packet *cache)
Free cached DHCP packet.
Definition: cachedhcp.c:81
A startup/shutdown function.
Definition: init.h:41
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
const char * name
Name.
Definition: netdevice.h:475
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define PROXYDHCP_SETTINGS_NAME
Settings block name used for ProxyDHCP responses.
Definition: dhcp.h:689
static int cachedhcp_apply(struct cached_dhcp_packet *cache, struct net_device *netdev)
Apply cached DHCP packet settings.
Definition: cachedhcp.c:94
#define DHCP_SETTINGS_NAME
Settings block name used for DHCP responses.
Definition: dhcp.h:686
Cached DHCP packet.
static struct net_device * netdev
Definition: gdbudp.c:52
A cached DHCP packet.
Definition: cachedhcp.c:42
#define PXEBS_SETTINGS_NAME
Setting block name used for BootServerDHCP responses.
Definition: dhcp.h:692
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
uint8_t chaddr[16]
Client hardware address.
Definition: dhcp.h:637
uint8_t * tmp
Definition: entropy.h:156
int cachedhcp_record(struct cached_dhcp_packet *cache, unsigned int vlan, userptr_t data, size_t max_len)
Record cached DHCP packet.
Definition: cachedhcp.c:187
A network device.
Definition: netdevice.h:352
A settings block.
Definition: settings.h:132
unsigned char uint8_t
Definition: stdint.h:10
static void cachedhcp_shutdown(int booting __unused)
Cached DHCP packet shutdown function.
Definition: cachedhcp.c:277
struct startup_fn cachedhcp_startup_fn __startup_fn(STARTUP_LATE)
Cached DHCPACK startup function.
struct cached_dhcp_packet cached_dhcpack
Cached DHCPACK.
Definition: cachedhcp.c:52
struct settings settings
Settings interface.
Definition: dhcppkt.h:28
A DHCP header.
Definition: dhcp.h:591
Network device management.
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
static int cachedhcp_probe(struct net_device *netdev)
Apply cached DHCPACK to network device, if applicable.
Definition: cachedhcp.c:300
void dhcppkt_init(struct dhcp_packet *dhcppkt, struct dhcphdr *data, size_t len)
Initialise DHCP packet.
Definition: dhcppkt.c:300
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition: netdevice.h:362
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:44
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:163
DHCP packets.
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:521
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition: settings.c:475
struct cached_dhcp_packet cached_pxebs
Cached PXEBSACK.
Definition: cachedhcp.c:62
uint8_t ll_addr[MAX_LL_ADDR_LEN]
Link-layer address.
Definition: netdevice.h:387
static unsigned int vlan_tag(struct net_device *netdev)
Get the VLAN tag.
Definition: vlan.h:73
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
uint8_t hw_addr[MAX_HW_ADDR_LEN]
Hardware address.
Definition: netdevice.h:381
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
struct ll_protocol * ll_protocol
Link-layer protocol.
Definition: netdevice.h:372
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33