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" 605828db6dSBrian Somers #include "ipcp.h" 615ca5389aSBrian Somers #include "filter.h" 622f786681SBrian Somers #include "descriptor.h" 633b0f8d2eSBrian Somers #include "lcp.h" 643b0f8d2eSBrian Somers #include "ccp.h" 653b0f8d2eSBrian Somers #include "link.h" 663b0f8d2eSBrian Somers #include "mp.h" 67972a1bcfSBrian Somers #ifndef NORADIUS 68972a1bcfSBrian Somers #include "radius.h" 69972a1bcfSBrian Somers #endif 705828db6dSBrian Somers #include "bundle.h" 716b457978SBrian Somers #include "iface.h" 7275240ed1SBrian Somers #include "arp.h" 7353c9f6c0SAtsushi Murai 7453c9f6c0SAtsushi Murai /* 7553c9f6c0SAtsushi Murai * SET_SA_FAMILY - set the sa_family field of a struct sockaddr, 7653c9f6c0SAtsushi Murai * if it exists. 7753c9f6c0SAtsushi Murai */ 7853c9f6c0SAtsushi Murai #define SET_SA_FAMILY(addr, family) \ 7975240ed1SBrian Somers memset((char *) &(addr), '\0', sizeof(addr)); \ 8053c9f6c0SAtsushi Murai addr.sa_family = (family); \ 8153c9f6c0SAtsushi Murai addr.sa_len = sizeof(addr); 8253c9f6c0SAtsushi Murai 8353c9f6c0SAtsushi Murai 8453c9f6c0SAtsushi Murai #if RTM_VERSION >= 3 8553c9f6c0SAtsushi Murai 8653c9f6c0SAtsushi Murai /* 87dd7e2610SBrian Somers * arp_SetProxy - Make a proxy ARP entry for the peer. 8853c9f6c0SAtsushi Murai */ 8953c9f6c0SAtsushi Murai static struct { 9053c9f6c0SAtsushi Murai struct rt_msghdr hdr; 9153c9f6c0SAtsushi Murai struct sockaddr_inarp dst; 9253c9f6c0SAtsushi Murai struct sockaddr_dl hwa; 9353c9f6c0SAtsushi Murai char extra[128]; 9453c9f6c0SAtsushi Murai } arpmsg; 9553c9f6c0SAtsushi Murai 963afe5ccbSBrian Somers static int 973afe5ccbSBrian Somers arp_ProxySub(struct bundle *bundle, struct in_addr addr, int add, int s) 9853c9f6c0SAtsushi Murai { 9953c9f6c0SAtsushi Murai int routes; 10053c9f6c0SAtsushi Murai 10153c9f6c0SAtsushi Murai /* 102944f7098SBrian Somers * Get the hardware address of an interface on the same subnet as our local 103944f7098SBrian Somers * address. 10453c9f6c0SAtsushi Murai */ 1053afe5ccbSBrian Somers 10670ee81ffSBrian Somers memset(&arpmsg, 0, sizeof arpmsg); 107503a7782SBrian Somers if (!get_ether_addr(s, addr, &arpmsg.hwa)) { 1083afe5ccbSBrian Somers log_Printf(LogWARN, "%s: Cannot determine ethernet address for proxy ARP\n", 1093afe5ccbSBrian Somers inet_ntoa(addr)); 11053c9f6c0SAtsushi Murai return 0; 11153c9f6c0SAtsushi Murai } 1125106c671SBrian Somers routes = ID0socket(PF_ROUTE, SOCK_RAW, AF_INET); 1135106c671SBrian Somers if (routes < 0) { 114dd7e2610SBrian Somers log_Printf(LogERROR, "arp_SetProxy: opening routing socket: %s\n", 115927145beSBrian Somers strerror(errno)); 11653c9f6c0SAtsushi Murai return 0; 11753c9f6c0SAtsushi Murai } 1183afe5ccbSBrian Somers arpmsg.hdr.rtm_type = add ? RTM_ADD : RTM_DELETE; 11953c9f6c0SAtsushi Murai arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC; 12053c9f6c0SAtsushi Murai arpmsg.hdr.rtm_version = RTM_VERSION; 121820de6ebSBrian Somers arpmsg.hdr.rtm_seq = ++bundle->routing_seq; 12253c9f6c0SAtsushi Murai arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY; 12353c9f6c0SAtsushi Murai arpmsg.hdr.rtm_inits = RTV_EXPIRE; 12453c9f6c0SAtsushi Murai arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp); 12553c9f6c0SAtsushi Murai arpmsg.dst.sin_family = AF_INET; 126503a7782SBrian Somers arpmsg.dst.sin_addr.s_addr = addr.s_addr; 12753c9f6c0SAtsushi Murai arpmsg.dst.sin_other = SIN_PROXY; 12853c9f6c0SAtsushi Murai 12953c9f6c0SAtsushi Murai arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg 13053c9f6c0SAtsushi Murai + arpmsg.hwa.sdl_len; 1313afe5ccbSBrian Somers 1323afe5ccbSBrian Somers 1333afe5ccbSBrian Somers if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0 && 1343afe5ccbSBrian Somers !(!add && errno == ESRCH)) { 1353afe5ccbSBrian Somers log_Printf(LogERROR, "%s proxy arp entry %s: %s\n", 1363afe5ccbSBrian Somers add ? "Add" : "Delete", inet_ntoa(addr), strerror(errno)); 13753c9f6c0SAtsushi Murai close(routes); 13853c9f6c0SAtsushi Murai return 0; 13953c9f6c0SAtsushi Murai } 14053c9f6c0SAtsushi Murai close(routes); 14153c9f6c0SAtsushi Murai return 1; 14253c9f6c0SAtsushi Murai } 14353c9f6c0SAtsushi Murai 1443afe5ccbSBrian Somers int 1453afe5ccbSBrian Somers arp_SetProxy(struct bundle *bundle, struct in_addr addr, int s) 1463afe5ccbSBrian Somers { 1473afe5ccbSBrian Somers 1483afe5ccbSBrian Somers return (arp_ProxySub(bundle, addr, 1, s)); 1493afe5ccbSBrian Somers } 1503afe5ccbSBrian Somers 15153c9f6c0SAtsushi Murai /* 152dd7e2610SBrian Somers * arp_ClearProxy - Delete the proxy ARP entry for the peer. 15353c9f6c0SAtsushi Murai */ 15453c9f6c0SAtsushi Murai int 155dd7e2610SBrian Somers arp_ClearProxy(struct bundle *bundle, struct in_addr addr, int s) 15653c9f6c0SAtsushi Murai { 15753c9f6c0SAtsushi Murai 1583afe5ccbSBrian Somers return (arp_ProxySub(bundle, addr, 0, s)); 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 */ 181503a7782SBrian Somers if (!get_ether_addr(s, addr, &dls.sdl)) { 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 /* 22353c9f6c0SAtsushi Murai * get_ether_addr - get the hardware address of an interface on the 22453c9f6c0SAtsushi Murai * the same subnet as ipaddr. 22553c9f6c0SAtsushi Murai */ 22653c9f6c0SAtsushi Murai 22708676022SBrian Somers int 228c6fe0cb2SBrian Somers get_ether_addr(int s, struct in_addr ipaddr, struct sockaddr_dl *hwaddr) 22953c9f6c0SAtsushi Murai { 2306b457978SBrian Somers int mib[6], skip; 231c6fe0cb2SBrian Somers size_t needed; 232813bfe4dSBrian Somers char *buf, *ptr, *end; 233813bfe4dSBrian Somers struct if_msghdr *ifm; 234813bfe4dSBrian Somers struct ifa_msghdr *ifam; 235813bfe4dSBrian Somers struct sockaddr_dl *dl; 2366b457978SBrian Somers struct sockaddr *sa[RTAX_MAX]; 237c6fe0cb2SBrian Somers 238c6fe0cb2SBrian Somers mib[0] = CTL_NET; 239c6fe0cb2SBrian Somers mib[1] = PF_ROUTE; 240c6fe0cb2SBrian Somers mib[2] = 0; 241c6fe0cb2SBrian Somers mib[3] = 0; 242813bfe4dSBrian Somers mib[4] = NET_RT_IFLIST; 243c6fe0cb2SBrian Somers mib[5] = 0; 244813bfe4dSBrian Somers 245c6fe0cb2SBrian Somers if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 246dd7e2610SBrian Somers log_Printf(LogERROR, "get_ether_addr: sysctl: estimate: %s\n", 2474147817aSBrian Somers strerror(errno)); 24853c9f6c0SAtsushi Murai return 0; 24953c9f6c0SAtsushi Murai } 250813bfe4dSBrian Somers 251813bfe4dSBrian Somers if ((buf = malloc(needed)) == NULL) 25253c9f6c0SAtsushi Murai return 0; 253813bfe4dSBrian Somers 254813bfe4dSBrian Somers if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 255813bfe4dSBrian Somers free(buf); 256c6fe0cb2SBrian Somers return 0; 257c6fe0cb2SBrian Somers } 258813bfe4dSBrian Somers end = buf + needed; 25953c9f6c0SAtsushi Murai 260813bfe4dSBrian Somers ptr = buf; 261813bfe4dSBrian Somers while (ptr < end) { 262813bfe4dSBrian Somers ifm = (struct if_msghdr *)ptr; /* On if_msghdr */ 263813bfe4dSBrian Somers if (ifm->ifm_type != RTM_IFINFO) 264813bfe4dSBrian Somers break; 265813bfe4dSBrian Somers dl = (struct sockaddr_dl *)(ifm + 1); /* Single _dl at end */ 266813bfe4dSBrian Somers skip = (ifm->ifm_flags & (IFF_UP | IFF_BROADCAST | IFF_POINTOPOINT | 267813bfe4dSBrian Somers IFF_NOARP | IFF_LOOPBACK)) != (IFF_UP | IFF_BROADCAST); 268813bfe4dSBrian Somers ptr += ifm->ifm_msglen; /* First ifa_msghdr */ 269813bfe4dSBrian Somers while (ptr < end) { 270813bfe4dSBrian Somers ifam = (struct ifa_msghdr *)ptr; /* Next ifa_msghdr (alias) */ 271813bfe4dSBrian Somers if (ifam->ifam_type != RTM_NEWADDR) /* finished ? */ 272813bfe4dSBrian Somers break; 273813bfe4dSBrian Somers ptr += ifam->ifam_msglen; 274813bfe4dSBrian Somers if (skip || (ifam->ifam_addrs & (RTA_NETMASK|RTA_IFA)) != 275813bfe4dSBrian Somers (RTA_NETMASK|RTA_IFA)) 276813bfe4dSBrian Somers continue; 277813bfe4dSBrian Somers /* Found a candidate. Do the addresses match ? */ 278dd7e2610SBrian Somers if (log_IsKept(LogDEBUG) && 279813bfe4dSBrian Somers ptr == (char *)ifm + ifm->ifm_msglen + ifam->ifam_msglen) 280dd7e2610SBrian Somers log_Printf(LogDEBUG, "%.*s interface is a candidate for proxy\n", 281813bfe4dSBrian Somers dl->sdl_nlen, dl->sdl_data); 2826b457978SBrian Somers 2836b457978SBrian Somers iface_ParseHdr(ifam, sa); 2846b457978SBrian Somers 2856b457978SBrian Somers if (sa[RTAX_IFA]->sa_family == AF_INET) { 2866b457978SBrian Somers struct sockaddr_in *ifa, *netmask; 2876b457978SBrian Somers 2886b457978SBrian Somers ifa = (struct sockaddr_in *)sa[RTAX_IFA]; 2896b457978SBrian Somers netmask = (struct sockaddr_in *)sa[RTAX_NETMASK]; 2906b457978SBrian Somers 291dd7e2610SBrian Somers if (log_IsKept(LogDEBUG)) { 292813bfe4dSBrian Somers char a[16]; 2936b457978SBrian Somers 2946b457978SBrian Somers strncpy(a, inet_ntoa(netmask->sin_addr), sizeof a - 1); 295813bfe4dSBrian Somers a[sizeof a - 1] = '\0'; 296dd7e2610SBrian Somers log_Printf(LogDEBUG, "Check addr %s, mask %s\n", 297813bfe4dSBrian Somers inet_ntoa(ifa->sin_addr), a); 298813bfe4dSBrian Somers } 2996b457978SBrian Somers 3006b457978SBrian Somers if ((ifa->sin_addr.s_addr & netmask->sin_addr.s_addr) == 3016b457978SBrian Somers (ipaddr.s_addr & netmask->sin_addr.s_addr)) { 302dd7e2610SBrian Somers log_Printf(LogPHASE, "Found interface %.*s for %s\n", 30308676022SBrian Somers dl->sdl_alen, dl->sdl_data, inet_ntoa(ipaddr)); 304c6fe0cb2SBrian Somers memcpy(hwaddr, dl, dl->sdl_len); 305813bfe4dSBrian Somers free(buf); 30653c9f6c0SAtsushi Murai return 1; 30753c9f6c0SAtsushi Murai } 30853c9f6c0SAtsushi Murai } 309c6fe0cb2SBrian Somers } 3106b457978SBrian Somers } 311813bfe4dSBrian Somers free(buf); 312813bfe4dSBrian Somers 31353c9f6c0SAtsushi Murai return 0; 31453c9f6c0SAtsushi Murai } 315