Skip to contentSkip to conversation

xtables: Fix compilation error with musl-libc

PATCH In review Joshua Lant 4 participants 5 messages Jul 9, 2024 – Aug 21, 2024 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xtables: Fix compilation error with musl-libc

Error compiling with musl-libc:
The commit hash 810f8568f44f5863c2350a39f4f5c8d60f762958 introduces the netinet/ether.h header into xtables.h, which causes an error due to the redefinition of the ethhdr struct, defined in linux/if_ether.h and netinet/ether.h.

This is is a known issue with musl-libc, with kernel headers providing guards against this happening when glibc is used:
https://wiki.musl-libc.org/faq (Q: Why am I getting “error: redefinition of struct ethhdr/tcphdr/etc”?)

The only value used from netinet/ether.h is ETH_ALEN, which is already set manually in libxtables/xtables.c. Move this definition to the header and eliminate the inclusion of netinet/if_ether.h.

---
 include/xtables.h    | 5 ++++-
 libxtables/xtables.c | 4 ----
 2 files changed, 4 insertions(+), 5 deletions(-)
include/xtables.h +4 -1
index ab856ebc..e80d0a8e 100644
@@ -12,13 +12,16 @@
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
-#include <netinet/ether.h>
 #include <netinet/in.h>
 #include <net/if.h>
 #include <linux/types.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter/x_tables.h>
 
+#ifndef ETH_ALEN
+#define ETH_ALEN 6
+#endif
+
 #ifndef IPPROTO_SCTP
 #define IPPROTO_SCTP 132
 #endif
libxtables/xtables.c +0 -4
index 7b370d48..b3219751 100644
@@ -71,10 +71,6 @@
 #define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
 #endif
 
-#ifndef ETH_ALEN
-#define ETH_ALEN 6
-#endif
-
 /* we need this for ip6?tables-restore.  ip6?tables-restore.c sets line to the
  * current line of the input file, in order  to give a more precise error
  * message.  ip6?tables itself doesn't need this, so it is initialized to the
Signature · 1 line
2.25.1
reviewer
#1 lore raw
Quoted 6 lines · Joshua Lant

Error compiling with musl-libc:
The commit hash 810f8568f44f5863c2350a39f4f5c8d60f762958 introduces the netinet/ether.h header into xtables.h, which causes an error due to the redefinition of the ethhdr struct, defined in linux/if_ether.h and netinet/ether.h.

Quoted 8 lines

This is is a known issue with musl-libc, with kernel headers providing guards against this happening when glibc is used:
https://wiki.musl-libc.org/faq (Q: Why am I getting “error: redefinition of struct ethhdr/tcphdr/etc”?)

The only value used from netinet/ether.h is ETH_ALEN, which is already set manually in libxtables/xtables.c. Move this definition to the header and eliminate the inclusion of netinet/if_ether.h.

Man, this crap keeps popping up. Last time we "fixed" it was in commit 0e7cf0ad306cd ("Revert "fix build for missing ETH_ALEN definition""). There, including netinet/ether.h was OK. Now it's problematic? Interestingly, linux/if_ether.h has this:

| /* allow libcs like musl to deactivate this, glibc does not implement this. */
| #ifndef __UAPI_DEF_ETHHDR
| #define __UAPI_DEF_ETHHDR               1
| #endif

So it's not like the other party is ignoring musl's needs. Does adding -D__UAPI_DEF_ETHHDR=0 fix the build? Should we maybe add a configure option for this instead of shooting the moving target?

Cheers, Phil

#2 lore raw
In reply to Joshua Lant
Quoted 6 lines · Joshua Lant

Error compiling with musl-libc:
The commit hash 810f8568f44f5863c2350a39f4f5c8d60f762958 introduces the netinet/ether.h header into xtables.h, which causes an error due to the redefinition of the ethhdr struct, defined in linux/if_ether.h and netinet/ether.h.

Quoted 8 lines

