blob: 74cfbec7c7f8efed2335ec61406ceb8d26de145a [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
40/* setsockopt ReUseAddr to on */
41void
Paul Jakma6ac29a52008-08-15 13:45:30 +010042ospf6_set_reuseaddr (void)
paul718e3742002-12-13 20:15:29 +000043{
44 u_int on = 0;
45 if (setsockopt (ospf6_sock, SOL_SOCKET, SO_REUSEADDR, &on,
46 sizeof (u_int)) < 0)
ajs6099b3b2004-11-20 02:06:59 +000047 zlog_warn ("Network: set SO_REUSEADDR failed: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000048}
49
50/* setsockopt MulticastLoop to off */
51void
Paul Jakma6ac29a52008-08-15 13:45:30 +010052ospf6_reset_mcastloop (void)
paul718e3742002-12-13 20:15:29 +000053{
54 u_int off = 0;
55 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
56 &off, sizeof (u_int)) < 0)
57 zlog_warn ("Network: reset IPV6_MULTICAST_LOOP failed: %s",
ajs6099b3b2004-11-20 02:06:59 +000058 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000059}
60
61void
Paul Jakma6ac29a52008-08-15 13:45:30 +010062ospf6_set_pktinfo (void)
paul718e3742002-12-13 20:15:29 +000063{
paul79dc3732004-07-23 15:17:45 +000064 setsockopt_ipv6_pktinfo (ospf6_sock, 1);
paul718e3742002-12-13 20:15:29 +000065}
66
67void
Stephen Hemminger6d0732c2011-09-28 14:23:35 +040068ospf6_set_transport_class (void)
69{
70#ifdef IPTOS_PREC_INTERNETCONTROL
71 setsockopt_ipv6_tclass (ospf6_sock, IPTOS_PREC_INTERNETCONTROL);
72#endif
73}
74
75void
Paul Jakma6ac29a52008-08-15 13:45:30 +010076ospf6_set_checksum (void)
paul718e3742002-12-13 20:15:29 +000077{
78 int offset = 12;
hasso508e53e2004-05-18 18:57:06 +000079#ifndef DISABLE_IPV6_CHECKSUM
paul718e3742002-12-13 20:15:29 +000080 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM,
81 &offset, sizeof (offset)) < 0)
ajs6099b3b2004-11-20 02:06:59 +000082 zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +000083#else
84 zlog_warn ("Network: Don't set IPV6_CHECKSUM");
85#endif /* DISABLE_IPV6_CHECKSUM */
86}
87
hasso508e53e2004-05-18 18:57:06 +000088/* Make ospf6d's server socket. */
89int
Paul Jakma6ac29a52008-08-15 13:45:30 +010090ospf6_serv_sock (void)
hasso508e53e2004-05-18 18:57:06 +000091{
92 if (ospf6d_privs.change (ZPRIVS_RAISE))
93 zlog_err ("ospf6_serv_sock: could not raise privs");
94
95 ospf6_sock = socket (AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
96 if (ospf6_sock < 0)
97 {
98 zlog_warn ("Network: can't create OSPF6 socket.");
99 if (ospf6d_privs.change (ZPRIVS_LOWER))
100 zlog_err ("ospf_sock_init: could not lower privs");
101 return -1;
102 }
103 if (ospf6d_privs.change (ZPRIVS_LOWER))
104 zlog_err ("ospf_sock_init: could not lower privs");
105
106 /* set socket options */
107#if 1
108 sockopt_reuseaddr (ospf6_sock);
109#else
110 ospf6_set_reuseaddr ();
111#endif /*1*/
112 ospf6_reset_mcastloop ();
113 ospf6_set_pktinfo ();
Stephen Hemminger6d0732c2011-09-28 14:23:35 +0400114 ospf6_set_transport_class ();
hasso508e53e2004-05-18 18:57:06 +0000115 ospf6_set_checksum ();
116
117 /* setup global in6_addr, allspf6 and alldr6 for later use */
118 inet_pton (AF_INET6, ALLSPFROUTERS6, &allspfrouters6);
119 inet_pton (AF_INET6, ALLDROUTERS6, &alldrouters6);
120
121 return 0;
122}
123
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400124/* ospf6 set socket option */
paul718e3742002-12-13 20:15:29 +0000125void
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400126ospf6_sso (u_int ifindex, struct in6_addr *group, int option)
hasso508e53e2004-05-18 18:57:06 +0000127{
128 struct ipv6_mreq mreq6;
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400129 int ret;
hasso508e53e2004-05-18 18:57:06 +0000130
131 assert (ifindex);
132 mreq6.ipv6mr_interface = ifindex;
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400133 memcpy (&mreq6.ipv6mr_multiaddr, group, sizeof (struct in6_addr));
hasso508e53e2004-05-18 18:57:06 +0000134
Vyacheslav Trushkin9a9446e2011-11-21 20:26:39 +0400135 ret = setsockopt (ospf6_sock, IPPROTO_IPV6, option,
136 &mreq6, sizeof (mreq6));
137 if (ret < 0)
138 zlog_err ("Network: setsockopt (%d) on ifindex %d failed: %s",
139 option, ifindex, safe_strerror (errno));
hasso508e53e2004-05-18 18:57:06 +0000140}
141
Paul Jakma6ac29a52008-08-15 13:45:30 +0100142static int
hasso508e53e2004-05-18 18:57:06 +0000143iov_count (struct iovec *iov)
144{
145 int i;
146 for (i = 0; iov[i].iov_base; i++)
147 ;
148 return i;
149}
150
Paul Jakma6ac29a52008-08-15 13:45:30 +0100151static int
hasso508e53e2004-05-18 18:57:06 +0000152iov_totallen (struct iovec *iov)
153{
154 int i;
155 int totallen = 0;
156 for (i = 0; iov[i].iov_base; i++)
157 totallen += iov[i].iov_len;
158 return totallen;
159}
160
161int
paul718e3742002-12-13 20:15:29 +0000162ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst,
163 unsigned int *ifindex, struct iovec *message)
164{
165 int retval;
166 struct msghdr smsghdr;
167 struct cmsghdr *scmsgp;
168 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
169 struct in6_pktinfo *pktinfo;
170 struct sockaddr_in6 dst_sin6;
171
172 assert (dst);
173 assert (*ifindex);
174
175 scmsgp = (struct cmsghdr *)cmsgbuf;
176 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
177 memset (&dst_sin6, 0, sizeof (struct sockaddr_in6));
178
179 /* source address */
180 pktinfo->ipi6_ifindex = *ifindex;
181 if (src)
182 memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr));
183 else
184 memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr));
185
186 /* destination address */
187 dst_sin6.sin6_family = AF_INET6;
188#ifdef SIN6_LEN
189 dst_sin6.sin6_len = sizeof (struct sockaddr_in6);
190#endif /*SIN6_LEN*/
191 memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr));
192#ifdef HAVE_SIN6_SCOPE_ID
193 dst_sin6.sin6_scope_id = *ifindex;
194#endif
195
196 /* send control msg */
197 scmsgp->cmsg_level = IPPROTO_IPV6;
198 scmsgp->cmsg_type = IPV6_PKTINFO;
199 scmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
200 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
201
202 /* send msg hdr */
Paul Jakmacf1ce252006-05-15 10:46:07 +0000203 memset (&smsghdr, 0, sizeof (smsghdr));
paul718e3742002-12-13 20:15:29 +0000204 smsghdr.msg_iov = message;
205 smsghdr.msg_iovlen = iov_count (message);
206 smsghdr.msg_name = (caddr_t) &dst_sin6;
207 smsghdr.msg_namelen = sizeof (struct sockaddr_in6);
208 smsghdr.msg_control = (caddr_t) cmsgbuf;
Christian Franke1579a672013-03-08 02:35:38 +0100209 smsghdr.msg_controllen = scmsgp->cmsg_len;
paul718e3742002-12-13 20:15:29 +0000210
211 retval = sendmsg (ospf6_sock, &smsghdr, 0);
212 if (retval != iov_totallen (message))
hasso508e53e2004-05-18 18:57:06 +0000213 zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)",
ajs6099b3b2004-11-20 02:06:59 +0000214 *ifindex, safe_strerror (errno), errno);
hasso508e53e2004-05-18 18:57:06 +0000215
216 return retval;
paul718e3742002-12-13 20:15:29 +0000217}
218
hasso508e53e2004-05-18 18:57:06 +0000219int
paul718e3742002-12-13 20:15:29 +0000220ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst,
221 unsigned int *ifindex, struct iovec *message)
222{
223 int retval;
224 struct msghdr rmsghdr;
225 struct cmsghdr *rcmsgp;
226 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
227 struct in6_pktinfo *pktinfo;
228 struct sockaddr_in6 src_sin6;
229
230 rcmsgp = (struct cmsghdr *)cmsgbuf;
231 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
232 memset (&src_sin6, 0, sizeof (struct sockaddr_in6));
233
234 /* receive control msg */
235 rcmsgp->cmsg_level = IPPROTO_IPV6;
236 rcmsgp->cmsg_type = IPV6_PKTINFO;
237 rcmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
238 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
239
240 /* receive msg hdr */
Paul Jakmacf1ce252006-05-15 10:46:07 +0000241 memset (&rmsghdr, 0, sizeof (rmsghdr));
paul718e3742002-12-13 20:15:29 +0000242 rmsghdr.msg_iov = message;
243 rmsghdr.msg_iovlen = iov_count (message);
244 rmsghdr.msg_name = (caddr_t) &src_sin6;
245 rmsghdr.msg_namelen = sizeof (struct sockaddr_in6);
246 rmsghdr.msg_control = (caddr_t) cmsgbuf;
247 rmsghdr.msg_controllen = sizeof (cmsgbuf);
248
249 retval = recvmsg (ospf6_sock, &rmsghdr, 0);
250 if (retval < 0)
ajs6099b3b2004-11-20 02:06:59 +0000251 zlog_warn ("recvmsg failed: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000252 else if (retval == iov_totallen (message))
hasso508e53e2004-05-18 18:57:06 +0000253 zlog_warn ("recvmsg read full buffer size: %d", retval);
paul718e3742002-12-13 20:15:29 +0000254
255 /* source address */
256 assert (src);
257 memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr));
258
259 /* destination address */
260 if (ifindex)
261 *ifindex = pktinfo->ipi6_ifindex;
262 if (dst)
263 memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr));
hasso508e53e2004-05-18 18:57:06 +0000264
265 return retval;
paul718e3742002-12-13 20:15:29 +0000266}
267
paul718e3742002-12-13 20:15:29 +0000268