ppp_generic.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic PPP layer for Linux.
  4. *
  5. * Copyright 1999-2002 Paul Mackerras.
  6. *
  7. * The generic PPP layer handles the PPP network interfaces, the
  8. * /dev/ppp device, packet and VJ compression, and multilink.
  9. * It talks to PPP `channels' via the interface defined in
  10. * include/linux/ppp_channel.h. Channels provide the basic means for
  11. * sending and receiving PPP frames on some kind of communications
  12. * channel.
  13. *
  14. * Part of the code in this driver was inspired by the old async-only
  15. * PPP driver, written by Michael Callahan and Al Longyear, and
  16. * subsequently hacked by Paul Mackerras.
  17. *
  18. * ==FILEVERSION 20041108==
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched/signal.h>
  23. #include <linux/kmod.h>
  24. #include <linux/init.h>
  25. #include <linux/list.h>
  26. #include <linux/idr.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/poll.h>
  29. #include <linux/ppp_defs.h>
  30. #include <linux/filter.h>
  31. #include <linux/ppp-ioctl.h>
  32. #include <linux/ppp_channel.h>
  33. #include <linux/ppp-comp.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/rculist.h>
  36. #include <linux/rtnetlink.h>
  37. #include <linux/if_arp.h>
  38. #include <linux/ip.h>
  39. #include <linux/tcp.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/rwsem.h>
  42. #include <linux/stddef.h>
  43. #include <linux/device.h>
  44. #include <linux/mutex.h>
  45. #include <linux/slab.h>
  46. #include <linux/file.h>
  47. #include <linux/unaligned.h>
  48. #include <net/slhc_vj.h>
  49. #include <linux/atomic.h>
  50. #include <linux/refcount.h>
  51. #include <linux/nsproxy.h>
  52. #include <net/net_namespace.h>
  53. #include <net/netns/generic.h>
  54. #define PPP_VERSION "2.4.2"
  55. /*
  56. * Network protocols we support.
  57. */
  58. #define NP_IP 0 /* Internet Protocol V4 */
  59. #define NP_IPV6 1 /* Internet Protocol V6 */
  60. #define NP_IPX 2 /* IPX protocol */
  61. #define NP_AT 3 /* Appletalk protocol */
  62. #define NP_MPLS_UC 4 /* MPLS unicast */
  63. #define NP_MPLS_MC 5 /* MPLS multicast */
  64. #define NUM_NP 6 /* Number of NPs. */
  65. #define MPHDRLEN 6 /* multilink protocol header length */
  66. #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
  67. #define PPP_PROTO_LEN 2
  68. #define PPP_LCP_HDRLEN 4
  69. /* The filter instructions generated by libpcap are constructed
  70. * assuming a four-byte PPP header on each packet, where the last
  71. * 2 bytes are the protocol field defined in the RFC and the first
  72. * byte of the first 2 bytes indicates the direction.
  73. * The second byte is currently unused, but we still need to initialize
  74. * it to prevent crafted BPF programs from reading them which would
  75. * cause reading of uninitialized data.
  76. */
  77. #define PPP_FILTER_OUTBOUND_TAG 0x0100
  78. #define PPP_FILTER_INBOUND_TAG 0x0000
  79. /*
  80. * An instance of /dev/ppp can be associated with either a ppp
  81. * interface unit or a ppp channel. In both cases, file->private_data
  82. * points to one of these.
  83. */
  84. struct ppp_file {
  85. enum {
  86. INTERFACE=1, CHANNEL
  87. } kind;
  88. struct sk_buff_head xq; /* pppd transmit queue */
  89. struct sk_buff_head rq; /* receive queue for pppd */
  90. wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
  91. refcount_t refcnt; /* # refs (incl /dev/ppp attached) */
  92. int hdrlen; /* space to leave for headers */
  93. int index; /* interface unit / channel number */
  94. int dead; /* unit/channel has been shut down */
  95. };
  96. #define PF_TO_X(pf, X) container_of(pf, X, file)
  97. #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
  98. #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
  99. /*
  100. * Data structure to hold primary network stats for which
  101. * we want to use 64 bit storage. Other network stats
  102. * are stored in dev->stats of the ppp strucute.
  103. */
  104. struct ppp_link_stats {
  105. u64 rx_packets;
  106. u64 tx_packets;
  107. u64 rx_bytes;
  108. u64 tx_bytes;
  109. };
  110. /*
  111. * Data structure describing one ppp unit.
  112. * A ppp unit corresponds to a ppp network interface device
  113. * and represents a multilink bundle.
  114. * It can have 0 or more ppp channels connected to it.
  115. */
  116. struct ppp {
  117. struct ppp_file file; /* stuff for read/write/poll 0 */
  118. struct file *owner; /* file that owns this unit 48 */
  119. struct list_head channels; /* list of attached channels 4c */
  120. int n_channels; /* how many channels are attached 54 */
  121. spinlock_t rlock; /* lock for receive side 58 */
  122. spinlock_t wlock; /* lock for transmit side 5c */
  123. int __percpu *xmit_recursion; /* xmit recursion detect */
  124. int mru; /* max receive unit 60 */
  125. unsigned int flags; /* control bits 64 */
  126. unsigned int xstate; /* transmit state bits 68 */
  127. unsigned int rstate; /* receive state bits 6c */
  128. int debug; /* debug flags 70 */
  129. struct slcompress *vj; /* state for VJ header compression */
  130. enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
  131. struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
  132. struct compressor *xcomp; /* transmit packet compressor 8c */
  133. void *xc_state; /* its internal state 90 */
  134. struct compressor *rcomp; /* receive decompressor 94 */
  135. void *rc_state; /* its internal state 98 */
  136. unsigned long last_xmit; /* jiffies when last pkt sent 9c */
  137. unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
  138. struct net_device *dev; /* network interface device a4 */
  139. int closing; /* is device closing down? a8 */
  140. #ifdef CONFIG_PPP_MULTILINK
  141. int nxchan; /* next channel to send something on */
  142. u32 nxseq; /* next sequence number to send */
  143. int mrru; /* MP: max reconst. receive unit */
  144. u32 nextseq; /* MP: seq no of next packet */
  145. u32 minseq; /* MP: min of most recent seqnos */
  146. struct sk_buff_head mrq; /* MP: receive reconstruction queue */
  147. #endif /* CONFIG_PPP_MULTILINK */
  148. #ifdef CONFIG_PPP_FILTER
  149. struct bpf_prog *pass_filter; /* filter for packets to pass */
  150. struct bpf_prog *active_filter; /* filter for pkts to reset idle */
  151. #endif /* CONFIG_PPP_FILTER */
  152. struct net *ppp_net; /* the net we belong to */
  153. struct ppp_link_stats stats64; /* 64 bit network stats */
  154. };
  155. /*
  156. * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
  157. * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
  158. * SC_MUST_COMP
  159. * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
  160. * Bits in xstate: SC_COMP_RUN
  161. */
  162. #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
  163. |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
  164. |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
  165. /*
  166. * Private data structure for each channel.
  167. * This includes the data structure used for multilink.
  168. */
  169. struct channel {
  170. struct ppp_file file; /* stuff for read/write/poll */
  171. struct list_head list; /* link in all/new_channels list */
  172. struct ppp_channel *chan; /* public channel data structure */
  173. struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
  174. spinlock_t downl; /* protects `chan', file.xq dequeue */
  175. struct ppp *ppp; /* ppp unit we're connected to */
  176. struct net *chan_net; /* the net channel belongs to */
  177. netns_tracker ns_tracker;
  178. struct list_head clist; /* link in list of channels per unit */
  179. rwlock_t upl; /* protects `ppp' and 'bridge' */
  180. struct channel __rcu *bridge; /* "bridged" ppp channel */
  181. #ifdef CONFIG_PPP_MULTILINK
  182. u8 avail; /* flag used in multilink stuff */
  183. u8 had_frag; /* >= 1 fragments have been sent */
  184. u32 lastseq; /* MP: last sequence # received */
  185. int speed; /* speed of the corresponding ppp channel*/
  186. #endif /* CONFIG_PPP_MULTILINK */
  187. };
  188. struct ppp_config {
  189. struct file *file;
  190. s32 unit;
  191. bool ifname_is_set;
  192. };
  193. /*
  194. * SMP locking issues:
  195. * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
  196. * list and the ppp.n_channels field, you need to take both locks
  197. * before you modify them.
  198. * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
  199. * channel.downl.
  200. */
  201. static DEFINE_MUTEX(ppp_mutex);
  202. static atomic_t ppp_unit_count = ATOMIC_INIT(0);
  203. static atomic_t channel_count = ATOMIC_INIT(0);
  204. /* per-net private data for this module */
  205. static unsigned int ppp_net_id __read_mostly;
  206. struct ppp_net {
  207. /* units to ppp mapping */
  208. struct idr units_idr;
  209. /*
  210. * all_ppp_mutex protects the units_idr mapping.
  211. * It also ensures that finding a ppp unit in the units_idr
  212. * map and updating its file.refcnt field is atomic.
  213. */
  214. struct mutex all_ppp_mutex;
  215. /* channels */
  216. struct list_head all_channels;
  217. struct list_head new_channels;
  218. int last_channel_index;
  219. /*
  220. * all_channels_lock protects all_channels and
  221. * last_channel_index, and the atomicity of find
  222. * a channel and updating its file.refcnt field.
  223. */
  224. spinlock_t all_channels_lock;
  225. };
  226. /* Get the PPP protocol number from a skb */
  227. #define PPP_PROTO(skb) get_unaligned_be16((skb)->data)
  228. /* We limit the length of ppp->file.rq to this (arbitrary) value */
  229. #define PPP_MAX_RQLEN 32
  230. /*
  231. * Maximum number of multilink fragments queued up.
  232. * This has to be large enough to cope with the maximum latency of
  233. * the slowest channel relative to the others. Strictly it should
  234. * depend on the number of channels and their characteristics.
  235. */
  236. #define PPP_MP_MAX_QLEN 128
  237. /* Multilink header bits. */
  238. #define B 0x80 /* this fragment begins a packet */
  239. #define E 0x40 /* this fragment ends a packet */
  240. /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
  241. #define seq_before(a, b) ((s32)((a) - (b)) < 0)
  242. #define seq_after(a, b) ((s32)((a) - (b)) > 0)
  243. /* Prototypes. */
  244. static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
  245. struct file *file, unsigned int cmd, unsigned long arg);
  246. static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb);
  247. static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
  248. static void ppp_push(struct ppp *ppp);
  249. static void ppp_channel_push(struct channel *pch);
  250. static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
  251. struct channel *pch);
  252. static void ppp_receive_error(struct ppp *ppp);
  253. static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
  254. static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
  255. struct sk_buff *skb);
  256. #ifdef CONFIG_PPP_MULTILINK
  257. static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
  258. struct channel *pch);
  259. static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
  260. static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
  261. static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
  262. #endif /* CONFIG_PPP_MULTILINK */
  263. static int ppp_set_compress(struct ppp *ppp, struct ppp_option_data *data);
  264. static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
  265. static void ppp_ccp_closed(struct ppp *ppp);
  266. static struct compressor *find_compressor(int type);
  267. static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
  268. static int ppp_create_interface(struct net *net, struct file *file, int *unit);
  269. static void init_ppp_file(struct ppp_file *pf, int kind);
  270. static void ppp_destroy_interface(struct ppp *ppp);
  271. static struct ppp *ppp_find_unit(struct ppp_net *pn, int unit);
  272. static struct channel *ppp_find_channel(struct ppp_net *pn, int unit);
  273. static int ppp_connect_channel(struct channel *pch, int unit);
  274. static int ppp_disconnect_channel(struct channel *pch);
  275. static void ppp_destroy_channel(struct channel *pch);
  276. static int unit_get(struct idr *p, void *ptr, int min);
  277. static int unit_set(struct idr *p, void *ptr, int n);
  278. static void unit_put(struct idr *p, int n);
  279. static void *unit_find(struct idr *p, int n);
  280. static void ppp_setup(struct net_device *dev);
  281. static const struct net_device_ops ppp_netdev_ops;
  282. static const struct class ppp_class = {
  283. .name = "ppp",
  284. };
  285. /* per net-namespace data */
  286. static inline struct ppp_net *ppp_pernet(struct net *net)
  287. {
  288. return net_generic(net, ppp_net_id);
  289. }
  290. /* Translates a PPP protocol number to a NP index (NP == network protocol) */
  291. static inline int proto_to_npindex(int proto)
  292. {
  293. switch (proto) {
  294. case PPP_IP:
  295. return NP_IP;
  296. case PPP_IPV6:
  297. return NP_IPV6;
  298. case PPP_IPX:
  299. return NP_IPX;
  300. case PPP_AT:
  301. return NP_AT;
  302. case PPP_MPLS_UC:
  303. return NP_MPLS_UC;
  304. case PPP_MPLS_MC:
  305. return NP_MPLS_MC;
  306. }
  307. return -EINVAL;
  308. }
  309. /* Translates an NP index into a PPP protocol number */
  310. static const int npindex_to_proto[NUM_NP] = {
  311. PPP_IP,
  312. PPP_IPV6,
  313. PPP_IPX,
  314. PPP_AT,
  315. PPP_MPLS_UC,
  316. PPP_MPLS_MC,
  317. };
  318. /* Translates an ethertype into an NP index */
  319. static inline int ethertype_to_npindex(int ethertype)
  320. {
  321. switch (ethertype) {
  322. case ETH_P_IP:
  323. return NP_IP;
  324. case ETH_P_IPV6:
  325. return NP_IPV6;
  326. case ETH_P_IPX:
  327. return NP_IPX;
  328. case ETH_P_PPPTALK:
  329. case ETH_P_ATALK:
  330. return NP_AT;
  331. case ETH_P_MPLS_UC:
  332. return NP_MPLS_UC;
  333. case ETH_P_MPLS_MC:
  334. return NP_MPLS_MC;
  335. }
  336. return -1;
  337. }
  338. /* Translates an NP index into an ethertype */
  339. static const int npindex_to_ethertype[NUM_NP] = {
  340. ETH_P_IP,
  341. ETH_P_IPV6,
  342. ETH_P_IPX,
  343. ETH_P_PPPTALK,
  344. ETH_P_MPLS_UC,
  345. ETH_P_MPLS_MC,
  346. };
  347. /*
  348. * Locking shorthand.
  349. */
  350. #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
  351. #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
  352. #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
  353. #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
  354. #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
  355. ppp_recv_lock(ppp); } while (0)
  356. #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
  357. ppp_xmit_unlock(ppp); } while (0)
  358. /*
  359. * /dev/ppp device routines.
  360. * The /dev/ppp device is used by pppd to control the ppp unit.
  361. * It supports the read, write, ioctl and poll functions.
  362. * Open instances of /dev/ppp can be in one of three states:
  363. * unattached, attached to a ppp unit, or attached to a ppp channel.
  364. */
  365. static int ppp_open(struct inode *inode, struct file *file)
  366. {
  367. /*
  368. * This could (should?) be enforced by the permissions on /dev/ppp.
  369. */
  370. if (!ns_capable(file->f_cred->user_ns, CAP_NET_ADMIN))
  371. return -EPERM;
  372. return 0;
  373. }
  374. static int ppp_release(struct inode *unused, struct file *file)
  375. {
  376. struct ppp_file *pf = file->private_data;
  377. struct ppp *ppp;
  378. if (pf) {
  379. file->private_data = NULL;
  380. if (pf->kind == INTERFACE) {
  381. ppp = PF_TO_PPP(pf);
  382. rtnl_lock();
  383. if (file == ppp->owner)
  384. unregister_netdevice(ppp->dev);
  385. rtnl_unlock();
  386. }
  387. if (refcount_dec_and_test(&pf->refcnt)) {
  388. switch (pf->kind) {
  389. case INTERFACE:
  390. ppp_destroy_interface(PF_TO_PPP(pf));
  391. break;
  392. case CHANNEL:
  393. ppp_destroy_channel(PF_TO_CHANNEL(pf));
  394. break;
  395. }
  396. }
  397. }
  398. return 0;
  399. }
  400. static ssize_t ppp_read(struct file *file, char __user *buf,
  401. size_t count, loff_t *ppos)
  402. {
  403. struct ppp_file *pf = file->private_data;
  404. DECLARE_WAITQUEUE(wait, current);
  405. ssize_t ret;
  406. struct sk_buff *skb = NULL;
  407. struct iovec iov;
  408. struct iov_iter to;
  409. ret = count;
  410. if (!pf)
  411. return -ENXIO;
  412. add_wait_queue(&pf->rwait, &wait);
  413. for (;;) {
  414. set_current_state(TASK_INTERRUPTIBLE);
  415. skb = skb_dequeue(&pf->rq);
  416. if (skb)
  417. break;
  418. ret = 0;
  419. if (pf->dead)
  420. break;
  421. if (pf->kind == INTERFACE) {
  422. /*
  423. * Return 0 (EOF) on an interface that has no
  424. * channels connected, unless it is looping
  425. * network traffic (demand mode).
  426. */
  427. struct ppp *ppp = PF_TO_PPP(pf);
  428. ppp_recv_lock(ppp);
  429. if (ppp->n_channels == 0 &&
  430. (ppp->flags & SC_LOOP_TRAFFIC) == 0) {
  431. ppp_recv_unlock(ppp);
  432. break;
  433. }
  434. ppp_recv_unlock(ppp);
  435. }
  436. ret = -EAGAIN;
  437. if (file->f_flags & O_NONBLOCK)
  438. break;
  439. ret = -ERESTARTSYS;
  440. if (signal_pending(current))
  441. break;
  442. schedule();
  443. }
  444. set_current_state(TASK_RUNNING);
  445. remove_wait_queue(&pf->rwait, &wait);
  446. if (!skb)
  447. goto out;
  448. ret = -EOVERFLOW;
  449. if (skb->len > count)
  450. goto outf;
  451. ret = -EFAULT;
  452. iov.iov_base = buf;
  453. iov.iov_len = count;
  454. iov_iter_init(&to, ITER_DEST, &iov, 1, count);
  455. if (skb_copy_datagram_iter(skb, 0, &to, skb->len))
  456. goto outf;
  457. ret = skb->len;
  458. outf:
  459. kfree_skb(skb);
  460. out:
  461. return ret;
  462. }
  463. static bool ppp_check_packet(struct sk_buff *skb, size_t count)
  464. {
  465. /* LCP packets must include LCP header which 4 bytes long:
  466. * 1-byte code, 1-byte identifier, and 2-byte length.
  467. */
  468. return get_unaligned_be16(skb->data) != PPP_LCP ||
  469. count >= PPP_PROTO_LEN + PPP_LCP_HDRLEN;
  470. }
  471. static ssize_t ppp_write(struct file *file, const char __user *buf,
  472. size_t count, loff_t *ppos)
  473. {
  474. struct ppp_file *pf = file->private_data;
  475. struct sk_buff *skb;
  476. ssize_t ret;
  477. if (!pf)
  478. return -ENXIO;
  479. /* All PPP packets should start with the 2-byte protocol */
  480. if (count < PPP_PROTO_LEN)
  481. return -EINVAL;
  482. ret = -ENOMEM;
  483. skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
  484. if (!skb)
  485. goto out;
  486. skb_reserve(skb, pf->hdrlen);
  487. ret = -EFAULT;
  488. if (copy_from_user(skb_put(skb, count), buf, count)) {
  489. kfree_skb(skb);
  490. goto out;
  491. }
  492. ret = -EINVAL;
  493. if (unlikely(!ppp_check_packet(skb, count))) {
  494. kfree_skb(skb);
  495. goto out;
  496. }
  497. switch (pf->kind) {
  498. case INTERFACE:
  499. ppp_xmit_process(PF_TO_PPP(pf), skb);
  500. break;
  501. case CHANNEL:
  502. skb_queue_tail(&pf->xq, skb);
  503. ppp_channel_push(PF_TO_CHANNEL(pf));
  504. break;
  505. }
  506. ret = count;
  507. out:
  508. return ret;
  509. }
  510. /* No kernel lock - fine */
  511. static __poll_t ppp_poll(struct file *file, poll_table *wait)
  512. {
  513. struct ppp_file *pf = file->private_data;
  514. __poll_t mask;
  515. if (!pf)
  516. return 0;
  517. poll_wait(file, &pf->rwait, wait);
  518. mask = EPOLLOUT | EPOLLWRNORM;
  519. if (skb_peek(&pf->rq))
  520. mask |= EPOLLIN | EPOLLRDNORM;
  521. if (pf->dead)
  522. mask |= EPOLLHUP;
  523. else if (pf->kind == INTERFACE) {
  524. /* see comment in ppp_read */
  525. struct ppp *ppp = PF_TO_PPP(pf);
  526. ppp_recv_lock(ppp);
  527. if (ppp->n_channels == 0 &&
  528. (ppp->flags & SC_LOOP_TRAFFIC) == 0)
  529. mask |= EPOLLIN | EPOLLRDNORM;
  530. ppp_recv_unlock(ppp);
  531. }
  532. return mask;
  533. }
  534. #ifdef CONFIG_PPP_FILTER
  535. static struct bpf_prog *get_filter(struct sock_fprog *uprog)
  536. {
  537. struct sock_fprog_kern fprog;
  538. struct bpf_prog *res = NULL;
  539. int err;
  540. if (!uprog->len)
  541. return NULL;
  542. /* uprog->len is unsigned short, so no overflow here */
  543. fprog.len = uprog->len;
  544. fprog.filter = memdup_array_user(uprog->filter,
  545. uprog->len, sizeof(struct sock_filter));
  546. if (IS_ERR(fprog.filter))
  547. return ERR_CAST(fprog.filter);
  548. err = bpf_prog_create(&res, &fprog);
  549. kfree(fprog.filter);
  550. return err ? ERR_PTR(err) : res;
  551. }
  552. static struct bpf_prog *ppp_get_filter(struct sock_fprog __user *p)
  553. {
  554. struct sock_fprog uprog;
  555. if (copy_from_user(&uprog, p, sizeof(struct sock_fprog)))
  556. return ERR_PTR(-EFAULT);
  557. return get_filter(&uprog);
  558. }
  559. #ifdef CONFIG_COMPAT
  560. struct sock_fprog32 {
  561. unsigned short len;
  562. compat_caddr_t filter;
  563. };
  564. #define PPPIOCSPASS32 _IOW('t', 71, struct sock_fprog32)
  565. #define PPPIOCSACTIVE32 _IOW('t', 70, struct sock_fprog32)
  566. static struct bpf_prog *compat_ppp_get_filter(struct sock_fprog32 __user *p)
  567. {
  568. struct sock_fprog32 uprog32;
  569. struct sock_fprog uprog;
  570. if (copy_from_user(&uprog32, p, sizeof(struct sock_fprog32)))
  571. return ERR_PTR(-EFAULT);
  572. uprog.len = uprog32.len;
  573. uprog.filter = compat_ptr(uprog32.filter);
  574. return get_filter(&uprog);
  575. }
  576. #endif
  577. #endif
  578. /* Bridge one PPP channel to another.
  579. * When two channels are bridged, ppp_input on one channel is redirected to
  580. * the other's ops->start_xmit handler.
  581. * In order to safely bridge channels we must reject channels which are already
  582. * part of a bridge instance, or which form part of an existing unit.
  583. * Once successfully bridged, each channel holds a reference on the other
  584. * to prevent it being freed while the bridge is extant.
  585. */
  586. static int ppp_bridge_channels(struct channel *pch, struct channel *pchb)
  587. {
  588. write_lock_bh(&pch->upl);
  589. if (pch->ppp ||
  590. rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl))) {
  591. write_unlock_bh(&pch->upl);
  592. return -EALREADY;
  593. }
  594. refcount_inc(&pchb->file.refcnt);
  595. rcu_assign_pointer(pch->bridge, pchb);
  596. write_unlock_bh(&pch->upl);
  597. write_lock_bh(&pchb->upl);
  598. if (pchb->ppp ||
  599. rcu_dereference_protected(pchb->bridge, lockdep_is_held(&pchb->upl))) {
  600. write_unlock_bh(&pchb->upl);
  601. goto err_unset;
  602. }
  603. refcount_inc(&pch->file.refcnt);
  604. rcu_assign_pointer(pchb->bridge, pch);
  605. write_unlock_bh(&pchb->upl);
  606. return 0;
  607. err_unset:
  608. write_lock_bh(&pch->upl);
  609. /* Re-read pch->bridge with upl held in case it was modified concurrently */
  610. pchb = rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl));
  611. RCU_INIT_POINTER(pch->bridge, NULL);
  612. write_unlock_bh(&pch->upl);
  613. synchronize_rcu();
  614. if (pchb)
  615. if (refcount_dec_and_test(&pchb->file.refcnt))
  616. ppp_destroy_channel(pchb);
  617. return -EALREADY;
  618. }
  619. static int ppp_unbridge_channels(struct channel *pch)
  620. {
  621. struct channel *pchb, *pchbb;
  622. write_lock_bh(&pch->upl);
  623. pchb = rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl));
  624. if (!pchb) {
  625. write_unlock_bh(&pch->upl);
  626. return -EINVAL;
  627. }
  628. RCU_INIT_POINTER(pch->bridge, NULL);
  629. write_unlock_bh(&pch->upl);
  630. /* Only modify pchb if phcb->bridge points back to pch.
  631. * If not, it implies that there has been a race unbridging (and possibly
  632. * even rebridging) pchb. We should leave pchb alone to avoid either a
  633. * refcount underflow, or breaking another established bridge instance.
  634. */
  635. write_lock_bh(&pchb->upl);
  636. pchbb = rcu_dereference_protected(pchb->bridge, lockdep_is_held(&pchb->upl));
  637. if (pchbb == pch)
  638. RCU_INIT_POINTER(pchb->bridge, NULL);
  639. write_unlock_bh(&pchb->upl);
  640. synchronize_rcu();
  641. if (pchbb == pch)
  642. if (refcount_dec_and_test(&pch->file.refcnt))
  643. ppp_destroy_channel(pch);
  644. if (refcount_dec_and_test(&pchb->file.refcnt))
  645. ppp_destroy_channel(pchb);
  646. return 0;
  647. }
  648. static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  649. {
  650. struct ppp_file *pf;
  651. struct ppp *ppp;
  652. int err = -EFAULT, val, val2, i;
  653. struct ppp_idle32 idle32;
  654. struct ppp_idle64 idle64;
  655. struct npioctl npi;
  656. int unit, cflags;
  657. struct slcompress *vj;
  658. void __user *argp = (void __user *)arg;
  659. int __user *p = argp;
  660. mutex_lock(&ppp_mutex);
  661. pf = file->private_data;
  662. if (!pf) {
  663. err = ppp_unattached_ioctl(current->nsproxy->net_ns,
  664. pf, file, cmd, arg);
  665. goto out;
  666. }
  667. if (cmd == PPPIOCDETACH) {
  668. /*
  669. * PPPIOCDETACH is no longer supported as it was heavily broken,
  670. * and is only known to have been used by pppd older than
  671. * ppp-2.4.2 (released November 2003).
  672. */
  673. pr_warn_once("%s (%d) used obsolete PPPIOCDETACH ioctl\n",
  674. current->comm, current->pid);
  675. err = -EINVAL;
  676. goto out;
  677. }
  678. if (pf->kind == CHANNEL) {
  679. struct channel *pch, *pchb;
  680. struct ppp_channel *chan;
  681. struct ppp_net *pn;
  682. pch = PF_TO_CHANNEL(pf);
  683. switch (cmd) {
  684. case PPPIOCCONNECT:
  685. if (get_user(unit, p))
  686. break;
  687. err = ppp_connect_channel(pch, unit);
  688. break;
  689. case PPPIOCDISCONN:
  690. err = ppp_disconnect_channel(pch);
  691. break;
  692. case PPPIOCBRIDGECHAN:
  693. if (get_user(unit, p))
  694. break;
  695. err = -ENXIO;
  696. pn = ppp_pernet(current->nsproxy->net_ns);
  697. spin_lock_bh(&pn->all_channels_lock);
  698. pchb = ppp_find_channel(pn, unit);
  699. /* Hold a reference to prevent pchb being freed while
  700. * we establish the bridge.
  701. */
  702. if (pchb)
  703. refcount_inc(&pchb->file.refcnt);
  704. spin_unlock_bh(&pn->all_channels_lock);
  705. if (!pchb)
  706. break;
  707. err = ppp_bridge_channels(pch, pchb);
  708. /* Drop earlier refcount now bridge establishment is complete */
  709. if (refcount_dec_and_test(&pchb->file.refcnt))
  710. ppp_destroy_channel(pchb);
  711. break;
  712. case PPPIOCUNBRIDGECHAN:
  713. err = ppp_unbridge_channels(pch);
  714. break;
  715. default:
  716. down_read(&pch->chan_sem);
  717. chan = pch->chan;
  718. err = -ENOTTY;
  719. if (chan && chan->ops->ioctl)
  720. err = chan->ops->ioctl(chan, cmd, arg);
  721. up_read(&pch->chan_sem);
  722. }
  723. goto out;
  724. }
  725. if (pf->kind != INTERFACE) {
  726. /* can't happen */
  727. pr_err("PPP: not interface or channel??\n");
  728. err = -EINVAL;
  729. goto out;
  730. }
  731. ppp = PF_TO_PPP(pf);
  732. switch (cmd) {
  733. case PPPIOCSMRU:
  734. if (get_user(val, p))
  735. break;
  736. ppp->mru = val;
  737. err = 0;
  738. break;
  739. case PPPIOCSFLAGS:
  740. if (get_user(val, p))
  741. break;
  742. ppp_lock(ppp);
  743. cflags = ppp->flags & ~val;
  744. #ifdef CONFIG_PPP_MULTILINK
  745. if (!(ppp->flags & SC_MULTILINK) && (val & SC_MULTILINK))
  746. ppp->nextseq = 0;
  747. #endif
  748. ppp->flags = val & SC_FLAG_BITS;
  749. ppp_unlock(ppp);
  750. if (cflags & SC_CCP_OPEN)
  751. ppp_ccp_closed(ppp);
  752. err = 0;
  753. break;
  754. case PPPIOCGFLAGS:
  755. val = ppp->flags | ppp->xstate | ppp->rstate;
  756. if (put_user(val, p))
  757. break;
  758. err = 0;
  759. break;
  760. case PPPIOCSCOMPRESS:
  761. {
  762. struct ppp_option_data data;
  763. if (copy_from_user(&data, argp, sizeof(data)))
  764. err = -EFAULT;
  765. else
  766. err = ppp_set_compress(ppp, &data);
  767. break;
  768. }
  769. case PPPIOCGUNIT:
  770. if (put_user(ppp->file.index, p))
  771. break;
  772. err = 0;
  773. break;
  774. case PPPIOCSDEBUG:
  775. if (get_user(val, p))
  776. break;
  777. ppp->debug = val;
  778. err = 0;
  779. break;
  780. case PPPIOCGDEBUG:
  781. if (put_user(ppp->debug, p))
  782. break;
  783. err = 0;
  784. break;
  785. case PPPIOCGIDLE32:
  786. idle32.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
  787. idle32.recv_idle = (jiffies - ppp->last_recv) / HZ;
  788. if (copy_to_user(argp, &idle32, sizeof(idle32)))
  789. break;
  790. err = 0;
  791. break;
  792. case PPPIOCGIDLE64:
  793. idle64.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
  794. idle64.recv_idle = (jiffies - ppp->last_recv) / HZ;
  795. if (copy_to_user(argp, &idle64, sizeof(idle64)))
  796. break;
  797. err = 0;
  798. break;
  799. case PPPIOCSMAXCID:
  800. if (get_user(val, p))
  801. break;
  802. val2 = 15;
  803. if ((val >> 16) != 0) {
  804. val2 = val >> 16;
  805. val &= 0xffff;
  806. }
  807. vj = slhc_init(val2+1, val+1);
  808. if (IS_ERR(vj)) {
  809. err = PTR_ERR(vj);
  810. break;
  811. }
  812. ppp_lock(ppp);
  813. if (ppp->vj)
  814. slhc_free(ppp->vj);
  815. ppp->vj = vj;
  816. ppp_unlock(ppp);
  817. err = 0;
  818. break;
  819. case PPPIOCGNPMODE:
  820. case PPPIOCSNPMODE:
  821. if (copy_from_user(&npi, argp, sizeof(npi)))
  822. break;
  823. err = proto_to_npindex(npi.protocol);
  824. if (err < 0)
  825. break;
  826. i = err;
  827. if (cmd == PPPIOCGNPMODE) {
  828. err = -EFAULT;
  829. npi.mode = ppp->npmode[i];
  830. if (copy_to_user(argp, &npi, sizeof(npi)))
  831. break;
  832. } else {
  833. ppp->npmode[i] = npi.mode;
  834. /* we may be able to transmit more packets now (??) */
  835. netif_wake_queue(ppp->dev);
  836. }
  837. err = 0;
  838. break;
  839. #ifdef CONFIG_PPP_FILTER
  840. case PPPIOCSPASS:
  841. case PPPIOCSACTIVE:
  842. {
  843. struct bpf_prog *filter = ppp_get_filter(argp);
  844. struct bpf_prog **which;
  845. if (IS_ERR(filter)) {
  846. err = PTR_ERR(filter);
  847. break;
  848. }
  849. if (cmd == PPPIOCSPASS)
  850. which = &ppp->pass_filter;
  851. else
  852. which = &ppp->active_filter;
  853. ppp_lock(ppp);
  854. if (*which)
  855. bpf_prog_destroy(*which);
  856. *which = filter;
  857. ppp_unlock(ppp);
  858. err = 0;
  859. break;
  860. }
  861. #endif /* CONFIG_PPP_FILTER */
  862. #ifdef CONFIG_PPP_MULTILINK
  863. case PPPIOCSMRRU:
  864. if (get_user(val, p))
  865. break;
  866. ppp_recv_lock(ppp);
  867. ppp->mrru = val;
  868. ppp_recv_unlock(ppp);
  869. err = 0;
  870. break;
  871. #endif /* CONFIG_PPP_MULTILINK */
  872. default:
  873. err = -ENOTTY;
  874. }
  875. out:
  876. mutex_unlock(&ppp_mutex);
  877. return err;
  878. }
  879. #ifdef CONFIG_COMPAT
  880. struct ppp_option_data32 {
  881. compat_uptr_t ptr;
  882. u32 length;
  883. compat_int_t transmit;
  884. };
  885. #define PPPIOCSCOMPRESS32 _IOW('t', 77, struct ppp_option_data32)
  886. static long ppp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  887. {
  888. struct ppp_file *pf;
  889. int err = -ENOIOCTLCMD;
  890. void __user *argp = (void __user *)arg;
  891. mutex_lock(&ppp_mutex);
  892. pf = file->private_data;
  893. if (pf && pf->kind == INTERFACE) {
  894. struct ppp *ppp = PF_TO_PPP(pf);
  895. switch (cmd) {
  896. #ifdef CONFIG_PPP_FILTER
  897. case PPPIOCSPASS32:
  898. case PPPIOCSACTIVE32:
  899. {
  900. struct bpf_prog *filter = compat_ppp_get_filter(argp);
  901. struct bpf_prog **which;
  902. if (IS_ERR(filter)) {
  903. err = PTR_ERR(filter);
  904. break;
  905. }
  906. if (cmd == PPPIOCSPASS32)
  907. which = &ppp->pass_filter;
  908. else
  909. which = &ppp->active_filter;
  910. ppp_lock(ppp);
  911. if (*which)
  912. bpf_prog_destroy(*which);
  913. *which = filter;
  914. ppp_unlock(ppp);
  915. err = 0;
  916. break;
  917. }
  918. #endif /* CONFIG_PPP_FILTER */
  919. case PPPIOCSCOMPRESS32:
  920. {
  921. struct ppp_option_data32 data32;
  922. if (copy_from_user(&data32, argp, sizeof(data32))) {
  923. err = -EFAULT;
  924. } else {
  925. struct ppp_option_data data = {
  926. .ptr = compat_ptr(data32.ptr),
  927. .length = data32.length,
  928. .transmit = data32.transmit
  929. };
  930. err = ppp_set_compress(ppp, &data);
  931. }
  932. break;
  933. }
  934. }
  935. }
  936. mutex_unlock(&ppp_mutex);
  937. /* all other commands have compatible arguments */
  938. if (err == -ENOIOCTLCMD)
  939. err = ppp_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  940. return err;
  941. }
  942. #endif
  943. static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
  944. struct file *file, unsigned int cmd, unsigned long arg)
  945. {
  946. int unit, err = -EFAULT;
  947. struct ppp *ppp;
  948. struct channel *chan;
  949. struct ppp_net *pn;
  950. int __user *p = (int __user *)arg;
  951. switch (cmd) {
  952. case PPPIOCNEWUNIT:
  953. /* Create a new ppp unit */
  954. if (get_user(unit, p))
  955. break;
  956. err = ppp_create_interface(net, file, &unit);
  957. if (err < 0)
  958. break;
  959. err = -EFAULT;
  960. if (put_user(unit, p))
  961. break;
  962. err = 0;
  963. break;
  964. case PPPIOCATTACH:
  965. /* Attach to an existing ppp unit */
  966. if (get_user(unit, p))
  967. break;
  968. err = -ENXIO;
  969. pn = ppp_pernet(net);
  970. mutex_lock(&pn->all_ppp_mutex);
  971. ppp = ppp_find_unit(pn, unit);
  972. if (ppp) {
  973. refcount_inc(&ppp->file.refcnt);
  974. file->private_data = &ppp->file;
  975. err = 0;
  976. }
  977. mutex_unlock(&pn->all_ppp_mutex);
  978. break;
  979. case PPPIOCATTCHAN:
  980. if (get_user(unit, p))
  981. break;
  982. err = -ENXIO;
  983. pn = ppp_pernet(net);
  984. spin_lock_bh(&pn->all_channels_lock);
  985. chan = ppp_find_channel(pn, unit);
  986. if (chan) {
  987. refcount_inc(&chan->file.refcnt);
  988. file->private_data = &chan->file;
  989. err = 0;
  990. }
  991. spin_unlock_bh(&pn->all_channels_lock);
  992. break;
  993. default:
  994. err = -ENOTTY;
  995. }
  996. return err;
  997. }
  998. static const struct file_operations ppp_device_fops = {
  999. .owner = THIS_MODULE,
  1000. .read = ppp_read,
  1001. .write = ppp_write,
  1002. .poll = ppp_poll,
  1003. .unlocked_ioctl = ppp_ioctl,
  1004. #ifdef CONFIG_COMPAT
  1005. .compat_ioctl = ppp_compat_ioctl,
  1006. #endif
  1007. .open = ppp_open,
  1008. .release = ppp_release,
  1009. .llseek = noop_llseek,
  1010. };
  1011. static __net_init int ppp_init_net(struct net *net)
  1012. {
  1013. struct ppp_net *pn = net_generic(net, ppp_net_id);
  1014. idr_init(&pn->units_idr);
  1015. mutex_init(&pn->all_ppp_mutex);
  1016. INIT_LIST_HEAD(&pn->all_channels);
  1017. INIT_LIST_HEAD(&pn->new_channels);
  1018. spin_lock_init(&pn->all_channels_lock);
  1019. return 0;
  1020. }
  1021. static __net_exit void ppp_exit_net(struct net *net)
  1022. {
  1023. struct ppp_net *pn = net_generic(net, ppp_net_id);
  1024. struct net_device *dev;
  1025. struct net_device *aux;
  1026. struct ppp *ppp;
  1027. LIST_HEAD(list);
  1028. int id;
  1029. rtnl_lock();
  1030. for_each_netdev_safe(net, dev, aux) {
  1031. if (dev->netdev_ops == &ppp_netdev_ops)
  1032. unregister_netdevice_queue(dev, &list);
  1033. }
  1034. idr_for_each_entry(&pn->units_idr, ppp, id)
  1035. /* Skip devices already unregistered by previous loop */
  1036. if (!net_eq(dev_net(ppp->dev), net))
  1037. unregister_netdevice_queue(ppp->dev, &list);
  1038. unregister_netdevice_many(&list);
  1039. rtnl_unlock();
  1040. mutex_destroy(&pn->all_ppp_mutex);
  1041. idr_destroy(&pn->units_idr);
  1042. WARN_ON_ONCE(!list_empty(&pn->all_channels));
  1043. WARN_ON_ONCE(!list_empty(&pn->new_channels));
  1044. }
  1045. static struct pernet_operations ppp_net_ops = {
  1046. .init = ppp_init_net,
  1047. .exit = ppp_exit_net,
  1048. .id = &ppp_net_id,
  1049. .size = sizeof(struct ppp_net),
  1050. };
  1051. static int ppp_unit_register(struct ppp *ppp, int unit, bool ifname_is_set)
  1052. {
  1053. struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
  1054. int ret;
  1055. mutex_lock(&pn->all_ppp_mutex);
  1056. if (unit < 0) {
  1057. ret = unit_get(&pn->units_idr, ppp, 0);
  1058. if (ret < 0)
  1059. goto err;
  1060. if (!ifname_is_set) {
  1061. while (1) {
  1062. snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ret);
  1063. if (!netdev_name_in_use(ppp->ppp_net, ppp->dev->name))
  1064. break;
  1065. unit_put(&pn->units_idr, ret);
  1066. ret = unit_get(&pn->units_idr, ppp, ret + 1);
  1067. if (ret < 0)
  1068. goto err;
  1069. }
  1070. }
  1071. } else {
  1072. /* Caller asked for a specific unit number. Fail with -EEXIST
  1073. * if unavailable. For backward compatibility, return -EEXIST
  1074. * too if idr allocation fails; this makes pppd retry without
  1075. * requesting a specific unit number.
  1076. */
  1077. if (unit_find(&pn->units_idr, unit)) {
  1078. ret = -EEXIST;
  1079. goto err;
  1080. }
  1081. ret = unit_set(&pn->units_idr, ppp, unit);
  1082. if (ret < 0) {
  1083. /* Rewrite error for backward compatibility */
  1084. ret = -EEXIST;
  1085. goto err;
  1086. }
  1087. }
  1088. ppp->file.index = ret;
  1089. if (!ifname_is_set)
  1090. snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ppp->file.index);
  1091. mutex_unlock(&pn->all_ppp_mutex);
  1092. ret = register_netdevice(ppp->dev);
  1093. if (ret < 0)
  1094. goto err_unit;
  1095. atomic_inc(&ppp_unit_count);
  1096. return 0;
  1097. err_unit:
  1098. mutex_lock(&pn->all_ppp_mutex);
  1099. unit_put(&pn->units_idr, ppp->file.index);
  1100. err:
  1101. mutex_unlock(&pn->all_ppp_mutex);
  1102. return ret;
  1103. }
  1104. static int ppp_dev_configure(struct net *src_net, struct net_device *dev,
  1105. const struct ppp_config *conf)
  1106. {
  1107. struct ppp *ppp = netdev_priv(dev);
  1108. int indx;
  1109. int err;
  1110. int cpu;
  1111. ppp->dev = dev;
  1112. ppp->ppp_net = src_net;
  1113. ppp->mru = PPP_MRU;
  1114. ppp->owner = conf->file;
  1115. init_ppp_file(&ppp->file, INTERFACE);
  1116. ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
  1117. for (indx = 0; indx < NUM_NP; ++indx)
  1118. ppp->npmode[indx] = NPMODE_PASS;
  1119. INIT_LIST_HEAD(&ppp->channels);
  1120. spin_lock_init(&ppp->rlock);
  1121. spin_lock_init(&ppp->wlock);
  1122. ppp->xmit_recursion = alloc_percpu(int);
  1123. if (!ppp->xmit_recursion) {
  1124. err = -ENOMEM;
  1125. goto err1;
  1126. }
  1127. for_each_possible_cpu(cpu)
  1128. (*per_cpu_ptr(ppp->xmit_recursion, cpu)) = 0;
  1129. #ifdef CONFIG_PPP_MULTILINK
  1130. ppp->minseq = -1;
  1131. skb_queue_head_init(&ppp->mrq);
  1132. #endif /* CONFIG_PPP_MULTILINK */
  1133. #ifdef CONFIG_PPP_FILTER
  1134. ppp->pass_filter = NULL;
  1135. ppp->active_filter = NULL;
  1136. #endif /* CONFIG_PPP_FILTER */
  1137. err = ppp_unit_register(ppp, conf->unit, conf->ifname_is_set);
  1138. if (err < 0)
  1139. goto err2;
  1140. conf->file->private_data = &ppp->file;
  1141. return 0;
  1142. err2:
  1143. free_percpu(ppp->xmit_recursion);
  1144. err1:
  1145. return err;
  1146. }
  1147. static const struct nla_policy ppp_nl_policy[IFLA_PPP_MAX + 1] = {
  1148. [IFLA_PPP_DEV_FD] = { .type = NLA_S32 },
  1149. };
  1150. static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[],
  1151. struct netlink_ext_ack *extack)
  1152. {
  1153. if (!data)
  1154. return -EINVAL;
  1155. if (!data[IFLA_PPP_DEV_FD])
  1156. return -EINVAL;
  1157. if (nla_get_s32(data[IFLA_PPP_DEV_FD]) < 0)
  1158. return -EBADF;
  1159. return 0;
  1160. }
  1161. static int ppp_nl_newlink(struct net *src_net, struct net_device *dev,
  1162. struct nlattr *tb[], struct nlattr *data[],
  1163. struct netlink_ext_ack *extack)
  1164. {
  1165. struct ppp_config conf = {
  1166. .unit = -1,
  1167. .ifname_is_set = true,
  1168. };
  1169. struct file *file;
  1170. int err;
  1171. file = fget(nla_get_s32(data[IFLA_PPP_DEV_FD]));
  1172. if (!file)
  1173. return -EBADF;
  1174. /* rtnl_lock is already held here, but ppp_create_interface() locks
  1175. * ppp_mutex before holding rtnl_lock. Using mutex_trylock() avoids
  1176. * possible deadlock due to lock order inversion, at the cost of
  1177. * pushing the problem back to userspace.
  1178. */
  1179. if (!mutex_trylock(&ppp_mutex)) {
  1180. err = -EBUSY;
  1181. goto out;
  1182. }
  1183. if (file->f_op != &ppp_device_fops || file->private_data) {
  1184. err = -EBADF;
  1185. goto out_unlock;
  1186. }
  1187. conf.file = file;
  1188. /* Don't use device name generated by the rtnetlink layer when ifname
  1189. * isn't specified. Let ppp_dev_configure() set the device name using
  1190. * the PPP unit identifer as suffix (i.e. ppp<unit_id>). This allows
  1191. * userspace to infer the device name using to the PPPIOCGUNIT ioctl.
  1192. */
  1193. if (!tb[IFLA_IFNAME] || !nla_len(tb[IFLA_IFNAME]) || !*(char *)nla_data(tb[IFLA_IFNAME]))
  1194. conf.ifname_is_set = false;
  1195. err = ppp_dev_configure(src_net, dev, &conf);
  1196. out_unlock:
  1197. mutex_unlock(&ppp_mutex);
  1198. out:
  1199. fput(file);
  1200. return err;
  1201. }
  1202. static void ppp_nl_dellink(struct net_device *dev, struct list_head *head)
  1203. {
  1204. unregister_netdevice_queue(dev, head);
  1205. }
  1206. static size_t ppp_nl_get_size(const struct net_device *dev)
  1207. {
  1208. return 0;
  1209. }
  1210. static int ppp_nl_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1211. {
  1212. return 0;
  1213. }
  1214. static struct net *ppp_nl_get_link_net(const struct net_device *dev)
  1215. {
  1216. struct ppp *ppp = netdev_priv(dev);
  1217. return READ_ONCE(ppp->ppp_net);
  1218. }
  1219. static struct rtnl_link_ops ppp_link_ops __read_mostly = {
  1220. .kind = "ppp",
  1221. .maxtype = IFLA_PPP_MAX,
  1222. .policy = ppp_nl_policy,
  1223. .priv_size = sizeof(struct ppp),
  1224. .setup = ppp_setup,
  1225. .validate = ppp_nl_validate,
  1226. .newlink = ppp_nl_newlink,
  1227. .dellink = ppp_nl_dellink,
  1228. .get_size = ppp_nl_get_size,
  1229. .fill_info = ppp_nl_fill_info,
  1230. .get_link_net = ppp_nl_get_link_net,
  1231. };
  1232. #define PPP_MAJOR 108
  1233. /* Called at boot time if ppp is compiled into the kernel,
  1234. or at module load time (from init_module) if compiled as a module. */
  1235. static int __init ppp_init(void)
  1236. {
  1237. int err;
  1238. pr_info("PPP generic driver version " PPP_VERSION "\n");
  1239. err = register_pernet_device(&ppp_net_ops);
  1240. if (err) {
  1241. pr_err("failed to register PPP pernet device (%d)\n", err);
  1242. goto out;
  1243. }
  1244. err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
  1245. if (err) {
  1246. pr_err("failed to register PPP device (%d)\n", err);
  1247. goto out_net;
  1248. }
  1249. err = class_register(&ppp_class);
  1250. if (err)
  1251. goto out_chrdev;
  1252. err = rtnl_link_register(&ppp_link_ops);
  1253. if (err) {
  1254. pr_err("failed to register rtnetlink PPP handler\n");
  1255. goto out_class;
  1256. }
  1257. /* not a big deal if we fail here :-) */
  1258. device_create(&ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
  1259. return 0;
  1260. out_class:
  1261. class_unregister(&ppp_class);
  1262. out_chrdev:
  1263. unregister_chrdev(PPP_MAJOR, "ppp");
  1264. out_net:
  1265. unregister_pernet_device(&ppp_net_ops);
  1266. out:
  1267. return err;
  1268. }
  1269. /*
  1270. * Network interface unit routines.
  1271. */
  1272. static netdev_tx_t
  1273. ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1274. {
  1275. struct ppp *ppp = netdev_priv(dev);
  1276. int npi, proto;
  1277. unsigned char *pp;
  1278. npi = ethertype_to_npindex(ntohs(skb->protocol));
  1279. if (npi < 0)
  1280. goto outf;
  1281. /* Drop, accept or reject the packet */
  1282. switch (ppp->npmode[npi]) {
  1283. case NPMODE_PASS:
  1284. break;
  1285. case NPMODE_QUEUE:
  1286. /* it would be nice to have a way to tell the network
  1287. system to queue this one up for later. */
  1288. goto outf;
  1289. case NPMODE_DROP:
  1290. case NPMODE_ERROR:
  1291. goto outf;
  1292. }
  1293. /* Put the 2-byte PPP protocol number on the front,
  1294. making sure there is room for the address and control fields. */
  1295. if (skb_cow_head(skb, PPP_HDRLEN))
  1296. goto outf;
  1297. pp = skb_push(skb, 2);
  1298. proto = npindex_to_proto[npi];
  1299. put_unaligned_be16(proto, pp);
  1300. skb_scrub_packet(skb, !net_eq(ppp->ppp_net, dev_net(dev)));
  1301. ppp_xmit_process(ppp, skb);
  1302. return NETDEV_TX_OK;
  1303. outf:
  1304. kfree_skb(skb);
  1305. ++dev->stats.tx_dropped;
  1306. return NETDEV_TX_OK;
  1307. }
  1308. static int
  1309. ppp_net_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
  1310. void __user *addr, int cmd)
  1311. {
  1312. struct ppp *ppp = netdev_priv(dev);
  1313. int err = -EFAULT;
  1314. struct ppp_stats stats;
  1315. struct ppp_comp_stats cstats;
  1316. char *vers;
  1317. switch (cmd) {
  1318. case SIOCGPPPSTATS:
  1319. ppp_get_stats(ppp, &stats);
  1320. if (copy_to_user(addr, &stats, sizeof(stats)))
  1321. break;
  1322. err = 0;
  1323. break;
  1324. case SIOCGPPPCSTATS:
  1325. memset(&cstats, 0, sizeof(cstats));
  1326. if (ppp->xc_state)
  1327. ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
  1328. if (ppp->rc_state)
  1329. ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
  1330. if (copy_to_user(addr, &cstats, sizeof(cstats)))
  1331. break;
  1332. err = 0;
  1333. break;
  1334. case SIOCGPPPVER:
  1335. vers = PPP_VERSION;
  1336. if (copy_to_user(addr, vers, strlen(vers) + 1))
  1337. break;
  1338. err = 0;
  1339. break;
  1340. default:
  1341. err = -EINVAL;
  1342. }
  1343. return err;
  1344. }
  1345. static void
  1346. ppp_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats64)
  1347. {
  1348. struct ppp *ppp = netdev_priv(dev);
  1349. ppp_recv_lock(ppp);
  1350. stats64->rx_packets = ppp->stats64.rx_packets;
  1351. stats64->rx_bytes = ppp->stats64.rx_bytes;
  1352. ppp_recv_unlock(ppp);
  1353. ppp_xmit_lock(ppp);
  1354. stats64->tx_packets = ppp->stats64.tx_packets;
  1355. stats64->tx_bytes = ppp->stats64.tx_bytes;
  1356. ppp_xmit_unlock(ppp);
  1357. stats64->rx_errors = dev->stats.rx_errors;
  1358. stats64->tx_errors = dev->stats.tx_errors;
  1359. stats64->rx_dropped = dev->stats.rx_dropped;
  1360. stats64->tx_dropped = dev->stats.tx_dropped;
  1361. stats64->rx_length_errors = dev->stats.rx_length_errors;
  1362. }
  1363. static int ppp_dev_init(struct net_device *dev)
  1364. {
  1365. struct ppp *ppp;
  1366. netdev_lockdep_set_classes(dev);
  1367. ppp = netdev_priv(dev);
  1368. /* Let the netdevice take a reference on the ppp file. This ensures
  1369. * that ppp_destroy_interface() won't run before the device gets
  1370. * unregistered.
  1371. */
  1372. refcount_inc(&ppp->file.refcnt);
  1373. return 0;
  1374. }
  1375. static void ppp_dev_uninit(struct net_device *dev)
  1376. {
  1377. struct ppp *ppp = netdev_priv(dev);
  1378. struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
  1379. ppp_lock(ppp);
  1380. ppp->closing = 1;
  1381. ppp_unlock(ppp);
  1382. mutex_lock(&pn->all_ppp_mutex);
  1383. unit_put(&pn->units_idr, ppp->file.index);
  1384. mutex_unlock(&pn->all_ppp_mutex);
  1385. ppp->owner = NULL;
  1386. ppp->file.dead = 1;
  1387. wake_up_interruptible(&ppp->file.rwait);
  1388. }
  1389. static void ppp_dev_priv_destructor(struct net_device *dev)
  1390. {
  1391. struct ppp *ppp;
  1392. ppp = netdev_priv(dev);
  1393. if (refcount_dec_and_test(&ppp->file.refcnt))
  1394. ppp_destroy_interface(ppp);
  1395. }
  1396. static int ppp_fill_forward_path(struct net_device_path_ctx *ctx,
  1397. struct net_device_path *path)
  1398. {
  1399. struct ppp *ppp = netdev_priv(ctx->dev);
  1400. struct ppp_channel *chan;
  1401. struct channel *pch;
  1402. if (ppp->flags & SC_MULTILINK)
  1403. return -EOPNOTSUPP;
  1404. pch = list_first_or_null_rcu(&ppp->channels, struct channel, clist);
  1405. if (!pch)
  1406. return -ENODEV;
  1407. chan = READ_ONCE(pch->chan);
  1408. if (!chan)
  1409. return -ENODEV;
  1410. if (!chan->ops->fill_forward_path)
  1411. return -EOPNOTSUPP;
  1412. return chan->ops->fill_forward_path(ctx, path, chan);
  1413. }
  1414. static const struct net_device_ops ppp_netdev_ops = {
  1415. .ndo_init = ppp_dev_init,
  1416. .ndo_uninit = ppp_dev_uninit,
  1417. .ndo_start_xmit = ppp_start_xmit,
  1418. .ndo_siocdevprivate = ppp_net_siocdevprivate,
  1419. .ndo_get_stats64 = ppp_get_stats64,
  1420. .ndo_fill_forward_path = ppp_fill_forward_path,
  1421. };
  1422. static const struct device_type ppp_type = {
  1423. .name = "ppp",
  1424. };
  1425. static void ppp_setup(struct net_device *dev)
  1426. {
  1427. dev->netdev_ops = &ppp_netdev_ops;
  1428. SET_NETDEV_DEVTYPE(dev, &ppp_type);
  1429. dev->lltx = true;
  1430. dev->hard_header_len = PPP_HDRLEN;
  1431. dev->mtu = PPP_MRU;
  1432. dev->addr_len = 0;
  1433. dev->tx_queue_len = 3;
  1434. dev->type = ARPHRD_PPP;
  1435. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  1436. dev->priv_destructor = ppp_dev_priv_destructor;
  1437. netif_keep_dst(dev);
  1438. }
  1439. /*
  1440. * Transmit-side routines.
  1441. */
  1442. /* Called to do any work queued up on the transmit side that can now be done */
  1443. static void __ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
  1444. {
  1445. ppp_xmit_lock(ppp);
  1446. if (!ppp->closing) {
  1447. ppp_push(ppp);
  1448. if (skb)
  1449. skb_queue_tail(&ppp->file.xq, skb);
  1450. while (!ppp->xmit_pending &&
  1451. (skb = skb_dequeue(&ppp->file.xq)))
  1452. ppp_send_frame(ppp, skb);
  1453. /* If there's no work left to do, tell the core net
  1454. code that we can accept some more. */
  1455. if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
  1456. netif_wake_queue(ppp->dev);
  1457. else
  1458. netif_stop_queue(ppp->dev);
  1459. } else {
  1460. kfree_skb(skb);
  1461. }
  1462. ppp_xmit_unlock(ppp);
  1463. }
  1464. static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
  1465. {
  1466. local_bh_disable();
  1467. if (unlikely(*this_cpu_ptr(ppp->xmit_recursion)))
  1468. goto err;
  1469. (*this_cpu_ptr(ppp->xmit_recursion))++;
  1470. __ppp_xmit_process(ppp, skb);
  1471. (*this_cpu_ptr(ppp->xmit_recursion))--;
  1472. local_bh_enable();
  1473. return;
  1474. err:
  1475. local_bh_enable();
  1476. kfree_skb(skb);
  1477. if (net_ratelimit())
  1478. netdev_err(ppp->dev, "recursion detected\n");
  1479. }
  1480. static inline struct sk_buff *
  1481. pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
  1482. {
  1483. struct sk_buff *new_skb;
  1484. int len;
  1485. int new_skb_size = ppp->dev->mtu +
  1486. ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
  1487. int compressor_skb_size = ppp->dev->mtu +
  1488. ppp->xcomp->comp_extra + PPP_HDRLEN;
  1489. new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
  1490. if (!new_skb) {
  1491. if (net_ratelimit())
  1492. netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n");
  1493. return NULL;
  1494. }
  1495. if (ppp->dev->hard_header_len > PPP_HDRLEN)
  1496. skb_reserve(new_skb,
  1497. ppp->dev->hard_header_len - PPP_HDRLEN);
  1498. /* compressor still expects A/C bytes in hdr */
  1499. len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
  1500. new_skb->data, skb->len + 2,
  1501. compressor_skb_size);
  1502. if (len > 0 && (ppp->flags & SC_CCP_UP)) {
  1503. consume_skb(skb);
  1504. skb = new_skb;
  1505. skb_put(skb, len);
  1506. skb_pull(skb, 2); /* pull off A/C bytes */
  1507. } else if (len == 0) {
  1508. /* didn't compress, or CCP not up yet */
  1509. consume_skb(new_skb);
  1510. new_skb = skb;
  1511. } else {
  1512. /*
  1513. * (len < 0)
  1514. * MPPE requires that we do not send unencrypted
  1515. * frames. The compressor will return -1 if we
  1516. * should drop the frame. We cannot simply test
  1517. * the compress_proto because MPPE and MPPC share
  1518. * the same number.
  1519. */
  1520. if (net_ratelimit())
  1521. netdev_err(ppp->dev, "ppp: compressor dropped pkt\n");
  1522. consume_skb(new_skb);
  1523. new_skb = NULL;
  1524. }
  1525. return new_skb;
  1526. }
  1527. /*
  1528. * Compress and send a frame.
  1529. * The caller should have locked the xmit path,
  1530. * and xmit_pending should be 0.
  1531. */
  1532. static void
  1533. ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
  1534. {
  1535. int proto = PPP_PROTO(skb);
  1536. struct sk_buff *new_skb;
  1537. int len;
  1538. unsigned char *cp;
  1539. skb->dev = ppp->dev;
  1540. if (proto < 0x8000) {
  1541. #ifdef CONFIG_PPP_FILTER
  1542. /* check if the packet passes the pass and active filters.
  1543. * See comment for PPP_FILTER_OUTBOUND_TAG above.
  1544. */
  1545. *(__be16 *)skb_push(skb, 2) = htons(PPP_FILTER_OUTBOUND_TAG);
  1546. if (ppp->pass_filter &&
  1547. bpf_prog_run(ppp->pass_filter, skb) == 0) {
  1548. if (ppp->debug & 1)
  1549. netdev_printk(KERN_DEBUG, ppp->dev,
  1550. "PPP: outbound frame "
  1551. "not passed\n");
  1552. kfree_skb(skb);
  1553. return;
  1554. }
  1555. /* if this packet passes the active filter, record the time */
  1556. if (!(ppp->active_filter &&
  1557. bpf_prog_run(ppp->active_filter, skb) == 0))
  1558. ppp->last_xmit = jiffies;
  1559. skb_pull(skb, 2);
  1560. #else
  1561. /* for data packets, record the time */
  1562. ppp->last_xmit = jiffies;
  1563. #endif /* CONFIG_PPP_FILTER */
  1564. }
  1565. ++ppp->stats64.tx_packets;
  1566. ppp->stats64.tx_bytes += skb->len - PPP_PROTO_LEN;
  1567. switch (proto) {
  1568. case PPP_IP:
  1569. if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
  1570. break;
  1571. /* try to do VJ TCP header compression */
  1572. new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
  1573. GFP_ATOMIC);
  1574. if (!new_skb) {
  1575. netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
  1576. goto drop;
  1577. }
  1578. skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
  1579. cp = skb->data + 2;
  1580. len = slhc_compress(ppp->vj, cp, skb->len - 2,
  1581. new_skb->data + 2, &cp,
  1582. !(ppp->flags & SC_NO_TCP_CCID));
  1583. if (cp == skb->data + 2) {
  1584. /* didn't compress */
  1585. consume_skb(new_skb);
  1586. } else {
  1587. if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
  1588. proto = PPP_VJC_COMP;
  1589. cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
  1590. } else {
  1591. proto = PPP_VJC_UNCOMP;
  1592. cp[0] = skb->data[2];
  1593. }
  1594. consume_skb(skb);
  1595. skb = new_skb;
  1596. cp = skb_put(skb, len + 2);
  1597. cp[0] = 0;
  1598. cp[1] = proto;
  1599. }
  1600. break;
  1601. case PPP_CCP:
  1602. /* peek at outbound CCP frames */
  1603. ppp_ccp_peek(ppp, skb, 0);
  1604. break;
  1605. }
  1606. /* try to do packet compression */
  1607. if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
  1608. proto != PPP_LCP && proto != PPP_CCP) {
  1609. if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
  1610. if (net_ratelimit())
  1611. netdev_err(ppp->dev,
  1612. "ppp: compression required but "
  1613. "down - pkt dropped.\n");
  1614. goto drop;
  1615. }
  1616. new_skb = pad_compress_skb(ppp, skb);
  1617. if (!new_skb)
  1618. goto drop;
  1619. skb = new_skb;
  1620. }
  1621. /*
  1622. * If we are waiting for traffic (demand dialling),
  1623. * queue it up for pppd to receive.
  1624. */
  1625. if (ppp->flags & SC_LOOP_TRAFFIC) {
  1626. if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
  1627. goto drop;
  1628. skb_queue_tail(&ppp->file.rq, skb);
  1629. wake_up_interruptible(&ppp->file.rwait);
  1630. return;
  1631. }
  1632. ppp->xmit_pending = skb;
  1633. ppp_push(ppp);
  1634. return;
  1635. drop:
  1636. kfree_skb(skb);
  1637. ++ppp->dev->stats.tx_errors;
  1638. }
  1639. /*
  1640. * Try to send the frame in xmit_pending.
  1641. * The caller should have the xmit path locked.
  1642. */
  1643. static void
  1644. ppp_push(struct ppp *ppp)
  1645. {
  1646. struct list_head *list;
  1647. struct channel *pch;
  1648. struct sk_buff *skb = ppp->xmit_pending;
  1649. if (!skb)
  1650. return;
  1651. list = &ppp->channels;
  1652. if (list_empty(list)) {
  1653. /* nowhere to send the packet, just drop it */
  1654. ppp->xmit_pending = NULL;
  1655. kfree_skb(skb);
  1656. return;
  1657. }
  1658. if ((ppp->flags & SC_MULTILINK) == 0) {
  1659. /* not doing multilink: send it down the first channel */
  1660. list = list->next;
  1661. pch = list_entry(list, struct channel, clist);
  1662. spin_lock(&pch->downl);
  1663. if (pch->chan) {
  1664. if (pch->chan->ops->start_xmit(pch->chan, skb))
  1665. ppp->xmit_pending = NULL;
  1666. } else {
  1667. /* channel got unregistered */
  1668. kfree_skb(skb);
  1669. ppp->xmit_pending = NULL;
  1670. }
  1671. spin_unlock(&pch->downl);
  1672. return;
  1673. }
  1674. #ifdef CONFIG_PPP_MULTILINK
  1675. /* Multilink: fragment the packet over as many links
  1676. as can take the packet at the moment. */
  1677. if (!ppp_mp_explode(ppp, skb))
  1678. return;
  1679. #endif /* CONFIG_PPP_MULTILINK */
  1680. ppp->xmit_pending = NULL;
  1681. kfree_skb(skb);
  1682. }
  1683. #ifdef CONFIG_PPP_MULTILINK
  1684. static bool mp_protocol_compress __read_mostly = true;
  1685. module_param(mp_protocol_compress, bool, 0644);
  1686. MODULE_PARM_DESC(mp_protocol_compress,
  1687. "compress protocol id in multilink fragments");
  1688. /*
  1689. * Divide a packet to be transmitted into fragments and
  1690. * send them out the individual links.
  1691. */
  1692. static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
  1693. {
  1694. int len, totlen;
  1695. int i, bits, hdrlen, mtu;
  1696. int flen;
  1697. int navail, nfree, nzero;
  1698. int nbigger;
  1699. int totspeed;
  1700. int totfree;
  1701. unsigned char *p, *q;
  1702. struct list_head *list;
  1703. struct channel *pch;
  1704. struct sk_buff *frag;
  1705. struct ppp_channel *chan;
  1706. totspeed = 0; /*total bitrate of the bundle*/
  1707. nfree = 0; /* # channels which have no packet already queued */
  1708. navail = 0; /* total # of usable channels (not deregistered) */
  1709. nzero = 0; /* number of channels with zero speed associated*/
  1710. totfree = 0; /*total # of channels available and
  1711. *having no queued packets before
  1712. *starting the fragmentation*/
  1713. hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  1714. i = 0;
  1715. list_for_each_entry(pch, &ppp->channels, clist) {
  1716. if (pch->chan) {
  1717. pch->avail = 1;
  1718. navail++;
  1719. pch->speed = pch->chan->speed;
  1720. } else {
  1721. pch->avail = 0;
  1722. }
  1723. if (pch->avail) {
  1724. if (skb_queue_empty(&pch->file.xq) ||
  1725. !pch->had_frag) {
  1726. if (pch->speed == 0)
  1727. nzero++;
  1728. else
  1729. totspeed += pch->speed;
  1730. pch->avail = 2;
  1731. ++nfree;
  1732. ++totfree;
  1733. }
  1734. if (!pch->had_frag && i < ppp->nxchan)
  1735. ppp->nxchan = i;
  1736. }
  1737. ++i;
  1738. }
  1739. /*
  1740. * Don't start sending this packet unless at least half of
  1741. * the channels are free. This gives much better TCP
  1742. * performance if we have a lot of channels.
  1743. */
  1744. if (nfree == 0 || nfree < navail / 2)
  1745. return 0; /* can't take now, leave it in xmit_pending */
  1746. /* Do protocol field compression */
  1747. p = skb->data;
  1748. len = skb->len;
  1749. if (*p == 0 && mp_protocol_compress) {
  1750. ++p;
  1751. --len;
  1752. }
  1753. totlen = len;
  1754. nbigger = len % nfree;
  1755. /* skip to the channel after the one we last used
  1756. and start at that one */
  1757. list = &ppp->channels;
  1758. for (i = 0; i < ppp->nxchan; ++i) {
  1759. list = list->next;
  1760. if (list == &ppp->channels) {
  1761. i = 0;
  1762. break;
  1763. }
  1764. }
  1765. /* create a fragment for each channel */
  1766. bits = B;
  1767. while (len > 0) {
  1768. list = list->next;
  1769. if (list == &ppp->channels) {
  1770. i = 0;
  1771. continue;
  1772. }
  1773. pch = list_entry(list, struct channel, clist);
  1774. ++i;
  1775. if (!pch->avail)
  1776. continue;
  1777. /*
  1778. * Skip this channel if it has a fragment pending already and
  1779. * we haven't given a fragment to all of the free channels.
  1780. */
  1781. if (pch->avail == 1) {
  1782. if (nfree > 0)
  1783. continue;
  1784. } else {
  1785. pch->avail = 1;
  1786. }
  1787. /* check the channel's mtu and whether it is still attached. */
  1788. spin_lock(&pch->downl);
  1789. if (pch->chan == NULL) {
  1790. /* can't use this channel, it's being deregistered */
  1791. if (pch->speed == 0)
  1792. nzero--;
  1793. else
  1794. totspeed -= pch->speed;
  1795. spin_unlock(&pch->downl);
  1796. pch->avail = 0;
  1797. totlen = len;
  1798. totfree--;
  1799. nfree--;
  1800. if (--navail == 0)
  1801. break;
  1802. continue;
  1803. }
  1804. /*
  1805. *if the channel speed is not set divide
  1806. *the packet evenly among the free channels;
  1807. *otherwise divide it according to the speed
  1808. *of the channel we are going to transmit on
  1809. */
  1810. flen = len;
  1811. if (nfree > 0) {
  1812. if (pch->speed == 0) {
  1813. flen = len/nfree;
  1814. if (nbigger > 0) {
  1815. flen++;
  1816. nbigger--;
  1817. }
  1818. } else {
  1819. flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
  1820. ((totspeed*totfree)/pch->speed)) - hdrlen;
  1821. if (nbigger > 0) {
  1822. flen += ((totfree - nzero)*pch->speed)/totspeed;
  1823. nbigger -= ((totfree - nzero)*pch->speed)/
  1824. totspeed;
  1825. }
  1826. }
  1827. nfree--;
  1828. }
  1829. /*
  1830. *check if we are on the last channel or
  1831. *we exceded the length of the data to
  1832. *fragment
  1833. */
  1834. if ((nfree <= 0) || (flen > len))
  1835. flen = len;
  1836. /*
  1837. *it is not worth to tx on slow channels:
  1838. *in that case from the resulting flen according to the
  1839. *above formula will be equal or less than zero.
  1840. *Skip the channel in this case
  1841. */
  1842. if (flen <= 0) {
  1843. pch->avail = 2;
  1844. spin_unlock(&pch->downl);
  1845. continue;
  1846. }
  1847. /*
  1848. * hdrlen includes the 2-byte PPP protocol field, but the
  1849. * MTU counts only the payload excluding the protocol field.
  1850. * (RFC1661 Section 2)
  1851. */
  1852. mtu = pch->chan->mtu - (hdrlen - 2);
  1853. if (mtu < 4)
  1854. mtu = 4;
  1855. if (flen > mtu)
  1856. flen = mtu;
  1857. if (flen == len)
  1858. bits |= E;
  1859. frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
  1860. if (!frag)
  1861. goto noskb;
  1862. q = skb_put(frag, flen + hdrlen);
  1863. /* make the MP header */
  1864. put_unaligned_be16(PPP_MP, q);
  1865. if (ppp->flags & SC_MP_XSHORTSEQ) {
  1866. q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
  1867. q[3] = ppp->nxseq;
  1868. } else {
  1869. q[2] = bits;
  1870. q[3] = ppp->nxseq >> 16;
  1871. q[4] = ppp->nxseq >> 8;
  1872. q[5] = ppp->nxseq;
  1873. }
  1874. memcpy(q + hdrlen, p, flen);
  1875. /* try to send it down the channel */
  1876. chan = pch->chan;
  1877. if (!skb_queue_empty(&pch->file.xq) ||
  1878. !chan->ops->start_xmit(chan, frag))
  1879. skb_queue_tail(&pch->file.xq, frag);
  1880. pch->had_frag = 1;
  1881. p += flen;
  1882. len -= flen;
  1883. ++ppp->nxseq;
  1884. bits = 0;
  1885. spin_unlock(&pch->downl);
  1886. }
  1887. ppp->nxchan = i;
  1888. return 1;
  1889. noskb:
  1890. spin_unlock(&pch->downl);
  1891. if (ppp->debug & 1)
  1892. netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
  1893. ++ppp->dev->stats.tx_errors;
  1894. ++ppp->nxseq;
  1895. return 1; /* abandon the frame */
  1896. }
  1897. #endif /* CONFIG_PPP_MULTILINK */
  1898. /* Try to send data out on a channel */
  1899. static void __ppp_channel_push(struct channel *pch)
  1900. {
  1901. struct sk_buff *skb;
  1902. struct ppp *ppp;
  1903. spin_lock(&pch->downl);
  1904. if (pch->chan) {
  1905. while (!skb_queue_empty(&pch->file.xq)) {
  1906. skb = skb_dequeue(&pch->file.xq);
  1907. if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
  1908. /* put the packet back and try again later */
  1909. skb_queue_head(&pch->file.xq, skb);
  1910. break;
  1911. }
  1912. }
  1913. } else {
  1914. /* channel got deregistered */
  1915. skb_queue_purge(&pch->file.xq);
  1916. }
  1917. spin_unlock(&pch->downl);
  1918. /* see if there is anything from the attached unit to be sent */
  1919. if (skb_queue_empty(&pch->file.xq)) {
  1920. ppp = pch->ppp;
  1921. if (ppp)
  1922. __ppp_xmit_process(ppp, NULL);
  1923. }
  1924. }
  1925. static void ppp_channel_push(struct channel *pch)
  1926. {
  1927. read_lock_bh(&pch->upl);
  1928. if (pch->ppp) {
  1929. (*this_cpu_ptr(pch->ppp->xmit_recursion))++;
  1930. __ppp_channel_push(pch);
  1931. (*this_cpu_ptr(pch->ppp->xmit_recursion))--;
  1932. } else {
  1933. __ppp_channel_push(pch);
  1934. }
  1935. read_unlock_bh(&pch->upl);
  1936. }
  1937. /*
  1938. * Receive-side routines.
  1939. */
  1940. struct ppp_mp_skb_parm {
  1941. u32 sequence;
  1942. u8 BEbits;
  1943. };
  1944. #define PPP_MP_CB(skb) ((struct ppp_mp_skb_parm *)((skb)->cb))
  1945. static inline void
  1946. ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1947. {
  1948. ppp_recv_lock(ppp);
  1949. if (!ppp->closing)
  1950. ppp_receive_frame(ppp, skb, pch);
  1951. else
  1952. kfree_skb(skb);
  1953. ppp_recv_unlock(ppp);
  1954. }
  1955. /**
  1956. * __ppp_decompress_proto - Decompress protocol field, slim version.
  1957. * @skb: Socket buffer where protocol field should be decompressed. It must have
  1958. * at least 1 byte of head room and 1 byte of linear data. First byte of
  1959. * data must be a protocol field byte.
  1960. *
  1961. * Decompress protocol field in PPP header if it's compressed, e.g. when
  1962. * Protocol-Field-Compression (PFC) was negotiated. No checks w.r.t. skb data
  1963. * length are done in this function.
  1964. */
  1965. static void __ppp_decompress_proto(struct sk_buff *skb)
  1966. {
  1967. if (skb->data[0] & 0x01)
  1968. *(u8 *)skb_push(skb, 1) = 0x00;
  1969. }
  1970. /**
  1971. * ppp_decompress_proto - Check skb data room and decompress protocol field.
  1972. * @skb: Socket buffer where protocol field should be decompressed. First byte
  1973. * of data must be a protocol field byte.
  1974. *
  1975. * Decompress protocol field in PPP header if it's compressed, e.g. when
  1976. * Protocol-Field-Compression (PFC) was negotiated. This function also makes
  1977. * sure that skb data room is sufficient for Protocol field, before and after
  1978. * decompression.
  1979. *
  1980. * Return: true - decompressed successfully, false - not enough room in skb.
  1981. */
  1982. static bool ppp_decompress_proto(struct sk_buff *skb)
  1983. {
  1984. /* At least one byte should be present (if protocol is compressed) */
  1985. if (!pskb_may_pull(skb, 1))
  1986. return false;
  1987. __ppp_decompress_proto(skb);
  1988. /* Protocol field should occupy 2 bytes when not compressed */
  1989. return pskb_may_pull(skb, 2);
  1990. }
  1991. /* Attempt to handle a frame via. a bridged channel, if one exists.
  1992. * If the channel is bridged, the frame is consumed by the bridge.
  1993. * If not, the caller must handle the frame by normal recv mechanisms.
  1994. * Returns true if the frame is consumed, false otherwise.
  1995. */
  1996. static bool ppp_channel_bridge_input(struct channel *pch, struct sk_buff *skb)
  1997. {
  1998. struct channel *pchb;
  1999. rcu_read_lock();
  2000. pchb = rcu_dereference(pch->bridge);
  2001. if (!pchb)
  2002. goto out_rcu;
  2003. spin_lock_bh(&pchb->downl);
  2004. if (!pchb->chan) {
  2005. /* channel got unregistered */
  2006. kfree_skb(skb);
  2007. goto outl;
  2008. }
  2009. skb_scrub_packet(skb, !net_eq(pch->chan_net, pchb->chan_net));
  2010. if (!pchb->chan->ops->start_xmit(pchb->chan, skb))
  2011. kfree_skb(skb);
  2012. outl:
  2013. spin_unlock_bh(&pchb->downl);
  2014. out_rcu:
  2015. rcu_read_unlock();
  2016. /* If pchb is set then we've consumed the packet */
  2017. return !!pchb;
  2018. }
  2019. void
  2020. ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
  2021. {
  2022. struct channel *pch = chan->ppp;
  2023. int proto;
  2024. if (!pch) {
  2025. kfree_skb(skb);
  2026. return;
  2027. }
  2028. /* If the channel is bridged, transmit via. bridge */
  2029. if (ppp_channel_bridge_input(pch, skb))
  2030. return;
  2031. read_lock_bh(&pch->upl);
  2032. if (!ppp_decompress_proto(skb)) {
  2033. kfree_skb(skb);
  2034. if (pch->ppp) {
  2035. ++pch->ppp->dev->stats.rx_length_errors;
  2036. ppp_receive_error(pch->ppp);
  2037. }
  2038. goto done;
  2039. }
  2040. proto = PPP_PROTO(skb);
  2041. if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
  2042. /* put it on the channel queue */
  2043. skb_queue_tail(&pch->file.rq, skb);
  2044. /* drop old frames if queue too long */
  2045. while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
  2046. (skb = skb_dequeue(&pch->file.rq)))
  2047. kfree_skb(skb);
  2048. wake_up_interruptible(&pch->file.rwait);
  2049. } else {
  2050. ppp_do_recv(pch->ppp, skb, pch);
  2051. }
  2052. done:
  2053. read_unlock_bh(&pch->upl);
  2054. }
  2055. /* Put a 0-length skb in the receive queue as an error indication */
  2056. void
  2057. ppp_input_error(struct ppp_channel *chan, int code)
  2058. {
  2059. struct channel *pch = chan->ppp;
  2060. struct sk_buff *skb;
  2061. if (!pch)
  2062. return;
  2063. read_lock_bh(&pch->upl);
  2064. if (pch->ppp) {
  2065. skb = alloc_skb(0, GFP_ATOMIC);
  2066. if (skb) {
  2067. skb->len = 0; /* probably unnecessary */
  2068. skb->cb[0] = code;
  2069. ppp_do_recv(pch->ppp, skb, pch);
  2070. }
  2071. }
  2072. read_unlock_bh(&pch->upl);
  2073. }
  2074. /*
  2075. * We come in here to process a received frame.
  2076. * The receive side of the ppp unit is locked.
  2077. */
  2078. static void
  2079. ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  2080. {
  2081. /* note: a 0-length skb is used as an error indication */
  2082. if (skb->len > 0) {
  2083. skb_checksum_complete_unset(skb);
  2084. #ifdef CONFIG_PPP_MULTILINK
  2085. /* XXX do channel-level decompression here */
  2086. if (PPP_PROTO(skb) == PPP_MP)
  2087. ppp_receive_mp_frame(ppp, skb, pch);
  2088. else
  2089. #endif /* CONFIG_PPP_MULTILINK */
  2090. ppp_receive_nonmp_frame(ppp, skb);
  2091. } else {
  2092. kfree_skb(skb);
  2093. ppp_receive_error(ppp);
  2094. }
  2095. }
  2096. static void
  2097. ppp_receive_error(struct ppp *ppp)
  2098. {
  2099. ++ppp->dev->stats.rx_errors;
  2100. if (ppp->vj)
  2101. slhc_toss(ppp->vj);
  2102. }
  2103. static void
  2104. ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
  2105. {
  2106. struct sk_buff *ns;
  2107. int proto, len, npi;
  2108. /*
  2109. * Decompress the frame, if compressed.
  2110. * Note that some decompressors need to see uncompressed frames
  2111. * that come in as well as compressed frames.
  2112. */
  2113. if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
  2114. (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
  2115. skb = ppp_decompress_frame(ppp, skb);
  2116. if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
  2117. goto err;
  2118. /* At this point the "Protocol" field MUST be decompressed, either in
  2119. * ppp_input(), ppp_decompress_frame() or in ppp_receive_mp_frame().
  2120. */
  2121. proto = PPP_PROTO(skb);
  2122. switch (proto) {
  2123. case PPP_VJC_COMP:
  2124. /* decompress VJ compressed packets */
  2125. if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
  2126. goto err;
  2127. if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
  2128. /* copy to a new sk_buff with more tailroom */
  2129. ns = dev_alloc_skb(skb->len + 128);
  2130. if (!ns) {
  2131. netdev_err(ppp->dev, "PPP: no memory "
  2132. "(VJ decomp)\n");
  2133. goto err;
  2134. }
  2135. skb_reserve(ns, 2);
  2136. skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
  2137. consume_skb(skb);
  2138. skb = ns;
  2139. }
  2140. else
  2141. skb->ip_summed = CHECKSUM_NONE;
  2142. len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
  2143. if (len <= 0) {
  2144. netdev_printk(KERN_DEBUG, ppp->dev,
  2145. "PPP: VJ decompression error\n");
  2146. goto err;
  2147. }
  2148. len += 2;
  2149. if (len > skb->len)
  2150. skb_put(skb, len - skb->len);
  2151. else if (len < skb->len)
  2152. skb_trim(skb, len);
  2153. proto = PPP_IP;
  2154. break;
  2155. case PPP_VJC_UNCOMP:
  2156. if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
  2157. goto err;
  2158. /* Until we fix the decompressor need to make sure
  2159. * data portion is linear.
  2160. */
  2161. if (!pskb_may_pull(skb, skb->len))
  2162. goto err;
  2163. if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
  2164. netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
  2165. goto err;
  2166. }
  2167. proto = PPP_IP;
  2168. break;
  2169. case PPP_CCP:
  2170. ppp_ccp_peek(ppp, skb, 1);
  2171. break;
  2172. }
  2173. ++ppp->stats64.rx_packets;
  2174. ppp->stats64.rx_bytes += skb->len - 2;
  2175. npi = proto_to_npindex(proto);
  2176. if (npi < 0) {
  2177. /* control or unknown frame - pass it to pppd */
  2178. skb_queue_tail(&ppp->file.rq, skb);
  2179. /* limit queue length by dropping old frames */
  2180. while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
  2181. (skb = skb_dequeue(&ppp->file.rq)))
  2182. kfree_skb(skb);
  2183. /* wake up any process polling or blocking on read */
  2184. wake_up_interruptible(&ppp->file.rwait);
  2185. } else {
  2186. /* network protocol frame - give it to the kernel */
  2187. #ifdef CONFIG_PPP_FILTER
  2188. if (ppp->pass_filter || ppp->active_filter) {
  2189. if (skb_unclone(skb, GFP_ATOMIC))
  2190. goto err;
  2191. /* Check if the packet passes the pass and active filters.
  2192. * See comment for PPP_FILTER_INBOUND_TAG above.
  2193. */
  2194. *(__be16 *)skb_push(skb, 2) = htons(PPP_FILTER_INBOUND_TAG);
  2195. if (ppp->pass_filter &&
  2196. bpf_prog_run(ppp->pass_filter, skb) == 0) {
  2197. if (ppp->debug & 1)
  2198. netdev_printk(KERN_DEBUG, ppp->dev,
  2199. "PPP: inbound frame "
  2200. "not passed\n");
  2201. kfree_skb(skb);
  2202. return;
  2203. }
  2204. if (!(ppp->active_filter &&
  2205. bpf_prog_run(ppp->active_filter, skb) == 0))
  2206. ppp->last_recv = jiffies;
  2207. __skb_pull(skb, 2);
  2208. } else
  2209. #endif /* CONFIG_PPP_FILTER */
  2210. ppp->last_recv = jiffies;
  2211. if ((ppp->dev->flags & IFF_UP) == 0 ||
  2212. ppp->npmode[npi] != NPMODE_PASS) {
  2213. kfree_skb(skb);
  2214. } else {
  2215. /* chop off protocol */
  2216. skb_pull_rcsum(skb, 2);
  2217. skb->dev = ppp->dev;
  2218. skb->protocol = htons(npindex_to_ethertype[npi]);
  2219. skb_reset_mac_header(skb);
  2220. skb_scrub_packet(skb, !net_eq(ppp->ppp_net,
  2221. dev_net(ppp->dev)));
  2222. netif_rx(skb);
  2223. }
  2224. }
  2225. return;
  2226. err:
  2227. kfree_skb(skb);
  2228. ppp_receive_error(ppp);
  2229. }
  2230. static struct sk_buff *
  2231. ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
  2232. {
  2233. int proto = PPP_PROTO(skb);
  2234. struct sk_buff *ns;
  2235. int len;
  2236. /* Until we fix all the decompressor's need to make sure
  2237. * data portion is linear.
  2238. */
  2239. if (!pskb_may_pull(skb, skb->len))
  2240. goto err;
  2241. if (proto == PPP_COMP) {
  2242. int obuff_size;
  2243. switch(ppp->rcomp->compress_proto) {
  2244. case CI_MPPE:
  2245. obuff_size = ppp->mru + PPP_HDRLEN + 1;
  2246. break;
  2247. default:
  2248. obuff_size = ppp->mru + PPP_HDRLEN;
  2249. break;
  2250. }
  2251. ns = dev_alloc_skb(obuff_size);
  2252. if (!ns) {
  2253. netdev_err(ppp->dev, "ppp_decompress_frame: "
  2254. "no memory\n");
  2255. goto err;
  2256. }
  2257. /* the decompressor still expects the A/C bytes in the hdr */
  2258. len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
  2259. skb->len + 2, ns->data, obuff_size);
  2260. if (len < 0) {
  2261. /* Pass the compressed frame to pppd as an
  2262. error indication. */
  2263. if (len == DECOMP_FATALERROR)
  2264. ppp->rstate |= SC_DC_FERROR;
  2265. kfree_skb(ns);
  2266. goto err;
  2267. }
  2268. consume_skb(skb);
  2269. skb = ns;
  2270. skb_put(skb, len);
  2271. skb_pull(skb, 2); /* pull off the A/C bytes */
  2272. /* Don't call __ppp_decompress_proto() here, but instead rely on
  2273. * corresponding algo (mppe/bsd/deflate) to decompress it.
  2274. */
  2275. } else {
  2276. /* Uncompressed frame - pass to decompressor so it
  2277. can update its dictionary if necessary. */
  2278. if (ppp->rcomp->incomp)
  2279. ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
  2280. skb->len + 2);
  2281. }
  2282. return skb;
  2283. err:
  2284. ppp->rstate |= SC_DC_ERROR;
  2285. ppp_receive_error(ppp);
  2286. return skb;
  2287. }
  2288. #ifdef CONFIG_PPP_MULTILINK
  2289. /*
  2290. * Receive a multilink frame.
  2291. * We put it on the reconstruction queue and then pull off
  2292. * as many completed frames as we can.
  2293. */
  2294. static void
  2295. ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  2296. {
  2297. u32 mask, seq;
  2298. struct channel *ch;
  2299. int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  2300. if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
  2301. goto err; /* no good, throw it away */
  2302. /* Decode sequence number and begin/end bits */
  2303. if (ppp->flags & SC_MP_SHORTSEQ) {
  2304. seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
  2305. mask = 0xfff;
  2306. } else {
  2307. seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
  2308. mask = 0xffffff;
  2309. }
  2310. PPP_MP_CB(skb)->BEbits = skb->data[2];
  2311. skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
  2312. /*
  2313. * Do protocol ID decompression on the first fragment of each packet.
  2314. * We have to do that here, because ppp_receive_nonmp_frame() expects
  2315. * decompressed protocol field.
  2316. */
  2317. if (PPP_MP_CB(skb)->BEbits & B)
  2318. __ppp_decompress_proto(skb);
  2319. /*
  2320. * Expand sequence number to 32 bits, making it as close
  2321. * as possible to ppp->minseq.
  2322. */
  2323. seq |= ppp->minseq & ~mask;
  2324. if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
  2325. seq += mask + 1;
  2326. else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
  2327. seq -= mask + 1; /* should never happen */
  2328. PPP_MP_CB(skb)->sequence = seq;
  2329. pch->lastseq = seq;
  2330. /*
  2331. * If this packet comes before the next one we were expecting,
  2332. * drop it.
  2333. */
  2334. if (seq_before(seq, ppp->nextseq)) {
  2335. kfree_skb(skb);
  2336. ++ppp->dev->stats.rx_dropped;
  2337. ppp_receive_error(ppp);
  2338. return;
  2339. }
  2340. /*
  2341. * Reevaluate minseq, the minimum over all channels of the
  2342. * last sequence number received on each channel. Because of
  2343. * the increasing sequence number rule, we know that any fragment
  2344. * before `minseq' which hasn't arrived is never going to arrive.
  2345. * The list of channels can't change because we have the receive
  2346. * side of the ppp unit locked.
  2347. */
  2348. list_for_each_entry(ch, &ppp->channels, clist) {
  2349. if (seq_before(ch->lastseq, seq))
  2350. seq = ch->lastseq;
  2351. }
  2352. if (seq_before(ppp->minseq, seq))
  2353. ppp->minseq = seq;
  2354. /* Put the fragment on the reconstruction queue */
  2355. ppp_mp_insert(ppp, skb);
  2356. /* If the queue is getting long, don't wait any longer for packets
  2357. before the start of the queue. */
  2358. if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
  2359. struct sk_buff *mskb = skb_peek(&ppp->mrq);
  2360. if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
  2361. ppp->minseq = PPP_MP_CB(mskb)->sequence;
  2362. }
  2363. /* Pull completed packets off the queue and receive them. */
  2364. while ((skb = ppp_mp_reconstruct(ppp))) {
  2365. if (pskb_may_pull(skb, 2))
  2366. ppp_receive_nonmp_frame(ppp, skb);
  2367. else {
  2368. ++ppp->dev->stats.rx_length_errors;
  2369. kfree_skb(skb);
  2370. ppp_receive_error(ppp);
  2371. }
  2372. }
  2373. return;
  2374. err:
  2375. kfree_skb(skb);
  2376. ppp_receive_error(ppp);
  2377. }
  2378. /*
  2379. * Insert a fragment on the MP reconstruction queue.
  2380. * The queue is ordered by increasing sequence number.
  2381. */
  2382. static void
  2383. ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
  2384. {
  2385. struct sk_buff *p;
  2386. struct sk_buff_head *list = &ppp->mrq;
  2387. u32 seq = PPP_MP_CB(skb)->sequence;
  2388. /* N.B. we don't need to lock the list lock because we have the
  2389. ppp unit receive-side lock. */
  2390. skb_queue_walk(list, p) {
  2391. if (seq_before(seq, PPP_MP_CB(p)->sequence))
  2392. break;
  2393. }
  2394. __skb_queue_before(list, p, skb);
  2395. }
  2396. /*
  2397. * Reconstruct a packet from the MP fragment queue.
  2398. * We go through increasing sequence numbers until we find a
  2399. * complete packet, or we get to the sequence number for a fragment
  2400. * which hasn't arrived but might still do so.
  2401. */
  2402. static struct sk_buff *
  2403. ppp_mp_reconstruct(struct ppp *ppp)
  2404. {
  2405. u32 seq = ppp->nextseq;
  2406. u32 minseq = ppp->minseq;
  2407. struct sk_buff_head *list = &ppp->mrq;
  2408. struct sk_buff *p, *tmp;
  2409. struct sk_buff *head, *tail;
  2410. struct sk_buff *skb = NULL;
  2411. int lost = 0, len = 0;
  2412. if (ppp->mrru == 0) /* do nothing until mrru is set */
  2413. return NULL;
  2414. head = __skb_peek(list);
  2415. tail = NULL;
  2416. skb_queue_walk_safe(list, p, tmp) {
  2417. again:
  2418. if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
  2419. /* this can't happen, anyway ignore the skb */
  2420. netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
  2421. "seq %u < %u\n",
  2422. PPP_MP_CB(p)->sequence, seq);
  2423. __skb_unlink(p, list);
  2424. kfree_skb(p);
  2425. continue;
  2426. }
  2427. if (PPP_MP_CB(p)->sequence != seq) {
  2428. u32 oldseq;
  2429. /* Fragment `seq' is missing. If it is after
  2430. minseq, it might arrive later, so stop here. */
  2431. if (seq_after(seq, minseq))
  2432. break;
  2433. /* Fragment `seq' is lost, keep going. */
  2434. lost = 1;
  2435. oldseq = seq;
  2436. seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
  2437. minseq + 1: PPP_MP_CB(p)->sequence;
  2438. if (ppp->debug & 1)
  2439. netdev_printk(KERN_DEBUG, ppp->dev,
  2440. "lost frag %u..%u\n",
  2441. oldseq, seq-1);
  2442. goto again;
  2443. }
  2444. /*
  2445. * At this point we know that all the fragments from
  2446. * ppp->nextseq to seq are either present or lost.
  2447. * Also, there are no complete packets in the queue
  2448. * that have no missing fragments and end before this
  2449. * fragment.
  2450. */
  2451. /* B bit set indicates this fragment starts a packet */
  2452. if (PPP_MP_CB(p)->BEbits & B) {
  2453. head = p;
  2454. lost = 0;
  2455. len = 0;
  2456. }
  2457. len += p->len;
  2458. /* Got a complete packet yet? */
  2459. if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
  2460. (PPP_MP_CB(head)->BEbits & B)) {
  2461. if (len > ppp->mrru + 2) {
  2462. ++ppp->dev->stats.rx_length_errors;
  2463. netdev_printk(KERN_DEBUG, ppp->dev,
  2464. "PPP: reconstructed packet"
  2465. " is too long (%d)\n", len);
  2466. } else {
  2467. tail = p;
  2468. break;
  2469. }
  2470. ppp->nextseq = seq + 1;
  2471. }
  2472. /*
  2473. * If this is the ending fragment of a packet,
  2474. * and we haven't found a complete valid packet yet,
  2475. * we can discard up to and including this fragment.
  2476. */
  2477. if (PPP_MP_CB(p)->BEbits & E) {
  2478. struct sk_buff *tmp2;
  2479. skb_queue_reverse_walk_from_safe(list, p, tmp2) {
  2480. if (ppp->debug & 1)
  2481. netdev_printk(KERN_DEBUG, ppp->dev,
  2482. "discarding frag %u\n",
  2483. PPP_MP_CB(p)->sequence);
  2484. __skb_unlink(p, list);
  2485. kfree_skb(p);
  2486. }
  2487. head = skb_peek(list);
  2488. if (!head)
  2489. break;
  2490. }
  2491. ++seq;
  2492. }
  2493. /* If we have a complete packet, copy it all into one skb. */
  2494. if (tail != NULL) {
  2495. /* If we have discarded any fragments,
  2496. signal a receive error. */
  2497. if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
  2498. skb_queue_walk_safe(list, p, tmp) {
  2499. if (p == head)
  2500. break;
  2501. if (ppp->debug & 1)
  2502. netdev_printk(KERN_DEBUG, ppp->dev,
  2503. "discarding frag %u\n",
  2504. PPP_MP_CB(p)->sequence);
  2505. __skb_unlink(p, list);
  2506. kfree_skb(p);
  2507. }
  2508. if (ppp->debug & 1)
  2509. netdev_printk(KERN_DEBUG, ppp->dev,
  2510. " missed pkts %u..%u\n",
  2511. ppp->nextseq,
  2512. PPP_MP_CB(head)->sequence-1);
  2513. ++ppp->dev->stats.rx_dropped;
  2514. ppp_receive_error(ppp);
  2515. }
  2516. skb = head;
  2517. if (head != tail) {
  2518. struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
  2519. p = skb_queue_next(list, head);
  2520. __skb_unlink(skb, list);
  2521. skb_queue_walk_from_safe(list, p, tmp) {
  2522. __skb_unlink(p, list);
  2523. *fragpp = p;
  2524. p->next = NULL;
  2525. fragpp = &p->next;
  2526. skb->len += p->len;
  2527. skb->data_len += p->len;
  2528. skb->truesize += p->truesize;
  2529. if (p == tail)
  2530. break;
  2531. }
  2532. } else {
  2533. __skb_unlink(skb, list);
  2534. }
  2535. ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
  2536. }
  2537. return skb;
  2538. }
  2539. #endif /* CONFIG_PPP_MULTILINK */
  2540. /*
  2541. * Channel interface.
  2542. */
  2543. /* Create a new, unattached ppp channel. */
  2544. int ppp_register_channel(struct ppp_channel *chan)
  2545. {
  2546. return ppp_register_net_channel(current->nsproxy->net_ns, chan);
  2547. }
  2548. /* Create a new, unattached ppp channel for specified net. */
  2549. int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
  2550. {
  2551. struct channel *pch;
  2552. struct ppp_net *pn;
  2553. pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
  2554. if (!pch)
  2555. return -ENOMEM;
  2556. pn = ppp_pernet(net);
  2557. pch->ppp = NULL;
  2558. pch->chan = chan;
  2559. pch->chan_net = get_net_track(net, &pch->ns_tracker, GFP_KERNEL);
  2560. chan->ppp = pch;
  2561. init_ppp_file(&pch->file, CHANNEL);
  2562. pch->file.hdrlen = chan->hdrlen;
  2563. #ifdef CONFIG_PPP_MULTILINK
  2564. pch->lastseq = -1;
  2565. #endif /* CONFIG_PPP_MULTILINK */
  2566. init_rwsem(&pch->chan_sem);
  2567. spin_lock_init(&pch->downl);
  2568. rwlock_init(&pch->upl);
  2569. spin_lock_bh(&pn->all_channels_lock);
  2570. pch->file.index = ++pn->last_channel_index;
  2571. list_add(&pch->list, &pn->new_channels);
  2572. atomic_inc(&channel_count);
  2573. spin_unlock_bh(&pn->all_channels_lock);
  2574. return 0;
  2575. }
  2576. /*
  2577. * Return the index of a channel.
  2578. */
  2579. int ppp_channel_index(struct ppp_channel *chan)
  2580. {
  2581. struct channel *pch = chan->ppp;
  2582. if (pch)
  2583. return pch->file.index;
  2584. return -1;
  2585. }
  2586. /*
  2587. * Return the PPP unit number to which a channel is connected.
  2588. */
  2589. int ppp_unit_number(struct ppp_channel *chan)
  2590. {
  2591. struct channel *pch = chan->ppp;
  2592. int unit = -1;
  2593. if (pch) {
  2594. read_lock_bh(&pch->upl);
  2595. if (pch->ppp)
  2596. unit = pch->ppp->file.index;
  2597. read_unlock_bh(&pch->upl);
  2598. }
  2599. return unit;
  2600. }
  2601. /*
  2602. * Return the PPP device interface name of a channel.
  2603. */
  2604. char *ppp_dev_name(struct ppp_channel *chan)
  2605. {
  2606. struct channel *pch = chan->ppp;
  2607. char *name = NULL;
  2608. if (pch) {
  2609. read_lock_bh(&pch->upl);
  2610. if (pch->ppp && pch->ppp->dev)
  2611. name = pch->ppp->dev->name;
  2612. read_unlock_bh(&pch->upl);
  2613. }
  2614. return name;
  2615. }
  2616. /*
  2617. * Disconnect a channel from the generic layer.
  2618. * This must be called in process context.
  2619. */
  2620. void
  2621. ppp_unregister_channel(struct ppp_channel *chan)
  2622. {
  2623. struct channel *pch = chan->ppp;
  2624. struct ppp_net *pn;
  2625. if (!pch)
  2626. return; /* should never happen */
  2627. chan->ppp = NULL;
  2628. /*
  2629. * This ensures that we have returned from any calls into
  2630. * the channel's start_xmit or ioctl routine before we proceed.
  2631. */
  2632. down_write(&pch->chan_sem);
  2633. spin_lock_bh(&pch->downl);
  2634. WRITE_ONCE(pch->chan, NULL);
  2635. spin_unlock_bh(&pch->downl);
  2636. up_write(&pch->chan_sem);
  2637. ppp_disconnect_channel(pch);
  2638. pn = ppp_pernet(pch->chan_net);
  2639. spin_lock_bh(&pn->all_channels_lock);
  2640. list_del(&pch->list);
  2641. spin_unlock_bh(&pn->all_channels_lock);
  2642. ppp_unbridge_channels(pch);
  2643. pch->file.dead = 1;
  2644. wake_up_interruptible(&pch->file.rwait);
  2645. if (refcount_dec_and_test(&pch->file.refcnt))
  2646. ppp_destroy_channel(pch);
  2647. }
  2648. /*
  2649. * Callback from a channel when it can accept more to transmit.
  2650. * This should be called at BH/softirq level, not interrupt level.
  2651. */
  2652. void
  2653. ppp_output_wakeup(struct ppp_channel *chan)
  2654. {
  2655. struct channel *pch = chan->ppp;
  2656. if (!pch)
  2657. return;
  2658. ppp_channel_push(pch);
  2659. }
  2660. /*
  2661. * Compression control.
  2662. */
  2663. /* Process the PPPIOCSCOMPRESS ioctl. */
  2664. static int
  2665. ppp_set_compress(struct ppp *ppp, struct ppp_option_data *data)
  2666. {
  2667. int err = -EFAULT;
  2668. struct compressor *cp, *ocomp;
  2669. void *state, *ostate;
  2670. unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
  2671. if (data->length > CCP_MAX_OPTION_LENGTH)
  2672. goto out;
  2673. if (copy_from_user(ccp_option, data->ptr, data->length))
  2674. goto out;
  2675. err = -EINVAL;
  2676. if (data->length < 2 || ccp_option[1] < 2 || ccp_option[1] > data->length)
  2677. goto out;
  2678. cp = try_then_request_module(
  2679. find_compressor(ccp_option[0]),
  2680. "ppp-compress-%d", ccp_option[0]);
  2681. if (!cp)
  2682. goto out;
  2683. err = -ENOBUFS;
  2684. if (data->transmit) {
  2685. state = cp->comp_alloc(ccp_option, data->length);
  2686. if (state) {
  2687. ppp_xmit_lock(ppp);
  2688. ppp->xstate &= ~SC_COMP_RUN;
  2689. ocomp = ppp->xcomp;
  2690. ostate = ppp->xc_state;
  2691. ppp->xcomp = cp;
  2692. ppp->xc_state = state;
  2693. ppp_xmit_unlock(ppp);
  2694. if (ostate) {
  2695. ocomp->comp_free(ostate);
  2696. module_put(ocomp->owner);
  2697. }
  2698. err = 0;
  2699. } else
  2700. module_put(cp->owner);
  2701. } else {
  2702. state = cp->decomp_alloc(ccp_option, data->length);
  2703. if (state) {
  2704. ppp_recv_lock(ppp);
  2705. ppp->rstate &= ~SC_DECOMP_RUN;
  2706. ocomp = ppp->rcomp;
  2707. ostate = ppp->rc_state;
  2708. ppp->rcomp = cp;
  2709. ppp->rc_state = state;
  2710. ppp_recv_unlock(ppp);
  2711. if (ostate) {
  2712. ocomp->decomp_free(ostate);
  2713. module_put(ocomp->owner);
  2714. }
  2715. err = 0;
  2716. } else
  2717. module_put(cp->owner);
  2718. }
  2719. out:
  2720. return err;
  2721. }
  2722. /*
  2723. * Look at a CCP packet and update our state accordingly.
  2724. * We assume the caller has the xmit or recv path locked.
  2725. */
  2726. static void
  2727. ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
  2728. {
  2729. unsigned char *dp;
  2730. int len;
  2731. if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
  2732. return; /* no header */
  2733. dp = skb->data + 2;
  2734. switch (CCP_CODE(dp)) {
  2735. case CCP_CONFREQ:
  2736. /* A ConfReq starts negotiation of compression
  2737. * in one direction of transmission,
  2738. * and hence brings it down...but which way?
  2739. *
  2740. * Remember:
  2741. * A ConfReq indicates what the sender would like to receive
  2742. */
  2743. if(inbound)
  2744. /* He is proposing what I should send */
  2745. ppp->xstate &= ~SC_COMP_RUN;
  2746. else
  2747. /* I am proposing to what he should send */
  2748. ppp->rstate &= ~SC_DECOMP_RUN;
  2749. break;
  2750. case CCP_TERMREQ:
  2751. case CCP_TERMACK:
  2752. /*
  2753. * CCP is going down, both directions of transmission
  2754. */
  2755. ppp->rstate &= ~SC_DECOMP_RUN;
  2756. ppp->xstate &= ~SC_COMP_RUN;
  2757. break;
  2758. case CCP_CONFACK:
  2759. if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
  2760. break;
  2761. len = CCP_LENGTH(dp);
  2762. if (!pskb_may_pull(skb, len + 2))
  2763. return; /* too short */
  2764. dp += CCP_HDRLEN;
  2765. len -= CCP_HDRLEN;
  2766. if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
  2767. break;
  2768. if (inbound) {
  2769. /* we will start receiving compressed packets */
  2770. if (!ppp->rc_state)
  2771. break;
  2772. if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
  2773. ppp->file.index, 0, ppp->mru, ppp->debug)) {
  2774. ppp->rstate |= SC_DECOMP_RUN;
  2775. ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
  2776. }
  2777. } else {
  2778. /* we will soon start sending compressed packets */
  2779. if (!ppp->xc_state)
  2780. break;
  2781. if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
  2782. ppp->file.index, 0, ppp->debug))
  2783. ppp->xstate |= SC_COMP_RUN;
  2784. }
  2785. break;
  2786. case CCP_RESETACK:
  2787. /* reset the [de]compressor */
  2788. if ((ppp->flags & SC_CCP_UP) == 0)
  2789. break;
  2790. if (inbound) {
  2791. if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
  2792. ppp->rcomp->decomp_reset(ppp->rc_state);
  2793. ppp->rstate &= ~SC_DC_ERROR;
  2794. }
  2795. } else {
  2796. if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
  2797. ppp->xcomp->comp_reset(ppp->xc_state);
  2798. }
  2799. break;
  2800. }
  2801. }
  2802. /* Free up compression resources. */
  2803. static void
  2804. ppp_ccp_closed(struct ppp *ppp)
  2805. {
  2806. void *xstate, *rstate;
  2807. struct compressor *xcomp, *rcomp;
  2808. ppp_lock(ppp);
  2809. ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
  2810. ppp->xstate = 0;
  2811. xcomp = ppp->xcomp;
  2812. xstate = ppp->xc_state;
  2813. ppp->xc_state = NULL;
  2814. ppp->rstate = 0;
  2815. rcomp = ppp->rcomp;
  2816. rstate = ppp->rc_state;
  2817. ppp->rc_state = NULL;
  2818. ppp_unlock(ppp);
  2819. if (xstate) {
  2820. xcomp->comp_free(xstate);
  2821. module_put(xcomp->owner);
  2822. }
  2823. if (rstate) {
  2824. rcomp->decomp_free(rstate);
  2825. module_put(rcomp->owner);
  2826. }
  2827. }
  2828. /* List of compressors. */
  2829. static LIST_HEAD(compressor_list);
  2830. static DEFINE_SPINLOCK(compressor_list_lock);
  2831. struct compressor_entry {
  2832. struct list_head list;
  2833. struct compressor *comp;
  2834. };
  2835. static struct compressor_entry *
  2836. find_comp_entry(int proto)
  2837. {
  2838. struct compressor_entry *ce;
  2839. list_for_each_entry(ce, &compressor_list, list) {
  2840. if (ce->comp->compress_proto == proto)
  2841. return ce;
  2842. }
  2843. return NULL;
  2844. }
  2845. /* Register a compressor */
  2846. int
  2847. ppp_register_compressor(struct compressor *cp)
  2848. {
  2849. struct compressor_entry *ce;
  2850. int ret;
  2851. spin_lock(&compressor_list_lock);
  2852. ret = -EEXIST;
  2853. if (find_comp_entry(cp->compress_proto))
  2854. goto out;
  2855. ret = -ENOMEM;
  2856. ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
  2857. if (!ce)
  2858. goto out;
  2859. ret = 0;
  2860. ce->comp = cp;
  2861. list_add(&ce->list, &compressor_list);
  2862. out:
  2863. spin_unlock(&compressor_list_lock);
  2864. return ret;
  2865. }
  2866. /* Unregister a compressor */
  2867. void
  2868. ppp_unregister_compressor(struct compressor *cp)
  2869. {
  2870. struct compressor_entry *ce;
  2871. spin_lock(&compressor_list_lock);
  2872. ce = find_comp_entry(cp->compress_proto);
  2873. if (ce && ce->comp == cp) {
  2874. list_del(&ce->list);
  2875. kfree(ce);
  2876. }
  2877. spin_unlock(&compressor_list_lock);
  2878. }
  2879. /* Find a compressor. */
  2880. static struct compressor *
  2881. find_compressor(int type)
  2882. {
  2883. struct compressor_entry *ce;
  2884. struct compressor *cp = NULL;
  2885. spin_lock(&compressor_list_lock);
  2886. ce = find_comp_entry(type);
  2887. if (ce) {
  2888. cp = ce->comp;
  2889. if (!try_module_get(cp->owner))
  2890. cp = NULL;
  2891. }
  2892. spin_unlock(&compressor_list_lock);
  2893. return cp;
  2894. }
  2895. /*
  2896. * Miscelleneous stuff.
  2897. */
  2898. static void
  2899. ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
  2900. {
  2901. struct slcompress *vj = ppp->vj;
  2902. memset(st, 0, sizeof(*st));
  2903. st->p.ppp_ipackets = ppp->stats64.rx_packets;
  2904. st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
  2905. st->p.ppp_ibytes = ppp->stats64.rx_bytes;
  2906. st->p.ppp_opackets = ppp->stats64.tx_packets;
  2907. st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
  2908. st->p.ppp_obytes = ppp->stats64.tx_bytes;
  2909. if (!vj)
  2910. return;
  2911. st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
  2912. st->vj.vjs_compressed = vj->sls_o_compressed;
  2913. st->vj.vjs_searches = vj->sls_o_searches;
  2914. st->vj.vjs_misses = vj->sls_o_misses;
  2915. st->vj.vjs_errorin = vj->sls_i_error;
  2916. st->vj.vjs_tossed = vj->sls_i_tossed;
  2917. st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
  2918. st->vj.vjs_compressedin = vj->sls_i_compressed;
  2919. }
  2920. /*
  2921. * Stuff for handling the lists of ppp units and channels
  2922. * and for initialization.
  2923. */
  2924. /*
  2925. * Create a new ppp interface unit. Fails if it can't allocate memory
  2926. * or if there is already a unit with the requested number.
  2927. * unit == -1 means allocate a new number.
  2928. */
  2929. static int ppp_create_interface(struct net *net, struct file *file, int *unit)
  2930. {
  2931. struct ppp_config conf = {
  2932. .file = file,
  2933. .unit = *unit,
  2934. .ifname_is_set = false,
  2935. };
  2936. struct net_device *dev;
  2937. struct ppp *ppp;
  2938. int err;
  2939. dev = alloc_netdev(sizeof(struct ppp), "", NET_NAME_ENUM, ppp_setup);
  2940. if (!dev) {
  2941. err = -ENOMEM;
  2942. goto err;
  2943. }
  2944. dev_net_set(dev, net);
  2945. dev->rtnl_link_ops = &ppp_link_ops;
  2946. rtnl_lock();
  2947. err = ppp_dev_configure(net, dev, &conf);
  2948. if (err < 0)
  2949. goto err_dev;
  2950. ppp = netdev_priv(dev);
  2951. *unit = ppp->file.index;
  2952. rtnl_unlock();
  2953. return 0;
  2954. err_dev:
  2955. rtnl_unlock();
  2956. free_netdev(dev);
  2957. err:
  2958. return err;
  2959. }
  2960. /*
  2961. * Initialize a ppp_file structure.
  2962. */
  2963. static void
  2964. init_ppp_file(struct ppp_file *pf, int kind)
  2965. {
  2966. pf->kind = kind;
  2967. skb_queue_head_init(&pf->xq);
  2968. skb_queue_head_init(&pf->rq);
  2969. refcount_set(&pf->refcnt, 1);
  2970. init_waitqueue_head(&pf->rwait);
  2971. }
  2972. /*
  2973. * Free the memory used by a ppp unit. This is only called once
  2974. * there are no channels connected to the unit and no file structs
  2975. * that reference the unit.
  2976. */
  2977. static void ppp_destroy_interface(struct ppp *ppp)
  2978. {
  2979. atomic_dec(&ppp_unit_count);
  2980. if (!ppp->file.dead || ppp->n_channels) {
  2981. /* "can't happen" */
  2982. netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
  2983. "but dead=%d n_channels=%d !\n",
  2984. ppp, ppp->file.dead, ppp->n_channels);
  2985. return;
  2986. }
  2987. ppp_ccp_closed(ppp);
  2988. if (ppp->vj) {
  2989. slhc_free(ppp->vj);
  2990. ppp->vj = NULL;
  2991. }
  2992. skb_queue_purge(&ppp->file.xq);
  2993. skb_queue_purge(&ppp->file.rq);
  2994. #ifdef CONFIG_PPP_MULTILINK
  2995. skb_queue_purge(&ppp->mrq);
  2996. #endif /* CONFIG_PPP_MULTILINK */
  2997. #ifdef CONFIG_PPP_FILTER
  2998. if (ppp->pass_filter) {
  2999. bpf_prog_destroy(ppp->pass_filter);
  3000. ppp->pass_filter = NULL;
  3001. }
  3002. if (ppp->active_filter) {
  3003. bpf_prog_destroy(ppp->active_filter);
  3004. ppp->active_filter = NULL;
  3005. }
  3006. #endif /* CONFIG_PPP_FILTER */
  3007. kfree_skb(ppp->xmit_pending);
  3008. free_percpu(ppp->xmit_recursion);
  3009. free_netdev(ppp->dev);
  3010. }
  3011. /*
  3012. * Locate an existing ppp unit.
  3013. * The caller should have locked the all_ppp_mutex.
  3014. */
  3015. static struct ppp *
  3016. ppp_find_unit(struct ppp_net *pn, int unit)
  3017. {
  3018. return unit_find(&pn->units_idr, unit);
  3019. }
  3020. /*
  3021. * Locate an existing ppp channel.
  3022. * The caller should have locked the all_channels_lock.
  3023. * First we look in the new_channels list, then in the
  3024. * all_channels list. If found in the new_channels list,
  3025. * we move it to the all_channels list. This is for speed
  3026. * when we have a lot of channels in use.
  3027. */
  3028. static struct channel *
  3029. ppp_find_channel(struct ppp_net *pn, int unit)
  3030. {
  3031. struct channel *pch;
  3032. list_for_each_entry(pch, &pn->new_channels, list) {
  3033. if (pch->file.index == unit) {
  3034. list_move(&pch->list, &pn->all_channels);
  3035. return pch;
  3036. }
  3037. }
  3038. list_for_each_entry(pch, &pn->all_channels, list) {
  3039. if (pch->file.index == unit)
  3040. return pch;
  3041. }
  3042. return NULL;
  3043. }
  3044. /*
  3045. * Connect a PPP channel to a PPP interface unit.
  3046. */
  3047. static int
  3048. ppp_connect_channel(struct channel *pch, int unit)
  3049. {
  3050. struct ppp *ppp;
  3051. struct ppp_net *pn;
  3052. int ret = -ENXIO;
  3053. int hdrlen;
  3054. pn = ppp_pernet(pch->chan_net);
  3055. mutex_lock(&pn->all_ppp_mutex);
  3056. ppp = ppp_find_unit(pn, unit);
  3057. if (!ppp)
  3058. goto out;
  3059. write_lock_bh(&pch->upl);
  3060. ret = -EINVAL;
  3061. if (pch->ppp ||
  3062. rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl)))
  3063. goto outl;
  3064. ppp_lock(ppp);
  3065. spin_lock_bh(&pch->downl);
  3066. if (!pch->chan) {
  3067. /* Don't connect unregistered channels */
  3068. spin_unlock_bh(&pch->downl);
  3069. ppp_unlock(ppp);
  3070. ret = -ENOTCONN;
  3071. goto outl;
  3072. }
  3073. spin_unlock_bh(&pch->downl);
  3074. if (pch->file.hdrlen > ppp->file.hdrlen)
  3075. ppp->file.hdrlen = pch->file.hdrlen;
  3076. hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
  3077. if (hdrlen > ppp->dev->hard_header_len)
  3078. ppp->dev->hard_header_len = hdrlen;
  3079. list_add_tail_rcu(&pch->clist, &ppp->channels);
  3080. ++ppp->n_channels;
  3081. pch->ppp = ppp;
  3082. refcount_inc(&ppp->file.refcnt);
  3083. ppp_unlock(ppp);
  3084. ret = 0;
  3085. outl:
  3086. write_unlock_bh(&pch->upl);
  3087. out:
  3088. mutex_unlock(&pn->all_ppp_mutex);
  3089. return ret;
  3090. }
  3091. /*
  3092. * Disconnect a channel from its ppp unit.
  3093. */
  3094. static int
  3095. ppp_disconnect_channel(struct channel *pch)
  3096. {
  3097. struct ppp *ppp;
  3098. int err = -EINVAL;
  3099. write_lock_bh(&pch->upl);
  3100. ppp = pch->ppp;
  3101. pch->ppp = NULL;
  3102. write_unlock_bh(&pch->upl);
  3103. if (ppp) {
  3104. /* remove it from the ppp unit's list */
  3105. ppp_lock(ppp);
  3106. list_del_rcu(&pch->clist);
  3107. if (--ppp->n_channels == 0)
  3108. wake_up_interruptible(&ppp->file.rwait);
  3109. ppp_unlock(ppp);
  3110. synchronize_net();
  3111. if (refcount_dec_and_test(&ppp->file.refcnt))
  3112. ppp_destroy_interface(ppp);
  3113. err = 0;
  3114. }
  3115. return err;
  3116. }
  3117. /*
  3118. * Free up the resources used by a ppp channel.
  3119. */
  3120. static void ppp_destroy_channel(struct channel *pch)
  3121. {
  3122. put_net_track(pch->chan_net, &pch->ns_tracker);
  3123. pch->chan_net = NULL;
  3124. atomic_dec(&channel_count);
  3125. if (!pch->file.dead) {
  3126. /* "can't happen" */
  3127. pr_err("ppp: destroying undead channel %p !\n", pch);
  3128. return;
  3129. }
  3130. skb_queue_purge(&pch->file.xq);
  3131. skb_queue_purge(&pch->file.rq);
  3132. kfree(pch);
  3133. }
  3134. static void __exit ppp_cleanup(void)
  3135. {
  3136. /* should never happen */
  3137. if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
  3138. pr_err("PPP: removing module but units remain!\n");
  3139. rtnl_link_unregister(&ppp_link_ops);
  3140. unregister_chrdev(PPP_MAJOR, "ppp");
  3141. device_destroy(&ppp_class, MKDEV(PPP_MAJOR, 0));
  3142. class_unregister(&ppp_class);
  3143. unregister_pernet_device(&ppp_net_ops);
  3144. }
  3145. /*
  3146. * Units handling. Caller must protect concurrent access
  3147. * by holding all_ppp_mutex
  3148. */
  3149. /* associate pointer with specified number */
  3150. static int unit_set(struct idr *p, void *ptr, int n)
  3151. {
  3152. int unit;
  3153. unit = idr_alloc(p, ptr, n, n + 1, GFP_KERNEL);
  3154. if (unit == -ENOSPC)
  3155. unit = -EINVAL;
  3156. return unit;
  3157. }
  3158. /* get new free unit number and associate pointer with it */
  3159. static int unit_get(struct idr *p, void *ptr, int min)
  3160. {
  3161. return idr_alloc(p, ptr, min, 0, GFP_KERNEL);
  3162. }
  3163. /* put unit number back to a pool */
  3164. static void unit_put(struct idr *p, int n)
  3165. {
  3166. idr_remove(p, n);
  3167. }
  3168. /* get pointer associated with the number */
  3169. static void *unit_find(struct idr *p, int n)
  3170. {
  3171. return idr_find(p, n);
  3172. }
  3173. /* Module/initialization stuff */
  3174. module_init(ppp_init);
  3175. module_exit(ppp_cleanup);
  3176. EXPORT_SYMBOL(ppp_register_net_channel);
  3177. EXPORT_SYMBOL(ppp_register_channel);
  3178. EXPORT_SYMBOL(ppp_unregister_channel);
  3179. EXPORT_SYMBOL(ppp_channel_index);
  3180. EXPORT_SYMBOL(ppp_unit_number);
  3181. EXPORT_SYMBOL(ppp_dev_name);
  3182. EXPORT_SYMBOL(ppp_input);
  3183. EXPORT_SYMBOL(ppp_input_error);
  3184. EXPORT_SYMBOL(ppp_output_wakeup);
  3185. EXPORT_SYMBOL(ppp_register_compressor);
  3186. EXPORT_SYMBOL(ppp_unregister_compressor);
  3187. MODULE_DESCRIPTION("Generic PPP layer driver");
  3188. MODULE_LICENSE("GPL");
  3189. MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
  3190. MODULE_ALIAS_RTNL_LINK("ppp");
  3191. MODULE_ALIAS("devname:ppp");