libnfnetlink byte alignment
PATCH 1/3 libnfnetlink byte alignment
Aligns buffers to maximum alignment of architecture to make the cast of char pointers to struct pointers more portable.
src/libnetfilter_log.c +12 -6
@@ -107,7 +107,8 @@ u_int16_t queuenum, u_int8_t pf) { char buf[NFNL_HEADER_LEN - +NFA_LENGTH (sizeof (struct nfulnl_msg_config_cmd ))]; + +NFA_LENGTH (sizeof (struct nfulnl_msg_config_cmd ))] + __attribute__ ((aligned));struct nfulnl_msg_config_cmd cmd;struct nlmsghdr *nmh = (struct nlmsghdr *) buf;
@@ -292,7 +293,8 @@ u_int8_t mode, u_int32_t range) { char buf[NFNL_HEADER_LEN - +NFA_LENGTH (sizeof (struct nfulnl_msg_config_mode ))]; + +NFA_LENGTH (sizeof (struct nfulnl_msg_config_mode ))] + __attribute__ ((aligned));struct nfulnl_msg_config_mode params;struct nlmsghdr *nmh = (struct nlmsghdr *) buf;
@@ -309,7 +311,8 @@int nflog_set_timeout (struct nflog_g_handle *gh,u_int32_t timeout) { -char buf[NFNL_HEADER_LEN +NFA_LENGTH (sizeof (u_int32_t))]; + char buf[NFNL_HEADER_LEN+NFA_LENGTH(sizeof(u_int32_t))] + __attribute__ ((aligned));struct nlmsghdr *nmh = (struct nlmsghdr *) buf; nfnl_fill_hdr(gh->h->nfnlssh, nmh, 0, AF_UNSPEC, gh->id,
@@ -322,7 +325,8 @@int nflog_set_qthresh (struct nflog_g_handle *gh,u_int32_t qthresh) { -char buf[NFNL_HEADER_LEN +NFA_LENGTH (sizeof (u_int32_t))]; + char buf[NFNL_HEADER_LEN+NFA_LENGTH(sizeof(u_int32_t))] + __attribute__ ((aligned));struct nlmsghdr *nmh = (struct nlmsghdr *) buf; nfnl_fill_hdr(gh->h->nfnlssh, nmh, 0, AF_UNSPEC, gh->id,
@@ -335,7 +339,8 @@int nflog_set_nlbufsiz (struct nflog_g_handle *gh,u_int32_t nlbufsiz) { -char buf[NFNL_HEADER_LEN +NFA_LENGTH (sizeof (u_int32_t))]; + char buf[NFNL_HEADER_LEN+NFA_LENGTH(sizeof(u_int32_t))] + __attribute__ ((aligned));struct nlmsghdr *nmh = (struct nlmsghdr *) buf;int status;
@@ -355,7 +360,8 @@int nflog_set_flags (struct nflog_g_handle *gh,u_int16_t flags) { -char buf[NFNL_HEADER_LEN +NFA_LENGTH (sizeof (u_int16_t))]; + char buf[NFNL_HEADER_LEN+NFA_LENGTH(sizeof(u_int16_t))] + __attribute__ ((aligned));struct nlmsghdr *nmh = (struct nlmsghdr *) buf; nfnl_fill_hdr(gh->h->nfnlssh, nmh, 0, AF_UNSPEC, gh->id,
Quoted 4 lines · Fabian Hugelshofer
Aligns buffers to maximum alignment of architecture to make the cast of char pointers to struct pointers more portable.
Signed-off-by Fabian Hugelshofer <[email protected]>
They all seem fine to me, but a union might make it look a bit nicer :)
Quoted 8 lines · Patrick McHardy
Quoted 4 lines · Fabian Hugelshofer
Aligns buffers to maximum alignment of architecture to make the cast of char pointers to struct pointers more portable.
Signed-off-by Fabian Hugelshofer <[email protected]>They all seem fine to me, but a union might make it look a bit nicer :)
In libnfnetlink the union does not make sense, because multiple casts are done from the same buffer at different locations.
For libnetfilter-(conntrack|log) the union would be possible:
Original (aligned):
<< code begin >>
char buf[...] __attribute__ ((aligned));
struct nlmsghdr *nmh = (struct nlmsghdr *) buf;
nfnl_fill_hdr(h->nfnlssh, nmh, 0, pf, queuenum, ...); << code end >>
Union style:
<< code begin >>
union {
char buf[...];
struct nlmsghdr nmh;
} u;nfnl_fill_hdr(h->nfnlssh, &u.nmh, 0, pf, queuenum, ...); << code end >>
I can rewrite it like this, if desired...
Quoted 10 lines · Fabian Hugelshofer
Quoted 8 lines · Patrick McHardy
Quoted 4 lines · Fabian Hugelshofer
Aligns buffers to maximum alignment of architecture to make the cast of char pointers to struct pointers more portable.
Signed-off-by Fabian Hugelshofer <[email protected]>They all seem fine to me, but a union might make it look a bit nicer :)
Quoted 6 lines
In libnfnetlink the union does not make sense, because multiple casts are done from the same buffer at different locations.
For libnetfilter-(conntrack|log) the union would be possible:
[...]I can rewrite it like this, if desired...
I'll leave that up to Pablo, thanks for the explanation.
Pablo Neira Ayuso
maintainerQuoted 30 lines · Fabian Hugelshofer
Quoted 8 lines · Patrick McHardy
Quoted 4 lines · Fabian Hugelshofer
Aligns buffers to maximum alignment of architecture to make the cast of char pointers to struct pointers more portable.
Signed-off-by Fabian Hugelshofer <[email protected]>They all seem fine to me, but a union might make it look a bit nicer :)
In libnfnetlink the union does not make sense, because multiple casts are done from the same buffer at different locations.
For libnetfilter-(conntrack|log) the union would be possible:
Original (aligned):
<< code begin >>
char buf[...]__attribute__((aligned));
struct nlmsghdr *nmh = (struct nlmsghdr *) buf;
nfnl_fill_hdr(h->nfnlssh, nmh, 0, pf, queuenum, ...); << code end >>Union style:
<< code begin >>union { char buf[...]; struct nlmsghdr nmh; } u;
Quoted 4 lines
nfnl_fill_hdr(h->nfnlssh, &u.nmh, 0, pf, queuenum, ...); << code end >>I can rewrite it like this, if desired...
Please, use union wherever it is possible. BTW, it seems to me that the conntrack-tools may suffer from the same portability problem. The (supposed) fix, which is attached, is ugly but union does not help since the size of struct nf_conntrack is not known. I do not find a way to do this cleanly without removing the use of the stack for temporary object allocation.
Signature · 1 line
"Los honestos son inadaptados sociales" -- Les Luthiers
src/cache_wt.c +2 -2
@@ -28,7 +28,7 @@static void add_wt (struct us_conntrack *u) {int ret; -char __ct[nfct_maxsize ()]; + char __ct[nfct_maxsize()] __attribute__((aligned));struct nf_conntrack *ct = (struct nf_conntrack *)(void *) __ct; ret =nl_exist_conntrack (u->ct);
@@ -56,7 +56,7 @@ static void add_wt(struct us_conntrack *u)static void upd_wt (struct us_conntrack *u) { -char __ct[nfct_maxsize ()]; + char __ct[nfct_maxsize()] __attribute__((aligned));struct nf_conntrack *ct = (struct nf_conntrack *)(void *) __ct;memcpy (ct, u->ct,nfct_maxsize ());
src/conntrack.c +5 -5
@@ -747,7 +747,7 @@ static int update_cb(enum nf_conntrack_msg_type type, {int res;struct nf_conntrack *obj = data; -char __tmp[nfct_maxsize ()]; + char __tmp[nfct_maxsize()] __attribute__((aligned));struct nf_conntrack *tmp = (struct nf_conntrack *) (void *)__tmp;memcpy (tmp, obj,sizeof (__tmp));
@@ -873,14 +873,14 @@ int main(int argc, char *argv[])unsigned int type =0 , event_mask =0 , l4flags =0 , status =0 ;int res =0 ;int family =AF_UNSPEC ; -char __obj[nfct_maxsize ()]; -char __exptuple[nfct_maxsize ()]; -char __mask[nfct_maxsize ()]; + char __obj[nfct_maxsize()] __attribute__((aligned)); + char __exptuple[nfct_maxsize()] __attribute__((aligned)); + char __mask[nfct_maxsize()] __attribute__((aligned));struct nf_conntrack *obj = (struct nf_conntrack *)(void *) __obj;struct nf_conntrack *exptuple = (struct nf_conntrack *)(void *) __exptuple;struct nf_conntrack *mask = (struct nf_conntrack *)(void *) __mask; -char __exp[nfexp_maxsize ()]; + char __exp[nfexp_maxsize()] __attribute__((aligned));struct nf_expect *exp = (struct nf_expect *)(void *) __exp;int l3protonum;union ct_address ad;
src/sync-mode.c +1 -1
@@ -38,7 +38,7 @@static void do_mcast_handler_step (struct nethdr *net,size_t remain) {int query; -char __ct[nfct_maxsize ()]; + char __ct[nfct_maxsize()] __attribute__((aligned));struct nf_conntrack *ct = (struct nf_conntrack *)(void *) __ct;struct us_conntrack *u;
Quoted 8 lines · Pablo Neira Ayuso
Quoted 8 lines
Quoted 7 lines · Patrick McHardy
Quoted 4 lines · Fabian Hugelshofer
Aligns buffers to maximum alignment of architecture to make the cast of char pointers to struct pointers more portable.
Signed-off-by Fabian Hugelshofer <[email protected]>They all seem fine to me, but a union might make it look a bit nicer :)
[...]
Quoted 6 lines
Please, use union wherever it is possible. BTW, it seems to me that the conntrack-tools may suffer from the same portability problem. The (supposed) fix, which is attached, is ugly but union does not help since the size of struct
nf_conntrackis not known. I do not find a way to do this cleanly without removing the use of the stack for temporary object allocation.
As you noted, conntrack-tools can be affected as well. I did not have a look at it before, beacause I don't use it.
I don't see another way with the stack either. Dynamic memory allocation would help, but is not nicer either.
Do you want me to rewrite the patches for libnetfilter-(log|contrack) in the union style? I probably would put the union declaration in the file scope rather than anonymous declarations in the function scope, because it is the same everywhere.
Pablo Neira Ayuso
maintainerQuoted 8 lines · Fabian Hugelshofer
Quoted 6 lines · Pablo Neira Ayuso
Please, use union wherever it is possible. BTW, it seems to me that the conntrack-tools may suffer from the same portability problem. The (supposed) fix, which is attached, is ugly but union does not help since the size of struct
nf_conntrackis not known. I do not find a way to do this cleanly without removing the use of the stack for temporary object allocation.
Quoted 5 lines
As you noted, conntrack-tools can be affected as well. I did not have a look at it before, beacause I don't use it.
I don't see another way with the stack either. Dynamic memory allocation would help, but is not nicer either.
I see.
Quoted 4 lines
Do you want me to rewrite the patches for libnetfilter-(log|contrack) in the union style? I probably would put the union declaration in the file scope rather than anonymous declarations in the function scope, because it is the same everywhere.
I'm fine with it. Thanks Fabian.
Signature · 1 line
"Los honestos son inadaptados sociales" -- Les Luthiers
Quoted 2 lines · Fabian Hugelshofer
Aligns buffers to maximum alignment of architecture to make the cast of char pointers to struct pointers more portable.
Applied. Thanks Fabian.
Signature · 1 line
"Los honestos son inadaptados sociales" -- Les Luthiers