pppol2tp.c 67 KB

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