Blob Blame History Raw
#ifndef __IP_SET_COMPAT_H
#define __IP_SET_COMPAT_H

/* Not everything could be moved here. Compatibility stuffs can be found in
 * xt_set.c, ip_set_core.c, ip_set_getport.c, pfxlen.c too.
 */

#@HAVE_STRUCT_XT_ACTION_PARAM@ HAVE_STRUCT_XT_ACTION_PARAM
#@HAVE_VZALLOC@ HAVE_VZALLOC
#@HAVE_ETHER_ADDR_EQUAL@ HAVE_ETHER_ADDR_EQUAL
#@HAVE_IS_ZERO_ETHER_ADDR@ HAVE_IS_ZERO_ETHER_ADDR
#@HAVE_NLA_PUT_BE16@ HAVE_NLA_PUT_BE16
#@HAVE_NLA_PUT_BE64@ HAVE_NLA_PUT_BE64
#@HAVE_NLA_PUT_64BIT@ HAVE_NLA_PUT_64BIT
#@HAVE_NL_INFO_PORTID@ HAVE_NL_INFO_PORTID
#define HAVE_NETLINK_DUMP_START_ARGS	@HAVE_NETLINK_DUMP_START_ARGS@
#@HAVE_NS_CAPABLE@ HAVE_NS_CAPABLE
#@HAVE_NFNL_LOCK_SUBSYS@ HAVE_NFNL_LOCK_SUBSYS
#@HAVE_EXPORT_H@ HAVE_EXPORT_H
#define HAVE_IPV6_SKIP_EXTHDR_ARGS	@HAVE_IPV6_SKIP_EXTHDR_ARGS@
#@HAVE_CHECKENTRY_BOOL@ HAVE_CHECKENTRY_BOOL
#@HAVE_XT_TARGET_PARAM@ HAVE_XT_TARGET_PARAM
#@HAVE_NET_OPS_ID@ HAVE_NET_OPS_ID
#@HAVE_NET_OPS_ASYNC@ HAVE_NET_OPS_ASYNC
#@HAVE_USER_NS_IN_STRUCT_NET@ HAVE_USER_NS_IN_STRUCT_NET
#@HAVE_RBTREE_POSTORDER_FOR_EACH_ENTRY_SAFE@ HAVE_RBTREE_POSTORDER_FOR_EACH_ENTRY_SAFE
#@HAVE_KVCALLOC@ HAVE_KVCALLOC
#@HAVE_KVFREE@ HAVE_KVFREE
#@HAVE_XT_MTCHK_PARAM_STRUCT_NET@ HAVE_XT_MTCHK_PARAM_STRUCT_NET
#@HAVE_TCF_EMATCH_OPS_CHANGE_ARG_NET@ HAVE_TCF_EMATCH_OPS_CHANGE_ARG_NET
#@HAVE_TCF_EMATCH_STRUCT_NET@ HAVE_TCF_EMATCH_STRUCT_NET
#@HAVE_LIST_LAST_ENTRY@ HAVE_LIST_LAST_ENTRY
#@HAVE_LIST_NEXT_ENTRY@ HAVE_LIST_NEXT_ENTRY
#@HAVE_ETHER_ADDR_COPY@ HAVE_ETHER_ADDR_COPY
#@HAVE_NF_BRIDGE_GET_PHYSDEV@ HAVE_NF_BRIDGE_GET_PHYSDEV
#@HAVE_NLA_PUT_IN_ADDR@ HAVE_NLA_PUT_IN_ADDR
#@HAVE_NET_IN_NFNL_CALLBACK_FN@ HAVE_NET_IN_NFNL_CALLBACK_FN
#@HAVE_EXPORT_SYMBOL_GPL_IN_MODULE_H@ HAVE_EXPORT_SYMBOL_GPL_IN_MODULE_H
#@HAVE_TC_SKB_PROTOCOL@ HAVE_TC_SKB_PROTOCOL
#@HAVE_NET_IN_XT_ACTION_PARAM@ HAVE_NET_IN_XT_ACTION_PARAM
#@HAVE_STATE_IN_XT_ACTION_PARAM@ HAVE_STATE_IN_XT_ACTION_PARAM
#@HAVE_XT_FAMILY@ HAVE_XT_FAMILY
#@HAVE_XT_NET@ HAVE_XT_NET
#@HAVE_NFNL_MSG_TYPE@ HAVE_NFNL_MSG_TYPE
#@HAVE_NETLINK_EXTENDED_ACK@ HAVE_NETLINK_EXTENDED_ACK
#@HAVE_PASSING_EXTENDED_ACK_TO_PARSERS@ HAVE_PASSING_EXTENDED_ACK_TO_PARSERS
#@HAVE_PASSING_EXTENDED_ACK_TO_CALLBACKS@ HAVE_PASSING_EXTENDED_ACK_TO_CALLBACKS
#@HAVE_TYPEDEF_SCTP_SCTPHDR_T@ HAVE_TYPEDEF_SCTP_SCTPHDR_T
#@HAVE_TIMER_SETUP@ HAVE_TIMER_SETUP
#@HAVE_STRSCPY@ HAVE_STRSCPY
#@HAVE_LOCKDEP_NFNL_IS_HELD@ HAVE_LOCKDEP_NFNL_IS_HELD

