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