source: rtems/c/src/libnetworking/pppd/sys-rtems.c @ 2f1b930

4.104.114.84.95
Last change on this file since 2f1b930 was 2f1b930, checked in by Joel Sherrill <joel.sherrill@…>, on 08/16/01 at 20:42:09

2001-08-16 Mike Siers <mikes@…>

  • Update of PPPD to 2.3.11 from 2.3.5 and addition of an example application. Mike's notes on the modifications:
    • renamed error() function because of namespace problems
    • removed calls to the exit() funciton
    • removed extra files from the pppd source directory
    • defined pppd task constant values in rtemspppd.h
    • modifyied example code to get actual tick per second value
    • placed the pppd 2.3.11 man page file (pppd.8) into the pppd directory
  • pppd/cbcp.c, pppd/cbcp.h, pppd/main.c, pppd/ppp_tty.c, pppd/pppmain.c, pppd/rtems-ppp.c, pppd/rtems-ppp.c: Deleted.
  • pppd/pppd.8, pppd/rtemsmain.c, pppd/rtemspppd.c, pppd/rtemspppd.h, pppd/sys-rtems.c, pppd/utils.c, pppd/example/Makefile, pppd/example/README, pppd/example/init.c, pppd/example/netconfig.h, pppd/example/ppp.conf, pppd/example/pppdapp.c, pppd/example/system.h: New files.
  • modem/ppp_tty.c, net/if_ppp.h, pppd/Makefile.am, pppd/README, pppd/STATUS, pppd/auth.c, pppd/ccp.c, pppd/ccp.h, pppd/chap.c, pppd/chap.h, pppd/chap_ms.c, pppd/chap_ms.h, pppd/chat.c, pppd/demand.c, pppd/fsm.c, pppd/fsm.h, pppd/ipcp.c, pppd/ipcp.h, pppd/ipxcp.c, pppd/ipxcp.h, pppd/lcp.c, pppd/lcp.h, pppd/magic.c, pppd/magic.h, pppd/options.c, pppd/patchlevel.h, pppd/pathnames.h, pppd/pppd.h, pppd/upap.c, pppd/upap.h: Modified.
  • 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    int    status;
