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.
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <sys/stream.h>
37 #include <sys/stropts.h>
40 #include <net/route.h>
41 #include <net/if_arp.h>
42 #include <netinet/in.h>
46 #include <net/ppp_str.h>
49 #define ifr_mtu ifr_metric
52 #define MAXMODULES 10 /* max number of module names to save */
53 static struct modlist {
54 char modname[FMNAMESZ+1];
55 } str_modules[MAXMODULES];
56 static int str_module_count = 0;
57 static int pushed_ppp;
59 extern int hungup; /* has the physical layer been disconnected? */
60 extern int kdebugflag;
62 #define PAI_FLAGS_B7_0 0x100
63 #define PAI_FLAGS_B7_1 0x200
64 #define PAI_FLAGS_PAR_EVEN 0x400
65 #define PAI_FLAGS_PAR_ODD 0x800
66 #define PAI_FLAGS_HIBITS 0xF00
69 * ppp_available - check if this kernel supports PPP.
76 fd = open("/dev/tty", O_RDONLY, 0);
78 return 1; /* can't find out - assume we have ppp */
79 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
86 * establish_ppp - Turn the serial port into a ppp interface.
91 /* go through and save the name of all the modules, then pop em */
93 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
94 ioctl(fd, I_POP, 0) < 0)
96 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
97 str_modules[str_module_count].modname));
101 /* now push the async/fcs module */
102 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
103 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
106 /* finally, push the ppp_if module that actually handles the */
107 /* network interface */
108 if (ioctl(fd, I_PUSH, "pppif") < 0) {
109 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
113 if (ioctl(fd, I_SETSIG, S_INPUT) < 0) {
114 syslog(LOG_ERR, "ioctl(I_SETSIG, S_INPUT): %m");
117 /* read mode, message non-discard mode */
118 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
119 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
122 /* Flush any waiting messages, or we'll never get SIGPOLL */
123 if (ioctl(fd, I_FLUSH, FLUSHRW) < 0) {
124 syslog(LOG_ERR, "ioctl(I_FLUSH, FLUSHRW): %m");
128 * Find out which interface we were given.
129 * (ppp_if handles this ioctl)
131 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
132 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
136 /* Set debug flags in driver */
137 if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
138 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
143 * disestablish_ppp - Restore the serial port to normal operation.
144 * It attempts to reconstruct the stream with the previously popped
145 * modules. This shouldn't call die() because it's called from die().
154 /* we can't push or pop modules after the stream has hung up */
155 str_module_count = 0;
161 * Check whether the link seems not to be 8-bit clean.
163 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
165 switch (~flags & PAI_FLAGS_HIBITS) {
167 s = "bit 7 set to 1";
170 s = "bit 7 set to 0";
172 case PAI_FLAGS_PAR_EVEN:
175 case PAI_FLAGS_PAR_ODD:
180 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
181 syslog(LOG_WARNING, "All received characters had %s", s);
186 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
190 for (; str_module_count > 0; str_module_count--) {
191 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
192 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
193 str_modules[str_module_count-1].modname);
195 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
196 str_modules[str_module_count-1].modname));
203 * output - Output PPP packet.
214 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
216 log_packet(p, len, "sent ");
219 str.buf = (caddr_t) p;
220 if(putmsg(fd, NULL, &str, 0) < 0) {
221 syslog(LOG_ERR, "putmsg: %m");
228 * read_packet - get a PPP packet from the serial device.
237 str.maxlen = MTU+DLLHEADERLEN;
238 str.buf = (caddr_t) buf;
240 len = getmsg(fd, NULL, &str, &i);
242 if (errno == EAGAIN || errno == EWOULDBLOCK) {
245 syslog(LOG_ERR, "getmsg(fd) %m");
249 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
252 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
261 * ppp_send_config - configure the transmit characteristics of
265 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
273 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
275 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
276 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
280 if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
281 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
286 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
287 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
292 if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
293 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
300 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
303 ppp_set_xaccm(unit, accm)
307 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
308 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
313 * ppp_recv_config - configure the receive-side characteristics of
317 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
324 if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
325 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
328 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
329 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
332 c = 2 + (pcomp? 1: 0);
333 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
334 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
337 c = 2 + (accomp? 1: 0);
338 if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
339 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
344 * sifvjcomp - config tcp header compression
347 sifvjcomp(u, vjcomp, cidcomp, maxcid)
348 int u, vjcomp, cidcomp, maxcid;
352 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
353 if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
354 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
361 * sifup - Config the interface up.
369 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
370 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
371 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
374 ifr.ifr_flags |= IFF_UP;
375 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
376 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
383 * 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.
422 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
423 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
424 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
425 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
426 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
429 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
430 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
431 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
435 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
436 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
437 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
438 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
446 * cifaddr - Clear the interface IP addresses, and delete routes
447 * through the interface if possible.
456 SET_SA_FAMILY(rt.rt_dst, AF_INET);
457 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
458 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
459 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
460 rt.rt_flags = RTF_HOST;
461 if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
462 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
469 * sifdefaultroute - assign a default route through the address given.
472 sifdefaultroute(u, g)
478 SET_SA_FAMILY(rt.rt_dst, AF_INET);
479 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
480 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
481 rt.rt_flags = RTF_GATEWAY;
482 if (ioctl(s, SIOCADDRT, &rt) < 0) {
483 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
490 * cifdefaultroute - delete a default route through the address given.
493 cifdefaultroute(u, g)
499 SET_SA_FAMILY(rt.rt_dst, AF_INET);
500 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
501 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
502 rt.rt_flags = RTF_GATEWAY;
503 if (ioctl(s, SIOCDELRT, &rt) < 0) {
504 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
511 * sifproxyarp - Make a proxy ARP entry for the peer.
514 sifproxyarp(unit, hisaddr)
518 struct arpreq arpreq;
520 BZERO(&arpreq, sizeof(arpreq));
523 * Get the hardware address of an interface on the same subnet
524 * as our local address.
526 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
527 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
531 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
532 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
533 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
534 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
535 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
543 * cifproxyarp - Delete the proxy ARP entry for the peer.
546 cifproxyarp(unit, hisaddr)
550 struct arpreq arpreq;
552 BZERO(&arpreq, sizeof(arpreq));
553 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
554 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
555 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
556 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
563 * get_ether_addr - get the hardware address of an interface on the
564 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
565 * in the cslip-2.6 distribution, which is subject to the following
566 * copyright notice (which also applies to logwtmp below):
568 * Copyright (c) 1990, 1992 The Regents of the University of California.
569 * All rights reserved.
571 * Redistribution and use in source and binary forms, with or without
572 * modification, are permitted provided that: (1) source code distributions
573 * retain the above copyright notice and this paragraph in its entirety, (2)
574 * distributions including binary code include the above copyright notice and
575 * this paragraph in its entirety in the documentation or other materials
576 * provided with the distribution, and (3) all advertising materials mentioning
577 * features or use of this software display the following acknowledgement:
578 * ``This product includes software developed by the University of California,
579 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
580 * the University nor the names of its contributors may be used to endorse
581 * or promote products derived from this software without specific prior
582 * written permission.
583 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
584 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
585 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
591 #include <arpa/inet.h>
593 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
599 #include <netinet/in_var.h>
601 #include <netinet/if_ether.h>
603 /* Cast a struct sockaddr to a structaddr_in */
604 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
606 /* Determine if "bits" is set in "flag" */
607 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
609 static struct nlist nl[] = {
618 get_ether_addr(ipaddr, hwaddr)
620 struct sockaddr *hwaddr;
623 register struct ifnet *ifp;
624 register struct arpcom *ac;
625 struct arpcom arpcom;
628 register struct ifaddr *ifa;
629 register struct in_ifaddr *in;
637 /* Open kernel memory for reading */
638 kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
640 syslog(LOG_ERR, "kvm_open: %m");
645 if (kvm_nlist(kd, nl) != 0) {
646 syslog(LOG_ERR, "kvm_nlist failed");
657 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
659 syslog(LOG_ERR, "error reading ifnet addr");
662 for ( ; addr; addr = (u_long)ifp->if_next) {
663 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
664 syslog(LOG_ERR, "error reading ifnet");
668 /* Only look at configured, broadcast interfaces */
669 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
672 /* This probably can't happen... */
673 if (ifp->if_addrlist == 0)
677 /* Get interface ip address */
679 if (kvm_read(kd, (u_long)ifp->if_addrlist, (char *)&ifaddr,
680 sizeof(ifaddr)) != sizeof(ifaddr)) {
681 syslog(LOG_ERR, "error reading ifaddr");
684 inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
686 inp = &SATOSIN(&ifp->if_addr)->sin_addr;
689 /* Check if this interface on the right subnet */
691 mask = in->ia_subnetmask;
693 mask = ifp->if_subnetmask;
695 if ((ipaddr & mask) != (inp->s_addr & mask))
698 /* Copy out the local ethernet address */
699 hwaddr->sa_family = AF_UNSPEC;
700 BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
701 sizeof(arpcom.ac_enaddr));
702 return 1; /* success! */
705 /* couldn't find one */
709 #define WTMPFILE "/usr/adm/wtmp"
712 logwtmp(line, name, host)
713 char *line, *name, *host;
719 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
721 if (!fstat(fd, &buf)) {
722 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
723 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
724 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
725 (void)time(&ut.ut_time);
726 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
727 (void)ftruncate(fd, buf.st_size);