ppp_generic.c 75 KB

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