1 | /* |
---|
2 | * sys-bsd.c - System-dependent procedures for setting up |
---|
3 | * PPP interfaces on bsd-4.4-ish systems (including 386BSD, NetBSD, RTEMS, etc.) |
---|
4 | * |
---|
5 | * Copyright (c) 1989 Carnegie Mellon University. |
---|
6 | * Copyright (c) 1995 The Australian National University. |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms are permitted |
---|
10 | * provided that the above copyright notice and this paragraph are |
---|
11 | * duplicated in all such forms and that any documentation, |
---|
12 | * advertising materials, and other materials related to such |
---|
13 | * distribution and use acknowledge that the software was developed |
---|
14 | * by Carnegie Mellon University and The Australian National University. |
---|
15 | * The names of the Universities may not be used to endorse or promote |
---|
16 | * products derived from this software without specific prior written |
---|
17 | * permission. |
---|
18 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
---|
19 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
---|
20 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef lint |
---|
24 | /* static char rcsid[] = "$Id$"; */ |
---|
25 | /* $NetBSD: sys-bsd.c,v 1.1.1.3 1997/09/26 18:53:04 christos Exp $ */ |
---|
26 | #endif |
---|
27 | |
---|
28 | /* |
---|
29 | * TODO: |
---|
30 | */ |
---|
31 | |
---|
32 | #include <stdio.h> |
---|
33 | #include <syslog.h> |
---|
34 | #include <string.h> |
---|
35 | #include <stdlib.h> |
---|
36 | #include <unistd.h> |
---|
37 | #include <errno.h> |
---|
38 | #include <fcntl.h> |
---|
39 | #include <termios.h> |
---|
40 | #include <signal.h> |
---|
41 | #include <sys/ioctl.h> |
---|
42 | #include <sys/types.h> |
---|
43 | #include <sys/socket.h> |
---|
44 | #include <sys/time.h> |
---|
45 | #include <sys/stat.h> |
---|
46 | #include <sys/param.h> |
---|
47 | #ifdef NetBSD1_2 |
---|
48 | #include <util.h> |
---|
49 | #endif |
---|
50 | #ifdef PPP_FILTER |
---|
51 | #include <net/bpf.h> |
---|
52 | #endif |
---|
53 | |
---|
54 | #include <net/if.h> |
---|
55 | #include <net/ppp_defs.h> |
---|
56 | #include <net/if_ppp.h> |
---|
57 | #include <net/route.h> |
---|
58 | #include <net/if_dl.h> |
---|
59 | #include <netinet/in.h> |
---|
60 | |
---|
61 | #if RTM_VERSION >= 3 |
---|
62 | #include <sys/param.h> |
---|
63 | #if defined(NetBSD) && (NetBSD >= 199703) |
---|
64 | #include <netinet/if_inarp.h> |
---|
65 | #else /* NetBSD 1.2D or later */ |
---|
66 | #include <netinet/if_ether.h> |
---|
67 | #endif |
---|
68 | #endif |
---|
69 | |
---|
70 | #include "pppd.h" |
---|
71 | #include "fsm.h" |
---|
72 | #include "ipcp.h" |
---|
73 | |
---|
74 | static int initdisc = -1; /* Initial TTY discipline for ppp_fd */ |
---|
75 | static int initfdflags = -1; /* Initial file descriptor flags for ppp_fd */ |
---|
76 | static int ppp_fd = -1; /* fd which is set to PPP discipline */ |
---|
77 | static int rtm_seq; |
---|
78 | |
---|
79 | static int restore_term; /* 1 => we've munged the terminal */ |
---|
80 | static struct termios inittermios; /* Initial TTY termios */ |
---|
81 | static struct winsize wsinfo; /* Initial window size info */ |
---|
82 | |
---|
83 | #if 0 |
---|
84 | static char *lock_file; /* name of lock file created */ |
---|
85 | #endif |
---|
86 | |
---|
87 | static int loop_slave = -1; |
---|
88 | static int loop_master; |
---|
89 | #if 0 |
---|
90 | static char loop_name[20]; |
---|
91 | #endif |
---|
92 | |
---|
93 | static unsigned char inbuf[512]; /* buffer for chars read from loopback */ |
---|
94 | |
---|
95 | static int sockfd; /* socket for doing interface ioctls */ |
---|
96 | |
---|
97 | static int if_is_up; /* the interface is currently up */ |
---|
98 | static u_int32_t ifaddrs[2]; /* local and remote addresses we set */ |
---|
99 | static u_int32_t default_route_gateway; /* gateway addr for default route */ |
---|
100 | static u_int32_t proxy_arp_addr; /* remote addr for proxy arp */ |
---|
101 | |
---|
102 | /* Prototypes for procedures local to this file. */ |
---|
103 | static int dodefaultroute __P((u_int32_t, int)); |
---|
104 | static int get_ether_addr __P((u_int32_t, struct sockaddr_dl *)); |
---|
105 | |
---|
106 | |
---|
107 | /* |
---|
108 | * sys_init - System-dependent initialization. |
---|
109 | */ |
---|
110 | void |
---|
111 | sys_init() |
---|
112 | { |
---|
113 | /* Get an internet socket for doing socket ioctl's on. */ |
---|
114 | if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
---|
115 | syslog(LOG_ERR, "Couldn't create IP socket: %m"); |
---|
116 | die(1); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | /* |
---|
121 | * sys_cleanup - restore any system state we modified before exiting: |
---|
122 | * mark the interface down, delete default route and/or proxy arp entry. |
---|
123 | * This should call die() because it's called from die(). |
---|
124 | */ |
---|
125 | void |
---|
126 | sys_cleanup() |
---|
127 | { |
---|
128 | struct ifreq ifr; |
---|
129 | |
---|
130 | if (if_is_up) { |
---|
131 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
---|
132 | if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0 |
---|
133 | && ((ifr.ifr_flags & IFF_UP) != 0)) { |
---|
134 | ifr.ifr_flags &= ~IFF_UP; |
---|
135 | ioctl(sockfd, SIOCSIFFLAGS, &ifr); |
---|
136 | } |
---|
137 | } |
---|
138 | if (ifaddrs[0] != 0) |
---|
139 | cifaddr(0, ifaddrs[0], ifaddrs[1]); |
---|
140 | if (default_route_gateway) |
---|
141 | cifdefaultroute(0, 0, default_route_gateway); |
---|
142 | if (proxy_arp_addr) |
---|
143 | cifproxyarp(0, proxy_arp_addr); |
---|
144 | } |
---|
145 | |
---|
146 | /* |
---|
147 | * sys_close - Clean up in a child process before execing. |
---|
148 | */ |
---|
149 | void |
---|
150 | sys_close() |
---|
151 | { |
---|
152 | close(sockfd); |
---|
153 | if (loop_slave >= 0) { |
---|
154 | close(loop_slave); |
---|
155 | close(loop_master); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | /* |
---|
160 | * sys_check_options - check the options that the user specified |
---|
161 | */ |
---|
162 | void |
---|
163 | sys_check_options() |
---|
164 | { |
---|
165 | } |
---|
166 | |
---|
167 | /* |
---|
168 | * ppp_available - check whether the system has any ppp interfaces |
---|
169 | * (in fact we check whether we can do an ioctl on ppp0). |
---|
170 | */ |
---|
171 | int |
---|
172 | ppp_available() |
---|
173 | { |
---|
174 | int s, ok; |
---|
175 | struct ifreq ifr; |
---|
176 | extern char *no_ppp_msg; |
---|
177 | |
---|
178 | if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) |
---|
179 | return 1; /* can't tell */ |
---|
180 | |
---|
181 | strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name)); |
---|
182 | ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0; |
---|
183 | close(s); |
---|
184 | |
---|
185 | no_ppp_msg = "\ |
---|
186 | This system lacks kernel support for PPP. To include PPP support\n\ |
---|
187 | in the kernel, please follow the steps detailed in the README.bsd\n\ |
---|
188 | file in the ppp-2.2 distribution.\n"; |
---|
189 | return ok; |
---|
190 | } |
---|
191 | |
---|
192 | /* |
---|
193 | * establish_ppp - Turn the serial port into a ppp interface. |
---|
194 | */ |
---|
195 | void |
---|
196 | establish_ppp(fd) |
---|
197 | int fd; |
---|
198 | { |
---|
199 | int pppdisc = PPPDISC; |
---|
200 | int x; |
---|
201 | |
---|
202 | if (demand) { |
---|
203 | /* |
---|
204 | * Demand mode - prime the old ppp device to relinquish the unit. |
---|
205 | */ |
---|
206 | if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0) { |
---|
207 | syslog(LOG_ERR, "ioctl(transfer ppp unit): %m"); |
---|
208 | die(1); |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | /* |
---|
213 | * Save the old line discipline of fd, and set it to PPP. |
---|
214 | */ |
---|
215 | if (ioctl(fd, TIOCGETD, &initdisc) < 0) { |
---|
216 | syslog(LOG_ERR, "ioctl(TIOCGETD): %m"); |
---|
217 | die(1); |
---|
218 | } |
---|
219 | if (ioctl(fd, TIOCSETD, &pppdisc) < 0) { |
---|
220 | syslog(LOG_ERR, "ioctl(TIOCSETD): %m"); |
---|
221 | die(1); |
---|
222 | } |
---|
223 | |
---|
224 | if (!demand) { |
---|
225 | /* |
---|
226 | * Find out which interface we were given. |
---|
227 | */ |
---|
228 | if (ioctl(fd, PPPIOCGUNIT, &interfunit) < 0) { |
---|
229 | syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m"); |
---|
230 | die(1); |
---|
231 | } |
---|
232 | } else { |
---|
233 | /* |
---|
234 | * Check that we got the same unit again. |
---|
235 | */ |
---|
236 | |
---|
237 | if (ioctl(fd, PPPIOCGUNIT, &x) < 0) { |
---|
238 | syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m"); |
---|
239 | die(1); |
---|
240 | } |
---|
241 | if (x != interfunit) { |
---|
242 | syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d", |
---|
243 | interfunit, x); |
---|
244 | die(1); |
---|
245 | } |
---|
246 | x = TTYDISC; |
---|
247 | ioctl(loop_slave, TIOCSETD, &x); |
---|
248 | } |
---|
249 | |
---|
250 | ppp_fd = fd; |
---|
251 | |
---|
252 | /* |
---|
253 | * Enable debug in the driver if requested. |
---|
254 | */ |
---|
255 | if (kdebugflag) { |
---|
256 | if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) { |
---|
257 | syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m"); |
---|
258 | } else { |
---|
259 | x |= (kdebugflag & 0xFF) * SC_DEBUG; |
---|
260 | if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) |
---|
261 | syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m") |
---|
262 | ; |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | /* |
---|
267 | * Set device for non-blocking reads. |
---|
268 | */ |
---|
269 | #define fcntl(a) |
---|
270 | |
---|
271 | |
---|
272 | /* if ((initfdflags = fcntl(fd, F_GETFL)) == -1 |
---|
273 | || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) { |
---|
274 | */ |
---|
275 | /* syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m"); |
---|
276 | */ /*}*/ |
---|
277 | } |
---|
278 | |
---|
279 | /* |
---|
280 | * restore_loop - reattach the ppp unit to the loopback. |
---|
281 | */ |
---|
282 | void |
---|
283 | restore_loop() |
---|
284 | { |
---|
285 | int x; |
---|
286 | |
---|
287 | /* |
---|
288 | * Transfer the ppp interface back to the loopback. |
---|
289 | */ |
---|
290 | if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0) { |
---|
291 | syslog(LOG_ERR, "ioctl(transfer ppp unit): %m"); |
---|
292 | die(1); |
---|
293 | } |
---|
294 | x = PPPDISC; |
---|
295 | if (ioctl(loop_slave, TIOCSETD, &x) < 0) { |
---|
296 | syslog(LOG_ERR, "ioctl(TIOCSETD): %m"); |
---|
297 | die(1); |
---|
298 | } |
---|
299 | |
---|
300 | /* |
---|
301 | * Check that we got the same unit again. |
---|
302 | */ |
---|
303 | if (ioctl(loop_slave, PPPIOCGUNIT, &x) < 0) { |
---|
304 | syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m"); |
---|
305 | die(1); |
---|
306 | } |
---|
307 | if (x != interfunit) { |
---|
308 | syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d", |
---|
309 | interfunit, x); |
---|
310 | die(1); |
---|
311 | } |
---|
312 | ppp_fd = loop_slave; |
---|
313 | } |
---|
314 | |
---|
315 | |
---|
316 | /* |
---|
317 | * disestablish_ppp - Restore the serial port to normal operation. |
---|
318 | * This shouldn't call die() because it's called from die(). |
---|
319 | */ |
---|
320 | void |
---|
321 | disestablish_ppp(fd) |
---|
322 | int fd; |
---|
323 | { |
---|
324 | /* Reset non-blocking mode on fd. */ |
---|
325 | /* if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)*/ |
---|
326 | /* syslog(LOG_WARNING, "Couldn't restore device fd flags: %m"); |
---|
327 | */ initfdflags = -1; |
---|
328 | |
---|
329 | /* Restore old line discipline. */ |
---|
330 | if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0) |
---|
331 | /* syslog(LOG_ERR, "ioctl(TIOCSETD): %m"); |
---|
332 | */ initdisc = -1; |
---|
333 | |
---|
334 | if (fd == ppp_fd) |
---|
335 | ppp_fd = -1; |
---|
336 | } |
---|
337 | |
---|
338 | /* |
---|
339 | * Check whether the link seems not to be 8-bit clean. |
---|
340 | */ |
---|
341 | void |
---|
342 | clean_check() |
---|
343 | { |
---|
344 | int x; |
---|
345 | char *s; |
---|
346 | |
---|
347 | if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) { |
---|
348 | s = NULL; |
---|
349 | switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) { |
---|
350 | case SC_RCV_B7_0: |
---|
351 | s = "bit 7 set to 1"; |
---|
352 | break; |
---|
353 | case SC_RCV_B7_1: |
---|
354 | s = "bit 7 set to 0"; |
---|
355 | break; |
---|
356 | case SC_RCV_EVNP: |
---|
357 | s = "odd parity"; |
---|
358 | break; |
---|
359 | case SC_RCV_ODDP: |
---|
360 | s = "even parity"; |
---|
361 | break; |
---|
362 | } |
---|
363 | /* if (s != NULL) { |
---|
364 | syslog(LOG_WARNING, "Serial link is not 8-bit clean:"); |
---|
365 | syslog(LOG_WARNING, "All received characters had %s", s); |
---|
366 | } |
---|
367 | */ } |
---|
368 | } |
---|
369 | |
---|
370 | /* |
---|
371 | * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity, |
---|
372 | * at the requested speed, etc. If `local' is true, set CLOCAL |
---|
373 | * regardless of whether the modem option was specified. |
---|
374 | * |
---|
375 | * For *BSD, we assume that speed_t values numerically equal bits/second. |
---|
376 | */ |
---|
377 | void |
---|
378 | set_up_tty(fd, local) |
---|
379 | int fd, local; |
---|
380 | { |
---|
381 | struct termios tios; |
---|
382 | |
---|
383 | if (tcgetattr(fd, &tios) < 0) { |
---|
384 | syslog(LOG_ERR, "tcgetattr: %m"); |
---|
385 | die(1); |
---|
386 | } |
---|
387 | |
---|
388 | if (!restore_term) { |
---|
389 | inittermios = tios; |
---|
390 | ioctl(fd, TIOCGWINSZ, &wsinfo); |
---|
391 | } |
---|
392 | |
---|
393 | tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL); |
---|
394 | if (crtscts > 0 && !local) |
---|
395 | tios.c_cflag |= CRTSCTS; |
---|
396 | else if (crtscts < 0) |
---|
397 | tios.c_cflag &= ~CRTSCTS; |
---|
398 | |
---|
399 | tios.c_cflag |= CS8 | CREAD | HUPCL; |
---|
400 | if (local || !modem) |
---|
401 | tios.c_cflag |= CLOCAL; |
---|
402 | tios.c_iflag = IGNBRK | IGNPAR; |
---|
403 | tios.c_oflag = 0; |
---|
404 | tios.c_lflag = 0; |
---|
405 | tios.c_cc[VMIN] = 1; |
---|
406 | tios.c_cc[VTIME] = 0; |
---|
407 | |
---|
408 | if (crtscts == -2) { |
---|
409 | tios.c_iflag |= IXON | IXOFF; |
---|
410 | tios.c_cc[VSTOP] = 0x13; |
---|
411 | tios.c_cc[VSTART] = 0x11; |
---|
412 | } |
---|
413 | |
---|
414 | if (inspeed) { |
---|
415 | cfsetospeed(&tios, inspeed); |
---|
416 | cfsetispeed(&tios, inspeed); |
---|
417 | } else { |
---|
418 | inspeed = cfgetospeed(&tios); |
---|
419 | /* |
---|
420 | * We can't proceed if the serial port speed is 0, |
---|
421 | * since that implies that the serial port is disabled. |
---|
422 | */ |
---|
423 | if (inspeed == 0) { |
---|
424 | syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate", |
---|
425 | devnam); |
---|
426 | die(1); |
---|
427 | } |
---|
428 | } |
---|
429 | /* baud_rate = 9600;*/ |
---|
430 | /* we hardcode it temporarily */ |
---|
431 | if (tcsetattr(fd, TCSANOW, &tios) < 0) { |
---|
432 | syslog(LOG_ERR, "tcsetattr: %m"); |
---|
433 | die(1); |
---|
434 | } |
---|
435 | |
---|
436 | restore_term = 1; |
---|
437 | } |
---|
438 | |
---|
439 | /* |
---|
440 | * restore_tty - restore the terminal to the saved settings. |
---|
441 | */ |
---|
442 | void |
---|
443 | restore_tty(fd) |
---|
444 | int fd; |
---|
445 | { |
---|
446 | /* if (restore_term) { |
---|
447 | if (!default_device) { |
---|
448 | */ /* |
---|
449 | * Turn off echoing, because otherwise we can get into |
---|
450 | * a loop with the tty and the modem echoing to each other. |
---|
451 | * We presume we are the sole user of this tty device, so |
---|
452 | * when we close it, it will revert to its defaults anyway. |
---|
453 | */ |
---|
454 | /* inittermios.c_lflag &= ~(ECHO | ECHONL); |
---|
455 | } |
---|
456 | if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0) |
---|
457 | if (errno != ENXIO) |
---|
458 | syslog(LOG_WARNING, "tcsetattr: %m"); |
---|
459 | ioctl(fd, TIOCSWINSZ, &wsinfo); |
---|
460 | restore_term = 0; |
---|
461 | } |
---|
462 | */} |
---|
463 | |
---|
464 | /* |
---|
465 | * - control the DTR line on the serial port. |
---|
466 | * This is called from die(), so it shouldn't call die(). |
---|
467 | */ |
---|
468 | void |
---|
469 | setdtr(fd, on) |
---|
470 | int fd, on; |
---|
471 | { |
---|
472 | int modembits = TIOCM_DTR; |
---|
473 | |
---|
474 | ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits); |
---|
475 | } |
---|
476 | |
---|
477 | |
---|
478 | /* |
---|
479 | * open_ppp_loopback - open the device we use for getting |
---|
480 | * packets in demand mode, and connect it to a ppp interface. |
---|
481 | * Here we use a pty. |
---|
482 | */ |
---|
483 | void |
---|
484 | open_ppp_loopback() |
---|
485 | { |
---|
486 | |
---|
487 | } |
---|
488 | |
---|
489 | |
---|
490 | /* |
---|
491 | * output - Output PPP packet. |
---|
492 | */ |
---|
493 | void |
---|
494 | output(unit, p, len) |
---|
495 | int unit; |
---|
496 | u_char *p; |
---|
497 | int len; |
---|
498 | { |
---|
499 | if (debug); |
---|
500 | syslog(LOG_DEBUG, "sent %d bytes ", len ); |
---|
501 | |
---|
502 | if (write(ttyfd, p, len) < 0) { |
---|
503 | /* if (errno != EIO) |
---|
504 | syslog(LOG_ERR, "write: %m"); |
---|
505 | */ } |
---|
506 | } |
---|
507 | |
---|
508 | extern int rtems_bsdnet_microseconds_per_tick; |
---|
509 | #include <rtems.h> |
---|
510 | #include <rtems/rtems_bsdnet.h> |
---|
511 | /* |
---|
512 | * wait_input - wait until there is data available on ttyfd, |
---|
513 | * for the length of time specified by *timo (indefinite |
---|
514 | * if timo is NULL). |
---|
515 | */ |
---|
516 | |
---|
517 | /* |
---|
518 | * wait_loop_output - wait until there is data available on the |
---|
519 | * loopback, for the length of time specified by *timo (indefinite |
---|
520 | * if timo is NULL). |
---|
521 | */ |
---|
522 | void |
---|
523 | wait_loop_output(timo) |
---|
524 | struct timeval *timo; |
---|
525 | { |
---|
526 | /* |
---|
527 | fd_set ready; |
---|
528 | int n; |
---|
529 | */ |
---|
530 | |
---|
531 | /* FD_ZERO(&ready); |
---|
532 | FD_SET(loop_master, &ready); |
---|
533 | n = select(loop_master + 1, &ready, NULL, &ready, timo); |
---|
534 | if (n < 0 && errno != EINTR) { |
---|
535 | */ |
---|
536 | /* syslog(LOG_ERR, "select: %m");*/ |
---|
537 | /* die(1); |
---|
538 | } |
---|
539 | */ |
---|
540 | } |
---|
541 | |
---|
542 | |
---|
543 | /* |
---|
544 | * wait_time - wait for a given length of time or until a |
---|
545 | * signal is received. |
---|
546 | */ |
---|
547 | void |
---|
548 | wait_time(timo) |
---|
549 | struct timeval *timo; |
---|
550 | { |
---|
551 | rtems_status_code status; |
---|
552 | rtems_interval ticks; |
---|
553 | |
---|
554 | ticks = |
---|
555 | (timo->tv_sec*1000000+timo->tv_usec)/rtems_bsdnet_microseconds_per_tick; |
---|
556 | status = rtems_task_wake_after( ticks ); |
---|
557 | } |
---|
558 | |
---|
559 | |
---|
560 | /* |
---|
561 | * read_packet - get a PPP packet from the serial device. |
---|
562 | */ |
---|
563 | int |
---|
564 | read_packet(buf) |
---|
565 | u_char *buf; |
---|
566 | { |
---|
567 | int len; |
---|
568 | |
---|
569 | if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) { |
---|
570 | if (errno == EWOULDBLOCK || errno == EINTR || errno == ETIMEDOUT) |
---|
571 | return -1; |
---|
572 | syslog(LOG_ERR, "read: %m"); |
---|
573 | } |
---|
574 | return len; |
---|
575 | } |
---|
576 | |
---|
577 | |
---|
578 | /* |
---|
579 | * get_loop_output - read characters from the loopback, form them |
---|
580 | * into frames, and detect when we want to bring the real link up. |
---|
581 | * Return value is 1 if we need to bring up the link, 0 otherwise. |
---|
582 | */ |
---|
583 | int |
---|
584 | get_loop_output() |
---|
585 | { |
---|
586 | int rv = 0; |
---|
587 | int n; |
---|
588 | |
---|
589 | while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) { |
---|
590 | if (loop_chars(inbuf, n)) |
---|
591 | rv = 1; |
---|
592 | } |
---|
593 | |
---|
594 | if (n == 0) { |
---|
595 | syslog(LOG_ERR, "eof on loopback"); |
---|
596 | die(1); |
---|
597 | } else if (errno != EWOULDBLOCK){ |
---|
598 | syslog(LOG_ERR, "read from loopback: %m"); |
---|
599 | die(1); |
---|
600 | } |
---|
601 | |
---|
602 | return rv; |
---|
603 | } |
---|
604 | |
---|
605 | |
---|
606 | /* |
---|
607 | * ppp_send_config - configure the transmit characteristics of |
---|
608 | * the ppp interface. |
---|
609 | */ |
---|
610 | void |
---|
611 | ppp_send_config(unit, mtu, asyncmap, pcomp, accomp) |
---|
612 | int unit, mtu; |
---|
613 | u_int32_t asyncmap; |
---|
614 | int pcomp, accomp; |
---|
615 | { |
---|
616 | u_int x; |
---|
617 | struct ifreq ifr; |
---|
618 | |
---|
619 | strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); |
---|
620 | ifr.ifr_mtu = mtu; |
---|
621 | if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) { |
---|
622 | syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m"); |
---|
623 | quit(); |
---|
624 | } |
---|
625 | |
---|
626 | if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) { |
---|
627 | syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m"); |
---|
628 | quit(); |
---|
629 | } |
---|
630 | |
---|
631 | if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) { |
---|
632 | syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m"); |
---|
633 | quit(); |
---|
634 | } |
---|
635 | x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT; |
---|
636 | x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC; |
---|
637 | if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) { |
---|
638 | syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m"); |
---|
639 | quit(); |
---|
640 | } |
---|
641 | } |
---|
642 | |
---|
643 | |
---|
644 | /* |
---|
645 | * ppp_set_xaccm - set the extended transmit ACCM for the interface. |
---|
646 | */ |
---|
647 | void |
---|
648 | ppp_set_xaccm(unit, accm) |
---|
649 | int unit; |
---|
650 | ext_accm accm; |
---|
651 | { |
---|
652 | if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY) |
---|
653 | /*syslog(LOG_WARNING, "ioctl(set extended ACCM): %m")*/; |
---|
654 | } |
---|
655 | |
---|
656 | |
---|
657 | /* |
---|
658 | * ppp_recv_config - configure the receive-side characteristics of |
---|
659 | * the ppp interface. |
---|
660 | */ |
---|
661 | void |
---|
662 | ppp_recv_config(unit, mru, asyncmap, pcomp, accomp) |
---|
663 | int unit, mru; |
---|
664 | u_int32_t asyncmap; |
---|
665 | int pcomp, accomp; |
---|
666 | { |
---|
667 | /* |
---|
668 | int x; |
---|
669 | */ |
---|
670 | |
---|
671 | /* if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) { |
---|
672 | syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m"); |
---|
673 | quit(); |
---|
674 | } |
---|
675 | */ |
---|
676 | /* if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) { |
---|
677 | syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m"); |
---|
678 | quit(); |
---|
679 | } |
---|
680 | */ |
---|
681 | /* if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) { |
---|
682 | syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m"); |
---|
683 | quit(); |
---|
684 | } |
---|
685 | */ |
---|
686 | /* x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC; |
---|
687 | if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) { |
---|
688 | syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m"); |
---|
689 | quit(); |
---|
690 | } |
---|
691 | */ |
---|
692 | } |
---|
693 | |
---|
694 | /* |
---|
695 | * ccp_test - ask kernel whether a given compression method |
---|
696 | * is acceptable for use. Returns 1 if the method and parameters |
---|
697 | * are OK, 0 if the method is known but the parameters are not OK |
---|
698 | * (e.g. code size should be reduced), or -1 if the method is unknown. |
---|
699 | */ |
---|
700 | int |
---|
701 | ccp_test(unit, opt_ptr, opt_len, for_transmit) |
---|
702 | int unit, opt_len, for_transmit; |
---|
703 | u_char *opt_ptr; |
---|
704 | { |
---|
705 | struct ppp_option_data data; |
---|
706 | |
---|
707 | data.ptr = opt_ptr; |
---|
708 | data.length = opt_len; |
---|
709 | data.transmit = for_transmit; |
---|
710 | if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0) |
---|
711 | return 1; |
---|
712 | return (errno == ENOBUFS)? 0: -1; |
---|
713 | } |
---|
714 | |
---|
715 | /* |
---|
716 | * ccp_flags_set - inform kernel about the current state of CCP. |
---|
717 | */ |
---|
718 | void |
---|
719 | ccp_flags_set(unit, isopen, isup) |
---|
720 | int unit, isopen, isup; |
---|
721 | { |
---|
722 | int x; |
---|
723 | |
---|
724 | if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) { |
---|
725 | /* syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m"); |
---|
726 | */ return; |
---|
727 | } |
---|
728 | x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN; |
---|
729 | x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP; |
---|
730 | if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) |
---|
731 | /* syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m")*/; |
---|
732 | } |
---|
733 | |
---|
734 | /* |
---|
735 | * ccp_fatal_error - returns 1 if decompression was disabled as a |
---|
736 | * result of an error detected after decompression of a packet, |
---|
737 | * 0 otherwise. This is necessary because of patent nonsense. |
---|
738 | */ |
---|
739 | int |
---|
740 | ccp_fatal_error(unit) |
---|
741 | int unit; |
---|
742 | { |
---|
743 | int x; |
---|
744 | |
---|
745 | if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) { |
---|
746 | /* syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");*/ |
---|
747 | return 0; |
---|
748 | } |
---|
749 | return x & SC_DC_FERROR; |
---|
750 | } |
---|
751 | |
---|
752 | /* |
---|
753 | * get_idle_time - return how long the link has been idle. |
---|
754 | */ |
---|
755 | int |
---|
756 | get_idle_time(u, ip) |
---|
757 | int u; |
---|
758 | struct ppp_idle *ip; |
---|
759 | { |
---|
760 | return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0; |
---|
761 | } |
---|
762 | |
---|
763 | |
---|
764 | #ifdef PPP_FILTER |
---|
765 | /* |
---|
766 | * set_filters - transfer the pass and active filters to the kernel. |
---|
767 | */ |
---|
768 | int |
---|
769 | set_filters(pass, active) |
---|
770 | struct bpf_program *pass, *active; |
---|
771 | { |
---|
772 | int ret = 1; |
---|
773 | |
---|
774 | if (pass->bf_len > 0) { |
---|
775 | if (ioctl(ppp_fd, PPPIOCSPASS, pass) < 0) { |
---|
776 | syslog(LOG_ERR, "Couldn't set pass-filter in kernel: %m"); |
---|
777 | ret = 0; |
---|
778 | } |
---|
779 | } |
---|
780 | if (active->bf_len > 0) { |
---|
781 | if (ioctl(ppp_fd, PPPIOCSACTIVE, active) < 0) { |
---|
782 | syslog(LOG_ERR, "Couldn't set active-filter in kernel: %m"); |
---|
783 | ret = 0; |
---|
784 | } |
---|
785 | } |
---|
786 | return ret; |
---|
787 | } |
---|
788 | #endif |
---|
789 | |
---|
790 | /* |
---|
791 | * sifvjcomp - config tcp header compression |
---|
792 | */ |
---|
793 | int |
---|
794 | sifvjcomp(u, vjcomp, cidcomp, maxcid) |
---|
795 | int u, vjcomp, cidcomp, maxcid; |
---|
796 | { |
---|
797 | u_int x; |
---|
798 | |
---|
799 | if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) { |
---|
800 | syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m"); |
---|
801 | return 0; |
---|
802 | } |
---|
803 | x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP; |
---|
804 | x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID; |
---|
805 | if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) { |
---|
806 | syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m"); |
---|
807 | return 0; |
---|
808 | } |
---|
809 | if (vjcomp && ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) { |
---|
810 | syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m"); |
---|
811 | return 0; |
---|
812 | } |
---|
813 | return 1; |
---|
814 | } |
---|
815 | |
---|
816 | /* |
---|
817 | * sifup - Config the interface up and enable IP packets to pass. |
---|
818 | */ |
---|
819 | int |
---|
820 | sifup(u) |
---|
821 | int u; |
---|
822 | { |
---|
823 | struct ifreq ifr; |
---|
824 | |
---|
825 | strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); |
---|
826 | if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) { |
---|
827 | syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m"); |
---|
828 | return 0; |
---|
829 | } |
---|
830 | ifr.ifr_flags |= IFF_UP; |
---|
831 | if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) { |
---|
832 | syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m"); |
---|
833 | return 0; |
---|
834 | } |
---|
835 | if_is_up = 1; |
---|
836 | return 1; |
---|
837 | } |
---|
838 | |
---|
839 | /* |
---|
840 | * sifnpmode - Set the mode for handling packets for a given NP. |
---|
841 | */ |
---|
842 | int |
---|
843 | sifnpmode(u, proto, mode) |
---|
844 | int u; |
---|
845 | int proto; |
---|
846 | enum NPmode mode; |
---|
847 | { |
---|
848 | struct npioctl npi; |
---|
849 | |
---|
850 | npi.protocol = proto; |
---|
851 | npi.mode = mode; |
---|
852 | if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) { |
---|
853 | /* syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);*/ |
---|
854 | return 0; |
---|
855 | } |
---|
856 | return 1; |
---|
857 | } |
---|
858 | |
---|
859 | /* |
---|
860 | * sifdown - Config the interface down and disable IP. |
---|
861 | */ |
---|
862 | int |
---|
863 | sifdown(u) |
---|
864 | int u; |
---|
865 | { |
---|
866 | struct ifreq ifr; |
---|
867 | int rv; |
---|
868 | struct npioctl npi; |
---|
869 | |
---|
870 | rv = 1; |
---|
871 | npi.protocol = PPP_IP; |
---|
872 | npi.mode = NPMODE_ERROR; |
---|
873 | ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi); |
---|
874 | /* ignore errors, because ppp_fd might have been closed by now. */ |
---|
875 | |
---|
876 | strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); |
---|
877 | if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) { |
---|
878 | /* syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");*/ |
---|
879 | rv = 0; |
---|
880 | } else { |
---|
881 | ifr.ifr_flags &= ~IFF_UP; |
---|
882 | if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) { |
---|
883 | /* syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");*/ |
---|
884 | rv = 0; |
---|
885 | } else |
---|
886 | if_is_up = 0; |
---|
887 | } |
---|
888 | return rv; |
---|
889 | } |
---|
890 | |
---|
891 | /* |
---|
892 | * SET_SA_FAMILY - set the sa_family field of a struct sockaddr, |
---|
893 | * if it exists. |
---|
894 | */ |
---|
895 | #define SET_SA_FAMILY(addr, family) \ |
---|
896 | BZERO((char *) &(addr), sizeof(addr)); \ |
---|
897 | addr.sa_family = (family); \ |
---|
898 | addr.sa_len = sizeof(addr); |
---|
899 | |
---|
900 | /* |
---|
901 | * sifaddr - Config the interface IP addresses and netmask. |
---|
902 | */ |
---|
903 | int |
---|
904 | sifaddr(u, o, h, m) |
---|
905 | int u; |
---|
906 | u_int32_t o, h, m; |
---|
907 | { |
---|
908 | struct ifaliasreq ifra; |
---|
909 | struct ifreq ifr; |
---|
910 | /* struct sockaddr_in address; |
---|
911 | struct sockaddr_in netmask; |
---|
912 | struct sockaddr_in broadcast; |
---|
913 | |
---|
914 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
---|
915 | memset (&address, '\0', sizeof address); |
---|
916 | address.sin_len = sizeof address; |
---|
917 | address.sin_family = AF_INET; |
---|
918 | address.sin_addr.s_addr = o; |
---|
919 | memcpy (&ifr.ifr_addr, &address, sizeof address); |
---|
920 | if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifr) < 0) { |
---|
921 | if (errno != EADDRNOTAVAIL) |
---|
922 | syslog(LOG_WARNING, "Couldn't remove interface address: %m"); |
---|
923 | } |
---|
924 | |
---|
925 | if (ioctl (sockfd, SIOCSIFADDR, &ifr) < 0) |
---|
926 | syslog(LOG_ERR, "Can't set address: "); |
---|
927 | |
---|
928 | |
---|
929 | memset (&netmask, '\0', sizeof netmask); |
---|
930 | netmask.sin_len = sizeof netmask; |
---|
931 | netmask.sin_family = AF_INET; |
---|
932 | netmask.sin_addr.s_addr = m ; |
---|
933 | memcpy (&ifr.ifr_addr, &netmask, sizeof netmask); |
---|
934 | |
---|
935 | if (ioctl (sockfd, SIOCSIFNETMASK, &ifr) < 0) |
---|
936 | syslog(LOG_ERR,"Can't set netmask: "); |
---|
937 | memset (&broadcast, '\0', sizeof broadcast); |
---|
938 | broadcast.sin_len = sizeof broadcast; |
---|
939 | broadcast.sin_family = AF_INET; |
---|
940 | broadcast.sin_addr.s_addr = h; |
---|
941 | memcpy (&ifr.ifr_broadaddr, &broadcast, sizeof broadcast); |
---|
942 | if (ioctl (sockfd, SIOCSIFBRDADDR, &ifr) < 0) |
---|
943 | syslog(LOG_ERR,"Can't set broadcast address: "); |
---|
944 | |
---|
945 | */ |
---|
946 | strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name)); |
---|
947 | SET_SA_FAMILY(ifra.ifra_addr, AF_INET); |
---|
948 | ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o; |
---|
949 | SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET); |
---|
950 | ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h; |
---|
951 | if (m != 0) { |
---|
952 | SET_SA_FAMILY(ifra.ifra_mask, AF_INET); |
---|
953 | ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m; |
---|
954 | } else |
---|
955 | BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask)); |
---|
956 | BZERO(&ifr, sizeof(ifr)); |
---|
957 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
---|
958 | |
---|
959 | if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifr) < 0) { |
---|
960 | /* if (errno != EADDRNOTAVAIL) |
---|
961 | syslog(LOG_WARNING, "Couldn't remove interface address: %m"); |
---|
962 | */ |
---|
963 | } |
---|
964 | |
---|
965 | |
---|
966 | |
---|
967 | if (ioctl(sockfd, SIOCAIFADDR, (caddr_t) &ifra) < 0) { |
---|
968 | if (errno != EEXIST) { |
---|
969 | syslog(LOG_ERR, "Couldn't set interface address: %m"); |
---|
970 | return 0; |
---|
971 | } |
---|
972 | /* syslog(LOG_WARNING, |
---|
973 | "Couldn't set interface address: Address %s already exists", |
---|
974 | ip_ntoa(o)); |
---|
975 | */ } |
---|
976 | ifaddrs[0] = o; |
---|
977 | ifaddrs[1] = h; |
---|
978 | return 1; |
---|
979 | } |
---|
980 | |
---|
981 | /* |
---|
982 | * cifaddr - Clear the interface IP addresses, and delete routes |
---|
983 | * through the interface if possible. |
---|
984 | */ |
---|
985 | int |
---|
986 | cifaddr(u, o, h) |
---|
987 | int u; |
---|
988 | u_int32_t o, h; |
---|
989 | { |
---|
990 | struct ifaliasreq ifra; |
---|
991 | |
---|
992 | ifaddrs[0] = 0; |
---|
993 | strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name)); |
---|
994 | SET_SA_FAMILY(ifra.ifra_addr, AF_INET); |
---|
995 | ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o; |
---|
996 | SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET); |
---|
997 | ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h; |
---|
998 | BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask)); |
---|
999 | if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifra) < 0) { |
---|
1000 | /* if (errno != EADDRNOTAVAIL) |
---|
1001 | syslog(LOG_WARNING, "Couldn't delete interface address: %m"); |
---|
1002 | */ return 0; |
---|
1003 | } |
---|
1004 | return 1; |
---|
1005 | } |
---|
1006 | |
---|
1007 | /* |
---|
1008 | * sifdefaultroute - assign a default route through the address given. |
---|
1009 | */ |
---|
1010 | int |
---|
1011 | sifdefaultroute(u, l, g) |
---|
1012 | int u; |
---|
1013 | u_int32_t l, g; |
---|
1014 | { |
---|
1015 | return dodefaultroute(g, 's'); |
---|
1016 | } |
---|
1017 | |
---|
1018 | /* |
---|
1019 | * cifdefaultroute - delete a default route through the address given. |
---|
1020 | */ |
---|
1021 | int |
---|
1022 | cifdefaultroute(u, l, g) |
---|
1023 | int u; |
---|
1024 | u_int32_t l, g; |
---|
1025 | { |
---|
1026 | return dodefaultroute(g, 'c'); |
---|
1027 | } |
---|
1028 | |
---|
1029 | /* |
---|
1030 | * dodefaultroute - talk to a routing socket to add/delete a default route. |
---|
1031 | */ |
---|
1032 | static int |
---|
1033 | dodefaultroute(g, cmd) |
---|
1034 | u_int32_t g; |
---|
1035 | int cmd; |
---|
1036 | { |
---|
1037 | /* int routes;*/ |
---|
1038 | |
---|
1039 | struct sockaddr_in address; |
---|
1040 | struct sockaddr_in netmask; |
---|
1041 | /* struct sockaddr_in broadcast; */ |
---|
1042 | struct sockaddr_in gateway; |
---|
1043 | |
---|
1044 | /* struct { |
---|
1045 | struct rt_msghdr hdr; |
---|
1046 | struct sockaddr_in dst; |
---|
1047 | struct sockaddr_in gway; |
---|
1048 | struct sockaddr_in mask; |
---|
1049 | } rtmsg; |
---|
1050 | */ |
---|
1051 | /* if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) { |
---|
1052 | syslog(LOG_ERR, "Couldn't %s default route: socket: %m", |
---|
1053 | cmd=='s'? "add": "delete"); |
---|
1054 | return 0; |
---|
1055 | } |
---|
1056 | */ |
---|
1057 | memset((void *) &address, 0, sizeof(address)); |
---|
1058 | address.sin_len = sizeof address; |
---|
1059 | address.sin_family = AF_INET; |
---|
1060 | address.sin_addr.s_addr = INADDR_ANY; |
---|
1061 | |
---|
1062 | memset((void *) &netmask, 0, sizeof(netmask)); |
---|
1063 | netmask.sin_len = sizeof netmask; |
---|
1064 | netmask.sin_addr.s_addr = INADDR_ANY; |
---|
1065 | netmask.sin_family = AF_INET; |
---|
1066 | |
---|
1067 | |
---|
1068 | if (cmd=='s') |
---|
1069 | { |
---|
1070 | memset((void *) &gateway, 0, sizeof(gateway)); |
---|
1071 | gateway.sin_len = sizeof gateway; |
---|
1072 | gateway.sin_family = AF_INET; |
---|
1073 | gateway.sin_addr.s_addr = htons(g) ; |
---|
1074 | |
---|
1075 | |
---|
1076 | if (rtems_bsdnet_rtrequest (RTM_ADD, |
---|
1077 | (struct sockaddr *)&address, |
---|
1078 | (struct sockaddr *)&gateway, |
---|
1079 | (struct sockaddr *)&netmask, |
---|
1080 | (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL) < 0) |
---|
1081 | /*syslog(LOG_WARNING,"Can't set default route: ")*/ |
---|
1082 | ; |
---|
1083 | } |
---|
1084 | else |
---|
1085 | { |
---|
1086 | memset((void *) &gateway, 0, sizeof(gateway)); |
---|
1087 | gateway.sin_len = sizeof gateway; |
---|
1088 | gateway.sin_family = AF_INET; |
---|
1089 | gateway.sin_addr.s_addr = INADDR_ANY; |
---|
1090 | |
---|
1091 | if (rtems_bsdnet_rtrequest (RTM_DELETE, |
---|
1092 | (struct sockaddr *)&address, |
---|
1093 | (struct sockaddr *)&gateway, |
---|
1094 | (struct sockaddr *)&netmask, |
---|
1095 | (RTF_UP | RTF_STATIC), NULL) < 0) |
---|
1096 | /*syslog(LOG_WARNING,"Can't set default route: ")*/ |
---|
1097 | ; |
---|
1098 | } |
---|
1099 | |
---|
1100 | /* |
---|
1101 | rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE; |
---|
1102 | rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY; |
---|
1103 | rtmsg.hdr.rtm_version = RTM_VERSION; |
---|
1104 | rtmsg.hdr.rtm_seq = ++rtm_seq; |
---|
1105 | rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; |
---|
1106 | rtmsg.dst.sin_len = sizeof(rtmsg.dst); |
---|
1107 | rtmsg.dst.sin_family = AF_INET; |
---|
1108 | rtmsg.gway.sin_len = sizeof(rtmsg.gway); |
---|
1109 | rtmsg.gway.sin_family = AF_INET; |
---|
1110 | rtmsg.gway.sin_addr.s_addr = g; |
---|
1111 | rtmsg.mask.sin_len = sizeof(rtmsg.dst); |
---|
1112 | rtmsg.mask.sin_family = AF_INET; |
---|
1113 | |
---|
1114 | rtmsg.hdr.rtm_msglen = sizeof(rtmsg); |
---|
1115 | |
---|
1116 | |
---|
1117 | if (write(routes, &rtmsg, sizeof(rtmsg)) < 0) { |
---|
1118 | syslog(LOG_ERR, "Couldn't %s default route: %m", |
---|
1119 | cmd=='s'? "add": "delete"); |
---|
1120 | close(routes); |
---|
1121 | return 0; |
---|
1122 | } |
---|
1123 | |
---|
1124 | close(routes);*/ |
---|
1125 | default_route_gateway = (cmd == 's')? g: 0; |
---|
1126 | return 1; |
---|
1127 | } |
---|
1128 | |
---|
1129 | #if RTM_VERSION >= 3 |
---|
1130 | |
---|
1131 | /* |
---|
1132 | * sifproxyarp - Make a proxy ARP entry for the peer. |
---|
1133 | */ |
---|
1134 | static struct { |
---|
1135 | struct rt_msghdr hdr; |
---|
1136 | struct sockaddr_inarp dst; |
---|
1137 | struct sockaddr_dl hwa; |
---|
1138 | char extra[128]; |
---|
1139 | } arpmsg; |
---|
1140 | |
---|
1141 | static int arpmsg_valid; |
---|
1142 | |
---|
1143 | int |
---|
1144 | sifproxyarp(unit, hisaddr) |
---|
1145 | int unit; |
---|
1146 | u_int32_t hisaddr; |
---|
1147 | { |
---|
1148 | int routes; |
---|
1149 | return 0; |
---|
1150 | |
---|
1151 | |
---|
1152 | /* |
---|
1153 | * Get the hardware address of an interface on the same subnet |
---|
1154 | * as our local address. |
---|
1155 | */ |
---|
1156 | memset(&arpmsg, 0, sizeof(arpmsg)); |
---|
1157 | if (!get_ether_addr(hisaddr, &arpmsg.hwa)) { |
---|
1158 | /* syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP"); |
---|
1159 | */ return 0; |
---|
1160 | } |
---|
1161 | |
---|
1162 | if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) { |
---|
1163 | /* syslog(LOG_ERR, "Couldn't add proxy arp entry: socket: %m"); |
---|
1164 | */ return 0; |
---|
1165 | } |
---|
1166 | |
---|
1167 | arpmsg.hdr.rtm_type = RTM_ADD; |
---|
1168 | arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC; |
---|
1169 | arpmsg.hdr.rtm_version = RTM_VERSION; |
---|
1170 | arpmsg.hdr.rtm_seq = ++rtm_seq; |
---|
1171 | arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY; |
---|
1172 | arpmsg.hdr.rtm_inits = RTV_EXPIRE; |
---|
1173 | arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp); |
---|
1174 | arpmsg.dst.sin_family = AF_INET; |
---|
1175 | arpmsg.dst.sin_addr.s_addr = hisaddr; |
---|
1176 | arpmsg.dst.sin_other = SIN_PROXY; |
---|
1177 | |
---|
1178 | arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg |
---|
1179 | + arpmsg.hwa.sdl_len; |
---|
1180 | if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) { |
---|
1181 | /* syslog(LOG_ERR, "Couldn't add proxy arp entry: %m"); |
---|
1182 | */ close(routes); |
---|
1183 | return 0; |
---|
1184 | } |
---|
1185 | |
---|
1186 | close(routes); |
---|
1187 | arpmsg_valid = 1; |
---|
1188 | proxy_arp_addr = hisaddr; |
---|
1189 | return 1; |
---|
1190 | } |
---|
1191 | |
---|
1192 | /* |
---|
1193 | * cifproxyarp - Delete the proxy ARP entry for the peer. |
---|
1194 | */ |
---|
1195 | int |
---|
1196 | cifproxyarp(unit, hisaddr) |
---|
1197 | int unit; |
---|
1198 | u_int32_t hisaddr; |
---|
1199 | { |
---|
1200 | int routes; |
---|
1201 | |
---|
1202 | if (!arpmsg_valid) |
---|
1203 | return 0; |
---|
1204 | arpmsg_valid = 0; |
---|
1205 | |
---|
1206 | arpmsg.hdr.rtm_type = RTM_DELETE; |
---|
1207 | arpmsg.hdr.rtm_seq = ++rtm_seq; |
---|
1208 | |
---|
1209 | if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) { |
---|
1210 | syslog(LOG_ERR, "Couldn't delete proxy arp entry: socket: %m"); |
---|
1211 | return 0; |
---|
1212 | } |
---|
1213 | |
---|
1214 | if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) { |
---|
1215 | syslog(LOG_ERR, "Couldn't delete proxy arp entry: %m"); |
---|
1216 | close(routes); |
---|
1217 | return 0; |
---|
1218 | } |
---|
1219 | |
---|
1220 | close(routes); |
---|
1221 | proxy_arp_addr = 0; |
---|
1222 | return 1; |
---|
1223 | } |
---|
1224 | |
---|
1225 | #else /* RTM_VERSION */ |
---|
1226 | |
---|
1227 | /* |
---|
1228 | * sifproxyarp - Make a proxy ARP entry for the peer. |
---|
1229 | */ |
---|
1230 | int |
---|
1231 | sifproxyarp(unit, hisaddr) |
---|
1232 | int unit; |
---|
1233 | u_int32_t hisaddr; |
---|
1234 | { |
---|
1235 | struct arpreq arpreq; |
---|
1236 | struct { |
---|
1237 | struct sockaddr_dl sdl; |
---|
1238 | char space[128]; |
---|
1239 | } dls; |
---|
1240 | |
---|
1241 | BZERO(&arpreq, sizeof(arpreq)); |
---|
1242 | |
---|
1243 | /* |
---|
1244 | * Get the hardware address of an interface on the same subnet |
---|
1245 | * as our local address. |
---|
1246 | */ |
---|
1247 | if (!get_ether_addr(hisaddr, &dls.sdl)) { |
---|
1248 | syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP"); |
---|
1249 | return 0; |
---|
1250 | } |
---|
1251 | |
---|
1252 | arpreq.arp_ha.sa_len = sizeof(struct sockaddr); |
---|
1253 | arpreq.arp_ha.sa_family = AF_UNSPEC; |
---|
1254 | BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen); |
---|
1255 | SET_SA_FAMILY(arpreq.arp_pa, AF_INET); |
---|
1256 | ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr; |
---|
1257 | arpreq.arp_flags = ATF_PERM | ATF_PUBL; |
---|
1258 | if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) { |
---|
1259 | syslog(LOG_ERR, "Couldn't add proxy arp entry: %m"); |
---|
1260 | return 0; |
---|
1261 | } |
---|
1262 | |
---|
1263 | proxy_arp_addr = hisaddr; |
---|
1264 | return 1; |
---|
1265 | } |
---|
1266 | |
---|
1267 | /* |
---|
1268 | * cifproxyarp - Delete the proxy ARP entry for the peer. |
---|
1269 | */ |
---|
1270 | int |
---|
1271 | cifproxyarp(unit, hisaddr) |
---|
1272 | int unit; |
---|
1273 | u_int32_t hisaddr; |
---|
1274 | { |
---|
1275 | struct arpreq arpreq; |
---|
1276 | |
---|
1277 | BZERO(&arpreq, sizeof(arpreq)); |
---|
1278 | SET_SA_FAMILY(arpreq.arp_pa, AF_INET); |
---|
1279 | ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr; |
---|
1280 | if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) { |
---|
1281 | syslog(LOG_WARNING, "Couldn't delete proxy arp entry: %m"); |
---|
1282 | return 0; |
---|
1283 | } |
---|
1284 | proxy_arp_addr = 0; |
---|
1285 | return 1; |
---|
1286 | } |
---|
1287 | #endif /* RTM_VERSION */ |
---|
1288 | |
---|
1289 | |
---|
1290 | /* |
---|
1291 | * get_ether_addr - get the hardware address of an interface on the |
---|
1292 | * the same subnet as ipaddr. |
---|
1293 | */ |
---|
1294 | #define MAX_IFS 32 |
---|
1295 | |
---|
1296 | static int |
---|
1297 | get_ether_addr(ipaddr, hwaddr) |
---|
1298 | u_int32_t ipaddr; |
---|
1299 | struct sockaddr_dl *hwaddr; |
---|
1300 | { |
---|
1301 | struct ifreq *ifr, *ifend, *ifp; |
---|
1302 | u_int32_t ina, mask; |
---|
1303 | struct sockaddr_dl *dla; |
---|
1304 | struct ifreq ifreq; |
---|
1305 | struct ifconf ifc; |
---|
1306 | struct ifreq ifs[MAX_IFS]; |
---|
1307 | |
---|
1308 | ifc.ifc_len = sizeof(ifs); |
---|
1309 | ifc.ifc_req = ifs; |
---|
1310 | if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) { |
---|
1311 | syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m"); |
---|
1312 | return 0; |
---|
1313 | } |
---|
1314 | |
---|
1315 | /* |
---|
1316 | * Scan through looking for an interface with an Internet |
---|
1317 | * address on the same subnet as `ipaddr'. |
---|
1318 | */ |
---|
1319 | ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len); |
---|
1320 | for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *) |
---|
1321 | ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) { |
---|
1322 | if (ifr->ifr_addr.sa_family == AF_INET) { |
---|
1323 | ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr; |
---|
1324 | strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name)); |
---|
1325 | /* |
---|
1326 | * Check that the interface is up, and not point-to-point |
---|
1327 | * or loopback. |
---|
1328 | */ |
---|
1329 | if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0) |
---|
1330 | continue; |
---|
1331 | if ((ifreq.ifr_flags & |
---|
1332 | (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP)) |
---|
1333 | != (IFF_UP|IFF_BROADCAST)) |
---|
1334 | continue; |
---|
1335 | /* |
---|
1336 | * Get its netmask and check that it's on the right subnet. |
---|
1337 | */ |
---|
1338 | if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0) |
---|
1339 | continue; |
---|
1340 | mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr; |
---|
1341 | if ((ipaddr & mask) != (ina & mask)) |
---|
1342 | continue; |
---|
1343 | |
---|
1344 | break; |
---|
1345 | } |
---|
1346 | } |
---|
1347 | |
---|
1348 | if (ifr >= ifend) |
---|
1349 | return 0; |
---|
1350 | syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name); |
---|
1351 | |
---|
1352 | /* |
---|
1353 | * Now scan through again looking for a link-level address |
---|
1354 | * for this interface. |
---|
1355 | */ |
---|
1356 | ifp = ifr; |
---|
1357 | for (ifr = ifc.ifc_req; ifr < ifend; ) { |
---|
1358 | if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 |
---|
1359 | && ifr->ifr_addr.sa_family == AF_LINK) { |
---|
1360 | /* |
---|
1361 | * Found the link-level address - copy it out |
---|
1362 | */ |
---|
1363 | dla = (struct sockaddr_dl *) &ifr->ifr_addr; |
---|
1364 | BCOPY(dla, hwaddr, dla->sdl_len); |
---|
1365 | return 1; |
---|
1366 | } |
---|
1367 | ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len); |
---|
1368 | } |
---|
1369 | |
---|
1370 | return 0; |
---|
1371 | } |
---|
1372 | |
---|
1373 | /* |
---|
1374 | * Return user specified netmask, modified by any mask we might determine |
---|
1375 | * for address `addr' (in network byte order). |
---|
1376 | * Here we scan through the system's list of interfaces, looking for |
---|
1377 | * any non-point-to-point interfaces which might appear to be on the same |
---|
1378 | * network as `addr'. If we find any, we OR in their netmask to the |
---|
1379 | * user-specified netmask. |
---|
1380 | */ |
---|
1381 | u_int32_t |
---|
1382 | GetMask(addr) |
---|
1383 | u_int32_t addr; |
---|
1384 | { |
---|
1385 | u_int32_t mask, nmask, ina; |
---|
1386 | struct ifreq *ifr, *ifend, ifreq; |
---|
1387 | struct ifconf ifc; |
---|
1388 | struct ifreq ifs[MAX_IFS]; |
---|
1389 | |
---|
1390 | addr = ntohl(addr); |
---|
1391 | if (IN_CLASSA(addr)) /* determine network mask for address class */ |
---|
1392 | nmask = IN_CLASSA_NET; |
---|
1393 | else if (IN_CLASSB(addr)) |
---|
1394 | nmask = IN_CLASSB_NET; |
---|
1395 | else |
---|
1396 | nmask = IN_CLASSC_NET; |
---|
1397 | /* class D nets are disallowed by bad_ip_adrs */ |
---|
1398 | mask = netmask | htonl(nmask); |
---|
1399 | |
---|
1400 | /* |
---|
1401 | * Scan through the system's network interfaces. |
---|
1402 | */ |
---|
1403 | ifc.ifc_len = sizeof(ifs); |
---|
1404 | ifc.ifc_req = ifs; |
---|
1405 | if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) { |
---|
1406 | syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m"); |
---|
1407 | return mask; |
---|
1408 | } |
---|
1409 | ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len); |
---|
1410 | for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *) |
---|
1411 | ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) { |
---|
1412 | /* |
---|
1413 | * Check the interface's internet address. |
---|
1414 | */ |
---|
1415 | if (ifr->ifr_addr.sa_family != AF_INET) |
---|
1416 | continue; |
---|
1417 | ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr; |
---|
1418 | if ((ntohl(ina) & nmask) != (addr & nmask)) |
---|
1419 | continue; |
---|
1420 | /* |
---|
1421 | * Check that the interface is up, and not point-to-point or loopback. |
---|
1422 | */ |
---|
1423 | strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name)); |
---|
1424 | if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0) |
---|
1425 | continue; |
---|
1426 | if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK)) |
---|
1427 | != IFF_UP) |
---|
1428 | continue; |
---|
1429 | /* |
---|
1430 | * Get its netmask and OR it into our mask. |
---|
1431 | */ |
---|
1432 | if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0) |
---|
1433 | continue; |
---|
1434 | mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr; |
---|
1435 | } |
---|
1436 | |
---|
1437 | return mask; |
---|
1438 | } |
---|
1439 | |
---|
1440 | /* |
---|
1441 | * Use the hostid as part of the random number seed. |
---|
1442 | */ |
---|
1443 | int |
---|
1444 | get_host_seed() |
---|
1445 | { |
---|
1446 | return 33; |
---|
1447 | /* return gethostid();*/ |
---|
1448 | } |
---|
1449 | |
---|
1450 | /* |
---|
1451 | * lock - create a lock file for the named lock device |
---|
1452 | */ |
---|
1453 | #define LOCK_PREFIX "/var/spool/lock/LCK.." |
---|
1454 | |
---|
1455 | int |
---|
1456 | lock(dev) |
---|
1457 | char *dev; |
---|
1458 | { |
---|
1459 | /* char hdb_lock_buffer[12]; |
---|
1460 | int fd, pid, n; |
---|
1461 | char *p; |
---|
1462 | |
---|
1463 | if ((p = strrchr(dev, '/')) != NULL) |
---|
1464 | dev = p + 1; |
---|
1465 | lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1); |
---|
1466 | if (lock_file == NULL) |
---|
1467 | novm("lock file name"); |
---|
1468 | strcat(strcpy(lock_file, LOCK_PREFIX), dev); |
---|
1469 | |
---|
1470 | while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) { |
---|
1471 | if (errno == EEXIST |
---|
1472 | && (fd = open(lock_file, O_RDONLY, 0)) >= 0) { |
---|
1473 | |
---|
1474 | n = read(fd, hdb_lock_buffer, 11); |
---|
1475 | if (n <= 0) { |
---|
1476 | syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file); |
---|
1477 | close(fd); |
---|
1478 | } else { |
---|
1479 | hdb_lock_buffer[n] = 0; |
---|
1480 | pid = atoi(hdb_lock_buffer); |
---|
1481 | if (kill(pid, 0) == -1 && errno == ESRCH) { |
---|
1482 | |
---|
1483 | if (unlink(lock_file) == 0) { |
---|
1484 | close(fd); |
---|
1485 | syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)", |
---|
1486 | dev, pid); |
---|
1487 | continue; |
---|
1488 | } else |
---|
1489 | syslog(LOG_WARNING, "Couldn't remove stale lock on %s", |
---|
1490 | dev); |
---|
1491 | } else |
---|
1492 | syslog(LOG_NOTICE, "Device %s is locked by pid %d", |
---|
1493 | dev, pid); |
---|
1494 | } |
---|
1495 | close(fd); |
---|
1496 | } else |
---|
1497 | syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file); |
---|
1498 | free(lock_file); |
---|
1499 | lock_file = NULL; |
---|
1500 | return -1; |
---|
1501 | } |
---|
1502 | |
---|
1503 | sprintf(hdb_lock_buffer, "%10d\n", getpid()); |
---|
1504 | write(fd, hdb_lock_buffer, 11); |
---|
1505 | |
---|
1506 | close(fd); |
---|
1507 | */ return 0; |
---|
1508 | } |
---|
1509 | |
---|
1510 | /* |
---|
1511 | * unlock - remove our lockfile |
---|
1512 | */ |
---|
1513 | void |
---|
1514 | unlock() |
---|
1515 | { |
---|
1516 | /* if (lock_file) { |
---|
1517 | unlink(lock_file); |
---|
1518 | free(lock_file); |
---|
1519 | lock_file = NULL; |
---|
1520 | } |
---|
1521 | */} |
---|