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