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