blob: c908e8b8308c37c9300586f706e6d8489a9a0b88 [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"
Everton Marques40765fe2009-09-30 17:10:11 -030041#include "pim_igmp_join.h"
Everton Marques871dbcf2009-08-11 15:43:05 -030042
Leonard Herve596470f2009-08-11 15:45:26 -030043/* GLOBAL VARS */
44extern struct zebra_privs_t pimd_privs;
45
Everton Marques871dbcf2009-08-11 15:43:05 -030046int pim_socket_raw(int protocol)
47{
48 int fd;
49
Leonard Herve596470f2009-08-11 15:45:26 -030050 if ( pimd_privs.change (ZPRIVS_RAISE) )
51 zlog_err ("pim_sockek_raw: could not raise privs, %s",
52 safe_strerror (errno) );
53
Everton Marques871dbcf2009-08-11 15:43:05 -030054 fd = socket(AF_INET, SOCK_RAW, protocol);
Leonard Herve596470f2009-08-11 15:45:26 -030055
56 if ( pimd_privs.change (ZPRIVS_LOWER) )
57 zlog_err ("pim_socket_raw: could not lower privs, %s",
58 safe_strerror (errno) );
59
Everton Marques871dbcf2009-08-11 15:43:05 -030060 if (fd < 0) {
61 zlog_warn("Could not create raw socket: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -030062 errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -030063 return PIM_SOCK_ERR_SOCKET;
64 }
65
66 return fd;
67}
68
69int pim_socket_mcast(int protocol, struct in_addr ifaddr, int loop)
70{
71 int fd;
72
73 fd = pim_socket_raw(protocol);
74 if (fd < 0) {
75 zlog_warn("Could not create multicast socket: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -030076 errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -030077 return PIM_SOCK_ERR_SOCKET;
78 }
79
80 /* Needed to obtain destination address from recvmsg() */
81 {
82#if defined(HAVE_IP_PKTINFO)
83 /* Linux IP_PKTINFO */
84 int opt = 1;
85 if (setsockopt(fd, SOL_IP, IP_PKTINFO, &opt, sizeof(opt))) {
86 zlog_warn("Could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -030087 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -030088 }
89#elif defined(HAVE_IP_RECVDSTADDR)
90 /* BSD IP_RECVDSTADDR */
91 int opt = 1;
92 if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt))) {
93 zlog_warn("Could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -030094 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -030095 }
96#else
97 zlog_err("%s %s: Missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
98 __FILE__, __PRETTY_FUNCTION__);
99 close(fd);
100 return PIM_SOCK_ERR_DSTADDR;
101#endif
102 }
103
104
Leonard Hervedf4044b2009-08-14 10:38:52 +0200105 /* Set router alert (RFC 2113) for all IGMP messages (RFC 3376 4. Message Formats)*/
106 if (protocol == IPPROTO_IGMP) {
Everton Marques871dbcf2009-08-11 15:43:05 -0300107 char ra[4];
108 ra[0] = 148;
109 ra[1] = 4;
110 ra[2] = 0;
111 ra[3] = 0;
112 if (setsockopt(fd, IPPROTO_IP, IP_OPTIONS, ra, 4)) {
113 zlog_warn("Could not set Router Alert Option on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300114 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300115 close(fd);
116 return PIM_SOCK_ERR_RA;
117 }
118 }
119
120 {
121 int reuse = 1;
122 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
123 (void *) &reuse, sizeof(reuse))) {
124 zlog_warn("Could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300125 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300126 close(fd);
127 return PIM_SOCK_ERR_REUSE;
128 }
129 }
130
131 {
132 const int MTTL = 1;
133 int ttl = MTTL;
134 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
135 (void *) &ttl, sizeof(ttl))) {
136 zlog_warn("Could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300137 MTTL, fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300138 close(fd);
139 return PIM_SOCK_ERR_TTL;
140 }
141 }
142
143 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
144 (void *) &loop, sizeof(loop))) {
145 zlog_warn("Could not %s Multicast Loopback Option on socket fd=%d: errno=%d: %s",
146 loop ? "enable" : "disable",
Everton Marquese96f0af2009-08-11 15:48:02 -0300147 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300148 close(fd);
149 return PIM_SOCK_ERR_LOOP;
150 }
151
152 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
153 (void *) &ifaddr, sizeof(ifaddr))) {
154 zlog_warn("Could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300155 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300156 close(fd);
157 return PIM_SOCK_ERR_IFACE;
158 }
159
160 {
161 long flags;
162
163 flags = fcntl(fd, F_GETFL, 0);
164 if (flags < 0) {
165 zlog_warn("Could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300166 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300167 close(fd);
168 return PIM_SOCK_ERR_NONBLOCK_GETFL;
169 }
170
171 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
172 zlog_warn("Could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300173 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300174 close(fd);
175 return PIM_SOCK_ERR_NONBLOCK_SETFL;
176 }
177 }
178
179 return fd;
180}
181
182int pim_socket_join(int fd, struct in_addr group,
183 struct in_addr ifaddr, int ifindex)
184{
185 int ret;
186
187#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
188 struct ip_mreqn opt;
189#else
190 struct ip_mreq opt;
191#endif
192
193 opt.imr_multiaddr = group;
194
195#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
196 opt.imr_address = ifaddr;
197 opt.imr_ifindex = ifindex;
198#else
199 opt.imr_interface = ifaddr;
200#endif
201
202 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &opt, sizeof(opt));
203 if (ret) {
204 char group_str[100];
205 char ifaddr_str[100];
206 if (!inet_ntop(AF_INET, &group, group_str , sizeof(group_str)))
207 sprintf(group_str, "<group?>");
208 if (!inet_ntop(AF_INET, &ifaddr, ifaddr_str , sizeof(ifaddr_str)))
209 sprintf(ifaddr_str, "<ifaddr?>");
210
211 zlog_err("Failure socket joining fd=%d group %s on interface address %s: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300212 fd, group_str, ifaddr_str, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300213 return ret;
214 }
215
216 if (PIM_DEBUG_TRACE) {
217 char group_str[100];
218 char ifaddr_str[100];
219 if (!inet_ntop(AF_INET, &group, group_str , sizeof(group_str)))
220 sprintf(group_str, "<group?>");
221 if (!inet_ntop(AF_INET, &ifaddr, ifaddr_str , sizeof(ifaddr_str)))
222 sprintf(ifaddr_str, "<ifaddr?>");
223
224 zlog_debug("Socket fd=%d joined group %s on interface address %s",
225 fd, group_str, ifaddr_str);
226 }
227
228 return ret;
229}
230
231int pim_socket_join_source(int fd, int ifindex,
232 struct in_addr group_addr,
233 struct in_addr source_addr,
234 const char *ifname)
235{
Everton Marques40765fe2009-09-30 17:10:11 -0300236 if (pim_igmp_join_source(fd, ifindex, group_addr, source_addr)) {
Everton Marques871dbcf2009-08-11 15:43:05 -0300237 int e = errno;
238 char group_str[100];
239 char source_str[100];
240 pim_inet4_dump("<grp?>", group_addr, group_str, sizeof(group_str));
241 pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
242 zlog_warn("%s: setsockopt(fd=%d) failure for IGMP group %s source %s ifindex %d on interface %s: errno=%d: %s",
243 __PRETTY_FUNCTION__,
244 fd, group_str, source_str, ifindex, ifname,
Everton Marquese96f0af2009-08-11 15:48:02 -0300245 e, safe_strerror(e));
Everton Marques871dbcf2009-08-11 15:43:05 -0300246 return -1;
247 }
248
249 return 0;
250}
251
252int pim_socket_recvfromto(int fd, char *buf, size_t len,
253 struct sockaddr_in *from, socklen_t *fromlen,
254 struct sockaddr_in *to, socklen_t *tolen,
255 int *ifindex)
256{
257 struct msghdr msgh;
258 struct cmsghdr *cmsg;
259 struct iovec iov;
260 char cbuf[1000];
261 int err;
262
263 memset(&msgh, 0, sizeof(struct msghdr));
264 iov.iov_base = buf;
265 iov.iov_len = len;
266 msgh.msg_control = cbuf;
267 msgh.msg_controllen = sizeof(cbuf);
268 msgh.msg_name = from;
269 msgh.msg_namelen = fromlen ? *fromlen : 0;
270 msgh.msg_iov = &iov;
271 msgh.msg_iovlen = 1;
272 msgh.msg_flags = 0;
273
274 err = recvmsg(fd, &msgh, 0);
275 if (err < 0)
276 return err;
277
278 if (fromlen)
279 *fromlen = msgh.msg_namelen;
280
281 for (cmsg = CMSG_FIRSTHDR(&msgh);
282 cmsg != NULL;
283 cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
284
285#ifdef HAVE_IP_PKTINFO
286 if ((cmsg->cmsg_level == SOL_IP) && (cmsg->cmsg_type == IP_PKTINFO)) {
287 struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
288 if (to)
289 ((struct sockaddr_in *) to)->sin_addr = i->ipi_addr;
290 if (tolen)
291 *tolen = sizeof(struct sockaddr_in);
292 if (ifindex)
293 *ifindex = i->ipi_ifindex;
294 break;
295 }
296#endif
297
298#ifdef HAVE_IP_RECVDSTADDR
299 if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_RECVDSTADDR)) {
300 struct in_addr *i = (struct in_addr *) CMSG_DATA(cmsg);
301 if (to)
302 ((struct sockaddr_in *) to)->sin_addr = *i;
303 if (tolen)
304 *tolen = sizeof(struct sockaddr_in);
305 break;
306 }
307#endif
308
309#if defined(HAVE_IP_RECVIF) && defined(CMSG_IFINDEX)
310 if (cmsg->cmsg_type == IP_RECVIF)
311 if (ifindex)
312 *ifindex = CMSG_IFINDEX(cmsg);
313#endif
314
315 } /* for (cmsg) */
316
317 return err; /* len */
318}
319
320int pim_socket_mcastloop_get(int fd)
321{
322 int loop;
323 socklen_t loop_len = sizeof(loop);
324
325 if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
326 &loop, &loop_len)) {
327 int e = errno;
328 zlog_warn("Could not get Multicast Loopback Option on socket fd=%d: errno=%d: %s",
Everton Marquese96f0af2009-08-11 15:48:02 -0300329 fd, errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -0300330 errno = e;
331 return PIM_SOCK_ERR_LOOP;
332 }
333
334 return loop;
335}