📄 nf_conntrack_reasm.c
字号:
/* * IPv6 fragment reassembly for connection tracking * * Copyright (C)2004 USAGI/WIDE Project * * Author: * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> * * Based on: net/ipv6/reassembly.c * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */#include <linux/errno.h>#include <linux/types.h>#include <linux/string.h>#include <linux/socket.h>#include <linux/sockios.h>#include <linux/jiffies.h>#include <linux/net.h>#include <linux/list.h>#include <linux/netdevice.h>#include <linux/in6.h>#include <linux/ipv6.h>#include <linux/icmpv6.h>#include <linux/random.h>#include <linux/jhash.h>#include <net/sock.h>#include <net/snmp.h>#include <net/inet_frag.h>#include <net/ipv6.h>#include <net/protocol.h>#include <net/transp_v6.h>#include <net/rawv6.h>#include <net/ndisc.h>#include <net/addrconf.h>#include <linux/sysctl.h>#include <linux/netfilter.h>#include <linux/netfilter_ipv6.h>#include <linux/kernel.h>#include <linux/module.h>#define NF_CT_FRAG6_HIGH_THRESH 262144 /* == 256*1024 */#define NF_CT_FRAG6_LOW_THRESH 196608 /* == 192*1024 */#define NF_CT_FRAG6_TIMEOUT IPV6_FRAG_TIMEOUTstruct nf_ct_frag6_skb_cb{ struct inet6_skb_parm h; int offset; struct sk_buff *orig;};#define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))struct nf_ct_frag6_queue{ struct inet_frag_queue q; __be32 id; /* fragment id */ struct in6_addr saddr; struct in6_addr daddr; unsigned int csum; __u16 nhoffset;};struct inet_frags_ctl nf_frags_ctl __read_mostly = { .high_thresh = 256 * 1024, .low_thresh = 192 * 1024, .timeout = IPV6_FRAG_TIMEOUT, .secret_interval = 10 * 60 * HZ,};static struct inet_frags nf_frags;static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr, struct in6_addr *daddr){ u32 a, b, c; a = (__force u32)saddr->s6_addr32[0]; b = (__force u32)saddr->s6_addr32[1]; c = (__force u32)saddr->s6_addr32[2]; a += JHASH_GOLDEN_RATIO; b += JHASH_GOLDEN_RATIO; c += nf_frags.rnd; __jhash_mix(a, b, c); a += (__force u32)saddr->s6_addr32[3]; b += (__force u32)daddr->s6_addr32[0]; c += (__force u32)daddr->s6_addr32[1]; __jhash_mix(a, b, c); a += (__force u32)daddr->s6_addr32[2]; b += (__force u32)daddr->s6_addr32[3]; c += (__force u32)id; __jhash_mix(a, b, c); return c & (INETFRAGS_HASHSZ - 1);}static unsigned int nf_hashfn(struct inet_frag_queue *q){ struct nf_ct_frag6_queue *nq; nq = container_of(q, struct nf_ct_frag6_queue, q); return ip6qhashfn(nq->id, &nq->saddr, &nq->daddr);}static void nf_skb_free(struct sk_buff *skb){ if (NFCT_FRAG6_CB(skb)->orig) kfree_skb(NFCT_FRAG6_CB(skb)->orig);}/* Memory Tracking Functions. */static inline void frag_kfree_skb(struct sk_buff *skb, unsigned int *work){ if (work) *work -= skb->truesize; atomic_sub(skb->truesize, &nf_frags.mem); nf_skb_free(skb); kfree_skb(skb);}/* Destruction primitives. */static __inline__ void fq_put(struct nf_ct_frag6_queue *fq){ inet_frag_put(&fq->q, &nf_frags);}/* Kill fq entry. It is not destroyed immediately, * because caller (and someone more) holds reference count. */static __inline__ void fq_kill(struct nf_ct_frag6_queue *fq){ inet_frag_kill(&fq->q, &nf_frags);}static void nf_ct_frag6_evictor(void){ inet_frag_evictor(&nf_frags);}static void nf_ct_frag6_expire(unsigned long data){ struct nf_ct_frag6_queue *fq; fq = container_of((struct inet_frag_queue *)data, struct nf_ct_frag6_queue, q); spin_lock(&fq->q.lock); if (fq->q.last_in & COMPLETE) goto out; fq_kill(fq);out: spin_unlock(&fq->q.lock); fq_put(fq);}/* Creation primitives. */static __inline__ struct nf_ct_frag6_queue *fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst){ struct inet_frag_queue *q; struct ip6_create_arg arg; unsigned int hash; arg.id = id; arg.src = src; arg.dst = dst; hash = ip6qhashfn(id, src, dst); q = inet_frag_find(&nf_frags, &arg, hash); if (q == NULL) goto oom; return container_of(q, struct nf_ct_frag6_queue, q);oom: pr_debug("Can't alloc new queue\n"); return NULL;}static int nf_ct_frag6_queue(struct nf_ct_frag6_queue *fq, struct sk_buff *skb, struct frag_hdr *fhdr, int nhoff){ struct sk_buff *prev, *next; int offset, end; if (fq->q.last_in & COMPLETE) { pr_debug("Allready completed\n"); goto err; } offset = ntohs(fhdr->frag_off) & ~0x7; end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); if ((unsigned int)end > IPV6_MAXPLEN) { pr_debug("offset is too large.\n"); return -1; } if (skb->ip_summed == CHECKSUM_COMPLETE) { const unsigned char *nh = skb_network_header(skb); skb->csum = csum_sub(skb->csum, csum_partial(nh, (u8 *)(fhdr + 1) - nh, 0)); } /* Is this the final fragment? */ if (!(fhdr->frag_off & htons(IP6_MF))) { /* If we already have some bits beyond end * or have different end, the segment is corrupted. */ if (end < fq->q.len || ((fq->q.last_in & LAST_IN) && end != fq->q.len)) { pr_debug("already received last fragment\n"); goto err; } fq->q.last_in |= LAST_IN; fq->q.len = end; } else { /* Check if the fragment is rounded to 8 bytes. * Required by the RFC. */ if (end & 0x7) { /* RFC2460 says always send parameter problem in * this case. -DaveM */ pr_debug("end of fragment not rounded to 8 bytes.\n"); return -1; } if (end > fq->q.len) { /* Some bits beyond end -> corruption. */ if (fq->q.last_in & LAST_IN) { pr_debug("last packet already reached.\n"); goto err; } fq->q.len = end; } } if (end == offset) goto err; /* Point into the IP datagram 'data' part. */ if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) { pr_debug("queue: message is too short.\n"); goto err; } if (pskb_trim_rcsum(skb, end - offset)) { pr_debug("Can't trim\n"); goto err; } /* Find out which fragments are in front and at the back of us * in the chain of fragments so far. We must know where to put * this fragment, right? */ prev = NULL; for (next = fq->q.fragments; next != NULL; next = next->next) { if (NFCT_FRAG6_CB(next)->offset >= offset) break; /* bingo! */ prev = next; } /* We found where to put this one. Check for overlap with * preceding fragment, and, if needed, align things so that * any overlaps are eliminated. */ if (prev) { int i = (NFCT_FRAG6_CB(prev)->offset + prev->len) - offset; if (i > 0) { offset += i; if (end <= offset) { pr_debug("overlap\n"); goto err; } if (!pskb_pull(skb, i)) { pr_debug("Can't pull\n"); goto err; } if (skb->ip_summed != CHECKSUM_UNNECESSARY) skb->ip_summed = CHECKSUM_NONE; } } /* Look for overlap with succeeding segments. * If we can merge fragments, do it. */ while (next && NFCT_FRAG6_CB(next)->offset < end) { /* overlap is 'i' bytes */ int i = end - NFCT_FRAG6_CB(next)->offset; if (i < next->len) { /* Eat head of the next overlapped fragment * and leave the loop. The next ones cannot overlap. */ pr_debug("Eat head of the overlapped parts.: %d", i); if (!pskb_pull(next, i)) goto err; /* next fragment */ NFCT_FRAG6_CB(next)->offset += i; fq->q.meat -= i; if (next->ip_summed != CHECKSUM_UNNECESSARY) next->ip_summed = CHECKSUM_NONE; break; } else { struct sk_buff *free_it = next; /* Old fragmnet is completely overridden with * new one drop it. */ next = next->next; if (prev) prev->next = next; else fq->q.fragments = next; fq->q.meat -= free_it->len; frag_kfree_skb(free_it, NULL); } } NFCT_FRAG6_CB(skb)->offset = offset; /* Insert this fragment in the chain of fragments. */ skb->next = next; if (prev) prev->next = skb; else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -