#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/mbuf.h>
#include <sys/errno.h>
#include <sys/random.h>
#include <sys/kernel_types.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/net_osdep.h>
#include <net/classq/classq.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#if INET6
#include <netinet/ip6.h>
#endif
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <libkern/libkern.h>
#if PF_ECN
u_int8_t
read_dsfield(struct mbuf *m, struct pf_mtag *t)
{
struct mbuf *m0;
u_int8_t ds_field = 0;
if (t->pftag_hdr == NULL ||
!(t->pftag_flags & (PF_TAG_HDR_INET | PF_TAG_HDR_INET6))) {
return (u_int8_t)0;
}
for (m0 = m; m0 != NULL; m0 = m0->m_next) {
if (((caddr_t)t->pftag_hdr >= m0->m_data) &&
((caddr_t)t->pftag_hdr < m0->m_data + m0->m_len)) {
break;
}
}
if (m0 == NULL) {
printf("%s: can't locate header!\n", __func__);
return (u_int8_t)0;
}
if (t->pftag_flags & PF_TAG_HDR_INET) {
struct ip *ip = (struct ip *)(void *)t->pftag_hdr;
if (((uintptr_t)ip + sizeof(*ip)) >
((uintptr_t)mbuf_datastart(m0) + mbuf_maxlen(m0))) {
return 0;
}
if (ip->ip_v != 4) {
return (u_int8_t)0;
}
ds_field = ip->ip_tos;
}
#if INET6
else if (t->pftag_flags & PF_TAG_HDR_INET6) {
struct ip6_hdr *ip6 = (struct ip6_hdr *)(void *)t->pftag_hdr;
u_int32_t flowlabel;
if (((uintptr_t)ip6 + sizeof(*ip6)) >
((uintptr_t)mbuf_datastart(m0) + mbuf_maxlen(m0))) {
return 0;
}
flowlabel = ntohl(ip6->ip6_flow);
if ((flowlabel >> 28) != 6) {
return (u_int8_t)0;
}
ds_field = (flowlabel >> 20) & 0xff;
}
#endif
return ds_field;
}
void
write_dsfield(struct mbuf *m, struct pf_mtag *t, u_int8_t dsfield)
{
struct mbuf *m0;
if (t->pftag_hdr == NULL ||
!(t->pftag_flags & (PF_TAG_HDR_INET | PF_TAG_HDR_INET6))) {
return;
}
for (m0 = m; m0 != NULL; m0 = m0->m_next) {
if (((caddr_t)t->pftag_hdr >= m0->m_data) &&
((caddr_t)t->pftag_hdr < m0->m_data + m0->m_len)) {
break;
}
}
if (m0 == NULL) {
printf("%s: can't locate header!\n", __func__);
return;
}
if (t->pftag_flags & PF_TAG_HDR_INET) {
struct ip *ip = (struct ip *)(void *)t->pftag_hdr;
u_int8_t old;
int32_t sum;
if (((uintptr_t)ip + sizeof(*ip)) >
((uintptr_t)mbuf_datastart(m0) + mbuf_maxlen(m0))) {
return;
}
if (ip->ip_v != 4) {
return;
}
old = ip->ip_tos;
dsfield |= old & 3;
if (old == dsfield) {
return;
}
ip->ip_tos = dsfield;
sum = ~ntohs(ip->ip_sum) & 0xffff;
sum += 0xff00 + (~old & 0xff) + dsfield;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
ip->ip_sum = htons(~sum & 0xffff);
}
#if INET6
else if (t->pftag_flags & PF_TAG_HDR_INET6) {
struct ip6_hdr *ip6 = (struct ip6_hdr *)t->pftag_hdr;
u_int32_t flowlabel;
if (((uintptr_t)ip6 + sizeof(*ip6)) >
((uintptr_t)mbuf_datastart(m0) + mbuf_maxlen(m0))) {
return;
}
flowlabel = ntohl(ip6->ip6_flow);
if ((flowlabel >> 28) != 6) {
return;
}
flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
ip6->ip6_flow = htonl(flowlabel);
}
#endif
}
int
mark_ecn(struct mbuf *m, struct pf_mtag *t, int flags)
{
struct mbuf *m0;
void *hdr;
int af;
if ((hdr = t->pftag_hdr) == NULL ||
!(t->pftag_flags & (PF_TAG_HDR_INET | PF_TAG_HDR_INET6))) {
return 0;
}
for (m0 = m; m0 != NULL; m0 = m0->m_next) {
if (((caddr_t)hdr >= m0->m_data) &&
((caddr_t)hdr < m0->m_data + m0->m_len)) {
break;
}
}
if (m0 == NULL) {
printf("%s: can't locate header!\n", __func__);
return 0;
}
if (t->pftag_flags & PF_TAG_HDR_INET) {
af = AF_INET;
} else if (t->pftag_flags & PF_TAG_HDR_INET6) {
af = AF_INET6;
} else {
af = AF_UNSPEC;
}
switch (af) {
case AF_INET:
if (flags & CLASSQF_ECN4) {
struct ip *ip = hdr;
u_int8_t otos;
int sum;
if (((uintptr_t)ip + sizeof(*ip)) >
((uintptr_t)mbuf_datastart(m0) + mbuf_maxlen(m0))) {
return 0;
}
if (ip->ip_v != 4) {
return 0;
}
if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_NOTECT) {
return 0;
}
if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_CE) {
return 1;
}
otos = ip->ip_tos;
ip->ip_tos |= IPTOS_ECN_CE;
if (!(m->m_pkthdr.csum_flags & CSUM_DELAY_IP)) {
sum = ~ntohs(ip->ip_sum) & 0xffff;
sum += (~otos & 0xffff) + ip->ip_tos;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
ip->ip_sum = htons(~sum & 0xffff);
}
return 1;
}
break;
#if INET6
case AF_INET6:
if (flags & CLASSQF_ECN6) {
struct ip6_hdr *ip6 = hdr;
u_int32_t flowlabel;
if (((uintptr_t)ip6 + sizeof(*ip6)) >
((uintptr_t)mbuf_datastart(m0) + mbuf_maxlen(m0))) {
return 0;
}
flowlabel = ntohl(ip6->ip6_flow);
if ((flowlabel >> 28) != 6) {
return 0;
}
if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
(IPTOS_ECN_NOTECT << 20)) {
return 0;
}
if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
(IPTOS_ECN_CE << 20)) {
return 1;
}
flowlabel |= (IPTOS_ECN_CE << 20);
ip6->ip6_flow = htonl(flowlabel);
return 1;
}
break;
#endif
}
return 0;
}
#endif