blob: e5a1436c6dd35c75abd2ccc1d3c11afc08c006e7 [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"
hasso03d52f82004-09-29 00:26:19 +000027#include "sockopt.h"
pauledd7c242003-06-04 13:59:38 +000028#include "privs.h"
paul718e3742002-12-13 20:15:29 +000029
paul718e3742002-12-13 20:15:29 +000030#include "ospf6_proto.h"
hasso508e53e2004-05-18 18:57:06 +000031#include "ospf6_network.h"
paul718e3742002-12-13 20:15:29 +000032
pauledd7c242003-06-04 13:59:38 +000033extern struct zebra_privs_t ospf6d_privs;
paul718e3742002-12-13 20:15:29 +000034
hasso508e53e2004-05-18 18:57:06 +000035int ospf6_sock;
36struct in6_addr allspfrouters6;
37struct in6_addr alldrouters6;
paul718e3742002-12-13 20:15:29 +000038
39/* setsockopt ReUseAddr to on */
40void
Paul Jakma6ac29a52008-08-15 13:45:30 +010041ospf6_set_reuseaddr (void)
paul718e3742002-12-13 20:15:29 +000042{
43 u_int on = 0;
44 if (setsockopt (ospf6_sock, SOL_SOCKET, SO_REUSEADDR, &on,
45 sizeof (u_int)) < 0)
ajs6099b3b2004-11-20 02:06:59 +000046 zlog_warn ("Network: set SO_REUSEADDR failed: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000047}
48
49/* setsockopt MulticastLoop to off */
50void
Paul Jakma6ac29a52008-08-15 13:45:30 +010051ospf6_reset_mcastloop (void)
paul718e3742002-12-13 20:15:29 +000052{
53 u_int off = 0;
54 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
55 &off, sizeof (u_int)) < 0)
56 zlog_warn ("Network: reset IPV6_MULTICAST_LOOP failed: %s",
ajs6099b3b2004-11-20 02:06:59 +000057 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000058}
59
60void
Paul Jakma6ac29a52008-08-15 13:45:30 +010061ospf6_set_pktinfo (void)
paul718e3742002-12-13 20:15:29 +000062{
paul79dc3732004-07-23 15:17:45 +000063 setsockopt_ipv6_pktinfo (ospf6_sock, 1);
paul718e3742002-12-13 20:15:29 +000064}
65
66void
Stephen Hemminger6d0732c2011-09-28 14:23:35 +040067ospf6_set_transport_class (void)
68{
69#ifdef IPTOS_PREC_INTERNETCONTROL
70 setsockopt_ipv6_tclass (ospf6_sock, IPTOS_PREC_INTERNETCONTROL);
71#endif
72}
73
74void
Paul Jakma6ac29a52008-08-15 13:45:30 +010075ospf6_set_checksum (void)
paul718e3742002-12-13 20:15:29 +000076{
77 int offset = 12;
hasso508e53e2004-05-18 18:57:06 +000078#ifndef DISABLE_IPV6_CHECKSUM
paul718e3742002-12-13 20:15:29 +000079 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM,
80 &offset, sizeof (offset)) < 0)
ajs6099b3b2004-11-20 02:06:59 +000081 zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000082#else
83 zlog_warn ("Network: Don't set IPV6_CHECKSUM");
84#endif /* DISABLE_IPV6_CHECKSUM */
85}
86
hasso508e53e2004-05-18 18:57:06 +000087/* Make ospf6d's server socket. */
88int
Paul Jakma6ac29a52008-08-15 13:45:30 +010089ospf6_serv_sock (void)
hasso508e53e2004-05-18 18:57:06 +000090{
91 if (ospf6d_privs.change (ZPRIVS_RAISE))
92 zlog_err ("ospf6_serv_sock: could not raise privs");
93
94 ospf6_sock = socket (AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
95 if (ospf6_sock < 0)
96 {
97 zlog_warn ("Network: can't create OSPF6 socket.");
98 if (ospf6d_privs.change (ZPRIVS_LOWER))
99 zlog_err ("ospf_sock_init: could not lower privs");
100 return -1;
101 }
102 if (ospf6d_privs.change (ZPRIVS_LOWER))
103 zlog_err ("ospf_sock_init: could not lower privs");
104
105 /* set socket options */
106#if 1
107 sockopt_reuseaddr (ospf6_sock);
108#else
109 ospf6_set_reuseaddr ();
110#endif /*1*/
111 ospf6_reset_mcastloop ();
112 ospf6_set_pktinfo ();
Stephen Hemminger6d0732c2011-09-28 14:23:35 +0400113 ospf6_set_transport_class ();
hasso508e53e2004-05-18 18:57:06 +0000114 ospf6_set_checksum ();
115
116 /* setup global in6_addr, allspf6 and alldr6 for later use */
117 inet_pton (AF_INET6, ALLSPFROUTERS6, &allspfrouters6);
118 inet_pton (AF_INET6, ALLDROUTERS6, &alldrouters6);
119
120 return 0;
121}
122
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400123/* ospf6 set socket option */
paul718e3742002-12-13 20:15:29 +0000124void
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400125ospf6_sso (u_int ifindex, struct in6_addr *group, int option)
hasso508e53e2004-05-18 18:57:06 +0000126{
127 struct ipv6_mreq mreq6;
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400128 int ret;
hasso508e53e2004-05-18 18:57:06 +0000129
130 assert (ifindex);
131 mreq6.ipv6mr_interface = ifindex;
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400132 memcpy (&mreq6.ipv6mr_multiaddr, group, sizeof (struct in6_addr));
hasso508e53e2004-05-18 18:57:06 +0000133
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400134 ret = setsockopt (ospf6_sock, IPPROTO_IPV6, option,
135 &mreq6, sizeof (mreq6));
136 if (ret < 0)
137 zlog_err ("Network: setsockopt (%d) on ifindex %d failed: %s",
138 option, ifindex, safe_strerror (errno));
hasso508e53e2004-05-18 18:57:06 +0000139}
140
Paul Jakma6ac29a52008-08-15 13:45:30 +0100141static int
hasso508e53e2004-05-18 18:57:06 +0000142iov_count (struct iovec *iov)
143{
144 int i;
145 for (i = 0; iov[i].iov_base; i++)
146 ;
147 return i;
148}
149
Paul Jakma6ac29a52008-08-15 13:45:30 +0100150static int
hasso508e53e2004-05-18 18:57:06 +0000151iov_totallen (struct iovec *iov)
152{
153 int i;
154 int totallen = 0;
155 for (i = 0; iov[i].iov_base; i++)
156 totallen += iov[i].iov_len;
157 return totallen;
158}
159
160int
paul718e3742002-12-13 20:15:29 +0000161ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst,
162 unsigned int *ifindex, struct iovec *message)
163{
164 int retval;
165 struct msghdr smsghdr;
166 struct cmsghdr *scmsgp;
167 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
168 struct in6_pktinfo *pktinfo;
169 struct sockaddr_in6 dst_sin6;
170
171 assert (dst);
172 assert (*ifindex);
173
174 scmsgp = (struct cmsghdr *)cmsgbuf;
175 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
176 memset (&dst_sin6, 0, sizeof (struct sockaddr_in6));
177
178 /* source address */
179 pktinfo->ipi6_ifindex = *ifindex;
180 if (src)
181 memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr));
182 else
183 memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr));
184
185 /* destination address */
186 dst_sin6.sin6_family = AF_INET6;
187#ifdef SIN6_LEN
188 dst_sin6.sin6_len = sizeof (struct sockaddr_in6);
189#endif /*SIN6_LEN*/
190 memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr));
191#ifdef HAVE_SIN6_SCOPE_ID
192 dst_sin6.sin6_scope_id = *ifindex;
193#endif
194
195 /* send control msg */
196 scmsgp->cmsg_level = IPPROTO_IPV6;
197 scmsgp->cmsg_type = IPV6_PKTINFO;
198 scmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
199 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
200
201 /* send msg hdr */
Paul Jakmacf1ce252006-05-15 10:46:07 +0000202 memset (&smsghdr, 0, sizeof (smsghdr));
paul718e3742002-12-13 20:15:29 +0000203 smsghdr.msg_iov = message;
204 smsghdr.msg_iovlen = iov_count (message);
205 smsghdr.msg_name = (caddr_t) &dst_sin6;
206 smsghdr.msg_namelen = sizeof (struct sockaddr_in6);
207 smsghdr.msg_control = (caddr_t) cmsgbuf;
208 smsghdr.msg_controllen = sizeof (cmsgbuf);
209
210 retval = sendmsg (ospf6_sock, &smsghdr, 0);
211 if (retval != iov_totallen (message))
hasso508e53e2004-05-18 18:57:06 +0000212 zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)",
ajs6099b3b2004-11-20 02:06:59 +0000213 *ifindex, safe_strerror (errno), errno);
hasso508e53e2004-05-18 18:57:06 +0000214
215 return retval;
paul718e3742002-12-13 20:15:29 +0000216}
217
hasso508e53e2004-05-18 18:57:06 +0000218int
paul718e3742002-12-13 20:15:29 +0000219ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst,
220 unsigned int *ifindex, struct iovec *message)
221{
222 int retval;
223 struct msghdr rmsghdr;
224 struct cmsghdr *rcmsgp;
225 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
226 struct in6_pktinfo *pktinfo;
227 struct sockaddr_in6 src_sin6;
228
229 rcmsgp = (struct cmsghdr *)cmsgbuf;
230 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
231 memset (&src_sin6, 0, sizeof (struct sockaddr_in6));
232
233 /* receive control msg */
234 rcmsgp->cmsg_level = IPPROTO_IPV6;
235 rcmsgp->cmsg_type = IPV6_PKTINFO;
236 rcmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
237 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
238
239 /* receive msg hdr */
Paul Jakmacf1ce252006-05-15 10:46:07 +0000240 memset (&rmsghdr, 0, sizeof (rmsghdr));
paul718e3742002-12-13 20:15:29 +0000241 rmsghdr.msg_iov = message;
242 rmsghdr.msg_iovlen = iov_count (message);
243 rmsghdr.msg_name = (caddr_t) &src_sin6;
244 rmsghdr.msg_namelen = sizeof (struct sockaddr_in6);
245 rmsghdr.msg_control = (caddr_t) cmsgbuf;
246 rmsghdr.msg_controllen = sizeof (cmsgbuf);
247
248 retval = recvmsg (ospf6_sock, &rmsghdr, 0);
249 if (retval < 0)
ajs6099b3b2004-11-20 02:06:59 +0000250 zlog_warn ("recvmsg failed: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000251 else if (retval == iov_totallen (message))
hasso508e53e2004-05-18 18:57:06 +0000252 zlog_warn ("recvmsg read full buffer size: %d", retval);
paul718e3742002-12-13 20:15:29 +0000253
254 /* source address */
255 assert (src);
256 memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr));
257
258 /* destination address */
259 if (ifindex)
260 *ifindex = pktinfo->ipi6_ifindex;
261 if (dst)
262 memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr));
hasso508e53e2004-05-18 18:57:06 +0000263
264 return retval;
paul718e3742002-12-13 20:15:29 +0000265}
266
paul718e3742002-12-13 20:15:29 +0000267