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