static void parse_udp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow) { const struct udp_header *udp = pull_udp(b); if (udp) { flow->tp_src = udp->udp_src; flow->tp_dst = udp->udp_dst; packet->l7 = b->data; } }
/* 'tun_id' is in network byte order, while 'in_port' is in host byte order. * These byte orders are the same as they are in struct odp_flow_key. * * Initializes packet header pointers as follows: * * - packet->l2 to the start of the Ethernet header. * * - packet->l3 to just past the Ethernet header, or just past the * vlan_header if one is present, to the first byte of the payload of the * Ethernet frame. * * - 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 or UDP or ICMP header, if one is * present and has a correct length, and otherwise NULL. */ int flow_extract(struct ofpbuf *packet, uint32_t tun_id, uint16_t in_port, flow_t *flow) { struct ofpbuf b = *packet; struct eth_header *eth; int retval = 0; COVERAGE_INC(flow_extract); memset(flow, 0, sizeof *flow); flow->tun_id = tun_id; flow->in_port = in_port; flow->dl_vlan = htons(OFP_VLAN_NONE); packet->l2 = b.data; packet->l3 = NULL; packet->l4 = NULL; packet->l7 = NULL; if (b.size < sizeof *eth) { return 0; } /* Link layer. */ eth = b.data; memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN); memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN); /* dl_type, dl_vlan, dl_vlan_pcp. */ ofpbuf_pull(&b, ETH_ADDR_LEN * 2); if (eth->eth_type == htons(ETH_TYPE_VLAN)) { parse_vlan(&b, flow); } flow->dl_type = parse_ethertype(&b); /* Network layer. */ packet->l3 = b.data; if (flow->dl_type == htons(ETH_TYPE_IP)) { const struct ip_header *nh = pull_ip(&b); if (nh) { flow->nw_src = get_unaligned_u32(&nh->ip_src); flow->nw_dst = get_unaligned_u32(&nh->ip_dst); flow->nw_tos = nh->ip_tos & IP_DSCP_MASK; flow->nw_proto = nh->ip_proto; packet->l4 = b.data; if (!IP_IS_FRAGMENT(nh->ip_frag_off)) { if (flow->nw_proto == IP_TYPE_TCP) { const struct tcp_header *tcp = pull_tcp(&b); if (tcp) { flow->tp_src = tcp->tcp_src; flow->tp_dst = tcp->tcp_dst; packet->l7 = b.data; } } else if (flow->nw_proto == IP_TYPE_UDP) { const struct udp_header *udp = pull_udp(&b); if (udp) { flow->tp_src = udp->udp_src; flow->tp_dst = udp->udp_dst; packet->l7 = b.data; } } else if (flow->nw_proto == IP_TYPE_ICMP) { const struct icmp_header *icmp = pull_icmp(&b); if (icmp) { flow->icmp_type = htons(icmp->icmp_type); flow->icmp_code = htons(icmp->icmp_code); packet->l7 = b.data; } } } else { retval = 1; } } } else if (flow->dl_type == htons(ETH_TYPE_ARP)) { const struct arp_eth_header *arp = pull_arp(&b); if (arp && arp->ar_hrd == htons(1) && arp->ar_pro == htons(ETH_TYPE_IP) && arp->ar_hln == ETH_ADDR_LEN && arp->ar_pln == 4) { /* We only match on the lower 8 bits of the opcode. */ if (ntohs(arp->ar_op) <= 0xff) { flow->nw_proto = ntohs(arp->ar_op); } if ((flow->nw_proto == ARP_OP_REQUEST) || (flow->nw_proto == ARP_OP_REPLY)) { flow->nw_src = arp->ar_spa; flow->nw_dst = arp->ar_tpa; } } } return retval; }
/* Returns 1 if 'packet' is an IP fragment, 0 otherwise. */ int flow_extract(struct ofpbuf *packet, uint32_t in_port, struct flow *flow) { struct ofpbuf b = *packet; struct eth_header *eth; int retval = 0; memset(flow, 0, sizeof *flow); flow->dl_vlan = htons(OFPVID_NONE); flow->in_port = htonl(in_port); packet->l2 = b.data; packet->l3 = NULL; packet->l4 = NULL; packet->l7 = NULL; eth = pull_eth(&b); if (eth) { if (ntohs(eth->eth_type) >= 0x600) { /* This is an Ethernet II frame */ flow->dl_type = eth->eth_type; } else { /* This is an 802.2 frame */ struct llc_header *llc = ofpbuf_at(&b, 0, sizeof *llc); struct snap_header *snap = ofpbuf_at(&b, sizeof *llc, sizeof *snap); if (llc == NULL) { return 0; } if (snap && llc->llc_dsap == LLC_DSAP_SNAP && llc->llc_ssap == LLC_SSAP_SNAP && llc->llc_cntl == LLC_CNTL_SNAP && !memcmp(snap->snap_org, SNAP_ORG_ETHERNET, sizeof snap->snap_org)) { flow->dl_type = snap->snap_type; ofpbuf_pull(&b, LLC_SNAP_HEADER_LEN); } else { flow->dl_type = htons(0x05ff); ofpbuf_pull(&b, sizeof(struct llc_header)); } } /* Check for a VLAN tag */ if (flow->dl_type == htons(ETH_TYPE_VLAN)) { struct vlan_header *vh = pull_vlan(&b); if (vh) { flow->dl_type = vh->vlan_next_type; flow->dl_vlan = vh->vlan_tci & htons(VLAN_VID_MASK); flow->dl_vlan_pcp = (uint8_t)((ntohs(vh->vlan_tci) >> VLAN_PCP_SHIFT) & VLAN_PCP_BITMASK); } } memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN); memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN); packet->l3 = b.data; if (flow->dl_type == htons(ETH_TYPE_IP)) { const struct ip_header *nh = pull_ip(&b); if (nh) { flow->nw_tos = nh->ip_tos & 0xfc; flow->nw_proto = nh->ip_proto; flow->nw_src = nh->ip_src; flow->nw_dst = nh->ip_dst; packet->l4 = b.data; if (!IP_IS_FRAGMENT(nh->ip_frag_off)) { if (flow->nw_proto == IP_TYPE_TCP) { const struct tcp_header *tcp = pull_tcp(&b); if (tcp) { flow->tp_src = tcp->tcp_src; flow->tp_dst = tcp->tcp_dst; packet->l7 = b.data; } else { /* Avoid tricking other code into thinking that * this packet has an L4 header. */ flow->nw_proto = 0; } } else if (flow->nw_proto == IP_TYPE_UDP) { const struct udp_header *udp = pull_udp(&b); if (udp) { flow->tp_src = udp->udp_src; flow->tp_dst = udp->udp_dst; packet->l7 = b.data; } else { /* Avoid tricking other code into thinking that * this packet has an L4 header. */ flow->nw_proto = 0; } } else if (flow->nw_proto == IP_TYPE_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; } else { /* Avoid tricking other code into thinking that * this packet has an L4 header. */ flow->nw_proto = 0; } } } else { retval = 1; } } } else if (flow->dl_type == htons(ETH_TYPE_ARP)) { struct arp_eth_header *arp = pull_arp(&b); if (arp) { if (arp->ar_pro == htons(ARP_PRO_IP) && arp->ar_pln == IP_ADDR_LEN) { flow->nw_src = arp->ar_spa; flow->nw_dst = arp->ar_tpa; } flow->nw_proto = ntohs(arp->ar_op) & 0xff; } } } return retval; }