source: rtems-libbsd/rtemsbsd/pppd/sys-rtems.c @ f0aaa04

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f0aaa04 was 573b4cd6, checked in by Sebastian Huber <sebastian.huber@…>, on 10/08/14 at 06:04:59

ppp: Port to new stack

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