#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <socket_impl.h>
#include <socket_inet.h>
#include <sys/salib.h>
#include <sys/socket.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include "udp_inet.h"
#include "ipv4.h"
#include "ipv4_impl.h"
#include "mac.h"
#include "mac_impl.h"
#include "v4_sum_impl.h"
#include <sys/bootdebug.h>
static int udp_cksum_flag = TRUE;
void
udp_socket_init(struct inetboot_socket *isp)
{
isp->type = INETBOOT_DGRAM;
isp->proto = IPPROTO_UDP;
isp->input[TRANSPORT_LVL] = udp_input;
isp->output[TRANSPORT_LVL] = udp_output;
isp->close[TRANSPORT_LVL] = NULL;
isp->headerlen[TRANSPORT_LVL] = udp_header_len;
isp->ports = udp_ports;
}
int
udp_header_len(struct inetgram *igm)
{
return (sizeof (struct udphdr));
}
in_port_t
udp_ports(uint16_t *udphp, enum Ports request)
{
if (request == SOURCE)
return (((struct udphdr *)udphp)->uh_sport);
return (((struct udphdr *)udphp)->uh_dport);
}
int
udp_input(int index)
{
int frames = 0, header_len;
struct inetgram *igp, *ugp = NULL;
struct udphdr *udphp;
mblk_t *mp;
#ifdef DEBUG
printf("udp_input(%d) ###############################\n", index);
#endif
while ((igp = sockets[index].inq) != NULL) {
if (igp->igm_level != TRANSPORT_LVL) {
#ifdef DEBUG
printf("udp_input(%d): level %d datagram discarded.\n",
index, igp->igm_level);
#endif
del_gram(&sockets[index].inq, igp, TRUE);
continue;
}
mp = igp->igm_mp;
udphp = (struct udphdr *)(mp->b_rptr +
IPH_HDR_LENGTH(mp->b_rptr));
header_len = (sockets[index].headerlen[TRANSPORT_LVL])(NULL);
mp->b_rptr = ((unsigned char *)udphp) + header_len;
mp->b_wptr = ((unsigned char *)udphp) + ntohs(udphp->uh_ulen);
if (udp_cksum_flag && udphp->uh_sum != 0) {
if (udp_chksum(udphp, &igp->igm_saddr.sin_addr,
&igp->igm_target, sockets[index].proto) != 0) {
dprintf("udp_input(%d): bad udp chksum "
"from %s.\n", index,
inet_ntoa(igp->igm_saddr.sin_addr));
del_gram(&sockets[index].inq, igp, TRUE);
continue;
}
}
if (sockets[index].bind.sin_port != udphp->uh_dport) {
dprintf("udp_input(%d): Unexpected port number: "
"%d != %d from %s.\n", index,
ntohs(udphp->uh_dport), ntohs(
sockets[index].bind.sin_port),
inet_ntoa(igp->igm_saddr.sin_addr));
del_gram(&sockets[index].inq, igp, TRUE);
continue;
}
igp->igm_level = APP_LVL;
del_gram(&sockets[index].inq, igp, FALSE);
add_grams(&ugp, igp);
frames++;
}
add_grams(&sockets[index].inq, ugp);
return (frames);
}
int
udp_output(int index, struct inetgram *ogp)
{
struct udphdr *udphp;
mblk_t *mp;
#ifdef DEBUG
printf("udp_output(%d): 0x%x, %d\n", index, ogp->igm_mp,
ogp->igm_mp->b_wptr - ogp->igm_mp->b_rptr);
#endif
mp = ogp->igm_mp;
mp->b_rptr -= sizeof (struct udphdr);
udphp = (struct udphdr *)(mp->b_rptr);
udphp->uh_dport = ogp->igm_saddr.sin_port;
if (sockets[index].bound)
udphp->uh_sport = sockets[index].bind.sin_port;
else
udphp->uh_sport = ogp->igm_saddr.sin_port;
udphp->uh_ulen = htons(mp->b_wptr - mp->b_rptr);
udphp->uh_sum = 0;
if (udp_cksum_flag) {
udphp->uh_sum = udp_chksum(udphp, &sockets[index].bind.sin_addr,
&ogp->igm_saddr.sin_addr, sockets[index].proto);
}
ogp->igm_level = NETWORK_LVL;
return (0);
}