ppp_generic.c 69 KB

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