Phonet: Phonet datagram transport protocol
[safe/jmp/linux-2.6] / net / phonet / af_phonet.c
1 /*
2  * File: af_phonet.c
3  *
4  * Phonet protocols family
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>
27 #include <linux/module.h>
28 #include <asm/unaligned.h>
29 #include <net/sock.h>
30
31 #include <linux/if_phonet.h>
32 #include <linux/phonet.h>
33 #include <net/phonet/phonet.h>
34 #include <net/phonet/pn_dev.h>
35
36 static struct net_proto_family phonet_proto_family;
37 static struct phonet_protocol *phonet_proto_get(int protocol);
38 static inline void phonet_proto_put(struct phonet_protocol *pp);
39
40 /* protocol family functions */
41
42 static int pn_socket_create(struct net *net, struct socket *sock, int protocol)
43 {
44         struct sock *sk;
45         struct pn_sock *pn;
46         struct phonet_protocol *pnp;
47         int err;
48
49         if (net != &init_net)
50                 return -EAFNOSUPPORT;
51
52         if (!capable(CAP_SYS_ADMIN))
53                 return -EPERM;
54
55         if (protocol == 0) {
56                 /* Default protocol selection */
57                 switch (sock->type) {
58                 case SOCK_DGRAM:
59                         protocol = PN_PROTO_PHONET;
60                         break;
61                 default:
62                         return -EPROTONOSUPPORT;
63                 }
64         }
65
66         pnp = phonet_proto_get(protocol);
67         if (pnp == NULL)
68                 return -EPROTONOSUPPORT;
69         if (sock->type != pnp->sock_type) {
70                 err = -EPROTONOSUPPORT;
71                 goto out;
72         }
73
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;
90
91 out:
92         phonet_proto_put(pnp);
93         return err;
94 }
95
96 static struct net_proto_family phonet_proto_family = {
97         .family = AF_PHONET,
98         .create = pn_socket_create,
99         .owner = THIS_MODULE,
100 };
101
102 /*
103  * Prepends an ISI header and sends a datagram.
104  */
105 static int pn_send(struct sk_buff *skb, struct net_device *dev,
106                         u16 dst, u16 src, u8 res)
107 {
108         struct phonethdr *ph;
109         int err;
110
111         if (skb->len + 2 > 0xffff) {
112                 /* Phonet length field would overflow */
113                 err = -EMSGSIZE;
114                 goto drop;
115         }
116
117         skb_reset_transport_header(skb);
118         WARN_ON(skb_headroom(skb) & 1); /* HW assumes word alignment */
119         skb_push(skb, sizeof(struct phonethdr));
120         skb_reset_network_header(skb);
121         ph = pn_hdr(skb);
122         ph->pn_rdev = pn_dev(dst);
123         ph->pn_sdev = pn_dev(src);
124         ph->pn_res = res;
125         ph->pn_length = __cpu_to_be16(skb->len + 2 - sizeof(*ph));
126         ph->pn_robj = pn_obj(dst);
127         ph->pn_sobj = pn_obj(src);
128
129         skb->protocol = htons(ETH_P_PHONET);
130         skb->priority = 0;
131         skb->dev = dev;
132
133         if (pn_addr(src) == pn_addr(dst)) {
134                 skb_reset_mac_header(skb);
135                 skb->pkt_type = PACKET_LOOPBACK;
136                 skb_orphan(skb);
137                 netif_rx_ni(skb);
138                 err = 0;
139         } else {
140                 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
141                                         NULL, NULL, skb->len);
142                 if (err < 0) {
143                         err = -EHOSTUNREACH;
144                         goto drop;
145                 }
146                 err = dev_queue_xmit(skb);
147         }
148
149         return err;
150 drop:
151         kfree_skb(skb);
152         return err;
153 }
154
155 /*
156  * Create a Phonet header for the skb and send it out. Returns
157  * non-zero error code if failed. The skb is freed then.
158  */
159 int pn_skb_send(struct sock *sk, struct sk_buff *skb,
160                 const struct sockaddr_pn *target)
161 {
162         struct net_device *dev;
163         struct pn_sock *pn = pn_sk(sk);
164         int err;
165         u16 src;
166         u8 daddr = pn_sockaddr_get_addr(target), saddr = PN_NO_ADDR;
167
168         err = -EHOSTUNREACH;
169         if (sk->sk_bound_dev_if)
170                 dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
171         else
172                 dev = phonet_device_get(sock_net(sk));
173         if (!dev || !(dev->flags & IFF_UP))
174                 goto drop;
175
176         saddr = phonet_address_get(dev, daddr);
177         if (saddr == PN_NO_ADDR)
178                 goto drop;
179
180         src = pn->sobject;
181         if (!pn_addr(src))
182                 src = pn_object(saddr, pn_obj(src));
183
184         err = pn_send(skb, dev, pn_sockaddr_get_object(target),
185                         src, pn_sockaddr_get_resource(target));
186         dev_put(dev);
187         return err;
188
189 drop:
190         kfree_skb(skb);
191         if (dev)
192                 dev_put(dev);
193         return err;
194 }
195 EXPORT_SYMBOL(pn_skb_send);
196
197 /* packet type functions */
198
199 /*
200  * Stuff received packets to associated sockets.
201  * On error, returns non-zero and releases the skb.
202  */
203 static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
204                         struct packet_type *pkttype,
205                         struct net_device *orig_dev)
206 {
207         struct phonethdr *ph;
208         struct sock *sk;
209         struct sockaddr_pn sa;
210         u16 len;
211
212         if (dev_net(dev) != &init_net)
213                 goto out;
214
215         /* check we have at least a full Phonet header */
216         if (!pskb_pull(skb, sizeof(struct phonethdr)))
217                 goto out;
218
219         /* check that the advertised length is correct */
220         ph = pn_hdr(skb);
221         len = get_unaligned_be16(&ph->pn_length);
222         if (len < 2)
223                 goto out;
224         len -= 2;
225         if ((len > skb->len) || pskb_trim(skb, len))
226                 goto out;
227         skb_reset_transport_header(skb);
228
229         pn_skb_get_dst_sockaddr(skb, &sa);
230         if (pn_sockaddr_get_addr(&sa) == 0)
231                 goto out; /* currently, we cannot be device 0 */
232
233         sk = pn_find_sock_by_sa(&sa);
234         if (sk == NULL)
235                 goto out;
236
237         /* Push data to the socket (or other sockets connected to it). */
238         return sk_receive_skb(sk, skb, 0);
239
240 out:
241         kfree_skb(skb);
242         return NET_RX_DROP;
243 }
244
245 static struct packet_type phonet_packet_type = {
246         .type = __constant_htons(ETH_P_PHONET),
247         .dev = NULL,
248         .func = phonet_rcv,
249 };
250
251 /* Transport protocol registration */
252 static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
253 static DEFINE_SPINLOCK(proto_tab_lock);
254
255 int __init_or_module phonet_proto_register(int protocol,
256                                                 struct phonet_protocol *pp)
257 {
258         int err = 0;
259
260         if (protocol >= PHONET_NPROTO)
261                 return -EINVAL;
262
263         err = proto_register(pp->prot, 1);
264         if (err)
265                 return err;
266
267         spin_lock(&proto_tab_lock);
268         if (proto_tab[protocol])
269                 err = -EBUSY;
270         else
271                 proto_tab[protocol] = pp;
272         spin_unlock(&proto_tab_lock);
273
274         return err;
275 }
276 EXPORT_SYMBOL(phonet_proto_register);
277
278 void phonet_proto_unregister(int protocol, struct phonet_protocol *pp)
279 {
280         spin_lock(&proto_tab_lock);
281         BUG_ON(proto_tab[protocol] != pp);
282         proto_tab[protocol] = NULL;
283         spin_unlock(&proto_tab_lock);
284         proto_unregister(pp->prot);
285 }
286 EXPORT_SYMBOL(phonet_proto_unregister);
287
288 static struct phonet_protocol *phonet_proto_get(int protocol)
289 {
290         struct phonet_protocol *pp;
291
292         if (protocol >= PHONET_NPROTO)
293                 return NULL;
294
295         spin_lock(&proto_tab_lock);
296         pp = proto_tab[protocol];
297         if (pp && !try_module_get(pp->prot->owner))
298                 pp = NULL;
299         spin_unlock(&proto_tab_lock);
300
301         return pp;
302 }
303
304 static inline void phonet_proto_put(struct phonet_protocol *pp)
305 {
306         module_put(pp->prot->owner);
307 }
308
309 /* Module registration */
310 static int __init phonet_init(void)
311 {
312         int err;
313
314         err = sock_register(&phonet_proto_family);
315         if (err) {
316                 printk(KERN_ALERT
317                         "phonet protocol family initialization failed\n");
318                 return err;
319         }
320
321         phonet_device_init();
322         dev_add_pack(&phonet_packet_type);
323         phonet_netlink_register();
324
325         err = isi_register();
326         if (err)
327                 goto err;
328         return 0;
329
330 err:
331         sock_unregister(AF_PHONET);
332         dev_remove_pack(&phonet_packet_type);
333         phonet_device_exit();
334         return err;
335 }
336
337 static void __exit phonet_exit(void)
338 {
339         isi_unregister();
340         sock_unregister(AF_PHONET);
341         dev_remove_pack(&phonet_packet_type);
342         phonet_device_exit();
343 }
344
345 module_init(phonet_init);
346 module_exit(phonet_exit);
347 MODULE_DESCRIPTION("Phonet protocol stack for Linux");
348 MODULE_LICENSE("GPL");