1 |
27 |
unneback |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// lib/bootp_support.c
|
4 |
|
|
//
|
5 |
|
|
// Minimal BOOTP functions
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
16 |
|
|
//
|
17 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
18 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
20 |
|
|
// for more details.
|
21 |
|
|
//
|
22 |
|
|
// You should have received a copy of the GNU General Public License along
|
23 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
24 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
25 |
|
|
//
|
26 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
27 |
|
|
// or inline functions from this file, or you compile this file and link it
|
28 |
|
|
// with other works to produce a work based on this file, this file does not
|
29 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
30 |
|
|
// License. However the source code for this file must still be made available
|
31 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
32 |
|
|
//
|
33 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
34 |
|
|
// this file might be covered by the GNU General Public License.
|
35 |
|
|
//
|
36 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
37 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
38 |
|
|
// -------------------------------------------
|
39 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
40 |
|
|
//==========================================================================
|
41 |
|
|
//#####DESCRIPTIONBEGIN####
|
42 |
|
|
//
|
43 |
|
|
// Author(s): gthomas
|
44 |
|
|
// Contributors: gthomas
|
45 |
|
|
// Date: 2000-01-10
|
46 |
|
|
// Purpose:
|
47 |
|
|
// Description:
|
48 |
|
|
//
|
49 |
|
|
//
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//
|
52 |
|
|
//==========================================================================
|
53 |
|
|
|
54 |
|
|
// BOOTP support (and a little DHCP support also)
|
55 |
|
|
|
56 |
|
|
#include <pkgconf/system.h>
|
57 |
|
|
#include <pkgconf/net.h>
|
58 |
|
|
#include <pkgconf/isoinfra.h>
|
59 |
|
|
|
60 |
|
|
#include <network.h>
|
61 |
|
|
#include <errno.h>
|
62 |
|
|
#ifdef CYGPKG_NET_FREEBSD_STACK // New layout
|
63 |
|
|
#include <net/if_var.h>
|
64 |
|
|
#include <netinet/in_var.h>
|
65 |
|
|
#include <netinet6/nd6.h>
|
66 |
|
|
#endif
|
67 |
|
|
|
68 |
|
|
#ifdef CYGINT_ISO_DNS
|
69 |
|
|
#include <netdb.h>
|
70 |
|
|
#endif
|
71 |
|
|
|
72 |
|
|
#ifndef CYGPKG_LIBC_STDIO
|
73 |
|
|
#define perror(s) diag_printf(#s ": %s\n", strerror(errno))
|
74 |
|
|
#endif
|
75 |
|
|
|
76 |
|
|
// This function sets up the interface it the simplest configuration.
|
77 |
|
|
// Just enough to broadcast a BOOTP request and get a response.
|
78 |
|
|
// It returns 'true' if a response was obtained.
|
79 |
|
|
cyg_bool_t
|
80 |
|
|
do_bootp(const char *intf, struct bootp *recv)
|
81 |
|
|
{
|
82 |
|
|
struct sockaddr_in *addrp;
|
83 |
|
|
struct ifreq ifr;
|
84 |
|
|
struct sockaddr_in cli_addr, serv_addr, bootp_server_addr;
|
85 |
|
|
struct ecos_rtentry route;
|
86 |
|
|
int s, addrlen;
|
87 |
|
|
int one = 1;
|
88 |
|
|
struct bootp bootp_xmit;
|
89 |
|
|
unsigned char mincookie[] = {99,130,83,99,255} ;
|
90 |
|
|
struct timeval tv;
|
91 |
|
|
cyg_bool_t retcode = true;
|
92 |
|
|
|
93 |
|
|
// Ensure clean slate
|
94 |
|
|
cyg_route_reinit(); // Force any existing routes to be forgotten
|
95 |
|
|
|
96 |
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
97 |
|
|
if (s < 0) {
|
98 |
|
|
perror("socket");
|
99 |
|
|
return false;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one))) {
|
103 |
|
|
perror("setsockopt");
|
104 |
|
|
return false;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
addrp = (struct sockaddr_in *) &ifr.ifr_addr;
|
108 |
|
|
memset(addrp, 0, sizeof(*addrp));
|
109 |
|
|
addrp->sin_family = AF_INET;
|
110 |
|
|
addrp->sin_len = sizeof(*addrp);
|
111 |
|
|
addrp->sin_port = 0;
|
112 |
|
|
addrp->sin_addr.s_addr = INADDR_ANY;
|
113 |
|
|
|
114 |
|
|
strcpy(ifr.ifr_name, intf);
|
115 |
|
|
if (ioctl(s, SIOCSIFADDR, &ifr)) {
|
116 |
|
|
perror("SIOCSIFADDR");
|
117 |
|
|
return false;
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
if (ioctl(s, SIOCSIFNETMASK, &ifr)) {
|
121 |
|
|
perror("SIOCSIFNETMASK");
|
122 |
|
|
return false;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
/* the broadcast address is 255.255.255.255 */
|
126 |
|
|
memset(&addrp->sin_addr, 255, sizeof(addrp->sin_addr));
|
127 |
|
|
if (ioctl(s, SIOCSIFBRDADDR, &ifr)) {
|
128 |
|
|
perror("SIOCSIFBRDADDR");
|
129 |
|
|
return false;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
ifr.ifr_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING;
|
133 |
|
|
if (ioctl(s, SIOCSIFFLAGS, &ifr)) {
|
134 |
|
|
perror("SIOCSIFFLAGS");
|
135 |
|
|
return false;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
|
139 |
|
|
perror("SIOCGIFHWADDR");
|
140 |
|
|
return false;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
// Set up routing
|
144 |
|
|
/* the broadcast address is 255.255.255.255 */
|
145 |
|
|
memset(&addrp->sin_addr, 255, sizeof(addrp->sin_addr));
|
146 |
|
|
memset(&route, 0, sizeof(route));
|
147 |
|
|
memcpy(&route.rt_gateway, addrp, sizeof(*addrp));
|
148 |
|
|
|
149 |
|
|
addrp->sin_family = AF_INET;
|
150 |
|
|
addrp->sin_port = 0;
|
151 |
|
|
addrp->sin_addr.s_addr = INADDR_ANY;
|
152 |
|
|
memcpy(&route.rt_dst, addrp, sizeof(*addrp));
|
153 |
|
|
memcpy(&route.rt_genmask, addrp, sizeof(*addrp));
|
154 |
|
|
|
155 |
|
|
route.rt_dev = ifr.ifr_name;
|
156 |
|
|
route.rt_flags = RTF_UP|RTF_GATEWAY;
|
157 |
|
|
route.rt_metric = 0;
|
158 |
|
|
|
159 |
|
|
if (ioctl(s, SIOCADDRT, &route)) {
|
160 |
|
|
if (errno != EEXIST) {
|
161 |
|
|
perror("SIOCADDRT 3");
|
162 |
|
|
return false;
|
163 |
|
|
}
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
memset((char *) &cli_addr, 0, sizeof(cli_addr));
|
167 |
|
|
cli_addr.sin_family = AF_INET;
|
168 |
|
|
cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
169 |
|
|
cli_addr.sin_port = htons(IPPORT_BOOTPC);
|
170 |
|
|
|
171 |
|
|
if(bind(s, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) {
|
172 |
|
|
perror("bind error");
|
173 |
|
|
return false;
|
174 |
|
|
}
|
175 |
|
|
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
|
176 |
|
|
perror("setsockopt SO_REUSEADDR");
|
177 |
|
|
return false;
|
178 |
|
|
}
|
179 |
|
|
if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
|
180 |
|
|
perror("setsockopt SO_REUSEPORT");
|
181 |
|
|
return false;
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
memset((char *) &serv_addr, 0, sizeof(serv_addr));
|
185 |
|
|
serv_addr.sin_family = AF_INET;
|
186 |
|
|
serv_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
|
187 |
|
|
serv_addr.sin_port = htons(IPPORT_BOOTPS);
|
188 |
|
|
|
189 |
|
|
// Fill in the BOOTP request
|
190 |
|
|
bzero(&bootp_xmit, sizeof(bootp_xmit));
|
191 |
|
|
if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
|
192 |
|
|
perror("SIOCGIFHWADDR");
|
193 |
|
|
return false;
|
194 |
|
|
}
|
195 |
|
|
bootp_xmit.bp_htype = HTYPE_ETHERNET;
|
196 |
|
|
bootp_xmit.bp_hlen = IFHWADDRLEN;
|
197 |
|
|
bcopy(ifr.ifr_hwaddr.sa_data, &bootp_xmit.bp_chaddr, bootp_xmit.bp_hlen);
|
198 |
|
|
|
199 |
|
|
bootp_xmit.bp_secs = 0;
|
200 |
|
|
bcopy(mincookie, bootp_xmit.bp_vend, sizeof(mincookie));
|
201 |
|
|
|
202 |
|
|
bootp_xmit.bp_op = BOOTREQUEST;
|
203 |
|
|
|
204 |
|
|
if(sendto(s, &bootp_xmit, sizeof(struct bootp), 0,
|
205 |
|
|
(struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
|
206 |
|
|
perror("sendto error");
|
207 |
|
|
return false;
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
tv.tv_sec = 5;
|
211 |
|
|
tv.tv_usec = 0;
|
212 |
|
|
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
213 |
|
|
|
214 |
|
|
addrlen = sizeof(bootp_server_addr);
|
215 |
|
|
if (recvfrom(s, recv, sizeof(struct bootp), 0,
|
216 |
|
|
(struct sockaddr *)&bootp_server_addr, &addrlen) < 0) {
|
217 |
|
|
// This is an "acceptable" error, it means there is no server for
|
218 |
|
|
// us: do not initialize the interface.
|
219 |
|
|
retcode = false;
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
// Shut things down regardless of success of rx, otherwise other
|
223 |
|
|
// interfaces cannot be initialised!
|
224 |
|
|
memset(addrp, 0, sizeof(*addrp));
|
225 |
|
|
addrp->sin_family = AF_INET;
|
226 |
|
|
addrp->sin_len = sizeof(*addrp);
|
227 |
|
|
addrp->sin_port = 0;
|
228 |
|
|
addrp->sin_addr.s_addr = INADDR_ANY;
|
229 |
|
|
|
230 |
|
|
strcpy(ifr.ifr_name, intf);
|
231 |
|
|
if (ioctl(s, SIOCDIFADDR, &ifr)) {
|
232 |
|
|
perror("SIOCDIFADDR");
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
// Shut down interface so it can be reinitialized
|
236 |
|
|
ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
|
237 |
|
|
if (ioctl(s, SIOCSIFFLAGS, &ifr)) {
|
238 |
|
|
perror("SIOCSIFFLAGS");
|
239 |
|
|
return false;
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
// All done with socket
|
243 |
|
|
close(s);
|
244 |
|
|
return retcode;
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
static char *_bootp_op[] = {"", "REQUEST", "REPLY"};
|
248 |
|
|
static char *_bootp_hw_type[] = {"", "Ethernet", "Exp Ethernet", "AX25",
|
249 |
|
|
"Pronet", "Chaos", "IEEE802", "Arcnet"};
|
250 |
|
|
|
251 |
|
|
static char *_dhcpmsgs[] = {"","DISCOVER", "OFFER", "REQUEST", "DECLINE",
|
252 |
|
|
"ACK", "NAK", "RELEASE" };
|
253 |
|
|
|
254 |
|
|
void
|
255 |
|
|
show_bootp(const char *intf, struct bootp *bp)
|
256 |
|
|
{
|
257 |
|
|
int i, len;
|
258 |
|
|
unsigned char *op, *ap = 0, optover;
|
259 |
|
|
unsigned char name[128];
|
260 |
|
|
struct in_addr addr[32];
|
261 |
|
|
unsigned int length;
|
262 |
|
|
|
263 |
|
|
diag_printf("BOOTP[%s] op: %s\n", intf, _bootp_op[bp->bp_op]);
|
264 |
|
|
diag_printf(" htype: %s\n", _bootp_hw_type[bp->bp_htype]);
|
265 |
|
|
diag_printf(" hlen: %d\n", bp->bp_hlen );
|
266 |
|
|
diag_printf(" hops: %d\n", bp->bp_hops );
|
267 |
|
|
diag_printf(" xid: 0x%x\n", bp->bp_xid );
|
268 |
|
|
diag_printf(" secs: %d\n", bp->bp_secs );
|
269 |
|
|
diag_printf(" flags: 0x%x\n", bp->bp_flags );
|
270 |
|
|
diag_printf(" hw_addr: ");
|
271 |
|
|
for (i = 0; i < bp->bp_hlen; i++) {
|
272 |
|
|
diag_printf("%02x", bp->bp_chaddr[i]);
|
273 |
|
|
if (i != (bp->bp_hlen-1)) diag_printf(":");
|
274 |
|
|
}
|
275 |
|
|
diag_printf("\n");
|
276 |
|
|
diag_printf(" client IP: %s\n", inet_ntoa(bp->bp_ciaddr));
|
277 |
|
|
diag_printf(" my IP: %s\n", inet_ntoa(bp->bp_yiaddr));
|
278 |
|
|
diag_printf(" server IP: %s\n", inet_ntoa(bp->bp_siaddr));
|
279 |
|
|
diag_printf(" gateway IP: %s\n", inet_ntoa(bp->bp_giaddr));
|
280 |
|
|
|
281 |
|
|
optover = 0; // See whether sname and file are overridden for options
|
282 |
|
|
length = sizeof(optover);
|
283 |
|
|
(void)get_bootp_option( bp, TAG_DHCP_OPTOVER, &optover, &length );
|
284 |
|
|
if ( !(1 & optover) && bp->bp_sname[0] )
|
285 |
|
|
diag_printf(" server: %s\n", bp->bp_sname);
|
286 |
|
|
if ( ! (2 & optover) && bp->bp_file[0] )
|
287 |
|
|
diag_printf(" file: %s\n", bp->bp_file);
|
288 |
|
|
if (bp->bp_vend[0]) {
|
289 |
|
|
diag_printf(" options:\n");
|
290 |
|
|
op = &bp->bp_vend[4];
|
291 |
|
|
while (*op != TAG_END) {
|
292 |
|
|
switch (*op) {
|
293 |
|
|
case TAG_SUBNET_MASK:
|
294 |
|
|
case TAG_GATEWAY:
|
295 |
|
|
case TAG_IP_BROADCAST:
|
296 |
|
|
case TAG_DOMAIN_SERVER:
|
297 |
|
|
ap = (unsigned char *)&addr[0];
|
298 |
|
|
len = *(op+1);
|
299 |
|
|
for (i = 0; i < len; i++) {
|
300 |
|
|
*ap++ = *(op+i+2);
|
301 |
|
|
}
|
302 |
|
|
if (*op == TAG_SUBNET_MASK) ap = " subnet mask";
|
303 |
|
|
if (*op == TAG_GATEWAY) ap = " gateway";
|
304 |
|
|
if (*op == TAG_IP_BROADCAST) ap = " IP broadcast";
|
305 |
|
|
if (*op == TAG_DOMAIN_SERVER) ap = "domain server";
|
306 |
|
|
diag_printf(" %s: ", ap);
|
307 |
|
|
ap = (unsigned char *)&addr[0];
|
308 |
|
|
while (len > 0) {
|
309 |
|
|
diag_printf("%s", inet_ntoa(*(struct in_addr *)ap));
|
310 |
|
|
len -= sizeof(struct in_addr);
|
311 |
|
|
ap += sizeof(struct in_addr);
|
312 |
|
|
if (len) diag_printf(", ");
|
313 |
|
|
}
|
314 |
|
|
diag_printf("\n");
|
315 |
|
|
break;
|
316 |
|
|
case TAG_DOMAIN_NAME:
|
317 |
|
|
case TAG_HOST_NAME:
|
318 |
|
|
for (i = 0; i < *(op+1); i++) {
|
319 |
|
|
name[i] = *(op+i+2);
|
320 |
|
|
}
|
321 |
|
|
name[*(op+1)] = '\0';
|
322 |
|
|
if (*op == TAG_DOMAIN_NAME) ap = " domain name";
|
323 |
|
|
if (*op == TAG_HOST_NAME) ap = " host name";
|
324 |
|
|
diag_printf(" %s: %s\n", ap, name);
|
325 |
|
|
break;
|
326 |
|
|
case TAG_DHCP_MESS_TYPE:
|
327 |
|
|
diag_printf(" DHCP message: %d %s\n",
|
328 |
|
|
op[2], _dhcpmsgs[op[2]] );
|
329 |
|
|
break;
|
330 |
|
|
case TAG_DHCP_REQ_IP:
|
331 |
|
|
diag_printf(" DHCP requested ip: %d.%d.%d.%d\n",
|
332 |
|
|
op[2], op[3], op[4], op[5] );
|
333 |
|
|
break;
|
334 |
|
|
case TAG_DHCP_LEASE_TIME :
|
335 |
|
|
case TAG_DHCP_RENEWAL_TIME :
|
336 |
|
|
case TAG_DHCP_REBIND_TIME :
|
337 |
|
|
diag_printf(" DHCP time %d: %d\n",
|
338 |
|
|
*op, ((((((op[2]<<8)+op[3])<<8)+op[4])<<8)+op[5]) );
|
339 |
|
|
|
340 |
|
|
break;
|
341 |
|
|
case TAG_DHCP_SERVER_ID :
|
342 |
|
|
diag_printf(" DHCP server id: %d.%d.%d.%d\n",
|
343 |
|
|
op[2], op[3], op[4], op[5] );
|
344 |
|
|
break;
|
345 |
|
|
|
346 |
|
|
case TAG_DHCP_OPTOVER :
|
347 |
|
|
case TAG_DHCP_PARM_REQ_LIST:
|
348 |
|
|
case TAG_DHCP_TEXT_MESSAGE :
|
349 |
|
|
case TAG_DHCP_MAX_MSGSZ :
|
350 |
|
|
case TAG_DHCP_CLASSID :
|
351 |
|
|
case TAG_DHCP_CLIENTID :
|
352 |
|
|
diag_printf(" DHCP option: %x/%d.%d:", *op, *op, *(op+1));
|
353 |
|
|
if ( 1 == op[1] )
|
354 |
|
|
diag_printf( " %d", op[2] );
|
355 |
|
|
else if ( 2 == op[1] )
|
356 |
|
|
diag_printf( " %d", (op[2]<<8)+op[3] );
|
357 |
|
|
else if ( 4 == op[1] )
|
358 |
|
|
diag_printf( " %d", ((((((op[2]<<8)+op[3])<<8)+op[4])<<8)+op[5]) );
|
359 |
|
|
else
|
360 |
|
|
for ( i = 2; i < 2 + op[1]; i++ )
|
361 |
|
|
diag_printf(" %d",op[i]);
|
362 |
|
|
diag_printf("\n");
|
363 |
|
|
break;
|
364 |
|
|
|
365 |
|
|
default:
|
366 |
|
|
diag_printf("Unknown option: %x/%d.%d:", *op, *op, *(op+1));
|
367 |
|
|
for ( i = 2; i < 2 + op[1]; i++ )
|
368 |
|
|
diag_printf(" %d",op[i]);
|
369 |
|
|
diag_printf("\n");
|
370 |
|
|
break;
|
371 |
|
|
}
|
372 |
|
|
op += *(op+1)+2;
|
373 |
|
|
}
|
374 |
|
|
}
|
375 |
|
|
}
|
376 |
|
|
|
377 |
|
|
cyg_bool_t
|
378 |
|
|
get_bootp_option(struct bootp *bp, unsigned char tag, void *opt,
|
379 |
|
|
unsigned int *length)
|
380 |
|
|
{
|
381 |
|
|
unsigned char *val = (unsigned char *)opt;
|
382 |
|
|
int i;
|
383 |
|
|
cyg_uint8 optover;
|
384 |
|
|
|
385 |
|
|
#define SCANTAG( ptr ) CYG_MACRO_START \
|
386 |
|
|
unsigned int max; \
|
387 |
|
|
unsigned char *op = (ptr); \
|
388 |
|
|
while (*op != TAG_END) { \
|
389 |
|
|
if (*op == tag) { \
|
390 |
|
|
max=(*(op+1)>*length ? *length : *(op+1)); \
|
391 |
|
|
for (i = 0; i < max; i++) { \
|
392 |
|
|
*val++ = *(op+i+2); \
|
393 |
|
|
} \
|
394 |
|
|
*length=max; \
|
395 |
|
|
return true; \
|
396 |
|
|
} \
|
397 |
|
|
op += *(op+1)+2; \
|
398 |
|
|
} \
|
399 |
|
|
CYG_MACRO_END
|
400 |
|
|
|
401 |
|
|
SCANTAG( &bp->bp_vend[4] );
|
402 |
|
|
|
403 |
|
|
if ( TAG_DHCP_OPTOVER == tag ) // prevent recursion > once
|
404 |
|
|
return false;
|
405 |
|
|
// else, look for that tag to see if there's more...
|
406 |
|
|
optover = 0;
|
407 |
|
|
if ( ! get_bootp_option( bp, TAG_DHCP_OPTOVER, &optover, length) )
|
408 |
|
|
return false;
|
409 |
|
|
|
410 |
|
|
if ( 1 & optover ) // then the file field also holds options
|
411 |
|
|
SCANTAG( &bp->bp_file[0] );
|
412 |
|
|
|
413 |
|
|
if ( 2 & optover ) // then the sname field also holds options
|
414 |
|
|
SCANTAG( &bp->bp_sname[0] );
|
415 |
|
|
|
416 |
|
|
return false;
|
417 |
|
|
}
|
418 |
|
|
|
419 |
|
|
// [Re]initialize the network interface with the info passed from BOOTP
|
420 |
|
|
cyg_bool_t
|
421 |
|
|
init_net(const char *intf, struct bootp *bp)
|
422 |
|
|
{
|
423 |
|
|
struct sockaddr_in *addrp;
|
424 |
|
|
struct ifreq ifr;
|
425 |
|
|
int s;
|
426 |
|
|
int one = 1;
|
427 |
|
|
struct ecos_rtentry route;
|
428 |
|
|
struct in_addr netmask, gateway;
|
429 |
|
|
unsigned int length;
|
430 |
|
|
|
431 |
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
432 |
|
|
if (s < 0) {
|
433 |
|
|
perror("socket");
|
434 |
|
|
return false;
|
435 |
|
|
}
|
436 |
|
|
|
437 |
|
|
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one))) {
|
438 |
|
|
perror("setsockopt");
|
439 |
|
|
return false;
|
440 |
|
|
}
|
441 |
|
|
|
442 |
|
|
addrp = (struct sockaddr_in *) &ifr.ifr_addr;
|
443 |
|
|
memset(addrp, 0, sizeof(*addrp));
|
444 |
|
|
addrp->sin_family = AF_INET;
|
445 |
|
|
addrp->sin_len = sizeof(*addrp);
|
446 |
|
|
addrp->sin_port = 0;
|
447 |
|
|
addrp->sin_addr = bp->bp_yiaddr; // The address BOOTP gave us
|
448 |
|
|
|
449 |
|
|
// Must do this temporarily with default route and netmask so that
|
450 |
|
|
// [sub]netmask can be set.
|
451 |
|
|
strcpy(ifr.ifr_name, intf);
|
452 |
|
|
if (ioctl(s, SIOCSIFADDR, &ifr)) {
|
453 |
|
|
perror("SIOCIFADDR");
|
454 |
|
|
return false;
|
455 |
|
|
}
|
456 |
|
|
|
457 |
|
|
length = sizeof(addrp->sin_addr);
|
458 |
|
|
if (get_bootp_option(bp, TAG_SUBNET_MASK, &addrp->sin_addr,&length)) {
|
459 |
|
|
netmask = addrp->sin_addr;
|
460 |
|
|
if (ioctl(s, SIOCSIFNETMASK, &ifr)) {
|
461 |
|
|
perror("SIOCSIFNETMASK");
|
462 |
|
|
return false;
|
463 |
|
|
}
|
464 |
|
|
// Must do this again so that [sub]netmask (and so default route)
|
465 |
|
|
// is taken notice of.
|
466 |
|
|
addrp->sin_addr = bp->bp_yiaddr; // The address BOOTP gave us
|
467 |
|
|
if (ioctl(s, SIOCSIFADDR, &ifr)) {
|
468 |
|
|
perror("SIOCIFADDR 2");
|
469 |
|
|
return false;
|
470 |
|
|
}
|
471 |
|
|
}
|
472 |
|
|
|
473 |
|
|
length = sizeof(addrp->sin_addr);
|
474 |
|
|
if (get_bootp_option(bp, TAG_IP_BROADCAST, &addrp->sin_addr,&length)) {
|
475 |
|
|
if (ioctl(s, SIOCSIFBRDADDR, &ifr)) {
|
476 |
|
|
perror("SIOCSIFBRDADDR");
|
477 |
|
|
return false;
|
478 |
|
|
}
|
479 |
|
|
// Do not re-set the IFADDR after this; doing *that* resets the
|
480 |
|
|
// BRDADDR to the default!
|
481 |
|
|
}
|
482 |
|
|
|
483 |
|
|
ifr.ifr_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING;
|
484 |
|
|
if (ioctl(s, SIOCSIFFLAGS, &ifr)) {
|
485 |
|
|
perror("SIOCSIFFLAGS");
|
486 |
|
|
return false;
|
487 |
|
|
}
|
488 |
|
|
|
489 |
|
|
// Set up routing
|
490 |
|
|
length = sizeof(addrp->sin_addr);
|
491 |
|
|
if (get_bootp_option(bp, TAG_GATEWAY, &gateway,&length)) {
|
492 |
|
|
// ...and it's a nonzero address...
|
493 |
|
|
if ( 0 != gateway.s_addr ) {
|
494 |
|
|
memset(&route, 0, sizeof(route));
|
495 |
|
|
addrp->sin_family = AF_INET;
|
496 |
|
|
addrp->sin_port = 0;
|
497 |
|
|
addrp->sin_len = sizeof(*addrp);
|
498 |
|
|
addrp->sin_addr.s_addr = 0; // Use 0,0,GATEWAY for the default route
|
499 |
|
|
memcpy(&route.rt_dst, addrp, sizeof(*addrp));
|
500 |
|
|
addrp->sin_addr.s_addr = 0;
|
501 |
|
|
memcpy(&route.rt_genmask, addrp, sizeof(*addrp));
|
502 |
|
|
addrp->sin_addr = gateway;
|
503 |
|
|
memcpy(&route.rt_gateway, addrp, sizeof(*addrp));
|
504 |
|
|
|
505 |
|
|
route.rt_dev = ifr.ifr_name;
|
506 |
|
|
route.rt_flags = RTF_UP|RTF_GATEWAY;
|
507 |
|
|
route.rt_metric = 0;
|
508 |
|
|
|
509 |
|
|
if (ioctl(s, SIOCADDRT, &route)) {
|
510 |
|
|
diag_printf("Route - dst: %s",
|
511 |
|
|
inet_ntoa(((struct sockaddr_in *)&route.rt_dst)->sin_addr));
|
512 |
|
|
diag_printf(", mask: %s",
|
513 |
|
|
inet_ntoa(((struct sockaddr_in *)&route.rt_genmask)->sin_addr));
|
514 |
|
|
diag_printf(", gateway: %s\n",
|
515 |
|
|
inet_ntoa(((struct sockaddr_in *)&route.rt_gateway)->sin_addr));
|
516 |
|
|
if (errno != EEXIST) {
|
517 |
|
|
perror("SIOCADDRT 3");
|
518 |
|
|
return false;
|
519 |
|
|
}
|
520 |
|
|
}
|
521 |
|
|
}
|
522 |
|
|
}
|
523 |
|
|
close(s);
|
524 |
|
|
#ifdef CYGINT_ISO_DNS
|
525 |
|
|
{
|
526 |
|
|
#define MAX_IP_ADDR_LEN 16
|
527 |
|
|
char buf[BP_MAX_OPTION_LEN+1];
|
528 |
|
|
memset(buf,0,sizeof(buf));
|
529 |
|
|
length = sizeof(buf);
|
530 |
|
|
if (get_bootp_option(bp, TAG_DOMAIN_NAME, buf, &length)) {
|
531 |
|
|
setdomainname(buf, length);
|
532 |
|
|
}
|
533 |
|
|
length = sizeof(buf);
|
534 |
|
|
if (get_bootp_option(bp, TAG_DOMAIN_SERVER, buf, &length)) {
|
535 |
|
|
cyg_dns_res_init((struct in_addr *)buf);
|
536 |
|
|
}
|
537 |
|
|
}
|
538 |
|
|
#endif
|
539 |
|
|
return true;
|
540 |
|
|
}
|
541 |
|
|
|
542 |
|
|
#ifdef INET6
|
543 |
|
|
extern const struct in6_addr in6mask128;
|
544 |
|
|
cyg_bool_t
|
545 |
|
|
init_net_IPv6(const char *intf, struct bootp *bp, char *prefix)
|
546 |
|
|
{
|
547 |
|
|
int s;
|
548 |
|
|
struct in6_aliasreq in6_addr;
|
549 |
|
|
char in6_ip[128];
|
550 |
|
|
|
551 |
|
|
// Set up non link-layer address
|
552 |
|
|
s = socket(AF_INET6, SOCK_DGRAM, 0);
|
553 |
|
|
if (s < 0) {
|
554 |
|
|
perror("socket IPv6");
|
555 |
|
|
return false;
|
556 |
|
|
}
|
557 |
|
|
bzero(&in6_addr, sizeof(in6_addr));
|
558 |
|
|
diag_sprintf(in6_ip, "%s::%s", prefix, inet_ntoa(bp->bp_yiaddr));
|
559 |
|
|
in6_addr.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
|
560 |
|
|
in6_addr.ifra_addr.sin6_family = AF_INET6;
|
561 |
|
|
if (!inet_pton(AF_INET6, in6_ip, (char *)&in6_addr.ifra_addr.sin6_addr)) {
|
562 |
|
|
diag_printf("Can't set IPv6 address: %s\n", in6_ip);
|
563 |
|
|
return false;
|
564 |
|
|
}
|
565 |
|
|
in6_addr.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
|
566 |
|
|
in6_addr.ifra_prefixmask.sin6_family = AF_INET6;
|
567 |
|
|
in6_addr.ifra_prefixmask.sin6_addr = in6mask128;
|
568 |
|
|
strcpy(in6_addr.ifra_name, intf);
|
569 |
|
|
in6_addr.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
|
570 |
|
|
in6_addr.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
|
571 |
|
|
if (ioctl(s, SIOCAIFADDR_IN6, &in6_addr)) {
|
572 |
|
|
perror("SIOCAIFADDR_IN6");
|
573 |
|
|
return false;
|
574 |
|
|
}
|
575 |
|
|
close(s);
|
576 |
|
|
return true;
|
577 |
|
|
}
|
578 |
|
|
#endif
|
579 |
|
|
|
580 |
|
|
// EOF bootp_support.c
|