pppol2tp.c 64 KB

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