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