ppp_generic.c 68 KB

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