pppol2tp.c 70 KB

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