l2_packet.h 4.8 KB

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