blob: d5bfdd508b10a00a294e15e0f99d10471b3cc5fd [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
hasso508e53e2004-05-18 18:57:06 +00002 * Copyright (C) 2003 Yasuhiro Ohara
paul718e3742002-12-13 20:15:29 +00003 *
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>
hasso508e53e2004-05-18 18:57:06 +000023
paul718e3742002-12-13 20:15:29 +000024#include "log.h"
hasso508e53e2004-05-18 18:57:06 +000025#include "memory.h"
paul718e3742002-12-13 20:15:29 +000026#include "sockunion.h"
pauledd7c242003-06-04 13:59:38 +000027#include "privs.h"
paul718e3742002-12-13 20:15:29 +000028
paul718e3742002-12-13 20:15:29 +000029#include "ospf6_proto.h"
hasso508e53e2004-05-18 18:57:06 +000030#include "ospf6_network.h"
paul718e3742002-12-13 20:15:29 +000031
pauledd7c242003-06-04 13:59:38 +000032extern struct zebra_privs_t ospf6d_privs;
paul718e3742002-12-13 20:15:29 +000033
hasso508e53e2004-05-18 18:57:06 +000034int ospf6_sock;
35struct in6_addr allspfrouters6;
36struct in6_addr alldrouters6;
paul718e3742002-12-13 20:15:29 +000037
38/* setsockopt ReUseAddr to on */
39void
40ospf6_set_reuseaddr ()
41{
42 u_int on = 0;
43 if (setsockopt (ospf6_sock, SOL_SOCKET, SO_REUSEADDR, &on,
44 sizeof (u_int)) < 0)
45 zlog_warn ("Network: set SO_REUSEADDR failed: %s", strerror (errno));
46}
47
48/* setsockopt MulticastLoop to off */
49void
50ospf6_reset_mcastloop ()
51{
52 u_int off = 0;
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",
56 strerror (errno));
57}
58
59void
60ospf6_set_pktinfo ()
61{
paul79dc3732004-07-23 15:17:45 +000062 setsockopt_ipv6_pktinfo (ospf6_sock, 1);
paul718e3742002-12-13 20:15:29 +000063}
64
65void
66ospf6_set_checksum ()
67{
68 int offset = 12;
hasso508e53e2004-05-18 18:57:06 +000069#ifndef DISABLE_IPV6_CHECKSUM
paul718e3742002-12-13 20:15:29 +000070 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM,
71 &offset, sizeof (offset)) < 0)
72 zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", strerror (errno));
73#else
74 zlog_warn ("Network: Don't set IPV6_CHECKSUM");
75#endif /* DISABLE_IPV6_CHECKSUM */
76}
77
hasso508e53e2004-05-18 18:57:06 +000078/* Make ospf6d's server socket. */
79int
80ospf6_serv_sock ()
81{
82 if (ospf6d_privs.change (ZPRIVS_RAISE))
83 zlog_err ("ospf6_serv_sock: could not raise privs");
84
85 ospf6_sock = socket (AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
86 if (ospf6_sock < 0)
87 {
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");
91 return -1;
92 }
93 if (ospf6d_privs.change (ZPRIVS_LOWER))
94 zlog_err ("ospf_sock_init: could not lower privs");
95
96 /* set socket options */
97#if 1
98 sockopt_reuseaddr (ospf6_sock);
99#else
100 ospf6_set_reuseaddr ();
101#endif /*1*/
102 ospf6_reset_mcastloop ();
103 ospf6_set_pktinfo ();
104 ospf6_set_checksum ();
105
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);
109
110 return 0;
111}
112
paul718e3742002-12-13 20:15:29 +0000113void
hasso508e53e2004-05-18 18:57:06 +0000114ospf6_join_allspfrouters (u_int ifindex)
115{
116 struct ipv6_mreq mreq6;
117 int retval;
118
119 assert (ifindex);
120 mreq6.ipv6mr_interface = ifindex;
121 memcpy (&mreq6.ipv6mr_multiaddr, &allspfrouters6,
122 sizeof (struct in6_addr));
123
124 retval = setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
125 &mreq6, sizeof (mreq6));
126
127 if (retval < 0)
128 zlog_err ("Network: Join AllSPFRouters on ifindex %d failed: %s",
129 ifindex, strerror (errno));
130#if 0
131 else
132 zlog_info ("Network: Join AllSPFRouters on ifindex %d", ifindex);
133#endif
134}
135
136void
137ospf6_leave_allspfrouters (u_int ifindex)
138{
139 struct ipv6_mreq mreq6;
140
141 assert (ifindex);
142 mreq6.ipv6mr_interface = ifindex;
143 memcpy (&mreq6.ipv6mr_multiaddr, &allspfrouters6,
144 sizeof (struct in6_addr));
145
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));
150#if 0
151 else
152 zlog_info ("Network: Leave AllSPFRouters on ifindex %d", ifindex);
153#endif
154}
155
156void
157ospf6_join_alldrouters (u_int ifindex)
158{
159 struct ipv6_mreq mreq6;
160
161 assert (ifindex);
162 mreq6.ipv6mr_interface = ifindex;
163 memcpy (&mreq6.ipv6mr_multiaddr, &alldrouters6,
164 sizeof (struct in6_addr));
165
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));
170#if 0
171 else
172 zlog_info ("Network: Join AllDRouters on ifindex %d", ifindex);
173#endif
174}
175
176void
177ospf6_leave_alldrouters (u_int ifindex)
178{
179 struct ipv6_mreq mreq6;
180
181 assert (ifindex);
182 mreq6.ipv6mr_interface = ifindex;
183 memcpy (&mreq6.ipv6mr_multiaddr, &alldrouters6,
184 sizeof (struct in6_addr));
185
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);
189#if 0
190 else
191 zlog_info ("Network: Leave AllDRouters on ifindex %d", ifindex);
192#endif
193}
194
195int
196iov_count (struct iovec *iov)
197{
198 int i;
199 for (i = 0; iov[i].iov_base; i++)
200 ;
201 return i;
202}
203
204int
205iov_totallen (struct iovec *iov)
206{
207 int i;
208 int totallen = 0;
209 for (i = 0; iov[i].iov_base; i++)
210 totallen += iov[i].iov_len;
211 return totallen;
212}
213
214int
paul718e3742002-12-13 20:15:29 +0000215ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst,
216 unsigned int *ifindex, struct iovec *message)
217{
218 int retval;
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;
224
225 assert (dst);
226 assert (*ifindex);
227
228 scmsgp = (struct cmsghdr *)cmsgbuf;
229 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
230 memset (&dst_sin6, 0, sizeof (struct sockaddr_in6));
231
232 /* source address */
233 pktinfo->ipi6_ifindex = *ifindex;
234 if (src)
235 memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr));
236 else
237 memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr));
238
239 /* destination address */
240 dst_sin6.sin6_family = AF_INET6;
241#ifdef SIN6_LEN
242 dst_sin6.sin6_len = sizeof (struct sockaddr_in6);
243#endif /*SIN6_LEN*/
244 memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr));
245#ifdef HAVE_SIN6_SCOPE_ID
246 dst_sin6.sin6_scope_id = *ifindex;
247#endif
248
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); */
254
255 /* send msg hdr */
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);
262
263 retval = sendmsg (ospf6_sock, &smsghdr, 0);
264 if (retval != iov_totallen (message))
hasso508e53e2004-05-18 18:57:06 +0000265 zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)",
paul718e3742002-12-13 20:15:29 +0000266 *ifindex, strerror (errno), errno);
hasso508e53e2004-05-18 18:57:06 +0000267
268 return retval;
paul718e3742002-12-13 20:15:29 +0000269}
270
hasso508e53e2004-05-18 18:57:06 +0000271int
paul718e3742002-12-13 20:15:29 +0000272ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst,
273 unsigned int *ifindex, struct iovec *message)
274{
275 int retval;
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;
281
282 rcmsgp = (struct cmsghdr *)cmsgbuf;
283 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
284 memset (&src_sin6, 0, sizeof (struct sockaddr_in6));
285
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); */
291
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);
299
300 retval = recvmsg (ospf6_sock, &rmsghdr, 0);
301 if (retval < 0)
hasso508e53e2004-05-18 18:57:06 +0000302 zlog_warn ("recvmsg failed: %s", strerror (errno));
paul718e3742002-12-13 20:15:29 +0000303 else if (retval == iov_totallen (message))
hasso508e53e2004-05-18 18:57:06 +0000304 zlog_warn ("recvmsg read full buffer size: %d", retval);
paul718e3742002-12-13 20:15:29 +0000305
306 /* source address */
307 assert (src);
308 memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr));
309
310 /* destination address */
311 if (ifindex)
312 *ifindex = pktinfo->ipi6_ifindex;
313 if (dst)
314 memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr));
hasso508e53e2004-05-18 18:57:06 +0000315
316 return retval;
paul718e3742002-12-13 20:15:29 +0000317}
318
paul718e3742002-12-13 20:15:29 +0000319