40
40
#define AF_INET 2
41
41
#define AF_INET6 10
42
42
#define TASK_COMM_LEN 16
43
+ #define IPPROTO_ICMPV6 58
43
44
44
45
#define OK 1
45
46
#define NOK 0
@@ -112,9 +113,9 @@ static inline int process_ip4(struct iphdr *ip4, void *data_end, statkey *key) {
112
113
113
114
key -> src_port = bpf_ntohs (tcp -> source );
114
115
key -> dst_port = bpf_ntohs (tcp -> dest );
115
- }
116
116
117
- break ;
117
+ break ;
118
+ }
118
119
case IPPROTO_UDP : {
119
120
struct udphdr * udp = (void * )ip4 + sizeof (* ip4 );
120
121
@@ -125,9 +126,24 @@ static inline int process_ip4(struct iphdr *ip4, void *data_end, statkey *key) {
125
126
126
127
key -> src_port = bpf_ntohs (udp -> source );
127
128
key -> dst_port = bpf_ntohs (udp -> dest );
129
+
130
+ break ;
128
131
}
132
+ case IPPROTO_ICMP : {
133
+ struct icmphdr * icmp = (void * )ip4 + sizeof (* ip4 );
129
134
130
- break ;
135
+ // validate ICMP size
136
+ if ((void * )icmp + sizeof (* icmp ) > data_end ) {
137
+ return NOK ;
138
+ }
139
+
140
+ // store ICMP type in src port
141
+ key -> src_port = icmp -> type ;
142
+ // store ICMP code in dst port
143
+ key -> dst_port = icmp -> code ;
144
+
145
+ break ;
146
+ }
131
147
}
132
148
133
149
return OK ;
@@ -167,9 +183,9 @@ static inline int process_ip6(struct ipv6hdr *ip6, void *data_end,
167
183
168
184
key -> src_port = bpf_ntohs (tcp -> source );
169
185
key -> dst_port = bpf_ntohs (tcp -> dest );
170
- }
171
186
172
- break ;
187
+ break ;
188
+ }
173
189
case IPPROTO_UDP : {
174
190
struct udphdr * udp = (void * )ip6 + sizeof (* ip6 );
175
191
@@ -180,17 +196,32 @@ static inline int process_ip6(struct ipv6hdr *ip6, void *data_end,
180
196
181
197
key -> src_port = bpf_ntohs (udp -> source );
182
198
key -> dst_port = bpf_ntohs (udp -> dest );
199
+
200
+ break ;
183
201
}
202
+ case IPPROTO_ICMPV6 : {
203
+ struct icmp6hdr * icmp = (void * )ip6 + sizeof (* ip6 );
184
204
185
- break ;
205
+ // validate ICMPv6 size
206
+ if ((void * )icmp + sizeof (* icmp ) > data_end ) {
207
+ return NOK ;
208
+ }
209
+
210
+ // store ICMP type in src port
211
+ key -> src_port = icmp -> icmp6_type ;
212
+ // store ICMP code in dst port
213
+ key -> dst_port = icmp -> icmp6_code ;
214
+
215
+ break ;
216
+ }
186
217
}
187
218
188
219
return OK ;
189
220
}
190
221
191
222
/**
192
- * Process the Ethernet header and extract relevant information to populate the
193
- * key.
223
+ * Process the Ethernet header and extract relevant information to populate
224
+ * the key.
194
225
*
195
226
* @param data pointer to the start of the Ethernet header
196
227
* @param data_end pointer to the end of the packet data
@@ -217,19 +248,21 @@ static inline void process_eth(void *data, void *data_end, __u64 pkt_len) {
217
248
case ETH_P_IP : {
218
249
struct iphdr * ip4 = (void * )eth + sizeof (* eth );
219
250
220
- if (process_ip4 (ip4 , data_end , & key ) == NOK )
251
+ if (process_ip4 (ip4 , data_end , & key ) == NOK ) {
221
252
return ;
222
- }
253
+ }
223
254
224
- break ;
255
+ break ;
256
+ }
225
257
case ETH_P_IPV6 : {
226
258
struct ipv6hdr * ip6 = (void * )eth + sizeof (* eth );
227
259
228
- if (process_ip6 (ip6 , data_end , & key ) == NOK )
260
+ if (process_ip6 (ip6 , data_end , & key ) == NOK ) {
229
261
return ;
230
- }
262
+ }
231
263
232
- break ;
264
+ break ;
265
+ }
233
266
default :
234
267
return ;
235
268
}
@@ -319,17 +352,17 @@ int tc_count_packets(struct __sk_buff *skb) {
319
352
}
320
353
321
354
/**
322
- * Process TCP socket information and populate the key structure with extracted
323
- * data.
355
+ * Process TCP socket information and populate the key structure with
356
+ * extracted data.
324
357
*
325
358
* @param sk pointer to the socket structure
326
359
* @param key pointer to the statkey structure to be populated
327
360
* @param pid process ID associated with the socket
328
361
*
329
362
* This function reads the socket's address family and based on whether it is
330
- * IPv4 or IPv6, it extracts the source and destination IP addresses and ports.
331
- * It also sets the protocol to TCP and assigns the provided process ID to the
332
- * key.
363
+ * IPv4 or IPv6, it extracts the source and destination IP addresses and
364
+ * ports. It also sets the protocol to TCP and assigns the provided process ID
365
+ * to the key.
333
366
*
334
367
* The function handles both IPv4 and IPv6 addresses by converting them to an
335
368
* IPv6-mapped format for uniformity.
@@ -360,7 +393,6 @@ static inline void process_tcp(struct sock *sk, statkey *key, pid_t pid) {
360
393
break ;
361
394
}
362
395
default : {
363
-
364
396
return ;
365
397
}
366
398
}
@@ -378,7 +410,8 @@ static inline void process_tcp(struct sock *sk, statkey *key, pid_t pid) {
378
410
}
379
411
380
412
/**
381
- * Process UDP socket information from a sk_buff and populate the key structure.
413
+ * Process UDP socket information from a sk_buff and populate the key
414
+ * structure.
382
415
*
383
416
* @param skb pointer to the socket buffer containing the UDP packet
384
417
* @param key pointer to the statkey structure to be populated
@@ -425,6 +458,8 @@ static inline void process_udp_recv(struct sk_buff *skb, statkey *key,
425
458
426
459
break ;
427
460
}
461
+ default :
462
+ return ;
428
463
}
429
464
430
465
key -> src_port = bpf_ntohs (BPF_CORE_READ (udphdr , source ));
@@ -468,9 +503,9 @@ static inline size_t process_icmp4(struct sk_buff *skb, statkey *key,
468
503
469
504
static inline size_t process_icmp6 (struct sk_buff * skb , statkey * key ,
470
505
pid_t pid ) {
471
- struct icmphdr * icmphdr =
472
- (struct icmphdr * )(BPF_CORE_READ (skb , head ) +
473
- BPF_CORE_READ (skb , transport_header ));
506
+ struct icmp6hdr * icmphdr =
507
+ (struct icmp6hdr * )(BPF_CORE_READ (skb , head ) +
508
+ BPF_CORE_READ (skb , transport_header ));
474
509
475
510
struct ipv6hdr * iphdr =
476
511
(struct ipv6hdr * )(BPF_CORE_READ (skb , head ) +
@@ -480,11 +515,11 @@ static inline size_t process_icmp6(struct sk_buff *skb, statkey *key,
480
515
BPF_CORE_READ_INTO (& key -> dstip , iphdr , daddr );
481
516
482
517
// store ICMP type in src port
483
- key -> src_port = BPF_CORE_READ (icmphdr , type );
518
+ key -> src_port = BPF_CORE_READ (icmphdr , icmp6_type );
484
519
// store ICMP code in dst port
485
- key -> dst_port = BPF_CORE_READ (icmphdr , code );
520
+ key -> dst_port = BPF_CORE_READ (icmphdr , icmp6_code );
486
521
487
- key -> proto = IPPROTO_ICMP ;
522
+ key -> proto = IPPROTO_ICMPV6 ;
488
523
key -> pid = pid ;
489
524
490
525
size_t msglen = bpf_ntohs (BPF_CORE_READ (iphdr , payload_len ));
@@ -493,7 +528,8 @@ static inline size_t process_icmp6(struct sk_buff *skb, statkey *key,
493
528
}
494
529
495
530
/**
496
- * Process UDP socket information from a sk_buff and populate the key structure.
531
+ * Process UDP socket information from a sk_buff and populate the key
532
+ * structure.
497
533
*
498
534
* @param skb pointer to the socket buffer containing the UDP packet
499
535
* @param key pointer to the statkey structure to be populated
@@ -525,7 +561,8 @@ static inline size_t process_udp_send(struct sk_buff *skb, statkey *key,
525
561
* packet and the given size in bytes. If the key is already present, the
526
562
* packet and byte counters are atomically incremented.
527
563
*
528
- * @param key pointer to the statkey structure containing the key to be updated
564
+ * @param key pointer to the statkey structure containing the key to be
565
+ * updated
529
566
* @param size size of the packet to be counted
530
567
*
531
568
* @throws none
@@ -547,8 +584,8 @@ static inline void update_val(statkey *key, size_t size) {
547
584
/**
548
585
* Hook function for kprobe on tcp_sendmsg function.
549
586
*
550
- * Populates the statkey structure with information from the UDP packet and the
551
- * process ID associated with the packet, and updates the packet and byte
587
+ * Populates the statkey structure with information from the UDP packet and
588
+ * the process ID associated with the packet, and updates the packet and byte
552
589
* counters in the packet count map.
553
590
*
554
591
* @param sk pointer to the socket structure
@@ -647,8 +684,8 @@ int BPF_KPROBE(ip_send_skb, struct net *net, struct sk_buff *skb) {
647
684
/**
648
685
* Hook function for kprobe on skb_consume_udp function.
649
686
*
650
- * Populates the statkey structure with information from the UDP packet and the
651
- * process ID associated with the packet, and updates the packet and byte
687
+ * Populates the statkey structure with information from the UDP packet and
688
+ * the process ID associated with the packet, and updates the packet and byte
652
689
* counters in the packet count map.
653
690
*
654
691
* @param sk pointer to the socket structure
@@ -678,8 +715,8 @@ int BPF_KPROBE(skb_consume_udp, struct sock *sk, struct sk_buff *skb, int len) {
678
715
/**
679
716
* Hook function for kprobe on icmp_send function.
680
717
*
681
- * Populates the statkey structure with information from the ICMP packet and the
682
- * process ID associated with the packet, and updates the packet and byte
718
+ * Populates the statkey structure with information from the ICMP packet and
719
+ * the process ID associated with the packet, and updates the packet and byte
683
720
* counters in the packet count map.
684
721
*
685
722
* @param skb pointer to the socket buffer containing the ICMP packet
@@ -738,8 +775,8 @@ int BPF_KPROBE(icmp6_send, struct sk_buff *skb, int type) {
738
775
/**
739
776
* Hook function for kprobe on icmp_rcv function.
740
777
*
741
- * Populates the statkey structure with information from the ICMP packet and the
742
- * process ID associated with the packet, and updates the packet and byte
778
+ * Populates the statkey structure with information from the ICMP packet and
779
+ * the process ID associated with the packet, and updates the packet and byte
743
780
* counters in the packet count map.
744
781
*
745
782
* @param skb pointer to the socket buffer containing the ICMP packet
0 commit comments