Commit fc7071d0 authored by Pavel Emelyanov's avatar Pavel Emelyanov

net: Packet sockets basic support

Support only basic packet socket functionality -- create and bind.
This should be enough to start testing dhclient inside container.
Other stuff (filter, mmaps, fanouts, etc.) will come later.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent b9144570
...@@ -44,6 +44,7 @@ OBJS += sockets.o ...@@ -44,6 +44,7 @@ OBJS += sockets.o
OBJS += sk-inet.o OBJS += sk-inet.o
OBJS += sk-tcp.o OBJS += sk-tcp.o
OBJS += sk-unix.o OBJS += sk-unix.o
OBJS += sk-packet.o
OBJS += sk-queue.o OBJS += sk-queue.o
OBJS += files.o OBJS += files.o
OBJS += files-reg.o OBJS += files-reg.o
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "syscall.h" #include "syscall.h"
#include "restorer.h" #include "restorer.h"
#include "sockets.h" #include "sockets.h"
#include "sk-packet.h"
#include "lock.h" #include "lock.h"
#include "files.h" #include "files.h"
#include "files-reg.h" #include "files-reg.h"
...@@ -102,6 +103,9 @@ static int prepare_shared(void) ...@@ -102,6 +103,9 @@ static int prepare_shared(void)
if (collect_unix_sockets()) if (collect_unix_sockets())
return -1; return -1;
if (collect_packet_sockets())
return -1;
if (collect_eventfd()) if (collect_eventfd())
return -1; return -1;
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "uts_ns.h" #include "uts_ns.h"
#include "ipc_ns.h" #include "ipc_ns.h"
#include "sk-inet.h" #include "sk-inet.h"
#include "sk-packet.h"
#include "mount.h" #include "mount.h"
#include "net.h" #include "net.h"
#include "protobuf.h" #include "protobuf.h"
...@@ -107,6 +108,7 @@ struct cr_fd_desc_tmpl fdset_template[CR_FD_MAX] = { ...@@ -107,6 +108,7 @@ struct cr_fd_desc_tmpl fdset_template[CR_FD_MAX] = {
FD_ENTRY(SIGACT, "sigacts-%d", show_sigacts), FD_ENTRY(SIGACT, "sigacts-%d", show_sigacts),
FD_ENTRY(UNIXSK, "unixsk", show_unixsk), FD_ENTRY(UNIXSK, "unixsk", show_unixsk),
FD_ENTRY(INETSK, "inetsk", show_inetsk), FD_ENTRY(INETSK, "inetsk", show_inetsk),
FD_ENTRY(PACKETSK, "packetsk", show_packetsk),
FD_ENTRY(SK_QUEUES, "sk-queues", show_sk_queues), FD_ENTRY(SK_QUEUES, "sk-queues", show_sk_queues),
FD_ENTRY(ITIMERS, "itimers-%d", show_itimers), FD_ENTRY(ITIMERS, "itimers-%d", show_itimers),
FD_ENTRY(CREDS, "creds-%d", show_creds), FD_ENTRY(CREDS, "creds-%d", show_creds),
......
...@@ -61,6 +61,7 @@ enum { ...@@ -61,6 +61,7 @@ enum {
CR_FD_REG_FILES, CR_FD_REG_FILES,
CR_FD_INETSK, CR_FD_INETSK,
CR_FD_UNIXSK, CR_FD_UNIXSK,
CR_FD_PACKETSK,
CR_FD_PIPES, CR_FD_PIPES,
CR_FD_PIPES_DATA, CR_FD_PIPES_DATA,
CR_FD_FIFO, CR_FD_FIFO,
......
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#define SIGACT_MAGIC 0x55344201 /* Murom */ #define SIGACT_MAGIC 0x55344201 /* Murom */
#define UNIXSK_MAGIC 0x54373943 /* Ryazan */ #define UNIXSK_MAGIC 0x54373943 /* Ryazan */
#define INETSK_MAGIC 0x56443851 /* Pereslavl */ #define INETSK_MAGIC 0x56443851 /* Pereslavl */
#define PACKETSK_MAGIC 0x60454618 /* Veliky Ustyug */
#define ITIMERS_MAGIC 0x57464056 /* Kostroma */ #define ITIMERS_MAGIC 0x57464056 /* Kostroma */
#define SK_QUEUES_MAGIC 0x56264026 /* Suzdal */ #define SK_QUEUES_MAGIC 0x56264026 /* Suzdal */
#define UTSNS_MAGIC 0x54473203 /* Smolensk */ #define UTSNS_MAGIC 0x54473203 /* Smolensk */
......
...@@ -30,6 +30,7 @@ enum { ...@@ -30,6 +30,7 @@ enum {
PB_REG_FILES, PB_REG_FILES,
PB_INETSK, PB_INETSK,
PB_UNIXSK, PB_UNIXSK,
PB_PACKETSK,
PB_PIPES, PB_PIPES,
PB_FIFO, PB_FIFO,
PB_PIPES_DATA, PB_PIPES_DATA,
......
#ifndef __CR_SK_PACKET_H__
#define __CR_SK_PACKET_H__
struct cr_fdset;
struct fd_parms;
struct cr_options;
int dump_one_packet_sk(struct fd_parms *p, int lfd, const struct cr_fdset *fds);
int collect_packet_sockets(void);
void show_packetsk(int fd, struct cr_options *);
#endif
...@@ -56,5 +56,6 @@ extern int inet_collect_one(struct nlmsghdr *h, int family, int type, int proto) ...@@ -56,5 +56,6 @@ extern int inet_collect_one(struct nlmsghdr *h, int family, int type, int proto)
extern int unix_receive_one(struct nlmsghdr *h, void *); extern int unix_receive_one(struct nlmsghdr *h, void *);
extern int do_dump_opt(int sk, int name, void *val, int len); extern int do_dump_opt(int sk, int name, void *val, int len);
#define dump_opt(s, n, f) do_dump_opt(s, n, f, sizeof(*f))
#endif /* CR_SOCKETS_H__ */ #endif /* CR_SOCKETS_H__ */
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "protobuf/sa.pb-c.h" #include "protobuf/sa.pb-c.h"
#include "protobuf/sk-unix.pb-c.h" #include "protobuf/sk-unix.pb-c.h"
#include "protobuf/sk-inet.pb-c.h" #include "protobuf/sk-inet.pb-c.h"
#include "protobuf/packet-sock.pb-c.h"
#include "protobuf/sk-packet.pb-c.h" #include "protobuf/sk-packet.pb-c.h"
#include "protobuf/creds.pb-c.h" #include "protobuf/creds.pb-c.h"
#include "protobuf/itimer.pb-c.h" #include "protobuf/itimer.pb-c.h"
...@@ -116,6 +117,7 @@ void cr_pb_init(void) ...@@ -116,6 +117,7 @@ void cr_pb_init(void)
CR_PB_DESC(TCP_STREAM, TcpStream, tcp_stream); CR_PB_DESC(TCP_STREAM, TcpStream, tcp_stream);
CR_PB_DESC(MOUNTPOINTS, Mnt, mnt); CR_PB_DESC(MOUNTPOINTS, Mnt, mnt);
CR_PB_DESC(NETDEV, NetDevice, net_device); CR_PB_DESC(NETDEV, NetDevice, net_device);
CR_PB_DESC(PACKETSK, PacketSock, packet_sock);
} }
/* /*
......
...@@ -44,6 +44,7 @@ PROTO_FILES += mm.proto ...@@ -44,6 +44,7 @@ PROTO_FILES += mm.proto
PROTO_FILES += sk-opts.proto PROTO_FILES += sk-opts.proto
PROTO_FILES += sk-unix.proto PROTO_FILES += sk-unix.proto
PROTO_FILES += sk-inet.proto PROTO_FILES += sk-inet.proto
PROTO_FILES += packet-sock.proto
PROTO_FILES += ipc-var.proto PROTO_FILES += ipc-var.proto
PROTO_FILES += ipc-desc.proto PROTO_FILES += ipc-desc.proto
PROTO_FILES += ipc-shm.proto PROTO_FILES += ipc-shm.proto
......
...@@ -9,6 +9,7 @@ enum fd_types { ...@@ -9,6 +9,7 @@ enum fd_types {
EVENTPOLL = 7; EVENTPOLL = 7;
INOTIFY = 8; INOTIFY = 8;
SIGNALFD = 9; SIGNALFD = 9;
PACKETSK = 10;
} }
message fdinfo_entry { message fdinfo_entry {
......
import "fown.proto";
import "sk-opts.proto";
message packet_sock_entry {
required uint32 id = 1;
required uint32 type = 2;
required uint32 protocol = 3;
required uint32 flags = 4;
required uint32 ifindex = 5;
required fown_entry fown = 6;
required sk_opts_entry opts = 7;
}
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <unistd.h>
#include <string.h>
#include "crtools.h"
#include "types.h"
#include "files.h"
#include "sockets.h"
#include "sk-packet.h"
#include "protobuf.h"
#include "protobuf/packet-sock.pb-c.h"
#include "protobuf/fdinfo.pb-c.h"
struct packet_sock_info {
PacketSockEntry *pse;
struct file_desc d;
};
void show_packetsk(int fd, struct cr_options *o)
{
pb_show_plain(fd, PB_PACKETSK);
}
static int dump_one_packet_fd(int lfd, u32 id, const struct fd_parms *p)
{
int type;
PacketSockEntry psk = PACKET_SOCK_ENTRY__INIT;
SkOptsEntry skopts = SK_OPTS_ENTRY__INIT;
struct sockaddr_ll addr;
socklen_t alen;
pr_info("Dumping packet socket fd %d id %#x\n", lfd, id);
if (dump_opt(lfd, SO_TYPE, &type))
return -1;
psk.id = id;
psk.type = type;
psk.flags = p->flags;
psk.fown = (FownEntry *)&p->fown;
psk.opts = &skopts;
if (dump_socket_opts(lfd, &skopts))
return -1;
alen = sizeof(addr);
if (getsockname(lfd, (struct sockaddr *)&addr, &alen) < 0) {
pr_perror("Can't get packet sock name");
return -1;
}
psk.protocol = addr.sll_protocol;
psk.ifindex = addr.sll_ifindex;
return pb_write_one(fdset_fd(glob_fdset, CR_FD_PACKETSK), &psk, PB_PACKETSK);
}
static const struct fdtype_ops packet_dump_ops = {
.type = FD_TYPES__PACKETSK,
.make_gen_id = make_gen_id,
.dump = dump_one_packet_fd,
};
int dump_one_packet_sk(struct fd_parms *p, int lfd, const struct cr_fdset *fds)
{
return do_dump_gen_file(p, lfd, &packet_dump_ops, fds);
}
static int open_packet_sk(struct file_desc *d)
{
struct packet_sock_info *psi;
PacketSockEntry *pse;
struct sockaddr_ll addr;
int sk;
psi = container_of(d, struct packet_sock_info, d);
pse = psi->pse;
pr_info("Opening packet socket id %#x\n", pse->id);
sk = socket(PF_PACKET, pse->type, pse->protocol);
if (sk < 0) {
pr_perror("Can't create packet sock");
goto err;
}
memset(&addr, 0, sizeof(addr));
addr.sll_family = AF_PACKET;
addr.sll_ifindex = pse->ifindex;
if (bind(sk, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
pr_perror("Can't bind packet socket");
goto err_cl;
}
if (rst_file_params(sk, pse->fown, pse->flags))
goto err_cl;
if (restore_socket_opts(sk, pse->opts))
goto err_cl;
return sk;
err_cl:
close(sk);
err:
return -1;
}
static struct file_desc_ops packet_sock_desc_ops = {
.type = FD_TYPES__PACKETSK,
.open = open_packet_sk,
};
static int collect_one_packet_sk(void *o, ProtobufCMessage *base)
{
struct packet_sock_info *si = o;
si->pse = pb_msg(base, PacketSockEntry);
file_desc_add(&si->d, si->pse->id, &packet_sock_desc_ops);
return 0;
}
int collect_packet_sockets(void)
{
return collect_image(CR_FD_PACKETSK, PB_PACKETSK,
sizeof(struct packet_sock_info), collect_one_packet_sk);
}
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "inet_diag.h" #include "inet_diag.h"
#include "files.h" #include "files.h"
#include "util-net.h" #include "util-net.h"
#include "sk-packet.h"
#ifndef NETLINK_SOCK_DIAG #ifndef NETLINK_SOCK_DIAG
#define NETLINK_SOCK_DIAG NETLINK_INET_DIAG #define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
...@@ -99,8 +100,6 @@ int do_dump_opt(int sk, int name, void *val, int len) ...@@ -99,8 +100,6 @@ int do_dump_opt(int sk, int name, void *val, int len)
return 0; return 0;
} }
#define dump_opt(s, n, f) do_dump_opt(s, n, f, sizeof(*f))
int dump_socket_opts(int sk, SkOptsEntry *soe) int dump_socket_opts(int sk, SkOptsEntry *soe)
{ {
int ret = 0; int ret = 0;
...@@ -133,6 +132,8 @@ int dump_socket(struct fd_parms *p, int lfd, const struct cr_fdset *cr_fdset) ...@@ -133,6 +132,8 @@ int dump_socket(struct fd_parms *p, int lfd, const struct cr_fdset *cr_fdset)
case AF_INET: case AF_INET:
case AF_INET6: case AF_INET6:
return dump_one_inet(p, lfd, cr_fdset); return dump_one_inet(p, lfd, cr_fdset);
case AF_PACKET:
return dump_one_packet_sk(p, lfd, cr_fdset);
default: default:
pr_err("BUG! Unknown socket collected\n"); pr_err("BUG! Unknown socket collected\n");
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment