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>
23 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
27 #include <linux/list.h>
28 #include <linux/net.h>
29 #include <linux/udp.h>
41 * LISP encapsulation header:
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * |N|L|E|V|I|flags| Nonce/Map-Version |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Instance ID/Locator Status Bits |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * struct lisphdr - LISP header
53 * @nonce_present: Flag indicating the presence of a 24 bit nonce value.
54 * @locator_status_bits_present: Flag indicating the presence of Locator Status
56 * @solicit_echo_nonce: Flag indicating the use of the echo noncing mechanism.
57 * @map_version_present: Flag indicating the use of mapping versioning.
58 * @instance_id_present: Flag indicating the presence of a 24 bit Instance ID.
59 * @reserved_flags: 3 bits reserved for future flags.
60 * @nonce: 24 bit nonce value.
61 * @map_version: 24 bit mapping version.
62 * @locator_status_bits: Locator Status Bits: 32 bits when instance_id_present
63 * is not set, 8 bits when it is.
64 * @instance_id: 24 bit Instance ID
67 #ifdef __LITTLE_ENDIAN_BITFIELD
68 __u8 reserved_flags:3;
69 __u8 instance_id_present:1;
70 __u8 map_version_present:1;
71 __u8 solicit_echo_nonce:1;
72 __u8 locator_status_bits_present:1;
76 __u8 locator_status_bits_present:1;
77 __u8 solicit_echo_nonce:1;
78 __u8 map_version_present:1;
79 __u8 instance_id_present:1;
80 __u8 reserved_flags:3;
87 __be32 locator_status_bits;
90 __u8 locator_status_bits;
95 #define LISP_HLEN (sizeof(struct udphdr) + sizeof(struct lisphdr))
97 static inline int lisp_hdr_len(const struct tnl_mutable_config *mutable,
98 const struct ovs_key_ipv4_tunnel *tun_key)
104 * struct lisp_port - Keeps track of open UDP ports
105 * @list: list element.
106 * @port: The UDP port number in network byte order.
107 * @socket: The socket created for this port number.
108 * @count: How many ports are using this socket/port.
111 struct list_head list;
113 struct socket *lisp_rcv_socket;
117 static LIST_HEAD(lisp_ports);
119 static struct lisp_port *lisp_port_exists(struct net *net, __be16 port)
121 struct lisp_port *lisp_port;
123 list_for_each_entry(lisp_port, &lisp_ports, list) {
124 if (lisp_port->port == port &&
125 net_eq(sock_net(lisp_port->lisp_rcv_socket->sk), net))
132 static inline struct lisphdr *lisp_hdr(const struct sk_buff *skb)
134 return (struct lisphdr *)(udp_hdr(skb) + 1);
137 static int lisp_tnl_send(struct vport *vport, struct sk_buff *skb)
140 int network_offset = skb_network_offset(skb);
142 /* We only encapsulate IPv4 and IPv6 packets */
143 switch (skb->protocol) {
144 case htons(ETH_P_IP):
145 case htons(ETH_P_IPV6):
146 /* Pop off "inner" Ethernet header */
147 skb_pull(skb, network_offset);
148 tnl_len = ovs_tnl_send(vport, skb);
149 return tnl_len > 0 ? tnl_len + network_offset : tnl_len;
156 /* Convert 64 bit tunnel ID to 24 bit Instance ID. */
157 static void tunnel_id_to_instance_id(__be64 tun_id, __u8 *iid)
161 iid[0] = (__force __u8)(tun_id >> 16);
162 iid[1] = (__force __u8)(tun_id >> 8);
163 iid[2] = (__force __u8)tun_id;
165 iid[0] = (__force __u8)((__force u64)tun_id >> 40);
166 iid[1] = (__force __u8)((__force u64)tun_id >> 48);
167 iid[2] = (__force __u8)((__force u64)tun_id >> 56);
171 /* Convert 24 bit Instance ID to 64 bit tunnel ID. */
172 static __be64 instance_id_to_tunnel_id(__u8 *iid)
175 return (iid[0] << 16) | (iid[1] << 8) | iid[2];
177 return (__force __be64)(((__force u64)iid[0] << 40) |
178 ((__force u64)iid[1] << 48) |
179 ((__force u64)iid[2] << 56));
183 static struct sk_buff *lisp_build_header(const struct vport *vport,
184 const struct tnl_mutable_config *mutable,
185 struct dst_entry *dst,
189 struct udphdr *udph = udp_hdr(skb);
190 struct lisphdr *lisph = (struct lisphdr *)(udph + 1);
191 const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
195 tnl_get_param(mutable, tun_key, &flags, &out_key);
197 udph->dest = mutable->dst_port;
198 udph->source = htons(ovs_tnl_get_src_port(skb));
200 udph->len = htons(skb->len - skb_transport_offset(skb));
202 lisph->nonce_present = 0; /* We don't support echo nonce algorithm */
203 lisph->locator_status_bits_present = 1; /* Set LSB */
204 lisph->solicit_echo_nonce = 0; /* No echo noncing */
205 lisph->map_version_present = 0; /* No mapping versioning, nonce instead */
206 lisph->instance_id_present = 1; /* Store the tun_id as Instance ID */
207 lisph->reserved_flags = 0; /* Reserved flags, set to 0 */
209 lisph->u1.nonce[0] = 0;
210 lisph->u1.nonce[1] = 0;
211 lisph->u1.nonce[2] = 0;
213 tunnel_id_to_instance_id(out_key, &lisph->u2.word2.instance_id[0]);
214 lisph->u2.word2.locator_status_bits = 1;
217 * Allow our local IP stack to fragment the outer packet even if the
218 * DF bit is set as a last resort. We also need to force selection of
219 * an IP ID here because Linux will otherwise leave it at 0 if the
220 * packet originally had DF set.
223 __ip_select_ident(ip_hdr(skb), dst, 0);
228 /* Called with rcu_read_lock and BH disabled. */
229 static int lisp_rcv(struct sock *sk, struct sk_buff *skb)
232 struct lisphdr *lisph;
233 const struct tnl_mutable_config *mutable;
234 struct iphdr *iph, *inner_iph;
235 struct ovs_key_ipv4_tunnel tun_key;
237 u32 tunnel_flags = 0;
241 if (unlikely(!pskb_may_pull(skb, LISP_HLEN)))
244 lisph = lisp_hdr(skb);
246 skb_pull_rcsum(skb, LISP_HLEN);
248 if (lisph->instance_id_present != 1)
251 key = instance_id_to_tunnel_id(&lisph->u2.word2.instance_id[0]);
254 vport = ovs_tnl_find_port(dev_net(skb->dev), iph->daddr, iph->saddr,
255 key, TNL_T_PROTO_LISP, &mutable);
256 if (unlikely(!vport))
259 if (mutable->flags & TNL_F_IN_KEY_MATCH || !mutable->key.daddr)
260 tunnel_flags = OVS_TNL_F_KEY;
264 /* Save outer tunnel values */
265 tnl_tun_key_init(&tun_key, iph, key, tunnel_flags);
266 OVS_CB(skb)->tun_key = &tun_key;
268 /* Drop non-IP inner packets */
269 inner_iph = (struct iphdr *)(lisph + 1);
270 switch (inner_iph->version) {
272 protocol = htons(ETH_P_IP);
275 protocol = htons(ETH_P_IPV6);
281 /* Add Ethernet header */
282 ethh = (struct ethhdr *)skb_push(skb, ETH_HLEN);
283 memset(ethh, 0, ETH_HLEN);
284 ethh->h_dest[0] = 0x02;
285 ethh->h_source[0] = 0x02;
286 ethh->h_proto = protocol;
288 ovs_tnl_rcv(vport, skb);
297 /* Arbitrary value. Irrelevant as long as it's not 0 since we set the handler. */
298 #define UDP_ENCAP_LISP 1
299 static int lisp_socket_init(struct lisp_port *lisp_port, struct net *net)
302 struct sockaddr_in sin;
304 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0,
305 &lisp_port->lisp_rcv_socket);
309 /* release net ref. */
310 sk_change_net(lisp_port->lisp_rcv_socket->sk, net);
312 sin.sin_family = AF_INET;
313 sin.sin_addr.s_addr = htonl(INADDR_ANY);
314 sin.sin_port = lisp_port->port;
316 err = kernel_bind(lisp_port->lisp_rcv_socket, (struct sockaddr *)&sin,
317 sizeof(struct sockaddr_in));
321 udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_type = UDP_ENCAP_LISP;
322 udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_rcv = lisp_rcv;
329 sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
331 pr_warn("cannot register lisp protocol handler: %d\n", err);
335 static void lisp_tunnel_release(struct lisp_port *lisp_port)
339 if (lisp_port->count == 0) {
340 /* Release old socket */
341 sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
342 list_del(&lisp_port->list);
347 static int lisp_tunnel_setup(struct net *net, struct nlattr *options,
348 struct lisp_port **lport)
353 struct lisp_port *lisp_port = NULL;
362 a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
363 if (a && nla_len(a) == sizeof(u16)) {
364 dst_port = nla_get_u16(a);
366 /* Require destination port from userspace. */
371 /* Verify if we already have a socket created for this port */
372 lisp_port = lisp_port_exists(net, htons(dst_port));
380 /* Add a new socket for this port */
381 lisp_port = kzalloc(sizeof(struct lisp_port), GFP_KERNEL);
387 lisp_port->port = htons(dst_port);
388 lisp_port->count = 1;
389 list_add_tail(&lisp_port->list, &lisp_ports);
391 err = lisp_socket_init(lisp_port, net);
399 list_del(&lisp_port->list);
405 static int lisp_tnl_set_options(struct vport *vport, struct nlattr *options)
408 struct net *net = ovs_dp_get_net(vport->dp);
409 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
410 struct tnl_mutable_config *config;
411 struct lisp_port *old_port = NULL;
412 struct lisp_port *lisp_port = NULL;
414 config = rtnl_dereference(tnl_vport->mutable);
416 old_port = lisp_port_exists(net, config->dst_port);
418 err = lisp_tunnel_setup(net, options, &lisp_port);
422 err = ovs_tnl_set_options(vport, options);
425 lisp_tunnel_release(lisp_port);
427 /* Release old socket */
428 lisp_tunnel_release(old_port);
434 static const struct tnl_ops ovs_lisp_tnl_ops = {
435 .tunnel_type = TNL_T_PROTO_LISP,
436 .ipproto = IPPROTO_UDP,
437 .hdr_len = lisp_hdr_len,
438 .build_header = lisp_build_header,
441 static void lisp_tnl_destroy(struct vport *vport)
443 struct lisp_port *lisp_port;
444 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
445 struct tnl_mutable_config *config;
447 config = rtnl_dereference(tnl_vport->mutable);
449 lisp_port = lisp_port_exists(ovs_dp_get_net(vport->dp),
452 lisp_tunnel_release(lisp_port);
454 ovs_tnl_destroy(vport);
457 static struct vport *lisp_tnl_create(const struct vport_parms *parms)
461 struct lisp_port *lisp_port = NULL;
463 err = lisp_tunnel_setup(ovs_dp_get_net(parms->dp), parms->options,
468 vport = ovs_tnl_create(parms, &ovs_lisp_vport_ops, &ovs_lisp_tnl_ops);
471 lisp_tunnel_release(lisp_port);
476 const struct vport_ops ovs_lisp_vport_ops = {
477 .type = OVS_VPORT_TYPE_LISP,
478 .flags = VPORT_F_TUN_ID,
479 .create = lisp_tnl_create,
480 .destroy = lisp_tnl_destroy,
481 .get_name = ovs_tnl_get_name,
482 .get_options = ovs_tnl_get_options,
483 .set_options = lisp_tnl_set_options,
484 .send = lisp_tnl_send,
487 #warning LISP tunneling will not be available on kernels before 2.6.26
488 #endif /* Linux kernel < 2.6.26 */