|
| 1 | +// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) |
| 2 | + |
| 3 | +#ifndef BPF_LOCAL_HELPERS_H_ |
| 4 | +#define BPF_LOCAL_HELPERS_H_ |
| 5 | + |
| 6 | +#include "bpf_shared_data.h" |
| 7 | + |
| 8 | +#define EEXIST 17 /* File exists */ |
| 9 | + |
| 10 | +#define BPF_MAP_TYPE_PIFO_GENERIC 31 |
| 11 | +#define BPF_MAP_TYPE_PIFO_XDP 32 |
| 12 | + |
| 13 | +/* |
| 14 | + * bpf_packet_dequeue |
| 15 | + * |
| 16 | + * Dequeue the packet at the head of the PIFO in *map* and return a pointer |
| 17 | + * to the packet (or NULL if the PIFO is empty). |
| 18 | + * |
| 19 | + * Returns |
| 20 | + * On success, a pointer to the packet, or NULL if the PIFO is empty. The |
| 21 | + * packet pointer must be freed using *bpf_packet_drop()* or returning |
| 22 | + * the packet pointer. The *rank* pointer will be set to the rank of |
| 23 | + * the dequeued packet on success, or a negative error code on error. |
| 24 | + */ |
| 25 | +static long (*bpf_packet_dequeue)(void *ctx, void *map, __u64 flags, __u64 *rank) = (void *) 194; |
| 26 | +static long (*bpf_packet_drop)(void *ctx, void *pkt) = (void *) 195; |
| 27 | + |
| 28 | +struct parsing_context { |
| 29 | + void *data; // Start of eth hdr |
| 30 | + void *data_end; // End of safe acessible area |
| 31 | + struct hdr_cursor nh; // Position to parse next |
| 32 | + __u32 pkt_len; // Full packet length (headers+data) |
| 33 | +}; |
| 34 | + |
| 35 | +#pragma GCC diagnostic push |
| 36 | +#pragma GCC diagnostic ignored "-Wunused-function" |
| 37 | +static __always_inline void * |
| 38 | +bpf_map_lookup_or_try_init(void *map, const void *key, const void *init) |
| 39 | +{ |
| 40 | + void *val; |
| 41 | + long err; |
| 42 | + |
| 43 | + val = bpf_map_lookup_elem(map, key); |
| 44 | + if (val) |
| 45 | + return val; |
| 46 | + |
| 47 | + err = bpf_map_update_elem(map, key, init, BPF_NOEXIST); |
| 48 | + if (err && err != -EEXIST) |
| 49 | + return NULL; |
| 50 | + |
| 51 | + return bpf_map_lookup_elem(map, key); |
| 52 | +} |
| 53 | + |
| 54 | +static __always_inline int bpf_max(__u64 left, __u64 right) |
| 55 | +{ |
| 56 | + return right > left ? right : left; |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +/* |
| 61 | + * Maps an IPv4 address into an IPv6 address according to RFC 4291 sec 2.5.5.2 |
| 62 | + */ |
| 63 | +static void map_ipv4_to_ipv6(struct in6_addr *ipv6, __be32 ipv4) |
| 64 | +{ |
| 65 | + __builtin_memset(&ipv6->in6_u.u6_addr8[0], 0x00, 10); |
| 66 | + __builtin_memset(&ipv6->in6_u.u6_addr8[10], 0xff, 2); |
| 67 | + ipv6->in6_u.u6_addr32[3] = ipv4; |
| 68 | +} |
| 69 | + |
| 70 | +/* |
| 71 | + * Five-tuple helpers |
| 72 | + */ |
| 73 | + |
| 74 | +/* This function currently only supports UDP packets */ |
| 75 | +static __always_inline int parse_packet(struct parsing_context *pctx, struct packet_info *p_info) |
| 76 | +{ |
| 77 | + /* Parse Ethernet and IP/IPv6 headers */ |
| 78 | + p_info->eth_type = parse_ethhdr(&pctx->nh, pctx->data_end, &p_info->eth); |
| 79 | + if (p_info->eth_type == bpf_htons(ETH_P_IP)) { |
| 80 | + p_info->ip_type = parse_iphdr(&pctx->nh, pctx->data_end, &p_info->iph); |
| 81 | + if (p_info->ip_type < 0) |
| 82 | + goto err; |
| 83 | + p_info->nt.ipv = 4; |
| 84 | + map_ipv4_to_ipv6(&p_info->nt.saddr.ip, p_info->iph->saddr); |
| 85 | + map_ipv4_to_ipv6(&p_info->nt.daddr.ip, p_info->iph->daddr); |
| 86 | + } else if (p_info->eth_type == bpf_htons(ETH_P_IPV6)) { |
| 87 | + p_info->ip_type = parse_ip6hdr(&pctx->nh, pctx->data_end, &p_info->ip6h); |
| 88 | + if (p_info->ip_type < 0) |
| 89 | + goto err; |
| 90 | + p_info->nt.ipv = 6; |
| 91 | + p_info->nt.saddr.ip = p_info->ip6h->saddr; |
| 92 | + p_info->nt.daddr.ip = p_info->ip6h->daddr; |
| 93 | + } else { |
| 94 | + goto err; |
| 95 | + } |
| 96 | + |
| 97 | + /* Parse UDP header */ |
| 98 | + if (p_info->ip_type != IPPROTO_UDP) |
| 99 | + goto err; |
| 100 | + if (parse_udphdr(&pctx->nh, pctx->data_end, &p_info->udph) < 0) |
| 101 | + goto err; |
| 102 | + |
| 103 | + p_info->nt.proto = IPPROTO_UDP; |
| 104 | + p_info->nt.saddr.port = p_info->udph->source; |
| 105 | + p_info->nt.daddr.port = p_info->udph->dest; |
| 106 | + |
| 107 | + return 0; |
| 108 | +err: |
| 109 | + bpf_printk("Failed to parse UDP packet"); |
| 110 | + return -1; |
| 111 | +} |
| 112 | + |
| 113 | +#pragma GCC diagnostic pop |
| 114 | + |
| 115 | +#endif // BPF_LOCAL_HELPERS_H_ |
0 commit comments