1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
| #include "toa_netfilter.h"
#include <linux/netfilter.h> #include <net/sock.h> #include <net/tcp.h> #include <uapi/linux/netfilter_ipv4.h>
static atomic_t g_hook_enable = ATOMIC_INIT(0); static void enable_netfilter_hook(void) { atomic_set(&g_hook_enable, 1); }
static void disable_netfilter_hook(void) { atomic_set(&g_hook_enable, 0); }
bool is_toa_netfilter_enable(void) { return atomic_read(&g_hook_enable) == 1; }
static bool insert_ipv4_toa(struct sock *sk, struct sk_buff *skb) { struct tcphdr *th; unsigned int tcp_options_size, tcp_header_size; int length, ip_offset, tcp_offset; int mac_offset = 0; __be32 *ptr; __be32 i; u16 port = 0;
tcp_options_size = tcp_optlen(skb); if (MAX_TCP_OPTION_SPACE - tcp_options_size < TCPOLEN_TOAV4_ALIGNED) { LOG_WARN("there is not enough space left to add toa v4, remain size %d", MAX_TCP_OPTION_SPACE - tcp_options_size); return false; }
tcp_offset = skb_transport_offset(skb); ip_offset = skb_network_offset(skb); if (skb_mac_header_was_set(skb)) { mac_offset = skb_mac_offset(skb); } ptr = skb_push(skb, TCPOLEN_TOAV4_ALIGNED); skb_set_network_header(skb, ip_offset); skb_set_transport_header(skb, tcp_offset); if (mac_offset) { skb_set_mac_header(skb, mac_offset); } length = (skb_transport_offset(skb) + sizeof(struct tcphdr)) >> 2; for (i = 0; i < length; i++) { *ptr = *(ptr + TCPOLEN_TOAV4_ALIGNED / 4); ptr++; }
*ptr++ = htonl((TCPOPT_TOA << 24) | (TCPOLEN_TOAV4 << 16) | (htons(port))); *ptr++ = (u32)((u64)(sk->sk_user_data) >> 32); sk->sk_user_data = NULL;
th = tcp_hdr(skb); tcp_header_size = tcp_hdrlen(skb) + TCPOLEN_TOAV4_ALIGNED; th->doff = tcp_header_size >> 2; return true; }
static bool insert_ipv6_toa(struct sock *sk, struct sk_buff *skb) { static const char *tag = __FUNCTION__; toa_ipv6_node_t *node; struct tcphdr *th; unsigned int tcp_options_size, tcp_header_size; int length, ip_offset, tcp_offset; int mac_offset = 0; __be32 *ptr; __be32 i; u16 port = 0;
tcp_options_size = tcp_optlen(skb); if (MAX_TCP_OPTION_SPACE - tcp_options_size < TCPOLEN_TOAV6_ALIGNED) { LOG_WARN("%s, there is not enough space left to add toa v6, remain size %d", tag, MAX_TCP_OPTION_SPACE - tcp_options_size); return NF_ACCEPT; }
node = toa_ipv6_lookup_node(sk, true); if (!node) { LOG_WARN("%s, can not find ipv6 node, maybe cleaned", tag); return NF_ACCEPT; }
tcp_offset = skb_transport_offset(skb); ip_offset = skb_network_offset(skb); if (skb_mac_header_was_set(skb)) { mac_offset = skb_mac_offset(skb); } ptr = skb_push(skb, TCPOLEN_TOAV6_ALIGNED); skb_set_network_header(skb, ip_offset); skb_set_transport_header(skb, tcp_offset); if (mac_offset) { skb_set_mac_header(skb, mac_offset); } length = (skb_transport_offset(skb) + sizeof(struct tcphdr)) >> 2; for (i = 0; i < length; i++) { *ptr = *(ptr + TCPOLEN_TOAV6_ALIGNED / 4); ptr++; }
*ptr++ = htonl((TCPOPT_TOA << 24) | (TCPOLEN_TOAV6 << 16) | (htons(port))); memcpy(ptr, &node->ipv6, sizeof(node->ipv6)); kmem_cache_free(toa_ipv6_node_slab, node); node = NULL;
sk->sk_user_data = NULL;
th = tcp_hdr(skb); tcp_header_size = tcp_hdrlen(skb) + TCPOLEN_TOAV6_ALIGNED; th->doff = tcp_header_size >> 2; return NF_ACCEPT; }
static unsigned int toa_insert_hook(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { struct tcphdr *th; struct sock *sk = skb_to_full_sk(skb);
if (!is_toa_netfilter_enable()) return NF_ACCEPT;
if (!sk || !sk_fullsock(sk)) { return NF_ACCEPT; }
switch (sk->sk_family) { case PF_INET: { struct iphdr *iph = ip_hdr(skb); if (likely(iph->protocol != IPPROTO_TCP)) { return NF_ACCEPT; } } break; case PF_INET6: { struct ipv6hdr *iph = ipv6_hdr(skb); if (likely(iph->nexthdr != NEXTHDR_TCP)) { return NF_ACCEPT; } } break; default: return NF_ACCEPT; }
th = tcp_hdr(skb); switch (s_toa_mode) { case TOA_MODE_SYN: if (th->syn && !th->ack) { break; } return NF_ACCEPT; case TOA_MODE_ACK: if (th->ack && !th->syn && !th->fin) { break; } return NF_ACCEPT; default: LOG_WARN("unsupport toa_mode %d", s_toa_mode); return NF_ACCEPT; }
if (sock_flag(sk, SOCK_TOAV4_SET)) { if (!insert_ipv4_toa(sk, skb)) { return NF_ACCEPT; } sock_reset_flag(sk, SOCK_TOAV4_SET); INC_ATOMIC(s_settcpopt_count); } else if (sock_flag(sk, SOCK_TOAV6_SET)) { if (!insert_ipv6_toa(sk, skb)) { return NF_ACCEPT; } sock_reset_flag(sk, SOCK_TOAV6_SET); INC_ATOMIC(s_settcpoptv6_count); } else { return NF_ACCEPT; }
if (sk->sk_family == PF_INET) { struct iphdr *iph = ip_hdr(skb); th = tcp_hdr(skb); th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb), iph->saddr, iph->daddr, 0); skb->csum_start = skb_transport_header(skb) - skb->head; skb->csum_offset = offsetof(struct tcphdr, check);
iph->tot_len = htons(skb->len); ip_send_check(iph); } else { struct ipv6hdr *iph = ipv6_hdr(skb); th = tcp_hdr(skb); th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb), &iph->saddr, &iph->daddr, 0); skb->csum_start = skb_transport_header(skb) - skb->head; skb->csum_offset = offsetof(struct tcphdr, check);
iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); } return NF_ACCEPT; }
static struct nf_hook_ops s_hook_local_out = { .hook = toa_insert_hook, .hooknum = NF_INET_LOCAL_OUT, .pf = NFPROTO_INET, .priority = NF_IP_PRI_FIRST, };
void unregister_toa_netfilter_hook(void) { nf_unregister_net_hook(&init_net, &s_hook_local_out); }
int register_toa_netfilter_hook(void) { return nf_register_net_hook(&init_net, &s_hook_local_out); }
|