2004-05-31 13:25:00 +00:00
|
|
|
/*
|
2014-05-18 09:42:26 +00:00
|
|
|
* BIRD Internet Routing Daemon -- BSD Multicasting and Network Includes
|
2004-05-31 13:25:00 +00:00
|
|
|
*
|
|
|
|
* (c) 2004 Ondrej Filip <feela@network.cz>
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <netinet/in_systm.h> // Workaround for some BSDs
|
|
|
|
#include <netinet/ip.h>
|
2017-01-17 12:21:25 +00:00
|
|
|
#include <sys/param.h>
|
2014-05-18 09:42:26 +00:00
|
|
|
|
|
|
|
|
2018-07-28 14:54:06 +00:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
/* Should be defined in sysdep/cf/bsd.h, but it is flavor-specific */
|
|
|
|
#define CONFIG_DONTROUTE_UNICAST
|
|
|
|
#endif
|
|
|
|
|
2013-07-11 11:50:44 +00:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
|
|
|
|
#ifndef IP_RECVTTL
|
|
|
|
#define IP_RECVTTL 23
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef IP_MINTTL
|
|
|
|
#define IP_MINTTL 24
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-08-08 12:10:31 +00:00
|
|
|
#ifdef __DragonFly__
|
|
|
|
#define TCP_MD5SIG TCP_SIGNATURE_ENABLE
|
|
|
|
#endif
|
2013-07-11 11:50:44 +00:00
|
|
|
|
2004-05-31 13:25:00 +00:00
|
|
|
|
2016-11-01 15:18:27 +00:00
|
|
|
#undef SA_LEN
|
2014-05-18 09:42:26 +00:00
|
|
|
#define SA_LEN(x) (x).sa.sa_len
|
2010-02-11 09:23:35 +00:00
|
|
|
|
2004-05-31 13:25:00 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
/*
|
|
|
|
* BSD IPv4 multicast syscalls
|
|
|
|
*/
|
2010-02-11 09:23:35 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
#define INIT_MREQ4(maddr,ifa) \
|
2017-12-07 12:06:01 +00:00
|
|
|
{ .imr_multiaddr = ipa_to_in4(maddr), .imr_interface = ip4_to_in4(ifa->sysdep) }
|
2010-02-11 09:23:35 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
static inline int
|
|
|
|
sk_setup_multicast4(sock *s)
|
2004-05-31 13:25:00 +00:00
|
|
|
{
|
2017-12-07 12:06:01 +00:00
|
|
|
struct in_addr ifa = ip4_to_in4(s->iface->sysdep);
|
2014-05-18 09:42:26 +00:00
|
|
|
u8 ttl = s->ttl;
|
|
|
|
u8 n = 0;
|
2004-05-31 13:25:00 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
/* This defines where should we send _outgoing_ multicasts */
|
|
|
|
if (setsockopt(s->fd, IPPROTO_IP, IP_MULTICAST_IF, &ifa, sizeof(ifa)) < 0)
|
|
|
|
ERR("IP_MULTICAST_IF");
|
2004-05-31 13:25:00 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
if (setsockopt(s->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
|
|
|
|
ERR("IP_MULTICAST_TTL");
|
2009-09-04 09:06:51 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
if (setsockopt(s->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &n, sizeof(n)) < 0)
|
|
|
|
ERR("IP_MULTICAST_LOOP");
|
2004-05-31 13:25:00 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
return 0;
|
2004-05-31 13:25:00 +00:00
|
|
|
}
|
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
static inline int
|
|
|
|
sk_join_group4(sock *s, ip_addr maddr)
|
2004-05-31 13:25:00 +00:00
|
|
|
{
|
2014-05-18 09:42:26 +00:00
|
|
|
struct ip_mreq mr = INIT_MREQ4(maddr, s->iface);
|
2004-05-31 13:25:00 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
if (setsockopt(s->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mr, sizeof(mr)) < 0)
|
|
|
|
ERR("IP_ADD_MEMBERSHIP");
|
2004-05-31 13:25:00 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
return 0;
|
2009-09-04 09:06:51 +00:00
|
|
|
}
|
2004-05-31 13:25:00 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
static inline int
|
|
|
|
sk_leave_group4(sock *s, ip_addr maddr)
|
2009-09-04 09:06:51 +00:00
|
|
|
{
|
2014-05-18 09:42:26 +00:00
|
|
|
struct ip_mreq mr = INIT_MREQ4(maddr, s->iface);
|
2004-05-31 13:25:00 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
if (setsockopt(s->fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mr, sizeof(mr)) < 0)
|
|
|
|
ERR("IP_ADD_MEMBERSHIP");
|
2009-09-04 09:06:51 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
return 0;
|
2004-05-31 13:25:00 +00:00
|
|
|
}
|
|
|
|
|
2010-02-11 09:23:35 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
/*
|
|
|
|
* BSD IPv4 packet control messages
|
|
|
|
*/
|
2010-02-11 09:23:35 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
/* It uses IP_RECVDSTADDR / IP_RECVIF socket options instead of IP_PKTINFO */
|
2010-02-11 09:23:35 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
#define CMSG4_SPACE_PKTINFO (CMSG_SPACE(sizeof(struct in_addr)) + \
|
|
|
|
CMSG_SPACE(sizeof(struct sockaddr_dl)))
|
|
|
|
#define CMSG4_SPACE_TTL CMSG_SPACE(sizeof(char))
|
2013-06-25 13:33:00 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
static inline int
|
|
|
|
sk_request_cmsg4_pktinfo(sock *s)
|
|
|
|
{
|
|
|
|
int y = 1;
|
2013-06-25 13:33:00 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
if (setsockopt(s->fd, IPPROTO_IP, IP_RECVDSTADDR, &y, sizeof(y)) < 0)
|
|
|
|
ERR("IP_RECVDSTADDR");
|
2010-02-11 09:23:35 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
if (setsockopt(s->fd, IPPROTO_IP, IP_RECVIF, &y, sizeof(y)) < 0)
|
|
|
|
ERR("IP_RECVIF");
|
2010-02-11 09:23:35 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
return 0;
|
2010-02-11 09:23:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
static inline int
|
|
|
|
sk_request_cmsg4_ttl(sock *s)
|
2010-02-11 09:23:35 +00:00
|
|
|
{
|
2014-05-18 09:42:26 +00:00
|
|
|
int y = 1;
|
2013-06-25 13:33:00 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
if (setsockopt(s->fd, IPPROTO_IP, IP_RECVTTL, &y, sizeof(y)) < 0)
|
|
|
|
ERR("IP_RECVTTL");
|
2013-06-25 13:33:00 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-06-25 13:33:00 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
static inline void
|
|
|
|
sk_process_cmsg4_pktinfo(sock *s, struct cmsghdr *cm)
|
|
|
|
{
|
|
|
|
if (cm->cmsg_type == IP_RECVDSTADDR)
|
|
|
|
s->laddr = ipa_from_in4(* (struct in_addr *) CMSG_DATA(cm));
|
2010-02-11 09:23:35 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
if (cm->cmsg_type == IP_RECVIF)
|
|
|
|
s->lifindex = ((struct sockaddr_dl *) CMSG_DATA(cm))->sdl_index;
|
2010-02-11 09:23:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
static inline void
|
|
|
|
sk_process_cmsg4_ttl(sock *s, struct cmsghdr *cm)
|
|
|
|
{
|
|
|
|
if (cm->cmsg_type == IP_RECVTTL)
|
2015-05-19 06:14:04 +00:00
|
|
|
s->rcv_ttl = * (byte *) CMSG_DATA(cm);
|
2014-05-18 09:42:26 +00:00
|
|
|
}
|
2014-02-06 16:46:01 +00:00
|
|
|
|
2016-10-14 13:37:04 +00:00
|
|
|
#ifdef IP_SENDSRCADDR
|
2014-02-06 16:46:01 +00:00
|
|
|
static inline void
|
2014-05-18 09:42:26 +00:00
|
|
|
sk_prepare_cmsgs4(sock *s, struct msghdr *msg, void *cbuf, size_t cbuflen)
|
2010-02-11 09:23:35 +00:00
|
|
|
{
|
2014-05-18 09:42:26 +00:00
|
|
|
/* Unfortunately, IP_SENDSRCADDR does not work for raw IP sockets on BSD kernels */
|
|
|
|
|
2010-02-11 09:23:35 +00:00
|
|
|
struct cmsghdr *cm;
|
|
|
|
struct in_addr *sa;
|
2014-06-26 11:30:27 +00:00
|
|
|
int controllen = 0;
|
2010-02-11 09:23:35 +00:00
|
|
|
|
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);
|
|
|
|
cm->cmsg_level = IPPROTO_IP;
|
|
|
|
cm->cmsg_type = IP_SENDSRCADDR;
|
|
|
|
cm->cmsg_len = CMSG_LEN(sizeof(*sa));
|
2014-06-26 11:30:27 +00:00
|
|
|
controllen += CMSG_SPACE(sizeof(*sa));
|
2010-02-11 09:23:35 +00:00
|
|
|
|
|
|
|
sa = (struct in_addr *) CMSG_DATA(cm);
|
2014-05-18 09:42:26 +00:00
|
|
|
*sa = ipa_to_in4(s->saddr);
|
2010-02-11 09:23:35 +00:00
|
|
|
|
2014-06-26 11:30:27 +00:00
|
|
|
msg->msg_controllen = controllen;
|
2014-02-06 16:46:01 +00:00
|
|
|
}
|
2016-10-14 13:37:04 +00:00
|
|
|
#else
|
|
|
|
static inline void
|
|
|
|
sk_prepare_cmsgs4(sock *s UNUSED, struct msghdr *msg UNUSED, void *cbuf UNUSED, size_t cbuflen UNUSED) { }
|
|
|
|
#endif
|
2014-02-06 16:46:01 +00:00
|
|
|
|
2016-10-14 13:37:04 +00:00
|
|
|
static void UNUSED
|
2014-05-18 09:42:26 +00:00
|
|
|
sk_prepare_ip_header(sock *s, void *hdr, int dlen)
|
2014-02-06 16:46:01 +00:00
|
|
|
{
|
|
|
|
struct ip *ip = hdr;
|
|
|
|
|
|
|
|
bzero(ip, 20);
|
|
|
|
|
|
|
|
ip->ip_v = 4;
|
|
|
|
ip->ip_hl = 5;
|
|
|
|
ip->ip_tos = (s->tos < 0) ? 0 : s->tos;
|
|
|
|
ip->ip_len = 20 + dlen;
|
|
|
|
ip->ip_ttl = (s->ttl < 0) ? 64 : s->ttl;
|
|
|
|
ip->ip_p = s->dport;
|
2014-05-18 09:42:26 +00:00
|
|
|
ip->ip_src = ipa_to_in4(s->saddr);
|
|
|
|
ip->ip_dst = ipa_to_in4(s->daddr);
|
2014-02-06 16:46:01 +00:00
|
|
|
|
2017-01-17 12:21:25 +00:00
|
|
|
#if (defined __OpenBSD__) || (defined __DragonFly__) || (defined __FreeBSD__ && (__FreeBSD_version >= 1100030))
|
|
|
|
/* Different BSDs have different expectations of ip_len endianity */
|
2014-02-06 16:46:01 +00:00
|
|
|
ip->ip_len = htons(ip->ip_len);
|
|
|
|
#endif
|
2010-02-11 09:23:35 +00:00
|
|
|
}
|
|
|
|
|
2009-05-04 16:17:46 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
/*
|
|
|
|
* Miscellaneous BSD socket syscalls
|
|
|
|
*/
|
2009-05-04 16:17:46 +00:00
|
|
|
|
|
|
|
#ifndef TCP_KEYLEN_MAX
|
|
|
|
#define TCP_KEYLEN_MAX 80
|
|
|
|
#endif
|
2016-04-13 12:30:28 +00:00
|
|
|
|
2009-05-04 16:17:46 +00:00
|
|
|
#ifndef TCP_SIG_SPI
|
|
|
|
#define TCP_SIG_SPI 0x1000
|
|
|
|
#endif
|
|
|
|
|
2016-04-13 12:30:28 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
#define USE_MD5SIG_SETKEY
|
2016-05-17 11:19:18 +00:00
|
|
|
#include "sysdep/bsd/setkey.h"
|
2016-04-13 12:30:28 +00:00
|
|
|
#endif
|
2009-05-04 16:17:46 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
int
|
2020-04-08 20:25:15 +00:00
|
|
|
sk_set_md5_auth(sock *s, ip_addr local UNUSED, ip_addr remote UNUSED, int pxlen UNUSED, struct iface *ifa UNUSED, const char *passwd, int setkey UNUSED)
|
2009-05-04 16:17:46 +00:00
|
|
|
{
|
2016-04-13 12:30:28 +00:00
|
|
|
#ifdef USE_MD5SIG_SETKEY
|
|
|
|
if (setkey)
|
2020-02-27 15:16:48 +00:00
|
|
|
if (sk_set_md5_in_sasp_db(s, local, remote, pxlen, ifa, passwd) < 0)
|
2016-04-13 12:30:28 +00:00
|
|
|
return -1;
|
|
|
|
#endif
|
2014-05-18 09:42:26 +00:00
|
|
|
|
2016-04-13 12:30:28 +00:00
|
|
|
int enable = (passwd && *passwd) ? TCP_SIG_SPI : 0;
|
2014-05-18 09:42:26 +00:00
|
|
|
if (setsockopt(s->fd, IPPROTO_TCP, TCP_MD5SIG, &enable, sizeof(enable)) < 0)
|
|
|
|
{
|
|
|
|
if (errno == ENOPROTOOPT)
|
|
|
|
ERR_MSG("Kernel does not support TCP MD5 signatures");
|
|
|
|
else
|
|
|
|
ERR("TCP_MD5SIG");
|
|
|
|
}
|
2011-08-16 21:05:35 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-08-16 21:05:35 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
static inline int
|
2011-08-16 21:05:35 +00:00
|
|
|
sk_set_min_ttl4(sock *s, int ttl)
|
|
|
|
{
|
|
|
|
if (setsockopt(s->fd, IPPROTO_IP, IP_MINTTL, &ttl, sizeof(ttl)) < 0)
|
|
|
|
{
|
|
|
|
if (errno == ENOPROTOOPT)
|
2014-05-18 09:42:26 +00:00
|
|
|
ERR_MSG("Kernel does not support IPv4 TTL security");
|
2011-08-16 21:05:35 +00:00
|
|
|
else
|
2014-05-18 09:42:26 +00:00
|
|
|
ERR("IP_MINTTL");
|
2011-08-16 21:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
static inline int
|
2016-10-14 13:37:04 +00:00
|
|
|
sk_set_min_ttl6(sock *s, int ttl UNUSED)
|
2011-08-16 21:05:35 +00:00
|
|
|
{
|
2014-05-18 09:42:26 +00:00
|
|
|
ERR_MSG("Kernel does not support IPv6 TTL security");
|
2011-08-16 21:05:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
static inline int
|
2016-10-14 13:37:04 +00:00
|
|
|
sk_disable_mtu_disc4(sock *s UNUSED)
|
2014-05-18 09:42:26 +00:00
|
|
|
{
|
|
|
|
/* TODO: Set IP_DONTFRAG to 0 ? */
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-16 21:05:35 +00:00
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
static inline int
|
2016-10-14 13:37:04 +00:00
|
|
|
sk_disable_mtu_disc6(sock *s UNUSED)
|
2014-05-18 09:42:26 +00:00
|
|
|
{
|
|
|
|
/* TODO: Set IPV6_DONTFRAG to 0 ? */
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-24 14:37:30 +00:00
|
|
|
|
|
|
|
int sk_priority_control = -1;
|
|
|
|
|
2014-05-18 09:42:26 +00:00
|
|
|
static inline int
|
2013-06-24 14:37:30 +00:00
|
|
|
sk_set_priority(sock *s, int prio UNUSED)
|
|
|
|
{
|
2014-05-18 09:42:26 +00:00
|
|
|
ERR_MSG("Socket priority not supported");
|
2013-06-24 14:37:30 +00:00
|
|
|
}
|
2022-01-08 17:31:56 +00:00
|
|
|
|
|
|
|
static inline int
|
|
|
|
sk_set_freebind(sock *s)
|
|
|
|
{
|
|
|
|
ERR_MSG("Freebind is not supported");
|
|
|
|
}
|