[zbin] Fix check for existence of most recent output byte
[ipxe.git] / src / net / fakedhcp.c
1 /*
2 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <ipxe/settings.h>
28 #include <ipxe/netdevice.h>
29 #include <ipxe/dhcppkt.h>
30 #include <ipxe/fakedhcp.h>
31
32 /** @file
33 *
34 * Fake DHCP packets
35 *
36 */
37
38 /**
39 * Copy settings to DHCP packet
40 *
41 * @v dest Destination DHCP packet
42 * @v source Source settings block
43 * @v encapsulator Encapsulating setting tag number, or zero
44 * @ret rc Return status code
45 */
46 static int copy_encap_settings ( struct dhcp_packet *dest,
47 struct settings *source,
48 unsigned int encapsulator ) {
49 struct setting setting = { .name = "" };
50 unsigned int subtag;
51 unsigned int tag;
52 void *data;
53 int len;
54 int rc;
55
56 for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
57 tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
58 switch ( tag ) {
59 case DHCP_EB_ENCAP:
60 case DHCP_VENDOR_ENCAP:
61 /* Process encapsulated settings */
62 if ( ( rc = copy_encap_settings ( dest, source,
63 tag ) ) != 0 )
64 return rc;
65 break;
66 default:
67 /* Copy setting, if present */
68 setting.tag = tag;
69 len = fetch_raw_setting_copy ( source, &setting, &data);
70 if ( len >= 0 ) {
71 rc = dhcppkt_store ( dest, tag, data, len );
72 free ( data );
73 if ( rc != 0 )
74 return rc;
75 }
76 break;
77 }
78 }
79
80 return 0;
81 }
82
83 /**
84 * Copy settings to DHCP packet
85 *
86 * @v dest Destination DHCP packet
87 * @v source Source settings block
88 * @ret rc Return status code
89 */
90 static int copy_settings ( struct dhcp_packet *dest,
91 struct settings *source ) {
92 return copy_encap_settings ( dest, source, 0 );
93 }
94
95 /**
96 * Create fake DHCPDISCOVER packet
97 *
98 * @v netdev Network device
99 * @v data Buffer for DHCP packet
100 * @v max_len Size of DHCP packet buffer
101 * @ret rc Return status code
102 *
103 * Used by external code.
104 */
105 int create_fakedhcpdiscover ( struct net_device *netdev,
106 void *data, size_t max_len ) {
107 struct dhcp_packet dhcppkt;
108 struct in_addr ciaddr = { 0 };
109 int rc;
110
111 if ( ( rc = dhcp_create_request ( &dhcppkt, netdev, DHCPDISCOVER,
112 dhcp_last_xid, ciaddr, data,
113 max_len ) ) != 0 ) {
114 DBG ( "Could not create DHCPDISCOVER: %s\n",
115 strerror ( rc ) );
116 return rc;
117 }
118
119 return 0;
120 }
121
122 /**
123 * Create fake DHCPACK packet
124 *
125 * @v netdev Network device
126 * @v data Buffer for DHCP packet
127 * @v max_len Size of DHCP packet buffer
128 * @ret rc Return status code
129 *
130 * Used by external code.
131 */
132 int create_fakedhcpack ( struct net_device *netdev,
133 void *data, size_t max_len ) {
134 struct dhcp_packet dhcppkt;
135 int rc;
136
137 /* Create base DHCPACK packet */
138 if ( ( rc = dhcp_create_packet ( &dhcppkt, netdev, DHCPACK,
139 dhcp_last_xid, NULL, 0,
140 data, max_len ) ) != 0 ) {
141 DBG ( "Could not create DHCPACK: %s\n", strerror ( rc ) );
142 return rc;
143 }
144
145 /* Merge in globally-scoped settings, then netdev-specific
146 * settings. Do it in this order so that netdev-specific
147 * settings take precedence regardless of stated priorities.
148 */
149 if ( ( rc = copy_settings ( &dhcppkt, NULL ) ) != 0 ) {
150 DBG ( "Could not set DHCPACK global settings: %s\n",
151 strerror ( rc ) );
152 return rc;
153 }
154 if ( ( rc = copy_settings ( &dhcppkt,
155 netdev_settings ( netdev ) ) ) != 0 ) {
156 DBG ( "Could not set DHCPACK netdev settings: %s\n",
157 strerror ( rc ) );
158 return rc;
159 }
160
161 return 0;
162 }
163
164 /**
165 * Create fake PXE Boot Server ACK packet
166 *
167 * @v netdev Network device
168 * @v data Buffer for DHCP packet
169 * @v max_len Size of DHCP packet buffer
170 * @ret rc Return status code
171 *
172 * Used by external code.
173 */
174 int create_fakepxebsack ( struct net_device *netdev,
175 void *data, size_t max_len ) {
176 struct dhcp_packet dhcppkt;
177 struct settings *proxy_settings;
178 struct settings *pxebs_settings;
179 int rc;
180
181 /* Identify available settings */
182 proxy_settings = find_settings ( PROXYDHCP_SETTINGS_NAME );
183 pxebs_settings = find_settings ( PXEBS_SETTINGS_NAME );
184 if ( ( ! proxy_settings ) && ( ! pxebs_settings ) ) {
185 /* No PXE boot server; return the regular DHCPACK */
186 return create_fakedhcpack ( netdev, data, max_len );
187 }
188
189 /* Create base DHCPACK packet */
190 if ( ( rc = dhcp_create_packet ( &dhcppkt, netdev, DHCPACK,
191 dhcp_last_xid, NULL, 0,
192 data, max_len ) ) != 0 ) {
193 DBG ( "Could not create PXE BS ACK: %s\n",
194 strerror ( rc ) );
195 return rc;
196 }
197
198 /* Merge in ProxyDHCP options */
199 if ( proxy_settings &&
200 ( ( rc = copy_settings ( &dhcppkt, proxy_settings ) ) != 0 ) ) {
201 DBG ( "Could not copy ProxyDHCP settings: %s\n",
202 strerror ( rc ) );
203 return rc;
204 }
205
206 /* Merge in BootServerDHCP options, if present */
207 if ( pxebs_settings &&
208 ( ( rc = copy_settings ( &dhcppkt, pxebs_settings ) ) != 0 ) ) {
209 DBG ( "Could not copy PXE BS settings: %s\n",
210 strerror ( rc ) );
211 return rc;
212 }
213
214 return 0;
215 }