blob: 8e6c559a8c99d21379bc54f22d53aea4ea2d3804 [file] [log] [blame]
Everton Marques871dbcf2009-08-11 15:43:05 -03001/*
2 PIM for Quagga
3 Copyright (C) 2008 Everton da Silva Marques
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 MA 02110-1301 USA
19
20 $QuaggaId: $Format:%an, %ai, %h$ $
21*/
22
23#include "pim_mroute.h"
24
25#include <sys/types.h>
26#include <sys/socket.h>
27#include <netinet/in.h>
28#include <netinet/igmp.h>
29#include <arpa/inet.h>
30#include <unistd.h>
31#include <netdb.h>
32#include <errno.h>
33
34#include <zebra.h>
35#include "log.h"
Leonard Herve596470f2009-08-11 15:45:26 -030036#include "privs.h"
Everton Marques871dbcf2009-08-11 15:43:05 -030037
38#include "pimd.h"
39#include "pim_sock.h"
40#include "pim_str.h"
41
Leonard Herve596470f2009-08-11 15:45:26 -030042/* GLOBAL VARS */
43extern struct zebra_privs_t pimd_privs;
44
Everton Marques871dbcf2009-08-11 15:43:05 -030045#ifndef MCAST_JOIN_SOURCE_GROUP
46#define MCAST_JOIN_SOURCE_GROUP 46
47struct group_source_req
48{
49 uint32_t gsr_interface;
50 struct sockaddr_storage gsr_group;
51 struct sockaddr_storage gsr_source;
52};
53#endif
54
55int pim_socket_raw(int protocol)
56{
57 int fd;
58
Leonard Herve596470f2009-08-11 15:45:26 -030059 if ( pimd_privs.change (ZPRIVS_RAISE) )
60 zlog_err ("pim_sockek_raw: could not raise privs, %s",
61 safe_strerror (errno) );
62
Everton Marques871dbcf2009-08-11 15:43:05 -030063 fd = socket(AF_INET, SOCK_RAW, protocol);
Leonard Herve596470f2009-08-11 15:45:26 -030064
65 if ( pimd_privs.change (ZPRIVS_LOWER) )
66 zlog_err ("pim_socket_raw: could not lower privs, %s",
67 safe_strerror (errno) );
68
Everton Marques871dbcf2009-08-11 15:43:05 -030069 if (fd < 0) {
70 zlog_warn("Could not create raw socket: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -030071 errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -030072 return PIM_SOCK_ERR_SOCKET;
73 }
74
75 return fd;
76}
77
78int pim_socket_mcast(int protocol, struct in_addr ifaddr, int loop)
79{
80 int fd;
81
82 fd = pim_socket_raw(protocol);
83 if (fd < 0) {
84 zlog_warn("Could not create multicast socket: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -030085 errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -030086 return PIM_SOCK_ERR_SOCKET;
87 }
88
89 /* Needed to obtain destination address from recvmsg() */
90 {
91#if defined(HAVE_IP_PKTINFO)
92 /* Linux IP_PKTINFO */
93 int opt = 1;
94 if (setsockopt(fd, SOL_IP, IP_PKTINFO, &opt, sizeof(opt))) {
95 zlog_warn("Could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -030096 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -030097 }
98#elif defined(HAVE_IP_RECVDSTADDR)
99 /* BSD IP_RECVDSTADDR */
100 int opt = 1;
101 if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt))) {
102 zlog_warn("Could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300103 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300104 }
105#else
106 zlog_err("%s %s: Missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
107 __FILE__, __PRETTY_FUNCTION__);
108 close(fd);
109 return PIM_SOCK_ERR_DSTADDR;
110#endif
111 }
112
113
114 /* Set router alert (RFC 2113) */
115 {
116 char ra[4];
117 ra[0] = 148;
118 ra[1] = 4;
119 ra[2] = 0;
120 ra[3] = 0;
121 if (setsockopt(fd, IPPROTO_IP, IP_OPTIONS, ra, 4)) {
122 zlog_warn("Could not set Router Alert Option on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300123 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300124 close(fd);
125 return PIM_SOCK_ERR_RA;
126 }
127 }
128
129 {
130 int reuse = 1;
131 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
132 (void *) &reuse, sizeof(reuse))) {
133 zlog_warn("Could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300134 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300135 close(fd);
136 return PIM_SOCK_ERR_REUSE;
137 }
138 }
139
140 {
141 const int MTTL = 1;
142 int ttl = MTTL;
143 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
144 (void *) &ttl, sizeof(ttl))) {
145 zlog_warn("Could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300146 MTTL, fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300147 close(fd);
148 return PIM_SOCK_ERR_TTL;
149 }
150 }
151
152 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
153 (void *) &loop, sizeof(loop))) {
154 zlog_warn("Could not %s Multicast Loopback Option on socket fd=%d: errno=%d: %s",
155 loop ? "enable" : "disable",
Everton Marquese96f0af2009-08-11 15:48:02 -0300156 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300157 close(fd);
158 return PIM_SOCK_ERR_LOOP;
159 }
160
161 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
162 (void *) &ifaddr, sizeof(ifaddr))) {
163 zlog_warn("Could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300164 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300165 close(fd);
166 return PIM_SOCK_ERR_IFACE;
167 }
168
169 {
170 long flags;
171
172 flags = fcntl(fd, F_GETFL, 0);
173 if (flags < 0) {
174 zlog_warn("Could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300175 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300176 close(fd);
177 return PIM_SOCK_ERR_NONBLOCK_GETFL;
178 }
179
180 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
181 zlog_warn("Could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300182 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300183 close(fd);
184 return PIM_SOCK_ERR_NONBLOCK_SETFL;
185 }
186 }
187
188 return fd;
189}
190
191int pim_socket_join(int fd, struct in_addr group,
192 struct in_addr ifaddr, int ifindex)
193{
194 int ret;
195
196#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
197 struct ip_mreqn opt;
198#else
199 struct ip_mreq opt;
200#endif
201
202 opt.imr_multiaddr = group;
203
204#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
205 opt.imr_address = ifaddr;
206 opt.imr_ifindex = ifindex;
207#else
208 opt.imr_interface = ifaddr;
209#endif
210
211 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &opt, sizeof(opt));
212 if (ret) {
213 char group_str[100];
214 char ifaddr_str[100];
215 if (!inet_ntop(AF_INET, &group, group_str , sizeof(group_str)))
216 sprintf(group_str, "<group?>");
217 if (!inet_ntop(AF_INET, &ifaddr, ifaddr_str , sizeof(ifaddr_str)))
218 sprintf(ifaddr_str, "<ifaddr?>");
219
220 zlog_err("Failure socket joining fd=%d group %s on interface address %s: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300221 fd, group_str, ifaddr_str, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300222 return ret;
223 }
224
225 if (PIM_DEBUG_TRACE) {
226 char group_str[100];
227 char ifaddr_str[100];
228 if (!inet_ntop(AF_INET, &group, group_str , sizeof(group_str)))
229 sprintf(group_str, "<group?>");
230 if (!inet_ntop(AF_INET, &ifaddr, ifaddr_str , sizeof(ifaddr_str)))
231 sprintf(ifaddr_str, "<ifaddr?>");
232
233 zlog_debug("Socket fd=%d joined group %s on interface address %s",
234 fd, group_str, ifaddr_str);
235 }
236
237 return ret;
238}
239
240int pim_socket_join_source(int fd, int ifindex,
241 struct in_addr group_addr,
242 struct in_addr source_addr,
243 const char *ifname)
244{
245 struct group_source_req req;
246 struct sockaddr_in *group_sa = (struct sockaddr_in *) &req.gsr_group;
247 struct sockaddr_in *source_sa = (struct sockaddr_in *) &req.gsr_source;
248
249 memset(group_sa, 0, sizeof(*group_sa));
250 group_sa->sin_family = AF_INET;
251 group_sa->sin_addr = group_addr;
252 group_sa->sin_port = htons(0);
253
254 memset(source_sa, 0, sizeof(*source_sa));
255 source_sa->sin_family = AF_INET;
256 source_sa->sin_addr = source_addr;
257 source_sa->sin_port = htons(0);
258
259 req.gsr_interface = ifindex;
260
261 if (setsockopt(fd, SOL_IP, MCAST_JOIN_SOURCE_GROUP,
262 &req, sizeof(req))) {
263 int e = errno;
264 char group_str[100];
265 char source_str[100];
266 pim_inet4_dump("<grp?>", group_addr, group_str, sizeof(group_str));
267 pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
268 zlog_warn("%s: setsockopt(fd=%d) failure for IGMP group %s source %s ifindex %d on interface %s: errno=%d: %s",
269 __PRETTY_FUNCTION__,
270 fd, group_str, source_str, ifindex, ifname,
Everton Marquese96f0af2009-08-11 15:48:02 -0300271 e, safe_strerror(e));
Everton Marques871dbcf2009-08-11 15:43:05 -0300272 return -1;
273 }
274
275 return 0;
276}
277
278int pim_socket_recvfromto(int fd, char *buf, size_t len,
279 struct sockaddr_in *from, socklen_t *fromlen,
280 struct sockaddr_in *to, socklen_t *tolen,
281 int *ifindex)
282{
283 struct msghdr msgh;
284 struct cmsghdr *cmsg;
285 struct iovec iov;
286 char cbuf[1000];
287 int err;
288
289 memset(&msgh, 0, sizeof(struct msghdr));
290 iov.iov_base = buf;
291 iov.iov_len = len;
292 msgh.msg_control = cbuf;
293 msgh.msg_controllen = sizeof(cbuf);
294 msgh.msg_name = from;
295 msgh.msg_namelen = fromlen ? *fromlen : 0;
296 msgh.msg_iov = &iov;
297 msgh.msg_iovlen = 1;
298 msgh.msg_flags = 0;
299
300 err = recvmsg(fd, &msgh, 0);
301 if (err < 0)
302 return err;
303
304 if (fromlen)
305 *fromlen = msgh.msg_namelen;
306
307 for (cmsg = CMSG_FIRSTHDR(&msgh);
308 cmsg != NULL;
309 cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
310
311#ifdef HAVE_IP_PKTINFO
312 if ((cmsg->cmsg_level == SOL_IP) && (cmsg->cmsg_type == IP_PKTINFO)) {
313 struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
314 if (to)
315 ((struct sockaddr_in *) to)->sin_addr = i->ipi_addr;
316 if (tolen)
317 *tolen = sizeof(struct sockaddr_in);
318 if (ifindex)
319 *ifindex = i->ipi_ifindex;
320 break;
321 }
322#endif
323
324#ifdef HAVE_IP_RECVDSTADDR
325 if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_RECVDSTADDR)) {
326 struct in_addr *i = (struct in_addr *) CMSG_DATA(cmsg);
327 if (to)
328 ((struct sockaddr_in *) to)->sin_addr = *i;
329 if (tolen)
330 *tolen = sizeof(struct sockaddr_in);
331 break;
332 }
333#endif
334
335#if defined(HAVE_IP_RECVIF) && defined(CMSG_IFINDEX)
336 if (cmsg->cmsg_type == IP_RECVIF)
337 if (ifindex)
338 *ifindex = CMSG_IFINDEX(cmsg);
339#endif
340
341 } /* for (cmsg) */
342
343 return err; /* len */
344}
345
346int pim_socket_mcastloop_get(int fd)
347{
348 int loop;
349 socklen_t loop_len = sizeof(loop);
350
351 if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
352 &loop, &loop_len)) {
353 int e = errno;
354 zlog_warn("Could not get Multicast Loopback Option on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300355 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300356 errno = e;
357 return PIM_SOCK_ERR_LOOP;
358 }
359
360 return loop;
361}