pppol2tp.c 69 KB

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