#ifdef HAVE_EXPORT_SYMBOL_GPL_IN_MODULE_H
#include <linux/module.h>
#endif

#ifdef HAVE_EXPORT_H
#include <linux/export.h>
#endif

#ifndef IP_SET_COMPAT_HEADERS

#ifndef CONFIG_IP_SET_MAX
#ifdef IP_SET_MAX
#define CONFIG_IP_SET_MAX	IP_SET_MAX
#else
#define CONFIG_IP_SET_MAX	256
#endif
#endif

#ifndef IS_ENABLED
#define __X_ARG_PLACEHOLDER_1 0,
#define x_config_enabled(cfg) _x_config_enabled(cfg)
#define _x_config_enabled(value) __x_config_enabled(__X_ARG_PLACEHOLDER_##value)
#define __x_config_enabled(arg1_or_junk) ___x_config_enabled(arg1_or_junk 1, 0)
#define ___x_config_enabled(__ignored, val, ...) val

#define IS_ENABLED(option) \
	(x_config_enabled(option) || x_config_enabled(option##_MODULE))
#endif

#include <linux/version.h>
#include <linux/netlink.h>

#ifndef rcu_dereference_bh
#define rcu_dereference_bh(p)		rcu_dereference(p)
#endif

#ifndef rcu_dereference_protected
#define rcu_dereference_protected(p, c)	(p)
#endif

#ifndef rcu_dereference_bh_check
#define rcu_dereference_bh_check(p, c)	rcu_dereference_bh(p)
#endif

#ifndef __rcu
#define	__rcu
#ifndef RCU_INIT_POINTER
#define RCU_INIT_POINTER(p, v) \
	do { \
		p = v; \
	} while (0)
#endif
#else

#ifndef RCU_INITIALIZER
#define RCU_INITIALIZER(v)	(typeof(*(v)) __force __rcu *)(v)
#endif
#ifndef RCU_INIT_POINTER
#define RCU_INIT_POINTER(p, v)	\
	do { \
		p = RCU_INITIALIZER(v); \
	} while (0)
#endif
#endif

#ifndef kfree_rcu

static inline void kfree_call_rcu(struct rcu_head *head,
				  void (*func)(struct rcu_head *rcu))
{
	call_rcu(head, func);
}

#define __is_kfree_rcu_offset(offset) ((offset) < 4096)

#define __kfree_rcu(head, offset)	\
do {				\
	BUILD_BUG_ON(!__is_kfree_rcu_offset(offset));	\
	kfree_call_rcu(head, (void (*)(struct rcu_head *))(unsigned long)(offset)); \
} while (0)

#define kfree_rcu(ptr, rcu_head)	\
	__kfree_rcu(&((ptr)->rcu_head), offsetof(typeof(*(ptr)), rcu_head))
#endif

#if 0
#ifdef CHECK_KCONFIG
#ifndef CONFIG_SPARSE_RCU_POINTER
#error "CONFIG_SPARSE_RCU_POINTER must be enabled"
#endif
#endif
#endif

#if defined(CONFIG_NETFILTER_NETLINK) || defined(CONFIG_NETFILTER_NETLINK_MODULE)
#else
#error "NETFILTER_NETLINK must be enabled: select NFACCT/NFQUEUE/LOG over NFNETLINK"
#endif

#ifndef HAVE_STRUCT_XT_ACTION_PARAM
#define xt_action_param		xt_match_param
#endif

#ifndef HAVE_VZALLOC
#define vzalloc(size)		__vmalloc(size,\
					  GFP_KERNEL|__GFP_ZERO|__GFP_HIGHMEM,\
					  PAGE_KERNEL)
#endif

#if !defined(HAVE_ETHER_ADDR_EQUAL) || !defined(HAVE_IS_ZERO_ETHER_ADDR)
#include <linux/etherdevice.h>

#ifndef HAVE_ETHER_ADDR_EQUAL
static inline bool ether_addr_equal(const u8 *addr1, const u8 *addr2)
{
	return !compare_ether_addr(addr1, addr2);
}
#endif

#ifndef HAVE_IS_ZERO_ETHER_ADDR
static inline bool is_zero_ether_addr(const u8 *addr)
{
	return (*(const u16 *)(addr + 0) |
		*(const u16 *)(addr + 2) |
		*(const u16 *)(addr + 4)) == 0;
}
#endif
#endif

#ifndef HAVE_NLA_PUT_BE64
static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value)
{
	return nla_put(skb, attrtype, sizeof(__be64), &value);
}