951    struct sockaddr_in address;
952    struct sockaddr_in netmask;
953    struct sockaddr_in gateway;
954
955    memset((void *) &address, 0, sizeof(address));
956    address.sin_len = sizeof address;
957    address.sin_family = AF_INET;
958    address.sin_addr.s_addr = INADDR_ANY;
959
960    memset((void *) &netmask, 0, sizeof(netmask));
961    netmask.sin_len = sizeof netmask;
962    netmask.sin_addr.s_addr = INADDR_ANY; 
963    netmask.sin_family = AF_INET;
964
965    if (cmd=='s') {     
966      memset((void *) &gateway, 0, sizeof(gateway));
967      gateway.sin_len = sizeof gateway;
968      gateway.sin_family = AF_INET;
969      gateway.sin_addr.s_addr = g;
970
971      rtems_bsdnet_rtrequest(RTM_ADD,
972                             (struct sockaddr *)&address,
973                             (struct sockaddr *)&gateway,
974                             (struct sockaddr *)&netmask,
975                             (RTF_UP|RTF_GATEWAY|RTF_STATIC), NULL);
976    }
977    else {
978      memset((void *) &gateway, 0, sizeof(gateway));
979      gateway.sin_len = sizeof gateway;
980      gateway.sin_family = AF_INET;
981      gateway.sin_addr.s_addr =  INADDR_ANY;
982
983      rtems_bsdnet_rtrequest(RTM_DELETE,
984                             (struct sockaddr *)&address,
985                             (struct sockaddr *)&gateway,
986                             (struct sockaddr *)&netmask,
987                             (RTF_UP|RTF_STATIC), NULL);
988    }
989
990    default_route_gateway = (cmd == 's')? g: 0;
991
992    return 1;
993}
994
995#if RTM_VERSION >= 3
996
997/*
998 * sifproxyarp - Make a proxy ARP entry for the peer.
999 */
1000static struct {
1001    struct rt_msghdr            hdr;
1002    struct sockaddr_inarp       dst;
1003    struct sockaddr_dl          hwa;
1004    char                        extra[128];
1005} arpmsg;
1006
1007static int arpmsg_valid;
1008
1009int
1010sifproxyarp(unit, hisaddr)
1011    int unit;
1012    u_int32_t hisaddr;
1013{
1014    int routes;
1015
1016    /*
1017     * Get the hardware address of an interface on the same subnet
1018     * as our local address.
1019     */
1020    memset(&arpmsg, 0, sizeof(arpmsg));
1021    if (!get_ether_addr(hisaddr, &arpmsg.hwa)) {
1022        error("Cannot determine ethernet address for proxy ARP");
1023        return 0;
1024    }
1025
1026    if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
1027        error("Couldn't add proxy arp entry: socket: %m");
1028        return 0;
1029    }
1030
1031    arpmsg.hdr.rtm_type = RTM_ADD;
1032    arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
1033    arpmsg.hdr.rtm_version = RTM_VERSION;
1034    arpmsg.hdr.rtm_seq = ++rtm_seq;
1035    arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
1036    arpmsg.hdr.rtm_inits = RTV_EXPIRE;
1037    arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
1038    arpmsg.dst.sin_family = AF_INET;
1039    arpmsg.dst.sin_addr.s_addr = hisaddr;
1040    arpmsg.dst.sin_other = SIN_PROXY;
1041
1042    arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
1043        + arpmsg.hwa.sdl_len;
1044    if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
1045        error("Couldn't add proxy arp entry: %m");
1046        close(routes);
1047        return 0;
1048    }
1049
1050    close(routes);
1051    arpmsg_valid = 1;
1052    proxy_arp_addr = hisaddr;
1053    return 1;
1054}
1055
1056/*
1057 * cifproxyarp - Delete the proxy ARP entry for the peer.
1058 */
1059int
1060cifproxyarp(unit, hisaddr)
1061    int unit;
1062    u_int32_t hisaddr;
1063{
1064    int routes;
1065
1066    if (!arpmsg_valid)
1067        return 0;
1068    arpmsg_valid = 0;
1069
1070    arpmsg.hdr.rtm_type = RTM_DELETE;
1071    arpmsg.hdr.rtm_seq = ++rtm_seq;
1072
1073    if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
1074        error("Couldn't delete proxy arp entry: socket: %m");
1075        return 0;
1076    }
1077
1078    if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
1079        error("Couldn't delete proxy arp entry: %m");
1080        close(routes);
1081        return 0;
1082    }
1083
1084    close(routes);
1085    proxy_arp_addr = 0;
1086    return 1;
1087}
1088
1089#else   /* RTM_VERSION */
1090
1091/*
1092 * sifproxyarp - Make a proxy ARP entry for the peer.
1093 */
1094int
1095sifproxyarp(unit, hisaddr)
1096    int unit;
1097    u_int32_t hisaddr;
1098{
1099    struct arpreq arpreq;
1100    struct {
1101        struct sockaddr_dl      sdl;
1102        char                    space[128];
1103    } dls;
1104
1105    BZERO(&arpreq, sizeof(arpreq));
1106
1107    /*
1108     * Get the hardware address of an interface on the same subnet
1109     * as our local address.
1110     */
1111    if (!get_ether_addr(hisaddr, &dls.sdl)) {
1112        error("Cannot determine ethernet address for proxy ARP");
1113        return 0;
1114    }
1115
1116    arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
1117    arpreq.arp_ha.sa_family = AF_UNSPEC;
1118    BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
1119    SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1120    ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1121    arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1122    if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1123        error("Couldn't add proxy arp entry: %m");
1124        return 0;
1125    }
1126
1127    proxy_arp_addr = hisaddr;
1128    return 1;
1129}
1130
1131/*
1132 * cifproxyarp - Delete the proxy ARP entry for the peer.
1133 */
1134int
1135cifproxyarp(unit, hisaddr)
1136    int unit;
1137    u_int32_t hisaddr;
1138{
1139    struct arpreq arpreq;
1140
1141    BZERO(&arpreq, sizeof(arpreq));
1142    SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1143    ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1144    if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1145        warn("Couldn't delete proxy arp entry: %m");
1146        return 0;
1147    }
1148    proxy_arp_addr = 0;
1149    return 1;
1150}
1151#endif  /* RTM_VERSION */
1152
1153
1154/*
1155 * get_ether_addr - get the hardware address of an interface on the
1156 * the same subnet as ipaddr.
1157 */
1158#define MAX_IFS         32
1159
1160static int
1161get_ether_addr(ipaddr, hwaddr)
1162    u_int32_t ipaddr;
1163    struct sockaddr_dl *hwaddr;
1164{
1165    struct ifreq *ifr, *ifend, *ifp;
1166    u_int32_t ina, mask;
1167    struct sockaddr_dl *dla;
1168    struct ifreq ifreq;
1169    struct ifconf ifc;
1170    struct ifreq ifs[MAX_IFS];
1171
1172    ifc.ifc_len = sizeof(ifs);
1173    ifc.ifc_req = ifs;
1174    if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1175        error("ioctl(SIOCGIFCONF): %m");
1176        return 0;
1177    }
1178
1179    /*
1180     * Scan through looking for an interface with an Internet
1181     * address on the same subnet as `ipaddr'.
1182     */
1183    ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1184    for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1185                ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1186        if (ifr->ifr_addr.sa_family == AF_INET) {
1187            ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1188            strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1189            /*
1190             * Check that the interface is up, and not point-to-point
1191             * or loopback.
1192             */
1193            if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1194                continue;
1195            if ((ifreq.ifr_flags &
1196                 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1197                 != (IFF_UP|IFF_BROADCAST))
1198                continue;
1199            /*
1200             * Get its netmask and check that it's on the right subnet.
1201             */
1202            if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1203                continue;
1204            mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1205            if ((ipaddr & mask) != (ina & mask))
1206                continue;
1207
1208            break;
1209        }
1210    }
1211
1212    if (ifr >= ifend)
1213        return 0;
1214    info("found interface %s for proxy arp", ifr->ifr_name);
1215
1216    /*
1217     * Now scan through again looking for a link-level address
1218     * for this interface.
1219     */
1220    ifp = ifr;
1221    for (ifr = ifc.ifc_req; ifr < ifend; ) {
1222        if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
1223            && ifr->ifr_addr.sa_family == AF_LINK) {
1224            /*
1225             * Found the link-level address - copy it out
1226             */
1227            dla = (struct sockaddr_dl *) &ifr->ifr_addr;
1228            BCOPY(dla, hwaddr, dla->sdl_len);
1229            return 1;
1230        }
1231        ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
1232    }
1233
1234    return 0;
1235}
1236
1237/*
1238 * Return user specified netmask, modified by any mask we might determine
1239 * for address `addr' (in network byte order).
1240 * Here we scan through the system's list of interfaces, looking for
1241 * any non-point-to-point interfaces which might appear to be on the same
1242 * network as `addr'.  If we find any, we OR in their netmask to the
1243 * user-specified netmask.
1244 */
1245u_int32_t
1246GetMask(addr)
1247    u_int32_t addr;
1248{
1249    u_int32_t mask, nmask, ina;
1250    struct ifreq *ifr, *ifend, ifreq;
1251    struct ifconf ifc;
1252    struct ifreq ifs[MAX_IFS];
1253
1254    addr = ntohl(addr);
1255    if (IN_CLASSA(addr))        /* determine network mask for address class */
1256        nmask = IN_CLASSA_NET;
1257    else if (IN_CLASSB(addr))
1258        nmask = IN_CLASSB_NET;
1259    else
1260        nmask = IN_CLASSC_NET;
1261    /* class D nets are disallowed by bad_ip_adrs */
1262    mask = netmask | htonl(nmask);
1263
1264    /*
1265     * Scan through the system's network interfaces.
1266     */
1267    ifc.ifc_len = sizeof(ifs);
1268    ifc.ifc_req = ifs;
1269    if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1270        warn("ioctl(SIOCGIFCONF): %m");
1271        return mask;
1272    }
1273    ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1274    for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1275                ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1276        /*
1277         * Check the interface's internet address.
1278         */
1279        if (ifr->ifr_addr.sa_family != AF_INET)
1280            continue;
1281        ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1282        if ((ntohl(ina) & nmask) != (addr & nmask))
1283            continue;
1284        /*
1285         * Check that the interface is up, and not point-to-point or loopback.
1286         */
1287        strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1288        if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1289            continue;
1290        if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1291            != IFF_UP)
1292            continue;
1293        /*
1294         * Get its netmask and OR it into our mask.
1295         */
1296        if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1297            continue;
1298        mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1299    }
1300
1301    return mask;
1302}
1303
1304/*
1305 * have_route_to - determine if the system has any route to
1306 * a given IP address.
1307 * For demand mode to work properly, we have to ignore routes
1308 * through our own interface.
1309 */
1310int have_route_to(u_int32_t addr)
1311{
1312    return -1;
1313}
1314
1315/*
1316 * Use the hostid as part of the random number seed.
1317 */
1318int
1319get_host_seed()
1320{
1321    return 17;
1322}
Note: See TracBrowser for help on using the repository browser.