• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef CONFIG_WAPI
19 #ifndef ETH_P_WAI
20 #define ETH_P_WAI 0x88b4
21 #endif
22 #endif /* WAPI */
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 enum l2_packet_filter_type {
49 	L2_PACKET_FILTER_DHCP,
50 	L2_PACKET_FILTER_NDISC,
51 	L2_PACKET_FILTER_PKTTYPE,
52 };
53 
54 /**
55  * l2_packet_init - Initialize l2_packet interface
56  * @ifname: Interface name
57  * @own_addr: Optional own MAC address if available from driver interface or
58  *	%NULL if not available
59  * @protocol: Ethernet protocol number in host byte order
60  * @rx_callback: Callback function that will be called for each received packet
61  * @rx_callback_ctx: Callback data (ctx) for calls to rx_callback()
62  * @l2_hdr: 1 = include layer 2 header, 0 = do not include header
63  * Returns: Pointer to internal data or %NULL on failure
64  *
65  * rx_callback function will be called with src_addr pointing to the source
66  * address (MAC address) of the the packet. If l2_hdr is set to 0, buf
67  * points to len bytes of the payload after the layer 2 header and similarly,
68  * TX buffers start with payload. This behavior can be changed by setting
69  * l2_hdr=1 to include the layer 2 header in the data buffer.
70  *
71  * IF rx_callback is NULL, receive operation is not opened at all, i.e., only
72  * the TX path and additional helper functions for fetching MAC and IP
73  * addresses can be used.
74  */
75 struct l2_packet_data * l2_packet_init(
76 	const char *ifname, const u8 *own_addr, unsigned short protocol,
77 	void (*rx_callback)(void *ctx, const u8 *src_addr,
78 			    const u8 *buf, size_t len),
79 	void *rx_callback_ctx, int l2_hdr);
80 
81 /**
82  * l2_packet_init_bridge - Like l2_packet_init() but with bridge workaround
83  *
84  * This version of l2_packet_init() can be used to enable a workaround for Linux
85  * packet socket in case of a station interface in a bridge.
86  */
87 struct l2_packet_data * l2_packet_init_bridge(
88 	const char *br_ifname, const char *ifname, const u8 *own_addr,
89 	unsigned short protocol,
90 	void (*rx_callback)(void *ctx, const u8 *src_addr,
91 			    const u8 *buf, size_t len),
92 	void *rx_callback_ctx, int l2_hdr);
93 
94 /**
95  * l2_packet_deinit - Deinitialize l2_packet interface
96  * @l2: Pointer to internal l2_packet data from l2_packet_init()
97  */
98 void l2_packet_deinit(struct l2_packet_data *l2);
99 
100 /**
101  * l2_packet_get_own_addr - Get own layer 2 address
102  * @l2: Pointer to internal l2_packet data from l2_packet_init()
103  * @addr: Buffer for the own address (6 bytes)
104  * Returns: 0 on success, -1 on failure
105  */
106 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr);
107 
108 /**
109  * l2_packet_send - Send a packet
110  * @l2: Pointer to internal l2_packet data from l2_packet_init()
111  * @dst_addr: Destination address for the packet (only used if l2_hdr == 0)
112  * @proto: Protocol/ethertype for the packet in host byte order (only used if
113  * l2_hdr == 0)
114  * @buf: Packet contents to be sent; including layer 2 header if l2_hdr was
115  * set to 1 in l2_packet_init() call. Otherwise, only the payload of the packet
116  * is included.
117  * @len: Length of the buffer (including l2 header only if l2_hdr == 1)
118  * Returns: >=0 on success, <0 on failure
119  */
120 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
121 		   const u8 *buf, size_t len);
122 
123 /**
124  * l2_packet_get_ip_addr - Get the current IP address from the interface
125  * @l2: Pointer to internal l2_packet data from l2_packet_init()
126  * @buf: Buffer for the IP address in text format
127  * @len: Maximum buffer length
128  * Returns: 0 on success, -1 on failure
129  *
130  * This function can be used to get the current IP address from the interface
131  * bound to the l2_packet. This is mainly for status information and the IP
132  * address will be stored as an ASCII string. This function is not essential
133  * for %wpa_supplicant operation, so full implementation is not required.
134  * l2_packet implementation will need to define the function, but it can return
135  * -1 if the IP address information is not available.
136  */
137 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len);
138 
139 
140 /**
141  * l2_packet_notify_auth_start - Notify l2_packet about start of authentication
142  * @l2: Pointer to internal l2_packet data from l2_packet_init()
143  *
144  * This function is called when authentication is expected to start, e.g., when
145  * association has been completed, in order to prepare l2_packet implementation
146  * for EAPOL frames. This function is used mainly if the l2_packet code needs
147  * to do polling in which case it can increasing polling frequency. This can
148  * also be an empty function if the l2_packet implementation does not benefit
149  * from knowing about the starting authentication.
150  */
151 void l2_packet_notify_auth_start(struct l2_packet_data *l2);
152 
153 /**
154  * l2_packet_set_packet_filter - Set socket filter for l2_packet
155  * @l2: Pointer to internal l2_packet data from l2_packet_init()
156  * @type: enum l2_packet_filter_type, type of filter
157  * Returns: 0 on success, -1 on failure
158  *
159  * This function is used to set the socket filter for l2_packet socket.
160  *
161  */
162 int l2_packet_set_packet_filter(struct l2_packet_data *l2,
163 				enum l2_packet_filter_type type);
164 
165 #endif /* L2_PACKET_H */
166