]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_network.c
Merge pull request #5196 from kuldeepkash/testcases
[mirror_frr.git] / ospf6d / ospf6_network.c
1 /*
2 * Copyright (C) 2003 Yasuhiro Ohara
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 *
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
19 */
20
21 #include <zebra.h>
22
23 #include "log.h"
24 #include "memory.h"
25 #include "sockunion.h"
26 #include "sockopt.h"
27 #include "privs.h"
28 #include "lib_errors.h"
29
30 #include "libospf.h"
31 #include "ospf6_proto.h"
32 #include "ospf6_network.h"
33 #include "ospf6d.h"
34
35 int ospf6_sock;
36 struct in6_addr allspfrouters6;
37 struct in6_addr alldrouters6;
38
39 /* setsockopt MulticastLoop to off */
40 static void ospf6_reset_mcastloop(void)
41 {
42 unsigned int off = 0;
43 if (setsockopt(ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off,
44 sizeof(unsigned int))
45 < 0)
46 zlog_warn("Network: reset IPV6_MULTICAST_LOOP failed: %s",
47 safe_strerror(errno));
48 }
49
50 static void ospf6_set_pktinfo(void)
51 {
52 setsockopt_ipv6_pktinfo(ospf6_sock, 1);
53 }
54
55 static void ospf6_set_transport_class(void)
56 {
57 #ifdef IPTOS_PREC_INTERNETCONTROL
58 setsockopt_ipv6_tclass(ospf6_sock, IPTOS_PREC_INTERNETCONTROL);
59 #endif
60 }
61
62 static void ospf6_set_checksum(void)
63 {
64 int offset = 12;
65 #ifndef DISABLE_IPV6_CHECKSUM
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));
71 #else
72 zlog_warn("Network: Don't set IPV6_CHECKSUM");
73 #endif /* DISABLE_IPV6_CHECKSUM */
74 }
75
76 void ospf6_serv_close(void)
77 {
78 if (ospf6_sock > 0) {
79 close(ospf6_sock);
80 ospf6_sock = -1;
81 return;
82 }
83 }
84
85 /* Make ospf6d's server socket. */
86 int ospf6_serv_sock(void)
87 {
88 frr_with_privs(&ospf6d_privs) {
89
90 ospf6_sock = socket(AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
91 if (ospf6_sock < 0) {
92 zlog_warn("Network: can't create OSPF6 socket.");
93 return -1;
94 }
95 }
96
97 /* set socket options */
98 #if 1
99 sockopt_reuseaddr(ospf6_sock);
100 #else
101 ospf6_set_reuseaddr();
102 #endif /*1*/
103 ospf6_reset_mcastloop();
104 ospf6_set_pktinfo();
105 ospf6_set_transport_class();
106 ospf6_set_checksum();
107
108 /* setup global in6_addr, allspf6 and alldr6 for later use */
109 inet_pton(AF_INET6, ALLSPFROUTERS6, &allspfrouters6);
110 inet_pton(AF_INET6, ALLDROUTERS6, &alldrouters6);
111
112 return 0;
113 }
114
115 /* ospf6 set socket option */
116 int ospf6_sso(ifindex_t ifindex, struct in6_addr *group, int option)
117 {
118 struct ipv6_mreq mreq6;
119 int ret;
120 int bufsize = (8 * 1024 * 1024);
121
122 assert(ifindex);
123 mreq6.ipv6mr_interface = ifindex;
124 memcpy(&mreq6.ipv6mr_multiaddr, group, sizeof(struct in6_addr));
125
126 ret = setsockopt(ospf6_sock, IPPROTO_IPV6, option, &mreq6,
127 sizeof(mreq6));
128 if (ret < 0) {
129 flog_err_sys(
130 EC_LIB_SOCKET,
131 "Network: setsockopt (%d) on ifindex %d failed: %s",
132 option, ifindex, safe_strerror(errno));
133 return ret;
134 }
135
136 setsockopt_so_sendbuf(ospf6_sock, bufsize);
137 setsockopt_so_recvbuf(ospf6_sock, bufsize);
138
139 return 0;
140 }
141
142 static int iov_count(struct iovec *iov)
143 {
144 int i;
145 for (i = 0; iov[i].iov_base; i++)
146 ;
147 return i;
148 }
149
150 static int iov_totallen(struct iovec *iov)
151 {
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;
157 }
158
159 int ospf6_sendmsg(struct in6_addr *src, struct in6_addr *dst,
160 ifindex_t *ifindex, struct iovec *message)
161 {
162 int retval;
163 struct msghdr smsghdr;
164 struct cmsghdr *scmsgp;
165 union {
166 struct cmsghdr hdr;
167 uint8_t buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
168 } cmsgbuf;
169 struct in6_pktinfo *pktinfo;
170 struct sockaddr_in6 dst_sin6;
171
172 assert(dst);
173 assert(*ifindex);
174
175 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
176 scmsgp = (struct cmsghdr *)&cmsgbuf;
177 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
178 memset(&dst_sin6, 0, sizeof(struct sockaddr_in6));
179
180 /* source address */
181 pktinfo->ipi6_ifindex = *ifindex;
182 if (src)
183 memcpy(&pktinfo->ipi6_addr, src, sizeof(struct in6_addr));
184 else
185 memset(&pktinfo->ipi6_addr, 0, sizeof(struct in6_addr));
186
187 /* destination address */
188 dst_sin6.sin6_family = AF_INET6;
189 #ifdef SIN6_LEN
190 dst_sin6.sin6_len = sizeof(struct sockaddr_in6);
191 #endif /*SIN6_LEN*/
192 memcpy(&dst_sin6.sin6_addr, dst, sizeof(struct in6_addr));
193 dst_sin6.sin6_scope_id = *ifindex;
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 */
202 memset(&smsghdr, 0, sizeof(smsghdr));
203 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.buf;
208 smsghdr.msg_controllen = sizeof(cmsgbuf.buf);
209
210 retval = sendmsg(ospf6_sock, &smsghdr, 0);
211 if (retval != iov_totallen(message))
212 zlog_warn("sendmsg failed: ifindex: %d: %s (%d)", *ifindex,
213 safe_strerror(errno), errno);
214
215 return retval;
216 }
217
218 int ospf6_recvmsg(struct in6_addr *src, struct in6_addr *dst,
219 ifindex_t *ifindex, struct iovec *message)
220 {
221 int retval;
222 struct msghdr rmsghdr;
223 struct cmsghdr *rcmsgp;
224 uint8_t cmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
225 struct in6_pktinfo *pktinfo;
226 struct sockaddr_in6 src_sin6;
227
228 rcmsgp = (struct cmsghdr *)cmsgbuf;
229 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
230 memset(&src_sin6, 0, sizeof(struct sockaddr_in6));
231
232 /* receive control msg */
233 rcmsgp->cmsg_level = IPPROTO_IPV6;
234 rcmsgp->cmsg_type = IPV6_PKTINFO;
235 rcmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
236 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
237
238 /* receive msg hdr */
239 memset(&rmsghdr, 0, sizeof(rmsghdr));
240 rmsghdr.msg_iov = message;
241 rmsghdr.msg_iovlen = iov_count(message);
242 rmsghdr.msg_name = (caddr_t)&src_sin6;
243 rmsghdr.msg_namelen = sizeof(struct sockaddr_in6);
244 rmsghdr.msg_control = (caddr_t)cmsgbuf;
245 rmsghdr.msg_controllen = sizeof(cmsgbuf);
246
247 retval = recvmsg(ospf6_sock, &rmsghdr, 0);
248 if (retval < 0)
249 zlog_warn("recvmsg failed: %s", safe_strerror(errno));
250 else if (retval == iov_totallen(message))
251 zlog_warn("recvmsg read full buffer size: %d", retval);
252
253 /* source address */
254 assert(src);
255 memcpy(src, &src_sin6.sin6_addr, sizeof(struct in6_addr));
256
257 /* destination address */
258 if (ifindex)
259 *ifindex = pktinfo->ipi6_ifindex;
260 if (dst)
261 memcpy(dst, &pktinfo->ipi6_addr, sizeof(struct in6_addr));
262
263 return retval;
264 }