1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2007-2008 BalaBit IT Ltd.
4  * Author: Krisztian Kovacs
5  */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/skbuff.h>
9 #include <net/tcp.h>
10 #include <net/udp.h>
11 #include <net/icmp.h>
12 #include <net/sock.h>
13 #include <net/inet_sock.h>
14 #include <net/inet6_hashtables.h>
15 #include <net/netfilter/nf_socket.h>
16 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
17 #include <net/netfilter/nf_conntrack.h>
18 #endif
19 
20 static int
extract_icmp6_fields(const struct sk_buff * skb,unsigned int outside_hdrlen,int * protocol,const struct in6_addr ** raddr,const struct in6_addr ** laddr,__be16 * rport,__be16 * lport,struct ipv6hdr * ipv6_var)21 extract_icmp6_fields(const struct sk_buff *skb,
22 		     unsigned int outside_hdrlen,
23 		     int *protocol,
24 		     const struct in6_addr **raddr,
25 		     const struct in6_addr **laddr,
26 		     __be16 *rport,
27 		     __be16 *lport,
28 		     struct ipv6hdr *ipv6_var)
29 {
30 	const struct ipv6hdr *inside_iph;
31 	struct icmp6hdr *icmph, _icmph;
32 	__be16 *ports, _ports[2];
33 	u8 inside_nexthdr;
34 	__be16 inside_fragoff;
35 	int inside_hdrlen;
36 
37 	icmph = skb_header_pointer(skb, outside_hdrlen,
38 				   sizeof(_icmph), &_icmph);
39 	if (icmph == NULL)
40 		return 1;
41 
42 	if (icmph->icmp6_type & ICMPV6_INFOMSG_MASK)
43 		return 1;
44 
45 	inside_iph = skb_header_pointer(skb, outside_hdrlen + sizeof(_icmph),
46 					sizeof(*ipv6_var), ipv6_var);
47 	if (inside_iph == NULL)
48 		return 1;
49 	inside_nexthdr = inside_iph->nexthdr;
50 
51 	inside_hdrlen = ipv6_skip_exthdr(skb, outside_hdrlen + sizeof(_icmph) +
52 					      sizeof(*ipv6_var),
53 					 &inside_nexthdr, &inside_fragoff);
54 	if (inside_hdrlen < 0)
55 		return 1; /* hjm: Packet has no/incomplete transport layer headers. */
56 
57 	if (inside_nexthdr != IPPROTO_TCP &&
58 	    inside_nexthdr != IPPROTO_UDP)
59 		return 1;
60 
61 	ports = skb_header_pointer(skb, inside_hdrlen,
62 				   sizeof(_ports), &_ports);
63 	if (ports == NULL)
64 		return 1;
65 
66 	/* the inside IP packet is the one quoted from our side, thus
67 	 * its saddr is the local address */
68 	*protocol = inside_nexthdr;
69 	*laddr = &inside_iph->saddr;
70 	*lport = ports[0];
71 	*raddr = &inside_iph->daddr;
72 	*rport = ports[1];
73 
74 	return 0;
75 }
76 
77 static struct sock *
nf_socket_get_sock_v6(struct net * net,struct sk_buff * skb,int doff,const u8 protocol,const struct in6_addr * saddr,const struct in6_addr * daddr,const __be16 sport,const __be16 dport,const struct net_device * in)78 nf_socket_get_sock_v6(struct net *net, struct sk_buff *skb, int doff,
79 		      const u8 protocol,
80 		      const struct in6_addr *saddr, const struct in6_addr *daddr,
81 		      const __be16 sport, const __be16 dport,
82 		      const struct net_device *in)
83 {
84 	switch (protocol) {
85 	case IPPROTO_TCP:
86 		return inet6_lookup(net, net->ipv4.tcp_death_row.hashinfo,
87 				    skb, doff, saddr, sport, daddr, dport,
88 				    in->ifindex);
89 	case IPPROTO_UDP:
90 		return udp6_lib_lookup(net, saddr, sport, daddr, dport,
91 				       in->ifindex);
92 	}
93 
94 	return NULL;
95 }
96 
nf_sk_lookup_slow_v6(struct net * net,const struct sk_buff * skb,const struct net_device * indev)97 struct sock *nf_sk_lookup_slow_v6(struct net *net, const struct sk_buff *skb,
98 				  const struct net_device *indev)
99 {
100 	__be16 dport, sport;
101 	const struct in6_addr *daddr = NULL, *saddr = NULL;
102 	struct ipv6hdr *iph = ipv6_hdr(skb), ipv6_var;
103 	struct sk_buff *data_skb = NULL;
104 	int doff = 0;
105 	int thoff = 0, tproto;
106 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
107 	enum ip_conntrack_info ctinfo;
108 	struct nf_conn const *ct;
109 #endif
110 
111 	tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
112 	if (tproto < 0) {
113 		pr_debug("unable to find transport header in IPv6 packet, dropping\n");
114 		return NULL;
115 	}
116 
117 	if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
118 		struct tcphdr _hdr;
119 		struct udphdr *hp;
120 
121 		hp = skb_header_pointer(skb, thoff, tproto == IPPROTO_UDP ?
122 					sizeof(*hp) : sizeof(_hdr), &_hdr);
123 		if (hp == NULL)
124 			return NULL;
125 
126 		saddr = &iph->saddr;
127 		sport = hp->source;
128 		daddr = &iph->daddr;
129 		dport = hp->dest;
130 		data_skb = (struct sk_buff *)skb;
131 		doff = tproto == IPPROTO_TCP ?
132 			thoff + __tcp_hdrlen((struct tcphdr *)hp) :
133 			thoff + sizeof(*hp);
134 
135 	} else if (tproto == IPPROTO_ICMPV6) {
136 		if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr,
137 					 &sport, &dport, &ipv6_var))
138 			return NULL;
139 	} else {
140 		return NULL;
141 	}
142 
143 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
144 	/* Do the lookup with the original socket address in
145 	 * case this is a reply packet of an established
146 	 * SNAT-ted connection.
147 	 */
148 	ct = nf_ct_get(skb, &ctinfo);
149 	if (ct &&
150 	    ((tproto != IPPROTO_ICMPV6 &&
151 	      ctinfo == IP_CT_ESTABLISHED_REPLY) ||
152 	     (tproto == IPPROTO_ICMPV6 &&
153 	      ctinfo == IP_CT_RELATED_REPLY)) &&
154 	    (ct->status & IPS_SRC_NAT_DONE)) {
155 		daddr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.in6;
156 		dport = (tproto == IPPROTO_TCP) ?
157 			ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.tcp.port :
158 			ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
159 	}
160 #endif
161 
162 	return nf_socket_get_sock_v6(net, data_skb, doff, tproto, saddr, daddr,
163 				     sport, dport, indev);
164 }
165 EXPORT_SYMBOL_GPL(nf_sk_lookup_slow_v6);
166 
167 MODULE_LICENSE("GPL");
168 MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
169 MODULE_DESCRIPTION("Netfilter IPv6 socket lookup infrastructure");
170