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