xref: /freebsd/contrib/wpa/src/l2_packet/l2_packet.h (revision b9f654b163bce26de79705e77b872427c9f2afa1)
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 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_init_bridge - Like l2_packet_init() but with bridge workaround
73  *
74  * This version of l2_packet_init() can be used to enable a workaround for Linux
75  * packet socket in case of a station interface in a bridge.
76  */
77 struct l2_packet_data * l2_packet_init_bridge(
78 	const char *br_ifname, const char *ifname, const u8 *own_addr,
79 	unsigned short protocol,
80 	void (*rx_callback)(void *ctx, const u8 *src_addr,
81 			    const u8 *buf, size_t len),
82 	void *rx_callback_ctx, int l2_hdr);
83 
84 /**
85  * l2_packet_deinit - Deinitialize l2_packet interface
86  * @l2: Pointer to internal l2_packet data from l2_packet_init()
87  */
88 void l2_packet_deinit(struct l2_packet_data *l2);
89 
90 /**
91  * l2_packet_get_own_addr - Get own layer 2 address
92  * @l2: Pointer to internal l2_packet data from l2_packet_init()
93  * @addr: Buffer for the own address (6 bytes)
94  * Returns: 0 on success, -1 on failure
95  */
96 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr);
97 
98 /**
99  * l2_packet_send - Send a packet
100  * @l2: Pointer to internal l2_packet data from l2_packet_init()
101  * @dst_addr: Destination address for the packet (only used if l2_hdr == 0)
102  * @proto: Protocol/ethertype for the packet in host byte order (only used if
103  * l2_hdr == 0)
104  * @buf: Packet contents to be sent; including layer 2 header if l2_hdr was
105  * set to 1 in l2_packet_init() call. Otherwise, only the payload of the packet
106  * is included.
107  * @len: Length of the buffer (including l2 header only if l2_hdr == 1)
108  * Returns: >=0 on success, <0 on failure
109  */
110 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
111 		   const u8 *buf, size_t len);
112 
113 /**
114  * l2_packet_get_ip_addr - Get the current IP address from the interface
115  * @l2: Pointer to internal l2_packet data from l2_packet_init()
116  * @buf: Buffer for the IP address in text format
117  * @len: Maximum buffer length
118  * Returns: 0 on success, -1 on failure
119  *
120  * This function can be used to get the current IP address from the interface
121  * bound to the l2_packet. This is mainly for status information and the IP
122  * address will be stored as an ASCII string. This function is not essential
123  * for %wpa_supplicant operation, so full implementation is not required.
124  * l2_packet implementation will need to define the function, but it can return
125  * -1 if the IP address information is not available.
126  */
127 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len);
128 
129 
130 /**
131  * l2_packet_notify_auth_start - Notify l2_packet about start of authentication
132  * @l2: Pointer to internal l2_packet data from l2_packet_init()
133  *
134  * This function is called when authentication is expected to start, e.g., when
135  * association has been completed, in order to prepare l2_packet implementation
136  * for EAPOL frames. This function is used mainly if the l2_packet code needs
137  * to do polling in which case it can increasing polling frequency. This can
138  * also be an empty function if the l2_packet implementation does not benefit
139  * from knowing about the starting authentication.
140  */
141 void l2_packet_notify_auth_start(struct l2_packet_data *l2);
142 
143 /**
144  * l2_packet_set_packet_filter - Set socket filter for l2_packet
145  * @l2: Pointer to internal l2_packet data from l2_packet_init()
146  * @type: enum l2_packet_filter_type, type of filter
147  * Returns: 0 on success, -1 on failure
148  *
149  * This function is used to set the socket filter for l2_packet socket.
150  *
151  */
152 int l2_packet_set_packet_filter(struct l2_packet_data *l2,
153 				enum l2_packet_filter_type type);
154 
155 #endif /* L2_PACKET_H */
156