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/net.h>
28 #include <linux/rculist.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 ovs_key_ipv4_tunnel *tun_key)
103 * struct lisp_port - Keeps track of open UDP ports
104 * @list: list element.
105 * @vport: vport for the tunnel.
106 * @socket: The socket created for this port number.
109 struct list_head list;
111 struct socket *lisp_rcv_socket;
115 static LIST_HEAD(lisp_ports);
117 static struct lisp_port *lisp_find_port(struct net *net, __be16 port)
119 struct lisp_port *lisp_port;
121 list_for_each_entry_rcu(lisp_port, &lisp_ports, list) {
122 struct tnl_vport *tnl_vport = tnl_vport_priv(lisp_port->vport);
124 if (tnl_vport->dst_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 void lisp_build_header(const struct vport *vport,
187 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
188 struct udphdr *udph = udp_hdr(skb);
189 struct lisphdr *lisph = (struct lisphdr *)(udph + 1);
190 const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
192 udph->dest = tnl_vport->dst_port;
193 udph->source = htons(ovs_tnl_get_src_port(skb));
195 udph->len = htons(skb->len - skb_transport_offset(skb));
197 lisph->nonce_present = 0; /* We don't support echo nonce algorithm */
198 lisph->locator_status_bits_present = 1; /* Set LSB */
199 lisph->solicit_echo_nonce = 0; /* No echo noncing */
200 lisph->map_version_present = 0; /* No mapping versioning, nonce instead */
201 lisph->instance_id_present = 1; /* Store the tun_id as Instance ID */
202 lisph->reserved_flags = 0; /* Reserved flags, set to 0 */
204 lisph->u1.nonce[0] = 0;
205 lisph->u1.nonce[1] = 0;
206 lisph->u1.nonce[2] = 0;
208 tunnel_id_to_instance_id(tun_key->tun_id, &lisph->u2.word2.instance_id[0]);
209 lisph->u2.word2.locator_status_bits = 1;
212 /* Called with rcu_read_lock and BH disabled. */
213 static int lisp_rcv(struct sock *sk, struct sk_buff *skb)
215 struct lisp_port *lisp_port;
216 struct lisphdr *lisph;
217 struct iphdr *iph, *inner_iph;
218 struct ovs_key_ipv4_tunnel tun_key;
223 lisp_port = lisp_find_port(dev_net(skb->dev), udp_hdr(skb)->dest);
224 if (unlikely(!lisp_port))
227 if (unlikely(!pskb_may_pull(skb, LISP_HLEN)))
230 lisph = lisp_hdr(skb);
232 skb_pull_rcsum(skb, LISP_HLEN);
234 if (lisph->instance_id_present != 1)
237 key = instance_id_to_tunnel_id(&lisph->u2.word2.instance_id[0]);
239 /* Save outer tunnel values */
241 tnl_tun_key_init(&tun_key, iph, key, OVS_TNL_F_KEY);
242 OVS_CB(skb)->tun_key = &tun_key;
244 /* Drop non-IP inner packets */
245 inner_iph = (struct iphdr *)(lisph + 1);
246 switch (inner_iph->version) {
248 protocol = htons(ETH_P_IP);
251 protocol = htons(ETH_P_IPV6);
257 /* Add Ethernet header */
258 ethh = (struct ethhdr *)skb_push(skb, ETH_HLEN);
259 memset(ethh, 0, ETH_HLEN);
260 ethh->h_dest[0] = 0x02;
261 ethh->h_source[0] = 0x02;
262 ethh->h_proto = protocol;
264 ovs_tnl_rcv(lisp_port->vport, skb);
273 /* Arbitrary value. Irrelevant as long as it's not 0 since we set the handler. */
274 #define UDP_ENCAP_LISP 1
275 static int lisp_socket_init(struct lisp_port *lisp_port, struct net *net)
278 struct sockaddr_in sin;
279 struct tnl_vport *tnl_vport = tnl_vport_priv(lisp_port->vport);
281 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0,
282 &lisp_port->lisp_rcv_socket);
286 /* release net ref. */
287 sk_change_net(lisp_port->lisp_rcv_socket->sk, net);
289 sin.sin_family = AF_INET;
290 sin.sin_addr.s_addr = htonl(INADDR_ANY);
291 sin.sin_port = tnl_vport->dst_port;
293 err = kernel_bind(lisp_port->lisp_rcv_socket, (struct sockaddr *)&sin,
294 sizeof(struct sockaddr_in));
298 udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_type = UDP_ENCAP_LISP;
299 udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_rcv = lisp_rcv;
306 sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
308 pr_warn("cannot register lisp protocol handler: %d\n", err);
313 static void free_port_rcu(struct rcu_head *rcu)
315 struct lisp_port *lisp_port = container_of(rcu,
316 struct lisp_port, rcu);
321 static void lisp_tunnel_release(struct lisp_port *lisp_port)
325 list_del_rcu(&lisp_port->list);
327 sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
328 call_rcu(&lisp_port->rcu, free_port_rcu);
331 static int lisp_tunnel_setup(struct net *net, struct vport *vport,
332 struct nlattr *options)
334 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
335 struct lisp_port *lisp_port;
345 a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
346 if (a && nla_len(a) == sizeof(u16)) {
347 dst_port = nla_get_u16(a);
349 /* Require destination port from userspace. */
354 /* Verify if we already have a socket created for this port */
355 lisp_port = lisp_find_port(net, htons(dst_port));
361 /* Add a new socket for this port */
362 lisp_port = kzalloc(sizeof(struct lisp_port), GFP_KERNEL);
368 tnl_vport->dst_port = htons(dst_port);
369 lisp_port->vport = vport;
370 list_add_tail_rcu(&lisp_port->list, &lisp_ports);
372 err = lisp_socket_init(lisp_port, net);
379 list_del_rcu(&lisp_port->list);
385 static int lisp_get_options(const struct vport *vport, struct sk_buff *skb)
387 const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
389 if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(tnl_vport->dst_port)))
394 static const struct tnl_ops ovs_lisp_tnl_ops = {
395 .ipproto = IPPROTO_UDP,
396 .hdr_len = lisp_hdr_len,
397 .build_header = lisp_build_header,
400 static void lisp_tnl_destroy(struct vport *vport)
402 struct lisp_port *lisp_port;
403 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
405 lisp_port = lisp_find_port(ovs_dp_get_net(vport->dp),
406 tnl_vport->dst_port);
408 lisp_tunnel_release(lisp_port);
409 ovs_tnl_destroy(vport);
412 static struct vport *lisp_tnl_create(const struct vport_parms *parms)
417 vport = ovs_tnl_create(parms, &ovs_lisp_vport_ops, &ovs_lisp_tnl_ops);
421 err = lisp_tunnel_setup(ovs_dp_get_net(parms->dp), vport,
424 ovs_tnl_destroy(vport);
431 const struct vport_ops ovs_lisp_vport_ops = {
432 .type = OVS_VPORT_TYPE_LISP,
433 .flags = VPORT_F_TUN_ID,
434 .create = lisp_tnl_create,
435 .destroy = lisp_tnl_destroy,
436 .get_name = ovs_tnl_get_name,
437 .get_options = lisp_get_options,
438 .send = lisp_tnl_send,
441 #warning LISP tunneling will not be available on kernels before 2.6.26
442 #endif /* Linux kernel < 2.6.26 */