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