]>
Commit | Line | Data |
---|---|---|
6b241d64 PS |
1 | /* |
2 | * Copyright (c) 2016 Nicira, Inc. | |
68da36fe | 3 | * Copyright (c) 2016 Red Hat, Inc. |
6b241d64 PS |
4 | * |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at: | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | #include <config.h> | |
19 | ||
aca40d4f TLSC |
20 | #include "netdev-native-tnl.h" |
21 | ||
6b241d64 PS |
22 | #include <errno.h> |
23 | #include <fcntl.h> | |
24 | #include <sys/socket.h> | |
25 | #include <net/if.h> | |
b2befd5b | 26 | #include <sys/types.h> |
67eaddc0 | 27 | #include <netinet/in.h> |
aca40d4f | 28 | #include <netinet/ip.h> |
6b241d64 PS |
29 | #include <netinet/ip6.h> |
30 | #include <sys/ioctl.h> | |
31 | ||
32 | #include <errno.h> | |
33 | #include <stdlib.h> | |
34 | #include <sys/time.h> | |
35 | ||
6b241d64 PS |
36 | #include "byte-order.h" |
37 | #include "csum.h" | |
6b241d64 | 38 | #include "dp-packet.h" |
aca40d4f | 39 | #include "netdev.h" |
6b241d64 PS |
40 | #include "netdev-vport.h" |
41 | #include "netdev-vport-private.h" | |
42 | #include "odp-netlink.h" | |
6b241d64 | 43 | #include "packets.h" |
aca40d4f | 44 | #include "seq.h" |
6b241d64 PS |
45 | #include "unaligned.h" |
46 | #include "unixctl.h" | |
aca40d4f | 47 | #include "openvswitch/vlog.h" |
6b241d64 PS |
48 | |
49 | VLOG_DEFINE_THIS_MODULE(native_tnl); | |
50 | static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(60, 5); | |
51 | ||
52 | #define VXLAN_HLEN (sizeof(struct udp_header) + \ | |
53 | sizeof(struct vxlanhdr)) | |
54 | ||
55 | #define GENEVE_BASE_HLEN (sizeof(struct udp_header) + \ | |
56 | sizeof(struct genevehdr)) | |
57 | ||
58 | uint16_t tnl_udp_port_min = 32768; | |
59 | uint16_t tnl_udp_port_max = 61000; | |
60 | ||
61 | void * | |
62 | netdev_tnl_ip_extract_tnl_md(struct dp_packet *packet, struct flow_tnl *tnl, | |
63 | unsigned int *hlen) | |
64 | { | |
65 | void *nh; | |
66 | struct ip_header *ip; | |
67 | struct ovs_16aligned_ip6_hdr *ip6; | |
68 | void *l4; | |
69 | int l3_size; | |
70 | ||
71 | nh = dp_packet_l3(packet); | |
72 | ip = nh; | |
73 | ip6 = nh; | |
74 | l4 = dp_packet_l4(packet); | |
75 | ||
76 | if (!nh || !l4) { | |
77 | return NULL; | |
78 | } | |
79 | ||
80 | *hlen = sizeof(struct eth_header); | |
81 | ||
82 | l3_size = dp_packet_size(packet) - | |
83 | ((char *)nh - (char *)dp_packet_data(packet)); | |
84 | ||
85 | if (IP_VER(ip->ip_ihl_ver) == 4) { | |
86 | ||
87 | ovs_be32 ip_src, ip_dst; | |
88 | ||
1a2bb118 SC |
89 | if (OVS_UNLIKELY(!dp_packet_ip_checksum_valid(packet))) { |
90 | if (csum(ip, IP_IHL(ip->ip_ihl_ver) * 4)) { | |
91 | VLOG_WARN_RL(&err_rl, "ip packet has invalid checksum"); | |
92 | return NULL; | |
93 | } | |
6b241d64 PS |
94 | } |
95 | ||
96 | if (ntohs(ip->ip_tot_len) > l3_size) { | |
97 | VLOG_WARN_RL(&err_rl, "ip packet is truncated (IP length %d, actual %d)", | |
98 | ntohs(ip->ip_tot_len), l3_size); | |
99 | return NULL; | |
100 | } | |
101 | if (IP_IHL(ip->ip_ihl_ver) * 4 > sizeof(struct ip_header)) { | |
102 | VLOG_WARN_RL(&err_rl, "ip options not supported on tunnel packets " | |
103 | "(%d bytes)", IP_IHL(ip->ip_ihl_ver) * 4); | |
104 | return NULL; | |
105 | } | |
106 | ||
107 | ip_src = get_16aligned_be32(&ip->ip_src); | |
108 | ip_dst = get_16aligned_be32(&ip->ip_dst); | |
109 | ||
110 | tnl->ip_src = ip_src; | |
111 | tnl->ip_dst = ip_dst; | |
112 | tnl->ip_tos = ip->ip_tos; | |
113 | tnl->ip_ttl = ip->ip_ttl; | |
114 | ||
115 | *hlen += IP_HEADER_LEN; | |
116 | ||
117 | } else if (IP_VER(ip->ip_ihl_ver) == 6) { | |
98c086db | 118 | ovs_be32 tc_flow = get_16aligned_be32(&ip6->ip6_flow); |
6b241d64 PS |
119 | |
120 | memcpy(tnl->ipv6_src.s6_addr, ip6->ip6_src.be16, sizeof ip6->ip6_src); | |
121 | memcpy(tnl->ipv6_dst.s6_addr, ip6->ip6_dst.be16, sizeof ip6->ip6_dst); | |
98c086db PS |
122 | |
123 | tnl->ip_tos = ntohl(tc_flow) >> 20; | |
6b241d64 PS |
124 | tnl->ip_ttl = ip6->ip6_hlim; |
125 | ||
3456684e | 126 | *hlen += packet->l4_ofs - packet->l3_ofs; |
6b241d64 PS |
127 | |
128 | } else { | |
129 | VLOG_WARN_RL(&err_rl, "ipv4 packet has invalid version (%d)", | |
130 | IP_VER(ip->ip_ihl_ver)); | |
131 | return NULL; | |
132 | } | |
133 | ||
134 | return l4; | |
135 | } | |
136 | ||
137 | /* Pushes the 'size' bytes of 'header' into the headroom of 'packet', | |
138 | * reallocating the packet if necessary. 'header' should contain an Ethernet | |
139 | * header, followed by an IPv4 header (without options), and an L4 header. | |
140 | * | |
141 | * This function sets the IP header's ip_tot_len field (which should be zeroed | |
142 | * as part of 'header') and puts its value into '*ip_tot_size' as well. Also | |
7c12dfc5 | 143 | * updates IP header checksum, as well as the l3 and l4 offsets in 'packet'. |
6b241d64 PS |
144 | * |
145 | * Return pointer to the L4 header added to 'packet'. */ | |
146 | void * | |
147 | netdev_tnl_push_ip_header(struct dp_packet *packet, | |
148 | const void *header, int size, int *ip_tot_size) | |
149 | { | |
150 | struct eth_header *eth; | |
151 | struct ip_header *ip; | |
152 | struct ovs_16aligned_ip6_hdr *ip6; | |
153 | ||
154 | eth = dp_packet_push_uninit(packet, size); | |
155 | *ip_tot_size = dp_packet_size(packet) - sizeof (struct eth_header); | |
156 | ||
157 | memcpy(eth, header, size); | |
63171f04 JS |
158 | /* The encapsulated packet has type Ethernet. Adjust dp_packet. */ |
159 | packet->packet_type = htonl(PT_ETH); | |
160 | dp_packet_reset_offsets(packet); | |
161 | packet->l3_ofs = sizeof (struct eth_header); | |
6b241d64 PS |
162 | |
163 | if (netdev_tnl_is_header_ipv6(header)) { | |
164 | ip6 = netdev_tnl_ipv6_hdr(eth); | |
165 | *ip_tot_size -= IPV6_HEADER_LEN; | |
166 | ip6->ip6_plen = htons(*ip_tot_size); | |
ce8bbd37 | 167 | packet->l4_ofs = dp_packet_size(packet) - *ip_tot_size; |
6b241d64 PS |
168 | return ip6 + 1; |
169 | } else { | |
170 | ip = netdev_tnl_ip_hdr(eth); | |
171 | ip->ip_tot_len = htons(*ip_tot_size); | |
172 | ip->ip_csum = recalc_csum16(ip->ip_csum, 0, ip->ip_tot_len); | |
173 | *ip_tot_size -= IP_HEADER_LEN; | |
ce8bbd37 | 174 | packet->l4_ofs = dp_packet_size(packet) - *ip_tot_size; |
6b241d64 PS |
175 | return ip + 1; |
176 | } | |
177 | } | |
178 | ||
179 | static void * | |
180 | udp_extract_tnl_md(struct dp_packet *packet, struct flow_tnl *tnl, | |
181 | unsigned int *hlen) | |
182 | { | |
183 | struct udp_header *udp; | |
184 | ||
185 | udp = netdev_tnl_ip_extract_tnl_md(packet, tnl, hlen); | |
186 | if (!udp) { | |
187 | return NULL; | |
188 | } | |
189 | ||
190 | if (udp->udp_csum) { | |
1a2bb118 SC |
191 | if (OVS_UNLIKELY(!dp_packet_l4_checksum_valid(packet))) { |
192 | uint32_t csum; | |
193 | if (netdev_tnl_is_header_ipv6(dp_packet_data(packet))) { | |
194 | csum = packet_csum_pseudoheader6(dp_packet_l3(packet)); | |
195 | } else { | |
196 | csum = packet_csum_pseudoheader(dp_packet_l3(packet)); | |
197 | } | |
198 | ||
199 | csum = csum_continue(csum, udp, dp_packet_size(packet) - | |
200 | ((const unsigned char *)udp - | |
2482b0b0 | 201 | (const unsigned char *)dp_packet_eth(packet) |
1a2bb118 SC |
202 | )); |
203 | if (csum_finish(csum)) { | |
204 | return NULL; | |
205 | } | |
6b241d64 PS |
206 | } |
207 | tnl->flags |= FLOW_TNL_F_CSUM; | |
208 | } | |
209 | ||
210 | tnl->tp_src = udp->udp_src; | |
211 | tnl->tp_dst = udp->udp_dst; | |
212 | ||
213 | return udp + 1; | |
214 | } | |
215 | ||
216 | ||
217 | void | |
754f8acb WT |
218 | netdev_tnl_push_udp_header(const struct netdev *netdev OVS_UNUSED, |
219 | struct dp_packet *packet, | |
6b241d64 PS |
220 | const struct ovs_action_push_tnl *data) |
221 | { | |
222 | struct udp_header *udp; | |
223 | int ip_tot_size; | |
224 | ||
225 | udp = netdev_tnl_push_ip_header(packet, data->header, data->header_len, &ip_tot_size); | |
226 | ||
227 | /* set udp src port */ | |
228 | udp->udp_src = netdev_tnl_get_src_port(packet); | |
229 | udp->udp_len = htons(ip_tot_size); | |
230 | ||
231 | if (udp->udp_csum) { | |
232 | uint32_t csum; | |
233 | if (netdev_tnl_is_header_ipv6(dp_packet_data(packet))) { | |
234 | csum = packet_csum_pseudoheader6(netdev_tnl_ipv6_hdr(dp_packet_data(packet))); | |
235 | } else { | |
236 | csum = packet_csum_pseudoheader(netdev_tnl_ip_hdr(dp_packet_data(packet))); | |
237 | } | |
238 | ||
239 | csum = csum_continue(csum, udp, ip_tot_size); | |
240 | udp->udp_csum = csum_finish(csum); | |
241 | ||
242 | if (!udp->udp_csum) { | |
243 | udp->udp_csum = htons(0xffff); | |
244 | } | |
245 | } | |
246 | } | |
247 | ||
248 | static void * | |
4975aa3e PS |
249 | eth_build_header(struct ovs_action_push_tnl *data, |
250 | const struct netdev_tnl_build_header_params *params) | |
6b241d64 | 251 | { |
4975aa3e PS |
252 | uint16_t eth_proto = params->is_ipv6 ? ETH_TYPE_IPV6 : ETH_TYPE_IP; |
253 | struct eth_header *eth; | |
6b241d64 | 254 | |
4975aa3e | 255 | memset(data->header, 0, sizeof data->header); |
6b241d64 | 256 | |
4975aa3e PS |
257 | eth = (struct eth_header *)data->header; |
258 | eth->eth_dst = params->dmac; | |
259 | eth->eth_src = params->smac; | |
260 | eth->eth_type = htons(eth_proto); | |
261 | data->header_len = sizeof(struct eth_header); | |
262 | return eth + 1; | |
263 | } | |
6b241d64 | 264 | |
4975aa3e PS |
265 | void * |
266 | netdev_tnl_ip_build_header(struct ovs_action_push_tnl *data, | |
267 | const struct netdev_tnl_build_header_params *params, | |
268 | uint8_t next_proto) | |
269 | { | |
270 | void *l3; | |
271 | ||
272 | l3 = eth_build_header(data, params); | |
273 | if (!params->is_ipv6) { | |
274 | ovs_be32 ip_src = in6_addr_get_mapped_ipv4(params->s_ip); | |
275 | struct ip_header *ip; | |
276 | ||
277 | ip = (struct ip_header *) l3; | |
278 | ||
279 | ip->ip_ihl_ver = IP_IHL_VER(5, 4); | |
280 | ip->ip_tos = params->flow->tunnel.ip_tos; | |
281 | ip->ip_ttl = params->flow->tunnel.ip_ttl; | |
282 | ip->ip_proto = next_proto; | |
283 | put_16aligned_be32(&ip->ip_src, ip_src); | |
284 | put_16aligned_be32(&ip->ip_dst, params->flow->tunnel.ip_dst); | |
285 | ||
286 | ip->ip_frag_off = (params->flow->tunnel.flags & FLOW_TNL_F_DONT_FRAGMENT) ? | |
287 | htons(IP_DF) : 0; | |
288 | ||
ece9c294 | 289 | /* Checksum has already been zeroed by eth_build_header. */ |
4975aa3e PS |
290 | ip->ip_csum = csum(ip, sizeof *ip); |
291 | ||
292 | data->header_len += IP_HEADER_LEN; | |
293 | return ip + 1; | |
6b241d64 | 294 | } else { |
4975aa3e PS |
295 | struct ovs_16aligned_ip6_hdr *ip6; |
296 | ||
297 | ip6 = (struct ovs_16aligned_ip6_hdr *) l3; | |
298 | ||
98c086db PS |
299 | put_16aligned_be32(&ip6->ip6_flow, htonl(6 << 28) | |
300 | htonl(params->flow->tunnel.ip_tos << 20)); | |
4975aa3e PS |
301 | ip6->ip6_hlim = params->flow->tunnel.ip_ttl; |
302 | ip6->ip6_nxt = next_proto; | |
303 | memcpy(&ip6->ip6_src, params->s_ip, sizeof(ovs_be32[4])); | |
304 | memcpy(&ip6->ip6_dst, ¶ms->flow->tunnel.ipv6_dst, sizeof(ovs_be32[4])); | |
305 | ||
306 | data->header_len += IPV6_HEADER_LEN; | |
307 | return ip6 + 1; | |
6b241d64 | 308 | } |
4975aa3e PS |
309 | } |
310 | ||
311 | static void * | |
312 | udp_build_header(struct netdev_tunnel_config *tnl_cfg, | |
313 | struct ovs_action_push_tnl *data, | |
314 | const struct netdev_tnl_build_header_params *params) | |
315 | { | |
316 | struct udp_header *udp; | |
6b241d64 | 317 | |
4975aa3e | 318 | udp = netdev_tnl_ip_build_header(data, params, IPPROTO_UDP); |
6b241d64 PS |
319 | udp->udp_dst = tnl_cfg->dst_port; |
320 | ||
4975aa3e | 321 | if (params->is_ipv6 || params->flow->tunnel.flags & FLOW_TNL_F_CSUM) { |
6b241d64 PS |
322 | /* Write a value in now to mark that we should compute the checksum |
323 | * later. 0xffff is handy because it is transparent to the | |
324 | * calculation. */ | |
325 | udp->udp_csum = htons(0xffff); | |
326 | } | |
4975aa3e | 327 | data->header_len += sizeof *udp; |
6b241d64 PS |
328 | return udp + 1; |
329 | } | |
330 | ||
331 | static int | |
332 | gre_header_len(ovs_be16 flags) | |
333 | { | |
334 | int hlen = 4; | |
335 | ||
336 | if (flags & htons(GRE_CSUM)) { | |
337 | hlen += 4; | |
338 | } | |
339 | if (flags & htons(GRE_KEY)) { | |
340 | hlen += 4; | |
341 | } | |
342 | if (flags & htons(GRE_SEQ)) { | |
343 | hlen += 4; | |
344 | } | |
345 | return hlen; | |
346 | } | |
347 | ||
348 | static int | |
349 | parse_gre_header(struct dp_packet *packet, | |
350 | struct flow_tnl *tnl) | |
351 | { | |
352 | const struct gre_base_hdr *greh; | |
353 | ovs_16aligned_be32 *options; | |
354 | int hlen; | |
355 | unsigned int ulen; | |
63171f04 | 356 | uint16_t greh_protocol; |
6b241d64 PS |
357 | |
358 | greh = netdev_tnl_ip_extract_tnl_md(packet, tnl, &ulen); | |
359 | if (!greh) { | |
360 | return -EINVAL; | |
361 | } | |
362 | ||
363 | if (greh->flags & ~(htons(GRE_CSUM | GRE_KEY | GRE_SEQ))) { | |
364 | return -EINVAL; | |
365 | } | |
366 | ||
6b241d64 PS |
367 | hlen = ulen + gre_header_len(greh->flags); |
368 | if (hlen > dp_packet_size(packet)) { | |
369 | return -EINVAL; | |
370 | } | |
371 | ||
372 | options = (ovs_16aligned_be32 *)(greh + 1); | |
373 | if (greh->flags & htons(GRE_CSUM)) { | |
374 | ovs_be16 pkt_csum; | |
375 | ||
376 | pkt_csum = csum(greh, dp_packet_size(packet) - | |
377 | ((const unsigned char *)greh - | |
2482b0b0 | 378 | (const unsigned char *)dp_packet_eth(packet))); |
6b241d64 PS |
379 | if (pkt_csum) { |
380 | return -EINVAL; | |
381 | } | |
382 | tnl->flags = FLOW_TNL_F_CSUM; | |
383 | options++; | |
384 | } | |
385 | ||
386 | if (greh->flags & htons(GRE_KEY)) { | |
3d75c660 | 387 | tnl->tun_id = be32_to_be64(get_16aligned_be32(options)); |
6b241d64 PS |
388 | tnl->flags |= FLOW_TNL_F_KEY; |
389 | options++; | |
390 | } | |
391 | ||
392 | if (greh->flags & htons(GRE_SEQ)) { | |
393 | options++; | |
394 | } | |
395 | ||
63171f04 JS |
396 | /* Set the new packet type depending on the GRE protocol field. */ |
397 | greh_protocol = ntohs(greh->protocol); | |
398 | if (greh_protocol == ETH_TYPE_TEB) { | |
399 | packet->packet_type = htonl(PT_ETH); | |
400 | } else if (greh_protocol >= ETH_TYPE_MIN) { | |
401 | /* Allow all GRE protocol values above 0x5ff as Ethertypes. */ | |
402 | packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, greh_protocol); | |
403 | } else { | |
404 | return -EINVAL; | |
405 | } | |
406 | ||
6b241d64 PS |
407 | return hlen; |
408 | } | |
409 | ||
1c8f98d9 | 410 | struct dp_packet * |
6b241d64 PS |
411 | netdev_gre_pop_header(struct dp_packet *packet) |
412 | { | |
413 | struct pkt_metadata *md = &packet->md; | |
414 | struct flow_tnl *tnl = &md->tunnel; | |
415 | int hlen = sizeof(struct eth_header) + 4; | |
416 | ||
417 | hlen += netdev_tnl_is_header_ipv6(dp_packet_data(packet)) ? | |
418 | IPV6_HEADER_LEN : IP_HEADER_LEN; | |
419 | ||
420 | pkt_metadata_init_tnl(md); | |
421 | if (hlen > dp_packet_size(packet)) { | |
1c8f98d9 | 422 | goto err; |
6b241d64 PS |
423 | } |
424 | ||
425 | hlen = parse_gre_header(packet, tnl); | |
426 | if (hlen < 0) { | |
1c8f98d9 | 427 | goto err; |
6b241d64 PS |
428 | } |
429 | ||
430 | dp_packet_reset_packet(packet, hlen); | |
431 | ||
1c8f98d9 PS |
432 | return packet; |
433 | err: | |
434 | dp_packet_delete(packet); | |
435 | return NULL; | |
6b241d64 PS |
436 | } |
437 | ||
438 | void | |
0ffff497 | 439 | netdev_gre_push_header(const struct netdev *netdev, |
754f8acb | 440 | struct dp_packet *packet, |
6b241d64 PS |
441 | const struct ovs_action_push_tnl *data) |
442 | { | |
0ffff497 WT |
443 | struct netdev_vport *dev = netdev_vport_cast(netdev); |
444 | struct netdev_tunnel_config *tnl_cfg; | |
6b241d64 PS |
445 | struct gre_base_hdr *greh; |
446 | int ip_tot_size; | |
447 | ||
448 | greh = netdev_tnl_push_ip_header(packet, data->header, data->header_len, &ip_tot_size); | |
449 | ||
450 | if (greh->flags & htons(GRE_CSUM)) { | |
451 | ovs_be16 *csum_opt = (ovs_be16 *) (greh + 1); | |
452 | *csum_opt = csum(greh, ip_tot_size); | |
453 | } | |
0ffff497 WT |
454 | |
455 | if (greh->flags & htons(GRE_SEQ)) { | |
456 | /* Last 4 byte is GRE seqno */ | |
457 | int seq_ofs = gre_header_len(greh->flags) - 4; | |
458 | ovs_16aligned_be32 *seq_opt = | |
459 | ALIGNED_CAST(ovs_16aligned_be32 *, (char *)greh + seq_ofs); | |
460 | tnl_cfg = &dev->tnl_cfg; | |
461 | put_16aligned_be32(seq_opt, htonl(tnl_cfg->seqno++)); | |
462 | } | |
6b241d64 PS |
463 | } |
464 | ||
465 | int | |
466 | netdev_gre_build_header(const struct netdev *netdev, | |
467 | struct ovs_action_push_tnl *data, | |
4975aa3e | 468 | const struct netdev_tnl_build_header_params *params) |
6b241d64 PS |
469 | { |
470 | struct netdev_vport *dev = netdev_vport_cast(netdev); | |
471 | struct netdev_tunnel_config *tnl_cfg; | |
6b241d64 PS |
472 | struct gre_base_hdr *greh; |
473 | ovs_16aligned_be32 *options; | |
4975aa3e | 474 | unsigned int hlen; |
6b241d64 PS |
475 | |
476 | /* XXX: RCUfy tnl_cfg. */ | |
477 | ovs_mutex_lock(&dev->mutex); | |
478 | tnl_cfg = &dev->tnl_cfg; | |
479 | ||
4975aa3e | 480 | greh = netdev_tnl_ip_build_header(data, params, IPPROTO_GRE); |
6b241d64 | 481 | |
875ab130 | 482 | if (params->flow->packet_type == htonl(PT_ETH)) { |
63171f04 | 483 | greh->protocol = htons(ETH_TYPE_TEB); |
875ab130 BP |
484 | } else if (pt_ns(params->flow->packet_type) == OFPHTN_ETHERTYPE) { |
485 | greh->protocol = pt_ns_type_be(params->flow->packet_type); | |
486 | } else { | |
487 | ovs_mutex_unlock(&dev->mutex); | |
488 | return 1; | |
63171f04 | 489 | } |
6b241d64 PS |
490 | greh->flags = 0; |
491 | ||
492 | options = (ovs_16aligned_be32 *) (greh + 1); | |
4975aa3e | 493 | if (params->flow->tunnel.flags & FLOW_TNL_F_CSUM) { |
6b241d64 PS |
494 | greh->flags |= htons(GRE_CSUM); |
495 | put_16aligned_be32(options, 0); | |
496 | options++; | |
497 | } | |
498 | ||
499 | if (tnl_cfg->out_key_present) { | |
500 | greh->flags |= htons(GRE_KEY); | |
3d75c660 | 501 | put_16aligned_be32(options, be64_to_be32(params->flow->tunnel.tun_id)); |
6b241d64 PS |
502 | options++; |
503 | } | |
504 | ||
0ffff497 WT |
505 | if (tnl_cfg->set_seq) { |
506 | greh->flags |= htons(GRE_SEQ); | |
507 | /* seqno is updated at push header */ | |
508 | options++; | |
509 | } | |
510 | ||
6b241d64 PS |
511 | ovs_mutex_unlock(&dev->mutex); |
512 | ||
513 | hlen = (uint8_t *) options - (uint8_t *) greh; | |
514 | ||
4975aa3e | 515 | data->header_len += hlen; |
3b10ceee GR |
516 | if (!params->is_ipv6) { |
517 | data->tnl_type = OVS_VPORT_TYPE_GRE; | |
518 | } else { | |
519 | data->tnl_type = OVS_VPORT_TYPE_IP6GRE; | |
520 | } | |
6b241d64 PS |
521 | return 0; |
522 | } | |
523 | ||
7dc18ae9 WT |
524 | struct dp_packet * |
525 | netdev_erspan_pop_header(struct dp_packet *packet) | |
526 | { | |
527 | const struct gre_base_hdr *greh; | |
528 | const struct erspan_base_hdr *ersh; | |
529 | struct pkt_metadata *md = &packet->md; | |
530 | struct flow_tnl *tnl = &md->tunnel; | |
531 | int hlen = sizeof(struct eth_header); | |
532 | unsigned int ulen; | |
533 | uint16_t greh_protocol; | |
534 | ||
535 | hlen += netdev_tnl_is_header_ipv6(dp_packet_data(packet)) ? | |
536 | IPV6_HEADER_LEN : IP_HEADER_LEN; | |
537 | ||
538 | pkt_metadata_init_tnl(md); | |
539 | if (hlen > dp_packet_size(packet)) { | |
540 | goto err; | |
541 | } | |
542 | ||
543 | greh = netdev_tnl_ip_extract_tnl_md(packet, tnl, &ulen); | |
544 | if (!greh) { | |
545 | goto err; | |
546 | } | |
547 | ||
548 | greh_protocol = ntohs(greh->protocol); | |
549 | if (greh_protocol != ETH_TYPE_ERSPAN1 && | |
550 | greh_protocol != ETH_TYPE_ERSPAN2) { | |
551 | goto err; | |
552 | } | |
553 | ||
554 | if (greh->flags & ~htons(GRE_SEQ)) { | |
555 | goto err; | |
556 | } | |
557 | ||
558 | ersh = ERSPAN_HDR(greh); | |
3b10ceee | 559 | tnl->tun_id = be16_to_be64(htons(get_sid(ersh))); |
7dc18ae9 WT |
560 | tnl->erspan_ver = ersh->ver; |
561 | ||
562 | if (ersh->ver == 1) { | |
563 | ovs_16aligned_be32 *index = ALIGNED_CAST(ovs_16aligned_be32 *, | |
564 | ersh + 1); | |
565 | tnl->erspan_idx = ntohl(get_16aligned_be32(index)); | |
566 | tnl->flags |= FLOW_TNL_F_KEY; | |
567 | hlen = ulen + ERSPAN_GREHDR_LEN + sizeof *ersh + ERSPAN_V1_MDSIZE; | |
568 | } else if (ersh->ver == 2) { | |
569 | struct erspan_md2 *md2 = ALIGNED_CAST(struct erspan_md2 *, ersh + 1); | |
570 | tnl->erspan_dir = md2->dir; | |
571 | tnl->erspan_hwid = get_hwid(md2); | |
572 | tnl->flags |= FLOW_TNL_F_KEY; | |
573 | hlen = ulen + ERSPAN_GREHDR_LEN + sizeof *ersh + ERSPAN_V2_MDSIZE; | |
574 | } else { | |
575 | VLOG_WARN_RL(&err_rl, "ERSPAN version error %d", ersh->ver); | |
576 | goto err; | |
577 | } | |
578 | ||
579 | if (hlen > dp_packet_size(packet)) { | |
580 | goto err; | |
581 | } | |
582 | ||
583 | dp_packet_reset_packet(packet, hlen); | |
584 | ||
585 | return packet; | |
586 | err: | |
587 | dp_packet_delete(packet); | |
588 | return NULL; | |
589 | } | |
590 | ||
591 | void | |
592 | netdev_erspan_push_header(const struct netdev *netdev, | |
593 | struct dp_packet *packet, | |
594 | const struct ovs_action_push_tnl *data) | |
595 | { | |
596 | struct netdev_vport *dev = netdev_vport_cast(netdev); | |
597 | struct netdev_tunnel_config *tnl_cfg; | |
598 | struct erspan_base_hdr *ersh; | |
599 | struct gre_base_hdr *greh; | |
600 | struct erspan_md2 *md2; | |
601 | int ip_tot_size; | |
602 | ||
603 | greh = netdev_tnl_push_ip_header(packet, data->header, | |
604 | data->header_len, &ip_tot_size); | |
605 | ||
606 | /* update GRE seqno */ | |
607 | tnl_cfg = &dev->tnl_cfg; | |
608 | ovs_16aligned_be32 *seqno = (ovs_16aligned_be32 *) (greh + 1); | |
609 | put_16aligned_be32(seqno, htonl(tnl_cfg->seqno++)); | |
610 | ||
611 | /* update v2 timestamp */ | |
612 | if (greh->protocol == htons(ETH_TYPE_ERSPAN2)) { | |
613 | ersh = ERSPAN_HDR(greh); | |
614 | md2 = ALIGNED_CAST(struct erspan_md2 *, ersh + 1); | |
615 | put_16aligned_be32(&md2->timestamp, get_erspan_ts(ERSPAN_100US)); | |
616 | } | |
617 | return; | |
618 | } | |
619 | ||
620 | int | |
621 | netdev_erspan_build_header(const struct netdev *netdev, | |
622 | struct ovs_action_push_tnl *data, | |
623 | const struct netdev_tnl_build_header_params *params) | |
624 | { | |
625 | struct netdev_vport *dev = netdev_vport_cast(netdev); | |
626 | struct netdev_tunnel_config *tnl_cfg; | |
627 | struct gre_base_hdr *greh; | |
628 | struct erspan_base_hdr *ersh; | |
629 | unsigned int hlen; | |
630 | uint32_t tun_id; | |
068794b4 | 631 | int erspan_ver; |
7dc18ae9 WT |
632 | uint16_t sid; |
633 | ||
634 | /* XXX: RCUfy tnl_cfg. */ | |
635 | ovs_mutex_lock(&dev->mutex); | |
636 | tnl_cfg = &dev->tnl_cfg; | |
637 | greh = netdev_tnl_ip_build_header(data, params, IPPROTO_GRE); | |
638 | ersh = ERSPAN_HDR(greh); | |
639 | ||
640 | tun_id = ntohl(be64_to_be32(params->flow->tunnel.tun_id)); | |
641 | /* ERSPAN only has 10-bit session ID */ | |
642 | if (tun_id & ~ERSPAN_SID_MASK) { | |
643 | ovs_mutex_unlock(&dev->mutex); | |
644 | return 1; | |
645 | } else { | |
646 | sid = (uint16_t) tun_id; | |
647 | } | |
648 | ||
068794b4 GR |
649 | if (tnl_cfg->erspan_ver_flow) { |
650 | erspan_ver = params->flow->tunnel.erspan_ver; | |
651 | } else { | |
652 | erspan_ver = tnl_cfg->erspan_ver; | |
653 | } | |
654 | ||
655 | if (erspan_ver == 1) { | |
656 | ovs_be32 *index; | |
657 | ||
7dc18ae9 WT |
658 | greh->protocol = htons(ETH_TYPE_ERSPAN1); |
659 | greh->flags = htons(GRE_SEQ); | |
660 | ersh->ver = 1; | |
661 | set_sid(ersh, sid); | |
662 | ||
663 | put_16aligned_be32(ALIGNED_CAST(ovs_16aligned_be32 *, ersh + 1), | |
664 | htonl(tnl_cfg->erspan_idx)); | |
665 | ||
068794b4 GR |
666 | index = (ovs_be32 *)(ersh + 1); |
667 | ||
668 | if (tnl_cfg->erspan_idx_flow) { | |
669 | *index = htonl(params->flow->tunnel.erspan_idx); | |
670 | } else { | |
671 | *index = htonl(tnl_cfg->erspan_idx); | |
672 | } | |
673 | ||
7dc18ae9 | 674 | hlen = ERSPAN_GREHDR_LEN + sizeof *ersh + ERSPAN_V1_MDSIZE; |
068794b4 GR |
675 | } else if (erspan_ver == 2) { |
676 | struct erspan_md2 *md2 = ALIGNED_CAST(struct erspan_md2 *, ersh + 1); | |
677 | ||
7dc18ae9 WT |
678 | greh->protocol = htons(ETH_TYPE_ERSPAN2); |
679 | greh->flags = htons(GRE_SEQ); | |
680 | ersh->ver = 2; | |
681 | set_sid(ersh, sid); | |
682 | ||
7dc18ae9 WT |
683 | md2->sgt = 0; /* security group tag */ |
684 | md2->gra = 0; | |
685 | put_16aligned_be32(&md2->timestamp, 0); | |
068794b4 GR |
686 | |
687 | if (tnl_cfg->erspan_hwid_flow) { | |
688 | set_hwid(md2, params->flow->tunnel.erspan_hwid); | |
689 | } else { | |
690 | set_hwid(md2, tnl_cfg->erspan_hwid); | |
691 | } | |
692 | ||
693 | if (tnl_cfg->erspan_dir_flow) { | |
694 | md2->dir = params->flow->tunnel.erspan_dir; | |
695 | } else { | |
696 | md2->dir = tnl_cfg->erspan_dir; | |
697 | } | |
7dc18ae9 WT |
698 | |
699 | hlen = ERSPAN_GREHDR_LEN + sizeof *ersh + ERSPAN_V2_MDSIZE; | |
700 | } else { | |
701 | VLOG_WARN_RL(&err_rl, "ERSPAN version error %d", tnl_cfg->erspan_ver); | |
702 | ovs_mutex_unlock(&dev->mutex); | |
703 | return 1; | |
704 | } | |
705 | ||
706 | ovs_mutex_unlock(&dev->mutex); | |
707 | ||
708 | data->header_len += hlen; | |
709 | ||
710 | if (params->is_ipv6) { | |
711 | data->tnl_type = OVS_VPORT_TYPE_IP6ERSPAN; | |
712 | } else { | |
713 | data->tnl_type = OVS_VPORT_TYPE_ERSPAN; | |
714 | } | |
715 | return 0; | |
716 | } | |
717 | ||
1c8f98d9 | 718 | struct dp_packet * |
6b241d64 PS |
719 | netdev_vxlan_pop_header(struct dp_packet *packet) |
720 | { | |
721 | struct pkt_metadata *md = &packet->md; | |
722 | struct flow_tnl *tnl = &md->tunnel; | |
723 | struct vxlanhdr *vxh; | |
724 | unsigned int hlen; | |
439f39cb GS |
725 | ovs_be32 vx_flags; |
726 | enum packet_type next_pt = PT_ETH; | |
6b241d64 | 727 | |
1fc2e1bd BB |
728 | ovs_assert(packet->l3_ofs > 0); |
729 | ovs_assert(packet->l4_ofs > 0); | |
730 | ||
6b241d64 PS |
731 | pkt_metadata_init_tnl(md); |
732 | if (VXLAN_HLEN > dp_packet_l4_size(packet)) { | |
1c8f98d9 | 733 | goto err; |
6b241d64 PS |
734 | } |
735 | ||
736 | vxh = udp_extract_tnl_md(packet, tnl, &hlen); | |
737 | if (!vxh) { | |
1c8f98d9 | 738 | goto err; |
6b241d64 PS |
739 | } |
740 | ||
439f39cb GS |
741 | vx_flags = get_16aligned_be32(&vxh->vx_flags); |
742 | if (vx_flags & htonl(VXLAN_HF_GPE)) { | |
743 | vx_flags &= htonl(~VXLAN_GPE_USED_BITS); | |
744 | /* Drop the OAM packets */ | |
745 | if (vxh->vx_gpe.flags & VXLAN_GPE_FLAGS_O) { | |
746 | goto err; | |
747 | } | |
748 | switch (vxh->vx_gpe.next_protocol) { | |
749 | case VXLAN_GPE_NP_IPV4: | |
750 | next_pt = PT_IPV4; | |
751 | break; | |
752 | case VXLAN_GPE_NP_IPV6: | |
753 | next_pt = PT_IPV6; | |
754 | break; | |
478b1473 JS |
755 | case VXLAN_GPE_NP_NSH: |
756 | next_pt = PT_NSH; | |
757 | break; | |
439f39cb GS |
758 | case VXLAN_GPE_NP_ETHERNET: |
759 | next_pt = PT_ETH; | |
760 | break; | |
761 | default: | |
762 | goto err; | |
763 | } | |
764 | } | |
765 | ||
766 | if (vx_flags != htonl(VXLAN_FLAGS) || | |
6b241d64 PS |
767 | (get_16aligned_be32(&vxh->vx_vni) & htonl(0xff))) { |
768 | VLOG_WARN_RL(&err_rl, "invalid vxlan flags=%#x vni=%#x\n", | |
439f39cb | 769 | ntohl(vx_flags), |
6b241d64 | 770 | ntohl(get_16aligned_be32(&vxh->vx_vni))); |
1c8f98d9 | 771 | goto err; |
6b241d64 PS |
772 | } |
773 | tnl->tun_id = htonll(ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8); | |
774 | tnl->flags |= FLOW_TNL_F_KEY; | |
775 | ||
439f39cb | 776 | packet->packet_type = htonl(next_pt); |
6b241d64 | 777 | dp_packet_reset_packet(packet, hlen + VXLAN_HLEN); |
439f39cb GS |
778 | if (next_pt != PT_ETH) { |
779 | packet->l3_ofs = 0; | |
780 | } | |
6b241d64 | 781 | |
1c8f98d9 PS |
782 | return packet; |
783 | err: | |
784 | dp_packet_delete(packet); | |
785 | return NULL; | |
6b241d64 PS |
786 | } |
787 | ||
788 | int | |
789 | netdev_vxlan_build_header(const struct netdev *netdev, | |
790 | struct ovs_action_push_tnl *data, | |
4975aa3e | 791 | const struct netdev_tnl_build_header_params *params) |
6b241d64 PS |
792 | { |
793 | struct netdev_vport *dev = netdev_vport_cast(netdev); | |
794 | struct netdev_tunnel_config *tnl_cfg; | |
795 | struct vxlanhdr *vxh; | |
6b241d64 PS |
796 | |
797 | /* XXX: RCUfy tnl_cfg. */ | |
798 | ovs_mutex_lock(&dev->mutex); | |
799 | tnl_cfg = &dev->tnl_cfg; | |
800 | ||
4975aa3e | 801 | vxh = udp_build_header(tnl_cfg, data, params); |
6b241d64 | 802 | |
439f39cb GS |
803 | if (tnl_cfg->exts & (1 << OVS_VXLAN_EXT_GPE)) { |
804 | put_16aligned_be32(&vxh->vx_flags, htonl(VXLAN_FLAGS | VXLAN_HF_GPE)); | |
805 | put_16aligned_be32(&vxh->vx_vni, | |
806 | htonl(ntohll(params->flow->tunnel.tun_id) << 8)); | |
875ab130 BP |
807 | if (params->flow->packet_type == htonl(PT_ETH)) { |
808 | vxh->vx_gpe.next_protocol = VXLAN_GPE_NP_ETHERNET; | |
809 | } else if (pt_ns(params->flow->packet_type) == OFPHTN_ETHERTYPE) { | |
810 | switch (pt_ns_type(params->flow->packet_type)) { | |
439f39cb GS |
811 | case ETH_TYPE_IP: |
812 | vxh->vx_gpe.next_protocol = VXLAN_GPE_NP_IPV4; | |
813 | break; | |
814 | case ETH_TYPE_IPV6: | |
815 | vxh->vx_gpe.next_protocol = VXLAN_GPE_NP_IPV6; | |
816 | break; | |
478b1473 JS |
817 | case ETH_TYPE_NSH: |
818 | vxh->vx_gpe.next_protocol = VXLAN_GPE_NP_NSH; | |
819 | break; | |
439f39cb GS |
820 | case ETH_TYPE_TEB: |
821 | vxh->vx_gpe.next_protocol = VXLAN_GPE_NP_ETHERNET; | |
822 | break; | |
875ab130 BP |
823 | default: |
824 | goto drop; | |
439f39cb GS |
825 | } |
826 | } else { | |
875ab130 | 827 | goto drop; |
439f39cb GS |
828 | } |
829 | } else { | |
830 | put_16aligned_be32(&vxh->vx_flags, htonl(VXLAN_FLAGS)); | |
831 | put_16aligned_be32(&vxh->vx_vni, | |
832 | htonl(ntohll(params->flow->tunnel.tun_id) << 8)); | |
833 | } | |
6b241d64 PS |
834 | |
835 | ovs_mutex_unlock(&dev->mutex); | |
4975aa3e | 836 | data->header_len += sizeof *vxh; |
6b241d64 PS |
837 | data->tnl_type = OVS_VPORT_TYPE_VXLAN; |
838 | return 0; | |
875ab130 BP |
839 | |
840 | drop: | |
841 | ovs_mutex_unlock(&dev->mutex); | |
842 | return 1; | |
6b241d64 PS |
843 | } |
844 | ||
1c8f98d9 | 845 | struct dp_packet * |
6b241d64 PS |
846 | netdev_geneve_pop_header(struct dp_packet *packet) |
847 | { | |
848 | struct pkt_metadata *md = &packet->md; | |
849 | struct flow_tnl *tnl = &md->tunnel; | |
850 | struct genevehdr *gnh; | |
851 | unsigned int hlen, opts_len, ulen; | |
852 | ||
853 | pkt_metadata_init_tnl(md); | |
854 | if (GENEVE_BASE_HLEN > dp_packet_l4_size(packet)) { | |
855 | VLOG_WARN_RL(&err_rl, "geneve packet too small: min header=%u packet size=%"PRIuSIZE"\n", | |
856 | (unsigned int)GENEVE_BASE_HLEN, dp_packet_l4_size(packet)); | |
1c8f98d9 | 857 | goto err; |
6b241d64 PS |
858 | } |
859 | ||
860 | gnh = udp_extract_tnl_md(packet, tnl, &ulen); | |
861 | if (!gnh) { | |
1c8f98d9 | 862 | goto err; |
6b241d64 PS |
863 | } |
864 | ||
865 | opts_len = gnh->opt_len * 4; | |
866 | hlen = ulen + GENEVE_BASE_HLEN + opts_len; | |
867 | if (hlen > dp_packet_size(packet)) { | |
868 | VLOG_WARN_RL(&err_rl, "geneve packet too small: header len=%u packet size=%u\n", | |
869 | hlen, dp_packet_size(packet)); | |
1c8f98d9 | 870 | goto err; |
6b241d64 PS |
871 | } |
872 | ||
873 | if (gnh->ver != 0) { | |
874 | VLOG_WARN_RL(&err_rl, "unknown geneve version: %"PRIu8"\n", gnh->ver); | |
1c8f98d9 | 875 | goto err; |
6b241d64 PS |
876 | } |
877 | ||
878 | if (gnh->proto_type != htons(ETH_TYPE_TEB)) { | |
879 | VLOG_WARN_RL(&err_rl, "unknown geneve encapsulated protocol: %#x\n", | |
880 | ntohs(gnh->proto_type)); | |
1c8f98d9 | 881 | goto err; |
6b241d64 PS |
882 | } |
883 | ||
884 | tnl->flags |= gnh->oam ? FLOW_TNL_F_OAM : 0; | |
885 | tnl->tun_id = htonll(ntohl(get_16aligned_be32(&gnh->vni)) >> 8); | |
886 | tnl->flags |= FLOW_TNL_F_KEY; | |
887 | ||
888 | memcpy(tnl->metadata.opts.gnv, gnh->options, opts_len); | |
889 | tnl->metadata.present.len = opts_len; | |
890 | tnl->flags |= FLOW_TNL_F_UDPIF; | |
891 | ||
63171f04 | 892 | packet->packet_type = htonl(PT_ETH); |
6b241d64 PS |
893 | dp_packet_reset_packet(packet, hlen); |
894 | ||
1c8f98d9 PS |
895 | return packet; |
896 | err: | |
897 | dp_packet_delete(packet); | |
898 | return NULL; | |
6b241d64 PS |
899 | } |
900 | ||
901 | int | |
902 | netdev_geneve_build_header(const struct netdev *netdev, | |
903 | struct ovs_action_push_tnl *data, | |
4975aa3e | 904 | const struct netdev_tnl_build_header_params *params) |
6b241d64 PS |
905 | { |
906 | struct netdev_vport *dev = netdev_vport_cast(netdev); | |
907 | struct netdev_tunnel_config *tnl_cfg; | |
908 | struct genevehdr *gnh; | |
909 | int opt_len; | |
910 | bool crit_opt; | |
6b241d64 PS |
911 | |
912 | /* XXX: RCUfy tnl_cfg. */ | |
913 | ovs_mutex_lock(&dev->mutex); | |
914 | tnl_cfg = &dev->tnl_cfg; | |
915 | ||
4975aa3e | 916 | gnh = udp_build_header(tnl_cfg, data, params); |
6b241d64 | 917 | |
4975aa3e | 918 | put_16aligned_be32(&gnh->vni, htonl(ntohll(params->flow->tunnel.tun_id) << 8)); |
6b241d64 PS |
919 | |
920 | ovs_mutex_unlock(&dev->mutex); | |
921 | ||
4975aa3e | 922 | opt_len = tun_metadata_to_geneve_header(¶ms->flow->tunnel, |
6b241d64 PS |
923 | gnh->options, &crit_opt); |
924 | ||
925 | gnh->opt_len = opt_len / 4; | |
4975aa3e | 926 | gnh->oam = !!(params->flow->tunnel.flags & FLOW_TNL_F_OAM); |
6b241d64 PS |
927 | gnh->critical = crit_opt ? 1 : 0; |
928 | gnh->proto_type = htons(ETH_TYPE_TEB); | |
929 | ||
4975aa3e | 930 | data->header_len += sizeof *gnh + opt_len; |
6b241d64 PS |
931 | data->tnl_type = OVS_VPORT_TYPE_GENEVE; |
932 | return 0; | |
933 | } | |
934 | ||
935 | \f | |
936 | void | |
937 | netdev_tnl_egress_port_range(struct unixctl_conn *conn, int argc, | |
938 | const char *argv[], void *aux OVS_UNUSED) | |
939 | { | |
940 | int val1, val2; | |
941 | ||
942 | if (argc < 3) { | |
943 | struct ds ds = DS_EMPTY_INITIALIZER; | |
944 | ||
945 | ds_put_format(&ds, "Tunnel UDP source port range: %"PRIu16"-%"PRIu16"\n", | |
946 | tnl_udp_port_min, tnl_udp_port_max); | |
947 | ||
948 | unixctl_command_reply(conn, ds_cstr(&ds)); | |
949 | ds_destroy(&ds); | |
950 | return; | |
951 | } | |
952 | ||
953 | if (argc != 3) { | |
954 | return; | |
955 | } | |
956 | ||
957 | val1 = atoi(argv[1]); | |
958 | if (val1 <= 0 || val1 > UINT16_MAX) { | |
959 | unixctl_command_reply(conn, "Invalid min."); | |
960 | return; | |
961 | } | |
962 | val2 = atoi(argv[2]); | |
963 | if (val2 <= 0 || val2 > UINT16_MAX) { | |
964 | unixctl_command_reply(conn, "Invalid max."); | |
965 | return; | |
966 | } | |
967 | ||
968 | if (val1 > val2) { | |
969 | tnl_udp_port_min = val2; | |
970 | tnl_udp_port_max = val1; | |
971 | } else { | |
972 | tnl_udp_port_min = val1; | |
973 | tnl_udp_port_max = val2; | |
974 | } | |
975 | seq_change(tnl_conf_seq); | |
976 | ||
977 | unixctl_command_reply(conn, "OK"); | |
978 | } |