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>
33 #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
ovs_tnl_get_src_port(struct sk_buff
*skb
)
171 struct sw_flow_key
*pkt_key
= OVS_CB(skb
)->pkt_key
;
172 u32 hash
= jhash2((const u32
*)pkt_key
,
173 sizeof(*pkt_key
) / sizeof(u32
), 0);
175 inet_get_local_port_range(&low
, &high
);
176 range
= (high
- low
) + 1;
177 return (((u64
) hash
* range
) >> 32) + low
;
180 static void lisp_build_header(const struct vport
*vport
,
184 struct lisp_port
*lisp_port
= lisp_vport(vport
);
185 struct udphdr
*udph
= udp_hdr(skb
);
186 struct lisphdr
*lisph
= (struct lisphdr
*)(udph
+ 1);
187 const struct ovs_key_ipv4_tunnel
*tun_key
= OVS_CB(skb
)->tun_key
;
189 udph
->dest
= lisp_port
->dst_port
;
190 udph
->source
= htons(ovs_tnl_get_src_port(skb
));
192 udph
->len
= htons(skb
->len
- skb_transport_offset(skb
));
194 lisph
->nonce_present
= 0; /* We don't support echo nonce algorithm */
195 lisph
->locator_status_bits_present
= 1; /* Set LSB */
196 lisph
->solicit_echo_nonce
= 0; /* No echo noncing */
197 lisph
->map_version_present
= 0; /* No mapping versioning, nonce instead */
198 lisph
->instance_id_present
= 1; /* Store the tun_id as Instance ID */
199 lisph
->reserved_flags
= 0; /* Reserved flags, set to 0 */
201 lisph
->u1
.nonce
[0] = 0;
202 lisph
->u1
.nonce
[1] = 0;
203 lisph
->u1
.nonce
[2] = 0;
205 tunnel_id_to_instance_id(tun_key
->tun_id
, &lisph
->u2
.word2
.instance_id
[0]);
206 lisph
->u2
.word2
.locator_status_bits
= 1;
210 * ovs_tnl_rcv - ingress point for generic tunnel code
212 * @vport: port this packet was received on
213 * @skb: received packet
214 * @tun_key: tunnel that carried packet
216 * Must be called with rcu_read_lock.
218 * Packets received by this function are in the following state:
219 * - skb->data points to the inner Ethernet header.
220 * - The inner Ethernet header is in the linear data area.
221 * - The layer pointers are undefined.
223 static void ovs_tnl_rcv(struct vport
*vport
, struct sk_buff
*skb
,
224 struct ovs_key_ipv4_tunnel
*tun_key
)
228 skb_reset_mac_header(skb
);
231 if (likely(ntohs(eh
->h_proto
) >= ETH_P_802_3_MIN
))
232 skb
->protocol
= eh
->h_proto
;
234 skb
->protocol
= htons(ETH_P_802_2
);
238 skb_clear_rxhash(skb
);
240 vlan_set_tci(skb
, 0);
242 if (unlikely(compute_ip_summed(skb
, false))) {
247 ovs_vport_receive(vport
, skb
, tun_key
);
250 /* Called with rcu_read_lock and BH disabled. */
251 static int lisp_rcv(struct sock
*sk
, struct sk_buff
*skb
)
253 struct lisp_port
*lisp_port
;
254 struct lisphdr
*lisph
;
255 struct iphdr
*iph
, *inner_iph
;
256 struct ovs_key_ipv4_tunnel tun_key
;
261 lisp_port
= lisp_find_port(dev_net(skb
->dev
), udp_hdr(skb
)->dest
);
262 if (unlikely(!lisp_port
))
265 if (unlikely(!pskb_may_pull(skb
, LISP_HLEN
)))
268 lisph
= lisp_hdr(skb
);
270 skb_pull_rcsum(skb
, LISP_HLEN
);
272 if (lisph
->instance_id_present
!= 1)
275 key
= instance_id_to_tunnel_id(&lisph
->u2
.word2
.instance_id
[0]);
277 /* Save outer tunnel values */
279 ovs_flow_tun_key_init(&tun_key
, iph
, key
, TUNNEL_KEY
);
281 /* Drop non-IP inner packets */
282 inner_iph
= (struct iphdr
*)(lisph
+ 1);
283 switch (inner_iph
->version
) {
285 protocol
= htons(ETH_P_IP
);
288 protocol
= htons(ETH_P_IPV6
);
294 /* Add Ethernet header */
295 ethh
= (struct ethhdr
*)skb_push(skb
, ETH_HLEN
);
296 memset(ethh
, 0, ETH_HLEN
);
297 ethh
->h_dest
[0] = 0x02;
298 ethh
->h_source
[0] = 0x02;
299 ethh
->h_proto
= protocol
;
301 ovs_skb_postpush_rcsum(skb
, skb
->data
, ETH_HLEN
);
303 ovs_tnl_rcv(vport_from_priv(lisp_port
), skb
, &tun_key
);
312 /* Arbitrary value. Irrelevant as long as it's not 0 since we set the handler. */
313 #define UDP_ENCAP_LISP 1
314 static int lisp_socket_init(struct lisp_port
*lisp_port
, struct net
*net
)
316 struct sockaddr_in sin
;
319 err
= sock_create_kern(AF_INET
, SOCK_DGRAM
, 0,
320 &lisp_port
->lisp_rcv_socket
);
324 /* release net ref. */
325 sk_change_net(lisp_port
->lisp_rcv_socket
->sk
, net
);
327 sin
.sin_family
= AF_INET
;
328 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
329 sin
.sin_port
= lisp_port
->dst_port
;
331 err
= kernel_bind(lisp_port
->lisp_rcv_socket
, (struct sockaddr
*)&sin
,
332 sizeof(struct sockaddr_in
));
336 udp_sk(lisp_port
->lisp_rcv_socket
->sk
)->encap_type
= UDP_ENCAP_LISP
;
337 udp_sk(lisp_port
->lisp_rcv_socket
->sk
)->encap_rcv
= lisp_rcv
;
344 sk_release_kernel(lisp_port
->lisp_rcv_socket
->sk
);
346 pr_warn("cannot register lisp protocol handler: %d\n", err
);
350 static int lisp_get_options(const struct vport
*vport
, struct sk_buff
*skb
)
352 struct lisp_port
*lisp_port
= lisp_vport(vport
);
354 if (nla_put_u16(skb
, OVS_TUNNEL_ATTR_DST_PORT
, ntohs(lisp_port
->dst_port
)))
359 static void lisp_tnl_destroy(struct vport
*vport
)
361 struct lisp_port
*lisp_port
= lisp_vport(vport
);
363 list_del_rcu(&lisp_port
->list
);
365 sk_release_kernel(lisp_port
->lisp_rcv_socket
->sk
);
367 ovs_vport_deferred_free(vport
);
370 static struct vport
*lisp_tnl_create(const struct vport_parms
*parms
)
372 struct net
*net
= ovs_dp_get_net(parms
->dp
);
373 struct nlattr
*options
= parms
->options
;
374 struct lisp_port
*lisp_port
;
385 a
= nla_find_nested(options
, OVS_TUNNEL_ATTR_DST_PORT
);
386 if (a
&& nla_len(a
) == sizeof(u16
)) {
387 dst_port
= nla_get_u16(a
);
389 /* Require destination port from userspace. */
394 /* Verify if we already have a socket created for this port */
395 if (lisp_find_port(net
, htons(dst_port
))) {
400 vport
= ovs_vport_alloc(sizeof(struct lisp_port
),
401 &ovs_lisp_vport_ops
, parms
);
405 lisp_port
= lisp_vport(vport
);
406 lisp_port
->dst_port
= htons(dst_port
);
407 strncpy(lisp_port
->name
, parms
->name
, IFNAMSIZ
);
409 err
= lisp_socket_init(lisp_port
, net
);
413 list_add_tail_rcu(&lisp_port
->list
, &lisp_ports
);
417 ovs_vport_free(vport
);
422 static bool need_linearize(const struct sk_buff
*skb
)
426 if (unlikely(skb_shinfo(skb
)->frag_list
))
430 * Generally speaking we should linearize if there are paged frags.
431 * However, if all of the refcounts are 1 we know nobody else can
432 * change them from underneath us and we can skip the linearization.
434 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
435 if (unlikely(page_count(skb_frag_page(&skb_shinfo(skb
)->frags
[i
])) > 1))
441 static struct sk_buff
*handle_offloads(struct sk_buff
*skb
)
445 forward_ip_summed(skb
, true);
448 if (skb_is_gso(skb
)) {
449 struct sk_buff
*nskb
;
450 char cb
[sizeof(skb
->cb
)];
452 memcpy(cb
, skb
->cb
, sizeof(cb
));
454 nskb
= __skb_gso_segment(skb
, 0, false);
463 memcpy(nskb
->cb
, cb
, sizeof(cb
));
466 } else if (get_ip_summed(skb
) == OVS_CSUM_PARTIAL
) {
467 /* Pages aren't locked and could change at any time.
468 * If this happens after we compute the checksum, the
469 * checksum will be wrong. We linearize now to avoid
472 if (unlikely(need_linearize(skb
))) {
473 err
= __skb_linearize(skb
);
478 err
= skb_checksum_help(skb
);
483 set_ip_summed(skb
, OVS_CSUM_NONE
);
491 static int ovs_tnl_send(struct vport
*vport
, struct sk_buff
*skb
,
492 u8 ipproto
, int tunnel_hlen
,
493 void (*build_header
)(const struct vport
*,
502 struct sk_buff
*nskb
;
505 saddr
= OVS_CB(skb
)->tun_key
->ipv4_src
;
506 rt
= find_route(ovs_dp_get_net(vport
->dp
),
508 OVS_CB(skb
)->tun_key
->ipv4_dst
,
510 OVS_CB(skb
)->tun_key
->ipv4_tos
,
517 tunnel_hlen
+= sizeof(struct iphdr
);
519 min_headroom
= LL_RESERVED_SPACE(rt_dst(rt
).dev
) + rt_dst(rt
).header_len
521 + (vlan_tx_tag_present(skb
) ? VLAN_HLEN
: 0);
523 if (skb_headroom(skb
) < min_headroom
|| skb_header_cloned(skb
)) {
524 int head_delta
= SKB_DATA_ALIGN(min_headroom
-
528 err
= pskb_expand_head(skb
, max_t(int, head_delta
, 0),
535 nskb
= handle_offloads(skb
);
546 skb_clear_rxhash(skb
);
549 struct sk_buff
*next_skb
= skb
->next
;
555 if (unlikely(vlan_deaccel_tag(skb
)))
559 skb_push(skb
, tunnel_hlen
);
560 skb_reset_network_header(skb
);
561 skb_set_transport_header(skb
, sizeof(struct iphdr
));
564 skb_dst_set(skb
, dst_clone(&rt_dst(rt
)));
566 skb_dst_set(skb
, &rt_dst(rt
));
568 /* Push Tunnel header. */
569 build_header(vport
, skb
, tunnel_hlen
);
571 /* Push IP header. */
574 iph
->ihl
= sizeof(struct iphdr
) >> 2;
575 iph
->protocol
= ipproto
;
576 iph
->daddr
= OVS_CB(skb
)->tun_key
->ipv4_dst
;
578 iph
->tos
= OVS_CB(skb
)->tun_key
->ipv4_tos
;
579 iph
->ttl
= OVS_CB(skb
)->tun_key
->ipv4_ttl
;
580 iph
->frag_off
= OVS_CB(skb
)->tun_key
->tun_flags
&
581 TUNNEL_DONT_FRAGMENT
? htons(IP_DF
) : 0;
583 * Allow our local IP stack to fragment the outer packet even
584 * if the DF bit is set as a last resort. We also need to
585 * force selection of an IP ID here with __ip_select_ident(),
586 * as ip_select_ident() assumes a proper ID is not needed when
587 * when the DF bit is set.
590 __ip_select_ident(iph
, skb_dst(skb
), 0);
592 memset(IPCB(skb
), 0, sizeof(*IPCB(skb
)));
594 err
= ip_local_out(skb
);
595 if (unlikely(net_xmit_eval(err
)))
598 sent_len
+= frag_len
;
612 static int lisp_tnl_send(struct vport
*vport
, struct sk_buff
*skb
)
615 int network_offset
= skb_network_offset(skb
);
617 if (unlikely(!OVS_CB(skb
)->tun_key
))
620 /* We only encapsulate IPv4 and IPv6 packets */
621 switch (skb
->protocol
) {
622 case htons(ETH_P_IP
):
623 case htons(ETH_P_IPV6
):
624 /* Pop off "inner" Ethernet header */
625 skb_pull(skb
, network_offset
);
626 tnl_len
= ovs_tnl_send(vport
, skb
, IPPROTO_UDP
,
627 LISP_HLEN
, lisp_build_header
);
628 return tnl_len
> 0 ? tnl_len
+ network_offset
: tnl_len
;
635 static const char *lisp_get_name(const struct vport
*vport
)
637 struct lisp_port
*lisp_port
= lisp_vport(vport
);
638 return lisp_port
->name
;
641 const struct vport_ops ovs_lisp_vport_ops
= {
642 .type
= OVS_VPORT_TYPE_LISP
,
643 .create
= lisp_tnl_create
,
644 .destroy
= lisp_tnl_destroy
,
645 .get_name
= lisp_get_name
,
646 .get_options
= lisp_get_options
,
647 .send
= lisp_tnl_send
,
650 #warning LISP tunneling will not be available on kernels before 2.6.26
651 #endif /* Linux kernel < 2.6.26 */