pppol2tp.c 70 KB

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