pppol2tp.c 69 KB

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