0002-Don-t-depend-on-net-if_ether.h.patch 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. From 8ed750311482824e427db3b8b2cec6842ea8bc96 Mon Sep 17 00:00:00 2001
  2. From: Matthias Schiffer <mschiffer@universe-factory.net>
  3. Date: Thu, 25 Jun 2015 01:03:23 +0200
  4. Subject: [PATCH] Don't depend on net/if_ether.h
  5. Instead of adding compatiblity code to make this work with musl, just
  6. duplicate the needed definitions in fastd.
  7. [Backport from upstream commit 9ac7f3588dda7d175e04878e7b871a88306d13bf.
  8. Fixes missing 'sys/queue.h' issue when building with musl.]
  9. Signed-off-by: Rodrigo Rebello <rprebello@gmail.com>
  10. ---
  11. cmake/checks.cmake | 5 -----
  12. src/compat.h | 22 ----------------------
  13. src/config.y | 1 +
  14. src/fastd.h | 15 +++++++++++----
  15. src/fastd_config.h.in | 3 ---
  16. src/log.c | 2 +-
  17. src/peer.c | 7 ++++---
  18. src/receive.c | 2 +-
  19. src/send.c | 2 +-
  20. src/socket.c | 2 ++
  21. src/status.c | 1 +
  22. src/types.h | 1 +
  23. 12 files changed, 23 insertions(+), 40 deletions(-)
  24. diff --git a/cmake/checks.cmake b/cmake/checks.cmake
  25. index f9f0399..62d52ff 100644
  26. --- a/cmake/checks.cmake
  27. +++ b/cmake/checks.cmake
  28. @@ -54,11 +54,6 @@ if(NOT DARWIN)
  29. endif(NOT DARWIN)
  30. -set(CMAKE_EXTRA_INCLUDE_FILES "netinet/if_ether.h")
  31. -check_type_size("struct ethhdr" SIZEOF_ETHHDR)
  32. -string(COMPARE NOTEQUAL "${SIZEOF_ETHHDR}" "" HAVE_ETHHDR)
  33. -
  34. -
  35. set(CMAKE_REQUIRED_INCLUDES "sys/types.h")
  36. if(NOT DARWIN)
  37. diff --git a/src/compat.h b/src/compat.h
  38. index 991c268..29c9253 100644
  39. --- a/src/compat.h
  40. +++ b/src/compat.h
  41. @@ -39,32 +39,10 @@
  42. #include <unistd.h>
  43. #include <sys/types.h>
  44. -#include <sys/queue.h>
  45. #include <sys/socket.h>
  46. -#include <net/if.h>
  47. -#include <net/if_arp.h>
  48. #include <netinet/in.h>
  49. -#include <netinet/if_ether.h>
  50. -#ifndef ETH_ALEN
  51. -/** The length of a MAC address */
  52. -#define ETH_ALEN 6
  53. -#endif
  54. -
  55. -#ifndef ETH_HLEN
  56. -/** The length of the standard ethernet header */
  57. -#define ETH_HLEN 14
  58. -#endif
  59. -
  60. -#ifndef HAVE_ETHHDR
  61. -/** An ethernet header */
  62. -struct ethhdr {
  63. - uint8_t h_dest[ETH_ALEN]; /**< The destination MAC address field */
  64. - uint8_t h_source[ETH_ALEN]; /**< The source MAC address field */
  65. - uint16_t h_proto; /**< The EtherType/length field */
  66. -} __attribute__((packed));
  67. -#endif
  68. #if defined(USE_FREEBIND) && !defined(IP_FREEBIND)
  69. /** Compatiblity define for systems supporting, but not defining IP_FREEBIND */
  70. diff --git a/src/config.y b/src/config.y
  71. index f2f597f..5b00d7c 100644
  72. --- a/src/config.y
  73. +++ b/src/config.y
  74. @@ -33,6 +33,7 @@
  75. %code requires {
  76. #include <src/fastd.h>
  77. #include <arpa/inet.h>
  78. + #include <net/if.h>
  79. }
  80. %union {
  81. diff --git a/src/fastd.h b/src/fastd.h
  82. index f1b2f93..ec2316b 100644
  83. --- a/src/fastd.h
  84. +++ b/src/fastd.h
  85. @@ -58,7 +58,14 @@
  86. /** An ethernet address */
  87. struct __attribute__((__packed__)) fastd_eth_addr {
  88. - uint8_t data[ETH_ALEN]; /**< The bytes of the address */
  89. + uint8_t data[6]; /**< The bytes of the address */
  90. +};
  91. +
  92. +/** An ethernet header */
  93. +struct __attribute__((packed)) fastd_eth_header {
  94. + fastd_eth_addr_t dest; /**< The destination MAC address field */
  95. + fastd_eth_addr_t source; /**< The source MAC address field */
  96. + uint16_t proto; /**< The EtherType/length field */
  97. };
  98. @@ -408,7 +415,7 @@ static inline void fastd_setnonblock(int fd) {
  99. static inline size_t fastd_max_payload(void) {
  100. switch (conf.mode) {
  101. case MODE_TAP:
  102. - return conf.mtu+ETH_HLEN;
  103. + return conf.mtu + sizeof(fastd_eth_header_t);
  104. case MODE_TUN:
  105. return conf.mtu;
  106. default:
  107. @@ -420,14 +427,14 @@ static inline size_t fastd_max_payload(void) {
  108. /** Returns the source address of an ethernet packet */
  109. static inline fastd_eth_addr_t fastd_buffer_source_address(const fastd_buffer_t buffer) {
  110. fastd_eth_addr_t ret;
  111. - memcpy(&ret, buffer.data+offsetof(struct ethhdr, h_source), ETH_ALEN);
  112. + memcpy(&ret, buffer.data + offsetof(fastd_eth_header_t, source), sizeof(fastd_eth_addr_t));
  113. return ret;
  114. }
  115. /** Returns the destination address of an ethernet packet */
  116. static inline fastd_eth_addr_t fastd_buffer_dest_address(const fastd_buffer_t buffer) {
  117. fastd_eth_addr_t ret;
  118. - memcpy(&ret, buffer.data+offsetof(struct ethhdr, h_dest), ETH_ALEN);
  119. + memcpy(&ret, buffer.data + offsetof(fastd_eth_header_t, dest), sizeof(fastd_eth_addr_t));
  120. return ret;
  121. }
  122. diff --git a/src/fastd_config.h.in b/src/fastd_config.h.in
  123. index 6a55930..5f9c868 100644
  124. --- a/src/fastd_config.h.in
  125. +++ b/src/fastd_config.h.in
  126. @@ -35,9 +35,6 @@
  127. /** Defined if the platform supports the AI_ADDRCONFIG flag to getaddrinfo() */
  128. #cmakedefine HAVE_AI_ADDRCONFIG
  129. -/** Defined if the platform defines the \e ethhdr struct */
  130. -#cmakedefine HAVE_ETHHDR
  131. -
  132. /** Defined if the platform defines get_current_dir_name() */
  133. #cmakedefine HAVE_GET_CURRENT_DIR_NAME
  134. diff --git a/src/log.c b/src/log.c
  135. index 7d1538a..0727bbb 100644
  136. --- a/src/log.c
  137. +++ b/src/log.c
  138. @@ -74,7 +74,7 @@ size_t fastd_snprint_peer_address(char *buffer, size_t size, const fastd_peer_ad
  139. if (!bind_address && hide)
  140. return snprintf_safe(buffer, size, "[hidden]:%u", ntohs(address->in6.sin6_port));
  141. if (inet_ntop(AF_INET6, &address->in6.sin6_addr, addr_buf, sizeof(addr_buf))) {
  142. - char ifname_buf[IF_NAMESIZE];
  143. + char ifname_buf[IFNAMSIZ];
  144. if (!iface && IN6_IS_ADDR_LINKLOCAL(&address->in6.sin6_addr))
  145. iface = if_indextoname(address->in6.sin6_scope_id, ifname_buf);
  146. diff --git a/src/peer.c b/src/peer.c
  147. index 9ce5ca2..3313112 100644
  148. --- a/src/peer.c
  149. +++ b/src/peer.c
  150. @@ -34,13 +34,14 @@
  151. #include "poll.h"
  152. #include <arpa/inet.h>
  153. +#include <net/if.h>
  154. #include <sys/wait.h>
  155. /** Adds peer-specific fields to \e env */
  156. void fastd_peer_set_shell_env(fastd_shell_env_t *env, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr) {
  157. - /* both INET6_ADDRSTRLEN and IFNAMESIZE already include space for the zero termination, so there is no need to add space for the '%' here. */
  158. - char buf[INET6_ADDRSTRLEN+IF_NAMESIZE];
  159. + /* both INET6_ADDRSTRLEN and IFNAMSIZ already include space for the zero termination, so there is no need to add space for the '%' here. */
  160. + char buf[INET6_ADDRSTRLEN+IFNAMSIZ];
  161. fastd_shell_env_set(env, "PEER_NAME", peer ? peer->name : NULL);
  162. @@ -850,7 +851,7 @@ void fastd_peer_set_established(fastd_peer_t *peer) {
  163. /** Compares two MAC addresses */
  164. static inline int eth_addr_cmp(const fastd_eth_addr_t *addr1, const fastd_eth_addr_t *addr2) {
  165. - return memcmp(addr1->data, addr2->data, ETH_ALEN);
  166. + return memcmp(addr1->data, addr2->data, sizeof(fastd_eth_addr_t));
  167. }
  168. /** Compares two fastd_peer_eth_addr_t entries by their MAC addresses */
  169. diff --git a/src/receive.c b/src/receive.c
  170. index 2ee402a..6db5cfd 100644
  171. --- a/src/receive.c
  172. +++ b/src/receive.c
  173. @@ -289,7 +289,7 @@ void fastd_receive(fastd_socket_t *sock) {
  174. /** Handles a received and decrypted payload packet */
  175. void fastd_handle_receive(fastd_peer_t *peer, fastd_buffer_t buffer, bool reordered) {
  176. if (conf.mode == MODE_TAP) {
  177. - if (buffer.len < ETH_HLEN) {
  178. + if (buffer.len < sizeof(fastd_eth_header_t)) {
  179. pr_debug("received truncated packet");
  180. fastd_buffer_free(buffer);
  181. return;
  182. diff --git a/src/send.c b/src/send.c
  183. index caa7312..6d9f66f 100644
  184. --- a/src/send.c
  185. +++ b/src/send.c
  186. @@ -209,7 +209,7 @@ static inline bool send_data_tap_single(fastd_buffer_t buffer, fastd_peer_t *sou
  187. if (conf.mode != MODE_TAP)
  188. return false;
  189. - if (buffer.len < ETH_HLEN) {
  190. + if (buffer.len < sizeof(fastd_eth_header_t)) {
  191. pr_debug("truncated ethernet packet");
  192. fastd_buffer_free(buffer);
  193. return true;
  194. diff --git a/src/socket.c b/src/socket.c
  195. index e932148..03a11a6 100644
  196. --- a/src/socket.c
  197. +++ b/src/socket.c
  198. @@ -32,6 +32,8 @@
  199. #include "fastd.h"
  200. #include "poll.h"
  201. +#include <net/if.h>
  202. +
  203. /**
  204. Creates a new socket bound to a specific address
  205. diff --git a/src/status.c b/src/status.c
  206. index d0b8511..27d6b38 100644
  207. --- a/src/status.c
  208. +++ b/src/status.c
  209. @@ -39,6 +39,7 @@
  210. #include "peer.h"
  211. #include <json.h>
  212. +#include <net/if.h>
  213. #include <sys/un.h>
  214. diff --git a/src/types.h b/src/types.h
  215. index f380541..b684621 100644
  216. --- a/src/types.h
  217. +++ b/src/types.h
  218. @@ -87,6 +87,7 @@ typedef struct fastd_bind_address fastd_bind_address_t;
  219. typedef struct fastd_socket fastd_socket_t;
  220. typedef struct fastd_peer_group fastd_peer_group_t;
  221. typedef struct fastd_eth_addr fastd_eth_addr_t;
  222. +typedef struct fastd_eth_header fastd_eth_header_t;
  223. typedef struct fastd_peer fastd_peer_t;
  224. typedef struct fastd_peer_eth_addr fastd_peer_eth_addr_t;
  225. typedef struct fastd_remote fastd_remote_t;
  226. --
  227. 2.1.4