static inline int nla_put_net64(struct sk_buff *skb, int attrtype, __be64 value)
{
	return nla_put_be64(skb, attrtype | NLA_F_NET_BYTEORDER, value);
}
#endif

#ifdef HAVE_NLA_PUT_64BIT
#define IPSET_NLA_PUT_NET64(skb, t, v, pa) nla_put_net64(skb, t, v, pa)
#else
#define IPSET_NLA_PUT_NET64(skb, t, v, pa) nla_put_net64(skb, t, v)
#endif

#ifdef HAVE_NL_INFO_PORTID
#define NETLINK_PORTID(skb)	NETLINK_CB(skb).portid
#else
#define NETLINK_PORTID(skb)	NETLINK_CB(skb).pid
#endif

#ifndef HAVE_USER_NS_IN_STRUCT_NET
#define ns_capable(ns, cap)	capable(cap)
#endif

#ifndef HAVE_NFNL_LOCK_SUBSYS
#define nfnl_lock(x)		nfnl_lock()
#define nfnl_unlock(x)		nfnl_unlock()
#endif

#ifndef HAVE_LOCKDEP_NFNL_IS_HELD
#define lockdep_nfnl_is_held(x)	1
#endif

#if HAVE_IPV6_SKIP_EXTHDR_ARGS == 3
#define ipv6_skip_exthdr(skbuff, start, nexthdrp, frag_offp)	\
	ipv6_skip_exthdr(skbuff, start, nexthdrp)
#endif

#ifndef HAVE_KVCALLOC
#define kvcalloc(n, size, flags)	kcalloc(n, size, flags)
#endif

#ifndef HAVE_KVFREE
#include <linux/vmalloc.h>
static inline void kvfree(const void *addr)
{
	if (is_vmalloc_addr(addr))
		vfree(addr);
	else
		kfree(addr);
}
#endif

#ifndef HAVE_NLA_PUT_BE16
static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
{
	return nla_put(skb, attrtype, sizeof(__be16), &value);
}

static inline int nla_put_net16(struct sk_buff *skb, int attrtype, __be16 value)
{
	return nla_put_be16(skb, attrtype | NLA_F_NET_BYTEORDER, value);
}

static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
{
	return nla_put(skb, attrtype, sizeof(__be32), &value);
}

static inline int nla_put_net32(struct sk_buff *skb, int attrtype, __be32 value)
{
	return nla_put_be32(skb, attrtype | NLA_F_NET_BYTEORDER, value);
}
#endif

#ifndef HAVE_LIST_LAST_ENTRY
#define list_last_entry(ptr, type, member) \
	list_entry((ptr)->prev, type, member)
#endif
#ifndef HAVE_LIST_NEXT_ENTRY
#define list_next_entry(pos, member)	\
	list_entry((pos)->member.next, typeof(*(pos)), member)
#define list_prev_entry(pos, member)	\
	list_entry((pos)->member.prev, typeof(*(pos)), member)
#endif

#ifndef HAVE_ETHER_ADDR_COPY
#define ether_addr_copy(dst, src)	memcpy(dst, src, ETH_ALEN)
#endif

#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
#ifndef HAVE_NF_BRIDGE_GET_PHYSDEV
static inline struct net_device *
nf_bridge_get_physindev(const struct sk_buff *skb)
{
	return skb->nf_bridge ? skb->nf_bridge->physindev : NULL;
}

static inline struct net_device *
nf_bridge_get_physoutdev(const struct sk_buff *skb)
{
	return skb->nf_bridge ? skb->nf_bridge->physoutdev : NULL;
}
#endif
#endif

