]>
Commit | Line | Data |
---|---|---|
b269a122 | 1 | /* |
967bb5c5 | 2 | * Copyright (c) 2015-2019 Nicira, Inc. |
b269a122 DDP |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | ||
19 | #include <errno.h> | |
20 | #include <sys/types.h> | |
21 | #include <netinet/in.h> | |
22 | #include <netinet/icmp6.h> | |
23 | ||
24 | #include "conntrack-private.h" | |
25 | #include "dp-packet.h" | |
26 | ||
967bb5c5 | 27 | enum OVS_PACKED_ENUM icmp_state { |
b269a122 DDP |
28 | ICMPS_FIRST, |
29 | ICMPS_REPLY, | |
30 | }; | |
31 | ||
32 | struct conn_icmp { | |
33 | struct conn up; | |
967bb5c5 | 34 | enum icmp_state state; /* 'conn' lock protected. */ |
b269a122 DDP |
35 | }; |
36 | ||
37 | static const enum ct_timeout icmp_timeouts[] = { | |
38 | [ICMPS_FIRST] = CT_TM_ICMP_FIRST, | |
39 | [ICMPS_REPLY] = CT_TM_ICMP_REPLY, | |
40 | }; | |
41 | ||
42 | static struct conn_icmp * | |
43 | conn_icmp_cast(const struct conn *conn) | |
44 | { | |
45 | return CONTAINER_OF(conn, struct conn_icmp, up); | |
46 | } | |
47 | ||
48 | static enum ct_update_res | |
967bb5c5 | 49 | icmp_conn_update(struct conntrack *ct, struct conn *conn_, |
b269a122 DDP |
50 | struct dp_packet *pkt OVS_UNUSED, bool reply, long long now) |
51 | { | |
52 | struct conn_icmp *conn = conn_icmp_cast(conn_); | |
967bb5c5 DB |
53 | conn->state = reply ? ICMPS_REPLY : ICMPS_FIRST; |
54 | conn_update_expiration(ct, &conn->up, icmp_timeouts[conn->state], now); | |
b269a122 DDP |
55 | |
56 | return CT_UPDATE_VALID; | |
57 | } | |
58 | ||
59 | static bool | |
60 | icmp4_valid_new(struct dp_packet *pkt) | |
61 | { | |
62 | struct icmp_header *icmp = dp_packet_l4(pkt); | |
63 | ||
64 | return icmp->icmp_type == ICMP4_ECHO_REQUEST | |
65 | || icmp->icmp_type == ICMP4_INFOREQUEST | |
66 | || icmp->icmp_type == ICMP4_TIMESTAMP; | |
67 | } | |
68 | ||
69 | static bool | |
70 | icmp6_valid_new(struct dp_packet *pkt) | |
71 | { | |
72 | struct icmp6_header *icmp6 = dp_packet_l4(pkt); | |
73 | ||
74 | return icmp6->icmp6_type == ICMP6_ECHO_REQUEST; | |
75 | } | |
76 | ||
77 | static struct conn * | |
967bb5c5 DB |
78 | icmp_new_conn(struct conntrack *ct, struct dp_packet *pkt OVS_UNUSED, |
79 | long long now) | |
b269a122 | 80 | { |
967bb5c5 | 81 | struct conn_icmp *conn = xzalloc(sizeof *conn); |
b269a122 | 82 | conn->state = ICMPS_FIRST; |
967bb5c5 | 83 | conn_init_expiration(ct, &conn->up, icmp_timeouts[conn->state], now); |
b269a122 DDP |
84 | |
85 | return &conn->up; | |
86 | } | |
87 | ||
88 | struct ct_l4_proto ct_proto_icmp4 = { | |
89 | .new_conn = icmp_new_conn, | |
90 | .valid_new = icmp4_valid_new, | |
91 | .conn_update = icmp_conn_update, | |
92 | }; | |
93 | ||
94 | struct ct_l4_proto ct_proto_icmp6 = { | |
95 | .new_conn = icmp_new_conn, | |
96 | .valid_new = icmp6_valid_new, | |
97 | .conn_update = icmp_conn_update, | |
98 | }; |