-static struct lisp_port *lisp_find_port(struct net *net, __be16 port)
-{
- struct lisp_port *lisp_port;
-
- list_for_each_entry_rcu(lisp_port, &lisp_ports, list) {
- if (lisp_port->dst_port == port &&
- net_eq(sock_net(lisp_port->lisp_rcv_socket->sk), net))
- return lisp_port;
- }
-
- return NULL;
-}
-
-static inline struct lisphdr *lisp_hdr(const struct sk_buff *skb)
-{
- return (struct lisphdr *)(udp_hdr(skb) + 1);
-}
-
-/* Convert 64 bit tunnel ID to 24 bit Instance ID. */
-static void tunnel_id_to_instance_id(__be64 tun_id, __u8 *iid)
-{
-
-#ifdef __BIG_ENDIAN
- iid[0] = (__force __u8)(tun_id >> 16);
- iid[1] = (__force __u8)(tun_id >> 8);
- iid[2] = (__force __u8)tun_id;
-#else
- iid[0] = (__force __u8)((__force u64)tun_id >> 40);
- iid[1] = (__force __u8)((__force u64)tun_id >> 48);
- iid[2] = (__force __u8)((__force u64)tun_id >> 56);
-#endif
-}
-
-/* Convert 24 bit Instance ID to 64 bit tunnel ID. */
-static __be64 instance_id_to_tunnel_id(__u8 *iid)
-{
-#ifdef __BIG_ENDIAN
- return (iid[0] << 16) | (iid[1] << 8) | iid[2];
-#else
- return (__force __be64)(((__force u64)iid[0] << 40) |
- ((__force u64)iid[1] << 48) |
- ((__force u64)iid[2] << 56));
-#endif
-}
-
-/* Compute source UDP port for outgoing packet.
- * Currently we use the flow hash.
- */
-static u16 get_src_port(struct net *net, struct sk_buff *skb)
-{
- u32 hash = skb_get_hash(skb);
- unsigned int range;
- int high;
- int low;
-
- if (!hash) {
- if (skb->protocol == htons(ETH_P_IP)) {
- struct iphdr *iph;
- int size = (sizeof(iph->saddr) * 2) / sizeof(u32);
-
- iph = (struct iphdr *) skb_network_header(skb);
- hash = jhash2((const u32 *)&iph->saddr, size, 0);
- } else if (skb->protocol == htons(ETH_P_IPV6)) {
- struct ipv6hdr *ipv6hdr;
-
- ipv6hdr = (struct ipv6hdr *) skb_network_header(skb);
- hash = jhash2((const u32 *)&ipv6hdr->saddr,
- (sizeof(struct in6_addr) * 2) / sizeof(u32), 0);
- } else {
- pr_warn_once("LISP inner protocol is not IP when "
- "calculating hash.\n");
- }
- }
-
- inet_get_local_port_range(net, &low, &high);
- range = (high - low) + 1;
- return (((u64) hash * range) >> 32) + low;
-}
-
-static void lisp_build_header(struct sk_buff *skb)
-{
- struct lisphdr *lisph;
- const struct ovs_key_ipv4_tunnel *tun_key;
-
- tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
-
- lisph = (struct lisphdr *)__skb_push(skb, sizeof(struct lisphdr));
- lisph->nonce_present = 0; /* We don't support echo nonce algorithm */
- lisph->locator_status_bits_present = 1; /* Set LSB */
- lisph->solicit_echo_nonce = 0; /* No echo noncing */
- lisph->map_version_present = 0; /* No mapping versioning, nonce instead */
- lisph->instance_id_present = 1; /* Store the tun_id as Instance ID */
- lisph->reserved_flags = 0; /* Reserved flags, set to 0 */
-
- lisph->u1.nonce[0] = 0;
- lisph->u1.nonce[1] = 0;
- lisph->u1.nonce[2] = 0;
-
- tunnel_id_to_instance_id(tun_key->tun_id, &lisph->u2.word2.instance_id[0]);
- lisph->u2.word2.locator_status_bits = 1;
-}
-
-/* Called with rcu_read_lock and BH disabled. */
-static int lisp_rcv(struct sock *sk, struct sk_buff *skb)
-{
- struct lisp_port *lisp_port;
- struct lisphdr *lisph;
- struct iphdr *iph, *inner_iph;
- struct ovs_tunnel_info tun_info;
- __be64 key;
- struct ethhdr *ethh;
- __be16 protocol;
-
- lisp_port = rcu_dereference_sk_user_data(sk);
- if (unlikely(!lisp_port))
- goto error;
-
- if (iptunnel_pull_header(skb, LISP_HLEN, 0))
- goto error;
-
- lisph = lisp_hdr(skb);
-
- if (lisph->instance_id_present != 1)
- key = 0;
- else
- key = instance_id_to_tunnel_id(&lisph->u2.word2.instance_id[0]);
-
- /* Save outer tunnel values */
- iph = ip_hdr(skb);
- ovs_flow_tun_info_init(&tun_info, iph,
- udp_hdr(skb)->source, udp_hdr(skb)->dest,
- key, TUNNEL_KEY, NULL, 0);
-
- /* Drop non-IP inner packets */
- inner_iph = (struct iphdr *)(lisph + 1);
- switch (inner_iph->version) {
- case 4:
- protocol = htons(ETH_P_IP);
- break;
- case 6:
- protocol = htons(ETH_P_IPV6);
- break;
- default:
- goto error;
- }
- skb->protocol = protocol;
-
- /* Add Ethernet header */
- ethh = (struct ethhdr *)skb_push(skb, ETH_HLEN);
- memset(ethh, 0, ETH_HLEN);
- ethh->h_dest[0] = 0x02;
- ethh->h_source[0] = 0x02;
- ethh->h_proto = protocol;
-
- ovs_skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
-
- ovs_vport_receive(vport_from_priv(lisp_port), skb, &tun_info);
- goto out;
-
-error:
- kfree_skb(skb);
-out:
- return 0;
-}
-
-static int lisp_socket_init(struct lisp_port *lisp_port, struct net *net)
-{
- struct udp_port_cfg udp_conf;
- struct udp_tunnel_sock_cfg tunnel_cfg;
- int err;
-
- memset(&udp_conf, 0, sizeof(udp_conf));
-
- udp_conf.family = AF_INET;
- udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
- udp_conf.local_udp_port = lisp_port->dst_port;
-
- err = udp_sock_create(net, &udp_conf, &lisp_port->lisp_rcv_socket);
- if (err < 0) {
- pr_warn("cannot register lisp protocol handler: %d\n", err);
- return err;
- }
-
- tunnel_cfg.sk_user_data = lisp_port;
- tunnel_cfg.encap_type = 1;
- tunnel_cfg.encap_rcv = lisp_rcv;
- tunnel_cfg.encap_destroy = NULL;
-
- setup_udp_tunnel_sock(net, lisp_port->lisp_rcv_socket, &tunnel_cfg);
-
- return 0;
-}
-
-static int lisp_get_options(const struct vport *vport, struct sk_buff *skb)