2 * Copyright (C) 2003 Yasuhiro Ohara
4 * This file is part of GNU Zebra.
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
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.
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.
26 #include "sockunion.h"
29 #include "ospf6_proto.h"
30 #include "ospf6_network.h"
32 extern struct zebra_privs_t ospf6d_privs
;
35 struct in6_addr allspfrouters6
;
36 struct in6_addr alldrouters6
;
38 /* setsockopt ReUseAddr to on */
40 ospf6_set_reuseaddr ()
43 if (setsockopt (ospf6_sock
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
45 zlog_warn ("Network: set SO_REUSEADDR failed: %s", strerror (errno
));
48 /* setsockopt MulticastLoop to off */
50 ospf6_reset_mcastloop ()
53 if (setsockopt (ospf6_sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
,
54 &off
, sizeof (u_int
)) < 0)
55 zlog_warn ("Network: reset IPV6_MULTICAST_LOOP failed: %s",
62 setsockopt_ipv6_pktinfo (ospf6_sock
, 1);
69 #ifndef DISABLE_IPV6_CHECKSUM
70 if (setsockopt (ospf6_sock
, IPPROTO_IPV6
, IPV6_CHECKSUM
,
71 &offset
, sizeof (offset
)) < 0)
72 zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", strerror (errno
));
74 zlog_warn ("Network: Don't set IPV6_CHECKSUM");
75 #endif /* DISABLE_IPV6_CHECKSUM */
78 /* Make ospf6d's server socket. */
82 if (ospf6d_privs
.change (ZPRIVS_RAISE
))
83 zlog_err ("ospf6_serv_sock: could not raise privs");
85 ospf6_sock
= socket (AF_INET6
, SOCK_RAW
, IPPROTO_OSPFIGP
);
88 zlog_warn ("Network: can't create OSPF6 socket.");
89 if (ospf6d_privs
.change (ZPRIVS_LOWER
))
90 zlog_err ("ospf_sock_init: could not lower privs");
93 if (ospf6d_privs
.change (ZPRIVS_LOWER
))
94 zlog_err ("ospf_sock_init: could not lower privs");
96 /* set socket options */
98 sockopt_reuseaddr (ospf6_sock
);
100 ospf6_set_reuseaddr ();
102 ospf6_reset_mcastloop ();
103 ospf6_set_pktinfo ();
104 ospf6_set_checksum ();
106 /* setup global in6_addr, allspf6 and alldr6 for later use */
107 inet_pton (AF_INET6
, ALLSPFROUTERS6
, &allspfrouters6
);
108 inet_pton (AF_INET6
, ALLDROUTERS6
, &alldrouters6
);
114 ospf6_join_allspfrouters (u_int ifindex
)
116 struct ipv6_mreq mreq6
;
120 mreq6
.ipv6mr_interface
= ifindex
;
121 memcpy (&mreq6
.ipv6mr_multiaddr
, &allspfrouters6
,
122 sizeof (struct in6_addr
));
124 retval
= setsockopt (ospf6_sock
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
125 &mreq6
, sizeof (mreq6
));
128 zlog_err ("Network: Join AllSPFRouters on ifindex %d failed: %s",
129 ifindex
, strerror (errno
));
132 zlog_info ("Network: Join AllSPFRouters on ifindex %d", ifindex
);
137 ospf6_leave_allspfrouters (u_int ifindex
)
139 struct ipv6_mreq mreq6
;
142 mreq6
.ipv6mr_interface
= ifindex
;
143 memcpy (&mreq6
.ipv6mr_multiaddr
, &allspfrouters6
,
144 sizeof (struct in6_addr
));
146 if (setsockopt (ospf6_sock
, IPPROTO_IPV6
, IPV6_LEAVE_GROUP
,
147 &mreq6
, sizeof (mreq6
)) < 0)
148 zlog_warn ("Network: Leave AllSPFRouters on ifindex %d Failed: %s",
149 ifindex
, strerror (errno
));
152 zlog_info ("Network: Leave AllSPFRouters on ifindex %d", ifindex
);
157 ospf6_join_alldrouters (u_int ifindex
)
159 struct ipv6_mreq mreq6
;
162 mreq6
.ipv6mr_interface
= ifindex
;
163 memcpy (&mreq6
.ipv6mr_multiaddr
, &alldrouters6
,
164 sizeof (struct in6_addr
));
166 if (setsockopt (ospf6_sock
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
167 &mreq6
, sizeof (mreq6
)) < 0)
168 zlog_warn ("Network: Join AllDRouters on ifindex %d Failed: %s",
169 ifindex
, strerror (errno
));
172 zlog_info ("Network: Join AllDRouters on ifindex %d", ifindex
);
177 ospf6_leave_alldrouters (u_int ifindex
)
179 struct ipv6_mreq mreq6
;
182 mreq6
.ipv6mr_interface
= ifindex
;
183 memcpy (&mreq6
.ipv6mr_multiaddr
, &alldrouters6
,
184 sizeof (struct in6_addr
));
186 if (setsockopt (ospf6_sock
, IPPROTO_IPV6
, IPV6_LEAVE_GROUP
,
187 &mreq6
, sizeof (mreq6
)) < 0)
188 zlog_warn ("Network: Leave AllDRouters on ifindex %d Failed", ifindex
);
191 zlog_info ("Network: Leave AllDRouters on ifindex %d", ifindex
);
196 iov_count (struct iovec
*iov
)
199 for (i
= 0; iov
[i
].iov_base
; i
++)
205 iov_totallen (struct iovec
*iov
)
209 for (i
= 0; iov
[i
].iov_base
; i
++)
210 totallen
+= iov
[i
].iov_len
;
215 ospf6_sendmsg (struct in6_addr
*src
, struct in6_addr
*dst
,
216 unsigned int *ifindex
, struct iovec
*message
)
219 struct msghdr smsghdr
;
220 struct cmsghdr
*scmsgp
;
221 u_char cmsgbuf
[CMSG_SPACE(sizeof (struct in6_pktinfo
))];
222 struct in6_pktinfo
*pktinfo
;
223 struct sockaddr_in6 dst_sin6
;
228 scmsgp
= (struct cmsghdr
*)cmsgbuf
;
229 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
230 memset (&dst_sin6
, 0, sizeof (struct sockaddr_in6
));
233 pktinfo
->ipi6_ifindex
= *ifindex
;
235 memcpy (&pktinfo
->ipi6_addr
, src
, sizeof (struct in6_addr
));
237 memset (&pktinfo
->ipi6_addr
, 0, sizeof (struct in6_addr
));
239 /* destination address */
240 dst_sin6
.sin6_family
= AF_INET6
;
242 dst_sin6
.sin6_len
= sizeof (struct sockaddr_in6
);
244 memcpy (&dst_sin6
.sin6_addr
, dst
, sizeof (struct in6_addr
));
245 #ifdef HAVE_SIN6_SCOPE_ID
246 dst_sin6
.sin6_scope_id
= *ifindex
;
249 /* send control msg */
250 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
251 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
252 scmsgp
->cmsg_len
= CMSG_LEN (sizeof (struct in6_pktinfo
));
253 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
256 smsghdr
.msg_iov
= message
;
257 smsghdr
.msg_iovlen
= iov_count (message
);
258 smsghdr
.msg_name
= (caddr_t
) &dst_sin6
;
259 smsghdr
.msg_namelen
= sizeof (struct sockaddr_in6
);
260 smsghdr
.msg_control
= (caddr_t
) cmsgbuf
;
261 smsghdr
.msg_controllen
= sizeof (cmsgbuf
);
263 retval
= sendmsg (ospf6_sock
, &smsghdr
, 0);
264 if (retval
!= iov_totallen (message
))
265 zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)",
266 *ifindex
, strerror (errno
), errno
);
272 ospf6_recvmsg (struct in6_addr
*src
, struct in6_addr
*dst
,
273 unsigned int *ifindex
, struct iovec
*message
)
276 struct msghdr rmsghdr
;
277 struct cmsghdr
*rcmsgp
;
278 u_char cmsgbuf
[CMSG_SPACE(sizeof (struct in6_pktinfo
))];
279 struct in6_pktinfo
*pktinfo
;
280 struct sockaddr_in6 src_sin6
;
282 rcmsgp
= (struct cmsghdr
*)cmsgbuf
;
283 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(rcmsgp
));
284 memset (&src_sin6
, 0, sizeof (struct sockaddr_in6
));
286 /* receive control msg */
287 rcmsgp
->cmsg_level
= IPPROTO_IPV6
;
288 rcmsgp
->cmsg_type
= IPV6_PKTINFO
;
289 rcmsgp
->cmsg_len
= CMSG_LEN (sizeof (struct in6_pktinfo
));
290 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
292 /* receive msg hdr */
293 rmsghdr
.msg_iov
= message
;
294 rmsghdr
.msg_iovlen
= iov_count (message
);
295 rmsghdr
.msg_name
= (caddr_t
) &src_sin6
;
296 rmsghdr
.msg_namelen
= sizeof (struct sockaddr_in6
);
297 rmsghdr
.msg_control
= (caddr_t
) cmsgbuf
;
298 rmsghdr
.msg_controllen
= sizeof (cmsgbuf
);
300 retval
= recvmsg (ospf6_sock
, &rmsghdr
, 0);
302 zlog_warn ("recvmsg failed: %s", strerror (errno
));
303 else if (retval
== iov_totallen (message
))
304 zlog_warn ("recvmsg read full buffer size: %d", retval
);
308 memcpy (src
, &src_sin6
.sin6_addr
, sizeof (struct in6_addr
));
310 /* destination address */
312 *ifindex
= pktinfo
->ipi6_ifindex
;
314 memcpy (dst
, &pktinfo
->ipi6_addr
, sizeof (struct in6_addr
));