]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_network.c
d5bfdd508b10a00a294e15e0f99d10471b3cc5fd
[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
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>
23
24 #include "log.h"
25 #include "memory.h"
26 #include "sockunion.h"
27 #include "privs.h"
28
29 #include "ospf6_proto.h"
30 #include "ospf6_network.h"
31
32 extern struct zebra_privs_t ospf6d_privs;
33
34 int ospf6_sock;
35 struct in6_addr allspfrouters6;
36 struct in6_addr alldrouters6;
37
38 /* setsockopt ReUseAddr to on */
39 void
40 ospf6_set_reuseaddr ()
41 {
42 u_int on = 0;
43 if (setsockopt (ospf6_sock, SOL_SOCKET, SO_REUSEADDR, &on,
44 sizeof (u_int)) < 0)
45 zlog_warn ("Network: set SO_REUSEADDR failed: %s", strerror (errno));
46 }
47
48 /* setsockopt MulticastLoop to off */
49 void
50 ospf6_reset_mcastloop ()
51 {
52 u_int off = 0;
53 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
54 &off, sizeof (u_int)) < 0)
55 zlog_warn ("Network: reset IPV6_MULTICAST_LOOP failed: %s",
56 strerror (errno));
57 }
58
59 void
60 ospf6_set_pktinfo ()
61 {
62 setsockopt_ipv6_pktinfo (ospf6_sock, 1);
63 }
64
65 void
66 ospf6_set_checksum ()
67 {
68 int offset = 12;
69 #ifndef DISABLE_IPV6_CHECKSUM
70 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM,
71 &offset, sizeof (offset)) < 0)
72 zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", strerror (errno));
73 #else
74 zlog_warn ("Network: Don't set IPV6_CHECKSUM");
75 #endif /* DISABLE_IPV6_CHECKSUM */
76 }
77
78 /* Make ospf6d's server socket. */
79 int
80 ospf6_serv_sock ()
81 {
82 if (ospf6d_privs.change (ZPRIVS_RAISE))
83 zlog_err ("ospf6_serv_sock: could not raise privs");
84
85 ospf6_sock = socket (AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
86 if (ospf6_sock < 0)
87 {
88 zlog_warn ("Network: can't create OSPF6 socket.");
89 if (ospf6d_privs.change (ZPRIVS_LOWER))
90 zlog_err ("ospf_sock_init: could not lower privs");
91 return -1;
92 }
93 if (ospf6d_privs.change (ZPRIVS_LOWER))
94 zlog_err ("ospf_sock_init: could not lower privs");
95
96 /* set socket options */
97 #if 1
98 sockopt_reuseaddr (ospf6_sock);
99 #else
100 ospf6_set_reuseaddr ();
101 #endif /*1*/
102 ospf6_reset_mcastloop ();
103 ospf6_set_pktinfo ();
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);
109
110 return 0;
111 }
112
113 void
114 ospf6_join_allspfrouters (u_int ifindex)
115 {
116 struct ipv6_mreq mreq6;
117 int retval;
118
119 assert (ifindex);
120 mreq6.ipv6mr_interface = ifindex;
121 memcpy (&mreq6.ipv6mr_multiaddr, &allspfrouters6,
122 sizeof (struct in6_addr));
123
124 retval = setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
125 &mreq6, sizeof (mreq6));
126
127 if (retval < 0)
128 zlog_err ("Network: Join AllSPFRouters on ifindex %d failed: %s",
129 ifindex, strerror (errno));
130 #if 0
131 else
132 zlog_info ("Network: Join AllSPFRouters on ifindex %d", ifindex);
133 #endif
134 }
135
136 void
137 ospf6_leave_allspfrouters (u_int ifindex)
138 {
139 struct ipv6_mreq mreq6;
140
141 assert (ifindex);
142 mreq6.ipv6mr_interface = ifindex;
143 memcpy (&mreq6.ipv6mr_multiaddr, &allspfrouters6,
144 sizeof (struct in6_addr));
145
146 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
147 &mreq6, sizeof (mreq6)) < 0)
148 zlog_warn ("Network: Leave AllSPFRouters on ifindex %d Failed: %s",
149 ifindex, strerror (errno));
150 #if 0
151 else
152 zlog_info ("Network: Leave AllSPFRouters on ifindex %d", ifindex);
153 #endif
154 }
155
156 void
157 ospf6_join_alldrouters (u_int ifindex)
158 {
159 struct ipv6_mreq mreq6;
160
161 assert (ifindex);
162 mreq6.ipv6mr_interface = ifindex;
163 memcpy (&mreq6.ipv6mr_multiaddr, &alldrouters6,
164 sizeof (struct in6_addr));
165
166 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
167 &mreq6, sizeof (mreq6)) < 0)
168 zlog_warn ("Network: Join AllDRouters on ifindex %d Failed: %s",
169 ifindex, strerror (errno));
170 #if 0
171 else
172 zlog_info ("Network: Join AllDRouters on ifindex %d", ifindex);
173 #endif
174 }
175
176 void
177 ospf6_leave_alldrouters (u_int ifindex)
178 {
179 struct ipv6_mreq mreq6;
180
181 assert (ifindex);
182 mreq6.ipv6mr_interface = ifindex;
183 memcpy (&mreq6.ipv6mr_multiaddr, &alldrouters6,
184 sizeof (struct in6_addr));
185
186 if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
187 &mreq6, sizeof (mreq6)) < 0)
188 zlog_warn ("Network: Leave AllDRouters on ifindex %d Failed", ifindex);
189 #if 0
190 else
191 zlog_info ("Network: Leave AllDRouters on ifindex %d", ifindex);
192 #endif
193 }
194
195 int
196 iov_count (struct iovec *iov)
197 {
198 int i;
199 for (i = 0; iov[i].iov_base; i++)
200 ;
201 return i;
202 }
203
204 int
205 iov_totallen (struct iovec *iov)
206 {
207 int i;
208 int totallen = 0;
209 for (i = 0; iov[i].iov_base; i++)
210 totallen += iov[i].iov_len;
211 return totallen;
212 }
213
214 int
215 ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst,
216 unsigned int *ifindex, struct iovec *message)
217 {
218 int retval;
219 struct msghdr smsghdr;
220 struct cmsghdr *scmsgp;
221 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
222 struct in6_pktinfo *pktinfo;
223 struct sockaddr_in6 dst_sin6;
224
225 assert (dst);
226 assert (*ifindex);
227
228 scmsgp = (struct cmsghdr *)cmsgbuf;
229 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
230 memset (&dst_sin6, 0, sizeof (struct sockaddr_in6));
231
232 /* source address */
233 pktinfo->ipi6_ifindex = *ifindex;
234 if (src)
235 memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr));
236 else
237 memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr));
238
239 /* destination address */
240 dst_sin6.sin6_family = AF_INET6;
241 #ifdef SIN6_LEN
242 dst_sin6.sin6_len = sizeof (struct sockaddr_in6);
243 #endif /*SIN6_LEN*/
244 memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr));
245 #ifdef HAVE_SIN6_SCOPE_ID
246 dst_sin6.sin6_scope_id = *ifindex;
247 #endif
248
249 /* send control msg */
250 scmsgp->cmsg_level = IPPROTO_IPV6;
251 scmsgp->cmsg_type = IPV6_PKTINFO;
252 scmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
253 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
254
255 /* send msg hdr */
256 smsghdr.msg_iov = message;
257 smsghdr.msg_iovlen = iov_count (message);
258 smsghdr.msg_name = (caddr_t) &dst_sin6;
259 smsghdr.msg_namelen = sizeof (struct sockaddr_in6);
260 smsghdr.msg_control = (caddr_t) cmsgbuf;
261 smsghdr.msg_controllen = sizeof (cmsgbuf);
262
263 retval = sendmsg (ospf6_sock, &smsghdr, 0);
264 if (retval != iov_totallen (message))
265 zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)",
266 *ifindex, strerror (errno), errno);
267
268 return retval;
269 }
270
271 int
272 ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst,
273 unsigned int *ifindex, struct iovec *message)
274 {
275 int retval;
276 struct msghdr rmsghdr;
277 struct cmsghdr *rcmsgp;
278 u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
279 struct in6_pktinfo *pktinfo;
280 struct sockaddr_in6 src_sin6;
281
282 rcmsgp = (struct cmsghdr *)cmsgbuf;
283 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
284 memset (&src_sin6, 0, sizeof (struct sockaddr_in6));
285
286 /* receive control msg */
287 rcmsgp->cmsg_level = IPPROTO_IPV6;
288 rcmsgp->cmsg_type = IPV6_PKTINFO;
289 rcmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
290 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
291
292 /* receive msg hdr */
293 rmsghdr.msg_iov = message;
294 rmsghdr.msg_iovlen = iov_count (message);
295 rmsghdr.msg_name = (caddr_t) &src_sin6;
296 rmsghdr.msg_namelen = sizeof (struct sockaddr_in6);
297 rmsghdr.msg_control = (caddr_t) cmsgbuf;
298 rmsghdr.msg_controllen = sizeof (cmsgbuf);
299
300 retval = recvmsg (ospf6_sock, &rmsghdr, 0);
301 if (retval < 0)
302 zlog_warn ("recvmsg failed: %s", strerror (errno));
303 else if (retval == iov_totallen (message))
304 zlog_warn ("recvmsg read full buffer size: %d", retval);
305
306 /* source address */
307 assert (src);
308 memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr));
309
310 /* destination address */
311 if (ifindex)
312 *ifindex = pktinfo->ipi6_ifindex;
313 if (dst)
314 memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr));
315
316 return retval;
317 }
318
319