title: Linux IPv6 source address selection when interface has >1 IPv6 addresses: How to deprecate one?
body: <p>In the context of IPv6 source address selection for outgoing traffic in Linux:</p>
  
  <p>I have some IPv6 address(es) on the interface.
  I want the kernel to pick one of those as the source IPv6 addr.
  I don't want the kernel to pick <em>this</em> address I'm about to send it as the source address for outgoing packets.</p>
  
  <p>More concretely, in this snippet I would like for the kernel to select any other IPv6 address already on this interface when dontUseAsSourceAddressForOutgoingPkts is true.
  What flags will yield that effect?
  If I'm using the wrong ifaddrmsg struct for IPv6 addressing, which one should I be using?</p>
  
  <p>Snippet containing further context:</p>
  
  <pre><code>int
  NetLnkSock::IpAdd(const std::string &amp;ifname,
                    const IpAddr &amp;ipaddr,
                    int prefixlen,
                    bool dontUseAsSourceAddressForOutgoingPkts)
      ifreq ifr;
      nlmsghdr *nlh;
      ifaddrmsg *ifa;
      nlmsgerr *nlerr;
      static uint32_t msg_seq = 0;
      NlSock nlsock;
      LogDev::Ostream logostr;
  
      nlsock.bind();
      memset(&amp;ifr, 0, sizeof(ifr));
  
      if (ifname.size() &gt; IFNAMSIZ)
          throw NetLnkNameErr();
  
      copy(ifname.begin(), ifname.end(), ifr.ifr_name);
      ifr.ifr_name[ifname.end() - ifname.begin()] = '\0';
  
      nlh = (nlmsghdr *)rcvbuf;
  
      nlh-&gt;nlmsg_len = sizeof(nlmsghdr);
  
      nlh-&gt;nlmsg_type = RTM_NEWADDR;
      nlh-&gt;nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
  
      nlh-&gt;nlmsg_seq = ++msg_seq;
      nlh-&gt;nlmsg_pid = 0;
  
      ifa = (ifaddrmsg *)&amp;nlh[1];
      ifa-&gt;ifa_family = (ipaddr.is_v4()) ? AF_INET : AF_INET6;
      ifa-&gt;ifa_prefixlen = prefixlen;
      /*
       * My question is about the behavior of the kernel
       * vis a vis source address selection for outgoing traffic
       * where there are multiple IP's on this interface.
       * How do the flags below impact the kernel's choice
       * for source address selection?
       */
      ifa-&gt;ifa_flags = 
      (dontUseAsSourceAddressForOutgoingPkts &amp;&amp; ipaddr.is_v6()) ?
          (IFA_F_SECONDARY | IFA_F_DEPRECATED) : 0;
      /*
       * I would like for the kernel to select any other IPv6
       * address already on this interface when
       * dontUseAsSourceAddressForOutgoingPkts is true.
       * Will these flags yield that effect?
       */
      ifa-&gt;ifa_scope = RT_SCOPE_UNIVERSE;
      ifa-&gt;ifa_index = ifr.ifr_ifindex;
      nlh-&gt;nlmsg_len += sizeof(ifaddrmsg);
      if (ipaddr.is_v4()) {
          IpAddr ip4_bcast;
          char *buf = rcvbuf + nlh-&gt;nlmsg_len;
  
          ip4_bcast.create_netmask(prefixlen, ipaddr);
          ip4_bcast.from_v4(~ip4_bcast.get_v4() | ipaddr.get_v4());
  
          nlh-&gt;nlmsg_len += NLMSG_ALIGN(setRtAttr(buf, IFA_LOCAL,
                                    &amp;ipaddr.get_v4(), sizeof(in_addr_t)));
  
          /*
           * Always send the netmask and broadcast even on delete.
           * Linux seems to ignore the prefixlen set in the original
           * message and simply matches by ip address on deletes.
           */
          buf = rcvbuf + nlh-&gt;nlmsg_len;
          nlh-&gt;nlmsg_len += NLMSG_ALIGN(setRtAttr(buf, IFA_ADDRESS,
                                    &amp;ipaddr.get_v4(), sizeof(in_addr_t)));
  
          buf = rcvbuf + nlh-&gt;nlmsg_len;
          nlh-&gt;nlmsg_len += NLMSG_ALIGN(setRtAttr(buf, IFA_BROADCAST,
                                    &amp;ip4_bcast.get_v4(), sizeof(in_addr_t)));
  
  
      } else { /* AF_INET6 */
          char *buf = rcvbuf + nlh-&gt;nlmsg_len;
  
          buf = rcvbuf + nlh-&gt;nlmsg_len;
          if (ipaddr.domain() != RD_DEFAULT_ID) {       // Hal doesn't support route domains
              throw NetLnkIpAddrErr();
          }
          nlh-&gt;nlmsg_len += NLMSG_ALIGN(setRtAttr(buf, IFA_LOCAL,
                                        &amp;ipaddr.get_v6(), sizeof(in6_addr)));
          buf = rcvbuf + nlh-&gt;nlmsg_len;
          nlh-&gt;nlmsg_len += NLMSG_ALIGN(setRtAttr(buf, IFA_ADDRESS,
                                        &amp;ipaddr.get_v6(), sizeof(in6_addr)));
  
      }
      nlsock.sendNlReq(rcvbuf);
  }
  </code></pre>