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 along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netinet/igmp.h>
26 #include <arpa/inet.h>
36 #include "lib_errors.h"
39 #include "pim_mroute.h"
45 int pim_socket_raw(int protocol
)
49 frr_elevate_privs(&pimd_privs
) {
51 fd
= socket(AF_INET
, SOCK_RAW
, protocol
);
56 zlog_warn("Could not create raw socket: errno=%d: %s", errno
,
57 safe_strerror(errno
));
58 return PIM_SOCK_ERR_SOCKET
;
64 void pim_socket_ip_hdr(int fd
)
68 frr_elevate_privs(&pimd_privs
) {
70 if (setsockopt(fd
, IPPROTO_IP
, IP_HDRINCL
, &on
, sizeof(on
)))
71 zlog_err("%s: Could not turn on IP_HDRINCL option: %s",
72 __PRETTY_FUNCTION__
, safe_strerror(errno
));
78 * Given a socket and a interface,
79 * Bind that socket to that interface
81 int pim_socket_bind(int fd
, struct interface
*ifp
)
84 #ifdef SO_BINDTODEVICE
86 frr_elevate_privs(&pimd_privs
) {
88 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, ifp
->name
,
97 int pim_socket_mcast(int protocol
, struct in_addr ifaddr
, struct interface
*ifp
,
100 int rcvbuf
= 1024 * 1024 * 8;
101 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
102 struct ip_mreqn mreq
;
108 fd
= pim_socket_raw(protocol
);
110 zlog_warn("Could not create multicast socket: errno=%d: %s",
111 errno
, safe_strerror(errno
));
112 return PIM_SOCK_ERR_SOCKET
;
115 #ifdef SO_BINDTODEVICE
116 if (protocol
== IPPROTO_PIM
) {
119 ret
= pim_socket_bind(fd
, ifp
);
123 "Could not set fd: %d for interface: %s to device",
125 return PIM_SOCK_ERR_BIND
;
129 /* XXX: use IP_PKTINFO / IP_RECVIF to emulate behaviour? Or change to
130 * only use 1 socket for all interfaces? */
133 /* Needed to obtain destination address from recvmsg() */
135 #if defined(HAVE_IP_PKTINFO)
136 /* Linux and Solaris IP_PKTINFO */
138 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &opt
, sizeof(opt
))) {
140 "Could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
141 fd
, errno
, safe_strerror(errno
));
143 #elif defined(HAVE_IP_RECVDSTADDR)
144 /* BSD IP_RECVDSTADDR */
146 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVDSTADDR
, &opt
,
149 "Could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
150 fd
, errno
, safe_strerror(errno
));
155 "%s %s: Missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
156 __FILE__
, __PRETTY_FUNCTION__
);
158 return PIM_SOCK_ERR_DSTADDR
;
163 /* Set router alert (RFC 2113) for all IGMP messages (RFC 3376 4.
165 if (protocol
== IPPROTO_IGMP
) {
171 if (setsockopt(fd
, IPPROTO_IP
, IP_OPTIONS
, ra
, 4)) {
173 "Could not set Router Alert Option on socket fd=%d: errno=%d: %s",
174 fd
, errno
, safe_strerror(errno
));
176 return PIM_SOCK_ERR_RA
;
182 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&reuse
,
185 "Could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
186 fd
, errno
, safe_strerror(errno
));
188 return PIM_SOCK_ERR_REUSE
;
195 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, (void *)&ttl
,
198 "Could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
199 MTTL
, fd
, errno
, safe_strerror(errno
));
201 return PIM_SOCK_ERR_TTL
;
205 if (setsockopt_ipv4_multicast_loop(fd
, loop
)) {
207 "Could not %s Multicast Loopback Option on socket fd=%d: errno=%d: %s",
208 loop
? "enable" : "disable", fd
, errno
,
209 safe_strerror(errno
));
211 return PIM_SOCK_ERR_LOOP
;
214 memset(&mreq
, 0, sizeof(mreq
));
215 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
216 mreq
.imr_ifindex
= ifp
->ifindex
;
219 * I am not sure what to do here yet for *BSD
221 // mreq.imr_interface = ifindex;
224 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&mreq
,
227 "Could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
228 fd
, errno
, safe_strerror(errno
));
230 return PIM_SOCK_ERR_IFACE
;
233 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &rcvbuf
, sizeof(rcvbuf
)))
234 zlog_warn("%s: Failure to set buffer size to %d",
235 __PRETTY_FUNCTION__
, rcvbuf
);
240 flags
= fcntl(fd
, F_GETFL
, 0);
243 "Could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
244 fd
, errno
, safe_strerror(errno
));
246 return PIM_SOCK_ERR_NONBLOCK_GETFL
;
249 if (fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
)) {
251 "Could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
252 fd
, errno
, safe_strerror(errno
));
254 return PIM_SOCK_ERR_NONBLOCK_SETFL
;
261 int pim_socket_join(int fd
, struct in_addr group
, struct in_addr ifaddr
,
266 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
272 opt
.imr_multiaddr
= group
;
274 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
275 opt
.imr_address
= ifaddr
;
276 opt
.imr_ifindex
= ifindex
;
278 opt
.imr_interface
= ifaddr
;
281 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
, &opt
, sizeof(opt
));
283 char group_str
[INET_ADDRSTRLEN
];
284 char ifaddr_str
[INET_ADDRSTRLEN
];
285 if (!inet_ntop(AF_INET
, &group
, group_str
, sizeof(group_str
)))
286 sprintf(group_str
, "<group?>");
287 if (!inet_ntop(AF_INET
, &ifaddr
, ifaddr_str
,
289 sprintf(ifaddr_str
, "<ifaddr?>");
293 "Failure socket joining fd=%d group %s on interface address %s: errno=%d: %s",
294 fd
, group_str
, ifaddr_str
, errno
, safe_strerror(errno
));
298 if (PIM_DEBUG_TRACE
) {
299 char group_str
[INET_ADDRSTRLEN
];
300 char ifaddr_str
[INET_ADDRSTRLEN
];
301 if (!inet_ntop(AF_INET
, &group
, group_str
, sizeof(group_str
)))
302 sprintf(group_str
, "<group?>");
303 if (!inet_ntop(AF_INET
, &ifaddr
, ifaddr_str
,
305 sprintf(ifaddr_str
, "<ifaddr?>");
308 "Socket fd=%d joined group %s on interface address %s",
309 fd
, group_str
, ifaddr_str
);
315 int pim_socket_recvfromto(int fd
, uint8_t *buf
, size_t len
,
316 struct sockaddr_in
*from
, socklen_t
*fromlen
,
317 struct sockaddr_in
*to
, socklen_t
*tolen
,
321 struct cmsghdr
*cmsg
;
327 * IP_PKTINFO / IP_RECVDSTADDR don't yield sin_port.
328 * Use getsockname() to get sin_port.
331 struct sockaddr_in si
;
332 socklen_t si_len
= sizeof(si
);
334 memset(&si
, 0, sizeof(si
));
335 to
->sin_family
= AF_INET
;
337 pim_socket_getsockname(fd
, (struct sockaddr
*)&si
, &si_len
);
339 to
->sin_port
= si
.sin_port
;
340 to
->sin_addr
= si
.sin_addr
;
346 memset(&msgh
, 0, sizeof(struct msghdr
));
349 msgh
.msg_control
= cbuf
;
350 msgh
.msg_controllen
= sizeof(cbuf
);
351 msgh
.msg_name
= from
;
352 msgh
.msg_namelen
= fromlen
? *fromlen
: 0;
357 err
= recvmsg(fd
, &msgh
, 0);
362 *fromlen
= msgh
.msg_namelen
;
364 for (cmsg
= CMSG_FIRSTHDR(&msgh
); cmsg
!= NULL
;
365 cmsg
= CMSG_NXTHDR(&msgh
, cmsg
)) {
367 #ifdef HAVE_IP_PKTINFO
368 if ((cmsg
->cmsg_level
== IPPROTO_IP
)
369 && (cmsg
->cmsg_type
== IP_PKTINFO
)) {
370 struct in_pktinfo
*i
=
371 (struct in_pktinfo
*)CMSG_DATA(cmsg
);
373 ((struct sockaddr_in
*)to
)->sin_addr
=
376 *tolen
= sizeof(struct sockaddr_in
);
378 *ifindex
= i
->ipi_ifindex
;
384 #ifdef HAVE_IP_RECVDSTADDR
385 if ((cmsg
->cmsg_level
== IPPROTO_IP
)
386 && (cmsg
->cmsg_type
== IP_RECVDSTADDR
)) {
387 struct in_addr
*i
= (struct in_addr
*)CMSG_DATA(cmsg
);
389 ((struct sockaddr_in
*)to
)->sin_addr
= *i
;
391 *tolen
= sizeof(struct sockaddr_in
);
397 #if defined(HAVE_IP_RECVIF) && defined(CMSG_IFINDEX)
398 if (cmsg
->cmsg_type
== IP_RECVIF
)
400 *ifindex
= CMSG_IFINDEX(cmsg
);
405 return err
; /* len */
408 int pim_socket_mcastloop_get(int fd
)
411 socklen_t loop_len
= sizeof(loop
);
413 if (getsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, &loop
, &loop_len
)) {
416 "Could not get Multicast Loopback Option on socket fd=%d: errno=%d: %s",
417 fd
, errno
, safe_strerror(errno
));
419 return PIM_SOCK_ERR_LOOP
;
425 int pim_socket_getsockname(int fd
, struct sockaddr
*name
, socklen_t
*namelen
)
427 if (getsockname(fd
, name
, namelen
)) {
430 "Could not get Socket Name for socket fd=%d: errno=%d: %s",
431 fd
, errno
, safe_strerror(errno
));
433 return PIM_SOCK_ERR_NAME
;
436 return PIM_SOCK_ERR_NONE
;