]> git.proxmox.com Git - ovs.git/blame - lib/conntrack-icmp.c
bump version to 2.15.0+ds1-2+deb11u3.1
[ovs.git] / lib / conntrack-icmp.c
CommitLineData
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"
2078901a 25#include "conntrack-tp.h"
b269a122
DDP
26#include "dp-packet.h"
27
967bb5c5 28enum OVS_PACKED_ENUM icmp_state {
b269a122
DDP
29 ICMPS_FIRST,
30 ICMPS_REPLY,
31};
32
33struct conn_icmp {
34 struct conn up;
967bb5c5 35 enum icmp_state state; /* 'conn' lock protected. */
b269a122
DDP
36};
37
38static const enum ct_timeout icmp_timeouts[] = {
39 [ICMPS_FIRST] = CT_TM_ICMP_FIRST,
40 [ICMPS_REPLY] = CT_TM_ICMP_REPLY,
41};
42
43static struct conn_icmp *
44conn_icmp_cast(const struct conn *conn)
45{
46 return CONTAINER_OF(conn, struct conn_icmp, up);
47}
48
49static enum ct_update_res
967bb5c5 50icmp_conn_update(struct conntrack *ct, struct conn *conn_,
b269a122
DDP
51 struct dp_packet *pkt OVS_UNUSED, bool reply, long long now)
52{
53 struct conn_icmp *conn = conn_icmp_cast(conn_);
08905c93 54 enum ct_update_res ret = CT_UPDATE_VALID;
b269a122 55
d93c3111
WT
56 if (reply && conn->state == ICMPS_FIRST) {
57 conn->state = ICMPS_REPLY;
08905c93
TZ
58 } else if (conn->state == ICMPS_FIRST) {
59 ret = CT_UPDATE_VALID_NEW;
d93c3111
WT
60 }
61
62 conn_update_expiration(ct, &conn->up, icmp_timeouts[conn->state], now);
08905c93 63 return ret;
b269a122
DDP
64}
65
66static bool
67icmp4_valid_new(struct dp_packet *pkt)
68{
69 struct icmp_header *icmp = dp_packet_l4(pkt);
70
71 return icmp->icmp_type == ICMP4_ECHO_REQUEST
72 || icmp->icmp_type == ICMP4_INFOREQUEST
73 || icmp->icmp_type == ICMP4_TIMESTAMP;
74}
75
76static bool
77icmp6_valid_new(struct dp_packet *pkt)
78{
79 struct icmp6_header *icmp6 = dp_packet_l4(pkt);
80
81 return icmp6->icmp6_type == ICMP6_ECHO_REQUEST;
82}
83
84static struct conn *
967bb5c5 85icmp_new_conn(struct conntrack *ct, struct dp_packet *pkt OVS_UNUSED,
2078901a 86 long long now, uint32_t tp_id)
b269a122 87{
967bb5c5 88 struct conn_icmp *conn = xzalloc(sizeof *conn);
b269a122 89 conn->state = ICMPS_FIRST;
2078901a 90 conn->up.tp_id = tp_id;
b269a122 91
2078901a 92 conn_init_expiration(ct, &conn->up, icmp_timeouts[conn->state], now);
b269a122
DDP
93 return &conn->up;
94}
95
96struct ct_l4_proto ct_proto_icmp4 = {
97 .new_conn = icmp_new_conn,
98 .valid_new = icmp4_valid_new,
99 .conn_update = icmp_conn_update,
100};
101
102struct ct_l4_proto ct_proto_icmp6 = {
103 .new_conn = icmp_new_conn,
104 .valid_new = icmp6_valid_new,
105 .conn_update = icmp_conn_update,
106};