• 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 <netinet/in.h>
20 #include <linux/netlink.h>
21 #include <linux/rtnetlink.h>
22 #include <string.h>
23 #include <errno.h>
24 
25 #include <netlink-types.h>
26 #include <netlink/socket.h>
27 #include <netlink/netlink.h>
28 #include <netlink/msg.h>
29 
30 #include "netlink_msg.h"
31 #include "netlink_callbacks.h"
32 
33 /* function: family_size
34  * returns the size of the address structure for the given family, or 0 on error
35  * family - AF_INET or AF_INET6
36  */
inet_family_size(int family)37 size_t inet_family_size(int family) {
38   if(family == AF_INET) {
39     return sizeof(struct in_addr);
40   } else if(family == AF_INET6) {
41     return sizeof(struct in6_addr);
42   } else {
43     return 0;
44   }
45 }
46 
47 /* function: nlmsg_alloc_generic
48  * allocates a netlink message with the given struct inside of it. returns NULL on failure
49  * type           - netlink message type
50  * flags          - netlink message flags
51  * payload_struct - pointer to a struct to add to netlink message
52  * payload_len    - bytelength of structure
53  */
nlmsg_alloc_generic(uint16_t type,uint16_t flags,void * payload_struct,size_t payload_len)54 struct nl_msg *nlmsg_alloc_generic(uint16_t type, uint16_t flags, void *payload_struct, size_t payload_len) {
55   struct nl_msg *msg;
56 
57   msg = nlmsg_alloc();
58   if(!msg) {
59     return NULL;
60   }
61 
62   if ((sizeof(struct nl_msg) + payload_len) > msg->nm_size) {
63     nlmsg_free(msg);
64     return NULL;
65   }
66 
67   msg->nm_nlh->nlmsg_len = NLMSG_LENGTH(payload_len);
68   msg->nm_nlh->nlmsg_flags = flags;
69   msg->nm_nlh->nlmsg_type = type;
70 
71   memcpy(nlmsg_data(msg->nm_nlh), payload_struct, payload_len);
72 
73   return msg;
74 }
75 
76 /* function: nlmsg_alloc_ifaddr
77  * allocates a netlink message with a struct ifaddrmsg inside of it. returns NULL on failure
78  * type  - netlink message type
79  * flags - netlink message flags
80  * ifa   - ifaddrmsg to copy into the new netlink message
81  */
nlmsg_alloc_ifaddr(uint16_t type,uint16_t flags,struct ifaddrmsg * ifa)82 struct nl_msg *nlmsg_alloc_ifaddr(uint16_t type, uint16_t flags, struct ifaddrmsg *ifa) {
83   return nlmsg_alloc_generic(type, flags, ifa, sizeof(*ifa));
84 }
85 
86 /* function: nlmsg_alloc_ifinfo
87  * allocates a netlink message with a struct ifinfomsg inside of it. returns NULL on failure
88  * type  - netlink message type
89  * flags - netlink message flags
90  * ifi   - ifinfomsg to copy into the new netlink message
91  */
nlmsg_alloc_ifinfo(uint16_t type,uint16_t flags,struct ifinfomsg * ifi)92 struct nl_msg *nlmsg_alloc_ifinfo(uint16_t type, uint16_t flags, struct ifinfomsg *ifi) {
93   return nlmsg_alloc_generic(type, flags, ifi, sizeof(*ifi));
94 }
95 
96 /* function: nlmsg_alloc_rtmsg
97  * allocates a netlink message with a struct rtmsg inside of it. returns NULL on failure
98  * type  - netlink message type
99  * flags - netlink message flags
100  * rt    - rtmsg to copy into the new netlink message
101  */
nlmsg_alloc_rtmsg(uint16_t type,uint16_t flags,struct rtmsg * rt)102 struct nl_msg *nlmsg_alloc_rtmsg(uint16_t type, uint16_t flags, struct rtmsg *rt) {
103   return nlmsg_alloc_generic(type, flags, rt, sizeof(*rt));
104 }
105 
106 /* function: netlink_set_kernel_only
107  * sets a socket to receive messages only from the kernel
108  * sock - socket to connect
109  */
netlink_set_kernel_only(struct nl_sock * nl_sk)110 int netlink_set_kernel_only(struct nl_sock *nl_sk) {
111   struct sockaddr_nl addr = { AF_NETLINK, 0, 0, 0 };
112 
113   if (!nl_sk) {
114     return -EFAULT;
115   }
116 
117   int sockfd = nl_socket_get_fd(nl_sk);
118   return connect(sockfd, (struct sockaddr *) &addr, sizeof(addr));
119 }
120 
121 /* function: send_netlink_msg
122  * sends a netlink message, reads a response, and hands the response(s) to the callbacks
123  * msg       - netlink message to send
124  * callbacks - callbacks to use on responses
125  */
send_netlink_msg(struct nl_msg * msg,struct nl_cb * callbacks)126 void send_netlink_msg(struct nl_msg *msg, struct nl_cb *callbacks) {
127   struct nl_sock *nl_sk;
128 
129   nl_sk = nl_socket_alloc();
130   if(!nl_sk)
131     goto cleanup;
132 
133   if(nl_connect(nl_sk, NETLINK_ROUTE) != 0)
134     goto cleanup;
135 
136   if(nl_send_auto_complete(nl_sk, msg) < 0)
137     goto cleanup;
138 
139   if(netlink_set_kernel_only(nl_sk) < 0)
140     goto cleanup;
141 
142   nl_recvmsgs(nl_sk, callbacks);
143 
144 cleanup:
145   if(nl_sk)
146     nl_socket_free(nl_sk);
147 }
148 
149 /* function: send_ifaddrmsg
150  * sends a netlink/ifaddrmsg message and hands the responses to the callbacks
151  * type      - netlink message type
152  * flags     - netlink message flags
153  * ifa       - ifaddrmsg to send
154  * callbacks - callbacks to use with the responses
155  */
send_ifaddrmsg(uint16_t type,uint16_t flags,struct ifaddrmsg * ifa,struct nl_cb * callbacks)156 void send_ifaddrmsg(uint16_t type, uint16_t flags, struct ifaddrmsg *ifa, struct nl_cb *callbacks) {
157   struct nl_msg *msg = NULL;
158 
159   msg = nlmsg_alloc_ifaddr(type, flags, ifa);
160   if(!msg)
161     return;
162 
163   send_netlink_msg(msg, callbacks);
164 
165   nlmsg_free(msg);
166 }
167 
168 /* function: netlink_sendrecv
169  * send a nl_msg and return an int status - only supports OK/ERROR responses
170  * msg - msg to send
171  */
netlink_sendrecv(struct nl_msg * msg)172 int netlink_sendrecv(struct nl_msg *msg) {
173   struct nl_cb *callbacks = NULL;
174   int retval = -EIO;
175 
176   callbacks = alloc_ack_callbacks(&retval);
177   if(!callbacks) {
178     return -ENOMEM;
179   }
180 
181   send_netlink_msg(msg, callbacks);
182 
183   nl_cb_put(callbacks);
184 
185   return retval;
186 }
187