4 * Phonet protocols family
6 * Copyright (C) 2008 Nokia Corporation.
8 * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9 * Original author: Sakari Ailus <sakari.ailus@nokia.com>
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.
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.
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
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <asm/unaligned.h>
31 #include <linux/if_phonet.h>
32 #include <linux/phonet.h>
33 #include <net/phonet/phonet.h>
34 #include <net/phonet/pn_dev.h>
36 static struct net_proto_family phonet_proto_family;
37 static struct phonet_protocol *phonet_proto_get(int protocol);
39 static inline void phonet_proto_put(struct phonet_protocol *pp)
41 module_put(pp->prot->owner);
44 /* protocol family functions */
46 static int pn_socket_create(struct net *net, struct socket *sock, int protocol)
50 struct phonet_protocol *pnp;
56 if (!capable(CAP_SYS_ADMIN))
60 /* Default protocol selection */
63 protocol = PN_PROTO_PHONET;
66 protocol = PN_PROTO_PIPE;
69 return -EPROTONOSUPPORT;
73 pnp = phonet_proto_get(protocol);
75 request_module("net-pf-%d-proto-%d", PF_PHONET, protocol) == 0)
76 pnp = phonet_proto_get(protocol);
79 return -EPROTONOSUPPORT;
80 if (sock->type != pnp->sock_type) {
81 err = -EPROTONOSUPPORT;
85 sk = sk_alloc(net, PF_PHONET, GFP_KERNEL, pnp->prot);
91 sock_init_data(sock, sk);
92 sock->state = SS_UNCONNECTED;
94 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
95 sk->sk_protocol = protocol;
99 sk->sk_prot->init(sk);
103 phonet_proto_put(pnp);
107 static struct net_proto_family phonet_proto_family = {
109 .create = pn_socket_create,
110 .owner = THIS_MODULE,
113 /* Phonet device header operations */
114 static int pn_header_create(struct sk_buff *skb, struct net_device *dev,
115 unsigned short type, const void *daddr,
116 const void *saddr, unsigned len)
118 u8 *media = skb_push(skb, 1);
120 if (type != ETH_P_PHONET)
124 saddr = dev->dev_addr;
125 *media = *(const u8 *)saddr;
129 static int pn_header_parse(const struct sk_buff *skb, unsigned char *haddr)
131 const u8 *media = skb_mac_header(skb);
136 struct header_ops phonet_header_ops = {
137 .create = pn_header_create,
138 .parse = pn_header_parse,
140 EXPORT_SYMBOL(phonet_header_ops);
143 * Prepends an ISI header and sends a datagram.
145 static int pn_send(struct sk_buff *skb, struct net_device *dev,
146 u16 dst, u16 src, u8 res, u8 irq)
148 struct phonethdr *ph;
151 if (skb->len + 2 > 0xffff) {
152 /* Phonet length field would overflow */
157 skb_reset_transport_header(skb);
158 WARN_ON(skb_headroom(skb) & 1); /* HW assumes word alignment */
159 skb_push(skb, sizeof(struct phonethdr));
160 skb_reset_network_header(skb);
162 ph->pn_rdev = pn_dev(dst);
163 ph->pn_sdev = pn_dev(src);
165 ph->pn_length = __cpu_to_be16(skb->len + 2 - sizeof(*ph));
166 ph->pn_robj = pn_obj(dst);
167 ph->pn_sobj = pn_obj(src);
169 skb->protocol = htons(ETH_P_PHONET);
173 if (pn_addr(src) == pn_addr(dst)) {
174 skb_reset_mac_header(skb);
175 skb->pkt_type = PACKET_LOOPBACK;
183 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
184 NULL, NULL, skb->len);
189 err = dev_queue_xmit(skb);
198 static int pn_raw_send(const void *data, int len, struct net_device *dev,
199 u16 dst, u16 src, u8 res)
201 struct sk_buff *skb = alloc_skb(MAX_PHONET_HEADER + len, GFP_ATOMIC);
205 skb_reserve(skb, MAX_PHONET_HEADER);
207 skb_copy_to_linear_data(skb, data, len);
208 return pn_send(skb, dev, dst, src, res, 1);
212 * Create a Phonet header for the skb and send it out. Returns
213 * non-zero error code if failed. The skb is freed then.
215 int pn_skb_send(struct sock *sk, struct sk_buff *skb,
216 const struct sockaddr_pn *target)
218 struct net_device *dev;
219 struct pn_sock *pn = pn_sk(sk);
222 u8 daddr = pn_sockaddr_get_addr(target), saddr = PN_NO_ADDR;
225 if (sk->sk_bound_dev_if)
226 dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
228 dev = phonet_device_get(sock_net(sk));
229 if (!dev || !(dev->flags & IFF_UP))
232 saddr = phonet_address_get(dev, daddr);
233 if (saddr == PN_NO_ADDR)
238 src = pn_object(saddr, pn_obj(src));
240 err = pn_send(skb, dev, pn_sockaddr_get_object(target),
241 src, pn_sockaddr_get_resource(target), 0);
251 EXPORT_SYMBOL(pn_skb_send);
253 /* Do not send an error message in response to an error message */
254 static inline int can_respond(struct sk_buff *skb)
256 const struct phonethdr *ph;
257 const struct phonetmsg *pm;
260 if (!pskb_may_pull(skb, 3))
264 if (phonet_address_get(skb->dev, ph->pn_rdev) != ph->pn_rdev)
265 return 0; /* we are not the destination */
266 if (ph->pn_res == PN_PREFIX && !pskb_may_pull(skb, 5))
268 if (ph->pn_res == PN_COMMGR) /* indications */
271 ph = pn_hdr(skb); /* re-acquires the pointer */
273 if (pm->pn_msg_id != PN_COMMON_MESSAGE)
275 submsg_id = (ph->pn_res == PN_PREFIX)
276 ? pm->pn_e_submsg_id : pm->pn_submsg_id;
277 if (submsg_id != PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP &&
278 pm->pn_e_submsg_id != PN_COMM_SERVICE_NOT_IDENTIFIED_RESP)
283 static int send_obj_unreachable(struct sk_buff *rskb)
285 const struct phonethdr *oph = pn_hdr(rskb);
286 const struct phonetmsg *opm = pn_msg(rskb);
287 struct phonetmsg resp;
289 memset(&resp, 0, sizeof(resp));
290 resp.pn_trans_id = opm->pn_trans_id;
291 resp.pn_msg_id = PN_COMMON_MESSAGE;
292 if (oph->pn_res == PN_PREFIX) {
293 resp.pn_e_res_id = opm->pn_e_res_id;
294 resp.pn_e_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
295 resp.pn_e_orig_msg_id = opm->pn_msg_id;
296 resp.pn_e_status = 0;
298 resp.pn_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
299 resp.pn_orig_msg_id = opm->pn_msg_id;
302 return pn_raw_send(&resp, sizeof(resp), rskb->dev,
303 pn_object(oph->pn_sdev, oph->pn_sobj),
304 pn_object(oph->pn_rdev, oph->pn_robj),
308 static int send_reset_indications(struct sk_buff *rskb)
310 struct phonethdr *oph = pn_hdr(rskb);
311 static const u8 data[4] = {
312 0x00 /* trans ID */, 0x10 /* subscribe msg */,
313 0x00 /* subscription count */, 0x00 /* dummy */
316 return pn_raw_send(data, sizeof(data), rskb->dev,
317 pn_object(oph->pn_sdev, 0x00),
318 pn_object(oph->pn_rdev, oph->pn_robj),
323 /* packet type functions */
326 * Stuff received packets to associated sockets.
327 * On error, returns non-zero and releases the skb.
329 static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
330 struct packet_type *pkttype,
331 struct net_device *orig_dev)
333 struct phonethdr *ph;
335 struct sockaddr_pn sa;
338 if (dev_net(dev) != &init_net)
341 /* check we have at least a full Phonet header */
342 if (!pskb_pull(skb, sizeof(struct phonethdr)))
345 /* check that the advertised length is correct */
347 len = get_unaligned_be16(&ph->pn_length);
351 if ((len > skb->len) || pskb_trim(skb, len))
353 skb_reset_transport_header(skb);
355 pn_skb_get_dst_sockaddr(skb, &sa);
356 if (pn_sockaddr_get_addr(&sa) == 0)
357 goto out; /* currently, we cannot be device 0 */
359 sk = pn_find_sock_by_sa(&sa);
361 if (can_respond(skb)) {
362 send_obj_unreachable(skb);
363 send_reset_indications(skb);
368 /* Push data to the socket (or other sockets connected to it). */
369 return sk_receive_skb(sk, skb, 0);
376 static struct packet_type phonet_packet_type = {
377 .type = __constant_htons(ETH_P_PHONET),
382 /* Transport protocol registration */
383 static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
384 static DEFINE_SPINLOCK(proto_tab_lock);
386 int __init_or_module phonet_proto_register(int protocol,
387 struct phonet_protocol *pp)
391 if (protocol >= PHONET_NPROTO)
394 err = proto_register(pp->prot, 1);
398 spin_lock(&proto_tab_lock);
399 if (proto_tab[protocol])
402 proto_tab[protocol] = pp;
403 spin_unlock(&proto_tab_lock);
407 EXPORT_SYMBOL(phonet_proto_register);
409 void phonet_proto_unregister(int protocol, struct phonet_protocol *pp)
411 spin_lock(&proto_tab_lock);
412 BUG_ON(proto_tab[protocol] != pp);
413 proto_tab[protocol] = NULL;
414 spin_unlock(&proto_tab_lock);
415 proto_unregister(pp->prot);
417 EXPORT_SYMBOL(phonet_proto_unregister);
419 static struct phonet_protocol *phonet_proto_get(int protocol)
421 struct phonet_protocol *pp;
423 if (protocol >= PHONET_NPROTO)
426 spin_lock(&proto_tab_lock);
427 pp = proto_tab[protocol];
428 if (pp && !try_module_get(pp->prot->owner))
430 spin_unlock(&proto_tab_lock);
435 /* Module registration */
436 static int __init phonet_init(void)
440 err = sock_register(&phonet_proto_family);
443 "phonet protocol family initialization failed\n");
447 phonet_device_init();
448 dev_add_pack(&phonet_packet_type);
449 phonet_netlink_register();
450 phonet_sysctl_init();
452 err = isi_register();
458 phonet_sysctl_exit();
459 sock_unregister(PF_PHONET);
460 dev_remove_pack(&phonet_packet_type);
461 phonet_device_exit();
465 static void __exit phonet_exit(void)
468 phonet_sysctl_exit();
469 sock_unregister(PF_PHONET);
470 dev_remove_pack(&phonet_packet_type);
471 phonet_device_exit();
474 module_init(phonet_init);
475 module_exit(phonet_exit);
476 MODULE_DESCRIPTION("Phonet protocol stack for Linux");
477 MODULE_LICENSE("GPL");
478 MODULE_ALIAS_NETPROTO(PF_PHONET);