pppol2tp.c 65 KB

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