ipvs: convert lblc scheduler to rcu
[linux-3.10.git] / net / netfilter / nf_nat_proto_sctp.c
1 /*
2  * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include <linux/types.h>
10 #include <linux/init.h>
11 #include <linux/sctp.h>
12 #include <linux/module.h>
13 #include <net/sctp/checksum.h>
14
15 #include <net/netfilter/nf_nat_l4proto.h>
16
17 static u_int16_t nf_sctp_port_rover;
18
19 static void
20 sctp_unique_tuple(const struct nf_nat_l3proto *l3proto,
21                   struct nf_conntrack_tuple *tuple,
22                   const struct nf_nat_range *range,
23                   enum nf_nat_manip_type maniptype,
24                   const struct nf_conn *ct)
25 {
26         nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct,
27                                     &nf_sctp_port_rover);
28 }
29
30 static bool
31 sctp_manip_pkt(struct sk_buff *skb,
32                const struct nf_nat_l3proto *l3proto,
33                unsigned int iphdroff, unsigned int hdroff,
34                const struct nf_conntrack_tuple *tuple,
35                enum nf_nat_manip_type maniptype)
36 {
37         struct sk_buff *frag;
38         sctp_sctphdr_t *hdr;
39         __be32 crc32;
40
41         if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
42                 return false;
43
44         hdr = (struct sctphdr *)(skb->data + hdroff);
45
46         if (maniptype == NF_NAT_MANIP_SRC) {
47                 /* Get rid of src port */
48                 hdr->source = tuple->src.u.sctp.port;
49         } else {
50                 /* Get rid of dst port */
51                 hdr->dest = tuple->dst.u.sctp.port;
52         }
53
54         crc32 = sctp_start_cksum((u8 *)hdr, skb_headlen(skb) - hdroff);
55         skb_walk_frags(skb, frag)
56                 crc32 = sctp_update_cksum((u8 *)frag->data, skb_headlen(frag),
57                                           crc32);
58         crc32 = sctp_end_cksum(crc32);
59         hdr->checksum = crc32;
60
61         return true;
62 }
63
64 static const struct nf_nat_l4proto nf_nat_l4proto_sctp = {
65         .l4proto                = IPPROTO_SCTP,
66         .manip_pkt              = sctp_manip_pkt,
67         .in_range               = nf_nat_l4proto_in_range,
68         .unique_tuple           = sctp_unique_tuple,
69 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
70         .nlattr_to_range        = nf_nat_l4proto_nlattr_to_range,
71 #endif
72 };
73
74 static int __init nf_nat_proto_sctp_init(void)
75 {
76         int err;
77
78         err = nf_nat_l4proto_register(NFPROTO_IPV4, &nf_nat_l4proto_sctp);
79         if (err < 0)
80                 goto err1;
81         err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_sctp);
82         if (err < 0)
83                 goto err2;
84         return 0;
85
86 err2:
87         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_sctp);
88 err1:
89         return err;
90 }
91
92 static void __exit nf_nat_proto_sctp_exit(void)
93 {
94         nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_sctp);
95         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_sctp);
96 }
97
98 module_init(nf_nat_proto_sctp_init);
99 module_exit(nf_nat_proto_sctp_exit);
100
101 MODULE_LICENSE("GPL");
102 MODULE_DESCRIPTION("SCTP NAT protocol helper");
103 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");