dma-debug: update DMA debug API to better handle multiple mappings of a buffer
[linux-2.6/cjktty.git] / net / ipv6 / ip6_offload.c
blob8234c1dcdf7286f23c32bc150279cffaeb818ef4
1 /*
2 * IPV6 GSO/GRO offload support
3 * Linux INET6 implementation
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 */
11 #include <linux/kernel.h>
12 #include <linux/socket.h>
13 #include <linux/netdevice.h>
14 #include <linux/skbuff.h>
15 #include <linux/printk.h>
17 #include <net/protocol.h>
18 #include <net/ipv6.h>
20 #include "ip6_offload.h"
22 static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
24 const struct net_offload *ops = NULL;
26 for (;;) {
27 struct ipv6_opt_hdr *opth;
28 int len;
30 if (proto != NEXTHDR_HOP) {
31 ops = rcu_dereference(inet6_offloads[proto]);
33 if (unlikely(!ops))
34 break;
36 if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
37 break;
40 if (unlikely(!pskb_may_pull(skb, 8)))
41 break;
43 opth = (void *)skb->data;
44 len = ipv6_optlen(opth);
46 if (unlikely(!pskb_may_pull(skb, len)))
47 break;
49 proto = opth->nexthdr;
50 __skb_pull(skb, len);
53 return proto;
56 static int ipv6_gso_send_check(struct sk_buff *skb)
58 const struct ipv6hdr *ipv6h;
59 const struct net_offload *ops;
60 int err = -EINVAL;
62 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
63 goto out;
65 ipv6h = ipv6_hdr(skb);
66 __skb_pull(skb, sizeof(*ipv6h));
67 err = -EPROTONOSUPPORT;
69 rcu_read_lock();
70 ops = rcu_dereference(inet6_offloads[
71 ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
73 if (likely(ops && ops->callbacks.gso_send_check)) {
74 skb_reset_transport_header(skb);
75 err = ops->callbacks.gso_send_check(skb);
77 rcu_read_unlock();
79 out:
80 return err;
83 static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
84 netdev_features_t features)
86 struct sk_buff *segs = ERR_PTR(-EINVAL);
87 struct ipv6hdr *ipv6h;
88 const struct net_offload *ops;
89 int proto;
90 struct frag_hdr *fptr;
91 unsigned int unfrag_ip6hlen;
92 u8 *prevhdr;
93 int offset = 0;
95 if (!(features & NETIF_F_V6_CSUM))
96 features &= ~NETIF_F_SG;
98 if (unlikely(skb_shinfo(skb)->gso_type &
99 ~(SKB_GSO_UDP |
100 SKB_GSO_DODGY |
101 SKB_GSO_TCP_ECN |
102 SKB_GSO_GRE |
103 SKB_GSO_TCPV6 |
104 0)))
105 goto out;
107 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
108 goto out;
110 ipv6h = ipv6_hdr(skb);
111 __skb_pull(skb, sizeof(*ipv6h));
112 segs = ERR_PTR(-EPROTONOSUPPORT);
114 proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
115 rcu_read_lock();
116 ops = rcu_dereference(inet6_offloads[proto]);
117 if (likely(ops && ops->callbacks.gso_segment)) {
118 skb_reset_transport_header(skb);
119 segs = ops->callbacks.gso_segment(skb, features);
121 rcu_read_unlock();
123 if (IS_ERR(segs))
124 goto out;
126 for (skb = segs; skb; skb = skb->next) {
127 ipv6h = ipv6_hdr(skb);
128 ipv6h->payload_len = htons(skb->len - skb->mac_len -
129 sizeof(*ipv6h));
130 if (proto == IPPROTO_UDP) {
131 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
132 fptr = (struct frag_hdr *)(skb_network_header(skb) +
133 unfrag_ip6hlen);
134 fptr->frag_off = htons(offset);
135 if (skb->next != NULL)
136 fptr->frag_off |= htons(IP6_MF);
137 offset += (ntohs(ipv6h->payload_len) -
138 sizeof(struct frag_hdr));
142 out:
143 return segs;
146 static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
147 struct sk_buff *skb)
149 const struct net_offload *ops;
150 struct sk_buff **pp = NULL;
151 struct sk_buff *p;
152 struct ipv6hdr *iph;
153 unsigned int nlen;
154 unsigned int hlen;
155 unsigned int off;
156 int flush = 1;
157 int proto;
158 __wsum csum;
160 off = skb_gro_offset(skb);
161 hlen = off + sizeof(*iph);
162 iph = skb_gro_header_fast(skb, off);
163 if (skb_gro_header_hard(skb, hlen)) {
164 iph = skb_gro_header_slow(skb, hlen, off);
165 if (unlikely(!iph))
166 goto out;
169 skb_gro_pull(skb, sizeof(*iph));
170 skb_set_transport_header(skb, skb_gro_offset(skb));
172 flush += ntohs(iph->payload_len) != skb_gro_len(skb);
174 rcu_read_lock();
175 proto = iph->nexthdr;
176 ops = rcu_dereference(inet6_offloads[proto]);
177 if (!ops || !ops->callbacks.gro_receive) {
178 __pskb_pull(skb, skb_gro_offset(skb));
179 proto = ipv6_gso_pull_exthdrs(skb, proto);
180 skb_gro_pull(skb, -skb_transport_offset(skb));
181 skb_reset_transport_header(skb);
182 __skb_push(skb, skb_gro_offset(skb));
184 ops = rcu_dereference(inet6_offloads[proto]);
185 if (!ops || !ops->callbacks.gro_receive)
186 goto out_unlock;
188 iph = ipv6_hdr(skb);
191 NAPI_GRO_CB(skb)->proto = proto;
193 flush--;
194 nlen = skb_network_header_len(skb);
196 for (p = *head; p; p = p->next) {
197 const struct ipv6hdr *iph2;
198 __be32 first_word; /* <Version:4><Traffic_Class:8><Flow_Label:20> */
200 if (!NAPI_GRO_CB(p)->same_flow)
201 continue;
203 iph2 = ipv6_hdr(p);
204 first_word = *(__be32 *)iph ^ *(__be32 *)iph2 ;
206 /* All fields must match except length and Traffic Class. */
207 if (nlen != skb_network_header_len(p) ||
208 (first_word & htonl(0xF00FFFFF)) ||
209 memcmp(&iph->nexthdr, &iph2->nexthdr,
210 nlen - offsetof(struct ipv6hdr, nexthdr))) {
211 NAPI_GRO_CB(p)->same_flow = 0;
212 continue;
214 /* flush if Traffic Class fields are different */
215 NAPI_GRO_CB(p)->flush |= !!(first_word & htonl(0x0FF00000));
216 NAPI_GRO_CB(p)->flush |= flush;
219 NAPI_GRO_CB(skb)->flush |= flush;
221 csum = skb->csum;
222 skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
224 pp = ops->callbacks.gro_receive(head, skb);
226 skb->csum = csum;
228 out_unlock:
229 rcu_read_unlock();
231 out:
232 NAPI_GRO_CB(skb)->flush |= flush;
234 return pp;
237 static int ipv6_gro_complete(struct sk_buff *skb)
239 const struct net_offload *ops;
240 struct ipv6hdr *iph = ipv6_hdr(skb);
241 int err = -ENOSYS;
243 iph->payload_len = htons(skb->len - skb_network_offset(skb) -
244 sizeof(*iph));
246 rcu_read_lock();
247 ops = rcu_dereference(inet6_offloads[NAPI_GRO_CB(skb)->proto]);
248 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
249 goto out_unlock;
251 err = ops->callbacks.gro_complete(skb);
253 out_unlock:
254 rcu_read_unlock();
256 return err;
259 static struct packet_offload ipv6_packet_offload __read_mostly = {
260 .type = cpu_to_be16(ETH_P_IPV6),
261 .callbacks = {
262 .gso_send_check = ipv6_gso_send_check,
263 .gso_segment = ipv6_gso_segment,
264 .gro_receive = ipv6_gro_receive,
265 .gro_complete = ipv6_gro_complete,
269 static int __init ipv6_offload_init(void)
272 if (tcpv6_offload_init() < 0)
273 pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
274 if (udp_offload_init() < 0)
275 pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
276 if (ipv6_exthdrs_offload_init() < 0)
277 pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
279 dev_add_offload(&ipv6_packet_offload);
280 return 0;
283 fs_initcall(ipv6_offload_init);