ppp_generic.c 76 KB

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