recvmsg4_prog.c (758B)
1// SPDX-License-Identifier: GPL-2.0 2 3#include <linux/stddef.h> 4#include <linux/bpf.h> 5#include <linux/in.h> 6#include <sys/socket.h> 7 8#include <bpf/bpf_helpers.h> 9#include <bpf/bpf_endian.h> 10 11#include <bpf_sockopt_helpers.h> 12 13#define SERV4_IP 0xc0a801feU /* 192.168.1.254 */ 14#define SERV4_PORT 4040 15 16SEC("cgroup/recvmsg4") 17int recvmsg4_prog(struct bpf_sock_addr *ctx) 18{ 19 struct bpf_sock *sk; 20 __u32 user_ip4; 21 __u16 user_port; 22 23 sk = ctx->sk; 24 if (!sk) 25 return 1; 26 27 if (sk->family != AF_INET) 28 return 1; 29 30 if (ctx->type != SOCK_STREAM && ctx->type != SOCK_DGRAM) 31 return 1; 32 33 if (!get_set_sk_priority(ctx)) 34 return 1; 35 36 ctx->user_ip4 = bpf_htonl(SERV4_IP); 37 ctx->user_port = bpf_htons(SERV4_PORT); 38 39 return 1; 40} 41 42char _license[] SEC("license") = "GPL";