1 // SPDX-License-Identifier: GPL-2.0-only
5 * Phonet protocols family
7 * Copyright (C) 2008 Nokia Corporation.
9 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
10 * RĂ©mi Denis-Courmont
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <asm/unaligned.h>
19 #include <linux/if_phonet.h>
20 #include <linux/phonet.h>
21 #include <net/phonet/phonet.h>
22 #include <net/phonet/pn_dev.h>
24 /* Transport protocol registration */
25 static const struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
27 static const struct phonet_protocol *phonet_proto_get(unsigned int protocol)
29 const struct phonet_protocol *pp;
31 if (protocol >= PHONET_NPROTO)
35 pp = rcu_dereference(proto_tab[protocol]);
36 if (pp && !try_module_get(pp->prot->owner))
43 static inline void phonet_proto_put(const struct phonet_protocol *pp)
45 module_put(pp->prot->owner);
48 /* protocol family functions */
50 static int pn_socket_create(struct net *net, struct socket *sock, int protocol,
55 const struct phonet_protocol *pnp;
58 if (!capable(CAP_SYS_ADMIN))
62 /* Default protocol selection */
65 protocol = PN_PROTO_PHONET;
68 protocol = PN_PROTO_PIPE;
71 return -EPROTONOSUPPORT;
75 pnp = phonet_proto_get(protocol);
77 request_module("net-pf-%d-proto-%d", PF_PHONET, protocol) == 0)
78 pnp = phonet_proto_get(protocol);
81 return -EPROTONOSUPPORT;
82 if (sock->type != pnp->sock_type) {
83 err = -EPROTONOSUPPORT;
87 sk = sk_alloc(net, PF_PHONET, GFP_KERNEL, pnp->prot, kern);
93 sock_init_data(sock, sk);
94 sock->state = SS_UNCONNECTED;
96 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
97 sk->sk_protocol = protocol;
102 sk->sk_prot->init(sk);
106 phonet_proto_put(pnp);
110 static const struct net_proto_family phonet_proto_family = {
112 .create = pn_socket_create,
113 .owner = THIS_MODULE,
116 /* Phonet device header operations */
117 static int pn_header_create(struct sk_buff *skb, struct net_device *dev,
118 unsigned short type, const void *daddr,
119 const void *saddr, unsigned int len)
121 u8 *media = skb_push(skb, 1);
123 if (type != ETH_P_PHONET)
127 saddr = dev->dev_addr;
128 *media = *(const u8 *)saddr;
132 static int pn_header_parse(const struct sk_buff *skb, unsigned char *haddr)
134 const u8 *media = skb_mac_header(skb);
139 const struct header_ops phonet_header_ops = {
140 .create = pn_header_create,
141 .parse = pn_header_parse,
143 EXPORT_SYMBOL(phonet_header_ops);
146 * Prepends an ISI header and sends a datagram.
148 static int pn_send(struct sk_buff *skb, struct net_device *dev,
149 u16 dst, u16 src, u8 res)
151 struct phonethdr *ph;
154 if (skb->len + 2 > 0xffff /* Phonet length field limit */ ||
155 skb->len + sizeof(struct phonethdr) > dev->mtu) {
160 /* Broadcast sending is not implemented */
161 if (pn_addr(dst) == PNADDR_BROADCAST) {
166 skb_reset_transport_header(skb);
167 WARN_ON(skb_headroom(skb) & 1); /* HW assumes word alignment */
168 skb_push(skb, sizeof(struct phonethdr));
169 skb_reset_network_header(skb);
171 ph->pn_rdev = pn_dev(dst);
172 ph->pn_sdev = pn_dev(src);
174 ph->pn_length = __cpu_to_be16(skb->len + 2 - sizeof(*ph));
175 ph->pn_robj = pn_obj(dst);
176 ph->pn_sobj = pn_obj(src);
178 skb->protocol = htons(ETH_P_PHONET);
182 if (skb->pkt_type == PACKET_LOOPBACK) {
183 skb_reset_mac_header(skb);
185 err = netif_rx(skb) ? -ENOBUFS : 0;
187 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
188 NULL, NULL, skb->len);
193 err = dev_queue_xmit(skb);
194 if (unlikely(err > 0))
195 err = net_xmit_errno(err);
204 static int pn_raw_send(const void *data, int len, struct net_device *dev,
205 u16 dst, u16 src, u8 res)
207 struct sk_buff *skb = alloc_skb(MAX_PHONET_HEADER + len, GFP_ATOMIC);
211 if (phonet_address_lookup(dev_net(dev), pn_addr(dst)) == 0)
212 skb->pkt_type = PACKET_LOOPBACK;
214 skb_reserve(skb, MAX_PHONET_HEADER);
216 skb_copy_to_linear_data(skb, data, len);
217 return pn_send(skb, dev, dst, src, res);
221 * Create a Phonet header for the skb and send it out. Returns
222 * non-zero error code if failed. The skb is freed then.
224 int pn_skb_send(struct sock *sk, struct sk_buff *skb,
225 const struct sockaddr_pn *target)
227 struct net *net = sock_net(sk);
228 struct net_device *dev;
229 struct pn_sock *pn = pn_sk(sk);
232 u8 daddr, saddr, res;
235 if (target != NULL) {
236 dst = pn_sockaddr_get_object(target);
237 res = pn_sockaddr_get_resource(target);
242 daddr = pn_addr(dst);
245 if (sk->sk_bound_dev_if)
246 dev = dev_get_by_index(net, sk->sk_bound_dev_if);
247 else if (phonet_address_lookup(net, daddr) == 0) {
248 dev = phonet_device_get(net);
249 skb->pkt_type = PACKET_LOOPBACK;
250 } else if (dst == 0) {
251 /* Resource routing (small race until phonet_rcv()) */
252 struct sock *sk = pn_find_sock_by_res(net, res);
255 dev = phonet_device_get(net);
256 skb->pkt_type = PACKET_LOOPBACK;
258 dev = phonet_route_output(net, daddr);
260 dev = phonet_route_output(net, daddr);
262 if (!dev || !(dev->flags & IFF_UP))
265 saddr = phonet_address_get(dev, daddr);
266 if (saddr == PN_NO_ADDR)
270 src = pn_object(saddr, pn_obj(src));
272 err = pn_send(skb, dev, dst, src, res);
281 EXPORT_SYMBOL(pn_skb_send);
283 /* Do not send an error message in response to an error message */
284 static inline int can_respond(struct sk_buff *skb)
286 const struct phonethdr *ph;
287 const struct phonetmsg *pm;
290 if (!pskb_may_pull(skb, 3))
294 if (ph->pn_res == PN_PREFIX && !pskb_may_pull(skb, 5))
296 if (ph->pn_res == PN_COMMGR) /* indications */
299 ph = pn_hdr(skb); /* re-acquires the pointer */
301 if (pm->pn_msg_id != PN_COMMON_MESSAGE)
303 submsg_id = (ph->pn_res == PN_PREFIX)
304 ? pm->pn_e_submsg_id : pm->pn_submsg_id;
305 if (submsg_id != PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP &&
306 pm->pn_e_submsg_id != PN_COMM_SERVICE_NOT_IDENTIFIED_RESP)
311 static int send_obj_unreachable(struct sk_buff *rskb)
313 const struct phonethdr *oph = pn_hdr(rskb);
314 const struct phonetmsg *opm = pn_msg(rskb);
315 struct phonetmsg resp;
317 memset(&resp, 0, sizeof(resp));
318 resp.pn_trans_id = opm->pn_trans_id;
319 resp.pn_msg_id = PN_COMMON_MESSAGE;
320 if (oph->pn_res == PN_PREFIX) {
321 resp.pn_e_res_id = opm->pn_e_res_id;
322 resp.pn_e_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
323 resp.pn_e_orig_msg_id = opm->pn_msg_id;
324 resp.pn_e_status = 0;
326 resp.pn_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
327 resp.pn_orig_msg_id = opm->pn_msg_id;
330 return pn_raw_send(&resp, sizeof(resp), rskb->dev,
331 pn_object(oph->pn_sdev, oph->pn_sobj),
332 pn_object(oph->pn_rdev, oph->pn_robj),
336 static int send_reset_indications(struct sk_buff *rskb)
338 struct phonethdr *oph = pn_hdr(rskb);
339 static const u8 data[4] = {
340 0x00 /* trans ID */, 0x10 /* subscribe msg */,
341 0x00 /* subscription count */, 0x00 /* dummy */
344 return pn_raw_send(data, sizeof(data), rskb->dev,
345 pn_object(oph->pn_sdev, 0x00),
346 pn_object(oph->pn_rdev, oph->pn_robj),
351 /* packet type functions */
354 * Stuff received packets to associated sockets.
355 * On error, returns non-zero and releases the skb.
357 static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
358 struct packet_type *pkttype,
359 struct net_device *orig_dev)
361 struct net *net = dev_net(dev);
362 struct phonethdr *ph;
363 struct sockaddr_pn sa;
366 skb = skb_share_check(skb, GFP_ATOMIC);
370 /* check we have at least a full Phonet header */
371 if (!pskb_pull(skb, sizeof(struct phonethdr)))
374 /* check that the advertised length is correct */
376 len = get_unaligned_be16(&ph->pn_length);
380 if ((len > skb->len) || pskb_trim(skb, len))
382 skb_reset_transport_header(skb);
384 pn_skb_get_dst_sockaddr(skb, &sa);
386 /* check if this is broadcasted */
387 if (pn_sockaddr_get_addr(&sa) == PNADDR_BROADCAST) {
388 pn_deliver_sock_broadcast(net, skb);
392 /* resource routing */
393 if (pn_sockaddr_get_object(&sa) == 0) {
394 struct sock *sk = pn_find_sock_by_res(net, sa.spn_resource);
396 return sk_receive_skb(sk, skb, 0);
399 /* check if we are the destination */
400 if (phonet_address_lookup(net, pn_sockaddr_get_addr(&sa)) == 0) {
401 /* Phonet packet input */
402 struct sock *sk = pn_find_sock_by_sa(net, &sa);
405 return sk_receive_skb(sk, skb, 0);
407 if (can_respond(skb)) {
408 send_obj_unreachable(skb);
409 send_reset_indications(skb);
411 } else if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
412 goto out; /* Race between address deletion and loopback */
414 /* Phonet packet routing */
415 struct net_device *out_dev;
417 out_dev = phonet_route_output(net, pn_sockaddr_get_addr(&sa));
419 net_dbg_ratelimited("No Phonet route to %02X\n",
420 pn_sockaddr_get_addr(&sa));
424 __skb_push(skb, sizeof(struct phonethdr));
426 if (out_dev == dev) {
427 net_dbg_ratelimited("Phonet loop to %02X on %s\n",
428 pn_sockaddr_get_addr(&sa),
432 /* Some drivers (e.g. TUN) do not allocate HW header space */
433 if (skb_cow_head(skb, out_dev->hard_header_len))
436 if (dev_hard_header(skb, out_dev, ETH_P_PHONET, NULL, NULL,
441 return NET_RX_SUCCESS;
451 static struct packet_type phonet_packet_type __read_mostly = {
452 .type = cpu_to_be16(ETH_P_PHONET),
456 static DEFINE_MUTEX(proto_tab_lock);
458 int __init_or_module phonet_proto_register(unsigned int protocol,
459 const struct phonet_protocol *pp)
463 if (protocol >= PHONET_NPROTO)
466 err = proto_register(pp->prot, 1);
470 mutex_lock(&proto_tab_lock);
471 if (proto_tab[protocol])
474 rcu_assign_pointer(proto_tab[protocol], pp);
475 mutex_unlock(&proto_tab_lock);
479 EXPORT_SYMBOL(phonet_proto_register);
481 void phonet_proto_unregister(unsigned int protocol,
482 const struct phonet_protocol *pp)
484 mutex_lock(&proto_tab_lock);
485 BUG_ON(proto_tab[protocol] != pp);
486 RCU_INIT_POINTER(proto_tab[protocol], NULL);
487 mutex_unlock(&proto_tab_lock);
489 proto_unregister(pp->prot);
491 EXPORT_SYMBOL(phonet_proto_unregister);
493 /* Module registration */
494 static int __init phonet_init(void)
498 err = phonet_device_init();
503 err = sock_register(&phonet_proto_family);
506 "phonet protocol family initialization failed\n");
510 dev_add_pack(&phonet_packet_type);
511 phonet_sysctl_init();
513 err = isi_register();
519 phonet_sysctl_exit();
520 sock_unregister(PF_PHONET);
521 dev_remove_pack(&phonet_packet_type);
523 phonet_device_exit();
527 static void __exit phonet_exit(void)
530 phonet_sysctl_exit();
531 sock_unregister(PF_PHONET);
532 dev_remove_pack(&phonet_packet_type);
533 phonet_device_exit();
536 module_init(phonet_init);
537 module_exit(phonet_exit);
538 MODULE_DESCRIPTION("Phonet protocol stack for Linux");
539 MODULE_LICENSE("GPL");
540 MODULE_ALIAS_NETPROTO(PF_PHONET);