1 | /************************************************************************** |
---|
2 | * |
---|
3 | * Copyright (c) 2013 Alcatel-Lucent |
---|
4 | * |
---|
5 | * Alcatel Lucent licenses this file to You under the Apache License, |
---|
6 | * Version 2.0 (the "License"); you may not use this file except in |
---|
7 | * compliance with the License. A copy of the License is contained the |
---|
8 | * file LICENSE at the top level of this repository. |
---|
9 | * You may also obtain a copy of the License at: |
---|
10 | * |
---|
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
12 | * |
---|
13 | * Unless required by applicable law or agreed to in writing, software |
---|
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
16 | * See the License for the specific language governing permissions and |
---|
17 | * limitations under the License. |
---|
18 | * |
---|
19 | ************************************************************************** |
---|
20 | * |
---|
21 | * dhcp_00.c: |
---|
22 | * |
---|
23 | * This file contains the functions that are called from dhcpboot.c if the |
---|
24 | * the default DHCP interaction is to be done by the monitor. Whenever a |
---|
25 | * new dhcp_XX.c file is created, this file should be used as the template. |
---|
26 | * |
---|
27 | * Original author: Ed Sutter (ed.sutter@alcatel-lucent.com) |
---|
28 | * |
---|
29 | */ |
---|
30 | |
---|
31 | #include "config.h" |
---|
32 | #include "cpuio.h" |
---|
33 | #include "ether.h" |
---|
34 | #include "tfs.h" |
---|
35 | #include "tfsprivate.h" |
---|
36 | #include "genlib.h" |
---|
37 | #include "endian.h" |
---|
38 | #include "stddefs.h" |
---|
39 | |
---|
40 | #if INCLUDE_DHCPBOOT |
---|
41 | |
---|
42 | /* ValidDHCPOffer(): |
---|
43 | * Target issued the DISCOVER, the incoming packet is the server's |
---|
44 | * OFFER reply. If the DHCPOFFRFLTR shell variable is not present, then |
---|
45 | * return 1 indicating acceptance of the offer. If the DHCPOFFRFLTR variable |
---|
46 | * exists, then use its content to determine if the offer should be accepted... |
---|
47 | * |
---|
48 | * Shell variable syntax: |
---|
49 | * |
---|
50 | * DHCP_FIELD_IDENTIFIER,EXPECTED_STRING |
---|
51 | * |
---|
52 | * where DHCP_FIELD_IDENTIFIER can be... |
---|
53 | * BFN bootfile name |
---|
54 | * SHN server-host name |
---|
55 | * VSO### vendor-specific option number (options encapsulated within |
---|
56 | * the Vendor-Specific Information (#43) option) |
---|
57 | * SSO### site-specific option number (range 128-254) |
---|
58 | * |
---|
59 | * For example: |
---|
60 | * 1... |
---|
61 | * if DHCPOFFRFLTR contains "BFN,abcfile" |
---|
62 | * then the offer will only be accepted if the bootfile specified by the |
---|
63 | * dhcp server contains the string "abcfile". |
---|
64 | * 2... |
---|
65 | * if DHCPOFFRFLTR contains "SHN,a_host_name" |
---|
66 | * then the offer will only be accepted if the server-hostname specified |
---|
67 | * by the dhcp server contains the string "a_host_name". |
---|
68 | * 3... |
---|
69 | * if DHCPOFFRFLTR contains "VSO18,some_String" |
---|
70 | * then the offer will only be accepted if the server returns vendor- |
---|
71 | * specific option # 18 and it contains the string "some_String". |
---|
72 | * |
---|
73 | * Note that "containing the string" means that the string specified in |
---|
74 | * the DHCPOFFRFLTR shell variable can be the entire string or a sub-string |
---|
75 | * within the server's response. |
---|
76 | */ |
---|
77 | int |
---|
78 | ValidDHCPOffer(struct dhcphdr *dhdr) |
---|
79 | { |
---|
80 | char *var; |
---|
81 | int offeraccepted, badsyntax; |
---|
82 | |
---|
83 | /* If no DHCPOFFRFLTR var, accept the offer... */ |
---|
84 | var = getenv("DHCPOFFRFLTR"); |
---|
85 | if (!var) |
---|
86 | return(1); |
---|
87 | |
---|
88 | /* Start off by assuming acceptance... */ |
---|
89 | badsyntax = 0; |
---|
90 | offeraccepted = 1; |
---|
91 | |
---|
92 | /* Now process the DHCPOFFRFLTR variable and incoming dhcp data, |
---|
93 | * and clear the offeraccepted (or set badsyntax) flag if necessary... |
---|
94 | */ |
---|
95 | |
---|
96 | if (!strncmp(var,"BFN,",4)) { |
---|
97 | if (!strstr((char *)dhdr->bootfile,var+4)) |
---|
98 | offeraccepted = 0; |
---|
99 | } |
---|
100 | else if (!strncmp(var,"SHN,",4)) { |
---|
101 | if (!strstr((char *)dhdr->server_hostname,var+4)) |
---|
102 | offeraccepted = 0; |
---|
103 | } |
---|
104 | else if ((!strncmp(var,"SSO",3)) || (!strncmp(var,"VSO",3))) { |
---|
105 | int optno; |
---|
106 | ulong cookie; |
---|
107 | char *comma, *optval, *vso; |
---|
108 | |
---|
109 | optno = atoi(var+3); |
---|
110 | comma = strchr(var,','); |
---|
111 | memcpy((char *)&cookie,(char *)&dhdr->magic_cookie,4); |
---|
112 | if (cookie != ecl(STANDARD_MAGIC_COOKIE)) |
---|
113 | offeraccepted = 0; |
---|
114 | else if (!comma) |
---|
115 | badsyntax = 1; |
---|
116 | else if (var[0] == 'S') { |
---|
117 | if ((optno < 128) || (optno > 254)) |
---|
118 | badsyntax = 1; |
---|
119 | else { |
---|
120 | optval = (char *)DhcpGetOption(optno,(unsigned char *)(dhdr+1)); |
---|
121 | if (!optval) |
---|
122 | offeraccepted = 0; |
---|
123 | else { |
---|
124 | if (!strstr(optval+2,comma+1)) |
---|
125 | offeraccepted = 0; |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | else if (var[0] == 'V') { |
---|
130 | if ((optno < 0) || (optno > 254)) |
---|
131 | badsyntax = 1; |
---|
132 | else { |
---|
133 | vso = (char *)DhcpGetOption(DHCPOPT_VENDORSPECIFICINFO, |
---|
134 | (uchar *)dhdr+1); |
---|
135 | if (!vso) |
---|
136 | offeraccepted = 0; |
---|
137 | else { |
---|
138 | optval = (char *)DhcpGetOption(optno,(unsigned char *)vso+2); |
---|
139 | if (!optval) |
---|
140 | offeraccepted = 0; |
---|
141 | else { |
---|
142 | if (!strstr(optval+2,comma+1)) |
---|
143 | offeraccepted = 0; |
---|
144 | } |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | else { |
---|
150 | badsyntax = 1; |
---|
151 | } |
---|
152 | if (badsyntax) { |
---|
153 | printf("Bad DHCPOFFRFLTR syntax.\n"); |
---|
154 | offeraccepted = 0; |
---|
155 | } |
---|
156 | return(offeraccepted); |
---|
157 | } |
---|
158 | |
---|
159 | /* DhcpVendorSpecific(): |
---|
160 | * Process any vendor specific data from the incoming header. |
---|
161 | */ |
---|
162 | void |
---|
163 | DhcpVendorSpecific(struct dhcphdr *dhdr) |
---|
164 | { |
---|
165 | return; /* Obviously, the default is no processing. */ |
---|
166 | } |
---|
167 | |
---|
168 | /* printDhcpVSopt(): |
---|
169 | * Input is the option, the option length and the pointer to the options. |
---|
170 | * Print the option. |
---|
171 | */ |
---|
172 | int |
---|
173 | printDhcpVSopt(int vsopt, int vsoptlen, char *options) |
---|
174 | { |
---|
175 | return(0); |
---|
176 | } |
---|
177 | |
---|
178 | /* buildDhcpHdr(): |
---|
179 | * Called by dhcpboot.c to allow application-specific header stuff to |
---|
180 | * be added to header. Return 0 if generic stuff in dhcpboot.c is to be |
---|
181 | * used; else return 1 and the calling code will assume this function is |
---|
182 | * dealing with it (see dhcpboot.c for basic idea). |
---|
183 | */ |
---|
184 | int |
---|
185 | buildDhcpHdr(struct dhcphdr *dhdr) |
---|
186 | { |
---|
187 | return(0); |
---|
188 | } |
---|
189 | |
---|
190 | /* DhcpBootpLoadVSA(): |
---|
191 | * Called by DhcpBootpDone to store an ascii-coded-hex copy of the |
---|
192 | * vendor-specific-area (BOOTP) or options (DHCP) in the shell variable |
---|
193 | * DHCPVSA. |
---|
194 | */ |
---|
195 | void |
---|
196 | DhcpBootpLoadVSA(uchar *vsabin, int vsize) |
---|
197 | { |
---|
198 | int i; |
---|
199 | uchar *cp, *vsaacx; |
---|
200 | |
---|
201 | /* If after the transaction has completed, the RLYAGNT is */ |
---|
202 | /* set, but GIPADD is not set, copy RLYAGNT to GIPADD... */ |
---|
203 | if (!getenv("GIPADD") && getenv("RLYAGNT")) |
---|
204 | setenv("GIPADD",getenv("RLYAGNT")); |
---|
205 | |
---|
206 | /* Since the VSA (bootp) or options list (DHCP) can be large, |
---|
207 | * This code looks for the presence of the DHCPVSA shell variable |
---|
208 | * as an indication as to whether or not this data should be stored |
---|
209 | * in the DHCPVSA variable... |
---|
210 | * If the variable is present, then this code will load the DHCPVSA |
---|
211 | * shell variable; else it just returns here. |
---|
212 | * Note that it doesn't matter what the content of DHCPVSA is, as |
---|
213 | * long as it is present, so just loading it with "TRUE" in monrc |
---|
214 | * will be sufficient to tell this logic to load the variable with |
---|
215 | * the data. |
---|
216 | */ |
---|
217 | if (!getenv("DHCPVSA")) |
---|
218 | return; |
---|
219 | |
---|
220 | /* If allocation succeeds, then copy BOOTP VendorSpecificArea to the */ |
---|
221 | /* DHCPVSA shell variable. Copy it as ascii-coded hex */ |
---|
222 | vsaacx = (uchar *)malloc((vsize*2)+4); |
---|
223 | if (vsaacx) { |
---|
224 | cp = vsaacx; |
---|
225 | for(i=0;i<vsize;i++,cp+=2) |
---|
226 | sprintf((char *)cp,"%02x", vsabin[i]); |
---|
227 | *cp = 0; |
---|
228 | setenv("DHCPVSA", (char *)vsaacx); |
---|
229 | free((char *)vsaacx); |
---|
230 | } |
---|
231 | else |
---|
232 | printf("DHCPVSA space allocation failed\n"); |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | /* DhcpBootpDone(): |
---|
237 | * Called at the end of the Bootp or Dhcp transaction. |
---|
238 | * Input... |
---|
239 | * bootp: 1 if BOOTP; else DHCP. |
---|
240 | * dhdr: pointer to dhcp or bootp header. |
---|
241 | * vsize: size of vendor specific area (for bootp this is fixed at 64, |
---|
242 | * but for dhcp it is variable). |
---|
243 | */ |
---|
244 | void |
---|
245 | DhcpBootpDone(int bootp, struct dhcphdr *dhdr, int vsize) |
---|
246 | { |
---|
247 | if (bootp) { |
---|
248 | struct bootphdr *bhdr; |
---|
249 | |
---|
250 | bhdr = (struct bootphdr *)dhdr; |
---|
251 | DhcpBootpLoadVSA(bhdr->vsa,vsize); |
---|
252 | } |
---|
253 | else { |
---|
254 | DhcpBootpLoadVSA((uchar *)&dhdr->magic_cookie,vsize); |
---|
255 | } |
---|
256 | return; |
---|
257 | } |
---|
258 | |
---|
259 | #endif |
---|