66682907d9536b5ee0fbfb9bf45d38d667616bb6
[strongswan.git] / src / libhydra / plugins / kernel_netlink / kernel_netlink_shared.h
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * 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 * General purpose netlink buffer.
25 *
26 * 1024 byte is currently sufficient for all operations. Some platform
27 * require an enforced aligment to four bytes (e.g. ARM).
28 */
29 typedef union {
30 struct nlmsghdr hdr;
31 u_char bytes[1024];
32 } netlink_buf_t __attribute__((aligned(RTA_ALIGNTO)));
33
34 typedef struct netlink_socket_t netlink_socket_t;
35
36 /**
37 * Wrapper around a netlink socket.
38 */
39 struct netlink_socket_t {
40
41 /**
42 * Send a netlink message and wait for a reply.
43 *
44 * @param in netlink message to send
45 * @param out received netlink message
46 * @param out_len length of the received message
47 */
48 status_t (*send)(netlink_socket_t *this, struct nlmsghdr *in,
49 struct nlmsghdr **out, size_t *out_len);
50
51 /**
52 * Send a netlink message and wait for its acknowledge.
53 *
54 * @param in netlink message to send
55 */
56 status_t (*send_ack)(netlink_socket_t *this, struct nlmsghdr *in);
57
58 /**
59 * Destroy the socket.
60 */
61 void (*destroy)(netlink_socket_t *this);
62 };
63
64 /**
65 * Create a netlink_socket_t object.
66 *
67 * @param protocol protocol type (e.g. NETLINK_XFRM or NETLINK_ROUTE)
68 * @param names optional enum names for Netlink messages
69 * @param parallel support parallel queries on this Netlink socket
70 */
71 netlink_socket_t *netlink_socket_create(int protocol, enum_name_t *names,
72 bool parallel);
73
74 /**
75 * Creates an rtattr and adds it to the given netlink message.
76 *
77 * @param hdr netlink message
78 * @param rta_type type of the rtattr
79 * @param data data to add to the rtattr
80 * @param buflen length of the netlink message buffer
81 */
82 void netlink_add_attribute(struct nlmsghdr *hdr, int rta_type, chunk_t data,
83 size_t buflen);
84
85 /**
86 * Reserve space in a netlink message for given size and type, returning buffer.
87 *
88 * @param hdr netlink message
89 * @param buflen size of full netlink buffer
90 * @param type RTA type
91 * @param len length of RTA data
92 * @return buffer to len bytes of attribute data, NULL on error
93 */
94 void* netlink_reserve(struct nlmsghdr *hdr, int buflen, int type, int len);
95
96 #endif /* KERNEL_NETLINK_SHARED_H_ */