]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/phonet/datagram.c
Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[net-next-2.6.git] / net / phonet / datagram.c
CommitLineData
107d0d9b
RDC
1/*
2 * File: datagram.c
3 *
4 * Datagram (ISI) Phonet sockets
5 *
6 * Copyright (C) 2008 Nokia Corporation.
7 *
8 * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9 * Original author: Sakari Ailus <sakari.ailus@nokia.com>
10 *
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.
14 *
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.
19 *
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
23 * 02110-1301 USA
24 */
25
26#include <linux/kernel.h>
5a0e3ad6 27#include <linux/slab.h>
107d0d9b
RDC
28#include <linux/socket.h>
29#include <asm/ioctls.h>
30#include <net/sock.h>
31
32#include <linux/phonet.h>
33#include <net/phonet/phonet.h>
34
35static int pn_backlog_rcv(struct sock *sk, struct sk_buff *skb);
36
37/* associated socket ceases to exist */
38static void pn_sock_close(struct sock *sk, long timeout)
39{
40 sk_common_release(sk);
41}
42
43static int pn_ioctl(struct sock *sk, int cmd, unsigned long arg)
44{
45 struct sk_buff *skb;
46 int answ;
47
48 switch (cmd) {
49 case SIOCINQ:
50 lock_sock(sk);
51 skb = skb_peek(&sk->sk_receive_queue);
52 answ = skb ? skb->len : 0;
53 release_sock(sk);
54 return put_user(answ, (int __user *)arg);
55 }
56
57 return -ENOIOCTLCMD;
58}
59
60/* Destroy socket. All references are gone. */
61static void pn_destruct(struct sock *sk)
62{
63 skb_queue_purge(&sk->sk_receive_queue);
64}
65
66static int pn_init(struct sock *sk)
67{
68 sk->sk_destruct = pn_destruct;
69 return 0;
70}
71
72static int pn_sendmsg(struct kiocb *iocb, struct sock *sk,
73 struct msghdr *msg, size_t len)
74{
75 struct sockaddr_pn *target;
76 struct sk_buff *skb;
77 int err;
78
82ecbcb9
RDC
79 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
80 MSG_CMSG_COMPAT))
107d0d9b
RDC
81 return -EOPNOTSUPP;
82
83 if (msg->msg_name == NULL)
84 return -EDESTADDRREQ;
85
86 if (msg->msg_namelen < sizeof(struct sockaddr_pn))
87 return -EINVAL;
88
89 target = (struct sockaddr_pn *)msg->msg_name;
90 if (target->spn_family != AF_PHONET)
91 return -EAFNOSUPPORT;
92
93 skb = sock_alloc_send_skb(sk, MAX_PHONET_HEADER + len,
94 msg->msg_flags & MSG_DONTWAIT, &err);
95 if (skb == NULL)
96 return err;
97 skb_reserve(skb, MAX_PHONET_HEADER);
98
99 err = memcpy_fromiovec((void *)skb_put(skb, len), msg->msg_iov, len);
100 if (err < 0) {
101 kfree_skb(skb);
102 return err;
103 }
104
105 /*
106 * Fill in the Phonet header and
107 * finally pass the packet forwards.
108 */
109 err = pn_skb_send(sk, skb, target);
110
111 /* If ok, return len. */
112 return (err >= 0) ? len : err;
113}
114
115static int pn_recvmsg(struct kiocb *iocb, struct sock *sk,
116 struct msghdr *msg, size_t len, int noblock,
117 int flags, int *addr_len)
118{
119 struct sk_buff *skb = NULL;
120 struct sockaddr_pn sa;
121 int rval = -EOPNOTSUPP;
122 int copylen;
123
82ecbcb9
RDC
124 if (flags & ~(MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_NOSIGNAL|
125 MSG_CMSG_COMPAT))
107d0d9b
RDC
126 goto out_nofree;
127
128 if (addr_len)
129 *addr_len = sizeof(sa);
130
131 skb = skb_recv_datagram(sk, flags, noblock, &rval);
132 if (skb == NULL)
133 goto out_nofree;
134
135 pn_skb_get_src_sockaddr(skb, &sa);
136
137 copylen = skb->len;
138 if (len < copylen) {
139 msg->msg_flags |= MSG_TRUNC;
140 copylen = len;
141 }
142
143 rval = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copylen);
144 if (rval) {
145 rval = -EFAULT;
146 goto out;
147 }
148
149 rval = (flags & MSG_TRUNC) ? skb->len : copylen;
150
151 if (msg->msg_name != NULL)
152 memcpy(msg->msg_name, &sa, sizeof(struct sockaddr_pn));
153
154out:
155 skb_free_datagram(sk, skb);
156
157out_nofree:
158 return rval;
159}
160
161/* Queue an skb for a sock. */
162static int pn_backlog_rcv(struct sock *sk, struct sk_buff *skb)
163{
164 int err = sock_queue_rcv_skb(sk, skb);
766e9037
ED
165
166 if (err < 0)
107d0d9b
RDC
167 kfree_skb(skb);
168 return err ? NET_RX_DROP : NET_RX_SUCCESS;
169}
170
171/* Module registration */
172static struct proto pn_proto = {
173 .close = pn_sock_close,
174 .ioctl = pn_ioctl,
175 .init = pn_init,
176 .sendmsg = pn_sendmsg,
177 .recvmsg = pn_recvmsg,
178 .backlog_rcv = pn_backlog_rcv,
179 .hash = pn_sock_hash,
180 .unhash = pn_sock_unhash,
181 .get_port = pn_sock_get_port,
182 .obj_size = sizeof(struct pn_sock),
183 .owner = THIS_MODULE,
184 .name = "PHONET",
185};
186
187static struct phonet_protocol pn_dgram_proto = {
188 .ops = &phonet_dgram_ops,
189 .prot = &pn_proto,
190 .sock_type = SOCK_DGRAM,
191};
192
193int __init isi_register(void)
194{
195 return phonet_proto_register(PN_PROTO_PHONET, &pn_dgram_proto);
196}
197
198void __exit isi_unregister(void)
199{
200 phonet_proto_unregister(PN_PROTO_PHONET, &pn_dgram_proto);
201}