This is is a known issue with musl-libc, with kernel headers providing guards against this happening when glibc is used:
https://wiki.musl-libc.org/faq (Q: Why am I getting “error: redefinition of struct ethhdr/tcphdr/etc”?)

The only value used from netinet/ether.h is ETH_ALEN, which is already set manually in libxtables/xtables.c. Move this definition to the header and eliminate the inclusion of netinet/if_ether.h.

Any chance that musl headers are being used so this can be autodetected? Then, no option to pass -D__UAPI_DEF_ETHHDR=0 is required.

thanks

#3 lore raw
Quoted 12 lines · Pablo Neira Ayuso
Quoted 11 lines · Joshua Lant

Error compiling with musl-libc:
The commit hash 810f8568f44f5863c2350a39f4f5c8d60f762958 introduces the netinet/ether.h header into xtables.h, which causes an error due to the redefinition of the ethhdr struct, defined in linux/if_ether.h and netinet/ether.h.

This is is a known issue with musl-libc, with kernel headers providing guards against this happening when glibc is used:
https://wiki.musl-libc.org/faq (Q: Why am I getting “error: redefinition of struct ethhdr/tcphdr/etc”?)

Quoted 6 lines
Quoted 3 lines

The only value used from netinet/ether.h is ETH_ALEN, which is already set manually in libxtables/xtables.c. Move this definition to the header and eliminate the inclusion of netinet/if_ether.h.

Any chance that musl headers are being used so this can be autodetected? Then, no option to pass -D__UAPI_DEF_ETHHDR=0 is required.

To clarify, what I mean is if it is possible to autodetect that musl headers are used, then add this definition.

I'd prefer no new --option as you propose is required to handle this.

#4 lore raw
Quoted 17 lines · Pablo Neira Ayuso
Quoted 16 lines · Pablo Neira Ayuso
Quoted 14 lines · Joshua Lant

Error compiling with musl-libc:
The commit hash 810f8568f44f5863c2350a39f4f5c8d60f762958 introduces the netinet/ether.h header into xtables.h, which causes an error due to the redefinition of the ethhdr struct, defined in linux/if_ether.h and netinet/ether.h.

This is is a known issue with musl-libc, with kernel headers providing guards against this happening when glibc is used:
https://wiki.musl-libc.org/faq (Q: Why am I getting “error: redefinition of struct ethhdr/tcphdr/etc”?)

The only value used from netinet/ether.h is ETH_ALEN, which is already set manually in libxtables/xtables.c. Move this definition to the header and eliminate the inclusion of netinet/if_ether.h.

Quoted 7 lines
Quoted 2 lines

Any chance that musl headers are being used so this can be autodetected? Then, no option to pass -D__UAPI_DEF_ETHHDR=0 is required.

To clarify, what I mean is if it is possible to autodetect that musl headers are used, then add this definition.

I'd prefer no new --option as you propose is required to handle this.

There are a couple of approaches that I see.

1. Test whether netinet/if_ether.h sets __UAPI_DEF_ETHHDR to zero (in which case we are building with musl):

  saved_CPPFLAGS=${CPPFLAGS}
  CPPFLAGS=${regular_CPPFLAGS}
  AC_PREPROC_IFELSE([AC_LANG_SOURCE([[
  #include <netinet/if_ether.h>
  #if defined(__UAPI_DEF_ETHHDR) && __UAPI_DEF_ETHHDR == 0
  #else
  #error No __UAPI_DEF_ETHHDR
  #endif
  ]])], [AC_DEFINE([__UAPI_DEF_ETHHDR], [0], [Prevent multiple definitions of `struct ethhdr`])])
  CPPFLAGS=${saved_CPPFLAGS}

2. Just check ${host_os}:

  AS_IF([test "${host_os}" = "linux-musl"],
        [AC_DEFINE([__UAPI_DEF_ETHHDR], [0], [Prevent multiple definitions of `struct ethhdr`])])

J.