inetpeer.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * INETPEER - A storage for permanent information about peers
  4. *
  5. * Authors: Andrey V. Savochkin <saw@msu.ru>
  6. */
  7. #ifndef _NET_INETPEER_H
  8. #define _NET_INETPEER_H
  9. #include <linux/types.h>
  10. #include <linux/init.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/rtnetlink.h>
  14. #include <net/ipv6.h>
  15. #include <linux/atomic.h>
  16. /* IPv4 address key for cache lookups */
  17. struct ipv4_addr_key {
  18. __be32 addr;
  19. int vif;
  20. };
  21. #define INETPEER_MAXKEYSZ (sizeof(struct in6_addr) / sizeof(u32))
  22. struct inetpeer_addr {
  23. union {
  24. struct ipv4_addr_key a4;
  25. struct in6_addr a6;
  26. u32 key[INETPEER_MAXKEYSZ];
  27. };
  28. __u16 family;
  29. };
  30. struct inet_peer {
  31. struct rb_node rb_node;
  32. struct inetpeer_addr daddr;
  33. u32 metrics[RTAX_MAX];
  34. u32 rate_tokens; /* rate limiting for ICMP */
  35. unsigned long rate_last;
  36. /*
  37. * Once inet_peer is queued for deletion (refcnt == 0), following field
  38. * is not available: rid
  39. * We can share memory with rcu_head to help keep inet_peer small.
  40. */
  41. union {
  42. struct {
  43. atomic_t rid; /* Frag reception counter */
  44. };
  45. struct rcu_head rcu;
  46. };
  47. /* following fields might be frequently dirtied */
  48. __u32 dtime; /* the time of last use of not referenced entries */
  49. refcount_t refcnt;
  50. };
  51. struct inet_peer_base {
  52. struct rb_root rb_root;
  53. seqlock_t lock;
  54. int total;
  55. };
  56. void inet_peer_base_init(struct inet_peer_base *);
  57. void inet_initpeers(void) __init;
  58. #define INETPEER_METRICS_NEW (~(u32) 0)
  59. static inline void inetpeer_set_addr_v4(struct inetpeer_addr *iaddr, __be32 ip)
  60. {
  61. iaddr->a4.addr = ip;
  62. iaddr->a4.vif = 0;
  63. iaddr->family = AF_INET;
  64. }
  65. static inline __be32 inetpeer_get_addr_v4(struct inetpeer_addr *iaddr)
  66. {
  67. return iaddr->a4.addr;
  68. }
  69. static inline void inetpeer_set_addr_v6(struct inetpeer_addr *iaddr,
  70. struct in6_addr *in6)
  71. {
  72. iaddr->a6 = *in6;
  73. iaddr->family = AF_INET6;
  74. }
  75. static inline struct in6_addr *inetpeer_get_addr_v6(struct inetpeer_addr *iaddr)
  76. {
  77. return &iaddr->a6;
  78. }
  79. /* can be called with or without local BH being disabled */
  80. struct inet_peer *inet_getpeer(struct inet_peer_base *base,
  81. const struct inetpeer_addr *daddr,
  82. int create);
  83. static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
  84. __be32 v4daddr,
  85. int vif, int create)
  86. {
  87. struct inetpeer_addr daddr;
  88. daddr.a4.addr = v4daddr;
  89. daddr.a4.vif = vif;
  90. daddr.family = AF_INET;
  91. return inet_getpeer(base, &daddr, create);
  92. }
  93. static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
  94. const struct in6_addr *v6daddr,
  95. int create)
  96. {
  97. struct inetpeer_addr daddr;
  98. daddr.a6 = *v6daddr;
  99. daddr.family = AF_INET6;
  100. return inet_getpeer(base, &daddr, create);
  101. }
  102. static inline int inetpeer_addr_cmp(const struct inetpeer_addr *a,
  103. const struct inetpeer_addr *b)
  104. {
  105. int i, n;
  106. if (a->family == AF_INET)
  107. n = sizeof(a->a4) / sizeof(u32);
  108. else
  109. n = sizeof(a->a6) / sizeof(u32);
  110. for (i = 0; i < n; i++) {
  111. if (a->key[i] == b->key[i])
  112. continue;
  113. if (a->key[i] < b->key[i])
  114. return -1;
  115. return 1;
  116. }
  117. return 0;
  118. }
  119. /* can be called from BH context or outside */
  120. void inet_putpeer(struct inet_peer *p);
  121. bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
  122. void inetpeer_invalidate_tree(struct inet_peer_base *);
  123. #endif /* _NET_INETPEER_H */