Import 2.3.48pre4
[davej-history.git] / net / ipv6 / ip6_input.c
blob7094437490d07f695408725c0bd10ae468103c81
1 /*
2 * IPv6 input
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Ian P. Morris <I.P.Morris@soton.ac.uk>
9 * $Id: ip6_input.c,v 1.15 2000/01/09 02:19:54 davem Exp $
11 * Based in linux/net/ipv4/ip_input.c
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/socket.h>
22 #include <linux/sockios.h>
23 #include <linux/sched.h>
24 #include <linux/net.h>
25 #include <linux/netdevice.h>
26 #include <linux/in6.h>
27 #include <linux/icmpv6.h>
29 #include <net/sock.h>
30 #include <net/snmp.h>
32 #include <net/ipv6.h>
33 #include <net/protocol.h>
34 #include <net/transp_v6.h>
35 #include <net/rawv6.h>
36 #include <net/ndisc.h>
37 #include <net/ip6_route.h>
38 #include <net/addrconf.h>
41 int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
43 struct ipv6hdr *hdr;
44 u32 pkt_len;
46 if (skb->pkt_type == PACKET_OTHERHOST)
47 goto drop;
49 IP6_INC_STATS_BH(Ip6InReceives);
51 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
52 goto out;
54 /* Store incoming device index. When the packet will
55 be queued, we cannot refer to skb->dev anymore.
57 ((struct inet6_skb_parm *)skb->cb)->iif = dev->ifindex;
59 hdr = skb->nh.ipv6h;
61 if (skb->len < sizeof(struct ipv6hdr) || hdr->version != 6)
62 goto err;
64 pkt_len = ntohs(hdr->payload_len);
66 /* pkt_len may be zero if Jumbo payload option is present */
67 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
68 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
69 goto truncated;
70 skb_trim(skb, pkt_len + sizeof(struct ipv6hdr));
73 if (hdr->nexthdr == NEXTHDR_HOP) {
74 skb->h.raw = (u8*)(hdr+1);
75 if (!ipv6_parse_hopopts(skb, &hdr->nexthdr)) {
76 IP6_INC_STATS_BH(Ip6InHdrErrors);
77 return 0;
81 if (skb->dst == NULL)
82 ip6_route_input(skb);
84 return skb->dst->input(skb);
86 truncated:
87 IP6_INC_STATS_BH(Ip6InTruncatedPkts);
88 err:
89 IP6_INC_STATS_BH(Ip6InHdrErrors);
90 drop:
91 kfree_skb(skb);
92 out:
93 return 0;
97 * Deliver the packet to the host
100 int ip6_input(struct sk_buff *skb)
102 struct ipv6hdr *hdr = skb->nh.ipv6h;
103 struct inet6_protocol *ipprot;
104 struct sock *raw_sk;
105 __u8 *nhptr;
106 int nexthdr;
107 int found = 0;
108 u8 hash;
109 int len;
111 skb->h.raw = skb->nh.raw + sizeof(struct ipv6hdr);
114 * Parse extension headers
117 nexthdr = hdr->nexthdr;
118 nhptr = &hdr->nexthdr;
120 /* Skip hop-by-hop options, they are already parsed. */
121 if (nexthdr == NEXTHDR_HOP) {
122 nhptr = (u8*)(hdr+1);
123 nexthdr = *nhptr;
124 skb->h.raw += (nhptr[1]+1)<<3;
127 /* This check is sort of optimization.
128 It would be stupid to detect for optional headers,
129 which are missing with probability of 200%
131 if (nexthdr != IPPROTO_TCP && nexthdr != IPPROTO_UDP) {
132 nhptr = ipv6_parse_exthdrs(&skb, nhptr);
133 if (nhptr == NULL)
134 return 0;
135 nexthdr = *nhptr;
136 hdr = skb->nh.ipv6h;
138 len = skb->tail - skb->h.raw;
140 if (skb->rx_dev) {
141 dev_put(skb->rx_dev);
142 skb->rx_dev = NULL;
145 raw_sk = raw_v6_htable[nexthdr&(MAX_INET_PROTOS-1)];
146 if (raw_sk)
147 raw_sk = ipv6_raw_deliver(skb, nexthdr, len);
149 hash = nexthdr & (MAX_INET_PROTOS - 1);
150 read_lock(&inet6_protocol_lock);
151 for (ipprot = (struct inet6_protocol *) inet6_protos[hash];
152 ipprot != NULL;
153 ipprot = (struct inet6_protocol *) ipprot->next) {
154 struct sk_buff *buff = skb;
156 if (ipprot->protocol != nexthdr)
157 continue;
159 if (ipprot->copy || raw_sk)
160 buff = skb_clone(skb, GFP_ATOMIC);
162 if (buff)
163 ipprot->handler(buff, len);
164 found = 1;
166 read_unlock(&inet6_protocol_lock);
168 if (raw_sk) {
169 rawv6_rcv(raw_sk, skb, len);
170 sock_put(raw_sk);
171 found = 1;
175 * not found: send ICMP parameter problem back
177 if (!found) {
178 IP6_INC_STATS_BH(Ip6InUnknownProtos);
179 icmpv6_param_prob(skb, ICMPV6_UNK_NEXTHDR, nhptr);
182 return 0;
185 int ip6_mc_input(struct sk_buff *skb)
187 struct ipv6hdr *hdr;
188 int deliver = 0;
189 int discard = 1;
191 IP6_INC_STATS_BH(Ip6InMcastPkts);
193 hdr = skb->nh.ipv6h;
194 if (ipv6_chk_mcast_addr(skb->dev, &hdr->daddr))
195 deliver = 1;
198 * IPv6 multicast router mode isnt currently supported.
200 #if 0
201 if (ipv6_config.multicast_route) {
202 int addr_type;
204 addr_type = ipv6_addr_type(&hdr->daddr);
206 if (!(addr_type & (IPV6_ADDR_LOOPBACK | IPV6_ADDR_LINKLOCAL))) {
207 struct sk_buff *skb2;
208 struct dst_entry *dst;
210 dst = skb->dst;
212 if (deliver) {
213 skb2 = skb_clone(skb, GFP_ATOMIC);
214 } else {
215 discard = 0;
216 skb2 = skb;
219 dst->output(skb2);
222 #endif
224 if (deliver) {
225 discard = 0;
226 ip6_input(skb);
229 if (discard)
230 kfree_skb(skb);
232 return 0;