Skip to content

Commit b6fc095

Browse files
committed
gtp: properly parse extension headers
Currently GTP packets are dropped if the next extension field is set to non-zero value, but this are valid GTP packets. TS 29.281 provides a longer header format, which is defined as struct gtp1_header_long. Such long header format is used if any of the S, PN, E flags is set. This long header is 4 bytes longer than struct gtp1_header, plus variable length (optional) extension headers. The next extension header field is zero is no extension header is provided. The extension header is composed of a length field which includes total number of 4 byte words including the extension header itself (1 byte), payload (variable length) and next type (1 byte). The extension header size and its payload is aligned to 4 bytes. A GTP packet might come with a chain extensions headers, which makes it slightly cumbersome to parse because the extension next header field comes at the end of the extension header, and there is a need to check if this field becomes zero to stop the extension header parser. Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 353f5ff commit b6fc095

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

drivers/net/gtp.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,43 @@ static int gtp1u_handle_echo_resp(struct gtp_dev *gtp, struct sk_buff *skb)
567567
msg, 0, GTP_GENL_MCGRP, GFP_ATOMIC);
568568
}
569569

570+
static int gtp_parse_exthdrs(struct sk_buff *skb, unsigned int *hdrlen)
571+
{
572+
struct gtp_ext_hdr *gtp_exthdr, _gtp_exthdr;
573+
unsigned int offset = *hdrlen;
574+
__u8 *next_type, _next_type;
575+
576+
/* From 29.060: "The Extension Header Length field specifies the length
577+
* of the particular Extension header in 4 octets units."
578+
*
579+
* This length field includes length field size itself (1 byte),
580+
* payload (variable length) and next type (1 byte). The extension
581+
* header is aligned to to 4 bytes.
582+
*/
583+
584+
do {
585+
gtp_exthdr = skb_header_pointer(skb, offset, sizeof(*gtp_exthdr),
586+
&_gtp_exthdr);
587+
if (!gtp_exthdr || !gtp_exthdr->len)
588+
return -1;
589+
590+
offset += gtp_exthdr->len * 4;
591+
592+
/* From 29.060: "If no such Header follows, then the value of
593+
* the Next Extension Header Type shall be 0."
594+
*/
595+
next_type = skb_header_pointer(skb, offset - 1,
596+
sizeof(_next_type), &_next_type);
597+
if (!next_type)
598+
return -1;
599+
600+
} while (*next_type != 0);
601+
602+
*hdrlen = offset;
603+
604+
return 0;
605+
}
606+
570607
static int gtp1u_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
571608
{
572609
unsigned int hdrlen = sizeof(struct udphdr) +
@@ -616,6 +653,10 @@ static int gtp1u_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
616653
return 1;
617654
}
618655

656+
if (gtp1->flags & GTP1_F_EXTHDR &&
657+
gtp_parse_exthdrs(skb, &hdrlen) < 0)
658+
return -1;
659+
619660
return gtp_rx(pctx, skb, hdrlen, gtp->role);
620661
}
621662

include/net/gtp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,9 @@ static inline bool netif_is_gtp(const struct net_device *dev)
7878
#define GTP1_F_EXTHDR 0x04
7979
#define GTP1_F_MASK 0x07
8080

81+
struct gtp_ext_hdr {
82+
__u8 len;
83+
__u8 data[];
84+
};
85+
8186
#endif

0 commit comments

Comments
 (0)