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