l2_packet_freebsd.c

Go to the documentation of this file.
00001 
00017 #include "includes.h"
00018 #ifdef __APPLE__
00019 #include <net/bpf.h>
00020 #endif /* __APPLE__ */
00021 #include <pcap.h>
00022 
00023 #include <sys/ioctl.h>
00024 #include <sys/sysctl.h>
00025 
00026 #include <net/if.h>
00027 #include <net/if_dl.h>
00028 #include <net/route.h>
00029 #include <netinet/in.h>
00030 
00031 #include "common.h"
00032 #include "eloop.h"
00033 #include "l2_packet.h"
00034 
00035 
00036 static const u8 pae_group_addr[ETH_ALEN] =
00037 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
00038 
00039 struct l2_packet_data {
00040         pcap_t *pcap;
00041         char ifname[100];
00042         u8 own_addr[ETH_ALEN];
00043         void (*rx_callback)(void *ctx, const u8 *src_addr,
00044                             const u8 *buf, size_t len);
00045         void *rx_callback_ctx;
00046         int l2_hdr; /* whether to include layer 2 (Ethernet) header data
00047                      * buffers */
00048 };
00049 
00050 
00051 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
00052 {
00053         os_memcpy(addr, l2->own_addr, ETH_ALEN);
00054         return 0;
00055 }
00056 
00057 
00058 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
00059                    const u8 *buf, size_t len)
00060 {
00061         if (!l2->l2_hdr) {
00062                 int ret;
00063                 struct l2_ethhdr *eth = os_malloc(sizeof(*eth) + len);
00064                 if (eth == NULL)
00065                         return -1;
00066                 os_memcpy(eth->h_dest, dst_addr, ETH_ALEN);
00067                 os_memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
00068                 eth->h_proto = htons(proto);
00069                 os_memcpy(eth + 1, buf, len);
00070                 ret = pcap_inject(l2->pcap, (u8 *) eth, len + sizeof(*eth));
00071                 os_free(eth);
00072                 return ret;
00073         } else
00074                 return pcap_inject(l2->pcap, buf, len);
00075 }
00076 
00077 
00078 static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
00079 {
00080         struct l2_packet_data *l2 = eloop_ctx;
00081         pcap_t *pcap = sock_ctx;
00082         struct pcap_pkthdr hdr;
00083         const u_char *packet;
00084         struct l2_ethhdr *ethhdr;
00085         unsigned char *buf;
00086         size_t len;
00087 
00088         packet = pcap_next(pcap, &hdr);
00089 
00090         if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
00091                 return;
00092 
00093         ethhdr = (struct l2_ethhdr *) packet;
00094         if (l2->l2_hdr) {
00095                 buf = (unsigned char *) ethhdr;
00096                 len = hdr.caplen;
00097         } else {
00098                 buf = (unsigned char *) (ethhdr + 1);
00099                 len = hdr.caplen - sizeof(*ethhdr);
00100         }
00101         l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
00102 }
00103 
00104 
00105 static int l2_packet_init_libpcap(struct l2_packet_data *l2,
00106                                   unsigned short protocol)
00107 {
00108         bpf_u_int32 pcap_maskp, pcap_netp;
00109         char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE];
00110         struct bpf_program pcap_fp;
00111 
00112         pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
00113         l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
00114         if (l2->pcap == NULL) {
00115                 fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
00116                 fprintf(stderr, "ifname='%s'\n", l2->ifname);
00117                 return -1;
00118         }
00119         if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
00120             pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
00121                 fprintf(stderr, "pcap_set_datalink(DLT_EN10MB): %s\n",
00122                         pcap_geterr(l2->pcap));
00123                 return -1;
00124         }
00125         os_snprintf(pcap_filter, sizeof(pcap_filter),
00126                     "not ether src " MACSTR " and "
00127                     "( ether dst " MACSTR " or ether dst " MACSTR " ) and "
00128                     "ether proto 0x%x",
00129                     MAC2STR(l2->own_addr), /* do not receive own packets */
00130                     MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
00131                     protocol);
00132         if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
00133                 fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
00134                 return -1;
00135         }
00136 
00137         if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
00138                 fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
00139                 return -1;
00140         }
00141 
00142         pcap_freecode(&pcap_fp);
00143         /*
00144          * When libpcap uses BPF we must enable "immediate mode" to
00145          * receive frames right away; otherwise the system may
00146          * buffer them for us.
00147          */
00148         {
00149                 unsigned int on = 1;
00150                 if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
00151                         fprintf(stderr, "%s: cannot enable immediate mode on "
00152                                 "interface %s: %s\n",
00153                                 __func__, l2->ifname, strerror(errno));
00154                         /* XXX should we fail? */
00155                 }
00156         }
00157 
00158         eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
00159                                  l2_packet_receive, l2, l2->pcap);
00160 
00161         return 0;
00162 }
00163 
00164 
00165 static int eth_get(const char *device, u8 ea[ETH_ALEN])
00166 {
00167         struct if_msghdr *ifm;
00168         struct sockaddr_dl *sdl;
00169         u_char *p, *buf;
00170         size_t len;
00171         int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
00172 
00173         if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
00174                 return -1;
00175         if ((buf = os_malloc(len)) == NULL)
00176                 return -1;
00177         if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
00178                 os_free(buf);
00179                 return -1;
00180         }
00181         for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
00182                 ifm = (struct if_msghdr *)p;
00183                 sdl = (struct sockaddr_dl *)(ifm + 1);
00184                 if (ifm->ifm_type != RTM_IFINFO ||
00185                     (ifm->ifm_addrs & RTA_IFP) == 0)
00186                         continue;
00187                 if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
00188                     os_memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
00189                         continue;
00190                 os_memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
00191                 break;
00192         }
00193         os_free(buf);
00194 
00195         if (p >= buf + len) {
00196                 errno = ESRCH;
00197                 return -1;
00198         }
00199         return 0;
00200 }
00201 
00202 
00203 struct l2_packet_data * l2_packet_init(
00204         const char *ifname, const u8 *own_addr, unsigned short protocol,
00205         void (*rx_callback)(void *ctx, const u8 *src_addr,
00206                             const u8 *buf, size_t len),
00207         void *rx_callback_ctx, int l2_hdr)
00208 {
00209         struct l2_packet_data *l2;
00210 
00211         l2 = os_zalloc(sizeof(struct l2_packet_data));
00212         if (l2 == NULL)
00213                 return NULL;
00214         os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
00215         l2->rx_callback = rx_callback;
00216         l2->rx_callback_ctx = rx_callback_ctx;
00217         l2->l2_hdr = l2_hdr;
00218 
00219         if (eth_get(l2->ifname, l2->own_addr) < 0) {
00220                 fprintf(stderr, "Failed to get link-level address for "
00221                         "interface '%s'.\n", l2->ifname);
00222                 os_free(l2);
00223                 return NULL;
00224         }
00225 
00226         if (l2_packet_init_libpcap(l2, protocol)) {
00227                 os_free(l2);
00228                 return NULL;
00229         }
00230 
00231         return l2;
00232 }
00233 
00234 
00235 void l2_packet_deinit(struct l2_packet_data *l2)
00236 {
00237         if (l2 != NULL) {
00238                 if (l2->pcap) {
00239                         eloop_unregister_read_sock(
00240                                 pcap_get_selectable_fd(l2->pcap));
00241                         pcap_close(l2->pcap);
00242                 }
00243                 os_free(l2);
00244         }
00245 }
00246 
00247 
00248 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
00249 {
00250         pcap_if_t *devs, *dev;
00251         struct pcap_addr *addr;
00252         struct sockaddr_in *saddr;
00253         int found = 0;
00254         char err[PCAP_ERRBUF_SIZE + 1];
00255 
00256         if (pcap_findalldevs(&devs, err) < 0) {
00257                 wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
00258                 return -1;
00259         }
00260 
00261         for (dev = devs; dev && !found; dev = dev->next) {
00262                 if (os_strcmp(dev->name, l2->ifname) != 0)
00263                         continue;
00264 
00265                 addr = dev->addresses;
00266                 while (addr) {
00267                         saddr = (struct sockaddr_in *) addr->addr;
00268                         if (saddr && saddr->sin_family == AF_INET) {
00269                                 os_strlcpy(buf, inet_ntoa(saddr->sin_addr),
00270                                            len);
00271                                 found = 1;
00272                                 break;
00273                         }
00274                         addr = addr->next;
00275                 }
00276         }
00277 
00278         pcap_freealldevs(devs);
00279 
00280         return found ? 0 : -1;
00281 }
00282 
00283 
00284 void l2_packet_notify_auth_start(struct l2_packet_data *l2)
00285 {
00286 }
00287 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:52 2009 for hostapd by  doxygen 1.6.1