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 | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 20 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 21 | #define sfe_define_post_routing_hook(FN_NAME, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 22 | static unsigned int FN_NAME(void *priv, \ |
| 23 | struct sk_buff *SKB, \ |
| 24 | const struct nf_hook_state *state) |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 25 | #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 26 | #define sfe_define_post_routing_hook(FN_NAME, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 27 | static unsigned int FN_NAME(const struct nf_hook_ops *OPS, \ |
| 28 | struct sk_buff *SKB, \ |
| 29 | const struct net_device *UNUSED, \ |
| 30 | const struct net_device *OUT, \ |
| 31 | int (*OKFN)(struct sk_buff *)) |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 32 | #else |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 33 | #define sfe_define_post_routing_hook(FN_NAME, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 34 | static unsigned int FN_NAME(unsigned int HOOKNUM, \ |
| 35 | struct sk_buff *SKB, \ |
| 36 | const struct net_device *UNUSED, \ |
| 37 | const struct net_device *OUT, \ |
| 38 | int (*OKFN)(struct sk_buff *)) |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 39 | #endif |
| 40 | |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 41 | #define sfe_cm_ipv4_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 42 | sfe_define_post_routing_hook(__sfe_cm_ipv4_post_routing_hook, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) |
| 43 | #define sfe_cm_ipv6_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 44 | sfe_define_post_routing_hook(__sfe_cm_ipv6_post_routing_hook, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) |
| 45 | #define fast_classifier_ipv4_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 46 | 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^] | 47 | #define fast_classifier_ipv6_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 48 | 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] | 49 | |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 50 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) |
| 51 | #define SFE_IPV4_NF_POST_ROUTING_HOOK(fn) \ |
| 52 | { \ |
| 53 | .hook = fn, \ |
| 54 | .pf = NFPROTO_IPV4, \ |
| 55 | .hooknum = NF_INET_POST_ROUTING, \ |
| 56 | .priority = NF_IP_PRI_NAT_SRC + 1, \ |
| 57 | } |
| 58 | #else |
| 59 | #define SFE_IPV4_NF_POST_ROUTING_HOOK(fn) \ |
| 60 | { \ |
| 61 | .hook = fn, \ |
| 62 | .owner = THIS_MODULE, \ |
| 63 | .pf = NFPROTO_IPV4, \ |
| 64 | .hooknum = NF_INET_POST_ROUTING, \ |
| 65 | .priority = NF_IP_PRI_NAT_SRC + 1, \ |
| 66 | } |
| 67 | #endif |
| 68 | |
| 69 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) |
| 70 | #define SFE_IPV6_NF_POST_ROUTING_HOOK(fn) \ |
| 71 | { \ |
| 72 | .hook = fn, \ |
| 73 | .pf = NFPROTO_IPV6, \ |
| 74 | .hooknum = NF_INET_POST_ROUTING, \ |
| 75 | .priority = NF_IP_PRI_NAT_SRC + 1, \ |
| 76 | } |
| 77 | #else |
| 78 | #define SFE_IPV6_NF_POST_ROUTING_HOOK(fn) \ |
| 79 | { \ |
| 80 | .hook = fn, \ |
| 81 | .owner = THIS_MODULE, \ |
| 82 | .pf = NFPROTO_IPV6, \ |
| 83 | .hooknum = NF_INET_POST_ROUTING, \ |
| 84 | .priority = NF_IP6_PRI_NAT_SRC + 1, \ |
| 85 | } |
| 86 | #endif |
| 87 | |
| 88 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)) |
| 89 | #define SFE_NF_CT_DEFAULT_ZONE (&nf_ct_zone_dflt) |
| 90 | #else |
| 91 | #define SFE_NF_CT_DEFAULT_ZONE NF_CT_DEFAULT_ZONE |
| 92 | #endif |
| 93 | |
Xiaoping Fan | 79ed729 | 2015-03-27 16:47:19 -0700 | [diff] [blame] | 94 | /* |
| 95 | * sfe_dev_get_master |
| 96 | * get master of bridge port, and hold it |
| 97 | */ |
| 98 | static inline struct net_device *sfe_dev_get_master(struct net_device *dev) |
| 99 | { |
| 100 | struct net_device *master; |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 101 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) |
Xiaoping Fan | 79ed729 | 2015-03-27 16:47:19 -0700 | [diff] [blame] | 102 | rcu_read_lock(); |
| 103 | master = netdev_master_upper_dev_get_rcu(dev); |
| 104 | if (master) |
| 105 | dev_hold(master); |
| 106 | |
| 107 | rcu_read_unlock(); |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 108 | #else |
Xiaoping Fan | 79ed729 | 2015-03-27 16:47:19 -0700 | [diff] [blame] | 109 | master = dev->master; |
| 110 | if (master) |
| 111 | dev_hold(master); |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 112 | #endif |
Xiaoping Fan | 79ed729 | 2015-03-27 16:47:19 -0700 | [diff] [blame] | 113 | return master; |
| 114 | } |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 115 | |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 116 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) |
Xiaoping Fan | 20f04cc | 2015-03-30 10:14:20 -0700 | [diff] [blame] | 117 | #define SFE_DEV_EVENT_PTR(PTR) netdev_notifier_info_to_dev(PTR) |
| 118 | #else |
| 119 | #define SFE_DEV_EVENT_PTR(PTR) (struct net_device *)(PTR) |
| 120 | #endif |
| 121 | |
| 122 | /* |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 123 | * declare function sfe_dev_event_cb_t |
Xiaoping Fan | 20f04cc | 2015-03-30 10:14:20 -0700 | [diff] [blame] | 124 | */ |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 125 | typedef int (*sfe_dev_event_cb_t)(struct notifier_block *this, unsigned long event, void *ptr); |
Xiaoping Fan | 20f04cc | 2015-03-30 10:14:20 -0700 | [diff] [blame] | 126 | |
| 127 | /* |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 128 | * sfe_propagate_dev_event |
Xiaoping Fan | 20f04cc | 2015-03-30 10:14:20 -0700 | [diff] [blame] | 129 | * propagate ip address event as network device event |
| 130 | */ |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 131 | static inline int sfe_propagate_dev_event(sfe_dev_event_cb_t fn, struct notifier_block *this, unsigned long event, struct net_device *dev) |
Xiaoping Fan | 20f04cc | 2015-03-30 10:14:20 -0700 | [diff] [blame] | 132 | { |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 133 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) |
Xiaoping Fan | 20f04cc | 2015-03-30 10:14:20 -0700 | [diff] [blame] | 134 | struct netdev_notifier_info info; |
| 135 | |
| 136 | netdev_notifier_info_init(&info, dev); |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 137 | return fn(this, event, &info); |
Xiaoping Fan | 20f04cc | 2015-03-30 10:14:20 -0700 | [diff] [blame] | 138 | #else |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 139 | return fn(this, event, dev); |
Xiaoping Fan | 20f04cc | 2015-03-30 10:14:20 -0700 | [diff] [blame] | 140 | #endif |
| 141 | } |
| 142 | |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 143 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 144 | #define SFE_NF_CONN_ACCT(NM) struct nf_conn_acct *NM |
| 145 | #else |
| 146 | #define SFE_NF_CONN_ACCT(NM) struct nf_conn_counter *NM |
| 147 | #endif |
| 148 | |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 149 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 150 | #define SFE_ACCT_COUNTER(NM) ((NM)->counter) |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 151 | #else |
| 152 | #define SFE_ACCT_COUNTER(NM) (NM) |
| 153 | #endif |
Xiaoping Fan | 8da1387 | 2016-04-04 18:51:56 -0700 | [diff] [blame] | 154 | |
| 155 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) |
| 156 | #define sfe_hash_for_each_possible(name, obj, node, member, key) \ |
| 157 | hash_for_each_possible(name, obj, member, key) |
| 158 | #else |
| 159 | #define sfe_hash_for_each_possible(name, obj, node, member, key) \ |
| 160 | hash_for_each_possible(name, obj, node, member, key) |
| 161 | #endif |
| 162 | |
| 163 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) |
| 164 | #define sfe_hash_for_each(name, bkt, node, obj, member) \ |
| 165 | hash_for_each(name, bkt, obj, member) |
| 166 | #else |
| 167 | #define sfe_hash_for_each(name, bkt, node, obj, member) \ |
| 168 | hash_for_each(name, bkt, node, obj, member) |
| 169 | #endif |