James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1 | /***************************************************************************** |
| 2 | * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets |
| 3 | * |
| 4 | * PPPoX --- Generic PPP encapsulation socket family |
| 5 | * PPPoL2TP --- PPP over L2TP (RFC 2661) |
| 6 | * |
| 7 | * Version: 1.0.0 |
| 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 | * License: |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License |
| 19 | * as published by the Free Software Foundation; either version |
| 20 | * 2 of the License, or (at your option) any later version. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | /* This driver handles only L2TP data frames; control frames are handled by a |
| 25 | * userspace application. |
| 26 | * |
| 27 | * To send data in an L2TP session, userspace opens a PPPoL2TP socket and |
| 28 | * attaches it to a bound UDP socket with local tunnel_id / session_id and |
| 29 | * peer tunnel_id / session_id set. Data can then be sent or received using |
| 30 | * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket |
| 31 | * can be read or modified using ioctl() or [gs]etsockopt() calls. |
| 32 | * |
| 33 | * When a PPPoL2TP socket is connected with local and peer session_id values |
| 34 | * zero, the socket is treated as a special tunnel management socket. |
| 35 | * |
| 36 | * Here's example userspace code to create a socket for sending/receiving data |
| 37 | * over an L2TP session:- |
| 38 | * |
| 39 | * struct sockaddr_pppol2tp sax; |
| 40 | * int fd; |
| 41 | * int session_fd; |
| 42 | * |
| 43 | * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP); |
| 44 | * |
| 45 | * sax.sa_family = AF_PPPOX; |
| 46 | * sax.sa_protocol = PX_PROTO_OL2TP; |
| 47 | * sax.pppol2tp.fd = tunnel_fd; // bound UDP socket |
| 48 | * sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr; |
| 49 | * sax.pppol2tp.addr.sin_port = addr->sin_port; |
| 50 | * sax.pppol2tp.addr.sin_family = AF_INET; |
| 51 | * sax.pppol2tp.s_tunnel = tunnel_id; |
| 52 | * sax.pppol2tp.s_session = session_id; |
| 53 | * sax.pppol2tp.d_tunnel = peer_tunnel_id; |
| 54 | * sax.pppol2tp.d_session = peer_session_id; |
| 55 | * |
| 56 | * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax)); |
| 57 | * |
| 58 | * A pppd plugin that allows PPP traffic to be carried over L2TP using |
| 59 | * this driver is available from the OpenL2TP project at |
| 60 | * http://openl2tp.sourceforge.net. |
| 61 | */ |
| 62 | |
| 63 | #include <linux/module.h> |
| 64 | #include <linux/version.h> |
| 65 | #include <linux/string.h> |
| 66 | #include <linux/list.h> |
| 67 | #include <asm/uaccess.h> |
| 68 | |
| 69 | #include <linux/kernel.h> |
| 70 | #include <linux/spinlock.h> |
| 71 | #include <linux/kthread.h> |
| 72 | #include <linux/sched.h> |
| 73 | #include <linux/slab.h> |
| 74 | #include <linux/errno.h> |
| 75 | #include <linux/jiffies.h> |
| 76 | |
| 77 | #include <linux/netdevice.h> |
| 78 | #include <linux/net.h> |
| 79 | #include <linux/inetdevice.h> |
| 80 | #include <linux/skbuff.h> |
| 81 | #include <linux/init.h> |
| 82 | #include <linux/ip.h> |
| 83 | #include <linux/udp.h> |
| 84 | #include <linux/if_pppox.h> |
| 85 | #include <linux/if_pppol2tp.h> |
| 86 | #include <net/sock.h> |
| 87 | #include <linux/ppp_channel.h> |
| 88 | #include <linux/ppp_defs.h> |
| 89 | #include <linux/if_ppp.h> |
| 90 | #include <linux/file.h> |
| 91 | #include <linux/hash.h> |
| 92 | #include <linux/sort.h> |
| 93 | #include <linux/proc_fs.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 94 | #include <net/net_namespace.h> |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 95 | #include <net/dst.h> |
| 96 | #include <net/ip.h> |
| 97 | #include <net/udp.h> |
| 98 | #include <net/xfrm.h> |
| 99 | |
| 100 | #include <asm/byteorder.h> |
| 101 | #include <asm/atomic.h> |
| 102 | |
| 103 | |
| 104 | #define PPPOL2TP_DRV_VERSION "V1.0" |
| 105 | |
| 106 | /* L2TP header constants */ |
| 107 | #define L2TP_HDRFLAG_T 0x8000 |
| 108 | #define L2TP_HDRFLAG_L 0x4000 |
| 109 | #define L2TP_HDRFLAG_S 0x0800 |
| 110 | #define L2TP_HDRFLAG_O 0x0200 |
| 111 | #define L2TP_HDRFLAG_P 0x0100 |
| 112 | |
| 113 | #define L2TP_HDR_VER_MASK 0x000F |
| 114 | #define L2TP_HDR_VER 0x0002 |
| 115 | |
| 116 | /* Space for UDP, L2TP and PPP headers */ |
| 117 | #define PPPOL2TP_HEADER_OVERHEAD 40 |
| 118 | |
| 119 | /* Just some random numbers */ |
| 120 | #define L2TP_TUNNEL_MAGIC 0x42114DDA |
| 121 | #define L2TP_SESSION_MAGIC 0x0C04EB7D |
| 122 | |
| 123 | #define PPPOL2TP_HASH_BITS 4 |
| 124 | #define PPPOL2TP_HASH_SIZE (1 << PPPOL2TP_HASH_BITS) |
| 125 | |
| 126 | /* Default trace flags */ |
| 127 | #define PPPOL2TP_DEFAULT_DEBUG_FLAGS 0 |
| 128 | |
| 129 | #define PRINTK(_mask, _type, _lvl, _fmt, args...) \ |
| 130 | do { \ |
| 131 | if ((_mask) & (_type)) \ |
| 132 | printk(_lvl "PPPOL2TP: " _fmt, ##args); \ |
| 133 | } while(0) |
| 134 | |
| 135 | /* Number of bytes to build transmit L2TP headers. |
| 136 | * Unfortunately the size is different depending on whether sequence numbers |
| 137 | * are enabled. |
| 138 | */ |
| 139 | #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10 |
| 140 | #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6 |
| 141 | |
| 142 | struct pppol2tp_tunnel; |
| 143 | |
| 144 | /* Describes a session. It is the sk_user_data field in the PPPoL2TP |
| 145 | * socket. Contains information to determine incoming packets and transmit |
| 146 | * outgoing ones. |
| 147 | */ |
| 148 | struct pppol2tp_session |
| 149 | { |
| 150 | int magic; /* should be |
| 151 | * L2TP_SESSION_MAGIC */ |
| 152 | int owner; /* pid that opened the socket */ |
| 153 | |
| 154 | struct sock *sock; /* Pointer to the session |
| 155 | * PPPoX socket */ |
| 156 | struct sock *tunnel_sock; /* Pointer to the tunnel UDP |
| 157 | * socket */ |
| 158 | |
| 159 | struct pppol2tp_addr tunnel_addr; /* Description of tunnel */ |
| 160 | |
| 161 | struct pppol2tp_tunnel *tunnel; /* back pointer to tunnel |
| 162 | * context */ |
| 163 | |
| 164 | char name[20]; /* "sess xxxxx/yyyyy", where |
| 165 | * x=tunnel_id, y=session_id */ |
| 166 | int mtu; |
| 167 | int mru; |
| 168 | int flags; /* accessed by PPPIOCGFLAGS. |
| 169 | * Unused. */ |
| 170 | unsigned recv_seq:1; /* expect receive packets with |
| 171 | * sequence numbers? */ |
| 172 | unsigned send_seq:1; /* send packets with sequence |
| 173 | * numbers? */ |
| 174 | unsigned lns_mode:1; /* behave as LNS? LAC enables |
| 175 | * sequence numbers under |
| 176 | * control of LNS. */ |
| 177 | int debug; /* bitmask of debug message |
| 178 | * categories */ |
| 179 | int reorder_timeout; /* configured reorder timeout |
| 180 | * (in jiffies) */ |
| 181 | u16 nr; /* session NR state (receive) */ |
| 182 | u16 ns; /* session NR state (send) */ |
| 183 | struct sk_buff_head reorder_q; /* receive reorder queue */ |
| 184 | struct pppol2tp_ioc_stats stats; |
| 185 | struct hlist_node hlist; /* Hash list node */ |
| 186 | }; |
| 187 | |
| 188 | /* The sk_user_data field of the tunnel's UDP socket. It contains info to track |
| 189 | * all the associated sessions so incoming packets can be sorted out |
| 190 | */ |
| 191 | struct pppol2tp_tunnel |
| 192 | { |
| 193 | int magic; /* Should be L2TP_TUNNEL_MAGIC */ |
| 194 | rwlock_t hlist_lock; /* protect session_hlist */ |
| 195 | struct hlist_head session_hlist[PPPOL2TP_HASH_SIZE]; |
| 196 | /* hashed list of sessions, |
| 197 | * hashed by id */ |
| 198 | int debug; /* bitmask of debug message |
| 199 | * categories */ |
| 200 | char name[12]; /* "tunl xxxxx" */ |
| 201 | struct pppol2tp_ioc_stats stats; |
| 202 | |
| 203 | void (*old_sk_destruct)(struct sock *); |
| 204 | |
| 205 | struct sock *sock; /* Parent socket */ |
| 206 | struct list_head list; /* Keep a list of all open |
| 207 | * prepared sockets */ |
| 208 | |
| 209 | atomic_t ref_count; |
| 210 | }; |
| 211 | |
| 212 | /* Private data stored for received packets in the skb. |
| 213 | */ |
| 214 | struct pppol2tp_skb_cb { |
| 215 | u16 ns; |
| 216 | u16 nr; |
| 217 | u16 has_seq; |
| 218 | u16 length; |
| 219 | unsigned long expires; |
| 220 | }; |
| 221 | |
| 222 | #define PPPOL2TP_SKB_CB(skb) ((struct pppol2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)]) |
| 223 | |
| 224 | static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb); |
| 225 | static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel); |
| 226 | |
| 227 | static atomic_t pppol2tp_tunnel_count; |
| 228 | static atomic_t pppol2tp_session_count; |
| 229 | static struct ppp_channel_ops pppol2tp_chan_ops = { pppol2tp_xmit , NULL }; |
| 230 | static struct proto_ops pppol2tp_ops; |
| 231 | static LIST_HEAD(pppol2tp_tunnel_list); |
| 232 | static DEFINE_RWLOCK(pppol2tp_tunnel_list_lock); |
| 233 | |
| 234 | /* Helpers to obtain tunnel/session contexts from sockets. |
| 235 | */ |
| 236 | static inline struct pppol2tp_session *pppol2tp_sock_to_session(struct sock *sk) |
| 237 | { |
| 238 | struct pppol2tp_session *session; |
| 239 | |
| 240 | if (sk == NULL) |
| 241 | return NULL; |
| 242 | |
| 243 | session = (struct pppol2tp_session *)(sk->sk_user_data); |
| 244 | if (session == NULL) |
| 245 | return NULL; |
| 246 | |
| 247 | BUG_ON(session->magic != L2TP_SESSION_MAGIC); |
| 248 | |
| 249 | return session; |
| 250 | } |
| 251 | |
| 252 | static inline struct pppol2tp_tunnel *pppol2tp_sock_to_tunnel(struct sock *sk) |
| 253 | { |
| 254 | struct pppol2tp_tunnel *tunnel; |
| 255 | |
| 256 | if (sk == NULL) |
| 257 | return NULL; |
| 258 | |
| 259 | tunnel = (struct pppol2tp_tunnel *)(sk->sk_user_data); |
| 260 | if (tunnel == NULL) |
| 261 | return NULL; |
| 262 | |
| 263 | BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); |
| 264 | |
| 265 | return tunnel; |
| 266 | } |
| 267 | |
| 268 | /* Tunnel reference counts. Incremented per session that is added to |
| 269 | * the tunnel. |
| 270 | */ |
| 271 | static inline void pppol2tp_tunnel_inc_refcount(struct pppol2tp_tunnel *tunnel) |
| 272 | { |
| 273 | atomic_inc(&tunnel->ref_count); |
| 274 | } |
| 275 | |
| 276 | static inline void pppol2tp_tunnel_dec_refcount(struct pppol2tp_tunnel *tunnel) |
| 277 | { |
| 278 | if (atomic_dec_and_test(&tunnel->ref_count)) |
| 279 | pppol2tp_tunnel_free(tunnel); |
| 280 | } |
| 281 | |
| 282 | /* Session hash list. |
| 283 | * The session_id SHOULD be random according to RFC2661, but several |
| 284 | * L2TP implementations (Cisco and Microsoft) use incrementing |
| 285 | * session_ids. So we do a real hash on the session_id, rather than a |
| 286 | * simple bitmask. |
| 287 | */ |
| 288 | static inline struct hlist_head * |
| 289 | pppol2tp_session_id_hash(struct pppol2tp_tunnel *tunnel, u16 session_id) |
| 290 | { |
| 291 | unsigned long hash_val = (unsigned long) session_id; |
| 292 | return &tunnel->session_hlist[hash_long(hash_val, PPPOL2TP_HASH_BITS)]; |
| 293 | } |
| 294 | |
| 295 | /* Lookup a session by id |
| 296 | */ |
| 297 | static struct pppol2tp_session * |
| 298 | pppol2tp_session_find(struct pppol2tp_tunnel *tunnel, u16 session_id) |
| 299 | { |
| 300 | struct hlist_head *session_list = |
| 301 | pppol2tp_session_id_hash(tunnel, session_id); |
| 302 | struct pppol2tp_session *session; |
| 303 | struct hlist_node *walk; |
| 304 | |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 305 | read_lock_bh(&tunnel->hlist_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 306 | hlist_for_each_entry(session, walk, session_list, hlist) { |
| 307 | if (session->tunnel_addr.s_session == session_id) { |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 308 | read_unlock_bh(&tunnel->hlist_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 309 | return session; |
| 310 | } |
| 311 | } |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 312 | read_unlock_bh(&tunnel->hlist_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 313 | |
| 314 | return NULL; |
| 315 | } |
| 316 | |
| 317 | /* Lookup a tunnel by id |
| 318 | */ |
| 319 | static struct pppol2tp_tunnel *pppol2tp_tunnel_find(u16 tunnel_id) |
| 320 | { |
| 321 | struct pppol2tp_tunnel *tunnel = NULL; |
| 322 | |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 323 | read_lock_bh(&pppol2tp_tunnel_list_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 324 | list_for_each_entry(tunnel, &pppol2tp_tunnel_list, list) { |
| 325 | if (tunnel->stats.tunnel_id == tunnel_id) { |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 326 | read_unlock_bh(&pppol2tp_tunnel_list_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 327 | return tunnel; |
| 328 | } |
| 329 | } |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 330 | read_unlock_bh(&pppol2tp_tunnel_list_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 331 | |
| 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | /***************************************************************************** |
| 336 | * Receive data handling |
| 337 | *****************************************************************************/ |
| 338 | |
| 339 | /* Queue a skb in order. We come here only if the skb has an L2TP sequence |
| 340 | * number. |
| 341 | */ |
| 342 | static void pppol2tp_recv_queue_skb(struct pppol2tp_session *session, struct sk_buff *skb) |
| 343 | { |
| 344 | struct sk_buff *skbp; |
James Chapman | e653181 | 2008-03-05 18:40:01 -0800 | [diff] [blame^] | 345 | struct sk_buff *tmp; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 346 | u16 ns = PPPOL2TP_SKB_CB(skb)->ns; |
| 347 | |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 348 | spin_lock_bh(&session->reorder_q.lock); |
James Chapman | e653181 | 2008-03-05 18:40:01 -0800 | [diff] [blame^] | 349 | skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 350 | if (PPPOL2TP_SKB_CB(skbp)->ns > ns) { |
| 351 | __skb_insert(skb, skbp->prev, skbp, &session->reorder_q); |
| 352 | PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, |
| 353 | "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", |
| 354 | session->name, ns, PPPOL2TP_SKB_CB(skbp)->ns, |
| 355 | skb_queue_len(&session->reorder_q)); |
| 356 | session->stats.rx_oos_packets++; |
| 357 | goto out; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | __skb_queue_tail(&session->reorder_q, skb); |
| 362 | |
| 363 | out: |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 364 | spin_unlock_bh(&session->reorder_q.lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /* Dequeue a single skb. |
| 368 | */ |
| 369 | static void pppol2tp_recv_dequeue_skb(struct pppol2tp_session *session, struct sk_buff *skb) |
| 370 | { |
| 371 | struct pppol2tp_tunnel *tunnel = session->tunnel; |
| 372 | int length = PPPOL2TP_SKB_CB(skb)->length; |
| 373 | struct sock *session_sock = NULL; |
| 374 | |
James Chapman | e653181 | 2008-03-05 18:40:01 -0800 | [diff] [blame^] | 375 | /* We're about to requeue the skb, so return resources |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 376 | * to its current owner (a socket receive buffer). |
| 377 | */ |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 378 | skb_orphan(skb); |
| 379 | |
| 380 | tunnel->stats.rx_packets++; |
| 381 | tunnel->stats.rx_bytes += length; |
| 382 | session->stats.rx_packets++; |
| 383 | session->stats.rx_bytes += length; |
| 384 | |
| 385 | if (PPPOL2TP_SKB_CB(skb)->has_seq) { |
| 386 | /* Bump our Nr */ |
| 387 | session->nr++; |
| 388 | PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, |
| 389 | "%s: updated nr to %hu\n", session->name, session->nr); |
| 390 | } |
| 391 | |
| 392 | /* If the socket is bound, send it in to PPP's input queue. Otherwise |
| 393 | * queue it on the session socket. |
| 394 | */ |
| 395 | session_sock = session->sock; |
| 396 | if (session_sock->sk_state & PPPOX_BOUND) { |
| 397 | struct pppox_sock *po; |
| 398 | PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, |
| 399 | "%s: recv %d byte data frame, passing to ppp\n", |
| 400 | session->name, length); |
| 401 | |
| 402 | /* We need to forget all info related to the L2TP packet |
| 403 | * gathered in the skb as we are going to reuse the same |
| 404 | * skb for the inner packet. |
| 405 | * Namely we need to: |
| 406 | * - reset xfrm (IPSec) information as it applies to |
| 407 | * the outer L2TP packet and not to the inner one |
| 408 | * - release the dst to force a route lookup on the inner |
| 409 | * IP packet since skb->dst currently points to the dst |
| 410 | * of the UDP tunnel |
| 411 | * - reset netfilter information as it doesn't apply |
| 412 | * to the inner packet either |
| 413 | */ |
| 414 | secpath_reset(skb); |
| 415 | dst_release(skb->dst); |
| 416 | skb->dst = NULL; |
| 417 | nf_reset(skb); |
| 418 | |
| 419 | po = pppox_sk(session_sock); |
| 420 | ppp_input(&po->chan, skb); |
| 421 | } else { |
| 422 | PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_INFO, |
| 423 | "%s: socket not bound\n", session->name); |
| 424 | |
| 425 | /* Not bound. Nothing we can do, so discard. */ |
| 426 | session->stats.rx_errors++; |
| 427 | kfree_skb(skb); |
| 428 | } |
| 429 | |
| 430 | sock_put(session->sock); |
| 431 | } |
| 432 | |
| 433 | /* Dequeue skbs from the session's reorder_q, subject to packet order. |
| 434 | * Skbs that have been in the queue for too long are simply discarded. |
| 435 | */ |
| 436 | static void pppol2tp_recv_dequeue(struct pppol2tp_session *session) |
| 437 | { |
| 438 | struct sk_buff *skb; |
| 439 | struct sk_buff *tmp; |
| 440 | |
| 441 | /* If the pkt at the head of the queue has the nr that we |
| 442 | * expect to send up next, dequeue it and any other |
| 443 | * in-sequence packets behind it. |
| 444 | */ |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 445 | spin_lock_bh(&session->reorder_q.lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 446 | skb_queue_walk_safe(&session->reorder_q, skb, tmp) { |
| 447 | if (time_after(jiffies, PPPOL2TP_SKB_CB(skb)->expires)) { |
| 448 | session->stats.rx_seq_discards++; |
| 449 | session->stats.rx_errors++; |
| 450 | PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, |
| 451 | "%s: oos pkt %hu len %d discarded (too old), " |
| 452 | "waiting for %hu, reorder_q_len=%d\n", |
| 453 | session->name, PPPOL2TP_SKB_CB(skb)->ns, |
| 454 | PPPOL2TP_SKB_CB(skb)->length, session->nr, |
| 455 | skb_queue_len(&session->reorder_q)); |
| 456 | __skb_unlink(skb, &session->reorder_q); |
| 457 | kfree_skb(skb); |
Jarek Poplawski | c8fff1c | 2008-03-03 20:48:53 -0800 | [diff] [blame] | 458 | sock_put(session->sock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 459 | continue; |
| 460 | } |
| 461 | |
| 462 | if (PPPOL2TP_SKB_CB(skb)->has_seq) { |
| 463 | if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) { |
| 464 | PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, |
| 465 | "%s: holding oos pkt %hu len %d, " |
| 466 | "waiting for %hu, reorder_q_len=%d\n", |
| 467 | session->name, PPPOL2TP_SKB_CB(skb)->ns, |
| 468 | PPPOL2TP_SKB_CB(skb)->length, session->nr, |
| 469 | skb_queue_len(&session->reorder_q)); |
| 470 | goto out; |
| 471 | } |
| 472 | } |
James Chapman | e653181 | 2008-03-05 18:40:01 -0800 | [diff] [blame^] | 473 | __skb_unlink(skb, &session->reorder_q); |
| 474 | |
| 475 | /* Process the skb. We release the queue lock while we |
| 476 | * do so to let other contexts process the queue. |
| 477 | */ |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 478 | spin_unlock_bh(&session->reorder_q.lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 479 | pppol2tp_recv_dequeue_skb(session, skb); |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 480 | spin_lock_bh(&session->reorder_q.lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | out: |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 484 | spin_unlock_bh(&session->reorder_q.lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* Internal receive frame. Do the real work of receiving an L2TP data frame |
| 488 | * here. The skb is not on a list when we get here. |
| 489 | * Returns 0 if the packet was a data packet and was successfully passed on. |
| 490 | * Returns 1 if the packet was not a good data packet and could not be |
| 491 | * forwarded. All such packets are passed up to userspace to deal with. |
| 492 | */ |
| 493 | static int pppol2tp_recv_core(struct sock *sock, struct sk_buff *skb) |
| 494 | { |
| 495 | struct pppol2tp_session *session = NULL; |
| 496 | struct pppol2tp_tunnel *tunnel; |
James Chapman | 9178100 | 2007-11-05 23:32:37 -0800 | [diff] [blame] | 497 | unsigned char *ptr, *optr; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 498 | u16 hdrflags; |
| 499 | u16 tunnel_id, session_id; |
| 500 | int length; |
Herbert Xu | 7a70e39 | 2007-09-18 13:18:42 -0700 | [diff] [blame] | 501 | int offset; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 502 | |
| 503 | tunnel = pppol2tp_sock_to_tunnel(sock); |
| 504 | if (tunnel == NULL) |
James Chapman | 9178100 | 2007-11-05 23:32:37 -0800 | [diff] [blame] | 505 | goto no_tunnel; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 506 | |
Herbert Xu | 7a70e39 | 2007-09-18 13:18:42 -0700 | [diff] [blame] | 507 | /* UDP always verifies the packet length. */ |
| 508 | __skb_pull(skb, sizeof(struct udphdr)); |
| 509 | |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 510 | /* Short packet? */ |
Herbert Xu | 7a70e39 | 2007-09-18 13:18:42 -0700 | [diff] [blame] | 511 | if (!pskb_may_pull(skb, 12)) { |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 512 | PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO, |
| 513 | "%s: recv short packet (len=%d)\n", tunnel->name, skb->len); |
| 514 | goto error; |
| 515 | } |
| 516 | |
| 517 | /* Point to L2TP header */ |
James Chapman | 9178100 | 2007-11-05 23:32:37 -0800 | [diff] [blame] | 518 | optr = ptr = skb->data; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 519 | |
| 520 | /* Get L2TP header flags */ |
| 521 | hdrflags = ntohs(*(__be16*)ptr); |
| 522 | |
| 523 | /* Trace packet contents, if enabled */ |
| 524 | if (tunnel->debug & PPPOL2TP_MSG_DATA) { |
Herbert Xu | 7a70e39 | 2007-09-18 13:18:42 -0700 | [diff] [blame] | 525 | length = min(16u, skb->len); |
| 526 | if (!pskb_may_pull(skb, length)) |
| 527 | goto error; |
| 528 | |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 529 | printk(KERN_DEBUG "%s: recv: ", tunnel->name); |
| 530 | |
Herbert Xu | 7a70e39 | 2007-09-18 13:18:42 -0700 | [diff] [blame] | 531 | offset = 0; |
| 532 | do { |
| 533 | printk(" %02X", ptr[offset]); |
| 534 | } while (++offset < length); |
| 535 | |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 536 | printk("\n"); |
| 537 | } |
| 538 | |
| 539 | /* Get length of L2TP packet */ |
Herbert Xu | 7a70e39 | 2007-09-18 13:18:42 -0700 | [diff] [blame] | 540 | length = skb->len; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 541 | |
| 542 | /* If type is control packet, it is handled by userspace. */ |
| 543 | if (hdrflags & L2TP_HDRFLAG_T) { |
| 544 | PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, |
| 545 | "%s: recv control packet, len=%d\n", tunnel->name, length); |
| 546 | goto error; |
| 547 | } |
| 548 | |
| 549 | /* Skip flags */ |
| 550 | ptr += 2; |
| 551 | |
| 552 | /* If length is present, skip it */ |
| 553 | if (hdrflags & L2TP_HDRFLAG_L) |
| 554 | ptr += 2; |
| 555 | |
| 556 | /* Extract tunnel and session ID */ |
| 557 | tunnel_id = ntohs(*(__be16 *) ptr); |
| 558 | ptr += 2; |
| 559 | session_id = ntohs(*(__be16 *) ptr); |
| 560 | ptr += 2; |
| 561 | |
| 562 | /* Find the session context */ |
| 563 | session = pppol2tp_session_find(tunnel, session_id); |
| 564 | if (!session) { |
| 565 | /* Not found? Pass to userspace to deal with */ |
| 566 | PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO, |
| 567 | "%s: no socket found (%hu/%hu). Passing up.\n", |
| 568 | tunnel->name, tunnel_id, session_id); |
| 569 | goto error; |
| 570 | } |
| 571 | sock_hold(session->sock); |
| 572 | |
| 573 | /* The ref count on the socket was increased by the above call since |
| 574 | * we now hold a pointer to the session. Take care to do sock_put() |
| 575 | * when exiting this function from now on... |
| 576 | */ |
| 577 | |
| 578 | /* Handle the optional sequence numbers. If we are the LAC, |
| 579 | * enable/disable sequence numbers under the control of the LNS. If |
| 580 | * no sequence numbers present but we were expecting them, discard |
| 581 | * frame. |
| 582 | */ |
| 583 | if (hdrflags & L2TP_HDRFLAG_S) { |
| 584 | u16 ns, nr; |
| 585 | ns = ntohs(*(__be16 *) ptr); |
| 586 | ptr += 2; |
| 587 | nr = ntohs(*(__be16 *) ptr); |
| 588 | ptr += 2; |
| 589 | |
| 590 | /* Received a packet with sequence numbers. If we're the LNS, |
| 591 | * check if we sre sending sequence numbers and if not, |
| 592 | * configure it so. |
| 593 | */ |
| 594 | if ((!session->lns_mode) && (!session->send_seq)) { |
| 595 | PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO, |
| 596 | "%s: requested to enable seq numbers by LNS\n", |
| 597 | session->name); |
| 598 | session->send_seq = -1; |
| 599 | } |
| 600 | |
| 601 | /* Store L2TP info in the skb */ |
| 602 | PPPOL2TP_SKB_CB(skb)->ns = ns; |
| 603 | PPPOL2TP_SKB_CB(skb)->nr = nr; |
| 604 | PPPOL2TP_SKB_CB(skb)->has_seq = 1; |
| 605 | |
| 606 | PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, |
| 607 | "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n", |
| 608 | session->name, ns, nr, session->nr); |
| 609 | } else { |
| 610 | /* No sequence numbers. |
| 611 | * If user has configured mandatory sequence numbers, discard. |
| 612 | */ |
| 613 | if (session->recv_seq) { |
| 614 | PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING, |
| 615 | "%s: recv data has no seq numbers when required. " |
| 616 | "Discarding\n", session->name); |
| 617 | session->stats.rx_seq_discards++; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 618 | goto discard; |
| 619 | } |
| 620 | |
| 621 | /* If we're the LAC and we're sending sequence numbers, the |
| 622 | * LNS has requested that we no longer send sequence numbers. |
| 623 | * If we're the LNS and we're sending sequence numbers, the |
| 624 | * LAC is broken. Discard the frame. |
| 625 | */ |
| 626 | if ((!session->lns_mode) && (session->send_seq)) { |
| 627 | PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO, |
| 628 | "%s: requested to disable seq numbers by LNS\n", |
| 629 | session->name); |
| 630 | session->send_seq = 0; |
| 631 | } else if (session->send_seq) { |
| 632 | PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING, |
| 633 | "%s: recv data has no seq numbers when required. " |
| 634 | "Discarding\n", session->name); |
| 635 | session->stats.rx_seq_discards++; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 636 | goto discard; |
| 637 | } |
| 638 | |
| 639 | /* Store L2TP info in the skb */ |
| 640 | PPPOL2TP_SKB_CB(skb)->has_seq = 0; |
| 641 | } |
| 642 | |
| 643 | /* If offset bit set, skip it. */ |
Herbert Xu | 7a70e39 | 2007-09-18 13:18:42 -0700 | [diff] [blame] | 644 | if (hdrflags & L2TP_HDRFLAG_O) { |
| 645 | offset = ntohs(*(__be16 *)ptr); |
James Chapman | 9178100 | 2007-11-05 23:32:37 -0800 | [diff] [blame] | 646 | ptr += 2 + offset; |
Herbert Xu | 7a70e39 | 2007-09-18 13:18:42 -0700 | [diff] [blame] | 647 | } |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 648 | |
James Chapman | 9178100 | 2007-11-05 23:32:37 -0800 | [diff] [blame] | 649 | offset = ptr - optr; |
| 650 | if (!pskb_may_pull(skb, offset)) |
| 651 | goto discard; |
| 652 | |
| 653 | __skb_pull(skb, offset); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 654 | |
| 655 | /* Skip PPP header, if present. In testing, Microsoft L2TP clients |
| 656 | * don't send the PPP header (PPP header compression enabled), but |
| 657 | * other clients can include the header. So we cope with both cases |
| 658 | * here. The PPP header is always FF03 when using L2TP. |
| 659 | * |
| 660 | * Note that skb->data[] isn't dereferenced from a u16 ptr here since |
| 661 | * the field may be unaligned. |
| 662 | */ |
James Chapman | 9178100 | 2007-11-05 23:32:37 -0800 | [diff] [blame] | 663 | if (!pskb_may_pull(skb, 2)) |
| 664 | goto discard; |
| 665 | |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 666 | if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03)) |
| 667 | skb_pull(skb, 2); |
| 668 | |
| 669 | /* Prepare skb for adding to the session's reorder_q. Hold |
| 670 | * packets for max reorder_timeout or 1 second if not |
| 671 | * reordering. |
| 672 | */ |
| 673 | PPPOL2TP_SKB_CB(skb)->length = length; |
| 674 | PPPOL2TP_SKB_CB(skb)->expires = jiffies + |
| 675 | (session->reorder_timeout ? session->reorder_timeout : HZ); |
| 676 | |
| 677 | /* Add packet to the session's receive queue. Reordering is done here, if |
| 678 | * enabled. Saved L2TP protocol info is stored in skb->sb[]. |
| 679 | */ |
| 680 | if (PPPOL2TP_SKB_CB(skb)->has_seq) { |
| 681 | if (session->reorder_timeout != 0) { |
| 682 | /* Packet reordering enabled. Add skb to session's |
| 683 | * reorder queue, in order of ns. |
| 684 | */ |
| 685 | pppol2tp_recv_queue_skb(session, skb); |
| 686 | } else { |
| 687 | /* Packet reordering disabled. Discard out-of-sequence |
| 688 | * packets |
| 689 | */ |
| 690 | if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) { |
| 691 | session->stats.rx_seq_discards++; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 692 | PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, |
| 693 | "%s: oos pkt %hu len %d discarded, " |
| 694 | "waiting for %hu, reorder_q_len=%d\n", |
| 695 | session->name, PPPOL2TP_SKB_CB(skb)->ns, |
| 696 | PPPOL2TP_SKB_CB(skb)->length, session->nr, |
| 697 | skb_queue_len(&session->reorder_q)); |
| 698 | goto discard; |
| 699 | } |
| 700 | skb_queue_tail(&session->reorder_q, skb); |
| 701 | } |
| 702 | } else { |
| 703 | /* No sequence numbers. Add the skb to the tail of the |
| 704 | * reorder queue. This ensures that it will be |
| 705 | * delivered after all previous sequenced skbs. |
| 706 | */ |
| 707 | skb_queue_tail(&session->reorder_q, skb); |
| 708 | } |
| 709 | |
| 710 | /* Try to dequeue as many skbs from reorder_q as we can. */ |
| 711 | pppol2tp_recv_dequeue(session); |
| 712 | |
| 713 | return 0; |
| 714 | |
| 715 | discard: |
Herbert Xu | 7a70e39 | 2007-09-18 13:18:42 -0700 | [diff] [blame] | 716 | session->stats.rx_errors++; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 717 | kfree_skb(skb); |
| 718 | sock_put(session->sock); |
| 719 | |
| 720 | return 0; |
| 721 | |
| 722 | error: |
James Chapman | 9178100 | 2007-11-05 23:32:37 -0800 | [diff] [blame] | 723 | /* Put UDP header back */ |
| 724 | __skb_push(skb, sizeof(struct udphdr)); |
| 725 | |
| 726 | no_tunnel: |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 727 | return 1; |
| 728 | } |
| 729 | |
| 730 | /* UDP encapsulation receive handler. See net/ipv4/udp.c. |
| 731 | * Return codes: |
| 732 | * 0 : success. |
| 733 | * <0: error |
| 734 | * >0: skb should be passed up to userspace as UDP. |
| 735 | */ |
| 736 | static int pppol2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) |
| 737 | { |
| 738 | struct pppol2tp_tunnel *tunnel; |
| 739 | |
| 740 | tunnel = pppol2tp_sock_to_tunnel(sk); |
| 741 | if (tunnel == NULL) |
| 742 | goto pass_up; |
| 743 | |
| 744 | PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, |
| 745 | "%s: received %d bytes\n", tunnel->name, skb->len); |
| 746 | |
| 747 | if (pppol2tp_recv_core(sk, skb)) |
| 748 | goto pass_up; |
| 749 | |
| 750 | return 0; |
| 751 | |
| 752 | pass_up: |
| 753 | return 1; |
| 754 | } |
| 755 | |
| 756 | /* Receive message. This is the recvmsg for the PPPoL2TP socket. |
| 757 | */ |
| 758 | static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 759 | struct msghdr *msg, size_t len, |
| 760 | int flags) |
| 761 | { |
| 762 | int err; |
| 763 | struct sk_buff *skb; |
| 764 | struct sock *sk = sock->sk; |
| 765 | |
| 766 | err = -EIO; |
| 767 | if (sk->sk_state & PPPOX_BOUND) |
| 768 | goto end; |
| 769 | |
| 770 | msg->msg_namelen = 0; |
| 771 | |
| 772 | err = 0; |
| 773 | skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, |
| 774 | flags & MSG_DONTWAIT, &err); |
| 775 | if (skb) { |
| 776 | err = memcpy_toiovec(msg->msg_iov, (unsigned char *) skb->data, |
| 777 | skb->len); |
| 778 | if (err < 0) |
| 779 | goto do_skb_free; |
| 780 | err = skb->len; |
| 781 | } |
| 782 | do_skb_free: |
| 783 | kfree_skb(skb); |
| 784 | end: |
| 785 | return err; |
| 786 | } |
| 787 | |
| 788 | /************************************************************************ |
| 789 | * Transmit handling |
| 790 | ***********************************************************************/ |
| 791 | |
| 792 | /* Tell how big L2TP headers are for a particular session. This |
| 793 | * depends on whether sequence numbers are being used. |
| 794 | */ |
| 795 | static inline int pppol2tp_l2tp_header_len(struct pppol2tp_session *session) |
| 796 | { |
| 797 | if (session->send_seq) |
| 798 | return PPPOL2TP_L2TP_HDR_SIZE_SEQ; |
| 799 | |
| 800 | return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ; |
| 801 | } |
| 802 | |
| 803 | /* Build an L2TP header for the session into the buffer provided. |
| 804 | */ |
| 805 | static void pppol2tp_build_l2tp_header(struct pppol2tp_session *session, |
| 806 | void *buf) |
| 807 | { |
| 808 | __be16 *bufp = buf; |
| 809 | u16 flags = L2TP_HDR_VER; |
| 810 | |
| 811 | if (session->send_seq) |
| 812 | flags |= L2TP_HDRFLAG_S; |
| 813 | |
| 814 | /* Setup L2TP header. |
| 815 | * FIXME: Can this ever be unaligned? Is direct dereferencing of |
| 816 | * 16-bit header fields safe here for all architectures? |
| 817 | */ |
| 818 | *bufp++ = htons(flags); |
| 819 | *bufp++ = htons(session->tunnel_addr.d_tunnel); |
| 820 | *bufp++ = htons(session->tunnel_addr.d_session); |
| 821 | if (session->send_seq) { |
| 822 | *bufp++ = htons(session->ns); |
| 823 | *bufp++ = 0; |
| 824 | session->ns++; |
| 825 | PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, |
| 826 | "%s: updated ns to %hu\n", session->name, session->ns); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here |
| 831 | * when a user application does a sendmsg() on the session socket. L2TP and |
| 832 | * PPP headers must be inserted into the user's data. |
| 833 | */ |
| 834 | static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m, |
| 835 | size_t total_len) |
| 836 | { |
| 837 | static const unsigned char ppph[2] = { 0xff, 0x03 }; |
| 838 | struct sock *sk = sock->sk; |
| 839 | struct inet_sock *inet; |
| 840 | __wsum csum = 0; |
| 841 | struct sk_buff *skb; |
| 842 | int error; |
| 843 | int hdr_len; |
| 844 | struct pppol2tp_session *session; |
| 845 | struct pppol2tp_tunnel *tunnel; |
| 846 | struct udphdr *uh; |
Patrick McHardy | 7d4372b | 2007-07-18 02:04:09 -0700 | [diff] [blame] | 847 | unsigned int len; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 848 | |
| 849 | error = -ENOTCONN; |
| 850 | if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) |
| 851 | goto error; |
| 852 | |
| 853 | /* Get session and tunnel contexts */ |
| 854 | error = -EBADF; |
| 855 | session = pppol2tp_sock_to_session(sk); |
| 856 | if (session == NULL) |
| 857 | goto error; |
| 858 | |
| 859 | tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock); |
| 860 | if (tunnel == NULL) |
| 861 | goto error; |
| 862 | |
| 863 | /* What header length is configured for this session? */ |
| 864 | hdr_len = pppol2tp_l2tp_header_len(session); |
| 865 | |
| 866 | /* Allocate a socket buffer */ |
| 867 | error = -ENOMEM; |
| 868 | skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) + |
| 869 | sizeof(struct udphdr) + hdr_len + |
| 870 | sizeof(ppph) + total_len, |
| 871 | 0, GFP_KERNEL); |
| 872 | if (!skb) |
| 873 | goto error; |
| 874 | |
| 875 | /* Reserve space for headers. */ |
| 876 | skb_reserve(skb, NET_SKB_PAD); |
| 877 | skb_reset_network_header(skb); |
| 878 | skb_reserve(skb, sizeof(struct iphdr)); |
| 879 | skb_reset_transport_header(skb); |
| 880 | |
| 881 | /* Build UDP header */ |
| 882 | inet = inet_sk(session->tunnel_sock); |
| 883 | uh = (struct udphdr *) skb->data; |
| 884 | uh->source = inet->sport; |
| 885 | uh->dest = inet->dport; |
| 886 | uh->len = htons(hdr_len + sizeof(ppph) + total_len); |
| 887 | uh->check = 0; |
| 888 | skb_put(skb, sizeof(struct udphdr)); |
| 889 | |
| 890 | /* Build L2TP header */ |
| 891 | pppol2tp_build_l2tp_header(session, skb->data); |
| 892 | skb_put(skb, hdr_len); |
| 893 | |
| 894 | /* Add PPP header */ |
| 895 | skb->data[0] = ppph[0]; |
| 896 | skb->data[1] = ppph[1]; |
| 897 | skb_put(skb, 2); |
| 898 | |
| 899 | /* Copy user data into skb */ |
| 900 | error = memcpy_fromiovec(skb->data, m->msg_iov, total_len); |
| 901 | if (error < 0) { |
| 902 | kfree_skb(skb); |
| 903 | goto error; |
| 904 | } |
| 905 | skb_put(skb, total_len); |
| 906 | |
| 907 | /* Calculate UDP checksum if configured to do so */ |
| 908 | if (session->tunnel_sock->sk_no_check != UDP_CSUM_NOXMIT) |
| 909 | csum = udp_csum_outgoing(sk, skb); |
| 910 | |
| 911 | /* Debug */ |
| 912 | if (session->send_seq) |
| 913 | PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, |
David S. Miller | 38d15b6 | 2007-06-27 15:52:25 -0700 | [diff] [blame] | 914 | "%s: send %Zd bytes, ns=%hu\n", session->name, |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 915 | total_len, session->ns - 1); |
| 916 | else |
| 917 | PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, |
David S. Miller | 38d15b6 | 2007-06-27 15:52:25 -0700 | [diff] [blame] | 918 | "%s: send %Zd bytes\n", session->name, total_len); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 919 | |
| 920 | if (session->debug & PPPOL2TP_MSG_DATA) { |
| 921 | int i; |
| 922 | unsigned char *datap = skb->data; |
| 923 | |
| 924 | printk(KERN_DEBUG "%s: xmit:", session->name); |
| 925 | for (i = 0; i < total_len; i++) { |
| 926 | printk(" %02X", *datap++); |
| 927 | if (i == 15) { |
| 928 | printk(" ..."); |
| 929 | break; |
| 930 | } |
| 931 | } |
| 932 | printk("\n"); |
| 933 | } |
| 934 | |
| 935 | /* Queue the packet to IP for output */ |
Patrick McHardy | 7d4372b | 2007-07-18 02:04:09 -0700 | [diff] [blame] | 936 | len = skb->len; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 937 | error = ip_queue_xmit(skb, 1); |
| 938 | |
| 939 | /* Update stats */ |
| 940 | if (error >= 0) { |
| 941 | tunnel->stats.tx_packets++; |
Patrick McHardy | 7d4372b | 2007-07-18 02:04:09 -0700 | [diff] [blame] | 942 | tunnel->stats.tx_bytes += len; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 943 | session->stats.tx_packets++; |
Patrick McHardy | 7d4372b | 2007-07-18 02:04:09 -0700 | [diff] [blame] | 944 | session->stats.tx_bytes += len; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 945 | } else { |
| 946 | tunnel->stats.tx_errors++; |
| 947 | session->stats.tx_errors++; |
| 948 | } |
| 949 | |
| 950 | error: |
| 951 | return error; |
| 952 | } |
| 953 | |
| 954 | /* Transmit function called by generic PPP driver. Sends PPP frame |
| 955 | * over PPPoL2TP socket. |
| 956 | * |
| 957 | * This is almost the same as pppol2tp_sendmsg(), but rather than |
| 958 | * being called with a msghdr from userspace, it is called with a skb |
| 959 | * from the kernel. |
| 960 | * |
| 961 | * The supplied skb from ppp doesn't have enough headroom for the |
| 962 | * insertion of L2TP, UDP and IP headers so we need to allocate more |
| 963 | * headroom in the skb. This will create a cloned skb. But we must be |
| 964 | * careful in the error case because the caller will expect to free |
| 965 | * the skb it supplied, not our cloned skb. So we take care to always |
| 966 | * leave the original skb unfreed if we return an error. |
| 967 | */ |
| 968 | static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb) |
| 969 | { |
| 970 | static const u8 ppph[2] = { 0xff, 0x03 }; |
| 971 | struct sock *sk = (struct sock *) chan->private; |
| 972 | struct sock *sk_tun; |
| 973 | int hdr_len; |
| 974 | struct pppol2tp_session *session; |
| 975 | struct pppol2tp_tunnel *tunnel; |
| 976 | int rc; |
| 977 | int headroom; |
| 978 | int data_len = skb->len; |
| 979 | struct inet_sock *inet; |
| 980 | __wsum csum = 0; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 981 | struct udphdr *uh; |
Patrick McHardy | 7d4372b | 2007-07-18 02:04:09 -0700 | [diff] [blame] | 982 | unsigned int len; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 983 | |
| 984 | if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) |
| 985 | goto abort; |
| 986 | |
| 987 | /* Get session and tunnel contexts from the socket */ |
| 988 | session = pppol2tp_sock_to_session(sk); |
| 989 | if (session == NULL) |
| 990 | goto abort; |
| 991 | |
| 992 | sk_tun = session->tunnel_sock; |
| 993 | if (sk_tun == NULL) |
| 994 | goto abort; |
| 995 | tunnel = pppol2tp_sock_to_tunnel(sk_tun); |
| 996 | if (tunnel == NULL) |
| 997 | goto abort; |
| 998 | |
| 999 | /* What header length is configured for this session? */ |
| 1000 | hdr_len = pppol2tp_l2tp_header_len(session); |
| 1001 | |
| 1002 | /* Check that there's enough headroom in the skb to insert IP, |
| 1003 | * UDP and L2TP and PPP headers. If not enough, expand it to |
| 1004 | * make room. Note that a new skb (or a clone) is |
| 1005 | * allocated. If we return an error from this point on, make |
| 1006 | * sure we free the new skb but do not free the original skb |
| 1007 | * since that is done by the caller for the error case. |
| 1008 | */ |
| 1009 | headroom = NET_SKB_PAD + sizeof(struct iphdr) + |
| 1010 | sizeof(struct udphdr) + hdr_len + sizeof(ppph); |
Herbert Xu | f3d5e3a | 2007-09-19 10:46:28 -0700 | [diff] [blame] | 1011 | if (skb_cow_head(skb, headroom)) |
| 1012 | goto abort; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1013 | |
| 1014 | /* Setup PPP header */ |
Herbert Xu | f3d5e3a | 2007-09-19 10:46:28 -0700 | [diff] [blame] | 1015 | __skb_push(skb, sizeof(ppph)); |
| 1016 | skb->data[0] = ppph[0]; |
| 1017 | skb->data[1] = ppph[1]; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1018 | |
| 1019 | /* Setup L2TP header */ |
Herbert Xu | f3d5e3a | 2007-09-19 10:46:28 -0700 | [diff] [blame] | 1020 | pppol2tp_build_l2tp_header(session, __skb_push(skb, hdr_len)); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1021 | |
| 1022 | /* Setup UDP header */ |
| 1023 | inet = inet_sk(sk_tun); |
Herbert Xu | f3d5e3a | 2007-09-19 10:46:28 -0700 | [diff] [blame] | 1024 | __skb_push(skb, sizeof(*uh)); |
| 1025 | skb_reset_transport_header(skb); |
| 1026 | uh = udp_hdr(skb); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1027 | uh->source = inet->sport; |
| 1028 | uh->dest = inet->dport; |
| 1029 | uh->len = htons(sizeof(struct udphdr) + hdr_len + sizeof(ppph) + data_len); |
| 1030 | uh->check = 0; |
| 1031 | |
Herbert Xu | f3d5e3a | 2007-09-19 10:46:28 -0700 | [diff] [blame] | 1032 | /* *BROKEN* Calculate UDP checksum if configured to do so */ |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1033 | if (sk_tun->sk_no_check != UDP_CSUM_NOXMIT) |
Herbert Xu | f3d5e3a | 2007-09-19 10:46:28 -0700 | [diff] [blame] | 1034 | csum = udp_csum_outgoing(sk_tun, skb); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1035 | |
| 1036 | /* Debug */ |
| 1037 | if (session->send_seq) |
| 1038 | PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, |
| 1039 | "%s: send %d bytes, ns=%hu\n", session->name, |
| 1040 | data_len, session->ns - 1); |
| 1041 | else |
| 1042 | PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, |
| 1043 | "%s: send %d bytes\n", session->name, data_len); |
| 1044 | |
| 1045 | if (session->debug & PPPOL2TP_MSG_DATA) { |
| 1046 | int i; |
Herbert Xu | f3d5e3a | 2007-09-19 10:46:28 -0700 | [diff] [blame] | 1047 | unsigned char *datap = skb->data; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1048 | |
| 1049 | printk(KERN_DEBUG "%s: xmit:", session->name); |
| 1050 | for (i = 0; i < data_len; i++) { |
| 1051 | printk(" %02X", *datap++); |
| 1052 | if (i == 31) { |
| 1053 | printk(" ..."); |
| 1054 | break; |
| 1055 | } |
| 1056 | } |
| 1057 | printk("\n"); |
| 1058 | } |
| 1059 | |
Herbert Xu | f3d5e3a | 2007-09-19 10:46:28 -0700 | [diff] [blame] | 1060 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
| 1061 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | |
| 1062 | IPSKB_REROUTED); |
| 1063 | nf_reset(skb); |
Patrick McHardy | f77ae93 | 2007-07-18 02:04:39 -0700 | [diff] [blame] | 1064 | |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1065 | /* Get routing info from the tunnel socket */ |
Herbert Xu | f3d5e3a | 2007-09-19 10:46:28 -0700 | [diff] [blame] | 1066 | dst_release(skb->dst); |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 1067 | skb->dst = dst_clone(__sk_dst_get(sk_tun)); |
James Chapman | 9178100 | 2007-11-05 23:32:37 -0800 | [diff] [blame] | 1068 | skb_orphan(skb); |
| 1069 | skb->sk = sk_tun; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1070 | |
| 1071 | /* Queue the packet to IP for output */ |
Herbert Xu | f3d5e3a | 2007-09-19 10:46:28 -0700 | [diff] [blame] | 1072 | len = skb->len; |
| 1073 | rc = ip_queue_xmit(skb, 1); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1074 | |
| 1075 | /* Update stats */ |
| 1076 | if (rc >= 0) { |
| 1077 | tunnel->stats.tx_packets++; |
Patrick McHardy | 7d4372b | 2007-07-18 02:04:09 -0700 | [diff] [blame] | 1078 | tunnel->stats.tx_bytes += len; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1079 | session->stats.tx_packets++; |
Patrick McHardy | 7d4372b | 2007-07-18 02:04:09 -0700 | [diff] [blame] | 1080 | session->stats.tx_bytes += len; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1081 | } else { |
| 1082 | tunnel->stats.tx_errors++; |
| 1083 | session->stats.tx_errors++; |
| 1084 | } |
| 1085 | |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1086 | return 1; |
| 1087 | |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1088 | abort: |
Herbert Xu | f3d5e3a | 2007-09-19 10:46:28 -0700 | [diff] [blame] | 1089 | /* Free the original skb */ |
| 1090 | kfree_skb(skb); |
| 1091 | return 1; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /***************************************************************************** |
| 1095 | * Session (and tunnel control) socket create/destroy. |
| 1096 | *****************************************************************************/ |
| 1097 | |
| 1098 | /* When the tunnel UDP socket is closed, all the attached sockets need to go |
| 1099 | * too. |
| 1100 | */ |
| 1101 | static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel *tunnel) |
| 1102 | { |
| 1103 | int hash; |
| 1104 | struct hlist_node *walk; |
| 1105 | struct hlist_node *tmp; |
| 1106 | struct pppol2tp_session *session; |
| 1107 | struct sock *sk; |
| 1108 | |
| 1109 | if (tunnel == NULL) |
| 1110 | BUG(); |
| 1111 | |
| 1112 | PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1113 | "%s: closing all sessions...\n", tunnel->name); |
| 1114 | |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 1115 | write_lock_bh(&tunnel->hlist_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1116 | for (hash = 0; hash < PPPOL2TP_HASH_SIZE; hash++) { |
| 1117 | again: |
| 1118 | hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { |
Jarek Poplawski | ec9b6ad | 2008-03-03 20:49:34 -0800 | [diff] [blame] | 1119 | struct sk_buff *skb; |
| 1120 | |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1121 | session = hlist_entry(walk, struct pppol2tp_session, hlist); |
| 1122 | |
| 1123 | sk = session->sock; |
| 1124 | |
| 1125 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1126 | "%s: closing session\n", session->name); |
| 1127 | |
| 1128 | hlist_del_init(&session->hlist); |
| 1129 | |
| 1130 | /* Since we should hold the sock lock while |
| 1131 | * doing any unbinding, we need to release the |
| 1132 | * lock we're holding before taking that lock. |
| 1133 | * Hold a reference to the sock so it doesn't |
| 1134 | * disappear as we're jumping between locks. |
| 1135 | */ |
| 1136 | sock_hold(sk); |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 1137 | write_unlock_bh(&tunnel->hlist_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1138 | lock_sock(sk); |
| 1139 | |
| 1140 | if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) { |
| 1141 | pppox_unbind_sock(sk); |
| 1142 | sk->sk_state = PPPOX_DEAD; |
| 1143 | sk->sk_state_change(sk); |
| 1144 | } |
| 1145 | |
| 1146 | /* Purge any queued data */ |
| 1147 | skb_queue_purge(&sk->sk_receive_queue); |
| 1148 | skb_queue_purge(&sk->sk_write_queue); |
Jarek Poplawski | ec9b6ad | 2008-03-03 20:49:34 -0800 | [diff] [blame] | 1149 | while ((skb = skb_dequeue(&session->reorder_q))) { |
| 1150 | kfree_skb(skb); |
| 1151 | sock_put(sk); |
| 1152 | } |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1153 | |
| 1154 | release_sock(sk); |
| 1155 | sock_put(sk); |
| 1156 | |
| 1157 | /* Now restart from the beginning of this hash |
| 1158 | * chain. We always remove a session from the |
| 1159 | * list so we are guaranteed to make forward |
| 1160 | * progress. |
| 1161 | */ |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 1162 | write_lock_bh(&tunnel->hlist_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1163 | goto again; |
| 1164 | } |
| 1165 | } |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 1166 | write_unlock_bh(&tunnel->hlist_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /* Really kill the tunnel. |
| 1170 | * Come here only when all sessions have been cleared from the tunnel. |
| 1171 | */ |
| 1172 | static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel) |
| 1173 | { |
| 1174 | /* Remove from socket list */ |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 1175 | write_lock_bh(&pppol2tp_tunnel_list_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1176 | list_del_init(&tunnel->list); |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 1177 | write_unlock_bh(&pppol2tp_tunnel_list_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1178 | |
| 1179 | atomic_dec(&pppol2tp_tunnel_count); |
| 1180 | kfree(tunnel); |
| 1181 | } |
| 1182 | |
| 1183 | /* Tunnel UDP socket destruct hook. |
| 1184 | * The tunnel context is deleted only when all session sockets have been |
| 1185 | * closed. |
| 1186 | */ |
| 1187 | static void pppol2tp_tunnel_destruct(struct sock *sk) |
| 1188 | { |
| 1189 | struct pppol2tp_tunnel *tunnel; |
| 1190 | |
| 1191 | tunnel = pppol2tp_sock_to_tunnel(sk); |
| 1192 | if (tunnel == NULL) |
| 1193 | goto end; |
| 1194 | |
| 1195 | PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1196 | "%s: closing...\n", tunnel->name); |
| 1197 | |
| 1198 | /* Close all sessions */ |
| 1199 | pppol2tp_tunnel_closeall(tunnel); |
| 1200 | |
| 1201 | /* No longer an encapsulation socket. See net/ipv4/udp.c */ |
| 1202 | (udp_sk(sk))->encap_type = 0; |
| 1203 | (udp_sk(sk))->encap_rcv = NULL; |
| 1204 | |
| 1205 | /* Remove hooks into tunnel socket */ |
| 1206 | tunnel->sock = NULL; |
| 1207 | sk->sk_destruct = tunnel->old_sk_destruct; |
| 1208 | sk->sk_user_data = NULL; |
| 1209 | |
| 1210 | /* Call original (UDP) socket descructor */ |
| 1211 | if (sk->sk_destruct != NULL) |
| 1212 | (*sk->sk_destruct)(sk); |
| 1213 | |
| 1214 | pppol2tp_tunnel_dec_refcount(tunnel); |
| 1215 | |
| 1216 | end: |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | /* Really kill the session socket. (Called from sock_put() if |
| 1221 | * refcnt == 0.) |
| 1222 | */ |
| 1223 | static void pppol2tp_session_destruct(struct sock *sk) |
| 1224 | { |
| 1225 | struct pppol2tp_session *session = NULL; |
| 1226 | |
| 1227 | if (sk->sk_user_data != NULL) { |
| 1228 | struct pppol2tp_tunnel *tunnel; |
| 1229 | |
| 1230 | session = pppol2tp_sock_to_session(sk); |
| 1231 | if (session == NULL) |
| 1232 | goto out; |
| 1233 | |
| 1234 | /* Don't use pppol2tp_sock_to_tunnel() here to |
| 1235 | * get the tunnel context because the tunnel |
| 1236 | * socket might have already been closed (its |
| 1237 | * sk->sk_user_data will be NULL) so use the |
| 1238 | * session's private tunnel ptr instead. |
| 1239 | */ |
| 1240 | tunnel = session->tunnel; |
| 1241 | if (tunnel != NULL) { |
| 1242 | BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); |
| 1243 | |
| 1244 | /* If session_id is zero, this is a null |
| 1245 | * session context, which was created for a |
| 1246 | * socket that is being used only to manage |
| 1247 | * tunnels. |
| 1248 | */ |
| 1249 | if (session->tunnel_addr.s_session != 0) { |
| 1250 | /* Delete the session socket from the |
| 1251 | * hash |
| 1252 | */ |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 1253 | write_lock_bh(&tunnel->hlist_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1254 | hlist_del_init(&session->hlist); |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 1255 | write_unlock_bh(&tunnel->hlist_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1256 | |
| 1257 | atomic_dec(&pppol2tp_session_count); |
| 1258 | } |
| 1259 | |
| 1260 | /* This will delete the tunnel context if this |
| 1261 | * is the last session on the tunnel. |
| 1262 | */ |
| 1263 | session->tunnel = NULL; |
| 1264 | session->tunnel_sock = NULL; |
| 1265 | pppol2tp_tunnel_dec_refcount(tunnel); |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | kfree(session); |
| 1270 | out: |
| 1271 | return; |
| 1272 | } |
| 1273 | |
| 1274 | /* Called when the PPPoX socket (session) is closed. |
| 1275 | */ |
| 1276 | static int pppol2tp_release(struct socket *sock) |
| 1277 | { |
| 1278 | struct sock *sk = sock->sk; |
| 1279 | int error; |
| 1280 | |
| 1281 | if (!sk) |
| 1282 | return 0; |
| 1283 | |
| 1284 | error = -EBADF; |
| 1285 | lock_sock(sk); |
| 1286 | if (sock_flag(sk, SOCK_DEAD) != 0) |
| 1287 | goto error; |
| 1288 | |
| 1289 | pppox_unbind_sock(sk); |
| 1290 | |
| 1291 | /* Signal the death of the socket. */ |
| 1292 | sk->sk_state = PPPOX_DEAD; |
| 1293 | sock_orphan(sk); |
| 1294 | sock->sk = NULL; |
| 1295 | |
| 1296 | /* Purge any queued data */ |
| 1297 | skb_queue_purge(&sk->sk_receive_queue); |
| 1298 | skb_queue_purge(&sk->sk_write_queue); |
| 1299 | |
| 1300 | release_sock(sk); |
| 1301 | |
| 1302 | /* This will delete the session context via |
| 1303 | * pppol2tp_session_destruct() if the socket's refcnt drops to |
| 1304 | * zero. |
| 1305 | */ |
| 1306 | sock_put(sk); |
| 1307 | |
| 1308 | return 0; |
| 1309 | |
| 1310 | error: |
| 1311 | release_sock(sk); |
| 1312 | return error; |
| 1313 | } |
| 1314 | |
| 1315 | /* Internal function to prepare a tunnel (UDP) socket to have PPPoX |
| 1316 | * sockets attached to it. |
| 1317 | */ |
| 1318 | static struct sock *pppol2tp_prepare_tunnel_socket(int fd, u16 tunnel_id, |
| 1319 | int *error) |
| 1320 | { |
| 1321 | int err; |
| 1322 | struct socket *sock = NULL; |
| 1323 | struct sock *sk; |
| 1324 | struct pppol2tp_tunnel *tunnel; |
| 1325 | struct sock *ret = NULL; |
| 1326 | |
| 1327 | /* Get the tunnel UDP socket from the fd, which was opened by |
| 1328 | * the userspace L2TP daemon. |
| 1329 | */ |
| 1330 | err = -EBADF; |
| 1331 | sock = sockfd_lookup(fd, &err); |
| 1332 | if (!sock) { |
| 1333 | PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR, |
| 1334 | "tunl %hu: sockfd_lookup(fd=%d) returned %d\n", |
| 1335 | tunnel_id, fd, err); |
| 1336 | goto err; |
| 1337 | } |
| 1338 | |
Herbert Xu | a14d6ab | 2007-09-18 13:18:17 -0700 | [diff] [blame] | 1339 | sk = sock->sk; |
| 1340 | |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1341 | /* Quick sanity checks */ |
Herbert Xu | a14d6ab | 2007-09-18 13:18:17 -0700 | [diff] [blame] | 1342 | err = -EPROTONOSUPPORT; |
| 1343 | if (sk->sk_protocol != IPPROTO_UDP) { |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1344 | PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR, |
Herbert Xu | a14d6ab | 2007-09-18 13:18:17 -0700 | [diff] [blame] | 1345 | "tunl %hu: fd %d wrong protocol, got %d, expected %d\n", |
| 1346 | tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1347 | goto err; |
| 1348 | } |
| 1349 | err = -EAFNOSUPPORT; |
| 1350 | if (sock->ops->family != AF_INET) { |
| 1351 | PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR, |
| 1352 | "tunl %hu: fd %d wrong family, got %d, expected %d\n", |
| 1353 | tunnel_id, fd, sock->ops->family, AF_INET); |
| 1354 | goto err; |
| 1355 | } |
| 1356 | |
| 1357 | err = -ENOTCONN; |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1358 | |
| 1359 | /* Check if this socket has already been prepped */ |
| 1360 | tunnel = (struct pppol2tp_tunnel *)sk->sk_user_data; |
| 1361 | if (tunnel != NULL) { |
| 1362 | /* User-data field already set */ |
| 1363 | err = -EBUSY; |
| 1364 | BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); |
| 1365 | |
| 1366 | /* This socket has already been prepped */ |
| 1367 | ret = tunnel->sock; |
| 1368 | goto out; |
| 1369 | } |
| 1370 | |
| 1371 | /* This socket is available and needs prepping. Create a new tunnel |
| 1372 | * context and init it. |
| 1373 | */ |
| 1374 | sk->sk_user_data = tunnel = kzalloc(sizeof(struct pppol2tp_tunnel), GFP_KERNEL); |
| 1375 | if (sk->sk_user_data == NULL) { |
| 1376 | err = -ENOMEM; |
| 1377 | goto err; |
| 1378 | } |
| 1379 | |
| 1380 | tunnel->magic = L2TP_TUNNEL_MAGIC; |
| 1381 | sprintf(&tunnel->name[0], "tunl %hu", tunnel_id); |
| 1382 | |
| 1383 | tunnel->stats.tunnel_id = tunnel_id; |
| 1384 | tunnel->debug = PPPOL2TP_DEFAULT_DEBUG_FLAGS; |
| 1385 | |
| 1386 | /* Hook on the tunnel socket destructor so that we can cleanup |
| 1387 | * if the tunnel socket goes away. |
| 1388 | */ |
| 1389 | tunnel->old_sk_destruct = sk->sk_destruct; |
| 1390 | sk->sk_destruct = &pppol2tp_tunnel_destruct; |
| 1391 | |
| 1392 | tunnel->sock = sk; |
| 1393 | sk->sk_allocation = GFP_ATOMIC; |
| 1394 | |
| 1395 | /* Misc init */ |
| 1396 | rwlock_init(&tunnel->hlist_lock); |
| 1397 | |
| 1398 | /* Add tunnel to our list */ |
| 1399 | INIT_LIST_HEAD(&tunnel->list); |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 1400 | write_lock_bh(&pppol2tp_tunnel_list_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1401 | list_add(&tunnel->list, &pppol2tp_tunnel_list); |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 1402 | write_unlock_bh(&pppol2tp_tunnel_list_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1403 | atomic_inc(&pppol2tp_tunnel_count); |
| 1404 | |
| 1405 | /* Bump the reference count. The tunnel context is deleted |
| 1406 | * only when this drops to zero. |
| 1407 | */ |
| 1408 | pppol2tp_tunnel_inc_refcount(tunnel); |
| 1409 | |
| 1410 | /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ |
| 1411 | (udp_sk(sk))->encap_type = UDP_ENCAP_L2TPINUDP; |
| 1412 | (udp_sk(sk))->encap_rcv = pppol2tp_udp_encap_recv; |
| 1413 | |
| 1414 | ret = tunnel->sock; |
| 1415 | |
| 1416 | *error = 0; |
| 1417 | out: |
| 1418 | if (sock) |
| 1419 | sockfd_put(sock); |
| 1420 | |
| 1421 | return ret; |
| 1422 | |
| 1423 | err: |
| 1424 | *error = err; |
| 1425 | goto out; |
| 1426 | } |
| 1427 | |
| 1428 | static struct proto pppol2tp_sk_proto = { |
| 1429 | .name = "PPPOL2TP", |
| 1430 | .owner = THIS_MODULE, |
| 1431 | .obj_size = sizeof(struct pppox_sock), |
| 1432 | }; |
| 1433 | |
| 1434 | /* socket() handler. Initialize a new struct sock. |
| 1435 | */ |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 1436 | static int pppol2tp_create(struct net *net, struct socket *sock) |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1437 | { |
| 1438 | int error = -ENOMEM; |
| 1439 | struct sock *sk; |
| 1440 | |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 1441 | sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1442 | if (!sk) |
| 1443 | goto out; |
| 1444 | |
| 1445 | sock_init_data(sock, sk); |
| 1446 | |
| 1447 | sock->state = SS_UNCONNECTED; |
| 1448 | sock->ops = &pppol2tp_ops; |
| 1449 | |
| 1450 | sk->sk_backlog_rcv = pppol2tp_recv_core; |
| 1451 | sk->sk_protocol = PX_PROTO_OL2TP; |
| 1452 | sk->sk_family = PF_PPPOX; |
| 1453 | sk->sk_state = PPPOX_NONE; |
| 1454 | sk->sk_type = SOCK_STREAM; |
| 1455 | sk->sk_destruct = pppol2tp_session_destruct; |
| 1456 | |
| 1457 | error = 0; |
| 1458 | |
| 1459 | out: |
| 1460 | return error; |
| 1461 | } |
| 1462 | |
| 1463 | /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket |
| 1464 | */ |
| 1465 | static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, |
| 1466 | int sockaddr_len, int flags) |
| 1467 | { |
| 1468 | struct sock *sk = sock->sk; |
| 1469 | struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr; |
| 1470 | struct pppox_sock *po = pppox_sk(sk); |
| 1471 | struct sock *tunnel_sock = NULL; |
| 1472 | struct pppol2tp_session *session = NULL; |
| 1473 | struct pppol2tp_tunnel *tunnel; |
| 1474 | struct dst_entry *dst; |
| 1475 | int error = 0; |
| 1476 | |
| 1477 | lock_sock(sk); |
| 1478 | |
| 1479 | error = -EINVAL; |
| 1480 | if (sp->sa_protocol != PX_PROTO_OL2TP) |
| 1481 | goto end; |
| 1482 | |
| 1483 | /* Check for already bound sockets */ |
| 1484 | error = -EBUSY; |
| 1485 | if (sk->sk_state & PPPOX_CONNECTED) |
| 1486 | goto end; |
| 1487 | |
| 1488 | /* We don't supporting rebinding anyway */ |
| 1489 | error = -EALREADY; |
| 1490 | if (sk->sk_user_data) |
| 1491 | goto end; /* socket is already attached */ |
| 1492 | |
| 1493 | /* Don't bind if s_tunnel is 0 */ |
| 1494 | error = -EINVAL; |
| 1495 | if (sp->pppol2tp.s_tunnel == 0) |
| 1496 | goto end; |
| 1497 | |
| 1498 | /* Special case: prepare tunnel socket if s_session and |
| 1499 | * d_session is 0. Otherwise look up tunnel using supplied |
| 1500 | * tunnel id. |
| 1501 | */ |
| 1502 | if ((sp->pppol2tp.s_session == 0) && (sp->pppol2tp.d_session == 0)) { |
| 1503 | tunnel_sock = pppol2tp_prepare_tunnel_socket(sp->pppol2tp.fd, |
| 1504 | sp->pppol2tp.s_tunnel, |
| 1505 | &error); |
| 1506 | if (tunnel_sock == NULL) |
| 1507 | goto end; |
| 1508 | |
| 1509 | tunnel = tunnel_sock->sk_user_data; |
| 1510 | } else { |
| 1511 | tunnel = pppol2tp_tunnel_find(sp->pppol2tp.s_tunnel); |
| 1512 | |
| 1513 | /* Error if we can't find the tunnel */ |
| 1514 | error = -ENOENT; |
| 1515 | if (tunnel == NULL) |
| 1516 | goto end; |
| 1517 | |
| 1518 | tunnel_sock = tunnel->sock; |
| 1519 | } |
| 1520 | |
| 1521 | /* Check that this session doesn't already exist */ |
| 1522 | error = -EEXIST; |
| 1523 | session = pppol2tp_session_find(tunnel, sp->pppol2tp.s_session); |
| 1524 | if (session != NULL) |
| 1525 | goto end; |
| 1526 | |
| 1527 | /* Allocate and initialize a new session context. */ |
| 1528 | session = kzalloc(sizeof(struct pppol2tp_session), GFP_KERNEL); |
| 1529 | if (session == NULL) { |
| 1530 | error = -ENOMEM; |
| 1531 | goto end; |
| 1532 | } |
| 1533 | |
| 1534 | skb_queue_head_init(&session->reorder_q); |
| 1535 | |
| 1536 | session->magic = L2TP_SESSION_MAGIC; |
| 1537 | session->owner = current->pid; |
| 1538 | session->sock = sk; |
| 1539 | session->tunnel = tunnel; |
| 1540 | session->tunnel_sock = tunnel_sock; |
| 1541 | session->tunnel_addr = sp->pppol2tp; |
| 1542 | sprintf(&session->name[0], "sess %hu/%hu", |
| 1543 | session->tunnel_addr.s_tunnel, |
| 1544 | session->tunnel_addr.s_session); |
| 1545 | |
| 1546 | session->stats.tunnel_id = session->tunnel_addr.s_tunnel; |
| 1547 | session->stats.session_id = session->tunnel_addr.s_session; |
| 1548 | |
| 1549 | INIT_HLIST_NODE(&session->hlist); |
| 1550 | |
| 1551 | /* Inherit debug options from tunnel */ |
| 1552 | session->debug = tunnel->debug; |
| 1553 | |
| 1554 | /* Default MTU must allow space for UDP/L2TP/PPP |
| 1555 | * headers. |
| 1556 | */ |
| 1557 | session->mtu = session->mru = 1500 - PPPOL2TP_HEADER_OVERHEAD; |
| 1558 | |
| 1559 | /* If PMTU discovery was enabled, use the MTU that was discovered */ |
| 1560 | dst = sk_dst_get(sk); |
| 1561 | if (dst != NULL) { |
| 1562 | u32 pmtu = dst_mtu(__sk_dst_get(sk)); |
| 1563 | if (pmtu != 0) |
| 1564 | session->mtu = session->mru = pmtu - |
| 1565 | PPPOL2TP_HEADER_OVERHEAD; |
| 1566 | dst_release(dst); |
| 1567 | } |
| 1568 | |
| 1569 | /* Special case: if source & dest session_id == 0x0000, this socket is |
| 1570 | * being created to manage the tunnel. Don't add the session to the |
| 1571 | * session hash list, just set up the internal context for use by |
| 1572 | * ioctl() and sockopt() handlers. |
| 1573 | */ |
| 1574 | if ((session->tunnel_addr.s_session == 0) && |
| 1575 | (session->tunnel_addr.d_session == 0)) { |
| 1576 | error = 0; |
| 1577 | sk->sk_user_data = session; |
| 1578 | goto out_no_ppp; |
| 1579 | } |
| 1580 | |
| 1581 | /* Get tunnel context from the tunnel socket */ |
| 1582 | tunnel = pppol2tp_sock_to_tunnel(tunnel_sock); |
| 1583 | if (tunnel == NULL) { |
| 1584 | error = -EBADF; |
| 1585 | goto end; |
| 1586 | } |
| 1587 | |
| 1588 | /* Right now, because we don't have a way to push the incoming skb's |
| 1589 | * straight through the UDP layer, the only header we need to worry |
| 1590 | * about is the L2TP header. This size is different depending on |
| 1591 | * whether sequence numbers are enabled for the data channel. |
| 1592 | */ |
| 1593 | po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ; |
| 1594 | |
| 1595 | po->chan.private = sk; |
| 1596 | po->chan.ops = &pppol2tp_chan_ops; |
| 1597 | po->chan.mtu = session->mtu; |
| 1598 | |
| 1599 | error = ppp_register_channel(&po->chan); |
| 1600 | if (error) |
| 1601 | goto end; |
| 1602 | |
| 1603 | /* This is how we get the session context from the socket. */ |
| 1604 | sk->sk_user_data = session; |
| 1605 | |
| 1606 | /* Add session to the tunnel's hash list */ |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 1607 | write_lock_bh(&tunnel->hlist_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1608 | hlist_add_head(&session->hlist, |
| 1609 | pppol2tp_session_id_hash(tunnel, |
| 1610 | session->tunnel_addr.s_session)); |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 1611 | write_unlock_bh(&tunnel->hlist_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 1612 | |
| 1613 | atomic_inc(&pppol2tp_session_count); |
| 1614 | |
| 1615 | out_no_ppp: |
| 1616 | pppol2tp_tunnel_inc_refcount(tunnel); |
| 1617 | sk->sk_state = PPPOX_CONNECTED; |
| 1618 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1619 | "%s: created\n", session->name); |
| 1620 | |
| 1621 | end: |
| 1622 | release_sock(sk); |
| 1623 | |
| 1624 | if (error != 0) |
| 1625 | PRINTK(session ? session->debug : -1, PPPOL2TP_MSG_CONTROL, KERN_WARNING, |
| 1626 | "%s: connect failed: %d\n", session->name, error); |
| 1627 | |
| 1628 | return error; |
| 1629 | } |
| 1630 | |
| 1631 | /* getname() support. |
| 1632 | */ |
| 1633 | static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr, |
| 1634 | int *usockaddr_len, int peer) |
| 1635 | { |
| 1636 | int len = sizeof(struct sockaddr_pppol2tp); |
| 1637 | struct sockaddr_pppol2tp sp; |
| 1638 | int error = 0; |
| 1639 | struct pppol2tp_session *session; |
| 1640 | |
| 1641 | error = -ENOTCONN; |
| 1642 | if (sock->sk->sk_state != PPPOX_CONNECTED) |
| 1643 | goto end; |
| 1644 | |
| 1645 | session = pppol2tp_sock_to_session(sock->sk); |
| 1646 | if (session == NULL) { |
| 1647 | error = -EBADF; |
| 1648 | goto end; |
| 1649 | } |
| 1650 | |
| 1651 | sp.sa_family = AF_PPPOX; |
| 1652 | sp.sa_protocol = PX_PROTO_OL2TP; |
| 1653 | memcpy(&sp.pppol2tp, &session->tunnel_addr, |
| 1654 | sizeof(struct pppol2tp_addr)); |
| 1655 | |
| 1656 | memcpy(uaddr, &sp, len); |
| 1657 | |
| 1658 | *usockaddr_len = len; |
| 1659 | |
| 1660 | error = 0; |
| 1661 | |
| 1662 | end: |
| 1663 | return error; |
| 1664 | } |
| 1665 | |
| 1666 | /**************************************************************************** |
| 1667 | * ioctl() handlers. |
| 1668 | * |
| 1669 | * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP |
| 1670 | * sockets. However, in order to control kernel tunnel features, we allow |
| 1671 | * userspace to create a special "tunnel" PPPoX socket which is used for |
| 1672 | * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow |
| 1673 | * the user application to issue L2TP setsockopt(), getsockopt() and ioctl() |
| 1674 | * calls. |
| 1675 | ****************************************************************************/ |
| 1676 | |
| 1677 | /* Session ioctl helper. |
| 1678 | */ |
| 1679 | static int pppol2tp_session_ioctl(struct pppol2tp_session *session, |
| 1680 | unsigned int cmd, unsigned long arg) |
| 1681 | { |
| 1682 | struct ifreq ifr; |
| 1683 | int err = 0; |
| 1684 | struct sock *sk = session->sock; |
| 1685 | int val = (int) arg; |
| 1686 | |
| 1687 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG, |
| 1688 | "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n", |
| 1689 | session->name, cmd, arg); |
| 1690 | |
| 1691 | sock_hold(sk); |
| 1692 | |
| 1693 | switch (cmd) { |
| 1694 | case SIOCGIFMTU: |
| 1695 | err = -ENXIO; |
| 1696 | if (!(sk->sk_state & PPPOX_CONNECTED)) |
| 1697 | break; |
| 1698 | |
| 1699 | err = -EFAULT; |
| 1700 | if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq))) |
| 1701 | break; |
| 1702 | ifr.ifr_mtu = session->mtu; |
| 1703 | if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq))) |
| 1704 | break; |
| 1705 | |
| 1706 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1707 | "%s: get mtu=%d\n", session->name, session->mtu); |
| 1708 | err = 0; |
| 1709 | break; |
| 1710 | |
| 1711 | case SIOCSIFMTU: |
| 1712 | err = -ENXIO; |
| 1713 | if (!(sk->sk_state & PPPOX_CONNECTED)) |
| 1714 | break; |
| 1715 | |
| 1716 | err = -EFAULT; |
| 1717 | if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq))) |
| 1718 | break; |
| 1719 | |
| 1720 | session->mtu = ifr.ifr_mtu; |
| 1721 | |
| 1722 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1723 | "%s: set mtu=%d\n", session->name, session->mtu); |
| 1724 | err = 0; |
| 1725 | break; |
| 1726 | |
| 1727 | case PPPIOCGMRU: |
| 1728 | err = -ENXIO; |
| 1729 | if (!(sk->sk_state & PPPOX_CONNECTED)) |
| 1730 | break; |
| 1731 | |
| 1732 | err = -EFAULT; |
| 1733 | if (put_user(session->mru, (int __user *) arg)) |
| 1734 | break; |
| 1735 | |
| 1736 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1737 | "%s: get mru=%d\n", session->name, session->mru); |
| 1738 | err = 0; |
| 1739 | break; |
| 1740 | |
| 1741 | case PPPIOCSMRU: |
| 1742 | err = -ENXIO; |
| 1743 | if (!(sk->sk_state & PPPOX_CONNECTED)) |
| 1744 | break; |
| 1745 | |
| 1746 | err = -EFAULT; |
| 1747 | if (get_user(val,(int __user *) arg)) |
| 1748 | break; |
| 1749 | |
| 1750 | session->mru = val; |
| 1751 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1752 | "%s: set mru=%d\n", session->name, session->mru); |
| 1753 | err = 0; |
| 1754 | break; |
| 1755 | |
| 1756 | case PPPIOCGFLAGS: |
| 1757 | err = -EFAULT; |
| 1758 | if (put_user(session->flags, (int __user *) arg)) |
| 1759 | break; |
| 1760 | |
| 1761 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1762 | "%s: get flags=%d\n", session->name, session->flags); |
| 1763 | err = 0; |
| 1764 | break; |
| 1765 | |
| 1766 | case PPPIOCSFLAGS: |
| 1767 | err = -EFAULT; |
| 1768 | if (get_user(val, (int __user *) arg)) |
| 1769 | break; |
| 1770 | session->flags = val; |
| 1771 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1772 | "%s: set flags=%d\n", session->name, session->flags); |
| 1773 | err = 0; |
| 1774 | break; |
| 1775 | |
| 1776 | case PPPIOCGL2TPSTATS: |
| 1777 | err = -ENXIO; |
| 1778 | if (!(sk->sk_state & PPPOX_CONNECTED)) |
| 1779 | break; |
| 1780 | |
| 1781 | if (copy_to_user((void __user *) arg, &session->stats, |
| 1782 | sizeof(session->stats))) |
| 1783 | break; |
| 1784 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1785 | "%s: get L2TP stats\n", session->name); |
| 1786 | err = 0; |
| 1787 | break; |
| 1788 | |
| 1789 | default: |
| 1790 | err = -ENOSYS; |
| 1791 | break; |
| 1792 | } |
| 1793 | |
| 1794 | sock_put(sk); |
| 1795 | |
| 1796 | return err; |
| 1797 | } |
| 1798 | |
| 1799 | /* Tunnel ioctl helper. |
| 1800 | * |
| 1801 | * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data |
| 1802 | * specifies a session_id, the session ioctl handler is called. This allows an |
| 1803 | * application to retrieve session stats via a tunnel socket. |
| 1804 | */ |
| 1805 | static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel *tunnel, |
| 1806 | unsigned int cmd, unsigned long arg) |
| 1807 | { |
| 1808 | int err = 0; |
| 1809 | struct sock *sk = tunnel->sock; |
| 1810 | struct pppol2tp_ioc_stats stats_req; |
| 1811 | |
| 1812 | PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG, |
| 1813 | "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel->name, |
| 1814 | cmd, arg); |
| 1815 | |
| 1816 | sock_hold(sk); |
| 1817 | |
| 1818 | switch (cmd) { |
| 1819 | case PPPIOCGL2TPSTATS: |
| 1820 | err = -ENXIO; |
| 1821 | if (!(sk->sk_state & PPPOX_CONNECTED)) |
| 1822 | break; |
| 1823 | |
| 1824 | if (copy_from_user(&stats_req, (void __user *) arg, |
| 1825 | sizeof(stats_req))) { |
| 1826 | err = -EFAULT; |
| 1827 | break; |
| 1828 | } |
| 1829 | if (stats_req.session_id != 0) { |
| 1830 | /* resend to session ioctl handler */ |
| 1831 | struct pppol2tp_session *session = |
| 1832 | pppol2tp_session_find(tunnel, stats_req.session_id); |
| 1833 | if (session != NULL) |
| 1834 | err = pppol2tp_session_ioctl(session, cmd, arg); |
| 1835 | else |
| 1836 | err = -EBADR; |
| 1837 | break; |
| 1838 | } |
| 1839 | #ifdef CONFIG_XFRM |
| 1840 | tunnel->stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0; |
| 1841 | #endif |
| 1842 | if (copy_to_user((void __user *) arg, &tunnel->stats, |
| 1843 | sizeof(tunnel->stats))) { |
| 1844 | err = -EFAULT; |
| 1845 | break; |
| 1846 | } |
| 1847 | PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1848 | "%s: get L2TP stats\n", tunnel->name); |
| 1849 | err = 0; |
| 1850 | break; |
| 1851 | |
| 1852 | default: |
| 1853 | err = -ENOSYS; |
| 1854 | break; |
| 1855 | } |
| 1856 | |
| 1857 | sock_put(sk); |
| 1858 | |
| 1859 | return err; |
| 1860 | } |
| 1861 | |
| 1862 | /* Main ioctl() handler. |
| 1863 | * Dispatch to tunnel or session helpers depending on the socket. |
| 1864 | */ |
| 1865 | static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd, |
| 1866 | unsigned long arg) |
| 1867 | { |
| 1868 | struct sock *sk = sock->sk; |
| 1869 | struct pppol2tp_session *session; |
| 1870 | struct pppol2tp_tunnel *tunnel; |
| 1871 | int err; |
| 1872 | |
| 1873 | if (!sk) |
| 1874 | return 0; |
| 1875 | |
| 1876 | err = -EBADF; |
| 1877 | if (sock_flag(sk, SOCK_DEAD) != 0) |
| 1878 | goto end; |
| 1879 | |
| 1880 | err = -ENOTCONN; |
| 1881 | if ((sk->sk_user_data == NULL) || |
| 1882 | (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)))) |
| 1883 | goto end; |
| 1884 | |
| 1885 | /* Get session context from the socket */ |
| 1886 | err = -EBADF; |
| 1887 | session = pppol2tp_sock_to_session(sk); |
| 1888 | if (session == NULL) |
| 1889 | goto end; |
| 1890 | |
| 1891 | /* Special case: if session's session_id is zero, treat ioctl as a |
| 1892 | * tunnel ioctl |
| 1893 | */ |
| 1894 | if ((session->tunnel_addr.s_session == 0) && |
| 1895 | (session->tunnel_addr.d_session == 0)) { |
| 1896 | err = -EBADF; |
| 1897 | tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock); |
| 1898 | if (tunnel == NULL) |
| 1899 | goto end; |
| 1900 | |
| 1901 | err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg); |
| 1902 | goto end; |
| 1903 | } |
| 1904 | |
| 1905 | err = pppol2tp_session_ioctl(session, cmd, arg); |
| 1906 | |
| 1907 | end: |
| 1908 | return err; |
| 1909 | } |
| 1910 | |
| 1911 | /***************************************************************************** |
| 1912 | * setsockopt() / getsockopt() support. |
| 1913 | * |
| 1914 | * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP |
| 1915 | * sockets. In order to control kernel tunnel features, we allow userspace to |
| 1916 | * create a special "tunnel" PPPoX socket which is used for control only. |
| 1917 | * Tunnel PPPoX sockets have session_id == 0 and simply allow the user |
| 1918 | * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls. |
| 1919 | *****************************************************************************/ |
| 1920 | |
| 1921 | /* Tunnel setsockopt() helper. |
| 1922 | */ |
| 1923 | static int pppol2tp_tunnel_setsockopt(struct sock *sk, |
| 1924 | struct pppol2tp_tunnel *tunnel, |
| 1925 | int optname, int val) |
| 1926 | { |
| 1927 | int err = 0; |
| 1928 | |
| 1929 | switch (optname) { |
| 1930 | case PPPOL2TP_SO_DEBUG: |
| 1931 | tunnel->debug = val; |
| 1932 | PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1933 | "%s: set debug=%x\n", tunnel->name, tunnel->debug); |
| 1934 | break; |
| 1935 | |
| 1936 | default: |
| 1937 | err = -ENOPROTOOPT; |
| 1938 | break; |
| 1939 | } |
| 1940 | |
| 1941 | return err; |
| 1942 | } |
| 1943 | |
| 1944 | /* Session setsockopt helper. |
| 1945 | */ |
| 1946 | static int pppol2tp_session_setsockopt(struct sock *sk, |
| 1947 | struct pppol2tp_session *session, |
| 1948 | int optname, int val) |
| 1949 | { |
| 1950 | int err = 0; |
| 1951 | |
| 1952 | switch (optname) { |
| 1953 | case PPPOL2TP_SO_RECVSEQ: |
| 1954 | if ((val != 0) && (val != 1)) { |
| 1955 | err = -EINVAL; |
| 1956 | break; |
| 1957 | } |
| 1958 | session->recv_seq = val ? -1 : 0; |
| 1959 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1960 | "%s: set recv_seq=%d\n", session->name, |
| 1961 | session->recv_seq); |
| 1962 | break; |
| 1963 | |
| 1964 | case PPPOL2TP_SO_SENDSEQ: |
| 1965 | if ((val != 0) && (val != 1)) { |
| 1966 | err = -EINVAL; |
| 1967 | break; |
| 1968 | } |
| 1969 | session->send_seq = val ? -1 : 0; |
| 1970 | { |
| 1971 | struct sock *ssk = session->sock; |
| 1972 | struct pppox_sock *po = pppox_sk(ssk); |
| 1973 | po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ : |
| 1974 | PPPOL2TP_L2TP_HDR_SIZE_NOSEQ; |
| 1975 | } |
| 1976 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1977 | "%s: set send_seq=%d\n", session->name, session->send_seq); |
| 1978 | break; |
| 1979 | |
| 1980 | case PPPOL2TP_SO_LNSMODE: |
| 1981 | if ((val != 0) && (val != 1)) { |
| 1982 | err = -EINVAL; |
| 1983 | break; |
| 1984 | } |
| 1985 | session->lns_mode = val ? -1 : 0; |
| 1986 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1987 | "%s: set lns_mode=%d\n", session->name, |
| 1988 | session->lns_mode); |
| 1989 | break; |
| 1990 | |
| 1991 | case PPPOL2TP_SO_DEBUG: |
| 1992 | session->debug = val; |
| 1993 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 1994 | "%s: set debug=%x\n", session->name, session->debug); |
| 1995 | break; |
| 1996 | |
| 1997 | case PPPOL2TP_SO_REORDERTO: |
| 1998 | session->reorder_timeout = msecs_to_jiffies(val); |
| 1999 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 2000 | "%s: set reorder_timeout=%d\n", session->name, |
| 2001 | session->reorder_timeout); |
| 2002 | break; |
| 2003 | |
| 2004 | default: |
| 2005 | err = -ENOPROTOOPT; |
| 2006 | break; |
| 2007 | } |
| 2008 | |
| 2009 | return err; |
| 2010 | } |
| 2011 | |
| 2012 | /* Main setsockopt() entry point. |
| 2013 | * Does API checks, then calls either the tunnel or session setsockopt |
| 2014 | * handler, according to whether the PPPoL2TP socket is a for a regular |
| 2015 | * session or the special tunnel type. |
| 2016 | */ |
| 2017 | static int pppol2tp_setsockopt(struct socket *sock, int level, int optname, |
| 2018 | char __user *optval, int optlen) |
| 2019 | { |
| 2020 | struct sock *sk = sock->sk; |
| 2021 | struct pppol2tp_session *session = sk->sk_user_data; |
| 2022 | struct pppol2tp_tunnel *tunnel; |
| 2023 | int val; |
| 2024 | int err; |
| 2025 | |
| 2026 | if (level != SOL_PPPOL2TP) |
| 2027 | return udp_prot.setsockopt(sk, level, optname, optval, optlen); |
| 2028 | |
| 2029 | if (optlen < sizeof(int)) |
| 2030 | return -EINVAL; |
| 2031 | |
| 2032 | if (get_user(val, (int __user *)optval)) |
| 2033 | return -EFAULT; |
| 2034 | |
| 2035 | err = -ENOTCONN; |
| 2036 | if (sk->sk_user_data == NULL) |
| 2037 | goto end; |
| 2038 | |
| 2039 | /* Get session context from the socket */ |
| 2040 | err = -EBADF; |
| 2041 | session = pppol2tp_sock_to_session(sk); |
| 2042 | if (session == NULL) |
| 2043 | goto end; |
| 2044 | |
| 2045 | /* Special case: if session_id == 0x0000, treat as operation on tunnel |
| 2046 | */ |
| 2047 | if ((session->tunnel_addr.s_session == 0) && |
| 2048 | (session->tunnel_addr.d_session == 0)) { |
| 2049 | err = -EBADF; |
| 2050 | tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock); |
| 2051 | if (tunnel == NULL) |
| 2052 | goto end; |
| 2053 | |
| 2054 | err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val); |
| 2055 | } else |
| 2056 | err = pppol2tp_session_setsockopt(sk, session, optname, val); |
| 2057 | |
| 2058 | err = 0; |
| 2059 | |
| 2060 | end: |
| 2061 | return err; |
| 2062 | } |
| 2063 | |
| 2064 | /* Tunnel getsockopt helper. Called with sock locked. |
| 2065 | */ |
| 2066 | static int pppol2tp_tunnel_getsockopt(struct sock *sk, |
| 2067 | struct pppol2tp_tunnel *tunnel, |
Al Viro | af3b162 | 2007-07-26 17:35:09 +0100 | [diff] [blame] | 2068 | int optname, int *val) |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 2069 | { |
| 2070 | int err = 0; |
| 2071 | |
| 2072 | switch (optname) { |
| 2073 | case PPPOL2TP_SO_DEBUG: |
| 2074 | *val = tunnel->debug; |
| 2075 | PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 2076 | "%s: get debug=%x\n", tunnel->name, tunnel->debug); |
| 2077 | break; |
| 2078 | |
| 2079 | default: |
| 2080 | err = -ENOPROTOOPT; |
| 2081 | break; |
| 2082 | } |
| 2083 | |
| 2084 | return err; |
| 2085 | } |
| 2086 | |
| 2087 | /* Session getsockopt helper. Called with sock locked. |
| 2088 | */ |
| 2089 | static int pppol2tp_session_getsockopt(struct sock *sk, |
| 2090 | struct pppol2tp_session *session, |
Al Viro | af3b162 | 2007-07-26 17:35:09 +0100 | [diff] [blame] | 2091 | int optname, int *val) |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 2092 | { |
| 2093 | int err = 0; |
| 2094 | |
| 2095 | switch (optname) { |
| 2096 | case PPPOL2TP_SO_RECVSEQ: |
| 2097 | *val = session->recv_seq; |
| 2098 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 2099 | "%s: get recv_seq=%d\n", session->name, *val); |
| 2100 | break; |
| 2101 | |
| 2102 | case PPPOL2TP_SO_SENDSEQ: |
| 2103 | *val = session->send_seq; |
| 2104 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 2105 | "%s: get send_seq=%d\n", session->name, *val); |
| 2106 | break; |
| 2107 | |
| 2108 | case PPPOL2TP_SO_LNSMODE: |
| 2109 | *val = session->lns_mode; |
| 2110 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 2111 | "%s: get lns_mode=%d\n", session->name, *val); |
| 2112 | break; |
| 2113 | |
| 2114 | case PPPOL2TP_SO_DEBUG: |
| 2115 | *val = session->debug; |
| 2116 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 2117 | "%s: get debug=%d\n", session->name, *val); |
| 2118 | break; |
| 2119 | |
| 2120 | case PPPOL2TP_SO_REORDERTO: |
| 2121 | *val = (int) jiffies_to_msecs(session->reorder_timeout); |
| 2122 | PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, |
| 2123 | "%s: get reorder_timeout=%d\n", session->name, *val); |
| 2124 | break; |
| 2125 | |
| 2126 | default: |
| 2127 | err = -ENOPROTOOPT; |
| 2128 | } |
| 2129 | |
| 2130 | return err; |
| 2131 | } |
| 2132 | |
| 2133 | /* Main getsockopt() entry point. |
| 2134 | * Does API checks, then calls either the tunnel or session getsockopt |
| 2135 | * handler, according to whether the PPPoX socket is a for a regular session |
| 2136 | * or the special tunnel type. |
| 2137 | */ |
| 2138 | static int pppol2tp_getsockopt(struct socket *sock, int level, |
| 2139 | int optname, char __user *optval, int __user *optlen) |
| 2140 | { |
| 2141 | struct sock *sk = sock->sk; |
| 2142 | struct pppol2tp_session *session = sk->sk_user_data; |
| 2143 | struct pppol2tp_tunnel *tunnel; |
| 2144 | int val, len; |
| 2145 | int err; |
| 2146 | |
| 2147 | if (level != SOL_PPPOL2TP) |
| 2148 | return udp_prot.getsockopt(sk, level, optname, optval, optlen); |
| 2149 | |
| 2150 | if (get_user(len, (int __user *) optlen)) |
| 2151 | return -EFAULT; |
| 2152 | |
| 2153 | len = min_t(unsigned int, len, sizeof(int)); |
| 2154 | |
| 2155 | if (len < 0) |
| 2156 | return -EINVAL; |
| 2157 | |
| 2158 | err = -ENOTCONN; |
| 2159 | if (sk->sk_user_data == NULL) |
| 2160 | goto end; |
| 2161 | |
| 2162 | /* Get the session context */ |
| 2163 | err = -EBADF; |
| 2164 | session = pppol2tp_sock_to_session(sk); |
| 2165 | if (session == NULL) |
| 2166 | goto end; |
| 2167 | |
| 2168 | /* Special case: if session_id == 0x0000, treat as operation on tunnel */ |
| 2169 | if ((session->tunnel_addr.s_session == 0) && |
| 2170 | (session->tunnel_addr.d_session == 0)) { |
| 2171 | err = -EBADF; |
| 2172 | tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock); |
| 2173 | if (tunnel == NULL) |
| 2174 | goto end; |
| 2175 | |
| 2176 | err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val); |
| 2177 | } else |
| 2178 | err = pppol2tp_session_getsockopt(sk, session, optname, &val); |
| 2179 | |
| 2180 | err = -EFAULT; |
| 2181 | if (put_user(len, (int __user *) optlen)) |
| 2182 | goto end; |
| 2183 | |
| 2184 | if (copy_to_user((void __user *) optval, &val, len)) |
| 2185 | goto end; |
| 2186 | |
| 2187 | err = 0; |
| 2188 | end: |
| 2189 | return err; |
| 2190 | } |
| 2191 | |
| 2192 | /***************************************************************************** |
| 2193 | * /proc filesystem for debug |
| 2194 | *****************************************************************************/ |
| 2195 | |
| 2196 | #ifdef CONFIG_PROC_FS |
| 2197 | |
| 2198 | #include <linux/seq_file.h> |
| 2199 | |
| 2200 | struct pppol2tp_seq_data { |
| 2201 | struct pppol2tp_tunnel *tunnel; /* current tunnel */ |
| 2202 | struct pppol2tp_session *session; /* NULL means get first session in tunnel */ |
| 2203 | }; |
| 2204 | |
| 2205 | static struct pppol2tp_session *next_session(struct pppol2tp_tunnel *tunnel, struct pppol2tp_session *curr) |
| 2206 | { |
| 2207 | struct pppol2tp_session *session = NULL; |
| 2208 | struct hlist_node *walk; |
| 2209 | int found = 0; |
| 2210 | int next = 0; |
| 2211 | int i; |
| 2212 | |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 2213 | read_lock_bh(&tunnel->hlist_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 2214 | for (i = 0; i < PPPOL2TP_HASH_SIZE; i++) { |
| 2215 | hlist_for_each_entry(session, walk, &tunnel->session_hlist[i], hlist) { |
| 2216 | if (curr == NULL) { |
| 2217 | found = 1; |
| 2218 | goto out; |
| 2219 | } |
| 2220 | if (session == curr) { |
| 2221 | next = 1; |
| 2222 | continue; |
| 2223 | } |
| 2224 | if (next) { |
| 2225 | found = 1; |
| 2226 | goto out; |
| 2227 | } |
| 2228 | } |
| 2229 | } |
| 2230 | out: |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 2231 | read_unlock_bh(&tunnel->hlist_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 2232 | if (!found) |
| 2233 | session = NULL; |
| 2234 | |
| 2235 | return session; |
| 2236 | } |
| 2237 | |
| 2238 | static struct pppol2tp_tunnel *next_tunnel(struct pppol2tp_tunnel *curr) |
| 2239 | { |
| 2240 | struct pppol2tp_tunnel *tunnel = NULL; |
| 2241 | |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 2242 | read_lock_bh(&pppol2tp_tunnel_list_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 2243 | if (list_is_last(&curr->list, &pppol2tp_tunnel_list)) { |
| 2244 | goto out; |
| 2245 | } |
| 2246 | tunnel = list_entry(curr->list.next, struct pppol2tp_tunnel, list); |
| 2247 | out: |
James Chapman | cf3752e | 2008-03-05 18:39:08 -0800 | [diff] [blame] | 2248 | read_unlock_bh(&pppol2tp_tunnel_list_lock); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 2249 | |
| 2250 | return tunnel; |
| 2251 | } |
| 2252 | |
| 2253 | static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs) |
| 2254 | { |
| 2255 | struct pppol2tp_seq_data *pd = SEQ_START_TOKEN; |
| 2256 | loff_t pos = *offs; |
| 2257 | |
| 2258 | if (!pos) |
| 2259 | goto out; |
| 2260 | |
| 2261 | BUG_ON(m->private == NULL); |
| 2262 | pd = m->private; |
| 2263 | |
| 2264 | if (pd->tunnel == NULL) { |
| 2265 | if (!list_empty(&pppol2tp_tunnel_list)) |
| 2266 | pd->tunnel = list_entry(pppol2tp_tunnel_list.next, struct pppol2tp_tunnel, list); |
| 2267 | } else { |
| 2268 | pd->session = next_session(pd->tunnel, pd->session); |
| 2269 | if (pd->session == NULL) { |
| 2270 | pd->tunnel = next_tunnel(pd->tunnel); |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | /* NULL tunnel and session indicates end of list */ |
| 2275 | if ((pd->tunnel == NULL) && (pd->session == NULL)) |
| 2276 | pd = NULL; |
| 2277 | |
| 2278 | out: |
| 2279 | return pd; |
| 2280 | } |
| 2281 | |
| 2282 | static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos) |
| 2283 | { |
| 2284 | (*pos)++; |
| 2285 | return NULL; |
| 2286 | } |
| 2287 | |
| 2288 | static void pppol2tp_seq_stop(struct seq_file *p, void *v) |
| 2289 | { |
| 2290 | /* nothing to do */ |
| 2291 | } |
| 2292 | |
| 2293 | static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v) |
| 2294 | { |
| 2295 | struct pppol2tp_tunnel *tunnel = v; |
| 2296 | |
| 2297 | seq_printf(m, "\nTUNNEL '%s', %c %d\n", |
| 2298 | tunnel->name, |
| 2299 | (tunnel == tunnel->sock->sk_user_data) ? 'Y':'N', |
| 2300 | atomic_read(&tunnel->ref_count) - 1); |
| 2301 | seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n", |
| 2302 | tunnel->debug, |
Andrew Morton | 0efeaa3 | 2008-02-09 23:17:51 -0800 | [diff] [blame] | 2303 | (unsigned long long)tunnel->stats.tx_packets, |
| 2304 | (unsigned long long)tunnel->stats.tx_bytes, |
| 2305 | (unsigned long long)tunnel->stats.tx_errors, |
| 2306 | (unsigned long long)tunnel->stats.rx_packets, |
| 2307 | (unsigned long long)tunnel->stats.rx_bytes, |
| 2308 | (unsigned long long)tunnel->stats.rx_errors); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 2309 | } |
| 2310 | |
| 2311 | static void pppol2tp_seq_session_show(struct seq_file *m, void *v) |
| 2312 | { |
| 2313 | struct pppol2tp_session *session = v; |
| 2314 | |
| 2315 | seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> " |
| 2316 | "%04X/%04X %d %c\n", |
| 2317 | session->name, |
| 2318 | ntohl(session->tunnel_addr.addr.sin_addr.s_addr), |
| 2319 | ntohs(session->tunnel_addr.addr.sin_port), |
| 2320 | session->tunnel_addr.s_tunnel, |
| 2321 | session->tunnel_addr.s_session, |
| 2322 | session->tunnel_addr.d_tunnel, |
| 2323 | session->tunnel_addr.d_session, |
| 2324 | session->sock->sk_state, |
| 2325 | (session == session->sock->sk_user_data) ? |
| 2326 | 'Y' : 'N'); |
| 2327 | seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n", |
| 2328 | session->mtu, session->mru, |
| 2329 | session->recv_seq ? 'R' : '-', |
| 2330 | session->send_seq ? 'S' : '-', |
| 2331 | session->lns_mode ? "LNS" : "LAC", |
| 2332 | session->debug, |
| 2333 | jiffies_to_msecs(session->reorder_timeout)); |
| 2334 | seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n", |
| 2335 | session->nr, session->ns, |
Andrew Morton | 0efeaa3 | 2008-02-09 23:17:51 -0800 | [diff] [blame] | 2336 | (unsigned long long)session->stats.tx_packets, |
| 2337 | (unsigned long long)session->stats.tx_bytes, |
| 2338 | (unsigned long long)session->stats.tx_errors, |
| 2339 | (unsigned long long)session->stats.rx_packets, |
| 2340 | (unsigned long long)session->stats.rx_bytes, |
| 2341 | (unsigned long long)session->stats.rx_errors); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | static int pppol2tp_seq_show(struct seq_file *m, void *v) |
| 2345 | { |
| 2346 | struct pppol2tp_seq_data *pd = v; |
| 2347 | |
| 2348 | /* display header on line 1 */ |
| 2349 | if (v == SEQ_START_TOKEN) { |
| 2350 | seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n"); |
| 2351 | seq_puts(m, "TUNNEL name, user-data-ok session-count\n"); |
| 2352 | seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n"); |
| 2353 | seq_puts(m, " SESSION name, addr/port src-tid/sid " |
| 2354 | "dest-tid/sid state user-data-ok\n"); |
| 2355 | seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n"); |
| 2356 | seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n"); |
| 2357 | goto out; |
| 2358 | } |
| 2359 | |
| 2360 | /* Show the tunnel or session context. |
| 2361 | */ |
| 2362 | if (pd->session == NULL) |
| 2363 | pppol2tp_seq_tunnel_show(m, pd->tunnel); |
| 2364 | else |
| 2365 | pppol2tp_seq_session_show(m, pd->session); |
| 2366 | |
| 2367 | out: |
| 2368 | return 0; |
| 2369 | } |
| 2370 | |
| 2371 | static struct seq_operations pppol2tp_seq_ops = { |
| 2372 | .start = pppol2tp_seq_start, |
| 2373 | .next = pppol2tp_seq_next, |
| 2374 | .stop = pppol2tp_seq_stop, |
| 2375 | .show = pppol2tp_seq_show, |
| 2376 | }; |
| 2377 | |
| 2378 | /* Called when our /proc file is opened. We allocate data for use when |
| 2379 | * iterating our tunnel / session contexts and store it in the private |
| 2380 | * data of the seq_file. |
| 2381 | */ |
| 2382 | static int pppol2tp_proc_open(struct inode *inode, struct file *file) |
| 2383 | { |
| 2384 | struct seq_file *m; |
| 2385 | struct pppol2tp_seq_data *pd; |
| 2386 | int ret = 0; |
| 2387 | |
| 2388 | ret = seq_open(file, &pppol2tp_seq_ops); |
| 2389 | if (ret < 0) |
| 2390 | goto out; |
| 2391 | |
| 2392 | m = file->private_data; |
| 2393 | |
| 2394 | /* Allocate and fill our proc_data for access later */ |
| 2395 | ret = -ENOMEM; |
| 2396 | m->private = kzalloc(sizeof(struct pppol2tp_seq_data), GFP_KERNEL); |
| 2397 | if (m->private == NULL) |
| 2398 | goto out; |
| 2399 | |
| 2400 | pd = m->private; |
| 2401 | ret = 0; |
| 2402 | |
| 2403 | out: |
| 2404 | return ret; |
| 2405 | } |
| 2406 | |
| 2407 | /* Called when /proc file access completes. |
| 2408 | */ |
| 2409 | static int pppol2tp_proc_release(struct inode *inode, struct file *file) |
| 2410 | { |
| 2411 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 2412 | |
| 2413 | kfree(m->private); |
| 2414 | m->private = NULL; |
| 2415 | |
| 2416 | return seq_release(inode, file); |
| 2417 | } |
| 2418 | |
| 2419 | static struct file_operations pppol2tp_proc_fops = { |
| 2420 | .owner = THIS_MODULE, |
| 2421 | .open = pppol2tp_proc_open, |
| 2422 | .read = seq_read, |
| 2423 | .llseek = seq_lseek, |
| 2424 | .release = pppol2tp_proc_release, |
| 2425 | }; |
| 2426 | |
| 2427 | static struct proc_dir_entry *pppol2tp_proc; |
| 2428 | |
| 2429 | #endif /* CONFIG_PROC_FS */ |
| 2430 | |
| 2431 | /***************************************************************************** |
| 2432 | * Init and cleanup |
| 2433 | *****************************************************************************/ |
| 2434 | |
| 2435 | static struct proto_ops pppol2tp_ops = { |
| 2436 | .family = AF_PPPOX, |
| 2437 | .owner = THIS_MODULE, |
| 2438 | .release = pppol2tp_release, |
| 2439 | .bind = sock_no_bind, |
| 2440 | .connect = pppol2tp_connect, |
| 2441 | .socketpair = sock_no_socketpair, |
| 2442 | .accept = sock_no_accept, |
| 2443 | .getname = pppol2tp_getname, |
| 2444 | .poll = datagram_poll, |
| 2445 | .listen = sock_no_listen, |
| 2446 | .shutdown = sock_no_shutdown, |
| 2447 | .setsockopt = pppol2tp_setsockopt, |
| 2448 | .getsockopt = pppol2tp_getsockopt, |
| 2449 | .sendmsg = pppol2tp_sendmsg, |
| 2450 | .recvmsg = pppol2tp_recvmsg, |
| 2451 | .mmap = sock_no_mmap, |
| 2452 | .ioctl = pppox_ioctl, |
| 2453 | }; |
| 2454 | |
| 2455 | static struct pppox_proto pppol2tp_proto = { |
| 2456 | .create = pppol2tp_create, |
| 2457 | .ioctl = pppol2tp_ioctl |
| 2458 | }; |
| 2459 | |
| 2460 | static int __init pppol2tp_init(void) |
| 2461 | { |
| 2462 | int err; |
| 2463 | |
| 2464 | err = proto_register(&pppol2tp_sk_proto, 0); |
| 2465 | if (err) |
| 2466 | goto out; |
| 2467 | err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto); |
| 2468 | if (err) |
| 2469 | goto out_unregister_pppol2tp_proto; |
| 2470 | |
| 2471 | #ifdef CONFIG_PROC_FS |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 2472 | pppol2tp_proc = create_proc_entry("pppol2tp", 0, init_net.proc_net); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 2473 | if (!pppol2tp_proc) { |
| 2474 | err = -ENOMEM; |
| 2475 | goto out_unregister_pppox_proto; |
| 2476 | } |
| 2477 | pppol2tp_proc->proc_fops = &pppol2tp_proc_fops; |
| 2478 | #endif /* CONFIG_PROC_FS */ |
| 2479 | printk(KERN_INFO "PPPoL2TP kernel driver, %s\n", |
| 2480 | PPPOL2TP_DRV_VERSION); |
| 2481 | |
| 2482 | out: |
| 2483 | return err; |
Rami Rosen | 731a060 | 2008-02-05 16:30:50 -0800 | [diff] [blame] | 2484 | #ifdef CONFIG_PROC_FS |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 2485 | out_unregister_pppox_proto: |
| 2486 | unregister_pppox_proto(PX_PROTO_OL2TP); |
Rami Rosen | 731a060 | 2008-02-05 16:30:50 -0800 | [diff] [blame] | 2487 | #endif |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 2488 | out_unregister_pppol2tp_proto: |
| 2489 | proto_unregister(&pppol2tp_sk_proto); |
| 2490 | goto out; |
| 2491 | } |
| 2492 | |
| 2493 | static void __exit pppol2tp_exit(void) |
| 2494 | { |
| 2495 | unregister_pppox_proto(PX_PROTO_OL2TP); |
| 2496 | |
| 2497 | #ifdef CONFIG_PROC_FS |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 2498 | remove_proc_entry("pppol2tp", init_net.proc_net); |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 2499 | #endif |
| 2500 | proto_unregister(&pppol2tp_sk_proto); |
| 2501 | } |
| 2502 | |
| 2503 | module_init(pppol2tp_init); |
| 2504 | module_exit(pppol2tp_exit); |
| 2505 | |
Joe Perches | 2450022 | 2007-11-19 17:48:28 -0800 | [diff] [blame] | 2506 | MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>, " |
James Chapman | 3557baa | 2007-06-27 15:49:24 -0700 | [diff] [blame] | 2507 | "James Chapman <jchapman@katalix.com>"); |
| 2508 | MODULE_DESCRIPTION("PPP over L2TP over UDP"); |
| 2509 | MODULE_LICENSE("GPL"); |
| 2510 | MODULE_VERSION(PPPOL2TP_DRV_VERSION); |