File:  [DragonFly] / src / sys / netinet / ip_demux.c
Revision 1.12: download - view: text, annotated - select for diffs
Thu Apr 1 23:04:50 2004 UTC (10 years, 3 months ago) by hsu
Branches: MAIN
CVS tags: HEAD
Consolidate length checks in ip_demux().

    1: /*
    2:  * Copyright (c) 2003 Jeffrey Hsu
    3:  * All rights reserved.
    4:  *
    5:  * $DragonFly: src/sys/netinet/ip_demux.c,v 1.12 2004/04/01 23:04:50 hsu Exp $
    6:  */
    7: 
    8: #include "opt_inet.h"
    9: 
   10: #include <sys/param.h>
   11: #include <sys/systm.h>
   12: #include <sys/kernel.h>
   13: #include <sys/socket.h>
   14: #include <sys/socketvar.h>
   15: #include <sys/thread.h>
   16: #include <sys/sysctl.h>
   17: 
   18: #include <net/if.h>
   19: #include <net/netisr.h>
   20: 
   21: #include <netinet/in_systm.h>
   22: #include <netinet/in.h>
   23: #include <netinet/in_var.h>
   24: #include <netinet/in_pcb.h>
   25: #include <netinet/ip.h>
   26: #include <netinet/ip_var.h>
   27: #include <netinet/tcp.h>
   28: #include <netinet/tcpip.h>
   29: #include <netinet/tcp_var.h>
   30: #include <netinet/udp.h>
   31: #include <netinet/udp_var.h>
   32: 
   33: extern struct thread netisr_cpu[];
   34: 
   35: static struct thread tcp_thread[MAXCPU];
   36: static struct thread udp_thread[MAXCPU];
   37: 
   38: static __inline int
   39: INP_MPORT_HASH(in_addr_t src, in_addr_t dst, in_port_t sport, in_port_t dport)
   40: {
   41: 	/*
   42: 	 * Use low order bytes.
   43: 	 */
   44: 
   45: #if (BYTE_ORDER == LITTLE_ENDIAN)
   46: 	KASSERT(ncpus2 < 256, ("need different hash function"));  /* XXX JH */
   47: 	return (((src >> 24) ^ (sport >> 8) ^ (dst >> 24) ^ (dport >> 8)) &
   48: 		ncpus2_mask);
   49: #else
   50: 	return ((src ^ sport ^ dst ^ dport) & ncpus2_mask);
   51: #endif
   52: }
   53: 
   54: /*
   55:  * Map a packet to a protocol processing thread.
   56:  */
   57: lwkt_port_t
   58: ip_mport(struct mbuf *m)
   59: {
   60: 	struct ip *ip;
   61: 	int iphlen;
   62: 	struct tcphdr *th;
   63: 	struct udphdr *uh;
   64: 	int thoff;				/* TCP data offset */
   65: 	lwkt_port_t port;
   66: 	int cpu;
   67: 
   68: 	if (m->m_pkthdr.len < sizeof(struct ip)) {
   69: 		ipstat.ips_tooshort++;
   70: 		return (NULL);
   71: 	}
   72: 
   73: 	if (m->m_len < sizeof(struct ip) &&
   74: 	    (m = m_pullup(m, sizeof(struct ip))) == NULL) {
   75: 		ipstat.ips_toosmall++;
   76: 		return (NULL);
   77: 	}
   78: 
   79: 	ip = mtod(m, struct ip *);
   80: 
   81: 	/*
   82: 	 * XXX generic packet handling defrag on CPU 0 for now.
   83: 	 */
   84: 	if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK))
   85: 		return (&netisr_cpu[0].td_msgport);
   86: 
   87: 	iphlen = ip->ip_hl << 2;
   88: 	if (iphlen < sizeof(struct ip)) {	/* minimum header length */
   89: 		ipstat.ips_badhlen++;
   90: 		return (NULL);
   91: 	}
   92: 
   93: 	switch (ip->ip_p) {
   94: 	case IPPROTO_TCP:
   95: 		if (m->m_len < iphlen + sizeof(struct tcphdr) &&
   96: 		    (m = m_pullup(m, iphlen + sizeof(struct tcphdr))) == NULL) {
   97: 			tcpstat.tcps_rcvshort++;
   98: 			return (NULL);
   99: 		}
  100: 		th = (struct tcphdr *)((caddr_t)ip + iphlen);
  101: 		thoff = th->th_off << 2;
  102: 		if (thoff < sizeof(struct tcphdr) || thoff > ip->ip_len) {
  103: 			tcpstat.tcps_rcvbadoff++;
  104: 			return (NULL);
  105: 		}
  106: 		if (m->m_len < iphlen + thoff) {
  107: 			m = m_pullup(m, iphlen + thoff);
  108: 			if (m == NULL) {
  109: 				tcpstat.tcps_rcvshort++;
  110: 				return (NULL);
  111: 			}
  112: 			ip = mtod(m, struct ip *);
  113: 			th = (struct tcphdr *)((caddr_t)ip + iphlen);
  114: 		}
  115: 
  116: 		cpu = INP_MPORT_HASH(ip->ip_src.s_addr, ip->ip_dst.s_addr,
  117: 		    th->th_sport, th->th_dport);
  118: 		port = &tcp_thread[cpu].td_msgport;
  119: 		break;
  120: 	case IPPROTO_UDP:
  121: 		if (m->m_len < iphlen + sizeof(struct udphdr)) {
  122: 			m = m_pullup(m, iphlen + sizeof(struct udphdr));
  123: 			if (m == NULL) {
  124: 				udpstat.udps_hdrops++;
  125: 				return (NULL);
  126: 			}
  127: 			ip = mtod(m, struct ip *);
  128: 		}
  129: 		uh = (struct udphdr *)((caddr_t)ip + iphlen);
  130: 
  131: 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
  132: 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
  133: 			cpu = 0;
  134: 		} else {
  135: 			cpu = INP_MPORT_HASH(ip->ip_src.s_addr,
  136: 			    ip->ip_dst.s_addr, uh->uh_sport, uh->uh_dport);
  137: 		}
  138: 		port = &udp_thread[cpu].td_msgport;
  139: 		break;
  140: 	default:
  141: 		if (m->m_len < iphlen && (m = m_pullup(m, iphlen)) == NULL) {
  142: 			ipstat.ips_badhlen++;
  143: 			return (NULL);
  144: 		}
  145: 		port = &netisr_cpu[0].td_msgport;
  146: 		break;
  147: 	}
  148: 	KKASSERT(port->mp_putport != NULL);
  149: 
  150: 	return (port);
  151: }
  152: 
  153: /*
  154:  * Map a TCP socket to a protocol processing thread.
  155:  */
  156: lwkt_port_t
  157: tcp_soport(struct socket *so, struct sockaddr *nam)
  158: {
  159: 	struct inpcb *inp;
  160: 
  161: 	/*
  162: 	 * The following processing all take place on Protocol Thread 0:
  163: 	 *   only bind() and connect() have a non-null nam parameter
  164: 	 *   attach() has a null socket parameter
  165: 	 *   Fast and slow timeouts pass in two NULLs
  166: 	 */
  167: 	if (nam != NULL || so == NULL)
  168: 		return (&tcp_thread[0].td_msgport);
  169: 
  170: 	/*
  171: 	 * Already bound and connected.  For TCP connections, the
  172: 	 * (faddr, fport, laddr, lport) association cannot change now.
  173: 	 *
  174: 	 * Note: T/TCP code needs some reorganization to fit into
  175: 	 * this model.  XXX JH
  176: 	 */
  177: 	inp = sotoinpcb(so);
  178: 	if (!inp)		/* connection reset by peer */
  179: 		return (&tcp_thread[0].td_msgport);
  180: 
  181: 	/*
  182: 	 * Rely on type-stable memory and check in protocol handler
  183: 	 * to fix race condition here w/ deallocation of inp.  XXX JH
  184: 	 */
  185: 
  186: 	return (&tcp_thread[INP_MPORT_HASH(inp->inp_laddr.s_addr,
  187: 	    inp->inp_faddr.s_addr, inp->inp_lport,
  188: 	    inp->inp_fport)].td_msgport);
  189: }
  190: 
  191: /*
  192:  * Map a UDP socket to a protocol processing thread.
  193:  */
  194: lwkt_port_t
  195: udp_soport(struct socket *so, struct sockaddr *nam)
  196: {
  197: 	struct inpcb *inp;
  198: 
  199: 	/*
  200: 	 * The following processing all take place on Protocol Thread 0:
  201: 	 *   only bind() and connect() have a non-null nam parameter
  202: 	 *   attach() has a null socket parameter
  203: 	 *   Fast and slow timeouts pass in two NULLs
  204: 	 */
  205: 	if (nam != NULL || so == NULL)
  206: 		return (&udp_thread[0].td_msgport);
  207: 
  208: 	inp = sotoinpcb(so);
  209: 
  210: 	if (IN_MULTICAST(ntohl(inp->inp_laddr.s_addr)))
  211: 		return (&udp_thread[0].td_msgport);
  212: 
  213: 	/*
  214: 	 * Rely on type-stable memory and check in protocol handler
  215: 	 * to fix race condition here w/ deallocation of inp.  XXX JH
  216: 	 */
  217: 
  218: 	return (&udp_thread[INP_MPORT_HASH(inp->inp_laddr.s_addr,
  219: 	    inp->inp_faddr.s_addr, inp->inp_lport,
  220: 	    inp->inp_fport)].td_msgport);
  221: }
  222: 
  223: /*
  224:  * Map a network address to a processor.
  225:  */
  226: int
  227: tcp_addrcpu(in_addr_t src, in_port_t sport, in_addr_t dst, in_port_t dport)
  228: {
  229: 	return (INP_MPORT_HASH(src, dst, sport, dport));
  230: }
  231: 
  232: int
  233: udp_addrcpu(in_addr_t src, in_port_t sport, in_addr_t dst, in_port_t dport)
  234: {
  235: 	if (IN_MULTICAST(ntohl(dst)))
  236: 		return (0);
  237: 	else
  238: 		return (INP_MPORT_HASH(src, dst, sport, dport));
  239: }
  240: 
  241: /*
  242:  * Return LWKT port for cpu.
  243:  */
  244: lwkt_port_t
  245: tcp_cport(int cpu)
  246: {
  247: 	return (&tcp_thread[cpu].td_msgport);
  248: }
  249: 
  250: void
  251: tcp_thread_init(void)
  252: {
  253: 	int cpu;
  254: 
  255: 	for (cpu = 0; cpu < ncpus2; cpu++) {
  256: 		lwkt_create(netmsg_service_loop, NULL, NULL, 
  257: 			&tcp_thread[cpu], 0, cpu, "tcp_thread %d", cpu);
  258: 	}
  259: }
  260: 
  261: void
  262: udp_thread_init(void)
  263: {
  264: 	int cpu;
  265: 
  266: 	for (cpu = 0; cpu < ncpus2; cpu++) {
  267: 		lwkt_create(netmsg_service_loop, NULL, NULL,
  268: 			&udp_thread[cpu], 0, cpu, "udp_thread %d", cpu);
  269: 	}
  270: }