]> git.proxmox.com Git - mirror_frr.git/blame - ospf6d/ospf6_network.c
lib: drop off "masters" list on master_free()
[mirror_frr.git] / ospf6d / ospf6_network.c
CommitLineData
718e3744 1/*
508e53e2 2 * Copyright (C) 2003 Yasuhiro Ohara
718e3744 3 *
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 *
896014f4
DL
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 19 */
20
21#include <zebra.h>
508e53e2 22
718e3744 23#include "log.h"
508e53e2 24#include "memory.h"
718e3744 25#include "sockunion.h"
03d52f8d 26#include "sockopt.h"
edd7c245 27#include "privs.h"
718e3744 28
8551e6da 29#include "libospf.h"
718e3744 30#include "ospf6_proto.h"
508e53e2 31#include "ospf6_network.h"
718e3744 32
edd7c245 33extern struct zebra_privs_t ospf6d_privs;
718e3744 34
508e53e2 35int ospf6_sock;
36struct in6_addr allspfrouters6;
37struct in6_addr alldrouters6;
718e3744 38
508e53e2 39/* setsockopt MulticastLoop to off */
1f9a9fff 40static void
6ac29a51 41ospf6_reset_mcastloop (void)
718e3744 42{
508e53e2 43 u_int off = 0;
44 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
45 &off, sizeof (u_int)) < 0)
46 zlog_warn ("Network: reset IPV6_MULTICAST_LOOP failed: %s",
6099b3b5 47 safe_strerror (errno));
718e3744 48}
49
1f9a9fff 50static void
6ac29a51 51ospf6_set_pktinfo (void)
718e3744 52{
79dc373a 53 setsockopt_ipv6_pktinfo (ospf6_sock, 1);
718e3744 54}
55
ed2eb093 56static void
6d0732c8
SH
57ospf6_set_transport_class (void)
58{
59#ifdef IPTOS_PREC_INTERNETCONTROL
60 setsockopt_ipv6_tclass (ospf6_sock, IPTOS_PREC_INTERNETCONTROL);
61#endif
62}
63
1f9a9fff 64static void
6ac29a51 65ospf6_set_checksum (void)
718e3744 66{
508e53e2 67 int offset = 12;
68#ifndef DISABLE_IPV6_CHECKSUM
69 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM,
70 &offset, sizeof (offset)) < 0)
6099b3b5 71 zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", safe_strerror (errno));
508e53e2 72#else
73 zlog_warn ("Network: Don't set IPV6_CHECKSUM");
74#endif /* DISABLE_IPV6_CHECKSUM */
718e3744 75}
76
718e3744 77/* Make ospf6d's server socket. */
78int
6ac29a51 79ospf6_serv_sock (void)
718e3744 80{
edd7c245 81 if (ospf6d_privs.change (ZPRIVS_RAISE))
508e53e2 82 zlog_err ("ospf6_serv_sock: could not raise privs");
83
718e3744 84 ospf6_sock = socket (AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
85 if (ospf6_sock < 0)
86 {
87 zlog_warn ("Network: can't create OSPF6 socket.");
508e53e2 88 if (ospf6d_privs.change (ZPRIVS_LOWER))
89 zlog_err ("ospf_sock_init: could not lower privs");
718e3744 90 return -1;
91 }
edd7c245 92 if (ospf6d_privs.change (ZPRIVS_LOWER))
93 zlog_err ("ospf_sock_init: could not lower privs");
508e53e2 94
95 /* set socket options */
96#if 1
97 sockopt_reuseaddr (ospf6_sock);
98#else
99 ospf6_set_reuseaddr ();
100#endif /*1*/
101 ospf6_reset_mcastloop ();
102 ospf6_set_pktinfo ();
6d0732c8 103 ospf6_set_transport_class ();
508e53e2 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);
718e3744 109
110 return 0;
111}
112
9a9446ea 113/* ospf6 set socket option */
8e370bfa 114int
b892f1dd 115ospf6_sso (ifindex_t ifindex, struct in6_addr *group, int option)
718e3744 116{
117 struct ipv6_mreq mreq6;
9a9446ea 118 int ret;
60e42c52 119 int bufsize = (8 * 1024 * 1024);
718e3744 120
121 assert (ifindex);
122 mreq6.ipv6mr_interface = ifindex;
9a9446ea 123 memcpy (&mreq6.ipv6mr_multiaddr, group, sizeof (struct in6_addr));
718e3744 124
9a9446ea
VT
125 ret = setsockopt (ospf6_sock, IPPROTO_IPV6, option,
126 &mreq6, sizeof (mreq6));
127 if (ret < 0)
8e370bfa
DS
128 {
129 zlog_err ("Network: setsockopt (%d) on ifindex %d failed: %s",
130 option, ifindex, safe_strerror (errno));
131 return ret;
132 }
60e42c52 133
6228a3b8
RW
134 setsockopt_so_sendbuf (ospf6_sock, bufsize);
135 setsockopt_so_recvbuf (ospf6_sock, bufsize);
8e370bfa
DS
136
137 return 0;
718e3744 138}
139
6ac29a51 140static int
508e53e2 141iov_count (struct iovec *iov)
718e3744 142{
508e53e2 143 int i;
144 for (i = 0; iov[i].iov_base; i++)
145 ;
146 return i;
718e3744 147}
148
6ac29a51 149static int
508e53e2 150iov_totallen (struct iovec *iov)
718e3744 151{
508e53e2 152 int i;
153 int totallen = 0;
154 for (i = 0; iov[i].iov_base; i++)
155 totallen += iov[i].iov_len;
156 return totallen;
718e3744 157}
158
508e53e2 159int
718e3744 160ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst,
b892f1dd 161 ifindex_t *ifindex, struct iovec *message)
718e3744 162{
163 int retval;
164 struct msghdr smsghdr;
165 struct cmsghdr *scmsgp;
288b91e0
RW
166 union
167 {
168 struct cmsghdr hdr;
169 u_char buf[CMSG_SPACE (sizeof (struct in6_pktinfo))];
170 } cmsgbuf;
718e3744 171 struct in6_pktinfo *pktinfo;
172 struct sockaddr_in6 dst_sin6;
173
174 assert (dst);
175 assert (*ifindex);
176
288b91e0 177 scmsgp = (struct cmsghdr *)&cmsgbuf;
718e3744 178 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
179 memset (&dst_sin6, 0, sizeof (struct sockaddr_in6));
180
181 /* source address */
182 pktinfo->ipi6_ifindex = *ifindex;
183 if (src)
184 memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr));
185 else
186 memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr));
187
188 /* destination address */
189 dst_sin6.sin6_family = AF_INET6;
190#ifdef SIN6_LEN
191 dst_sin6.sin6_len = sizeof (struct sockaddr_in6);
192#endif /*SIN6_LEN*/
193 memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr));
718e3744 194 dst_sin6.sin6_scope_id = *ifindex;
718e3744 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 */
cf1ce250 203 memset (&smsghdr, 0, sizeof (smsghdr));
718e3744 204 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);
288b91e0
RW
208 smsghdr.msg_control = (caddr_t) &cmsgbuf.buf;
209 smsghdr.msg_controllen = sizeof(cmsgbuf.buf);
718e3744 210
211 retval = sendmsg (ospf6_sock, &smsghdr, 0);
212 if (retval != iov_totallen (message))
508e53e2 213 zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)",
6099b3b5 214 *ifindex, safe_strerror (errno), errno);
508e53e2 215
216 return retval;
718e3744 217}
218
508e53e2 219int
718e3744 220ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst,
b892f1dd 221 ifindex_t *ifindex, struct iovec *message)
718e3744 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 */
cf1ce250 241 memset (&rmsghdr, 0, sizeof (rmsghdr));
718e3744 242 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)
6099b3b5 251 zlog_warn ("recvmsg failed: %s", safe_strerror (errno));
718e3744 252 else if (retval == iov_totallen (message))
508e53e2 253 zlog_warn ("recvmsg read full buffer size: %d", retval);
718e3744 254
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));
718e3744 264
508e53e2 265 return retval;
718e3744 266}
267
508e53e2 268