Skip to content

Commit eba75c5

Browse files
wdebruijdavem330
authored andcommitted
icmp: support rfc 4884
Add setsockopt SOL_IP/IP_RECVERR_4884 to return the offset to an extension struct if present. ICMP messages may include an extension structure after the original datagram. RFC 4884 standardized this behavior. It stores the offset in words to the extension header in u8 icmphdr.un.reserved[1]. The field is valid only for ICMP types destination unreachable, time exceeded and parameter problem, if length is at least 128 bytes and entire packet does not exceed 576 bytes. Return the offset to the start of the extension struct when reading an ICMP error from the error queue, if it matches the above constraints. Do not return the raw u8 field. Return the offset from the start of the user buffer, in bytes. The kernel does not return the network and transport headers, so subtract those. Also validate the headers. Return the offset regardless of validation, as an invalid extension must still not be misinterpreted as part of the original datagram. Note that !invalid does not imply valid. If the extension version does not match, no validation can take place, for instance. For backward compatibility, make this optional, set by setsockopt SOL_IP/IP_RECVERR_RFC4884. For API example and feature test, see github.com/wdebruij/kerneltools/blob/master/tests/recv_icmp_v2.c For forward compatibility, reserve only setsockopt value 1, leaving other bits for additional icmp extensions. Changes v1->v2: - convert word offset to byte offset from start of user buffer - return in ee_data as u8 may be insufficient - define extension struct and object header structs - return len only if constraints met - if returning len, also validate Signed-off-by: Willem de Bruijn <willemb@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 930bc4c commit eba75c5

7 files changed

Lines changed: 124 additions & 1 deletion

File tree

‎include/linux/icmp.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <linux/skbuff.h>
1717
#include <uapi/linux/icmp.h>
18+
#include <uapi/linux/errqueue.h>
1819

1920
static inline struct icmphdr *icmp_hdr(const struct sk_buff *skb)
2021
{
@@ -35,4 +36,7 @@ static inline bool icmp_is_err(int type)
3536
return false;
3637
}
3738

39+
void ip_icmp_error_rfc4884(const struct sk_buff *skb,
40+
struct sock_ee_data_rfc4884 *out);
41+
3842
#endif /* _LINUX_ICMP_H */

