ppp_generic.c 65 KB

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