version 1.14, 1994/06/29 06:38:57 |
version 1.26, 1996/01/31 03:49:38 |
|
|
|
|
#include <netinet/in.h> |
#include <netinet/in.h> |
#include <netinet/in_systm.h> |
#include <netinet/in_systm.h> |
|
#include <netinet/in_var.h> |
#include <netinet/ip.h> |
#include <netinet/ip.h> |
#include <netinet/in_pcb.h> |
#include <netinet/in_pcb.h> |
#include <netinet/ip_var.h> |
#include <netinet/ip_var.h> |
Line 67 int udpcksum = 0; /* XXX */ |
|
Line 68 int udpcksum = 0; /* XXX */ |
|
#endif |
#endif |
|
|
struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; |
struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; |
struct inpcb *udp_last_inpcb = &udb; |
|
|
|
static void udp_detach __P((struct inpcb *)); |
static void udp_detach __P((struct inpcb *)); |
static void udp_notify __P((struct inpcb *, int)); |
static void udp_notify __P((struct inpcb *, int)); |
static struct mbuf *udp_saveopt __P((caddr_t, int, int)); |
static struct mbuf *udp_saveopt __P((caddr_t, int, int)); |
|
|
|
#ifndef UDBHASHSIZE |
|
#define UDBHASHSIZE 128 |
|
#endif |
|
int udbhashsize = UDBHASHSIZE; |
|
|
void |
void |
udp_init() |
udp_init() |
{ |
{ |
udb.inp_next = udb.inp_prev = &udb; |
|
|
in_pcbinit(&udbtable, udbhashsize); |
} |
} |
|
|
void |
void |
Line 121 udp_input(m, iphlen) |
|
Line 127 udp_input(m, iphlen) |
|
* Make mbuf data length reflect UDP length. |
* Make mbuf data length reflect UDP length. |
* If not enough data to reflect UDP length, drop. |
* If not enough data to reflect UDP length, drop. |
*/ |
*/ |
len = ntohs((u_short)uh->uh_ulen); |
len = ntohs((u_int16_t)uh->uh_ulen); |
if (ip->ip_len != len) { |
if (ip->ip_len != len) { |
if (len > ip->ip_len) { |
if (len > ip->ip_len) { |
udpstat.udps_badlen++; |
udpstat.udps_badlen++; |
Line 140 udp_input(m, iphlen) |
|
Line 146 udp_input(m, iphlen) |
|
* Checksum extended UDP header and data. |
* Checksum extended UDP header and data. |
*/ |
*/ |
if (udpcksum && uh->uh_sum) { |
if (udpcksum && uh->uh_sum) { |
((struct ipovly *)ip)->ih_next = 0; |
bzero(((struct ipovly *)ip)->ih_x1, |
((struct ipovly *)ip)->ih_prev = 0; |
sizeof ((struct ipovly *)ip)->ih_x1); |
((struct ipovly *)ip)->ih_x1 = 0; |
|
((struct ipovly *)ip)->ih_len = uh->uh_ulen; |
((struct ipovly *)ip)->ih_len = uh->uh_ulen; |
if (uh->uh_sum = in_cksum(m, len + sizeof (struct ip))) { |
if (uh->uh_sum = in_cksum(m, len + sizeof (struct ip))) { |
udpstat.udps_badsum++; |
udpstat.udps_badsum++; |
Line 151 udp_input(m, iphlen) |
|
Line 156 udp_input(m, iphlen) |
|
} |
} |
} |
} |
|
|
if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || |
if (IN_MULTICAST(ip->ip_dst.s_addr) || |
in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { |
in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { |
struct socket *last; |
struct socket *last; |
/* |
/* |
Line 182 udp_input(m, iphlen) |
|
Line 187 udp_input(m, iphlen) |
|
* (Algorithm copied from raw_intr().) |
* (Algorithm copied from raw_intr().) |
*/ |
*/ |
last = NULL; |
last = NULL; |
for (inp = udb.inp_next; inp != &udb; inp = inp->inp_next) { |
for (inp = udbtable.inpt_queue.cqh_first; |
|
inp != (struct inpcb *)&udbtable.inpt_queue; |
|
inp = inp->inp_queue.cqe_next) { |
if (inp->inp_lport != uh->uh_dport) |
if (inp->inp_lport != uh->uh_dport) |
continue; |
continue; |
if (inp->inp_laddr.s_addr != INADDR_ANY) { |
if (inp->inp_laddr.s_addr != INADDR_ANY) { |
Line 202 udp_input(m, iphlen) |
|
Line 209 udp_input(m, iphlen) |
|
|
|
if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { |
if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { |
if (sbappendaddr(&last->so_rcv, |
if (sbappendaddr(&last->so_rcv, |
(struct sockaddr *)&udp_in, |
sintosa(&udp_in), n, |
n, (struct mbuf *)0) == 0) { |
(struct mbuf *)0) == 0) { |
m_freem(n); |
m_freem(n); |
udpstat.udps_fullsock++; |
udpstat.udps_fullsock++; |
} else |
} else |
Line 232 udp_input(m, iphlen) |
|
Line 239 udp_input(m, iphlen) |
|
udpstat.udps_noportbcast++; |
udpstat.udps_noportbcast++; |
goto bad; |
goto bad; |
} |
} |
if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&udp_in, |
if (sbappendaddr(&last->so_rcv, sintosa(&udp_in), m, |
m, (struct mbuf *)0) == 0) { |
(struct mbuf *)0) == 0) { |
udpstat.udps_fullsock++; |
udpstat.udps_fullsock++; |
goto bad; |
goto bad; |
} |
} |
Line 243 udp_input(m, iphlen) |
|
Line 250 udp_input(m, iphlen) |
|
/* |
/* |
* Locate pcb for datagram. |
* Locate pcb for datagram. |
*/ |
*/ |
inp = udp_last_inpcb; |
inp = in_pcbhashlookup(&udbtable, ip->ip_src, uh->uh_sport, |
if (inp->inp_lport != uh->uh_dport || |
ip->ip_dst, uh->uh_dport); |
inp->inp_fport != uh->uh_sport || |
|
inp->inp_faddr.s_addr != ip->ip_src.s_addr || |
|
inp->inp_laddr.s_addr != ip->ip_dst.s_addr) { |
|
inp = in_pcblookup(&udb, ip->ip_src, uh->uh_sport, |
|
ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD); |
|
if (inp) |
|
udp_last_inpcb = inp; |
|
udpstat.udpps_pcbcachemiss++; |
|
} |
|
if (inp == 0) { |
if (inp == 0) { |
udpstat.udps_noport++; |
++udpstat.udps_pcbhashmiss; |
if (m->m_flags & (M_BCAST | M_MCAST)) { |
inp = in_pcblookup(&udbtable, ip->ip_src, uh->uh_sport, |
udpstat.udps_noportbcast++; |
ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD); |
goto bad; |
if (inp == 0) { |
|
udpstat.udps_noport++; |
|
if (m->m_flags & (M_BCAST | M_MCAST)) { |
|
udpstat.udps_noportbcast++; |
|
goto bad; |
|
} |
|
*ip = save_ip; |
|
ip->ip_len += iphlen; |
|
icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); |
|
return; |
} |
} |
*ip = save_ip; |
|
ip->ip_len += iphlen; |
|
icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); |
|
return; |
|
} |
} |
|
|
/* |
/* |
Line 302 udp_input(m, iphlen) |
|
Line 305 udp_input(m, iphlen) |
|
m->m_len -= iphlen; |
m->m_len -= iphlen; |
m->m_pkthdr.len -= iphlen; |
m->m_pkthdr.len -= iphlen; |
m->m_data += iphlen; |
m->m_data += iphlen; |
if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, |
if (sbappendaddr(&inp->inp_socket->so_rcv, sintosa(&udp_in), m, |
m, opts) == 0) { |
opts) == 0) { |
udpstat.udps_fullsock++; |
udpstat.udps_fullsock++; |
goto bad; |
goto bad; |
} |
} |
Line 362 udp_ctlinput(cmd, sa, ip) |
|
Line 365 udp_ctlinput(cmd, sa, ip) |
|
{ |
{ |
register struct udphdr *uh; |
register struct udphdr *uh; |
extern struct in_addr zeroin_addr; |
extern struct in_addr zeroin_addr; |
extern u_char inetctlerrmap[]; |
extern int inetctlerrmap[]; |
|
void (*notify) __P((struct inpcb *, int)) = udp_notify; |
|
int errno; |
|
|
if (!PRC_IS_REDIRECT(cmd) && |
if ((unsigned)cmd >= PRC_NCMDS) |
((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)) |
return; |
|
errno = inetctlerrmap[cmd]; |
|
if (PRC_IS_REDIRECT(cmd)) |
|
notify = in_rtchange, ip = 0; |
|
else if (cmd == PRC_HOSTDEAD) |
|
ip = 0; |
|
else if (errno == 0) |
return; |
return; |
if (ip) { |
if (ip) { |
uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); |
uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); |
in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, |
in_pcbnotify(&udbtable, sa, uh->uh_dport, ip->ip_src, |
cmd, udp_notify); |
uh->uh_sport, errno, notify); |
} else |
} else |
in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); |
in_pcbnotifyall(&udbtable, sa, errno, notify); |
} |
} |
|
|
int |
int |
Line 398 udp_output(inp, m, addr, control) |
|
Line 409 udp_output(inp, m, addr, control) |
|
/* |
/* |
* Must block input while temporarily connected. |
* Must block input while temporarily connected. |
*/ |
*/ |
s = splnet(); |
s = splsoftnet(); |
error = in_pcbconnect(inp, addr); |
error = in_pcbconnect(inp, addr); |
if (error) { |
if (error) { |
splx(s); |
splx(s); |
Line 425 udp_output(inp, m, addr, control) |
|
Line 436 udp_output(inp, m, addr, control) |
|
* and addresses and length put into network format. |
* and addresses and length put into network format. |
*/ |
*/ |
ui = mtod(m, struct udpiphdr *); |
ui = mtod(m, struct udpiphdr *); |
ui->ui_next = ui->ui_prev = 0; |
bzero(ui->ui_x1, sizeof ui->ui_x1); |
ui->ui_x1 = 0; |
|
ui->ui_pr = IPPROTO_UDP; |
ui->ui_pr = IPPROTO_UDP; |
ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); |
ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr)); |
ui->ui_src = inp->inp_laddr; |
ui->ui_src = inp->inp_laddr; |
ui->ui_dst = inp->inp_faddr; |
ui->ui_dst = inp->inp_faddr; |
ui->ui_sport = inp->inp_lport; |
ui->ui_sport = inp->inp_lport; |
Line 479 udp_usrreq(so, req, m, addr, control) |
|
Line 489 udp_usrreq(so, req, m, addr, control) |
|
int s; |
int s; |
|
|
if (req == PRU_CONTROL) |
if (req == PRU_CONTROL) |
return (in_control(so, (int)m, (caddr_t)addr, |
return (in_control(so, (long)m, (caddr_t)addr, |
(struct ifnet *)control)); |
(struct ifnet *)control)); |
if (inp == NULL && req != PRU_ATTACH) { |
if (inp == NULL && req != PRU_ATTACH) { |
error = EINVAL; |
error = EINVAL; |
Line 496 udp_usrreq(so, req, m, addr, control) |
|
Line 506 udp_usrreq(so, req, m, addr, control) |
|
error = EINVAL; |
error = EINVAL; |
break; |
break; |
} |
} |
s = splnet(); |
s = splsoftnet(); |
error = in_pcballoc(so, &udb); |
error = in_pcballoc(so, &udbtable); |
splx(s); |
splx(s); |
if (error) |
if (error) |
break; |
break; |
Line 512 udp_usrreq(so, req, m, addr, control) |
|
Line 522 udp_usrreq(so, req, m, addr, control) |
|
break; |
break; |
|
|
case PRU_BIND: |
case PRU_BIND: |
s = splnet(); |
s = splsoftnet(); |
error = in_pcbbind(inp, addr); |
error = in_pcbbind(inp, addr); |
splx(s); |
splx(s); |
break; |
break; |
Line 526 udp_usrreq(so, req, m, addr, control) |
|
Line 536 udp_usrreq(so, req, m, addr, control) |
|
error = EISCONN; |
error = EISCONN; |
break; |
break; |
} |
} |
s = splnet(); |
s = splsoftnet(); |
error = in_pcbconnect(inp, addr); |
error = in_pcbconnect(inp, addr); |
splx(s); |
splx(s); |
if (error == 0) |
if (error == 0) |
Line 546 udp_usrreq(so, req, m, addr, control) |
|
Line 556 udp_usrreq(so, req, m, addr, control) |
|
error = ENOTCONN; |
error = ENOTCONN; |
break; |
break; |
} |
} |
s = splnet(); |
s = splsoftnet(); |
in_pcbdisconnect(inp); |
in_pcbdisconnect(inp); |
inp->inp_laddr.s_addr = INADDR_ANY; |
inp->inp_laddr.s_addr = INADDR_ANY; |
splx(s); |
splx(s); |
|
|
udp_detach(inp) |
udp_detach(inp) |
struct inpcb *inp; |
struct inpcb *inp; |
{ |
{ |
int s = splnet(); |
int s = splsoftnet(); |
|
|
if (inp == udp_last_inpcb) |
|
udp_last_inpcb = &udb; |
|
in_pcbdetach(inp); |
in_pcbdetach(inp); |
splx(s); |
splx(s); |
} |
} |