153c9f6c0SAtsushi Murai /* 253c9f6c0SAtsushi Murai * sys-bsd.c - System-dependent procedures for setting up 353c9f6c0SAtsushi Murai * PPP interfaces on bsd-4.4-ish systems (including 386BSD, NetBSD, etc.) 453c9f6c0SAtsushi Murai * 553c9f6c0SAtsushi Murai * Copyright (c) 1989 Carnegie Mellon University. 653c9f6c0SAtsushi Murai * All rights reserved. 753c9f6c0SAtsushi Murai * 853c9f6c0SAtsushi Murai * Redistribution and use in source and binary forms are permitted 953c9f6c0SAtsushi Murai * provided that the above copyright notice and this paragraph are 1053c9f6c0SAtsushi Murai * duplicated in all such forms and that any documentation, 1153c9f6c0SAtsushi Murai * advertising materials, and other materials related to such 1253c9f6c0SAtsushi Murai * distribution and use acknowledge that the software was developed 1353c9f6c0SAtsushi Murai * by Carnegie Mellon University. The name of the 1453c9f6c0SAtsushi Murai * University may not be used to endorse or promote products derived 1553c9f6c0SAtsushi Murai * from this software without specific prior written permission. 1653c9f6c0SAtsushi Murai * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1753c9f6c0SAtsushi Murai * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1853c9f6c0SAtsushi Murai * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1953c9f6c0SAtsushi Murai * 2097d92980SPeter Wemm * $FreeBSD$ 2153c9f6c0SAtsushi Murai * 2253c9f6c0SAtsushi Murai */ 2353c9f6c0SAtsushi Murai 2453c9f6c0SAtsushi Murai /* 2553c9f6c0SAtsushi Murai * TODO: 2653c9f6c0SAtsushi Murai */ 2753c9f6c0SAtsushi Murai 28972a1bcfSBrian Somers #include <sys/param.h> 2975240ed1SBrian Somers #include <sys/socket.h> 3053c9f6c0SAtsushi Murai #include <net/if.h> 3153c9f6c0SAtsushi Murai #include <net/route.h> 3253c9f6c0SAtsushi Murai #include <net/if_dl.h> 3353c9f6c0SAtsushi Murai #include <netinet/in.h> 3453c9f6c0SAtsushi Murai #include <netinet/if_ether.h> 35813bfe4dSBrian Somers #include <arpa/inet.h> 36eaa4df37SBrian Somers #include <netinet/in_systm.h> 37eaa4df37SBrian Somers #include <netinet/ip.h> 381fa665f5SBrian Somers #include <sys/un.h> 3975240ed1SBrian Somers 40119386a3SBrian Somers #include <errno.h> 4175240ed1SBrian Somers #include <stdio.h> 42c6fe0cb2SBrian Somers #include <stdlib.h> 4375240ed1SBrian Somers #include <string.h> 44c6fe0cb2SBrian Somers #include <sys/sysctl.h> 455d9e6103SBrian Somers #include <termios.h> 4675240ed1SBrian Somers #include <unistd.h> 4775240ed1SBrian Somers 485d9e6103SBrian Somers #include "layer.h" 4975240ed1SBrian Somers #include "mbuf.h" 5010a91a42SBrian Somers #include "log.h" 515106c671SBrian Somers #include "id.h" 52455aabc3SBrian Somers #include "timer.h" 53455aabc3SBrian Somers #include "fsm.h" 54455aabc3SBrian Somers #include "defs.h" 55455aabc3SBrian Somers #include "iplist.h" 565828db6dSBrian Somers #include "throughput.h" 57eaa4df37SBrian Somers #include "slcompress.h" 585a72b6edSBrian Somers #include "lqr.h" 595a72b6edSBrian Somers #include "hdlc.h" 6030949fd4SBrian Somers #include "ncpaddr.h" 615828db6dSBrian Somers #include "ipcp.h" 6230949fd4SBrian Somers #include "ipv6cp.h" 632f786681SBrian Somers #include "descriptor.h" 643b0f8d2eSBrian Somers #include "lcp.h" 653b0f8d2eSBrian Somers #include "ccp.h" 663b0f8d2eSBrian Somers #include "link.h" 673b0f8d2eSBrian Somers #include "mp.h" 6830949fd4SBrian Somers #include "ncp.h" 6930949fd4SBrian Somers #include "filter.h" 70972a1bcfSBrian Somers #ifndef NORADIUS 71972a1bcfSBrian Somers #include "radius.h" 72972a1bcfSBrian Somers #endif 735828db6dSBrian Somers #include "bundle.h" 746b457978SBrian Somers #include "iface.h" 7575240ed1SBrian Somers #include "arp.h" 7653c9f6c0SAtsushi Murai 7753c9f6c0SAtsushi Murai /* 7853c9f6c0SAtsushi Murai * SET_SA_FAMILY - set the sa_family field of a struct sockaddr, 7953c9f6c0SAtsushi Murai * if it exists. 8053c9f6c0SAtsushi Murai */ 8153c9f6c0SAtsushi Murai #define SET_SA_FAMILY(addr, family) \ 8275240ed1SBrian Somers memset((char *) &(addr), '\0', sizeof(addr)); \ 8353c9f6c0SAtsushi Murai addr.sa_family = (family); \ 8453c9f6c0SAtsushi Murai addr.sa_len = sizeof(addr); 8553c9f6c0SAtsushi Murai 8653c9f6c0SAtsushi Murai 8753c9f6c0SAtsushi Murai #if RTM_VERSION >= 3 8853c9f6c0SAtsushi Murai 8953c9f6c0SAtsushi Murai /* 90dd7e2610SBrian Somers * arp_SetProxy - Make a proxy ARP entry for the peer. 9153c9f6c0SAtsushi Murai */ 9253c9f6c0SAtsushi Murai static struct { 9353c9f6c0SAtsushi Murai struct rt_msghdr hdr; 94*9711a168SGleb Smirnoff struct sockaddr_in dst; 9553c9f6c0SAtsushi Murai struct sockaddr_dl hwa; 9653c9f6c0SAtsushi Murai char extra[128]; 9753c9f6c0SAtsushi Murai } arpmsg; 9853c9f6c0SAtsushi Murai 993afe5ccbSBrian Somers static int 100057f1760SBrian Somers arp_ProxySub(struct bundle *bundle, struct in_addr addr, int add) 10153c9f6c0SAtsushi Murai { 10253c9f6c0SAtsushi Murai int routes; 10353c9f6c0SAtsushi Murai 10453c9f6c0SAtsushi Murai /* 105944f7098SBrian Somers * Get the hardware address of an interface on the same subnet as our local 106944f7098SBrian Somers * address. 10753c9f6c0SAtsushi Murai */ 1083afe5ccbSBrian Somers 10970ee81ffSBrian Somers memset(&arpmsg, 0, sizeof arpmsg); 110057f1760SBrian Somers if (!arp_EtherAddr(addr, &arpmsg.hwa, 0)) { 1113afe5ccbSBrian Somers log_Printf(LogWARN, "%s: Cannot determine ethernet address for proxy ARP\n", 1123afe5ccbSBrian Somers inet_ntoa(addr)); 11353c9f6c0SAtsushi Murai return 0; 11453c9f6c0SAtsushi Murai } 11563c6cac9SBrian Somers routes = ID0socket(PF_ROUTE, SOCK_RAW, AF_INET); 1165106c671SBrian Somers if (routes < 0) { 117dd7e2610SBrian Somers log_Printf(LogERROR, "arp_SetProxy: opening routing socket: %s\n", 118927145beSBrian Somers strerror(errno)); 11953c9f6c0SAtsushi Murai return 0; 12053c9f6c0SAtsushi Murai } 1213afe5ccbSBrian Somers arpmsg.hdr.rtm_type = add ? RTM_ADD : RTM_DELETE; 12278f3ac9cSQing Li arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC | RTF_LLDATA; 12353c9f6c0SAtsushi Murai arpmsg.hdr.rtm_version = RTM_VERSION; 124820de6ebSBrian Somers arpmsg.hdr.rtm_seq = ++bundle->routing_seq; 12553c9f6c0SAtsushi Murai arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY; 12653c9f6c0SAtsushi Murai arpmsg.hdr.rtm_inits = RTV_EXPIRE; 127*9711a168SGleb Smirnoff arpmsg.dst.sin_len = sizeof(struct sockaddr_in); 12853c9f6c0SAtsushi Murai arpmsg.dst.sin_family = AF_INET; 129503a7782SBrian Somers arpmsg.dst.sin_addr.s_addr = addr.s_addr; 13053c9f6c0SAtsushi Murai 13153c9f6c0SAtsushi Murai arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg 13253c9f6c0SAtsushi Murai + arpmsg.hwa.sdl_len; 1333afe5ccbSBrian Somers 1343afe5ccbSBrian Somers 13572736552SRuslan Ermilov if (ID0write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0 && 1363afe5ccbSBrian Somers !(!add && errno == ESRCH)) { 1373afe5ccbSBrian Somers log_Printf(LogERROR, "%s proxy arp entry %s: %s\n", 1383afe5ccbSBrian Somers add ? "Add" : "Delete", inet_ntoa(addr), strerror(errno)); 13953c9f6c0SAtsushi Murai close(routes); 14053c9f6c0SAtsushi Murai return 0; 14153c9f6c0SAtsushi Murai } 14253c9f6c0SAtsushi Murai close(routes); 14353c9f6c0SAtsushi Murai return 1; 14453c9f6c0SAtsushi Murai } 14553c9f6c0SAtsushi Murai 1463afe5ccbSBrian Somers int 147057f1760SBrian Somers arp_SetProxy(struct bundle *bundle, struct in_addr addr) 1483afe5ccbSBrian Somers { 149057f1760SBrian Somers return (arp_ProxySub(bundle, addr, 1)); 1503afe5ccbSBrian Somers } 1513afe5ccbSBrian Somers 15253c9f6c0SAtsushi Murai /* 153dd7e2610SBrian Somers * arp_ClearProxy - Delete the proxy ARP entry for the peer. 15453c9f6c0SAtsushi Murai */ 15553c9f6c0SAtsushi Murai int 156057f1760SBrian Somers arp_ClearProxy(struct bundle *bundle, struct in_addr addr) 15753c9f6c0SAtsushi Murai { 158057f1760SBrian Somers return (arp_ProxySub(bundle, addr, 0)); 15953c9f6c0SAtsushi Murai } 16053c9f6c0SAtsushi Murai 16153c9f6c0SAtsushi Murai #else /* RTM_VERSION */ 16253c9f6c0SAtsushi Murai 16353c9f6c0SAtsushi Murai /* 164dd7e2610SBrian Somers * arp_SetProxy - Make a proxy ARP entry for the peer. 16553c9f6c0SAtsushi Murai */ 16653c9f6c0SAtsushi Murai int 167dd7e2610SBrian Somers arp_SetProxy(struct bundle *bundle, struct in_addr addr, int s) 16853c9f6c0SAtsushi Murai { 16953c9f6c0SAtsushi Murai struct arpreq arpreq; 17053c9f6c0SAtsushi Murai struct { 17153c9f6c0SAtsushi Murai struct sockaddr_dl sdl; 17253c9f6c0SAtsushi Murai char space[128]; 17353c9f6c0SAtsushi Murai } dls; 17453c9f6c0SAtsushi Murai 17570ee81ffSBrian Somers memset(&arpreq, '\0', sizeof arpreq); 17653c9f6c0SAtsushi Murai 17753c9f6c0SAtsushi Murai /* 178944f7098SBrian Somers * Get the hardware address of an interface on the same subnet as our local 179944f7098SBrian Somers * address. 18053c9f6c0SAtsushi Murai */ 181057f1760SBrian Somers if (!arp_EtherAddr(addr, &dls.sdl, 1)) { 1829b996792SBrian Somers log_Printf(LOG_PHASE_BIT, "Cannot determine ethernet address for " 1839b996792SBrian Somers "proxy ARP\n"); 18453c9f6c0SAtsushi Murai return 0; 18553c9f6c0SAtsushi Murai } 18653c9f6c0SAtsushi Murai arpreq.arp_ha.sa_len = sizeof(struct sockaddr); 18753c9f6c0SAtsushi Murai arpreq.arp_ha.sa_family = AF_UNSPEC; 18875240ed1SBrian Somers memcpy(arpreq.arp_ha.sa_data, LLADDR(&dls.sdl), dls.sdl.sdl_alen); 18953c9f6c0SAtsushi Murai SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 190503a7782SBrian Somers ((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr; 19153c9f6c0SAtsushi Murai arpreq.arp_flags = ATF_PERM | ATF_PUBL; 192820de6ebSBrian Somers if (ID0ioctl(s, SIOCSARP, (caddr_t) & arpreq) < 0) { 193a33b2ef7SBrian Somers log_Printf(LogERROR, "arp_SetProxy: ioctl(SIOCSARP): %s\n", 194a33b2ef7SBrian Somers strerror(errno)); 19553c9f6c0SAtsushi Murai return 0; 19653c9f6c0SAtsushi Murai } 19753c9f6c0SAtsushi Murai return 1; 19853c9f6c0SAtsushi Murai } 19953c9f6c0SAtsushi Murai 20053c9f6c0SAtsushi Murai /* 201dd7e2610SBrian Somers * arp_ClearProxy - Delete the proxy ARP entry for the peer. 20253c9f6c0SAtsushi Murai */ 20353c9f6c0SAtsushi Murai int 204dd7e2610SBrian Somers arp_ClearProxy(struct bundle *bundle, struct in_addr addr, int s) 20553c9f6c0SAtsushi Murai { 20653c9f6c0SAtsushi Murai struct arpreq arpreq; 20753c9f6c0SAtsushi Murai 20870ee81ffSBrian Somers memset(&arpreq, '\0', sizeof arpreq); 20953c9f6c0SAtsushi Murai SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 210503a7782SBrian Somers ((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr; 211820de6ebSBrian Somers if (ID0ioctl(s, SIOCDARP, (caddr_t) & arpreq) < 0) { 212a33b2ef7SBrian Somers log_Printf(LogERROR, "arp_ClearProxy: ioctl(SIOCDARP): %s\n", 213a33b2ef7SBrian Somers strerror(errno)); 21453c9f6c0SAtsushi Murai return 0; 21553c9f6c0SAtsushi Murai } 21653c9f6c0SAtsushi Murai return 1; 21753c9f6c0SAtsushi Murai } 218944f7098SBrian Somers 21953c9f6c0SAtsushi Murai #endif /* RTM_VERSION */ 22053c9f6c0SAtsushi Murai 22153c9f6c0SAtsushi Murai 22253c9f6c0SAtsushi Murai /* 22365cacad4SBrian Somers * arp_EtherAddr - get the hardware address of an interface on the 22453c9f6c0SAtsushi Murai * the same subnet as ipaddr. 22553c9f6c0SAtsushi Murai */ 22653c9f6c0SAtsushi Murai 22708676022SBrian Somers int 228057f1760SBrian Somers arp_EtherAddr(struct in_addr ipaddr, struct sockaddr_dl *hwaddr, 22965cacad4SBrian Somers int verbose) 23053c9f6c0SAtsushi Murai { 2316b457978SBrian Somers int mib[6], skip; 232c6fe0cb2SBrian Somers size_t needed; 233813bfe4dSBrian Somers char *buf, *ptr, *end; 234813bfe4dSBrian Somers struct if_msghdr *ifm; 235813bfe4dSBrian Somers struct ifa_msghdr *ifam; 236813bfe4dSBrian Somers struct sockaddr_dl *dl; 2376b457978SBrian Somers struct sockaddr *sa[RTAX_MAX]; 238c6fe0cb2SBrian Somers 239c6fe0cb2SBrian Somers mib[0] = CTL_NET; 240c6fe0cb2SBrian Somers mib[1] = PF_ROUTE; 241c6fe0cb2SBrian Somers mib[2] = 0; 242c6fe0cb2SBrian Somers mib[3] = 0; 243813bfe4dSBrian Somers mib[4] = NET_RT_IFLIST; 244c6fe0cb2SBrian Somers mib[5] = 0; 245813bfe4dSBrian Somers 246c6fe0cb2SBrian Somers if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 24765cacad4SBrian Somers log_Printf(LogERROR, "arp_EtherAddr: sysctl: estimate: %s\n", 2484147817aSBrian Somers strerror(errno)); 24953c9f6c0SAtsushi Murai return 0; 25053c9f6c0SAtsushi Murai } 251813bfe4dSBrian Somers 252813bfe4dSBrian Somers if ((buf = malloc(needed)) == NULL) 25353c9f6c0SAtsushi Murai return 0; 254813bfe4dSBrian Somers 255813bfe4dSBrian Somers if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 256813bfe4dSBrian Somers free(buf); 257c6fe0cb2SBrian Somers return 0; 258c6fe0cb2SBrian Somers } 259813bfe4dSBrian Somers end = buf + needed; 26053c9f6c0SAtsushi Murai 261813bfe4dSBrian Somers ptr = buf; 262813bfe4dSBrian Somers while (ptr < end) { 263813bfe4dSBrian Somers ifm = (struct if_msghdr *)ptr; /* On if_msghdr */ 264813bfe4dSBrian Somers if (ifm->ifm_type != RTM_IFINFO) 265813bfe4dSBrian Somers break; 266813bfe4dSBrian Somers dl = (struct sockaddr_dl *)(ifm + 1); /* Single _dl at end */ 267813bfe4dSBrian Somers skip = (ifm->ifm_flags & (IFF_UP | IFF_BROADCAST | IFF_POINTOPOINT | 268813bfe4dSBrian Somers IFF_NOARP | IFF_LOOPBACK)) != (IFF_UP | IFF_BROADCAST); 269813bfe4dSBrian Somers ptr += ifm->ifm_msglen; /* First ifa_msghdr */ 270813bfe4dSBrian Somers while (ptr < end) { 271813bfe4dSBrian Somers ifam = (struct ifa_msghdr *)ptr; /* Next ifa_msghdr (alias) */ 272813bfe4dSBrian Somers if (ifam->ifam_type != RTM_NEWADDR) /* finished ? */ 273813bfe4dSBrian Somers break; 274813bfe4dSBrian Somers ptr += ifam->ifam_msglen; 275813bfe4dSBrian Somers if (skip || (ifam->ifam_addrs & (RTA_NETMASK|RTA_IFA)) != 276813bfe4dSBrian Somers (RTA_NETMASK|RTA_IFA)) 277813bfe4dSBrian Somers continue; 278813bfe4dSBrian Somers /* Found a candidate. Do the addresses match ? */ 279dd7e2610SBrian Somers if (log_IsKept(LogDEBUG) && 280813bfe4dSBrian Somers ptr == (char *)ifm + ifm->ifm_msglen + ifam->ifam_msglen) 281dd7e2610SBrian Somers log_Printf(LogDEBUG, "%.*s interface is a candidate for proxy\n", 282813bfe4dSBrian Somers dl->sdl_nlen, dl->sdl_data); 2836b457978SBrian Somers 2846b457978SBrian Somers iface_ParseHdr(ifam, sa); 2856b457978SBrian Somers 2866b457978SBrian Somers if (sa[RTAX_IFA]->sa_family == AF_INET) { 2876b457978SBrian Somers struct sockaddr_in *ifa, *netmask; 2886b457978SBrian Somers 2896b457978SBrian Somers ifa = (struct sockaddr_in *)sa[RTAX_IFA]; 2906b457978SBrian Somers netmask = (struct sockaddr_in *)sa[RTAX_NETMASK]; 2916b457978SBrian Somers 292dd7e2610SBrian Somers if (log_IsKept(LogDEBUG)) { 293813bfe4dSBrian Somers char a[16]; 2946b457978SBrian Somers 2956b457978SBrian Somers strncpy(a, inet_ntoa(netmask->sin_addr), sizeof a - 1); 296813bfe4dSBrian Somers a[sizeof a - 1] = '\0'; 297dd7e2610SBrian Somers log_Printf(LogDEBUG, "Check addr %s, mask %s\n", 298813bfe4dSBrian Somers inet_ntoa(ifa->sin_addr), a); 299813bfe4dSBrian Somers } 3006b457978SBrian Somers 3016b457978SBrian Somers if ((ifa->sin_addr.s_addr & netmask->sin_addr.s_addr) == 3026b457978SBrian Somers (ipaddr.s_addr & netmask->sin_addr.s_addr)) { 30365cacad4SBrian Somers log_Printf(verbose ? LogPHASE : LogDEBUG, 304772670eaSBrooks Davis "Found interface %.*s for %s\n", dl->sdl_nlen, 30565cacad4SBrian Somers dl->sdl_data, inet_ntoa(ipaddr)); 306c6fe0cb2SBrian Somers memcpy(hwaddr, dl, dl->sdl_len); 307813bfe4dSBrian Somers free(buf); 30853c9f6c0SAtsushi Murai return 1; 30953c9f6c0SAtsushi Murai } 31053c9f6c0SAtsushi Murai } 311c6fe0cb2SBrian Somers } 3126b457978SBrian Somers } 313813bfe4dSBrian Somers free(buf); 314813bfe4dSBrian Somers 31553c9f6c0SAtsushi Murai return 0; 31653c9f6c0SAtsushi Murai } 317