xref: /netbsd-src/external/bsd/wpa/dist/src/l2_packet/l2_packet.h (revision bb6183629cf165db498d8e1f4e2de129f7efb21c)
18dbcf02cSchristos /*
28dbcf02cSchristos  * WPA Supplicant - Layer2 packet interface definition
38dbcf02cSchristos  * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
48dbcf02cSchristos  *
5e604d861Schristos  * This software may be distributed under the terms of the BSD license.
6e604d861Schristos  * See README for more details.
78dbcf02cSchristos  *
88dbcf02cSchristos  * This file defines an interface for layer 2 (link layer) packet sending and
98dbcf02cSchristos  * receiving. l2_packet_linux.c is one implementation for such a layer 2
108dbcf02cSchristos  * implementation using Linux packet sockets and l2_packet_pcap.c another one
118dbcf02cSchristos  * using libpcap and libdnet. When porting %wpa_supplicant to other operating
128dbcf02cSchristos  * systems, a new l2_packet implementation may need to be added.
138dbcf02cSchristos  */
148dbcf02cSchristos 
158dbcf02cSchristos #ifndef L2_PACKET_H
168dbcf02cSchristos #define L2_PACKET_H
178dbcf02cSchristos 
188dbcf02cSchristos /**
198dbcf02cSchristos  * struct l2_packet_data - Internal l2_packet data structure
208dbcf02cSchristos  *
218dbcf02cSchristos  * This structure is used by the l2_packet implementation to store its private
228dbcf02cSchristos  * data. Other files use a pointer to this data when calling the l2_packet
238dbcf02cSchristos  * functions, but the contents of this structure should not be used directly
248dbcf02cSchristos  * outside l2_packet implementation.
258dbcf02cSchristos  */
268dbcf02cSchristos struct l2_packet_data;
278dbcf02cSchristos 
288dbcf02cSchristos #ifdef _MSC_VER
298dbcf02cSchristos #pragma pack(push, 1)
308dbcf02cSchristos #endif /* _MSC_VER */
318dbcf02cSchristos 
328dbcf02cSchristos struct l2_ethhdr {
338dbcf02cSchristos 	u8 h_dest[ETH_ALEN];
348dbcf02cSchristos 	u8 h_source[ETH_ALEN];
358dbcf02cSchristos 	be16 h_proto;
368dbcf02cSchristos } STRUCT_PACKED;
378dbcf02cSchristos 
388dbcf02cSchristos #ifdef _MSC_VER
398dbcf02cSchristos #pragma pack(pop)
408dbcf02cSchristos #endif /* _MSC_VER */
418dbcf02cSchristos 
42bb610346Schristos enum l2_packet_filter_type {
43bb610346Schristos 	L2_PACKET_FILTER_DHCP,
44bb610346Schristos 	L2_PACKET_FILTER_NDISC,
450a73ee0aSchristos 	L2_PACKET_FILTER_PKTTYPE,
46bb610346Schristos };
47bb610346Schristos 
488dbcf02cSchristos /**
498dbcf02cSchristos  * l2_packet_init - Initialize l2_packet interface
508dbcf02cSchristos  * @ifname: Interface name
518dbcf02cSchristos  * @own_addr: Optional own MAC address if available from driver interface or
528dbcf02cSchristos  *	%NULL if not available
538dbcf02cSchristos  * @protocol: Ethernet protocol number in host byte order
548dbcf02cSchristos  * @rx_callback: Callback function that will be called for each received packet
558dbcf02cSchristos  * @rx_callback_ctx: Callback data (ctx) for calls to rx_callback()
568dbcf02cSchristos  * @l2_hdr: 1 = include layer 2 header, 0 = do not include header
578dbcf02cSchristos  * Returns: Pointer to internal data or %NULL on failure
588dbcf02cSchristos  *
598dbcf02cSchristos  * rx_callback function will be called with src_addr pointing to the source
608dbcf02cSchristos  * address (MAC address) of the the packet. If l2_hdr is set to 0, buf
618dbcf02cSchristos  * points to len bytes of the payload after the layer 2 header and similarly,
628dbcf02cSchristos  * TX buffers start with payload. This behavior can be changed by setting
638dbcf02cSchristos  * l2_hdr=1 to include the layer 2 header in the data buffer.
64*bb618362Schristos  *
65*bb618362Schristos  * IF rx_callback is NULL, receive operation is not opened at all, i.e., only
66*bb618362Schristos  * the TX path and additional helper functions for fetching MAC and IP
67*bb618362Schristos  * addresses can be used.
688dbcf02cSchristos  */
698dbcf02cSchristos struct l2_packet_data * l2_packet_init(
708dbcf02cSchristos 	const char *ifname, const u8 *own_addr, unsigned short protocol,
718dbcf02cSchristos 	void (*rx_callback)(void *ctx, const u8 *src_addr,
728dbcf02cSchristos 			    const u8 *buf, size_t len),
738dbcf02cSchristos 	void *rx_callback_ctx, int l2_hdr);
748dbcf02cSchristos 
758dbcf02cSchristos /**
76bb610346Schristos  * l2_packet_init_bridge - Like l2_packet_init() but with bridge workaround
77bb610346Schristos  *
78bb610346Schristos  * This version of l2_packet_init() can be used to enable a workaround for Linux
79bb610346Schristos  * packet socket in case of a station interface in a bridge.
80bb610346Schristos  */
81bb610346Schristos struct l2_packet_data * l2_packet_init_bridge(
82bb610346Schristos 	const char *br_ifname, const char *ifname, const u8 *own_addr,
83bb610346Schristos 	unsigned short protocol,
84bb610346Schristos 	void (*rx_callback)(void *ctx, const u8 *src_addr,
85bb610346Schristos 			    const u8 *buf, size_t len),
86bb610346Schristos 	void *rx_callback_ctx, int l2_hdr);
87bb610346Schristos 
88bb610346Schristos /**
898dbcf02cSchristos  * l2_packet_deinit - Deinitialize l2_packet interface
908dbcf02cSchristos  * @l2: Pointer to internal l2_packet data from l2_packet_init()
918dbcf02cSchristos  */
928dbcf02cSchristos void l2_packet_deinit(struct l2_packet_data *l2);
938dbcf02cSchristos 
948dbcf02cSchristos /**
958dbcf02cSchristos  * l2_packet_get_own_addr - Get own layer 2 address
968dbcf02cSchristos  * @l2: Pointer to internal l2_packet data from l2_packet_init()
978dbcf02cSchristos  * @addr: Buffer for the own address (6 bytes)
988dbcf02cSchristos  * Returns: 0 on success, -1 on failure
998dbcf02cSchristos  */
1008dbcf02cSchristos int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr);
1018dbcf02cSchristos 
1028dbcf02cSchristos /**
1038dbcf02cSchristos  * l2_packet_send - Send a packet
1048dbcf02cSchristos  * @l2: Pointer to internal l2_packet data from l2_packet_init()
1058dbcf02cSchristos  * @dst_addr: Destination address for the packet (only used if l2_hdr == 0)
1068dbcf02cSchristos  * @proto: Protocol/ethertype for the packet in host byte order (only used if
1078dbcf02cSchristos  * l2_hdr == 0)
1088dbcf02cSchristos  * @buf: Packet contents to be sent; including layer 2 header if l2_hdr was
1098dbcf02cSchristos  * set to 1 in l2_packet_init() call. Otherwise, only the payload of the packet
1108dbcf02cSchristos  * is included.
1118dbcf02cSchristos  * @len: Length of the buffer (including l2 header only if l2_hdr == 1)
1128dbcf02cSchristos  * Returns: >=0 on success, <0 on failure
1138dbcf02cSchristos  */
1148dbcf02cSchristos int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
1158dbcf02cSchristos 		   const u8 *buf, size_t len);
1168dbcf02cSchristos 
1178dbcf02cSchristos /**
1188dbcf02cSchristos  * l2_packet_get_ip_addr - Get the current IP address from the interface
1198dbcf02cSchristos  * @l2: Pointer to internal l2_packet data from l2_packet_init()
1208dbcf02cSchristos  * @buf: Buffer for the IP address in text format
1218dbcf02cSchristos  * @len: Maximum buffer length
1228dbcf02cSchristos  * Returns: 0 on success, -1 on failure
1238dbcf02cSchristos  *
1248dbcf02cSchristos  * This function can be used to get the current IP address from the interface
1258dbcf02cSchristos  * bound to the l2_packet. This is mainly for status information and the IP
1268dbcf02cSchristos  * address will be stored as an ASCII string. This function is not essential
1278dbcf02cSchristos  * for %wpa_supplicant operation, so full implementation is not required.
1288dbcf02cSchristos  * l2_packet implementation will need to define the function, but it can return
1298dbcf02cSchristos  * -1 if the IP address information is not available.
1308dbcf02cSchristos  */
1318dbcf02cSchristos int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len);
1328dbcf02cSchristos 
1338dbcf02cSchristos 
1348dbcf02cSchristos /**
1358dbcf02cSchristos  * l2_packet_notify_auth_start - Notify l2_packet about start of authentication
1368dbcf02cSchristos  * @l2: Pointer to internal l2_packet data from l2_packet_init()
1378dbcf02cSchristos  *
1388dbcf02cSchristos  * This function is called when authentication is expected to start, e.g., when
1398dbcf02cSchristos  * association has been completed, in order to prepare l2_packet implementation
1408dbcf02cSchristos  * for EAPOL frames. This function is used mainly if the l2_packet code needs
1418dbcf02cSchristos  * to do polling in which case it can increasing polling frequency. This can
1428dbcf02cSchristos  * also be an empty function if the l2_packet implementation does not benefit
1438dbcf02cSchristos  * from knowing about the starting authentication.
1448dbcf02cSchristos  */
1458dbcf02cSchristos void l2_packet_notify_auth_start(struct l2_packet_data *l2);
1468dbcf02cSchristos 
147bb610346Schristos /**
148bb610346Schristos  * l2_packet_set_packet_filter - Set socket filter for l2_packet
149bb610346Schristos  * @l2: Pointer to internal l2_packet data from l2_packet_init()
150bb610346Schristos  * @type: enum l2_packet_filter_type, type of filter
151bb610346Schristos  * Returns: 0 on success, -1 on failure
152bb610346Schristos  *
153bb610346Schristos  * This function is used to set the socket filter for l2_packet socket.
154bb610346Schristos  *
155bb610346Schristos  */
156bb610346Schristos int l2_packet_set_packet_filter(struct l2_packet_data *l2,
157bb610346Schristos 				enum l2_packet_filter_type type);
158bb610346Schristos 
1598dbcf02cSchristos #endif /* L2_PACKET_H */
160