]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_sock.c
pimd: SO_BINDTODEVICE is not available on some platforms.
[mirror_frr.git] / pimd / pim_sock.c
1 /*
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
21 #include <zebra.h>
22
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>
28 #include <unistd.h>
29 #include <netdb.h>
30 #include <errno.h>
31
32 #include "log.h"
33 #include "privs.h"
34 #include "if.h"
35 #include "vrf.h"
36 #include "sockopt.h"
37
38 #include "pimd.h"
39 #include "pim_mroute.h"
40 #include "pim_sock.h"
41 #include "pim_str.h"
42 #include "pim_igmp_join.h"
43
44 /* GLOBAL VARS */
45 extern struct zebra_privs_t pimd_privs;
46
47 int
48 pim_socket_raw (int protocol)
49 {
50 int fd;
51
52 if ( pimd_privs.change (ZPRIVS_RAISE) )
53 zlog_err ("pim_sockek_raw: could not raise privs, %s",
54 safe_strerror (errno) );
55
56 fd = socket(AF_INET, SOCK_RAW, protocol);
57
58 if ( pimd_privs.change (ZPRIVS_LOWER) )
59 zlog_err ("pim_socket_raw: could not lower privs, %s",
60 safe_strerror (errno) );
61
62 if (fd < 0) {
63 zlog_warn("Could not create raw socket: errno=%d: %s",
64 errno, safe_strerror(errno));
65 return PIM_SOCK_ERR_SOCKET;
66 }
67
68 return fd;
69 }
70
71 int
72 pim_socket_ip_hdr (int fd)
73 {
74 const int on = 1;
75 int ret;
76
77 if (pimd_privs.change (ZPRIVS_RAISE))
78 zlog_err ("%s: could not raise privs, %s",
79 __PRETTY_FUNCTION__, safe_strerror (errno));
80
81 ret = setsockopt (fd, IPPROTO_IP, IP_HDRINCL, &on, sizeof (on));
82
83 if (pimd_privs.change (ZPRIVS_LOWER))
84 zlog_err ("%s: could not lower privs, %s",
85 __PRETTY_FUNCTION__, safe_strerror (errno));
86
87 return ret;
88 }
89
90 /*
91 * Given a socket and a interface,
92 * Bind that socket to that interface
93 */
94 int
95 pim_socket_bind (int fd, struct interface *ifp)
96 {
97 int ret = 0;
98 #ifdef SO_BINDTODEVICE
99
100 if (pimd_privs.change (ZPRIVS_RAISE))
101 zlog_err ("%s: could not raise privs, %s",
102 __PRETTY_FUNCTION__, safe_strerror (errno));
103
104 ret = setsockopt (fd, SOL_SOCKET,
105 SO_BINDTODEVICE, ifp->name, strlen (ifp->name));
106
107 if (pimd_privs.change (ZPRIVS_LOWER))
108 zlog_err ("%s: could not lower privs, %s",
109 __PRETTY_FUNCTION__, safe_strerror (errno));
110
111 #endif
112 return ret;
113 }
114
115 int pim_socket_mcast(int protocol, struct in_addr ifaddr, int ifindex, u_char loop)
116 {
117 int rcvbuf = 1024 * 1024 * 8;
118 struct ip_mreqn mreq;
119 int fd;
120
121 fd = pim_socket_raw(protocol);
122 if (fd < 0) {
123 zlog_warn("Could not create multicast socket: errno=%d: %s",
124 errno, safe_strerror(errno));
125 return PIM_SOCK_ERR_SOCKET;
126 }
127
128 #ifdef SO_BINDTODEVICE
129 if (protocol == IPPROTO_PIM)
130 {
131 int ret;
132 struct interface *ifp = NULL;
133
134 ifp = if_lookup_by_index_vrf (ifindex, VRF_DEFAULT);
135
136 ret = pim_socket_bind (fd, ifp);
137 if (ret)
138 {
139 zlog_warn("Could not set fd: %d for interface: %s to device",
140 fd, ifp->name);
141 return PIM_SOCK_ERR_BIND;
142 }
143 }
144 #else
145 /* XXX: use IP_PKTINFO / IP_RECVIF to emulate behaviour? Or change to
146 * only use 1 socket for all interfaces? */
147 #endif
148
149 /* Needed to obtain destination address from recvmsg() */
150 {
151 #if defined(HAVE_IP_PKTINFO)
152 /* Linux and Solaris IP_PKTINFO */
153 int opt = 1;
154 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt))) {
155 zlog_warn("Could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
156 fd, errno, safe_strerror(errno));
157 }
158 #elif defined(HAVE_IP_RECVDSTADDR)
159 /* BSD IP_RECVDSTADDR */
160 int opt = 1;
161 if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt))) {
162 zlog_warn("Could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
163 fd, errno, safe_strerror(errno));
164 }
165 #else
166 zlog_err("%s %s: Missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
167 __FILE__, __PRETTY_FUNCTION__);
168 close(fd);
169 return PIM_SOCK_ERR_DSTADDR;
170 #endif
171 }
172
173
174 /* Set router alert (RFC 2113) for all IGMP messages (RFC 3376 4. Message Formats)*/
175 if (protocol == IPPROTO_IGMP) {
176 char ra[4];
177 ra[0] = 148;
178 ra[1] = 4;
179 ra[2] = 0;
180 ra[3] = 0;
181 if (setsockopt(fd, IPPROTO_IP, IP_OPTIONS, ra, 4)) {
182 zlog_warn("Could not set Router Alert Option on socket fd=%d: errno=%d: %s",
183 fd, errno, safe_strerror(errno));
184 close(fd);
185 return PIM_SOCK_ERR_RA;
186 }
187 }
188
189 {
190 int reuse = 1;
191 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
192 (void *) &reuse, sizeof(reuse))) {
193 zlog_warn("Could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
194 fd, errno, safe_strerror(errno));
195 close(fd);
196 return PIM_SOCK_ERR_REUSE;
197 }
198 }
199
200 {
201 const int MTTL = 1;
202 int ttl = MTTL;
203 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
204 (void *) &ttl, sizeof(ttl))) {
205 zlog_warn("Could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
206 MTTL, fd, errno, safe_strerror(errno));
207 close(fd);
208 return PIM_SOCK_ERR_TTL;
209 }
210 }
211
212 if (setsockopt_ipv4_multicast_loop (fd, loop)) {
213 zlog_warn("Could not %s Multicast Loopback Option on socket fd=%d: errno=%d: %s",
214 loop ? "enable" : "disable",
215 fd, errno, safe_strerror(errno));
216 close(fd);
217 return PIM_SOCK_ERR_LOOP;
218 }
219
220 memset (&mreq, 0, sizeof (mreq));
221 mreq.imr_ifindex = ifindex;
222 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
223 (void *) &mreq, sizeof(mreq))) {
224 zlog_warn("Could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
225 fd, errno, safe_strerror(errno));
226 close(fd);
227 return PIM_SOCK_ERR_IFACE;
228 }
229
230 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)))
231 zlog_warn("%s: Failure to set buffer size to %d",
232 __PRETTY_FUNCTION__, rcvbuf);
233
234 {
235 long flags;
236
237 flags = fcntl(fd, F_GETFL, 0);
238 if (flags < 0) {
239 zlog_warn("Could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
240 fd, errno, safe_strerror(errno));
241 close(fd);
242 return PIM_SOCK_ERR_NONBLOCK_GETFL;
243 }
244
245 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
246 zlog_warn("Could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
247 fd, errno, safe_strerror(errno));
248 close(fd);
249 return PIM_SOCK_ERR_NONBLOCK_SETFL;
250 }
251 }
252
253 return fd;
254 }
255
256 int pim_socket_join(int fd, struct in_addr group,
257 struct in_addr ifaddr, ifindex_t ifindex)
258 {
259 int ret;
260
261 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
262 struct ip_mreqn opt;
263 #else
264 struct ip_mreq opt;
265 #endif
266
267 opt.imr_multiaddr = group;
268
269 #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
270 opt.imr_address = ifaddr;
271 opt.imr_ifindex = ifindex;
272 #else
273 opt.imr_interface = ifaddr;
274 #endif
275
276 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &opt, sizeof(opt));
277 if (ret) {
278 char group_str[INET_ADDRSTRLEN];
279 char ifaddr_str[INET_ADDRSTRLEN];
280 if (!inet_ntop(AF_INET, &group, group_str , sizeof(group_str)))
281 sprintf(group_str, "<group?>");
282 if (!inet_ntop(AF_INET, &ifaddr, ifaddr_str , sizeof(ifaddr_str)))
283 sprintf(ifaddr_str, "<ifaddr?>");
284
285 zlog_err("Failure socket joining fd=%d group %s on interface address %s: errno=%d: %s",
286 fd, group_str, ifaddr_str, errno, safe_strerror(errno));
287 return ret;
288 }
289
290 if (PIM_DEBUG_TRACE) {
291 char group_str[INET_ADDRSTRLEN];
292 char ifaddr_str[INET_ADDRSTRLEN];
293 if (!inet_ntop(AF_INET, &group, group_str , sizeof(group_str)))
294 sprintf(group_str, "<group?>");
295 if (!inet_ntop(AF_INET, &ifaddr, ifaddr_str , sizeof(ifaddr_str)))
296 sprintf(ifaddr_str, "<ifaddr?>");
297
298 zlog_debug("Socket fd=%d joined group %s on interface address %s",
299 fd, group_str, ifaddr_str);
300 }
301
302 return ret;
303 }
304
305 int pim_socket_join_source(int fd, ifindex_t ifindex,
306 struct in_addr group_addr,
307 struct in_addr source_addr,
308 const char *ifname)
309 {
310 if (pim_igmp_join_source(fd, ifindex, group_addr, source_addr)) {
311 char group_str[INET_ADDRSTRLEN];
312 char source_str[INET_ADDRSTRLEN];
313 pim_inet4_dump("<grp?>", group_addr, group_str, sizeof(group_str));
314 pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
315 zlog_warn("%s: setsockopt(fd=%d) failure for IGMP group %s source %s ifindex %d on interface %s: errno=%d: %s",
316 __PRETTY_FUNCTION__,
317 fd, group_str, source_str, ifindex, ifname,
318 errno, safe_strerror(errno));
319 return -1;
320 }
321
322 return 0;
323 }
324
325 int pim_socket_recvfromto(int fd, uint8_t *buf, size_t len,
326 struct sockaddr_in *from, socklen_t *fromlen,
327 struct sockaddr_in *to, socklen_t *tolen,
328 ifindex_t *ifindex)
329 {
330 struct msghdr msgh;
331 struct cmsghdr *cmsg;
332 struct iovec iov;
333 char cbuf[1000];
334 int err;
335
336 /*
337 * IP_PKTINFO / IP_RECVDSTADDR don't yield sin_port.
338 * Use getsockname() to get sin_port.
339 */
340 if (to) {
341 struct sockaddr_in si;
342 socklen_t si_len = sizeof(si);
343
344 memset (&si, 0, sizeof (si));
345 to->sin_family = AF_INET;
346
347 pim_socket_getsockname(fd, (struct sockaddr *) &si, &si_len);
348
349 to->sin_port = si.sin_port;
350 to->sin_addr = si.sin_addr;
351
352 if (tolen)
353 *tolen = sizeof(si);
354 }
355
356 memset(&msgh, 0, sizeof(struct msghdr));
357 iov.iov_base = buf;
358 iov.iov_len = len;
359 msgh.msg_control = cbuf;
360 msgh.msg_controllen = sizeof(cbuf);
361 msgh.msg_name = from;
362 msgh.msg_namelen = fromlen ? *fromlen : 0;
363 msgh.msg_iov = &iov;
364 msgh.msg_iovlen = 1;
365 msgh.msg_flags = 0;
366
367 err = recvmsg(fd, &msgh, 0);
368 if (err < 0)
369 return err;
370
371 if (fromlen)
372 *fromlen = msgh.msg_namelen;
373
374 for (cmsg = CMSG_FIRSTHDR(&msgh);
375 cmsg != NULL;
376 cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
377
378 #ifdef HAVE_IP_PKTINFO
379 if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_PKTINFO)) {
380 struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
381 if (to)
382 ((struct sockaddr_in *) to)->sin_addr = i->ipi_addr;
383 if (tolen)
384 *tolen = sizeof(struct sockaddr_in);
385 if (ifindex)
386 *ifindex = i->ipi_ifindex;
387
388 break;
389 }
390 #endif
391
392 #ifdef HAVE_IP_RECVDSTADDR
393 if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_RECVDSTADDR)) {
394 struct in_addr *i = (struct in_addr *) CMSG_DATA(cmsg);
395 if (to)
396 ((struct sockaddr_in *) to)->sin_addr = *i;
397 if (tolen)
398 *tolen = sizeof(struct sockaddr_in);
399
400 break;
401 }
402 #endif
403
404 #if defined(HAVE_IP_RECVIF) && defined(CMSG_IFINDEX)
405 if (cmsg->cmsg_type == IP_RECVIF)
406 if (ifindex)
407 *ifindex = CMSG_IFINDEX(cmsg);
408 #endif
409
410 } /* for (cmsg) */
411
412 return err; /* len */
413 }
414
415 int pim_socket_mcastloop_get(int fd)
416 {
417 int loop;
418 socklen_t loop_len = sizeof(loop);
419
420 if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
421 &loop, &loop_len)) {
422 int e = errno;
423 zlog_warn("Could not get Multicast Loopback Option on socket fd=%d: errno=%d: %s",
424 fd, errno, safe_strerror(errno));
425 errno = e;
426 return PIM_SOCK_ERR_LOOP;
427 }
428
429 return loop;
430 }
431
432 int pim_socket_getsockname(int fd, struct sockaddr *name, socklen_t *namelen)
433 {
434 if (getsockname(fd, name, namelen)) {
435 int e = errno;
436 zlog_warn("Could not get Socket Name for socket fd=%d: errno=%d: %s",
437 fd, errno, safe_strerror(errno));
438 errno = e;
439 return PIM_SOCK_ERR_NAME;
440 }
441
442 return PIM_SOCK_ERR_NONE;
443 }