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/cachedhcp.h>
33 
34 /** @file
35  *
36  * Cached DHCP packet
37  *
38  */
39 
40 /** A cached DHCP packet */
42  /** Settings block name */
43  const char *name;
44  /** DHCP packet (if any) */
46 };
47 
48 /** Cached DHCPACK */
51 };
52 
53 /** Cached ProxyDHCPOFFER */
56 };
57 
58 /** Cached PXEBSACK */
61 };
62 
63 /** List of cached DHCP packets */
64 static struct cached_dhcp_packet *cached_packets[] = {
67  &cached_pxebs,
68 };
69 
70 /** Colour for debug messages */
71 #define colour &cached_dhcpack
72 
73 /**
74  * Free cached DHCP packet
75  *
76  * @v cache Cached DHCP packet
77  */
78 static void cachedhcp_free ( struct cached_dhcp_packet *cache ) {
79 
80  dhcppkt_put ( cache->dhcppkt );
81  cache->dhcppkt = NULL;
82 }
83 
84 /**
85  * Apply cached DHCP packet settings
86  *
87  * @v cache Cached DHCP packet
88  * @v netdev Network device, or NULL
89  * @ret rc Return status code
90  */
91 static int cachedhcp_apply ( struct cached_dhcp_packet *cache,
92  struct net_device *netdev ) {
93  struct settings *settings;
94  int rc;
95 
96  /* Do nothing if cache is empty */
97  if ( ! cache->dhcppkt )
98  return 0;
99 
100  /* Do nothing unless cached packet's MAC address matches this
101  * network device, if specified.
102  */
103  if ( netdev ) {
104  if ( memcmp ( netdev->ll_addr, cache->dhcppkt->dhcphdr->chaddr,
105  netdev->ll_protocol->ll_addr_len ) != 0 ) {
106  DBGC ( colour, "CACHEDHCP %s does not match %s\n",
107  cache->name, netdev->name );
108  return 0;
109  }
110  DBGC ( colour, "CACHEDHCP %s is for %s\n",
111  cache->name, netdev->name );
112  }
113 
114  /* Select appropriate parent settings block */
115  settings = ( netdev ? netdev_settings ( netdev ) : NULL );
116 
117  /* Register settings */
118  if ( ( rc = register_settings ( &cache->dhcppkt->settings, settings,
119  cache->name ) ) != 0 ) {
120  DBGC ( colour, "CACHEDHCP %s could not register settings: %s\n",
121  cache->name, strerror ( rc ) );
122  return rc;
123  }
124 
125  /* Free cached DHCP packet */
126  cachedhcp_free ( cache );
127 
128  return 0;
129 }
130 
131 /**
132  * Record cached DHCP packet
133  *
134  * @v cache Cached DHCP packet
135  * @v data DHCPACK packet buffer
136  * @v max_len Maximum possible length
137  * @ret rc Return status code
138  */
140  size_t max_len ) {
141  struct dhcp_packet *dhcppkt;
142  struct dhcp_packet *tmp;
143  struct dhcphdr *dhcphdr;
144  unsigned int i;
145  size_t len;
146 
147  /* Free any existing cached packet */
148  cachedhcp_free ( cache );
149 
150  /* Allocate and populate DHCP packet */
151  dhcppkt = zalloc ( sizeof ( *dhcppkt ) + max_len );
152  if ( ! dhcppkt ) {
153  DBGC ( colour, "CACHEDHCP %s could not allocate copy\n",
154  cache->name );
155  return -ENOMEM;
156  }
157  dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
159  dhcppkt_init ( dhcppkt, dhcphdr, max_len );
160 
161  /* Shrink packet to required length. If reallocation fails,
162  * just continue to use the original packet and waste the
163  * unused space.
164  */
165  len = dhcppkt_len ( dhcppkt );
166  assert ( len <= max_len );
167  tmp = realloc ( dhcppkt, ( sizeof ( *dhcppkt ) + len ) );
168  if ( tmp )
169  dhcppkt = tmp;
170 
171  /* Reinitialise packet at new address */
172  dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
173  dhcppkt_init ( dhcppkt, dhcphdr, len );
174 
175  /* Discard duplicate packets, since some PXE stacks (including
176  * iPXE itself) will report the DHCPACK packet as the PXEBSACK
177  * if no separate PXEBSACK exists.
178  */
179  for ( i = 0 ; i < ( sizeof ( cached_packets ) /
180  sizeof ( cached_packets[0] ) ) ; i++ ) {
181  tmp = cached_packets[i]->dhcppkt;
182  if ( tmp && ( dhcppkt_len ( tmp ) == len ) &&
183  ( memcmp ( tmp->dhcphdr, dhcppkt->dhcphdr, len ) == 0 ) ) {
184  DBGC ( colour, "CACHEDHCP %s duplicates %s\n",
185  cache->name, cached_packets[i]->name );
186  dhcppkt_put ( dhcppkt );
187  return -EEXIST;
188  }
189  }
190 
191  /* Store as cached packet */
192  DBGC ( colour, "CACHEDHCP %s at %#08lx+%#zx/%#zx\n", cache->name,
193  user_to_phys ( data, 0 ), len, max_len );
194  cache->dhcppkt = dhcppkt;
195 
196  return 0;
197 }
198 
199 /**
200  * Cached DHCPACK startup function
201  *
202  */
203 static void cachedhcp_startup ( void ) {
204 
205  /* Apply cached ProxyDHCPOFFER, if any */
207 
208  /* Apply cached PXEBSACK, if any */
210 
211  /* Free any remaining cached packets */
212  if ( cached_dhcpack.dhcppkt ) {
213  DBGC ( colour, "CACHEDHCP %s unclaimed\n",
215  }
219 }
220 
221 /** Cached DHCPACK startup function */
222 struct startup_fn cachedhcp_startup_fn __startup_fn ( STARTUP_LATE ) = {
223  .name = "cachedhcp",
224  .startup = cachedhcp_startup,
225 };
226 
227 /**
228  * Apply cached DHCPACK to network device, if applicable
229  *
230  * @v netdev Network device
231  * @ret rc Return status code
232  */
233 static int cachedhcp_probe ( struct net_device *netdev ) {
234 
235  /* Apply cached DHCPACK to network device, if applicable */
237 }
238 
239 /** Cached DHCP packet network device driver */
240 struct net_driver cachedhcp_driver __net_driver = {
241  .name = "cachedhcp",
242  .probe = cachedhcp_probe,
243 };
#define colour
Colour for debug messages.
Definition: cachedhcp.c:71
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
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:54
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 DHCPACK startup function.
Definition: cachedhcp.c:203
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct net_driver cachedhcp_driver __net_driver
Cached DHCP packet network device driver.
Definition: cachedhcp.c:240
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
#define STARTUP_LATE
Late startup.
Definition: init.h:64
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
A startup/shutdown function.
Definition: init.h:41
#define ENOMEM
Not enough space.
Definition: errno.h:534
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:91
#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:41
#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
A network device.
Definition: netdevice.h:352
A settings block.
Definition: settings.h:132
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:49
struct settings settings
Settings interface.
Definition: dhcppkt.h:28
A DHCP header.
Definition: dhcp.h:591
Network device management.
static int cachedhcp_probe(struct net_device *netdev)
Apply cached DHCPACK to network device, if applicable.
Definition: cachedhcp.c:233
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:43
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:478
struct cached_dhcp_packet cached_pxebs
Cached PXEBSACK.
Definition: cachedhcp.c:59
int cachedhcp_record(struct cached_dhcp_packet *cache, userptr_t data, size_t max_len)
Record cached DHCP packet.
Definition: cachedhcp.c:139
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
uint8_t ll_addr[MAX_LL_ADDR_LEN]
Link-layer address.
Definition: netdevice.h:387
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
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