ppp_generic.c 68 KB

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