source: rtems/cpukit/pppd/sys-rtems.c @ dd2906e

4.104.114.84.95
Last change on this file since dd2906e was ab1ed86, checked in by Joel Sherrill <joel.sherrill@…>, on 01/06/02 at 20:09:55

2002-02-05 Ralf Corsepius <corsepiu@…>

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