prefix.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /*
  2. * Prefix related functions.
  3. * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "prefix.h"
  24. #include "vty.h"
  25. #include "sockunion.h"
  26. #include "memory.h"
  27. #include "log.h"
  28. /* Maskbit. */
  29. static const u_char maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
  30. 0xf8, 0xfc, 0xfe, 0xff};
  31. static const struct in6_addr maskbytes6[] =
  32. {
  33. /* /0 */ { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  34. /* /1 */ { { { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  35. /* /2 */ { { { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  36. /* /3 */ { { { 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  37. /* /4 */ { { { 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  38. /* /5 */ { { { 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  39. /* /6 */ { { { 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  40. /* /7 */ { { { 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  41. /* /8 */ { { { 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  42. /* /9 */ { { { 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  43. /* /10 */ { { { 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  44. /* /11 */ { { { 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  45. /* /12 */ { { { 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  46. /* /13 */ { { { 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  47. /* /14 */ { { { 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  48. /* /15 */ { { { 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  49. /* /16 */ { { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  50. /* /17 */ { { { 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  51. /* /18 */ { { { 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  52. /* /19 */ { { { 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  53. /* /20 */ { { { 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  54. /* /21 */ { { { 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  55. /* /22 */ { { { 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  56. /* /23 */ { { { 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  57. /* /24 */ { { { 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  58. /* /25 */ { { { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  59. /* /26 */ { { { 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  60. /* /27 */ { { { 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  61. /* /28 */ { { { 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  62. /* /29 */ { { { 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  63. /* /30 */ { { { 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  64. /* /31 */ { { { 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  65. /* /32 */ { { { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  66. /* /33 */ { { { 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  67. /* /34 */ { { { 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  68. /* /35 */ { { { 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  69. /* /36 */ { { { 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  70. /* /37 */ { { { 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  71. /* /38 */ { { { 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  72. /* /39 */ { { { 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  73. /* /40 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  74. /* /41 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  75. /* /42 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  76. /* /43 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  77. /* /44 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  78. /* /45 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  79. /* /46 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  80. /* /47 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  81. /* /48 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  82. /* /49 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  83. /* /50 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  84. /* /51 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  85. /* /52 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  86. /* /53 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  87. /* /54 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  88. /* /55 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  89. /* /56 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  90. /* /57 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  91. /* /58 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  92. /* /59 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  93. /* /60 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  94. /* /61 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  95. /* /62 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  96. /* /63 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  97. /* /64 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  98. /* /65 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  99. /* /66 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  100. /* /67 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  101. /* /68 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  102. /* /69 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  103. /* /70 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  104. /* /71 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  105. /* /72 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  106. /* /73 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  107. /* /74 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  108. /* /75 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  109. /* /76 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  110. /* /77 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  111. /* /78 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  112. /* /79 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  113. /* /80 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  114. /* /81 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  115. /* /82 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  116. /* /83 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  117. /* /84 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  118. /* /85 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  119. /* /86 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  120. /* /87 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  121. /* /88 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
  122. /* /89 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00 } } },
  123. /* /90 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00 } } },
  124. /* /91 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00 } } },
  125. /* /92 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00 } } },
  126. /* /93 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00 } } },
  127. /* /94 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00 } } },
  128. /* /95 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00 } } },
  129. /* /96 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 } } },
  130. /* /97 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00 } } },
  131. /* /98 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00 } } },
  132. /* /99 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00 } } },
  133. /* /100 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00 } } },
  134. /* /101 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00 } } },
  135. /* /102 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00 } } },
  136. /* /103 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00 } } },
  137. /* /104 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00 } } },
  138. /* /105 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 } } },
  139. /* /106 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00 } } },
  140. /* /107 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00 } } },
  141. /* /108 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00 } } },
  142. /* /109 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00 } } },
  143. /* /110 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00 } } },
  144. /* /111 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00 } } },
  145. /* /112 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00 } } },
  146. /* /113 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00 } } },
  147. /* /114 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00 } } },
  148. /* /115 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00 } } },
  149. /* /116 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00 } } },
  150. /* /117 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00 } } },
  151. /* /118 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00 } } },
  152. /* /119 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00 } } },
  153. /* /120 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 } } },
  154. /* /121 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 } } },
  155. /* /122 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0 } } },
  156. /* /123 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0 } } },
  157. /* /124 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 } } },
  158. /* /125 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8 } } },
  159. /* /126 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc } } },
  160. /* /127 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe } } },
  161. /* /128 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } }
  162. };
  163. /* Number of bits in prefix type. */
  164. #ifndef PNBBY
  165. #define PNBBY 8
  166. #endif /* PNBBY */
  167. #define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff)
  168. unsigned int
  169. prefix_bit (const u_char *prefix, const u_char prefixlen)
  170. {
  171. unsigned int offset = prefixlen / 8;
  172. unsigned int shift = 7 - (prefixlen % 8);
  173. return (prefix[offset] >> shift) & 1;
  174. }
  175. unsigned int
  176. prefix6_bit (const struct in6_addr *prefix, const u_char prefixlen)
  177. {
  178. return prefix_bit((const u_char *) &prefix->s6_addr, prefixlen);
  179. }
  180. int
  181. str2family(const char *string)
  182. {
  183. if (!strcmp("ipv4", string))
  184. return AF_INET;
  185. else if (!strcmp("ipv6", string))
  186. return AF_INET6;
  187. else if (!strcmp("ethernet", string))
  188. return AF_ETHERNET;
  189. return -1;
  190. }
  191. /* Address Famiy Identifier to Address Family converter. */
  192. int
  193. afi2family (afi_t afi)
  194. {
  195. if (afi == AFI_IP)
  196. return AF_INET;
  197. #ifdef HAVE_IPV6
  198. else if (afi == AFI_IP6)
  199. return AF_INET6;
  200. #endif /* HAVE_IPV6 */
  201. else if (afi == AFI_ETHER)
  202. return AF_ETHERNET;
  203. return 0;
  204. }
  205. afi_t
  206. family2afi (int family)
  207. {
  208. if (family == AF_INET)
  209. return AFI_IP;
  210. #ifdef HAVE_IPV6
  211. else if (family == AF_INET6)
  212. return AFI_IP6;
  213. #endif /* HAVE_IPV6 */
  214. else if (family == AF_ETHERNET)
  215. return AFI_ETHER;
  216. return 0;
  217. }
  218. const char *
  219. afi2str(afi_t afi)
  220. {
  221. switch (afi) {
  222. case AFI_IP:
  223. return "IPv4";
  224. case AFI_IP6:
  225. return "IPv6";
  226. case AFI_ETHER:
  227. return "ethernet";
  228. }
  229. return NULL;
  230. }
  231. const char *
  232. safi2str(safi_t safi)
  233. {
  234. switch (safi) {
  235. case SAFI_UNICAST:
  236. return "unicast";
  237. case SAFI_MULTICAST:
  238. return "multicast";
  239. case SAFI_ENCAP:
  240. return "encap";
  241. case SAFI_MPLS_VPN:
  242. return "vpn";
  243. }
  244. return NULL;
  245. }
  246. /* If n includes p prefix then return 1 else return 0. */
  247. int
  248. prefix_match (const struct prefix *n, const struct prefix *p)
  249. {
  250. int offset;
  251. int shift;
  252. const u_char *np, *pp;
  253. /* If n's prefix is longer than p's one return 0. */
  254. if (n->prefixlen > p->prefixlen)
  255. return 0;
  256. /* Set both prefix's head pointer. */
  257. np = (const u_char *)&n->u.prefix;
  258. pp = (const u_char *)&p->u.prefix;
  259. offset = n->prefixlen / PNBBY;
  260. shift = n->prefixlen % PNBBY;
  261. if (shift)
  262. if (maskbit[shift] & (np[offset] ^ pp[offset]))
  263. return 0;
  264. while (offset--)
  265. if (np[offset] != pp[offset])
  266. return 0;
  267. return 1;
  268. }
  269. /* Copy prefix from src to dest. */
  270. void
  271. prefix_copy (struct prefix *dest, const struct prefix *src)
  272. {
  273. dest->family = src->family;
  274. dest->prefixlen = src->prefixlen;
  275. if (src->family == AF_INET)
  276. dest->u.prefix4 = src->u.prefix4;
  277. #ifdef HAVE_IPV6
  278. else if (src->family == AF_INET6)
  279. dest->u.prefix6 = src->u.prefix6;
  280. #endif /* HAVE_IPV6 */
  281. else if (src->family == AF_UNSPEC)
  282. {
  283. dest->u.lp.id = src->u.lp.id;
  284. dest->u.lp.adv_router = src->u.lp.adv_router;
  285. }
  286. else if (src->family == AF_ETHERNET)
  287. {
  288. dest->u.prefix_eth = src->u.prefix_eth;
  289. }
  290. else
  291. {
  292. zlog (NULL, LOG_ERR, "prefix_copy(): Unknown address family %d",
  293. src->family);
  294. assert (0);
  295. }
  296. }
  297. /*
  298. * Return 1 if the address/netmask contained in the prefix structure
  299. * is the same, and else return 0. For this routine, 'same' requires
  300. * that not only the prefix length and the network part be the same,
  301. * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
  302. * the same. Note that this routine has the same return value sense
  303. * as '==' (which is different from prefix_cmp).
  304. */
  305. int
  306. prefix_same (const struct prefix *p1, const struct prefix *p2)
  307. {
  308. if (p1->family == p2->family && p1->prefixlen == p2->prefixlen)
  309. {
  310. if (p1->family == AF_INET)
  311. if (IPV4_ADDR_SAME (&p1->u.prefix4.s_addr, &p2->u.prefix4.s_addr))
  312. return 1;
  313. #ifdef HAVE_IPV6
  314. if (p1->family == AF_INET6 )
  315. if (IPV6_ADDR_SAME (&p1->u.prefix6.s6_addr, &p2->u.prefix6.s6_addr))
  316. return 1;
  317. #endif /* HAVE_IPV6 */
  318. if (p1->family == AF_ETHERNET) {
  319. if (!memcmp(p1->u.prefix_eth.octet, p2->u.prefix_eth.octet, ETHER_ADDR_LEN))
  320. return 1;
  321. }
  322. }
  323. return 0;
  324. }
  325. /*
  326. * Return 0 if the network prefixes represented by the struct prefix
  327. * arguments are the same prefix, and 1 otherwise. Network prefixes
  328. * are considered the same if the prefix lengths are equal and the
  329. * network parts are the same. Host bits (which are considered masked
  330. * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
  331. * 10.0.0.2/8 are considered equivalent by this routine. Note that
  332. * this routine has the same return sense as strcmp (which is different
  333. * from prefix_same).
  334. */
  335. int
  336. prefix_cmp (const struct prefix *p1, const struct prefix *p2)
  337. {
  338. int offset;
  339. int shift;
  340. /* Set both prefix's head pointer. */
  341. const u_char *pp1 = (const u_char *)&p1->u.prefix;
  342. const u_char *pp2 = (const u_char *)&p2->u.prefix;
  343. if (p1->family != p2->family || p1->prefixlen != p2->prefixlen)
  344. return 1;
  345. offset = p1->prefixlen / PNBBY;
  346. shift = p1->prefixlen % PNBBY;
  347. if (shift)
  348. if (maskbit[shift] & (pp1[offset] ^ pp2[offset]))
  349. return 1;
  350. while (offset--)
  351. if (pp1[offset] != pp2[offset])
  352. return 1;
  353. return 0;
  354. }
  355. /*
  356. * Count the number of common bits in 2 prefixes. The prefix length is
  357. * ignored for this function; the whole prefix is compared. If the prefix
  358. * address families don't match, return -1; otherwise the return value is
  359. * in range 0 ... maximum prefix length for the address family.
  360. */
  361. int
  362. prefix_common_bits (const struct prefix *p1, const struct prefix *p2)
  363. {
  364. int pos, bit;
  365. int length = 0;
  366. u_char xor;
  367. /* Set both prefix's head pointer. */
  368. const u_char *pp1 = (const u_char *)&p1->u.prefix;
  369. const u_char *pp2 = (const u_char *)&p2->u.prefix;
  370. if (p1->family == AF_INET)
  371. length = IPV4_MAX_BYTELEN;
  372. #ifdef HAVE_IPV6
  373. if (p1->family == AF_INET6)
  374. length = IPV6_MAX_BYTELEN;
  375. #endif
  376. if (p1->family != p2->family || !length)
  377. return -1;
  378. for (pos = 0; pos < length; pos++)
  379. if (pp1[pos] != pp2[pos])
  380. break;
  381. if (pos == length)
  382. return pos * 8;
  383. xor = pp1[pos] ^ pp2[pos];
  384. for (bit = 0; bit < 8; bit++)
  385. if (xor & (1 << (7 - bit)))
  386. break;
  387. return pos * 8 + bit;
  388. }
  389. /* Return prefix family type string. */
  390. const char *
  391. prefix_family_str (const struct prefix *p)
  392. {
  393. if (p->family == AF_INET)
  394. return "inet";
  395. #ifdef HAVE_IPV6
  396. if (p->family == AF_INET6)
  397. return "inet6";
  398. #endif /* HAVE_IPV6 */
  399. if (p->family == AF_ETHERNET)
  400. return "ether";
  401. return "unspec";
  402. }
  403. /* Allocate new prefix_ipv4 structure. */
  404. struct prefix_ipv4 *
  405. prefix_ipv4_new ()
  406. {
  407. struct prefix_ipv4 *p;
  408. /* Call prefix_new to allocate a full-size struct prefix to avoid problems
  409. where the struct prefix_ipv4 is cast to struct prefix and unallocated
  410. bytes were being referenced (e.g. in structure assignments). */
  411. p = (struct prefix_ipv4 *)prefix_new();
  412. p->family = AF_INET;
  413. return p;
  414. }
  415. /* Free prefix_ipv4 structure. */
  416. void
  417. prefix_ipv4_free (struct prefix_ipv4 *p)
  418. {
  419. prefix_free((struct prefix *)p);
  420. }
  421. /* When string format is invalid return 0. */
  422. int
  423. str2prefix_ipv4 (const char *str, struct prefix_ipv4 *p)
  424. {
  425. int ret;
  426. int plen;
  427. char *pnt;
  428. char *cp;
  429. /* Find slash inside string. */
  430. pnt = strchr (str, '/');
  431. /* String doesn't contail slash. */
  432. if (pnt == NULL)
  433. {
  434. /* Convert string to prefix. */
  435. ret = inet_aton (str, &p->prefix);
  436. if (ret == 0)
  437. return 0;
  438. /* If address doesn't contain slash we assume it host address. */
  439. p->family = AF_INET;
  440. p->prefixlen = IPV4_MAX_BITLEN;
  441. return ret;
  442. }
  443. else
  444. {
  445. cp = XMALLOC (MTYPE_TMP, (pnt - str) + 1);
  446. strncpy (cp, str, pnt - str);
  447. *(cp + (pnt - str)) = '\0';
  448. ret = inet_aton (cp, &p->prefix);
  449. XFREE (MTYPE_TMP, cp);
  450. /* Get prefix length. */
  451. plen = (u_char) atoi (++pnt);
  452. if (plen > IPV4_MAX_PREFIXLEN)
  453. return 0;
  454. p->family = AF_INET;
  455. p->prefixlen = plen;
  456. }
  457. return ret;
  458. }
  459. /* When string format is invalid return 0. */
  460. int
  461. str2prefix_eth (const char *str, struct prefix_eth *p)
  462. {
  463. int ret = 0;
  464. int plen = 48;
  465. char *pnt;
  466. char *cp = NULL;
  467. const char *str_addr = str;
  468. unsigned int a[6];
  469. int i;
  470. /* Find slash inside string. */
  471. pnt = strchr (str, '/');
  472. if (pnt)
  473. {
  474. /* Get prefix length. */
  475. plen = (u_char) atoi (++pnt);
  476. if (plen > 48)
  477. {
  478. ret = 0;
  479. goto done;
  480. }
  481. cp = XMALLOC (MTYPE_TMP, (pnt - str) + 1);
  482. strncpy (cp, str, pnt - str);
  483. *(cp + (pnt - str)) = '\0';
  484. str_addr = cp;
  485. }
  486. /* Convert string to prefix. */
  487. if (sscanf(str_addr, "%2x:%2x:%2x:%2x:%2x:%2x",
  488. a+0, a+1, a+2, a+3, a+4, a+5) != 6)
  489. {
  490. ret = 0;
  491. goto done;
  492. }
  493. for (i = 0; i < 6; ++i)
  494. {
  495. p->eth_addr.octet[i] = a[i] & 0xff;
  496. }
  497. p->prefixlen = plen;
  498. p->family = AF_ETHERNET;
  499. ret = 1;
  500. done:
  501. if (cp)
  502. XFREE (MTYPE_TMP, cp);
  503. return ret;
  504. }
  505. /* Convert masklen into IP address's netmask (network byte order). */
  506. void
  507. masklen2ip (const int masklen, struct in_addr *netmask)
  508. {
  509. assert (masklen >= 0 && masklen <= IPV4_MAX_BITLEN);
  510. /* left shift is only defined for less than the size of the type.
  511. * we unconditionally use long long in case the target platform
  512. * has defined behaviour for << 32 (or has a 64-bit left shift) */
  513. if (sizeof(unsigned long long) > 4)
  514. netmask->s_addr = htonl(0xffffffffULL << (32 - masklen));
  515. else
  516. netmask->s_addr = htonl(masklen ? 0xffffffffU << (32 - masklen) : 0);
  517. }
  518. /* Convert IP address's netmask into integer. We assume netmask is
  519. sequential one. Argument netmask should be network byte order. */
  520. u_char
  521. ip_masklen (struct in_addr netmask)
  522. {
  523. uint32_t tmp = ~ntohl(netmask.s_addr);
  524. if (tmp)
  525. /* clz: count leading zeroes. sadly, the behaviour of this builtin
  526. * is undefined for a 0 argument, even though most CPUs give 32 */
  527. return __builtin_clz(tmp);
  528. else
  529. return 32;
  530. }
  531. /* Apply mask to IPv4 prefix (network byte order). */
  532. void
  533. apply_mask_ipv4 (struct prefix_ipv4 *p)
  534. {
  535. struct in_addr mask;
  536. masklen2ip(p->prefixlen, &mask);
  537. p->prefix.s_addr &= mask.s_addr;
  538. }
  539. /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
  540. int
  541. prefix_ipv4_any (const struct prefix_ipv4 *p)
  542. {
  543. return (p->prefix.s_addr == 0 && p->prefixlen == 0);
  544. }
  545. #ifdef HAVE_IPV6
  546. /* Allocate a new ip version 6 route */
  547. struct prefix_ipv6 *
  548. prefix_ipv6_new (void)
  549. {
  550. struct prefix_ipv6 *p;
  551. /* Allocate a full-size struct prefix to avoid problems with structure
  552. size mismatches. */
  553. p = (struct prefix_ipv6 *)prefix_new();
  554. p->family = AF_INET6;
  555. return p;
  556. }
  557. /* Free prefix for IPv6. */
  558. void
  559. prefix_ipv6_free (struct prefix_ipv6 *p)
  560. {
  561. prefix_free((struct prefix *)p);
  562. }
  563. /* If given string is valid return pin6 else return NULL */
  564. int
  565. str2prefix_ipv6 (const char *str, struct prefix_ipv6 *p)
  566. {
  567. char *pnt;
  568. char *cp;
  569. int ret;
  570. pnt = strchr (str, '/');
  571. /* If string doesn't contain `/' treat it as host route. */
  572. if (pnt == NULL)
  573. {
  574. ret = inet_pton (AF_INET6, str, &p->prefix);
  575. if (ret == 0)
  576. return 0;
  577. p->prefixlen = IPV6_MAX_BITLEN;
  578. }
  579. else
  580. {
  581. int plen;
  582. cp = XMALLOC (MTYPE_TMP, (pnt - str) + 1);
  583. strncpy (cp, str, pnt - str);
  584. *(cp + (pnt - str)) = '\0';
  585. ret = inet_pton (AF_INET6, cp, &p->prefix);
  586. free (cp);
  587. if (ret == 0)
  588. return 0;
  589. plen = (u_char) atoi (++pnt);
  590. if (plen > IPV6_MAX_BITLEN)
  591. return 0;
  592. p->prefixlen = plen;
  593. }
  594. p->family = AF_INET6;
  595. return ret;
  596. }
  597. /* Convert struct in6_addr netmask into integer.
  598. * FIXME return u_char as ip_maskleni() does. */
  599. int
  600. ip6_masklen (struct in6_addr netmask)
  601. {
  602. int len = 0;
  603. unsigned char val;
  604. unsigned char *pnt;
  605. pnt = (unsigned char *) & netmask;
  606. while ((*pnt == 0xff) && len < IPV6_MAX_BITLEN)
  607. {
  608. len += 8;
  609. pnt++;
  610. }
  611. if (len < IPV6_MAX_BITLEN)
  612. {
  613. val = *pnt;
  614. while (val)
  615. {
  616. len++;
  617. val <<= 1;
  618. }
  619. }
  620. return len;
  621. }
  622. void
  623. masklen2ip6 (const int masklen, struct in6_addr *netmask)
  624. {
  625. assert (masklen >= 0 && masklen <= IPV6_MAX_BITLEN);
  626. memcpy (netmask, maskbytes6 + masklen, sizeof (struct in6_addr));
  627. }
  628. void
  629. apply_mask_ipv6 (struct prefix_ipv6 *p)
  630. {
  631. u_char *pnt;
  632. int index;
  633. int offset;
  634. index = p->prefixlen / 8;
  635. if (index < 16)
  636. {
  637. pnt = (u_char *) &p->prefix;
  638. offset = p->prefixlen % 8;
  639. pnt[index] &= maskbit[offset];
  640. index++;
  641. while (index < 16)
  642. pnt[index++] = 0;
  643. }
  644. }
  645. void
  646. str2in6_addr (const char *str, struct in6_addr *addr)
  647. {
  648. int i;
  649. unsigned int x;
  650. /* %x must point to unsinged int */
  651. for (i = 0; i < 16; i++)
  652. {
  653. sscanf (str + (i * 2), "%02x", &x);
  654. addr->s6_addr[i] = x & 0xff;
  655. }
  656. }
  657. #endif /* HAVE_IPV6 */
  658. void
  659. apply_mask (struct prefix *p)
  660. {
  661. switch (p->family)
  662. {
  663. case AF_INET:
  664. apply_mask_ipv4 ((struct prefix_ipv4 *)p);
  665. break;
  666. #ifdef HAVE_IPV6
  667. case AF_INET6:
  668. apply_mask_ipv6 ((struct prefix_ipv6 *)p);
  669. break;
  670. #endif /* HAVE_IPV6 */
  671. default:
  672. break;
  673. }
  674. return;
  675. }
  676. /* Utility function of convert between struct prefix <=> union sockunion.
  677. * FIXME This function isn't used anywhere. */
  678. struct prefix *
  679. sockunion2prefix (const union sockunion *dest,
  680. const union sockunion *mask)
  681. {
  682. if (dest->sa.sa_family == AF_INET)
  683. {
  684. struct prefix_ipv4 *p;
  685. p = prefix_ipv4_new ();
  686. p->family = AF_INET;
  687. p->prefix = dest->sin.sin_addr;
  688. p->prefixlen = ip_masklen (mask->sin.sin_addr);
  689. return (struct prefix *) p;
  690. }
  691. #ifdef HAVE_IPV6
  692. if (dest->sa.sa_family == AF_INET6)
  693. {
  694. struct prefix_ipv6 *p;
  695. p = prefix_ipv6_new ();
  696. p->family = AF_INET6;
  697. p->prefixlen = ip6_masklen (mask->sin6.sin6_addr);
  698. memcpy (&p->prefix, &dest->sin6.sin6_addr, sizeof (struct in6_addr));
  699. return (struct prefix *) p;
  700. }
  701. #endif /* HAVE_IPV6 */
  702. return NULL;
  703. }
  704. /* Utility function of convert between struct prefix <=> union sockunion. */
  705. struct prefix *
  706. sockunion2hostprefix (const union sockunion *su, struct prefix *prefix)
  707. {
  708. if (su->sa.sa_family == AF_INET)
  709. {
  710. struct prefix_ipv4 *p;
  711. p = prefix ? (struct prefix_ipv4 *) prefix : prefix_ipv4_new ();
  712. p->family = AF_INET;
  713. p->prefix = su->sin.sin_addr;
  714. p->prefixlen = IPV4_MAX_BITLEN;
  715. return (struct prefix *) p;
  716. }
  717. #ifdef HAVE_IPV6
  718. if (su->sa.sa_family == AF_INET6)
  719. {
  720. struct prefix_ipv6 *p;
  721. p = prefix ? (struct prefix_ipv6 *) prefix : prefix_ipv6_new ();
  722. p->family = AF_INET6;
  723. p->prefixlen = IPV6_MAX_BITLEN;
  724. memcpy (&p->prefix, &su->sin6.sin6_addr, sizeof (struct in6_addr));
  725. return (struct prefix *) p;
  726. }
  727. #endif /* HAVE_IPV6 */
  728. return NULL;
  729. }
  730. void
  731. prefix2sockunion (const struct prefix *p, union sockunion *su)
  732. {
  733. memset (su, 0, sizeof (*su));
  734. su->sa.sa_family = p->family;
  735. if (p->family == AF_INET)
  736. su->sin.sin_addr = p->u.prefix4;
  737. #ifdef HAVE_IPV6
  738. if (p->family == AF_INET6)
  739. memcpy (&su->sin6.sin6_addr, &p->u.prefix6, sizeof (struct in6_addr));
  740. #endif /* HAVE_IPV6 */
  741. }
  742. int
  743. prefix_blen (const struct prefix *p)
  744. {
  745. switch (p->family)
  746. {
  747. case AF_INET:
  748. return IPV4_MAX_BYTELEN;
  749. break;
  750. #ifdef HAVE_IPV6
  751. case AF_INET6:
  752. return IPV6_MAX_BYTELEN;
  753. break;
  754. #endif /* HAVE_IPV6 */
  755. case AF_ETHERNET:
  756. return ETHER_ADDR_LEN;
  757. }
  758. return 0;
  759. }
  760. /* Generic function for conversion string to struct prefix. */
  761. int
  762. str2prefix (const char *str, struct prefix *p)
  763. {
  764. int ret;
  765. /* First we try to convert string to struct prefix_ipv4. */
  766. ret = str2prefix_ipv4 (str, (struct prefix_ipv4 *) p);
  767. if (ret)
  768. return ret;
  769. #ifdef HAVE_IPV6
  770. /* Next we try to convert string to struct prefix_ipv6. */
  771. ret = str2prefix_ipv6 (str, (struct prefix_ipv6 *) p);
  772. if (ret)
  773. return ret;
  774. #endif /* HAVE_IPV6 */
  775. /* Next we try to convert string to struct prefix_eth. */
  776. ret = str2prefix_eth (str, (struct prefix_eth *) p);
  777. if (ret)
  778. return ret;
  779. return 0;
  780. }
  781. const char *
  782. prefix2str (union prefix46constptr pu, char *str, int size)
  783. {
  784. const struct prefix *p = pu.p;
  785. char buf[BUFSIZ];
  786. if (p->family == AF_ETHERNET) {
  787. int i;
  788. char *s = str;
  789. assert(size > (3*ETHER_ADDR_LEN) + 1 /* slash */ + 3 /* plen */ );
  790. for (i = 0; i < ETHER_ADDR_LEN; ++i) {
  791. sprintf(s, "%02x", p->u.prefix_eth.octet[i]);
  792. if (i < (ETHER_ADDR_LEN - 1)) {
  793. *(s+2) = ':';
  794. s += 3;
  795. } else {
  796. s += 2;
  797. }
  798. }
  799. sprintf(s, "/%d", p->prefixlen);
  800. return 0;
  801. }
  802. inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ);
  803. snprintf (str, size, "%s/%d", buf, p->prefixlen);
  804. return str;
  805. }
  806. struct prefix *
  807. prefix_new ()
  808. {
  809. struct prefix *p;
  810. p = XCALLOC (MTYPE_PREFIX, sizeof *p);
  811. return p;
  812. }
  813. /* Free prefix structure. */
  814. void
  815. prefix_free (struct prefix *p)
  816. {
  817. XFREE (MTYPE_PREFIX, p);
  818. }
  819. /* Utility function. Check the string only contains digit
  820. * character.
  821. * FIXME str.[c|h] would be better place for this function. */
  822. int
  823. all_digit (const char *str)
  824. {
  825. for (; *str != '\0'; str++)
  826. if (!isdigit ((int) *str))
  827. return 0;
  828. return 1;
  829. }
  830. /* Utility function to convert ipv4 prefixes to Classful prefixes */
  831. void apply_classful_mask_ipv4 (struct prefix_ipv4 *p)
  832. {
  833. u_int32_t destination;
  834. destination = ntohl (p->prefix.s_addr);
  835. if (p->prefixlen == IPV4_MAX_PREFIXLEN);
  836. /* do nothing for host routes */
  837. else if (IN_CLASSC (destination))
  838. {
  839. p->prefixlen=24;
  840. apply_mask_ipv4(p);
  841. }
  842. else if (IN_CLASSB(destination))
  843. {
  844. p->prefixlen=16;
  845. apply_mask_ipv4(p);
  846. }
  847. else
  848. {
  849. p->prefixlen=8;
  850. apply_mask_ipv4(p);
  851. }
  852. }
  853. in_addr_t
  854. ipv4_network_addr (in_addr_t hostaddr, int masklen)
  855. {
  856. struct in_addr mask;
  857. masklen2ip (masklen, &mask);
  858. return hostaddr & mask.s_addr;
  859. }
  860. in_addr_t
  861. ipv4_broadcast_addr (in_addr_t hostaddr, int masklen)
  862. {
  863. struct in_addr mask;
  864. masklen2ip (masklen, &mask);
  865. return (masklen != IPV4_MAX_PREFIXLEN-1) ?
  866. /* normal case */
  867. (hostaddr | ~mask.s_addr) :
  868. /* special case for /31 */
  869. (hostaddr ^ ~mask.s_addr);
  870. }
  871. /* Utility function to convert ipv4 netmask to prefixes
  872. ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
  873. ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
  874. int
  875. netmask_str2prefix_str (const char *net_str, const char *mask_str,
  876. char *prefix_str)
  877. {
  878. struct in_addr network;
  879. struct in_addr mask;
  880. u_char prefixlen;
  881. u_int32_t destination;
  882. int ret;
  883. ret = inet_aton (net_str, &network);
  884. if (! ret)
  885. return 0;
  886. if (mask_str)
  887. {
  888. ret = inet_aton (mask_str, &mask);
  889. if (! ret)
  890. return 0;
  891. prefixlen = ip_masklen (mask);
  892. }
  893. else
  894. {
  895. destination = ntohl (network.s_addr);
  896. if (network.s_addr == 0)
  897. prefixlen = 0;
  898. else if (IN_CLASSC (destination))
  899. prefixlen = 24;
  900. else if (IN_CLASSB (destination))
  901. prefixlen = 16;
  902. else if (IN_CLASSA (destination))
  903. prefixlen = 8;
  904. else
  905. return 0;
  906. }
  907. sprintf (prefix_str, "%s/%d", net_str, prefixlen);
  908. return 1;
  909. }
  910. #ifdef HAVE_IPV6
  911. /* Utility function for making IPv6 address string. */
  912. const char *
  913. inet6_ntoa (struct in6_addr addr)
  914. {
  915. static char buf[INET6_ADDRSTRLEN];
  916. inet_ntop (AF_INET6, &addr, buf, INET6_ADDRSTRLEN);
  917. return buf;
  918. }
  919. #endif /* HAVE_IPV6 */