Lines Matching refs:l2
71 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
73 os_memcpy(addr, l2->own_addr, ETH_ALEN);
78 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
84 if (l2 == NULL)
87 if (l2->l2_hdr) {
88 ret = pcap_sendpacket(l2->pcap, buf, len);
96 os_memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
99 ret = pcap_sendpacket(l2->pcap, (u8 *) eth, mlen);
111 struct l2_packet_data *l2 = (struct l2_packet_data *) user;
118 if (l2->l2_hdr) {
119 l2->rx_buf = (u8 *) ethhdr;
120 l2->rx_len = hdr->caplen;
122 l2->rx_buf = (u8 *) (ethhdr + 1);
123 l2->rx_len = hdr->caplen - sizeof(*ethhdr);
125 l2->rx_src = ethhdr->h_source;
126 SetEvent(l2->rx_avail);
127 WaitForSingleObject(l2->rx_done, INFINITE);
128 ResetEvent(l2->rx_done);
129 l2->rx_no_wait = no_wait_count;
136 struct l2_packet_data *l2 = arg;
138 while (l2->running) {
139 pcap_dispatch(l2->pcap, 1, l2_packet_receive_cb,
140 (u_char *) l2);
141 if (l2->rx_no_wait > 0)
142 l2->rx_no_wait--;
143 if (WaitForSingleObject(l2->rx_notify,
144 l2->rx_no_wait ? 0 : 50) ==
146 l2->rx_no_wait = no_wait_count;
147 ResetEvent(l2->rx_notify);
150 SetEvent(l2->rx_thread_done);
159 struct l2_packet_data *l2 = eloop_data;
160 l2->rx_callback(l2->rx_callback_ctx, l2->rx_src, l2->rx_buf,
161 l2->rx_len);
162 ResetEvent(l2->rx_avail);
163 SetEvent(l2->rx_done);
167 static int l2_packet_init_libpcap(struct l2_packet_data *l2,
174 pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
175 l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 1, pcap_err);
176 if (l2->pcap == NULL) {
178 fprintf(stderr, "ifname='%s'\n", l2->ifname);
185 MAC2STR(l2->own_addr), /* do not receive own packets */
186 MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
188 if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
189 fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
193 if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
194 fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
210 struct l2_packet_data *l2;
213 l2 = os_zalloc(sizeof(struct l2_packet_data));
214 if (l2 == NULL)
217 os_strncpy(l2->ifname, ifname, sizeof(l2->ifname));
219 os_snprintf(l2->ifname, sizeof(l2->ifname), "\\Device\\NPF_%s",
221 l2->rx_callback = rx_callback;
222 l2->rx_callback_ctx = rx_callback_ctx;
223 l2->l2_hdr = l2_hdr;
226 os_memcpy(l2->own_addr, own_addr, ETH_ALEN);
228 if (l2_packet_init_libpcap(l2, protocol)) {
229 os_free(l2);
233 l2->rx_avail = CreateEvent(NULL, TRUE, FALSE, NULL);
234 l2->rx_done = CreateEvent(NULL, TRUE, FALSE, NULL);
235 l2->rx_notify = CreateEvent(NULL, TRUE, FALSE, NULL);
236 if (l2->rx_avail == NULL || l2->rx_done == NULL ||
237 l2->rx_notify == NULL) {
238 CloseHandle(l2->rx_avail);
239 CloseHandle(l2->rx_done);
240 CloseHandle(l2->rx_notify);
241 pcap_close(l2->pcap);
242 os_free(l2);
246 eloop_register_event(l2->rx_avail, sizeof(l2->rx_avail),
247 l2_packet_rx_event, l2, NULL);
249 l2->running = 1;
250 l2->rx_thread = CreateThread(NULL, 0, l2_packet_receive_thread, l2, 0,
253 return l2;
259 struct l2_packet_data *l2 = eloop_ctx;
261 if (l2->rx_thread_done &&
262 WaitForSingleObject(l2->rx_thread_done, 2000) != WAIT_OBJECT_0) {
265 TerminateThread(l2->rx_thread, 0);
267 CloseHandle(l2->rx_thread_done);
268 CloseHandle(l2->rx_thread);
269 if (l2->pcap)
270 pcap_close(l2->pcap);
271 eloop_unregister_event(l2->rx_avail, sizeof(l2->rx_avail));
272 CloseHandle(l2->rx_avail);
273 CloseHandle(l2->rx_done);
274 CloseHandle(l2->rx_notify);
275 os_free(l2);
279 void l2_packet_deinit(struct l2_packet_data *l2)
281 if (l2 == NULL)
284 l2->rx_thread_done = CreateEvent(NULL, TRUE, FALSE, NULL);
286 l2->running = 0;
287 pcap_breakloop(l2->pcap);
290 * RX thread may be waiting in l2_packet_receive_cb() for l2->rx_done
292 * l2_packet_deinit() may end up being called from l2->rx_callback(),
297 eloop_register_timeout(0, 0, l2_packet_deinit_timeout, l2, NULL);
301 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
315 if (os_strcmp(dev->name, l2->ifname) != 0)
337 void l2_packet_notify_auth_start(struct l2_packet_data *l2)
339 SetEvent(l2->rx_notify);