source: rtems/cpukit/pppd/sys-rtems.c @ 24312f34

5
Last change on this file since 24312f34 was ac9f8087, checked in by Sebastian Huber <sebastian.huber@…>, on 09/25/18 at 13:19:08

pppd: Remove unused get_pty() function

Update #3526.

  • Property mode set to 100644
File size: 30.9 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#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <errno.h>
28#include <fcntl.h>
29#include <termios.h>
30#include <signal.h>
31#include <sys/ioctl.h>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <sys/time.h>
35#include <sys/stat.h>
36#include <sys/param.h>
37#ifdef PPP_FILTER
38#include <net/bpf.h>
39#endif
40
41#include <net/if.h>
42#include <net/ppp_defs.h>
43#include <net/if_ppp.h>
44#include <net/route.h>
45#include <net/if_dl.h>
46#include <netinet/in.h>
47
48#if RTM_VERSION >= 3
49#include <sys/param.h>
50#if defined(NetBSD) && (NetBSD >= 199703)
51#include <netinet/if_inarp.h>
52#else   /* NetBSD 1.2D or later */
53#include <netinet/if_ether.h>
54#endif
55#endif
56
57#include <rtems.h>
58#include <rtems/rtems_bsdnet.h>
59#include <rtems/termiostypes.h>
60extern int      rtems_bsdnet_microseconds_per_tick;
61extern rtems_id rtems_pppd_taskid;
62
63#include "pppd.h"
64#include "fsm.h"
65#include "ipcp.h"
66
67static int initdisc = -1;       /* Initial TTY discipline for ppp_fd */
68static int initfdflags = -1;    /* Initial file descriptor flags for ppp_fd */
69static int ppp_fd = -1;         /* fd which is set to PPP discipline */
70static int rtm_seq;
71
72static int restore_term;        /* 1 => we've munged the terminal */
73static struct termios inittermios; /* Initial TTY termios */
74static struct winsize wsinfo;   /* Initial window size info */
75
76static int loop_slave = -1;
77static int loop_master;
78
79static unsigned char inbuf[512]; /* buffer for chars read from loopback */
80
81static int sockfd;              /* socket for doing interface ioctls */
82
83static int if_is_up;            /* the interface is currently up */
84static uint32_t ifaddrs[2];     /* local and remote addresses we set */
85static uint32_t default_route_gateway;  /* gateway addr for default route */
86static uint32_t proxy_arp_addr; /* remote addr for proxy arp */
87
88/* Prototypes for procedures local to this file. */
89static int dodefaultroute(uint32_t, int);
90static int get_ether_addr(uint32_t, struct sockaddr_dl *);
91
92
93/*
94 * sys_init - System-dependent initialization.
95 */
96void
97sys_init(void)
98{
99    /* Get an internet socket for doing socket ioctl's on. */
100    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
101        fatal("Couldn't create IP socket: %m");
102}
103
104/*
105 * sys_cleanup - restore any system state we modified before exiting:
106 * mark the interface down, delete default route and/or proxy arp entry.
107 * This should call die() because it's called from die().
108 */
109void
110sys_cleanup(void)
111{
112    struct ifreq ifr;
113
114    if (if_is_up) {
115        strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
116        if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
117            && ((ifr.ifr_flags & IFF_UP) != 0)) {
118            ifr.ifr_flags &= ~IFF_UP;
119            ioctl(sockfd, SIOCSIFFLAGS, &ifr);
120        }
121    }
122    if (ifaddrs[0] != 0)
123        cifaddr(0, ifaddrs[0], ifaddrs[1]);
124    if (default_route_gateway)
125        cifdefaultroute(0, 0, default_route_gateway);
126    if (proxy_arp_addr)
127        cifproxyarp(0, proxy_arp_addr);
128}
129
130/*
131 * sys_close - Clean up in a child process before execing.
132 */
133void
134sys_close(void)
135{
136    close(sockfd);
137    if (loop_slave >= 0) {
138        close(loop_slave);
139        close(loop_master);
140    }
141}
142
143/*
144 * sys_check_options - check the options that the user specified
145 */
146int
147sys_check_options(void)
148{
149    return 1;
150}
151
152/*
153 * ppp_available - check whether the system has any ppp interfaces
154 * (in fact we check whether we can do an ioctl on ppp0).
155 */
156int
157ppp_available(void)
158{
159    int s, ok;
160    struct ifreq ifr;
161
162    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
163        return 1;               /* can't tell */
164
165    strlcpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
166    ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
167    close(s);
168
169    return ok;
170}
171
172/*
173 * establish_ppp - Turn the serial port into a ppp interface.
174 */
175int
176establish_ppp(
177    int fd)
178{
179    int taskid  = (int)rtems_pppd_taskid;
180    int pppdisc = PPPDISC;
181    int x;
182
183    if (demand) {
184        /*
185         * Demand mode - prime the old ppp device to relinquish the unit.
186         */
187        if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0)
188            fatal("ioctl(transfer ppp unit): %m");
189    }
190
191    /*
192     * Save the old line discipline of fd, and set it to PPP.
193     */
194    if (ioctl(fd, TIOCGETD, &initdisc) < 0)
195        fatal("ioctl(TIOCGETD): %m");
196    if (ioctl(fd, TIOCSETD, &pppdisc) < 0)
197        fatal("ioctl(TIOCSETD): %m");
198
199    /* set pppd taskid into the driver */
200    ioctl(fd, PPPIOCSTASK, &taskid);
201
202    if (!demand) {
203        /*
204         * Find out which interface we were given.
205         */
206        if (ioctl(fd, PPPIOCGUNIT, &pppifunit) < 0)
207            fatal("ioctl(PPPIOCGUNIT): %m");
208    } else {
209        /*
210         * Check that we got the same unit again.
211         */
212        if (ioctl(fd, PPPIOCGUNIT, &x) < 0)
213            fatal("ioctl(PPPIOCGUNIT): %m");
214        if (x != pppifunit)
215            fatal("transfer_ppp failed: wanted unit %d, got %d", pppifunit, x);
216        x = TTYDISC;
217        ioctl(loop_slave, TIOCSETD, &x);
218    }
219
220    ppp_fd = fd;
221
222    /*
223     * Enable debug in the driver if requested.
224     */
225    if (kdebugflag) {
226        if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
227            warn("ioctl (PPPIOCGFLAGS): %m");
228        } else {
229            x |= (kdebugflag & 0xFF) * SC_DEBUG;
230            if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
231                warn("ioctl(PPPIOCSFLAGS): %m");
232        }
233    }
234
235    /*
236     * Set device for non-blocking reads.
237     */
238    if ((initfdflags = fcntl(fd, F_GETFL)) == -1
239        || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
240        warn("Couldn't set device to non-blocking mode: %m");
241    }
242
243    return fd;
244}
245
246/*
247 * restore_loop - reattach the ppp unit to the loopback.
248 */
249void
250restore_loop(void)
251{
252    int x;
253
254    /*
255     * Transfer the ppp interface back to the loopback.
256     */
257    if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0)
258        fatal("ioctl(transfer ppp unit): %m");
259    x = PPPDISC;
260    if (ioctl(loop_slave, TIOCSETD, &x) < 0)
261        fatal("ioctl(TIOCSETD): %m");
262
263    /*
264     * Check that we got the same unit again.
265     */
266    if (ioctl(loop_slave, PPPIOCGUNIT, &x) < 0)
267        fatal("ioctl(PPPIOCGUNIT): %m");
268    if (x != pppifunit)
269        fatal("transfer_ppp failed: wanted unit %d, got %d", pppifunit, x);
270    ppp_fd = loop_slave;
271}
272
273
274/*
275 * disestablish_ppp - Restore the serial port to normal operation.
276 * This shouldn't call die() because it's called from die().
277 */
278void
279disestablish_ppp(
280    int fd)
281{
282    int taskid = (int)0;
283
284    /* clear pppd taskid from the driver */
285    ioctl(fd, PPPIOCSTASK, &taskid);
286
287    /* Reset non-blocking mode on fd. */
288    if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
289        warn("Couldn't restore device fd flags: %m");
290    initfdflags = -1;
291
292    /* Restore old line discipline. */
293    if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
294        error("ioctl(TIOCSETD): %m");
295    initdisc = -1;
296
297    if (fd == ppp_fd)
298        ppp_fd = -1;
299}
300
301/*
302 * Check whether the link seems not to be 8-bit clean.
303 */
304void
305clean_check(void)
306{
307    int x;
308    char *s;
309
310    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
311        s = NULL;
312        switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
313        case SC_RCV_B7_0:
314            s = "bit 7 set to 1";
315            break;
316        case SC_RCV_B7_1:
317            s = "bit 7 set to 0";
318            break;
319        case SC_RCV_EVNP:
320            s = "odd parity";
321            break;
322        case SC_RCV_ODDP:
323            s = "even parity";
324            break;
325        }
326        if (s != NULL) {
327            warn("Serial link is not 8-bit clean:");
328            warn("All received characters had %s", s);
329        }
330    }
331}
332
333/*
334 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
335 * at the requested speed, etc.  If `local' is true, set CLOCAL
336 * regardless of whether the modem option was specified.
337 *
338 * For *BSD, we assume that speed_t values numerically equal bits/second.
339 */
340void
341set_up_tty(
342    int fd, int local)
343{
344    struct termios     tios;
345
346    if (tcgetattr(fd, &tios) < 0)
347        fatal("tcgetattr: %m");
348
349    if (!restore_term) {
350        inittermios = tios;
351        ioctl(fd, TIOCGWINSZ, &wsinfo);
352    }
353
354    tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
355    if (crtscts > 0 && !local) {
356        if (crtscts == 2) {
357#ifdef CDTRCTS
358            tios.c_cflag |= CDTRCTS;
359#endif
360        } else
361            tios.c_cflag |= CRTSCTS;
362    } else if (crtscts < 0) {
363        tios.c_cflag &= ~CRTSCTS;
364#ifdef CDTRCTS
365        tios.c_cflag &= ~CDTRCTS;
366#endif
367    }
368
369    tios.c_cflag |= CS8 | CREAD | HUPCL;
370    if (local || !modem)
371        tios.c_cflag |= CLOCAL;
372    tios.c_iflag = IGNBRK | IGNPAR;
373    tios.c_oflag = 0;
374    tios.c_lflag = 0;
375    tios.c_cc[VMIN] = 1;
376    tios.c_cc[VTIME] = 0;
377
378    if (crtscts == -2) {
379        tios.c_iflag |= IXON | IXOFF;
380        tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
381        tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
382    }
383
384    if (inspeed) {
385        cfsetospeed(&tios, inspeed);
386        cfsetispeed(&tios, inspeed);
387    } else {
388        inspeed = cfgetospeed(&tios);
389        /*
390         * We can't proceed if the serial port speed is 0,
391         * since that implies that the serial port is disabled.
392         */
393        if (inspeed == 0)
394            fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
395    }
396    baud_rate = inspeed;
397
398/*    if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {  */
399    if (tcsetattr(fd, TCSADRAIN, &tios) < 0) {
400        fatal("tcsetattr: %m");
401    }
402
403    restore_term = 1;
404}
405
406/*
407 * restore_tty - restore the terminal to the saved settings.
408 */
409void
410restore_tty(
411    int fd)
412{
413    if (restore_term) {
414        if (!default_device) {
415            /*
416             * Turn off echoing, because otherwise we can get into
417             * a loop with the tty and the modem echoing to each other.
418             * We presume we are the sole user of this tty device, so
419             * when we close it, it will revert to its defaults anyway.
420             */
421            inittermios.c_lflag &= ~(ECHO | ECHONL);
422        }
423/*      if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0) { */
424        if (tcsetattr(fd, TCSADRAIN, &inittermios) < 0) {
425            if (errno != ENXIO)
426                warn("tcsetattr: %m");
427        }
428        ioctl(fd, TIOCSWINSZ, &wsinfo);
429        restore_term = 0;
430    }
431}
432
433/*
434 * setdtr - control the DTR line on the serial port.
435 * This is called from die(), so it shouldn't call die().
436 */
437void
438setdtr(
439  int fd, int on )
440{
441    int modembits = TIOCM_DTR;
442
443    ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
444}
445
446
447/*
448 * open_ppp_loopback - open the device we use for getting
449 * packets in demand mode, and connect it to a ppp interface.
450 * Here we use a pty.
451 */
452int
453open_ppp_loopback(void)
454{
455    return loop_master;
456}
457
458
459/*
460 * output - Output PPP packet.
461 */
462void
463output(
464    int unit,
465    u_char *p,
466    int len)
467{
468    if (debug)
469        dbglog("sent %P", p, len);
470/*    printf("sent packet [%d]\n", len); */
471
472    if (write(pppd_ttyfd, p, len) < 0) {
473        if (errno != EIO)
474            error("write: %m");
475    }
476}
477
478void
479ppp_delay(void)
480{
481  rtems_interval     ticks;
482
483  /* recommended delay to help negotiation */
484  ticks = 300000/rtems_bsdnet_microseconds_per_tick;
485  rtems_task_wake_after(ticks);
486}
487
488/*
489 * wait_input - wait until there is data available,
490 * for the length of time specified by *timo (indefinite
491 * if timo is NULL).
492 */
493void
494wait_input(
495    struct timeval *timo)
496{
497  rtems_event_set    events;
498  rtems_interval     ticks = 0;
499  rtems_option       wait = RTEMS_WAIT;
500
501  if(timo) {
502    if(timo->tv_sec == 0 && timo->tv_usec == 0)
503      wait = RTEMS_NO_WAIT;
504    else {
505      ticks = (timo->tv_sec * 1000000 + timo->tv_usec) /
506        rtems_bsdnet_microseconds_per_tick;
507      if(ticks <= 0)
508        ticks = 1;
509    }
510  }
511  rtems_event_receive(RTEMS_EVENT_31, RTEMS_EVENT_ANY | wait, ticks, &events);
512}
513
514/*
515 * read_packet - get a PPP packet from the serial device.
516 */
517int
518read_packet(
519    u_char *buf)
520{
521    int len;
522
523    if ((len = read(pppd_ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
524        if (errno == EWOULDBLOCK || errno == EINTR) len = -1;
525        /*fatal("read: %m"); */
526    }
527
528/*    printf("read packet [%d]\n", len); */
529    return len;
530}
531
532
533/*
534 * get_loop_output - read characters from the loopback, form them
535 * into frames, and detect when we want to bring the real link up.
536 * Return value is 1 if we need to bring up the link, 0 otherwise.
537 */
538int
539get_loop_output(void)
540{
541    int rv = 0;
542    int n;
543
544    while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) {
545        if (loop_chars(inbuf, n))
546            rv = 1;
547    }
548
549    if (n == 0)
550        fatal("eof on loopback");
551    if (errno != EWOULDBLOCK)
552        fatal("read from loopback: %m");
553
554    return rv;
555}
556
557
558/*
559 * ppp_send_config - configure the transmit characteristics of
560 * the ppp interface.
561 */
562void
563ppp_send_config(
564    int unit,
565    int mtu,
566    uint32_t asyncmap,
567    int pcomp,
568    int accomp)
569{
570    u_int x;
571    struct ifreq ifr;
572
573    strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
574    ifr.ifr_mtu = mtu;
575    if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
576        fatal("ioctl(SIOCSIFMTU): %m");
577
578    if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
579        fatal("ioctl(PPPIOCSASYNCMAP): %m");
580
581    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
582        fatal("ioctl (PPPIOCGFLAGS): %m");
583    x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
584    x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
585/*    x = sync_serial ? x | SC_SYNC : x & ~SC_SYNC; */
586    if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
587        fatal("ioctl(PPPIOCSFLAGS): %m");
588}
589
590
591/*
592 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
593 */
594void
595ppp_set_xaccm(
596    int unit,
597    ext_accm accm)
598{
599    if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
600        warn("ioctl(set extended ACCM): %m");
601}
602
603
604/*
605 * ppp_recv_config - configure the receive-side characteristics of
606 * the ppp interface.
607 */
608void
609ppp_recv_config(
610    int unit,
611    int mru,
612    uint32_t asyncmap,
613    int pcomp, int 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(
636    int unit, u_char *opt_ptr, int opt_len, int for_transmit)
637{
638    struct ppp_option_data data;
639
640    data.ptr = opt_ptr;
641    data.length = opt_len;
642    data.transmit = for_transmit;
643    if (ioctl(pppd_ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
644        return 1;
645    return (errno == ENOBUFS)? 0: -1;
646}
647
648/*
649 * ccp_flags_set - inform kernel about the current state of CCP.
650 */
651void
652ccp_flags_set(
653    int unit, int isopen, int isup)
654{
655    int x;
656
657    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
658        error("ioctl (PPPIOCGFLAGS): %m");
659        return;
660    }
661    x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
662    x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
663    if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
664        error("ioctl(PPPIOCSFLAGS): %m");
665}
666
667/*
668 * ccp_fatal_error - returns 1 if decompression was disabled as a
669 * result of an error detected after decompression of a packet,
670 * 0 otherwise.  This is necessary because of patent nonsense.
671 */
672int
673ccp_fatal_error(
674    int unit)
675{
676    int x;
677
678    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
679        error("ioctl(PPPIOCGFLAGS): %m");
680        return 0;
681    }
682    return x & SC_DC_FERROR;
683}
684
685/*
686 * get_idle_time - return how long the link has been idle.
687 */
688int
689get_idle_time(
690    int u,
691    struct ppp_idle *ip)
692{
693    return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
694}
695
696/*
697 * get_ppp_stats - return statistics for the link.
698 */
699int
700get_ppp_stats(
701    int u,
702    struct pppd_stats *stats)
703{
704    struct ifpppstatsreq req;
705
706    memset (&req, 0, sizeof (req));
707    strlcpy(req.ifr_name, ifname, sizeof(req.ifr_name));
708    if (ioctl(sockfd, SIOCGPPPSTATS, &req) < 0) {
709        error("Couldn't get PPP statistics: %m");
710        return 0;
711    }
712    stats->bytes_in = req.stats.p.ppp_ibytes;
713    stats->bytes_out = req.stats.p.ppp_obytes;
714    return 1;
715}
716
717
718#ifdef PPP_FILTER
719/*
720 * set_filters - transfer the pass and active filters to the kernel.
721 */
722int
723set_filters(
724    struct bpf_program *pass, struct bpf_program *active)
725{
726    int ret = 1;
727
728    if (pass->bf_len > 0) {
729        if (ioctl(ppp_fd, PPPIOCSPASS, pass) < 0) {
730            error("Couldn't set pass-filter in kernel: %m");
731            ret = 0;
732        }
733    }
734    if (active->bf_len > 0) {
735        if (ioctl(ppp_fd, PPPIOCSACTIVE, active) < 0) {
736            error("Couldn't set active-filter in kernel: %m");
737            ret = 0;
738        }
739    }
740    return ret;
741}
742#endif
743
744/*
745 * sifvjcomp - config tcp header compression
746 */
747int
748sifvjcomp(
749    int u, int vjcomp, int cidcomp, int maxcid)
750{
751    u_int x;
752
753    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
754        error("ioctl (PPPIOCGFLAGS): %m");
755        return 0;
756    }
757    x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
758    x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
759    if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
760        error("ioctl(PPPIOCSFLAGS): %m");
761        return 0;
762    }
763    if (vjcomp && ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
764        error("ioctl(PPPIOCSMAXCID): %m");
765        return 0;
766    }
767    return 1;
768}
769
770/*
771 * sifup - Config the interface up and enable IP packets to pass.
772 */
773int
774sifup(
775    int u)
776{
777    struct ifreq ifr;
778
779    strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
780    if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
781        error("ioctl (SIOCGIFFLAGS): %m");
782        return 0;
783    }
784    ifr.ifr_flags |= IFF_UP;
785    if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
786        error("ioctl(SIOCSIFFLAGS): %m");
787        return 0;
788    }
789    if_is_up = 1;
790    return 1;
791}
792
793/*
794 * sifnpmode - Set the mode for handling packets for a given NP.
795 */
796int
797sifnpmode(
798    int u,
799    int proto,
800    enum NPmode mode)
801{
802    struct npioctl npi;
803
804    npi.protocol = proto;
805    npi.mode = mode;
806    if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
807        error("ioctl(set NP %d mode to %d): %m", proto, mode);
808        return 0;
809    }
810    return 1;
811}
812
813/*
814 * sifdown - Config the interface down and disable IP.
815 */
816int
817sifdown(
818    int u)
819{
820    struct ifreq ifr;
821    int rv;
822    struct npioctl npi;
823
824    rv = 1;
825    npi.protocol = PPP_IP;
826    npi.mode = NPMODE_ERROR;
827    ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
828    /* ignore errors, because ppp_fd might have been closed by now. */
829
830    strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
831    if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
832        error("ioctl (SIOCGIFFLAGS): %m");
833        rv = 0;
834    } else {
835        ifr.ifr_flags &= ~IFF_UP;
836        if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
837            error("ioctl(SIOCSIFFLAGS): %m");
838            rv = 0;
839        } else
840            if_is_up = 0;
841    }
842    return rv;
843}
844
845/*
846 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
847 * if it exists.
848 */
849#define SET_SA_FAMILY(addr, family)             \
850    BZERO((char *) &(addr), sizeof(addr));      \
851    addr.sa_family = (family);                  \
852    addr.sa_len = sizeof(addr);
853
854/*
855 * sifaddr - Config the interface IP addresses and netmask.
856 */
857int
858sifaddr(
859    int u,
860    uint32_t o, uint32_t h, uint32_t m )
861{
862    struct ifaliasreq ifra;
863    struct ifreq ifr;
864
865    strlcpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
866    SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
867    ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
868    SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
869    ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
870    if (m != 0) {
871        SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
872        ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
873    } else
874        BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
875    BZERO(&ifr, sizeof(ifr));
876    strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
877    if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifr) < 0) {
878        if (errno != EADDRNOTAVAIL)
879            warn("Couldn't remove interface address: %m");
880    }
881    if (ioctl(sockfd, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
882        if (errno != EEXIST) {
883            error("Couldn't set interface address: %m");
884            return 0;
885        }
886        warn("Couldn't set interface address: Address %I already exists", o);
887    }
888    ifaddrs[0] = o;
889    ifaddrs[1] = h;
890    return 1;
891}
892
893/*
894 * cifaddr - Clear the interface IP addresses, and delete routes
895 * through the interface if possible.
896 */
897int
898cifaddr(
899    int u,
900    uint32_t o, uint32_t h )
901{
902    struct ifaliasreq ifra;
903
904    ifaddrs[0] = 0;
905    strlcpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
906    SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
907    ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
908    SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
909    ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
910    BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
911    if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
912        if (errno != EADDRNOTAVAIL)
913            warn("Couldn't delete interface address: %m");
914        return 0;
915    }
916    return 1;
917}
918
919/*
920 * sifdefaultroute - assign a default route through the address given.
921 */
922int
923sifdefaultroute(
924    int u,
925    uint32_t l, uint32_t g)
926{
927    return dodefaultroute(g, 's');
928}
929
930/*
931 * cifdefaultroute - delete a default route through the address given.
932 */
933int
934cifdefaultroute(
935    int u,
936    uint32_t l, uint32_t g)
937{
938    return dodefaultroute(g, 'c');
939}
940
941/*
942 * dodefaultroute - talk to a routing socket to add/delete a default route.
943 */
944static int
945dodefaultroute(
946    uint32_t g,
947    int cmd)
948{
949/*    int    status;  */
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(
1010    int unit,
1011    uint32_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(
1060    int unit,
1061    uint32_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(
1095    int unit,
1096    uint32_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(
1135    int unit,
1136    uint32_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(
1161    uint32_t ipaddr,
1162    struct sockaddr_dl *hwaddr)
1163{
1164    struct ifreq *ifr, *ifend, *ifp;
1165    uint32_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 */
1244uint32_t
1245GetMask(
1246    uint32_t addr)
1247{
1248    uint32_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(uint32_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(void)
1319{
1320    return 17;
1321}
Note: See TracBrowser for help on using the repository browser.