pppol2tp.c 64 KB

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