ppp_generic.c 72 KB

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