pppol2tp.c 65 KB

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