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

4.104.114.84.95
Last change on this file since d2970aae was 9f114a8, checked in by Joel Sherrill <joel.sherrill@…>, on 05/20/05 at 19:33:23

2005-05-20 Sergei Organov <osv@…>

PR 750/networking

  • libnetworking/pppd/sys-rtems.c: Avoid NULL dereference in wait_input().
  • Property mode set to 100644
File size: 31.5 KB
Line 
1/*
2 * sys-bsd.c - System-dependent procedures for setting up
3 * PPP interfaces on bsd-4.4-ish systems (including 386BSD, NetBSD, etc.)
4 *
5 * Copyright (c) 1989 Carnegie Mellon University.
6 * Copyright (c) 1995 The Australian National University.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms are permitted
10 * provided that the above copyright notice and this paragraph are
11 * duplicated in all such forms and that any documentation,
12 * advertising materials, and other materials related to such
13 * distribution and use acknowledge that the software was developed
14 * by Carnegie Mellon University and The Australian National University.
15 * The names of the Universities may not be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 */
22
23#define RCSID   "$Id$"
24
25#include <stdio.h>
26#include <string.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <termios.h>
32#include <signal.h>
33#include <sys/ioctl.h>
34#include <sys/types.h>
35#include <sys/socket.h>
36#include <sys/time.h>
37#include <sys/stat.h>
38#include <sys/param.h>
39#ifdef PPP_FILTER
40#include <net/bpf.h>
41#endif
42
43#include <net/if.h>
44#include <net/ppp_defs.h>
45#include <net/if_ppp.h>
46#include <net/route.h>
47#include <net/if_dl.h>
48#include <netinet/in.h>
49
50#if RTM_VERSION >= 3
51#include <sys/param.h>
52#if defined(NetBSD) && (NetBSD >= 199703)
53#include <netinet/if_inarp.h>
54#else   /* NetBSD 1.2D or later */
55#include <netinet/if_ether.h>
56#endif
57#endif
58
59#include <rtems.h>
60#include <rtems/rtems_bsdnet.h>
61#include <rtems/termiostypes.h>
62extern int      rtems_bsdnet_microseconds_per_tick;
63extern rtems_id rtems_pppd_taskid;
64
65#include "pppd.h"
66#include "fsm.h"
67#include "ipcp.h"
68
69static const char rcsid[] = RCSID;
70
71
72static int initdisc = -1;       /* Initial TTY discipline for ppp_fd */
73static int initfdflags = -1;    /* Initial file descriptor flags for ppp_fd */
74static int ppp_fd = -1;         /* fd which is set to PPP discipline */
75static int rtm_seq;
76
77static int restore_term;        /* 1 => we've munged the terminal */
78static struct termios inittermios; /* Initial TTY termios */
79static struct winsize wsinfo;   /* Initial window size info */
80
81static int loop_slave = -1;
82static int loop_master;
83
84static unsigned char inbuf[512]; /* buffer for chars read from loopback */
85
86static int sockfd;              /* socket for doing interface ioctls */
87
88static int if_is_up;            /* the interface is currently up */
89static u_int32_t ifaddrs[2];    /* local and remote addresses we set */
90static u_int32_t default_route_gateway; /* gateway addr for default route */
91static u_int32_t proxy_arp_addr;        /* remote addr for proxy arp */
92
93/* Prototypes for procedures local to this file. */
94static int dodefaultroute __P((u_int32_t, int));
95static int get_ether_addr __P((u_int32_t, struct sockaddr_dl *));
96
97
98/*
99 * sys_init - System-dependent initialization.
100 */
101void
102sys_init()
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()
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()
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()
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()
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(fd)
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()
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(fd)
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()
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(fd, local)
347    int fd, 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(fd)
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(fd, on)
444int fd, 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(master_fdp, slave_fdp, slave_name, uid)
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()
473{
474    return loop_master;
475}
476
477
478/*
479 * output - Output PPP packet.
480 */
481void
482output(unit, p, len)
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(timo)
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(buf)
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()
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(unit, mtu, asyncmap, pcomp, accomp)
583    int unit, mtu;
584    u_int32_t asyncmap;
585    int pcomp, accomp;
586{
587    u_int x;
588    struct ifreq ifr;
589
590    strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
591    ifr.ifr_mtu = mtu;
592    if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
593        fatal("ioctl(SIOCSIFMTU): %m");
594
595    if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
596        fatal("ioctl(PPPIOCSASYNCMAP): %m");
597
598    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
599        fatal("ioctl (PPPIOCGFLAGS): %m");
600    x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
601    x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
602/*    x = sync_serial ? x | SC_SYNC : x & ~SC_SYNC; */
603    if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
604        fatal("ioctl(PPPIOCSFLAGS): %m");
605}
606
607
608/*
609 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
610 */
611void
612ppp_set_xaccm(unit, accm)
613    int unit;
614    ext_accm accm;
615{
616    if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
617        warn("ioctl(set extended ACCM): %m");
618}
619
620
621/*
622 * ppp_recv_config - configure the receive-side characteristics of
623 * the ppp interface.
624 */
625void
626ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
627    int unit, mru;
628    u_int32_t asyncmap;
629    int pcomp, accomp;
630{
631    int x;
632
633    if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
634        fatal("ioctl(PPPIOCSMRU): %m");
635    if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
636        fatal("ioctl(PPPIOCSRASYNCMAP): %m");
637    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
638        fatal("ioctl (PPPIOCGFLAGS): %m");
639    x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
640    if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
641        fatal("ioctl(PPPIOCSFLAGS): %m");
642}
643
644/*
645 * ccp_test - ask kernel whether a given compression method
646 * is acceptable for use.  Returns 1 if the method and parameters
647 * are OK, 0 if the method is known but the parameters are not OK
648 * (e.g. code size should be reduced), or -1 if the method is unknown.
649 */
650int
651ccp_test(unit, opt_ptr, opt_len, for_transmit)
652    int unit, opt_len, for_transmit;
653    u_char *opt_ptr;
654{
655    struct ppp_option_data data;
656
657    data.ptr = opt_ptr;
658    data.length = opt_len;
659    data.transmit = for_transmit;
660    if (ioctl(pppd_ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
661        return 1;
662    return (errno == ENOBUFS)? 0: -1;
663}
664
665/*
666 * ccp_flags_set - inform kernel about the current state of CCP.
667 */
668void
669ccp_flags_set(unit, isopen, isup)
670    int unit, isopen, isup;
671{
672    int x;
673
674    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
675        error("ioctl (PPPIOCGFLAGS): %m");
676        return;
677    }
678    x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
679    x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
680    if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
681        error("ioctl(PPPIOCSFLAGS): %m");
682}
683
684/*
685 * ccp_fatal_error - returns 1 if decompression was disabled as a
686 * result of an error detected after decompression of a packet,
687 * 0 otherwise.  This is necessary because of patent nonsense.
688 */
689int
690ccp_fatal_error(unit)
691    int unit;
692{
693    int x;
694
695    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
696        error("ioctl(PPPIOCGFLAGS): %m");
697        return 0;
698    }
699    return x & SC_DC_FERROR;
700}
701
702/*
703 * get_idle_time - return how long the link has been idle.
704 */
705int
706get_idle_time(u, ip)
707    int u;
708    struct ppp_idle *ip;
709{
710    return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
711}
712
713/*
714 * get_ppp_stats - return statistics for the link.
715 */
716int
717get_ppp_stats(u, stats)
718    int u;
719    struct pppd_stats *stats;
720{
721    struct ifpppstatsreq req;
722
723    memset (&req, 0, sizeof (req));
724    strlcpy(req.ifr_name, ifname, sizeof(req.ifr_name));
725    if (ioctl(sockfd, SIOCGPPPSTATS, &req) < 0) {
726        error("Couldn't get PPP statistics: %m");
727        return 0;
728    }
729    stats->bytes_in = req.stats.p.ppp_ibytes;
730    stats->bytes_out = req.stats.p.ppp_obytes;
731    return 1;
732}
733
734
735#ifdef PPP_FILTER
736/*
737 * set_filters - transfer the pass and active filters to the kernel.
738 */
739int
740set_filters(pass, active)
741    struct bpf_program *pass, *active;
742{
743    int ret = 1;
744
745    if (pass->bf_len > 0) {
746        if (ioctl(ppp_fd, PPPIOCSPASS, pass) < 0) {
747            error("Couldn't set pass-filter in kernel: %m");
748            ret = 0;
749        }
750    }
751    if (active->bf_len > 0) {
752        if (ioctl(ppp_fd, PPPIOCSACTIVE, active) < 0) {
753            error("Couldn't set active-filter in kernel: %m");
754            ret = 0;
755        }
756    }
757    return ret;
758}
759#endif
760
761/*
762 * sifvjcomp - config tcp header compression
763 */
764int
765sifvjcomp(u, vjcomp, cidcomp, maxcid)
766    int u, vjcomp, cidcomp, maxcid;
767{
768    u_int x;
769
770    if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
771        error("ioctl (PPPIOCGFLAGS): %m");
772        return 0;
773    }
774    x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
775    x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
776    if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
777        error("ioctl(PPPIOCSFLAGS): %m");
778        return 0;
779    }
780    if (vjcomp && ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
781        error("ioctl(PPPIOCSFLAGS): %m");
782        return 0;
783    }
784    return 1;
785}
786
787/*
788 * sifup - Config the interface up and enable IP packets to pass.
789 */
790int
791sifup(u)
792    int u;
793{
794    struct ifreq ifr;
795
796    strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
797    if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
798        error("ioctl (SIOCGIFFLAGS): %m");
799        return 0;
800    }
801    ifr.ifr_flags |= IFF_UP;
802    if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
803        error("ioctl(SIOCSIFFLAGS): %m");
804        return 0;
805    }
806    if_is_up = 1;
807    return 1;
808}
809
810/*
811 * sifnpmode - Set the mode for handling packets for a given NP.
812 */
813int
814sifnpmode(u, proto, mode)
815    int u;
816    int proto;
817    enum NPmode mode;
818{
819    struct npioctl npi;
820
821    npi.protocol = proto;
822    npi.mode = mode;
823    if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
824        error("ioctl(set NP %d mode to %d): %m", proto, mode);
825        return 0;
826    }
827    return 1;
828}
829
830/*
831 * sifdown - Config the interface down and disable IP.
832 */
833int
834sifdown(u)
835    int u;
836{
837    struct ifreq ifr;
838    int rv;
839    struct npioctl npi;
840
841    rv = 1;
842    npi.protocol = PPP_IP;
843    npi.mode = NPMODE_ERROR;
844    ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
845    /* ignore errors, because ppp_fd might have been closed by now. */
846
847    strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
848    if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
849        error("ioctl (SIOCGIFFLAGS): %m");
850        rv = 0;
851    } else {
852        ifr.ifr_flags &= ~IFF_UP;
853        if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
854            error("ioctl(SIOCSIFFLAGS): %m");
855            rv = 0;
856        } else
857            if_is_up = 0;
858    }
859    return rv;
860}
861
862/*
863 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
864 * if it exists.
865 */
866#define SET_SA_FAMILY(addr, family)             \
867    BZERO((char *) &(addr), sizeof(addr));      \
868    addr.sa_family = (family);                  \
869    addr.sa_len = sizeof(addr);
870
871/*
872 * sifaddr - Config the interface IP addresses and netmask.
873 */
874int
875sifaddr(u, o, h, m)
876    int u;
877    u_int32_t o, h, m;
878{
879    struct ifaliasreq ifra;
880    struct ifreq ifr;
881
882    strlcpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
883    SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
884    ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
885    SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
886    ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
887    if (m != 0) {
888        SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
889        ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
890    } else
891        BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
892    BZERO(&ifr, sizeof(ifr));
893    strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
894    if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifr) < 0) {
895        if (errno != EADDRNOTAVAIL)
896            warn("Couldn't remove interface address: %m");
897    }
898    if (ioctl(sockfd, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
899        if (errno != EEXIST) {
900            error("Couldn't set interface address: %m");
901            return 0;
902        }
903        warn("Couldn't set interface address: Address %I already exists", o);
904    }
905    ifaddrs[0] = o;
906    ifaddrs[1] = h;
907    return 1;
908}
909
910/*
911 * cifaddr - Clear the interface IP addresses, and delete routes
912 * through the interface if possible.
913 */
914int
915cifaddr(u, o, h)
916    int u;
917    u_int32_t o, h;
918{
919    struct ifaliasreq ifra;
920
921    ifaddrs[0] = 0;
922    strlcpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
923    SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
924    ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
925    SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
926    ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
927    BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
928    if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
929        if (errno != EADDRNOTAVAIL)
930            warn("Couldn't delete interface address: %m");
931        return 0;
932    }
933    return 1;
934}
935
936/*
937 * sifdefaultroute - assign a default route through the address given.
938 */
939int
940sifdefaultroute(u, l, g)
941    int u;
942    u_int32_t l, g;
943{
944    return dodefaultroute(g, 's');
945}
946
947/*
948 * cifdefaultroute - delete a default route through the address given.
949 */
950int
951cifdefaultroute(u, l, g)
952    int u;
953    u_int32_t l, g;
954{
955    return dodefaultroute(g, 'c');
956}
957
958/*
959 * dodefaultroute - talk to a routing socket to add/delete a default route.
960 */
961static int
962dodefaultroute(g, cmd)
963    u_int32_t g;
964    int cmd;
965{
966/*    int    status;  */
967    struct sockaddr_in address;
968    struct sockaddr_in netmask;
969    struct sockaddr_in gateway;
970
971    memset((void *) &address, 0, sizeof(address));
972    address.sin_len = sizeof address;
973    address.sin_family = AF_INET;
974    address.sin_addr.s_addr = INADDR_ANY;
975
976    memset((void *) &netmask, 0, sizeof(netmask));
977    netmask.sin_len = sizeof netmask;
978    netmask.sin_addr.s_addr = INADDR_ANY; 
979    netmask.sin_family = AF_INET;
980
981    if (cmd=='s') {     
982      memset((void *) &gateway, 0, sizeof(gateway));
983      gateway.sin_len = sizeof gateway;
984      gateway.sin_family = AF_INET;
985      gateway.sin_addr.s_addr = g;
986
987      rtems_bsdnet_rtrequest(RTM_ADD,
988                             (struct sockaddr *)&address,
989                             (struct sockaddr *)&gateway,
990                             (struct sockaddr *)&netmask,
991                             (RTF_UP|RTF_GATEWAY|RTF_STATIC), NULL);
992    }
993    else {
994      memset((void *) &gateway, 0, sizeof(gateway));
995      gateway.sin_len = sizeof gateway;
996      gateway.sin_family = AF_INET;
997      gateway.sin_addr.s_addr =  INADDR_ANY;
998
999      rtems_bsdnet_rtrequest(RTM_DELETE,
1000                             (struct sockaddr *)&address,
1001                             (struct sockaddr *)&gateway,
1002                             (struct sockaddr *)&netmask,
1003                             (RTF_UP|RTF_STATIC), NULL);
1004    }
1005
1006    default_route_gateway = (cmd == 's')? g: 0;
1007
1008    return 1;
1009}
1010
1011#if RTM_VERSION >= 3
1012
1013/*
1014 * sifproxyarp - Make a proxy ARP entry for the peer.
1015 */
1016static struct {
1017    struct rt_msghdr            hdr;
1018    struct sockaddr_inarp       dst;
1019    struct sockaddr_dl          hwa;
1020    char                        extra[128];
1021} arpmsg;
1022
1023static int arpmsg_valid;
1024
1025int
1026sifproxyarp(unit, hisaddr)
1027    int unit;
1028    u_int32_t hisaddr;
1029{
1030    int routes;
1031
1032    /*
1033     * Get the hardware address of an interface on the same subnet
1034     * as our local address.
1035     */
1036    memset(&arpmsg, 0, sizeof(arpmsg));
1037    if (!get_ether_addr(hisaddr, &arpmsg.hwa)) {
1038        error("Cannot determine ethernet address for proxy ARP");
1039        return 0;
1040    }
1041
1042    if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
1043        error("Couldn't add proxy arp entry: socket: %m");
1044        return 0;
1045    }
1046
1047    arpmsg.hdr.rtm_type = RTM_ADD;
1048    arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
1049    arpmsg.hdr.rtm_version = RTM_VERSION;
1050    arpmsg.hdr.rtm_seq = ++rtm_seq;
1051    arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
1052    arpmsg.hdr.rtm_inits = RTV_EXPIRE;
1053    arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
1054    arpmsg.dst.sin_family = AF_INET;
1055    arpmsg.dst.sin_addr.s_addr = hisaddr;
1056    arpmsg.dst.sin_other = SIN_PROXY;
1057
1058    arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
1059        + arpmsg.hwa.sdl_len;
1060    if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
1061        error("Couldn't add proxy arp entry: %m");
1062        close(routes);
1063        return 0;
1064    }
1065
1066    close(routes);
1067    arpmsg_valid = 1;
1068    proxy_arp_addr = hisaddr;
1069    return 1;
1070}
1071
1072/*
1073 * cifproxyarp - Delete the proxy ARP entry for the peer.
1074 */
1075int
1076cifproxyarp(unit, hisaddr)
1077    int unit;
1078    u_int32_t hisaddr;
1079{
1080    int routes;
1081
1082    if (!arpmsg_valid)
1083        return 0;
1084    arpmsg_valid = 0;
1085
1086    arpmsg.hdr.rtm_type = RTM_DELETE;
1087    arpmsg.hdr.rtm_seq = ++rtm_seq;
1088
1089    if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
1090        error("Couldn't delete proxy arp entry: socket: %m");
1091        return 0;
1092    }
1093
1094    if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
1095        error("Couldn't delete proxy arp entry: %m");
1096        close(routes);
1097        return 0;
1098    }
1099
1100    close(routes);
1101    proxy_arp_addr = 0;
1102    return 1;
1103}
1104
1105#else   /* RTM_VERSION */
1106
1107/*
1108 * sifproxyarp - Make a proxy ARP entry for the peer.
1109 */
1110int
1111sifproxyarp(unit, hisaddr)
1112    int unit;
1113    u_int32_t hisaddr;
1114{
1115    struct arpreq arpreq;
1116    struct {
1117        struct sockaddr_dl      sdl;
1118        char                    space[128];
1119    } dls;
1120
1121    BZERO(&arpreq, sizeof(arpreq));
1122
1123    /*
1124     * Get the hardware address of an interface on the same subnet
1125     * as our local address.
1126     */
1127    if (!get_ether_addr(hisaddr, &dls.sdl)) {
1128        error("Cannot determine ethernet address for proxy ARP");
1129        return 0;
1130    }
1131
1132    arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
1133    arpreq.arp_ha.sa_family = AF_UNSPEC;
1134    BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
1135    SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1136    ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1137    arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1138    if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1139        error("Couldn't add proxy arp entry: %m");
1140        return 0;
1141    }
1142
1143    proxy_arp_addr = hisaddr;
1144    return 1;
1145}
1146
1147/*
1148 * cifproxyarp - Delete the proxy ARP entry for the peer.
1149 */
1150int
1151cifproxyarp(unit, hisaddr)
1152    int unit;
1153    u_int32_t hisaddr;
1154{
1155    struct arpreq arpreq;
1156
1157    BZERO(&arpreq, sizeof(arpreq));
1158    SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1159    ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1160    if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1161        warn("Couldn't delete proxy arp entry: %m");
1162        return 0;
1163    }
1164    proxy_arp_addr = 0;
1165    return 1;
1166}
1167#endif  /* RTM_VERSION */
1168
1169
1170/*
1171 * get_ether_addr - get the hardware address of an interface on the
1172 * the same subnet as ipaddr.
1173 */
1174#define MAX_IFS         32
1175
1176static int
1177get_ether_addr(ipaddr, hwaddr)
1178    u_int32_t ipaddr;
1179    struct sockaddr_dl *hwaddr;
1180{
1181    struct ifreq *ifr, *ifend, *ifp;
1182    u_int32_t ina, mask;
1183    struct sockaddr_dl *dla;
1184    struct ifreq ifreq;
1185    struct ifconf ifc;
1186    struct ifreq ifs[MAX_IFS];
1187
1188    ifc.ifc_len = sizeof(ifs);
1189    ifc.ifc_req = ifs;
1190    if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1191        error("ioctl(SIOCGIFCONF): %m");
1192        return 0;
1193    }
1194
1195    /*
1196     * Scan through looking for an interface with an Internet
1197     * address on the same subnet as `ipaddr'.
1198     */
1199    ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1200    for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1201                ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1202        if (ifr->ifr_addr.sa_family == AF_INET) {
1203            ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1204            strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1205            /*
1206             * Check that the interface is up, and not point-to-point
1207             * or loopback.
1208             */
1209            if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1210                continue;
1211            if ((ifreq.ifr_flags &
1212                 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1213                 != (IFF_UP|IFF_BROADCAST))
1214                continue;
1215            /*
1216             * Get its netmask and check that it's on the right subnet.
1217             */
1218            if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1219                continue;
1220            mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1221            if ((ipaddr & mask) != (ina & mask))
1222                continue;
1223
1224            break;
1225        }
1226    }
1227
1228    if (ifr >= ifend)
1229        return 0;
1230    info("found interface %s for proxy arp", ifr->ifr_name);
1231
1232    /*
1233     * Now scan through again looking for a link-level address
1234     * for this interface.
1235     */
1236    ifp = ifr;
1237    for (ifr = ifc.ifc_req; ifr < ifend; ) {
1238        if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
1239            && ifr->ifr_addr.sa_family == AF_LINK) {
1240            /*
1241             * Found the link-level address - copy it out
1242             */
1243            dla = (struct sockaddr_dl *) &ifr->ifr_addr;
1244            BCOPY(dla, hwaddr, dla->sdl_len);
1245            return 1;
1246        }
1247        ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
1248    }
1249
1250    return 0;
1251}
1252
1253/*
1254 * Return user specified netmask, modified by any mask we might determine
1255 * for address `addr' (in network byte order).
1256 * Here we scan through the system's list of interfaces, looking for
1257 * any non-point-to-point interfaces which might appear to be on the same
1258 * network as `addr'.  If we find any, we OR in their netmask to the
1259 * user-specified netmask.
1260 */
1261u_int32_t
1262GetMask(addr)
1263    u_int32_t addr;
1264{
1265    u_int32_t mask, nmask, ina;
1266    struct ifreq *ifr, *ifend, ifreq;
1267    struct ifconf ifc;
1268    struct ifreq ifs[MAX_IFS];
1269
1270    addr = ntohl(addr);
1271    if (IN_CLASSA(addr))        /* determine network mask for address class */
1272        nmask = IN_CLASSA_NET;
1273    else if (IN_CLASSB(addr))
1274        nmask = IN_CLASSB_NET;
1275    else
1276        nmask = IN_CLASSC_NET;
1277    /* class D nets are disallowed by bad_ip_adrs */
1278    mask = netmask | htonl(nmask);
1279
1280    /*
1281     * Scan through the system's network interfaces.
1282     */
1283    ifc.ifc_len = sizeof(ifs);
1284    ifc.ifc_req = ifs;
1285    if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1286        warn("ioctl(SIOCGIFCONF): %m");
1287        return mask;
1288    }
1289    ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1290    for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1291                ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1292        /*
1293         * Check the interface's internet address.
1294         */
1295        if (ifr->ifr_addr.sa_family != AF_INET)
1296            continue;
1297        ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1298        if ((ntohl(ina) & nmask) != (addr & nmask))
1299            continue;
1300        /*
1301         * Check that the interface is up, and not point-to-point or loopback.
1302         */
1303        strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1304        if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1305            continue;
1306        if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1307            != IFF_UP)
1308            continue;
1309        /*
1310         * Get its netmask and OR it into our mask.
1311         */
1312        if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1313            continue;
1314        mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1315    }
1316
1317    return mask;
1318}
1319
1320/*
1321 * have_route_to - determine if the system has any route to
1322 * a given IP address.
1323 * For demand mode to work properly, we have to ignore routes
1324 * through our own interface.
1325 */
1326int have_route_to(u_int32_t addr)
1327{
1328    return -1;
1329}
1330
1331/*
1332 * Use the hostid as part of the random number seed.
1333 */
1334int
1335get_host_seed()
1336{
1337    return 17;
1338}
Note: See TracBrowser for help on using the repository browser.