]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libcharon/plugins/kernel_netlink/kernel_netlink_shared.h
kernel-netlink: Extract shared route handling code in net/ipsec
[thirdparty/strongswan.git] / src / libcharon / plugins / kernel_netlink / kernel_netlink_shared.h
1 /*
2 * Copyright (C) 2008-2020 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
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 #ifndef KERNEL_NETLINK_SHARED_H_
17 #define KERNEL_NETLINK_SHARED_H_
18
19 #include <library.h>
20
21 #include <linux/rtnetlink.h>
22
23 /**
24 * Default buffer size.
25 *
26 * 1024 byte is currently sufficient for all operations.
27 */
28 #ifndef KERNEL_NETLINK_BUFSIZE
29 #define KERNEL_NETLINK_BUFSIZE 1024
30 #endif
31
32 /**
33 * General purpose netlink buffer.
34 *
35 * Some platforms require an enforced alignment to four bytes (e.g. ARM).
36 */
37 typedef union {
38 struct nlmsghdr hdr;
39 u_char bytes[KERNEL_NETLINK_BUFSIZE];
40 } netlink_buf_t __attribute__((aligned(RTA_ALIGNTO)));
41
42 typedef struct netlink_socket_t netlink_socket_t;
43
44 /**
45 * Wrapper around a netlink socket.
46 */
47 struct netlink_socket_t {
48
49 /**
50 * Send a netlink message and wait for a reply.
51 *
52 * @param in netlink message to send
53 * @param out received netlink message
54 * @param out_len length of the received message
55 */
56 status_t (*send)(netlink_socket_t *this, struct nlmsghdr *in,
57 struct nlmsghdr **out, size_t *out_len);
58
59 /**
60 * Send a netlink message and wait for its acknowledge.
61 *
62 * @param in netlink message to send
63 */
64 status_t (*send_ack)(netlink_socket_t *this, struct nlmsghdr *in);
65
66 /**
67 * Destroy the socket.
68 */
69 void (*destroy)(netlink_socket_t *this);
70 };
71
72 /**
73 * Create a netlink_socket_t object.
74 *
75 * @param protocol protocol type (e.g. NETLINK_XFRM or NETLINK_ROUTE)
76 * @param names optional enum names for Netlink messages
77 * @param parallel support parallel queries on this Netlink socket
78 */
79 netlink_socket_t *netlink_socket_create(int protocol, enum_name_t *names,
80 bool parallel);
81
82 /**
83 * Creates an rtattr and adds it to the given netlink message.
84 *
85 * @param hdr netlink message
86 * @param rta_type type of the rtattr
87 * @param data data to add to the rtattr
88 * @param buflen length of the netlink message buffer
89 */
90 void netlink_add_attribute(struct nlmsghdr *hdr, int rta_type, chunk_t data,
91 size_t buflen);
92
93 /**
94 * Creates an rtattr under which other rtattrs are nested to the given netlink
95 * message.
96 *
97 * The returned pointer has to be passed to netlink_nested_end() after the
98 * nested attributes have been added to the message.
99 *
100 * @param hdr netlink message
101 * @param buflen size of full netlink buffer
102 * @param type RTA type
103 * @return attribute pointer
104 */
105 void *netlink_nested_start(struct nlmsghdr *hdr, size_t buflen, int type);
106
107 /**
108 * Updates the length of the given attribute after nested attributes were added.
109 *
110 * @param hdr netlink message
111 * @param attr attribute returned from netlink_nested_start()
112 */
113 void netlink_nested_end(struct nlmsghdr *hdr, void *attr);
114
115 /**
116 * Reserve space in a netlink message for given size and type, returning buffer.
117 *
118 * @param hdr netlink message
119 * @param buflen size of full netlink buffer
120 * @param type RTA type
121 * @param len length of RTA data
122 * @return buffer to len bytes of attribute data, NULL on error
123 */
124 void* netlink_reserve(struct nlmsghdr *hdr, int buflen, int type, int len);
125
126 /**
127 * Determine buffer size for received messages (e.g. events).
128 *
129 * @return buffer size
130 */
131 u_int netlink_get_buflen();
132
133 /**
134 * Information about an installed route.
135 */
136 struct route_entry_t {
137
138 /** Destination net */
139 chunk_t dst_net;
140
141 /** Destination net prefix length */
142 uint8_t prefixlen;
143
144 /** Name of the interface the route is bound to (optional) */
145 char *if_name;
146
147 /** Source IP of the route (virtual IP or %any) */
148 host_t *src_ip;
149
150 /** Gateway for this route (optional) */
151 host_t *gateway;
152
153 /** Whether the route was installed for a passthrough policy */
154 bool pass;
155 };
156
157 typedef struct route_entry_t route_entry_t;
158
159 /**
160 * Destroy a route entry.
161 */
162 void route_entry_destroy(route_entry_t *this);
163
164 /**
165 * Clone a route entry.
166 */
167 route_entry_t *route_entry_clone(const route_entry_t *this);
168
169 /**
170 * Hash a route entry (note that this only hashes the destination).
171 */
172 u_int route_entry_hash(const route_entry_t *this);
173
174 /**
175 * Compare two route entries.
176 */
177 bool route_entry_equals(const route_entry_t *a, const route_entry_t *b);
178
179 #endif /* KERNEL_NETLINK_SHARED_H_ */