ppp_generic.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985
  1. /*
  2. * Generic PPP layer for Linux.
  3. *
  4. * Copyright 1999-2002 Paul Mackerras.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * The generic PPP layer handles the PPP network interfaces, the
  12. * /dev/ppp device, packet and VJ compression, and multilink.
  13. * It talks to PPP `channels' via the interface defined in
  14. * include/linux/ppp_channel.h. Channels provide the basic means for
  15. * sending and receiving PPP frames on some kind of communications
  16. * channel.
  17. *
  18. * Part of the code in this driver was inspired by the old async-only
  19. * PPP driver, written by Michael Callahan and Al Longyear, and
  20. * subsequently hacked by Paul Mackerras.
  21. *
  22. * ==FILEVERSION 20041108==
  23. */
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/kmod.h>
  27. #include <linux/init.h>
  28. #include <linux/list.h>
  29. #include <linux/idr.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/poll.h>
  32. #include <linux/ppp_defs.h>
  33. #include <linux/filter.h>
  34. #include <linux/ppp-ioctl.h>
  35. #include <linux/ppp_channel.h>
  36. #include <linux/ppp-comp.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/rtnetlink.h>
  39. #include <linux/if_arp.h>
  40. #include <linux/ip.h>
  41. #include <linux/tcp.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/rwsem.h>
  44. #include <linux/stddef.h>
  45. #include <linux/device.h>
  46. #include <linux/mutex.h>
  47. #include <linux/slab.h>
  48. #include <asm/unaligned.h>
  49. #include <net/slhc_vj.h>
  50. #include <linux/atomic.h>
  51. #include <linux/nsproxy.h>
  52. #include <net/net_namespace.h>
  53. #include <net/netns/generic.h>
  54. #define PPP_VERSION "2.4.2"
  55. /*
  56. * Network protocols we support.
  57. */
  58. #define NP_IP 0 /* Internet Protocol V4 */
  59. #define NP_IPV6 1 /* Internet Protocol V6 */
  60. #define NP_IPX 2 /* IPX protocol */
  61. #define NP_AT 3 /* Appletalk protocol */
  62. #define NP_MPLS_UC 4 /* MPLS unicast */
  63. #define NP_MPLS_MC 5 /* MPLS multicast */
  64. #define NUM_NP 6 /* Number of NPs. */
  65. #define MPHDRLEN 6 /* multilink protocol header length */
  66. #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
  67. /*
  68. * An instance of /dev/ppp can be associated with either a ppp
  69. * interface unit or a ppp channel. In both cases, file->private_data
  70. * points to one of these.
  71. */
  72. struct ppp_file {
  73. enum {
  74. INTERFACE=1, CHANNEL
  75. } kind;
  76. struct sk_buff_head xq; /* pppd transmit queue */
  77. struct sk_buff_head rq; /* receive queue for pppd */
  78. wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
  79. atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
  80. int hdrlen; /* space to leave for headers */
  81. int index; /* interface unit / channel number */
  82. int dead; /* unit/channel has been shut down */
  83. };
  84. #define PF_TO_X(pf, X) container_of(pf, X, file)
  85. #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
  86. #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
  87. /*
  88. * Data structure 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 (IS_ERR(vj)) {
  634. err = PTR_ERR(vj);
  635. break;
  636. }
  637. ppp_lock(ppp);
  638. if (ppp->vj)
  639. slhc_free(ppp->vj);
  640. ppp->vj = vj;
  641. ppp_unlock(ppp);
  642. err = 0;
  643. break;
  644. case PPPIOCGNPMODE:
  645. case PPPIOCSNPMODE:
  646. if (copy_from_user(&npi, argp, sizeof(npi)))
  647. break;
  648. err = proto_to_npindex(npi.protocol);
  649. if (err < 0)
  650. break;
  651. i = err;
  652. if (cmd == PPPIOCGNPMODE) {
  653. err = -EFAULT;
  654. npi.mode = ppp->npmode[i];
  655. if (copy_to_user(argp, &npi, sizeof(npi)))
  656. break;
  657. } else {
  658. ppp->npmode[i] = npi.mode;
  659. /* we may be able to transmit more packets now (??) */
  660. netif_wake_queue(ppp->dev);
  661. }
  662. err = 0;
  663. break;
  664. #ifdef CONFIG_PPP_FILTER
  665. case PPPIOCSPASS:
  666. {
  667. struct sock_filter *code;
  668. err = get_filter(argp, &code);
  669. if (err >= 0) {
  670. ppp_lock(ppp);
  671. kfree(ppp->pass_filter);
  672. ppp->pass_filter = code;
  673. ppp->pass_len = err;
  674. ppp_unlock(ppp);
  675. err = 0;
  676. }
  677. break;
  678. }
  679. case PPPIOCSACTIVE:
  680. {
  681. struct sock_filter *code;
  682. err = get_filter(argp, &code);
  683. if (err >= 0) {
  684. ppp_lock(ppp);
  685. kfree(ppp->active_filter);
  686. ppp->active_filter = code;
  687. ppp->active_len = err;
  688. ppp_unlock(ppp);
  689. err = 0;
  690. }
  691. break;
  692. }
  693. #endif /* CONFIG_PPP_FILTER */
  694. #ifdef CONFIG_PPP_MULTILINK
  695. case PPPIOCSMRRU:
  696. if (get_user(val, p))
  697. break;
  698. ppp_recv_lock(ppp);
  699. ppp->mrru = val;
  700. ppp_recv_unlock(ppp);
  701. err = 0;
  702. break;
  703. #endif /* CONFIG_PPP_MULTILINK */
  704. default:
  705. err = -ENOTTY;
  706. }
  707. mutex_unlock(&ppp_mutex);
  708. return err;
  709. }
  710. static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
  711. struct file *file, unsigned int cmd, unsigned long arg)
  712. {
  713. int unit, err = -EFAULT;
  714. struct ppp *ppp;
  715. struct channel *chan;
  716. struct ppp_net *pn;
  717. int __user *p = (int __user *)arg;
  718. mutex_lock(&ppp_mutex);
  719. switch (cmd) {
  720. case PPPIOCNEWUNIT:
  721. /* Create a new ppp unit */
  722. if (get_user(unit, p))
  723. break;
  724. ppp = ppp_create_interface(net, unit, &err);
  725. if (!ppp)
  726. break;
  727. file->private_data = &ppp->file;
  728. ppp->owner = file;
  729. err = -EFAULT;
  730. if (put_user(ppp->file.index, p))
  731. break;
  732. err = 0;
  733. break;
  734. case PPPIOCATTACH:
  735. /* Attach to an existing ppp unit */
  736. if (get_user(unit, p))
  737. break;
  738. err = -ENXIO;
  739. pn = ppp_pernet(net);
  740. mutex_lock(&pn->all_ppp_mutex);
  741. ppp = ppp_find_unit(pn, unit);
  742. if (ppp) {
  743. atomic_inc(&ppp->file.refcnt);
  744. file->private_data = &ppp->file;
  745. err = 0;
  746. }
  747. mutex_unlock(&pn->all_ppp_mutex);
  748. break;
  749. case PPPIOCATTCHAN:
  750. if (get_user(unit, p))
  751. break;
  752. err = -ENXIO;
  753. pn = ppp_pernet(net);
  754. spin_lock_bh(&pn->all_channels_lock);
  755. chan = ppp_find_channel(pn, unit);
  756. if (chan) {
  757. atomic_inc(&chan->file.refcnt);
  758. file->private_data = &chan->file;
  759. err = 0;
  760. }
  761. spin_unlock_bh(&pn->all_channels_lock);
  762. break;
  763. default:
  764. err = -ENOTTY;
  765. }
  766. mutex_unlock(&ppp_mutex);
  767. return err;
  768. }
  769. static const struct file_operations ppp_device_fops = {
  770. .owner = THIS_MODULE,
  771. .read = ppp_read,
  772. .write = ppp_write,
  773. .poll = ppp_poll,
  774. .unlocked_ioctl = ppp_ioctl,
  775. .open = ppp_open,
  776. .release = ppp_release,
  777. .llseek = noop_llseek,
  778. };
  779. static __net_init int ppp_init_net(struct net *net)
  780. {
  781. struct ppp_net *pn = net_generic(net, ppp_net_id);
  782. idr_init(&pn->units_idr);
  783. mutex_init(&pn->all_ppp_mutex);
  784. INIT_LIST_HEAD(&pn->all_channels);
  785. INIT_LIST_HEAD(&pn->new_channels);
  786. spin_lock_init(&pn->all_channels_lock);
  787. return 0;
  788. }
  789. static __net_exit void ppp_exit_net(struct net *net)
  790. {
  791. struct ppp_net *pn = net_generic(net, ppp_net_id);
  792. idr_destroy(&pn->units_idr);
  793. }
  794. static struct pernet_operations ppp_net_ops = {
  795. .init = ppp_init_net,
  796. .exit = ppp_exit_net,
  797. .id = &ppp_net_id,
  798. .size = sizeof(struct ppp_net),
  799. };
  800. #define PPP_MAJOR 108
  801. /* Called at boot time if ppp is compiled into the kernel,
  802. or at module load time (from init_module) if compiled as a module. */
  803. static int __init ppp_init(void)
  804. {
  805. int err;
  806. pr_info("PPP generic driver version " PPP_VERSION "\n");
  807. err = register_pernet_device(&ppp_net_ops);
  808. if (err) {
  809. pr_err("failed to register PPP pernet device (%d)\n", err);
  810. goto out;
  811. }
  812. err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
  813. if (err) {
  814. pr_err("failed to register PPP device (%d)\n", err);
  815. goto out_net;
  816. }
  817. ppp_class = class_create(THIS_MODULE, "ppp");
  818. if (IS_ERR(ppp_class)) {
  819. err = PTR_ERR(ppp_class);
  820. goto out_chrdev;
  821. }
  822. /* not a big deal if we fail here :-) */
  823. device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
  824. return 0;
  825. out_chrdev:
  826. unregister_chrdev(PPP_MAJOR, "ppp");
  827. out_net:
  828. unregister_pernet_device(&ppp_net_ops);
  829. out:
  830. return err;
  831. }
  832. /*
  833. * Network interface unit routines.
  834. */
  835. static netdev_tx_t
  836. ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
  837. {
  838. struct ppp *ppp = netdev_priv(dev);
  839. int npi, proto;
  840. unsigned char *pp;
  841. npi = ethertype_to_npindex(ntohs(skb->protocol));
  842. if (npi < 0)
  843. goto outf;
  844. /* Drop, accept or reject the packet */
  845. switch (ppp->npmode[npi]) {
  846. case NPMODE_PASS:
  847. break;
  848. case NPMODE_QUEUE:
  849. /* it would be nice to have a way to tell the network
  850. system to queue this one up for later. */
  851. goto outf;
  852. case NPMODE_DROP:
  853. case NPMODE_ERROR:
  854. goto outf;
  855. }
  856. /* Put the 2-byte PPP protocol number on the front,
  857. making sure there is room for the address and control fields. */
  858. if (skb_cow_head(skb, PPP_HDRLEN))
  859. goto outf;
  860. pp = skb_push(skb, 2);
  861. proto = npindex_to_proto[npi];
  862. put_unaligned_be16(proto, pp);
  863. skb_queue_tail(&ppp->file.xq, skb);
  864. ppp_xmit_process(ppp);
  865. return NETDEV_TX_OK;
  866. outf:
  867. kfree_skb(skb);
  868. ++dev->stats.tx_dropped;
  869. return NETDEV_TX_OK;
  870. }
  871. static int
  872. ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  873. {
  874. struct ppp *ppp = netdev_priv(dev);
  875. int err = -EFAULT;
  876. void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
  877. struct ppp_stats stats;
  878. struct ppp_comp_stats cstats;
  879. char *vers;
  880. switch (cmd) {
  881. case SIOCGPPPSTATS:
  882. ppp_get_stats(ppp, &stats);
  883. if (copy_to_user(addr, &stats, sizeof(stats)))
  884. break;
  885. err = 0;
  886. break;
  887. case SIOCGPPPCSTATS:
  888. memset(&cstats, 0, sizeof(cstats));
  889. if (ppp->xc_state)
  890. ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
  891. if (ppp->rc_state)
  892. ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
  893. if (copy_to_user(addr, &cstats, sizeof(cstats)))
  894. break;
  895. err = 0;
  896. break;
  897. case SIOCGPPPVER:
  898. vers = PPP_VERSION;
  899. if (copy_to_user(addr, vers, strlen(vers) + 1))
  900. break;
  901. err = 0;
  902. break;
  903. default:
  904. err = -EINVAL;
  905. }
  906. return err;
  907. }
  908. static const struct net_device_ops ppp_netdev_ops = {
  909. .ndo_start_xmit = ppp_start_xmit,
  910. .ndo_do_ioctl = ppp_net_ioctl,
  911. };
  912. static void ppp_setup(struct net_device *dev)
  913. {
  914. dev->netdev_ops = &ppp_netdev_ops;
  915. dev->hard_header_len = PPP_HDRLEN;
  916. dev->mtu = PPP_MRU;
  917. dev->addr_len = 0;
  918. dev->tx_queue_len = 3;
  919. dev->type = ARPHRD_PPP;
  920. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  921. dev->features |= NETIF_F_NETNS_LOCAL;
  922. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  923. }
  924. /*
  925. * Transmit-side routines.
  926. */
  927. /*
  928. * Called to do any work queued up on the transmit side
  929. * that can now be done.
  930. */
  931. static void
  932. ppp_xmit_process(struct ppp *ppp)
  933. {
  934. struct sk_buff *skb;
  935. ppp_xmit_lock(ppp);
  936. if (!ppp->closing) {
  937. ppp_push(ppp);
  938. while (!ppp->xmit_pending &&
  939. (skb = skb_dequeue(&ppp->file.xq)))
  940. ppp_send_frame(ppp, skb);
  941. /* If there's no work left to do, tell the core net
  942. code that we can accept some more. */
  943. if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
  944. netif_wake_queue(ppp->dev);
  945. else
  946. netif_stop_queue(ppp->dev);
  947. }
  948. ppp_xmit_unlock(ppp);
  949. }
  950. static inline struct sk_buff *
  951. pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
  952. {
  953. struct sk_buff *new_skb;
  954. int len;
  955. int new_skb_size = ppp->dev->mtu +
  956. ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
  957. int compressor_skb_size = ppp->dev->mtu +
  958. ppp->xcomp->comp_extra + PPP_HDRLEN;
  959. new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
  960. if (!new_skb) {
  961. if (net_ratelimit())
  962. netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n");
  963. return NULL;
  964. }
  965. if (ppp->dev->hard_header_len > PPP_HDRLEN)
  966. skb_reserve(new_skb,
  967. ppp->dev->hard_header_len - PPP_HDRLEN);
  968. /* compressor still expects A/C bytes in hdr */
  969. len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
  970. new_skb->data, skb->len + 2,
  971. compressor_skb_size);
  972. if (len > 0 && (ppp->flags & SC_CCP_UP)) {
  973. kfree_skb(skb);
  974. skb = new_skb;
  975. skb_put(skb, len);
  976. skb_pull(skb, 2); /* pull off A/C bytes */
  977. } else if (len == 0) {
  978. /* didn't compress, or CCP not up yet */
  979. kfree_skb(new_skb);
  980. new_skb = skb;
  981. } else {
  982. /*
  983. * (len < 0)
  984. * MPPE requires that we do not send unencrypted
  985. * frames. The compressor will return -1 if we
  986. * should drop the frame. We cannot simply test
  987. * the compress_proto because MPPE and MPPC share
  988. * the same number.
  989. */
  990. if (net_ratelimit())
  991. netdev_err(ppp->dev, "ppp: compressor dropped pkt\n");
  992. kfree_skb(skb);
  993. kfree_skb(new_skb);
  994. new_skb = NULL;
  995. }
  996. return new_skb;
  997. }
  998. /*
  999. * Compress and send a frame.
  1000. * The caller should have locked the xmit path,
  1001. * and xmit_pending should be 0.
  1002. */
  1003. static void
  1004. ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
  1005. {
  1006. int proto = PPP_PROTO(skb);
  1007. struct sk_buff *new_skb;
  1008. int len;
  1009. unsigned char *cp;
  1010. if (proto < 0x8000) {
  1011. #ifdef CONFIG_PPP_FILTER
  1012. /* check if we should pass this packet */
  1013. /* the filter instructions are constructed assuming
  1014. a four-byte PPP header on each packet */
  1015. *skb_push(skb, 2) = 1;
  1016. if (ppp->pass_filter &&
  1017. sk_run_filter(skb, ppp->pass_filter) == 0) {
  1018. if (ppp->debug & 1)
  1019. netdev_printk(KERN_DEBUG, ppp->dev,
  1020. "PPP: outbound frame "
  1021. "not passed\n");
  1022. kfree_skb(skb);
  1023. return;
  1024. }
  1025. /* if this packet passes the active filter, record the time */
  1026. if (!(ppp->active_filter &&
  1027. sk_run_filter(skb, ppp->active_filter) == 0))
  1028. ppp->last_xmit = jiffies;
  1029. skb_pull(skb, 2);
  1030. #else
  1031. /* for data packets, record the time */
  1032. ppp->last_xmit = jiffies;
  1033. #endif /* CONFIG_PPP_FILTER */
  1034. }
  1035. ++ppp->dev->stats.tx_packets;
  1036. ppp->dev->stats.tx_bytes += skb->len - 2;
  1037. switch (proto) {
  1038. case PPP_IP:
  1039. if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
  1040. break;
  1041. /* try to do VJ TCP header compression */
  1042. new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
  1043. GFP_ATOMIC);
  1044. if (!new_skb) {
  1045. netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
  1046. goto drop;
  1047. }
  1048. skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
  1049. cp = skb->data + 2;
  1050. len = slhc_compress(ppp->vj, cp, skb->len - 2,
  1051. new_skb->data + 2, &cp,
  1052. !(ppp->flags & SC_NO_TCP_CCID));
  1053. if (cp == skb->data + 2) {
  1054. /* didn't compress */
  1055. kfree_skb(new_skb);
  1056. } else {
  1057. if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
  1058. proto = PPP_VJC_COMP;
  1059. cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
  1060. } else {
  1061. proto = PPP_VJC_UNCOMP;
  1062. cp[0] = skb->data[2];
  1063. }
  1064. kfree_skb(skb);
  1065. skb = new_skb;
  1066. cp = skb_put(skb, len + 2);
  1067. cp[0] = 0;
  1068. cp[1] = proto;
  1069. }
  1070. break;
  1071. case PPP_CCP:
  1072. /* peek at outbound CCP frames */
  1073. ppp_ccp_peek(ppp, skb, 0);
  1074. break;
  1075. }
  1076. /* try to do packet compression */
  1077. if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
  1078. proto != PPP_LCP && proto != PPP_CCP) {
  1079. if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
  1080. if (net_ratelimit())
  1081. netdev_err(ppp->dev,
  1082. "ppp: compression required but "
  1083. "down - pkt dropped.\n");
  1084. goto drop;
  1085. }
  1086. skb = pad_compress_skb(ppp, skb);
  1087. if (!skb)
  1088. goto drop;
  1089. }
  1090. /*
  1091. * If we are waiting for traffic (demand dialling),
  1092. * queue it up for pppd to receive.
  1093. */
  1094. if (ppp->flags & SC_LOOP_TRAFFIC) {
  1095. if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
  1096. goto drop;
  1097. skb_queue_tail(&ppp->file.rq, skb);
  1098. wake_up_interruptible(&ppp->file.rwait);
  1099. return;
  1100. }
  1101. ppp->xmit_pending = skb;
  1102. ppp_push(ppp);
  1103. return;
  1104. drop:
  1105. kfree_skb(skb);
  1106. ++ppp->dev->stats.tx_errors;
  1107. }
  1108. /*
  1109. * Try to send the frame in xmit_pending.
  1110. * The caller should have the xmit path locked.
  1111. */
  1112. static void
  1113. ppp_push(struct ppp *ppp)
  1114. {
  1115. struct list_head *list;
  1116. struct channel *pch;
  1117. struct sk_buff *skb = ppp->xmit_pending;
  1118. if (!skb)
  1119. return;
  1120. list = &ppp->channels;
  1121. if (list_empty(list)) {
  1122. /* nowhere to send the packet, just drop it */
  1123. ppp->xmit_pending = NULL;
  1124. kfree_skb(skb);
  1125. return;
  1126. }
  1127. if ((ppp->flags & SC_MULTILINK) == 0) {
  1128. /* not doing multilink: send it down the first channel */
  1129. list = list->next;
  1130. pch = list_entry(list, struct channel, clist);
  1131. spin_lock_bh(&pch->downl);
  1132. if (pch->chan) {
  1133. if (pch->chan->ops->start_xmit(pch->chan, skb))
  1134. ppp->xmit_pending = NULL;
  1135. } else {
  1136. /* channel got unregistered */
  1137. kfree_skb(skb);
  1138. ppp->xmit_pending = NULL;
  1139. }
  1140. spin_unlock_bh(&pch->downl);
  1141. return;
  1142. }
  1143. #ifdef CONFIG_PPP_MULTILINK
  1144. /* Multilink: fragment the packet over as many links
  1145. as can take the packet at the moment. */
  1146. if (!ppp_mp_explode(ppp, skb))
  1147. return;
  1148. #endif /* CONFIG_PPP_MULTILINK */
  1149. ppp->xmit_pending = NULL;
  1150. kfree_skb(skb);
  1151. }
  1152. #ifdef CONFIG_PPP_MULTILINK
  1153. static bool mp_protocol_compress __read_mostly = true;
  1154. module_param(mp_protocol_compress, bool, S_IRUGO | S_IWUSR);
  1155. MODULE_PARM_DESC(mp_protocol_compress,
  1156. "compress protocol id in multilink fragments");
  1157. /*
  1158. * Divide a packet to be transmitted into fragments and
  1159. * send them out the individual links.
  1160. */
  1161. static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
  1162. {
  1163. int len, totlen;
  1164. int i, bits, hdrlen, mtu;
  1165. int flen;
  1166. int navail, nfree, nzero;
  1167. int nbigger;
  1168. int totspeed;
  1169. int totfree;
  1170. unsigned char *p, *q;
  1171. struct list_head *list;
  1172. struct channel *pch;
  1173. struct sk_buff *frag;
  1174. struct ppp_channel *chan;
  1175. totspeed = 0; /*total bitrate of the bundle*/
  1176. nfree = 0; /* # channels which have no packet already queued */
  1177. navail = 0; /* total # of usable channels (not deregistered) */
  1178. nzero = 0; /* number of channels with zero speed associated*/
  1179. totfree = 0; /*total # of channels available and
  1180. *having no queued packets before
  1181. *starting the fragmentation*/
  1182. hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  1183. i = 0;
  1184. list_for_each_entry(pch, &ppp->channels, clist) {
  1185. if (pch->chan) {
  1186. pch->avail = 1;
  1187. navail++;
  1188. pch->speed = pch->chan->speed;
  1189. } else {
  1190. pch->avail = 0;
  1191. }
  1192. if (pch->avail) {
  1193. if (skb_queue_empty(&pch->file.xq) ||
  1194. !pch->had_frag) {
  1195. if (pch->speed == 0)
  1196. nzero++;
  1197. else
  1198. totspeed += pch->speed;
  1199. pch->avail = 2;
  1200. ++nfree;
  1201. ++totfree;
  1202. }
  1203. if (!pch->had_frag && i < ppp->nxchan)
  1204. ppp->nxchan = i;
  1205. }
  1206. ++i;
  1207. }
  1208. /*
  1209. * Don't start sending this packet unless at least half of
  1210. * the channels are free. This gives much better TCP
  1211. * performance if we have a lot of channels.
  1212. */
  1213. if (nfree == 0 || nfree < navail / 2)
  1214. return 0; /* can't take now, leave it in xmit_pending */
  1215. /* Do protocol field compression */
  1216. p = skb->data;
  1217. len = skb->len;
  1218. if (*p == 0 && mp_protocol_compress) {
  1219. ++p;
  1220. --len;
  1221. }
  1222. totlen = len;
  1223. nbigger = len % nfree;
  1224. /* skip to the channel after the one we last used
  1225. and start at that one */
  1226. list = &ppp->channels;
  1227. for (i = 0; i < ppp->nxchan; ++i) {
  1228. list = list->next;
  1229. if (list == &ppp->channels) {
  1230. i = 0;
  1231. break;
  1232. }
  1233. }
  1234. /* create a fragment for each channel */
  1235. bits = B;
  1236. while (len > 0) {
  1237. list = list->next;
  1238. if (list == &ppp->channels) {
  1239. i = 0;
  1240. continue;
  1241. }
  1242. pch = list_entry(list, struct channel, clist);
  1243. ++i;
  1244. if (!pch->avail)
  1245. continue;
  1246. /*
  1247. * Skip this channel if it has a fragment pending already and
  1248. * we haven't given a fragment to all of the free channels.
  1249. */
  1250. if (pch->avail == 1) {
  1251. if (nfree > 0)
  1252. continue;
  1253. } else {
  1254. pch->avail = 1;
  1255. }
  1256. /* check the channel's mtu and whether it is still attached. */
  1257. spin_lock_bh(&pch->downl);
  1258. if (pch->chan == NULL) {
  1259. /* can't use this channel, it's being deregistered */
  1260. if (pch->speed == 0)
  1261. nzero--;
  1262. else
  1263. totspeed -= pch->speed;
  1264. spin_unlock_bh(&pch->downl);
  1265. pch->avail = 0;
  1266. totlen = len;
  1267. totfree--;
  1268. nfree--;
  1269. if (--navail == 0)
  1270. break;
  1271. continue;
  1272. }
  1273. /*
  1274. *if the channel speed is not set divide
  1275. *the packet evenly among the free channels;
  1276. *otherwise divide it according to the speed
  1277. *of the channel we are going to transmit on
  1278. */
  1279. flen = len;
  1280. if (nfree > 0) {
  1281. if (pch->speed == 0) {
  1282. flen = len/nfree;
  1283. if (nbigger > 0) {
  1284. flen++;
  1285. nbigger--;
  1286. }
  1287. } else {
  1288. flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
  1289. ((totspeed*totfree)/pch->speed)) - hdrlen;
  1290. if (nbigger > 0) {
  1291. flen += ((totfree - nzero)*pch->speed)/totspeed;
  1292. nbigger -= ((totfree - nzero)*pch->speed)/
  1293. totspeed;
  1294. }
  1295. }
  1296. nfree--;
  1297. }
  1298. /*
  1299. *check if we are on the last channel or
  1300. *we exceded the length of the data to
  1301. *fragment
  1302. */
  1303. if ((nfree <= 0) || (flen > len))
  1304. flen = len;
  1305. /*
  1306. *it is not worth to tx on slow channels:
  1307. *in that case from the resulting flen according to the
  1308. *above formula will be equal or less than zero.
  1309. *Skip the channel in this case
  1310. */
  1311. if (flen <= 0) {
  1312. pch->avail = 2;
  1313. spin_unlock_bh(&pch->downl);
  1314. continue;
  1315. }
  1316. /*
  1317. * hdrlen includes the 2-byte PPP protocol field, but the
  1318. * MTU counts only the payload excluding the protocol field.
  1319. * (RFC1661 Section 2)
  1320. */
  1321. mtu = pch->chan->mtu - (hdrlen - 2);
  1322. if (mtu < 4)
  1323. mtu = 4;
  1324. if (flen > mtu)
  1325. flen = mtu;
  1326. if (flen == len)
  1327. bits |= E;
  1328. frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
  1329. if (!frag)
  1330. goto noskb;
  1331. q = skb_put(frag, flen + hdrlen);
  1332. /* make the MP header */
  1333. put_unaligned_be16(PPP_MP, q);
  1334. if (ppp->flags & SC_MP_XSHORTSEQ) {
  1335. q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
  1336. q[3] = ppp->nxseq;
  1337. } else {
  1338. q[2] = bits;
  1339. q[3] = ppp->nxseq >> 16;
  1340. q[4] = ppp->nxseq >> 8;
  1341. q[5] = ppp->nxseq;
  1342. }
  1343. memcpy(q + hdrlen, p, flen);
  1344. /* try to send it down the channel */
  1345. chan = pch->chan;
  1346. if (!skb_queue_empty(&pch->file.xq) ||
  1347. !chan->ops->start_xmit(chan, frag))
  1348. skb_queue_tail(&pch->file.xq, frag);
  1349. pch->had_frag = 1;
  1350. p += flen;
  1351. len -= flen;
  1352. ++ppp->nxseq;
  1353. bits = 0;
  1354. spin_unlock_bh(&pch->downl);
  1355. }
  1356. ppp->nxchan = i;
  1357. return 1;
  1358. noskb:
  1359. spin_unlock_bh(&pch->downl);
  1360. if (ppp->debug & 1)
  1361. netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
  1362. ++ppp->dev->stats.tx_errors;
  1363. ++ppp->nxseq;
  1364. return 1; /* abandon the frame */
  1365. }
  1366. #endif /* CONFIG_PPP_MULTILINK */
  1367. /*
  1368. * Try to send data out on a channel.
  1369. */
  1370. static void
  1371. ppp_channel_push(struct channel *pch)
  1372. {
  1373. struct sk_buff *skb;
  1374. struct ppp *ppp;
  1375. spin_lock_bh(&pch->downl);
  1376. if (pch->chan) {
  1377. while (!skb_queue_empty(&pch->file.xq)) {
  1378. skb = skb_dequeue(&pch->file.xq);
  1379. if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
  1380. /* put the packet back and try again later */
  1381. skb_queue_head(&pch->file.xq, skb);
  1382. break;
  1383. }
  1384. }
  1385. } else {
  1386. /* channel got deregistered */
  1387. skb_queue_purge(&pch->file.xq);
  1388. }
  1389. spin_unlock_bh(&pch->downl);
  1390. /* see if there is anything from the attached unit to be sent */
  1391. if (skb_queue_empty(&pch->file.xq)) {
  1392. read_lock_bh(&pch->upl);
  1393. ppp = pch->ppp;
  1394. if (ppp)
  1395. ppp_xmit_process(ppp);
  1396. read_unlock_bh(&pch->upl);
  1397. }
  1398. }
  1399. /*
  1400. * Receive-side routines.
  1401. */
  1402. struct ppp_mp_skb_parm {
  1403. u32 sequence;
  1404. u8 BEbits;
  1405. };
  1406. #define PPP_MP_CB(skb) ((struct ppp_mp_skb_parm *)((skb)->cb))
  1407. static inline void
  1408. ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1409. {
  1410. ppp_recv_lock(ppp);
  1411. if (!ppp->closing)
  1412. ppp_receive_frame(ppp, skb, pch);
  1413. else
  1414. kfree_skb(skb);
  1415. ppp_recv_unlock(ppp);
  1416. }
  1417. void
  1418. ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
  1419. {
  1420. struct channel *pch = chan->ppp;
  1421. int proto;
  1422. if (!pch) {
  1423. kfree_skb(skb);
  1424. return;
  1425. }
  1426. read_lock_bh(&pch->upl);
  1427. if (!pskb_may_pull(skb, 2)) {
  1428. kfree_skb(skb);
  1429. if (pch->ppp) {
  1430. ++pch->ppp->dev->stats.rx_length_errors;
  1431. ppp_receive_error(pch->ppp);
  1432. }
  1433. goto done;
  1434. }
  1435. proto = PPP_PROTO(skb);
  1436. if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
  1437. /* put it on the channel queue */
  1438. skb_queue_tail(&pch->file.rq, skb);
  1439. /* drop old frames if queue too long */
  1440. while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
  1441. (skb = skb_dequeue(&pch->file.rq)))
  1442. kfree_skb(skb);
  1443. wake_up_interruptible(&pch->file.rwait);
  1444. } else {
  1445. ppp_do_recv(pch->ppp, skb, pch);
  1446. }
  1447. done:
  1448. read_unlock_bh(&pch->upl);
  1449. }
  1450. /* Put a 0-length skb in the receive queue as an error indication */
  1451. void
  1452. ppp_input_error(struct ppp_channel *chan, int code)
  1453. {
  1454. struct channel *pch = chan->ppp;
  1455. struct sk_buff *skb;
  1456. if (!pch)
  1457. return;
  1458. read_lock_bh(&pch->upl);
  1459. if (pch->ppp) {
  1460. skb = alloc_skb(0, GFP_ATOMIC);
  1461. if (skb) {
  1462. skb->len = 0; /* probably unnecessary */
  1463. skb->cb[0] = code;
  1464. ppp_do_recv(pch->ppp, skb, pch);
  1465. }
  1466. }
  1467. read_unlock_bh(&pch->upl);
  1468. }
  1469. /*
  1470. * We come in here to process a received frame.
  1471. * The receive side of the ppp unit is locked.
  1472. */
  1473. static void
  1474. ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1475. {
  1476. /* note: a 0-length skb is used as an error indication */
  1477. if (skb->len > 0) {
  1478. #ifdef CONFIG_PPP_MULTILINK
  1479. /* XXX do channel-level decompression here */
  1480. if (PPP_PROTO(skb) == PPP_MP)
  1481. ppp_receive_mp_frame(ppp, skb, pch);
  1482. else
  1483. #endif /* CONFIG_PPP_MULTILINK */
  1484. ppp_receive_nonmp_frame(ppp, skb);
  1485. } else {
  1486. kfree_skb(skb);
  1487. ppp_receive_error(ppp);
  1488. }
  1489. }
  1490. static void
  1491. ppp_receive_error(struct ppp *ppp)
  1492. {
  1493. ++ppp->dev->stats.rx_errors;
  1494. if (ppp->vj)
  1495. slhc_toss(ppp->vj);
  1496. }
  1497. static void
  1498. ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
  1499. {
  1500. struct sk_buff *ns;
  1501. int proto, len, npi;
  1502. /*
  1503. * Decompress the frame, if compressed.
  1504. * Note that some decompressors need to see uncompressed frames
  1505. * that come in as well as compressed frames.
  1506. */
  1507. if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
  1508. (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
  1509. skb = ppp_decompress_frame(ppp, skb);
  1510. if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
  1511. goto err;
  1512. proto = PPP_PROTO(skb);
  1513. switch (proto) {
  1514. case PPP_VJC_COMP:
  1515. /* decompress VJ compressed packets */
  1516. if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
  1517. goto err;
  1518. if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
  1519. /* copy to a new sk_buff with more tailroom */
  1520. ns = dev_alloc_skb(skb->len + 128);
  1521. if (!ns) {
  1522. netdev_err(ppp->dev, "PPP: no memory "
  1523. "(VJ decomp)\n");
  1524. goto err;
  1525. }
  1526. skb_reserve(ns, 2);
  1527. skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
  1528. kfree_skb(skb);
  1529. skb = ns;
  1530. }
  1531. else
  1532. skb->ip_summed = CHECKSUM_NONE;
  1533. len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
  1534. if (len <= 0) {
  1535. netdev_printk(KERN_DEBUG, ppp->dev,
  1536. "PPP: VJ decompression error\n");
  1537. goto err;
  1538. }
  1539. len += 2;
  1540. if (len > skb->len)
  1541. skb_put(skb, len - skb->len);
  1542. else if (len < skb->len)
  1543. skb_trim(skb, len);
  1544. proto = PPP_IP;
  1545. break;
  1546. case PPP_VJC_UNCOMP:
  1547. if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
  1548. goto err;
  1549. /* Until we fix the decompressor need to make sure
  1550. * data portion is linear.
  1551. */
  1552. if (!pskb_may_pull(skb, skb->len))
  1553. goto err;
  1554. if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
  1555. netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
  1556. goto err;
  1557. }
  1558. proto = PPP_IP;
  1559. break;
  1560. case PPP_CCP:
  1561. ppp_ccp_peek(ppp, skb, 1);
  1562. break;
  1563. }
  1564. ++ppp->dev->stats.rx_packets;
  1565. ppp->dev->stats.rx_bytes += skb->len - 2;
  1566. npi = proto_to_npindex(proto);
  1567. if (npi < 0) {
  1568. /* control or unknown frame - pass it to pppd */
  1569. skb_queue_tail(&ppp->file.rq, skb);
  1570. /* limit queue length by dropping old frames */
  1571. while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
  1572. (skb = skb_dequeue(&ppp->file.rq)))
  1573. kfree_skb(skb);
  1574. /* wake up any process polling or blocking on read */
  1575. wake_up_interruptible(&ppp->file.rwait);
  1576. } else {
  1577. /* network protocol frame - give it to the kernel */
  1578. #ifdef CONFIG_PPP_FILTER
  1579. /* check if the packet passes the pass and active filters */
  1580. /* the filter instructions are constructed assuming
  1581. a four-byte PPP header on each packet */
  1582. if (ppp->pass_filter || ppp->active_filter) {
  1583. if (skb_cloned(skb) &&
  1584. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  1585. goto err;
  1586. *skb_push(skb, 2) = 0;
  1587. if (ppp->pass_filter &&
  1588. sk_run_filter(skb, ppp->pass_filter) == 0) {
  1589. if (ppp->debug & 1)
  1590. netdev_printk(KERN_DEBUG, ppp->dev,
  1591. "PPP: inbound frame "
  1592. "not passed\n");
  1593. kfree_skb(skb);
  1594. return;
  1595. }
  1596. if (!(ppp->active_filter &&
  1597. sk_run_filter(skb, ppp->active_filter) == 0))
  1598. ppp->last_recv = jiffies;
  1599. __skb_pull(skb, 2);
  1600. } else
  1601. #endif /* CONFIG_PPP_FILTER */
  1602. ppp->last_recv = jiffies;
  1603. if ((ppp->dev->flags & IFF_UP) == 0 ||
  1604. ppp->npmode[npi] != NPMODE_PASS) {
  1605. kfree_skb(skb);
  1606. } else {
  1607. /* chop off protocol */
  1608. skb_pull_rcsum(skb, 2);
  1609. skb->dev = ppp->dev;
  1610. skb->protocol = htons(npindex_to_ethertype[npi]);
  1611. skb_reset_mac_header(skb);
  1612. netif_rx(skb);
  1613. }
  1614. }
  1615. return;
  1616. err:
  1617. kfree_skb(skb);
  1618. ppp_receive_error(ppp);
  1619. }
  1620. static struct sk_buff *
  1621. ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
  1622. {
  1623. int proto = PPP_PROTO(skb);
  1624. struct sk_buff *ns;
  1625. int len;
  1626. /* Until we fix all the decompressor's need to make sure
  1627. * data portion is linear.
  1628. */
  1629. if (!pskb_may_pull(skb, skb->len))
  1630. goto err;
  1631. if (proto == PPP_COMP) {
  1632. int obuff_size;
  1633. switch(ppp->rcomp->compress_proto) {
  1634. case CI_MPPE:
  1635. obuff_size = ppp->mru + PPP_HDRLEN + 1;
  1636. break;
  1637. default:
  1638. obuff_size = ppp->mru + PPP_HDRLEN;
  1639. break;
  1640. }
  1641. ns = dev_alloc_skb(obuff_size);
  1642. if (!ns) {
  1643. netdev_err(ppp->dev, "ppp_decompress_frame: "
  1644. "no memory\n");
  1645. goto err;
  1646. }
  1647. /* the decompressor still expects the A/C bytes in the hdr */
  1648. len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
  1649. skb->len + 2, ns->data, obuff_size);
  1650. if (len < 0) {
  1651. /* Pass the compressed frame to pppd as an
  1652. error indication. */
  1653. if (len == DECOMP_FATALERROR)
  1654. ppp->rstate |= SC_DC_FERROR;
  1655. kfree_skb(ns);
  1656. goto err;
  1657. }
  1658. kfree_skb(skb);
  1659. skb = ns;
  1660. skb_put(skb, len);
  1661. skb_pull(skb, 2); /* pull off the A/C bytes */
  1662. } else {
  1663. /* Uncompressed frame - pass to decompressor so it
  1664. can update its dictionary if necessary. */
  1665. if (ppp->rcomp->incomp)
  1666. ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
  1667. skb->len + 2);
  1668. }
  1669. return skb;
  1670. err:
  1671. ppp->rstate |= SC_DC_ERROR;
  1672. ppp_receive_error(ppp);
  1673. return skb;
  1674. }
  1675. #ifdef CONFIG_PPP_MULTILINK
  1676. /*
  1677. * Receive a multilink frame.
  1678. * We put it on the reconstruction queue and then pull off
  1679. * as many completed frames as we can.
  1680. */
  1681. static void
  1682. ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1683. {
  1684. u32 mask, seq;
  1685. struct channel *ch;
  1686. int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  1687. if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
  1688. goto err; /* no good, throw it away */
  1689. /* Decode sequence number and begin/end bits */
  1690. if (ppp->flags & SC_MP_SHORTSEQ) {
  1691. seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
  1692. mask = 0xfff;
  1693. } else {
  1694. seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
  1695. mask = 0xffffff;
  1696. }
  1697. PPP_MP_CB(skb)->BEbits = skb->data[2];
  1698. skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
  1699. /*
  1700. * Do protocol ID decompression on the first fragment of each packet.
  1701. */
  1702. if ((PPP_MP_CB(skb)->BEbits & B) && (skb->data[0] & 1))
  1703. *skb_push(skb, 1) = 0;
  1704. /*
  1705. * Expand sequence number to 32 bits, making it as close
  1706. * as possible to ppp->minseq.
  1707. */
  1708. seq |= ppp->minseq & ~mask;
  1709. if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
  1710. seq += mask + 1;
  1711. else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
  1712. seq -= mask + 1; /* should never happen */
  1713. PPP_MP_CB(skb)->sequence = seq;
  1714. pch->lastseq = seq;
  1715. /*
  1716. * If this packet comes before the next one we were expecting,
  1717. * drop it.
  1718. */
  1719. if (seq_before(seq, ppp->nextseq)) {
  1720. kfree_skb(skb);
  1721. ++ppp->dev->stats.rx_dropped;
  1722. ppp_receive_error(ppp);
  1723. return;
  1724. }
  1725. /*
  1726. * Reevaluate minseq, the minimum over all channels of the
  1727. * last sequence number received on each channel. Because of
  1728. * the increasing sequence number rule, we know that any fragment
  1729. * before `minseq' which hasn't arrived is never going to arrive.
  1730. * The list of channels can't change because we have the receive
  1731. * side of the ppp unit locked.
  1732. */
  1733. list_for_each_entry(ch, &ppp->channels, clist) {
  1734. if (seq_before(ch->lastseq, seq))
  1735. seq = ch->lastseq;
  1736. }
  1737. if (seq_before(ppp->minseq, seq))
  1738. ppp->minseq = seq;
  1739. /* Put the fragment on the reconstruction queue */
  1740. ppp_mp_insert(ppp, skb);
  1741. /* If the queue is getting long, don't wait any longer for packets
  1742. before the start of the queue. */
  1743. if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
  1744. struct sk_buff *mskb = skb_peek(&ppp->mrq);
  1745. if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
  1746. ppp->minseq = PPP_MP_CB(mskb)->sequence;
  1747. }
  1748. /* Pull completed packets off the queue and receive them. */
  1749. while ((skb = ppp_mp_reconstruct(ppp))) {
  1750. if (pskb_may_pull(skb, 2))
  1751. ppp_receive_nonmp_frame(ppp, skb);
  1752. else {
  1753. ++ppp->dev->stats.rx_length_errors;
  1754. kfree_skb(skb);
  1755. ppp_receive_error(ppp);
  1756. }
  1757. }
  1758. return;
  1759. err:
  1760. kfree_skb(skb);
  1761. ppp_receive_error(ppp);
  1762. }
  1763. /*
  1764. * Insert a fragment on the MP reconstruction queue.
  1765. * The queue is ordered by increasing sequence number.
  1766. */
  1767. static void
  1768. ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
  1769. {
  1770. struct sk_buff *p;
  1771. struct sk_buff_head *list = &ppp->mrq;
  1772. u32 seq = PPP_MP_CB(skb)->sequence;
  1773. /* N.B. we don't need to lock the list lock because we have the
  1774. ppp unit receive-side lock. */
  1775. skb_queue_walk(list, p) {
  1776. if (seq_before(seq, PPP_MP_CB(p)->sequence))
  1777. break;
  1778. }
  1779. __skb_queue_before(list, p, skb);
  1780. }
  1781. /*
  1782. * Reconstruct a packet from the MP fragment queue.
  1783. * We go through increasing sequence numbers until we find a
  1784. * complete packet, or we get to the sequence number for a fragment
  1785. * which hasn't arrived but might still do so.
  1786. */
  1787. static struct sk_buff *
  1788. ppp_mp_reconstruct(struct ppp *ppp)
  1789. {
  1790. u32 seq = ppp->nextseq;
  1791. u32 minseq = ppp->minseq;
  1792. struct sk_buff_head *list = &ppp->mrq;
  1793. struct sk_buff *p, *tmp;
  1794. struct sk_buff *head, *tail;
  1795. struct sk_buff *skb = NULL;
  1796. int lost = 0, len = 0;
  1797. if (ppp->mrru == 0) /* do nothing until mrru is set */
  1798. return NULL;
  1799. head = list->next;
  1800. tail = NULL;
  1801. skb_queue_walk_safe(list, p, tmp) {
  1802. again:
  1803. if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
  1804. /* this can't happen, anyway ignore the skb */
  1805. netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
  1806. "seq %u < %u\n",
  1807. PPP_MP_CB(p)->sequence, seq);
  1808. __skb_unlink(p, list);
  1809. kfree_skb(p);
  1810. continue;
  1811. }
  1812. if (PPP_MP_CB(p)->sequence != seq) {
  1813. u32 oldseq;
  1814. /* Fragment `seq' is missing. If it is after
  1815. minseq, it might arrive later, so stop here. */
  1816. if (seq_after(seq, minseq))
  1817. break;
  1818. /* Fragment `seq' is lost, keep going. */
  1819. lost = 1;
  1820. oldseq = seq;
  1821. seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
  1822. minseq + 1: PPP_MP_CB(p)->sequence;
  1823. if (ppp->debug & 1)
  1824. netdev_printk(KERN_DEBUG, ppp->dev,
  1825. "lost frag %u..%u\n",
  1826. oldseq, seq-1);
  1827. goto again;
  1828. }
  1829. /*
  1830. * At this point we know that all the fragments from
  1831. * ppp->nextseq to seq are either present or lost.
  1832. * Also, there are no complete packets in the queue
  1833. * that have no missing fragments and end before this
  1834. * fragment.
  1835. */
  1836. /* B bit set indicates this fragment starts a packet */
  1837. if (PPP_MP_CB(p)->BEbits & B) {
  1838. head = p;
  1839. lost = 0;
  1840. len = 0;
  1841. }
  1842. len += p->len;
  1843. /* Got a complete packet yet? */
  1844. if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
  1845. (PPP_MP_CB(head)->BEbits & B)) {
  1846. if (len > ppp->mrru + 2) {
  1847. ++ppp->dev->stats.rx_length_errors;
  1848. netdev_printk(KERN_DEBUG, ppp->dev,
  1849. "PPP: reconstructed packet"
  1850. " is too long (%d)\n", len);
  1851. } else {
  1852. tail = p;
  1853. break;
  1854. }
  1855. ppp->nextseq = seq + 1;
  1856. }
  1857. /*
  1858. * If this is the ending fragment of a packet,
  1859. * and we haven't found a complete valid packet yet,
  1860. * we can discard up to and including this fragment.
  1861. */
  1862. if (PPP_MP_CB(p)->BEbits & E) {
  1863. struct sk_buff *tmp2;
  1864. skb_queue_reverse_walk_from_safe(list, p, tmp2) {
  1865. if (ppp->debug & 1)
  1866. netdev_printk(KERN_DEBUG, ppp->dev,
  1867. "discarding frag %u\n",
  1868. PPP_MP_CB(p)->sequence);
  1869. __skb_unlink(p, list);
  1870. kfree_skb(p);
  1871. }
  1872. head = skb_peek(list);
  1873. if (!head)
  1874. break;
  1875. }
  1876. ++seq;
  1877. }
  1878. /* If we have a complete packet, copy it all into one skb. */
  1879. if (tail != NULL) {
  1880. /* If we have discarded any fragments,
  1881. signal a receive error. */
  1882. if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
  1883. skb_queue_walk_safe(list, p, tmp) {
  1884. if (p == head)
  1885. break;
  1886. if (ppp->debug & 1)
  1887. netdev_printk(KERN_DEBUG, ppp->dev,
  1888. "discarding frag %u\n",
  1889. PPP_MP_CB(p)->sequence);
  1890. __skb_unlink(p, list);
  1891. kfree_skb(p);
  1892. }
  1893. if (ppp->debug & 1)
  1894. netdev_printk(KERN_DEBUG, ppp->dev,
  1895. " missed pkts %u..%u\n",
  1896. ppp->nextseq,
  1897. PPP_MP_CB(head)->sequence-1);
  1898. ++ppp->dev->stats.rx_dropped;
  1899. ppp_receive_error(ppp);
  1900. }
  1901. skb = head;
  1902. if (head != tail) {
  1903. struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
  1904. p = skb_queue_next(list, head);
  1905. __skb_unlink(skb, list);
  1906. skb_queue_walk_from_safe(list, p, tmp) {
  1907. __skb_unlink(p, list);
  1908. *fragpp = p;
  1909. p->next = NULL;
  1910. fragpp = &p->next;
  1911. skb->len += p->len;
  1912. skb->data_len += p->len;
  1913. skb->truesize += p->truesize;
  1914. if (p == tail)
  1915. break;
  1916. }
  1917. } else {
  1918. __skb_unlink(skb, list);
  1919. }
  1920. ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
  1921. }
  1922. return skb;
  1923. }
  1924. #endif /* CONFIG_PPP_MULTILINK */
  1925. /*
  1926. * Channel interface.
  1927. */
  1928. /* Create a new, unattached ppp channel. */
  1929. int ppp_register_channel(struct ppp_channel *chan)
  1930. {
  1931. return ppp_register_net_channel(current->nsproxy->net_ns, chan);
  1932. }
  1933. /* Create a new, unattached ppp channel for specified net. */
  1934. int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
  1935. {
  1936. struct channel *pch;
  1937. struct ppp_net *pn;
  1938. pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
  1939. if (!pch)
  1940. return -ENOMEM;
  1941. pn = ppp_pernet(net);
  1942. pch->ppp = NULL;
  1943. pch->chan = chan;
  1944. pch->chan_net = get_net(net);
  1945. chan->ppp = pch;
  1946. init_ppp_file(&pch->file, CHANNEL);
  1947. pch->file.hdrlen = chan->hdrlen;
  1948. #ifdef CONFIG_PPP_MULTILINK
  1949. pch->lastseq = -1;
  1950. #endif /* CONFIG_PPP_MULTILINK */
  1951. init_rwsem(&pch->chan_sem);
  1952. spin_lock_init(&pch->downl);
  1953. rwlock_init(&pch->upl);
  1954. spin_lock_bh(&pn->all_channels_lock);
  1955. pch->file.index = ++pn->last_channel_index;
  1956. list_add(&pch->list, &pn->new_channels);
  1957. atomic_inc(&channel_count);
  1958. spin_unlock_bh(&pn->all_channels_lock);
  1959. return 0;
  1960. }
  1961. /*
  1962. * Return the index of a channel.
  1963. */
  1964. int ppp_channel_index(struct ppp_channel *chan)
  1965. {
  1966. struct channel *pch = chan->ppp;
  1967. if (pch)
  1968. return pch->file.index;
  1969. return -1;
  1970. }
  1971. /*
  1972. * Return the PPP unit number to which a channel is connected.
  1973. */
  1974. int ppp_unit_number(struct ppp_channel *chan)
  1975. {
  1976. struct channel *pch = chan->ppp;
  1977. int unit = -1;
  1978. if (pch) {
  1979. read_lock_bh(&pch->upl);
  1980. if (pch->ppp)
  1981. unit = pch->ppp->file.index;
  1982. read_unlock_bh(&pch->upl);
  1983. }
  1984. return unit;
  1985. }
  1986. /*
  1987. * Return the PPP device interface name of a channel.
  1988. */
  1989. char *ppp_dev_name(struct ppp_channel *chan)
  1990. {
  1991. struct channel *pch = chan->ppp;
  1992. char *name = NULL;
  1993. if (pch) {
  1994. read_lock_bh(&pch->upl);
  1995. if (pch->ppp && pch->ppp->dev)
  1996. name = pch->ppp->dev->name;
  1997. read_unlock_bh(&pch->upl);
  1998. }
  1999. return name;
  2000. }
  2001. /*
  2002. * Disconnect a channel from the generic layer.
  2003. * This must be called in process context.
  2004. */
  2005. void
  2006. ppp_unregister_channel(struct ppp_channel *chan)
  2007. {
  2008. struct channel *pch = chan->ppp;
  2009. struct ppp_net *pn;
  2010. if (!pch)
  2011. return; /* should never happen */
  2012. chan->ppp = NULL;
  2013. /*
  2014. * This ensures that we have returned from any calls into the
  2015. * the channel's start_xmit or ioctl routine before we proceed.
  2016. */
  2017. down_write(&pch->chan_sem);
  2018. spin_lock_bh(&pch->downl);
  2019. pch->chan = NULL;
  2020. spin_unlock_bh(&pch->downl);
  2021. up_write(&pch->chan_sem);
  2022. ppp_disconnect_channel(pch);
  2023. pn = ppp_pernet(pch->chan_net);
  2024. spin_lock_bh(&pn->all_channels_lock);
  2025. list_del(&pch->list);
  2026. spin_unlock_bh(&pn->all_channels_lock);
  2027. pch->file.dead = 1;
  2028. wake_up_interruptible(&pch->file.rwait);
  2029. if (atomic_dec_and_test(&pch->file.refcnt))
  2030. ppp_destroy_channel(pch);
  2031. }
  2032. /*
  2033. * Callback from a channel when it can accept more to transmit.
  2034. * This should be called at BH/softirq level, not interrupt level.
  2035. */
  2036. void
  2037. ppp_output_wakeup(struct ppp_channel *chan)
  2038. {
  2039. struct channel *pch = chan->ppp;
  2040. if (!pch)
  2041. return;
  2042. ppp_channel_push(pch);
  2043. }
  2044. /*
  2045. * Compression control.
  2046. */
  2047. /* Process the PPPIOCSCOMPRESS ioctl. */
  2048. static int
  2049. ppp_set_compress(struct ppp *ppp, unsigned long arg)
  2050. {
  2051. int err;
  2052. struct compressor *cp, *ocomp;
  2053. struct ppp_option_data data;
  2054. void *state, *ostate;
  2055. unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
  2056. err = -EFAULT;
  2057. if (copy_from_user(&data, (void __user *) arg, sizeof(data)) ||
  2058. (data.length <= CCP_MAX_OPTION_LENGTH &&
  2059. copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
  2060. goto out;
  2061. err = -EINVAL;
  2062. if (data.length > CCP_MAX_OPTION_LENGTH ||
  2063. ccp_option[1] < 2 || ccp_option[1] > data.length)
  2064. goto out;
  2065. cp = try_then_request_module(
  2066. find_compressor(ccp_option[0]),
  2067. "ppp-compress-%d", ccp_option[0]);
  2068. if (!cp)
  2069. goto out;
  2070. err = -ENOBUFS;
  2071. if (data.transmit) {
  2072. state = cp->comp_alloc(ccp_option, data.length);
  2073. if (state) {
  2074. ppp_xmit_lock(ppp);
  2075. ppp->xstate &= ~SC_COMP_RUN;
  2076. ocomp = ppp->xcomp;
  2077. ostate = ppp->xc_state;
  2078. ppp->xcomp = cp;
  2079. ppp->xc_state = state;
  2080. ppp_xmit_unlock(ppp);
  2081. if (ostate) {
  2082. ocomp->comp_free(ostate);
  2083. module_put(ocomp->owner);
  2084. }
  2085. err = 0;
  2086. } else
  2087. module_put(cp->owner);
  2088. } else {
  2089. state = cp->decomp_alloc(ccp_option, data.length);
  2090. if (state) {
  2091. ppp_recv_lock(ppp);
  2092. ppp->rstate &= ~SC_DECOMP_RUN;
  2093. ocomp = ppp->rcomp;
  2094. ostate = ppp->rc_state;
  2095. ppp->rcomp = cp;
  2096. ppp->rc_state = state;
  2097. ppp_recv_unlock(ppp);
  2098. if (ostate) {
  2099. ocomp->decomp_free(ostate);
  2100. module_put(ocomp->owner);
  2101. }
  2102. err = 0;
  2103. } else
  2104. module_put(cp->owner);
  2105. }
  2106. out:
  2107. return err;
  2108. }
  2109. /*
  2110. * Look at a CCP packet and update our state accordingly.
  2111. * We assume the caller has the xmit or recv path locked.
  2112. */
  2113. static void
  2114. ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
  2115. {
  2116. unsigned char *dp;
  2117. int len;
  2118. if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
  2119. return; /* no header */
  2120. dp = skb->data + 2;
  2121. switch (CCP_CODE(dp)) {
  2122. case CCP_CONFREQ:
  2123. /* A ConfReq starts negotiation of compression
  2124. * in one direction of transmission,
  2125. * and hence brings it down...but which way?
  2126. *
  2127. * Remember:
  2128. * A ConfReq indicates what the sender would like to receive
  2129. */
  2130. if(inbound)
  2131. /* He is proposing what I should send */
  2132. ppp->xstate &= ~SC_COMP_RUN;
  2133. else
  2134. /* I am proposing to what he should send */
  2135. ppp->rstate &= ~SC_DECOMP_RUN;
  2136. break;
  2137. case CCP_TERMREQ:
  2138. case CCP_TERMACK:
  2139. /*
  2140. * CCP is going down, both directions of transmission
  2141. */
  2142. ppp->rstate &= ~SC_DECOMP_RUN;
  2143. ppp->xstate &= ~SC_COMP_RUN;
  2144. break;
  2145. case CCP_CONFACK:
  2146. if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
  2147. break;
  2148. len = CCP_LENGTH(dp);
  2149. if (!pskb_may_pull(skb, len + 2))
  2150. return; /* too short */
  2151. dp += CCP_HDRLEN;
  2152. len -= CCP_HDRLEN;
  2153. if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
  2154. break;
  2155. if (inbound) {
  2156. /* we will start receiving compressed packets */
  2157. if (!ppp->rc_state)
  2158. break;
  2159. if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
  2160. ppp->file.index, 0, ppp->mru, ppp->debug)) {
  2161. ppp->rstate |= SC_DECOMP_RUN;
  2162. ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
  2163. }
  2164. } else {
  2165. /* we will soon start sending compressed packets */
  2166. if (!ppp->xc_state)
  2167. break;
  2168. if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
  2169. ppp->file.index, 0, ppp->debug))
  2170. ppp->xstate |= SC_COMP_RUN;
  2171. }
  2172. break;
  2173. case CCP_RESETACK:
  2174. /* reset the [de]compressor */
  2175. if ((ppp->flags & SC_CCP_UP) == 0)
  2176. break;
  2177. if (inbound) {
  2178. if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
  2179. ppp->rcomp->decomp_reset(ppp->rc_state);
  2180. ppp->rstate &= ~SC_DC_ERROR;
  2181. }
  2182. } else {
  2183. if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
  2184. ppp->xcomp->comp_reset(ppp->xc_state);
  2185. }
  2186. break;
  2187. }
  2188. }
  2189. /* Free up compression resources. */
  2190. static void
  2191. ppp_ccp_closed(struct ppp *ppp)
  2192. {
  2193. void *xstate, *rstate;
  2194. struct compressor *xcomp, *rcomp;
  2195. ppp_lock(ppp);
  2196. ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
  2197. ppp->xstate = 0;
  2198. xcomp = ppp->xcomp;
  2199. xstate = ppp->xc_state;
  2200. ppp->xc_state = NULL;
  2201. ppp->rstate = 0;
  2202. rcomp = ppp->rcomp;
  2203. rstate = ppp->rc_state;
  2204. ppp->rc_state = NULL;
  2205. ppp_unlock(ppp);
  2206. if (xstate) {
  2207. xcomp->comp_free(xstate);
  2208. module_put(xcomp->owner);
  2209. }
  2210. if (rstate) {
  2211. rcomp->decomp_free(rstate);
  2212. module_put(rcomp->owner);
  2213. }
  2214. }
  2215. /* List of compressors. */
  2216. static LIST_HEAD(compressor_list);
  2217. static DEFINE_SPINLOCK(compressor_list_lock);
  2218. struct compressor_entry {
  2219. struct list_head list;
  2220. struct compressor *comp;
  2221. };
  2222. static struct compressor_entry *
  2223. find_comp_entry(int proto)
  2224. {
  2225. struct compressor_entry *ce;
  2226. list_for_each_entry(ce, &compressor_list, list) {
  2227. if (ce->comp->compress_proto == proto)
  2228. return ce;
  2229. }
  2230. return NULL;
  2231. }
  2232. /* Register a compressor */
  2233. int
  2234. ppp_register_compressor(struct compressor *cp)
  2235. {
  2236. struct compressor_entry *ce;
  2237. int ret;
  2238. spin_lock(&compressor_list_lock);
  2239. ret = -EEXIST;
  2240. if (find_comp_entry(cp->compress_proto))
  2241. goto out;
  2242. ret = -ENOMEM;
  2243. ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
  2244. if (!ce)
  2245. goto out;
  2246. ret = 0;
  2247. ce->comp = cp;
  2248. list_add(&ce->list, &compressor_list);
  2249. out:
  2250. spin_unlock(&compressor_list_lock);
  2251. return ret;
  2252. }
  2253. /* Unregister a compressor */
  2254. void
  2255. ppp_unregister_compressor(struct compressor *cp)
  2256. {
  2257. struct compressor_entry *ce;
  2258. spin_lock(&compressor_list_lock);
  2259. ce = find_comp_entry(cp->compress_proto);
  2260. if (ce && ce->comp == cp) {
  2261. list_del(&ce->list);
  2262. kfree(ce);
  2263. }
  2264. spin_unlock(&compressor_list_lock);
  2265. }
  2266. /* Find a compressor. */
  2267. static struct compressor *
  2268. find_compressor(int type)
  2269. {
  2270. struct compressor_entry *ce;
  2271. struct compressor *cp = NULL;
  2272. spin_lock(&compressor_list_lock);
  2273. ce = find_comp_entry(type);
  2274. if (ce) {
  2275. cp = ce->comp;
  2276. if (!try_module_get(cp->owner))
  2277. cp = NULL;
  2278. }
  2279. spin_unlock(&compressor_list_lock);
  2280. return cp;
  2281. }
  2282. /*
  2283. * Miscelleneous stuff.
  2284. */
  2285. static void
  2286. ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
  2287. {
  2288. struct slcompress *vj = ppp->vj;
  2289. memset(st, 0, sizeof(*st));
  2290. st->p.ppp_ipackets = ppp->dev->stats.rx_packets;
  2291. st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
  2292. st->p.ppp_ibytes = ppp->dev->stats.rx_bytes;
  2293. st->p.ppp_opackets = ppp->dev->stats.tx_packets;
  2294. st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
  2295. st->p.ppp_obytes = ppp->dev->stats.tx_bytes;
  2296. if (!vj)
  2297. return;
  2298. st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
  2299. st->vj.vjs_compressed = vj->sls_o_compressed;
  2300. st->vj.vjs_searches = vj->sls_o_searches;
  2301. st->vj.vjs_misses = vj->sls_o_misses;
  2302. st->vj.vjs_errorin = vj->sls_i_error;
  2303. st->vj.vjs_tossed = vj->sls_i_tossed;
  2304. st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
  2305. st->vj.vjs_compressedin = vj->sls_i_compressed;
  2306. }
  2307. /*
  2308. * Stuff for handling the lists of ppp units and channels
  2309. * and for initialization.
  2310. */
  2311. /*
  2312. * Create a new ppp interface unit. Fails if it can't allocate memory
  2313. * or if there is already a unit with the requested number.
  2314. * unit == -1 means allocate a new number.
  2315. */
  2316. static struct ppp *
  2317. ppp_create_interface(struct net *net, int unit, int *retp)
  2318. {
  2319. struct ppp *ppp;
  2320. struct ppp_net *pn;
  2321. struct net_device *dev = NULL;
  2322. int ret = -ENOMEM;
  2323. int i;
  2324. dev = alloc_netdev(sizeof(struct ppp), "", ppp_setup);
  2325. if (!dev)
  2326. goto out1;
  2327. pn = ppp_pernet(net);
  2328. ppp = netdev_priv(dev);
  2329. ppp->dev = dev;
  2330. ppp->mru = PPP_MRU;
  2331. init_ppp_file(&ppp->file, INTERFACE);
  2332. ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
  2333. for (i = 0; i < NUM_NP; ++i)
  2334. ppp->npmode[i] = NPMODE_PASS;
  2335. INIT_LIST_HEAD(&ppp->channels);
  2336. spin_lock_init(&ppp->rlock);
  2337. spin_lock_init(&ppp->wlock);
  2338. #ifdef CONFIG_PPP_MULTILINK
  2339. ppp->minseq = -1;
  2340. skb_queue_head_init(&ppp->mrq);
  2341. #endif /* CONFIG_PPP_MULTILINK */
  2342. /*
  2343. * drum roll: don't forget to set
  2344. * the net device is belong to
  2345. */
  2346. dev_net_set(dev, net);
  2347. mutex_lock(&pn->all_ppp_mutex);
  2348. if (unit < 0) {
  2349. unit = unit_get(&pn->units_idr, ppp);
  2350. if (unit < 0) {
  2351. ret = unit;
  2352. goto out2;
  2353. }
  2354. } else {
  2355. ret = -EEXIST;
  2356. if (unit_find(&pn->units_idr, unit))
  2357. goto out2; /* unit already exists */
  2358. /*
  2359. * if caller need a specified unit number
  2360. * lets try to satisfy him, otherwise --
  2361. * he should better ask us for new unit number
  2362. *
  2363. * NOTE: yes I know that returning EEXIST it's not
  2364. * fair but at least pppd will ask us to allocate
  2365. * new unit in this case so user is happy :)
  2366. */
  2367. unit = unit_set(&pn->units_idr, ppp, unit);
  2368. if (unit < 0)
  2369. goto out2;
  2370. }
  2371. /* Initialize the new ppp unit */
  2372. ppp->file.index = unit;
  2373. sprintf(dev->name, "ppp%d", unit);
  2374. ret = register_netdev(dev);
  2375. if (ret != 0) {
  2376. unit_put(&pn->units_idr, unit);
  2377. netdev_err(ppp->dev, "PPP: couldn't register device %s (%d)\n",
  2378. dev->name, ret);
  2379. goto out2;
  2380. }
  2381. ppp->ppp_net = net;
  2382. atomic_inc(&ppp_unit_count);
  2383. mutex_unlock(&pn->all_ppp_mutex);
  2384. *retp = 0;
  2385. return ppp;
  2386. out2:
  2387. mutex_unlock(&pn->all_ppp_mutex);
  2388. free_netdev(dev);
  2389. out1:
  2390. *retp = ret;
  2391. return NULL;
  2392. }
  2393. /*
  2394. * Initialize a ppp_file structure.
  2395. */
  2396. static void
  2397. init_ppp_file(struct ppp_file *pf, int kind)
  2398. {
  2399. pf->kind = kind;
  2400. skb_queue_head_init(&pf->xq);
  2401. skb_queue_head_init(&pf->rq);
  2402. atomic_set(&pf->refcnt, 1);
  2403. init_waitqueue_head(&pf->rwait);
  2404. }
  2405. /*
  2406. * Take down a ppp interface unit - called when the owning file
  2407. * (the one that created the unit) is closed or detached.
  2408. */
  2409. static void ppp_shutdown_interface(struct ppp *ppp)
  2410. {
  2411. struct ppp_net *pn;
  2412. pn = ppp_pernet(ppp->ppp_net);
  2413. mutex_lock(&pn->all_ppp_mutex);
  2414. /* This will call dev_close() for us. */
  2415. ppp_lock(ppp);
  2416. if (!ppp->closing) {
  2417. ppp->closing = 1;
  2418. ppp_unlock(ppp);
  2419. unregister_netdev(ppp->dev);
  2420. unit_put(&pn->units_idr, ppp->file.index);
  2421. } else
  2422. ppp_unlock(ppp);
  2423. ppp->file.dead = 1;
  2424. ppp->owner = NULL;
  2425. wake_up_interruptible(&ppp->file.rwait);
  2426. mutex_unlock(&pn->all_ppp_mutex);
  2427. }
  2428. /*
  2429. * Free the memory used by a ppp unit. This is only called once
  2430. * there are no channels connected to the unit and no file structs
  2431. * that reference the unit.
  2432. */
  2433. static void ppp_destroy_interface(struct ppp *ppp)
  2434. {
  2435. atomic_dec(&ppp_unit_count);
  2436. if (!ppp->file.dead || ppp->n_channels) {
  2437. /* "can't happen" */
  2438. netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
  2439. "but dead=%d n_channels=%d !\n",
  2440. ppp, ppp->file.dead, ppp->n_channels);
  2441. return;
  2442. }
  2443. ppp_ccp_closed(ppp);
  2444. if (ppp->vj) {
  2445. slhc_free(ppp->vj);
  2446. ppp->vj = NULL;
  2447. }
  2448. skb_queue_purge(&ppp->file.xq);
  2449. skb_queue_purge(&ppp->file.rq);
  2450. #ifdef CONFIG_PPP_MULTILINK
  2451. skb_queue_purge(&ppp->mrq);
  2452. #endif /* CONFIG_PPP_MULTILINK */
  2453. #ifdef CONFIG_PPP_FILTER
  2454. kfree(ppp->pass_filter);
  2455. ppp->pass_filter = NULL;
  2456. kfree(ppp->active_filter);
  2457. ppp->active_filter = NULL;
  2458. #endif /* CONFIG_PPP_FILTER */
  2459. kfree_skb(ppp->xmit_pending);
  2460. free_netdev(ppp->dev);
  2461. }
  2462. /*
  2463. * Locate an existing ppp unit.
  2464. * The caller should have locked the all_ppp_mutex.
  2465. */
  2466. static struct ppp *
  2467. ppp_find_unit(struct ppp_net *pn, int unit)
  2468. {
  2469. return unit_find(&pn->units_idr, unit);
  2470. }
  2471. /*
  2472. * Locate an existing ppp channel.
  2473. * The caller should have locked the all_channels_lock.
  2474. * First we look in the new_channels list, then in the
  2475. * all_channels list. If found in the new_channels list,
  2476. * we move it to the all_channels list. This is for speed
  2477. * when we have a lot of channels in use.
  2478. */
  2479. static struct channel *
  2480. ppp_find_channel(struct ppp_net *pn, int unit)
  2481. {
  2482. struct channel *pch;
  2483. list_for_each_entry(pch, &pn->new_channels, list) {
  2484. if (pch->file.index == unit) {
  2485. list_move(&pch->list, &pn->all_channels);
  2486. return pch;
  2487. }
  2488. }
  2489. list_for_each_entry(pch, &pn->all_channels, list) {
  2490. if (pch->file.index == unit)
  2491. return pch;
  2492. }
  2493. return NULL;
  2494. }
  2495. /*
  2496. * Connect a PPP channel to a PPP interface unit.
  2497. */
  2498. static int
  2499. ppp_connect_channel(struct channel *pch, int unit)
  2500. {
  2501. struct ppp *ppp;
  2502. struct ppp_net *pn;
  2503. int ret = -ENXIO;
  2504. int hdrlen;
  2505. pn = ppp_pernet(pch->chan_net);
  2506. mutex_lock(&pn->all_ppp_mutex);
  2507. ppp = ppp_find_unit(pn, unit);
  2508. if (!ppp)
  2509. goto out;
  2510. write_lock_bh(&pch->upl);
  2511. ret = -EINVAL;
  2512. if (pch->ppp)
  2513. goto outl;
  2514. ppp_lock(ppp);
  2515. if (pch->file.hdrlen > ppp->file.hdrlen)
  2516. ppp->file.hdrlen = pch->file.hdrlen;
  2517. hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
  2518. if (hdrlen > ppp->dev->hard_header_len)
  2519. ppp->dev->hard_header_len = hdrlen;
  2520. list_add_tail(&pch->clist, &ppp->channels);
  2521. ++ppp->n_channels;
  2522. pch->ppp = ppp;
  2523. atomic_inc(&ppp->file.refcnt);
  2524. ppp_unlock(ppp);
  2525. ret = 0;
  2526. outl:
  2527. write_unlock_bh(&pch->upl);
  2528. out:
  2529. mutex_unlock(&pn->all_ppp_mutex);
  2530. return ret;
  2531. }
  2532. /*
  2533. * Disconnect a channel from its ppp unit.
  2534. */
  2535. static int
  2536. ppp_disconnect_channel(struct channel *pch)
  2537. {
  2538. struct ppp *ppp;
  2539. int err = -EINVAL;
  2540. write_lock_bh(&pch->upl);
  2541. ppp = pch->ppp;
  2542. pch->ppp = NULL;
  2543. write_unlock_bh(&pch->upl);
  2544. if (ppp) {
  2545. /* remove it from the ppp unit's list */
  2546. ppp_lock(ppp);
  2547. list_del(&pch->clist);
  2548. if (--ppp->n_channels == 0)
  2549. wake_up_interruptible(&ppp->file.rwait);
  2550. ppp_unlock(ppp);
  2551. if (atomic_dec_and_test(&ppp->file.refcnt))
  2552. ppp_destroy_interface(ppp);
  2553. err = 0;
  2554. }
  2555. return err;
  2556. }
  2557. /*
  2558. * Free up the resources used by a ppp channel.
  2559. */
  2560. static void ppp_destroy_channel(struct channel *pch)
  2561. {
  2562. put_net(pch->chan_net);
  2563. pch->chan_net = NULL;
  2564. atomic_dec(&channel_count);
  2565. if (!pch->file.dead) {
  2566. /* "can't happen" */
  2567. pr_err("ppp: destroying undead channel %p !\n", pch);
  2568. return;
  2569. }
  2570. skb_queue_purge(&pch->file.xq);
  2571. skb_queue_purge(&pch->file.rq);
  2572. kfree(pch);
  2573. }
  2574. static void __exit ppp_cleanup(void)
  2575. {
  2576. /* should never happen */
  2577. if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
  2578. pr_err("PPP: removing module but units remain!\n");
  2579. unregister_chrdev(PPP_MAJOR, "ppp");
  2580. device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
  2581. class_destroy(ppp_class);
  2582. unregister_pernet_device(&ppp_net_ops);
  2583. }
  2584. /*
  2585. * Units handling. Caller must protect concurrent access
  2586. * by holding all_ppp_mutex
  2587. */
  2588. static int __unit_alloc(struct idr *p, void *ptr, int n)
  2589. {
  2590. int unit, err;
  2591. again:
  2592. if (!idr_pre_get(p, GFP_KERNEL)) {
  2593. pr_err("PPP: No free memory for idr\n");
  2594. return -ENOMEM;
  2595. }
  2596. err = idr_get_new_above(p, ptr, n, &unit);
  2597. if (err < 0) {
  2598. if (err == -EAGAIN)
  2599. goto again;
  2600. return err;
  2601. }
  2602. return unit;
  2603. }
  2604. /* associate pointer with specified number */
  2605. static int unit_set(struct idr *p, void *ptr, int n)
  2606. {
  2607. int unit;
  2608. unit = __unit_alloc(p, ptr, n);
  2609. if (unit < 0)
  2610. return unit;
  2611. else if (unit != n) {
  2612. idr_remove(p, unit);
  2613. return -EINVAL;
  2614. }
  2615. return unit;
  2616. }
  2617. /* get new free unit number and associate pointer with it */
  2618. static int unit_get(struct idr *p, void *ptr)
  2619. {
  2620. return __unit_alloc(p, ptr, 0);
  2621. }
  2622. /* put unit number back to a pool */
  2623. static void unit_put(struct idr *p, int n)
  2624. {
  2625. idr_remove(p, n);
  2626. }
  2627. /* get pointer associated with the number */
  2628. static void *unit_find(struct idr *p, int n)
  2629. {
  2630. return idr_find(p, n);
  2631. }
  2632. /* Module/initialization stuff */
  2633. module_init(ppp_init);
  2634. module_exit(ppp_cleanup);
  2635. EXPORT_SYMBOL(ppp_register_net_channel);
  2636. EXPORT_SYMBOL(ppp_register_channel);
  2637. EXPORT_SYMBOL(ppp_unregister_channel);
  2638. EXPORT_SYMBOL(ppp_channel_index);
  2639. EXPORT_SYMBOL(ppp_unit_number);
  2640. EXPORT_SYMBOL(ppp_dev_name);
  2641. EXPORT_SYMBOL(ppp_input);
  2642. EXPORT_SYMBOL(ppp_input_error);
  2643. EXPORT_SYMBOL(ppp_output_wakeup);
  2644. EXPORT_SYMBOL(ppp_register_compressor);
  2645. EXPORT_SYMBOL(ppp_unregister_compressor);
  2646. MODULE_LICENSE("GPL");
  2647. MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
  2648. MODULE_ALIAS("devname:ppp");