inetpeer.h 2.3 KB

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