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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef HAVE_STRUCT_IP_MREQN
|
|
|
|
/* Several versions of glibc don't define this structure, so we have to do it ourselves */
|
|
|
|
struct ip_mreqn
|
|
|
|
{
|
2014-05-18 17:42:26 +08:00
|
|
|
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 */
|
1999-12-16 21:06:13 +08:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
#ifndef IP_MINTTL
|
|
|
|
#define IP_MINTTL 21
|
|
|
|
#endif
|
2009-09-04 17:06:51 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
#ifndef IPV6_TCLASS
|
|
|
|
#define IPV6_TCLASS 67
|
|
|
|
#endif
|
1999-12-16 21:06:13 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
#ifndef IPV6_MINHOPCOUNT
|
|
|
|
#define IPV6_MINHOPCOUNT 73
|
|
|
|
#endif
|
1999-12-16 21:06:13 +08:00
|
|
|
|
2009-09-04 17:06:51 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
#ifndef TCP_MD5SIG
|
2009-09-04 17:06:51 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
#define TCP_MD5SIG 14
|
|
|
|
#define TCP_MD5SIG_MAXKEYLEN 80
|
2009-09-04 17:06:51 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
struct tcp_md5sig {
|
|
|
|
struct sockaddr_storage tcpm_addr; /* address associated */
|
|
|
|
u16 __tcpm_pad1; /* zero */
|
|
|
|
u16 tcpm_keylen; /* key length */
|
|
|
|
u32 __tcpm_pad2; /* zero */
|
|
|
|
u8 tcpm_key[TCP_MD5SIG_MAXKEYLEN]; /* key (binary) */
|
|
|
|
};
|
2009-09-04 17:06:51 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
#endif
|
2009-09-04 17:06:51 +08:00
|
|
|
|
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
/* Linux does not care if sa_len is larger than needed */
|
|
|
|
#define SA_LEN(x) sizeof(sockaddr)
|
2009-09-04 17:06:51 +08:00
|
|
|
|
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
/*
|
|
|
|
* Linux IPv4 multicast syscalls
|
|
|
|
*/
|
2009-09-04 17:06:51 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
#define INIT_MREQ4(maddr,ifa) \
|
|
|
|
{ .imr_multiaddr = ipa_to_in4(maddr), .imr_ifindex = ifa->index }
|
1999-12-16 21:06:13 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
static inline int
|
|
|
|
sk_setup_multicast4(sock *s)
|
|
|
|
{
|
|
|
|
struct ip_mreqn mr = { .imr_ifindex = s->iface->index };
|
|
|
|
int ttl = s->ttl;
|
|
|
|
int n = 0;
|
2008-10-27 05:42:39 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
/* This defines where should we send _outgoing_ multicasts */
|
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_IF, &mr, sizeof(mr)) < 0)
|
|
|
|
ERR("IP_MULTICAST_IF");
|
2009-09-04 17:06:51 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
|
|
|
|
ERR("IP_MULTICAST_TTL");
|
2008-10-27 05:42:39 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_LOOP, &n, sizeof(n)) < 0)
|
|
|
|
ERR("IP_MULTICAST_LOOP");
|
2008-10-27 05:42:39 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2008-10-27 05:42:39 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
static inline int
|
|
|
|
sk_join_group4(sock *s, ip_addr maddr)
|
|
|
|
{
|
|
|
|
struct ip_mreqn mr = INIT_MREQ4(maddr, s->iface);
|
2014-03-31 19:21:13 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_ADD_MEMBERSHIP, &mr, sizeof(mr)) < 0)
|
|
|
|
ERR("IP_ADD_MEMBERSHIP");
|
2008-10-27 05:42:39 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-05-05 00:17:46 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
static inline int
|
|
|
|
sk_leave_group4(sock *s, ip_addr maddr)
|
2009-05-05 00:17:46 +08:00
|
|
|
{
|
2014-05-18 17:42:26 +08:00
|
|
|
struct ip_mreqn mr = INIT_MREQ4(maddr, s->iface);
|
2009-05-05 00:17:46 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_DROP_MEMBERSHIP, &mr, sizeof(mr)) < 0)
|
|
|
|
ERR("IP_DROP_MEMBERSHIP");
|
2009-05-05 00:17:46 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
return 0;
|
2009-05-05 00:17:46 +08:00
|
|
|
}
|
2010-02-11 17:23:35 +08:00
|
|
|
|
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
/*
|
|
|
|
* Linux IPv4 packet control messages
|
|
|
|
*/
|
2010-02-11 17:23:35 +08:00
|
|
|
|
|
|
|
/* Mostly similar to standardized IPv6 code */
|
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
#define CMSG4_SPACE_PKTINFO CMSG_SPACE(sizeof(struct in_pktinfo))
|
|
|
|
#define CMSG4_SPACE_TTL CMSG_SPACE(sizeof(int))
|
2010-02-11 17:23:35 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
static inline int
|
|
|
|
sk_request_cmsg4_pktinfo(sock *s)
|
2010-02-11 17:23:35 +08:00
|
|
|
{
|
2014-05-18 17:42:26 +08:00
|
|
|
int y = 1;
|
2013-06-25 21:33:00 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_PKTINFO, &y, sizeof(y)) < 0)
|
|
|
|
ERR("IP_PKTINFO");
|
2010-02-11 17:23:35 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
return 0;
|
2010-02-11 17:23:35 +08:00
|
|
|
}
|
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
static inline int
|
|
|
|
sk_request_cmsg4_ttl(sock *s)
|
2010-02-11 17:23:35 +08:00
|
|
|
{
|
2014-05-18 17:42:26 +08:00
|
|
|
int y = 1;
|
2010-02-11 17:23:35 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_RECVTTL, &y, sizeof(y)) < 0)
|
|
|
|
ERR("IP_RECVTTL");
|
2013-06-25 21:33:00 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-06-25 21:33:00 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
static inline void
|
|
|
|
sk_process_cmsg4_pktinfo(sock *s, struct cmsghdr *cm)
|
|
|
|
{
|
|
|
|
if (cm->cmsg_type == IP_PKTINFO)
|
2013-06-25 21:33:00 +08:00
|
|
|
{
|
2014-05-18 17:42:26 +08:00
|
|
|
struct in_pktinfo *pi = (struct in_pktinfo *) CMSG_DATA(cm);
|
|
|
|
s->laddr = ipa_from_in4(pi->ipi_addr);
|
|
|
|
s->lifindex = pi->ipi_ifindex;
|
2013-06-25 21:33:00 +08:00
|
|
|
}
|
2014-05-18 17:42:26 +08:00
|
|
|
}
|
2013-06-25 21:33:00 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
static inline void
|
|
|
|
sk_process_cmsg4_ttl(sock *s, struct cmsghdr *cm)
|
|
|
|
{
|
|
|
|
if (cm->cmsg_type == IP_TTL)
|
|
|
|
s->rcv_ttl = * (int *) CMSG_DATA(cm);
|
2010-02-11 17:23:35 +08:00
|
|
|
}
|
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
static inline void
|
|
|
|
sk_prepare_cmsgs4(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;
|
|
|
|
|
2010-04-02 17:31:20 +08:00
|
|
|
msg->msg_control = cbuf;
|
|
|
|
msg->msg_controllen = cbuflen;
|
2010-02-11 17:23:35 +08:00
|
|
|
|
|
|
|
cm = CMSG_FIRSTHDR(msg);
|
2014-02-07 00:46:01 +08:00
|
|
|
cm->cmsg_level = SOL_IP;
|
2010-02-11 17:23:35 +08: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-18 17:42:26 +08:00
|
|
|
pi->ipi_spec_dst = ipa_to_in4(s->saddr);
|
|
|
|
pi->ipi_addr = ipa_to_in4(IPA_NONE);
|
2010-02-11 17:23:35 +08:00
|
|
|
|
|
|
|
msg->msg_controllen = cm->cmsg_len;
|
|
|
|
}
|
2014-02-07 00:46:01 +08:00
|
|
|
|
2010-04-08 05:15:56 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
/*
|
|
|
|
* Miscellaneous Linux socket syscalls
|
|
|
|
*/
|
2011-08-17 05:05:35 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
int
|
|
|
|
sk_set_md5_auth(sock *s, ip_addr a, struct iface *ifa, char *passwd)
|
|
|
|
{
|
|
|
|
struct tcp_md5sig md5;
|
2011-08-17 05:05:35 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
memset(&md5, 0, sizeof(md5));
|
|
|
|
sockaddr_fill((sockaddr *) &md5.tcpm_addr, s->af, a, ifa, 0);
|
2011-08-17 05:05:35 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
if (passwd)
|
|
|
|
{
|
|
|
|
int len = strlen(passwd);
|
|
|
|
|
|
|
|
if (len > TCP_MD5SIG_MAXKEYLEN)
|
|
|
|
ERR_MSG("MD5 password too long");
|
2011-08-17 05:05:35 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
md5.tcpm_keylen = len;
|
|
|
|
memcpy(&md5.tcpm_key, passwd, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setsockopt(s->fd, SOL_TCP, TCP_MD5SIG, &md5, sizeof(md5)) < 0)
|
|
|
|
{
|
|
|
|
if (errno == ENOPROTOOPT)
|
|
|
|
ERR_MSG("Kernel does not support TCP MD5 signatures");
|
|
|
|
else
|
|
|
|
ERR("TCP_MD5SIG");
|
|
|
|
}
|
2011-08-17 05:05:35 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2011-08-17 05:05:35 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
static inline int
|
2011-08-17 05:05:35 +08:00
|
|
|
sk_set_min_ttl4(sock *s, int ttl)
|
|
|
|
{
|
2014-02-07 00:46:01 +08:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_MINTTL, &ttl, sizeof(ttl)) < 0)
|
2011-08-17 05:05:35 +08:00
|
|
|
{
|
|
|
|
if (errno == ENOPROTOOPT)
|
2014-05-18 17:42:26 +08:00
|
|
|
ERR_MSG("Kernel does not support IPv4 TTL security");
|
2011-08-17 05:05:35 +08:00
|
|
|
else
|
2014-05-18 17:42:26 +08:00
|
|
|
ERR("IP_MINTTL");
|
2011-08-17 05:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
static inline int
|
2011-08-17 05:05:35 +08:00
|
|
|
sk_set_min_ttl6(sock *s, int ttl)
|
|
|
|
{
|
2014-02-07 00:46:01 +08:00
|
|
|
if (setsockopt(s->fd, SOL_IPV6, IPV6_MINHOPCOUNT, &ttl, sizeof(ttl)) < 0)
|
2011-08-17 05:05:35 +08:00
|
|
|
{
|
|
|
|
if (errno == ENOPROTOOPT)
|
2014-05-18 17:42:26 +08:00
|
|
|
ERR_MSG("Kernel does not support IPv6 TTL security");
|
2011-08-17 05:05:35 +08:00
|
|
|
else
|
2014-05-18 17:42:26 +08:00
|
|
|
ERR("IPV6_MINHOPCOUNT");
|
2011-08-17 05:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
static inline int
|
|
|
|
sk_disable_mtu_disc4(sock *s)
|
|
|
|
{
|
|
|
|
int dont = IP_PMTUDISC_DONT;
|
2013-06-24 22:37:30 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
if (setsockopt(s->fd, SOL_IP, IP_MTU_DISCOVER, &dont, sizeof(dont)) < 0)
|
|
|
|
ERR("IP_MTU_DISCOVER");
|
2013-06-24 22:37:30 +08:00
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
sk_disable_mtu_disc6(sock *s)
|
|
|
|
{
|
|
|
|
int dont = IPV6_PMTUDISC_DONT;
|
|
|
|
|
|
|
|
if (setsockopt(s->fd, SOL_IPV6, IPV6_MTU_DISCOVER, &dont, sizeof(dont)) < 0)
|
|
|
|
ERR("IPV6_MTU_DISCOVER");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-24 22:37:30 +08:00
|
|
|
|
|
|
|
int sk_priority_control = 7;
|
|
|
|
|
2014-05-18 17:42:26 +08:00
|
|
|
static inline int
|
2013-06-24 22:37:30 +08:00
|
|
|
sk_set_priority(sock *s, int prio)
|
|
|
|
{
|
|
|
|
if (setsockopt(s->fd, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)) < 0)
|
2014-05-18 17:42:26 +08:00
|
|
|
ERR("SO_PRIORITY");
|
2013-06-24 22:37:30 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-18 17:42:26 +08:00
|
|
|
|