]> git.proxmox.com Git - mirror_frr.git/blob - zebra/irdp_main.c
Merge pull request #1882 from LabNConsulting/working/master/community-dismiss
[mirror_frr.git] / zebra / irdp_main.c
1 /*
2 *
3 * Copyright (C) 2000 Robert Olsson.
4 * Swedish University of Agricultural Sciences
5 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra 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 * GNU Zebra 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 /*
24 * This work includes work with the following copywrite:
25 *
26 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
27 *
28 */
29
30 /*
31 * Thanks to Jens Låås at Swedish University of Agricultural Sciences
32 * for reviewing and tests.
33 */
34
35
36 #include <zebra.h>
37
38 #include "if.h"
39 #include "vty.h"
40 #include "sockunion.h"
41 #include "sockopt.h"
42 #include "prefix.h"
43 #include "command.h"
44 #include "memory.h"
45 #include "zebra_memory.h"
46 #include "stream.h"
47 #include "ioctl.h"
48 #include "connected.h"
49 #include "log.h"
50 #include "zclient.h"
51 #include "thread.h"
52 #include "privs.h"
53 #include "libfrr.h"
54 #include "version.h"
55 #include "zebra/interface.h"
56 #include "zebra/rtadv.h"
57 #include "zebra/rib.h"
58 #include "zebra/zserv.h"
59 #include "zebra/redistribute.h"
60 #include "zebra/irdp.h"
61 #include <netinet/ip_icmp.h>
62
63 #include "checksum.h"
64 #include "if.h"
65 #include "sockunion.h"
66 #include "log.h"
67
68 /* GLOBAL VARS */
69
70 extern struct zebra_privs_t zserv_privs;
71
72 struct thread *t_irdp_raw;
73
74 /* Timer interval of irdp. */
75 int irdp_timer_interval = IRDP_DEFAULT_INTERVAL;
76
77 int irdp_sock_init(void)
78 {
79 int ret, i;
80 int save_errno;
81 int sock;
82
83 if (zserv_privs.change(ZPRIVS_RAISE))
84 zlog_err("irdp_sock_init: could not raise privs, %s",
85 safe_strerror(errno));
86
87 sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
88 save_errno = errno;
89
90 if (zserv_privs.change(ZPRIVS_LOWER))
91 zlog_err("irdp_sock_init: could not lower privs, %s",
92 safe_strerror(errno));
93
94 if (sock < 0) {
95 zlog_warn("IRDP: can't create irdp socket %s",
96 safe_strerror(save_errno));
97 return sock;
98 };
99
100 i = 1;
101 ret = setsockopt(sock, IPPROTO_IP, IP_TTL, (void *)&i, sizeof(i));
102 if (ret < 0) {
103 zlog_warn("IRDP: can't do irdp sockopt %s",
104 safe_strerror(errno));
105 close(sock);
106 return ret;
107 };
108
109 ret = setsockopt_ifindex(AF_INET, sock, 1);
110 if (ret < 0) {
111 zlog_warn("IRDP: can't do irdp sockopt %s",
112 safe_strerror(errno));
113 close(sock);
114 return ret;
115 };
116
117 t_irdp_raw = NULL;
118 thread_add_read(zebrad.master, irdp_read_raw, NULL, sock, &t_irdp_raw);
119
120 return sock;
121 }
122
123
124 static int get_pref(struct irdp_interface *irdp, struct prefix *p)
125 {
126 struct listnode *node;
127 struct Adv *adv;
128
129 /* Use default preference or use the override pref */
130
131 if (irdp->AdvPrefList == NULL)
132 return irdp->Preference;
133
134 for (ALL_LIST_ELEMENTS_RO(irdp->AdvPrefList, node, adv))
135 if (p->u.prefix4.s_addr == adv->ip.s_addr)
136 return adv->pref;
137
138 return irdp->Preference;
139 }
140
141 /* Make ICMP Router Advertisement Message. */
142 static int make_advertisement_packet(struct interface *ifp, struct prefix *p,
143 struct stream *s)
144 {
145 struct zebra_if *zi = ifp->info;
146 struct irdp_interface *irdp = zi->irdp;
147 int size;
148 int pref;
149 uint16_t checksum;
150
151 pref = get_pref(irdp, p);
152
153 stream_putc(s, ICMP_ROUTERADVERT); /* Type. */
154 stream_putc(s, 0); /* Code. */
155 stream_putw(s, 0); /* Checksum. */
156 stream_putc(s, 1); /* Num address. */
157 stream_putc(s, 2); /* Address Entry Size. */
158
159 if (irdp->flags & IF_SHUTDOWN)
160 stream_putw(s, 0);
161 else
162 stream_putw(s, irdp->Lifetime);
163
164 stream_putl(s, htonl(p->u.prefix4.s_addr)); /* Router address. */
165 stream_putl(s, pref);
166
167 /* in_cksum return network byte order value */
168 size = 16;
169 checksum = in_cksum(s->data, size);
170 stream_putw_at(s, 2, htons(checksum));
171
172 return size;
173 }
174
175 static void irdp_send(struct interface *ifp, struct prefix *p, struct stream *s)
176 {
177 struct zebra_if *zi = ifp->info;
178 struct irdp_interface *irdp = zi->irdp;
179 char buf[PREFIX_STRLEN];
180 uint32_t dst;
181 uint32_t ttl = 1;
182
183 if (!irdp)
184 return;
185 if (!(ifp->flags & IFF_UP))
186 return;
187
188 if (irdp->flags & IF_BROADCAST)
189 dst = INADDR_BROADCAST;
190 else
191 dst = htonl(INADDR_ALLHOSTS_GROUP);
192
193 if (irdp->flags & IF_DEBUG_MESSAGES)
194 zlog_debug("IRDP: TX Advert on %s %s Holdtime=%d Preference=%d",
195 ifp->name, prefix2str(p, buf, sizeof buf),
196 irdp->flags & IF_SHUTDOWN ? 0 : irdp->Lifetime,
197 get_pref(irdp, p));
198
199 send_packet(ifp, s, dst, p, ttl);
200 }
201
202 static void irdp_advertisement(struct interface *ifp, struct prefix *p)
203 {
204 struct stream *s;
205 s = stream_new(128);
206 make_advertisement_packet(ifp, p, s);
207 irdp_send(ifp, p, s);
208 stream_free(s);
209 }
210
211 int irdp_send_thread(struct thread *t_advert)
212 {
213 uint32_t timer, tmp;
214 struct interface *ifp = THREAD_ARG(t_advert);
215 struct zebra_if *zi = ifp->info;
216 struct irdp_interface *irdp = zi->irdp;
217 struct prefix *p;
218 struct listnode *node, *nnode;
219 struct connected *ifc;
220
221 if (!irdp)
222 return 0;
223
224 irdp->flags &= ~IF_SOLICIT;
225
226 if (ifp->connected)
227 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, ifc)) {
228 p = ifc->address;
229
230 if (p->family != AF_INET)
231 continue;
232
233 irdp_advertisement(ifp, p);
234 irdp->irdp_sent++;
235 }
236
237 tmp = irdp->MaxAdvertInterval - irdp->MinAdvertInterval;
238 timer = random() % (tmp + 1);
239 timer = irdp->MinAdvertInterval + timer;
240
241 if (irdp->irdp_sent < MAX_INITIAL_ADVERTISEMENTS
242 && timer > MAX_INITIAL_ADVERT_INTERVAL)
243 timer = MAX_INITIAL_ADVERT_INTERVAL;
244
245 if (irdp->flags & IF_DEBUG_MISC)
246 zlog_debug("IRDP: New timer for %s set to %u\n", ifp->name,
247 timer);
248
249 irdp->t_advertise = NULL;
250 thread_add_timer(zebrad.master, irdp_send_thread, ifp, timer,
251 &irdp->t_advertise);
252 return 0;
253 }
254
255 void irdp_advert_off(struct interface *ifp)
256 {
257 struct zebra_if *zi = ifp->info;
258 struct irdp_interface *irdp = zi->irdp;
259 struct listnode *node, *nnode;
260 int i;
261 struct connected *ifc;
262 struct prefix *p;
263
264 if (!irdp)
265 return;
266
267 if (irdp->t_advertise)
268 thread_cancel(irdp->t_advertise);
269 irdp->t_advertise = NULL;
270
271 if (ifp->connected)
272 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, ifc)) {
273 p = ifc->address;
274
275 /* Output some packets with Lifetime 0
276 we should add a wait...
277 */
278
279 for (i = 0; i < IRDP_LAST_ADVERT_MESSAGES; i++) {
280 irdp->irdp_sent++;
281 irdp_advertisement(ifp, p);
282 }
283 }
284 }
285
286
287 void process_solicit(struct interface *ifp)
288 {
289 struct zebra_if *zi = ifp->info;
290 struct irdp_interface *irdp = zi->irdp;
291 uint32_t timer;
292
293 if (!irdp)
294 return;
295
296 /* When SOLICIT is active we reject further incoming solicits
297 this keeps down the answering rate so we don't have think
298 about DoS attacks here. */
299
300 if (irdp->flags & IF_SOLICIT)
301 return;
302
303 irdp->flags |= IF_SOLICIT;
304 if (irdp->t_advertise)
305 thread_cancel(irdp->t_advertise);
306 irdp->t_advertise = NULL;
307
308 timer = (random() % MAX_RESPONSE_DELAY) + 1;
309
310 irdp->t_advertise = NULL;
311 thread_add_timer(zebrad.master, irdp_send_thread, ifp, timer,
312 &irdp->t_advertise);
313 }
314
315 static int irdp_finish(void)
316 {
317 struct vrf *vrf;
318 struct interface *ifp;
319 struct zebra_if *zi;
320 struct irdp_interface *irdp;
321
322 zlog_info("IRDP: Received shutdown notification.");
323
324 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
325 FOR_ALL_INTERFACES (vrf, ifp) {
326 zi = ifp->info;
327
328 if (!zi)
329 continue;
330 irdp = zi->irdp;
331 if (!irdp)
332 continue;
333
334 if (irdp->flags & IF_ACTIVE) {
335 irdp->flags |= IF_SHUTDOWN;
336 irdp_advert_off(ifp);
337 }
338 }
339 return 0;
340 }
341
342 static int irdp_init(struct thread_master *master)
343 {
344 irdp_if_init();
345
346 hook_register(frr_early_fini, irdp_finish);
347 return 0;
348 }
349
350 static int irdp_module_init(void)
351 {
352 hook_register(frr_late_init, irdp_init);
353 return 0;
354 }
355
356 FRR_MODULE_SETUP(.name = "zebra_irdp", .version = FRR_VERSION,
357 .description = "zebra IRDP module", .init = irdp_module_init, )