pppol2tp.c 65 KB

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