From: "Riccardo Paolo Bestetti" <pbl@bestov.io>
To: <netdev@vger.kernel.org>
Subject: IP_FREEBIND not working with SOCK_RAW socket
Date: Fri, 19 Mar 2021 08:58:01 +0100 [thread overview]
Message-ID: <CA1623NCR32W.28H2TORVKG2SL@enhorning> (raw)
Hey,
I noticed that the IP_FREEBIND socket option doesn't work with the
SOCK_RAW socket type, nor does the net.ipv4.ip_nonlocal_bind kernel
parameter. When attempting to bind a nonlocal address to such a socket,
EADDRNOTAVAIL is returned.
I briefly adventured into the Kernel Source in search of insights, but
soon got lost in the Dark Forest of The Structs and had to get rescued.
I've attach below a small program to reproduce the issue. It gives no
output whatsoever, so you might want to run it through strace.
You should attempt to run it with an actual local address to observe it
works:
# strace ./a.out 127.0.0.1
And then run it with a nonlocal address to observe it doesn't work both
without (ok) or with (not ok?) the IP_FREEBIND socket option:
# strace ./a.out 8.8.8.8
# strace ./a.out 8.8.8.8 freebind
Any insights?
Riccardo P. Bestetti
--
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main (int argc, char** argv) {
char src[16];
unsigned int freebind = 0;
if (argc < 2) {
fprintf(stderr, "Usage: out.a ADDR [ freebind ]\n");
return 1;
}
strncpy(src, argv[1], 16);
src[15] = 0;
if (argc >= 3 && strcmp(argv[2], "freebind") == 0)
freebind = 1;
struct sockaddr_in bind_addr;
socklen_t laddrlen = sizeof(bind_addr);
memset(&bind_addr, 0, laddrlen);
bind_addr.sin_family = AF_INET;
bind_addr.sin_port = htons(IPPROTO_GRE);
inet_pton(AF_INET, src, &bind_addr.sin_addr);
int fd = socket(AF_INET, SOCK_RAW, IPPROTO_GRE);
setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &freebind, sizeof(freebind));
bind(fd, (struct sockaddr *)&bind_addr, laddrlen);
return 0;
}
reply other threads:[~2021-03-19 8:58 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CA1623NCR32W.28H2TORVKG2SL@enhorning \
--to=pbl@bestov.io \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox