Skip to content

Commit 9995a32

Browse files
Rémi Denis-Courmontdavem330
authored andcommitted
Phonet: connected sockets glue
Signed-off-by: Rémi Denis-Courmont <[email protected]> Acked-by: Arnaldo Carvalho de Melo <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 2553282 commit 9995a32

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

include/net/phonet/pep.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* File: pep.h
3+
*
4+
* Phonet Pipe End Point sockets definitions
5+
*
6+
* Copyright (C) 2008 Nokia Corporation.
7+
*
8+
* This program is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU General Public License
10+
* version 2 as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful, but
13+
* WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20+
* 02110-1301 USA
21+
*/
22+
23+
#ifndef NET_PHONET_PEP_H
24+
#define NET_PHONET_PEP_H
25+
26+
struct pep_sock {
27+
struct pn_sock pn_sk;
28+
29+
/* Listening socket stuff: */
30+
struct hlist_head ackq;
31+
32+
/* Connected socket stuff: */
33+
u8 tx_credits;
34+
};
35+
36+
static inline struct pep_sock *pep_sk(struct sock *sk)
37+
{
38+
return (struct pep_sock *)sk;
39+
}
40+
41+
extern const struct proto_ops phonet_stream_ops;
42+
43+
#endif

net/phonet/socket.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525

2626
#include <linux/kernel.h>
2727
#include <linux/net.h>
28+
#include <linux/poll.h>
2829
#include <net/sock.h>
2930
#include <net/tcp_states.h>
3031

3132
#include <linux/phonet.h>
3233
#include <net/phonet/phonet.h>
34+
#include <net/phonet/pep.h>
3335
#include <net/phonet/pn_dev.h>
3436

3537
static int pn_socket_release(struct socket *sock)
@@ -166,6 +168,24 @@ static int pn_socket_autobind(struct socket *sock)
166168
return 0; /* socket was already bound */
167169
}
168170

171+
static int pn_socket_accept(struct socket *sock, struct socket *newsock,
172+
int flags)
173+
{
174+
struct sock *sk = sock->sk;
175+
struct sock *newsk;
176+
int err;
177+
178+
newsk = sk->sk_prot->accept(sk, flags, &err);
179+
if (!newsk)
180+
return err;
181+
182+
lock_sock(newsk);
183+
sock_graft(newsk, newsock);
184+
newsock->state = SS_CONNECTED;
185+
release_sock(newsk);
186+
return 0;
187+
}
188+
169189
static int pn_socket_getname(struct socket *sock, struct sockaddr *addr,
170190
int *sockaddr_len, int peer)
171191
{
@@ -182,6 +202,33 @@ static int pn_socket_getname(struct socket *sock, struct sockaddr *addr,
182202
return 0;
183203
}
184204

205+
static unsigned int pn_socket_poll(struct file *file, struct socket *sock,
206+
poll_table *wait)
207+
{
208+
struct sock *sk = sock->sk;
209+
struct pep_sock *pn = pep_sk(sk);
210+
unsigned int mask = 0;
211+
212+
poll_wait(file, &sock->wait, wait);
213+
214+
switch (sk->sk_state) {
215+
case TCP_LISTEN:
216+
return hlist_empty(&pn->ackq) ? 0 : POLLIN;
217+
case TCP_CLOSE:
218+
return POLLERR;
219+
}
220+
221+
if (!skb_queue_empty(&sk->sk_receive_queue))
222+
mask |= POLLIN | POLLRDNORM;
223+
else if (sk->sk_state == TCP_CLOSE_WAIT)
224+
return POLLHUP;
225+
226+
if (sk->sk_state == TCP_ESTABLISHED && pn->tx_credits)
227+
mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
228+
229+
return mask;
230+
}
231+
185232
static int pn_socket_ioctl(struct socket *sock, unsigned int cmd,
186233
unsigned long arg)
187234
{
@@ -220,6 +267,30 @@ static int pn_socket_ioctl(struct socket *sock, unsigned int cmd,
220267
return sk->sk_prot->ioctl(sk, cmd, arg);
221268
}
222269

270+
static int pn_socket_listen(struct socket *sock, int backlog)
271+
{
272+
struct sock *sk = sock->sk;
273+
int err = 0;
274+
275+
if (sock->state != SS_UNCONNECTED)
276+
return -EINVAL;
277+
if (pn_socket_autobind(sock))
278+
return -ENOBUFS;
279+
280+
lock_sock(sk);
281+
if (sk->sk_state != TCP_CLOSE) {
282+
err = -EINVAL;
283+
goto out;
284+
}
285+
286+
sk->sk_state = TCP_LISTEN;
287+
sk->sk_ack_backlog = 0;
288+
sk->sk_max_ack_backlog = backlog;
289+
out:
290+
release_sock(sk);
291+
return err;
292+
}
293+
223294
static int pn_socket_sendmsg(struct kiocb *iocb, struct socket *sock,
224295
struct msghdr *m, size_t total_len)
225296
{
@@ -256,6 +327,32 @@ const struct proto_ops phonet_dgram_ops = {
256327
.sendpage = sock_no_sendpage,
257328
};
258329

330+
const struct proto_ops phonet_stream_ops = {
331+
.family = AF_PHONET,
332+
.owner = THIS_MODULE,
333+
.release = pn_socket_release,
334+
.bind = pn_socket_bind,
335+
.connect = sock_no_connect,
336+
.socketpair = sock_no_socketpair,
337+
.accept = pn_socket_accept,
338+
.getname = pn_socket_getname,
339+
.poll = pn_socket_poll,
340+
.ioctl = pn_socket_ioctl,
341+
.listen = pn_socket_listen,
342+
.shutdown = sock_no_shutdown,
343+
.setsockopt = sock_no_setsockopt,
344+
.getsockopt = sock_no_getsockopt,
345+
#ifdef CONFIG_COMPAT
346+
.compat_setsockopt = sock_no_setsockopt,
347+
.compat_getsockopt = compat_sock_no_getsockopt,
348+
#endif
349+
.sendmsg = pn_socket_sendmsg,
350+
.recvmsg = sock_common_recvmsg,
351+
.mmap = sock_no_mmap,
352+
.sendpage = sock_no_sendpage,
353+
};
354+
EXPORT_SYMBOL(phonet_stream_ops);
355+
259356
static DEFINE_MUTEX(port_mutex);
260357

261358
/* allocate port for a socket */

0 commit comments

Comments
 (0)