]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * IPv6 output functions | |
1ab1457c | 3 | * Linux INET6 implementation |
1da177e4 LT |
4 | * |
5 | * Authors: | |
1ab1457c | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
1da177e4 | 7 | * |
1da177e4 LT |
8 | * 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 | ||
1da177e4 | 29 | #include <linux/errno.h> |
ef76bc23 | 30 | #include <linux/kernel.h> |
1da177e4 LT |
31 | #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> | |
b59f45d0 | 39 | #include <linux/module.h> |
5a0e3ad6 | 40 | #include <linux/slab.h> |
1da177e4 LT |
41 | |
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> | |
7bc570c8 | 57 | #include <linux/mroute6.h> |
1da177e4 | 58 | |
9e508490 | 59 | static int ip6_finish_output2(struct sk_buff *skb) |
1da177e4 | 60 | { |
adf30907 | 61 | struct dst_entry *dst = skb_dst(skb); |
1da177e4 | 62 | struct net_device *dev = dst->dev; |
f6b72b62 | 63 | struct neighbour *neigh; |
6fd6ce20 YH |
64 | struct in6_addr *nexthop; |
65 | int ret; | |
1da177e4 LT |
66 | |
67 | skb->protocol = htons(ETH_P_IPV6); | |
68 | skb->dev = dev; | |
69 | ||
0660e03f | 70 | if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) { |
adf30907 | 71 | struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb)); |
1da177e4 | 72 | |
7ad6848c | 73 | if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(skb->sk) && |
d1db275d | 74 | ((mroute6_socket(dev_net(dev), skb) && |
bd91b8bf | 75 | !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) || |
7bc570c8 YH |
76 | ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr, |
77 | &ipv6_hdr(skb)->saddr))) { | |
1da177e4 LT |
78 | struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); |
79 | ||
80 | /* Do not check for IFF_ALLMULTI; multicast routing | |
81 | is not supported in any case. | |
82 | */ | |
83 | if (newskb) | |
b2e0b385 JE |
84 | NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, |
85 | newskb, NULL, newskb->dev, | |
95603e22 | 86 | dev_loopback_xmit); |
1da177e4 | 87 | |
0660e03f | 88 | if (ipv6_hdr(skb)->hop_limit == 0) { |
3bd653c8 DL |
89 | IP6_INC_STATS(dev_net(dev), idev, |
90 | IPSTATS_MIB_OUTDISCARDS); | |
1da177e4 LT |
91 | kfree_skb(skb); |
92 | return 0; | |
93 | } | |
94 | } | |
95 | ||
edf391ff NH |
96 | IP6_UPD_PO_STATS(dev_net(dev), idev, IPSTATS_MIB_OUTMCAST, |
97 | skb->len); | |
dd408515 HFS |
98 | |
99 | if (IPV6_ADDR_MC_SCOPE(&ipv6_hdr(skb)->daddr) <= | |
100 | IPV6_ADDR_SCOPE_NODELOCAL && | |
101 | !(dev->flags & IFF_LOOPBACK)) { | |
102 | kfree_skb(skb); | |
103 | return 0; | |
104 | } | |
1da177e4 LT |
105 | } |
106 | ||
6fd6ce20 YH |
107 | rcu_read_lock_bh(); |
108 | nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr); | |
109 | neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop); | |
110 | if (unlikely(!neigh)) | |
111 | neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false); | |
112 | if (!IS_ERR(neigh)) { | |
113 | ret = dst_neigh_output(dst, neigh, skb); | |
114 | rcu_read_unlock_bh(); | |
115 | return ret; | |
116 | } | |
117 | rcu_read_unlock_bh(); | |
05e3aa09 | 118 | |
9e508490 JE |
119 | IP6_INC_STATS_BH(dev_net(dst->dev), |
120 | ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES); | |
121 | kfree_skb(skb); | |
122 | return -EINVAL; | |
1da177e4 LT |
123 | } |
124 | ||
9e508490 JE |
125 | static int ip6_finish_output(struct sk_buff *skb) |
126 | { | |
127 | if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) || | |
128 | dst_allfrag(skb_dst(skb))) | |
129 | return ip6_fragment(skb, ip6_finish_output2); | |
130 | else | |
131 | return ip6_finish_output2(skb); | |
132 | } | |
133 | ||
1da177e4 LT |
134 | int ip6_output(struct sk_buff *skb) |
135 | { | |
9e508490 | 136 | struct net_device *dev = skb_dst(skb)->dev; |
adf30907 | 137 | struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb)); |
778d80be | 138 | if (unlikely(idev->cnf.disable_ipv6)) { |
9e508490 | 139 | IP6_INC_STATS(dev_net(dev), idev, |
3bd653c8 | 140 | IPSTATS_MIB_OUTDISCARDS); |
778d80be YH |
141 | kfree_skb(skb); |
142 | return 0; | |
143 | } | |
144 | ||
9c6eb28a JE |
145 | return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev, |
146 | ip6_finish_output, | |
147 | !(IP6CB(skb)->flags & IP6SKB_REROUTED)); | |
1da177e4 LT |
148 | } |
149 | ||
1da177e4 | 150 | /* |
b5d43998 | 151 | * xmit an sk_buff (used by TCP, SCTP and DCCP) |
1da177e4 LT |
152 | */ |
153 | ||
4c9483b2 | 154 | int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6, |
b903d324 | 155 | struct ipv6_txoptions *opt, int tclass) |
1da177e4 | 156 | { |
3bd653c8 | 157 | struct net *net = sock_net(sk); |
b30bd282 | 158 | struct ipv6_pinfo *np = inet6_sk(sk); |
4c9483b2 | 159 | struct in6_addr *first_hop = &fl6->daddr; |
adf30907 | 160 | struct dst_entry *dst = skb_dst(skb); |
1da177e4 | 161 | struct ipv6hdr *hdr; |
4c9483b2 | 162 | u8 proto = fl6->flowi6_proto; |
1da177e4 | 163 | int seg_len = skb->len; |
e651f03a | 164 | int hlimit = -1; |
1da177e4 LT |
165 | u32 mtu; |
166 | ||
167 | if (opt) { | |
c2636b4d | 168 | unsigned int head_room; |
1da177e4 LT |
169 | |
170 | /* First: exthdrs may take lots of space (~8K for now) | |
171 | MAX_HEADER is not enough. | |
172 | */ | |
173 | head_room = opt->opt_nflen + opt->opt_flen; | |
174 | seg_len += head_room; | |
175 | head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev); | |
176 | ||
177 | if (skb_headroom(skb) < head_room) { | |
178 | struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room); | |
a11d206d | 179 | if (skb2 == NULL) { |
adf30907 | 180 | IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
a11d206d YH |
181 | IPSTATS_MIB_OUTDISCARDS); |
182 | kfree_skb(skb); | |
1da177e4 LT |
183 | return -ENOBUFS; |
184 | } | |
808db80a | 185 | consume_skb(skb); |
a11d206d | 186 | skb = skb2; |
83d7eb29 | 187 | skb_set_owner_w(skb, sk); |
1da177e4 LT |
188 | } |
189 | if (opt->opt_flen) | |
190 | ipv6_push_frag_opts(skb, opt, &proto); | |
191 | if (opt->opt_nflen) | |
192 | ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop); | |
193 | } | |
194 | ||
e2d1bca7 ACM |
195 | skb_push(skb, sizeof(struct ipv6hdr)); |
196 | skb_reset_network_header(skb); | |
0660e03f | 197 | hdr = ipv6_hdr(skb); |
1da177e4 LT |
198 | |
199 | /* | |
200 | * Fill in the IPv6 header | |
201 | */ | |
b903d324 | 202 | if (np) |
1da177e4 LT |
203 | hlimit = np->hop_limit; |
204 | if (hlimit < 0) | |
6b75d090 | 205 | hlimit = ip6_dst_hoplimit(dst); |
1da177e4 | 206 | |
3e4e4c1f | 207 | ip6_flow_hdr(hdr, tclass, fl6->flowlabel); |
41a1f8ea | 208 | |
1da177e4 LT |
209 | hdr->payload_len = htons(seg_len); |
210 | hdr->nexthdr = proto; | |
211 | hdr->hop_limit = hlimit; | |
212 | ||
4e3fd7a0 AD |
213 | hdr->saddr = fl6->saddr; |
214 | hdr->daddr = *first_hop; | |
1da177e4 | 215 | |
9c9c9ad5 | 216 | skb->protocol = htons(ETH_P_IPV6); |
a2c2064f | 217 | skb->priority = sk->sk_priority; |
4a19ec58 | 218 | skb->mark = sk->sk_mark; |
a2c2064f | 219 | |
1da177e4 | 220 | mtu = dst_mtu(dst); |
283d07ac | 221 | if ((skb->len <= mtu) || skb->local_df || skb_is_gso(skb)) { |
adf30907 | 222 | IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)), |
edf391ff | 223 | IPSTATS_MIB_OUT, skb->len); |
b2e0b385 JE |
224 | return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, |
225 | dst->dev, dst_output); | |
1da177e4 LT |
226 | } |
227 | ||
1da177e4 | 228 | skb->dev = dst->dev; |
f4e53e29 | 229 | ipv6_local_error(sk, EMSGSIZE, fl6, mtu); |
adf30907 | 230 | IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS); |
1da177e4 LT |
231 | kfree_skb(skb); |
232 | return -EMSGSIZE; | |
233 | } | |
234 | ||
7159039a YH |
235 | EXPORT_SYMBOL(ip6_xmit); |
236 | ||
1da177e4 LT |
237 | static int ip6_call_ra_chain(struct sk_buff *skb, int sel) |
238 | { | |
239 | struct ip6_ra_chain *ra; | |
240 | struct sock *last = NULL; | |
241 | ||
242 | read_lock(&ip6_ra_lock); | |
243 | for (ra = ip6_ra_chain; ra; ra = ra->next) { | |
244 | struct sock *sk = ra->sk; | |
0bd1b59b AM |
245 | if (sk && ra->sel == sel && |
246 | (!sk->sk_bound_dev_if || | |
247 | sk->sk_bound_dev_if == skb->dev->ifindex)) { | |
1da177e4 LT |
248 | if (last) { |
249 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); | |
250 | if (skb2) | |
251 | rawv6_rcv(last, skb2); | |
252 | } | |
253 | last = sk; | |
254 | } | |
255 | } | |
256 | ||
257 | if (last) { | |
258 | rawv6_rcv(last, skb); | |
259 | read_unlock(&ip6_ra_lock); | |
260 | return 1; | |
261 | } | |
262 | read_unlock(&ip6_ra_lock); | |
263 | return 0; | |
264 | } | |
265 | ||
e21e0b5f VN |
266 | static int ip6_forward_proxy_check(struct sk_buff *skb) |
267 | { | |
0660e03f | 268 | struct ipv6hdr *hdr = ipv6_hdr(skb); |
e21e0b5f | 269 | u8 nexthdr = hdr->nexthdr; |
75f2811c | 270 | __be16 frag_off; |
e21e0b5f VN |
271 | int offset; |
272 | ||
273 | if (ipv6_ext_hdr(nexthdr)) { | |
75f2811c | 274 | offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off); |
e21e0b5f VN |
275 | if (offset < 0) |
276 | return 0; | |
277 | } else | |
278 | offset = sizeof(struct ipv6hdr); | |
279 | ||
280 | if (nexthdr == IPPROTO_ICMPV6) { | |
281 | struct icmp6hdr *icmp6; | |
282 | ||
d56f90a7 ACM |
283 | if (!pskb_may_pull(skb, (skb_network_header(skb) + |
284 | offset + 1 - skb->data))) | |
e21e0b5f VN |
285 | return 0; |
286 | ||
d56f90a7 | 287 | icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset); |
e21e0b5f VN |
288 | |
289 | switch (icmp6->icmp6_type) { | |
290 | case NDISC_ROUTER_SOLICITATION: | |
291 | case NDISC_ROUTER_ADVERTISEMENT: | |
292 | case NDISC_NEIGHBOUR_SOLICITATION: | |
293 | case NDISC_NEIGHBOUR_ADVERTISEMENT: | |
294 | case NDISC_REDIRECT: | |
295 | /* For reaction involving unicast neighbor discovery | |
296 | * message destined to the proxied address, pass it to | |
297 | * input function. | |
298 | */ | |
299 | return 1; | |
300 | default: | |
301 | break; | |
302 | } | |
303 | } | |
304 | ||
74553b09 VN |
305 | /* |
306 | * The proxying router can't forward traffic sent to a link-local | |
307 | * address, so signal the sender and discard the packet. This | |
308 | * behavior is clarified by the MIPv6 specification. | |
309 | */ | |
310 | if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) { | |
311 | dst_link_failure(skb); | |
312 | return -1; | |
313 | } | |
314 | ||
e21e0b5f VN |
315 | return 0; |
316 | } | |
317 | ||
1da177e4 LT |
318 | static inline int ip6_forward_finish(struct sk_buff *skb) |
319 | { | |
320 | return dst_output(skb); | |
321 | } | |
322 | ||
323 | int ip6_forward(struct sk_buff *skb) | |
324 | { | |
adf30907 | 325 | struct dst_entry *dst = skb_dst(skb); |
0660e03f | 326 | struct ipv6hdr *hdr = ipv6_hdr(skb); |
1da177e4 | 327 | struct inet6_skb_parm *opt = IP6CB(skb); |
c346dca1 | 328 | struct net *net = dev_net(dst->dev); |
14f3ad6f | 329 | u32 mtu; |
1ab1457c | 330 | |
53b7997f | 331 | if (net->ipv6.devconf_all->forwarding == 0) |
1da177e4 LT |
332 | goto error; |
333 | ||
4497b076 BH |
334 | if (skb_warn_if_lro(skb)) |
335 | goto drop; | |
336 | ||
1da177e4 | 337 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) { |
3bd653c8 | 338 | IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS); |
1da177e4 LT |
339 | goto drop; |
340 | } | |
341 | ||
72b43d08 AK |
342 | if (skb->pkt_type != PACKET_HOST) |
343 | goto drop; | |
344 | ||
35fc92a9 | 345 | skb_forward_csum(skb); |
1da177e4 LT |
346 | |
347 | /* | |
348 | * We DO NOT make any processing on | |
349 | * RA packets, pushing them to user level AS IS | |
350 | * without ane WARRANTY that application will be able | |
351 | * to interpret them. The reason is that we | |
352 | * cannot make anything clever here. | |
353 | * | |
354 | * We are not end-node, so that if packet contains | |
355 | * AH/ESP, we cannot make anything. | |
356 | * Defragmentation also would be mistake, RA packets | |
357 | * cannot be fragmented, because there is no warranty | |
358 | * that different fragments will go along one path. --ANK | |
359 | */ | |
ab4eb353 YH |
360 | if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) { |
361 | if (ip6_call_ra_chain(skb, ntohs(opt->ra))) | |
1da177e4 LT |
362 | return 0; |
363 | } | |
364 | ||
365 | /* | |
366 | * check and decrement ttl | |
367 | */ | |
368 | if (hdr->hop_limit <= 1) { | |
369 | /* Force OUTPUT device used as source address */ | |
370 | skb->dev = dst->dev; | |
3ffe533c | 371 | icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0); |
483a47d2 DL |
372 | IP6_INC_STATS_BH(net, |
373 | ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS); | |
1da177e4 LT |
374 | |
375 | kfree_skb(skb); | |
376 | return -ETIMEDOUT; | |
377 | } | |
378 | ||
fbea49e1 | 379 | /* XXX: idev->cnf.proxy_ndp? */ |
53b7997f | 380 | if (net->ipv6.devconf_all->proxy_ndp && |
8a3edd80 | 381 | pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) { |
74553b09 VN |
382 | int proxied = ip6_forward_proxy_check(skb); |
383 | if (proxied > 0) | |
e21e0b5f | 384 | return ip6_input(skb); |
74553b09 | 385 | else if (proxied < 0) { |
3bd653c8 DL |
386 | IP6_INC_STATS(net, ip6_dst_idev(dst), |
387 | IPSTATS_MIB_INDISCARDS); | |
74553b09 VN |
388 | goto drop; |
389 | } | |
e21e0b5f VN |
390 | } |
391 | ||
1da177e4 | 392 | if (!xfrm6_route_forward(skb)) { |
3bd653c8 | 393 | IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS); |
1da177e4 LT |
394 | goto drop; |
395 | } | |
adf30907 | 396 | dst = skb_dst(skb); |
1da177e4 LT |
397 | |
398 | /* IPv6 specs say nothing about it, but it is clear that we cannot | |
399 | send redirects to source routed frames. | |
1e5dc146 | 400 | We don't send redirects to frames decapsulated from IPsec. |
1da177e4 | 401 | */ |
c45a3dfb | 402 | if (skb->dev == dst->dev && opt->srcrt == 0 && !skb_sec_path(skb)) { |
1da177e4 | 403 | struct in6_addr *target = NULL; |
fbfe95a4 | 404 | struct inet_peer *peer; |
1da177e4 | 405 | struct rt6_info *rt; |
1da177e4 LT |
406 | |
407 | /* | |
408 | * incoming and outgoing devices are the same | |
409 | * send a redirect. | |
410 | */ | |
411 | ||
412 | rt = (struct rt6_info *) dst; | |
c45a3dfb DM |
413 | if (rt->rt6i_flags & RTF_GATEWAY) |
414 | target = &rt->rt6i_gateway; | |
1da177e4 LT |
415 | else |
416 | target = &hdr->daddr; | |
417 | ||
1d861aa4 | 418 | peer = inet_getpeer_v6(net->ipv6.peers, &rt->rt6i_dst.addr, 1); |
92d86829 | 419 | |
1da177e4 LT |
420 | /* Limit redirects both by destination (here) |
421 | and by source (inside ndisc_send_redirect) | |
422 | */ | |
fbfe95a4 | 423 | if (inet_peer_xrlim_allow(peer, 1*HZ)) |
4991969a | 424 | ndisc_send_redirect(skb, target); |
1d861aa4 DM |
425 | if (peer) |
426 | inet_putpeer(peer); | |
5bb1ab09 DS |
427 | } else { |
428 | int addrtype = ipv6_addr_type(&hdr->saddr); | |
429 | ||
1da177e4 | 430 | /* This check is security critical. */ |
f81b2e7d YH |
431 | if (addrtype == IPV6_ADDR_ANY || |
432 | addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK)) | |
5bb1ab09 DS |
433 | goto error; |
434 | if (addrtype & IPV6_ADDR_LINKLOCAL) { | |
435 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, | |
3ffe533c | 436 | ICMPV6_NOT_NEIGHBOUR, 0); |
5bb1ab09 DS |
437 | goto error; |
438 | } | |
1da177e4 LT |
439 | } |
440 | ||
14f3ad6f UW |
441 | mtu = dst_mtu(dst); |
442 | if (mtu < IPV6_MIN_MTU) | |
443 | mtu = IPV6_MIN_MTU; | |
444 | ||
4cdd3408 PM |
445 | if ((!skb->local_df && skb->len > mtu && !skb_is_gso(skb)) || |
446 | (IP6CB(skb)->frag_max_size && IP6CB(skb)->frag_max_size > mtu)) { | |
1da177e4 LT |
447 | /* Again, force OUTPUT device used as source address */ |
448 | skb->dev = dst->dev; | |
14f3ad6f | 449 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
483a47d2 DL |
450 | IP6_INC_STATS_BH(net, |
451 | ip6_dst_idev(dst), IPSTATS_MIB_INTOOBIGERRORS); | |
452 | IP6_INC_STATS_BH(net, | |
453 | ip6_dst_idev(dst), IPSTATS_MIB_FRAGFAILS); | |
1da177e4 LT |
454 | kfree_skb(skb); |
455 | return -EMSGSIZE; | |
456 | } | |
457 | ||
458 | if (skb_cow(skb, dst->dev->hard_header_len)) { | |
3bd653c8 | 459 | IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTDISCARDS); |
1da177e4 LT |
460 | goto drop; |
461 | } | |
462 | ||
0660e03f | 463 | hdr = ipv6_hdr(skb); |
1da177e4 LT |
464 | |
465 | /* Mangling hops number delayed to point after skb COW */ | |
1ab1457c | 466 | |
1da177e4 LT |
467 | hdr->hop_limit--; |
468 | ||
483a47d2 | 469 | IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS); |
2d8dbb04 | 470 | IP6_ADD_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTOCTETS, skb->len); |
b2e0b385 | 471 | return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dst->dev, |
6e23ae2a | 472 | ip6_forward_finish); |
1da177e4 LT |
473 | |
474 | error: | |
483a47d2 | 475 | IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS); |
1da177e4 LT |
476 | drop: |
477 | kfree_skb(skb); | |
478 | return -EINVAL; | |
479 | } | |
480 | ||
481 | static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from) | |
482 | { | |
483 | to->pkt_type = from->pkt_type; | |
484 | to->priority = from->priority; | |
485 | to->protocol = from->protocol; | |
adf30907 ED |
486 | skb_dst_drop(to); |
487 | skb_dst_set(to, dst_clone(skb_dst(from))); | |
1da177e4 | 488 | to->dev = from->dev; |
82e91ffe | 489 | to->mark = from->mark; |
1da177e4 LT |
490 | |
491 | #ifdef CONFIG_NET_SCHED | |
492 | to->tc_index = from->tc_index; | |
493 | #endif | |
e7ac05f3 | 494 | nf_copy(to, from); |
07a93626 | 495 | #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) |
ba9dda3a JK |
496 | to->nf_trace = from->nf_trace; |
497 | #endif | |
984bc16c | 498 | skb_copy_secmark(to, from); |
1da177e4 LT |
499 | } |
500 | ||
ad0081e4 | 501 | int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *)) |
1da177e4 | 502 | { |
1da177e4 | 503 | struct sk_buff *frag; |
adf30907 | 504 | struct rt6_info *rt = (struct rt6_info*)skb_dst(skb); |
d91675f9 | 505 | struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL; |
1da177e4 LT |
506 | struct ipv6hdr *tmp_hdr; |
507 | struct frag_hdr *fh; | |
508 | unsigned int mtu, hlen, left, len; | |
a7ae1992 | 509 | int hroom, troom; |
ae08e1f0 | 510 | __be32 frag_id = 0; |
1da177e4 LT |
511 | int ptr, offset = 0, err=0; |
512 | u8 *prevhdr, nexthdr = 0; | |
adf30907 | 513 | struct net *net = dev_net(skb_dst(skb)->dev); |
1da177e4 | 514 | |
1da177e4 LT |
515 | hlen = ip6_find_1stfragopt(skb, &prevhdr); |
516 | nexthdr = *prevhdr; | |
517 | ||
628a5c56 | 518 | mtu = ip6_skb_dst_mtu(skb); |
b881ef76 JH |
519 | |
520 | /* We must not fragment if the socket is set to force MTU discovery | |
14f3ad6f | 521 | * or if the skb it not generated by a local socket. |
b881ef76 | 522 | */ |
4cdd3408 PM |
523 | if (unlikely(!skb->local_df && skb->len > mtu) || |
524 | (IP6CB(skb)->frag_max_size && | |
525 | IP6CB(skb)->frag_max_size > mtu)) { | |
a34a101e ED |
526 | if (skb->sk && dst_allfrag(skb_dst(skb))) |
527 | sk_nocaps_add(skb->sk, NETIF_F_GSO_MASK); | |
528 | ||
adf30907 | 529 | skb->dev = skb_dst(skb)->dev; |
3ffe533c | 530 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
adf30907 | 531 | IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
3bd653c8 | 532 | IPSTATS_MIB_FRAGFAILS); |
b881ef76 JH |
533 | kfree_skb(skb); |
534 | return -EMSGSIZE; | |
535 | } | |
536 | ||
d91675f9 YH |
537 | if (np && np->frag_size < mtu) { |
538 | if (np->frag_size) | |
539 | mtu = np->frag_size; | |
540 | } | |
541 | mtu -= hlen + sizeof(struct frag_hdr); | |
1da177e4 | 542 | |
21dc3301 | 543 | if (skb_has_frag_list(skb)) { |
1da177e4 | 544 | int first_len = skb_pagelen(skb); |
3d13008e | 545 | struct sk_buff *frag2; |
1da177e4 LT |
546 | |
547 | if (first_len - hlen > mtu || | |
548 | ((first_len - hlen) & 7) || | |
549 | skb_cloned(skb)) | |
550 | goto slow_path; | |
551 | ||
4d9092bb | 552 | skb_walk_frags(skb, frag) { |
1da177e4 LT |
553 | /* Correct geometry. */ |
554 | if (frag->len > mtu || | |
555 | ((frag->len & 7) && frag->next) || | |
556 | skb_headroom(frag) < hlen) | |
3d13008e | 557 | goto slow_path_clean; |
1da177e4 | 558 | |
1da177e4 LT |
559 | /* Partially cloned skb? */ |
560 | if (skb_shared(frag)) | |
3d13008e | 561 | goto slow_path_clean; |
2fdba6b0 HX |
562 | |
563 | BUG_ON(frag->sk); | |
564 | if (skb->sk) { | |
2fdba6b0 HX |
565 | frag->sk = skb->sk; |
566 | frag->destructor = sock_wfree; | |
2fdba6b0 | 567 | } |
3d13008e | 568 | skb->truesize -= frag->truesize; |
1da177e4 LT |
569 | } |
570 | ||
571 | err = 0; | |
572 | offset = 0; | |
573 | frag = skb_shinfo(skb)->frag_list; | |
4d9092bb | 574 | skb_frag_list_init(skb); |
1da177e4 LT |
575 | /* BUILD HEADER */ |
576 | ||
9a217a1c | 577 | *prevhdr = NEXTHDR_FRAGMENT; |
d56f90a7 | 578 | tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC); |
1da177e4 | 579 | if (!tmp_hdr) { |
adf30907 | 580 | IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
3bd653c8 | 581 | IPSTATS_MIB_FRAGFAILS); |
1da177e4 LT |
582 | return -ENOMEM; |
583 | } | |
584 | ||
1da177e4 LT |
585 | __skb_pull(skb, hlen); |
586 | fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr)); | |
e2d1bca7 ACM |
587 | __skb_push(skb, hlen); |
588 | skb_reset_network_header(skb); | |
d56f90a7 | 589 | memcpy(skb_network_header(skb), tmp_hdr, hlen); |
1da177e4 | 590 | |
87c48fa3 | 591 | ipv6_select_ident(fh, rt); |
1da177e4 LT |
592 | fh->nexthdr = nexthdr; |
593 | fh->reserved = 0; | |
594 | fh->frag_off = htons(IP6_MF); | |
595 | frag_id = fh->identification; | |
596 | ||
597 | first_len = skb_pagelen(skb); | |
598 | skb->data_len = first_len - skb_headlen(skb); | |
599 | skb->len = first_len; | |
0660e03f ACM |
600 | ipv6_hdr(skb)->payload_len = htons(first_len - |
601 | sizeof(struct ipv6hdr)); | |
a11d206d | 602 | |
d8d1f30b | 603 | dst_hold(&rt->dst); |
1da177e4 LT |
604 | |
605 | for (;;) { | |
606 | /* Prepare header of the next frame, | |
607 | * before previous one went down. */ | |
608 | if (frag) { | |
609 | frag->ip_summed = CHECKSUM_NONE; | |
badff6d0 | 610 | skb_reset_transport_header(frag); |
1da177e4 | 611 | fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr)); |
e2d1bca7 ACM |
612 | __skb_push(frag, hlen); |
613 | skb_reset_network_header(frag); | |
d56f90a7 ACM |
614 | memcpy(skb_network_header(frag), tmp_hdr, |
615 | hlen); | |
1da177e4 LT |
616 | offset += skb->len - hlen - sizeof(struct frag_hdr); |
617 | fh->nexthdr = nexthdr; | |
618 | fh->reserved = 0; | |
619 | fh->frag_off = htons(offset); | |
620 | if (frag->next != NULL) | |
621 | fh->frag_off |= htons(IP6_MF); | |
622 | fh->identification = frag_id; | |
0660e03f ACM |
623 | ipv6_hdr(frag)->payload_len = |
624 | htons(frag->len - | |
625 | sizeof(struct ipv6hdr)); | |
1da177e4 LT |
626 | ip6_copy_metadata(frag, skb); |
627 | } | |
1ab1457c | 628 | |
1da177e4 | 629 | err = output(skb); |
dafee490 | 630 | if(!err) |
d8d1f30b | 631 | IP6_INC_STATS(net, ip6_dst_idev(&rt->dst), |
3bd653c8 | 632 | IPSTATS_MIB_FRAGCREATES); |
dafee490 | 633 | |
1da177e4 LT |
634 | if (err || !frag) |
635 | break; | |
636 | ||
637 | skb = frag; | |
638 | frag = skb->next; | |
639 | skb->next = NULL; | |
640 | } | |
641 | ||
a51482bd | 642 | kfree(tmp_hdr); |
1da177e4 LT |
643 | |
644 | if (err == 0) { | |
d8d1f30b | 645 | IP6_INC_STATS(net, ip6_dst_idev(&rt->dst), |
3bd653c8 | 646 | IPSTATS_MIB_FRAGOKS); |
94e187c0 | 647 | ip6_rt_put(rt); |
1da177e4 LT |
648 | return 0; |
649 | } | |
650 | ||
651 | while (frag) { | |
652 | skb = frag->next; | |
653 | kfree_skb(frag); | |
654 | frag = skb; | |
655 | } | |
656 | ||
d8d1f30b | 657 | IP6_INC_STATS(net, ip6_dst_idev(&rt->dst), |
3bd653c8 | 658 | IPSTATS_MIB_FRAGFAILS); |
94e187c0 | 659 | ip6_rt_put(rt); |
1da177e4 | 660 | return err; |
3d13008e ED |
661 | |
662 | slow_path_clean: | |
663 | skb_walk_frags(skb, frag2) { | |
664 | if (frag2 == frag) | |
665 | break; | |
666 | frag2->sk = NULL; | |
667 | frag2->destructor = NULL; | |
668 | skb->truesize += frag2->truesize; | |
669 | } | |
1da177e4 LT |
670 | } |
671 | ||
672 | slow_path: | |
72e843bb ED |
673 | if ((skb->ip_summed == CHECKSUM_PARTIAL) && |
674 | skb_checksum_help(skb)) | |
675 | goto fail; | |
676 | ||
1da177e4 LT |
677 | left = skb->len - hlen; /* Space per frame */ |
678 | ptr = hlen; /* Where to start from */ | |
679 | ||
680 | /* | |
681 | * Fragment the datagram. | |
682 | */ | |
683 | ||
684 | *prevhdr = NEXTHDR_FRAGMENT; | |
a7ae1992 HX |
685 | hroom = LL_RESERVED_SPACE(rt->dst.dev); |
686 | troom = rt->dst.dev->needed_tailroom; | |
1da177e4 LT |
687 | |
688 | /* | |
689 | * Keep copying data until we run out. | |
690 | */ | |
691 | while(left > 0) { | |
692 | len = left; | |
693 | /* IF: it doesn't fit, use 'mtu' - the data space left */ | |
694 | if (len > mtu) | |
695 | len = mtu; | |
25985edc | 696 | /* IF: we are not sending up to and including the packet end |
1da177e4 LT |
697 | then align the next start on an eight byte boundary */ |
698 | if (len < left) { | |
699 | len &= ~7; | |
700 | } | |
701 | /* | |
702 | * Allocate buffer. | |
703 | */ | |
704 | ||
a7ae1992 HX |
705 | if ((frag = alloc_skb(len + hlen + sizeof(struct frag_hdr) + |
706 | hroom + troom, GFP_ATOMIC)) == NULL) { | |
64ce2073 | 707 | NETDEBUG(KERN_INFO "IPv6: frag: no memory for new fragment!\n"); |
adf30907 | 708 | IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
a11d206d | 709 | IPSTATS_MIB_FRAGFAILS); |
1da177e4 LT |
710 | err = -ENOMEM; |
711 | goto fail; | |
712 | } | |
713 | ||
714 | /* | |
715 | * Set up data on packet | |
716 | */ | |
717 | ||
718 | ip6_copy_metadata(frag, skb); | |
a7ae1992 | 719 | skb_reserve(frag, hroom); |
1da177e4 | 720 | skb_put(frag, len + hlen + sizeof(struct frag_hdr)); |
c1d2bbe1 | 721 | skb_reset_network_header(frag); |
badff6d0 | 722 | fh = (struct frag_hdr *)(skb_network_header(frag) + hlen); |
b0e380b1 ACM |
723 | frag->transport_header = (frag->network_header + hlen + |
724 | sizeof(struct frag_hdr)); | |
1da177e4 LT |
725 | |
726 | /* | |
727 | * Charge the memory for the fragment to any owner | |
728 | * it might possess | |
729 | */ | |
730 | if (skb->sk) | |
731 | skb_set_owner_w(frag, skb->sk); | |
732 | ||
733 | /* | |
734 | * Copy the packet header into the new buffer. | |
735 | */ | |
d626f62b | 736 | skb_copy_from_linear_data(skb, skb_network_header(frag), hlen); |
1da177e4 LT |
737 | |
738 | /* | |
739 | * Build fragment header. | |
740 | */ | |
741 | fh->nexthdr = nexthdr; | |
742 | fh->reserved = 0; | |
f36d6ab1 | 743 | if (!frag_id) { |
87c48fa3 | 744 | ipv6_select_ident(fh, rt); |
1da177e4 LT |
745 | frag_id = fh->identification; |
746 | } else | |
747 | fh->identification = frag_id; | |
748 | ||
749 | /* | |
750 | * Copy a block of the IP datagram. | |
751 | */ | |
8984e41d | 752 | if (skb_copy_bits(skb, ptr, skb_transport_header(frag), len)) |
1da177e4 LT |
753 | BUG(); |
754 | left -= len; | |
755 | ||
756 | fh->frag_off = htons(offset); | |
757 | if (left > 0) | |
758 | fh->frag_off |= htons(IP6_MF); | |
0660e03f ACM |
759 | ipv6_hdr(frag)->payload_len = htons(frag->len - |
760 | sizeof(struct ipv6hdr)); | |
1da177e4 LT |
761 | |
762 | ptr += len; | |
763 | offset += len; | |
764 | ||
765 | /* | |
766 | * Put this fragment into the sending queue. | |
767 | */ | |
1da177e4 LT |
768 | err = output(frag); |
769 | if (err) | |
770 | goto fail; | |
dafee490 | 771 | |
adf30907 | 772 | IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
3bd653c8 | 773 | IPSTATS_MIB_FRAGCREATES); |
1da177e4 | 774 | } |
adf30907 | 775 | IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
a11d206d | 776 | IPSTATS_MIB_FRAGOKS); |
808db80a | 777 | consume_skb(skb); |
1da177e4 LT |
778 | return err; |
779 | ||
780 | fail: | |
adf30907 | 781 | IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
a11d206d | 782 | IPSTATS_MIB_FRAGFAILS); |
1ab1457c | 783 | kfree_skb(skb); |
1da177e4 LT |
784 | return err; |
785 | } | |
786 | ||
b71d1d42 ED |
787 | static inline int ip6_rt_check(const struct rt6key *rt_key, |
788 | const struct in6_addr *fl_addr, | |
789 | const struct in6_addr *addr_cache) | |
cf6b1982 | 790 | { |
a02cec21 ED |
791 | return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) && |
792 | (addr_cache == NULL || !ipv6_addr_equal(fl_addr, addr_cache)); | |
cf6b1982 YH |
793 | } |
794 | ||
497c615a HX |
795 | static struct dst_entry *ip6_sk_dst_check(struct sock *sk, |
796 | struct dst_entry *dst, | |
b71d1d42 | 797 | const struct flowi6 *fl6) |
1da177e4 | 798 | { |
497c615a | 799 | struct ipv6_pinfo *np = inet6_sk(sk); |
a963a37d | 800 | struct rt6_info *rt; |
1da177e4 | 801 | |
497c615a HX |
802 | if (!dst) |
803 | goto out; | |
804 | ||
a963a37d ED |
805 | if (dst->ops->family != AF_INET6) { |
806 | dst_release(dst); | |
807 | return NULL; | |
808 | } | |
809 | ||
810 | rt = (struct rt6_info *)dst; | |
497c615a HX |
811 | /* Yes, checking route validity in not connected |
812 | * case is not very simple. Take into account, | |
813 | * that we do not support routing by source, TOS, | |
814 | * and MSG_DONTROUTE --ANK (980726) | |
815 | * | |
cf6b1982 YH |
816 | * 1. ip6_rt_check(): If route was host route, |
817 | * check that cached destination is current. | |
497c615a HX |
818 | * If it is network route, we still may |
819 | * check its validity using saved pointer | |
820 | * to the last used address: daddr_cache. | |
821 | * We do not want to save whole address now, | |
822 | * (because main consumer of this service | |
823 | * is tcp, which has not this problem), | |
824 | * so that the last trick works only on connected | |
825 | * sockets. | |
826 | * 2. oif also should be the same. | |
827 | */ | |
4c9483b2 | 828 | if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) || |
8e1ef0a9 | 829 | #ifdef CONFIG_IPV6_SUBTREES |
4c9483b2 | 830 | ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) || |
8e1ef0a9 | 831 | #endif |
4c9483b2 | 832 | (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex)) { |
497c615a HX |
833 | dst_release(dst); |
834 | dst = NULL; | |
1da177e4 LT |
835 | } |
836 | ||
497c615a HX |
837 | out: |
838 | return dst; | |
839 | } | |
840 | ||
841 | static int ip6_dst_lookup_tail(struct sock *sk, | |
4c9483b2 | 842 | struct dst_entry **dst, struct flowi6 *fl6) |
497c615a | 843 | { |
3b1e0a65 | 844 | struct net *net = sock_net(sk); |
69cce1d1 DM |
845 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
846 | struct neighbour *n; | |
97cac082 | 847 | struct rt6_info *rt; |
69cce1d1 DM |
848 | #endif |
849 | int err; | |
497c615a | 850 | |
1da177e4 | 851 | if (*dst == NULL) |
4c9483b2 | 852 | *dst = ip6_route_output(net, sk, fl6); |
1da177e4 LT |
853 | |
854 | if ((err = (*dst)->error)) | |
855 | goto out_err_release; | |
856 | ||
4c9483b2 | 857 | if (ipv6_addr_any(&fl6->saddr)) { |
c3968a85 DW |
858 | struct rt6_info *rt = (struct rt6_info *) *dst; |
859 | err = ip6_route_get_saddr(net, rt, &fl6->daddr, | |
860 | sk ? inet6_sk(sk)->srcprefs : 0, | |
861 | &fl6->saddr); | |
44456d37 | 862 | if (err) |
1da177e4 | 863 | goto out_err_release; |
1da177e4 LT |
864 | } |
865 | ||
95c385b4 | 866 | #ifdef CONFIG_IPV6_OPTIMISTIC_DAD |
e550dfb0 NH |
867 | /* |
868 | * Here if the dst entry we've looked up | |
869 | * has a neighbour entry that is in the INCOMPLETE | |
870 | * state and the src address from the flow is | |
871 | * marked as OPTIMISTIC, we release the found | |
872 | * dst entry and replace it instead with the | |
873 | * dst entry of the nexthop router | |
874 | */ | |
c56bf6fe | 875 | rt = (struct rt6_info *) *dst; |
707be1ff YH |
876 | rcu_read_lock_bh(); |
877 | n = __ipv6_neigh_lookup_noref(rt->dst.dev, rt6_nexthop(rt, &fl6->daddr)); | |
878 | err = n && !(n->nud_state & NUD_VALID) ? -EINVAL : 0; | |
879 | rcu_read_unlock_bh(); | |
880 | ||
881 | if (err) { | |
e550dfb0 | 882 | struct inet6_ifaddr *ifp; |
4c9483b2 | 883 | struct flowi6 fl_gw6; |
e550dfb0 NH |
884 | int redirect; |
885 | ||
4c9483b2 | 886 | ifp = ipv6_get_ifaddr(net, &fl6->saddr, |
e550dfb0 NH |
887 | (*dst)->dev, 1); |
888 | ||
889 | redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC); | |
890 | if (ifp) | |
891 | in6_ifa_put(ifp); | |
892 | ||
893 | if (redirect) { | |
894 | /* | |
895 | * We need to get the dst entry for the | |
896 | * default router instead | |
897 | */ | |
898 | dst_release(*dst); | |
4c9483b2 DM |
899 | memcpy(&fl_gw6, fl6, sizeof(struct flowi6)); |
900 | memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr)); | |
901 | *dst = ip6_route_output(net, sk, &fl_gw6); | |
e550dfb0 NH |
902 | if ((err = (*dst)->error)) |
903 | goto out_err_release; | |
95c385b4 | 904 | } |
e550dfb0 | 905 | } |
95c385b4 NH |
906 | #endif |
907 | ||
1da177e4 LT |
908 | return 0; |
909 | ||
910 | out_err_release: | |
ca46f9c8 | 911 | if (err == -ENETUNREACH) |
483a47d2 | 912 | IP6_INC_STATS_BH(net, NULL, IPSTATS_MIB_OUTNOROUTES); |
1da177e4 LT |
913 | dst_release(*dst); |
914 | *dst = NULL; | |
915 | return err; | |
916 | } | |
34a0b3cd | 917 | |
497c615a HX |
918 | /** |
919 | * ip6_dst_lookup - perform route lookup on flow | |
920 | * @sk: socket which provides route info | |
921 | * @dst: pointer to dst_entry * for result | |
4c9483b2 | 922 | * @fl6: flow to lookup |
497c615a HX |
923 | * |
924 | * This function performs a route lookup on the given flow. | |
925 | * | |
926 | * It returns zero on success, or a standard errno code on error. | |
927 | */ | |
4c9483b2 | 928 | int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi6 *fl6) |
497c615a HX |
929 | { |
930 | *dst = NULL; | |
4c9483b2 | 931 | return ip6_dst_lookup_tail(sk, dst, fl6); |
497c615a | 932 | } |
3cf3dc6c ACM |
933 | EXPORT_SYMBOL_GPL(ip6_dst_lookup); |
934 | ||
497c615a | 935 | /** |
68d0c6d3 DM |
936 | * ip6_dst_lookup_flow - perform route lookup on flow with ipsec |
937 | * @sk: socket which provides route info | |
4c9483b2 | 938 | * @fl6: flow to lookup |
68d0c6d3 | 939 | * @final_dst: final destination address for ipsec lookup |
a1414715 | 940 | * @can_sleep: we are in a sleepable context |
68d0c6d3 DM |
941 | * |
942 | * This function performs a route lookup on the given flow. | |
943 | * | |
944 | * It returns a valid dst pointer on success, or a pointer encoded | |
945 | * error code. | |
946 | */ | |
4c9483b2 | 947 | struct dst_entry *ip6_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6, |
68d0c6d3 | 948 | const struct in6_addr *final_dst, |
a1414715 | 949 | bool can_sleep) |
68d0c6d3 DM |
950 | { |
951 | struct dst_entry *dst = NULL; | |
952 | int err; | |
953 | ||
4c9483b2 | 954 | err = ip6_dst_lookup_tail(sk, &dst, fl6); |
68d0c6d3 DM |
955 | if (err) |
956 | return ERR_PTR(err); | |
957 | if (final_dst) | |
4e3fd7a0 | 958 | fl6->daddr = *final_dst; |
2774c131 | 959 | if (can_sleep) |
4c9483b2 | 960 | fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP; |
2774c131 | 961 | |
4c9483b2 | 962 | return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0); |
68d0c6d3 DM |
963 | } |
964 | EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow); | |
965 | ||
966 | /** | |
967 | * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow | |
497c615a | 968 | * @sk: socket which provides the dst cache and route info |
4c9483b2 | 969 | * @fl6: flow to lookup |
68d0c6d3 | 970 | * @final_dst: final destination address for ipsec lookup |
a1414715 | 971 | * @can_sleep: we are in a sleepable context |
497c615a HX |
972 | * |
973 | * This function performs a route lookup on the given flow with the | |
974 | * possibility of using the cached route in the socket if it is valid. | |
975 | * It will take the socket dst lock when operating on the dst cache. | |
976 | * As a result, this function can only be used in process context. | |
977 | * | |
68d0c6d3 DM |
978 | * It returns a valid dst pointer on success, or a pointer encoded |
979 | * error code. | |
497c615a | 980 | */ |
4c9483b2 | 981 | struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6, |
68d0c6d3 | 982 | const struct in6_addr *final_dst, |
a1414715 | 983 | bool can_sleep) |
497c615a | 984 | { |
68d0c6d3 DM |
985 | struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie); |
986 | int err; | |
497c615a | 987 | |
4c9483b2 | 988 | dst = ip6_sk_dst_check(sk, dst, fl6); |
68d0c6d3 | 989 | |
4c9483b2 | 990 | err = ip6_dst_lookup_tail(sk, &dst, fl6); |
68d0c6d3 DM |
991 | if (err) |
992 | return ERR_PTR(err); | |
993 | if (final_dst) | |
4e3fd7a0 | 994 | fl6->daddr = *final_dst; |
2774c131 | 995 | if (can_sleep) |
4c9483b2 | 996 | fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP; |
2774c131 | 997 | |
4c9483b2 | 998 | return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0); |
497c615a | 999 | } |
68d0c6d3 | 1000 | EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow); |
497c615a | 1001 | |
34a0b3cd | 1002 | static inline int ip6_ufo_append_data(struct sock *sk, |
e89e9cf5 AR |
1003 | int getfrag(void *from, char *to, int offset, int len, |
1004 | int odd, struct sk_buff *skb), | |
1005 | void *from, int length, int hh_len, int fragheaderlen, | |
87c48fa3 ED |
1006 | int transhdrlen, int mtu,unsigned int flags, |
1007 | struct rt6_info *rt) | |
e89e9cf5 AR |
1008 | |
1009 | { | |
1010 | struct sk_buff *skb; | |
c547dbf5 | 1011 | struct frag_hdr fhdr; |
e89e9cf5 AR |
1012 | int err; |
1013 | ||
1014 | /* There is support for UDP large send offload by network | |
1015 | * device, so create one single skb packet containing complete | |
1016 | * udp datagram | |
1017 | */ | |
1018 | if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) { | |
1019 | skb = sock_alloc_send_skb(sk, | |
1020 | hh_len + fragheaderlen + transhdrlen + 20, | |
1021 | (flags & MSG_DONTWAIT), &err); | |
1022 | if (skb == NULL) | |
504744e4 | 1023 | return err; |
e89e9cf5 AR |
1024 | |
1025 | /* reserve space for Hardware header */ | |
1026 | skb_reserve(skb, hh_len); | |
1027 | ||
1028 | /* create space for UDP/IP header */ | |
1029 | skb_put(skb,fragheaderlen + transhdrlen); | |
1030 | ||
1031 | /* initialize network header pointer */ | |
c1d2bbe1 | 1032 | skb_reset_network_header(skb); |
e89e9cf5 AR |
1033 | |
1034 | /* initialize protocol header pointer */ | |
b0e380b1 | 1035 | skb->transport_header = skb->network_header + fragheaderlen; |
e89e9cf5 | 1036 | |
9c9c9ad5 | 1037 | skb->protocol = htons(ETH_P_IPV6); |
e89e9cf5 | 1038 | skb->csum = 0; |
e89e9cf5 | 1039 | |
e89e9cf5 | 1040 | __skb_queue_tail(&sk->sk_write_queue, skb); |
c547dbf5 JP |
1041 | } else if (skb_is_gso(skb)) { |
1042 | goto append; | |
e89e9cf5 | 1043 | } |
e89e9cf5 | 1044 | |
c547dbf5 JP |
1045 | skb->ip_summed = CHECKSUM_PARTIAL; |
1046 | /* Specify the length of each IPv6 datagram fragment. | |
1047 | * It has to be a multiple of 8. | |
1048 | */ | |
1049 | skb_shinfo(skb)->gso_size = (mtu - fragheaderlen - | |
1050 | sizeof(struct frag_hdr)) & ~7; | |
1051 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP; | |
1052 | ipv6_select_ident(&fhdr, rt); | |
1053 | skb_shinfo(skb)->ip6_frag_id = fhdr.identification; | |
1054 | ||
1055 | append: | |
2811ebac HFS |
1056 | return skb_append_datato_frags(sk, skb, getfrag, from, |
1057 | (length - transhdrlen)); | |
e89e9cf5 | 1058 | } |
1da177e4 | 1059 | |
0178b695 HX |
1060 | static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src, |
1061 | gfp_t gfp) | |
1062 | { | |
1063 | return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL; | |
1064 | } | |
1065 | ||
1066 | static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src, | |
1067 | gfp_t gfp) | |
1068 | { | |
1069 | return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL; | |
1070 | } | |
1071 | ||
75a493e6 | 1072 | static void ip6_append_data_mtu(unsigned int *mtu, |
0c183379 G |
1073 | int *maxfraglen, |
1074 | unsigned int fragheaderlen, | |
1075 | struct sk_buff *skb, | |
75a493e6 HFS |
1076 | struct rt6_info *rt, |
1077 | bool pmtuprobe) | |
0c183379 G |
1078 | { |
1079 | if (!(rt->dst.flags & DST_XFRM_TUNNEL)) { | |
1080 | if (skb == NULL) { | |
1081 | /* first fragment, reserve header_len */ | |
1082 | *mtu = *mtu - rt->dst.header_len; | |
1083 | ||
1084 | } else { | |
1085 | /* | |
1086 | * this fragment is not first, the headers | |
1087 | * space is regarded as data space. | |
1088 | */ | |
75a493e6 HFS |
1089 | *mtu = min(*mtu, pmtuprobe ? |
1090 | rt->dst.dev->mtu : | |
1091 | dst_mtu(rt->dst.path)); | |
0c183379 G |
1092 | } |
1093 | *maxfraglen = ((*mtu - fragheaderlen) & ~7) | |
1094 | + fragheaderlen - sizeof(struct frag_hdr); | |
1095 | } | |
1096 | } | |
1097 | ||
41a1f8ea YH |
1098 | int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to, |
1099 | int offset, int len, int odd, struct sk_buff *skb), | |
1100 | void *from, int length, int transhdrlen, | |
4c9483b2 | 1101 | int hlimit, int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6, |
13b52cd4 | 1102 | struct rt6_info *rt, unsigned int flags, int dontfrag) |
1da177e4 LT |
1103 | { |
1104 | struct inet_sock *inet = inet_sk(sk); | |
1105 | struct ipv6_pinfo *np = inet6_sk(sk); | |
bdc712b4 | 1106 | struct inet_cork *cork; |
0c183379 | 1107 | struct sk_buff *skb, *skb_prev = NULL; |
75a493e6 | 1108 | unsigned int maxfraglen, fragheaderlen, mtu; |
1da177e4 | 1109 | int exthdrlen; |
299b0767 | 1110 | int dst_exthdrlen; |
1da177e4 | 1111 | int hh_len; |
1da177e4 LT |
1112 | int copy; |
1113 | int err; | |
1114 | int offset = 0; | |
a693e698 | 1115 | __u8 tx_flags = 0; |
1da177e4 LT |
1116 | |
1117 | if (flags&MSG_PROBE) | |
1118 | return 0; | |
bdc712b4 | 1119 | cork = &inet->cork.base; |
1da177e4 LT |
1120 | if (skb_queue_empty(&sk->sk_write_queue)) { |
1121 | /* | |
1122 | * setup for corking | |
1123 | */ | |
1124 | if (opt) { | |
0178b695 | 1125 | if (WARN_ON(np->cork.opt)) |
1da177e4 | 1126 | return -EINVAL; |
0178b695 | 1127 | |
284041ef | 1128 | np->cork.opt = kzalloc(opt->tot_len, sk->sk_allocation); |
0178b695 HX |
1129 | if (unlikely(np->cork.opt == NULL)) |
1130 | return -ENOBUFS; | |
1131 | ||
1132 | np->cork.opt->tot_len = opt->tot_len; | |
1133 | np->cork.opt->opt_flen = opt->opt_flen; | |
1134 | np->cork.opt->opt_nflen = opt->opt_nflen; | |
1135 | ||
1136 | np->cork.opt->dst0opt = ip6_opt_dup(opt->dst0opt, | |
1137 | sk->sk_allocation); | |
1138 | if (opt->dst0opt && !np->cork.opt->dst0opt) | |
1139 | return -ENOBUFS; | |
1140 | ||
1141 | np->cork.opt->dst1opt = ip6_opt_dup(opt->dst1opt, | |
1142 | sk->sk_allocation); | |
1143 | if (opt->dst1opt && !np->cork.opt->dst1opt) | |
1144 | return -ENOBUFS; | |
1145 | ||
1146 | np->cork.opt->hopopt = ip6_opt_dup(opt->hopopt, | |
1147 | sk->sk_allocation); | |
1148 | if (opt->hopopt && !np->cork.opt->hopopt) | |
1149 | return -ENOBUFS; | |
1150 | ||
1151 | np->cork.opt->srcrt = ip6_rthdr_dup(opt->srcrt, | |
1152 | sk->sk_allocation); | |
1153 | if (opt->srcrt && !np->cork.opt->srcrt) | |
1154 | return -ENOBUFS; | |
1155 | ||
1da177e4 LT |
1156 | /* need source address above miyazawa*/ |
1157 | } | |
d8d1f30b | 1158 | dst_hold(&rt->dst); |
bdc712b4 | 1159 | cork->dst = &rt->dst; |
4c9483b2 | 1160 | inet->cork.fl.u.ip6 = *fl6; |
1da177e4 | 1161 | np->cork.hop_limit = hlimit; |
41a1f8ea | 1162 | np->cork.tclass = tclass; |
0c183379 G |
1163 | if (rt->dst.flags & DST_XFRM_TUNNEL) |
1164 | mtu = np->pmtudisc == IPV6_PMTUDISC_PROBE ? | |
1165 | rt->dst.dev->mtu : dst_mtu(&rt->dst); | |
1166 | else | |
1167 | mtu = np->pmtudisc == IPV6_PMTUDISC_PROBE ? | |
1168 | rt->dst.dev->mtu : dst_mtu(rt->dst.path); | |
c7503609 | 1169 | if (np->frag_size < mtu) { |
d91675f9 YH |
1170 | if (np->frag_size) |
1171 | mtu = np->frag_size; | |
1172 | } | |
bdc712b4 | 1173 | cork->fragsize = mtu; |
d8d1f30b | 1174 | if (dst_allfrag(rt->dst.path)) |
bdc712b4 DM |
1175 | cork->flags |= IPCORK_ALLFRAG; |
1176 | cork->length = 0; | |
7efdba5b | 1177 | exthdrlen = (opt ? opt->opt_flen : 0); |
1da177e4 LT |
1178 | length += exthdrlen; |
1179 | transhdrlen += exthdrlen; | |
7efdba5b | 1180 | dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len; |
1da177e4 | 1181 | } else { |
bdc712b4 | 1182 | rt = (struct rt6_info *)cork->dst; |
4c9483b2 | 1183 | fl6 = &inet->cork.fl.u.ip6; |
0178b695 | 1184 | opt = np->cork.opt; |
1da177e4 LT |
1185 | transhdrlen = 0; |
1186 | exthdrlen = 0; | |
299b0767 | 1187 | dst_exthdrlen = 0; |
bdc712b4 | 1188 | mtu = cork->fragsize; |
1da177e4 LT |
1189 | } |
1190 | ||
d8d1f30b | 1191 | hh_len = LL_RESERVED_SPACE(rt->dst.dev); |
1da177e4 | 1192 | |
a1b05140 | 1193 | fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len + |
b4ce9277 | 1194 | (opt ? opt->opt_nflen : 0); |
1da177e4 LT |
1195 | maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr); |
1196 | ||
1197 | if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) { | |
bdc712b4 | 1198 | if (cork->length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) { |
4c9483b2 | 1199 | ipv6_local_error(sk, EMSGSIZE, fl6, mtu-exthdrlen); |
1da177e4 LT |
1200 | return -EMSGSIZE; |
1201 | } | |
1202 | } | |
1203 | ||
a693e698 | 1204 | /* For UDP, check if TX timestamp is enabled */ |
bf84a010 DB |
1205 | if (sk->sk_type == SOCK_DGRAM) |
1206 | sock_tx_timestamp(sk, &tx_flags); | |
a693e698 | 1207 | |
1da177e4 LT |
1208 | /* |
1209 | * Let's try using as much space as possible. | |
1210 | * Use MTU if total length of the message fits into the MTU. | |
1211 | * Otherwise, we need to reserve fragment header and | |
1212 | * fragment alignment (= 8-15 octects, in total). | |
1213 | * | |
1214 | * Note that we may need to "move" the data from the tail of | |
1ab1457c | 1215 | * of the buffer to the new fragment when we split |
1da177e4 LT |
1216 | * the message. |
1217 | * | |
1ab1457c | 1218 | * FIXME: It may be fragmented into multiple chunks |
1da177e4 LT |
1219 | * at once if non-fragmentable extension headers |
1220 | * are too large. | |
1ab1457c | 1221 | * --yoshfuji |
1da177e4 LT |
1222 | */ |
1223 | ||
2811ebac HFS |
1224 | if ((length > mtu) && dontfrag && (sk->sk_protocol == IPPROTO_UDP || |
1225 | sk->sk_protocol == IPPROTO_RAW)) { | |
1226 | ipv6_local_rxpmtu(sk, fl6, mtu-exthdrlen); | |
1227 | return -EMSGSIZE; | |
1228 | } | |
4b340ae2 | 1229 | |
2811ebac HFS |
1230 | skb = skb_peek_tail(&sk->sk_write_queue); |
1231 | cork->length += length; | |
1232 | if (((length > mtu) || | |
1233 | (skb && skb_is_gso(skb))) && | |
1234 | (sk->sk_protocol == IPPROTO_UDP) && | |
1235 | (rt->dst.dev->features & NETIF_F_UFO)) { | |
1236 | err = ip6_ufo_append_data(sk, getfrag, from, length, | |
1237 | hh_len, fragheaderlen, | |
1238 | transhdrlen, mtu, flags, rt); | |
1239 | if (err) | |
1240 | goto error; | |
1241 | return 0; | |
e89e9cf5 | 1242 | } |
1da177e4 | 1243 | |
2811ebac | 1244 | if (!skb) |
1da177e4 LT |
1245 | goto alloc_new_skb; |
1246 | ||
1247 | while (length > 0) { | |
1248 | /* Check if the remaining data fits into current packet. */ | |
bdc712b4 | 1249 | copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len; |
1da177e4 LT |
1250 | if (copy < length) |
1251 | copy = maxfraglen - skb->len; | |
1252 | ||
1253 | if (copy <= 0) { | |
1254 | char *data; | |
1255 | unsigned int datalen; | |
1256 | unsigned int fraglen; | |
1257 | unsigned int fraggap; | |
1258 | unsigned int alloclen; | |
1da177e4 | 1259 | alloc_new_skb: |
1da177e4 | 1260 | /* There's no room in the current skb */ |
0c183379 G |
1261 | if (skb) |
1262 | fraggap = skb->len - maxfraglen; | |
1da177e4 LT |
1263 | else |
1264 | fraggap = 0; | |
0c183379 G |
1265 | /* update mtu and maxfraglen if necessary */ |
1266 | if (skb == NULL || skb_prev == NULL) | |
1267 | ip6_append_data_mtu(&mtu, &maxfraglen, | |
75a493e6 HFS |
1268 | fragheaderlen, skb, rt, |
1269 | np->pmtudisc == | |
1270 | IPV6_PMTUDISC_PROBE); | |
0c183379 G |
1271 | |
1272 | skb_prev = skb; | |
1da177e4 LT |
1273 | |
1274 | /* | |
1275 | * If remaining data exceeds the mtu, | |
1276 | * we know we need more fragment(s). | |
1277 | */ | |
1278 | datalen = length + fraggap; | |
1da177e4 | 1279 | |
0c183379 G |
1280 | if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen) |
1281 | datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len; | |
1da177e4 | 1282 | if ((flags & MSG_MORE) && |
d8d1f30b | 1283 | !(rt->dst.dev->features&NETIF_F_SG)) |
1da177e4 LT |
1284 | alloclen = mtu; |
1285 | else | |
1286 | alloclen = datalen + fragheaderlen; | |
1287 | ||
299b0767 SK |
1288 | alloclen += dst_exthdrlen; |
1289 | ||
0c183379 G |
1290 | if (datalen != length + fraggap) { |
1291 | /* | |
1292 | * this is not the last fragment, the trailer | |
1293 | * space is regarded as data space. | |
1294 | */ | |
1295 | datalen += rt->dst.trailer_len; | |
1296 | } | |
1297 | ||
1298 | alloclen += rt->dst.trailer_len; | |
1299 | fraglen = datalen + fragheaderlen; | |
1da177e4 LT |
1300 | |
1301 | /* | |
1302 | * We just reserve space for fragment header. | |
1ab1457c | 1303 | * Note: this may be overallocation if the message |
1da177e4 LT |
1304 | * (without MSG_MORE) fits into the MTU. |
1305 | */ | |
1306 | alloclen += sizeof(struct frag_hdr); | |
1307 | ||
1308 | if (transhdrlen) { | |
1309 | skb = sock_alloc_send_skb(sk, | |
1310 | alloclen + hh_len, | |
1311 | (flags & MSG_DONTWAIT), &err); | |
1312 | } else { | |
1313 | skb = NULL; | |
1314 | if (atomic_read(&sk->sk_wmem_alloc) <= | |
1315 | 2 * sk->sk_sndbuf) | |
1316 | skb = sock_wmalloc(sk, | |
1317 | alloclen + hh_len, 1, | |
1318 | sk->sk_allocation); | |
1319 | if (unlikely(skb == NULL)) | |
1320 | err = -ENOBUFS; | |
a693e698 AB |
1321 | else { |
1322 | /* Only the initial fragment | |
1323 | * is time stamped. | |
1324 | */ | |
1325 | tx_flags = 0; | |
1326 | } | |
1da177e4 LT |
1327 | } |
1328 | if (skb == NULL) | |
1329 | goto error; | |
1330 | /* | |
1331 | * Fill in the control structures | |
1332 | */ | |
9c9c9ad5 | 1333 | skb->protocol = htons(ETH_P_IPV6); |
d7f7c0ac | 1334 | skb->ip_summed = CHECKSUM_NONE; |
1da177e4 | 1335 | skb->csum = 0; |
1f85851e G |
1336 | /* reserve for fragmentation and ipsec header */ |
1337 | skb_reserve(skb, hh_len + sizeof(struct frag_hdr) + | |
1338 | dst_exthdrlen); | |
1da177e4 | 1339 | |
a693e698 AB |
1340 | if (sk->sk_type == SOCK_DGRAM) |
1341 | skb_shinfo(skb)->tx_flags = tx_flags; | |
1342 | ||
1da177e4 LT |
1343 | /* |
1344 | * Find where to start putting bytes | |
1345 | */ | |
1f85851e G |
1346 | data = skb_put(skb, fraglen); |
1347 | skb_set_network_header(skb, exthdrlen); | |
1348 | data += fragheaderlen; | |
b0e380b1 ACM |
1349 | skb->transport_header = (skb->network_header + |
1350 | fragheaderlen); | |
1da177e4 LT |
1351 | if (fraggap) { |
1352 | skb->csum = skb_copy_and_csum_bits( | |
1353 | skb_prev, maxfraglen, | |
1354 | data + transhdrlen, fraggap, 0); | |
1355 | skb_prev->csum = csum_sub(skb_prev->csum, | |
1356 | skb->csum); | |
1357 | data += fraggap; | |
e9fa4f7b | 1358 | pskb_trim_unique(skb_prev, maxfraglen); |
1da177e4 LT |
1359 | } |
1360 | copy = datalen - transhdrlen - fraggap; | |
299b0767 | 1361 | |
1da177e4 LT |
1362 | if (copy < 0) { |
1363 | err = -EINVAL; | |
1364 | kfree_skb(skb); | |
1365 | goto error; | |
1366 | } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) { | |
1367 | err = -EFAULT; | |
1368 | kfree_skb(skb); | |
1369 | goto error; | |
1370 | } | |
1371 | ||
1372 | offset += copy; | |
1373 | length -= datalen - fraggap; | |
1374 | transhdrlen = 0; | |
1375 | exthdrlen = 0; | |
299b0767 | 1376 | dst_exthdrlen = 0; |
1da177e4 LT |
1377 | |
1378 | /* | |
1379 | * Put the packet on the pending queue | |
1380 | */ | |
1381 | __skb_queue_tail(&sk->sk_write_queue, skb); | |
1382 | continue; | |
1383 | } | |
1384 | ||
1385 | if (copy > length) | |
1386 | copy = length; | |
1387 | ||
d8d1f30b | 1388 | if (!(rt->dst.dev->features&NETIF_F_SG)) { |
1da177e4 LT |
1389 | unsigned int off; |
1390 | ||
1391 | off = skb->len; | |
1392 | if (getfrag(from, skb_put(skb, copy), | |
1393 | offset, copy, off, skb) < 0) { | |
1394 | __skb_trim(skb, off); | |
1395 | err = -EFAULT; | |
1396 | goto error; | |
1397 | } | |
1398 | } else { | |
1399 | int i = skb_shinfo(skb)->nr_frags; | |
5640f768 | 1400 | struct page_frag *pfrag = sk_page_frag(sk); |
1da177e4 | 1401 | |
5640f768 ED |
1402 | err = -ENOMEM; |
1403 | if (!sk_page_frag_refill(sk, pfrag)) | |
1da177e4 | 1404 | goto error; |
5640f768 ED |
1405 | |
1406 | if (!skb_can_coalesce(skb, i, pfrag->page, | |
1407 | pfrag->offset)) { | |
1408 | err = -EMSGSIZE; | |
1409 | if (i == MAX_SKB_FRAGS) | |
1410 | goto error; | |
1411 | ||
1412 | __skb_fill_page_desc(skb, i, pfrag->page, | |
1413 | pfrag->offset, 0); | |
1414 | skb_shinfo(skb)->nr_frags = ++i; | |
1415 | get_page(pfrag->page); | |
1da177e4 | 1416 | } |
5640f768 | 1417 | copy = min_t(int, copy, pfrag->size - pfrag->offset); |
9e903e08 | 1418 | if (getfrag(from, |
5640f768 ED |
1419 | page_address(pfrag->page) + pfrag->offset, |
1420 | offset, copy, skb->len, skb) < 0) | |
1421 | goto error_efault; | |
1422 | ||
1423 | pfrag->offset += copy; | |
1424 | skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); | |
1da177e4 LT |
1425 | skb->len += copy; |
1426 | skb->data_len += copy; | |
f945fa7a HX |
1427 | skb->truesize += copy; |
1428 | atomic_add(copy, &sk->sk_wmem_alloc); | |
1da177e4 LT |
1429 | } |
1430 | offset += copy; | |
1431 | length -= copy; | |
1432 | } | |
5640f768 | 1433 | |
1da177e4 | 1434 | return 0; |
5640f768 ED |
1435 | |
1436 | error_efault: | |
1437 | err = -EFAULT; | |
1da177e4 | 1438 | error: |
bdc712b4 | 1439 | cork->length -= length; |
3bd653c8 | 1440 | IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); |
1da177e4 LT |
1441 | return err; |
1442 | } | |
a495f836 | 1443 | EXPORT_SYMBOL_GPL(ip6_append_data); |
1da177e4 | 1444 | |
bf138862 PE |
1445 | static void ip6_cork_release(struct inet_sock *inet, struct ipv6_pinfo *np) |
1446 | { | |
0178b695 HX |
1447 | if (np->cork.opt) { |
1448 | kfree(np->cork.opt->dst0opt); | |
1449 | kfree(np->cork.opt->dst1opt); | |
1450 | kfree(np->cork.opt->hopopt); | |
1451 | kfree(np->cork.opt->srcrt); | |
1452 | kfree(np->cork.opt); | |
1453 | np->cork.opt = NULL; | |
1454 | } | |
1455 | ||
bdc712b4 DM |
1456 | if (inet->cork.base.dst) { |
1457 | dst_release(inet->cork.base.dst); | |
1458 | inet->cork.base.dst = NULL; | |
1459 | inet->cork.base.flags &= ~IPCORK_ALLFRAG; | |
bf138862 PE |
1460 | } |
1461 | memset(&inet->cork.fl, 0, sizeof(inet->cork.fl)); | |
1462 | } | |
1463 | ||
1da177e4 LT |
1464 | int ip6_push_pending_frames(struct sock *sk) |
1465 | { | |
1466 | struct sk_buff *skb, *tmp_skb; | |
1467 | struct sk_buff **tail_skb; | |
1468 | struct in6_addr final_dst_buf, *final_dst = &final_dst_buf; | |
1469 | struct inet_sock *inet = inet_sk(sk); | |
1470 | struct ipv6_pinfo *np = inet6_sk(sk); | |
3bd653c8 | 1471 | struct net *net = sock_net(sk); |
1da177e4 LT |
1472 | struct ipv6hdr *hdr; |
1473 | struct ipv6_txoptions *opt = np->cork.opt; | |
bdc712b4 | 1474 | struct rt6_info *rt = (struct rt6_info *)inet->cork.base.dst; |
4c9483b2 DM |
1475 | struct flowi6 *fl6 = &inet->cork.fl.u.ip6; |
1476 | unsigned char proto = fl6->flowi6_proto; | |
1da177e4 LT |
1477 | int err = 0; |
1478 | ||
1479 | if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL) | |
1480 | goto out; | |
1481 | tail_skb = &(skb_shinfo(skb)->frag_list); | |
1482 | ||
1483 | /* move skb->data to ip header from ext header */ | |
d56f90a7 | 1484 | if (skb->data < skb_network_header(skb)) |
bbe735e4 | 1485 | __skb_pull(skb, skb_network_offset(skb)); |
1da177e4 | 1486 | while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) { |
cfe1fc77 | 1487 | __skb_pull(tmp_skb, skb_network_header_len(skb)); |
1da177e4 LT |
1488 | *tail_skb = tmp_skb; |
1489 | tail_skb = &(tmp_skb->next); | |
1490 | skb->len += tmp_skb->len; | |
1491 | skb->data_len += tmp_skb->len; | |
1da177e4 | 1492 | skb->truesize += tmp_skb->truesize; |
1da177e4 LT |
1493 | tmp_skb->destructor = NULL; |
1494 | tmp_skb->sk = NULL; | |
1da177e4 LT |
1495 | } |
1496 | ||
28a89453 | 1497 | /* Allow local fragmentation. */ |
b5c15fc0 | 1498 | if (np->pmtudisc < IPV6_PMTUDISC_DO) |
28a89453 HX |
1499 | skb->local_df = 1; |
1500 | ||
4e3fd7a0 | 1501 | *final_dst = fl6->daddr; |
cfe1fc77 | 1502 | __skb_pull(skb, skb_network_header_len(skb)); |
1da177e4 LT |
1503 | if (opt && opt->opt_flen) |
1504 | ipv6_push_frag_opts(skb, opt, &proto); | |
1505 | if (opt && opt->opt_nflen) | |
1506 | ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst); | |
1507 | ||
e2d1bca7 ACM |
1508 | skb_push(skb, sizeof(struct ipv6hdr)); |
1509 | skb_reset_network_header(skb); | |
0660e03f | 1510 | hdr = ipv6_hdr(skb); |
1ab1457c | 1511 | |
3e4e4c1f | 1512 | ip6_flow_hdr(hdr, np->cork.tclass, fl6->flowlabel); |
1da177e4 LT |
1513 | hdr->hop_limit = np->cork.hop_limit; |
1514 | hdr->nexthdr = proto; | |
4e3fd7a0 AD |
1515 | hdr->saddr = fl6->saddr; |
1516 | hdr->daddr = *final_dst; | |
1da177e4 | 1517 | |
a2c2064f | 1518 | skb->priority = sk->sk_priority; |
4a19ec58 | 1519 | skb->mark = sk->sk_mark; |
a2c2064f | 1520 | |
d8d1f30b | 1521 | skb_dst_set(skb, dst_clone(&rt->dst)); |
edf391ff | 1522 | IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len); |
14878f75 | 1523 | if (proto == IPPROTO_ICMPV6) { |
adf30907 | 1524 | struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb)); |
14878f75 | 1525 | |
5a57d4c7 | 1526 | ICMP6MSGOUT_INC_STATS_BH(net, idev, icmp6_hdr(skb)->icmp6_type); |
e41b5368 | 1527 | ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS); |
14878f75 DS |
1528 | } |
1529 | ||
ef76bc23 | 1530 | err = ip6_local_out(skb); |
1da177e4 LT |
1531 | if (err) { |
1532 | if (err > 0) | |
6ce9e7b5 | 1533 | err = net_xmit_errno(err); |
1da177e4 LT |
1534 | if (err) |
1535 | goto error; | |
1536 | } | |
1537 | ||
1538 | out: | |
bf138862 | 1539 | ip6_cork_release(inet, np); |
1da177e4 LT |
1540 | return err; |
1541 | error: | |
06254914 | 1542 | IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS); |
1da177e4 LT |
1543 | goto out; |
1544 | } | |
a495f836 | 1545 | EXPORT_SYMBOL_GPL(ip6_push_pending_frames); |
1da177e4 LT |
1546 | |
1547 | void ip6_flush_pending_frames(struct sock *sk) | |
1548 | { | |
1da177e4 LT |
1549 | struct sk_buff *skb; |
1550 | ||
1551 | while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) { | |
adf30907 ED |
1552 | if (skb_dst(skb)) |
1553 | IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)), | |
e1f52208 | 1554 | IPSTATS_MIB_OUTDISCARDS); |
1da177e4 LT |
1555 | kfree_skb(skb); |
1556 | } | |
1557 | ||
bf138862 | 1558 | ip6_cork_release(inet_sk(sk), inet6_sk(sk)); |
1da177e4 | 1559 | } |
a495f836 | 1560 | EXPORT_SYMBOL_GPL(ip6_flush_pending_frames); |