]>
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"
33 #include "zebra_memory.h"
35 #include "connected.h"
42 #include "zebra/zserv.h"
43 #include "zebra/zebra_vrf.h"
44 #include "zebra/router-id.h"
45 #include "zebra/redistribute.h"
47 /* master zebra server structure */
48 extern struct zebra_t zebrad
;
50 static struct connected
*
51 router_id_find_node (struct list
*l
, struct connected
*ifc
)
53 struct listnode
*node
;
56 for (ALL_LIST_ELEMENTS_RO (l
, node
, c
))
57 if (prefix_same (ifc
->address
, c
->address
))
64 router_id_bad_address (struct connected
*ifc
)
66 if (ifc
->address
->family
!= AF_INET
)
69 /* non-redistributable addresses shouldn't be used for RIDs either */
70 if (!zebra_check_addr (ifc
->address
))
77 router_id_get (struct prefix
*p
, vrf_id_t vrf_id
)
79 struct listnode
*node
;
81 struct zebra_vrf
*zvrf
= vrf_info_get (vrf_id
);
83 p
->u
.prefix4
.s_addr
= 0;
87 if (zvrf
->rid_user_assigned
.u
.prefix4
.s_addr
)
88 p
->u
.prefix4
.s_addr
= zvrf
->rid_user_assigned
.u
.prefix4
.s_addr
;
89 else if (!list_isempty (zvrf
->rid_lo_sorted_list
))
91 node
= listtail (zvrf
->rid_lo_sorted_list
);
92 c
= listgetdata (node
);
93 p
->u
.prefix4
.s_addr
= c
->address
->u
.prefix4
.s_addr
;
95 else if (!list_isempty (zvrf
->rid_all_sorted_list
))
97 node
= listtail (zvrf
->rid_all_sorted_list
);
98 c
= listgetdata (node
);
99 p
->u
.prefix4
.s_addr
= c
->address
->u
.prefix4
.s_addr
;
104 router_id_set (struct prefix
*p
, vrf_id_t vrf_id
)
107 struct listnode
*node
;
108 struct zserv
*client
;
109 struct zebra_vrf
*zvrf
;
111 if (p
->u
.prefix4
.s_addr
== 0) /* unset */
113 zvrf
= vrf_info_lookup (vrf_id
);
118 zvrf
= vrf_info_get (vrf_id
);
120 zvrf
->rid_user_assigned
.u
.prefix4
.s_addr
= p
->u
.prefix4
.s_addr
;
122 router_id_get (&p2
, vrf_id
);
124 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
125 zsend_router_id_update (client
, &p2
, vrf_id
);
129 router_id_add_address (struct connected
*ifc
)
131 struct list
*l
= NULL
;
132 struct listnode
*node
;
133 struct prefix before
;
135 struct zserv
*client
;
136 struct zebra_vrf
*zvrf
= vrf_info_get (ifc
->ifp
->vrf_id
);
138 if (router_id_bad_address (ifc
))
141 router_id_get (&before
, zvrf
->vrf_id
);
143 if (!strncmp (ifc
->ifp
->name
, "lo", 2)
144 || !strncmp (ifc
->ifp
->name
, "dummy", 5))
145 l
= zvrf
->rid_lo_sorted_list
;
147 l
= zvrf
->rid_all_sorted_list
;
149 if (!router_id_find_node (l
, ifc
))
150 listnode_add_sort (l
, ifc
);
152 router_id_get (&after
, zvrf
->vrf_id
);
154 if (prefix_same (&before
, &after
))
157 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
158 zsend_router_id_update (client
, &after
, zvrf
->vrf_id
);
162 router_id_del_address (struct connected
*ifc
)
167 struct prefix before
;
168 struct listnode
*node
;
169 struct zserv
*client
;
170 struct zebra_vrf
*zvrf
= vrf_info_get (ifc
->ifp
->vrf_id
);
172 if (router_id_bad_address (ifc
))
175 router_id_get (&before
, zvrf
->vrf_id
);
177 if (!strncmp (ifc
->ifp
->name
, "lo", 2)
178 || !strncmp (ifc
->ifp
->name
, "dummy", 5))
179 l
= zvrf
->rid_lo_sorted_list
;
181 l
= zvrf
->rid_all_sorted_list
;
183 if ((c
= router_id_find_node (l
, ifc
)))
184 listnode_delete (l
, c
);
186 router_id_get (&after
, zvrf
->vrf_id
);
188 if (prefix_same (&before
, &after
))
191 for (ALL_LIST_ELEMENTS_RO (zebrad
.client_list
, node
, client
))
192 zsend_router_id_update (client
, &after
, zvrf
->vrf_id
);
196 router_id_write (struct vty
*vty
)
198 struct zebra_vrf
*zvrf
;
201 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
202 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
203 if (zvrf
->rid_user_assigned
.u
.prefix4
.s_addr
)
205 if (zvrf
->vrf_id
== VRF_DEFAULT
)
206 vty_out (vty
, "router-id %s%s",
207 inet_ntoa (zvrf
->rid_user_assigned
.u
.prefix4
),
210 vty_out (vty
, "router-id %s vrf %s%s",
211 inet_ntoa (zvrf
->rid_user_assigned
.u
.prefix4
),
220 "Manually set the router-id\n"
221 "IP address to use for router-id\n")
224 vrf_id_t vrf_id
= VRF_DEFAULT
;
226 rid
.u
.prefix4
.s_addr
= inet_addr (argv
[1]->arg
);
227 if (!rid
.u
.prefix4
.s_addr
)
231 rid
.family
= AF_INET
;
234 VRF_GET_ID (vrf_id
, argv
[1]);
236 router_id_set (&rid
, vrf_id
);
243 "router-id A.B.C.D " VRF_CMD_STR
,
244 "Manually set the router-id\n"
245 "IP address to use for router-id\n"
252 "Remove the manually configured router-id\n")
255 vrf_id_t vrf_id
= VRF_DEFAULT
;
257 rid
.u
.prefix4
.s_addr
= 0;
259 rid
.family
= AF_INET
;
262 VRF_GET_ID (vrf_id
, argv
[1]);
264 router_id_set (&rid
, vrf_id
);
270 no_router_id_val_cmd
,
271 "no router-id A.B.C.D",
273 "Remove the manually configured router-id\n"
274 "IP address to use for router-id\n")
277 no_router_id_vrf_cmd
,
278 "no router-id A.B.C.D " VRF_CMD_STR
,
280 "Remove the manually configured router-id\n"
281 "IP address to use for router-id\n"
285 router_id_cmp (void *a
, void *b
)
287 const struct connected
*ifa
= (const struct connected
*)a
;
288 const struct connected
*ifb
= (const struct connected
*)b
;
290 return IPV4_ADDR_CMP(&ifa
->address
->u
.prefix4
.s_addr
,&ifb
->address
->u
.prefix4
.s_addr
);
294 router_id_cmd_init (void)
296 install_element (CONFIG_NODE
, &router_id_cmd
);
297 install_element (CONFIG_NODE
, &no_router_id_cmd
);
298 install_element (CONFIG_NODE
, &router_id_vrf_cmd
);
299 install_element (CONFIG_NODE
, &no_router_id_val_cmd
);
300 install_element (CONFIG_NODE
, &no_router_id_vrf_cmd
);
304 router_id_init (struct zebra_vrf
*zvrf
)
306 zvrf
->rid_all_sorted_list
= &zvrf
->_rid_all_sorted_list
;
307 zvrf
->rid_lo_sorted_list
= &zvrf
->_rid_lo_sorted_list
;
309 memset (zvrf
->rid_all_sorted_list
, 0, sizeof (zvrf
->_rid_all_sorted_list
));
310 memset (zvrf
->rid_lo_sorted_list
, 0, sizeof (zvrf
->_rid_lo_sorted_list
));
311 memset (&zvrf
->rid_user_assigned
, 0, sizeof (zvrf
->rid_user_assigned
));
313 zvrf
->rid_all_sorted_list
->cmp
= router_id_cmp
;
314 zvrf
->rid_lo_sorted_list
->cmp
= router_id_cmp
;
316 zvrf
->rid_user_assigned
.family
= AF_INET
;
317 zvrf
->rid_user_assigned
.prefixlen
= 32;