prefix.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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
  188. return -1;
  189. }
  190. /* Address Famiy Identifier to Address Family converter. */
  191. int
  192. afi2family (afi_t afi)
  193. {
  194. if (afi == AFI_IP)
  195. return AF_INET;
  196. #ifdef HAVE_IPV6
  197. else if (afi == AFI_IP6)
  198. return AF_INET6;
  199. #endif /* HAVE_IPV6 */
  200. return 0;
  201. }
  202. afi_t
  203. family2afi (int family)
  204. {
  205. if (family == AF_INET)
  206. return AFI_IP;
  207. #ifdef HAVE_IPV6
  208. else if (family == AF_INET6)
  209. return AFI_IP6;
  210. #endif /* HAVE_IPV6 */
  211. return 0;
  212. }
  213. const char *
  214. safi2str(safi_t safi)
  215. {
  216. switch (safi) {
  217. case SAFI_UNICAST:
  218. return "unicast";
  219. case SAFI_MULTICAST:
  220. return "multicast";
  221. case SAFI_ENCAP:
  222. return "encap";
  223. case SAFI_MPLS_VPN:
  224. return "vpn";
  225. }
  226. return NULL;
  227. }
  228. /* If n includes p prefix then return 1 else return 0. */
  229. int
  230. prefix_match (const struct prefix *n, const struct prefix *p)
  231. {
  232. int offset;
  233. int shift;
  234. const u_char *np, *pp;
  235. /* If n's prefix is longer than p's one return 0. */
  236. if (n->prefixlen > p->prefixlen)
  237. return 0;
  238. /* Set both prefix's head pointer. */
  239. np = (const u_char *)&n->u.prefix;
  240. pp = (const u_char *)&p->u.prefix;
  241. offset = n->prefixlen / PNBBY;
  242. shift = n->prefixlen % PNBBY;
  243. if (shift)
  244. if (maskbit[shift] & (np[offset] ^ pp[offset]))
  245. return 0;
  246. while (offset--)
  247. if (np[offset] != pp[offset])
  248. return 0;
  249. return 1;
  250. }
  251. /* Copy prefix from src to dest. */
  252. void
  253. prefix_copy (struct prefix *dest, const struct prefix *src)
  254. {
  255. dest->family = src->family;
  256. dest->prefixlen = src->prefixlen;
  257. if (src->family == AF_INET)
  258. dest->u.prefix4 = src->u.prefix4;
  259. #ifdef HAVE_IPV6
  260. else if (src->family == AF_INET6)
  261. dest->u.prefix6 = src->u.prefix6;
  262. #endif /* HAVE_IPV6 */
  263. else if (src->family == AF_UNSPEC)
  264. {
  265. dest->u.lp.id = src->u.lp.id;
  266. dest->u.lp.adv_router = src->u.lp.adv_router;
  267. }
  268. else
  269. {
  270. zlog (NULL, LOG_ERR, "prefix_copy(): Unknown address family %d",
  271. src->family);
  272. assert (0);
  273. }
  274. }
  275. /*
  276. * Return 1 if the address/netmask contained in the prefix structure
  277. * is the same, and else return 0. For this routine, 'same' requires
  278. * that not only the prefix length and the network part be the same,
  279. * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
  280. * the same. Note that this routine has the same return value sense
  281. * as '==' (which is different from prefix_cmp).
  282. */
  283. int
  284. prefix_same (const struct prefix *p1, const struct prefix *p2)
  285. {
  286. if (p1->family == p2->family && p1->prefixlen == p2->prefixlen)
  287. {
  288. if (p1->family == AF_INET)
  289. if (IPV4_ADDR_SAME (&p1->u.prefix4.s_addr, &p2->u.prefix4.s_addr))
  290. return 1;
  291. #ifdef HAVE_IPV6
  292. if (p1->family == AF_INET6 )
  293. if (IPV6_ADDR_SAME (&p1->u.prefix6.s6_addr, &p2->u.prefix6.s6_addr))
  294. return 1;
  295. #endif /* HAVE_IPV6 */
  296. }
  297. return 0;
  298. }
  299. /*
  300. * Return 0 if the network prefixes represented by the struct prefix
  301. * arguments are the same prefix, and 1 otherwise. Network prefixes
  302. * are considered the same if the prefix lengths are equal and the
  303. * network parts are the same. Host bits (which are considered masked
  304. * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
  305. * 10.0.0.2/8 are considered equivalent by this routine. Note that
  306. * this routine has the same return sense as strcmp (which is different
  307. * from prefix_same).
  308. */
  309. int
  310. prefix_cmp (const struct prefix *p1, const struct prefix *p2)
  311. {
  312. int offset;
  313. int shift;
  314. /* Set both prefix's head pointer. */
  315. const u_char *pp1 = (const u_char *)&p1->u.prefix;
  316. const u_char *pp2 = (const u_char *)&p2->u.prefix;
  317. if (p1->family != p2->family || p1->prefixlen != p2->prefixlen)
  318. return 1;
  319. offset = p1->prefixlen / PNBBY;
  320. shift = p1->prefixlen % PNBBY;
  321. if (shift)
  322. if (maskbit[shift] & (pp1[offset] ^ pp2[offset]))
  323. return 1;
  324. while (offset--)
  325. if (pp1[offset] != pp2[offset])
  326. return 1;
  327. return 0;
  328. }
  329. /*
  330. * Count the number of common bits in 2 prefixes. The prefix length is
  331. * ignored for this function; the whole prefix is compared. If the prefix
  332. * address families don't match, return -1; otherwise the return value is
  333. * in range 0 ... maximum prefix length for the address family.
  334. */
  335. int
  336. prefix_common_bits (const struct prefix *p1, const struct prefix *p2)
  337. {
  338. int pos, bit;
  339. int length = 0;
  340. u_char xor;
  341. /* Set both prefix's head pointer. */
  342. const u_char *pp1 = (const u_char *)&p1->u.prefix;
  343. const u_char *pp2 = (const u_char *)&p2->u.prefix;
  344. if (p1->family == AF_INET)
  345. length = IPV4_MAX_BYTELEN;
  346. #ifdef HAVE_IPV6
  347. if (p1->family == AF_INET6)
  348. length = IPV6_MAX_BYTELEN;
  349. #endif
  350. if (p1->family != p2->family || !length)
  351. return -1;
  352. for (pos = 0; pos < length; pos++)
  353. if (pp1[pos] != pp2[pos])
  354. break;
  355. if (pos == length)
  356. return pos * 8;
  357. xor = pp1[pos] ^ pp2[pos];
  358. for (bit = 0; bit < 8; bit++)
  359. if (xor & (1 << (7 - bit)))
  360. break;
  361. return pos * 8 + bit;
  362. }
  363. /* Return prefix family type string. */
  364. const char *
  365. prefix_family_str (const struct prefix *p)
  366. {
  367. if (p->family == AF_INET)
  368. return "inet";
  369. #ifdef HAVE_IPV6
  370. if (p->family == AF_INET6)
  371. return "inet6";
  372. #endif /* HAVE_IPV6 */
  373. return "unspec";
  374. }
  375. /* Allocate new prefix_ipv4 structure. */
  376. struct prefix_ipv4 *
  377. prefix_ipv4_new ()
  378. {
  379. struct prefix_ipv4 *p;
  380. /* Call prefix_new to allocate a full-size struct prefix to avoid problems
  381. where the struct prefix_ipv4 is cast to struct prefix and unallocated
  382. bytes were being referenced (e.g. in structure assignments). */
  383. p = (struct prefix_ipv4 *)prefix_new();
  384. p->family = AF_INET;
  385. return p;
  386. }
  387. /* Free prefix_ipv4 structure. */
  388. void
  389. prefix_ipv4_free (struct prefix_ipv4 *p)
  390. {
  391. prefix_free((struct prefix *)p);
  392. }
  393. /* When string format is invalid return 0. */
  394. int
  395. str2prefix_ipv4 (const char *str, struct prefix_ipv4 *p)
  396. {
  397. int ret;
  398. int plen;
  399. char *pnt;
  400. char *cp;
  401. /* Find slash inside string. */
  402. pnt = strchr (str, '/');
  403. /* String doesn't contail slash. */
  404. if (pnt == NULL)
  405. {
  406. /* Convert string to prefix. */
  407. ret = inet_aton (str, &p->prefix);
  408. if (ret == 0)
  409. return 0;
  410. /* If address doesn't contain slash we assume it host address. */
  411. p->family = AF_INET;
  412. p->prefixlen = IPV4_MAX_BITLEN;
  413. return ret;
  414. }
  415. else
  416. {
  417. cp = XMALLOC (MTYPE_TMP, (pnt - str) + 1);
  418. strncpy (cp, str, pnt - str);
  419. *(cp + (pnt - str)) = '\0';
  420. ret = inet_aton (cp, &p->prefix);
  421. XFREE (MTYPE_TMP, cp);
  422. /* Get prefix length. */
  423. plen = (u_char) atoi (++pnt);
  424. if (plen > IPV4_MAX_PREFIXLEN)
  425. return 0;
  426. p->family = AF_INET;
  427. p->prefixlen = plen;
  428. }
  429. return ret;
  430. }
  431. /* Convert masklen into IP address's netmask (network byte order). */
  432. void
  433. masklen2ip (const int masklen, struct in_addr *netmask)
  434. {
  435. assert (masklen >= 0 && masklen <= IPV4_MAX_BITLEN);
  436. /* left shift is only defined for less than the size of the type.
  437. * we unconditionally use long long in case the target platform
  438. * has defined behaviour for << 32 (or has a 64-bit left shift) */
  439. if (sizeof(unsigned long long) > 4)
  440. netmask->s_addr = htonl(0xffffffffULL << (32 - masklen));
  441. else
  442. netmask->s_addr = htonl(masklen ? 0xffffffffU << (32 - masklen) : 0);
  443. }
  444. /* Convert IP address's netmask into integer. We assume netmask is
  445. sequential one. Argument netmask should be network byte order. */
  446. u_char
  447. ip_masklen (struct in_addr netmask)
  448. {
  449. uint32_t tmp = ~ntohl(netmask.s_addr);
  450. if (tmp)
  451. /* clz: count leading zeroes. sadly, the behaviour of this builtin
  452. * is undefined for a 0 argument, even though most CPUs give 32 */
  453. return __builtin_clz(tmp);
  454. else
  455. return 32;
  456. }
  457. /* Apply mask to IPv4 prefix (network byte order). */
  458. void
  459. apply_mask_ipv4 (struct prefix_ipv4 *p)
  460. {
  461. struct in_addr mask;
  462. masklen2ip(p->prefixlen, &mask);
  463. p->prefix.s_addr &= mask.s_addr;
  464. }
  465. /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
  466. int
  467. prefix_ipv4_any (const struct prefix_ipv4 *p)
  468. {
  469. return (p->prefix.s_addr == 0 && p->prefixlen == 0);
  470. }
  471. #ifdef HAVE_IPV6
  472. /* Allocate a new ip version 6 route */
  473. struct prefix_ipv6 *
  474. prefix_ipv6_new (void)
  475. {
  476. struct prefix_ipv6 *p;
  477. /* Allocate a full-size struct prefix to avoid problems with structure
  478. size mismatches. */
  479. p = (struct prefix_ipv6 *)prefix_new();
  480. p->family = AF_INET6;
  481. return p;
  482. }
  483. /* Free prefix for IPv6. */
  484. void
  485. prefix_ipv6_free (struct prefix_ipv6 *p)
  486. {
  487. prefix_free((struct prefix *)p);
  488. }
  489. /* If given string is valid return pin6 else return NULL */
  490. int
  491. str2prefix_ipv6 (const char *str, struct prefix_ipv6 *p)
  492. {
  493. char *pnt;
  494. char *cp;
  495. int ret;
  496. pnt = strchr (str, '/');
  497. /* If string doesn't contain `/' treat it as host route. */
  498. if (pnt == NULL)
  499. {
  500. ret = inet_pton (AF_INET6, str, &p->prefix);
  501. if (ret == 0)
  502. return 0;
  503. p->prefixlen = IPV6_MAX_BITLEN;
  504. }
  505. else
  506. {
  507. int plen;
  508. cp = XMALLOC (MTYPE_TMP, (pnt - str) + 1);
  509. strncpy (cp, str, pnt - str);
  510. *(cp + (pnt - str)) = '\0';
  511. ret = inet_pton (AF_INET6, cp, &p->prefix);
  512. free (cp);
  513. if (ret == 0)
  514. return 0;
  515. plen = (u_char) atoi (++pnt);
  516. if (plen > IPV6_MAX_BITLEN)
  517. return 0;
  518. p->prefixlen = plen;
  519. }
  520. p->family = AF_INET6;
  521. return ret;
  522. }
  523. /* Convert struct in6_addr netmask into integer.
  524. * FIXME return u_char as ip_maskleni() does. */
  525. int
  526. ip6_masklen (struct in6_addr netmask)
  527. {
  528. int len = 0;
  529. unsigned char val;
  530. unsigned char *pnt;
  531. pnt = (unsigned char *) & netmask;
  532. while ((*pnt == 0xff) && len < IPV6_MAX_BITLEN)
  533. {
  534. len += 8;
  535. pnt++;
  536. }
  537. if (len < IPV6_MAX_BITLEN)
  538. {
  539. val = *pnt;
  540. while (val)
  541. {
  542. len++;
  543. val <<= 1;
  544. }
  545. }
  546. return len;
  547. }
  548. void
  549. masklen2ip6 (const int masklen, struct in6_addr *netmask)
  550. {
  551. assert (masklen >= 0 && masklen <= IPV6_MAX_BITLEN);
  552. memcpy (netmask, maskbytes6 + masklen, sizeof (struct in6_addr));
  553. }
  554. void
  555. apply_mask_ipv6 (struct prefix_ipv6 *p)
  556. {
  557. u_char *pnt;
  558. int index;
  559. int offset;
  560. index = p->prefixlen / 8;
  561. if (index < 16)
  562. {
  563. pnt = (u_char *) &p->prefix;
  564. offset = p->prefixlen % 8;
  565. pnt[index] &= maskbit[offset];
  566. index++;
  567. while (index < 16)
  568. pnt[index++] = 0;
  569. }
  570. }
  571. void
  572. str2in6_addr (const char *str, struct in6_addr *addr)
  573. {
  574. int i;
  575. unsigned int x;
  576. /* %x must point to unsinged int */
  577. for (i = 0; i < 16; i++)
  578. {
  579. sscanf (str + (i * 2), "%02x", &x);
  580. addr->s6_addr[i] = x & 0xff;
  581. }
  582. }
  583. #endif /* HAVE_IPV6 */
  584. void
  585. apply_mask (struct prefix *p)
  586. {
  587. switch (p->family)
  588. {
  589. case AF_INET:
  590. apply_mask_ipv4 ((struct prefix_ipv4 *)p);
  591. break;
  592. #ifdef HAVE_IPV6
  593. case AF_INET6:
  594. apply_mask_ipv6 ((struct prefix_ipv6 *)p);
  595. break;
  596. #endif /* HAVE_IPV6 */
  597. default:
  598. break;
  599. }
  600. return;
  601. }
  602. /* Utility function of convert between struct prefix <=> union sockunion.
  603. * FIXME This function isn't used anywhere. */
  604. struct prefix *
  605. sockunion2prefix (const union sockunion *dest,
  606. const union sockunion *mask)
  607. {
  608. if (dest->sa.sa_family == AF_INET)
  609. {
  610. struct prefix_ipv4 *p;
  611. p = prefix_ipv4_new ();
  612. p->family = AF_INET;
  613. p->prefix = dest->sin.sin_addr;
  614. p->prefixlen = ip_masklen (mask->sin.sin_addr);
  615. return (struct prefix *) p;
  616. }
  617. #ifdef HAVE_IPV6
  618. if (dest->sa.sa_family == AF_INET6)
  619. {
  620. struct prefix_ipv6 *p;
  621. p = prefix_ipv6_new ();
  622. p->family = AF_INET6;
  623. p->prefixlen = ip6_masklen (mask->sin6.sin6_addr);
  624. memcpy (&p->prefix, &dest->sin6.sin6_addr, sizeof (struct in6_addr));
  625. return (struct prefix *) p;
  626. }
  627. #endif /* HAVE_IPV6 */
  628. return NULL;
  629. }
  630. /* Utility function of convert between struct prefix <=> union sockunion. */
  631. struct prefix *
  632. sockunion2hostprefix (const union sockunion *su, struct prefix *prefix)
  633. {
  634. if (su->sa.sa_family == AF_INET)
  635. {
  636. struct prefix_ipv4 *p;
  637. p = prefix ? (struct prefix_ipv4 *) prefix : prefix_ipv4_new ();
  638. p->family = AF_INET;
  639. p->prefix = su->sin.sin_addr;
  640. p->prefixlen = IPV4_MAX_BITLEN;
  641. return (struct prefix *) p;
  642. }
  643. #ifdef HAVE_IPV6
  644. if (su->sa.sa_family == AF_INET6)
  645. {
  646. struct prefix_ipv6 *p;
  647. p = prefix ? (struct prefix_ipv6 *) prefix : prefix_ipv6_new ();
  648. p->family = AF_INET6;
  649. p->prefixlen = IPV6_MAX_BITLEN;
  650. memcpy (&p->prefix, &su->sin6.sin6_addr, sizeof (struct in6_addr));
  651. return (struct prefix *) p;
  652. }
  653. #endif /* HAVE_IPV6 */
  654. return NULL;
  655. }
  656. void
  657. prefix2sockunion (const struct prefix *p, union sockunion *su)
  658. {
  659. memset (su, 0, sizeof (*su));
  660. su->sa.sa_family = p->family;
  661. if (p->family == AF_INET)
  662. su->sin.sin_addr = p->u.prefix4;
  663. #ifdef HAVE_IPV6
  664. if (p->family == AF_INET6)
  665. memcpy (&su->sin6.sin6_addr, &p->u.prefix6, sizeof (struct in6_addr));
  666. #endif /* HAVE_IPV6 */
  667. }
  668. int
  669. prefix_blen (const struct prefix *p)
  670. {
  671. switch (p->family)
  672. {
  673. case AF_INET:
  674. return IPV4_MAX_BYTELEN;
  675. break;
  676. #ifdef HAVE_IPV6
  677. case AF_INET6:
  678. return IPV6_MAX_BYTELEN;
  679. break;
  680. #endif /* HAVE_IPV6 */
  681. }
  682. return 0;
  683. }
  684. /* Generic function for conversion string to struct prefix. */
  685. int
  686. str2prefix (const char *str, struct prefix *p)
  687. {
  688. int ret;
  689. /* First we try to convert string to struct prefix_ipv4. */
  690. ret = str2prefix_ipv4 (str, (struct prefix_ipv4 *) p);
  691. if (ret)
  692. return ret;
  693. #ifdef HAVE_IPV6
  694. /* Next we try to convert string to struct prefix_ipv6. */
  695. ret = str2prefix_ipv6 (str, (struct prefix_ipv6 *) p);
  696. if (ret)
  697. return ret;
  698. #endif /* HAVE_IPV6 */
  699. return 0;
  700. }
  701. const char *
  702. prefix2str (union prefix46constptr pu, char *str, int size)
  703. {
  704. const struct prefix *p = pu.p;
  705. char buf[BUFSIZ];
  706. inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ);
  707. snprintf (str, size, "%s/%d", buf, p->prefixlen);
  708. return str;
  709. }
  710. struct prefix *
  711. prefix_new ()
  712. {
  713. struct prefix *p;
  714. p = XCALLOC (MTYPE_PREFIX, sizeof *p);
  715. return p;
  716. }
  717. /* Free prefix structure. */
  718. void
  719. prefix_free (struct prefix *p)
  720. {
  721. XFREE (MTYPE_PREFIX, p);
  722. }
  723. /* Utility function. Check the string only contains digit
  724. * character.
  725. * FIXME str.[c|h] would be better place for this function. */
  726. int
  727. all_digit (const char *str)
  728. {
  729. for (; *str != '\0'; str++)
  730. if (!isdigit ((int) *str))
  731. return 0;
  732. return 1;
  733. }
  734. /* Utility function to convert ipv4 prefixes to Classful prefixes */
  735. void apply_classful_mask_ipv4 (struct prefix_ipv4 *p)
  736. {
  737. u_int32_t destination;
  738. destination = ntohl (p->prefix.s_addr);
  739. if (p->prefixlen == IPV4_MAX_PREFIXLEN);
  740. /* do nothing for host routes */
  741. else if (IN_CLASSC (destination))
  742. {
  743. p->prefixlen=24;
  744. apply_mask_ipv4(p);
  745. }
  746. else if (IN_CLASSB(destination))
  747. {
  748. p->prefixlen=16;
  749. apply_mask_ipv4(p);
  750. }
  751. else
  752. {
  753. p->prefixlen=8;
  754. apply_mask_ipv4(p);
  755. }
  756. }
  757. in_addr_t
  758. ipv4_network_addr (in_addr_t hostaddr, int masklen)
  759. {
  760. struct in_addr mask;
  761. masklen2ip (masklen, &mask);
  762. return hostaddr & mask.s_addr;
  763. }
  764. in_addr_t
  765. ipv4_broadcast_addr (in_addr_t hostaddr, int masklen)
  766. {
  767. struct in_addr mask;
  768. masklen2ip (masklen, &mask);
  769. return (masklen != IPV4_MAX_PREFIXLEN-1) ?
  770. /* normal case */
  771. (hostaddr | ~mask.s_addr) :
  772. /* special case for /31 */
  773. (hostaddr ^ ~mask.s_addr);
  774. }
  775. /* Utility function to convert ipv4 netmask to prefixes
  776. ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
  777. ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
  778. int
  779. netmask_str2prefix_str (const char *net_str, const char *mask_str,
  780. char *prefix_str)
  781. {
  782. struct in_addr network;
  783. struct in_addr mask;
  784. u_char prefixlen;
  785. u_int32_t destination;
  786. int ret;
  787. ret = inet_aton (net_str, &network);
  788. if (! ret)
  789. return 0;
  790. if (mask_str)
  791. {
  792. ret = inet_aton (mask_str, &mask);
  793. if (! ret)
  794. return 0;
  795. prefixlen = ip_masklen (mask);
  796. }
  797. else
  798. {
  799. destination = ntohl (network.s_addr);
  800. if (network.s_addr == 0)
  801. prefixlen = 0;
  802. else if (IN_CLASSC (destination))
  803. prefixlen = 24;
  804. else if (IN_CLASSB (destination))
  805. prefixlen = 16;
  806. else if (IN_CLASSA (destination))
  807. prefixlen = 8;
  808. else
  809. return 0;
  810. }
  811. sprintf (prefix_str, "%s/%d", net_str, prefixlen);
  812. return 1;
  813. }
  814. #ifdef HAVE_IPV6
  815. /* Utility function for making IPv6 address string. */
  816. const char *
  817. inet6_ntoa (struct in6_addr addr)
  818. {
  819. static char buf[INET6_ADDRSTRLEN];
  820. inet_ntop (AF_INET6, &addr, buf, INET6_ADDRSTRLEN);
  821. return buf;
  822. }
  823. #endif /* HAVE_IPV6 */