1995-02-26 12:18:08 +00:00
|
|
|
/*
|
|
|
|
* sys-bsd.c - System-dependent procedures for setting up
|
|
|
|
* PPP interfaces on bsd-4.4-ish systems (including 386BSD, NetBSD, etc.)
|
|
|
|
*
|
2017-12-13 16:13:17 +00:00
|
|
|
* SPDX-License-Identifier: BSD-1-Clause
|
2017-11-27 15:37:16 +00:00
|
|
|
*
|
1995-02-26 12:18:08 +00:00
|
|
|
* Copyright (c) 1989 Carnegie Mellon University.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1999-08-28 01:35:59 +00:00
|
|
|
* $FreeBSD$
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-02-26 12:18:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO:
|
|
|
|
*/
|
|
|
|
|
1999-01-28 01:56:34 +00:00
|
|
|
#include <sys/param.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <sys/socket.h>
|
1995-02-26 12:18:08 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/if_ether.h>
|
1998-01-23 21:37:27 +00:00
|
|
|
#include <arpa/inet.h>
|
1998-05-21 21:49:08 +00:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <sys/un.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1999-04-26 08:54:25 +00:00
|
|
|
#include <errno.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
1998-01-19 02:59:34 +00:00
|
|
|
#include <stdlib.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <string.h>
|
1998-01-19 02:59:34 +00:00
|
|
|
#include <sys/sysctl.h>
|
1999-05-08 11:07:56 +00:00
|
|
|
#include <termios.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
#include "layer.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "mbuf.h"
|
1997-09-10 02:20:35 +00:00
|
|
|
#include "log.h"
|
1997-11-09 06:22:49 +00:00
|
|
|
#include "id.h"
|
1998-05-21 21:49:08 +00:00
|
|
|
#include "timer.h"
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "iplist.h"
|
|
|
|
#include "throughput.h"
|
|
|
|
#include "slcompress.h"
|
1998-08-26 17:39:37 +00:00
|
|
|
#include "lqr.h"
|
|
|
|
#include "hdlc.h"
|
2001-08-14 16:05:52 +00:00
|
|
|
#include "ncpaddr.h"
|
1998-05-21 21:49:08 +00:00
|
|
|
#include "ipcp.h"
|
2001-08-14 16:05:52 +00:00
|
|
|
#include "ipv6cp.h"
|
1998-05-21 21:49:08 +00:00
|
|
|
#include "descriptor.h"
|
|
|
|
#include "lcp.h"
|
|
|
|
#include "ccp.h"
|
|
|
|
#include "link.h"
|
|
|
|
#include "mp.h"
|
2001-08-14 16:05:52 +00:00
|
|
|
#include "ncp.h"
|
|
|
|
#include "filter.h"
|
1999-01-28 01:56:34 +00:00
|
|
|
#ifndef NORADIUS
|
|
|
|
#include "radius.h"
|
|
|
|
#endif
|
1998-05-21 21:49:08 +00:00
|
|
|
#include "bundle.h"
|
2000-03-14 01:46:44 +00:00
|
|
|
#include "iface.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "arp.h"
|
1995-02-26 12:18:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
|
|
|
|
* if it exists.
|
|
|
|
*/
|
|
|
|
#define SET_SA_FAMILY(addr, family) \
|
1997-10-26 01:04:02 +00:00
|
|
|
memset((char *) &(addr), '\0', sizeof(addr)); \
|
1995-02-26 12:18:08 +00:00
|
|
|
addr.sa_family = (family); \
|
|
|
|
addr.sa_len = sizeof(addr);
|
|
|
|
|
|
|
|
|
|
|
|
#if RTM_VERSION >= 3
|
|
|
|
|
|
|
|
/*
|
1998-05-21 21:49:08 +00:00
|
|
|
* arp_SetProxy - Make a proxy ARP entry for the peer.
|
1995-02-26 12:18:08 +00:00
|
|
|
*/
|
|
|
|
static struct {
|
1997-08-25 00:29:32 +00:00
|
|
|
struct rt_msghdr hdr;
|
2013-01-31 08:55:21 +00:00
|
|
|
struct sockaddr_in dst;
|
1997-08-25 00:29:32 +00:00
|
|
|
struct sockaddr_dl hwa;
|
|
|
|
char extra[128];
|
1997-10-26 01:04:02 +00:00
|
|
|
} arpmsg;
|
1995-02-26 12:18:08 +00:00
|
|
|
|
1998-10-26 19:07:39 +00:00
|
|
|
static int
|
2004-09-05 01:46:52 +00:00
|
|
|
arp_ProxySub(struct bundle *bundle, struct in_addr addr, int add)
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
int routes;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the hardware address of an interface on the same subnet as our local
|
|
|
|
* address.
|
|
|
|
*/
|
1998-10-26 19:07:39 +00:00
|
|
|
|
1997-12-24 09:29:17 +00:00
|
|
|
memset(&arpmsg, 0, sizeof arpmsg);
|
2004-09-05 01:46:52 +00:00
|
|
|
if (!arp_EtherAddr(addr, &arpmsg.hwa, 0)) {
|
1998-10-26 19:07:39 +00:00
|
|
|
log_Printf(LogWARN, "%s: Cannot determine ethernet address for proxy ARP\n",
|
|
|
|
inet_ntoa(addr));
|
1997-08-25 00:29:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-01-16 14:03:52 +00:00
|
|
|
routes = ID0socket(PF_ROUTE, SOCK_RAW, AF_INET);
|
1997-11-09 06:22:49 +00:00
|
|
|
if (routes < 0) {
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogERROR, "arp_SetProxy: opening routing socket: %s\n",
|
1997-08-25 00:29:32 +00:00
|
|
|
strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
1998-10-26 19:07:39 +00:00
|
|
|
arpmsg.hdr.rtm_type = add ? RTM_ADD : RTM_DELETE;
|
2010-03-18 00:23:39 +00:00
|
|
|
arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC | RTF_LLDATA;
|
1997-08-25 00:29:32 +00:00
|
|
|
arpmsg.hdr.rtm_version = RTM_VERSION;
|
1998-05-21 21:49:08 +00:00
|
|
|
arpmsg.hdr.rtm_seq = ++bundle->routing_seq;
|
1997-08-25 00:29:32 +00:00
|
|
|
arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
|
|
|
|
arpmsg.hdr.rtm_inits = RTV_EXPIRE;
|
2013-01-31 08:55:21 +00:00
|
|
|
arpmsg.dst.sin_len = sizeof(struct sockaddr_in);
|
1997-08-25 00:29:32 +00:00
|
|
|
arpmsg.dst.sin_family = AF_INET;
|
1998-05-21 21:49:08 +00:00
|
|
|
arpmsg.dst.sin_addr.s_addr = addr.s_addr;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
|
|
|
arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
|
|
|
|
+ arpmsg.hwa.sdl_len;
|
1998-10-26 19:07:39 +00:00
|
|
|
|
|
|
|
|
2001-09-21 16:14:53 +00:00
|
|
|
if (ID0write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0 &&
|
1998-10-26 19:07:39 +00:00
|
|
|
!(!add && errno == ESRCH)) {
|
|
|
|
log_Printf(LogERROR, "%s proxy arp entry %s: %s\n",
|
|
|
|
add ? "Add" : "Delete", inet_ntoa(addr), strerror(errno));
|
1995-02-26 12:18:08 +00:00
|
|
|
close(routes);
|
1997-08-25 00:29:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
close(routes);
|
|
|
|
return 1;
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
|
|
|
|
1998-10-26 19:07:39 +00:00
|
|
|
int
|
2004-09-05 01:46:52 +00:00
|
|
|
arp_SetProxy(struct bundle *bundle, struct in_addr addr)
|
1998-10-26 19:07:39 +00:00
|
|
|
{
|
2004-09-05 01:46:52 +00:00
|
|
|
return (arp_ProxySub(bundle, addr, 1));
|
1998-10-26 19:07:39 +00:00
|
|
|
}
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
/*
|
1998-05-21 21:49:08 +00:00
|
|
|
* arp_ClearProxy - Delete the proxy ARP entry for the peer.
|
1995-02-26 12:18:08 +00:00
|
|
|
*/
|
|
|
|
int
|
2004-09-05 01:46:52 +00:00
|
|
|
arp_ClearProxy(struct bundle *bundle, struct in_addr addr)
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
2004-09-05 01:46:52 +00:00
|
|
|
return (arp_ProxySub(bundle, addr, 0));
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
#else /* RTM_VERSION */
|
1995-02-26 12:18:08 +00:00
|
|
|
|
|
|
|
/*
|
1998-05-21 21:49:08 +00:00
|
|
|
* arp_SetProxy - Make a proxy ARP entry for the peer.
|
1995-02-26 12:18:08 +00:00
|
|
|
*/
|
|
|
|
int
|
1998-05-21 21:49:08 +00:00
|
|
|
arp_SetProxy(struct bundle *bundle, struct in_addr addr, int s)
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
struct arpreq arpreq;
|
|
|
|
struct {
|
|
|
|
struct sockaddr_dl sdl;
|
|
|
|
char space[128];
|
|
|
|
} dls;
|
|
|
|
|
1997-12-24 09:29:17 +00:00
|
|
|
memset(&arpreq, '\0', sizeof arpreq);
|
1997-08-25 00:29:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the hardware address of an interface on the same subnet as our local
|
|
|
|
* address.
|
|
|
|
*/
|
2004-09-05 01:46:52 +00:00
|
|
|
if (!arp_EtherAddr(addr, &dls.sdl, 1)) {
|
2000-10-30 00:15:29 +00:00
|
|
|
log_Printf(LOG_PHASE_BIT, "Cannot determine ethernet address for "
|
|
|
|
"proxy ARP\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
|
|
|
|
arpreq.arp_ha.sa_family = AF_UNSPEC;
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(arpreq.arp_ha.sa_data, LLADDR(&dls.sdl), dls.sdl.sdl_alen);
|
1997-08-25 00:29:32 +00:00
|
|
|
SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
|
1998-05-21 21:49:08 +00:00
|
|
|
((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr;
|
1997-08-25 00:29:32 +00:00
|
|
|
arpreq.arp_flags = ATF_PERM | ATF_PUBL;
|
1998-05-21 21:49:08 +00:00
|
|
|
if (ID0ioctl(s, SIOCSARP, (caddr_t) & arpreq) < 0) {
|
1998-06-16 19:40:42 +00:00
|
|
|
log_Printf(LogERROR, "arp_SetProxy: ioctl(SIOCSARP): %s\n",
|
|
|
|
strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-05-21 21:49:08 +00:00
|
|
|
* arp_ClearProxy - Delete the proxy ARP entry for the peer.
|
1995-02-26 12:18:08 +00:00
|
|
|
*/
|
|
|
|
int
|
1998-05-21 21:49:08 +00:00
|
|
|
arp_ClearProxy(struct bundle *bundle, struct in_addr addr, int s)
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
struct arpreq arpreq;
|
|
|
|
|
1997-12-24 09:29:17 +00:00
|
|
|
memset(&arpreq, '\0', sizeof arpreq);
|
1997-08-25 00:29:32 +00:00
|
|
|
SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
|
1998-05-21 21:49:08 +00:00
|
|
|
((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr;
|
|
|
|
if (ID0ioctl(s, SIOCDARP, (caddr_t) & arpreq) < 0) {
|
1998-06-16 19:40:42 +00:00
|
|
|
log_Printf(LogERROR, "arp_ClearProxy: ioctl(SIOCDARP): %s\n",
|
|
|
|
strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
|
|
|
|
#endif /* RTM_VERSION */
|
1995-02-26 12:18:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2001-07-31 15:19:07 +00:00
|
|
|
* arp_EtherAddr - get the hardware address of an interface on the
|
1995-02-26 12:18:08 +00:00
|
|
|
* the same subnet as ipaddr.
|
|
|
|
*/
|
|
|
|
|
1998-05-21 21:49:08 +00:00
|
|
|
int
|
2004-09-05 01:46:52 +00:00
|
|
|
arp_EtherAddr(struct in_addr ipaddr, struct sockaddr_dl *hwaddr,
|
2001-07-31 15:19:07 +00:00
|
|
|
int verbose)
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
2000-03-14 01:46:44 +00:00
|
|
|
int mib[6], skip;
|
1998-01-19 02:59:34 +00:00
|
|
|
size_t needed;
|
1998-01-23 21:37:27 +00:00
|
|
|
char *buf, *ptr, *end;
|
|
|
|
struct if_msghdr *ifm;
|
|
|
|
struct ifa_msghdr *ifam;
|
|
|
|
struct sockaddr_dl *dl;
|
2000-03-14 01:46:44 +00:00
|
|
|
struct sockaddr *sa[RTAX_MAX];
|
1998-01-19 02:59:34 +00:00
|
|
|
|
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = 0;
|
1998-01-23 21:37:27 +00:00
|
|
|
mib[4] = NET_RT_IFLIST;
|
1998-01-19 02:59:34 +00:00
|
|
|
mib[5] = 0;
|
1998-01-23 21:37:27 +00:00
|
|
|
|
1998-01-19 02:59:34 +00:00
|
|
|
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
|
2001-07-31 15:19:07 +00:00
|
|
|
log_Printf(LogERROR, "arp_EtherAddr: sysctl: estimate: %s\n",
|
1998-01-24 00:03:14 +00:00
|
|
|
strerror(errno));
|
1998-01-19 02:59:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1998-01-23 21:37:27 +00:00
|
|
|
|
|
|
|
if ((buf = malloc(needed)) == NULL)
|
1998-01-19 02:59:34 +00:00
|
|
|
return 0;
|
1998-01-23 21:37:27 +00:00
|
|
|
|
|
|
|
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
|
|
|
|
free(buf);
|
1998-01-19 02:59:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1998-01-23 21:37:27 +00:00
|
|
|
end = buf + needed;
|
|
|
|
|
|
|
|
ptr = buf;
|
|
|
|
while (ptr < end) {
|
|
|
|
ifm = (struct if_msghdr *)ptr; /* On if_msghdr */
|
|
|
|
if (ifm->ifm_type != RTM_IFINFO)
|
|
|
|
break;
|
|
|
|
dl = (struct sockaddr_dl *)(ifm + 1); /* Single _dl at end */
|
|
|
|
skip = (ifm->ifm_flags & (IFF_UP | IFF_BROADCAST | IFF_POINTOPOINT |
|
|
|
|
IFF_NOARP | IFF_LOOPBACK)) != (IFF_UP | IFF_BROADCAST);
|
|
|
|
ptr += ifm->ifm_msglen; /* First ifa_msghdr */
|
|
|
|
while (ptr < end) {
|
|
|
|
ifam = (struct ifa_msghdr *)ptr; /* Next ifa_msghdr (alias) */
|
|
|
|
if (ifam->ifam_type != RTM_NEWADDR) /* finished ? */
|
|
|
|
break;
|
|
|
|
ptr += ifam->ifam_msglen;
|
|
|
|
if (skip || (ifam->ifam_addrs & (RTA_NETMASK|RTA_IFA)) !=
|
|
|
|
(RTA_NETMASK|RTA_IFA))
|
|
|
|
continue;
|
|
|
|
/* Found a candidate. Do the addresses match ? */
|
1998-05-21 21:49:08 +00:00
|
|
|
if (log_IsKept(LogDEBUG) &&
|
1998-01-23 21:37:27 +00:00
|
|
|
ptr == (char *)ifm + ifm->ifm_msglen + ifam->ifam_msglen)
|
1998-05-21 21:49:08 +00:00
|
|
|
log_Printf(LogDEBUG, "%.*s interface is a candidate for proxy\n",
|
1998-01-23 21:37:27 +00:00
|
|
|
dl->sdl_nlen, dl->sdl_data);
|
2000-03-14 01:46:44 +00:00
|
|
|
|
|
|
|
iface_ParseHdr(ifam, sa);
|
|
|
|
|
|
|
|
if (sa[RTAX_IFA]->sa_family == AF_INET) {
|
|
|
|
struct sockaddr_in *ifa, *netmask;
|
|
|
|
|
|
|
|
ifa = (struct sockaddr_in *)sa[RTAX_IFA];
|
|
|
|
netmask = (struct sockaddr_in *)sa[RTAX_NETMASK];
|
|
|
|
|
|
|
|
if (log_IsKept(LogDEBUG)) {
|
|
|
|
char a[16];
|
|
|
|
|
|
|
|
strncpy(a, inet_ntoa(netmask->sin_addr), sizeof a - 1);
|
|
|
|
a[sizeof a - 1] = '\0';
|
|
|
|
log_Printf(LogDEBUG, "Check addr %s, mask %s\n",
|
|
|
|
inet_ntoa(ifa->sin_addr), a);
|
1998-01-19 02:59:34 +00:00
|
|
|
}
|
2000-03-14 01:46:44 +00:00
|
|
|
|
|
|
|
if ((ifa->sin_addr.s_addr & netmask->sin_addr.s_addr) ==
|
|
|
|
(ipaddr.s_addr & netmask->sin_addr.s_addr)) {
|
2001-07-31 15:19:07 +00:00
|
|
|
log_Printf(verbose ? LogPHASE : LogDEBUG,
|
2004-01-21 22:00:50 +00:00
|
|
|
"Found interface %.*s for %s\n", dl->sdl_nlen,
|
2001-07-31 15:19:07 +00:00
|
|
|
dl->sdl_data, inet_ntoa(ipaddr));
|
2000-03-14 01:46:44 +00:00
|
|
|
memcpy(hwaddr, dl, dl->sdl_len);
|
|
|
|
free(buf);
|
|
|
|
return 1;
|
1998-01-23 21:37:27 +00:00
|
|
|
}
|
1998-01-19 02:59:34 +00:00
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1998-01-23 21:37:27 +00:00
|
|
|
free(buf);
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
return 0;
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|