2011-04-07 09:44:11 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2011-09-08 15:57:03 +02:00
|
|
|
#include <sys/ioc_net.h>
|
2011-04-07 09:44:11 +02:00
|
|
|
#include <net/gen/in.h>
|
|
|
|
#include <net/gen/ip_io.h>
|
|
|
|
|
|
|
|
#include <lwip/raw.h>
|
|
|
|
#include <lwip/ip_addr.h>
|
|
|
|
|
2012-02-24 19:02:32 +01:00
|
|
|
#include <minix/netsock.h>
|
2011-04-07 09:44:11 +02:00
|
|
|
#include "proto.h"
|
|
|
|
|
|
|
|
#define RAW_IP_BUF_SIZE (32 << 10)
|
|
|
|
|
|
|
|
#define sock_alloc_buf(s) debug_malloc(s)
|
|
|
|
#define sock_free_buf(x) debug_free(x)
|
|
|
|
|
|
|
|
struct raw_ip_recv_data {
|
|
|
|
ip_addr_t ip;
|
|
|
|
struct pbuf * pbuf;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define raw_ip_recv_alloc() debug_malloc(sizeof(struct raw_ip_recv_data))
|
|
|
|
|
|
|
|
static void raw_ip_recv_free(void * data)
|
|
|
|
{
|
|
|
|
if (((struct raw_ip_recv_data *)data)->pbuf)
|
|
|
|
pbuf_free(((struct raw_ip_recv_data *)data)->pbuf);
|
|
|
|
debug_free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
static int raw_ip_op_open(struct socket * sock)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
|
|
|
debug_print("socket num %ld", get_sock_num(sock));
|
|
|
|
|
|
|
|
if (!(sock->buf = sock_alloc_buf(RAW_IP_BUF_SIZE))) {
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
sock->buf_size = RAW_IP_BUF_SIZE;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void raw_ip_close(struct socket * sock)
|
|
|
|
{
|
|
|
|
/* deque and free all enqueued data before closing */
|
|
|
|
sock_dequeue_data_all(sock, raw_ip_recv_free);
|
|
|
|
|
|
|
|
if (sock->pcb)
|
|
|
|
raw_remove(sock->pcb);
|
|
|
|
if (sock->buf)
|
|
|
|
sock_free_buf(sock->buf);
|
|
|
|
|
|
|
|
/* mark it as unused */
|
|
|
|
sock->ops = NULL;
|
|
|
|
}
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
static int raw_ip_op_close(struct socket * sock)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
|
|
|
debug_print("socket num %ld", get_sock_num(sock));
|
|
|
|
|
|
|
|
raw_ip_close(sock);
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
return OK;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
static int raw_ip_do_receive(struct sock_req *req,
|
2011-04-07 09:44:11 +02:00
|
|
|
struct pbuf *pbuf)
|
|
|
|
{
|
|
|
|
struct pbuf * p;
|
2013-09-11 00:50:36 +02:00
|
|
|
size_t rem_len = req->size;
|
2013-04-11 22:37:30 +02:00
|
|
|
unsigned int written = 0, hdr_sz = 0;
|
2011-04-07 09:44:11 +02:00
|
|
|
int err;
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
debug_print("user buffer size : %u\n", rem_len);
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
for (p = pbuf; p && rem_len; p = p->next) {
|
|
|
|
size_t cp_len;
|
|
|
|
|
|
|
|
cp_len = (rem_len < p->len) ? rem_len : p->len;
|
2013-09-11 00:50:36 +02:00
|
|
|
err = copy_to_user(req->endpt, p->payload, cp_len, req->grant,
|
2011-04-07 09:44:11 +02:00
|
|
|
hdr_sz + written);
|
|
|
|
|
|
|
|
if (err != OK)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
written += cp_len;
|
|
|
|
rem_len -= cp_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_print("copied %d bytes\n", written + hdr_sz);
|
|
|
|
return written + hdr_sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u8_t raw_ip_op_receive(void *arg,
|
|
|
|
__unused struct raw_pcb *pcb,
|
|
|
|
struct pbuf *pbuf,
|
|
|
|
ip_addr_t *addr)
|
|
|
|
{
|
|
|
|
struct socket * sock = (struct socket *) arg;
|
|
|
|
struct raw_ip_recv_data * data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
debug_print("socket num : %ld addr : %x\n",
|
|
|
|
get_sock_num(sock), (unsigned int) addr->addr);
|
|
|
|
|
|
|
|
if (sock->flags & SOCK_FLG_OP_PENDING) {
|
|
|
|
/* we are resuming a suspended operation */
|
2013-09-11 00:50:36 +02:00
|
|
|
ret = raw_ip_do_receive(&sock->req, pbuf);
|
|
|
|
|
|
|
|
send_req_reply(&sock->req, ret);
|
|
|
|
sock->flags &= ~SOCK_FLG_OP_PENDING;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
if (ret > 0) {
|
|
|
|
if (sock->usr_flags & NWIO_EXCL) {
|
|
|
|
pbuf_free(pbuf);
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do not enqueue more data than allowed */
|
|
|
|
if (sock->recv_data_size > RAW_IP_BUF_SIZE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nobody is waiting for the data or an error occured above, we enqueue
|
|
|
|
* the packet
|
|
|
|
*/
|
|
|
|
if (!(data = raw_ip_recv_alloc())) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->ip = *addr;
|
|
|
|
if (sock->usr_flags & NWIO_EXCL) {
|
|
|
|
data->pbuf = pbuf;
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
|
|
|
/* we store a copy of this packet */
|
|
|
|
data->pbuf = pbuf_alloc(PBUF_RAW, pbuf->tot_len, PBUF_RAM);
|
|
|
|
if (data->pbuf == NULL) {
|
|
|
|
debug_print("LWIP : cannot allocated new pbuf\n");
|
|
|
|
raw_ip_recv_free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pbuf_copy(data->pbuf, pbuf) != ERR_OK) {
|
|
|
|
debug_print("LWIP : cannot copy pbuf\n");
|
|
|
|
raw_ip_recv_free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we didn't managed to enqueue the packet we report it as not
|
|
|
|
* consumed
|
|
|
|
*/
|
|
|
|
if (sock_enqueue_data(sock, data, data->pbuf->tot_len) != OK) {
|
|
|
|
raw_ip_recv_free(data);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
static int raw_ip_op_read(struct socket * sock, struct sock_req * req, int blk)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
|
|
|
debug_print("socket num %ld", get_sock_num(sock));
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
if (sock->pcb == NULL)
|
|
|
|
return EIO;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
if (sock->recv_head) {
|
|
|
|
/* data available receive immeditely */
|
|
|
|
|
|
|
|
struct raw_ip_recv_data * data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
data = (struct raw_ip_recv_data *) sock->recv_head->data;
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
ret = raw_ip_do_receive(req, data->pbuf);
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
if (ret > 0) {
|
|
|
|
sock_dequeue_data(sock);
|
|
|
|
sock->recv_data_size -= data->pbuf->tot_len;
|
|
|
|
raw_ip_recv_free(data);
|
|
|
|
}
|
2013-09-11 00:50:36 +02:00
|
|
|
return ret;
|
2012-02-24 19:02:32 +01:00
|
|
|
} else if (!blk)
|
2013-09-11 00:50:36 +02:00
|
|
|
return EAGAIN;
|
2012-02-24 19:02:32 +01:00
|
|
|
else {
|
2013-09-11 00:50:36 +02:00
|
|
|
/* store the request so we know how to reply */
|
|
|
|
sock->req = *req;
|
2011-04-07 09:44:11 +02:00
|
|
|
/* operation is being processes */
|
|
|
|
sock->flags |= SOCK_FLG_OP_PENDING;
|
|
|
|
|
|
|
|
debug_print("no data to read, suspending");
|
2013-09-11 00:50:36 +02:00
|
|
|
return EDONTREPLY;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
static int raw_ip_op_write(struct socket * sock, struct sock_req * req,
|
|
|
|
__unused int blk)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct pbuf * pbuf;
|
|
|
|
struct ip_hdr * ip_hdr;
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
debug_print("socket num %ld data size %u",
|
|
|
|
get_sock_num(sock), req->size);
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
if (sock->pcb == NULL)
|
|
|
|
return EIO;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
if (req->size > sock->buf_size)
|
|
|
|
return ENOMEM;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
pbuf = pbuf_alloc(PBUF_LINK, req->size, PBUF_RAM);
|
|
|
|
if (!pbuf)
|
|
|
|
return ENOMEM;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
if ((ret = copy_from_user(req->endpt, pbuf->payload, req->size,
|
|
|
|
req->grant, 0)) != OK) {
|
2011-04-07 09:44:11 +02:00
|
|
|
pbuf_free(pbuf);
|
2013-09-11 00:50:36 +02:00
|
|
|
return ret;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ip_hdr = (struct ip_hdr *) pbuf->payload;
|
|
|
|
if (pbuf_header(pbuf, -IP_HLEN)) {
|
|
|
|
pbuf_free(pbuf);
|
2013-09-11 00:50:36 +02:00
|
|
|
return EIO;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = raw_sendto((struct raw_pcb *)sock->pcb, pbuf,
|
|
|
|
(ip_addr_t *) &ip_hdr->dest)) != OK) {
|
|
|
|
debug_print("raw_sendto failed %d", ret);
|
|
|
|
ret = EIO;
|
|
|
|
} else
|
2013-09-11 00:50:36 +02:00
|
|
|
ret = req->size;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
pbuf_free(pbuf);
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
return ret;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
static int raw_ip_set_opt(struct socket * sock, endpoint_t endpt,
|
|
|
|
cp_grant_id_t grant)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
nwio_ipopt_t ipopt;
|
|
|
|
struct raw_pcb * pcb;
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
err = copy_from_user(endpt, &ipopt, sizeof(ipopt), grant, 0);
|
2011-04-07 09:44:11 +02:00
|
|
|
|
|
|
|
if (err != OK)
|
2013-09-11 00:50:36 +02:00
|
|
|
return err;
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
debug_print("ipopt.nwio_flags = 0x%x", ipopt.nwio_flags);
|
2011-04-07 09:44:11 +02:00
|
|
|
debug_print("ipopt.nwio_proto = 0x%x", ipopt.nwio_proto);
|
|
|
|
debug_print("ipopt.nwio_rem = 0x%x",
|
|
|
|
(unsigned int) ipopt.nwio_rem);
|
|
|
|
|
|
|
|
if (sock->pcb == NULL) {
|
|
|
|
if (!(pcb = raw_new(ipopt.nwio_proto))) {
|
|
|
|
raw_ip_close(sock);
|
2013-09-11 00:50:36 +02:00
|
|
|
return ENOMEM;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sock->pcb = pcb;
|
|
|
|
} else
|
|
|
|
pcb = (struct raw_pcb *) sock->pcb;
|
|
|
|
|
|
|
|
if (pcb->protocol != ipopt.nwio_proto) {
|
|
|
|
debug_print("conflicting ip socket protocols\n");
|
2013-09-11 00:50:36 +02:00
|
|
|
return EINVAL;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sock->usr_flags = ipopt.nwio_flags;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (raw_bind(pcb, (ip_addr_t *)&ipopt.nwio_rem) == ERR_USE) {
|
|
|
|
raw_ip_close(sock);
|
2013-09-11 00:50:36 +02:00
|
|
|
return EADDRINUSE;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* register a receive hook */
|
|
|
|
raw_recv((struct raw_pcb *) sock->pcb, raw_ip_op_receive, sock);
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
return OK;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
static int raw_ip_get_opt(struct socket * sock, endpoint_t endpt,
|
|
|
|
cp_grant_id_t grant)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
|
|
|
nwio_ipopt_t ipopt;
|
|
|
|
struct raw_pcb * pcb = (struct raw_pcb *) sock->pcb;
|
|
|
|
|
|
|
|
assert(pcb);
|
|
|
|
|
|
|
|
ipopt.nwio_rem = pcb->remote_ip.addr;
|
|
|
|
ipopt.nwio_flags = sock->usr_flags;
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
return copy_to_user(endpt, &ipopt, sizeof(ipopt), grant, 0);
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
static int raw_ip_op_ioctl(struct socket * sock, struct sock_req * req,
|
|
|
|
__unused int blk)
|
2011-04-07 09:44:11 +02:00
|
|
|
{
|
2013-09-11 00:50:36 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
debug_print("socket num %ld req %c %ld %ld",
|
2011-04-07 09:44:11 +02:00
|
|
|
get_sock_num(sock),
|
2013-09-11 00:50:36 +02:00
|
|
|
(unsigned char) (req->req >> 8),
|
|
|
|
req->req & 0xff,
|
2013-12-12 17:34:44 +01:00
|
|
|
_MINIX_IOCTL_SIZE(req->req));
|
2011-04-07 09:44:11 +02:00
|
|
|
|
2013-09-11 00:50:36 +02:00
|
|
|
switch (req->req) {
|
2011-04-07 09:44:11 +02:00
|
|
|
case NWIOSIPOPT:
|
2013-09-11 00:50:36 +02:00
|
|
|
r = raw_ip_set_opt(sock, req->endpt, req->grant);
|
2011-04-07 09:44:11 +02:00
|
|
|
break;
|
|
|
|
case NWIOGIPOPT:
|
2013-09-11 00:50:36 +02:00
|
|
|
r = raw_ip_get_opt(sock, req->endpt, req->grant);
|
2011-04-07 09:44:11 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* /dev/ip can be also accessed as a default device to be
|
|
|
|
* configured
|
|
|
|
*/
|
2013-09-11 00:50:36 +02:00
|
|
|
r = nic_default_ioctl(req);
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
2013-09-11 00:50:36 +02:00
|
|
|
|
|
|
|
return r;
|
2011-04-07 09:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct sock_ops sock_raw_ip_ops = {
|
|
|
|
.open = raw_ip_op_open,
|
|
|
|
.close = raw_ip_op_close,
|
|
|
|
.read = raw_ip_op_read,
|
|
|
|
.write = raw_ip_op_write,
|
|
|
|
.ioctl = raw_ip_op_ioctl,
|
|
|
|
.select = generic_op_select,
|
|
|
|
.select_reply = generic_op_select_reply
|
|
|
|
};
|