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.
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <sys/errno.h>
39 #include <linux/ppp.h>
40 #include <linux/route.h>
41 #include <linux/if_ether.h>
42 #include <netinet/in.h>
43 #include <net/if_ppp.h>
50 static int initdisc = -1; /* Initial TTY discipline */
51 static int prev_kdebugflag = 0;
52 extern int kdebugflag;
53 extern u_long netmask;
58 void die __ARGS((int));
61 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
65 #define SET_SA_FAMILY(addr, family) \
66 memset ((char *) &(addr), '\0', sizeof(addr)); \
67 addr.sa_family = (family);
70 * set_kdebugflag - Define the debugging level for the kernel
73 int set_kdebugflag (int requested_level)
75 if (ioctl(fd, PPPIOCGDEBUG, &prev_kdebugflag) < 0) {
76 syslog(LOG_ERR, "ioctl(PPPIOCGDEBUG): %m");
80 if (prev_kdebugflag != requested_level) {
81 if (ioctl(fd, PPPIOCSDEBUG, &requested_level) < 0) {
82 syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
85 syslog(LOG_INFO, "set kernel debugging level to %d", requested_level);
91 * establish_ppp - Turn the serial port into a ppp interface.
94 void establish_ppp (void)
99 if (ioctl(fd, TIOCEXCL, 0) < 0) {
100 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
103 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
104 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
108 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
109 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
113 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
114 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
118 set_kdebugflag (kdebugflag);
121 * Set the device to give us a SIGIO when data is available.
123 if (ioctl(fd, PPPIOCSINPSIG, &sig) == -1) {
124 syslog(LOG_ERR, "ioctl(PPPIOCSINPSIG): %m");
130 * disestablish_ppp - Restore the serial port to normal operation.
131 * This shouldn't call die() because it's called from die().
134 void disestablish_ppp(void)
140 set_kdebugflag (prev_kdebugflag);
142 * Check whether the link seems not to be 8-bit clean.
144 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
146 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
148 s = "bit 7 set to 1";
151 s = "bit 7 set to 0";
161 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
162 syslog(LOG_WARNING, "All received characters had %s", s);
166 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
167 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
169 if (ioctl(fd, TIOCNXCL, 0) < 0)
170 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m");
177 * output - Output PPP packet.
180 void output (int unit, unsigned char *p, int len)
183 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
186 log_packet(p, len, "sent ");
188 if (write(fd, p, len) < 0) {
189 syslog(LOG_ERR, "write: %m");
195 * read_packet - get a PPP packet from the serial device.
198 int read_packet (unsigned char *buf)
202 len = read(fd, buf, MTU + DLLHEADERLEN);
204 if (errno == EWOULDBLOCK) {
206 MAINDEBUG((LOG_DEBUG, "read(fd): EWOULDBLOCK"));
210 syslog(LOG_ERR, "read(fd): %m");
217 * ppp_send_config - configure the transmit characteristics of
220 void ppp_send_config (int unit,int mtu,u_long asyncmap,int pcomp,int accomp)
225 MAINDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
226 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
228 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
229 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
233 MAINDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
234 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
235 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
239 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
240 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
244 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
245 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
247 MAINDEBUG ((LOG_DEBUG, "send_config: flags = %x\n", x));
248 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
249 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
255 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
258 ppp_set_xaccm(unit, accm)
262 MAINDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
263 accm[0], accm[1], accm[2], accm[3]));
264 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
265 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
269 * ppp_recv_config - configure the receive-side characteristics of
272 void ppp_recv_config (int unit,int mru,u_long asyncmap,int pcomp,int accomp)
276 MAINDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
277 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
278 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
280 MAINDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
281 if (ioctl(fd, PPPIOCRASYNCMAP, (caddr_t) &asyncmap) < 0) {
282 syslog(LOG_ERR, "ioctl(PPPIOCRASYNCMAP): %m");
286 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
287 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
291 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
292 MAINDEBUG ((LOG_DEBUG, "recv_config: flags = %x\n", x));
293 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
294 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
300 * sifvjcomp - config tcp header compression
303 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
307 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
308 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
312 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
313 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
315 if(ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
316 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
321 if (ioctl (fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
322 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
331 * sifup - Config the interface up and enable IP packets to pass.
338 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
339 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
340 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
344 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
345 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
346 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
353 * sifdown - Config the interface down and disable IP.
360 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
361 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
362 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
366 ifr.ifr_flags &= ~IFF_UP;
367 ifr.ifr_flags |= IFF_POINTOPOINT;
368 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
369 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
376 * sifaddr - Config the interface IP addresses and netmask.
379 int sifaddr (int unit, int our_adr, int his_adr, int net_mask)
384 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
385 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
386 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
388 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
392 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
393 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
395 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m");
397 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
401 * Set the gateway address
403 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
404 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
405 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
412 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
413 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
414 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
419 * Add the device route
421 memset (&rt, '\0', sizeof (rt));
423 SET_SA_FAMILY (rt.rt_dst, AF_INET);
424 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
425 rt.rt_dev = ifname; /* MJC */
427 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
428 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
429 rt.rt_flags = RTF_UP | RTF_HOST;
431 if (ioctl(s, SIOCADDRT, &rt) < 0) {
432 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m");
439 * cifaddr - Clear the interface IP addresses, and delete routes
440 * through the interface if possible.
443 int cifaddr (int unit, int our_adr, int his_adr)
447 * Delete the route through the device
449 memset (&rt, '\0', sizeof (rt));
451 SET_SA_FAMILY (rt.rt_dst, AF_INET);
452 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
453 rt.rt_dev = ifname; /* MJC */
455 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
456 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
457 rt.rt_flags = RTF_UP | RTF_HOST;
459 if (ioctl(s, SIOCDELRT, &rt) < 0) {
460 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m");
467 * path_to_route - determine the path to the proc file system data
470 FILE *route_fd = (FILE *) 0;
471 static char route_buffer [100];
473 static char *path_to_route (void);
474 static int open_route_table (void);
475 static void close_route_table (void);
476 static int read_route_table (struct rtentry *rt);
477 static int defaultroute_exists (void);
480 * path_to_route - find the path to the route tables in the proc file system
483 static char *path_to_route (void)
485 struct mntent *mntent;
488 fp = fopen (MOUNTED, "r");
490 while ((mntent = getmntent (fp)) != 0) {
491 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) == 0)
494 if (strcmp (mntent->mnt_type, "proc") == 0) {
495 strncpy (route_buffer, mntent->mnt_dir,
496 sizeof (route_buffer)-10);
497 route_buffer [sizeof (route_buffer)-10] = '\0';
498 strcat (route_buffer, "/net/route");
501 return (route_buffer);
506 syslog (LOG_ERR, "proc file system not mounted");
511 * open_route_table - open the interface to the route table
514 static int open_route_table (void)
518 if (route_fd != (FILE *) 0)
521 path = path_to_route();
525 route_fd = fopen (path, "r");
526 if (route_fd == (FILE *) 0) {
527 syslog (LOG_ERR, "can not open %s: %m", path);
531 /* read and discard the header line. */
532 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0) {
540 * close_route_table - close the interface to the route table
543 static void close_route_table (void)
545 if (route_fd != (FILE *) 0) {
547 route_fd = (FILE *) 0;
552 * read_route_table - read the next entry from the route table
555 static int read_route_table (struct rtentry *rt)
557 static char delims[] = " \t\n";
558 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
560 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
563 memset (rt, '\0', sizeof (struct rtentry));
565 dev_ptr = strtok (route_buffer, delims); /* interface name */
566 dst_ptr = strtok (NULL, delims); /* destination address */
567 gw_ptr = strtok (NULL, delims); /* gateway */
568 flag_ptr = strtok (NULL, delims); /* flags */
570 ptr = strtok (NULL, delims); /* reference count */
571 ptr = strtok (NULL, delims); /* useage count */
572 ptr = strtok (NULL, delims); /* metric */
573 ptr = strtok (NULL, delims); /* mask */
576 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
577 strtoul (dst_ptr, NULL, 16);
579 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
580 strtoul (gw_ptr, NULL, 16);
582 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
583 rt->rt_dev = dev_ptr;
589 * defaultroute_exists - determine if there is a default route
592 static int defaultroute_exists (void)
597 if (!open_route_table())
600 while (read_route_table(&rt) != 0) {
601 if (rt.rt_flags & RTF_UP == 0)
604 if (((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr == 0L) {
606 "ppp not replacing existing default route to %s[%s]",
608 inet_ntoa (((struct sockaddr_in *) &rt.rt_gateway)->
619 * sifdefaultroute - assign a default route through the address given.
622 int sifdefaultroute (int unit, int gateway)
626 if (defaultroute_exists())
629 memset (&rt, '\0', sizeof (rt));
630 SET_SA_FAMILY (rt.rt_dst, AF_INET);
631 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
632 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
634 rt.rt_flags = RTF_UP | RTF_GATEWAY;
635 if (ioctl(s, SIOCADDRT, &rt) < 0) {
636 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m");
643 * cifdefaultroute - delete a default route through the address given.
646 int cifdefaultroute (int unit, int gateway)
650 SET_SA_FAMILY (rt.rt_dst, AF_INET);
651 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
652 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
654 rt.rt_flags = RTF_UP | RTF_GATEWAY;
655 if (ioctl(s, SIOCDELRT, &rt) < 0) {
656 syslog (LOG_ERR, "default route ioctl(SIOCDELRT): %m");
663 * sifproxyarp - Make a proxy ARP entry for the peer.
666 int sifproxyarp (int unit, u_long his_adr)
668 struct arpreq arpreq;
670 memset (&arpreq, '\0', sizeof(arpreq));
672 * Get the hardware address of an interface on the same subnet
673 * as our local address.
675 if (!get_ether_addr(his_adr, &arpreq.arp_ha)) {
676 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
680 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
681 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
682 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
684 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
685 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
692 * cifproxyarp - Delete the proxy ARP entry for the peer.
695 int cifproxyarp (int unit, u_long his_adr)
697 struct arpreq arpreq;
699 memset (&arpreq, '\0', sizeof(arpreq));
700 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
702 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
703 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
704 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
711 * get_ether_addr - get the hardware address of an interface on the
712 * the same subnet as ipaddr.
715 int get_ether_addr (u_long ipaddr, struct sockaddr *hwaddr)
717 struct ifreq *ifr, *ifend, *ifp;
720 struct sockaddr_dl *dla;
723 struct ifreq ifs[MAX_IFS];
725 ifc.ifc_len = sizeof(ifs);
727 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
728 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
731 MAINDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
732 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
734 * Scan through looking for an interface with an Internet
735 * address on the same subnet as `ipaddr'.
737 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
738 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
739 if (ifr->ifr_addr.sa_family == AF_INET) {
740 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
741 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
742 MAINDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
745 * Check that the interface is up, and not point-to-point
748 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
750 if ((ifreq.ifr_flags &
751 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
752 != (IFF_UP|IFF_BROADCAST))
755 * Get its netmask and check that it's on the right subnet.
757 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
759 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
760 MAINDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
761 ip_ntoa(ina), ntohl(mask)));
762 if (((ipaddr ^ ina) & mask) != 0)
771 syslog(LOG_INFO, "found interface %s for proxy arp", ifreq.ifr_name);
773 * Now get the hardware address.
775 if (ioctl (s, SIOCGIFHWADDR, &ifreq) < 0) {
776 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m", ifreq.ifr_name);
780 hwaddr->sa_family = ARPHRD_ETHER;
781 memcpy (&hwaddr->sa_data, &ifr->ifr_hwaddr, ETH_ALEN);
783 MAINDEBUG ((LOG_DEBUG,
784 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x",
785 (int) ((unsigned char *) &ifr->ifr_hwaddr)[0],
786 (int) ((unsigned char *) &ifr->ifr_hwaddr)[1],
787 (int) ((unsigned char *) &ifr->ifr_hwaddr)[2],
788 (int) ((unsigned char *) &ifr->ifr_hwaddr)[3],
789 (int) ((unsigned char *) &ifr->ifr_hwaddr)[4],
790 (int) ((unsigned char *) &ifr->ifr_hwaddr)[5]));
795 * ppp_available - check whether the system has any ppp interfaces
796 * (in fact we check whether we can do an ioctl on ppp0).
799 int ppp_available(void)
804 s = socket(AF_INET, SOCK_DGRAM, 0);
806 return 1; /* can't tell - maybe we're not root */
808 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
809 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
816 logwtmp(line, name, host)
817 char *line, *name, *host;
821 memset (&ut, 0, sizeof (ut));
822 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
823 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
824 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
825 (void)time(&ut.ut_time);
827 pututline (&ut); /* Write the line to the proper place */
828 endutent(); /* Indicate operation is complete */