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