98dfcce1a5fcf62dab464ffbede408fe366ac24d
[linux-drm-fsl-dcu.git] / net / l2tp / l2tp_core.c
1 /*
2  * L2TP core.
3  *
4  * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
5  *
6  * This file contains some code of the original L2TPv2 pppol2tp
7  * driver, which has the following copyright:
8  *
9  * Authors:     Martijn van Oosterhout <kleptog@svana.org>
10  *              James Chapman (jchapman@katalix.com)
11  * Contributors:
12  *              Michal Ostrowski <mostrows@speakeasy.net>
13  *              Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
14  *              David S. Miller (davem@redhat.com)
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License version 2 as
18  * published by the Free Software Foundation.
19  */
20
21 #include <linux/module.h>
22 #include <linux/string.h>
23 #include <linux/list.h>
24 #include <linux/rculist.h>
25 #include <linux/uaccess.h>
26
27 #include <linux/kernel.h>
28 #include <linux/spinlock.h>
29 #include <linux/kthread.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32 #include <linux/errno.h>
33 #include <linux/jiffies.h>
34
35 #include <linux/netdevice.h>
36 #include <linux/net.h>
37 #include <linux/inetdevice.h>
38 #include <linux/skbuff.h>
39 #include <linux/init.h>
40 #include <linux/in.h>
41 #include <linux/ip.h>
42 #include <linux/udp.h>
43 #include <linux/l2tp.h>
44 #include <linux/hash.h>
45 #include <linux/sort.h>
46 #include <linux/file.h>
47 #include <linux/nsproxy.h>
48 #include <net/net_namespace.h>
49 #include <net/netns/generic.h>
50 #include <net/dst.h>
51 #include <net/ip.h>
52 #include <net/udp.h>
53 #include <net/inet_common.h>
54 #include <net/xfrm.h>
55 #include <net/protocol.h>
56
57 #include <asm/byteorder.h>
58 #include <asm/atomic.h>
59
60 #include "l2tp_core.h"
61
62 #define L2TP_DRV_VERSION        "V2.0"
63
64 /* L2TP header constants */
65 #define L2TP_HDRFLAG_T     0x8000
66 #define L2TP_HDRFLAG_L     0x4000
67 #define L2TP_HDRFLAG_S     0x0800
68 #define L2TP_HDRFLAG_O     0x0200
69 #define L2TP_HDRFLAG_P     0x0100
70
71 #define L2TP_HDR_VER_MASK  0x000F
72 #define L2TP_HDR_VER_2     0x0002
73 #define L2TP_HDR_VER_3     0x0003
74
75 /* L2TPv3 default L2-specific sublayer */
76 #define L2TP_SLFLAG_S      0x40000000
77 #define L2TP_SL_SEQ_MASK   0x00ffffff
78
79 #define L2TP_HDR_SIZE_SEQ               10
80 #define L2TP_HDR_SIZE_NOSEQ             6
81
82 /* Default trace flags */
83 #define L2TP_DEFAULT_DEBUG_FLAGS        0
84
85 #define PRINTK(_mask, _type, _lvl, _fmt, args...)                       \
86         do {                                                            \
87                 if ((_mask) & (_type))                                  \
88                         printk(_lvl "L2TP: " _fmt, ##args);             \
89         } while (0)
90
91 /* Private data stored for received packets in the skb.
92  */
93 struct l2tp_skb_cb {
94         u32                     ns;
95         u16                     has_seq;
96         u16                     length;
97         unsigned long           expires;
98 };
99
100 #define L2TP_SKB_CB(skb)        ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
101
102 static atomic_t l2tp_tunnel_count;
103 static atomic_t l2tp_session_count;
104
105 /* per-net private data for this module */
106 static unsigned int l2tp_net_id;
107 struct l2tp_net {
108         struct list_head l2tp_tunnel_list;
109         spinlock_t l2tp_tunnel_list_lock;
110         struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
111         spinlock_t l2tp_session_hlist_lock;
112 };
113
114 static inline struct l2tp_net *l2tp_pernet(struct net *net)
115 {
116         BUG_ON(!net);
117
118         return net_generic(net, l2tp_net_id);
119 }
120
121 /* Session hash global list for L2TPv3.
122  * The session_id SHOULD be random according to RFC3931, but several
123  * L2TP implementations use incrementing session_ids.  So we do a real
124  * hash on the session_id, rather than a simple bitmask.
125  */
126 static inline struct hlist_head *
127 l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
128 {
129         return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
130
131 }
132
133 /* Lookup a session by id in the global session list
134  */
135 static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
136 {
137         struct l2tp_net *pn = l2tp_pernet(net);
138         struct hlist_head *session_list =
139                 l2tp_session_id_hash_2(pn, session_id);
140         struct l2tp_session *session;
141         struct hlist_node *walk;
142
143         rcu_read_lock_bh();
144         hlist_for_each_entry_rcu(session, walk, session_list, global_hlist) {
145                 if (session->session_id == session_id) {
146                         rcu_read_unlock_bh();
147                         return session;
148                 }
149         }
150         rcu_read_unlock_bh();
151
152         return NULL;
153 }
154
155 /* Session hash list.
156  * The session_id SHOULD be random according to RFC2661, but several
157  * L2TP implementations (Cisco and Microsoft) use incrementing
158  * session_ids.  So we do a real hash on the session_id, rather than a
159  * simple bitmask.
160  */
161 static inline struct hlist_head *
162 l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
163 {
164         return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
165 }
166
167 /* Lookup a session by id
168  */
169 struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
170 {
171         struct hlist_head *session_list;
172         struct l2tp_session *session;
173         struct hlist_node *walk;
174
175         /* In L2TPv3, session_ids are unique over all tunnels and we
176          * sometimes need to look them up before we know the
177          * tunnel.
178          */
179         if (tunnel == NULL)
180                 return l2tp_session_find_2(net, session_id);
181
182         session_list = l2tp_session_id_hash(tunnel, session_id);
183         read_lock_bh(&tunnel->hlist_lock);
184         hlist_for_each_entry(session, walk, session_list, hlist) {
185                 if (session->session_id == session_id) {
186                         read_unlock_bh(&tunnel->hlist_lock);
187                         return session;
188                 }
189         }
190         read_unlock_bh(&tunnel->hlist_lock);
191
192         return NULL;
193 }
194 EXPORT_SYMBOL_GPL(l2tp_session_find);
195
196 struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
197 {
198         int hash;
199         struct hlist_node *walk;
200         struct l2tp_session *session;
201         int count = 0;
202
203         read_lock_bh(&tunnel->hlist_lock);
204         for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
205                 hlist_for_each_entry(session, walk, &tunnel->session_hlist[hash], hlist) {
206                         if (++count > nth) {
207                                 read_unlock_bh(&tunnel->hlist_lock);
208                                 return session;
209                         }
210                 }
211         }
212
213         read_unlock_bh(&tunnel->hlist_lock);
214
215         return NULL;
216 }
217 EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
218
219 /* Lookup a session by interface name.
220  * This is very inefficient but is only used by management interfaces.
221  */
222 struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
223 {
224         struct l2tp_net *pn = l2tp_pernet(net);
225         int hash;
226         struct hlist_node *walk;
227         struct l2tp_session *session;
228
229         rcu_read_lock_bh();
230         for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
231                 hlist_for_each_entry_rcu(session, walk, &pn->l2tp_session_hlist[hash], global_hlist) {
232                         if (!strcmp(session->ifname, ifname)) {
233                                 rcu_read_unlock_bh();
234                                 return session;
235                         }
236                 }
237         }
238
239         rcu_read_unlock_bh();
240
241         return NULL;
242 }
243 EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
244
245 /* Lookup a tunnel by id
246  */
247 struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
248 {
249         struct l2tp_tunnel *tunnel;
250         struct l2tp_net *pn = l2tp_pernet(net);
251
252         rcu_read_lock_bh();
253         list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
254                 if (tunnel->tunnel_id == tunnel_id) {
255                         rcu_read_unlock_bh();
256                         return tunnel;
257                 }
258         }
259         rcu_read_unlock_bh();
260
261         return NULL;
262 }
263 EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
264
265 struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
266 {
267         struct l2tp_net *pn = l2tp_pernet(net);
268         struct l2tp_tunnel *tunnel;
269         int count = 0;
270
271         rcu_read_lock_bh();
272         list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
273                 if (++count > nth) {
274                         rcu_read_unlock_bh();
275                         return tunnel;
276                 }
277         }
278
279         rcu_read_unlock_bh();
280
281         return NULL;
282 }
283 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
284
285 /*****************************************************************************
286  * Receive data handling
287  *****************************************************************************/
288
289 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
290  * number.
291  */
292 static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
293 {
294         struct sk_buff *skbp;
295         struct sk_buff *tmp;
296         u32 ns = L2TP_SKB_CB(skb)->ns;
297
298         spin_lock_bh(&session->reorder_q.lock);
299         skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
300                 if (L2TP_SKB_CB(skbp)->ns > ns) {
301                         __skb_queue_before(&session->reorder_q, skbp, skb);
302                         PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
303                                "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
304                                session->name, ns, L2TP_SKB_CB(skbp)->ns,
305                                skb_queue_len(&session->reorder_q));
306                         session->stats.rx_oos_packets++;
307                         goto out;
308                 }
309         }
310
311         __skb_queue_tail(&session->reorder_q, skb);
312
313 out:
314         spin_unlock_bh(&session->reorder_q.lock);
315 }
316
317 /* Dequeue a single skb.
318  */
319 static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
320 {
321         struct l2tp_tunnel *tunnel = session->tunnel;
322         int length = L2TP_SKB_CB(skb)->length;
323
324         /* We're about to requeue the skb, so return resources
325          * to its current owner (a socket receive buffer).
326          */
327         skb_orphan(skb);
328
329         tunnel->stats.rx_packets++;
330         tunnel->stats.rx_bytes += length;
331         session->stats.rx_packets++;
332         session->stats.rx_bytes += length;
333
334         if (L2TP_SKB_CB(skb)->has_seq) {
335                 /* Bump our Nr */
336                 session->nr++;
337                 if (tunnel->version == L2TP_HDR_VER_2)
338                         session->nr &= 0xffff;
339                 else
340                         session->nr &= 0xffffff;
341
342                 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
343                        "%s: updated nr to %hu\n", session->name, session->nr);
344         }
345
346         /* call private receive handler */
347         if (session->recv_skb != NULL)
348                 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
349         else
350                 kfree_skb(skb);
351
352         if (session->deref)
353                 (*session->deref)(session);
354 }
355
356 /* Dequeue skbs from the session's reorder_q, subject to packet order.
357  * Skbs that have been in the queue for too long are simply discarded.
358  */
359 static void l2tp_recv_dequeue(struct l2tp_session *session)
360 {
361         struct sk_buff *skb;
362         struct sk_buff *tmp;
363
364         /* If the pkt at the head of the queue has the nr that we
365          * expect to send up next, dequeue it and any other
366          * in-sequence packets behind it.
367          */
368         spin_lock_bh(&session->reorder_q.lock);
369         skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
370                 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
371                         session->stats.rx_seq_discards++;
372                         session->stats.rx_errors++;
373                         PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
374                                "%s: oos pkt %u len %d discarded (too old), "
375                                "waiting for %u, reorder_q_len=%d\n",
376                                session->name, L2TP_SKB_CB(skb)->ns,
377                                L2TP_SKB_CB(skb)->length, session->nr,
378                                skb_queue_len(&session->reorder_q));
379                         __skb_unlink(skb, &session->reorder_q);
380                         kfree_skb(skb);
381                         if (session->deref)
382                                 (*session->deref)(session);
383                         continue;
384                 }
385
386                 if (L2TP_SKB_CB(skb)->has_seq) {
387                         if (L2TP_SKB_CB(skb)->ns != session->nr) {
388                                 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
389                                        "%s: holding oos pkt %u len %d, "
390                                        "waiting for %u, reorder_q_len=%d\n",
391                                        session->name, L2TP_SKB_CB(skb)->ns,
392                                        L2TP_SKB_CB(skb)->length, session->nr,
393                                        skb_queue_len(&session->reorder_q));
394                                 goto out;
395                         }
396                 }
397                 __skb_unlink(skb, &session->reorder_q);
398
399                 /* Process the skb. We release the queue lock while we
400                  * do so to let other contexts process the queue.
401                  */
402                 spin_unlock_bh(&session->reorder_q.lock);
403                 l2tp_recv_dequeue_skb(session, skb);
404                 spin_lock_bh(&session->reorder_q.lock);
405         }
406
407 out:
408         spin_unlock_bh(&session->reorder_q.lock);
409 }
410
411 static inline int l2tp_verify_udp_checksum(struct sock *sk,
412                                            struct sk_buff *skb)
413 {
414         struct udphdr *uh = udp_hdr(skb);
415         u16 ulen = ntohs(uh->len);
416         struct inet_sock *inet;
417         __wsum psum;
418
419         if (sk->sk_no_check || skb_csum_unnecessary(skb) || !uh->check)
420                 return 0;
421
422         inet = inet_sk(sk);
423         psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr, ulen,
424                                   IPPROTO_UDP, 0);
425
426         if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
427             !csum_fold(csum_add(psum, skb->csum)))
428                 return 0;
429
430         skb->csum = psum;
431
432         return __skb_checksum_complete(skb);
433 }
434
435 /* Do receive processing of L2TP data frames. We handle both L2TPv2
436  * and L2TPv3 data frames here.
437  *
438  * L2TPv2 Data Message Header
439  *
440  *  0                   1                   2                   3
441  *  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
442  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
443  * |T|L|x|x|S|x|O|P|x|x|x|x|  Ver  |          Length (opt)         |
444  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
445  * |           Tunnel ID           |           Session ID          |
446  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
447  * |             Ns (opt)          |             Nr (opt)          |
448  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
449  * |      Offset Size (opt)        |    Offset pad... (opt)
450  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
451  *
452  * Data frames are marked by T=0. All other fields are the same as
453  * those in L2TP control frames.
454  *
455  * L2TPv3 Data Message Header
456  *
457  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
458  * |                      L2TP Session Header                      |
459  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
460  * |                      L2-Specific Sublayer                     |
461  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
462  * |                        Tunnel Payload                      ...
463  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
464  *
465  * L2TPv3 Session Header Over IP
466  *
467  *  0                   1                   2                   3
468  *  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
469  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
470  * |                           Session ID                          |
471  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
472  * |               Cookie (optional, maximum 64 bits)...
473  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
474  *                                                                 |
475  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
476  *
477  * L2TPv3 L2-Specific Sublayer Format
478  *
479  *  0                   1                   2                   3
480  *  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
481  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
482  * |x|S|x|x|x|x|x|x|              Sequence Number                  |
483  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
484  *
485  * Cookie value, sublayer format and offset (pad) are negotiated with
486  * the peer when the session is set up. Unlike L2TPv2, we do not need
487  * to parse the packet header to determine if optional fields are
488  * present.
489  *
490  * Caller must already have parsed the frame and determined that it is
491  * a data (not control) frame before coming here. Fields up to the
492  * session-id have already been parsed and ptr points to the data
493  * after the session-id.
494  */
495 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
496                       unsigned char *ptr, unsigned char *optr, u16 hdrflags,
497                       int length, int (*payload_hook)(struct sk_buff *skb))
498 {
499         struct l2tp_tunnel *tunnel = session->tunnel;
500         int offset;
501         u32 ns, nr;
502
503         /* The ref count is increased since we now hold a pointer to
504          * the session. Take care to decrement the refcnt when exiting
505          * this function from now on...
506          */
507         l2tp_session_inc_refcount(session);
508         if (session->ref)
509                 (*session->ref)(session);
510
511         /* Parse and check optional cookie */
512         if (session->peer_cookie_len > 0) {
513                 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
514                         PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
515                                "%s: cookie mismatch (%u/%u). Discarding.\n",
516                                tunnel->name, tunnel->tunnel_id, session->session_id);
517                         session->stats.rx_cookie_discards++;
518                         goto discard;
519                 }
520                 ptr += session->peer_cookie_len;
521         }
522
523         /* Handle the optional sequence numbers. Sequence numbers are
524          * in different places for L2TPv2 and L2TPv3.
525          *
526          * If we are the LAC, enable/disable sequence numbers under
527          * the control of the LNS.  If no sequence numbers present but
528          * we were expecting them, discard frame.
529          */
530         ns = nr = 0;
531         L2TP_SKB_CB(skb)->has_seq = 0;
532         if (tunnel->version == L2TP_HDR_VER_2) {
533                 if (hdrflags & L2TP_HDRFLAG_S) {
534                         ns = ntohs(*(__be16 *) ptr);
535                         ptr += 2;
536                         nr = ntohs(*(__be16 *) ptr);
537                         ptr += 2;
538
539                         /* Store L2TP info in the skb */
540                         L2TP_SKB_CB(skb)->ns = ns;
541                         L2TP_SKB_CB(skb)->has_seq = 1;
542
543                         PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
544                                "%s: recv data ns=%u, nr=%u, session nr=%u\n",
545                                session->name, ns, nr, session->nr);
546                 }
547         } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
548                 u32 l2h = ntohl(*(__be32 *) ptr);
549
550                 if (l2h & 0x40000000) {
551                         ns = l2h & 0x00ffffff;
552
553                         /* Store L2TP info in the skb */
554                         L2TP_SKB_CB(skb)->ns = ns;
555                         L2TP_SKB_CB(skb)->has_seq = 1;
556
557                         PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
558                                "%s: recv data ns=%u, session nr=%u\n",
559                                session->name, ns, session->nr);
560                 }
561         }
562
563         /* Advance past L2-specific header, if present */
564         ptr += session->l2specific_len;
565
566         if (L2TP_SKB_CB(skb)->has_seq) {
567                 /* Received a packet with sequence numbers. If we're the LNS,
568                  * check if we sre sending sequence numbers and if not,
569                  * configure it so.
570                  */
571                 if ((!session->lns_mode) && (!session->send_seq)) {
572                         PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
573                                "%s: requested to enable seq numbers by LNS\n",
574                                session->name);
575                         session->send_seq = -1;
576                         l2tp_session_set_header_len(session, tunnel->version);
577                 }
578         } else {
579                 /* No sequence numbers.
580                  * If user has configured mandatory sequence numbers, discard.
581                  */
582                 if (session->recv_seq) {
583                         PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
584                                "%s: recv data has no seq numbers when required. "
585                                "Discarding\n", session->name);
586                         session->stats.rx_seq_discards++;
587                         goto discard;
588                 }
589
590                 /* If we're the LAC and we're sending sequence numbers, the
591                  * LNS has requested that we no longer send sequence numbers.
592                  * If we're the LNS and we're sending sequence numbers, the
593                  * LAC is broken. Discard the frame.
594                  */
595                 if ((!session->lns_mode) && (session->send_seq)) {
596                         PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
597                                "%s: requested to disable seq numbers by LNS\n",
598                                session->name);
599                         session->send_seq = 0;
600                         l2tp_session_set_header_len(session, tunnel->version);
601                 } else if (session->send_seq) {
602                         PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
603                                "%s: recv data has no seq numbers when required. "
604                                "Discarding\n", session->name);
605                         session->stats.rx_seq_discards++;
606                         goto discard;
607                 }
608         }
609
610         /* Session data offset is handled differently for L2TPv2 and
611          * L2TPv3. For L2TPv2, there is an optional 16-bit value in
612          * the header. For L2TPv3, the offset is negotiated using AVPs
613          * in the session setup control protocol.
614          */
615         if (tunnel->version == L2TP_HDR_VER_2) {
616                 /* If offset bit set, skip it. */
617                 if (hdrflags & L2TP_HDRFLAG_O) {
618                         offset = ntohs(*(__be16 *)ptr);
619                         ptr += 2 + offset;
620                 }
621         } else
622                 ptr += session->offset;
623
624         offset = ptr - optr;
625         if (!pskb_may_pull(skb, offset))
626                 goto discard;
627
628         __skb_pull(skb, offset);
629
630         /* If caller wants to process the payload before we queue the
631          * packet, do so now.
632          */
633         if (payload_hook)
634                 if ((*payload_hook)(skb))
635                         goto discard;
636
637         /* Prepare skb for adding to the session's reorder_q.  Hold
638          * packets for max reorder_timeout or 1 second if not
639          * reordering.
640          */
641         L2TP_SKB_CB(skb)->length = length;
642         L2TP_SKB_CB(skb)->expires = jiffies +
643                 (session->reorder_timeout ? session->reorder_timeout : HZ);
644
645         /* Add packet to the session's receive queue. Reordering is done here, if
646          * enabled. Saved L2TP protocol info is stored in skb->sb[].
647          */
648         if (L2TP_SKB_CB(skb)->has_seq) {
649                 if (session->reorder_timeout != 0) {
650                         /* Packet reordering enabled. Add skb to session's
651                          * reorder queue, in order of ns.
652                          */
653                         l2tp_recv_queue_skb(session, skb);
654                 } else {
655                         /* Packet reordering disabled. Discard out-of-sequence
656                          * packets
657                          */
658                         if (L2TP_SKB_CB(skb)->ns != session->nr) {
659                                 session->stats.rx_seq_discards++;
660                                 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
661                                        "%s: oos pkt %u len %d discarded, "
662                                        "waiting for %u, reorder_q_len=%d\n",
663                                        session->name, L2TP_SKB_CB(skb)->ns,
664                                        L2TP_SKB_CB(skb)->length, session->nr,
665                                        skb_queue_len(&session->reorder_q));
666                                 goto discard;
667                         }
668                         skb_queue_tail(&session->reorder_q, skb);
669                 }
670         } else {
671                 /* No sequence numbers. Add the skb to the tail of the
672                  * reorder queue. This ensures that it will be
673                  * delivered after all previous sequenced skbs.
674                  */
675                 skb_queue_tail(&session->reorder_q, skb);
676         }
677
678         /* Try to dequeue as many skbs from reorder_q as we can. */
679         l2tp_recv_dequeue(session);
680
681         l2tp_session_dec_refcount(session);
682
683         return;
684
685 discard:
686         session->stats.rx_errors++;
687         kfree_skb(skb);
688
689         if (session->deref)
690                 (*session->deref)(session);
691
692         l2tp_session_dec_refcount(session);
693 }
694 EXPORT_SYMBOL(l2tp_recv_common);
695
696 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
697  * here. The skb is not on a list when we get here.
698  * Returns 0 if the packet was a data packet and was successfully passed on.
699  * Returns 1 if the packet was not a good data packet and could not be
700  * forwarded.  All such packets are passed up to userspace to deal with.
701  */
702 int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
703                        int (*payload_hook)(struct sk_buff *skb))
704 {
705         struct l2tp_session *session = NULL;
706         unsigned char *ptr, *optr;
707         u16 hdrflags;
708         u32 tunnel_id, session_id;
709         int offset;
710         u16 version;
711         int length;
712
713         if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
714                 goto discard_bad_csum;
715
716         /* UDP always verifies the packet length. */
717         __skb_pull(skb, sizeof(struct udphdr));
718
719         /* Short packet? */
720         if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
721                 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
722                        "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
723                 goto error;
724         }
725
726         /* Point to L2TP header */
727         optr = ptr = skb->data;
728
729         /* Trace packet contents, if enabled */
730         if (tunnel->debug & L2TP_MSG_DATA) {
731                 length = min(32u, skb->len);
732                 if (!pskb_may_pull(skb, length))
733                         goto error;
734
735                 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
736
737                 offset = 0;
738                 do {
739                         printk(" %02X", ptr[offset]);
740                 } while (++offset < length);
741
742                 printk("\n");
743         }
744
745         /* Get L2TP header flags */
746         hdrflags = ntohs(*(__be16 *) ptr);
747
748         /* Check protocol version */
749         version = hdrflags & L2TP_HDR_VER_MASK;
750         if (version != tunnel->version) {
751                 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
752                        "%s: recv protocol version mismatch: got %d expected %d\n",
753                        tunnel->name, version, tunnel->version);
754                 goto error;
755         }
756
757         /* Get length of L2TP packet */
758         length = skb->len;
759
760         /* If type is control packet, it is handled by userspace. */
761         if (hdrflags & L2TP_HDRFLAG_T) {
762                 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
763                        "%s: recv control packet, len=%d\n", tunnel->name, length);
764                 goto error;
765         }
766
767         /* Skip flags */
768         ptr += 2;
769
770         if (tunnel->version == L2TP_HDR_VER_2) {
771                 /* If length is present, skip it */
772                 if (hdrflags & L2TP_HDRFLAG_L)
773                         ptr += 2;
774
775                 /* Extract tunnel and session ID */
776                 tunnel_id = ntohs(*(__be16 *) ptr);
777                 ptr += 2;
778                 session_id = ntohs(*(__be16 *) ptr);
779                 ptr += 2;
780         } else {
781                 ptr += 2;       /* skip reserved bits */
782                 tunnel_id = tunnel->tunnel_id;
783                 session_id = ntohl(*(__be32 *) ptr);
784                 ptr += 4;
785         }
786
787         /* Find the session context */
788         session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
789         if (!session || !session->recv_skb) {
790                 /* Not found? Pass to userspace to deal with */
791                 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
792                        "%s: no session found (%u/%u). Passing up.\n",
793                        tunnel->name, tunnel_id, session_id);
794                 goto error;
795         }
796
797         l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
798
799         return 0;
800
801 discard_bad_csum:
802         LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
803         UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
804         tunnel->stats.rx_errors++;
805         kfree_skb(skb);
806
807         return 0;
808
809 error:
810         /* Put UDP header back */
811         __skb_push(skb, sizeof(struct udphdr));
812
813         return 1;
814 }
815 EXPORT_SYMBOL_GPL(l2tp_udp_recv_core);
816
817 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
818  * Return codes:
819  * 0 : success.
820  * <0: error
821  * >0: skb should be passed up to userspace as UDP.
822  */
823 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
824 {
825         struct l2tp_tunnel *tunnel;
826
827         tunnel = l2tp_sock_to_tunnel(sk);
828         if (tunnel == NULL)
829                 goto pass_up;
830
831         PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
832                "%s: received %d bytes\n", tunnel->name, skb->len);
833
834         if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
835                 goto pass_up_put;
836
837         sock_put(sk);
838         return 0;
839
840 pass_up_put:
841         sock_put(sk);
842 pass_up:
843         return 1;
844 }
845 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
846
847 /************************************************************************
848  * Transmit handling
849  ***********************************************************************/
850
851 /* Build an L2TP header for the session into the buffer provided.
852  */
853 static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
854 {
855         struct l2tp_tunnel *tunnel = session->tunnel;
856         __be16 *bufp = buf;
857         __be16 *optr = buf;
858         u16 flags = L2TP_HDR_VER_2;
859         u32 tunnel_id = tunnel->peer_tunnel_id;
860         u32 session_id = session->peer_session_id;
861
862         if (session->send_seq)
863                 flags |= L2TP_HDRFLAG_S;
864
865         /* Setup L2TP header. */
866         *bufp++ = htons(flags);
867         *bufp++ = htons(tunnel_id);
868         *bufp++ = htons(session_id);
869         if (session->send_seq) {
870                 *bufp++ = htons(session->ns);
871                 *bufp++ = 0;
872                 session->ns++;
873                 session->ns &= 0xffff;
874                 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
875                        "%s: updated ns to %u\n", session->name, session->ns);
876         }
877
878         return bufp - optr;
879 }
880
881 static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
882 {
883         struct l2tp_tunnel *tunnel = session->tunnel;
884         char *bufp = buf;
885         char *optr = bufp;
886
887         /* Setup L2TP header. The header differs slightly for UDP and
888          * IP encapsulations. For UDP, there is 4 bytes of flags.
889          */
890         if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
891                 u16 flags = L2TP_HDR_VER_3;
892                 *((__be16 *) bufp) = htons(flags);
893                 bufp += 2;
894                 *((__be16 *) bufp) = 0;
895                 bufp += 2;
896         }
897
898         *((__be32 *) bufp) = htonl(session->peer_session_id);
899         bufp += 4;
900         if (session->cookie_len) {
901                 memcpy(bufp, &session->cookie[0], session->cookie_len);
902                 bufp += session->cookie_len;
903         }
904         if (session->l2specific_len) {
905                 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
906                         u32 l2h = 0;
907                         if (session->send_seq) {
908                                 l2h = 0x40000000 | session->ns;
909                                 session->ns++;
910                                 session->ns &= 0xffffff;
911                                 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
912                                        "%s: updated ns to %u\n", session->name, session->ns);
913                         }
914
915                         *((__be32 *) bufp) = htonl(l2h);
916                 }
917                 bufp += session->l2specific_len;
918         }
919         if (session->offset)
920                 bufp += session->offset;
921
922         return bufp - optr;
923 }
924
925 int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, size_t data_len)
926 {
927         struct l2tp_tunnel *tunnel = session->tunnel;
928         unsigned int len = skb->len;
929         int error;
930
931         /* Debug */
932         if (session->send_seq)
933                 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
934                        "%s: send %Zd bytes, ns=%u\n", session->name,
935                        data_len, session->ns - 1);
936         else
937                 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
938                        "%s: send %Zd bytes\n", session->name, data_len);
939
940         if (session->debug & L2TP_MSG_DATA) {
941                 int i;
942                 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
943                 unsigned char *datap = skb->data + uhlen;
944
945                 printk(KERN_DEBUG "%s: xmit:", session->name);
946                 for (i = 0; i < (len - uhlen); i++) {
947                         printk(" %02X", *datap++);
948                         if (i == 31) {
949                                 printk(" ...");
950                                 break;
951                         }
952                 }
953                 printk("\n");
954         }
955
956         /* Queue the packet to IP for output */
957         error = ip_queue_xmit(skb, 1);
958
959         /* Update stats */
960         if (error >= 0) {
961                 tunnel->stats.tx_packets++;
962                 tunnel->stats.tx_bytes += len;
963                 session->stats.tx_packets++;
964                 session->stats.tx_bytes += len;
965         } else {
966                 tunnel->stats.tx_errors++;
967                 session->stats.tx_errors++;
968         }
969
970         return 0;
971 }
972 EXPORT_SYMBOL_GPL(l2tp_xmit_core);
973
974 /* Automatically called when the skb is freed.
975  */
976 static void l2tp_sock_wfree(struct sk_buff *skb)
977 {
978         sock_put(skb->sk);
979 }
980
981 /* For data skbs that we transmit, we associate with the tunnel socket
982  * but don't do accounting.
983  */
984 static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
985 {
986         sock_hold(sk);
987         skb->sk = sk;
988         skb->destructor = l2tp_sock_wfree;
989 }
990
991 /* If caller requires the skb to have a ppp header, the header must be
992  * inserted in the skb data before calling this function.
993  */
994 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
995 {
996         int data_len = skb->len;
997         struct l2tp_tunnel *tunnel = session->tunnel;
998         struct sock *sk = tunnel->sock;
999         struct udphdr *uh;
1000         struct inet_sock *inet;
1001         __wsum csum;
1002         int old_headroom;
1003         int new_headroom;
1004         int headroom;
1005         int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1006         int udp_len;
1007
1008         /* Check that there's enough headroom in the skb to insert IP,
1009          * UDP and L2TP headers. If not enough, expand it to
1010          * make room. Adjust truesize.
1011          */
1012         headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1013                 uhlen + hdr_len;
1014         old_headroom = skb_headroom(skb);
1015         if (skb_cow_head(skb, headroom))
1016                 goto abort;
1017
1018         new_headroom = skb_headroom(skb);
1019         skb_orphan(skb);
1020         skb->truesize += new_headroom - old_headroom;
1021
1022         /* Setup L2TP header */
1023         session->build_header(session, __skb_push(skb, hdr_len));
1024
1025         /* Reset skb netfilter state */
1026         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1027         IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1028                               IPSKB_REROUTED);
1029         nf_reset(skb);
1030
1031         /* Get routing info from the tunnel socket */
1032         skb_dst_drop(skb);
1033         skb_dst_set(skb, dst_clone(__sk_dst_get(sk)));
1034
1035         switch (tunnel->encap) {
1036         case L2TP_ENCAPTYPE_UDP:
1037                 /* Setup UDP header */
1038                 inet = inet_sk(sk);
1039                 __skb_push(skb, sizeof(*uh));
1040                 skb_reset_transport_header(skb);
1041                 uh = udp_hdr(skb);
1042                 uh->source = inet->inet_sport;
1043                 uh->dest = inet->inet_dport;
1044                 udp_len = uhlen + hdr_len + data_len;
1045                 uh->len = htons(udp_len);
1046                 uh->check = 0;
1047
1048                 /* Calculate UDP checksum if configured to do so */
1049                 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1050                         skb->ip_summed = CHECKSUM_NONE;
1051                 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1052                          (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1053                         skb->ip_summed = CHECKSUM_COMPLETE;
1054                         csum = skb_checksum(skb, 0, udp_len, 0);
1055                         uh->check = csum_tcpudp_magic(inet->inet_saddr,
1056                                                       inet->inet_daddr,
1057                                                       udp_len, IPPROTO_UDP, csum);
1058                         if (uh->check == 0)
1059                                 uh->check = CSUM_MANGLED_0;
1060                 } else {
1061                         skb->ip_summed = CHECKSUM_PARTIAL;
1062                         skb->csum_start = skb_transport_header(skb) - skb->head;
1063                         skb->csum_offset = offsetof(struct udphdr, check);
1064                         uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1065                                                        inet->inet_daddr,
1066                                                        udp_len, IPPROTO_UDP, 0);
1067                 }
1068                 break;
1069
1070         case L2TP_ENCAPTYPE_IP:
1071                 break;
1072         }
1073
1074         l2tp_skb_set_owner_w(skb, sk);
1075
1076         l2tp_xmit_core(session, skb, data_len);
1077
1078 abort:
1079         return 0;
1080 }
1081 EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1082
1083 /*****************************************************************************
1084  * Tinnel and session create/destroy.
1085  *****************************************************************************/
1086
1087 /* Tunnel socket destruct hook.
1088  * The tunnel context is deleted only when all session sockets have been
1089  * closed.
1090  */
1091 void l2tp_tunnel_destruct(struct sock *sk)
1092 {
1093         struct l2tp_tunnel *tunnel;
1094
1095         tunnel = sk->sk_user_data;
1096         if (tunnel == NULL)
1097                 goto end;
1098
1099         PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1100                "%s: closing...\n", tunnel->name);
1101
1102         /* Close all sessions */
1103         l2tp_tunnel_closeall(tunnel);
1104
1105         switch (tunnel->encap) {
1106         case L2TP_ENCAPTYPE_UDP:
1107                 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1108                 (udp_sk(sk))->encap_type = 0;
1109                 (udp_sk(sk))->encap_rcv = NULL;
1110                 break;
1111         case L2TP_ENCAPTYPE_IP:
1112                 break;
1113         }
1114
1115         /* Remove hooks into tunnel socket */
1116         tunnel->sock = NULL;
1117         sk->sk_destruct = tunnel->old_sk_destruct;
1118         sk->sk_user_data = NULL;
1119
1120         /* Call the original destructor */
1121         if (sk->sk_destruct)
1122                 (*sk->sk_destruct)(sk);
1123
1124         /* We're finished with the socket */
1125         l2tp_tunnel_dec_refcount(tunnel);
1126
1127 end:
1128         return;
1129 }
1130 EXPORT_SYMBOL(l2tp_tunnel_destruct);
1131
1132 /* When the tunnel is closed, all the attached sessions need to go too.
1133  */
1134 void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1135 {
1136         int hash;
1137         struct hlist_node *walk;
1138         struct hlist_node *tmp;
1139         struct l2tp_session *session;
1140
1141         BUG_ON(tunnel == NULL);
1142
1143         PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1144                "%s: closing all sessions...\n", tunnel->name);
1145
1146         write_lock_bh(&tunnel->hlist_lock);
1147         for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1148 again:
1149                 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1150                         session = hlist_entry(walk, struct l2tp_session, hlist);
1151
1152                         PRINTK(session->debug, L2TP_MSG_CONTROL, KERN_INFO,
1153                                "%s: closing session\n", session->name);
1154
1155                         hlist_del_init(&session->hlist);
1156
1157                         /* Since we should hold the sock lock while
1158                          * doing any unbinding, we need to release the
1159                          * lock we're holding before taking that lock.
1160                          * Hold a reference to the sock so it doesn't
1161                          * disappear as we're jumping between locks.
1162                          */
1163                         if (session->ref != NULL)
1164                                 (*session->ref)(session);
1165
1166                         write_unlock_bh(&tunnel->hlist_lock);
1167
1168                         if (tunnel->version != L2TP_HDR_VER_2) {
1169                                 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1170
1171                                 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1172                                 hlist_del_init_rcu(&session->global_hlist);
1173                                 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1174                                 synchronize_rcu();
1175                         }
1176
1177                         if (session->session_close != NULL)
1178                                 (*session->session_close)(session);
1179
1180                         if (session->deref != NULL)
1181                                 (*session->deref)(session);
1182
1183                         write_lock_bh(&tunnel->hlist_lock);
1184
1185                         /* Now restart from the beginning of this hash
1186                          * chain.  We always remove a session from the
1187                          * list so we are guaranteed to make forward
1188                          * progress.
1189                          */
1190                         goto again;
1191                 }
1192         }
1193         write_unlock_bh(&tunnel->hlist_lock);
1194 }
1195 EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
1196
1197 /* Really kill the tunnel.
1198  * Come here only when all sessions have been cleared from the tunnel.
1199  */
1200 void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
1201 {
1202         struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1203
1204         BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1205         BUG_ON(tunnel->sock != NULL);
1206
1207         PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1208                "%s: free...\n", tunnel->name);
1209
1210         /* Remove from tunnel list */
1211         spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1212         list_del_rcu(&tunnel->list);
1213         spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1214         synchronize_rcu();
1215
1216         atomic_dec(&l2tp_tunnel_count);
1217         kfree(tunnel);
1218 }
1219 EXPORT_SYMBOL_GPL(l2tp_tunnel_free);
1220
1221 /* Create a socket for the tunnel, if one isn't set up by
1222  * userspace. This is used for static tunnels where there is no
1223  * managing L2TP daemon.
1224  */
1225 static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct socket **sockp)
1226 {
1227         int err = -EINVAL;
1228         struct sockaddr_in udp_addr;
1229         struct sockaddr_l2tpip ip_addr;
1230         struct socket *sock = NULL;
1231
1232         switch (cfg->encap) {
1233         case L2TP_ENCAPTYPE_UDP:
1234                 err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp);
1235                 if (err < 0)
1236                         goto out;
1237
1238                 sock = *sockp;
1239
1240                 memset(&udp_addr, 0, sizeof(udp_addr));
1241                 udp_addr.sin_family = AF_INET;
1242                 udp_addr.sin_addr = cfg->local_ip;
1243                 udp_addr.sin_port = htons(cfg->local_udp_port);
1244                 err = kernel_bind(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr));
1245                 if (err < 0)
1246                         goto out;
1247
1248                 udp_addr.sin_family = AF_INET;
1249                 udp_addr.sin_addr = cfg->peer_ip;
1250                 udp_addr.sin_port = htons(cfg->peer_udp_port);
1251                 err = kernel_connect(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr), 0);
1252                 if (err < 0)
1253                         goto out;
1254
1255                 if (!cfg->use_udp_checksums)
1256                         sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1257
1258                 break;
1259
1260         case L2TP_ENCAPTYPE_IP:
1261                 err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP, sockp);
1262                 if (err < 0)
1263                         goto out;
1264
1265                 sock = *sockp;
1266
1267                 memset(&ip_addr, 0, sizeof(ip_addr));
1268                 ip_addr.l2tp_family = AF_INET;
1269                 ip_addr.l2tp_addr = cfg->local_ip;
1270                 ip_addr.l2tp_conn_id = tunnel_id;
1271                 err = kernel_bind(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr));
1272                 if (err < 0)
1273                         goto out;
1274
1275                 ip_addr.l2tp_family = AF_INET;
1276                 ip_addr.l2tp_addr = cfg->peer_ip;
1277                 ip_addr.l2tp_conn_id = peer_tunnel_id;
1278                 err = kernel_connect(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr), 0);
1279                 if (err < 0)
1280                         goto out;
1281
1282                 break;
1283
1284         default:
1285                 goto out;
1286         }
1287
1288 out:
1289         if ((err < 0) && sock) {
1290                 sock_release(sock);
1291                 *sockp = NULL;
1292         }
1293
1294         return err;
1295 }
1296
1297 int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
1298 {
1299         struct l2tp_tunnel *tunnel = NULL;
1300         int err;
1301         struct socket *sock = NULL;
1302         struct sock *sk = NULL;
1303         struct l2tp_net *pn;
1304         enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1305
1306         /* Get the tunnel socket from the fd, which was opened by
1307          * the userspace L2TP daemon. If not specified, create a
1308          * kernel socket.
1309          */
1310         if (fd < 0) {
1311                 err = l2tp_tunnel_sock_create(tunnel_id, peer_tunnel_id, cfg, &sock);
1312                 if (err < 0)
1313                         goto err;
1314         } else {
1315                 err = -EBADF;
1316                 sock = sockfd_lookup(fd, &err);
1317                 if (!sock) {
1318                         printk(KERN_ERR "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1319                                tunnel_id, fd, err);
1320                         goto err;
1321                 }
1322         }
1323
1324         sk = sock->sk;
1325
1326         if (cfg != NULL)
1327                 encap = cfg->encap;
1328
1329         /* Quick sanity checks */
1330         switch (encap) {
1331         case L2TP_ENCAPTYPE_UDP:
1332                 err = -EPROTONOSUPPORT;
1333                 if (sk->sk_protocol != IPPROTO_UDP) {
1334                         printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1335                                tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1336                         goto err;
1337                 }
1338                 break;
1339         case L2TP_ENCAPTYPE_IP:
1340                 err = -EPROTONOSUPPORT;
1341                 if (sk->sk_protocol != IPPROTO_L2TP) {
1342                         printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1343                                tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1344                         goto err;
1345                 }
1346                 break;
1347         }
1348
1349         /* Check if this socket has already been prepped */
1350         tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1351         if (tunnel != NULL) {
1352                 /* This socket has already been prepped */
1353                 err = -EBUSY;
1354                 goto err;
1355         }
1356
1357         tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1358         if (tunnel == NULL) {
1359                 err = -ENOMEM;
1360                 goto err;
1361         }
1362
1363         tunnel->version = version;
1364         tunnel->tunnel_id = tunnel_id;
1365         tunnel->peer_tunnel_id = peer_tunnel_id;
1366         tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1367
1368         tunnel->magic = L2TP_TUNNEL_MAGIC;
1369         sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1370         rwlock_init(&tunnel->hlist_lock);
1371
1372         /* The net we belong to */
1373         tunnel->l2tp_net = net;
1374         pn = l2tp_pernet(net);
1375
1376         if (cfg != NULL)
1377                 tunnel->debug = cfg->debug;
1378
1379         /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1380         tunnel->encap = encap;
1381         if (encap == L2TP_ENCAPTYPE_UDP) {
1382                 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1383                 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1384                 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
1385         }
1386
1387         sk->sk_user_data = tunnel;
1388
1389         /* Hook on the tunnel socket destructor so that we can cleanup
1390          * if the tunnel socket goes away.
1391          */
1392         tunnel->old_sk_destruct = sk->sk_destruct;
1393         sk->sk_destruct = &l2tp_tunnel_destruct;
1394         tunnel->sock = sk;
1395         sk->sk_allocation = GFP_ATOMIC;
1396
1397         /* Add tunnel to our list */
1398         INIT_LIST_HEAD(&tunnel->list);
1399         spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1400         list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1401         spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1402         synchronize_rcu();
1403         atomic_inc(&l2tp_tunnel_count);
1404
1405         /* Bump the reference count. The tunnel context is deleted
1406          * only when this drops to zero.
1407          */
1408         l2tp_tunnel_inc_refcount(tunnel);
1409
1410         err = 0;
1411 err:
1412         if (tunnelp)
1413                 *tunnelp = tunnel;
1414
1415         /* If tunnel's socket was created by the kernel, it doesn't
1416          *  have a file.
1417          */
1418         if (sock && sock->file)
1419                 sockfd_put(sock);
1420
1421         return err;
1422 }
1423 EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1424
1425 /* This function is used by the netlink TUNNEL_DELETE command.
1426  */
1427 int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1428 {
1429         int err = 0;
1430         struct socket *sock = tunnel->sock ? tunnel->sock->sk_socket : NULL;
1431
1432         /* Force the tunnel socket to close. This will eventually
1433          * cause the tunnel to be deleted via the normal socket close
1434          * mechanisms when userspace closes the tunnel socket.
1435          */
1436         if (sock != NULL) {
1437                 err = inet_shutdown(sock, 2);
1438
1439                 /* If the tunnel's socket was created by the kernel,
1440                  * close the socket here since the socket was not
1441                  * created by userspace.
1442                  */
1443                 if (sock->file == NULL)
1444                         err = inet_release(sock);
1445         }
1446
1447         return err;
1448 }
1449 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1450
1451 /* Really kill the session.
1452  */
1453 void l2tp_session_free(struct l2tp_session *session)
1454 {
1455         struct l2tp_tunnel *tunnel;
1456
1457         BUG_ON(atomic_read(&session->ref_count) != 0);
1458
1459         tunnel = session->tunnel;
1460         if (tunnel != NULL) {
1461                 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1462
1463                 /* Delete the session from the hash */
1464                 write_lock_bh(&tunnel->hlist_lock);
1465                 hlist_del_init(&session->hlist);
1466                 write_unlock_bh(&tunnel->hlist_lock);
1467
1468                 /* Unlink from the global hash if not L2TPv2 */
1469                 if (tunnel->version != L2TP_HDR_VER_2) {
1470                         struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1471
1472                         spin_lock_bh(&pn->l2tp_session_hlist_lock);
1473                         hlist_del_init_rcu(&session->global_hlist);
1474                         spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1475                         synchronize_rcu();
1476                 }
1477
1478                 if (session->session_id != 0)
1479                         atomic_dec(&l2tp_session_count);
1480
1481                 sock_put(tunnel->sock);
1482
1483                 /* This will delete the tunnel context if this
1484                  * is the last session on the tunnel.
1485                  */
1486                 session->tunnel = NULL;
1487                 l2tp_tunnel_dec_refcount(tunnel);
1488         }
1489
1490         kfree(session);
1491
1492         return;
1493 }
1494 EXPORT_SYMBOL_GPL(l2tp_session_free);
1495
1496 /* This function is used by the netlink SESSION_DELETE command and by
1497    pseudowire modules.
1498  */
1499 int l2tp_session_delete(struct l2tp_session *session)
1500 {
1501         if (session->session_close != NULL)
1502                 (*session->session_close)(session);
1503
1504         l2tp_session_dec_refcount(session);
1505
1506         return 0;
1507 }
1508 EXPORT_SYMBOL_GPL(l2tp_session_delete);
1509
1510
1511 /* We come here whenever a session's send_seq, cookie_len or
1512  * l2specific_len parameters are set.
1513  */
1514 void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1515 {
1516         if (version == L2TP_HDR_VER_2) {
1517                 session->hdr_len = 6;
1518                 if (session->send_seq)
1519                         session->hdr_len += 4;
1520         } else {
1521                 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1522                 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1523                         session->hdr_len += 4;
1524         }
1525
1526 }
1527 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
1528
1529 struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1530 {
1531         struct l2tp_session *session;
1532
1533         session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1534         if (session != NULL) {
1535                 session->magic = L2TP_SESSION_MAGIC;
1536                 session->tunnel = tunnel;
1537
1538                 session->session_id = session_id;
1539                 session->peer_session_id = peer_session_id;
1540                 session->nr = 1;
1541
1542                 sprintf(&session->name[0], "sess %u/%u",
1543                         tunnel->tunnel_id, session->session_id);
1544
1545                 skb_queue_head_init(&session->reorder_q);
1546
1547                 INIT_HLIST_NODE(&session->hlist);
1548                 INIT_HLIST_NODE(&session->global_hlist);
1549
1550                 /* Inherit debug options from tunnel */
1551                 session->debug = tunnel->debug;
1552
1553                 if (cfg) {
1554                         session->pwtype = cfg->pw_type;
1555                         session->debug = cfg->debug;
1556                         session->mtu = cfg->mtu;
1557                         session->mru = cfg->mru;
1558                         session->send_seq = cfg->send_seq;
1559                         session->recv_seq = cfg->recv_seq;
1560                         session->lns_mode = cfg->lns_mode;
1561                         session->reorder_timeout = cfg->reorder_timeout;
1562                         session->offset = cfg->offset;
1563                         session->l2specific_type = cfg->l2specific_type;
1564                         session->l2specific_len = cfg->l2specific_len;
1565                         session->cookie_len = cfg->cookie_len;
1566                         memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1567                         session->peer_cookie_len = cfg->peer_cookie_len;
1568                         memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1569                 }
1570
1571                 if (tunnel->version == L2TP_HDR_VER_2)
1572                         session->build_header = l2tp_build_l2tpv2_header;
1573                 else
1574                         session->build_header = l2tp_build_l2tpv3_header;
1575
1576                 l2tp_session_set_header_len(session, tunnel->version);
1577
1578                 /* Bump the reference count. The session context is deleted
1579                  * only when this drops to zero.
1580                  */
1581                 l2tp_session_inc_refcount(session);
1582                 l2tp_tunnel_inc_refcount(tunnel);
1583
1584                 /* Ensure tunnel socket isn't deleted */
1585                 sock_hold(tunnel->sock);
1586
1587                 /* Add session to the tunnel's hash list */
1588                 write_lock_bh(&tunnel->hlist_lock);
1589                 hlist_add_head(&session->hlist,
1590                                l2tp_session_id_hash(tunnel, session_id));
1591                 write_unlock_bh(&tunnel->hlist_lock);
1592
1593                 /* And to the global session list if L2TPv3 */
1594                 if (tunnel->version != L2TP_HDR_VER_2) {
1595                         struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1596
1597                         spin_lock_bh(&pn->l2tp_session_hlist_lock);
1598                         hlist_add_head_rcu(&session->global_hlist,
1599                                            l2tp_session_id_hash_2(pn, session_id));
1600                         spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1601                         synchronize_rcu();
1602                 }
1603
1604                 /* Ignore management session in session count value */
1605                 if (session->session_id != 0)
1606                         atomic_inc(&l2tp_session_count);
1607         }
1608
1609         return session;
1610 }
1611 EXPORT_SYMBOL_GPL(l2tp_session_create);
1612
1613 /*****************************************************************************
1614  * Init and cleanup
1615  *****************************************************************************/
1616
1617 static __net_init int l2tp_init_net(struct net *net)
1618 {
1619         struct l2tp_net *pn;
1620         int err;
1621         int hash;
1622
1623         pn = kzalloc(sizeof(*pn), GFP_KERNEL);
1624         if (!pn)
1625                 return -ENOMEM;
1626
1627         INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1628         spin_lock_init(&pn->l2tp_tunnel_list_lock);
1629
1630         for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1631                 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1632
1633         spin_lock_init(&pn->l2tp_session_hlist_lock);
1634
1635         err = net_assign_generic(net, l2tp_net_id, pn);
1636         if (err)
1637                 goto out;
1638
1639         return 0;
1640
1641 out:
1642         kfree(pn);
1643         return err;
1644 }
1645
1646 static __net_exit void l2tp_exit_net(struct net *net)
1647 {
1648         struct l2tp_net *pn;
1649
1650         pn = net_generic(net, l2tp_net_id);
1651         /*
1652          * if someone has cached our net then
1653          * further net_generic call will return NULL
1654          */
1655         net_assign_generic(net, l2tp_net_id, NULL);
1656         kfree(pn);
1657 }
1658
1659 static struct pernet_operations l2tp_net_ops = {
1660         .init = l2tp_init_net,
1661         .exit = l2tp_exit_net,
1662         .id   = &l2tp_net_id,
1663         .size = sizeof(struct l2tp_net),
1664 };
1665
1666 static int __init l2tp_init(void)
1667 {
1668         int rc = 0;
1669
1670         rc = register_pernet_device(&l2tp_net_ops);
1671         if (rc)
1672                 goto out;
1673
1674         printk(KERN_INFO "L2TP core driver, %s\n", L2TP_DRV_VERSION);
1675
1676 out:
1677         return rc;
1678 }
1679
1680 static void __exit l2tp_exit(void)
1681 {
1682         unregister_pernet_device(&l2tp_net_ops);
1683 }
1684
1685 module_init(l2tp_init);
1686 module_exit(l2tp_exit);
1687
1688 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1689 MODULE_DESCRIPTION("L2TP core");
1690 MODULE_LICENSE("GPL");
1691 MODULE_VERSION(L2TP_DRV_VERSION);
1692