2 * Copyright (c) 2011 Nicira, Inc.
3 * Copyright (c) 2013 Cisco Systems, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/version.h>
26 #include <linux/net.h>
27 #include <linux/module.h>
28 #include <linux/rculist.h>
29 #include <linux/udp.h>
33 #include <net/route.h>
35 #include <net/udp_tunnel.h>
43 * LISP encapsulation header:
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * |N|L|E|V|I|flags| Nonce/Map-Version |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | Instance ID/Locator Status Bits |
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * struct lisphdr - LISP header
55 * @nonce_present: Flag indicating the presence of a 24 bit nonce value.
56 * @locator_status_bits_present: Flag indicating the presence of Locator Status
58 * @solicit_echo_nonce: Flag indicating the use of the echo noncing mechanism.
59 * @map_version_present: Flag indicating the use of mapping versioning.
60 * @instance_id_present: Flag indicating the presence of a 24 bit Instance ID.
61 * @reserved_flags: 3 bits reserved for future flags.
62 * @nonce: 24 bit nonce value.
63 * @map_version: 24 bit mapping version.
64 * @locator_status_bits: Locator Status Bits: 32 bits when instance_id_present
65 * is not set, 8 bits when it is.
66 * @instance_id: 24 bit Instance ID
69 #ifdef __LITTLE_ENDIAN_BITFIELD
70 __u8 reserved_flags:3;
71 __u8 instance_id_present:1;
72 __u8 map_version_present:1;
73 __u8 solicit_echo_nonce:1;
74 __u8 locator_status_bits_present:1;
78 __u8 locator_status_bits_present:1;
79 __u8 solicit_echo_nonce:1;
80 __u8 map_version_present:1;
81 __u8 instance_id_present:1;
82 __u8 reserved_flags:3;
89 __be32 locator_status_bits;
92 __u8 locator_status_bits;
97 #define LISP_HLEN (sizeof(struct udphdr) + sizeof(struct lisphdr))
100 * struct lisp_port - Keeps track of open UDP ports
101 * @dst_port: lisp UDP port no.
102 * @list: list element in @lisp_ports.
103 * @lisp_rcv_socket: The socket created for this port number.
108 struct list_head list;
109 struct socket *lisp_rcv_socket;
113 static LIST_HEAD(lisp_ports);
114 static struct vport_ops ovs_lisp_vport_ops;
116 static inline struct lisp_port *lisp_vport(const struct vport *vport)
118 return vport_priv(vport);
121 static struct lisp_port *lisp_find_port(struct net *net, __be16 port)
123 struct lisp_port *lisp_port;
125 list_for_each_entry_rcu(lisp_port, &lisp_ports, list) {
126 if (lisp_port->dst_port == port &&
127 net_eq(sock_net(lisp_port->lisp_rcv_socket->sk), net))
134 static inline struct lisphdr *lisp_hdr(const struct sk_buff *skb)
136 return (struct lisphdr *)(udp_hdr(skb) + 1);
139 /* Convert 64 bit tunnel ID to 24 bit Instance ID. */
140 static void tunnel_id_to_instance_id(__be64 tun_id, __u8 *iid)
144 iid[0] = (__force __u8)(tun_id >> 16);
145 iid[1] = (__force __u8)(tun_id >> 8);
146 iid[2] = (__force __u8)tun_id;
148 iid[0] = (__force __u8)((__force u64)tun_id >> 40);
149 iid[1] = (__force __u8)((__force u64)tun_id >> 48);
150 iid[2] = (__force __u8)((__force u64)tun_id >> 56);
154 /* Convert 24 bit Instance ID to 64 bit tunnel ID. */
155 static __be64 instance_id_to_tunnel_id(__u8 *iid)
158 return (iid[0] << 16) | (iid[1] << 8) | iid[2];
160 return (__force __be64)(((__force u64)iid[0] << 40) |
161 ((__force u64)iid[1] << 48) |
162 ((__force u64)iid[2] << 56));
166 /* Compute source UDP port for outgoing packet.
167 * Currently we use the flow hash.
169 static u16 get_src_port(struct net *net, struct sk_buff *skb)
171 u32 hash = skb_get_hash(skb);
177 if (skb->protocol == htons(ETH_P_IP)) {
179 int size = (sizeof(iph->saddr) * 2) / sizeof(u32);
181 iph = (struct iphdr *) skb_network_header(skb);
182 hash = jhash2((const u32 *)&iph->saddr, size, 0);
183 } else if (skb->protocol == htons(ETH_P_IPV6)) {
184 struct ipv6hdr *ipv6hdr;
186 ipv6hdr = (struct ipv6hdr *) skb_network_header(skb);
187 hash = jhash2((const u32 *)&ipv6hdr->saddr,
188 (sizeof(struct in6_addr) * 2) / sizeof(u32), 0);
190 pr_warn_once("LISP inner protocol is not IP when "
191 "calculating hash.\n");
195 inet_get_local_port_range(net, &low, &high);
196 range = (high - low) + 1;
197 return (((u64) hash * range) >> 32) + low;
200 static void lisp_build_header(struct sk_buff *skb)
202 struct lisphdr *lisph;
203 const struct ovs_key_ipv4_tunnel *tun_key;
205 tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
207 lisph = (struct lisphdr *)__skb_push(skb, sizeof(struct lisphdr));
208 lisph->nonce_present = 0; /* We don't support echo nonce algorithm */
209 lisph->locator_status_bits_present = 1; /* Set LSB */
210 lisph->solicit_echo_nonce = 0; /* No echo noncing */
211 lisph->map_version_present = 0; /* No mapping versioning, nonce instead */
212 lisph->instance_id_present = 1; /* Store the tun_id as Instance ID */
213 lisph->reserved_flags = 0; /* Reserved flags, set to 0 */
215 lisph->u1.nonce[0] = 0;
216 lisph->u1.nonce[1] = 0;
217 lisph->u1.nonce[2] = 0;
219 tunnel_id_to_instance_id(tun_key->tun_id, &lisph->u2.word2.instance_id[0]);
220 lisph->u2.word2.locator_status_bits = 1;
223 /* Called with rcu_read_lock and BH disabled. */
224 static int lisp_rcv(struct sock *sk, struct sk_buff *skb)
226 struct lisp_port *lisp_port;
227 struct lisphdr *lisph;
228 struct iphdr *iph, *inner_iph;
229 struct ovs_tunnel_info tun_info;
234 lisp_port = rcu_dereference_sk_user_data(sk);
235 if (unlikely(!lisp_port))
238 if (iptunnel_pull_header(skb, LISP_HLEN, 0))
241 lisph = lisp_hdr(skb);
243 if (lisph->instance_id_present != 1)
246 key = instance_id_to_tunnel_id(&lisph->u2.word2.instance_id[0]);
248 /* Save outer tunnel values */
250 ovs_flow_tun_info_init(&tun_info, iph,
251 udp_hdr(skb)->source, udp_hdr(skb)->dest,
252 key, TUNNEL_KEY, NULL, 0);
254 /* Drop non-IP inner packets */
255 inner_iph = (struct iphdr *)(lisph + 1);
256 switch (inner_iph->version) {
258 protocol = htons(ETH_P_IP);
261 protocol = htons(ETH_P_IPV6);
266 skb->protocol = protocol;
268 /* Add Ethernet header */
269 ethh = (struct ethhdr *)skb_push(skb, ETH_HLEN);
270 memset(ethh, 0, ETH_HLEN);
271 ethh->h_dest[0] = 0x02;
272 ethh->h_source[0] = 0x02;
273 ethh->h_proto = protocol;
275 ovs_skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
277 ovs_vport_receive(vport_from_priv(lisp_port), skb, &tun_info);
286 static int lisp_socket_init(struct lisp_port *lisp_port, struct net *net)
288 struct udp_port_cfg udp_conf;
289 struct udp_tunnel_sock_cfg tunnel_cfg;
292 memset(&udp_conf, 0, sizeof(udp_conf));
294 udp_conf.family = AF_INET;
295 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
296 udp_conf.local_udp_port = lisp_port->dst_port;
298 err = udp_sock_create(net, &udp_conf, &lisp_port->lisp_rcv_socket);
300 pr_warn("cannot register lisp protocol handler: %d\n", err);
304 tunnel_cfg.sk_user_data = lisp_port;
305 tunnel_cfg.encap_type = 1;
306 tunnel_cfg.encap_rcv = lisp_rcv;
307 tunnel_cfg.encap_destroy = NULL;
309 setup_udp_tunnel_sock(net, lisp_port->lisp_rcv_socket, &tunnel_cfg);
314 static int lisp_get_options(const struct vport *vport, struct sk_buff *skb)
316 struct lisp_port *lisp_port = lisp_vport(vport);
318 if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(lisp_port->dst_port)))
323 static void lisp_tnl_destroy(struct vport *vport)
325 struct lisp_port *lisp_port = lisp_vport(vport);
327 list_del_rcu(&lisp_port->list);
328 udp_tunnel_sock_release(lisp_port->lisp_rcv_socket);
329 ovs_vport_deferred_free(vport);
332 static struct vport *lisp_tnl_create(const struct vport_parms *parms)
334 struct net *net = ovs_dp_get_net(parms->dp);
335 struct nlattr *options = parms->options;
336 struct lisp_port *lisp_port;
347 a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
348 if (a && nla_len(a) == sizeof(u16)) {
349 dst_port = nla_get_u16(a);
351 /* Require destination port from userspace. */
356 /* Verify if we already have a socket created for this port */
357 if (lisp_find_port(net, htons(dst_port))) {
362 vport = ovs_vport_alloc(sizeof(struct lisp_port),
363 &ovs_lisp_vport_ops, parms);
367 lisp_port = lisp_vport(vport);
368 lisp_port->dst_port = htons(dst_port);
369 strncpy(lisp_port->name, parms->name, IFNAMSIZ);
371 err = lisp_socket_init(lisp_port, net);
375 list_add_tail_rcu(&lisp_port->list, &lisp_ports);
379 ovs_vport_free(vport);
384 static int lisp_send(struct vport *vport, struct sk_buff *skb)
386 struct ovs_key_ipv4_tunnel *tun_key;
387 struct lisp_port *lisp_port = lisp_vport(vport);
388 struct net *net = ovs_dp_get_net(vport->dp);
389 int network_offset = skb_network_offset(skb);
393 __be16 src_port, dst_port;
398 if (unlikely(!OVS_CB(skb)->egress_tun_info)) {
403 tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
405 if (skb->protocol != htons(ETH_P_IP) &&
406 skb->protocol != htons(ETH_P_IPV6)) {
412 saddr = tun_key->ipv4_src;
413 rt = find_route(ovs_dp_get_net(vport->dp),
414 &saddr, tun_key->ipv4_dst,
415 IPPROTO_UDP, tun_key->ipv4_tos,
422 min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
423 + sizeof(struct iphdr) + LISP_HLEN;
425 if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
426 int head_delta = SKB_DATA_ALIGN(min_headroom -
430 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
436 /* Reset l2 headers. */
437 skb_pull(skb, network_offset);
438 skb_reset_mac_header(skb);
439 vlan_set_tci(skb, 0);
441 skb = udp_tunnel_handle_offloads(skb, false, false);
448 src_port = htons(get_src_port(net, skb));
449 dst_port = lisp_port->dst_port;
451 lisp_build_header(skb);
455 ovs_skb_set_inner_protocol(skb, skb->protocol);
457 df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
458 sent_len = udp_tunnel_xmit_skb(rt, lisp_port->lisp_rcv_socket->sk, skb,
459 saddr, tun_key->ipv4_dst,
460 tun_key->ipv4_tos, tun_key->ipv4_ttl,
461 df, src_port, dst_port, false, true);
463 return sent_len > 0 ? sent_len + network_offset : sent_len;
472 static const char *lisp_get_name(const struct vport *vport)
474 struct lisp_port *lisp_port = lisp_vport(vport);
475 return lisp_port->name;
478 static int lisp_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
479 struct ovs_tunnel_info *egress_tun_info)
481 struct net *net = ovs_dp_get_net(vport->dp);
482 struct lisp_port *lisp_port = lisp_vport(vport);
484 if (skb->protocol != htons(ETH_P_IP) &&
485 skb->protocol != htons(ETH_P_IPV6)) {
490 * Get tp_src and tp_dst, refert to lisp_build_header().
492 return ovs_tunnel_get_egress_info(egress_tun_info, net,
493 OVS_CB(skb)->egress_tun_info,
494 IPPROTO_UDP, skb->mark,
495 htons(get_src_port(net, skb)),
496 lisp_port->dst_port);
499 static struct vport_ops ovs_lisp_vport_ops = {
500 .type = OVS_VPORT_TYPE_LISP,
501 .create = lisp_tnl_create,
502 .destroy = lisp_tnl_destroy,
503 .get_name = lisp_get_name,
504 .get_options = lisp_get_options,
506 .get_egress_tun_info = lisp_get_egress_tun_info,
507 .owner = THIS_MODULE,
510 static int __init ovs_lisp_tnl_init(void)
512 return ovs_vport_ops_register(&ovs_lisp_vport_ops);
515 static void __exit ovs_lisp_tnl_exit(void)
517 ovs_vport_ops_unregister(&ovs_lisp_vport_ops);
520 module_init(ovs_lisp_tnl_init);
521 module_exit(ovs_lisp_tnl_exit);
523 MODULE_DESCRIPTION("OVS: LISP switching port");
524 MODULE_LICENSE("GPL");
525 MODULE_ALIAS("vport-type-105");