]>
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"
40 #include "zebra/zserv.h"
42 static struct list rid_all_sorted_list
;
43 static struct list rid_lo_sorted_list
;
44 static struct prefix rid_user_assigned
;
46 /* master zebra server structure */
47 extern struct zebra_t zebrad
;
49 static struct connected
*
50 router_id_find_node (struct list
*l
, struct connected
*ifc
)
52 struct listnode
*node
;
55 for (node
= l
->head
; node
; node
= node
->next
)
57 c
= (struct connected
*) getdata (node
);
58 if (prefix_same (ifc
->address
, c
->address
))
65 router_id_bad_address (struct connected
*ifc
)
69 if (ifc
->address
->family
!= AF_INET
)
72 n
.u
.prefix4
.s_addr
= htonl (INADDR_LOOPBACK
);
76 if (prefix_match (&n
, ifc
->address
))
83 router_id_get (struct prefix
*p
)
85 struct listnode
*node
;
88 p
->u
.prefix4
.s_addr
= 0;
92 if (rid_user_assigned
.u
.prefix4
.s_addr
)
93 p
->u
.prefix4
.s_addr
= rid_user_assigned
.u
.prefix4
.s_addr
;
94 else if (!list_isempty (&rid_lo_sorted_list
))
96 node
= listtail (&rid_lo_sorted_list
);
98 p
->u
.prefix4
.s_addr
= c
->address
->u
.prefix4
.s_addr
;
100 else if (!list_isempty (&rid_all_sorted_list
))
102 node
= listtail (&rid_all_sorted_list
);
104 p
->u
.prefix4
.s_addr
= c
->address
->u
.prefix4
.s_addr
;
109 router_id_set (struct prefix
*p
)
112 struct listnode
*node
;
113 struct zserv
*client
;
115 rid_user_assigned
.u
.prefix4
.s_addr
= p
->u
.prefix4
.s_addr
;
118 for (node
= listhead (zebrad
.client_list
); node
; nextnode (node
))
119 if ((client
= getdata (node
)) != NULL
)
120 zsend_router_id_update (client
, &p2
);
124 router_id_add_address (struct connected
*ifc
)
126 struct list
*l
= NULL
;
127 struct listnode
*node
;
128 struct prefix before
;
130 struct zserv
*client
;
132 if (router_id_bad_address (ifc
))
135 router_id_get (&before
);
137 if (!strncmp (ifc
->ifp
->name
, "lo", 2)
138 || !strncmp (ifc
->ifp
->name
, "dummy", 5))
139 l
= &rid_lo_sorted_list
;
141 l
= &rid_all_sorted_list
;
143 if (!router_id_find_node (l
, ifc
))
144 listnode_add (l
, ifc
);
146 router_id_get (&after
);
148 if (prefix_same (&before
, &after
))
151 for (node
= listhead (zebrad
.client_list
); node
; nextnode (node
))
152 if ((client
= getdata (node
)) != NULL
)
153 zsend_router_id_update (client
, &after
);
157 router_id_del_address (struct connected
*ifc
)
162 struct prefix before
;
163 struct listnode
*node
;
164 struct zserv
*client
;
166 if (router_id_bad_address (ifc
))
169 router_id_get (&before
);
171 if (!strncmp (ifc
->ifp
->name
, "lo", 2)
172 || !strncmp (ifc
->ifp
->name
, "dummy", 5))
173 l
= &rid_lo_sorted_list
;
175 l
= &rid_all_sorted_list
;
177 if ((c
= router_id_find_node (l
, ifc
)))
178 listnode_delete (l
, c
);
180 router_id_get (&after
);
182 if (prefix_same (&before
, &after
))
185 for (node
= listhead (zebrad
.client_list
); node
; nextnode (node
))
186 if ((client
= getdata (node
)) != NULL
)
187 zsend_router_id_update (client
, &after
);
191 router_id_write (struct vty
*vty
)
193 if (rid_user_assigned
.u
.prefix4
.s_addr
)
194 vty_out (vty
, "router-id %s%s", inet_ntoa (rid_user_assigned
.u
.prefix4
),
201 "Manually set the router-id\n"
202 "IP address to use for router-id\n")
206 rid
.u
.prefix4
.s_addr
= inet_addr (argv
[0]);
207 if (!rid
.u
.prefix4
.s_addr
)
211 rid
.family
= AF_INET
;
213 router_id_set (&rid
);
222 "Remove the manually configured router-id\n")
226 rid
.u
.prefix4
.s_addr
= 0;
228 rid
.family
= AF_INET
;
230 router_id_set (&rid
);
236 router_id_cmp (void *a
, void *b
)
240 A
= ((struct connected
*) a
)->address
->u
.prefix4
.s_addr
;
241 B
= ((struct connected
*) b
)->address
->u
.prefix4
.s_addr
;
251 router_id_init (void)
253 install_element (CONFIG_NODE
, &router_id_cmd
);
254 install_element (CONFIG_NODE
, &no_router_id_cmd
);
256 memset (&rid_all_sorted_list
, 0, sizeof (rid_all_sorted_list
));
257 memset (&rid_lo_sorted_list
, 0, sizeof (rid_lo_sorted_list
));
258 memset (&rid_user_assigned
, 0, sizeof (rid_user_assigned
));
260 rid_all_sorted_list
.cmp
= router_id_cmp
;
261 rid_lo_sorted_list
.cmp
= router_id_cmp
;
263 rid_user_assigned
.family
= AF_INET
;
264 rid_user_assigned
.prefixlen
= 32;