inet_frag.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. struct inet_frags *f;
  10. struct rhashtable rhashtable ____cacheline_aligned_in_smp;
  11. /* Keep atomic mem on separate cachelines in structs that include it */
  12. atomic_long_t mem ____cacheline_aligned_in_smp;
  13. };
  14. /**
  15. * fragment queue flags
  16. *
  17. * @INET_FRAG_FIRST_IN: first fragment has arrived
  18. * @INET_FRAG_LAST_IN: final fragment has arrived
  19. * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
  20. */
  21. enum {
  22. INET_FRAG_FIRST_IN = BIT(0),
  23. INET_FRAG_LAST_IN = BIT(1),
  24. INET_FRAG_COMPLETE = BIT(2),
  25. };
  26. struct frag_v4_compare_key {
  27. __be32 saddr;
  28. __be32 daddr;
  29. u32 user;
  30. u32 vif;
  31. __be16 id;
  32. u16 protocol;
  33. };
  34. struct frag_v6_compare_key {
  35. struct in6_addr saddr;
  36. struct in6_addr daddr;
  37. u32 user;
  38. __be32 id;
  39. u32 iif;
  40. };
  41. /**
  42. * struct inet_frag_queue - fragment queue
  43. *
  44. * @node: rhash node
  45. * @key: keys identifying this frag.
  46. * @timer: queue expiration timer
  47. * @lock: spinlock protecting this frag
  48. * @refcnt: reference count of the queue
  49. * @fragments: received fragments head
  50. * @rb_fragments: received fragments rb-tree root
  51. * @fragments_tail: received fragments tail
  52. * @last_run_head: the head of the last "run". see ip_fragment.c
  53. * @stamp: timestamp of the last received fragment
  54. * @len: total length of the original datagram
  55. * @meat: length of received fragments so far
  56. * @flags: fragment queue flags
  57. * @max_size: maximum received fragment size
  58. * @net: namespace that this frag belongs to
  59. * @rcu: rcu head for freeing deferall
  60. */
  61. struct inet_frag_queue {
  62. struct rhash_head node;
  63. union {
  64. struct frag_v4_compare_key v4;
  65. struct frag_v6_compare_key v6;
  66. } key;
  67. struct timer_list timer;
  68. spinlock_t lock;
  69. atomic_t refcnt;
  70. struct sk_buff *fragments; /* Used in IPv6. */
  71. struct rb_root rb_fragments; /* Used in IPv4. */
  72. struct sk_buff *fragments_tail;
  73. struct sk_buff *last_run_head;
  74. ktime_t stamp;
  75. int len;
  76. int meat;
  77. __u8 flags;
  78. u16 max_size;
  79. struct netns_frags *net;
  80. struct rcu_head rcu;
  81. };
  82. struct inet_frags {
  83. int qsize;
  84. void (*constructor)(struct inet_frag_queue *q,
  85. const void *arg);
  86. void (*destructor)(struct inet_frag_queue *);
  87. void (*skb_free)(struct sk_buff *);
  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. #endif