inet_frag.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #ifndef __NET_FRAG_H__
  2. #define __NET_FRAG_H__
  3. struct netns_frags {
  4. /* Keep atomic mem on separate cachelines in structs that include it */
  5. atomic_t mem ____cacheline_aligned_in_smp;
  6. /* sysctls */
  7. int timeout;
  8. int high_thresh;
  9. int low_thresh;
  10. int max_dist;
  11. };
  12. /**
  13. * fragment queue flags
  14. *
  15. * @INET_FRAG_FIRST_IN: first fragment has arrived
  16. * @INET_FRAG_LAST_IN: final fragment has arrived
  17. * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
  18. */
  19. enum {
  20. INET_FRAG_FIRST_IN = BIT(0),
  21. INET_FRAG_LAST_IN = BIT(1),
  22. INET_FRAG_COMPLETE = BIT(2),
  23. };
  24. /**
  25. * struct inet_frag_queue - fragment queue
  26. *
  27. * @lock: spinlock protecting the queue
  28. * @timer: queue expiration timer
  29. * @list: hash bucket list
  30. * @refcnt: reference count of the queue
  31. * @fragments: received fragments head
  32. * @fragments_tail: received fragments tail
  33. * @stamp: timestamp of the last received fragment
  34. * @len: total length of the original datagram
  35. * @meat: length of received fragments so far
  36. * @flags: fragment queue flags
  37. * @max_size: maximum received fragment size
  38. * @net: namespace that this frag belongs to
  39. * @list_evictor: list of queues to forcefully evict (e.g. due to low memory)
  40. */
  41. struct inet_frag_queue {
  42. spinlock_t lock;
  43. struct timer_list timer;
  44. struct hlist_node list;
  45. atomic_t refcnt;
  46. struct sk_buff *fragments;
  47. struct sk_buff *fragments_tail;
  48. ktime_t stamp;
  49. int len;
  50. int meat;
  51. __u8 flags;
  52. u16 max_size;
  53. struct netns_frags *net;
  54. struct hlist_node list_evictor;
  55. };
  56. #define INETFRAGS_HASHSZ 1024
  57. /* averaged:
  58. * max_depth = default ipfrag_high_thresh / INETFRAGS_HASHSZ /
  59. * rounded up (SKB_TRUELEN(0) + sizeof(struct ipq or
  60. * struct frag_queue))
  61. */
  62. #define INETFRAGS_MAXDEPTH 128
  63. struct inet_frag_bucket {
  64. struct hlist_head chain;
  65. spinlock_t chain_lock;
  66. };
  67. struct inet_frags {
  68. struct inet_frag_bucket hash[INETFRAGS_HASHSZ];
  69. struct work_struct frags_work;
  70. unsigned int next_bucket;
  71. unsigned long last_rebuild_jiffies;
  72. bool rebuild;
  73. /* The first call to hashfn is responsible to initialize
  74. * rnd. This is best done with net_get_random_once.
  75. *
  76. * rnd_seqlock is used to let hash insertion detect
  77. * when it needs to re-lookup the hash chain to use.
  78. */
  79. u32 rnd;
  80. seqlock_t rnd_seqlock;
  81. int qsize;
  82. unsigned int (*hashfn)(const struct inet_frag_queue *);
  83. bool (*match)(const struct inet_frag_queue *q,
  84. const void *arg);
  85. void (*constructor)(struct inet_frag_queue *q,
  86. const void *arg);
  87. void (*destructor)(struct inet_frag_queue *);
  88. void (*frag_expire)(unsigned long data);
  89. struct kmem_cache *frags_cachep;
  90. const char *frags_cache_name;
  91. };
  92. int inet_frags_init(struct inet_frags *);
  93. void inet_frags_fini(struct inet_frags *);
  94. static inline void inet_frags_init_net(struct netns_frags *nf)
  95. {
  96. atomic_set(&nf->mem, 0);
  97. }
  98. void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f);
  99. void inet_frag_kill(struct inet_frag_queue *q, struct inet_frags *f);
  100. void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f);
  101. struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
  102. struct inet_frags *f, void *key, unsigned int hash);
  103. void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
  104. const char *prefix);
  105. static inline void inet_frag_put(struct inet_frag_queue *q, struct inet_frags *f)
  106. {
  107. if (atomic_dec_and_test(&q->refcnt))
  108. inet_frag_destroy(q, f);
  109. }
  110. static inline bool inet_frag_evicting(struct inet_frag_queue *q)
  111. {
  112. return !hlist_unhashed(&q->list_evictor);
  113. }
  114. /* Memory Tracking Functions. */
  115. static inline int frag_mem_limit(struct netns_frags *nf)
  116. {
  117. return atomic_read(&nf->mem);
  118. }
  119. static inline void sub_frag_mem_limit(struct netns_frags *nf, int i)
  120. {
  121. atomic_sub(i, &nf->mem);
  122. }
  123. static inline void add_frag_mem_limit(struct netns_frags *nf, int i)
  124. {
  125. atomic_add(i, &nf->mem);
  126. }
  127. static inline int sum_frag_mem_limit(struct netns_frags *nf)
  128. {
  129. return atomic_read(&nf->mem);
  130. }
  131. /* RFC 3168 support :
  132. * We want to check ECN values of all fragments, do detect invalid combinations.
  133. * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value.
  134. */
  135. #define IPFRAG_ECN_NOT_ECT 0x01 /* one frag had ECN_NOT_ECT */
  136. #define IPFRAG_ECN_ECT_1 0x02 /* one frag had ECN_ECT_1 */
  137. #define IPFRAG_ECN_ECT_0 0x04 /* one frag had ECN_ECT_0 */
  138. #define IPFRAG_ECN_CE 0x08 /* one frag had ECN_CE */
  139. extern const u8 ip_frag_ecn_table[16];
  140. #endif