inetpeer.h 3.4 KB

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