You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.7 KiB

  1. #!/usr/bin/awk
  2. ##############################################################################
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License version 2 as
  6. # published by the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # Copyright (C) 2016 Eric Luehrsen
  14. #
  15. ##############################################################################
  16. #
  17. # Turn DHCP records into meaningful A, AAAA, and PTR records. Also lift a
  18. # function from dnsmasq and use DHCPv4 MAC to find IPV6 SLAAC hosts.
  19. #
  20. # External Parameters
  21. # "hostfile" = where this script will cache host DNS data
  22. # "domain" = text domain suffix
  23. # "bslaac" = boolean, use DHCPv4 MAC to find GA and ULA IPV6 SLAAC
  24. # "bisolt" = boolean, format <host>.<network>.<domain>. so you can isolate
  25. #
  26. ##############################################################################
  27. /^#/ {
  28. # We need to pick out DHCP v4 or v6 records
  29. net = $2 ; id = $3 ; cls = $4 ; hst = $5 ; adr = $9 ;
  30. cdr = adr ;
  31. sub( /\/.*/, "", adr ) ;
  32. sub( /.*\//, "", cdr ) ;
  33. if ( bisolt == 1 ) {
  34. # TODO: this might be better with a substituion option,
  35. # or per DHCP pool do-not-DNS option, but its getting busy here.
  36. fqdn = net
  37. fqdn = sub( /\./, "-", fqdn ) ;
  38. fqdn = tolower( hst "." fqdn "." domain ) ;
  39. }
  40. else {
  41. fqdn = tolower( hst "." domain ) ;
  42. }
  43. if ( cls == "ipv4" ) {
  44. if ( NF == 8 ) {
  45. # odhcpd errata in field format without host name
  46. adr = $8 ; hst = "-" ; cdr = adr ;
  47. sub( /\/.*/, "", adr ) ;
  48. sub( /.*\//, "", cdr ) ;
  49. }
  50. if (( cdr == 32 ) && ( hst != "-" )) {
  51. # only for provided hostnames and full /32 assignments
  52. ptr = adr ; qpr = "" ; split( ptr, ptr, "." ) ;
  53. slaac = slaac_eui64( id ) ;
  54. for( i=1; i<=4; i++ ) { qpr = ( ptr[i] "." qpr) ; }
  55. # DHCP A and PTR records with FQDN
  56. x = ( fqdn ". 120 IN A " adr ) ;
  57. y = ( qpr "in-addr.arpa. 120 IN PTR " fqdn ) ;
  58. print ( x "\n" y ) > hostfile ;
  59. if ((bslaac == 1) && (slaac != 0)) {
  60. # UCI option to discover IPV6 routed SLAAC addresses
  61. # NOT TODO - ping probe take too long when added in awk-rule loop
  62. cmd = ( "ip -6 --oneline route show dev " net ) ;
  63. while ( ( cmd | getline adr ) > 0 ) {
  64. if (( substr( adr, 1, 5 ) <= "fd00:" ) \
  65. && ( index( adr, "via" ) == 0 )) {
  66. # GA or ULA routed addresses only (not LL or MC)
  67. sub( /\/.*/, "", adr ) ;
  68. adr = ( adr slaac ) ;
  69. if ( split( adr, tmp0, ":" ) >= 8 ) { sub( "::", ":", adr ) ; }
  70. qpr = ipv6_ptr( adr ) ;
  71. x = ( fqdn ". 120 IN AAAA " adr ) ;
  72. y = ( qpr " 120 IN PTR " fqdn ) ;
  73. print ( x "\n" y ) > hostfile ;
  74. }
  75. }
  76. close( cmd ) ;
  77. }
  78. }
  79. }
  80. else {
  81. if (( cdr == 128 ) && ( hst != "-" )) {
  82. # only for provided hostnames and full /128 assignments
  83. qpr = ipv6_ptr( adr ) ;
  84. x = ( fqdn ". 120 IN AAAA " adr ) ;
  85. y = ( qpr " 120 IN PTR " fqdn ) ;
  86. print ( x "\n" y ) > hostfile ;
  87. }
  88. }
  89. }
  90. ##############################################################################
  91. function ipv6_ptr( ipv6, arpa, ary, end, i, j, new6, sz, start ) {
  92. # IPV6 colon flexibility is a challenge when creating [ptr].ip6.arpa.
  93. sz = split( ipv6, ary, ":" ) ; end = 9 - sz ;
  94. for( i=1; i<=sz; i++ ) {
  95. if( length(ary[i]) == 0 ) {
  96. for( j=1; j<=end; j++ ) { ary[i] = ( ary[i] "0000" ) ; }
  97. }
  98. else {
  99. ary[i] = substr( ( "0000" ary[i] ), length( ary[i] )+5-4 ) ;
  100. }
  101. }
  102. new6 = ary[1] ;
  103. for( i = 2; i <= sz; i++ ) { new6 = ( new6 ary[i] ) ; }
  104. start = length( new6 ) ;
  105. for( i=start; i>0; i-- ) { arpa = ( arpa substr( new6, i, 1 ) ) ; } ;
  106. gsub( /./, "&\.", arpa ) ; arpa = ( arpa "ip6.arpa" ) ;
  107. return arpa ;
  108. }
  109. ##############################################################################
  110. function slaac_eui64( mac, ary, glbit, eui64 ) {
  111. if ( length(mac) >= 12 ) {
  112. # RFC2373 and use DHCPv4 registered MAC to find SLAAC addresses
  113. split( mac , ary , "" ) ;
  114. glbit = ( "0x" ary[2] ) ;
  115. glbit = sprintf( "%d", glbit ) ;
  116. glbit = or( glbit, 2 ) ;
  117. ary[2] = sprintf( "%x", glbit ) ;
  118. eui64 = ( ary[1] ary[2] ary[3] ary[4] ":" ary[5] ary[6] "ff:fe" ) ;
  119. eui64 = ( eui64 ary[7] ary[8] ":" ary[9] ary[10] ary[11] ary[12] ) ;
  120. }
  121. else {
  122. eui64 = 0 ;
  123. }
  124. return eui64 ;
  125. }
  126. ##############################################################################