return NULL;
}
-static struct tcp_header *
-pull_tcp(struct ofpbuf *packet)
-{
- if (packet->size >= TCP_HEADER_LEN) {
- struct tcp_header *tcp = packet->data;
- int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
- if (tcp_len >= TCP_HEADER_LEN && packet->size >= tcp_len) {
- return ofpbuf_pull(packet, tcp_len);
- }
- }
- return NULL;
-}
-
-static struct udp_header *
-pull_udp(struct ofpbuf *packet)
-{
- return ofpbuf_try_pull(packet, UDP_HEADER_LEN);
-}
-
-static struct sctp_header *
-pull_sctp(struct ofpbuf *packet)
-{
- return ofpbuf_try_pull(packet, SCTP_HEADER_LEN);
-}
-
static struct icmp_header *
pull_icmp(struct ofpbuf *packet)
{
}
static void
-parse_tcp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
+parse_tcp(struct ofpbuf *b, struct flow *flow)
{
- const struct tcp_header *tcp = pull_tcp(b);
- if (tcp) {
+ if (b->size >= TCP_HEADER_LEN) {
+ const struct tcp_header *tcp = b->data;
+
flow->tp_src = tcp->tcp_src;
flow->tp_dst = tcp->tcp_dst;
flow->tcp_flags = tcp->tcp_ctl & htons(0x0fff);
- packet->l7 = b->data;
}
}
static void
-parse_udp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
+parse_udp(struct ofpbuf *b, struct flow *flow)
{
- const struct udp_header *udp = pull_udp(b);
- if (udp) {
+ if (b->size >= UDP_HEADER_LEN) {
+ const struct udp_header *udp = b->data;
+
flow->tp_src = udp->udp_src;
flow->tp_dst = udp->udp_dst;
- packet->l7 = b->data;
}
}
static void
-parse_sctp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
+parse_sctp(struct ofpbuf *b, struct flow *flow)
{
- const struct sctp_header *sctp = pull_sctp(b);
- if (sctp) {
+ if (b->size >= SCTP_HEADER_LEN) {
+ const struct sctp_header *sctp = b->data;
+
flow->tp_src = sctp->sctp_src;
flow->tp_dst = sctp->sctp_dst;
- packet->l7 = b->data;
}
}
-static bool
+static void
parse_icmpv6(struct ofpbuf *b, struct flow *flow)
{
const struct icmp6_hdr *icmp = pull_icmpv6(b);
if (!icmp) {
- return false;
+ return;
}
/* The ICMPv6 type and code fields use the 16-bit transport port
nd_target = ofpbuf_try_pull(b, sizeof *nd_target);
if (!nd_target) {
- return false;
+ return;
}
flow->nd_target = *nd_target;
}
}
- return true;
+ return;
invalid:
memset(&flow->nd_target, 0, sizeof(flow->nd_target));
memset(flow->arp_sha, 0, sizeof(flow->arp_sha));
memset(flow->arp_tha, 0, sizeof(flow->arp_tha));
- return false;
-
+ return;
}
/* Initializes 'flow' members from 'packet' and 'md'
*
* - packet->l4 to just past the IPv4 header, if one is present and has a
* correct length, and otherwise NULL.
- *
- * - packet->l7 to just past the TCP/UDP/SCTP/ICMP header, if one is
- * present and has a correct length, and otherwise NULL.
*/
void
flow_extract(struct ofpbuf *packet, const struct pkt_metadata *md,
packet->l2_5 = NULL;
packet->l3 = NULL;
packet->l4 = NULL;
- packet->l7 = NULL;
if (b.size < sizeof *eth) {
return;
if (!(nh->ip_frag_off & htons(IP_FRAG_OFF_MASK))) {
if (flow->nw_proto == IPPROTO_TCP) {
- parse_tcp(packet, &b, flow);
+ parse_tcp(&b, flow);
} else if (flow->nw_proto == IPPROTO_UDP) {
- parse_udp(packet, &b, flow);
+ parse_udp(&b, flow);
} else if (flow->nw_proto == IPPROTO_SCTP) {
- parse_sctp(packet, &b, flow);
+ parse_sctp(&b, flow);
} else if (flow->nw_proto == IPPROTO_ICMP) {
const struct icmp_header *icmp = pull_icmp(&b);
if (icmp) {
flow->tp_src = htons(icmp->icmp_type);
flow->tp_dst = htons(icmp->icmp_code);
- packet->l7 = b.data;
}
}
}
packet->l4 = b.data;
if (flow->nw_proto == IPPROTO_TCP) {
- parse_tcp(packet, &b, flow);
+ parse_tcp(&b, flow);
} else if (flow->nw_proto == IPPROTO_UDP) {
- parse_udp(packet, &b, flow);
+ parse_udp(&b, flow);
} else if (flow->nw_proto == IPPROTO_SCTP) {
- parse_sctp(packet, &b, flow);
+ parse_sctp(&b, flow);
} else if (flow->nw_proto == IPPROTO_ICMPV6) {
- if (parse_icmpv6(&b, flow)) {
- packet->l7 = b.data;
- }
+ parse_icmpv6(&b, flow);
}
} else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
flow->dl_type == htons(ETH_TYPE_RARP)) {
flow->mpls_lse[idx] = lse;
}
-static void
+static uint8_t *
flow_compose_l4(struct ofpbuf *b, const struct flow *flow)
{
if (!(flow->nw_frag & FLOW_NW_FRAG_ANY)
tcp->tcp_src = flow->tp_src;
tcp->tcp_dst = flow->tp_dst;
tcp->tcp_ctl = TCP_CTL(ntohs(flow->tcp_flags), 5);
- b->l7 = ofpbuf_tail(b);
+ return ofpbuf_tail(b);
} else if (flow->nw_proto == IPPROTO_UDP) {
struct udp_header *udp;
udp = ofpbuf_put_zeros(b, sizeof *udp);
udp->udp_src = flow->tp_src;
udp->udp_dst = flow->tp_dst;
- b->l7 = ofpbuf_tail(b);
+ return ofpbuf_tail(b);
} else if (flow->nw_proto == IPPROTO_SCTP) {
struct sctp_header *sctp;
sctp = ofpbuf_put_zeros(b, sizeof *sctp);
sctp->sctp_src = flow->tp_src;
sctp->sctp_dst = flow->tp_dst;
- b->l7 = ofpbuf_tail(b);
+ return ofpbuf_tail(b);
} else if (flow->nw_proto == IPPROTO_ICMP) {
struct icmp_header *icmp;
icmp->icmp_type = ntohs(flow->tp_src);
icmp->icmp_code = ntohs(flow->tp_dst);
icmp->icmp_csum = csum(icmp, ICMP_HEADER_LEN);
- b->l7 = ofpbuf_tail(b);
+ return ofpbuf_tail(b);
} else if (flow->nw_proto == IPPROTO_ICMPV6) {
struct icmp6_hdr *icmp;
}
icmp->icmp6_cksum = (OVS_FORCE uint16_t)
csum(icmp, (char *)ofpbuf_tail(b) - (char *)icmp);
- b->l7 = ofpbuf_tail(b);
+ return ofpbuf_tail(b);
}
}
+ return NULL;
}
/* Puts into 'b' a packet that flow_extract() would parse as having the given
ip->ip_csum = csum(ip, sizeof *ip);
} else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
struct ovs_16aligned_ip6_hdr *nh;
+ uint8_t *l7;
nh = ofpbuf_put_zeros(b, sizeof *nh);
put_16aligned_be32(&nh->ip6_flow, htonl(6 << 28) |
b->l4 = ofpbuf_tail(b);
- flow_compose_l4(b, flow);
+ l7 = flow_compose_l4(b, flow);
- nh->ip6_plen =
- b->l7 ? htons((uint8_t *) b->l7 - (uint8_t *) b->l4) : htons(0);
+ nh->ip6_plen = l7 ? htons(l7 - (uint8_t *) b->l4) : htons(0);
} else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
flow->dl_type == htons(ETH_TYPE_RARP)) {
struct arp_eth_header *arp;
const struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
struct ofpbuf buf;
struct flow flow;
+ size_t l4_size;
ofpbuf_use_const(&buf, data, len);
flow_extract(&buf, &md, &flow);
flow_format(&ds, &flow);
- if (buf.l7) {
- if (flow.nw_proto == IPPROTO_TCP) {
- struct tcp_header *th = buf.l4;
- ds_put_format(&ds, " tcp_csum:%"PRIx16,
- ntohs(th->tcp_csum));
- } else if (flow.nw_proto == IPPROTO_UDP) {
- struct udp_header *uh = buf.l4;
- ds_put_format(&ds, " udp_csum:%"PRIx16,
- ntohs(uh->udp_csum));
- } else if (flow.nw_proto == IPPROTO_SCTP) {
- struct sctp_header *sh = buf.l4;
- ds_put_format(&ds, " sctp_csum:%"PRIx32,
- ntohl(sh->sctp_csum));
- }
+ l4_size = ofpbuf_get_l4_size(&buf);
+
+ if (flow.nw_proto == IPPROTO_TCP && l4_size >= TCP_HEADER_LEN) {
+ struct tcp_header *th = buf.l4;
+ ds_put_format(&ds, " tcp_csum:%"PRIx16, ntohs(th->tcp_csum));
+ } else if (flow.nw_proto == IPPROTO_UDP && l4_size >= UDP_HEADER_LEN) {
+ struct udp_header *uh = buf.l4;
+ ds_put_format(&ds, " udp_csum:%"PRIx16, ntohs(uh->udp_csum));
+ } else if (flow.nw_proto == IPPROTO_SCTP && l4_size >= SCTP_HEADER_LEN) {
+ struct sctp_header *sh = buf.l4;
+ ds_put_format(&ds, " sctp_csum:%"PRIx32, ntohl(sh->sctp_csum));
}
ds_put_char(&ds, '\n');
#include <stddef.h>
#include <stdint.h>
#include "list.h"
+#include "packets.h"
#include "util.h"
#ifdef __cplusplus
void *l2_5; /* MPLS label stack */
void *l3; /* Network-level header. */
void *l4; /* Transport-level header. */
- void *l7; /* Application data. */
struct list list_node; /* Private list element for use by owner. */
void *private_p; /* Private pointer for use by owner. */
return a->size == b->size && memcmp(a->data, b->data, a->size) == 0;
}
+static inline size_t ofpbuf_get_l4_size(const struct ofpbuf *b)
+{
+ return b->l4 ? (const char *)ofpbuf_tail(b) - (const char *)b->l4 : 0;
+}
+
+static inline const void *ofpbuf_get_tcp_payload(const struct ofpbuf *b)
+{
+ size_t l4_size = ofpbuf_get_l4_size(b);
+
+ if (OVS_LIKELY(l4_size >= TCP_HEADER_LEN)) {
+ struct tcp_header *tcp = b->l4;
+ int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
+
+ if (OVS_LIKELY(tcp_len >= TCP_HEADER_LEN && tcp_len <= l4_size)) {
+ return (const char *)tcp + tcp_len;
+ }
+ }
+ return NULL;
+}
+
+static inline const void *ofpbuf_get_udp_payload(const struct ofpbuf *b)
+{
+ return OVS_LIKELY(ofpbuf_get_l4_size(b) >= UDP_HEADER_LEN)
+ ? (const char *)b->l4 + UDP_HEADER_LEN : NULL;
+}
+
+static inline const void *ofpbuf_get_sctp_payload(const struct ofpbuf *b)
+{
+ return OVS_LIKELY(ofpbuf_get_l4_size(b) >= SCTP_HEADER_LEN)
+ ? (const char *)b->l4 + SCTP_HEADER_LEN : NULL;
+}
+
+static inline const void *ofpbuf_get_icmp_payload(const struct ofpbuf *b)
+{
+ return OVS_LIKELY(ofpbuf_get_l4_size(b) >= ICMP_HEADER_LEN)
+ ? (const char *)b->l4 + ICMP_HEADER_LEN : NULL;
+}
#ifdef __cplusplus
}