xref: /freebsd/contrib/wpa/src/l2_packet/l2_packet.h (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*
2  * WPA Supplicant - Layer2 packet interface definition
3  * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  *
8  * This file defines an interface for layer 2 (link layer) packet sending and
9  * receiving. l2_packet_linux.c is one implementation for such a layer 2
10  * implementation using Linux packet sockets and l2_packet_pcap.c another one
11  * using libpcap and libdnet. When porting %wpa_supplicant to other operating
12  * systems, a new l2_packet implementation may need to be added.
13  */
14 
15 #ifndef L2_PACKET_H
16 #define L2_PACKET_H
17 
18 /**
19  * struct l2_packet_data - Internal l2_packet data structure
20  *
21  * This structure is used by the l2_packet implementation to store its private
22  * data. Other files use a pointer to this data when calling the l2_packet
23  * functions, but the contents of this structure should not be used directly
24  * outside l2_packet implementation.
25  */
26 struct l2_packet_data;
27 
28 #ifdef _MSC_VER
29 #pragma pack(push, 1)
30 #endif /* _MSC_VER */
31 
32 struct l2_ethhdr {
33 	u8 h_dest[ETH_ALEN];
34 	u8 h_source[ETH_ALEN];
35 	be16 h_proto;
36 } STRUCT_PACKED;
37 
38 #ifdef _MSC_VER
39 #pragma pack(pop)
40 #endif /* _MSC_VER */
41 
42 enum l2_packet_filter_type {
43 	L2_PACKET_FILTER_DHCP,
44 	L2_PACKET_FILTER_NDISC,
45 	L2_PACKET_FILTER_PKTTYPE,
46 };
47 
48 /**
49  * l2_packet_init - Initialize l2_packet interface
50  * @ifname: Interface name
51  * @own_addr: Optional own MAC address if available from driver interface or
52  *	%NULL if not available
53  * @protocol: Ethernet protocol number in host byte order
54  * @rx_callback: Callback function that will be called for each received packet
55  * @rx_callback_ctx: Callback data (ctx) for calls to rx_callback()
56  * @l2_hdr: 1 = include layer 2 header, 0 = do not include header
57  * Returns: Pointer to internal data or %NULL on failure
58  *
59  * rx_callback function will be called with src_addr pointing to the source
60  * address (MAC address) of the the packet. If l2_hdr is set to 0, buf
61  * points to len bytes of the payload after the layer 2 header and similarly,
62  * TX buffers start with payload. This behavior can be changed by setting
63  * l2_hdr=1 to include the layer 2 header in the data buffer.
64  *
65  * IF rx_callback is NULL, receive operation is not opened at all, i.e., only
66  * the TX path and additional helper functions for fetching MAC and IP
67  * addresses can be used.
68  */
69 struct l2_packet_data * l2_packet_init(
70 	const char *ifname, const u8 *own_addr, unsigned short protocol,
71 	void (*rx_callback)(void *ctx, const u8 *src_addr,
72 			    const u8 *buf, size_t len),
73 	void *rx_callback_ctx, int l2_hdr);
74 
75 /**
76  * l2_packet_init_bridge - Like l2_packet_init() but with bridge workaround
77  *
78  * This version of l2_packet_init() can be used to enable a workaround for Linux
79  * packet socket in case of a station interface in a bridge.
80  */
81 struct l2_packet_data * l2_packet_init_bridge(
82 	const char *br_ifname, const char *ifname, const u8 *own_addr,
83 	unsigned short protocol,
84 	void (*rx_callback)(void *ctx, const u8 *src_addr,
85 			    const u8 *buf, size_t len),
86 	void *rx_callback_ctx, int l2_hdr);
87 
88 /**
89  * l2_packet_deinit - Deinitialize l2_packet interface
90  * @l2: Pointer to internal l2_packet data from l2_packet_init()
91  */
92 void l2_packet_deinit(struct l2_packet_data *l2);
93 
94 /**
95  * l2_packet_get_own_addr - Get own layer 2 address
96  * @l2: Pointer to internal l2_packet data from l2_packet_init()
97  * @addr: Buffer for the own address (6 bytes)
98  * Returns: 0 on success, -1 on failure
99  */
100 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr);
101 
102 /**
103  * l2_packet_send - Send a packet
104  * @l2: Pointer to internal l2_packet data from l2_packet_init()
105  * @dst_addr: Destination address for the packet (only used if l2_hdr == 0)
106  * @proto: Protocol/ethertype for the packet in host byte order (only used if
107  * l2_hdr == 0)
108  * @buf: Packet contents to be sent; including layer 2 header if l2_hdr was
109  * set to 1 in l2_packet_init() call. Otherwise, only the payload of the packet
110  * is included.
111  * @len: Length of the buffer (including l2 header only if l2_hdr == 1)
112  * Returns: >=0 on success, <0 on failure
113  */
114 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
115 		   const u8 *buf, size_t len);
116 
117 /**
118  * l2_packet_get_ip_addr - Get the current IP address from the interface
119  * @l2: Pointer to internal l2_packet data from l2_packet_init()
120  * @buf: Buffer for the IP address in text format
121  * @len: Maximum buffer length
122  * Returns: 0 on success, -1 on failure
123  *
124  * This function can be used to get the current IP address from the interface
125  * bound to the l2_packet. This is mainly for status information and the IP
126  * address will be stored as an ASCII string. This function is not essential
127  * for %wpa_supplicant operation, so full implementation is not required.
128  * l2_packet implementation will need to define the function, but it can return
129  * -1 if the IP address information is not available.
130  */
131 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len);
132 
133 
134 /**
135  * l2_packet_notify_auth_start - Notify l2_packet about start of authentication
136  * @l2: Pointer to internal l2_packet data from l2_packet_init()
137  *
138  * This function is called when authentication is expected to start, e.g., when
139  * association has been completed, in order to prepare l2_packet implementation
140  * for EAPOL frames. This function is used mainly if the l2_packet code needs
141  * to do polling in which case it can increasing polling frequency. This can
142  * also be an empty function if the l2_packet implementation does not benefit
143  * from knowing about the starting authentication.
144  */
145 void l2_packet_notify_auth_start(struct l2_packet_data *l2);
146 
147 /**
148  * l2_packet_set_packet_filter - Set socket filter for l2_packet
149  * @l2: Pointer to internal l2_packet data from l2_packet_init()
150  * @type: enum l2_packet_filter_type, type of filter
151  * Returns: 0 on success, -1 on failure
152  *
153  * This function is used to set the socket filter for l2_packet socket.
154  *
155  */
156 int l2_packet_set_packet_filter(struct l2_packet_data *l2,
157 				enum l2_packet_filter_type type);
158 
159 #endif /* L2_PACKET_H */
160