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>
38 #include "pim_mroute.h"
44 int pim_socket_raw(int protocol
)
48 if (pimd_privs
.change(ZPRIVS_RAISE
))
49 zlog_err("pim_sockek_raw: could not raise privs, %s",
50 safe_strerror(errno
));
52 fd
= socket(AF_INET
, SOCK_RAW
, protocol
);
54 if (pimd_privs
.change(ZPRIVS_LOWER
))
55 zlog_err("pim_socket_raw: could not lower privs, %s",
56 safe_strerror(errno
));
59 zlog_warn("Could not create raw socket: errno=%d: %s", errno
,
60 safe_strerror(errno
));
61 return PIM_SOCK_ERR_SOCKET
;
67 void pim_socket_ip_hdr(int fd
)
71 if (pimd_privs
.change(ZPRIVS_RAISE
))
72 zlog_err("%s: could not raise privs, %s", __PRETTY_FUNCTION__
,
73 safe_strerror(errno
));
75 if (setsockopt(fd
, IPPROTO_IP
, IP_HDRINCL
, &on
, sizeof(on
)))
76 zlog_err("%s: Could not turn on IP_HDRINCL option: %s",
77 __PRETTY_FUNCTION__
, safe_strerror(errno
));
79 if (pimd_privs
.change(ZPRIVS_LOWER
))
80 zlog_err("%s: could not lower privs, %s", __PRETTY_FUNCTION__
,
81 safe_strerror(errno
));
85 * Given a socket and a interface,
86 * Bind that socket to that interface
88 int pim_socket_bind(int fd
, struct interface
*ifp
)
91 #ifdef SO_BINDTODEVICE
93 if (pimd_privs
.change(ZPRIVS_RAISE
))
94 zlog_err("%s: could not raise privs, %s", __PRETTY_FUNCTION__
,
95 safe_strerror(errno
));
97 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, ifp
->name
,
100 if (pimd_privs
.change(ZPRIVS_LOWER
))
101 zlog_err("%s: could not lower privs, %s", __PRETTY_FUNCTION__
,
102 safe_strerror(errno
));
108 int pim_socket_mcast(int protocol
, struct in_addr ifaddr
, struct interface
*ifp
,
111 int rcvbuf
= 1024 * 1024 * 8;
112 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
113 struct ip_mreqn mreq
;
119 fd
= pim_socket_raw(protocol
);
121 zlog_warn("Could not create multicast socket: errno=%d: %s",
122 errno
, safe_strerror(errno
));
123 return PIM_SOCK_ERR_SOCKET
;
126 #ifdef SO_BINDTODEVICE
127 if (protocol
== IPPROTO_PIM
) {
130 ret
= pim_socket_bind(fd
, ifp
);
134 "Could not set fd: %d for interface: %s to device",
136 return PIM_SOCK_ERR_BIND
;
140 /* XXX: use IP_PKTINFO / IP_RECVIF to emulate behaviour? Or change to
141 * only use 1 socket for all interfaces? */
144 /* Needed to obtain destination address from recvmsg() */
146 #if defined(HAVE_IP_PKTINFO)
147 /* Linux and Solaris IP_PKTINFO */
149 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &opt
, sizeof(opt
))) {
151 "Could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
152 fd
, errno
, safe_strerror(errno
));
154 #elif defined(HAVE_IP_RECVDSTADDR)
155 /* BSD IP_RECVDSTADDR */
157 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVDSTADDR
, &opt
,
160 "Could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
161 fd
, errno
, safe_strerror(errno
));
165 "%s %s: Missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
166 __FILE__
, __PRETTY_FUNCTION__
);
168 return PIM_SOCK_ERR_DSTADDR
;
173 /* Set router alert (RFC 2113) for all IGMP messages (RFC 3376 4.
175 if (protocol
== IPPROTO_IGMP
) {
181 if (setsockopt(fd
, IPPROTO_IP
, IP_OPTIONS
, ra
, 4)) {
183 "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
, (void *)&reuse
,
195 "Could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
196 fd
, errno
, safe_strerror(errno
));
198 return PIM_SOCK_ERR_REUSE
;
205 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, (void *)&ttl
,
208 "Could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
209 MTTL
, fd
, errno
, safe_strerror(errno
));
211 return PIM_SOCK_ERR_TTL
;
215 if (setsockopt_ipv4_multicast_loop(fd
, loop
)) {
217 "Could not %s Multicast Loopback Option on socket fd=%d: errno=%d: %s",
218 loop
? "enable" : "disable", fd
, errno
,
219 safe_strerror(errno
));
221 return PIM_SOCK_ERR_LOOP
;
224 memset(&mreq
, 0, sizeof(mreq
));
225 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
226 mreq
.imr_ifindex
= ifp
->ifindex
;
229 * I am not sure what to do here yet for *BSD
231 // mreq.imr_interface = ifindex;
234 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&mreq
,
237 "Could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
238 fd
, errno
, safe_strerror(errno
));
240 return PIM_SOCK_ERR_IFACE
;
243 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &rcvbuf
, sizeof(rcvbuf
)))
244 zlog_warn("%s: Failure to set buffer size to %d",
245 __PRETTY_FUNCTION__
, rcvbuf
);
250 flags
= fcntl(fd
, F_GETFL
, 0);
253 "Could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
254 fd
, errno
, safe_strerror(errno
));
256 return PIM_SOCK_ERR_NONBLOCK_GETFL
;
259 if (fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
)) {
261 "Could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
262 fd
, errno
, safe_strerror(errno
));
264 return PIM_SOCK_ERR_NONBLOCK_SETFL
;
271 int pim_socket_join(int fd
, struct in_addr group
, struct in_addr ifaddr
,
276 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
282 opt
.imr_multiaddr
= group
;
284 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
285 opt
.imr_address
= ifaddr
;
286 opt
.imr_ifindex
= ifindex
;
288 opt
.imr_interface
= ifaddr
;
291 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
, &opt
, sizeof(opt
));
293 char group_str
[INET_ADDRSTRLEN
];
294 char ifaddr_str
[INET_ADDRSTRLEN
];
295 if (!inet_ntop(AF_INET
, &group
, group_str
, sizeof(group_str
)))
296 sprintf(group_str
, "<group?>");
297 if (!inet_ntop(AF_INET
, &ifaddr
, ifaddr_str
,
299 sprintf(ifaddr_str
, "<ifaddr?>");
302 "Failure socket joining fd=%d group %s on interface address %s: errno=%d: %s",
303 fd
, group_str
, ifaddr_str
, errno
, safe_strerror(errno
));
307 if (PIM_DEBUG_TRACE
) {
308 char group_str
[INET_ADDRSTRLEN
];
309 char ifaddr_str
[INET_ADDRSTRLEN
];
310 if (!inet_ntop(AF_INET
, &group
, group_str
, sizeof(group_str
)))
311 sprintf(group_str
, "<group?>");
312 if (!inet_ntop(AF_INET
, &ifaddr
, ifaddr_str
,
314 sprintf(ifaddr_str
, "<ifaddr?>");
317 "Socket fd=%d joined group %s on interface address %s",
318 fd
, group_str
, ifaddr_str
);
324 int pim_socket_recvfromto(int fd
, uint8_t *buf
, size_t len
,
325 struct sockaddr_in
*from
, socklen_t
*fromlen
,
326 struct sockaddr_in
*to
, socklen_t
*tolen
,
330 struct cmsghdr
*cmsg
;
336 * IP_PKTINFO / IP_RECVDSTADDR don't yield sin_port.
337 * Use getsockname() to get sin_port.
340 struct sockaddr_in si
;
341 socklen_t si_len
= sizeof(si
);
343 memset(&si
, 0, sizeof(si
));
344 to
->sin_family
= AF_INET
;
346 pim_socket_getsockname(fd
, (struct sockaddr
*)&si
, &si_len
);
348 to
->sin_port
= si
.sin_port
;
349 to
->sin_addr
= si
.sin_addr
;
355 memset(&msgh
, 0, sizeof(struct msghdr
));
358 msgh
.msg_control
= cbuf
;
359 msgh
.msg_controllen
= sizeof(cbuf
);
360 msgh
.msg_name
= from
;
361 msgh
.msg_namelen
= fromlen
? *fromlen
: 0;
366 err
= recvmsg(fd
, &msgh
, 0);
371 *fromlen
= msgh
.msg_namelen
;
373 for (cmsg
= CMSG_FIRSTHDR(&msgh
); cmsg
!= NULL
;
374 cmsg
= CMSG_NXTHDR(&msgh
, cmsg
)) {
376 #ifdef HAVE_IP_PKTINFO
377 if ((cmsg
->cmsg_level
== IPPROTO_IP
)
378 && (cmsg
->cmsg_type
== IP_PKTINFO
)) {
379 struct in_pktinfo
*i
=
380 (struct in_pktinfo
*)CMSG_DATA(cmsg
);
382 ((struct sockaddr_in
*)to
)->sin_addr
=
385 *tolen
= sizeof(struct sockaddr_in
);
387 *ifindex
= i
->ipi_ifindex
;
393 #ifdef HAVE_IP_RECVDSTADDR
394 if ((cmsg
->cmsg_level
== IPPROTO_IP
)
395 && (cmsg
->cmsg_type
== IP_RECVDSTADDR
)) {
396 struct in_addr
*i
= (struct in_addr
*)CMSG_DATA(cmsg
);
398 ((struct sockaddr_in
*)to
)->sin_addr
= *i
;
400 *tolen
= sizeof(struct sockaddr_in
);
406 #if defined(HAVE_IP_RECVIF) && defined(CMSG_IFINDEX)
407 if (cmsg
->cmsg_type
== IP_RECVIF
)
409 *ifindex
= CMSG_IFINDEX(cmsg
);
414 return err
; /* len */
417 int pim_socket_mcastloop_get(int fd
)
420 socklen_t loop_len
= sizeof(loop
);
422 if (getsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, &loop
, &loop_len
)) {
425 "Could not get Multicast Loopback Option on socket fd=%d: errno=%d: %s",
426 fd
, errno
, safe_strerror(errno
));
428 return PIM_SOCK_ERR_LOOP
;
434 int pim_socket_getsockname(int fd
, struct sockaddr
*name
, socklen_t
*namelen
)
436 if (getsockname(fd
, name
, namelen
)) {
439 "Could not get Socket Name for socket fd=%d: errno=%d: %s",
440 fd
, errno
, safe_strerror(errno
));
442 return PIM_SOCK_ERR_NAME
;
445 return PIM_SOCK_ERR_NONE
;