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 /* Transport protocol registration */
37 static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
38 static DEFINE_SPINLOCK(proto_tab_lock);
40 static struct phonet_protocol *phonet_proto_get(int protocol)
42 struct phonet_protocol *pp;
44 if (protocol >= PHONET_NPROTO)
47 spin_lock(&proto_tab_lock);
48 pp = proto_tab[protocol];
49 if (pp && !try_module_get(pp->prot->owner))
51 spin_unlock(&proto_tab_lock);
56 static inline void phonet_proto_put(struct phonet_protocol *pp)
58 module_put(pp->prot->owner);
61 /* protocol family functions */
63 static int pn_socket_create(struct net *net, struct socket *sock, int protocol)
67 struct phonet_protocol *pnp;
70 if (!capable(CAP_SYS_ADMIN))
74 /* Default protocol selection */
77 protocol = PN_PROTO_PHONET;
80 protocol = PN_PROTO_PIPE;
83 return -EPROTONOSUPPORT;
87 pnp = phonet_proto_get(protocol);
89 request_module("net-pf-%d-proto-%d", PF_PHONET, protocol) == 0)
90 pnp = phonet_proto_get(protocol);
93 return -EPROTONOSUPPORT;
94 if (sock->type != pnp->sock_type) {
95 err = -EPROTONOSUPPORT;
99 sk = sk_alloc(net, PF_PHONET, GFP_KERNEL, pnp->prot);
105 sock_init_data(sock, sk);
106 sock->state = SS_UNCONNECTED;
107 sock->ops = pnp->ops;
108 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
109 sk->sk_protocol = protocol;
113 sk->sk_prot->init(sk);
117 phonet_proto_put(pnp);
121 static struct net_proto_family phonet_proto_family = {
123 .create = pn_socket_create,
124 .owner = THIS_MODULE,
127 /* Phonet device header operations */
128 static int pn_header_create(struct sk_buff *skb, struct net_device *dev,
129 unsigned short type, const void *daddr,
130 const void *saddr, unsigned len)
132 u8 *media = skb_push(skb, 1);
134 if (type != ETH_P_PHONET)
138 saddr = dev->dev_addr;
139 *media = *(const u8 *)saddr;
143 static int pn_header_parse(const struct sk_buff *skb, unsigned char *haddr)
145 const u8 *media = skb_mac_header(skb);
150 struct header_ops phonet_header_ops = {
151 .create = pn_header_create,
152 .parse = pn_header_parse,
154 EXPORT_SYMBOL(phonet_header_ops);
157 * Prepends an ISI header and sends a datagram.
159 static int pn_send(struct sk_buff *skb, struct net_device *dev,
160 u16 dst, u16 src, u8 res, u8 irq)
162 struct phonethdr *ph;
165 if (skb->len + 2 > 0xffff /* Phonet length field limit */ ||
166 skb->len + sizeof(struct phonethdr) > dev->mtu) {
171 skb_reset_transport_header(skb);
172 WARN_ON(skb_headroom(skb) & 1); /* HW assumes word alignment */
173 skb_push(skb, sizeof(struct phonethdr));
174 skb_reset_network_header(skb);
176 ph->pn_rdev = pn_dev(dst);
177 ph->pn_sdev = pn_dev(src);
179 ph->pn_length = __cpu_to_be16(skb->len + 2 - sizeof(*ph));
180 ph->pn_robj = pn_obj(dst);
181 ph->pn_sobj = pn_obj(src);
183 skb->protocol = htons(ETH_P_PHONET);
187 if (pn_addr(src) == pn_addr(dst)) {
188 skb_reset_mac_header(skb);
189 skb->pkt_type = PACKET_LOOPBACK;
197 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
198 NULL, NULL, skb->len);
203 err = dev_queue_xmit(skb);
212 static int pn_raw_send(const void *data, int len, struct net_device *dev,
213 u16 dst, u16 src, u8 res)
215 struct sk_buff *skb = alloc_skb(MAX_PHONET_HEADER + len, GFP_ATOMIC);
219 skb_reserve(skb, MAX_PHONET_HEADER);
221 skb_copy_to_linear_data(skb, data, len);
222 return pn_send(skb, dev, dst, src, res, 1);
226 * Create a Phonet header for the skb and send it out. Returns
227 * non-zero error code if failed. The skb is freed then.
229 int pn_skb_send(struct sock *sk, struct sk_buff *skb,
230 const struct sockaddr_pn *target)
232 struct net_device *dev;
233 struct pn_sock *pn = pn_sk(sk);
236 u8 daddr = pn_sockaddr_get_addr(target), saddr = PN_NO_ADDR;
239 if (sk->sk_bound_dev_if)
240 dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
242 dev = phonet_device_get(sock_net(sk));
243 if (!dev || !(dev->flags & IFF_UP))
246 saddr = phonet_address_get(dev, daddr);
247 if (saddr == PN_NO_ADDR)
252 src = pn_object(saddr, pn_obj(src));
254 err = pn_send(skb, dev, pn_sockaddr_get_object(target),
255 src, pn_sockaddr_get_resource(target), 0);
265 EXPORT_SYMBOL(pn_skb_send);
267 /* Do not send an error message in response to an error message */
268 static inline int can_respond(struct sk_buff *skb)
270 const struct phonethdr *ph;
271 const struct phonetmsg *pm;
274 if (!pskb_may_pull(skb, 3))
278 if (phonet_address_get(skb->dev, ph->pn_rdev) != ph->pn_rdev)
279 return 0; /* we are not the destination */
280 if (ph->pn_res == PN_PREFIX && !pskb_may_pull(skb, 5))
282 if (ph->pn_res == PN_COMMGR) /* indications */
285 ph = pn_hdr(skb); /* re-acquires the pointer */
287 if (pm->pn_msg_id != PN_COMMON_MESSAGE)
289 submsg_id = (ph->pn_res == PN_PREFIX)
290 ? pm->pn_e_submsg_id : pm->pn_submsg_id;
291 if (submsg_id != PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP &&
292 pm->pn_e_submsg_id != PN_COMM_SERVICE_NOT_IDENTIFIED_RESP)
297 static int send_obj_unreachable(struct sk_buff *rskb)
299 const struct phonethdr *oph = pn_hdr(rskb);
300 const struct phonetmsg *opm = pn_msg(rskb);
301 struct phonetmsg resp;
303 memset(&resp, 0, sizeof(resp));
304 resp.pn_trans_id = opm->pn_trans_id;
305 resp.pn_msg_id = PN_COMMON_MESSAGE;
306 if (oph->pn_res == PN_PREFIX) {
307 resp.pn_e_res_id = opm->pn_e_res_id;
308 resp.pn_e_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
309 resp.pn_e_orig_msg_id = opm->pn_msg_id;
310 resp.pn_e_status = 0;
312 resp.pn_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
313 resp.pn_orig_msg_id = opm->pn_msg_id;
316 return pn_raw_send(&resp, sizeof(resp), rskb->dev,
317 pn_object(oph->pn_sdev, oph->pn_sobj),
318 pn_object(oph->pn_rdev, oph->pn_robj),
322 static int send_reset_indications(struct sk_buff *rskb)
324 struct phonethdr *oph = pn_hdr(rskb);
325 static const u8 data[4] = {
326 0x00 /* trans ID */, 0x10 /* subscribe msg */,
327 0x00 /* subscription count */, 0x00 /* dummy */
330 return pn_raw_send(data, sizeof(data), rskb->dev,
331 pn_object(oph->pn_sdev, 0x00),
332 pn_object(oph->pn_rdev, oph->pn_robj),
337 /* packet type functions */
340 * Stuff received packets to associated sockets.
341 * On error, returns non-zero and releases the skb.
343 static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
344 struct packet_type *pkttype,
345 struct net_device *orig_dev)
347 struct phonethdr *ph;
349 struct sockaddr_pn sa;
352 /* check we have at least a full Phonet header */
353 if (!pskb_pull(skb, sizeof(struct phonethdr)))
356 /* check that the advertised length is correct */
358 len = get_unaligned_be16(&ph->pn_length);
362 if ((len > skb->len) || pskb_trim(skb, len))
364 skb_reset_transport_header(skb);
366 pn_skb_get_dst_sockaddr(skb, &sa);
367 if (pn_sockaddr_get_addr(&sa) == 0)
368 goto out; /* currently, we cannot be device 0 */
370 sk = pn_find_sock_by_sa(dev_net(dev), &sa);
372 if (can_respond(skb)) {
373 send_obj_unreachable(skb);
374 send_reset_indications(skb);
379 /* Push data to the socket (or other sockets connected to it). */
380 return sk_receive_skb(sk, skb, 0);
387 static struct packet_type phonet_packet_type = {
388 .type = __constant_htons(ETH_P_PHONET),
393 int __init_or_module phonet_proto_register(int protocol,
394 struct phonet_protocol *pp)
398 if (protocol >= PHONET_NPROTO)
401 err = proto_register(pp->prot, 1);
405 spin_lock(&proto_tab_lock);
406 if (proto_tab[protocol])
409 proto_tab[protocol] = pp;
410 spin_unlock(&proto_tab_lock);
414 EXPORT_SYMBOL(phonet_proto_register);
416 void phonet_proto_unregister(int protocol, struct phonet_protocol *pp)
418 spin_lock(&proto_tab_lock);
419 BUG_ON(proto_tab[protocol] != pp);
420 proto_tab[protocol] = NULL;
421 spin_unlock(&proto_tab_lock);
422 proto_unregister(pp->prot);
424 EXPORT_SYMBOL(phonet_proto_unregister);
426 /* Module registration */
427 static int __init phonet_init(void)
431 err = sock_register(&phonet_proto_family);
434 "phonet protocol family initialization failed\n");
438 phonet_device_init();
439 dev_add_pack(&phonet_packet_type);
440 phonet_netlink_register();
441 phonet_sysctl_init();
443 err = isi_register();
449 phonet_sysctl_exit();
450 sock_unregister(PF_PHONET);
451 dev_remove_pack(&phonet_packet_type);
452 phonet_device_exit();
456 static void __exit phonet_exit(void)
459 phonet_sysctl_exit();
460 sock_unregister(PF_PHONET);
461 dev_remove_pack(&phonet_packet_type);
462 phonet_device_exit();
465 module_init(phonet_init);
466 module_exit(phonet_exit);
467 MODULE_DESCRIPTION("Phonet protocol stack for Linux");
468 MODULE_LICENSE("GPL");
469 MODULE_ALIAS_NETPROTO(PF_PHONET);