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