blob: d1f049bc48c79bb25a5681a0d18e0869370d3e84 [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? */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455 if ((net->ipv6.devconf_all->proxy_ndp == 1 &&
456 pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0))
457 || net->ipv6.devconf_all->proxy_ndp >= 2) {
Ville Nuorvala74553b02006-09-22 14:42:18 -0700458 int proxied = ip6_forward_proxy_check(skb);
459 if (proxied > 0)
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700460 return ip6_input(skb);
Ville Nuorvala74553b02006-09-22 14:42:18 -0700461 else if (proxied < 0) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700462 IP6_INC_STATS(net, ip6_dst_idev(dst),
463 IPSTATS_MIB_INDISCARDS);
Ville Nuorvala74553b02006-09-22 14:42:18 -0700464 goto drop;
465 }
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700466 }
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!xfrm6_route_forward(skb)) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700469 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 goto drop;
471 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000472 dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /* IPv6 specs say nothing about it, but it is clear that we cannot
475 send redirects to source routed frames.
Masahide NAKAMURA1e5dc142007-08-24 19:08:55 +0900476 We don't send redirects to frames decapsulated from IPsec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 */
Eric Dumazet8a533662012-02-09 16:13:19 -0500478 n = dst_get_neighbour(dst);
479 if (skb->dev == dst->dev && n && opt->srcrt == 0 && !skb_sec_path(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct in6_addr *target = NULL;
481 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /*
484 * incoming and outgoing devices are the same
485 * send a redirect.
486 */
487
488 rt = (struct rt6_info *) dst;
489 if ((rt->rt6i_flags & RTF_GATEWAY))
490 target = (struct in6_addr*)&n->primary_key;
491 else
492 target = &hdr->daddr;
493
David S. Miller92d86822011-02-04 15:55:25 -0800494 if (!rt->rt6i_peer)
495 rt6_bind_peer(rt, 1);
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /* Limit redirects both by destination (here)
498 and by source (inside ndisc_send_redirect)
499 */
David S. Miller92d86822011-02-04 15:55:25 -0800500 if (inet_peer_xrlim_allow(rt->rt6i_peer, 1*HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 ndisc_send_redirect(skb, n, target);
David L Stevens5bb1ab02007-05-09 13:53:44 -0700502 } else {
503 int addrtype = ipv6_addr_type(&hdr->saddr);
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* This check is security critical. */
YOSHIFUJI Hideakif81b2e72008-06-25 16:55:26 +0900506 if (addrtype == IPV6_ADDR_ANY ||
507 addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
David L Stevens5bb1ab02007-05-09 13:53:44 -0700508 goto error;
509 if (addrtype & IPV6_ADDR_LINKLOCAL) {
510 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000511 ICMPV6_NOT_NEIGHBOUR, 0);
David L Stevens5bb1ab02007-05-09 13:53:44 -0700512 goto error;
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800516 mtu = dst_mtu(dst);
517 if (mtu < IPV6_MIN_MTU)
518 mtu = IPV6_MIN_MTU;
519
Herbert Xu0aa68272010-05-27 16:14:30 -0700520 if (skb->len > mtu && !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* Again, force OUTPUT device used as source address */
522 skb->dev = dst->dev;
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800523 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700524 IP6_INC_STATS_BH(net,
525 ip6_dst_idev(dst), IPSTATS_MIB_INTOOBIGERRORS);
526 IP6_INC_STATS_BH(net,
527 ip6_dst_idev(dst), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 kfree_skb(skb);
529 return -EMSGSIZE;
530 }
531
532 if (skb_cow(skb, dst->dev->hard_header_len)) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700533 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto drop;
535 }
536
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700537 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 /* Mangling hops number delayed to point after skb COW */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 hdr->hop_limit--;
542
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700543 IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
Jan Engelhardtb2e0b382010-03-23 04:09:07 +0100544 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dst->dev,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800545 ip6_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547error:
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700548 IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549drop:
550 kfree_skb(skb);
551 return -EINVAL;
552}
553
554static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
555{
556 to->pkt_type = from->pkt_type;
557 to->priority = from->priority;
558 to->protocol = from->protocol;
Eric Dumazetadf30902009-06-02 05:19:30 +0000559 skb_dst_drop(to);
560 skb_dst_set(to, dst_clone(skb_dst(from)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 to->dev = from->dev;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800562 to->mark = from->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564#ifdef CONFIG_NET_SCHED
565 to->tc_index = from->tc_index;
566#endif
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -0700567 nf_copy(to, from);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700568#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
569 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
570 to->nf_trace = from->nf_trace;
571#endif
James Morris984bc162006-06-09 00:29:17 -0700572 skb_copy_secmark(to, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
576{
577 u16 offset = sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700578 struct ipv6_opt_hdr *exthdr =
579 (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700580 unsigned int packet_len = skb->tail - skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 int found_rhdr = 0;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700582 *nexthdr = &ipv6_hdr(skb)->nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 while (offset + 1 <= packet_len) {
585
586 switch (**nexthdr) {
587
588 case NEXTHDR_HOP:
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700589 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 case NEXTHDR_ROUTING:
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700591 found_rhdr = 1;
592 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 case NEXTHDR_DEST:
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -0700594#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700595 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
596 break;
597#endif
598 if (found_rhdr)
599 return offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
601 default :
602 return offset;
603 }
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700604
605 offset += ipv6_optlen(exthdr);
606 *nexthdr = &exthdr->nexthdr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700607 exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
608 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
611 return offset;
612}
613
Eric Dumazetef81bb42011-08-08 23:44:00 -0700614static u32 hashidentrnd __read_mostly;
615#define FID_HASH_SZ 16
616static u32 ipv6_fragmentation_id[FID_HASH_SZ];
617
618void __init initialize_hashidentrnd(void)
619{
620 get_random_bytes(&hashidentrnd, sizeof(hashidentrnd));
621}
622
623static u32 __ipv6_select_ident(const struct in6_addr *addr)
624{
625 u32 newid, oldid, hash = jhash2((u32 *)addr, 4, hashidentrnd);
626 u32 *pid = &ipv6_fragmentation_id[hash % FID_HASH_SZ];
627
628 do {
629 oldid = *pid;
630 newid = oldid + 1;
631 if (!(hash + newid))
632 newid++;
633 } while (cmpxchg(pid, oldid, newid) != oldid);
634
635 return hash + newid;
636}
637
Jason Wanga1b7ab02011-10-09 10:56:44 +0800638void ipv6_select_ident(struct frag_hdr *fhdr, struct in6_addr *addr)
Eric Dumazetef81bb42011-08-08 23:44:00 -0700639{
Jason Wanga1b7ab02011-10-09 10:56:44 +0800640 fhdr->identification = htonl(__ipv6_select_ident(addr));
Eric Dumazetef81bb42011-08-08 23:44:00 -0700641}
642
David Stevensad0081e2010-12-17 11:42:42 +0000643int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 struct sk_buff *frag;
Eric Dumazetadf30902009-06-02 05:19:30 +0000646 struct rt6_info *rt = (struct rt6_info*)skb_dst(skb);
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -0800647 struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct ipv6hdr *tmp_hdr;
649 struct frag_hdr *fh;
650 unsigned int mtu, hlen, left, len;
Al Viroae08e1f2006-11-08 00:27:11 -0800651 __be32 frag_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 int ptr, offset = 0, err=0;
653 u8 *prevhdr, nexthdr = 0;
Eric Dumazetadf30902009-06-02 05:19:30 +0000654 struct net *net = dev_net(skb_dst(skb)->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 hlen = ip6_find_1stfragopt(skb, &prevhdr);
657 nexthdr = *prevhdr;
658
John Heffner628a5c52007-04-20 15:53:27 -0700659 mtu = ip6_skb_dst_mtu(skb);
John Heffnerb881ef72007-04-20 15:52:39 -0700660
661 /* We must not fragment if the socket is set to force MTU discovery
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800662 * or if the skb it not generated by a local socket.
John Heffnerb881ef72007-04-20 15:52:39 -0700663 */
Shan Weif2228f72010-04-18 16:58:22 +0000664 if (!skb->local_df && skb->len > mtu) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000665 skb->dev = skb_dst(skb)->dev;
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000666 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Eric Dumazetadf30902009-06-02 05:19:30 +0000667 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700668 IPSTATS_MIB_FRAGFAILS);
John Heffnerb881ef72007-04-20 15:52:39 -0700669 kfree_skb(skb);
670 return -EMSGSIZE;
671 }
672
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -0800673 if (np && np->frag_size < mtu) {
674 if (np->frag_size)
675 mtu = np->frag_size;
676 }
677 mtu -= hlen + sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
David S. Miller21dc3302010-08-23 00:13:46 -0700679 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int first_len = skb_pagelen(skb);
Eric Dumazet3d130082010-09-21 08:47:45 +0000681 struct sk_buff *frag2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if (first_len - hlen > mtu ||
684 ((first_len - hlen) & 7) ||
685 skb_cloned(skb))
686 goto slow_path;
687
David S. Miller4d9092b2009-06-09 00:20:05 -0700688 skb_walk_frags(skb, frag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* Correct geometry. */
690 if (frag->len > mtu ||
691 ((frag->len & 7) && frag->next) ||
692 skb_headroom(frag) < hlen)
Eric Dumazet3d130082010-09-21 08:47:45 +0000693 goto slow_path_clean;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* Partially cloned skb? */
696 if (skb_shared(frag))
Eric Dumazet3d130082010-09-21 08:47:45 +0000697 goto slow_path_clean;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700698
699 BUG_ON(frag->sk);
700 if (skb->sk) {
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700701 frag->sk = skb->sk;
702 frag->destructor = sock_wfree;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700703 }
Eric Dumazet3d130082010-09-21 08:47:45 +0000704 skb->truesize -= frag->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
707 err = 0;
708 offset = 0;
709 frag = skb_shinfo(skb)->frag_list;
David S. Miller4d9092b2009-06-09 00:20:05 -0700710 skb_frag_list_init(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* BUILD HEADER */
712
YOSHIFUJI Hideaki9a217a12006-12-05 13:47:21 -0800713 *prevhdr = NEXTHDR_FRAGMENT;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700714 tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (!tmp_hdr) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000716 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700717 IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return -ENOMEM;
719 }
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 __skb_pull(skb, hlen);
722 fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700723 __skb_push(skb, hlen);
724 skb_reset_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700725 memcpy(skb_network_header(skb), tmp_hdr, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Jason Wanga1b7ab02011-10-09 10:56:44 +0800727 ipv6_select_ident(fh, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 fh->nexthdr = nexthdr;
729 fh->reserved = 0;
730 fh->frag_off = htons(IP6_MF);
731 frag_id = fh->identification;
732
733 first_len = skb_pagelen(skb);
734 skb->data_len = first_len - skb_headlen(skb);
735 skb->len = first_len;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700736 ipv6_hdr(skb)->payload_len = htons(first_len -
737 sizeof(struct ipv6hdr));
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900738
Changli Gaod8d1f302010-06-10 23:31:35 -0700739 dst_hold(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 for (;;) {
742 /* Prepare header of the next frame,
743 * before previous one went down. */
744 if (frag) {
745 frag->ip_summed = CHECKSUM_NONE;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300746 skb_reset_transport_header(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700748 __skb_push(frag, hlen);
749 skb_reset_network_header(frag);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700750 memcpy(skb_network_header(frag), tmp_hdr,
751 hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 offset += skb->len - hlen - sizeof(struct frag_hdr);
753 fh->nexthdr = nexthdr;
754 fh->reserved = 0;
755 fh->frag_off = htons(offset);
756 if (frag->next != NULL)
757 fh->frag_off |= htons(IP6_MF);
758 fh->identification = frag_id;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700759 ipv6_hdr(frag)->payload_len =
760 htons(frag->len -
761 sizeof(struct ipv6hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ip6_copy_metadata(frag, skb);
763 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 err = output(skb);
Wei Dongdafee492006-08-02 13:41:21 -0700766 if(!err)
Changli Gaod8d1f302010-06-10 23:31:35 -0700767 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700768 IPSTATS_MIB_FRAGCREATES);
Wei Dongdafee492006-08-02 13:41:21 -0700769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (err || !frag)
771 break;
772
773 skb = frag;
774 frag = skb->next;
775 skb->next = NULL;
776 }
777
Jesper Juhla51482b2005-11-08 09:41:34 -0800778 kfree(tmp_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (err == 0) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700781 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700782 IPSTATS_MIB_FRAGOKS);
Changli Gaod8d1f302010-06-10 23:31:35 -0700783 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return 0;
785 }
786
787 while (frag) {
788 skb = frag->next;
789 kfree_skb(frag);
790 frag = skb;
791 }
792
Changli Gaod8d1f302010-06-10 23:31:35 -0700793 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700794 IPSTATS_MIB_FRAGFAILS);
Changli Gaod8d1f302010-06-10 23:31:35 -0700795 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return err;
Eric Dumazet3d130082010-09-21 08:47:45 +0000797
798slow_path_clean:
799 skb_walk_frags(skb, frag2) {
800 if (frag2 == frag)
801 break;
802 frag2->sk = NULL;
803 frag2->destructor = NULL;
804 skb->truesize += frag2->truesize;
805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
808slow_path:
809 left = skb->len - hlen; /* Space per frame */
810 ptr = hlen; /* Where to start from */
811
812 /*
813 * Fragment the datagram.
814 */
815
816 *prevhdr = NEXTHDR_FRAGMENT;
817
818 /*
819 * Keep copying data until we run out.
820 */
821 while(left > 0) {
822 len = left;
823 /* IF: it doesn't fit, use 'mtu' - the data space left */
824 if (len > mtu)
825 len = mtu;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300826 /* IF: we are not sending up to and including the packet end
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 then align the next start on an eight byte boundary */
828 if (len < left) {
829 len &= ~7;
830 }
831 /*
832 * Allocate buffer.
833 */
834
Changli Gaod8d1f302010-06-10 23:31:35 -0700835 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 -0700836 NETDEBUG(KERN_INFO "IPv6: frag: no memory for new fragment!\n");
Eric Dumazetadf30902009-06-02 05:19:30 +0000837 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900838 IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 err = -ENOMEM;
840 goto fail;
841 }
842
843 /*
844 * Set up data on packet
845 */
846
847 ip6_copy_metadata(frag, skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700848 skb_reserve(frag, LL_RESERVED_SPACE(rt->dst.dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 skb_put(frag, len + hlen + sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700850 skb_reset_network_header(frag);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300851 fh = (struct frag_hdr *)(skb_network_header(frag) + hlen);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700852 frag->transport_header = (frag->network_header + hlen +
853 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /*
856 * Charge the memory for the fragment to any owner
857 * it might possess
858 */
859 if (skb->sk)
860 skb_set_owner_w(frag, skb->sk);
861
862 /*
863 * Copy the packet header into the new buffer.
864 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300865 skb_copy_from_linear_data(skb, skb_network_header(frag), hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /*
868 * Build fragment header.
869 */
870 fh->nexthdr = nexthdr;
871 fh->reserved = 0;
Yan Zhengf36d6ab2005-10-03 14:19:15 -0700872 if (!frag_id) {
Jason Wanga1b7ab02011-10-09 10:56:44 +0800873 ipv6_select_ident(fh, &rt->rt6i_dst.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 frag_id = fh->identification;
875 } else
876 fh->identification = frag_id;
877
878 /*
879 * Copy a block of the IP datagram.
880 */
Wei Yongjun8984e412007-08-21 20:59:08 -0700881 if (skb_copy_bits(skb, ptr, skb_transport_header(frag), len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 BUG();
883 left -= len;
884
885 fh->frag_off = htons(offset);
886 if (left > 0)
887 fh->frag_off |= htons(IP6_MF);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700888 ipv6_hdr(frag)->payload_len = htons(frag->len -
889 sizeof(struct ipv6hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 ptr += len;
892 offset += len;
893
894 /*
895 * Put this fragment into the sending queue.
896 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 err = output(frag);
898 if (err)
899 goto fail;
Wei Dongdafee492006-08-02 13:41:21 -0700900
Eric Dumazetadf30902009-06-02 05:19:30 +0000901 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700902 IPSTATS_MIB_FRAGCREATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000904 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900905 IPSTATS_MIB_FRAGOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return err;
908
909fail:
Eric Dumazetadf30902009-06-02 05:19:30 +0000910 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900911 IPSTATS_MIB_FRAGFAILS);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900912 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return err;
914}
915
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000916static inline int ip6_rt_check(const struct rt6key *rt_key,
917 const struct in6_addr *fl_addr,
918 const struct in6_addr *addr_cache)
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700919{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000920 return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
921 (addr_cache == NULL || !ipv6_addr_equal(fl_addr, addr_cache));
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700922}
923
Herbert Xu497c6152006-07-30 20:19:33 -0700924static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
925 struct dst_entry *dst,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000926 const struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Herbert Xu497c6152006-07-30 20:19:33 -0700928 struct ipv6_pinfo *np = inet6_sk(sk);
929 struct rt6_info *rt = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Herbert Xu497c6152006-07-30 20:19:33 -0700931 if (!dst)
932 goto out;
933
934 /* Yes, checking route validity in not connected
935 * case is not very simple. Take into account,
936 * that we do not support routing by source, TOS,
937 * and MSG_DONTROUTE --ANK (980726)
938 *
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700939 * 1. ip6_rt_check(): If route was host route,
940 * check that cached destination is current.
Herbert Xu497c6152006-07-30 20:19:33 -0700941 * If it is network route, we still may
942 * check its validity using saved pointer
943 * to the last used address: daddr_cache.
944 * We do not want to save whole address now,
945 * (because main consumer of this service
946 * is tcp, which has not this problem),
947 * so that the last trick works only on connected
948 * sockets.
949 * 2. oif also should be the same.
950 */
David S. Miller4c9483b2011-03-12 16:22:43 -0500951 if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700952#ifdef CONFIG_IPV6_SUBTREES
David S. Miller4c9483b2011-03-12 16:22:43 -0500953 ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700954#endif
David S. Miller4c9483b2011-03-12 16:22:43 -0500955 (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex)) {
Herbert Xu497c6152006-07-30 20:19:33 -0700956 dst_release(dst);
957 dst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959
Herbert Xu497c6152006-07-30 20:19:33 -0700960out:
961 return dst;
962}
963
964static int ip6_dst_lookup_tail(struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500965 struct dst_entry **dst, struct flowi6 *fl6)
Herbert Xu497c6152006-07-30 20:19:33 -0700966{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900967 struct net *net = sock_net(sk);
Eric Dumazet8a533662012-02-09 16:13:19 -0500968#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
969 struct neighbour *n;
970#endif
971 int err;
Herbert Xu497c6152006-07-30 20:19:33 -0700972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (*dst == NULL)
David S. Miller4c9483b2011-03-12 16:22:43 -0500974 *dst = ip6_route_output(net, sk, fl6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if ((err = (*dst)->error))
977 goto out_err_release;
978
David S. Miller4c9483b2011-03-12 16:22:43 -0500979 if (ipv6_addr_any(&fl6->saddr)) {
Daniel Walterc3968a82011-04-13 21:10:57 +0000980 struct rt6_info *rt = (struct rt6_info *) *dst;
981 err = ip6_route_get_saddr(net, rt, &fl6->daddr,
982 sk ? inet6_sk(sk)->srcprefs : 0,
983 &fl6->saddr);
Olaf Hering44456d32005-07-27 11:45:17 -0700984 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 goto out_err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987
Neil Horman95c385b2007-04-25 17:08:10 -0700988#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Neil Hormane550dfb2008-09-09 13:51:35 -0700989 /*
990 * Here if the dst entry we've looked up
991 * has a neighbour entry that is in the INCOMPLETE
992 * state and the src address from the flow is
993 * marked as OPTIMISTIC, we release the found
994 * dst entry and replace it instead with the
995 * dst entry of the nexthop router
996 */
Eric Dumazet8a533662012-02-09 16:13:19 -0500997 rcu_read_lock();
998 n = dst_get_neighbour(*dst);
999 if (n && !(n->nud_state & NUD_VALID)) {
Neil Hormane550dfb2008-09-09 13:51:35 -07001000 struct inet6_ifaddr *ifp;
David S. Miller4c9483b2011-03-12 16:22:43 -05001001 struct flowi6 fl_gw6;
Neil Hormane550dfb2008-09-09 13:51:35 -07001002 int redirect;
Neil Horman95c385b2007-04-25 17:08:10 -07001003
Eric Dumazet8a533662012-02-09 16:13:19 -05001004 rcu_read_unlock();
David S. Miller4c9483b2011-03-12 16:22:43 -05001005 ifp = ipv6_get_ifaddr(net, &fl6->saddr,
Neil Hormane550dfb2008-09-09 13:51:35 -07001006 (*dst)->dev, 1);
Neil Horman95c385b2007-04-25 17:08:10 -07001007
Neil Hormane550dfb2008-09-09 13:51:35 -07001008 redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
1009 if (ifp)
1010 in6_ifa_put(ifp);
Neil Horman95c385b2007-04-25 17:08:10 -07001011
Neil Hormane550dfb2008-09-09 13:51:35 -07001012 if (redirect) {
1013 /*
1014 * We need to get the dst entry for the
1015 * default router instead
1016 */
1017 dst_release(*dst);
David S. Miller4c9483b2011-03-12 16:22:43 -05001018 memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
1019 memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
1020 *dst = ip6_route_output(net, sk, &fl_gw6);
Neil Hormane550dfb2008-09-09 13:51:35 -07001021 if ((err = (*dst)->error))
1022 goto out_err_release;
Neil Horman95c385b2007-04-25 17:08:10 -07001023 }
Eric Dumazet8a533662012-02-09 16:13:19 -05001024 } else {
1025 rcu_read_unlock();
Neil Hormane550dfb2008-09-09 13:51:35 -07001026 }
Neil Horman95c385b2007-04-25 17:08:10 -07001027#endif
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return 0;
1030
1031out_err_release:
Mitsuru Chinenca46f9c2007-12-05 22:31:47 -08001032 if (err == -ENETUNREACH)
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001033 IP6_INC_STATS_BH(net, NULL, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 dst_release(*dst);
1035 *dst = NULL;
1036 return err;
1037}
Adrian Bunk34a0b3c2005-11-29 16:28:56 -08001038
Herbert Xu497c6152006-07-30 20:19:33 -07001039/**
1040 * ip6_dst_lookup - perform route lookup on flow
1041 * @sk: socket which provides route info
1042 * @dst: pointer to dst_entry * for result
David S. Miller4c9483b2011-03-12 16:22:43 -05001043 * @fl6: flow to lookup
Herbert Xu497c6152006-07-30 20:19:33 -07001044 *
1045 * This function performs a route lookup on the given flow.
1046 *
1047 * It returns zero on success, or a standard errno code on error.
1048 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001049int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi6 *fl6)
Herbert Xu497c6152006-07-30 20:19:33 -07001050{
1051 *dst = NULL;
David S. Miller4c9483b2011-03-12 16:22:43 -05001052 return ip6_dst_lookup_tail(sk, dst, fl6);
Herbert Xu497c6152006-07-30 20:19:33 -07001053}
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -08001054EXPORT_SYMBOL_GPL(ip6_dst_lookup);
1055
Herbert Xu497c6152006-07-30 20:19:33 -07001056/**
David S. Miller68d0c6d2011-03-01 13:19:07 -08001057 * ip6_dst_lookup_flow - perform route lookup on flow with ipsec
1058 * @sk: socket which provides route info
David S. Miller4c9483b2011-03-12 16:22:43 -05001059 * @fl6: flow to lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001060 * @final_dst: final destination address for ipsec lookup
David S. Millera1414712011-03-01 14:32:04 -08001061 * @can_sleep: we are in a sleepable context
David S. Miller68d0c6d2011-03-01 13:19:07 -08001062 *
1063 * This function performs a route lookup on the given flow.
1064 *
1065 * It returns a valid dst pointer on success, or a pointer encoded
1066 * error code.
1067 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001068struct dst_entry *ip6_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
David S. Miller68d0c6d2011-03-01 13:19:07 -08001069 const struct in6_addr *final_dst,
David S. Millera1414712011-03-01 14:32:04 -08001070 bool can_sleep)
David S. Miller68d0c6d2011-03-01 13:19:07 -08001071{
1072 struct dst_entry *dst = NULL;
1073 int err;
1074
David S. Miller4c9483b2011-03-12 16:22:43 -05001075 err = ip6_dst_lookup_tail(sk, &dst, fl6);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001076 if (err)
1077 return ERR_PTR(err);
1078 if (final_dst)
David S. Miller4c9483b2011-03-12 16:22:43 -05001079 ipv6_addr_copy(&fl6->daddr, final_dst);
David S. Miller2774c132011-03-01 14:59:04 -08001080 if (can_sleep)
David S. Miller4c9483b2011-03-12 16:22:43 -05001081 fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
David S. Miller2774c132011-03-01 14:59:04 -08001082
David S. Miller4c9483b2011-03-12 16:22:43 -05001083 return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001084}
1085EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
1086
1087/**
1088 * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
1089 * @sk: socket which provides the dst cache and route info
David S. Miller4c9483b2011-03-12 16:22:43 -05001090 * @fl6: flow to lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001091 * @final_dst: final destination address for ipsec lookup
David S. Millera1414712011-03-01 14:32:04 -08001092 * @can_sleep: we are in a sleepable context
Herbert Xu497c6152006-07-30 20:19:33 -07001093 *
1094 * This function performs a route lookup on the given flow with the
1095 * possibility of using the cached route in the socket if it is valid.
1096 * It will take the socket dst lock when operating on the dst cache.
1097 * As a result, this function can only be used in process context.
1098 *
David S. Miller68d0c6d2011-03-01 13:19:07 -08001099 * It returns a valid dst pointer on success, or a pointer encoded
1100 * error code.
Herbert Xu497c6152006-07-30 20:19:33 -07001101 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001102struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
David S. Miller68d0c6d2011-03-01 13:19:07 -08001103 const struct in6_addr *final_dst,
David S. Millera1414712011-03-01 14:32:04 -08001104 bool can_sleep)
Herbert Xu497c6152006-07-30 20:19:33 -07001105{
David S. Miller68d0c6d2011-03-01 13:19:07 -08001106 struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
1107 int err;
Herbert Xu497c6152006-07-30 20:19:33 -07001108
David S. Miller4c9483b2011-03-12 16:22:43 -05001109 dst = ip6_sk_dst_check(sk, dst, fl6);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001110
David S. Miller4c9483b2011-03-12 16:22:43 -05001111 err = ip6_dst_lookup_tail(sk, &dst, fl6);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001112 if (err)
1113 return ERR_PTR(err);
1114 if (final_dst)
David S. Miller4c9483b2011-03-12 16:22:43 -05001115 ipv6_addr_copy(&fl6->daddr, final_dst);
David S. Miller2774c132011-03-01 14:59:04 -08001116 if (can_sleep)
David S. Miller4c9483b2011-03-12 16:22:43 -05001117 fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
David S. Miller2774c132011-03-01 14:59:04 -08001118
David S. Miller4c9483b2011-03-12 16:22:43 -05001119 return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
Herbert Xu497c6152006-07-30 20:19:33 -07001120}
David S. Miller68d0c6d2011-03-01 13:19:07 -08001121EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
Herbert Xu497c6152006-07-30 20:19:33 -07001122
Adrian Bunk34a0b3c2005-11-29 16:28:56 -08001123static inline int ip6_ufo_append_data(struct sock *sk,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001124 int getfrag(void *from, char *to, int offset, int len,
1125 int odd, struct sk_buff *skb),
1126 void *from, int length, int hh_len, int fragheaderlen,
Eric Dumazetef81bb42011-08-08 23:44:00 -07001127 int transhdrlen, int mtu,unsigned int flags,
1128 struct rt6_info *rt)
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001129
1130{
1131 struct sk_buff *skb;
1132 int err;
1133
1134 /* There is support for UDP large send offload by network
1135 * device, so create one single skb packet containing complete
1136 * udp datagram
1137 */
1138 if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) {
1139 skb = sock_alloc_send_skb(sk,
1140 hh_len + fragheaderlen + transhdrlen + 20,
1141 (flags & MSG_DONTWAIT), &err);
1142 if (skb == NULL)
1143 return -ENOMEM;
1144
1145 /* reserve space for Hardware header */
1146 skb_reserve(skb, hh_len);
1147
1148 /* create space for UDP/IP header */
1149 skb_put(skb,fragheaderlen + transhdrlen);
1150
1151 /* initialize network header pointer */
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07001152 skb_reset_network_header(skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001153
1154 /* initialize protocol header pointer */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001155 skb->transport_header = skb->network_header + fragheaderlen;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001156
Patrick McHardy84fa7932006-08-29 16:44:56 -07001157 skb->ip_summed = CHECKSUM_PARTIAL;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001158 skb->csum = 0;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001159 }
1160
1161 err = skb_append_datato_frags(sk,skb, getfrag, from,
1162 (length - transhdrlen));
1163 if (!err) {
1164 struct frag_hdr fhdr;
1165
Sridhar Samudralac31d5322009-07-09 08:09:58 +00001166 /* Specify the length of each IPv6 datagram fragment.
1167 * It has to be a multiple of 8.
1168 */
1169 skb_shinfo(skb)->gso_size = (mtu - fragheaderlen -
1170 sizeof(struct frag_hdr)) & ~7;
Herbert Xuf83ef8c2006-06-30 13:37:03 -07001171 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Jason Wanga1b7ab02011-10-09 10:56:44 +08001172 ipv6_select_ident(&fhdr, &rt->rt6i_dst.addr);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001173 skb_shinfo(skb)->ip6_frag_id = fhdr.identification;
1174 __skb_queue_tail(&sk->sk_write_queue, skb);
1175
1176 return 0;
1177 }
1178 /* There is not enough support do UPD LSO,
1179 * so follow normal path
1180 */
1181 kfree_skb(skb);
1182
1183 return err;
1184}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Herbert Xu0178b692009-02-05 15:15:50 -08001186static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
1187 gfp_t gfp)
1188{
1189 return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
1190}
1191
1192static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
1193 gfp_t gfp)
1194{
1195 return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
1196}
1197
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001198int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
1199 int offset, int len, int odd, struct sk_buff *skb),
1200 void *from, int length, int transhdrlen,
David S. Miller4c9483b2011-03-12 16:22:43 -05001201 int hlimit, int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6,
Brian Haley13b52cd2010-04-23 11:26:08 +00001202 struct rt6_info *rt, unsigned int flags, int dontfrag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 struct inet_sock *inet = inet_sk(sk);
1205 struct ipv6_pinfo *np = inet6_sk(sk);
David S. Millerbdc712b2011-05-06 15:02:07 -07001206 struct inet_cork *cork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 struct sk_buff *skb;
1208 unsigned int maxfraglen, fragheaderlen;
1209 int exthdrlen;
1210 int hh_len;
1211 int mtu;
1212 int copy;
1213 int err;
1214 int offset = 0;
1215 int csummode = CHECKSUM_NONE;
Anders Berggrena693e692011-02-28 12:32:11 -08001216 __u8 tx_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 if (flags&MSG_PROBE)
1219 return 0;
David S. Millerbdc712b2011-05-06 15:02:07 -07001220 cork = &inet->cork.base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (skb_queue_empty(&sk->sk_write_queue)) {
1222 /*
1223 * setup for corking
1224 */
1225 if (opt) {
Herbert Xu0178b692009-02-05 15:15:50 -08001226 if (WARN_ON(np->cork.opt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return -EINVAL;
Herbert Xu0178b692009-02-05 15:15:50 -08001228
1229 np->cork.opt = kmalloc(opt->tot_len, sk->sk_allocation);
1230 if (unlikely(np->cork.opt == NULL))
1231 return -ENOBUFS;
1232
1233 np->cork.opt->tot_len = opt->tot_len;
1234 np->cork.opt->opt_flen = opt->opt_flen;
1235 np->cork.opt->opt_nflen = opt->opt_nflen;
1236
1237 np->cork.opt->dst0opt = ip6_opt_dup(opt->dst0opt,
1238 sk->sk_allocation);
1239 if (opt->dst0opt && !np->cork.opt->dst0opt)
1240 return -ENOBUFS;
1241
1242 np->cork.opt->dst1opt = ip6_opt_dup(opt->dst1opt,
1243 sk->sk_allocation);
1244 if (opt->dst1opt && !np->cork.opt->dst1opt)
1245 return -ENOBUFS;
1246
1247 np->cork.opt->hopopt = ip6_opt_dup(opt->hopopt,
1248 sk->sk_allocation);
1249 if (opt->hopopt && !np->cork.opt->hopopt)
1250 return -ENOBUFS;
1251
1252 np->cork.opt->srcrt = ip6_rthdr_dup(opt->srcrt,
1253 sk->sk_allocation);
1254 if (opt->srcrt && !np->cork.opt->srcrt)
1255 return -ENOBUFS;
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /* need source address above miyazawa*/
1258 }
Changli Gaod8d1f302010-06-10 23:31:35 -07001259 dst_hold(&rt->dst);
David S. Millerbdc712b2011-05-06 15:02:07 -07001260 cork->dst = &rt->dst;
David S. Miller4c9483b2011-03-12 16:22:43 -05001261 inet->cork.fl.u.ip6 = *fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 np->cork.hop_limit = hlimit;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001263 np->cork.tclass = tclass;
John Heffner628a5c52007-04-20 15:53:27 -07001264 mtu = np->pmtudisc == IPV6_PMTUDISC_PROBE ?
Changli Gaod8d1f302010-06-10 23:31:35 -07001265 rt->dst.dev->mtu : dst_mtu(rt->dst.path);
Dave Jonesc7503602006-03-20 22:44:52 -08001266 if (np->frag_size < mtu) {
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -08001267 if (np->frag_size)
1268 mtu = np->frag_size;
1269 }
David S. Millerbdc712b2011-05-06 15:02:07 -07001270 cork->fragsize = mtu;
Changli Gaod8d1f302010-06-10 23:31:35 -07001271 if (dst_allfrag(rt->dst.path))
David S. Millerbdc712b2011-05-06 15:02:07 -07001272 cork->flags |= IPCORK_ALLFRAG;
1273 cork->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 sk->sk_sndmsg_page = NULL;
1275 sk->sk_sndmsg_off = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -07001276 exthdrlen = rt->dst.header_len + (opt ? opt->opt_flen : 0) -
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001277 rt->rt6i_nfheader_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 length += exthdrlen;
1279 transhdrlen += exthdrlen;
1280 } else {
David S. Millerbdc712b2011-05-06 15:02:07 -07001281 rt = (struct rt6_info *)cork->dst;
David S. Miller4c9483b2011-03-12 16:22:43 -05001282 fl6 = &inet->cork.fl.u.ip6;
Herbert Xu0178b692009-02-05 15:15:50 -08001283 opt = np->cork.opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 transhdrlen = 0;
1285 exthdrlen = 0;
David S. Millerbdc712b2011-05-06 15:02:07 -07001286 mtu = cork->fragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288
Changli Gaod8d1f302010-06-10 23:31:35 -07001289 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001291 fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
Herbert Xub4ce9272007-11-13 21:33:32 -08001292 (opt ? opt->opt_nflen : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
1294
1295 if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
David S. Millerbdc712b2011-05-06 15:02:07 -07001296 if (cork->length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001297 ipv6_local_error(sk, EMSGSIZE, fl6, mtu-exthdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EMSGSIZE;
1299 }
1300 }
1301
Anders Berggrena693e692011-02-28 12:32:11 -08001302 /* For UDP, check if TX timestamp is enabled */
1303 if (sk->sk_type == SOCK_DGRAM) {
1304 err = sock_tx_timestamp(sk, &tx_flags);
1305 if (err)
1306 goto error;
1307 }
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /*
1310 * Let's try using as much space as possible.
1311 * Use MTU if total length of the message fits into the MTU.
1312 * Otherwise, we need to reserve fragment header and
1313 * fragment alignment (= 8-15 octects, in total).
1314 *
1315 * Note that we may need to "move" the data from the tail of
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001316 * of the buffer to the new fragment when we split
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 * the message.
1318 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001319 * FIXME: It may be fragmented into multiple chunks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 * at once if non-fragmentable extension headers
1321 * are too large.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001322 * --yoshfuji
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 */
1324
David S. Millerbdc712b2011-05-06 15:02:07 -07001325 cork->length += length;
Brian Haley4b340ae2010-04-23 11:26:09 +00001326 if (length > mtu) {
1327 int proto = sk->sk_protocol;
1328 if (dontfrag && (proto == IPPROTO_UDP || proto == IPPROTO_RAW)){
David S. Miller4c9483b2011-03-12 16:22:43 -05001329 ipv6_local_rxpmtu(sk, fl6, mtu-exthdrlen);
Brian Haley4b340ae2010-04-23 11:26:09 +00001330 return -EMSGSIZE;
1331 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001332
Brian Haley4b340ae2010-04-23 11:26:09 +00001333 if (proto == IPPROTO_UDP &&
Changli Gaod8d1f302010-06-10 23:31:35 -07001334 (rt->dst.dev->features & NETIF_F_UFO)) {
Brian Haley4b340ae2010-04-23 11:26:09 +00001335
1336 err = ip6_ufo_append_data(sk, getfrag, from, length,
1337 hh_len, fragheaderlen,
Eric Dumazetef81bb42011-08-08 23:44:00 -07001338 transhdrlen, mtu, flags, rt);
Brian Haley4b340ae2010-04-23 11:26:09 +00001339 if (err)
1340 goto error;
1341 return 0;
1342 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
1346 goto alloc_new_skb;
1347
1348 while (length > 0) {
1349 /* Check if the remaining data fits into current packet. */
David S. Millerbdc712b2011-05-06 15:02:07 -07001350 copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (copy < length)
1352 copy = maxfraglen - skb->len;
1353
1354 if (copy <= 0) {
1355 char *data;
1356 unsigned int datalen;
1357 unsigned int fraglen;
1358 unsigned int fraggap;
1359 unsigned int alloclen;
1360 struct sk_buff *skb_prev;
1361alloc_new_skb:
1362 skb_prev = skb;
1363
1364 /* There's no room in the current skb */
1365 if (skb_prev)
1366 fraggap = skb_prev->len - maxfraglen;
1367 else
1368 fraggap = 0;
1369
1370 /*
1371 * If remaining data exceeds the mtu,
1372 * we know we need more fragment(s).
1373 */
1374 datalen = length + fraggap;
David S. Millerbdc712b2011-05-06 15:02:07 -07001375 if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 datalen = maxfraglen - fragheaderlen;
1377
1378 fraglen = datalen + fragheaderlen;
1379 if ((flags & MSG_MORE) &&
Changli Gaod8d1f302010-06-10 23:31:35 -07001380 !(rt->dst.dev->features&NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 alloclen = mtu;
1382 else
1383 alloclen = datalen + fragheaderlen;
1384
1385 /*
1386 * The last fragment gets additional space at tail.
1387 * Note: we overallocate on fragments with MSG_MODE
1388 * because we have no idea if we're the last one.
1389 */
1390 if (datalen == length + fraggap)
Changli Gaod8d1f302010-06-10 23:31:35 -07001391 alloclen += rt->dst.trailer_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 /*
1394 * We just reserve space for fragment header.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001395 * Note: this may be overallocation if the message
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 * (without MSG_MORE) fits into the MTU.
1397 */
1398 alloclen += sizeof(struct frag_hdr);
1399
1400 if (transhdrlen) {
1401 skb = sock_alloc_send_skb(sk,
1402 alloclen + hh_len,
1403 (flags & MSG_DONTWAIT), &err);
1404 } else {
1405 skb = NULL;
1406 if (atomic_read(&sk->sk_wmem_alloc) <=
1407 2 * sk->sk_sndbuf)
1408 skb = sock_wmalloc(sk,
1409 alloclen + hh_len, 1,
1410 sk->sk_allocation);
1411 if (unlikely(skb == NULL))
1412 err = -ENOBUFS;
Anders Berggrena693e692011-02-28 12:32:11 -08001413 else {
1414 /* Only the initial fragment
1415 * is time stamped.
1416 */
1417 tx_flags = 0;
1418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420 if (skb == NULL)
1421 goto error;
1422 /*
1423 * Fill in the control structures
1424 */
1425 skb->ip_summed = csummode;
1426 skb->csum = 0;
1427 /* reserve for fragmentation */
1428 skb_reserve(skb, hh_len+sizeof(struct frag_hdr));
1429
Anders Berggrena693e692011-02-28 12:32:11 -08001430 if (sk->sk_type == SOCK_DGRAM)
1431 skb_shinfo(skb)->tx_flags = tx_flags;
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /*
1434 * Find where to start putting bytes
1435 */
1436 data = skb_put(skb, fraglen);
Arnaldo Carvalho de Meloc14d2452007-03-11 22:39:41 -03001437 skb_set_network_header(skb, exthdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 data += fragheaderlen;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001439 skb->transport_header = (skb->network_header +
1440 fragheaderlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (fraggap) {
1442 skb->csum = skb_copy_and_csum_bits(
1443 skb_prev, maxfraglen,
1444 data + transhdrlen, fraggap, 0);
1445 skb_prev->csum = csum_sub(skb_prev->csum,
1446 skb->csum);
1447 data += fraggap;
Herbert Xue9fa4f72006-08-13 20:12:58 -07001448 pskb_trim_unique(skb_prev, maxfraglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450 copy = datalen - transhdrlen - fraggap;
1451 if (copy < 0) {
1452 err = -EINVAL;
1453 kfree_skb(skb);
1454 goto error;
1455 } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
1456 err = -EFAULT;
1457 kfree_skb(skb);
1458 goto error;
1459 }
1460
1461 offset += copy;
1462 length -= datalen - fraggap;
1463 transhdrlen = 0;
1464 exthdrlen = 0;
1465 csummode = CHECKSUM_NONE;
1466
1467 /*
1468 * Put the packet on the pending queue
1469 */
1470 __skb_queue_tail(&sk->sk_write_queue, skb);
1471 continue;
1472 }
1473
1474 if (copy > length)
1475 copy = length;
1476
Changli Gaod8d1f302010-06-10 23:31:35 -07001477 if (!(rt->dst.dev->features&NETIF_F_SG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 unsigned int off;
1479
1480 off = skb->len;
1481 if (getfrag(from, skb_put(skb, copy),
1482 offset, copy, off, skb) < 0) {
1483 __skb_trim(skb, off);
1484 err = -EFAULT;
1485 goto error;
1486 }
1487 } else {
1488 int i = skb_shinfo(skb)->nr_frags;
1489 skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
1490 struct page *page = sk->sk_sndmsg_page;
1491 int off = sk->sk_sndmsg_off;
1492 unsigned int left;
1493
1494 if (page && (left = PAGE_SIZE - off) > 0) {
1495 if (copy >= left)
1496 copy = left;
1497 if (page != frag->page) {
1498 if (i == MAX_SKB_FRAGS) {
1499 err = -EMSGSIZE;
1500 goto error;
1501 }
1502 get_page(page);
1503 skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0);
1504 frag = &skb_shinfo(skb)->frags[i];
1505 }
1506 } else if(i < MAX_SKB_FRAGS) {
1507 if (copy > PAGE_SIZE)
1508 copy = PAGE_SIZE;
1509 page = alloc_pages(sk->sk_allocation, 0);
1510 if (page == NULL) {
1511 err = -ENOMEM;
1512 goto error;
1513 }
1514 sk->sk_sndmsg_page = page;
1515 sk->sk_sndmsg_off = 0;
1516
1517 skb_fill_page_desc(skb, i, page, 0, 0);
1518 frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 } else {
1520 err = -EMSGSIZE;
1521 goto error;
1522 }
1523 if (getfrag(from, page_address(frag->page)+frag->page_offset+frag->size, offset, copy, skb->len, skb) < 0) {
1524 err = -EFAULT;
1525 goto error;
1526 }
1527 sk->sk_sndmsg_off += copy;
1528 frag->size += copy;
1529 skb->len += copy;
1530 skb->data_len += copy;
Herbert Xuf945fa72008-01-22 22:39:26 -08001531 skb->truesize += copy;
1532 atomic_add(copy, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534 offset += copy;
1535 length -= copy;
1536 }
1537 return 0;
1538error:
David S. Millerbdc712b2011-05-06 15:02:07 -07001539 cork->length -= length;
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001540 IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return err;
1542}
1543
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001544static void ip6_cork_release(struct inet_sock *inet, struct ipv6_pinfo *np)
1545{
Herbert Xu0178b692009-02-05 15:15:50 -08001546 if (np->cork.opt) {
1547 kfree(np->cork.opt->dst0opt);
1548 kfree(np->cork.opt->dst1opt);
1549 kfree(np->cork.opt->hopopt);
1550 kfree(np->cork.opt->srcrt);
1551 kfree(np->cork.opt);
1552 np->cork.opt = NULL;
1553 }
1554
David S. Millerbdc712b2011-05-06 15:02:07 -07001555 if (inet->cork.base.dst) {
1556 dst_release(inet->cork.base.dst);
1557 inet->cork.base.dst = NULL;
1558 inet->cork.base.flags &= ~IPCORK_ALLFRAG;
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001559 }
1560 memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
1561}
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563int ip6_push_pending_frames(struct sock *sk)
1564{
1565 struct sk_buff *skb, *tmp_skb;
1566 struct sk_buff **tail_skb;
1567 struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
1568 struct inet_sock *inet = inet_sk(sk);
1569 struct ipv6_pinfo *np = inet6_sk(sk);
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001570 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 struct ipv6hdr *hdr;
1572 struct ipv6_txoptions *opt = np->cork.opt;
David S. Millerbdc712b2011-05-06 15:02:07 -07001573 struct rt6_info *rt = (struct rt6_info *)inet->cork.base.dst;
David S. Miller4c9483b2011-03-12 16:22:43 -05001574 struct flowi6 *fl6 = &inet->cork.fl.u.ip6;
1575 unsigned char proto = fl6->flowi6_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 int err = 0;
1577
1578 if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
1579 goto out;
1580 tail_skb = &(skb_shinfo(skb)->frag_list);
1581
1582 /* move skb->data to ip header from ext header */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001583 if (skb->data < skb_network_header(skb))
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001584 __skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001586 __skb_pull(tmp_skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 *tail_skb = tmp_skb;
1588 tail_skb = &(tmp_skb->next);
1589 skb->len += tmp_skb->len;
1590 skb->data_len += tmp_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 skb->truesize += tmp_skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 tmp_skb->destructor = NULL;
1593 tmp_skb->sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595
Herbert Xu28a89452008-02-12 18:07:27 -08001596 /* Allow local fragmentation. */
Herbert Xub5c15fc2008-02-14 23:49:37 -08001597 if (np->pmtudisc < IPV6_PMTUDISC_DO)
Herbert Xu28a89452008-02-12 18:07:27 -08001598 skb->local_df = 1;
1599
David S. Miller4c9483b2011-03-12 16:22:43 -05001600 ipv6_addr_copy(final_dst, &fl6->daddr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001601 __skb_pull(skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (opt && opt->opt_flen)
1603 ipv6_push_frag_opts(skb, opt, &proto);
1604 if (opt && opt->opt_nflen)
1605 ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
1606
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001607 skb_push(skb, sizeof(struct ipv6hdr));
1608 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001609 hdr = ipv6_hdr(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001610
David S. Miller4c9483b2011-03-12 16:22:43 -05001611 *(__be32*)hdr = fl6->flowlabel |
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001612 htonl(0x60000000 | ((int)np->cork.tclass << 20));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 hdr->hop_limit = np->cork.hop_limit;
1615 hdr->nexthdr = proto;
David S. Miller4c9483b2011-03-12 16:22:43 -05001616 ipv6_addr_copy(&hdr->saddr, &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 ipv6_addr_copy(&hdr->daddr, final_dst);
1618
Patrick McHardya2c20642006-01-08 22:37:26 -08001619 skb->priority = sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -08001620 skb->mark = sk->sk_mark;
Patrick McHardya2c20642006-01-08 22:37:26 -08001621
Changli Gaod8d1f302010-06-10 23:31:35 -07001622 skb_dst_set(skb, dst_clone(&rt->dst));
Neil Hormanedf391f2009-04-27 02:45:02 -07001623 IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
David L Stevens14878f72007-09-16 16:52:35 -07001624 if (proto == IPPROTO_ICMPV6) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001625 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
David L Stevens14878f72007-09-16 16:52:35 -07001626
Denis V. Lunev5a57d4c2008-10-08 10:34:14 -07001627 ICMP6MSGOUT_INC_STATS_BH(net, idev, icmp6_hdr(skb)->icmp6_type);
Denis V. Luneve41b5362008-10-08 10:33:26 -07001628 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
David L Stevens14878f72007-09-16 16:52:35 -07001629 }
1630
Herbert Xuef76bc22008-01-11 19:15:08 -08001631 err = ip6_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 if (err) {
1633 if (err > 0)
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -07001634 err = net_xmit_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (err)
1636 goto error;
1637 }
1638
1639out:
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001640 ip6_cork_release(inet, np);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return err;
1642error:
Eric Dumazet06254912009-09-01 18:37:16 -07001643 IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 goto out;
1645}
1646
1647void ip6_flush_pending_frames(struct sock *sk)
1648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 struct sk_buff *skb;
1650
1651 while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001652 if (skb_dst(skb))
1653 IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakie1f522082007-09-11 11:31:43 +02001654 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 kfree_skb(skb);
1656 }
1657
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001658 ip6_cork_release(inet_sk(sk), inet6_sk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}