Phonet: don't bother with transaction IDs (especially for indications)
[linux-3.10.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 <linux/slab.h>
29 #include <asm/unaligned.h>
30 #include <net/sock.h>
31
32 #include <linux/if_phonet.h>
33 #include <linux/phonet.h>
34 #include <net/phonet/phonet.h>
35 #include <net/phonet/pn_dev.h>
36
37 /* Transport protocol registration */
38 static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
39
40 static struct phonet_protocol *phonet_proto_get(unsigned int protocol)
41 {
42         struct phonet_protocol *pp;
43
44         if (protocol >= PHONET_NPROTO)
45                 return NULL;
46
47         rcu_read_lock();
48         pp = rcu_dereference(proto_tab[protocol]);
49         if (pp && !try_module_get(pp->prot->owner))
50                 pp = NULL;
51         rcu_read_unlock();
52
53         return pp;
54 }
55
56 static inline void phonet_proto_put(struct phonet_protocol *pp)
57 {
58         module_put(pp->prot->owner);
59 }
60
61 /* protocol family functions */
62
63 static int pn_socket_create(struct net *net, struct socket *sock, int protocol,
64                             int kern)
65 {
66         struct sock *sk;
67         struct pn_sock *pn;
68         struct phonet_protocol *pnp;
69         int err;
70
71         if (!capable(CAP_SYS_ADMIN))
72                 return -EPERM;
73
74         if (protocol == 0) {
75                 /* Default protocol selection */
76                 switch (sock->type) {
77                 case SOCK_DGRAM:
78                         protocol = PN_PROTO_PHONET;
79                         break;
80                 case SOCK_SEQPACKET:
81                         protocol = PN_PROTO_PIPE;
82                         break;
83                 default:
84                         return -EPROTONOSUPPORT;
85                 }
86         }
87
88         pnp = phonet_proto_get(protocol);
89         if (pnp == NULL &&
90             request_module("net-pf-%d-proto-%d", PF_PHONET, protocol) == 0)
91                 pnp = phonet_proto_get(protocol);
92
93         if (pnp == NULL)
94                 return -EPROTONOSUPPORT;
95         if (sock->type != pnp->sock_type) {
96                 err = -EPROTONOSUPPORT;
97                 goto out;
98         }
99
100         sk = sk_alloc(net, PF_PHONET, GFP_KERNEL, pnp->prot);
101         if (sk == NULL) {
102                 err = -ENOMEM;
103                 goto out;
104         }
105
106         sock_init_data(sock, sk);
107         sock->state = SS_UNCONNECTED;
108         sock->ops = pnp->ops;
109         sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
110         sk->sk_protocol = protocol;
111         pn = pn_sk(sk);
112         pn->sobject = 0;
113         pn->dobject = 0;
114         pn->resource = 0;
115         sk->sk_prot->init(sk);
116         err = 0;
117
118 out:
119         phonet_proto_put(pnp);
120         return err;
121 }
122
123 static const struct net_proto_family phonet_proto_family = {
124         .family = PF_PHONET,
125         .create = pn_socket_create,
126         .owner = THIS_MODULE,
127 };
128
129 /* Phonet device header operations */
130 static int pn_header_create(struct sk_buff *skb, struct net_device *dev,
131                                 unsigned short type, const void *daddr,
132                                 const void *saddr, unsigned len)
133 {
134         u8 *media = skb_push(skb, 1);
135
136         if (type != ETH_P_PHONET)
137                 return -1;
138
139         if (!saddr)
140                 saddr = dev->dev_addr;
141         *media = *(const u8 *)saddr;
142         return 1;
143 }
144
145 static int pn_header_parse(const struct sk_buff *skb, unsigned char *haddr)
146 {
147         const u8 *media = skb_mac_header(skb);
148         *haddr = *media;
149         return 1;
150 }
151
152 struct header_ops phonet_header_ops = {
153         .create = pn_header_create,
154         .parse = pn_header_parse,
155 };
156 EXPORT_SYMBOL(phonet_header_ops);
157
158 /*
159  * Prepends an ISI header and sends a datagram.
160  */
161 static int pn_send(struct sk_buff *skb, struct net_device *dev,
162                         u16 dst, u16 src, u8 res, u8 irq)
163 {
164         struct phonethdr *ph;
165         int err;
166
167         if (skb->len + 2 > 0xffff /* Phonet length field limit */ ||
168             skb->len + sizeof(struct phonethdr) > dev->mtu) {
169                 err = -EMSGSIZE;
170                 goto drop;
171         }
172
173         /* Broadcast sending is not implemented */
174         if (pn_addr(dst) == PNADDR_BROADCAST) {
175                 err = -EOPNOTSUPP;
176                 goto drop;
177         }
178
179         skb_reset_transport_header(skb);
180         WARN_ON(skb_headroom(skb) & 1); /* HW assumes word alignment */
181         skb_push(skb, sizeof(struct phonethdr));
182         skb_reset_network_header(skb);
183         ph = pn_hdr(skb);
184         ph->pn_rdev = pn_dev(dst);
185         ph->pn_sdev = pn_dev(src);
186         ph->pn_res = res;
187         ph->pn_length = __cpu_to_be16(skb->len + 2 - sizeof(*ph));
188         ph->pn_robj = pn_obj(dst);
189         ph->pn_sobj = pn_obj(src);
190
191         skb->protocol = htons(ETH_P_PHONET);
192         skb->priority = 0;
193         skb->dev = dev;
194
195         if (skb->pkt_type == PACKET_LOOPBACK) {
196                 skb_reset_mac_header(skb);
197                 skb_orphan(skb);
198                 if (irq)
199                         netif_rx(skb);
200                 else
201                         netif_rx_ni(skb);
202                 err = 0;
203         } else {
204                 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
205                                         NULL, NULL, skb->len);
206                 if (err < 0) {
207                         err = -EHOSTUNREACH;
208                         goto drop;
209                 }
210                 err = dev_queue_xmit(skb);
211         }
212
213         return err;
214 drop:
215         kfree_skb(skb);
216         return err;
217 }
218
219 static int pn_raw_send(const void *data, int len, struct net_device *dev,
220                         u16 dst, u16 src, u8 res)
221 {
222         struct sk_buff *skb = alloc_skb(MAX_PHONET_HEADER + len, GFP_ATOMIC);
223         if (skb == NULL)
224                 return -ENOMEM;
225
226         if (phonet_address_lookup(dev_net(dev), pn_addr(dst)) == 0)
227                 skb->pkt_type = PACKET_LOOPBACK;
228
229         skb_reserve(skb, MAX_PHONET_HEADER);
230         __skb_put(skb, len);
231         skb_copy_to_linear_data(skb, data, len);
232         return pn_send(skb, dev, dst, src, res, 1);
233 }
234
235 /*
236  * Create a Phonet header for the skb and send it out. Returns
237  * non-zero error code if failed. The skb is freed then.
238  */
239 int pn_skb_send(struct sock *sk, struct sk_buff *skb,
240                 const struct sockaddr_pn *target)
241 {
242         struct net *net = sock_net(sk);
243         struct net_device *dev;
244         struct pn_sock *pn = pn_sk(sk);
245         int err;
246         u16 src, dst;
247         u8 daddr, saddr, res;
248
249         src = pn->sobject;
250         if (target != NULL) {
251                 dst = pn_sockaddr_get_object(target);
252                 res = pn_sockaddr_get_resource(target);
253         } else {
254                 dst = pn->dobject;
255                 res = pn->resource;
256         }
257         daddr = pn_addr(dst);
258
259         err = -EHOSTUNREACH;
260         if (sk->sk_bound_dev_if)
261                 dev = dev_get_by_index(net, sk->sk_bound_dev_if);
262         else if (phonet_address_lookup(net, daddr) == 0) {
263                 dev = phonet_device_get(net);
264                 skb->pkt_type = PACKET_LOOPBACK;
265         } else if (pn_sockaddr_get_object(target) == 0) {
266                 /* Resource routing (small race until phonet_rcv()) */
267                 struct sock *sk = pn_find_sock_by_res(net,
268                                                         target->spn_resource);
269                 if (sk) {
270                         sock_put(sk);
271                         dev = phonet_device_get(net);
272                         skb->pkt_type = PACKET_LOOPBACK;
273                 } else
274                         dev = phonet_route_output(net, daddr);
275         } else
276                 dev = phonet_route_output(net, daddr);
277
278         if (!dev || !(dev->flags & IFF_UP))
279                 goto drop;
280
281         saddr = phonet_address_get(dev, daddr);
282         if (saddr == PN_NO_ADDR)
283                 goto drop;
284
285         if (!pn_addr(src))
286                 src = pn_object(saddr, pn_obj(src));
287
288         err = pn_send(skb, dev, dst, src, res, 0);
289         dev_put(dev);
290         return err;
291
292 drop:
293         kfree_skb(skb);
294         if (dev)
295                 dev_put(dev);
296         return err;
297 }
298 EXPORT_SYMBOL(pn_skb_send);
299
300 /* Do not send an error message in response to an error message */
301 static inline int can_respond(struct sk_buff *skb)
302 {
303         const struct phonethdr *ph;
304         const struct phonetmsg *pm;
305         u8 submsg_id;
306
307         if (!pskb_may_pull(skb, 3))
308                 return 0;
309
310         ph = pn_hdr(skb);
311         if (ph->pn_res == PN_PREFIX && !pskb_may_pull(skb, 5))
312                 return 0;
313         if (ph->pn_res == PN_COMMGR) /* indications */
314                 return 0;
315
316         ph = pn_hdr(skb); /* re-acquires the pointer */
317         pm = pn_msg(skb);
318         if (pm->pn_msg_id != PN_COMMON_MESSAGE)
319                 return 1;
320         submsg_id = (ph->pn_res == PN_PREFIX)
321                 ? pm->pn_e_submsg_id : pm->pn_submsg_id;
322         if (submsg_id != PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP &&
323                 pm->pn_e_submsg_id != PN_COMM_SERVICE_NOT_IDENTIFIED_RESP)
324                 return 1;
325         return 0;
326 }
327
328 static int send_obj_unreachable(struct sk_buff *rskb)
329 {
330         const struct phonethdr *oph = pn_hdr(rskb);
331         const struct phonetmsg *opm = pn_msg(rskb);
332         struct phonetmsg resp;
333
334         memset(&resp, 0, sizeof(resp));
335         resp.pn_trans_id = opm->pn_trans_id;
336         resp.pn_msg_id = PN_COMMON_MESSAGE;
337         if (oph->pn_res == PN_PREFIX) {
338                 resp.pn_e_res_id = opm->pn_e_res_id;
339                 resp.pn_e_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
340                 resp.pn_e_orig_msg_id = opm->pn_msg_id;
341                 resp.pn_e_status = 0;
342         } else {
343                 resp.pn_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
344                 resp.pn_orig_msg_id = opm->pn_msg_id;
345                 resp.pn_status = 0;
346         }
347         return pn_raw_send(&resp, sizeof(resp), rskb->dev,
348                                 pn_object(oph->pn_sdev, oph->pn_sobj),
349                                 pn_object(oph->pn_rdev, oph->pn_robj),
350                                 oph->pn_res);
351 }
352
353 static int send_reset_indications(struct sk_buff *rskb)
354 {
355         struct phonethdr *oph = pn_hdr(rskb);
356         static const u8 data[4] = {
357                 0x00 /* trans ID */, 0x10 /* subscribe msg */,
358                 0x00 /* subscription count */, 0x00 /* dummy */
359         };
360
361         return pn_raw_send(data, sizeof(data), rskb->dev,
362                                 pn_object(oph->pn_sdev, 0x00),
363                                 pn_object(oph->pn_rdev, oph->pn_robj),
364                                 PN_COMMGR);
365 }
366
367
368 /* packet type functions */
369
370 /*
371  * Stuff received packets to associated sockets.
372  * On error, returns non-zero and releases the skb.
373  */
374 static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
375                         struct packet_type *pkttype,
376                         struct net_device *orig_dev)
377 {
378         struct net *net = dev_net(dev);
379         struct phonethdr *ph;
380         struct sockaddr_pn sa;
381         u16 len;
382
383         /* check we have at least a full Phonet header */
384         if (!pskb_pull(skb, sizeof(struct phonethdr)))
385                 goto out;
386
387         /* check that the advertised length is correct */
388         ph = pn_hdr(skb);
389         len = get_unaligned_be16(&ph->pn_length);
390         if (len < 2)
391                 goto out;
392         len -= 2;
393         if ((len > skb->len) || pskb_trim(skb, len))
394                 goto out;
395         skb_reset_transport_header(skb);
396
397         pn_skb_get_dst_sockaddr(skb, &sa);
398
399         /* check if this is broadcasted */
400         if (pn_sockaddr_get_addr(&sa) == PNADDR_BROADCAST) {
401                 pn_deliver_sock_broadcast(net, skb);
402                 goto out;
403         }
404
405         /* resource routing */
406         if (pn_sockaddr_get_object(&sa) == 0) {
407                 struct sock *sk = pn_find_sock_by_res(net, sa.spn_resource);
408                 if (sk)
409                         return sk_receive_skb(sk, skb, 0);
410         }
411
412         /* check if we are the destination */
413         if (phonet_address_lookup(net, pn_sockaddr_get_addr(&sa)) == 0) {
414                 /* Phonet packet input */
415                 struct sock *sk = pn_find_sock_by_sa(net, &sa);
416
417                 if (sk)
418                         return sk_receive_skb(sk, skb, 0);
419
420                 if (can_respond(skb)) {
421                         send_obj_unreachable(skb);
422                         send_reset_indications(skb);
423                 }
424         } else if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
425                 goto out; /* Race between address deletion and loopback */
426         else {
427                 /* Phonet packet routing */
428                 struct net_device *out_dev;
429
430                 out_dev = phonet_route_output(net, pn_sockaddr_get_addr(&sa));
431                 if (!out_dev) {
432                         LIMIT_NETDEBUG(KERN_WARNING"No Phonet route to %02X\n",
433                                         pn_sockaddr_get_addr(&sa));
434                         goto out;
435                 }
436
437                 __skb_push(skb, sizeof(struct phonethdr));
438                 skb->dev = out_dev;
439                 if (out_dev == dev) {
440                         LIMIT_NETDEBUG(KERN_ERR"Phonet loop to %02X on %s\n",
441                                         pn_sockaddr_get_addr(&sa), dev->name);
442                         goto out_dev;
443                 }
444                 /* Some drivers (e.g. TUN) do not allocate HW header space */
445                 if (skb_cow_head(skb, out_dev->hard_header_len))
446                         goto out_dev;
447
448                 if (dev_hard_header(skb, out_dev, ETH_P_PHONET, NULL, NULL,
449                                         skb->len) < 0)
450                         goto out_dev;
451                 dev_queue_xmit(skb);
452                 dev_put(out_dev);
453                 return NET_RX_SUCCESS;
454 out_dev:
455                 dev_put(out_dev);
456         }
457
458 out:
459         kfree_skb(skb);
460         return NET_RX_DROP;
461 }
462
463 static struct packet_type phonet_packet_type __read_mostly = {
464         .type = cpu_to_be16(ETH_P_PHONET),
465         .func = phonet_rcv,
466 };
467
468 static DEFINE_MUTEX(proto_tab_lock);
469
470 int __init_or_module phonet_proto_register(unsigned int protocol,
471                                                 struct phonet_protocol *pp)
472 {
473         int err = 0;
474
475         if (protocol >= PHONET_NPROTO)
476                 return -EINVAL;
477
478         err = proto_register(pp->prot, 1);
479         if (err)
480                 return err;
481
482         mutex_lock(&proto_tab_lock);
483         if (proto_tab[protocol])
484                 err = -EBUSY;
485         else
486                 rcu_assign_pointer(proto_tab[protocol], pp);
487         mutex_unlock(&proto_tab_lock);
488
489         return err;
490 }
491 EXPORT_SYMBOL(phonet_proto_register);
492
493 void phonet_proto_unregister(unsigned int protocol, struct phonet_protocol *pp)
494 {
495         mutex_lock(&proto_tab_lock);
496         BUG_ON(proto_tab[protocol] != pp);
497         rcu_assign_pointer(proto_tab[protocol], NULL);
498         mutex_unlock(&proto_tab_lock);
499         synchronize_rcu();
500         proto_unregister(pp->prot);
501 }
502 EXPORT_SYMBOL(phonet_proto_unregister);
503
504 /* Module registration */
505 static int __init phonet_init(void)
506 {
507         int err;
508
509         err = phonet_device_init();
510         if (err)
511                 return err;
512
513         pn_sock_init();
514         err = sock_register(&phonet_proto_family);
515         if (err) {
516                 printk(KERN_ALERT
517                         "phonet protocol family initialization failed\n");
518                 goto err_sock;
519         }
520
521         dev_add_pack(&phonet_packet_type);
522         phonet_sysctl_init();
523
524         err = isi_register();
525         if (err)
526                 goto err;
527         return 0;
528
529 err:
530         phonet_sysctl_exit();
531         sock_unregister(PF_PHONET);
532         dev_remove_pack(&phonet_packet_type);
533 err_sock:
534         phonet_device_exit();
535         return err;
536 }
537
538 static void __exit phonet_exit(void)
539 {
540         isi_unregister();
541         phonet_sysctl_exit();
542         sock_unregister(PF_PHONET);
543         dev_remove_pack(&phonet_packet_type);
544         phonet_device_exit();
545 }
546
547 module_init(phonet_init);
548 module_exit(phonet_exit);
549 MODULE_DESCRIPTION("Phonet protocol stack for Linux");
550 MODULE_LICENSE("GPL");
551 MODULE_ALIAS_NETPROTO(PF_PHONET);