]>
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 | |
218 | netdev_tnl_push_udp_header(struct dp_packet *packet, | |
219 | const struct ovs_action_push_tnl *data) | |
220 | { | |
221 | struct udp_header *udp; | |
222 | int ip_tot_size; | |
223 | ||
224 | udp = netdev_tnl_push_ip_header(packet, data->header, data->header_len, &ip_tot_size); | |
225 | ||
226 | /* set udp src port */ | |
227 | udp->udp_src = netdev_tnl_get_src_port(packet); | |
228 | udp->udp_len = htons(ip_tot_size); | |
229 | ||
230 | if (udp->udp_csum) { | |
231 | uint32_t csum; | |
232 | if (netdev_tnl_is_header_ipv6(dp_packet_data(packet))) { | |
233 | csum = packet_csum_pseudoheader6(netdev_tnl_ipv6_hdr(dp_packet_data(packet))); | |
234 | } else { | |
235 | csum = packet_csum_pseudoheader(netdev_tnl_ip_hdr(dp_packet_data(packet))); | |
236 | } | |
237 | ||
238 | csum = csum_continue(csum, udp, ip_tot_size); | |
239 | udp->udp_csum = csum_finish(csum); | |
240 | ||
241 | if (!udp->udp_csum) { | |
242 | udp->udp_csum = htons(0xffff); | |
243 | } | |
244 | } | |
245 | } | |
246 | ||
247 | static void * | |
4975aa3e PS |
248 | eth_build_header(struct ovs_action_push_tnl *data, |
249 | const struct netdev_tnl_build_header_params *params) | |
6b241d64 | 250 | { |
4975aa3e PS |
251 | uint16_t eth_proto = params->is_ipv6 ? ETH_TYPE_IPV6 : ETH_TYPE_IP; |
252 | struct eth_header *eth; | |
6b241d64 | 253 | |
4975aa3e | 254 | memset(data->header, 0, sizeof data->header); |
6b241d64 | 255 | |
4975aa3e PS |
256 | eth = (struct eth_header *)data->header; |
257 | eth->eth_dst = params->dmac; | |
258 | eth->eth_src = params->smac; | |
259 | eth->eth_type = htons(eth_proto); | |
260 | data->header_len = sizeof(struct eth_header); | |
261 | return eth + 1; | |
262 | } | |
6b241d64 | 263 | |
4975aa3e PS |
264 | void * |
265 | netdev_tnl_ip_build_header(struct ovs_action_push_tnl *data, | |
266 | const struct netdev_tnl_build_header_params *params, | |
267 | uint8_t next_proto) | |
268 | { | |
269 | void *l3; | |
270 | ||
271 | l3 = eth_build_header(data, params); | |
272 | if (!params->is_ipv6) { | |
273 | ovs_be32 ip_src = in6_addr_get_mapped_ipv4(params->s_ip); | |
274 | struct ip_header *ip; | |
275 | ||
276 | ip = (struct ip_header *) l3; | |
277 | ||
278 | ip->ip_ihl_ver = IP_IHL_VER(5, 4); | |
279 | ip->ip_tos = params->flow->tunnel.ip_tos; | |
280 | ip->ip_ttl = params->flow->tunnel.ip_ttl; | |
281 | ip->ip_proto = next_proto; | |
282 | put_16aligned_be32(&ip->ip_src, ip_src); | |
283 | put_16aligned_be32(&ip->ip_dst, params->flow->tunnel.ip_dst); | |
284 | ||
285 | ip->ip_frag_off = (params->flow->tunnel.flags & FLOW_TNL_F_DONT_FRAGMENT) ? | |
286 | htons(IP_DF) : 0; | |
287 | ||
ece9c294 | 288 | /* Checksum has already been zeroed by eth_build_header. */ |
4975aa3e PS |
289 | ip->ip_csum = csum(ip, sizeof *ip); |
290 | ||
291 | data->header_len += IP_HEADER_LEN; | |
292 | return ip + 1; | |
6b241d64 | 293 | } else { |
4975aa3e PS |
294 | struct ovs_16aligned_ip6_hdr *ip6; |
295 | ||
296 | ip6 = (struct ovs_16aligned_ip6_hdr *) l3; | |
297 | ||
98c086db PS |
298 | put_16aligned_be32(&ip6->ip6_flow, htonl(6 << 28) | |
299 | htonl(params->flow->tunnel.ip_tos << 20)); | |
4975aa3e PS |
300 | ip6->ip6_hlim = params->flow->tunnel.ip_ttl; |
301 | ip6->ip6_nxt = next_proto; | |
302 | memcpy(&ip6->ip6_src, params->s_ip, sizeof(ovs_be32[4])); | |
303 | memcpy(&ip6->ip6_dst, ¶ms->flow->tunnel.ipv6_dst, sizeof(ovs_be32[4])); | |
304 | ||
305 | data->header_len += IPV6_HEADER_LEN; | |
306 | return ip6 + 1; | |
6b241d64 | 307 | } |
4975aa3e PS |
308 | } |
309 | ||
310 | static void * | |
311 | udp_build_header(struct netdev_tunnel_config *tnl_cfg, | |
312 | struct ovs_action_push_tnl *data, | |
313 | const struct netdev_tnl_build_header_params *params) | |
314 | { | |
315 | struct udp_header *udp; | |
6b241d64 | 316 | |
4975aa3e | 317 | udp = netdev_tnl_ip_build_header(data, params, IPPROTO_UDP); |
6b241d64 PS |
318 | udp->udp_dst = tnl_cfg->dst_port; |
319 | ||
4975aa3e | 320 | if (params->is_ipv6 || params->flow->tunnel.flags & FLOW_TNL_F_CSUM) { |
6b241d64 PS |
321 | /* Write a value in now to mark that we should compute the checksum |
322 | * later. 0xffff is handy because it is transparent to the | |
323 | * calculation. */ | |
324 | udp->udp_csum = htons(0xffff); | |
325 | } | |
4975aa3e | 326 | data->header_len += sizeof *udp; |
6b241d64 PS |
327 | return udp + 1; |
328 | } | |
329 | ||
330 | static int | |
331 | gre_header_len(ovs_be16 flags) | |
332 | { | |
333 | int hlen = 4; | |
334 | ||
335 | if (flags & htons(GRE_CSUM)) { | |
336 | hlen += 4; | |
337 | } | |
338 | if (flags & htons(GRE_KEY)) { | |
339 | hlen += 4; | |
340 | } | |
341 | if (flags & htons(GRE_SEQ)) { | |
342 | hlen += 4; | |
343 | } | |
344 | return hlen; | |
345 | } | |
346 | ||
347 | static int | |
348 | parse_gre_header(struct dp_packet *packet, | |
349 | struct flow_tnl *tnl) | |
350 | { | |
351 | const struct gre_base_hdr *greh; | |
352 | ovs_16aligned_be32 *options; | |
353 | int hlen; | |
354 | unsigned int ulen; | |
63171f04 | 355 | uint16_t greh_protocol; |
6b241d64 PS |
356 | |
357 | greh = netdev_tnl_ip_extract_tnl_md(packet, tnl, &ulen); | |
358 | if (!greh) { | |
359 | return -EINVAL; | |
360 | } | |
361 | ||
362 | if (greh->flags & ~(htons(GRE_CSUM | GRE_KEY | GRE_SEQ))) { | |
363 | return -EINVAL; | |
364 | } | |
365 | ||
6b241d64 PS |
366 | hlen = ulen + gre_header_len(greh->flags); |
367 | if (hlen > dp_packet_size(packet)) { | |
368 | return -EINVAL; | |
369 | } | |
370 | ||
371 | options = (ovs_16aligned_be32 *)(greh + 1); | |
372 | if (greh->flags & htons(GRE_CSUM)) { | |
373 | ovs_be16 pkt_csum; | |
374 | ||
375 | pkt_csum = csum(greh, dp_packet_size(packet) - | |
376 | ((const unsigned char *)greh - | |
2482b0b0 | 377 | (const unsigned char *)dp_packet_eth(packet))); |
6b241d64 PS |
378 | if (pkt_csum) { |
379 | return -EINVAL; | |
380 | } | |
381 | tnl->flags = FLOW_TNL_F_CSUM; | |
382 | options++; | |
383 | } | |
384 | ||
385 | if (greh->flags & htons(GRE_KEY)) { | |
3d75c660 | 386 | tnl->tun_id = be32_to_be64(get_16aligned_be32(options)); |
6b241d64 PS |
387 | tnl->flags |= FLOW_TNL_F_KEY; |
388 | options++; | |
389 | } | |
390 | ||
391 | if (greh->flags & htons(GRE_SEQ)) { | |
392 | options++; | |
393 | } | |
394 | ||
63171f04 JS |
395 | /* Set the new packet type depending on the GRE protocol field. */ |
396 | greh_protocol = ntohs(greh->protocol); | |
397 | if (greh_protocol == ETH_TYPE_TEB) { | |
398 | packet->packet_type = htonl(PT_ETH); | |
399 | } else if (greh_protocol >= ETH_TYPE_MIN) { | |
400 | /* Allow all GRE protocol values above 0x5ff as Ethertypes. */ | |
401 | packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, greh_protocol); | |
402 | } else { | |
403 | return -EINVAL; | |
404 | } | |
405 | ||
6b241d64 PS |
406 | return hlen; |
407 | } | |
408 | ||
1c8f98d9 | 409 | struct dp_packet * |
6b241d64 PS |
410 | netdev_gre_pop_header(struct dp_packet *packet) |
411 | { | |
412 | struct pkt_metadata *md = &packet->md; | |
413 | struct flow_tnl *tnl = &md->tunnel; | |
414 | int hlen = sizeof(struct eth_header) + 4; | |
415 | ||
416 | hlen += netdev_tnl_is_header_ipv6(dp_packet_data(packet)) ? | |
417 | IPV6_HEADER_LEN : IP_HEADER_LEN; | |
418 | ||
419 | pkt_metadata_init_tnl(md); | |
420 | if (hlen > dp_packet_size(packet)) { | |
1c8f98d9 | 421 | goto err; |
6b241d64 PS |
422 | } |
423 | ||
424 | hlen = parse_gre_header(packet, tnl); | |
425 | if (hlen < 0) { | |
1c8f98d9 | 426 | goto err; |
6b241d64 PS |
427 | } |
428 | ||
429 | dp_packet_reset_packet(packet, hlen); | |
430 | ||
1c8f98d9 PS |
431 | return packet; |
432 | err: | |
433 | dp_packet_delete(packet); | |
434 | return NULL; | |
6b241d64 PS |
435 | } |
436 | ||
437 | void | |
438 | netdev_gre_push_header(struct dp_packet *packet, | |
439 | const struct ovs_action_push_tnl *data) | |
440 | { | |
441 | struct gre_base_hdr *greh; | |
442 | int ip_tot_size; | |
443 | ||
444 | greh = netdev_tnl_push_ip_header(packet, data->header, data->header_len, &ip_tot_size); | |
445 | ||
446 | if (greh->flags & htons(GRE_CSUM)) { | |
447 | ovs_be16 *csum_opt = (ovs_be16 *) (greh + 1); | |
448 | *csum_opt = csum(greh, ip_tot_size); | |
449 | } | |
450 | } | |
451 | ||
452 | int | |
453 | netdev_gre_build_header(const struct netdev *netdev, | |
454 | struct ovs_action_push_tnl *data, | |
4975aa3e | 455 | const struct netdev_tnl_build_header_params *params) |
6b241d64 PS |
456 | { |
457 | struct netdev_vport *dev = netdev_vport_cast(netdev); | |
458 | struct netdev_tunnel_config *tnl_cfg; | |
6b241d64 PS |
459 | struct gre_base_hdr *greh; |
460 | ovs_16aligned_be32 *options; | |
4975aa3e | 461 | unsigned int hlen; |
6b241d64 PS |
462 | |
463 | /* XXX: RCUfy tnl_cfg. */ | |
464 | ovs_mutex_lock(&dev->mutex); | |
465 | tnl_cfg = &dev->tnl_cfg; | |
466 | ||
4975aa3e | 467 | greh = netdev_tnl_ip_build_header(data, params, IPPROTO_GRE); |
6b241d64 | 468 | |
875ab130 | 469 | if (params->flow->packet_type == htonl(PT_ETH)) { |
63171f04 | 470 | greh->protocol = htons(ETH_TYPE_TEB); |
875ab130 BP |
471 | } else if (pt_ns(params->flow->packet_type) == OFPHTN_ETHERTYPE) { |
472 | greh->protocol = pt_ns_type_be(params->flow->packet_type); | |
473 | } else { | |
474 | ovs_mutex_unlock(&dev->mutex); | |
475 | return 1; | |
63171f04 | 476 | } |
6b241d64 PS |
477 | greh->flags = 0; |
478 | ||
479 | options = (ovs_16aligned_be32 *) (greh + 1); | |
4975aa3e | 480 | if (params->flow->tunnel.flags & FLOW_TNL_F_CSUM) { |
6b241d64 PS |
481 | greh->flags |= htons(GRE_CSUM); |
482 | put_16aligned_be32(options, 0); | |
483 | options++; | |
484 | } | |
485 | ||
486 | if (tnl_cfg->out_key_present) { | |
487 | greh->flags |= htons(GRE_KEY); | |
3d75c660 | 488 | put_16aligned_be32(options, be64_to_be32(params->flow->tunnel.tun_id)); |
6b241d64 PS |
489 | options++; |
490 | } | |
491 | ||
492 | ovs_mutex_unlock(&dev->mutex); | |
493 | ||
494 | hlen = (uint8_t *) options - (uint8_t *) greh; | |
495 | ||
4975aa3e | 496 | data->header_len += hlen; |
6b241d64 PS |
497 | data->tnl_type = OVS_VPORT_TYPE_GRE; |
498 | return 0; | |
499 | } | |
500 | ||
1c8f98d9 | 501 | struct dp_packet * |
6b241d64 PS |
502 | netdev_vxlan_pop_header(struct dp_packet *packet) |
503 | { | |
504 | struct pkt_metadata *md = &packet->md; | |
505 | struct flow_tnl *tnl = &md->tunnel; | |
506 | struct vxlanhdr *vxh; | |
507 | unsigned int hlen; | |
439f39cb GS |
508 | ovs_be32 vx_flags; |
509 | enum packet_type next_pt = PT_ETH; | |
6b241d64 | 510 | |
1fc2e1bd BB |
511 | ovs_assert(packet->l3_ofs > 0); |
512 | ovs_assert(packet->l4_ofs > 0); | |
513 | ||
6b241d64 PS |
514 | pkt_metadata_init_tnl(md); |
515 | if (VXLAN_HLEN > dp_packet_l4_size(packet)) { | |
1c8f98d9 | 516 | goto err; |
6b241d64 PS |
517 | } |
518 | ||
519 | vxh = udp_extract_tnl_md(packet, tnl, &hlen); | |
520 | if (!vxh) { | |
1c8f98d9 | 521 | goto err; |
6b241d64 PS |
522 | } |
523 | ||
439f39cb GS |
524 | vx_flags = get_16aligned_be32(&vxh->vx_flags); |
525 | if (vx_flags & htonl(VXLAN_HF_GPE)) { | |
526 | vx_flags &= htonl(~VXLAN_GPE_USED_BITS); | |
527 | /* Drop the OAM packets */ | |
528 | if (vxh->vx_gpe.flags & VXLAN_GPE_FLAGS_O) { | |
529 | goto err; | |
530 | } | |
531 | switch (vxh->vx_gpe.next_protocol) { | |
532 | case VXLAN_GPE_NP_IPV4: | |
533 | next_pt = PT_IPV4; | |
534 | break; | |
535 | case VXLAN_GPE_NP_IPV6: | |
536 | next_pt = PT_IPV6; | |
537 | break; | |
478b1473 JS |
538 | case VXLAN_GPE_NP_NSH: |
539 | next_pt = PT_NSH; | |
540 | break; | |
439f39cb GS |
541 | case VXLAN_GPE_NP_ETHERNET: |
542 | next_pt = PT_ETH; | |
543 | break; | |
544 | default: | |
545 | goto err; | |
546 | } | |
547 | } | |
548 | ||
549 | if (vx_flags != htonl(VXLAN_FLAGS) || | |
6b241d64 PS |
550 | (get_16aligned_be32(&vxh->vx_vni) & htonl(0xff))) { |
551 | VLOG_WARN_RL(&err_rl, "invalid vxlan flags=%#x vni=%#x\n", | |
439f39cb | 552 | ntohl(vx_flags), |
6b241d64 | 553 | ntohl(get_16aligned_be32(&vxh->vx_vni))); |
1c8f98d9 | 554 | goto err; |
6b241d64 PS |
555 | } |
556 | tnl->tun_id = htonll(ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8); | |
557 | tnl->flags |= FLOW_TNL_F_KEY; | |
558 | ||
439f39cb | 559 | packet->packet_type = htonl(next_pt); |
6b241d64 | 560 | dp_packet_reset_packet(packet, hlen + VXLAN_HLEN); |
439f39cb GS |
561 | if (next_pt != PT_ETH) { |
562 | packet->l3_ofs = 0; | |
563 | } | |
6b241d64 | 564 | |
1c8f98d9 PS |
565 | return packet; |
566 | err: | |
567 | dp_packet_delete(packet); | |
568 | return NULL; | |
6b241d64 PS |
569 | } |
570 | ||
571 | int | |
572 | netdev_vxlan_build_header(const struct netdev *netdev, | |
573 | struct ovs_action_push_tnl *data, | |
4975aa3e | 574 | const struct netdev_tnl_build_header_params *params) |
6b241d64 PS |
575 | { |
576 | struct netdev_vport *dev = netdev_vport_cast(netdev); | |
577 | struct netdev_tunnel_config *tnl_cfg; | |
578 | struct vxlanhdr *vxh; | |
6b241d64 PS |
579 | |
580 | /* XXX: RCUfy tnl_cfg. */ | |
581 | ovs_mutex_lock(&dev->mutex); | |
582 | tnl_cfg = &dev->tnl_cfg; | |
583 | ||
4975aa3e | 584 | vxh = udp_build_header(tnl_cfg, data, params); |
6b241d64 | 585 | |
439f39cb GS |
586 | if (tnl_cfg->exts & (1 << OVS_VXLAN_EXT_GPE)) { |
587 | put_16aligned_be32(&vxh->vx_flags, htonl(VXLAN_FLAGS | VXLAN_HF_GPE)); | |
588 | put_16aligned_be32(&vxh->vx_vni, | |
589 | htonl(ntohll(params->flow->tunnel.tun_id) << 8)); | |
875ab130 BP |
590 | if (params->flow->packet_type == htonl(PT_ETH)) { |
591 | vxh->vx_gpe.next_protocol = VXLAN_GPE_NP_ETHERNET; | |
592 | } else if (pt_ns(params->flow->packet_type) == OFPHTN_ETHERTYPE) { | |
593 | switch (pt_ns_type(params->flow->packet_type)) { | |
439f39cb GS |
594 | case ETH_TYPE_IP: |
595 | vxh->vx_gpe.next_protocol = VXLAN_GPE_NP_IPV4; | |
596 | break; | |
597 | case ETH_TYPE_IPV6: | |
598 | vxh->vx_gpe.next_protocol = VXLAN_GPE_NP_IPV6; | |
599 | break; | |
478b1473 JS |
600 | case ETH_TYPE_NSH: |
601 | vxh->vx_gpe.next_protocol = VXLAN_GPE_NP_NSH; | |
602 | break; | |
439f39cb GS |
603 | case ETH_TYPE_TEB: |
604 | vxh->vx_gpe.next_protocol = VXLAN_GPE_NP_ETHERNET; | |
605 | break; | |
875ab130 BP |
606 | default: |
607 | goto drop; | |
439f39cb GS |
608 | } |
609 | } else { | |
875ab130 | 610 | goto drop; |
439f39cb GS |
611 | } |
612 | } else { | |
613 | put_16aligned_be32(&vxh->vx_flags, htonl(VXLAN_FLAGS)); | |
614 | put_16aligned_be32(&vxh->vx_vni, | |
615 | htonl(ntohll(params->flow->tunnel.tun_id) << 8)); | |
616 | } | |
6b241d64 PS |
617 | |
618 | ovs_mutex_unlock(&dev->mutex); | |
4975aa3e | 619 | data->header_len += sizeof *vxh; |
6b241d64 PS |
620 | data->tnl_type = OVS_VPORT_TYPE_VXLAN; |
621 | return 0; | |
875ab130 BP |
622 | |
623 | drop: | |
624 | ovs_mutex_unlock(&dev->mutex); | |
625 | return 1; | |
6b241d64 PS |
626 | } |
627 | ||
1c8f98d9 | 628 | struct dp_packet * |
6b241d64 PS |
629 | netdev_geneve_pop_header(struct dp_packet *packet) |
630 | { | |
631 | struct pkt_metadata *md = &packet->md; | |
632 | struct flow_tnl *tnl = &md->tunnel; | |
633 | struct genevehdr *gnh; | |
634 | unsigned int hlen, opts_len, ulen; | |
635 | ||
636 | pkt_metadata_init_tnl(md); | |
637 | if (GENEVE_BASE_HLEN > dp_packet_l4_size(packet)) { | |
638 | VLOG_WARN_RL(&err_rl, "geneve packet too small: min header=%u packet size=%"PRIuSIZE"\n", | |
639 | (unsigned int)GENEVE_BASE_HLEN, dp_packet_l4_size(packet)); | |
1c8f98d9 | 640 | goto err; |
6b241d64 PS |
641 | } |
642 | ||
643 | gnh = udp_extract_tnl_md(packet, tnl, &ulen); | |
644 | if (!gnh) { | |
1c8f98d9 | 645 | goto err; |
6b241d64 PS |
646 | } |
647 | ||
648 | opts_len = gnh->opt_len * 4; | |
649 | hlen = ulen + GENEVE_BASE_HLEN + opts_len; | |
650 | if (hlen > dp_packet_size(packet)) { | |
651 | VLOG_WARN_RL(&err_rl, "geneve packet too small: header len=%u packet size=%u\n", | |
652 | hlen, dp_packet_size(packet)); | |
1c8f98d9 | 653 | goto err; |
6b241d64 PS |
654 | } |
655 | ||
656 | if (gnh->ver != 0) { | |
657 | VLOG_WARN_RL(&err_rl, "unknown geneve version: %"PRIu8"\n", gnh->ver); | |
1c8f98d9 | 658 | goto err; |
6b241d64 PS |
659 | } |
660 | ||
661 | if (gnh->proto_type != htons(ETH_TYPE_TEB)) { | |
662 | VLOG_WARN_RL(&err_rl, "unknown geneve encapsulated protocol: %#x\n", | |
663 | ntohs(gnh->proto_type)); | |
1c8f98d9 | 664 | goto err; |
6b241d64 PS |
665 | } |
666 | ||
667 | tnl->flags |= gnh->oam ? FLOW_TNL_F_OAM : 0; | |
668 | tnl->tun_id = htonll(ntohl(get_16aligned_be32(&gnh->vni)) >> 8); | |
669 | tnl->flags |= FLOW_TNL_F_KEY; | |
670 | ||
671 | memcpy(tnl->metadata.opts.gnv, gnh->options, opts_len); | |
672 | tnl->metadata.present.len = opts_len; | |
673 | tnl->flags |= FLOW_TNL_F_UDPIF; | |
674 | ||
63171f04 | 675 | packet->packet_type = htonl(PT_ETH); |
6b241d64 PS |
676 | dp_packet_reset_packet(packet, hlen); |
677 | ||
1c8f98d9 PS |
678 | return packet; |
679 | err: | |
680 | dp_packet_delete(packet); | |
681 | return NULL; | |
6b241d64 PS |
682 | } |
683 | ||
684 | int | |
685 | netdev_geneve_build_header(const struct netdev *netdev, | |
686 | struct ovs_action_push_tnl *data, | |
4975aa3e | 687 | const struct netdev_tnl_build_header_params *params) |
6b241d64 PS |
688 | { |
689 | struct netdev_vport *dev = netdev_vport_cast(netdev); | |
690 | struct netdev_tunnel_config *tnl_cfg; | |
691 | struct genevehdr *gnh; | |
692 | int opt_len; | |
693 | bool crit_opt; | |
6b241d64 PS |
694 | |
695 | /* XXX: RCUfy tnl_cfg. */ | |
696 | ovs_mutex_lock(&dev->mutex); | |
697 | tnl_cfg = &dev->tnl_cfg; | |
698 | ||
4975aa3e | 699 | gnh = udp_build_header(tnl_cfg, data, params); |
6b241d64 | 700 | |
4975aa3e | 701 | put_16aligned_be32(&gnh->vni, htonl(ntohll(params->flow->tunnel.tun_id) << 8)); |
6b241d64 PS |
702 | |
703 | ovs_mutex_unlock(&dev->mutex); | |
704 | ||
4975aa3e | 705 | opt_len = tun_metadata_to_geneve_header(¶ms->flow->tunnel, |
6b241d64 PS |
706 | gnh->options, &crit_opt); |
707 | ||
708 | gnh->opt_len = opt_len / 4; | |
4975aa3e | 709 | gnh->oam = !!(params->flow->tunnel.flags & FLOW_TNL_F_OAM); |
6b241d64 PS |
710 | gnh->critical = crit_opt ? 1 : 0; |
711 | gnh->proto_type = htons(ETH_TYPE_TEB); | |
712 | ||
4975aa3e | 713 | data->header_len += sizeof *gnh + opt_len; |
6b241d64 PS |
714 | data->tnl_type = OVS_VPORT_TYPE_GENEVE; |
715 | return 0; | |
716 | } | |
717 | ||
718 | \f | |
719 | void | |
720 | netdev_tnl_egress_port_range(struct unixctl_conn *conn, int argc, | |
721 | const char *argv[], void *aux OVS_UNUSED) | |
722 | { | |
723 | int val1, val2; | |
724 | ||
725 | if (argc < 3) { | |
726 | struct ds ds = DS_EMPTY_INITIALIZER; | |
727 | ||
728 | ds_put_format(&ds, "Tunnel UDP source port range: %"PRIu16"-%"PRIu16"\n", | |
729 | tnl_udp_port_min, tnl_udp_port_max); | |
730 | ||
731 | unixctl_command_reply(conn, ds_cstr(&ds)); | |
732 | ds_destroy(&ds); | |
733 | return; | |
734 | } | |
735 | ||
736 | if (argc != 3) { | |
737 | return; | |
738 | } | |
739 | ||
740 | val1 = atoi(argv[1]); | |
741 | if (val1 <= 0 || val1 > UINT16_MAX) { | |
742 | unixctl_command_reply(conn, "Invalid min."); | |
743 | return; | |
744 | } | |
745 | val2 = atoi(argv[2]); | |
746 | if (val2 <= 0 || val2 > UINT16_MAX) { | |
747 | unixctl_command_reply(conn, "Invalid max."); | |
748 | return; | |
749 | } | |
750 | ||
751 | if (val1 > val2) { | |
752 | tnl_udp_port_min = val2; | |
753 | tnl_udp_port_max = val1; | |
754 | } else { | |
755 | tnl_udp_port_min = val1; | |
756 | tnl_udp_port_max = val2; | |
757 | } | |
758 | seq_change(tnl_conf_seq); | |
759 | ||
760 | unixctl_command_reply(conn, "OK"); | |
761 | } |