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 /* finally, push the ppp_if module that actually handles the */
136 /* network interface */
137 if (ioctl(fd, I_PUSH, "pppif") < 0) {
138 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
143 if (ioctl(fd, I_SETSIG, S_INPUT) < 0) {
144 syslog(LOG_ERR, "ioctl(I_SETSIG, S_INPUT): %m");
148 /* read mode, message non-discard mode */
149 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
150 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
154 /* Flush any waiting messages, or we'll never get SIGPOLL */
155 if (ioctl(fd, I_FLUSH, FLUSHRW) < 0) {
156 syslog(LOG_ERR, "ioctl(I_FLUSH, FLUSHRW): %m");
161 * Find out which interface we were given.
162 * (ppp_if handles this ioctl)
164 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
165 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
169 /* Set debug flags in driver */
170 if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
171 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
174 /* close stdin, stdout, stderr if they might refer to the device */
175 if (default_device && !closed_stdio) {
178 for (i = 0; i <= 2; ++i)
179 if (i != fd && i != s)
186 * disestablish_ppp - Restore the serial port to normal operation.
187 * It attempts to reconstruct the stream with the previously popped
188 * modules. This shouldn't call die() because it's called from die().
197 /* we can't push or pop modules after the stream has hung up */
198 str_module_count = 0;
199 restore_term = 0; /* nor can we fix up terminal settings */
205 * Check whether the link seems not to be 8-bit clean.
207 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
209 switch (~flags & PAI_FLAGS_HIBITS) {
211 s = "bit 7 set to 1";
214 s = "bit 7 set to 0";
216 case PAI_FLAGS_PAR_EVEN:
219 case PAI_FLAGS_PAR_ODD:
224 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
225 syslog(LOG_WARNING, "All received characters had %s", s);
230 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
234 for (; str_module_count > 0; str_module_count--) {
235 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
237 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
238 str_modules[str_module_count-1].modname);
240 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
241 str_modules[str_module_count-1].modname));
248 * output - Output PPP packet.
259 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
261 log_packet(p, len, "sent ");
264 str.buf = (caddr_t) p;
265 if (putmsg(fd, NULL, &str, 0) < 0) {
266 if (errno != ENXIO) {
267 syslog(LOG_ERR, "putmsg: %m");
275 struct timeval *timo;
280 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
282 pfd.events = POLLIN | POLLPRI | POLLHUP;
283 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
284 syslog(LOG_ERR, "poll: %m");
290 * read_packet - get a PPP packet from the serial device.
296 struct strbuf str, ctl;
298 unsigned char ctlbuf[16];
300 str.maxlen = MTU+DLLHEADERLEN;
301 str.buf = (caddr_t) buf;
302 ctl.maxlen = sizeof(ctlbuf);
303 ctl.buf = (caddr_t) ctlbuf;
305 len = getmsg(fd, &ctl, &str, &i);
307 if (errno == EAGAIN || errno == EWOULDBLOCK) {
310 syslog(LOG_ERR, "getmsg(fd) %m");
314 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
316 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
317 ctlbuf[0], ctlbuf[1]);
320 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
329 * ppp_send_config - configure the transmit characteristics of
333 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
341 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
343 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
344 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
348 if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
349 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
354 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
355 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
360 if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
361 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
368 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
371 ppp_set_xaccm(unit, accm)
375 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
376 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
381 * ppp_recv_config - configure the receive-side characteristics of
385 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
392 if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
393 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
396 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
397 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
400 c = 2 + (pcomp? 1: 0);
401 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
402 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
405 c = 2 + (accomp? 1: 0);
406 if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
407 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
412 * ccp_flags_set - inform kernel about the current state of CCP.
415 ccp_flags_set(unit, isopen, isup)
416 int unit, isopen, isup;
421 x = (isopen? 1: 0) + (isup? 2: 0);
422 if (ioctl(fd, SIOCSIFCOMP, (caddr_t) &x) < 0)
423 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
428 * sifvjcomp - config tcp header compression
431 sifvjcomp(u, vjcomp, cidcomp, maxcid)
432 int u, vjcomp, cidcomp, maxcid;
436 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
437 if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
438 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
445 * sifup - Config the interface up.
453 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
454 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
455 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
458 ifr.ifr_flags |= IFF_UP;
459 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
460 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
467 * sifdown - Config the interface down.
474 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
475 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
476 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
479 ifr.ifr_flags &= ~IFF_UP;
480 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
481 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
488 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
490 #define SET_SA_FAMILY(addr, family) \
491 BZERO((char *) &(addr), sizeof(addr)); \
492 addr.sa_family = (family);
495 * sifaddr - Config the interface IP addresses and netmask.
506 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
507 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
508 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
509 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
510 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
513 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
514 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
515 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
519 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
520 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
521 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
522 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
530 * cifaddr - Clear the interface IP addresses, and delete routes
531 * through the interface if possible.
540 SET_SA_FAMILY(rt.rt_dst, AF_INET);
541 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
542 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
543 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
544 rt.rt_flags = RTF_HOST;
545 if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
546 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
553 * sifdefaultroute - assign a default route through the address given.
556 sifdefaultroute(u, g)
562 SET_SA_FAMILY(rt.rt_dst, AF_INET);
563 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
564 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
565 rt.rt_flags = RTF_GATEWAY;
566 if (ioctl(s, SIOCADDRT, &rt) < 0) {
567 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
574 * cifdefaultroute - delete a default route through the address given.
577 cifdefaultroute(u, g)
583 SET_SA_FAMILY(rt.rt_dst, AF_INET);
584 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
585 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
586 rt.rt_flags = RTF_GATEWAY;
587 if (ioctl(s, SIOCDELRT, &rt) < 0) {
588 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
595 * sifproxyarp - Make a proxy ARP entry for the peer.
598 sifproxyarp(unit, hisaddr)
602 struct arpreq arpreq;
604 BZERO(&arpreq, sizeof(arpreq));
607 * Get the hardware address of an interface on the same subnet
608 * as our local address.
610 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
611 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
615 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
616 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
617 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
618 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
619 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
627 * cifproxyarp - Delete the proxy ARP entry for the peer.
630 cifproxyarp(unit, hisaddr)
634 struct arpreq arpreq;
636 BZERO(&arpreq, sizeof(arpreq));
637 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
638 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
639 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
640 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
647 * get_ether_addr - get the hardware address of an interface on the
648 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
649 * in the cslip-2.6 distribution, which is subject to the following
650 * copyright notice (which also applies to logwtmp below):
652 * Copyright (c) 1990, 1992 The Regents of the University of California.
653 * All rights reserved.
655 * Redistribution and use in source and binary forms, with or without
656 * modification, are permitted provided that: (1) source code distributions
657 * retain the above copyright notice and this paragraph in its entirety, (2)
658 * distributions including binary code include the above copyright notice and
659 * this paragraph in its entirety in the documentation or other materials
660 * provided with the distribution, and (3) all advertising materials mentioning
661 * features or use of this software display the following acknowledgement:
662 * ``This product includes software developed by the University of California,
663 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
664 * the University nor the names of its contributors may be used to endorse
665 * or promote products derived from this software without specific prior
666 * written permission.
667 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
668 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
669 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
675 #include <arpa/inet.h>
677 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
683 #include <netinet/in_var.h>
685 #include <netinet/if_ether.h>
687 /* Cast a struct sockaddr to a structaddr_in */
688 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
690 /* Determine if "bits" is set in "flag" */
691 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
693 static struct nlist nl[] = {
702 get_ether_addr(ipaddr, hwaddr)
704 struct sockaddr *hwaddr;
707 register struct ifnet *ifp;
708 register struct arpcom *ac;
709 struct arpcom arpcom;
712 register struct ifaddr *ifa;
713 register struct in_ifaddr *in;
721 /* Open kernel memory for reading */
722 kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
724 syslog(LOG_ERR, "kvm_open: %m");
729 if (kvm_nlist(kd, nl) != 0) {
730 syslog(LOG_ERR, "kvm_nlist failed");
741 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
743 syslog(LOG_ERR, "error reading ifnet addr");
746 for ( ; addr; addr = (u_long)ifp->if_next) {
747 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
748 syslog(LOG_ERR, "error reading ifnet");
752 /* Only look at configured, broadcast interfaces */
753 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
756 /* This probably can't happen... */
757 if (ifp->if_addrlist == 0)
761 /* Get interface ip address */
763 if (kvm_read(kd, (u_long)ifp->if_addrlist, (char *)&ifaddr,
764 sizeof(ifaddr)) != sizeof(ifaddr)) {
765 syslog(LOG_ERR, "error reading ifaddr");
768 inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
770 inp = &SATOSIN(&ifp->if_addr)->sin_addr;
773 /* Check if this interface on the right subnet */
775 mask = in->ia_subnetmask;
777 mask = ifp->if_subnetmask;
779 if ((ipaddr & mask) != (inp->s_addr & mask))
782 /* Copy out the local ethernet address */
783 hwaddr->sa_family = AF_UNSPEC;
784 BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
785 sizeof(arpcom.ac_enaddr));
786 return 1; /* success! */
789 /* couldn't find one */
793 #define WTMPFILE "/usr/adm/wtmp"
796 logwtmp(line, name, host)
797 char *line, *name, *host;
803 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
805 if (!fstat(fd, &buf)) {
806 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
807 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
808 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
809 (void)time(&ut.ut_time);
810 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
811 (void)ftruncate(fd, buf.st_size);