Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Extension Header handling for IPv6 |
| 3 | * Linux INET6 implementation |
| 4 | * |
| 5 | * Authors: |
| 6 | * Pedro Roque <roque@di.fc.ul.pt> |
| 7 | * Andi Kleen <ak@muc.de> |
| 8 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
| 9 | * |
| 10 | * $Id: exthdrs.c,v 1.13 2001/06/19 15:58:56 davem Exp $ |
| 11 | * |
| 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. |
| 16 | */ |
| 17 | |
| 18 | /* Changes: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 19 | * yoshfuji : ensure not to overrun while parsing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | * tlv options. |
| 21 | * Mitsuru KANDA @USAGI and: Remove ipv6_parse_exthdrs(). |
| 22 | * YOSHIFUJI Hideaki @USAGI Register inbound extension header |
| 23 | * handlers as inet6_protocol{}. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/socket.h> |
| 29 | #include <linux/sockios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/net.h> |
| 31 | #include <linux/netdevice.h> |
| 32 | #include <linux/in6.h> |
| 33 | #include <linux/icmpv6.h> |
| 34 | |
| 35 | #include <net/sock.h> |
| 36 | #include <net/snmp.h> |
| 37 | |
| 38 | #include <net/ipv6.h> |
| 39 | #include <net/protocol.h> |
| 40 | #include <net/transp_v6.h> |
| 41 | #include <net/rawv6.h> |
| 42 | #include <net/ndisc.h> |
| 43 | #include <net/ip6_route.h> |
| 44 | #include <net/addrconf.h> |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 45 | #ifdef CONFIG_IPV6_MIP6 |
| 46 | #include <net/xfrm.h> |
| 47 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | #include <asm/uaccess.h> |
| 50 | |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 51 | int ipv6_find_tlv(struct sk_buff *skb, int offset, int type) |
| 52 | { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 53 | const unsigned char *nh = skb_network_header(skb); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 54 | int packet_len = skb->tail - skb->network_header; |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 55 | struct ipv6_opt_hdr *hdr; |
| 56 | int len; |
| 57 | |
| 58 | if (offset + 2 > packet_len) |
| 59 | goto bad; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 60 | hdr = (struct ipv6_opt_hdr *)(nh + offset); |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 61 | len = ((hdr->hdrlen + 1) << 3); |
| 62 | |
| 63 | if (offset + len > packet_len) |
| 64 | goto bad; |
| 65 | |
| 66 | offset += 2; |
| 67 | len -= 2; |
| 68 | |
| 69 | while (len > 0) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 70 | int opttype = nh[offset]; |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 71 | int optlen; |
| 72 | |
| 73 | if (opttype == type) |
| 74 | return offset; |
| 75 | |
| 76 | switch (opttype) { |
| 77 | case IPV6_TLV_PAD0: |
| 78 | optlen = 1; |
| 79 | break; |
| 80 | default: |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 81 | optlen = nh[offset + 1] + 2; |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 82 | if (optlen > len) |
| 83 | goto bad; |
| 84 | break; |
| 85 | } |
| 86 | offset += optlen; |
| 87 | len -= optlen; |
| 88 | } |
| 89 | /* not_found */ |
Masahide NAKAMURA | c61a404 | 2006-08-23 19:18:35 -0700 | [diff] [blame] | 90 | bad: |
| 91 | return -1; |
| 92 | } |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* |
| 95 | * Parsing tlv encoded headers. |
| 96 | * |
| 97 | * Parsing function "func" returns 1, if parsing succeed |
| 98 | * and 0, if it failed. |
| 99 | * It MUST NOT touch skb->h. |
| 100 | */ |
| 101 | |
| 102 | struct tlvtype_proc { |
| 103 | int type; |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 104 | int (*func)(struct sk_buff **skbp, int offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | /********************* |
| 108 | Generic functions |
| 109 | *********************/ |
| 110 | |
| 111 | /* An unknown option is detected, decide what to do */ |
| 112 | |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 113 | static int ip6_tlvopt_unknown(struct sk_buff **skbp, int optoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 115 | struct sk_buff *skb = *skbp; |
| 116 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 117 | switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | case 0: /* ignore */ |
| 119 | return 1; |
| 120 | |
| 121 | case 1: /* drop packet */ |
| 122 | break; |
| 123 | |
| 124 | case 3: /* Send ICMP if not a multicast address and drop packet */ |
| 125 | /* Actually, it is redundant check. icmp_send |
| 126 | will recheck in any case. |
| 127 | */ |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 128 | if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | break; |
| 130 | case 2: /* send ICMP PARM PROB regardless and drop packet */ |
| 131 | icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff); |
| 132 | return 0; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 133 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | kfree_skb(skb); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | /* Parse tlv encoded option header (hop-by-hop or destination) */ |
| 140 | |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 141 | static int ip6_parse_tlv(struct tlvtype_proc *procs, struct sk_buff **skbp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 143 | struct sk_buff *skb = *skbp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | struct tlvtype_proc *curr; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 145 | const unsigned char *nh = skb_network_header(skb); |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 146 | int off = skb_network_header_len(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 147 | int len = (skb_transport_header(skb)[1] + 1) << 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 149 | if (skb_transport_offset(skb) + len > skb_headlen(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | goto bad; |
| 151 | |
| 152 | off += 2; |
| 153 | len -= 2; |
| 154 | |
| 155 | while (len > 0) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 156 | int optlen = nh[off + 1] + 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 158 | switch (nh[off]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | case IPV6_TLV_PAD0: |
| 160 | optlen = 1; |
| 161 | break; |
| 162 | |
| 163 | case IPV6_TLV_PADN: |
| 164 | break; |
| 165 | |
| 166 | default: /* Other TLV code so scan list */ |
| 167 | if (optlen > len) |
| 168 | goto bad; |
| 169 | for (curr=procs; curr->type >= 0; curr++) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 170 | if (curr->type == nh[off]) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 171 | /* type specific length/alignment |
| 172 | checks will be performed in the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | func(). */ |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 174 | if (curr->func(skbp, off) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return 0; |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | if (curr->type < 0) { |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 180 | if (ip6_tlvopt_unknown(skbp, off) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | return 0; |
| 182 | } |
| 183 | break; |
| 184 | } |
| 185 | off += optlen; |
| 186 | len -= optlen; |
| 187 | } |
| 188 | if (len == 0) |
| 189 | return 1; |
| 190 | bad: |
| 191 | kfree_skb(skb); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /***************************** |
| 196 | Destination options header. |
| 197 | *****************************/ |
| 198 | |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 199 | #ifdef CONFIG_IPV6_MIP6 |
| 200 | static int ipv6_dest_hao(struct sk_buff **skbp, int optoff) |
| 201 | { |
| 202 | struct sk_buff *skb = *skbp; |
| 203 | struct ipv6_destopt_hao *hao; |
| 204 | struct inet6_skb_parm *opt = IP6CB(skb); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 205 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 206 | struct in6_addr tmp_addr; |
| 207 | int ret; |
| 208 | |
| 209 | if (opt->dsthao) { |
| 210 | LIMIT_NETDEBUG(KERN_DEBUG "hao duplicated\n"); |
| 211 | goto discard; |
| 212 | } |
| 213 | opt->dsthao = opt->dst1; |
| 214 | opt->dst1 = 0; |
| 215 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 216 | hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 217 | |
| 218 | if (hao->length != 16) { |
| 219 | LIMIT_NETDEBUG( |
| 220 | KERN_DEBUG "hao invalid option length = %d\n", hao->length); |
| 221 | goto discard; |
| 222 | } |
| 223 | |
| 224 | if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) { |
| 225 | LIMIT_NETDEBUG( |
| 226 | KERN_DEBUG "hao is not an unicast addr: " NIP6_FMT "\n", NIP6(hao->addr)); |
| 227 | goto discard; |
| 228 | } |
| 229 | |
| 230 | ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr, |
| 231 | (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS); |
| 232 | if (unlikely(ret < 0)) |
| 233 | goto discard; |
| 234 | |
| 235 | if (skb_cloned(skb)) { |
| 236 | struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC); |
Masahide NAKAMURA | dc435e6 | 2006-08-31 15:18:49 -0700 | [diff] [blame] | 237 | struct inet6_skb_parm *opt2; |
| 238 | |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 239 | if (skb2 == NULL) |
| 240 | goto discard; |
| 241 | |
Masahide NAKAMURA | dc435e6 | 2006-08-31 15:18:49 -0700 | [diff] [blame] | 242 | opt2 = IP6CB(skb2); |
| 243 | memcpy(opt2, opt, sizeof(*opt2)); |
| 244 | |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 245 | kfree_skb(skb); |
| 246 | |
| 247 | /* update all variable using below by copied skbuff */ |
| 248 | *skbp = skb = skb2; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 249 | hao = (struct ipv6_destopt_hao *)(skb_network_header(skb2) + |
| 250 | optoff); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 251 | ipv6h = ipv6_hdr(skb2); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 255 | skb->ip_summed = CHECKSUM_NONE; |
| 256 | |
| 257 | ipv6_addr_copy(&tmp_addr, &ipv6h->saddr); |
| 258 | ipv6_addr_copy(&ipv6h->saddr, &hao->addr); |
| 259 | ipv6_addr_copy(&hao->addr, &tmp_addr); |
| 260 | |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 261 | if (skb->tstamp.tv64 == 0) |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 262 | __net_timestamp(skb); |
| 263 | |
| 264 | return 1; |
| 265 | |
| 266 | discard: |
| 267 | kfree_skb(skb); |
| 268 | return 0; |
| 269 | } |
| 270 | #endif |
| 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | static struct tlvtype_proc tlvprocdestopt_lst[] = { |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 273 | #ifdef CONFIG_IPV6_MIP6 |
| 274 | { |
| 275 | .type = IPV6_TLV_HAO, |
| 276 | .func = ipv6_dest_hao, |
| 277 | }, |
| 278 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | {-1, NULL} |
| 280 | }; |
| 281 | |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 282 | static int ipv6_destopt_rcv(struct sk_buff **skbp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
| 284 | struct sk_buff *skb = *skbp; |
| 285 | struct inet6_skb_parm *opt = IP6CB(skb); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 286 | #ifdef CONFIG_IPV6_MIP6 |
| 287 | __u16 dstbuf; |
| 288 | #endif |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 289 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 291 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) || |
| 292 | !pskb_may_pull(skb, (skb_transport_offset(skb) + |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 293 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 294 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 295 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | kfree_skb(skb); |
| 297 | return -1; |
| 298 | } |
| 299 | |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 300 | opt->lastopt = opt->dst1 = skb_network_header_len(skb); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 301 | #ifdef CONFIG_IPV6_MIP6 |
| 302 | dstbuf = opt->dst1; |
| 303 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 305 | dst = dst_clone(skb->dst); |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 306 | if (ip6_parse_tlv(tlvprocdestopt_lst, skbp)) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 307 | dst_release(dst); |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 308 | skb = *skbp; |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 309 | skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3; |
Masahide NAKAMURA | dc435e6 | 2006-08-31 15:18:49 -0700 | [diff] [blame] | 310 | opt = IP6CB(skb); |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 311 | #ifdef CONFIG_IPV6_MIP6 |
| 312 | opt->nhoff = dstbuf; |
| 313 | #else |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 314 | opt->nhoff = opt->dst1; |
Masahide NAKAMURA | a831f5b | 2006-08-23 19:24:48 -0700 | [diff] [blame] | 315 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | return 1; |
| 317 | } |
| 318 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 319 | IP6_INC_STATS_BH(ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS); |
| 320 | dst_release(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | static struct inet6_protocol destopt_protocol = { |
| 325 | .handler = ipv6_destopt_rcv, |
Herbert Xu | adcfc7d | 2006-06-30 13:36:15 -0700 | [diff] [blame] | 326 | .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | void __init ipv6_destopt_init(void) |
| 330 | { |
| 331 | if (inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS) < 0) |
| 332 | printk(KERN_ERR "ipv6_destopt_init: Could not register protocol\n"); |
| 333 | } |
| 334 | |
| 335 | /******************************** |
| 336 | NONE header. No data in packet. |
| 337 | ********************************/ |
| 338 | |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 339 | static int ipv6_nodata_rcv(struct sk_buff **skbp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | { |
| 341 | struct sk_buff *skb = *skbp; |
| 342 | |
| 343 | kfree_skb(skb); |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static struct inet6_protocol nodata_protocol = { |
| 348 | .handler = ipv6_nodata_rcv, |
| 349 | .flags = INET6_PROTO_NOPOLICY, |
| 350 | }; |
| 351 | |
| 352 | void __init ipv6_nodata_init(void) |
| 353 | { |
| 354 | if (inet6_add_protocol(&nodata_protocol, IPPROTO_NONE) < 0) |
| 355 | printk(KERN_ERR "ipv6_nodata_init: Could not register protocol\n"); |
| 356 | } |
| 357 | |
| 358 | /******************************** |
| 359 | Routing header. |
| 360 | ********************************/ |
| 361 | |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 362 | static int ipv6_rthdr_rcv(struct sk_buff **skbp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
| 364 | struct sk_buff *skb = *skbp; |
| 365 | struct inet6_skb_parm *opt = IP6CB(skb); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 366 | struct in6_addr *addr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | struct in6_addr daddr; |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 368 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | int n, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | struct ipv6_rt_hdr *hdr; |
| 371 | struct rt0_hdr *rthdr; |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 372 | int accept_source_route = ipv6_devconf.accept_source_route; |
| 373 | |
| 374 | if (accept_source_route < 0 || |
| 375 | ((idev = in6_dev_get(skb->dev)) == NULL)) { |
| 376 | kfree_skb(skb); |
| 377 | return -1; |
| 378 | } |
| 379 | if (idev->cnf.accept_source_route < 0) { |
| 380 | in6_dev_put(idev); |
| 381 | kfree_skb(skb); |
| 382 | return -1; |
| 383 | } |
| 384 | |
| 385 | if (accept_source_route > idev->cnf.accept_source_route) |
| 386 | accept_source_route = idev->cnf.accept_source_route; |
| 387 | |
| 388 | in6_dev_put(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 390 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) || |
| 391 | !pskb_may_pull(skb, (skb_transport_offset(skb) + |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 392 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 393 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 394 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | kfree_skb(skb); |
| 396 | return -1; |
| 397 | } |
| 398 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 399 | hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 401 | switch (hdr->type) { |
| 402 | #ifdef CONFIG_IPV6_MIP6 |
YOSHIFUJI Hideaki | ebbd90a | 2007-04-27 02:13:39 -0700 | [diff] [blame] | 403 | case IPV6_SRCRT_TYPE_2: |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 404 | break; |
| 405 | #endif |
| 406 | case IPV6_SRCRT_TYPE_0: |
YOSHIFUJI Hideaki | a23cf14 | 2007-04-25 11:13:49 +0900 | [diff] [blame] | 407 | if (accept_source_route > 0) |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 408 | break; |
| 409 | kfree_skb(skb); |
| 410 | return -1; |
| 411 | default: |
| 412 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 413 | IPSTATS_MIB_INHDRERRORS); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 414 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 415 | (&hdr->type) - skb_network_header(skb)); |
YOSHIFUJI Hideaki | 0bcbc92 | 2007-04-24 14:58:30 -0700 | [diff] [blame] | 416 | return -1; |
| 417 | } |
| 418 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 419 | if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | skb->pkt_type != PACKET_HOST) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 421 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 422 | IPSTATS_MIB_INADDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | kfree_skb(skb); |
| 424 | return -1; |
| 425 | } |
| 426 | |
| 427 | looped_back: |
| 428 | if (hdr->segments_left == 0) { |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 429 | switch (hdr->type) { |
| 430 | #ifdef CONFIG_IPV6_MIP6 |
| 431 | case IPV6_SRCRT_TYPE_2: |
| 432 | /* Silently discard type 2 header unless it was |
| 433 | * processed by own |
| 434 | */ |
| 435 | if (!addr) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 436 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 437 | IPSTATS_MIB_INADDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 438 | kfree_skb(skb); |
| 439 | return -1; |
| 440 | } |
| 441 | break; |
| 442 | #endif |
| 443 | default: |
| 444 | break; |
| 445 | } |
| 446 | |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 447 | opt->lastopt = opt->srcrt = skb_network_header_len(skb); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 448 | skb->transport_header += (hdr->hdrlen + 1) << 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | opt->dst0 = opt->dst1; |
| 450 | opt->dst1 = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 451 | opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | return 1; |
| 453 | } |
| 454 | |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 455 | switch (hdr->type) { |
| 456 | case IPV6_SRCRT_TYPE_0: |
| 457 | if (hdr->hdrlen & 0x01) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 458 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 459 | IPSTATS_MIB_INHDRERRORS); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 460 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 461 | ((&hdr->hdrlen) - |
| 462 | skb_network_header(skb))); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 463 | return -1; |
| 464 | } |
| 465 | break; |
| 466 | #ifdef CONFIG_IPV6_MIP6 |
| 467 | case IPV6_SRCRT_TYPE_2: |
| 468 | /* Silently discard invalid RTH type 2 */ |
| 469 | if (hdr->hdrlen != 2 || hdr->segments_left != 1) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 470 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 471 | IPSTATS_MIB_INHDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 472 | kfree_skb(skb); |
| 473 | return -1; |
| 474 | } |
| 475 | break; |
| 476 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 479 | /* |
| 480 | * This is the routing header forwarding algorithm from |
| 481 | * RFC 2460, page 16. |
| 482 | */ |
| 483 | |
| 484 | n = hdr->hdrlen >> 1; |
| 485 | |
| 486 | if (hdr->segments_left > n) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 487 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 488 | IPSTATS_MIB_INHDRERRORS); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 489 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 490 | ((&hdr->segments_left) - |
| 491 | skb_network_header(skb))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | return -1; |
| 493 | } |
| 494 | |
| 495 | /* We are about to mangle packet header. Be careful! |
| 496 | Do not damage packets queued somewhere. |
| 497 | */ |
| 498 | if (skb_cloned(skb)) { |
| 499 | struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | /* the copy is a forwarded packet */ |
| 501 | if (skb2 == NULL) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 502 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 503 | IPSTATS_MIB_OUTDISCARDS); |
| 504 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | return -1; |
| 506 | } |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 507 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | *skbp = skb = skb2; |
| 509 | opt = IP6CB(skb2); |
Arnaldo Carvalho de Melo | bff9b61 | 2007-03-16 17:19:57 -0300 | [diff] [blame] | 510 | hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 513 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | skb->ip_summed = CHECKSUM_NONE; |
| 515 | |
| 516 | i = n - --hdr->segments_left; |
| 517 | |
| 518 | rthdr = (struct rt0_hdr *) hdr; |
| 519 | addr = rthdr->addr; |
| 520 | addr += i - 1; |
| 521 | |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 522 | switch (hdr->type) { |
| 523 | #ifdef CONFIG_IPV6_MIP6 |
| 524 | case IPV6_SRCRT_TYPE_2: |
| 525 | if (xfrm6_input_addr(skb, (xfrm_address_t *)addr, |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 526 | (xfrm_address_t *)&ipv6_hdr(skb)->saddr, |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 527 | IPPROTO_ROUTING) < 0) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 528 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 529 | IPSTATS_MIB_INADDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 530 | kfree_skb(skb); |
| 531 | return -1; |
| 532 | } |
| 533 | if (!ipv6_chk_home_addr(addr)) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 534 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 535 | IPSTATS_MIB_INADDRERRORS); |
Masahide NAKAMURA | 65d4ed9 | 2006-08-23 19:16:22 -0700 | [diff] [blame] | 536 | kfree_skb(skb); |
| 537 | return -1; |
| 538 | } |
| 539 | break; |
| 540 | #endif |
| 541 | default: |
| 542 | break; |
| 543 | } |
| 544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | if (ipv6_addr_is_multicast(addr)) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 546 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 547 | IPSTATS_MIB_INADDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | kfree_skb(skb); |
| 549 | return -1; |
| 550 | } |
| 551 | |
| 552 | ipv6_addr_copy(&daddr, addr); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 553 | ipv6_addr_copy(addr, &ipv6_hdr(skb)->daddr); |
| 554 | ipv6_addr_copy(&ipv6_hdr(skb)->daddr, &daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | dst_release(xchg(&skb->dst, NULL)); |
| 557 | ip6_route_input(skb); |
| 558 | if (skb->dst->error) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 559 | skb_push(skb, skb->data - skb_network_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | dst_input(skb); |
| 561 | return -1; |
| 562 | } |
| 563 | |
| 564 | if (skb->dst->dev->flags&IFF_LOOPBACK) { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 565 | if (ipv6_hdr(skb)->hop_limit <= 1) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 566 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 567 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, |
| 569 | 0, skb->dev); |
| 570 | kfree_skb(skb); |
| 571 | return -1; |
| 572 | } |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 573 | ipv6_hdr(skb)->hop_limit--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | goto looped_back; |
| 575 | } |
| 576 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 577 | skb_push(skb, skb->data - skb_network_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | dst_input(skb); |
| 579 | return -1; |
| 580 | } |
| 581 | |
| 582 | static struct inet6_protocol rthdr_protocol = { |
| 583 | .handler = ipv6_rthdr_rcv, |
Herbert Xu | adcfc7d | 2006-06-30 13:36:15 -0700 | [diff] [blame] | 584 | .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_GSO_EXTHDR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | }; |
| 586 | |
| 587 | void __init ipv6_rthdr_init(void) |
| 588 | { |
| 589 | if (inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING) < 0) |
| 590 | printk(KERN_ERR "ipv6_rthdr_init: Could not register protocol\n"); |
| 591 | }; |
| 592 | |
| 593 | /* |
| 594 | This function inverts received rthdr. |
| 595 | NOTE: specs allow to make it automatically only if |
| 596 | packet authenticated. |
| 597 | |
| 598 | I will not discuss it here (though, I am really pissed off at |
| 599 | this stupid requirement making rthdr idea useless) |
| 600 | |
| 601 | Actually, it creates severe problems for us. |
| 602 | Embryonic requests has no associated sockets, |
| 603 | so that user have no control over it and |
| 604 | cannot not only to set reply options, but |
| 605 | even to know, that someone wants to connect |
| 606 | without success. :-( |
| 607 | |
| 608 | For now we need to test the engine, so that I created |
| 609 | temporary (or permanent) backdoor. |
| 610 | If listening socket set IPV6_RTHDR to 2, then we invert header. |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 611 | --ANK (980729) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | */ |
| 613 | |
| 614 | struct ipv6_txoptions * |
| 615 | ipv6_invert_rthdr(struct sock *sk, struct ipv6_rt_hdr *hdr) |
| 616 | { |
| 617 | /* Received rthdr: |
| 618 | |
| 619 | [ H1 -> H2 -> ... H_prev ] daddr=ME |
| 620 | |
| 621 | Inverted result: |
| 622 | [ H_prev -> ... -> H1 ] daddr =sender |
| 623 | |
| 624 | Note, that IP output engine will rewrite this rthdr |
| 625 | by rotating it left by one addr. |
| 626 | */ |
| 627 | |
| 628 | int n, i; |
| 629 | struct rt0_hdr *rthdr = (struct rt0_hdr*)hdr; |
| 630 | struct rt0_hdr *irthdr; |
| 631 | struct ipv6_txoptions *opt; |
| 632 | int hdrlen = ipv6_optlen(hdr); |
| 633 | |
| 634 | if (hdr->segments_left || |
| 635 | hdr->type != IPV6_SRCRT_TYPE_0 || |
| 636 | hdr->hdrlen & 0x01) |
| 637 | return NULL; |
| 638 | |
| 639 | n = hdr->hdrlen >> 1; |
| 640 | opt = sock_kmalloc(sk, sizeof(*opt) + hdrlen, GFP_ATOMIC); |
| 641 | if (opt == NULL) |
| 642 | return NULL; |
| 643 | memset(opt, 0, sizeof(*opt)); |
| 644 | opt->tot_len = sizeof(*opt) + hdrlen; |
| 645 | opt->srcrt = (void*)(opt+1); |
| 646 | opt->opt_nflen = hdrlen; |
| 647 | |
| 648 | memcpy(opt->srcrt, hdr, sizeof(*hdr)); |
| 649 | irthdr = (struct rt0_hdr*)opt->srcrt; |
Brian Haley | e6df439 | 2005-09-10 00:15:06 -0700 | [diff] [blame] | 650 | irthdr->reserved = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | opt->srcrt->segments_left = n; |
| 652 | for (i=0; i<n; i++) |
| 653 | memcpy(irthdr->addr+i, rthdr->addr+(n-1-i), 16); |
| 654 | return opt; |
| 655 | } |
| 656 | |
Arnaldo Carvalho de Melo | 3cf3dc6 | 2005-12-13 23:23:20 -0800 | [diff] [blame] | 657 | EXPORT_SYMBOL_GPL(ipv6_invert_rthdr); |
| 658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | /********************************** |
| 660 | Hop-by-hop options. |
| 661 | **********************************/ |
| 662 | |
YOSHIFUJI Hideaki | e76b2b2 | 2007-05-09 14:01:59 -0700 | [diff] [blame] | 663 | /* |
| 664 | * Note: we cannot rely on skb->dst before we assign it in ip6_route_input(). |
| 665 | */ |
| 666 | static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb) |
| 667 | { |
| 668 | return skb->dst ? ip6_dst_idev(skb->dst) : __in6_dev_get(skb->dev); |
| 669 | } |
| 670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | /* Router Alert as of RFC 2711 */ |
| 672 | |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 673 | static int ipv6_hop_ra(struct sk_buff **skbp, int optoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | { |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 675 | struct sk_buff *skb = *skbp; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 676 | const unsigned char *nh = skb_network_header(skb); |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 677 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 678 | if (nh[optoff + 1] == 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | IP6CB(skb)->ra = optoff; |
| 680 | return 1; |
| 681 | } |
Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 682 | LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_ra: wrong RA length %d\n", |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 683 | nh[optoff + 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | kfree_skb(skb); |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | /* Jumbo payload */ |
| 689 | |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 690 | static int ipv6_hop_jumbo(struct sk_buff **skbp, int optoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 692 | struct sk_buff *skb = *skbp; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 693 | const unsigned char *nh = skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | u32 pkt_len; |
| 695 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 696 | if (nh[optoff + 1] != 4 || (optoff & 3) != 2) { |
Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 697 | LIMIT_NETDEBUG(KERN_DEBUG "ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n", |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 698 | nh[optoff+1]); |
YOSHIFUJI Hideaki | e76b2b2 | 2007-05-09 14:01:59 -0700 | [diff] [blame] | 699 | IP6_INC_STATS_BH(ipv6_skb_idev(skb), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 700 | IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | goto drop; |
| 702 | } |
| 703 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 704 | pkt_len = ntohl(*(__be32 *)(nh + optoff + 2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | if (pkt_len <= IPV6_MAXPLEN) { |
YOSHIFUJI Hideaki | e76b2b2 | 2007-05-09 14:01:59 -0700 | [diff] [blame] | 706 | IP6_INC_STATS_BH(ipv6_skb_idev(skb), IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2); |
| 708 | return 0; |
| 709 | } |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 710 | if (ipv6_hdr(skb)->payload_len) { |
YOSHIFUJI Hideaki | e76b2b2 | 2007-05-09 14:01:59 -0700 | [diff] [blame] | 711 | IP6_INC_STATS_BH(ipv6_skb_idev(skb), IPSTATS_MIB_INHDRERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff); |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | if (pkt_len > skb->len - sizeof(struct ipv6hdr)) { |
YOSHIFUJI Hideaki | e76b2b2 | 2007-05-09 14:01:59 -0700 | [diff] [blame] | 717 | IP6_INC_STATS_BH(ipv6_skb_idev(skb), IPSTATS_MIB_INTRUNCATEDPKTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | goto drop; |
| 719 | } |
Stephen Hemminger | 42ca89c | 2005-09-08 12:57:43 -0700 | [diff] [blame] | 720 | |
| 721 | if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) |
| 722 | goto drop; |
| 723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | return 1; |
| 725 | |
| 726 | drop: |
| 727 | kfree_skb(skb); |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static struct tlvtype_proc tlvprochopopt_lst[] = { |
| 732 | { |
| 733 | .type = IPV6_TLV_ROUTERALERT, |
| 734 | .func = ipv6_hop_ra, |
| 735 | }, |
| 736 | { |
| 737 | .type = IPV6_TLV_JUMBO, |
| 738 | .func = ipv6_hop_jumbo, |
| 739 | }, |
| 740 | { -1, } |
| 741 | }; |
| 742 | |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 743 | int ipv6_parse_hopopts(struct sk_buff **skbp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | { |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 745 | struct sk_buff *skb = *skbp; |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 746 | struct inet6_skb_parm *opt = IP6CB(skb); |
| 747 | |
YOSHIFUJI Hideaki | ec67009 | 2006-04-18 14:46:26 -0700 | [diff] [blame] | 748 | /* |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 749 | * skb_network_header(skb) is equal to skb->data, and |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 750 | * skb_network_header_len(skb) is always equal to |
YOSHIFUJI Hideaki | ec67009 | 2006-04-18 14:46:26 -0700 | [diff] [blame] | 751 | * sizeof(struct ipv6hdr) by definition of |
| 752 | * hop-by-hop options. |
| 753 | */ |
| 754 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) || |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 755 | !pskb_may_pull(skb, (sizeof(struct ipv6hdr) + |
| 756 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
YOSHIFUJI Hideaki | ec67009 | 2006-04-18 14:46:26 -0700 | [diff] [blame] | 757 | kfree_skb(skb); |
| 758 | return -1; |
| 759 | } |
| 760 | |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 761 | opt->hop = sizeof(struct ipv6hdr); |
Masahide NAKAMURA | a80ff03 | 2006-08-23 19:19:50 -0700 | [diff] [blame] | 762 | if (ip6_parse_tlv(tlvprochopopt_lst, skbp)) { |
| 763 | skb = *skbp; |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 764 | skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3; |
Masahide NAKAMURA | dc435e6 | 2006-08-31 15:18:49 -0700 | [diff] [blame] | 765 | opt = IP6CB(skb); |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 766 | opt->nhoff = sizeof(struct ipv6hdr); |
YOSHIFUJI Hideaki | b809739 | 2006-04-18 14:48:45 -0700 | [diff] [blame] | 767 | return 1; |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | return -1; |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * Creating outbound headers. |
| 774 | * |
| 775 | * "build" functions work when skb is filled from head to tail (datagram) |
| 776 | * "push" functions work when headers are added from tail to head (tcp) |
| 777 | * |
| 778 | * In both cases we assume, that caller reserved enough room |
| 779 | * for headers. |
| 780 | */ |
| 781 | |
| 782 | static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto, |
| 783 | struct ipv6_rt_hdr *opt, |
| 784 | struct in6_addr **addr_p) |
| 785 | { |
| 786 | struct rt0_hdr *phdr, *ihdr; |
| 787 | int hops; |
| 788 | |
| 789 | ihdr = (struct rt0_hdr *) opt; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | phdr = (struct rt0_hdr *) skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3); |
| 792 | memcpy(phdr, ihdr, sizeof(struct rt0_hdr)); |
| 793 | |
| 794 | hops = ihdr->rt_hdr.hdrlen >> 1; |
| 795 | |
| 796 | if (hops > 1) |
| 797 | memcpy(phdr->addr, ihdr->addr + 1, |
| 798 | (hops - 1) * sizeof(struct in6_addr)); |
| 799 | |
| 800 | ipv6_addr_copy(phdr->addr + (hops - 1), *addr_p); |
| 801 | *addr_p = ihdr->addr; |
| 802 | |
| 803 | phdr->rt_hdr.nexthdr = *proto; |
| 804 | *proto = NEXTHDR_ROUTING; |
| 805 | } |
| 806 | |
| 807 | static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt) |
| 808 | { |
| 809 | struct ipv6_opt_hdr *h = (struct ipv6_opt_hdr *)skb_push(skb, ipv6_optlen(opt)); |
| 810 | |
| 811 | memcpy(h, opt, ipv6_optlen(opt)); |
| 812 | h->nexthdr = *proto; |
| 813 | *proto = type; |
| 814 | } |
| 815 | |
| 816 | void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, |
| 817 | u8 *proto, |
| 818 | struct in6_addr **daddr) |
| 819 | { |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 820 | if (opt->srcrt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | ipv6_push_rthdr(skb, proto, opt->srcrt, daddr); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 822 | /* |
| 823 | * IPV6_RTHDRDSTOPTS is ignored |
| 824 | * unless IPV6_RTHDR is set (RFC3542). |
| 825 | */ |
| 826 | if (opt->dst0opt) |
| 827 | ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt); |
| 828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | if (opt->hopopt) |
| 830 | ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt); |
| 831 | } |
| 832 | |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 833 | EXPORT_SYMBOL(ipv6_push_nfrag_opts); |
| 834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto) |
| 836 | { |
| 837 | if (opt->dst1opt) |
| 838 | ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt); |
| 839 | } |
| 840 | |
| 841 | struct ipv6_txoptions * |
| 842 | ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt) |
| 843 | { |
| 844 | struct ipv6_txoptions *opt2; |
| 845 | |
| 846 | opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC); |
| 847 | if (opt2) { |
| 848 | long dif = (char*)opt2 - (char*)opt; |
| 849 | memcpy(opt2, opt, opt->tot_len); |
| 850 | if (opt2->hopopt) |
| 851 | *((char**)&opt2->hopopt) += dif; |
| 852 | if (opt2->dst0opt) |
| 853 | *((char**)&opt2->dst0opt) += dif; |
| 854 | if (opt2->dst1opt) |
| 855 | *((char**)&opt2->dst1opt) += dif; |
| 856 | if (opt2->srcrt) |
| 857 | *((char**)&opt2->srcrt) += dif; |
| 858 | } |
| 859 | return opt2; |
| 860 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 861 | |
Arnaldo Carvalho de Melo | 3cf3dc6 | 2005-12-13 23:23:20 -0800 | [diff] [blame] | 862 | EXPORT_SYMBOL_GPL(ipv6_dup_options); |
| 863 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 864 | static int ipv6_renew_option(void *ohdr, |
| 865 | struct ipv6_opt_hdr __user *newopt, int newoptlen, |
| 866 | int inherit, |
| 867 | struct ipv6_opt_hdr **hdr, |
| 868 | char **p) |
| 869 | { |
| 870 | if (inherit) { |
| 871 | if (ohdr) { |
| 872 | memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr)); |
| 873 | *hdr = (struct ipv6_opt_hdr *)*p; |
| 874 | *p += CMSG_ALIGN(ipv6_optlen(*(struct ipv6_opt_hdr **)hdr)); |
| 875 | } |
| 876 | } else { |
| 877 | if (newopt) { |
| 878 | if (copy_from_user(*p, newopt, newoptlen)) |
| 879 | return -EFAULT; |
| 880 | *hdr = (struct ipv6_opt_hdr *)*p; |
| 881 | if (ipv6_optlen(*(struct ipv6_opt_hdr **)hdr) > newoptlen) |
| 882 | return -EINVAL; |
| 883 | *p += CMSG_ALIGN(newoptlen); |
| 884 | } |
| 885 | } |
| 886 | return 0; |
| 887 | } |
| 888 | |
| 889 | struct ipv6_txoptions * |
| 890 | ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt, |
| 891 | int newtype, |
| 892 | struct ipv6_opt_hdr __user *newopt, int newoptlen) |
| 893 | { |
| 894 | int tot_len = 0; |
| 895 | char *p; |
| 896 | struct ipv6_txoptions *opt2; |
| 897 | int err; |
| 898 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 899 | if (opt) { |
| 900 | if (newtype != IPV6_HOPOPTS && opt->hopopt) |
| 901 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt)); |
| 902 | if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt) |
| 903 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt)); |
| 904 | if (newtype != IPV6_RTHDR && opt->srcrt) |
| 905 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt)); |
| 906 | if (newtype != IPV6_DSTOPTS && opt->dst1opt) |
| 907 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt)); |
| 908 | } |
| 909 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 910 | if (newopt && newoptlen) |
| 911 | tot_len += CMSG_ALIGN(newoptlen); |
| 912 | |
| 913 | if (!tot_len) |
| 914 | return NULL; |
| 915 | |
YOSHIFUJI Hideaki | 8b8aa4b | 2005-11-20 12:18:17 +0900 | [diff] [blame] | 916 | tot_len += sizeof(*opt2); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 917 | opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC); |
| 918 | if (!opt2) |
| 919 | return ERR_PTR(-ENOBUFS); |
| 920 | |
| 921 | memset(opt2, 0, tot_len); |
| 922 | |
| 923 | opt2->tot_len = tot_len; |
| 924 | p = (char *)(opt2 + 1); |
| 925 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 926 | err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen, |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 927 | newtype != IPV6_HOPOPTS, |
| 928 | &opt2->hopopt, &p); |
| 929 | if (err) |
| 930 | goto out; |
| 931 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 932 | err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen, |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 933 | newtype != IPV6_RTHDRDSTOPTS, |
| 934 | &opt2->dst0opt, &p); |
| 935 | if (err) |
| 936 | goto out; |
| 937 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 938 | err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen, |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 939 | newtype != IPV6_RTHDR, |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 940 | (struct ipv6_opt_hdr **)&opt2->srcrt, &p); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 941 | if (err) |
| 942 | goto out; |
| 943 | |
YOSHIFUJI Hideaki | 99c7bc0 | 2006-08-31 14:52:17 -0700 | [diff] [blame] | 944 | err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen, |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 945 | newtype != IPV6_DSTOPTS, |
| 946 | &opt2->dst1opt, &p); |
| 947 | if (err) |
| 948 | goto out; |
| 949 | |
| 950 | opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) + |
| 951 | (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) + |
| 952 | (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0); |
| 953 | opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0); |
| 954 | |
| 955 | return opt2; |
| 956 | out: |
YOSHIFUJI Hideaki | 8b8aa4b | 2005-11-20 12:18:17 +0900 | [diff] [blame] | 957 | sock_kfree_s(sk, opt2, opt2->tot_len); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 958 | return ERR_PTR(err); |
| 959 | } |
| 960 | |
YOSHIFUJI Hideaki | df9890c | 2005-11-20 12:23:18 +0900 | [diff] [blame] | 961 | struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space, |
| 962 | struct ipv6_txoptions *opt) |
| 963 | { |
| 964 | /* |
| 965 | * ignore the dest before srcrt unless srcrt is being included. |
| 966 | * --yoshfuji |
| 967 | */ |
| 968 | if (opt && opt->dst0opt && !opt->srcrt) { |
| 969 | if (opt_space != opt) { |
| 970 | memcpy(opt_space, opt, sizeof(*opt_space)); |
| 971 | opt = opt_space; |
| 972 | } |
| 973 | opt->opt_nflen -= ipv6_optlen(opt->dst0opt); |
| 974 | opt->dst0opt = NULL; |
| 975 | } |
| 976 | |
| 977 | return opt; |
| 978 | } |
| 979 | |