xref: /freebsd/contrib/wpa/src/l2_packet/l2_packet.h (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
139beb93cSSam Leffler /*
239beb93cSSam Leffler  * WPA Supplicant - Layer2 packet interface definition
339beb93cSSam Leffler  * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
439beb93cSSam Leffler  *
5f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo  * See README for more details.
739beb93cSSam Leffler  *
839beb93cSSam Leffler  * This file defines an interface for layer 2 (link layer) packet sending and
939beb93cSSam Leffler  * receiving. l2_packet_linux.c is one implementation for such a layer 2
1039beb93cSSam Leffler  * implementation using Linux packet sockets and l2_packet_pcap.c another one
1139beb93cSSam Leffler  * using libpcap and libdnet. When porting %wpa_supplicant to other operating
1239beb93cSSam Leffler  * systems, a new l2_packet implementation may need to be added.
1339beb93cSSam Leffler  */
1439beb93cSSam Leffler 
1539beb93cSSam Leffler #ifndef L2_PACKET_H
1639beb93cSSam Leffler #define L2_PACKET_H
1739beb93cSSam Leffler 
1839beb93cSSam Leffler /**
1939beb93cSSam Leffler  * struct l2_packet_data - Internal l2_packet data structure
2039beb93cSSam Leffler  *
2139beb93cSSam Leffler  * This structure is used by the l2_packet implementation to store its private
2239beb93cSSam Leffler  * data. Other files use a pointer to this data when calling the l2_packet
2339beb93cSSam Leffler  * functions, but the contents of this structure should not be used directly
2439beb93cSSam Leffler  * outside l2_packet implementation.
2539beb93cSSam Leffler  */
2639beb93cSSam Leffler struct l2_packet_data;
2739beb93cSSam Leffler 
2839beb93cSSam Leffler #ifdef _MSC_VER
2939beb93cSSam Leffler #pragma pack(push, 1)
3039beb93cSSam Leffler #endif /* _MSC_VER */
3139beb93cSSam Leffler 
3239beb93cSSam Leffler struct l2_ethhdr {
3339beb93cSSam Leffler 	u8 h_dest[ETH_ALEN];
3439beb93cSSam Leffler 	u8 h_source[ETH_ALEN];
3539beb93cSSam Leffler 	be16 h_proto;
3639beb93cSSam Leffler } STRUCT_PACKED;
3739beb93cSSam Leffler 
3839beb93cSSam Leffler #ifdef _MSC_VER
3939beb93cSSam Leffler #pragma pack(pop)
4039beb93cSSam Leffler #endif /* _MSC_VER */
4139beb93cSSam Leffler 
425b9c547cSRui Paulo enum l2_packet_filter_type {
435b9c547cSRui Paulo 	L2_PACKET_FILTER_DHCP,
445b9c547cSRui Paulo 	L2_PACKET_FILTER_NDISC,
4585732ac8SCy Schubert 	L2_PACKET_FILTER_PKTTYPE,
465b9c547cSRui Paulo };
475b9c547cSRui Paulo 
4839beb93cSSam Leffler /**
4939beb93cSSam Leffler  * l2_packet_init - Initialize l2_packet interface
5039beb93cSSam Leffler  * @ifname: Interface name
5139beb93cSSam Leffler  * @own_addr: Optional own MAC address if available from driver interface or
5239beb93cSSam Leffler  *	%NULL if not available
5339beb93cSSam Leffler  * @protocol: Ethernet protocol number in host byte order
5439beb93cSSam Leffler  * @rx_callback: Callback function that will be called for each received packet
5539beb93cSSam Leffler  * @rx_callback_ctx: Callback data (ctx) for calls to rx_callback()
5639beb93cSSam Leffler  * @l2_hdr: 1 = include layer 2 header, 0 = do not include header
5739beb93cSSam Leffler  * Returns: Pointer to internal data or %NULL on failure
5839beb93cSSam Leffler  *
5939beb93cSSam Leffler  * rx_callback function will be called with src_addr pointing to the source
6039beb93cSSam Leffler  * address (MAC address) of the the packet. If l2_hdr is set to 0, buf
6139beb93cSSam Leffler  * points to len bytes of the payload after the layer 2 header and similarly,
6239beb93cSSam Leffler  * TX buffers start with payload. This behavior can be changed by setting
6339beb93cSSam Leffler  * l2_hdr=1 to include the layer 2 header in the data buffer.
64*c1d255d3SCy Schubert  *
65*c1d255d3SCy Schubert  * IF rx_callback is NULL, receive operation is not opened at all, i.e., only
66*c1d255d3SCy Schubert  * the TX path and additional helper functions for fetching MAC and IP
67*c1d255d3SCy Schubert  * addresses can be used.
6839beb93cSSam Leffler  */
6939beb93cSSam Leffler struct l2_packet_data * l2_packet_init(
7039beb93cSSam Leffler 	const char *ifname, const u8 *own_addr, unsigned short protocol,
7139beb93cSSam Leffler 	void (*rx_callback)(void *ctx, const u8 *src_addr,
7239beb93cSSam Leffler 			    const u8 *buf, size_t len),
7339beb93cSSam Leffler 	void *rx_callback_ctx, int l2_hdr);
7439beb93cSSam Leffler 
7539beb93cSSam Leffler /**
765b9c547cSRui Paulo  * l2_packet_init_bridge - Like l2_packet_init() but with bridge workaround
775b9c547cSRui Paulo  *
785b9c547cSRui Paulo  * This version of l2_packet_init() can be used to enable a workaround for Linux
795b9c547cSRui Paulo  * packet socket in case of a station interface in a bridge.
805b9c547cSRui Paulo  */
815b9c547cSRui Paulo struct l2_packet_data * l2_packet_init_bridge(
825b9c547cSRui Paulo 	const char *br_ifname, const char *ifname, const u8 *own_addr,
835b9c547cSRui Paulo 	unsigned short protocol,
845b9c547cSRui Paulo 	void (*rx_callback)(void *ctx, const u8 *src_addr,
855b9c547cSRui Paulo 			    const u8 *buf, size_t len),
865b9c547cSRui Paulo 	void *rx_callback_ctx, int l2_hdr);
875b9c547cSRui Paulo 
885b9c547cSRui Paulo /**
8939beb93cSSam Leffler  * l2_packet_deinit - Deinitialize l2_packet interface
9039beb93cSSam Leffler  * @l2: Pointer to internal l2_packet data from l2_packet_init()
9139beb93cSSam Leffler  */
9239beb93cSSam Leffler void l2_packet_deinit(struct l2_packet_data *l2);
9339beb93cSSam Leffler 
9439beb93cSSam Leffler /**
9539beb93cSSam Leffler  * l2_packet_get_own_addr - Get own layer 2 address
9639beb93cSSam Leffler  * @l2: Pointer to internal l2_packet data from l2_packet_init()
9739beb93cSSam Leffler  * @addr: Buffer for the own address (6 bytes)
9839beb93cSSam Leffler  * Returns: 0 on success, -1 on failure
9939beb93cSSam Leffler  */
10039beb93cSSam Leffler int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr);
10139beb93cSSam Leffler 
10239beb93cSSam Leffler /**
10339beb93cSSam Leffler  * l2_packet_send - Send a packet
10439beb93cSSam Leffler  * @l2: Pointer to internal l2_packet data from l2_packet_init()
10539beb93cSSam Leffler  * @dst_addr: Destination address for the packet (only used if l2_hdr == 0)
10639beb93cSSam Leffler  * @proto: Protocol/ethertype for the packet in host byte order (only used if
10739beb93cSSam Leffler  * l2_hdr == 0)
10839beb93cSSam Leffler  * @buf: Packet contents to be sent; including layer 2 header if l2_hdr was
10939beb93cSSam Leffler  * set to 1 in l2_packet_init() call. Otherwise, only the payload of the packet
11039beb93cSSam Leffler  * is included.
11139beb93cSSam Leffler  * @len: Length of the buffer (including l2 header only if l2_hdr == 1)
11239beb93cSSam Leffler  * Returns: >=0 on success, <0 on failure
11339beb93cSSam Leffler  */
11439beb93cSSam Leffler int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
11539beb93cSSam Leffler 		   const u8 *buf, size_t len);
11639beb93cSSam Leffler 
11739beb93cSSam Leffler /**
11839beb93cSSam Leffler  * l2_packet_get_ip_addr - Get the current IP address from the interface
11939beb93cSSam Leffler  * @l2: Pointer to internal l2_packet data from l2_packet_init()
12039beb93cSSam Leffler  * @buf: Buffer for the IP address in text format
12139beb93cSSam Leffler  * @len: Maximum buffer length
12239beb93cSSam Leffler  * Returns: 0 on success, -1 on failure
12339beb93cSSam Leffler  *
12439beb93cSSam Leffler  * This function can be used to get the current IP address from the interface
12539beb93cSSam Leffler  * bound to the l2_packet. This is mainly for status information and the IP
12639beb93cSSam Leffler  * address will be stored as an ASCII string. This function is not essential
12739beb93cSSam Leffler  * for %wpa_supplicant operation, so full implementation is not required.
12839beb93cSSam Leffler  * l2_packet implementation will need to define the function, but it can return
12939beb93cSSam Leffler  * -1 if the IP address information is not available.
13039beb93cSSam Leffler  */
13139beb93cSSam Leffler int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len);
13239beb93cSSam Leffler 
13339beb93cSSam Leffler 
13439beb93cSSam Leffler /**
13539beb93cSSam Leffler  * l2_packet_notify_auth_start - Notify l2_packet about start of authentication
13639beb93cSSam Leffler  * @l2: Pointer to internal l2_packet data from l2_packet_init()
13739beb93cSSam Leffler  *
13839beb93cSSam Leffler  * This function is called when authentication is expected to start, e.g., when
13939beb93cSSam Leffler  * association has been completed, in order to prepare l2_packet implementation
14039beb93cSSam Leffler  * for EAPOL frames. This function is used mainly if the l2_packet code needs
14139beb93cSSam Leffler  * to do polling in which case it can increasing polling frequency. This can
14239beb93cSSam Leffler  * also be an empty function if the l2_packet implementation does not benefit
14339beb93cSSam Leffler  * from knowing about the starting authentication.
14439beb93cSSam Leffler  */
14539beb93cSSam Leffler void l2_packet_notify_auth_start(struct l2_packet_data *l2);
14639beb93cSSam Leffler 
1475b9c547cSRui Paulo /**
1485b9c547cSRui Paulo  * l2_packet_set_packet_filter - Set socket filter for l2_packet
1495b9c547cSRui Paulo  * @l2: Pointer to internal l2_packet data from l2_packet_init()
1505b9c547cSRui Paulo  * @type: enum l2_packet_filter_type, type of filter
1515b9c547cSRui Paulo  * Returns: 0 on success, -1 on failure
1525b9c547cSRui Paulo  *
1535b9c547cSRui Paulo  * This function is used to set the socket filter for l2_packet socket.
1545b9c547cSRui Paulo  *
1555b9c547cSRui Paulo  */
1565b9c547cSRui Paulo int l2_packet_set_packet_filter(struct l2_packet_data *l2,
1575b9c547cSRui Paulo 				enum l2_packet_filter_type type);
1585b9c547cSRui Paulo 
15939beb93cSSam Leffler #endif /* L2_PACKET_H */
160