~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/raw.c

Version: ~ [ linux-5.8 ] ~ [ linux-5.7.12 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.55 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.136 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.191 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.232 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.232 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/sched.h>
 28 #include <linux/net.h>
 29 #include <linux/in6.h>
 30 #include <linux/netdevice.h>
 31 #include <linux/if_arp.h>
 32 #include <linux/icmpv6.h>
 33 #include <linux/netfilter.h>
 34 #include <linux/netfilter_ipv6.h>
 35 #include <asm/uaccess.h>
 36 #include <asm/ioctls.h>
 37 
 38 #include <net/sock.h>
 39 #include <net/snmp.h>
 40 
 41 #include <net/ipv6.h>
 42 #include <net/ndisc.h>
 43 #include <net/protocol.h>
 44 #include <net/ip6_route.h>
 45 #include <net/addrconf.h>
 46 #include <net/transp_v6.h>
 47 #include <net/udp.h>
 48 #include <net/inet_common.h>
 49 
 50 #include <net/rawv6.h>
 51 #include <net/xfrm.h>
 52 
 53 #include <linux/proc_fs.h>
 54 #include <linux/seq_file.h>
 55 
 56 struct hlist_head raw_v6_htable[RAWV6_HTABLE_SIZE];
 57 rwlock_t raw_v6_lock = RW_LOCK_UNLOCKED;
 58 
 59 static void raw_v6_hash(struct sock *sk)
 60 {
 61         struct hlist_head *list = &raw_v6_htable[inet_sk(sk)->num &
 62                                                  (RAWV6_HTABLE_SIZE - 1)];
 63 
 64         write_lock_bh(&raw_v6_lock);
 65         sk_add_node(sk, list);
 66         sock_prot_inc_use(sk->sk_prot);
 67         write_unlock_bh(&raw_v6_lock);
 68 }
 69 
 70 static void raw_v6_unhash(struct sock *sk)
 71 {
 72         write_lock_bh(&raw_v6_lock);
 73         if (sk_del_node_init(sk))
 74                 sock_prot_dec_use(sk->sk_prot);
 75         write_unlock_bh(&raw_v6_lock);
 76 }
 77 
 78 
 79 /* Grumble... icmp and ip_input want to get at this... */
 80 struct sock *__raw_v6_lookup(struct sock *sk, unsigned short num,
 81                              struct in6_addr *loc_addr, struct in6_addr *rmt_addr)
 82 {
 83         struct hlist_node *node;
 84         int addr_type = ipv6_addr_type(loc_addr);
 85 
 86         sk_for_each_from(sk, node)
 87                 if (inet_sk(sk)->num == num) {
 88                         struct ipv6_pinfo *np = inet6_sk(sk);
 89 
 90                         if (!ipv6_addr_any(&np->daddr) &&
 91                             ipv6_addr_cmp(&np->daddr, rmt_addr))
 92                                 continue;
 93 
 94                         if (!ipv6_addr_any(&np->rcv_saddr)) {
 95                                 if (!ipv6_addr_cmp(&np->rcv_saddr, loc_addr))
 96                                         goto found;
 97                                 if ((addr_type & IPV6_ADDR_MULTICAST) &&
 98                                     inet6_mc_check(sk, loc_addr, rmt_addr))
 99                                         goto found;
100                                 continue;
101                         }
102                         goto found;
103                 }
104         sk = NULL;
105 found:
106         return sk;
107 }
108 
109 /*
110  *      0 - deliver
111  *      1 - block
112  */
113 static __inline__ int icmpv6_filter(struct sock *sk, struct sk_buff *skb)
114 {
115         struct icmp6hdr *icmph;
116         struct raw6_opt *opt = raw6_sk(sk);
117 
118         if (pskb_may_pull(skb, sizeof(struct icmp6hdr))) {
119                 __u32 *data = &opt->filter.data[0];
120                 int bit_nr;
121 
122                 icmph = (struct icmp6hdr *) skb->data;
123                 bit_nr = icmph->icmp6_type;
124 
125                 return (data[bit_nr >> 5] & (1 << (bit_nr & 31))) != 0;
126         }
127         return 0;
128 }
129 
130 /*
131  *      demultiplex raw sockets.
132  *      (should consider queueing the skb in the sock receive_queue
133  *      without calling rawv6.c)
134  *
135  *      Caller owns SKB so we must make clones.
136  */
137 void ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
138 {
139         struct in6_addr *saddr;
140         struct in6_addr *daddr;
141         struct sock *sk;
142         __u8 hash;
143 
144         saddr = &skb->nh.ipv6h->saddr;
145         daddr = saddr + 1;
146 
147         hash = nexthdr & (MAX_INET_PROTOS - 1);
148 
149         read_lock(&raw_v6_lock);
150         sk = sk_head(&raw_v6_htable[hash]);
151 
152         /*
153          *      The first socket found will be delivered after
154          *      delivery to transport protocols.
155          */
156 
157         if (sk == NULL)
158                 goto out;
159 
160         sk = __raw_v6_lookup(sk, nexthdr, daddr, saddr);
161 
162         while (sk) {
163                 if (nexthdr != IPPROTO_ICMPV6 || !icmpv6_filter(sk, skb)) {
164                         struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
165 
166                         /* Not releasing hash table! */
167                         if (clone)
168                                 rawv6_rcv(sk, clone);
169                 }
170                 sk = __raw_v6_lookup(sk_next(sk), nexthdr, daddr, saddr);
171         }
172 out:
173         read_unlock(&raw_v6_lock);
174 }
175 
176 /* This cleans up af_inet6 a bit. -DaveM */
177 static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
178 {
179         struct inet_opt *inet = inet_sk(sk);
180         struct ipv6_pinfo *np = inet6_sk(sk);
181         struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
182         __u32 v4addr = 0;
183         int addr_type;
184         int err;
185 
186         if (addr_len < SIN6_LEN_RFC2133)
187                 return -EINVAL;
188         addr_type = ipv6_addr_type(&addr->sin6_addr);
189 
190         /* Raw sockets are IPv6 only */
191         if (addr_type == IPV6_ADDR_MAPPED)
192                 return(-EADDRNOTAVAIL);
193 
194         lock_sock(sk);
195 
196         err = -EINVAL;
197         if (sk->sk_state != TCP_CLOSE)
198                 goto out;
199 
200         if (addr_type & IPV6_ADDR_LINKLOCAL) {
201                 if (addr_len >= sizeof(struct sockaddr_in6) &&
202                     addr->sin6_scope_id) {
203                         /* Override any existing binding, if another one
204                          * is supplied by user.
205                          */
206                         sk->sk_bound_dev_if = addr->sin6_scope_id;
207                 }
208 
209                 /* Binding to link-local address requires an interface */
210                 if (!sk->sk_bound_dev_if)
211                         goto out;
212         }
213 
214         /* Check if the address belongs to the host. */
215         if (addr_type != IPV6_ADDR_ANY) {
216                 /* ipv4 addr of the socket is invalid.  Only the
217                  * unpecified and mapped address have a v4 equivalent.
218                  */
219                 v4addr = LOOPBACK4_IPV6;
220                 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
221                         err = -EADDRNOTAVAIL;
222                         if (!ipv6_chk_addr(&addr->sin6_addr, NULL))
223                                 goto out;
224                 }
225         }
226 
227         inet->rcv_saddr = inet->saddr = v4addr;
228         ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
229         if (!(addr_type & IPV6_ADDR_MULTICAST))
230                 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
231         err = 0;
232 out:
233         release_sock(sk);
234         return err;
235 }
236 
237 void rawv6_err(struct sock *sk, struct sk_buff *skb,
238                struct inet6_skb_parm *opt,
239                int type, int code, int offset, u32 info)
240 {
241         struct inet_opt *inet = inet_sk(sk);
242         struct ipv6_pinfo *np = inet6_sk(sk);
243         int err;
244         int harderr;
245 
246         /* Report error on raw socket, if:
247            1. User requested recverr.
248            2. Socket is connected (otherwise the error indication
249               is useless without recverr and error is hard.
250          */
251         if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
252                 return;
253 
254         harderr = icmpv6_err_convert(type, code, &err);
255         if (type == ICMPV6_PKT_TOOBIG)
256                 harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
257 
258         if (np->recverr) {
259                 u8 *payload = skb->data;
260                 if (!inet->hdrincl)
261                         payload += offset;
262                 ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
263         }
264 
265         if (np->recverr || harderr) {
266                 sk->sk_err = err;
267                 sk->sk_error_report(sk);
268         }
269 }
270 
271 static inline int rawv6_rcv_skb(struct sock * sk, struct sk_buff * skb)
272 {
273         if (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
274                 if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum))) {
275                         /* FIXME: increment a raw6 drops counter here */
276                         kfree_skb(skb);
277                         return 0;
278                 }
279                 skb->ip_summed = CHECKSUM_UNNECESSARY;
280         }
281 
282         /* Charge it to the socket. */
283         if (sock_queue_rcv_skb(sk,skb)<0) {
284                 /* FIXME: increment a raw6 drops counter here */
285                 kfree_skb(skb);
286                 return 0;
287         }
288 
289         return 0;
290 }
291 
292 /*
293  *      This is next to useless... 
294  *      if we demultiplex in network layer we don't need the extra call
295  *      just to queue the skb... 
296  *      maybe we could have the network decide uppon a hint if it 
297  *      should call raw_rcv for demultiplexing
298  */
299 int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
300 {
301         struct inet_opt *inet = inet_sk(sk);
302         struct raw6_opt *raw_opt = raw6_sk(sk);
303 
304         if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
305                 kfree_skb(skb);
306                 return NET_RX_DROP;
307         }
308 
309         if (!raw_opt->checksum)
310                 skb->ip_summed = CHECKSUM_UNNECESSARY;
311 
312         if (skb->ip_summed != CHECKSUM_UNNECESSARY) {
313                 if (skb->ip_summed == CHECKSUM_HW) {
314                         skb->ip_summed = CHECKSUM_UNNECESSARY;
315                         if (csum_ipv6_magic(&skb->nh.ipv6h->saddr,
316                                             &skb->nh.ipv6h->daddr,
317                                             skb->len, inet->num, skb->csum)) {
318                                 NETDEBUG(if (net_ratelimit()) printk(KERN_DEBUG "raw v6 hw csum failure.\n"));
319                                 skb->ip_summed = CHECKSUM_NONE;
320                         }
321                 }
322                 if (skb->ip_summed == CHECKSUM_NONE)
323                         skb->csum = ~csum_ipv6_magic(&skb->nh.ipv6h->saddr,
324                                                      &skb->nh.ipv6h->daddr,
325                                                      skb->len, inet->num, 0);
326         }
327 
328         if (inet->hdrincl) {
329                 if (skb->ip_summed != CHECKSUM_UNNECESSARY &&
330                     (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum))) {
331                         /* FIXME: increment a raw6 drops counter here */
332                         kfree_skb(skb);
333                         return 0;
334                 }
335                 skb->ip_summed = CHECKSUM_UNNECESSARY;
336         }
337 
338         rawv6_rcv_skb(sk, skb);
339         return 0;
340 }
341 
342 
343 /*
344  *      This should be easy, if there is something there
345  *      we return it, otherwise we block.
346  */
347 
348 static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, int len,
349                   int noblock, int flags, int *addr_len)
350 {
351         struct ipv6_pinfo *np = inet6_sk(sk);
352         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)msg->msg_name;
353         struct sk_buff *skb;
354         int copied, err;
355 
356         if (flags & MSG_OOB)
357                 return -EOPNOTSUPP;
358                 
359         if (addr_len) 
360                 *addr_len=sizeof(*sin6);
361 
362         if (flags & MSG_ERRQUEUE)
363                 return ipv6_recv_error(sk, msg, len);
364 
365         skb = skb_recv_datagram(sk, flags, noblock, &err);
366         if (!skb)
367                 goto out;
368 
369         copied = skb->len;
370         if (copied > len) {
371                 copied = len;
372                 msg->msg_flags |= MSG_TRUNC;
373         }
374 
375         if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
376                 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
377         } else if (msg->msg_flags&MSG_TRUNC) {
378                 if ((unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum)))
379                         goto csum_copy_err;
380                 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
381         } else {
382                 err = skb_copy_and_csum_datagram_iovec(skb, 0, msg->msg_iov);
383                 if (err == -EINVAL)
384                         goto csum_copy_err;
385         }
386         if (err)
387                 goto out_free;
388 
389         /* Copy the address. */
390         if (sin6) {
391                 sin6->sin6_family = AF_INET6;
392                 ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
393                 sin6->sin6_flowinfo = 0;
394                 sin6->sin6_scope_id = 0;
395                 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) {
396                         struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
397                         sin6->sin6_scope_id = opt->iif;
398                 }
399         }
400 
401         sock_recv_timestamp(msg, sk, skb);
402 
403         if (np->rxopt.all)
404                 datagram_recv_ctl(sk, msg, skb);
405         err = copied;
406 
407 out_free:
408         skb_free_datagram(sk, skb);
409 out:
410         return err;
411 
412 csum_copy_err:
413         /* Clear queue. */
414         if (flags&MSG_PEEK) {
415                 int clear = 0;
416                 spin_lock_irq(&sk->sk_receive_queue.lock);
417                 if (skb == skb_peek(&sk->sk_receive_queue)) {
418                         __skb_unlink(skb, &sk->sk_receive_queue);
419                         clear = 1;
420                 }
421                 spin_unlock_irq(&sk->sk_receive_queue.lock);
422                 if (clear)
423                         kfree_skb(skb);
424         }
425 
426         /* Error for blocking case is chosen to masquerade
427            as some normal condition.
428          */
429         err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
430         /* FIXME: increment a raw6 drops counter here */
431         goto out_free;
432 }
433 
434 static int rawv6_push_pending_frames(struct sock *sk, struct flowi *fl, struct raw6_opt *opt, int len)
435 {
436         struct sk_buff *skb;
437         int err = 0;
438         u16 *csum;
439 
440         if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
441                 goto out;
442 
443         if (opt->offset + 1 < len)
444                 csum = (u16 *)(skb->h.raw + opt->offset);
445         else {
446                 err = -EINVAL;
447                 goto out;
448         }
449 
450         if (skb_queue_len(&sk->sk_write_queue) == 1) {
451                 /*
452                  * Only one fragment on the socket.
453                  */
454                 /* should be check HW csum miyazawa */
455                 *csum = csum_ipv6_magic(&fl->fl6_src,
456                                         &fl->fl6_dst,
457                                         len, fl->proto, skb->csum);
458         } else {
459                 u32 tmp_csum = 0;
460 
461                 skb_queue_walk(&sk->sk_write_queue, skb) {
462                         tmp_csum = csum_add(tmp_csum, skb->csum);
463                 }
464 
465                 tmp_csum = csum_ipv6_magic(&fl->fl6_src,
466                                            &fl->fl6_dst,
467                                            len, fl->proto, tmp_csum);
468                 *csum = tmp_csum;
469         }
470         if (*csum == 0)
471                 *csum = -1;
472         ip6_push_pending_frames(sk);
473 out:
474         return err;
475 }
476 
477 static int rawv6_send_hdrinc(struct sock *sk, void *from, int length,
478                         struct flowi *fl, struct rt6_info *rt, 
479                         unsigned int flags)
480 {
481         struct inet_opt *inet = inet_sk(sk);
482         struct ipv6hdr *iph;
483         struct sk_buff *skb;
484         unsigned int hh_len;
485         int err;
486 
487         if (length > rt->u.dst.dev->mtu) {
488                 ipv6_local_error(sk, EMSGSIZE, fl, rt->u.dst.dev->mtu);
489                 return -EMSGSIZE;
490         }
491         if (flags&MSG_PROBE)
492                 goto out;
493 
494         hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
495 
496         skb = sock_alloc_send_skb(sk, length+hh_len+15,
497                                   flags&MSG_DONTWAIT, &err);
498         if (skb == NULL)
499                 goto error; 
500         skb_reserve(skb, hh_len);
501 
502         skb->priority = sk->sk_priority;
503         skb->dst = dst_clone(&rt->u.dst);
504 
505         skb->nh.ipv6h = iph = (struct ipv6hdr *)skb_put(skb, length);
506 
507         skb->ip_summed = CHECKSUM_NONE;
508 
509         skb->h.raw = skb->nh.raw;
510         err = memcpy_fromiovecend((void *)iph, from, 0, length);
511         if (err)
512                 goto error_fault;
513 
514         err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
515                       dst_output);
516         if (err > 0)
517                 err = inet->recverr ? net_xmit_errno(err) : 0;
518         if (err)
519                 goto error;
520 out:
521         return 0;
522 
523 error_fault:
524         err = -EFAULT;
525         kfree_skb(skb);
526 error:
527         IP6_INC_STATS(Ip6OutDiscards);
528         return err; 
529 }
530 static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, int len)
531 {
532         struct ipv6_txoptions opt_space;
533         struct sockaddr_in6 * sin6 = (struct sockaddr_in6 *) msg->msg_name;
534         struct in6_addr *daddr;
535         struct inet_opt *inet = inet_sk(sk);
536         struct ipv6_pinfo *np = inet6_sk(sk);
537         struct raw6_opt *raw_opt = raw6_sk(sk);
538         struct ipv6_txoptions *opt = NULL;
539         struct ip6_flowlabel *flowlabel = NULL;
540         struct dst_entry *dst = NULL;
541         struct flowi fl;
542         int addr_len = msg->msg_namelen;
543         int hlimit = -1;
544         u16 proto;
545         int err;
546 
547         /* Rough check on arithmetic overflow,
548            better check is made in ip6_build_xmit
549          */
550         if (len < 0)
551                 return -EMSGSIZE;
552 
553         /* Mirror BSD error message compatibility */
554         if (msg->msg_flags & MSG_OOB)           
555                 return -EOPNOTSUPP;
556 
557         /*
558          *      Get and verify the address. 
559          */
560         memset(&fl, 0, sizeof(fl));
561 
562         if (sin6) {
563                 if (addr_len < SIN6_LEN_RFC2133) 
564                         return -EINVAL;
565 
566                 if (sin6->sin6_family && sin6->sin6_family != AF_INET6) 
567                         return(-EINVAL);
568 
569                 /* port is the proto value [0..255] carried in nexthdr */
570                 proto = ntohs(sin6->sin6_port);
571 
572                 if (!proto)
573                         proto = inet->num;
574                 else if (proto != inet->num)
575                         return(-EINVAL);
576 
577                 if (proto > 255)
578                         return(-EINVAL);
579 
580                 daddr = &sin6->sin6_addr;
581                 if (np->sndflow) {
582                         fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
583                         if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
584                                 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
585                                 if (flowlabel == NULL)
586                                         return -EINVAL;
587                                 daddr = &flowlabel->dst;
588                         }
589                 }
590 
591                 /*
592                  * Otherwise it will be difficult to maintain
593                  * sk->sk_dst_cache.
594                  */
595                 if (sk->sk_state == TCP_ESTABLISHED &&
596                     !ipv6_addr_cmp(daddr, &np->daddr))
597                         daddr = &np->daddr;
598 
599                 if (addr_len >= sizeof(struct sockaddr_in6) &&
600                     sin6->sin6_scope_id &&
601                     ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
602                         fl.oif = sin6->sin6_scope_id;
603         } else {
604                 if (sk->sk_state != TCP_ESTABLISHED) 
605                         return -EDESTADDRREQ;
606                 
607                 proto = inet->num;
608                 daddr = &np->daddr;
609                 fl.fl6_flowlabel = np->flow_label;
610         }
611 
612         if (ipv6_addr_any(daddr)) {
613                 /* 
614                  * unspecfied destination address 
615                  * treated as error... is this correct ?
616                  */
617                 fl6_sock_release(flowlabel);
618                 return(-EINVAL);
619         }
620 
621         if (fl.oif == 0)
622                 fl.oif = sk->sk_bound_dev_if;
623 
624         if (msg->msg_controllen) {
625                 opt = &opt_space;
626                 memset(opt, 0, sizeof(struct ipv6_txoptions));
627                 opt->tot_len = sizeof(struct ipv6_txoptions);
628 
629                 err = datagram_send_ctl(msg, &fl, opt, &hlimit);
630                 if (err < 0) {
631                         fl6_sock_release(flowlabel);
632                         return err;
633                 }
634                 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
635                         flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
636                         if (flowlabel == NULL)
637                                 return -EINVAL;
638                 }
639                 if (!(opt->opt_nflen|opt->opt_flen))
640                         opt = NULL;
641         }
642         if (opt == NULL)
643                 opt = np->opt;
644         if (flowlabel)
645                 opt = fl6_merge_options(&opt_space, flowlabel, opt);
646 
647         fl.proto = proto;
648         ipv6_addr_copy(&fl.fl6_dst, daddr);
649         if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
650                 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
651 
652         /* merge ip6_build_xmit from ip6_output */
653         if (opt && opt->srcrt) {
654                 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
655                 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
656         }
657 
658         if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst))
659                 fl.oif = np->mcast_oif;
660 
661         err = ip6_dst_lookup(sk, &dst, &fl);
662         if (err)
663                 goto out;
664 
665         if (hlimit < 0) {
666                 if (ipv6_addr_is_multicast(&fl.fl6_dst))
667                         hlimit = np->mcast_hops;
668                 else
669                         hlimit = np->hop_limit;
670                 if (hlimit < 0)
671                         hlimit = dst_metric(dst, RTAX_HOPLIMIT);
672         }
673 
674         if (msg->msg_flags&MSG_CONFIRM)
675                 goto do_confirm;
676 
677 back_from_confirm:
678         if (inet->hdrincl) {
679                 err = rawv6_send_hdrinc(sk, msg->msg_iov, len, &fl, (struct rt6_info*)dst, msg->msg_flags);
680         } else {
681                 lock_sock(sk);
682                 err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov, len, 0,
683                                         hlimit, opt, &fl, (struct rt6_info*)dst, msg->msg_flags);
684 
685                 if (err)
686                         ip6_flush_pending_frames(sk);
687                 else if (!(msg->msg_flags & MSG_MORE)) {
688                         if (raw_opt->checksum) {
689                                 err = rawv6_push_pending_frames(sk, &fl, raw_opt, len);
690                         } else {
691                                 err = ip6_push_pending_frames(sk);
692                         }
693                 }
694         }
695 done:
696         ip6_dst_store(sk, dst,
697                       !ipv6_addr_cmp(&fl.fl6_dst, &np->daddr) ?
698                       &np->daddr : NULL);
699         if (err > 0)
700                 err = np->recverr ? net_xmit_errno(err) : 0;
701 
702         release_sock(sk);
703 out:    
704         fl6_sock_release(flowlabel);
705         return err<0?err:len;
706 do_confirm:
707         dst_confirm(dst);
708         if (!(msg->msg_flags & MSG_PROBE) || len)
709                 goto back_from_confirm;
710         err = 0;
711         goto done;
712 }
713 
714 static int rawv6_seticmpfilter(struct sock *sk, int level, int optname, 
715                                char *optval, int optlen)
716 {
717         switch (optname) {
718         case ICMPV6_FILTER:
719                 if (optlen > sizeof(struct icmp6_filter))
720                         optlen = sizeof(struct icmp6_filter);
721                 if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
722                         return -EFAULT;
723                 return 0;
724         default:
725                 return -ENOPROTOOPT;
726         };
727 
728         return 0;
729 }
730 
731 static int rawv6_geticmpfilter(struct sock *sk, int level, int optname, 
732                                char *optval, int *optlen)
733 {
734         int len;
735 
736         switch (optname) {
737         case ICMPV6_FILTER:
738                 if (get_user(len, optlen))
739                         return -EFAULT;
740                 if (len < 0)
741                         return -EINVAL;
742                 if (len > sizeof(struct icmp6_filter))
743                         len = sizeof(struct icmp6_filter);
744                 if (put_user(len, optlen))
745                         return -EFAULT;
746                 if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
747                         return -EFAULT;
748                 return 0;
749         default:
750                 return -ENOPROTOOPT;
751         };
752 
753         return 0;
754 }
755 
756 
757 static int rawv6_setsockopt(struct sock *sk, int level, int optname, 
758                             char *optval, int optlen)
759 {
760         struct raw6_opt *opt = raw6_sk(sk);
761         int val;
762 
763         switch(level) {
764                 case SOL_RAW:
765                         break;
766 
767                 case SOL_ICMPV6:
768                         if (inet_sk(sk)->num != IPPROTO_ICMPV6)
769                                 return -EOPNOTSUPP;
770                         return rawv6_seticmpfilter(sk, level, optname, optval,
771                                                    optlen);
772                 case SOL_IPV6:
773                         if (optname == IPV6_CHECKSUM)
774                                 break;
775                 default:
776                         return ipv6_setsockopt(sk, level, optname, optval,
777                                                optlen);
778         };
779 
780         if (get_user(val, (int *)optval))
781                 return -EFAULT;
782 
783         switch (optname) {
784                 case IPV6_CHECKSUM:
785                         /* You may get strange result with a positive odd offset;
786                            RFC2292bis agrees with me. */
787                         if (val > 0 && (val&1))
788                                 return(-EINVAL);
789                         if (val < 0) {
790                                 opt->checksum = 0;
791                         } else {
792                                 opt->checksum = 1;
793                                 opt->offset = val;
794                         }
795 
796                         return 0;
797                         break;
798 
799                 default:
800                         return(-ENOPROTOOPT);
801         }
802 }
803 
804 static int rawv6_getsockopt(struct sock *sk, int level, int optname, 
805                             char *optval, int *optlen)
806 {
807         struct raw6_opt *opt = raw6_sk(sk);
808         int val, len;
809 
810         switch(level) {
811                 case SOL_RAW:
812                         break;
813 
814                 case SOL_ICMPV6:
815                         if (inet_sk(sk)->num != IPPROTO_ICMPV6)
816                                 return -EOPNOTSUPP;
817                         return rawv6_geticmpfilter(sk, level, optname, optval,
818                                                    optlen);
819                 case SOL_IPV6:
820                         if (optname == IPV6_CHECKSUM)
821                                 break;
822                 default:
823                         return ipv6_getsockopt(sk, level, optname, optval,
824                                                optlen);
825         };
826 
827         if (get_user(len,optlen))
828                 return -EFAULT;
829 
830         switch (optname) {
831         case IPV6_CHECKSUM:
832                 if (opt->checksum == 0)
833                         val = -1;
834                 else
835                         val = opt->offset;
836                 break;
837 
838         default:
839                 return -ENOPROTOOPT;
840         }
841 
842         len = min_t(unsigned int, sizeof(int), len);
843 
844         if (put_user(len, optlen))
845                 return -EFAULT;
846         if (copy_to_user(optval,&val,len))
847                 return -EFAULT;
848         return 0;
849 }
850 
851 static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
852 {
853         switch(cmd) {
854                 case SIOCOUTQ:
855                 {
856                         int amount = atomic_read(&sk->sk_wmem_alloc);
857                         return put_user(amount, (int *)arg);
858                 }
859                 case SIOCINQ:
860                 {
861                         struct sk_buff *skb;
862                         int amount = 0;
863 
864                         spin_lock_irq(&sk->sk_receive_queue.lock);
865                         skb = skb_peek(&sk->sk_receive_queue);
866                         if (skb != NULL)
867                                 amount = skb->tail - skb->h.raw;
868                         spin_unlock_irq(&sk->sk_receive_queue.lock);
869                         return put_user(amount, (int *)arg);
870                 }
871 
872                 default:
873                         return -ENOIOCTLCMD;
874         }
875 }
876 
877 static void rawv6_close(struct sock *sk, long timeout)
878 {
879         if (inet_sk(sk)->num == IPPROTO_RAW)
880                 ip6_ra_control(sk, -1, NULL);
881 
882         inet_sock_release(sk);
883 }
884 
885 static int rawv6_init_sk(struct sock *sk)
886 {
887         if (inet_sk(sk)->num == IPPROTO_ICMPV6) {
888                 struct raw6_opt *opt = raw6_sk(sk);
889                 opt->checksum = 1;
890                 opt->offset = 2;
891         }
892         return(0);
893 }
894 
895 struct proto rawv6_prot = {
896         .name =         "RAW",
897         .close =        rawv6_close,
898         .connect =      udpv6_connect,
899         .disconnect =   udp_disconnect,
900         .ioctl =        rawv6_ioctl,
901         .init =         rawv6_init_sk,
902         .destroy =      inet6_destroy_sock,
903         .setsockopt =   rawv6_setsockopt,
904         .getsockopt =   rawv6_getsockopt,
905         .sendmsg =      rawv6_sendmsg,
906         .recvmsg =      rawv6_recvmsg,
907         .bind =         rawv6_bind,
908         .backlog_rcv =  rawv6_rcv_skb,
909         .hash =         raw_v6_hash,
910         .unhash =       raw_v6_unhash,
911 };
912 
913 #ifdef CONFIG_PROC_FS
914 struct raw6_iter_state {
915         int bucket;
916 };
917 
918 #define raw6_seq_private(seq) ((struct raw6_iter_state *)(seq)->private)
919 
920 static struct sock *raw6_get_first(struct seq_file *seq)
921 {
922         struct sock *sk;
923         struct hlist_node *node;
924         struct raw6_iter_state* state = raw6_seq_private(seq);
925 
926         for (state->bucket = 0; state->bucket < RAWV6_HTABLE_SIZE; ++state->bucket)
927                 sk_for_each(sk, node, &raw_v6_htable[state->bucket])
928                         if (sk->sk_family == PF_INET6)
929                                 goto out;
930         sk = NULL;
931 out:
932         return sk;
933 }
934 
935 static struct sock *raw6_get_next(struct seq_file *seq, struct sock *sk)
936 {
937         struct raw6_iter_state* state = raw6_seq_private(seq);
938 
939         do {
940                 sk = sk_next(sk);
941 try_again:
942                 ;
943         } while (sk && sk->sk_family != PF_INET6);
944 
945         if (!sk && ++state->bucket < RAWV6_HTABLE_SIZE) {
946                 sk = sk_head(&raw_v6_htable[state->bucket]);
947                 goto try_again;
948         }
949         return sk;
950 }
951 
952 static struct sock *raw6_get_idx(struct seq_file *seq, loff_t pos)
953 {
954         struct sock *sk = raw6_get_first(seq);
955         if (sk)
956                 while (pos && (sk = raw6_get_next(seq, sk)) != NULL)
957                         --pos;
958         return pos ? NULL : sk;
959 }
960 
961 static void *raw6_seq_start(struct seq_file *seq, loff_t *pos)
962 {
963         read_lock(&raw_v6_lock);
964         return *pos ? raw6_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
965 }
966 
967 static void *raw6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
968 {
969         struct sock *sk;
970 
971         if (v == SEQ_START_TOKEN)
972                 sk = raw6_get_first(seq);
973         else
974                 sk = raw6_get_next(seq, v);
975         ++*pos;
976         return sk;
977 }
978 
979 static void raw6_seq_stop(struct seq_file *seq, void *v)
980 {
981         read_unlock(&raw_v6_lock);
982 }
983 
984 static void raw6_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
985 {
986         struct ipv6_pinfo *np = inet6_sk(sp);
987         struct in6_addr *dest, *src;
988         __u16 destp, srcp;
989 
990         dest  = &np->daddr;
991         src   = &np->rcv_saddr;
992         destp = 0;
993         srcp  = inet_sk(sp)->num;
994         seq_printf(seq,
995                    "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
996                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
997                    i,
998                    src->s6_addr32[0], src->s6_addr32[1],
999                    src->s6_addr32[2], src->s6_addr32[3], srcp,
1000                    dest->s6_addr32[0], dest->s6_addr32[1],
1001                    dest->s6_addr32[2], dest->s6_addr32[3], destp,
1002                    sp->sk_state, 
1003                    atomic_read(&sp->sk_wmem_alloc),
1004                    atomic_read(&sp->sk_rmem_alloc),
1005                    0, 0L, 0,
1006                    sock_i_uid(sp), 0,
1007                    sock_i_ino(sp),
1008                    atomic_read(&sp->sk_refcnt), sp);
1009 }
1010 
1011 static int raw6_seq_show(struct seq_file *seq, void *v)
1012 {
1013         if (v == SEQ_START_TOKEN)
1014                 seq_printf(seq,
1015                            "  sl  "
1016                            "local_address                         "
1017                            "remote_address                        "
1018                            "st tx_queue rx_queue tr tm->when retrnsmt"
1019                            "   uid  timeout inode\n");
1020         else
1021                 raw6_sock_seq_show(seq, v, raw6_seq_private(seq)->bucket);
1022         return 0;
1023 }
1024 
1025 static struct seq_operations raw6_seq_ops = {
1026         .start =        raw6_seq_start,
1027         .next =         raw6_seq_next,
1028         .stop =         raw6_seq_stop,
1029         .show =         raw6_seq_show,
1030 };
1031 
1032 static int raw6_seq_open(struct inode *inode, struct file *file)
1033 {
1034         struct seq_file *seq;
1035         int rc = -ENOMEM;
1036         struct raw6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
1037         if (!s)
1038                 goto out;
1039         rc = seq_open(file, &raw6_seq_ops);
1040         if (rc)
1041                 goto out_kfree;
1042         seq = file->private_data;
1043         seq->private = s;
1044         memset(s, 0, sizeof(*s));
1045 out:
1046         return rc;
1047 out_kfree:
1048         kfree(s);
1049         goto out;
1050 }
1051 
1052 static struct file_operations raw6_seq_fops = {
1053         .owner =        THIS_MODULE,
1054         .open =         raw6_seq_open,
1055         .read =         seq_read,
1056         .llseek =       seq_lseek,
1057         .release =      seq_release_private,
1058 };
1059 
1060 int __init raw6_proc_init(void)
1061 {
1062         if (!proc_net_fops_create("raw6", S_IRUGO, &raw6_seq_fops))
1063                 return -ENOMEM;
1064         return 0;
1065 }
1066 
1067 void raw6_proc_exit(void)
1068 {
1069         proc_net_remove("raw6");
1070 }
1071 #endif  /* CONFIG_PROC_FS */
1072 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp