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)) |
| 21 | #define sfe_cm_ipv4_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 22 | static 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) \ |
| 27 | static 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 Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 31 | #define sfe_cm_ipv4_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 32 | static 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 Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 37 | |
| 38 | #define sfe_cm_ipv6_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 39 | static 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 Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 44 | #else |
| 45 | #define sfe_cm_ipv4_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 46 | static 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 Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 51 | |
| 52 | #define sfe_cm_ipv6_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \ |
| 53 | static 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 Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 58 | #endif |
| 59 | |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame^] | 60 | #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 Fan | 79ed729 | 2015-03-27 16:47:19 -0700 | [diff] [blame] | 104 | /* |
| 105 | * sfe_dev_get_master |
| 106 | * get master of bridge port, and hold it |
| 107 | */ |
| 108 | static inline struct net_device *sfe_dev_get_master(struct net_device *dev) |
| 109 | { |
| 110 | struct net_device *master; |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame^] | 111 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)) |
Xiaoping Fan | 79ed729 | 2015-03-27 16:47:19 -0700 | [diff] [blame] | 112 | 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 Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 118 | #else |
Xiaoping Fan | 79ed729 | 2015-03-27 16:47:19 -0700 | [diff] [blame] | 119 | master = dev->master; |
| 120 | if (master) |
| 121 | dev_hold(master); |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 122 | #endif |
Xiaoping Fan | 79ed729 | 2015-03-27 16:47:19 -0700 | [diff] [blame] | 123 | return master; |
| 124 | } |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 125 | |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame^] | 126 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) |
Xiaoping Fan | 20f04cc | 2015-03-30 10:14:20 -0700 | [diff] [blame] | 127 | #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 | */ |
| 135 | int 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 | */ |
| 141 | static inline int sfe_cm_propagate_event(struct notifier_block *this, unsigned long event, struct net_device *dev) |
| 142 | { |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame^] | 143 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)) |
Xiaoping Fan | 20f04cc | 2015-03-30 10:14:20 -0700 | [diff] [blame] | 144 | 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 Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame^] | 153 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 154 | #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 Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame^] | 159 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 160 | #define SFE_ACCT_COUNTER(NM) ((NM)->counter) |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 161 | #else |
| 162 | #define SFE_ACCT_COUNTER(NM) (NM) |
| 163 | #endif |