]> git.proxmox.com Git - mirror_frr.git/blob - zebra/router-id.c
Merge pull request #532 from opensourcerouting/gpl-headers
[mirror_frr.git] / zebra / router-id.c
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 along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <zebra.h>
24
25 #include "if.h"
26 #include "vty.h"
27 #include "sockunion.h"
28 #include "prefix.h"
29 #include "stream.h"
30 #include "command.h"
31 #include "memory.h"
32 #include "zebra_memory.h"
33 #include "ioctl.h"
34 #include "connected.h"
35 #include "network.h"
36 #include "log.h"
37 #include "table.h"
38 #include "rib.h"
39 #include "vrf.h"
40
41 #include "zebra/zserv.h"
42 #include "zebra/zebra_vrf.h"
43 #include "zebra/router-id.h"
44 #include "zebra/redistribute.h"
45
46 /* master zebra server structure */
47 extern struct zebra_t zebrad;
48
49 static struct connected *
50 router_id_find_node (struct list *l, struct connected *ifc)
51 {
52 struct listnode *node;
53 struct connected *c;
54
55 for (ALL_LIST_ELEMENTS_RO (l, node, c))
56 if (prefix_same (ifc->address, c->address))
57 return c;
58
59 return NULL;
60 }
61
62 static int
63 router_id_bad_address (struct connected *ifc)
64 {
65 if (ifc->address->family != AF_INET)
66 return 1;
67
68 /* non-redistributable addresses shouldn't be used for RIDs either */
69 if (!zebra_check_addr (ifc->address))
70 return 1;
71
72 return 0;
73 }
74
75 void
76 router_id_get (struct prefix *p, vrf_id_t vrf_id)
77 {
78 struct listnode *node;
79 struct connected *c;
80 struct zebra_vrf *zvrf = vrf_info_get (vrf_id);
81
82 p->u.prefix4.s_addr = 0;
83 p->family = AF_INET;
84 p->prefixlen = 32;
85
86 if (zvrf->rid_user_assigned.u.prefix4.s_addr)
87 p->u.prefix4.s_addr = zvrf->rid_user_assigned.u.prefix4.s_addr;
88 else if (!list_isempty (zvrf->rid_lo_sorted_list))
89 {
90 node = listtail (zvrf->rid_lo_sorted_list);
91 c = listgetdata (node);
92 p->u.prefix4.s_addr = c->address->u.prefix4.s_addr;
93 }
94 else if (!list_isempty (zvrf->rid_all_sorted_list))
95 {
96 node = listtail (zvrf->rid_all_sorted_list);
97 c = listgetdata (node);
98 p->u.prefix4.s_addr = c->address->u.prefix4.s_addr;
99 }
100 }
101
102 static void
103 router_id_set (struct prefix *p, vrf_id_t vrf_id)
104 {
105 struct prefix p2;
106 struct listnode *node;
107 struct zserv *client;
108 struct zebra_vrf *zvrf;
109
110 if (p->u.prefix4.s_addr == 0) /* unset */
111 {
112 zvrf = vrf_info_lookup (vrf_id);
113 if (! zvrf)
114 return;
115 }
116 else /* set */
117 zvrf = vrf_info_get (vrf_id);
118
119 zvrf->rid_user_assigned.u.prefix4.s_addr = p->u.prefix4.s_addr;
120
121 router_id_get (&p2, vrf_id);
122
123 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
124 zsend_router_id_update (client, &p2, vrf_id);
125 }
126
127 void
128 router_id_add_address (struct connected *ifc)
129 {
130 struct list *l = NULL;
131 struct listnode *node;
132 struct prefix before;
133 struct prefix after;
134 struct zserv *client;
135 struct zebra_vrf *zvrf = vrf_info_get (ifc->ifp->vrf_id);
136
137 if (router_id_bad_address (ifc))
138 return;
139
140 router_id_get (&before, zvrf_id (zvrf));
141
142 if (!strncmp (ifc->ifp->name, "lo", 2)
143 || !strncmp (ifc->ifp->name, "dummy", 5))
144 l = zvrf->rid_lo_sorted_list;
145 else
146 l = zvrf->rid_all_sorted_list;
147
148 if (!router_id_find_node (l, ifc))
149 listnode_add_sort (l, ifc);
150
151 router_id_get (&after, zvrf_id (zvrf));
152
153 if (prefix_same (&before, &after))
154 return;
155
156 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
157 zsend_router_id_update (client, &after, zvrf_id (zvrf));
158 }
159
160 void
161 router_id_del_address (struct connected *ifc)
162 {
163 struct connected *c;
164 struct list *l;
165 struct prefix after;
166 struct prefix before;
167 struct listnode *node;
168 struct zserv *client;
169 struct zebra_vrf *zvrf = vrf_info_get (ifc->ifp->vrf_id);
170
171 if (router_id_bad_address (ifc))
172 return;
173
174 router_id_get (&before, zvrf_id (zvrf));
175
176 if (!strncmp (ifc->ifp->name, "lo", 2)
177 || !strncmp (ifc->ifp->name, "dummy", 5))
178 l = zvrf->rid_lo_sorted_list;
179 else
180 l = zvrf->rid_all_sorted_list;
181
182 if ((c = router_id_find_node (l, ifc)))
183 listnode_delete (l, c);
184
185 router_id_get (&after, zvrf_id (zvrf));
186
187 if (prefix_same (&before, &after))
188 return;
189
190 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
191 zsend_router_id_update (client, &after, zvrf_id (zvrf));
192 }
193
194 void
195 router_id_write (struct vty *vty)
196 {
197 struct vrf *vrf;
198 struct zebra_vrf *zvrf;
199
200 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
201 if ((zvrf = vrf->info) != NULL)
202 if (zvrf->rid_user_assigned.u.prefix4.s_addr)
203 {
204 if (zvrf_id (zvrf) == VRF_DEFAULT)
205 vty_out (vty, "router-id %s%s",
206 inet_ntoa (zvrf->rid_user_assigned.u.prefix4),
207 VTY_NEWLINE);
208 else
209 vty_out (vty, "router-id %s vrf %s%s",
210 inet_ntoa (zvrf->rid_user_assigned.u.prefix4),
211 zvrf_name (zvrf),
212 VTY_NEWLINE);
213 }
214 }
215
216 DEFUN (router_id,
217 router_id_cmd,
218 "router-id A.B.C.D [vrf NAME]",
219 "Manually set the router-id\n"
220 "IP address to use for router-id\n"
221 VRF_CMD_HELP_STR)
222 {
223 int idx_ipv4 = 1;
224 int idx_name = 3;
225
226 struct prefix rid;
227 vrf_id_t vrf_id = VRF_DEFAULT;
228
229 rid.u.prefix4.s_addr = inet_addr (argv[idx_ipv4]->arg);
230 if (!rid.u.prefix4.s_addr)
231 return CMD_WARNING;
232
233 rid.prefixlen = 32;
234 rid.family = AF_INET;
235
236 if (argc > 2)
237 VRF_GET_ID (vrf_id, argv[idx_name]->arg);
238
239 router_id_set (&rid, vrf_id);
240
241 return CMD_SUCCESS;
242 }
243
244 DEFUN (no_router_id,
245 no_router_id_cmd,
246 "no router-id [A.B.C.D [vrf NAME]]",
247 NO_STR
248 "Remove the manually configured router-id\n"
249 "IP address to use for router-id\n"
250 VRF_CMD_HELP_STR)
251 {
252 int idx_name = 4;
253
254 struct prefix rid;
255 vrf_id_t vrf_id = VRF_DEFAULT;
256
257 rid.u.prefix4.s_addr = 0;
258 rid.prefixlen = 0;
259 rid.family = AF_INET;
260
261 if (argc > 3)
262 VRF_GET_ID (vrf_id, argv[idx_name]->arg);
263
264 router_id_set (&rid, vrf_id);
265
266 return CMD_SUCCESS;
267 }
268
269
270
271 static int
272 router_id_cmp (void *a, void *b)
273 {
274 const struct connected *ifa = (const struct connected *)a;
275 const struct connected *ifb = (const struct connected *)b;
276
277 return IPV4_ADDR_CMP(&ifa->address->u.prefix4.s_addr,&ifb->address->u.prefix4.s_addr);
278 }
279
280 void
281 router_id_cmd_init (void)
282 {
283 install_element (CONFIG_NODE, &router_id_cmd);
284 install_element (CONFIG_NODE, &no_router_id_cmd);
285 }
286
287 void
288 router_id_init (struct zebra_vrf *zvrf)
289 {
290 zvrf->rid_all_sorted_list = &zvrf->_rid_all_sorted_list;
291 zvrf->rid_lo_sorted_list = &zvrf->_rid_lo_sorted_list;
292
293 memset (zvrf->rid_all_sorted_list, 0, sizeof (zvrf->_rid_all_sorted_list));
294 memset (zvrf->rid_lo_sorted_list, 0, sizeof (zvrf->_rid_lo_sorted_list));
295 memset (&zvrf->rid_user_assigned, 0, sizeof (zvrf->rid_user_assigned));
296
297 zvrf->rid_all_sorted_list->cmp = router_id_cmp;
298 zvrf->rid_lo_sorted_list->cmp = router_id_cmp;
299
300 zvrf->rid_user_assigned.family = AF_INET;
301 zvrf->rid_user_assigned.prefixlen = 32;
302 }