iPXE
fakedhcp.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/settings.h>
00032 #include <ipxe/netdevice.h>
00033 #include <ipxe/dhcppkt.h>
00034 #include <ipxe/fakedhcp.h>
00035 
00036 /** @file
00037  *
00038  * Fake DHCP packets
00039  *
00040  */
00041 
00042 /**
00043  * Copy settings to DHCP packet
00044  *
00045  * @v dest              Destination DHCP packet
00046  * @v source            Source settings block
00047  * @v encapsulator      Encapsulating setting tag number, or zero
00048  * @ret rc              Return status code
00049  */
00050 static int copy_encap_settings ( struct dhcp_packet *dest,
00051                                  struct settings *source,
00052                                  unsigned int encapsulator ) {
00053         struct setting setting = { .name = "" };
00054         unsigned int subtag;
00055         unsigned int tag;
00056         void *data;
00057         int len;
00058         int rc;
00059 
00060         for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
00061                 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
00062                 switch ( tag ) {
00063                 case DHCP_EB_ENCAP:
00064                 case DHCP_VENDOR_ENCAP:
00065                         /* Process encapsulated settings */
00066                         if ( ( rc = copy_encap_settings ( dest, source,
00067                                                           tag ) ) != 0 )
00068                                 return rc;
00069                         break;
00070                 default:
00071                         /* Copy setting, if present */
00072                         setting.tag = tag;
00073                         len = fetch_raw_setting_copy ( source, &setting, &data);
00074                         if ( len >= 0 ) {
00075                                 rc = dhcppkt_store ( dest, tag, data, len );
00076                                 free ( data );
00077                                 if ( rc != 0 )
00078                                         return rc;
00079                         }
00080                         break;
00081                 }
00082         }
00083 
00084         return 0;
00085 }
00086 
00087 /**
00088  * Copy settings to DHCP packet
00089  *
00090  * @v dest              Destination DHCP packet
00091  * @v source            Source settings block
00092  * @ret rc              Return status code
00093  */
00094 static int copy_settings ( struct dhcp_packet *dest,
00095                            struct settings *source ) {
00096         return copy_encap_settings ( dest, source, 0 );
00097 }
00098 
00099 /**
00100  * Create fake DHCPDISCOVER packet
00101  *
00102  * @v netdev            Network device
00103  * @v data              Buffer for DHCP packet
00104  * @v max_len           Size of DHCP packet buffer
00105  * @ret rc              Return status code
00106  *
00107  * Used by external code.
00108  */
00109 int create_fakedhcpdiscover ( struct net_device *netdev,
00110                               void *data, size_t max_len ) {
00111         struct dhcp_packet dhcppkt;
00112         struct in_addr ciaddr = { 0 };
00113         int rc;
00114 
00115         if ( ( rc = dhcp_create_request ( &dhcppkt, netdev, DHCPDISCOVER,
00116                                           dhcp_last_xid, ciaddr, data,
00117                                           max_len ) ) != 0 ) {
00118                 DBG ( "Could not create DHCPDISCOVER: %s\n",
00119                       strerror ( rc ) );
00120                 return rc;
00121         }
00122 
00123         return 0;
00124 }
00125 
00126 /**
00127  * Create fake DHCPACK packet
00128  *
00129  * @v netdev            Network device
00130  * @v data              Buffer for DHCP packet
00131  * @v max_len           Size of DHCP packet buffer
00132  * @ret rc              Return status code
00133  *
00134  * Used by external code.
00135  */
00136 int create_fakedhcpack ( struct net_device *netdev,
00137                          void *data, size_t max_len ) {
00138         struct dhcp_packet dhcppkt;
00139         int rc;
00140 
00141         /* Create base DHCPACK packet */
00142         if ( ( rc = dhcp_create_packet ( &dhcppkt, netdev, DHCPACK,
00143                                          dhcp_last_xid, NULL, 0,
00144                                          data, max_len ) ) != 0 ) {
00145                 DBG ( "Could not create DHCPACK: %s\n", strerror ( rc ) );
00146                 return rc;
00147         }
00148 
00149         /* Merge in globally-scoped settings, then netdev-specific
00150          * settings.  Do it in this order so that netdev-specific
00151          * settings take precedence regardless of stated priorities.
00152          */
00153         if ( ( rc = copy_settings ( &dhcppkt, NULL ) ) != 0 ) {
00154                 DBG ( "Could not set DHCPACK global settings: %s\n",
00155                       strerror ( rc ) );
00156                 return rc;
00157         }
00158         if ( ( rc = copy_settings ( &dhcppkt,
00159                                     netdev_settings ( netdev ) ) ) != 0 ) {
00160                 DBG ( "Could not set DHCPACK netdev settings: %s\n",
00161                       strerror ( rc ) );
00162                 return rc;
00163         }
00164 
00165         return 0;
00166 }
00167 
00168 /**
00169  * Create fake PXE Boot Server ACK packet
00170  *
00171  * @v netdev            Network device
00172  * @v data              Buffer for DHCP packet
00173  * @v max_len           Size of DHCP packet buffer
00174  * @ret rc              Return status code
00175  *
00176  * Used by external code.
00177  */
00178 int create_fakepxebsack ( struct net_device *netdev,
00179                           void *data, size_t max_len ) {
00180         struct dhcp_packet dhcppkt;
00181         struct settings *proxy_settings;
00182         struct settings *pxebs_settings;
00183         int rc;
00184 
00185         /* Identify available settings */
00186         proxy_settings = find_settings ( PROXYDHCP_SETTINGS_NAME );
00187         pxebs_settings = find_settings ( PXEBS_SETTINGS_NAME );
00188         if ( ( ! proxy_settings ) && ( ! pxebs_settings ) ) {
00189                 /* No PXE boot server; return the regular DHCPACK */
00190                 return create_fakedhcpack ( netdev, data, max_len );
00191         }
00192 
00193         /* Create base DHCPACK packet */
00194         if ( ( rc = dhcp_create_packet ( &dhcppkt, netdev, DHCPACK,
00195                                          dhcp_last_xid, NULL, 0,
00196                                          data, max_len ) ) != 0 ) {
00197                 DBG ( "Could not create PXE BS ACK: %s\n",
00198                       strerror ( rc ) );
00199                 return rc;
00200         }
00201 
00202         /* Populate ciaddr */
00203         fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
00204                              &dhcppkt.dhcphdr->ciaddr );
00205 
00206         /* Merge in ProxyDHCP options */
00207         if ( proxy_settings &&
00208              ( ( rc = copy_settings ( &dhcppkt, proxy_settings ) ) != 0 ) ) {
00209                 DBG ( "Could not copy ProxyDHCP settings: %s\n",
00210                       strerror ( rc ) );
00211                 return rc;
00212         }
00213 
00214         /* Merge in BootServerDHCP options, if present */
00215         if ( pxebs_settings &&
00216              ( ( rc = copy_settings ( &dhcppkt, pxebs_settings ) ) != 0 ) ) {
00217                 DBG ( "Could not copy PXE BS settings: %s\n",
00218                       strerror ( rc ) );
00219                 return rc;
00220         }
00221 
00222         return 0;
00223 }