router-id.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Router ID for zebra daemon.
  3. *
  4. * Copyright (C) 2004 James R. Leu
  5. *
  6. * This file is part of Quagga routing suite.
  7. *
  8. * Quagga is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2, or (at your option) any
  11. * later version.
  12. *
  13. * Quagga is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  20. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  21. * 02111-1307, USA.
  22. */
  23. #include <zebra.h>
  24. #include "if.h"
  25. #include "vty.h"
  26. #include "sockunion.h"
  27. #include "prefix.h"
  28. #include "stream.h"
  29. #include "command.h"
  30. #include "memory.h"
  31. #include "ioctl.h"
  32. #include "connected.h"
  33. #include "network.h"
  34. #include "log.h"
  35. #include "table.h"
  36. #include "rib.h"
  37. #include "zebra/zserv.h"
  38. #include "zebra/router-id.h"
  39. #include "zebra/redistribute.h"
  40. static struct list rid_all_sorted_list;
  41. static struct list rid_lo_sorted_list;
  42. static struct prefix rid_user_assigned;
  43. /* master zebra server structure */
  44. extern struct zebra_t zebrad;
  45. static struct connected *
  46. router_id_find_node (struct list *l, struct connected *ifc)
  47. {
  48. struct listnode *node;
  49. struct connected *c;
  50. for (ALL_LIST_ELEMENTS_RO (l, node, c))
  51. if (prefix_same (ifc->address, c->address))
  52. return c;
  53. return NULL;
  54. }
  55. static int
  56. router_id_bad_address (struct connected *ifc)
  57. {
  58. if (ifc->address->family != AF_INET)
  59. return 1;
  60. /* non-redistributable addresses shouldn't be used for RIDs either */
  61. if (!zebra_check_addr (ifc->address))
  62. return 1;
  63. return 0;
  64. }
  65. void
  66. router_id_get (struct prefix *p)
  67. {
  68. struct listnode *node;
  69. struct connected *c;
  70. p->u.prefix4.s_addr = 0;
  71. p->family = AF_INET;
  72. p->prefixlen = 32;
  73. if (rid_user_assigned.u.prefix4.s_addr)
  74. p->u.prefix4.s_addr = rid_user_assigned.u.prefix4.s_addr;
  75. else if (!list_isempty (&rid_lo_sorted_list))
  76. {
  77. node = listtail (&rid_lo_sorted_list);
  78. c = listgetdata (node);
  79. p->u.prefix4.s_addr = c->address->u.prefix4.s_addr;
  80. }
  81. else if (!list_isempty (&rid_all_sorted_list))
  82. {
  83. node = listtail (&rid_all_sorted_list);
  84. c = listgetdata (node);
  85. p->u.prefix4.s_addr = c->address->u.prefix4.s_addr;
  86. }
  87. }
  88. static void
  89. router_id_set (struct prefix *p)
  90. {
  91. struct prefix p2;
  92. struct listnode *node;
  93. struct zserv *client;
  94. rid_user_assigned.u.prefix4.s_addr = p->u.prefix4.s_addr;
  95. router_id_get (&p2);
  96. for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
  97. zsend_router_id_update (client, &p2);
  98. }
  99. void
  100. router_id_add_address (struct connected *ifc)
  101. {
  102. struct list *l = NULL;
  103. struct listnode *node;
  104. struct prefix before;
  105. struct prefix after;
  106. struct zserv *client;
  107. if (router_id_bad_address (ifc))
  108. return;
  109. router_id_get (&before);
  110. if (!strncmp (ifc->ifp->name, "lo", 2)
  111. || !strncmp (ifc->ifp->name, "dummy", 5))
  112. l = &rid_lo_sorted_list;
  113. else
  114. l = &rid_all_sorted_list;
  115. if (!router_id_find_node (l, ifc))
  116. listnode_add_sort (l, ifc);
  117. router_id_get (&after);
  118. if (prefix_same (&before, &after))
  119. return;
  120. for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
  121. zsend_router_id_update (client, &after);
  122. }
  123. void
  124. router_id_del_address (struct connected *ifc)
  125. {
  126. struct connected *c;
  127. struct list *l;
  128. struct prefix after;
  129. struct prefix before;
  130. struct listnode *node;
  131. struct zserv *client;
  132. if (router_id_bad_address (ifc))
  133. return;
  134. router_id_get (&before);
  135. if (!strncmp (ifc->ifp->name, "lo", 2)
  136. || !strncmp (ifc->ifp->name, "dummy", 5))
  137. l = &rid_lo_sorted_list;
  138. else
  139. l = &rid_all_sorted_list;
  140. if ((c = router_id_find_node (l, ifc)))
  141. listnode_delete (l, c);
  142. router_id_get (&after);
  143. if (prefix_same (&before, &after))
  144. return;
  145. for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
  146. zsend_router_id_update (client, &after);
  147. }
  148. void
  149. router_id_write (struct vty *vty)
  150. {
  151. if (rid_user_assigned.u.prefix4.s_addr)
  152. vty_out (vty, "router-id %s%s", inet_ntoa (rid_user_assigned.u.prefix4),
  153. VTY_NEWLINE);
  154. }
  155. DEFUN (router_id,
  156. router_id_cmd,
  157. "router-id A.B.C.D",
  158. "Manually set the router-id\n"
  159. "IP address to use for router-id\n")
  160. {
  161. struct prefix rid;
  162. rid.u.prefix4.s_addr = inet_addr (argv[0]);
  163. if (!rid.u.prefix4.s_addr)
  164. return CMD_WARNING;
  165. rid.prefixlen = 32;
  166. rid.family = AF_INET;
  167. router_id_set (&rid);
  168. return CMD_SUCCESS;
  169. }
  170. DEFUN (no_router_id,
  171. no_router_id_cmd,
  172. "no router-id",
  173. NO_STR
  174. "Remove the manually configured router-id\n")
  175. {
  176. struct prefix rid;
  177. rid.u.prefix4.s_addr = 0;
  178. rid.prefixlen = 0;
  179. rid.family = AF_INET;
  180. router_id_set (&rid);
  181. return CMD_SUCCESS;
  182. }
  183. static int
  184. router_id_cmp (void *a, void *b)
  185. {
  186. const struct connected *ifa = (const struct connected *)a;
  187. const struct connected *ifb = (const struct connected *)b;
  188. unsigned int A = ntohl(ifa->address->u.prefix4.s_addr);
  189. unsigned int B = ntohl(ifb->address->u.prefix4.s_addr);
  190. return (int) (A - B);
  191. }
  192. void
  193. router_id_init (void)
  194. {
  195. install_element (CONFIG_NODE, &router_id_cmd);
  196. install_element (CONFIG_NODE, &no_router_id_cmd);
  197. memset (&rid_all_sorted_list, 0, sizeof (rid_all_sorted_list));
  198. memset (&rid_lo_sorted_list, 0, sizeof (rid_lo_sorted_list));
  199. memset (&rid_user_assigned, 0, sizeof (rid_user_assigned));
  200. rid_all_sorted_list.cmp = router_id_cmp;
  201. rid_lo_sorted_list.cmp = router_id_cmp;
  202. rid_user_assigned.family = AF_INET;
  203. rid_user_assigned.prefixlen = 32;
  204. }