Set l4 pointer properly in buffers that contain IP options.
[sliver-openvswitch.git] / lib / flow.c
1 #include <sys/types.h>
2 #include "flow.h"
3 #include <inttypes.h>
4 #include <netinet/in.h>
5 #include <string.h>
6 #include "buffer.h"
7 #include "hash.h"
8 #include "ip.h"
9 #include "mac.h"
10 #include "openflow.h"
11 #include "packets.h"
12
13 #include "vlog.h"
14 #define THIS_MODULE VLM_flow
15
16 void
17 flow_extract(struct buffer *packet, uint16_t in_port, struct flow *flow)
18 {
19     struct buffer b = *packet;
20     struct eth_header *eth;
21
22     if (b.size < ETH_TOTAL_MIN) {
23         VLOG_WARN("packet length %d less than minimum size %d",
24                   b.size, ETH_TOTAL_MIN);
25     }
26
27     memset(flow, 0, sizeof *flow);
28     flow->in_port = htons(in_port);
29
30     packet->l2 = b.data;
31     packet->l3 = NULL;
32     packet->l4 = NULL;
33
34     eth = buffer_at(&b, 0, sizeof *eth);
35     if (eth) {
36         buffer_pull(&b, ETH_HEADER_LEN);
37         if (ntohs(eth->eth_type) >= OFP_DL_TYPE_ETH2_CUTOFF) {
38             /* This is an Ethernet II frame */
39             flow->dl_type = eth->eth_type;
40         } else {
41             /* This is an 802.2 frame */
42             struct llc_snap_header *h = buffer_at(&b, 0, sizeof *h);
43             if (h == NULL) {
44                 return;
45             }
46             if (h->llc.llc_dsap == LLC_DSAP_SNAP
47                 && h->llc.llc_ssap == LLC_SSAP_SNAP
48                 && h->llc.llc_cntl == LLC_CNTL_SNAP
49                 && !memcmp(h->snap.snap_org, SNAP_ORG_ETHERNET,
50                            sizeof h->snap.snap_org)) {
51                 flow->dl_type = h->snap.snap_type;
52                 buffer_pull(&b, sizeof *h);
53             } else {
54                 flow->dl_type = OFP_DL_TYPE_NOT_ETH_TYPE;
55                 buffer_pull(&b, sizeof(struct llc_header));
56             }
57         }
58
59         /* Check for a VLAN tag */
60         if (flow->dl_type != htons(ETH_TYPE_VLAN)) {
61             flow->dl_vlan = htons(OFP_VLAN_NONE);
62         } else {
63             struct vlan_header *vh = buffer_at(&b, 0, sizeof *vh);
64             flow->dl_type = vh->vlan_next_type;
65             flow->dl_vlan = vh->vlan_tci & htons(VLAN_VID);
66             buffer_pull(&b, sizeof *vh);
67         }
68         memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
69         memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
70
71         packet->l3 = b.data;
72         if (flow->dl_type == htons(ETH_TYPE_IP)) {
73             const struct ip_header *nh = buffer_at(&b, 0, sizeof *nh);
74             if (nh) {
75                 int ip_len = IP_IHL(nh->ip_ihl_ver) * 4;
76                 if (ip_len < IP_HEADER_LEN) {
77                     return;
78                 }
79
80                 flow->nw_src = nh->ip_src;
81                 flow->nw_dst = nh->ip_dst;
82                 flow->nw_proto = nh->ip_proto;
83                 if (flow->nw_proto == IP_TYPE_TCP
84                     || flow->nw_proto == IP_TYPE_UDP) {
85                     const struct udp_header *th;
86                     th = packet->l4 = buffer_at(&b, ip_len, sizeof *th);
87                     if (th) {
88                         flow->tp_src = th->udp_src;
89                         flow->tp_dst = th->udp_dst;
90                     } else {
91                         /* Avoid tricking other code into thinking that this
92                          * packet has an L4 header. */
93                         flow->nw_proto = 0;
94                     }
95                 }
96             }
97         } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
98             const struct arp_eth_header *ah = buffer_at(&b, 0, sizeof *ah);
99             if (ah && ah->ar_hrd == htons(ARP_HRD_ETHERNET)
100                 && ah->ar_pro == htons(ARP_PRO_IP)
101                 && ah->ar_hln == ETH_ADDR_LEN
102                 && ah->ar_pln == sizeof flow->nw_src)
103             {
104                 /* check if sha/tha match dl_src/dl_dst? */
105                 flow->nw_src = ah->ar_spa;
106                 flow->nw_dst = ah->ar_tpa;
107             }
108         }
109     }
110 }
111
112 void
113 flow_print(FILE *stream, const struct flow *flow) 
114 {
115     fprintf(stream,
116             "port%04x:vlan%04x mac"MAC_FMT"->"MAC_FMT" "
117             "proto%04x ip"IP_FMT"->"IP_FMT" port%d->%d",
118             ntohs(flow->in_port), ntohs(flow->dl_vlan),
119             MAC_ARGS(flow->dl_src), MAC_ARGS(flow->dl_dst),
120             ntohs(flow->dl_type),
121             IP_ARGS(&flow->nw_src), IP_ARGS(&flow->nw_dst),
122             ntohs(flow->tp_src), ntohs(flow->tp_dst));
123 }
124
125 int
126 flow_compare(const struct flow *a, const struct flow *b)
127 {
128     return memcmp(a, b, sizeof *a);
129 }
130
131 unsigned long int
132 flow_hash(const struct flow *flow, uint32_t basis) 
133 {
134     return hash_fnv(flow, sizeof *flow, basis);
135 }