blob: b2be749d22172d540d85093ea4d059cc9f289a70 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 output functions
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 *
8 * $Id: ip6_output.c,v 1.34 2002/02/01 22:01:04 davem Exp $
9 *
10 * Based on linux/net/ipv4/ip_output.c
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 * Changes:
18 * A.N.Kuznetsov : airthmetics in fragmentation.
19 * extension headers are implemented.
20 * route changes now work.
21 * ip6_forward does not confuse sniffers.
22 * etc.
23 *
24 * H. von Brand : Added missing #include <linux/string.h>
25 * Imran Patel : frag id should be in NBO
26 * Kazunori MIYAZAWA @USAGI
27 * : add ip6_append_data and related functions
28 * for datagram xmit
29 */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
32#include <linux/types.h>
33#include <linux/string.h>
34#include <linux/socket.h>
35#include <linux/net.h>
36#include <linux/netdevice.h>
37#include <linux/if_arp.h>
38#include <linux/in6.h>
39#include <linux/tcp.h>
40#include <linux/route.h>
Herbert Xub59f45d2006-05-27 23:05:54 -070041#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <linux/netfilter.h>
44#include <linux/netfilter_ipv6.h>
45
46#include <net/sock.h>
47#include <net/snmp.h>
48
49#include <net/ipv6.h>
50#include <net/ndisc.h>
51#include <net/protocol.h>
52#include <net/ip6_route.h>
53#include <net/addrconf.h>
54#include <net/rawv6.h>
55#include <net/icmp.h>
56#include <net/xfrm.h>
57#include <net/checksum.h>
58
59static int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *));
60
61static __inline__ void ipv6_select_ident(struct sk_buff *skb, struct frag_hdr *fhdr)
62{
63 static u32 ipv6_fragmentation_id = 1;
64 static DEFINE_SPINLOCK(ip6_id_lock);
65
66 spin_lock_bh(&ip6_id_lock);
67 fhdr->identification = htonl(ipv6_fragmentation_id);
68 if (++ipv6_fragmentation_id == 0)
69 ipv6_fragmentation_id = 1;
70 spin_unlock_bh(&ip6_id_lock);
71}
72
73static inline int ip6_output_finish(struct sk_buff *skb)
74{
75
76 struct dst_entry *dst = skb->dst;
77 struct hh_cache *hh = dst->hh;
78
79 if (hh) {
80 int hh_alen;
81
82 read_lock_bh(&hh->hh_lock);
83 hh_alen = HH_DATA_ALIGN(hh->hh_len);
84 memcpy(skb->data - hh_alen, hh->hh_data, hh_alen);
85 read_unlock_bh(&hh->hh_lock);
86 skb_push(skb, hh->hh_len);
87 return hh->hh_output(skb);
88 } else if (dst->neighbour)
89 return dst->neighbour->output(skb);
90
91 IP6_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
92 kfree_skb(skb);
93 return -EINVAL;
94
95}
96
97/* dev_loopback_xmit for use with netfilter. */
98static int ip6_dev_loopback_xmit(struct sk_buff *newskb)
99{
100 newskb->mac.raw = newskb->data;
101 __skb_pull(newskb, newskb->nh.raw - newskb->data);
102 newskb->pkt_type = PACKET_LOOPBACK;
103 newskb->ip_summed = CHECKSUM_UNNECESSARY;
104 BUG_TRAP(newskb->dst);
105
106 netif_rx(newskb);
107 return 0;
108}
109
110
111static int ip6_output2(struct sk_buff *skb)
112{
113 struct dst_entry *dst = skb->dst;
114 struct net_device *dev = dst->dev;
115
116 skb->protocol = htons(ETH_P_IPV6);
117 skb->dev = dev;
118
119 if (ipv6_addr_is_multicast(&skb->nh.ipv6h->daddr)) {
120 struct ipv6_pinfo* np = skb->sk ? inet6_sk(skb->sk) : NULL;
121
122 if (!(dev->flags & IFF_LOOPBACK) && (!np || np->mc_loop) &&
123 ipv6_chk_mcast_addr(dev, &skb->nh.ipv6h->daddr,
124 &skb->nh.ipv6h->saddr)) {
125 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
126
127 /* Do not check for IFF_ALLMULTI; multicast routing
128 is not supported in any case.
129 */
130 if (newskb)
131 NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, newskb, NULL,
132 newskb->dev,
133 ip6_dev_loopback_xmit);
134
135 if (skb->nh.ipv6h->hop_limit == 0) {
136 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
137 kfree_skb(skb);
138 return 0;
139 }
140 }
141
142 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
143 }
144
145 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb,NULL, skb->dev,ip6_output_finish);
146}
147
148int ip6_output(struct sk_buff *skb)
149{
Herbert Xu89114af2006-07-08 13:34:32 -0700150 if ((skb->len > dst_mtu(skb->dst) && !skb_is_gso(skb)) ||
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700151 dst_allfrag(skb->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return ip6_fragment(skb, ip6_output2);
153 else
154 return ip6_output2(skb);
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
158 * xmit an sk_buff (used by TCP)
159 */
160
161int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl,
162 struct ipv6_txoptions *opt, int ipfragok)
163{
Patrick McHardyb30bd282006-03-23 01:17:25 -0800164 struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 struct in6_addr *first_hop = &fl->fl6_dst;
166 struct dst_entry *dst = skb->dst;
167 struct ipv6hdr *hdr;
168 u8 proto = fl->proto;
169 int seg_len = skb->len;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900170 int hlimit, tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 u32 mtu;
172
173 if (opt) {
174 int head_room;
175
176 /* First: exthdrs may take lots of space (~8K for now)
177 MAX_HEADER is not enough.
178 */
179 head_room = opt->opt_nflen + opt->opt_flen;
180 seg_len += head_room;
181 head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
182
183 if (skb_headroom(skb) < head_room) {
184 struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
185 kfree_skb(skb);
186 skb = skb2;
187 if (skb == NULL) {
188 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
189 return -ENOBUFS;
190 }
191 if (sk)
192 skb_set_owner_w(skb, sk);
193 }
194 if (opt->opt_flen)
195 ipv6_push_frag_opts(skb, opt, &proto);
196 if (opt->opt_nflen)
197 ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
198 }
199
200 hdr = skb->nh.ipv6h = (struct ipv6hdr*)skb_push(skb, sizeof(struct ipv6hdr));
201
202 /*
203 * Fill in the IPv6 header
204 */
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 hlimit = -1;
207 if (np)
208 hlimit = np->hop_limit;
209 if (hlimit < 0)
210 hlimit = dst_metric(dst, RTAX_HOPLIMIT);
211 if (hlimit < 0)
212 hlimit = ipv6_get_hoplimit(dst->dev);
213
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900214 tclass = -1;
215 if (np)
216 tclass = np->tclass;
217 if (tclass < 0)
218 tclass = 0;
219
220 *(u32 *)hdr = htonl(0x60000000 | (tclass << 20)) | fl->fl6_flowlabel;
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 hdr->payload_len = htons(seg_len);
223 hdr->nexthdr = proto;
224 hdr->hop_limit = hlimit;
225
226 ipv6_addr_copy(&hdr->saddr, &fl->fl6_src);
227 ipv6_addr_copy(&hdr->daddr, first_hop);
228
Patrick McHardya2c20642006-01-08 22:37:26 -0800229 skb->priority = sk->sk_priority;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 mtu = dst_mtu(dst);
Herbert Xu89114af2006-07-08 13:34:32 -0700232 if ((skb->len <= mtu) || ipfragok || skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
Harald Welte6869c4d2005-08-09 19:24:19 -0700234 return NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, dst->dev,
235 dst_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
238 if (net_ratelimit())
239 printk(KERN_DEBUG "IPv6: sending pkt_too_big to self\n");
240 skb->dev = dst->dev;
241 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
242 IP6_INC_STATS(IPSTATS_MIB_FRAGFAILS);
243 kfree_skb(skb);
244 return -EMSGSIZE;
245}
246
247/*
248 * To avoid extra problems ND packets are send through this
249 * routine. It's code duplication but I really want to avoid
250 * extra checks since ipv6_build_header is used by TCP (which
251 * is for us performance critical)
252 */
253
254int ip6_nd_hdr(struct sock *sk, struct sk_buff *skb, struct net_device *dev,
255 struct in6_addr *saddr, struct in6_addr *daddr,
256 int proto, int len)
257{
258 struct ipv6_pinfo *np = inet6_sk(sk);
259 struct ipv6hdr *hdr;
260 int totlen;
261
262 skb->protocol = htons(ETH_P_IPV6);
263 skb->dev = dev;
264
265 totlen = len + sizeof(struct ipv6hdr);
266
267 hdr = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
268 skb->nh.ipv6h = hdr;
269
270 *(u32*)hdr = htonl(0x60000000);
271
272 hdr->payload_len = htons(len);
273 hdr->nexthdr = proto;
274 hdr->hop_limit = np->hop_limit;
275
276 ipv6_addr_copy(&hdr->saddr, saddr);
277 ipv6_addr_copy(&hdr->daddr, daddr);
278
279 return 0;
280}
281
282static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
283{
284 struct ip6_ra_chain *ra;
285 struct sock *last = NULL;
286
287 read_lock(&ip6_ra_lock);
288 for (ra = ip6_ra_chain; ra; ra = ra->next) {
289 struct sock *sk = ra->sk;
Andrew McDonald0bd1b592005-08-09 19:44:42 -0700290 if (sk && ra->sel == sel &&
291 (!sk->sk_bound_dev_if ||
292 sk->sk_bound_dev_if == skb->dev->ifindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (last) {
294 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
295 if (skb2)
296 rawv6_rcv(last, skb2);
297 }
298 last = sk;
299 }
300 }
301
302 if (last) {
303 rawv6_rcv(last, skb);
304 read_unlock(&ip6_ra_lock);
305 return 1;
306 }
307 read_unlock(&ip6_ra_lock);
308 return 0;
309}
310
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700311static int ip6_forward_proxy_check(struct sk_buff *skb)
312{
313 struct ipv6hdr *hdr = skb->nh.ipv6h;
314 u8 nexthdr = hdr->nexthdr;
315 int offset;
316
317 if (ipv6_ext_hdr(nexthdr)) {
318 offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr);
319 if (offset < 0)
320 return 0;
321 } else
322 offset = sizeof(struct ipv6hdr);
323
324 if (nexthdr == IPPROTO_ICMPV6) {
325 struct icmp6hdr *icmp6;
326
327 if (!pskb_may_pull(skb, skb->nh.raw + offset + 1 - skb->data))
328 return 0;
329
330 icmp6 = (struct icmp6hdr *)(skb->nh.raw + offset);
331
332 switch (icmp6->icmp6_type) {
333 case NDISC_ROUTER_SOLICITATION:
334 case NDISC_ROUTER_ADVERTISEMENT:
335 case NDISC_NEIGHBOUR_SOLICITATION:
336 case NDISC_NEIGHBOUR_ADVERTISEMENT:
337 case NDISC_REDIRECT:
338 /* For reaction involving unicast neighbor discovery
339 * message destined to the proxied address, pass it to
340 * input function.
341 */
342 return 1;
343 default:
344 break;
345 }
346 }
347
Ville Nuorvala74553b02006-09-22 14:42:18 -0700348 /*
349 * The proxying router can't forward traffic sent to a link-local
350 * address, so signal the sender and discard the packet. This
351 * behavior is clarified by the MIPv6 specification.
352 */
353 if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
354 dst_link_failure(skb);
355 return -1;
356 }
357
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700358 return 0;
359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static inline int ip6_forward_finish(struct sk_buff *skb)
362{
363 return dst_output(skb);
364}
365
366int ip6_forward(struct sk_buff *skb)
367{
368 struct dst_entry *dst = skb->dst;
369 struct ipv6hdr *hdr = skb->nh.ipv6h;
370 struct inet6_skb_parm *opt = IP6CB(skb);
371
372 if (ipv6_devconf.forwarding == 0)
373 goto error;
374
375 if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
376 IP6_INC_STATS(IPSTATS_MIB_INDISCARDS);
377 goto drop;
378 }
379
380 skb->ip_summed = CHECKSUM_NONE;
381
382 /*
383 * We DO NOT make any processing on
384 * RA packets, pushing them to user level AS IS
385 * without ane WARRANTY that application will be able
386 * to interpret them. The reason is that we
387 * cannot make anything clever here.
388 *
389 * We are not end-node, so that if packet contains
390 * AH/ESP, we cannot make anything.
391 * Defragmentation also would be mistake, RA packets
392 * cannot be fragmented, because there is no warranty
393 * that different fragments will go along one path. --ANK
394 */
395 if (opt->ra) {
396 u8 *ptr = skb->nh.raw + opt->ra;
397 if (ip6_call_ra_chain(skb, (ptr[2]<<8) + ptr[3]))
398 return 0;
399 }
400
401 /*
402 * check and decrement ttl
403 */
404 if (hdr->hop_limit <= 1) {
405 /* Force OUTPUT device used as source address */
406 skb->dev = dst->dev;
407 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
408 0, skb->dev);
Wei Dong32c524d2006-08-02 13:39:57 -0700409 IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 kfree_skb(skb);
412 return -ETIMEDOUT;
413 }
414
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700415 if (pneigh_lookup(&nd_tbl, &hdr->daddr, skb->dev, 0)) {
Ville Nuorvala74553b02006-09-22 14:42:18 -0700416 int proxied = ip6_forward_proxy_check(skb);
417 if (proxied > 0)
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700418 return ip6_input(skb);
Ville Nuorvala74553b02006-09-22 14:42:18 -0700419 else if (proxied < 0) {
420 IP6_INC_STATS(IPSTATS_MIB_INDISCARDS);
421 goto drop;
422 }
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700423 }
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (!xfrm6_route_forward(skb)) {
426 IP6_INC_STATS(IPSTATS_MIB_INDISCARDS);
427 goto drop;
428 }
429 dst = skb->dst;
430
431 /* IPv6 specs say nothing about it, but it is clear that we cannot
432 send redirects to source routed frames.
433 */
434 if (skb->dev == dst->dev && dst->neighbour && opt->srcrt == 0) {
435 struct in6_addr *target = NULL;
436 struct rt6_info *rt;
437 struct neighbour *n = dst->neighbour;
438
439 /*
440 * incoming and outgoing devices are the same
441 * send a redirect.
442 */
443
444 rt = (struct rt6_info *) dst;
445 if ((rt->rt6i_flags & RTF_GATEWAY))
446 target = (struct in6_addr*)&n->primary_key;
447 else
448 target = &hdr->daddr;
449
450 /* Limit redirects both by destination (here)
451 and by source (inside ndisc_send_redirect)
452 */
453 if (xrlim_allow(dst, 1*HZ))
454 ndisc_send_redirect(skb, n, target);
455 } else if (ipv6_addr_type(&hdr->saddr)&(IPV6_ADDR_MULTICAST|IPV6_ADDR_LOOPBACK
456 |IPV6_ADDR_LINKLOCAL)) {
457 /* This check is security critical. */
458 goto error;
459 }
460
461 if (skb->len > dst_mtu(dst)) {
462 /* Again, force OUTPUT device used as source address */
463 skb->dev = dst->dev;
464 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, dst_mtu(dst), skb->dev);
465 IP6_INC_STATS_BH(IPSTATS_MIB_INTOOBIGERRORS);
466 IP6_INC_STATS_BH(IPSTATS_MIB_FRAGFAILS);
467 kfree_skb(skb);
468 return -EMSGSIZE;
469 }
470
471 if (skb_cow(skb, dst->dev->hard_header_len)) {
472 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
473 goto drop;
474 }
475
476 hdr = skb->nh.ipv6h;
477
478 /* Mangling hops number delayed to point after skb COW */
479
480 hdr->hop_limit--;
481
482 IP6_INC_STATS_BH(IPSTATS_MIB_OUTFORWDATAGRAMS);
483 return NF_HOOK(PF_INET6,NF_IP6_FORWARD, skb, skb->dev, dst->dev, ip6_forward_finish);
484
485error:
486 IP6_INC_STATS_BH(IPSTATS_MIB_INADDRERRORS);
487drop:
488 kfree_skb(skb);
489 return -EINVAL;
490}
491
492static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
493{
494 to->pkt_type = from->pkt_type;
495 to->priority = from->priority;
496 to->protocol = from->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 dst_release(to->dst);
498 to->dst = dst_clone(from->dst);
499 to->dev = from->dev;
500
501#ifdef CONFIG_NET_SCHED
502 to->tc_index = from->tc_index;
503#endif
504#ifdef CONFIG_NETFILTER
505 to->nfmark = from->nfmark;
506 /* Connection association is same as pre-frag packet */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800507 nf_conntrack_put(to->nfct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 to->nfct = from->nfct;
509 nf_conntrack_get(to->nfct);
510 to->nfctinfo = from->nfctinfo;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800511#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
512 nf_conntrack_put_reasm(to->nfct_reasm);
513 to->nfct_reasm = from->nfct_reasm;
514 nf_conntrack_get_reasm(to->nfct_reasm);
515#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516#ifdef CONFIG_BRIDGE_NETFILTER
517 nf_bridge_put(to->nf_bridge);
518 to->nf_bridge = from->nf_bridge;
519 nf_bridge_get(to->nf_bridge);
520#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521#endif
James Morris984bc162006-06-09 00:29:17 -0700522 skb_copy_secmark(to, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
525int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
526{
527 u16 offset = sizeof(struct ipv6hdr);
528 struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
529 unsigned int packet_len = skb->tail - skb->nh.raw;
530 int found_rhdr = 0;
531 *nexthdr = &skb->nh.ipv6h->nexthdr;
532
533 while (offset + 1 <= packet_len) {
534
535 switch (**nexthdr) {
536
537 case NEXTHDR_HOP:
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700538 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 case NEXTHDR_ROUTING:
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700540 found_rhdr = 1;
541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 case NEXTHDR_DEST:
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700543#ifdef CONFIG_IPV6_MIP6
544 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
545 break;
546#endif
547 if (found_rhdr)
548 return offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 break;
550 default :
551 return offset;
552 }
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700553
554 offset += ipv6_optlen(exthdr);
555 *nexthdr = &exthdr->nexthdr;
556 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
559 return offset;
560}
Herbert Xub59f45d2006-05-27 23:05:54 -0700561EXPORT_SYMBOL_GPL(ip6_find_1stfragopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563static int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
564{
565 struct net_device *dev;
566 struct sk_buff *frag;
567 struct rt6_info *rt = (struct rt6_info*)skb->dst;
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -0800568 struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct ipv6hdr *tmp_hdr;
570 struct frag_hdr *fh;
571 unsigned int mtu, hlen, left, len;
572 u32 frag_id = 0;
573 int ptr, offset = 0, err=0;
574 u8 *prevhdr, nexthdr = 0;
575
576 dev = rt->u.dst.dev;
577 hlen = ip6_find_1stfragopt(skb, &prevhdr);
578 nexthdr = *prevhdr;
579
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -0800580 mtu = dst_mtu(&rt->u.dst);
581 if (np && np->frag_size < mtu) {
582 if (np->frag_size)
583 mtu = np->frag_size;
584 }
585 mtu -= hlen + sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (skb_shinfo(skb)->frag_list) {
588 int first_len = skb_pagelen(skb);
589
590 if (first_len - hlen > mtu ||
591 ((first_len - hlen) & 7) ||
592 skb_cloned(skb))
593 goto slow_path;
594
595 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
596 /* Correct geometry. */
597 if (frag->len > mtu ||
598 ((frag->len & 7) && frag->next) ||
599 skb_headroom(frag) < hlen)
600 goto slow_path;
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* Partially cloned skb? */
603 if (skb_shared(frag))
604 goto slow_path;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700605
606 BUG_ON(frag->sk);
607 if (skb->sk) {
608 sock_hold(skb->sk);
609 frag->sk = skb->sk;
610 frag->destructor = sock_wfree;
611 skb->truesize -= frag->truesize;
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
615 err = 0;
616 offset = 0;
617 frag = skb_shinfo(skb)->frag_list;
618 skb_shinfo(skb)->frag_list = NULL;
619 /* BUILD HEADER */
620
621 tmp_hdr = kmalloc(hlen, GFP_ATOMIC);
622 if (!tmp_hdr) {
623 IP6_INC_STATS(IPSTATS_MIB_FRAGFAILS);
624 return -ENOMEM;
625 }
626
627 *prevhdr = NEXTHDR_FRAGMENT;
628 memcpy(tmp_hdr, skb->nh.raw, hlen);
629 __skb_pull(skb, hlen);
630 fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
631 skb->nh.raw = __skb_push(skb, hlen);
632 memcpy(skb->nh.raw, tmp_hdr, hlen);
633
634 ipv6_select_ident(skb, fh);
635 fh->nexthdr = nexthdr;
636 fh->reserved = 0;
637 fh->frag_off = htons(IP6_MF);
638 frag_id = fh->identification;
639
640 first_len = skb_pagelen(skb);
641 skb->data_len = first_len - skb_headlen(skb);
642 skb->len = first_len;
643 skb->nh.ipv6h->payload_len = htons(first_len - sizeof(struct ipv6hdr));
644
645
646 for (;;) {
647 /* Prepare header of the next frame,
648 * before previous one went down. */
649 if (frag) {
650 frag->ip_summed = CHECKSUM_NONE;
651 frag->h.raw = frag->data;
652 fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr));
653 frag->nh.raw = __skb_push(frag, hlen);
654 memcpy(frag->nh.raw, tmp_hdr, hlen);
655 offset += skb->len - hlen - sizeof(struct frag_hdr);
656 fh->nexthdr = nexthdr;
657 fh->reserved = 0;
658 fh->frag_off = htons(offset);
659 if (frag->next != NULL)
660 fh->frag_off |= htons(IP6_MF);
661 fh->identification = frag_id;
662 frag->nh.ipv6h->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
663 ip6_copy_metadata(frag, skb);
664 }
665
666 err = output(skb);
Wei Dongdafee492006-08-02 13:41:21 -0700667 if(!err)
668 IP6_INC_STATS(IPSTATS_MIB_FRAGCREATES);
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (err || !frag)
671 break;
672
673 skb = frag;
674 frag = skb->next;
675 skb->next = NULL;
676 }
677
Jesper Juhla51482b2005-11-08 09:41:34 -0800678 kfree(tmp_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 if (err == 0) {
681 IP6_INC_STATS(IPSTATS_MIB_FRAGOKS);
682 return 0;
683 }
684
685 while (frag) {
686 skb = frag->next;
687 kfree_skb(frag);
688 frag = skb;
689 }
690
691 IP6_INC_STATS(IPSTATS_MIB_FRAGFAILS);
692 return err;
693 }
694
695slow_path:
696 left = skb->len - hlen; /* Space per frame */
697 ptr = hlen; /* Where to start from */
698
699 /*
700 * Fragment the datagram.
701 */
702
703 *prevhdr = NEXTHDR_FRAGMENT;
704
705 /*
706 * Keep copying data until we run out.
707 */
708 while(left > 0) {
709 len = left;
710 /* IF: it doesn't fit, use 'mtu' - the data space left */
711 if (len > mtu)
712 len = mtu;
713 /* IF: we are not sending upto and including the packet end
714 then align the next start on an eight byte boundary */
715 if (len < left) {
716 len &= ~7;
717 }
718 /*
719 * Allocate buffer.
720 */
721
722 if ((frag = alloc_skb(len+hlen+sizeof(struct frag_hdr)+LL_RESERVED_SPACE(rt->u.dst.dev), GFP_ATOMIC)) == NULL) {
Patrick McHardy64ce2072005-08-09 20:50:53 -0700723 NETDEBUG(KERN_INFO "IPv6: frag: no memory for new fragment!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 IP6_INC_STATS(IPSTATS_MIB_FRAGFAILS);
725 err = -ENOMEM;
726 goto fail;
727 }
728
729 /*
730 * Set up data on packet
731 */
732
733 ip6_copy_metadata(frag, skb);
734 skb_reserve(frag, LL_RESERVED_SPACE(rt->u.dst.dev));
735 skb_put(frag, len + hlen + sizeof(struct frag_hdr));
736 frag->nh.raw = frag->data;
737 fh = (struct frag_hdr*)(frag->data + hlen);
738 frag->h.raw = frag->data + hlen + sizeof(struct frag_hdr);
739
740 /*
741 * Charge the memory for the fragment to any owner
742 * it might possess
743 */
744 if (skb->sk)
745 skb_set_owner_w(frag, skb->sk);
746
747 /*
748 * Copy the packet header into the new buffer.
749 */
750 memcpy(frag->nh.raw, skb->data, hlen);
751
752 /*
753 * Build fragment header.
754 */
755 fh->nexthdr = nexthdr;
756 fh->reserved = 0;
Yan Zhengf36d6ab2005-10-03 14:19:15 -0700757 if (!frag_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 ipv6_select_ident(skb, fh);
759 frag_id = fh->identification;
760 } else
761 fh->identification = frag_id;
762
763 /*
764 * Copy a block of the IP datagram.
765 */
766 if (skb_copy_bits(skb, ptr, frag->h.raw, len))
767 BUG();
768 left -= len;
769
770 fh->frag_off = htons(offset);
771 if (left > 0)
772 fh->frag_off |= htons(IP6_MF);
773 frag->nh.ipv6h->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
774
775 ptr += len;
776 offset += len;
777
778 /*
779 * Put this fragment into the sending queue.
780 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 err = output(frag);
782 if (err)
783 goto fail;
Wei Dongdafee492006-08-02 13:41:21 -0700784
785 IP6_INC_STATS(IPSTATS_MIB_FRAGCREATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787 kfree_skb(skb);
788 IP6_INC_STATS(IPSTATS_MIB_FRAGOKS);
789 return err;
790
791fail:
792 kfree_skb(skb);
793 IP6_INC_STATS(IPSTATS_MIB_FRAGFAILS);
794 return err;
795}
796
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700797static inline int ip6_rt_check(struct rt6key *rt_key,
798 struct in6_addr *fl_addr,
799 struct in6_addr *addr_cache)
800{
801 return ((rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
802 (addr_cache == NULL || !ipv6_addr_equal(fl_addr, addr_cache)));
803}
804
Herbert Xu497c6152006-07-30 20:19:33 -0700805static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
806 struct dst_entry *dst,
807 struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Herbert Xu497c6152006-07-30 20:19:33 -0700809 struct ipv6_pinfo *np = inet6_sk(sk);
810 struct rt6_info *rt = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Herbert Xu497c6152006-07-30 20:19:33 -0700812 if (!dst)
813 goto out;
814
815 /* Yes, checking route validity in not connected
816 * case is not very simple. Take into account,
817 * that we do not support routing by source, TOS,
818 * and MSG_DONTROUTE --ANK (980726)
819 *
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700820 * 1. ip6_rt_check(): If route was host route,
821 * check that cached destination is current.
Herbert Xu497c6152006-07-30 20:19:33 -0700822 * If it is network route, we still may
823 * check its validity using saved pointer
824 * to the last used address: daddr_cache.
825 * We do not want to save whole address now,
826 * (because main consumer of this service
827 * is tcp, which has not this problem),
828 * so that the last trick works only on connected
829 * sockets.
830 * 2. oif also should be the same.
831 */
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700832 if (ip6_rt_check(&rt->rt6i_dst, &fl->fl6_dst, np->daddr_cache) ||
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700833#ifdef CONFIG_IPV6_SUBTREES
834 ip6_rt_check(&rt->rt6i_src, &fl->fl6_src, np->saddr_cache) ||
835#endif
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700836 (fl->oif && fl->oif != dst->dev->ifindex)) {
Herbert Xu497c6152006-07-30 20:19:33 -0700837 dst_release(dst);
838 dst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
Herbert Xu497c6152006-07-30 20:19:33 -0700841out:
842 return dst;
843}
844
845static int ip6_dst_lookup_tail(struct sock *sk,
846 struct dst_entry **dst, struct flowi *fl)
847{
848 int err;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (*dst == NULL)
851 *dst = ip6_route_output(sk, fl);
852
853 if ((err = (*dst)->error))
854 goto out_err_release;
855
856 if (ipv6_addr_any(&fl->fl6_src)) {
857 err = ipv6_get_saddr(*dst, &fl->fl6_dst, &fl->fl6_src);
Olaf Hering44456d32005-07-27 11:45:17 -0700858 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 goto out_err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861
862 return 0;
863
864out_err_release:
865 dst_release(*dst);
866 *dst = NULL;
867 return err;
868}
Adrian Bunk34a0b3c2005-11-29 16:28:56 -0800869
Herbert Xu497c6152006-07-30 20:19:33 -0700870/**
871 * ip6_dst_lookup - perform route lookup on flow
872 * @sk: socket which provides route info
873 * @dst: pointer to dst_entry * for result
874 * @fl: flow to lookup
875 *
876 * This function performs a route lookup on the given flow.
877 *
878 * It returns zero on success, or a standard errno code on error.
879 */
880int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi *fl)
881{
882 *dst = NULL;
883 return ip6_dst_lookup_tail(sk, dst, fl);
884}
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800885EXPORT_SYMBOL_GPL(ip6_dst_lookup);
886
Herbert Xu497c6152006-07-30 20:19:33 -0700887/**
888 * ip6_sk_dst_lookup - perform socket cached route lookup on flow
889 * @sk: socket which provides the dst cache and route info
890 * @dst: pointer to dst_entry * for result
891 * @fl: flow to lookup
892 *
893 * This function performs a route lookup on the given flow with the
894 * possibility of using the cached route in the socket if it is valid.
895 * It will take the socket dst lock when operating on the dst cache.
896 * As a result, this function can only be used in process context.
897 *
898 * It returns zero on success, or a standard errno code on error.
899 */
900int ip6_sk_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi *fl)
901{
902 *dst = NULL;
903 if (sk) {
904 *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
905 *dst = ip6_sk_dst_check(sk, *dst, fl);
906 }
907
908 return ip6_dst_lookup_tail(sk, dst, fl);
909}
910EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup);
911
Adrian Bunk34a0b3c2005-11-29 16:28:56 -0800912static inline int ip6_ufo_append_data(struct sock *sk,
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700913 int getfrag(void *from, char *to, int offset, int len,
914 int odd, struct sk_buff *skb),
915 void *from, int length, int hh_len, int fragheaderlen,
916 int transhdrlen, int mtu,unsigned int flags)
917
918{
919 struct sk_buff *skb;
920 int err;
921
922 /* There is support for UDP large send offload by network
923 * device, so create one single skb packet containing complete
924 * udp datagram
925 */
926 if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) {
927 skb = sock_alloc_send_skb(sk,
928 hh_len + fragheaderlen + transhdrlen + 20,
929 (flags & MSG_DONTWAIT), &err);
930 if (skb == NULL)
931 return -ENOMEM;
932
933 /* reserve space for Hardware header */
934 skb_reserve(skb, hh_len);
935
936 /* create space for UDP/IP header */
937 skb_put(skb,fragheaderlen + transhdrlen);
938
939 /* initialize network header pointer */
940 skb->nh.raw = skb->data;
941
942 /* initialize protocol header pointer */
943 skb->h.raw = skb->data + fragheaderlen;
944
Patrick McHardy84fa7932006-08-29 16:44:56 -0700945 skb->ip_summed = CHECKSUM_PARTIAL;
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700946 skb->csum = 0;
947 sk->sk_sndmsg_off = 0;
948 }
949
950 err = skb_append_datato_frags(sk,skb, getfrag, from,
951 (length - transhdrlen));
952 if (!err) {
953 struct frag_hdr fhdr;
954
955 /* specify the length of each IP datagram fragment*/
Herbert Xu79671682006-06-22 02:40:14 -0700956 skb_shinfo(skb)->gso_size = mtu - fragheaderlen -
957 sizeof(struct frag_hdr);
Herbert Xuf83ef8c2006-06-30 13:37:03 -0700958 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700959 ipv6_select_ident(skb, &fhdr);
960 skb_shinfo(skb)->ip6_frag_id = fhdr.identification;
961 __skb_queue_tail(&sk->sk_write_queue, skb);
962
963 return 0;
964 }
965 /* There is not enough support do UPD LSO,
966 * so follow normal path
967 */
968 kfree_skb(skb);
969
970 return err;
971}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900973int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
974 int offset, int len, int odd, struct sk_buff *skb),
975 void *from, int length, int transhdrlen,
976 int hlimit, int tclass, struct ipv6_txoptions *opt, struct flowi *fl,
977 struct rt6_info *rt, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct inet_sock *inet = inet_sk(sk);
980 struct ipv6_pinfo *np = inet6_sk(sk);
981 struct sk_buff *skb;
982 unsigned int maxfraglen, fragheaderlen;
983 int exthdrlen;
984 int hh_len;
985 int mtu;
986 int copy;
987 int err;
988 int offset = 0;
989 int csummode = CHECKSUM_NONE;
990
991 if (flags&MSG_PROBE)
992 return 0;
993 if (skb_queue_empty(&sk->sk_write_queue)) {
994 /*
995 * setup for corking
996 */
997 if (opt) {
998 if (np->cork.opt == NULL) {
999 np->cork.opt = kmalloc(opt->tot_len,
1000 sk->sk_allocation);
1001 if (unlikely(np->cork.opt == NULL))
1002 return -ENOBUFS;
1003 } else if (np->cork.opt->tot_len < opt->tot_len) {
1004 printk(KERN_DEBUG "ip6_append_data: invalid option length\n");
1005 return -EINVAL;
1006 }
1007 memcpy(np->cork.opt, opt, opt->tot_len);
1008 inet->cork.flags |= IPCORK_OPT;
1009 /* need source address above miyazawa*/
1010 }
1011 dst_hold(&rt->u.dst);
1012 np->cork.rt = rt;
1013 inet->cork.fl = *fl;
1014 np->cork.hop_limit = hlimit;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001015 np->cork.tclass = tclass;
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -08001016 mtu = dst_mtu(rt->u.dst.path);
Dave Jonesc7503602006-03-20 22:44:52 -08001017 if (np->frag_size < mtu) {
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -08001018 if (np->frag_size)
1019 mtu = np->frag_size;
1020 }
1021 inet->cork.fragsize = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (dst_allfrag(rt->u.dst.path))
1023 inet->cork.flags |= IPCORK_ALLFRAG;
1024 inet->cork.length = 0;
1025 sk->sk_sndmsg_page = NULL;
1026 sk->sk_sndmsg_off = 0;
1027 exthdrlen = rt->u.dst.header_len + (opt ? opt->opt_flen : 0);
1028 length += exthdrlen;
1029 transhdrlen += exthdrlen;
1030 } else {
1031 rt = np->cork.rt;
1032 fl = &inet->cork.fl;
1033 if (inet->cork.flags & IPCORK_OPT)
1034 opt = np->cork.opt;
1035 transhdrlen = 0;
1036 exthdrlen = 0;
1037 mtu = inet->cork.fragsize;
1038 }
1039
1040 hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
1041
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -07001042 fragheaderlen = sizeof(struct ipv6hdr) + rt->u.dst.nfheader_len + (opt ? opt->opt_nflen : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
1044
1045 if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
1046 if (inet->cork.length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
1047 ipv6_local_error(sk, EMSGSIZE, fl, mtu-exthdrlen);
1048 return -EMSGSIZE;
1049 }
1050 }
1051
1052 /*
1053 * Let's try using as much space as possible.
1054 * Use MTU if total length of the message fits into the MTU.
1055 * Otherwise, we need to reserve fragment header and
1056 * fragment alignment (= 8-15 octects, in total).
1057 *
1058 * Note that we may need to "move" the data from the tail of
1059 * of the buffer to the new fragment when we split
1060 * the message.
1061 *
1062 * FIXME: It may be fragmented into multiple chunks
1063 * at once if non-fragmentable extension headers
1064 * are too large.
1065 * --yoshfuji
1066 */
1067
1068 inet->cork.length += length;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001069 if (((length > mtu) && (sk->sk_protocol == IPPROTO_UDP)) &&
1070 (rt->u.dst.dev->features & NETIF_F_UFO)) {
1071
Patrick McHardybaa829d2006-03-12 20:35:12 -08001072 err = ip6_ufo_append_data(sk, getfrag, from, length, hh_len,
1073 fragheaderlen, transhdrlen, mtu,
1074 flags);
1075 if (err)
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001076 goto error;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001077 return 0;
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
1081 goto alloc_new_skb;
1082
1083 while (length > 0) {
1084 /* Check if the remaining data fits into current packet. */
1085 copy = (inet->cork.length <= mtu && !(inet->cork.flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
1086 if (copy < length)
1087 copy = maxfraglen - skb->len;
1088
1089 if (copy <= 0) {
1090 char *data;
1091 unsigned int datalen;
1092 unsigned int fraglen;
1093 unsigned int fraggap;
1094 unsigned int alloclen;
1095 struct sk_buff *skb_prev;
1096alloc_new_skb:
1097 skb_prev = skb;
1098
1099 /* There's no room in the current skb */
1100 if (skb_prev)
1101 fraggap = skb_prev->len - maxfraglen;
1102 else
1103 fraggap = 0;
1104
1105 /*
1106 * If remaining data exceeds the mtu,
1107 * we know we need more fragment(s).
1108 */
1109 datalen = length + fraggap;
1110 if (datalen > (inet->cork.length <= mtu && !(inet->cork.flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
1111 datalen = maxfraglen - fragheaderlen;
1112
1113 fraglen = datalen + fragheaderlen;
1114 if ((flags & MSG_MORE) &&
1115 !(rt->u.dst.dev->features&NETIF_F_SG))
1116 alloclen = mtu;
1117 else
1118 alloclen = datalen + fragheaderlen;
1119
1120 /*
1121 * The last fragment gets additional space at tail.
1122 * Note: we overallocate on fragments with MSG_MODE
1123 * because we have no idea if we're the last one.
1124 */
1125 if (datalen == length + fraggap)
1126 alloclen += rt->u.dst.trailer_len;
1127
1128 /*
1129 * We just reserve space for fragment header.
1130 * Note: this may be overallocation if the message
1131 * (without MSG_MORE) fits into the MTU.
1132 */
1133 alloclen += sizeof(struct frag_hdr);
1134
1135 if (transhdrlen) {
1136 skb = sock_alloc_send_skb(sk,
1137 alloclen + hh_len,
1138 (flags & MSG_DONTWAIT), &err);
1139 } else {
1140 skb = NULL;
1141 if (atomic_read(&sk->sk_wmem_alloc) <=
1142 2 * sk->sk_sndbuf)
1143 skb = sock_wmalloc(sk,
1144 alloclen + hh_len, 1,
1145 sk->sk_allocation);
1146 if (unlikely(skb == NULL))
1147 err = -ENOBUFS;
1148 }
1149 if (skb == NULL)
1150 goto error;
1151 /*
1152 * Fill in the control structures
1153 */
1154 skb->ip_summed = csummode;
1155 skb->csum = 0;
1156 /* reserve for fragmentation */
1157 skb_reserve(skb, hh_len+sizeof(struct frag_hdr));
1158
1159 /*
1160 * Find where to start putting bytes
1161 */
1162 data = skb_put(skb, fraglen);
1163 skb->nh.raw = data + exthdrlen;
1164 data += fragheaderlen;
1165 skb->h.raw = data + exthdrlen;
1166
1167 if (fraggap) {
1168 skb->csum = skb_copy_and_csum_bits(
1169 skb_prev, maxfraglen,
1170 data + transhdrlen, fraggap, 0);
1171 skb_prev->csum = csum_sub(skb_prev->csum,
1172 skb->csum);
1173 data += fraggap;
Herbert Xue9fa4f72006-08-13 20:12:58 -07001174 pskb_trim_unique(skb_prev, maxfraglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176 copy = datalen - transhdrlen - fraggap;
1177 if (copy < 0) {
1178 err = -EINVAL;
1179 kfree_skb(skb);
1180 goto error;
1181 } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
1182 err = -EFAULT;
1183 kfree_skb(skb);
1184 goto error;
1185 }
1186
1187 offset += copy;
1188 length -= datalen - fraggap;
1189 transhdrlen = 0;
1190 exthdrlen = 0;
1191 csummode = CHECKSUM_NONE;
1192
1193 /*
1194 * Put the packet on the pending queue
1195 */
1196 __skb_queue_tail(&sk->sk_write_queue, skb);
1197 continue;
1198 }
1199
1200 if (copy > length)
1201 copy = length;
1202
1203 if (!(rt->u.dst.dev->features&NETIF_F_SG)) {
1204 unsigned int off;
1205
1206 off = skb->len;
1207 if (getfrag(from, skb_put(skb, copy),
1208 offset, copy, off, skb) < 0) {
1209 __skb_trim(skb, off);
1210 err = -EFAULT;
1211 goto error;
1212 }
1213 } else {
1214 int i = skb_shinfo(skb)->nr_frags;
1215 skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
1216 struct page *page = sk->sk_sndmsg_page;
1217 int off = sk->sk_sndmsg_off;
1218 unsigned int left;
1219
1220 if (page && (left = PAGE_SIZE - off) > 0) {
1221 if (copy >= left)
1222 copy = left;
1223 if (page != frag->page) {
1224 if (i == MAX_SKB_FRAGS) {
1225 err = -EMSGSIZE;
1226 goto error;
1227 }
1228 get_page(page);
1229 skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0);
1230 frag = &skb_shinfo(skb)->frags[i];
1231 }
1232 } else if(i < MAX_SKB_FRAGS) {
1233 if (copy > PAGE_SIZE)
1234 copy = PAGE_SIZE;
1235 page = alloc_pages(sk->sk_allocation, 0);
1236 if (page == NULL) {
1237 err = -ENOMEM;
1238 goto error;
1239 }
1240 sk->sk_sndmsg_page = page;
1241 sk->sk_sndmsg_off = 0;
1242
1243 skb_fill_page_desc(skb, i, page, 0, 0);
1244 frag = &skb_shinfo(skb)->frags[i];
1245 skb->truesize += PAGE_SIZE;
1246 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1247 } else {
1248 err = -EMSGSIZE;
1249 goto error;
1250 }
1251 if (getfrag(from, page_address(frag->page)+frag->page_offset+frag->size, offset, copy, skb->len, skb) < 0) {
1252 err = -EFAULT;
1253 goto error;
1254 }
1255 sk->sk_sndmsg_off += copy;
1256 frag->size += copy;
1257 skb->len += copy;
1258 skb->data_len += copy;
1259 }
1260 offset += copy;
1261 length -= copy;
1262 }
1263 return 0;
1264error:
1265 inet->cork.length -= length;
1266 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1267 return err;
1268}
1269
1270int ip6_push_pending_frames(struct sock *sk)
1271{
1272 struct sk_buff *skb, *tmp_skb;
1273 struct sk_buff **tail_skb;
1274 struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
1275 struct inet_sock *inet = inet_sk(sk);
1276 struct ipv6_pinfo *np = inet6_sk(sk);
1277 struct ipv6hdr *hdr;
1278 struct ipv6_txoptions *opt = np->cork.opt;
1279 struct rt6_info *rt = np->cork.rt;
1280 struct flowi *fl = &inet->cork.fl;
1281 unsigned char proto = fl->proto;
1282 int err = 0;
1283
1284 if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
1285 goto out;
1286 tail_skb = &(skb_shinfo(skb)->frag_list);
1287
1288 /* move skb->data to ip header from ext header */
1289 if (skb->data < skb->nh.raw)
1290 __skb_pull(skb, skb->nh.raw - skb->data);
1291 while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
1292 __skb_pull(tmp_skb, skb->h.raw - skb->nh.raw);
1293 *tail_skb = tmp_skb;
1294 tail_skb = &(tmp_skb->next);
1295 skb->len += tmp_skb->len;
1296 skb->data_len += tmp_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 skb->truesize += tmp_skb->truesize;
1298 __sock_put(tmp_skb->sk);
1299 tmp_skb->destructor = NULL;
1300 tmp_skb->sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302
1303 ipv6_addr_copy(final_dst, &fl->fl6_dst);
1304 __skb_pull(skb, skb->h.raw - skb->nh.raw);
1305 if (opt && opt->opt_flen)
1306 ipv6_push_frag_opts(skb, opt, &proto);
1307 if (opt && opt->opt_nflen)
1308 ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
1309
1310 skb->nh.ipv6h = hdr = (struct ipv6hdr*) skb_push(skb, sizeof(struct ipv6hdr));
1311
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001312 *(u32*)hdr = fl->fl6_flowlabel |
1313 htonl(0x60000000 | ((int)np->cork.tclass << 20));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (skb->len <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN)
1316 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
1317 else
1318 hdr->payload_len = 0;
1319 hdr->hop_limit = np->cork.hop_limit;
1320 hdr->nexthdr = proto;
1321 ipv6_addr_copy(&hdr->saddr, &fl->fl6_src);
1322 ipv6_addr_copy(&hdr->daddr, final_dst);
1323
Patrick McHardya2c20642006-01-08 22:37:26 -08001324 skb->priority = sk->sk_priority;
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 skb->dst = dst_clone(&rt->u.dst);
1327 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1328 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dst->dev, dst_output);
1329 if (err) {
1330 if (err > 0)
Herbert Xu3320da82005-04-19 22:32:22 -07001331 err = np->recverr ? net_xmit_errno(err) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (err)
1333 goto error;
1334 }
1335
1336out:
1337 inet->cork.flags &= ~IPCORK_OPT;
Jesper Juhla51482b2005-11-08 09:41:34 -08001338 kfree(np->cork.opt);
1339 np->cork.opt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (np->cork.rt) {
1341 dst_release(&np->cork.rt->u.dst);
1342 np->cork.rt = NULL;
1343 inet->cork.flags &= ~IPCORK_ALLFRAG;
1344 }
1345 memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
1346 return err;
1347error:
1348 goto out;
1349}
1350
1351void ip6_flush_pending_frames(struct sock *sk)
1352{
1353 struct inet_sock *inet = inet_sk(sk);
1354 struct ipv6_pinfo *np = inet6_sk(sk);
1355 struct sk_buff *skb;
1356
1357 while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
1358 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1359 kfree_skb(skb);
1360 }
1361
1362 inet->cork.flags &= ~IPCORK_OPT;
1363
Jesper Juhla51482b2005-11-08 09:41:34 -08001364 kfree(np->cork.opt);
1365 np->cork.opt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (np->cork.rt) {
1367 dst_release(&np->cork.rt->u.dst);
1368 np->cork.rt = NULL;
1369 inet->cork.flags &= ~IPCORK_ALLFRAG;
1370 }
1371 memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
1372}