[j-nsp] source address selection for RE generated traffic addresses to direct neighbors

Martin T m4rtntns at gmail.com
Tue Feb 5 10:44:12 EST 2019


Andy, Ivan,

thanks!

If it helps someone, then I wrote a SLAX function, which returns an
IPv4 address based on the same rules as Junos does when
default-address-selection is not enabled and destination is directly
connected:

/* $ip is a dst IP; $prefixes is a node-set containing src prefixes */
function test:first-matching-ip($ip, $prefixes) {

  var $ipint = test:ip-to-int($ip);

  var $matches := {
    for-each($prefixes) {

      var $prefix = jcs:split("/", .);
      var $hostbits = 32 - $prefix[2];
      var $netint = test:ip-to-int($prefix[1]);
      var $binstring = bit:from-int($netint);

      mvar $chop;
      set $chop = 0;

      for $i( 0 ... $hostbits - 1 ) {
        if ( $i < string-length($binstring) ) {
          var $shift = string-length($binstring) - $i;
          set $chop = $chop +
                  substring($binstring, $shift, 1) *
                  math:power(2, $i);
        }
      }

      var $minval = $netint - $chop;
      var $maxval = $minval + math:power(2, $hostbits) - 1;

      if ( $prefix[2] == 32 && $ip == $prefix[1] ) {
        <entry> {
          <ip> $prefix[1];
          <ipint> $ipint;
        }
      }
      else if ( $minval <= $ipint && $ipint <= $maxval ) {
        <entry> {
          <ip> $prefix[1];
          <ipint> $netint;
        }
      }
    }
  }

  mvar $maxip = 4294967296;
  mvar $firstip;

  for-each( $matches/entry ) {
    if ( ipint < $maxip ) {
      set $maxip = ipint;
      set $firstip = ip;
    }
  }
  result $firstip;
}


Martin


More information about the juniper-nsp mailing list