b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Fast path Classifier |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | */ |
| 9 | |
| 10 | #define pr_fmt(fmt) "mfp" " classifier:%s:%d: " fmt, __func__, __LINE__ |
| 11 | |
| 12 | #include "fp_common.h" |
| 13 | #include "fp_database.h" |
| 14 | #include "fp_device.h" |
| 15 | #include "fp_core.h" |
| 16 | |
| 17 | struct fpc_stats { |
| 18 | u32 total; |
| 19 | u32 slow; |
| 20 | u32 fast; |
| 21 | }; |
| 22 | |
| 23 | static struct fpc_stats stats; |
| 24 | |
| 25 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 3, 0) |
| 26 | static unsigned int udp_ct_timeout = UDP_DEFAULT_TIMEOUT; |
| 27 | static unsigned int udp_ct_timeout_stream = UDP_DEFAULT_TIMEOUT_STREAM; |
| 28 | static unsigned int tcp_ct_timeout = TCP_DEFAULT_TIMEOUT; |
| 29 | #endif |
| 30 | static int fp_acct_flag = 1; |
| 31 | static int fp_ip_log_en = 255; |
| 32 | static int fp_ip_log_pkt_num; |
| 33 | static unsigned char *fp_ip_log_buf; |
| 34 | static int fp_ip_log_index; |
| 35 | #define ONE_IP_LOG_LEN 96 |
| 36 | |
| 37 | //#define CONFIG_SET_HL_64 |
| 38 | |
| 39 | static inline int fp_ip_decrease_ttl(struct sk_buff *skb) |
| 40 | { |
| 41 | if (ip_hdr(skb)->version == 4) |
| 42 | return ip_decrease_ttl(ip_hdr(skb)); |
| 43 | else |
| 44 | return --ipv6_hdr(skb)->hop_limit; |
| 45 | } |
| 46 | |
| 47 | /* builds a tuple according to the parameters received) */ |
| 48 | static inline void build_tuple(const struct sk_buff *skb, |
| 49 | struct nf_conntrack_tuple *tuple) |
| 50 | { |
| 51 | int proto; |
| 52 | struct udphdr *udph; |
| 53 | /* Fill l3 info */ |
| 54 | if (ip_hdr(skb)->version == 4) { |
| 55 | tuple->src.l3num = AF_INET; |
| 56 | tuple->src.u3.ip = ip_hdr(skb)->saddr; |
| 57 | tuple->dst.u3.ip = ip_hdr(skb)->daddr; |
| 58 | proto = ip_hdr(skb)->protocol; |
| 59 | } else { |
| 60 | tuple->src.l3num = AF_INET6; |
| 61 | tuple->src.u3.in6 = ipv6_hdr(skb)->saddr; |
| 62 | tuple->dst.u3.in6 = ipv6_hdr(skb)->daddr; |
| 63 | proto = ipv6_hdr(skb)->nexthdr; |
| 64 | } |
| 65 | |
| 66 | /* Fill l4 info*/ |
| 67 | udph = (struct udphdr *)skb_transport_header(skb); |
| 68 | tuple->dst.protonum = proto; |
| 69 | tuple->dst.u.all = udph->dest; |
| 70 | tuple->src.u.all = udph->source; |
| 71 | tuple->dst.dir = 0; |
| 72 | } |
| 73 | |
| 74 | static inline void log_ip_pkt(const struct sk_buff *skb, unsigned char *buf) |
| 75 | { |
| 76 | struct tcphdr *tcph; |
| 77 | struct iphdr *piphdr; |
| 78 | struct ipv6hdr *pipv6hdr; |
| 79 | struct timespec64 ts; |
| 80 | piphdr = ip_hdr(skb); |
| 81 | |
| 82 | ktime_get_real_ts64(&ts); |
| 83 | memcpy(buf, &ts.tv_sec, 8); |
| 84 | buf += 8; |
| 85 | memcpy(buf, &ts.tv_nsec, 4); |
| 86 | buf += 4; |
| 87 | /* Fill l3 info */ |
| 88 | if (piphdr->version == 4) { |
| 89 | *buf = 4; |
| 90 | buf += 1; |
| 91 | *buf = piphdr->protocol; |
| 92 | buf += 1; |
| 93 | memcpy(buf, &piphdr->id, 2); |
| 94 | buf += 2; |
| 95 | memcpy(buf, &piphdr->tot_len, 2); |
| 96 | buf += 4; |
| 97 | memcpy(buf, &piphdr->saddr, 4); |
| 98 | buf += 16; |
| 99 | memcpy(buf, &piphdr->daddr, 4); |
| 100 | buf += 16; |
| 101 | } else { |
| 102 | pipv6hdr = ipv6_hdr(skb); |
| 103 | *buf = 6; |
| 104 | buf += 1; |
| 105 | *buf = pipv6hdr->nexthdr; |
| 106 | buf += 1; |
| 107 | *buf = 0; |
| 108 | *(buf+1) = 0; |
| 109 | buf += 2; |
| 110 | memcpy(buf, &pipv6hdr->payload_len, 2); |
| 111 | buf += 4; |
| 112 | memcpy(buf, &pipv6hdr->saddr, 16); |
| 113 | buf += 16; |
| 114 | memcpy(buf, &pipv6hdr->daddr, 16); |
| 115 | buf += 16; |
| 116 | } |
| 117 | |
| 118 | /* Fill l4 info*/ |
| 119 | tcph = (struct tcphdr *)skb_transport_header(skb); |
| 120 | |
| 121 | memcpy(buf, &tcph->source, 2); |
| 122 | buf += 2; |
| 123 | memcpy(buf, &tcph->dest, 2); |
| 124 | buf += 2; |
| 125 | |
| 126 | memcpy(buf, &tcph->seq, 4); |
| 127 | buf += 4; |
| 128 | |
| 129 | memcpy(buf, &tcph->ack_seq, 4); |
| 130 | buf += 4; |
| 131 | memcpy(buf, ((char *)&tcph->ack_seq)+4, 2); |
| 132 | buf += 2; |
| 133 | |
| 134 | |
| 135 | } |
| 136 | |
| 137 | /* checksum adjust (inline) */ |
| 138 | static inline void fpc_checksum(unsigned char *chksum, |
| 139 | unsigned char *optr, unsigned long olen, |
| 140 | unsigned char *nptr, unsigned long nlen, |
| 141 | int proto) |
| 142 | { |
| 143 | long x, old, neu; |
| 144 | |
| 145 | if (proto == IPPROTO_UDP && *(__sum16 *)chksum == 0) |
| 146 | return; |
| 147 | |
| 148 | x = chksum[0] * 256 + chksum[1]; |
| 149 | x = ~x & 0xFFFF; |
| 150 | while (olen) { |
| 151 | old = optr[0] * 256 + optr[1]; |
| 152 | optr += 2; |
| 153 | x -= old & 0xffff; |
| 154 | if (x <= 0) { |
| 155 | x--; |
| 156 | x &= 0xffff; |
| 157 | } |
| 158 | olen -= 2; |
| 159 | } |
| 160 | |
| 161 | while (nlen) { |
| 162 | neu = nptr[0] * 256 + nptr[1]; |
| 163 | nptr += 2; |
| 164 | x += neu & 0xffff; |
| 165 | if (x & 0x10000) { |
| 166 | x++; |
| 167 | x &= 0xffff; |
| 168 | } |
| 169 | nlen -= 2; |
| 170 | } |
| 171 | x = ~x & 0xFFFF; |
| 172 | chksum[0] = (unsigned char)(x / 256); |
| 173 | chksum[1] = (unsigned char)(x & 0xff); |
| 174 | } |
| 175 | |
| 176 | static inline int fp_hard_header(struct sk_buff *skb, struct fpdb_entry *e) |
| 177 | { |
| 178 | struct hh_cache *hh = &e->hh; |
| 179 | int hh_len = hh->hh_len; |
| 180 | unsigned int hh_alen = 0; |
| 181 | unsigned int headroom; |
| 182 | |
| 183 | if (!hh_len) |
| 184 | return 0; |
| 185 | |
| 186 | headroom = skb_headroom(skb); |
| 187 | if (likely(hh_len <= HH_DATA_MOD)) { |
| 188 | hh_alen = HH_DATA_MOD; |
| 189 | |
| 190 | /* this is inlined by gcc */ |
| 191 | if (likely(headroom >= HH_DATA_MOD)) |
| 192 | memcpy(skb->data - HH_DATA_MOD, hh->hh_data, |
| 193 | HH_DATA_MOD); |
| 194 | } else { |
| 195 | hh_alen = HH_DATA_ALIGN(hh_len); |
| 196 | |
| 197 | if (likely(headroom >= hh_alen)) |
| 198 | memcpy(skb->data - hh_alen, hh->hh_data, |
| 199 | hh_alen); |
| 200 | } |
| 201 | |
| 202 | if (WARN_ON_ONCE(headroom < hh_alen)) |
| 203 | return 1; |
| 204 | |
| 205 | skb_push(skb, hh_len); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Refresh ct (reschedule timeout) |
| 212 | * |
| 213 | * @param skb |
| 214 | * @param el |
| 215 | * @param acct do accounting |
| 216 | */ |
| 217 | static inline void fpc_refresh(struct sk_buff *skb, struct fpdb_entry *el, int acct) |
| 218 | { |
| 219 | struct nf_conn *ct = el->ct; |
| 220 | const struct nf_conntrack_l4proto *l4proto; |
| 221 | enum ip_conntrack_info ctinfo = el->dir ? IP_CT_IS_REPLY : 0; |
| 222 | unsigned long extra_jiffies = 0; |
| 223 | #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0) |
| 224 | unsigned int *timeouts; |
| 225 | #endif |
| 226 | |
| 227 | l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct)); |
| 228 | NF_CT_ASSERT(l4proto); |
| 229 | |
| 230 | #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0) |
| 231 | if (l4proto->l4proto == IPPROTO_TCP) { |
| 232 | timeouts = nf_tcp_pernet(nf_ct_net(ct))->timeouts; |
| 233 | WARN_ON(ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED); |
| 234 | extra_jiffies = timeouts[TCP_CONNTRACK_ESTABLISHED]; |
| 235 | } else if (l4proto->l4proto == IPPROTO_UDP) { |
| 236 | timeouts = nf_udp_pernet(nf_ct_net(ct))->timeouts; |
| 237 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) |
| 238 | extra_jiffies = timeouts[UDP_CT_REPLIED]; |
| 239 | else |
| 240 | extra_jiffies = timeouts[UDP_CT_UNREPLIED]; |
| 241 | } |
| 242 | #else |
| 243 | if (l4proto->l4proto == IPPROTO_TCP) { |
| 244 | WARN_ON(ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED); |
| 245 | extra_jiffies = tcp_ct_timeout; |
| 246 | } else if (l4proto->l4proto == IPPROTO_UDP) { |
| 247 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) |
| 248 | extra_jiffies = udp_ct_timeout_stream; |
| 249 | else |
| 250 | extra_jiffies = udp_ct_timeout; |
| 251 | } |
| 252 | #endif |
| 253 | __nf_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, acct); |
| 254 | |
| 255 | fpdb_trace(el, (l4proto->l4proto == IPPROTO_TCP) ? tcp_hdr(skb) : NULL); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Modify skb as if it was forwarded by the ip stack: |
| 260 | * L2: Add MAC Header, set skb->pkt_type = PACKET_HOST |
| 261 | * L3: Decrement ttl, NAT, checksum |
| 262 | * L4: Checksum |
| 263 | * |
| 264 | * @param skb skb to modify |
| 265 | * @param el fpdb_entry related to this connection |
| 266 | */ |
| 267 | static inline int fpc_modify(struct sk_buff *skb, |
| 268 | struct fpdb_entry *el) |
| 269 | { |
| 270 | int version = ip_hdr(skb)->version; |
| 271 | int proto = (version == 4) ? ip_hdr(skb)->protocol : ipv6_hdr(skb)->nexthdr; |
| 272 | struct udphdr *udph = udp_hdr(skb); |
| 273 | struct tcphdr *tcph = tcp_hdr(skb); |
| 274 | |
| 275 | /** |
| 276 | * skb->pkt_type can be either PACKET_HOST or PACKET_OTHERHOST |
| 277 | * (see fpc_classify_start). We also know that this flow passed |
| 278 | * through slowpath (otherwise fastpath connection would not |
| 279 | * have been created in the first place). Therefore it is safe |
| 280 | * to change the pkt_type since this is what the IP Stack would |
| 281 | * have done. |
| 282 | * |
| 283 | * Slowpath behavior: |
| 284 | * PACKET_OTHERHOST is set by the receiving interface if the |
| 285 | * dest MAC is different from it's MAC address. In this case |
| 286 | * this means that the packet is not destined to us and is |
| 287 | * dropped. The only exception is if the receiving interface is |
| 288 | * behind a bridge. In this case, the dest MAC in packets sent |
| 289 | * outside the LAN is the bridge MAC address, in which case the |
| 290 | * bridging code sets the pkt_type to PACKET_HOST before |
| 291 | * routing the packet. Packes withing the LAN sre bridged and |
| 292 | * are not passed to the upper layers, and therefore doesn't go |
| 293 | * through fastpath unless CONFIG_BRIDGE_NETFILTER is enabled - |
| 294 | * which is the only case where fastpath "misbehaves" and sets |
| 295 | * the pkt_type to PACKET_HOST for bridged packets - this might |
| 296 | * need revision in the future. |
| 297 | */ |
| 298 | skb->pkt_type = PACKET_HOST; |
| 299 | |
| 300 | if (fp_hard_header(skb, el)) |
| 301 | return 1; |
| 302 | |
| 303 | fp_ip_decrease_ttl(skb); |
| 304 | |
| 305 | /* NAT (incase used by this connection) */ |
| 306 | if (NF_CT_NAT(el->ct)) { |
| 307 | void *old, *new; |
| 308 | unsigned int size; |
| 309 | __sum16 *check; |
| 310 | |
| 311 | /* NAT L3 ip addresses manipulation */ |
| 312 | if (likely(version == 4)) { |
| 313 | struct iphdr *iph = ip_hdr(skb); |
| 314 | iph->saddr = el->out_tuple.dst.u3.ip; |
| 315 | iph->daddr = el->out_tuple.src.u3.ip; |
| 316 | #ifdef CONFIG_SET_HL_64 |
| 317 | iph->ttl = 64; |
| 318 | #endif |
| 319 | ip_send_check(iph); /*IPv4 checksum */ |
| 320 | } else { |
| 321 | struct ipv6hdr *iph = ipv6_hdr(skb); |
| 322 | iph->saddr = el->out_tuple.dst.u3.in6; |
| 323 | iph->daddr = el->out_tuple.src.u3.in6; |
| 324 | #ifdef CONFIG_SET_HL_64 |
| 325 | iph->hop_limit = 64; |
| 326 | #endif |
| 327 | } |
| 328 | |
| 329 | /* Adjust transport header checksum */ |
| 330 | check = (proto == IPPROTO_UDP) ? &udph->check : &tcph->check; |
| 331 | size = (version == 4) ? 4 : 16; |
| 332 | old = &el->in_tuple.src.u3.in6; |
| 333 | new = &el->out_tuple.dst.u3.in6; |
| 334 | fpc_checksum((u8 *)check, old, size, new, size, proto); |
| 335 | old = &el->in_tuple.dst.u3.in6; |
| 336 | new = &el->out_tuple.src.u3.in6; |
| 337 | fpc_checksum((u8 *)check, old, size, new, size, proto); |
| 338 | |
| 339 | |
| 340 | /* NAT L4 ports manipulation */ |
| 341 | size = sizeof(__be16); |
| 342 | old = &el->in_tuple.dst.u.all; |
| 343 | new = &el->out_tuple.src.u.all; |
| 344 | if (*(__be16 *)old != *(__be16 *)new) { |
| 345 | udph->dest = *(__be16 *)new; |
| 346 | fpc_checksum((u8 *)check, old, size, new, size, proto); |
| 347 | } |
| 348 | old = &el->in_tuple.src.u.all; |
| 349 | new = &el->out_tuple.dst.u.all; |
| 350 | if (*(__be16 *)old != *(__be16 *)new) { |
| 351 | udph->source = *(__be16 *)new; |
| 352 | fpc_checksum((u8 *)check, old, size, new, size, proto); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static inline bool ipv4_is_fragmented(struct iphdr *iph) |
| 360 | { |
| 361 | __be16 df = iph->frag_off & htons(IP_DF); |
| 362 | return (iph->frag_off && !df); |
| 363 | } |
| 364 | |
| 365 | static inline int parse_headers(struct sk_buff *skb) |
| 366 | { |
| 367 | int ihl, proto; |
| 368 | |
| 369 | BUG_ON(!skb); |
| 370 | skb_reset_network_header(skb); |
| 371 | |
| 372 | /* L3 Protocol parsing */ |
| 373 | if (likely(ip_hdr(skb)->version == 4)) { |
| 374 | ihl = ip_hdr(skb)->ihl * 4; |
| 375 | proto = ip_hdr(skb)->protocol; |
| 376 | |
| 377 | /*ipv4 sanity checks*/ |
| 378 | if (unlikely(ihl > sizeof(struct iphdr))) { |
| 379 | pr_debug("ipv4 options in header\n"); |
| 380 | return 0; |
| 381 | } |
| 382 | /* check ttl */ |
| 383 | if (unlikely(ip_hdr(skb)->ttl == 1)) { |
| 384 | pr_debug("ip->ttl==1\n"); |
| 385 | return 0; |
| 386 | } |
| 387 | /* check fragmantation */ |
| 388 | if (unlikely(ipv4_is_fragmented(ip_hdr(skb)))) { |
| 389 | pr_debug("fragmented packet (frag_offs=%x)\n", |
| 390 | ntohs(ip_hdr(skb)->frag_off)); |
| 391 | return 0; |
| 392 | } |
| 393 | /* ipv4 reassembled pkts */ |
| 394 | if (unlikely(skb->data_len)) { |
| 395 | pr_debug("ipv4 reassembled pkts --> send to slowpath\n"); |
| 396 | return 0; |
| 397 | } |
| 398 | } else if (likely(ip_hdr(skb)->version == 6)) { |
| 399 | ihl = sizeof(struct ipv6hdr); /* without extentions */ |
| 400 | proto = ipv6_hdr(skb)->nexthdr; |
| 401 | |
| 402 | /* ipv6 sanity checks */ |
| 403 | if (unlikely(ipv6_hdr(skb)->hop_limit == 1)) { |
| 404 | pr_debug("ip->ttl==1 --> send to slowpath\n"); |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | /* ipv6 reassembled pkts */ |
| 409 | if (unlikely(skb->data_len)) { |
| 410 | pr_debug("ipv6 reassembled pkts --> send to slowpath\n"); |
| 411 | return 0; |
| 412 | } |
| 413 | } else { |
| 414 | /* Not an IP packet (neither ipv4 nor ipv6) */ |
| 415 | pr_debug("not an IP packet\n"); |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | /* L4 Protocol parsing */ |
| 420 | skb_set_transport_header(skb, ihl); |
| 421 | |
| 422 | if (proto == IPPROTO_TCP) { |
| 423 | struct tcphdr *th = tcp_hdr(skb); |
| 424 | |
| 425 | if (tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_FIN)) { |
| 426 | pr_debug("tcp rst or fin\n"); |
| 427 | return 0; |
| 428 | } |
| 429 | } else if (proto != IPPROTO_UDP) { |
| 430 | pr_debug("not a TCP or UDP packet\n"); |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | return 1; |
| 435 | } |
| 436 | |
| 437 | #define NETIF_INVALID(x) (!(x) || !netif_device_present(x) || \ |
| 438 | !netif_running(x) || !netif_carrier_ok(x)) |
| 439 | |
| 440 | /** |
| 441 | * finish classification for this database entry. |
| 442 | * If skb is not NULL, it is tracked & mangled. |
| 443 | * |
| 444 | * @param skb skb to mangle & track, or NULL if not desired |
| 445 | * @param el fpdb_entry previously aquired by fpc_classify |
| 446 | */ |
| 447 | int fpc_classify_finish(struct sk_buff *skb, struct fpdb_entry *el) |
| 448 | { |
| 449 | int ret = 0; |
| 450 | |
| 451 | if (skb) { |
| 452 | fpc_refresh(skb, el, fp_acct_flag); |
| 453 | if (fpc_modify(skb, el)) { |
| 454 | ret = 1; |
| 455 | goto exit; |
| 456 | } |
| 457 | |
| 458 | /* update timestamp if fpdb used */ |
| 459 | el->tstamp = jiffies; |
| 460 | if (!el->tstamp) |
| 461 | el->tstamp = 1; |
| 462 | } |
| 463 | exit: |
| 464 | fpdb_put(el); |
| 465 | return ret; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Classifies an skb as fast or slow, without changing the skb. |
| 470 | * Caller MUST call fpc_classify_finish to free the database entry. |
| 471 | * |
| 472 | * @param skb skb to classify |
| 473 | * |
| 474 | * @return fpdb_entry for this skb |
| 475 | */ |
| 476 | struct fpdb_entry *fpc_classify_start(struct sk_buff *skb, struct nf_conntrack_tuple *tuple) |
| 477 | { |
| 478 | struct fpdb_entry *el = NULL; |
| 479 | struct net_device *src, *dst; |
| 480 | int tmp_log_pkt_index; |
| 481 | unsigned char *plog_pos; |
| 482 | |
| 483 | BUG_ON(!skb); |
| 484 | BUG_ON(!skb->dev); /* eth_type_trans always sets skb->dev - we count on it here */ |
| 485 | |
| 486 | src = skb->dev; |
| 487 | stats.total++; |
| 488 | |
| 489 | if (unlikely(skb_headroom(skb) < ETH_HLEN)) { |
| 490 | pr_debug("No room for MAC header in skb\n"); |
| 491 | goto slowpath; |
| 492 | } |
| 493 | |
| 494 | /* source device sanity checks */ |
| 495 | if (unlikely(NETIF_INVALID(src))) { |
| 496 | pr_debug("src (%s) state invalid (%lu)\n", src->name, src->state); |
| 497 | goto slowpath; |
| 498 | } |
| 499 | |
| 500 | memset(tuple, 0, sizeof(struct nf_conntrack_tuple)); |
| 501 | |
| 502 | if (unlikely(!parse_headers(skb))) |
| 503 | goto slowpath; |
| 504 | |
| 505 | /* Check fp_database for match */ |
| 506 | build_tuple(skb, tuple); |
| 507 | if (1 == fp_ip_log_en) { |
| 508 | tmp_log_pkt_index = fp_ip_log_index++; |
| 509 | if (fp_ip_log_index > fp_ip_log_pkt_num - 50) |
| 510 | fp_ip_log_index = 0; |
| 511 | |
| 512 | plog_pos = fp_ip_log_buf + tmp_log_pkt_index*ONE_IP_LOG_LEN; |
| 513 | log_ip_pkt(skb, plog_pos); |
| 514 | } |
| 515 | el = fpdb_get(tuple); |
| 516 | if (unlikely(!el)) |
| 517 | goto slowpath; |
| 518 | |
| 519 | if (unlikely(el->block)) { |
| 520 | pr_debug("entry blocked, send to slowpath\n"); |
| 521 | goto slowpath; |
| 522 | } |
| 523 | |
| 524 | if (unlikely(nf_ct_protonum(el->ct) == IPPROTO_TCP) && |
| 525 | el->ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED) { |
| 526 | pr_debug("tcp connection state not established\n"); |
| 527 | goto slowpath; |
| 528 | } |
| 529 | |
| 530 | if (unlikely(el->in_dev->dev != src && |
| 531 | el->in_dev->br != src)) { |
| 532 | /** |
| 533 | * Since entry can be updated (due to route changes) this case |
| 534 | * is legal for a short period of time in which packets are |
| 535 | * received using the old entry and transmitted using the new |
| 536 | * one. Since we dont knwo if this is the case or not we will |
| 537 | * just forward this packets to slowpath to decide what to do. |
| 538 | */ |
| 539 | pr_debug("in_dev->dev=%s(%p) != src=%s(%p)\n", |
| 540 | el->in_dev->dev->name, el->in_dev->dev, src->name, src); |
| 541 | goto slowpath; |
| 542 | } |
| 543 | |
| 544 | if (unlikely(!el->in_dev->forward || !el->out_dev->forward)) { |
| 545 | pr_debug("forwarding disabled (%s forward=%d, %s forward=%d)\n", |
| 546 | el->in_dev->dev->name, el->in_dev->forward, |
| 547 | el->out_dev->dev->name, el->out_dev->forward); |
| 548 | goto slowpath; |
| 549 | } |
| 550 | |
| 551 | dst = el->out_dev->dev; |
| 552 | if (unlikely(NETIF_INVALID(dst))) { |
| 553 | pr_debug("dst (%s) state invalid (%lu)\n", dst->name, dst->state); |
| 554 | goto slowpath; |
| 555 | } |
| 556 | |
| 557 | if (unlikely(dst->mtu < skb->len)) { |
| 558 | pr_info_once("mtu (%d) < len (%d)\n", dst->mtu, skb->len); |
| 559 | goto slowpath; |
| 560 | } |
| 561 | |
| 562 | if (unlikely(dst == src)) { |
| 563 | /* src == dst entries should be blocked, it's a bug otherwise */ |
| 564 | /* here we don't need to dump entry. It will cause assert */ |
| 565 | /* because it takes a lot of time yhuang 20160622 */ |
| 566 | pr_err("Bug in classifier dst_dev==src_dev(%s), block=%d\n", |
| 567 | src->name, (unsigned int)el->block); |
| 568 | /* FP_ERR_DUMP_ENTRY(NULL, el); */ |
| 569 | /* BUG_ON(debug_level & DBG_WARN_AS_ERR); */ |
| 570 | goto slowpath; |
| 571 | |
| 572 | } |
| 573 | |
| 574 | if (unlikely(dst->header_ops && !el->hh.hh_len)) { |
| 575 | pr_debug("hh_cache not valid, send to slowpath\n"); |
| 576 | goto slowpath; |
| 577 | } |
| 578 | |
| 579 | if (unlikely(skb->pkt_type != PACKET_HOST && |
| 580 | skb->pkt_type != PACKET_OTHERHOST)) { |
| 581 | pr_debug("invalid skb->pkt_type(%d)\n", skb->pkt_type); |
| 582 | goto slowpath; |
| 583 | } |
| 584 | |
| 585 | pr_debug("Packet from %s to %s (pkt_p %p len %d) classified as fast path\n", |
| 586 | src->name, dst->name, skb->data, skb->len); |
| 587 | stats.fast++; |
| 588 | return el; |
| 589 | |
| 590 | slowpath: |
| 591 | if (el) |
| 592 | fpdb_put(el); |
| 593 | pr_debug("Packet from %s (pkt_p %p len %d) classified as slow path\n", |
| 594 | src->name, skb->data, skb->len); |
| 595 | stats.slow++; |
| 596 | return NULL; |
| 597 | |
| 598 | } |
| 599 | |
| 600 | |
| 601 | /** |
| 602 | * classify, mangle, track and hold the output device |
| 603 | * Caller MUST release the device with fp_dev_put() once finished. |
| 604 | * |
| 605 | * @param skb skb to classify and mangle |
| 606 | * |
| 607 | * @return destination fp_net_device or NULL if classified as |
| 608 | * slow path |
| 609 | */ |
| 610 | struct fp_net_device *fpc_classify(struct sk_buff *skb) |
| 611 | { |
| 612 | struct fpdb_entry *el; |
| 613 | struct fp_net_device *fdev; |
| 614 | struct nf_conntrack_tuple tuple; |
| 615 | |
| 616 | el = fpc_classify_start(skb, &tuple); |
| 617 | if (unlikely(!el)) |
| 618 | return NULL; |
| 619 | fdev = fpdev_hold(el->out_dev); |
| 620 | if (fpc_classify_finish(skb, el)) |
| 621 | return NULL; |
| 622 | |
| 623 | return fdev; |
| 624 | } |
| 625 | |
| 626 | static ssize_t stats_show(struct fastpath_module *m, char *buf) |
| 627 | { |
| 628 | int len; |
| 629 | |
| 630 | len = sprintf(buf, "Fast Path Classifier statistics:\n"); |
| 631 | |
| 632 | len += sprintf(buf + len, "Total Classified %d ", stats.total); |
| 633 | len += sprintf(buf + len, "(Fast %d, Slow %d)\n", stats.fast, stats.slow); |
| 634 | |
| 635 | return len; |
| 636 | } |
| 637 | |
| 638 | static ssize_t stats_clear(struct fastpath_module *m, const char *buf, |
| 639 | size_t count) |
| 640 | { |
| 641 | pr_debug("reset stats...\n"); |
| 642 | memset(&stats, 0, sizeof(stats)); |
| 643 | return count; |
| 644 | } |
| 645 | |
| 646 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 3, 0) |
| 647 | static ssize_t udp_ct_timeout_set(struct fastpath_module *m, const char *buf, |
| 648 | size_t count) |
| 649 | { |
| 650 | unsigned int sec; |
| 651 | sscanf(buf, "%u", &sec); |
| 652 | udp_ct_timeout = sec * HZ; |
| 653 | return count; |
| 654 | } |
| 655 | |
| 656 | static ssize_t udp_ct_timeout_get(struct fastpath_module *m, char *buf) |
| 657 | { |
| 658 | unsigned int sec = udp_ct_timeout / HZ; |
| 659 | return sprintf(buf, "%u\n", sec); |
| 660 | } |
| 661 | |
| 662 | static ssize_t tcp_ct_timeout_set(struct fastpath_module *m, const char *buf, |
| 663 | size_t count) |
| 664 | { |
| 665 | unsigned int sec; |
| 666 | sscanf(buf, "%u", &sec); |
| 667 | tcp_ct_timeout = sec * HZ; |
| 668 | return count; |
| 669 | } |
| 670 | |
| 671 | static ssize_t tcp_ct_timeout_get(struct fastpath_module *m, char *buf) |
| 672 | { |
| 673 | unsigned int sec = tcp_ct_timeout / HZ; |
| 674 | return sprintf(buf, "%u\n", sec); |
| 675 | } |
| 676 | |
| 677 | |
| 678 | static FP_ATTR(udp_ct_timeout, S_IRUGO|S_IWUSR, udp_ct_timeout_get, udp_ct_timeout_set); |
| 679 | static FP_ATTR(tcp_ct_timeout, S_IRUGO|S_IWUSR, tcp_ct_timeout_get, tcp_ct_timeout_set); |
| 680 | #endif |
| 681 | |
| 682 | static ssize_t fp_acct_set(struct fastpath_module *m, const char *buf, |
| 683 | size_t count) |
| 684 | { |
| 685 | int flag; |
| 686 | sscanf(buf, "%d", &flag); |
| 687 | fp_acct_flag = flag; |
| 688 | return count; |
| 689 | } |
| 690 | |
| 691 | static ssize_t fp_acct_get(struct fastpath_module *m, char *buf) |
| 692 | { |
| 693 | int flag = fp_acct_flag; |
| 694 | return sprintf(buf, "%d\n", flag); |
| 695 | } |
| 696 | |
| 697 | |
| 698 | static ssize_t fp_ip_log_set(struct fastpath_module *m, const char *buf, |
| 699 | size_t count) |
| 700 | { |
| 701 | int flag; |
| 702 | int old_flag; |
| 703 | int num; |
| 704 | int ret; |
| 705 | struct file *filep; |
| 706 | mm_segment_t old_fs; |
| 707 | |
| 708 | sscanf(buf, "%d", &flag); |
| 709 | switch (flag) { |
| 710 | case 0: |
| 711 | fp_ip_log_en = flag; |
| 712 | pr_err("fp_ip_log_set: disable ip_log:fp_ip_log_index=%d to 0\n", |
| 713 | fp_ip_log_index); |
| 714 | fp_ip_log_index = 0; |
| 715 | break; |
| 716 | case 1: |
| 717 | fp_ip_log_index = 0; |
| 718 | sscanf(buf, "%d,%d", &flag, &num); |
| 719 | |
| 720 | if (fp_ip_log_buf == NULL) { |
| 721 | fp_ip_log_buf = kzalloc(ONE_IP_LOG_LEN*num, GFP_KERNEL); |
| 722 | if (fp_ip_log_buf == NULL) |
| 723 | pr_err("fp_ip_log_set: %d,%d,%d, but malloc failed\n", |
| 724 | flag, num, fp_ip_log_index); |
| 725 | else |
| 726 | pr_err("fp_ip_log_set: %d,%d,%d, buf=%x, size=%d\n", |
| 727 | flag, num, fp_ip_log_index, |
| 728 | (unsigned int)fp_ip_log_buf, |
| 729 | num*ONE_IP_LOG_LEN); |
| 730 | } else { |
| 731 | |
| 732 | pr_err(" fp_ip_log_set: buffer has been allocated:%d\n", |
| 733 | fp_ip_log_pkt_num); |
| 734 | } |
| 735 | fp_ip_log_pkt_num = num; |
| 736 | fp_ip_log_en = flag; |
| 737 | break; |
| 738 | |
| 739 | case 2: |
| 740 | old_flag = fp_ip_log_en; |
| 741 | pr_err("fp_ip_log_set: output buf to file(tmp/iplog.txt):\ |
| 742 | old_flag=%d index=%d\n", |
| 743 | old_flag, fp_ip_log_index); |
| 744 | fp_ip_log_en = 2; |
| 745 | /*Don't delete this part of code. It's for reference on data structure |
| 746 | { |
| 747 | char* pex_log_pos; |
| 748 | unsigned int* ptime_h; |
| 749 | unsigned int* ptime_l; |
| 750 | unsigned short* pver; |
| 751 | unsigned short* ppro; |
| 752 | unsigned short* plen; |
| 753 | unsigned int* psadd; |
| 754 | unsigned int* pdadd; |
| 755 | unsigned short* psport; |
| 756 | unsigned short* pdport; |
| 757 | unsigned int* pseq; |
| 758 | unsigned int* pack_seq; |
| 759 | int i; |
| 760 | |
| 761 | for (i = 0; i < 2; i++) { |
| 762 | pex_log_pos = fp_ip_log_buf+i*ONE_IP_LOG_LEN; |
| 763 | ptime_h = (unsigned int*)pex_log_pos; |
| 764 | pex_log_pos +=4; |
| 765 | ptime_l = (unsigned int*)pex_log_pos; |
| 766 | pex_log_pos +=4; |
| 767 | pver = (unsigned short*)pex_log_pos; |
| 768 | pex_log_pos +=2; |
| 769 | ppro = (unsigned short*)pex_log_pos; |
| 770 | pex_log_pos +=2; |
| 771 | plen = (unsigned short*)pex_log_pos; |
| 772 | pex_log_pos +=4; |
| 773 | psadd = (unsigned int*)pex_log_pos; |
| 774 | pex_log_pos += 16; |
| 775 | pdadd = (unsigned int*) pex_log_pos; |
| 776 | pex_log_pos+=16; |
| 777 | psport = (unsigned short*) pex_log_pos; |
| 778 | pex_log_pos +=2; |
| 779 | pdport = (unsigned short*) pex_log_pos; |
| 780 | pex_log_pos+=2; |
| 781 | pseq = (unsigned int*)pex_log_pos; |
| 782 | pex_log_pos +=4; |
| 783 | pack_seq =(unsigned int*)pex_log_pos; |
| 784 | |
| 785 | pr_err("Time:%x %x, ver*pro:%x, pid:%x, len:%x, |
| 786 | sadd:%x, dadd:%x, sport:%x, dport:%x, |
| 787 | seq;%x, ack_seq:%x\n", |
| 788 | *ptime_h, *ptime_l, *pver, *ppro, *plen, |
| 789 | *psadd, *pdadd, *psport, *pdport, |
| 790 | *pseq, *pack_seq); |
| 791 | } |
| 792 | } |
| 793 | */ |
| 794 | filep = filp_open("/tmp/iplog.bin", O_RDWR|O_CREAT, 0644); |
| 795 | if (IS_ERR(filep)) { |
| 796 | pr_err("fp_ip_log_set: fail to open IP log file\n"); |
| 797 | } else { |
| 798 | old_fs = get_fs(); |
| 799 | set_fs(KERNEL_DS); |
| 800 | filep->f_pos = 0; |
| 801 | ret = filep->f_op->write(filep, fp_ip_log_buf, |
| 802 | ONE_IP_LOG_LEN*fp_ip_log_pkt_num, |
| 803 | &filep->f_pos); |
| 804 | set_fs(old_fs); |
| 805 | pr_err("fp_ip_log_set: write to /tmp/iplog.bin, ret=%d\n", |
| 806 | ret); |
| 807 | } |
| 808 | filp_close(filep, NULL); |
| 809 | fp_ip_log_en = old_flag; |
| 810 | break; |
| 811 | case 3: |
| 812 | fp_ip_log_en = flag; |
| 813 | if (fp_ip_log_buf != NULL) { |
| 814 | kfree(fp_ip_log_buf); |
| 815 | pr_err("fp_ip_log_set: free the buffer\n"); |
| 816 | fp_ip_log_buf = NULL; |
| 817 | } else { |
| 818 | pr_err("fp_ip_log_set: buffer is NULL\n"); |
| 819 | } |
| 820 | break; |
| 821 | default: |
| 822 | fp_ip_log_en = flag; |
| 823 | pr_err("fp_ip_log_set: not support this command:\ |
| 824 | %d, but the log will stop\n", flag); |
| 825 | break; |
| 826 | } |
| 827 | return count; |
| 828 | } |
| 829 | |
| 830 | static ssize_t fp_ip_log_get(struct fastpath_module *m, char *buf) |
| 831 | { |
| 832 | int flag = fp_ip_log_en; |
| 833 | int num = fp_ip_log_pkt_num; |
| 834 | return sprintf(buf, "%d,%d buf=%x\n", |
| 835 | flag, |
| 836 | num, |
| 837 | (unsigned int)fp_ip_log_buf); |
| 838 | } |
| 839 | |
| 840 | |
| 841 | static FP_ATTR(fp_acct_flag, S_IRUGO|S_IWUSR, fp_acct_get, fp_acct_set); |
| 842 | static FP_ATTR(fp_ip_log, S_IRUGO|S_IWUSR, fp_ip_log_get, fp_ip_log_set); |
| 843 | static FP_ATTR(stats, S_IRUGO|S_IWUSR, stats_show, stats_clear); |
| 844 | |
| 845 | static struct attribute *fp_classifier_attrs[] = { |
| 846 | &fp_attr_stats.attr, |
| 847 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 3, 0) |
| 848 | &fp_attr_udp_ct_timeout.attr, |
| 849 | &fp_attr_tcp_ct_timeout.attr, |
| 850 | #endif |
| 851 | &fp_attr_fp_acct_flag.attr, |
| 852 | &fp_attr_fp_ip_log.attr, |
| 853 | NULL, /* need to NULL terminate the list of attributes */ |
| 854 | }; |
| 855 | |
| 856 | static void fp_classifier_release(struct kobject *kobj) |
| 857 | { |
| 858 | struct fastpath_module *module = to_fpmod(kobj); |
| 859 | |
| 860 | pr_debug("fp_classifier released\n"); |
| 861 | kfree(module); |
| 862 | } |
| 863 | |
| 864 | static struct kobj_type ktype_classifier = { |
| 865 | .sysfs_ops = &fp_sysfs_ops, |
| 866 | .default_attrs = fp_classifier_attrs, |
| 867 | .release = fp_classifier_release, |
| 868 | }; |
| 869 | |
| 870 | static int fp_classifier_probe(struct fastpath_module *module) |
| 871 | { |
| 872 | int ret; |
| 873 | |
| 874 | module->priv = NULL; |
| 875 | snprintf(module->name, sizeof(module->name), "fp_classifier"); |
| 876 | |
| 877 | kobject_init(&module->kobj, &ktype_classifier); |
| 878 | ret = kobject_add(&module->kobj, module->fastpath->kobj, "%s", module->name); |
| 879 | if (ret < 0) { |
| 880 | pr_err("kobject_add failed (%d)\n", ret); |
| 881 | kobject_put(&module->kobj); |
| 882 | return ret; |
| 883 | } |
| 884 | kobject_uevent(&module->kobj, KOBJ_ADD); |
| 885 | |
| 886 | pr_debug("fp_classifier probed\n"); |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | static int fp_classifier_remove(struct fastpath_module *module) |
| 891 | { |
| 892 | kobject_put(&module->kobj); |
| 893 | |
| 894 | pr_debug("fp_classifier removed\n"); |
| 895 | return 0; |
| 896 | } |
| 897 | |
| 898 | struct fastpath_module_ops fp_classifier_ops = { |
| 899 | .probe = fp_classifier_probe, |
| 900 | .remove = fp_classifier_remove, |
| 901 | }; |
| 902 | |