l2tp: hold tunnel socket when handling control frames in l2tp_ip and l2tp_ip6
[pandora-kernel.git] / net / l2tp / l2tp_ip.c
1 /*
2  * L2TPv3 IP encapsulation support
3  *
4  * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License
8  *      as published by the Free Software Foundation; either version
9  *      2 of the License, or (at your option) any later version.
10  */
11
12 #include <asm/ioctls.h>
13 #include <linux/icmp.h>
14 #include <linux/module.h>
15 #include <linux/skbuff.h>
16 #include <linux/random.h>
17 #include <linux/socket.h>
18 #include <linux/l2tp.h>
19 #include <linux/in.h>
20 #include <net/sock.h>
21 #include <net/ip.h>
22 #include <net/icmp.h>
23 #include <net/udp.h>
24 #include <net/inet_common.h>
25 #include <net/inet_hashtables.h>
26 #include <net/tcp_states.h>
27 #include <net/protocol.h>
28 #include <net/xfrm.h>
29
30 #include "l2tp_core.h"
31
32 struct l2tp_ip_sock {
33         /* inet_sock has to be the first member of l2tp_ip_sock */
34         struct inet_sock        inet;
35
36         __u32                   conn_id;
37         __u32                   peer_conn_id;
38
39         __u64                   tx_packets;
40         __u64                   tx_bytes;
41         __u64                   tx_errors;
42         __u64                   rx_packets;
43         __u64                   rx_bytes;
44         __u64                   rx_errors;
45 };
46
47 static DEFINE_RWLOCK(l2tp_ip_lock);
48 static struct hlist_head l2tp_ip_table;
49 static struct hlist_head l2tp_ip_bind_table;
50
51 static inline struct l2tp_ip_sock *l2tp_ip_sk(const struct sock *sk)
52 {
53         return (struct l2tp_ip_sock *)sk;
54 }
55
56 static struct sock *__l2tp_ip_bind_lookup(struct net *net, __be32 laddr, int dif, u32 tunnel_id)
57 {
58         struct hlist_node *node;
59         struct sock *sk;
60
61         sk_for_each_bound(sk, node, &l2tp_ip_bind_table) {
62                 struct inet_sock *inet = inet_sk(sk);
63                 struct l2tp_ip_sock *l2tp = l2tp_ip_sk(sk);
64
65                 if (l2tp == NULL)
66                         continue;
67
68                 if ((l2tp->conn_id == tunnel_id) &&
69                     net_eq(sock_net(sk), net) &&
70                     !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
71                     !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
72                         goto found;
73         }
74
75         sk = NULL;
76 found:
77         return sk;
78 }
79
80 static inline struct sock *l2tp_ip_bind_lookup(struct net *net, __be32 laddr, int dif, u32 tunnel_id)
81 {
82         struct sock *sk = __l2tp_ip_bind_lookup(net, laddr, dif, tunnel_id);
83         if (sk)
84                 sock_hold(sk);
85
86         return sk;
87 }
88
89 /* When processing receive frames, there are two cases to
90  * consider. Data frames consist of a non-zero session-id and an
91  * optional cookie. Control frames consist of a regular L2TP header
92  * preceded by 32-bits of zeros.
93  *
94  * L2TPv3 Session Header Over IP
95  *
96  *  0                   1                   2                   3
97  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
98  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99  * |                           Session ID                          |
100  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101  * |               Cookie (optional, maximum 64 bits)...
102  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103  *                                                                 |
104  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105  *
106  * L2TPv3 Control Message Header Over IP
107  *
108  *  0                   1                   2                   3
109  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
110  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111  * |                      (32 bits of zeros)                       |
112  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113  * |T|L|x|x|S|x|x|x|x|x|x|x|  Ver  |             Length            |
114  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115  * |                     Control Connection ID                     |
116  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117  * |               Ns              |               Nr              |
118  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119  *
120  * All control frames are passed to userspace.
121  */
122 static int l2tp_ip_recv(struct sk_buff *skb)
123 {
124         struct sock *sk;
125         u32 session_id;
126         u32 tunnel_id;
127         unsigned char *ptr, *optr;
128         struct l2tp_session *session;
129         struct l2tp_tunnel *tunnel = NULL;
130         int length;
131         int offset;
132
133         if (!pskb_may_pull(skb, 4))
134                 goto discard;
135
136         /* Point to L2TP header */
137         optr = ptr = skb->data;
138         session_id = ntohl(*((__be32 *) ptr));
139         ptr += 4;
140
141         /* RFC3931: L2TP/IP packets have the first 4 bytes containing
142          * the session_id. If it is 0, the packet is a L2TP control
143          * frame and the session_id value can be discarded.
144          */
145         if (session_id == 0) {
146                 __skb_pull(skb, 4);
147                 goto pass_up;
148         }
149
150         /* Ok, this is a data packet. Lookup the session. */
151         session = l2tp_session_get(&init_net, NULL, session_id, true);
152         if (!session)
153                 goto discard;
154
155         tunnel = session->tunnel;
156         if (!tunnel)
157                 goto discard_sess;
158
159         /* Trace packet contents, if enabled */
160         if (tunnel->debug & L2TP_MSG_DATA) {
161                 length = min(32u, skb->len);
162                 if (!pskb_may_pull(skb, length))
163                         goto discard_sess;
164
165                 /* Point to L2TP header */
166                 optr = ptr = skb->data;
167                 ptr += 4;
168                 printk(KERN_DEBUG "%s: ip recv: ", tunnel->name);
169
170                 offset = 0;
171                 do {
172                         printk(" %02X", ptr[offset]);
173                 } while (++offset < length);
174
175                 printk("\n");
176         }
177
178         l2tp_recv_common(session, skb, ptr, optr, 0, skb->len, tunnel->recv_payload_hook);
179         l2tp_session_dec_refcount(session);
180
181         return 0;
182
183 pass_up:
184         /* Get the tunnel_id from the L2TP header */
185         if (!pskb_may_pull(skb, 12))
186                 goto discard;
187
188         if ((skb->data[0] & 0xc0) != 0xc0)
189                 goto discard;
190
191         tunnel_id = ntohl(*(__be32 *) &skb->data[4]);
192         tunnel = l2tp_tunnel_find(&init_net, tunnel_id);
193         if (tunnel) {
194                 sk = tunnel->sock;
195                 sock_hold(sk);
196         } else {
197                 struct iphdr *iph = (struct iphdr *) skb_network_header(skb);
198
199                 read_lock_bh(&l2tp_ip_lock);
200                 sk = __l2tp_ip_bind_lookup(&init_net, iph->daddr, 0, tunnel_id);
201                 if (!sk) {
202                         read_unlock_bh(&l2tp_ip_lock);
203                         goto discard;
204                 }
205
206                 sock_hold(sk);
207                 read_unlock_bh(&l2tp_ip_lock);
208         }
209
210         if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
211                 goto discard_put;
212
213         nf_reset(skb);
214
215         return sk_receive_skb(sk, skb, 1);
216
217 discard_sess:
218         if (session->deref)
219                 session->deref(session);
220         l2tp_session_dec_refcount(session);
221         goto discard;
222
223 discard_put:
224         sock_put(sk);
225
226 discard:
227         kfree_skb(skb);
228         return 0;
229 }
230
231 static int l2tp_ip_open(struct sock *sk)
232 {
233         /* Prevent autobind. We don't have ports. */
234         inet_sk(sk)->inet_num = IPPROTO_L2TP;
235
236         write_lock_bh(&l2tp_ip_lock);
237         sk_add_node(sk, &l2tp_ip_table);
238         write_unlock_bh(&l2tp_ip_lock);
239
240         return 0;
241 }
242
243 static void l2tp_ip_close(struct sock *sk, long timeout)
244 {
245         write_lock_bh(&l2tp_ip_lock);
246         hlist_del_init(&sk->sk_bind_node);
247         hlist_del_init(&sk->sk_node);
248         write_unlock_bh(&l2tp_ip_lock);
249         sk_common_release(sk);
250 }
251
252 static void l2tp_ip_destroy_sock(struct sock *sk)
253 {
254         struct sk_buff *skb;
255
256         while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL)
257                 kfree_skb(skb);
258
259         sk_refcnt_debug_dec(sk);
260 }
261
262 static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
263 {
264         struct inet_sock *inet = inet_sk(sk);
265         struct sockaddr_l2tpip *addr = (struct sockaddr_l2tpip *) uaddr;
266         int ret;
267         int chk_addr_ret;
268
269         if (addr_len < sizeof(struct sockaddr_l2tpip))
270                 return -EINVAL;
271         if (addr->l2tp_family != AF_INET)
272                 return -EINVAL;
273
274         ret = -EADDRINUSE;
275         read_lock_bh(&l2tp_ip_lock);
276         if (__l2tp_ip_bind_lookup(&init_net, addr->l2tp_addr.s_addr, sk->sk_bound_dev_if, addr->l2tp_conn_id))
277                 goto out_in_use;
278
279         read_unlock_bh(&l2tp_ip_lock);
280
281         lock_sock(sk);
282         if (!sock_flag(sk, SOCK_ZAPPED))
283                 goto out;
284
285         if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_l2tpip))
286                 goto out;
287
288         chk_addr_ret = inet_addr_type(&init_net, addr->l2tp_addr.s_addr);
289         ret = -EADDRNOTAVAIL;
290         if (addr->l2tp_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
291             chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
292                 goto out;
293
294         inet->inet_rcv_saddr = inet->inet_saddr = addr->l2tp_addr.s_addr;
295         if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
296                 inet->inet_saddr = 0;  /* Use device */
297         sk_dst_reset(sk);
298
299         l2tp_ip_sk(sk)->conn_id = addr->l2tp_conn_id;
300
301         write_lock_bh(&l2tp_ip_lock);
302         sk_add_bind_node(sk, &l2tp_ip_bind_table);
303         sk_del_node_init(sk);
304         write_unlock_bh(&l2tp_ip_lock);
305         ret = 0;
306         sock_reset_flag(sk, SOCK_ZAPPED);
307
308 out:
309         release_sock(sk);
310
311         return ret;
312
313 out_in_use:
314         read_unlock_bh(&l2tp_ip_lock);
315
316         return ret;
317 }
318
319 static int l2tp_ip_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
320 {
321         struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *) uaddr;
322         struct inet_sock *inet = inet_sk(sk);
323         struct flowi4 *fl4;
324         struct rtable *rt;
325         __be32 saddr;
326         int oif, rc;
327
328         if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
329                 return -EINVAL;
330
331         if (addr_len < sizeof(*lsa))
332                 return -EINVAL;
333
334         if (lsa->l2tp_family != AF_INET)
335                 return -EAFNOSUPPORT;
336
337         lock_sock(sk);
338
339         sk_dst_reset(sk);
340
341         oif = sk->sk_bound_dev_if;
342         saddr = inet->inet_saddr;
343
344         rc = -EINVAL;
345         if (ipv4_is_multicast(lsa->l2tp_addr.s_addr))
346                 goto out;
347
348         fl4 = &inet->cork.fl.u.ip4;
349         rt = ip_route_connect(fl4, lsa->l2tp_addr.s_addr, saddr,
350                               RT_CONN_FLAGS(sk), oif,
351                               IPPROTO_L2TP,
352                               0, 0, sk, true);
353         if (IS_ERR(rt)) {
354                 rc = PTR_ERR(rt);
355                 if (rc == -ENETUNREACH)
356                         IP_INC_STATS_BH(&init_net, IPSTATS_MIB_OUTNOROUTES);
357                 goto out;
358         }
359
360         rc = -ENETUNREACH;
361         if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
362                 ip_rt_put(rt);
363                 goto out;
364         }
365
366         l2tp_ip_sk(sk)->peer_conn_id = lsa->l2tp_conn_id;
367
368         if (!inet->inet_saddr)
369                 inet->inet_saddr = fl4->saddr;
370         if (!inet->inet_rcv_saddr)
371                 inet->inet_rcv_saddr = fl4->saddr;
372         inet->inet_daddr = fl4->daddr;
373         sk->sk_state = TCP_ESTABLISHED;
374         inet->inet_id = jiffies;
375
376         sk_dst_set(sk, &rt->dst);
377
378         write_lock_bh(&l2tp_ip_lock);
379         hlist_del_init(&sk->sk_bind_node);
380         sk_add_bind_node(sk, &l2tp_ip_bind_table);
381         write_unlock_bh(&l2tp_ip_lock);
382
383         rc = 0;
384 out:
385         release_sock(sk);
386         return rc;
387 }
388
389 static int l2tp_ip_disconnect(struct sock *sk, int flags)
390 {
391         if (sock_flag(sk, SOCK_ZAPPED))
392                 return 0;
393
394         return udp_disconnect(sk, flags);
395 }
396
397 static int l2tp_ip_getname(struct socket *sock, struct sockaddr *uaddr,
398                            int *uaddr_len, int peer)
399 {
400         struct sock *sk         = sock->sk;
401         struct inet_sock *inet  = inet_sk(sk);
402         struct l2tp_ip_sock *lsk = l2tp_ip_sk(sk);
403         struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *)uaddr;
404
405         memset(lsa, 0, sizeof(*lsa));
406         lsa->l2tp_family = AF_INET;
407         if (peer) {
408                 if (!inet->inet_dport)
409                         return -ENOTCONN;
410                 lsa->l2tp_conn_id = lsk->peer_conn_id;
411                 lsa->l2tp_addr.s_addr = inet->inet_daddr;
412         } else {
413                 __be32 addr = inet->inet_rcv_saddr;
414                 if (!addr)
415                         addr = inet->inet_saddr;
416                 lsa->l2tp_conn_id = lsk->conn_id;
417                 lsa->l2tp_addr.s_addr = addr;
418         }
419         *uaddr_len = sizeof(*lsa);
420         return 0;
421 }
422
423 static int l2tp_ip_backlog_recv(struct sock *sk, struct sk_buff *skb)
424 {
425         int rc;
426
427         /* Charge it to the socket, dropping if the queue is full. */
428         rc = sock_queue_rcv_skb(sk, skb);
429         if (rc < 0)
430                 goto drop;
431
432         return 0;
433
434 drop:
435         IP_INC_STATS(&init_net, IPSTATS_MIB_INDISCARDS);
436         kfree_skb(skb);
437         return 0;
438 }
439
440 /* Userspace will call sendmsg() on the tunnel socket to send L2TP
441  * control frames.
442  */
443 static int l2tp_ip_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, size_t len)
444 {
445         struct sk_buff *skb;
446         int rc;
447         struct l2tp_ip_sock *lsa = l2tp_ip_sk(sk);
448         struct inet_sock *inet = inet_sk(sk);
449         struct rtable *rt = NULL;
450         struct flowi4 *fl4;
451         int connected = 0;
452         __be32 daddr;
453
454         lock_sock(sk);
455
456         rc = -ENOTCONN;
457         if (sock_flag(sk, SOCK_DEAD))
458                 goto out;
459
460         /* Get and verify the address. */
461         if (msg->msg_name) {
462                 struct sockaddr_l2tpip *lip = (struct sockaddr_l2tpip *) msg->msg_name;
463                 rc = -EINVAL;
464                 if (msg->msg_namelen < sizeof(*lip))
465                         goto out;
466
467                 if (lip->l2tp_family != AF_INET) {
468                         rc = -EAFNOSUPPORT;
469                         if (lip->l2tp_family != AF_UNSPEC)
470                                 goto out;
471                 }
472
473                 daddr = lip->l2tp_addr.s_addr;
474         } else {
475                 rc = -EDESTADDRREQ;
476                 if (sk->sk_state != TCP_ESTABLISHED)
477                         goto out;
478
479                 daddr = inet->inet_daddr;
480                 connected = 1;
481         }
482
483         /* Allocate a socket buffer */
484         rc = -ENOMEM;
485         skb = sock_wmalloc(sk, 2 + NET_SKB_PAD + sizeof(struct iphdr) +
486                            4 + len, 0, GFP_KERNEL);
487         if (!skb)
488                 goto error;
489
490         /* Reserve space for headers, putting IP header on 4-byte boundary. */
491         skb_reserve(skb, 2 + NET_SKB_PAD);
492         skb_reset_network_header(skb);
493         skb_reserve(skb, sizeof(struct iphdr));
494         skb_reset_transport_header(skb);
495
496         /* Insert 0 session_id */
497         *((__be32 *) skb_put(skb, 4)) = 0;
498
499         /* Copy user data into skb */
500         rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
501         if (rc < 0) {
502                 kfree_skb(skb);
503                 goto error;
504         }
505
506         fl4 = &inet->cork.fl.u.ip4;
507         if (connected)
508                 rt = (struct rtable *) __sk_dst_check(sk, 0);
509
510         rcu_read_lock();
511         if (rt == NULL) {
512                 const struct ip_options_rcu *inet_opt;
513
514                 inet_opt = rcu_dereference(inet->inet_opt);
515
516                 /* Use correct destination address if we have options. */
517                 if (inet_opt && inet_opt->opt.srr)
518                         daddr = inet_opt->opt.faddr;
519
520                 /* If this fails, retransmit mechanism of transport layer will
521                  * keep trying until route appears or the connection times
522                  * itself out.
523                  */
524                 rt = ip_route_output_ports(sock_net(sk), fl4, sk,
525                                            daddr, inet->inet_saddr,
526                                            inet->inet_dport, inet->inet_sport,
527                                            sk->sk_protocol, RT_CONN_FLAGS(sk),
528                                            sk->sk_bound_dev_if);
529                 if (IS_ERR(rt))
530                         goto no_route;
531                 if (connected) {
532                         sk_setup_caps(sk, &rt->dst);
533                 } else {
534                         skb_dst_set(skb, &rt->dst);
535                         goto xmit;
536                 }
537         }
538
539         /* We dont need to clone dst here, it is guaranteed to not disappear.
540          *  __dev_xmit_skb() might force a refcount if needed.
541          */
542         skb_dst_set_noref(skb, &rt->dst);
543
544 xmit:
545         /* Queue the packet to IP for output */
546         rc = ip_queue_xmit(skb, &inet->cork.fl);
547         rcu_read_unlock();
548
549 error:
550         /* Update stats */
551         if (rc >= 0) {
552                 lsa->tx_packets++;
553                 lsa->tx_bytes += len;
554                 rc = len;
555         } else {
556                 lsa->tx_errors++;
557         }
558
559 out:
560         release_sock(sk);
561         return rc;
562
563 no_route:
564         rcu_read_unlock();
565         IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
566         kfree_skb(skb);
567         rc = -EHOSTUNREACH;
568         goto out;
569 }
570
571 static int l2tp_ip_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
572                            size_t len, int noblock, int flags, int *addr_len)
573 {
574         struct inet_sock *inet = inet_sk(sk);
575         struct l2tp_ip_sock *lsk = l2tp_ip_sk(sk);
576         size_t copied = 0;
577         int err = -EOPNOTSUPP;
578         struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
579         struct sk_buff *skb;
580
581         if (flags & MSG_OOB)
582                 goto out;
583
584         skb = skb_recv_datagram(sk, flags, noblock, &err);
585         if (!skb)
586                 goto out;
587
588         copied = skb->len;
589         if (len < copied) {
590                 msg->msg_flags |= MSG_TRUNC;
591                 copied = len;
592         }
593
594         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
595         if (err)
596                 goto done;
597
598         sock_recv_timestamp(msg, sk, skb);
599
600         /* Copy the address. */
601         if (sin) {
602                 sin->sin_family = AF_INET;
603                 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
604                 sin->sin_port = 0;
605                 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
606                 *addr_len = sizeof(*sin);
607         }
608         if (inet->cmsg_flags)
609                 ip_cmsg_recv(msg, skb);
610         if (flags & MSG_TRUNC)
611                 copied = skb->len;
612 done:
613         skb_free_datagram(sk, skb);
614 out:
615         if (err) {
616                 lsk->rx_errors++;
617                 return err;
618         }
619
620         lsk->rx_packets++;
621         lsk->rx_bytes += copied;
622
623         return copied;
624 }
625
626 int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg)
627 {
628         struct sk_buff *skb;
629         int amount;
630
631         switch (cmd) {
632         case SIOCOUTQ:
633                 amount = sk_wmem_alloc_get(sk);
634                 break;
635         case SIOCINQ:
636                 spin_lock_bh(&sk->sk_receive_queue.lock);
637                 skb = skb_peek(&sk->sk_receive_queue);
638                 amount = skb ? skb->len : 0;
639                 spin_unlock_bh(&sk->sk_receive_queue.lock);
640                 break;
641
642         default:
643                 return -ENOIOCTLCMD;
644         }
645
646         return put_user(amount, (int __user *)arg);
647 }
648 EXPORT_SYMBOL(l2tp_ioctl);
649
650 static struct proto l2tp_ip_prot = {
651         .name              = "L2TP/IP",
652         .owner             = THIS_MODULE,
653         .init              = l2tp_ip_open,
654         .close             = l2tp_ip_close,
655         .bind              = l2tp_ip_bind,
656         .connect           = l2tp_ip_connect,
657         .disconnect        = l2tp_ip_disconnect,
658         .ioctl             = l2tp_ioctl,
659         .destroy           = l2tp_ip_destroy_sock,
660         .setsockopt        = ip_setsockopt,
661         .getsockopt        = ip_getsockopt,
662         .sendmsg           = l2tp_ip_sendmsg,
663         .recvmsg           = l2tp_ip_recvmsg,
664         .backlog_rcv       = l2tp_ip_backlog_recv,
665         .hash              = inet_hash,
666         .unhash            = inet_unhash,
667         .obj_size          = sizeof(struct l2tp_ip_sock),
668 #ifdef CONFIG_COMPAT
669         .compat_setsockopt = compat_ip_setsockopt,
670         .compat_getsockopt = compat_ip_getsockopt,
671 #endif
672 };
673
674 static const struct proto_ops l2tp_ip_ops = {
675         .family            = PF_INET,
676         .owner             = THIS_MODULE,
677         .release           = inet_release,
678         .bind              = inet_bind,
679         .connect           = inet_dgram_connect,
680         .socketpair        = sock_no_socketpair,
681         .accept            = sock_no_accept,
682         .getname           = l2tp_ip_getname,
683         .poll              = datagram_poll,
684         .ioctl             = inet_ioctl,
685         .listen            = sock_no_listen,
686         .shutdown          = inet_shutdown,
687         .setsockopt        = sock_common_setsockopt,
688         .getsockopt        = sock_common_getsockopt,
689         .sendmsg           = inet_sendmsg,
690         .recvmsg           = sock_common_recvmsg,
691         .mmap              = sock_no_mmap,
692         .sendpage          = sock_no_sendpage,
693 #ifdef CONFIG_COMPAT
694         .compat_setsockopt = compat_sock_common_setsockopt,
695         .compat_getsockopt = compat_sock_common_getsockopt,
696 #endif
697 };
698
699 static struct inet_protosw l2tp_ip_protosw = {
700         .type           = SOCK_DGRAM,
701         .protocol       = IPPROTO_L2TP,
702         .prot           = &l2tp_ip_prot,
703         .ops            = &l2tp_ip_ops,
704         .no_check       = 0,
705 };
706
707 static struct net_protocol l2tp_ip_protocol __read_mostly = {
708         .handler        = l2tp_ip_recv,
709 };
710
711 static int __init l2tp_ip_init(void)
712 {
713         int err;
714
715         printk(KERN_INFO "L2TP IP encapsulation support (L2TPv3)\n");
716
717         err = proto_register(&l2tp_ip_prot, 1);
718         if (err != 0)
719                 goto out;
720
721         err = inet_add_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
722         if (err)
723                 goto out1;
724
725         inet_register_protosw(&l2tp_ip_protosw);
726         return 0;
727
728 out1:
729         proto_unregister(&l2tp_ip_prot);
730 out:
731         return err;
732 }
733
734 static void __exit l2tp_ip_exit(void)
735 {
736         inet_unregister_protosw(&l2tp_ip_protosw);
737         inet_del_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
738         proto_unregister(&l2tp_ip_prot);
739 }
740
741 module_init(l2tp_ip_init);
742 module_exit(l2tp_ip_exit);
743
744 MODULE_LICENSE("GPL");
745 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
746 MODULE_DESCRIPTION("L2TP over IP");
747 MODULE_VERSION("1.0");
748
749 /* Use the value of SOCK_DGRAM (2) directory, because __stringify doesn't like
750  * enums
751  */
752 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 2, IPPROTO_L2TP);