48
48
static struct kmem_cache *flow_cache;
50
static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
51
struct sw_flow_key_range *range, u8 val);
53
static void update_range__(struct sw_flow_match *match,
54
size_t offset, size_t size, bool is_mask)
56
struct sw_flow_key_range *range = NULL;
57
size_t start = offset;
58
size_t end = offset + size;
61
range = &match->range;
63
range = &match->mask->range;
68
if (range->start == range->end) {
74
if (range->start > start)
81
#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
83
update_range__(match, offsetof(struct sw_flow_key, field), \
84
sizeof((match)->key->field), is_mask); \
87
(match)->mask->key.field = value; \
89
(match)->key->field = value; \
93
#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
95
update_range__(match, offsetof(struct sw_flow_key, field), \
99
memcpy(&(match)->mask->key.field, value_p, len);\
101
memcpy(&(match)->key->field, value_p, len); \
105
void ovs_match_init(struct sw_flow_match *match,
106
struct sw_flow_key *key,
107
struct sw_flow_mask *mask)
109
memset(match, 0, sizeof(*match));
113
memset(key, 0, sizeof(*key));
116
memset(&mask->key, 0, sizeof(mask->key));
117
mask->range.start = mask->range.end = 0;
121
static bool ovs_match_validate(const struct sw_flow_match *match,
122
u64 key_attrs, u64 mask_attrs)
124
u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
125
u64 mask_allowed = key_attrs; /* At most allow all key attributes */
127
/* The following mask attributes allowed only if they
128
* pass the validation tests. */
129
mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
130
| (1ULL << OVS_KEY_ATTR_IPV6)
131
| (1ULL << OVS_KEY_ATTR_TCP)
132
| (1ULL << OVS_KEY_ATTR_UDP)
133
| (1ULL << OVS_KEY_ATTR_ICMP)
134
| (1ULL << OVS_KEY_ATTR_ICMPV6)
135
| (1ULL << OVS_KEY_ATTR_ARP)
136
| (1ULL << OVS_KEY_ATTR_ND));
138
/* Always allowed mask fields. */
139
mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
140
| (1ULL << OVS_KEY_ATTR_IN_PORT)
141
| (11ULL << OVS_KEY_ATTR_ETHERTYPE));
143
/* Check key attributes. */
144
if (match->key->eth.type == htons(ETH_P_ARP)
145
|| match->key->eth.type == htons(ETH_P_RARP)) {
146
key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
147
if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
148
mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
151
if (match->key->eth.type == htons(ETH_P_IP)) {
152
key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
153
if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
154
mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
156
if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
157
if (match->key->ip.proto == IPPROTO_UDP) {
158
key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
159
if (match->mask && (match->mask->key.ip.proto == 0xff))
160
mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
163
if (match->key->ip.proto == IPPROTO_TCP) {
164
key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
165
if (match->mask && (match->mask->key.ip.proto == 0xff))
166
mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
169
if (match->key->ip.proto == IPPROTO_ICMP) {
170
key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
171
if (match->mask && (match->mask->key.ip.proto == 0xff))
172
mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
177
if (match->key->eth.type == htons(ETH_P_IPV6)) {
178
key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
179
if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
180
mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
182
if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
183
if (match->key->ip.proto == IPPROTO_UDP) {
184
key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
185
if (match->mask && (match->mask->key.ip.proto == 0xff))
186
mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
189
if (match->key->ip.proto == IPPROTO_TCP) {
190
key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
191
if (match->mask && (match->mask->key.ip.proto == 0xff))
192
mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
195
if (match->key->ip.proto == IPPROTO_ICMPV6) {
196
key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
197
if (match->mask && (match->mask->key.ip.proto == 0xff))
198
mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
200
if (match->key->ipv6.tp.src ==
201
htons(NDISC_NEIGHBOUR_SOLICITATION) ||
202
match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
203
key_expected |= 1ULL << OVS_KEY_ATTR_ND;
204
if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
205
mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
211
if ((key_attrs & key_expected) != key_expected) {
212
/* Key attributes check failed. */
213
OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
214
key_attrs, key_expected);
218
if ((mask_attrs & mask_allowed) != mask_attrs) {
219
/* Mask attributes check failed. */
220
OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
221
mask_attrs, mask_allowed);
50
228
static int check_header(struct sk_buff *skb, int len)
52
230
if (unlikely(skb->len < len))
793
988
return offsetof(struct sw_flow_key, phy);
796
struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
797
struct sw_flow_key *key, int key_len)
799
struct sw_flow *flow;
800
struct hlist_node *n;
801
struct hlist_head *head;
991
static bool __cmp_key(const struct sw_flow_key *key1,
992
const struct sw_flow_key *key2, int key_start, int key_len)
994
return !memcmp((u8 *)key1 + key_start,
995
(u8 *)key2 + key_start, (key_len - key_start));
998
static bool __flow_cmp_key(const struct sw_flow *flow,
999
const struct sw_flow_key *key, int key_start, int key_len)
1001
return __cmp_key(&flow->key, key, key_start, key_len);
1004
static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
1005
const struct sw_flow_key *key, int key_start, int key_len)
1007
return __cmp_key(&flow->unmasked_key, key, key_start, key_len);
1010
bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
1011
const struct sw_flow_key *key, int key_len)
1014
key_start = flow_key_start(key);
1016
return __flow_cmp_unmasked_key(flow, key, key_start, key_len);
1020
struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
1021
struct sw_flow_match *match)
1023
struct sw_flow_key *unmasked = match->key;
1024
int key_len = match->range.end;
1025
struct sw_flow *flow;
1027
flow = ovs_flow_lookup(table, unmasked);
1028
if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_len)))
1034
static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
1035
const struct sw_flow_key *flow_key,
1036
struct sw_flow_mask *mask)
1038
struct sw_flow *flow;
1039
struct hlist_head *head;
1040
int key_start = mask->range.start;
1041
int key_len = mask->range.end;
806
key_start = flow_key_start(key);
807
hash = ovs_flow_hash(key, key_start, key_len);
809
_key = (u8 *) key + key_start;
1043
struct sw_flow_key masked_key;
1045
ovs_flow_key_mask(&masked_key, flow_key, mask);
1046
hash = ovs_flow_hash(&masked_key, key_start, key_len);
810
1047
head = find_bucket(table, hash);
811
hlist_for_each_entry_rcu(flow, n, head, hash_node[table->node_ver]) {
813
if (flow->hash == hash &&
814
!memcmp((u8 *)&flow->key + key_start, _key, key_len - key_start)) {
1048
hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
1049
if (flow->mask == mask &&
1050
__flow_cmp_key(flow, &masked_key, key_start, key_len))
821
void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
822
struct sw_flow_key *key, int key_len)
824
flow->hash = ovs_flow_hash(key, flow_key_start(key), key_len);
825
memcpy(&flow->key, key, sizeof(flow->key));
826
__flow_tbl_insert(table, flow);
829
void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
1056
struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
1057
const struct sw_flow_key *key)
1059
struct sw_flow *flow = NULL;
1060
struct sw_flow_mask *mask;
1062
list_for_each_entry_rcu(mask, tbl->mask_list, list) {
1063
flow = ovs_masked_flow_lookup(tbl, key, mask);
1064
if (flow) /* Found */
1072
void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow)
1074
flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start,
1075
flow->mask->range.end);
1076
__tbl_insert(table, flow);
1079
void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
1081
BUG_ON(table->count == 0);
831
1082
hlist_del_rcu(&flow->hash_node[table->node_ver]);
833
BUG_ON(table->count < 0);
836
1086
/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
853
1103
[OVS_KEY_ATTR_TUNNEL] = -1,
856
static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
857
const struct nlattr *a[], u64 *attrs)
859
const struct ovs_key_icmp *icmp_key;
860
const struct ovs_key_tcp *tcp_key;
861
const struct ovs_key_udp *udp_key;
863
switch (swkey->ip.proto) {
865
if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
867
*attrs &= ~(1 << OVS_KEY_ATTR_TCP);
869
*key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
870
tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
871
swkey->ipv4.tp.src = tcp_key->tcp_src;
872
swkey->ipv4.tp.dst = tcp_key->tcp_dst;
876
if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
878
*attrs &= ~(1 << OVS_KEY_ATTR_UDP);
880
*key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
881
udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
882
swkey->ipv4.tp.src = udp_key->udp_src;
883
swkey->ipv4.tp.dst = udp_key->udp_dst;
887
if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
889
*attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
891
*key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
892
icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
893
swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
894
swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
901
static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
902
const struct nlattr *a[], u64 *attrs)
904
const struct ovs_key_icmpv6 *icmpv6_key;
905
const struct ovs_key_tcp *tcp_key;
906
const struct ovs_key_udp *udp_key;
908
switch (swkey->ip.proto) {
910
if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
912
*attrs &= ~(1 << OVS_KEY_ATTR_TCP);
914
*key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
915
tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
916
swkey->ipv6.tp.src = tcp_key->tcp_src;
917
swkey->ipv6.tp.dst = tcp_key->tcp_dst;
921
if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
923
*attrs &= ~(1 << OVS_KEY_ATTR_UDP);
925
*key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
926
udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
927
swkey->ipv6.tp.src = udp_key->udp_src;
928
swkey->ipv6.tp.dst = udp_key->udp_dst;
932
if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
934
*attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
936
*key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
937
icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
938
swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
939
swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
941
if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
942
swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
943
const struct ovs_key_nd *nd_key;
945
if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
947
*attrs &= ~(1 << OVS_KEY_ATTR_ND);
949
*key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
950
nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
951
memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
952
sizeof(swkey->ipv6.nd.target));
953
memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
954
memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
962
static int parse_flow_nlattrs(const struct nlattr *attr,
963
const struct nlattr *a[], u64 *attrsp)
1106
static bool is_all_zero(const u8 *fp, size_t size)
1113
for (i = 0; i < size; i++)
1120
static int __parse_flow_nlattrs(const struct nlattr *attr,
1121
const struct nlattr *a[],
1122
u64 *attrsp, bool nz)
965
1124
const struct nlattr *nla;
970
1129
nla_for_each_nested(nla, attr, rem) {
971
1130
u16 type = nla_type(nla);
972
1131
int expected_len;
974
if (type > OVS_KEY_ATTR_MAX || attrs & (1ULL << type))
1133
if (type > OVS_KEY_ATTR_MAX) {
1134
OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
1135
type, OVS_KEY_ATTR_MAX);
1138
if (attrs & (1ULL << type)) {
1139
OVS_NLERR("Duplicate key attribute (type %d).\n", type);
977
1143
expected_len = ovs_key_lens[type];
978
if (nla_len(nla) != expected_len && expected_len != -1)
1144
if (nla_len(nla) != expected_len && expected_len != -1) {
1145
OVS_NLERR("Key attribute has unexpected length (type=%d"
1146
", length=%d, expected=%d).\n", type,
1147
nla_len(nla), expected_len);
981
attrs |= 1ULL << type;
1151
if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
1152
attrs |= 1ULL << type;
1157
OVS_NLERR("Message has %d unknown bytes.\n", rem);
987
1161
*attrsp = attrs;
1165
static int parse_flow_mask_nlattrs(const struct nlattr *attr,
1166
const struct nlattr *a[], u64 *attrsp)
1168
return __parse_flow_nlattrs(attr, a, attrsp, true);
1171
static int parse_flow_nlattrs(const struct nlattr *attr,
1172
const struct nlattr *a[], u64 *attrsp)
1174
return __parse_flow_nlattrs(attr, a, attrsp, false);
991
1177
int ipv4_tun_from_nlattr(const struct nlattr *attr,
992
struct ovs_key_ipv4_tunnel *tun_key)
1178
struct sw_flow_match *match, bool is_mask)
994
1180
struct nlattr *a;
996
1182
bool ttl = false;
998
memset(tun_key, 0, sizeof(*tun_key));
1183
__be16 tun_flags = 0;
1000
1185
nla_for_each_nested(a, attr, rem) {
1001
1186
int type = nla_type(a);
1091
* ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
1092
* @swkey: receives the extracted flow key.
1093
* @key_lenp: number of bytes used in @swkey.
1094
* @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1097
int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
1098
const struct nlattr *attr)
1302
static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
1303
const struct nlattr **a, bool is_mask)
1100
const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1101
const struct ovs_key_ethernet *eth_key;
1106
memset(swkey, 0, sizeof(struct sw_flow_key));
1107
key_len = SW_FLOW_KEY_OFFSET(eth);
1109
err = parse_flow_nlattrs(attr, a, &attrs);
1113
/* Metadata attributes. */
1114
if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1115
swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
1116
attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1305
if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
1306
SW_FLOW_KEY_PUT(match, phy.priority,
1307
nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1308
*attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
1118
if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1311
if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
1119
1312
u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1120
if (in_port >= DP_MAX_PORTS)
1315
in_port = 0xffffffff; /* Always exact match in_port. */
1316
else if (in_port >= DP_MAX_PORTS)
1121
1317
return -EINVAL;
1122
swkey->phy.in_port = in_port;
1123
attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1125
swkey->phy.in_port = DP_MAX_PORTS;
1319
SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1320
*attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
1321
} else if (!is_mask) {
1322
SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1127
if (attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1325
if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
1128
1326
uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1129
1327
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1328
if (!is_mask && mark != 0) {
1329
OVS_NLERR("skb->mark must be zero on this kernel (mark=%d).\n", mark);
1131
1330
return -EINVAL;
1133
swkey->phy.skb_mark = mark;
1134
attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1137
if (attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1138
err = ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], &swkey->tun_key);
1142
attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
1145
/* Data attributes. */
1146
if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1148
attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1150
eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1151
memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1152
memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1154
if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1155
nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1156
const struct nlattr *encap;
1333
SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1334
*attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
1336
if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1337
if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1340
*attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
1345
static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
1346
const struct nlattr **a, bool is_mask)
1349
u64 orig_attrs = attrs;
1351
err = metadata_from_nlattrs(match, &attrs, a, is_mask);
1355
if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
1356
const struct ovs_key_ethernet *eth_key;
1358
eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1359
SW_FLOW_KEY_MEMCPY(match, eth.src,
1360
eth_key->eth_src, ETH_ALEN, is_mask);
1361
SW_FLOW_KEY_MEMCPY(match, eth.dst,
1362
eth_key->eth_dst, ETH_ALEN, is_mask);
1363
attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
1366
if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
1159
if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1160
(1 << OVS_KEY_ATTR_ETHERTYPE) |
1161
(1 << OVS_KEY_ATTR_ENCAP)))
1164
encap = a[OVS_KEY_ATTR_ENCAP];
1165
1369
tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1166
if (tci & htons(VLAN_TAG_PRESENT)) {
1167
swkey->eth.tci = tci;
1169
err = parse_flow_nlattrs(encap, a, &attrs);
1173
/* Corner case for truncated 802.1Q header. */
1177
swkey->eth.type = htons(ETH_P_8021Q);
1178
*key_lenp = key_len;
1185
if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1186
swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1187
if (ntohs(swkey->eth.type) < 1536)
1189
attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1191
swkey->eth.type = htons(ETH_P_802_2);
1194
if (swkey->eth.type == htons(ETH_P_IP)) {
1370
if (!(tci & htons(VLAN_TAG_PRESENT))) {
1372
OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
1374
OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
1379
SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
1380
attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
1381
} else if (!is_mask)
1382
SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1384
if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
1387
eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1389
/* Always exact match EtherType. */
1390
eth_type = htons(0xffff);
1391
} else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
1392
OVS_NLERR("EtherType is less than mimimum (type=%x, min=%x).\n",
1393
ntohs(eth_type), ETH_P_802_3_MIN);
1397
SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1398
attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1399
} else if (!is_mask) {
1400
SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1403
if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1195
1404
const struct ovs_key_ipv4 *ipv4_key;
1197
if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1199
attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1201
key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1202
1406
ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1203
if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1407
if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1408
OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
1409
ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1204
1410
return -EINVAL;
1205
swkey->ip.proto = ipv4_key->ipv4_proto;
1206
swkey->ip.tos = ipv4_key->ipv4_tos;
1207
swkey->ip.ttl = ipv4_key->ipv4_ttl;
1208
swkey->ip.frag = ipv4_key->ipv4_frag;
1209
swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1210
swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1412
SW_FLOW_KEY_PUT(match, ip.proto,
1413
ipv4_key->ipv4_proto, is_mask);
1414
SW_FLOW_KEY_PUT(match, ip.tos,
1415
ipv4_key->ipv4_tos, is_mask);
1416
SW_FLOW_KEY_PUT(match, ip.ttl,
1417
ipv4_key->ipv4_ttl, is_mask);
1418
SW_FLOW_KEY_PUT(match, ip.frag,
1419
ipv4_key->ipv4_frag, is_mask);
1420
SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1421
ipv4_key->ipv4_src, is_mask);
1422
SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1423
ipv4_key->ipv4_dst, is_mask);
1424
attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
1212
if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1213
err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1217
} else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1427
if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
1218
1428
const struct ovs_key_ipv6 *ipv6_key;
1220
if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1222
attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1224
key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1225
1430
ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1226
if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1431
if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1432
OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
1433
ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1227
1434
return -EINVAL;
1228
swkey->ipv6.label = ipv6_key->ipv6_label;
1229
swkey->ip.proto = ipv6_key->ipv6_proto;
1230
swkey->ip.tos = ipv6_key->ipv6_tclass;
1231
swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1232
swkey->ip.frag = ipv6_key->ipv6_frag;
1233
memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1234
sizeof(swkey->ipv6.addr.src));
1235
memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1236
sizeof(swkey->ipv6.addr.dst));
1238
if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1239
err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1243
} else if (swkey->eth.type == htons(ETH_P_ARP) ||
1244
swkey->eth.type == htons(ETH_P_RARP)) {
1436
SW_FLOW_KEY_PUT(match, ipv6.label,
1437
ipv6_key->ipv6_label, is_mask);
1438
SW_FLOW_KEY_PUT(match, ip.proto,
1439
ipv6_key->ipv6_proto, is_mask);
1440
SW_FLOW_KEY_PUT(match, ip.tos,
1441
ipv6_key->ipv6_tclass, is_mask);
1442
SW_FLOW_KEY_PUT(match, ip.ttl,
1443
ipv6_key->ipv6_hlimit, is_mask);
1444
SW_FLOW_KEY_PUT(match, ip.frag,
1445
ipv6_key->ipv6_frag, is_mask);
1446
SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1448
sizeof(match->key->ipv6.addr.src),
1450
SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1452
sizeof(match->key->ipv6.addr.dst),
1455
attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
1458
if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
1245
1459
const struct ovs_key_arp *arp_key;
1247
if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1249
attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1251
key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1252
1461
arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1253
swkey->ipv4.addr.src = arp_key->arp_sip;
1254
swkey->ipv4.addr.dst = arp_key->arp_tip;
1255
if (arp_key->arp_op & htons(0xff00))
1462
if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1463
OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
1256
1465
return -EINVAL;
1257
swkey->ip.proto = ntohs(arp_key->arp_op);
1258
memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1259
memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1264
*key_lenp = key_len;
1468
SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1469
arp_key->arp_sip, is_mask);
1470
SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1471
arp_key->arp_tip, is_mask);
1472
SW_FLOW_KEY_PUT(match, ip.proto,
1473
ntohs(arp_key->arp_op), is_mask);
1474
SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1475
arp_key->arp_sha, ETH_ALEN, is_mask);
1476
SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1477
arp_key->arp_tha, ETH_ALEN, is_mask);
1479
attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
1482
if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
1483
const struct ovs_key_tcp *tcp_key;
1485
tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1486
if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1487
SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1488
tcp_key->tcp_src, is_mask);
1489
SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1490
tcp_key->tcp_dst, is_mask);
1492
SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1493
tcp_key->tcp_src, is_mask);
1494
SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1495
tcp_key->tcp_dst, is_mask);
1497
attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
1500
if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
1501
const struct ovs_key_udp *udp_key;
1503
udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1504
if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1505
SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1506
udp_key->udp_src, is_mask);
1507
SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1508
udp_key->udp_dst, is_mask);
1510
SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1511
udp_key->udp_src, is_mask);
1512
SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1513
udp_key->udp_dst, is_mask);
1515
attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
1518
if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
1519
const struct ovs_key_icmp *icmp_key;
1521
icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1522
SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1523
htons(icmp_key->icmp_type), is_mask);
1524
SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1525
htons(icmp_key->icmp_code), is_mask);
1526
attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
1529
if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
1530
const struct ovs_key_icmpv6 *icmpv6_key;
1532
icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1533
SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1534
htons(icmpv6_key->icmpv6_type), is_mask);
1535
SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1536
htons(icmpv6_key->icmpv6_code), is_mask);
1537
attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
1540
if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
1541
const struct ovs_key_nd *nd_key;
1543
nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1544
SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1546
sizeof(match->key->ipv6.nd.target),
1548
SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1549
nd_key->nd_sll, ETH_ALEN, is_mask);
1550
SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1551
nd_key->nd_tll, ETH_ALEN, is_mask);
1552
attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
1562
* ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
1563
* mask. In case the 'mask' is NULL, the flow is treated as exact match
1564
* flow. Otherwise, it is treated as a wildcarded flow, except the mask
1565
* does not include any don't care bit.
1566
* @match: receives the extracted flow match information.
1567
* @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1568
* sequence. The fields should of the packet that triggered the creation
1570
* @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1571
* attribute specifies the mask field of the wildcarded flow.
1573
int ovs_match_from_nlattrs(struct sw_flow_match *match,
1574
const struct nlattr *key,
1575
const struct nlattr *mask)
1577
const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1578
const struct nlattr *encap;
1581
bool encap_valid = false;
1584
err = parse_flow_nlattrs(key, a, &key_attrs);
1588
if (key_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1589
encap = a[OVS_KEY_ATTR_ENCAP];
1590
key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1591
if (nla_len(encap)) {
1592
__be16 eth_type = 0; /* ETH_P_8021Q */
1594
if (a[OVS_KEY_ATTR_ETHERTYPE])
1595
eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1597
if ((eth_type == htons(ETH_P_8021Q)) && (a[OVS_KEY_ATTR_VLAN])) {
1599
key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1600
err = parse_flow_nlattrs(encap, a, &key_attrs);
1602
OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
1611
err = ovs_key_from_nlattrs(match, key_attrs, a, false);
1616
err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
1620
if ((mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) && encap_valid) {
1621
__be16 eth_type = 0;
1623
mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1624
if (a[OVS_KEY_ATTR_ETHERTYPE])
1625
eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1626
if (eth_type == htons(0xffff)) {
1627
mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1628
encap = a[OVS_KEY_ATTR_ENCAP];
1629
err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
1631
OVS_NLERR("VLAN frames must have an exact match"
1632
" on the TPID (mask=%x).\n",
1641
err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
1645
/* Populate exact match flow's key mask. */
1647
ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
1650
if (!ovs_match_validate(match, key_attrs, mask_attrs))
1270
1657
* ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1271
* @in_port: receives the extracted input port.
1272
* @tun_id: receives the extracted tunnel ID.
1273
* @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1658
* @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1659
* @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1276
1662
* This parses a series of Netlink attributes that form a flow key, which must
1279
1665
* extracted from the packet itself.
1282
int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow, int key_len, const struct nlattr *attr)
1668
int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1669
const struct nlattr *attr)
1284
1671
struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
1285
const struct nlattr *nla;
1672
const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1675
struct sw_flow_match match;
1288
1677
flow->key.phy.in_port = DP_MAX_PORTS;
1289
1678
flow->key.phy.priority = 0;
1290
1679
flow->key.phy.skb_mark = 0;
1291
1680
memset(tun_key, 0, sizeof(flow->key.tun_key));
1293
nla_for_each_nested(nla, attr, rem) {
1294
int type = nla_type(nla);
1296
if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1299
if (nla_len(nla) != ovs_key_lens[type])
1303
case OVS_KEY_ATTR_PRIORITY:
1304
flow->key.phy.priority = nla_get_u32(nla);
1307
case OVS_KEY_ATTR_TUNNEL:
1308
err = ipv4_tun_from_nlattr(nla, tun_key);
1313
case OVS_KEY_ATTR_IN_PORT:
1314
if (nla_get_u32(nla) >= DP_MAX_PORTS)
1316
flow->key.phy.in_port = nla_get_u32(nla);
1319
case OVS_KEY_ATTR_SKB_MARK:
1320
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1321
if (nla_get_u32(nla) != 0)
1324
flow->key.phy.skb_mark = nla_get_u32(nla);
1682
err = parse_flow_nlattrs(attr, a, &attrs);
1330
1684
return -EINVAL;
1332
flow->hash = ovs_flow_hash(&flow->key,
1333
flow_key_start(&flow->key), key_len);
1686
memset(&match, 0, sizeof(match));
1687
match.key = &flow->key;
1689
err = metadata_from_nlattrs(&match, &attrs, a, false);
1338
int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
1696
int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
1697
const struct sw_flow_key *output, struct sk_buff *skb)
1340
1699
struct ovs_key_ethernet *eth_key;
1341
1700
struct nlattr *nla, *encap;
1343
if (swkey->phy.priority &&
1344
nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
1345
goto nla_put_failure;
1347
if (swkey->tun_key.ipv4_dst &&
1348
ipv4_tun_to_nlattr(skb, &swkey->tun_key))
1349
goto nla_put_failure;
1351
if (swkey->phy.in_port != DP_MAX_PORTS &&
1352
nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
1353
goto nla_put_failure;
1355
if (swkey->phy.skb_mark &&
1356
nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, swkey->phy.skb_mark))
1701
bool is_mask = (swkey != output);
1703
if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1704
goto nla_put_failure;
1706
if ((swkey->tun_key.ipv4_dst || is_mask) &&
1707
ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
1708
goto nla_put_failure;
1710
if (swkey->phy.in_port == DP_MAX_PORTS) {
1711
if (is_mask && (output->phy.in_port == 0xffff))
1712
if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1713
goto nla_put_failure;
1716
upper_u16 = !is_mask ? 0 : 0xffff;
1718
if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1719
(upper_u16 << 16) | output->phy.in_port))
1720
goto nla_put_failure;
1723
if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1357
1724
goto nla_put_failure;
1359
1726
nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1361
1728
goto nla_put_failure;
1362
1730
eth_key = nla_data(nla);
1363
memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1364
memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1731
memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
1732
memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
1366
1734
if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1367
if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
1368
nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
1736
eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1737
if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1738
nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1369
1739
goto nla_put_failure;
1370
1740
encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1371
1741
if (!swkey->eth.tci)
1377
if (swkey->eth.type == htons(ETH_P_802_2))
1746
if (swkey->eth.type == htons(ETH_P_802_2)) {
1748
* Ethertype 802.2 is represented in the netlink with omitted
1749
* OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1750
* 0xffff in the mask attribute. Ethertype can also
1753
if (is_mask && output->eth.type)
1754
if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1756
goto nla_put_failure;
1380
if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
1760
if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1381
1761
goto nla_put_failure;
1383
1763
if (swkey->eth.type == htons(ETH_P_IP)) {