Skip to content

Commit 5dec597

Browse files
teknoraverdavem330
authored andcommitted
flow_dissector: extract more ICMP information
The ICMP flow dissector currently parses only the Type and Code fields. Some ICMP packets (echo, timestamp) have a 16 bit Identifier field which is used to correlate packets. Add such field in flow_dissector_key_icmp and replace skb_flow_get_be16() with a more complex function which populate this field. Signed-off-by: Matteo Croce <mcroce@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 3b336d6 commit 5dec597

2 files changed

Lines changed: 61 additions & 32 deletions

File tree

‎include/net/flow_dissector.h‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <linux/in6.h>
77
#include <uapi/linux/if_ether.h>
88

9+
struct sk_buff;
10+
911
/**
1012
* struct flow_dissector_key_control:
1113
* @thoff: Transport header offset
@@ -156,19 +158,16 @@ struct flow_dissector_key_ports {
156158

157159
/**
158160
* flow_dissector_key_icmp:
159-
* @ports: type and code of ICMP header
160-
* icmp: ICMP type (high) and code (low)
161161
* type: ICMP type
162162
* code: ICMP code
163+
* id: session identifier
163164
*/
164165
struct flow_dissector_key_icmp {
165-
union {
166-
__be16 icmp;
167-
struct {
168-
u8 type;
169-
u8 code;
170-
};
166+
struct {
167+
u8 type;
168+
u8 code;
171169
};
170+
u16 id;
172171
};
173172

174173
/**
@@ -282,6 +281,7 @@ struct flow_keys {
282281
struct flow_dissector_key_vlan cvlan;
283282
struct flow_dissector_key_keyid keyid;
284283
struct flow_dissector_key_ports ports;
284+
struct flow_dissector_key_icmp icmp;
285285
/* 'addrs' must be the last member */
286286
struct flow_dissector_key_addrs addrs;
287287
};
@@ -316,6 +316,9 @@ static inline bool flow_keys_have_l4(const struct flow_keys *keys)
316316
}
317317

318318
u32 flow_hash_from_keys(struct flow_keys *keys);
319+
void skb_flow_get_icmp_tci(const struct sk_buff *skb,
320+
struct flow_dissector_key_icmp *key_icmp,
321+
void *data, int thoff, int hlen);
319322

320323
static inline bool dissector_uses_key(const struct flow_dissector *flow_dissector,
321324
enum flow_dissector_key_id key_id)

‎net/core/flow_dissector.c‎

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -178,27 +178,6 @@ int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
178178
mutex_unlock(&flow_dissector_mutex);
179179
return 0;
180180
}
181-
/**
182-
* skb_flow_get_be16 - extract be16 entity
183-
* @skb: sk_buff to extract from
184-
* @poff: offset to extract at
185-
* @data: raw buffer pointer to the packet
186-
* @hlen: packet header length
187-
*
188-
* The function will try to retrieve a be32 entity at
189-
* offset poff
190-
*/
191-
static __be16 skb_flow_get_be16(const struct sk_buff *skb, int poff,
192-
void *data, int hlen)
193-
{
194-
__be16 *u, _u;
195-
196-
u = __skb_header_pointer(skb, poff, sizeof(_u), data, hlen, &_u);
197-
if (u)
198-
return *u;
199-
200-
return 0;
201-
}
202181

203182
/**
204183
* __skb_flow_get_ports - extract the upper layer ports and return them
@@ -234,8 +213,54 @@ __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
234213
}
235214
EXPORT_SYMBOL(__skb_flow_get_ports);
236215

237-
/* If FLOW_DISSECTOR_KEY_ICMP is set, get the Type and Code from an ICMP packet
238-
* using skb_flow_get_be16().
216+
static bool icmp_has_id(u8 type)
217+
{
218+
switch (type) {
219+
case ICMP_ECHO:
220+
case ICMP_ECHOREPLY:
221+
case ICMP_TIMESTAMP:
222+
case ICMP_TIMESTAMPREPLY:
223+
case ICMPV6_ECHO_REQUEST:
224+
case ICMPV6_ECHO_REPLY:
225+
return true;
226+
}
227+
228+
return false;
229+
}
230+
231+
/**
232+
* skb_flow_get_icmp_tci - extract ICMP(6) Type, Code and Identifier fields
233+
* @skb: sk_buff to extract from
234+
* @key_icmp: struct flow_dissector_key_icmp to fill
235+
* @data: raw buffer pointer to the packet
236+
* @toff: offset to extract at
237+
* @hlen: packet header length
238+
*/
239+
void skb_flow_get_icmp_tci(const struct sk_buff *skb,
240+
struct flow_dissector_key_icmp *key_icmp,
241+
void *data, int thoff, int hlen)
242+
{
243+
struct icmphdr *ih, _ih;
244+
245+
ih = __skb_header_pointer(skb, thoff, sizeof(_ih), data, hlen, &_ih);
246+
if (!ih)
247+
return;
248+
249+
key_icmp->type = ih->type;
250+
key_icmp->code = ih->code;
251+
252+
/* As we use 0 to signal that the Id field is not present,
253+
* avoid confusion with packets without such field
254+
*/
255+
if (icmp_has_id(ih->type))
256+
key_icmp->id = ih->un.echo.id ? : 1;
257+
else
258+
key_icmp->id = 0;
259+
}
260+
EXPORT_SYMBOL(skb_flow_get_icmp_tci);
261+
262+
/* If FLOW_DISSECTOR_KEY_ICMP is set, dissect an ICMP packet
263+
* using skb_flow_get_icmp_tci().
239264
*/
240265
static void __skb_flow_dissect_icmp(const struct sk_buff *skb,
241266
struct flow_dissector *flow_dissector,
@@ -250,7 +275,8 @@ static void __skb_flow_dissect_icmp(const struct sk_buff *skb,
250275
key_icmp = skb_flow_dissector_target(flow_dissector,
251276
FLOW_DISSECTOR_KEY_ICMP,
252277
target_container);
253-
key_icmp->icmp = skb_flow_get_be16(skb, thoff, data, hlen);
278+
279+
skb_flow_get_icmp_tci(skb, key_icmp, data, thoff, hlen);
254280
}
255281

256282
void skb_flow_dissect_meta(const struct sk_buff *skb,

0 commit comments

Comments
 (0)