pppol2tp.c 65 KB

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