1
2
3
4
5
6
7#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
8
9#include <linux/fs.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/sched/signal.h>
13#include <linux/spinlock.h>
14#include <linux/mutex.h>
15#include <linux/list.h>
16#include <linux/wait.h>
17#include <linux/poll.h>
18#include <linux/tcp.h>
19#include <linux/uaccess.h>
20#include <linux/debugfs.h>
21#include <linux/caif/caif_socket.h>
22#include <linux/pkt_sched.h>
23#include <net/sock.h>
24#include <net/tcp_states.h>
25#include <net/caif/caif_layer.h>
26#include <net/caif/caif_dev.h>
27#include <net/caif/cfpkt.h>
28
29MODULE_LICENSE("GPL");
30MODULE_ALIAS_NETPROTO(AF_CAIF);
31
32
33
34
35
36
37enum caif_states {
38 CAIF_CONNECTED = TCP_ESTABLISHED,
39 CAIF_CONNECTING = TCP_SYN_SENT,
40 CAIF_DISCONNECTED = TCP_CLOSE
41};
42
43#define TX_FLOW_ON_BIT 1
44#define RX_FLOW_ON_BIT 2
45
46struct caifsock {
47 struct sock sk;
48 struct cflayer layer;
49 u32 flow_state;
50 struct caif_connect_request conn_req;
51 struct mutex readlock;
52 struct dentry *debugfs_socket_dir;
53 int headroom, tailroom, maxframe;
54};
55
56static int rx_flow_is_on(struct caifsock *cf_sk)
57{
58 return test_bit(RX_FLOW_ON_BIT,
59 (void *) &cf_sk->flow_state);
60}
61
62static int tx_flow_is_on(struct caifsock *cf_sk)
63{
64 return test_bit(TX_FLOW_ON_BIT,
65 (void *) &cf_sk->flow_state);
66}
67
68static void set_rx_flow_off(struct caifsock *cf_sk)
69{
70 clear_bit(RX_FLOW_ON_BIT,
71 (void *) &cf_sk->flow_state);
72}
73
74static void set_rx_flow_on(struct caifsock *cf_sk)
75{
76 set_bit(RX_FLOW_ON_BIT,
77 (void *) &cf_sk->flow_state);
78}
79
80static void set_tx_flow_off(struct caifsock *cf_sk)
81{
82 clear_bit(TX_FLOW_ON_BIT,
83 (void *) &cf_sk->flow_state);
84}
85
86static void set_tx_flow_on(struct caifsock *cf_sk)
87{
88 set_bit(TX_FLOW_ON_BIT,
89 (void *) &cf_sk->flow_state);
90}
91
92static void caif_read_lock(struct sock *sk)
93{
94 struct caifsock *cf_sk;
95 cf_sk = container_of(sk, struct caifsock, sk);
96 mutex_lock(&cf_sk->readlock);
97}
98
99static void caif_read_unlock(struct sock *sk)
100{
101 struct caifsock *cf_sk;
102 cf_sk = container_of(sk, struct caifsock, sk);
103 mutex_unlock(&cf_sk->readlock);
104}
105
106static int sk_rcvbuf_lowwater(struct caifsock *cf_sk)
107{
108
109 return cf_sk->sk.sk_rcvbuf / 4;
110}
111
112static void caif_flow_ctrl(struct sock *sk, int mode)
113{
114 struct caifsock *cf_sk;
115 cf_sk = container_of(sk, struct caifsock, sk);
116 if (cf_sk->layer.dn && cf_sk->layer.dn->modemcmd)
117 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode);
118}
119
120
121
122
123
124static void caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
125{
126 int err;
127 unsigned long flags;
128 struct sk_buff_head *list = &sk->sk_receive_queue;
129 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
130 bool queued = false;
131
132 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
133 (unsigned int)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
134 net_dbg_ratelimited("sending flow OFF (queue len = %d %d)\n",
135 atomic_read(&cf_sk->sk.sk_rmem_alloc),
136 sk_rcvbuf_lowwater(cf_sk));
137 set_rx_flow_off(cf_sk);
138 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
139 }
140
141 err = sk_filter(sk, skb);
142 if (err)
143 goto out;
144
145 if (!sk_rmem_schedule(sk, skb, skb->truesize) && rx_flow_is_on(cf_sk)) {
146 set_rx_flow_off(cf_sk);
147 net_dbg_ratelimited("sending flow OFF due to rmem_schedule\n");
148 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
149 }
150 skb->dev = NULL;
151 skb_set_owner_r(skb, sk);
152 spin_lock_irqsave(&list->lock, flags);
153 queued = !sock_flag(sk, SOCK_DEAD);
154 if (queued)
155 __skb_queue_tail(list, skb);
156 spin_unlock_irqrestore(&list->lock, flags);
157out:
158 if (queued)
159 sk->sk_data_ready(sk);
160 else
161 kfree_skb(skb);
162}
163
164
165static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
166{
167 struct caifsock *cf_sk;
168 struct sk_buff *skb;
169
170 cf_sk = container_of(layr, struct caifsock, layer);
171 skb = cfpkt_tonative(pkt);
172
173 if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) {
174 kfree_skb(skb);
175 return 0;
176 }
177 caif_queue_rcv_skb(&cf_sk->sk, skb);
178 return 0;
179}
180
181static void cfsk_hold(struct cflayer *layr)
182{
183 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
184 sock_hold(&cf_sk->sk);
185}
186
187static void cfsk_put(struct cflayer *layr)
188{
189 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
190 sock_put(&cf_sk->sk);
191}
192
193
194static void caif_ctrl_cb(struct cflayer *layr,
195 enum caif_ctrlcmd flow,
196 int phyid)
197{
198 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
199 switch (flow) {
200 case CAIF_CTRLCMD_FLOW_ON_IND:
201
202 set_tx_flow_on(cf_sk);
203 cf_sk->sk.sk_state_change(&cf_sk->sk);
204 break;
205
206 case CAIF_CTRLCMD_FLOW_OFF_IND:
207
208 set_tx_flow_off(cf_sk);
209 cf_sk->sk.sk_state_change(&cf_sk->sk);
210 break;
211
212 case CAIF_CTRLCMD_INIT_RSP:
213
214 caif_client_register_refcnt(&cf_sk->layer,
215 cfsk_hold, cfsk_put);
216 cf_sk->sk.sk_state = CAIF_CONNECTED;
217 set_tx_flow_on(cf_sk);
218 cf_sk->sk.sk_shutdown = 0;
219 cf_sk->sk.sk_state_change(&cf_sk->sk);
220 break;
221
222 case CAIF_CTRLCMD_DEINIT_RSP:
223
224 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
225 cf_sk->sk.sk_state_change(&cf_sk->sk);
226 break;
227
228 case CAIF_CTRLCMD_INIT_FAIL_RSP:
229
230 cf_sk->sk.sk_err = ECONNREFUSED;
231 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
232 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
233
234
235
236
237 set_tx_flow_on(cf_sk);
238 cf_sk->sk.sk_state_change(&cf_sk->sk);
239 break;
240
241 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
242
243 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
244 cf_sk->sk.sk_err = ECONNRESET;
245 set_rx_flow_on(cf_sk);
246 sk_error_report(&cf_sk->sk);
247 break;
248
249 default:
250 pr_debug("Unexpected flow command %d\n", flow);
251 }
252}
253
254static void caif_check_flow_release(struct sock *sk)
255{
256 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
257
258 if (rx_flow_is_on(cf_sk))
259 return;
260
261 if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
262 set_rx_flow_on(cf_sk);
263 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ);
264 }
265}
266
267
268
269
270
271static int caif_seqpkt_recvmsg(struct socket *sock, struct msghdr *m,
272 size_t len, int flags)
273
274{
275 struct sock *sk = sock->sk;
276 struct sk_buff *skb;
277 int ret;
278 int copylen;
279
280 ret = -EOPNOTSUPP;
281 if (flags & MSG_OOB)
282 goto read_error;
283
284 skb = skb_recv_datagram(sk, flags, 0 , &ret);
285 if (!skb)
286 goto read_error;
287 copylen = skb->len;
288 if (len < copylen) {
289 m->msg_flags |= MSG_TRUNC;
290 copylen = len;
291 }
292
293 ret = skb_copy_datagram_msg(skb, 0, m, copylen);
294 if (ret)
295 goto out_free;
296
297 ret = (flags & MSG_TRUNC) ? skb->len : copylen;
298out_free:
299 skb_free_datagram(sk, skb);
300 caif_check_flow_release(sk);
301 return ret;
302
303read_error:
304 return ret;
305}
306
307
308
309static long caif_stream_data_wait(struct sock *sk, long timeo)
310{
311 DEFINE_WAIT(wait);
312 lock_sock(sk);
313
314 for (;;) {
315 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
316
317 if (!skb_queue_empty(&sk->sk_receive_queue) ||
318 sk->sk_err ||
319 sk->sk_state != CAIF_CONNECTED ||
320 sock_flag(sk, SOCK_DEAD) ||
321 (sk->sk_shutdown & RCV_SHUTDOWN) ||
322 signal_pending(current) ||
323 !timeo)
324 break;
325
326 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
327 release_sock(sk);
328 timeo = schedule_timeout(timeo);
329 lock_sock(sk);
330
331 if (sock_flag(sk, SOCK_DEAD))
332 break;
333
334 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
335 }
336
337 finish_wait(sk_sleep(sk), &wait);
338 release_sock(sk);
339 return timeo;
340}
341
342
343
344
345
346
347static int caif_stream_recvmsg(struct socket *sock, struct msghdr *msg,
348 size_t size, int flags)
349{
350 struct sock *sk = sock->sk;
351 int copied = 0;
352 int target;
353 int err = 0;
354 long timeo;
355
356 err = -EOPNOTSUPP;
357 if (flags&MSG_OOB)
358 goto out;
359
360
361
362
363
364 err = -EAGAIN;
365 if (sk->sk_state == CAIF_CONNECTING)
366 goto out;
367
368 caif_read_lock(sk);
369 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
370 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
371
372 do {
373 int chunk;
374 struct sk_buff *skb;
375
376 lock_sock(sk);
377 if (sock_flag(sk, SOCK_DEAD)) {
378 err = -ECONNRESET;
379 goto unlock;
380 }
381 skb = skb_dequeue(&sk->sk_receive_queue);
382 caif_check_flow_release(sk);
383
384 if (skb == NULL) {
385 if (copied >= target)
386 goto unlock;
387
388
389
390 err = sock_error(sk);
391 if (err)
392 goto unlock;
393 err = -ECONNRESET;
394 if (sk->sk_shutdown & RCV_SHUTDOWN)
395 goto unlock;
396
397 err = -EPIPE;
398 if (sk->sk_state != CAIF_CONNECTED)
399 goto unlock;
400 if (sock_flag(sk, SOCK_DEAD))
401 goto unlock;
402
403 release_sock(sk);
404
405 err = -EAGAIN;
406 if (!timeo)
407 break;
408
409 caif_read_unlock(sk);
410
411 timeo = caif_stream_data_wait(sk, timeo);
412
413 if (signal_pending(current)) {
414 err = sock_intr_errno(timeo);
415 goto out;
416 }
417 caif_read_lock(sk);
418 continue;
419unlock:
420 release_sock(sk);
421 break;
422 }
423 release_sock(sk);
424 chunk = min_t(unsigned int, skb->len, size);
425 if (memcpy_to_msg(msg, skb->data, chunk)) {
426 skb_queue_head(&sk->sk_receive_queue, skb);
427 if (copied == 0)
428 copied = -EFAULT;
429 break;
430 }
431 copied += chunk;
432 size -= chunk;
433
434
435 if (!(flags & MSG_PEEK)) {
436 skb_pull(skb, chunk);
437
438
439 if (skb->len) {
440 skb_queue_head(&sk->sk_receive_queue, skb);
441 break;
442 }
443 kfree_skb(skb);
444
445 } else {
446
447
448
449
450 skb_queue_head(&sk->sk_receive_queue, skb);
451 break;
452 }
453 } while (size);
454 caif_read_unlock(sk);
455
456out:
457 return copied ? : err;
458}
459
460
461
462
463
464static long caif_wait_for_flow_on(struct caifsock *cf_sk,
465 int wait_writeable, long timeo, int *err)
466{
467 struct sock *sk = &cf_sk->sk;
468 DEFINE_WAIT(wait);
469 for (;;) {
470 *err = 0;
471 if (tx_flow_is_on(cf_sk) &&
472 (!wait_writeable || sock_writeable(&cf_sk->sk)))
473 break;
474 *err = -ETIMEDOUT;
475 if (!timeo)
476 break;
477 *err = -ERESTARTSYS;
478 if (signal_pending(current))
479 break;
480 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
481 *err = -ECONNRESET;
482 if (sk->sk_shutdown & SHUTDOWN_MASK)
483 break;
484 *err = -sk->sk_err;
485 if (sk->sk_err)
486 break;
487 *err = -EPIPE;
488 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
489 break;
490 timeo = schedule_timeout(timeo);
491 }
492 finish_wait(sk_sleep(sk), &wait);
493 return timeo;
494}
495
496
497
498
499
500static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
501 int noblock, long timeo)
502{
503 struct cfpkt *pkt;
504
505 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
506 memset(skb->cb, 0, sizeof(struct caif_payload_info));
507 cfpkt_set_prio(pkt, cf_sk->sk.sk_priority);
508
509 if (cf_sk->layer.dn == NULL) {
510 kfree_skb(skb);
511 return -EINVAL;
512 }
513
514 return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
515}
516
517
518static int caif_seqpkt_sendmsg(struct socket *sock, struct msghdr *msg,
519 size_t len)
520{
521 struct sock *sk = sock->sk;
522 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
523 int buffer_size;
524 int ret = 0;
525 struct sk_buff *skb = NULL;
526 int noblock;
527 long timeo;
528 caif_assert(cf_sk);
529 ret = sock_error(sk);
530 if (ret)
531 goto err;
532
533 ret = -EOPNOTSUPP;
534 if (msg->msg_flags&MSG_OOB)
535 goto err;
536
537 ret = -EOPNOTSUPP;
538 if (msg->msg_namelen)
539 goto err;
540
541 ret = -EINVAL;
542 if (unlikely(msg->msg_iter.nr_segs == 0) ||
543 unlikely(msg->msg_iter.iov->iov_base == NULL))
544 goto err;
545 noblock = msg->msg_flags & MSG_DONTWAIT;
546
547 timeo = sock_sndtimeo(sk, noblock);
548 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
549 1, timeo, &ret);
550
551 if (ret)
552 goto err;
553 ret = -EPIPE;
554 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
555 sock_flag(sk, SOCK_DEAD) ||
556 (sk->sk_shutdown & RCV_SHUTDOWN))
557 goto err;
558
559
560 ret = -EMSGSIZE;
561 if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
562 goto err;
563
564 buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
565
566 ret = -ENOMEM;
567 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
568
569 if (!skb || skb_tailroom(skb) < buffer_size)
570 goto err;
571
572 skb_reserve(skb, cf_sk->headroom);
573
574 ret = memcpy_from_msg(skb_put(skb, len), msg, len);
575
576 if (ret)
577 goto err;
578 ret = transmit_skb(skb, cf_sk, noblock, timeo);
579 if (ret < 0)
580
581 return ret;
582
583 return len;
584err:
585 kfree_skb(skb);
586 return ret;
587}
588
589
590
591
592
593
594static int caif_stream_sendmsg(struct socket *sock, struct msghdr *msg,
595 size_t len)
596{
597 struct sock *sk = sock->sk;
598 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
599 int err, size;
600 struct sk_buff *skb;
601 int sent = 0;
602 long timeo;
603
604 err = -EOPNOTSUPP;
605 if (unlikely(msg->msg_flags&MSG_OOB))
606 goto out_err;
607
608 if (unlikely(msg->msg_namelen))
609 goto out_err;
610
611 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
612 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
613
614 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
615 goto pipe_err;
616
617 while (sent < len) {
618
619 size = len-sent;
620
621 if (size > cf_sk->maxframe)
622 size = cf_sk->maxframe;
623
624
625 if (size > ((sk->sk_sndbuf >> 1) - 64))
626 size = (sk->sk_sndbuf >> 1) - 64;
627
628 if (size > SKB_MAX_ALLOC)
629 size = SKB_MAX_ALLOC;
630
631 skb = sock_alloc_send_skb(sk,
632 size + cf_sk->headroom +
633 cf_sk->tailroom,
634 msg->msg_flags&MSG_DONTWAIT,
635 &err);
636 if (skb == NULL)
637 goto out_err;
638
639 skb_reserve(skb, cf_sk->headroom);
640
641
642
643
644
645
646
647 size = min_t(int, size, skb_tailroom(skb));
648
649 err = memcpy_from_msg(skb_put(skb, size), msg, size);
650 if (err) {
651 kfree_skb(skb);
652 goto out_err;
653 }
654 err = transmit_skb(skb, cf_sk,
655 msg->msg_flags&MSG_DONTWAIT, timeo);
656 if (err < 0)
657
658 goto pipe_err;
659
660 sent += size;
661 }
662
663 return sent;
664
665pipe_err:
666 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
667 send_sig(SIGPIPE, current, 0);
668 err = -EPIPE;
669out_err:
670 return sent ? : err;
671}
672
673static int setsockopt(struct socket *sock, int lvl, int opt, sockptr_t ov,
674 unsigned int ol)
675{
676 struct sock *sk = sock->sk;
677 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
678 int linksel;
679
680 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
681 return -ENOPROTOOPT;
682
683 switch (opt) {
684 case CAIFSO_LINK_SELECT:
685 if (ol < sizeof(int))
686 return -EINVAL;
687 if (lvl != SOL_CAIF)
688 goto bad_sol;
689 if (copy_from_sockptr(&linksel, ov, sizeof(int)))
690 return -EINVAL;
691 lock_sock(&(cf_sk->sk));
692 cf_sk->conn_req.link_selector = linksel;
693 release_sock(&cf_sk->sk);
694 return 0;
695
696 case CAIFSO_REQ_PARAM:
697 if (lvl != SOL_CAIF)
698 goto bad_sol;
699 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
700 return -ENOPROTOOPT;
701 lock_sock(&(cf_sk->sk));
702 if (ol > sizeof(cf_sk->conn_req.param.data) ||
703 copy_from_sockptr(&cf_sk->conn_req.param.data, ov, ol)) {
704 release_sock(&cf_sk->sk);
705 return -EINVAL;
706 }
707 cf_sk->conn_req.param.size = ol;
708 release_sock(&cf_sk->sk);
709 return 0;
710
711 default:
712 return -ENOPROTOOPT;
713 }
714
715 return 0;
716bad_sol:
717 return -ENOPROTOOPT;
718
719}
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
746 int addr_len, int flags)
747{
748 struct sock *sk = sock->sk;
749 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
750 long timeo;
751 int err;
752 int ifindex, headroom, tailroom;
753 unsigned int mtu;
754 struct net_device *dev;
755
756 lock_sock(sk);
757
758 err = -EINVAL;
759 if (addr_len < offsetofend(struct sockaddr, sa_family))
760 goto out;
761
762 err = -EAFNOSUPPORT;
763 if (uaddr->sa_family != AF_CAIF)
764 goto out;
765
766 switch (sock->state) {
767 case SS_UNCONNECTED:
768
769 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
770 break;
771 case SS_CONNECTING:
772 switch (sk->sk_state) {
773 case CAIF_CONNECTED:
774 sock->state = SS_CONNECTED;
775 err = -EISCONN;
776 goto out;
777 case CAIF_DISCONNECTED:
778
779 break;
780 case CAIF_CONNECTING:
781 err = -EALREADY;
782 if (flags & O_NONBLOCK)
783 goto out;
784 goto wait_connect;
785 }
786 break;
787 case SS_CONNECTED:
788 caif_assert(sk->sk_state == CAIF_CONNECTED ||
789 sk->sk_state == CAIF_DISCONNECTED);
790 if (sk->sk_shutdown & SHUTDOWN_MASK) {
791
792 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
793 caif_free_client(&cf_sk->layer);
794 break;
795 }
796
797 err = -EISCONN;
798 goto out;
799 case SS_DISCONNECTING:
800 case SS_FREE:
801 caif_assert(1);
802 break;
803 }
804 sk->sk_state = CAIF_DISCONNECTED;
805 sock->state = SS_UNCONNECTED;
806 sk_stream_kill_queues(&cf_sk->sk);
807
808 err = -EINVAL;
809 if (addr_len != sizeof(struct sockaddr_caif))
810 goto out;
811
812 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
813 sizeof(struct sockaddr_caif));
814
815
816 sock->state = SS_CONNECTING;
817 sk->sk_state = CAIF_CONNECTING;
818
819
820
821 if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX)
822 cf_sk->conn_req.priority = CAIF_PRIO_MAX;
823 else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN)
824 cf_sk->conn_req.priority = CAIF_PRIO_MIN;
825 else
826 cf_sk->conn_req.priority = cf_sk->sk.sk_priority;
827
828
829 cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if;
830
831 cf_sk->layer.receive = caif_sktrecv_cb;
832
833 err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
834 &cf_sk->layer, &ifindex, &headroom, &tailroom);
835
836 if (err < 0) {
837 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
838 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
839 goto out;
840 }
841
842 err = -ENODEV;
843 rcu_read_lock();
844 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
845 if (!dev) {
846 rcu_read_unlock();
847 goto out;
848 }
849 cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
850 mtu = dev->mtu;
851 rcu_read_unlock();
852
853 cf_sk->tailroom = tailroom;
854 cf_sk->maxframe = mtu - (headroom + tailroom);
855 if (cf_sk->maxframe < 1) {
856 pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
857 err = -ENODEV;
858 goto out;
859 }
860
861 err = -EINPROGRESS;
862wait_connect:
863
864 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
865 goto out;
866
867 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
868
869 release_sock(sk);
870 err = -ERESTARTSYS;
871 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
872 sk->sk_state != CAIF_CONNECTING,
873 timeo);
874 lock_sock(sk);
875 if (timeo < 0)
876 goto out;
877
878 err = -ETIMEDOUT;
879 if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
880 goto out;
881 if (sk->sk_state != CAIF_CONNECTED) {
882 sock->state = SS_UNCONNECTED;
883 err = sock_error(sk);
884 if (!err)
885 err = -ECONNREFUSED;
886 goto out;
887 }
888 sock->state = SS_CONNECTED;
889 err = 0;
890out:
891 release_sock(sk);
892 return err;
893}
894
895
896
897
898
899static int caif_release(struct socket *sock)
900{
901 struct sock *sk = sock->sk;
902 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
903
904 if (!sk)
905 return 0;
906
907 set_tx_flow_off(cf_sk);
908
909
910
911
912
913
914 spin_lock_bh(&sk->sk_receive_queue.lock);
915 sock_set_flag(sk, SOCK_DEAD);
916 spin_unlock_bh(&sk->sk_receive_queue.lock);
917 sock->sk = NULL;
918
919 WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir));
920 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
921
922 lock_sock(&(cf_sk->sk));
923 sk->sk_state = CAIF_DISCONNECTED;
924 sk->sk_shutdown = SHUTDOWN_MASK;
925
926 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
927 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
928 wake_up_interruptible_poll(sk_sleep(sk), EPOLLERR|EPOLLHUP);
929
930 sock_orphan(sk);
931 sk_stream_kill_queues(&cf_sk->sk);
932 release_sock(sk);
933 sock_put(sk);
934 return 0;
935}
936
937
938static __poll_t caif_poll(struct file *file,
939 struct socket *sock, poll_table *wait)
940{
941 struct sock *sk = sock->sk;
942 __poll_t mask;
943 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
944
945 sock_poll_wait(file, sock, wait);
946 mask = 0;
947
948
949 if (sk->sk_err)
950 mask |= EPOLLERR;
951 if (sk->sk_shutdown == SHUTDOWN_MASK)
952 mask |= EPOLLHUP;
953 if (sk->sk_shutdown & RCV_SHUTDOWN)
954 mask |= EPOLLRDHUP;
955
956
957 if (!skb_queue_empty_lockless(&sk->sk_receive_queue) ||
958 (sk->sk_shutdown & RCV_SHUTDOWN))
959 mask |= EPOLLIN | EPOLLRDNORM;
960
961
962
963
964
965 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
966 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
967
968 return mask;
969}
970
971static const struct proto_ops caif_seqpacket_ops = {
972 .family = PF_CAIF,
973 .owner = THIS_MODULE,
974 .release = caif_release,
975 .bind = sock_no_bind,
976 .connect = caif_connect,
977 .socketpair = sock_no_socketpair,
978 .accept = sock_no_accept,
979 .getname = sock_no_getname,
980 .poll = caif_poll,
981 .ioctl = sock_no_ioctl,
982 .listen = sock_no_listen,
983 .shutdown = sock_no_shutdown,
984 .setsockopt = setsockopt,
985 .sendmsg = caif_seqpkt_sendmsg,
986 .recvmsg = caif_seqpkt_recvmsg,
987 .mmap = sock_no_mmap,
988 .sendpage = sock_no_sendpage,
989};
990
991static const struct proto_ops caif_stream_ops = {
992 .family = PF_CAIF,
993 .owner = THIS_MODULE,
994 .release = caif_release,
995 .bind = sock_no_bind,
996 .connect = caif_connect,
997 .socketpair = sock_no_socketpair,
998 .accept = sock_no_accept,
999 .getname = sock_no_getname,
1000 .poll = caif_poll,
1001 .ioctl = sock_no_ioctl,
1002 .listen = sock_no_listen,
1003 .shutdown = sock_no_shutdown,
1004 .setsockopt = setsockopt,
1005 .sendmsg = caif_stream_sendmsg,
1006 .recvmsg = caif_stream_recvmsg,
1007 .mmap = sock_no_mmap,
1008 .sendpage = sock_no_sendpage,
1009};
1010
1011
1012static void caif_sock_destructor(struct sock *sk)
1013{
1014 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
1015 caif_assert(!refcount_read(&sk->sk_wmem_alloc));
1016 caif_assert(sk_unhashed(sk));
1017 caif_assert(!sk->sk_socket);
1018 if (!sock_flag(sk, SOCK_DEAD)) {
1019 pr_debug("Attempt to release alive CAIF socket: %p\n", sk);
1020 return;
1021 }
1022 sk_stream_kill_queues(&cf_sk->sk);
1023 caif_free_client(&cf_sk->layer);
1024}
1025
1026static int caif_create(struct net *net, struct socket *sock, int protocol,
1027 int kern)
1028{
1029 struct sock *sk = NULL;
1030 struct caifsock *cf_sk = NULL;
1031 static struct proto prot = {.name = "PF_CAIF",
1032 .owner = THIS_MODULE,
1033 .obj_size = sizeof(struct caifsock),
1034 .useroffset = offsetof(struct caifsock, conn_req.param),
1035 .usersize = sizeof_field(struct caifsock, conn_req.param)
1036 };
1037
1038 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1039 return -EPERM;
1040
1041
1042
1043
1044
1045
1046 if (sock->type == SOCK_SEQPACKET)
1047 sock->ops = &caif_seqpacket_ops;
1048 else if (sock->type == SOCK_STREAM)
1049 sock->ops = &caif_stream_ops;
1050 else
1051 return -ESOCKTNOSUPPORT;
1052
1053 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1054 return -EPROTONOSUPPORT;
1055
1056
1057
1058
1059
1060 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot, kern);
1061 if (!sk)
1062 return -ENOMEM;
1063
1064 cf_sk = container_of(sk, struct caifsock, sk);
1065
1066
1067 sk->sk_protocol = (unsigned char) protocol;
1068
1069
1070 switch (protocol) {
1071 case CAIFPROTO_AT:
1072 sk->sk_priority = TC_PRIO_CONTROL;
1073 break;
1074 case CAIFPROTO_RFM:
1075 sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
1076 break;
1077 default:
1078 sk->sk_priority = TC_PRIO_BESTEFFORT;
1079 }
1080
1081
1082
1083
1084
1085 lock_sock(&(cf_sk->sk));
1086
1087
1088 sock_init_data(sock, sk);
1089 sk->sk_destruct = caif_sock_destructor;
1090
1091 mutex_init(&cf_sk->readlock);
1092 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1093 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1094 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
1095
1096 set_tx_flow_off(cf_sk);
1097 set_rx_flow_on(cf_sk);
1098
1099
1100 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
1101 cf_sk->conn_req.protocol = protocol;
1102 release_sock(&cf_sk->sk);
1103 return 0;
1104}
1105
1106
1107static const struct net_proto_family caif_family_ops = {
1108 .family = PF_CAIF,
1109 .create = caif_create,
1110 .owner = THIS_MODULE,
1111};
1112
1113static int __init caif_sktinit_module(void)
1114{
1115 return sock_register(&caif_family_ops);
1116}
1117
1118static void __exit caif_sktexit_module(void)
1119{
1120 sock_unregister(PF_CAIF);
1121}
1122module_init(caif_sktinit_module);
1123module_exit(caif_sktexit_module);
1124