#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/stream.h>
#include <sys/strsun.h>
#include <sys/strsubr.h>
#include <sys/pattr.h>
#include <sys/ethernet.h>
#include <inet/ip.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/sctp.h>
#include "sfxge.h"
#include "efx.h"
sfxge_packet_type_t
sfxge_pkthdr_parse(mblk_t *mp, struct ether_header **etherhpp,
struct ip **iphpp, struct tcphdr **thpp,
size_t *offp, size_t *sizep,
uint16_t *sportp, uint16_t *dportp)
{
struct ether_header *etherhp;
uint16_t ether_type;
size_t etherhs;
struct ip *iphp;
size_t iphs;
struct tcphdr *thp;
size_t len;
size_t ths;
size_t off;
size_t size;
uint16_t sport;
uint16_t dport;
sfxge_packet_type_t pkt_type = SFXGE_PACKET_TYPE_UNKNOWN;
etherhp = NULL;
iphp = NULL;
thp = NULL;
off = 0;
size = 0;
sport = 0;
dport = 0;
etherhs = sizeof (struct ether_header);
if ((MBLKL(mp) < etherhs) && (pullupmsg(mp, etherhs) == 0))
goto done;
etherhp = (struct ether_header *)(mp->b_rptr);
ether_type = etherhp->ether_type;
if (ether_type == htons(ETHERTYPE_VLAN)) {
struct ether_vlan_header *ethervhp;
etherhs = sizeof (struct ether_vlan_header);
if ((MBLKL(mp) < etherhs) && (pullupmsg(mp, etherhs) == 0))
goto done;
ethervhp = (struct ether_vlan_header *)(mp->b_rptr);
ether_type = ethervhp->ether_type;
}
if (ether_type != htons(ETHERTYPE_IP))
goto done;
off += etherhs;
len = off + sizeof (struct ip);
if ((MBLKL(mp) < len) && (pullupmsg(mp, len) == 0))
goto done;
iphp = (struct ip *)(mp->b_rptr + off);
iphs = iphp->ip_hl * 4;
if (iphp->ip_v != IPV4_VERSION)
goto done;
size = ntohs(iphp->ip_len);
ASSERT3U(etherhs + size, <=, msgdsize(mp));
pkt_type = SFXGE_PACKET_TYPE_IPV4_OTHER;
off += iphs;
size -= iphs;
if (iphp->ip_p == IPPROTO_TCP) {
len = off + sizeof (struct tcphdr);
if ((MBLKL(mp) < len) && (pullupmsg(mp, len) == 0))
goto done;
thp = (struct tcphdr *)(mp->b_rptr + off);
ths = thp->th_off * 4;
dport = thp->th_dport;
sport = thp->th_sport;
off += ths;
size -= ths;
pkt_type = SFXGE_PACKET_TYPE_IPV4_TCP;
} else if (iphp->ip_p == IPPROTO_UDP) {
struct udphdr *uhp;
len = off + sizeof (struct udphdr);
if ((MBLKL(mp) < len) && (pullupmsg(mp, len) == 0))
goto done;
uhp = (struct udphdr *)(mp->b_rptr + off);
dport = uhp->uh_dport;
sport = uhp->uh_sport;
off += sizeof (struct udphdr);
size -= sizeof (struct udphdr);
pkt_type = SFXGE_PACKET_TYPE_IPV4_UDP;
} else if (iphp->ip_p == IPPROTO_SCTP) {
struct sctp_hdr *shp;
len = off + sizeof (struct sctp_hdr);
if ((MBLKL(mp) < len) && (pullupmsg(mp, len) == 0))
goto done;
shp = (struct sctp_hdr *)(mp->b_rptr + off);
dport = shp->sh_dport;
sport = shp->sh_sport;
off += sizeof (struct sctp_hdr);
size -= sizeof (struct sctp_hdr);
pkt_type = SFXGE_PACKET_TYPE_IPV4_SCTP;
}
if (MBLKL(mp) < off)
(void) pullupmsg(mp, off);
done:
*etherhpp = etherhp;
*iphpp = iphp;
*thpp = thp;
*offp = off;
*sizep = size;
*sportp = sport;
*dportp = dport;
return (pkt_type);
}