blob: 7cdf65fa06f6fa738c50d97b0207834f00b2d56b [file] [log] [blame]
Xiaoping Fan45f4f2c2015-05-22 16:13:00 -07001/*
2 * sfe_backport.h
3 * Shortcut forwarding engine compatible header file.
4 *
Xiaoping Fane70da412016-02-26 16:47:57 -08005 * Copyright (c) 2014-2016 The Linux Foundation. All rights reserved.
Xiaoping Fana42c68b2015-08-07 18:00:39 -07006 * Permission to use, copy, modify, and/or distribute this software for
7 * any purpose with or without fee is hereby granted, provided that the
8 * above copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Xiaoping Fan45f4f2c2015-05-22 16:13:00 -070016 */
17
Xiaoping Fan3f1fe512014-11-05 12:14:57 -080018#include <linux/version.h>
19
Xiaoping Fane70da412016-02-26 16:47:57 -080020#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
21#define sfe_cm_ipv4_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \
22static unsigned int __sfe_cm_ipv4_post_routing_hook(void *priv, \
23 struct sk_buff *SKB, \
24 const struct nf_hook_state *state)
25
26#define sfe_cm_ipv6_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \
27static unsigned int __sfe_cm_ipv6_post_routing_hook(void *priv, \
28 struct sk_buff *SKB, \
29 const struct nf_hook_state *state)
30#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0))
Xiaoping Fan3f1fe512014-11-05 12:14:57 -080031#define sfe_cm_ipv4_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \
32static unsigned int __sfe_cm_ipv4_post_routing_hook(const struct nf_hook_ops *OPS, \
33 struct sk_buff *SKB, \
34 const struct net_device *UNUSED, \
35 const struct net_device *OUT, \
36 int (*OKFN)(struct sk_buff *))
Xiaoping Fan978b3772015-05-27 14:15:18 -070037
38#define sfe_cm_ipv6_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \
39static unsigned int __sfe_cm_ipv6_post_routing_hook(const struct nf_hook_ops *OPS, \
40 struct sk_buff *SKB, \
41 const struct net_device *UNUSED, \
42 const struct net_device *OUT, \
43 int (*OKFN)(struct sk_buff *))
Xiaoping Fan3f1fe512014-11-05 12:14:57 -080044#else
45#define sfe_cm_ipv4_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \
46static unsigned int __sfe_cm_ipv4_post_routing_hook(unsigned int HOOKNUM, \
47 struct sk_buff *SKB, \
48 const struct net_device *UNUSED, \
49 const struct net_device *OUT, \
50 int (*OKFN)(struct sk_buff *))
Xiaoping Fan978b3772015-05-27 14:15:18 -070051
52#define sfe_cm_ipv6_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \
53static unsigned int __sfe_cm_ipv6_post_routing_hook(unsigned int HOOKNUM, \
54 struct sk_buff *SKB, \
55 const struct net_device *UNUSED, \
56 const struct net_device *OUT, \
57 int (*OKFN)(struct sk_buff *))
Xiaoping Fan3f1fe512014-11-05 12:14:57 -080058#endif
59
Xiaoping Fane70da412016-02-26 16:47:57 -080060#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
61#define SFE_IPV4_NF_POST_ROUTING_HOOK(fn) \
62 { \
63 .hook = fn, \
64 .pf = NFPROTO_IPV4, \
65 .hooknum = NF_INET_POST_ROUTING, \
66 .priority = NF_IP_PRI_NAT_SRC + 1, \
67 }
68#else
69#define SFE_IPV4_NF_POST_ROUTING_HOOK(fn) \
70 { \
71 .hook = fn, \
72 .owner = THIS_MODULE, \
73 .pf = NFPROTO_IPV4, \
74 .hooknum = NF_INET_POST_ROUTING, \
75 .priority = NF_IP_PRI_NAT_SRC + 1, \
76 }
77#endif
78
79#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
80#define SFE_IPV6_NF_POST_ROUTING_HOOK(fn) \
81 { \
82 .hook = fn, \
83 .pf = NFPROTO_IPV6, \
84 .hooknum = NF_INET_POST_ROUTING, \
85 .priority = NF_IP_PRI_NAT_SRC + 1, \
86 }
87#else
88#define SFE_IPV6_NF_POST_ROUTING_HOOK(fn) \
89 { \
90 .hook = fn, \
91 .owner = THIS_MODULE, \
92 .pf = NFPROTO_IPV6, \
93 .hooknum = NF_INET_POST_ROUTING, \
94 .priority = NF_IP6_PRI_NAT_SRC + 1, \
95 }
96#endif
97
98#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0))
99#define SFE_NF_CT_DEFAULT_ZONE (&nf_ct_zone_dflt)
100#else
101#define SFE_NF_CT_DEFAULT_ZONE NF_CT_DEFAULT_ZONE
102#endif
103
Xiaoping Fan79ed7292015-03-27 16:47:19 -0700104/*
105 * sfe_dev_get_master
106 * get master of bridge port, and hold it
107 */
108static inline struct net_device *sfe_dev_get_master(struct net_device *dev)
109{
110 struct net_device *master;
Xiaoping Fane70da412016-02-26 16:47:57 -0800111#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
Xiaoping Fan79ed7292015-03-27 16:47:19 -0700112 rcu_read_lock();
113 master = netdev_master_upper_dev_get_rcu(dev);
114 if (master)
115 dev_hold(master);
116
117 rcu_read_unlock();
Xiaoping Fan3f1fe512014-11-05 12:14:57 -0800118#else
Xiaoping Fan79ed7292015-03-27 16:47:19 -0700119 master = dev->master;
120 if (master)
121 dev_hold(master);
Xiaoping Fan3f1fe512014-11-05 12:14:57 -0800122#endif
Xiaoping Fan79ed7292015-03-27 16:47:19 -0700123 return master;
124}
Xiaoping Fan3f1fe512014-11-05 12:14:57 -0800125
Xiaoping Fane70da412016-02-26 16:47:57 -0800126#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0))
Xiaoping Fan20f04cc2015-03-30 10:14:20 -0700127#define SFE_DEV_EVENT_PTR(PTR) netdev_notifier_info_to_dev(PTR)
128#else
129#define SFE_DEV_EVENT_PTR(PTR) (struct net_device *)(PTR)
130#endif
131
132/*
133 * declare function sfe_cm_device_event
134 */
135int sfe_cm_device_event(struct notifier_block *this, unsigned long event, void *ptr);
136
137/*
138 * sfe_cm_propagate_event
139 * propagate ip address event as network device event
140 */
141static inline int sfe_cm_propagate_event(struct notifier_block *this, unsigned long event, struct net_device *dev)
142{
Xiaoping Fane70da412016-02-26 16:47:57 -0800143#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0))
Xiaoping Fan20f04cc2015-03-30 10:14:20 -0700144 struct netdev_notifier_info info;
145
146 netdev_notifier_info_init(&info, dev);
147 return sfe_cm_device_event(this, event, &info);
148#else
149 return sfe_cm_device_event(this, event, dev);
150#endif
151}
152
Xiaoping Fane70da412016-02-26 16:47:57 -0800153#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0))
Xiaoping Fan3f1fe512014-11-05 12:14:57 -0800154#define SFE_NF_CONN_ACCT(NM) struct nf_conn_acct *NM
155#else
156#define SFE_NF_CONN_ACCT(NM) struct nf_conn_counter *NM
157#endif
158
Xiaoping Fane70da412016-02-26 16:47:57 -0800159#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0))
Xiaoping Fan59176422015-05-22 15:58:10 -0700160#define SFE_ACCT_COUNTER(NM) ((NM)->counter)
Xiaoping Fan3f1fe512014-11-05 12:14:57 -0800161#else
162#define SFE_ACCT_COUNTER(NM) (NM)
163#endif