inetpeer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <asm/atomic.h>
  15. struct inetpeer_addr_base {
  16. union {
  17. __be32 a4;
  18. __be32 a6[4];
  19. };
  20. };
  21. struct inetpeer_addr {
  22. struct inetpeer_addr_base addr;
  23. __u16 family;
  24. };
  25. struct inet_peer {
  26. /* group together avl_left,avl_right,v4daddr to speedup lookups */
  27. struct inet_peer __rcu *avl_left, *avl_right;
  28. struct inetpeer_addr daddr;
  29. __u32 avl_height;
  30. struct list_head unused;
  31. __u32 dtime; /* the time of last use of not
  32. * referenced entries */
  33. atomic_t refcnt;
  34. /*
  35. * Once inet_peer is queued for deletion (refcnt == -1), following fields
  36. * are not available: rid, ip_id_count, tcp_ts, tcp_ts_stamp, metrics
  37. * We can share memory with rcu_head to help keep inet_peer small.
  38. */
  39. union {
  40. struct {
  41. atomic_t rid; /* Frag reception counter */
  42. atomic_t ip_id_count; /* IP ID for the next packet */
  43. __u32 tcp_ts;
  44. __u32 tcp_ts_stamp;
  45. u32 metrics[RTAX_MAX];
  46. u32 rate_tokens; /* rate limiting for ICMP */
  47. unsigned long rate_last;
  48. unsigned long pmtu_expires;
  49. u32 pmtu_orig;
  50. u32 pmtu_learned;
  51. struct inetpeer_addr_base redirect_learned;
  52. };
  53. struct rcu_head rcu;
  54. };
  55. };
  56. void inet_initpeers(void) __init;
  57. #define INETPEER_METRICS_NEW (~(u32) 0)
  58. static inline bool inet_metrics_new(const struct inet_peer *p)
  59. {
  60. return p->metrics[RTAX_LOCK-1] == INETPEER_METRICS_NEW;
  61. }
  62. /* can be called with or without local BH being disabled */
  63. struct inet_peer *inet_getpeer(struct inetpeer_addr *daddr, int create);
  64. static inline struct inet_peer *inet_getpeer_v4(__be32 v4daddr, int create)
  65. {
  66. struct inetpeer_addr daddr;
  67. daddr.addr.a4 = v4daddr;
  68. daddr.family = AF_INET;
  69. return inet_getpeer(&daddr, create);
  70. }
  71. static inline struct inet_peer *inet_getpeer_v6(const struct in6_addr *v6daddr, int create)
  72. {
  73. struct inetpeer_addr daddr;
  74. ipv6_addr_copy((struct in6_addr *)daddr.addr.a6, v6daddr);
  75. daddr.family = AF_INET6;
  76. return inet_getpeer(&daddr, create);
  77. }
  78. /* can be called from BH context or outside */
  79. extern void inet_putpeer(struct inet_peer *p);
  80. extern bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
  81. /*
  82. * temporary check to make sure we dont access rid, ip_id_count, tcp_ts,
  83. * tcp_ts_stamp if no refcount is taken on inet_peer
  84. */
  85. static inline void inet_peer_refcheck(const struct inet_peer *p)
  86. {
  87. WARN_ON_ONCE(atomic_read(&p->refcnt) <= 0);
  88. }
  89. /* can be called with or without local BH being disabled */
  90. static inline __u16 inet_getid(struct inet_peer *p, int more)
  91. {
  92. more++;
  93. inet_peer_refcheck(p);
  94. return atomic_add_return(more, &p->ip_id_count) - more;
  95. }
  96. #endif /* _NET_INETPEER_H */