pppol2tp.c 65 KB

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