+ int error = 0;
+ const struct nlattr *nla;
+ u16 prev_type;
+ int rem;
+ int key_len;
+
+ memset(swkey, 0, sizeof(*swkey));
+ swkey->eth.in_port = USHRT_MAX;
+ swkey->eth.type = htons(ETH_P_802_2);
+ key_len = SW_FLOW_KEY_OFFSET(eth);
+
+ prev_type = ODP_KEY_ATTR_UNSPEC;
+ nla_for_each_nested(nla, attr, rem) {
+ const struct odp_key_ethernet *eth_key;
+ const struct odp_key_8021q *q_key;
+ const struct odp_key_ipv4 *ipv4_key;
+ const struct odp_key_ipv6 *ipv6_key;
+ const struct odp_key_tcp *tcp_key;
+ const struct odp_key_udp *udp_key;
+ const struct odp_key_icmp *icmp_key;
+ const struct odp_key_icmpv6 *icmpv6_key;
+ const struct odp_key_arp *arp_key;
+ const struct odp_key_nd *nd_key;
+
+ int type = nla_type(nla);
+
+ if (type > ODP_KEY_ATTR_MAX || nla_len(nla) != key_lens[type])
+ goto invalid;
+
+#define TRANSITION(PREV_TYPE, TYPE) (((PREV_TYPE) << 16) | (TYPE))
+ switch (TRANSITION(prev_type, type)) {
+ case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_TUN_ID):
+ swkey->eth.tun_id = nla_get_be64(nla);
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_IN_PORT):
+ case TRANSITION(ODP_KEY_ATTR_TUN_ID, ODP_KEY_ATTR_IN_PORT):
+ if (nla_get_u32(nla) >= DP_MAX_PORTS)
+ goto invalid;
+ swkey->eth.in_port = nla_get_u32(nla);
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_ETHERNET):
+ case TRANSITION(ODP_KEY_ATTR_TUN_ID, ODP_KEY_ATTR_ETHERNET):
+ case TRANSITION(ODP_KEY_ATTR_IN_PORT, ODP_KEY_ATTR_ETHERNET):
+ eth_key = nla_data(nla);
+ memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
+ memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_8021Q):
+ q_key = nla_data(nla);
+ /* Only standard 0x8100 VLANs currently supported. */
+ if (q_key->q_tpid != htons(ETH_P_8021Q))
+ goto invalid;
+ if (q_key->q_tci & htons(VLAN_TAG_PRESENT))
+ goto invalid;
+ swkey->eth.tci = q_key->q_tci | htons(VLAN_TAG_PRESENT);
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_8021Q, ODP_KEY_ATTR_ETHERTYPE):
+ case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_ETHERTYPE):
+ swkey->eth.type = nla_get_be16(nla);
+ if (ntohs(swkey->eth.type) < 1536)
+ goto invalid;
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_IPV4):
+ key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
+ if (swkey->eth.type != htons(ETH_P_IP))
+ goto invalid;
+ ipv4_key = nla_data(nla);
+ swkey->ip.proto = ipv4_key->ipv4_proto;
+ swkey->ip.tos = ipv4_key->ipv4_tos;
+ swkey->ipv4.addr.src = ipv4_key->ipv4_src;
+ swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
+ if (swkey->ip.tos & INET_ECN_MASK)
+ goto invalid;
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_IPV6):
+ key_len = SW_FLOW_KEY_OFFSET(ipv6.addr);
+ if (swkey->eth.type != htons(ETH_P_IPV6))
+ goto invalid;
+ ipv6_key = nla_data(nla);
+ swkey->ip.proto = ipv6_key->ipv6_proto;
+ swkey->ip.tos = ipv6_key->ipv6_tos;
+ memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
+ sizeof(swkey->ipv6.addr.src));
+ memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
+ sizeof(swkey->ipv6.addr.dst));
+ if (swkey->ip.tos & INET_ECN_MASK)
+ goto invalid;
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_TCP):
+ key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
+ if (swkey->ip.proto != IPPROTO_TCP)
+ goto invalid;
+ tcp_key = nla_data(nla);
+ swkey->ipv4.tp.src = tcp_key->tcp_src;
+ swkey->ipv4.tp.dst = tcp_key->tcp_dst;
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_TCP):
+ key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
+ if (swkey->ip.proto != IPPROTO_TCP)
+ goto invalid;
+ tcp_key = nla_data(nla);
+ swkey->ipv6.tp.src = tcp_key->tcp_src;
+ swkey->ipv6.tp.dst = tcp_key->tcp_dst;
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_UDP):
+ key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
+ if (swkey->ip.proto != IPPROTO_UDP)
+ goto invalid;
+ udp_key = nla_data(nla);
+ swkey->ipv4.tp.src = udp_key->udp_src;
+ swkey->ipv4.tp.dst = udp_key->udp_dst;
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_UDP):
+ key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
+ if (swkey->ip.proto != IPPROTO_UDP)
+ goto invalid;
+ udp_key = nla_data(nla);
+ swkey->ipv6.tp.src = udp_key->udp_src;
+ swkey->ipv6.tp.dst = udp_key->udp_dst;
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_ICMP):
+ key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
+ if (swkey->ip.proto != IPPROTO_ICMP)
+ goto invalid;
+ icmp_key = nla_data(nla);
+ swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
+ swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_ICMPV6):
+ key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
+ if (swkey->ip.proto != IPPROTO_ICMPV6)
+ goto invalid;
+ icmpv6_key = nla_data(nla);
+ swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
+ swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_ARP):
+ key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
+ if (swkey->eth.type != htons(ETH_P_ARP))
+ goto invalid;
+ arp_key = nla_data(nla);
+ swkey->ipv4.addr.src = arp_key->arp_sip;
+ swkey->ipv4.addr.dst = arp_key->arp_tip;
+ if (arp_key->arp_op & htons(0xff00))
+ goto invalid;
+ swkey->ip.proto = ntohs(arp_key->arp_op);
+ memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
+ memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
+ break;
+
+ case TRANSITION(ODP_KEY_ATTR_ICMPV6, ODP_KEY_ATTR_ND):
+ key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
+ if (swkey->ipv6.tp.src != htons(NDISC_NEIGHBOUR_SOLICITATION)
+ && swkey->ipv6.tp.src != htons(NDISC_NEIGHBOUR_ADVERTISEMENT))
+ goto invalid;
+ nd_key = nla_data(nla);
+ memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
+ sizeof(swkey->ipv6.nd.target));
+ memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
+ memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
+ break;
+
+ default:
+ goto invalid;
+ }
+
+ prev_type = type;
+ }
+ if (rem)
+ goto invalid;
+
+ switch (prev_type) {
+ case ODP_KEY_ATTR_UNSPEC:
+ goto invalid;
+
+ case ODP_KEY_ATTR_TUN_ID:
+ case ODP_KEY_ATTR_IN_PORT:
+ goto invalid;
+
+ case ODP_KEY_ATTR_ETHERNET:
+ case ODP_KEY_ATTR_8021Q:
+ goto ok;
+
+ case ODP_KEY_ATTR_ETHERTYPE:
+ if (swkey->eth.type == htons(ETH_P_IP) ||
+ swkey->eth.type == htons(ETH_P_ARP))
+ goto invalid;
+ goto ok;
+
+ case ODP_KEY_ATTR_IPV4:
+ if (swkey->ip.proto == IPPROTO_TCP ||
+ swkey->ip.proto == IPPROTO_UDP ||
+ swkey->ip.proto == IPPROTO_ICMP)
+ goto invalid;
+ goto ok;
+
+ case ODP_KEY_ATTR_IPV6:
+ if (swkey->ip.proto == IPPROTO_TCP ||
+ swkey->ip.proto == IPPROTO_UDP ||
+ swkey->ip.proto == IPPROTO_ICMPV6)
+ goto invalid;
+ goto ok;
+
+ case ODP_KEY_ATTR_ICMPV6:
+ if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
+ swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT))
+ goto invalid;
+ goto ok;
+
+ case ODP_KEY_ATTR_TCP:
+ case ODP_KEY_ATTR_UDP:
+ case ODP_KEY_ATTR_ICMP:
+ case ODP_KEY_ATTR_ARP:
+ case ODP_KEY_ATTR_ND:
+ goto ok;
+
+ default:
+ WARN_ON_ONCE(1);
+ }
+
+invalid:
+ error = -EINVAL;
+
+ok:
+ WARN_ON_ONCE(!key_len && !error);
+ *key_lenp = key_len;
+ return error;