2 * sys-str.c - System-dependent procedures for setting up
3 * PPP interfaces on systems which use the STREAMS ppp interface.
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.
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <sys/stream.h>
38 #include <sys/stropts.h>
41 #include <net/route.h>
42 #include <net/if_arp.h>
43 #include <netinet/in.h>
47 #include <net/ppp_str.h>
50 #define ifr_mtu ifr_metric
53 #define MAXMODULES 10 /* max number of module names to save */
54 static struct modlist {
55 char modname[FMNAMESZ+1];
56 } str_modules[MAXMODULES];
57 static int str_module_count = 0;
58 static int pushed_ppp;
59 static int closed_stdio;
61 extern int hungup; /* has the physical layer been disconnected? */
62 extern int kdebugflag;
63 extern int default_device;
64 extern int restore_term;
66 #define PAI_FLAGS_B7_0 0x100
67 #define PAI_FLAGS_B7_1 0x200
68 #define PAI_FLAGS_PAR_EVEN 0x400
69 #define PAI_FLAGS_PAR_ODD 0x800
70 #define PAI_FLAGS_HIBITS 0xF00
73 * daemon - Detach us from the terminal session.
76 daemon(nochdir, noclose)
81 if ((pid = fork()) < 0)
84 exit(0); /* parent dies */
89 fclose(stdin); /* don't need stdin, stdout, stderr */
98 * ppp_available - check if this kernel supports PPP.
105 fd = open("/dev/tty", O_RDONLY, 0);
107 return 1; /* can't find out - assume we have ppp */
108 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
115 * establish_ppp - Turn the serial port into a ppp interface.
120 /* go through and save the name of all the modules, then pop em */
122 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
123 ioctl(fd, I_POP, 0) < 0)
125 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
126 str_modules[str_module_count].modname));
130 /* now push the async/fcs module */
131 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
132 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
135 /* push the compress module */
136 if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
137 syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
139 /* finally, push the ppp_if module that actually handles the */
140 /* network interface */
141 if (ioctl(fd, I_PUSH, "pppif") < 0) {
142 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
146 /* read mode, message non-discard mode */
147 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
148 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
152 * Find out which interface we were given.
153 * (ppp_if handles this ioctl)
155 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
156 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
160 /* Set debug flags in driver */
161 if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
162 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
165 /* close stdin, stdout, stderr if they might refer to the device */
166 if (default_device && !closed_stdio) {
169 for (i = 0; i <= 2; ++i)
170 if (i != fd && i != s)
177 * disestablish_ppp - Restore the serial port to normal operation.
178 * It attempts to reconstruct the stream with the previously popped
179 * modules. This shouldn't call die() because it's called from die().
188 /* we can't push or pop modules after the stream has hung up */
189 str_module_count = 0;
190 restore_term = 0; /* nor can we fix up terminal settings */
196 * Check whether the link seems not to be 8-bit clean.
198 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
200 switch (~flags & PAI_FLAGS_HIBITS) {
202 s = "bit 7 set to 1";
205 s = "bit 7 set to 0";
207 case PAI_FLAGS_PAR_EVEN:
210 case PAI_FLAGS_PAR_ODD:
215 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
216 syslog(LOG_WARNING, "All received characters had %s", s);
221 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
225 for (; str_module_count > 0; str_module_count--) {
226 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
228 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
229 str_modules[str_module_count-1].modname);
231 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
232 str_modules[str_module_count-1].modname));
239 * output - Output PPP packet.
250 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
252 log_packet(p, len, "sent ");
255 str.buf = (caddr_t) p;
256 if (putmsg(fd, NULL, &str, 0) < 0) {
257 if (errno != ENXIO) {
258 syslog(LOG_ERR, "putmsg: %m");
266 struct timeval *timo;
271 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
273 pfd.events = POLLIN | POLLPRI | POLLHUP;
274 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
275 syslog(LOG_ERR, "poll: %m");
281 * read_packet - get a PPP packet from the serial device.
287 struct strbuf str, ctl;
289 unsigned char ctlbuf[16];
291 str.maxlen = MTU+DLLHEADERLEN;
292 str.buf = (caddr_t) buf;
293 ctl.maxlen = sizeof(ctlbuf);
294 ctl.buf = (caddr_t) ctlbuf;
296 len = getmsg(fd, &ctl, &str, &i);
298 if (errno == EAGAIN || errno == EWOULDBLOCK) {
301 syslog(LOG_ERR, "getmsg(fd) %m");
305 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
307 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
308 ctlbuf[0], ctlbuf[1]);
311 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
320 * ppp_send_config - configure the transmit characteristics of
324 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
332 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
334 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
335 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
339 if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
340 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
345 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
346 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
351 if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
352 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
359 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
362 ppp_set_xaccm(unit, accm)
366 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
367 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
372 * ppp_recv_config - configure the receive-side characteristics of
376 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
383 if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
384 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
387 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
388 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
391 c = 2 + (pcomp? 1: 0);
392 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
393 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
396 c = 2 + (accomp? 1: 0);
397 if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
398 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
403 * ccp_test - ask kernel whether a given compression method
404 * is acceptable for use.
406 ccp_test(unit, opt_ptr, opt_len, for_transmit)
407 int unit, opt_len, for_transmit;
410 struct ppp_option_data data;
412 if ((unsigned) opt_len > MAX_PPP_OPTION)
413 opt_len = MAX_PPP_OPTION;
414 data.length = opt_len;
415 data.transmit = for_transmit;
416 BCOPY(opt_ptr, data.opt_data, opt_len);
417 return ioctl(fd, SIOCSCOMPRESS, (caddr_t) &data) >= 0;
421 * ccp_flags_set - inform kernel about the current state of CCP.
424 ccp_flags_set(unit, isopen, isup)
425 int unit, isopen, isup;
429 x = (isopen? 1: 0) + (isup? 2: 0);
430 if (ioctl(fd, SIOCSIFCOMP, (caddr_t) &x) < 0 && errno != ENOTTY)
431 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
435 * sifvjcomp - config tcp header compression
438 sifvjcomp(u, vjcomp, cidcomp, maxcid)
439 int u, vjcomp, cidcomp, maxcid;
443 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
444 if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
445 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
452 * sifup - Config the interface up.
460 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
461 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
462 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
465 ifr.ifr_flags |= IFF_UP;
466 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
467 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
474 * sifdown - Config the interface down.
481 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
482 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
483 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
486 ifr.ifr_flags &= ~IFF_UP;
487 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
488 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
495 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
497 #define SET_SA_FAMILY(addr, family) \
498 BZERO((char *) &(addr), sizeof(addr)); \
499 addr.sa_family = (family);
502 * sifaddr - Config the interface IP addresses and netmask.
513 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
514 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
515 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
516 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
517 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
520 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
521 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
522 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
526 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
527 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
528 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
529 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
537 * cifaddr - Clear the interface IP addresses, and delete routes
538 * through the interface if possible.
547 SET_SA_FAMILY(rt.rt_dst, AF_INET);
548 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
549 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
550 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
551 rt.rt_flags = RTF_HOST;
552 if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
553 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
560 * sifdefaultroute - assign a default route through the address given.
563 sifdefaultroute(u, g)
569 SET_SA_FAMILY(rt.rt_dst, AF_INET);
570 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
571 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
572 rt.rt_flags = RTF_GATEWAY;
573 if (ioctl(s, SIOCADDRT, &rt) < 0) {
574 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
581 * cifdefaultroute - delete a default route through the address given.
584 cifdefaultroute(u, g)
590 SET_SA_FAMILY(rt.rt_dst, AF_INET);
591 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
592 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
593 rt.rt_flags = RTF_GATEWAY;
594 if (ioctl(s, SIOCDELRT, &rt) < 0) {
595 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
602 * sifproxyarp - Make a proxy ARP entry for the peer.
605 sifproxyarp(unit, hisaddr)
609 struct arpreq arpreq;
611 BZERO(&arpreq, sizeof(arpreq));
614 * Get the hardware address of an interface on the same subnet
615 * as our local address.
617 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
618 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
622 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
623 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
624 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
625 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
626 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
634 * cifproxyarp - Delete the proxy ARP entry for the peer.
637 cifproxyarp(unit, hisaddr)
641 struct arpreq arpreq;
643 BZERO(&arpreq, sizeof(arpreq));
644 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
645 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
646 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
647 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
654 * get_ether_addr - get the hardware address of an interface on the
655 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
656 * in the cslip-2.6 distribution, which is subject to the following
657 * copyright notice (which also applies to logwtmp below):
659 * Copyright (c) 1990, 1992 The Regents of the University of California.
660 * All rights reserved.
662 * Redistribution and use in source and binary forms, with or without
663 * modification, are permitted provided that: (1) source code distributions
664 * retain the above copyright notice and this paragraph in its entirety, (2)
665 * distributions including binary code include the above copyright notice and
666 * this paragraph in its entirety in the documentation or other materials
667 * provided with the distribution, and (3) all advertising materials mentioning
668 * features or use of this software display the following acknowledgement:
669 * ``This product includes software developed by the University of California,
670 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
671 * the University nor the names of its contributors may be used to endorse
672 * or promote products derived from this software without specific prior
673 * written permission.
674 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
675 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
676 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
682 #include <arpa/inet.h>
684 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
690 #include <netinet/in_var.h>
692 #include <netinet/if_ether.h>
694 /* Cast a struct sockaddr to a structaddr_in */
695 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
697 /* Determine if "bits" is set in "flag" */
698 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
700 static struct nlist nl[] = {
709 get_ether_addr(ipaddr, hwaddr)
711 struct sockaddr *hwaddr;
714 register struct ifnet *ifp;
715 register struct arpcom *ac;
716 struct arpcom arpcom;
719 register struct ifaddr *ifa;
720 register struct in_ifaddr *in;
728 /* Open kernel memory for reading */
729 kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
731 syslog(LOG_ERR, "kvm_open: %m");
736 if (kvm_nlist(kd, nl) != 0) {
737 syslog(LOG_ERR, "kvm_nlist failed");
748 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
750 syslog(LOG_ERR, "error reading ifnet addr");
753 for ( ; addr; addr = (u_long)ifp->if_next) {
754 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
755 syslog(LOG_ERR, "error reading ifnet");
759 /* Only look at configured, broadcast interfaces */
760 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
763 /* This probably can't happen... */
764 if (ifp->if_addrlist == 0)
768 /* Get interface ip address */
770 if (kvm_read(kd, (u_long)ifp->if_addrlist, (char *)&ifaddr,
771 sizeof(ifaddr)) != sizeof(ifaddr)) {
772 syslog(LOG_ERR, "error reading ifaddr");
775 inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
777 inp = &SATOSIN(&ifp->if_addr)->sin_addr;
780 /* Check if this interface on the right subnet */
782 mask = in->ia_subnetmask;
784 mask = ifp->if_subnetmask;
786 if ((ipaddr & mask) != (inp->s_addr & mask))
789 /* Copy out the local ethernet address */
790 hwaddr->sa_family = AF_UNSPEC;
791 BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
792 sizeof(arpcom.ac_enaddr));
793 return 1; /* success! */
796 /* couldn't find one */
800 #define WTMPFILE "/usr/adm/wtmp"
803 logwtmp(line, name, host)
804 char *line, *name, *host;
810 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
812 if (!fstat(fd, &buf)) {
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);
817 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
818 (void)ftruncate(fd, buf.st_size);