51397ff308bd58d38e3aa1ac812422b86dc17936
[linux-2.6.git] / 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 /* Phonet device header operations */
103 static int pn_header_create(struct sk_buff *skb, struct net_device *dev,
104                                 unsigned short type, const void *daddr,
105                                 const void *saddr, unsigned len)
106 {
107         u8 *media = skb_push(skb, 1);
108
109         if (type != ETH_P_PHONET)
110                 return -1;
111
112         if (!saddr)
113                 saddr = dev->dev_addr;
114         *media = *(const u8 *)saddr;
115         return 1;
116 }
117
118 static int pn_header_parse(const struct sk_buff *skb, unsigned char *haddr)
119 {
120         const u8 *media = skb_mac_header(skb);
121         *haddr = *media;
122         return 1;
123 }
124
125 struct header_ops phonet_header_ops = {
126         .create = pn_header_create,
127         .parse = pn_header_parse,
128 };
129 EXPORT_SYMBOL(phonet_header_ops);
130
131 /*
132  * Prepends an ISI header and sends a datagram.
133  */
134 static int pn_send(struct sk_buff *skb, struct net_device *dev,
135                         u16 dst, u16 src, u8 res)
136 {
137         struct phonethdr *ph;
138         int err;
139
140         if (skb->len + 2 > 0xffff) {
141                 /* Phonet length field would overflow */
142                 err = -EMSGSIZE;
143                 goto drop;
144         }
145
146         skb_reset_transport_header(skb);
147         WARN_ON(skb_headroom(skb) & 1); /* HW assumes word alignment */
148         skb_push(skb, sizeof(struct phonethdr));
149         skb_reset_network_header(skb);
150         ph = pn_hdr(skb);
151         ph->pn_rdev = pn_dev(dst);
152         ph->pn_sdev = pn_dev(src);
153         ph->pn_res = res;
154         ph->pn_length = __cpu_to_be16(skb->len + 2 - sizeof(*ph));
155         ph->pn_robj = pn_obj(dst);
156         ph->pn_sobj = pn_obj(src);
157
158         skb->protocol = htons(ETH_P_PHONET);
159         skb->priority = 0;
160         skb->dev = dev;
161
162         if (pn_addr(src) == pn_addr(dst)) {
163                 skb_reset_mac_header(skb);
164                 skb->pkt_type = PACKET_LOOPBACK;
165                 skb_orphan(skb);
166                 netif_rx_ni(skb);
167                 err = 0;
168         } else {
169                 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
170                                         NULL, NULL, skb->len);
171                 if (err < 0) {
172                         err = -EHOSTUNREACH;
173                         goto drop;
174                 }
175                 err = dev_queue_xmit(skb);
176         }
177
178         return err;
179 drop:
180         kfree_skb(skb);
181         return err;
182 }
183
184 /*
185  * Create a Phonet header for the skb and send it out. Returns
186  * non-zero error code if failed. The skb is freed then.
187  */
188 int pn_skb_send(struct sock *sk, struct sk_buff *skb,
189                 const struct sockaddr_pn *target)
190 {
191         struct net_device *dev;
192         struct pn_sock *pn = pn_sk(sk);
193         int err;
194         u16 src;
195         u8 daddr = pn_sockaddr_get_addr(target), saddr = PN_NO_ADDR;
196
197         err = -EHOSTUNREACH;
198         if (sk->sk_bound_dev_if)
199                 dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
200         else
201                 dev = phonet_device_get(sock_net(sk));
202         if (!dev || !(dev->flags & IFF_UP))
203                 goto drop;
204
205         saddr = phonet_address_get(dev, daddr);
206         if (saddr == PN_NO_ADDR)
207                 goto drop;
208
209         src = pn->sobject;
210         if (!pn_addr(src))
211                 src = pn_object(saddr, pn_obj(src));
212
213         err = pn_send(skb, dev, pn_sockaddr_get_object(target),
214                         src, pn_sockaddr_get_resource(target));
215         dev_put(dev);
216         return err;
217
218 drop:
219         kfree_skb(skb);
220         if (dev)
221                 dev_put(dev);
222         return err;
223 }
224 EXPORT_SYMBOL(pn_skb_send);
225
226 /* packet type functions */
227
228 /*
229  * Stuff received packets to associated sockets.
230  * On error, returns non-zero and releases the skb.
231  */
232 static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
233                         struct packet_type *pkttype,
234                         struct net_device *orig_dev)
235 {
236         struct phonethdr *ph;
237         struct sock *sk;
238         struct sockaddr_pn sa;
239         u16 len;
240
241         if (dev_net(dev) != &init_net)
242                 goto out;
243
244         /* check we have at least a full Phonet header */
245         if (!pskb_pull(skb, sizeof(struct phonethdr)))
246                 goto out;
247
248         /* check that the advertised length is correct */
249         ph = pn_hdr(skb);
250         len = get_unaligned_be16(&ph->pn_length);
251         if (len < 2)
252                 goto out;
253         len -= 2;
254         if ((len > skb->len) || pskb_trim(skb, len))
255                 goto out;
256         skb_reset_transport_header(skb);
257
258         pn_skb_get_dst_sockaddr(skb, &sa);
259         if (pn_sockaddr_get_addr(&sa) == 0)
260                 goto out; /* currently, we cannot be device 0 */
261
262         sk = pn_find_sock_by_sa(&sa);
263         if (sk == NULL)
264                 goto out;
265
266         /* Push data to the socket (or other sockets connected to it). */
267         return sk_receive_skb(sk, skb, 0);
268
269 out:
270         kfree_skb(skb);
271         return NET_RX_DROP;
272 }
273
274 static struct packet_type phonet_packet_type = {
275         .type = __constant_htons(ETH_P_PHONET),
276         .dev = NULL,
277         .func = phonet_rcv,
278 };
279
280 /* Transport protocol registration */
281 static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
282 static DEFINE_SPINLOCK(proto_tab_lock);
283
284 int __init_or_module phonet_proto_register(int protocol,
285                                                 struct phonet_protocol *pp)
286 {
287         int err = 0;
288
289         if (protocol >= PHONET_NPROTO)
290                 return -EINVAL;
291
292         err = proto_register(pp->prot, 1);
293         if (err)
294                 return err;
295
296         spin_lock(&proto_tab_lock);
297         if (proto_tab[protocol])
298                 err = -EBUSY;
299         else
300                 proto_tab[protocol] = pp;
301         spin_unlock(&proto_tab_lock);
302
303         return err;
304 }
305 EXPORT_SYMBOL(phonet_proto_register);
306
307 void phonet_proto_unregister(int protocol, struct phonet_protocol *pp)
308 {
309         spin_lock(&proto_tab_lock);
310         BUG_ON(proto_tab[protocol] != pp);
311         proto_tab[protocol] = NULL;
312         spin_unlock(&proto_tab_lock);
313         proto_unregister(pp->prot);
314 }
315 EXPORT_SYMBOL(phonet_proto_unregister);
316
317 static struct phonet_protocol *phonet_proto_get(int protocol)
318 {
319         struct phonet_protocol *pp;
320
321         if (protocol >= PHONET_NPROTO)
322                 return NULL;
323
324         spin_lock(&proto_tab_lock);
325         pp = proto_tab[protocol];
326         if (pp && !try_module_get(pp->prot->owner))
327                 pp = NULL;
328         spin_unlock(&proto_tab_lock);
329
330         return pp;
331 }
332
333 static inline void phonet_proto_put(struct phonet_protocol *pp)
334 {
335         module_put(pp->prot->owner);
336 }
337
338 /* Module registration */
339 static int __init phonet_init(void)
340 {
341         int err;
342
343         err = sock_register(&phonet_proto_family);
344         if (err) {
345                 printk(KERN_ALERT
346                         "phonet protocol family initialization failed\n");
347                 return err;
348         }
349
350         phonet_device_init();
351         dev_add_pack(&phonet_packet_type);
352         phonet_netlink_register();
353
354         err = isi_register();
355         if (err)
356                 goto err;
357         return 0;
358
359 err:
360         sock_unregister(AF_PHONET);
361         dev_remove_pack(&phonet_packet_type);
362         phonet_device_exit();
363         return err;
364 }
365
366 static void __exit phonet_exit(void)
367 {
368         isi_unregister();
369         sock_unregister(AF_PHONET);
370         dev_remove_pack(&phonet_packet_type);
371         phonet_device_exit();
372 }
373
374 module_init(phonet_init);
375 module_exit(phonet_exit);
376 MODULE_DESCRIPTION("Phonet protocol stack for Linux");
377 MODULE_LICENSE("GPL");