1 /*
2  * WPA Supplicant - Layer2 packet handling example with dummy functions
3  * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  *
14  * This file can be used as a starting point for layer2 packet implementation.
15  */
16 
17 #include "includes.h"
18 
19 #include "common.h"
20 #include "eloop.h"
21 #include "l2_packet.h"
22 
23 
24 struct l2_packet_data {
25 	char ifname[17];
26 	u8 own_addr[ETH_ALEN];
27 	void (*rx_callback)(void *ctx, const u8 *src_addr,
28 			    const u8 *buf, size_t len);
29 	void *rx_callback_ctx;
30 	int l2_hdr; /* whether to include layer 2 (Ethernet) header data
31 		     * buffers */
32 	int fd;
33 };
34 
35 
36 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
37 {
38 	os_memcpy(addr, l2->own_addr, ETH_ALEN);
39 	return 0;
40 }
41 
42 
43 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
44 		   const u8 *buf, size_t len)
45 {
46 	if (l2 == NULL)
47 		return -1;
48 
49 	/*
50 	 * TODO: Send frame (may need different implementation depending on
51 	 * whether l2->l2_hdr is set).
52 	 */
53 
54 	return 0;
55 }
56 
57 
58 static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
59 {
60 	struct l2_packet_data *l2 = eloop_ctx;
61 	u8 buf[2300];
62 	int res;
63 
64 	/* TODO: receive frame (e.g., recv() using sock */
65 	buf[0] = 0;
66 	res = 0;
67 
68 	l2->rx_callback(l2->rx_callback_ctx, NULL /* TODO: src addr */,
69 			buf, res);
70 }
71 
72 
73 struct l2_packet_data * l2_packet_init(
74 	const char *ifname, const u8 *own_addr, unsigned short protocol,
75 	void (*rx_callback)(void *ctx, const u8 *src_addr,
76 			    const u8 *buf, size_t len),
77 	void *rx_callback_ctx, int l2_hdr)
78 {
79 	struct l2_packet_data *l2;
80 
81 	l2 = os_zalloc(sizeof(struct l2_packet_data));
82 	if (l2 == NULL)
83 		return NULL;
84 	os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
85 	l2->rx_callback = rx_callback;
86 	l2->rx_callback_ctx = rx_callback_ctx;
87 	l2->l2_hdr = l2_hdr;
88 
89 	/*
90 	 * TODO: open connection for receiving frames
91 	 */
92 	l2->fd = -1;
93 	eloop_register_read_sock(l2->fd, l2_packet_receive, l2, NULL);
94 
95 	return l2;
96 }
97 
98 
99 void l2_packet_deinit(struct l2_packet_data *l2)
100 {
101 	if (l2 == NULL)
102 		return;
103 
104 	if (l2->fd >= 0) {
105 		eloop_unregister_read_sock(l2->fd);
106 		/* TODO: close connection */
107 	}
108 
109 	os_free(l2);
110 }
111 
112 
113 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
114 {
115 	/* TODO: get interface IP address */
116 	return -1;
117 }
118 
119 
120 void l2_packet_notify_auth_start(struct l2_packet_data *l2)
121 {
122 	/* This function can be left empty */
123 }
124