iPXE
cachedhcp.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <stdlib.h>
00028 #include <ipxe/dhcppkt.h>
00029 #include <ipxe/init.h>
00030 #include <ipxe/netdevice.h>
00031 #include <realmode.h>
00032 #include <pxe_api.h>
00033 
00034 /** @file
00035  *
00036  * Cached DHCP packet
00037  *
00038  */
00039 
00040 /** Cached DHCPACK physical address
00041  *
00042  * This can be set by the prefix.
00043  */
00044 uint32_t __bss16 ( cached_dhcpack_phys );
00045 #define cached_dhcpack_phys __use_data16 ( cached_dhcpack_phys )
00046 
00047 /** Colour for debug messages */
00048 #define colour &cached_dhcpack_phys
00049 
00050 /** Cached DHCPACK */
00051 static struct dhcp_packet *cached_dhcpack;
00052 
00053 /**
00054  * Cached DHCPACK startup function
00055  *
00056  */
00057 static void cachedhcp_init ( void ) {
00058         struct dhcp_packet *dhcppkt;
00059         struct dhcp_packet *tmp;
00060         struct dhcphdr *dhcphdr;
00061         size_t max_len;
00062         size_t len;
00063 
00064         /* Do nothing if no cached DHCPACK is present */
00065         if ( ! cached_dhcpack_phys ) {
00066                 DBGC ( colour, "CACHEDHCP found no cached DHCPACK\n" );
00067                 return;
00068         }
00069 
00070         /* No reliable way to determine length before parsing packet;
00071          * start by assuming maximum length permitted by PXE.
00072          */
00073         max_len = sizeof ( BOOTPLAYER_t );
00074 
00075         /* Allocate and populate DHCP packet */
00076         dhcppkt = zalloc ( sizeof ( *dhcppkt ) + max_len );
00077         if ( ! dhcppkt ) {
00078                 DBGC ( colour, "CACHEDHCP could not allocate copy\n" );
00079                 return;
00080         }
00081         dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
00082         copy_from_user ( dhcphdr, phys_to_user ( cached_dhcpack_phys ), 0,
00083                          max_len );
00084         dhcppkt_init ( dhcppkt, dhcphdr, max_len );
00085 
00086         /* Shrink packet to required length.  If reallocation fails,
00087          * just continue to use the original packet and waste the
00088          * unused space.
00089          */
00090         len = dhcppkt_len ( dhcppkt );
00091         assert ( len <= max_len );
00092         tmp = realloc ( dhcppkt, ( sizeof ( *dhcppkt ) + len ) );
00093         if ( tmp )
00094                 dhcppkt = tmp;
00095 
00096         /* Reinitialise packet at new address */
00097         dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
00098         dhcppkt_init ( dhcppkt, dhcphdr, len );
00099 
00100         /* Store as cached DHCPACK, and mark original copy as consumed */
00101         DBGC ( colour, "CACHEDHCP found cached DHCPACK at %08x+%zx\n",
00102                cached_dhcpack_phys, len );
00103         cached_dhcpack = dhcppkt;
00104         cached_dhcpack_phys = 0;
00105 }
00106 
00107 /**
00108  * Cached DHCPACK startup function
00109  *
00110  */
00111 static void cachedhcp_startup ( void ) {
00112 
00113         /* If cached DHCP packet was not claimed by any network device
00114          * during startup, then free it.
00115          */
00116         if ( cached_dhcpack ) {
00117                 DBGC ( colour, "CACHEDHCP freeing unclaimed cached DHCPACK\n" );
00118                 dhcppkt_put ( cached_dhcpack );
00119                 cached_dhcpack = NULL;
00120         }
00121 }
00122 
00123 /** Cached DHCPACK initialisation function */
00124 struct init_fn cachedhcp_init_fn __init_fn ( INIT_NORMAL ) = {
00125         .initialise = cachedhcp_init,
00126 };
00127 
00128 /** Cached DHCPACK startup function */
00129 struct startup_fn cachedhcp_startup_fn __startup_fn ( STARTUP_LATE ) = {
00130         .startup = cachedhcp_startup,
00131 };
00132 
00133 /**
00134  * Apply cached DHCPACK to network device, if applicable
00135  *
00136  * @v netdev            Network device
00137  * @ret rc              Return status code
00138  */
00139 static int cachedhcp_probe ( struct net_device *netdev ) {
00140         struct ll_protocol *ll_protocol = netdev->ll_protocol;
00141         int rc;
00142 
00143         /* Do nothing unless we have a cached DHCPACK */
00144         if ( ! cached_dhcpack )
00145                 return 0;
00146 
00147         /* Do nothing unless cached DHCPACK's MAC address matches this
00148          * network device.
00149          */
00150         if ( memcmp ( netdev->ll_addr, cached_dhcpack->dhcphdr->chaddr,
00151                       ll_protocol->ll_addr_len ) != 0 ) {
00152                 DBGC ( colour, "CACHEDHCP cached DHCPACK does not match %s\n",
00153                        netdev->name );
00154                 return 0;
00155         }
00156         DBGC ( colour, "CACHEDHCP cached DHCPACK is for %s\n", netdev->name );
00157 
00158         /* Register as DHCP settings for this network device */
00159         if ( ( rc = register_settings ( &cached_dhcpack->settings,
00160                                         netdev_settings ( netdev ),
00161                                         DHCP_SETTINGS_NAME ) ) != 0 ) {
00162                 DBGC ( colour, "CACHEDHCP could not register settings: %s\n",
00163                        strerror ( rc ) );
00164                 return rc;
00165         }
00166 
00167         /* Claim cached DHCPACK */
00168         dhcppkt_put ( cached_dhcpack );
00169         cached_dhcpack = NULL;
00170 
00171         return 0;
00172 }
00173 
00174 /** Cached DHCP packet network device driver */
00175 struct net_driver cachedhcp_driver __net_driver = {
00176         .name = "cachedhcp",
00177         .probe = cachedhcp_probe,
00178 };