blob: e0be38b32652ad0e0e8e96badf382104babdea1f [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
Dinesh Dutt8551e6d2013-10-22 17:42:18 -070030#include "libospf.h"
paul718e3742002-12-13 20:15:29 +000031#include "ospf6_proto.h"
hasso508e53e2004-05-18 18:57:06 +000032#include "ospf6_network.h"
paul718e3742002-12-13 20:15:29 +000033
pauledd7c242003-06-04 13:59:38 +000034extern struct zebra_privs_t ospf6d_privs;
paul718e3742002-12-13 20:15:29 +000035
hasso508e53e2004-05-18 18:57:06 +000036int ospf6_sock;
37struct in6_addr allspfrouters6;
38struct in6_addr alldrouters6;
paul718e3742002-12-13 20:15:29 +000039
paul718e3742002-12-13 20:15:29 +000040/* setsockopt MulticastLoop to off */
Paul Jakma7aa9dce2014-09-19 14:42:23 +010041static void
Paul Jakma6ac29a52008-08-15 13:45:30 +010042ospf6_reset_mcastloop (void)
paul718e3742002-12-13 20:15:29 +000043{
44 u_int off = 0;
45 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
46 &off, sizeof (u_int)) < 0)
47 zlog_warn ("Network: reset IPV6_MULTICAST_LOOP failed: %s",
ajs6099b3b2004-11-20 02:06:59 +000048 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000049}
50
Paul Jakma7aa9dce2014-09-19 14:42:23 +010051static void
Paul Jakma6ac29a52008-08-15 13:45:30 +010052ospf6_set_pktinfo (void)
paul718e3742002-12-13 20:15:29 +000053{
paul79dc3732004-07-23 15:17:45 +000054 setsockopt_ipv6_pktinfo (ospf6_sock, 1);
paul718e3742002-12-13 20:15:29 +000055}
56
Paul Jakma7aa9dce2014-09-19 14:42:23 +010057static void
Stephen Hemminger6d0732c2011-09-28 14:23:35 +040058ospf6_set_transport_class (void)
59{
60#ifdef IPTOS_PREC_INTERNETCONTROL
61 setsockopt_ipv6_tclass (ospf6_sock, IPTOS_PREC_INTERNETCONTROL);
62#endif
63}
64
Paul Jakma7aa9dce2014-09-19 14:42:23 +010065static void
Paul Jakma6ac29a52008-08-15 13:45:30 +010066ospf6_set_checksum (void)
paul718e3742002-12-13 20:15:29 +000067{
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)
ajs6099b3b2004-11-20 02:06:59 +000072 zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000073#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
Paul Jakma6ac29a52008-08-15 13:45:30 +010080ospf6_serv_sock (void)
hasso508e53e2004-05-18 18:57:06 +000081{
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 ();
Stephen Hemminger6d0732c2011-09-28 14:23:35 +0400104 ospf6_set_transport_class ();
hasso508e53e2004-05-18 18:57:06 +0000105 ospf6_set_checksum ();
106
107 /* setup global in6_addr, allspf6 and alldr6 for later use */
108 inet_pton (AF_INET6, ALLSPFROUTERS6, &allspfrouters6);
109 inet_pton (AF_INET6, ALLDROUTERS6, &alldrouters6);
110
111 return 0;
112}
113
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400114/* ospf6 set socket option */
paul718e3742002-12-13 20:15:29 +0000115void
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400116ospf6_sso (u_int ifindex, struct in6_addr *group, int option)
hasso508e53e2004-05-18 18:57:06 +0000117{
118 struct ipv6_mreq mreq6;
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400119 int ret;
hasso508e53e2004-05-18 18:57:06 +0000120
121 assert (ifindex);
122 mreq6.ipv6mr_interface = ifindex;
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400123 memcpy (&mreq6.ipv6mr_multiaddr, group, sizeof (struct in6_addr));
hasso508e53e2004-05-18 18:57:06 +0000124
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400125 ret = setsockopt (ospf6_sock, IPPROTO_IPV6, option,
126 &mreq6, sizeof (mreq6));
127 if (ret < 0)
128 zlog_err ("Network: setsockopt (%d) on ifindex %d failed: %s",
129 option, ifindex, safe_strerror (errno));
hasso508e53e2004-05-18 18:57:06 +0000130}
131
Paul Jakma6ac29a52008-08-15 13:45:30 +0100132static int
hasso508e53e2004-05-18 18:57:06 +0000133iov_count (struct iovec *iov)
134{
135 int i;
136 for (i = 0; iov[i].iov_base; i++)
137 ;
138 return i;
139}
140
Paul Jakma6ac29a52008-08-15 13:45:30 +0100141static int
hasso508e53e2004-05-18 18:57:06 +0000142iov_totallen (struct iovec *iov)
143{
144 int i;
145 int totallen = 0;
146 for (i = 0; iov[i].iov_base; i++)
147 totallen += iov[i].iov_len;
148 return totallen;
149}
150
151int
paul718e3742002-12-13 20:15:29 +0000152ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst,
153 unsigned int *ifindex, struct iovec *message)
154{
155 int retval;
156 struct msghdr smsghdr;
157 struct cmsghdr *scmsgp;
158 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
159 struct in6_pktinfo *pktinfo;
160 struct sockaddr_in6 dst_sin6;
161
162 assert (dst);
163 assert (*ifindex);
164
165 scmsgp = (struct cmsghdr *)cmsgbuf;
166 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
167 memset (&dst_sin6, 0, sizeof (struct sockaddr_in6));
168
169 /* source address */
170 pktinfo->ipi6_ifindex = *ifindex;
171 if (src)
172 memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr));
173 else
174 memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr));
175
176 /* destination address */
177 dst_sin6.sin6_family = AF_INET6;
178#ifdef SIN6_LEN
179 dst_sin6.sin6_len = sizeof (struct sockaddr_in6);
180#endif /*SIN6_LEN*/
181 memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr));
182#ifdef HAVE_SIN6_SCOPE_ID
183 dst_sin6.sin6_scope_id = *ifindex;
184#endif
185
186 /* send control msg */
187 scmsgp->cmsg_level = IPPROTO_IPV6;
188 scmsgp->cmsg_type = IPV6_PKTINFO;
189 scmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
190 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
191
192 /* send msg hdr */
Paul Jakmacf1ce252006-05-15 10:46:07 +0000193 memset (&smsghdr, 0, sizeof (smsghdr));
paul718e3742002-12-13 20:15:29 +0000194 smsghdr.msg_iov = message;
195 smsghdr.msg_iovlen = iov_count (message);
196 smsghdr.msg_name = (caddr_t) &dst_sin6;
197 smsghdr.msg_namelen = sizeof (struct sockaddr_in6);
198 smsghdr.msg_control = (caddr_t) cmsgbuf;
Christian Franke1579a672013-03-08 02:35:38 +0100199 smsghdr.msg_controllen = scmsgp->cmsg_len;
paul718e3742002-12-13 20:15:29 +0000200
201 retval = sendmsg (ospf6_sock, &smsghdr, 0);
202 if (retval != iov_totallen (message))
hasso508e53e2004-05-18 18:57:06 +0000203 zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)",
ajs6099b3b2004-11-20 02:06:59 +0000204 *ifindex, safe_strerror (errno), errno);
hasso508e53e2004-05-18 18:57:06 +0000205
206 return retval;
paul718e3742002-12-13 20:15:29 +0000207}
208
hasso508e53e2004-05-18 18:57:06 +0000209int
paul718e3742002-12-13 20:15:29 +0000210ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst,
211 unsigned int *ifindex, struct iovec *message)
212{
213 int retval;
214 struct msghdr rmsghdr;
215 struct cmsghdr *rcmsgp;
216 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
217 struct in6_pktinfo *pktinfo;
218 struct sockaddr_in6 src_sin6;
219
220 rcmsgp = (struct cmsghdr *)cmsgbuf;
221 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
222 memset (&src_sin6, 0, sizeof (struct sockaddr_in6));
223
224 /* receive control msg */
225 rcmsgp->cmsg_level = IPPROTO_IPV6;
226 rcmsgp->cmsg_type = IPV6_PKTINFO;
227 rcmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
228 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
229
230 /* receive msg hdr */
Paul Jakmacf1ce252006-05-15 10:46:07 +0000231 memset (&rmsghdr, 0, sizeof (rmsghdr));
paul718e3742002-12-13 20:15:29 +0000232 rmsghdr.msg_iov = message;
233 rmsghdr.msg_iovlen = iov_count (message);
234 rmsghdr.msg_name = (caddr_t) &src_sin6;
235 rmsghdr.msg_namelen = sizeof (struct sockaddr_in6);
236 rmsghdr.msg_control = (caddr_t) cmsgbuf;
237 rmsghdr.msg_controllen = sizeof (cmsgbuf);
238
239 retval = recvmsg (ospf6_sock, &rmsghdr, 0);
240 if (retval < 0)
ajs6099b3b2004-11-20 02:06:59 +0000241 zlog_warn ("recvmsg failed: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000242 else if (retval == iov_totallen (message))
hasso508e53e2004-05-18 18:57:06 +0000243 zlog_warn ("recvmsg read full buffer size: %d", retval);
paul718e3742002-12-13 20:15:29 +0000244
245 /* source address */
246 assert (src);
247 memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr));
248
249 /* destination address */
250 if (ifindex)
251 *ifindex = pktinfo->ipi6_ifindex;
252 if (dst)
253 memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr));
hasso508e53e2004-05-18 18:57:06 +0000254
255 return retval;
paul718e3742002-12-13 20:15:29 +0000256}
257
paul718e3742002-12-13 20:15:29 +0000258