inet_frag.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_FRAG_H__
  3. #define __NET_FRAG_H__
  4. #include <linux/rhashtable.h>
  5. struct netns_frags {
  6. /* sysctls */
  7. long high_thresh;
  8. long low_thresh;
  9. int timeout;
  10. int max_dist;
  11. struct inet_frags *f;
  12. struct rhashtable rhashtable ____cacheline_aligned_in_smp;
  13. /* Keep atomic mem on separate cachelines in structs that include it */
  14. atomic_long_t mem ____cacheline_aligned_in_smp;
  15. };
  16. /**
  17. * fragment queue flags
  18. *
  19. * @INET_FRAG_FIRST_IN: first fragment has arrived
  20. * @INET_FRAG_LAST_IN: final fragment has arrived
  21. * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
  22. */
  23. enum {
  24. INET_FRAG_FIRST_IN = BIT(0),
  25. INET_FRAG_LAST_IN = BIT(1),
  26. INET_FRAG_COMPLETE = BIT(2),
  27. };
  28. struct frag_v4_compare_key {
  29. __be32 saddr;
  30. __be32 daddr;
  31. u32 user;
  32. u32 vif;
  33. __be16 id;
  34. u16 protocol;
  35. };
  36. struct frag_v6_compare_key {
  37. struct in6_addr saddr;
  38. struct in6_addr daddr;
  39. u32 user;
  40. __be32 id;
  41. u32 iif;
  42. };
  43. /**
  44. * struct inet_frag_queue - fragment queue
  45. *
  46. * @node: rhash node
  47. * @key: keys identifying this frag.
  48. * @timer: queue expiration timer
  49. * @lock: spinlock protecting this frag
  50. * @refcnt: reference count of the queue
  51. * @fragments: received fragments head
  52. * @fragments_tail: received fragments tail
  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. refcount_t refcnt;
  70. struct sk_buff *fragments;
  71. struct sk_buff *fragments_tail;
  72. ktime_t stamp;
  73. int len;
  74. int meat;
  75. __u8 flags;
  76. u16 max_size;
  77. struct netns_frags *net;
  78. struct rcu_head rcu;
  79. };
  80. struct inet_frags {
  81. unsigned int qsize;
  82. void (*constructor)(struct inet_frag_queue *q,
  83. const void *arg);
  84. void (*destructor)(struct inet_frag_queue *);
  85. void (*frag_expire)(struct timer_list *t);
  86. struct kmem_cache *frags_cachep;
  87. const char *frags_cache_name;
  88. struct rhashtable_params rhash_params;
  89. };
  90. int inet_frags_init(struct inet_frags *);
  91. void inet_frags_fini(struct inet_frags *);
  92. static inline int inet_frags_init_net(struct netns_frags *nf)
  93. {
  94. atomic_long_set(&nf->mem, 0);
  95. return rhashtable_init(&nf->rhashtable, &nf->f->rhash_params);
  96. }
  97. void inet_frags_exit_net(struct netns_frags *nf);
  98. void inet_frag_kill(struct inet_frag_queue *q);
  99. void inet_frag_destroy(struct inet_frag_queue *q);
  100. struct inet_frag_queue *inet_frag_find(struct netns_frags *nf, void *key);
  101. static inline void inet_frag_put(struct inet_frag_queue *q)
  102. {
  103. if (refcount_dec_and_test(&q->refcnt))
  104. inet_frag_destroy(q);
  105. }
  106. /* Memory Tracking Functions. */
  107. static inline long frag_mem_limit(const struct netns_frags *nf)
  108. {
  109. return atomic_long_read(&nf->mem);
  110. }
  111. static inline void sub_frag_mem_limit(struct netns_frags *nf, long val)
  112. {
  113. atomic_long_sub(val, &nf->mem);
  114. }
  115. static inline void add_frag_mem_limit(struct netns_frags *nf, long val)
  116. {
  117. atomic_long_add(val, &nf->mem);
  118. }
  119. /* RFC 3168 support :
  120. * We want to check ECN values of all fragments, do detect invalid combinations.
  121. * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value.
  122. */
  123. #define IPFRAG_ECN_NOT_ECT 0x01 /* one frag had ECN_NOT_ECT */
  124. #define IPFRAG_ECN_ECT_1 0x02 /* one frag had ECN_ECT_1 */
  125. #define IPFRAG_ECN_ECT_0 0x04 /* one frag had ECN_ECT_0 */
  126. #define IPFRAG_ECN_CE 0x08 /* one frag had ECN_CE */
  127. extern const u8 ip_frag_ecn_table[16];
  128. #endif