ppp_generic.c 76 KB

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