* sys-str.c - System-dependent procedures for setting up
* PPP interfaces on systems which use the STREAMS ppp interface.
* Copyright (c) 1989 Carnegie Mellon University.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by Carnegie Mellon University. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#define ifr_mtu ifr_metric
#define MAXMODULES 10 /* max number of module names to save */
char modname
[FMNAMESZ
+1];
} str_modules
[MAXMODULES
];
static int str_module_count
= 0;
extern int hungup
; /* has the physical layer been disconnected? */
* ppp_available - check if this kernel supports PPP.
fd
= open("/dev/tty", O_RDONLY
, 0);
return 1; /* can't find out - assume we have ppp */
ret
= ioctl(fd
, I_FIND
, "pppasync") >= 0;
* establish_ppp - Turn the serial port into a ppp interface.
/* go through and save the name of all the modules, then pop em */
if (ioctl(fd
, I_LOOK
, str_modules
[str_module_count
].modname
) < 0 ||
MAINDEBUG((LOG_DEBUG
, "popped stream module : %s",
str_modules
[str_module_count
].modname
));
MAINDEBUG((LOG_INFO
, "about to push modules..."));
/* now push the async/fcs module */
if (ioctl(fd
, I_PUSH
, "pppasync") < 0) {
syslog(LOG_ERR
, "ioctl(I_PUSH, ppp_async): %m");
/* finally, push the ppp_if module that actually handles the */
if (ioctl(fd
, I_PUSH
, "pppif") < 0) {
syslog(LOG_ERR
, "ioctl(I_PUSH, ppp_if): %m");
if (ioctl(fd
, I_SETSIG
, S_INPUT
) < 0) {
syslog(LOG_ERR
, "ioctl(I_SETSIG, S_INPUT): %m");
/* read mode, message non-discard mode */
if (ioctl(fd
, I_SRDOPT
, RMSGN
) < 0) {
syslog(LOG_ERR
, "ioctl(I_SRDOPT, RMSGN): %m");
/* Flush any waiting messages, or we'll never get SIGPOLL */
if (ioctl(fd
, I_FLUSH
, FLUSHRW
) < 0) {
syslog(LOG_ERR
, "ioctl(I_FLUSH, FLUSHRW): %m");
* Find out which interface we were given.
* (ppp_if handles this ioctl)
if (ioctl(fd
, SIOCGETU
, &ifunit
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCGETU): %m");
/* if debug, set debug flags in driver */
int flags
= debug
? 0x3 : 0;
if (ioctl(fd
, SIOCSIFDEBUG
, &flags
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFDEBUG): %m");
MAINDEBUG((LOG_INFO
, "done pushing modules, ifunit %d", ifunit
));
* disestablish_ppp - Restore the serial port to normal operation.
* It attempts to reconstruct the stream with the previously popped
* modules. This shouldn't call die() because it's called from die().
/* we can't push or pop modules after the stream has hung up */
while (ioctl(fd
, I_POP
, 0) == 0) /* pop any we pushed */
for (; str_module_count
> 0; str_module_count
--) {
if (ioctl(fd
, I_PUSH
, str_modules
[str_module_count
-1].modname
)) {
syslog(LOG_ERR
, "str_restore: couldn't push module %s: %m",
str_modules
[str_module_count
-1].modname
);
MAINDEBUG((LOG_INFO
, "str_restore: pushed module %s",
str_modules
[str_module_count
-1].modname
));
* output - Output PPP packet.
MAINDEBUG((LOG_WARNING
, "output: unit != 0!"));
if(putmsg(fd
, NULL
, &str
, 0) < 0) {
syslog(LOG_ERR
, "putmsg: %m");
* read_packet - get a PPP packet from the serial device.
str
.maxlen
= MTU
+DLLHEADERLEN
;
len
= getmsg(fd
, NULL
, &str
, &i
);
if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
syslog(LOG_ERR
, "getmsg(fd) %m");
MAINDEBUG((LOG_DEBUG
, "getmsg returned 0x%x",len
));
MAINDEBUG((LOG_DEBUG
, "getmsg short return length %d", str
.len
));
* ppp_send_config - configure the transmit characteristics of
ppp_send_config(unit
, mtu
, asyncmap
, pcomp
, accomp
)
strncpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
if (ioctl(s
, SIOCSIFMTU
, (caddr_t
) &ifr
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFMTU): %m");
if(ioctl(fd
, SIOCSIFASYNCMAP
, (caddr_t
) &asyncmap
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFASYNCMAP): %m");
if(ioctl(fd
, SIOCSIFCOMPPROT
, &c
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFCOMPPROT): %m");
if(ioctl(fd
, SIOCSIFCOMPAC
, &c
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFCOMPAC): %m");
* ppp_recv_config - configure the receive-side characteristics of
* the ppp interface. At present this just sets the MRU.
ppp_recv_config(unit
, mru
, asyncmap
, pcomp
, accomp
)
if (ioctl(fd
, SIOCSIFMRU
, &mru
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFMRU): %m");
if(ioctl(fd
, SIOCSIFRASYNCMAP
, (caddr_t
) &asyncmap
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFRASYNCMAP): %m");
if(ioctl(fd
, SIOCSIFRCOMPAC
, &c
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFRCOMPAC): %m");
* sifvjcomp - config tcp header compression
sifvjcomp(u
, vjcomp
, cidcomp
)
if(ioctl(fd
, SIOCSIFVJCOMP
, (caddr_t
) &x
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFVJCOMP): %m");
* sifup - Config the interface up.
strncpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
) &ifr
) < 0) {
syslog(LOG_ERR
, "ioctl (SIOCGIFFLAGS): %m");
if (ioctl(s
, SIOCSIFFLAGS
, (caddr_t
) &ifr
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFFLAGS): %m");
* sifdown - Config the interface down.
strncpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
) &ifr
) < 0) {
syslog(LOG_ERR
, "ioctl (SIOCGIFFLAGS): %m");
ifr
.ifr_flags
&= ~IFF_UP
;
if (ioctl(s
, SIOCSIFFLAGS
, (caddr_t
) &ifr
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFFLAGS): %m");
* SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
#define SET_SA_FAMILY(addr, family) \
BZERO((char *) &(addr), sizeof(addr)); \
addr.sa_family = (family);
* sifaddr - Config the interface IP addresses and netmask.
strncpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
SET_SA_FAMILY(ifr
.ifr_addr
, AF_INET
);
((struct sockaddr_in
*) &ifr
.ifr_addr
)->sin_addr
.s_addr
= o
;
if (ioctl(s
, SIOCSIFADDR
, (caddr_t
) &ifr
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFADDR): %m");
((struct sockaddr_in
*) &ifr
.ifr_dstaddr
)->sin_addr
.s_addr
= h
;
if (ioctl(s
, SIOCSIFDSTADDR
, (caddr_t
) &ifr
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFDSTADDR): %m");
((struct sockaddr_in
*) &ifr
.ifr_addr
)->sin_addr
.s_addr
= m
;
syslog(LOG_INFO
, "Setting interface mask to %s\n", ip_ntoa(m
));
if (ioctl(s
, SIOCSIFNETMASK
, (caddr_t
) &ifr
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSIFNETMASK): %m");
* cifaddr - Clear the interface IP addresses, and delete routes
* through the interface if possible.
SET_SA_FAMILY(rt
.rt_dst
, AF_INET
);
((struct sockaddr_in
*) &rt
.rt_dst
)->sin_addr
.s_addr
= h
;
SET_SA_FAMILY(rt
.rt_gateway
, AF_INET
);
((struct sockaddr_in
*) &rt
.rt_gateway
)->sin_addr
.s_addr
= o
;
if (ioctl(s
, SIOCDELRT
, (caddr_t
) &rt
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCDELRT): %m");
* sifdefaultroute - assign a default route through the address given.
SET_SA_FAMILY(rt
.rt_dst
, AF_INET
);
SET_SA_FAMILY(rt
.rt_gateway
, AF_INET
);
((struct sockaddr_in
*) &rt
.rt_gateway
)->sin_addr
.s_addr
= g
;
rt
.rt_flags
= RTF_GATEWAY
;
if (ioctl(s
, SIOCADDRT
, &rt
) < 0) {
syslog(LOG_ERR
, "default route ioctl(SIOCADDRT): %m");
* cifdefaultroute - delete a default route through the address given.
SET_SA_FAMILY(rt
.rt_dst
, AF_INET
);
SET_SA_FAMILY(rt
.rt_gateway
, AF_INET
);
((struct sockaddr_in
*) &rt
.rt_gateway
)->sin_addr
.s_addr
= g
;
rt
.rt_flags
= RTF_GATEWAY
;
if (ioctl(s
, SIOCDELRT
, &rt
) < 0) {
syslog(LOG_ERR
, "default route ioctl(SIOCDELRT): %m");
* sifproxyarp - Make a proxy ARP entry for the peer.
sifproxyarp(unit
, hisaddr
)
BZERO(&arpreq
, sizeof(arpreq
));
* Get the hardware address of an interface on the same subnet
if (!get_ether_addr(hisaddr
, &arpreq
.arp_ha
)) {
syslog(LOG_ERR
, "Cannot determine ethernet address for proxy ARP");
SET_SA_FAMILY(arpreq
.arp_pa
, AF_INET
);
((struct sockaddr_in
*) &arpreq
.arp_pa
)->sin_addr
.s_addr
= hisaddr
;
arpreq
.arp_flags
= ATF_PERM
| ATF_PUBL
;
if (ioctl(s
, SIOCSARP
, (caddr_t
)&arpreq
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCSARP): %m");
* cifproxyarp - Delete the proxy ARP entry for the peer.
cifproxyarp(unit
, hisaddr
)
BZERO(&arpreq
, sizeof(arpreq
));
SET_SA_FAMILY(arpreq
.arp_pa
, AF_INET
);
((struct sockaddr_in
*) &arpreq
.arp_pa
)->sin_addr
.s_addr
= hisaddr
;
if (ioctl(s
, SIOCDARP
, (caddr_t
)&arpreq
) < 0) {
syslog(LOG_ERR
, "ioctl(SIOCDARP): %m");
* get_ether_addr - get the hardware address of an interface on the
* the same subnet as ipaddr. Code borrowed from myetheraddr.c
* in the cslip-2.6 distribution, which is subject to the following
* Copyright (c) 1990, 1992 The Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the University of California,
* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
* the University nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
/* XXX SunOS 4.1 defines this and 3.5 doesn't... */
#include <netinet/in_var.h>
#include <netinet/if_ether.h>
/* Cast a struct sockaddr to a structaddr_in */
#define SATOSIN(sa) ((struct sockaddr_in *)(sa))
/* Determine if "bits" is set in "flag" */
#define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
static struct nlist nl
[] = {
get_ether_addr(ipaddr
, hwaddr
)
register struct ifnet
*ifp
;
register struct arpcom
*ac
;
register struct ifaddr
*ifa
;
register struct in_ifaddr
*in
;
/* Open kernel memory for reading */
kd
= kvm_open(0, 0, 0, O_RDONLY
, NULL
);
syslog(LOG_ERR
, "kvm_open: %m");
if (kvm_nlist(kd
, nl
) != 0) {
syslog(LOG_ERR
, "kvm_nlist failed");
if (kvm_read(kd
, nl
[N_IFNET
].n_value
, (char *)&addr
, sizeof(addr
))
syslog(LOG_ERR
, "error reading ifnet addr");
for ( ; addr
; addr
= (u_long
)ifp
->if_next
) {
if (kvm_read(kd
, addr
, (char *)ac
, sizeof(*ac
)) != sizeof(*ac
)) {
syslog(LOG_ERR
, "error reading ifnet");
/* Only look at configured, broadcast interfaces */
if (!ALLSET(ifp
->if_flags
, IFF_UP
| IFF_BROADCAST
))
/* This probably can't happen... */
if (ifp
->if_addrlist
== 0)
/* Get interface ip address */
if (kvm_read(kd
, (u_long
)ifp
->if_addrlist
, (char *)&ifaddr
,
sizeof(ifaddr
)) != sizeof(ifaddr
)) {
syslog(LOG_ERR
, "error reading ifaddr");
inp
= &SATOSIN(&ifa
->ifa_addr
)->sin_addr
;
inp
= &SATOSIN(&ifp
->if_addr
)->sin_addr
;
/* Check if this interface on the right subnet */
mask
= in
->ia_subnetmask
;
mask
= ifp
->if_subnetmask
;
if ((ipaddr
& mask
) != (inp
->s_addr
& mask
))
/* Copy out the local ethernet address */
hwaddr
->sa_family
= AF_UNSPEC
;
BCOPY((caddr_t
) &arpcom
.ac_enaddr
, hwaddr
->sa_data
,
sizeof(arpcom
.ac_enaddr
));