inet_frag.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #ifndef __NET_FRAG_H__
  2. #define __NET_FRAG_H__
  3. #include <linux/rhashtable.h>
  4. struct netns_frags {
  5. /* sysctls */
  6. long high_thresh;
  7. long low_thresh;
  8. int timeout;
  9. int max_dist;
  10. struct inet_frags *f;
  11. struct rhashtable rhashtable ____cacheline_aligned_in_smp;
  12. /* Keep atomic mem on separate cachelines in structs that include it */
  13. atomic_long_t mem ____cacheline_aligned_in_smp;
  14. };
  15. /**
  16. * fragment queue flags
  17. *
  18. * @INET_FRAG_FIRST_IN: first fragment has arrived
  19. * @INET_FRAG_LAST_IN: final fragment has arrived
  20. * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
  21. */
  22. enum {
  23. INET_FRAG_FIRST_IN = BIT(0),
  24. INET_FRAG_LAST_IN = BIT(1),
  25. INET_FRAG_COMPLETE = BIT(2),
  26. };
  27. struct frag_v4_compare_key {
  28. __be32 saddr;
  29. __be32 daddr;
  30. u32 user;
  31. u32 vif;
  32. __be16 id;
  33. u16 protocol;
  34. };
  35. struct frag_v6_compare_key {
  36. struct in6_addr saddr;
  37. struct in6_addr daddr;
  38. u32 user;
  39. __be32 id;
  40. u32 iif;
  41. };
  42. /**
  43. * struct inet_frag_queue - fragment queue
  44. *
  45. * @node: rhash node
  46. * @key: keys identifying this frag.
  47. * @timer: queue expiration timer
  48. * @lock: spinlock protecting this frag
  49. * @refcnt: reference count of the queue
  50. * @fragments: received fragments head
  51. * @rb_fragments: received fragments rb-tree root
  52. * @fragments_tail: received fragments tail
  53. * @last_run_head: the head of the last "run". see ip_fragment.c
  54. * @stamp: timestamp of the last received fragment
  55. * @len: total length of the original datagram
  56. * @meat: length of received fragments so far
  57. * @flags: fragment queue flags
  58. * @max_size: maximum received fragment size
  59. * @net: namespace that this frag belongs to
  60. * @rcu: rcu head for freeing deferall
  61. */
  62. struct inet_frag_queue {
  63. struct rhash_head node;
  64. union {
  65. struct frag_v4_compare_key v4;
  66. struct frag_v6_compare_key v6;
  67. } key;
  68. struct timer_list timer;
  69. spinlock_t lock;
  70. atomic_t refcnt;
  71. struct sk_buff *fragments; /* used in 6lopwpan IPv6. */
  72. struct rb_root rb_fragments; /* Used in IPv4/IPv6. */
  73. struct sk_buff *fragments_tail;
  74. struct sk_buff *last_run_head;
  75. ktime_t stamp;
  76. int len;
  77. int meat;
  78. __u8 flags;
  79. u16 max_size;
  80. struct netns_frags *net;
  81. struct rcu_head rcu;
  82. };
  83. struct inet_frags {
  84. int qsize;
  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. struct rhashtable_params rhash_params;
  92. };
  93. int inet_frags_init(struct inet_frags *);
  94. void inet_frags_fini(struct inet_frags *);
  95. static inline int inet_frags_init_net(struct netns_frags *nf)
  96. {
  97. atomic_long_set(&nf->mem, 0);
  98. return rhashtable_init(&nf->rhashtable, &nf->f->rhash_params);
  99. }
  100. void inet_frags_exit_net(struct netns_frags *nf);
  101. void inet_frag_kill(struct inet_frag_queue *q);
  102. void inet_frag_destroy(struct inet_frag_queue *q);
  103. struct inet_frag_queue *inet_frag_find(struct netns_frags *nf, void *key);
  104. /* Free all skbs in the queue; return the sum of their truesizes. */
  105. unsigned int inet_frag_rbtree_purge(struct rb_root *root);
  106. static inline void inet_frag_put(struct inet_frag_queue *q)
  107. {
  108. if (atomic_dec_and_test(&q->refcnt))
  109. inet_frag_destroy(q);
  110. }
  111. /* Memory Tracking Functions. */
  112. static inline long frag_mem_limit(const struct netns_frags *nf)
  113. {
  114. return atomic_long_read(&nf->mem);
  115. }
  116. static inline void sub_frag_mem_limit(struct netns_frags *nf, long val)
  117. {
  118. atomic_long_sub(val, &nf->mem);
  119. }
  120. static inline void add_frag_mem_limit(struct netns_frags *nf, long val)
  121. {
  122. atomic_long_add(val, &nf->mem);
  123. }
  124. /* RFC 3168 support :
  125. * We want to check ECN values of all fragments, do detect invalid combinations.
  126. * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value.
  127. */
  128. #define IPFRAG_ECN_NOT_ECT 0x01 /* one frag had ECN_NOT_ECT */
  129. #define IPFRAG_ECN_ECT_1 0x02 /* one frag had ECN_ECT_1 */
  130. #define IPFRAG_ECN_ECT_0 0x04 /* one frag had ECN_ECT_0 */
  131. #define IPFRAG_ECN_CE 0x08 /* one frag had ECN_CE */
  132. extern const u8 ip_frag_ecn_table[16];
  133. /* Return values of inet_frag_queue_insert() */
  134. #define IPFRAG_OK 0
  135. #define IPFRAG_DUP 1
  136. #define IPFRAG_OVERLAP 2
  137. int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
  138. int offset, int end);
  139. void *inet_frag_reasm_prepare(struct inet_frag_queue *q, struct sk_buff *skb,
  140. struct sk_buff *parent);
  141. void inet_frag_reasm_finish(struct inet_frag_queue *q, struct sk_buff *head,
  142. void *reasm_data);
  143. struct sk_buff *inet_frag_pull_head(struct inet_frag_queue *q);
  144. #endif