Fixing nss-mdns for IPv6 linklocal scoped addresses

De Ensiwiki
Aller à : navigation, rechercher

Mycomputer.png  Deuxième Année  CDROM.png  Informatique 

The configuration

Running FreeBSD 8.2-RELEASE with GENERIC kernel.

Update: still works on 9 and 9.1

nss_mdns is a port of nss-mdns for FreeBSD. It adds support for mDNS address resolution system-wide through the use of a name service switch plugin. Here is the simplest configuration:

     # cd /usr/ports/net/avahi
     # make install
     # cd /usr/ports/dns/nss_mdns
     # make install

and make appropriate configuration changes

  • in /usr/local/etc/avahi/avahi-daemon.conf
  • in /etc/rc.conf
  • in /etc/nsswitch.conf
    hosts: files mdns dns

The problem

IPv6 is configured on all interfaces and no global prefix is defined. We only have linklocal addresses. mDNS resolution works fine, but ping6 fails miserably, even when specifying the outgoing interface.

   bsd1# RES_OPTIONS=inet6 getent hosts bsd2.local
   fe80::a00:27ff:fef4:c55d  bsd2.local
   bsd1# ping6 bsd2.local
   ping6: UDP connect: Device not configured
   bsd1# ping6 -I em0 bsd2.local
   ping6: UDP connect: Device not configured

This is not a routing problem, we would have Network is unreachable in this case, but an interface / scope id problem.

The bug

Even option -I in ping6 would not change anything. And when tracing down the scope id used, it turned out to be 673273636! Obviously, there is no such device configured. (cf. ping6.c:966 src.sin6_scope_id and dst.sin6_scope_id1)

The problem is in bsdnss.c where the address is copied in sin6_addr with a way to long length, ai->ai_addrlen, where it should be sizeof(struct in6_addr).

The quick fix and dirty enhancement

With this fixed, no more device not configured, but still a network unreachable. Although, using ping6 -I works nicely now. But it would be great to have the scope id filled in the data structure, which is possible at low cost, since avahi returns this information, the second field in the reply below.

   bsd1# nc -U /var/run/avahi-daemon/socket
   + 2 1 bsd2.local fe80::a00:27ff:fef4:c55d

A simple and dirty patch in query.h to add the scope_id to the data structure (we can do that safely, even if it is very ugly, because the buffer mallocated at nss.c:307 is huge), avahi.c to fill this info, and bsdnss.c to fill the struct sockaddr_in6 when we have a linklocal address, and that's it. Things work nicely now:

   # ping6 bsd2.local
   PING6(56=40+8+8 bytes) fe80::a00:27ff:fe9d:5a8f%em1 --> fe80::a00:27ff:fef4:c55d%em1
   16 bytes from fe80::a00:27ff:fef4:c55d%em1, icmp_seq=0 hlim=64 time=0.329 ms
   16 bytes from fe80::a00:27ff:fef4:c55d%em1, icmp_seq=1 hlim=64 time=0.657 ms
   --- bsd2.local ping6 statistics ---
   2 packets transmitted, 2 packets received, 0.0% packet loss
   round-trip min/avg/max/std-dev = 0.329/0.493/0.657/0.164 ms

The patch

This fix and enhancement were made only for the FreeBSD port, based on release-0.10.

  • [ git diff patch file] on branch release-0.10 (not needed if you use the port)
  • patch-src_AAA-nss-mdns-linklocal.c patch file for the FreeBSD port: this patch was made so that it can be applied before the port patches without problems (hence the stupid name)
    • copy this file in /usr/ports/dns/nss_mdns/files (then change back the capital P to p, mediawiki does that)
    • make install (deinstall before if needed)


  1. FreeBSD 8.2
  2. IPv6 on FreeBSD
  3. avahi
  4. nss-mdns
  5. nss_mdns port
  6. ping6 source code
  7. Installing avahi on FreeBSD
  8. Local Patches and Ports