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.
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
29 #include <sys/errno.h>
43 #include <net/ppp_defs.h>
44 #include <net/if_arp.h>
45 #include <net/if_ppp.h>
46 #include <net/if_route.h>
47 #include <linux/if_ether.h>
48 #include <netinet/in.h>
54 static int initdisc = -1; /* Initial TTY discipline */
55 static int prev_kdebugflag = 0;
57 static int restore_term; /* 1 => we've munged the terminal */
58 static struct termios inittermios; /* Initial TTY termios */
60 int sockfd; /* socket for doing interface ioctls */
62 static int driver_version = 0;
63 static int driver_modification = 0;
64 static int driver_patch = 0;
66 static char *lock_file;
71 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
75 #define SET_SA_FAMILY(addr, family) \
76 memset ((char *) &(addr), '\0', sizeof(addr)); \
77 addr.sa_family = (family);
80 * sys_init - System-dependent initialization.
85 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
86 setlogmask(LOG_UPTO(LOG_INFO));
88 setlogmask(LOG_UPTO(LOG_DEBUG));
90 /* Get an internet socket for doing socket ioctl's on. */
91 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
92 syslog(LOG_ERR, "Couldn't create IP socket: %m");
98 * note_debug_level - note a change in the debug level.
100 void note_debug_level (void)
103 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
104 setlogmask(LOG_UPTO(LOG_DEBUG));
106 setlogmask(LOG_UPTO(LOG_WARNING));
111 * set_kdebugflag - Define the debugging level for the kernel
114 int set_kdebugflag (int requested_level)
116 if (ioctl(fd, PPPIOCGDEBUG, &prev_kdebugflag) < 0) {
117 syslog(LOG_ERR, "ioctl(PPPIOCGDEBUG): %m");
121 if (prev_kdebugflag != requested_level) {
122 if (ioctl(fd, PPPIOCSDEBUG, &requested_level) < 0) {
123 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
126 syslog(LOG_INFO, "set kernel debugging level to %d", requested_level);
132 * establish_ppp - Turn the serial port into a ppp interface.
135 void establish_ppp (void)
140 if (ioctl(fd, TIOCEXCL, 0) < 0) {
141 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
144 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
145 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
149 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
150 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
153 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
154 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
158 set_kdebugflag (kdebugflag);
160 syslog (LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
161 driver_version, driver_modification, driver_patch);
165 * disestablish_ppp - Restore the serial port to normal operation.
166 * This shouldn't call die() because it's called from die().
169 void disestablish_ppp(void)
175 set_kdebugflag (prev_kdebugflag);
177 * Check whether the link seems not to be 8-bit clean.
179 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
181 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
183 s = "bit 7 set to 1";
186 s = "bit 7 set to 0";
196 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
197 syslog(LOG_WARNING, "All received characters had %s", s);
201 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
202 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
204 if (ioctl(fd, TIOCNXCL, 0) < 0)
205 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m");
212 * List of valid speeds.
215 int speed_int, speed_val;
287 * Translate from bits/second to a speed_t.
289 int translate_speed (int bps)
291 struct speed *speedp;
295 for (speedp = speeds; speedp->speed_int; speedp++)
296 if (bps == speedp->speed_int)
297 return speedp->speed_val;
298 syslog(LOG_WARNING, "speed %d not supported", bps);
303 * Translate from a speed_t to bits/second.
305 int baud_rate_of (int speed)
307 struct speed *speedp;
311 for (speedp = speeds; speedp->speed_int; speedp++)
312 if (speed == speedp->speed_val)
313 return speedp->speed_int;
318 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
319 * at the requested speed, etc. If `local' is true, set CLOCAL
320 * regardless of whether the modem option was specified.
322 void set_up_tty (int fd, int local)
327 if (tcgetattr(fd, &tios) < 0) {
328 syslog(LOG_ERR, "tcgetattr: %m");
335 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
337 tios.c_cflag |= CRTSCTS;
338 else if (crtscts < 0)
339 tios.c_cflag &= ~CRTSCTS;
341 tios.c_cflag |= CS8 | CREAD | HUPCL;
344 tios.c_cflag |= CLOCAL;
346 tios.c_iflag = IGNBRK | IGNPAR;
350 tios.c_cc[VTIME] = 0;
353 tios.c_iflag |= IXOFF;
354 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
355 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
358 speed = translate_speed(inspeed);
360 cfsetospeed(&tios, speed);
361 cfsetispeed(&tios, speed);
363 speed = cfgetospeed(&tios);
365 * We can't proceed if the serial port speed is B0,
366 * since that implies that the serial port is disabled.
369 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
375 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
376 syslog(LOG_ERR, "tcsetattr: %m");
380 baud_rate = baud_rate_of(speed);
385 * setdtr - control the DTR line on the serial port.
386 * This is called from die(), so it shouldn't call die().
388 void setdtr (int fd, int on)
390 int modembits = TIOCM_DTR;
392 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
396 * restore_tty - restore the terminal to the saved settings.
399 void restore_tty (void)
402 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
404 syslog(LOG_WARNING, "tcsetattr: %m");
410 * output - Output PPP packet.
413 void output (int unit, unsigned char *p, int len)
416 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
419 log_packet(p, len, "sent ");
421 if (write(fd, p, len) < 0) {
422 syslog(LOG_ERR, "write: %m");
428 * wait_input - wait until there is data available on fd,
429 * for the length of time specified by *timo (indefinite
432 void wait_input (struct timeval *timo)
439 n = select(fd+1, &ready, NULL, &ready, timo);
440 if (n < 0 && errno != EINTR) {
441 syslog(LOG_ERR, "select: %m");
447 * read_packet - get a PPP packet from the serial device.
450 int read_packet (unsigned char *buf)
454 len = read(fd, buf, PPP_MTU + PPP_HDRLEN);
456 if (errno == EWOULDBLOCK) {
459 syslog(LOG_ERR, "read(fd): %m");
466 * ppp_send_config - configure the transmit characteristics of
469 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
474 MAINDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
475 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
477 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
478 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
482 MAINDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
483 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
484 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
488 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
489 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
493 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
494 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
496 MAINDEBUG ((LOG_DEBUG, "send_config: flags = %x\n", x));
497 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
498 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
504 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
506 void ppp_set_xaccm (int unit, ext_accm accm)
508 MAINDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
509 accm[0], accm[1], accm[2], accm[3]));
510 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
511 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
515 * ppp_recv_config - configure the receive-side characteristics of
518 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
522 MAINDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
523 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
524 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
526 MAINDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
527 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
528 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
532 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
533 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
537 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
538 MAINDEBUG ((LOG_DEBUG, "recv_config: flags = %x\n", x));
539 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
540 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
546 * ccp_test - ask kernel whether a given compression method
547 * is acceptable for use.
549 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
551 struct ppp_option_data data;
554 data.length = opt_len;
555 data.transmit = for_transmit;
557 return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
561 * ccp_flags_set - inform kernel about the current state of CCP.
564 void ccp_flags_set (int unit, int isopen, int isup)
568 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
570 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
574 x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
575 x = isup? x | SC_CCP_UP : x &~ SC_CCP_UP;
577 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
579 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
585 * ccp_fatal_error - returns 1 if decompression was disabled as a
586 * result of an error detected after decompression of a packet,
587 * 0 otherwise. This is necessary because of patent nonsense.
590 int ccp_fatal_error (int unit)
594 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
596 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
599 return x & SC_DC_FERROR;
603 * sifvjcomp - config tcp header compression
606 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
610 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
612 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
616 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
617 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
619 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
621 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
627 if (ioctl (fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0)
629 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
638 * sifup - Config the interface up and enable IP packets to pass.
645 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
646 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
648 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
652 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
653 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
655 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
662 * sifdown - Config the interface down and disable IP.
669 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
670 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
672 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
676 ifr.ifr_flags &= ~IFF_UP;
677 ifr.ifr_flags |= IFF_POINTOPOINT;
678 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
680 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
687 * sifaddr - Config the interface IP addresses and netmask.
690 int sifaddr (int unit, int our_adr, int his_adr, int net_mask)
695 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
696 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
697 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
699 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
703 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
704 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
708 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m");
712 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
717 * Set the gateway address
719 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
720 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0)
722 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
730 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
731 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0)
733 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
738 * Add the device route
740 memset (&rt, '\0', sizeof (rt));
742 SET_SA_FAMILY (rt.rt_dst, AF_INET);
743 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
744 rt.rt_dev = ifname; /* MJC */
746 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
747 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
748 rt.rt_flags = RTF_UP | RTF_HOST;
750 if (ioctl(sockfd, SIOCADDRT, &rt) < 0)
752 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m");
759 * cifaddr - Clear the interface IP addresses, and delete routes
760 * through the interface if possible.
763 int cifaddr (int unit, int our_adr, int his_adr)
767 * Delete the route through the device
769 memset (&rt, '\0', sizeof (rt));
771 SET_SA_FAMILY (rt.rt_dst, AF_INET);
772 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
773 rt.rt_dev = ifname; /* MJC */
775 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
776 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
777 rt.rt_flags = RTF_UP | RTF_HOST;
779 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
781 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m");
788 * path_to_route - determine the path to the proc file system data
791 FILE *route_fd = (FILE *) 0;
792 static char route_buffer [100];
794 static char *path_to_route (void);
795 static int open_route_table (void);
796 static void close_route_table (void);
797 static int read_route_table (struct rtentry *rt);
798 static int defaultroute_exists (void);
801 * path_to_route - find the path to the route tables in the proc file system
804 static char *path_to_route (void)
806 struct mntent *mntent;
809 fp = fopen (MOUNTED, "r");
812 while ((mntent = getmntent (fp)) != 0)
814 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) == 0)
819 if (strcmp (mntent->mnt_type, "proc") == 0)
821 strncpy (route_buffer, mntent->mnt_dir,
822 sizeof (route_buffer)-10);
823 route_buffer [sizeof (route_buffer)-10] = '\0';
824 strcat (route_buffer, "/net/route");
827 return (route_buffer);
832 syslog (LOG_ERR, "proc file system not mounted");
837 * open_route_table - open the interface to the route table
840 static int open_route_table (void)
844 if (route_fd != (FILE *) 0)
849 path = path_to_route();
855 route_fd = fopen (path, "r");
856 if (route_fd == (FILE *) 0)
858 syslog (LOG_ERR, "can not open %s: %m", path);
862 /* read and discard the header line. */
863 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
872 * close_route_table - close the interface to the route table
875 static void close_route_table (void)
877 if (route_fd != (FILE *) 0)
880 route_fd = (FILE *) 0;
885 * read_route_table - read the next entry from the route table
888 static int read_route_table (struct rtentry *rt)
890 static char delims[] = " \t\n";
891 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
893 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
898 memset (rt, '\0', sizeof (struct rtentry));
900 dev_ptr = strtok (route_buffer, delims); /* interface name */
901 dst_ptr = strtok (NULL, delims); /* destination address */
902 gw_ptr = strtok (NULL, delims); /* gateway */
903 flag_ptr = strtok (NULL, delims); /* flags */
905 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
906 strtoul (dst_ptr, NULL, 16);
908 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
909 strtoul (gw_ptr, NULL, 16);
911 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
912 rt->rt_dev = dev_ptr;
918 * defaultroute_exists - determine if there is a default route
921 static int defaultroute_exists (void)
926 if (!open_route_table())
931 while (read_route_table(&rt) != 0)
933 if (rt.rt_flags & RTF_UP == 0)
938 if (((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr == 0L)
941 "ppp not replacing existing default route to %s[%s]",
943 inet_ntoa (((struct sockaddr_in *) &rt.rt_gateway)->
954 * sifdefaultroute - assign a default route through the address given.
957 int sifdefaultroute (int unit, int gateway)
961 if (defaultroute_exists())
966 memset (&rt, '\0', sizeof (rt));
967 SET_SA_FAMILY (rt.rt_dst, AF_INET);
968 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
969 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
971 rt.rt_flags = RTF_UP | RTF_GATEWAY;
972 if (ioctl(sockfd, SIOCADDRT, &rt) < 0)
974 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m");
981 * cifdefaultroute - delete a default route through the address given.
984 int cifdefaultroute (int unit, int gateway)
988 SET_SA_FAMILY (rt.rt_dst, AF_INET);
989 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
990 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
992 rt.rt_flags = RTF_UP | RTF_GATEWAY;
993 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
995 syslog (LOG_ERR, "default route ioctl(SIOCDELRT): %m");
1002 * sifproxyarp - Make a proxy ARP entry for the peer.
1005 int sifproxyarp (int unit, u_int32_t his_adr)
1007 struct arpreq arpreq;
1009 memset (&arpreq, '\0', sizeof(arpreq));
1011 * Get the hardware address of an interface on the same subnet
1012 * as our local address.
1014 if (!get_ether_addr(his_adr, &arpreq.arp_ha))
1016 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1020 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1021 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1022 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1024 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0)
1026 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1033 * cifproxyarp - Delete the proxy ARP entry for the peer.
1036 int cifproxyarp (int unit, u_int32_t his_adr)
1038 struct arpreq arpreq;
1040 memset (&arpreq, '\0', sizeof(arpreq));
1041 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1043 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1044 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0)
1046 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
1053 * get_ether_addr - get the hardware address of an interface on the
1054 * the same subnet as ipaddr.
1057 int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr)
1059 struct ifreq *ifr, *ifend, *ifp;
1061 u_int32_t ina, mask;
1062 struct sockaddr_dl *dla;
1065 struct ifreq ifs[MAX_IFS];
1067 ifc.ifc_len = sizeof(ifs);
1069 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
1071 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1074 MAINDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1075 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1077 * Scan through looking for an interface with an Internet
1078 * address on the same subnet as `ipaddr'.
1080 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1081 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1083 if (ifr->ifr_addr.sa_family == AF_INET)
1085 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1086 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1087 MAINDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1090 * Check that the interface is up, and not point-to-point
1093 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1097 if ((ifreq.ifr_flags &
1098 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1099 != (IFF_UP|IFF_BROADCAST))
1104 * Get its netmask and check that it's on the right subnet.
1106 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1110 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1111 MAINDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1112 ip_ntoa(ina), ntohl(mask)));
1113 if (((ipaddr ^ ina) & mask) != 0)
1126 syslog(LOG_INFO, "found interface %s for proxy arp", ifreq.ifr_name);
1128 * Now get the hardware address.
1130 if (ioctl (sockfd, SIOCGIFHWADDR, &ifreq) < 0)
1132 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m", ifreq.ifr_name);
1136 hwaddr->sa_family = ARPHRD_ETHER;
1137 memcpy (&hwaddr->sa_data, &ifreq.ifr_hwaddr.sa_data, ETH_ALEN);
1139 MAINDEBUG ((LOG_DEBUG,
1140 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x",
1141 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1142 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1143 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1144 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1145 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1146 (int) ((unsigned char *) &hwaddr->sa_data)[5]));
1151 * Internal routine to decode the version.modification.patch level
1154 static void decode_version (char *buf, int *version,
1155 int *modification, int *patch)
1158 *version = (int) strtoul (buf, &buf, 10);
1165 *modification = (int) strtoul (buf, &buf, 10);
1169 *patch = (int) strtoul (buf, &buf, 10);
1182 * ppp_available - check whether the system has any ppp interfaces
1183 * (in fact we check whether we can do an ioctl on ppp0).
1186 int ppp_available(void)
1190 int my_version, my_modification, my_patch;
1192 * Open a socket for doing the ioctl operations.
1194 s = socket(AF_INET, SOCK_DGRAM, 0);
1200 strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1201 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1203 * Ensure that the hardware address is for PPP and not something else
1207 ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1210 if (ok != 0 && ifr.ifr_hwaddr.sa_family != ARPHRD_PPP)
1215 * This is the PPP device. Validate the version of the driver at this
1216 * point to ensure that this program will work with the driver.
1220 char abBuffer [1024];
1223 ifr.ifr_data = abBuffer;
1224 size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1229 decode_version (abBuffer,
1231 &driver_modification,
1239 driver_modification =
1243 * Validate the version of the driver against the version that we used.
1245 decode_version (PPP_VERSION,
1250 /* The version numbers must match */
1251 if (driver_version != my_version)
1256 /* The modification levels must be legal */
1257 if (driver_modification < my_modification)
1264 fprintf(stderr, "Sorry - PPP driver version %d.%d.%d is out of date\n",
1265 driver_version, driver_modification, driver_patch);
1272 int logwtmp (char *line, char *name, char *host)
1277 * Control the 'mesg' function based upon the state of the logon
1280 mode = (*name != '\0') ? 0600 : 0622;
1281 if (chmod (devnam, mode) < 0)
1283 syslog (LOG_ERR, "chmod(\"%s\", 0%o): %m", devnam, mode);
1286 * Update the signon database for users.
1288 memset (&ut, 0, sizeof (ut));
1289 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1290 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1291 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1292 (void)time(&ut.ut_time);
1294 pututline (&ut); /* Write the line to the proper place */
1295 endutent(); /* Indicate operation is complete */
1299 * Code for locking/unlocking the serial device.
1300 * This code is derived from chat.c.
1303 #define LOCK_PREFIX "/var/lock/LCK.."
1306 * lock - create a lock file for the named device
1312 char hdb_lock_buffer[12];
1316 if ((p = strrchr(dev, '/')) != NULL)
1318 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1319 if (lock_file == NULL)
1320 novm("lock file name");
1321 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1323 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1325 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1326 /* Read the lock file to find out who has the device locked */
1327 n = read(fd, hdb_lock_buffer, 11);
1329 hdb_lock_buffer[n] = 0;
1330 sscanf (hdb_lock_buffer, " %d", &pid);
1333 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1336 if (kill(pid, 0) == -1 && errno == ESRCH) {
1337 /* pid no longer exists - remove the lock file */
1338 if (unlink(lock_file) == 0) {
1340 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1344 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1347 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1352 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1358 sprintf(hdb_lock_buffer, "%010d\n", getpid());
1359 write(fd, hdb_lock_buffer, 11);
1366 * unlock - remove our lockfile