#ifndef HAVE_NLA_PUT_IN_ADDR
static inline int nla_put_in_addr(struct sk_buff *skb, int attrtype,
				  __be32 addr)
{
	return nla_put_be32(skb, attrtype, addr);
}

static inline int nla_put_in6_addr(struct sk_buff *skb, int attrtype,
				   const struct in6_addr *addr)
{
	return nla_put(skb, attrtype, sizeof(*addr), addr);
}
#endif

#ifdef HAVE_PASSING_EXTENDED_ACK_TO_CALLBACKS
#define IPSET_CBFN(fn, net, nl, skb, nlh, cda, e)	fn(net, nl, skb, nlh, cda, e)
#define IPSET_SOCK_NET(net, ctnl)			net
#elif defined(HAVE_NET_IN_NFNL_CALLBACK_FN)
#define IPSET_CBFN(fn, net, nl, skb, nlh, cda, e)	fn(net, nl, skb, nlh, cda)
#define IPSET_SOCK_NET(net, ctnl)			net
#else
#define IPSET_CBFN(fn, net, nl, skb, nlh, cda,e)	fn(nl, skb, nlh, cda)
#define IPSET_SOCK_NET(net, ctnl)			sock_net(ctnl)
#endif

#ifndef HAVE_TC_SKB_PROTOCOL
#include <linux/if_vlan.h>
static inline __be16 tc_skb_protocol(const struct sk_buff *skb)
{
	if (vlan_tx_tag_present(skb))
		return skb->vlan_proto;
	return skb->protocol;
}
#endif

#ifdef HAVE_XT_NET
#define IPSET_DEV_NET(par)	xt_net(par)
#elif defined(HAVE_NET_IN_XT_ACTION_PARAM)
#define IPSET_DEV_NET(par)	(par)->net
#else
#define IPSET_DEV_NET(par)	dev_net((par)->in ? (par)->in : (par)->out)
#endif

#ifndef HAVE_NFNL_MSG_TYPE
static inline u16 nfnl_msg_type(u8 subsys, u8 msg_type)
{
	return subsys << 8 | msg_type;
}
#endif

#ifdef HAVE_NETLINK_EXTENDED_ACK
#define NETLINK_ACK(in_skb, nlh, err, extack)	netlink_ack(in_skb, nlh, err, extack)
#else
#define NETLINK_ACK(in_skb, nlh, err, extack)	netlink_ack(in_skb, nlh, err)
#endif

#ifdef HAVE_PASSING_EXTENDED_ACK_TO_PARSERS
#define NLA_PARSE(t, m, h, l, p, e)		nla_parse(t, m, h, l, p, e)
#define NLA_PARSE_NESTED(t, m, n, p, e)		nla_parse_nested(t, m, n, p, e)
#else
#define NLA_PARSE(t, m, h, l, p, e)		nla_parse(t, m, h, l, p)
#define NLA_PARSE_NESTED(t, m, n, p, e)		nla_parse_nested(t, m, n, p)
#endif

#ifdef HAVE_STATE_IN_XT_ACTION_PARAM
#define XAP_STATE(par)		(par->state)
#else
#define XAP_STATE(par)		par
#endif

#ifdef HAVE_XT_FAMILY
#define XT_FAMILY(par)		xt_family(par)
#else
#define XT_FAMILY(par)		(par)->family
#endif

#ifdef HAVE_TIMER_SETUP
#define GC_ARG			struct timer_list *t
#define TIMER_SETUP(a, b)	timer_setup(a, b, 0)
#define INIT_GC_VARS(type, var)				\
	struct type *var = from_timer(var, t, gc); 	\
	struct ip_set *set = var->set
#else
#define GC_ARG			unsigned long ul_set
#define TIMER_SETUP(a, b)	setup_timer(a, b, (unsigned long)set)
#define INIT_GC_VARS(type, var)				\
	struct ip_set *set = (struct ip_set *)ul_set;	\
	struct type *var = set->data
#endif

#ifndef HAVE_STRSCPY
#define	strscpy(dst, src, n)	(strncpy(dst, src, n) == (dst))
#endif

#ifndef smp_mb__before_atomic
#define smp_mb__before_atomic()	smp_mb()
#define smp_mb__after_atomic()	smp_mb()
#endif

#ifndef pr_warn
#define pr_warn	pr_warning
#endif

#ifndef SIZE_MAX
#define SIZE_MAX	(~(size_t)0)
#endif

#endif /* IP_SET_COMPAT_HEADERS */
#endif /* __IP_SET_COMPAT_H */