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/if_ppp.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;
60 extern int hungup; /* has the physical layer been disconnected? */
61 extern int kdebugflag;
63 #define PAI_FLAGS_B7_0 0x100
64 #define PAI_FLAGS_B7_1 0x200
65 #define PAI_FLAGS_PAR_EVEN 0x400
66 #define PAI_FLAGS_PAR_ODD 0x800
67 #define PAI_FLAGS_HIBITS 0xF00
70 * ppp_available - check if this kernel supports PPP.
77 fd = open("/dev/tty", O_RDONLY, 0);
79 return 1; /* can't find out - assume we have ppp */
80 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
87 * establish_ppp - Turn the serial port into a ppp interface.
92 /* go through and save the name of all the modules, then pop em */
94 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
95 ioctl(fd, I_POP, 0) < 0)
97 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
98 str_modules[str_module_count].modname));
102 /* now push the async/fcs module */
103 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
104 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
107 /* finally, push the ppp_if module that actually handles the */
108 /* network interface */
109 if (ioctl(fd, I_PUSH, "pppif") < 0) {
110 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
114 if (ioctl(fd, I_SETSIG, S_INPUT) < 0) {
115 syslog(LOG_ERR, "ioctl(I_SETSIG, S_INPUT): %m");
118 /* read mode, message non-discard mode */
119 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
120 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
123 /* Flush any waiting messages, or we'll never get SIGPOLL */
124 if (ioctl(fd, I_FLUSH, FLUSHRW) < 0) {
125 syslog(LOG_ERR, "ioctl(I_FLUSH, FLUSHRW): %m");
129 * Find out which interface we were given.
130 * (ppp_if handles this ioctl)
132 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
133 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
137 /* Set debug flags in driver */
138 if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
139 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
144 * disestablish_ppp - Restore the serial port to normal operation.
145 * It attempts to reconstruct the stream with the previously popped
146 * modules. This shouldn't call die() because it's called from die().
155 /* we can't push or pop modules after the stream has hung up */
156 str_module_count = 0;
162 * Check whether the link seems not to be 8-bit clean.
164 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
166 switch (~flags & PAI_FLAGS_HIBITS) {
168 s = "bit 7 set to 1";
171 s = "bit 7 set to 0";
173 case PAI_FLAGS_PAR_EVEN:
176 case PAI_FLAGS_PAR_ODD:
181 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
182 syslog(LOG_WARNING, "All received characters had %s", s);
187 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
191 for (; str_module_count > 0; str_module_count--) {
192 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
193 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
194 str_modules[str_module_count-1].modname);
196 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
197 str_modules[str_module_count-1].modname));
204 * output - Output PPP packet.
215 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
217 log_packet(p, len, "sent ");
220 str.buf = (caddr_t) p;
221 if(putmsg(fd, NULL, &str, 0) < 0) {
222 syslog(LOG_ERR, "putmsg: %m");
229 * read_packet - get a PPP packet from the serial device.
238 str.maxlen = MTU+DLLHEADERLEN;
239 str.buf = (caddr_t) buf;
241 len = getmsg(fd, NULL, &str, &i);
243 if (errno == EAGAIN || errno == EWOULDBLOCK) {
246 syslog(LOG_ERR, "getmsg(fd) %m");
250 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
253 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
262 * ppp_send_config - configure the transmit characteristics of
266 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
274 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
276 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
277 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
281 if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
282 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
287 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
288 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
293 if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
294 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
301 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
304 ppp_set_xaccm(unit, accm)
308 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
309 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
314 * ppp_recv_config - configure the receive-side characteristics of
318 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
325 if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
326 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
329 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
330 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
333 c = 2 + (pcomp? 1: 0);
334 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
335 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
338 c = 2 + (accomp? 1: 0);
339 if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
340 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
345 * sifvjcomp - config tcp header compression
348 sifvjcomp(u, vjcomp, cidcomp, maxcid)
349 int u, vjcomp, cidcomp, maxcid;
353 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
354 if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
355 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
362 * 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.
391 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
392 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
393 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
396 ifr.ifr_flags &= ~IFF_UP;
397 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
398 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
405 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
407 #define SET_SA_FAMILY(addr, family) \
408 BZERO((char *) &(addr), sizeof(addr)); \
409 addr.sa_family = (family);
412 * sifaddr - Config the interface IP addresses and netmask.
423 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
424 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
425 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
426 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
427 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
430 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
431 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
432 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
436 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
437 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
438 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
439 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
447 * cifaddr - Clear the interface IP addresses, and delete routes
448 * through the interface if possible.
457 SET_SA_FAMILY(rt.rt_dst, AF_INET);
458 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
459 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
460 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
461 rt.rt_flags = RTF_HOST;
462 if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
463 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
470 * sifdefaultroute - assign a default route through the address given.
473 sifdefaultroute(u, g)
479 SET_SA_FAMILY(rt.rt_dst, AF_INET);
480 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
481 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
482 rt.rt_flags = RTF_GATEWAY;
483 if (ioctl(s, SIOCADDRT, &rt) < 0) {
484 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
491 * cifdefaultroute - delete a default route through the address given.
494 cifdefaultroute(u, g)
500 SET_SA_FAMILY(rt.rt_dst, AF_INET);
501 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
502 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
503 rt.rt_flags = RTF_GATEWAY;
504 if (ioctl(s, SIOCDELRT, &rt) < 0) {
505 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
512 * sifproxyarp - Make a proxy ARP entry for the peer.
515 sifproxyarp(unit, hisaddr)
519 struct arpreq arpreq;
521 BZERO(&arpreq, sizeof(arpreq));
524 * Get the hardware address of an interface on the same subnet
525 * as our local address.
527 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
528 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
532 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
533 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
534 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
535 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
536 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
544 * cifproxyarp - Delete the proxy ARP entry for the peer.
547 cifproxyarp(unit, hisaddr)
551 struct arpreq arpreq;
553 BZERO(&arpreq, sizeof(arpreq));
554 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
555 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
556 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
557 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
564 * get_ether_addr - get the hardware address of an interface on the
565 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
566 * in the cslip-2.6 distribution, which is subject to the following
567 * copyright notice (which also applies to logwtmp below):
569 * Copyright (c) 1990, 1992 The Regents of the University of California.
570 * All rights reserved.
572 * Redistribution and use in source and binary forms, with or without
573 * modification, are permitted provided that: (1) source code distributions
574 * retain the above copyright notice and this paragraph in its entirety, (2)
575 * distributions including binary code include the above copyright notice and
576 * this paragraph in its entirety in the documentation or other materials
577 * provided with the distribution, and (3) all advertising materials mentioning
578 * features or use of this software display the following acknowledgement:
579 * ``This product includes software developed by the University of California,
580 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
581 * the University nor the names of its contributors may be used to endorse
582 * or promote products derived from this software without specific prior
583 * written permission.
584 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
585 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
586 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
592 #include <arpa/inet.h>
594 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
600 #include <netinet/in_var.h>
602 #include <netinet/if_ether.h>
604 /* Cast a struct sockaddr to a structaddr_in */
605 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
607 /* Determine if "bits" is set in "flag" */
608 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
610 static struct nlist nl[] = {
619 get_ether_addr(ipaddr, hwaddr)
621 struct sockaddr *hwaddr;
624 register struct ifnet *ifp;
625 register struct arpcom *ac;
626 struct arpcom arpcom;
629 register struct ifaddr *ifa;
630 register struct in_ifaddr *in;
638 /* Open kernel memory for reading */
639 kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
641 syslog(LOG_ERR, "kvm_open: %m");
646 if (kvm_nlist(kd, nl) != 0) {
647 syslog(LOG_ERR, "kvm_nlist failed");
658 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
660 syslog(LOG_ERR, "error reading ifnet addr");
663 for ( ; addr; addr = (u_long)ifp->if_next) {
664 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
665 syslog(LOG_ERR, "error reading ifnet");
669 /* Only look at configured, broadcast interfaces */
670 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
673 /* This probably can't happen... */
674 if (ifp->if_addrlist == 0)
678 /* Get interface ip address */
680 if (kvm_read(kd, (u_long)ifp->if_addrlist, (char *)&ifaddr,
681 sizeof(ifaddr)) != sizeof(ifaddr)) {
682 syslog(LOG_ERR, "error reading ifaddr");
685 inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
687 inp = &SATOSIN(&ifp->if_addr)->sin_addr;
690 /* Check if this interface on the right subnet */
692 mask = in->ia_subnetmask;
694 mask = ifp->if_subnetmask;
696 if ((ipaddr & mask) != (inp->s_addr & mask))
699 /* Copy out the local ethernet address */
700 hwaddr->sa_family = AF_UNSPEC;
701 BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
702 sizeof(arpcom.ac_enaddr));
703 return 1; /* success! */
706 /* couldn't find one */
710 #define WTMPFILE "/usr/adm/wtmp"
713 logwtmp(line, name, host)
714 char *line, *name, *host;
720 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
722 if (!fstat(fd, &buf)) {
723 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
724 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
725 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
726 (void)time(&ut.ut_time);
727 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
728 (void)ftruncate(fd, buf.st_size);