Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * DCCP NAT protocol helper |
| 3 | * |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 4 | * Copyright (c) 2005, 2006, 2008 Patrick McHardy <kaber@trash.net> |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 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> |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 16 | #include <linux/dccp.h> |
| 17 | |
| 18 | #include <net/netfilter/nf_conntrack.h> |
| 19 | #include <net/netfilter/nf_nat.h> |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 20 | #include <net/netfilter/nf_nat_l3proto.h> |
| 21 | #include <net/netfilter/nf_nat_l4proto.h> |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 22 | |
| 23 | static u_int16_t dccp_port_rover; |
| 24 | |
Changli Gao | f43dc98 | 2010-08-02 17:20:54 +0200 | [diff] [blame] | 25 | static void |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 26 | dccp_unique_tuple(const struct nf_nat_l3proto *l3proto, |
| 27 | struct nf_conntrack_tuple *tuple, |
| 28 | const struct nf_nat_range *range, |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 29 | enum nf_nat_manip_type maniptype, |
| 30 | const struct nf_conn *ct) |
| 31 | { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 32 | nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct, |
| 33 | &dccp_port_rover); |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 34 | } |
| 35 | |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 36 | static bool |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 37 | dccp_manip_pkt(struct sk_buff *skb, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 38 | const struct nf_nat_l3proto *l3proto, |
| 39 | unsigned int iphdroff, unsigned int hdroff, |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 40 | const struct nf_conntrack_tuple *tuple, |
| 41 | enum nf_nat_manip_type maniptype) |
| 42 | { |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 43 | struct dccp_hdr *hdr; |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 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)) |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 51 | return false; |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 52 | |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 53 | hdr = (struct dccp_hdr *)(skb->data + hdroff); |
| 54 | |
Patrick McHardy | cbc9f2f | 2011-12-23 13:59:49 +0100 | [diff] [blame] | 55 | if (maniptype == NF_NAT_MANIP_SRC) { |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 56 | newport = tuple->src.u.dccp.port; |
| 57 | portptr = &hdr->dccph_sport; |
| 58 | } else { |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 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)) |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 67 | return true; |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 68 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 69 | l3proto->csum_update(skb, iphdroff, &hdr->dccph_checksum, |
| 70 | tuple, maniptype); |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 71 | inet_proto_csum_replace2(&hdr->dccph_checksum, skb, oldport, newport, |
Tom Herbert | 4b048d6 | 2015-08-17 13:42:25 -0700 | [diff] [blame^] | 72 | false); |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 73 | return true; |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 76 | static const struct nf_nat_l4proto nf_nat_l4proto_dccp = { |
| 77 | .l4proto = IPPROTO_DCCP, |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 78 | .manip_pkt = dccp_manip_pkt, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 79 | .in_range = nf_nat_l4proto_in_range, |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 80 | .unique_tuple = dccp_unique_tuple, |
Duan Jiong | 24de3d3 | 2014-06-30 09:19:32 +0800 | [diff] [blame] | 81 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 82 | .nlattr_to_range = nf_nat_l4proto_nlattr_to_range, |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 83 | #endif |
| 84 | }; |
| 85 | |
| 86 | static int __init nf_nat_proto_dccp_init(void) |
| 87 | { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 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; |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void __exit nf_nat_proto_dccp_fini(void) |
| 105 | { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 106 | nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_dccp); |
| 107 | nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_dccp); |
| 108 | |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 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"); |