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/if_ppp.h>
48 #include <net/ppp_str.h>
51 #define ifr_mtu ifr_metric
54 #define MAXMODULES 10 /* max number of module names to save */
55 static struct modlist {
56 char modname[FMNAMESZ+1];
57 } str_modules[MAXMODULES];
58 static int str_module_count = 0;
59 static int pushed_ppp;
61 extern int hungup; /* has the physical layer been disconnected? */
62 extern int kdebugflag;
64 #define PAI_FLAGS_B7_0 0x100
65 #define PAI_FLAGS_B7_1 0x200
66 #define PAI_FLAGS_PAR_EVEN 0x400
67 #define PAI_FLAGS_PAR_ODD 0x800
68 #define PAI_FLAGS_HIBITS 0xF00
71 * ppp_available - check if this kernel supports PPP.
78 fd = open("/dev/tty", O_RDONLY, 0);
80 return 1; /* can't find out - assume we have ppp */
81 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
88 * establish_ppp - Turn the serial port into a ppp interface.
93 /* go through and save the name of all the modules, then pop em */
95 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
96 ioctl(fd, I_POP, 0) < 0)
98 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
99 str_modules[str_module_count].modname));
103 /* now push the async/fcs module */
104 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
105 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
108 /* finally, push the ppp_if module that actually handles the */
109 /* network interface */
110 if (ioctl(fd, I_PUSH, "pppif") < 0) {
111 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
115 if (ioctl(fd, I_SETSIG, S_INPUT) < 0) {
116 syslog(LOG_ERR, "ioctl(I_SETSIG, S_INPUT): %m");
119 /* read mode, message non-discard mode */
120 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
121 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
124 /* Flush any waiting messages, or we'll never get SIGPOLL */
125 if (ioctl(fd, I_FLUSH, FLUSHRW) < 0) {
126 syslog(LOG_ERR, "ioctl(I_FLUSH, FLUSHRW): %m");
130 * Find out which interface we were given.
131 * (ppp_if handles this ioctl)
133 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
134 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
138 /* Set debug flags in driver */
139 if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
140 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
145 * disestablish_ppp - Restore the serial port to normal operation.
146 * It attempts to reconstruct the stream with the previously popped
147 * modules. This shouldn't call die() because it's called from die().
156 /* we can't push or pop modules after the stream has hung up */
157 str_module_count = 0;
163 * Check whether the link seems not to be 8-bit clean.
165 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
167 switch (~flags & PAI_FLAGS_HIBITS) {
169 s = "bit 7 set to 1";
172 s = "bit 7 set to 0";
174 case PAI_FLAGS_PAR_EVEN:
177 case PAI_FLAGS_PAR_ODD:
182 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
183 syslog(LOG_WARNING, "All received characters had %s", s);
188 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
192 for (; str_module_count > 0; str_module_count--) {
193 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
194 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
195 str_modules[str_module_count-1].modname);
197 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
198 str_modules[str_module_count-1].modname));
205 * output - Output PPP packet.
216 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
218 log_packet(p, len, "sent ");
221 str.buf = (caddr_t) p;
222 if(putmsg(fd, NULL, &str, 0) < 0) {
223 syslog(LOG_ERR, "putmsg: %m");
230 * read_packet - get a PPP packet from the serial device.
239 str.maxlen = MTU+DLLHEADERLEN;
240 str.buf = (caddr_t) buf;
242 len = getmsg(fd, NULL, &str, &i);
244 if (errno == EAGAIN || errno == EWOULDBLOCK) {
247 syslog(LOG_ERR, "getmsg(fd) %m");
251 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
254 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
263 * ppp_send_config - configure the transmit characteristics of
267 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
275 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
277 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
278 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
282 if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
283 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
288 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
289 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
294 if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
295 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
302 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
305 ppp_set_xaccm(unit, accm)
309 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
310 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
315 * ppp_recv_config - configure the receive-side characteristics of
319 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
326 if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
327 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
330 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
331 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
334 c = 2 + (pcomp? 1: 0);
335 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
336 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
339 c = 2 + (accomp? 1: 0);
340 if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
341 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
346 * sifvjcomp - config tcp header compression
349 sifvjcomp(u, vjcomp, cidcomp, maxcid)
350 int u, vjcomp, cidcomp, maxcid;
354 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
355 if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
356 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
363 * sifup - Config the interface up.
370 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
371 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
372 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
375 ifr.ifr_flags |= IFF_UP;
376 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
377 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
384 * sifdown - Config the interface down.
390 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
391 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
392 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
395 ifr.ifr_flags &= ~IFF_UP;
396 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
397 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
404 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
406 #define SET_SA_FAMILY(addr, family) \
407 BZERO((char *) &(addr), sizeof(addr)); \
408 addr.sa_family = (family);
411 * sifaddr - Config the interface IP addresses and netmask.
420 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
421 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
422 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
423 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
424 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
427 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
428 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
429 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
433 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
434 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
435 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
436 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
444 * cifaddr - Clear the interface IP addresses, and delete routes
445 * through the interface if possible.
452 SET_SA_FAMILY(rt.rt_dst, AF_INET);
453 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
454 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
455 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
456 rt.rt_flags = RTF_HOST;
457 if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
458 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
465 * sifdefaultroute - assign a default route through the address given.
468 sifdefaultroute(u, g)
472 SET_SA_FAMILY(rt.rt_dst, AF_INET);
473 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
474 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
475 rt.rt_flags = RTF_GATEWAY;
476 if (ioctl(s, SIOCADDRT, &rt) < 0) {
477 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
484 * cifdefaultroute - delete a default route through the address given.
487 cifdefaultroute(u, g)
491 SET_SA_FAMILY(rt.rt_dst, AF_INET);
492 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
493 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
494 rt.rt_flags = RTF_GATEWAY;
495 if (ioctl(s, SIOCDELRT, &rt) < 0) {
496 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
503 * sifproxyarp - Make a proxy ARP entry for the peer.
506 sifproxyarp(unit, hisaddr)
510 struct arpreq arpreq;
512 BZERO(&arpreq, sizeof(arpreq));
515 * Get the hardware address of an interface on the same subnet
516 * as our local address.
518 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
519 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
523 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
524 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
525 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
526 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
527 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
535 * cifproxyarp - Delete the proxy ARP entry for the peer.
538 cifproxyarp(unit, hisaddr)
542 struct arpreq arpreq;
544 BZERO(&arpreq, sizeof(arpreq));
545 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
546 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
547 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
548 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
555 * get_ether_addr - get the hardware address of an interface on the
556 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
557 * in the cslip-2.6 distribution, which is subject to the following
558 * copyright notice (which also applies to logwtmp below):
560 * Copyright (c) 1990, 1992 The Regents of the University of California.
561 * All rights reserved.
563 * Redistribution and use in source and binary forms, with or without
564 * modification, are permitted provided that: (1) source code distributions
565 * retain the above copyright notice and this paragraph in its entirety, (2)
566 * distributions including binary code include the above copyright notice and
567 * this paragraph in its entirety in the documentation or other materials
568 * provided with the distribution, and (3) all advertising materials mentioning
569 * features or use of this software display the following acknowledgement:
570 * ``This product includes software developed by the University of California,
571 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
572 * the University nor the names of its contributors may be used to endorse
573 * or promote products derived from this software without specific prior
574 * written permission.
575 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
576 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
577 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
583 #include <arpa/inet.h>
585 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
591 #include <netinet/in_var.h>
593 #include <netinet/if_ether.h>
595 /* Cast a struct sockaddr to a structaddr_in */
596 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
598 /* Determine if "bits" is set in "flag" */
599 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
601 static struct nlist nl[] = {
610 get_ether_addr(ipaddr, hwaddr)
612 struct sockaddr *hwaddr;
615 register struct ifnet *ifp;
616 register struct arpcom *ac;
617 struct arpcom arpcom;
620 register struct ifaddr *ifa;
621 register struct in_ifaddr *in;
629 /* Open kernel memory for reading */
630 kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
632 syslog(LOG_ERR, "kvm_open: %m");
637 if (kvm_nlist(kd, nl) != 0) {
638 syslog(LOG_ERR, "kvm_nlist failed");
649 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
651 syslog(LOG_ERR, "error reading ifnet addr");
654 for ( ; addr; addr = (u_long)ifp->if_next) {
655 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
656 syslog(LOG_ERR, "error reading ifnet");
660 /* Only look at configured, broadcast interfaces */
661 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
664 /* This probably can't happen... */
665 if (ifp->if_addrlist == 0)
669 /* Get interface ip address */
671 if (kvm_read(kd, (u_long)ifp->if_addrlist, (char *)&ifaddr,
672 sizeof(ifaddr)) != sizeof(ifaddr)) {
673 syslog(LOG_ERR, "error reading ifaddr");
676 inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
678 inp = &SATOSIN(&ifp->if_addr)->sin_addr;
681 /* Check if this interface on the right subnet */
683 mask = in->ia_subnetmask;
685 mask = ifp->if_subnetmask;
687 if ((ipaddr & mask) != (inp->s_addr & mask))
690 /* Copy out the local ethernet address */
691 hwaddr->sa_family = AF_UNSPEC;
692 BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
693 sizeof(arpcom.ac_enaddr));
694 return 1; /* success! */
697 /* couldn't find one */
701 #define WTMPFILE "/usr/adm/wtmp"
704 logwtmp(line, name, host)
705 char *line, *name, *host;
711 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
713 if (!fstat(fd, &buf)) {
714 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
715 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
716 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
717 (void)time(&ut.ut_time);
718 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
719 (void)ftruncate(fd, buf.st_size);