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.
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <sys/stream.h>
34 #include <sys/stropts.h>
37 #include <net/route.h>
38 #include <net/if_arp.h>
39 #include <netinet/in.h>
43 #include <net/if_ppp.h>
44 #include <net/ppp_str.h>
47 #define ifr_mtu ifr_metric
50 #define MAXMODULES 10 /* max number of module names to save */
51 static struct modlist {
52 char modname[FMNAMESZ+1];
53 } str_modules[MAXMODULES];
54 static int str_module_count = 0;
55 static int pushed_ppp;
57 extern int hungup; /* has the physical layer been disconnected? */
58 extern int kdebugflag;
60 #define PAI_FLAGS_B7_0 0x100
61 #define PAI_FLAGS_B7_1 0x200
62 #define PAI_FLAGS_PAR_EVEN 0x400
63 #define PAI_FLAGS_PAR_ODD 0x800
64 #define PAI_FLAGS_HIBITS 0xF00
67 * ppp_available - check if this kernel supports PPP.
74 fd = open("/dev/tty", O_RDONLY, 0);
76 return 1; /* can't find out - assume we have ppp */
77 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
84 * establish_ppp - Turn the serial port into a ppp interface.
89 /* go through and save the name of all the modules, then pop em */
91 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
92 ioctl(fd, I_POP, 0) < 0)
94 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
95 str_modules[str_module_count].modname));
99 /* now push the async/fcs module */
100 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
101 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
104 /* finally, push the ppp_if module that actually handles the */
105 /* network interface */
106 if (ioctl(fd, I_PUSH, "pppif") < 0) {
107 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
111 if (ioctl(fd, I_SETSIG, S_INPUT) < 0) {
112 syslog(LOG_ERR, "ioctl(I_SETSIG, S_INPUT): %m");
115 /* read mode, message non-discard mode */
116 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
117 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
120 /* Flush any waiting messages, or we'll never get SIGPOLL */
121 if (ioctl(fd, I_FLUSH, FLUSHRW) < 0) {
122 syslog(LOG_ERR, "ioctl(I_FLUSH, FLUSHRW): %m");
126 * Find out which interface we were given.
127 * (ppp_if handles this ioctl)
129 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
130 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
134 /* Set debug flags in driver */
135 if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
136 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
141 * disestablish_ppp - Restore the serial port to normal operation.
142 * It attempts to reconstruct the stream with the previously popped
143 * modules. This shouldn't call die() because it's called from die().
152 /* we can't push or pop modules after the stream has hung up */
153 str_module_count = 0;
159 * Check whether the link seems not to be 8-bit clean.
161 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
163 switch (~flags & PAI_FLAGS_HIBITS) {
165 s = "bit 7 set to 1";
168 s = "bit 7 set to 0";
170 case PAI_FLAGS_PAR_EVEN:
173 case PAI_FLAGS_PAR_ODD:
178 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
179 syslog(LOG_WARNING, "All received characters had %s", s);
184 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
188 for (; str_module_count > 0; str_module_count--) {
189 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
190 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
191 str_modules[str_module_count-1].modname);
193 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
194 str_modules[str_module_count-1].modname));
201 * output - Output PPP packet.
212 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
214 log_packet(p, len, "sent ");
217 str.buf = (caddr_t) p;
218 if(putmsg(fd, NULL, &str, 0) < 0) {
219 syslog(LOG_ERR, "putmsg: %m");
226 * read_packet - get a PPP packet from the serial device.
235 str.maxlen = MTU+DLLHEADERLEN;
236 str.buf = (caddr_t) buf;
238 len = getmsg(fd, NULL, &str, &i);
240 if (errno == EAGAIN || errno == EWOULDBLOCK) {
243 syslog(LOG_ERR, "getmsg(fd) %m");
247 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
250 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
259 * ppp_send_config - configure the transmit characteristics of
263 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
271 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
273 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
274 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
278 if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
279 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
284 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
285 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
290 if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
291 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
298 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
301 ppp_set_xaccm(unit, accm)
305 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
306 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
311 * ppp_recv_config - configure the receive-side characteristics of
315 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
322 if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
323 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
326 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
327 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
330 c = 2 + (pcomp? 1: 0);
331 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
332 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
335 c = 2 + (accomp? 1: 0);
336 if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
337 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
342 * sifvjcomp - config tcp header compression
345 sifvjcomp(u, vjcomp, cidcomp, maxcid)
346 int u, vjcomp, cidcomp, maxcid;
350 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
351 if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
352 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
359 * sifup - Config the interface up.
366 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
367 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
368 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
371 ifr.ifr_flags |= IFF_UP;
372 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
373 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
380 * sifdown - Config the interface down.
386 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
387 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
388 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
391 ifr.ifr_flags &= ~IFF_UP;
392 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
393 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
400 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
402 #define SET_SA_FAMILY(addr, family) \
403 BZERO((char *) &(addr), sizeof(addr)); \
404 addr.sa_family = (family);
407 * sifaddr - Config the interface IP addresses and netmask.
416 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
417 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
418 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
419 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
420 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
423 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
424 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
425 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
429 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
430 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
431 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
432 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
440 * cifaddr - Clear the interface IP addresses, and delete routes
441 * through the interface if possible.
448 SET_SA_FAMILY(rt.rt_dst, AF_INET);
449 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
450 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
451 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
452 rt.rt_flags = RTF_HOST;
453 if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
454 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
461 * sifdefaultroute - assign a default route through the address given.
464 sifdefaultroute(u, g)
468 SET_SA_FAMILY(rt.rt_dst, AF_INET);
469 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
470 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
471 rt.rt_flags = RTF_GATEWAY;
472 if (ioctl(s, SIOCADDRT, &rt) < 0) {
473 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
480 * cifdefaultroute - delete a default route through the address given.
483 cifdefaultroute(u, g)
487 SET_SA_FAMILY(rt.rt_dst, AF_INET);
488 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
489 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
490 rt.rt_flags = RTF_GATEWAY;
491 if (ioctl(s, SIOCDELRT, &rt) < 0) {
492 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
499 * sifproxyarp - Make a proxy ARP entry for the peer.
502 sifproxyarp(unit, hisaddr)
506 struct arpreq arpreq;
508 BZERO(&arpreq, sizeof(arpreq));
511 * Get the hardware address of an interface on the same subnet
512 * as our local address.
514 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
515 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
519 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
520 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
521 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
522 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
523 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
531 * cifproxyarp - Delete the proxy ARP entry for the peer.
534 cifproxyarp(unit, hisaddr)
538 struct arpreq arpreq;
540 BZERO(&arpreq, sizeof(arpreq));
541 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
542 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
543 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
544 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
551 * get_ether_addr - get the hardware address of an interface on the
552 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
553 * in the cslip-2.6 distribution, which is subject to the following
556 * Copyright (c) 1990, 1992 The Regents of the University of California.
557 * All rights reserved.
559 * Redistribution and use in source and binary forms, with or without
560 * modification, are permitted provided that: (1) source code distributions
561 * retain the above copyright notice and this paragraph in its entirety, (2)
562 * distributions including binary code include the above copyright notice and
563 * this paragraph in its entirety in the documentation or other materials
564 * provided with the distribution, and (3) all advertising materials mentioning
565 * features or use of this software display the following acknowledgement:
566 * ``This product includes software developed by the University of California,
567 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
568 * the University nor the names of its contributors may be used to endorse
569 * or promote products derived from this software without specific prior
570 * written permission.
571 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
572 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
573 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
579 #include <arpa/inet.h>
581 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
587 #include <netinet/in_var.h>
589 #include <netinet/if_ether.h>
591 /* Cast a struct sockaddr to a structaddr_in */
592 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
594 /* Determine if "bits" is set in "flag" */
595 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
597 static struct nlist nl[] = {
606 get_ether_addr(ipaddr, hwaddr)
608 struct sockaddr *hwaddr;
611 register struct ifnet *ifp;
612 register struct arpcom *ac;
613 struct arpcom arpcom;
616 register struct ifaddr *ifa;
617 register struct in_ifaddr *in;
625 /* Open kernel memory for reading */
626 kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
628 syslog(LOG_ERR, "kvm_open: %m");
633 if (kvm_nlist(kd, nl) != 0) {
634 syslog(LOG_ERR, "kvm_nlist failed");
645 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
647 syslog(LOG_ERR, "error reading ifnet addr");
650 for ( ; addr; addr = (u_long)ifp->if_next) {
651 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
652 syslog(LOG_ERR, "error reading ifnet");
656 /* Only look at configured, broadcast interfaces */
657 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
660 /* This probably can't happen... */
661 if (ifp->if_addrlist == 0)
665 /* Get interface ip address */
667 if (kvm_read(kd, (u_long)ifp->if_addrlist, (char *)&ifaddr,
668 sizeof(ifaddr)) != sizeof(ifaddr)) {
669 syslog(LOG_ERR, "error reading ifaddr");
672 inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
674 inp = &SATOSIN(&ifp->if_addr)->sin_addr;
677 /* Check if this interface on the right subnet */
679 mask = in->ia_subnetmask;
681 mask = ifp->if_subnetmask;
683 if ((ipaddr & mask) != (inp->s_addr & mask))
686 /* Copy out the local ethernet address */
687 hwaddr->sa_family = AF_UNSPEC;
688 BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
689 sizeof(arpcom.ac_enaddr));
690 return 1; /* success! */
693 /* couldn't find one */