binding non local ip.

Eygene Ryabinkin rea at freebsd.org
Fri Jan 7 13:39:04 UTC 2011


Fri, Jan 07, 2011 at 01:57:21PM +0100, joris dedieu wrote:
> What do you think about it ?
[...]
> +static int     bindany = 0; /* 1 allows to bind a non local ip */
> +SYSCTL_INT(_net_inet_ip, OID_AUTO, bindany, CTLFLAG_RW, &bindany, 0,
> +    "Allow to bind a non local ip");

On at least 8.x, you will likely want to use VNET_* macros to enable
your new sysctl to be virtualized.  Something like this:
{{{
VNET_DEFINE(int, inp_bindany) = 0;
SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, bindany, CTLFLAG_RW,
        &VNET_NAME(inp_bindany), 0, "Force INP_BINDANY on all sockets");
}}}
and use VNET(inp_bindany) in subsequent code.

>                         if ((inp->inp_flags & INP_BINDANY) == 0 &&
> -                           ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
> -                               return (EADDRNOTAVAIL);
> +                           ifa_ifwithaddr_check((struct sockaddr *)sin) == 0) {
> +                               if(bindany > 0)
> +                                       inp->inp_flags |= INP_BINDANY;
> +                               else
> +                                       return (EADDRNOTAVAIL);
> +                       }

The check is better to be rewritten as
{{{
			if (VNET(inp_bindany) > 0)
				inp->inp_flags |= INP_BINDANY;
			else if ((inp->inp_flags & INP_BINDANY) == 0 &&
                            ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
                                return (EADDRNOTAVAIL);
}}}
it will save you two conditionals if bindany is enabled.  On the other
hand, if you will set the value of VNET(inp_bindany) to INP_BINDANY
instead of 1, you can even do
{{{
			inp->inp_flags |= VNET(inp_bindany);
			if ((inp->inp_flags & INP_BINDANY) == 0 &&
                            ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
                                return (EADDRNOTAVAIL);
}}}
and this will eliminate one branching instruction at the expense of
memory access and fast logical operation.
-- 
Eygene Ryabinkin                                        ,,,^..^,,,
[ Life's unfair - but root password helps!           | codelabs.ru ]
[ 82FE 06BC D497 C0DE 49EC  4FF0 16AF 9EAE 8152 ECFB | freebsd.org ]
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20110107/95ae36cf/attachment.pgp


More information about the freebsd-hackers mailing list