2 * sys-linux.c - System-dependent procedures for setting up
3 * PPP interfaces on Linux systems
5 * Copyright (c) 1994-2004 Paul Mackerras. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. The name(s) of the authors of this software must not be used to
15 * endorse or promote products derived from this software without
16 * prior written permission.
18 * 3. Redistributions of any form whatsoever must retain the following
20 * "This product includes software developed by Paul Mackerras
21 * <paulus@samba.org>".
23 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
24 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
26 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
28 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
29 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 * Derived from main.c and pppd.h, which are:
33 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
47 * 3. The name "Carnegie Mellon University" must not be used to
48 * endorse or promote products derived from this software without
49 * prior written permission. For permission or any legal
50 * details, please contact
51 * Office of Technology Transfer
52 * Carnegie Mellon University
54 * Pittsburgh, PA 15213-3890
55 * (412) 268-4387, fax: (412) 268-7395
56 * tech-transfer@andrew.cmu.edu
58 * 4. Redistributions of any form whatsoever must retain the following
60 * "This product includes software developed by Computing Services
61 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
63 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
64 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
65 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
66 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
67 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
68 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
69 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
76 #include <sys/ioctl.h>
77 #include <sys/types.h>
78 #include <sys/socket.h>
82 #include <sys/utsname.h>
83 #include <sys/sysmacros.h>
84 #include <sys/param.h>
104 /* This is in netdevice.h. However, this compile will fail miserably if
105 you attempt to include netdevice.h because it has so many references
106 to __memcpy functions which it should not attempt to do. So, since I
107 really don't use it, but it must be defined, define it now. */
110 #define MAX_ADDR_LEN 7
113 #if !defined(__GLIBC__) || __GLIBC__ >= 2
114 #include <asm/types.h> /* glibc 2 conflicts with linux/types.h */
116 #include <net/if_arp.h>
117 #include <net/route.h>
118 #include <netinet/if_ether.h>
120 #include <linux/types.h>
121 #include <linux/if.h>
122 #include <linux/if_arp.h>
123 #include <linux/route.h>
124 #include <linux/if_ether.h>
126 #include <netinet/in.h>
127 #include <arpa/inet.h>
129 #include <linux/ppp-ioctl.h>
132 #ifdef PPP_WITH_IPV6CP
133 #include <linux/netlink.h>
134 #include <linux/rtnetlink.h>
135 #include <linux/if_link.h>
136 /* Attempt at retaining compile-support with older than 4.7 kernels, or kernels
137 * where RTM_NEWSTATS isn't defined for whatever reason.
140 #define RTM_NEWSTATS 92
141 #define RTM_GETSTATS 94
142 #define IFLA_STATS_LINK_64 1
143 #endif /* PPP_WITH_IPV6CP */
145 #include <linux/if_addr.h>
146 /* glibc versions prior to 2.24 do not define SOL_NETLINK */
148 #define SOL_NETLINK 270
151 /* linux kernel versions prior to 4.3 do not define/support NETLINK_CAP_ACK */
152 #ifndef NETLINK_CAP_ACK
153 #define NETLINK_CAP_ACK 10
161 #ifdef PPP_WITH_FILTER
162 #include <pcap-bpf.h>
163 #include <linux/filter.h>
164 #endif /* PPP_WITH_FILTER */
167 #include <sys/locks.h>
171 * Instead of system header file <termios.h> use local "termios_linux.h" header
172 * file as it provides additional support for arbitrary baud rates via BOTHER.
174 #include "termios_linux.h"
176 #ifdef PPP_WITH_IPV6CP
179 * This is in linux/include/net/ipv6.h.
183 struct in6_addr ifr6_addr;
184 __u32 ifr6_prefixlen;
185 unsigned int ifr6_ifindex;
189 #define IN6_LLADDR_FROM_EUI64(sin6, eui64) do { \
190 memset(&(sin6).s6_addr, 0, sizeof(struct in6_addr)); \
191 (sin6).s6_addr16[0] = htons(0xfe80); \
192 eui64_copy(eui64, (sin6).s6_addr32[2]); \
195 static const eui64_t nulleui64;
196 #endif /* PPP_WITH_IPV6CP */
198 /* We can get an EIO error on an ioctl if the modem has hung up */
199 #define ok_error(num) ((num)==EIO)
201 static int tty_disc = N_TTY; /* The TTY discipline */
202 static int ppp_disc = N_PPP; /* The PPP discpline */
203 static int initfdflags = -1; /* Initial file descriptor flags for fd */
204 static int ppp_fd = -1; /* fd which is set to PPP discipline */
205 static int sock_fd = -1; /* socket for doing interface ioctls */
206 static int slave_fd = -1; /* pty for old-style demand mode, slave */
207 static int master_fd = -1; /* pty for old-style demand mode, master */
208 #ifdef PPP_WITH_IPV6CP
209 static int sock6_fd = -1;
210 #endif /* PPP_WITH_IPV6CP */
213 * For the old-style kernel driver, this is the same as ppp_fd.
214 * For the new-style driver, it is the fd of an instance of /dev/ppp
215 * which is attached to the ppp unit and is used for controlling it.
217 int ppp_dev_fd = -1; /* fd for /dev/ppp (new style driver) */
219 static int chindex; /* channel index (new style driver) */
221 static fd_set in_fds; /* set of fds that wait_input waits for */
222 static int max_in_fd; /* highest fd set in in_fds */
224 static int has_proxy_arp = 0;
225 static int driver_version = 0;
226 static int driver_modification = 0;
227 static int driver_patch = 0;
228 static int driver_is_old = 0;
229 static int restore_term = 0; /* 1 => we've munged the terminal */
230 static struct termios inittermios; /* Initial TTY termios */
232 int new_style_driver = 0;
234 static char loop_name[20];
235 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
237 static int if_is_up; /* Interface has been marked up */
238 static int if6_is_up; /* Interface has been marked up for IPv6, to help differentiate */
239 static int have_default_route; /* Gateway for default route added */
240 static int have_default_route6; /* Gateway for default IPv6 route added */
241 static struct rtentry old_def_rt; /* Old default route */
242 static int default_rt_repl_rest; /* replace and restore old default rt */
243 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
244 static char proxy_arp_dev[16]; /* Device for proxy arp entry */
245 static u_int32_t our_old_addr; /* for detecting address changes */
246 static int dynaddr_set; /* 1 if ip_dynaddr set */
247 static int looped; /* 1 if using loop */
248 static int link_mtu; /* mtu for the link (not bundle) */
250 static struct utsname utsname; /* for the kernel version */
251 static int kernel_version;
252 #define KVERSION(j,n,p) ((j)*1000000 + (n)*1000 + (p))
256 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
257 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
258 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
260 #define SIN_ADDR(x) (((struct sockaddr_in *) (&(x)))->sin_addr.s_addr)
262 /* Prototypes for procedures local to this file. */
263 static int modify_flags(int fd, int clear_bits, int set_bits);
264 static int translate_speed (int bps);
265 static int baud_rate_of (int speed);
266 static void close_route_table (void);
267 static int open_route_table (void);
268 static int read_route_table (struct rtentry *rt);
269 static int defaultroute_exists (struct rtentry *rt, int metric);
270 static int defaultroute6_exists (struct in6_rtmsg *rt, int metric);
271 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
272 char *name, int namelen);
273 static void decode_version (char *buf, int *version, int *mod, int *patch);
274 static int set_kdebugflag(int level);
275 static int ppp_registered(void);
276 static int make_ppp_unit(void);
277 static int setifstate (int u, int state);
279 extern u_char inpacket_buf[]; /* borrowed from main.c */
281 extern int dfl_route_metric;
284 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
288 #define SET_SA_FAMILY(addr, family) \
289 memset ((char *) &(addr), '\0', sizeof(addr)); \
290 addr.sa_family = (family);
293 * Determine if the PPP connection should still be present.
298 /* new_fd is the fd of a tty */
299 static void set_ppp_fd (int new_fd)
302 if (!new_style_driver)
306 static int still_ppp(void)
308 if (new_style_driver)
309 return !hungup && ppp_fd >= 0;
310 if (!hungup || ppp_fd == slave_fd)
313 set_ppp_fd(slave_fd);
320 * modify_flags - set and clear flag bits controlling the kernel
323 static int modify_flags(int fd, int clear_bits, int set_bits)
327 if (ioctl(fd, PPPIOCGFLAGS, &flags) == -1)
329 flags = (flags & ~clear_bits) | set_bits;
330 if (ioctl(fd, PPPIOCSFLAGS, &flags) == -1)
337 error("Failed to set PPP kernel option flags: %m");
341 /********************************************************************
343 * sys_init - System-dependent initialization.
348 /* Get an internet socket for doing socket ioctls. */
349 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
351 fatal("Couldn't create IP socket: %m(%d)", errno);
353 #ifdef PPP_WITH_IPV6CP
354 sock6_fd = socket(AF_INET6, SOCK_DGRAM, 0);
356 sock6_fd = -errno; /* save errno for later */
363 /********************************************************************
365 * sys_cleanup - restore any system state we modified before exiting:
366 * mark the interface down, delete default route and/or proxy arp entry.
367 * This shouldn't call die() because it's called from die().
370 void sys_cleanup(void)
373 * Take down the device
379 #ifdef PPP_WITH_IPV6CP
385 * Delete any routes through the device.
387 if (have_default_route)
388 cifdefaultroute(0, 0, 0);
389 #ifdef PPP_WITH_IPV6CP
390 if (have_default_route6)
391 cif6defaultroute(0, nulleui64, nulleui64);
395 cifproxyarp(0, proxy_arp_addr);
398 /********************************************************************
400 * sys_close - Clean up in a child process before execing.
405 if (new_style_driver && ppp_dev_fd >= 0)
409 #ifdef PPP_WITH_IPV6CP
419 /********************************************************************
421 * set_kdebugflag - Define the debugging level for the kernel
424 static int set_kdebugflag (int requested_level)
428 if (ioctl(ppp_dev_fd, PPPIOCSDEBUG, &requested_level) < 0) {
429 if ( ! ok_error (errno) )
430 error("ioctl(PPPIOCSDEBUG): %m (line %d)", __LINE__);
436 /********************************************************************
438 * tty_establish_ppp - Turn the serial port into a ppp interface.
441 int tty_establish_ppp (int tty_fd)
446 * Ensure that the tty device is in exclusive mode.
448 if (ioctl(tty_fd, TIOCEXCL, 0) < 0) {
449 if ( ! ok_error ( errno ))
450 warn("Couldn't make tty exclusive: %m");
453 * Demand mode - prime the old ppp device to relinquish the unit.
455 if (!new_style_driver && looped
456 && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0) {
457 error("ioctl(transfer ppp unit): %m, line %d", __LINE__);
461 * Set the current tty to the PPP discpline
465 #define N_SYNC_PPP 14
467 ppp_disc = (new_style_driver && sync_serial)? N_SYNC_PPP: N_PPP;
468 if (ioctl(tty_fd, TIOCSETD, &ppp_disc) < 0) {
469 if ( ! ok_error (errno) ) {
470 error("Couldn't set tty to PPP discipline: %m");
475 ret_fd = generic_establish_ppp(tty_fd);
477 #define SC_RCVB (SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP)
478 #define SC_LOGB (SC_DEBUG | SC_LOG_INPKT | SC_LOG_OUTPKT | SC_LOG_RAWIN \
482 modify_flags(ppp_fd, SC_RCVB | SC_LOGB,
483 (kdebugflag * SC_DEBUG) & SC_LOGB);
485 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0 && !ok_error(errno))
486 warn("Couldn't reset tty to normal line discipline: %m");
492 /********************************************************************
494 * generic_establish_ppp - Turn the fd into a ppp interface.
496 int generic_establish_ppp (int fd)
500 if (new_style_driver) {
503 /* If a ppp_fd is already open, close it first */
510 /* Open an instance of /dev/ppp and connect the channel to it */
511 if (ioctl(fd, PPPIOCGCHAN, &chindex) == -1) {
512 error("Couldn't get channel number: %m");
515 dbglog("using channel %d", chindex);
516 fd = open("/dev/ppp", O_RDWR);
518 error("Couldn't reopen /dev/ppp: %m");
521 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
522 if (ioctl(fd, PPPIOCATTCHAN, &chindex) < 0) {
523 error("Couldn't attach to channel %d: %m", chindex);
526 flags = fcntl(fd, F_GETFL);
527 if (flags == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
528 warn("Couldn't set /dev/ppp (channel) to nonblock: %m");
533 if (!looped && !multilink) {
535 * Create a new PPP unit.
537 if (make_ppp_unit() < 0)
542 modify_flags(ppp_dev_fd, SC_LOOP_TRAFFIC, 0);
546 if (ioctl(fd, PPPIOCCONNECT, &ifunit) < 0) {
547 error("Couldn't attach to PPP unit %d: %m", ifunit);
554 * Old-style driver: find out which interface we were given.
557 if (ioctl(fd, PPPIOCGUNIT, &x) < 0) {
558 if (ok_error (errno))
560 fatal("ioctl(PPPIOCGUNIT): %m (line %d)", __LINE__);
562 /* Check that we got the same unit again. */
563 if (looped && x != ifunit)
564 fatal("transfer_ppp failed: wanted unit %d, got %d", ifunit, x);
568 * Fetch the initial file flags and reset blocking mode on the file.
570 initfdflags = fcntl(fd, F_GETFL);
571 if (initfdflags == -1 ||
572 fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
573 if ( ! ok_error (errno))
574 warn("Couldn't set device to non-blocking mode: %m");
579 * Enable debug in the driver if requested.
582 set_kdebugflag (kdebugflag);
594 /********************************************************************
596 * tty_disestablish_ppp - Restore the serial port to normal operation.
597 * This shouldn't call die() because it's called from die().
600 void tty_disestablish_ppp(int tty_fd)
604 * Flush the tty output buffer so that the TIOCSETD doesn't hang.
606 if (tcflush(tty_fd, TCIOFLUSH) < 0)
608 warn("tcflush failed: %m");
612 * Restore the previous line discipline
614 if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0) {
615 if ( ! ok_error (errno))
616 error("ioctl(TIOCSETD, N_TTY): %m (line %d)", __LINE__);
619 if (ioctl(tty_fd, TIOCNXCL, 0) < 0) {
620 if ( ! ok_error (errno))
621 warn("ioctl(TIOCNXCL): %m (line %d)", __LINE__);
624 /* Reset non-blocking mode on fd. */
625 if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0) {
626 if ( ! ok_error (errno))
627 warn("Couldn't restore device fd flags: %m");
633 generic_disestablish_ppp(tty_fd);
636 /********************************************************************
638 * generic_disestablish_ppp - Restore device components to normal
639 * operation, and reconnect the ppp unit to the loopback if in demand
640 * mode. This shouldn't call die() because it's called from die().
642 void generic_disestablish_ppp(int dev_fd)
644 if (new_style_driver) {
648 modify_flags(ppp_dev_fd, 0, SC_LOOP_TRAFFIC);
650 } else if (!doing_multilink && ppp_dev_fd >= 0) {
652 remove_fd(ppp_dev_fd);
656 /* old-style driver */
658 set_ppp_fd(slave_fd);
665 * make_ppp_unit - make a new ppp unit for ppp_dev_fd.
666 * Assumes new_style_driver.
668 static int make_ppp_unit(void)
672 if (ppp_dev_fd >= 0) {
673 dbglog("in make_ppp_unit, already had /dev/ppp open?");
676 ppp_dev_fd = open("/dev/ppp", O_RDWR);
678 fatal("Couldn't open /dev/ppp: %m");
679 flags = fcntl(ppp_dev_fd, F_GETFL);
681 || fcntl(ppp_dev_fd, F_SETFL, flags | O_NONBLOCK) == -1)
682 warn("Couldn't set /dev/ppp to nonblock: %m");
685 x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit);
686 if (x < 0 && req_unit >= 0 && errno == EEXIST) {
687 warn("Couldn't allocate PPP unit %d as it is already in use", req_unit);
689 x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit);
692 error("Couldn't create new ppp unit: %m");
694 if (x == 0 && req_ifname[0] != '\0') {
697 memset(&ifr, 0, sizeof(struct ifreq));
698 slprintf(t, sizeof(t), "%s%d", PPP_DRV_NAME, ifunit);
699 strlcpy(ifr.ifr_name, t, IFNAMSIZ);
700 strlcpy(ifr.ifr_newname, req_ifname, IFNAMSIZ);
701 x = ioctl(sock_fd, SIOCSIFNAME, &ifr);
703 error("Couldn't rename interface %s to %s: %m", t, req_ifname);
705 info("Renamed interface %s to %s", t, req_ifname);
712 * cfg_bundle - configure the existing bundle.
713 * Used in demand mode.
715 void cfg_bundle(int mrru, int mtru, int rssn, int tssn)
717 if (!new_style_driver)
720 /* set the mrru, mtu and flags */
721 if (ioctl(ppp_dev_fd, PPPIOCSMRRU, &mrru) < 0)
722 error("Couldn't set MRRU: %m");
724 modify_flags(ppp_dev_fd, SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ|SC_MULTILINK,
725 ((rssn? SC_MP_SHORTSEQ: 0) | (tssn? SC_MP_XSHORTSEQ: 0)
726 | (mrru? SC_MULTILINK: 0)));
728 /* connect up the channel */
729 if (ioctl(ppp_fd, PPPIOCCONNECT, &ifunit) < 0)
730 fatal("Couldn't attach to PPP unit %d: %m", ifunit);
735 * make_new_bundle - create a new PPP unit (i.e. a bundle)
736 * and connect our channel to it. This should only get called
737 * if `multilink' was set at the time establish_ppp was called.
738 * In demand mode this uses our existing bundle instead of making
741 void make_new_bundle(int mrru, int mtru, int rssn, int tssn)
743 if (!new_style_driver)
746 /* make us a ppp unit */
747 if (make_ppp_unit() < 0)
750 /* set the mrru and flags */
751 cfg_bundle(mrru, mtru, rssn, tssn);
755 * bundle_attach - attach our link to a given PPP unit.
756 * We assume the unit is controlled by another pppd.
758 int bundle_attach(int ifnum)
762 if (!new_style_driver)
765 master_fd = open("/dev/ppp", O_RDWR);
767 fatal("Couldn't open /dev/ppp: %m");
768 if (ioctl(master_fd, PPPIOCATTACH, &ifnum) < 0) {
769 if (errno == ENXIO) {
771 return 0; /* doesn't still exist */
773 fatal("Couldn't attach to interface unit %d: %m\n", ifnum);
775 if (ioctl(ppp_fd, PPPIOCCONNECT, &ifnum) < 0)
776 fatal("Couldn't connect to interface unit %d: %m", ifnum);
777 modify_flags(master_fd, 0, SC_MULTILINK);
785 * destroy_bundle - tell the driver to destroy our bundle.
787 void destroy_bundle(void)
789 if (ppp_dev_fd >= 0) {
791 remove_fd(ppp_dev_fd);
796 /********************************************************************
798 * clean_check - Fetch the flags for the device and generate
799 * appropriate error messages.
801 void clean_check(void)
807 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
809 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
811 s = "all had bit 7 set to 1";
815 s = "all had bit 7 set to 0";
819 s = "all had odd parity";
823 s = "all had even parity";
828 warn("Receive serial link is not 8-bit clean:");
829 warn("Problem: %s", s);
837 * List of valid speeds.
841 int speed_int, speed_val;
937 { 1000000, B1000000 },
940 { 1152000, B1152000 },
943 { 1500000, B1500000 },
946 { 2000000, B2000000 },
949 { 2500000, B2500000 },
952 { 3000000, B3000000 },
955 { 3500000, B3500000 },
958 { 4000000, B4000000 },
963 /********************************************************************
965 * Translate from bits/second to a speed_t.
968 static int translate_speed (int bps)
970 struct speed *speedp;
973 for (speedp = speeds; speedp->speed_int; speedp++) {
974 if (bps == speedp->speed_int)
975 return speedp->speed_val;
981 /********************************************************************
983 * Translate from a speed_t to bits/second.
986 static int baud_rate_of (int speed)
988 struct speed *speedp;
991 for (speedp = speeds; speedp->speed_int; speedp++) {
992 if (speed == speedp->speed_val)
993 return speedp->speed_int;
999 /********************************************************************
1001 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
1002 * at the requested speed, etc. If `local' is true, set CLOCAL
1003 * regardless of whether the modem option was specified.
1006 void set_up_tty(int tty_fd, int local)
1009 struct termios tios;
1012 if (tcgetattr(tty_fd, &tios) < 0) {
1013 if (!ok_error(errno))
1014 fatal("tcgetattr: %m (line %d)", __LINE__);
1021 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
1022 tios.c_cflag |= CS8 | CREAD | HUPCL;
1024 tios.c_iflag = IGNBRK | IGNPAR;
1027 tios.c_cc[VMIN] = 1;
1028 tios.c_cc[VTIME] = 0;
1030 if (local || !modem)
1031 tios.c_cflag ^= (CLOCAL | HUPCL);
1035 tios.c_cflag |= CRTSCTS;
1039 tios.c_iflag |= IXON | IXOFF;
1040 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
1041 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
1045 tios.c_cflag &= ~CRTSCTS;
1053 tios.c_cflag |= CSTOPB;
1056 speed = translate_speed(inspeed);
1058 cfsetospeed (&tios, speed);
1059 cfsetispeed (&tios, speed);
1060 speed = cfgetospeed(&tios);
1061 baud_rate = baud_rate_of(speed);
1064 tios.c_cflag &= ~CBAUD;
1065 tios.c_cflag |= BOTHER;
1066 tios.c_ospeed = inspeed;
1068 /* B0 sets input baudrate to the output baudrate */
1069 tios.c_cflag &= ~(CBAUD << IBSHIFT);
1070 tios.c_cflag |= B0 << IBSHIFT;
1071 tios.c_ispeed = inspeed;
1073 baud_rate = inspeed;
1080 speed = cfgetospeed(&tios);
1081 baud_rate = baud_rate_of(speed);
1084 baud_rate = tios.c_ospeed;
1089 * We can't proceed if the serial port baud rate is unknown,
1090 * since that implies that the serial port is disabled.
1094 fatal("speed %d not supported", inspeed);
1096 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
1099 while (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0 && !ok_error(errno))
1101 fatal("tcsetattr: %m (line %d)", __LINE__);
1105 /********************************************************************
1107 * setdtr - control the DTR line on the serial port.
1108 * This is called from die(), so it shouldn't call die().
1111 void setdtr (int tty_fd, int on)
1113 int modembits = TIOCM_DTR;
1115 ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
1118 /********************************************************************
1120 * restore_tty - restore the terminal to the saved settings.
1123 void restore_tty (int tty_fd)
1128 * Turn off echoing, because otherwise we can get into
1129 * a loop with the tty and the modem echoing to each other.
1130 * We presume we are the sole user of this tty device, so
1131 * when we close it, it will revert to its defaults anyway.
1133 if (!default_device)
1134 inittermios.c_lflag &= ~(ECHO | ECHONL);
1136 if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0) {
1137 if (! ok_error (errno))
1138 warn("tcsetattr: %m (line %d)", __LINE__);
1143 /********************************************************************
1145 * output - Output PPP packet.
1148 void output (int unit, unsigned char *p, int len)
1153 dump_packet("sent", p, len);
1154 if (snoop_send_hook) snoop_send_hook(p, len);
1156 if (len < PPP_HDRLEN)
1158 if (new_style_driver) {
1161 proto = (p[0] << 8) + p[1];
1162 if (ppp_dev_fd >= 0 && !(proto >= 0xc000 || proto == PPP_CCPFRAG))
1165 if (write(fd, p, len) < 0) {
1166 if (errno == EWOULDBLOCK || errno == EAGAIN || errno == ENOBUFS
1167 || errno == ENXIO || errno == EIO || errno == EINTR)
1168 warn("write: warning: %m (%d)", errno);
1170 error("write: %m (%d)", errno);
1174 /********************************************************************
1176 * wait_input - wait until there is data available,
1177 * for the length of time specified by *timo (indefinite
1181 void wait_input(struct timeval *timo)
1188 n = select(max_in_fd + 1, &ready, NULL, &exc, timo);
1189 if (n < 0 && errno != EINTR)
1190 fatal("select: %m");
1194 * add_fd - add an fd to the set that wait_input waits for.
1198 if (fd >= FD_SETSIZE)
1199 fatal("internal error: file descriptor too large (%d)", fd);
1200 FD_SET(fd, &in_fds);
1206 * remove_fd - remove an fd from the set that wait_input waits for.
1208 void remove_fd(int fd)
1210 FD_CLR(fd, &in_fds);
1214 /********************************************************************
1216 * read_packet - get a PPP packet from the serial device.
1219 int read_packet (unsigned char *buf)
1223 len = PPP_MRU + PPP_HDRLEN;
1224 if (new_style_driver) {
1225 *buf++ = PPP_ALLSTATIONS;
1231 nr = read(ppp_fd, buf, len);
1232 if (nr < 0 && errno != EWOULDBLOCK && errno != EAGAIN
1233 && errno != EIO && errno != EINTR)
1235 if (nr < 0 && errno == ENXIO)
1238 if (nr < 0 && new_style_driver && ppp_dev_fd >= 0 && !bundle_eof) {
1239 /* N.B. we read ppp_fd first since LCP packets come in there. */
1240 nr = read(ppp_dev_fd, buf, len);
1241 if (nr < 0 && errno != EWOULDBLOCK && errno != EAGAIN
1242 && errno != EIO && errno != EINTR)
1243 error("read /dev/ppp: %m");
1244 if (nr < 0 && errno == ENXIO)
1246 if (nr == 0 && doing_multilink) {
1247 remove_fd(ppp_dev_fd);
1251 if (new_style_driver && ppp_fd < 0 && ppp_dev_fd < 0)
1253 return (new_style_driver && nr > 0)? nr+2: nr;
1256 /********************************************************************
1258 * get_loop_output - get outgoing packets from the ppp device,
1259 * and detect when we want to bring the real link up.
1260 * Return value is 1 if we need to bring up the link, 0 otherwise.
1263 get_loop_output(void)
1268 if (new_style_driver) {
1269 while ((n = read_packet(inpacket_buf)) > 0)
1270 if (loop_frame(inpacket_buf, n))
1275 while ((n = read(master_fd, inbuf, sizeof(inbuf))) > 0)
1276 if (loop_chars(inbuf, n))
1280 fatal("eof on loopback");
1282 if (errno != EWOULDBLOCK && errno != EAGAIN)
1283 fatal("read from loopback: %m(%d)", errno);
1289 * netif_set_mtu - set the MTU on the PPP network interface.
1292 netif_set_mtu(int unit, int mtu)
1296 memset (&ifr, '\0', sizeof (ifr));
1297 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1300 if (ifunit >= 0 && ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
1301 error("ioctl(SIOCSIFMTU): %m (line %d)", __LINE__);
1305 * netif_get_mtu - get the MTU on the PPP network interface.
1308 netif_get_mtu(int unit)
1312 memset (&ifr, '\0', sizeof (ifr));
1313 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1315 if (ifunit >= 0 && ioctl(sock_fd, SIOCGIFMTU, (caddr_t) &ifr) < 0) {
1316 error("ioctl(SIOCGIFMTU): %m (line %d)", __LINE__);
1322 /********************************************************************
1324 * tty_send_config - configure the transmit characteristics of
1325 * the ppp interface.
1328 void tty_send_config(int mtu, u_int32_t asyncmap, int pcomp, int accomp)
1335 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
1336 if (errno != EIO && errno != ENOTTY)
1337 error("Couldn't set transmit async character map: %m");
1342 x = (pcomp? SC_COMP_PROT: 0) | (accomp? SC_COMP_AC: 0)
1343 | (sync_serial? SC_SYNC: 0);
1344 modify_flags(ppp_fd, SC_COMP_PROT|SC_COMP_AC|SC_SYNC, x);
1347 /********************************************************************
1349 * tty_set_xaccm - set the extended transmit ACCM for the interface.
1352 void tty_set_xaccm (ext_accm accm)
1356 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY) {
1357 if ( ! ok_error (errno))
1358 warn("ioctl(set extended ACCM): %m (line %d)", __LINE__);
1362 /********************************************************************
1364 * tty_recv_config - configure the receive-side characteristics of
1365 * the ppp interface.
1368 void tty_recv_config(int mru, u_int32_t asyncmap, int pcomp, int accomp)
1371 * If we were called because the link has gone down then there is nothing
1372 * which may be done. Just return without incident.
1377 * Set the receiver parameters
1379 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
1380 if (errno != EIO && errno != ENOTTY)
1381 error("Couldn't set channel receive MRU: %m");
1383 if (new_style_driver && ppp_dev_fd >= 0
1384 && ioctl(ppp_dev_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
1385 error("Couldn't set MRU in generic PPP layer: %m");
1387 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
1388 if (errno != EIO && errno != ENOTTY)
1389 error("Couldn't set channel receive asyncmap: %m");
1393 /********************************************************************
1395 * ccp_test - ask kernel whether a given compression method
1396 * is acceptable for use.
1400 ccp_test(int unit, u_char *opt_ptr, int opt_len, int for_transmit)
1402 struct ppp_option_data data;
1404 memset (&data, '\0', sizeof (data));
1406 data.length = opt_len;
1407 data.transmit = for_transmit;
1409 if (ioctl(ppp_dev_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
1412 return (errno == ENOBUFS)? 0: -1;
1415 /********************************************************************
1417 * ccp_flags_set - inform kernel about the current state of CCP.
1420 void ccp_flags_set (int unit, int isopen, int isup)
1424 x = (isopen? SC_CCP_OPEN: 0) | (isup? SC_CCP_UP: 0);
1425 if (still_ppp() && ppp_dev_fd >= 0)
1426 modify_flags(ppp_dev_fd, SC_CCP_OPEN|SC_CCP_UP, x);
1429 #ifdef PPP_WITH_FILTER
1431 * set_filters - set the active and pass filters in the kernel driver.
1433 int set_filters(struct bpf_program *pass, struct bpf_program *active)
1435 struct sock_fprog fp;
1437 fp.len = pass->bf_len;
1438 fp.filter = (struct sock_filter *) pass->bf_insns;
1439 if (ioctl(ppp_dev_fd, PPPIOCSPASS, &fp) < 0) {
1440 if (errno == ENOTTY)
1441 warn("kernel does not support PPP filtering");
1443 error("Couldn't set pass-filter in kernel: %m");
1446 fp.len = active->bf_len;
1447 fp.filter = (struct sock_filter *) active->bf_insns;
1448 if (ioctl(ppp_dev_fd, PPPIOCSACTIVE, &fp) < 0) {
1449 error("Couldn't set active-filter in kernel: %m");
1454 #endif /* PPP_WITH_FILTER */
1456 /********************************************************************
1458 * get_idle_time - return how long the link has been idle.
1461 get_idle_time(int u, struct ppp_idle *ip)
1463 return ioctl(ppp_dev_fd, PPPIOCGIDLE, ip) >= 0;
1466 /********************************************************************
1468 * get_ppp_stats_iocl - return statistics for the link, using the ioctl() method,
1469 * this only supports 32-bit counters, so need to count the wraps.
1472 get_ppp_stats_ioctl(int u, struct pppd_stats *stats)
1474 static u_int32_t previbytes = 0;
1475 static u_int32_t prevobytes = 0;
1476 static u_int32_t iwraps = 0;
1477 static u_int32_t owraps = 0;
1480 struct ppp_stats data;
1482 memset (&req, 0, sizeof (req));
1484 req.ifr_data = (caddr_t) &data;
1485 strlcpy(req.ifr_name, ifname, sizeof(req.ifr_name));
1486 if (ioctl(sock_fd, SIOCGPPPSTATS, &req) < 0) {
1487 error("Couldn't get PPP statistics: %m");
1490 stats->bytes_in = data.p.ppp_ibytes;
1491 stats->bytes_out = data.p.ppp_obytes;
1492 stats->pkts_in = data.p.ppp_ipackets;
1493 stats->pkts_out = data.p.ppp_opackets;
1495 if (stats->bytes_in < previbytes)
1497 if (stats->bytes_out < prevobytes)
1500 previbytes = stats->bytes_in;
1501 prevobytes = stats->bytes_out;
1503 stats->bytes_in += (uint64_t)iwraps << 32;
1504 stats->bytes_out += (uint64_t)owraps << 32;
1509 /********************************************************************
1510 * get_ppp_stats_rtnetlink - return statistics for the link, using rtnetlink
1511 * This provides native 64-bit counters.
1514 get_ppp_stats_rtnetlink(int u, struct pppd_stats *stats)
1516 static int rtnl_fd = -1;
1518 struct sockaddr_nl nladdr;
1520 struct nlmsghdr nlh;
1521 struct if_stats_msg ifsm;
1524 struct nlmsghdr nlh;
1527 struct nlmsgerr nlerr;
1533 /* We only case about these first fields from rtnl_link_stats64 */
1534 uint64_t rx_packets;
1535 uint64_t tx_packets;
1539 char __end_stats[0];
1547 memset(&nladdr, 0, sizeof(nladdr));
1548 nladdr.nl_family = AF_NETLINK;
1551 rtnl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1553 error("get_ppp_stats_rtnetlink: error creating NETLINK socket: %m (line %d)", __LINE__);
1557 if (bind(rtnl_fd, (struct sockaddr *)&nladdr, sizeof(nladdr)) < 0) {
1558 error("get_ppp_stats_rtnetlink: bind(AF_NETLINK): %m (line %d)", __LINE__);
1563 memset(&nlreq, 0, sizeof(nlreq));
1564 nlreq.nlh.nlmsg_len = sizeof(nlreq);
1565 nlreq.nlh.nlmsg_type = RTM_GETSTATS;
1566 nlreq.nlh.nlmsg_flags = NLM_F_REQUEST;
1568 nlreq.ifsm.ifindex = if_nametoindex(ifname);
1569 nlreq.ifsm.filter_mask = IFLA_STATS_LINK_64;
1571 memset(&iov, 0, sizeof(iov));
1572 iov.iov_base = &nlreq;
1573 iov.iov_len = sizeof(nlreq);
1575 memset(&msg, 0, sizeof(msg));
1576 msg.msg_name = &nladdr;
1577 msg.msg_namelen = sizeof(nladdr);
1581 if (sendmsg(rtnl_fd, &msg, 0) < 0) {
1582 error("get_ppp_stats_rtnetlink: sendmsg(RTM_GETSTATS): %m (line %d)", __LINE__);
1586 /* We just need to repoint to IOV ... everything else stays the same */
1587 iov.iov_base = &nlresp;
1588 iov.iov_len = sizeof(nlresp);
1590 nlresplen = recvmsg(rtnl_fd, &msg, 0);
1592 if (nlresplen < 0) {
1593 error("get_ppp_stats_rtnetlink: recvmsg(RTM_GETSTATS): %m (line %d)", __LINE__);
1597 if (nlresplen < sizeof(nlresp.nlh)) {
1598 error("get_ppp_stats_rtnetlink: Netlink response message was incomplete (line %d)", __LINE__);
1602 if (nlresp.nlh.nlmsg_type == NLMSG_ERROR) {
1603 if (nlresplen < offsetof(struct nlresp, __end_err)) {
1604 if (kernel_version >= KVERSION(4,7,0))
1605 error("get_ppp_stats_rtnetlink: Netlink responded with error: %s (line %d)", strerror(-nlresp.nlerr.error), __LINE__);
1607 error("get_ppp_stats_rtnetlink: Netlink responded with an error message, but the nlmsgerr structure is incomplete (line %d).",
1613 if (nlresp.nlh.nlmsg_type != RTM_NEWSTATS) {
1614 error("get_ppp_stats_rtnetlink: Expected RTM_NEWSTATS response, found something else (mlmsg_type %d, line %d)",
1615 nlresp.nlh.nlmsg_type, __LINE__);
1619 if (nlresplen < offsetof(struct nlresp, __end_stats)) {
1620 error("get_ppp_stats_rtnetlink: Obtained an insufficiently sized rtnl_link_stats64 struct from the kernel (line %d).", __LINE__);
1624 stats->bytes_in = nlresp.stats.rx_bytes;
1625 stats->bytes_out = nlresp.stats.tx_bytes;
1626 stats->pkts_in = nlresp.stats.rx_packets;
1627 stats->pkts_out = nlresp.stats.tx_packets;
1636 /********************************************************************
1637 * get_ppp_stats_sysfs - return statistics for the link, using the files in sysfs,
1638 * this provides native 64-bit counters.
1641 get_ppp_stats_sysfs(int u, struct pppd_stats *stats)
1643 char fname[PATH_MAX+1];
1644 char buf[21], *err; /* 2^64 < 10^20 */
1646 unsigned long long val;
1653 #define statfield(fn, field) { .fname = #fn, .ptr = &stats->field, .size = sizeof(stats->field) }
1654 statfield(rx_bytes, bytes_in),
1655 statfield(tx_bytes, bytes_out),
1656 statfield(rx_packets, pkts_in),
1657 statfield(tx_packets, pkts_out),
1661 blen = snprintf(fname, sizeof(fname), "/sys/class/net/%s/statistics/", ifname);
1662 if (blen >= sizeof(fname))
1663 return 0; /* ifname max 15, so this should be impossible */
1665 for (int i = 0; i < sizeof(slist) / sizeof(*slist); ++i) {
1666 if (snprintf(fname + blen, sizeof(fname) - blen, "%s", slist[i].fname) >= sizeof(fname) - blen) {
1668 error("sysfs stats: filename %s/%s overflowed PATH_MAX", fname, slist[i].fname);
1672 fd = open(fname, O_RDONLY);
1674 error("%s: %m", fname);
1678 rlen = read(fd, buf, sizeof(buf) - 1);
1681 error("%s: %m", fname);
1684 /* trim trailing \n if present */
1685 while (rlen > 0 && buf[rlen-1] == '\n')
1690 val = strtoull(buf, &err, 10);
1691 if (*buf < '0' || *buf > '9' || errno != 0 || *err) {
1692 error("string to number conversion error converting %s (from %s) for remaining string %s%s%s",
1693 buf, fname, err, errno ? ": " : "", errno ? strerror(errno) : "");
1696 switch (slist[i].size) {
1697 #define stattype(type) case sizeof(type): *(type*)slist[i].ptr = (type)val; break
1704 error("Don't know how to store stats for %s of size %u", slist[i].fname, slist[i].size);
1712 /********************************************************************
1713 * Periodic timer function to be used to keep stats up to date in case of ioctl
1716 * Given the 25s interval this should be fine up to data rates of 1.37Gbps.
1717 * If you do change the timer, remember to also bring the get_ppp_stats (which
1718 * sets up the initial trigger) as well.
1721 ppp_stats_poller(void* u)
1723 struct pppd_stats dummy;
1724 get_ppp_stats_ioctl((long)u, &dummy);
1725 TIMEOUT(ppp_stats_poller, u, 25);
1728 /********************************************************************
1729 * get_ppp_stats - return statistics for the link.
1731 int get_ppp_stats(int u, struct pppd_stats *stats)
1733 static int (*func)(int, struct pppd_stats*) = NULL;
1736 if (get_ppp_stats_rtnetlink(u, stats)) {
1737 func = get_ppp_stats_rtnetlink;
1740 if (get_ppp_stats_sysfs(u, stats)) {
1741 func = get_ppp_stats_sysfs;
1744 warn("statistics falling back to ioctl which only supports 32-bit counters");
1745 func = get_ppp_stats_ioctl;
1746 TIMEOUT(ppp_stats_poller, (void*)(long)u, 25);
1749 return func(u, stats);
1752 /********************************************************************
1754 * ccp_fatal_error - returns 1 if decompression was disabled as a
1755 * result of an error detected after decompression of a packet,
1756 * 0 otherwise. This is necessary because of patent nonsense.
1759 int ccp_fatal_error (int unit)
1763 if (ioctl(ppp_dev_fd, PPPIOCGFLAGS, &flags) < 0) {
1764 error("Couldn't read compression error flags: %m");
1767 return flags & SC_DC_FERROR;
1770 /********************************************************************
1772 * path_to_procfs - find the path to the proc file system mount point
1774 static char proc_path[MAXPATHLEN];
1775 static int proc_path_len;
1777 static char *path_to_procfs(const char *tail)
1779 struct mntent *mntent;
1782 if (proc_path_len == 0) {
1783 /* Default the mount location of /proc */
1784 strlcpy (proc_path, "/proc", sizeof(proc_path));
1786 fp = fopen(MOUNTED, "r");
1788 while ((mntent = getmntent(fp)) != NULL) {
1789 if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0)
1791 if (strcmp(mntent->mnt_type, "proc") == 0) {
1792 strlcpy(proc_path, mntent->mnt_dir, sizeof(proc_path));
1793 proc_path_len = strlen(proc_path);
1801 strlcpy(proc_path + proc_path_len, tail,
1802 sizeof(proc_path) - proc_path_len);
1807 * /proc/net/route parsing stuff.
1809 #define ROUTE_MAX_COLS 12
1810 FILE *route_fd = (FILE *) 0;
1811 static char route_buffer[512];
1812 static int route_dev_col, route_dest_col, route_gw_col;
1813 static int route_flags_col, route_metric_col, route_mask_col;
1814 static int route_num_cols;
1816 static int open_route_table (void);
1817 static void close_route_table (void);
1818 static int read_route_table (struct rtentry *rt);
1820 /********************************************************************
1822 * close_route_table - close the interface to the route table
1825 static void close_route_table (void)
1827 if (route_fd != (FILE *) 0) {
1829 route_fd = (FILE *) 0;
1833 /********************************************************************
1835 * open_route_table - open the interface to the route table
1837 static char route_delims[] = " \t\n";
1839 static int open_route_table (void)
1843 close_route_table();
1845 path = path_to_procfs("/net/route");
1846 route_fd = fopen (path, "r");
1847 if (route_fd == NULL) {
1848 error("can't open routing table %s: %m", path);
1852 route_dev_col = 0; /* default to usual columns */
1855 route_flags_col = 3;
1856 route_metric_col = 6;
1860 /* parse header line */
1861 if (fgets(route_buffer, sizeof(route_buffer), route_fd) != 0) {
1862 char *p = route_buffer, *q;
1864 for (col = 0; col < ROUTE_MAX_COLS; ++col) {
1866 if ((q = strtok(p, route_delims)) == 0)
1868 if (strcasecmp(q, "iface") == 0)
1869 route_dev_col = col;
1870 else if (strcasecmp(q, "destination") == 0)
1871 route_dest_col = col;
1872 else if (strcasecmp(q, "gateway") == 0)
1874 else if (strcasecmp(q, "flags") == 0)
1875 route_flags_col = col;
1876 else if (strcasecmp(q, "mask") == 0)
1877 route_mask_col = col;
1880 if (used && col >= route_num_cols)
1881 route_num_cols = col + 1;
1889 /********************************************************************
1891 * read_route_table - read the next entry from the route table
1894 static int read_route_table(struct rtentry *rt)
1896 char *cols[ROUTE_MAX_COLS], *p;
1899 memset (rt, '\0', sizeof (struct rtentry));
1901 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
1905 for (col = 0; col < route_num_cols; ++col) {
1906 cols[col] = strtok(p, route_delims);
1907 if (cols[col] == NULL)
1908 return 0; /* didn't get enough columns */
1912 SET_SA_FAMILY (rt->rt_dst, AF_INET);
1913 SET_SA_FAMILY (rt->rt_gateway, AF_INET);
1915 SIN_ADDR(rt->rt_dst) = strtoul(cols[route_dest_col], NULL, 16);
1916 SIN_ADDR(rt->rt_gateway) = strtoul(cols[route_gw_col], NULL, 16);
1917 SIN_ADDR(rt->rt_genmask) = strtoul(cols[route_mask_col], NULL, 16);
1919 rt->rt_flags = (short) strtoul(cols[route_flags_col], NULL, 16);
1920 rt->rt_metric = (short) strtoul(cols[route_metric_col], NULL, 10);
1921 rt->rt_dev = cols[route_dev_col];
1926 /********************************************************************
1928 * defaultroute_exists - determine if there is a default route
1929 * with the given metric (or negative for any)
1932 static int defaultroute_exists (struct rtentry *rt, int metric)
1936 if (!open_route_table())
1939 while (read_route_table(rt) != 0) {
1940 if ((rt->rt_flags & RTF_UP) == 0)
1943 if (kernel_version > KVERSION(2,1,0) && SIN_ADDR(rt->rt_genmask) != 0)
1945 if (SIN_ADDR(rt->rt_dst) == 0L && (metric < 0
1946 || rt->rt_metric == metric)) {
1952 close_route_table();
1957 * have_route_to - determine if the system has any route to
1958 * a given IP address. `addr' is in network byte order.
1959 * Return value is 1 if yes, 0 if no, -1 if don't know.
1960 * For demand mode to work properly, we have to ignore routes
1961 * through our own interface.
1963 int have_route_to(u_int32_t addr)
1968 if (!open_route_table())
1969 return -1; /* don't know */
1971 while (read_route_table(&rt)) {
1972 if ((rt.rt_flags & RTF_UP) == 0 || strcmp(rt.rt_dev, ifname) == 0)
1974 if ((addr & SIN_ADDR(rt.rt_genmask)) == SIN_ADDR(rt.rt_dst)) {
1980 close_route_table();
1984 /********************************************************************
1986 * sifdefaultroute - assign a default route through the address given.
1988 * If the global default_rt_repl_rest flag is set, then this function
1989 * already replaced the original system defaultroute with some other
1990 * route and it should just replace the current defaultroute with
1991 * another one, without saving the current route. Use: demand mode,
1992 * when pppd sets first a defaultroute it it's temporary ppp0 addresses
1993 * and then changes the temporary addresses to the addresses for the real
1994 * ppp connection when it has come up.
1997 int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway, bool replace)
1999 struct rtentry rt, tmp_rt;
2000 struct rtentry *del_rt = NULL;
2002 if (default_rt_repl_rest) {
2003 /* We have already replaced the original defaultroute, if we
2004 * are called again, we will delete the current default route
2005 * and set the new default route in this function.
2006 * - this is normally only the case the doing demand: */
2007 if (defaultroute_exists(&tmp_rt, -1))
2009 } else if (defaultroute_exists(&old_def_rt, -1 ) &&
2010 strcmp( old_def_rt.rt_dev, ifname) != 0) {
2012 * We did not yet replace an existing default route, let's
2013 * check if we should save and replace a default route:
2015 u_int32_t old_gateway = SIN_ADDR(old_def_rt.rt_gateway);
2017 if (old_gateway != gateway) {
2019 error("not replacing default route to %s [%I]",
2020 old_def_rt.rt_dev, old_gateway);
2023 /* we need to copy rt_dev because we need it permanent too: */
2024 char * tmp_dev = malloc(strlen(old_def_rt.rt_dev)+1);
2025 strcpy(tmp_dev, old_def_rt.rt_dev);
2026 old_def_rt.rt_dev = tmp_dev;
2028 notice("replacing old default route to %s [%I]",
2029 old_def_rt.rt_dev, old_gateway);
2030 default_rt_repl_rest = 1;
2031 del_rt = &old_def_rt;
2036 memset (&rt, 0, sizeof (rt));
2037 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2040 rt.rt_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
2042 if (kernel_version > KVERSION(2,1,0)) {
2043 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2044 SIN_ADDR(rt.rt_genmask) = 0L;
2047 rt.rt_flags = RTF_UP;
2048 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
2049 if ( ! ok_error ( errno ))
2050 error("default route ioctl(SIOCADDRT): %m");
2053 if (default_rt_repl_rest && del_rt)
2054 if (ioctl(sock_fd, SIOCDELRT, del_rt) < 0) {
2055 if ( ! ok_error ( errno ))
2056 error("del old default route ioctl(SIOCDELRT): %m(%d)", errno);
2060 have_default_route = 1;
2064 /********************************************************************
2066 * cifdefaultroute - delete a default route through the address given.
2069 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
2073 have_default_route = 0;
2075 memset (&rt, '\0', sizeof (rt));
2076 SET_SA_FAMILY (rt.rt_dst, AF_INET);
2077 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2082 rt.rt_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
2084 if (kernel_version > KVERSION(2,1,0)) {
2085 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2086 SIN_ADDR(rt.rt_genmask) = 0L;
2089 rt.rt_flags = RTF_UP;
2090 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
2092 if ( ! ok_error ( errno ))
2093 error("default route ioctl(SIOCDELRT): %m");
2097 if (default_rt_repl_rest) {
2098 notice("restoring old default route to %s [%I]",
2099 old_def_rt.rt_dev, SIN_ADDR(old_def_rt.rt_gateway));
2100 if (ioctl(sock_fd, SIOCADDRT, &old_def_rt) < 0) {
2101 if ( ! ok_error ( errno ))
2102 error("restore default route ioctl(SIOCADDRT): %m(%d)", errno);
2105 default_rt_repl_rest = 0;
2111 #ifdef PPP_WITH_IPV6CP
2113 * /proc/net/ipv6_route parsing stuff.
2115 static int route_dest_plen_col;
2116 static int open_route6_table (void);
2117 static int read_route6_table (struct in6_rtmsg *rt);
2119 /********************************************************************
2121 * open_route6_table - open the interface to the route table
2123 static int open_route6_table (void)
2127 close_route_table();
2129 path = path_to_procfs("/net/ipv6_route");
2130 route_fd = fopen (path, "r");
2131 if (route_fd == NULL) {
2132 error("can't open routing table %s: %m", path);
2136 /* default to usual columns */
2138 route_dest_plen_col = 1;
2140 route_metric_col = 5;
2141 route_flags_col = 8;
2143 route_num_cols = 10;
2148 /********************************************************************
2150 * read_route6_table - read the next entry from the route table
2153 static void hex_to_in6_addr(struct in6_addr *addr, const char *s)
2160 for (i = 0; i < 4; i++) {
2161 memcpy(hex8, s + 8*i, 8);
2162 v = strtoul(hex8, NULL, 16);
2163 addr->s6_addr32[i] = v;
2167 static int read_route6_table(struct in6_rtmsg *rt)
2169 char *cols[ROUTE_MAX_COLS], *p;
2172 memset (rt, '\0', sizeof (struct in6_rtmsg));
2174 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
2178 for (col = 0; col < route_num_cols; ++col) {
2179 cols[col] = strtok(p, route_delims);
2180 if (cols[col] == NULL)
2181 return 0; /* didn't get enough columns */
2185 hex_to_in6_addr(&rt->rtmsg_dst, cols[route_dest_col]);
2186 rt->rtmsg_dst_len = strtoul(cols[route_dest_plen_col], NULL, 16);
2187 hex_to_in6_addr(&rt->rtmsg_gateway, cols[route_gw_col]);
2189 rt->rtmsg_metric = strtoul(cols[route_metric_col], NULL, 16);
2190 rt->rtmsg_flags = strtoul(cols[route_flags_col], NULL, 16);
2191 rt->rtmsg_ifindex = if_nametoindex(cols[route_dev_col]);
2196 /********************************************************************
2198 * defaultroute6_exists - determine if there is a default route
2201 static int defaultroute6_exists (struct in6_rtmsg *rt, int metric)
2205 if (!open_route6_table())
2208 while (read_route6_table(rt) != 0) {
2209 if ((rt->rtmsg_flags & RTF_UP) == 0)
2212 if (rt->rtmsg_dst_len != 0)
2214 if (rt->rtmsg_dst.s6_addr32[0] == 0L
2215 && rt->rtmsg_dst.s6_addr32[1] == 0L
2216 && rt->rtmsg_dst.s6_addr32[2] == 0L
2217 && rt->rtmsg_dst.s6_addr32[3] == 0L
2218 && (metric < 0 || rt->rtmsg_metric == metric)) {
2224 close_route_table();
2228 /********************************************************************
2230 * sif6defaultroute - assign a default route through the address given.
2232 * If the global default_rt_repl_rest flag is set, then this function
2233 * already replaced the original system defaultroute with some other
2234 * route and it should just replace the current defaultroute with
2235 * another one, without saving the current route. Use: demand mode,
2236 * when pppd sets first a defaultroute it it's temporary ppp0 addresses
2237 * and then changes the temporary addresses to the addresses for the real
2238 * ppp connection when it has come up.
2241 int sif6defaultroute (int unit, eui64_t ouraddr, eui64_t gateway)
2243 struct in6_rtmsg rt;
2244 char buf[IF_NAMESIZE];
2246 if (defaultroute6_exists(&rt, dfl_route_metric) &&
2247 rt.rtmsg_ifindex != if_nametoindex(ifname)) {
2248 if (rt.rtmsg_flags & RTF_GATEWAY)
2249 error("not replacing existing default route via gateway");
2251 error("not replacing existing default route through %s",
2252 if_indextoname(rt.rtmsg_ifindex, buf));
2256 memset (&rt, 0, sizeof (rt));
2258 rt.rtmsg_ifindex = if_nametoindex(ifname);
2259 rt.rtmsg_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
2260 rt.rtmsg_dst_len = 0;
2262 rt.rtmsg_flags = RTF_UP;
2263 if (ioctl(sock6_fd, SIOCADDRT, &rt) < 0) {
2264 if ( ! ok_error ( errno ))
2265 error("default route ioctl(SIOCADDRT): %m");
2269 have_default_route6 = 1;
2273 /********************************************************************
2275 * cif6defaultroute - delete a default route through the address given.
2278 int cif6defaultroute (int unit, eui64_t ouraddr, eui64_t gateway)
2280 struct in6_rtmsg rt;
2282 have_default_route6 = 0;
2284 memset (&rt, '\0', sizeof (rt));
2286 rt.rtmsg_ifindex = if_nametoindex(ifname);
2287 rt.rtmsg_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
2288 rt.rtmsg_dst_len = 0;
2290 rt.rtmsg_flags = RTF_UP;
2291 if (ioctl(sock6_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
2293 if ( ! ok_error ( errno ))
2294 error("default route ioctl(SIOCDELRT): %m");
2301 #endif /* PPP_WITH_IPV6CP */
2303 /********************************************************************
2305 * sifproxyarp - Make a proxy ARP entry for the peer.
2308 int sifproxyarp (int unit, u_int32_t his_adr)
2310 struct arpreq arpreq;
2313 if (has_proxy_arp == 0) {
2314 memset (&arpreq, '\0', sizeof(arpreq));
2316 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
2317 SIN_ADDR(arpreq.arp_pa) = his_adr;
2318 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
2320 * Get the hardware address of an interface on the same subnet
2321 * as our local address.
2323 if (!get_ether_addr(his_adr, &arpreq.arp_ha, proxy_arp_dev,
2324 sizeof(proxy_arp_dev))) {
2325 error("Cannot determine ethernet address for proxy ARP");
2328 strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev));
2330 if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0) {
2331 if ( ! ok_error ( errno ))
2332 error("ioctl(SIOCSARP): %m");
2335 proxy_arp_addr = his_adr;
2339 forw_path = path_to_procfs("/sys/net/ipv4/ip_forward");
2340 if (forw_path != 0) {
2341 int fd = open(forw_path, O_WRONLY);
2343 if (write(fd, "1", 1) != 1)
2344 error("Couldn't enable IP forwarding: %m");
2354 /********************************************************************
2356 * cifproxyarp - Delete the proxy ARP entry for the peer.
2359 int cifproxyarp (int unit, u_int32_t his_adr)
2361 struct arpreq arpreq;
2363 if (has_proxy_arp) {
2365 memset (&arpreq, '\0', sizeof(arpreq));
2366 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
2367 SIN_ADDR(arpreq.arp_pa) = his_adr;
2368 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
2369 strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev));
2371 if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0) {
2372 if ( ! ok_error ( errno ))
2373 warn("ioctl(SIOCDARP): %m");
2380 /********************************************************************
2382 * get_ether_addr - get the hardware address of an interface on the
2383 * the same subnet as ipaddr.
2386 static int get_ether_addr (u_int32_t ipaddr,
2387 struct sockaddr *hwaddr,
2388 char *name, int namelen)
2390 struct ifreq *ifr, *ifend;
2391 u_int32_t ina, mask;
2393 struct ifreq ifreq, bestifreq;
2395 struct ifreq ifs[MAX_IFS];
2397 u_int32_t bestmask=0;
2398 int found_interface = 0;
2400 ifc.ifc_len = sizeof(ifs);
2402 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) {
2403 if ( ! ok_error ( errno ))
2404 error("ioctl(SIOCGIFCONF): %m (line %d)", __LINE__);
2409 * Scan through looking for an interface with an Internet
2410 * address on the same subnet as `ipaddr'.
2412 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
2413 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
2414 if (ifr->ifr_addr.sa_family == AF_INET) {
2415 ina = SIN_ADDR(ifr->ifr_addr);
2416 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
2418 * Check that the interface is up, and not point-to-point
2421 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
2424 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
2427 * Get its netmask and check that it's on the right subnet.
2429 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
2432 mask = SIN_ADDR(ifreq.ifr_addr);
2434 if (((ipaddr ^ ina) & mask) != 0)
2435 continue; /* no match */
2437 if (mask >= bestmask) {
2438 /* Compare using >= instead of > -- it is possible for
2439 an interface to have a netmask of 0.0.0.0 */
2440 found_interface = 1;
2447 if (!found_interface) return 0;
2449 strlcpy(name, bestifreq.ifr_name, namelen);
2451 /* trim off the :1 in eth0:1 */
2452 aliasp = strchr(name, ':');
2456 info("found interface %s for proxy arp", name);
2458 * Now get the hardware address.
2460 memset (&bestifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
2461 if (ioctl (sock_fd, SIOCGIFHWADDR, &bestifreq) < 0) {
2462 error("SIOCGIFHWADDR(%s): %m", bestifreq.ifr_name);
2467 &bestifreq.ifr_hwaddr,
2468 sizeof (struct sockaddr));
2474 * get_if_hwaddr - get the hardware address for the specified
2475 * network interface device.
2478 get_if_hwaddr(u_char *addr, char *name)
2483 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
2486 memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
2487 strlcpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
2488 ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
2491 memcpy(addr, ifreq.ifr_hwaddr.sa_data, 6);
2496 * get_first_ether_hwaddr - get the hardware address for the first
2497 * ethernet-style interface on this system.
2500 get_first_ether_hwaddr(u_char *addr)
2502 struct if_nameindex *if_ni, *i;
2506 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
2510 if_ni = if_nameindex();
2518 for (i = if_ni; !(i->if_index == 0 && i->if_name == NULL); i++) {
2519 memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
2520 strlcpy(ifreq.ifr_name, i->if_name, sizeof(ifreq.ifr_name));
2521 ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
2522 if (ret >= 0 && ifreq.ifr_hwaddr.sa_family == ARPHRD_ETHER) {
2523 memcpy(addr, ifreq.ifr_hwaddr.sa_data, 6);
2529 if_freenameindex(if_ni);
2535 /********************************************************************
2537 * Return user specified netmask, modified by any mask we might determine
2538 * for address `addr' (in network byte order).
2539 * Here we scan through the system's list of interfaces, looking for
2540 * any non-point-to-point interfaces which might appear to be on the same
2541 * network as `addr'. If we find any, we OR in their netmask to the
2542 * user-specified netmask.
2545 u_int32_t GetMask (u_int32_t addr)
2547 u_int32_t mask, nmask, ina;
2548 struct ifreq *ifr, *ifend, ifreq;
2550 struct ifreq ifs[MAX_IFS];
2554 if (IN_CLASSA(addr)) /* determine network mask for address class */
2555 nmask = IN_CLASSA_NET;
2556 else if (IN_CLASSB(addr))
2557 nmask = IN_CLASSB_NET;
2559 nmask = IN_CLASSC_NET;
2561 /* class D nets are disallowed by bad_ip_adrs */
2562 mask = netmask | htonl(nmask);
2564 * Scan through the system's network interfaces.
2566 ifc.ifc_len = sizeof(ifs);
2568 if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) {
2569 if ( ! ok_error ( errno ))
2570 warn("ioctl(SIOCGIFCONF): %m (line %d)", __LINE__);
2574 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
2575 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
2577 * Check the interface's internet address.
2579 if (ifr->ifr_addr.sa_family != AF_INET)
2581 ina = SIN_ADDR(ifr->ifr_addr);
2582 if (((ntohl(ina) ^ addr) & nmask) != 0)
2585 * Check that the interface is up, and not point-to-point nor loopback.
2587 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
2588 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
2591 if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
2594 * Get its netmask and OR it into our mask.
2596 if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
2598 mask |= SIN_ADDR(ifreq.ifr_addr);
2604 /********************************************************************
2606 * Internal routine to decode the version.modification.patch level
2609 static void decode_version (char *buf, int *version,
2610 int *modification, int *patch)
2614 *version = (int) strtoul (buf, &endp, 10);
2618 if (endp != buf && *endp == '.') {
2620 *modification = (int) strtoul (buf, &endp, 10);
2621 if (endp != buf && *endp == '.') {
2623 *patch = (int) strtoul (buf, &buf, 10);
2628 /********************************************************************
2630 * Procedure to determine if the PPP line discipline is registered.
2634 ppp_registered(void)
2642 * We used to open the serial device and set it to the ppp line
2643 * discipline here, in order to create a ppp unit. But that is
2644 * not a good idea - the user might have specified a device that
2645 * they can't open (permission, or maybe it doesn't really exist).
2646 * So we grab a pty master/slave pair and use that.
2648 if (!get_pty(&mfd, &local_fd, slave, 0)) {
2649 no_ppp_msg = "Couldn't determine if PPP is supported (no free ptys)";
2654 * Try to put the device into the PPP discipline.
2656 if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0) {
2657 error("ioctl(TIOCSETD(PPP)): %m (line %d)", __LINE__);
2666 /********************************************************************
2668 * ppp_available - check whether the system has any ppp interfaces
2669 * (in fact we check whether we can do an ioctl on ppp0).
2672 int ppp_available(void)
2677 int my_version, my_modification, my_patch;
2678 int osmaj, osmin, ospatch;
2680 /* get the kernel version now, since we are called before sys_init */
2682 osmaj = osmin = ospatch = 0;
2683 sscanf(utsname.release, "%d.%d.%d", &osmaj, &osmin, &ospatch);
2684 kernel_version = KVERSION(osmaj, osmin, ospatch);
2686 fd = open("/dev/ppp", O_RDWR);
2688 new_style_driver = 1;
2690 /* XXX should get from driver */
2692 driver_modification = 4;
2698 if (kernel_version >= KVERSION(2,3,13)) {
2699 error("Couldn't open the /dev/ppp device: %m");
2700 if (errno == ENOENT)
2702 "You need to create the /dev/ppp device node by\n"
2703 "executing the following command as root:\n"
2704 " mknod /dev/ppp c 108 0\n";
2705 else if (errno == ENODEV || errno == ENXIO)
2707 "Please load the ppp_generic kernel module.\n";
2711 /* we are running on a really really old kernel */
2713 "This system lacks kernel support for PPP. This could be because\n"
2714 "the PPP kernel module could not be loaded, or because PPP was not\n"
2715 "included in the kernel configuration. If PPP was included as a\n"
2716 "module, try `/sbin/modprobe -v ppp'. If that fails, check that\n"
2717 "ppp.o exists in /lib/modules/`uname -r`/net.\n"
2718 "See README.linux file in the ppp distribution for more details.\n";
2721 * Open a socket for doing the ioctl operations.
2723 s = socket(AF_INET, SOCK_DGRAM, 0);
2727 strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
2728 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
2730 * If the device did not exist then attempt to create one by putting the
2731 * current tty into the PPP discipline. If this works then obtain the
2732 * flags for the device again.
2735 if (ppp_registered()) {
2736 strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
2737 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
2741 * Ensure that the hardware address is for PPP and not something else
2744 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
2746 if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
2750 * This is the PPP device. Validate the version of the driver at this
2751 * point to ensure that this program will work with the driver.
2754 char abBuffer [1024];
2756 ifr.ifr_data = abBuffer;
2757 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
2759 error("Couldn't read driver version: %m");
2761 no_ppp_msg = "Sorry, couldn't verify kernel driver version\n";
2764 decode_version(abBuffer,
2766 &driver_modification,
2769 * Validate the version of the driver against the version that we used.
2771 decode_version(VERSION,
2776 /* The version numbers must match */
2777 if (driver_version != my_version)
2780 /* The modification levels must be legal */
2781 if (driver_modification < 3) {
2782 if (driver_modification >= 2) {
2783 /* we can cope with 2.2.0 and above */
2791 slprintf(route_buffer, sizeof(route_buffer),
2792 "Sorry - PPP driver version %d.%d.%d is out of date\n",
2793 driver_version, driver_modification, driver_patch);
2795 no_ppp_msg = route_buffer;
2803 #ifndef HAVE_LOGWTMP
2804 /********************************************************************
2806 * Update the wtmp file with the appropriate user name and tty device.
2809 void logwtmp (const char *line, const char *name, const char *host)
2811 struct utmp ut, *utp;
2812 pid_t mypid = getpid();
2818 * Update the signon database for users.
2819 * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
2821 utmpname(_PATH_UTMP);
2823 while ((utp = getutent()) && (utp->ut_pid != mypid))
2827 memcpy(&ut, utp, sizeof(ut));
2829 /* some gettys/telnetds don't initialize utmp... */
2830 memset(&ut, 0, sizeof(ut));
2832 if (ut.ut_id[0] == 0)
2833 strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
2835 strncpy(ut.ut_user, name, sizeof(ut.ut_user));
2836 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
2840 ut.ut_type = USER_PROCESS;
2843 /* Insert the host name if one is supplied */
2845 strncpy (ut.ut_host, host, sizeof(ut.ut_host));
2847 /* Insert the IP address of the remote system if IP is enabled */
2848 if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
2849 memcpy(&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
2850 sizeof(ut.ut_addr));
2852 /* CL: Makes sure that the logout works */
2853 if (*host == 0 && *name==0)
2859 * Update the wtmp file.
2862 updwtmp(_PATH_WTMP, &ut);
2864 wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
2866 flock(wtmp, LOCK_EX);
2868 if (write (wtmp, (char *)&ut, sizeof(ut)) != sizeof(ut))
2869 warn("error writing %s: %m", _PATH_WTMP);
2871 flock(wtmp, LOCK_UN);
2877 #endif /* HAVE_LOGWTMP */
2879 /********************************************************************
2881 * sifvjcomp - config tcp header compression
2884 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
2889 if (ioctl(ppp_dev_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
2890 error("Couldn't set up TCP header compression: %m");
2895 x = (vjcomp? SC_COMP_TCP: 0) | (cidcomp? 0: SC_NO_TCP_CCID);
2896 modify_flags(ppp_dev_fd, SC_COMP_TCP|SC_NO_TCP_CCID, x);
2901 /********************************************************************
2903 * sifup - Config the interface up and enable IP packets to pass.
2910 if ((ret = setifstate(u, 1)))
2916 /********************************************************************
2918 * sifdown - Disable the indicated protocol and config the interface
2919 * down if there are no remaining protocols.
2924 if (if_is_up && --if_is_up > 0)
2927 #ifdef PPP_WITH_IPV6CP
2930 #endif /* PPP_WITH_IPV6CP */
2932 return setifstate(u, 0);
2935 #ifdef PPP_WITH_IPV6CP
2936 /********************************************************************
2938 * sif6up - Config the interface up for IPv6
2945 if ((ret = setifstate(u, 1)))
2951 /********************************************************************
2953 * sif6down - Disable the IPv6CP protocol and config the interface
2954 * down if there are no remaining protocols.
2957 int sif6down (int u)
2964 return setifstate(u, 0);
2966 #endif /* PPP_WITH_IPV6CP */
2968 /********************************************************************
2970 * setifstate - Config the interface up or down
2973 static int setifstate (int u, int state)
2977 memset (&ifr, '\0', sizeof (ifr));
2978 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2979 if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
2980 if (! ok_error (errno))
2981 error("ioctl (SIOCGIFFLAGS): %m (line %d)", __LINE__);
2986 ifr.ifr_flags |= IFF_UP;
2988 ifr.ifr_flags &= ~IFF_UP;
2989 ifr.ifr_flags |= IFF_POINTOPOINT;
2990 if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
2991 if (! ok_error (errno))
2992 error("ioctl(SIOCSIFFLAGS): %m (line %d)", __LINE__);
2998 /********************************************************************
3000 * sifaddr - Config the interface IP addresses and netmask.
3003 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
3009 memset (&ifr, '\0', sizeof (ifr));
3010 memset (&rt, '\0', sizeof (rt));
3012 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
3013 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
3014 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
3016 strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
3018 * Set our IP address
3020 SIN_ADDR(ifr.ifr_addr) = our_adr;
3021 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
3022 if (errno != EEXIST) {
3023 if (! ok_error (errno))
3024 error("ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
3027 warn("ioctl(SIOCSIFADDR): Address already exists");
3032 * Set the gateway address
3035 SIN_ADDR(ifr.ifr_dstaddr) = his_adr;
3036 if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
3037 if (! ok_error (errno))
3038 error("ioctl(SIOCSIFDSTADDR): %m (line %d)", __LINE__);
3044 * For recent kernels, force the netmask to 255.255.255.255.
3046 if (kernel_version >= KVERSION(2,1,16))
3048 if (net_mask != 0) {
3049 SIN_ADDR(ifr.ifr_netmask) = net_mask;
3050 if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
3051 if (! ok_error (errno))
3052 error("ioctl(SIOCSIFNETMASK): %m (line %d)", __LINE__);
3057 * Add the device route
3059 if (kernel_version < KVERSION(2,1,16)) {
3060 SET_SA_FAMILY (rt.rt_dst, AF_INET);
3061 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
3064 SIN_ADDR(rt.rt_gateway) = 0L;
3065 SIN_ADDR(rt.rt_dst) = his_adr;
3066 rt.rt_flags = RTF_UP | RTF_HOST;
3068 if (kernel_version > KVERSION(2,1,0)) {
3069 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
3070 SIN_ADDR(rt.rt_genmask) = -1L;
3073 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
3074 if (! ok_error (errno))
3075 error("ioctl(SIOCADDRT) device route: %m (line %d)", __LINE__);
3080 /* set ip_dynaddr in demand mode if address changes */
3081 if (demand && tune_kernel && !dynaddr_set
3082 && our_old_addr && our_old_addr != our_adr) {
3083 /* set ip_dynaddr if possible */
3087 path = path_to_procfs("/sys/net/ipv4/ip_dynaddr");
3088 if (path != 0 && (fd = open(path, O_WRONLY)) >= 0) {
3089 if (write(fd, "1", 1) != 1)
3090 error("Couldn't enable dynamic IP addressing: %m");
3093 dynaddr_set = 1; /* only 1 attempt */
3100 /********************************************************************
3102 * cifaddr - Clear the interface IP addresses, and delete routes
3103 * through the interface if possible.
3106 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
3110 if (kernel_version < KVERSION(2,1,16)) {
3112 * Delete the route through the device
3115 memset (&rt, '\0', sizeof (rt));
3117 SET_SA_FAMILY (rt.rt_dst, AF_INET);
3118 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
3121 SIN_ADDR(rt.rt_gateway) = 0;
3122 SIN_ADDR(rt.rt_dst) = his_adr;
3123 rt.rt_flags = RTF_UP | RTF_HOST;
3125 if (kernel_version > KVERSION(2,1,0)) {
3126 SET_SA_FAMILY (rt.rt_genmask, AF_INET);
3127 SIN_ADDR(rt.rt_genmask) = -1L;
3130 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
3131 if (still_ppp() && ! ok_error (errno))
3132 error("ioctl(SIOCDELRT) device route: %m (line %d)", __LINE__);
3137 /* This way it is possible to have an IPv6-only interface */
3138 memset(&ifr, 0, sizeof(ifr));
3139 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
3140 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
3142 if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
3143 if (! ok_error (errno)) {
3144 error("ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
3149 our_old_addr = our_adr;
3154 #ifdef PPP_WITH_IPV6CP
3155 /********************************************************************
3157 * sif6addr_rtnetlink - Config the interface with both IPv6 link-local addresses via rtnetlink
3159 static int sif6addr_rtnetlink(unsigned int iface, eui64_t our_eui64, eui64_t his_eui64)
3162 struct nlmsghdr nlh;
3163 struct ifaddrmsg ifa;
3166 struct in6_addr addr;
3170 struct nlmsghdr nlh;
3171 struct nlmsgerr nlerr;
3173 struct sockaddr_nl nladdr;
3180 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
3182 error("sif6addr_rtnetlink: socket(NETLINK_ROUTE): %m (line %d)", __LINE__);
3187 * Tell kernel to not send to us payload of acknowledgment error message.
3188 * NETLINK_CAP_ACK option is supported since Linux kernel version 4.3 and
3189 * older kernel versions always send full payload in acknowledgment netlink
3190 * message. We ignore payload of this message as we need only error code,
3191 * to check if our set remote peer address request succeeded or failed.
3192 * So ignore return value from the following setsockopt() call as setting
3193 * option NETLINK_CAP_ACK means for us just a kernel hint / optimization.
3196 setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, &one, sizeof(one));
3198 memset(&nladdr, 0, sizeof(nladdr));
3199 nladdr.nl_family = AF_NETLINK;
3201 if (bind(fd, (struct sockaddr *)&nladdr, sizeof(nladdr)) < 0) {
3202 error("sif6addr_rtnetlink: bind(AF_NETLINK): %m (line %d)", __LINE__);
3207 memset(&nlreq, 0, sizeof(nlreq));
3208 nlreq.nlh.nlmsg_len = sizeof(nlreq);
3209 nlreq.nlh.nlmsg_type = RTM_NEWADDR;
3210 nlreq.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE;
3211 nlreq.ifa.ifa_family = AF_INET6;
3212 nlreq.ifa.ifa_prefixlen = 128;
3213 nlreq.ifa.ifa_flags = IFA_F_NODAD | IFA_F_PERMANENT;
3214 nlreq.ifa.ifa_scope = RT_SCOPE_LINK;
3215 nlreq.ifa.ifa_index = iface;
3216 nlreq.addrs[0].rta.rta_len = sizeof(nlreq.addrs[0]);
3217 nlreq.addrs[0].rta.rta_type = IFA_LOCAL;
3218 IN6_LLADDR_FROM_EUI64(nlreq.addrs[0].addr, our_eui64);
3219 nlreq.addrs[1].rta.rta_len = sizeof(nlreq.addrs[1]);
3220 nlreq.addrs[1].rta.rta_type = IFA_ADDRESS;
3223 * To set only local address, older kernel expects that local address is
3224 * in IFA_ADDRESS field (not IFA_LOCAL). New kernels with support for peer
3225 * address, ignore IFA_ADDRESS if is same as IFA_LOCAL. So for backward
3226 * compatibility when setting only local address, set it via both IFA_LOCAL
3227 * and IFA_ADDRESS fields. Same logic is implemented in 'ip address' command
3228 * from iproute2 project.
3230 if (!eui64_iszero(his_eui64))
3231 IN6_LLADDR_FROM_EUI64(nlreq.addrs[1].addr, his_eui64);
3233 IN6_LLADDR_FROM_EUI64(nlreq.addrs[1].addr, our_eui64);
3235 memset(&nladdr, 0, sizeof(nladdr));
3236 nladdr.nl_family = AF_NETLINK;
3238 memset(&iov, 0, sizeof(iov));
3239 iov.iov_base = &nlreq;
3240 iov.iov_len = sizeof(nlreq);
3242 memset(&msg, 0, sizeof(msg));
3243 msg.msg_name = &nladdr;
3244 msg.msg_namelen = sizeof(nladdr);
3248 if (sendmsg(fd, &msg, 0) < 0) {
3249 error("sif6addr_rtnetlink: sendmsg(RTM_NEWADDR/NLM_F_CREATE): %m (line %d)", __LINE__);
3254 memset(&iov, 0, sizeof(iov));
3255 iov.iov_base = &nlresp;
3256 iov.iov_len = sizeof(nlresp);
3258 memset(&msg, 0, sizeof(msg));
3259 msg.msg_name = &nladdr;
3260 msg.msg_namelen = sizeof(nladdr);
3264 nlresplen = recvmsg(fd, &msg, 0);
3266 if (nlresplen < 0) {
3267 error("sif6addr_rtnetlink: recvmsg(NLM_F_ACK): %m (line %d)", __LINE__);
3274 if (nladdr.nl_family != AF_NETLINK) {
3275 error("sif6addr_rtnetlink: recvmsg(NLM_F_ACK): Not a netlink packet (line %d)", __LINE__);
3279 if ((size_t)nlresplen != sizeof(nlresp) || nlresp.nlh.nlmsg_len < sizeof(nlresp)) {
3280 error("sif6addr_rtnetlink: recvmsg(NLM_F_ACK): Acknowledgment netlink packet too short (line %d)", __LINE__);
3284 /* acknowledgment packet for NLM_F_ACK is NLMSG_ERROR */
3285 if (nlresp.nlh.nlmsg_type != NLMSG_ERROR) {
3286 error("sif6addr_rtnetlink: recvmsg(NLM_F_ACK): Not an acknowledgment netlink packet (line %d)", __LINE__);
3290 /* error == 0 indicates success, negative value is errno code */
3291 if (nlresp.nlerr.error != 0) {
3293 * Linux kernel versions prior 3.11 do not support setting IPv6 peer
3294 * addresses and error response is expected. On older kernel versions
3295 * do not show this error message. On error pppd tries to fallback to
3296 * the old IOCTL method.
3298 if (kernel_version >= KVERSION(3,11,0))
3299 error("sif6addr_rtnetlink: %s (line %d)", strerror(-nlresp.nlerr.error), __LINE__);
3306 /********************************************************************
3308 * sif6addr - Config the interface with an IPv6 link-local address
3310 int sif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64)
3312 struct in6_ifreq ifr6;
3314 struct in6_rtmsg rt6;
3319 error("IPv6 socket creation failed: %m");
3322 memset(&ifr, 0, sizeof (ifr));
3323 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
3324 if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) {
3325 error("sif6addr: ioctl(SIOCGIFINDEX): %m (line %d)", __LINE__);
3329 if (kernel_version >= KVERSION(2,1,16)) {
3330 /* Set both local address and remote peer address (with route for it) via rtnetlink */
3331 ret = sif6addr_rtnetlink(ifr.ifr_ifindex, our_eui64, his_eui64);
3337 * Linux kernel versions prior 3.11 do not support setting IPv6 peer address
3338 * via rtnetlink. So if sif6addr_rtnetlink() fails then try old IOCTL method.
3341 /* Local interface */
3342 memset(&ifr6, 0, sizeof(ifr6));
3343 IN6_LLADDR_FROM_EUI64(ifr6.ifr6_addr, our_eui64);
3344 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
3345 ifr6.ifr6_prefixlen = 128;
3347 if (ioctl(sock6_fd, SIOCSIFADDR, &ifr6) < 0) {
3348 error("sif6addr: ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
3353 if (!ret && !eui64_iszero(his_eui64)) {
3355 * Linux kernel does not provide AF_INET6 ioctl SIOCSIFDSTADDR for
3356 * setting remote peer host address, so set only route to remote host.
3359 /* Route to remote host */
3360 memset(&rt6, 0, sizeof(rt6));
3361 IN6_LLADDR_FROM_EUI64(rt6.rtmsg_dst, his_eui64);
3362 rt6.rtmsg_flags = RTF_UP;
3363 rt6.rtmsg_dst_len = 128;
3364 rt6.rtmsg_ifindex = ifr.ifr_ifindex;
3365 rt6.rtmsg_metric = 1;
3367 if (ioctl(sock6_fd, SIOCADDRT, &rt6) < 0) {
3368 error("sif6addr: ioctl(SIOCADDRT): %m (line %d)", __LINE__);
3377 /********************************************************************
3379 * cif6addr - Remove IPv6 address from interface
3381 int cif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64)
3384 struct in6_ifreq ifr6;
3388 error("IPv6 socket creation failed: %m");
3391 memset(&ifr, 0, sizeof(ifr));
3392 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
3393 if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) {
3394 error("cif6addr: ioctl(SIOCGIFINDEX): %m (line %d)", __LINE__);
3398 memset(&ifr6, 0, sizeof(ifr6));
3399 IN6_LLADDR_FROM_EUI64(ifr6.ifr6_addr, our_eui64);
3400 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
3401 ifr6.ifr6_prefixlen = 128;
3403 if (ioctl(sock6_fd, SIOCDIFADDR, &ifr6) < 0) {
3404 if (errno != EADDRNOTAVAIL) {
3405 if (! ok_error (errno))
3406 error("cif6addr: ioctl(SIOCDIFADDR): %m (line %d)", __LINE__);
3409 warn("cif6addr: ioctl(SIOCDIFADDR): No such address");
3415 #endif /* PPP_WITH_IPV6CP */
3418 * get_pty - get a pty master/slave pair and chown the slave side
3419 * to the uid given. Assumes slave_name points to >= 16 bytes of space.
3422 get_pty(int *master_fdp, int *slave_fdp, char *slave_name, int uid)
3424 int i, mfd, ret, sfd = -1;
3426 struct termios tios;
3430 * Try the unix98 way first.
3432 mfd = open("/dev/ptmx", O_RDWR);
3435 if (ioctl(mfd, TIOCGPTN, &ptn) >= 0) {
3436 slprintf(pty_name, sizeof(pty_name), "/dev/pts/%d", ptn);
3437 chmod(pty_name, S_IRUSR | S_IWUSR);
3440 if (ioctl(mfd, TIOCSPTLCK, &ptn) < 0)
3441 warn("Couldn't unlock pty slave %s: %m", pty_name);
3443 if ((sfd = open(pty_name, O_RDWR | O_NOCTTY)) < 0)
3445 warn("Couldn't open pty slave %s: %m", pty_name);
3450 #endif /* TIOCGPTN */
3453 /* the old way - scan through the pty name space */
3454 for (i = 0; i < 64; ++i) {
3455 slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x",
3456 'p' + i / 16, i % 16);
3457 mfd = open(pty_name, O_RDWR, 0);
3460 sfd = open(pty_name, O_RDWR | O_NOCTTY, 0);
3462 ret = fchown(sfd, uid, -1);
3464 warn("Couldn't change ownership of %s, %m", pty_name);
3466 ret = fchmod(sfd, S_IRUSR | S_IWUSR);
3468 warn("Couldn't change permissions of %s, %m", pty_name);
3480 strlcpy(slave_name, pty_name, 16);
3483 if (tcgetattr(sfd, &tios) == 0) {
3484 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
3485 tios.c_cflag |= CS8 | CREAD | CLOCAL;
3486 tios.c_iflag = IGNPAR;
3489 if (tcsetattr(sfd, TCSAFLUSH, &tios) < 0)
3490 warn("couldn't set attributes on pty: %m");
3492 warn("couldn't get attributes on pty: %m");
3497 /********************************************************************
3499 * open_loopback - open the device we use for getting packets
3500 * in demand mode. Under Linux, we use a pty master/slave pair.
3503 open_ppp_loopback(void)
3508 if (new_style_driver) {
3509 /* allocate ourselves a ppp unit */
3510 if (make_ppp_unit() < 0)
3512 modify_flags(ppp_dev_fd, 0, SC_LOOP_TRAFFIC);
3513 set_kdebugflag(kdebugflag);
3518 if (!get_pty(&master_fd, &slave_fd, loop_name, 0))
3519 fatal("No free pty for loopback");
3521 set_ppp_fd(slave_fd);
3523 flags = fcntl(master_fd, F_GETFL);
3525 fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
3526 warn("couldn't set master loopback to nonblock: %m");
3528 flags = fcntl(ppp_fd, F_GETFL);
3530 fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
3531 warn("couldn't set slave loopback to nonblock: %m");
3533 if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
3534 fatal("ioctl(TIOCSETD): %m (line %d)", __LINE__);
3536 * Find out which interface we were given.
3538 if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
3539 fatal("ioctl(PPPIOCGUNIT): %m (line %d)", __LINE__);
3541 * Enable debug in the driver if requested.
3543 set_kdebugflag (kdebugflag);
3548 /********************************************************************
3550 * sifnpmode - Set the mode for handling packets for a given NP.
3554 sifnpmode(int u, int proto, enum NPmode mode)
3558 npi.protocol = proto;
3560 if (ioctl(ppp_dev_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
3561 if (! ok_error (errno))
3562 error("ioctl(PPPIOCSNPMODE, %d, %d): %m", proto, mode);
3569 * Use the hostname as part of the random number seed.
3578 for (p = hostname; *p != 0; ++p)
3583 /********************************************************************
3585 * sys_check_options - check the options that the user specified
3589 sys_check_options(void)
3591 if (demand && driver_is_old) {
3592 option_error("demand dialling is not supported by kernel driver "
3593 "version %d.%d.%d", driver_version, driver_modification,
3597 if (multilink && !new_style_driver) {
3598 warn("Warning: multilink is not supported by the kernel driver");
3604 /********************************************************************
3606 * get_time - Get current time, monotonic if possible.
3609 get_time(struct timeval *tv)
3611 /* Old glibc (< 2.3.4) does define CLOCK_MONOTONIC, but kernel may have it.
3612 * Runtime checking makes it safe. */
3613 #ifndef CLOCK_MONOTONIC
3614 #define CLOCK_MONOTONIC 1
3616 static int monotonic = -1;
3621 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
3625 tv->tv_sec = ts.tv_sec;
3626 tv->tv_usec = ts.tv_nsec / 1000;
3629 } else if (monotonic > 0)
3633 warn("Couldn't use monotonic clock source: %m");
3636 return gettimeofday(tv, NULL);