Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /* (C) 1999-2001 Paul `Rusty' Russell
  2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  3  * (C) 2007 Patrick McHardy <kaber@trash.net>
  4  *
  5  * This program is free software; you can redistribute it and/or modify
  6  * it under the terms of the GNU General Public License version 2 as
  7  * published by the Free Software Foundation.
  8  */
  9 
 10 #include <linux/types.h>
 11 #include <linux/timer.h>
 12 #include <linux/module.h>
 13 #include <linux/udp.h>
 14 #include <linux/seq_file.h>
 15 #include <linux/skbuff.h>
 16 #include <linux/ipv6.h>
 17 #include <net/ip6_checksum.h>
 18 #include <net/checksum.h>
 19 
 20 #include <linux/netfilter.h>
 21 #include <linux/netfilter_ipv4.h>
 22 #include <linux/netfilter_ipv6.h>
 23 #include <net/netfilter/nf_conntrack_l4proto.h>
 24 #include <net/netfilter/nf_conntrack_ecache.h>
 25 #include <net/netfilter/nf_log.h>
 26 
 27 static unsigned int nf_ct_udplite_timeout __read_mostly = 30*HZ;
 28 static unsigned int nf_ct_udplite_timeout_stream __read_mostly = 180*HZ;
 29 
 30 static int udplite_pkt_to_tuple(const struct sk_buff *skb,
 31                                 unsigned int dataoff,
 32                                 struct nf_conntrack_tuple *tuple)
 33 {
 34         const struct udphdr *hp;
 35         struct udphdr _hdr;
 36 
 37         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
 38         if (hp == NULL)
 39                 return 0;
 40 
 41         tuple->src.u.udp.port = hp->source;
 42         tuple->dst.u.udp.port = hp->dest;
 43         return 1;
 44 }
 45 
 46 static int udplite_invert_tuple(struct nf_conntrack_tuple *tuple,
 47                                 const struct nf_conntrack_tuple *orig)
 48 {
 49         tuple->src.u.udp.port = orig->dst.u.udp.port;
 50         tuple->dst.u.udp.port = orig->src.u.udp.port;
 51         return 1;
 52 }
 53 
 54 /* Print out the per-protocol part of the tuple. */
 55 static int udplite_print_tuple(struct seq_file *s,
 56                                const struct nf_conntrack_tuple *tuple)
 57 {
 58         return seq_printf(s, "sport=%hu dport=%hu ",
 59                           ntohs(tuple->src.u.udp.port),
 60                           ntohs(tuple->dst.u.udp.port));
 61 }
 62 
 63 /* Returns verdict for packet, and may modify conntracktype */
 64 static int udplite_packet(struct nf_conn *ct,
 65                           const struct sk_buff *skb,
 66                           unsigned int dataoff,
 67                           enum ip_conntrack_info ctinfo,
 68                           int pf,
 69                           unsigned int hooknum)
 70 {
 71         /* If we've seen traffic both ways, this is some kind of UDP
 72            stream.  Extend timeout. */
 73         if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
 74                 nf_ct_refresh_acct(ct, ctinfo, skb,
 75                                    nf_ct_udplite_timeout_stream);
 76                 /* Also, more likely to be important, and not a probe */
 77                 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
 78                         nf_conntrack_event_cache(IPCT_STATUS, skb);
 79         } else
 80                 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_udplite_timeout);
 81 
 82         return NF_ACCEPT;
 83 }
 84 
 85 /* Called when a new connection for this protocol found. */
 86 static int udplite_new(struct nf_conn *ct, const struct sk_buff *skb,
 87                        unsigned int dataoff)
 88 {
 89         return 1;
 90 }
 91 
 92 static int udplite_error(struct sk_buff *skb, unsigned int dataoff,
 93                          enum ip_conntrack_info *ctinfo,
 94                          int pf,
 95                          unsigned int hooknum)
 96 {
 97         unsigned int udplen = skb->len - dataoff;
 98         const struct udphdr *hdr;
 99         struct udphdr _hdr;
100         unsigned int cscov;
101 
102         /* Header is too small? */
103         hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
104         if (hdr == NULL) {
105                 if (LOG_INVALID(IPPROTO_UDPLITE))
106                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
107                                       "nf_ct_udplite: short packet ");
108                 return -NF_ACCEPT;
109         }
110 
111         cscov = ntohs(hdr->len);
112         if (cscov == 0)
113                 cscov = udplen;
114         else if (cscov < sizeof(*hdr) || cscov > udplen) {
115                 if (LOG_INVALID(IPPROTO_UDPLITE))
116                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
117                                 "nf_ct_udplite: invalid checksum coverage ");
118                 return -NF_ACCEPT;
119         }
120 
121         /* UDPLITE mandates checksums */
122         if (!hdr->check) {
123                 if (LOG_INVALID(IPPROTO_UDPLITE))
124                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
125                                       "nf_ct_udplite: checksum missing ");
126                 return -NF_ACCEPT;
127         }
128 
129         /* Checksum invalid? Ignore. */
130         if (nf_conntrack_checksum && !skb_csum_unnecessary(skb) &&
131             hooknum == NF_INET_PRE_ROUTING) {
132                 if (pf == PF_INET) {
133                         struct iphdr *iph = ip_hdr(skb);
134 
135                         skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
136                                                        udplen, IPPROTO_UDPLITE, 0);
137                 } else {
138                         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
139                         __wsum hsum = skb_checksum(skb, 0, dataoff, 0);
140 
141                         skb->csum = ~csum_unfold(
142                                 csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
143                                                 udplen, IPPROTO_UDPLITE,
144                                                 csum_sub(0, hsum)));
145                 }
146 
147                 skb->ip_summed = CHECKSUM_NONE;
148                 if (__skb_checksum_complete_head(skb, dataoff + cscov)) {
149                         if (LOG_INVALID(IPPROTO_UDPLITE))
150                                 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
151                                               "nf_ct_udplite: bad UDPLite "
152                                               "checksum ");
153                         return -NF_ACCEPT;
154                 }
155                 skb->ip_summed = CHECKSUM_UNNECESSARY;
156         }
157 
158         return NF_ACCEPT;
159 }
160 
161 #ifdef CONFIG_SYSCTL
162 static unsigned int udplite_sysctl_table_users;
163 static struct ctl_table_header *udplite_sysctl_header;
164 static struct ctl_table udplite_sysctl_table[] = {
165         {
166                 .ctl_name       = CTL_UNNUMBERED,
167                 .procname       = "nf_conntrack_udplite_timeout",
168                 .data           = &nf_ct_udplite_timeout,
169                 .maxlen         = sizeof(unsigned int),
170                 .mode           = 0644,
171                 .proc_handler   = &proc_dointvec_jiffies,
172         },
173         {
174                 .ctl_name       = CTL_UNNUMBERED,
175                 .procname       = "nf_conntrack_udplite_timeout_stream",
176                 .data           = &nf_ct_udplite_timeout_stream,
177                 .maxlen         = sizeof(unsigned int),
178                 .mode           = 0644,
179                 .proc_handler   = &proc_dointvec_jiffies,
180         },
181         {
182                 .ctl_name       = 0
183         }
184 };
185 #endif /* CONFIG_SYSCTL */
186 
187 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly =
188 {
189         .l3proto                = PF_INET,
190         .l4proto                = IPPROTO_UDPLITE,
191         .name                   = "udplite",
192         .pkt_to_tuple           = udplite_pkt_to_tuple,
193         .invert_tuple           = udplite_invert_tuple,
194         .print_tuple            = udplite_print_tuple,
195         .packet                 = udplite_packet,
196         .new                    = udplite_new,
197         .error                  = udplite_error,
198 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
199         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
200         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
201         .nla_policy             = nf_ct_port_nla_policy,
202 #endif
203 #ifdef CONFIG_SYSCTL
204         .ctl_table_users        = &udplite_sysctl_table_users,
205         .ctl_table_header       = &udplite_sysctl_header,
206         .ctl_table              = udplite_sysctl_table,
207 #endif
208 };
209 
210 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
211 {
212         .l3proto                = PF_INET6,
213         .l4proto                = IPPROTO_UDPLITE,
214         .name                   = "udplite",
215         .pkt_to_tuple           = udplite_pkt_to_tuple,
216         .invert_tuple           = udplite_invert_tuple,
217         .print_tuple            = udplite_print_tuple,
218         .packet                 = udplite_packet,
219         .new                    = udplite_new,
220         .error                  = udplite_error,
221 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
222         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
223         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
224         .nla_policy             = nf_ct_port_nla_policy,
225 #endif
226 #ifdef CONFIG_SYSCTL
227         .ctl_table_users        = &udplite_sysctl_table_users,
228         .ctl_table_header       = &udplite_sysctl_header,
229         .ctl_table              = udplite_sysctl_table,
230 #endif
231 };
232 
233 static int __init nf_conntrack_proto_udplite_init(void)
234 {
235         int err;
236 
237         err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite4);
238         if (err < 0)
239                 goto err1;
240         err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite6);
241         if (err < 0)
242                 goto err2;
243         return 0;
244 err2:
245         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
246 err1:
247         return err;
248 }
249 
250 static void __exit nf_conntrack_proto_udplite_exit(void)
251 {
252         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite6);
253         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
254 }
255 
256 module_init(nf_conntrack_proto_udplite_init);
257 module_exit(nf_conntrack_proto_udplite_exit);
258 
259 MODULE_LICENSE("GPL");
260 
  This page was automatically generated by the LXR engine.