pppol2tp.c 70 KB

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