inet6_hashtables.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Generic INET6 transport hashtables
  7. *
  8. * Authors: Lotsa people, from code originally in tcp
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/config.h>
  16. #include <linux/module.h>
  17. #include <net/inet_connection_sock.h>
  18. #include <net/inet_hashtables.h>
  19. #include <net/inet6_hashtables.h>
  20. struct sock *inet6_lookup_listener(struct inet_hashinfo *hashinfo,
  21. const struct in6_addr *daddr,
  22. const unsigned short hnum, const int dif)
  23. {
  24. struct sock *sk;
  25. const struct hlist_node *node;
  26. struct sock *result = NULL;
  27. int score, hiscore = 0;
  28. read_lock(&hashinfo->lhash_lock);
  29. sk_for_each(sk, node, &hashinfo->listening_hash[inet_lhashfn(hnum)]) {
  30. if (inet_sk(sk)->num == hnum && sk->sk_family == PF_INET6) {
  31. const struct ipv6_pinfo *np = inet6_sk(sk);
  32. score = 1;
  33. if (!ipv6_addr_any(&np->rcv_saddr)) {
  34. if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
  35. continue;
  36. score++;
  37. }
  38. if (sk->sk_bound_dev_if) {
  39. if (sk->sk_bound_dev_if != dif)
  40. continue;
  41. score++;
  42. }
  43. if (score == 3) {
  44. result = sk;
  45. break;
  46. }
  47. if (score > hiscore) {
  48. hiscore = score;
  49. result = sk;
  50. }
  51. }
  52. }
  53. if (result)
  54. sock_hold(result);
  55. read_unlock(&hashinfo->lhash_lock);
  56. return result;
  57. }
  58. EXPORT_SYMBOL_GPL(inet6_lookup_listener);
  59. struct sock *inet6_lookup(struct inet_hashinfo *hashinfo,
  60. const struct in6_addr *saddr, const u16 sport,
  61. const struct in6_addr *daddr, const u16 dport,
  62. const int dif)
  63. {
  64. struct sock *sk;
  65. local_bh_disable();
  66. sk = __inet6_lookup(hashinfo, saddr, sport, daddr, ntohs(dport), dif);
  67. local_bh_enable();
  68. return sk;
  69. }
  70. EXPORT_SYMBOL_GPL(inet6_lookup);