1999-12-16 21:06:13 +08:00
|
|
|
/*
|
|
|
|
* BIRD Internet Routing Daemon -- Linux Multicasting and Network Includes
|
|
|
|
*
|
2000-04-21 07:05:41 +08:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1999-12-16 21:06:13 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2010-04-02 22:11:46 +08:00
|
|
|
#include <net/if.h>
|
|
|
|
|
1999-12-16 21:06:13 +08:00
|
|
|
#ifdef IPV6
|
1999-12-16 21:14:02 +08:00
|
|
|
|
2000-05-07 05:46:09 +08:00
|
|
|
#ifndef IPV6_UNICAST_HOPS
|
|
|
|
/* Needed on glibc 2.0 systems */
|
2000-04-21 07:05:41 +08:00
|
|
|
#include <linux/in6.h>
|
2000-05-07 05:46:09 +08:00
|
|
|
#define CONFIG_IPV6_GLIBC_20
|
2000-04-21 07:05:41 +08:00
|
|
|
#endif
|
1999-12-16 21:06:13 +08:00
|
|
|
|
1999-12-16 21:14:02 +08:00
|
|
|
static inline void
|
|
|
|
set_inaddr(struct in6_addr *ia, ip_addr a)
|
|
|
|
{
|
|
|
|
ipa_hton(a);
|
|
|
|
memcpy(ia, &a, sizeof(a));
|
|
|
|
}
|
|
|
|
|
2010-02-11 17:23:35 +08:00
|
|
|
static inline void
|
|
|
|
get_inaddr(ip_addr *a, struct in6_addr *ia)
|
|
|
|
{
|
|
|
|
memcpy(a, ia, sizeof(*a));
|
|
|
|
ipa_ntoh(*a);
|
|
|
|
}
|
|
|
|
|
2010-04-02 22:11:46 +08:00
|
|
|
static inline char *
|
|
|
|
sysio_bind_to_iface(sock *s)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
strcpy(ifr.ifr_name, s->iface->name);
|
|
|
|
if (setsockopt(s->fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0)
|
|
|
|
return "SO_BINDTODEVICE";
|
1999-12-16 21:06:13 +08:00
|
|
|
|
2010-04-02 22:11:46 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2000-05-12 00:30:56 +08:00
|
|
|
|
1999-12-16 21:14:02 +08:00
|
|
|
static inline void
|
|
|
|
set_inaddr(struct in_addr *ia, ip_addr a)
|
|
|
|
{
|
|
|
|
ipa_hton(a);
|
|
|
|
memcpy(&ia->s_addr, &a, sizeof(a));
|
|
|
|
}
|
|
|
|
|
2010-02-11 17:23:35 +08:00
|
|
|
static inline void
|
|
|
|
get_inaddr(ip_addr *a, struct in_addr *ia)
|
|
|
|
{
|
|
|
|
memcpy(a, &ia->s_addr, sizeof(*a));
|
|
|
|
ipa_ntoh(*a);
|
|
|
|
}
|
|
|
|
|
1999-12-16 21:06:13 +08: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 17:06:51 +08:00
|
|
|
|
2012-05-01 04:21:52 +08:00
|
|
|
static inline void fill_mreqn(struct ip_mreqn *m, struct iface *ifa, ip_addr saddr, ip_addr maddr)
|
1999-12-16 21:06:13 +08:00
|
|
|
{
|
2009-09-04 17:06:51 +08:00
|
|
|
bzero(m, sizeof(*m));
|
|
|
|
m->imr_ifindex = ifa->index;
|
2010-03-14 23:36:59 +08:00
|
|
|
set_inaddr(&m->imr_address, saddr);
|
2009-09-04 17:06:51 +08:00
|
|
|
set_inaddr(&m->imr_multiaddr, maddr);
|
1999-12-16 21:06:13 +08:00
|
|
|
}
|
|
|
|
|
2009-09-04 17:06:51 +08:00
|
|
|
static inline char *
|
|
|
|
sysio_setup_multicast(sock *s)
|
1999-12-16 21:06:13 +08:00
|
|
|
{
|
2012-05-01 04:21:52 +08:00
|
|
|
struct ip_mreqn m;
|
2009-09-04 17:06:51 +08:00
|
|
|
int zero = 0;
|
1999-12-16 21:06:13 +08:00
|
|
|
|
2009-09-04 17:06:51 +08: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 */
|
2012-05-01 04:21:52 +08:00
|
|
|
fill_mreqn(&m, s->iface, s->saddr, IPA_NONE);
|
2009-09-04 17:06:51 +08:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_IF, &m, sizeof(m)) < 0)
|
|
|
|
return "IP_MULTICAST_IF";
|
|
|
|
|
2012-05-01 04:21:52 +08:00
|
|
|
/* Is this necessary? */
|
|
|
|
struct ifreq ifr;
|
|
|
|
strcpy(ifr.ifr_name, s->iface->name);
|
|
|
|
if (setsockopt(s->fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0)
|
|
|
|
return "SO_BINDTODEVICE";
|
2009-09-04 17:06:51 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *
|
|
|
|
sysio_join_group(sock *s, ip_addr maddr)
|
|
|
|
{
|
2012-05-01 04:21:52 +08:00
|
|
|
struct ip_mreqn m;
|
2009-09-04 17:06:51 +08:00
|
|
|
|
1999-12-16 21:06:13 +08:00
|
|
|
/* And this one sets interface for _receiving_ multicasts from */
|
2012-05-01 04:21:52 +08:00
|
|
|
fill_mreqn(&m, s->iface, s->saddr, maddr);
|
2009-09-04 17:06:51 +08:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_ADD_MEMBERSHIP, &m, sizeof(m)) < 0)
|
1999-12-16 21:06:13 +08:00
|
|
|
return "IP_ADD_MEMBERSHIP";
|
2009-09-04 17:06:51 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *
|
|
|
|
sysio_leave_group(sock *s, ip_addr maddr)
|
|
|
|
{
|
2012-05-01 04:21:52 +08:00
|
|
|
struct ip_mreqn m;
|
2009-09-04 17:06:51 +08:00
|
|
|
|
|
|
|
/* And this one sets interface for _receiving_ multicasts from */
|
2012-05-01 04:21:52 +08:00
|
|
|
fill_mreqn(&m, s->iface, s->saddr, maddr);
|
2009-09-04 17:06:51 +08:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_DROP_MEMBERSHIP, &m, sizeof(m)) < 0)
|
|
|
|
return "IP_DROP_MEMBERSHIP";
|
|
|
|
|
1999-12-16 21:06:13 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2008-10-27 05:42:39 +08:00
|
|
|
|
2009-09-04 17:06:51 +08:00
|
|
|
|
2008-10-27 05:42:39 +08:00
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <linux/tcp.h>
|
|
|
|
|
|
|
|
/* For the case that we have older kernel headers */
|
|
|
|
/* 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 19:49:51 +08:00
|
|
|
struct sockaddr_storage tcpm_addr; /* address associated */
|
2008-10-27 05:42:39 +08: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-05 00:17:46 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
sk_set_md5_auth_int(sock *s, sockaddr *sa, char *passwd)
|
|
|
|
{
|
|
|
|
struct tcp_md5sig md5;
|
|
|
|
|
|
|
|
memset(&md5, 0, sizeof(md5));
|
|
|
|
memcpy(&md5.tcpm_addr, (struct sockaddr *) sa, sizeof(*sa));
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rv = setsockopt(s->fd, IPPROTO_TCP, TCP_MD5SIG, &md5, sizeof(md5));
|
|
|
|
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
if (errno == ENOPROTOOPT)
|
|
|
|
log(L_ERR "Kernel does not support TCP MD5 signatures");
|
|
|
|
else
|
|
|
|
log(L_ERR "sk_set_md5_auth_int: setsockopt: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2010-02-11 17:23:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef IPV6
|
|
|
|
|
|
|
|
/* RX/TX packet info handling for IPv4 */
|
|
|
|
/* Mostly similar to standardized IPv6 code */
|
|
|
|
|
|
|
|
#define CMSG_RX_SPACE CMSG_SPACE(sizeof(struct in_pktinfo))
|
|
|
|
#define CMSG_TX_SPACE CMSG_SPACE(sizeof(struct in_pktinfo))
|
|
|
|
|
|
|
|
static char *
|
|
|
|
sysio_register_cmsgs(sock *s)
|
|
|
|
{
|
|
|
|
int ok = 1;
|
|
|
|
if ((s->flags & SKF_LADDR_RX) &&
|
|
|
|
setsockopt(s->fd, IPPROTO_IP, IP_PKTINFO, &ok, sizeof(ok)) < 0)
|
|
|
|
return "IP_PKTINFO";
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sysio_process_rx_cmsgs(sock *s, struct msghdr *msg)
|
|
|
|
{
|
|
|
|
struct cmsghdr *cm;
|
|
|
|
struct in_pktinfo *pi = NULL;
|
|
|
|
|
|
|
|
if (!(s->flags & SKF_LADDR_RX))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (cm = CMSG_FIRSTHDR(msg); cm != NULL; cm = CMSG_NXTHDR(msg, cm))
|
|
|
|
{
|
|
|
|
if (cm->cmsg_level == IPPROTO_IP && cm->cmsg_type == IP_PKTINFO)
|
|
|
|
pi = (struct in_pktinfo *) CMSG_DATA(cm);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pi)
|
|
|
|
{
|
|
|
|
s->laddr = IPA_NONE;
|
|
|
|
s->lifindex = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
get_inaddr(&s->laddr, &pi->ipi_addr);
|
|
|
|
s->lifindex = pi->ipi_ifindex;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-08 05:15:56 +08:00
|
|
|
/*
|
2010-04-02 17:31:20 +08:00
|
|
|
static void
|
|
|
|
sysio_prepare_tx_cmsgs(sock *s, struct msghdr *msg, void *cbuf, size_t cbuflen)
|
2010-02-11 17:23:35 +08:00
|
|
|
{
|
|
|
|
struct cmsghdr *cm;
|
|
|
|
struct in_pktinfo *pi;
|
|
|
|
|
|
|
|
if (!(s->flags & SKF_LADDR_TX))
|
2010-04-02 17:31:20 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
msg->msg_control = cbuf;
|
|
|
|
msg->msg_controllen = cbuflen;
|
2010-02-11 17:23:35 +08:00
|
|
|
|
|
|
|
cm = CMSG_FIRSTHDR(msg);
|
|
|
|
cm->cmsg_level = IPPROTO_IP;
|
|
|
|
cm->cmsg_type = IP_PKTINFO;
|
|
|
|
cm->cmsg_len = CMSG_LEN(sizeof(*pi));
|
|
|
|
|
|
|
|
pi = (struct in_pktinfo *) CMSG_DATA(cm);
|
|
|
|
set_inaddr(&pi->ipi_spec_dst, s->saddr);
|
|
|
|
pi->ipi_ifindex = s->iface ? s->iface->index : 0;
|
|
|
|
|
|
|
|
msg->msg_controllen = cm->cmsg_len;
|
|
|
|
}
|
2010-04-08 05:15:56 +08:00
|
|
|
*/
|
|
|
|
|
2010-02-11 17:23:35 +08:00
|
|
|
#endif
|
2011-08-17 05:05:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef IP_MINTTL
|
|
|
|
#define IP_MINTTL 21
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef IPV6_MINHOPCOUNT
|
|
|
|
#define IPV6_MINHOPCOUNT 73
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef IPV6
|
|
|
|
|
|
|
|
static int
|
|
|
|
sk_set_min_ttl4(sock *s, int ttl)
|
|
|
|
{
|
|
|
|
if (setsockopt(s->fd, IPPROTO_IP, IP_MINTTL, &ttl, sizeof(ttl)) < 0)
|
|
|
|
{
|
|
|
|
if (errno == ENOPROTOOPT)
|
|
|
|
log(L_ERR "Kernel does not support IPv4 TTL security");
|
|
|
|
else
|
|
|
|
log(L_ERR "sk_set_min_ttl4: setsockopt: %m");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static int
|
|
|
|
sk_set_min_ttl6(sock *s, int ttl)
|
|
|
|
{
|
|
|
|
if (setsockopt(s->fd, IPPROTO_IPV6, IPV6_MINHOPCOUNT, &ttl, sizeof(ttl)) < 0)
|
|
|
|
{
|
|
|
|
if (errno == ENOPROTOOPT)
|
|
|
|
log(L_ERR "Kernel does not support IPv6 TTL security");
|
|
|
|
else
|
2012-05-05 05:05:47 +08:00
|
|
|
log(L_ERR "sk_set_min_ttl6: setsockopt: %m");
|
2011-08-17 05:05:35 +08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|