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>
40 * LISP encapsulation header:
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * |N|L|E|V|I|flags| Nonce/Map-Version |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | Instance ID/Locator Status Bits |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * struct lisphdr - LISP header
52 * @nonce_present: Flag indicating the presence of a 24 bit nonce value.
53 * @locator_status_bits_present: Flag indicating the presence of Locator Status
55 * @solicit_echo_nonce: Flag indicating the use of the echo noncing mechanism.
56 * @map_version_present: Flag indicating the use of mapping versioning.
57 * @instance_id_present: Flag indicating the presence of a 24 bit Instance ID.
58 * @reserved_flags: 3 bits reserved for future flags.
59 * @nonce: 24 bit nonce value.
60 * @map_version: 24 bit mapping version.
61 * @locator_status_bits: Locator Status Bits: 32 bits when instance_id_present
62 * is not set, 8 bits when it is.
63 * @instance_id: 24 bit Instance ID
66 #ifdef __LITTLE_ENDIAN_BITFIELD
67 __u8 reserved_flags
:3;
68 __u8 instance_id_present
:1;
69 __u8 map_version_present
:1;
70 __u8 solicit_echo_nonce
:1;
71 __u8 locator_status_bits_present
:1;
75 __u8 locator_status_bits_present
:1;
76 __u8 solicit_echo_nonce
:1;
77 __u8 map_version_present
:1;
78 __u8 instance_id_present
:1;
79 __u8 reserved_flags
:3;
86 __be32 locator_status_bits
;
89 __u8 locator_status_bits
;
94 #define LISP_HLEN (sizeof(struct udphdr) + sizeof(struct lisphdr))
97 * struct lisp_port - Keeps track of open UDP ports
98 * @dst_port: lisp UDP port no.
99 * @list: list element in @lisp_ports.
100 * @lisp_rcv_socket: The socket created for this port number.
105 struct list_head list
;
106 struct socket
*lisp_rcv_socket
;
110 static LIST_HEAD(lisp_ports
);
112 static inline struct lisp_port
*lisp_vport(const struct vport
*vport
)
114 return vport_priv(vport
);
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 if (lisp_port
->dst_port
== port
&&
123 net_eq(sock_net(lisp_port
->lisp_rcv_socket
->sk
), net
))
130 static inline struct lisphdr
*lisp_hdr(const struct sk_buff
*skb
)
132 return (struct lisphdr
*)(udp_hdr(skb
) + 1);
135 /* Convert 64 bit tunnel ID to 24 bit Instance ID. */
136 static void tunnel_id_to_instance_id(__be64 tun_id
, __u8
*iid
)
140 iid
[0] = (__force __u8
)(tun_id
>> 16);
141 iid
[1] = (__force __u8
)(tun_id
>> 8);
142 iid
[2] = (__force __u8
)tun_id
;
144 iid
[0] = (__force __u8
)((__force u64
)tun_id
>> 40);
145 iid
[1] = (__force __u8
)((__force u64
)tun_id
>> 48);
146 iid
[2] = (__force __u8
)((__force u64
)tun_id
>> 56);
150 /* Convert 24 bit Instance ID to 64 bit tunnel ID. */
151 static __be64
instance_id_to_tunnel_id(__u8
*iid
)
154 return (iid
[0] << 16) | (iid
[1] << 8) | iid
[2];
156 return (__force __be64
)(((__force u64
)iid
[0] << 40) |
157 ((__force u64
)iid
[1] << 48) |
158 ((__force u64
)iid
[2] << 56));
162 /* Compute source UDP port for outgoing packet.
163 * Currently we use the flow hash.
165 static u16
ovs_tnl_get_src_port(struct sk_buff
*skb
)
170 struct sw_flow_key
*pkt_key
= OVS_CB(skb
)->pkt_key
;
171 u32 hash
= jhash2((const u32
*)pkt_key
,
172 sizeof(*pkt_key
) / sizeof(u32
), 0);
174 inet_get_local_port_range(&low
, &high
);
175 range
= (high
- low
) + 1;
176 return (((u64
) hash
* range
) >> 32) + low
;
179 static void lisp_build_header(const struct vport
*vport
,
183 struct lisp_port
*lisp_port
= lisp_vport(vport
);
184 struct udphdr
*udph
= udp_hdr(skb
);
185 struct lisphdr
*lisph
= (struct lisphdr
*)(udph
+ 1);
186 const struct ovs_key_ipv4_tunnel
*tun_key
= OVS_CB(skb
)->tun_key
;
188 udph
->dest
= lisp_port
->dst_port
;
189 udph
->source
= htons(ovs_tnl_get_src_port(skb
));
191 udph
->len
= htons(skb
->len
- skb_transport_offset(skb
));
193 lisph
->nonce_present
= 0; /* We don't support echo nonce algorithm */
194 lisph
->locator_status_bits_present
= 1; /* Set LSB */
195 lisph
->solicit_echo_nonce
= 0; /* No echo noncing */
196 lisph
->map_version_present
= 0; /* No mapping versioning, nonce instead */
197 lisph
->instance_id_present
= 1; /* Store the tun_id as Instance ID */
198 lisph
->reserved_flags
= 0; /* Reserved flags, set to 0 */
200 lisph
->u1
.nonce
[0] = 0;
201 lisph
->u1
.nonce
[1] = 0;
202 lisph
->u1
.nonce
[2] = 0;
204 tunnel_id_to_instance_id(tun_key
->tun_id
, &lisph
->u2
.word2
.instance_id
[0]);
205 lisph
->u2
.word2
.locator_status_bits
= 1;
208 /* Called with rcu_read_lock and BH disabled. */
209 static int lisp_rcv(struct sock
*sk
, struct sk_buff
*skb
)
211 struct lisp_port
*lisp_port
;
212 struct lisphdr
*lisph
;
213 struct iphdr
*iph
, *inner_iph
;
214 struct ovs_key_ipv4_tunnel tun_key
;
219 lisp_port
= lisp_find_port(dev_net(skb
->dev
), udp_hdr(skb
)->dest
);
220 if (unlikely(!lisp_port
))
223 if (iptunnel_pull_header(skb
, LISP_HLEN
, 0))
226 lisph
= lisp_hdr(skb
);
228 if (lisph
->instance_id_present
!= 1)
231 key
= instance_id_to_tunnel_id(&lisph
->u2
.word2
.instance_id
[0]);
233 /* Save outer tunnel values */
235 ovs_flow_tun_key_init(&tun_key
, iph
, key
, TUNNEL_KEY
);
237 /* Drop non-IP inner packets */
238 inner_iph
= (struct iphdr
*)(lisph
+ 1);
239 switch (inner_iph
->version
) {
241 protocol
= htons(ETH_P_IP
);
244 protocol
= htons(ETH_P_IPV6
);
249 skb
->protocol
= protocol
;
251 /* Add Ethernet header */
252 ethh
= (struct ethhdr
*)skb_push(skb
, ETH_HLEN
);
253 memset(ethh
, 0, ETH_HLEN
);
254 ethh
->h_dest
[0] = 0x02;
255 ethh
->h_source
[0] = 0x02;
256 ethh
->h_proto
= protocol
;
258 ovs_skb_postpush_rcsum(skb
, skb
->data
, ETH_HLEN
);
260 ovs_vport_receive(vport_from_priv(lisp_port
), skb
, &tun_key
);
269 /* Arbitrary value. Irrelevant as long as it's not 0 since we set the handler. */
270 #define UDP_ENCAP_LISP 1
271 static int lisp_socket_init(struct lisp_port
*lisp_port
, struct net
*net
)
273 struct sockaddr_in sin
;
276 err
= sock_create_kern(AF_INET
, SOCK_DGRAM
, 0,
277 &lisp_port
->lisp_rcv_socket
);
281 /* release net ref. */
282 sk_change_net(lisp_port
->lisp_rcv_socket
->sk
, net
);
284 sin
.sin_family
= AF_INET
;
285 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
286 sin
.sin_port
= lisp_port
->dst_port
;
288 err
= kernel_bind(lisp_port
->lisp_rcv_socket
, (struct sockaddr
*)&sin
,
289 sizeof(struct sockaddr_in
));
293 udp_sk(lisp_port
->lisp_rcv_socket
->sk
)->encap_type
= UDP_ENCAP_LISP
;
294 udp_sk(lisp_port
->lisp_rcv_socket
->sk
)->encap_rcv
= lisp_rcv
;
301 sk_release_kernel(lisp_port
->lisp_rcv_socket
->sk
);
303 pr_warn("cannot register lisp protocol handler: %d\n", err
);
307 static int lisp_get_options(const struct vport
*vport
, struct sk_buff
*skb
)
309 struct lisp_port
*lisp_port
= lisp_vport(vport
);
311 if (nla_put_u16(skb
, OVS_TUNNEL_ATTR_DST_PORT
, ntohs(lisp_port
->dst_port
)))
316 static void lisp_tnl_destroy(struct vport
*vport
)
318 struct lisp_port
*lisp_port
= lisp_vport(vport
);
320 list_del_rcu(&lisp_port
->list
);
322 sk_release_kernel(lisp_port
->lisp_rcv_socket
->sk
);
324 ovs_vport_deferred_free(vport
);
327 static struct vport
*lisp_tnl_create(const struct vport_parms
*parms
)
329 struct net
*net
= ovs_dp_get_net(parms
->dp
);
330 struct nlattr
*options
= parms
->options
;
331 struct lisp_port
*lisp_port
;
342 a
= nla_find_nested(options
, OVS_TUNNEL_ATTR_DST_PORT
);
343 if (a
&& nla_len(a
) == sizeof(u16
)) {
344 dst_port
= nla_get_u16(a
);
346 /* Require destination port from userspace. */
351 /* Verify if we already have a socket created for this port */
352 if (lisp_find_port(net
, htons(dst_port
))) {
357 vport
= ovs_vport_alloc(sizeof(struct lisp_port
),
358 &ovs_lisp_vport_ops
, parms
);
362 lisp_port
= lisp_vport(vport
);
363 lisp_port
->dst_port
= htons(dst_port
);
364 strncpy(lisp_port
->name
, parms
->name
, IFNAMSIZ
);
366 err
= lisp_socket_init(lisp_port
, net
);
370 list_add_tail_rcu(&lisp_port
->list
, &lisp_ports
);
374 ovs_vport_free(vport
);
379 static bool need_linearize(const struct sk_buff
*skb
)
383 if (unlikely(skb_shinfo(skb
)->frag_list
))
387 * Generally speaking we should linearize if there are paged frags.
388 * However, if all of the refcounts are 1 we know nobody else can
389 * change them from underneath us and we can skip the linearization.
391 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
392 if (unlikely(page_count(skb_frag_page(&skb_shinfo(skb
)->frags
[i
])) > 1))
398 static struct sk_buff
*handle_offloads(struct sk_buff
*skb
)
402 if (skb_is_gso(skb
)) {
403 struct sk_buff
*nskb
;
404 char cb
[sizeof(skb
->cb
)];
406 memcpy(cb
, skb
->cb
, sizeof(cb
));
408 nskb
= __skb_gso_segment(skb
, 0, false);
417 memcpy(nskb
->cb
, cb
, sizeof(cb
));
420 } else if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
421 /* Pages aren't locked and could change at any time.
422 * If this happens after we compute the checksum, the
423 * checksum will be wrong. We linearize now to avoid
426 if (unlikely(need_linearize(skb
))) {
427 err
= __skb_linearize(skb
);
432 err
= skb_checksum_help(skb
);
437 skb
->ip_summed
= CHECKSUM_NONE
;
444 static int ovs_tnl_send(struct vport
*vport
, struct sk_buff
*skb
,
445 u8 ipproto
, int tunnel_hlen
,
446 void (*build_header
)(const struct vport
*,
455 struct sk_buff
*nskb
;
458 saddr
= OVS_CB(skb
)->tun_key
->ipv4_src
;
459 rt
= find_route(ovs_dp_get_net(vport
->dp
),
461 OVS_CB(skb
)->tun_key
->ipv4_dst
,
463 OVS_CB(skb
)->tun_key
->ipv4_tos
,
470 tunnel_hlen
+= sizeof(struct iphdr
);
472 min_headroom
= LL_RESERVED_SPACE(rt_dst(rt
).dev
) + rt_dst(rt
).header_len
474 + (vlan_tx_tag_present(skb
) ? VLAN_HLEN
: 0);
476 if (skb_headroom(skb
) < min_headroom
|| skb_header_cloned(skb
)) {
477 int head_delta
= SKB_DATA_ALIGN(min_headroom
-
481 err
= pskb_expand_head(skb
, max_t(int, head_delta
, 0),
488 nskb
= handle_offloads(skb
);
499 skb_clear_rxhash(skb
);
502 struct sk_buff
*next_skb
= skb
->next
;
508 if (vlan_tx_tag_present(skb
)) {
509 if (unlikely(!__vlan_put_tag(skb
,
511 vlan_tx_tag_get(skb
))))
514 vlan_set_tci(skb
, 0);
518 skb_push(skb
, tunnel_hlen
);
519 skb_reset_network_header(skb
);
520 skb_set_transport_header(skb
, sizeof(struct iphdr
));
523 skb_dst_set(skb
, dst_clone(&rt_dst(rt
)));
525 skb_dst_set(skb
, &rt_dst(rt
));
527 /* Push Tunnel header. */
528 build_header(vport
, skb
, tunnel_hlen
);
530 /* Push IP header. */
533 iph
->ihl
= sizeof(struct iphdr
) >> 2;
534 iph
->protocol
= ipproto
;
535 iph
->daddr
= OVS_CB(skb
)->tun_key
->ipv4_dst
;
537 iph
->tos
= OVS_CB(skb
)->tun_key
->ipv4_tos
;
538 iph
->ttl
= OVS_CB(skb
)->tun_key
->ipv4_ttl
;
539 iph
->frag_off
= OVS_CB(skb
)->tun_key
->tun_flags
&
540 TUNNEL_DONT_FRAGMENT
? htons(IP_DF
) : 0;
542 * Allow our local IP stack to fragment the outer packet even
543 * if the DF bit is set as a last resort. We also need to
544 * force selection of an IP ID here with __ip_select_ident(),
545 * as ip_select_ident() assumes a proper ID is not needed when
546 * when the DF bit is set.
549 __ip_select_ident(iph
, skb_dst(skb
), 0);
551 memset(IPCB(skb
), 0, sizeof(*IPCB(skb
)));
553 err
= ip_local_out(skb
);
554 if (unlikely(net_xmit_eval(err
)))
557 sent_len
+= frag_len
;
571 static int lisp_tnl_send(struct vport
*vport
, struct sk_buff
*skb
)
574 int network_offset
= skb_network_offset(skb
);
576 if (unlikely(!OVS_CB(skb
)->tun_key
))
579 /* We only encapsulate IPv4 and IPv6 packets */
580 switch (skb
->protocol
) {
581 case htons(ETH_P_IP
):
582 case htons(ETH_P_IPV6
):
583 /* Pop off "inner" Ethernet header */
584 skb_pull(skb
, network_offset
);
585 tnl_len
= ovs_tnl_send(vport
, skb
, IPPROTO_UDP
,
586 LISP_HLEN
, lisp_build_header
);
587 return tnl_len
> 0 ? tnl_len
+ network_offset
: tnl_len
;
594 static const char *lisp_get_name(const struct vport
*vport
)
596 struct lisp_port
*lisp_port
= lisp_vport(vport
);
597 return lisp_port
->name
;
600 const struct vport_ops ovs_lisp_vport_ops
= {
601 .type
= OVS_VPORT_TYPE_LISP
,
602 .create
= lisp_tnl_create
,
603 .destroy
= lisp_tnl_destroy
,
604 .get_name
= lisp_get_name
,
605 .get_options
= lisp_get_options
,
606 .send
= lisp_tnl_send
,