Remove unused replay variable in conftests seq number reset hook
[strongswan.git] / src / conftest / hooks / reset_seq.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "hook.h"
17
18 #include <linux/xfrm.h>
19 #include <unistd.h>
20 #include <errno.h>
21
22 #include <processing/jobs/callback_job.h>
23 #include <plugins/kernel_netlink/kernel_netlink_shared.h>
24
25 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
26
27 typedef struct private_reset_seq_t private_reset_seq_t;
28
29 /**
30 * Private data of an reset_seq_t object.
31 */
32 struct private_reset_seq_t {
33
34 /**
35 * Implements the hook_t interface.
36 */
37 hook_t hook;
38
39 /**
40 * Delay for reset
41 */
42 int delay;
43 };
44
45 /**
46 * Callback job
47 */
48 static job_requeue_t reset_cb(struct xfrm_usersa_id *data)
49 {
50 netlink_buf_t request;
51 struct nlmsghdr *hdr;
52 struct xfrm_aevent_id *id;
53 struct rtattr *rthdr;
54 struct sockaddr_nl addr;
55 int s, len;
56
57 DBG1(DBG_CFG, "resetting sequence number of SPI 0x%x", htonl(data->spi));
58
59 memset(&request, 0, sizeof(request));
60
61 hdr = (struct nlmsghdr*)request;
62 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE;
63 hdr->nlmsg_seq = 201;
64 hdr->nlmsg_pid = getpid();
65 hdr->nlmsg_type = XFRM_MSG_NEWAE;
66 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
67
68 id = (struct xfrm_aevent_id*)NLMSG_DATA(hdr);
69 id->sa_id = *data;
70
71 rthdr = XFRM_RTA(hdr, struct xfrm_aevent_id);
72 rthdr->rta_type = XFRMA_REPLAY_VAL;
73 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_replay_state));
74 hdr->nlmsg_len += rthdr->rta_len;
75
76 s = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
77 if (s == -1)
78 {
79 DBG1(DBG_CFG, "opening XFRM socket failed: %s", strerror(errno));
80 return JOB_REQUEUE_NONE;
81 }
82 memset(&addr, 0, sizeof(addr));
83 addr.nl_family = AF_NETLINK;
84 len = sendto(s, hdr, hdr->nlmsg_len, 0,
85 (struct sockaddr*)&addr, sizeof(addr));
86 if (len != hdr->nlmsg_len)
87 {
88 DBG1(DBG_CFG, "sending XFRM aevent failed: %s", strerror(errno));
89 }
90 close(s);
91 return JOB_REQUEUE_NONE;
92 }
93
94 /**
95 * Schedule sequence number reset job
96 */
97 static void schedule_reset_job(private_reset_seq_t *this, host_t *dst,
98 u_int32_t spi)
99 {
100 struct xfrm_usersa_id *data;
101 chunk_t chunk;
102
103 INIT(data,
104 .spi = spi,
105 .family = dst->get_family(dst),
106 .proto = IPPROTO_ESP,
107 );
108
109 chunk = dst->get_address(dst);
110 memcpy(&data->daddr, chunk.ptr, min(chunk.len, sizeof(xfrm_address_t)));
111
112 lib->scheduler->schedule_job(lib->scheduler,
113 (job_t*)callback_job_create(
114 (void*)reset_cb, data, (void*)free, NULL),
115 this->delay);
116 }
117
118 METHOD(listener_t, child_updown, bool,
119 private_reset_seq_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa,
120 bool up)
121 {
122 if (up)
123 {
124 schedule_reset_job(this, ike_sa->get_other_host(ike_sa),
125 child_sa->get_spi(child_sa, FALSE));
126 }
127 return TRUE;
128 }
129
130 METHOD(hook_t, destroy, void,
131 private_reset_seq_t *this)
132 {
133 free(this);
134 }
135
136 /**
137 * Create the IKE_AUTH fill hook
138 */
139 hook_t *reset_seq_hook_create(char *name)
140 {
141 private_reset_seq_t *this;
142
143 INIT(this,
144 .hook = {
145 .listener = {
146 .child_updown = _child_updown,
147 },
148 .destroy = _destroy,
149 },
150 .delay = conftest->test->get_int(conftest->test,
151 "hooks.%s.delay", 10, name),
152 );
153
154 return &this->hook;
155 }