pppol2tp.c 69 KB

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