3 Copyright (C) 2008 Everton da Silva Marques
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.
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.
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,
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <netinet/igmp.h>
27 #include <arpa/inet.h>
39 #include "pim_mroute.h"
42 #include "pim_igmp_join.h"
47 pim_socket_raw (int protocol
)
51 if ( pimd_privs
.change (ZPRIVS_RAISE
) )
52 zlog_err ("pim_sockek_raw: could not raise privs, %s",
53 safe_strerror (errno
) );
55 fd
= socket(AF_INET
, SOCK_RAW
, protocol
);
57 if ( pimd_privs
.change (ZPRIVS_LOWER
) )
58 zlog_err ("pim_socket_raw: could not lower privs, %s",
59 safe_strerror (errno
) );
62 zlog_warn("Could not create raw socket: errno=%d: %s",
63 errno
, safe_strerror(errno
));
64 return PIM_SOCK_ERR_SOCKET
;
71 pim_socket_ip_hdr (int fd
)
76 if (pimd_privs
.change (ZPRIVS_RAISE
))
77 zlog_err ("%s: could not raise privs, %s",
78 __PRETTY_FUNCTION__
, safe_strerror (errno
));
80 ret
= setsockopt (fd
, IPPROTO_IP
, IP_HDRINCL
, &on
, sizeof (on
));
82 if (pimd_privs
.change (ZPRIVS_LOWER
))
83 zlog_err ("%s: could not lower privs, %s",
84 __PRETTY_FUNCTION__
, safe_strerror (errno
));
90 * Given a socket and a interface,
91 * Bind that socket to that interface
94 pim_socket_bind (int fd
, struct interface
*ifp
)
97 #ifdef SO_BINDTODEVICE
99 if (pimd_privs
.change (ZPRIVS_RAISE
))
100 zlog_err ("%s: could not raise privs, %s",
101 __PRETTY_FUNCTION__
, safe_strerror (errno
));
103 ret
= setsockopt (fd
, SOL_SOCKET
,
104 SO_BINDTODEVICE
, ifp
->name
, strlen (ifp
->name
));
106 if (pimd_privs
.change (ZPRIVS_LOWER
))
107 zlog_err ("%s: could not lower privs, %s",
108 __PRETTY_FUNCTION__
, safe_strerror (errno
));
114 int pim_socket_mcast(int protocol
, struct in_addr ifaddr
, struct interface
*ifp
, u_char loop
)
116 int rcvbuf
= 1024 * 1024 * 8;
117 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
118 struct ip_mreqn mreq
;
124 fd
= pim_socket_raw(protocol
);
126 zlog_warn("Could not create multicast socket: errno=%d: %s",
127 errno
, safe_strerror(errno
));
128 return PIM_SOCK_ERR_SOCKET
;
131 #ifdef SO_BINDTODEVICE
132 if (protocol
== IPPROTO_PIM
)
136 ret
= pim_socket_bind (fd
, ifp
);
140 zlog_warn("Could not set fd: %d for interface: %s to device",
142 return PIM_SOCK_ERR_BIND
;
146 /* XXX: use IP_PKTINFO / IP_RECVIF to emulate behaviour? Or change to
147 * only use 1 socket for all interfaces? */
150 /* Needed to obtain destination address from recvmsg() */
152 #if defined(HAVE_IP_PKTINFO)
153 /* Linux and Solaris IP_PKTINFO */
155 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &opt
, sizeof(opt
))) {
156 zlog_warn("Could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
157 fd
, errno
, safe_strerror(errno
));
159 #elif defined(HAVE_IP_RECVDSTADDR)
160 /* BSD IP_RECVDSTADDR */
162 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVDSTADDR
, &opt
, sizeof(opt
))) {
163 zlog_warn("Could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
164 fd
, errno
, safe_strerror(errno
));
167 zlog_err("%s %s: Missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
168 __FILE__
, __PRETTY_FUNCTION__
);
170 return PIM_SOCK_ERR_DSTADDR
;
175 /* Set router alert (RFC 2113) for all IGMP messages (RFC 3376 4. Message Formats)*/
176 if (protocol
== IPPROTO_IGMP
) {
182 if (setsockopt(fd
, IPPROTO_IP
, IP_OPTIONS
, ra
, 4)) {
183 zlog_warn("Could not set Router Alert Option on socket fd=%d: errno=%d: %s",
184 fd
, errno
, safe_strerror(errno
));
186 return PIM_SOCK_ERR_RA
;
192 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
193 (void *) &reuse
, sizeof(reuse
))) {
194 zlog_warn("Could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
195 fd
, errno
, safe_strerror(errno
));
197 return PIM_SOCK_ERR_REUSE
;
204 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_TTL
,
205 (void *) &ttl
, sizeof(ttl
))) {
206 zlog_warn("Could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
207 MTTL
, fd
, errno
, safe_strerror(errno
));
209 return PIM_SOCK_ERR_TTL
;
213 if (setsockopt_ipv4_multicast_loop (fd
, loop
)) {
214 zlog_warn("Could not %s Multicast Loopback Option on socket fd=%d: errno=%d: %s",
215 loop
? "enable" : "disable",
216 fd
, errno
, safe_strerror(errno
));
218 return PIM_SOCK_ERR_LOOP
;
221 memset (&mreq
, 0, sizeof (mreq
));
222 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
223 mreq
.imr_ifindex
= ifp
->ifindex
;
226 * I am not sure what to do here yet for *BSD
228 //mreq.imr_interface = ifindex;
231 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
,
232 (void *) &mreq
, sizeof(mreq
))) {
233 zlog_warn("Could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
234 fd
, errno
, safe_strerror(errno
));
236 return PIM_SOCK_ERR_IFACE
;
239 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &rcvbuf
, sizeof(rcvbuf
)))
240 zlog_warn("%s: Failure to set buffer size to %d",
241 __PRETTY_FUNCTION__
, rcvbuf
);
246 flags
= fcntl(fd
, F_GETFL
, 0);
248 zlog_warn("Could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
249 fd
, errno
, safe_strerror(errno
));
251 return PIM_SOCK_ERR_NONBLOCK_GETFL
;
254 if (fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
)) {
255 zlog_warn("Could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
256 fd
, errno
, safe_strerror(errno
));
258 return PIM_SOCK_ERR_NONBLOCK_SETFL
;
265 int pim_socket_join(int fd
, struct in_addr group
,
266 struct in_addr ifaddr
, ifindex_t ifindex
)
270 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
276 opt
.imr_multiaddr
= group
;
278 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
279 opt
.imr_address
= ifaddr
;
280 opt
.imr_ifindex
= ifindex
;
282 opt
.imr_interface
= ifaddr
;
285 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
, &opt
, sizeof(opt
));
287 char group_str
[INET_ADDRSTRLEN
];
288 char ifaddr_str
[INET_ADDRSTRLEN
];
289 if (!inet_ntop(AF_INET
, &group
, group_str
, sizeof(group_str
)))
290 sprintf(group_str
, "<group?>");
291 if (!inet_ntop(AF_INET
, &ifaddr
, ifaddr_str
, sizeof(ifaddr_str
)))
292 sprintf(ifaddr_str
, "<ifaddr?>");
294 zlog_err("Failure socket joining fd=%d group %s on interface address %s: errno=%d: %s",
295 fd
, group_str
, ifaddr_str
, errno
, safe_strerror(errno
));
299 if (PIM_DEBUG_TRACE
) {
300 char group_str
[INET_ADDRSTRLEN
];
301 char ifaddr_str
[INET_ADDRSTRLEN
];
302 if (!inet_ntop(AF_INET
, &group
, group_str
, sizeof(group_str
)))
303 sprintf(group_str
, "<group?>");
304 if (!inet_ntop(AF_INET
, &ifaddr
, ifaddr_str
, sizeof(ifaddr_str
)))
305 sprintf(ifaddr_str
, "<ifaddr?>");
307 zlog_debug("Socket fd=%d joined group %s on interface address %s",
308 fd
, group_str
, ifaddr_str
);
314 int pim_socket_join_source(int fd
, ifindex_t ifindex
,
315 struct in_addr group_addr
,
316 struct in_addr source_addr
,
319 if (pim_igmp_join_source(fd
, ifindex
, group_addr
, source_addr
)) {
320 char group_str
[INET_ADDRSTRLEN
];
321 char source_str
[INET_ADDRSTRLEN
];
322 pim_inet4_dump("<grp?>", group_addr
, group_str
, sizeof(group_str
));
323 pim_inet4_dump("<src?>", source_addr
, source_str
, sizeof(source_str
));
324 zlog_warn("%s: setsockopt(fd=%d) failure for IGMP group %s source %s ifindex %d on interface %s: errno=%d: %s",
326 fd
, group_str
, source_str
, ifindex
, ifname
,
327 errno
, safe_strerror(errno
));
334 int pim_socket_recvfromto(int fd
, uint8_t *buf
, size_t len
,
335 struct sockaddr_in
*from
, socklen_t
*fromlen
,
336 struct sockaddr_in
*to
, socklen_t
*tolen
,
340 struct cmsghdr
*cmsg
;
346 * IP_PKTINFO / IP_RECVDSTADDR don't yield sin_port.
347 * Use getsockname() to get sin_port.
350 struct sockaddr_in si
;
351 socklen_t si_len
= sizeof(si
);
353 memset (&si
, 0, sizeof (si
));
354 to
->sin_family
= AF_INET
;
356 pim_socket_getsockname(fd
, (struct sockaddr
*) &si
, &si_len
);
358 to
->sin_port
= si
.sin_port
;
359 to
->sin_addr
= si
.sin_addr
;
365 memset(&msgh
, 0, sizeof(struct msghdr
));
368 msgh
.msg_control
= cbuf
;
369 msgh
.msg_controllen
= sizeof(cbuf
);
370 msgh
.msg_name
= from
;
371 msgh
.msg_namelen
= fromlen
? *fromlen
: 0;
376 err
= recvmsg(fd
, &msgh
, 0);
381 *fromlen
= msgh
.msg_namelen
;
383 for (cmsg
= CMSG_FIRSTHDR(&msgh
);
385 cmsg
= CMSG_NXTHDR(&msgh
,cmsg
)) {
387 #ifdef HAVE_IP_PKTINFO
388 if ((cmsg
->cmsg_level
== IPPROTO_IP
) && (cmsg
->cmsg_type
== IP_PKTINFO
)) {
389 struct in_pktinfo
*i
= (struct in_pktinfo
*) CMSG_DATA(cmsg
);
391 ((struct sockaddr_in
*) to
)->sin_addr
= i
->ipi_addr
;
393 *tolen
= sizeof(struct sockaddr_in
);
395 *ifindex
= i
->ipi_ifindex
;
401 #ifdef HAVE_IP_RECVDSTADDR
402 if ((cmsg
->cmsg_level
== IPPROTO_IP
) && (cmsg
->cmsg_type
== IP_RECVDSTADDR
)) {
403 struct in_addr
*i
= (struct in_addr
*) CMSG_DATA(cmsg
);
405 ((struct sockaddr_in
*) to
)->sin_addr
= *i
;
407 *tolen
= sizeof(struct sockaddr_in
);
413 #if defined(HAVE_IP_RECVIF) && defined(CMSG_IFINDEX)
414 if (cmsg
->cmsg_type
== IP_RECVIF
)
416 *ifindex
= CMSG_IFINDEX(cmsg
);
421 return err
; /* len */
424 int pim_socket_mcastloop_get(int fd
)
427 socklen_t loop_len
= sizeof(loop
);
429 if (getsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
432 zlog_warn("Could not get Multicast Loopback Option on socket fd=%d: errno=%d: %s",
433 fd
, errno
, safe_strerror(errno
));
435 return PIM_SOCK_ERR_LOOP
;
441 int pim_socket_getsockname(int fd
, struct sockaddr
*name
, socklen_t
*namelen
)
443 if (getsockname(fd
, name
, namelen
)) {
445 zlog_warn("Could not get Socket Name for socket fd=%d: errno=%d: %s",
446 fd
, errno
, safe_strerror(errno
));
448 return PIM_SOCK_ERR_NAME
;
451 return PIM_SOCK_ERR_NONE
;