]> git.proxmox.com Git - mirror_frr.git/blame - ospf6d/ospf6_network.c
*: reindent
[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
d62a17ae 35int ospf6_sock;
508e53e2 36struct in6_addr allspfrouters6;
37struct in6_addr alldrouters6;
718e3744 38
508e53e2 39/* setsockopt MulticastLoop to off */
d62a17ae 40static void ospf6_reset_mcastloop(void)
718e3744 41{
d62a17ae 42 u_int off = 0;
43 if (setsockopt(ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off,
44 sizeof(u_int))
45 < 0)
46 zlog_warn("Network: reset IPV6_MULTICAST_LOOP failed: %s",
47 safe_strerror(errno));
718e3744 48}
49
d62a17ae 50static void ospf6_set_pktinfo(void)
718e3744 51{
d62a17ae 52 setsockopt_ipv6_pktinfo(ospf6_sock, 1);
718e3744 53}
54
d62a17ae 55static void ospf6_set_transport_class(void)
6d0732c8
SH
56{
57#ifdef IPTOS_PREC_INTERNETCONTROL
d62a17ae 58 setsockopt_ipv6_tclass(ospf6_sock, IPTOS_PREC_INTERNETCONTROL);
6d0732c8
SH
59#endif
60}
61
d62a17ae 62static void ospf6_set_checksum(void)
718e3744 63{
d62a17ae 64 int offset = 12;
508e53e2 65#ifndef DISABLE_IPV6_CHECKSUM
d62a17ae 66 if (setsockopt(ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM, &offset,
67 sizeof(offset))
68 < 0)
69 zlog_warn("Network: set IPV6_CHECKSUM failed: %s",
70 safe_strerror(errno));
508e53e2 71#else
d62a17ae 72 zlog_warn("Network: Don't set IPV6_CHECKSUM");
508e53e2 73#endif /* DISABLE_IPV6_CHECKSUM */
718e3744 74}
75
718e3744 76/* Make ospf6d's server socket. */
d62a17ae 77int ospf6_serv_sock(void)
718e3744 78{
d62a17ae 79 if (ospf6d_privs.change(ZPRIVS_RAISE))
80 zlog_err("ospf6_serv_sock: could not raise privs");
81
82 ospf6_sock = socket(AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
83 if (ospf6_sock < 0) {
84 zlog_warn("Network: can't create OSPF6 socket.");
85 if (ospf6d_privs.change(ZPRIVS_LOWER))
86 zlog_err("ospf_sock_init: could not lower privs");
87 return -1;
88 }
89 if (ospf6d_privs.change(ZPRIVS_LOWER))
90 zlog_err("ospf_sock_init: could not lower privs");
91
92/* set socket options */
508e53e2 93#if 1
d62a17ae 94 sockopt_reuseaddr(ospf6_sock);
508e53e2 95#else
d62a17ae 96 ospf6_set_reuseaddr();
508e53e2 97#endif /*1*/
d62a17ae 98 ospf6_reset_mcastloop();
99 ospf6_set_pktinfo();
100 ospf6_set_transport_class();
101 ospf6_set_checksum();
508e53e2 102
d62a17ae 103 /* setup global in6_addr, allspf6 and alldr6 for later use */
104 inet_pton(AF_INET6, ALLSPFROUTERS6, &allspfrouters6);
105 inet_pton(AF_INET6, ALLDROUTERS6, &alldrouters6);
718e3744 106
d62a17ae 107 return 0;
718e3744 108}
109
9a9446ea 110/* ospf6 set socket option */
d62a17ae 111int ospf6_sso(ifindex_t ifindex, struct in6_addr *group, int option)
718e3744 112{
d62a17ae 113 struct ipv6_mreq mreq6;
114 int ret;
115 int bufsize = (8 * 1024 * 1024);
116
117 assert(ifindex);
118 mreq6.ipv6mr_interface = ifindex;
119 memcpy(&mreq6.ipv6mr_multiaddr, group, sizeof(struct in6_addr));
120
121 ret = setsockopt(ospf6_sock, IPPROTO_IPV6, option, &mreq6,
122 sizeof(mreq6));
123 if (ret < 0) {
124 zlog_err("Network: setsockopt (%d) on ifindex %d failed: %s",
125 option, ifindex, safe_strerror(errno));
126 return ret;
127 }
128
129 setsockopt_so_sendbuf(ospf6_sock, bufsize);
130 setsockopt_so_recvbuf(ospf6_sock, bufsize);
131
132 return 0;
718e3744 133}
134
d62a17ae 135static int iov_count(struct iovec *iov)
718e3744 136{
d62a17ae 137 int i;
138 for (i = 0; iov[i].iov_base; i++)
139 ;
140 return i;
718e3744 141}
142
d62a17ae 143static int iov_totallen(struct iovec *iov)
718e3744 144{
d62a17ae 145 int i;
146 int totallen = 0;
147 for (i = 0; iov[i].iov_base; i++)
148 totallen += iov[i].iov_len;
149 return totallen;
718e3744 150}
151
d62a17ae 152int ospf6_sendmsg(struct in6_addr *src, struct in6_addr *dst,
153 ifindex_t *ifindex, struct iovec *message)
718e3744 154{
d62a17ae 155 int retval;
156 struct msghdr smsghdr;
157 struct cmsghdr *scmsgp;
158 union {
159 struct cmsghdr hdr;
160 u_char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
161 } cmsgbuf;
162 struct in6_pktinfo *pktinfo;
163 struct sockaddr_in6 dst_sin6;
164
165 assert(dst);
166 assert(*ifindex);
167
168 scmsgp = (struct cmsghdr *)&cmsgbuf;
169 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
170 memset(&dst_sin6, 0, sizeof(struct sockaddr_in6));
171
172 /* source address */
173 pktinfo->ipi6_ifindex = *ifindex;
174 if (src)
175 memcpy(&pktinfo->ipi6_addr, src, sizeof(struct in6_addr));
176 else
177 memset(&pktinfo->ipi6_addr, 0, sizeof(struct in6_addr));
178
179 /* destination address */
180 dst_sin6.sin6_family = AF_INET6;
718e3744 181#ifdef SIN6_LEN
d62a17ae 182 dst_sin6.sin6_len = sizeof(struct sockaddr_in6);
718e3744 183#endif /*SIN6_LEN*/
d62a17ae 184 memcpy(&dst_sin6.sin6_addr, dst, sizeof(struct in6_addr));
185 dst_sin6.sin6_scope_id = *ifindex;
186
187 /* send control msg */
188 scmsgp->cmsg_level = IPPROTO_IPV6;
189 scmsgp->cmsg_type = IPV6_PKTINFO;
190 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
191 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
192
193 /* send msg hdr */
194 memset(&smsghdr, 0, sizeof(smsghdr));
195 smsghdr.msg_iov = message;
196 smsghdr.msg_iovlen = iov_count(message);
197 smsghdr.msg_name = (caddr_t)&dst_sin6;
198 smsghdr.msg_namelen = sizeof(struct sockaddr_in6);
199 smsghdr.msg_control = (caddr_t)&cmsgbuf.buf;
200 smsghdr.msg_controllen = sizeof(cmsgbuf.buf);
201
202 retval = sendmsg(ospf6_sock, &smsghdr, 0);
203 if (retval != iov_totallen(message))
204 zlog_warn("sendmsg failed: ifindex: %d: %s (%d)", *ifindex,
205 safe_strerror(errno), errno);
206
207 return retval;
718e3744 208}
209
d62a17ae 210int ospf6_recvmsg(struct in6_addr *src, struct in6_addr *dst,
211 ifindex_t *ifindex, struct iovec *message)
718e3744 212{
d62a17ae 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 */
231 memset(&rmsghdr, 0, sizeof(rmsghdr));
232 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)
241 zlog_warn("recvmsg failed: %s", safe_strerror(errno));
242 else if (retval == iov_totallen(message))
243 zlog_warn("recvmsg read full buffer size: %d", retval);
244
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));
254
255 return retval;
718e3744 256}