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

4.115
Last change on this file since b799e4eb was 05c1886, checked in by Ralf Corsepius <ralf.corsepius@…>, on 11/30/09 at 16:01:51

Whitespace removal.

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