route compare function doesn't consider prefixlen

Thomas Graf tgraf at infradead.org
Wed Jan 11 10:55:52 EST 2012


On Wed, Jan 11, 2012 at 10:01:52AM -0500, Brett Ciphery wrote:
> The problem is the call to nl_addr_cmp from route_compare
> (route_obj.c:306):
> 
> diff |= ROUTE_DIFF(DST,         nl_addr_cmp(a->rt_dst, b->rt_dst));
> 
> doesn't consider prefixlen.  The below patch fixes it, but I see there
> is also an nl_addr_cmp_prefix() maybe meant to be used, but it wasn't
> giving me correct behaviour either when used inside route_compare.
> 
> 
> --- a/lib/addr.c
> +++ b/lib/addr.c
> @@ -497,7 +497,10 @@ int nl_addr_cmp(struct nl_addr *a, struct nl_addr
> *b)
>                 d = a->a_len - b->a_len;
> 
>                 if (a->a_len && d == 0)
> -                       return memcmp(a->a_addr, b->a_addr, a->a_len);
> +                       d = memcmp(a->a_addr, b->a_addr, a->a_len);
> +
> +                       if (d == 0)
> +                               return (a->a_prefixlen -
> b->a_prefixlen);
>         }
> 
>         return d;

I agree, this should be fixed like you describe. Not only for the
reason you mention but also to handle address notifications
correctly. ::1/64 ::1/128 are really different addresses and the
kernel explicitely allows to add them simultaneously.

Do you want to submit an official patch I can apply with git am?

Also, looking at nl_addr_cmp_prefix() the function looks broken.
I'll look into that.



More information about the libnl mailing list