Skip to content

Commit ba113a9

Browse files
Remi Denis-Courmontdavem330
authored andcommitted
Phonet: common socket glue
This provides the socket API for the Phonet protocols family. Signed-off-by: Remi Denis-Courmont <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8fb3974 commit ba113a9

File tree

5 files changed

+363
-3
lines changed

5 files changed

+363
-3
lines changed

include/linux/phonet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#define PNADDR_ANY 0
3333
#define PNPORT_RESOURCE_ROUTING 0
3434

35+
/* ioctls */
36+
#define SIOCPNGETOBJECT (SIOCPROTOPRIVATE + 0)
37+
3538
/* Phonet protocol header */
3639
struct phonethdr {
3740
__u8 pn_rdev;

include/net/phonet/phonet.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@
2929
*/
3030
#define MAX_PHONET_HEADER 8
3131

32+
/*
33+
* Every Phonet* socket has this structure first in its
34+
* protocol-specific structure under name c.
35+
*/
36+
struct pn_sock {
37+
struct sock sk;
38+
u16 sobject;
39+
u8 resource;
40+
};
41+
42+
static inline struct pn_sock *pn_sk(struct sock *sk)
43+
{
44+
return (struct pn_sock *)sk;
45+
}
46+
47+
extern const struct proto_ops phonet_dgram_ops;
48+
49+
struct sock *pn_find_sock_by_sa(const struct sockaddr_pn *sa);
50+
void pn_sock_hash(struct sock *sk);
51+
void pn_sock_unhash(struct sock *sk);
52+
int pn_sock_get_port(struct sock *sk, unsigned short sport);
53+
3254
static inline struct phonethdr *pn_hdr(struct sk_buff *skb)
3355
{
3456
return (struct phonethdr *)skb_network_header(skb);
@@ -64,6 +86,7 @@ void pn_skb_get_dst_sockaddr(struct sk_buff *skb, struct sockaddr_pn *sa)
6486

6587
/* Protocols in Phonet protocol family. */
6688
struct phonet_protocol {
89+
const struct proto_ops *ops;
6790
struct proto *prot;
6891
int sock_type;
6992
};

net/phonet/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ obj-$(CONFIG_PHONET) += phonet.o
33
phonet-objs := \
44
pn_dev.o \
55
pn_netlink.o \
6+
socket.o \
67
af_phonet.o

net/phonet/af_phonet.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ static inline void phonet_proto_put(struct phonet_protocol *pp);
4141

4242
static int pn_socket_create(struct net *net, struct socket *sock, int protocol)
4343
{
44+
struct sock *sk;
45+
struct pn_sock *pn;
4446
struct phonet_protocol *pnp;
4547
int err;
4648

@@ -69,8 +71,22 @@ static int pn_socket_create(struct net *net, struct socket *sock, int protocol)
6971
goto out;
7072
}
7173

72-
/* TODO: create and init the struct sock */
73-
err = -EPROTONOSUPPORT;
74+
sk = sk_alloc(net, PF_PHONET, GFP_KERNEL, pnp->prot);
75+
if (sk == NULL) {
76+
err = -ENOMEM;
77+
goto out;
78+
}
79+
80+
sock_init_data(sock, sk);
81+
sock->state = SS_UNCONNECTED;
82+
sock->ops = pnp->ops;
83+
sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
84+
sk->sk_protocol = protocol;
85+
pn = pn_sk(sk);
86+
pn->sobject = 0;
87+
pn->resource = 0;
88+
sk->sk_prot->init(sk);
89+
err = 0;
7490

7591
out:
7692
phonet_proto_put(pnp);
@@ -94,6 +110,7 @@ static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
94110
struct net_device *orig_dev)
95111
{
96112
struct phonethdr *ph;
113+
struct sock *sk;
97114
struct sockaddr_pn sa;
98115
u16 len;
99116

@@ -118,7 +135,12 @@ static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
118135
if (pn_sockaddr_get_addr(&sa) == 0)
119136
goto out; /* currently, we cannot be device 0 */
120137

121-
/* TODO: put packets to sockets backlog */
138+
sk = pn_find_sock_by_sa(&sa);
139+
if (sk == NULL)
140+
goto out;
141+
142+
/* Push data to the socket (or other sockets connected to it). */
143+
return sk_receive_skb(sk, skb, 0);
122144

123145
out:
124146
kfree_skb(skb);

0 commit comments

Comments
 (0)