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