Skip to content

Commit

Permalink
samples/bpf: replace BPF programs header with net_shared.h
Browse files Browse the repository at this point in the history
This commit applies "net_shared.h" to BPF programs to remove existing
network related header dependencies. Also, this commit removes
unnecessary headers before applying "vmlinux.h" to the BPF programs.

Mostly, endianness conversion function has been applied to the source.
In addition, several macros have been defined to fulfill the INET,
TC-related constants.

Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Link: https://lore.kernel.org/r/20230115071613.125791-9-danieltimlee@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
DanielTimLee authored and Alexei Starovoitov committed Jan 15, 2023
1 parent e69fe84 commit c2f4f55
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
6 changes: 6 additions & 0 deletions samples/bpf/net_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#ifndef _NET_SHARED_H
#define _NET_SHARED_H

#define AF_INET 2
#define AF_INET6 10

#define ETH_ALEN 6
#define ETH_P_802_3_MIN 0x0600
#define ETH_P_8021Q 0x8100
Expand All @@ -11,6 +14,9 @@
#define ETH_P_ARP 0x0806
#define IPPROTO_ICMPV6 58

#define TC_ACT_OK 0
#define TC_ACT_SHOT 2

#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define bpf_ntohs(x) __builtin_bswap16(x)
Expand Down
10 changes: 5 additions & 5 deletions samples/bpf/sock_flags_kern.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "net_shared.h"
#include <uapi/linux/bpf.h>
#include <linux/socket.h>
#include <linux/net.h>
#include <uapi/linux/in.h>
#include <uapi/linux/in6.h>
Expand All @@ -17,10 +17,10 @@ int bpf_prog1(struct bpf_sock *sk)
bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
bpf_trace_printk(fmt2, sizeof(fmt2), uid, gid);

/* block PF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6 sockets
/* block AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6 sockets
* ie., make ping6 fail
*/
if (sk->family == PF_INET6 &&
if (sk->family == AF_INET6 &&
sk->type == SOCK_DGRAM &&
sk->protocol == IPPROTO_ICMPV6)
return 0;
Expand All @@ -35,10 +35,10 @@ int bpf_prog2(struct bpf_sock *sk)

bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);

/* block PF_INET, SOCK_DGRAM, IPPROTO_ICMP sockets
/* block AF_INET, SOCK_DGRAM, IPPROTO_ICMP sockets
* ie., make ping fail
*/
if (sk->family == PF_INET &&
if (sk->family == AF_INET &&
sk->type == SOCK_DGRAM &&
sk->protocol == IPPROTO_ICMP)
return 0;
Expand Down
6 changes: 2 additions & 4 deletions samples/bpf/test_cgrp2_tc_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
* License as published by the Free Software Foundation.
*/
#define KBUILD_MODNAME "foo"
#include <uapi/linux/if_ether.h>
#include <uapi/linux/in6.h>
#include "net_shared.h"
#include <uapi/linux/ipv6.h>
#include <uapi/linux/pkt_cls.h>
#include <uapi/linux/bpf.h>
#include <bpf/bpf_helpers.h>

Expand Down Expand Up @@ -42,7 +40,7 @@ int handle_egress(struct __sk_buff *skb)
if (data + sizeof(*eth) + sizeof(*ip6h) > data_end)
return TC_ACT_OK;

if (eth->h_proto != htons(ETH_P_IPV6) ||
if (eth->h_proto != bpf_htons(ETH_P_IPV6) ||
ip6h->nexthdr != IPPROTO_ICMPV6) {
bpf_trace_printk(dont_care_msg, sizeof(dont_care_msg),
eth->h_proto, ip6h->nexthdr);
Expand Down
3 changes: 2 additions & 1 deletion samples/bpf/test_lwt_bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* General Public License for more details.
*/

#include "net_shared.h"
#include <stdint.h>
#include <stddef.h>
#include <linux/bpf.h>
Expand Down Expand Up @@ -176,7 +177,7 @@ static inline int __do_push_ll_and_redirect(struct __sk_buff *skb)
printk("skb_change_head() failed: %d", ret);
}

ehdr.h_proto = __constant_htons(ETH_P_IP);
ehdr.h_proto = bpf_htons(ETH_P_IP);
memcpy(&ehdr.h_source, &smac, 6);
memcpy(&ehdr.h_dest, &dmac, 6);

Expand Down

0 comments on commit c2f4f55

Please sign in to comment.