Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame^] | 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | */ |
| 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: send_netlink_msg |
| 107 | * sends a netlink message, reads a response, and hands the response(s) to the callbacks |
| 108 | * msg - netlink message to send |
| 109 | * callbacks - callbacks to use on responses |
| 110 | */ |
| 111 | void send_netlink_msg(struct nl_msg *msg, struct nl_cb *callbacks) { |
| 112 | struct nl_sock *nl_sk; |
| 113 | |
| 114 | nl_sk = nl_socket_alloc(); |
| 115 | if(!nl_sk) |
| 116 | goto cleanup; |
| 117 | |
| 118 | if(nl_connect(nl_sk, NETLINK_ROUTE) != 0) |
| 119 | goto cleanup; |
| 120 | |
| 121 | if(nl_send_auto_complete(nl_sk, msg) < 0) |
| 122 | goto cleanup; |
| 123 | |
| 124 | nl_recvmsgs(nl_sk, callbacks); |
| 125 | |
| 126 | cleanup: |
| 127 | if(nl_sk) |
| 128 | nl_socket_free(nl_sk); |
| 129 | } |
| 130 | |
| 131 | /* function: send_ifaddrmsg |
| 132 | * sends a netlink/ifaddrmsg message and hands the responses to the callbacks |
| 133 | * type - netlink message type |
| 134 | * flags - netlink message flags |
| 135 | * ifa - ifaddrmsg to send |
| 136 | * callbacks - callbacks to use with the responses |
| 137 | */ |
| 138 | void send_ifaddrmsg(uint16_t type, uint16_t flags, struct ifaddrmsg *ifa, struct nl_cb *callbacks) { |
| 139 | struct nl_msg *msg = NULL; |
| 140 | |
| 141 | msg = nlmsg_alloc_ifaddr(type, flags, ifa); |
| 142 | if(!msg) |
| 143 | return; |
| 144 | |
| 145 | send_netlink_msg(msg, callbacks); |
| 146 | |
| 147 | nlmsg_free(msg); |
| 148 | } |
| 149 | |
| 150 | /* function: netlink_sendrecv |
| 151 | * send a nl_msg and return an int status - only supports OK/ERROR responses |
| 152 | * msg - msg to send |
| 153 | */ |
| 154 | int netlink_sendrecv(struct nl_msg *msg) { |
| 155 | struct nl_cb *callbacks = NULL; |
| 156 | int retval = -EIO; |
| 157 | |
| 158 | callbacks = alloc_ack_callbacks(&retval); |
| 159 | if(!callbacks) { |
| 160 | return -ENOMEM; |
| 161 | } |
| 162 | |
| 163 | send_netlink_msg(msg, callbacks); |
| 164 | |
| 165 | nl_cb_put(callbacks); |
| 166 | |
| 167 | return retval; |
| 168 | } |