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);
38 static inline void phonet_proto_put(struct phonet_protocol *pp);
40 /* protocol family functions */
42 static int pn_socket_create(struct net *net, struct socket *sock, int protocol)
44 struct phonet_protocol *pnp;
50 if (!capable(CAP_SYS_ADMIN))
54 /* Default protocol selection */
57 protocol = PN_PROTO_PHONET;
60 return -EPROTONOSUPPORT;
64 pnp = phonet_proto_get(protocol);
66 return -EPROTONOSUPPORT;
67 if (sock->type != pnp->sock_type) {
68 err = -EPROTONOSUPPORT;
72 /* TODO: create and init the struct sock */
73 err = -EPROTONOSUPPORT;
76 phonet_proto_put(pnp);
80 static struct net_proto_family phonet_proto_family = {
82 .create = pn_socket_create,
86 /* packet type functions */
89 * Stuff received packets to associated sockets.
90 * On error, returns non-zero and releases the skb.
92 static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
93 struct packet_type *pkttype,
94 struct net_device *orig_dev)
97 struct sockaddr_pn sa;
100 if (dev_net(dev) != &init_net)
103 /* check we have at least a full Phonet header */
104 if (!pskb_pull(skb, sizeof(struct phonethdr)))
107 /* check that the advertised length is correct */
109 len = get_unaligned_be16(&ph->pn_length);
113 if ((len > skb->len) || pskb_trim(skb, len))
115 skb_reset_transport_header(skb);
117 pn_skb_get_dst_sockaddr(skb, &sa);
118 if (pn_sockaddr_get_addr(&sa) == 0)
119 goto out; /* currently, we cannot be device 0 */
121 /* TODO: put packets to sockets backlog */
128 static struct packet_type phonet_packet_type = {
129 .type = __constant_htons(ETH_P_PHONET),
134 /* Transport protocol registration */
135 static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
136 static DEFINE_SPINLOCK(proto_tab_lock);
138 int __init_or_module phonet_proto_register(int protocol,
139 struct phonet_protocol *pp)
143 if (protocol >= PHONET_NPROTO)
146 err = proto_register(pp->prot, 1);
150 spin_lock(&proto_tab_lock);
151 if (proto_tab[protocol])
154 proto_tab[protocol] = pp;
155 spin_unlock(&proto_tab_lock);
159 EXPORT_SYMBOL(phonet_proto_register);
161 void phonet_proto_unregister(int protocol, struct phonet_protocol *pp)
163 spin_lock(&proto_tab_lock);
164 BUG_ON(proto_tab[protocol] != pp);
165 proto_tab[protocol] = NULL;
166 spin_unlock(&proto_tab_lock);
167 proto_unregister(pp->prot);
169 EXPORT_SYMBOL(phonet_proto_unregister);
171 static struct phonet_protocol *phonet_proto_get(int protocol)
173 struct phonet_protocol *pp;
175 if (protocol >= PHONET_NPROTO)
178 spin_lock(&proto_tab_lock);
179 pp = proto_tab[protocol];
180 if (pp && !try_module_get(pp->prot->owner))
182 spin_unlock(&proto_tab_lock);
187 static inline void phonet_proto_put(struct phonet_protocol *pp)
189 module_put(pp->prot->owner);
192 /* Module registration */
193 static int __init phonet_init(void)
197 err = sock_register(&phonet_proto_family);
200 "phonet protocol family initialization failed\n");
204 phonet_device_init();
205 dev_add_pack(&phonet_packet_type);
206 phonet_netlink_register();
210 static void __exit phonet_exit(void)
212 sock_unregister(AF_PHONET);
213 dev_remove_pack(&phonet_packet_type);
214 phonet_device_exit();
217 module_init(phonet_init);
218 module_exit(phonet_exit);
219 MODULE_DESCRIPTION("Phonet protocol stack for Linux");
220 MODULE_LICENSE("GPL");