]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-linux.c
5b1d6c30f96faa5c2097d8fe6e7d3a0a1585b571
[ppp.git] / pppd / sys-linux.c
1 /*
2  * sys-linux.c - System-dependent procedures for setting up
3  * PPP interfaces on Linux systems
4  *
5  * Copyright (c) 1994-2004 Paul Mackerras. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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.
17  *
18  * 3. Redistributions of any form whatsoever must retain the following
19  *    acknowledgment:
20  *    "This product includes software developed by Paul Mackerras
21  *     <paulus@samba.org>".
22  *
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.
30  *
31  * Derived from main.c and pppd.h, which are:
32  *
33  * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  *
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  *
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
45  *    distribution.
46  *
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
53  *      5000 Forbes Avenue
54  *      Pittsburgh, PA  15213-3890
55  *      (412) 268-4387, fax: (412) 268-7395
56  *      tech-transfer@andrew.cmu.edu
57  *
58  * 4. Redistributions of any form whatsoever must retain the following
59  *    acknowledgment:
60  *    "This product includes software developed by Computing Services
61  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
62  *
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.
70  */
71
72 #ifdef HAVE_CONFIG_H
73 #include "config.h"
74 #endif
75
76 #include <sys/ioctl.h>
77 #include <sys/types.h>
78 #include <sys/socket.h>
79 #include <sys/time.h>
80 #include <sys/file.h>
81 #include <sys/stat.h>
82 #include <sys/utsname.h>
83 #include <sys/sysmacros.h>
84 #include <sys/param.h>
85
86 #include <errno.h>
87 #include <stddef.h>
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <syslog.h>
91 #include <string.h>
92 #include <time.h>
93 #include <memory.h>
94 #ifdef HAVE_UTMP_H
95 #include <utmp.h>
96 #endif
97 #include <mntent.h>
98 #include <signal.h>
99 #include <fcntl.h>
100 #include <ctype.h>
101 #include <unistd.h>
102 #include <limits.h>
103
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. */
108
109 #ifndef MAX_ADDR_LEN
110 #define MAX_ADDR_LEN 7
111 #endif
112
113 #if !defined(__GLIBC__) || __GLIBC__ >= 2
114 #include <asm/types.h>          /* glibc 2 conflicts with linux/types.h */
115 #include <net/if.h>
116 #include <net/if_arp.h>
117 #include <net/route.h>
118 #include <netinet/if_ether.h>
119 #else
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>
125 #endif
126 #include <netinet/in.h>
127 #include <arpa/inet.h>
128
129 #include <linux/ppp-ioctl.h>
130
131 #include <linux/netlink.h>
132 #include <linux/rtnetlink.h>
133 #include <linux/if_link.h>
134
135 /* Attempt at retaining compile-support with older than 4.7 kernels, or kernels
136  * where RTM_NEWSTATS isn't defined for whatever reason.
137  */
138 #ifndef RTM_NEWSTATS
139 #define RTM_NEWSTATS 92
140 #define RTM_GETSTATS 94
141 #define IFLA_STATS_LINK_64 1
142 #endif
143
144 #include <linux/if_addr.h>
145
146 /* glibc versions prior to 2.24 do not define SOL_NETLINK */
147 #ifndef SOL_NETLINK
148 #define SOL_NETLINK 270
149 #endif
150
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
154 #endif
155
156 /* linux kernel versions prior to 4.7 do not define/support IFLA_PPP_DEV_FD */
157 #ifndef IFLA_PPP_MAX
158 /* IFLA_PPP_DEV_FD is declared as enum when IFLA_PPP_MAX is defined */
159 #define IFLA_PPP_DEV_FD 1
160 #endif
161
162 #include "pppd.h"
163 #include "fsm.h"
164 #include "ipcp.h"
165
166 #ifdef PPP_WITH_IPV6CP
167 #include "eui64.h"
168 #endif /* PPP_WITH_IPV6CP */
169
170 #ifdef PPP_WITH_FILTER
171 #include <pcap-bpf.h>
172 #include <linux/filter.h>
173 #endif /* PPP_WITH_FILTER */
174
175 #ifdef LOCKLIB
176 #include <sys/locks.h>
177 #endif
178
179 /*
180  * Instead of system header file <termios.h> use local "termios_linux.h" header
181  * file as it provides additional support for arbitrary baud rates via BOTHER.
182  */
183 #include "termios_linux.h"
184
185 #ifdef PPP_WITH_IPV6CP
186 #ifndef _LINUX_IN6_H
187 /*
188  *    This is in linux/include/net/ipv6.h.
189  */
190
191 struct in6_ifreq {
192     struct in6_addr ifr6_addr;
193     __u32 ifr6_prefixlen;
194     unsigned int ifr6_ifindex;
195 };
196 #endif
197
198 #define IN6_LLADDR_FROM_EUI64(sin6, eui64) do {                 \
199         memset(&(sin6).s6_addr, 0, sizeof(struct in6_addr));    \
200         (sin6).s6_addr16[0] = htons(0xfe80);                    \
201         eui64_copy(eui64, (sin6).s6_addr32[2]);                 \
202         } while (0)
203
204 static const eui64_t nulleui64;
205 #endif /* PPP_WITH_IPV6CP */
206
207 /* We can get an EIO error on an ioctl if the modem has hung up */
208 #define ok_error(num) ((num)==EIO)
209
210 static int tty_disc = N_TTY;    /* The TTY discipline */
211 static int ppp_disc = N_PPP;    /* The PPP discpline */
212 static int initfdflags = -1;    /* Initial file descriptor flags for fd */
213 static int ppp_fd = -1;         /* fd which is set to PPP discipline */
214 static int sock_fd = -1;        /* socket for doing interface ioctls */
215 static int slave_fd = -1;       /* pty for old-style demand mode, slave */
216 static int master_fd = -1;      /* pty for old-style demand mode, master */
217 #ifdef PPP_WITH_IPV6CP
218 static int sock6_fd = -1;
219 #endif /* PPP_WITH_IPV6CP */
220
221 /*
222  * For the old-style kernel driver, this is the same as ppp_fd.
223  * For the new-style driver, it is the fd of an instance of /dev/ppp
224  * which is attached to the ppp unit and is used for controlling it.
225  */
226 int ppp_dev_fd = -1;            /* fd for /dev/ppp (new style driver) */
227
228 static int chindex;             /* channel index (new style driver) */
229
230 static fd_set in_fds;           /* set of fds that wait_input waits for */
231 static int max_in_fd;           /* highest fd set in in_fds */
232
233 static int has_proxy_arp       = 0;
234 static int driver_version      = 0;
235 static int driver_modification = 0;
236 static int driver_patch        = 0;
237 static int driver_is_old       = 0;
238 static int restore_term        = 0;     /* 1 => we've munged the terminal */
239 static struct termios inittermios;      /* Initial TTY termios */
240
241 int new_style_driver = 0;
242
243 static char loop_name[20];
244 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
245
246 static int      if_is_up;       /* Interface has been marked up */
247 static int      if6_is_up;      /* Interface has been marked up for IPv6, to help differentiate */
248 static int      have_default_route;     /* Gateway for default route added */
249 static int      have_default_route6;    /* Gateway for default IPv6 route added */
250 static struct   rtentry old_def_rt;     /* Old default route */
251 static int      default_rt_repl_rest;   /* replace and restore old default rt */
252 static u_int32_t proxy_arp_addr;        /* Addr for proxy arp entry added */
253 static char proxy_arp_dev[16];          /* Device for proxy arp entry */
254 static u_int32_t our_old_addr;          /* for detecting address changes */
255 static int      dynaddr_set;            /* 1 if ip_dynaddr set */
256 static int      looped;                 /* 1 if using loop */
257 static int      link_mtu;               /* mtu for the link (not bundle) */
258
259 static struct utsname utsname;  /* for the kernel version */
260 static int kernel_version;
261 #define KVERSION(j,n,p) ((j)*1000000 + (n)*1000 + (p))
262
263 #define MAX_IFS         100
264
265 #define FLAGS_GOOD (IFF_UP          | IFF_BROADCAST)
266 #define FLAGS_MASK (IFF_UP          | IFF_BROADCAST | \
267                     IFF_POINTOPOINT | IFF_LOOPBACK  | IFF_NOARP)
268
269 #define SIN_ADDR(x)     (((struct sockaddr_in *) (&(x)))->sin_addr.s_addr)
270
271 /* Prototypes for procedures local to this file. */
272 static int modify_flags(int fd, int clear_bits, int set_bits);
273 static int translate_speed (int bps);
274 static int baud_rate_of (int speed);
275 static void close_route_table (void);
276 static int open_route_table (void);
277 static int read_route_table (struct rtentry *rt);
278 static int defaultroute_exists (struct rtentry *rt, int metric);
279 static int defaultroute6_exists (struct in6_rtmsg *rt, int metric);
280 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
281                            char *name, int namelen);
282 static void decode_version (char *buf, int *version, int *mod, int *patch);
283 static int set_kdebugflag(int level);
284 static int ppp_registered(void);
285 static int make_ppp_unit(void);
286 static int setifstate (int u, int state);
287
288 extern u_char   inpacket_buf[]; /* borrowed from main.c */
289
290 extern int dfl_route_metric;
291
292 /*
293  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
294  * if it exists.
295  */
296
297 #define SET_SA_FAMILY(addr, family)                     \
298     memset ((char *) &(addr), '\0', sizeof(addr));      \
299     addr.sa_family = (family);
300
301
302 /*
303  * send_rtnetlink_msg - send rtnetlink message and return received error code:
304  * 0              - success
305  * positive value - error during sending / receiving message
306  * negative value - rtnetlink responce error code
307  */
308 static int send_rtnetlink_msg(const char *desc, void *nlreq, size_t nlreq_len)
309 {
310     struct {
311         struct nlmsghdr nlh;
312         struct nlmsgerr nlerr;
313     } nlresp;
314     struct sockaddr_nl nladdr;
315     struct iovec iov;
316     struct msghdr msg;
317     ssize_t nlresp_len;
318     int one;
319     int fd;
320
321     fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
322     if (fd < 0) {
323         error("send_rtnetlink_msg: socket(NETLINK_ROUTE): %m (line %d)", __LINE__);
324         return 1;
325     }
326
327     /*
328      * Tell kernel to not send to us payload of acknowledgment error message.
329      * NETLINK_CAP_ACK option is supported since Linux kernel version 4.3 and
330      * older kernel versions always send full payload in acknowledgment netlink
331      * message. We ignore payload of this message as we need only error code,
332      * to check if our set remote peer address request succeeded or failed.
333      * So ignore return value from the following setsockopt() call as setting
334      * option NETLINK_CAP_ACK means for us just a kernel hint / optimization.
335      */
336     one = 1;
337     setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, &one, sizeof(one));
338
339     memset(&nladdr, 0, sizeof(nladdr));
340     nladdr.nl_family = AF_NETLINK;
341
342     if (bind(fd, (struct sockaddr *)&nladdr, sizeof(nladdr)) < 0) {
343         error("send_rtnetlink_msg: bind(AF_NETLINK): %m (line %d)", __LINE__);
344         close(fd);
345         return 1;
346     }
347
348     memset(&nladdr, 0, sizeof(nladdr));
349     nladdr.nl_family = AF_NETLINK;
350
351     memset(&iov, 0, sizeof(iov));
352     iov.iov_base = nlreq;
353     iov.iov_len = nlreq_len;
354
355     memset(&msg, 0, sizeof(msg));
356     msg.msg_name = &nladdr;
357     msg.msg_namelen = sizeof(nladdr);
358     msg.msg_iov = &iov;
359     msg.msg_iovlen = 1;
360
361     if (sendmsg(fd, &msg, 0) < 0) {
362         error("send_rtnetlink_msg: sendmsg(%s): %m (line %d)", desc, __LINE__);
363         close(fd);
364         return 1;
365     }
366
367     memset(&iov, 0, sizeof(iov));
368     iov.iov_base = &nlresp;
369     iov.iov_len = sizeof(nlresp);
370
371     memset(&msg, 0, sizeof(msg));
372     msg.msg_name = &nladdr;
373     msg.msg_namelen = sizeof(nladdr);
374     msg.msg_iov = &iov;
375     msg.msg_iovlen = 1;
376
377     nlresp_len = recvmsg(fd, &msg, 0);
378
379     if (nlresp_len < 0) {
380         error("send_rtnetlink_msg: recvmsg(%s): %m (line %d)", desc, __LINE__);
381         close(fd);
382         return 1;
383     }
384
385     close(fd);
386
387     if (nladdr.nl_family != AF_NETLINK) {
388         error("send_rtnetlink_msg: recvmsg(%s): Not a netlink packet (line %d)", desc, __LINE__);
389         return 1;
390     }
391
392     if ((size_t)nlresp_len < sizeof(nlresp) || nlresp.nlh.nlmsg_len < sizeof(nlresp)) {
393         error("send_rtnetlink_msg: recvmsg(%s): Acknowledgment netlink packet too short (line %d)", desc, __LINE__);
394         return 1;
395     }
396
397     /* acknowledgment packet for NLM_F_ACK is NLMSG_ERROR */
398     if (nlresp.nlh.nlmsg_type != NLMSG_ERROR) {
399         error("send_rtnetlink_msg: recvmsg(%s): Not an acknowledgment netlink packet (line %d)", desc, __LINE__);
400         return 1;
401     }
402
403     /* error == 0 indicates success, negative value is errno code */
404     return nlresp.nlerr.error;
405 }
406
407 /*
408  * Determine if the PPP connection should still be present.
409  */
410
411 extern int hungup;
412
413 /* new_fd is the fd of a tty */
414 static void set_ppp_fd (int new_fd)
415 {
416         ppp_fd = new_fd;
417         if (!new_style_driver)
418                 ppp_dev_fd = new_fd;
419 }
420
421 static int still_ppp(void)
422 {
423         if (new_style_driver)
424                 return !hungup && ppp_fd >= 0;
425         if (!hungup || ppp_fd == slave_fd)
426                 return 1;
427         if (slave_fd >= 0) {
428                 set_ppp_fd(slave_fd);
429                 return 1;
430         }
431         return 0;
432 }
433
434 /*
435  * modify_flags - set and clear flag bits controlling the kernel
436  * PPP driver.
437  */
438 static int modify_flags(int fd, int clear_bits, int set_bits)
439 {
440         int flags;
441
442         if (ioctl(fd, PPPIOCGFLAGS, &flags) == -1)
443                 goto err;
444         flags = (flags & ~clear_bits) | set_bits;
445         if (ioctl(fd, PPPIOCSFLAGS, &flags) == -1)
446                 goto err;
447
448         return 0;
449
450  err:
451         if (errno != EIO)
452                 error("Failed to set PPP kernel option flags: %m");
453         return -1;
454 }
455
456 /********************************************************************
457  *
458  * sys_init - System-dependent initialization.
459  */
460
461 void sys_init(void)
462 {
463     /* Get an internet socket for doing socket ioctls. */
464     sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
465     if (sock_fd < 0)
466         fatal("Couldn't create IP socket: %m(%d)", errno);
467
468 #ifdef PPP_WITH_IPV6CP
469     sock6_fd = socket(AF_INET6, SOCK_DGRAM, 0);
470     if (sock6_fd < 0)
471         sock6_fd = -errno;      /* save errno for later */
472 #endif
473
474     FD_ZERO(&in_fds);
475     max_in_fd = 0;
476 }
477
478 /********************************************************************
479  *
480  * sys_cleanup - restore any system state we modified before exiting:
481  * mark the interface down, delete default route and/or proxy arp entry.
482  * This shouldn't call die() because it's called from die().
483  */
484
485 void sys_cleanup(void)
486 {
487 /*
488  * Take down the device
489  */
490     if (if_is_up) {
491         if_is_up = 0;
492         sifdown(0);
493     }
494 #ifdef PPP_WITH_IPV6CP
495     if (if6_is_up)
496         sif6down(0);
497 #endif
498
499 /*
500  * Delete any routes through the device.
501  */
502     if (have_default_route)
503         cifdefaultroute(0, 0, 0);
504 #ifdef PPP_WITH_IPV6CP
505     if (have_default_route6)
506         cif6defaultroute(0, nulleui64, nulleui64);
507 #endif
508
509     if (has_proxy_arp)
510         cifproxyarp(0, proxy_arp_addr);
511 }
512
513 /********************************************************************
514  *
515  * sys_close - Clean up in a child process before execing.
516  */
517 void
518 sys_close(void)
519 {
520     if (new_style_driver && ppp_dev_fd >= 0)
521         close(ppp_dev_fd);
522     if (sock_fd >= 0)
523         close(sock_fd);
524 #ifdef PPP_WITH_IPV6CP
525     if (sock6_fd >= 0)
526         close(sock6_fd);
527 #endif
528     if (slave_fd >= 0)
529         close(slave_fd);
530     if (master_fd >= 0)
531         close(master_fd);
532 }
533
534 /********************************************************************
535  *
536  * set_kdebugflag - Define the debugging level for the kernel
537  */
538
539 static int set_kdebugflag (int requested_level)
540 {
541     if (ppp_dev_fd < 0)
542         return 1;
543     if (ioctl(ppp_dev_fd, PPPIOCSDEBUG, &requested_level) < 0) {
544         if ( ! ok_error (errno) )
545             error("ioctl(PPPIOCSDEBUG): %m (line %d)", __LINE__);
546         return (0);
547     }
548     return (1);
549 }
550
551 /********************************************************************
552  *
553  * tty_establish_ppp - Turn the serial port into a ppp interface.
554  */
555
556 int tty_establish_ppp (int tty_fd)
557 {
558     int ret_fd;
559
560 /*
561  * Ensure that the tty device is in exclusive mode.
562  */
563     if (ioctl(tty_fd, TIOCEXCL, 0) < 0) {
564         if ( ! ok_error ( errno ))
565             warn("Couldn't make tty exclusive: %m");
566     }
567 /*
568  * Demand mode - prime the old ppp device to relinquish the unit.
569  */
570     if (!new_style_driver && looped
571         && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0) {
572         error("ioctl(transfer ppp unit): %m, line %d", __LINE__);
573         return -1;
574     }
575 /*
576  * Set the current tty to the PPP discpline
577  */
578
579 #ifndef N_SYNC_PPP
580 #define N_SYNC_PPP 14
581 #endif
582     ppp_disc = (new_style_driver && sync_serial)? N_SYNC_PPP: N_PPP;
583     if (ioctl(tty_fd, TIOCSETD, &ppp_disc) < 0) {
584         if ( ! ok_error (errno) ) {
585             error("Couldn't set tty to PPP discipline: %m");
586             return -1;
587         }
588     }
589
590     ret_fd = generic_establish_ppp(tty_fd);
591
592 #define SC_RCVB (SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP)
593 #define SC_LOGB (SC_DEBUG | SC_LOG_INPKT | SC_LOG_OUTPKT | SC_LOG_RAWIN \
594                  | SC_LOG_FLUSH)
595
596     if (ret_fd >= 0) {
597         modify_flags(ppp_fd, SC_RCVB | SC_LOGB,
598                      (kdebugflag * SC_DEBUG) & SC_LOGB);
599     } else {
600         if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0 && !ok_error(errno))
601             warn("Couldn't reset tty to normal line discipline: %m");
602     }
603
604     return ret_fd;
605 }
606
607 /********************************************************************
608  *
609  * generic_establish_ppp - Turn the fd into a ppp interface.
610  */
611 int generic_establish_ppp (int fd)
612 {
613     int x;
614
615     if (new_style_driver) {
616         int flags;
617
618         /* If a ppp_fd is already open, close it first */
619         if (ppp_fd >= 0) {
620             close(ppp_fd);
621             remove_fd(ppp_fd);
622             ppp_fd = -1;
623         }
624
625         /* Open an instance of /dev/ppp and connect the channel to it */
626         if (ioctl(fd, PPPIOCGCHAN, &chindex) == -1) {
627             error("Couldn't get channel number: %m");
628             goto err;
629         }
630         dbglog("using channel %d", chindex);
631         fd = open("/dev/ppp", O_RDWR);
632         if (fd < 0) {
633             error("Couldn't reopen /dev/ppp: %m");
634             goto err;
635         }
636         (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
637         if (ioctl(fd, PPPIOCATTCHAN, &chindex) < 0) {
638             error("Couldn't attach to channel %d: %m", chindex);
639             goto err_close;
640         }
641         flags = fcntl(fd, F_GETFL);
642         if (flags == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
643             warn("Couldn't set /dev/ppp (channel) to nonblock: %m");
644         set_ppp_fd(fd);
645
646         if (!looped)
647             ifunit = -1;
648         if (!looped && !multilink) {
649             /*
650              * Create a new PPP unit.
651              */
652             if (make_ppp_unit() < 0)
653                 goto err_close;
654         }
655
656         if (looped)
657             modify_flags(ppp_dev_fd, SC_LOOP_TRAFFIC, 0);
658
659         if (!multilink) {
660             add_fd(ppp_dev_fd);
661             if (ioctl(fd, PPPIOCCONNECT, &ifunit) < 0) {
662                 error("Couldn't attach to PPP unit %d: %m", ifunit);
663                 goto err_close;
664             }
665         }
666
667     } else {
668         /*
669          * Old-style driver: find out which interface we were given.
670          */
671         set_ppp_fd (fd);
672         if (ioctl(fd, PPPIOCGUNIT, &x) < 0) {
673             if (ok_error (errno))
674                 goto err;
675             fatal("ioctl(PPPIOCGUNIT): %m (line %d)", __LINE__);
676         }
677         /* Check that we got the same unit again. */
678         if (looped && x != ifunit)
679             fatal("transfer_ppp failed: wanted unit %d, got %d", ifunit, x);
680         ifunit = x;
681
682         /*
683          * Fetch the initial file flags and reset blocking mode on the file.
684          */
685         initfdflags = fcntl(fd, F_GETFL);
686         if (initfdflags == -1 ||
687             fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
688             if ( ! ok_error (errno))
689                 warn("Couldn't set device to non-blocking mode: %m");
690         }
691     }
692
693     /*
694      * Enable debug in the driver if requested.
695      */
696     if (!looped)
697         set_kdebugflag (kdebugflag);
698
699     looped = 0;
700
701     return ppp_fd;
702
703  err_close:
704     close(fd);
705  err:
706     return -1;
707 }
708
709 /********************************************************************
710  *
711  * tty_disestablish_ppp - Restore the serial port to normal operation.
712  * This shouldn't call die() because it's called from die().
713  */
714
715 void tty_disestablish_ppp(int tty_fd)
716 {
717     if (!hungup) {
718 /*
719  * Flush the tty output buffer so that the TIOCSETD doesn't hang.
720  */
721         if (tcflush(tty_fd, TCIOFLUSH) < 0)
722         {
723             warn("tcflush failed: %m");
724             goto flushfailed;
725         }
726 /*
727  * Restore the previous line discipline
728  */
729         if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0) {
730             if ( ! ok_error (errno))
731                 error("ioctl(TIOCSETD, N_TTY): %m (line %d)", __LINE__);
732         }
733
734         if (ioctl(tty_fd, TIOCNXCL, 0) < 0) {
735             if ( ! ok_error (errno))
736                 warn("ioctl(TIOCNXCL): %m (line %d)", __LINE__);
737         }
738
739         /* Reset non-blocking mode on fd. */
740         if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0) {
741             if ( ! ok_error (errno))
742                 warn("Couldn't restore device fd flags: %m");
743         }
744     }
745 flushfailed:
746     initfdflags = -1;
747
748     generic_disestablish_ppp(tty_fd);
749 }
750
751 /********************************************************************
752  *
753  * generic_disestablish_ppp - Restore device components to normal
754  * operation, and reconnect the ppp unit to the loopback if in demand
755  * mode.  This shouldn't call die() because it's called from die().
756  */
757 void generic_disestablish_ppp(int dev_fd)
758 {
759     if (new_style_driver) {
760         close(ppp_fd);
761         ppp_fd = -1;
762         if (demand) {
763             modify_flags(ppp_dev_fd, 0, SC_LOOP_TRAFFIC);
764             looped = 1;
765         } else if (!doing_multilink && ppp_dev_fd >= 0) {
766             close(ppp_dev_fd);
767             remove_fd(ppp_dev_fd);
768             ppp_dev_fd = -1;
769         }
770     } else {
771         /* old-style driver */
772         if (demand)
773             set_ppp_fd(slave_fd);
774         else
775             ppp_dev_fd = -1;
776     }
777 }
778
779 /*
780  * make_ppp_unit_rtnetlink - register a new ppp network interface for ppp_dev_fd
781  * with specified req_ifname via rtnetlink. Interface name req_ifname must not
782  * be empty. Custom ppp unit id req_unit is ignored and kernel choose some free.
783  */
784 static int make_ppp_unit_rtnetlink(void)
785 {
786     struct {
787         struct nlmsghdr nlh;
788         struct ifinfomsg ifm;
789         struct {
790             struct rtattr rta;
791             char ifname[IFNAMSIZ];
792         } ifn;
793         struct {
794             struct rtattr rta;
795             struct {
796                 struct rtattr rta;
797                 char ifkind[sizeof("ppp")];
798             } ifik;
799             struct {
800                 struct rtattr rta;
801                 struct {
802                     struct rtattr rta;
803                     union {
804                         int ppp_dev_fd;
805                     } ppp;
806                 } ifdata[1];
807             } ifid;
808         } ifli;
809     } nlreq;
810     int resp;
811
812     memset(&nlreq, 0, sizeof(nlreq));
813     nlreq.nlh.nlmsg_len = sizeof(nlreq);
814     nlreq.nlh.nlmsg_type = RTM_NEWLINK;
815     nlreq.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE;
816     nlreq.ifm.ifi_family = AF_UNSPEC;
817     nlreq.ifm.ifi_type = ARPHRD_NETROM;
818     nlreq.ifn.rta.rta_len = sizeof(nlreq.ifn);
819     nlreq.ifn.rta.rta_type = IFLA_IFNAME;
820     strlcpy(nlreq.ifn.ifname, req_ifname, sizeof(nlreq.ifn.ifname));
821     nlreq.ifli.rta.rta_len = sizeof(nlreq.ifli);
822     nlreq.ifli.rta.rta_type = IFLA_LINKINFO;
823     nlreq.ifli.ifik.rta.rta_len = sizeof(nlreq.ifli.ifik);
824     nlreq.ifli.ifik.rta.rta_type = IFLA_INFO_KIND;
825     strcpy(nlreq.ifli.ifik.ifkind, "ppp");
826     nlreq.ifli.ifid.rta.rta_len = sizeof(nlreq.ifli.ifid);
827     nlreq.ifli.ifid.rta.rta_type = IFLA_INFO_DATA;
828     nlreq.ifli.ifid.ifdata[0].rta.rta_len = sizeof(nlreq.ifli.ifid.ifdata[0]);
829     nlreq.ifli.ifid.ifdata[0].rta.rta_type = IFLA_PPP_DEV_FD;
830     nlreq.ifli.ifid.ifdata[0].ppp.ppp_dev_fd = ppp_dev_fd;
831
832     resp = send_rtnetlink_msg("RTM_NEWLINK/NLM_F_CREATE", &nlreq, sizeof(nlreq));
833     if (resp) {
834         /*
835          * Linux kernel versions prior to 4.7 do not support creating ppp
836          * interfaces via rtnetlink API and therefore error response is
837          * expected. On older kernel versions do not show this error message.
838          * When error is different than EEXIST then pppd tries to fallback to
839          * the old ioctl method.
840          */
841         errno = (resp < 0) ? -resp : EINVAL;
842         if (kernel_version >= KVERSION(4,7,0))
843             error("Couldn't create ppp interface %s: %m", req_ifname);
844         return 0;
845     }
846
847     return 1;
848 }
849
850 /*
851  * make_ppp_unit - make a new ppp unit for ppp_dev_fd.
852  * Assumes new_style_driver.
853  */
854 static int make_ppp_unit(void)
855 {
856         int x, flags;
857
858         if (ppp_dev_fd >= 0) {
859                 dbglog("in make_ppp_unit, already had /dev/ppp open?");
860                 close(ppp_dev_fd);
861         }
862         ppp_dev_fd = open("/dev/ppp", O_RDWR);
863         if (ppp_dev_fd < 0)
864                 fatal("Couldn't open /dev/ppp: %m");
865         flags = fcntl(ppp_dev_fd, F_GETFL);
866         if (flags == -1
867             || fcntl(ppp_dev_fd, F_SETFL, flags | O_NONBLOCK) == -1)
868                 warn("Couldn't set /dev/ppp to nonblock: %m");
869
870         /*
871          * Via rtnetlink it is possible to create ppp network interface with
872          * custom ifname atomically. But it is not possible to specify custom
873          * ppp unit id.
874          *
875          * Tools like systemd, udev or NetworkManager are trying to query
876          * interface attributes based on interface name immediately when new
877          * network interface is created. And therefore immediate interface
878          * renaming is causing issues.
879          *
880          * So use rtnetlink API only when user requested custom ifname. It will
881          * avoid system issues with interface renaming.
882          */
883         if (req_unit == -1 && req_ifname[0] != '\0' && kernel_version >= KVERSION(2,1,16)) {
884             if (make_ppp_unit_rtnetlink()) {
885                 if (ioctl(ppp_dev_fd, PPPIOCGUNIT, &ifunit))
886                     fatal("Couldn't retrieve PPP unit id: %m");
887                 return 0;
888             }
889             /*
890              * If interface with requested name already exist return error
891              * otherwise fallback to old ioctl method.
892              */
893             if (errno == EEXIST)
894                 return -1;
895         }
896
897         ifunit = req_unit;
898         x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit);
899         if (x < 0 && req_unit >= 0 && errno == EEXIST) {
900                 warn("Couldn't allocate PPP unit %d as it is already in use", req_unit);
901                 ifunit = -1;
902                 x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit);
903         }
904         if (x < 0 && errno == EEXIST) {
905                 srand(time(NULL) * getpid());
906                 ifunit = rand() % 10000;
907                 x = ioctl(ppp_dev_fd, PPPIOCNEWUNIT, &ifunit);
908         }
909         if (x < 0)
910                 error("Couldn't create new ppp unit: %m");
911
912         if (x == 0 && req_ifname[0] != '\0') {
913                 struct ifreq ifr;
914                 char t[IFNAMSIZ];
915                 memset(&ifr, 0, sizeof(struct ifreq));
916                 slprintf(t, sizeof(t), "%s%d", PPP_DRV_NAME, ifunit);
917                 strlcpy(ifr.ifr_name, t, IFNAMSIZ);
918                 strlcpy(ifr.ifr_newname, req_ifname, IFNAMSIZ);
919                 x = ioctl(sock_fd, SIOCSIFNAME, &ifr);
920                 if (x < 0)
921                     error("Couldn't rename interface %s to %s: %m", t, req_ifname);
922                 else
923                     info("Renamed interface %s to %s", t, req_ifname);
924         }
925
926         return x;
927 }
928
929 /*
930  * cfg_bundle - configure the existing bundle.
931  * Used in demand mode.
932  */
933 void cfg_bundle(int mrru, int mtru, int rssn, int tssn)
934 {
935         if (!new_style_driver)
936                 return;
937
938         /* set the mrru, mtu and flags */
939         if (ioctl(ppp_dev_fd, PPPIOCSMRRU, &mrru) < 0)
940                 error("Couldn't set MRRU: %m");
941
942         modify_flags(ppp_dev_fd, SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ|SC_MULTILINK,
943                      ((rssn? SC_MP_SHORTSEQ: 0) | (tssn? SC_MP_XSHORTSEQ: 0)
944                       | (mrru? SC_MULTILINK: 0)));
945
946         /* connect up the channel */
947         if (ioctl(ppp_fd, PPPIOCCONNECT, &ifunit) < 0)
948                 fatal("Couldn't attach to PPP unit %d: %m", ifunit);
949         add_fd(ppp_dev_fd);
950 }
951
952 /*
953  * make_new_bundle - create a new PPP unit (i.e. a bundle)
954  * and connect our channel to it.  This should only get called
955  * if `multilink' was set at the time establish_ppp was called.
956  * In demand mode this uses our existing bundle instead of making
957  * a new one.
958  */
959 void make_new_bundle(int mrru, int mtru, int rssn, int tssn)
960 {
961         if (!new_style_driver)
962                 return;
963
964         /* make us a ppp unit */
965         if (make_ppp_unit() < 0)
966                 die(1);
967
968         /* set the mrru and flags */
969         cfg_bundle(mrru, mtru, rssn, tssn);
970 }
971
972 /*
973  * bundle_attach - attach our link to a given PPP unit.
974  * We assume the unit is controlled by another pppd.
975  */
976 int bundle_attach(int ifnum)
977 {
978         int master_fd;
979
980         if (!new_style_driver)
981                 return -1;
982
983         master_fd = open("/dev/ppp", O_RDWR);
984         if (master_fd < 0)
985                 fatal("Couldn't open /dev/ppp: %m");
986         if (ioctl(master_fd, PPPIOCATTACH, &ifnum) < 0) {
987                 if (errno == ENXIO) {
988                         close(master_fd);
989                         return 0;       /* doesn't still exist */
990                 }
991                 fatal("Couldn't attach to interface unit %d: %m\n", ifnum);
992         }
993         if (ioctl(ppp_fd, PPPIOCCONNECT, &ifnum) < 0)
994                 fatal("Couldn't connect to interface unit %d: %m", ifnum);
995         modify_flags(master_fd, 0, SC_MULTILINK);
996         close(master_fd);
997
998         ifunit = ifnum;
999         return 1;
1000 }
1001
1002 /*
1003  * destroy_bundle - tell the driver to destroy our bundle.
1004  */
1005 void destroy_bundle(void)
1006 {
1007         if (ppp_dev_fd >= 0) {
1008                 close(ppp_dev_fd);
1009                 remove_fd(ppp_dev_fd);
1010                 ppp_dev_fd = -1;
1011         }
1012 }
1013
1014 /********************************************************************
1015  *
1016  * clean_check - Fetch the flags for the device and generate
1017  * appropriate error messages.
1018  */
1019 void clean_check(void)
1020 {
1021     int x;
1022     char *s;
1023
1024     if (still_ppp()) {
1025         if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
1026             s = NULL;
1027             switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
1028             case SC_RCV_B7_0:
1029                 s = "all had bit 7 set to 1";
1030                 break;
1031
1032             case SC_RCV_B7_1:
1033                 s = "all had bit 7 set to 0";
1034                 break;
1035
1036             case SC_RCV_EVNP:
1037                 s = "all had odd parity";
1038                 break;
1039
1040             case SC_RCV_ODDP:
1041                 s = "all had even parity";
1042                 break;
1043             }
1044
1045             if (s != NULL) {
1046                 warn("Receive serial link is not 8-bit clean:");
1047                 warn("Problem: %s", s);
1048             }
1049         }
1050     }
1051 }
1052
1053
1054 /*
1055  * List of valid speeds.
1056  */
1057
1058 struct speed {
1059     int speed_int, speed_val;
1060 } speeds[] = {
1061 #ifdef B50
1062     { 50, B50 },
1063 #endif
1064 #ifdef B75
1065     { 75, B75 },
1066 #endif
1067 #ifdef B110
1068     { 110, B110 },
1069 #endif
1070 #ifdef B134
1071     { 134, B134 },
1072 #endif
1073 #ifdef B150
1074     { 150, B150 },
1075 #endif
1076 #ifdef B200
1077     { 200, B200 },
1078 #endif
1079 #ifdef B300
1080     { 300, B300 },
1081 #endif
1082 #ifdef B600
1083     { 600, B600 },
1084 #endif
1085 #ifdef B1200
1086     { 1200, B1200 },
1087 #endif
1088 #ifdef B1800
1089     { 1800, B1800 },
1090 #endif
1091 #ifdef B2000
1092     { 2000, B2000 },
1093 #endif
1094 #ifdef B2400
1095     { 2400, B2400 },
1096 #endif
1097 #ifdef B3600
1098     { 3600, B3600 },
1099 #endif
1100 #ifdef B4800
1101     { 4800, B4800 },
1102 #endif
1103 #ifdef B7200
1104     { 7200, B7200 },
1105 #endif
1106 #ifdef B9600
1107     { 9600, B9600 },
1108 #endif
1109 #ifdef B19200
1110     { 19200, B19200 },
1111 #endif
1112 #ifdef B38400
1113     { 38400, B38400 },
1114 #endif
1115 #ifdef B57600
1116     { 57600, B57600 },
1117 #endif
1118 #ifdef B76800
1119     { 76800, B76800 },
1120 #endif
1121 #ifdef B115200
1122     { 115200, B115200 },
1123 #endif
1124 #ifdef B153600
1125     { 153600, B153600 },
1126 #endif
1127 #ifdef EXTA
1128     { 19200, EXTA },
1129 #endif
1130 #ifdef EXTB
1131     { 38400, EXTB },
1132 #endif
1133 #ifdef B230400
1134     { 230400, B230400 },
1135 #endif
1136 #ifdef B307200
1137     { 307200, B307200 },
1138 #endif
1139 #ifdef B460800
1140     { 460800, B460800 },
1141 #endif
1142 #ifdef B500000
1143     { 500000, B500000 },
1144 #endif
1145 #ifdef B576000
1146     { 576000, B576000 },
1147 #endif
1148 #ifdef B614400
1149     { 614400, B614400 },
1150 #endif
1151 #ifdef B921600
1152     { 921600, B921600 },
1153 #endif
1154 #ifdef B1000000
1155     { 1000000, B1000000 },
1156 #endif
1157 #ifdef B1152000
1158     { 1152000, B1152000 },
1159 #endif
1160 #ifdef B1500000
1161     { 1500000, B1500000 },
1162 #endif
1163 #ifdef B2000000
1164     { 2000000, B2000000 },
1165 #endif
1166 #ifdef B2500000
1167     { 2500000, B2500000 },
1168 #endif
1169 #ifdef B3000000
1170     { 3000000, B3000000 },
1171 #endif
1172 #ifdef B3500000
1173     { 3500000, B3500000 },
1174 #endif
1175 #ifdef B4000000
1176     { 4000000, B4000000 },
1177 #endif
1178     { 0, 0 }
1179 };
1180
1181 /********************************************************************
1182  *
1183  * Translate from bits/second to a speed_t.
1184  */
1185
1186 static int translate_speed (int bps)
1187 {
1188     struct speed *speedp;
1189
1190     if (bps != 0) {
1191         for (speedp = speeds; speedp->speed_int; speedp++) {
1192             if (bps == speedp->speed_int)
1193                 return speedp->speed_val;
1194         }
1195     }
1196     return 0;
1197 }
1198
1199 /********************************************************************
1200  *
1201  * Translate from a speed_t to bits/second.
1202  */
1203
1204 static int baud_rate_of (int speed)
1205 {
1206     struct speed *speedp;
1207
1208     if (speed != 0) {
1209         for (speedp = speeds; speedp->speed_int; speedp++) {
1210             if (speed == speedp->speed_val)
1211                 return speedp->speed_int;
1212         }
1213     }
1214     return 0;
1215 }
1216
1217 /********************************************************************
1218  *
1219  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
1220  * at the requested speed, etc.  If `local' is true, set CLOCAL
1221  * regardless of whether the modem option was specified.
1222  */
1223
1224 void set_up_tty(int tty_fd, int local)
1225 {
1226     int speed;
1227     struct termios tios;
1228
1229     setdtr(tty_fd, 1);
1230     if (tcgetattr(tty_fd, &tios) < 0) {
1231         if (!ok_error(errno))
1232             fatal("tcgetattr: %m (line %d)", __LINE__);
1233         return;
1234     }
1235
1236     if (!restore_term)
1237         inittermios = tios;
1238
1239     tios.c_cflag     &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
1240     tios.c_cflag     |= CS8 | CREAD | HUPCL;
1241
1242     tios.c_iflag      = IGNBRK | IGNPAR;
1243     tios.c_oflag      = 0;
1244     tios.c_lflag      = 0;
1245     tios.c_cc[VMIN]   = 1;
1246     tios.c_cc[VTIME]  = 0;
1247
1248     if (local || !modem)
1249         tios.c_cflag ^= (CLOCAL | HUPCL);
1250
1251     switch (crtscts) {
1252     case 1:
1253         tios.c_cflag |= CRTSCTS;
1254         break;
1255
1256     case -2:
1257         tios.c_iflag     |= IXON | IXOFF;
1258         tios.c_cc[VSTOP]  = 0x13;       /* DC3 = XOFF = ^S */
1259         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
1260         break;
1261
1262     case -1:
1263         tios.c_cflag &= ~CRTSCTS;
1264         break;
1265
1266     default:
1267         break;
1268     }
1269
1270     if (stop_bits >= 2)
1271         tios.c_cflag |= CSTOPB;
1272
1273     if (inspeed) {
1274         speed = translate_speed(inspeed);
1275         if (speed) {
1276             cfsetospeed (&tios, speed);
1277             cfsetispeed (&tios, speed);
1278             speed = cfgetospeed(&tios);
1279             baud_rate = baud_rate_of(speed);
1280         } else {
1281 #ifdef BOTHER
1282             tios.c_cflag &= ~CBAUD;
1283             tios.c_cflag |= BOTHER;
1284             tios.c_ospeed = inspeed;
1285 #ifdef IBSHIFT
1286             /* B0 sets input baudrate to the output baudrate */
1287             tios.c_cflag &= ~(CBAUD << IBSHIFT);
1288             tios.c_cflag |= B0 << IBSHIFT;
1289             tios.c_ispeed = inspeed;
1290 #endif
1291             baud_rate = inspeed;
1292 #else
1293             baud_rate = 0;
1294 #endif
1295         }
1296     }
1297     else {
1298         speed = cfgetospeed(&tios);
1299         baud_rate = baud_rate_of(speed);
1300 #ifdef BOTHER
1301         if (!baud_rate)
1302             baud_rate = tios.c_ospeed;
1303 #endif
1304     }
1305
1306 /*
1307  * We can't proceed if the serial port baud rate is unknown,
1308  * since that implies that the serial port is disabled.
1309  */
1310     if (!baud_rate) {
1311         if (inspeed)
1312             fatal("speed %d not supported", inspeed);
1313         else
1314             fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
1315     }
1316
1317     while (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0 && !ok_error(errno))
1318         if (errno != EINTR)
1319             fatal("tcsetattr: %m (line %d)", __LINE__);
1320     restore_term = 1;
1321 }
1322
1323 /********************************************************************
1324  *
1325  * setdtr - control the DTR line on the serial port.
1326  * This is called from die(), so it shouldn't call die().
1327  */
1328
1329 void setdtr (int tty_fd, int on)
1330 {
1331     int modembits = TIOCM_DTR;
1332
1333     ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
1334 }
1335
1336 /********************************************************************
1337  *
1338  * restore_tty - restore the terminal to the saved settings.
1339  */
1340
1341 void restore_tty (int tty_fd)
1342 {
1343     if (restore_term) {
1344         restore_term = 0;
1345 /*
1346  * Turn off echoing, because otherwise we can get into
1347  * a loop with the tty and the modem echoing to each other.
1348  * We presume we are the sole user of this tty device, so
1349  * when we close it, it will revert to its defaults anyway.
1350  */
1351         if (!default_device)
1352             inittermios.c_lflag &= ~(ECHO | ECHONL);
1353
1354         if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0) {
1355             if (! ok_error (errno))
1356                 warn("tcsetattr: %m (line %d)", __LINE__);
1357         }
1358     }
1359 }
1360
1361 /********************************************************************
1362  *
1363  * output - Output PPP packet.
1364  */
1365
1366 void output (int unit, unsigned char *p, int len)
1367 {
1368     int fd = ppp_fd;
1369     int proto;
1370
1371     dump_packet("sent", p, len);
1372     if (snoop_send_hook) snoop_send_hook(p, len);
1373
1374     if (len < PPP_HDRLEN)
1375         return;
1376     if (new_style_driver) {
1377         p += 2;
1378         len -= 2;
1379         proto = (p[0] << 8) + p[1];
1380         if (ppp_dev_fd >= 0 && !(proto >= 0xc000 || proto == PPP_CCPFRAG))
1381             fd = ppp_dev_fd;
1382     }
1383     if (write(fd, p, len) < 0) {
1384         if (errno == EWOULDBLOCK || errno == EAGAIN || errno == ENOBUFS
1385             || errno == ENXIO || errno == EIO || errno == EINTR)
1386             warn("write: warning: %m (%d)", errno);
1387         else
1388             error("write: %m (%d)", errno);
1389     }
1390 }
1391
1392 /********************************************************************
1393  *
1394  * wait_input - wait until there is data available,
1395  * for the length of time specified by *timo (indefinite
1396  * if timo is NULL).
1397  */
1398
1399 void wait_input(struct timeval *timo)
1400 {
1401     fd_set ready, exc;
1402     int n;
1403
1404     ready = in_fds;
1405     exc = in_fds;
1406     n = select(max_in_fd + 1, &ready, NULL, &exc, timo);
1407     if (n < 0 && errno != EINTR)
1408         fatal("select: %m");
1409 }
1410
1411 /*
1412  * add_fd - add an fd to the set that wait_input waits for.
1413  */
1414 void add_fd(int fd)
1415 {
1416     if (fd >= FD_SETSIZE)
1417         fatal("internal error: file descriptor too large (%d)", fd);
1418     FD_SET(fd, &in_fds);
1419     if (fd > max_in_fd)
1420         max_in_fd = fd;
1421 }
1422
1423 /*
1424  * remove_fd - remove an fd from the set that wait_input waits for.
1425  */
1426 void remove_fd(int fd)
1427 {
1428     FD_CLR(fd, &in_fds);
1429 }
1430
1431
1432 /********************************************************************
1433  *
1434  * read_packet - get a PPP packet from the serial device.
1435  */
1436
1437 int read_packet (unsigned char *buf)
1438 {
1439     int len, nr;
1440
1441     len = PPP_MRU + PPP_HDRLEN;
1442     if (new_style_driver) {
1443         *buf++ = PPP_ALLSTATIONS;
1444         *buf++ = PPP_UI;
1445         len -= 2;
1446     }
1447     nr = -1;
1448     if (ppp_fd >= 0) {
1449         nr = read(ppp_fd, buf, len);
1450         if (nr < 0 && errno != EWOULDBLOCK && errno != EAGAIN
1451             && errno != EIO && errno != EINTR)
1452             error("read: %m");
1453         if (nr < 0 && errno == ENXIO)
1454             return 0;
1455     }
1456     if (nr < 0 && new_style_driver && ppp_dev_fd >= 0 && !bundle_eof) {
1457         /* N.B. we read ppp_fd first since LCP packets come in there. */
1458         nr = read(ppp_dev_fd, buf, len);
1459         if (nr < 0 && errno != EWOULDBLOCK && errno != EAGAIN
1460             && errno != EIO && errno != EINTR)
1461             error("read /dev/ppp: %m");
1462         if (nr < 0 && errno == ENXIO)
1463             nr = 0;
1464         if (nr == 0 && doing_multilink) {
1465             remove_fd(ppp_dev_fd);
1466             bundle_eof = 1;
1467         }
1468     }
1469     if (new_style_driver && ppp_fd < 0 && ppp_dev_fd < 0)
1470         nr = 0;
1471     return (new_style_driver && nr > 0)? nr+2: nr;
1472 }
1473
1474 /********************************************************************
1475  *
1476  * get_loop_output - get outgoing packets from the ppp device,
1477  * and detect when we want to bring the real link up.
1478  * Return value is 1 if we need to bring up the link, 0 otherwise.
1479  */
1480 int
1481 get_loop_output(void)
1482 {
1483     int rv = 0;
1484     int n;
1485
1486     if (new_style_driver) {
1487         while ((n = read_packet(inpacket_buf)) > 0)
1488             if (loop_frame(inpacket_buf, n))
1489                 rv = 1;
1490         return rv;
1491     }
1492
1493     while ((n = read(master_fd, inbuf, sizeof(inbuf))) > 0)
1494         if (loop_chars(inbuf, n))
1495             rv = 1;
1496
1497     if (n == 0)
1498         fatal("eof on loopback");
1499
1500     if (errno != EWOULDBLOCK && errno != EAGAIN)
1501         fatal("read from loopback: %m(%d)", errno);
1502
1503     return rv;
1504 }
1505
1506 /*
1507  * netif_set_mtu - set the MTU on the PPP network interface.
1508  */
1509 void
1510 netif_set_mtu(int unit, int mtu)
1511 {
1512     struct ifreq ifr;
1513
1514     memset (&ifr, '\0', sizeof (ifr));
1515     strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1516     ifr.ifr_mtu = mtu;
1517
1518     if (ifunit >= 0 && ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
1519         error("ioctl(SIOCSIFMTU): %m (line %d)", __LINE__);
1520 }
1521
1522 /*
1523  * netif_get_mtu - get the MTU on the PPP network interface.
1524  */
1525 int
1526 netif_get_mtu(int unit)
1527 {
1528     struct ifreq ifr;
1529
1530     memset (&ifr, '\0', sizeof (ifr));
1531     strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1532
1533     if (ifunit >= 0 && ioctl(sock_fd, SIOCGIFMTU, (caddr_t) &ifr) < 0) {
1534         error("ioctl(SIOCGIFMTU): %m (line %d)", __LINE__);
1535         return 0;
1536     }
1537     return ifr.ifr_mtu;
1538 }
1539
1540 /********************************************************************
1541  *
1542  * tty_send_config - configure the transmit characteristics of
1543  * the ppp interface.
1544  */
1545
1546 void tty_send_config(int mtu, u_int32_t asyncmap, int pcomp, int accomp)
1547 {
1548         int x;
1549
1550         if (!still_ppp())
1551                 return;
1552         link_mtu = mtu;
1553         if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
1554                 if (errno != EIO && errno != ENOTTY)
1555                         error("Couldn't set transmit async character map: %m");
1556                 ++error_count;
1557                 return;
1558         }
1559
1560         x = (pcomp? SC_COMP_PROT: 0) | (accomp? SC_COMP_AC: 0)
1561             | (sync_serial? SC_SYNC: 0);
1562         modify_flags(ppp_fd, SC_COMP_PROT|SC_COMP_AC|SC_SYNC, x);
1563 }
1564
1565 /********************************************************************
1566  *
1567  * tty_set_xaccm - set the extended transmit ACCM for the interface.
1568  */
1569
1570 void tty_set_xaccm (ext_accm accm)
1571 {
1572     if (!still_ppp())
1573         return;
1574     if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY) {
1575         if ( ! ok_error (errno))
1576             warn("ioctl(set extended ACCM): %m (line %d)", __LINE__);
1577     }
1578 }
1579
1580 /********************************************************************
1581  *
1582  * tty_recv_config - configure the receive-side characteristics of
1583  * the ppp interface.
1584  */
1585
1586 void tty_recv_config(int mru, u_int32_t asyncmap, int pcomp, int accomp)
1587 {
1588 /*
1589  * If we were called because the link has gone down then there is nothing
1590  * which may be done. Just return without incident.
1591  */
1592         if (!still_ppp())
1593                 return;
1594 /*
1595  * Set the receiver parameters
1596  */
1597         if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
1598                 if (errno != EIO && errno != ENOTTY)
1599                         error("Couldn't set channel receive MRU: %m");
1600         }
1601         if (new_style_driver && ppp_dev_fd >= 0
1602             && ioctl(ppp_dev_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
1603                 error("Couldn't set MRU in generic PPP layer: %m");
1604
1605         if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
1606                 if (errno != EIO && errno != ENOTTY)
1607                         error("Couldn't set channel receive asyncmap: %m");
1608         }
1609 }
1610
1611 /********************************************************************
1612  *
1613  * ccp_test - ask kernel whether a given compression method
1614  * is acceptable for use.
1615  */
1616
1617 int
1618 ccp_test(int unit, u_char *opt_ptr, int opt_len, int for_transmit)
1619 {
1620     struct ppp_option_data data;
1621
1622     memset (&data, '\0', sizeof (data));
1623     data.ptr      = opt_ptr;
1624     data.length   = opt_len;
1625     data.transmit = for_transmit;
1626
1627     if (ioctl(ppp_dev_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
1628         return 1;
1629
1630     return (errno == ENOBUFS)? 0: -1;
1631 }
1632
1633 /********************************************************************
1634  *
1635  * ccp_flags_set - inform kernel about the current state of CCP.
1636  */
1637
1638 void ccp_flags_set (int unit, int isopen, int isup)
1639 {
1640         int x;
1641
1642         x = (isopen? SC_CCP_OPEN: 0) | (isup? SC_CCP_UP: 0);
1643         if (still_ppp() && ppp_dev_fd >= 0)
1644                 modify_flags(ppp_dev_fd, SC_CCP_OPEN|SC_CCP_UP, x);
1645 }
1646
1647 #ifdef PPP_WITH_FILTER
1648 /*
1649  * set_filters - set the active and pass filters in the kernel driver.
1650  */
1651 int set_filters(struct bpf_program *pass, struct bpf_program *active)
1652 {
1653         struct sock_fprog fp;
1654
1655         fp.len = pass->bf_len;
1656         fp.filter = (struct sock_filter *) pass->bf_insns;
1657         if (ioctl(ppp_dev_fd, PPPIOCSPASS, &fp) < 0) {
1658                 if (errno == ENOTTY)
1659                         warn("kernel does not support PPP filtering");
1660                 else
1661                         error("Couldn't set pass-filter in kernel: %m");
1662                 return 0;
1663         }
1664         fp.len = active->bf_len;
1665         fp.filter = (struct sock_filter *) active->bf_insns;
1666         if (ioctl(ppp_dev_fd, PPPIOCSACTIVE, &fp) < 0) {
1667                 error("Couldn't set active-filter in kernel: %m");
1668                 return 0;
1669         }
1670         return 1;
1671 }
1672 #endif /* PPP_WITH_FILTER */
1673
1674 /********************************************************************
1675  *
1676  * get_idle_time - return how long the link has been idle.
1677  */
1678 int
1679 get_idle_time(int u, struct ppp_idle *ip)
1680 {
1681     return ioctl(ppp_dev_fd, PPPIOCGIDLE, ip) >= 0;
1682 }
1683
1684 /********************************************************************
1685  *
1686  * get_ppp_stats_iocl - return statistics for the link, using the ioctl() method,
1687  * this only supports 32-bit counters, so need to count the wraps.
1688  */
1689 static int
1690 get_ppp_stats_ioctl(int u, struct pppd_stats *stats)
1691 {
1692     static u_int32_t previbytes = 0;
1693     static u_int32_t prevobytes = 0;
1694     static u_int32_t iwraps = 0;
1695     static u_int32_t owraps = 0;
1696
1697     struct ifreq req;
1698     struct ppp_stats data;
1699
1700     memset (&req, 0, sizeof (req));
1701
1702     req.ifr_data = (caddr_t) &data;
1703     strlcpy(req.ifr_name, ifname, sizeof(req.ifr_name));
1704     if (ioctl(sock_fd, SIOCGPPPSTATS, &req) < 0) {
1705         error("Couldn't get PPP statistics: %m");
1706         return 0;
1707     }
1708     stats->bytes_in = data.p.ppp_ibytes;
1709     stats->bytes_out = data.p.ppp_obytes;
1710     stats->pkts_in = data.p.ppp_ipackets;
1711     stats->pkts_out = data.p.ppp_opackets;
1712
1713     if (stats->bytes_in < previbytes)
1714         ++iwraps;
1715     if (stats->bytes_out < prevobytes)
1716         ++owraps;
1717
1718     previbytes = stats->bytes_in;
1719     prevobytes = stats->bytes_out;
1720
1721     stats->bytes_in += (uint64_t)iwraps << 32;
1722     stats->bytes_out += (uint64_t)owraps << 32;
1723
1724     return 1;
1725 }
1726
1727 /********************************************************************
1728  * get_ppp_stats_rtnetlink - return statistics for the link, using rtnetlink
1729  * This provides native 64-bit counters.
1730  */
1731 static int
1732 get_ppp_stats_rtnetlink(int u, struct pppd_stats *stats)
1733 {
1734     static int rtnl_fd = -1;
1735
1736     struct sockaddr_nl nladdr;
1737     struct {
1738         struct nlmsghdr nlh;
1739         struct if_stats_msg ifsm;
1740     } nlreq;
1741     struct nlresp {
1742         struct nlmsghdr nlh;
1743         union {
1744             struct {
1745                 struct nlmsgerr nlerr;
1746                 char __end_err[0];
1747             };
1748             struct {
1749                 struct rtmsg rth;
1750                 struct  {
1751                     /* We only case about these first fields from rtnl_link_stats64 */
1752                     uint64_t rx_packets;
1753                     uint64_t tx_packets;
1754                     uint64_t rx_bytes;
1755                     uint64_t tx_bytes;
1756                 } stats;
1757                 char __end_stats[0];
1758             };
1759         };
1760     } nlresp;
1761     ssize_t nlresplen;
1762     struct iovec iov;
1763     struct msghdr msg;
1764
1765     memset(&nladdr, 0, sizeof(nladdr));
1766     nladdr.nl_family = AF_NETLINK;
1767
1768     if (rtnl_fd < 0) {
1769         rtnl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1770         if (rtnl_fd < 0) {
1771             error("get_ppp_stats_rtnetlink: error creating NETLINK socket: %m (line %d)", __LINE__);
1772             return 0;
1773         }
1774
1775         if (bind(rtnl_fd, (struct sockaddr *)&nladdr, sizeof(nladdr)) < 0) {
1776             error("get_ppp_stats_rtnetlink: bind(AF_NETLINK): %m (line %d)", __LINE__);
1777             goto err;
1778         }
1779     }
1780
1781     memset(&nlreq, 0, sizeof(nlreq));
1782     nlreq.nlh.nlmsg_len = sizeof(nlreq);
1783     nlreq.nlh.nlmsg_type = RTM_GETSTATS;
1784     nlreq.nlh.nlmsg_flags = NLM_F_REQUEST;
1785
1786     nlreq.ifsm.ifindex = if_nametoindex(ifname);
1787     nlreq.ifsm.filter_mask = IFLA_STATS_LINK_64;
1788
1789     memset(&iov, 0, sizeof(iov));
1790     iov.iov_base = &nlreq;
1791     iov.iov_len = sizeof(nlreq);
1792
1793     memset(&msg, 0, sizeof(msg));
1794     msg.msg_name = &nladdr;
1795     msg.msg_namelen = sizeof(nladdr);
1796     msg.msg_iov = &iov;
1797     msg.msg_iovlen = 1;
1798
1799     if (sendmsg(rtnl_fd, &msg, 0) < 0) {
1800         error("get_ppp_stats_rtnetlink: sendmsg(RTM_GETSTATS): %m (line %d)", __LINE__);
1801         goto err;
1802     }
1803
1804     /* We just need to repoint to IOV ... everything else stays the same */
1805     iov.iov_base = &nlresp;
1806     iov.iov_len = sizeof(nlresp);
1807
1808     nlresplen = recvmsg(rtnl_fd, &msg, 0);
1809
1810     if (nlresplen < 0) {
1811         error("get_ppp_stats_rtnetlink: recvmsg(RTM_GETSTATS): %m (line %d)", __LINE__);
1812         goto err;
1813     }
1814
1815     if (nlresplen < sizeof(nlresp.nlh)) {
1816         error("get_ppp_stats_rtnetlink: Netlink response message was incomplete (line %d)", __LINE__);
1817         goto err;
1818     }
1819
1820     if (nlresp.nlh.nlmsg_type == NLMSG_ERROR) {
1821         if (nlresplen < offsetof(struct nlresp, __end_err))
1822             error("get_ppp_stats_rtnetlink: Netlink responded with an error message, but the nlmsgerr structure is incomplete (line %d).", __LINE__);
1823         else if (kernel_version >= KVERSION(4,7,0))
1824             error("get_ppp_stats_rtnetlink: Netlink responded with error: %s (line %d)", strerror(-nlresp.nlerr.error), __LINE__);
1825         goto err;
1826     }
1827
1828     if (nlresp.nlh.nlmsg_type != RTM_NEWSTATS) {
1829         error("get_ppp_stats_rtnetlink: Expected RTM_NEWSTATS response, found something else (mlmsg_type %d, line %d)",
1830                 nlresp.nlh.nlmsg_type, __LINE__);
1831         goto err;
1832     }
1833
1834     if (nlresplen < offsetof(struct nlresp, __end_stats)) {
1835         error("get_ppp_stats_rtnetlink: Obtained an insufficiently sized rtnl_link_stats64 struct from the kernel (line %d).", __LINE__);
1836         goto err;
1837     }
1838
1839     stats->bytes_in  = nlresp.stats.rx_bytes;
1840     stats->bytes_out = nlresp.stats.tx_bytes;
1841     stats->pkts_in   = nlresp.stats.rx_packets;
1842     stats->pkts_out  = nlresp.stats.tx_packets;
1843
1844     return 1;
1845 err:
1846     close(rtnl_fd);
1847     rtnl_fd = -1;
1848     return 0;
1849 }
1850
1851 /********************************************************************
1852  * get_ppp_stats_sysfs - return statistics for the link, using the files in sysfs,
1853  * this provides native 64-bit counters.
1854  */
1855 static int
1856 get_ppp_stats_sysfs(int u, struct pppd_stats *stats)
1857 {
1858     char fname[PATH_MAX+1];
1859     char buf[21], *err; /* 2^64 < 10^20 */
1860     int blen, fd, rlen;
1861     unsigned long long val;
1862
1863     struct {
1864         const char* fname;
1865         void* ptr;
1866         unsigned size;
1867     } slist[] = {
1868 #define statfield(fn, field)    { .fname = #fn, .ptr = &stats->field, .size = sizeof(stats->field) }
1869         statfield(rx_bytes, bytes_in),
1870         statfield(tx_bytes, bytes_out),
1871         statfield(rx_packets, pkts_in),
1872         statfield(tx_packets, pkts_out),
1873 #undef statfield
1874     };
1875
1876     blen = snprintf(fname, sizeof(fname), "/sys/class/net/%s/statistics/", ifname);
1877     if (blen >= sizeof(fname))
1878         return 0; /* ifname max 15, so this should be impossible */
1879
1880     for (int i = 0; i < sizeof(slist) / sizeof(*slist); ++i) {
1881         if (snprintf(fname + blen, sizeof(fname) - blen, "%s", slist[i].fname) >= sizeof(fname) - blen) {
1882             fname[blen] = 0;
1883             error("sysfs stats: filename %s/%s overflowed PATH_MAX", fname, slist[i].fname);
1884             return 0;
1885         }
1886
1887         fd = open(fname, O_RDONLY);
1888         if (fd < 0) {
1889             error("%s: %m", fname);
1890             return 0;
1891         }
1892
1893         rlen = read(fd, buf, sizeof(buf) - 1);
1894         close(fd);
1895         if (rlen < 0) {
1896             error("%s: %m", fname);
1897             return 0;
1898         }
1899         /* trim trailing \n if present */
1900         while (rlen > 0 && buf[rlen-1] == '\n')
1901             rlen--;
1902         buf[rlen] = 0;
1903
1904         errno = 0;
1905         val = strtoull(buf, &err, 10);
1906         if (*buf < '0' || *buf > '9' || errno != 0 || *err) {
1907             error("string to number conversion error converting %s (from %s) for remaining string %s%s%s",
1908                     buf, fname, err, errno ? ": " : "", errno ? strerror(errno) : "");
1909             return 0;
1910         }
1911         switch (slist[i].size) {
1912 #define stattype(type)  case sizeof(type): *(type*)slist[i].ptr = (type)val; break
1913             stattype(uint64_t);
1914             stattype(uint32_t);
1915             stattype(uint16_t);
1916             stattype(uint8_t);
1917 #undef stattype
1918         default:
1919             error("Don't know how to store stats for %s of size %u", slist[i].fname, slist[i].size);
1920             return 0;
1921         }
1922     }
1923
1924     return 1;
1925 }
1926
1927 /********************************************************************
1928  * Periodic timer function to be used to keep stats up to date in case of ioctl
1929  * polling.
1930  *
1931  * Given the 25s interval this should be fine up to data rates of 1.37Gbps.
1932  * If you do change the timer, remember to also bring the get_ppp_stats (which
1933  * sets up the initial trigger) as well.
1934  */
1935 static void
1936 ppp_stats_poller(void* u)
1937 {
1938     struct pppd_stats dummy;
1939     get_ppp_stats_ioctl((long)u, &dummy);
1940     TIMEOUT(ppp_stats_poller, u, 25);
1941 }
1942
1943 /********************************************************************
1944  * get_ppp_stats - return statistics for the link.
1945  */
1946 int get_ppp_stats(int u, struct pppd_stats *stats)
1947 {
1948     static int (*func)(int, struct pppd_stats*) = NULL;
1949
1950     if (!func) {
1951         if (get_ppp_stats_rtnetlink(u, stats)) {
1952             func = get_ppp_stats_rtnetlink;
1953             return 1;
1954         }
1955         if (get_ppp_stats_sysfs(u, stats)) {
1956             func = get_ppp_stats_sysfs;
1957             return 1;
1958         }
1959         warn("statistics falling back to ioctl which only supports 32-bit counters");
1960         func = get_ppp_stats_ioctl;
1961         TIMEOUT(ppp_stats_poller, (void*)(long)u, 25);
1962     }
1963
1964     return func(u, stats);
1965 }
1966
1967 /********************************************************************
1968  *
1969  * ccp_fatal_error - returns 1 if decompression was disabled as a
1970  * result of an error detected after decompression of a packet,
1971  * 0 otherwise.  This is necessary because of patent nonsense.
1972  */
1973
1974 int ccp_fatal_error (int unit)
1975 {
1976         int flags;
1977
1978         if (ioctl(ppp_dev_fd, PPPIOCGFLAGS, &flags) < 0) {
1979                 error("Couldn't read compression error flags: %m");
1980                 flags = 0;
1981         }
1982         return flags & SC_DC_FERROR;
1983 }
1984
1985 /********************************************************************
1986  *
1987  * path_to_procfs - find the path to the proc file system mount point
1988  */
1989 static char proc_path[MAXPATHLEN];
1990 static int proc_path_len;
1991
1992 static char *path_to_procfs(const char *tail)
1993 {
1994     struct mntent *mntent;
1995     FILE *fp;
1996
1997     if (proc_path_len == 0) {
1998         /* Default the mount location of /proc */
1999         strlcpy (proc_path, "/proc", sizeof(proc_path));
2000         proc_path_len = 5;
2001         fp = fopen(MOUNTED, "r");
2002         if (fp != NULL) {
2003             while ((mntent = getmntent(fp)) != NULL) {
2004                 if (strcmp(mntent->mnt_type, MNTTYPE_IGNORE) == 0)
2005                     continue;
2006                 if (strcmp(mntent->mnt_type, "proc") == 0) {
2007                     strlcpy(proc_path, mntent->mnt_dir, sizeof(proc_path));
2008                     proc_path_len = strlen(proc_path);
2009                     break;
2010                 }
2011             }
2012             fclose (fp);
2013         }
2014     }
2015
2016     strlcpy(proc_path + proc_path_len, tail,
2017             sizeof(proc_path) - proc_path_len);
2018     return proc_path;
2019 }
2020
2021 /*
2022  * /proc/net/route parsing stuff.
2023  */
2024 #define ROUTE_MAX_COLS  12
2025 FILE *route_fd = (FILE *) 0;
2026 static char route_buffer[512];
2027 static int route_dev_col, route_dest_col, route_gw_col;
2028 static int route_flags_col, route_metric_col, route_mask_col;
2029 static int route_num_cols;
2030
2031 static int open_route_table (void);
2032 static void close_route_table (void);
2033 static int read_route_table (struct rtentry *rt);
2034
2035 /********************************************************************
2036  *
2037  * close_route_table - close the interface to the route table
2038  */
2039
2040 static void close_route_table (void)
2041 {
2042     if (route_fd != (FILE *) 0) {
2043         fclose (route_fd);
2044         route_fd = (FILE *) 0;
2045     }
2046 }
2047
2048 /********************************************************************
2049  *
2050  * open_route_table - open the interface to the route table
2051  */
2052 static char route_delims[] = " \t\n";
2053
2054 static int open_route_table (void)
2055 {
2056     char *path;
2057
2058     close_route_table();
2059
2060     path = path_to_procfs("/net/route");
2061     route_fd = fopen (path, "r");
2062     if (route_fd == NULL) {
2063         error("can't open routing table %s: %m", path);
2064         return 0;
2065     }
2066
2067     route_dev_col = 0;          /* default to usual columns */
2068     route_dest_col = 1;
2069     route_gw_col = 2;
2070     route_flags_col = 3;
2071     route_metric_col = 6;
2072     route_mask_col = 7;
2073     route_num_cols = 8;
2074
2075     /* parse header line */
2076     if (fgets(route_buffer, sizeof(route_buffer), route_fd) != 0) {
2077         char *p = route_buffer, *q;
2078         int col;
2079         for (col = 0; col < ROUTE_MAX_COLS; ++col) {
2080             int used = 1;
2081             if ((q = strtok(p, route_delims)) == 0)
2082                 break;
2083             if (strcasecmp(q, "iface") == 0)
2084                 route_dev_col = col;
2085             else if (strcasecmp(q, "destination") == 0)
2086                 route_dest_col = col;
2087             else if (strcasecmp(q, "gateway") == 0)
2088                 route_gw_col = col;
2089             else if (strcasecmp(q, "flags") == 0)
2090                 route_flags_col = col;
2091             else if (strcasecmp(q, "mask") == 0)
2092                 route_mask_col = col;
2093             else
2094                 used = 0;
2095             if (used && col >= route_num_cols)
2096                 route_num_cols = col + 1;
2097             p = NULL;
2098         }
2099     }
2100
2101     return 1;
2102 }
2103
2104 /********************************************************************
2105  *
2106  * read_route_table - read the next entry from the route table
2107  */
2108
2109 static int read_route_table(struct rtentry *rt)
2110 {
2111     char *cols[ROUTE_MAX_COLS], *p;
2112     int col;
2113
2114     memset (rt, '\0', sizeof (struct rtentry));
2115
2116     if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
2117         return 0;
2118
2119     p = route_buffer;
2120     for (col = 0; col < route_num_cols; ++col) {
2121         cols[col] = strtok(p, route_delims);
2122         if (cols[col] == NULL)
2123             return 0;           /* didn't get enough columns */
2124         p = NULL;
2125     }
2126
2127     SET_SA_FAMILY (rt->rt_dst,     AF_INET);
2128     SET_SA_FAMILY (rt->rt_gateway, AF_INET);
2129
2130     SIN_ADDR(rt->rt_dst) = strtoul(cols[route_dest_col], NULL, 16);
2131     SIN_ADDR(rt->rt_gateway) = strtoul(cols[route_gw_col], NULL, 16);
2132     SIN_ADDR(rt->rt_genmask) = strtoul(cols[route_mask_col], NULL, 16);
2133
2134     rt->rt_flags = (short) strtoul(cols[route_flags_col], NULL, 16);
2135     rt->rt_metric = (short) strtoul(cols[route_metric_col], NULL, 10);
2136     rt->rt_dev   = cols[route_dev_col];
2137
2138     return 1;
2139 }
2140
2141 /********************************************************************
2142  *
2143  * defaultroute_exists - determine if there is a default route
2144  * with the given metric (or negative for any)
2145  */
2146
2147 static int defaultroute_exists (struct rtentry *rt, int metric)
2148 {
2149     int result = 0;
2150
2151     if (!open_route_table())
2152         return 0;
2153
2154     while (read_route_table(rt) != 0) {
2155         if ((rt->rt_flags & RTF_UP) == 0)
2156             continue;
2157
2158         if (kernel_version > KVERSION(2,1,0) && SIN_ADDR(rt->rt_genmask) != 0)
2159             continue;
2160         if (SIN_ADDR(rt->rt_dst) == 0L && (metric < 0
2161                                            || rt->rt_metric == metric)) {
2162             result = 1;
2163             break;
2164         }
2165     }
2166
2167     close_route_table();
2168     return result;
2169 }
2170
2171 /*
2172  * have_route_to - determine if the system has any route to
2173  * a given IP address.  `addr' is in network byte order.
2174  * Return value is 1 if yes, 0 if no, -1 if don't know.
2175  * For demand mode to work properly, we have to ignore routes
2176  * through our own interface.
2177  */
2178 int have_route_to(u_int32_t addr)
2179 {
2180     struct rtentry rt;
2181     int result = 0;
2182
2183     if (!open_route_table())
2184         return -1;              /* don't know */
2185
2186     while (read_route_table(&rt)) {
2187         if ((rt.rt_flags & RTF_UP) == 0 || strcmp(rt.rt_dev, ifname) == 0)
2188             continue;
2189         if ((addr & SIN_ADDR(rt.rt_genmask)) == SIN_ADDR(rt.rt_dst)) {
2190             result = 1;
2191             break;
2192         }
2193     }
2194
2195     close_route_table();
2196     return result;
2197 }
2198
2199 /********************************************************************
2200  *
2201  * sifdefaultroute - assign a default route through the address given.
2202  *
2203  * If the global default_rt_repl_rest flag is set, then this function
2204  * already replaced the original system defaultroute with some other
2205  * route and it should just replace the current defaultroute with
2206  * another one, without saving the current route. Use: demand mode,
2207  * when pppd sets first a defaultroute it it's temporary ppp0 addresses
2208  * and then changes the temporary addresses to the addresses for the real
2209  * ppp connection when it has come up.
2210  */
2211
2212 int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway, bool replace)
2213 {
2214     struct rtentry rt, tmp_rt;
2215     struct rtentry *del_rt = NULL;
2216
2217     if (default_rt_repl_rest) {
2218         /* We have already replaced the original defaultroute, if we
2219          * are called again, we will delete the current default route
2220          * and set the new default route in this function.
2221          * - this is normally only the case the doing demand: */
2222         if (defaultroute_exists(&tmp_rt, -1))
2223             del_rt = &tmp_rt;
2224     } else if (defaultroute_exists(&old_def_rt, -1           ) &&
2225                             strcmp( old_def_rt.rt_dev, ifname) != 0) {
2226         /*
2227          * We did not yet replace an existing default route, let's
2228          * check if we should save and replace a default route:
2229          */
2230         u_int32_t old_gateway = SIN_ADDR(old_def_rt.rt_gateway);
2231
2232         if (old_gateway != gateway) {
2233             if (!replace) {
2234                 error("not replacing default route to %s [%I]",
2235                         old_def_rt.rt_dev, old_gateway);
2236                 return 0;
2237             } else {
2238                 /* we need to copy rt_dev because we need it permanent too: */
2239                 char * tmp_dev = malloc(strlen(old_def_rt.rt_dev)+1);
2240                 strcpy(tmp_dev, old_def_rt.rt_dev);
2241                 old_def_rt.rt_dev = tmp_dev;
2242
2243                 notice("replacing old default route to %s [%I]",
2244                         old_def_rt.rt_dev, old_gateway);
2245                 default_rt_repl_rest = 1;
2246                 del_rt = &old_def_rt;
2247             }
2248         }
2249     }
2250
2251     memset (&rt, 0, sizeof (rt));
2252     SET_SA_FAMILY (rt.rt_dst, AF_INET);
2253
2254     rt.rt_dev = ifname;
2255     rt.rt_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
2256
2257     if (kernel_version > KVERSION(2,1,0)) {
2258         SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2259         SIN_ADDR(rt.rt_genmask) = 0L;
2260     }
2261
2262     rt.rt_flags = RTF_UP;
2263     if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
2264         if ( ! ok_error ( errno ))
2265             error("default route ioctl(SIOCADDRT): %m");
2266         return 0;
2267     }
2268     if (default_rt_repl_rest && del_rt)
2269         if (ioctl(sock_fd, SIOCDELRT, del_rt) < 0) {
2270             if ( ! ok_error ( errno ))
2271                 error("del old default route ioctl(SIOCDELRT): %m(%d)", errno);
2272             return 0;
2273         }
2274
2275     have_default_route = 1;
2276     return 1;
2277 }
2278
2279 /********************************************************************
2280  *
2281  * cifdefaultroute - delete a default route through the address given.
2282  */
2283
2284 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
2285 {
2286     struct rtentry rt;
2287
2288     have_default_route = 0;
2289
2290     memset (&rt, '\0', sizeof (rt));
2291     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
2292     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2293
2294     rt.rt_dev = ifname;
2295
2296     rt.rt_dev = ifname;
2297     rt.rt_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
2298
2299     if (kernel_version > KVERSION(2,1,0)) {
2300         SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2301         SIN_ADDR(rt.rt_genmask) = 0L;
2302     }
2303
2304     rt.rt_flags = RTF_UP;
2305     if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
2306         if (still_ppp()) {
2307             if ( ! ok_error ( errno ))
2308                 error("default route ioctl(SIOCDELRT): %m");
2309             return 0;
2310         }
2311     }
2312     if (default_rt_repl_rest) {
2313         notice("restoring old default route to %s [%I]",
2314                         old_def_rt.rt_dev, SIN_ADDR(old_def_rt.rt_gateway));
2315         if (ioctl(sock_fd, SIOCADDRT, &old_def_rt) < 0) {
2316             if ( ! ok_error ( errno ))
2317                 error("restore default route ioctl(SIOCADDRT): %m(%d)", errno);
2318             return 0;
2319         }
2320         default_rt_repl_rest = 0;
2321     }
2322
2323     return 1;
2324 }
2325
2326 #ifdef PPP_WITH_IPV6CP
2327 /*
2328  * /proc/net/ipv6_route parsing stuff.
2329  */
2330 static int route_dest_plen_col;
2331 static int open_route6_table (void);
2332 static int read_route6_table (struct in6_rtmsg *rt);
2333
2334 /********************************************************************
2335  *
2336  * open_route6_table - open the interface to the route table
2337  */
2338 static int open_route6_table (void)
2339 {
2340     char *path;
2341
2342     close_route_table();
2343
2344     path = path_to_procfs("/net/ipv6_route");
2345     route_fd = fopen (path, "r");
2346     if (route_fd == NULL) {
2347         error("can't open routing table %s: %m", path);
2348         return 0;
2349     }
2350
2351     /* default to usual columns */
2352     route_dest_col = 0;
2353     route_dest_plen_col = 1;
2354     route_gw_col = 4;
2355     route_metric_col = 5;
2356     route_flags_col = 8;
2357     route_dev_col = 9;
2358     route_num_cols = 10;
2359
2360     return 1;
2361 }
2362
2363 /********************************************************************
2364  *
2365  * read_route6_table - read the next entry from the route table
2366  */
2367
2368 static void hex_to_in6_addr(struct in6_addr *addr, const char *s)
2369 {
2370     char hex8[9];
2371     unsigned i;
2372     uint32_t v;
2373
2374     hex8[8] = 0;
2375     for (i = 0; i < 4; i++) {
2376         memcpy(hex8, s + 8*i, 8);
2377         v = strtoul(hex8, NULL, 16);
2378         addr->s6_addr32[i] = v;
2379     }
2380 }
2381
2382 static int read_route6_table(struct in6_rtmsg *rt)
2383 {
2384     char *cols[ROUTE_MAX_COLS], *p;
2385     int col;
2386
2387     memset (rt, '\0', sizeof (struct in6_rtmsg));
2388
2389     if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
2390         return 0;
2391
2392     p = route_buffer;
2393     for (col = 0; col < route_num_cols; ++col) {
2394         cols[col] = strtok(p, route_delims);
2395         if (cols[col] == NULL)
2396             return 0;           /* didn't get enough columns */
2397         p = NULL;
2398     }
2399
2400     hex_to_in6_addr(&rt->rtmsg_dst, cols[route_dest_col]);
2401     rt->rtmsg_dst_len = strtoul(cols[route_dest_plen_col], NULL, 16);
2402     hex_to_in6_addr(&rt->rtmsg_gateway, cols[route_gw_col]);
2403
2404     rt->rtmsg_metric = strtoul(cols[route_metric_col], NULL, 16);
2405     rt->rtmsg_flags = strtoul(cols[route_flags_col], NULL, 16);
2406     rt->rtmsg_ifindex = if_nametoindex(cols[route_dev_col]);
2407
2408     return 1;
2409 }
2410
2411 /********************************************************************
2412  *
2413  * defaultroute6_exists - determine if there is a default route
2414  */
2415
2416 static int defaultroute6_exists (struct in6_rtmsg *rt, int metric)
2417 {
2418     int result = 0;
2419
2420     if (!open_route6_table())
2421         return 0;
2422
2423     while (read_route6_table(rt) != 0) {
2424         if ((rt->rtmsg_flags & RTF_UP) == 0)
2425             continue;
2426
2427         if (rt->rtmsg_dst_len != 0)
2428             continue;
2429         if (rt->rtmsg_dst.s6_addr32[0] == 0L
2430          && rt->rtmsg_dst.s6_addr32[1] == 0L
2431          && rt->rtmsg_dst.s6_addr32[2] == 0L
2432          && rt->rtmsg_dst.s6_addr32[3] == 0L
2433          && (metric < 0 || rt->rtmsg_metric == metric)) {
2434             result = 1;
2435             break;
2436         }
2437     }
2438
2439     close_route_table();
2440     return result;
2441 }
2442
2443 /********************************************************************
2444  *
2445  * sif6defaultroute - assign a default route through the address given.
2446  *
2447  * If the global default_rt_repl_rest flag is set, then this function
2448  * already replaced the original system defaultroute with some other
2449  * route and it should just replace the current defaultroute with
2450  * another one, without saving the current route. Use: demand mode,
2451  * when pppd sets first a defaultroute it it's temporary ppp0 addresses
2452  * and then changes the temporary addresses to the addresses for the real
2453  * ppp connection when it has come up.
2454  */
2455
2456 int sif6defaultroute (int unit, eui64_t ouraddr, eui64_t gateway)
2457 {
2458     struct in6_rtmsg rt;
2459     char buf[IF_NAMESIZE];
2460
2461     if (defaultroute6_exists(&rt, dfl_route_metric) &&
2462             rt.rtmsg_ifindex != if_nametoindex(ifname)) {
2463         if (rt.rtmsg_flags & RTF_GATEWAY)
2464             error("not replacing existing default route via gateway");
2465         else
2466             error("not replacing existing default route through %s",
2467                   if_indextoname(rt.rtmsg_ifindex, buf));
2468         return 0;
2469     }
2470
2471     memset (&rt, 0, sizeof (rt));
2472
2473     rt.rtmsg_ifindex = if_nametoindex(ifname);
2474     rt.rtmsg_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
2475     rt.rtmsg_dst_len = 0;
2476
2477     rt.rtmsg_flags = RTF_UP;
2478     if (ioctl(sock6_fd, SIOCADDRT, &rt) < 0) {
2479         if ( ! ok_error ( errno ))
2480             error("default route ioctl(SIOCADDRT): %m");
2481         return 0;
2482     }
2483
2484     have_default_route6 = 1;
2485     return 1;
2486 }
2487
2488 /********************************************************************
2489  *
2490  * cif6defaultroute - delete a default route through the address given.
2491  */
2492
2493 int cif6defaultroute (int unit, eui64_t ouraddr, eui64_t gateway)
2494 {
2495     struct in6_rtmsg rt;
2496
2497     have_default_route6 = 0;
2498
2499     memset (&rt, '\0', sizeof (rt));
2500
2501     rt.rtmsg_ifindex = if_nametoindex(ifname);
2502     rt.rtmsg_metric = dfl_route_metric + 1; /* +1 for binary compatibility */
2503     rt.rtmsg_dst_len = 0;
2504
2505     rt.rtmsg_flags = RTF_UP;
2506     if (ioctl(sock6_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
2507         if (still_ppp()) {
2508             if ( ! ok_error ( errno ))
2509                 error("default route ioctl(SIOCDELRT): %m");
2510             return 0;
2511         }
2512     }
2513
2514     return 1;
2515 }
2516 #endif /* PPP_WITH_IPV6CP */
2517
2518 /********************************************************************
2519  *
2520  * sifproxyarp - Make a proxy ARP entry for the peer.
2521  */
2522
2523 int sifproxyarp (int unit, u_int32_t his_adr)
2524 {
2525     struct arpreq arpreq;
2526     char *forw_path;
2527
2528     if (has_proxy_arp == 0) {
2529         memset (&arpreq, '\0', sizeof(arpreq));
2530
2531         SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
2532         SIN_ADDR(arpreq.arp_pa) = his_adr;
2533         arpreq.arp_flags = ATF_PERM | ATF_PUBL;
2534 /*
2535  * Get the hardware address of an interface on the same subnet
2536  * as our local address.
2537  */
2538         if (!get_ether_addr(his_adr, &arpreq.arp_ha, proxy_arp_dev,
2539                             sizeof(proxy_arp_dev))) {
2540             error("Cannot determine ethernet address for proxy ARP");
2541             return 0;
2542         }
2543         strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev));
2544
2545         if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0) {
2546             if ( ! ok_error ( errno ))
2547                 error("ioctl(SIOCSARP): %m");
2548             return 0;
2549         }
2550         proxy_arp_addr = his_adr;
2551         has_proxy_arp = 1;
2552
2553         if (tune_kernel) {
2554             forw_path = path_to_procfs("/sys/net/ipv4/ip_forward");
2555             if (forw_path != 0) {
2556                 int fd = open(forw_path, O_WRONLY);
2557                 if (fd >= 0) {
2558                     if (write(fd, "1", 1) != 1)
2559                         error("Couldn't enable IP forwarding: %m");
2560                     close(fd);
2561                 }
2562             }
2563         }
2564     }
2565
2566     return 1;
2567 }
2568
2569 /********************************************************************
2570  *
2571  * cifproxyarp - Delete the proxy ARP entry for the peer.
2572  */
2573
2574 int cifproxyarp (int unit, u_int32_t his_adr)
2575 {
2576     struct arpreq arpreq;
2577
2578     if (has_proxy_arp) {
2579         has_proxy_arp = 0;
2580         memset (&arpreq, '\0', sizeof(arpreq));
2581         SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
2582         SIN_ADDR(arpreq.arp_pa) = his_adr;
2583         arpreq.arp_flags = ATF_PERM | ATF_PUBL;
2584         strlcpy(arpreq.arp_dev, proxy_arp_dev, sizeof(arpreq.arp_dev));
2585
2586         if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0) {
2587             if ( ! ok_error ( errno ))
2588                 warn("ioctl(SIOCDARP): %m");
2589             return 0;
2590         }
2591     }
2592     return 1;
2593 }
2594
2595 /********************************************************************
2596  *
2597  * get_ether_addr - get the hardware address of an interface on the
2598  * the same subnet as ipaddr.
2599  */
2600
2601 static int get_ether_addr (u_int32_t ipaddr,
2602                            struct sockaddr *hwaddr,
2603                            char *name, int namelen)
2604 {
2605     struct ifreq *ifr, *ifend;
2606     u_int32_t ina, mask;
2607     char *aliasp;
2608     struct ifreq ifreq, bestifreq;
2609     struct ifconf ifc;
2610     struct ifreq ifs[MAX_IFS];
2611
2612     u_int32_t bestmask=0;
2613     int found_interface = 0;
2614
2615     ifc.ifc_len = sizeof(ifs);
2616     ifc.ifc_req = ifs;
2617     if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) {
2618         if ( ! ok_error ( errno ))
2619             error("ioctl(SIOCGIFCONF): %m (line %d)", __LINE__);
2620         return 0;
2621     }
2622
2623 /*
2624  * Scan through looking for an interface with an Internet
2625  * address on the same subnet as `ipaddr'.
2626  */
2627     ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
2628     for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
2629         if (ifr->ifr_addr.sa_family == AF_INET) {
2630             ina = SIN_ADDR(ifr->ifr_addr);
2631             strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
2632 /*
2633  * Check that the interface is up, and not point-to-point
2634  * nor loopback.
2635  */
2636             if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
2637                 continue;
2638
2639             if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
2640                 continue;
2641 /*
2642  * Get its netmask and check that it's on the right subnet.
2643  */
2644             if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
2645                 continue;
2646
2647             mask = SIN_ADDR(ifreq.ifr_addr);
2648
2649             if (((ipaddr ^ ina) & mask) != 0)
2650                 continue; /* no match */
2651             /* matched */
2652             if (mask >= bestmask) {
2653                 /* Compare using >= instead of > -- it is possible for
2654                    an interface to have a netmask of 0.0.0.0 */
2655                 found_interface = 1;
2656                 bestifreq = ifreq;
2657                 bestmask = mask;
2658             }
2659         }
2660     }
2661
2662     if (!found_interface) return 0;
2663
2664     strlcpy(name, bestifreq.ifr_name, namelen);
2665
2666     /* trim off the :1 in eth0:1 */
2667     aliasp = strchr(name, ':');
2668     if (aliasp != 0)
2669         *aliasp = 0;
2670
2671     info("found interface %s for proxy arp", name);
2672 /*
2673  * Now get the hardware address.
2674  */
2675     memset (&bestifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
2676     if (ioctl (sock_fd, SIOCGIFHWADDR, &bestifreq) < 0) {
2677         error("SIOCGIFHWADDR(%s): %m", bestifreq.ifr_name);
2678         return 0;
2679     }
2680
2681     memcpy (hwaddr,
2682             &bestifreq.ifr_hwaddr,
2683             sizeof (struct sockaddr));
2684
2685     return 1;
2686 }
2687
2688 /*
2689  * get_if_hwaddr - get the hardware address for the specified
2690  * network interface device.
2691  */
2692 int
2693 get_if_hwaddr(u_char *addr, char *name)
2694 {
2695         struct ifreq ifreq;
2696         int ret, sock_fd;
2697
2698         sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
2699         if (sock_fd < 0)
2700                 return -1;
2701         memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
2702         strlcpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
2703         ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
2704         close(sock_fd);
2705         if (ret >= 0)
2706                 memcpy(addr, ifreq.ifr_hwaddr.sa_data, 6);
2707         return ret;
2708 }
2709
2710 /*
2711  * get_first_ether_hwaddr - get the hardware address for the first
2712  * ethernet-style interface on this system.
2713  */
2714 int
2715 get_first_ether_hwaddr(u_char *addr)
2716 {
2717         struct if_nameindex *if_ni, *i;
2718         struct ifreq ifreq;
2719         int ret, sock_fd;
2720
2721         sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
2722         if (sock_fd < 0)
2723                 return -1;
2724
2725         if_ni = if_nameindex();
2726         if (!if_ni) {
2727                 close(sock_fd);
2728                 return -1;
2729         }
2730
2731         ret = -1;
2732
2733         for (i = if_ni; !(i->if_index == 0 && i->if_name == NULL); i++) {
2734                 memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
2735                 strlcpy(ifreq.ifr_name, i->if_name, sizeof(ifreq.ifr_name));
2736                 ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
2737                 if (ret >= 0 && ifreq.ifr_hwaddr.sa_family == ARPHRD_ETHER) {
2738                         memcpy(addr, ifreq.ifr_hwaddr.sa_data, 6);
2739                         break;
2740                 }
2741                 ret = -1;
2742         }
2743
2744         if_freenameindex(if_ni);
2745         close(sock_fd);
2746
2747         return ret;
2748 }
2749
2750 /********************************************************************
2751  *
2752  * Return user specified netmask, modified by any mask we might determine
2753  * for address `addr' (in network byte order).
2754  * Here we scan through the system's list of interfaces, looking for
2755  * any non-point-to-point interfaces which might appear to be on the same
2756  * network as `addr'.  If we find any, we OR in their netmask to the
2757  * user-specified netmask.
2758  */
2759
2760 u_int32_t GetMask (u_int32_t addr)
2761 {
2762     u_int32_t mask, nmask, ina;
2763     struct ifreq *ifr, *ifend, ifreq;
2764     struct ifconf ifc;
2765     struct ifreq ifs[MAX_IFS];
2766
2767     addr = ntohl(addr);
2768
2769     if (IN_CLASSA(addr))        /* determine network mask for address class */
2770         nmask = IN_CLASSA_NET;
2771     else if (IN_CLASSB(addr))
2772             nmask = IN_CLASSB_NET;
2773     else
2774             nmask = IN_CLASSC_NET;
2775
2776     /* class D nets are disallowed by bad_ip_adrs */
2777     mask = netmask | htonl(nmask);
2778 /*
2779  * Scan through the system's network interfaces.
2780  */
2781     ifc.ifc_len = sizeof(ifs);
2782     ifc.ifc_req = ifs;
2783     if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) {
2784         if ( ! ok_error ( errno ))
2785             warn("ioctl(SIOCGIFCONF): %m (line %d)", __LINE__);
2786         return mask;
2787     }
2788
2789     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
2790     for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
2791 /*
2792  * Check the interface's internet address.
2793  */
2794         if (ifr->ifr_addr.sa_family != AF_INET)
2795             continue;
2796         ina = SIN_ADDR(ifr->ifr_addr);
2797         if (((ntohl(ina) ^ addr) & nmask) != 0)
2798             continue;
2799 /*
2800  * Check that the interface is up, and not point-to-point nor loopback.
2801  */
2802         strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
2803         if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
2804             continue;
2805
2806         if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
2807             continue;
2808 /*
2809  * Get its netmask and OR it into our mask.
2810  */
2811         if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
2812             continue;
2813         mask |= SIN_ADDR(ifreq.ifr_addr);
2814         break;
2815     }
2816     return mask;
2817 }
2818
2819 /********************************************************************
2820  *
2821  * Internal routine to decode the version.modification.patch level
2822  */
2823
2824 static void decode_version (char *buf, int *version,
2825                             int *modification, int *patch)
2826 {
2827     char *endp;
2828
2829     *version      = (int) strtoul (buf, &endp, 10);
2830     *modification = 0;
2831     *patch        = 0;
2832
2833     if (endp != buf && *endp == '.') {
2834         buf = endp + 1;
2835         *modification = (int) strtoul (buf, &endp, 10);
2836         if (endp != buf && *endp == '.') {
2837             buf = endp + 1;
2838             *patch = (int) strtoul (buf, &buf, 10);
2839         }
2840     }
2841 }
2842
2843 /********************************************************************
2844  *
2845  * Procedure to determine if the PPP line discipline is registered.
2846  */
2847
2848 static int
2849 ppp_registered(void)
2850 {
2851     int local_fd;
2852     int mfd = -1;
2853     int ret = 0;
2854     char slave[16];
2855
2856     /*
2857      * We used to open the serial device and set it to the ppp line
2858      * discipline here, in order to create a ppp unit.  But that is
2859      * not a good idea - the user might have specified a device that
2860      * they can't open (permission, or maybe it doesn't really exist).
2861      * So we grab a pty master/slave pair and use that.
2862      */
2863     if (!get_pty(&mfd, &local_fd, slave, 0)) {
2864         no_ppp_msg = "Couldn't determine if PPP is supported (no free ptys)";
2865         return 0;
2866     }
2867
2868     /*
2869      * Try to put the device into the PPP discipline.
2870      */
2871     if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0) {
2872         error("ioctl(TIOCSETD(PPP)): %m (line %d)", __LINE__);
2873     } else
2874         ret = 1;
2875
2876     close(local_fd);
2877     close(mfd);
2878     return ret;
2879 }
2880
2881 /********************************************************************
2882  *
2883  * ppp_available - check whether the system has any ppp interfaces
2884  * (in fact we check whether we can do an ioctl on ppp0).
2885  */
2886
2887 int ppp_available(void)
2888 {
2889     int s, ok, fd;
2890     struct ifreq ifr;
2891     int    size;
2892     int    my_version, my_modification, my_patch;
2893     int osmaj, osmin, ospatch;
2894
2895     /* get the kernel version now, since we are called before sys_init */
2896     uname(&utsname);
2897     osmaj = osmin = ospatch = 0;
2898     sscanf(utsname.release, "%d.%d.%d", &osmaj, &osmin, &ospatch);
2899     kernel_version = KVERSION(osmaj, osmin, ospatch);
2900
2901     fd = open("/dev/ppp", O_RDWR);
2902     if (fd >= 0) {
2903         new_style_driver = 1;
2904
2905         /* XXX should get from driver */
2906         driver_version = 2;
2907         driver_modification = 4;
2908         driver_patch = 0;
2909         close(fd);
2910         return 1;
2911     }
2912
2913     if (kernel_version >= KVERSION(2,3,13)) {
2914         error("Couldn't open the /dev/ppp device: %m");
2915         if (errno == ENOENT)
2916             no_ppp_msg =
2917                 "You need to create the /dev/ppp device node by\n"
2918                 "executing the following command as root:\n"
2919                 "       mknod /dev/ppp c 108 0\n";
2920         else if (errno == ENODEV || errno == ENXIO)
2921             no_ppp_msg =
2922                 "Please load the ppp_generic kernel module.\n";
2923         return 0;
2924     }
2925
2926     /* we are running on a really really old kernel */
2927     no_ppp_msg =
2928         "This system lacks kernel support for PPP.  This could be because\n"
2929         "the PPP kernel module could not be loaded, or because PPP was not\n"
2930         "included in the kernel configuration.  If PPP was included as a\n"
2931         "module, try `/sbin/modprobe -v ppp'.  If that fails, check that\n"
2932         "ppp.o exists in /lib/modules/`uname -r`/net.\n"
2933         "See README.linux file in the ppp distribution for more details.\n";
2934
2935 /*
2936  * Open a socket for doing the ioctl operations.
2937  */
2938     s = socket(AF_INET, SOCK_DGRAM, 0);
2939     if (s < 0)
2940         return 0;
2941
2942     strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
2943     ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
2944 /*
2945  * If the device did not exist then attempt to create one by putting the
2946  * current tty into the PPP discipline. If this works then obtain the
2947  * flags for the device again.
2948  */
2949     if (!ok) {
2950         if (ppp_registered()) {
2951             strlcpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
2952             ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
2953         }
2954     }
2955 /*
2956  * Ensure that the hardware address is for PPP and not something else
2957  */
2958     if (ok)
2959         ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
2960
2961     if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
2962         ok = 0;
2963
2964 /*
2965  *  This is the PPP device. Validate the version of the driver at this
2966  *  point to ensure that this program will work with the driver.
2967  */
2968     if (ok) {
2969         char   abBuffer [1024];
2970
2971         ifr.ifr_data = abBuffer;
2972         size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
2973         if (size < 0) {
2974             error("Couldn't read driver version: %m");
2975             ok = 0;
2976             no_ppp_msg = "Sorry, couldn't verify kernel driver version\n";
2977
2978         } else {
2979             decode_version(abBuffer,
2980                            &driver_version,
2981                            &driver_modification,
2982                            &driver_patch);
2983 /*
2984  * Validate the version of the driver against the version that we used.
2985  */
2986             decode_version(VERSION,
2987                            &my_version,
2988                            &my_modification,
2989                            &my_patch);
2990
2991             /* The version numbers must match */
2992             if (driver_version != my_version)
2993                 ok = 0;
2994
2995             /* The modification levels must be legal */
2996             if (driver_modification < 3) {
2997                 if (driver_modification >= 2) {
2998                     /* we can cope with 2.2.0 and above */
2999                     driver_is_old = 1;
3000                 } else {
3001                     ok = 0;
3002                 }
3003             }
3004
3005             if (!ok) {
3006                 slprintf(route_buffer, sizeof(route_buffer),
3007                          "Sorry - PPP driver version %d.%d.%d is out of date\n",
3008                          driver_version, driver_modification, driver_patch);
3009
3010                 no_ppp_msg = route_buffer;
3011             }
3012         }
3013     }
3014     close(s);
3015     return ok;
3016 }
3017
3018 #ifndef HAVE_LOGWTMP
3019 /********************************************************************
3020  *
3021  * Update the wtmp file with the appropriate user name and tty device.
3022  */
3023
3024 void logwtmp (const char *line, const char *name, const char *host)
3025 {
3026     struct utmp ut, *utp;
3027     pid_t  mypid = getpid();
3028 #if __GLIBC__ < 2
3029     int    wtmp;
3030 #endif
3031
3032 /*
3033  * Update the signon database for users.
3034  * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
3035  */
3036     utmpname(_PATH_UTMP);
3037     setutent();
3038     while ((utp = getutent()) && (utp->ut_pid != mypid))
3039         /* nothing */;
3040
3041     if (utp)
3042         memcpy(&ut, utp, sizeof(ut));
3043     else
3044         /* some gettys/telnetds don't initialize utmp... */
3045         memset(&ut, 0, sizeof(ut));
3046
3047     if (ut.ut_id[0] == 0)
3048         strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
3049
3050     strncpy(ut.ut_user, name, sizeof(ut.ut_user));
3051     strncpy(ut.ut_line, line, sizeof(ut.ut_line));
3052
3053     time(&ut.ut_time);
3054
3055     ut.ut_type = USER_PROCESS;
3056     ut.ut_pid  = mypid;
3057
3058     /* Insert the host name if one is supplied */
3059     if (*host)
3060         strncpy (ut.ut_host, host, sizeof(ut.ut_host));
3061
3062     /* Insert the IP address of the remote system if IP is enabled */
3063     if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
3064         memcpy(&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
3065                  sizeof(ut.ut_addr));
3066
3067     /* CL: Makes sure that the logout works */
3068     if (*host == 0 && *name==0)
3069         ut.ut_host[0]=0;
3070
3071     pututline(&ut);
3072     endutent();
3073 /*
3074  * Update the wtmp file.
3075  */
3076 #if __GLIBC__ >= 2
3077     updwtmp(_PATH_WTMP, &ut);
3078 #else
3079     wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
3080     if (wtmp >= 0) {
3081         flock(wtmp, LOCK_EX);
3082
3083         if (write (wtmp, (char *)&ut, sizeof(ut)) != sizeof(ut))
3084             warn("error writing %s: %m", _PATH_WTMP);
3085
3086         flock(wtmp, LOCK_UN);
3087
3088         close (wtmp);
3089     }
3090 #endif
3091 }
3092 #endif /* HAVE_LOGWTMP */
3093
3094 /********************************************************************
3095  *
3096  * sifvjcomp - config tcp header compression
3097  */
3098
3099 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
3100 {
3101         u_int x;
3102
3103         if (vjcomp) {
3104                 if (ioctl(ppp_dev_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
3105                         error("Couldn't set up TCP header compression: %m");
3106                         vjcomp = 0;
3107                 }
3108         }
3109
3110         x = (vjcomp? SC_COMP_TCP: 0) | (cidcomp? 0: SC_NO_TCP_CCID);
3111         modify_flags(ppp_dev_fd, SC_COMP_TCP|SC_NO_TCP_CCID, x);
3112
3113         return 1;
3114 }
3115
3116 /********************************************************************
3117  *
3118  * sifup - Config the interface up and enable IP packets to pass.
3119  */
3120
3121 int sifup(int u)
3122 {
3123     int ret;
3124
3125     if ((ret = setifstate(u, 1)))
3126         if_is_up++;
3127
3128     return ret;
3129 }
3130
3131 /********************************************************************
3132  *
3133  * sifdown - Disable the indicated protocol and config the interface
3134  *           down if there are no remaining protocols.
3135  */
3136
3137 int sifdown (int u)
3138 {
3139     if (if_is_up && --if_is_up > 0)
3140         return 1;
3141
3142 #ifdef PPP_WITH_IPV6CP
3143     if (if6_is_up)
3144         return 1;
3145 #endif /* PPP_WITH_IPV6CP */
3146
3147     return setifstate(u, 0);
3148 }
3149
3150 #ifdef PPP_WITH_IPV6CP
3151 /********************************************************************
3152  *
3153  * sif6up - Config the interface up for IPv6
3154  */
3155
3156 int sif6up(int u)
3157 {
3158     int ret;
3159
3160     if ((ret = setifstate(u, 1)))
3161         if6_is_up = 1;
3162
3163     return ret;
3164 }
3165
3166 /********************************************************************
3167  *
3168  * sif6down - Disable the IPv6CP protocol and config the interface
3169  *            down if there are no remaining protocols.
3170  */
3171
3172 int sif6down (int u)
3173 {
3174     if6_is_up = 0;
3175
3176     if (if_is_up)
3177         return 1;
3178
3179     return setifstate(u, 0);
3180 }
3181 #endif /* PPP_WITH_IPV6CP */
3182
3183 /********************************************************************
3184  *
3185  * setifstate - Config the interface up or down
3186  */
3187
3188 static int setifstate (int u, int state)
3189 {
3190     struct ifreq ifr;
3191
3192     memset (&ifr, '\0', sizeof (ifr));
3193     strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
3194     if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
3195         if (! ok_error (errno))
3196             error("ioctl (SIOCGIFFLAGS): %m (line %d)", __LINE__);
3197         return 0;
3198     }
3199
3200     if (state)
3201         ifr.ifr_flags |= IFF_UP;
3202     else
3203         ifr.ifr_flags &= ~IFF_UP;
3204     ifr.ifr_flags |= IFF_POINTOPOINT;
3205     if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
3206         if (! ok_error (errno))
3207             error("ioctl(SIOCSIFFLAGS): %m (line %d)", __LINE__);
3208         return 0;
3209     }
3210     return 1;
3211 }
3212
3213 /********************************************************************
3214  *
3215  * sifaddr - Config the interface IP addresses and netmask.
3216  */
3217
3218 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
3219              u_int32_t net_mask)
3220 {
3221     struct ifreq   ifr;
3222     struct rtentry rt;
3223
3224     memset (&ifr, '\0', sizeof (ifr));
3225     memset (&rt,  '\0', sizeof (rt));
3226
3227     SET_SA_FAMILY (ifr.ifr_addr,    AF_INET);
3228     SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
3229     SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
3230
3231     strlcpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
3232 /*
3233  *  Set our IP address
3234  */
3235     SIN_ADDR(ifr.ifr_addr) = our_adr;
3236     if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
3237         if (errno != EEXIST) {
3238             if (! ok_error (errno))
3239                 error("ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
3240         }
3241         else {
3242             warn("ioctl(SIOCSIFADDR): Address already exists");
3243         }
3244         return (0);
3245     }
3246 /*
3247  *  Set the gateway address
3248  */
3249     if (his_adr != 0) {
3250         SIN_ADDR(ifr.ifr_dstaddr) = his_adr;
3251         if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
3252             if (! ok_error (errno))
3253                 error("ioctl(SIOCSIFDSTADDR): %m (line %d)", __LINE__);
3254             return (0);
3255         }
3256     }
3257 /*
3258  *  Set the netmask.
3259  *  For recent kernels, force the netmask to 255.255.255.255.
3260  */
3261     if (kernel_version >= KVERSION(2,1,16))
3262         net_mask = ~0L;
3263     if (net_mask != 0) {
3264         SIN_ADDR(ifr.ifr_netmask) = net_mask;
3265         if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
3266             if (! ok_error (errno))
3267                 error("ioctl(SIOCSIFNETMASK): %m (line %d)", __LINE__);
3268             return (0);
3269         }
3270     }
3271 /*
3272  *  Add the device route
3273  */
3274     if (kernel_version < KVERSION(2,1,16)) {
3275         SET_SA_FAMILY (rt.rt_dst,     AF_INET);
3276         SET_SA_FAMILY (rt.rt_gateway, AF_INET);
3277         rt.rt_dev = ifname;
3278
3279         SIN_ADDR(rt.rt_gateway) = 0L;
3280         SIN_ADDR(rt.rt_dst)     = his_adr;
3281         rt.rt_flags = RTF_UP | RTF_HOST;
3282
3283         if (kernel_version > KVERSION(2,1,0)) {
3284             SET_SA_FAMILY (rt.rt_genmask, AF_INET);
3285             SIN_ADDR(rt.rt_genmask) = -1L;
3286         }
3287
3288         if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
3289             if (! ok_error (errno))
3290                 error("ioctl(SIOCADDRT) device route: %m (line %d)", __LINE__);
3291             return (0);
3292         }
3293     }
3294
3295     /* set ip_dynaddr in demand mode if address changes */
3296     if (demand && tune_kernel && !dynaddr_set
3297         && our_old_addr && our_old_addr != our_adr) {
3298         /* set ip_dynaddr if possible */
3299         char *path;
3300         int fd;
3301
3302         path = path_to_procfs("/sys/net/ipv4/ip_dynaddr");
3303         if (path != 0 && (fd = open(path, O_WRONLY)) >= 0) {
3304             if (write(fd, "1", 1) != 1)
3305                 error("Couldn't enable dynamic IP addressing: %m");
3306             close(fd);
3307         }
3308         dynaddr_set = 1;        /* only 1 attempt */
3309     }
3310     our_old_addr = 0;
3311
3312     return 1;
3313 }
3314
3315 /********************************************************************
3316  *
3317  * cifaddr - Clear the interface IP addresses, and delete routes
3318  * through the interface if possible.
3319  */
3320
3321 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
3322 {
3323     struct ifreq ifr;
3324
3325     if (kernel_version < KVERSION(2,1,16)) {
3326 /*
3327  *  Delete the route through the device
3328  */
3329         struct rtentry rt;
3330         memset (&rt, '\0', sizeof (rt));
3331
3332         SET_SA_FAMILY (rt.rt_dst,     AF_INET);
3333         SET_SA_FAMILY (rt.rt_gateway, AF_INET);
3334         rt.rt_dev = ifname;
3335
3336         SIN_ADDR(rt.rt_gateway) = 0;
3337         SIN_ADDR(rt.rt_dst)     = his_adr;
3338         rt.rt_flags = RTF_UP | RTF_HOST;
3339
3340         if (kernel_version > KVERSION(2,1,0)) {
3341             SET_SA_FAMILY (rt.rt_genmask, AF_INET);
3342             SIN_ADDR(rt.rt_genmask) = -1L;
3343         }
3344
3345         if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
3346             if (still_ppp() && ! ok_error (errno))
3347                 error("ioctl(SIOCDELRT) device route: %m (line %d)", __LINE__);
3348             return (0);
3349         }
3350     }
3351
3352     /* This way it is possible to have an IPv6-only interface */
3353     memset(&ifr, 0, sizeof(ifr));
3354     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
3355     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
3356
3357     if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
3358         if (! ok_error (errno)) {
3359             error("ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
3360             return 0;
3361         }
3362     }
3363
3364     our_old_addr = our_adr;
3365
3366     return 1;
3367 }
3368
3369 #ifdef PPP_WITH_IPV6CP
3370 /********************************************************************
3371  *
3372  * sif6addr_rtnetlink - Config the interface with both IPv6 link-local addresses via rtnetlink
3373  */
3374 static int sif6addr_rtnetlink(unsigned int iface, eui64_t our_eui64, eui64_t his_eui64)
3375 {
3376     struct {
3377         struct nlmsghdr nlh;
3378         struct ifaddrmsg ifa;
3379         struct {
3380             struct rtattr rta;
3381             struct in6_addr addr;
3382         } addrs[2];
3383     } nlreq;
3384     int resp;
3385
3386     memset(&nlreq, 0, sizeof(nlreq));
3387     nlreq.nlh.nlmsg_len = sizeof(nlreq);
3388     nlreq.nlh.nlmsg_type = RTM_NEWADDR;
3389     nlreq.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE;
3390     nlreq.ifa.ifa_family = AF_INET6;
3391     nlreq.ifa.ifa_prefixlen = 128;
3392     nlreq.ifa.ifa_flags = IFA_F_NODAD | IFA_F_PERMANENT;
3393     nlreq.ifa.ifa_scope = RT_SCOPE_LINK;
3394     nlreq.ifa.ifa_index = iface;
3395     nlreq.addrs[0].rta.rta_len = sizeof(nlreq.addrs[0]);
3396     nlreq.addrs[0].rta.rta_type = IFA_LOCAL;
3397     IN6_LLADDR_FROM_EUI64(nlreq.addrs[0].addr, our_eui64);
3398     nlreq.addrs[1].rta.rta_len = sizeof(nlreq.addrs[1]);
3399     nlreq.addrs[1].rta.rta_type = IFA_ADDRESS;
3400
3401     /*
3402      * To set only local address, older kernel expects that local address is
3403      * in IFA_ADDRESS field (not IFA_LOCAL). New kernels with support for peer
3404      * address, ignore IFA_ADDRESS if is same as IFA_LOCAL. So for backward
3405      * compatibility when setting only local address, set it via both IFA_LOCAL
3406      * and IFA_ADDRESS fields. Same logic is implemented in 'ip address' command
3407      * from iproute2 project.
3408      */
3409     if (!eui64_iszero(his_eui64))
3410         IN6_LLADDR_FROM_EUI64(nlreq.addrs[1].addr, his_eui64);
3411     else
3412         IN6_LLADDR_FROM_EUI64(nlreq.addrs[1].addr, our_eui64);
3413
3414     resp = send_rtnetlink_msg("RTM_NEWADDR/NLM_F_CREATE", &nlreq, sizeof(nlreq));
3415     if (resp) {
3416         /*
3417          * Linux kernel versions prior 3.11 do not support setting IPv6 peer
3418          * addresses and error response is expected. On older kernel versions
3419          * do not show this error message. On error pppd tries to fallback to
3420          * the old IOCTL method.
3421          */
3422         errno = (resp < 0) ? -resp : EINVAL;
3423         if (kernel_version >= KVERSION(3,11,0))
3424             error("sif6addr_rtnetlink: %m (line %d)", __LINE__);
3425         return 0;
3426     }
3427
3428     return 1;
3429 }
3430
3431 /********************************************************************
3432  *
3433  * sif6addr - Config the interface with an IPv6 link-local address
3434  */
3435 int sif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64)
3436 {
3437     struct in6_ifreq ifr6;
3438     struct ifreq ifr;
3439     struct in6_rtmsg rt6;
3440     int ret;
3441
3442     if (sock6_fd < 0) {
3443         errno = -sock6_fd;
3444         error("IPv6 socket creation failed: %m");
3445         return 0;
3446     }
3447     memset(&ifr, 0, sizeof (ifr));
3448     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
3449     if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) {
3450         error("sif6addr: ioctl(SIOCGIFINDEX): %m (line %d)", __LINE__);
3451         return 0;
3452     }
3453
3454     if (kernel_version >= KVERSION(2,1,16)) {
3455         /* Set both local address and remote peer address (with route for it) via rtnetlink */
3456         ret = sif6addr_rtnetlink(ifr.ifr_ifindex, our_eui64, his_eui64);
3457     } else {
3458         ret = 0;
3459     }
3460
3461     /*
3462      * Linux kernel versions prior 3.11 do not support setting IPv6 peer address
3463      * via rtnetlink. So if sif6addr_rtnetlink() fails then try old IOCTL method.
3464      */
3465     if (!ret) {
3466         /* Local interface */
3467         memset(&ifr6, 0, sizeof(ifr6));
3468         IN6_LLADDR_FROM_EUI64(ifr6.ifr6_addr, our_eui64);
3469         ifr6.ifr6_ifindex = ifr.ifr_ifindex;
3470         ifr6.ifr6_prefixlen = 128;
3471
3472         if (ioctl(sock6_fd, SIOCSIFADDR, &ifr6) < 0) {
3473             error("sif6addr: ioctl(SIOCSIFADDR): %m (line %d)", __LINE__);
3474             return 0;
3475         }
3476     }
3477
3478     if (!ret && !eui64_iszero(his_eui64)) {
3479         /*
3480          * Linux kernel does not provide AF_INET6 ioctl SIOCSIFDSTADDR for
3481          * setting remote peer host address, so set only route to remote host.
3482          */
3483
3484         /* Route to remote host */
3485         memset(&rt6, 0, sizeof(rt6));
3486         IN6_LLADDR_FROM_EUI64(rt6.rtmsg_dst, his_eui64);
3487         rt6.rtmsg_flags = RTF_UP;
3488         rt6.rtmsg_dst_len = 128;
3489         rt6.rtmsg_ifindex = ifr.ifr_ifindex;
3490         rt6.rtmsg_metric = 1;
3491
3492         if (ioctl(sock6_fd, SIOCADDRT, &rt6) < 0) {
3493             error("sif6addr: ioctl(SIOCADDRT): %m (line %d)", __LINE__);
3494             return 0;
3495         }
3496     }
3497
3498     return 1;
3499 }
3500
3501
3502 /********************************************************************
3503  *
3504  * cif6addr - Remove IPv6 address from interface
3505  */
3506 int cif6addr (int unit, eui64_t our_eui64, eui64_t his_eui64)
3507 {
3508     struct ifreq ifr;
3509     struct in6_ifreq ifr6;
3510
3511     if (sock6_fd < 0) {
3512         errno = -sock6_fd;
3513         error("IPv6 socket creation failed: %m");
3514         return 0;
3515     }
3516     memset(&ifr, 0, sizeof(ifr));
3517     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
3518     if (ioctl(sock6_fd, SIOCGIFINDEX, (caddr_t) &ifr) < 0) {
3519         error("cif6addr: ioctl(SIOCGIFINDEX): %m (line %d)", __LINE__);
3520         return 0;
3521     }
3522
3523     memset(&ifr6, 0, sizeof(ifr6));
3524     IN6_LLADDR_FROM_EUI64(ifr6.ifr6_addr, our_eui64);
3525     ifr6.ifr6_ifindex = ifr.ifr_ifindex;
3526     ifr6.ifr6_prefixlen = 128;
3527
3528     if (ioctl(sock6_fd, SIOCDIFADDR, &ifr6) < 0) {
3529         if (errno != EADDRNOTAVAIL) {
3530             if (! ok_error (errno))
3531                 error("cif6addr: ioctl(SIOCDIFADDR): %m (line %d)", __LINE__);
3532         }
3533         else {
3534             warn("cif6addr: ioctl(SIOCDIFADDR): No such address");
3535         }
3536         return (0);
3537     }
3538     return 1;
3539 }
3540 #endif /* PPP_WITH_IPV6CP */
3541
3542 /*
3543  * get_pty - get a pty master/slave pair and chown the slave side
3544  * to the uid given.  Assumes slave_name points to >= 16 bytes of space.
3545  */
3546 int
3547 get_pty(int *master_fdp, int *slave_fdp, char *slave_name, int uid)
3548 {
3549     int i, mfd, ret, sfd = -1;
3550     char pty_name[16];
3551     struct termios tios;
3552
3553 #ifdef TIOCGPTN
3554     /*
3555      * Try the unix98 way first.
3556      */
3557     mfd = open("/dev/ptmx", O_RDWR);
3558     if (mfd >= 0) {
3559         int ptn;
3560         if (ioctl(mfd, TIOCGPTN, &ptn) >= 0) {
3561             slprintf(pty_name, sizeof(pty_name), "/dev/pts/%d", ptn);
3562             chmod(pty_name, S_IRUSR | S_IWUSR);
3563 #ifdef TIOCSPTLCK
3564             ptn = 0;
3565             if (ioctl(mfd, TIOCSPTLCK, &ptn) < 0)
3566                 warn("Couldn't unlock pty slave %s: %m", pty_name);
3567 #endif
3568             if ((sfd = open(pty_name, O_RDWR | O_NOCTTY)) < 0)
3569             {
3570                 warn("Couldn't open pty slave %s: %m", pty_name);
3571                 close(mfd);
3572             }
3573         }
3574     }
3575 #endif /* TIOCGPTN */
3576
3577     if (sfd < 0) {
3578         /* the old way - scan through the pty name space */
3579         for (i = 0; i < 64; ++i) {
3580             slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x",
3581                      'p' + i / 16, i % 16);
3582             mfd = open(pty_name, O_RDWR, 0);
3583             if (mfd >= 0) {
3584                 pty_name[5] = 't';
3585                 sfd = open(pty_name, O_RDWR | O_NOCTTY, 0);
3586                 if (sfd >= 0) {
3587                     ret = fchown(sfd, uid, -1);
3588                     if (ret != 0) {
3589                         warn("Couldn't change ownership of %s, %m", pty_name);
3590                     }
3591                     ret = fchmod(sfd, S_IRUSR | S_IWUSR);
3592                     if (ret != 0) {
3593                         warn("Couldn't change permissions of %s, %m", pty_name);
3594                     }
3595                     break;
3596                 }
3597                 close(mfd);
3598             }
3599         }
3600     }
3601
3602     if (sfd < 0)
3603         return 0;
3604
3605     strlcpy(slave_name, pty_name, 16);
3606     *master_fdp = mfd;
3607     *slave_fdp = sfd;
3608     if (tcgetattr(sfd, &tios) == 0) {
3609         tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
3610         tios.c_cflag |= CS8 | CREAD | CLOCAL;
3611         tios.c_iflag  = IGNPAR;
3612         tios.c_oflag  = 0;
3613         tios.c_lflag  = 0;
3614         if (tcsetattr(sfd, TCSAFLUSH, &tios) < 0)
3615             warn("couldn't set attributes on pty: %m");
3616     } else
3617         warn("couldn't get attributes on pty: %m");
3618
3619     return 1;
3620 }
3621
3622 /********************************************************************
3623  *
3624  * open_loopback - open the device we use for getting packets
3625  * in demand mode.  Under Linux, we use a pty master/slave pair.
3626  */
3627 int
3628 open_ppp_loopback(void)
3629 {
3630     int flags;
3631
3632     looped = 1;
3633     if (new_style_driver) {
3634         /* allocate ourselves a ppp unit */
3635         if (make_ppp_unit() < 0)
3636             die(1);
3637         modify_flags(ppp_dev_fd, 0, SC_LOOP_TRAFFIC);
3638         set_kdebugflag(kdebugflag);
3639         ppp_fd = -1;
3640         return ppp_dev_fd;
3641     }
3642
3643     if (!get_pty(&master_fd, &slave_fd, loop_name, 0))
3644         fatal("No free pty for loopback");
3645
3646     set_ppp_fd(slave_fd);
3647
3648     flags = fcntl(master_fd, F_GETFL);
3649     if (flags == -1 ||
3650         fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
3651         warn("couldn't set master loopback to nonblock: %m");
3652
3653     flags = fcntl(ppp_fd, F_GETFL);
3654     if (flags == -1 ||
3655         fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
3656         warn("couldn't set slave loopback to nonblock: %m");
3657
3658     if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
3659         fatal("ioctl(TIOCSETD): %m (line %d)", __LINE__);
3660 /*
3661  * Find out which interface we were given.
3662  */
3663     if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
3664         fatal("ioctl(PPPIOCGUNIT): %m (line %d)", __LINE__);
3665 /*
3666  * Enable debug in the driver if requested.
3667  */
3668     set_kdebugflag (kdebugflag);
3669
3670     return master_fd;
3671 }
3672
3673 /********************************************************************
3674  *
3675  * sifnpmode - Set the mode for handling packets for a given NP.
3676  */
3677
3678 int
3679 sifnpmode(int u, int proto, enum NPmode mode)
3680 {
3681     struct npioctl npi;
3682
3683     npi.protocol = proto;
3684     npi.mode     = mode;
3685     if (ioctl(ppp_dev_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
3686         if (! ok_error (errno))
3687             error("ioctl(PPPIOCSNPMODE, %d, %d): %m", proto, mode);
3688         return 0;
3689     }
3690     return 1;
3691 }
3692
3693 /*
3694  * Use the hostname as part of the random number seed.
3695  */
3696 int
3697 get_host_seed(void)
3698 {
3699     int h;
3700     char *p = hostname;
3701
3702     h = 407;
3703     for (p = hostname; *p != 0; ++p)
3704         h = h * 37 + *p;
3705     return h;
3706 }
3707
3708 /********************************************************************
3709  *
3710  * sys_check_options - check the options that the user specified
3711  */
3712
3713 int
3714 sys_check_options(void)
3715 {
3716     if (demand && driver_is_old) {
3717         option_error("demand dialling is not supported by kernel driver "
3718                      "version %d.%d.%d", driver_version, driver_modification,
3719                      driver_patch);
3720         return 0;
3721     }
3722     if (multilink && !new_style_driver) {
3723         warn("Warning: multilink is not supported by the kernel driver");
3724         multilink = 0;
3725     }
3726     return 1;
3727 }
3728
3729 /********************************************************************
3730  *
3731  * get_time - Get current time, monotonic if possible.
3732  */
3733 int
3734 get_time(struct timeval *tv)
3735 {
3736 /* Old glibc (< 2.3.4) does define CLOCK_MONOTONIC, but kernel may have it.
3737  * Runtime checking makes it safe. */
3738 #ifndef CLOCK_MONOTONIC
3739 #define CLOCK_MONOTONIC 1
3740 #endif
3741     static int monotonic = -1;
3742     struct timespec ts;
3743     int ret;
3744
3745     if (monotonic) {
3746         ret = clock_gettime(CLOCK_MONOTONIC, &ts);
3747         if (ret == 0) {
3748             monotonic = 1;
3749             if (tv) {
3750                 tv->tv_sec = ts.tv_sec;
3751                 tv->tv_usec = ts.tv_nsec / 1000;
3752             }
3753             return ret;
3754         } else if (monotonic > 0)
3755             return ret;
3756
3757         monotonic = 0;
3758         warn("Couldn't use monotonic clock source: %m");
3759     }
3760
3761     return gettimeofday(tv, NULL);
3762 }