From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16F0EC433E6 for ; Fri, 19 Mar 2021 08:58:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DFF0764E09 for ; Fri, 19 Mar 2021 08:58:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229841AbhCSI5i convert rfc822-to-8bit (ORCPT ); Fri, 19 Mar 2021 04:57:38 -0400 Received: from mslow2.mail.gandi.net ([217.70.178.242]:56871 "EHLO mslow2.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229638AbhCSI5R (ORCPT ); Fri, 19 Mar 2021 04:57:17 -0400 Received: from relay13.mail.gandi.net (unknown [217.70.178.233]) by mslow2.mail.gandi.net (Postfix) with ESMTP id CD2A2424256 for ; Fri, 19 Mar 2021 08:35:49 +0000 (UTC) Received: from localhost (unknown [151.79.131.81]) (Authenticated sender: pbl@bestov.io) by relay13.mail.gandi.net (Postfix) with ESMTPSA id AA55B80015 for ; Fri, 19 Mar 2021 08:35:28 +0000 (UTC) Mime-Version: 1.0 Content-Transfer-Encoding: 8BIT Content-Type: text/plain; charset=UTF-8 Subject: IP_FREEBIND not working with SOCK_RAW socket From: "Riccardo Paolo Bestetti" To: Date: Fri, 19 Mar 2021 08:58:01 +0100 Message-Id: Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 #include #include #include 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; }