]>
Commit | Line | Data |
---|---|---|
4910a087 PM |
1 | /* |
2 | * DCCP NAT protocol helper | |
3 | * | |
c7232c99 | 4 | * Copyright (c) 2005, 2006, 2008 Patrick McHardy <kaber@trash.net> |
4910a087 PM |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | */ | |
11 | ||
12 | #include <linux/kernel.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/skbuff.h> | |
4910a087 PM |
16 | #include <linux/dccp.h> |
17 | ||
18 | #include <net/netfilter/nf_conntrack.h> | |
19 | #include <net/netfilter/nf_nat.h> | |
c7232c99 PM |
20 | #include <net/netfilter/nf_nat_l3proto.h> |
21 | #include <net/netfilter/nf_nat_l4proto.h> | |
4910a087 PM |
22 | |
23 | static u_int16_t dccp_port_rover; | |
24 | ||
f43dc98b | 25 | static void |
c7232c99 PM |
26 | dccp_unique_tuple(const struct nf_nat_l3proto *l3proto, |
27 | struct nf_conntrack_tuple *tuple, | |
28 | const struct nf_nat_range *range, | |
4910a087 PM |
29 | enum nf_nat_manip_type maniptype, |
30 | const struct nf_conn *ct) | |
31 | { | |
c7232c99 PM |
32 | nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct, |
33 | &dccp_port_rover); | |
4910a087 PM |
34 | } |
35 | ||
f2ea825f | 36 | static bool |
4910a087 | 37 | dccp_manip_pkt(struct sk_buff *skb, |
c7232c99 PM |
38 | const struct nf_nat_l3proto *l3proto, |
39 | unsigned int iphdroff, unsigned int hdroff, | |
4910a087 PM |
40 | const struct nf_conntrack_tuple *tuple, |
41 | enum nf_nat_manip_type maniptype) | |
42 | { | |
4910a087 | 43 | struct dccp_hdr *hdr; |
4910a087 PM |
44 | __be16 *portptr, oldport, newport; |
45 | int hdrsize = 8; /* DCCP connection tracking guarantees this much */ | |
46 | ||
47 | if (skb->len >= hdroff + sizeof(struct dccp_hdr)) | |
48 | hdrsize = sizeof(struct dccp_hdr); | |
49 | ||
50 | if (!skb_make_writable(skb, hdroff + hdrsize)) | |
f2ea825f | 51 | return false; |
4910a087 | 52 | |
4910a087 PM |
53 | hdr = (struct dccp_hdr *)(skb->data + hdroff); |
54 | ||
cbc9f2f4 | 55 | if (maniptype == NF_NAT_MANIP_SRC) { |
4910a087 PM |
56 | newport = tuple->src.u.dccp.port; |
57 | portptr = &hdr->dccph_sport; | |
58 | } else { | |
4910a087 PM |
59 | newport = tuple->dst.u.dccp.port; |
60 | portptr = &hdr->dccph_dport; | |
61 | } | |
62 | ||
63 | oldport = *portptr; | |
64 | *portptr = newport; | |
65 | ||
66 | if (hdrsize < sizeof(*hdr)) | |
f2ea825f | 67 | return true; |
4910a087 | 68 | |
c7232c99 PM |
69 | l3proto->csum_update(skb, iphdroff, &hdr->dccph_checksum, |
70 | tuple, maniptype); | |
4910a087 | 71 | inet_proto_csum_replace2(&hdr->dccph_checksum, skb, oldport, newport, |
4b048d6d | 72 | false); |
f2ea825f | 73 | return true; |
4910a087 PM |
74 | } |
75 | ||
c7232c99 PM |
76 | static const struct nf_nat_l4proto nf_nat_l4proto_dccp = { |
77 | .l4proto = IPPROTO_DCCP, | |
4910a087 | 78 | .manip_pkt = dccp_manip_pkt, |
c7232c99 | 79 | .in_range = nf_nat_l4proto_in_range, |
4910a087 | 80 | .unique_tuple = dccp_unique_tuple, |
24de3d37 | 81 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
c7232c99 | 82 | .nlattr_to_range = nf_nat_l4proto_nlattr_to_range, |
4910a087 PM |
83 | #endif |
84 | }; | |
85 | ||
86 | static int __init nf_nat_proto_dccp_init(void) | |
87 | { | |
c7232c99 PM |
88 | int err; |
89 | ||
90 | err = nf_nat_l4proto_register(NFPROTO_IPV4, &nf_nat_l4proto_dccp); | |
91 | if (err < 0) | |
92 | goto err1; | |
93 | err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_dccp); | |
94 | if (err < 0) | |
95 | goto err2; | |
96 | return 0; | |
97 | ||
98 | err2: | |
99 | nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_dccp); | |
100 | err1: | |
101 | return err; | |
4910a087 PM |
102 | } |
103 | ||
104 | static void __exit nf_nat_proto_dccp_fini(void) | |
105 | { | |
c7232c99 PM |
106 | nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_dccp); |
107 | nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_dccp); | |
108 | ||
4910a087 PM |
109 | } |
110 | ||
111 | module_init(nf_nat_proto_dccp_init); | |
112 | module_exit(nf_nat_proto_dccp_fini); | |
113 | ||
114 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
115 | MODULE_DESCRIPTION("DCCP NAT protocol helper"); | |
116 | MODULE_LICENSE("GPL"); |