npf_socket.c revision 1.2 1 1.1 joe /*-
2 1.1 joe * Copyright (c) 2025 Emmanuel Nyarko
3 1.1 joe *
4 1.1 joe * Redistribution and use in source and binary forms, with or without
5 1.1 joe * modification, are permitted provided that the following conditions
6 1.1 joe * are met:
7 1.1 joe * 1. Redistributions of source code must retain the above copyright
8 1.1 joe * notice, this list of conditions and the following disclaimer.
9 1.1 joe * 2. Redistributions in binary form must reproduce the above copyright
10 1.1 joe * notice, this list of conditions and the following disclaimer in the
11 1.1 joe * documentation and/or other materials provided with the distribution.
12 1.1 joe *
13 1.1 joe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
14 1.1 joe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15 1.1 joe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 1.1 joe * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
17 1.1 joe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 1.1 joe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 1.1 joe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 1.1 joe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 1.1 joe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 1.1 joe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 1.1 joe * POSSIBILITY OF SUCH DAMAGE.
24 1.1 joe */
25 1.1 joe
26 1.2 joe #ifdef _KERNEL
27 1.2 joe #include <sys/cdefs.h>
28 1.2 joe __KERNEL_RCSID(0, "$NetBSD: npf_socket.c,v 1.2 2025/06/02 11:57:05 joe Exp $");
29 1.2 joe
30 1.2 joe #include <sys/param.h>
31 1.2 joe #include <sys/types.h>
32 1.2 joe
33 1.1 joe #include <netinet/tcp.h>
34 1.1 joe #include <netinet/udp.h>
35 1.1 joe #include <netinet/in_pcb.h>
36 1.1 joe #include <sys/socketvar.h>
37 1.1 joe
38 1.1 joe #include <netinet/ip6.h>
39 1.1 joe #include <netinet6/ip6_var.h>
40 1.1 joe #include <netinet6/in6_pcb.h>
41 1.2 joe #endif
42 1.1 joe
43 1.1 joe #include "npf_impl.h"
44 1.1 joe
45 1.1 joe extern struct inpcbtable tcbtable; /* head of queue of active tcpcb's */
46 1.1 joe extern struct inpcbtable udbtable;
47 1.1 joe
48 1.1 joe static struct socket * npf_ip6_socket(npf_cache_t *, int);
49 1.1 joe static struct socket * npf_ip_socket(npf_cache_t *, int);
50 1.1 joe static int npf_match(uint8_t, uint32_t, uint32_t, uint32_t);
51 1.1 joe
52 1.1 joe /*
53 1.1 joe * NPF process socket module
54 1.1 joe */
55 1.1 joe
56 1.1 joe int
57 1.1 joe npf_match_rid(rid_t *rid, uint32_t uid_lookup)
58 1.1 joe {
59 1.1 joe return npf_match(rid->op, rid->id[0], rid->id[1], uid_lookup);
60 1.1 joe }
61 1.1 joe
62 1.1 joe static int
63 1.1 joe npf_match(uint8_t op, uint32_t rid1, uint32_t rid2, uint32_t id_lp)
64 1.1 joe {
65 1.1 joe switch (op) {
66 1.1 joe case NPF_OP_IRG:
67 1.1 joe return id_lp > rid1 && id_lp < rid2;
68 1.1 joe case NPF_OP_XRG:
69 1.1 joe return id_lp < rid1 || id_lp > rid2;
70 1.1 joe case NPF_OP_EQ:
71 1.1 joe return id_lp == rid1;
72 1.1 joe case NPF_OP_NE:
73 1.1 joe return id_lp != rid1;
74 1.1 joe case NPF_OP_LT:
75 1.1 joe return id_lp < rid1;
76 1.1 joe case NPF_OP_LE:
77 1.1 joe return id_lp <= rid1;
78 1.1 joe case NPF_OP_GT:
79 1.1 joe return id_lp > rid1;
80 1.1 joe case NPF_OP_GE:
81 1.1 joe return id_lp >= rid1;
82 1.1 joe }
83 1.1 joe return 0; /* never reached */
84 1.1 joe }
85 1.1 joe
86 1.1 joe int
87 1.1 joe npf_socket_lookup_rid(npf_cache_t *npc, get_rid_t get_rid, uint32_t *rid, int dir)
88 1.1 joe {
89 1.1 joe struct socket *so = NULL;
90 1.1 joe
91 1.1 joe KASSERT(npf_iscached(npc, NPC_IP46));
92 1.1 joe
93 1.1 joe if (npf_iscached(npc, NPC_IP4)) {
94 1.1 joe so = npf_ip_socket(npc, dir);
95 1.1 joe } else if (npf_iscached(npc, NPC_IP6)) {
96 1.1 joe so = npf_ip6_socket(npc, dir);
97 1.1 joe }
98 1.1 joe
99 1.1 joe if (so == NULL || so->so_cred == NULL)
100 1.1 joe return -1;
101 1.1 joe
102 1.1 joe *rid = get_rid(so->so_cred);
103 1.1 joe return 0;
104 1.1 joe }
105 1.1 joe
106 1.1 joe static struct socket *
107 1.1 joe npf_ip_socket(npf_cache_t *npc, int dir)
108 1.1 joe {
109 1.1 joe struct inpcbtable *tb = NULL;
110 1.1 joe struct in_addr saddr, daddr;
111 1.1 joe uint16_t sport, dport;
112 1.1 joe struct socket *so = NULL;
113 1.1 joe struct inpcb *inp = NULL;
114 1.1 joe
115 1.1 joe #define in_pcbhashlookup(tbl, saddr, sport, daddr, dport) \
116 1.1 joe inpcb_lookup(tbl, saddr, sport, daddr, dport, NULL)
117 1.1 joe #define in_pcblookup_listen(tbl, addr, port) \
118 1.1 joe inpcb_lookup_bound(tbl, addr, port)
119 1.1 joe
120 1.1 joe KASSERT(npf_iscached(npc, NPC_LAYER4));
121 1.1 joe KASSERT(npf_iscached(npc, NPC_IP4));
122 1.1 joe
123 1.1 joe struct tcphdr *tcp = npc->npc_l4.tcp;
124 1.1 joe struct udphdr *udp = npc->npc_l4.udp;
125 1.1 joe struct ip *ip = npc->npc_ip.v4;
126 1.1 joe
127 1.1 joe switch(npc->npc_proto) {
128 1.1 joe case IPPROTO_TCP:
129 1.1 joe sport = tcp->th_sport;
130 1.1 joe dport = tcp->th_dport;
131 1.1 joe tb = &tcbtable;
132 1.1 joe break;
133 1.1 joe case IPPROTO_UDP:
134 1.1 joe sport = udp->uh_sport;
135 1.1 joe dport = udp->uh_dport;
136 1.1 joe tb = &udbtable;
137 1.1 joe break;
138 1.1 joe default:
139 1.1 joe return NULL;
140 1.1 joe }
141 1.1 joe
142 1.1 joe if (dir == PFIL_IN) {
143 1.1 joe saddr = ip->ip_src;
144 1.1 joe daddr = ip->ip_dst;
145 1.1 joe } else {
146 1.1 joe uint16_t p_temp;
147 1.1 joe /* swap ports and addresses */
148 1.1 joe p_temp = sport;
149 1.1 joe sport = dport;
150 1.1 joe dport = p_temp;
151 1.1 joe saddr = ip->ip_dst;
152 1.1 joe daddr = ip->ip_src;
153 1.1 joe }
154 1.1 joe
155 1.1 joe inp = in_pcbhashlookup(tb, saddr, sport, daddr, dport);
156 1.1 joe if (inp == NULL) {
157 1.1 joe inp = in_pcblookup_listen(tb, daddr, dport);
158 1.1 joe if (inp == NULL) {
159 1.1 joe return NULL;
160 1.1 joe }
161 1.1 joe }
162 1.1 joe
163 1.1 joe so = inp->inp_socket;
164 1.1 joe return so;
165 1.1 joe }
166 1.1 joe
167 1.1 joe static struct socket *
168 1.1 joe npf_ip6_socket(npf_cache_t *npc, int dir)
169 1.1 joe {
170 1.1 joe struct inpcbtable *tb = NULL;
171 1.1 joe const struct in6_addr *s6addr, *d6addr;
172 1.1 joe uint16_t sport, dport;
173 1.1 joe struct inpcb *in6p = NULL;
174 1.1 joe struct socket *so = NULL;
175 1.1 joe
176 1.1 joe #define in6_pcbhashlookup(tbl, saddr, sport, daddr, dport) \
177 1.1 joe in6pcb_lookup(tbl, saddr, sport, daddr, dport, 0, NULL)
178 1.1 joe
179 1.1 joe #define in6_pcblookup_listen(tbl, addr, port) \
180 1.1 joe in6pcb_lookup_bound(tbl, addr, port, 0)
181 1.1 joe
182 1.1 joe KASSERT(npf_iscached(npc, NPC_LAYER4));
183 1.1 joe KASSERT(npf_iscached(npc, NPC_IP6));
184 1.1 joe
185 1.1 joe struct tcphdr *tcp = npc->npc_l4.tcp;
186 1.1 joe struct udphdr *udp = npc->npc_l4.udp;
187 1.1 joe struct ip6_hdr *ip6 = npc->npc_ip.v6;
188 1.1 joe
189 1.1 joe switch(npc->npc_proto) {
190 1.1 joe case IPPROTO_TCP:
191 1.1 joe sport = tcp->th_sport;
192 1.1 joe dport = tcp->th_dport;
193 1.1 joe tb = &tcbtable;
194 1.1 joe break;
195 1.1 joe case IPPROTO_UDP:
196 1.1 joe sport = udp->uh_sport;
197 1.1 joe dport = udp->uh_dport;
198 1.1 joe tb = &udbtable;
199 1.1 joe break;
200 1.1 joe default:
201 1.1 joe return NULL;
202 1.1 joe }
203 1.1 joe
204 1.1 joe if (dir == PFIL_IN) {
205 1.1 joe s6addr = &ip6->ip6_src;
206 1.1 joe d6addr = &ip6->ip6_dst;
207 1.1 joe } else {
208 1.1 joe uint16_t p_temp;
209 1.1 joe /* swap ports and addresses */
210 1.1 joe p_temp = sport;
211 1.1 joe sport = dport;
212 1.1 joe dport = p_temp;
213 1.1 joe s6addr = &ip6->ip6_dst;
214 1.1 joe d6addr = &ip6->ip6_src;
215 1.1 joe }
216 1.1 joe in6p = in6_pcbhashlookup(tb, s6addr, sport, d6addr,
217 1.1 joe dport);
218 1.1 joe if (in6p == NULL) {
219 1.1 joe in6p = in6_pcblookup_listen(tb, d6addr, dport);
220 1.1 joe if (in6p == NULL)
221 1.1 joe return NULL;
222 1.1 joe }
223 1.1 joe so = in6p->inp_socket;
224 1.1 joe return so;
225 1.1 joe }
226