1999-12-16 13:06:13 +00:00
|
|
|
/*
|
|
|
|
* BIRD Internet Routing Daemon -- Linux Multicasting and Network Includes
|
|
|
|
*
|
2000-04-20 23:05:41 +00:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1999-12-16 13:06:13 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2010-04-02 14:11:46 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
|
1999-12-16 13:14:02 +00:00
|
|
|
|
2000-05-06 21:46:09 +00:00
|
|
|
#ifndef IPV6_UNICAST_HOPS
|
|
|
|
/* Needed on glibc 2.0 systems */
|
2000-04-20 23:05:41 +00:00
|
|
|
#include <linux/in6.h>
|
2000-05-06 21:46:09 +00:00
|
|
|
#define CONFIG_IPV6_GLIBC_20
|
2000-04-20 23:05:41 +00:00
|
|
|
#endif
|
1999-12-16 13:06:13 +00:00
|
|
|
|
1999-12-16 13:14:02 +00:00
|
|
|
|
1999-12-16 13:06:13 +00:00
|
|
|
#ifndef HAVE_STRUCT_IP_MREQN
|
|
|
|
/* Several versions of glibc don't define this structure, so we have to do it ourselves */
|
|
|
|
struct ip_mreqn
|
|
|
|
{
|
|
|
|
struct in_addr imr_multiaddr; /* IP multicast address of group */
|
|
|
|
struct in_addr imr_address; /* local IP address of interface */
|
|
|
|
int imr_ifindex; /* Interface index */
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2009-09-04 09:06:51 +00:00
|
|
|
|
2014-02-06 16:46:01 +00:00
|
|
|
static inline void fill_mreqn(struct ip_mreqn *m, ip_addr maddr, struct iface *ifa)
|
1999-12-16 13:06:13 +00:00
|
|
|
{
|
2009-09-04 09:06:51 +00:00
|
|
|
bzero(m, sizeof(*m));
|
|
|
|
m->imr_ifindex = ifa->index;
|
2012-06-22 11:38:52 +00:00
|
|
|
ipa_put_in4(&m->imr_multiaddr, maddr);
|
1999-12-16 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2009-09-04 09:06:51 +00:00
|
|
|
static inline char *
|
2012-06-22 11:38:52 +00:00
|
|
|
sk_setup_multicast4(sock *s)
|
1999-12-16 13:06:13 +00:00
|
|
|
{
|
2012-04-30 20:21:52 +00:00
|
|
|
struct ip_mreqn m;
|
2009-09-04 09:06:51 +00:00
|
|
|
int zero = 0;
|
1999-12-16 13:06:13 +00:00
|
|
|
|
2009-09-04 09:06:51 +00:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_LOOP, &zero, sizeof(zero)) < 0)
|
|
|
|
return "IP_MULTICAST_LOOP";
|
|
|
|
|
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_TTL, &s->ttl, sizeof(s->ttl)) < 0)
|
|
|
|
return "IP_MULTICAST_TTL";
|
|
|
|
|
|
|
|
/* This defines where should we send _outgoing_ multicasts */
|
2014-02-06 16:46:01 +00:00
|
|
|
fill_mreqn(&m, IPA_NONE, s->iface);
|
2009-09-04 09:06:51 +00:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_IF, &m, sizeof(m)) < 0)
|
|
|
|
return "IP_MULTICAST_IF";
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *
|
2012-06-22 11:38:52 +00:00
|
|
|
sk_join_group4(sock *s, ip_addr maddr)
|
2009-09-04 09:06:51 +00:00
|
|
|
{
|
2012-04-30 20:21:52 +00:00
|
|
|
struct ip_mreqn m;
|
2009-09-04 09:06:51 +00:00
|
|
|
|
2014-02-06 16:46:01 +00:00
|
|
|
fill_mreqn(&m, maddr, s->iface);
|
2009-09-04 09:06:51 +00:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_ADD_MEMBERSHIP, &m, sizeof(m)) < 0)
|
1999-12-16 13:06:13 +00:00
|
|
|
return "IP_ADD_MEMBERSHIP";
|
2009-09-04 09:06:51 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *
|
2012-06-22 11:38:52 +00:00
|
|
|
sk_leave_group4(sock *s, ip_addr maddr)
|
2009-09-04 09:06:51 +00:00
|
|
|
{
|
2012-04-30 20:21:52 +00:00
|
|
|
struct ip_mreqn m;
|
2009-09-04 09:06:51 +00:00
|
|
|
|
2014-02-06 16:46:01 +00:00
|
|
|
fill_mreqn(&m, maddr, s->iface);
|
2009-09-04 09:06:51 +00:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_DROP_MEMBERSHIP, &m, sizeof(m)) < 0)
|
|
|
|
return "IP_DROP_MEMBERSHIP";
|
|
|
|
|
1999-12-16 13:06:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-04 09:06:51 +00:00
|
|
|
|
2014-02-06 16:46:01 +00:00
|
|
|
/* For the case that we have older libc headers */
|
2008-10-26 21:42:39 +00:00
|
|
|
/* Copied from Linux kernel file include/linux/tcp.h */
|
|
|
|
|
|
|
|
#ifndef TCP_MD5SIG
|
|
|
|
|
|
|
|
#define TCP_MD5SIG 14
|
|
|
|
#define TCP_MD5SIG_MAXKEYLEN 80
|
|
|
|
|
|
|
|
struct tcp_md5sig {
|
2009-01-16 11:49:51 +00:00
|
|
|
struct sockaddr_storage tcpm_addr; /* address associated */
|
2008-10-26 21:42:39 +00:00
|
|
|
__u16 __tcpm_pad1; /* zero */
|
|
|
|
__u16 tcpm_keylen; /* key length */
|
|
|
|
__u32 __tcpm_pad2; /* zero */
|
|
|
|
__u8 tcpm_key[TCP_MD5SIG_MAXKEYLEN]; /* key (binary) */
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
2009-05-04 16:17:46 +00:00
|
|
|
|
|
|
|
static int
|
2012-06-22 11:38:52 +00:00
|
|
|
sk_set_md5_auth_int(sock *s, struct sockaddr *sa, int sa_len, char *passwd)
|
2009-05-04 16:17:46 +00:00
|
|
|
{
|
|
|
|
struct tcp_md5sig md5;
|
|
|
|
|
|
|
|
memset(&md5, 0, sizeof(md5));
|
2012-06-22 11:38:52 +00:00
|
|
|
memcpy(&md5.tcpm_addr, sa, sa_len);
|
2009-05-04 16:17:46 +00:00
|
|
|
|
|
|
|
if (passwd)
|
|
|
|
{
|
|
|
|
int len = strlen(passwd);
|
|
|
|
|
|
|
|
if (len > TCP_MD5SIG_MAXKEYLEN)
|
|
|
|
{
|
|
|
|
log(L_ERR "MD5 password too long");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
md5.tcpm_keylen = len;
|
|
|
|
memcpy(&md5.tcpm_key, passwd, len);
|
|
|
|
}
|
|
|
|
|
2014-02-06 16:46:01 +00:00
|
|
|
int rv = setsockopt(s->fd, SOL_TCP, TCP_MD5SIG, &md5, sizeof(md5));
|
2009-05-04 16:17:46 +00:00
|
|
|
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
if (errno == ENOPROTOOPT)
|
|
|
|
log(L_ERR "Kernel does not support TCP MD5 signatures");
|
|
|
|
else
|
2012-06-22 11:38:52 +00:00
|
|
|
log(L_ERR "sk_set_md5_auth_int: TCP_MD5SIG: %m");
|
2009-05-04 16:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2010-02-11 09:23:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* RX/TX packet info handling for IPv4 */
|
|
|
|
/* Mostly similar to standardized IPv6 code */
|
|
|
|
|
2013-07-31 14:16:48 +00:00
|
|
|
#define CMSG4_SPACE_PKTINFO CMSG_SPACE(sizeof(struct in_pktinfo))
|
2012-06-22 11:38:52 +00:00
|
|
|
|
2013-07-31 14:16:48 +00:00
|
|
|
static inline char *
|
|
|
|
sk_request_cmsg4_pktinfo(sock *s)
|
2010-02-11 09:23:35 +00:00
|
|
|
{
|
|
|
|
int ok = 1;
|
2013-06-25 13:33:00 +00:00
|
|
|
|
2014-05-02 15:05:23 +00:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_PKTINFO, &ok, sizeof(ok)) < 0)
|
2010-02-11 09:23:35 +00:00
|
|
|
return "IP_PKTINFO";
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-31 14:16:48 +00:00
|
|
|
static inline void
|
|
|
|
sk_process_cmsg4_pktinfo(sock *s, struct cmsghdr *cm)
|
2010-02-11 09:23:35 +00:00
|
|
|
{
|
2013-07-31 14:16:48 +00:00
|
|
|
if ((cm->cmsg_type == IP_PKTINFO) && (s->flags & SKF_LADDR_RX))
|
2013-06-25 13:33:00 +00:00
|
|
|
{
|
2013-07-31 14:16:48 +00:00
|
|
|
struct in_pktinfo *pi = (struct in_pktinfo *) CMSG_DATA(cm);
|
|
|
|
s->laddr = ipa_get_in4(&pi->ipi_addr);
|
|
|
|
s->lifindex = pi->ipi_ifindex;
|
2013-06-25 13:33:00 +00:00
|
|
|
}
|
2013-07-31 14:16:48 +00:00
|
|
|
}
|
2013-06-25 13:33:00 +00:00
|
|
|
|
|
|
|
|
2013-07-31 14:16:48 +00:00
|
|
|
#define CMSG4_SPACE_TTL CMSG_SPACE(sizeof(int))
|
2010-02-11 09:23:35 +00:00
|
|
|
|
2012-06-22 11:38:52 +00:00
|
|
|
static inline char *
|
2013-07-31 14:16:48 +00:00
|
|
|
sk_request_cmsg4_ttl(sock *s)
|
2010-02-11 09:23:35 +00:00
|
|
|
{
|
|
|
|
int ok = 1;
|
2013-07-31 14:16:48 +00:00
|
|
|
|
2014-05-02 15:05:23 +00:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_RECVTTL, &ok, sizeof(ok)) < 0)
|
2013-07-31 14:16:48 +00:00
|
|
|
return "IP_RECVTTL";
|
2010-02-11 09:23:35 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-22 11:38:52 +00:00
|
|
|
static inline void
|
2013-07-31 14:16:48 +00:00
|
|
|
sk_process_cmsg4_ttl(sock *s, struct cmsghdr *cm)
|
2010-02-11 09:23:35 +00:00
|
|
|
{
|
2013-07-31 14:16:48 +00:00
|
|
|
if ((cm->cmsg_type == IP_TTL) && (s->flags & SKF_TTL_RX))
|
|
|
|
s->ttl = * (int *) CMSG_DATA(cm);
|
2010-02-11 09:23:35 +00:00
|
|
|
}
|
|
|
|
|
2013-07-31 14:16:48 +00:00
|
|
|
|
2014-05-02 15:05:23 +00:00
|
|
|
static inline void
|
|
|
|
sk_prepare_cmsgs4(sock *s, struct msghdr *msg, void *cbuf, size_t cbuflen)
|
2010-02-11 09:23:35 +00:00
|
|
|
{
|
|
|
|
struct cmsghdr *cm;
|
|
|
|
struct in_pktinfo *pi;
|
|
|
|
|
2010-04-02 09:31:20 +00:00
|
|
|
msg->msg_control = cbuf;
|
|
|
|
msg->msg_controllen = cbuflen;
|
2010-02-11 09:23:35 +00:00
|
|
|
|
|
|
|
cm = CMSG_FIRSTHDR(msg);
|
2014-02-06 16:46:01 +00:00
|
|
|
cm->cmsg_level = SOL_IP;
|
2010-02-11 09:23:35 +00:00
|
|
|
cm->cmsg_type = IP_PKTINFO;
|
|
|
|
cm->cmsg_len = CMSG_LEN(sizeof(*pi));
|
|
|
|
|
|
|
|
pi = (struct in_pktinfo *) CMSG_DATA(cm);
|
|
|
|
pi->ipi_ifindex = s->iface ? s->iface->index : 0;
|
2014-05-02 15:05:23 +00:00
|
|
|
ipa_put_in4(&pi->ipi_spec_dst, s->saddr);
|
|
|
|
ipa_put_in4(&pi->ipi_addr, IPA_NONE);
|
2010-02-11 09:23:35 +00:00
|
|
|
|
|
|
|
msg->msg_controllen = cm->cmsg_len;
|
|
|
|
}
|
2014-02-06 16:46:01 +00:00
|
|
|
|
2010-04-07 21:15:56 +00:00
|
|
|
|
2011-08-16 21:05:35 +00:00
|
|
|
|
|
|
|
#ifndef IP_MINTTL
|
|
|
|
#define IP_MINTTL 21
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef IPV6_MINHOPCOUNT
|
|
|
|
#define IPV6_MINHOPCOUNT 73
|
|
|
|
#endif
|
|
|
|
|
2013-06-27 22:57:22 +00:00
|
|
|
static inline char *
|
2011-08-16 21:05:35 +00:00
|
|
|
sk_set_min_ttl4(sock *s, int ttl)
|
|
|
|
{
|
2014-02-06 16:46:01 +00:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_MINTTL, &ttl, sizeof(ttl)) < 0)
|
2013-06-27 22:57:22 +00:00
|
|
|
return "IP_MINTTL";
|
2011-08-16 21:05:35 +00:00
|
|
|
|
2013-06-27 22:57:22 +00:00
|
|
|
return NULL;
|
2011-08-16 21:05:35 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 22:57:22 +00:00
|
|
|
static inline char *
|
2011-08-16 21:05:35 +00:00
|
|
|
sk_set_min_ttl6(sock *s, int ttl)
|
|
|
|
{
|
2014-02-06 16:46:01 +00:00
|
|
|
if (setsockopt(s->fd, SOL_IPV6, IPV6_MINHOPCOUNT, &ttl, sizeof(ttl)) < 0)
|
2013-06-27 22:57:22 +00:00
|
|
|
return "IPV6_MINHOPCOUNT";
|
2011-08-16 21:05:35 +00:00
|
|
|
|
2013-06-27 22:57:22 +00:00
|
|
|
return NULL;
|
2011-08-16 21:05:35 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 22:57:22 +00:00
|
|
|
|
2013-06-24 14:37:30 +00:00
|
|
|
#ifndef IPV6_TCLASS
|
|
|
|
#define IPV6_TCLASS 67
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int sk_priority_control = 7;
|
|
|
|
|
|
|
|
static int
|
|
|
|
sk_set_priority(sock *s, int prio)
|
|
|
|
{
|
|
|
|
if (setsockopt(s->fd, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)) < 0)
|
|
|
|
{
|
|
|
|
log(L_WARN "sk_set_priority: setsockopt: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-30 21:48:07 +00:00
|
|
|
|