blob: 9cbf17686a1b81c54ece58214e36263d519f222c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 output functions
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Based on linux/net/ipv4/ip_output.c
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * Changes:
16 * A.N.Kuznetsov : airthmetics in fragmentation.
17 * extension headers are implemented.
18 * route changes now work.
19 * ip6_forward does not confuse sniffers.
20 * etc.
21 *
22 * H. von Brand : Added missing #include <linux/string.h>
23 * Imran Patel : frag id should be in NBO
24 * Kazunori MIYAZAWA @USAGI
25 * : add ip6_append_data and related functions
26 * for datagram xmit
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/errno.h>
Herbert Xuef76bc22008-01-11 19:15:08 -080030#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/string.h>
32#include <linux/socket.h>
33#include <linux/net.h>
34#include <linux/netdevice.h>
35#include <linux/if_arp.h>
36#include <linux/in6.h>
37#include <linux/tcp.h>
38#include <linux/route.h>
Herbert Xub59f45d2006-05-27 23:05:54 -070039#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <linux/netfilter.h>
43#include <linux/netfilter_ipv6.h>
44
45#include <net/sock.h>
46#include <net/snmp.h>
47
48#include <net/ipv6.h>
49#include <net/ndisc.h>
50#include <net/protocol.h>
51#include <net/ip6_route.h>
52#include <net/addrconf.h>
53#include <net/rawv6.h>
54#include <net/icmp.h>
55#include <net/xfrm.h>
56#include <net/checksum.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090057#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David Stevensad0081e2010-12-17 11:42:42 +000059int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *));
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Herbert Xuef76bc22008-01-11 19:15:08 -080061int __ip6_local_out(struct sk_buff *skb)
62{
63 int len;
64
65 len = skb->len - sizeof(struct ipv6hdr);
66 if (len > IPV6_MAXPLEN)
67 len = 0;
68 ipv6_hdr(skb)->payload_len = htons(len);
69
Jan Engelhardtb2e0b382010-03-23 04:09:07 +010070 return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
71 skb_dst(skb)->dev, dst_output);
Herbert Xuef76bc22008-01-11 19:15:08 -080072}
73
74int ip6_local_out(struct sk_buff *skb)
75{
76 int err;
77
78 err = __ip6_local_out(skb);
79 if (likely(err == 1))
80 err = dst_output(skb);
81
82 return err;
83}
84EXPORT_SYMBOL_GPL(ip6_local_out);
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* dev_loopback_xmit for use with netfilter. */
87static int ip6_dev_loopback_xmit(struct sk_buff *newskb)
88{
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -070089 skb_reset_mac_header(newskb);
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -030090 __skb_pull(newskb, skb_network_offset(newskb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 newskb->pkt_type = PACKET_LOOPBACK;
92 newskb->ip_summed = CHECKSUM_UNNECESSARY;
Eric Dumazetadf30902009-06-02 05:19:30 +000093 WARN_ON(!skb_dst(newskb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Eric Dumazete30b38c2010-04-15 09:13:03 +000095 netif_rx_ni(newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return 0;
97}
98
Jan Engelhardt9e508492010-04-13 15:28:11 +020099static int ip6_finish_output2(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Eric Dumazetadf30902009-06-02 05:19:30 +0000101 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct net_device *dev = dst->dev;
Eric Dumazet8a533662012-02-09 16:13:19 -0500103 struct neighbour *neigh;
104 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 skb->protocol = htons(ETH_P_IPV6);
107 skb->dev = dev;
108
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700109 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000110 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Octavian Purdila7ad68482010-01-06 20:37:01 -0800112 if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(skb->sk) &&
Patrick McHardyd1db2752010-05-11 14:40:55 +0200113 ((mroute6_socket(dev_net(dev), skb) &&
Benjamin Therybd91b8b2008-12-10 16:07:08 -0800114 !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900115 ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
116 &ipv6_hdr(skb)->saddr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
118
119 /* Do not check for IFF_ALLMULTI; multicast routing
120 is not supported in any case.
121 */
122 if (newskb)
Jan Engelhardtb2e0b382010-03-23 04:09:07 +0100123 NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
124 newskb, NULL, newskb->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 ip6_dev_loopback_xmit);
126
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700127 if (ipv6_hdr(skb)->hop_limit == 0) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700128 IP6_INC_STATS(dev_net(dev), idev,
129 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 kfree_skb(skb);
131 return 0;
132 }
133 }
134
Neil Hormanedf391f2009-04-27 02:45:02 -0700135 IP6_UPD_PO_STATS(dev_net(dev), idev, IPSTATS_MIB_OUTMCAST,
136 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138
Eric Dumazet8a533662012-02-09 16:13:19 -0500139 rcu_read_lock();
140 if (dst->hh) {
141 res = neigh_hh_output(dst->hh, skb);
142
143 rcu_read_unlock();
144 return res;
145 } else {
146 neigh = dst_get_neighbour(dst);
147 if (neigh) {
148 res = neigh->output(skb);
149
150 rcu_read_unlock();
151 return res;
152 }
153 rcu_read_unlock();
154 }
Jan Engelhardt9e508492010-04-13 15:28:11 +0200155
156 IP6_INC_STATS_BH(dev_net(dst->dev),
157 ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
158 kfree_skb(skb);
159 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Jan Engelhardt9e508492010-04-13 15:28:11 +0200162static int ip6_finish_output(struct sk_buff *skb)
163{
164 if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
165 dst_allfrag(skb_dst(skb)))
166 return ip6_fragment(skb, ip6_finish_output2);
167 else
168 return ip6_finish_output2(skb);
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171int ip6_output(struct sk_buff *skb)
172{
Jan Engelhardt9e508492010-04-13 15:28:11 +0200173 struct net_device *dev = skb_dst(skb)->dev;
Eric Dumazetadf30902009-06-02 05:19:30 +0000174 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900175 if (unlikely(idev->cnf.disable_ipv6)) {
Jan Engelhardt9e508492010-04-13 15:28:11 +0200176 IP6_INC_STATS(dev_net(dev), idev,
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700177 IPSTATS_MIB_OUTDISCARDS);
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900178 kfree_skb(skb);
179 return 0;
180 }
181
Jan Engelhardt9c6eb282010-04-13 15:32:16 +0200182 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev,
183 ip6_finish_output,
184 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
Shan Weib5d43992010-04-15 16:48:48 +0000188 * xmit an sk_buff (used by TCP, SCTP and DCCP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
190
David S. Miller4c9483b2011-03-12 16:22:43 -0500191int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
Shan Wei4e15ed42010-04-15 16:43:08 +0000192 struct ipv6_txoptions *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700194 struct net *net = sock_net(sk);
Patrick McHardyb30bd282006-03-23 01:17:25 -0800195 struct ipv6_pinfo *np = inet6_sk(sk);
David S. Miller4c9483b2011-03-12 16:22:43 -0500196 struct in6_addr *first_hop = &fl6->daddr;
Eric Dumazetadf30902009-06-02 05:19:30 +0000197 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct ipv6hdr *hdr;
David S. Miller4c9483b2011-03-12 16:22:43 -0500199 u8 proto = fl6->flowi6_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int seg_len = skb->len;
Gerrit Renkere651f032009-08-09 08:12:48 +0000201 int hlimit = -1;
202 int tclass = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 u32 mtu;
204
205 if (opt) {
Chuck Leverc2636b42007-10-23 21:07:32 -0700206 unsigned int head_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /* First: exthdrs may take lots of space (~8K for now)
209 MAX_HEADER is not enough.
210 */
211 head_room = opt->opt_nflen + opt->opt_flen;
212 seg_len += head_room;
213 head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
214
215 if (skb_headroom(skb) < head_room) {
216 struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900217 if (skb2 == NULL) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000218 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900219 IPSTATS_MIB_OUTDISCARDS);
220 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -ENOBUFS;
222 }
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900223 kfree_skb(skb);
224 skb = skb2;
Dan Carpenter83d7eb22010-04-30 16:42:08 -0700225 skb_set_owner_w(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 if (opt->opt_flen)
228 ipv6_push_frag_opts(skb, opt, &proto);
229 if (opt->opt_nflen)
230 ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
231 }
232
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700233 skb_push(skb, sizeof(struct ipv6hdr));
234 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700235 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /*
238 * Fill in the IPv6 header
239 */
Gerrit Renkere651f032009-08-09 08:12:48 +0000240 if (np) {
241 tclass = np->tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 hlimit = np->hop_limit;
Gerrit Renkere651f032009-08-09 08:12:48 +0000243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (hlimit < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400245 hlimit = ip6_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David S. Miller4c9483b2011-03-12 16:22:43 -0500247 *(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | fl6->flowlabel;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 hdr->payload_len = htons(seg_len);
250 hdr->nexthdr = proto;
251 hdr->hop_limit = hlimit;
252
David S. Miller4c9483b2011-03-12 16:22:43 -0500253 ipv6_addr_copy(&hdr->saddr, &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 ipv6_addr_copy(&hdr->daddr, first_hop);
255
Patrick McHardya2c20642006-01-08 22:37:26 -0800256 skb->priority = sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800257 skb->mark = sk->sk_mark;
Patrick McHardya2c20642006-01-08 22:37:26 -0800258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 mtu = dst_mtu(dst);
Wei Yongjun283d07ac2008-08-03 21:15:59 -0700260 if ((skb->len <= mtu) || skb->local_df || skb_is_gso(skb)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000261 IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
Neil Hormanedf391f2009-04-27 02:45:02 -0700262 IPSTATS_MIB_OUT, skb->len);
Jan Engelhardtb2e0b382010-03-23 04:09:07 +0100263 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
264 dst->dev, dst_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
267 if (net_ratelimit())
268 printk(KERN_DEBUG "IPv6: sending pkt_too_big to self\n");
269 skb->dev = dst->dev;
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000270 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Eric Dumazetadf30902009-06-02 05:19:30 +0000271 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 kfree_skb(skb);
273 return -EMSGSIZE;
274}
275
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900276EXPORT_SYMBOL(ip6_xmit);
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/*
279 * To avoid extra problems ND packets are send through this
280 * routine. It's code duplication but I really want to avoid
281 * extra checks since ipv6_build_header is used by TCP (which
282 * is for us performance critical)
283 */
284
285int ip6_nd_hdr(struct sock *sk, struct sk_buff *skb, struct net_device *dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900286 const struct in6_addr *saddr, const struct in6_addr *daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 int proto, int len)
288{
289 struct ipv6_pinfo *np = inet6_sk(sk);
290 struct ipv6hdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 skb->protocol = htons(ETH_P_IPV6);
293 skb->dev = dev;
294
Arnaldo Carvalho de Melo55f79cc2007-03-14 21:05:03 -0300295 skb_reset_network_header(skb);
296 skb_put(skb, sizeof(struct ipv6hdr));
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700297 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Al Viroae08e1f2006-11-08 00:27:11 -0800299 *(__be32*)hdr = htonl(0x60000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 hdr->payload_len = htons(len);
302 hdr->nexthdr = proto;
303 hdr->hop_limit = np->hop_limit;
304
305 ipv6_addr_copy(&hdr->saddr, saddr);
306 ipv6_addr_copy(&hdr->daddr, daddr);
307
308 return 0;
309}
310
311static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
312{
313 struct ip6_ra_chain *ra;
314 struct sock *last = NULL;
315
316 read_lock(&ip6_ra_lock);
317 for (ra = ip6_ra_chain; ra; ra = ra->next) {
318 struct sock *sk = ra->sk;
Andrew McDonald0bd1b592005-08-09 19:44:42 -0700319 if (sk && ra->sel == sel &&
320 (!sk->sk_bound_dev_if ||
321 sk->sk_bound_dev_if == skb->dev->ifindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (last) {
323 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
324 if (skb2)
325 rawv6_rcv(last, skb2);
326 }
327 last = sk;
328 }
329 }
330
331 if (last) {
332 rawv6_rcv(last, skb);
333 read_unlock(&ip6_ra_lock);
334 return 1;
335 }
336 read_unlock(&ip6_ra_lock);
337 return 0;
338}
339
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700340static int ip6_forward_proxy_check(struct sk_buff *skb)
341{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700342 struct ipv6hdr *hdr = ipv6_hdr(skb);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700343 u8 nexthdr = hdr->nexthdr;
344 int offset;
345
346 if (ipv6_ext_hdr(nexthdr)) {
347 offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr);
348 if (offset < 0)
349 return 0;
350 } else
351 offset = sizeof(struct ipv6hdr);
352
353 if (nexthdr == IPPROTO_ICMPV6) {
354 struct icmp6hdr *icmp6;
355
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700356 if (!pskb_may_pull(skb, (skb_network_header(skb) +
357 offset + 1 - skb->data)))
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700358 return 0;
359
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700360 icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700361
362 switch (icmp6->icmp6_type) {
363 case NDISC_ROUTER_SOLICITATION:
364 case NDISC_ROUTER_ADVERTISEMENT:
365 case NDISC_NEIGHBOUR_SOLICITATION:
366 case NDISC_NEIGHBOUR_ADVERTISEMENT:
367 case NDISC_REDIRECT:
368 /* For reaction involving unicast neighbor discovery
369 * message destined to the proxied address, pass it to
370 * input function.
371 */
372 return 1;
373 default:
374 break;
375 }
376 }
377
Ville Nuorvala74553b02006-09-22 14:42:18 -0700378 /*
379 * The proxying router can't forward traffic sent to a link-local
380 * address, so signal the sender and discard the packet. This
381 * behavior is clarified by the MIPv6 specification.
382 */
383 if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
384 dst_link_failure(skb);
385 return -1;
386 }
387
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700388 return 0;
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391static inline int ip6_forward_finish(struct sk_buff *skb)
392{
393 return dst_output(skb);
394}
395
396int ip6_forward(struct sk_buff *skb)
397{
Eric Dumazetadf30902009-06-02 05:19:30 +0000398 struct dst_entry *dst = skb_dst(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700399 struct ipv6hdr *hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct inet6_skb_parm *opt = IP6CB(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900401 struct net *net = dev_net(dst->dev);
Eric Dumazet8a533662012-02-09 16:13:19 -0500402 struct neighbour *n;
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800403 u32 mtu;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900404
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700405 if (net->ipv6.devconf_all->forwarding == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto error;
407
Ben Hutchings4497b072008-06-19 16:22:28 -0700408 if (skb_warn_if_lro(skb))
409 goto drop;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700412 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 goto drop;
414 }
415
Alexey Kuznetsov72b43d02011-01-12 08:34:08 +0000416 if (skb->pkt_type != PACKET_HOST)
417 goto drop;
418
Herbert Xu35fc92a2007-03-26 23:22:20 -0700419 skb_forward_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /*
422 * We DO NOT make any processing on
423 * RA packets, pushing them to user level AS IS
424 * without ane WARRANTY that application will be able
425 * to interpret them. The reason is that we
426 * cannot make anything clever here.
427 *
428 * We are not end-node, so that if packet contains
429 * AH/ESP, we cannot make anything.
430 * Defragmentation also would be mistake, RA packets
431 * cannot be fragmented, because there is no warranty
432 * that different fragments will go along one path. --ANK
433 */
434 if (opt->ra) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700435 u8 *ptr = skb_network_header(skb) + opt->ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (ip6_call_ra_chain(skb, (ptr[2]<<8) + ptr[3]))
437 return 0;
438 }
439
440 /*
441 * check and decrement ttl
442 */
443 if (hdr->hop_limit <= 1) {
444 /* Force OUTPUT device used as source address */
445 skb->dev = dst->dev;
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000446 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700447 IP6_INC_STATS_BH(net,
448 ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 kfree_skb(skb);
451 return -ETIMEDOUT;
452 }
453
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700454 /* XXX: idev->cnf.proxy_ndp? */
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700455 if (net->ipv6.devconf_all->proxy_ndp &&
Daniel Lezcano8a3edd82008-03-07 11:14:16 -0800456 pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
Ville Nuorvala74553b02006-09-22 14:42:18 -0700457 int proxied = ip6_forward_proxy_check(skb);
458 if (proxied > 0)
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700459 return ip6_input(skb);
Ville Nuorvala74553b02006-09-22 14:42:18 -0700460 else if (proxied < 0) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700461 IP6_INC_STATS(net, ip6_dst_idev(dst),
462 IPSTATS_MIB_INDISCARDS);
Ville Nuorvala74553b02006-09-22 14:42:18 -0700463 goto drop;
464 }
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700465 }
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!xfrm6_route_forward(skb)) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700468 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto drop;
470 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000471 dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /* IPv6 specs say nothing about it, but it is clear that we cannot
474 send redirects to source routed frames.
Masahide NAKAMURA1e5dc142007-08-24 19:08:55 +0900475 We don't send redirects to frames decapsulated from IPsec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 */
Eric Dumazet8a533662012-02-09 16:13:19 -0500477 n = dst_get_neighbour(dst);
478 if (skb->dev == dst->dev && n && opt->srcrt == 0 && !skb_sec_path(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 struct in6_addr *target = NULL;
480 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /*
483 * incoming and outgoing devices are the same
484 * send a redirect.
485 */
486
487 rt = (struct rt6_info *) dst;
488 if ((rt->rt6i_flags & RTF_GATEWAY))
489 target = (struct in6_addr*)&n->primary_key;
490 else
491 target = &hdr->daddr;
492
David S. Miller92d86822011-02-04 15:55:25 -0800493 if (!rt->rt6i_peer)
494 rt6_bind_peer(rt, 1);
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* Limit redirects both by destination (here)
497 and by source (inside ndisc_send_redirect)
498 */
David S. Miller92d86822011-02-04 15:55:25 -0800499 if (inet_peer_xrlim_allow(rt->rt6i_peer, 1*HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 ndisc_send_redirect(skb, n, target);
David L Stevens5bb1ab02007-05-09 13:53:44 -0700501 } else {
502 int addrtype = ipv6_addr_type(&hdr->saddr);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* This check is security critical. */
YOSHIFUJI Hideakif81b2e72008-06-25 16:55:26 +0900505 if (addrtype == IPV6_ADDR_ANY ||
506 addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
David L Stevens5bb1ab02007-05-09 13:53:44 -0700507 goto error;
508 if (addrtype & IPV6_ADDR_LINKLOCAL) {
509 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000510 ICMPV6_NOT_NEIGHBOUR, 0);
David L Stevens5bb1ab02007-05-09 13:53:44 -0700511 goto error;
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800515 mtu = dst_mtu(dst);
516 if (mtu < IPV6_MIN_MTU)
517 mtu = IPV6_MIN_MTU;
518
Herbert Xu0aa68272010-05-27 16:14:30 -0700519 if (skb->len > mtu && !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* Again, force OUTPUT device used as source address */
521 skb->dev = dst->dev;
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800522 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700523 IP6_INC_STATS_BH(net,
524 ip6_dst_idev(dst), IPSTATS_MIB_INTOOBIGERRORS);
525 IP6_INC_STATS_BH(net,
526 ip6_dst_idev(dst), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 kfree_skb(skb);
528 return -EMSGSIZE;
529 }
530
531 if (skb_cow(skb, dst->dev->hard_header_len)) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700532 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 goto drop;
534 }
535
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700536 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 /* Mangling hops number delayed to point after skb COW */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 hdr->hop_limit--;
541
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700542 IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
Jan Engelhardtb2e0b382010-03-23 04:09:07 +0100543 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dst->dev,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800544 ip6_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546error:
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700547 IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548drop:
549 kfree_skb(skb);
550 return -EINVAL;
551}
552
553static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
554{
555 to->pkt_type = from->pkt_type;
556 to->priority = from->priority;
557 to->protocol = from->protocol;
Eric Dumazetadf30902009-06-02 05:19:30 +0000558 skb_dst_drop(to);
559 skb_dst_set(to, dst_clone(skb_dst(from)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 to->dev = from->dev;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800561 to->mark = from->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563#ifdef CONFIG_NET_SCHED
564 to->tc_index = from->tc_index;
565#endif
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -0700566 nf_copy(to, from);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700567#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
568 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
569 to->nf_trace = from->nf_trace;
570#endif
James Morris984bc162006-06-09 00:29:17 -0700571 skb_copy_secmark(to, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
575{
576 u16 offset = sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700577 struct ipv6_opt_hdr *exthdr =
578 (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700579 unsigned int packet_len = skb->tail - skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int found_rhdr = 0;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700581 *nexthdr = &ipv6_hdr(skb)->nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 while (offset + 1 <= packet_len) {
584
585 switch (**nexthdr) {
586
587 case NEXTHDR_HOP:
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700588 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 case NEXTHDR_ROUTING:
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700590 found_rhdr = 1;
591 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 case NEXTHDR_DEST:
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -0700593#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700594 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
595 break;
596#endif
597 if (found_rhdr)
598 return offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 break;
600 default :
601 return offset;
602 }
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700603
604 offset += ipv6_optlen(exthdr);
605 *nexthdr = &exthdr->nexthdr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700606 exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
607 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
610 return offset;
611}
612
Eric Dumazetef81bb42011-08-08 23:44:00 -0700613static u32 hashidentrnd __read_mostly;
614#define FID_HASH_SZ 16
615static u32 ipv6_fragmentation_id[FID_HASH_SZ];
616
617void __init initialize_hashidentrnd(void)
618{
619 get_random_bytes(&hashidentrnd, sizeof(hashidentrnd));
620}
621
622static u32 __ipv6_select_ident(const struct in6_addr *addr)
623{
624 u32 newid, oldid, hash = jhash2((u32 *)addr, 4, hashidentrnd);
625 u32 *pid = &ipv6_fragmentation_id[hash % FID_HASH_SZ];
626
627 do {
628 oldid = *pid;
629 newid = oldid + 1;
630 if (!(hash + newid))
631 newid++;
632 } while (cmpxchg(pid, oldid, newid) != oldid);
633
634 return hash + newid;
635}
636
Jason Wanga1b7ab02011-10-09 10:56:44 +0800637void ipv6_select_ident(struct frag_hdr *fhdr, struct in6_addr *addr)
Eric Dumazetef81bb42011-08-08 23:44:00 -0700638{
Jason Wanga1b7ab02011-10-09 10:56:44 +0800639 fhdr->identification = htonl(__ipv6_select_ident(addr));
Eric Dumazetef81bb42011-08-08 23:44:00 -0700640}
641
David Stevensad0081e2010-12-17 11:42:42 +0000642int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct sk_buff *frag;
Eric Dumazetadf30902009-06-02 05:19:30 +0000645 struct rt6_info *rt = (struct rt6_info*)skb_dst(skb);
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -0800646 struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct ipv6hdr *tmp_hdr;
648 struct frag_hdr *fh;
649 unsigned int mtu, hlen, left, len;
Al Viroae08e1f2006-11-08 00:27:11 -0800650 __be32 frag_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int ptr, offset = 0, err=0;
652 u8 *prevhdr, nexthdr = 0;
Eric Dumazetadf30902009-06-02 05:19:30 +0000653 struct net *net = dev_net(skb_dst(skb)->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 hlen = ip6_find_1stfragopt(skb, &prevhdr);
656 nexthdr = *prevhdr;
657
John Heffner628a5c52007-04-20 15:53:27 -0700658 mtu = ip6_skb_dst_mtu(skb);
John Heffnerb881ef72007-04-20 15:52:39 -0700659
660 /* We must not fragment if the socket is set to force MTU discovery
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800661 * or if the skb it not generated by a local socket.
John Heffnerb881ef72007-04-20 15:52:39 -0700662 */
Shan Weif2228f72010-04-18 16:58:22 +0000663 if (!skb->local_df && skb->len > mtu) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000664 skb->dev = skb_dst(skb)->dev;
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000665 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Eric Dumazetadf30902009-06-02 05:19:30 +0000666 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700667 IPSTATS_MIB_FRAGFAILS);
John Heffnerb881ef72007-04-20 15:52:39 -0700668 kfree_skb(skb);
669 return -EMSGSIZE;
670 }
671
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -0800672 if (np && np->frag_size < mtu) {
673 if (np->frag_size)
674 mtu = np->frag_size;
675 }
676 mtu -= hlen + sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
David S. Miller21dc3302010-08-23 00:13:46 -0700678 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int first_len = skb_pagelen(skb);
Eric Dumazet3d130082010-09-21 08:47:45 +0000680 struct sk_buff *frag2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 if (first_len - hlen > mtu ||
683 ((first_len - hlen) & 7) ||
684 skb_cloned(skb))
685 goto slow_path;
686
David S. Miller4d9092b2009-06-09 00:20:05 -0700687 skb_walk_frags(skb, frag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* Correct geometry. */
689 if (frag->len > mtu ||
690 ((frag->len & 7) && frag->next) ||
691 skb_headroom(frag) < hlen)
Eric Dumazet3d130082010-09-21 08:47:45 +0000692 goto slow_path_clean;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* Partially cloned skb? */
695 if (skb_shared(frag))
Eric Dumazet3d130082010-09-21 08:47:45 +0000696 goto slow_path_clean;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700697
698 BUG_ON(frag->sk);
699 if (skb->sk) {
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700700 frag->sk = skb->sk;
701 frag->destructor = sock_wfree;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700702 }
Eric Dumazet3d130082010-09-21 08:47:45 +0000703 skb->truesize -= frag->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705
706 err = 0;
707 offset = 0;
708 frag = skb_shinfo(skb)->frag_list;
David S. Miller4d9092b2009-06-09 00:20:05 -0700709 skb_frag_list_init(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* BUILD HEADER */
711
YOSHIFUJI Hideaki9a217a12006-12-05 13:47:21 -0800712 *prevhdr = NEXTHDR_FRAGMENT;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700713 tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (!tmp_hdr) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000715 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700716 IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return -ENOMEM;
718 }
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 __skb_pull(skb, hlen);
721 fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700722 __skb_push(skb, hlen);
723 skb_reset_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700724 memcpy(skb_network_header(skb), tmp_hdr, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Jason Wanga1b7ab02011-10-09 10:56:44 +0800726 ipv6_select_ident(fh, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 fh->nexthdr = nexthdr;
728 fh->reserved = 0;
729 fh->frag_off = htons(IP6_MF);
730 frag_id = fh->identification;
731
732 first_len = skb_pagelen(skb);
733 skb->data_len = first_len - skb_headlen(skb);
734 skb->len = first_len;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700735 ipv6_hdr(skb)->payload_len = htons(first_len -
736 sizeof(struct ipv6hdr));
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900737
Changli Gaod8d1f302010-06-10 23:31:35 -0700738 dst_hold(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 for (;;) {
741 /* Prepare header of the next frame,
742 * before previous one went down. */
743 if (frag) {
744 frag->ip_summed = CHECKSUM_NONE;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300745 skb_reset_transport_header(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700747 __skb_push(frag, hlen);
748 skb_reset_network_header(frag);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700749 memcpy(skb_network_header(frag), tmp_hdr,
750 hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 offset += skb->len - hlen - sizeof(struct frag_hdr);
752 fh->nexthdr = nexthdr;
753 fh->reserved = 0;
754 fh->frag_off = htons(offset);
755 if (frag->next != NULL)
756 fh->frag_off |= htons(IP6_MF);
757 fh->identification = frag_id;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700758 ipv6_hdr(frag)->payload_len =
759 htons(frag->len -
760 sizeof(struct ipv6hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 ip6_copy_metadata(frag, skb);
762 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 err = output(skb);
Wei Dongdafee492006-08-02 13:41:21 -0700765 if(!err)
Changli Gaod8d1f302010-06-10 23:31:35 -0700766 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700767 IPSTATS_MIB_FRAGCREATES);
Wei Dongdafee492006-08-02 13:41:21 -0700768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (err || !frag)
770 break;
771
772 skb = frag;
773 frag = skb->next;
774 skb->next = NULL;
775 }
776
Jesper Juhla51482b2005-11-08 09:41:34 -0800777 kfree(tmp_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 if (err == 0) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700780 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700781 IPSTATS_MIB_FRAGOKS);
Changli Gaod8d1f302010-06-10 23:31:35 -0700782 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return 0;
784 }
785
786 while (frag) {
787 skb = frag->next;
788 kfree_skb(frag);
789 frag = skb;
790 }
791
Changli Gaod8d1f302010-06-10 23:31:35 -0700792 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700793 IPSTATS_MIB_FRAGFAILS);
Changli Gaod8d1f302010-06-10 23:31:35 -0700794 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return err;
Eric Dumazet3d130082010-09-21 08:47:45 +0000796
797slow_path_clean:
798 skb_walk_frags(skb, frag2) {
799 if (frag2 == frag)
800 break;
801 frag2->sk = NULL;
802 frag2->destructor = NULL;
803 skb->truesize += frag2->truesize;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806
807slow_path:
808 left = skb->len - hlen; /* Space per frame */
809 ptr = hlen; /* Where to start from */
810
811 /*
812 * Fragment the datagram.
813 */
814
815 *prevhdr = NEXTHDR_FRAGMENT;
816
817 /*
818 * Keep copying data until we run out.
819 */
820 while(left > 0) {
821 len = left;
822 /* IF: it doesn't fit, use 'mtu' - the data space left */
823 if (len > mtu)
824 len = mtu;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300825 /* IF: we are not sending up to and including the packet end
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 then align the next start on an eight byte boundary */
827 if (len < left) {
828 len &= ~7;
829 }
830 /*
831 * Allocate buffer.
832 */
833
Changli Gaod8d1f302010-06-10 23:31:35 -0700834 if ((frag = alloc_skb(len+hlen+sizeof(struct frag_hdr)+LL_ALLOCATED_SPACE(rt->dst.dev), GFP_ATOMIC)) == NULL) {
Patrick McHardy64ce2072005-08-09 20:50:53 -0700835 NETDEBUG(KERN_INFO "IPv6: frag: no memory for new fragment!\n");
Eric Dumazetadf30902009-06-02 05:19:30 +0000836 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900837 IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 err = -ENOMEM;
839 goto fail;
840 }
841
842 /*
843 * Set up data on packet
844 */
845
846 ip6_copy_metadata(frag, skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700847 skb_reserve(frag, LL_RESERVED_SPACE(rt->dst.dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 skb_put(frag, len + hlen + sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700849 skb_reset_network_header(frag);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300850 fh = (struct frag_hdr *)(skb_network_header(frag) + hlen);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700851 frag->transport_header = (frag->network_header + hlen +
852 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 /*
855 * Charge the memory for the fragment to any owner
856 * it might possess
857 */
858 if (skb->sk)
859 skb_set_owner_w(frag, skb->sk);
860
861 /*
862 * Copy the packet header into the new buffer.
863 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300864 skb_copy_from_linear_data(skb, skb_network_header(frag), hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 /*
867 * Build fragment header.
868 */
869 fh->nexthdr = nexthdr;
870 fh->reserved = 0;
Yan Zhengf36d6ab2005-10-03 14:19:15 -0700871 if (!frag_id) {
Jason Wanga1b7ab02011-10-09 10:56:44 +0800872 ipv6_select_ident(fh, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 frag_id = fh->identification;
874 } else
875 fh->identification = frag_id;
876
877 /*
878 * Copy a block of the IP datagram.
879 */
Wei Yongjun8984e412007-08-21 20:59:08 -0700880 if (skb_copy_bits(skb, ptr, skb_transport_header(frag), len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 BUG();
882 left -= len;
883
884 fh->frag_off = htons(offset);
885 if (left > 0)
886 fh->frag_off |= htons(IP6_MF);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700887 ipv6_hdr(frag)->payload_len = htons(frag->len -
888 sizeof(struct ipv6hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 ptr += len;
891 offset += len;
892
893 /*
894 * Put this fragment into the sending queue.
895 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 err = output(frag);
897 if (err)
898 goto fail;
Wei Dongdafee492006-08-02 13:41:21 -0700899
Eric Dumazetadf30902009-06-02 05:19:30 +0000900 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700901 IPSTATS_MIB_FRAGCREATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000903 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900904 IPSTATS_MIB_FRAGOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return err;
907
908fail:
Eric Dumazetadf30902009-06-02 05:19:30 +0000909 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900910 IPSTATS_MIB_FRAGFAILS);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900911 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return err;
913}
914
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000915static inline int ip6_rt_check(const struct rt6key *rt_key,
916 const struct in6_addr *fl_addr,
917 const struct in6_addr *addr_cache)
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700918{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000919 return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
920 (addr_cache == NULL || !ipv6_addr_equal(fl_addr, addr_cache));
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700921}
922
Herbert Xu497c6152006-07-30 20:19:33 -0700923static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
924 struct dst_entry *dst,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000925 const struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Herbert Xu497c6152006-07-30 20:19:33 -0700927 struct ipv6_pinfo *np = inet6_sk(sk);
928 struct rt6_info *rt = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Herbert Xu497c6152006-07-30 20:19:33 -0700930 if (!dst)
931 goto out;
932
933 /* Yes, checking route validity in not connected
934 * case is not very simple. Take into account,
935 * that we do not support routing by source, TOS,
936 * and MSG_DONTROUTE --ANK (980726)
937 *
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700938 * 1. ip6_rt_check(): If route was host route,
939 * check that cached destination is current.
Herbert Xu497c6152006-07-30 20:19:33 -0700940 * If it is network route, we still may
941 * check its validity using saved pointer
942 * to the last used address: daddr_cache.
943 * We do not want to save whole address now,
944 * (because main consumer of this service
945 * is tcp, which has not this problem),
946 * so that the last trick works only on connected
947 * sockets.
948 * 2. oif also should be the same.
949 */
David S. Miller4c9483b2011-03-12 16:22:43 -0500950 if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700951#ifdef CONFIG_IPV6_SUBTREES
David S. Miller4c9483b2011-03-12 16:22:43 -0500952 ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700953#endif
David S. Miller4c9483b2011-03-12 16:22:43 -0500954 (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex)) {
Herbert Xu497c6152006-07-30 20:19:33 -0700955 dst_release(dst);
956 dst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
Herbert Xu497c6152006-07-30 20:19:33 -0700959out:
960 return dst;
961}
962
963static int ip6_dst_lookup_tail(struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500964 struct dst_entry **dst, struct flowi6 *fl6)
Herbert Xu497c6152006-07-30 20:19:33 -0700965{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900966 struct net *net = sock_net(sk);
Eric Dumazet8a533662012-02-09 16:13:19 -0500967#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
968 struct neighbour *n;
969#endif
970 int err;
Herbert Xu497c6152006-07-30 20:19:33 -0700971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (*dst == NULL)
David S. Miller4c9483b2011-03-12 16:22:43 -0500973 *dst = ip6_route_output(net, sk, fl6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if ((err = (*dst)->error))
976 goto out_err_release;
977
David S. Miller4c9483b2011-03-12 16:22:43 -0500978 if (ipv6_addr_any(&fl6->saddr)) {
Daniel Walterc3968a82011-04-13 21:10:57 +0000979 struct rt6_info *rt = (struct rt6_info *) *dst;
980 err = ip6_route_get_saddr(net, rt, &fl6->daddr,
981 sk ? inet6_sk(sk)->srcprefs : 0,
982 &fl6->saddr);
Olaf Hering44456d32005-07-27 11:45:17 -0700983 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto out_err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
Neil Horman95c385b2007-04-25 17:08:10 -0700987#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Neil Hormane550dfb2008-09-09 13:51:35 -0700988 /*
989 * Here if the dst entry we've looked up
990 * has a neighbour entry that is in the INCOMPLETE
991 * state and the src address from the flow is
992 * marked as OPTIMISTIC, we release the found
993 * dst entry and replace it instead with the
994 * dst entry of the nexthop router
995 */
Eric Dumazet8a533662012-02-09 16:13:19 -0500996 rcu_read_lock();
997 n = dst_get_neighbour(*dst);
998 if (n && !(n->nud_state & NUD_VALID)) {
Neil Hormane550dfb2008-09-09 13:51:35 -0700999 struct inet6_ifaddr *ifp;
David S. Miller4c9483b2011-03-12 16:22:43 -05001000 struct flowi6 fl_gw6;
Neil Hormane550dfb2008-09-09 13:51:35 -07001001 int redirect;
Neil Horman95c385b2007-04-25 17:08:10 -07001002
Eric Dumazet8a533662012-02-09 16:13:19 -05001003 rcu_read_unlock();
David S. Miller4c9483b2011-03-12 16:22:43 -05001004 ifp = ipv6_get_ifaddr(net, &fl6->saddr,
Neil Hormane550dfb2008-09-09 13:51:35 -07001005 (*dst)->dev, 1);
Neil Horman95c385b2007-04-25 17:08:10 -07001006
Neil Hormane550dfb2008-09-09 13:51:35 -07001007 redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
1008 if (ifp)
1009 in6_ifa_put(ifp);
Neil Horman95c385b2007-04-25 17:08:10 -07001010
Neil Hormane550dfb2008-09-09 13:51:35 -07001011 if (redirect) {
1012 /*
1013 * We need to get the dst entry for the
1014 * default router instead
1015 */
1016 dst_release(*dst);
David S. Miller4c9483b2011-03-12 16:22:43 -05001017 memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
1018 memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
1019 *dst = ip6_route_output(net, sk, &fl_gw6);
Neil Hormane550dfb2008-09-09 13:51:35 -07001020 if ((err = (*dst)->error))
1021 goto out_err_release;
Neil Horman95c385b2007-04-25 17:08:10 -07001022 }
Eric Dumazet8a533662012-02-09 16:13:19 -05001023 } else {
1024 rcu_read_unlock();
Neil Hormane550dfb2008-09-09 13:51:35 -07001025 }
Neil Horman95c385b2007-04-25 17:08:10 -07001026#endif
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return 0;
1029
1030out_err_release:
Mitsuru Chinenca46f9c2007-12-05 22:31:47 -08001031 if (err == -ENETUNREACH)
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001032 IP6_INC_STATS_BH(net, NULL, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 dst_release(*dst);
1034 *dst = NULL;
1035 return err;
1036}
Adrian Bunk34a0b3c2005-11-29 16:28:56 -08001037
Herbert Xu497c6152006-07-30 20:19:33 -07001038/**
1039 * ip6_dst_lookup - perform route lookup on flow
1040 * @sk: socket which provides route info
1041 * @dst: pointer to dst_entry * for result
David S. Miller4c9483b2011-03-12 16:22:43 -05001042 * @fl6: flow to lookup
Herbert Xu497c6152006-07-30 20:19:33 -07001043 *
1044 * This function performs a route lookup on the given flow.
1045 *
1046 * It returns zero on success, or a standard errno code on error.
1047 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001048int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi6 *fl6)
Herbert Xu497c6152006-07-30 20:19:33 -07001049{
1050 *dst = NULL;
David S. Miller4c9483b2011-03-12 16:22:43 -05001051 return ip6_dst_lookup_tail(sk, dst, fl6);
Herbert Xu497c6152006-07-30 20:19:33 -07001052}
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -08001053EXPORT_SYMBOL_GPL(ip6_dst_lookup);
1054
Herbert Xu497c6152006-07-30 20:19:33 -07001055/**
David S. Miller68d0c6d2011-03-01 13:19:07 -08001056 * ip6_dst_lookup_flow - perform route lookup on flow with ipsec
1057 * @sk: socket which provides route info
David S. Miller4c9483b2011-03-12 16:22:43 -05001058 * @fl6: flow to lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001059 * @final_dst: final destination address for ipsec lookup
David S. Millera1414712011-03-01 14:32:04 -08001060 * @can_sleep: we are in a sleepable context
David S. Miller68d0c6d2011-03-01 13:19:07 -08001061 *
1062 * This function performs a route lookup on the given flow.
1063 *
1064 * It returns a valid dst pointer on success, or a pointer encoded
1065 * error code.
1066 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001067struct dst_entry *ip6_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
David S. Miller68d0c6d2011-03-01 13:19:07 -08001068 const struct in6_addr *final_dst,
David S. Millera1414712011-03-01 14:32:04 -08001069 bool can_sleep)
David S. Miller68d0c6d2011-03-01 13:19:07 -08001070{
1071 struct dst_entry *dst = NULL;
1072 int err;
1073
David S. Miller4c9483b2011-03-12 16:22:43 -05001074 err = ip6_dst_lookup_tail(sk, &dst, fl6);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001075 if (err)
1076 return ERR_PTR(err);
1077 if (final_dst)
David S. Miller4c9483b2011-03-12 16:22:43 -05001078 ipv6_addr_copy(&fl6->daddr, final_dst);
David S. Miller2774c132011-03-01 14:59:04 -08001079 if (can_sleep)
David S. Miller4c9483b2011-03-12 16:22:43 -05001080 fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
David S. Miller2774c132011-03-01 14:59:04 -08001081
David S. Miller4c9483b2011-03-12 16:22:43 -05001082 return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001083}
1084EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
1085
1086/**
1087 * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
1088 * @sk: socket which provides the dst cache and route info
David S. Miller4c9483b2011-03-12 16:22:43 -05001089 * @fl6: flow to lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001090 * @final_dst: final destination address for ipsec lookup
David S. Millera1414712011-03-01 14:32:04 -08001091 * @can_sleep: we are in a sleepable context
Herbert Xu497c6152006-07-30 20:19:33 -07001092 *
1093 * This function performs a route lookup on the given flow with the
1094 * possibility of using the cached route in the socket if it is valid.
1095 * It will take the socket dst lock when operating on the dst cache.
1096 * As a result, this function can only be used in process context.
1097 *
David S. Miller68d0c6d2011-03-01 13:19:07 -08001098 * It returns a valid dst pointer on success, or a pointer encoded
1099 * error code.
Herbert Xu497c6152006-07-30 20:19:33 -07001100 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001101struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
David S. Miller68d0c6d2011-03-01 13:19:07 -08001102 const struct in6_addr *final_dst,
David S. Millera1414712011-03-01 14:32:04 -08001103 bool can_sleep)
Herbert Xu497c6152006-07-30 20:19:33 -07001104{
David S. Miller68d0c6d2011-03-01 13:19:07 -08001105 struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
1106 int err;
Herbert Xu497c6152006-07-30 20:19:33 -07001107
David S. Miller4c9483b2011-03-12 16:22:43 -05001108 dst = ip6_sk_dst_check(sk, dst, fl6);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001109
David S. Miller4c9483b2011-03-12 16:22:43 -05001110 err = ip6_dst_lookup_tail(sk, &dst, fl6);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001111 if (err)
1112 return ERR_PTR(err);
1113 if (final_dst)
David S. Miller4c9483b2011-03-12 16:22:43 -05001114 ipv6_addr_copy(&fl6->daddr, final_dst);
David S. Miller2774c132011-03-01 14:59:04 -08001115 if (can_sleep)
David S. Miller4c9483b2011-03-12 16:22:43 -05001116 fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
David S. Miller2774c132011-03-01 14:59:04 -08001117
David S. Miller4c9483b2011-03-12 16:22:43 -05001118 return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
Herbert Xu497c6152006-07-30 20:19:33 -07001119}
David S. Miller68d0c6d2011-03-01 13:19:07 -08001120EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
Herbert Xu497c6152006-07-30 20:19:33 -07001121
Adrian Bunk34a0b3c2005-11-29 16:28:56 -08001122static inline int ip6_ufo_append_data(struct sock *sk,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001123 int getfrag(void *from, char *to, int offset, int len,
1124 int odd, struct sk_buff *skb),
1125 void *from, int length, int hh_len, int fragheaderlen,
Eric Dumazetef81bb42011-08-08 23:44:00 -07001126 int transhdrlen, int mtu,unsigned int flags,
1127 struct rt6_info *rt)
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001128
1129{
1130 struct sk_buff *skb;
1131 int err;
1132
1133 /* There is support for UDP large send offload by network
1134 * device, so create one single skb packet containing complete
1135 * udp datagram
1136 */
1137 if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) {
1138 skb = sock_alloc_send_skb(sk,
1139 hh_len + fragheaderlen + transhdrlen + 20,
1140 (flags & MSG_DONTWAIT), &err);
1141 if (skb == NULL)
1142 return -ENOMEM;
1143
1144 /* reserve space for Hardware header */
1145 skb_reserve(skb, hh_len);
1146
1147 /* create space for UDP/IP header */
1148 skb_put(skb,fragheaderlen + transhdrlen);
1149
1150 /* initialize network header pointer */
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07001151 skb_reset_network_header(skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001152
1153 /* initialize protocol header pointer */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001154 skb->transport_header = skb->network_header + fragheaderlen;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001155
Patrick McHardy84fa7932006-08-29 16:44:56 -07001156 skb->ip_summed = CHECKSUM_PARTIAL;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001157 skb->csum = 0;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001158 }
1159
1160 err = skb_append_datato_frags(sk,skb, getfrag, from,
1161 (length - transhdrlen));
1162 if (!err) {
1163 struct frag_hdr fhdr;
1164
Sridhar Samudralac31d5322009-07-09 08:09:58 +00001165 /* Specify the length of each IPv6 datagram fragment.
1166 * It has to be a multiple of 8.
1167 */
1168 skb_shinfo(skb)->gso_size = (mtu - fragheaderlen -
1169 sizeof(struct frag_hdr)) & ~7;
Herbert Xuf83ef8c2006-06-30 13:37:03 -07001170 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Jason Wanga1b7ab02011-10-09 10:56:44 +08001171 ipv6_select_ident(&fhdr, &rt->rt6i_dst.addr);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001172 skb_shinfo(skb)->ip6_frag_id = fhdr.identification;
1173 __skb_queue_tail(&sk->sk_write_queue, skb);
1174
1175 return 0;
1176 }
1177 /* There is not enough support do UPD LSO,
1178 * so follow normal path
1179 */
1180 kfree_skb(skb);
1181
1182 return err;
1183}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Herbert Xu0178b692009-02-05 15:15:50 -08001185static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
1186 gfp_t gfp)
1187{
1188 return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
1189}
1190
1191static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
1192 gfp_t gfp)
1193{
1194 return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
1195}
1196
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001197int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
1198 int offset, int len, int odd, struct sk_buff *skb),
1199 void *from, int length, int transhdrlen,
David S. Miller4c9483b2011-03-12 16:22:43 -05001200 int hlimit, int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6,
Brian Haley13b52cd2010-04-23 11:26:08 +00001201 struct rt6_info *rt, unsigned int flags, int dontfrag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 struct inet_sock *inet = inet_sk(sk);
1204 struct ipv6_pinfo *np = inet6_sk(sk);
David S. Millerbdc712b2011-05-06 15:02:07 -07001205 struct inet_cork *cork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 struct sk_buff *skb;
1207 unsigned int maxfraglen, fragheaderlen;
1208 int exthdrlen;
1209 int hh_len;
1210 int mtu;
1211 int copy;
1212 int err;
1213 int offset = 0;
1214 int csummode = CHECKSUM_NONE;
Anders Berggrena693e692011-02-28 12:32:11 -08001215 __u8 tx_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 if (flags&MSG_PROBE)
1218 return 0;
David S. Millerbdc712b2011-05-06 15:02:07 -07001219 cork = &inet->cork.base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (skb_queue_empty(&sk->sk_write_queue)) {
1221 /*
1222 * setup for corking
1223 */
1224 if (opt) {
Herbert Xu0178b692009-02-05 15:15:50 -08001225 if (WARN_ON(np->cork.opt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return -EINVAL;
Herbert Xu0178b692009-02-05 15:15:50 -08001227
1228 np->cork.opt = kmalloc(opt->tot_len, sk->sk_allocation);
1229 if (unlikely(np->cork.opt == NULL))
1230 return -ENOBUFS;
1231
1232 np->cork.opt->tot_len = opt->tot_len;
1233 np->cork.opt->opt_flen = opt->opt_flen;
1234 np->cork.opt->opt_nflen = opt->opt_nflen;
1235
1236 np->cork.opt->dst0opt = ip6_opt_dup(opt->dst0opt,
1237 sk->sk_allocation);
1238 if (opt->dst0opt && !np->cork.opt->dst0opt)
1239 return -ENOBUFS;
1240
1241 np->cork.opt->dst1opt = ip6_opt_dup(opt->dst1opt,
1242 sk->sk_allocation);
1243 if (opt->dst1opt && !np->cork.opt->dst1opt)
1244 return -ENOBUFS;
1245
1246 np->cork.opt->hopopt = ip6_opt_dup(opt->hopopt,
1247 sk->sk_allocation);
1248 if (opt->hopopt && !np->cork.opt->hopopt)
1249 return -ENOBUFS;
1250
1251 np->cork.opt->srcrt = ip6_rthdr_dup(opt->srcrt,
1252 sk->sk_allocation);
1253 if (opt->srcrt && !np->cork.opt->srcrt)
1254 return -ENOBUFS;
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /* need source address above miyazawa*/
1257 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001258 dst_hold(&rt->dst);
David S. Millerbdc712b2011-05-06 15:02:07 -07001259 cork->dst = &rt->dst;
David S. Miller4c9483b2011-03-12 16:22:43 -05001260 inet->cork.fl.u.ip6 = *fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 np->cork.hop_limit = hlimit;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001262 np->cork.tclass = tclass;
John Heffner628a5c52007-04-20 15:53:27 -07001263 mtu = np->pmtudisc == IPV6_PMTUDISC_PROBE ?
Changli Gaod8d1f302010-06-10 23:31:35 -07001264 rt->dst.dev->mtu : dst_mtu(rt->dst.path);
Dave Jonesc7503602006-03-20 22:44:52 -08001265 if (np->frag_size < mtu) {
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -08001266 if (np->frag_size)
1267 mtu = np->frag_size;
1268 }
David S. Millerbdc712b2011-05-06 15:02:07 -07001269 cork->fragsize = mtu;
Changli Gaod8d1f302010-06-10 23:31:35 -07001270 if (dst_allfrag(rt->dst.path))
David S. Millerbdc712b2011-05-06 15:02:07 -07001271 cork->flags |= IPCORK_ALLFRAG;
1272 cork->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 sk->sk_sndmsg_page = NULL;
1274 sk->sk_sndmsg_off = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001275 exthdrlen = rt->dst.header_len + (opt ? opt->opt_flen : 0) -
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001276 rt->rt6i_nfheader_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 length += exthdrlen;
1278 transhdrlen += exthdrlen;
1279 } else {
David S. Millerbdc712b2011-05-06 15:02:07 -07001280 rt = (struct rt6_info *)cork->dst;
David S. Miller4c9483b2011-03-12 16:22:43 -05001281 fl6 = &inet->cork.fl.u.ip6;
Herbert Xu0178b692009-02-05 15:15:50 -08001282 opt = np->cork.opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 transhdrlen = 0;
1284 exthdrlen = 0;
David S. Millerbdc712b2011-05-06 15:02:07 -07001285 mtu = cork->fragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287
Changli Gaod8d1f302010-06-10 23:31:35 -07001288 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001290 fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
Herbert Xub4ce9272007-11-13 21:33:32 -08001291 (opt ? opt->opt_nflen : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
1293
1294 if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
David S. Millerbdc712b2011-05-06 15:02:07 -07001295 if (cork->length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001296 ipv6_local_error(sk, EMSGSIZE, fl6, mtu-exthdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -EMSGSIZE;
1298 }
1299 }
1300
Anders Berggrena693e692011-02-28 12:32:11 -08001301 /* For UDP, check if TX timestamp is enabled */
1302 if (sk->sk_type == SOCK_DGRAM) {
1303 err = sock_tx_timestamp(sk, &tx_flags);
1304 if (err)
1305 goto error;
1306 }
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /*
1309 * Let's try using as much space as possible.
1310 * Use MTU if total length of the message fits into the MTU.
1311 * Otherwise, we need to reserve fragment header and
1312 * fragment alignment (= 8-15 octects, in total).
1313 *
1314 * Note that we may need to "move" the data from the tail of
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001315 * of the buffer to the new fragment when we split
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 * the message.
1317 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001318 * FIXME: It may be fragmented into multiple chunks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 * at once if non-fragmentable extension headers
1320 * are too large.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001321 * --yoshfuji
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 */
1323
David S. Millerbdc712b2011-05-06 15:02:07 -07001324 cork->length += length;
Brian Haley4b340ae2010-04-23 11:26:09 +00001325 if (length > mtu) {
1326 int proto = sk->sk_protocol;
1327 if (dontfrag && (proto == IPPROTO_UDP || proto == IPPROTO_RAW)){
David S. Miller4c9483b2011-03-12 16:22:43 -05001328 ipv6_local_rxpmtu(sk, fl6, mtu-exthdrlen);
Brian Haley4b340ae2010-04-23 11:26:09 +00001329 return -EMSGSIZE;
1330 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001331
Brian Haley4b340ae2010-04-23 11:26:09 +00001332 if (proto == IPPROTO_UDP &&
Changli Gaod8d1f302010-06-10 23:31:35 -07001333 (rt->dst.dev->features & NETIF_F_UFO)) {
Brian Haley4b340ae2010-04-23 11:26:09 +00001334
1335 err = ip6_ufo_append_data(sk, getfrag, from, length,
1336 hh_len, fragheaderlen,
Eric Dumazetef81bb42011-08-08 23:44:00 -07001337 transhdrlen, mtu, flags, rt);
Brian Haley4b340ae2010-04-23 11:26:09 +00001338 if (err)
1339 goto error;
1340 return 0;
1341 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
1345 goto alloc_new_skb;
1346
1347 while (length > 0) {
1348 /* Check if the remaining data fits into current packet. */
David S. Millerbdc712b2011-05-06 15:02:07 -07001349 copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (copy < length)
1351 copy = maxfraglen - skb->len;
1352
1353 if (copy <= 0) {
1354 char *data;
1355 unsigned int datalen;
1356 unsigned int fraglen;
1357 unsigned int fraggap;
1358 unsigned int alloclen;
1359 struct sk_buff *skb_prev;
1360alloc_new_skb:
1361 skb_prev = skb;
1362
1363 /* There's no room in the current skb */
1364 if (skb_prev)
1365 fraggap = skb_prev->len - maxfraglen;
1366 else
1367 fraggap = 0;
1368
1369 /*
1370 * If remaining data exceeds the mtu,
1371 * we know we need more fragment(s).
1372 */
1373 datalen = length + fraggap;
David S. Millerbdc712b2011-05-06 15:02:07 -07001374 if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 datalen = maxfraglen - fragheaderlen;
1376
1377 fraglen = datalen + fragheaderlen;
1378 if ((flags & MSG_MORE) &&
Changli Gaod8d1f302010-06-10 23:31:35 -07001379 !(rt->dst.dev->features&NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 alloclen = mtu;
1381 else
1382 alloclen = datalen + fragheaderlen;
1383
1384 /*
1385 * The last fragment gets additional space at tail.
1386 * Note: we overallocate on fragments with MSG_MODE
1387 * because we have no idea if we're the last one.
1388 */
1389 if (datalen == length + fraggap)
Changli Gaod8d1f302010-06-10 23:31:35 -07001390 alloclen += rt->dst.trailer_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /*
1393 * We just reserve space for fragment header.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001394 * Note: this may be overallocation if the message
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 * (without MSG_MORE) fits into the MTU.
1396 */
1397 alloclen += sizeof(struct frag_hdr);
1398
1399 if (transhdrlen) {
1400 skb = sock_alloc_send_skb(sk,
1401 alloclen + hh_len,
1402 (flags & MSG_DONTWAIT), &err);
1403 } else {
1404 skb = NULL;
1405 if (atomic_read(&sk->sk_wmem_alloc) <=
1406 2 * sk->sk_sndbuf)
1407 skb = sock_wmalloc(sk,
1408 alloclen + hh_len, 1,
1409 sk->sk_allocation);
1410 if (unlikely(skb == NULL))
1411 err = -ENOBUFS;
Anders Berggrena693e692011-02-28 12:32:11 -08001412 else {
1413 /* Only the initial fragment
1414 * is time stamped.
1415 */
1416 tx_flags = 0;
1417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419 if (skb == NULL)
1420 goto error;
1421 /*
1422 * Fill in the control structures
1423 */
1424 skb->ip_summed = csummode;
1425 skb->csum = 0;
1426 /* reserve for fragmentation */
1427 skb_reserve(skb, hh_len+sizeof(struct frag_hdr));
1428
Anders Berggrena693e692011-02-28 12:32:11 -08001429 if (sk->sk_type == SOCK_DGRAM)
1430 skb_shinfo(skb)->tx_flags = tx_flags;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /*
1433 * Find where to start putting bytes
1434 */
1435 data = skb_put(skb, fraglen);
Arnaldo Carvalho de Meloc14d2452007-03-11 22:39:41 -03001436 skb_set_network_header(skb, exthdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 data += fragheaderlen;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001438 skb->transport_header = (skb->network_header +
1439 fragheaderlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (fraggap) {
1441 skb->csum = skb_copy_and_csum_bits(
1442 skb_prev, maxfraglen,
1443 data + transhdrlen, fraggap, 0);
1444 skb_prev->csum = csum_sub(skb_prev->csum,
1445 skb->csum);
1446 data += fraggap;
Herbert Xue9fa4f72006-08-13 20:12:58 -07001447 pskb_trim_unique(skb_prev, maxfraglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449 copy = datalen - transhdrlen - fraggap;
1450 if (copy < 0) {
1451 err = -EINVAL;
1452 kfree_skb(skb);
1453 goto error;
1454 } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
1455 err = -EFAULT;
1456 kfree_skb(skb);
1457 goto error;
1458 }
1459
1460 offset += copy;
1461 length -= datalen - fraggap;
1462 transhdrlen = 0;
1463 exthdrlen = 0;
1464 csummode = CHECKSUM_NONE;
1465
1466 /*
1467 * Put the packet on the pending queue
1468 */
1469 __skb_queue_tail(&sk->sk_write_queue, skb);
1470 continue;
1471 }
1472
1473 if (copy > length)
1474 copy = length;
1475
Changli Gaod8d1f302010-06-10 23:31:35 -07001476 if (!(rt->dst.dev->features&NETIF_F_SG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 unsigned int off;
1478
1479 off = skb->len;
1480 if (getfrag(from, skb_put(skb, copy),
1481 offset, copy, off, skb) < 0) {
1482 __skb_trim(skb, off);
1483 err = -EFAULT;
1484 goto error;
1485 }
1486 } else {
1487 int i = skb_shinfo(skb)->nr_frags;
1488 skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
1489 struct page *page = sk->sk_sndmsg_page;
1490 int off = sk->sk_sndmsg_off;
1491 unsigned int left;
1492
1493 if (page && (left = PAGE_SIZE - off) > 0) {
1494 if (copy >= left)
1495 copy = left;
1496 if (page != frag->page) {
1497 if (i == MAX_SKB_FRAGS) {
1498 err = -EMSGSIZE;
1499 goto error;
1500 }
1501 get_page(page);
1502 skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0);
1503 frag = &skb_shinfo(skb)->frags[i];
1504 }
1505 } else if(i < MAX_SKB_FRAGS) {
1506 if (copy > PAGE_SIZE)
1507 copy = PAGE_SIZE;
1508 page = alloc_pages(sk->sk_allocation, 0);
1509 if (page == NULL) {
1510 err = -ENOMEM;
1511 goto error;
1512 }
1513 sk->sk_sndmsg_page = page;
1514 sk->sk_sndmsg_off = 0;
1515
1516 skb_fill_page_desc(skb, i, page, 0, 0);
1517 frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 } else {
1519 err = -EMSGSIZE;
1520 goto error;
1521 }
1522 if (getfrag(from, page_address(frag->page)+frag->page_offset+frag->size, offset, copy, skb->len, skb) < 0) {
1523 err = -EFAULT;
1524 goto error;
1525 }
1526 sk->sk_sndmsg_off += copy;
1527 frag->size += copy;
1528 skb->len += copy;
1529 skb->data_len += copy;
Herbert Xuf945fa72008-01-22 22:39:26 -08001530 skb->truesize += copy;
1531 atomic_add(copy, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 offset += copy;
1534 length -= copy;
1535 }
1536 return 0;
1537error:
David S. Millerbdc712b2011-05-06 15:02:07 -07001538 cork->length -= length;
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001539 IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return err;
1541}
1542
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001543static void ip6_cork_release(struct inet_sock *inet, struct ipv6_pinfo *np)
1544{
Herbert Xu0178b692009-02-05 15:15:50 -08001545 if (np->cork.opt) {
1546 kfree(np->cork.opt->dst0opt);
1547 kfree(np->cork.opt->dst1opt);
1548 kfree(np->cork.opt->hopopt);
1549 kfree(np->cork.opt->srcrt);
1550 kfree(np->cork.opt);
1551 np->cork.opt = NULL;
1552 }
1553
David S. Millerbdc712b2011-05-06 15:02:07 -07001554 if (inet->cork.base.dst) {
1555 dst_release(inet->cork.base.dst);
1556 inet->cork.base.dst = NULL;
1557 inet->cork.base.flags &= ~IPCORK_ALLFRAG;
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001558 }
1559 memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
1560}
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562int ip6_push_pending_frames(struct sock *sk)
1563{
1564 struct sk_buff *skb, *tmp_skb;
1565 struct sk_buff **tail_skb;
1566 struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
1567 struct inet_sock *inet = inet_sk(sk);
1568 struct ipv6_pinfo *np = inet6_sk(sk);
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001569 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 struct ipv6hdr *hdr;
1571 struct ipv6_txoptions *opt = np->cork.opt;
David S. Millerbdc712b2011-05-06 15:02:07 -07001572 struct rt6_info *rt = (struct rt6_info *)inet->cork.base.dst;
David S. Miller4c9483b2011-03-12 16:22:43 -05001573 struct flowi6 *fl6 = &inet->cork.fl.u.ip6;
1574 unsigned char proto = fl6->flowi6_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 int err = 0;
1576
1577 if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
1578 goto out;
1579 tail_skb = &(skb_shinfo(skb)->frag_list);
1580
1581 /* move skb->data to ip header from ext header */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001582 if (skb->data < skb_network_header(skb))
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001583 __skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001585 __skb_pull(tmp_skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 *tail_skb = tmp_skb;
1587 tail_skb = &(tmp_skb->next);
1588 skb->len += tmp_skb->len;
1589 skb->data_len += tmp_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 skb->truesize += tmp_skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 tmp_skb->destructor = NULL;
1592 tmp_skb->sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
1594
Herbert Xu28a89452008-02-12 18:07:27 -08001595 /* Allow local fragmentation. */
Herbert Xub5c15fc2008-02-14 23:49:37 -08001596 if (np->pmtudisc < IPV6_PMTUDISC_DO)
Herbert Xu28a89452008-02-12 18:07:27 -08001597 skb->local_df = 1;
1598
David S. Miller4c9483b2011-03-12 16:22:43 -05001599 ipv6_addr_copy(final_dst, &fl6->daddr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001600 __skb_pull(skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (opt && opt->opt_flen)
1602 ipv6_push_frag_opts(skb, opt, &proto);
1603 if (opt && opt->opt_nflen)
1604 ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
1605
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001606 skb_push(skb, sizeof(struct ipv6hdr));
1607 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001608 hdr = ipv6_hdr(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001609
David S. Miller4c9483b2011-03-12 16:22:43 -05001610 *(__be32*)hdr = fl6->flowlabel |
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001611 htonl(0x60000000 | ((int)np->cork.tclass << 20));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 hdr->hop_limit = np->cork.hop_limit;
1614 hdr->nexthdr = proto;
David S. Miller4c9483b2011-03-12 16:22:43 -05001615 ipv6_addr_copy(&hdr->saddr, &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 ipv6_addr_copy(&hdr->daddr, final_dst);
1617
Patrick McHardya2c20642006-01-08 22:37:26 -08001618 skb->priority = sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -08001619 skb->mark = sk->sk_mark;
Patrick McHardya2c20642006-01-08 22:37:26 -08001620
Changli Gaod8d1f302010-06-10 23:31:35 -07001621 skb_dst_set(skb, dst_clone(&rt->dst));
Neil Hormanedf391f2009-04-27 02:45:02 -07001622 IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
David L Stevens14878f72007-09-16 16:52:35 -07001623 if (proto == IPPROTO_ICMPV6) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001624 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
David L Stevens14878f72007-09-16 16:52:35 -07001625
Denis V. Lunev5a57d4c2008-10-08 10:34:14 -07001626 ICMP6MSGOUT_INC_STATS_BH(net, idev, icmp6_hdr(skb)->icmp6_type);
Denis V. Luneve41b5362008-10-08 10:33:26 -07001627 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
David L Stevens14878f72007-09-16 16:52:35 -07001628 }
1629
Herbert Xuef76bc22008-01-11 19:15:08 -08001630 err = ip6_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (err) {
1632 if (err > 0)
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -07001633 err = net_xmit_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (err)
1635 goto error;
1636 }
1637
1638out:
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001639 ip6_cork_release(inet, np);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return err;
1641error:
Eric Dumazet06254912009-09-01 18:37:16 -07001642 IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 goto out;
1644}
1645
1646void ip6_flush_pending_frames(struct sock *sk)
1647{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 struct sk_buff *skb;
1649
1650 while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001651 if (skb_dst(skb))
1652 IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakie1f522082007-09-11 11:31:43 +02001653 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 kfree_skb(skb);
1655 }
1656
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001657 ip6_cork_release(inet_sk(sk), inet6_sk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}