blob: 1303e8ab0d9f0ec52ed81a8e83fd62ca32448e82 [file] [log] [blame]
Frank Makered6b39c2011-05-23 21:14:58 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
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
17/* NOTICE: This is a clean room re-implementation of libnl */
18
19#include <malloc.h>
20#include <unistd.h>
Elliott Hughesb77d3d72012-09-11 18:52:40 -070021#include <sys/socket.h>
Frank Makered6b39c2011-05-23 21:14:58 -070022#include <linux/netlink.h>
23#include "netlink-types.h"
24
25/* Allocate a new netlink message with the default maximum payload size. */
26struct nl_msg *nlmsg_alloc(void)
27{
28 /* Whole page will store nl_msg + nlmsghdr + genlmsghdr + payload */
29 const int page_sz = getpagesize();
30 struct nl_msg *nm;
31 struct nlmsghdr *nlh;
32
33 /* Netlink message */
34 nm = (struct nl_msg *) malloc(page_sz);
35 if (!nm)
36 goto fail;
37
38 /* Netlink message header pointer */
39 nlh = (struct nlmsghdr *) ((char *) nm + sizeof(struct nl_msg));
40
41 /* Initialize */
42 memset(nm, 0, page_sz);
43 nm->nm_size = page_sz;
44
45 nm->nm_src.nl_family = AF_NETLINK;
46 nm->nm_src.nl_pid = getpid();
47
48 nm->nm_dst.nl_family = AF_NETLINK;
49 nm->nm_dst.nl_pid = 0; /* Kernel */
50
51 /* Initialize and add to netlink message */
52 nlh->nlmsg_len = NLMSG_HDRLEN;
53 nm->nm_nlh = nlh;
54
55 /* Add to reference count and return nl_msg */
56 nlmsg_get(nm);
57 return nm;
58fail:
59 return NULL;
60}
61
62/* Return pointer to message payload. */
63void *nlmsg_data(const struct nlmsghdr *nlh)
64{
65 return (char *) nlh + NLMSG_HDRLEN;
66}
67
68/* Add reference count to nl_msg */
69void nlmsg_get(struct nl_msg *nm)
70{
71 nm->nm_refcnt++;
72}
73
74/* Release a reference from an netlink message. */
75void nlmsg_free(struct nl_msg *nm)
76{
77 if (nm) {
78 nm->nm_refcnt--;
79 if (nm->nm_refcnt <= 0)
80 free(nm);
81 }
82
83}
84
85/* Return actual netlink message. */
86struct nlmsghdr *nlmsg_hdr(struct nl_msg *n)
87{
88 return n->nm_nlh;
89}
90
91/* Return head of attributes data / payload section */
92struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, int hdrlen)
93{
94 unsigned char *data = nlmsg_data(nlh);
Dmitry Shmidt64f17e82011-08-10 15:25:51 -070095 return (struct nlattr *)(data + NLMSG_ALIGN(hdrlen));
Frank Makered6b39c2011-05-23 21:14:58 -070096}
97
98/* Returns pointer to end of netlink message */
99void *nlmsg_tail(const struct nlmsghdr *nlh)
100{
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700101 return (void *)((char *)nlh + NLMSG_ALIGN(nlh->nlmsg_len));
Frank Makered6b39c2011-05-23 21:14:58 -0700102}
103
104/* Next netlink message in message stream */
105struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
106{
107 struct nlmsghdr *next_nlh = NULL;
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700108 int len = nlmsg_len(nlh);
Frank Makered6b39c2011-05-23 21:14:58 -0700109
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700110 len = NLMSG_ALIGN(len);
111 if (*remaining > 0 &&
112 len <= *remaining &&
113 len >= (int) sizeof(struct nlmsghdr)) {
114 next_nlh = (struct nlmsghdr *)((char *)nlh + len);
115 *remaining -= len;
Frank Makered6b39c2011-05-23 21:14:58 -0700116 }
117
118 return next_nlh;
119}
120
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700121int nlmsg_datalen(const struct nlmsghdr *nlh)
122{
123 return nlh->nlmsg_len - NLMSG_HDRLEN;
124}
125
Frank Makered6b39c2011-05-23 21:14:58 -0700126/* Length of attributes data */
127int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
128{
Dmitry Shmidt64f17e82011-08-10 15:25:51 -0700129 return nlmsg_datalen(nlh) - NLMSG_ALIGN(hdrlen);
Frank Makered6b39c2011-05-23 21:14:58 -0700130}
131
132/* Length of netlink message */
133int nlmsg_len(const struct nlmsghdr *nlh)
134{
135 return nlh->nlmsg_len;
136}
137
138/* Check if the netlink message fits into the remaining bytes */
139int nlmsg_ok(const struct nlmsghdr *nlh, int rem)
140{
141 return rem >= (int)sizeof(struct nlmsghdr) &&
142 rem >= nlmsg_len(nlh) &&
143 nlmsg_len(nlh) >= (int) sizeof(struct nlmsghdr) &&
144 nlmsg_len(nlh) <= (rem);
145}
146
147int nlmsg_padlen(int payload)
148{
149 return NLMSG_ALIGN(payload) - payload;
150}