xref: /freebsd/contrib/wpa/src/l2_packet/l2_packet.h (revision 9a14aa017b21c292740c00ee098195cd46642730)
1 /*
2  * WPA Supplicant - Layer2 packet interface definition
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 defines an interface for layer 2 (link layer) packet sending and
15  * receiving. l2_packet_linux.c is one implementation for such a layer 2
16  * implementation using Linux packet sockets and l2_packet_pcap.c another one
17  * using libpcap and libdnet. When porting %wpa_supplicant to other operating
18  * systems, a new l2_packet implementation may need to be added.
19  */
20 
21 #ifndef L2_PACKET_H
22 #define L2_PACKET_H
23 
24 /**
25  * struct l2_packet_data - Internal l2_packet data structure
26  *
27  * This structure is used by the l2_packet implementation to store its private
28  * data. Other files use a pointer to this data when calling the l2_packet
29  * functions, but the contents of this structure should not be used directly
30  * outside l2_packet implementation.
31  */
32 struct l2_packet_data;
33 
34 #ifdef _MSC_VER
35 #pragma pack(push, 1)
36 #endif /* _MSC_VER */
37 
38 struct l2_ethhdr {
39 	u8 h_dest[ETH_ALEN];
40 	u8 h_source[ETH_ALEN];
41 	be16 h_proto;
42 } STRUCT_PACKED;
43 
44 #ifdef _MSC_VER
45 #pragma pack(pop)
46 #endif /* _MSC_VER */
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 struct l2_packet_data * l2_packet_init(
66 	const char *ifname, const u8 *own_addr, unsigned short protocol,
67 	void (*rx_callback)(void *ctx, const u8 *src_addr,
68 			    const u8 *buf, size_t len),
69 	void *rx_callback_ctx, int l2_hdr);
70 
71 /**
72  * l2_packet_deinit - Deinitialize l2_packet interface
73  * @l2: Pointer to internal l2_packet data from l2_packet_init()
74  */
75 void l2_packet_deinit(struct l2_packet_data *l2);
76 
77 /**
78  * l2_packet_get_own_addr - Get own layer 2 address
79  * @l2: Pointer to internal l2_packet data from l2_packet_init()
80  * @addr: Buffer for the own address (6 bytes)
81  * Returns: 0 on success, -1 on failure
82  */
83 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr);
84 
85 /**
86  * l2_packet_send - Send a packet
87  * @l2: Pointer to internal l2_packet data from l2_packet_init()
88  * @dst_addr: Destination address for the packet (only used if l2_hdr == 0)
89  * @proto: Protocol/ethertype for the packet in host byte order (only used if
90  * l2_hdr == 0)
91  * @buf: Packet contents to be sent; including layer 2 header if l2_hdr was
92  * set to 1 in l2_packet_init() call. Otherwise, only the payload of the packet
93  * is included.
94  * @len: Length of the buffer (including l2 header only if l2_hdr == 1)
95  * Returns: >=0 on success, <0 on failure
96  */
97 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
98 		   const u8 *buf, size_t len);
99 
100 /**
101  * l2_packet_get_ip_addr - Get the current IP address from the interface
102  * @l2: Pointer to internal l2_packet data from l2_packet_init()
103  * @buf: Buffer for the IP address in text format
104  * @len: Maximum buffer length
105  * Returns: 0 on success, -1 on failure
106  *
107  * This function can be used to get the current IP address from the interface
108  * bound to the l2_packet. This is mainly for status information and the IP
109  * address will be stored as an ASCII string. This function is not essential
110  * for %wpa_supplicant operation, so full implementation is not required.
111  * l2_packet implementation will need to define the function, but it can return
112  * -1 if the IP address information is not available.
113  */
114 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len);
115 
116 
117 /**
118  * l2_packet_notify_auth_start - Notify l2_packet about start of authentication
119  * @l2: Pointer to internal l2_packet data from l2_packet_init()
120  *
121  * This function is called when authentication is expected to start, e.g., when
122  * association has been completed, in order to prepare l2_packet implementation
123  * for EAPOL frames. This function is used mainly if the l2_packet code needs
124  * to do polling in which case it can increasing polling frequency. This can
125  * also be an empty function if the l2_packet implementation does not benefit
126  * from knowing about the starting authentication.
127  */
128 void l2_packet_notify_auth_start(struct l2_packet_data *l2);
129 
130 #endif /* L2_PACKET_H */
131