iPXE
dhcppkt.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 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 <stdio.h>
00029 #include <errno.h>
00030 #include <string.h>
00031 #include <ipxe/netdevice.h>
00032 #include <ipxe/dhcp.h>
00033 #include <ipxe/dhcpopts.h>
00034 #include <ipxe/dhcppkt.h>
00035 
00036 /** @file
00037  *
00038  * DHCP packets
00039  *
00040  */
00041 
00042 /****************************************************************************
00043  *
00044  * DHCP packet raw interface
00045  *
00046  */
00047 
00048 /**
00049  * Calculate used length of an IPv4 field within a DHCP packet
00050  *
00051  * @v data              Field data
00052  * @v len               Length of field
00053  * @ret used            Used length of field
00054  */
00055 static size_t used_len_ipv4 ( const void *data, size_t len __unused ) {
00056         const struct in_addr *in = data;
00057 
00058         return ( in->s_addr ? sizeof ( *in ) : 0 );
00059 }
00060 
00061 /**
00062  * Calculate used length of a string field within a DHCP packet
00063  *
00064  * @v data              Field data
00065  * @v len               Length of field
00066  * @ret used            Used length of field
00067  */
00068 static size_t used_len_string ( const void *data, size_t len ) {
00069         return strnlen ( data, len );
00070 }
00071 
00072 /** A dedicated field within a DHCP packet */
00073 struct dhcp_packet_field {
00074         /** Settings tag number */
00075         unsigned int tag;
00076         /** Offset within DHCP packet */
00077         uint16_t offset;
00078         /** Length of field */
00079         uint16_t len;
00080         /** Calculate used length of field
00081          *
00082          * @v data      Field data
00083          * @v len       Length of field
00084          * @ret used    Used length of field
00085          */
00086         size_t ( * used_len ) ( const void *data, size_t len );
00087 };
00088 
00089 /** Declare a dedicated field within a DHCP packet
00090  *
00091  * @v _tag              Settings tag number
00092  * @v _field            Field name
00093  * @v _used_len         Function to calculate used length of field
00094  */
00095 #define DHCP_PACKET_FIELD( _tag, _field, _used_len ) {                  \
00096                 .tag = (_tag),                                          \
00097                 .offset = offsetof ( struct dhcphdr, _field ),          \
00098                 .len = sizeof ( ( ( struct dhcphdr * ) 0 )->_field ),   \
00099                 .used_len = _used_len,                                  \
00100         }
00101 
00102 /** Dedicated fields within a DHCP packet */
00103 static struct dhcp_packet_field dhcp_packet_fields[] = {
00104         DHCP_PACKET_FIELD ( DHCP_EB_YIADDR, yiaddr, used_len_ipv4 ),
00105         DHCP_PACKET_FIELD ( DHCP_EB_SIADDR, siaddr, used_len_ipv4 ),
00106         DHCP_PACKET_FIELD ( DHCP_TFTP_SERVER_NAME, sname, used_len_string ),
00107         DHCP_PACKET_FIELD ( DHCP_BOOTFILE_NAME, file, used_len_string ),
00108 };
00109 
00110 /**
00111  * Get address of a DHCP packet field
00112  *
00113  * @v dhcphdr           DHCP packet header
00114  * @v field             DHCP packet field
00115  * @ret data            Packet field data
00116  */
00117 static inline void * dhcp_packet_field ( struct dhcphdr *dhcphdr,
00118                                          struct dhcp_packet_field *field ) {
00119         return ( ( ( void * ) dhcphdr ) + field->offset );
00120 }
00121 
00122 /**
00123  * Find DHCP packet field corresponding to settings tag number
00124  *
00125  * @v tag               Settings tag number
00126  * @ret field           DHCP packet field, or NULL
00127  */
00128 static struct dhcp_packet_field *
00129 find_dhcp_packet_field ( unsigned int tag ) {
00130         struct dhcp_packet_field *field;
00131         unsigned int i;
00132 
00133         for ( i = 0 ; i < ( sizeof ( dhcp_packet_fields ) /
00134                             sizeof ( dhcp_packet_fields[0] ) ) ; i++ ) {
00135                 field = &dhcp_packet_fields[i];
00136                 if ( field->tag == tag )
00137                         return field;
00138         }
00139         return NULL;
00140 }
00141 
00142 /**
00143  * Check applicability of DHCP setting
00144  *
00145  * @v dhcppkt           DHCP packet
00146  * @v tag               Setting tag number
00147  * @ret applies         Setting applies within this settings block
00148  */
00149 static int dhcppkt_applies ( struct dhcp_packet *dhcppkt __unused,
00150                              unsigned int tag ) {
00151 
00152         return dhcpopt_applies ( tag );
00153 }
00154 
00155 /**
00156  * Store value of DHCP packet setting
00157  *
00158  * @v dhcppkt           DHCP packet
00159  * @v tag               Setting tag number
00160  * @v data              Setting data, or NULL to clear setting
00161  * @v len               Length of setting data
00162  * @ret rc              Return status code
00163  */
00164 int dhcppkt_store ( struct dhcp_packet *dhcppkt, unsigned int tag,
00165                     const void *data, size_t len ) {
00166         struct dhcp_packet_field *field;
00167         void *field_data;
00168 
00169         /* If this is a special field, fill it in */
00170         if ( ( field = find_dhcp_packet_field ( tag ) ) != NULL ) {
00171                 if ( len > field->len )
00172                         return -ENOSPC;
00173                 field_data = dhcp_packet_field ( dhcppkt->dhcphdr, field );
00174                 memset ( field_data, 0, field->len );
00175                 memcpy ( dhcp_packet_field ( dhcppkt->dhcphdr, field ),
00176                          data, len );
00177                 /* Erase any equivalent option from the options block */
00178                 dhcpopt_store ( &dhcppkt->options, tag, NULL, 0 );
00179                 return 0;
00180         }
00181 
00182         /* Otherwise, use the generic options block */
00183         return dhcpopt_store ( &dhcppkt->options, tag, data, len );
00184 }
00185 
00186 /**
00187  * Fetch value of DHCP packet setting
00188  *
00189  * @v dhcppkt           DHCP packet
00190  * @v tag               Setting tag number
00191  * @v data              Buffer to fill with setting data
00192  * @v len               Length of buffer
00193  * @ret len             Length of setting data, or negative error
00194  */
00195 int dhcppkt_fetch ( struct dhcp_packet *dhcppkt, unsigned int tag,
00196                     void *data, size_t len ) {
00197         struct dhcp_packet_field *field;
00198         void *field_data;
00199         size_t field_len = 0;
00200         
00201         /* Identify special field, if any */
00202         if ( ( field = find_dhcp_packet_field ( tag ) ) != NULL ) {
00203                 field_data = dhcp_packet_field ( dhcppkt->dhcphdr, field );
00204                 field_len = field->used_len ( field_data, field->len );
00205         }
00206 
00207         /* Return special field, if it exists and is populated */
00208         if ( field_len ) {
00209                 if ( len > field_len )
00210                         len = field_len;
00211                 memcpy ( data, field_data, len );
00212                 return field_len;
00213         }
00214 
00215         /* Otherwise, use the generic options block */
00216         return dhcpopt_fetch ( &dhcppkt->options, tag, data, len );
00217 }
00218 
00219 /****************************************************************************
00220  *
00221  * DHCP packet settings interface
00222  *
00223  */
00224 
00225 /**
00226  * Check applicability of DHCP setting
00227  *
00228  * @v settings          Settings block
00229  * @v setting           Setting
00230  * @ret applies         Setting applies within this settings block
00231  */
00232 static int dhcppkt_settings_applies ( struct settings *settings,
00233                                       const struct setting *setting ) {
00234         struct dhcp_packet *dhcppkt =
00235                 container_of ( settings, struct dhcp_packet, settings );
00236 
00237         return ( ( setting->scope == NULL ) &&
00238                  dhcppkt_applies ( dhcppkt, setting->tag ) );
00239 }
00240 
00241 /**
00242  * Store value of DHCP setting
00243  *
00244  * @v settings          Settings block
00245  * @v setting           Setting to store
00246  * @v data              Setting data, or NULL to clear setting
00247  * @v len               Length of setting data
00248  * @ret rc              Return status code
00249  */
00250 static int dhcppkt_settings_store ( struct settings *settings,
00251                                     const struct setting *setting,
00252                                     const void *data, size_t len ) {
00253         struct dhcp_packet *dhcppkt =
00254                 container_of ( settings, struct dhcp_packet, settings );
00255 
00256         return dhcppkt_store ( dhcppkt, setting->tag, data, len );
00257 }
00258 
00259 /**
00260  * Fetch value of DHCP setting
00261  *
00262  * @v settings          Settings block, or NULL to search all blocks
00263  * @v setting           Setting to fetch
00264  * @v data              Buffer to fill with setting data
00265  * @v len               Length of buffer
00266  * @ret len             Length of setting data, or negative error
00267  */
00268 static int dhcppkt_settings_fetch ( struct settings *settings,
00269                                     struct setting *setting,
00270                                     void *data, size_t len ) {
00271         struct dhcp_packet *dhcppkt =
00272                 container_of ( settings, struct dhcp_packet, settings );
00273 
00274         return dhcppkt_fetch ( dhcppkt, setting->tag, data, len );
00275 }
00276 
00277 /** DHCP settings operations */
00278 static struct settings_operations dhcppkt_settings_operations = {
00279         .applies = dhcppkt_settings_applies,
00280         .store = dhcppkt_settings_store,
00281         .fetch = dhcppkt_settings_fetch,
00282 };
00283 
00284 /****************************************************************************
00285  *
00286  * Constructor
00287  *
00288  */
00289 
00290 /**
00291  * Initialise DHCP packet
00292  *
00293  * @v dhcppkt           DHCP packet structure to fill in
00294  * @v data              DHCP packet raw data
00295  * @v max_len           Length of raw data buffer
00296  *
00297  * Initialise a DHCP packet structure from a data buffer containing a
00298  * DHCP packet.
00299  */
00300 void dhcppkt_init ( struct dhcp_packet *dhcppkt, struct dhcphdr *data,
00301                     size_t len ) {
00302         ref_init ( &dhcppkt->refcnt, NULL );
00303         dhcppkt->dhcphdr = data;
00304         dhcpopt_init ( &dhcppkt->options, &dhcppkt->dhcphdr->options,
00305                        ( len - offsetof ( struct dhcphdr, options ) ),
00306                        dhcpopt_no_realloc );
00307         settings_init ( &dhcppkt->settings, &dhcppkt_settings_operations,
00308                         &dhcppkt->refcnt, NULL );
00309 }