Xiaoping Fan | 45f4f2c | 2015-05-22 16:13:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * sfe_backport.h |
| 3 | * Shortcut forwarding engine compatible header file. |
| 4 | * |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 5 | * Copyright (c) 2014-2016 The Linux Foundation. All rights reserved. |
Xiaoping Fan | a42c68b | 2015-08-07 18:00:39 -0700 | [diff] [blame] | 6 | * 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 Fan | 45f4f2c | 2015-05-22 16:13:00 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 18 | #include <linux/version.h> |
| 19 | |
Xiaoping Fan | b31b2ec | 2016-11-17 17:44:36 -0800 | [diff] [blame] | 20 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)) |
| 21 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)) |
| 22 | #include <net/netfilter/nf_conntrack_timeout.h> |
| 23 | #else |
| 24 | enum udp_conntrack { |
| 25 | UDP_CT_UNREPLIED, |
| 26 | UDP_CT_REPLIED, |
| 27 | UDP_CT_MAX |
| 28 | }; |
| 29 | |
| 30 | static inline unsigned int * |
| 31 | nf_ct_timeout_lookup(struct net *net, struct nf_conn *ct, |
| 32 | struct nf_conntrack_l4proto *l4proto) |
| 33 | { |
| 34 | #ifdef CONFIG_NF_CONNTRACK_TIMEOUT |
| 35 | struct nf_conn_timeout *timeout_ext; |
| 36 | unsigned int *timeouts; |
| 37 | |
| 38 | timeout_ext = nf_ct_timeout_find(ct); |
| 39 | if (timeout_ext) |
| 40 | timeouts = NF_CT_TIMEOUT_EXT_DATA(timeout_ext); |
| 41 | else |
| 42 | timeouts = l4proto->get_timeouts(net); |
| 43 | |
| 44 | return timeouts; |
| 45 | #else |
| 46 | return l4proto->get_timeouts(net); |
| 47 | #endif /*CONFIG_NF_CONNTRACK_TIMEOUT*/ |
| 48 | } |
| 49 | #endif /*KERNEL_VERSION(3, 7, 0)*/ |
| 50 | #endif /*KERNEL_VERSION(3, 4, 0)*/ |
| 51 | |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 52 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 53 | #define sfe_define_post_routing_hook(FN_NAME, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 54 | static unsigned int FN_NAME(void *priv, \ |
| 55 | struct sk_buff *SKB, \ |
| 56 | const struct nf_hook_state *state) |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 57 | #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 58 | #define sfe_define_post_routing_hook(FN_NAME, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 59 | static unsigned int FN_NAME(const struct nf_hook_ops *OPS, \ |
| 60 | struct sk_buff *SKB, \ |
| 61 | const struct net_device *UNUSED, \ |
| 62 | const struct net_device *OUT, \ |
| 63 | int (*OKFN)(struct sk_buff *)) |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 64 | #else |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 65 | #define sfe_define_post_routing_hook(FN_NAME, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 66 | static unsigned int FN_NAME(unsigned int HOOKNUM, \ |
| 67 | struct sk_buff *SKB, \ |
| 68 | const struct net_device *UNUSED, \ |
| 69 | const struct net_device *OUT, \ |
| 70 | int (*OKFN)(struct sk_buff *)) |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 71 | #endif |
| 72 | |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 73 | #define sfe_cm_ipv4_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 74 | sfe_define_post_routing_hook(__sfe_cm_ipv4_post_routing_hook, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) |
| 75 | #define sfe_cm_ipv6_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 76 | sfe_define_post_routing_hook(__sfe_cm_ipv6_post_routing_hook, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) |
| 77 | #define fast_classifier_ipv4_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 78 | sfe_define_post_routing_hook(__fast_classifier_ipv4_post_routing_hook, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) |
Xiaoping Fan | 9b6bb33 | 2016-04-05 19:21:26 -0700 | [diff] [blame] | 79 | #define fast_classifier_ipv6_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 80 | sfe_define_post_routing_hook(__fast_classifier_ipv6_post_routing_hook, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 81 | |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 82 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) |
| 83 | #define SFE_IPV4_NF_POST_ROUTING_HOOK(fn) \ |
| 84 | { \ |
| 85 | .hook = fn, \ |
| 86 | .pf = NFPROTO_IPV4, \ |
| 87 | .hooknum = NF_INET_POST_ROUTING, \ |
| 88 | .priority = NF_IP_PRI_NAT_SRC + 1, \ |
| 89 | } |
| 90 | #else |
| 91 | #define SFE_IPV4_NF_POST_ROUTING_HOOK(fn) \ |
| 92 | { \ |
| 93 | .hook = fn, \ |
| 94 | .owner = THIS_MODULE, \ |
| 95 | .pf = NFPROTO_IPV4, \ |
| 96 | .hooknum = NF_INET_POST_ROUTING, \ |
| 97 | .priority = NF_IP_PRI_NAT_SRC + 1, \ |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) |
| 102 | #define SFE_IPV6_NF_POST_ROUTING_HOOK(fn) \ |
| 103 | { \ |
| 104 | .hook = fn, \ |
| 105 | .pf = NFPROTO_IPV6, \ |
| 106 | .hooknum = NF_INET_POST_ROUTING, \ |
| 107 | .priority = NF_IP_PRI_NAT_SRC + 1, \ |
| 108 | } |
| 109 | #else |
| 110 | #define SFE_IPV6_NF_POST_ROUTING_HOOK(fn) \ |
| 111 | { \ |
| 112 | .hook = fn, \ |
| 113 | .owner = THIS_MODULE, \ |
| 114 | .pf = NFPROTO_IPV6, \ |
| 115 | .hooknum = NF_INET_POST_ROUTING, \ |
| 116 | .priority = NF_IP6_PRI_NAT_SRC + 1, \ |
| 117 | } |
| 118 | #endif |
| 119 | |
| 120 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)) |
| 121 | #define SFE_NF_CT_DEFAULT_ZONE (&nf_ct_zone_dflt) |
| 122 | #else |
| 123 | #define SFE_NF_CT_DEFAULT_ZONE NF_CT_DEFAULT_ZONE |
| 124 | #endif |
| 125 | |
Xiaoping Fan | 79ed729 | 2015-03-27 16:47:19 -0700 | [diff] [blame] | 126 | /* |
| 127 | * sfe_dev_get_master |
| 128 | * get master of bridge port, and hold it |
| 129 | */ |
| 130 | static inline struct net_device *sfe_dev_get_master(struct net_device *dev) |
| 131 | { |
| 132 | struct net_device *master; |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 133 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) |
Xiaoping Fan | 79ed729 | 2015-03-27 16:47:19 -0700 | [diff] [blame] | 134 | rcu_read_lock(); |
| 135 | master = netdev_master_upper_dev_get_rcu(dev); |
| 136 | if (master) |
| 137 | dev_hold(master); |
| 138 | |
| 139 | rcu_read_unlock(); |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 140 | #else |
Xiaoping Fan | 79ed729 | 2015-03-27 16:47:19 -0700 | [diff] [blame] | 141 | master = dev->master; |
| 142 | if (master) |
| 143 | dev_hold(master); |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 144 | #endif |
Xiaoping Fan | 79ed729 | 2015-03-27 16:47:19 -0700 | [diff] [blame] | 145 | return master; |
| 146 | } |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 147 | |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 148 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) |
Xiaoping Fan | 20f04cc | 2015-03-30 10:14:20 -0700 | [diff] [blame] | 149 | #define SFE_DEV_EVENT_PTR(PTR) netdev_notifier_info_to_dev(PTR) |
| 150 | #else |
| 151 | #define SFE_DEV_EVENT_PTR(PTR) (struct net_device *)(PTR) |
| 152 | #endif |
| 153 | |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 154 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 155 | #define SFE_NF_CONN_ACCT(NM) struct nf_conn_acct *NM |
| 156 | #else |
| 157 | #define SFE_NF_CONN_ACCT(NM) struct nf_conn_counter *NM |
| 158 | #endif |
| 159 | |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 160 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 161 | #define SFE_ACCT_COUNTER(NM) ((NM)->counter) |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 162 | #else |
| 163 | #define SFE_ACCT_COUNTER(NM) (NM) |
| 164 | #endif |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 165 | |
| 166 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) |
| 167 | #define sfe_hash_for_each_possible(name, obj, node, member, key) \ |
| 168 | hash_for_each_possible(name, obj, member, key) |
| 169 | #else |
| 170 | #define sfe_hash_for_each_possible(name, obj, node, member, key) \ |
| 171 | hash_for_each_possible(name, obj, node, member, key) |
| 172 | #endif |
| 173 | |
| 174 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) |
| 175 | #define sfe_hash_for_each(name, bkt, node, obj, member) \ |
| 176 | hash_for_each(name, bkt, obj, member) |
| 177 | #else |
| 178 | #define sfe_hash_for_each(name, bkt, node, obj, member) \ |
| 179 | hash_for_each(name, bkt, node, obj, member) |
| 180 | #endif |
Xiaoping Fan | 9b7690b | 2016-10-12 19:04:24 -0700 | [diff] [blame] | 181 | |
| 182 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)) |
| 183 | #define sfe_dst_get_neighbour(dst, daddr) dst_neigh_lookup(dst, addr) |
| 184 | #else |
| 185 | static inline struct neighbour * |
| 186 | sfe_dst_get_neighbour(struct dst_entry *dst, void *daddr) |
| 187 | { |
| 188 | struct neighbour *neigh = dst_get_neighbour_noref(dst); |
| 189 | |
| 190 | if (neigh) |
| 191 | neigh_hold(neigh); |
| 192 | |
| 193 | return neigh; |
| 194 | } |
| 195 | #endif |