Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /* Copyright (c) 2016 Facebook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * modify it under the terms of version 2 of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * License as published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #define KBUILD_MODNAME "foo"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/if_vlan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <uapi/linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <bpf/bpf_helpers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define DEFAULT_PKTGEN_UDP_PORT 9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define DEBUG 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static int tcp(void *data, uint64_t tp_off, void *data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct tcphdr *tcp = data + tp_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	if (tcp + 1 > data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	if (tcp->dest == htons(80) || tcp->source == htons(80))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		return TC_ACT_SHOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static int udp(void *data, uint64_t tp_off, void *data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct udphdr *udp = data + tp_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	if (udp + 1 > data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	if (udp->dest == htons(DEFAULT_PKTGEN_UDP_PORT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	    udp->source == htons(DEFAULT_PKTGEN_UDP_PORT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		if (DEBUG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			char fmt[] = "udp port 9 indeed\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			bpf_trace_printk(fmt, sizeof(fmt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return TC_ACT_SHOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static int parse_ipv4(void *data, uint64_t nh_off, void *data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	uint64_t ihl_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	iph = data + nh_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (iph + 1 > data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (ip_is_fragment(iph))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	ihl_len = iph->ihl * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (iph->protocol == IPPROTO_IPIP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		iph = data + nh_off + ihl_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		if (iph + 1 > data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		ihl_len += iph->ihl * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (iph->protocol == IPPROTO_TCP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		return tcp(data, nh_off + ihl_len, data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	else if (iph->protocol == IPPROTO_UDP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		return udp(data, nh_off + ihl_len, data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static int parse_ipv6(void *data, uint64_t nh_off, void *data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct ipv6hdr *ip6h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	uint64_t ihl_len = sizeof(struct ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	uint64_t nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	ip6h = data + nh_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (ip6h + 1 > data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	nexthdr = ip6h->nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (nexthdr == IPPROTO_IPIP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		iph = data + nh_off + ihl_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		if (iph + 1 > data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		ihl_len += iph->ihl * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		nexthdr = iph->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	} else if (nexthdr == IPPROTO_IPV6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		ip6h = data + nh_off + ihl_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		if (ip6h + 1 > data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		ihl_len += sizeof(struct ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		nexthdr = ip6h->nexthdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (nexthdr == IPPROTO_TCP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		return tcp(data, nh_off + ihl_len, data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	else if (nexthdr == IPPROTO_UDP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		return udp(data, nh_off + ihl_len, data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) SEC("varlen")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int handle_ingress(struct __sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	void *data = (void *)(long)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct ethhdr *eth = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	void *data_end = (void *)(long)skb->data_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	uint64_t h_proto, nh_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	nh_off = sizeof(*eth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (data + nh_off > data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	h_proto = eth->h_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (h_proto == ETH_P_8021Q || h_proto == ETH_P_8021AD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		struct vlan_hdr *vhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		vhdr = data + nh_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		nh_off += sizeof(struct vlan_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		if (data + nh_off > data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		h_proto = vhdr->h_vlan_encapsulated_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (h_proto == ETH_P_8021Q || h_proto == ETH_P_8021AD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		struct vlan_hdr *vhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		vhdr = data + nh_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		nh_off += sizeof(struct vlan_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		if (data + nh_off > data_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		h_proto = vhdr->h_vlan_encapsulated_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (h_proto == htons(ETH_P_IP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return parse_ipv4(data, nh_off, data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	else if (h_proto == htons(ETH_P_IPV6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		return parse_ipv6(data, nh_off, data_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) char _license[] SEC("license") = "GPL";