8173ee3c46e59d1f5d8eaa86594554b4b6375ada
[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 != NULL)
194                 sk = tunnel->sock;
195         else {
196                 struct iphdr *iph = (struct iphdr *) skb_network_header(skb);
197
198                 read_lock_bh(&l2tp_ip_lock);
199                 sk = __l2tp_ip_bind_lookup(&init_net, iph->daddr, 0, tunnel_id);
200                 if (!sk) {
201                         read_unlock_bh(&l2tp_ip_lock);
202                         goto discard;
203                 }
204
205                 sock_hold(sk);
206                 read_unlock_bh(&l2tp_ip_lock);
207         }
208
209         if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
210                 goto discard_put;
211
212         nf_reset(skb);
213
214         return sk_receive_skb(sk, skb, 1);
215
216 discard_sess:
217         if (session->deref)
218                 session->deref(session);
219         l2tp_session_dec_refcount(session);
220         goto discard;
221
222 discard_put:
223         sock_put(sk);
224
225 discard:
226         kfree_skb(skb);
227         return 0;
228 }
229
230 static int l2tp_ip_open(struct sock *sk)
231 {
232         /* Prevent autobind. We don't have ports. */
233         inet_sk(sk)->inet_num = IPPROTO_L2TP;
234
235         write_lock_bh(&l2tp_ip_lock);
236         sk_add_node(sk, &l2tp_ip_table);
237         write_unlock_bh(&l2tp_ip_lock);
238
239         return 0;
240 }
241
242 static void l2tp_ip_close(struct sock *sk, long timeout)
243 {
244         write_lock_bh(&l2tp_ip_lock);
245         hlist_del_init(&sk->sk_bind_node);
246         hlist_del_init(&sk->sk_node);
247         write_unlock_bh(&l2tp_ip_lock);
248         sk_common_release(sk);
249 }
250
251 static void l2tp_ip_destroy_sock(struct sock *sk)
252 {
253         struct sk_buff *skb;
254
255         while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL)
256                 kfree_skb(skb);
257
258         sk_refcnt_debug_dec(sk);
259 }
260
261 static int l2tp_ip_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
262 {
263         struct inet_sock *inet = inet_sk(sk);
264         struct sockaddr_l2tpip *addr = (struct sockaddr_l2tpip *) uaddr;
265         int ret;
266         int chk_addr_ret;
267
268         if (addr_len < sizeof(struct sockaddr_l2tpip))
269                 return -EINVAL;
270         if (addr->l2tp_family != AF_INET)
271                 return -EINVAL;
272
273         ret = -EADDRINUSE;
274         read_lock_bh(&l2tp_ip_lock);
275         if (__l2tp_ip_bind_lookup(&init_net, addr->l2tp_addr.s_addr, sk->sk_bound_dev_if, addr->l2tp_conn_id))
276                 goto out_in_use;
277
278         read_unlock_bh(&l2tp_ip_lock);
279
280         lock_sock(sk);
281         if (!sock_flag(sk, SOCK_ZAPPED))
282                 goto out;
283
284         if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_l2tpip))
285                 goto out;
286
287         chk_addr_ret = inet_addr_type(&init_net, addr->l2tp_addr.s_addr);
288         ret = -EADDRNOTAVAIL;
289         if (addr->l2tp_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
290             chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
291                 goto out;
292
293         inet->inet_rcv_saddr = inet->inet_saddr = addr->l2tp_addr.s_addr;
294         if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
295                 inet->inet_saddr = 0;  /* Use device */
296         sk_dst_reset(sk);
297
298         l2tp_ip_sk(sk)->conn_id = addr->l2tp_conn_id;
299
300         write_lock_bh(&l2tp_ip_lock);
301         sk_add_bind_node(sk, &l2tp_ip_bind_table);
302         sk_del_node_init(sk);
303         write_unlock_bh(&l2tp_ip_lock);
304         ret = 0;
305         sock_reset_flag(sk, SOCK_ZAPPED);
306
307 out:
308         release_sock(sk);
309
310         return ret;
311
312 out_in_use:
313         read_unlock_bh(&l2tp_ip_lock);
314
315         return ret;
316 }
317
318 static int l2tp_ip_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
319 {
320         struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *) uaddr;
321         struct inet_sock *inet = inet_sk(sk);
322         struct flowi4 *fl4;
323         struct rtable *rt;
324         __be32 saddr;
325         int oif, rc;
326
327         if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
328                 return -EINVAL;
329
330         if (addr_len < sizeof(*lsa))
331                 return -EINVAL;
332
333         if (lsa->l2tp_family != AF_INET)
334                 return -EAFNOSUPPORT;
335
336         lock_sock(sk);
337
338         sk_dst_reset(sk);
339
340         oif = sk->sk_bound_dev_if;
341         saddr = inet->inet_saddr;
342
343         rc = -EINVAL;
344         if (ipv4_is_multicast(lsa->l2tp_addr.s_addr))
345                 goto out;
346
347         fl4 = &inet->cork.fl.u.ip4;
348         rt = ip_route_connect(fl4, lsa->l2tp_addr.s_addr, saddr,
349                               RT_CONN_FLAGS(sk), oif,
350                               IPPROTO_L2TP,
351                               0, 0, sk, true);
352         if (IS_ERR(rt)) {
353                 rc = PTR_ERR(rt);
354                 if (rc == -ENETUNREACH)
355                         IP_INC_STATS_BH(&init_net, IPSTATS_MIB_OUTNOROUTES);
356                 goto out;
357         }
358
359         rc = -ENETUNREACH;
360         if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
361                 ip_rt_put(rt);
362                 goto out;
363         }
364
365         l2tp_ip_sk(sk)->peer_conn_id = lsa->l2tp_conn_id;
366
367         if (!inet->inet_saddr)
368                 inet->inet_saddr = fl4->saddr;
369         if (!inet->inet_rcv_saddr)
370                 inet->inet_rcv_saddr = fl4->saddr;
371         inet->inet_daddr = fl4->daddr;
372         sk->sk_state = TCP_ESTABLISHED;
373         inet->inet_id = jiffies;
374
375         sk_dst_set(sk, &rt->dst);
376
377         write_lock_bh(&l2tp_ip_lock);
378         hlist_del_init(&sk->sk_bind_node);
379         sk_add_bind_node(sk, &l2tp_ip_bind_table);
380         write_unlock_bh(&l2tp_ip_lock);
381
382         rc = 0;
383 out:
384         release_sock(sk);
385         return rc;
386 }
387
388 static int l2tp_ip_disconnect(struct sock *sk, int flags)
389 {
390         if (sock_flag(sk, SOCK_ZAPPED))
391                 return 0;
392
393         return udp_disconnect(sk, flags);
394 }
395
396 static int l2tp_ip_getname(struct socket *sock, struct sockaddr *uaddr,
397                            int *uaddr_len, int peer)
398 {
399         struct sock *sk         = sock->sk;
400         struct inet_sock *inet  = inet_sk(sk);
401         struct l2tp_ip_sock *lsk = l2tp_ip_sk(sk);
402         struct sockaddr_l2tpip *lsa = (struct sockaddr_l2tpip *)uaddr;
403
404         memset(lsa, 0, sizeof(*lsa));
405         lsa->l2tp_family = AF_INET;
406         if (peer) {
407                 if (!inet->inet_dport)
408                         return -ENOTCONN;
409                 lsa->l2tp_conn_id = lsk->peer_conn_id;
410                 lsa->l2tp_addr.s_addr = inet->inet_daddr;
411         } else {
412                 __be32 addr = inet->inet_rcv_saddr;
413                 if (!addr)
414                         addr = inet->inet_saddr;
415                 lsa->l2tp_conn_id = lsk->conn_id;
416                 lsa->l2tp_addr.s_addr = addr;
417         }
418         *uaddr_len = sizeof(*lsa);
419         return 0;
420 }
421
422 static int l2tp_ip_backlog_recv(struct sock *sk, struct sk_buff *skb)
423 {
424         int rc;
425
426         /* Charge it to the socket, dropping if the queue is full. */
427         rc = sock_queue_rcv_skb(sk, skb);
428         if (rc < 0)
429                 goto drop;
430
431         return 0;
432
433 drop:
434         IP_INC_STATS(&init_net, IPSTATS_MIB_INDISCARDS);
435         kfree_skb(skb);
436         return 0;
437 }
438
439 /* Userspace will call sendmsg() on the tunnel socket to send L2TP
440  * control frames.
441  */
442 static int l2tp_ip_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, size_t len)
443 {
444         struct sk_buff *skb;
445         int rc;
446         struct l2tp_ip_sock *lsa = l2tp_ip_sk(sk);
447         struct inet_sock *inet = inet_sk(sk);
448         struct rtable *rt = NULL;
449         struct flowi4 *fl4;
450         int connected = 0;
451         __be32 daddr;
452
453         lock_sock(sk);
454
455         rc = -ENOTCONN;
456         if (sock_flag(sk, SOCK_DEAD))
457                 goto out;
458
459         /* Get and verify the address. */
460         if (msg->msg_name) {
461                 struct sockaddr_l2tpip *lip = (struct sockaddr_l2tpip *) msg->msg_name;
462                 rc = -EINVAL;
463                 if (msg->msg_namelen < sizeof(*lip))
464                         goto out;
465
466                 if (lip->l2tp_family != AF_INET) {
467                         rc = -EAFNOSUPPORT;
468                         if (lip->l2tp_family != AF_UNSPEC)
469                                 goto out;
470                 }
471
472                 daddr = lip->l2tp_addr.s_addr;
473         } else {
474                 rc = -EDESTADDRREQ;
475                 if (sk->sk_state != TCP_ESTABLISHED)
476                         goto out;
477
478                 daddr = inet->inet_daddr;
479                 connected = 1;
480         }
481
482         /* Allocate a socket buffer */
483         rc = -ENOMEM;
484         skb = sock_wmalloc(sk, 2 + NET_SKB_PAD + sizeof(struct iphdr) +
485                            4 + len, 0, GFP_KERNEL);
486         if (!skb)
487                 goto error;
488
489         /* Reserve space for headers, putting IP header on 4-byte boundary. */
490         skb_reserve(skb, 2 + NET_SKB_PAD);
491         skb_reset_network_header(skb);
492         skb_reserve(skb, sizeof(struct iphdr));
493         skb_reset_transport_header(skb);
494
495         /* Insert 0 session_id */
496         *((__be32 *) skb_put(skb, 4)) = 0;
497
498         /* Copy user data into skb */
499         rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
500         if (rc < 0) {
501                 kfree_skb(skb);
502                 goto error;
503         }
504
505         fl4 = &inet->cork.fl.u.ip4;
506         if (connected)
507                 rt = (struct rtable *) __sk_dst_check(sk, 0);
508
509         rcu_read_lock();
510         if (rt == NULL) {
511                 const struct ip_options_rcu *inet_opt;
512
513                 inet_opt = rcu_dereference(inet->inet_opt);
514
515                 /* Use correct destination address if we have options. */
516                 if (inet_opt && inet_opt->opt.srr)
517                         daddr = inet_opt->opt.faddr;
518
519                 /* If this fails, retransmit mechanism of transport layer will
520                  * keep trying until route appears or the connection times
521                  * itself out.
522                  */
523                 rt = ip_route_output_ports(sock_net(sk), fl4, sk,
524                                            daddr, inet->inet_saddr,
525                                            inet->inet_dport, inet->inet_sport,
526                                            sk->sk_protocol, RT_CONN_FLAGS(sk),
527                                            sk->sk_bound_dev_if);
528                 if (IS_ERR(rt))
529                         goto no_route;
530                 if (connected) {
531                         sk_setup_caps(sk, &rt->dst);
532                 } else {
533                         skb_dst_set(skb, &rt->dst);
534                         goto xmit;
535                 }
536         }
537
538         /* We dont need to clone dst here, it is guaranteed to not disappear.
539          *  __dev_xmit_skb() might force a refcount if needed.
540          */
541         skb_dst_set_noref(skb, &rt->dst);
542
543 xmit:
544         /* Queue the packet to IP for output */
545         rc = ip_queue_xmit(skb, &inet->cork.fl);
546         rcu_read_unlock();
547
548 error:
549         /* Update stats */
550         if (rc >= 0) {
551                 lsa->tx_packets++;
552                 lsa->tx_bytes += len;
553                 rc = len;
554         } else {
555                 lsa->tx_errors++;
556         }
557
558 out:
559         release_sock(sk);
560         return rc;
561
562 no_route:
563         rcu_read_unlock();
564         IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
565         kfree_skb(skb);
566         rc = -EHOSTUNREACH;
567         goto out;
568 }
569
570 static int l2tp_ip_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
571                            size_t len, int noblock, int flags, int *addr_len)
572 {
573         struct inet_sock *inet = inet_sk(sk);
574         struct l2tp_ip_sock *lsk = l2tp_ip_sk(sk);
575         size_t copied = 0;
576         int err = -EOPNOTSUPP;
577         struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
578         struct sk_buff *skb;
579
580         if (flags & MSG_OOB)
581                 goto out;
582
583         skb = skb_recv_datagram(sk, flags, noblock, &err);
584         if (!skb)
585                 goto out;
586
587         copied = skb->len;
588         if (len < copied) {
589                 msg->msg_flags |= MSG_TRUNC;
590                 copied = len;
591         }
592
593         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
594         if (err)
595                 goto done;
596
597         sock_recv_timestamp(msg, sk, skb);
598
599         /* Copy the address. */
600         if (sin) {
601                 sin->sin_family = AF_INET;
602                 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
603                 sin->sin_port = 0;
604                 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
605                 *addr_len = sizeof(*sin);
606         }
607         if (inet->cmsg_flags)
608                 ip_cmsg_recv(msg, skb);
609         if (flags & MSG_TRUNC)
610                 copied = skb->len;
611 done:
612         skb_free_datagram(sk, skb);
613 out:
614         if (err) {
615                 lsk->rx_errors++;
616                 return err;
617         }
618
619         lsk->rx_packets++;
620         lsk->rx_bytes += copied;
621
622         return copied;
623 }
624
625 int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg)
626 {
627         struct sk_buff *skb;
628         int amount;
629
630         switch (cmd) {
631         case SIOCOUTQ:
632                 amount = sk_wmem_alloc_get(sk);
633                 break;
634         case SIOCINQ:
635                 spin_lock_bh(&sk->sk_receive_queue.lock);
636                 skb = skb_peek(&sk->sk_receive_queue);
637                 amount = skb ? skb->len : 0;
638                 spin_unlock_bh(&sk->sk_receive_queue.lock);
639                 break;
640
641         default:
642                 return -ENOIOCTLCMD;
643         }
644
645         return put_user(amount, (int __user *)arg);
646 }
647 EXPORT_SYMBOL(l2tp_ioctl);
648
649 static struct proto l2tp_ip_prot = {
650         .name              = "L2TP/IP",
651         .owner             = THIS_MODULE,
652         .init              = l2tp_ip_open,
653         .close             = l2tp_ip_close,
654         .bind              = l2tp_ip_bind,
655         .connect           = l2tp_ip_connect,
656         .disconnect        = l2tp_ip_disconnect,
657         .ioctl             = l2tp_ioctl,
658         .destroy           = l2tp_ip_destroy_sock,
659         .setsockopt        = ip_setsockopt,
660         .getsockopt        = ip_getsockopt,
661         .sendmsg           = l2tp_ip_sendmsg,
662         .recvmsg           = l2tp_ip_recvmsg,
663         .backlog_rcv       = l2tp_ip_backlog_recv,
664         .hash              = inet_hash,
665         .unhash            = inet_unhash,
666         .obj_size          = sizeof(struct l2tp_ip_sock),
667 #ifdef CONFIG_COMPAT
668         .compat_setsockopt = compat_ip_setsockopt,
669         .compat_getsockopt = compat_ip_getsockopt,
670 #endif
671 };
672
673 static const struct proto_ops l2tp_ip_ops = {
674         .family            = PF_INET,
675         .owner             = THIS_MODULE,
676         .release           = inet_release,
677         .bind              = inet_bind,
678         .connect           = inet_dgram_connect,
679         .socketpair        = sock_no_socketpair,
680         .accept            = sock_no_accept,
681         .getname           = l2tp_ip_getname,
682         .poll              = datagram_poll,
683         .ioctl             = inet_ioctl,
684         .listen            = sock_no_listen,
685         .shutdown          = inet_shutdown,
686         .setsockopt        = sock_common_setsockopt,
687         .getsockopt        = sock_common_getsockopt,
688         .sendmsg           = inet_sendmsg,
689         .recvmsg           = sock_common_recvmsg,
690         .mmap              = sock_no_mmap,
691         .sendpage          = sock_no_sendpage,
692 #ifdef CONFIG_COMPAT
693         .compat_setsockopt = compat_sock_common_setsockopt,
694         .compat_getsockopt = compat_sock_common_getsockopt,
695 #endif
696 };
697
698 static struct inet_protosw l2tp_ip_protosw = {
699         .type           = SOCK_DGRAM,
700         .protocol       = IPPROTO_L2TP,
701         .prot           = &l2tp_ip_prot,
702         .ops            = &l2tp_ip_ops,
703         .no_check       = 0,
704 };
705
706 static struct net_protocol l2tp_ip_protocol __read_mostly = {
707         .handler        = l2tp_ip_recv,
708 };
709
710 static int __init l2tp_ip_init(void)
711 {
712         int err;
713
714         printk(KERN_INFO "L2TP IP encapsulation support (L2TPv3)\n");
715
716         err = proto_register(&l2tp_ip_prot, 1);
717         if (err != 0)
718                 goto out;
719
720         err = inet_add_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
721         if (err)
722                 goto out1;
723
724         inet_register_protosw(&l2tp_ip_protosw);
725         return 0;
726
727 out1:
728         proto_unregister(&l2tp_ip_prot);
729 out:
730         return err;
731 }
732
733 static void __exit l2tp_ip_exit(void)
734 {
735         inet_unregister_protosw(&l2tp_ip_protosw);
736         inet_del_protocol(&l2tp_ip_protocol, IPPROTO_L2TP);
737         proto_unregister(&l2tp_ip_prot);
738 }
739
740 module_init(l2tp_ip_init);
741 module_exit(l2tp_ip_exit);
742
743 MODULE_LICENSE("GPL");
744 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
745 MODULE_DESCRIPTION("L2TP over IP");
746 MODULE_VERSION("1.0");
747
748 /* Use the value of SOCK_DGRAM (2) directory, because __stringify doesn't like
749  * enums
750  */
751 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 2, IPPROTO_L2TP);