ppp_generic.c 77 KB

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