Debian Codegraph @

net/ipv6/netfilter/nf_socket_ipv6.c

raw
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
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 *
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, skb, doff, saddr, sport, daddr, dport,
87 in->ifindex);
88 case IPPROTO_UDP:
89 return udp6_lib_lookup(net, saddr, sport, daddr, dport,
90 in->ifindex);
91 }
92
93 return NULL;
94 }
95
96 struct sock *nf_sk_lookup_slow_v6(struct net *net, const struct sk_buff *skb,
97 const struct net_device *indev)
98 {
99 __be16 dport, sport;
100 const struct in6_addr *daddr = NULL, *saddr = NULL;
101 struct ipv6hdr *iph = ipv6_hdr(skb), ipv6_var;
102 struct sk_buff *data_skb = NULL;
103 int doff = 0;
104 int thoff = 0, tproto;
105 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
106 enum ip_conntrack_info ctinfo;
107 struct nf_conn const *ct;
108 #endif
109
110 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
111 if (tproto < 0) {
112 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
113 return NULL;
114 }
115
116 if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
117 struct tcphdr _hdr;
118 struct udphdr *hp;
119
120 hp = skb_header_pointer(skb, thoff, tproto == IPPROTO_UDP ?
121 sizeof(*hp) : sizeof(_hdr), &_hdr);
122 if (hp == NULL)
123 return NULL;
124
125 saddr = &iph->saddr;
126 sport = hp->source;
127 daddr = &iph->daddr;
128 dport = hp->dest;
129 data_skb = (struct sk_buff *)skb;
130 doff = tproto == IPPROTO_TCP ?
131 thoff + __tcp_hdrlen((struct tcphdr *)hp) :
132 thoff + sizeof(*hp);
133
134 } else if (tproto == IPPROTO_ICMPV6) {
135 if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr,
136 &sport, &dport, &ipv6_var))
137 return NULL;
138 } else {
139 return NULL;
140 }
141
142 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
143 /* Do the lookup with the original socket address in
144 * case this is a reply packet of an established
145 * SNAT-ted connection.
146 */
147 ct = nf_ct_get(skb, &ctinfo);
148 if (ct &&
149 ((tproto != IPPROTO_ICMPV6 &&
150 ctinfo == IP_CT_ESTABLISHED_REPLY) ||
151 (tproto == IPPROTO_ICMPV6 &&
152 ctinfo == IP_CT_RELATED_REPLY)) &&
153 (ct->status & IPS_SRC_NAT_DONE)) {
154 daddr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.in6;
155 dport = (tproto == IPPROTO_TCP) ?
156 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.tcp.port :
157 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
158 }
159 #endif
160
161 return nf_socket_get_sock_v6(net, data_skb, doff, tproto, saddr, daddr,
162 sport, dport, indev);
163 }
164 EXPORT_SYMBOL_GPL(nf_sk_lookup_slow_v6);
165
166 MODULE_LICENSE("GPL");
167 MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
168 MODULE_DESCRIPTION("Netfilter IPv6 socket lookup infrastructure");