8000 minix/minix/net/lwip/rtsock.c at master · devinblood/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1912 lines (1557 loc) · 50.1 KB

rtsock.c

File metadata and controls

1912 lines (1557 loc) · 50.1 KB
}
/*
* Send the routing message in 'pbuf' to the given routing socket if possible,
* or check whether such a message could be sent to that socket if 'pbuf' is
* NULL. In the former case, the function takes ownership of 'pbuf'. The
* given routing socket is assumed to be the source of the routing request that
* generated this message. In the latter case, the function returns TRUE if
* the socket would take the message or FALSE if not. If 'family' is not
* AF_UNSPEC, it is to be the address family of the message.
*/
static int
rtsock_msg_one(struct rtsock * rt, int family, struct pbuf * pbuf)
{
if (rtsock_can_send(rt, rt, family)) {
if (pbuf != NULL)
rtsock_enqueue(rt, pbuf);
return TRUE;
} else {
if (pbuf != NULL)
pbuf_free(pbuf);
return FALSE;
}
}
/*
* Send the routing message in 'pbuf' to all matching routing sockets, or check
* whether there are any such matching routing sockets if 'pbuf' is NULL. In
* the former case, the function takes ownership of 'pbuf'. In the latter
* case, the function returns TRUE if there are any matching sockets or FALSE
* if there are none. If 'rtsrc' is not NULL, it is to be the routing socket
* that is the source of the message. If 'family' is not AF_UNSPEC, it is to
* be the address family of the message.
*/
static int
rtsock_msg_match(struct rtsock * rtsrc, int family, struct pbuf * pbuf)
{
struct rtsock *rt, *rtprev;
struct pbuf *pcopy;
rtprev = NULL;
TAILQ_FOREACH(rt, &rt_activelist, rt_next) {
if (!rtsock_can_send(rt, rtsrc, family))
continue;
/*
* There is at least one routing socket that is interested in
* receiving this message, and able to receive it.
*/
if (pbuf == NULL)
return TRUE;
/*
* We need to make copies of the generated message for all but
* the last matching socket, which gets the original. If we're
* out of memory, free the original and stop: there are more
* important things to spend memory on than routing sockets.
*/
if (rtprev != NULL) {
if ((pcopy = rtsock_alloc(pbuf->tot_len)) == NULL) {
pbuf_free(pbuf);
return TRUE;
}
if (pbuf_copy(pcopy, pbuf) != ERR_OK)
panic("unexpected pbuf copy failure");
rtsock_enqueue(rtprev, pcopy);
}
rtprev = rt;
}
if (rtprev != NULL)
rtsock_enqueue(rtprev, pbuf);
else if (pbuf != NULL)
pbuf_free(pbuf);
return (rtprev != NULL);
}
/*
* Dequeue and free the head of the receive queue of a routing socket.
*/
static void
rtsock_dequeue(struct rtsock * rt)
{
struct pbuf *pbuf, **pnext;
size_t size;
pbuf = rt->rt_rcvhead;
assert(pbuf != NULL);
pnext = pchain_end(pbuf);
size = pchain_size(pbuf);
if ((rt->rt_rcvhead = *pnext) == NULL)
rt->rt_rcvtailp = &rt->rt_rcvhead;
assert(rt->rt_rcvlen >= size);
rt->rt_rcvlen -= size;
*pnext = NULL;
pbuf_free(pbuf);
}
/*
* Process a routing message sent on a socket. Return OK on success, in which
* case the caller assumes that the processing routine has sent a reply to the
* user and possibly other routing sockets. Return a negative error code on
* failure, in which case the caller will send the reply to the user instead.
*/
static int
rtsock_process(struct rtsock *rt, struct rt_msghdr * rtm, char * buf,
size_t len, int is_root)
{
struct rtsock_request rtr;
struct rtsock_rta rta;
const struct sockaddr *netmask;
struct sockaddr sa;
union sockaddr_any mask;
size_t off;
int i;
if (rtm->rtm_msglen != len)
return EINVAL;
if (rtm->rtm_version != RTM_VERSION) {
printf("LWIP: PID %d uses routing sockets version %u\n",
rtm->rtm_pid, rtm->rtm_version);
return EPROTONOSUPPORT;
}
/*
* Make sure that we won't misinterpret the rest of the message. While
* looking at the message type, also make sure non-root users can only
* ever issue RTM_GET requests.
*/
switch (rtm->rtm_type) {
case RTM_ADD:
case RTM_DELETE:
case RTM_CHANGE:
case RTM_LOCK:
if (!is_root)
return EPERM;
/* FALLTHROUGH */
case RTM_GET:
break;
default:
return EOPNOTSUPP;
}
/*
* Extract all given addresses. We do not actually support all types
* of entries, but we cannot skip the ones we do not need either.
*/
rtsock_rta_init(&rta);
off = sizeof(*rtm);
assert(off == RT_ROUNDUP(off));
for (i = 0; i < RTAX_MAX; i++) {
if (!(rtm->rtm_addrs & (1 << i)))
continue;
if (off + offsetof(struct sockaddr, sa_data) > len)
return EINVAL;
/*
* It is safe to access sa_len and even sa_family in all cases,
* in particular even when the structure is of size zero.
*/
assert(offsetof(struct sockaddr, sa_data) <= RT_ROUNDUP(0));
memcpy(&sa, &buf[off], offsetof(struct sockaddr, sa_data));
if (off + sa.sa_len > len)
return EINVAL;
rtsock_rta_set(&rta, i, &buf[off], sa.sa_len);
off += RT_ROUNDUP((size_t)sa.sa_len);
}
/*
* Expand the given netmask if it is in compressed IPv4 form. We do
* this here because it is particular to routing sockets; we also do
* the compression in this module. Note how the compression may even
* strip off the address family; really, who came up with this ****?
*/
netmask = (const struct sockaddr *)rta.rta_ptr[RTAX_NETMASK];
if (netmask != NULL) {
if (!rtsock_expand_netmask(&mask, netmask))
return EINVAL;
rtsock_rta_set(&rta, RTAX_NETMASK, &mask, mask.sa.sa_len);
}
/*
* Actually process the command. Pass on enough information so that a
* reply can be generated on success. The abstraction as sketched at
* the top of the file imposes that we pass quite a few parameters.
*/
rtr.rtr_src = rt;
rtr.rtr_pid = rtm->rtm_pid;
rtr.rtr_seq = rtm->rtm_seq;
rtr.rtr_getif = (rtm->rtm_type == RTM_GET &&
(rta.rta_ptr[RTAX_IFP] != NULL || rta.rta_ptr[RTAX_IFA] != NULL));
return route_process(rtm->rtm_type,
(const struct sockaddr *)rta.rta_ptr[RTAX_DST],
(const struct sockaddr *)rta.rta_ptr[RTAX_NETMASK],
(const struct sockaddr *)rta.rta_ptr[RTAX_GATEWAY],
(const struct sockaddr *)rta.rta_ptr[RTAX_IFP],
(const struct sockaddr *)rta.rta_ptr[RTAX_IFA],
rtm->rtm_flags, rtm->rtm_inits, &rtm->rtm_rmx, &rtr);
}
/*
* Perform preliminary checks on a send request.
*/
static int
rtsock_pre_send(struct sock * sock __unused, size_t len,
socklen_t ctl_len __unused, const struct sockaddr * addr,
socklen_t addr_len __unused, endpoint_t user_endpt __unused, int flags)
{
if (flags != 0)
return EOPNOTSUPP;
if (addr != NULL)
return EISCONN;
/*
* For the most basic failures - that is, we cannot even manage to
* receive the request - we do not generate a reply message.
*/
if (len < sizeof(struct rt_msghdr))
return ENOBUFS;
if (len > RT_SNDBUF_MAX)
return EMSGSIZE;
return OK;
}
/*
* Send data on a routing socket.
*/
static int
rtsock_send(struct sock * sock, const struct sockdriver_data * data,
size_t len, size_t * offp, const struct sockdriver_data * ctl __unused,
socklen_t ctl_len __unused, socklen_t * ctl_off __unused,
const struct sockaddr * addr __unused, socklen_t addr_len __unused,
endpoint_t user_endpt, int flags __unused, size_t min __unused)
{
struct rtsock *rt = (struct rtsock *)sock;
char buf[RT_SNDBUF_MAX] __aligned(4);
struct rt_msghdr rtm;
struct pbuf *pbuf;
uid_t euid;
int r, is_root;
/* Copy in the request, and adjust some fields right away. */
assert(len >= sizeof(rtm));
assert(len <= sizeof(buf));
if ((r = sockdriver_copyin(data, 0, buf, len)) != OK)
return r;
memcpy(&rtm, buf, sizeof(rtm));
rtm.rtm_errno = 0;
rtm.rtm_flags &= ~RTF_DONE;
rtm.rtm_pid = getepinfo(user_endpt, &euid, NULL /*gid*/);
is_root = (euid == ROOT_EUID);
/* Process the request. */
r = rtsock_process(rt, &rtm, buf, len, is_root);
/*
* If the request has been processed successfully, a reply has been
* sent already, possibly also to other routing sockets. Here, we
* handle the case that the request has resulted in failure, in which
* case we send a reply to the caller only. This behavior is different
* from the traditional BSD behavior, which also sends failure replies
* to other sockets. Our motivation is that while other parties are
* never going to be interested in failures anyway, it is in fact easy
* for an unprivileged user process to abuse the failure-reply system
* in order to fake other types of routing messages (e.g., RTM_IFINFO)
* to other parties. By sending failure replies only to the requestor,
* we eliminate the need for security-sensitive request validation.
*/
if (r != OK && rtsock_can_send(rt, rt, AF_UNSPEC)) {
rtm.rtm_errno = -r;
if ((pbuf = rtsock_alloc(len)) == NULL)
return ENOMEM;
/* For the reply, reuse the request message largely as is. */
memcpy(pbuf->payload, &rtm, sizeof(rtm));
if (len > sizeof(rtm))
memcpy((uint8_t *)pbuf->payload + sizeof(rtm),
buf + sizeof(rtm), len - sizeof(rtm));
rtsock_enqueue(rt, pbuf);
} else if (r == OK)
*offp = len;
return r;
}
/*
* Perform preliminary checks on a receive request.
*/
static int
rtsock_pre_recv(struct sock * sock __unused, endpoint_t user_endpt __unused,
int flags)
{
/*
* We accept the same flags across all socket types in LWIP, and then
* simply ignore the ones we do not support for routing sockets.
*/
if ((flags & ~(MSG_PEEK | MSG_WAITALL)) != 0)
return EOPNOTSUPP;
return OK;
}
/*
* Receive data on a routing socket.
*/
static int
rtsock_recv(struct sock * sock, const struct sockdriver_data * data,
size_t len, size_t * off, const struct sockdriver_data * ctl __unused,
socklen_t ctl_len __unused, socklen_t * ctl_off __unused,
struct sockaddr * addr, socklen_t * addr_len,
endpoint_t user_endpt __unused, int flags, size_t min __unused,
int * rflags)
{
struct rtsock *rt = (struct rtsock *)sock;
struct pbuf *pbuf;
int r;
if ((pbuf = rt->rt_rcvhead) == NULL)
return SUSPEND;
/* Copy out the data to the calling user process. */
if (len >= pbuf->tot_len)
len = pbuf->tot_len;
else
*rflags |= MSG_TRUNC;
r = util_copy_data(data, len, 0, pbuf, 0, FALSE /*copy_in*/);
if (r != OK)
return r;
/* Generate a dummy source address. */
addr->sa_len = RTSOCK_ADDR_LEN;
addr->sa_family = AF_ROUTE;
*addr_len = RTSOCK_ADDR_LEN;
/* Discard the data now, unless we were instructed to peek only. */
if (!(flags & MSG_PEEK))
rtsock_dequeue(rt);
/* Return the received part of the data length. */
*off = len;
return OK;
}
/*
* Test whether data can be received on a routing socket, and if so, how many
* bytes of data.
*/
static int
rtsock_test_recv(struct sock * sock, size_t min __unused, size_t * size)
{
struct rtsock *rt = (struct rtsock *)sock;
if (rt->rt_rcvhead == NULL)
return SUSPEND;
if (size != NULL)
*size = rt->rt_rcvhead->tot_len;
return OK;
}
/*
* Set socket options on a routing socket.
*/
static int
rtsock_setsockopt(struct sock * sock, int level, int name,
const struct sockdriver_data * data, socklen_t len)
{
struct rtsock *rt = (struct rtsock *)sock;
int r, val;
if (level == SOL_SOCKET) {
switch (name) {
case SO_USELOOPBACK:
if ((r = sockdriver_copyin_opt(data, &val, sizeof(val),
len)) != OK)
return r;
if (!val)
rt->rt_flags |= RTF_NOLOOPBACK;
else
rt->rt_flags &= ~RTF_NOLOOPBACK;
return OK;
case SO_RCVBUF:
if ((r = sockdriver_copyin_opt(data, &val, sizeof(val),
len)) != OK)
return r;
if (val < RT_RCVBUF_MIN || val > RT_RCVBUF_MAX)
return EINVAL;
rt->rt_rcvbuf = (size_t)val;
return OK;
}
}
return ENOPROTOOPT;
}
/*
* Retrieve socket options on a routing socket.
*/
static int
rtsock_getsockopt(struct sock * sock, int level, int name,
const struct sockdriver_data * data, socklen_t * len)
{
struct rtsock *rt = (struct rtsock *)sock;
int val;
if (level == SOL_SOCKET) {
switch (name) {
case SO_USELOOPBACK:
val = !(rt->rt_flags & RTF_NOLOOPBACK);
return sockdriver_copyout_opt(data, &val, sizeof(val),
len);
case SO_RCVBUF:
val = rt->rt_rcvbuf;
return sockdriver_copyout_opt(data, &val, sizeof(val),
len);
}
}
return ENOPROTOOPT;
}
/*
* Retrieve the local or remote socket address of a routing socket.
*/
static int
rtsock_getname(struct sock * sock __unused, struct sockaddr * addr,
socklen_t * addr_len)
{
/* This is entirely useless but apparently common between OSes. */
addr->sa_len = RTSOCK_ADDR_LEN;
addr->sa_family = AF_ROUTE;
*addr_len = RTSOCK_ADDR_LEN;
return OK;
}
/*
* Drain the receive queue of a routing socket.
*/
static void
rtsock_drain(struct rtsock * rt)
{
while (rt->rt_rcvhead != NULL)
rtsock_dequeue(rt);
}
/*
* Shut down a routing socket for reading and/or writing.
*/
static int
rtsock_shutdown(struct sock * sock, unsigned int mask)
{
struct rtsock *rt = (struct rtsock *)sock;
if (mask & SFL_SHUT_RD)
rtsock_drain(rt);
return OK;
}
/*
* Close a routing socket.
*/
static int
rtsock_close(struct sock * sock, int force __unused)
{
struct rtsock *rt = (struct rtsock *)sock;
rtsock_drain(rt);
return OK;
}
/*
* Free up a closed routing socket.
*/
static void
rtsock_free(struct sock * sock)
{
struct rtsock *rt = (struct rtsock *)sock;
TAILQ_REMOVE(&rt_activelist, rt, rt_next);
TAILQ_INSERT_HEAD(&rt_freelist, rt, rt_next);
}
static const struct sockevent_ops rtsock_ops = {
.sop_pre_send = rtsock_pre_send,
.sop_send = rtsock_send,
.sop_pre_recv = rtsock_pre_recv,
.sop_recv = rtsock_recv,
.sop_test_recv = rtsock_test_recv,
.sop_setsockopt = rtsock_setsockopt,
.sop_getsockopt = rtsock_getsockopt,
.sop_getsockname = rtsock_getname,
.sop_getpeername = rtsock_getname,
.sop_shutdown = rtsock_shutdown,
.sop_close = rtsock_close,
.sop_free = rtsock_free
};
/*
* Send an interface announcement message about the given interface. If
* 'arrival' is set, the interface has just been created; otherwise, the
* interface is about to be destroyed.
*/
void
rtsock_msg_ifannounce(struct ifdev * ifdev, int arrival)
{
struct if_announcemsghdr ifan;
struct pbuf *pbuf;
if (!rtsock_msg_match(NULL /*rtsrc*/, AF_UNSPEC, NULL /*pbuf*/))
return;
memset(&ifan, 0, sizeof(ifan));
ifan.ifan_msglen = sizeof(ifan);
ifan.ifan_version = RTM_VERSION;
ifan.ifan_type = RTM_IFANNOUNCE;
ifan.ifan_index = ifdev_get_index(ifdev);
strlcpy(ifan.ifan_name, ifdev_get_name(ifdev), sizeof(ifan.ifan_name));
ifan.ifan_what = (arrival) ? IFAN_ARRIVAL : IFAN_DEPARTURE;
if ((pbuf = rtsock_alloc(sizeof(ifan))) == NULL)
return;
memcpy(pbuf->payload, &ifan, sizeof(ifan));
rtsock_msg_match(NULL /*rtsrc*/, AF_UNSPEC, pbuf);
}
/*
* Send an interface information routing message.
*/
void
rtsock_msg_ifinfo(struct ifdev * ifdev)
{
struct if_msghdr ifm;
struct pbuf *pbuf;
if (!rtsock_msg_match(NULL /*rtsrc*/, AF_UNSPEC, NULL /*pbuf*/))
return;
memset(&ifm, 0, sizeof(ifm));
ifm.ifm_msglen = sizeof(ifm);
ifm.ifm_version = RTM_VERSION;
ifm.ifm_type = RTM_IFINFO;
ifm.ifm_addrs = 0;
ifm.ifm_flags = ifdev_get_ifflags(ifdev);
ifm.ifm_index = ifdev_get_index(ifdev);
0