blob: 041d829b504453a7107a8cfcfc76612330d8fbf9 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Copyright (C) 1999 Yasuhiro Ohara
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22#include <zebra.h>
23#include "memory.h"
24#include "log.h"
25#include "sockunion.h"
26
27#include "ospf6d.h"
28#include "ospf6_proto.h"
29
30extern int errno;
31extern struct sockaddr_in6 allspfrouters6;
32extern struct sockaddr_in6 alldrouters6;
33extern int ospf6_sock;
34extern struct thread_master *master;
35
36/* iovec functions */
37void
38iov_clear (struct iovec *iov, size_t iovlen)
39{
40 int i;
41 for (i = 0; i < iovlen; i++)
42 {
43 iov[i].iov_base = NULL;
44 iov[i].iov_len = 0;
45 }
46}
47
48int
49iov_count (struct iovec *iov)
50{
51 int i;
52 for (i = 0; iov[i].iov_base; i++)
53 ;
54 return i;
55}
56
57int
58iov_totallen (struct iovec *iov)
59{
60 int i;
61 int totallen = 0;
62 for (i = 0; iov[i].iov_base; i++)
63 totallen += iov[i].iov_len;
64 return totallen;
65}
66
67void *
68iov_prepend (int mtype, struct iovec *iov, size_t len)
69{
70 int i, iovlen;
71 void *base;
72
73 base = (void *) XMALLOC (mtype, len);
74 if (!base)
75 {
76 zlog_warn ("Network: iov_prepend failed");
77 return NULL;
78 }
79 memset (base, 0, len);
80
81 iovlen = iov_count (iov);
82 for (i = iovlen; i; i--)
83 {
84 iov[i].iov_base = iov[i - 1].iov_base;
85 iov[i].iov_len = iov[i - 1].iov_len;
86 }
87 iov[0].iov_base = (char *)base;
88 iov[0].iov_len = len;
89
90 return base;
91}
92
93void *
94iov_append (int mtype, struct iovec *iov, size_t len)
95{
96 int i;
97 void *base;
98
99 base = (void *)XMALLOC (mtype, len);
100 if (!base)
101 {
102 zlog_warn ("Network: iov_append failed");
103 return NULL;
104 }
105 memset (base, 0, len);
106
107 /* proceed to the end */
108 i = iov_count (iov);
109
110 iov[i].iov_base = (char *)base;
111 iov[i].iov_len = len;
112
113 return base;
114}
115
116void *
117iov_attach_last (struct iovec *iov, void *base, size_t len)
118{
119 int i;
120 i = iov_count (iov);
121 iov[i].iov_base = (char *)base;
122 iov[i].iov_len = len;
123 return base;
124}
125
126void *
127iov_detach_first (struct iovec *iov)
128{
129 int i, iovlen;
130 void *base;
131 size_t len;
132
133 base = iov[0].iov_base;
134 len = iov[0].iov_len;
135 iovlen = iov_count (iov);
136 for (i = 0; i < iovlen; i++)
137 {
138 iov[i].iov_base = iov[i + 1].iov_base;
139 iov[i].iov_len = iov[i + 1].iov_len;
140 }
141 return base;
142}
143
144int
145iov_free (int mtype, struct iovec *iov, u_int begin, u_int end)
146{
147 int i;
148
149 for (i = begin; i < end; i++)
150 {
151 XFREE (mtype, iov[i].iov_base);
152 iov[i].iov_base = NULL;
153 iov[i].iov_len = 0;
154 }
155
156 return 0;
157}
158
159void
160iov_trim_head (int mtype, struct iovec *iov)
161{
162 void *base;
163
164 base = iov_detach_first (iov);
165 XFREE (mtype, base);
166 return;
167}
168
169void
170iov_free_all (int mtype, struct iovec *iov)
171{
172 int i, end = iov_count (iov);
173 for (i = 0; i < end; i++)
174 {
175 XFREE (mtype, iov[i].iov_base);
176 iov[i].iov_base = NULL;
177 iov[i].iov_len = 0;
178 }
179}
180
181void
182iov_copy_all (struct iovec *dst, struct iovec *src, size_t size)
183{
184 int i;
185 for (i = 0; i < size; i++)
186 {
187 dst[i].iov_base = src[i].iov_base;
188 dst[i].iov_len = src[i].iov_len;
189 }
190}
191
192
193/* Make ospf6d's server socket. */
194int
195ospf6_serv_sock ()
196{
197 ospf6_sock = socket (AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
198 if (ospf6_sock < 0)
199 {
200 zlog_warn ("Network: can't create OSPF6 socket.");
201 return -1;
202 }
203 sockopt_reuseaddr (ospf6_sock);
204
205 /* setup global sockaddr_in6, allspf6 & alldr6 for later use */
206 allspfrouters6.sin6_family = AF_INET6;
207 alldrouters6.sin6_family = AF_INET6;
208#ifdef SIN6_LEN
209 allspfrouters6.sin6_len = sizeof (struct sockaddr_in6);
210 alldrouters6.sin6_len = sizeof (struct sockaddr_in6);
211#endif /* SIN6_LEN */
212 inet_pton (AF_INET6, ALLSPFROUTERS6, &allspfrouters6.sin6_addr);
213 inet_pton (AF_INET6, ALLDROUTERS6, &alldrouters6.sin6_addr);
214
215 return 0;
216}
217
218/* returns 0 if succeed, else returns -1 */
219int
220ospf6_join_allspfrouters (u_int ifindex)
221{
222 struct ipv6_mreq mreq6;
223 int retval;
224
225 assert (ifindex);
226 mreq6.ipv6mr_interface = ifindex;
227 memcpy (&mreq6.ipv6mr_multiaddr, &allspfrouters6.sin6_addr,
228 sizeof (struct in6_addr));
229
230 retval = setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
231 &mreq6, sizeof (mreq6));
232
233 if (retval < 0)
234 zlog_err ("Network: Join AllSPFRouters on ifindex %d failed: %s",
235 ifindex, strerror (errno));
236#if 0
237 else
238 zlog_info ("Network: Join AllSPFRouters on ifindex %d", ifindex);
239#endif
240
241 return retval;
242}
243
244void
245ospf6_leave_allspfrouters (u_int ifindex)
246{
247 struct ipv6_mreq mreq6;
248
249 assert (ifindex);
250 mreq6.ipv6mr_interface = ifindex;
251 memcpy (&mreq6.ipv6mr_multiaddr, &allspfrouters6.sin6_addr,
252 sizeof (struct in6_addr));
253
254 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
255 &mreq6, sizeof (mreq6)) < 0)
256 zlog_warn ("Network: Leave AllSPFRouters on ifindex %d Failed: %s",
257 ifindex, strerror (errno));
258#if 0
259 else
260 zlog_info ("Network: Leave AllSPFRouters on ifindex %d", ifindex);
261#endif
262}
263
264void
265ospf6_join_alldrouters (u_int ifindex)
266{
267 struct ipv6_mreq mreq6;
268
269 assert (ifindex);
270 mreq6.ipv6mr_interface = ifindex;
271 memcpy (&mreq6.ipv6mr_multiaddr, &alldrouters6.sin6_addr,
272 sizeof (struct in6_addr));
273
274 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
275 &mreq6, sizeof (mreq6)) < 0)
276 zlog_warn ("Network: Join AllDRouters on ifindex %d Failed: %s",
277 ifindex, strerror (errno));
278#if 0
279 else
280 zlog_info ("Network: Join AllDRouters on ifindex %d", ifindex);
281#endif
282}
283
284void
285ospf6_leave_alldrouters (u_int ifindex)
286{
287 struct ipv6_mreq mreq6;
288
289 assert (ifindex);
290 mreq6.ipv6mr_interface = ifindex;
291 memcpy (&mreq6.ipv6mr_multiaddr, &alldrouters6.sin6_addr,
292 sizeof (struct in6_addr));
293
294 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
295 &mreq6, sizeof (mreq6)) < 0)
296 zlog_warn ("Network: Leave AllDRouters on ifindex %d Failed", ifindex);
297#if 0
298 else
299 zlog_info ("Network: Leave AllDRouters on ifindex %d", ifindex);
300#endif
301}
302
303/* setsockopt ReUseAddr to on */
304void
305ospf6_set_reuseaddr ()
306{
307 u_int on = 0;
308 if (setsockopt (ospf6_sock, SOL_SOCKET, SO_REUSEADDR, &on,
309 sizeof (u_int)) < 0)
310 zlog_warn ("Network: set SO_REUSEADDR failed: %s", strerror (errno));
311}
312
313/* setsockopt MulticastLoop to off */
314void
315ospf6_reset_mcastloop ()
316{
317 u_int off = 0;
318 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
319 &off, sizeof (u_int)) < 0)
320 zlog_warn ("Network: reset IPV6_MULTICAST_LOOP failed: %s",
321 strerror (errno));
322}
323
324void
325ospf6_set_pktinfo ()
326{
327 u_int on = 1;
328#ifdef IPV6_RECVPKTINFO /*2292bis-01*/
329 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
330 &on, sizeof (u_int)) < 0)
331 zlog_warn ("Network: set IPV6_RECVPKTINFO failed: %s", strerror (errno));
332#else /*RFC2292*/
333 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_PKTINFO,
334 &on, sizeof (u_int)) < 0)
335 zlog_warn ("Network: set IPV6_PKTINFO failed: %s", strerror (errno));
336#endif
337}
338
339void
340ospf6_set_checksum ()
341{
342 int offset = 12;
343#ifndef DISABLE_IPV6_CHECKSUM
344 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM,
345 &offset, sizeof (offset)) < 0)
346 zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", strerror (errno));
347#else
348 zlog_warn ("Network: Don't set IPV6_CHECKSUM");
349#endif /* DISABLE_IPV6_CHECKSUM */
350}
351
352void
353ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst,
354 unsigned int *ifindex, struct iovec *message)
355{
356 int retval;
357 struct msghdr smsghdr;
358 struct cmsghdr *scmsgp;
359 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
360 struct in6_pktinfo *pktinfo;
361 struct sockaddr_in6 dst_sin6;
362
363 assert (dst);
364 assert (*ifindex);
365
366 scmsgp = (struct cmsghdr *)cmsgbuf;
367 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
368 memset (&dst_sin6, 0, sizeof (struct sockaddr_in6));
369
370 /* source address */
371 pktinfo->ipi6_ifindex = *ifindex;
372 if (src)
373 memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr));
374 else
375 memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr));
376
377 /* destination address */
378 dst_sin6.sin6_family = AF_INET6;
379#ifdef SIN6_LEN
380 dst_sin6.sin6_len = sizeof (struct sockaddr_in6);
381#endif /*SIN6_LEN*/
382 memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr));
383#ifdef HAVE_SIN6_SCOPE_ID
384 dst_sin6.sin6_scope_id = *ifindex;
385#endif
386
387 /* send control msg */
388 scmsgp->cmsg_level = IPPROTO_IPV6;
389 scmsgp->cmsg_type = IPV6_PKTINFO;
390 scmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
391 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
392
393 /* send msg hdr */
394 smsghdr.msg_iov = message;
395 smsghdr.msg_iovlen = iov_count (message);
396 smsghdr.msg_name = (caddr_t) &dst_sin6;
397 smsghdr.msg_namelen = sizeof (struct sockaddr_in6);
398 smsghdr.msg_control = (caddr_t) cmsgbuf;
399 smsghdr.msg_controllen = sizeof (cmsgbuf);
400
401 retval = sendmsg (ospf6_sock, &smsghdr, 0);
402 if (retval != iov_totallen (message))
403 zlog_warn ("Network: sendmsg (ifindex: %d) failed: %s(%d)",
404 *ifindex, strerror (errno), errno);
405}
406
407void
408ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst,
409 unsigned int *ifindex, struct iovec *message)
410{
411 int retval;
412 struct msghdr rmsghdr;
413 struct cmsghdr *rcmsgp;
414 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
415 struct in6_pktinfo *pktinfo;
416 struct sockaddr_in6 src_sin6;
417
418 rcmsgp = (struct cmsghdr *)cmsgbuf;
419 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
420 memset (&src_sin6, 0, sizeof (struct sockaddr_in6));
421
422 /* receive control msg */
423 rcmsgp->cmsg_level = IPPROTO_IPV6;
424 rcmsgp->cmsg_type = IPV6_PKTINFO;
425 rcmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
426 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
427
428 /* receive msg hdr */
429 rmsghdr.msg_iov = message;
430 rmsghdr.msg_iovlen = iov_count (message);
431 rmsghdr.msg_name = (caddr_t) &src_sin6;
432 rmsghdr.msg_namelen = sizeof (struct sockaddr_in6);
433 rmsghdr.msg_control = (caddr_t) cmsgbuf;
434 rmsghdr.msg_controllen = sizeof (cmsgbuf);
435
436 retval = recvmsg (ospf6_sock, &rmsghdr, 0);
437 if (retval < 0)
438 {
439 zlog_warn ("Network: recvmsg failed: %s", strerror (errno));
440 }
441 else if (retval == iov_totallen (message))
442 {
443 zlog_warn ("Network: possibly buffer shortage: %d received, buffer size: %d",
444 retval, iov_totallen (message));
445 }
446
447 /* source address */
448 assert (src);
449 memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr));
450
451 /* destination address */
452 if (ifindex)
453 *ifindex = pktinfo->ipi6_ifindex;
454 if (dst)
455 memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr));
456}
457
458void
459ospf6_recvmsg_peek (struct in6_addr *src, struct in6_addr *dst,
460 unsigned int *ifindex, struct iovec *message)
461{
462 int retval;
463 struct msghdr rmsghdr;
464 struct cmsghdr *rcmsgp;
465 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
466 struct in6_pktinfo *pktinfo;
467 struct sockaddr_in6 src_sin6;
468
469 rcmsgp = (struct cmsghdr *)cmsgbuf;
470 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
471 memset (&src_sin6, 0, sizeof (struct sockaddr_in6));
472
473 /* receive control msg */
474 rcmsgp->cmsg_level = IPPROTO_IPV6;
475 rcmsgp->cmsg_type = IPV6_PKTINFO;
476 rcmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
477 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
478
479 /* receive msg hdr */
480 rmsghdr.msg_iov = message;
481 rmsghdr.msg_iovlen = iov_count (message);
482 rmsghdr.msg_name = (caddr_t) &src_sin6;
483 rmsghdr.msg_namelen = sizeof (struct sockaddr_in6);
484 rmsghdr.msg_control = (caddr_t) cmsgbuf;
485 rmsghdr.msg_controllen = sizeof (cmsgbuf);
486
487 retval = recvmsg (ospf6_sock, &rmsghdr, MSG_PEEK);
488 if (retval != iov_totallen (message))
489 zlog_warn ("Network: recvmsg failed: %s", strerror (errno));
490
491 /* source address */
492 assert (src);
493 memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr));
494
495 /* destination address */
496 if (ifindex)
497 *ifindex = pktinfo->ipi6_ifindex;
498 if (dst)
499 memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr));
500}
501