inetpeer.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. u32 n_redirects;
  36. unsigned long rate_last;
  37. /*
  38. * Once inet_peer is queued for deletion (refcnt == 0), following field
  39. * is not available: rid
  40. * We can share memory with rcu_head to help keep inet_peer small.
  41. */
  42. union {
  43. struct {
  44. atomic_t rid; /* Frag reception counter */
  45. };
  46. struct rcu_head rcu;
  47. };
  48. /* following fields might be frequently dirtied */
  49. __u32 dtime; /* the time of last use of not referenced entries */
  50. refcount_t refcnt;
  51. };
  52. struct inet_peer_base {
  53. struct rb_root rb_root;
  54. seqlock_t lock;
  55. int total;
  56. };
  57. void inet_peer_base_init(struct inet_peer_base *);
  58. void inet_initpeers(void) __init;
  59. #define INETPEER_METRICS_NEW (~(u32) 0)
  60. static inline void inetpeer_set_addr_v4(struct inetpeer_addr *iaddr, __be32 ip)
  61. {
  62. iaddr->a4.addr = ip;
  63. iaddr->a4.vif = 0;
  64. iaddr->family = AF_INET;
  65. }
  66. static inline __be32 inetpeer_get_addr_v4(struct inetpeer_addr *iaddr)
  67. {
  68. return iaddr->a4.addr;
  69. }
  70. static inline void inetpeer_set_addr_v6(struct inetpeer_addr *iaddr,
  71. struct in6_addr *in6)
  72. {
  73. iaddr->a6 = *in6;
  74. iaddr->family = AF_INET6;
  75. }
  76. static inline struct in6_addr *inetpeer_get_addr_v6(struct inetpeer_addr *iaddr)
  77. {
  78. return &iaddr->a6;
  79. }
  80. /* can be called with or without local BH being disabled */
  81. struct inet_peer *inet_getpeer(struct inet_peer_base *base,
  82. const struct inetpeer_addr *daddr,
  83. int create);
  84. static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
  85. __be32 v4daddr,
  86. int vif, int create)
  87. {
  88. struct inetpeer_addr daddr;
  89. daddr.a4.addr = v4daddr;
  90. daddr.a4.vif = vif;
  91. daddr.family = AF_INET;
  92. return inet_getpeer(base, &daddr, create);
  93. }
  94. static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
  95. const struct in6_addr *v6daddr,
  96. int create)
  97. {
  98. struct inetpeer_addr daddr;
  99. daddr.a6 = *v6daddr;
  100. daddr.family = AF_INET6;
  101. return inet_getpeer(base, &daddr, create);
  102. }
  103. static inline int inetpeer_addr_cmp(const struct inetpeer_addr *a,
  104. const struct inetpeer_addr *b)
  105. {
  106. int i, n;
  107. if (a->family == AF_INET)
  108. n = sizeof(a->a4) / sizeof(u32);
  109. else
  110. n = sizeof(a->a6) / sizeof(u32);
  111. for (i = 0; i < n; i++) {
  112. if (a->key[i] == b->key[i])
  113. continue;
  114. if (a->key[i] < b->key[i])
  115. return -1;
  116. return 1;
  117. }
  118. return 0;
  119. }
  120. /* can be called from BH context or outside */
  121. void inet_putpeer(struct inet_peer *p);
  122. bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
  123. void inetpeer_invalidate_tree(struct inet_peer_base *);
  124. #endif /* _NET_INETPEER_H */