blob: ee1bb450bfe43271056c94c38b926bef9427630c [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
Jan Engelhardt9e508492010-04-13 15:28:11 +020086static int ip6_finish_output2(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Eric Dumazetadf30902009-06-02 05:19:30 +000088 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct net_device *dev = dst->dev;
David S. Millerf6b72b622011-07-14 07:53:20 -070090 struct neighbour *neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 skb->protocol = htons(ETH_P_IPV6);
93 skb->dev = dev;
94
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070095 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
Eric Dumazetadf30902009-06-02 05:19:30 +000096 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Octavian Purdila7ad68482010-01-06 20:37:01 -080098 if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(skb->sk) &&
Patrick McHardyd1db2752010-05-11 14:40:55 +020099 ((mroute6_socket(dev_net(dev), skb) &&
Benjamin Therybd91b8b2008-12-10 16:07:08 -0800100 !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +0900101 ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
102 &ipv6_hdr(skb)->saddr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
104
105 /* Do not check for IFF_ALLMULTI; multicast routing
106 is not supported in any case.
107 */
108 if (newskb)
Jan Engelhardtb2e0b382010-03-23 04:09:07 +0100109 NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
110 newskb, NULL, newskb->dev,
Michel Machado95603e22012-06-12 10:16:35 +0000111 dev_loopback_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700113 if (ipv6_hdr(skb)->hop_limit == 0) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700114 IP6_INC_STATS(dev_net(dev), idev,
115 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 kfree_skb(skb);
117 return 0;
118 }
119 }
120
Neil Hormanedf391f2009-04-27 02:45:02 -0700121 IP6_UPD_PO_STATS(dev_net(dev), idev, IPSTATS_MIB_OUTMCAST,
122 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000125 rcu_read_lock();
David Miller27217452011-12-02 16:52:08 +0000126 neigh = dst_get_neighbour_noref(dst);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000127 if (neigh) {
128 int res = neigh_output(neigh, skb);
David S. Miller05e3aa02011-07-16 17:26:00 -0700129
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000130 rcu_read_unlock();
131 return res;
132 }
133 rcu_read_unlock();
Jan Engelhardt9e508492010-04-13 15:28:11 +0200134 IP6_INC_STATS_BH(dev_net(dst->dev),
135 ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
136 kfree_skb(skb);
137 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Jan Engelhardt9e508492010-04-13 15:28:11 +0200140static int ip6_finish_output(struct sk_buff *skb)
141{
142 if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
143 dst_allfrag(skb_dst(skb)))
144 return ip6_fragment(skb, ip6_finish_output2);
145 else
146 return ip6_finish_output2(skb);
147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149int ip6_output(struct sk_buff *skb)
150{
Jan Engelhardt9e508492010-04-13 15:28:11 +0200151 struct net_device *dev = skb_dst(skb)->dev;
Eric Dumazetadf30902009-06-02 05:19:30 +0000152 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900153 if (unlikely(idev->cnf.disable_ipv6)) {
Jan Engelhardt9e508492010-04-13 15:28:11 +0200154 IP6_INC_STATS(dev_net(dev), idev,
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700155 IPSTATS_MIB_OUTDISCARDS);
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900156 kfree_skb(skb);
157 return 0;
158 }
159
Jan Engelhardt9c6eb282010-04-13 15:32:16 +0200160 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev,
161 ip6_finish_output,
162 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
Shan Weib5d43992010-04-15 16:48:48 +0000166 * xmit an sk_buff (used by TCP, SCTP and DCCP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
168
David S. Miller4c9483b2011-03-12 16:22:43 -0500169int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
Eric Dumazetb903d322011-10-27 00:44:35 -0400170 struct ipv6_txoptions *opt, int tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700172 struct net *net = sock_net(sk);
Patrick McHardyb30bd282006-03-23 01:17:25 -0800173 struct ipv6_pinfo *np = inet6_sk(sk);
David S. Miller4c9483b2011-03-12 16:22:43 -0500174 struct in6_addr *first_hop = &fl6->daddr;
Eric Dumazetadf30902009-06-02 05:19:30 +0000175 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct ipv6hdr *hdr;
David S. Miller4c9483b2011-03-12 16:22:43 -0500177 u8 proto = fl6->flowi6_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int seg_len = skb->len;
Gerrit Renkere651f032009-08-09 08:12:48 +0000179 int hlimit = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 u32 mtu;
181
182 if (opt) {
Chuck Leverc2636b42007-10-23 21:07:32 -0700183 unsigned int head_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /* First: exthdrs may take lots of space (~8K for now)
186 MAX_HEADER is not enough.
187 */
188 head_room = opt->opt_nflen + opt->opt_flen;
189 seg_len += head_room;
190 head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
191
192 if (skb_headroom(skb) < head_room) {
193 struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900194 if (skb2 == NULL) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000195 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900196 IPSTATS_MIB_OUTDISCARDS);
197 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -ENOBUFS;
199 }
Eric Dumazet808db802012-04-24 10:17:59 +0000200 consume_skb(skb);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900201 skb = skb2;
Dan Carpenter83d7eb22010-04-30 16:42:08 -0700202 skb_set_owner_w(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204 if (opt->opt_flen)
205 ipv6_push_frag_opts(skb, opt, &proto);
206 if (opt->opt_nflen)
207 ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
208 }
209
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700210 skb_push(skb, sizeof(struct ipv6hdr));
211 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700212 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /*
215 * Fill in the IPv6 header
216 */
Eric Dumazetb903d322011-10-27 00:44:35 -0400217 if (np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 hlimit = np->hop_limit;
219 if (hlimit < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400220 hlimit = ip6_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
David S. Miller4c9483b2011-03-12 16:22:43 -0500222 *(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | fl6->flowlabel;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 hdr->payload_len = htons(seg_len);
225 hdr->nexthdr = proto;
226 hdr->hop_limit = hlimit;
227
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000228 hdr->saddr = fl6->saddr;
229 hdr->daddr = *first_hop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Patrick McHardya2c20642006-01-08 22:37:26 -0800231 skb->priority = sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800232 skb->mark = sk->sk_mark;
Patrick McHardya2c20642006-01-08 22:37:26 -0800233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 mtu = dst_mtu(dst);
Wei Yongjun283d07ac2008-08-03 21:15:59 -0700235 if ((skb->len <= mtu) || skb->local_df || skb_is_gso(skb)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000236 IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
Neil Hormanedf391f2009-04-27 02:45:02 -0700237 IPSTATS_MIB_OUT, skb->len);
Jan Engelhardtb2e0b382010-03-23 04:09:07 +0100238 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
239 dst->dev, dst_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
Joe Perchese87cc472012-05-13 21:56:26 +0000242 net_dbg_ratelimited("IPv6: sending pkt_too_big to self\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 skb->dev = dst->dev;
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000244 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Eric Dumazetadf30902009-06-02 05:19:30 +0000245 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 kfree_skb(skb);
247 return -EMSGSIZE;
248}
249
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900250EXPORT_SYMBOL(ip6_xmit);
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/*
253 * To avoid extra problems ND packets are send through this
254 * routine. It's code duplication but I really want to avoid
255 * extra checks since ipv6_build_header is used by TCP (which
256 * is for us performance critical)
257 */
258
259int ip6_nd_hdr(struct sock *sk, struct sk_buff *skb, struct net_device *dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900260 const struct in6_addr *saddr, const struct in6_addr *daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int proto, int len)
262{
263 struct ipv6_pinfo *np = inet6_sk(sk);
264 struct ipv6hdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 skb->protocol = htons(ETH_P_IPV6);
267 skb->dev = dev;
268
Arnaldo Carvalho de Melo55f79cc2007-03-14 21:05:03 -0300269 skb_reset_network_header(skb);
270 skb_put(skb, sizeof(struct ipv6hdr));
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700271 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Al Viroae08e1f2006-11-08 00:27:11 -0800273 *(__be32*)hdr = htonl(0x60000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 hdr->payload_len = htons(len);
276 hdr->nexthdr = proto;
277 hdr->hop_limit = np->hop_limit;
278
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000279 hdr->saddr = *saddr;
280 hdr->daddr = *daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 return 0;
283}
284
285static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
286{
287 struct ip6_ra_chain *ra;
288 struct sock *last = NULL;
289
290 read_lock(&ip6_ra_lock);
291 for (ra = ip6_ra_chain; ra; ra = ra->next) {
292 struct sock *sk = ra->sk;
Andrew McDonald0bd1b592005-08-09 19:44:42 -0700293 if (sk && ra->sel == sel &&
294 (!sk->sk_bound_dev_if ||
295 sk->sk_bound_dev_if == skb->dev->ifindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (last) {
297 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
298 if (skb2)
299 rawv6_rcv(last, skb2);
300 }
301 last = sk;
302 }
303 }
304
305 if (last) {
306 rawv6_rcv(last, skb);
307 read_unlock(&ip6_ra_lock);
308 return 1;
309 }
310 read_unlock(&ip6_ra_lock);
311 return 0;
312}
313
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700314static int ip6_forward_proxy_check(struct sk_buff *skb)
315{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700316 struct ipv6hdr *hdr = ipv6_hdr(skb);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700317 u8 nexthdr = hdr->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800318 __be16 frag_off;
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700319 int offset;
320
321 if (ipv6_ext_hdr(nexthdr)) {
Jesse Gross75f28112011-11-30 17:05:51 -0800322 offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700323 if (offset < 0)
324 return 0;
325 } else
326 offset = sizeof(struct ipv6hdr);
327
328 if (nexthdr == IPPROTO_ICMPV6) {
329 struct icmp6hdr *icmp6;
330
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700331 if (!pskb_may_pull(skb, (skb_network_header(skb) +
332 offset + 1 - skb->data)))
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700333 return 0;
334
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700335 icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700336
337 switch (icmp6->icmp6_type) {
338 case NDISC_ROUTER_SOLICITATION:
339 case NDISC_ROUTER_ADVERTISEMENT:
340 case NDISC_NEIGHBOUR_SOLICITATION:
341 case NDISC_NEIGHBOUR_ADVERTISEMENT:
342 case NDISC_REDIRECT:
343 /* For reaction involving unicast neighbor discovery
344 * message destined to the proxied address, pass it to
345 * input function.
346 */
347 return 1;
348 default:
349 break;
350 }
351 }
352
Ville Nuorvala74553b02006-09-22 14:42:18 -0700353 /*
354 * The proxying router can't forward traffic sent to a link-local
355 * address, so signal the sender and discard the packet. This
356 * behavior is clarified by the MIPv6 specification.
357 */
358 if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
359 dst_link_failure(skb);
360 return -1;
361 }
362
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700363 return 0;
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366static inline int ip6_forward_finish(struct sk_buff *skb)
367{
368 return dst_output(skb);
369}
370
371int ip6_forward(struct sk_buff *skb)
372{
Eric Dumazetadf30902009-06-02 05:19:30 +0000373 struct dst_entry *dst = skb_dst(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700374 struct ipv6hdr *hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 struct inet6_skb_parm *opt = IP6CB(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900376 struct net *net = dev_net(dst->dev);
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800377 u32 mtu;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900378
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700379 if (net->ipv6.devconf_all->forwarding == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 goto error;
381
Ben Hutchings4497b072008-06-19 16:22:28 -0700382 if (skb_warn_if_lro(skb))
383 goto drop;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700386 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 goto drop;
388 }
389
Alexey Kuznetsov72b43d02011-01-12 08:34:08 +0000390 if (skb->pkt_type != PACKET_HOST)
391 goto drop;
392
Herbert Xu35fc92a2007-03-26 23:22:20 -0700393 skb_forward_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /*
396 * We DO NOT make any processing on
397 * RA packets, pushing them to user level AS IS
398 * without ane WARRANTY that application will be able
399 * to interpret them. The reason is that we
400 * cannot make anything clever here.
401 *
402 * We are not end-node, so that if packet contains
403 * AH/ESP, we cannot make anything.
404 * Defragmentation also would be mistake, RA packets
405 * cannot be fragmented, because there is no warranty
406 * that different fragments will go along one path. --ANK
407 */
408 if (opt->ra) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700409 u8 *ptr = skb_network_header(skb) + opt->ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (ip6_call_ra_chain(skb, (ptr[2]<<8) + ptr[3]))
411 return 0;
412 }
413
414 /*
415 * check and decrement ttl
416 */
417 if (hdr->hop_limit <= 1) {
418 /* Force OUTPUT device used as source address */
419 skb->dev = dst->dev;
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000420 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700421 IP6_INC_STATS_BH(net,
422 ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 kfree_skb(skb);
425 return -ETIMEDOUT;
426 }
427
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700428 /* XXX: idev->cnf.proxy_ndp? */
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700429 if (net->ipv6.devconf_all->proxy_ndp &&
Daniel Lezcano8a3edd82008-03-07 11:14:16 -0800430 pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
Ville Nuorvala74553b02006-09-22 14:42:18 -0700431 int proxied = ip6_forward_proxy_check(skb);
432 if (proxied > 0)
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700433 return ip6_input(skb);
Ville Nuorvala74553b02006-09-22 14:42:18 -0700434 else if (proxied < 0) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700435 IP6_INC_STATS(net, ip6_dst_idev(dst),
436 IPSTATS_MIB_INDISCARDS);
Ville Nuorvala74553b02006-09-22 14:42:18 -0700437 goto drop;
438 }
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700439 }
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!xfrm6_route_forward(skb)) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700442 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 goto drop;
444 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000445 dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /* IPv6 specs say nothing about it, but it is clear that we cannot
448 send redirects to source routed frames.
Masahide NAKAMURA1e5dc142007-08-24 19:08:55 +0900449 We don't send redirects to frames decapsulated from IPsec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
David S. Millerc45a3df2012-01-27 15:32:19 -0800451 if (skb->dev == dst->dev && opt->srcrt == 0 && !skb_sec_path(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct in6_addr *target = NULL;
David S. Millerfbfe95a2012-06-08 23:24:18 -0700453 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /*
457 * incoming and outgoing devices are the same
458 * send a redirect.
459 */
460
461 rt = (struct rt6_info *) dst;
David S. Millerc45a3df2012-01-27 15:32:19 -0800462 if (rt->rt6i_flags & RTF_GATEWAY)
463 target = &rt->rt6i_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 else
465 target = &hdr->daddr;
466
David S. Millerfbfe95a2012-06-08 23:24:18 -0700467 peer = rt6_get_peer_create(rt);
David S. Miller92d86822011-02-04 15:55:25 -0800468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* Limit redirects both by destination (here)
470 and by source (inside ndisc_send_redirect)
471 */
David S. Millerfbfe95a2012-06-08 23:24:18 -0700472 if (inet_peer_xrlim_allow(peer, 1*HZ))
David S. Miller49919692012-01-27 15:30:48 -0800473 ndisc_send_redirect(skb, target);
David L Stevens5bb1ab02007-05-09 13:53:44 -0700474 } else {
475 int addrtype = ipv6_addr_type(&hdr->saddr);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /* This check is security critical. */
YOSHIFUJI Hideakif81b2e72008-06-25 16:55:26 +0900478 if (addrtype == IPV6_ADDR_ANY ||
479 addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
David L Stevens5bb1ab02007-05-09 13:53:44 -0700480 goto error;
481 if (addrtype & IPV6_ADDR_LINKLOCAL) {
482 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000483 ICMPV6_NOT_NEIGHBOUR, 0);
David L Stevens5bb1ab02007-05-09 13:53:44 -0700484 goto error;
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800488 mtu = dst_mtu(dst);
489 if (mtu < IPV6_MIN_MTU)
490 mtu = IPV6_MIN_MTU;
491
Herbert Xu0aa68272010-05-27 16:14:30 -0700492 if (skb->len > mtu && !skb_is_gso(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* Again, force OUTPUT device used as source address */
494 skb->dev = dst->dev;
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800495 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700496 IP6_INC_STATS_BH(net,
497 ip6_dst_idev(dst), IPSTATS_MIB_INTOOBIGERRORS);
498 IP6_INC_STATS_BH(net,
499 ip6_dst_idev(dst), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 kfree_skb(skb);
501 return -EMSGSIZE;
502 }
503
504 if (skb_cow(skb, dst->dev->hard_header_len)) {
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700505 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto drop;
507 }
508
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700509 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* Mangling hops number delayed to point after skb COW */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 hdr->hop_limit--;
514
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700515 IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
Jan Engelhardtb2e0b382010-03-23 04:09:07 +0100516 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dst->dev,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800517 ip6_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519error:
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700520 IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521drop:
522 kfree_skb(skb);
523 return -EINVAL;
524}
525
526static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
527{
528 to->pkt_type = from->pkt_type;
529 to->priority = from->priority;
530 to->protocol = from->protocol;
Eric Dumazetadf30902009-06-02 05:19:30 +0000531 skb_dst_drop(to);
532 skb_dst_set(to, dst_clone(skb_dst(from)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 to->dev = from->dev;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800534 to->mark = from->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536#ifdef CONFIG_NET_SCHED
537 to->tc_index = from->tc_index;
538#endif
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -0700539 nf_copy(to, from);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700540#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
541 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
542 to->nf_trace = from->nf_trace;
543#endif
James Morris984bc162006-06-09 00:29:17 -0700544 skb_copy_secmark(to, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
548{
549 u16 offset = sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700550 struct ipv6_opt_hdr *exthdr =
551 (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700552 unsigned int packet_len = skb->tail - skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 int found_rhdr = 0;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700554 *nexthdr = &ipv6_hdr(skb)->nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 while (offset + 1 <= packet_len) {
557
558 switch (**nexthdr) {
559
560 case NEXTHDR_HOP:
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700561 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 case NEXTHDR_ROUTING:
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700563 found_rhdr = 1;
564 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 case NEXTHDR_DEST:
Masahide NAKAMURA59fbb3a62007-06-26 23:56:32 -0700566#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700567 if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
568 break;
569#endif
570 if (found_rhdr)
571 return offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 break;
573 default :
574 return offset;
575 }
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700576
577 offset += ipv6_optlen(exthdr);
578 *nexthdr = &exthdr->nexthdr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700579 exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
580 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
583 return offset;
584}
585
Eric Dumazet87c48fa2011-07-21 21:25:58 -0700586void ipv6_select_ident(struct frag_hdr *fhdr, struct rt6_info *rt)
587{
588 static atomic_t ipv6_fragmentation_id;
589 int old, new;
590
Eric Dumazete688a602011-12-22 04:15:53 +0000591 if (rt && !(rt->dst.flags & DST_NOPEER)) {
David S. Millerfbfe95a2012-06-08 23:24:18 -0700592 struct inet_peer *peer = rt6_get_peer_create(rt);
Eric Dumazet87c48fa2011-07-21 21:25:58 -0700593
Eric Dumazet87c48fa2011-07-21 21:25:58 -0700594 if (peer) {
595 fhdr->identification = htonl(inet_getid(peer, 0));
596 return;
597 }
598 }
599 do {
600 old = atomic_read(&ipv6_fragmentation_id);
601 new = old + 1;
602 if (!new)
603 new = 1;
604 } while (atomic_cmpxchg(&ipv6_fragmentation_id, old, new) != old);
605 fhdr->identification = htonl(new);
606}
607
David Stevensad0081e2010-12-17 11:42:42 +0000608int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct sk_buff *frag;
Eric Dumazetadf30902009-06-02 05:19:30 +0000611 struct rt6_info *rt = (struct rt6_info*)skb_dst(skb);
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -0800612 struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct ipv6hdr *tmp_hdr;
614 struct frag_hdr *fh;
615 unsigned int mtu, hlen, left, len;
Herbert Xua7ae1992011-11-18 02:20:04 +0000616 int hroom, troom;
Al Viroae08e1f2006-11-08 00:27:11 -0800617 __be32 frag_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int ptr, offset = 0, err=0;
619 u8 *prevhdr, nexthdr = 0;
Eric Dumazetadf30902009-06-02 05:19:30 +0000620 struct net *net = dev_net(skb_dst(skb)->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 hlen = ip6_find_1stfragopt(skb, &prevhdr);
623 nexthdr = *prevhdr;
624
John Heffner628a5c52007-04-20 15:53:27 -0700625 mtu = ip6_skb_dst_mtu(skb);
John Heffnerb881ef72007-04-20 15:52:39 -0700626
627 /* We must not fragment if the socket is set to force MTU discovery
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800628 * or if the skb it not generated by a local socket.
John Heffnerb881ef72007-04-20 15:52:39 -0700629 */
Eric Dumazeta34a1012012-05-18 21:51:44 +0000630 if (unlikely(!skb->local_df && skb->len > mtu)) {
631 if (skb->sk && dst_allfrag(skb_dst(skb)))
632 sk_nocaps_add(skb->sk, NETIF_F_GSO_MASK);
633
Eric Dumazetadf30902009-06-02 05:19:30 +0000634 skb->dev = skb_dst(skb)->dev;
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000635 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Eric Dumazetadf30902009-06-02 05:19:30 +0000636 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700637 IPSTATS_MIB_FRAGFAILS);
John Heffnerb881ef72007-04-20 15:52:39 -0700638 kfree_skb(skb);
639 return -EMSGSIZE;
640 }
641
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -0800642 if (np && np->frag_size < mtu) {
643 if (np->frag_size)
644 mtu = np->frag_size;
645 }
646 mtu -= hlen + sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
David S. Miller21dc3302010-08-23 00:13:46 -0700648 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int first_len = skb_pagelen(skb);
Eric Dumazet3d130082010-09-21 08:47:45 +0000650 struct sk_buff *frag2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (first_len - hlen > mtu ||
653 ((first_len - hlen) & 7) ||
654 skb_cloned(skb))
655 goto slow_path;
656
David S. Miller4d9092b2009-06-09 00:20:05 -0700657 skb_walk_frags(skb, frag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /* Correct geometry. */
659 if (frag->len > mtu ||
660 ((frag->len & 7) && frag->next) ||
661 skb_headroom(frag) < hlen)
Eric Dumazet3d130082010-09-21 08:47:45 +0000662 goto slow_path_clean;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /* Partially cloned skb? */
665 if (skb_shared(frag))
Eric Dumazet3d130082010-09-21 08:47:45 +0000666 goto slow_path_clean;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700667
668 BUG_ON(frag->sk);
669 if (skb->sk) {
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700670 frag->sk = skb->sk;
671 frag->destructor = sock_wfree;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700672 }
Eric Dumazet3d130082010-09-21 08:47:45 +0000673 skb->truesize -= frag->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
676 err = 0;
677 offset = 0;
678 frag = skb_shinfo(skb)->frag_list;
David S. Miller4d9092b2009-06-09 00:20:05 -0700679 skb_frag_list_init(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /* BUILD HEADER */
681
YOSHIFUJI Hideaki9a217a12006-12-05 13:47:21 -0800682 *prevhdr = NEXTHDR_FRAGMENT;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700683 tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (!tmp_hdr) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000685 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700686 IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -ENOMEM;
688 }
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 __skb_pull(skb, hlen);
691 fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700692 __skb_push(skb, hlen);
693 skb_reset_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700694 memcpy(skb_network_header(skb), tmp_hdr, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Eric Dumazet87c48fa2011-07-21 21:25:58 -0700696 ipv6_select_ident(fh, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 fh->nexthdr = nexthdr;
698 fh->reserved = 0;
699 fh->frag_off = htons(IP6_MF);
700 frag_id = fh->identification;
701
702 first_len = skb_pagelen(skb);
703 skb->data_len = first_len - skb_headlen(skb);
704 skb->len = first_len;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700705 ipv6_hdr(skb)->payload_len = htons(first_len -
706 sizeof(struct ipv6hdr));
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900707
Changli Gaod8d1f302010-06-10 23:31:35 -0700708 dst_hold(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 for (;;) {
711 /* Prepare header of the next frame,
712 * before previous one went down. */
713 if (frag) {
714 frag->ip_summed = CHECKSUM_NONE;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300715 skb_reset_transport_header(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700717 __skb_push(frag, hlen);
718 skb_reset_network_header(frag);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700719 memcpy(skb_network_header(frag), tmp_hdr,
720 hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 offset += skb->len - hlen - sizeof(struct frag_hdr);
722 fh->nexthdr = nexthdr;
723 fh->reserved = 0;
724 fh->frag_off = htons(offset);
725 if (frag->next != NULL)
726 fh->frag_off |= htons(IP6_MF);
727 fh->identification = frag_id;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700728 ipv6_hdr(frag)->payload_len =
729 htons(frag->len -
730 sizeof(struct ipv6hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 ip6_copy_metadata(frag, skb);
732 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 err = output(skb);
Wei Dongdafee492006-08-02 13:41:21 -0700735 if(!err)
Changli Gaod8d1f302010-06-10 23:31:35 -0700736 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700737 IPSTATS_MIB_FRAGCREATES);
Wei Dongdafee492006-08-02 13:41:21 -0700738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (err || !frag)
740 break;
741
742 skb = frag;
743 frag = skb->next;
744 skb->next = NULL;
745 }
746
Jesper Juhla51482b2005-11-08 09:41:34 -0800747 kfree(tmp_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (err == 0) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700750 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700751 IPSTATS_MIB_FRAGOKS);
Changli Gaod8d1f302010-06-10 23:31:35 -0700752 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return 0;
754 }
755
756 while (frag) {
757 skb = frag->next;
758 kfree_skb(frag);
759 frag = skb;
760 }
761
Changli Gaod8d1f302010-06-10 23:31:35 -0700762 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700763 IPSTATS_MIB_FRAGFAILS);
Changli Gaod8d1f302010-06-10 23:31:35 -0700764 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return err;
Eric Dumazet3d130082010-09-21 08:47:45 +0000766
767slow_path_clean:
768 skb_walk_frags(skb, frag2) {
769 if (frag2 == frag)
770 break;
771 frag2->sk = NULL;
772 frag2->destructor = NULL;
773 skb->truesize += frag2->truesize;
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
777slow_path:
Eric Dumazet72e843b2012-05-18 11:02:15 +0000778 if ((skb->ip_summed == CHECKSUM_PARTIAL) &&
779 skb_checksum_help(skb))
780 goto fail;
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 left = skb->len - hlen; /* Space per frame */
783 ptr = hlen; /* Where to start from */
784
785 /*
786 * Fragment the datagram.
787 */
788
789 *prevhdr = NEXTHDR_FRAGMENT;
Herbert Xua7ae1992011-11-18 02:20:04 +0000790 hroom = LL_RESERVED_SPACE(rt->dst.dev);
791 troom = rt->dst.dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /*
794 * Keep copying data until we run out.
795 */
796 while(left > 0) {
797 len = left;
798 /* IF: it doesn't fit, use 'mtu' - the data space left */
799 if (len > mtu)
800 len = mtu;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300801 /* IF: we are not sending up to and including the packet end
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 then align the next start on an eight byte boundary */
803 if (len < left) {
804 len &= ~7;
805 }
806 /*
807 * Allocate buffer.
808 */
809
Herbert Xua7ae1992011-11-18 02:20:04 +0000810 if ((frag = alloc_skb(len + hlen + sizeof(struct frag_hdr) +
811 hroom + troom, GFP_ATOMIC)) == NULL) {
Patrick McHardy64ce2072005-08-09 20:50:53 -0700812 NETDEBUG(KERN_INFO "IPv6: frag: no memory for new fragment!\n");
Eric Dumazetadf30902009-06-02 05:19:30 +0000813 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900814 IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 err = -ENOMEM;
816 goto fail;
817 }
818
819 /*
820 * Set up data on packet
821 */
822
823 ip6_copy_metadata(frag, skb);
Herbert Xua7ae1992011-11-18 02:20:04 +0000824 skb_reserve(frag, hroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 skb_put(frag, len + hlen + sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700826 skb_reset_network_header(frag);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300827 fh = (struct frag_hdr *)(skb_network_header(frag) + hlen);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700828 frag->transport_header = (frag->network_header + hlen +
829 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 /*
832 * Charge the memory for the fragment to any owner
833 * it might possess
834 */
835 if (skb->sk)
836 skb_set_owner_w(frag, skb->sk);
837
838 /*
839 * Copy the packet header into the new buffer.
840 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300841 skb_copy_from_linear_data(skb, skb_network_header(frag), hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /*
844 * Build fragment header.
845 */
846 fh->nexthdr = nexthdr;
847 fh->reserved = 0;
Yan Zhengf36d6ab2005-10-03 14:19:15 -0700848 if (!frag_id) {
Eric Dumazet87c48fa2011-07-21 21:25:58 -0700849 ipv6_select_ident(fh, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 frag_id = fh->identification;
851 } else
852 fh->identification = frag_id;
853
854 /*
855 * Copy a block of the IP datagram.
856 */
Wei Yongjun8984e412007-08-21 20:59:08 -0700857 if (skb_copy_bits(skb, ptr, skb_transport_header(frag), len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 BUG();
859 left -= len;
860
861 fh->frag_off = htons(offset);
862 if (left > 0)
863 fh->frag_off |= htons(IP6_MF);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700864 ipv6_hdr(frag)->payload_len = htons(frag->len -
865 sizeof(struct ipv6hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 ptr += len;
868 offset += len;
869
870 /*
871 * Put this fragment into the sending queue.
872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 err = output(frag);
874 if (err)
875 goto fail;
Wei Dongdafee492006-08-02 13:41:21 -0700876
Eric Dumazetadf30902009-06-02 05:19:30 +0000877 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700878 IPSTATS_MIB_FRAGCREATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000880 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900881 IPSTATS_MIB_FRAGOKS);
Eric Dumazet808db802012-04-24 10:17:59 +0000882 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return err;
884
885fail:
Eric Dumazetadf30902009-06-02 05:19:30 +0000886 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900887 IPSTATS_MIB_FRAGFAILS);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900888 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return err;
890}
891
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000892static inline int ip6_rt_check(const struct rt6key *rt_key,
893 const struct in6_addr *fl_addr,
894 const struct in6_addr *addr_cache)
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700895{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000896 return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
897 (addr_cache == NULL || !ipv6_addr_equal(fl_addr, addr_cache));
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700898}
899
Herbert Xu497c6152006-07-30 20:19:33 -0700900static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
901 struct dst_entry *dst,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000902 const struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Herbert Xu497c6152006-07-30 20:19:33 -0700904 struct ipv6_pinfo *np = inet6_sk(sk);
905 struct rt6_info *rt = (struct rt6_info *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Herbert Xu497c6152006-07-30 20:19:33 -0700907 if (!dst)
908 goto out;
909
910 /* Yes, checking route validity in not connected
911 * case is not very simple. Take into account,
912 * that we do not support routing by source, TOS,
913 * and MSG_DONTROUTE --ANK (980726)
914 *
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700915 * 1. ip6_rt_check(): If route was host route,
916 * check that cached destination is current.
Herbert Xu497c6152006-07-30 20:19:33 -0700917 * If it is network route, we still may
918 * check its validity using saved pointer
919 * to the last used address: daddr_cache.
920 * We do not want to save whole address now,
921 * (because main consumer of this service
922 * is tcp, which has not this problem),
923 * so that the last trick works only on connected
924 * sockets.
925 * 2. oif also should be the same.
926 */
David S. Miller4c9483b2011-03-12 16:22:43 -0500927 if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700928#ifdef CONFIG_IPV6_SUBTREES
David S. Miller4c9483b2011-03-12 16:22:43 -0500929 ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700930#endif
David S. Miller4c9483b2011-03-12 16:22:43 -0500931 (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex)) {
Herbert Xu497c6152006-07-30 20:19:33 -0700932 dst_release(dst);
933 dst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
Herbert Xu497c6152006-07-30 20:19:33 -0700936out:
937 return dst;
938}
939
940static int ip6_dst_lookup_tail(struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500941 struct dst_entry **dst, struct flowi6 *fl6)
Herbert Xu497c6152006-07-30 20:19:33 -0700942{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900943 struct net *net = sock_net(sk);
David S. Miller69cce1d2011-07-17 23:09:49 -0700944#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
945 struct neighbour *n;
946#endif
947 int err;
Herbert Xu497c6152006-07-30 20:19:33 -0700948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (*dst == NULL)
David S. Miller4c9483b2011-03-12 16:22:43 -0500950 *dst = ip6_route_output(net, sk, fl6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 if ((err = (*dst)->error))
953 goto out_err_release;
954
David S. Miller4c9483b2011-03-12 16:22:43 -0500955 if (ipv6_addr_any(&fl6->saddr)) {
Daniel Walterc3968a82011-04-13 21:10:57 +0000956 struct rt6_info *rt = (struct rt6_info *) *dst;
957 err = ip6_route_get_saddr(net, rt, &fl6->daddr,
958 sk ? inet6_sk(sk)->srcprefs : 0,
959 &fl6->saddr);
Olaf Hering44456d32005-07-27 11:45:17 -0700960 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 goto out_err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963
Neil Horman95c385b2007-04-25 17:08:10 -0700964#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Neil Hormane550dfb2008-09-09 13:51:35 -0700965 /*
966 * Here if the dst entry we've looked up
967 * has a neighbour entry that is in the INCOMPLETE
968 * state and the src address from the flow is
969 * marked as OPTIMISTIC, we release the found
970 * dst entry and replace it instead with the
971 * dst entry of the nexthop router
972 */
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000973 rcu_read_lock();
David Miller27217452011-12-02 16:52:08 +0000974 n = dst_get_neighbour_noref(*dst);
David S. Miller69cce1d2011-07-17 23:09:49 -0700975 if (n && !(n->nud_state & NUD_VALID)) {
Neil Hormane550dfb2008-09-09 13:51:35 -0700976 struct inet6_ifaddr *ifp;
David S. Miller4c9483b2011-03-12 16:22:43 -0500977 struct flowi6 fl_gw6;
Neil Hormane550dfb2008-09-09 13:51:35 -0700978 int redirect;
Neil Horman95c385b2007-04-25 17:08:10 -0700979
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000980 rcu_read_unlock();
David S. Miller4c9483b2011-03-12 16:22:43 -0500981 ifp = ipv6_get_ifaddr(net, &fl6->saddr,
Neil Hormane550dfb2008-09-09 13:51:35 -0700982 (*dst)->dev, 1);
Neil Horman95c385b2007-04-25 17:08:10 -0700983
Neil Hormane550dfb2008-09-09 13:51:35 -0700984 redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
985 if (ifp)
986 in6_ifa_put(ifp);
Neil Horman95c385b2007-04-25 17:08:10 -0700987
Neil Hormane550dfb2008-09-09 13:51:35 -0700988 if (redirect) {
989 /*
990 * We need to get the dst entry for the
991 * default router instead
992 */
993 dst_release(*dst);
David S. Miller4c9483b2011-03-12 16:22:43 -0500994 memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
995 memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
996 *dst = ip6_route_output(net, sk, &fl_gw6);
Neil Hormane550dfb2008-09-09 13:51:35 -0700997 if ((err = (*dst)->error))
998 goto out_err_release;
Neil Horman95c385b2007-04-25 17:08:10 -0700999 }
Eric Dumazetf2c31e32011-07-29 19:00:53 +00001000 } else {
1001 rcu_read_unlock();
Neil Hormane550dfb2008-09-09 13:51:35 -07001002 }
Neil Horman95c385b2007-04-25 17:08:10 -07001003#endif
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return 0;
1006
1007out_err_release:
Mitsuru Chinenca46f9c2007-12-05 22:31:47 -08001008 if (err == -ENETUNREACH)
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001009 IP6_INC_STATS_BH(net, NULL, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 dst_release(*dst);
1011 *dst = NULL;
1012 return err;
1013}
Adrian Bunk34a0b3c2005-11-29 16:28:56 -08001014
Herbert Xu497c6152006-07-30 20:19:33 -07001015/**
1016 * ip6_dst_lookup - perform route lookup on flow
1017 * @sk: socket which provides route info
1018 * @dst: pointer to dst_entry * for result
David S. Miller4c9483b2011-03-12 16:22:43 -05001019 * @fl6: flow to lookup
Herbert Xu497c6152006-07-30 20:19:33 -07001020 *
1021 * This function performs a route lookup on the given flow.
1022 *
1023 * It returns zero on success, or a standard errno code on error.
1024 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001025int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi6 *fl6)
Herbert Xu497c6152006-07-30 20:19:33 -07001026{
1027 *dst = NULL;
David S. Miller4c9483b2011-03-12 16:22:43 -05001028 return ip6_dst_lookup_tail(sk, dst, fl6);
Herbert Xu497c6152006-07-30 20:19:33 -07001029}
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -08001030EXPORT_SYMBOL_GPL(ip6_dst_lookup);
1031
Herbert Xu497c6152006-07-30 20:19:33 -07001032/**
David S. Miller68d0c6d2011-03-01 13:19:07 -08001033 * ip6_dst_lookup_flow - perform route lookup on flow with ipsec
1034 * @sk: socket which provides route info
David S. Miller4c9483b2011-03-12 16:22:43 -05001035 * @fl6: flow to lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001036 * @final_dst: final destination address for ipsec lookup
David S. Millera1414712011-03-01 14:32:04 -08001037 * @can_sleep: we are in a sleepable context
David S. Miller68d0c6d2011-03-01 13:19:07 -08001038 *
1039 * This function performs a route lookup on the given flow.
1040 *
1041 * It returns a valid dst pointer on success, or a pointer encoded
1042 * error code.
1043 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001044struct dst_entry *ip6_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
David S. Miller68d0c6d2011-03-01 13:19:07 -08001045 const struct in6_addr *final_dst,
David S. Millera1414712011-03-01 14:32:04 -08001046 bool can_sleep)
David S. Miller68d0c6d2011-03-01 13:19:07 -08001047{
1048 struct dst_entry *dst = NULL;
1049 int err;
1050
David S. Miller4c9483b2011-03-12 16:22:43 -05001051 err = ip6_dst_lookup_tail(sk, &dst, fl6);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001052 if (err)
1053 return ERR_PTR(err);
1054 if (final_dst)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001055 fl6->daddr = *final_dst;
David S. Miller2774c132011-03-01 14:59:04 -08001056 if (can_sleep)
David S. Miller4c9483b2011-03-12 16:22:43 -05001057 fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
David S. Miller2774c132011-03-01 14:59:04 -08001058
David S. Miller4c9483b2011-03-12 16:22:43 -05001059 return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001060}
1061EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
1062
1063/**
1064 * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
1065 * @sk: socket which provides the dst cache and route info
David S. Miller4c9483b2011-03-12 16:22:43 -05001066 * @fl6: flow to lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001067 * @final_dst: final destination address for ipsec lookup
David S. Millera1414712011-03-01 14:32:04 -08001068 * @can_sleep: we are in a sleepable context
Herbert Xu497c6152006-07-30 20:19:33 -07001069 *
1070 * This function performs a route lookup on the given flow with the
1071 * possibility of using the cached route in the socket if it is valid.
1072 * It will take the socket dst lock when operating on the dst cache.
1073 * As a result, this function can only be used in process context.
1074 *
David S. Miller68d0c6d2011-03-01 13:19:07 -08001075 * It returns a valid dst pointer on success, or a pointer encoded
1076 * error code.
Herbert Xu497c6152006-07-30 20:19:33 -07001077 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001078struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
David S. Miller68d0c6d2011-03-01 13:19:07 -08001079 const struct in6_addr *final_dst,
David S. Millera1414712011-03-01 14:32:04 -08001080 bool can_sleep)
Herbert Xu497c6152006-07-30 20:19:33 -07001081{
David S. Miller68d0c6d2011-03-01 13:19:07 -08001082 struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
1083 int err;
Herbert Xu497c6152006-07-30 20:19:33 -07001084
David S. Miller4c9483b2011-03-12 16:22:43 -05001085 dst = ip6_sk_dst_check(sk, dst, fl6);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001086
David S. Miller4c9483b2011-03-12 16:22:43 -05001087 err = ip6_dst_lookup_tail(sk, &dst, fl6);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001088 if (err)
1089 return ERR_PTR(err);
1090 if (final_dst)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001091 fl6->daddr = *final_dst;
David S. Miller2774c132011-03-01 14:59:04 -08001092 if (can_sleep)
David S. Miller4c9483b2011-03-12 16:22:43 -05001093 fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
David S. Miller2774c132011-03-01 14:59:04 -08001094
David S. Miller4c9483b2011-03-12 16:22:43 -05001095 return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
Herbert Xu497c6152006-07-30 20:19:33 -07001096}
David S. Miller68d0c6d2011-03-01 13:19:07 -08001097EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
Herbert Xu497c6152006-07-30 20:19:33 -07001098
Adrian Bunk34a0b3c2005-11-29 16:28:56 -08001099static inline int ip6_ufo_append_data(struct sock *sk,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001100 int getfrag(void *from, char *to, int offset, int len,
1101 int odd, struct sk_buff *skb),
1102 void *from, int length, int hh_len, int fragheaderlen,
Eric Dumazet87c48fa2011-07-21 21:25:58 -07001103 int transhdrlen, int mtu,unsigned int flags,
1104 struct rt6_info *rt)
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001105
1106{
1107 struct sk_buff *skb;
1108 int err;
1109
1110 /* There is support for UDP large send offload by network
1111 * device, so create one single skb packet containing complete
1112 * udp datagram
1113 */
1114 if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) {
1115 skb = sock_alloc_send_skb(sk,
1116 hh_len + fragheaderlen + transhdrlen + 20,
1117 (flags & MSG_DONTWAIT), &err);
1118 if (skb == NULL)
Zheng Yan504744e2011-10-28 00:26:00 -04001119 return err;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001120
1121 /* reserve space for Hardware header */
1122 skb_reserve(skb, hh_len);
1123
1124 /* create space for UDP/IP header */
1125 skb_put(skb,fragheaderlen + transhdrlen);
1126
1127 /* initialize network header pointer */
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07001128 skb_reset_network_header(skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001129
1130 /* initialize protocol header pointer */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001131 skb->transport_header = skb->network_header + fragheaderlen;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001132
Patrick McHardy84fa7932006-08-29 16:44:56 -07001133 skb->ip_summed = CHECKSUM_PARTIAL;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001134 skb->csum = 0;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001135 }
1136
1137 err = skb_append_datato_frags(sk,skb, getfrag, from,
1138 (length - transhdrlen));
1139 if (!err) {
1140 struct frag_hdr fhdr;
1141
Sridhar Samudralac31d5322009-07-09 08:09:58 +00001142 /* Specify the length of each IPv6 datagram fragment.
1143 * It has to be a multiple of 8.
1144 */
1145 skb_shinfo(skb)->gso_size = (mtu - fragheaderlen -
1146 sizeof(struct frag_hdr)) & ~7;
Herbert Xuf83ef8c2006-06-30 13:37:03 -07001147 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Eric Dumazet87c48fa2011-07-21 21:25:58 -07001148 ipv6_select_ident(&fhdr, rt);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001149 skb_shinfo(skb)->ip6_frag_id = fhdr.identification;
1150 __skb_queue_tail(&sk->sk_write_queue, skb);
1151
1152 return 0;
1153 }
1154 /* There is not enough support do UPD LSO,
1155 * so follow normal path
1156 */
1157 kfree_skb(skb);
1158
1159 return err;
1160}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Herbert Xu0178b692009-02-05 15:15:50 -08001162static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
1163 gfp_t gfp)
1164{
1165 return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
1166}
1167
1168static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
1169 gfp_t gfp)
1170{
1171 return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
1172}
1173
Gao feng0c183372012-05-26 01:30:53 +00001174static void ip6_append_data_mtu(int *mtu,
1175 int *maxfraglen,
1176 unsigned int fragheaderlen,
1177 struct sk_buff *skb,
1178 struct rt6_info *rt)
1179{
1180 if (!(rt->dst.flags & DST_XFRM_TUNNEL)) {
1181 if (skb == NULL) {
1182 /* first fragment, reserve header_len */
1183 *mtu = *mtu - rt->dst.header_len;
1184
1185 } else {
1186 /*
1187 * this fragment is not first, the headers
1188 * space is regarded as data space.
1189 */
1190 *mtu = dst_mtu(rt->dst.path);
1191 }
1192 *maxfraglen = ((*mtu - fragheaderlen) & ~7)
1193 + fragheaderlen - sizeof(struct frag_hdr);
1194 }
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;
Gao feng0c183372012-05-26 01:30:53 +00001206 struct sk_buff *skb, *skb_prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 unsigned int maxfraglen, fragheaderlen;
1208 int exthdrlen;
Steffen Klassert299b0762011-10-11 01:43:33 +00001209 int dst_exthdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 int hh_len;
1211 int mtu;
1212 int copy;
1213 int err;
1214 int offset = 0;
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;
Gao feng0c183372012-05-26 01:30:53 +00001263 if (rt->dst.flags & DST_XFRM_TUNNEL)
1264 mtu = np->pmtudisc == IPV6_PMTUDISC_PROBE ?
1265 rt->dst.dev->mtu : dst_mtu(&rt->dst);
1266 else
1267 mtu = np->pmtudisc == IPV6_PMTUDISC_PROBE ?
1268 rt->dst.dev->mtu : dst_mtu(rt->dst.path);
Dave Jonesc7503602006-03-20 22:44:52 -08001269 if (np->frag_size < mtu) {
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -08001270 if (np->frag_size)
1271 mtu = np->frag_size;
1272 }
David S. Millerbdc712b2011-05-06 15:02:07 -07001273 cork->fragsize = mtu;
Changli Gaod8d1f302010-06-10 23:31:35 -07001274 if (dst_allfrag(rt->dst.path))
David S. Millerbdc712b2011-05-06 15:02:07 -07001275 cork->flags |= IPCORK_ALLFRAG;
1276 cork->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 sk->sk_sndmsg_page = NULL;
1278 sk->sk_sndmsg_off = 0;
Steffen Klassert299b0762011-10-11 01:43:33 +00001279 exthdrlen = (opt ? opt->opt_flen : 0) - rt->rt6i_nfheader_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 length += exthdrlen;
1281 transhdrlen += exthdrlen;
Steffen Klassert299b0762011-10-11 01:43:33 +00001282 dst_exthdrlen = rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 } else {
David S. Millerbdc712b2011-05-06 15:02:07 -07001284 rt = (struct rt6_info *)cork->dst;
David S. Miller4c9483b2011-03-12 16:22:43 -05001285 fl6 = &inet->cork.fl.u.ip6;
Herbert Xu0178b692009-02-05 15:15:50 -08001286 opt = np->cork.opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 transhdrlen = 0;
1288 exthdrlen = 0;
Steffen Klassert299b0762011-10-11 01:43:33 +00001289 dst_exthdrlen = 0;
David S. Millerbdc712b2011-05-06 15:02:07 -07001290 mtu = cork->fragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292
Changli Gaod8d1f302010-06-10 23:31:35 -07001293 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001295 fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
Herbert Xub4ce9272007-11-13 21:33:32 -08001296 (opt ? opt->opt_nflen : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
1298
1299 if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
David S. Millerbdc712b2011-05-06 15:02:07 -07001300 if (cork->length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001301 ipv6_local_error(sk, EMSGSIZE, fl6, mtu-exthdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return -EMSGSIZE;
1303 }
1304 }
1305
Anders Berggrena693e692011-02-28 12:32:11 -08001306 /* For UDP, check if TX timestamp is enabled */
1307 if (sk->sk_type == SOCK_DGRAM) {
1308 err = sock_tx_timestamp(sk, &tx_flags);
1309 if (err)
1310 goto error;
1311 }
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /*
1314 * Let's try using as much space as possible.
1315 * Use MTU if total length of the message fits into the MTU.
1316 * Otherwise, we need to reserve fragment header and
1317 * fragment alignment (= 8-15 octects, in total).
1318 *
1319 * Note that we may need to "move" the data from the tail of
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001320 * of the buffer to the new fragment when we split
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 * the message.
1322 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001323 * FIXME: It may be fragmented into multiple chunks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 * at once if non-fragmentable extension headers
1325 * are too large.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001326 * --yoshfuji
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
1328
David S. Millerbdc712b2011-05-06 15:02:07 -07001329 cork->length += length;
Brian Haley4b340ae2010-04-23 11:26:09 +00001330 if (length > mtu) {
1331 int proto = sk->sk_protocol;
1332 if (dontfrag && (proto == IPPROTO_UDP || proto == IPPROTO_RAW)){
David S. Miller4c9483b2011-03-12 16:22:43 -05001333 ipv6_local_rxpmtu(sk, fl6, mtu-exthdrlen);
Brian Haley4b340ae2010-04-23 11:26:09 +00001334 return -EMSGSIZE;
1335 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001336
Brian Haley4b340ae2010-04-23 11:26:09 +00001337 if (proto == IPPROTO_UDP &&
Changli Gaod8d1f302010-06-10 23:31:35 -07001338 (rt->dst.dev->features & NETIF_F_UFO)) {
Brian Haley4b340ae2010-04-23 11:26:09 +00001339
1340 err = ip6_ufo_append_data(sk, getfrag, from, length,
1341 hh_len, fragheaderlen,
Eric Dumazet87c48fa2011-07-21 21:25:58 -07001342 transhdrlen, mtu, flags, rt);
Brian Haley4b340ae2010-04-23 11:26:09 +00001343 if (err)
1344 goto error;
1345 return 0;
1346 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
1350 goto alloc_new_skb;
1351
1352 while (length > 0) {
1353 /* Check if the remaining data fits into current packet. */
David S. Millerbdc712b2011-05-06 15:02:07 -07001354 copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (copy < length)
1356 copy = maxfraglen - skb->len;
1357
1358 if (copy <= 0) {
1359 char *data;
1360 unsigned int datalen;
1361 unsigned int fraglen;
1362 unsigned int fraggap;
1363 unsigned int alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364alloc_new_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 /* There's no room in the current skb */
Gao feng0c183372012-05-26 01:30:53 +00001366 if (skb)
1367 fraggap = skb->len - maxfraglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 else
1369 fraggap = 0;
Gao feng0c183372012-05-26 01:30:53 +00001370 /* update mtu and maxfraglen if necessary */
1371 if (skb == NULL || skb_prev == NULL)
1372 ip6_append_data_mtu(&mtu, &maxfraglen,
1373 fragheaderlen, skb, rt);
1374
1375 skb_prev = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /*
1378 * If remaining data exceeds the mtu,
1379 * we know we need more fragment(s).
1380 */
1381 datalen = length + fraggap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Gao feng0c183372012-05-26 01:30:53 +00001383 if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
1384 datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if ((flags & MSG_MORE) &&
Changli Gaod8d1f302010-06-10 23:31:35 -07001386 !(rt->dst.dev->features&NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 alloclen = mtu;
1388 else
1389 alloclen = datalen + fragheaderlen;
1390
Steffen Klassert299b0762011-10-11 01:43:33 +00001391 alloclen += dst_exthdrlen;
1392
Gao feng0c183372012-05-26 01:30:53 +00001393 if (datalen != length + fraggap) {
1394 /*
1395 * this is not the last fragment, the trailer
1396 * space is regarded as data space.
1397 */
1398 datalen += rt->dst.trailer_len;
1399 }
1400
1401 alloclen += rt->dst.trailer_len;
1402 fraglen = datalen + fragheaderlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 /*
1405 * We just reserve space for fragment header.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001406 * Note: this may be overallocation if the message
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 * (without MSG_MORE) fits into the MTU.
1408 */
1409 alloclen += sizeof(struct frag_hdr);
1410
1411 if (transhdrlen) {
1412 skb = sock_alloc_send_skb(sk,
1413 alloclen + hh_len,
1414 (flags & MSG_DONTWAIT), &err);
1415 } else {
1416 skb = NULL;
1417 if (atomic_read(&sk->sk_wmem_alloc) <=
1418 2 * sk->sk_sndbuf)
1419 skb = sock_wmalloc(sk,
1420 alloclen + hh_len, 1,
1421 sk->sk_allocation);
1422 if (unlikely(skb == NULL))
1423 err = -ENOBUFS;
Anders Berggrena693e692011-02-28 12:32:11 -08001424 else {
1425 /* Only the initial fragment
1426 * is time stamped.
1427 */
1428 tx_flags = 0;
1429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431 if (skb == NULL)
1432 goto error;
1433 /*
1434 * Fill in the control structures
1435 */
Eric Dumazetd7f7c0a2012-05-18 05:37:56 +00001436 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 skb->csum = 0;
Gao feng1f858512012-03-19 22:36:10 +00001438 /* reserve for fragmentation and ipsec header */
1439 skb_reserve(skb, hh_len + sizeof(struct frag_hdr) +
1440 dst_exthdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Anders Berggrena693e692011-02-28 12:32:11 -08001442 if (sk->sk_type == SOCK_DGRAM)
1443 skb_shinfo(skb)->tx_flags = tx_flags;
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 /*
1446 * Find where to start putting bytes
1447 */
Gao feng1f858512012-03-19 22:36:10 +00001448 data = skb_put(skb, fraglen);
1449 skb_set_network_header(skb, exthdrlen);
1450 data += fragheaderlen;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001451 skb->transport_header = (skb->network_header +
1452 fragheaderlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (fraggap) {
1454 skb->csum = skb_copy_and_csum_bits(
1455 skb_prev, maxfraglen,
1456 data + transhdrlen, fraggap, 0);
1457 skb_prev->csum = csum_sub(skb_prev->csum,
1458 skb->csum);
1459 data += fraggap;
Herbert Xue9fa4f72006-08-13 20:12:58 -07001460 pskb_trim_unique(skb_prev, maxfraglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462 copy = datalen - transhdrlen - fraggap;
Steffen Klassert299b0762011-10-11 01:43:33 +00001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (copy < 0) {
1465 err = -EINVAL;
1466 kfree_skb(skb);
1467 goto error;
1468 } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
1469 err = -EFAULT;
1470 kfree_skb(skb);
1471 goto error;
1472 }
1473
1474 offset += copy;
1475 length -= datalen - fraggap;
1476 transhdrlen = 0;
1477 exthdrlen = 0;
Steffen Klassert299b0762011-10-11 01:43:33 +00001478 dst_exthdrlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 /*
1481 * Put the packet on the pending queue
1482 */
1483 __skb_queue_tail(&sk->sk_write_queue, skb);
1484 continue;
1485 }
1486
1487 if (copy > length)
1488 copy = length;
1489
Changli Gaod8d1f302010-06-10 23:31:35 -07001490 if (!(rt->dst.dev->features&NETIF_F_SG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 unsigned int off;
1492
1493 off = skb->len;
1494 if (getfrag(from, skb_put(skb, copy),
1495 offset, copy, off, skb) < 0) {
1496 __skb_trim(skb, off);
1497 err = -EFAULT;
1498 goto error;
1499 }
1500 } else {
1501 int i = skb_shinfo(skb)->nr_frags;
1502 skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
1503 struct page *page = sk->sk_sndmsg_page;
1504 int off = sk->sk_sndmsg_off;
1505 unsigned int left;
1506
1507 if (page && (left = PAGE_SIZE - off) > 0) {
1508 if (copy >= left)
1509 copy = left;
Ian Campbell408dadf2011-08-22 23:45:00 +00001510 if (page != skb_frag_page(frag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (i == MAX_SKB_FRAGS) {
1512 err = -EMSGSIZE;
1513 goto error;
1514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0);
Ian Campbell408dadf2011-08-22 23:45:00 +00001516 skb_frag_ref(skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 frag = &skb_shinfo(skb)->frags[i];
1518 }
1519 } else if(i < MAX_SKB_FRAGS) {
1520 if (copy > PAGE_SIZE)
1521 copy = PAGE_SIZE;
1522 page = alloc_pages(sk->sk_allocation, 0);
1523 if (page == NULL) {
1524 err = -ENOMEM;
1525 goto error;
1526 }
1527 sk->sk_sndmsg_page = page;
1528 sk->sk_sndmsg_off = 0;
1529
1530 skb_fill_page_desc(skb, i, page, 0, 0);
1531 frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 } else {
1533 err = -EMSGSIZE;
1534 goto error;
1535 }
Eric Dumazet9e903e02011-10-18 21:00:24 +00001536 if (getfrag(from,
1537 skb_frag_address(frag) + skb_frag_size(frag),
Ian Campbell408dadf2011-08-22 23:45:00 +00001538 offset, copy, skb->len, skb) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 err = -EFAULT;
1540 goto error;
1541 }
1542 sk->sk_sndmsg_off += copy;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001543 skb_frag_size_add(frag, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 skb->len += copy;
1545 skb->data_len += copy;
Herbert Xuf945fa72008-01-22 22:39:26 -08001546 skb->truesize += copy;
1547 atomic_add(copy, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549 offset += copy;
1550 length -= copy;
1551 }
1552 return 0;
1553error:
David S. Millerbdc712b2011-05-06 15:02:07 -07001554 cork->length -= length;
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001555 IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return err;
1557}
Chris Elstona495f832012-04-29 21:48:53 +00001558EXPORT_SYMBOL_GPL(ip6_append_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001560static void ip6_cork_release(struct inet_sock *inet, struct ipv6_pinfo *np)
1561{
Herbert Xu0178b692009-02-05 15:15:50 -08001562 if (np->cork.opt) {
1563 kfree(np->cork.opt->dst0opt);
1564 kfree(np->cork.opt->dst1opt);
1565 kfree(np->cork.opt->hopopt);
1566 kfree(np->cork.opt->srcrt);
1567 kfree(np->cork.opt);
1568 np->cork.opt = NULL;
1569 }
1570
David S. Millerbdc712b2011-05-06 15:02:07 -07001571 if (inet->cork.base.dst) {
1572 dst_release(inet->cork.base.dst);
1573 inet->cork.base.dst = NULL;
1574 inet->cork.base.flags &= ~IPCORK_ALLFRAG;
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001575 }
1576 memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
1577}
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579int ip6_push_pending_frames(struct sock *sk)
1580{
1581 struct sk_buff *skb, *tmp_skb;
1582 struct sk_buff **tail_skb;
1583 struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
1584 struct inet_sock *inet = inet_sk(sk);
1585 struct ipv6_pinfo *np = inet6_sk(sk);
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001586 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct ipv6hdr *hdr;
1588 struct ipv6_txoptions *opt = np->cork.opt;
David S. Millerbdc712b2011-05-06 15:02:07 -07001589 struct rt6_info *rt = (struct rt6_info *)inet->cork.base.dst;
David S. Miller4c9483b2011-03-12 16:22:43 -05001590 struct flowi6 *fl6 = &inet->cork.fl.u.ip6;
1591 unsigned char proto = fl6->flowi6_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int err = 0;
1593
1594 if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
1595 goto out;
1596 tail_skb = &(skb_shinfo(skb)->frag_list);
1597
1598 /* move skb->data to ip header from ext header */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001599 if (skb->data < skb_network_header(skb))
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001600 __skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001602 __skb_pull(tmp_skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 *tail_skb = tmp_skb;
1604 tail_skb = &(tmp_skb->next);
1605 skb->len += tmp_skb->len;
1606 skb->data_len += tmp_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 skb->truesize += tmp_skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 tmp_skb->destructor = NULL;
1609 tmp_skb->sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
1611
Herbert Xu28a894532008-02-12 18:07:27 -08001612 /* Allow local fragmentation. */
Herbert Xub5c15fc2008-02-14 23:49:37 -08001613 if (np->pmtudisc < IPV6_PMTUDISC_DO)
Herbert Xu28a894532008-02-12 18:07:27 -08001614 skb->local_df = 1;
1615
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001616 *final_dst = fl6->daddr;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001617 __skb_pull(skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (opt && opt->opt_flen)
1619 ipv6_push_frag_opts(skb, opt, &proto);
1620 if (opt && opt->opt_nflen)
1621 ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
1622
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001623 skb_push(skb, sizeof(struct ipv6hdr));
1624 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001625 hdr = ipv6_hdr(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001626
David S. Miller4c9483b2011-03-12 16:22:43 -05001627 *(__be32*)hdr = fl6->flowlabel |
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001628 htonl(0x60000000 | ((int)np->cork.tclass << 20));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 hdr->hop_limit = np->cork.hop_limit;
1631 hdr->nexthdr = proto;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001632 hdr->saddr = fl6->saddr;
1633 hdr->daddr = *final_dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Patrick McHardya2c20642006-01-08 22:37:26 -08001635 skb->priority = sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -08001636 skb->mark = sk->sk_mark;
Patrick McHardya2c20642006-01-08 22:37:26 -08001637
Changli Gaod8d1f302010-06-10 23:31:35 -07001638 skb_dst_set(skb, dst_clone(&rt->dst));
Neil Hormanedf391f2009-04-27 02:45:02 -07001639 IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
David L Stevens14878f72007-09-16 16:52:35 -07001640 if (proto == IPPROTO_ICMPV6) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001641 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
David L Stevens14878f72007-09-16 16:52:35 -07001642
Denis V. Lunev5a57d4c2008-10-08 10:34:14 -07001643 ICMP6MSGOUT_INC_STATS_BH(net, idev, icmp6_hdr(skb)->icmp6_type);
Denis V. Luneve41b5362008-10-08 10:33:26 -07001644 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
David L Stevens14878f72007-09-16 16:52:35 -07001645 }
1646
Herbert Xuef76bc22008-01-11 19:15:08 -08001647 err = ip6_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (err) {
1649 if (err > 0)
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -07001650 err = net_xmit_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (err)
1652 goto error;
1653 }
1654
1655out:
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001656 ip6_cork_release(inet, np);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return err;
1658error:
Eric Dumazet06254912009-09-01 18:37:16 -07001659 IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto out;
1661}
Chris Elstona495f832012-04-29 21:48:53 +00001662EXPORT_SYMBOL_GPL(ip6_push_pending_frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664void ip6_flush_pending_frames(struct sock *sk)
1665{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 struct sk_buff *skb;
1667
1668 while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001669 if (skb_dst(skb))
1670 IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakie1f522082007-09-11 11:31:43 +02001671 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 kfree_skb(skb);
1673 }
1674
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001675 ip6_cork_release(inet_sk(sk), inet6_sk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
Chris Elstona495f832012-04-29 21:48:53 +00001677EXPORT_SYMBOL_GPL(ip6_flush_pending_frames);