2 * sys-linux.c - System-dependent procedures for setting up
3 * PPP interfaces on Linux systems
5 * Copyright (c) 1989 Carnegie Mellon University.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by Carnegie Mellon University. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <sys/errno.h>
28 #include <sys/utsname.h>
29 #include <sys/sysmacros.h>
45 /* This is in netdevice.h. However, this compile will fail miserably if
46 you attempt to include netdevice.h because it has so many references
47 to __memcpy functions which it should not attempt to do. So, since I
48 really don't use it, but it must be defined, define it now. */
51 #define MAX_ADDR_LEN 7
55 #include <asm/types.h> /* glibc 2 conflicts with linux/types.h */
57 #include <net/if_arp.h>
58 #include <net/route.h>
59 #include <netinet/if_ether.h>
61 #include <linux/types.h>
63 #include <linux/if_arp.h>
64 #include <linux/route.h>
65 #include <linux/if_ether.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
70 #include <linux/ppp_defs.h>
71 #include <linux/if_ppp.h>
77 /* We can get an EIO error on an ioctl if the modem has hung up */
78 #define ok_error(num) ((num)==EIO)
80 static int tty_disc
= N_TTY
; /* The TTY discipline */
81 static int ppp_disc
= N_PPP
; /* The PPP discpline */
82 static int initfdflags
= -1; /* Initial file descriptor flags for fd */
83 static int ppp_fd
= -1; /* fd which is set to PPP discipline */
84 static int sock_fd
= -1; /* socket for doing interface ioctls */
85 static int slave_fd
= -1;
86 static int master_fd
= -1;
87 static int ppp_dev_fd
= -1; /* fd for /dev/ppp (new style driver) */
88 static int chindex
; /* channel index (new style driver) */
90 static fd_set in_fds
; /* set of fds that wait_input waits for */
91 static int max_in_fd
; /* highest fd set in in_fds */
93 static int driver_version
= 0;
94 static int driver_modification
= 0;
95 static int driver_patch
= 0;
97 // static char loop_name[20];
98 static unsigned char inbuf
[512]; /* buffer for chars read from loopback */
100 static int if_is_up
; /* Interface has been marked up */
101 static u_int32_t our_old_addr
; /* for detecting address changes */
102 static int dynaddr_set
; /* 1 if ip_dynaddr set */
103 static int looped
; /* 1 if using loop */
105 static int kernel_version
;
106 #define KVERSION(j,n,p) ((j)*1000000 + (n)*1000 + (p))
110 #define FLAGS_GOOD (IFF_UP | IFF_BROADCAST)
111 #define FLAGS_MASK (IFF_UP | IFF_BROADCAST | \
112 IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)
114 #define SIN_ADDR(x) (((struct sockaddr_in *) (&(x)))->sin_addr.s_addr)
116 /* Prototypes for procedures local to this file. */
117 static int get_flags (int fd
);
118 static void set_flags (int fd
, int flags
);
119 static int make_ppp_unit(void);
120 static void restore_loop(void); /* Transfer ppp unit back to loopback */
122 extern u_char inpacket_buf
[]; /* borrowed from main.c */
125 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
129 #define SET_SA_FAMILY(addr, family) \
130 memset ((char *) &(addr), '\0', sizeof(addr)); \
131 addr.sa_family = (family);
134 * Determine if the PPP connection should still be present.
139 /* new_fd is the fd of a tty */
140 static void set_ppp_fd (int new_fd
)
142 SYSDEBUG ((LOG_DEBUG
, "setting ppp_fd to %d\n", new_fd
));
144 if (!new_style_driver
)
148 static int still_ppp(void)
150 if (new_style_driver
)
151 return !hungup
&& ppp_fd
>= 0;
152 if (!hungup
|| ppp_fd
== slave_fd
)
155 set_ppp_fd(slave_fd
);
161 /********************************************************************
163 * Functions to read and set the flags value in the device driver
166 static int get_flags (int fd
)
170 if (ioctl(fd
, PPPIOCGFLAGS
, (caddr_t
) &flags
) < 0) {
171 if ( ok_error (errno
) )
174 fatal("ioctl(PPPIOCGFLAGS): %m");
177 SYSDEBUG ((LOG_DEBUG
, "get flags = %x\n", flags
));
181 /********************************************************************/
183 static void set_flags (int fd
, int flags
)
185 SYSDEBUG ((LOG_DEBUG
, "set flags = %x\n", flags
));
187 if (ioctl(fd
, PPPIOCSFLAGS
, (caddr_t
) &flags
) < 0) {
188 if (! ok_error (errno
) )
189 fatal("ioctl(PPPIOCSFLAGS, %x): %m", flags
, errno
);
193 /********************************************************************
195 * sys_init - System-dependent initialization.
202 if (new_style_driver
) {
203 ppp_dev_fd
= open("/dev/ppp", O_RDWR
);
205 fatal("Couldn't open /dev/ppp: %m");
206 flags
= fcntl(ppp_dev_fd
, F_GETFL
);
208 || fcntl(ppp_dev_fd
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
209 warn("Couldn't set /dev/ppp to nonblock: %m");
212 /* Get an internet socket for doing socket ioctls. */
213 sock_fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
215 fatal("Couldn't create IP socket: %m(%d)", errno
);
221 /********************************************************************
223 * sys_cleanup - restore any system state we modified before exiting:
224 * mark the interface down, delete default route and/or proxy arp entry.
225 * This shouldn't call die() because it's called from die().
228 void sys_cleanup(void)
231 * Take down the device
239 /********************************************************************
241 * sys_close - Clean up in a child process before execing.
256 /********************************************************************
258 * set_kdebugflag - Define the debugging level for the kernel
261 static int set_kdebugflag (int requested_level
)
263 if (new_style_driver
&& ifunit
< 0)
265 if (ioctl(ppp_dev_fd
, PPPIOCSDEBUG
, &requested_level
) < 0) {
266 if ( ! ok_error (errno
) )
267 error("ioctl(PPPIOCSDEBUG): %m");
270 SYSDEBUG ((LOG_INFO
, "set kernel debugging level to %d",
276 /********************************************************************
278 * generic_establish_ppp - Turn the fd into a ppp interface.
280 int generic_establish_ppp (int fd
)
284 * Demand mode - prime the old ppp device to relinquish the unit.
286 if (!new_style_driver
&& looped
287 && ioctl(slave_fd
, PPPIOCXFERUNIT
, 0) < 0) {
288 error("ioctl(transfer ppp unit): %m");
293 if (new_style_driver
) {
294 /* Open another instance of /dev/ppp and connect the channel to it */
297 if (ioctl(fd
, PPPIOCGCHAN
, &chindex
) == -1) {
298 error("Couldn't get channel number: %m");
301 dbglog("using channel %d", chindex
);
302 fd
= open("/dev/ppp", O_RDWR
);
304 error("Couldn't reopen /dev/ppp: %m");
307 if (ioctl(fd
, PPPIOCATTCHAN
, &chindex
) < 0) {
308 error("Couldn't attach to channel %d: %m", chindex
);
311 flags
= fcntl(fd
, F_GETFL
);
312 if (flags
== -1 || fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
313 warn("Couldn't set /dev/ppp (channel) to nonblock: %m");
318 if (!looped
&& !multilink
) {
320 * Create a new PPP unit.
322 if (make_ppp_unit() < 0)
327 set_flags(ppp_dev_fd
, get_flags(ppp_dev_fd
) & ~SC_LOOP_TRAFFIC
);
331 if (ioctl(fd
, PPPIOCCONNECT
, &ifunit
) < 0) {
332 error("Couldn't attach to PPP unit %d: %m", ifunit
);
340 * Old-style driver: find out which interface we were given.
343 if (ioctl(fd
, PPPIOCGUNIT
, &x
) < 0) {
344 if (ok_error (errno
))
346 fatal("ioctl(PPPIOCGUNIT): %m(%d)", errno
);
348 /* Check that we got the same unit again. */
349 if (looped
&& x
!= ifunit
)
350 fatal("transfer_ppp failed: wanted unit %d, got %d", ifunit
, x
);
354 * Fetch the initial file flags and reset blocking mode on the file.
356 initfdflags
= fcntl(fd
, F_GETFL
);
357 if (initfdflags
== -1 ||
358 fcntl(fd
, F_SETFL
, initfdflags
| O_NONBLOCK
) == -1) {
359 if ( ! ok_error (errno
))
360 warn("Couldn't set device to non-blocking mode: %m");
366 * Enable debug in the driver if requested.
369 set_kdebugflag (kdebugflag
);
371 SYSDEBUG ((LOG_NOTICE
, "Using version %d.%d.%d of PPP driver",
372 driver_version
, driver_modification
, driver_patch
));
379 if (ioctl(fd
, TIOCSETD
, &tty_disc
) < 0 && !ok_error(errno
))
380 warn("Couldn't reset tty to normal line discipline: %m");
384 /********************************************************************
386 * generic_disestablish_ppp - Restore device components to normal
387 * operation, and reconnect the ppp unit to the loopback if in demand
388 * mode. This shouldn't call die() because it's called from die().
390 void generic_disestablish_ppp(int dev_fd
){
391 /* Restore loop if needed */
395 /* Finally detach the device */
398 if (new_style_driver
) {
401 if (!looped
&& ifunit
>= 0 && ioctl(ppp_dev_fd
, PPPIOCDETACH
) < 0)
402 error("Couldn't release PPP unit: %m");
404 remove_fd(ppp_dev_fd
);
409 * make_ppp_unit - make a new ppp unit for ppp_dev_fd.
410 * Assumes new_style_driver.
412 static int make_ppp_unit()
417 x
= ioctl(ppp_dev_fd
, PPPIOCNEWUNIT
, &ifunit
);
418 if (x
< 0 && req_unit
>= 0 && errno
== EEXIST
) {
419 warn("Couldn't allocate PPP unit %d as it is already in use");
421 x
= ioctl(ppp_dev_fd
, PPPIOCNEWUNIT
, &ifunit
);
424 error("Couldn't create new ppp unit: %m");
428 /********************************************************************
430 * clean_check - Fetch the flags for the device and generate
431 * appropriate error messages.
433 void clean_check(void)
439 if (ioctl(ppp_fd
, PPPIOCGFLAGS
, (caddr_t
) &x
) == 0) {
441 switch (~x
& (SC_RCV_B7_0
|SC_RCV_B7_1
|SC_RCV_EVNP
|SC_RCV_ODDP
)) {
443 s
= "all had bit 7 set to 1";
447 s
= "all had bit 7 set to 0";
451 s
= "all had odd parity";
455 s
= "all had even parity";
460 warn("Receive serial link is not 8-bit clean:");
461 warn("Problem: %s", s
);
467 /********************************************************************
469 * output - Output PPP packet.
472 void output (int unit
, unsigned char *p
, int len
)
478 dbglog("sent %P", p
, len
);
480 if (len
< PPP_HDRLEN
)
482 if (new_style_driver
) {
485 proto
= (p
[0] << 8) + p
[1];
486 if (ifunit
>= 0 && !(proto
>= 0xc000 || proto
== PPP_CCPFRAG
))
489 if (write(fd
, p
, len
) < 0) {
490 if (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
491 || errno
== ENXIO
|| errno
== EIO
|| errno
== EINTR
)
492 warn("write: warning: %m (%d)", errno
);
494 error("write: %m (%d)", errno
);
498 /********************************************************************
500 * wait_input - wait until there is data available,
501 * for the length of time specified by *timo (indefinite
505 void wait_input(struct timeval
*timo
)
512 n
= select(max_in_fd
+ 1, &ready
, NULL
, &exc
, timo
);
513 if (n
< 0 && errno
!= EINTR
)
514 fatal("select: %m(%d)", errno
);
518 * add_fd - add an fd to the set that wait_input waits for.
528 * remove_fd - remove an fd from the set that wait_input waits for.
530 void remove_fd(int fd
)
536 /********************************************************************
538 * read_packet - get a PPP packet from the serial device.
541 int read_packet (unsigned char *buf
)
545 len
= PPP_MRU
+ PPP_HDRLEN
;
546 if (new_style_driver
) {
547 *buf
++ = PPP_ALLSTATIONS
;
553 nr
= read(ppp_fd
, buf
, len
);
554 if (nr
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EIO
&& errno
!= EINTR
)
556 if (nr
< 0 && errno
== ENXIO
)
559 if (nr
< 0 && new_style_driver
&& ifunit
>= 0) {
560 /* N.B. we read ppp_fd first since LCP packets come in there. */
561 nr
= read(ppp_dev_fd
, buf
, len
);
562 if (nr
< 0 && errno
!= EWOULDBLOCK
&& errno
!= EIO
&& errno
!= EINTR
)
563 error("read /dev/ppp: %m");
564 if (nr
< 0 && errno
== ENXIO
)
567 return (new_style_driver
&& nr
> 0)? nr
+2: nr
;
570 /********************************************************************
572 * get_loop_output - get outgoing packets from the ppp device,
573 * and detect when we want to bring the real link up.
574 * Return value is 1 if we need to bring up the link, 0 otherwise.
577 get_loop_output(void)
582 if (new_style_driver
) {
583 while ((n
= read_packet(inpacket_buf
)) > 0)
584 if (loop_frame(inpacket_buf
, n
))
589 while ((n
= read(master_fd
, inbuf
, sizeof(inbuf
))) > 0)
590 if (loop_chars(inbuf
, n
))
594 fatal("eof on loopback");
596 if (errno
!= EWOULDBLOCK
)
597 fatal("read from loopback: %m(%d)", errno
);
603 * netif_set_mtu - set the MTU on the PPP network interface.
606 netif_set_mtu(int unit
, int mtu
)
610 SYSDEBUG ((LOG_DEBUG
, "netif_set_mtu: mtu = %d\n", mtu
));
612 memset (&ifr
, '\0', sizeof (ifr
));
613 strlcpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
616 if (ifunit
>= 0 && ioctl(sock_fd
, SIOCSIFMTU
, (caddr_t
) &ifr
) < 0)
617 fatal("ioctl(SIOCSIFMTU): %m");
620 /********************************************************************
622 * ccp_test - ask kernel whether a given compression method
623 * is acceptable for use.
626 int ccp_test (int unit
, u_char
*opt_ptr
, int opt_len
, int for_transmit
)
628 struct ppp_option_data data
;
630 memset (&data
, '\0', sizeof (data
));
632 data
.length
= opt_len
;
633 data
.transmit
= for_transmit
;
635 if (ioctl(ppp_dev_fd
, PPPIOCSCOMPRESS
, (caddr_t
) &data
) >= 0)
638 return (errno
== ENOBUFS
)? 0: -1;
641 /********************************************************************
643 * ccp_flags_set - inform kernel about the current state of CCP.
646 void ccp_flags_set (int unit
, int isopen
, int isup
)
649 int x
= get_flags(ppp_dev_fd
);
650 x
= isopen
? x
| SC_CCP_OPEN
: x
&~ SC_CCP_OPEN
;
651 x
= isup
? x
| SC_CCP_UP
: x
&~ SC_CCP_UP
;
652 set_flags (ppp_dev_fd
, x
);
656 /********************************************************************
658 * get_idle_time - return how long the link has been idle.
665 return ioctl(ppp_dev_fd
, PPPIOCGIDLE
, ip
) >= 0;
668 /********************************************************************
670 * get_ppp_stats - return statistics for the link.
673 get_ppp_stats(u
, stats
)
675 struct pppd_stats
*stats
;
677 struct ifpppstatsreq req
;
679 memset (&req
, 0, sizeof (req
));
681 req
.stats_ptr
= (caddr_t
) &req
.stats
;
682 strlcpy(req
.ifr__name
, ifname
, sizeof(req
.ifr__name
));
683 if (ioctl(sock_fd
, SIOCGPPPSTATS
, &req
) < 0) {
684 error("Couldn't get PPP statistics: %m");
687 stats
->bytes_in
= req
.stats
.p
.ppp_ibytes
;
688 stats
->bytes_out
= req
.stats
.p
.ppp_obytes
;
692 /********************************************************************
694 * ccp_fatal_error - returns 1 if decompression was disabled as a
695 * result of an error detected after decompression of a packet,
696 * 0 otherwise. This is necessary because of patent nonsense.
699 int ccp_fatal_error (int unit
)
701 int x
= get_flags(ppp_dev_fd
);
703 return x
& SC_DC_FERROR
;
706 /********************************************************************
708 * Return user specified netmask, modified by any mask we might determine
709 * for address `addr' (in network byte order).
710 * Here we scan through the system's list of interfaces, looking for
711 * any non-point-to-point interfaces which might appear to be on the same
712 * network as `addr'. If we find any, we OR in their netmask to the
713 * user-specified netmask.
716 u_int32_t
GetMask (u_int32_t addr
)
718 u_int32_t mask
, nmask
, ina
;
719 struct ifreq
*ifr
, *ifend
, ifreq
;
721 struct ifreq ifs
[MAX_IFS
];
725 if (IN_CLASSA(addr
)) /* determine network mask for address class */
726 nmask
= IN_CLASSA_NET
;
727 else if (IN_CLASSB(addr
))
728 nmask
= IN_CLASSB_NET
;
730 nmask
= IN_CLASSC_NET
;
732 /* class D nets are disallowed by bad_ip_adrs */
733 mask
= netmask
| htonl(nmask
);
735 * Scan through the system's network interfaces.
737 ifc
.ifc_len
= sizeof(ifs
);
739 if (ioctl(sock_fd
, SIOCGIFCONF
, &ifc
) < 0) {
740 if ( ! ok_error ( errno
))
741 warn("ioctl(SIOCGIFCONF): %m(%d)", errno
);
745 ifend
= (struct ifreq
*) (ifc
.ifc_buf
+ ifc
.ifc_len
);
746 for (ifr
= ifc
.ifc_req
; ifr
< ifend
; ifr
++) {
748 * Check the interface's internet address.
750 if (ifr
->ifr_addr
.sa_family
!= AF_INET
)
752 ina
= SIN_ADDR(ifr
->ifr_addr
);
753 if (((ntohl(ina
) ^ addr
) & nmask
) != 0)
756 * Check that the interface is up, and not point-to-point nor loopback.
758 strlcpy(ifreq
.ifr_name
, ifr
->ifr_name
, sizeof(ifreq
.ifr_name
));
759 if (ioctl(sock_fd
, SIOCGIFFLAGS
, &ifreq
) < 0)
762 if (((ifreq
.ifr_flags
^ FLAGS_GOOD
) & FLAGS_MASK
) != 0)
765 * Get its netmask and OR it into our mask.
767 if (ioctl(sock_fd
, SIOCGIFNETMASK
, &ifreq
) < 0)
769 mask
|= SIN_ADDR(ifreq
.ifr_addr
);
775 /********************************************************************
777 * ppp_available - check whether the system has any ppp interfaces
778 * (in fact we check whether we can do an ioctl on ppp0).
781 int ppp_available(void)
783 struct utsname utsname
; /* for the kernel version */
784 int osmaj
, osmin
, ospatch
;
786 /* get the kernel version now, since we are called before sys_init */
788 osmaj
= osmin
= ospatch
= 0;
789 sscanf(utsname
.release
, "%d.%d.%d", &osmaj
, &osmin
, &ospatch
);
790 kernel_version
= KVERSION(osmaj
, osmin
, ospatch
);
793 driver_modification
= 4;
799 /********************************************************************
801 * sifvjcomp - config tcp header compression
804 int sifvjcomp (int u
, int vjcomp
, int cidcomp
, int maxcid
)
806 u_int x
= get_flags(ppp_dev_fd
);
809 if (ioctl (ppp_dev_fd
, PPPIOCSMAXCID
, (caddr_t
) &maxcid
) < 0) {
810 if (! ok_error (errno
))
811 error("ioctl(PPPIOCSMAXCID): %m(%d)", errno
);
816 x
= vjcomp
? x
| SC_COMP_TCP
: x
&~ SC_COMP_TCP
;
817 x
= cidcomp
? x
& ~SC_NO_TCP_CCID
: x
| SC_NO_TCP_CCID
;
818 set_flags (ppp_dev_fd
, x
);
823 /********************************************************************
825 * sifup - Config the interface up and enable IP packets to pass.
832 memset (&ifr
, '\0', sizeof (ifr
));
833 strlcpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
834 if (ioctl(sock_fd
, SIOCGIFFLAGS
, (caddr_t
) &ifr
) < 0) {
835 if (! ok_error (errno
))
836 error("ioctl (SIOCGIFFLAGS): %m(%d)", errno
);
840 ifr
.ifr_flags
|= (IFF_UP
| IFF_POINTOPOINT
);
841 if (ioctl(sock_fd
, SIOCSIFFLAGS
, (caddr_t
) &ifr
) < 0) {
842 if (! ok_error (errno
))
843 error("ioctl(SIOCSIFFLAGS): %m(%d)", errno
);
851 /********************************************************************
853 * sifdown - Disable the indicated protocol and config the interface
854 * down if there are no remaining protocols.
861 if (if_is_up
&& --if_is_up
> 0)
864 memset (&ifr
, '\0', sizeof (ifr
));
865 strlcpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
866 if (ioctl(sock_fd
, SIOCGIFFLAGS
, (caddr_t
) &ifr
) < 0) {
867 if (! ok_error (errno
))
868 error("ioctl (SIOCGIFFLAGS): %m(%d)", errno
);
872 ifr
.ifr_flags
&= ~IFF_UP
;
873 ifr
.ifr_flags
|= IFF_POINTOPOINT
;
874 if (ioctl(sock_fd
, SIOCSIFFLAGS
, (caddr_t
) &ifr
) < 0) {
875 if (! ok_error (errno
))
876 error("ioctl(SIOCSIFFLAGS): %m(%d)", errno
);
882 /********************************************************************
884 * sifaddr - Config the interface IP addresses and netmask.
887 int sifaddr (int unit
, u_int32_t our_adr
, u_int32_t his_adr
,
893 memset (&ifr
, '\0', sizeof (ifr
));
894 memset (&rt
, '\0', sizeof (rt
));
896 SET_SA_FAMILY (ifr
.ifr_addr
, AF_INET
);
897 SET_SA_FAMILY (ifr
.ifr_dstaddr
, AF_INET
);
898 SET_SA_FAMILY (ifr
.ifr_netmask
, AF_INET
);
900 strlcpy (ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
904 SIN_ADDR(ifr
.ifr_addr
) = our_adr
;
905 if (ioctl(sock_fd
, SIOCSIFADDR
, (caddr_t
) &ifr
) < 0) {
906 if (errno
!= EEXIST
) {
907 if (! ok_error (errno
))
908 error("ioctl(SIOCSIFADDR): %m(%d)", errno
);
911 warn("ioctl(SIOCSIFADDR): Address already exists");
916 * Set the gateway address
918 SIN_ADDR(ifr
.ifr_dstaddr
) = his_adr
;
919 if (ioctl(sock_fd
, SIOCSIFDSTADDR
, (caddr_t
) &ifr
) < 0) {
920 if (! ok_error (errno
))
921 error("ioctl(SIOCSIFDSTADDR): %m(%d)", errno
);
926 * For recent kernels, force the netmask to 255.255.255.255.
928 if (kernel_version
>= KVERSION(2,1,16))
931 SIN_ADDR(ifr
.ifr_netmask
) = net_mask
;
932 if (ioctl(sock_fd
, SIOCSIFNETMASK
, (caddr_t
) &ifr
) < 0) {
933 if (! ok_error (errno
))
934 error("ioctl(SIOCSIFNETMASK): %m(%d)", errno
);
939 * Add the device route
941 if (kernel_version
< KVERSION(2,1,16)) {
942 SET_SA_FAMILY (rt
.rt_dst
, AF_INET
);
943 SET_SA_FAMILY (rt
.rt_gateway
, AF_INET
);
946 SIN_ADDR(rt
.rt_gateway
) = 0L;
947 SIN_ADDR(rt
.rt_dst
) = his_adr
;
948 rt
.rt_flags
= RTF_UP
| RTF_HOST
;
950 if (kernel_version
> KVERSION(2,1,0)) {
951 SET_SA_FAMILY (rt
.rt_genmask
, AF_INET
);
952 SIN_ADDR(rt
.rt_genmask
) = -1L;
955 if (ioctl(sock_fd
, SIOCADDRT
, &rt
) < 0) {
956 if (! ok_error (errno
))
957 error("ioctl(SIOCADDRT) device route: %m(%d)", errno
);
962 /* set ip_dynaddr in demand mode if address changes */
963 if (demand
&& tune_kernel
&& !dynaddr_set
964 && our_old_addr
&& our_old_addr
!= our_adr
) {
965 /* set ip_dynaddr if possible */
969 path
= "/proc/sys/net/ipv4/ip_dynaddr";
970 if (path
!= 0 && (fd
= open(path
, O_WRONLY
)) >= 0) {
971 if (write(fd
, "1", 1) != 1)
972 error("Couldn't enable dynamic IP addressing: %m");
975 dynaddr_set
= 1; /* only 1 attempt */
982 /********************************************************************
984 * cifaddr - Clear the interface IP addresses, and delete routes
985 * through the interface if possible.
988 int cifaddr (int unit
, u_int32_t our_adr
, u_int32_t his_adr
)
992 if (kernel_version
< KVERSION(2,1,16)) {
994 * Delete the route through the device
997 memset (&rt
, '\0', sizeof (rt
));
999 SET_SA_FAMILY (rt
.rt_dst
, AF_INET
);
1000 SET_SA_FAMILY (rt
.rt_gateway
, AF_INET
);
1003 SIN_ADDR(rt
.rt_gateway
) = 0;
1004 SIN_ADDR(rt
.rt_dst
) = his_adr
;
1005 rt
.rt_flags
= RTF_UP
| RTF_HOST
;
1007 if (kernel_version
> KVERSION(2,1,0)) {
1008 SET_SA_FAMILY (rt
.rt_genmask
, AF_INET
);
1009 SIN_ADDR(rt
.rt_genmask
) = -1L;
1012 if (ioctl(sock_fd
, SIOCDELRT
, &rt
) < 0 && errno
!= ESRCH
) {
1013 if (still_ppp() && ! ok_error (errno
))
1014 error("ioctl(SIOCDELRT) device route: %m(%d)", errno
);
1019 /* This way it is possible to have an IPX-only or IPv6-only interface */
1020 memset(&ifr
, 0, sizeof(ifr
));
1021 SET_SA_FAMILY(ifr
.ifr_addr
, AF_INET
);
1022 strlcpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
1024 if (ioctl(sock_fd
, SIOCSIFADDR
, (caddr_t
) &ifr
) < 0) {
1025 if (! ok_error (errno
)) {
1026 error("ioctl(SIOCSIFADDR): %m(%d)", errno
);
1031 our_old_addr
= our_adr
;
1036 //===================================================================
1040 * /proc/net/route parsing stuff.
1043 #define ROUTE_MAX_COLS 12
1044 FILE *route_fd
= (FILE *) 0;
1045 static char route_buffer
[512];
1046 static int route_dev_col
, route_dest_col
, route_gw_col
;
1047 static int route_flags_col
, route_mask_col
;
1048 static int route_num_cols
;
1050 static int open_route_table (void);
1051 static void close_route_table (void);
1052 static int read_route_table (struct rtentry
*rt
);
1053 static char route_delims
[] = " \t\n";
1055 /********************************************************************
1057 * close_route_table - close the interface to the route table
1059 // copy from pppd/sys-linux.c by tallest
1060 static void close_route_table (void)
1062 if (route_fd
!= (FILE *) 0) {
1064 route_fd
= (FILE *) 0;
1068 /********************************************************************
1070 * read_route_table - read the next entry from the route table
1072 // copy from pppd/sys-linux.c by tallest
1073 static int read_route_table(struct rtentry
*rt
)
1075 char *cols
[ROUTE_MAX_COLS
], *p
;
1078 memset (rt
, '\0', sizeof (struct rtentry
));
1080 if (fgets (route_buffer
, sizeof (route_buffer
), route_fd
) == (char *) 0)
1084 for (col
= 0; col
< route_num_cols
; ++col
) {
1085 cols
[col
] = strtok(p
, route_delims
);
1086 if (cols
[col
] == NULL
)
1087 return 0; /* didn't get enough columns */
1091 SIN_ADDR(rt
->rt_dst
) = strtoul(cols
[route_dest_col
], NULL
, 16);
1092 SIN_ADDR(rt
->rt_gateway
) = strtoul(cols
[route_gw_col
], NULL
, 16);
1093 SIN_ADDR(rt
->rt_genmask
) = strtoul(cols
[route_mask_col
], NULL
, 16);
1095 rt
->rt_flags
= (short) strtoul(cols
[route_flags_col
], NULL
, 16);
1096 rt
->rt_dev
= cols
[route_dev_col
];
1101 /********************************************************************
1103 * open_route_table - open the interface to the route table
1105 // copy from pppd/sys-linux.c by tallest
1107 static int open_route_table (void)
1109 close_route_table();
1111 route_fd
= fopen ("proc/net/route", "r");
1112 if (route_fd
== NULL
) {
1113 error("can't open routing table");
1117 route_dev_col
= 0; /* default to usual columns */
1120 route_flags_col
= 3;
1124 /* parse header line */
1125 if (fgets(route_buffer
, sizeof(route_buffer
), route_fd
) != 0) {
1126 char *p
= route_buffer
, *q
;
1128 for (col
= 0; col
< ROUTE_MAX_COLS
; ++col
) {
1130 if ((q
= strtok(p
, route_delims
)) == 0)
1132 if (strcasecmp(q
, "iface") == 0)
1133 route_dev_col
= col
;
1134 else if (strcasecmp(q
, "destination") == 0)
1135 route_dest_col
= col
;
1136 else if (strcasecmp(q
, "gateway") == 0)
1138 else if (strcasecmp(q
, "flags") == 0)
1139 route_mask_col
= col
;
1142 if (used
&& col
>= route_num_cols
)
1143 route_num_cols
= col
+ 1;
1151 /********************************************************************
1153 * defaultroute_exists - determine if there is a default route
1155 // copy from pppd/sys-linux.c by tallest
1156 static int defaultroute_exists (struct rtentry
*rt
)
1160 if (!open_route_table())
1163 while (read_route_table(rt
) != 0) {
1164 if ((rt
->rt_flags
& RTF_UP
) == 0)
1167 if (kernel_version
> KVERSION(2,1,0) && SIN_ADDR(rt
->rt_genmask
) != 0)
1169 if (SIN_ADDR(rt
->rt_dst
) == 0L) {
1175 close_route_table();
1179 /********************************************************************
1181 * sifdefaultroute - assign a default route through the address given.
1183 // copy from pppd/sys_linux.c by tallest
1185 sifdefaultroute (int unit
, u_int32_t ouraddr
, u_int32_t gateway
)
1189 if (defaultroute_exists(&rt
) && strcmp(rt
.rt_dev
, ifname
) != 0) {
1190 u_int32_t old_gateway
= SIN_ADDR(rt
.rt_gateway
);
1192 if (old_gateway
!= gateway
)
1193 error("not replacing existing default route to %s [%I]",
1194 rt
.rt_dev
, old_gateway
);
1198 memset (&rt
, '\0', sizeof (rt
));
1199 SET_SA_FAMILY (rt
.rt_dst
, AF_INET
);
1200 SET_SA_FAMILY (rt
.rt_gateway
, AF_INET
);
1202 if (kernel_version
> KVERSION(2,1,0)) {
1203 SET_SA_FAMILY (rt
.rt_genmask
, AF_INET
);
1204 SIN_ADDR(rt
.rt_genmask
) = 0L;
1207 SIN_ADDR(rt
.rt_gateway
) = gateway
;
1209 rt
.rt_flags
= RTF_UP
| RTF_GATEWAY
;
1210 if (ioctl(sock_fd
, SIOCADDRT
, &rt
) < 0) {
1211 if ( ! ok_error ( errno
))
1212 error("default route ioctl(SIOCADDRT): %m(%d)", errno
);
1216 //default_route_gateway = gateway;
1220 //===================================================================
1223 /********************************************************************
1225 * open_loopback - open the device we use for getting packets
1226 * in demand mode. Under Linux, we use a pty master/slave pair.
1229 open_ppp_loopback(void)
1234 if (new_style_driver
) {
1235 /* allocate ourselves a ppp unit */
1236 if (make_ppp_unit() < 0)
1238 set_flags(ppp_dev_fd
, SC_LOOP_TRAFFIC
);
1239 set_kdebugflag(kdebugflag
);
1244 if (!get_pty(&master_fd
, &slave_fd
, loop_name
, 0))
1245 fatal("No free pty for loopback");
1246 SYSDEBUG(("using %s for loopback", loop_name
));
1248 set_ppp_fd(slave_fd
);
1250 flags
= fcntl(master_fd
, F_GETFL
);
1252 fcntl(master_fd
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
1253 warn("couldn't set master loopback to nonblock: %m(%d)", errno
);
1255 flags
= fcntl(ppp_fd
, F_GETFL
);
1257 fcntl(ppp_fd
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
1258 warn("couldn't set slave loopback to nonblock: %m(%d)", errno
);
1260 if (ioctl(ppp_fd
, TIOCSETD
, &ppp_disc
) < 0)
1261 fatal("ioctl(TIOCSETD): %m(%d)", errno
);
1263 * Find out which interface we were given.
1265 if (ioctl(ppp_fd
, PPPIOCGUNIT
, &ifunit
) < 0)
1266 fatal("ioctl(PPPIOCGUNIT): %m(%d)", errno
);
1268 * Enable debug in the driver if requested.
1270 set_kdebugflag (kdebugflag
);
1275 /********************************************************************
1277 * restore_loop - reattach the ppp unit to the loopback.
1279 * The kernel ppp driver automatically reattaches the ppp unit to
1280 * the loopback if the serial port is set to a line discipline other
1281 * than ppp, or if it detects a modem hangup. The former will happen
1282 * in disestablish_ppp if the latter hasn't already happened, so we
1283 * shouldn't need to do anything.
1285 * Just to be sure, set the real serial port to the normal discipline.
1292 if (new_style_driver
) {
1293 set_flags(ppp_dev_fd
, get_flags(ppp_dev_fd
) | SC_LOOP_TRAFFIC
);
1296 if (ppp_fd
!= slave_fd
) {
1297 (void) ioctl(ppp_fd
, TIOCSETD
, &tty_disc
);
1298 set_ppp_fd(slave_fd
);
1302 /********************************************************************
1304 * sifnpmode - Set the mode for handling packets for a given NP.
1308 sifnpmode(u
, proto
, mode
)
1315 npi
.protocol
= proto
;
1317 if (ioctl(ppp_dev_fd
, PPPIOCSNPMODE
, (caddr_t
) &npi
) < 0) {
1318 if (! ok_error (errno
))
1319 error("ioctl(PPPIOCSNPMODE, %d, %d): %m (%d)",
1320 proto
, mode
, errno
);
1327 * Use the hostname as part of the random number seed.
1336 for (p
= hostname
; *p
!= 0; ++p
)