Skip to content

Commit 2efdaaa

Browse files
liuhangbinkuba-moo
authored andcommitted
IPv6: reply ICMP error if the first fragment don't include all headers
Based on RFC 8200, Section 4.5 Fragment Header: - If the first fragment does not include all headers through an Upper-Layer header, then that fragment should be discarded and an ICMP Parameter Problem, Code 3, message should be sent to the source of the fragment, with the Pointer field set to zero. Checking each packet header in IPv6 fast path will have performance impact, so I put the checking in ipv6_frag_rcv(). As the packet may be any kind of L4 protocol, I only checked some common protocols' header length and handle others by (offset + 1) > skb->len. Also use !(frag_off & htons(IP6_OFFSET)) to catch atomic fragments (fragmented packet with only one fragment). When send ICMP error message, if the 1st truncated fragment is ICMP message, icmp6_send() will break as is_ineligible() return true. So I added a check in is_ineligible() to let fragment packet with nexthdr ICMP but no ICMP header return false. Signed-off-by: Hangbin Liu <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]>
1 parent b59e286 commit 2efdaaa

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

net/ipv6/icmp.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,13 @@ static bool is_ineligible(const struct sk_buff *skb)
158158
tp = skb_header_pointer(skb,
159159
ptr+offsetof(struct icmp6hdr, icmp6_type),
160160
sizeof(_type), &_type);
161-
if (!tp || !(*tp & ICMPV6_INFOMSG_MASK))
161+
162+
/* Based on RFC 8200, Section 4.5 Fragment Header, return
163+
* false if this is a fragment packet with no icmp header info.
164+
*/
165+
if (!tp && frag_off != 0)
166+
return false;
167+
else if (!tp || !(*tp & ICMPV6_INFOMSG_MASK))
162168
return true;
163169
}
164170
return false;

net/ipv6/reassembly.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#include <linux/skbuff.h>
4343
#include <linux/slab.h>
4444
#include <linux/export.h>
45+
#include <linux/tcp.h>
46+
#include <linux/udp.h>
4547

4648
#include <net/sock.h>
4749
#include <net/snmp.h>
@@ -322,7 +324,9 @@ static int ipv6_frag_rcv(struct sk_buff *skb)
322324
struct frag_queue *fq;
323325
const struct ipv6hdr *hdr = ipv6_hdr(skb);
324326
struct net *net = dev_net(skb_dst(skb)->dev);
325-
int iif;
327+
__be16 frag_off;
328+
int iif, offset;
329+
u8 nexthdr;
326330

327331
if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
328332
goto fail_hdr;
@@ -351,6 +355,33 @@ static int ipv6_frag_rcv(struct sk_buff *skb)
351355
return 1;
352356
}
353357

358+
/* RFC 8200, Section 4.5 Fragment Header:
359+
* If the first fragment does not include all headers through an
360+
* Upper-Layer header, then that fragment should be discarded and
361+
* an ICMP Parameter Problem, Code 3, message should be sent to
362+
* the source of the fragment, with the Pointer field set to zero.
363+
*/
364+
nexthdr = hdr->nexthdr;
365+
offset = ipv6_skip_exthdr(skb, skb_transport_offset(skb), &nexthdr, &frag_off);
366+
if (offset >= 0) {
367+
/* Check some common protocols' header */
368+
if (nexthdr == IPPROTO_TCP)
369+
offset += sizeof(struct tcphdr);
370+
else if (nexthdr == IPPROTO_UDP)
371+
offset += sizeof(struct udphdr);
372+
else if (nexthdr == IPPROTO_ICMPV6)
373+
offset += sizeof(struct icmp6hdr);
374+
else
375+
offset += 1;
376+
377+
if (!(frag_off & htons(IP6_OFFSET)) && offset > skb->len) {
378+
__IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
379+
IPSTATS_MIB_INHDRERRORS);
380+
icmpv6_param_prob(skb, ICMPV6_HDR_INCOMP, 0);
381+
return -1;
382+
}
383+
}
384+
354385
iif = skb->dev ? skb->dev->ifindex : 0;
355386
fq = fq_find(net, fhdr->identification, hdr, iif);
356387
if (fq) {

0 commit comments

Comments
 (0)