ppp_generic.c 77 KB

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