1 /* 2 * WPA Supplicant - Layer2 packet handling with FreeBSD 3 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi> 4 * Copyright (c) 2005, Sam Leffler <sam@errno.com> 5 * 6 * This software may be distributed under the terms of the BSD license. 7 * See README for more details. 8 */ 9 10 #include "includes.h" 11 #include <sys/param.h> 12 #if defined(__APPLE__) || defined(__GLIBC__) || defined(__FreeBSD_version) 13 #include <net/bpf.h> 14 #endif /* __APPLE__ */ 15 #include <pcap.h> 16 17 #include <sys/ioctl.h> 18 #ifdef __sun__ 19 #include <libdlpi.h> 20 #else /* __sun__ */ 21 #include <sys/sysctl.h> 22 #endif /* __sun__ */ 23 24 #include <net/if.h> 25 #include <net/if_dl.h> 26 #include <net/route.h> 27 #include <netinet/in.h> 28 29 #include "common.h" 30 #include "eloop.h" 31 #include "l2_packet.h" 32 33 34 static const u8 pae_group_addr[ETH_ALEN] = 35 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 }; 36 37 struct l2_packet_data { 38 pcap_t *pcap; 39 char ifname[100]; 40 u8 own_addr[ETH_ALEN]; 41 void (*rx_callback)(void *ctx, const u8 *src_addr, 42 const u8 *buf, size_t len); 43 void *rx_callback_ctx; 44 int l2_hdr; /* whether to include layer 2 (Ethernet) header data 45 * buffers */ 46 }; 47 48 49 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr) 50 { 51 os_memcpy(addr, l2->own_addr, ETH_ALEN); 52 return 0; 53 } 54 55 56 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto, 57 const u8 *buf, size_t len) 58 { 59 if (!l2->l2_hdr) { 60 int ret; 61 struct l2_ethhdr *eth = os_malloc(sizeof(*eth) + len); 62 if (eth == NULL) 63 return -1; 64 os_memcpy(eth->h_dest, dst_addr, ETH_ALEN); 65 os_memcpy(eth->h_source, l2->own_addr, ETH_ALEN); 66 eth->h_proto = htons(proto); 67 os_memcpy(eth + 1, buf, len); 68 ret = pcap_inject(l2->pcap, (u8 *) eth, len + sizeof(*eth)); 69 os_free(eth); 70 return ret; 71 } else 72 return pcap_inject(l2->pcap, buf, len); 73 } 74 75 76 static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx) 77 { 78 struct l2_packet_data *l2 = eloop_ctx; 79 pcap_t *pcap = sock_ctx; 80 struct pcap_pkthdr *hdr; 81 const u_char *packet; 82 struct l2_ethhdr *ethhdr; 83 unsigned char *buf; 84 size_t len; 85 86 if (pcap_next_ex(pcap, &hdr, &packet) == -1) { 87 wpa_printf(MSG_ERROR, "Error reading packet, has device disappeared?"); 88 eloop_terminate(); 89 } 90 91 if (!l2->rx_callback || !packet || hdr->caplen < sizeof(*ethhdr)) 92 return; 93 94 ethhdr = (struct l2_ethhdr *) packet; 95 if (l2->l2_hdr) { 96 buf = (unsigned char *) ethhdr; 97 len = hdr->caplen; 98 } else { 99 buf = (unsigned char *) (ethhdr + 1); 100 len = hdr->caplen - sizeof(*ethhdr); 101 } 102 l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len); 103 } 104 105 106 static int l2_packet_init_libpcap(struct l2_packet_data *l2, 107 unsigned short protocol) 108 { 109 bpf_u_int32 pcap_maskp, pcap_netp; 110 char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE]; 111 struct bpf_program pcap_fp; 112 113 pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err); 114 l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err); 115 if (l2->pcap == NULL) { 116 fprintf(stderr, "pcap_open_live: %s\n", pcap_err); 117 fprintf(stderr, "ifname='%s'\n", l2->ifname); 118 return -1; 119 } 120 if (pcap_datalink(l2->pcap) != DLT_EN10MB && 121 pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) { 122 fprintf(stderr, "pcap_set_datalink(DLT_EN10MB): %s\n", 123 pcap_geterr(l2->pcap)); 124 return -1; 125 } 126 os_snprintf(pcap_filter, sizeof(pcap_filter), 127 "not ether src " MACSTR " and " 128 "( ether dst " MACSTR " or ether dst " MACSTR " ) and " 129 "ether proto 0x%x", 130 MAC2STR(l2->own_addr), /* do not receive own packets */ 131 MAC2STR(l2->own_addr), MAC2STR(pae_group_addr), 132 protocol); 133 if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) { 134 fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap)); 135 return -1; 136 } 137 138 if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) { 139 fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap)); 140 return -1; 141 } 142 143 pcap_freecode(&pcap_fp); 144 #ifndef __sun__ 145 /* 146 * When libpcap uses BPF we must enable "immediate mode" to 147 * receive frames right away; otherwise the system may 148 * buffer them for us. 149 */ 150 { 151 unsigned int on = 1; 152 if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) { 153 fprintf(stderr, "%s: cannot enable immediate mode on " 154 "interface %s: %s\n", 155 __func__, l2->ifname, strerror(errno)); 156 /* XXX should we fail? */ 157 } 158 } 159 #endif /* __sun__ */ 160 161 eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap), 162 l2_packet_receive, l2, l2->pcap); 163 164 return 0; 165 } 166 167 168 static int eth_get(const char *device, u8 ea[ETH_ALEN]) 169 { 170 #ifdef __sun__ 171 dlpi_handle_t dh; 172 u32 physaddrlen = DLPI_PHYSADDR_MAX; 173 u8 physaddr[DLPI_PHYSADDR_MAX]; 174 int retval; 175 176 retval = dlpi_open(device, &dh, 0); 177 if (retval != DLPI_SUCCESS) { 178 wpa_printf(MSG_ERROR, "dlpi_open error: %s", 179 dlpi_strerror(retval)); 180 return -1; 181 } 182 183 retval = dlpi_get_physaddr(dh, DL_CURR_PHYS_ADDR, physaddr, 184 &physaddrlen); 185 if (retval != DLPI_SUCCESS) { 186 wpa_printf(MSG_ERROR, "dlpi_get_physaddr error: %s", 187 dlpi_strerror(retval)); 188 dlpi_close(dh); 189 return -1; 190 } 191 os_memcpy(ea, physaddr, ETH_ALEN); 192 dlpi_close(dh); 193 #else /* __sun__ */ 194 struct if_msghdr *ifm; 195 struct sockaddr_dl *sdl; 196 u_char *p, *buf; 197 size_t len; 198 int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 }; 199 200 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) 201 return -1; 202 if ((buf = os_malloc(len)) == NULL) 203 return -1; 204 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { 205 os_free(buf); 206 return -1; 207 } 208 for (p = buf; p < buf + len; p += ifm->ifm_msglen) { 209 ifm = (struct if_msghdr *)p; 210 sdl = (struct sockaddr_dl *)(ifm + 1); 211 if (ifm->ifm_type != RTM_IFINFO || 212 (ifm->ifm_addrs & RTA_IFP) == 0) 213 continue; 214 if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 || 215 os_memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0) 216 continue; 217 os_memcpy(ea, LLADDR(sdl), sdl->sdl_alen); 218 break; 219 } 220 os_free(buf); 221 222 if (p >= buf + len) { 223 errno = ESRCH; 224 return -1; 225 } 226 #endif /* __sun__ */ 227 return 0; 228 } 229 230 231 struct l2_packet_data * l2_packet_init( 232 const char *ifname, const u8 *own_addr, unsigned short protocol, 233 void (*rx_callback)(void *ctx, const u8 *src_addr, 234 const u8 *buf, size_t len), 235 void *rx_callback_ctx, int l2_hdr) 236 { 237 struct l2_packet_data *l2; 238 239 l2 = os_zalloc(sizeof(struct l2_packet_data)); 240 if (l2 == NULL) 241 return NULL; 242 os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname)); 243 l2->rx_callback = rx_callback; 244 l2->rx_callback_ctx = rx_callback_ctx; 245 l2->l2_hdr = l2_hdr; 246 247 if (eth_get(l2->ifname, l2->own_addr) < 0) { 248 fprintf(stderr, "Failed to get link-level address for " 249 "interface '%s'.\n", l2->ifname); 250 os_free(l2); 251 return NULL; 252 } 253 254 if (l2_packet_init_libpcap(l2, protocol)) { 255 os_free(l2); 256 return NULL; 257 } 258 259 return l2; 260 } 261 262 263 struct l2_packet_data * l2_packet_init_bridge( 264 const char *br_ifname, const char *ifname, const u8 *own_addr, 265 unsigned short protocol, 266 void (*rx_callback)(void *ctx, const u8 *src_addr, 267 const u8 *buf, size_t len), 268 void *rx_callback_ctx, int l2_hdr) 269 { 270 return l2_packet_init(br_ifname, own_addr, protocol, rx_callback, 271 rx_callback_ctx, l2_hdr); 272 } 273 274 275 void l2_packet_deinit(struct l2_packet_data *l2) 276 { 277 if (l2 != NULL) { 278 if (l2->pcap) { 279 eloop_unregister_read_sock( 280 pcap_get_selectable_fd(l2->pcap)); 281 pcap_close(l2->pcap); 282 } 283 os_free(l2); 284 } 285 } 286 287 288 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len) 289 { 290 pcap_if_t *devs, *dev; 291 struct pcap_addr *addr; 292 struct sockaddr_in *saddr; 293 int found = 0; 294 char err[PCAP_ERRBUF_SIZE + 1]; 295 296 if (pcap_findalldevs(&devs, err) < 0) { 297 wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err); 298 return -1; 299 } 300 301 for (dev = devs; dev && !found; dev = dev->next) { 302 if (os_strcmp(dev->name, l2->ifname) != 0) 303 continue; 304 305 addr = dev->addresses; 306 while (addr) { 307 saddr = (struct sockaddr_in *) addr->addr; 308 if (saddr && saddr->sin_family == AF_INET) { 309 os_strlcpy(buf, inet_ntoa(saddr->sin_addr), 310 len); 311 found = 1; 312 break; 313 } 314 addr = addr->next; 315 } 316 } 317 318 pcap_freealldevs(devs); 319 320 return found ? 0 : -1; 321 } 322 323 324 void l2_packet_notify_auth_start(struct l2_packet_data *l2) 325 { 326 } 327 328 329 int l2_packet_set_packet_filter(struct l2_packet_data *l2, 330 enum l2_packet_filter_type type) 331 { 332 return -1; 333 } 334