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