pppol2tp.c 67 KB

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