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/rculist.h>
28 #include <linux/udp.h>
32 #include <net/route.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))
98 * struct lisp_port - Keeps track of open UDP ports
99 * @dst_port: lisp UDP port no.
100 * @list: list element in @lisp_ports.
101 * @lisp_rcv_socket: The socket created for this port number.
106 struct list_head list;
107 struct socket *lisp_rcv_socket;
111 static LIST_HEAD(lisp_ports);
113 static inline struct lisp_port *lisp_vport(const struct vport *vport)
115 return vport_priv(vport);
118 static struct lisp_port *lisp_find_port(struct net *net, __be16 port)
120 struct lisp_port *lisp_port;
122 list_for_each_entry_rcu(lisp_port, &lisp_ports, list) {
123 if (lisp_port->dst_port == port &&
124 net_eq(sock_net(lisp_port->lisp_rcv_socket->sk), net))
131 static inline struct lisphdr *lisp_hdr(const struct sk_buff *skb)
133 return (struct lisphdr *)(udp_hdr(skb) + 1);
136 /* Convert 64 bit tunnel ID to 24 bit Instance ID. */
137 static void tunnel_id_to_instance_id(__be64 tun_id, __u8 *iid)
141 iid[0] = (__force __u8)(tun_id >> 16);
142 iid[1] = (__force __u8)(tun_id >> 8);
143 iid[2] = (__force __u8)tun_id;
145 iid[0] = (__force __u8)((__force u64)tun_id >> 40);
146 iid[1] = (__force __u8)((__force u64)tun_id >> 48);
147 iid[2] = (__force __u8)((__force u64)tun_id >> 56);
151 /* Convert 24 bit Instance ID to 64 bit tunnel ID. */
152 static __be64 instance_id_to_tunnel_id(__u8 *iid)
155 return (iid[0] << 16) | (iid[1] << 8) | iid[2];
157 return (__force __be64)(((__force u64)iid[0] << 40) |
158 ((__force u64)iid[1] << 48) |
159 ((__force u64)iid[2] << 56));
163 /* Compute source UDP port for outgoing packet.
164 * Currently we use the flow hash.
166 static u16 get_src_port(struct net *net, struct sk_buff *skb)
168 u32 hash = skb_get_rxhash(skb);
174 struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
176 hash = jhash2((const u32 *)pkt_key,
177 sizeof(*pkt_key) / sizeof(u32), 0);
180 inet_get_local_port_range(net, &low, &high);
181 range = (high - low) + 1;
182 return (((u64) hash * range) >> 32) + low;
185 static void lisp_build_header(const struct vport *vport,
188 struct net *net = ovs_dp_get_net(vport->dp);
189 struct lisp_port *lisp_port = lisp_vport(vport);
190 struct udphdr *udph = udp_hdr(skb);
191 struct lisphdr *lisph = (struct lisphdr *)(udph + 1);
192 const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
194 udph->dest = lisp_port->dst_port;
195 udph->source = htons(get_src_port(net, skb));
197 udph->len = htons(skb->len - skb_transport_offset(skb));
199 lisph->nonce_present = 0; /* We don't support echo nonce algorithm */
200 lisph->locator_status_bits_present = 1; /* Set LSB */
201 lisph->solicit_echo_nonce = 0; /* No echo noncing */
202 lisph->map_version_present = 0; /* No mapping versioning, nonce instead */
203 lisph->instance_id_present = 1; /* Store the tun_id as Instance ID */
204 lisph->reserved_flags = 0; /* Reserved flags, set to 0 */
206 lisph->u1.nonce[0] = 0;
207 lisph->u1.nonce[1] = 0;
208 lisph->u1.nonce[2] = 0;
210 tunnel_id_to_instance_id(tun_key->tun_id, &lisph->u2.word2.instance_id[0]);
211 lisph->u2.word2.locator_status_bits = 1;
214 /* Called with rcu_read_lock and BH disabled. */
215 static int lisp_rcv(struct sock *sk, struct sk_buff *skb)
217 struct lisp_port *lisp_port;
218 struct lisphdr *lisph;
219 struct iphdr *iph, *inner_iph;
220 struct ovs_key_ipv4_tunnel tun_key;
225 lisp_port = lisp_find_port(dev_net(skb->dev), udp_hdr(skb)->dest);
226 if (unlikely(!lisp_port))
229 if (iptunnel_pull_header(skb, LISP_HLEN, 0))
232 lisph = lisp_hdr(skb);
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 ovs_flow_tun_key_init(&tun_key, iph, key, TUNNEL_KEY);
243 /* Drop non-IP inner packets */
244 inner_iph = (struct iphdr *)(lisph + 1);
245 switch (inner_iph->version) {
247 protocol = htons(ETH_P_IP);
250 protocol = htons(ETH_P_IPV6);
255 skb->protocol = protocol;
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_skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
266 ovs_vport_receive(vport_from_priv(lisp_port), skb, &tun_key);
275 /* Arbitrary value. Irrelevant as long as it's not 0 since we set the handler. */
276 #define UDP_ENCAP_LISP 1
277 static int lisp_socket_init(struct lisp_port *lisp_port, struct net *net)
279 struct sockaddr_in sin;
282 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0,
283 &lisp_port->lisp_rcv_socket);
287 /* release net ref. */
288 sk_change_net(lisp_port->lisp_rcv_socket->sk, net);
290 sin.sin_family = AF_INET;
291 sin.sin_addr.s_addr = htonl(INADDR_ANY);
292 sin.sin_port = lisp_port->dst_port;
294 err = kernel_bind(lisp_port->lisp_rcv_socket, (struct sockaddr *)&sin,
295 sizeof(struct sockaddr_in));
299 udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_type = UDP_ENCAP_LISP;
300 udp_sk(lisp_port->lisp_rcv_socket->sk)->encap_rcv = lisp_rcv;
307 sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
309 pr_warn("cannot register lisp protocol handler: %d\n", err);
313 static int lisp_get_options(const struct vport *vport, struct sk_buff *skb)
315 struct lisp_port *lisp_port = lisp_vport(vport);
317 if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(lisp_port->dst_port)))
322 static void lisp_tnl_destroy(struct vport *vport)
324 struct lisp_port *lisp_port = lisp_vport(vport);
326 list_del_rcu(&lisp_port->list);
328 sk_release_kernel(lisp_port->lisp_rcv_socket->sk);
330 ovs_vport_deferred_free(vport);
333 static struct vport *lisp_tnl_create(const struct vport_parms *parms)
335 struct net *net = ovs_dp_get_net(parms->dp);
336 struct nlattr *options = parms->options;
337 struct lisp_port *lisp_port;
348 a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
349 if (a && nla_len(a) == sizeof(u16)) {
350 dst_port = nla_get_u16(a);
352 /* Require destination port from userspace. */
357 /* Verify if we already have a socket created for this port */
358 if (lisp_find_port(net, htons(dst_port))) {
363 vport = ovs_vport_alloc(sizeof(struct lisp_port),
364 &ovs_lisp_vport_ops, parms);
368 lisp_port = lisp_vport(vport);
369 lisp_port->dst_port = htons(dst_port);
370 strncpy(lisp_port->name, parms->name, IFNAMSIZ);
372 err = lisp_socket_init(lisp_port, net);
376 list_add_tail_rcu(&lisp_port->list, &lisp_ports);
380 ovs_vport_free(vport);
385 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,12,0)
387 static void lisp_fix_segment(struct sk_buff *skb)
389 struct udphdr *udph = udp_hdr(skb);
391 udph->len = htons(skb->len - skb_transport_offset(skb));
394 static int handle_offloads(struct sk_buff *skb)
397 OVS_GSO_CB(skb)->fix_segment = lisp_fix_segment;
398 else if (skb->ip_summed != CHECKSUM_PARTIAL)
399 skb->ip_summed = CHECKSUM_NONE;
403 static int handle_offloads(struct sk_buff *skb)
405 if (skb_is_gso(skb)) {
406 int err = skb_unclone(skb, GFP_ATOMIC);
410 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
411 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
412 skb->ip_summed = CHECKSUM_NONE;
414 skb->encapsulation = 1;
419 static int lisp_send(struct vport *vport, struct sk_buff *skb)
421 int network_offset = skb_network_offset(skb);
429 if (unlikely(!OVS_CB(skb)->tun_key))
432 if (skb->protocol != htons(ETH_P_IP) &&
433 skb->protocol != htons(ETH_P_IPV6)) {
439 saddr = OVS_CB(skb)->tun_key->ipv4_src;
440 rt = find_route(ovs_dp_get_net(vport->dp),
442 OVS_CB(skb)->tun_key->ipv4_dst,
444 OVS_CB(skb)->tun_key->ipv4_tos,
451 min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
452 + sizeof(struct iphdr) + LISP_HLEN;
454 if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
455 int head_delta = SKB_DATA_ALIGN(min_headroom -
459 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
465 /* Reset l2 headers. */
466 skb_pull(skb, network_offset);
467 skb_reset_mac_header(skb);
468 vlan_set_tci(skb, 0);
470 skb_reset_inner_headers(skb);
472 __skb_push(skb, LISP_HLEN);
473 skb_reset_transport_header(skb);
475 lisp_build_header(vport, skb);
478 err = handle_offloads(skb);
484 df = OVS_CB(skb)->tun_key->tun_flags &
485 TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
486 sent_len = iptunnel_xmit(rt, skb,
487 saddr, OVS_CB(skb)->tun_key->ipv4_dst,
488 IPPROTO_UDP, OVS_CB(skb)->tun_key->ipv4_tos,
489 OVS_CB(skb)->tun_key->ipv4_ttl, df, false);
491 return sent_len > 0 ? sent_len + network_offset : sent_len;
499 static const char *lisp_get_name(const struct vport *vport)
501 struct lisp_port *lisp_port = lisp_vport(vport);
502 return lisp_port->name;
505 const struct vport_ops ovs_lisp_vport_ops = {
506 .type = OVS_VPORT_TYPE_LISP,
507 .create = lisp_tnl_create,
508 .destroy = lisp_tnl_destroy,
509 .get_name = lisp_get_name,
510 .get_options = lisp_get_options,