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