Phonet: Netlink interface
[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 <asm/unaligned.h>
29 #include <net/sock.h>
30
31 #include <linux/if_phonet.h>
32 #include <linux/phonet.h>
33 #include <net/phonet/phonet.h>
34 #include <net/phonet/pn_dev.h>
35
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);
39
40 /* protocol family functions */
41
42 static int pn_socket_create(struct net *net, struct socket *sock, int protocol)
43 {
44         struct phonet_protocol *pnp;
45         int err;
46
47         if (net != &init_net)
48                 return -EAFNOSUPPORT;
49
50         if (!capable(CAP_SYS_ADMIN))
51                 return -EPERM;
52
53         if (protocol == 0) {
54                 /* Default protocol selection */
55                 switch (sock->type) {
56                 case SOCK_DGRAM:
57                         protocol = PN_PROTO_PHONET;
58                         break;
59                 default:
60                         return -EPROTONOSUPPORT;
61                 }
62         }
63
64         pnp = phonet_proto_get(protocol);
65         if (pnp == NULL)
66                 return -EPROTONOSUPPORT;
67         if (sock->type != pnp->sock_type) {
68                 err = -EPROTONOSUPPORT;
69                 goto out;
70         }
71
72         /* TODO: create and init the struct sock */
73         err = -EPROTONOSUPPORT;
74
75 out:
76         phonet_proto_put(pnp);
77         return err;
78 }
79
80 static struct net_proto_family phonet_proto_family = {
81         .family = AF_PHONET,
82         .create = pn_socket_create,
83         .owner = THIS_MODULE,
84 };
85
86 /* packet type functions */
87
88 /*
89  * Stuff received packets to associated sockets.
90  * On error, returns non-zero and releases the skb.
91  */
92 static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
93                         struct packet_type *pkttype,
94                         struct net_device *orig_dev)
95 {
96         struct phonethdr *ph;
97         struct sockaddr_pn sa;
98         u16 len;
99
100         if (dev_net(dev) != &init_net)
101                 goto out;
102
103         /* check we have at least a full Phonet header */
104         if (!pskb_pull(skb, sizeof(struct phonethdr)))
105                 goto out;
106
107         /* check that the advertised length is correct */
108         ph = pn_hdr(skb);
109         len = get_unaligned_be16(&ph->pn_length);
110         if (len < 2)
111                 goto out;
112         len -= 2;
113         if ((len > skb->len) || pskb_trim(skb, len))
114                 goto out;
115         skb_reset_transport_header(skb);
116
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 */
120
121         /* TODO: put packets to sockets backlog */
122
123 out:
124         kfree_skb(skb);
125         return NET_RX_DROP;
126 }
127
128 static struct packet_type phonet_packet_type = {
129         .type = __constant_htons(ETH_P_PHONET),
130         .dev = NULL,
131         .func = phonet_rcv,
132 };
133
134 /* Transport protocol registration */
135 static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
136 static DEFINE_SPINLOCK(proto_tab_lock);
137
138 int __init_or_module phonet_proto_register(int protocol,
139                                                 struct phonet_protocol *pp)
140 {
141         int err = 0;
142
143         if (protocol >= PHONET_NPROTO)
144                 return -EINVAL;
145
146         err = proto_register(pp->prot, 1);
147         if (err)
148                 return err;
149
150         spin_lock(&proto_tab_lock);
151         if (proto_tab[protocol])
152                 err = -EBUSY;
153         else
154                 proto_tab[protocol] = pp;
155         spin_unlock(&proto_tab_lock);
156
157         return err;
158 }
159 EXPORT_SYMBOL(phonet_proto_register);
160
161 void phonet_proto_unregister(int protocol, struct phonet_protocol *pp)
162 {
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);
168 }
169 EXPORT_SYMBOL(phonet_proto_unregister);
170
171 static struct phonet_protocol *phonet_proto_get(int protocol)
172 {
173         struct phonet_protocol *pp;
174
175         if (protocol >= PHONET_NPROTO)
176                 return NULL;
177
178         spin_lock(&proto_tab_lock);
179         pp = proto_tab[protocol];
180         if (pp && !try_module_get(pp->prot->owner))
181                 pp = NULL;
182         spin_unlock(&proto_tab_lock);
183
184         return pp;
185 }
186
187 static inline void phonet_proto_put(struct phonet_protocol *pp)
188 {
189         module_put(pp->prot->owner);
190 }
191
192 /* Module registration */
193 static int __init phonet_init(void)
194 {
195         int err;
196
197         err = sock_register(&phonet_proto_family);
198         if (err) {
199                 printk(KERN_ALERT
200                         "phonet protocol family initialization failed\n");
201                 return err;
202         }
203
204         phonet_device_init();
205         dev_add_pack(&phonet_packet_type);
206         phonet_netlink_register();
207         return 0;
208 }
209
210 static void __exit phonet_exit(void)
211 {
212         sock_unregister(AF_PHONET);
213         dev_remove_pack(&phonet_packet_type);
214         phonet_device_exit();
215 }
216
217 module_init(phonet_init);
218 module_exit(phonet_exit);
219 MODULE_DESCRIPTION("Phonet protocol stack for Linux");
220 MODULE_LICENSE("GPL");