net: PPPoPNS and PPPoLAC fixes.
[firefly-linux-kernel-4.4.55.git] / drivers / net / ppp / pppolac.c
1 /* drivers/net/pppolac.c
2  *
3  * Driver for PPP on L2TP Access Concentrator / PPPoLAC Socket (RFC 2661)
4  *
5  * Copyright (C) 2009 Google, Inc.
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 /* This driver handles L2TP data packets between a UDP socket and a PPP channel.
18  * To keep things simple, only one session per socket is permitted. Packets are
19  * sent via the socket, so it must keep connected to the same address. One must
20  * not set sequencing in ICCN but let LNS controll it. Currently this driver
21  * only works on IPv4 due to the lack of UDP encapsulation support in IPv6. */
22
23 #include <linux/module.h>
24 #include <linux/workqueue.h>
25 #include <linux/skbuff.h>
26 #include <linux/file.h>
27 #include <linux/netdevice.h>
28 #include <linux/net.h>
29 #include <linux/udp.h>
30 #include <linux/ppp_defs.h>
31 #include <linux/if_ppp.h>
32 #include <linux/if_pppox.h>
33 #include <linux/ppp_channel.h>
34 #include <net/tcp_states.h>
35 #include <asm/uaccess.h>
36
37 #define L2TP_CONTROL_BIT        0x80
38 #define L2TP_LENGTH_BIT         0x40
39 #define L2TP_SEQUENCE_BIT       0x08
40 #define L2TP_OFFSET_BIT         0x02
41 #define L2TP_VERSION            0x02
42 #define L2TP_VERSION_MASK       0x0F
43
44 #define PPP_ADDR        0xFF
45 #define PPP_CTRL        0x03
46
47 union unaligned {
48         __u32 u32;
49 } __attribute__((packed));
50
51 static inline union unaligned *unaligned(void *ptr)
52 {
53         return (union unaligned *)ptr;
54 }
55
56 static int pppolac_recv_core(struct sock *sk_udp, struct sk_buff *skb)
57 {
58         struct sock *sk = (struct sock *)sk_udp->sk_user_data;
59         struct pppolac_opt *opt = &pppox_sk(sk)->proto.lac;
60         __u8 bits;
61         __u8 *ptr;
62
63         /* Drop the packet if it is too short. */
64         if (skb->len < sizeof(struct udphdr) + 6)
65                 goto drop;
66
67         /* Put it back if it is a control packet. */
68         if (skb->data[sizeof(struct udphdr)] & L2TP_CONTROL_BIT)
69                 return opt->backlog_rcv(sk_udp, skb);
70
71         /* Skip UDP header. */
72         skb_pull(skb, sizeof(struct udphdr));
73
74         /* Check the version. */
75         if ((skb->data[1] & L2TP_VERSION_MASK) != L2TP_VERSION)
76                 goto drop;
77         bits = skb->data[0];
78         ptr = &skb->data[2];
79
80         /* Check the length if it is present. */
81         if (bits & L2TP_LENGTH_BIT) {
82                 if ((ptr[0] << 8 | ptr[1]) != skb->len)
83                         goto drop;
84                 ptr += 2;
85         }
86
87         /* Skip all fields including optional ones. */
88         if (!skb_pull(skb, 6 + (bits & L2TP_SEQUENCE_BIT ? 4 : 0) +
89                         (bits & L2TP_LENGTH_BIT ? 2 : 0) +
90                         (bits & L2TP_OFFSET_BIT ? 2 : 0)))
91                 goto drop;
92
93         /* Skip the offset padding if it is present. */
94         if (bits & L2TP_OFFSET_BIT &&
95                         !skb_pull(skb, skb->data[-2] << 8 | skb->data[-1]))
96                 goto drop;
97
98         /* Check the tunnel and the session. */
99         if (unaligned(ptr)->u32 != opt->local)
100                 goto drop;
101
102         /* Check the sequence if it is present. According to RFC 2661 section
103          * 5.4, the only thing to do is to update opt->sequencing. */
104         opt->sequencing = bits & L2TP_SEQUENCE_BIT;
105
106         /* Skip PPP address and control if they are present. */
107         if (skb->len >= 2 && skb->data[0] == PPP_ADDR &&
108                         skb->data[1] == PPP_CTRL)
109                 skb_pull(skb, 2);
110
111         /* Fix PPP protocol if it is compressed. */
112         if (skb->len >= 1 && skb->data[0] & 1)
113                 skb_push(skb, 1)[0] = 0;
114
115         /* Finally, deliver the packet to PPP channel. */
116         skb_orphan(skb);
117         ppp_input(&pppox_sk(sk)->chan, skb);
118         return NET_RX_SUCCESS;
119 drop:
120         kfree_skb(skb);
121         return NET_RX_DROP;
122 }
123
124 static int pppolac_recv(struct sock *sk_udp, struct sk_buff *skb)
125 {
126         sock_hold(sk_udp);
127         sk_receive_skb(sk_udp, skb, 0);
128         return 0;
129 }
130
131 static struct sk_buff_head delivery_queue;
132
133 static void pppolac_xmit_core(struct work_struct *delivery_work)
134 {
135         mm_segment_t old_fs = get_fs();
136         struct sk_buff *skb;
137
138         set_fs(KERNEL_DS);
139         while ((skb = skb_dequeue(&delivery_queue))) {
140                 struct sock *sk_udp = skb->sk;
141                 struct kvec iov = {.iov_base = skb->data, .iov_len = skb->len};
142                 struct msghdr msg = {
143                         .msg_iov = (struct iovec *)&iov,
144                         .msg_iovlen = 1,
145                         .msg_flags = MSG_NOSIGNAL | MSG_DONTWAIT,
146                 };
147                 sk_udp->sk_prot->sendmsg(NULL, sk_udp, &msg, skb->len);
148                 kfree_skb(skb);
149         }
150         set_fs(old_fs);
151 }
152
153 static DECLARE_WORK(delivery_work, pppolac_xmit_core);
154
155 static int pppolac_xmit(struct ppp_channel *chan, struct sk_buff *skb)
156 {
157         struct sock *sk_udp = (struct sock *)chan->private;
158         struct pppolac_opt *opt = &pppox_sk(sk_udp->sk_user_data)->proto.lac;
159
160         /* Install PPP address and control. */
161         skb_push(skb, 2);
162         skb->data[0] = PPP_ADDR;
163         skb->data[1] = PPP_CTRL;
164
165         /* Install L2TP header. */
166         if (opt->sequencing) {
167                 skb_push(skb, 10);
168                 skb->data[0] = L2TP_SEQUENCE_BIT;
169                 skb->data[6] = opt->sequence >> 8;
170                 skb->data[7] = opt->sequence;
171                 skb->data[8] = 0;
172                 skb->data[9] = 0;
173                 opt->sequence++;
174         } else {
175                 skb_push(skb, 6);
176                 skb->data[0] = 0;
177         }
178         skb->data[1] = L2TP_VERSION;
179         unaligned(&skb->data[2])->u32 = opt->remote;
180
181         /* Now send the packet via the delivery queue. */
182         skb_set_owner_w(skb, sk_udp);
183         skb_queue_tail(&delivery_queue, skb);
184         schedule_work(&delivery_work);
185         return 1;
186 }
187
188 /******************************************************************************/
189
190 static struct ppp_channel_ops pppolac_channel_ops = {
191         .start_xmit = pppolac_xmit,
192 };
193
194 static int pppolac_connect(struct socket *sock, struct sockaddr *useraddr,
195         int addrlen, int flags)
196 {
197         struct sock *sk = sock->sk;
198         struct pppox_sock *po = pppox_sk(sk);
199         struct sockaddr_pppolac *addr = (struct sockaddr_pppolac *)useraddr;
200         struct socket *sock_udp = NULL;
201         struct sock *sk_udp;
202         int error;
203
204         if (addrlen != sizeof(struct sockaddr_pppolac) ||
205                         !addr->local.tunnel || !addr->local.session ||
206                         !addr->remote.tunnel || !addr->remote.session) {
207                 return -EINVAL;
208         }
209
210         lock_sock(sk);
211         error = -EALREADY;
212         if (sk->sk_state != PPPOX_NONE)
213                 goto out;
214
215         sock_udp = sockfd_lookup(addr->udp_socket, &error);
216         if (!sock_udp)
217                 goto out;
218         sk_udp = sock_udp->sk;
219         lock_sock(sk_udp);
220
221         /* Remove this check when IPv6 supports UDP encapsulation. */
222         error = -EAFNOSUPPORT;
223         if (sk_udp->sk_family != AF_INET)
224                 goto out;
225         error = -EPROTONOSUPPORT;
226         if (sk_udp->sk_protocol != IPPROTO_UDP)
227                 goto out;
228         error = -EDESTADDRREQ;
229         if (sk_udp->sk_state != TCP_ESTABLISHED)
230                 goto out;
231         error = -EBUSY;
232         if (udp_sk(sk_udp)->encap_type || sk_udp->sk_user_data)
233                 goto out;
234         if (!sk_udp->sk_bound_dev_if) {
235                 struct dst_entry *dst = sk_dst_get(sk_udp);
236                 error = -ENODEV;
237                 if (!dst)
238                         goto out;
239                 sk_udp->sk_bound_dev_if = dst->dev->ifindex;
240                 dst_release(dst);
241         }
242
243         po->chan.hdrlen = 12;
244         po->chan.private = sk_udp;
245         po->chan.ops = &pppolac_channel_ops;
246         po->chan.mtu = PPP_MTU - 80;
247         po->proto.lac.local = unaligned(&addr->local)->u32;
248         po->proto.lac.remote = unaligned(&addr->remote)->u32;
249         po->proto.lac.backlog_rcv = sk_udp->sk_backlog_rcv;
250
251         error = ppp_register_channel(&po->chan);
252         if (error)
253                 goto out;
254
255         sk->sk_state = PPPOX_CONNECTED;
256         udp_sk(sk_udp)->encap_type = UDP_ENCAP_L2TPINUDP;
257         udp_sk(sk_udp)->encap_rcv = pppolac_recv;
258         sk_udp->sk_backlog_rcv = pppolac_recv_core;
259         sk_udp->sk_user_data = sk;
260 out:
261         if (sock_udp) {
262                 release_sock(sk_udp);
263                 if (error)
264                         sockfd_put(sock_udp);
265         }
266         release_sock(sk);
267         return error;
268 }
269
270 static int pppolac_release(struct socket *sock)
271 {
272         struct sock *sk = sock->sk;
273
274         if (!sk)
275                 return 0;
276
277         lock_sock(sk);
278         if (sock_flag(sk, SOCK_DEAD)) {
279                 release_sock(sk);
280                 return -EBADF;
281         }
282
283         if (sk->sk_state != PPPOX_NONE) {
284                 struct sock *sk_udp = (struct sock *)pppox_sk(sk)->chan.private;
285                 lock_sock(sk_udp);
286                 pppox_unbind_sock(sk);
287                 udp_sk(sk_udp)->encap_type = 0;
288                 udp_sk(sk_udp)->encap_rcv = NULL;
289                 sk_udp->sk_backlog_rcv = pppox_sk(sk)->proto.lac.backlog_rcv;
290                 sk_udp->sk_user_data = NULL;
291                 release_sock(sk_udp);
292                 sockfd_put(sk_udp->sk_socket);
293         }
294
295         sock_orphan(sk);
296         sock->sk = NULL;
297         release_sock(sk);
298         sock_put(sk);
299         return 0;
300 }
301
302 /******************************************************************************/
303
304 static struct proto pppolac_proto = {
305         .name = "PPPOLAC",
306         .owner = THIS_MODULE,
307         .obj_size = sizeof(struct pppox_sock),
308 };
309
310 static struct proto_ops pppolac_proto_ops = {
311         .family = PF_PPPOX,
312         .owner = THIS_MODULE,
313         .release = pppolac_release,
314         .bind = sock_no_bind,
315         .connect = pppolac_connect,
316         .socketpair = sock_no_socketpair,
317         .accept = sock_no_accept,
318         .getname = sock_no_getname,
319         .poll = sock_no_poll,
320         .ioctl = pppox_ioctl,
321         .listen = sock_no_listen,
322         .shutdown = sock_no_shutdown,
323         .setsockopt = sock_no_setsockopt,
324         .getsockopt = sock_no_getsockopt,
325         .sendmsg = sock_no_sendmsg,
326         .recvmsg = sock_no_recvmsg,
327         .mmap = sock_no_mmap,
328 };
329
330 static int pppolac_create(struct net *net, struct socket *sock)
331 {
332         struct sock *sk;
333
334         sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppolac_proto);
335         if (!sk)
336                 return -ENOMEM;
337
338         sock_init_data(sock, sk);
339         sock->state = SS_UNCONNECTED;
340         sock->ops = &pppolac_proto_ops;
341         sk->sk_protocol = PX_PROTO_OLAC;
342         sk->sk_state = PPPOX_NONE;
343         return 0;
344 }
345
346 /******************************************************************************/
347
348 static struct pppox_proto pppolac_pppox_proto = {
349         .create = pppolac_create,
350         .owner = THIS_MODULE,
351 };
352
353 static int __init pppolac_init(void)
354 {
355         int error;
356
357         error = proto_register(&pppolac_proto, 0);
358         if (error)
359                 return error;
360
361         error = register_pppox_proto(PX_PROTO_OLAC, &pppolac_pppox_proto);
362         if (error)
363                 proto_unregister(&pppolac_proto);
364         else
365                 skb_queue_head_init(&delivery_queue);
366         return error;
367 }
368
369 static void __exit pppolac_exit(void)
370 {
371         unregister_pppox_proto(PX_PROTO_OLAC);
372         proto_unregister(&pppolac_proto);
373 }
374
375 module_init(pppolac_init);
376 module_exit(pppolac_exit);
377
378 MODULE_DESCRIPTION("PPP on L2TP Access Concentrator (PPPoLAC)");
379 MODULE_AUTHOR("Chia-chi Yeh <chiachi@android.com>");
380 MODULE_LICENSE("GPL");