123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620 |
- #include "defs.h"
- #include <netlink-local.h>
- #include <netlink/netlink.h>
- #include <netlink/utils.h>
- #include <netlink/handlers.h>
- #include <netlink/msg.h>
- #include <netlink/attr.h>
- static int default_cb = NL_CB_DEFAULT;
- static void __init init_default_cb(void)
- {
- char *nlcb;
- if ((nlcb = getenv("NLCB"))) {
- if (!strcasecmp(nlcb, "default"))
- default_cb = NL_CB_DEFAULT;
- else if (!strcasecmp(nlcb, "verbose"))
- default_cb = NL_CB_VERBOSE;
- else if (!strcasecmp(nlcb, "debug"))
- default_cb = NL_CB_DEBUG;
- else {
- fprintf(stderr, "Unknown value for NLCB, valid values: "
- "{default | verbose | debug}\n");
- }
- }
- }
- static uint32_t used_ports_map[32];
- static NL_RW_LOCK(port_map_lock);
- static uint32_t generate_local_port(void)
- {
- int i, n;
- uint32_t pid = getpid() & 0x3FFFFF;
- nl_write_lock(&port_map_lock);
- for (i = 0; i < 32; i++) {
- if (used_ports_map[i] == 0xFFFFFFFF)
- continue;
- for (n = 0; n < 32; n++) {
- if (1UL & (used_ports_map[i] >> n))
- continue;
- used_ports_map[i] |= (1UL << n);
- n += (i * 32);
-
- nl_write_unlock(&port_map_lock);
- return pid + (n << 22);
- }
- }
- nl_write_unlock(&port_map_lock);
-
- return UINT_MAX;
- }
- static void release_local_port(uint32_t port)
- {
- int nr;
- if (port == UINT_MAX)
- return;
-
- nr = port >> 22;
- nl_write_lock(&port_map_lock);
- used_ports_map[nr / 32] &= ~(1 << (nr % 32));
- nl_write_unlock(&port_map_lock);
- }
- static struct nl_handle *__alloc_handle(struct nl_cb *cb)
- {
- struct nl_handle *handle;
- handle = calloc(1, sizeof(*handle));
- if (!handle) {
- nl_errno(ENOMEM);
- return NULL;
- }
- handle->h_fd = -1;
- handle->h_cb = nl_cb_get(cb);
- handle->h_local.nl_family = AF_NETLINK;
- handle->h_peer.nl_family = AF_NETLINK;
- handle->h_seq_expect = handle->h_seq_next = time(0);
- handle->h_local.nl_pid = generate_local_port();
- if (handle->h_local.nl_pid == UINT_MAX) {
- nl_handle_destroy(handle);
- nl_error(ENOBUFS, "Out of local ports");
- return NULL;
- }
- return handle;
- }
- struct nl_handle *nl_handle_alloc(void)
- {
- struct nl_cb *cb;
- struct nl_handle *sk;
- cb = nl_cb_alloc(default_cb);
- if (!cb) {
- nl_errno(ENOMEM);
- return NULL;
- }
-
- sk = __alloc_handle(cb);
- nl_cb_put(cb);
- return sk;
- }
- struct nl_handle *nl_handle_alloc_cb(struct nl_cb *cb)
- {
- if (cb == NULL)
- BUG();
- return __alloc_handle(cb);
- }
- void nl_handle_destroy(struct nl_handle *handle)
- {
- if (!handle)
- return;
- if (handle->h_fd >= 0)
- close(handle->h_fd);
- if (!(handle->h_flags & NL_OWN_PORT))
- release_local_port(handle->h_local.nl_pid);
- nl_cb_put(handle->h_cb);
- free(handle);
- }
- static int noop_seq_check(struct nl_msg *msg, void *arg)
- {
- return NL_OK;
- }
- void nl_disable_sequence_check(struct nl_handle *handle)
- {
- nl_cb_set(handle->h_cb, NL_CB_SEQ_CHECK,
- NL_CB_CUSTOM, noop_seq_check, NULL);
- }
- unsigned int nl_socket_use_seq(struct nl_handle *handle)
- {
- return handle->h_seq_next++;
- }
- uint32_t nl_socket_get_local_port(struct nl_handle *handle)
- {
- return handle->h_local.nl_pid;
- }
- void nl_socket_set_local_port(struct nl_handle *handle, uint32_t port)
- {
- if (port == 0) {
- port = generate_local_port();
- handle->h_flags &= ~NL_OWN_PORT;
- } else {
- if (!(handle->h_flags & NL_OWN_PORT))
- release_local_port(handle->h_local.nl_pid);
- handle->h_flags |= NL_OWN_PORT;
- }
- handle->h_local.nl_pid = port;
- }
- int nl_socket_add_membership(struct nl_handle *handle, int group)
- {
- int err;
- if (handle->h_fd == -1)
- return nl_error(EBADFD, "Socket not connected");
- err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
- &group, sizeof(group));
- if (err < 0)
- return nl_error(errno, "setsockopt(NETLINK_ADD_MEMBERSHIP) "
- "failed");
- return 0;
- }
- int nl_socket_drop_membership(struct nl_handle *handle, int group)
- {
- int err;
- if (handle->h_fd == -1)
- return nl_error(EBADFD, "Socket not connected");
- err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
- &group, sizeof(group));
- if (err < 0)
- return nl_error(errno, "setsockopt(NETLINK_DROP_MEMBERSHIP) "
- "failed");
- return 0;
- }
- void nl_join_groups(struct nl_handle *handle, int groups)
- {
- handle->h_local.nl_groups |= groups;
- }
- uint32_t nl_socket_get_peer_port(struct nl_handle *handle)
- {
- return handle->h_peer.nl_pid;
- }
- void nl_socket_set_peer_port(struct nl_handle *handle, uint32_t port)
- {
- handle->h_peer.nl_pid = port;
- }
- int nl_socket_get_fd(struct nl_handle *handle)
- {
- return handle->h_fd;
- }
- int nl_socket_set_nonblocking(struct nl_handle *handle)
- {
- if (handle->h_fd == -1)
- return nl_error(EBADFD, "Socket not connected");
- if (fcntl(handle->h_fd, F_SETFL, O_NONBLOCK) < 0)
- return nl_error(errno, "fcntl(F_SETFL, O_NONBLOCK) failed");
- return 0;
- }
- void nl_socket_enable_msg_peek(struct nl_handle *handle)
- {
- handle->h_flags |= NL_MSG_PEEK;
- }
- void nl_socket_disable_msg_peek(struct nl_handle *handle)
- {
- handle->h_flags &= ~NL_MSG_PEEK;
- }
- struct nl_cb *nl_socket_get_cb(struct nl_handle *handle)
- {
- return nl_cb_get(handle->h_cb);
- }
- void nl_socket_set_cb(struct nl_handle *handle, struct nl_cb *cb)
- {
- if (cb == NULL)
- BUG();
- nl_cb_put(handle->h_cb);
- handle->h_cb = nl_cb_get(cb);
- }
- int nl_socket_modify_cb(struct nl_handle *handle, enum nl_cb_type type,
- enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func,
- void *arg)
- {
- return nl_cb_set(handle->h_cb, type, kind, func, arg);
- }
- int nl_set_buffer_size(struct nl_handle *handle, int rxbuf, int txbuf)
- {
- int err;
- if (rxbuf <= 0)
- rxbuf = 32768;
- if (txbuf <= 0)
- txbuf = 32768;
- if (handle->h_fd == -1)
- return nl_error(EBADFD, "Socket not connected");
-
- err = setsockopt(handle->h_fd, SOL_SOCKET, SO_SNDBUF,
- &txbuf, sizeof(txbuf));
- if (err < 0)
- return nl_error(errno, "setsockopt(SO_SNDBUF) failed");
- err = setsockopt(handle->h_fd, SOL_SOCKET, SO_RCVBUF,
- &rxbuf, sizeof(rxbuf));
- if (err < 0)
- return nl_error(errno, "setsockopt(SO_RCVBUF) failed");
- handle->h_flags |= NL_SOCK_BUFSIZE_SET;
- return 0;
- }
- int nl_set_passcred(struct nl_handle *handle, int state)
- {
- int err;
- if (handle->h_fd == -1)
- return nl_error(EBADFD, "Socket not connected");
- err = setsockopt(handle->h_fd, SOL_SOCKET, SO_PASSCRED,
- &state, sizeof(state));
- if (err < 0)
- return nl_error(errno, "setsockopt(SO_PASSCRED) failed");
- if (state)
- handle->h_flags |= NL_SOCK_PASSCRED;
- else
- handle->h_flags &= ~NL_SOCK_PASSCRED;
- return 0;
- }
- int nl_socket_recv_pktinfo(struct nl_handle *handle, int state)
- {
- int err;
- if (handle->h_fd == -1)
- return nl_error(EBADFD, "Socket not connected");
- err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_PKTINFO,
- &state, sizeof(state));
- if (err < 0)
- return nl_error(errno, "setsockopt(NETLINK_PKTINFO) failed");
- return 0;
- }
|