router-id.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. /* 2nd pointer type used primarily to quell a warning on
  41. * ALL_LIST_ELEMENTS_RO
  42. */
  43. static struct list _rid_all_sorted_list;
  44. static struct list _rid_lo_sorted_list;
  45. static struct list *rid_all_sorted_list = &_rid_all_sorted_list;
  46. static struct list *rid_lo_sorted_list = &_rid_lo_sorted_list;
  47. static struct prefix rid_user_assigned;
  48. /* master zebra server structure */
  49. extern struct zebra_t zebrad;
  50. static struct connected *
  51. router_id_find_node (struct list *l, struct connected *ifc)
  52. {
  53. struct listnode *node;
  54. struct connected *c;
  55. for (ALL_LIST_ELEMENTS_RO (l, node, c))
  56. if (prefix_same (ifc->address, c->address))
  57. return c;
  58. return NULL;
  59. }
  60. static int
  61. router_id_bad_address (struct connected *ifc)
  62. {
  63. if (ifc->address->family != AF_INET)
  64. return 1;
  65. /* non-redistributable addresses shouldn't be used for RIDs either */
  66. if (!zebra_check_addr (ifc->address))
  67. return 1;
  68. return 0;
  69. }
  70. void
  71. router_id_get (struct prefix *p)
  72. {
  73. struct listnode *node;
  74. struct connected *c;
  75. p->u.prefix4.s_addr = 0;
  76. p->family = AF_INET;
  77. p->prefixlen = 32;
  78. if (rid_user_assigned.u.prefix4.s_addr)
  79. p->u.prefix4.s_addr = rid_user_assigned.u.prefix4.s_addr;
  80. else if (!list_isempty (rid_lo_sorted_list))
  81. {
  82. node = listtail (rid_lo_sorted_list);
  83. c = listgetdata (node);
  84. p->u.prefix4.s_addr = c->address->u.prefix4.s_addr;
  85. }
  86. else if (!list_isempty (rid_all_sorted_list))
  87. {
  88. node = listtail (rid_all_sorted_list);
  89. c = listgetdata (node);
  90. p->u.prefix4.s_addr = c->address->u.prefix4.s_addr;
  91. }
  92. }
  93. static void
  94. router_id_set (struct prefix *p)
  95. {
  96. struct prefix p2;
  97. struct listnode *node;
  98. struct zserv *client;
  99. rid_user_assigned.u.prefix4.s_addr = p->u.prefix4.s_addr;
  100. router_id_get (&p2);
  101. for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
  102. zsend_router_id_update (client, &p2);
  103. }
  104. void
  105. router_id_add_address (struct connected *ifc)
  106. {
  107. struct list *l = NULL;
  108. struct listnode *node;
  109. struct prefix before;
  110. struct prefix after;
  111. struct zserv *client;
  112. if (router_id_bad_address (ifc))
  113. return;
  114. router_id_get (&before);
  115. if (!strncmp (ifc->ifp->name, "lo", 2)
  116. || !strncmp (ifc->ifp->name, "dummy", 5))
  117. l = rid_lo_sorted_list;
  118. else
  119. l = rid_all_sorted_list;
  120. if (!router_id_find_node (l, ifc))
  121. listnode_add_sort (l, ifc);
  122. router_id_get (&after);
  123. if (prefix_same (&before, &after))
  124. return;
  125. for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
  126. zsend_router_id_update (client, &after);
  127. }
  128. void
  129. router_id_del_address (struct connected *ifc)
  130. {
  131. struct connected *c;
  132. struct list *l;
  133. struct prefix after;
  134. struct prefix before;
  135. struct listnode *node;
  136. struct zserv *client;
  137. if (router_id_bad_address (ifc))
  138. return;
  139. router_id_get (&before);
  140. if (!strncmp (ifc->ifp->name, "lo", 2)
  141. || !strncmp (ifc->ifp->name, "dummy", 5))
  142. l = rid_lo_sorted_list;
  143. else
  144. l = rid_all_sorted_list;
  145. if ((c = router_id_find_node (l, ifc)))
  146. listnode_delete (l, c);
  147. router_id_get (&after);
  148. if (prefix_same (&before, &after))
  149. return;
  150. for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
  151. zsend_router_id_update (client, &after);
  152. }
  153. void
  154. router_id_write (struct vty *vty)
  155. {
  156. if (rid_user_assigned.u.prefix4.s_addr)
  157. vty_out (vty, "router-id %s%s", inet_ntoa (rid_user_assigned.u.prefix4),
  158. VTY_NEWLINE);
  159. }
  160. DEFUN (router_id,
  161. router_id_cmd,
  162. "router-id A.B.C.D",
  163. "Manually set the router-id\n"
  164. "IP address to use for router-id\n")
  165. {
  166. struct prefix rid;
  167. rid.u.prefix4.s_addr = inet_addr (argv[0]);
  168. if (!rid.u.prefix4.s_addr)
  169. return CMD_WARNING;
  170. rid.prefixlen = 32;
  171. rid.family = AF_INET;
  172. router_id_set (&rid);
  173. return CMD_SUCCESS;
  174. }
  175. DEFUN (no_router_id,
  176. no_router_id_cmd,
  177. "no router-id",
  178. NO_STR
  179. "Remove the manually configured router-id\n")
  180. {
  181. struct prefix rid;
  182. rid.u.prefix4.s_addr = 0;
  183. rid.prefixlen = 0;
  184. rid.family = AF_INET;
  185. router_id_set (&rid);
  186. return CMD_SUCCESS;
  187. }
  188. static int
  189. router_id_cmp (void *a, void *b)
  190. {
  191. const struct connected *ifa = (const struct connected *)a;
  192. const struct connected *ifb = (const struct connected *)b;
  193. return IPV4_ADDR_CMP(&ifa->address->u.prefix4.s_addr,&ifb->address->u.prefix4.s_addr);
  194. }
  195. void
  196. router_id_init (void)
  197. {
  198. install_element (CONFIG_NODE, &router_id_cmd);
  199. install_element (CONFIG_NODE, &no_router_id_cmd);
  200. memset (rid_all_sorted_list, 0, sizeof (rid_all_sorted_list));
  201. memset (rid_lo_sorted_list, 0, sizeof (rid_lo_sorted_list));
  202. memset (&rid_user_assigned, 0, sizeof (rid_user_assigned));
  203. rid_all_sorted_list->cmp = router_id_cmp;
  204. rid_lo_sorted_list->cmp = router_id_cmp;
  205. rid_user_assigned.family = AF_INET;
  206. rid_user_assigned.prefixlen = 32;
  207. }