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