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

Generated on Tue Apr 6 20:01:10 2010 for gPXE by  doxygen 1.5.7.1