]>
git.proxmox.com Git - mirror_frr.git/blob - zebra/router-id.c
2 * Router ID for zebra daemon.
4 * Copyright (C) 2004 James R. Leu
6 * This file is part of Quagga routing suite.
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
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.
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
28 #include "sockunion.h"
34 #include "connected.h"
41 #include "zebra/zserv.h"
42 #include "zebra/zebra_vrf.h"
43 #include "zebra/router-id.h"
44 #include "zebra/redistribute.h"
46 static struct connected
*
47 router_id_find_node (struct list
*l
, struct connected
*ifc
)
49 struct listnode
*node
;
52 for (ALL_LIST_ELEMENTS_RO (l
, node
, c
))
53 if (prefix_same (ifc
->address
, c
->address
))
60 router_id_bad_address (struct connected
*ifc
)
62 if (ifc
->address
->family
!= AF_INET
)
65 /* non-redistributable addresses shouldn't be used for RIDs either */
66 if (!zebra_check_addr (ifc
->address
))
73 router_id_get (struct prefix
*p
, vrf_id_t vrf_id
)
75 struct listnode
*node
;
77 struct zebra_vrf
*zvrf
= vrf_info_get (vrf_id
);
79 p
->u
.prefix4
.s_addr
= 0;
83 if (zvrf
->rid_user_assigned
.u
.prefix4
.s_addr
)
84 p
->u
.prefix4
.s_addr
= zvrf
->rid_user_assigned
.u
.prefix4
.s_addr
;
85 else if (!list_isempty (zvrf
->rid_lo_sorted_list
))
87 node
= listtail (zvrf
->rid_lo_sorted_list
);
88 c
= listgetdata (node
);
89 p
->u
.prefix4
.s_addr
= c
->address
->u
.prefix4
.s_addr
;
91 else if (!list_isempty (zvrf
->rid_all_sorted_list
))
93 node
= listtail (zvrf
->rid_all_sorted_list
);
94 c
= listgetdata (node
);
95 p
->u
.prefix4
.s_addr
= c
->address
->u
.prefix4
.s_addr
;
100 router_id_set (struct prefix
*p
, vrf_id_t vrf_id
)
103 struct listnode
*node
;
104 struct zserv
*client
;
105 struct zebra_vrf
*zvrf
;
107 if (p
->u
.prefix4
.s_addr
== 0) /* unset */
109 zvrf
= vrf_info_lookup (vrf_id
);
114 zvrf
= vrf_info_get (vrf_id
);
116 zvrf
->rid_user_assigned
.u
.prefix4
.s_addr
= p
->u
.prefix4
.s_addr
;
118 router_id_get (&p2
, vrf_id
);
120 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
121 zsend_router_id_update (client
, &p2
, vrf_id
);
125 router_id_add_address (struct connected
*ifc
)
127 struct list
*l
= NULL
;
128 struct listnode
*node
;
129 struct prefix before
;
131 struct zserv
*client
;
132 struct zebra_vrf
*zvrf
= vrf_info_get (ifc
->ifp
->vrf_id
);
134 if (router_id_bad_address (ifc
))
137 router_id_get (&before
, zvrf
->vrf_id
);
139 if (!strncmp (ifc
->ifp
->name
, "lo", 2)
140 || !strncmp (ifc
->ifp
->name
, "dummy", 5))
141 l
= zvrf
->rid_lo_sorted_list
;
143 l
= zvrf
->rid_all_sorted_list
;
145 if (!router_id_find_node (l
, ifc
))
146 listnode_add_sort (l
, ifc
);
148 router_id_get (&after
, zvrf
->vrf_id
);
150 if (prefix_same (&before
, &after
))
153 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
154 zsend_router_id_update (client
, &after
, zvrf
->vrf_id
);
158 router_id_del_address (struct connected
*ifc
)
163 struct prefix before
;
164 struct listnode
*node
;
165 struct zserv
*client
;
166 struct zebra_vrf
*zvrf
= vrf_info_get (ifc
->ifp
->vrf_id
);
168 if (router_id_bad_address (ifc
))
171 router_id_get (&before
, zvrf
->vrf_id
);
173 if (!strncmp (ifc
->ifp
->name
, "lo", 2)
174 || !strncmp (ifc
->ifp
->name
, "dummy", 5))
175 l
= zvrf
->rid_lo_sorted_list
;
177 l
= zvrf
->rid_all_sorted_list
;
179 if ((c
= router_id_find_node (l
, ifc
)))
180 listnode_delete (l
, c
);
182 router_id_get (&after
, zvrf
->vrf_id
);
184 if (prefix_same (&before
, &after
))
187 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
188 zsend_router_id_update (client
, &after
, zvrf
->vrf_id
);
192 router_id_write (struct vty
*vty
)
194 struct zebra_vrf
*zvrf
;
197 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
198 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
199 if (zvrf
->rid_user_assigned
.u
.prefix4
.s_addr
)
201 if (zvrf
->vrf_id
== VRF_DEFAULT
)
202 vty_out (vty
, "router-id %s%s",
203 inet_ntoa (zvrf
->rid_user_assigned
.u
.prefix4
),
206 vty_out (vty
, "router-id %s vrf %s%s",
207 inet_ntoa (zvrf
->rid_user_assigned
.u
.prefix4
),
216 "Manually set the router-id\n"
217 "IP address to use for router-id\n")
220 vrf_id_t vrf_id
= VRF_DEFAULT
;
222 rid
.u
.prefix4
.s_addr
= inet_addr (argv
[0]);
223 if (!rid
.u
.prefix4
.s_addr
)
227 rid
.family
= AF_INET
;
230 VRF_GET_ID (vrf_id
, argv
[1]);
232 router_id_set (&rid
, vrf_id
);
239 "router-id A.B.C.D " VRF_CMD_STR
,
240 "Manually set the router-id\n"
241 "IP address to use for router-id\n"
248 "Remove the manually configured router-id\n")
251 vrf_id_t vrf_id
= VRF_DEFAULT
;
253 rid
.u
.prefix4
.s_addr
= 0;
255 rid
.family
= AF_INET
;
258 VRF_GET_ID (vrf_id
, argv
[1]);
260 router_id_set (&rid
, vrf_id
);
266 no_router_id_val_cmd
,
267 "no router-id A.B.C.D",
269 "Remove the manually configured router-id\n"
270 "IP address to use for router-id\n")
273 no_router_id_vrf_cmd
,
274 "no router-id A.B.C.D " VRF_CMD_STR
,
276 "Remove the manually configured router-id\n"
277 "IP address to use for router-id\n"
281 router_id_cmp (void *a
, void *b
)
283 const struct connected
*ifa
= (const struct connected
*)a
;
284 const struct connected
*ifb
= (const struct connected
*)b
;
286 return IPV4_ADDR_CMP(&ifa
->address
->u
.prefix4
.s_addr
,&ifb
->address
->u
.prefix4
.s_addr
);
290 router_id_cmd_init (void)
292 install_element (CONFIG_NODE
, &router_id_cmd
);
293 install_element (CONFIG_NODE
, &no_router_id_cmd
);
294 install_element (CONFIG_NODE
, &router_id_vrf_cmd
);
295 install_element (CONFIG_NODE
, &no_router_id_val_cmd
);
296 install_element (CONFIG_NODE
, &no_router_id_vrf_cmd
);
300 router_id_init (struct zebra_vrf
*zvrf
)
302 zvrf
->rid_all_sorted_list
= &zvrf
->_rid_all_sorted_list
;
303 zvrf
->rid_lo_sorted_list
= &zvrf
->_rid_lo_sorted_list
;
305 memset (zvrf
->rid_all_sorted_list
, 0, sizeof (zvrf
->_rid_all_sorted_list
));
306 memset (zvrf
->rid_lo_sorted_list
, 0, sizeof (zvrf
->_rid_lo_sorted_list
));
307 memset (&zvrf
->rid_user_assigned
, 0, sizeof (zvrf
->rid_user_assigned
));
309 zvrf
->rid_all_sorted_list
->cmp
= router_id_cmp
;
310 zvrf
->rid_lo_sorted_list
->cmp
= router_id_cmp
;
312 zvrf
->rid_user_assigned
.family
= AF_INET
;
313 zvrf
->rid_user_assigned
.prefixlen
= 32;