iPXE
Defines | 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.

Defines

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

Functions

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

Variables

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

Detailed Description

Cached DHCP packet.

Definition in file cachedhcp.c.


Define Documentation

Definition at line 45 of file cachedhcp.c.

Referenced by cachedhcp_init().

#define colour   &cached_dhcpack_phys

Colour for debug messages.

Definition at line 48 of file cachedhcp.c.

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


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

Cached DHCPACK physical address.

This can be set by the prefix.

static void cachedhcp_init ( void  ) [static]

Cached DHCPACK startup function.

Definition at line 57 of file cachedhcp.c.

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

                                    {
        struct dhcp_packet *dhcppkt;
        struct dhcp_packet *tmp;
        struct dhcphdr *dhcphdr;
        size_t max_len;
        size_t len;

        /* Do nothing if no cached DHCPACK is present */
        if ( ! cached_dhcpack_phys ) {
                DBGC ( colour, "CACHEDHCP found no cached DHCPACK\n" );
                return;
        }

        /* No reliable way to determine length before parsing packet;
         * start by assuming maximum length permitted by PXE.
         */
        max_len = sizeof ( BOOTPLAYER_t );

        /* Allocate and populate DHCP packet */
        dhcppkt = zalloc ( sizeof ( *dhcppkt ) + max_len );
        if ( ! dhcppkt ) {
                DBGC ( colour, "CACHEDHCP could not allocate copy\n" );
                return;
        }
        dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
        copy_from_user ( dhcphdr, phys_to_user ( cached_dhcpack_phys ), 0,
                         max_len );
        dhcppkt_init ( dhcppkt, dhcphdr, max_len );

        /* Shrink packet to required length.  If reallocation fails,
         * just continue to use the original packet and waste the
         * unused space.
         */
        len = dhcppkt_len ( dhcppkt );
        assert ( len <= max_len );
        tmp = realloc ( dhcppkt, ( sizeof ( *dhcppkt ) + len ) );
        if ( tmp )
                dhcppkt = tmp;

        /* Reinitialise packet at new address */
        dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
        dhcppkt_init ( dhcppkt, dhcphdr, len );

        /* Store as cached DHCPACK, and mark original copy as consumed */
        DBGC ( colour, "CACHEDHCP found cached DHCPACK at %08x+%zx\n",
               cached_dhcpack_phys, len );
        cached_dhcpack = dhcppkt;
        cached_dhcpack_phys = 0;
}
static void cachedhcp_startup ( void  ) [static]

Cached DHCPACK startup function.

Definition at line 111 of file cachedhcp.c.

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

                                       {

        /* If cached DHCP packet was not claimed by any network device
         * during startup, then free it.
         */
        if ( cached_dhcpack ) {
                DBGC ( colour, "CACHEDHCP freeing unclaimed cached DHCPACK\n" );
                dhcppkt_put ( cached_dhcpack );
                cached_dhcpack = NULL;
        }
}
struct init_fn cachedhcp_init_fn __init_fn ( INIT_NORMAL  ) [read]

Cached DHCPACK initialisation function.

struct startup_fn cachedhcp_startup_fn __startup_fn ( STARTUP_LATE  ) [read]

Cached DHCPACK startup function.

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 139 of file cachedhcp.c.

References 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_settings(), NULL, rc, register_settings(), dhcp_packet::settings, and strerror().

                                                         {
        struct ll_protocol *ll_protocol = netdev->ll_protocol;
        int rc;

        /* Do nothing unless we have a cached DHCPACK */
        if ( ! cached_dhcpack )
                return 0;

        /* Do nothing unless cached DHCPACK's MAC address matches this
         * network device.
         */
        if ( memcmp ( netdev->ll_addr, cached_dhcpack->dhcphdr->chaddr,
                      ll_protocol->ll_addr_len ) != 0 ) {
                DBGC ( colour, "CACHEDHCP cached DHCPACK does not match %s\n",
                       netdev->name );
                return 0;
        }
        DBGC ( colour, "CACHEDHCP cached DHCPACK is for %s\n", netdev->name );

        /* Register as DHCP settings for this network device */
        if ( ( rc = register_settings ( &cached_dhcpack->settings,
                                        netdev_settings ( netdev ),
                                        DHCP_SETTINGS_NAME ) ) != 0 ) {
                DBGC ( colour, "CACHEDHCP could not register settings: %s\n",
                       strerror ( rc ) );
                return rc;
        }

        /* Claim cached DHCPACK */
        dhcppkt_put ( cached_dhcpack );
        cached_dhcpack = NULL;

        return 0;
}

Variable Documentation

struct dhcp_packet* cached_dhcpack [static]

Cached DHCPACK.

Definition at line 51 of file cachedhcp.c.

struct net_driver cachedhcp_driver __net_driver
Initial value:
 {
        .name = "cachedhcp",
        .probe = cachedhcp_probe,
}

Cached DHCP packet network device driver.

Definition at line 175 of file cachedhcp.c.