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,
20 $QuaggaId: $Format:%an, %ai, %h$ $
24 #include "pim_mroute.h"
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <netinet/igmp.h>
30 #include <arpa/inet.h>
41 #include "pim_igmp_join.h"
44 extern struct zebra_privs_t pimd_privs;
46 int pim_socket_raw(int protocol)
50 if ( pimd_privs.change (ZPRIVS_RAISE) )
51 zlog_err ("pim_sockek_raw: could not raise privs, %s",
52 safe_strerror (errno) );
54 fd = socket(AF_INET, SOCK_RAW, protocol);
56 if ( pimd_privs.change (ZPRIVS_LOWER) )
57 zlog_err ("pim_socket_raw: could not lower privs, %s",
58 safe_strerror (errno) );
61 zlog_warn("Could not create raw socket: errno=%d: %s",
62 errno, safe_strerror(errno));
63 return PIM_SOCK_ERR_SOCKET;
69 int pim_socket_mcast(int protocol, struct in_addr ifaddr, int loop)
73 fd = pim_socket_raw(protocol);
75 zlog_warn("Could not create multicast socket: errno=%d: %s",
76 errno, safe_strerror(errno));
77 return PIM_SOCK_ERR_SOCKET;
80 /* Needed to obtain destination address from recvmsg() */
82 #if defined(HAVE_IP_PKTINFO)
83 /* Linux and Solaris IP_PKTINFO */
85 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt))) {
86 zlog_warn("Could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
87 fd, errno, safe_strerror(errno));
89 #elif defined(HAVE_IP_RECVDSTADDR)
90 /* BSD IP_RECVDSTADDR */
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",
94 fd, errno, safe_strerror(errno));
97 zlog_err("%s %s: Missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
98 __FILE__, __PRETTY_FUNCTION__);
100 return PIM_SOCK_ERR_DSTADDR;
105 /* Set router alert (RFC 2113) for all IGMP messages (RFC 3376 4. Message Formats)*/
106 if (protocol == IPPROTO_IGMP) {
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",
114 fd, errno, safe_strerror(errno));
116 return PIM_SOCK_ERR_RA;
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",
125 fd, errno, safe_strerror(errno));
127 return PIM_SOCK_ERR_REUSE;
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",
137 MTTL, fd, errno, safe_strerror(errno));
139 return PIM_SOCK_ERR_TTL;
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",
147 fd, errno, safe_strerror(errno));
149 return PIM_SOCK_ERR_LOOP;
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",
155 fd, errno, safe_strerror(errno));
157 return PIM_SOCK_ERR_IFACE;
163 flags = fcntl(fd, F_GETFL, 0);
165 zlog_warn("Could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
166 fd, errno, safe_strerror(errno));
168 return PIM_SOCK_ERR_NONBLOCK_GETFL;
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",
173 fd, errno, safe_strerror(errno));
175 return PIM_SOCK_ERR_NONBLOCK_SETFL;
182 int pim_socket_join(int fd, struct in_addr group,
183 struct in_addr ifaddr, ifindex_t ifindex)
187 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
193 opt.imr_multiaddr = group;
195 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
196 opt.imr_address = ifaddr;
197 opt.imr_ifindex = ifindex;
199 opt.imr_interface = ifaddr;
202 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &opt, sizeof(opt));
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?>");
211 zlog_err("Failure socket joining fd=%d group %s on interface address %s: errno=%d: %s",
212 fd, group_str, ifaddr_str, errno, safe_strerror(errno));
216 if (PIM_DEBUG_TRACE) {
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?>");
224 zlog_debug("Socket fd=%d joined group %s on interface address %s",
225 fd, group_str, ifaddr_str);
231 int pim_socket_join_source(int fd, ifindex_t ifindex,
232 struct in_addr group_addr,
233 struct in_addr source_addr,
236 if (pim_igmp_join_source(fd, ifindex, group_addr, source_addr)) {
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",
244 fd, group_str, source_str, ifindex, ifname,
245 e, safe_strerror(e));
252 int pim_socket_recvfromto(int fd, uint8_t *buf, size_t len,
253 struct sockaddr_in *from, socklen_t *fromlen,
254 struct sockaddr_in *to, socklen_t *tolen,
258 struct cmsghdr *cmsg;
264 * IP_PKTINFO / IP_RECVDSTADDR don't yield sin_port.
265 * Use getsockname() to get sin_port.
268 struct sockaddr_in si;
269 socklen_t si_len = sizeof(si);
271 ((struct sockaddr_in *) to)->sin_family = AF_INET;
273 if (pim_socket_getsockname(fd, (struct sockaddr *) &si, &si_len)) {
274 ((struct sockaddr_in *) to)->sin_port = ntohs(0);
275 ((struct sockaddr_in *) to)->sin_addr.s_addr = ntohl(0);
278 ((struct sockaddr_in *) to)->sin_port = si.sin_port;
279 ((struct sockaddr_in *) to)->sin_addr = si.sin_addr;
286 memset(&msgh, 0, sizeof(struct msghdr));
289 msgh.msg_control = cbuf;
290 msgh.msg_controllen = sizeof(cbuf);
291 msgh.msg_name = from;
292 msgh.msg_namelen = fromlen ? *fromlen : 0;
297 err = recvmsg(fd, &msgh, 0);
302 *fromlen = msgh.msg_namelen;
304 for (cmsg = CMSG_FIRSTHDR(&msgh);
306 cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
308 #ifdef HAVE_IP_PKTINFO
309 if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_PKTINFO)) {
310 struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
312 ((struct sockaddr_in *) to)->sin_addr = i->ipi_addr;
314 *tolen = sizeof(struct sockaddr_in);
316 *ifindex = i->ipi_ifindex;
318 if (to && PIM_DEBUG_PACKETS) {
320 pim_inet4_dump("<to?>", to->sin_addr, to_str, sizeof(to_str));
321 zlog_debug("%s: HAVE_IP_PKTINFO to=%s,%d",
323 to_str, ntohs(to->sin_port));
330 #ifdef HAVE_IP_RECVDSTADDR
331 if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_RECVDSTADDR)) {
332 struct in_addr *i = (struct in_addr *) CMSG_DATA(cmsg);
334 ((struct sockaddr_in *) to)->sin_addr = *i;
336 *tolen = sizeof(struct sockaddr_in);
338 if (to && PIM_DEBUG_PACKETS) {
340 pim_inet4_dump("<to?>", to->sin_addr, to_str, sizeof(to_str));
341 zlog_debug("%s: HAVE_IP_RECVDSTADDR to=%s,%d",
343 to_str, ntohs(to->sin_port));
350 #if defined(HAVE_IP_RECVIF) && defined(CMSG_IFINDEX)
351 if (cmsg->cmsg_type == IP_RECVIF)
353 *ifindex = CMSG_IFINDEX(cmsg);
358 return err; /* len */
361 int pim_socket_mcastloop_get(int fd)
364 socklen_t loop_len = sizeof(loop);
366 if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
369 zlog_warn("Could not get Multicast Loopback Option on socket fd=%d: errno=%d: %s",
370 fd, errno, safe_strerror(errno));
372 return PIM_SOCK_ERR_LOOP;
378 int pim_socket_getsockname(int fd, struct sockaddr *name, socklen_t *namelen)
380 if (getsockname(fd, name, namelen)) {
382 zlog_warn("Could not get Socket Name for socket fd=%d: errno=%d: %s",
383 fd, errno, safe_strerror(errno));
385 return PIM_SOCK_ERR_NAME;
388 return PIM_SOCK_ERR_NONE;