From: Sjur Brændeland
Add file rtnl.c for creating and deleting CAIF network
interfaces using the RTNL protocol. The interface is
asynchronous.
Only RTNL NEWLINK and DELLINK commands are implemented.
CAIF requires NEWLINK to contain channel-id and ip-type
(ipv4/ipv6) as arguments.
---
drivers/stemodem/rtnl.c | 365 +++
1 files changed, 365 insertions(+), 0 deletions(-)
create mode 100644 drivers/stemodem/rtnl.c
diff --git a/drivers/stemodem/rtnl.c b/drivers/stemodem/rtnl.c
new file mode 100644
index 000..38f7f43
--- /dev/null
+++ b/drivers/stemodem/rtnl.c
@@ -0,0 +1,365 @@
+/*
+ *
+ * oFono - Open Source Telephony
+ *
+ * Copyright (C) 2010 ST-Ericsson AB.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include
+#endif
+
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include
+
+#include
+
+#include "if_caif.h"
+#include "rtnl.h"
+
+#define NLMSG_TAIL(nmsg) \
+ ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
+
+struct iplink_req {
+ struct nlmsghdr n;
+ struct ifinfomsg i;
+ char pad[1024];
+ int request_id;
+ struct rtnl_req_param req;
+ struct rtnl_rsp_param rsp;
+};
+
+static GSList *pending_requests;
+static GIOChannel *channel;
+static guint32 rtnl_seqnr;
+static guint rtnl_watch;
+
+static gboolean get_ifname(struct ifinfomsg *msg, int bytes,
+ const char **ifname)
+{
+ struct rtattr *attr;
+
+ for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
+ attr = RTA_NEXT(attr, bytes))
+
+ if (attr->rta_type == IFLA_IFNAME &&
+ ifname != NULL) {
+ *ifname = RTA_DATA(attr);
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static void store_newlink_param(struct iplink_req *req, unsigned short type,
+ int index, unsigned flags,
+ unsigned change, struct ifinfomsg *msg,
+ int bytes)
+{
+ const char *ifname = NULL;
+
+ get_ifname(msg, bytes, &ifname);
+ strncpy(req->rsp.ifname, ifname,
+ sizeof(req->rsp.ifname));
+ req->rsp.ifname[sizeof(req->rsp.ifname)-1] = '\0';
+ req->rsp.ifindex = index;
+}
+
+static int send_rtnl_req(struct iplink_req *req)
+{
+ struct sockaddr_nl addr;
+ int sk, ret;
+
+ sk = g_io_channel_unix_get_fd(channel);
+
+ memset(&addr, 0, sizeof(addr));
+ addr.nl_family = AF_NETLINK;
+
+ ret = sendto(sk, req, req->n.nlmsg_len, 0,
+ (struct sockaddr *) &addr, sizeof(addr));
+ if (ret < 0)
+ return ret;
+ return 0;
+}
+
+static struct iplink_req *find_request(guint32 seq)
+{
+ GSList *list;
+
+ for (list = pending_requests; list; list = list->next) {
+ struct iplink_req *req = list->data;
+
+ if (req->n.nlmsg_seq == seq)
+ return req;
+ }
+
+ return NULL;
+}
+
+static void rtnl_client_response(struct iplink_req *req)
+{
+
+ if (req->req.callback && req->n.nlmsg_type == RTM_NEWLINK)
+ req->req.callback(&req->rsp);
+
+ pending_requests = g_slist_remove(pending_requests, req);
+ g_free(req);
+}
+
+static void parse_rtnl_message(void *buf, size_t len)
+{
+ struct ifinfomsg *msg;
+ struct iplink_req *req;
+
+ while (len > 0) {
+ struct nlmsghdr *hdr = buf;
+ if (!NLMSG_OK(hdr, len))
+ break;
+ if (hdr->nlmsg_type == RTM_NEWLINK) {
+ req = g_slist_nth_data(pending_requests, 0);
+ DBG("NEWLINK req:%p\n", req);
+ if (req == NULL)
+ break;
+ msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
+ store_newlink_param(req, msg->ifi_type,
+ msg->ifi_index, msg->ifi_flags,
+ msg->ifi_change, msg,
+ IFA_PAYLOAD(hdr));
+ rtnl_client_response(req);
+