pppol2tp.c 65 KB

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