13157ba21SRui Paulo /*
23157ba21SRui Paulo * WPA Supplicant - Layer2 packet handling with FreeBSD
33157ba21SRui Paulo * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
43157ba21SRui Paulo * Copyright (c) 2005, Sam Leffler <sam@errno.com>
53157ba21SRui Paulo *
6f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
7f05cddf9SRui Paulo * See README for more details.
83157ba21SRui Paulo */
93157ba21SRui Paulo
103157ba21SRui Paulo #include "includes.h"
11c1d255d3SCy Schubert #include <sys/param.h>
12c1d255d3SCy Schubert #if defined(__APPLE__) || defined(__GLIBC__) || defined(__FreeBSD_version)
133157ba21SRui Paulo #include <net/bpf.h>
143157ba21SRui Paulo #endif /* __APPLE__ */
153157ba21SRui Paulo #include <pcap.h>
163157ba21SRui Paulo
173157ba21SRui Paulo #include <sys/ioctl.h>
18f05cddf9SRui Paulo #ifdef __sun__
19f05cddf9SRui Paulo #include <libdlpi.h>
20f05cddf9SRui Paulo #else /* __sun__ */
213157ba21SRui Paulo #include <sys/sysctl.h>
22f05cddf9SRui Paulo #endif /* __sun__ */
233157ba21SRui Paulo
24bb5d6d14SR. Christian McDonald #include <net/ethernet.h>
253157ba21SRui Paulo #include <net/if.h>
263157ba21SRui Paulo #include <net/if_dl.h>
273157ba21SRui Paulo #include <net/route.h>
283157ba21SRui Paulo #include <netinet/in.h>
293157ba21SRui Paulo
303157ba21SRui Paulo #include "common.h"
313157ba21SRui Paulo #include "eloop.h"
323157ba21SRui Paulo #include "l2_packet.h"
333157ba21SRui Paulo
34*a90b9d01SCy Schubert #ifndef ETHER_VLAN_ENCAP_LEN
35*a90b9d01SCy Schubert #define ETHER_VLAN_ENCAP_LEN 4
36*a90b9d01SCy Schubert #endif
373157ba21SRui Paulo
383157ba21SRui Paulo static const u8 pae_group_addr[ETH_ALEN] =
393157ba21SRui Paulo { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
403157ba21SRui Paulo
413157ba21SRui Paulo struct l2_packet_data {
423157ba21SRui Paulo pcap_t *pcap;
433157ba21SRui Paulo char ifname[100];
443157ba21SRui Paulo u8 own_addr[ETH_ALEN];
453157ba21SRui Paulo void (*rx_callback)(void *ctx, const u8 *src_addr,
463157ba21SRui Paulo const u8 *buf, size_t len);
473157ba21SRui Paulo void *rx_callback_ctx;
483157ba21SRui Paulo int l2_hdr; /* whether to include layer 2 (Ethernet) header data
493157ba21SRui Paulo * buffers */
503157ba21SRui Paulo };
513157ba21SRui Paulo
523157ba21SRui Paulo
l2_packet_get_own_addr(struct l2_packet_data * l2,u8 * addr)533157ba21SRui Paulo int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
543157ba21SRui Paulo {
553157ba21SRui Paulo os_memcpy(addr, l2->own_addr, ETH_ALEN);
563157ba21SRui Paulo return 0;
573157ba21SRui Paulo }
583157ba21SRui Paulo
593157ba21SRui Paulo
l2_packet_send(struct l2_packet_data * l2,const u8 * dst_addr,u16 proto,const u8 * buf,size_t len)603157ba21SRui Paulo int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
613157ba21SRui Paulo const u8 *buf, size_t len)
623157ba21SRui Paulo {
633157ba21SRui Paulo if (!l2->l2_hdr) {
643157ba21SRui Paulo int ret;
653157ba21SRui Paulo struct l2_ethhdr *eth = os_malloc(sizeof(*eth) + len);
663157ba21SRui Paulo if (eth == NULL)
673157ba21SRui Paulo return -1;
683157ba21SRui Paulo os_memcpy(eth->h_dest, dst_addr, ETH_ALEN);
693157ba21SRui Paulo os_memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
703157ba21SRui Paulo eth->h_proto = htons(proto);
713157ba21SRui Paulo os_memcpy(eth + 1, buf, len);
723157ba21SRui Paulo ret = pcap_inject(l2->pcap, (u8 *) eth, len + sizeof(*eth));
733157ba21SRui Paulo os_free(eth);
743157ba21SRui Paulo return ret;
753157ba21SRui Paulo } else
763157ba21SRui Paulo return pcap_inject(l2->pcap, buf, len);
773157ba21SRui Paulo }
783157ba21SRui Paulo
793157ba21SRui Paulo
l2_packet_receive(int sock,void * eloop_ctx,void * sock_ctx)803157ba21SRui Paulo static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
813157ba21SRui Paulo {
823157ba21SRui Paulo struct l2_packet_data *l2 = eloop_ctx;
833157ba21SRui Paulo pcap_t *pcap = sock_ctx;
841e0ca65aSCy Schubert struct pcap_pkthdr *hdr;
853157ba21SRui Paulo const u_char *packet;
863157ba21SRui Paulo struct l2_ethhdr *ethhdr;
873157ba21SRui Paulo unsigned char *buf;
883157ba21SRui Paulo size_t len;
893157ba21SRui Paulo
906e5d0112SCy Schubert if (pcap_next_ex(pcap, &hdr, &packet) == -1) {
916e5d0112SCy Schubert wpa_printf(MSG_ERROR, "Error reading packet, has device disappeared?");
92953efa5bSCy Schubert packet = NULL;
936e5d0112SCy Schubert eloop_terminate();
946e5d0112SCy Schubert }
953157ba21SRui Paulo
961e0ca65aSCy Schubert if (!l2->rx_callback || !packet || hdr->caplen < sizeof(*ethhdr))
973157ba21SRui Paulo return;
983157ba21SRui Paulo
993157ba21SRui Paulo ethhdr = (struct l2_ethhdr *) packet;
1003157ba21SRui Paulo if (l2->l2_hdr) {
1013157ba21SRui Paulo buf = (unsigned char *) ethhdr;
1021e0ca65aSCy Schubert len = hdr->caplen;
1033157ba21SRui Paulo } else {
1043157ba21SRui Paulo buf = (unsigned char *) (ethhdr + 1);
1051e0ca65aSCy Schubert len = hdr->caplen - sizeof(*ethhdr);
106*a90b9d01SCy Schubert
107*a90b9d01SCy Schubert /* Handle IEEE 802.1Q encapsulated frames */
108*a90b9d01SCy Schubert if (len >= ETHER_VLAN_ENCAP_LEN &&
109*a90b9d01SCy Schubert ethhdr->h_proto == htons(ETH_P_8021Q)) {
110bb5d6d14SR. Christian McDonald buf += ETHER_VLAN_ENCAP_LEN;
111bb5d6d14SR. Christian McDonald len -= ETHER_VLAN_ENCAP_LEN;
112bb5d6d14SR. Christian McDonald }
1133157ba21SRui Paulo }
1143157ba21SRui Paulo l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
1153157ba21SRui Paulo }
1163157ba21SRui Paulo
1173157ba21SRui Paulo
l2_packet_init_libpcap(struct l2_packet_data * l2,unsigned short protocol)1183157ba21SRui Paulo static int l2_packet_init_libpcap(struct l2_packet_data *l2,
1193157ba21SRui Paulo unsigned short protocol)
1203157ba21SRui Paulo {
1213157ba21SRui Paulo bpf_u_int32 pcap_maskp, pcap_netp;
1223157ba21SRui Paulo char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE];
1233157ba21SRui Paulo struct bpf_program pcap_fp;
1243157ba21SRui Paulo
1253157ba21SRui Paulo pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
1263157ba21SRui Paulo l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
1273157ba21SRui Paulo if (l2->pcap == NULL) {
1283157ba21SRui Paulo fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
1293157ba21SRui Paulo fprintf(stderr, "ifname='%s'\n", l2->ifname);
1303157ba21SRui Paulo return -1;
1313157ba21SRui Paulo }
1323157ba21SRui Paulo if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
1333157ba21SRui Paulo pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
1343157ba21SRui Paulo fprintf(stderr, "pcap_set_datalink(DLT_EN10MB): %s\n",
1353157ba21SRui Paulo pcap_geterr(l2->pcap));
1363157ba21SRui Paulo return -1;
1373157ba21SRui Paulo }
1383157ba21SRui Paulo os_snprintf(pcap_filter, sizeof(pcap_filter),
1393157ba21SRui Paulo "not ether src " MACSTR " and "
1403157ba21SRui Paulo "( ether dst " MACSTR " or ether dst " MACSTR " ) and "
141bb5d6d14SR. Christian McDonald "( ether proto 0x%x or ( vlan 0 and ether proto 0x%x ) )",
1423157ba21SRui Paulo MAC2STR(l2->own_addr), /* do not receive own packets */
1433157ba21SRui Paulo MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
144bb5d6d14SR. Christian McDonald protocol, protocol);
1453157ba21SRui Paulo if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
1463157ba21SRui Paulo fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
1473157ba21SRui Paulo return -1;
1483157ba21SRui Paulo }
1493157ba21SRui Paulo
1503157ba21SRui Paulo if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
1513157ba21SRui Paulo fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
1523157ba21SRui Paulo return -1;
1533157ba21SRui Paulo }
1543157ba21SRui Paulo
1553157ba21SRui Paulo pcap_freecode(&pcap_fp);
156f05cddf9SRui Paulo #ifndef __sun__
1573157ba21SRui Paulo /*
1583157ba21SRui Paulo * When libpcap uses BPF we must enable "immediate mode" to
1593157ba21SRui Paulo * receive frames right away; otherwise the system may
1603157ba21SRui Paulo * buffer them for us.
1613157ba21SRui Paulo */
1623157ba21SRui Paulo {
1633157ba21SRui Paulo unsigned int on = 1;
1643157ba21SRui Paulo if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
1653157ba21SRui Paulo fprintf(stderr, "%s: cannot enable immediate mode on "
1663157ba21SRui Paulo "interface %s: %s\n",
1673157ba21SRui Paulo __func__, l2->ifname, strerror(errno));
1683157ba21SRui Paulo /* XXX should we fail? */
1693157ba21SRui Paulo }
1703157ba21SRui Paulo }
171f05cddf9SRui Paulo #endif /* __sun__ */
1723157ba21SRui Paulo
1733157ba21SRui Paulo eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
1743157ba21SRui Paulo l2_packet_receive, l2, l2->pcap);
1753157ba21SRui Paulo
1763157ba21SRui Paulo return 0;
1773157ba21SRui Paulo }
1783157ba21SRui Paulo
1793157ba21SRui Paulo
eth_get(const char * device,u8 ea[ETH_ALEN])1803157ba21SRui Paulo static int eth_get(const char *device, u8 ea[ETH_ALEN])
1813157ba21SRui Paulo {
182f05cddf9SRui Paulo #ifdef __sun__
183f05cddf9SRui Paulo dlpi_handle_t dh;
184f05cddf9SRui Paulo u32 physaddrlen = DLPI_PHYSADDR_MAX;
185f05cddf9SRui Paulo u8 physaddr[DLPI_PHYSADDR_MAX];
186f05cddf9SRui Paulo int retval;
187f05cddf9SRui Paulo
188f05cddf9SRui Paulo retval = dlpi_open(device, &dh, 0);
189f05cddf9SRui Paulo if (retval != DLPI_SUCCESS) {
190f05cddf9SRui Paulo wpa_printf(MSG_ERROR, "dlpi_open error: %s",
191f05cddf9SRui Paulo dlpi_strerror(retval));
192f05cddf9SRui Paulo return -1;
193f05cddf9SRui Paulo }
194f05cddf9SRui Paulo
195f05cddf9SRui Paulo retval = dlpi_get_physaddr(dh, DL_CURR_PHYS_ADDR, physaddr,
196f05cddf9SRui Paulo &physaddrlen);
197f05cddf9SRui Paulo if (retval != DLPI_SUCCESS) {
198f05cddf9SRui Paulo wpa_printf(MSG_ERROR, "dlpi_get_physaddr error: %s",
199f05cddf9SRui Paulo dlpi_strerror(retval));
200f05cddf9SRui Paulo dlpi_close(dh);
201f05cddf9SRui Paulo return -1;
202f05cddf9SRui Paulo }
203f05cddf9SRui Paulo os_memcpy(ea, physaddr, ETH_ALEN);
204f05cddf9SRui Paulo dlpi_close(dh);
205f05cddf9SRui Paulo #else /* __sun__ */
2063157ba21SRui Paulo struct if_msghdr *ifm;
2073157ba21SRui Paulo struct sockaddr_dl *sdl;
2083157ba21SRui Paulo u_char *p, *buf;
2093157ba21SRui Paulo size_t len;
2103157ba21SRui Paulo int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
2113157ba21SRui Paulo
2123157ba21SRui Paulo if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
2133157ba21SRui Paulo return -1;
2143157ba21SRui Paulo if ((buf = os_malloc(len)) == NULL)
2153157ba21SRui Paulo return -1;
2163157ba21SRui Paulo if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
2173157ba21SRui Paulo os_free(buf);
2183157ba21SRui Paulo return -1;
2193157ba21SRui Paulo }
2203157ba21SRui Paulo for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
2213157ba21SRui Paulo ifm = (struct if_msghdr *)p;
2223157ba21SRui Paulo sdl = (struct sockaddr_dl *)(ifm + 1);
2233157ba21SRui Paulo if (ifm->ifm_type != RTM_IFINFO ||
2243157ba21SRui Paulo (ifm->ifm_addrs & RTA_IFP) == 0)
2253157ba21SRui Paulo continue;
2263157ba21SRui Paulo if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
2273157ba21SRui Paulo os_memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
2283157ba21SRui Paulo continue;
2293157ba21SRui Paulo os_memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
2303157ba21SRui Paulo break;
2313157ba21SRui Paulo }
2323157ba21SRui Paulo os_free(buf);
2333157ba21SRui Paulo
2343157ba21SRui Paulo if (p >= buf + len) {
2353157ba21SRui Paulo errno = ESRCH;
2363157ba21SRui Paulo return -1;
2373157ba21SRui Paulo }
238f05cddf9SRui Paulo #endif /* __sun__ */
2393157ba21SRui Paulo return 0;
2403157ba21SRui Paulo }
2413157ba21SRui Paulo
2423157ba21SRui Paulo
l2_packet_init(const char * ifname,const u8 * own_addr,unsigned short protocol,void (* rx_callback)(void * ctx,const u8 * src_addr,const u8 * buf,size_t len),void * rx_callback_ctx,int l2_hdr)2433157ba21SRui Paulo struct l2_packet_data * l2_packet_init(
2443157ba21SRui Paulo const char *ifname, const u8 *own_addr, unsigned short protocol,
2453157ba21SRui Paulo void (*rx_callback)(void *ctx, const u8 *src_addr,
2463157ba21SRui Paulo const u8 *buf, size_t len),
2473157ba21SRui Paulo void *rx_callback_ctx, int l2_hdr)
2483157ba21SRui Paulo {
2493157ba21SRui Paulo struct l2_packet_data *l2;
2503157ba21SRui Paulo
2513157ba21SRui Paulo l2 = os_zalloc(sizeof(struct l2_packet_data));
2523157ba21SRui Paulo if (l2 == NULL)
2533157ba21SRui Paulo return NULL;
2543157ba21SRui Paulo os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
2553157ba21SRui Paulo l2->rx_callback = rx_callback;
2563157ba21SRui Paulo l2->rx_callback_ctx = rx_callback_ctx;
2573157ba21SRui Paulo l2->l2_hdr = l2_hdr;
2583157ba21SRui Paulo
2593157ba21SRui Paulo if (eth_get(l2->ifname, l2->own_addr) < 0) {
2603157ba21SRui Paulo fprintf(stderr, "Failed to get link-level address for "
2613157ba21SRui Paulo "interface '%s'.\n", l2->ifname);
2623157ba21SRui Paulo os_free(l2);
2633157ba21SRui Paulo return NULL;
2643157ba21SRui Paulo }
2653157ba21SRui Paulo
2663157ba21SRui Paulo if (l2_packet_init_libpcap(l2, protocol)) {
2673157ba21SRui Paulo os_free(l2);
2683157ba21SRui Paulo return NULL;
2693157ba21SRui Paulo }
2703157ba21SRui Paulo
2713157ba21SRui Paulo return l2;
2723157ba21SRui Paulo }
2733157ba21SRui Paulo
2743157ba21SRui Paulo
l2_packet_init_bridge(const char * br_ifname,const char * ifname,const u8 * own_addr,unsigned short protocol,void (* rx_callback)(void * ctx,const u8 * src_addr,const u8 * buf,size_t len),void * rx_callback_ctx,int l2_hdr)2755b9c547cSRui Paulo struct l2_packet_data * l2_packet_init_bridge(
2765b9c547cSRui Paulo const char *br_ifname, const char *ifname, const u8 *own_addr,
2775b9c547cSRui Paulo unsigned short protocol,
2785b9c547cSRui Paulo void (*rx_callback)(void *ctx, const u8 *src_addr,
2795b9c547cSRui Paulo const u8 *buf, size_t len),
2805b9c547cSRui Paulo void *rx_callback_ctx, int l2_hdr)
2815b9c547cSRui Paulo {
2825b9c547cSRui Paulo return l2_packet_init(br_ifname, own_addr, protocol, rx_callback,
2835b9c547cSRui Paulo rx_callback_ctx, l2_hdr);
2845b9c547cSRui Paulo }
2855b9c547cSRui Paulo
2865b9c547cSRui Paulo
l2_packet_deinit(struct l2_packet_data * l2)2873157ba21SRui Paulo void l2_packet_deinit(struct l2_packet_data *l2)
2883157ba21SRui Paulo {
2893157ba21SRui Paulo if (l2 != NULL) {
2903157ba21SRui Paulo if (l2->pcap) {
2913157ba21SRui Paulo eloop_unregister_read_sock(
2923157ba21SRui Paulo pcap_get_selectable_fd(l2->pcap));
2933157ba21SRui Paulo pcap_close(l2->pcap);
2943157ba21SRui Paulo }
2953157ba21SRui Paulo os_free(l2);
2963157ba21SRui Paulo }
2973157ba21SRui Paulo }
2983157ba21SRui Paulo
2993157ba21SRui Paulo
l2_packet_get_ip_addr(struct l2_packet_data * l2,char * buf,size_t len)3003157ba21SRui Paulo int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
3013157ba21SRui Paulo {
3023157ba21SRui Paulo pcap_if_t *devs, *dev;
3033157ba21SRui Paulo struct pcap_addr *addr;
3043157ba21SRui Paulo struct sockaddr_in *saddr;
3053157ba21SRui Paulo int found = 0;
3063157ba21SRui Paulo char err[PCAP_ERRBUF_SIZE + 1];
3073157ba21SRui Paulo
3083157ba21SRui Paulo if (pcap_findalldevs(&devs, err) < 0) {
3093157ba21SRui Paulo wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
3103157ba21SRui Paulo return -1;
3113157ba21SRui Paulo }
3123157ba21SRui Paulo
3133157ba21SRui Paulo for (dev = devs; dev && !found; dev = dev->next) {
3143157ba21SRui Paulo if (os_strcmp(dev->name, l2->ifname) != 0)
3153157ba21SRui Paulo continue;
3163157ba21SRui Paulo
3173157ba21SRui Paulo addr = dev->addresses;
3183157ba21SRui Paulo while (addr) {
3193157ba21SRui Paulo saddr = (struct sockaddr_in *) addr->addr;
3203157ba21SRui Paulo if (saddr && saddr->sin_family == AF_INET) {
3213157ba21SRui Paulo os_strlcpy(buf, inet_ntoa(saddr->sin_addr),
3223157ba21SRui Paulo len);
3233157ba21SRui Paulo found = 1;
3243157ba21SRui Paulo break;
3253157ba21SRui Paulo }
3263157ba21SRui Paulo addr = addr->next;
3273157ba21SRui Paulo }
3283157ba21SRui Paulo }
3293157ba21SRui Paulo
3303157ba21SRui Paulo pcap_freealldevs(devs);
3313157ba21SRui Paulo
3323157ba21SRui Paulo return found ? 0 : -1;
3333157ba21SRui Paulo }
3343157ba21SRui Paulo
3353157ba21SRui Paulo
l2_packet_notify_auth_start(struct l2_packet_data * l2)3363157ba21SRui Paulo void l2_packet_notify_auth_start(struct l2_packet_data *l2)
3373157ba21SRui Paulo {
3383157ba21SRui Paulo }
3395b9c547cSRui Paulo
3405b9c547cSRui Paulo
l2_packet_set_packet_filter(struct l2_packet_data * l2,enum l2_packet_filter_type type)3415b9c547cSRui Paulo int l2_packet_set_packet_filter(struct l2_packet_data *l2,
3425b9c547cSRui Paulo enum l2_packet_filter_type type)
3435b9c547cSRui Paulo {
3445b9c547cSRui Paulo return -1;
3455b9c547cSRui Paulo }
346