Blame view
net/netfilter/nf_nat_proto.c
26.6 KB
d2912cb15 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
faec18dbb netfilter: nat: r... |
2 3 |
/* (C) 1999-2001 Paul `Rusty' Russell * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> |
faec18dbb netfilter: nat: r... |
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
*/ #include <linux/types.h> #include <linux/export.h> #include <linux/init.h> #include <linux/udp.h> #include <linux/tcp.h> #include <linux/icmp.h> #include <linux/icmpv6.h> #include <linux/dccp.h> #include <linux/sctp.h> #include <net/sctp/checksum.h> #include <linux/netfilter.h> #include <net/netfilter/nf_nat.h> |
faec18dbb netfilter: nat: r... |
20 |
|
3bf195ae6 netfilter: nat: m... |
21 22 23 24 25 26 27 28 29 30 |
#include <linux/ipv6.h> #include <linux/netfilter_ipv6.h> #include <net/checksum.h> #include <net/ip6_checksum.h> #include <net/ip6_route.h> #include <net/xfrm.h> #include <net/ipv6.h> #include <net/netfilter/nf_conntrack_core.h> #include <net/netfilter/nf_conntrack.h> |
14cb1a6e2 netfilter: nat: r... |
31 |
#include <linux/netfilter/nfnetlink_conntrack.h> |
3bf195ae6 netfilter: nat: m... |
32 |
|
03fe5efc4 netfilter: nat: r... |
33 34 35 36 |
static void nf_csum_update(struct sk_buff *skb, unsigned int iphdroff, __sum16 *check, const struct nf_conntrack_tuple *t, enum nf_nat_manip_type maniptype); |
faec18dbb netfilter: nat: r... |
37 38 |
static void __udp_manip_pkt(struct sk_buff *skb, |
faec18dbb netfilter: nat: r... |
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
unsigned int iphdroff, struct udphdr *hdr, const struct nf_conntrack_tuple *tuple, enum nf_nat_manip_type maniptype, bool do_csum) { __be16 *portptr, newport; if (maniptype == NF_NAT_MANIP_SRC) { /* Get rid of src port */ newport = tuple->src.u.udp.port; portptr = &hdr->source; } else { /* Get rid of dst port */ newport = tuple->dst.u.udp.port; portptr = &hdr->dest; } if (do_csum) { |
03fe5efc4 netfilter: nat: r... |
55 |
nf_csum_update(skb, iphdroff, &hdr->check, tuple, maniptype); |
faec18dbb netfilter: nat: r... |
56 57 58 59 60 61 62 63 64 |
inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport, false); if (!hdr->check) hdr->check = CSUM_MANGLED_0; } *portptr = newport; } static bool udp_manip_pkt(struct sk_buff *skb, |
faec18dbb netfilter: nat: r... |
65 66 67 68 69 |
unsigned int iphdroff, unsigned int hdroff, const struct nf_conntrack_tuple *tuple, enum nf_nat_manip_type maniptype) { struct udphdr *hdr; |
faec18dbb netfilter: nat: r... |
70 |
|
86f045385 netfilter: conntr... |
71 |
if (skb_ensure_writable(skb, hdroff + sizeof(*hdr))) |
faec18dbb netfilter: nat: r... |
72 73 74 |
return false; hdr = (struct udphdr *)(skb->data + hdroff); |
ea64d8d6c netfilter: nat: n... |
75 |
__udp_manip_pkt(skb, iphdroff, hdr, tuple, maniptype, !!hdr->check); |
faec18dbb netfilter: nat: r... |
76 |
|
faec18dbb netfilter: nat: r... |
77 78 79 80 |
return true; } static bool udplite_manip_pkt(struct sk_buff *skb, |
faec18dbb netfilter: nat: r... |
81 82 83 84 85 86 |
unsigned int iphdroff, unsigned int hdroff, const struct nf_conntrack_tuple *tuple, enum nf_nat_manip_type maniptype) { #ifdef CONFIG_NF_CT_PROTO_UDPLITE struct udphdr *hdr; |
86f045385 netfilter: conntr... |
87 |
if (skb_ensure_writable(skb, hdroff + sizeof(*hdr))) |
faec18dbb netfilter: nat: r... |
88 89 90 |
return false; hdr = (struct udphdr *)(skb->data + hdroff); |
03fe5efc4 netfilter: nat: r... |
91 |
__udp_manip_pkt(skb, iphdroff, hdr, tuple, maniptype, true); |
faec18dbb netfilter: nat: r... |
92 93 94 95 96 97 |
#endif return true; } static bool sctp_manip_pkt(struct sk_buff *skb, |
faec18dbb netfilter: nat: r... |
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
unsigned int iphdroff, unsigned int hdroff, const struct nf_conntrack_tuple *tuple, enum nf_nat_manip_type maniptype) { #ifdef CONFIG_NF_CT_PROTO_SCTP struct sctphdr *hdr; int hdrsize = 8; /* This could be an inner header returned in imcp packet; in such * cases we cannot update the checksum field since it is outside * of the 8 bytes of transport layer headers we are guaranteed. */ if (skb->len >= hdroff + sizeof(*hdr)) hdrsize = sizeof(*hdr); |
86f045385 netfilter: conntr... |
112 |
if (skb_ensure_writable(skb, hdroff + hdrsize)) |
faec18dbb netfilter: nat: r... |
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 |
return false; hdr = (struct sctphdr *)(skb->data + hdroff); if (maniptype == NF_NAT_MANIP_SRC) { /* Get rid of src port */ hdr->source = tuple->src.u.sctp.port; } else { /* Get rid of dst port */ hdr->dest = tuple->dst.u.sctp.port; } if (hdrsize < sizeof(*hdr)) return true; if (skb->ip_summed != CHECKSUM_PARTIAL) { hdr->checksum = sctp_compute_cksum(skb, hdroff); skb->ip_summed = CHECKSUM_NONE; } #endif return true; } static bool tcp_manip_pkt(struct sk_buff *skb, |
faec18dbb netfilter: nat: r... |
139 140 141 142 143 144 145 146 147 148 149 150 151 |
unsigned int iphdroff, unsigned int hdroff, const struct nf_conntrack_tuple *tuple, enum nf_nat_manip_type maniptype) { struct tcphdr *hdr; __be16 *portptr, newport, oldport; int hdrsize = 8; /* TCP connection tracking guarantees this much */ /* this could be a inner header returned in icmp packet; in such cases we cannot update the checksum field since it is outside of the 8 bytes of transport layer headers we are guaranteed */ if (skb->len >= hdroff + sizeof(struct tcphdr)) hdrsize = sizeof(struct tcphdr); |
86f045385 netfilter: conntr... |
152 |
if (skb_ensure_writable(skb, hdroff + hdrsize)) |
faec18dbb netfilter: nat: r... |
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
return false; hdr = (struct tcphdr *)(skb->data + hdroff); if (maniptype == NF_NAT_MANIP_SRC) { /* Get rid of src port */ newport = tuple->src.u.tcp.port; portptr = &hdr->source; } else { /* Get rid of dst port */ newport = tuple->dst.u.tcp.port; portptr = &hdr->dest; } oldport = *portptr; *portptr = newport; if (hdrsize < sizeof(*hdr)) return true; |
03fe5efc4 netfilter: nat: r... |
172 |
nf_csum_update(skb, iphdroff, &hdr->check, tuple, maniptype); |
faec18dbb netfilter: nat: r... |
173 174 175 176 177 178 |
inet_proto_csum_replace2(&hdr->check, skb, oldport, newport, false); return true; } static bool dccp_manip_pkt(struct sk_buff *skb, |
faec18dbb netfilter: nat: r... |
179 180 181 182 183 184 185 186 187 188 189 |
unsigned int iphdroff, unsigned int hdroff, const struct nf_conntrack_tuple *tuple, enum nf_nat_manip_type maniptype) { #ifdef CONFIG_NF_CT_PROTO_DCCP struct dccp_hdr *hdr; __be16 *portptr, oldport, newport; int hdrsize = 8; /* DCCP connection tracking guarantees this much */ if (skb->len >= hdroff + sizeof(struct dccp_hdr)) hdrsize = sizeof(struct dccp_hdr); |
86f045385 netfilter: conntr... |
190 |
if (skb_ensure_writable(skb, hdroff + hdrsize)) |
faec18dbb netfilter: nat: r... |
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
return false; hdr = (struct dccp_hdr *)(skb->data + hdroff); if (maniptype == NF_NAT_MANIP_SRC) { newport = tuple->src.u.dccp.port; portptr = &hdr->dccph_sport; } else { newport = tuple->dst.u.dccp.port; portptr = &hdr->dccph_dport; } oldport = *portptr; *portptr = newport; if (hdrsize < sizeof(*hdr)) return true; |
03fe5efc4 netfilter: nat: r... |
208 |
nf_csum_update(skb, iphdroff, &hdr->dccph_checksum, tuple, maniptype); |
faec18dbb netfilter: nat: r... |
209 210 211 212 213 214 215 216 |
inet_proto_csum_replace2(&hdr->dccph_checksum, skb, oldport, newport, false); #endif return true; } static bool icmp_manip_pkt(struct sk_buff *skb, |
faec18dbb netfilter: nat: r... |
217 218 219 220 221 |
unsigned int iphdroff, unsigned int hdroff, const struct nf_conntrack_tuple *tuple, enum nf_nat_manip_type maniptype) { struct icmphdr *hdr; |
86f045385 netfilter: conntr... |
222 |
if (skb_ensure_writable(skb, hdroff + sizeof(*hdr))) |
faec18dbb netfilter: nat: r... |
223 224 225 |
return false; hdr = (struct icmphdr *)(skb->data + hdroff); |
61177e911 netfilter: nat: f... |
226 227 228 229 230 231 232 233 234 235 236 237 238 |
switch (hdr->type) { case ICMP_ECHO: case ICMP_ECHOREPLY: case ICMP_TIMESTAMP: case ICMP_TIMESTAMPREPLY: case ICMP_INFO_REQUEST: case ICMP_INFO_REPLY: case ICMP_ADDRESS: case ICMP_ADDRESSREPLY: break; default: return true; } |
faec18dbb netfilter: nat: r... |
239 240 241 242 243 244 245 246 |
inet_proto_csum_replace2(&hdr->checksum, skb, hdr->un.echo.id, tuple->src.u.icmp.id, false); hdr->un.echo.id = tuple->src.u.icmp.id; return true; } static bool icmpv6_manip_pkt(struct sk_buff *skb, |
faec18dbb netfilter: nat: r... |
247 248 249 250 251 |
unsigned int iphdroff, unsigned int hdroff, const struct nf_conntrack_tuple *tuple, enum nf_nat_manip_type maniptype) { struct icmp6hdr *hdr; |
86f045385 netfilter: conntr... |
252 |
if (skb_ensure_writable(skb, hdroff + sizeof(*hdr))) |
faec18dbb netfilter: nat: r... |
253 254 255 |
return false; hdr = (struct icmp6hdr *)(skb->data + hdroff); |
03fe5efc4 netfilter: nat: r... |
256 |
nf_csum_update(skb, iphdroff, &hdr->icmp6_cksum, tuple, maniptype); |
faec18dbb netfilter: nat: r... |
257 258 259 260 261 262 263 264 265 266 267 268 269 |
if (hdr->icmp6_type == ICMPV6_ECHO_REQUEST || hdr->icmp6_type == ICMPV6_ECHO_REPLY) { inet_proto_csum_replace2(&hdr->icmp6_cksum, skb, hdr->icmp6_identifier, tuple->src.u.icmp.id, false); hdr->icmp6_identifier = tuple->src.u.icmp.id; } return true; } /* manipulate a GRE packet according to maniptype */ static bool gre_manip_pkt(struct sk_buff *skb, |
faec18dbb netfilter: nat: r... |
270 271 272 273 274 275 276 277 278 279 |
unsigned int iphdroff, unsigned int hdroff, const struct nf_conntrack_tuple *tuple, enum nf_nat_manip_type maniptype) { #if IS_ENABLED(CONFIG_NF_CT_PROTO_GRE) const struct gre_base_hdr *greh; struct pptp_gre_header *pgreh; /* pgreh includes two optional 32bit fields which are not required * to be there. That's where the magic '8' comes from */ |
86f045385 netfilter: conntr... |
280 |
if (skb_ensure_writable(skb, hdroff + sizeof(*pgreh) - 8)) |
faec18dbb netfilter: nat: r... |
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
return false; greh = (void *)skb->data + hdroff; pgreh = (struct pptp_gre_header *)greh; /* we only have destination manip of a packet, since 'source key' * is not present in the packet itself */ if (maniptype != NF_NAT_MANIP_DST) return true; switch (greh->flags & GRE_VERSION) { case GRE_VERSION_0: /* We do not currently NAT any GREv0 packets. * Try to behave like "nf_nat_proto_unknown" */ break; case GRE_VERSION_1: pr_debug("call_id -> 0x%04x ", ntohs(tuple->dst.u.gre.key)); pgreh->call_id = tuple->dst.u.gre.key; break; default: pr_debug("can't nat unknown GRE version "); return false; } #endif return true; } |
14cb1a6e2 netfilter: nat: r... |
309 |
static bool l4proto_manip_pkt(struct sk_buff *skb, |
faec18dbb netfilter: nat: r... |
310 311 312 313 314 315 |
unsigned int iphdroff, unsigned int hdroff, const struct nf_conntrack_tuple *tuple, enum nf_nat_manip_type maniptype) { switch (tuple->dst.protonum) { case IPPROTO_TCP: |
03fe5efc4 netfilter: nat: r... |
316 |
return tcp_manip_pkt(skb, iphdroff, hdroff, |
faec18dbb netfilter: nat: r... |
317 318 |
tuple, maniptype); case IPPROTO_UDP: |
03fe5efc4 netfilter: nat: r... |
319 |
return udp_manip_pkt(skb, iphdroff, hdroff, |
faec18dbb netfilter: nat: r... |
320 321 |
tuple, maniptype); case IPPROTO_UDPLITE: |
03fe5efc4 netfilter: nat: r... |
322 |
return udplite_manip_pkt(skb, iphdroff, hdroff, |
faec18dbb netfilter: nat: r... |
323 324 |
tuple, maniptype); case IPPROTO_SCTP: |
03fe5efc4 netfilter: nat: r... |
325 |
return sctp_manip_pkt(skb, iphdroff, hdroff, |
faec18dbb netfilter: nat: r... |
326 327 |
tuple, maniptype); case IPPROTO_ICMP: |
03fe5efc4 netfilter: nat: r... |
328 |
return icmp_manip_pkt(skb, iphdroff, hdroff, |
faec18dbb netfilter: nat: r... |
329 330 |
tuple, maniptype); case IPPROTO_ICMPV6: |
03fe5efc4 netfilter: nat: r... |
331 |
return icmpv6_manip_pkt(skb, iphdroff, hdroff, |
faec18dbb netfilter: nat: r... |
332 333 |
tuple, maniptype); case IPPROTO_DCCP: |
03fe5efc4 netfilter: nat: r... |
334 |
return dccp_manip_pkt(skb, iphdroff, hdroff, |
faec18dbb netfilter: nat: r... |
335 336 |
tuple, maniptype); case IPPROTO_GRE: |
03fe5efc4 netfilter: nat: r... |
337 |
return gre_manip_pkt(skb, iphdroff, hdroff, |
faec18dbb netfilter: nat: r... |
338 339 340 341 342 343 |
tuple, maniptype); } /* If we don't know protocol -- no error, pass it unmodified. */ return true; } |
3bf195ae6 netfilter: nat: m... |
344 345 346 347 348 349 350 351 |
static bool nf_nat_ipv4_manip_pkt(struct sk_buff *skb, unsigned int iphdroff, const struct nf_conntrack_tuple *target, enum nf_nat_manip_type maniptype) { struct iphdr *iph; unsigned int hdroff; |
86f045385 netfilter: conntr... |
352 |
if (skb_ensure_writable(skb, iphdroff + sizeof(*iph))) |
3bf195ae6 netfilter: nat: m... |
353 354 355 356 |
return false; iph = (void *)skb->data + iphdroff; hdroff = iphdroff + iph->ihl * 4; |
03fe5efc4 netfilter: nat: r... |
357 |
if (!l4proto_manip_pkt(skb, iphdroff, hdroff, target, maniptype)) |
3bf195ae6 netfilter: nat: m... |
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
return false; iph = (void *)skb->data + iphdroff; if (maniptype == NF_NAT_MANIP_SRC) { csum_replace4(&iph->check, iph->saddr, target->src.u3.ip); iph->saddr = target->src.u3.ip; } else { csum_replace4(&iph->check, iph->daddr, target->dst.u3.ip); iph->daddr = target->dst.u3.ip; } return true; } static bool nf_nat_ipv6_manip_pkt(struct sk_buff *skb, unsigned int iphdroff, const struct nf_conntrack_tuple *target, enum nf_nat_manip_type maniptype) { #if IS_ENABLED(CONFIG_IPV6) struct ipv6hdr *ipv6h; __be16 frag_off; int hdroff; u8 nexthdr; |
86f045385 netfilter: conntr... |
381 |
if (skb_ensure_writable(skb, iphdroff + sizeof(*ipv6h))) |
3bf195ae6 netfilter: nat: m... |
382 383 384 385 386 387 388 389 390 391 |
return false; ipv6h = (void *)skb->data + iphdroff; nexthdr = ipv6h->nexthdr; hdroff = ipv6_skip_exthdr(skb, iphdroff + sizeof(*ipv6h), &nexthdr, &frag_off); if (hdroff < 0) goto manip_addr; if ((frag_off & htons(~0x7)) == 0 && |
03fe5efc4 netfilter: nat: r... |
392 |
!l4proto_manip_pkt(skb, iphdroff, hdroff, target, maniptype)) |
3bf195ae6 netfilter: nat: m... |
393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
return false; /* must reload, offset might have changed */ ipv6h = (void *)skb->data + iphdroff; manip_addr: if (maniptype == NF_NAT_MANIP_SRC) ipv6h->saddr = target->src.u3.in6; else ipv6h->daddr = target->dst.u3.in6; #endif return true; } |
2e666b229 netfilter: nat: r... |
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
unsigned int nf_nat_manip_pkt(struct sk_buff *skb, struct nf_conn *ct, enum nf_nat_manip_type mtype, enum ip_conntrack_dir dir) { struct nf_conntrack_tuple target; /* We are aiming to look like inverse of other direction. */ nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple); switch (target.src.l3num) { case NFPROTO_IPV6: if (nf_nat_ipv6_manip_pkt(skb, 0, &target, mtype)) return NF_ACCEPT; break; case NFPROTO_IPV4: if (nf_nat_ipv4_manip_pkt(skb, 0, &target, mtype)) return NF_ACCEPT; break; default: WARN_ON_ONCE(1); break; } return NF_DROP; } |
3bf195ae6 netfilter: nat: m... |
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
static void nf_nat_ipv4_csum_update(struct sk_buff *skb, unsigned int iphdroff, __sum16 *check, const struct nf_conntrack_tuple *t, enum nf_nat_manip_type maniptype) { struct iphdr *iph = (struct iphdr *)(skb->data + iphdroff); __be32 oldip, newip; if (maniptype == NF_NAT_MANIP_SRC) { oldip = iph->saddr; newip = t->src.u3.ip; } else { oldip = iph->daddr; newip = t->dst.u3.ip; } inet_proto_csum_replace4(check, skb, oldip, newip, true); } static void nf_nat_ipv6_csum_update(struct sk_buff *skb, unsigned int iphdroff, __sum16 *check, const struct nf_conntrack_tuple *t, enum nf_nat_manip_type maniptype) { #if IS_ENABLED(CONFIG_IPV6) const struct ipv6hdr *ipv6h = (struct ipv6hdr *)(skb->data + iphdroff); const struct in6_addr *oldip, *newip; if (maniptype == NF_NAT_MANIP_SRC) { oldip = &ipv6h->saddr; newip = &t->src.u3.in6; } else { oldip = &ipv6h->daddr; newip = &t->dst.u3.in6; } inet_proto_csum_replace16(check, skb, oldip->s6_addr32, newip->s6_addr32, true); #endif } |
03fe5efc4 netfilter: nat: r... |
470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
static void nf_csum_update(struct sk_buff *skb, unsigned int iphdroff, __sum16 *check, const struct nf_conntrack_tuple *t, enum nf_nat_manip_type maniptype) { switch (t->src.l3num) { case NFPROTO_IPV4: nf_nat_ipv4_csum_update(skb, iphdroff, check, t, maniptype); return; case NFPROTO_IPV6: nf_nat_ipv6_csum_update(skb, iphdroff, check, t, maniptype); return; } } |
3bf195ae6 netfilter: nat: m... |
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
static void nf_nat_ipv4_csum_recalc(struct sk_buff *skb, u8 proto, void *data, __sum16 *check, int datalen, int oldlen) { if (skb->ip_summed != CHECKSUM_PARTIAL) { const struct iphdr *iph = ip_hdr(skb); skb->ip_summed = CHECKSUM_PARTIAL; skb->csum_start = skb_headroom(skb) + skb_network_offset(skb) + ip_hdrlen(skb); skb->csum_offset = (void *)check - data; *check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, datalen, proto, 0); } else { inet_proto_csum_replace2(check, skb, htons(oldlen), htons(datalen), true); } } #if IS_ENABLED(CONFIG_IPV6) static void nf_nat_ipv6_csum_recalc(struct sk_buff *skb, u8 proto, void *data, __sum16 *check, int datalen, int oldlen) { if (skb->ip_summed != CHECKSUM_PARTIAL) { const struct ipv6hdr *ipv6h = ipv6_hdr(skb); skb->ip_summed = CHECKSUM_PARTIAL; skb->csum_start = skb_headroom(skb) + skb_network_offset(skb) + (data - (void *)skb->data); skb->csum_offset = (void *)check - data; *check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, datalen, proto, 0); } else { inet_proto_csum_replace2(check, skb, htons(oldlen), htons(datalen), true); } } #endif |
dac3fe725 netfilter: nat: r... |
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
void nf_nat_csum_recalc(struct sk_buff *skb, u8 nfproto, u8 proto, void *data, __sum16 *check, int datalen, int oldlen) { switch (nfproto) { case NFPROTO_IPV4: nf_nat_ipv4_csum_recalc(skb, proto, data, check, datalen, oldlen); return; #if IS_ENABLED(CONFIG_IPV6) case NFPROTO_IPV6: nf_nat_ipv6_csum_recalc(skb, proto, data, check, datalen, oldlen); return; #endif } WARN_ON_ONCE(1); } |
3bf195ae6 netfilter: nat: m... |
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
int nf_nat_icmp_reply_translation(struct sk_buff *skb, struct nf_conn *ct, enum ip_conntrack_info ctinfo, unsigned int hooknum) { struct { struct icmphdr icmp; struct iphdr ip; } *inside; enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); enum nf_nat_manip_type manip = HOOK2MANIP(hooknum); unsigned int hdrlen = ip_hdrlen(skb); struct nf_conntrack_tuple target; unsigned long statusbit; WARN_ON(ctinfo != IP_CT_RELATED && ctinfo != IP_CT_RELATED_REPLY); |
86f045385 netfilter: conntr... |
558 |
if (skb_ensure_writable(skb, hdrlen + sizeof(*inside))) |
3bf195ae6 netfilter: nat: m... |
559 |
return 0; |
5d1549847 netfilter: Fix re... |
560 |
if (nf_ip_checksum(skb, hooknum, hdrlen, IPPROTO_ICMP)) |
3bf195ae6 netfilter: nat: m... |
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 |
return 0; inside = (void *)skb->data + hdrlen; if (inside->icmp.type == ICMP_REDIRECT) { if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK) return 0; if (ct->status & IPS_NAT_MASK) return 0; } if (manip == NF_NAT_MANIP_SRC) statusbit = IPS_SRC_NAT; else statusbit = IPS_DST_NAT; /* Invert if this is reply direction */ if (dir == IP_CT_DIR_REPLY) statusbit ^= IPS_NAT_MASK; if (!(ct->status & statusbit)) return 1; if (!nf_nat_ipv4_manip_pkt(skb, hdrlen + sizeof(inside->icmp), &ct->tuplehash[!dir].tuple, !manip)) return 0; if (skb->ip_summed != CHECKSUM_PARTIAL) { /* Reloading "inside" here since manip_pkt may reallocate */ inside = (void *)skb->data + hdrlen; inside->icmp.checksum = 0; inside->icmp.checksum = csum_fold(skb_checksum(skb, hdrlen, skb->len - hdrlen, 0)); } /* Change outer to look like the reply to an incoming packet */ nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple); target.dst.protonum = IPPROTO_ICMP; if (!nf_nat_ipv4_manip_pkt(skb, 0, &target, manip)) return 0; return 1; } EXPORT_SYMBOL_GPL(nf_nat_icmp_reply_translation); static unsigned int nf_nat_ipv4_fn(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { struct nf_conn *ct; enum ip_conntrack_info ctinfo; ct = nf_ct_get(skb, &ctinfo); if (!ct) return NF_ACCEPT; if (ctinfo == IP_CT_RELATED || ctinfo == IP_CT_RELATED_REPLY) { if (ip_hdr(skb)->protocol == IPPROTO_ICMP) { if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo, state->hook)) return NF_DROP; else return NF_ACCEPT; } } return nf_nat_inet_fn(priv, skb, state); } static unsigned int nf_nat_ipv4_in(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { unsigned int ret; __be32 daddr = ip_hdr(skb)->daddr; ret = nf_nat_ipv4_fn(priv, skb, state); if (ret == NF_ACCEPT && daddr != ip_hdr(skb)->daddr) skb_dst_drop(skb); return ret; } static unsigned int nf_nat_ipv4_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { #ifdef CONFIG_XFRM const struct nf_conn *ct; enum ip_conntrack_info ctinfo; int err; #endif unsigned int ret; ret = nf_nat_ipv4_fn(priv, skb, state); #ifdef CONFIG_XFRM if (ret != NF_ACCEPT) return ret; if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) return ret; ct = nf_ct_get(skb, &ctinfo); if (ct) { enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); if (ct->tuplehash[dir].tuple.src.u3.ip != ct->tuplehash[!dir].tuple.dst.u3.ip || (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP && ct->tuplehash[dir].tuple.src.u.all != ct->tuplehash[!dir].tuple.dst.u.all)) { err = nf_xfrm_me_harder(state->net, skb, AF_INET); if (err < 0) ret = NF_DROP_ERR(err); } } #endif return ret; } static unsigned int nf_nat_ipv4_local_fn(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { const struct nf_conn *ct; enum ip_conntrack_info ctinfo; unsigned int ret; int err; ret = nf_nat_ipv4_fn(priv, skb, state); if (ret != NF_ACCEPT) return ret; ct = nf_ct_get(skb, &ctinfo); if (ct) { enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); if (ct->tuplehash[dir].tuple.dst.u3.ip != ct->tuplehash[!dir].tuple.src.u3.ip) { |
46d6c5ae9 netfilter: use ac... |
700 |
err = ip_route_me_harder(state->net, state->sk, skb, RTN_UNSPEC); |
3bf195ae6 netfilter: nat: m... |
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
if (err < 0) ret = NF_DROP_ERR(err); } #ifdef CONFIG_XFRM else if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) && ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP && ct->tuplehash[dir].tuple.dst.u.all != ct->tuplehash[!dir].tuple.src.u.all) { err = nf_xfrm_me_harder(state->net, skb, AF_INET); if (err < 0) ret = NF_DROP_ERR(err); } #endif } return ret; } |
0a30ba509 netfilter: nf_nat... |
717 |
static const struct nf_hook_ops nf_nat_ipv4_ops[] = { |
3bf195ae6 netfilter: nat: m... |
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 |
/* Before packet filtering, change destination */ { .hook = nf_nat_ipv4_in, .pf = NFPROTO_IPV4, .hooknum = NF_INET_PRE_ROUTING, .priority = NF_IP_PRI_NAT_DST, }, /* After packet filtering, change source */ { .hook = nf_nat_ipv4_out, .pf = NFPROTO_IPV4, .hooknum = NF_INET_POST_ROUTING, .priority = NF_IP_PRI_NAT_SRC, }, /* Before packet filtering, change destination */ { .hook = nf_nat_ipv4_local_fn, .pf = NFPROTO_IPV4, .hooknum = NF_INET_LOCAL_OUT, .priority = NF_IP_PRI_NAT_DST, }, /* After packet filtering, change source */ { .hook = nf_nat_ipv4_fn, .pf = NFPROTO_IPV4, .hooknum = NF_INET_LOCAL_IN, .priority = NF_IP_PRI_NAT_SRC, }, }; |
d2c5c103b netfilter: nat: r... |
747 |
int nf_nat_ipv4_register_fn(struct net *net, const struct nf_hook_ops *ops) |
3bf195ae6 netfilter: nat: m... |
748 |
{ |
d164385ec netfilter: nat: a... |
749 750 |
return nf_nat_register_fn(net, ops->pf, ops, nf_nat_ipv4_ops, ARRAY_SIZE(nf_nat_ipv4_ops)); |
3bf195ae6 netfilter: nat: m... |
751 |
} |
d2c5c103b netfilter: nat: r... |
752 |
EXPORT_SYMBOL_GPL(nf_nat_ipv4_register_fn); |
3bf195ae6 netfilter: nat: m... |
753 |
|
d2c5c103b netfilter: nat: r... |
754 |
void nf_nat_ipv4_unregister_fn(struct net *net, const struct nf_hook_ops *ops) |
3bf195ae6 netfilter: nat: m... |
755 |
{ |
d164385ec netfilter: nat: a... |
756 |
nf_nat_unregister_fn(net, ops->pf, ops, ARRAY_SIZE(nf_nat_ipv4_ops)); |
3bf195ae6 netfilter: nat: m... |
757 |
} |
d2c5c103b netfilter: nat: r... |
758 |
EXPORT_SYMBOL_GPL(nf_nat_ipv4_unregister_fn); |
3bf195ae6 netfilter: nat: m... |
759 |
|
3bf195ae6 netfilter: nat: m... |
760 |
#if IS_ENABLED(CONFIG_IPV6) |
3bf195ae6 netfilter: nat: m... |
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 |
int nf_nat_icmpv6_reply_translation(struct sk_buff *skb, struct nf_conn *ct, enum ip_conntrack_info ctinfo, unsigned int hooknum, unsigned int hdrlen) { struct { struct icmp6hdr icmp6; struct ipv6hdr ip6; } *inside; enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); enum nf_nat_manip_type manip = HOOK2MANIP(hooknum); struct nf_conntrack_tuple target; unsigned long statusbit; WARN_ON(ctinfo != IP_CT_RELATED && ctinfo != IP_CT_RELATED_REPLY); |
86f045385 netfilter: conntr... |
777 |
if (skb_ensure_writable(skb, hdrlen + sizeof(*inside))) |
3bf195ae6 netfilter: nat: m... |
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 |
return 0; if (nf_ip6_checksum(skb, hooknum, hdrlen, IPPROTO_ICMPV6)) return 0; inside = (void *)skb->data + hdrlen; if (inside->icmp6.icmp6_type == NDISC_REDIRECT) { if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK) return 0; if (ct->status & IPS_NAT_MASK) return 0; } if (manip == NF_NAT_MANIP_SRC) statusbit = IPS_SRC_NAT; else statusbit = IPS_DST_NAT; /* Invert if this is reply direction */ if (dir == IP_CT_DIR_REPLY) statusbit ^= IPS_NAT_MASK; if (!(ct->status & statusbit)) return 1; if (!nf_nat_ipv6_manip_pkt(skb, hdrlen + sizeof(inside->icmp6), &ct->tuplehash[!dir].tuple, !manip)) return 0; if (skb->ip_summed != CHECKSUM_PARTIAL) { struct ipv6hdr *ipv6h = ipv6_hdr(skb); inside = (void *)skb->data + hdrlen; inside->icmp6.icmp6_cksum = 0; inside->icmp6.icmp6_cksum = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len - hdrlen, IPPROTO_ICMPV6, skb_checksum(skb, hdrlen, skb->len - hdrlen, 0)); } nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple); target.dst.protonum = IPPROTO_ICMPV6; if (!nf_nat_ipv6_manip_pkt(skb, 0, &target, manip)) return 0; return 1; } EXPORT_SYMBOL_GPL(nf_nat_icmpv6_reply_translation); static unsigned int nf_nat_ipv6_fn(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { struct nf_conn *ct; enum ip_conntrack_info ctinfo; __be16 frag_off; int hdrlen; u8 nexthdr; ct = nf_ct_get(skb, &ctinfo); /* Can't track? It's not due to stress, or conntrack would * have dropped it. Hence it's the user's responsibilty to * packet filter it out, or implement conntrack/NAT for that * protocol. 8) --RR */ if (!ct) return NF_ACCEPT; if (ctinfo == IP_CT_RELATED || ctinfo == IP_CT_RELATED_REPLY) { nexthdr = ipv6_hdr(skb)->nexthdr; hdrlen = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr, &frag_off); if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) { if (!nf_nat_icmpv6_reply_translation(skb, ct, ctinfo, state->hook, hdrlen)) return NF_DROP; else return NF_ACCEPT; } } return nf_nat_inet_fn(priv, skb, state); } static unsigned int nf_nat_ipv6_in(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { unsigned int ret; struct in6_addr daddr = ipv6_hdr(skb)->daddr; ret = nf_nat_ipv6_fn(priv, skb, state); if (ret != NF_DROP && ret != NF_STOLEN && ipv6_addr_cmp(&daddr, &ipv6_hdr(skb)->daddr)) skb_dst_drop(skb); return ret; } static unsigned int nf_nat_ipv6_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { #ifdef CONFIG_XFRM const struct nf_conn *ct; enum ip_conntrack_info ctinfo; int err; #endif unsigned int ret; ret = nf_nat_ipv6_fn(priv, skb, state); #ifdef CONFIG_XFRM if (ret != NF_ACCEPT) return ret; if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) return ret; ct = nf_ct_get(skb, &ctinfo); if (ct) { enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, &ct->tuplehash[!dir].tuple.dst.u3) || (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 && ct->tuplehash[dir].tuple.src.u.all != ct->tuplehash[!dir].tuple.dst.u.all)) { err = nf_xfrm_me_harder(state->net, skb, AF_INET6); if (err < 0) ret = NF_DROP_ERR(err); } } #endif return ret; } |
3bf195ae6 netfilter: nat: m... |
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 |
static unsigned int nf_nat_ipv6_local_fn(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { const struct nf_conn *ct; enum ip_conntrack_info ctinfo; unsigned int ret; int err; ret = nf_nat_ipv6_fn(priv, skb, state); if (ret != NF_ACCEPT) return ret; ct = nf_ct_get(skb, &ctinfo); if (ct) { enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, &ct->tuplehash[!dir].tuple.src.u3)) { |
46d6c5ae9 netfilter: use ac... |
934 |
err = nf_ip6_route_me_harder(state->net, state->sk, skb); |
3bf195ae6 netfilter: nat: m... |
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 |
if (err < 0) ret = NF_DROP_ERR(err); } #ifdef CONFIG_XFRM else if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) && ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 && ct->tuplehash[dir].tuple.dst.u.all != ct->tuplehash[!dir].tuple.src.u.all) { err = nf_xfrm_me_harder(state->net, skb, AF_INET6); if (err < 0) ret = NF_DROP_ERR(err); } #endif } return ret; } |
0a30ba509 netfilter: nf_nat... |
952 |
static const struct nf_hook_ops nf_nat_ipv6_ops[] = { |
3bf195ae6 netfilter: nat: m... |
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 |
/* Before packet filtering, change destination */ { .hook = nf_nat_ipv6_in, .pf = NFPROTO_IPV6, .hooknum = NF_INET_PRE_ROUTING, .priority = NF_IP6_PRI_NAT_DST, }, /* After packet filtering, change source */ { .hook = nf_nat_ipv6_out, .pf = NFPROTO_IPV6, .hooknum = NF_INET_POST_ROUTING, .priority = NF_IP6_PRI_NAT_SRC, }, /* Before packet filtering, change destination */ { .hook = nf_nat_ipv6_local_fn, .pf = NFPROTO_IPV6, .hooknum = NF_INET_LOCAL_OUT, .priority = NF_IP6_PRI_NAT_DST, }, /* After packet filtering, change source */ { .hook = nf_nat_ipv6_fn, .pf = NFPROTO_IPV6, .hooknum = NF_INET_LOCAL_IN, .priority = NF_IP6_PRI_NAT_SRC, }, }; |
d2c5c103b netfilter: nat: r... |
982 |
int nf_nat_ipv6_register_fn(struct net *net, const struct nf_hook_ops *ops) |
3bf195ae6 netfilter: nat: m... |
983 |
{ |
d164385ec netfilter: nat: a... |
984 |
return nf_nat_register_fn(net, ops->pf, ops, nf_nat_ipv6_ops, |
3bf195ae6 netfilter: nat: m... |
985 986 |
ARRAY_SIZE(nf_nat_ipv6_ops)); } |
d2c5c103b netfilter: nat: r... |
987 |
EXPORT_SYMBOL_GPL(nf_nat_ipv6_register_fn); |
3bf195ae6 netfilter: nat: m... |
988 |
|
d2c5c103b netfilter: nat: r... |
989 |
void nf_nat_ipv6_unregister_fn(struct net *net, const struct nf_hook_ops *ops) |
3bf195ae6 netfilter: nat: m... |
990 |
{ |
d164385ec netfilter: nat: a... |
991 |
nf_nat_unregister_fn(net, ops->pf, ops, ARRAY_SIZE(nf_nat_ipv6_ops)); |
3bf195ae6 netfilter: nat: m... |
992 |
} |
d2c5c103b netfilter: nat: r... |
993 |
EXPORT_SYMBOL_GPL(nf_nat_ipv6_unregister_fn); |
3bf195ae6 netfilter: nat: m... |
994 |
#endif /* CONFIG_IPV6 */ |
d164385ec netfilter: nat: a... |
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 |
#if defined(CONFIG_NF_TABLES_INET) && IS_ENABLED(CONFIG_NFT_NAT) int nf_nat_inet_register_fn(struct net *net, const struct nf_hook_ops *ops) { int ret; if (WARN_ON_ONCE(ops->pf != NFPROTO_INET)) return -EINVAL; ret = nf_nat_register_fn(net, NFPROTO_IPV6, ops, nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops)); if (ret) return ret; ret = nf_nat_register_fn(net, NFPROTO_IPV4, ops, nf_nat_ipv4_ops, ARRAY_SIZE(nf_nat_ipv4_ops)); if (ret) |
b4faef173 netfilter: nat: f... |
1012 1013 |
nf_nat_unregister_fn(net, NFPROTO_IPV6, ops, ARRAY_SIZE(nf_nat_ipv6_ops)); |
d164385ec netfilter: nat: a... |
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 |
return ret; } EXPORT_SYMBOL_GPL(nf_nat_inet_register_fn); void nf_nat_inet_unregister_fn(struct net *net, const struct nf_hook_ops *ops) { nf_nat_unregister_fn(net, NFPROTO_IPV4, ops, ARRAY_SIZE(nf_nat_ipv4_ops)); nf_nat_unregister_fn(net, NFPROTO_IPV6, ops, ARRAY_SIZE(nf_nat_ipv6_ops)); } EXPORT_SYMBOL_GPL(nf_nat_inet_unregister_fn); #endif /* NFT INET NAT */ |