[RAW]: Consolidate proto->hash callback
[deliverable/linux.git] / net / ipv6 / raw.c
1 /*
2 * RAW sockets for IPv6
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 *
8 * Adapted from linux/net/ipv4/raw.c
9 *
10 * $Id: raw.c,v 1.51 2002/02/01 22:01:04 davem Exp $
11 *
12 * Fixes:
13 * Hideaki YOSHIFUJI : sin6_scope_id support
14 * YOSHIFUJI,H.@USAGI : raw checksum (RFC2292(bis) compliance)
15 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
21 */
22
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/socket.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/in6.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/icmpv6.h>
32 #include <linux/netfilter.h>
33 #include <linux/netfilter_ipv6.h>
34 #include <linux/skbuff.h>
35 #include <asm/uaccess.h>
36 #include <asm/ioctls.h>
37
38 #include <net/net_namespace.h>
39 #include <net/ip.h>
40 #include <net/sock.h>
41 #include <net/snmp.h>
42
43 #include <net/ipv6.h>
44 #include <net/ndisc.h>
45 #include <net/protocol.h>
46 #include <net/ip6_route.h>
47 #include <net/ip6_checksum.h>
48 #include <net/addrconf.h>
49 #include <net/transp_v6.h>
50 #include <net/udp.h>
51 #include <net/inet_common.h>
52 #include <net/tcp_states.h>
53 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
54 #include <net/mip6.h>
55 #endif
56
57 #include <net/raw.h>
58 #include <net/rawv6.h>
59 #include <net/xfrm.h>
60
61 #include <linux/proc_fs.h>
62 #include <linux/seq_file.h>
63
64 static struct raw_hashinfo raw_v6_hashinfo = {
65 .lock = __RW_LOCK_UNLOCKED(),
66 };
67
68 static void raw_v6_hash(struct sock *sk)
69 {
70 raw_hash_sk(sk, &raw_v6_hashinfo);
71 }
72
73 static void raw_v6_unhash(struct sock *sk)
74 {
75 write_lock_bh(&raw_v6_hashinfo.lock);
76 if (sk_del_node_init(sk))
77 sock_prot_dec_use(sk->sk_prot);
78 write_unlock_bh(&raw_v6_hashinfo.lock);
79 }
80
81
82 static struct sock *__raw_v6_lookup(struct sock *sk, unsigned short num,
83 struct in6_addr *loc_addr, struct in6_addr *rmt_addr, int dif)
84 {
85 struct hlist_node *node;
86 int is_multicast = ipv6_addr_is_multicast(loc_addr);
87
88 sk_for_each_from(sk, node)
89 if (inet_sk(sk)->num == num) {
90 struct ipv6_pinfo *np = inet6_sk(sk);
91
92 if (!ipv6_addr_any(&np->daddr) &&
93 !ipv6_addr_equal(&np->daddr, rmt_addr))
94 continue;
95
96 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
97 continue;
98
99 if (!ipv6_addr_any(&np->rcv_saddr)) {
100 if (ipv6_addr_equal(&np->rcv_saddr, loc_addr))
101 goto found;
102 if (is_multicast &&
103 inet6_mc_check(sk, loc_addr, rmt_addr))
104 goto found;
105 continue;
106 }
107 goto found;
108 }
109 sk = NULL;
110 found:
111 return sk;
112 }
113
114 /*
115 * 0 - deliver
116 * 1 - block
117 */
118 static __inline__ int icmpv6_filter(struct sock *sk, struct sk_buff *skb)
119 {
120 struct icmp6hdr *icmph;
121 struct raw6_sock *rp = raw6_sk(sk);
122
123 if (pskb_may_pull(skb, sizeof(struct icmp6hdr))) {
124 __u32 *data = &rp->filter.data[0];
125 int bit_nr;
126
127 icmph = (struct icmp6hdr *) skb->data;
128 bit_nr = icmph->icmp6_type;
129
130 return (data[bit_nr >> 5] & (1 << (bit_nr & 31))) != 0;
131 }
132 return 0;
133 }
134
135 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
136 static int (*mh_filter)(struct sock *sock, struct sk_buff *skb);
137
138 int rawv6_mh_filter_register(int (*filter)(struct sock *sock,
139 struct sk_buff *skb))
140 {
141 rcu_assign_pointer(mh_filter, filter);
142 return 0;
143 }
144 EXPORT_SYMBOL(rawv6_mh_filter_register);
145
146 int rawv6_mh_filter_unregister(int (*filter)(struct sock *sock,
147 struct sk_buff *skb))
148 {
149 rcu_assign_pointer(mh_filter, NULL);
150 synchronize_rcu();
151 return 0;
152 }
153 EXPORT_SYMBOL(rawv6_mh_filter_unregister);
154
155 #endif
156
157 /*
158 * demultiplex raw sockets.
159 * (should consider queueing the skb in the sock receive_queue
160 * without calling rawv6.c)
161 *
162 * Caller owns SKB so we must make clones.
163 */
164 static int ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
165 {
166 struct in6_addr *saddr;
167 struct in6_addr *daddr;
168 struct sock *sk;
169 int delivered = 0;
170 __u8 hash;
171
172 saddr = &ipv6_hdr(skb)->saddr;
173 daddr = saddr + 1;
174
175 hash = nexthdr & (MAX_INET_PROTOS - 1);
176
177 read_lock(&raw_v6_hashinfo.lock);
178 sk = sk_head(&raw_v6_hashinfo.ht[hash]);
179
180 /*
181 * The first socket found will be delivered after
182 * delivery to transport protocols.
183 */
184
185 if (sk == NULL)
186 goto out;
187
188 sk = __raw_v6_lookup(sk, nexthdr, daddr, saddr, IP6CB(skb)->iif);
189
190 while (sk) {
191 int filtered;
192
193 delivered = 1;
194 switch (nexthdr) {
195 case IPPROTO_ICMPV6:
196 filtered = icmpv6_filter(sk, skb);
197 break;
198
199 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
200 case IPPROTO_MH:
201 {
202 /* XXX: To validate MH only once for each packet,
203 * this is placed here. It should be after checking
204 * xfrm policy, however it doesn't. The checking xfrm
205 * policy is placed in rawv6_rcv() because it is
206 * required for each socket.
207 */
208 int (*filter)(struct sock *sock, struct sk_buff *skb);
209
210 filter = rcu_dereference(mh_filter);
211 filtered = filter ? filter(sk, skb) : 0;
212 break;
213 }
214 #endif
215 default:
216 filtered = 0;
217 break;
218 }
219
220 if (filtered < 0)
221 break;
222 if (filtered == 0) {
223 struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
224
225 /* Not releasing hash table! */
226 if (clone) {
227 nf_reset(clone);
228 rawv6_rcv(sk, clone);
229 }
230 }
231 sk = __raw_v6_lookup(sk_next(sk), nexthdr, daddr, saddr,
232 IP6CB(skb)->iif);
233 }
234 out:
235 read_unlock(&raw_v6_hashinfo.lock);
236 return delivered;
237 }
238
239 int raw6_local_deliver(struct sk_buff *skb, int nexthdr)
240 {
241 struct sock *raw_sk;
242
243 raw_sk = sk_head(&raw_v6_hashinfo.ht[nexthdr & (MAX_INET_PROTOS - 1)]);
244 if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
245 raw_sk = NULL;
246
247 return raw_sk != NULL;
248 }
249
250 /* This cleans up af_inet6 a bit. -DaveM */
251 static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
252 {
253 struct inet_sock *inet = inet_sk(sk);
254 struct ipv6_pinfo *np = inet6_sk(sk);
255 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
256 __be32 v4addr = 0;
257 int addr_type;
258 int err;
259
260 if (addr_len < SIN6_LEN_RFC2133)
261 return -EINVAL;
262 addr_type = ipv6_addr_type(&addr->sin6_addr);
263
264 /* Raw sockets are IPv6 only */
265 if (addr_type == IPV6_ADDR_MAPPED)
266 return(-EADDRNOTAVAIL);
267
268 lock_sock(sk);
269
270 err = -EINVAL;
271 if (sk->sk_state != TCP_CLOSE)
272 goto out;
273
274 /* Check if the address belongs to the host. */
275 if (addr_type != IPV6_ADDR_ANY) {
276 struct net_device *dev = NULL;
277
278 if (addr_type & IPV6_ADDR_LINKLOCAL) {
279 if (addr_len >= sizeof(struct sockaddr_in6) &&
280 addr->sin6_scope_id) {
281 /* Override any existing binding, if another
282 * one is supplied by user.
283 */
284 sk->sk_bound_dev_if = addr->sin6_scope_id;
285 }
286
287 /* Binding to link-local address requires an interface */
288 if (!sk->sk_bound_dev_if)
289 goto out;
290
291 dev = dev_get_by_index(&init_net, sk->sk_bound_dev_if);
292 if (!dev) {
293 err = -ENODEV;
294 goto out;
295 }
296 }
297
298 /* ipv4 addr of the socket is invalid. Only the
299 * unspecified and mapped address have a v4 equivalent.
300 */
301 v4addr = LOOPBACK4_IPV6;
302 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
303 err = -EADDRNOTAVAIL;
304 if (!ipv6_chk_addr(&addr->sin6_addr, dev, 0)) {
305 if (dev)
306 dev_put(dev);
307 goto out;
308 }
309 }
310 if (dev)
311 dev_put(dev);
312 }
313
314 inet->rcv_saddr = inet->saddr = v4addr;
315 ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
316 if (!(addr_type & IPV6_ADDR_MULTICAST))
317 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
318 err = 0;
319 out:
320 release_sock(sk);
321 return err;
322 }
323
324 static void rawv6_err(struct sock *sk, struct sk_buff *skb,
325 struct inet6_skb_parm *opt,
326 int type, int code, int offset, __be32 info)
327 {
328 struct inet_sock *inet = inet_sk(sk);
329 struct ipv6_pinfo *np = inet6_sk(sk);
330 int err;
331 int harderr;
332
333 /* Report error on raw socket, if:
334 1. User requested recverr.
335 2. Socket is connected (otherwise the error indication
336 is useless without recverr and error is hard.
337 */
338 if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
339 return;
340
341 harderr = icmpv6_err_convert(type, code, &err);
342 if (type == ICMPV6_PKT_TOOBIG)
343 harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
344
345 if (np->recverr) {
346 u8 *payload = skb->data;
347 if (!inet->hdrincl)
348 payload += offset;
349 ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
350 }
351
352 if (np->recverr || harderr) {
353 sk->sk_err = err;
354 sk->sk_error_report(sk);
355 }
356 }
357
358 void raw6_icmp_error(struct sk_buff *skb, int nexthdr,
359 int type, int code, int inner_offset, __be32 info)
360 {
361 struct sock *sk;
362 int hash;
363 struct in6_addr *saddr, *daddr;
364
365 hash = nexthdr & (RAW_HTABLE_SIZE - 1);
366
367 read_lock(&raw_v6_hashinfo.lock);
368 sk = sk_head(&raw_v6_hashinfo.ht[hash]);
369 if (sk != NULL) {
370 saddr = &ipv6_hdr(skb)->saddr;
371 daddr = &ipv6_hdr(skb)->daddr;
372
373 while ((sk = __raw_v6_lookup(sk, nexthdr, saddr, daddr,
374 IP6CB(skb)->iif))) {
375 rawv6_err(sk, skb, NULL, type, code,
376 inner_offset, info);
377 sk = sk_next(sk);
378 }
379 }
380 read_unlock(&raw_v6_hashinfo.lock);
381 }
382
383 static inline int rawv6_rcv_skb(struct sock * sk, struct sk_buff * skb)
384 {
385 if ((raw6_sk(sk)->checksum || sk->sk_filter) &&
386 skb_checksum_complete(skb)) {
387 atomic_inc(&sk->sk_drops);
388 kfree_skb(skb);
389 return 0;
390 }
391
392 /* Charge it to the socket. */
393 if (sock_queue_rcv_skb(sk,skb)<0) {
394 atomic_inc(&sk->sk_drops);
395 kfree_skb(skb);
396 return 0;
397 }
398
399 return 0;
400 }
401
402 /*
403 * This is next to useless...
404 * if we demultiplex in network layer we don't need the extra call
405 * just to queue the skb...
406 * maybe we could have the network decide upon a hint if it
407 * should call raw_rcv for demultiplexing
408 */
409 int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
410 {
411 struct inet_sock *inet = inet_sk(sk);
412 struct raw6_sock *rp = raw6_sk(sk);
413
414 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
415 atomic_inc(&sk->sk_drops);
416 kfree_skb(skb);
417 return NET_RX_DROP;
418 }
419
420 if (!rp->checksum)
421 skb->ip_summed = CHECKSUM_UNNECESSARY;
422
423 if (skb->ip_summed == CHECKSUM_COMPLETE) {
424 skb_postpull_rcsum(skb, skb_network_header(skb),
425 skb_network_header_len(skb));
426 if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
427 &ipv6_hdr(skb)->daddr,
428 skb->len, inet->num, skb->csum))
429 skb->ip_summed = CHECKSUM_UNNECESSARY;
430 }
431 if (!skb_csum_unnecessary(skb))
432 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
433 &ipv6_hdr(skb)->daddr,
434 skb->len,
435 inet->num, 0));
436
437 if (inet->hdrincl) {
438 if (skb_checksum_complete(skb)) {
439 atomic_inc(&sk->sk_drops);
440 kfree_skb(skb);
441 return 0;
442 }
443 }
444
445 rawv6_rcv_skb(sk, skb);
446 return 0;
447 }
448
449
450 /*
451 * This should be easy, if there is something there
452 * we return it, otherwise we block.
453 */
454
455 static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk,
456 struct msghdr *msg, size_t len,
457 int noblock, int flags, int *addr_len)
458 {
459 struct ipv6_pinfo *np = inet6_sk(sk);
460 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)msg->msg_name;
461 struct sk_buff *skb;
462 size_t copied;
463 int err;
464
465 if (flags & MSG_OOB)
466 return -EOPNOTSUPP;
467
468 if (addr_len)
469 *addr_len=sizeof(*sin6);
470
471 if (flags & MSG_ERRQUEUE)
472 return ipv6_recv_error(sk, msg, len);
473
474 skb = skb_recv_datagram(sk, flags, noblock, &err);
475 if (!skb)
476 goto out;
477
478 copied = skb->len;
479 if (copied > len) {
480 copied = len;
481 msg->msg_flags |= MSG_TRUNC;
482 }
483
484 if (skb_csum_unnecessary(skb)) {
485 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
486 } else if (msg->msg_flags&MSG_TRUNC) {
487 if (__skb_checksum_complete(skb))
488 goto csum_copy_err;
489 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
490 } else {
491 err = skb_copy_and_csum_datagram_iovec(skb, 0, msg->msg_iov);
492 if (err == -EINVAL)
493 goto csum_copy_err;
494 }
495 if (err)
496 goto out_free;
497
498 /* Copy the address. */
499 if (sin6) {
500 sin6->sin6_family = AF_INET6;
501 sin6->sin6_port = 0;
502 ipv6_addr_copy(&sin6->sin6_addr, &ipv6_hdr(skb)->saddr);
503 sin6->sin6_flowinfo = 0;
504 sin6->sin6_scope_id = 0;
505 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
506 sin6->sin6_scope_id = IP6CB(skb)->iif;
507 }
508
509 sock_recv_timestamp(msg, sk, skb);
510
511 if (np->rxopt.all)
512 datagram_recv_ctl(sk, msg, skb);
513
514 err = copied;
515 if (flags & MSG_TRUNC)
516 err = skb->len;
517
518 out_free:
519 skb_free_datagram(sk, skb);
520 out:
521 return err;
522
523 csum_copy_err:
524 skb_kill_datagram(sk, skb, flags);
525
526 /* Error for blocking case is chosen to masquerade
527 as some normal condition.
528 */
529 err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
530 atomic_inc(&sk->sk_drops);
531 goto out;
532 }
533
534 static int rawv6_push_pending_frames(struct sock *sk, struct flowi *fl,
535 struct raw6_sock *rp)
536 {
537 struct sk_buff *skb;
538 int err = 0;
539 int offset;
540 int len;
541 int total_len;
542 __wsum tmp_csum;
543 __sum16 csum;
544
545 if (!rp->checksum)
546 goto send;
547
548 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
549 goto out;
550
551 offset = rp->offset;
552 total_len = inet_sk(sk)->cork.length - (skb_network_header(skb) -
553 skb->data);
554 if (offset >= total_len - 1) {
555 err = -EINVAL;
556 ip6_flush_pending_frames(sk);
557 goto out;
558 }
559
560 /* should be check HW csum miyazawa */
561 if (skb_queue_len(&sk->sk_write_queue) == 1) {
562 /*
563 * Only one fragment on the socket.
564 */
565 tmp_csum = skb->csum;
566 } else {
567 struct sk_buff *csum_skb = NULL;
568 tmp_csum = 0;
569
570 skb_queue_walk(&sk->sk_write_queue, skb) {
571 tmp_csum = csum_add(tmp_csum, skb->csum);
572
573 if (csum_skb)
574 continue;
575
576 len = skb->len - skb_transport_offset(skb);
577 if (offset >= len) {
578 offset -= len;
579 continue;
580 }
581
582 csum_skb = skb;
583 }
584
585 skb = csum_skb;
586 }
587
588 offset += skb_transport_offset(skb);
589 if (skb_copy_bits(skb, offset, &csum, 2))
590 BUG();
591
592 /* in case cksum was not initialized */
593 if (unlikely(csum))
594 tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
595
596 csum = csum_ipv6_magic(&fl->fl6_src,
597 &fl->fl6_dst,
598 total_len, fl->proto, tmp_csum);
599
600 if (csum == 0 && fl->proto == IPPROTO_UDP)
601 csum = CSUM_MANGLED_0;
602
603 if (skb_store_bits(skb, offset, &csum, 2))
604 BUG();
605
606 send:
607 err = ip6_push_pending_frames(sk);
608 out:
609 return err;
610 }
611
612 static int rawv6_send_hdrinc(struct sock *sk, void *from, int length,
613 struct flowi *fl, struct rt6_info *rt,
614 unsigned int flags)
615 {
616 struct ipv6_pinfo *np = inet6_sk(sk);
617 struct ipv6hdr *iph;
618 struct sk_buff *skb;
619 unsigned int hh_len;
620 int err;
621
622 if (length > rt->u.dst.dev->mtu) {
623 ipv6_local_error(sk, EMSGSIZE, fl, rt->u.dst.dev->mtu);
624 return -EMSGSIZE;
625 }
626 if (flags&MSG_PROBE)
627 goto out;
628
629 hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
630
631 skb = sock_alloc_send_skb(sk, length+hh_len+15,
632 flags&MSG_DONTWAIT, &err);
633 if (skb == NULL)
634 goto error;
635 skb_reserve(skb, hh_len);
636
637 skb->priority = sk->sk_priority;
638 skb->dst = dst_clone(&rt->u.dst);
639
640 skb_put(skb, length);
641 skb_reset_network_header(skb);
642 iph = ipv6_hdr(skb);
643
644 skb->ip_summed = CHECKSUM_NONE;
645
646 skb->transport_header = skb->network_header;
647 err = memcpy_fromiovecend((void *)iph, from, 0, length);
648 if (err)
649 goto error_fault;
650
651 IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTREQUESTS);
652 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
653 dst_output);
654 if (err > 0)
655 err = np->recverr ? net_xmit_errno(err) : 0;
656 if (err)
657 goto error;
658 out:
659 return 0;
660
661 error_fault:
662 err = -EFAULT;
663 kfree_skb(skb);
664 error:
665 IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
666 return err;
667 }
668
669 static int rawv6_probe_proto_opt(struct flowi *fl, struct msghdr *msg)
670 {
671 struct iovec *iov;
672 u8 __user *type = NULL;
673 u8 __user *code = NULL;
674 u8 len = 0;
675 int probed = 0;
676 int i;
677
678 if (!msg->msg_iov)
679 return 0;
680
681 for (i = 0; i < msg->msg_iovlen; i++) {
682 iov = &msg->msg_iov[i];
683 if (!iov)
684 continue;
685
686 switch (fl->proto) {
687 case IPPROTO_ICMPV6:
688 /* check if one-byte field is readable or not. */
689 if (iov->iov_base && iov->iov_len < 1)
690 break;
691
692 if (!type) {
693 type = iov->iov_base;
694 /* check if code field is readable or not. */
695 if (iov->iov_len > 1)
696 code = type + 1;
697 } else if (!code)
698 code = iov->iov_base;
699
700 if (type && code) {
701 if (get_user(fl->fl_icmp_type, type) ||
702 get_user(fl->fl_icmp_code, code))
703 return -EFAULT;
704 probed = 1;
705 }
706 break;
707 case IPPROTO_MH:
708 if (iov->iov_base && iov->iov_len < 1)
709 break;
710 /* check if type field is readable or not. */
711 if (iov->iov_len > 2 - len) {
712 u8 __user *p = iov->iov_base;
713 if (get_user(fl->fl_mh_type, &p[2 - len]))
714 return -EFAULT;
715 probed = 1;
716 } else
717 len += iov->iov_len;
718
719 break;
720 default:
721 probed = 1;
722 break;
723 }
724 if (probed)
725 break;
726 }
727 return 0;
728 }
729
730 static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
731 struct msghdr *msg, size_t len)
732 {
733 struct ipv6_txoptions opt_space;
734 struct sockaddr_in6 * sin6 = (struct sockaddr_in6 *) msg->msg_name;
735 struct in6_addr *daddr, *final_p = NULL, final;
736 struct inet_sock *inet = inet_sk(sk);
737 struct ipv6_pinfo *np = inet6_sk(sk);
738 struct raw6_sock *rp = raw6_sk(sk);
739 struct ipv6_txoptions *opt = NULL;
740 struct ip6_flowlabel *flowlabel = NULL;
741 struct dst_entry *dst = NULL;
742 struct flowi fl;
743 int addr_len = msg->msg_namelen;
744 int hlimit = -1;
745 int tclass = -1;
746 u16 proto;
747 int err;
748
749 /* Rough check on arithmetic overflow,
750 better check is made in ip6_append_data().
751 */
752 if (len > INT_MAX)
753 return -EMSGSIZE;
754
755 /* Mirror BSD error message compatibility */
756 if (msg->msg_flags & MSG_OOB)
757 return -EOPNOTSUPP;
758
759 /*
760 * Get and verify the address.
761 */
762 memset(&fl, 0, sizeof(fl));
763
764 if (sin6) {
765 if (addr_len < SIN6_LEN_RFC2133)
766 return -EINVAL;
767
768 if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
769 return(-EAFNOSUPPORT);
770
771 /* port is the proto value [0..255] carried in nexthdr */
772 proto = ntohs(sin6->sin6_port);
773
774 if (!proto)
775 proto = inet->num;
776 else if (proto != inet->num)
777 return(-EINVAL);
778
779 if (proto > 255)
780 return(-EINVAL);
781
782 daddr = &sin6->sin6_addr;
783 if (np->sndflow) {
784 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
785 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
786 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
787 if (flowlabel == NULL)
788 return -EINVAL;
789 daddr = &flowlabel->dst;
790 }
791 }
792
793 /*
794 * Otherwise it will be difficult to maintain
795 * sk->sk_dst_cache.
796 */
797 if (sk->sk_state == TCP_ESTABLISHED &&
798 ipv6_addr_equal(daddr, &np->daddr))
799 daddr = &np->daddr;
800
801 if (addr_len >= sizeof(struct sockaddr_in6) &&
802 sin6->sin6_scope_id &&
803 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
804 fl.oif = sin6->sin6_scope_id;
805 } else {
806 if (sk->sk_state != TCP_ESTABLISHED)
807 return -EDESTADDRREQ;
808
809 proto = inet->num;
810 daddr = &np->daddr;
811 fl.fl6_flowlabel = np->flow_label;
812 }
813
814 if (ipv6_addr_any(daddr)) {
815 /*
816 * unspecified destination address
817 * treated as error... is this correct ?
818 */
819 fl6_sock_release(flowlabel);
820 return(-EINVAL);
821 }
822
823 if (fl.oif == 0)
824 fl.oif = sk->sk_bound_dev_if;
825
826 if (msg->msg_controllen) {
827 opt = &opt_space;
828 memset(opt, 0, sizeof(struct ipv6_txoptions));
829 opt->tot_len = sizeof(struct ipv6_txoptions);
830
831 err = datagram_send_ctl(msg, &fl, opt, &hlimit, &tclass);
832 if (err < 0) {
833 fl6_sock_release(flowlabel);
834 return err;
835 }
836 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
837 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
838 if (flowlabel == NULL)
839 return -EINVAL;
840 }
841 if (!(opt->opt_nflen|opt->opt_flen))
842 opt = NULL;
843 }
844 if (opt == NULL)
845 opt = np->opt;
846 if (flowlabel)
847 opt = fl6_merge_options(&opt_space, flowlabel, opt);
848 opt = ipv6_fixup_options(&opt_space, opt);
849
850 fl.proto = proto;
851 err = rawv6_probe_proto_opt(&fl, msg);
852 if (err)
853 goto out;
854
855 ipv6_addr_copy(&fl.fl6_dst, daddr);
856 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
857 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
858
859 /* merge ip6_build_xmit from ip6_output */
860 if (opt && opt->srcrt) {
861 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
862 ipv6_addr_copy(&final, &fl.fl6_dst);
863 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
864 final_p = &final;
865 }
866
867 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst))
868 fl.oif = np->mcast_oif;
869 security_sk_classify_flow(sk, &fl);
870
871 err = ip6_dst_lookup(sk, &dst, &fl);
872 if (err)
873 goto out;
874 if (final_p)
875 ipv6_addr_copy(&fl.fl6_dst, final_p);
876
877 if ((err = __xfrm_lookup(&dst, &fl, sk, 1)) < 0) {
878 if (err == -EREMOTE)
879 err = ip6_dst_blackhole(sk, &dst, &fl);
880 if (err < 0)
881 goto out;
882 }
883
884 if (hlimit < 0) {
885 if (ipv6_addr_is_multicast(&fl.fl6_dst))
886 hlimit = np->mcast_hops;
887 else
888 hlimit = np->hop_limit;
889 if (hlimit < 0)
890 hlimit = dst_metric(dst, RTAX_HOPLIMIT);
891 if (hlimit < 0)
892 hlimit = ipv6_get_hoplimit(dst->dev);
893 }
894
895 if (tclass < 0) {
896 tclass = np->tclass;
897 if (tclass < 0)
898 tclass = 0;
899 }
900
901 if (msg->msg_flags&MSG_CONFIRM)
902 goto do_confirm;
903
904 back_from_confirm:
905 if (inet->hdrincl) {
906 err = rawv6_send_hdrinc(sk, msg->msg_iov, len, &fl, (struct rt6_info*)dst, msg->msg_flags);
907 } else {
908 lock_sock(sk);
909 err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov,
910 len, 0, hlimit, tclass, opt, &fl, (struct rt6_info*)dst,
911 msg->msg_flags);
912
913 if (err)
914 ip6_flush_pending_frames(sk);
915 else if (!(msg->msg_flags & MSG_MORE))
916 err = rawv6_push_pending_frames(sk, &fl, rp);
917 release_sock(sk);
918 }
919 done:
920 dst_release(dst);
921 out:
922 fl6_sock_release(flowlabel);
923 return err<0?err:len;
924 do_confirm:
925 dst_confirm(dst);
926 if (!(msg->msg_flags & MSG_PROBE) || len)
927 goto back_from_confirm;
928 err = 0;
929 goto done;
930 }
931
932 static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
933 char __user *optval, int optlen)
934 {
935 switch (optname) {
936 case ICMPV6_FILTER:
937 if (optlen > sizeof(struct icmp6_filter))
938 optlen = sizeof(struct icmp6_filter);
939 if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
940 return -EFAULT;
941 return 0;
942 default:
943 return -ENOPROTOOPT;
944 }
945
946 return 0;
947 }
948
949 static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
950 char __user *optval, int __user *optlen)
951 {
952 int len;
953
954 switch (optname) {
955 case ICMPV6_FILTER:
956 if (get_user(len, optlen))
957 return -EFAULT;
958 if (len < 0)
959 return -EINVAL;
960 if (len > sizeof(struct icmp6_filter))
961 len = sizeof(struct icmp6_filter);
962 if (put_user(len, optlen))
963 return -EFAULT;
964 if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
965 return -EFAULT;
966 return 0;
967 default:
968 return -ENOPROTOOPT;
969 }
970
971 return 0;
972 }
973
974
975 static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
976 char __user *optval, int optlen)
977 {
978 struct raw6_sock *rp = raw6_sk(sk);
979 int val;
980
981 if (get_user(val, (int __user *)optval))
982 return -EFAULT;
983
984 switch (optname) {
985 case IPV6_CHECKSUM:
986 /* You may get strange result with a positive odd offset;
987 RFC2292bis agrees with me. */
988 if (val > 0 && (val&1))
989 return(-EINVAL);
990 if (val < 0) {
991 rp->checksum = 0;
992 } else {
993 rp->checksum = 1;
994 rp->offset = val;
995 }
996
997 return 0;
998 break;
999
1000 default:
1001 return(-ENOPROTOOPT);
1002 }
1003 }
1004
1005 static int rawv6_setsockopt(struct sock *sk, int level, int optname,
1006 char __user *optval, int optlen)
1007 {
1008 switch(level) {
1009 case SOL_RAW:
1010 break;
1011
1012 case SOL_ICMPV6:
1013 if (inet_sk(sk)->num != IPPROTO_ICMPV6)
1014 return -EOPNOTSUPP;
1015 return rawv6_seticmpfilter(sk, level, optname, optval,
1016 optlen);
1017 case SOL_IPV6:
1018 if (optname == IPV6_CHECKSUM)
1019 break;
1020 default:
1021 return ipv6_setsockopt(sk, level, optname, optval,
1022 optlen);
1023 }
1024
1025 return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
1026 }
1027
1028 #ifdef CONFIG_COMPAT
1029 static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname,
1030 char __user *optval, int optlen)
1031 {
1032 switch (level) {
1033 case SOL_RAW:
1034 break;
1035 case SOL_ICMPV6:
1036 if (inet_sk(sk)->num != IPPROTO_ICMPV6)
1037 return -EOPNOTSUPP;
1038 return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
1039 case SOL_IPV6:
1040 if (optname == IPV6_CHECKSUM)
1041 break;
1042 default:
1043 return compat_ipv6_setsockopt(sk, level, optname,
1044 optval, optlen);
1045 }
1046 return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
1047 }
1048 #endif
1049
1050 static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
1051 char __user *optval, int __user *optlen)
1052 {
1053 struct raw6_sock *rp = raw6_sk(sk);
1054 int val, len;
1055
1056 if (get_user(len,optlen))
1057 return -EFAULT;
1058
1059 switch (optname) {
1060 case IPV6_CHECKSUM:
1061 if (rp->checksum == 0)
1062 val = -1;
1063 else
1064 val = rp->offset;
1065 break;
1066
1067 default:
1068 return -ENOPROTOOPT;
1069 }
1070
1071 len = min_t(unsigned int, sizeof(int), len);
1072
1073 if (put_user(len, optlen))
1074 return -EFAULT;
1075 if (copy_to_user(optval,&val,len))
1076 return -EFAULT;
1077 return 0;
1078 }
1079
1080 static int rawv6_getsockopt(struct sock *sk, int level, int optname,
1081 char __user *optval, int __user *optlen)
1082 {
1083 switch(level) {
1084 case SOL_RAW:
1085 break;
1086
1087 case SOL_ICMPV6:
1088 if (inet_sk(sk)->num != IPPROTO_ICMPV6)
1089 return -EOPNOTSUPP;
1090 return rawv6_geticmpfilter(sk, level, optname, optval,
1091 optlen);
1092 case SOL_IPV6:
1093 if (optname == IPV6_CHECKSUM)
1094 break;
1095 default:
1096 return ipv6_getsockopt(sk, level, optname, optval,
1097 optlen);
1098 }
1099
1100 return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
1101 }
1102
1103 #ifdef CONFIG_COMPAT
1104 static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname,
1105 char __user *optval, int __user *optlen)
1106 {
1107 switch (level) {
1108 case SOL_RAW:
1109 break;
1110 case SOL_ICMPV6:
1111 if (inet_sk(sk)->num != IPPROTO_ICMPV6)
1112 return -EOPNOTSUPP;
1113 return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
1114 case SOL_IPV6:
1115 if (optname == IPV6_CHECKSUM)
1116 break;
1117 default:
1118 return compat_ipv6_getsockopt(sk, level, optname,
1119 optval, optlen);
1120 }
1121 return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
1122 }
1123 #endif
1124
1125 static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
1126 {
1127 switch(cmd) {
1128 case SIOCOUTQ:
1129 {
1130 int amount = atomic_read(&sk->sk_wmem_alloc);
1131 return put_user(amount, (int __user *)arg);
1132 }
1133 case SIOCINQ:
1134 {
1135 struct sk_buff *skb;
1136 int amount = 0;
1137
1138 spin_lock_bh(&sk->sk_receive_queue.lock);
1139 skb = skb_peek(&sk->sk_receive_queue);
1140 if (skb != NULL)
1141 amount = skb->tail - skb->transport_header;
1142 spin_unlock_bh(&sk->sk_receive_queue.lock);
1143 return put_user(amount, (int __user *)arg);
1144 }
1145
1146 default:
1147 return -ENOIOCTLCMD;
1148 }
1149 }
1150
1151 static void rawv6_close(struct sock *sk, long timeout)
1152 {
1153 if (inet_sk(sk)->num == IPPROTO_RAW)
1154 ip6_ra_control(sk, -1, NULL);
1155
1156 sk_common_release(sk);
1157 }
1158
1159 static int rawv6_init_sk(struct sock *sk)
1160 {
1161 struct raw6_sock *rp = raw6_sk(sk);
1162
1163 switch (inet_sk(sk)->num) {
1164 case IPPROTO_ICMPV6:
1165 rp->checksum = 1;
1166 rp->offset = 2;
1167 break;
1168 case IPPROTO_MH:
1169 rp->checksum = 1;
1170 rp->offset = 4;
1171 break;
1172 default:
1173 break;
1174 }
1175 return(0);
1176 }
1177
1178 DEFINE_PROTO_INUSE(rawv6)
1179
1180 struct proto rawv6_prot = {
1181 .name = "RAWv6",
1182 .owner = THIS_MODULE,
1183 .close = rawv6_close,
1184 .connect = ip6_datagram_connect,
1185 .disconnect = udp_disconnect,
1186 .ioctl = rawv6_ioctl,
1187 .init = rawv6_init_sk,
1188 .destroy = inet6_destroy_sock,
1189 .setsockopt = rawv6_setsockopt,
1190 .getsockopt = rawv6_getsockopt,
1191 .sendmsg = rawv6_sendmsg,
1192 .recvmsg = rawv6_recvmsg,
1193 .bind = rawv6_bind,
1194 .backlog_rcv = rawv6_rcv_skb,
1195 .hash = raw_v6_hash,
1196 .unhash = raw_v6_unhash,
1197 .obj_size = sizeof(struct raw6_sock),
1198 #ifdef CONFIG_COMPAT
1199 .compat_setsockopt = compat_rawv6_setsockopt,
1200 .compat_getsockopt = compat_rawv6_getsockopt,
1201 #endif
1202 REF_PROTO_INUSE(rawv6)
1203 };
1204
1205 #ifdef CONFIG_PROC_FS
1206 struct raw6_iter_state {
1207 int bucket;
1208 };
1209
1210 #define raw6_seq_private(seq) ((struct raw6_iter_state *)(seq)->private)
1211
1212 static struct sock *raw6_get_first(struct seq_file *seq)
1213 {
1214 struct sock *sk;
1215 struct hlist_node *node;
1216 struct raw6_iter_state* state = raw6_seq_private(seq);
1217
1218 for (state->bucket = 0; state->bucket < RAW_HTABLE_SIZE;
1219 ++state->bucket)
1220 sk_for_each(sk, node, &raw_v6_hashinfo.ht[state->bucket])
1221 if (sk->sk_family == PF_INET6)
1222 goto out;
1223 sk = NULL;
1224 out:
1225 return sk;
1226 }
1227
1228 static struct sock *raw6_get_next(struct seq_file *seq, struct sock *sk)
1229 {
1230 struct raw6_iter_state* state = raw6_seq_private(seq);
1231
1232 do {
1233 sk = sk_next(sk);
1234 try_again:
1235 ;
1236 } while (sk && sk->sk_family != PF_INET6);
1237
1238 if (!sk && ++state->bucket < RAW_HTABLE_SIZE) {
1239 sk = sk_head(&raw_v6_hashinfo.ht[state->bucket]);
1240 goto try_again;
1241 }
1242 return sk;
1243 }
1244
1245 static struct sock *raw6_get_idx(struct seq_file *seq, loff_t pos)
1246 {
1247 struct sock *sk = raw6_get_first(seq);
1248 if (sk)
1249 while (pos && (sk = raw6_get_next(seq, sk)) != NULL)
1250 --pos;
1251 return pos ? NULL : sk;
1252 }
1253
1254 static void *raw6_seq_start(struct seq_file *seq, loff_t *pos)
1255 {
1256 read_lock(&raw_v6_hashinfo.lock);
1257 return *pos ? raw6_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
1258 }
1259
1260 static void *raw6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1261 {
1262 struct sock *sk;
1263
1264 if (v == SEQ_START_TOKEN)
1265 sk = raw6_get_first(seq);
1266 else
1267 sk = raw6_get_next(seq, v);
1268 ++*pos;
1269 return sk;
1270 }
1271
1272 static void raw6_seq_stop(struct seq_file *seq, void *v)
1273 {
1274 read_unlock(&raw_v6_hashinfo.lock);
1275 }
1276
1277 static void raw6_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
1278 {
1279 struct ipv6_pinfo *np = inet6_sk(sp);
1280 struct in6_addr *dest, *src;
1281 __u16 destp, srcp;
1282
1283 dest = &np->daddr;
1284 src = &np->rcv_saddr;
1285 destp = 0;
1286 srcp = inet_sk(sp)->num;
1287 seq_printf(seq,
1288 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1289 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
1290 i,
1291 src->s6_addr32[0], src->s6_addr32[1],
1292 src->s6_addr32[2], src->s6_addr32[3], srcp,
1293 dest->s6_addr32[0], dest->s6_addr32[1],
1294 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1295 sp->sk_state,
1296 atomic_read(&sp->sk_wmem_alloc),
1297 atomic_read(&sp->sk_rmem_alloc),
1298 0, 0L, 0,
1299 sock_i_uid(sp), 0,
1300 sock_i_ino(sp),
1301 atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
1302 }
1303
1304 static int raw6_seq_show(struct seq_file *seq, void *v)
1305 {
1306 if (v == SEQ_START_TOKEN)
1307 seq_printf(seq,
1308 " sl "
1309 "local_address "
1310 "remote_address "
1311 "st tx_queue rx_queue tr tm->when retrnsmt"
1312 " uid timeout inode drops\n");
1313 else
1314 raw6_sock_seq_show(seq, v, raw6_seq_private(seq)->bucket);
1315 return 0;
1316 }
1317
1318 static const struct seq_operations raw6_seq_ops = {
1319 .start = raw6_seq_start,
1320 .next = raw6_seq_next,
1321 .stop = raw6_seq_stop,
1322 .show = raw6_seq_show,
1323 };
1324
1325 static int raw6_seq_open(struct inode *inode, struct file *file)
1326 {
1327 return seq_open_private(file, &raw6_seq_ops,
1328 sizeof(struct raw6_iter_state));
1329 }
1330
1331 static const struct file_operations raw6_seq_fops = {
1332 .owner = THIS_MODULE,
1333 .open = raw6_seq_open,
1334 .read = seq_read,
1335 .llseek = seq_lseek,
1336 .release = seq_release_private,
1337 };
1338
1339 int __init raw6_proc_init(void)
1340 {
1341 if (!proc_net_fops_create(&init_net, "raw6", S_IRUGO, &raw6_seq_fops))
1342 return -ENOMEM;
1343 return 0;
1344 }
1345
1346 void raw6_proc_exit(void)
1347 {
1348 proc_net_remove(&init_net, "raw6");
1349 }
1350 #endif /* CONFIG_PROC_FS */
This page took 0.093098 seconds and 6 git commands to generate.