2 * IPv6 output functions
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: ip6_output.c,v 1.20 1999/06/09 10:11:12 davem Exp $
10 * Based on linux/net/ipv4/ip_output.c
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
18 * A.N.Kuznetsov : airthmetics in fragmentation.
19 * extension headers are implemented.
20 * route changes now work.
21 * ip6_forward does not confuse sniffers.
24 * H. von Brand : Added missing #include <linux/string.h>
27 #include <linux/errno.h>
28 #include <linux/types.h>
29 #include <linux/string.h>
30 #include <linux/socket.h>
31 #include <linux/net.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_arp.h>
34 #include <linux/in6.h>
35 #include <linux/route.h>
41 #include <net/ndisc.h>
42 #include <net/protocol.h>
43 #include <net/ip6_route.h>
44 #include <net/addrconf.h>
45 #include <net/rawv6.h>
48 static u32 ipv6_fragmentation_id
= 1;
50 int ip6_output(struct sk_buff
*skb
)
52 struct dst_entry
*dst
= skb
->dst
;
53 struct device
*dev
= dst
->dev
;
54 struct hh_cache
*hh
= dst
->hh
;
56 skb
->protocol
= __constant_htons(ETH_P_IPV6
);
59 if (ipv6_addr_is_multicast(&skb
->nh
.ipv6h
->daddr
)) {
60 if (!(dev
->flags
&IFF_LOOPBACK
) &&
61 (skb
->sk
== NULL
|| skb
->sk
->net_pinfo
.af_inet6
.mc_loop
) &&
62 ipv6_chk_mcast_addr(dev
, &skb
->nh
.ipv6h
->daddr
)) {
63 /* Do not check for IFF_ALLMULTI; multicast routing
64 is not supported in any case.
66 dev_loopback_xmit(skb
);
68 if (skb
->nh
.ipv6h
->hop_limit
== 0) {
74 ipv6_statistics
.Ip6OutMcastPkts
++;
78 read_lock_bh(&hh
->hh_lock
);
79 memcpy(skb
->data
- 16, hh
->hh_data
, 16);
80 read_unlock_bh(&hh
->hh_lock
);
81 skb_push(skb
, hh
->hh_len
);
82 return hh
->hh_output(skb
);
83 } else if (dst
->neighbour
)
84 return dst
->neighbour
->output(skb
);
91 * xmit an sk_buff (used by TCP)
94 int ip6_xmit(struct sock
*sk
, struct sk_buff
*skb
, struct flowi
*fl
,
95 struct ipv6_txoptions
*opt
)
97 struct ipv6_pinfo
* np
= sk
? &sk
->net_pinfo
.af_inet6
: NULL
;
98 struct in6_addr
*first_hop
= fl
->nl_u
.ip6_u
.daddr
;
99 struct dst_entry
*dst
= skb
->dst
;
101 u8 proto
= fl
->proto
;
102 int seg_len
= skb
->len
;
108 /* First: exthdrs may take lots of space (~8K for now)
109 MAX_HEADER is not enough.
111 head_room
= opt
->opt_nflen
+ opt
->opt_flen
;
112 seg_len
+= head_room
;
113 head_room
+= sizeof(struct ipv6hdr
) + ((dst
->dev
->hard_header_len
+ 15)&~15);
115 if (skb_headroom(skb
) < head_room
) {
116 struct sk_buff
*skb2
= skb_realloc_headroom(skb
, head_room
);
122 skb_set_owner_w(skb
, sk
);
125 ipv6_push_frag_opts(skb
, opt
, &proto
);
127 ipv6_push_nfrag_opts(skb
, opt
, &proto
, &first_hop
);
130 hdr
= skb
->nh
.ipv6h
= (struct ipv6hdr
*)skb_push(skb
, sizeof(struct ipv6hdr
));
133 * Fill in the IPv6 header
136 *(u32
*)hdr
= __constant_htonl(0x60000000) | fl
->fl6_flowlabel
;
139 hlimit
= np
->hop_limit
;
141 hlimit
= ((struct rt6_info
*)dst
)->rt6i_hoplimit
;
143 hdr
->payload_len
= htons(seg_len
);
144 hdr
->nexthdr
= proto
;
145 hdr
->hop_limit
= hlimit
;
147 ipv6_addr_copy(&hdr
->saddr
, fl
->nl_u
.ip6_u
.saddr
);
148 ipv6_addr_copy(&hdr
->daddr
, first_hop
);
150 if (skb
->len
<= dst
->pmtu
) {
151 ipv6_statistics
.Ip6OutRequests
++;
156 printk(KERN_DEBUG
"IPv6: sending pkt_too_big to self\n");
158 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, dst
->pmtu
, skb
->dev
);
165 * To avoid extra problems ND packets are send through this
166 * routine. It's code duplication but I really want to avoid
167 * extra checks since ipv6_build_header is used by TCP (which
168 * is for us performace critical)
171 int ip6_nd_hdr(struct sock
*sk
, struct sk_buff
*skb
, struct device
*dev
,
172 struct in6_addr
*saddr
, struct in6_addr
*daddr
,
175 struct ipv6_pinfo
*np
= &sk
->net_pinfo
.af_inet6
;
179 skb
->protocol
= __constant_htons(ETH_P_IPV6
);
182 totlen
= len
+ sizeof(struct ipv6hdr
);
184 hdr
= (struct ipv6hdr
*) skb_put(skb
, sizeof(struct ipv6hdr
));
187 *(u32
*)hdr
= htonl(0x60000000);
189 hdr
->payload_len
= htons(len
);
190 hdr
->nexthdr
= proto
;
191 hdr
->hop_limit
= np
->hop_limit
;
193 ipv6_addr_copy(&hdr
->saddr
, saddr
);
194 ipv6_addr_copy(&hdr
->daddr
, daddr
);
199 static struct ipv6hdr
* ip6_bld_1(struct sock
*sk
, struct sk_buff
*skb
, struct flowi
*fl
,
200 int hlimit
, unsigned pktlength
)
204 skb
->nh
.raw
= skb_put(skb
, sizeof(struct ipv6hdr
));
207 *(u32
*)hdr
= fl
->fl6_flowlabel
| htonl(0x60000000);
209 hdr
->payload_len
= htons(pktlength
- sizeof(struct ipv6hdr
));
210 hdr
->hop_limit
= hlimit
;
211 hdr
->nexthdr
= fl
->proto
;
213 ipv6_addr_copy(&hdr
->saddr
, fl
->nl_u
.ip6_u
.saddr
);
214 ipv6_addr_copy(&hdr
->daddr
, fl
->nl_u
.ip6_u
.daddr
);
218 static __inline__ u8
* ipv6_build_fraghdr(struct sk_buff
*skb
, u8
* prev_hdr
, unsigned offset
)
220 struct frag_hdr
*fhdr
;
222 fhdr
= (struct frag_hdr
*) skb_put(skb
, sizeof(struct frag_hdr
));
224 fhdr
->nexthdr
= *prev_hdr
;
225 *prev_hdr
= NEXTHDR_FRAGMENT
;
226 prev_hdr
= &fhdr
->nexthdr
;
229 fhdr
->frag_off
= htons(offset
);
230 fhdr
->identification
= ipv6_fragmentation_id
++;
231 return &fhdr
->nexthdr
;
234 static int ip6_frag_xmit(struct sock
*sk
, inet_getfrag_t getfrag
,
235 const void *data
, struct dst_entry
*dst
,
236 struct flowi
*fl
, struct ipv6_txoptions
*opt
,
237 struct in6_addr
*final_dst
,
238 int hlimit
, int flags
, unsigned length
, int mtu
)
241 struct sk_buff
*last_skb
;
255 * Extension header order:
256 * Hop-by-hop -> Dest0 -> Routing -> Fragment -> Auth -> Dest1 -> rest (...)
258 * We must build the non-fragmented part that
259 * will be in every packet... this also means
260 * that other extension headers (Dest, Auth, etc)
261 * must be considered in the data to be fragmented
264 unfrag_len
= sizeof(struct ipv6hdr
) + sizeof(struct frag_hdr
);
268 unfrag_len
+= opt
->opt_nflen
;
269 last_len
+= opt
->opt_flen
;
273 * Length of fragmented part on every packet but
274 * the last must be an:
275 * "integer multiple of 8 octects".
278 frag_len
= (mtu
- unfrag_len
) & ~0x7;
280 /* Unfragmentable part exceeds mtu. */
282 ipv6_local_error(sk
, EMSGSIZE
, fl
, mtu
);
286 nfrags
= last_len
/ frag_len
;
289 * We must send from end to start because of
290 * UDP/ICMP checksums. We do a funny trick:
291 * fill the last skb first with the fixed
292 * header (and its data) and then use it
293 * to create the following segments and send it
294 * in the end. If the peer is checking the M_flag
295 * to trigger the reassembly code then this
296 * might be a good idea.
299 frag_off
= nfrags
* frag_len
;
300 last_len
-= frag_off
;
304 frag_off
-= frag_len
;
309 /* And it is implementation problem: for now we assume, that
310 all the exthdrs will fit to the first fragment.
313 if (frag_len
< opt
->opt_flen
) {
314 ipv6_local_error(sk
, EMSGSIZE
, fl
, mtu
);
317 data_off
= frag_off
- opt
->opt_flen
;
320 last_skb
= sock_alloc_send_skb(sk
, unfrag_len
+ frag_len
+
321 dst
->dev
->hard_header_len
+ 15,
322 0, flags
& MSG_DONTWAIT
, &err
);
324 if (last_skb
== NULL
)
327 last_skb
->dst
= dst_clone(dst
);
329 skb_reserve(last_skb
, (dst
->dev
->hard_header_len
+ 15) & ~15);
331 hdr
= ip6_bld_1(sk
, last_skb
, fl
, hlimit
, frag_len
+unfrag_len
);
332 prev_hdr
= &hdr
->nexthdr
;
334 if (opt
&& opt
->opt_nflen
)
335 prev_hdr
= ipv6_build_nfrag_opts(last_skb
, prev_hdr
, opt
, final_dst
, 0);
337 prev_hdr
= ipv6_build_fraghdr(last_skb
, prev_hdr
, frag_off
);
338 fhdr_dist
= prev_hdr
- last_skb
->data
;
340 err
= getfrag(data
, &hdr
->saddr
, last_skb
->tail
, data_off
, last_len
);
346 struct frag_hdr
*fhdr2
;
348 skb
= skb_copy(last_skb
, sk
->allocation
);
351 ipv6_statistics
.Ip6FragFails
++;
356 frag_off
-= frag_len
;
357 data_off
-= frag_len
;
359 fhdr2
= (struct frag_hdr
*) (skb
->data
+ fhdr_dist
);
362 fhdr2
->frag_off
= htons(frag_off
| 1);
364 /* Write fragmentable exthdrs to the first chunk */
365 if (nfrags
== 0 && opt
&& opt
->opt_flen
) {
366 ipv6_build_frag_opts(skb
, &fhdr2
->nexthdr
, opt
);
367 frag_len
-= opt
->opt_flen
;
371 err
= getfrag(data
, &hdr
->saddr
,skb_put(skb
, frag_len
),
379 ipv6_statistics
.Ip6FragCreates
++;
380 ipv6_statistics
.Ip6OutRequests
++;
386 ipv6_statistics
.Ip6FragFails
++;
391 hdr
->payload_len
= htons(unfrag_len
+ last_len
- sizeof(struct ipv6hdr
));
394 * update last_skb to reflect the getfrag we did
398 skb_put(last_skb
, last_len
);
400 ipv6_statistics
.Ip6FragCreates
++;
401 ipv6_statistics
.Ip6FragOKs
++;
402 ipv6_statistics
.Ip6OutRequests
++;
403 dst
->output(last_skb
);
408 int ip6_build_xmit(struct sock
*sk
, inet_getfrag_t getfrag
, const void *data
,
409 struct flowi
*fl
, unsigned length
,
410 struct ipv6_txoptions
*opt
, int hlimit
, int flags
)
412 struct ipv6_pinfo
*np
= &sk
->net_pinfo
.af_inet6
;
413 struct in6_addr
*final_dst
= NULL
;
414 struct dst_entry
*dst
;
416 unsigned int pktlength
, jumbolen
, mtu
;
417 struct in6_addr saddr
;
419 if (opt
&& opt
->srcrt
) {
420 struct rt0_hdr
*rt0
= (struct rt0_hdr
*) opt
->srcrt
;
421 final_dst
= fl
->fl6_dst
;
422 fl
->fl6_dst
= rt0
->addr
;
425 if (!fl
->oif
&& ipv6_addr_is_multicast(fl
->nl_u
.ip6_u
.daddr
))
426 fl
->oif
= np
->mcast_oif
;
430 dst
= dst_check(&sk
->dst_cache
, np
->dst_cookie
);
432 struct rt6_info
*rt
= (struct rt6_info
*)dst_clone(dst
);
434 /* Yes, checking route validity in not connected
435 case is not very simple. Take into account,
436 that we do not support routing by source, TOS,
437 and MSG_DONTROUTE --ANK (980726)
439 1. If route was host route, check that
440 cached destination is current.
441 If it is network route, we still may
442 check its validity using saved pointer
443 to the last used address: daddr_cache.
444 We do not want to save whole address now,
445 (because main consumer of this service
446 is tcp, which has not this problem),
447 so that the last trick works only on connected
449 2. oif also should be the same.
451 if (((rt
->rt6i_dst
.plen
!= 128 ||
452 ipv6_addr_cmp(fl
->fl6_dst
, &rt
->rt6i_dst
.addr
))
453 && (np
->daddr_cache
== NULL
||
454 ipv6_addr_cmp(fl
->fl6_dst
, np
->daddr_cache
)))
455 || (fl
->oif
&& fl
->oif
!= dst
->dev
->ifindex
)) {
463 dst
= ip6_route_output(sk
, fl
);
466 ipv6_statistics
.Ip6OutNoRoutes
++;
471 if (fl
->fl6_src
== NULL
) {
472 err
= ipv6_get_saddr(dst
, fl
->fl6_dst
, &saddr
);
476 printk(KERN_DEBUG
"ip6_build_xmit: "
477 "no availiable source address\n");
481 fl
->fl6_src
= &saddr
;
486 if (ipv6_addr_is_multicast(fl
->fl6_dst
))
487 hlimit
= np
->mcast_hops
;
489 hlimit
= np
->hop_limit
;
491 hlimit
= ((struct rt6_info
*)dst
)->rt6i_hoplimit
;
496 if (!sk
->ip_hdrincl
) {
497 pktlength
+= sizeof(struct ipv6hdr
);
499 pktlength
+= opt
->opt_flen
+ opt
->opt_nflen
;
501 if (pktlength
> 0xFFFF + sizeof(struct ipv6hdr
)) {
503 It is assumed, that in the case of sk->ip_hdrincl
504 jumbo option is supplied by user.
507 jumbolen
= pktlength
- sizeof(struct ipv6hdr
);
512 if (np
->frag_size
< mtu
) {
515 else if (np
->pmtudisc
== IPV6_PMTUDISC_DONT
)
519 /* Critical arithmetic overflow check.
520 FIXME: may gcc optimize it out? --ANK (980726)
522 if (pktlength
< length
) {
523 ipv6_local_error(sk
, EMSGSIZE
, fl
, mtu
);
528 if (pktlength
<= mtu
) {
531 struct device
*dev
= dst
->dev
;
533 skb
= sock_alloc_send_skb(sk
, pktlength
+ 15 +
534 dev
->hard_header_len
, 0,
535 flags
& MSG_DONTWAIT
, &err
);
538 ipv6_statistics
.Ip6OutDiscards
++;
542 skb
->dst
= dst_clone(dst
);
544 skb_reserve(skb
, (dev
->hard_header_len
+ 15) & ~15);
546 hdr
= (struct ipv6hdr
*) skb
->tail
;
549 if (!sk
->ip_hdrincl
) {
550 ip6_bld_1(sk
, skb
, fl
, hlimit
,
551 jumbolen
? sizeof(struct ipv6hdr
) : pktlength
);
553 if (opt
|| jumbolen
) {
554 u8
*prev_hdr
= &hdr
->nexthdr
;
555 prev_hdr
= ipv6_build_nfrag_opts(skb
, prev_hdr
, opt
, final_dst
, jumbolen
);
556 if (opt
&& opt
->opt_flen
)
557 ipv6_build_frag_opts(skb
, prev_hdr
, opt
);
561 skb_put(skb
, length
);
562 err
= getfrag(data
, &hdr
->saddr
,
563 ((char *) hdr
) + (pktlength
- length
),
567 ipv6_statistics
.Ip6OutRequests
++;
574 if (sk
->ip_hdrincl
|| jumbolen
||
575 np
->pmtudisc
== IPV6_PMTUDISC_DO
) {
576 ipv6_local_error(sk
, EMSGSIZE
, fl
, mtu
);
581 err
= ip6_frag_xmit(sk
, getfrag
, data
, dst
, fl
, opt
, final_dst
, hlimit
,
589 ip6_dst_store(sk
, dst
, fl
->nl_u
.ip6_u
.daddr
== &np
->daddr
? &np
->daddr
: NULL
);
593 int ip6_call_ra_chain(struct sk_buff
*skb
, int sel
)
595 struct ip6_ra_chain
*ra
;
596 struct sock
*last
= NULL
;
598 for (ra
= ip6_ra_chain
; ra
; ra
= ra
->next
) {
599 struct sock
*sk
= ra
->sk
;
600 if (sk
&& ra
->sel
== sel
) {
602 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
604 rawv6_rcv(last
, skb2
, skb2
->len
);
611 rawv6_rcv(last
, skb
, skb
->len
);
617 int ip6_forward(struct sk_buff
*skb
)
619 struct dst_entry
*dst
= skb
->dst
;
620 struct ipv6hdr
*hdr
= skb
->nh
.ipv6h
;
621 struct inet6_skb_parm
*opt
=(struct inet6_skb_parm
*)skb
->cb
;
623 if (ipv6_devconf
.forwarding
== 0 && opt
->srcrt
== 0)
627 * We DO NOT make any processing on
628 * RA packets, pushing them to user level AS IS
629 * without ane WARRANTY that application will be able
630 * to interpret them. The reason is that we
631 * cannot make anything clever here.
633 * We are not end-node, so that if packet contains
634 * AH/ESP, we cannot make anything.
635 * Defragmentation also would be mistake, RA packets
636 * cannot be fragmented, because there is no warranty
637 * that different fragments will go along one path. --ANK
640 u8
*ptr
= skb
->nh
.raw
+ opt
->ra
;
641 if (ip6_call_ra_chain(skb
, (ptr
[2]<<8) + ptr
[3]))
646 * check and decrement ttl
648 if (hdr
->hop_limit
<= 1) {
649 /* Force OUTPUT device used as source address */
651 icmpv6_send(skb
, ICMPV6_TIME_EXCEED
, ICMPV6_EXC_HOPLIMIT
,
658 /* IPv6 specs say nothing about it, but it is clear that we cannot
659 send redirects to source routed frames.
661 if (skb
->dev
== dst
->dev
&& dst
->neighbour
&& opt
->srcrt
== 0) {
662 struct in6_addr
*target
= NULL
;
664 struct neighbour
*n
= dst
->neighbour
;
667 * incoming and outgoing devices are the same
671 rt
= (struct rt6_info
*) dst
;
672 if ((rt
->rt6i_flags
& RTF_GATEWAY
))
673 target
= (struct in6_addr
*)&n
->primary_key
;
675 target
= &hdr
->daddr
;
677 /* Limit redirects both by destination (here)
678 and by source (inside ndisc_send_redirect)
680 if (xrlim_allow(dst
, 1*HZ
))
681 ndisc_send_redirect(skb
, n
, target
);
682 } else if (ipv6_addr_type(&hdr
->saddr
)&(IPV6_ADDR_MULTICAST
|IPV6_ADDR_LOOPBACK
683 |IPV6_ADDR_LINKLOCAL
)) {
684 /* This check is security critical. */
688 if (skb
->len
> dst
->pmtu
) {
689 /* Again, force OUTPUT device used as source address */
691 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, dst
->pmtu
, skb
->dev
);
692 ipv6_statistics
.Ip6InTooBigErrors
++;
697 if ((skb
= skb_cow(skb
, dst
->dev
->hard_header_len
)) == NULL
)
702 /* Mangling hops number delayed to point after skb COW */
706 ipv6_statistics
.Ip6OutForwDatagrams
++;
707 return dst
->output(skb
);
710 ipv6_statistics
.Ip6InAddrErrors
++;