1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _IPV6_FRAG_H 3 #define _IPV6_FRAG_H 4 #include <linux/kernel.h> 5 #include <net/addrconf.h> 6 #include <net/ipv6.h> 7 #include <net/inet_frag.h> 8 9 enum ip6_defrag_users { 10 IP6_DEFRAG_LOCAL_DELIVER, 11 IP6_DEFRAG_CONNTRACK_IN, 12 __IP6_DEFRAG_CONNTRACK_IN = IP6_DEFRAG_CONNTRACK_IN + USHRT_MAX, 13 IP6_DEFRAG_CONNTRACK_OUT, 14 __IP6_DEFRAG_CONNTRACK_OUT = IP6_DEFRAG_CONNTRACK_OUT + USHRT_MAX, 15 IP6_DEFRAG_CONNTRACK_BRIDGE_IN, 16 __IP6_DEFRAG_CONNTRACK_BRIDGE_IN = IP6_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX, 17 }; 18 19 /* 20 * Equivalent of ipv4 struct ip 21 */ 22 struct frag_queue { 23 struct inet_frag_queue q; 24 25 int iif; 26 __u16 nhoffset; 27 u8 ecn; 28 }; 29 30 #if IS_ENABLED(CONFIG_IPV6) 31 static inline void ip6frag_init(struct inet_frag_queue *q, const void *a) 32 { 33 struct frag_queue *fq = container_of(q, struct frag_queue, q); 34 const struct frag_v6_compare_key *key = a; 35 36 q->key.v6 = *key; 37 fq->ecn = 0; 38 } 39 40 static inline u32 ip6frag_key_hashfn(const void *data, u32 len, u32 seed) 41 { 42 return jhash2(data, 43 sizeof(struct frag_v6_compare_key) / sizeof(u32), seed); 44 } 45 46 static inline u32 ip6frag_obj_hashfn(const void *data, u32 len, u32 seed) 47 { 48 const struct inet_frag_queue *fq = data; 49 50 return jhash2((const u32 *)&fq->key.v6, 51 sizeof(struct frag_v6_compare_key) / sizeof(u32), seed); 52 } 53 54 static inline int 55 ip6frag_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr) 56 { 57 const struct frag_v6_compare_key *key = arg->key; 58 const struct inet_frag_queue *fq = ptr; 59 60 return !!memcmp(&fq->key, key, sizeof(*key)); 61 } 62 63 static inline void 64 ip6frag_expire_frag_queue(struct net *net, struct frag_queue *fq) 65 { 66 struct net_device *dev = NULL; 67 struct sk_buff *head; 68 69 rcu_read_lock(); 70 /* Paired with the WRITE_ONCE() in fqdir_pre_exit(). */ 71 if (READ_ONCE(fq->q.fqdir->dead)) 72 goto out_rcu_unlock; 73 spin_lock(&fq->q.lock); 74 75 if (fq->q.flags & INET_FRAG_COMPLETE) 76 goto out; 77 78 inet_frag_kill(&fq->q); 79 80 dev = dev_get_by_index_rcu(net, fq->iif); 81 if (!dev) 82 goto out; 83 84 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); 85 __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT); 86 87 /* Don't send error if the first segment did not arrive. */ 88 if (!(fq->q.flags & INET_FRAG_FIRST_IN)) 89 goto out; 90 91 /* sk_buff::dev and sk_buff::rbnode are unionized. So we 92 * pull the head out of the tree in order to be able to 93 * deal with head->dev. 94 */ 95 head = inet_frag_pull_head(&fq->q); 96 if (!head) 97 goto out; 98 99 head->dev = dev; 100 spin_unlock(&fq->q.lock); 101 102 icmpv6_send(head, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0); 103 kfree_skb(head); 104 goto out_rcu_unlock; 105 106 out: 107 spin_unlock(&fq->q.lock); 108 out_rcu_unlock: 109 rcu_read_unlock(); 110 inet_frag_put(&fq->q); 111 } 112 113 /* Check if the upper layer header is truncated in the first fragment. */ 114 static inline bool 115 ipv6frag_thdr_truncated(struct sk_buff *skb, int start, u8 *nexthdrp) 116 { 117 u8 nexthdr = *nexthdrp; 118 __be16 frag_off; 119 int offset; 120 121 offset = ipv6_skip_exthdr(skb, start, &nexthdr, &frag_off); 122 if (offset < 0 || (frag_off & htons(IP6_OFFSET))) 123 return false; 124 switch (nexthdr) { 125 case NEXTHDR_TCP: 126 offset += sizeof(struct tcphdr); 127 break; 128 case NEXTHDR_UDP: 129 offset += sizeof(struct udphdr); 130 break; 131 case NEXTHDR_ICMP: 132 offset += sizeof(struct icmp6hdr); 133 break; 134 default: 135 offset += 1; 136 } 137 if (offset > skb->len) 138 return true; 139 return false; 140 } 141 142 #endif 143 #endif 144