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