l2_packet.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #ifndef L2_PACKET_H
  21. #define L2_PACKET_H
  22. /**
  23. * struct l2_packet_data - Internal l2_packet data structure
  24. *
  25. * This structure is used by the l2_packet implementation to store its private
  26. * data. Other files use a pointer to this data when calling the l2_packet
  27. * functions, but the contents of this structure should not be used directly
  28. * outside l2_packet implementation.
  29. */
  30. struct l2_packet_data;
  31. #ifdef _MSC_VER
  32. #pragma pack(push, 1)
  33. #endif /* _MSC_VER */
  34. struct l2_ethhdr {
  35. u8 h_dest[ETH_ALEN];
  36. u8 h_source[ETH_ALEN];
  37. be16 h_proto;
  38. } STRUCT_PACKED;
  39. #ifdef _MSC_VER
  40. #pragma pack(pop)
  41. #endif /* _MSC_VER */
  42. /**
  43. * l2_packet_init - Initialize l2_packet interface
  44. * @ifname: Interface name
  45. * @own_addr: Optional own MAC address if available from driver interface or
  46. * %NULL if not available
  47. * @protocol: Ethernet protocol number in host byte order
  48. * @rx_callback: Callback function that will be called for each received packet
  49. * @rx_callback_ctx: Callback data (ctx) for calls to rx_callback()
  50. * @l2_hdr: 1 = include layer 2 header, 0 = do not include header
  51. * Returns: Pointer to internal data or %NULL on failure
  52. *
  53. * rx_callback function will be called with src_addr pointing to the source
  54. * address (MAC address) of the the packet. If l2_hdr is set to 0, buf
  55. * points to len bytes of the payload after the layer 2 header and similarly,
  56. * TX buffers start with payload. This behavior can be changed by setting
  57. * l2_hdr=1 to include the layer 2 header in the data buffer.
  58. */
  59. struct l2_packet_data * l2_packet_init(
  60. const char *ifname, const u8 *own_addr, unsigned short protocol,
  61. void (*rx_callback)(void *ctx, const u8 *src_addr,
  62. const u8 *buf, size_t len),
  63. void *rx_callback_ctx, int l2_hdr);
  64. /**
  65. * l2_packet_deinit - Deinitialize l2_packet interface
  66. * @l2: Pointer to internal l2_packet data from l2_packet_init()
  67. */
  68. void l2_packet_deinit(struct l2_packet_data *l2);
  69. /**
  70. * l2_packet_get_own_addr - Get own layer 2 address
  71. * @l2: Pointer to internal l2_packet data from l2_packet_init()
  72. * @addr: Buffer for the own address (6 bytes)
  73. * Returns: 0 on success, -1 on failure
  74. */
  75. int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr);
  76. /**
  77. * l2_packet_send - Send a packet
  78. * @l2: Pointer to internal l2_packet data from l2_packet_init()
  79. * @dst_addr: Destination address for the packet (only used if l2_hdr == 0)
  80. * @proto: Protocol/ethertype for the packet in host byte order (only used if
  81. * l2_hdr == 0)
  82. * @buf: Packet contents to be sent; including layer 2 header if l2_hdr was
  83. * set to 1 in l2_packet_init() call. Otherwise, only the payload of the packet
  84. * is included.
  85. * @len: Length of the buffer (including l2 header only if l2_hdr == 1)
  86. * Returns: >=0 on success, <0 on failure
  87. */
  88. int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
  89. const u8 *buf, size_t len);
  90. /**
  91. * l2_packet_get_ip_addr - Get the current IP address from the interface
  92. * @l2: Pointer to internal l2_packet data from l2_packet_init()
  93. * @buf: Buffer for the IP address in text format
  94. * @len: Maximum buffer length
  95. * Returns: 0 on success, -1 on failure
  96. *
  97. * This function can be used to get the current IP address from the interface
  98. * bound to the l2_packet. This is mainly for status information and the IP
  99. * address will be stored as an ASCII string. This function is not essential
  100. * for %wpa_supplicant operation, so full implementation is not required.
  101. * l2_packet implementation will need to define the function, but it can return
  102. * -1 if the IP address information is not available.
  103. */
  104. int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len);
  105. /**
  106. * l2_packet_notify_auth_start - Notify l2_packet about start of authentication
  107. * @l2: Pointer to internal l2_packet data from l2_packet_init()
  108. *
  109. * This function is called when authentication is expected to start, e.g., when
  110. * association has been completed, in order to prepare l2_packet implementation
  111. * for EAPOL frames. This function is used mainly if the l2_packet code needs
  112. * to do polling in which case it can increasing polling frequency. This can
  113. * also be an empty function if the l2_packet implementation does not benefit
  114. * from knowing about the starting authentication.
  115. */
  116. void l2_packet_notify_auth_start(struct l2_packet_data *l2);
  117. #endif /* L2_PACKET_H */