l2_packet.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #ifndef L2_PACKET_H
  15. #define L2_PACKET_H
  16. /**
  17. * struct l2_packet_data - Internal l2_packet data structure
  18. *
  19. * This structure is used by the l2_packet implementation to store its private
  20. * data. Other files use a pointer to this data when calling the l2_packet
  21. * functions, but the contents of this structure should not be used directly
  22. * outside l2_packet implementation.
  23. */
  24. struct l2_packet_data;
  25. #ifdef _MSC_VER
  26. #pragma pack(push, 1)
  27. #endif /* _MSC_VER */
  28. struct l2_ethhdr {
  29. u8 h_dest[ETH_ALEN];
  30. u8 h_source[ETH_ALEN];
  31. be16 h_proto;
  32. } STRUCT_PACKED;
  33. #ifdef _MSC_VER
  34. #pragma pack(pop)
  35. #endif /* _MSC_VER */
  36. enum l2_packet_filter_type {
  37. L2_PACKET_FILTER_DHCP,
  38. L2_PACKET_FILTER_NDISC,
  39. };
  40. /**
  41. * l2_packet_init - Initialize l2_packet interface
  42. * @ifname: Interface name
  43. * @own_addr: Optional own MAC address if available from driver interface or
  44. * %NULL if not available
  45. * @protocol: Ethernet protocol number in host byte order
  46. * @rx_callback: Callback function that will be called for each received packet
  47. * @rx_callback_ctx: Callback data (ctx) for calls to rx_callback()
  48. * @l2_hdr: 1 = include layer 2 header, 0 = do not include header
  49. * Returns: Pointer to internal data or %NULL on failure
  50. *
  51. * rx_callback function will be called with src_addr pointing to the source
  52. * address (MAC address) of the the packet. If l2_hdr is set to 0, buf
  53. * points to len bytes of the payload after the layer 2 header and similarly,
  54. * TX buffers start with payload. This behavior can be changed by setting
  55. * l2_hdr=1 to include the layer 2 header in the data buffer.
  56. */
  57. struct l2_packet_data * l2_packet_init(
  58. const char *ifname, const u8 *own_addr, unsigned short protocol,
  59. void (*rx_callback)(void *ctx, const u8 *src_addr,
  60. const u8 *buf, size_t len),
  61. void *rx_callback_ctx, int l2_hdr);
  62. /**
  63. * l2_packet_init_bridge - Like l2_packet_init() but with bridge workaround
  64. *
  65. * This version of l2_packet_init() can be used to enable a workaround for Linux
  66. * packet socket in case of a station interface in a bridge.
  67. */
  68. struct l2_packet_data * l2_packet_init_bridge(
  69. const char *br_ifname, const char *ifname, const u8 *own_addr,
  70. unsigned short protocol,
  71. void (*rx_callback)(void *ctx, const u8 *src_addr,
  72. const u8 *buf, size_t len),
  73. void *rx_callback_ctx, int l2_hdr);
  74. /**
  75. * l2_packet_deinit - Deinitialize l2_packet interface
  76. * @l2: Pointer to internal l2_packet data from l2_packet_init()
  77. */
  78. void l2_packet_deinit(struct l2_packet_data *l2);
  79. /**
  80. * l2_packet_get_own_addr - Get own layer 2 address
  81. * @l2: Pointer to internal l2_packet data from l2_packet_init()
  82. * @addr: Buffer for the own address (6 bytes)
  83. * Returns: 0 on success, -1 on failure
  84. */
  85. int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr);
  86. /**
  87. * l2_packet_send - Send a packet
  88. * @l2: Pointer to internal l2_packet data from l2_packet_init()
  89. * @dst_addr: Destination address for the packet (only used if l2_hdr == 0)
  90. * @proto: Protocol/ethertype for the packet in host byte order (only used if
  91. * l2_hdr == 0)
  92. * @buf: Packet contents to be sent; including layer 2 header if l2_hdr was
  93. * set to 1 in l2_packet_init() call. Otherwise, only the payload of the packet
  94. * is included.
  95. * @len: Length of the buffer (including l2 header only if l2_hdr == 1)
  96. * Returns: >=0 on success, <0 on failure
  97. */
  98. int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
  99. const u8 *buf, size_t len);
  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. * l2_packet_notify_auth_start - Notify l2_packet about start of authentication
  117. * @l2: Pointer to internal l2_packet data from l2_packet_init()
  118. *
  119. * This function is called when authentication is expected to start, e.g., when
  120. * association has been completed, in order to prepare l2_packet implementation
  121. * for EAPOL frames. This function is used mainly if the l2_packet code needs
  122. * to do polling in which case it can increasing polling frequency. This can
  123. * also be an empty function if the l2_packet implementation does not benefit
  124. * from knowing about the starting authentication.
  125. */
  126. void l2_packet_notify_auth_start(struct l2_packet_data *l2);
  127. /**
  128. * l2_packet_set_packet_filter - Set socket filter for l2_packet
  129. * @l2: Pointer to internal l2_packet data from l2_packet_init()
  130. * @type: enum l2_packet_filter_type, type of filter
  131. * Returns: 0 on success, -1 on failure
  132. *
  133. * This function is used to set the socket filter for l2_packet socket.
  134. *
  135. */
  136. int l2_packet_set_packet_filter(struct l2_packet_data *l2,
  137. enum l2_packet_filter_type type);
  138. #endif /* L2_PACKET_H */