odhcpd.awk 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. # "bconf" = boolean, write conf file format rather than pipe records
  26. #
  27. ##############################################################################
  28. /^#/ {
  29. # We need to pick out DHCP v4 or v6 records
  30. net = $2 ; id = $3 ; cls = $4 ; hst = $5 ; adr = $9 ; adr2 = $10
  31. cdr = adr ;
  32. cdr2 = adr2 ;
  33. sub( /\/.*/, "", adr ) ;
  34. sub( /.*\//, "", cdr ) ;
  35. sub( /\/.*/, "", adr2 ) ;
  36. sub( /.*\//, "", cdr2 ) ;
  37. if ( bisolt == 1 ) {
  38. # TODO: this might be better with a substituion option,
  39. # or per DHCP pool do-not-DNS option, but its getting busy here.
  40. fqdn = net
  41. fqdn = sub( /\./, "-", fqdn ) ;
  42. fqdn = tolower( hst "." fqdn "." domain ) ;
  43. }
  44. else {
  45. fqdn = tolower( hst "." domain ) ;
  46. }
  47. if ( cls == "ipv4" ) {
  48. if ( NF == 8 ) {
  49. # odhcpd errata in field format without host name
  50. adr = $8 ; hst = "-" ; cdr = adr ;
  51. sub( /\/.*/, "", adr ) ;
  52. sub( /.*\//, "", cdr ) ;
  53. }
  54. if (( cdr == 32 ) && ( hst != "-" )) {
  55. # only for provided hostnames and full /32 assignments
  56. ptr = adr ; qpr = "" ; split( ptr, ptr, "." ) ;
  57. slaac = slaac_eui64( id ) ;
  58. if ( bconf == 1 ) {
  59. x = ( "local-data: \"" fqdn ". 120 IN A " adr "\"" ) ;
  60. y = ( "local-data-ptr: \"" adr " 120 " fqdn "\"" ) ;
  61. print ( x "\n" y ) > hostfile ;
  62. }
  63. else {
  64. for( i=1; i<=4; i++ ) { qpr = ( ptr[i] "." qpr) ; }
  65. x = ( fqdn ". 120 IN A " adr ) ;
  66. y = ( qpr "in-addr.arpa. 120 IN PTR " fqdn ) ;
  67. print ( x "\n" y ) > hostfile ;
  68. }
  69. if (( bslaac == 1 ) && ( slaac != 0 )) {
  70. # UCI option to discover IPV6 routed SLAAC addresses
  71. # NOT TODO - ping probe take too long when added in awk-rule loop
  72. cmd = ( "ip -6 --oneline route show dev " net ) ;
  73. while ( ( cmd | getline adr ) > 0 ) {
  74. if (( substr( adr, 1, 5 ) <= "fd00:" ) \
  75. && ( index( adr, "via" ) == 0 )) {
  76. # GA or ULA routed addresses only (not LL or MC)
  77. sub( /\/.*/, "", adr ) ;
  78. adr = ( adr slaac ) ;
  79. if ( split( adr, tmp0, ":" ) >= 8 ) {
  80. sub( "::", ":", adr ) ;
  81. }
  82. if ( bconf == 1 ) {
  83. x = ( "local-data: \"" fqdn ". 120 IN AAAA " adr "\"" ) ;
  84. y = ( "local-data-ptr: \"" adr " 120 " fqdn "\"" ) ;
  85. print ( x "\n" y ) > hostfile ;
  86. }
  87. else {
  88. qpr = ipv6_ptr( adr ) ;
  89. x = ( fqdn ". 120 IN AAAA " adr ) ;
  90. y = ( qpr ". 120 IN PTR " fqdn ) ;
  91. print ( x "\n" y ) > hostfile ;
  92. }
  93. }
  94. }
  95. close( cmd ) ;
  96. }
  97. }
  98. }
  99. else {
  100. if (( cdr == 128 ) && ( hst != "-" )) {
  101. if ( bconf == 1 ) {
  102. x = ( "local-data: \"" fqdn ". 120 IN AAAA " adr "\"" ) ;
  103. y = ( "local-data-ptr: \"" adr " 120 " fqdn "\"" ) ;
  104. print ( x "\n" y ) > hostfile ;
  105. }
  106. else {
  107. # only for provided hostnames and full /128 assignments
  108. qpr = ipv6_ptr( adr ) ;
  109. x = ( fqdn ". 120 IN AAAA " adr ) ;
  110. y = ( qpr ". 120 IN PTR " fqdn ) ;
  111. print ( x "\n" y ) > hostfile ;
  112. }
  113. }
  114. if (( cdr2 == 128 ) && ( hst != "-" )) {
  115. if ( bconf == 1 ) {
  116. x = ( "local-data: \"" fqdn ". 120 IN AAAA " adr2 "\"" ) ;
  117. y = ( "local-data-ptr: \"" adr2 " 120 " fqdn "\"" ) ;
  118. print ( x "\n" y ) > hostfile ;
  119. }
  120. else {
  121. # odhcp puts GA and ULA on the same line (position 9 and 10)
  122. qpr2 = ipv6_ptr( adr2 ) ;
  123. x = ( fqdn ". 120 IN AAAA " adr2 ) ;
  124. y = ( qpr2 ". 120 IN PTR " fqdn ) ;
  125. print ( x "\n" y ) > hostfile ;
  126. }
  127. }
  128. }
  129. }
  130. ##############################################################################
  131. function ipv6_ptr( ipv6, arpa, ary, end, i, j, new6, sz, start ) {
  132. # IPV6 colon flexibility is a challenge when creating [ptr].ip6.arpa.
  133. sz = split( ipv6, ary, ":" ) ; end = 9 - sz ;
  134. for( i=1; i<=sz; i++ ) {
  135. if( length(ary[i]) == 0 ) {
  136. for( j=1; j<=end; j++ ) { ary[i] = ( ary[i] "0000" ) ; }
  137. }
  138. else {
  139. ary[i] = substr( ( "0000" ary[i] ), length( ary[i] )+5-4 ) ;
  140. }
  141. }
  142. new6 = ary[1] ;
  143. for( i = 2; i <= sz; i++ ) { new6 = ( new6 ary[i] ) ; }
  144. start = length( new6 ) ;
  145. for( i=start; i>0; i-- ) { arpa = ( arpa substr( new6, i, 1 ) ) ; } ;
  146. gsub( /./, "&\.", arpa ) ; arpa = ( arpa "ip6.arpa" ) ;
  147. return arpa ;
  148. }
  149. ##############################################################################
  150. function slaac_eui64( mac, ary, glbit, eui64 ) {
  151. if ( length(mac) >= 12 ) {
  152. # RFC2373 and use DHCPv4 registered MAC to find SLAAC addresses
  153. split( mac , ary , "" ) ;
  154. glbit = ( "0x" ary[2] ) ;
  155. glbit = sprintf( "%d", glbit ) ;
  156. glbit = or( glbit, 2 ) ;
  157. ary[2] = sprintf( "%x", glbit ) ;
  158. eui64 = ( ary[1] ary[2] ary[3] ary[4] ":" ary[5] ary[6] "ff:fe" ) ;
  159. eui64 = ( eui64 ary[7] ary[8] ":" ary[9] ary[10] ary[11] ary[12] ) ;
  160. }
  161. else {
  162. eui64 = 0 ;
  163. }
  164. return eui64 ;
  165. }
  166. ##############################################################################