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)
98 if (ioctl(fd, TIOCEXCL, 0) < 0) {
99 syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
102 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
103 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
107 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
108 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
112 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
113 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
117 set_kdebugflag (kdebugflag);
121 * disestablish_ppp - Restore the serial port to normal operation.
122 * This shouldn't call die() because it's called from die().
125 void disestablish_ppp(void)
131 set_kdebugflag (prev_kdebugflag);
133 * Check whether the link seems not to be 8-bit clean.
135 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
137 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
139 s = "bit 7 set to 1";
142 s = "bit 7 set to 0";
152 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
153 syslog(LOG_WARNING, "All received characters had %s", s);
157 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
158 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
160 if (ioctl(fd, TIOCNXCL, 0) < 0)
161 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m");
168 * output - Output PPP packet.
171 void output (int unit, unsigned char *p, int len)
174 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
177 log_packet(p, len, "sent ");
179 if (write(fd, p, len) < 0) {
180 syslog(LOG_ERR, "write: %m");
186 * read_packet - get a PPP packet from the serial device.
189 int read_packet (unsigned char *buf)
193 len = read(fd, buf, MTU + DLLHEADERLEN);
195 if (errno == EWOULDBLOCK) {
197 MAINDEBUG((LOG_DEBUG, "read(fd): EWOULDBLOCK"));
201 syslog(LOG_ERR, "read(fd): %m");
208 * ppp_send_config - configure the transmit characteristics of
211 void ppp_send_config (int unit,int mtu,u_long asyncmap,int pcomp,int accomp)
216 MAINDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
217 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
219 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
220 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
224 MAINDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
225 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
226 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
230 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
231 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
235 x = pcomp ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
236 x = accomp ? x | SC_COMP_AC : x & ~SC_COMP_AC;
238 MAINDEBUG ((LOG_DEBUG, "send_config: flags = %x\n", x));
239 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
240 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
246 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
249 ppp_set_xaccm(unit, accm)
253 MAINDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
254 accm[0], accm[1], accm[2], accm[3]));
255 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
256 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
260 * ppp_recv_config - configure the receive-side characteristics of
263 void ppp_recv_config (int unit,int mru,u_long asyncmap,int pcomp,int accomp)
267 MAINDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
268 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
269 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
271 MAINDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
272 if (ioctl(fd, PPPIOCRASYNCMAP, (caddr_t) &asyncmap) < 0) {
273 syslog(LOG_ERR, "ioctl(PPPIOCRASYNCMAP): %m");
277 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
278 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
282 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
283 MAINDEBUG ((LOG_DEBUG, "recv_config: flags = %x\n", x));
284 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
285 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
291 * sifvjcomp - config tcp header compression
294 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
298 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
299 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
303 x = vjcomp ? x | SC_COMP_TCP : x &~ SC_COMP_TCP;
304 x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
306 if(ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
307 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
312 if (ioctl (fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
313 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
322 * sifup - Config the interface up and enable IP packets to pass.
329 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
330 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
331 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
335 ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
336 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
337 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
344 * sifdown - Config the interface down and disable IP.
351 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
352 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
353 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
357 ifr.ifr_flags &= ~IFF_UP;
358 ifr.ifr_flags |= IFF_POINTOPOINT;
359 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
360 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
367 * sifaddr - Config the interface IP addresses and netmask.
370 int sifaddr (int unit, int our_adr, int his_adr, int net_mask)
375 SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
376 SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
377 SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
379 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
383 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
384 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
386 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m");
388 syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
392 * Set the gateway address
394 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
395 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
396 syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
403 ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
404 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
405 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
410 * Add the device route
412 memset (&rt, '\0', sizeof (rt));
414 SET_SA_FAMILY (rt.rt_dst, AF_INET);
415 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
416 rt.rt_dev = ifname; /* MJC */
418 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
419 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
420 rt.rt_flags = RTF_UP | RTF_HOST;
422 if (ioctl(s, SIOCADDRT, &rt) < 0) {
423 syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m");
430 * cifaddr - Clear the interface IP addresses, and delete routes
431 * through the interface if possible.
434 int cifaddr (int unit, int our_adr, int his_adr)
438 * Delete the route through the device
440 memset (&rt, '\0', sizeof (rt));
442 SET_SA_FAMILY (rt.rt_dst, AF_INET);
443 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
444 rt.rt_dev = ifname; /* MJC */
446 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
447 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = his_adr;
448 rt.rt_flags = RTF_UP | RTF_HOST;
450 if (ioctl(s, SIOCDELRT, &rt) < 0) {
451 syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m");
458 * path_to_route - determine the path to the proc file system data
461 FILE *route_fd = (FILE *) 0;
462 static char route_buffer [100];
464 static char *path_to_route (void);
465 static int open_route_table (void);
466 static void close_route_table (void);
467 static int read_route_table (struct rtentry *rt);
468 static int defaultroute_exists (void);
471 * path_to_route - find the path to the route tables in the proc file system
474 static char *path_to_route (void)
476 struct mntent *mntent;
479 fp = fopen (MOUNTED, "r");
481 while ((mntent = getmntent (fp)) != 0) {
482 if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) == 0)
485 if (strcmp (mntent->mnt_type, "proc") == 0) {
486 strncpy (route_buffer, mntent->mnt_dir,
487 sizeof (route_buffer)-10);
488 route_buffer [sizeof (route_buffer)-10] = '\0';
489 strcat (route_buffer, "/net/route");
492 return (route_buffer);
497 syslog (LOG_ERR, "proc file system not mounted");
502 * open_route_table - open the interface to the route table
505 static int open_route_table (void)
509 if (route_fd != (FILE *) 0)
512 path = path_to_route();
516 route_fd = fopen (path, "r");
517 if (route_fd == (FILE *) 0) {
518 syslog (LOG_ERR, "can not open %s: %m", path);
522 /* read and discard the header line. */
523 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0) {
531 * close_route_table - close the interface to the route table
534 static void close_route_table (void)
536 if (route_fd != (FILE *) 0) {
538 route_fd = (FILE *) 0;
543 * read_route_table - read the next entry from the route table
546 static int read_route_table (struct rtentry *rt)
548 static char delims[] = " \t\n";
549 char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
551 if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
554 memset (rt, '\0', sizeof (struct rtentry));
556 dev_ptr = strtok (route_buffer, delims); /* interface name */
557 dst_ptr = strtok (NULL, delims); /* destination address */
558 gw_ptr = strtok (NULL, delims); /* gateway */
559 flag_ptr = strtok (NULL, delims); /* flags */
561 ptr = strtok (NULL, delims); /* reference count */
562 ptr = strtok (NULL, delims); /* useage count */
563 ptr = strtok (NULL, delims); /* metric */
564 ptr = strtok (NULL, delims); /* mask */
567 ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
568 strtoul (dst_ptr, NULL, 16);
570 ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
571 strtoul (gw_ptr, NULL, 16);
573 rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
574 rt->rt_dev = dev_ptr;
580 * defaultroute_exists - determine if there is a default route
583 static int defaultroute_exists (void)
588 if (!open_route_table())
591 while (read_route_table(&rt) != 0) {
592 if (rt.rt_flags & RTF_UP == 0)
595 if (((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr == 0L) {
597 "ppp not replacing existing default route to %s[%s]",
599 inet_ntoa (((struct sockaddr_in *) &rt.rt_gateway)->
610 * sifdefaultroute - assign a default route through the address given.
613 int sifdefaultroute (int unit, int gateway)
617 if (defaultroute_exists())
620 memset (&rt, '\0', sizeof (rt));
621 SET_SA_FAMILY (rt.rt_dst, AF_INET);
622 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
623 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
625 rt.rt_flags = RTF_UP | RTF_GATEWAY;
626 if (ioctl(s, SIOCADDRT, &rt) < 0) {
627 syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m");
634 * cifdefaultroute - delete a default route through the address given.
637 int cifdefaultroute (int unit, int gateway)
641 SET_SA_FAMILY (rt.rt_dst, AF_INET);
642 SET_SA_FAMILY (rt.rt_gateway, AF_INET);
643 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
645 rt.rt_flags = RTF_UP | RTF_GATEWAY;
646 if (ioctl(s, SIOCDELRT, &rt) < 0) {
647 syslog (LOG_ERR, "default route ioctl(SIOCDELRT): %m");
654 * sifproxyarp - Make a proxy ARP entry for the peer.
657 int sifproxyarp (int unit, u_long his_adr)
659 struct arpreq arpreq;
661 memset (&arpreq, '\0', sizeof(arpreq));
663 * Get the hardware address of an interface on the same subnet
664 * as our local address.
666 if (!get_ether_addr(his_adr, &arpreq.arp_ha)) {
667 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
671 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
672 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
673 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
675 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
676 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
683 * cifproxyarp - Delete the proxy ARP entry for the peer.
686 int cifproxyarp (int unit, u_long his_adr)
688 struct arpreq arpreq;
690 memset (&arpreq, '\0', sizeof(arpreq));
691 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
693 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
694 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
695 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
702 * get_ether_addr - get the hardware address of an interface on the
703 * the same subnet as ipaddr.
706 int get_ether_addr (u_long ipaddr, struct sockaddr *hwaddr)
708 struct ifreq *ifr, *ifend, *ifp;
711 struct sockaddr_dl *dla;
714 struct ifreq ifs[MAX_IFS];
716 ifc.ifc_len = sizeof(ifs);
718 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
719 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
722 MAINDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
723 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
725 * Scan through looking for an interface with an Internet
726 * address on the same subnet as `ipaddr'.
728 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
729 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
730 if (ifr->ifr_addr.sa_family == AF_INET) {
731 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
732 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
733 MAINDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
736 * Check that the interface is up, and not point-to-point
739 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
741 if ((ifreq.ifr_flags &
742 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
743 != (IFF_UP|IFF_BROADCAST))
746 * Get its netmask and check that it's on the right subnet.
748 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
750 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
751 MAINDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
752 ip_ntoa(ina), ntohl(mask)));
753 if (((ipaddr ^ ina) & mask) != 0)
762 syslog(LOG_INFO, "found interface %s for proxy arp", ifreq.ifr_name);
764 * Now get the hardware address.
766 if (ioctl (s, SIOCGIFHWADDR, &ifreq) < 0) {
767 syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m", ifreq.ifr_name);
771 hwaddr->sa_family = ARPHRD_ETHER;
772 memcpy (&hwaddr->sa_data, &ifr->ifr_hwaddr, ETH_ALEN);
774 MAINDEBUG ((LOG_DEBUG,
775 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x",
776 (int) ((unsigned char *) &ifr->ifr_hwaddr)[0],
777 (int) ((unsigned char *) &ifr->ifr_hwaddr)[1],
778 (int) ((unsigned char *) &ifr->ifr_hwaddr)[2],
779 (int) ((unsigned char *) &ifr->ifr_hwaddr)[3],
780 (int) ((unsigned char *) &ifr->ifr_hwaddr)[4],
781 (int) ((unsigned char *) &ifr->ifr_hwaddr)[5]));
786 * ppp_available - check whether the system has any ppp interfaces
787 * (in fact we check whether we can do an ioctl on ppp0).
790 int ppp_available(void)
795 s = socket(AF_INET, SOCK_DGRAM, 0);
797 return 1; /* can't tell - maybe we're not root */
799 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
800 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
807 logwtmp(line, name, host)
808 char *line, *name, *host;
812 memset (&ut, 0, sizeof (ut));
813 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
814 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
815 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
816 (void)time(&ut.ut_time);
818 pututline (&ut); /* Write the line to the proper place */
819 endutent(); /* Indicate operation is complete */