#include <linux/kernel.h>
#include <linux/icmpv6.h>
#include <linux/skbuff_ref.h>
#include <net/gro.h>
#include <net/icmp.h>
#include <net/ip6_route.h>
#include <net/inet_ecn.h>
#include <net/xfrm.h>
#include <crypto/aead.h>
#include "xfrm_inout.h"
#include "trace_iptfs.h"
#define IPTFS_SUBTYPE_BASIC 0
#define IPTFS_SUBTYPE_CC 1
#define IPTFS_DEFAULT_DROP_TIME_USECS 1000000
#define IPTFS_DEFAULT_REORDER_WINDOW 3
#define IPTFS_DEFAULT_INIT_DELAY_USECS 0
#define IPTFS_DEFAULT_MAX_QUEUE_SIZE (1024 * 10240)
#define XFRM_IPTFS_MIN_L3HEADROOM 128
#define XFRM_IPTFS_MIN_L2HEADROOM (L1_CACHE_BYTES > 64 ? 64 : 64 + 16)
#define IPTFS_PKT_SHARE_MIN 129
#define NSECS_IN_USEC 1000
#define IPTFS_HRTIMER_MODE HRTIMER_MODE_REL_SOFT
struct xfrm_iptfs_config {
u32 pkt_size;
u32 max_queue_size;
u16 reorder_win_size;
u8 dont_frag : 1;
};
struct skb_wseq {
struct sk_buff *skb;
u64 drop_time;
};
struct xfrm_iptfs_data {
struct xfrm_iptfs_config cfg;
struct xfrm_state *x;
struct sk_buff_head queue;
u32 queue_size;
u32 ecn_queue_size;
u64 init_delay_ns;
struct hrtimer iptfs_timer;
time64_t iptfs_settime;
u32 payload_mtu;
bool w_seq_set;
u64 w_wantseq;
struct skb_wseq *w_saved;
u32 w_savedlen;
spinlock_t drop_lock;
struct hrtimer drop_timer;
u64 drop_time_ns;
struct sk_buff *ra_newskb;
u64 ra_wantseq;
u8 ra_runt[6];
u8 ra_runtlen;
};
static u32 __iptfs_get_inner_mtu(struct xfrm_state *x, int outer_mtu);
static enum hrtimer_restart iptfs_delay_timer(struct hrtimer *me);
static enum hrtimer_restart iptfs_drop_timer(struct hrtimer *me);
#ifdef TRACEPOINTS_ENABLED
static u32 __trace_ip_proto(struct iphdr *iph)
{
if (iph->version == 4)
return iph->protocol;
return ((struct ipv6hdr *)iph)->nexthdr;
}
static u32 __trace_ip_proto_seq(struct iphdr *iph)
{
void *nexthdr;
u32 protocol = 0;
if (iph->version == 4) {
nexthdr = (void *)(iph + 1);
protocol = iph->protocol;
} else if (iph->version == 6) {
nexthdr = (void *)(((struct ipv6hdr *)(iph)) + 1);
protocol = ((struct ipv6hdr *)(iph))->nexthdr;
}
switch (protocol) {
case IPPROTO_ICMP:
return ntohs(((struct icmphdr *)nexthdr)->un.echo.sequence);
case IPPROTO_ICMPV6:
return ntohs(((struct icmp6hdr *)nexthdr)->icmp6_sequence);
case IPPROTO_TCP:
return ntohl(((struct tcphdr *)nexthdr)->seq);
case IPPROTO_UDP:
return ntohs(((struct udphdr *)nexthdr)->source);
default:
return 0;
}
}
#endif
static u64 __esp_seq(struct sk_buff *skb)
{
u64 seq = ntohl(XFRM_SKB_CB(skb)->seq.input.low);
return seq | (u64)ntohl(XFRM_SKB_CB(skb)->seq.input.hi) << 32;
}
static struct sk_buff *iptfs_alloc_skb(struct sk_buff *tpl, u32 len, bool l3resv)
{
struct sk_buff *skb;
u32 resv;
if (!l3resv) {
resv = XFRM_IPTFS_MIN_L2HEADROOM;
} else {
struct dst_entry *dst = skb_dst(tpl);
resv = LL_RESERVED_SPACE(dst->dev) + dst->header_len;
resv = max(resv, XFRM_IPTFS_MIN_L3HEADROOM);
resv = L1_CACHE_ALIGN(resv);
}
skb = alloc_skb(len + resv, GFP_ATOMIC | __GFP_NOWARN);
if (!skb)
return NULL;
skb_reserve(skb, resv);
if (!l3resv) {
skb->dev = tpl->dev;
__skb_ext_copy(skb, tpl);
}
skb_dst_copy(skb, tpl);
return skb;
}
static void iptfs_skb_head_to_frag(const struct sk_buff *skb, skb_frag_t *frag)
{
struct page *page = virt_to_head_page(skb->data);
unsigned char *addr = (unsigned char *)page_address(page);
skb_frag_fill_page_desc(frag, page, skb->data - addr, skb_headlen(skb));
}
struct iptfs_skb_frag_walk {
u32 fragi;
u32 past;
u32 total;
u32 nr_frags;
u32 initial_offset;
skb_frag_t frags[MAX_SKB_FRAGS + 1];
bool pp_recycle;
};
static void iptfs_skb_prepare_frag_walk(struct sk_buff *skb, u32 initial_offset,
struct iptfs_skb_frag_walk *walk)
{
struct skb_shared_info *shinfo = skb_shinfo(skb);
skb_frag_t *frag, *from;
u32 i;
walk->initial_offset = initial_offset;
walk->fragi = 0;
walk->past = 0;
walk->total = 0;
walk->nr_frags = 0;
walk->pp_recycle = skb->pp_recycle;
if (skb->head_frag) {
if (initial_offset >= skb_headlen(skb)) {
initial_offset -= skb_headlen(skb);
} else {
frag = &walk->frags[walk->nr_frags++];
iptfs_skb_head_to_frag(skb, frag);
frag->offset += initial_offset;
frag->len -= initial_offset;
walk->total += frag->len;
initial_offset = 0;
}
} else {
initial_offset -= skb_headlen(skb);
}
for (i = 0; i < shinfo->nr_frags; i++) {
from = &shinfo->frags[i];
if (initial_offset >= from->len) {
initial_offset -= from->len;
continue;
}
frag = &walk->frags[walk->nr_frags++];
*frag = *from;
if (initial_offset) {
frag->offset += initial_offset;
frag->len -= initial_offset;
initial_offset = 0;
}
walk->total += frag->len;
}
}
static u32 iptfs_skb_reset_frag_walk(struct iptfs_skb_frag_walk *walk,
u32 offset)
{
offset -= walk->initial_offset;
while (offset < walk->past) {
walk->past -= walk->frags[--walk->fragi].len;
if (offset >= walk->past)
break;
}
while (offset >= walk->past + walk->frags[walk->fragi].len)
walk->past += walk->frags[walk->fragi++].len;
offset -= walk->past;
return offset;
}
static bool iptfs_skb_can_add_frags(const struct sk_buff *skb,
struct iptfs_skb_frag_walk *walk,
u32 offset, u32 len)
{
struct skb_shared_info *shinfo = skb_shinfo(skb);
u32 fragi, nr_frags, fraglen;
if (skb_has_frag_list(skb) || skb->pp_recycle != walk->pp_recycle)
return false;
offset = iptfs_skb_reset_frag_walk(walk, offset);
fragi = walk->fragi;
nr_frags = shinfo->nr_frags;
while (len && fragi < walk->nr_frags) {
skb_frag_t *frag = &walk->frags[fragi];
fraglen = frag->len;
if (offset) {
fraglen -= offset;
offset = 0;
}
if (++nr_frags > MAX_SKB_FRAGS)
return false;
if (len <= fraglen)
return true;
len -= fraglen;
fragi++;
}
return true;
}
static int iptfs_skb_add_frags(struct sk_buff *skb,
struct iptfs_skb_frag_walk *walk, u32 offset,
u32 len)
{
struct skb_shared_info *shinfo = skb_shinfo(skb);
u32 fraglen;
if (!walk->nr_frags || offset >= walk->total + walk->initial_offset)
return len;
offset = iptfs_skb_reset_frag_walk(walk, offset);
while (len && walk->fragi < walk->nr_frags) {
skb_frag_t *frag = &walk->frags[walk->fragi];
skb_frag_t *tofrag = &shinfo->frags[shinfo->nr_frags];
*tofrag = *frag;
if (offset) {
tofrag->offset += offset;
tofrag->len -= offset;
offset = 0;
}
__skb_frag_ref(tofrag);
shinfo->nr_frags++;
shinfo->flags |= SKBFL_SHARED_FRAG;
fraglen = tofrag->len;
if (len < fraglen) {
tofrag->len = len;
skb->len += len;
skb->data_len += len;
return 0;
}
len -= fraglen;
skb->len += fraglen;
skb->data_len += fraglen;
walk->past += frag->len;
walk->fragi++;
}
return len;
}
#define CREATE_TRACE_POINTS
#include "trace_iptfs.h"
static struct sk_buff *
iptfs_pskb_add_frags(struct sk_buff *tpl, struct iptfs_skb_frag_walk *walk,
u32 off, u32 len, struct skb_seq_state *st, u32 copy_len)
{
struct sk_buff *skb;
skb = iptfs_alloc_skb(tpl, copy_len, false);
if (!skb)
return NULL;
if (!iptfs_skb_can_add_frags(skb, walk, off + copy_len,
len - copy_len)) {
kfree_skb(skb);
return NULL;
}
if (copy_len &&
skb_copy_seq_read(st, off, skb_put(skb, copy_len), copy_len)) {
XFRM_INC_STATS(dev_net(st->root_skb->dev),
LINUX_MIB_XFRMINERROR);
kfree_skb(skb);
return NULL;
}
iptfs_skb_add_frags(skb, walk, off + copy_len, len - copy_len);
return skb;
}
static struct sk_buff *
iptfs_pskb_extract_seq(u32 skblen, struct skb_seq_state *st, u32 off, int len)
{
struct sk_buff *skb = iptfs_alloc_skb(st->root_skb, skblen, false);
if (!skb)
return NULL;
if (skb_copy_seq_read(st, off, skb_put(skb, len), len)) {
XFRM_INC_STATS(dev_net(st->root_skb->dev), LINUX_MIB_XFRMINERROR);
kfree_skb(skb);
return NULL;
}
return skb;
}
static void iptfs_input_save_runt(struct xfrm_iptfs_data *xtfs, u64 seq,
u8 *buf, int len)
{
memcpy(xtfs->ra_runt, buf, len);
xtfs->ra_runtlen = len;
xtfs->ra_wantseq = seq + 1;
}
static u32 __iptfs_iphlen(u8 *data)
{
struct iphdr *iph = (struct iphdr *)data;
if (iph->version == 0x4)
return sizeof(*iph);
return sizeof(struct ipv6hdr);
}
static u32 __iptfs_iplen(u8 *data)
{
struct iphdr *iph = (struct iphdr *)data;
if (iph->version == 0x4)
return ntohs(iph->tot_len);
return ntohs(((struct ipv6hdr *)iph)->payload_len) +
sizeof(struct ipv6hdr);
}
static void iptfs_complete_inner_skb(struct xfrm_state *x, struct sk_buff *skb)
{
skb_reset_network_header(skb);
skb_reset_transport_header(skb);
skb->ip_summed = CHECKSUM_NONE;
if (ip_hdr(skb)->version == 0x4) {
struct iphdr *iph = ip_hdr(skb);
if (x->props.flags & XFRM_STATE_DECAP_DSCP)
ipv4_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, iph);
if (!(x->props.flags & XFRM_STATE_NOECN))
if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
IP_ECN_set_ce(iph);
skb->protocol = htons(ETH_P_IP);
} else {
struct ipv6hdr *iph = ipv6_hdr(skb);
if (x->props.flags & XFRM_STATE_DECAP_DSCP)
ipv6_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, iph);
if (!(x->props.flags & XFRM_STATE_NOECN))
if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
IP6_ECN_set_ce(skb, iph);
skb->protocol = htons(ETH_P_IPV6);
}
}
static void __iptfs_reassem_done(struct xfrm_iptfs_data *xtfs, bool free)
{
assert_spin_locked(&xtfs->drop_lock);
hrtimer_try_to_cancel(&xtfs->drop_timer);
if (free)
kfree_skb(xtfs->ra_newskb);
xtfs->ra_newskb = NULL;
}
static void iptfs_reassem_abort(struct xfrm_iptfs_data *xtfs)
{
__iptfs_reassem_done(xtfs, true);
}
static void iptfs_reassem_done(struct xfrm_iptfs_data *xtfs)
{
__iptfs_reassem_done(xtfs, false);
}
static u32 iptfs_reassem_cont(struct xfrm_iptfs_data *xtfs, u64 seq,
struct skb_seq_state *st, struct sk_buff *skb,
u32 data, u32 blkoff, struct list_head *list)
{
struct iptfs_skb_frag_walk _fragwalk;
struct iptfs_skb_frag_walk *fragwalk = NULL;
struct sk_buff *newskb = xtfs->ra_newskb;
u32 remaining = skb->len - data;
u32 runtlen = xtfs->ra_runtlen;
u32 copylen, fraglen, ipremain, iphlen, iphremain, rrem;
if (!runtlen && !xtfs->ra_newskb)
return data + min(blkoff, remaining);
if (seq < xtfs->ra_wantseq)
return data + remaining;
if (seq > xtfs->ra_wantseq) {
XFRM_INC_STATS(xs_net(xtfs->x), LINUX_MIB_XFRMINIPTFSERROR);
goto abandon;
}
if (blkoff == 0) {
if ((*skb->data & 0xF0) != 0) {
XFRM_INC_STATS(xs_net(xtfs->x),
LINUX_MIB_XFRMINIPTFSERROR);
goto abandon;
}
xtfs->ra_wantseq++;
return data + remaining;
}
if (runtlen) {
xtfs->ra_runtlen = 0;
rrem = sizeof(xtfs->ra_runt) - runtlen;
if (remaining < rrem || blkoff < rrem) {
XFRM_INC_STATS(xs_net(xtfs->x),
LINUX_MIB_XFRMINIPTFSERROR);
goto abandon;
}
if (skb_copy_seq_read(st, data, &xtfs->ra_runt[runtlen],
rrem)) {
XFRM_INC_STATS(xs_net(xtfs->x),
LINUX_MIB_XFRMINBUFFERERROR);
goto abandon;
}
ipremain = __iptfs_iplen(xtfs->ra_runt);
if (ipremain < sizeof(xtfs->ra_runt)) {
XFRM_INC_STATS(xs_net(xtfs->x),
LINUX_MIB_XFRMINIPTFSERROR);
goto abandon;
}
newskb = iptfs_alloc_skb(skb, ipremain, false);
if (!newskb) {
XFRM_INC_STATS(xs_net(xtfs->x), LINUX_MIB_XFRMINERROR);
goto abandon;
}
xtfs->ra_newskb = newskb;
memcpy(skb_put(newskb, runtlen), xtfs->ra_runt,
sizeof(xtfs->ra_runt));
}
ipremain = __iptfs_iplen(newskb->data);
iphlen = __iptfs_iphlen(newskb->data);
ipremain -= newskb->len;
if (blkoff < ipremain) {
XFRM_INC_STATS(xs_net(xtfs->x), LINUX_MIB_XFRMINIPTFSERROR);
goto abandon;
}
if (newskb->len < iphlen) {
iphremain = iphlen - newskb->len;
if (blkoff < iphremain) {
XFRM_INC_STATS(xs_net(xtfs->x),
LINUX_MIB_XFRMINIPTFSERROR);
goto abandon;
}
fraglen = min(blkoff, remaining);
copylen = min(fraglen, iphremain);
if (skb_copy_seq_read(st, data, skb_put(newskb, copylen),
copylen)) {
XFRM_INC_STATS(xs_net(xtfs->x),
LINUX_MIB_XFRMINBUFFERERROR);
goto abandon;
}
if (copylen < iphremain) {
xtfs->ra_wantseq++;
return data + fraglen;
}
data += copylen;
blkoff -= copylen;
remaining -= copylen;
ipremain -= copylen;
}
fraglen = min(blkoff, remaining);
copylen = min(fraglen, ipremain);
if (!skb_has_frag_list(skb) && !skb_has_frag_list(newskb) &&
(skb->head_frag || skb->len == skb->data_len) &&
skb->pp_recycle == newskb->pp_recycle) {
fragwalk = &_fragwalk;
iptfs_skb_prepare_frag_walk(skb, data, fragwalk);
}
if (fragwalk &&
iptfs_skb_can_add_frags(newskb, fragwalk, data, copylen)) {
iptfs_skb_add_frags(newskb, fragwalk, data, copylen);
} else {
if (skb_linearize(newskb)) {
XFRM_INC_STATS(xs_net(xtfs->x),
LINUX_MIB_XFRMINBUFFERERROR);
goto abandon;
}
if (skb_copy_seq_read(st, data, skb_put(newskb, copylen),
copylen)) {
XFRM_INC_STATS(xs_net(xtfs->x),
LINUX_MIB_XFRMINBUFFERERROR);
goto abandon;
}
}
if (copylen < ipremain) {
xtfs->ra_wantseq++;
} else {
iptfs_reassem_done(xtfs);
iptfs_complete_inner_skb(xtfs->x, newskb);
list_add_tail(&newskb->list, list);
}
return data + fraglen;
abandon:
if (xtfs->ra_newskb) {
iptfs_reassem_abort(xtfs);
} else {
xtfs->ra_runtlen = 0;
xtfs->ra_wantseq = 0;
}
return data + min(blkoff, remaining);
}
static bool __input_process_payload(struct xfrm_state *x, u32 data,
struct skb_seq_state *skbseq,
struct list_head *sublist)
{
u8 hbytes[sizeof(struct ipv6hdr)];
struct iptfs_skb_frag_walk _fragwalk;
struct iptfs_skb_frag_walk *fragwalk = NULL;
struct sk_buff *defer, *first_skb, *next, *skb;
const unsigned char *old_mac;
struct xfrm_iptfs_data *xtfs;
struct iphdr *iph;
struct net *net;
u32 first_iplen, iphlen, iplen, remaining, tail;
u32 capturelen;
u64 seq;
bool first_skb_partial = false;
xtfs = x->mode_data;
net = xs_net(x);
skb = skbseq->root_skb;
first_skb = NULL;
defer = NULL;
seq = __esp_seq(skb);
old_mac = skb_mac_header_was_set(skb) ? skb_mac_header(skb) : NULL;
tail = skb->len;
while (data < tail) {
__be16 protocol = 0;
remaining = tail - data;
iphlen = min_t(u32, remaining, 6);
if (skb_copy_seq_read(skbseq, data, hbytes, iphlen)) {
XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
goto done;
}
iph = (struct iphdr *)hbytes;
if (iph->version == 0x4) {
if (remaining < 4) {
iptfs_input_save_runt(xtfs, seq, hbytes,
remaining);
data += remaining;
break;
}
iplen = be16_to_cpu(iph->tot_len);
iphlen = iph->ihl << 2;
if (iplen < iphlen || iphlen < sizeof(*iph)) {
XFRM_INC_STATS(net,
LINUX_MIB_XFRMINHDRERROR);
goto done;
}
protocol = cpu_to_be16(ETH_P_IP);
XFRM_MODE_SKB_CB(skbseq->root_skb)->tos = iph->tos;
} else if (iph->version == 0x6) {
if (remaining < 6) {
iptfs_input_save_runt(xtfs, seq, hbytes,
remaining);
data += remaining;
break;
}
iplen = be16_to_cpu(((struct ipv6hdr *)hbytes)->payload_len);
iplen += sizeof(struct ipv6hdr);
iphlen = sizeof(struct ipv6hdr);
protocol = cpu_to_be16(ETH_P_IPV6);
XFRM_MODE_SKB_CB(skbseq->root_skb)->tos =
ipv6_get_dsfield((struct ipv6hdr *)iph);
} else if (iph->version == 0x0) {
data = tail;
break;
} else {
XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
goto done;
}
if (unlikely(skbseq->stepped_offset)) {
struct sk_buff *save = skbseq->root_skb;
skb_abort_seq_read(skbseq);
skb_prepare_seq_read(save, data, tail, skbseq);
}
if (first_skb) {
skb = NULL;
} else {
first_skb = skb;
first_iplen = iplen;
fragwalk = NULL;
if (skb_has_frag_list(skb)) {
defer = skb;
skb = NULL;
} else if (data + iphlen <= skb_headlen(skb) &&
skb_tailroom(skb) + tail - data >= iplen) {
skb_pull(skb, data);
data = 0;
tail = skb->len;
remaining = skb->len;
skb->protocol = protocol;
skb_mac_header_rebuild(skb);
if (skb->mac_len)
eth_hdr(skb)->h_proto = skb->protocol;
skb_abort_seq_read(skbseq);
skb_prepare_seq_read(skb, data, tail, skbseq);
} else if (skb->head_frag &&
remaining >= iphlen) {
fragwalk = &_fragwalk;
iptfs_skb_prepare_frag_walk(skb, data, fragwalk);
defer = skb;
skb = NULL;
} else {
defer = skb;
skb = NULL;
}
}
capturelen = min(iplen, remaining);
if (!skb) {
if (!fragwalk ||
iplen < IPTFS_PKT_SHARE_MIN ||
capturelen <= iphlen ||
!(skb = iptfs_pskb_add_frags(first_skb, fragwalk,
data, capturelen,
skbseq, iphlen))) {
skb = iptfs_pskb_extract_seq(iplen, skbseq, data, capturelen);
}
if (!skb) {
data += capturelen;
continue;
}
skb->protocol = protocol;
if (old_mac) {
skb_set_mac_header(skb, -first_skb->mac_len);
memcpy(skb_mac_header(skb), old_mac, first_skb->mac_len);
eth_hdr(skb)->h_proto = skb->protocol;
}
}
data += capturelen;
if (skb->len < iplen) {
spin_lock(&xtfs->drop_lock);
xtfs->ra_newskb = skb;
xtfs->ra_wantseq = seq + 1;
if (!hrtimer_is_queued(&xtfs->drop_timer)) {
hrtimer_start(&xtfs->drop_timer,
xtfs->drop_time_ns,
IPTFS_HRTIMER_MODE);
}
spin_unlock(&xtfs->drop_lock);
first_skb_partial = (first_skb == skb);
break;
}
iptfs_complete_inner_skb(x, skb);
list_add_tail(&skb->list, sublist);
}
if (data != tail)
XFRM_INC_STATS(net, LINUX_MIB_XFRMINIPTFSERROR);
if (first_skb && first_iplen && !defer && !first_skb_partial) {
if (pskb_trim(first_skb, first_iplen)) {
list_del(&first_skb->list);
defer = first_skb;
}
first_skb->ip_summed = CHECKSUM_NONE;
}
list_for_each_entry_safe(skb, next, sublist, list) {
skb_list_del_init(skb);
if (xfrm_input(skb, 0, 0, -2))
kfree_skb(skb);
}
done:
skb = skbseq->root_skb;
skb_abort_seq_read(skbseq);
if (defer) {
consume_skb(defer);
} else if (!first_skb) {
kfree_skb(skb);
}
return true;
}
static void iptfs_input_ordered(struct xfrm_state *x, struct sk_buff *skb)
{
struct ip_iptfs_cc_hdr iptcch;
struct skb_seq_state skbseq;
struct list_head sublist;
struct xfrm_iptfs_data *xtfs;
struct ip_iptfs_hdr *ipth;
struct net *net;
u32 blkoff, data, remaining;
bool consumed = false;
u64 seq;
xtfs = x->mode_data;
net = xs_net(x);
seq = __esp_seq(skb);
ipth = (struct ip_iptfs_hdr *)&iptcch;
skb_prepare_seq_read(skb, 0, skb->len, &skbseq);
if (skb_copy_seq_read(&skbseq, 0, ipth, sizeof(*ipth))) {
XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
goto done;
}
data = sizeof(*ipth);
trace_iptfs_egress_recv(skb, xtfs, be16_to_cpu(ipth->block_offset));
if (ipth->subtype == IPTFS_SUBTYPE_CC) {
remaining = sizeof(iptcch) - sizeof(*ipth);
if (skb_copy_seq_read(&skbseq, data, ipth + 1, remaining)) {
XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
goto done;
}
data += remaining;
} else if (ipth->subtype != IPTFS_SUBTYPE_BASIC) {
XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
goto done;
}
if (ipth->flags != 0) {
XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
goto done;
}
INIT_LIST_HEAD(&sublist);
blkoff = ntohs(ipth->block_offset);
if (blkoff || xtfs->ra_runtlen || xtfs->ra_newskb) {
spin_lock(&xtfs->drop_lock);
if (blkoff || xtfs->ra_runtlen || xtfs->ra_newskb) {
data = iptfs_reassem_cont(xtfs, seq, &skbseq, skb, data,
blkoff, &sublist);
}
spin_unlock(&xtfs->drop_lock);
}
consumed = __input_process_payload(x, data, &skbseq, &sublist);
done:
if (!consumed) {
skb = skbseq.root_skb;
skb_abort_seq_read(&skbseq);
kfree_skb(skb);
}
}
static void __vec_shift(struct xfrm_iptfs_data *xtfs, u32 shift)
{
u32 savedlen = xtfs->w_savedlen;
if (shift > savedlen)
shift = savedlen;
if (shift != savedlen)
memcpy(xtfs->w_saved, xtfs->w_saved + shift,
(savedlen - shift) * sizeof(*xtfs->w_saved));
memset(xtfs->w_saved + savedlen - shift, 0,
shift * sizeof(*xtfs->w_saved));
xtfs->w_savedlen -= shift;
}
static void __reorder_past(struct xfrm_iptfs_data *xtfs, struct sk_buff *inskb,
struct list_head *freelist)
{
list_add_tail(&inskb->list, freelist);
}
static u32 __reorder_drop(struct xfrm_iptfs_data *xtfs, struct list_head *list)
{
struct skb_wseq *s, *se;
const u32 savedlen = xtfs->w_savedlen;
time64_t now = ktime_get_raw_fast_ns();
u32 count = 0;
u32 scount = 0;
if (xtfs->w_saved[0].drop_time > now)
goto set_timer;
++xtfs->w_wantseq;
s = xtfs->w_saved;
se = s + savedlen;
do {
for (; s < se && !s->skb; s++) {
if (s->drop_time > now)
goto outerdone;
}
for (; s < se && s->skb; scount++, s++)
list_add_tail(&s->skb->list, list);
} while (s < se);
outerdone:
count = s - xtfs->w_saved;
if (count) {
xtfs->w_wantseq += count;
__vec_shift(xtfs, count);
}
if (xtfs->w_savedlen) {
set_timer:
hrtimer_start(&xtfs->drop_timer,
xtfs->w_saved[0].drop_time - now,
IPTFS_HRTIMER_MODE);
}
return scount;
}
static void __reorder_this(struct xfrm_iptfs_data *xtfs, struct sk_buff *inskb,
struct list_head *list)
{
struct skb_wseq *s, *se;
const u32 savedlen = xtfs->w_savedlen;
u32 count = 0;
list_add_tail(&inskb->list, list);
++xtfs->w_wantseq;
if (!savedlen)
return;
for (s = xtfs->w_saved, se = s + savedlen; s < se && s->skb; s++)
list_add_tail(&s->skb->list, list);
count = s - xtfs->w_saved;
if (count)
xtfs->w_wantseq += count;
__vec_shift(xtfs, count + 1);
}
static void iptfs_set_window_drop_times(struct xfrm_iptfs_data *xtfs, int index)
{
const u32 savedlen = xtfs->w_savedlen;
struct skb_wseq *s = xtfs->w_saved;
time64_t drop_time;
assert_spin_locked(&xtfs->drop_lock);
if (savedlen > index + 1) {
return;
}
drop_time = ktime_get_raw_fast_ns();
drop_time += xtfs->drop_time_ns;
s[index].drop_time = drop_time;
while (index-- > 0 && !s[index].skb)
s[index].drop_time = drop_time;
if (index == -1 && !hrtimer_is_queued(&xtfs->drop_timer))
hrtimer_start(&xtfs->drop_timer, xtfs->drop_time_ns,
IPTFS_HRTIMER_MODE);
}
static void __reorder_future_fits(struct xfrm_iptfs_data *xtfs,
struct sk_buff *inskb,
struct list_head *freelist)
{
const u64 inseq = __esp_seq(inskb);
const u64 wantseq = xtfs->w_wantseq;
const u64 distance = inseq - wantseq;
const u32 savedlen = xtfs->w_savedlen;
const u32 index = distance - 1;
if (xtfs->w_saved[index].skb) {
list_add_tail(&inskb->list, freelist);
return;
}
xtfs->w_saved[index].skb = inskb;
xtfs->w_savedlen = max(savedlen, index + 1);
iptfs_set_window_drop_times(xtfs, index);
}
static void __reorder_future_shifts(struct xfrm_iptfs_data *xtfs,
struct sk_buff *inskb,
struct list_head *list)
{
const u32 nslots = xtfs->cfg.reorder_win_size + 1;
const u64 inseq = __esp_seq(inskb);
u32 savedlen = xtfs->w_savedlen;
u64 wantseq = xtfs->w_wantseq;
struct skb_wseq *wnext;
struct sk_buff *slot0;
u32 beyond, shifting, slot;
u64 distance;
distance = inseq - wantseq;
beyond = distance - (nslots - 1);
shifting = min(savedlen, beyond);
slot0 = NULL;
wnext = xtfs->w_saved;
for (slot = 1; slot <= shifting; slot++, wnext++) {
if (slot0)
list_add_tail(&slot0->list, list);
slot0 = wnext->skb;
wnext->skb = NULL;
}
if (savedlen < beyond) {
if (savedlen != 0)
list_add_tail(&slot0->list, list);
slot0 = NULL;
}
__vec_shift(xtfs, beyond);
xtfs->w_wantseq += beyond;
xtfs->w_savedlen = nslots - 1;
xtfs->w_saved[xtfs->w_savedlen - 1].skb = inskb;
iptfs_set_window_drop_times(xtfs, xtfs->w_savedlen - 1);
if (!slot0)
return;
__reorder_this(xtfs, slot0, list);
}
static void iptfs_input_reorder(struct xfrm_iptfs_data *xtfs,
struct sk_buff *inskb, struct list_head *list,
struct list_head *freelist)
{
const u32 nslots = xtfs->cfg.reorder_win_size + 1;
u64 inseq = __esp_seq(inskb);
u64 wantseq;
assert_spin_locked(&xtfs->drop_lock);
if (unlikely(!xtfs->w_seq_set)) {
xtfs->w_seq_set = true;
xtfs->w_wantseq = inseq;
}
wantseq = xtfs->w_wantseq;
if (likely(inseq == wantseq))
__reorder_this(xtfs, inskb, list);
else if (inseq < wantseq)
__reorder_past(xtfs, inskb, freelist);
else if ((inseq - wantseq) < nslots)
__reorder_future_fits(xtfs, inskb, freelist);
else
__reorder_future_shifts(xtfs, inskb, list);
}
static enum hrtimer_restart iptfs_drop_timer(struct hrtimer *me)
{
struct sk_buff *skb, *next;
struct list_head list;
struct xfrm_iptfs_data *xtfs;
struct xfrm_state *x;
u32 count;
xtfs = container_of(me, typeof(*xtfs), drop_timer);
x = xtfs->x;
INIT_LIST_HEAD(&list);
spin_lock(&xtfs->drop_lock);
skb = xtfs->ra_newskb;
xtfs->ra_newskb = NULL;
count = xtfs->w_savedlen ? __reorder_drop(xtfs, &list) : 0;
spin_unlock(&xtfs->drop_lock);
if (skb)
kfree_skb_reason(skb, SKB_DROP_REASON_FRAG_REASM_TIMEOUT);
if (count) {
list_for_each_entry_safe(skb, next, &list, list) {
skb_list_del_init(skb);
iptfs_input_ordered(x, skb);
}
}
return HRTIMER_NORESTART;
}
static int iptfs_input(struct xfrm_state *x, struct sk_buff *skb)
{
struct list_head freelist, list;
struct xfrm_iptfs_data *xtfs = x->mode_data;
struct sk_buff *next;
if (xtfs->cfg.reorder_win_size == 0) {
iptfs_input_ordered(x, skb);
goto done;
}
INIT_LIST_HEAD(&list);
INIT_LIST_HEAD(&freelist);
spin_lock(&xtfs->drop_lock);
iptfs_input_reorder(xtfs, skb, &list, &freelist);
spin_unlock(&xtfs->drop_lock);
list_for_each_entry_safe(skb, next, &list, list) {
skb_list_del_init(skb);
iptfs_input_ordered(x, skb);
}
list_for_each_entry_safe(skb, next, &freelist, list) {
skb_list_del_init(skb);
kfree_skb(skb);
}
done:
return -EINPROGRESS;
}
static bool iptfs_enqueue(struct xfrm_iptfs_data *xtfs, struct sk_buff *skb)
{
u64 newsz = xtfs->queue_size + skb->len;
struct iphdr *iph;
assert_spin_locked(&xtfs->x->lock);
if (newsz > xtfs->cfg.max_queue_size)
return false;
if (newsz > xtfs->ecn_queue_size) {
iph = ip_hdr(skb);
if (iph->version == 4)
IP_ECN_set_ce(iph);
else if (iph->version == 6)
IP6_ECN_set_ce(skb, ipv6_hdr(skb));
}
__skb_queue_tail(&xtfs->queue, skb);
xtfs->queue_size += skb->len;
return true;
}
static int iptfs_get_cur_pmtu(struct xfrm_state *x, struct xfrm_iptfs_data *xtfs,
struct sk_buff *skb)
{
struct xfrm_dst *xdst = (struct xfrm_dst *)skb_dst(skb);
u32 payload_mtu = xtfs->payload_mtu;
u32 pmtu = __iptfs_get_inner_mtu(x, xdst->child_mtu_cached);
if (payload_mtu && payload_mtu < pmtu)
pmtu = payload_mtu;
return pmtu;
}
static int iptfs_is_too_big(struct sock *sk, struct sk_buff *skb, u32 pmtu)
{
if (skb->len <= pmtu)
return 0;
if (skb->dev)
XFRM_INC_STATS(dev_net(skb->dev), LINUX_MIB_XFRMOUTERROR);
if (sk)
xfrm_local_error(skb, pmtu);
else if (ip_hdr(skb)->version == 4)
icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(pmtu));
else
icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, pmtu);
return 1;
}
static int iptfs_output_collect(struct net *net, struct sock *sk, struct sk_buff *skb)
{
struct dst_entry *dst = skb_dst(skb);
struct xfrm_state *x = dst->xfrm;
struct xfrm_iptfs_data *xtfs = x->mode_data;
struct sk_buff *segs, *nskb;
u32 pmtu = 0;
bool ok = true;
bool was_gso;
if (xtfs->cfg.dont_frag)
pmtu = iptfs_get_cur_pmtu(x, xtfs, skb);
was_gso = skb_is_gso(skb);
if (!was_gso) {
segs = skb;
} else {
segs = skb_gso_segment(skb, 0);
if (IS_ERR_OR_NULL(segs)) {
XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
kfree_skb(skb);
if (IS_ERR(segs))
return PTR_ERR(segs);
return -EINVAL;
}
consume_skb(skb);
skb = NULL;
}
spin_lock_bh(&x->lock);
skb_list_walk_safe(segs, skb, nskb) {
skb_mark_not_on_list(skb);
if (!ok) {
nospace:
trace_iptfs_no_queue_space(skb, xtfs, pmtu, was_gso);
XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOQSPACE);
kfree_skb_reason(skb, SKB_DROP_REASON_FULL_RING);
continue;
}
if (xtfs->cfg.dont_frag && iptfs_is_too_big(sk, skb, pmtu)) {
trace_iptfs_too_big(skb, xtfs, pmtu, was_gso);
kfree_skb_reason(skb, SKB_DROP_REASON_PKT_TOO_BIG);
continue;
}
ok = iptfs_enqueue(xtfs, skb);
if (!ok)
goto nospace;
trace_iptfs_enqueue(skb, xtfs, pmtu, was_gso);
}
if (!hrtimer_is_queued(&xtfs->iptfs_timer)) {
hrtimer_start(&xtfs->iptfs_timer, xtfs->init_delay_ns, IPTFS_HRTIMER_MODE);
xtfs->iptfs_settime = ktime_get_raw_fast_ns();
trace_iptfs_timer_start(xtfs, xtfs->init_delay_ns);
}
spin_unlock_bh(&x->lock);
return 0;
}
static void iptfs_output_prepare_skb(struct sk_buff *skb, u32 blkoff)
{
struct ip_iptfs_hdr *h;
size_t hsz = sizeof(*h);
h = skb_push(skb, hsz);
memset(h, 0, hsz);
if (blkoff)
h->block_offset = htons(blkoff);
skb->transport_header = skb->network_header;
skb->network_header -= hsz;
IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
}
static struct sk_buff *iptfs_copy_create_frag(struct skb_seq_state *st, u32 offset, u32 copy_len)
{
struct sk_buff *src = st->root_skb;
struct sk_buff *skb;
int err;
skb = iptfs_alloc_skb(src, copy_len, true);
if (!skb)
return ERR_PTR(-ENOMEM);
err = skb_copy_seq_read(st, offset, skb_put(skb, copy_len), copy_len);
if (err) {
kfree_skb(skb);
return ERR_PTR(err);
}
return skb;
}
static int iptfs_copy_create_frags(struct sk_buff **skbp, struct xfrm_iptfs_data *xtfs, u32 mtu)
{
struct skb_seq_state skbseq;
struct list_head sublist;
struct sk_buff *skb = *skbp;
struct sk_buff *nskb = *skbp;
u32 copy_len, offset;
u32 to_copy = skb->len - mtu;
u32 blkoff = 0;
int err = 0;
INIT_LIST_HEAD(&sublist);
skb_prepare_seq_read(skb, 0, skb->len, &skbseq);
offset = mtu;
to_copy = skb->len - offset;
while (to_copy) {
trace_iptfs_first_fragmenting(nskb, mtu, to_copy, NULL);
list_add_tail(&nskb->list, &sublist);
copy_len = min(to_copy, mtu);
nskb = iptfs_copy_create_frag(&skbseq, offset, copy_len);
if (IS_ERR(nskb)) {
XFRM_INC_STATS(xs_net(xtfs->x), LINUX_MIB_XFRMOUTERROR);
skb_abort_seq_read(&skbseq);
err = PTR_ERR(nskb);
nskb = NULL;
break;
}
iptfs_output_prepare_skb(nskb, to_copy);
offset += copy_len;
to_copy -= copy_len;
blkoff = to_copy;
}
skb_abort_seq_read(&skbseq);
*skbp = nskb;
if (nskb)
trace_iptfs_first_final_fragment(nskb, mtu, blkoff, NULL);
if (!err)
err = pskb_trim(skb, mtu);
if (err) {
kfree_skb(nskb);
list_for_each_entry_safe(skb, nskb, &sublist, list) {
skb_list_del_init(skb);
kfree_skb(skb);
}
return err;
}
iptfs_output_prepare_skb(skb, 0);
list_for_each_entry_safe(skb, nskb, &sublist, list) {
skb_list_del_init(skb);
if (!err)
err = xfrm_output(NULL, skb);
else
kfree_skb(skb);
}
if (err)
kfree_skb(*skbp);
return err;
}
static int iptfs_first_skb(struct sk_buff **skbp, struct xfrm_iptfs_data *xtfs, u32 mtu)
{
struct sk_buff *skb = *skbp;
int err;
if (skb->ip_summed == CHECKSUM_PARTIAL) {
err = skb_checksum_help(skb);
if (err)
return err;
}
trace_iptfs_first_dequeue(skb, mtu, 0, ip_hdr(skb));
skb_orphan(skb);
if (skb->len <= mtu) {
iptfs_output_prepare_skb(skb, 0);
return 0;
}
return iptfs_copy_create_frags(skbp, xtfs, mtu);
}
static struct sk_buff **iptfs_rehome_fraglist(struct sk_buff **nextp, struct sk_buff *child)
{
u32 fllen = 0;
*nextp = skb_shinfo(child)->frag_list;
while (*nextp) {
fllen += (*nextp)->len;
nextp = &(*nextp)->next;
}
skb_frag_list_init(child);
child->len -= fllen;
child->data_len -= fllen;
return nextp;
}
static void iptfs_consume_frags(struct sk_buff *to, struct sk_buff *from)
{
struct skb_shared_info *fromi = skb_shinfo(from);
struct skb_shared_info *toi = skb_shinfo(to);
unsigned int new_truesize;
if (!skb_headlen(from)) {
new_truesize = SKB_TRUESIZE(skb_end_offset(from));
} else {
iptfs_skb_head_to_frag(from, &toi->frags[toi->nr_frags]);
skb_frag_ref(to, toi->nr_frags++);
new_truesize = SKB_DATA_ALIGN(sizeof(struct sk_buff));
}
memcpy(&toi->frags[toi->nr_frags], fromi->frags,
sizeof(fromi->frags[0]) * fromi->nr_frags);
toi->nr_frags += fromi->nr_frags;
if (fromi->nr_frags)
toi->flags |= fromi->flags & SKBFL_SHARED_FRAG;
fromi->nr_frags = 0;
from->data_len = 0;
from->len = 0;
to->truesize += from->truesize - new_truesize;
from->truesize = new_truesize;
consume_skb(from);
}
static void iptfs_output_queued(struct xfrm_state *x, struct sk_buff_head *list)
{
struct xfrm_iptfs_data *xtfs = x->mode_data;
struct sk_buff *skb, *skb2, **nextp;
struct skb_shared_info *shi, *shi2;
while ((skb = __skb_dequeue(list))) {
u32 mtu = iptfs_get_cur_pmtu(x, xtfs, skb);
bool share_ok = true;
int remaining;
skb->protocol = x->outer_mode.family == AF_INET ? htons(ETH_P_IP) :
htons(ETH_P_IPV6);
if (skb->len > mtu && xtfs->cfg.dont_frag) {
XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTERROR);
trace_iptfs_first_toobig(skb, mtu, 0, ip_hdr(skb));
kfree_skb_reason(skb, SKB_DROP_REASON_PKT_TOO_BIG);
continue;
}
if (iptfs_first_skb(&skb, xtfs, mtu))
continue;
remaining = mtu - (skb->len - sizeof(struct ip_iptfs_hdr));
shi = skb_shinfo(skb);
nextp = &shi->frag_list;
while (*nextp) {
if (skb_has_frag_list(*nextp))
nextp = iptfs_rehome_fraglist(&(*nextp)->next, *nextp);
else
nextp = &(*nextp)->next;
}
if (shi->frag_list || skb_cloned(skb) || skb_shared(skb))
share_ok = false;
while ((skb2 = skb_peek(list))) {
trace_iptfs_ingress_nth_peek(skb2, remaining);
if (skb2->len > remaining)
break;
__skb_unlink(skb2, list);
skb_orphan(skb);
if (skb2->ip_summed == CHECKSUM_PARTIAL) {
if (skb_checksum_help(skb2)) {
XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTERROR);
kfree_skb(skb2);
continue;
}
}
shi2 = skb_shinfo(skb2);
if (share_ok &&
(shi2->frag_list ||
(!skb2->head_frag && skb_headlen(skb)) ||
skb->pp_recycle != skb2->pp_recycle ||
skb_zcopy(skb2) ||
(shi->nr_frags + shi2->nr_frags + 1 > MAX_SKB_FRAGS)))
share_ok = false;
skb->data_len += skb2->len;
skb->len += skb2->len;
remaining -= skb2->len;
trace_iptfs_ingress_nth_add(skb2, share_ok);
if (share_ok) {
iptfs_consume_frags(skb, skb2);
} else {
*nextp = skb2;
nextp = &skb2->next;
if (skb_has_frag_list(skb2))
nextp = iptfs_rehome_fraglist(nextp,
skb2);
skb->truesize += skb2->truesize;
}
}
xfrm_output(NULL, skb);
}
}
static enum hrtimer_restart iptfs_delay_timer(struct hrtimer *me)
{
struct sk_buff_head list;
struct xfrm_iptfs_data *xtfs;
struct xfrm_state *x;
time64_t settime;
xtfs = container_of(me, typeof(*xtfs), iptfs_timer);
x = xtfs->x;
spin_lock(&x->lock);
__skb_queue_head_init(&list);
skb_queue_splice_init(&xtfs->queue, &list);
xtfs->queue_size = 0;
settime = xtfs->iptfs_settime;
spin_unlock(&x->lock);
trace_iptfs_timer_expire(xtfs, (unsigned long long)(ktime_get_raw_fast_ns() - settime));
iptfs_output_queued(x, &list);
return HRTIMER_NORESTART;
}
static int iptfs_encap_add_ipv4(struct xfrm_state *x, struct sk_buff *skb)
{
struct dst_entry *dst = skb_dst(skb);
struct iphdr *top_iph;
skb_reset_inner_network_header(skb);
skb_reset_inner_transport_header(skb);
skb_set_network_header(skb, -(x->props.header_len - x->props.enc_hdr_len));
skb->mac_header = skb->network_header + offsetof(struct iphdr, protocol);
skb->transport_header = skb->network_header + sizeof(*top_iph);
top_iph = ip_hdr(skb);
top_iph->ihl = 5;
top_iph->version = 4;
top_iph->protocol = IPPROTO_AGGFRAG;
top_iph->tos = 0;
top_iph->frag_off = htons(IP_DF);
top_iph->ttl = ip4_dst_hoplimit(xfrm_dst_child(dst));
top_iph->saddr = x->props.saddr.a4;
top_iph->daddr = x->id.daddr.a4;
ip_select_ident(dev_net(dst->dev), skb, NULL);
return 0;
}
#if IS_ENABLED(CONFIG_IPV6)
static int iptfs_encap_add_ipv6(struct xfrm_state *x, struct sk_buff *skb)
{
struct dst_entry *dst = skb_dst(skb);
struct ipv6hdr *top_iph;
int dsfield;
skb_reset_inner_network_header(skb);
skb_reset_inner_transport_header(skb);
skb_set_network_header(skb, -x->props.header_len + x->props.enc_hdr_len);
skb->mac_header = skb->network_header + offsetof(struct ipv6hdr, nexthdr);
skb->transport_header = skb->network_header + sizeof(*top_iph);
top_iph = ipv6_hdr(skb);
top_iph->version = 6;
top_iph->priority = 0;
memset(top_iph->flow_lbl, 0, sizeof(top_iph->flow_lbl));
top_iph->nexthdr = IPPROTO_AGGFRAG;
dsfield = 0;
ipv6_change_dsfield(top_iph, 0, dsfield);
top_iph->hop_limit = ip6_dst_hoplimit(xfrm_dst_child(dst));
top_iph->saddr = *(struct in6_addr *)&x->props.saddr;
top_iph->daddr = *(struct in6_addr *)&x->id.daddr;
return 0;
}
#endif
static int iptfs_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
{
if (x->outer_mode.family == AF_INET)
return iptfs_encap_add_ipv4(x, skb);
if (x->outer_mode.family == AF_INET6) {
#if IS_ENABLED(CONFIG_IPV6)
return iptfs_encap_add_ipv6(x, skb);
#else
return -EAFNOSUPPORT;
#endif
}
return -EOPNOTSUPP;
}
static u32 __iptfs_get_inner_mtu(struct xfrm_state *x, int outer_mtu)
{
struct crypto_aead *aead;
u32 blksize;
aead = x->data;
blksize = ALIGN(crypto_aead_blocksize(aead), 4);
return ((outer_mtu - x->props.header_len - crypto_aead_authsize(aead)) &
~(blksize - 1)) - 2;
}
static u32 iptfs_get_inner_mtu(struct xfrm_state *x, int outer_mtu)
{
struct xfrm_iptfs_data *xtfs = x->mode_data;
if (!xtfs->cfg.dont_frag)
return x->outer_mode.family == AF_INET ? IP_MAX_MTU : IP6_MAX_MTU;
return __iptfs_get_inner_mtu(x, outer_mtu);
}
static int iptfs_user_init(struct net *net, struct xfrm_state *x,
struct nlattr **attrs,
struct netlink_ext_ack *extack)
{
struct xfrm_iptfs_data *xtfs = x->mode_data;
struct xfrm_iptfs_config *xc;
u64 q;
xc = &xtfs->cfg;
xc->max_queue_size = IPTFS_DEFAULT_MAX_QUEUE_SIZE;
xc->reorder_win_size = IPTFS_DEFAULT_REORDER_WINDOW;
xtfs->drop_time_ns = IPTFS_DEFAULT_DROP_TIME_USECS * NSECS_IN_USEC;
xtfs->init_delay_ns = IPTFS_DEFAULT_INIT_DELAY_USECS * NSECS_IN_USEC;
if (attrs[XFRMA_IPTFS_DONT_FRAG])
xc->dont_frag = true;
if (attrs[XFRMA_IPTFS_REORDER_WINDOW])
xc->reorder_win_size =
nla_get_u16(attrs[XFRMA_IPTFS_REORDER_WINDOW]);
if (xc->reorder_win_size) {
xtfs->w_saved = kzalloc_objs(*xtfs->w_saved,
xc->reorder_win_size);
if (!xtfs->w_saved) {
NL_SET_ERR_MSG(extack, "Cannot alloc reorder window");
return -ENOMEM;
}
}
if (attrs[XFRMA_IPTFS_PKT_SIZE]) {
xc->pkt_size = nla_get_u32(attrs[XFRMA_IPTFS_PKT_SIZE]);
if (!xc->pkt_size) {
xtfs->payload_mtu = 0;
} else if (xc->pkt_size > x->props.header_len) {
xtfs->payload_mtu = xc->pkt_size - x->props.header_len;
} else {
NL_SET_ERR_MSG(extack,
"Packet size must be 0 or greater than IPTFS/ESP header length");
return -EINVAL;
}
}
if (attrs[XFRMA_IPTFS_MAX_QSIZE])
xc->max_queue_size = nla_get_u32(attrs[XFRMA_IPTFS_MAX_QSIZE]);
if (attrs[XFRMA_IPTFS_DROP_TIME])
xtfs->drop_time_ns =
(u64)nla_get_u32(attrs[XFRMA_IPTFS_DROP_TIME]) *
NSECS_IN_USEC;
if (attrs[XFRMA_IPTFS_INIT_DELAY])
xtfs->init_delay_ns =
(u64)nla_get_u32(attrs[XFRMA_IPTFS_INIT_DELAY]) * NSECS_IN_USEC;
q = (u64)xc->max_queue_size * 95;
do_div(q, 100);
xtfs->ecn_queue_size = (u32)q;
return 0;
}
static unsigned int iptfs_sa_len(const struct xfrm_state *x)
{
struct xfrm_iptfs_data *xtfs = x->mode_data;
struct xfrm_iptfs_config *xc = &xtfs->cfg;
unsigned int l = 0;
if (x->dir == XFRM_SA_DIR_IN) {
l += nla_total_size(sizeof(u32));
l += nla_total_size(sizeof(xc->reorder_win_size));
} else {
if (xc->dont_frag)
l += nla_total_size(0);
l += nla_total_size(sizeof(u32));
l += nla_total_size(sizeof(xc->max_queue_size));
l += nla_total_size(sizeof(xc->pkt_size));
}
return l;
}
static int iptfs_copy_to_user(struct xfrm_state *x, struct sk_buff *skb)
{
struct xfrm_iptfs_data *xtfs = x->mode_data;
struct xfrm_iptfs_config *xc = &xtfs->cfg;
int ret = 0;
u64 q;
if (x->dir == XFRM_SA_DIR_IN) {
q = xtfs->drop_time_ns;
do_div(q, NSECS_IN_USEC);
ret = nla_put_u32(skb, XFRMA_IPTFS_DROP_TIME, q);
if (ret)
return ret;
ret = nla_put_u16(skb, XFRMA_IPTFS_REORDER_WINDOW,
xc->reorder_win_size);
} else {
if (xc->dont_frag) {
ret = nla_put_flag(skb, XFRMA_IPTFS_DONT_FRAG);
if (ret)
return ret;
}
q = xtfs->init_delay_ns;
do_div(q, NSECS_IN_USEC);
ret = nla_put_u32(skb, XFRMA_IPTFS_INIT_DELAY, q);
if (ret)
return ret;
ret = nla_put_u32(skb, XFRMA_IPTFS_MAX_QSIZE, xc->max_queue_size);
if (ret)
return ret;
ret = nla_put_u32(skb, XFRMA_IPTFS_PKT_SIZE, xc->pkt_size);
}
return ret;
}
static void __iptfs_init_state(struct xfrm_state *x,
struct xfrm_iptfs_data *xtfs)
{
__skb_queue_head_init(&xtfs->queue);
hrtimer_setup(&xtfs->iptfs_timer, iptfs_delay_timer, CLOCK_MONOTONIC, IPTFS_HRTIMER_MODE);
spin_lock_init(&xtfs->drop_lock);
hrtimer_setup(&xtfs->drop_timer, iptfs_drop_timer, CLOCK_MONOTONIC, IPTFS_HRTIMER_MODE);
if (x->props.family == AF_INET)
x->props.header_len += sizeof(struct iphdr) + sizeof(struct ip_iptfs_hdr);
else if (x->props.family == AF_INET6)
x->props.header_len += sizeof(struct ipv6hdr) + sizeof(struct ip_iptfs_hdr);
x->props.enc_hdr_len = sizeof(struct ip_iptfs_hdr);
if (x->mode_data != xtfs)
__module_get(x->mode_cbs->owner);
x->mode_data = xtfs;
xtfs->x = x;
}
static int iptfs_clone_state(struct xfrm_state *x, struct xfrm_state *orig)
{
struct skb_wseq *w_saved = NULL;
struct xfrm_iptfs_data *xtfs;
xtfs = kmemdup(orig->mode_data, sizeof(*xtfs), GFP_KERNEL);
if (!xtfs)
return -ENOMEM;
if (xtfs->cfg.reorder_win_size) {
w_saved = kzalloc_objs(*w_saved, xtfs->cfg.reorder_win_size);
if (!w_saved) {
kfree_sensitive(xtfs);
return -ENOMEM;
}
}
xtfs->w_saved = w_saved;
__skb_queue_head_init(&xtfs->queue);
xtfs->queue_size = 0;
hrtimer_setup(&xtfs->iptfs_timer, iptfs_delay_timer, CLOCK_MONOTONIC,
IPTFS_HRTIMER_MODE);
spin_lock_init(&xtfs->drop_lock);
hrtimer_setup(&xtfs->drop_timer, iptfs_drop_timer, CLOCK_MONOTONIC,
IPTFS_HRTIMER_MODE);
xtfs->w_seq_set = false;
xtfs->w_wantseq = 0;
xtfs->w_savedlen = 0;
xtfs->ra_newskb = NULL;
xtfs->ra_wantseq = 0;
xtfs->ra_runtlen = 0;
__module_get(x->mode_cbs->owner);
x->mode_data = xtfs;
xtfs->x = x;
return 0;
}
static int iptfs_init_state(struct xfrm_state *x)
{
struct xfrm_iptfs_data *xtfs;
if (x->mode_data) {
xtfs = x->mode_data;
} else {
xtfs = kzalloc_obj(*xtfs);
if (!xtfs)
return -ENOMEM;
}
__iptfs_init_state(x, xtfs);
return 0;
}
static void iptfs_destroy_state(struct xfrm_state *x)
{
struct xfrm_iptfs_data *xtfs = x->mode_data;
struct sk_buff_head list;
struct skb_wseq *s, *se;
struct sk_buff *skb;
if (!xtfs)
return;
hrtimer_cancel(&xtfs->iptfs_timer);
spin_lock_bh(&xtfs->x->lock);
__skb_queue_head_init(&list);
skb_queue_splice_init(&xtfs->queue, &list);
spin_unlock_bh(&xtfs->x->lock);
while ((skb = __skb_dequeue(&list)))
kfree_skb(skb);
hrtimer_cancel(&xtfs->drop_timer);
if (xtfs->ra_newskb)
kfree_skb(xtfs->ra_newskb);
for (s = xtfs->w_saved, se = s + xtfs->w_savedlen; s < se; s++) {
if (s->skb)
kfree_skb(s->skb);
}
kfree_sensitive(xtfs->w_saved);
kfree_sensitive(xtfs);
module_put(x->mode_cbs->owner);
}
static const struct xfrm_mode_cbs iptfs_mode_cbs = {
.owner = THIS_MODULE,
.init_state = iptfs_init_state,
.clone_state = iptfs_clone_state,
.destroy_state = iptfs_destroy_state,
.user_init = iptfs_user_init,
.copy_to_user = iptfs_copy_to_user,
.sa_len = iptfs_sa_len,
.get_inner_mtu = iptfs_get_inner_mtu,
.input = iptfs_input,
.output = iptfs_output_collect,
.prepare_output = iptfs_prepare_output,
};
static int __init xfrm_iptfs_init(void)
{
int err;
pr_info("xfrm_iptfs: IPsec IP-TFS tunnel mode module\n");
err = xfrm_register_mode_cbs(XFRM_MODE_IPTFS, &iptfs_mode_cbs);
if (err < 0)
pr_info("%s: can't register IP-TFS\n", __func__);
return err;
}
static void __exit xfrm_iptfs_fini(void)
{
xfrm_unregister_mode_cbs(XFRM_MODE_IPTFS);
}
module_init(xfrm_iptfs_init);
module_exit(xfrm_iptfs_fini);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("IP-TFS support for xfrm ipsec tunnels");