blob: be76ecda833cbe3c6759dafa667eab06ed819009 [file] [log] [blame]
Daniel Drowna45056e2012-03-23 10:42:54 -05001/*
2 * Copyright 2012 Daniel Drown
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 * netlink_msg.c - send an ifaddrmsg/ifinfomsg/rtmsg via netlink
17 */
18
junyulaic4e591a2018-11-26 22:36:10 +090019#include <errno.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050020#include <linux/netlink.h>
21#include <linux/rtnetlink.h>
junyulaic4e591a2018-11-26 22:36:10 +090022#include <netinet/in.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050023#include <string.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050024
Paul Stewartc2f9edd2016-11-11 11:58:52 -080025#include <netlink-private/object-api.h>
26#include <netlink-private/types.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050027#include <netlink/msg.h>
junyulaic4e591a2018-11-26 22:36:10 +090028#include <netlink/netlink.h>
29#include <netlink/socket.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050030
Daniel Drowna45056e2012-03-23 10:42:54 -050031#include "netlink_callbacks.h"
junyulaic4e591a2018-11-26 22:36:10 +090032#include "netlink_msg.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050033
34/* function: family_size
35 * returns the size of the address structure for the given family, or 0 on error
36 * family - AF_INET or AF_INET6
37 */
38size_t inet_family_size(int family) {
junyulaic4e591a2018-11-26 22:36:10 +090039 if (family == AF_INET) {
Daniel Drowna45056e2012-03-23 10:42:54 -050040 return sizeof(struct in_addr);
junyulaic4e591a2018-11-26 22:36:10 +090041 } else if (family == AF_INET6) {
Daniel Drowna45056e2012-03-23 10:42:54 -050042 return sizeof(struct in6_addr);
43 } else {
44 return 0;
45 }
46}
47
48/* function: nlmsg_alloc_generic
49 * allocates a netlink message with the given struct inside of it. returns NULL on failure
50 * type - netlink message type
51 * flags - netlink message flags
52 * payload_struct - pointer to a struct to add to netlink message
53 * payload_len - bytelength of structure
54 */
junyulaic4e591a2018-11-26 22:36:10 +090055struct nl_msg *nlmsg_alloc_generic(uint16_t type, uint16_t flags, void *payload_struct,
56 size_t payload_len) {
Daniel Drowna45056e2012-03-23 10:42:54 -050057 struct nl_msg *msg;
58
59 msg = nlmsg_alloc();
junyulaic4e591a2018-11-26 22:36:10 +090060 if (!msg) {
Daniel Drowna45056e2012-03-23 10:42:54 -050061 return NULL;
62 }
63
64 if ((sizeof(struct nl_msg) + payload_len) > msg->nm_size) {
65 nlmsg_free(msg);
66 return NULL;
67 }
68
junyulaic4e591a2018-11-26 22:36:10 +090069 msg->nm_nlh->nlmsg_len = NLMSG_LENGTH(payload_len);
Daniel Drowna45056e2012-03-23 10:42:54 -050070 msg->nm_nlh->nlmsg_flags = flags;
junyulaic4e591a2018-11-26 22:36:10 +090071 msg->nm_nlh->nlmsg_type = type;
Daniel Drowna45056e2012-03-23 10:42:54 -050072
73 memcpy(nlmsg_data(msg->nm_nlh), payload_struct, payload_len);
74
75 return msg;
76}
77
78/* function: nlmsg_alloc_ifaddr
79 * allocates a netlink message with a struct ifaddrmsg inside of it. returns NULL on failure
80 * type - netlink message type
81 * flags - netlink message flags
82 * ifa - ifaddrmsg to copy into the new netlink message
83 */
84struct nl_msg *nlmsg_alloc_ifaddr(uint16_t type, uint16_t flags, struct ifaddrmsg *ifa) {
85 return nlmsg_alloc_generic(type, flags, ifa, sizeof(*ifa));
86}
87
88/* function: nlmsg_alloc_ifinfo
89 * allocates a netlink message with a struct ifinfomsg inside of it. returns NULL on failure
90 * type - netlink message type
91 * flags - netlink message flags
92 * ifi - ifinfomsg to copy into the new netlink message
93 */
94struct nl_msg *nlmsg_alloc_ifinfo(uint16_t type, uint16_t flags, struct ifinfomsg *ifi) {
95 return nlmsg_alloc_generic(type, flags, ifi, sizeof(*ifi));
96}
97
98/* function: nlmsg_alloc_rtmsg
99 * allocates a netlink message with a struct rtmsg inside of it. returns NULL on failure
100 * type - netlink message type
101 * flags - netlink message flags
102 * rt - rtmsg to copy into the new netlink message
103 */
104struct nl_msg *nlmsg_alloc_rtmsg(uint16_t type, uint16_t flags, struct rtmsg *rt) {
105 return nlmsg_alloc_generic(type, flags, rt, sizeof(*rt));
106}
107
Lorenzo Colitti4f3d7862013-02-01 13:18:35 +0900108/* function: netlink_set_kernel_only
109 * sets a socket to receive messages only from the kernel
110 * sock - socket to connect
111 */
112int netlink_set_kernel_only(struct nl_sock *nl_sk) {
113 struct sockaddr_nl addr = { AF_NETLINK, 0, 0, 0 };
114
115 if (!nl_sk) {
116 return -EFAULT;
117 }
118
119 int sockfd = nl_socket_get_fd(nl_sk);
junyulaic4e591a2018-11-26 22:36:10 +0900120 return connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
Lorenzo Colitti4f3d7862013-02-01 13:18:35 +0900121}
122
Daniel Drowna45056e2012-03-23 10:42:54 -0500123/* function: send_netlink_msg
124 * sends a netlink message, reads a response, and hands the response(s) to the callbacks
125 * msg - netlink message to send
126 * callbacks - callbacks to use on responses
127 */
128void send_netlink_msg(struct nl_msg *msg, struct nl_cb *callbacks) {
129 struct nl_sock *nl_sk;
130
131 nl_sk = nl_socket_alloc();
junyulaic4e591a2018-11-26 22:36:10 +0900132 if (!nl_sk) goto cleanup;
Daniel Drowna45056e2012-03-23 10:42:54 -0500133
junyulaic4e591a2018-11-26 22:36:10 +0900134 if (nl_connect(nl_sk, NETLINK_ROUTE) != 0) goto cleanup;
Daniel Drowna45056e2012-03-23 10:42:54 -0500135
junyulaic4e591a2018-11-26 22:36:10 +0900136 if (nl_send_auto_complete(nl_sk, msg) < 0) goto cleanup;
Daniel Drowna45056e2012-03-23 10:42:54 -0500137
junyulaic4e591a2018-11-26 22:36:10 +0900138 if (netlink_set_kernel_only(nl_sk) < 0) goto cleanup;
Lorenzo Colitti4f3d7862013-02-01 13:18:35 +0900139
Daniel Drowna45056e2012-03-23 10:42:54 -0500140 nl_recvmsgs(nl_sk, callbacks);
141
142cleanup:
junyulaic4e591a2018-11-26 22:36:10 +0900143 if (nl_sk) nl_socket_free(nl_sk);
Daniel Drowna45056e2012-03-23 10:42:54 -0500144}
145
146/* function: send_ifaddrmsg
147 * sends a netlink/ifaddrmsg message and hands the responses to the callbacks
148 * type - netlink message type
149 * flags - netlink message flags
150 * ifa - ifaddrmsg to send
151 * callbacks - callbacks to use with the responses
152 */
153void send_ifaddrmsg(uint16_t type, uint16_t flags, struct ifaddrmsg *ifa, struct nl_cb *callbacks) {
154 struct nl_msg *msg = NULL;
155
156 msg = nlmsg_alloc_ifaddr(type, flags, ifa);
junyulaic4e591a2018-11-26 22:36:10 +0900157 if (!msg) return;
Daniel Drowna45056e2012-03-23 10:42:54 -0500158
159 send_netlink_msg(msg, callbacks);
160
161 nlmsg_free(msg);
162}
163
164/* function: netlink_sendrecv
165 * send a nl_msg and return an int status - only supports OK/ERROR responses
166 * msg - msg to send
167 */
168int netlink_sendrecv(struct nl_msg *msg) {
169 struct nl_cb *callbacks = NULL;
junyulaic4e591a2018-11-26 22:36:10 +0900170 int retval = -EIO;
Daniel Drowna45056e2012-03-23 10:42:54 -0500171
172 callbacks = alloc_ack_callbacks(&retval);
junyulaic4e591a2018-11-26 22:36:10 +0900173 if (!callbacks) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500174 return -ENOMEM;
175 }
176
177 send_netlink_msg(msg, callbacks);
178
179 nl_cb_put(callbacks);
180
181 return retval;
182}