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