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