libertas_tf: add get_survey callback in order to get channel noise
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv6 / datagram.c
blob7d929a22cbc2f505aca94ba5a6fd9cae412824dc
1 /*
2 * common UDP/RAW code
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
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.
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
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>
24 #include <linux/slab.h>
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>
31 #include <net/tcp_states.h>
33 #include <linux/errqueue.h>
34 #include <asm/uaccess.h>
36 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
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, final;
42 struct dst_entry *dst;
43 struct flowi fl;
44 struct ip6_flowlabel *flowlabel = NULL;
45 struct ipv6_txoptions *opt;
46 int addr_type;
47 int err;
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;
56 if (addr_len < SIN6_LEN_RFC2133)
57 return -EINVAL;
59 if (usin->sin6_family != AF_INET6)
60 return -EAFNOSUPPORT;
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);
73 addr_type = ipv6_addr_type(&usin->sin6_addr);
75 if (addr_type == IPV6_ADDR_ANY) {
77 * connect to self
79 usin->sin6_addr.s6_addr[15] = 0x01;
82 daddr = &usin->sin6_addr;
84 if (addr_type == IPV6_ADDR_MAPPED) {
85 struct sockaddr_in sin;
87 if (__ipv6_only_sock(sk)) {
88 err = -ENETUNREACH;
89 goto out;
91 sin.sin_family = AF_INET;
92 sin.sin_addr.s_addr = daddr->s6_addr32[3];
93 sin.sin_port = usin->sin6_port;
95 err = ip4_datagram_connect(sk,
96 (struct sockaddr*) &sin,
97 sizeof(sin));
99 ipv4_connected:
100 if (err)
101 goto out;
103 ipv6_addr_set_v4mapped(inet->inet_daddr, &np->daddr);
105 if (ipv6_addr_any(&np->saddr))
106 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
108 if (ipv6_addr_any(&np->rcv_saddr))
109 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
110 &np->rcv_saddr);
112 goto out;
115 if (addr_type&IPV6_ADDR_LINKLOCAL) {
116 if (addr_len >= sizeof(struct sockaddr_in6) &&
117 usin->sin6_scope_id) {
118 if (sk->sk_bound_dev_if &&
119 sk->sk_bound_dev_if != usin->sin6_scope_id) {
120 err = -EINVAL;
121 goto out;
123 sk->sk_bound_dev_if = usin->sin6_scope_id;
126 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
127 sk->sk_bound_dev_if = np->mcast_oif;
129 /* Connect to link-local address requires an interface */
130 if (!sk->sk_bound_dev_if) {
131 err = -EINVAL;
132 goto out;
136 ipv6_addr_copy(&np->daddr, daddr);
137 np->flow_label = fl.fl6_flowlabel;
139 inet->inet_dport = usin->sin6_port;
142 * Check for a route to destination an obtain the
143 * destination cache for it.
146 fl.proto = sk->sk_protocol;
147 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
148 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
149 fl.oif = sk->sk_bound_dev_if;
150 fl.mark = sk->sk_mark;
151 fl.fl_ip_dport = inet->inet_dport;
152 fl.fl_ip_sport = inet->inet_sport;
154 if (!fl.oif && (addr_type&IPV6_ADDR_MULTICAST))
155 fl.oif = np->mcast_oif;
157 security_sk_classify_flow(sk, &fl);
159 opt = flowlabel ? flowlabel->opt : np->opt;
160 final_p = fl6_update_dst(&fl, opt, &final);
162 err = ip6_dst_lookup(sk, &dst, &fl);
163 if (err)
164 goto out;
165 if (final_p)
166 ipv6_addr_copy(&fl.fl6_dst, final_p);
168 err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT);
169 if (err < 0) {
170 if (err == -EREMOTE)
171 err = ip6_dst_blackhole(sk, &dst, &fl);
172 if (err < 0)
173 goto out;
176 /* source address lookup done in ip6_dst_lookup */
178 if (ipv6_addr_any(&np->saddr))
179 ipv6_addr_copy(&np->saddr, &fl.fl6_src);
181 if (ipv6_addr_any(&np->rcv_saddr)) {
182 ipv6_addr_copy(&np->rcv_saddr, &fl.fl6_src);
183 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
186 ip6_dst_store(sk, dst,
187 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
188 &np->daddr : NULL,
189 #ifdef CONFIG_IPV6_SUBTREES
190 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
191 &np->saddr :
192 #endif
193 NULL);
195 sk->sk_state = TCP_ESTABLISHED;
196 out:
197 fl6_sock_release(flowlabel);
198 return err;
201 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
202 __be16 port, u32 info, u8 *payload)
204 struct ipv6_pinfo *np = inet6_sk(sk);
205 struct icmp6hdr *icmph = icmp6_hdr(skb);
206 struct sock_exterr_skb *serr;
208 if (!np->recverr)
209 return;
211 skb = skb_clone(skb, GFP_ATOMIC);
212 if (!skb)
213 return;
215 skb->protocol = htons(ETH_P_IPV6);
217 serr = SKB_EXT_ERR(skb);
218 serr->ee.ee_errno = err;
219 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
220 serr->ee.ee_type = icmph->icmp6_type;
221 serr->ee.ee_code = icmph->icmp6_code;
222 serr->ee.ee_pad = 0;
223 serr->ee.ee_info = info;
224 serr->ee.ee_data = 0;
225 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
226 skb_network_header(skb);
227 serr->port = port;
229 __skb_pull(skb, payload - skb->data);
230 skb_reset_transport_header(skb);
232 if (sock_queue_err_skb(sk, skb))
233 kfree_skb(skb);
236 void ipv6_local_error(struct sock *sk, int err, struct flowi *fl, u32 info)
238 struct ipv6_pinfo *np = inet6_sk(sk);
239 struct sock_exterr_skb *serr;
240 struct ipv6hdr *iph;
241 struct sk_buff *skb;
243 if (!np->recverr)
244 return;
246 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
247 if (!skb)
248 return;
250 skb->protocol = htons(ETH_P_IPV6);
252 skb_put(skb, sizeof(struct ipv6hdr));
253 skb_reset_network_header(skb);
254 iph = ipv6_hdr(skb);
255 ipv6_addr_copy(&iph->daddr, &fl->fl6_dst);
257 serr = SKB_EXT_ERR(skb);
258 serr->ee.ee_errno = err;
259 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
260 serr->ee.ee_type = 0;
261 serr->ee.ee_code = 0;
262 serr->ee.ee_pad = 0;
263 serr->ee.ee_info = info;
264 serr->ee.ee_data = 0;
265 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
266 serr->port = fl->fl_ip_dport;
268 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
269 skb_reset_transport_header(skb);
271 if (sock_queue_err_skb(sk, skb))
272 kfree_skb(skb);
275 void ipv6_local_rxpmtu(struct sock *sk, struct flowi *fl, u32 mtu)
277 struct ipv6_pinfo *np = inet6_sk(sk);
278 struct ipv6hdr *iph;
279 struct sk_buff *skb;
280 struct ip6_mtuinfo *mtu_info;
282 if (!np->rxopt.bits.rxpmtu)
283 return;
285 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
286 if (!skb)
287 return;
289 skb_put(skb, sizeof(struct ipv6hdr));
290 skb_reset_network_header(skb);
291 iph = ipv6_hdr(skb);
292 ipv6_addr_copy(&iph->daddr, &fl->fl6_dst);
294 mtu_info = IP6CBMTU(skb);
295 if (!mtu_info) {
296 kfree_skb(skb);
297 return;
300 mtu_info->ip6m_mtu = mtu;
301 mtu_info->ip6m_addr.sin6_family = AF_INET6;
302 mtu_info->ip6m_addr.sin6_port = 0;
303 mtu_info->ip6m_addr.sin6_flowinfo = 0;
304 mtu_info->ip6m_addr.sin6_scope_id = fl->oif;
305 ipv6_addr_copy(&mtu_info->ip6m_addr.sin6_addr, &ipv6_hdr(skb)->daddr);
307 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
308 skb_reset_transport_header(skb);
310 skb = xchg(&np->rxpmtu, skb);
311 kfree_skb(skb);
315 * Handle MSG_ERRQUEUE
317 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
319 struct ipv6_pinfo *np = inet6_sk(sk);
320 struct sock_exterr_skb *serr;
321 struct sk_buff *skb, *skb2;
322 struct sockaddr_in6 *sin;
323 struct {
324 struct sock_extended_err ee;
325 struct sockaddr_in6 offender;
326 } errhdr;
327 int err;
328 int copied;
330 err = -EAGAIN;
331 skb = skb_dequeue(&sk->sk_error_queue);
332 if (skb == NULL)
333 goto out;
335 copied = skb->len;
336 if (copied > len) {
337 msg->msg_flags |= MSG_TRUNC;
338 copied = len;
340 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
341 if (err)
342 goto out_free_skb;
344 sock_recv_timestamp(msg, sk, skb);
346 serr = SKB_EXT_ERR(skb);
348 sin = (struct sockaddr_in6 *)msg->msg_name;
349 if (sin) {
350 const unsigned char *nh = skb_network_header(skb);
351 sin->sin6_family = AF_INET6;
352 sin->sin6_flowinfo = 0;
353 sin->sin6_port = serr->port;
354 sin->sin6_scope_id = 0;
355 if (skb->protocol == htons(ETH_P_IPV6)) {
356 ipv6_addr_copy(&sin->sin6_addr,
357 (struct in6_addr *)(nh + serr->addr_offset));
358 if (np->sndflow)
359 sin->sin6_flowinfo =
360 (*(__be32 *)(nh + serr->addr_offset - 24) &
361 IPV6_FLOWINFO_MASK);
362 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
363 sin->sin6_scope_id = IP6CB(skb)->iif;
364 } else {
365 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
366 &sin->sin6_addr);
370 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
371 sin = &errhdr.offender;
372 sin->sin6_family = AF_UNSPEC;
373 if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
374 sin->sin6_family = AF_INET6;
375 sin->sin6_flowinfo = 0;
376 sin->sin6_scope_id = 0;
377 if (skb->protocol == htons(ETH_P_IPV6)) {
378 ipv6_addr_copy(&sin->sin6_addr, &ipv6_hdr(skb)->saddr);
379 if (np->rxopt.all)
380 datagram_recv_ctl(sk, msg, skb);
381 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
382 sin->sin6_scope_id = IP6CB(skb)->iif;
383 } else {
384 struct inet_sock *inet = inet_sk(sk);
386 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
387 &sin->sin6_addr);
388 if (inet->cmsg_flags)
389 ip_cmsg_recv(msg, skb);
393 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
395 /* Now we could try to dump offended packet options */
397 msg->msg_flags |= MSG_ERRQUEUE;
398 err = copied;
400 /* Reset and regenerate socket error */
401 spin_lock_bh(&sk->sk_error_queue.lock);
402 sk->sk_err = 0;
403 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
404 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
405 spin_unlock_bh(&sk->sk_error_queue.lock);
406 sk->sk_error_report(sk);
407 } else {
408 spin_unlock_bh(&sk->sk_error_queue.lock);
411 out_free_skb:
412 kfree_skb(skb);
413 out:
414 return err;
418 * Handle IPV6_RECVPATHMTU
420 int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len)
422 struct ipv6_pinfo *np = inet6_sk(sk);
423 struct sk_buff *skb;
424 struct sockaddr_in6 *sin;
425 struct ip6_mtuinfo mtu_info;
426 int err;
427 int copied;
429 err = -EAGAIN;
430 skb = xchg(&np->rxpmtu, NULL);
431 if (skb == NULL)
432 goto out;
434 copied = skb->len;
435 if (copied > len) {
436 msg->msg_flags |= MSG_TRUNC;
437 copied = len;
439 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
440 if (err)
441 goto out_free_skb;
443 sock_recv_timestamp(msg, sk, skb);
445 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
447 sin = (struct sockaddr_in6 *)msg->msg_name;
448 if (sin) {
449 sin->sin6_family = AF_INET6;
450 sin->sin6_flowinfo = 0;
451 sin->sin6_port = 0;
452 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
453 ipv6_addr_copy(&sin->sin6_addr, &mtu_info.ip6m_addr.sin6_addr);
456 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
458 err = copied;
460 out_free_skb:
461 kfree_skb(skb);
462 out:
463 return err;
467 int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
469 struct ipv6_pinfo *np = inet6_sk(sk);
470 struct inet6_skb_parm *opt = IP6CB(skb);
471 unsigned char *nh = skb_network_header(skb);
473 if (np->rxopt.bits.rxinfo) {
474 struct in6_pktinfo src_info;
476 src_info.ipi6_ifindex = opt->iif;
477 ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr);
478 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
481 if (np->rxopt.bits.rxhlim) {
482 int hlim = ipv6_hdr(skb)->hop_limit;
483 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
486 if (np->rxopt.bits.rxtclass) {
487 int tclass = (ntohl(*(__be32 *)ipv6_hdr(skb)) >> 20) & 0xff;
488 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
491 if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) {
492 __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK;
493 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
496 /* HbH is allowed only once */
497 if (np->rxopt.bits.hopopts && opt->hop) {
498 u8 *ptr = nh + opt->hop;
499 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
502 if (opt->lastopt &&
503 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
505 * Silly enough, but we need to reparse in order to
506 * report extension headers (except for HbH)
507 * in order.
509 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
510 * (and WILL NOT be) defined because
511 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
513 unsigned int off = sizeof(struct ipv6hdr);
514 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
516 while (off <= opt->lastopt) {
517 unsigned len;
518 u8 *ptr = nh + off;
520 switch(nexthdr) {
521 case IPPROTO_DSTOPTS:
522 nexthdr = ptr[0];
523 len = (ptr[1] + 1) << 3;
524 if (np->rxopt.bits.dstopts)
525 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
526 break;
527 case IPPROTO_ROUTING:
528 nexthdr = ptr[0];
529 len = (ptr[1] + 1) << 3;
530 if (np->rxopt.bits.srcrt)
531 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
532 break;
533 case IPPROTO_AH:
534 nexthdr = ptr[0];
535 len = (ptr[1] + 2) << 2;
536 break;
537 default:
538 nexthdr = ptr[0];
539 len = (ptr[1] + 1) << 3;
540 break;
543 off += len;
547 /* socket options in old style */
548 if (np->rxopt.bits.rxoinfo) {
549 struct in6_pktinfo src_info;
551 src_info.ipi6_ifindex = opt->iif;
552 ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr);
553 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
555 if (np->rxopt.bits.rxohlim) {
556 int hlim = ipv6_hdr(skb)->hop_limit;
557 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
559 if (np->rxopt.bits.ohopopts && opt->hop) {
560 u8 *ptr = nh + opt->hop;
561 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
563 if (np->rxopt.bits.odstopts && opt->dst0) {
564 u8 *ptr = nh + opt->dst0;
565 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
567 if (np->rxopt.bits.osrcrt && opt->srcrt) {
568 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
569 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
571 if (np->rxopt.bits.odstopts && opt->dst1) {
572 u8 *ptr = nh + opt->dst1;
573 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
575 return 0;
578 int datagram_send_ctl(struct net *net,
579 struct msghdr *msg, struct flowi *fl,
580 struct ipv6_txoptions *opt,
581 int *hlimit, int *tclass, int *dontfrag)
583 struct in6_pktinfo *src_info;
584 struct cmsghdr *cmsg;
585 struct ipv6_rt_hdr *rthdr;
586 struct ipv6_opt_hdr *hdr;
587 int len;
588 int err = 0;
590 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
591 int addr_type;
593 if (!CMSG_OK(msg, cmsg)) {
594 err = -EINVAL;
595 goto exit_f;
598 if (cmsg->cmsg_level != SOL_IPV6)
599 continue;
601 switch (cmsg->cmsg_type) {
602 case IPV6_PKTINFO:
603 case IPV6_2292PKTINFO:
605 struct net_device *dev = NULL;
607 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
608 err = -EINVAL;
609 goto exit_f;
612 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
614 if (src_info->ipi6_ifindex) {
615 if (fl->oif && src_info->ipi6_ifindex != fl->oif)
616 return -EINVAL;
617 fl->oif = src_info->ipi6_ifindex;
620 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
622 rcu_read_lock();
623 if (fl->oif) {
624 dev = dev_get_by_index_rcu(net, fl->oif);
625 if (!dev) {
626 rcu_read_unlock();
627 return -ENODEV;
629 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
630 rcu_read_unlock();
631 return -EINVAL;
634 if (addr_type != IPV6_ADDR_ANY) {
635 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
636 if (!ipv6_chk_addr(net, &src_info->ipi6_addr,
637 strict ? dev : NULL, 0))
638 err = -EINVAL;
639 else
640 ipv6_addr_copy(&fl->fl6_src, &src_info->ipi6_addr);
643 rcu_read_unlock();
645 if (err)
646 goto exit_f;
648 break;
651 case IPV6_FLOWINFO:
652 if (cmsg->cmsg_len < CMSG_LEN(4)) {
653 err = -EINVAL;
654 goto exit_f;
657 if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) {
658 if ((fl->fl6_flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
659 err = -EINVAL;
660 goto exit_f;
663 fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
664 break;
666 case IPV6_2292HOPOPTS:
667 case IPV6_HOPOPTS:
668 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
669 err = -EINVAL;
670 goto exit_f;
673 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
674 len = ((hdr->hdrlen + 1) << 3);
675 if (cmsg->cmsg_len < CMSG_LEN(len)) {
676 err = -EINVAL;
677 goto exit_f;
679 if (!capable(CAP_NET_RAW)) {
680 err = -EPERM;
681 goto exit_f;
683 opt->opt_nflen += len;
684 opt->hopopt = hdr;
685 break;
687 case IPV6_2292DSTOPTS:
688 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
689 err = -EINVAL;
690 goto exit_f;
693 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
694 len = ((hdr->hdrlen + 1) << 3);
695 if (cmsg->cmsg_len < CMSG_LEN(len)) {
696 err = -EINVAL;
697 goto exit_f;
699 if (!capable(CAP_NET_RAW)) {
700 err = -EPERM;
701 goto exit_f;
703 if (opt->dst1opt) {
704 err = -EINVAL;
705 goto exit_f;
707 opt->opt_flen += len;
708 opt->dst1opt = hdr;
709 break;
711 case IPV6_DSTOPTS:
712 case IPV6_RTHDRDSTOPTS:
713 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
714 err = -EINVAL;
715 goto exit_f;
718 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
719 len = ((hdr->hdrlen + 1) << 3);
720 if (cmsg->cmsg_len < CMSG_LEN(len)) {
721 err = -EINVAL;
722 goto exit_f;
724 if (!capable(CAP_NET_RAW)) {
725 err = -EPERM;
726 goto exit_f;
728 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
729 opt->opt_flen += len;
730 opt->dst1opt = hdr;
731 } else {
732 opt->opt_nflen += len;
733 opt->dst0opt = hdr;
735 break;
737 case IPV6_2292RTHDR:
738 case IPV6_RTHDR:
739 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
740 err = -EINVAL;
741 goto exit_f;
744 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
746 switch (rthdr->type) {
747 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
748 case IPV6_SRCRT_TYPE_2:
749 if (rthdr->hdrlen != 2 ||
750 rthdr->segments_left != 1) {
751 err = -EINVAL;
752 goto exit_f;
754 break;
755 #endif
756 default:
757 err = -EINVAL;
758 goto exit_f;
761 len = ((rthdr->hdrlen + 1) << 3);
763 if (cmsg->cmsg_len < CMSG_LEN(len)) {
764 err = -EINVAL;
765 goto exit_f;
768 /* segments left must also match */
769 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
770 err = -EINVAL;
771 goto exit_f;
774 opt->opt_nflen += len;
775 opt->srcrt = rthdr;
777 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
778 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
780 opt->opt_nflen += dsthdrlen;
781 opt->dst0opt = opt->dst1opt;
782 opt->dst1opt = NULL;
783 opt->opt_flen -= dsthdrlen;
786 break;
788 case IPV6_2292HOPLIMIT:
789 case IPV6_HOPLIMIT:
790 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
791 err = -EINVAL;
792 goto exit_f;
795 *hlimit = *(int *)CMSG_DATA(cmsg);
796 if (*hlimit < -1 || *hlimit > 0xff) {
797 err = -EINVAL;
798 goto exit_f;
801 break;
803 case IPV6_TCLASS:
805 int tc;
807 err = -EINVAL;
808 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
809 goto exit_f;
812 tc = *(int *)CMSG_DATA(cmsg);
813 if (tc < -1 || tc > 0xff)
814 goto exit_f;
816 err = 0;
817 *tclass = tc;
819 break;
822 case IPV6_DONTFRAG:
824 int df;
826 err = -EINVAL;
827 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
828 goto exit_f;
831 df = *(int *)CMSG_DATA(cmsg);
832 if (df < 0 || df > 1)
833 goto exit_f;
835 err = 0;
836 *dontfrag = df;
838 break;
840 default:
841 LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
842 cmsg->cmsg_type);
843 err = -EINVAL;
844 goto exit_f;
848 exit_f:
849 return err;