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