• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
19 #include <errno.h>
20 #include <linux/netlink.h>
21 #include <linux/rtnetlink.h>
22 #include <netinet/in.h>
23 #include <string.h>
24 
25 #include <netlink-private/object-api.h>
26 #include <netlink-private/types.h>
27 #include <netlink/msg.h>
28 #include <netlink/netlink.h>
29 #include <netlink/socket.h>
30 
31 #include "netlink_callbacks.h"
32 #include "netlink_msg.h"
33 
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  */
inet_family_size(int family)38 size_t inet_family_size(int family) {
39   if (family == AF_INET) {
40     return sizeof(struct in_addr);
41   } else if (family == AF_INET6) {
42     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  */
nlmsg_alloc_generic(uint16_t type,uint16_t flags,void * payload_struct,size_t payload_len)55 struct nl_msg *nlmsg_alloc_generic(uint16_t type, uint16_t flags, void *payload_struct,
56                                    size_t payload_len) {
57   struct nl_msg *msg;
58 
59   msg = nlmsg_alloc();
60   if (!msg) {
61     return NULL;
62   }
63 
64   if ((sizeof(struct nl_msg) + payload_len) > msg->nm_size) {
65     nlmsg_free(msg);
66     return NULL;
67   }
68 
69   msg->nm_nlh->nlmsg_len   = NLMSG_LENGTH(payload_len);
70   msg->nm_nlh->nlmsg_flags = flags;
71   msg->nm_nlh->nlmsg_type  = type;
72 
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  */
nlmsg_alloc_ifaddr(uint16_t type,uint16_t flags,struct ifaddrmsg * ifa)84 struct 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  */
nlmsg_alloc_ifinfo(uint16_t type,uint16_t flags,struct ifinfomsg * ifi)94 struct 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  */
nlmsg_alloc_rtmsg(uint16_t type,uint16_t flags,struct rtmsg * rt)104 struct 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 
108 /* function: netlink_set_kernel_only
109  * sets a socket to receive messages only from the kernel
110  * sock - socket to connect
111  */
netlink_set_kernel_only(struct nl_sock * nl_sk)112 int 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);
120   return connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
121 }
122 
123 /* 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  */
send_netlink_msg(struct nl_msg * msg,struct nl_cb * callbacks)128 void send_netlink_msg(struct nl_msg *msg, struct nl_cb *callbacks) {
129   struct nl_sock *nl_sk;
130 
131   nl_sk = nl_socket_alloc();
132   if (!nl_sk) goto cleanup;
133 
134   if (nl_connect(nl_sk, NETLINK_ROUTE) != 0) goto cleanup;
135 
136   if (nl_send_auto_complete(nl_sk, msg) < 0) goto cleanup;
137 
138   if (netlink_set_kernel_only(nl_sk) < 0) goto cleanup;
139 
140   nl_recvmsgs(nl_sk, callbacks);
141 
142 cleanup:
143   if (nl_sk) nl_socket_free(nl_sk);
144 }
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  */
send_ifaddrmsg(uint16_t type,uint16_t flags,struct ifaddrmsg * ifa,struct nl_cb * callbacks)153 void 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);
157   if (!msg) return;
158 
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  */
netlink_sendrecv(struct nl_msg * msg)168 int netlink_sendrecv(struct nl_msg *msg) {
169   struct nl_cb *callbacks = NULL;
170   int retval              = -EIO;
171 
172   callbacks = alloc_ack_callbacks(&retval);
173   if (!callbacks) {
174     return -ENOMEM;
175   }
176 
177   send_netlink_msg(msg, callbacks);
178 
179   nl_cb_put(callbacks);
180 
181   return retval;
182 }
183