Commit | Line | Data |
---|---|---|
4b07b3f6 RDC |
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> | |
f8ff6028 | 34 | #include <net/phonet/pn_dev.h> |
4b07b3f6 | 35 | |
566521d6 AD |
36 | /* Transport protocol registration */ |
37 | static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly; | |
38 | static DEFINE_SPINLOCK(proto_tab_lock); | |
39 | ||
40 | static struct phonet_protocol *phonet_proto_get(int protocol) | |
41 | { | |
42 | struct phonet_protocol *pp; | |
43 | ||
44 | if (protocol >= PHONET_NPROTO) | |
45 | return NULL; | |
46 | ||
47 | spin_lock(&proto_tab_lock); | |
48 | pp = proto_tab[protocol]; | |
49 | if (pp && !try_module_get(pp->prot->owner)) | |
50 | pp = NULL; | |
51 | spin_unlock(&proto_tab_lock); | |
52 | ||
53 | return pp; | |
54 | } | |
55 | ||
56 | static inline void phonet_proto_put(struct phonet_protocol *pp) | |
57 | { | |
58 | module_put(pp->prot->owner); | |
59 | } | |
4b07b3f6 RDC |
60 | |
61 | /* protocol family functions */ | |
62 | ||
63 | static int pn_socket_create(struct net *net, struct socket *sock, int protocol) | |
64 | { | |
ba113a94 RDC |
65 | struct sock *sk; |
66 | struct pn_sock *pn; | |
4b07b3f6 RDC |
67 | struct phonet_protocol *pnp; |
68 | int err; | |
69 | ||
70 | if (net != &init_net) | |
71 | return -EAFNOSUPPORT; | |
72 | ||
73 | if (!capable(CAP_SYS_ADMIN)) | |
74 | return -EPERM; | |
75 | ||
76 | if (protocol == 0) { | |
77 | /* Default protocol selection */ | |
78 | switch (sock->type) { | |
79 | case SOCK_DGRAM: | |
80 | protocol = PN_PROTO_PHONET; | |
81 | break; | |
9641458d RDC |
82 | case SOCK_SEQPACKET: |
83 | protocol = PN_PROTO_PIPE; | |
84 | break; | |
4b07b3f6 RDC |
85 | default: |
86 | return -EPROTONOSUPPORT; | |
87 | } | |
88 | } | |
89 | ||
90 | pnp = phonet_proto_get(protocol); | |
25532824 RDC |
91 | if (pnp == NULL && |
92 | request_module("net-pf-%d-proto-%d", PF_PHONET, protocol) == 0) | |
93 | pnp = phonet_proto_get(protocol); | |
95a5afca | 94 | |
4b07b3f6 RDC |
95 | if (pnp == NULL) |
96 | return -EPROTONOSUPPORT; | |
97 | if (sock->type != pnp->sock_type) { | |
98 | err = -EPROTONOSUPPORT; | |
99 | goto out; | |
100 | } | |
101 | ||
ba113a94 RDC |
102 | sk = sk_alloc(net, PF_PHONET, GFP_KERNEL, pnp->prot); |
103 | if (sk == NULL) { | |
104 | err = -ENOMEM; | |
105 | goto out; | |
106 | } | |
107 | ||
108 | sock_init_data(sock, sk); | |
109 | sock->state = SS_UNCONNECTED; | |
110 | sock->ops = pnp->ops; | |
111 | sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; | |
112 | sk->sk_protocol = protocol; | |
113 | pn = pn_sk(sk); | |
114 | pn->sobject = 0; | |
115 | pn->resource = 0; | |
116 | sk->sk_prot->init(sk); | |
117 | err = 0; | |
4b07b3f6 RDC |
118 | |
119 | out: | |
120 | phonet_proto_put(pnp); | |
121 | return err; | |
122 | } | |
123 | ||
124 | static struct net_proto_family phonet_proto_family = { | |
25532824 | 125 | .family = PF_PHONET, |
4b07b3f6 RDC |
126 | .create = pn_socket_create, |
127 | .owner = THIS_MODULE, | |
128 | }; | |
129 | ||
5f77076d RDC |
130 | /* Phonet device header operations */ |
131 | static int pn_header_create(struct sk_buff *skb, struct net_device *dev, | |
132 | unsigned short type, const void *daddr, | |
133 | const void *saddr, unsigned len) | |
134 | { | |
135 | u8 *media = skb_push(skb, 1); | |
136 | ||
137 | if (type != ETH_P_PHONET) | |
138 | return -1; | |
139 | ||
140 | if (!saddr) | |
141 | saddr = dev->dev_addr; | |
142 | *media = *(const u8 *)saddr; | |
143 | return 1; | |
144 | } | |
145 | ||
146 | static int pn_header_parse(const struct sk_buff *skb, unsigned char *haddr) | |
147 | { | |
148 | const u8 *media = skb_mac_header(skb); | |
149 | *haddr = *media; | |
150 | return 1; | |
151 | } | |
152 | ||
153 | struct header_ops phonet_header_ops = { | |
154 | .create = pn_header_create, | |
155 | .parse = pn_header_parse, | |
156 | }; | |
157 | EXPORT_SYMBOL(phonet_header_ops); | |
158 | ||
107d0d9b RDC |
159 | /* |
160 | * Prepends an ISI header and sends a datagram. | |
161 | */ | |
162 | static int pn_send(struct sk_buff *skb, struct net_device *dev, | |
be0c52bf | 163 | u16 dst, u16 src, u8 res, u8 irq) |
107d0d9b RDC |
164 | { |
165 | struct phonethdr *ph; | |
166 | int err; | |
167 | ||
ebfe92ca RDC |
168 | if (skb->len + 2 > 0xffff /* Phonet length field limit */ || |
169 | skb->len + sizeof(struct phonethdr) > dev->mtu) { | |
107d0d9b RDC |
170 | err = -EMSGSIZE; |
171 | goto drop; | |
172 | } | |
173 | ||
174 | skb_reset_transport_header(skb); | |
175 | WARN_ON(skb_headroom(skb) & 1); /* HW assumes word alignment */ | |
176 | skb_push(skb, sizeof(struct phonethdr)); | |
177 | skb_reset_network_header(skb); | |
178 | ph = pn_hdr(skb); | |
179 | ph->pn_rdev = pn_dev(dst); | |
180 | ph->pn_sdev = pn_dev(src); | |
181 | ph->pn_res = res; | |
182 | ph->pn_length = __cpu_to_be16(skb->len + 2 - sizeof(*ph)); | |
183 | ph->pn_robj = pn_obj(dst); | |
184 | ph->pn_sobj = pn_obj(src); | |
185 | ||
186 | skb->protocol = htons(ETH_P_PHONET); | |
187 | skb->priority = 0; | |
188 | skb->dev = dev; | |
189 | ||
190 | if (pn_addr(src) == pn_addr(dst)) { | |
191 | skb_reset_mac_header(skb); | |
192 | skb->pkt_type = PACKET_LOOPBACK; | |
193 | skb_orphan(skb); | |
be0c52bf RDC |
194 | if (irq) |
195 | netif_rx(skb); | |
196 | else | |
197 | netif_rx_ni(skb); | |
107d0d9b RDC |
198 | err = 0; |
199 | } else { | |
200 | err = dev_hard_header(skb, dev, ntohs(skb->protocol), | |
201 | NULL, NULL, skb->len); | |
202 | if (err < 0) { | |
203 | err = -EHOSTUNREACH; | |
204 | goto drop; | |
205 | } | |
206 | err = dev_queue_xmit(skb); | |
207 | } | |
208 | ||
209 | return err; | |
210 | drop: | |
211 | kfree_skb(skb); | |
212 | return err; | |
213 | } | |
214 | ||
be0c52bf RDC |
215 | static int pn_raw_send(const void *data, int len, struct net_device *dev, |
216 | u16 dst, u16 src, u8 res) | |
217 | { | |
218 | struct sk_buff *skb = alloc_skb(MAX_PHONET_HEADER + len, GFP_ATOMIC); | |
219 | if (skb == NULL) | |
220 | return -ENOMEM; | |
221 | ||
222 | skb_reserve(skb, MAX_PHONET_HEADER); | |
223 | __skb_put(skb, len); | |
224 | skb_copy_to_linear_data(skb, data, len); | |
225 | return pn_send(skb, dev, dst, src, res, 1); | |
226 | } | |
227 | ||
107d0d9b RDC |
228 | /* |
229 | * Create a Phonet header for the skb and send it out. Returns | |
230 | * non-zero error code if failed. The skb is freed then. | |
231 | */ | |
232 | int pn_skb_send(struct sock *sk, struct sk_buff *skb, | |
233 | const struct sockaddr_pn *target) | |
234 | { | |
235 | struct net_device *dev; | |
236 | struct pn_sock *pn = pn_sk(sk); | |
237 | int err; | |
238 | u16 src; | |
239 | u8 daddr = pn_sockaddr_get_addr(target), saddr = PN_NO_ADDR; | |
240 | ||
241 | err = -EHOSTUNREACH; | |
242 | if (sk->sk_bound_dev_if) | |
243 | dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if); | |
244 | else | |
245 | dev = phonet_device_get(sock_net(sk)); | |
246 | if (!dev || !(dev->flags & IFF_UP)) | |
247 | goto drop; | |
248 | ||
249 | saddr = phonet_address_get(dev, daddr); | |
250 | if (saddr == PN_NO_ADDR) | |
251 | goto drop; | |
252 | ||
253 | src = pn->sobject; | |
254 | if (!pn_addr(src)) | |
255 | src = pn_object(saddr, pn_obj(src)); | |
256 | ||
257 | err = pn_send(skb, dev, pn_sockaddr_get_object(target), | |
be0c52bf | 258 | src, pn_sockaddr_get_resource(target), 0); |
107d0d9b RDC |
259 | dev_put(dev); |
260 | return err; | |
261 | ||
262 | drop: | |
263 | kfree_skb(skb); | |
264 | if (dev) | |
265 | dev_put(dev); | |
266 | return err; | |
267 | } | |
268 | EXPORT_SYMBOL(pn_skb_send); | |
269 | ||
be0c52bf RDC |
270 | /* Do not send an error message in response to an error message */ |
271 | static inline int can_respond(struct sk_buff *skb) | |
272 | { | |
273 | const struct phonethdr *ph; | |
274 | const struct phonetmsg *pm; | |
275 | u8 submsg_id; | |
276 | ||
277 | if (!pskb_may_pull(skb, 3)) | |
278 | return 0; | |
279 | ||
280 | ph = pn_hdr(skb); | |
281 | if (phonet_address_get(skb->dev, ph->pn_rdev) != ph->pn_rdev) | |
282 | return 0; /* we are not the destination */ | |
283 | if (ph->pn_res == PN_PREFIX && !pskb_may_pull(skb, 5)) | |
284 | return 0; | |
c3a90c78 RDC |
285 | if (ph->pn_res == PN_COMMGR) /* indications */ |
286 | return 0; | |
be0c52bf RDC |
287 | |
288 | ph = pn_hdr(skb); /* re-acquires the pointer */ | |
289 | pm = pn_msg(skb); | |
290 | if (pm->pn_msg_id != PN_COMMON_MESSAGE) | |
291 | return 1; | |
292 | submsg_id = (ph->pn_res == PN_PREFIX) | |
293 | ? pm->pn_e_submsg_id : pm->pn_submsg_id; | |
294 | if (submsg_id != PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP && | |
295 | pm->pn_e_submsg_id != PN_COMM_SERVICE_NOT_IDENTIFIED_RESP) | |
296 | return 1; | |
297 | return 0; | |
298 | } | |
299 | ||
300 | static int send_obj_unreachable(struct sk_buff *rskb) | |
301 | { | |
302 | const struct phonethdr *oph = pn_hdr(rskb); | |
303 | const struct phonetmsg *opm = pn_msg(rskb); | |
304 | struct phonetmsg resp; | |
305 | ||
306 | memset(&resp, 0, sizeof(resp)); | |
307 | resp.pn_trans_id = opm->pn_trans_id; | |
308 | resp.pn_msg_id = PN_COMMON_MESSAGE; | |
309 | if (oph->pn_res == PN_PREFIX) { | |
310 | resp.pn_e_res_id = opm->pn_e_res_id; | |
311 | resp.pn_e_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP; | |
312 | resp.pn_e_orig_msg_id = opm->pn_msg_id; | |
313 | resp.pn_e_status = 0; | |
314 | } else { | |
315 | resp.pn_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP; | |
316 | resp.pn_orig_msg_id = opm->pn_msg_id; | |
317 | resp.pn_status = 0; | |
318 | } | |
319 | return pn_raw_send(&resp, sizeof(resp), rskb->dev, | |
320 | pn_object(oph->pn_sdev, oph->pn_sobj), | |
321 | pn_object(oph->pn_rdev, oph->pn_robj), | |
322 | oph->pn_res); | |
323 | } | |
324 | ||
325 | static int send_reset_indications(struct sk_buff *rskb) | |
326 | { | |
327 | struct phonethdr *oph = pn_hdr(rskb); | |
328 | static const u8 data[4] = { | |
329 | 0x00 /* trans ID */, 0x10 /* subscribe msg */, | |
330 | 0x00 /* subscription count */, 0x00 /* dummy */ | |
331 | }; | |
332 | ||
333 | return pn_raw_send(data, sizeof(data), rskb->dev, | |
334 | pn_object(oph->pn_sdev, 0x00), | |
c3a90c78 RDC |
335 | pn_object(oph->pn_rdev, oph->pn_robj), |
336 | PN_COMMGR); | |
be0c52bf RDC |
337 | } |
338 | ||
339 | ||
4b07b3f6 RDC |
340 | /* packet type functions */ |
341 | ||
342 | /* | |
343 | * Stuff received packets to associated sockets. | |
344 | * On error, returns non-zero and releases the skb. | |
345 | */ | |
346 | static int phonet_rcv(struct sk_buff *skb, struct net_device *dev, | |
347 | struct packet_type *pkttype, | |
348 | struct net_device *orig_dev) | |
349 | { | |
350 | struct phonethdr *ph; | |
ba113a94 | 351 | struct sock *sk; |
4b07b3f6 RDC |
352 | struct sockaddr_pn sa; |
353 | u16 len; | |
354 | ||
355 | if (dev_net(dev) != &init_net) | |
356 | goto out; | |
357 | ||
358 | /* check we have at least a full Phonet header */ | |
359 | if (!pskb_pull(skb, sizeof(struct phonethdr))) | |
360 | goto out; | |
361 | ||
362 | /* check that the advertised length is correct */ | |
363 | ph = pn_hdr(skb); | |
364 | len = get_unaligned_be16(&ph->pn_length); | |
365 | if (len < 2) | |
366 | goto out; | |
367 | len -= 2; | |
368 | if ((len > skb->len) || pskb_trim(skb, len)) | |
369 | goto out; | |
370 | skb_reset_transport_header(skb); | |
371 | ||
372 | pn_skb_get_dst_sockaddr(skb, &sa); | |
373 | if (pn_sockaddr_get_addr(&sa) == 0) | |
374 | goto out; /* currently, we cannot be device 0 */ | |
375 | ||
ba113a94 | 376 | sk = pn_find_sock_by_sa(&sa); |
be0c52bf RDC |
377 | if (sk == NULL) { |
378 | if (can_respond(skb)) { | |
379 | send_obj_unreachable(skb); | |
380 | send_reset_indications(skb); | |
381 | } | |
ba113a94 | 382 | goto out; |
be0c52bf | 383 | } |
ba113a94 RDC |
384 | |
385 | /* Push data to the socket (or other sockets connected to it). */ | |
386 | return sk_receive_skb(sk, skb, 0); | |
4b07b3f6 RDC |
387 | |
388 | out: | |
389 | kfree_skb(skb); | |
390 | return NET_RX_DROP; | |
391 | } | |
392 | ||
393 | static struct packet_type phonet_packet_type = { | |
394 | .type = __constant_htons(ETH_P_PHONET), | |
395 | .dev = NULL, | |
396 | .func = phonet_rcv, | |
397 | }; | |
398 | ||
4b07b3f6 RDC |
399 | int __init_or_module phonet_proto_register(int protocol, |
400 | struct phonet_protocol *pp) | |
401 | { | |
402 | int err = 0; | |
403 | ||
404 | if (protocol >= PHONET_NPROTO) | |
405 | return -EINVAL; | |
406 | ||
407 | err = proto_register(pp->prot, 1); | |
408 | if (err) | |
409 | return err; | |
410 | ||
411 | spin_lock(&proto_tab_lock); | |
412 | if (proto_tab[protocol]) | |
413 | err = -EBUSY; | |
414 | else | |
415 | proto_tab[protocol] = pp; | |
416 | spin_unlock(&proto_tab_lock); | |
417 | ||
418 | return err; | |
419 | } | |
420 | EXPORT_SYMBOL(phonet_proto_register); | |
421 | ||
422 | void phonet_proto_unregister(int protocol, struct phonet_protocol *pp) | |
423 | { | |
424 | spin_lock(&proto_tab_lock); | |
425 | BUG_ON(proto_tab[protocol] != pp); | |
426 | proto_tab[protocol] = NULL; | |
427 | spin_unlock(&proto_tab_lock); | |
428 | proto_unregister(pp->prot); | |
429 | } | |
430 | EXPORT_SYMBOL(phonet_proto_unregister); | |
431 | ||
4b07b3f6 RDC |
432 | /* Module registration */ |
433 | static int __init phonet_init(void) | |
434 | { | |
435 | int err; | |
436 | ||
437 | err = sock_register(&phonet_proto_family); | |
438 | if (err) { | |
439 | printk(KERN_ALERT | |
440 | "phonet protocol family initialization failed\n"); | |
441 | return err; | |
442 | } | |
443 | ||
f8ff6028 | 444 | phonet_device_init(); |
4b07b3f6 | 445 | dev_add_pack(&phonet_packet_type); |
8fb39740 | 446 | phonet_netlink_register(); |
87ab4e20 | 447 | phonet_sysctl_init(); |
107d0d9b RDC |
448 | |
449 | err = isi_register(); | |
450 | if (err) | |
451 | goto err; | |
4b07b3f6 | 452 | return 0; |
107d0d9b RDC |
453 | |
454 | err: | |
87ab4e20 | 455 | phonet_sysctl_exit(); |
25532824 | 456 | sock_unregister(PF_PHONET); |
107d0d9b RDC |
457 | dev_remove_pack(&phonet_packet_type); |
458 | phonet_device_exit(); | |
459 | return err; | |
4b07b3f6 RDC |
460 | } |
461 | ||
462 | static void __exit phonet_exit(void) | |
463 | { | |
107d0d9b | 464 | isi_unregister(); |
87ab4e20 | 465 | phonet_sysctl_exit(); |
25532824 | 466 | sock_unregister(PF_PHONET); |
4b07b3f6 | 467 | dev_remove_pack(&phonet_packet_type); |
f8ff6028 | 468 | phonet_device_exit(); |
4b07b3f6 RDC |
469 | } |
470 | ||
471 | module_init(phonet_init); | |
472 | module_exit(phonet_exit); | |
473 | MODULE_DESCRIPTION("Phonet protocol stack for Linux"); | |
474 | MODULE_LICENSE("GPL"); | |
25532824 | 475 | MODULE_ALIAS_NETPROTO(PF_PHONET); |