netisr: Add hashcheck method to check and adjust hw provided hash
[dragonfly.git] / sys / netinet / ip_demux.c
CommitLineData
bf82f9b7 1/*
380d2ea3
JH
2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved.
f23061d4 4 *
380d2ea3
JH
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
f23061d4 7 *
380d2ea3
JH
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
f23061d4 19 *
380d2ea3
JH
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
bf82f9b7 32 *
14572273 33 * $DragonFly: src/sys/netinet/ip_demux.c,v 1.45 2008/11/11 10:46:58 sephe Exp $
bf82f9b7
MD
34 */
35
36#include "opt_inet.h"
e99d9a39 37#include "opt_rss.h"
bf82f9b7
MD
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/socket.h>
43#include <sys/socketvar.h>
44#include <sys/thread.h>
45#include <sys/sysctl.h>
2b57d013 46#include <sys/globaldata.h>
bf82f9b7
MD
47
48#include <net/if.h>
49#include <net/netisr.h>
6d5eef1e 50#include <net/toeplitz2.h>
bf82f9b7
MD
51
52#include <netinet/in_systm.h>
53#include <netinet/in.h>
54#include <netinet/in_var.h>
55#include <netinet/in_pcb.h>
56#include <netinet/ip.h>
57#include <netinet/ip_var.h>
58#include <netinet/tcp.h>
59#include <netinet/tcpip.h>
60#include <netinet/tcp_var.h>
61#include <netinet/udp.h>
62#include <netinet/udp_var.h>
63
92db3805 64extern int udp_mpsafe_thread;
bf82f9b7 65
c3c96e44
MD
66/*
67 * Toeplitz hash functions - the idea is to match the hardware.
68 */
f861aec9
SZ
69static __inline int
70INP_MPORT_HASH_UDP(in_addr_t faddr, in_addr_t laddr,
71 in_port_t fport, in_port_t lport)
72{
b73d4152 73 return toeplitz_hash(toeplitz_rawhash_addr(faddr, laddr));
f861aec9
SZ
74}
75
76static __inline int
77INP_MPORT_HASH_TCP(in_addr_t faddr, in_addr_t laddr,
78 in_port_t fport, in_port_t lport)
79{
b73d4152
SZ
80 return toeplitz_hash(
81 toeplitz_rawhash_addrport(faddr, laddr, fport, lport));
f861aec9
SZ
82}
83
e9f5b82f 84/*
c3c96e44
MD
85 * Map a network address to a processor.
86 */
87int
88tcp_addrcpu(in_addr_t faddr, in_port_t fport, in_addr_t laddr, in_port_t lport)
89{
90 return (INP_MPORT_HASH_TCP(faddr, laddr, fport, lport));
91}
92
d8d6714a
MD
93/*
94 * Not implemented yet, use protocol thread 0
95 */
c3c96e44
MD
96int
97udp_addrcpu(in_addr_t faddr, in_port_t fport, in_addr_t laddr, in_port_t lport)
98{
4a9a1ec1
SZ
99#ifdef notyet
100 return (INP_MPORT_HASH_UDP(faddr, laddr, fport, lport));
101#else
d8d6714a 102 return 0;
4a9a1ec1 103#endif
c3c96e44
MD
104}
105
106/*
e9f5b82f
SZ
107 * If the packet is a valid IP datagram, upon returning of this function
108 * following things are promised:
109 *
dc6a6a0e
SZ
110 * o IP header (including any possible IP options) and any data preceding
111 * IP header (usually linker layer header) are in one mbuf (m_len).
e9f5b82f
SZ
112 * o IP header length is not less than the minimum (sizeof(struct ip)).
113 * o IP total length is not less than IP header length.
9b161cc2
SZ
114 * o IP datagram resides completely in the mbuf chain,
115 * i.e. pkthdr.len >= IP total length.
e9f5b82f
SZ
116 *
117 * If the packet is a UDP datagram,
118 * o IP header (including any possible IP options) and UDP header are in
119 * one mbuf (m_len).
120 * o IP total length is not less than (IP header length + UDP header length).
121 *
122 * If the packet is a TCP segment,
123 * o IP header (including any possible IP options) and TCP header (including
124 * any possible TCP options) are in one mbuf (m_len).
125 * o TCP header length is not less than the minimum (sizeof(struct tcphdr)).
126 * o IP total length is not less than (IP header length + TCP header length).
127 */
1e316d14 128boolean_t
c3c96e44 129ip_lengthcheck(struct mbuf **mp, int hoff)
bf82f9b7 130{
1e316d14 131 struct mbuf *m = *mp;
55d829f8 132 struct ip *ip;
c3c96e44 133 int len, iphlen, iplen;
bf82f9b7 134 struct tcphdr *th;
55d829f8 135 int thoff; /* TCP data offset */
bf82f9b7 136
c3c96e44
MD
137 len = hoff + sizeof(struct ip);
138
ead1d3cb 139 /* The packet must be at least the size of an IP header. */
c3c96e44 140 if (m->m_pkthdr.len < len) {
55d829f8 141 ipstat.ips_tooshort++;
dd4df7e9 142 goto fail;
55d829f8
JH
143 }
144
ead1d3cb 145 /* The fixed IP header must reside completely in the first mbuf. */
c3c96e44
MD
146 if (m->m_len < len) {
147 m = m_pullup(m, len);
ead1d3cb
JH
148 if (m == NULL) {
149 ipstat.ips_toosmall++;
dd4df7e9 150 goto fail;
ead1d3cb 151 }
bf82f9b7 152 }
ead1d3cb 153
c3c96e44 154 ip = mtodoff(m, struct ip *, hoff);
55d829f8 155
ead1d3cb 156 /* Bound check the packet's stated IP header length. */
9eeaa8a9 157 iphlen = ip->ip_hl << 2;
9babcab8
JH
158 if (iphlen < sizeof(struct ip)) { /* minimum header length */
159 ipstat.ips_badhlen++;
dd4df7e9 160 goto fail;
9babcab8 161 }
ead1d3cb
JH
162
163 /* The full IP header must reside completely in the one mbuf. */
c3c96e44
MD
164 if (m->m_len < hoff + iphlen) {
165 m = m_pullup(m, hoff + iphlen);
b01ae44a
MD
166 if (m == NULL) {
167 ipstat.ips_badhlen++;
dd4df7e9 168 goto fail;
b01ae44a 169 }
c3c96e44 170 ip = mtodoff(m, struct ip *, hoff);
b01ae44a
MD
171 }
172
ead1d3cb
JH
173 iplen = ntohs(ip->ip_len);
174
175 /*
9b161cc2
SZ
176 * Check that the amount of data in the buffers is as
177 * at least much as the IP header would have us expect.
178 */
c3c96e44 179 if (m->m_pkthdr.len < hoff + iplen) {
9b161cc2
SZ
180 ipstat.ips_tooshort++;
181 goto fail;
182 }
183
184 /*
ead1d3cb
JH
185 * Fragments other than the first fragment don't have much
186 * length information.
187 */
188 if (ntohs(ip->ip_off) & IP_OFFMASK)
189 goto ipcheckonly;
190
b01ae44a
MD
191 /*
192 * The TCP/IP or UDP/IP header must be entirely contained within
193 * the first fragment of a packet. Packet filters will break if they
194 * aren't.
ee7990a0
MD
195 *
196 * Since the packet will be trimmed to ip_len we must also make sure
197 * the potentially trimmed down length is still sufficient to hold
198 * the header(s).
b01ae44a 199 */
ead1d3cb
JH
200 switch (ip->ip_p) {
201 case IPPROTO_TCP:
202 if (iplen < iphlen + sizeof(struct tcphdr)) {
203 ++tcpstat.tcps_rcvshort;
dd4df7e9 204 goto fail;
ead1d3cb 205 }
c3c96e44
MD
206 if (m->m_len < hoff + iphlen + sizeof(struct tcphdr)) {
207 m = m_pullup(m, hoff + iphlen + sizeof(struct tcphdr));
ead1d3cb
JH
208 if (m == NULL) {
209 tcpstat.tcps_rcvshort++;
dd4df7e9 210 goto fail;
ee7990a0 211 }
c3c96e44 212 ip = mtodoff(m, struct ip *, hoff);
b01ae44a 213 }
9eeaa8a9 214 th = (struct tcphdr *)((caddr_t)ip + iphlen);
55d829f8 215 thoff = th->th_off << 2;
096deca3 216 if (thoff < sizeof(struct tcphdr) ||
cd5e5855 217 thoff + iphlen > ntohs(ip->ip_len)) {
9eeaa8a9 218 tcpstat.tcps_rcvbadoff++;
dd4df7e9 219 goto fail;
9eeaa8a9 220 }
c3c96e44
MD
221 if (m->m_len < hoff + iphlen + thoff) {
222 m = m_pullup(m, hoff + iphlen + thoff);
55d829f8
JH
223 if (m == NULL) {
224 tcpstat.tcps_rcvshort++;
dd4df7e9 225 goto fail;
55d829f8 226 }
bf82f9b7 227 }
1e316d14 228 break;
ead1d3cb
JH
229 case IPPROTO_UDP:
230 if (iplen < iphlen + sizeof(struct udphdr)) {
231 ++udpstat.udps_hdrops;
dd4df7e9 232 goto fail;
ead1d3cb 233 }
c3c96e44
MD
234 if (m->m_len < hoff + iphlen + sizeof(struct udphdr)) {
235 m = m_pullup(m, hoff + iphlen + sizeof(struct udphdr));
ead1d3cb
JH
236 if (m == NULL) {
237 udpstat.udps_hdrops++;
dd4df7e9 238 goto fail;
ead1d3cb
JH
239 }
240 }
241 break;
242 default:
243ipcheckonly:
244 if (iplen < iphlen) {
245 ++ipstat.ips_badlen;
dd4df7e9 246 goto fail;
ead1d3cb
JH
247 }
248 break;
1e316d14
JH
249 }
250
8697599b 251 m->m_flags |= M_LENCHECKED;
1e316d14
JH
252 *mp = m;
253 return TRUE;
dd4df7e9
SZ
254
255fail:
256 if (m != NULL)
257 m_freem(m);
258 *mp = NULL;
259 return FALSE;
1e316d14 260}
bf82f9b7 261
1e316d14 262/*
c3c96e44
MD
263 * Assign a protocol processing thread to a packet. The IP header is at
264 * offset (hoff) in the packet (i.e. the mac header might still be intact).
265 *
266 * This function can blow away the mbuf if the packet is malformed.
1e316d14 267 */
c3c96e44
MD
268void
269ip_cpufn(struct mbuf **mptr, int hoff, int dir)
1e316d14
JH
270{
271 struct ip *ip;
272 int iphlen;
273 struct tcphdr *th;
274 struct udphdr *uh;
275 struct mbuf *m;
276 int thoff; /* TCP data offset */
1e316d14
JH
277 int cpu;
278
c3c96e44
MD
279 if (!ip_lengthcheck(mptr, hoff))
280 return;
1e316d14
JH
281
282 m = *mptr;
c3c96e44 283 ip = mtodoff(m, struct ip *, hoff);
1e316d14
JH
284 iphlen = ip->ip_hl << 2;
285
286 /*
287 * XXX generic packet handling defrag on CPU 0 for now.
288 */
97a43e72
SZ
289 if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) {
290 cpu = 0;
97a43e72
SZ
291 goto back;
292 }
1e316d14
JH
293
294 switch (ip->ip_p) {
295 case IPPROTO_TCP:
296 th = (struct tcphdr *)((caddr_t)ip + iphlen);
297 thoff = th->th_off << 2;
9389fe19
MD
298 cpu = INP_MPORT_HASH_TCP(ip->ip_src.s_addr,
299 ip->ip_dst.s_addr,
300 th->th_sport,
301 th->th_dport);
bf82f9b7 302 break;
f861aec9 303
bf82f9b7 304 case IPPROTO_UDP:
9eeaa8a9
JH
305 uh = (struct udphdr *)((caddr_t)ip + iphlen);
306
9389fe19
MD
307 cpu = INP_MPORT_HASH_UDP(ip->ip_src.s_addr,
308 ip->ip_dst.s_addr,
309 uh->uh_sport,
310 uh->uh_dport);
bf82f9b7 311 break;
f861aec9 312
bf82f9b7 313 default:
97a43e72 314 cpu = 0;
bf82f9b7
MD
315 break;
316 }
97a43e72
SZ
317back:
318 m->m_flags |= M_HASH;
319 m->m_pkthdr.hash = cpu;
bf82f9b7
MD
320}
321
c3c96e44
MD
322void
323ip_cpufn_in(struct mbuf **mptr, int hoff)
934c6849 324{
c3c96e44 325 ip_cpufn(mptr, hoff, IP_MPORT_IN);
934c6849
SZ
326}
327
9eeaa8a9 328/*
e6f77b88
SZ
329 * Verify and adjust the hash value of the packet.
330 *
c3c96e44 331 * Unlike ip_cpufn(), the packet content is not accessed. The packet info
e6f77b88 332 * (pi) and the hash of the packet (m_pkthdr.hash) is used instead.
8a5c0ed6
SZ
333 *
334 * Caller has already made sure that m_pkthdr.hash is valid, i.e. m_flags
335 * has M_HASH set.
336 */
e6f77b88
SZ
337void
338ip_hashcheck(struct mbuf *m, const struct pktinfo *pi)
8a5c0ed6 339{
e6f77b88 340 KASSERT((m->m_flags & M_HASH), ("no valid packet hash\n"));
8a5c0ed6
SZ
341 KASSERT(m->m_pkthdr.hash < ncpus2,
342 ("invalid packet hash %#x\n", m->m_pkthdr.hash));
343
344 /*
345 * XXX generic packet handling defrag on CPU 0 for now.
346 */
347 if (pi->pi_flags & PKTINFO_FLAG_FRAG) {
348 m->m_pkthdr.hash = 0;
e6f77b88 349 return;
8a5c0ed6
SZ
350 }
351
352 switch (pi->pi_l3proto) {
353 case IPPROTO_TCP:
8a5c0ed6 354 case IPPROTO_UDP:
8a5c0ed6
SZ
355 break;
356
357 default:
e6f77b88
SZ
358 /* Let software calculate the hash */
359 m->m_flags &= ~M_HASH;
8a5c0ed6
SZ
360 break;
361 }
8a5c0ed6
SZ
362}
363
364/*
48e7b118
MD
365 * This is used to map a socket to a message port for sendmsg() and friends.
366 * It is not called for any other purpose. In the case of TCP we just return
367 * the port already installed in the socket.
368 */
369lwkt_port_t
370tcp_soport(struct socket *so, struct sockaddr *nam,
371 struct mbuf **dummy __unused)
372{
373 return(so->so_port);
bf82f9b7
MD
374}
375
e3873585
SZ
376/*
377 * Used to route icmp messages to the proper protocol thread for ctlinput
378 * operation.
379 */
380lwkt_port_t
381tcp_ctlport(int cmd, struct sockaddr *sa, void *vip)
382{
383 struct ip *ip = vip;
384 struct tcphdr *th;
385 struct in_addr faddr;
386 int cpu;
387
388 faddr = ((struct sockaddr_in *)sa)->sin_addr;
389 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
390 return(NULL);
14572273
SZ
391 if (ip == NULL || PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
392 /*
8e0ec33b
SZ
393 * A new message will be allocated later to save necessary
394 * information and will be forwarded to all network protocol
395 * threads in the following way:
14572273 396 *
8e0ec33b
SZ
397 * (the the thread owns the msgport that we return here)
398 * netisr0 <--+
399 * | |
400 * | |
401 * | |
402 * +-------+
403 * sendmsg
404 * [msg is kmalloc()ed]
405 *
406 *
407 * Later on, when the msg is received by netisr0:
408 *
409 * forwardmsg forwardmsg
410 * netisr0 ---------> netisr1 ---------> netisrN
411 * [msg is kfree()ed]
14572273
SZ
412 */
413 return cpu0_ctlport(cmd, sa, vip);
e3873585
SZ
414 } else {
415 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
416 cpu = tcp_addrcpu(faddr.s_addr, th->th_dport,
417 ip->ip_src.s_addr, th->th_sport);
418 }
c3c96e44 419 return(cpu_portfn(cpu));
e3873585
SZ
420}
421
7fe56515
JH
422lwkt_port_t
423tcp_addrport(in_addr_t faddr, in_port_t fport, in_addr_t laddr, in_port_t lport)
424{
c3c96e44 425 return(cpu_portfn(tcp_addrcpu(faddr, fport, laddr, lport)));
7fe56515
JH
426}
427
65f3e756
MD
428lwkt_port_t
429tcp_addrport0(void)
430{
c3c96e44 431 return(cpu_portfn(0));
65f3e756
MD
432}
433
48e7b118
MD
434lwkt_port_t
435udp_addrport(in_addr_t faddr, in_port_t fport, in_addr_t laddr, in_port_t lport)
436{
c3c96e44 437 return(cpu_portfn(udp_addrcpu(faddr, fport, laddr, lport)));
48e7b118
MD
438}
439
9eeaa8a9 440/*
e3873585
SZ
441 * Used to route icmp messages to the proper protocol thread for ctlinput
442 * operation.
443 */
444lwkt_port_t
445udp_ctlport(int cmd, struct sockaddr *sa, void *vip)
446{
447 struct ip *ip = vip;
448 struct udphdr *uh;
449 struct in_addr faddr;
450 int cpu;
451
452 faddr = ((struct sockaddr_in *)sa)->sin_addr;
453 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
454 return(NULL);
14572273
SZ
455 if (PRC_IS_REDIRECT(cmd)) {
456 /*
457 * See the comment in tcp_ctlport; the only difference
458 * is that message is forwarded to UDP protocol theads.
459 */
460 return cpu0_ctlport(cmd, sa, vip);
461 } else if (ip == NULL || cmd == PRC_HOSTDEAD) {
462 /*
463 * XXX
464 * Once UDP inpcbs are CPU localized, we should do
465 * the same forwarding as PRC_IS_REDIRECT(cmd)
466 */
e3873585
SZ
467 cpu = 0;
468 } else {
469 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
470
4a9a1ec1
SZ
471 cpu = udp_addrcpu(faddr.s_addr, ip->ip_src.s_addr,
472 uh->uh_dport, uh->uh_sport);
e3873585 473 }
c3c96e44 474 return (cpu_portfn(cpu));
bf82f9b7 475}