‎include/net/inet_sock.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ struct inet_sock {
225225
mc_all:1,
226226
nodefrag:1;
227227
__u8 bind_address_no_port:1,
228+
recverr_rfc4884:1,
228229
defer_connect:1; /* Indicates that fastopen_connect is set
229230
* and cookie exists so we defer connect
230231
* until first data frame is written

‎include/uapi/linux/errqueue.h‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@
55
#include <linux/types.h>
66
#include <linux/time_types.h>
77

8+
/* RFC 4884: return offset to extension struct + validation */
9+
struct sock_ee_data_rfc4884 {
10+
__u16 len;
11+
__u8 flags;
12+
__u8 reserved;
13+
};
14+
815
struct sock_extended_err {
916
__u32 ee_errno;
1017
__u8 ee_origin;
1118
__u8 ee_type;
1219
__u8 ee_code;
1320
__u8 ee_pad;
1421
__u32 ee_info;
15-
__u32 ee_data;
22+
union {
23+
__u32 ee_data;
24+
struct sock_ee_data_rfc4884 ee_rfc4884;
25+
};
1626
};
1727

1828
#define SO_EE_ORIGIN_NONE 0
@@ -31,6 +41,8 @@ struct sock_extended_err {
3141
#define SO_EE_CODE_TXTIME_INVALID_PARAM 1
3242
#define SO_EE_CODE_TXTIME_MISSED 2
3343

44+
#define SO_EE_RFC4884_FLAG_INVALID 1
45+
3446
/**
3547
* struct scm_timestamping - timestamps exposed through cmsg
3648
*

‎include/uapi/linux/icmp.h‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define _UAPI_LINUX_ICMP_H
2020

2121
#include <linux/types.h>
22+
#include <asm/byteorder.h>
2223

2324
#define ICMP_ECHOREPLY 0 /* Echo Reply */
2425
#define ICMP_DEST_UNREACH 3 /* Destination Unreachable */
@@ -95,5 +96,26 @@ struct icmp_filter {
9596
__u32 data;
9697
};
9798

99+
/* RFC 4884 extension struct: one per message */
100+
struct icmp_ext_hdr {
101+
#if defined(__LITTLE_ENDIAN_BITFIELD)
102+
__u8 reserved1:4,
103+
version:4;
104+
#elif defined(__BIG_ENDIAN_BITFIELD)
105+
__u8 version:4,
106+
reserved1:4;
107+
#else
108+
#error "Please fix <asm/byteorder.h>"
109+
#endif
110+
__u8 reserved2;
111+
__sum16 checksum;
112+
};
113+
114+
/* RFC 4884 extension object header: one for each object */
115+
struct icmp_extobj_hdr {
116+
__be16 length;
117+
__u8 class_num;
118+
__u8 class_type;
119+
};
98120

99121
#endif /* _UAPI_LINUX_ICMP_H */

‎include/uapi/linux/in.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ struct in_addr {
123123
#define IP_CHECKSUM 23
124124
#define IP_BIND_ADDRESS_NO_PORT 24
125125
#define IP_RECVFRAGSIZE 25
126+
#define IP_RECVERR_RFC4884 26
126127

127128
/* IP_MTU_DISCOVER values */
128129
#define IP_PMTUDISC_DONT 0 /* Never send DF frames */

‎net/ipv4/icmp.c‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,77 @@ int icmp_rcv(struct sk_buff *skb)
11161116
goto drop;
11171117
}
11181118

1119+
static bool ip_icmp_error_rfc4884_validate(const struct sk_buff *skb, int off)
1120+
{
1121+
struct icmp_extobj_hdr *objh, _objh;
1122+
struct icmp_ext_hdr *exth, _exth;
1123+
u16 olen;
1124+
1125+
exth = skb_header_pointer(skb, off, sizeof(_exth), &_exth);
1126+
if (!exth)
1127+
return false;
1128+
if (exth->version != 2)
1129+
return true;
1130+
1131+
if (exth->checksum &&
1132+
csum_fold(skb_checksum(skb, off, skb->len - off, 0)))
1133+
return false;
1134+
1135+
off += sizeof(_exth);
1136+
while (off < skb->len) {
1137+
objh = skb_header_pointer(skb, off, sizeof(_objh), &_objh);
1138+
if (!objh)
1139+
return false;
1140+
1141+
olen = ntohs(objh->length);
1142+
if (olen < sizeof(_objh))
1143+
return false;
1144+
1145+
off += olen;
1146+
if (off > skb->len)
1147+
return false;
1148+
}
1149+
1150+
return true;
1151+
}
1152+
1153+
void ip_icmp_error_rfc4884(const struct sk_buff *skb,
1154+
struct sock_ee_data_rfc4884 *out)
1155+
{
1156+
int hlen, off;
1157+
1158+
switch (icmp_hdr(skb)->type) {
1159+
case ICMP_DEST_UNREACH:
1160+
case ICMP_TIME_EXCEEDED:
1161+
case ICMP_PARAMETERPROB:
1162+
break;
1163+
default:
1164+
return;
1165+
}
1166+
1167+
/* outer headers up to inner iph. skb->data is at inner payload */
1168+
hlen = -skb_transport_offset(skb) - sizeof(struct icmphdr);
1169+
1170+
/* per rfc 791: maximum packet length of 576 bytes */
1171+
if (hlen + skb->len > 576)
1172+
return;
1173+
1174+
/* per rfc 4884: minimal datagram length of 128 bytes */
1175+
off = icmp_hdr(skb)->un.reserved[1] * sizeof(u32);
1176+
if (off < 128)
1177+
return;
1178+
1179+
/* kernel has stripped headers: return payload offset in bytes */
1180+
off -= hlen;
1181+
if (off + sizeof(struct icmp_ext_hdr) > skb->len)
1182+
return;
1183+
1184+
out->len = off;
1185+
1186+
if (!ip_icmp_error_rfc4884_validate(skb, off))
1187+
out->flags |= SO_EE_RFC4884_FLAG_INVALID;
1188+
}
1189+
11191190
int icmp_err(struct sk_buff *skb, u32 info)
11201191
{
11211192
struct iphdr *iph = (struct iphdr *)skb->data;

‎net/ipv4/ip_sockglue.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
411411
serr->port = port;
412412

413413
if (skb_pull(skb, payload - skb->data)) {
414+
if (inet_sk(sk)->recverr_rfc4884)
415+
ip_icmp_error_rfc4884(skb, &serr->ee.ee_rfc4884);
416+
414417
skb_reset_transport_header(skb);
415418
if (sock_queue_err_skb(sk, skb) == 0)
416419
return;
@@ -904,6 +907,7 @@ static int do_ip_setsockopt(struct sock *sk, int level,
904907
case IP_RECVORIGDSTADDR:
905908
case IP_CHECKSUM:
906909
case IP_RECVFRAGSIZE:
910+
case IP_RECVERR_RFC4884:
907911
if (optlen >= sizeof(int)) {
908912
if (get_user(val, (int __user *) optval))
909913
return -EFAULT;
@@ -1063,6 +1067,11 @@ static int do_ip_setsockopt(struct sock *sk, int level,
10631067
if (!val)
10641068
skb_queue_purge(&sk->sk_error_queue);
10651069
break;
1070+
case IP_RECVERR_RFC4884:
1071+
if (val < 0 || val > 1)
1072+
goto e_inval;
1073+
inet->recverr_rfc4884 = !!val;
1074+
break;
10661075
case IP_MULTICAST_TTL:
10671076
if (sk->sk_type == SOCK_STREAM)
10681077
goto e_inval;
@@ -1611,6 +1620,9 @@ static int do_ip_getsockopt(struct sock *sk, int level, int optname,
16111620
case IP_RECVERR:
16121621
val = inet->recverr;
16131622
break;
1623+
case IP_RECVERR_RFC4884:
1624+
val = inet->recverr_rfc4884;
1625+
break;
16141626
case IP_MULTICAST_TTL:
16151627
val = inet->mc_ttl;
16161628
break;

0 commit comments

Comments
 (0)