4 * Datagram (ISI) Phonet sockets
6 * Copyright (C) 2008 Nokia Corporation.
8 * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9 * Original author: Sakari Ailus <sakari.ailus@nokia.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * version 2 as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 #include <linux/kernel.h>
27 #include <linux/socket.h>
28 #include <asm/ioctls.h>
31 #include <linux/phonet.h>
32 #include <net/phonet/phonet.h>
34 static int pn_backlog_rcv(struct sock *sk, struct sk_buff *skb);
36 /* associated socket ceases to exist */
37 static void pn_sock_close(struct sock *sk, long timeout)
39 sk_common_release(sk);
42 static int pn_ioctl(struct sock *sk, int cmd, unsigned long arg)
50 skb = skb_peek(&sk->sk_receive_queue);
51 answ = skb ? skb->len : 0;
53 return put_user(answ, (int __user *)arg);
59 /* Destroy socket. All references are gone. */
60 static void pn_destruct(struct sock *sk)
62 skb_queue_purge(&sk->sk_receive_queue);
65 static int pn_init(struct sock *sk)
67 sk->sk_destruct = pn_destruct;
71 static int pn_sendmsg(struct kiocb *iocb, struct sock *sk,
72 struct msghdr *msg, size_t len)
74 struct sockaddr_pn *target;
78 if (msg->msg_flags & MSG_OOB)
81 if (msg->msg_name == NULL)
84 if (msg->msg_namelen < sizeof(struct sockaddr_pn))
87 target = (struct sockaddr_pn *)msg->msg_name;
88 if (target->spn_family != AF_PHONET)
91 skb = sock_alloc_send_skb(sk, MAX_PHONET_HEADER + len,
92 msg->msg_flags & MSG_DONTWAIT, &err);
95 skb_reserve(skb, MAX_PHONET_HEADER);
97 err = memcpy_fromiovec((void *)skb_put(skb, len), msg->msg_iov, len);
104 * Fill in the Phonet header and
105 * finally pass the packet forwards.
107 err = pn_skb_send(sk, skb, target);
109 /* If ok, return len. */
110 return (err >= 0) ? len : err;
113 static int pn_recvmsg(struct kiocb *iocb, struct sock *sk,
114 struct msghdr *msg, size_t len, int noblock,
115 int flags, int *addr_len)
117 struct sk_buff *skb = NULL;
118 struct sockaddr_pn sa;
119 int rval = -EOPNOTSUPP;
126 *addr_len = sizeof(sa);
128 skb = skb_recv_datagram(sk, flags, noblock, &rval);
132 pn_skb_get_src_sockaddr(skb, &sa);
136 msg->msg_flags |= MSG_TRUNC;
140 rval = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copylen);
146 rval = (flags & MSG_TRUNC) ? skb->len : copylen;
148 if (msg->msg_name != NULL)
149 memcpy(msg->msg_name, &sa, sizeof(struct sockaddr_pn));
152 skb_free_datagram(sk, skb);
158 /* Queue an skb for a sock. */
159 static int pn_backlog_rcv(struct sock *sk, struct sk_buff *skb)
161 int err = sock_queue_rcv_skb(sk, skb);
164 return err ? NET_RX_DROP : NET_RX_SUCCESS;
167 /* Module registration */
168 static struct proto pn_proto = {
169 .close = pn_sock_close,
172 .sendmsg = pn_sendmsg,
173 .recvmsg = pn_recvmsg,
174 .backlog_rcv = pn_backlog_rcv,
175 .hash = pn_sock_hash,
176 .unhash = pn_sock_unhash,
177 .get_port = pn_sock_get_port,
178 .obj_size = sizeof(struct pn_sock),
179 .owner = THIS_MODULE,
183 static struct phonet_protocol pn_dgram_proto = {
184 .ops = &phonet_dgram_ops,
186 .sock_type = SOCK_DGRAM,
189 int __init isi_register(void)
191 return phonet_proto_register(PN_PROTO_PHONET, &pn_dgram_proto);
194 void __exit isi_unregister(void)
196 phonet_proto_unregister(PN_PROTO_PHONET, &pn_dgram_proto);