Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * common UDP/RAW code |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3 | * Linux INET6 implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 14 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/errno.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/socket.h> |
| 20 | #include <linux/sockios.h> |
| 21 | #include <linux/in6.h> |
| 22 | #include <linux/ipv6.h> |
| 23 | #include <linux/route.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include <net/ipv6.h> |
| 27 | #include <net/ndisc.h> |
| 28 | #include <net/addrconf.h> |
| 29 | #include <net/transp_v6.h> |
| 30 | #include <net/ip6_route.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 31 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #include <linux/errqueue.h> |
| 34 | #include <asm/uaccess.h> |
| 35 | |
| 36 | int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
| 37 | { |
| 38 | struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr; |
| 39 | struct inet_sock *inet = inet_sk(sk); |
| 40 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 41 | struct in6_addr *daddr, *final_p = NULL, final; |
| 42 | struct dst_entry *dst; |
| 43 | struct flowi fl; |
| 44 | struct ip6_flowlabel *flowlabel = NULL; |
| 45 | int addr_type; |
| 46 | int err; |
| 47 | |
| 48 | if (usin->sin6_family == AF_INET) { |
| 49 | if (__ipv6_only_sock(sk)) |
| 50 | return -EAFNOSUPPORT; |
| 51 | err = ip4_datagram_connect(sk, uaddr, addr_len); |
| 52 | goto ipv4_connected; |
| 53 | } |
| 54 | |
| 55 | if (addr_len < SIN6_LEN_RFC2133) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 56 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 58 | if (usin->sin6_family != AF_INET6) |
| 59 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | memset(&fl, 0, sizeof(fl)); |
| 62 | if (np->sndflow) { |
| 63 | fl.fl6_flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK; |
| 64 | if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) { |
| 65 | flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel); |
| 66 | if (flowlabel == NULL) |
| 67 | return -EINVAL; |
| 68 | ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | addr_type = ipv6_addr_type(&usin->sin6_addr); |
| 73 | |
| 74 | if (addr_type == IPV6_ADDR_ANY) { |
| 75 | /* |
| 76 | * connect to self |
| 77 | */ |
| 78 | usin->sin6_addr.s6_addr[15] = 0x01; |
| 79 | } |
| 80 | |
| 81 | daddr = &usin->sin6_addr; |
| 82 | |
| 83 | if (addr_type == IPV6_ADDR_MAPPED) { |
| 84 | struct sockaddr_in sin; |
| 85 | |
| 86 | if (__ipv6_only_sock(sk)) { |
| 87 | err = -ENETUNREACH; |
| 88 | goto out; |
| 89 | } |
| 90 | sin.sin_family = AF_INET; |
| 91 | sin.sin_addr.s_addr = daddr->s6_addr32[3]; |
| 92 | sin.sin_port = usin->sin6_port; |
| 93 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 94 | err = ip4_datagram_connect(sk, |
| 95 | (struct sockaddr*) &sin, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | sizeof(sin)); |
| 97 | |
| 98 | ipv4_connected: |
| 99 | if (err) |
| 100 | goto out; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 101 | |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 102 | ipv6_addr_set_v4mapped(inet->inet_daddr, &np->daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Brian Haley | b301e82 | 2009-10-07 13:58:25 -0700 | [diff] [blame] | 104 | if (ipv6_addr_any(&np->saddr)) |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 105 | ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Brian Haley | b301e82 | 2009-10-07 13:58:25 -0700 | [diff] [blame] | 107 | if (ipv6_addr_any(&np->rcv_saddr)) |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 108 | ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, |
| 109 | &np->rcv_saddr); |
Brian Haley | b301e82 | 2009-10-07 13:58:25 -0700 | [diff] [blame] | 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | goto out; |
| 112 | } |
| 113 | |
| 114 | if (addr_type&IPV6_ADDR_LINKLOCAL) { |
| 115 | if (addr_len >= sizeof(struct sockaddr_in6) && |
| 116 | usin->sin6_scope_id) { |
| 117 | if (sk->sk_bound_dev_if && |
| 118 | sk->sk_bound_dev_if != usin->sin6_scope_id) { |
| 119 | err = -EINVAL; |
| 120 | goto out; |
| 121 | } |
| 122 | sk->sk_bound_dev_if = usin->sin6_scope_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Brian Haley | 1ac4f00 | 2008-01-08 23:52:21 -0800 | [diff] [blame] | 125 | if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST)) |
| 126 | sk->sk_bound_dev_if = np->mcast_oif; |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | /* Connect to link-local address requires an interface */ |
| 129 | if (!sk->sk_bound_dev_if) { |
| 130 | err = -EINVAL; |
| 131 | goto out; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | ipv6_addr_copy(&np->daddr, daddr); |
| 136 | np->flow_label = fl.fl6_flowlabel; |
| 137 | |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 138 | inet->inet_dport = usin->sin6_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Check for a route to destination an obtain the |
| 142 | * destination cache for it. |
| 143 | */ |
| 144 | |
| 145 | fl.proto = sk->sk_protocol; |
| 146 | ipv6_addr_copy(&fl.fl6_dst, &np->daddr); |
| 147 | ipv6_addr_copy(&fl.fl6_src, &np->saddr); |
| 148 | fl.oif = sk->sk_bound_dev_if; |
Brian Haley | 51953d5 | 2009-10-05 08:24:16 +0000 | [diff] [blame] | 149 | fl.mark = sk->sk_mark; |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 150 | fl.fl_ip_dport = inet->inet_dport; |
| 151 | fl.fl_ip_sport = inet->inet_sport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | if (!fl.oif && (addr_type&IPV6_ADDR_MULTICAST)) |
| 154 | fl.oif = np->mcast_oif; |
| 155 | |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 156 | security_sk_classify_flow(sk, &fl); |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | if (flowlabel) { |
| 159 | if (flowlabel->opt && flowlabel->opt->srcrt) { |
| 160 | struct rt0_hdr *rt0 = (struct rt0_hdr *) flowlabel->opt->srcrt; |
| 161 | ipv6_addr_copy(&final, &fl.fl6_dst); |
| 162 | ipv6_addr_copy(&fl.fl6_dst, rt0->addr); |
| 163 | final_p = &final; |
| 164 | } |
| 165 | } else if (np->opt && np->opt->srcrt) { |
| 166 | struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt; |
| 167 | ipv6_addr_copy(&final, &fl.fl6_dst); |
| 168 | ipv6_addr_copy(&fl.fl6_dst, rt0->addr); |
| 169 | final_p = &final; |
| 170 | } |
| 171 | |
| 172 | err = ip6_dst_lookup(sk, &dst, &fl); |
| 173 | if (err) |
| 174 | goto out; |
| 175 | if (final_p) |
| 176 | ipv6_addr_copy(&fl.fl6_dst, final_p); |
| 177 | |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 178 | err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT); |
| 179 | if (err < 0) { |
David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 180 | if (err == -EREMOTE) |
| 181 | err = ip6_dst_blackhole(sk, &dst, &fl); |
| 182 | if (err < 0) |
| 183 | goto out; |
| 184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | /* source address lookup done in ip6_dst_lookup */ |
| 187 | |
| 188 | if (ipv6_addr_any(&np->saddr)) |
| 189 | ipv6_addr_copy(&np->saddr, &fl.fl6_src); |
| 190 | |
| 191 | if (ipv6_addr_any(&np->rcv_saddr)) { |
| 192 | ipv6_addr_copy(&np->rcv_saddr, &fl.fl6_src); |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 193 | inet->inet_rcv_saddr = LOOPBACK4_IPV6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | ip6_dst_store(sk, dst, |
| 197 | ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ? |
YOSHIFUJI Hideaki | 8e1ef0a | 2006-08-29 17:15:09 -0700 | [diff] [blame] | 198 | &np->daddr : NULL, |
| 199 | #ifdef CONFIG_IPV6_SUBTREES |
| 200 | ipv6_addr_equal(&fl.fl6_src, &np->saddr) ? |
| 201 | &np->saddr : |
| 202 | #endif |
| 203 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | sk->sk_state = TCP_ESTABLISHED; |
| 206 | out: |
| 207 | fl6_sock_release(flowlabel); |
| 208 | return err; |
| 209 | } |
| 210 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 211 | void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, |
Al Viro | e69a4adc | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 212 | __be16 port, u32 info, u8 *payload) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
| 214 | struct ipv6_pinfo *np = inet6_sk(sk); |
Arnaldo Carvalho de Melo | cc70ab2 | 2007-03-13 14:03:22 -0300 | [diff] [blame] | 215 | struct icmp6hdr *icmph = icmp6_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | struct sock_exterr_skb *serr; |
| 217 | |
| 218 | if (!np->recverr) |
| 219 | return; |
| 220 | |
| 221 | skb = skb_clone(skb, GFP_ATOMIC); |
| 222 | if (!skb) |
| 223 | return; |
| 224 | |
| 225 | serr = SKB_EXT_ERR(skb); |
| 226 | serr->ee.ee_errno = err; |
| 227 | serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 228 | serr->ee.ee_type = icmph->icmp6_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | serr->ee.ee_code = icmph->icmp6_code; |
| 230 | serr->ee.ee_pad = 0; |
| 231 | serr->ee.ee_info = info; |
| 232 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 233 | serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) - |
| 234 | skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | serr->port = port; |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | __skb_pull(skb, payload - skb->data); |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 238 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
| 240 | if (sock_queue_err_skb(sk, skb)) |
| 241 | kfree_skb(skb); |
| 242 | } |
| 243 | |
| 244 | void ipv6_local_error(struct sock *sk, int err, struct flowi *fl, u32 info) |
| 245 | { |
| 246 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 247 | struct sock_exterr_skb *serr; |
| 248 | struct ipv6hdr *iph; |
| 249 | struct sk_buff *skb; |
| 250 | |
| 251 | if (!np->recverr) |
| 252 | return; |
| 253 | |
| 254 | skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC); |
| 255 | if (!skb) |
| 256 | return; |
| 257 | |
Arnaldo Carvalho de Melo | 1ced98e | 2007-03-10 19:57:15 -0300 | [diff] [blame] | 258 | skb_put(skb, sizeof(struct ipv6hdr)); |
| 259 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 260 | iph = ipv6_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | ipv6_addr_copy(&iph->daddr, &fl->fl6_dst); |
| 262 | |
| 263 | serr = SKB_EXT_ERR(skb); |
| 264 | serr->ee.ee_errno = err; |
| 265 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 266 | serr->ee.ee_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | serr->ee.ee_code = 0; |
| 268 | serr->ee.ee_pad = 0; |
| 269 | serr->ee.ee_info = info; |
| 270 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 271 | serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | serr->port = fl->fl_ip_dport; |
| 273 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 274 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 275 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | if (sock_queue_err_skb(sk, skb)) |
| 278 | kfree_skb(skb); |
| 279 | } |
| 280 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 281 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | * Handle MSG_ERRQUEUE |
| 283 | */ |
| 284 | int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len) |
| 285 | { |
| 286 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 287 | struct sock_exterr_skb *serr; |
| 288 | struct sk_buff *skb, *skb2; |
| 289 | struct sockaddr_in6 *sin; |
| 290 | struct { |
| 291 | struct sock_extended_err ee; |
| 292 | struct sockaddr_in6 offender; |
| 293 | } errhdr; |
| 294 | int err; |
| 295 | int copied; |
| 296 | |
| 297 | err = -EAGAIN; |
| 298 | skb = skb_dequeue(&sk->sk_error_queue); |
| 299 | if (skb == NULL) |
| 300 | goto out; |
| 301 | |
| 302 | copied = skb->len; |
| 303 | if (copied > len) { |
| 304 | msg->msg_flags |= MSG_TRUNC; |
| 305 | copied = len; |
| 306 | } |
| 307 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 308 | if (err) |
| 309 | goto out_free_skb; |
| 310 | |
| 311 | sock_recv_timestamp(msg, sk, skb); |
| 312 | |
| 313 | serr = SKB_EXT_ERR(skb); |
| 314 | |
| 315 | sin = (struct sockaddr_in6 *)msg->msg_name; |
| 316 | if (sin) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 317 | const unsigned char *nh = skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | sin->sin6_family = AF_INET6; |
| 319 | sin->sin6_flowinfo = 0; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 320 | sin->sin6_port = serr->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | sin->sin6_scope_id = 0; |
| 322 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) { |
| 323 | ipv6_addr_copy(&sin->sin6_addr, |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 324 | (struct in6_addr *)(nh + serr->addr_offset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | if (np->sndflow) |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 326 | sin->sin6_flowinfo = |
| 327 | (*(__be32 *)(nh + serr->addr_offset - 24) & |
| 328 | IPV6_FLOWINFO_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL) |
| 330 | sin->sin6_scope_id = IP6CB(skb)->iif; |
| 331 | } else { |
Brian Haley | b301e82 | 2009-10-07 13:58:25 -0700 | [diff] [blame] | 332 | ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset), |
| 333 | &sin->sin6_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); |
| 338 | sin = &errhdr.offender; |
| 339 | sin->sin6_family = AF_UNSPEC; |
| 340 | if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) { |
| 341 | sin->sin6_family = AF_INET6; |
| 342 | sin->sin6_flowinfo = 0; |
| 343 | sin->sin6_scope_id = 0; |
| 344 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6) { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 345 | ipv6_addr_copy(&sin->sin6_addr, &ipv6_hdr(skb)->saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | if (np->rxopt.all) |
| 347 | datagram_recv_ctl(sk, msg, skb); |
| 348 | if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL) |
| 349 | sin->sin6_scope_id = IP6CB(skb)->iif; |
| 350 | } else { |
| 351 | struct inet_sock *inet = inet_sk(sk); |
| 352 | |
Brian Haley | b301e82 | 2009-10-07 13:58:25 -0700 | [diff] [blame] | 353 | ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, |
| 354 | &sin->sin6_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | if (inet->cmsg_flags) |
| 356 | ip_cmsg_recv(msg, skb); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr); |
| 361 | |
| 362 | /* Now we could try to dump offended packet options */ |
| 363 | |
| 364 | msg->msg_flags |= MSG_ERRQUEUE; |
| 365 | err = copied; |
| 366 | |
| 367 | /* Reset and regenerate socket error */ |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 368 | spin_lock_bh(&sk->sk_error_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | sk->sk_err = 0; |
| 370 | if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) { |
| 371 | sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno; |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 372 | spin_unlock_bh(&sk->sk_error_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | sk->sk_error_report(sk); |
| 374 | } else { |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 375 | spin_unlock_bh(&sk->sk_error_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 378 | out_free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | kfree_skb(skb); |
| 380 | out: |
| 381 | return err; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | |
| 386 | int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb) |
| 387 | { |
| 388 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 389 | struct inet6_skb_parm *opt = IP6CB(skb); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 390 | unsigned char *nh = skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | if (np->rxopt.bits.rxinfo) { |
| 393 | struct in6_pktinfo src_info; |
| 394 | |
| 395 | src_info.ipi6_ifindex = opt->iif; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 396 | ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); |
| 398 | } |
| 399 | |
| 400 | if (np->rxopt.bits.rxhlim) { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 401 | int hlim = ipv6_hdr(skb)->hop_limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); |
| 403 | } |
| 404 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 405 | if (np->rxopt.bits.rxtclass) { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 406 | int tclass = (ntohl(*(__be32 *)ipv6_hdr(skb)) >> 20) & 0xff; |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 407 | put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); |
| 408 | } |
| 409 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 410 | if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) { |
| 411 | __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); |
| 413 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 414 | |
| 415 | /* HbH is allowed only once */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | if (np->rxopt.bits.hopopts && opt->hop) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 417 | u8 *ptr = nh + opt->hop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr); |
| 419 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 420 | |
| 421 | if (opt->lastopt && |
| 422 | (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) { |
| 423 | /* |
| 424 | * Silly enough, but we need to reparse in order to |
| 425 | * report extension headers (except for HbH) |
| 426 | * in order. |
| 427 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 428 | * Also note that IPV6_RECVRTHDRDSTOPTS is NOT |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 429 | * (and WILL NOT be) defined because |
| 430 | * IPV6_RECVDSTOPTS is more generic. --yoshfuji |
| 431 | */ |
| 432 | unsigned int off = sizeof(struct ipv6hdr); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 433 | u8 nexthdr = ipv6_hdr(skb)->nexthdr; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 434 | |
| 435 | while (off <= opt->lastopt) { |
| 436 | unsigned len; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 437 | u8 *ptr = nh + off; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 438 | |
| 439 | switch(nexthdr) { |
| 440 | case IPPROTO_DSTOPTS: |
| 441 | nexthdr = ptr[0]; |
| 442 | len = (ptr[1] + 1) << 3; |
| 443 | if (np->rxopt.bits.dstopts) |
| 444 | put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr); |
| 445 | break; |
| 446 | case IPPROTO_ROUTING: |
| 447 | nexthdr = ptr[0]; |
| 448 | len = (ptr[1] + 1) << 3; |
| 449 | if (np->rxopt.bits.srcrt) |
| 450 | put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr); |
| 451 | break; |
| 452 | case IPPROTO_AH: |
| 453 | nexthdr = ptr[0]; |
Ville Nuorvala | a305989 | 2005-11-20 12:21:59 +0900 | [diff] [blame] | 454 | len = (ptr[1] + 2) << 2; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 455 | break; |
| 456 | default: |
| 457 | nexthdr = ptr[0]; |
| 458 | len = (ptr[1] + 1) << 3; |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | off += len; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /* socket options in old style */ |
| 467 | if (np->rxopt.bits.rxoinfo) { |
| 468 | struct in6_pktinfo src_info; |
| 469 | |
| 470 | src_info.ipi6_ifindex = opt->iif; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 471 | ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 472 | put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); |
| 473 | } |
| 474 | if (np->rxopt.bits.rxohlim) { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 475 | int hlim = ipv6_hdr(skb)->hop_limit; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 476 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); |
| 477 | } |
| 478 | if (np->rxopt.bits.ohopopts && opt->hop) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 479 | u8 *ptr = nh + opt->hop; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 480 | put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr); |
| 481 | } |
| 482 | if (np->rxopt.bits.odstopts && opt->dst0) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 483 | u8 *ptr = nh + opt->dst0; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 484 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 486 | if (np->rxopt.bits.osrcrt && opt->srcrt) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 487 | struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt); |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 488 | put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 490 | if (np->rxopt.bits.odstopts && opt->dst1) { |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 491 | u8 *ptr = nh + opt->dst1; |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 492 | put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | return 0; |
| 495 | } |
| 496 | |
YOSHIFUJI Hideaki | 91e1908 | 2008-06-04 13:02:49 +0900 | [diff] [blame] | 497 | int datagram_send_ctl(struct net *net, |
| 498 | struct msghdr *msg, struct flowi *fl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | struct ipv6_txoptions *opt, |
Brian Haley | 13b52cd | 2010-04-23 11:26:08 +0000 | [diff] [blame^] | 500 | int *hlimit, int *tclass, int *dontfrag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
| 502 | struct in6_pktinfo *src_info; |
| 503 | struct cmsghdr *cmsg; |
| 504 | struct ipv6_rt_hdr *rthdr; |
| 505 | struct ipv6_opt_hdr *hdr; |
| 506 | int len; |
| 507 | int err = 0; |
| 508 | |
| 509 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
| 510 | int addr_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
| 512 | if (!CMSG_OK(msg, cmsg)) { |
| 513 | err = -EINVAL; |
| 514 | goto exit_f; |
| 515 | } |
| 516 | |
| 517 | if (cmsg->cmsg_level != SOL_IPV6) |
| 518 | continue; |
| 519 | |
| 520 | switch (cmsg->cmsg_type) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 521 | case IPV6_PKTINFO: |
| 522 | case IPV6_2292PKTINFO: |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 523 | { |
| 524 | struct net_device *dev = NULL; |
| 525 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 526 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | err = -EINVAL; |
| 528 | goto exit_f; |
| 529 | } |
| 530 | |
| 531 | src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | if (src_info->ipi6_ifindex) { |
| 534 | if (fl->oif && src_info->ipi6_ifindex != fl->oif) |
| 535 | return -EINVAL; |
| 536 | fl->oif = src_info->ipi6_ifindex; |
| 537 | } |
| 538 | |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 539 | addr_type = __ipv6_addr_type(&src_info->ipi6_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Eric Dumazet | 536b2e9 | 2009-11-02 12:21:06 +0100 | [diff] [blame] | 541 | rcu_read_lock(); |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 542 | if (fl->oif) { |
Eric Dumazet | 536b2e9 | 2009-11-02 12:21:06 +0100 | [diff] [blame] | 543 | dev = dev_get_by_index_rcu(net, fl->oif); |
| 544 | if (!dev) { |
| 545 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 546 | return -ENODEV; |
Eric Dumazet | 536b2e9 | 2009-11-02 12:21:06 +0100 | [diff] [blame] | 547 | } |
| 548 | } else if (addr_type & IPV6_ADDR_LINKLOCAL) { |
| 549 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 550 | return -EINVAL; |
Eric Dumazet | 536b2e9 | 2009-11-02 12:21:06 +0100 | [diff] [blame] | 551 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 552 | |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 553 | if (addr_type != IPV6_ADDR_ANY) { |
| 554 | int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL; |
YOSHIFUJI Hideaki | 91e1908 | 2008-06-04 13:02:49 +0900 | [diff] [blame] | 555 | if (!ipv6_chk_addr(net, &src_info->ipi6_addr, |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 556 | strict ? dev : NULL, 0)) |
| 557 | err = -EINVAL; |
| 558 | else |
| 559 | ipv6_addr_copy(&fl->fl6_src, &src_info->ipi6_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | } |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 561 | |
Eric Dumazet | 536b2e9 | 2009-11-02 12:21:06 +0100 | [diff] [blame] | 562 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 564 | if (err) |
| 565 | goto exit_f; |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | break; |
YOSHIFUJI Hideaki | 187e383 | 2008-06-04 13:01:37 +0900 | [diff] [blame] | 568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
| 570 | case IPV6_FLOWINFO: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 571 | if (cmsg->cmsg_len < CMSG_LEN(4)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | err = -EINVAL; |
| 573 | goto exit_f; |
| 574 | } |
| 575 | |
| 576 | if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) { |
Al Viro | 90bcaf7 | 2006-11-08 00:25:17 -0800 | [diff] [blame] | 577 | if ((fl->fl6_flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | err = -EINVAL; |
| 579 | goto exit_f; |
| 580 | } |
| 581 | } |
Al Viro | 90bcaf7 | 2006-11-08 00:25:17 -0800 | [diff] [blame] | 582 | fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | break; |
| 584 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 585 | case IPV6_2292HOPOPTS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | case IPV6_HOPOPTS: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 587 | if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | err = -EINVAL; |
| 589 | goto exit_f; |
| 590 | } |
| 591 | |
| 592 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); |
| 593 | len = ((hdr->hdrlen + 1) << 3); |
| 594 | if (cmsg->cmsg_len < CMSG_LEN(len)) { |
| 595 | err = -EINVAL; |
| 596 | goto exit_f; |
| 597 | } |
| 598 | if (!capable(CAP_NET_RAW)) { |
| 599 | err = -EPERM; |
| 600 | goto exit_f; |
| 601 | } |
| 602 | opt->opt_nflen += len; |
| 603 | opt->hopopt = hdr; |
| 604 | break; |
| 605 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 606 | case IPV6_2292DSTOPTS: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 607 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | err = -EINVAL; |
| 609 | goto exit_f; |
| 610 | } |
| 611 | |
| 612 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); |
| 613 | len = ((hdr->hdrlen + 1) << 3); |
| 614 | if (cmsg->cmsg_len < CMSG_LEN(len)) { |
| 615 | err = -EINVAL; |
| 616 | goto exit_f; |
| 617 | } |
| 618 | if (!capable(CAP_NET_RAW)) { |
| 619 | err = -EPERM; |
| 620 | goto exit_f; |
| 621 | } |
| 622 | if (opt->dst1opt) { |
| 623 | err = -EINVAL; |
| 624 | goto exit_f; |
| 625 | } |
| 626 | opt->opt_flen += len; |
| 627 | opt->dst1opt = hdr; |
| 628 | break; |
| 629 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 630 | case IPV6_DSTOPTS: |
| 631 | case IPV6_RTHDRDSTOPTS: |
| 632 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) { |
| 633 | err = -EINVAL; |
| 634 | goto exit_f; |
| 635 | } |
| 636 | |
| 637 | hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg); |
| 638 | len = ((hdr->hdrlen + 1) << 3); |
| 639 | if (cmsg->cmsg_len < CMSG_LEN(len)) { |
| 640 | err = -EINVAL; |
| 641 | goto exit_f; |
| 642 | } |
| 643 | if (!capable(CAP_NET_RAW)) { |
| 644 | err = -EPERM; |
| 645 | goto exit_f; |
| 646 | } |
| 647 | if (cmsg->cmsg_type == IPV6_DSTOPTS) { |
| 648 | opt->opt_flen += len; |
| 649 | opt->dst1opt = hdr; |
| 650 | } else { |
| 651 | opt->opt_nflen += len; |
| 652 | opt->dst0opt = hdr; |
| 653 | } |
| 654 | break; |
| 655 | |
| 656 | case IPV6_2292RTHDR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | case IPV6_RTHDR: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 658 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | err = -EINVAL; |
| 660 | goto exit_f; |
| 661 | } |
| 662 | |
| 663 | rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg); |
| 664 | |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 665 | switch (rthdr->type) { |
Masahide NAKAMURA | 59fbb3a6 | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 666 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 667 | case IPV6_SRCRT_TYPE_2: |
Brian Haley | 6e093d9 | 2008-11-12 22:59:21 -0800 | [diff] [blame] | 668 | if (rthdr->hdrlen != 2 || |
| 669 | rthdr->segments_left != 1) { |
| 670 | err = -EINVAL; |
| 671 | goto exit_f; |
| 672 | } |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 673 | break; |
YOSHIFUJI Hideaki | bb4dbf9 | 2007-07-10 22:55:49 -0700 | [diff] [blame] | 674 | #endif |
Masahide NAKAMURA | 280a9d3 | 2006-08-23 19:17:12 -0700 | [diff] [blame] | 675 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | err = -EINVAL; |
| 677 | goto exit_f; |
| 678 | } |
| 679 | |
| 680 | len = ((rthdr->hdrlen + 1) << 3); |
| 681 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 682 | if (cmsg->cmsg_len < CMSG_LEN(len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | err = -EINVAL; |
| 684 | goto exit_f; |
| 685 | } |
| 686 | |
| 687 | /* segments left must also match */ |
| 688 | if ((rthdr->hdrlen >> 1) != rthdr->segments_left) { |
| 689 | err = -EINVAL; |
| 690 | goto exit_f; |
| 691 | } |
| 692 | |
| 693 | opt->opt_nflen += len; |
| 694 | opt->srcrt = rthdr; |
| 695 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 696 | if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3); |
| 698 | |
| 699 | opt->opt_nflen += dsthdrlen; |
| 700 | opt->dst0opt = opt->dst1opt; |
| 701 | opt->dst1opt = NULL; |
| 702 | opt->opt_flen -= dsthdrlen; |
| 703 | } |
| 704 | |
| 705 | break; |
| 706 | |
YOSHIFUJI Hideaki | 333fad5 | 2005-09-08 09:59:17 +0900 | [diff] [blame] | 707 | case IPV6_2292HOPLIMIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | case IPV6_HOPLIMIT: |
| 709 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { |
| 710 | err = -EINVAL; |
| 711 | goto exit_f; |
| 712 | } |
| 713 | |
| 714 | *hlimit = *(int *)CMSG_DATA(cmsg); |
Shan Wei | e8766fc | 2008-06-10 15:50:55 +0800 | [diff] [blame] | 715 | if (*hlimit < -1 || *hlimit > 0xff) { |
| 716 | err = -EINVAL; |
| 717 | goto exit_f; |
| 718 | } |
| 719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | break; |
| 721 | |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 722 | case IPV6_TCLASS: |
| 723 | { |
| 724 | int tc; |
| 725 | |
| 726 | err = -EINVAL; |
| 727 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { |
| 728 | goto exit_f; |
| 729 | } |
| 730 | |
| 731 | tc = *(int *)CMSG_DATA(cmsg); |
Remi Denis-Courmont | d0ee011 | 2006-09-13 20:08:07 -0700 | [diff] [blame] | 732 | if (tc < -1 || tc > 0xff) |
YOSHIFUJI Hideaki | 41a1f8e | 2005-09-08 10:19:03 +0900 | [diff] [blame] | 733 | goto exit_f; |
| 734 | |
| 735 | err = 0; |
| 736 | *tclass = tc; |
| 737 | |
| 738 | break; |
| 739 | } |
Brian Haley | 13b52cd | 2010-04-23 11:26:08 +0000 | [diff] [blame^] | 740 | |
| 741 | case IPV6_DONTFRAG: |
| 742 | { |
| 743 | int df; |
| 744 | |
| 745 | err = -EINVAL; |
| 746 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) { |
| 747 | goto exit_f; |
| 748 | } |
| 749 | |
| 750 | df = *(int *)CMSG_DATA(cmsg); |
| 751 | if (df < 0 || df > 1) |
| 752 | goto exit_f; |
| 753 | |
| 754 | err = 0; |
| 755 | *dontfrag = df; |
| 756 | |
| 757 | break; |
| 758 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | default: |
Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 760 | LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n", |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 761 | cmsg->cmsg_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | err = -EINVAL; |
Miao Xie | 4a36702 | 2008-07-29 23:57:58 -0700 | [diff] [blame] | 763 | goto exit_f; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 764 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | exit_f: |
| 768 | return err; |
| 769 | } |