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) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/filter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <net/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <net/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/tc_act/tc_bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <net/tc_act/tc_bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define ACT_BPF_NAME_LEN	256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) struct tcf_bpf_cfg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct bpf_prog *filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct sock_filter *bpf_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	const char *bpf_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	u16 bpf_num_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	bool is_ebpf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static unsigned int bpf_net_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static struct tc_action_ops act_bpf_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static int tcf_bpf_act(struct sk_buff *skb, const struct tc_action *act,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		       struct tcf_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	bool at_ingress = skb_at_tc_ingress(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct tcf_bpf *prog = to_bpf(act);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct bpf_prog *filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	int action, filter_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	tcf_lastuse_update(&prog->tcf_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	bstats_cpu_update(this_cpu_ptr(prog->common.cpu_bstats), skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	filter = rcu_dereference(prog->filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (at_ingress) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		__skb_push(skb, skb->mac_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		bpf_compute_data_pointers(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		filter_res = BPF_PROG_RUN(filter, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		__skb_pull(skb, skb->mac_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		bpf_compute_data_pointers(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		filter_res = BPF_PROG_RUN(filter, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (skb_sk_is_prefetched(skb) && filter_res != TC_ACT_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		skb_orphan(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	/* A BPF program may overwrite the default action opcode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	 * Similarly as in cls_bpf, if filter_res == -1 we use the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	 * default action specified from tc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	 * In case a different well-known TC_ACT opcode has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	 * returned, it will overwrite the default one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	 * For everything else that is unkown, TC_ACT_UNSPEC is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	 * returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	switch (filter_res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	case TC_ACT_PIPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	case TC_ACT_RECLASSIFY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	case TC_ACT_OK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	case TC_ACT_REDIRECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		action = filter_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	case TC_ACT_SHOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		action = filter_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		qstats_drop_inc(this_cpu_ptr(prog->common.cpu_qstats));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	case TC_ACT_UNSPEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		action = prog->tcf_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		action = TC_ACT_UNSPEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	return action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static bool tcf_bpf_is_ebpf(const struct tcf_bpf *prog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	return !prog->bpf_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) static int tcf_bpf_dump_bpf_info(const struct tcf_bpf *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 				 struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (nla_put_u16(skb, TCA_ACT_BPF_OPS_LEN, prog->bpf_num_ops))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	nla = nla_reserve(skb, TCA_ACT_BPF_OPS, prog->bpf_num_ops *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			  sizeof(struct sock_filter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (nla == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int tcf_bpf_dump_ebpf_info(const struct tcf_bpf *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 				  struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (prog->bpf_name &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	    nla_put_string(skb, TCA_ACT_BPF_NAME, prog->bpf_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (nla_put_u32(skb, TCA_ACT_BPF_ID, prog->filter->aux->id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	nla = nla_reserve(skb, TCA_ACT_BPF_TAG, sizeof(prog->filter->tag));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (nla == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	memcpy(nla_data(nla), prog->filter->tag, nla_len(nla));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *act,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			int bind, int ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	unsigned char *tp = skb_tail_pointer(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	struct tcf_bpf *prog = to_bpf(act);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	struct tc_act_bpf opt = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		.index   = prog->tcf_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		.refcnt  = refcount_read(&prog->tcf_refcnt) - ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		.bindcnt = atomic_read(&prog->tcf_bindcnt) - bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	struct tcf_t tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	spin_lock_bh(&prog->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	opt.action = prog->tcf_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (tcf_bpf_is_ebpf(prog))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		ret = tcf_bpf_dump_ebpf_info(prog, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		ret = tcf_bpf_dump_bpf_info(prog, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	tcf_tm_dump(&tm, &prog->tcf_tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (nla_put_64bit(skb, TCA_ACT_BPF_TM, sizeof(tm), &tm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			  TCA_ACT_BPF_PAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	spin_unlock_bh(&prog->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	spin_unlock_bh(&prog->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	nlmsg_trim(skb, tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	[TCA_ACT_BPF_PARMS]	= { .len = sizeof(struct tc_act_bpf) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	[TCA_ACT_BPF_FD]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	[TCA_ACT_BPF_NAME]	= { .type = NLA_NUL_STRING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 				    .len = ACT_BPF_NAME_LEN },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	[TCA_ACT_BPF_OPS_LEN]	= { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	[TCA_ACT_BPF_OPS]	= { .type = NLA_BINARY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 				    .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static int tcf_bpf_init_from_ops(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct sock_filter *bpf_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	struct sock_fprog_kern fprog_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	struct bpf_prog *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	u16 bpf_size, bpf_num_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	bpf_num_ops = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (bpf_num_ops	> BPF_MAXINSNS || bpf_num_ops == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	bpf_size = bpf_num_ops * sizeof(*bpf_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	bpf_ops = kmemdup(nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (bpf_ops == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	fprog_tmp.len = bpf_num_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	fprog_tmp.filter = bpf_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	ret = bpf_prog_create(&fp, &fprog_tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		kfree(bpf_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	cfg->bpf_ops = bpf_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	cfg->bpf_num_ops = bpf_num_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	cfg->filter = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	cfg->is_ebpf = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static int tcf_bpf_init_from_efd(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	struct bpf_prog *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	u32 bpf_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	bpf_fd = nla_get_u32(tb[TCA_ACT_BPF_FD]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	fp = bpf_prog_get_type(bpf_fd, BPF_PROG_TYPE_SCHED_ACT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (IS_ERR(fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		return PTR_ERR(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (tb[TCA_ACT_BPF_NAME]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		name = nla_memdup(tb[TCA_ACT_BPF_NAME], GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			bpf_prog_put(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	cfg->bpf_name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	cfg->filter = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	cfg->is_ebpf = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static void tcf_bpf_cfg_cleanup(const struct tcf_bpf_cfg *cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	struct bpf_prog *filter = cfg->filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (filter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		if (cfg->is_ebpf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			bpf_prog_put(filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			bpf_prog_destroy(filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	kfree(cfg->bpf_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	kfree(cfg->bpf_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static void tcf_bpf_prog_fill_cfg(const struct tcf_bpf *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 				  struct tcf_bpf_cfg *cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	cfg->is_ebpf = tcf_bpf_is_ebpf(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	/* updates to prog->filter are prevented, since it's called either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	 * with tcf lock or during final cleanup in rcu callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	cfg->filter = rcu_dereference_protected(prog->filter, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	cfg->bpf_ops = prog->bpf_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	cfg->bpf_name = prog->bpf_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static int tcf_bpf_init(struct net *net, struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			struct nlattr *est, struct tc_action **act,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			int replace, int bind, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			struct tcf_proto *tp, u32 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct tc_action_net *tn = net_generic(net, bpf_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	struct tcf_chain *goto_ch = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	struct tcf_bpf_cfg cfg, old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct tc_act_bpf *parm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	struct tcf_bpf *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	bool is_bpf, is_ebpf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	int ret, res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	u32 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (!nla)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	ret = nla_parse_nested_deprecated(tb, TCA_ACT_BPF_MAX, nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 					  act_bpf_policy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (!tb[TCA_ACT_BPF_PARMS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	parm = nla_data(tb[TCA_ACT_BPF_PARMS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	index = parm->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	ret = tcf_idr_check_alloc(tn, &index, act, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		ret = tcf_idr_create(tn, index, est, act,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 				     &act_bpf_ops, bind, true, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			tcf_idr_cleanup(tn, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		res = ACT_P_CREATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	} else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		/* Don't override defaults. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		if (bind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		if (!replace) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			tcf_idr_release(*act, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	ret = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		goto release_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	is_bpf = tb[TCA_ACT_BPF_OPS_LEN] && tb[TCA_ACT_BPF_OPS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	is_ebpf = tb[TCA_ACT_BPF_FD];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		goto put_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	memset(&cfg, 0, sizeof(cfg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	ret = is_bpf ? tcf_bpf_init_from_ops(tb, &cfg) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		       tcf_bpf_init_from_efd(tb, &cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		goto put_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	prog = to_bpf(*act);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	spin_lock_bh(&prog->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (res != ACT_P_CREATED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		tcf_bpf_prog_fill_cfg(prog, &old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	prog->bpf_ops = cfg.bpf_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	prog->bpf_name = cfg.bpf_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	if (cfg.bpf_num_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		prog->bpf_num_ops = cfg.bpf_num_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	goto_ch = tcf_action_set_ctrlact(*act, parm->action, goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	rcu_assign_pointer(prog->filter, cfg.filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	spin_unlock_bh(&prog->tcf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if (goto_ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		tcf_chain_put_by_act(goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (res != ACT_P_CREATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		/* make sure the program being replaced is no longer executing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		tcf_bpf_cfg_cleanup(&old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) put_chain:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	if (goto_ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		tcf_chain_put_by_act(goto_ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) release_idr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	tcf_idr_release(*act, bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static void tcf_bpf_cleanup(struct tc_action *act)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	struct tcf_bpf_cfg tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	tcf_bpf_prog_fill_cfg(to_bpf(act), &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	tcf_bpf_cfg_cleanup(&tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static int tcf_bpf_walker(struct net *net, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			  struct netlink_callback *cb, int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			  const struct tc_action_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			  struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	struct tc_action_net *tn = net_generic(net, bpf_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static int tcf_bpf_search(struct net *net, struct tc_action **a, u32 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	struct tc_action_net *tn = net_generic(net, bpf_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	return tcf_idr_search(tn, a, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static struct tc_action_ops act_bpf_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	.kind		=	"bpf",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	.id		=	TCA_ID_BPF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	.owner		=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	.act		=	tcf_bpf_act,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	.dump		=	tcf_bpf_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	.cleanup	=	tcf_bpf_cleanup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	.init		=	tcf_bpf_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	.walk		=	tcf_bpf_walker,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	.lookup		=	tcf_bpf_search,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	.size		=	sizeof(struct tcf_bpf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) static __net_init int bpf_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	struct tc_action_net *tn = net_generic(net, bpf_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	return tc_action_net_init(net, tn, &act_bpf_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) static void __net_exit bpf_exit_net(struct list_head *net_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	tc_action_net_exit(net_list, bpf_net_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) static struct pernet_operations bpf_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	.init = bpf_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	.exit_batch = bpf_exit_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	.id   = &bpf_net_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	.size = sizeof(struct tc_action_net),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static int __init bpf_init_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	return tcf_register_action(&act_bpf_ops, &bpf_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) static void __exit bpf_cleanup_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	tcf_unregister_action(&act_bpf_ops, &bpf_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) module_init(bpf_init_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) module_exit(bpf_cleanup_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) MODULE_DESCRIPTION("TC BPF based action");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) MODULE_LICENSE("GPL v2");