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