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 * Allow our local IP stack to fragment the outer packet even if the
213 * DF bit is set as a last resort. We also need to force selection of
214 * an IP ID here because Linux will otherwise leave it at 0 if the
215 * packet originally had DF set.
218 __ip_select_ident(ip_hdr(skb), skb_dst(skb), 0);
221 /* Called with rcu_read_lock and BH disabled. */
222 static int lisp_rcv(struct sock *sk, struct sk_buff *skb)
224 struct lisp_port *lisp_port;
225 struct lisphdr *lisph;
226 struct iphdr *iph, *inner_iph;
227 struct ovs_key_ipv4_tunnel tun_key;
232 lisp_port = lisp_find_port(dev_net(skb->dev), udp_hdr(skb)->dest);
233 if (unlikely(!lisp_port))
236 if (unlikely(!pskb_may_pull(skb, LISP_HLEN)))
239 lisph = lisp_hdr(skb);
241 skb_pull_rcsum(skb, LISP_HLEN);
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 tnl_tun_key_init(&tun_key, iph, key, OVS_TNL_F_KEY);
251 OVS_CB(skb)->tun_key = &tun_key;
253 /* Drop non-IP inner packets */
254 inner_iph = (struct iphdr *)(lisph + 1);
255 switch (inner_iph->version) {
257 protocol = htons(ETH_P_IP);
260 protocol = htons(ETH_P_IPV6);
266 /* Add Ethernet header */
267 ethh = (struct ethhdr *)skb_push(skb, ETH_HLEN);
268 memset(ethh, 0, ETH_HLEN);
269 ethh->h_dest[0] = 0x02;
270 ethh->h_source[0] = 0x02;
271 ethh->h_proto = protocol;
273 ovs_tnl_rcv(lisp_port->vport, skb);
282 /* Arbitrary value. Irrelevant as long as it's not 0 since we set the handler. */
283 #define UDP_ENCAP_LISP 1
284 static int lisp_socket_init(struct lisp_port *lisp_port, struct net *net)
287 struct sockaddr_in sin;
288 struct tnl_vport *tnl_vport = tnl_vport_priv(lisp_port->vport);
290 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0,
291 &lisp_port->lisp_rcv_socket);
295 /* release net ref. */
296 sk_change_net(lisp_port->lisp_rcv_socket->sk, net);
298 sin.sin_family = AF_INET;
299 sin.sin_addr.s_addr = htonl(INADDR_ANY);
300 sin.sin_port = tnl_vport->dst_port;
302 err = kernel_bind(lisp_port->lisp_rcv_socket, (struct sockaddr *)&sin,
303 sizeof(struct sockaddr_in));
307 udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_type = UDP_ENCAP_LISP;
308 udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_rcv = lisp_rcv;
315 sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
317 pr_warn("cannot register lisp protocol handler: %d\n", err);
322 static void free_port_rcu(struct rcu_head *rcu)
324 struct lisp_port *lisp_port = container_of(rcu,
325 struct lisp_port, rcu);
330 static void lisp_tunnel_release(struct lisp_port *lisp_port)
334 list_del_rcu(&lisp_port->list);
336 sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
337 call_rcu(&lisp_port->rcu, free_port_rcu);
340 static int lisp_tunnel_setup(struct net *net, struct vport *vport,
341 struct nlattr *options)
343 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
344 struct lisp_port *lisp_port;
354 a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
355 if (a && nla_len(a) == sizeof(u16)) {
356 dst_port = nla_get_u16(a);
358 /* Require destination port from userspace. */
363 /* Verify if we already have a socket created for this port */
364 lisp_port = lisp_find_port(net, htons(dst_port));
370 /* Add a new socket for this port */
371 lisp_port = kzalloc(sizeof(struct lisp_port), GFP_KERNEL);
377 tnl_vport->dst_port = htons(dst_port);
378 lisp_port->vport = vport;
379 list_add_tail_rcu(&lisp_port->list, &lisp_ports);
381 err = lisp_socket_init(lisp_port, net);
388 list_del_rcu(&lisp_port->list);
394 static int lisp_get_options(const struct vport *vport, struct sk_buff *skb)
396 const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
398 if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(tnl_vport->dst_port)))
403 static const struct tnl_ops ovs_lisp_tnl_ops = {
404 .ipproto = IPPROTO_UDP,
405 .hdr_len = lisp_hdr_len,
406 .build_header = lisp_build_header,
409 static void lisp_tnl_destroy(struct vport *vport)
411 struct lisp_port *lisp_port;
412 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
414 lisp_port = lisp_find_port(ovs_dp_get_net(vport->dp),
415 tnl_vport->dst_port);
417 lisp_tunnel_release(lisp_port);
418 ovs_tnl_destroy(vport);
421 static struct vport *lisp_tnl_create(const struct vport_parms *parms)
426 vport = ovs_tnl_create(parms, &ovs_lisp_vport_ops, &ovs_lisp_tnl_ops);
430 err = lisp_tunnel_setup(ovs_dp_get_net(parms->dp), vport,
433 ovs_tnl_destroy(vport);
440 const struct vport_ops ovs_lisp_vport_ops = {
441 .type = OVS_VPORT_TYPE_LISP,
442 .flags = VPORT_F_TUN_ID,
443 .create = lisp_tnl_create,
444 .destroy = lisp_tnl_destroy,
445 .get_name = ovs_tnl_get_name,
446 .get_options = lisp_get_options,
447 .send = lisp_tnl_send,
450 #warning LISP tunneling will not be available on kernels before 2.6.26
451 #endif /* Linux kernel < 2.6.26 */