ppp_generic.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814
  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/netdevice.h>
  30. #include <linux/poll.h>
  31. #include <linux/ppp_defs.h>
  32. #include <linux/filter.h>
  33. #include <linux/if_ppp.h>
  34. #include <linux/ppp_channel.h>
  35. #include <linux/ppp-comp.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/rtnetlink.h>
  38. #include <linux/if_arp.h>
  39. #include <linux/ip.h>
  40. #include <linux/tcp.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/smp_lock.h>
  43. #include <linux/rwsem.h>
  44. #include <linux/stddef.h>
  45. #include <linux/device.h>
  46. #include <linux/mutex.h>
  47. #include <net/slhc_vj.h>
  48. #include <asm/atomic.h>
  49. #define PPP_VERSION "2.4.2"
  50. /*
  51. * Network protocols we support.
  52. */
  53. #define NP_IP 0 /* Internet Protocol V4 */
  54. #define NP_IPV6 1 /* Internet Protocol V6 */
  55. #define NP_IPX 2 /* IPX protocol */
  56. #define NP_AT 3 /* Appletalk protocol */
  57. #define NP_MPLS_UC 4 /* MPLS unicast */
  58. #define NP_MPLS_MC 5 /* MPLS multicast */
  59. #define NUM_NP 6 /* Number of NPs. */
  60. #define MPHDRLEN 6 /* multilink protocol header length */
  61. #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
  62. #define MIN_FRAG_SIZE 64
  63. /*
  64. * An instance of /dev/ppp can be associated with either a ppp
  65. * interface unit or a ppp channel. In both cases, file->private_data
  66. * points to one of these.
  67. */
  68. struct ppp_file {
  69. enum {
  70. INTERFACE=1, CHANNEL
  71. } kind;
  72. struct sk_buff_head xq; /* pppd transmit queue */
  73. struct sk_buff_head rq; /* receive queue for pppd */
  74. wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
  75. atomic_t refcnt; /* # refs (incl /dev/ppp attached) */
  76. int hdrlen; /* space to leave for headers */
  77. int index; /* interface unit / channel number */
  78. int dead; /* unit/channel has been shut down */
  79. };
  80. #define PF_TO_X(pf, X) container_of(pf, X, file)
  81. #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
  82. #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
  83. #define ROUNDUP(n, x) (((n) + (x) - 1) / (x))
  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. #ifdef CONFIG_PPP_MULTILINK
  113. int nxchan; /* next channel to send something on */
  114. u32 nxseq; /* next sequence number to send */
  115. int mrru; /* MP: max reconst. receive unit */
  116. u32 nextseq; /* MP: seq no of next packet */
  117. u32 minseq; /* MP: min of most recent seqnos */
  118. struct sk_buff_head mrq; /* MP: receive reconstruction queue */
  119. #endif /* CONFIG_PPP_MULTILINK */
  120. struct net_device_stats stats; /* statistics */
  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. * A cardmap represents a mapping from unsigned integers to pointers,
  166. * and provides a fast "find lowest unused number" operation.
  167. * It uses a broad (32-way) tree with a bitmap at each level.
  168. * It is designed to be space-efficient for small numbers of entries
  169. * and time-efficient for large numbers of entries.
  170. */
  171. #define CARDMAP_ORDER 5
  172. #define CARDMAP_WIDTH (1U << CARDMAP_ORDER)
  173. #define CARDMAP_MASK (CARDMAP_WIDTH - 1)
  174. struct cardmap {
  175. int shift;
  176. unsigned long inuse;
  177. struct cardmap *parent;
  178. void *ptr[CARDMAP_WIDTH];
  179. };
  180. static void *cardmap_get(struct cardmap *map, unsigned int nr);
  181. static int cardmap_set(struct cardmap **map, unsigned int nr, void *ptr);
  182. static unsigned int cardmap_find_first_free(struct cardmap *map);
  183. static void cardmap_destroy(struct cardmap **map);
  184. /*
  185. * all_ppp_mutex protects the all_ppp_units mapping.
  186. * It also ensures that finding a ppp unit in the all_ppp_units map
  187. * and updating its file.refcnt field is atomic.
  188. */
  189. static DEFINE_MUTEX(all_ppp_mutex);
  190. static struct cardmap *all_ppp_units;
  191. static atomic_t ppp_unit_count = ATOMIC_INIT(0);
  192. /*
  193. * all_channels_lock protects all_channels and last_channel_index,
  194. * and the atomicity of find a channel and updating its file.refcnt
  195. * field.
  196. */
  197. static DEFINE_SPINLOCK(all_channels_lock);
  198. static LIST_HEAD(all_channels);
  199. static LIST_HEAD(new_channels);
  200. static int last_channel_index;
  201. static atomic_t channel_count = ATOMIC_INIT(0);
  202. /* Get the PPP protocol number from a skb */
  203. #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
  204. /* We limit the length of ppp->file.rq to this (arbitrary) value */
  205. #define PPP_MAX_RQLEN 32
  206. /*
  207. * Maximum number of multilink fragments queued up.
  208. * This has to be large enough to cope with the maximum latency of
  209. * the slowest channel relative to the others. Strictly it should
  210. * depend on the number of channels and their characteristics.
  211. */
  212. #define PPP_MP_MAX_QLEN 128
  213. /* Multilink header bits. */
  214. #define B 0x80 /* this fragment begins a packet */
  215. #define E 0x40 /* this fragment ends a packet */
  216. /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
  217. #define seq_before(a, b) ((s32)((a) - (b)) < 0)
  218. #define seq_after(a, b) ((s32)((a) - (b)) > 0)
  219. /* Prototypes. */
  220. static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
  221. unsigned int cmd, unsigned long arg);
  222. static void ppp_xmit_process(struct ppp *ppp);
  223. static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
  224. static void ppp_push(struct ppp *ppp);
  225. static void ppp_channel_push(struct channel *pch);
  226. static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
  227. struct channel *pch);
  228. static void ppp_receive_error(struct ppp *ppp);
  229. static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
  230. static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
  231. struct sk_buff *skb);
  232. #ifdef CONFIG_PPP_MULTILINK
  233. static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
  234. struct channel *pch);
  235. static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
  236. static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
  237. static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
  238. #endif /* CONFIG_PPP_MULTILINK */
  239. static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
  240. static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
  241. static void ppp_ccp_closed(struct ppp *ppp);
  242. static struct compressor *find_compressor(int type);
  243. static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
  244. static struct ppp *ppp_create_interface(int unit, int *retp);
  245. static void init_ppp_file(struct ppp_file *pf, int kind);
  246. static void ppp_shutdown_interface(struct ppp *ppp);
  247. static void ppp_destroy_interface(struct ppp *ppp);
  248. static struct ppp *ppp_find_unit(int unit);
  249. static struct channel *ppp_find_channel(int unit);
  250. static int ppp_connect_channel(struct channel *pch, int unit);
  251. static int ppp_disconnect_channel(struct channel *pch);
  252. static void ppp_destroy_channel(struct channel *pch);
  253. static struct class *ppp_class;
  254. /* Translates a PPP protocol number to a NP index (NP == network protocol) */
  255. static inline int proto_to_npindex(int proto)
  256. {
  257. switch (proto) {
  258. case PPP_IP:
  259. return NP_IP;
  260. case PPP_IPV6:
  261. return NP_IPV6;
  262. case PPP_IPX:
  263. return NP_IPX;
  264. case PPP_AT:
  265. return NP_AT;
  266. case PPP_MPLS_UC:
  267. return NP_MPLS_UC;
  268. case PPP_MPLS_MC:
  269. return NP_MPLS_MC;
  270. }
  271. return -EINVAL;
  272. }
  273. /* Translates an NP index into a PPP protocol number */
  274. static const int npindex_to_proto[NUM_NP] = {
  275. PPP_IP,
  276. PPP_IPV6,
  277. PPP_IPX,
  278. PPP_AT,
  279. PPP_MPLS_UC,
  280. PPP_MPLS_MC,
  281. };
  282. /* Translates an ethertype into an NP index */
  283. static inline int ethertype_to_npindex(int ethertype)
  284. {
  285. switch (ethertype) {
  286. case ETH_P_IP:
  287. return NP_IP;
  288. case ETH_P_IPV6:
  289. return NP_IPV6;
  290. case ETH_P_IPX:
  291. return NP_IPX;
  292. case ETH_P_PPPTALK:
  293. case ETH_P_ATALK:
  294. return NP_AT;
  295. case ETH_P_MPLS_UC:
  296. return NP_MPLS_UC;
  297. case ETH_P_MPLS_MC:
  298. return NP_MPLS_MC;
  299. }
  300. return -1;
  301. }
  302. /* Translates an NP index into an ethertype */
  303. static const int npindex_to_ethertype[NUM_NP] = {
  304. ETH_P_IP,
  305. ETH_P_IPV6,
  306. ETH_P_IPX,
  307. ETH_P_PPPTALK,
  308. ETH_P_MPLS_UC,
  309. ETH_P_MPLS_MC,
  310. };
  311. /*
  312. * Locking shorthand.
  313. */
  314. #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
  315. #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
  316. #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
  317. #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
  318. #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
  319. ppp_recv_lock(ppp); } while (0)
  320. #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
  321. ppp_xmit_unlock(ppp); } while (0)
  322. /*
  323. * /dev/ppp device routines.
  324. * The /dev/ppp device is used by pppd to control the ppp unit.
  325. * It supports the read, write, ioctl and poll functions.
  326. * Open instances of /dev/ppp can be in one of three states:
  327. * unattached, attached to a ppp unit, or attached to a ppp channel.
  328. */
  329. static int ppp_open(struct inode *inode, struct file *file)
  330. {
  331. /*
  332. * This could (should?) be enforced by the permissions on /dev/ppp.
  333. */
  334. if (!capable(CAP_NET_ADMIN))
  335. return -EPERM;
  336. return 0;
  337. }
  338. static int ppp_release(struct inode *inode, struct file *file)
  339. {
  340. struct ppp_file *pf = file->private_data;
  341. struct ppp *ppp;
  342. if (pf != 0) {
  343. file->private_data = NULL;
  344. if (pf->kind == INTERFACE) {
  345. ppp = PF_TO_PPP(pf);
  346. if (file == ppp->owner)
  347. ppp_shutdown_interface(ppp);
  348. }
  349. if (atomic_dec_and_test(&pf->refcnt)) {
  350. switch (pf->kind) {
  351. case INTERFACE:
  352. ppp_destroy_interface(PF_TO_PPP(pf));
  353. break;
  354. case CHANNEL:
  355. ppp_destroy_channel(PF_TO_CHANNEL(pf));
  356. break;
  357. }
  358. }
  359. }
  360. return 0;
  361. }
  362. static ssize_t ppp_read(struct file *file, char __user *buf,
  363. size_t count, loff_t *ppos)
  364. {
  365. struct ppp_file *pf = file->private_data;
  366. DECLARE_WAITQUEUE(wait, current);
  367. ssize_t ret;
  368. struct sk_buff *skb = NULL;
  369. ret = count;
  370. if (pf == 0)
  371. return -ENXIO;
  372. add_wait_queue(&pf->rwait, &wait);
  373. for (;;) {
  374. set_current_state(TASK_INTERRUPTIBLE);
  375. skb = skb_dequeue(&pf->rq);
  376. if (skb)
  377. break;
  378. ret = 0;
  379. if (pf->dead)
  380. break;
  381. if (pf->kind == INTERFACE) {
  382. /*
  383. * Return 0 (EOF) on an interface that has no
  384. * channels connected, unless it is looping
  385. * network traffic (demand mode).
  386. */
  387. struct ppp *ppp = PF_TO_PPP(pf);
  388. if (ppp->n_channels == 0
  389. && (ppp->flags & SC_LOOP_TRAFFIC) == 0)
  390. break;
  391. }
  392. ret = -EAGAIN;
  393. if (file->f_flags & O_NONBLOCK)
  394. break;
  395. ret = -ERESTARTSYS;
  396. if (signal_pending(current))
  397. break;
  398. schedule();
  399. }
  400. set_current_state(TASK_RUNNING);
  401. remove_wait_queue(&pf->rwait, &wait);
  402. if (skb == 0)
  403. goto out;
  404. ret = -EOVERFLOW;
  405. if (skb->len > count)
  406. goto outf;
  407. ret = -EFAULT;
  408. if (copy_to_user(buf, skb->data, skb->len))
  409. goto outf;
  410. ret = skb->len;
  411. outf:
  412. kfree_skb(skb);
  413. out:
  414. return ret;
  415. }
  416. static ssize_t ppp_write(struct file *file, const char __user *buf,
  417. size_t count, loff_t *ppos)
  418. {
  419. struct ppp_file *pf = file->private_data;
  420. struct sk_buff *skb;
  421. ssize_t ret;
  422. if (pf == 0)
  423. return -ENXIO;
  424. ret = -ENOMEM;
  425. skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
  426. if (skb == 0)
  427. goto out;
  428. skb_reserve(skb, pf->hdrlen);
  429. ret = -EFAULT;
  430. if (copy_from_user(skb_put(skb, count), buf, count)) {
  431. kfree_skb(skb);
  432. goto out;
  433. }
  434. skb_queue_tail(&pf->xq, skb);
  435. switch (pf->kind) {
  436. case INTERFACE:
  437. ppp_xmit_process(PF_TO_PPP(pf));
  438. break;
  439. case CHANNEL:
  440. ppp_channel_push(PF_TO_CHANNEL(pf));
  441. break;
  442. }
  443. ret = count;
  444. out:
  445. return ret;
  446. }
  447. /* No kernel lock - fine */
  448. static unsigned int ppp_poll(struct file *file, poll_table *wait)
  449. {
  450. struct ppp_file *pf = file->private_data;
  451. unsigned int mask;
  452. if (pf == 0)
  453. return 0;
  454. poll_wait(file, &pf->rwait, wait);
  455. mask = POLLOUT | POLLWRNORM;
  456. if (skb_peek(&pf->rq) != 0)
  457. mask |= POLLIN | POLLRDNORM;
  458. if (pf->dead)
  459. mask |= POLLHUP;
  460. else if (pf->kind == INTERFACE) {
  461. /* see comment in ppp_read */
  462. struct ppp *ppp = PF_TO_PPP(pf);
  463. if (ppp->n_channels == 0
  464. && (ppp->flags & SC_LOOP_TRAFFIC) == 0)
  465. mask |= POLLIN | POLLRDNORM;
  466. }
  467. return mask;
  468. }
  469. #ifdef CONFIG_PPP_FILTER
  470. static int get_filter(void __user *arg, struct sock_filter **p)
  471. {
  472. struct sock_fprog uprog;
  473. struct sock_filter *code = NULL;
  474. int len, err;
  475. if (copy_from_user(&uprog, arg, sizeof(uprog)))
  476. return -EFAULT;
  477. if (!uprog.len) {
  478. *p = NULL;
  479. return 0;
  480. }
  481. len = uprog.len * sizeof(struct sock_filter);
  482. code = kmalloc(len, GFP_KERNEL);
  483. if (code == NULL)
  484. return -ENOMEM;
  485. if (copy_from_user(code, uprog.filter, len)) {
  486. kfree(code);
  487. return -EFAULT;
  488. }
  489. err = sk_chk_filter(code, uprog.len);
  490. if (err) {
  491. kfree(code);
  492. return err;
  493. }
  494. *p = code;
  495. return uprog.len;
  496. }
  497. #endif /* CONFIG_PPP_FILTER */
  498. static int ppp_ioctl(struct inode *inode, struct file *file,
  499. unsigned int cmd, unsigned long arg)
  500. {
  501. struct ppp_file *pf = file->private_data;
  502. struct ppp *ppp;
  503. int err = -EFAULT, val, val2, i;
  504. struct ppp_idle idle;
  505. struct npioctl npi;
  506. int unit, cflags;
  507. struct slcompress *vj;
  508. void __user *argp = (void __user *)arg;
  509. int __user *p = argp;
  510. if (pf == 0)
  511. return ppp_unattached_ioctl(pf, file, cmd, arg);
  512. if (cmd == PPPIOCDETACH) {
  513. /*
  514. * We have to be careful here... if the file descriptor
  515. * has been dup'd, we could have another process in the
  516. * middle of a poll using the same file *, so we had
  517. * better not free the interface data structures -
  518. * instead we fail the ioctl. Even in this case, we
  519. * shut down the interface if we are the owner of it.
  520. * Actually, we should get rid of PPPIOCDETACH, userland
  521. * (i.e. pppd) could achieve the same effect by closing
  522. * this fd and reopening /dev/ppp.
  523. */
  524. err = -EINVAL;
  525. if (pf->kind == INTERFACE) {
  526. ppp = PF_TO_PPP(pf);
  527. if (file == ppp->owner)
  528. ppp_shutdown_interface(ppp);
  529. }
  530. if (atomic_read(&file->f_count) <= 2) {
  531. ppp_release(inode, file);
  532. err = 0;
  533. } else
  534. printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%d\n",
  535. atomic_read(&file->f_count));
  536. return err;
  537. }
  538. if (pf->kind == CHANNEL) {
  539. struct channel *pch = PF_TO_CHANNEL(pf);
  540. struct ppp_channel *chan;
  541. switch (cmd) {
  542. case PPPIOCCONNECT:
  543. if (get_user(unit, p))
  544. break;
  545. err = ppp_connect_channel(pch, unit);
  546. break;
  547. case PPPIOCDISCONN:
  548. err = ppp_disconnect_channel(pch);
  549. break;
  550. default:
  551. down_read(&pch->chan_sem);
  552. chan = pch->chan;
  553. err = -ENOTTY;
  554. if (chan && chan->ops->ioctl)
  555. err = chan->ops->ioctl(chan, cmd, arg);
  556. up_read(&pch->chan_sem);
  557. }
  558. return err;
  559. }
  560. if (pf->kind != INTERFACE) {
  561. /* can't happen */
  562. printk(KERN_ERR "PPP: not interface or channel??\n");
  563. return -EINVAL;
  564. }
  565. ppp = PF_TO_PPP(pf);
  566. switch (cmd) {
  567. case PPPIOCSMRU:
  568. if (get_user(val, p))
  569. break;
  570. ppp->mru = val;
  571. err = 0;
  572. break;
  573. case PPPIOCSFLAGS:
  574. if (get_user(val, p))
  575. break;
  576. ppp_lock(ppp);
  577. cflags = ppp->flags & ~val;
  578. ppp->flags = val & SC_FLAG_BITS;
  579. ppp_unlock(ppp);
  580. if (cflags & SC_CCP_OPEN)
  581. ppp_ccp_closed(ppp);
  582. err = 0;
  583. break;
  584. case PPPIOCGFLAGS:
  585. val = ppp->flags | ppp->xstate | ppp->rstate;
  586. if (put_user(val, p))
  587. break;
  588. err = 0;
  589. break;
  590. case PPPIOCSCOMPRESS:
  591. err = ppp_set_compress(ppp, arg);
  592. break;
  593. case PPPIOCGUNIT:
  594. if (put_user(ppp->file.index, p))
  595. break;
  596. err = 0;
  597. break;
  598. case PPPIOCSDEBUG:
  599. if (get_user(val, p))
  600. break;
  601. ppp->debug = val;
  602. err = 0;
  603. break;
  604. case PPPIOCGDEBUG:
  605. if (put_user(ppp->debug, p))
  606. break;
  607. err = 0;
  608. break;
  609. case PPPIOCGIDLE:
  610. idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
  611. idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
  612. if (copy_to_user(argp, &idle, sizeof(idle)))
  613. break;
  614. err = 0;
  615. break;
  616. case PPPIOCSMAXCID:
  617. if (get_user(val, p))
  618. break;
  619. val2 = 15;
  620. if ((val >> 16) != 0) {
  621. val2 = val >> 16;
  622. val &= 0xffff;
  623. }
  624. vj = slhc_init(val2+1, val+1);
  625. if (vj == 0) {
  626. printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
  627. err = -ENOMEM;
  628. break;
  629. }
  630. ppp_lock(ppp);
  631. if (ppp->vj != 0)
  632. slhc_free(ppp->vj);
  633. ppp->vj = vj;
  634. ppp_unlock(ppp);
  635. err = 0;
  636. break;
  637. case PPPIOCGNPMODE:
  638. case PPPIOCSNPMODE:
  639. if (copy_from_user(&npi, argp, sizeof(npi)))
  640. break;
  641. err = proto_to_npindex(npi.protocol);
  642. if (err < 0)
  643. break;
  644. i = err;
  645. if (cmd == PPPIOCGNPMODE) {
  646. err = -EFAULT;
  647. npi.mode = ppp->npmode[i];
  648. if (copy_to_user(argp, &npi, sizeof(npi)))
  649. break;
  650. } else {
  651. ppp->npmode[i] = npi.mode;
  652. /* we may be able to transmit more packets now (??) */
  653. netif_wake_queue(ppp->dev);
  654. }
  655. err = 0;
  656. break;
  657. #ifdef CONFIG_PPP_FILTER
  658. case PPPIOCSPASS:
  659. {
  660. struct sock_filter *code;
  661. err = get_filter(argp, &code);
  662. if (err >= 0) {
  663. ppp_lock(ppp);
  664. kfree(ppp->pass_filter);
  665. ppp->pass_filter = code;
  666. ppp->pass_len = err;
  667. ppp_unlock(ppp);
  668. err = 0;
  669. }
  670. break;
  671. }
  672. case PPPIOCSACTIVE:
  673. {
  674. struct sock_filter *code;
  675. err = get_filter(argp, &code);
  676. if (err >= 0) {
  677. ppp_lock(ppp);
  678. kfree(ppp->active_filter);
  679. ppp->active_filter = code;
  680. ppp->active_len = err;
  681. ppp_unlock(ppp);
  682. err = 0;
  683. }
  684. break;
  685. }
  686. #endif /* CONFIG_PPP_FILTER */
  687. #ifdef CONFIG_PPP_MULTILINK
  688. case PPPIOCSMRRU:
  689. if (get_user(val, p))
  690. break;
  691. ppp_recv_lock(ppp);
  692. ppp->mrru = val;
  693. ppp_recv_unlock(ppp);
  694. err = 0;
  695. break;
  696. #endif /* CONFIG_PPP_MULTILINK */
  697. default:
  698. err = -ENOTTY;
  699. }
  700. return err;
  701. }
  702. static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
  703. unsigned int cmd, unsigned long arg)
  704. {
  705. int unit, err = -EFAULT;
  706. struct ppp *ppp;
  707. struct channel *chan;
  708. int __user *p = (int __user *)arg;
  709. switch (cmd) {
  710. case PPPIOCNEWUNIT:
  711. /* Create a new ppp unit */
  712. if (get_user(unit, p))
  713. break;
  714. ppp = ppp_create_interface(unit, &err);
  715. if (ppp == 0)
  716. break;
  717. file->private_data = &ppp->file;
  718. ppp->owner = file;
  719. err = -EFAULT;
  720. if (put_user(ppp->file.index, p))
  721. break;
  722. err = 0;
  723. break;
  724. case PPPIOCATTACH:
  725. /* Attach to an existing ppp unit */
  726. if (get_user(unit, p))
  727. break;
  728. mutex_lock(&all_ppp_mutex);
  729. err = -ENXIO;
  730. ppp = ppp_find_unit(unit);
  731. if (ppp != 0) {
  732. atomic_inc(&ppp->file.refcnt);
  733. file->private_data = &ppp->file;
  734. err = 0;
  735. }
  736. mutex_unlock(&all_ppp_mutex);
  737. break;
  738. case PPPIOCATTCHAN:
  739. if (get_user(unit, p))
  740. break;
  741. spin_lock_bh(&all_channels_lock);
  742. err = -ENXIO;
  743. chan = ppp_find_channel(unit);
  744. if (chan != 0) {
  745. atomic_inc(&chan->file.refcnt);
  746. file->private_data = &chan->file;
  747. err = 0;
  748. }
  749. spin_unlock_bh(&all_channels_lock);
  750. break;
  751. default:
  752. err = -ENOTTY;
  753. }
  754. return err;
  755. }
  756. static const struct file_operations ppp_device_fops = {
  757. .owner = THIS_MODULE,
  758. .read = ppp_read,
  759. .write = ppp_write,
  760. .poll = ppp_poll,
  761. .ioctl = ppp_ioctl,
  762. .open = ppp_open,
  763. .release = ppp_release
  764. };
  765. #define PPP_MAJOR 108
  766. /* Called at boot time if ppp is compiled into the kernel,
  767. or at module load time (from init_module) if compiled as a module. */
  768. static int __init ppp_init(void)
  769. {
  770. int err;
  771. printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
  772. err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
  773. if (!err) {
  774. ppp_class = class_create(THIS_MODULE, "ppp");
  775. if (IS_ERR(ppp_class)) {
  776. err = PTR_ERR(ppp_class);
  777. goto out_chrdev;
  778. }
  779. device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), "ppp");
  780. }
  781. out:
  782. if (err)
  783. printk(KERN_ERR "failed to register PPP device (%d)\n", err);
  784. return err;
  785. out_chrdev:
  786. unregister_chrdev(PPP_MAJOR, "ppp");
  787. goto out;
  788. }
  789. /*
  790. * Network interface unit routines.
  791. */
  792. static int
  793. ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
  794. {
  795. struct ppp *ppp = (struct ppp *) dev->priv;
  796. int npi, proto;
  797. unsigned char *pp;
  798. npi = ethertype_to_npindex(ntohs(skb->protocol));
  799. if (npi < 0)
  800. goto outf;
  801. /* Drop, accept or reject the packet */
  802. switch (ppp->npmode[npi]) {
  803. case NPMODE_PASS:
  804. break;
  805. case NPMODE_QUEUE:
  806. /* it would be nice to have a way to tell the network
  807. system to queue this one up for later. */
  808. goto outf;
  809. case NPMODE_DROP:
  810. case NPMODE_ERROR:
  811. goto outf;
  812. }
  813. /* Put the 2-byte PPP protocol number on the front,
  814. making sure there is room for the address and control fields. */
  815. if (skb_headroom(skb) < PPP_HDRLEN) {
  816. struct sk_buff *ns;
  817. ns = alloc_skb(skb->len + dev->hard_header_len, GFP_ATOMIC);
  818. if (ns == 0)
  819. goto outf;
  820. skb_reserve(ns, dev->hard_header_len);
  821. skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
  822. kfree_skb(skb);
  823. skb = ns;
  824. }
  825. pp = skb_push(skb, 2);
  826. proto = npindex_to_proto[npi];
  827. pp[0] = proto >> 8;
  828. pp[1] = proto;
  829. netif_stop_queue(dev);
  830. skb_queue_tail(&ppp->file.xq, skb);
  831. ppp_xmit_process(ppp);
  832. return 0;
  833. outf:
  834. kfree_skb(skb);
  835. ++ppp->stats.tx_dropped;
  836. return 0;
  837. }
  838. static struct net_device_stats *
  839. ppp_net_stats(struct net_device *dev)
  840. {
  841. struct ppp *ppp = (struct ppp *) dev->priv;
  842. return &ppp->stats;
  843. }
  844. static int
  845. ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  846. {
  847. struct ppp *ppp = dev->priv;
  848. int err = -EFAULT;
  849. void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
  850. struct ppp_stats stats;
  851. struct ppp_comp_stats cstats;
  852. char *vers;
  853. switch (cmd) {
  854. case SIOCGPPPSTATS:
  855. ppp_get_stats(ppp, &stats);
  856. if (copy_to_user(addr, &stats, sizeof(stats)))
  857. break;
  858. err = 0;
  859. break;
  860. case SIOCGPPPCSTATS:
  861. memset(&cstats, 0, sizeof(cstats));
  862. if (ppp->xc_state != 0)
  863. ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
  864. if (ppp->rc_state != 0)
  865. ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
  866. if (copy_to_user(addr, &cstats, sizeof(cstats)))
  867. break;
  868. err = 0;
  869. break;
  870. case SIOCGPPPVER:
  871. vers = PPP_VERSION;
  872. if (copy_to_user(addr, vers, strlen(vers) + 1))
  873. break;
  874. err = 0;
  875. break;
  876. default:
  877. err = -EINVAL;
  878. }
  879. return err;
  880. }
  881. static void ppp_setup(struct net_device *dev)
  882. {
  883. dev->hard_header_len = PPP_HDRLEN;
  884. dev->mtu = PPP_MTU;
  885. dev->addr_len = 0;
  886. dev->tx_queue_len = 3;
  887. dev->type = ARPHRD_PPP;
  888. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  889. }
  890. /*
  891. * Transmit-side routines.
  892. */
  893. /*
  894. * Called to do any work queued up on the transmit side
  895. * that can now be done.
  896. */
  897. static void
  898. ppp_xmit_process(struct ppp *ppp)
  899. {
  900. struct sk_buff *skb;
  901. ppp_xmit_lock(ppp);
  902. if (ppp->dev != 0) {
  903. ppp_push(ppp);
  904. while (ppp->xmit_pending == 0
  905. && (skb = skb_dequeue(&ppp->file.xq)) != 0)
  906. ppp_send_frame(ppp, skb);
  907. /* If there's no work left to do, tell the core net
  908. code that we can accept some more. */
  909. if (ppp->xmit_pending == 0 && skb_peek(&ppp->file.xq) == 0)
  910. netif_wake_queue(ppp->dev);
  911. }
  912. ppp_xmit_unlock(ppp);
  913. }
  914. static inline struct sk_buff *
  915. pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
  916. {
  917. struct sk_buff *new_skb;
  918. int len;
  919. int new_skb_size = ppp->dev->mtu +
  920. ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
  921. int compressor_skb_size = ppp->dev->mtu +
  922. ppp->xcomp->comp_extra + PPP_HDRLEN;
  923. new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
  924. if (!new_skb) {
  925. if (net_ratelimit())
  926. printk(KERN_ERR "PPP: no memory (comp pkt)\n");
  927. return NULL;
  928. }
  929. if (ppp->dev->hard_header_len > PPP_HDRLEN)
  930. skb_reserve(new_skb,
  931. ppp->dev->hard_header_len - PPP_HDRLEN);
  932. /* compressor still expects A/C bytes in hdr */
  933. len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
  934. new_skb->data, skb->len + 2,
  935. compressor_skb_size);
  936. if (len > 0 && (ppp->flags & SC_CCP_UP)) {
  937. kfree_skb(skb);
  938. skb = new_skb;
  939. skb_put(skb, len);
  940. skb_pull(skb, 2); /* pull off A/C bytes */
  941. } else if (len == 0) {
  942. /* didn't compress, or CCP not up yet */
  943. kfree_skb(new_skb);
  944. new_skb = skb;
  945. } else {
  946. /*
  947. * (len < 0)
  948. * MPPE requires that we do not send unencrypted
  949. * frames. The compressor will return -1 if we
  950. * should drop the frame. We cannot simply test
  951. * the compress_proto because MPPE and MPPC share
  952. * the same number.
  953. */
  954. if (net_ratelimit())
  955. printk(KERN_ERR "ppp: compressor dropped pkt\n");
  956. kfree_skb(skb);
  957. kfree_skb(new_skb);
  958. new_skb = NULL;
  959. }
  960. return new_skb;
  961. }
  962. /*
  963. * Compress and send a frame.
  964. * The caller should have locked the xmit path,
  965. * and xmit_pending should be 0.
  966. */
  967. static void
  968. ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
  969. {
  970. int proto = PPP_PROTO(skb);
  971. struct sk_buff *new_skb;
  972. int len;
  973. unsigned char *cp;
  974. if (proto < 0x8000) {
  975. #ifdef CONFIG_PPP_FILTER
  976. /* check if we should pass this packet */
  977. /* the filter instructions are constructed assuming
  978. a four-byte PPP header on each packet */
  979. *skb_push(skb, 2) = 1;
  980. if (ppp->pass_filter
  981. && sk_run_filter(skb, ppp->pass_filter,
  982. ppp->pass_len) == 0) {
  983. if (ppp->debug & 1)
  984. printk(KERN_DEBUG "PPP: outbound frame not passed\n");
  985. kfree_skb(skb);
  986. return;
  987. }
  988. /* if this packet passes the active filter, record the time */
  989. if (!(ppp->active_filter
  990. && sk_run_filter(skb, ppp->active_filter,
  991. ppp->active_len) == 0))
  992. ppp->last_xmit = jiffies;
  993. skb_pull(skb, 2);
  994. #else
  995. /* for data packets, record the time */
  996. ppp->last_xmit = jiffies;
  997. #endif /* CONFIG_PPP_FILTER */
  998. }
  999. ++ppp->stats.tx_packets;
  1000. ppp->stats.tx_bytes += skb->len - 2;
  1001. switch (proto) {
  1002. case PPP_IP:
  1003. if (ppp->vj == 0 || (ppp->flags & SC_COMP_TCP) == 0)
  1004. break;
  1005. /* try to do VJ TCP header compression */
  1006. new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
  1007. GFP_ATOMIC);
  1008. if (new_skb == 0) {
  1009. printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
  1010. goto drop;
  1011. }
  1012. skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
  1013. cp = skb->data + 2;
  1014. len = slhc_compress(ppp->vj, cp, skb->len - 2,
  1015. new_skb->data + 2, &cp,
  1016. !(ppp->flags & SC_NO_TCP_CCID));
  1017. if (cp == skb->data + 2) {
  1018. /* didn't compress */
  1019. kfree_skb(new_skb);
  1020. } else {
  1021. if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
  1022. proto = PPP_VJC_COMP;
  1023. cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
  1024. } else {
  1025. proto = PPP_VJC_UNCOMP;
  1026. cp[0] = skb->data[2];
  1027. }
  1028. kfree_skb(skb);
  1029. skb = new_skb;
  1030. cp = skb_put(skb, len + 2);
  1031. cp[0] = 0;
  1032. cp[1] = proto;
  1033. }
  1034. break;
  1035. case PPP_CCP:
  1036. /* peek at outbound CCP frames */
  1037. ppp_ccp_peek(ppp, skb, 0);
  1038. break;
  1039. }
  1040. /* try to do packet compression */
  1041. if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
  1042. && proto != PPP_LCP && proto != PPP_CCP) {
  1043. if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
  1044. if (net_ratelimit())
  1045. printk(KERN_ERR "ppp: compression required but down - pkt dropped.\n");
  1046. goto drop;
  1047. }
  1048. skb = pad_compress_skb(ppp, skb);
  1049. if (!skb)
  1050. goto drop;
  1051. }
  1052. /*
  1053. * If we are waiting for traffic (demand dialling),
  1054. * queue it up for pppd to receive.
  1055. */
  1056. if (ppp->flags & SC_LOOP_TRAFFIC) {
  1057. if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
  1058. goto drop;
  1059. skb_queue_tail(&ppp->file.rq, skb);
  1060. wake_up_interruptible(&ppp->file.rwait);
  1061. return;
  1062. }
  1063. ppp->xmit_pending = skb;
  1064. ppp_push(ppp);
  1065. return;
  1066. drop:
  1067. if (skb)
  1068. kfree_skb(skb);
  1069. ++ppp->stats.tx_errors;
  1070. }
  1071. /*
  1072. * Try to send the frame in xmit_pending.
  1073. * The caller should have the xmit path locked.
  1074. */
  1075. static void
  1076. ppp_push(struct ppp *ppp)
  1077. {
  1078. struct list_head *list;
  1079. struct channel *pch;
  1080. struct sk_buff *skb = ppp->xmit_pending;
  1081. if (skb == 0)
  1082. return;
  1083. list = &ppp->channels;
  1084. if (list_empty(list)) {
  1085. /* nowhere to send the packet, just drop it */
  1086. ppp->xmit_pending = NULL;
  1087. kfree_skb(skb);
  1088. return;
  1089. }
  1090. if ((ppp->flags & SC_MULTILINK) == 0) {
  1091. /* not doing multilink: send it down the first channel */
  1092. list = list->next;
  1093. pch = list_entry(list, struct channel, clist);
  1094. spin_lock_bh(&pch->downl);
  1095. if (pch->chan) {
  1096. if (pch->chan->ops->start_xmit(pch->chan, skb))
  1097. ppp->xmit_pending = NULL;
  1098. } else {
  1099. /* channel got unregistered */
  1100. kfree_skb(skb);
  1101. ppp->xmit_pending = NULL;
  1102. }
  1103. spin_unlock_bh(&pch->downl);
  1104. return;
  1105. }
  1106. #ifdef CONFIG_PPP_MULTILINK
  1107. /* Multilink: fragment the packet over as many links
  1108. as can take the packet at the moment. */
  1109. if (!ppp_mp_explode(ppp, skb))
  1110. return;
  1111. #endif /* CONFIG_PPP_MULTILINK */
  1112. ppp->xmit_pending = NULL;
  1113. kfree_skb(skb);
  1114. }
  1115. #ifdef CONFIG_PPP_MULTILINK
  1116. /*
  1117. * Divide a packet to be transmitted into fragments and
  1118. * send them out the individual links.
  1119. */
  1120. static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
  1121. {
  1122. int len, fragsize;
  1123. int i, bits, hdrlen, mtu;
  1124. int flen;
  1125. int navail, nfree;
  1126. int nbigger;
  1127. unsigned char *p, *q;
  1128. struct list_head *list;
  1129. struct channel *pch;
  1130. struct sk_buff *frag;
  1131. struct ppp_channel *chan;
  1132. nfree = 0; /* # channels which have no packet already queued */
  1133. navail = 0; /* total # of usable channels (not deregistered) */
  1134. hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  1135. i = 0;
  1136. list_for_each_entry(pch, &ppp->channels, clist) {
  1137. navail += pch->avail = (pch->chan != NULL);
  1138. if (pch->avail) {
  1139. if (skb_queue_empty(&pch->file.xq) ||
  1140. !pch->had_frag) {
  1141. pch->avail = 2;
  1142. ++nfree;
  1143. }
  1144. if (!pch->had_frag && i < ppp->nxchan)
  1145. ppp->nxchan = i;
  1146. }
  1147. ++i;
  1148. }
  1149. /*
  1150. * Don't start sending this packet unless at least half of
  1151. * the channels are free. This gives much better TCP
  1152. * performance if we have a lot of channels.
  1153. */
  1154. if (nfree == 0 || nfree < navail / 2)
  1155. return 0; /* can't take now, leave it in xmit_pending */
  1156. /* Do protocol field compression (XXX this should be optional) */
  1157. p = skb->data;
  1158. len = skb->len;
  1159. if (*p == 0) {
  1160. ++p;
  1161. --len;
  1162. }
  1163. /*
  1164. * Decide on fragment size.
  1165. * We create a fragment for each free channel regardless of
  1166. * how small they are (i.e. even 0 length) in order to minimize
  1167. * the time that it will take to detect when a channel drops
  1168. * a fragment.
  1169. */
  1170. fragsize = len;
  1171. if (nfree > 1)
  1172. fragsize = ROUNDUP(fragsize, nfree);
  1173. /* nbigger channels get fragsize bytes, the rest get fragsize-1,
  1174. except if nbigger==0, then they all get fragsize. */
  1175. nbigger = len % nfree;
  1176. /* skip to the channel after the one we last used
  1177. and start at that one */
  1178. list = &ppp->channels;
  1179. for (i = 0; i < ppp->nxchan; ++i) {
  1180. list = list->next;
  1181. if (list == &ppp->channels) {
  1182. i = 0;
  1183. break;
  1184. }
  1185. }
  1186. /* create a fragment for each channel */
  1187. bits = B;
  1188. while (nfree > 0 || len > 0) {
  1189. list = list->next;
  1190. if (list == &ppp->channels) {
  1191. i = 0;
  1192. continue;
  1193. }
  1194. pch = list_entry(list, struct channel, clist);
  1195. ++i;
  1196. if (!pch->avail)
  1197. continue;
  1198. /*
  1199. * Skip this channel if it has a fragment pending already and
  1200. * we haven't given a fragment to all of the free channels.
  1201. */
  1202. if (pch->avail == 1) {
  1203. if (nfree > 0)
  1204. continue;
  1205. } else {
  1206. --nfree;
  1207. pch->avail = 1;
  1208. }
  1209. /* check the channel's mtu and whether it is still attached. */
  1210. spin_lock_bh(&pch->downl);
  1211. if (pch->chan == NULL) {
  1212. /* can't use this channel, it's being deregistered */
  1213. spin_unlock_bh(&pch->downl);
  1214. pch->avail = 0;
  1215. if (--navail == 0)
  1216. break;
  1217. continue;
  1218. }
  1219. /*
  1220. * Create a fragment for this channel of
  1221. * min(max(mtu+2-hdrlen, 4), fragsize, len) bytes.
  1222. * If mtu+2-hdrlen < 4, that is a ridiculously small
  1223. * MTU, so we use mtu = 2 + hdrlen.
  1224. */
  1225. if (fragsize > len)
  1226. fragsize = len;
  1227. flen = fragsize;
  1228. mtu = pch->chan->mtu + 2 - hdrlen;
  1229. if (mtu < 4)
  1230. mtu = 4;
  1231. if (flen > mtu)
  1232. flen = mtu;
  1233. if (flen == len && nfree == 0)
  1234. bits |= E;
  1235. frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
  1236. if (frag == 0)
  1237. goto noskb;
  1238. q = skb_put(frag, flen + hdrlen);
  1239. /* make the MP header */
  1240. q[0] = PPP_MP >> 8;
  1241. q[1] = PPP_MP;
  1242. if (ppp->flags & SC_MP_XSHORTSEQ) {
  1243. q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
  1244. q[3] = ppp->nxseq;
  1245. } else {
  1246. q[2] = bits;
  1247. q[3] = ppp->nxseq >> 16;
  1248. q[4] = ppp->nxseq >> 8;
  1249. q[5] = ppp->nxseq;
  1250. }
  1251. /*
  1252. * Copy the data in.
  1253. * Unfortunately there is a bug in older versions of
  1254. * the Linux PPP multilink reconstruction code where it
  1255. * drops 0-length fragments. Therefore we make sure the
  1256. * fragment has at least one byte of data. Any bytes
  1257. * we add in this situation will end up as padding on the
  1258. * end of the reconstructed packet.
  1259. */
  1260. if (flen == 0)
  1261. *skb_put(frag, 1) = 0;
  1262. else
  1263. memcpy(q + hdrlen, p, flen);
  1264. /* try to send it down the channel */
  1265. chan = pch->chan;
  1266. if (!skb_queue_empty(&pch->file.xq) ||
  1267. !chan->ops->start_xmit(chan, frag))
  1268. skb_queue_tail(&pch->file.xq, frag);
  1269. pch->had_frag = 1;
  1270. p += flen;
  1271. len -= flen;
  1272. ++ppp->nxseq;
  1273. bits = 0;
  1274. spin_unlock_bh(&pch->downl);
  1275. if (--nbigger == 0 && fragsize > 0)
  1276. --fragsize;
  1277. }
  1278. ppp->nxchan = i;
  1279. return 1;
  1280. noskb:
  1281. spin_unlock_bh(&pch->downl);
  1282. if (ppp->debug & 1)
  1283. printk(KERN_ERR "PPP: no memory (fragment)\n");
  1284. ++ppp->stats.tx_errors;
  1285. ++ppp->nxseq;
  1286. return 1; /* abandon the frame */
  1287. }
  1288. #endif /* CONFIG_PPP_MULTILINK */
  1289. /*
  1290. * Try to send data out on a channel.
  1291. */
  1292. static void
  1293. ppp_channel_push(struct channel *pch)
  1294. {
  1295. struct sk_buff *skb;
  1296. struct ppp *ppp;
  1297. spin_lock_bh(&pch->downl);
  1298. if (pch->chan != 0) {
  1299. while (!skb_queue_empty(&pch->file.xq)) {
  1300. skb = skb_dequeue(&pch->file.xq);
  1301. if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
  1302. /* put the packet back and try again later */
  1303. skb_queue_head(&pch->file.xq, skb);
  1304. break;
  1305. }
  1306. }
  1307. } else {
  1308. /* channel got deregistered */
  1309. skb_queue_purge(&pch->file.xq);
  1310. }
  1311. spin_unlock_bh(&pch->downl);
  1312. /* see if there is anything from the attached unit to be sent */
  1313. if (skb_queue_empty(&pch->file.xq)) {
  1314. read_lock_bh(&pch->upl);
  1315. ppp = pch->ppp;
  1316. if (ppp != 0)
  1317. ppp_xmit_process(ppp);
  1318. read_unlock_bh(&pch->upl);
  1319. }
  1320. }
  1321. /*
  1322. * Receive-side routines.
  1323. */
  1324. /* misuse a few fields of the skb for MP reconstruction */
  1325. #define sequence priority
  1326. #define BEbits cb[0]
  1327. static inline void
  1328. ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1329. {
  1330. ppp_recv_lock(ppp);
  1331. /* ppp->dev == 0 means interface is closing down */
  1332. if (ppp->dev != 0)
  1333. ppp_receive_frame(ppp, skb, pch);
  1334. else
  1335. kfree_skb(skb);
  1336. ppp_recv_unlock(ppp);
  1337. }
  1338. void
  1339. ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
  1340. {
  1341. struct channel *pch = chan->ppp;
  1342. int proto;
  1343. if (pch == 0 || skb->len == 0) {
  1344. kfree_skb(skb);
  1345. return;
  1346. }
  1347. proto = PPP_PROTO(skb);
  1348. read_lock_bh(&pch->upl);
  1349. if (pch->ppp == 0 || proto >= 0xc000 || proto == PPP_CCPFRAG) {
  1350. /* put it on the channel queue */
  1351. skb_queue_tail(&pch->file.rq, skb);
  1352. /* drop old frames if queue too long */
  1353. while (pch->file.rq.qlen > PPP_MAX_RQLEN
  1354. && (skb = skb_dequeue(&pch->file.rq)) != 0)
  1355. kfree_skb(skb);
  1356. wake_up_interruptible(&pch->file.rwait);
  1357. } else {
  1358. ppp_do_recv(pch->ppp, skb, pch);
  1359. }
  1360. read_unlock_bh(&pch->upl);
  1361. }
  1362. /* Put a 0-length skb in the receive queue as an error indication */
  1363. void
  1364. ppp_input_error(struct ppp_channel *chan, int code)
  1365. {
  1366. struct channel *pch = chan->ppp;
  1367. struct sk_buff *skb;
  1368. if (pch == 0)
  1369. return;
  1370. read_lock_bh(&pch->upl);
  1371. if (pch->ppp != 0) {
  1372. skb = alloc_skb(0, GFP_ATOMIC);
  1373. if (skb != 0) {
  1374. skb->len = 0; /* probably unnecessary */
  1375. skb->cb[0] = code;
  1376. ppp_do_recv(pch->ppp, skb, pch);
  1377. }
  1378. }
  1379. read_unlock_bh(&pch->upl);
  1380. }
  1381. /*
  1382. * We come in here to process a received frame.
  1383. * The receive side of the ppp unit is locked.
  1384. */
  1385. static void
  1386. ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1387. {
  1388. if (skb->len >= 2) {
  1389. #ifdef CONFIG_PPP_MULTILINK
  1390. /* XXX do channel-level decompression here */
  1391. if (PPP_PROTO(skb) == PPP_MP)
  1392. ppp_receive_mp_frame(ppp, skb, pch);
  1393. else
  1394. #endif /* CONFIG_PPP_MULTILINK */
  1395. ppp_receive_nonmp_frame(ppp, skb);
  1396. return;
  1397. }
  1398. if (skb->len > 0)
  1399. /* note: a 0-length skb is used as an error indication */
  1400. ++ppp->stats.rx_length_errors;
  1401. kfree_skb(skb);
  1402. ppp_receive_error(ppp);
  1403. }
  1404. static void
  1405. ppp_receive_error(struct ppp *ppp)
  1406. {
  1407. ++ppp->stats.rx_errors;
  1408. if (ppp->vj != 0)
  1409. slhc_toss(ppp->vj);
  1410. }
  1411. static void
  1412. ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
  1413. {
  1414. struct sk_buff *ns;
  1415. int proto, len, npi;
  1416. /*
  1417. * Decompress the frame, if compressed.
  1418. * Note that some decompressors need to see uncompressed frames
  1419. * that come in as well as compressed frames.
  1420. */
  1421. if (ppp->rc_state != 0 && (ppp->rstate & SC_DECOMP_RUN)
  1422. && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
  1423. skb = ppp_decompress_frame(ppp, skb);
  1424. if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
  1425. goto err;
  1426. proto = PPP_PROTO(skb);
  1427. switch (proto) {
  1428. case PPP_VJC_COMP:
  1429. /* decompress VJ compressed packets */
  1430. if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
  1431. goto err;
  1432. if (skb_tailroom(skb) < 124) {
  1433. /* copy to a new sk_buff with more tailroom */
  1434. ns = dev_alloc_skb(skb->len + 128);
  1435. if (ns == 0) {
  1436. printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
  1437. goto err;
  1438. }
  1439. skb_reserve(ns, 2);
  1440. skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
  1441. kfree_skb(skb);
  1442. skb = ns;
  1443. }
  1444. else
  1445. skb->ip_summed = CHECKSUM_NONE;
  1446. len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
  1447. if (len <= 0) {
  1448. printk(KERN_DEBUG "PPP: VJ decompression error\n");
  1449. goto err;
  1450. }
  1451. len += 2;
  1452. if (len > skb->len)
  1453. skb_put(skb, len - skb->len);
  1454. else if (len < skb->len)
  1455. skb_trim(skb, len);
  1456. proto = PPP_IP;
  1457. break;
  1458. case PPP_VJC_UNCOMP:
  1459. if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
  1460. goto err;
  1461. /* Until we fix the decompressor need to make sure
  1462. * data portion is linear.
  1463. */
  1464. if (!pskb_may_pull(skb, skb->len))
  1465. goto err;
  1466. if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
  1467. printk(KERN_ERR "PPP: VJ uncompressed error\n");
  1468. goto err;
  1469. }
  1470. proto = PPP_IP;
  1471. break;
  1472. case PPP_CCP:
  1473. ppp_ccp_peek(ppp, skb, 1);
  1474. break;
  1475. }
  1476. ++ppp->stats.rx_packets;
  1477. ppp->stats.rx_bytes += skb->len - 2;
  1478. npi = proto_to_npindex(proto);
  1479. if (npi < 0) {
  1480. /* control or unknown frame - pass it to pppd */
  1481. skb_queue_tail(&ppp->file.rq, skb);
  1482. /* limit queue length by dropping old frames */
  1483. while (ppp->file.rq.qlen > PPP_MAX_RQLEN
  1484. && (skb = skb_dequeue(&ppp->file.rq)) != 0)
  1485. kfree_skb(skb);
  1486. /* wake up any process polling or blocking on read */
  1487. wake_up_interruptible(&ppp->file.rwait);
  1488. } else {
  1489. /* network protocol frame - give it to the kernel */
  1490. #ifdef CONFIG_PPP_FILTER
  1491. /* check if the packet passes the pass and active filters */
  1492. /* the filter instructions are constructed assuming
  1493. a four-byte PPP header on each packet */
  1494. *skb_push(skb, 2) = 0;
  1495. if (ppp->pass_filter
  1496. && sk_run_filter(skb, ppp->pass_filter,
  1497. ppp->pass_len) == 0) {
  1498. if (ppp->debug & 1)
  1499. printk(KERN_DEBUG "PPP: inbound frame not passed\n");
  1500. kfree_skb(skb);
  1501. return;
  1502. }
  1503. if (!(ppp->active_filter
  1504. && sk_run_filter(skb, ppp->active_filter,
  1505. ppp->active_len) == 0))
  1506. ppp->last_recv = jiffies;
  1507. skb_pull(skb, 2);
  1508. #else
  1509. ppp->last_recv = jiffies;
  1510. #endif /* CONFIG_PPP_FILTER */
  1511. if ((ppp->dev->flags & IFF_UP) == 0
  1512. || ppp->npmode[npi] != NPMODE_PASS) {
  1513. kfree_skb(skb);
  1514. } else {
  1515. /* chop off protocol */
  1516. skb_pull_rcsum(skb, 2);
  1517. skb->dev = ppp->dev;
  1518. skb->protocol = htons(npindex_to_ethertype[npi]);
  1519. skb->mac.raw = skb->data;
  1520. netif_rx(skb);
  1521. ppp->dev->last_rx = jiffies;
  1522. }
  1523. }
  1524. return;
  1525. err:
  1526. kfree_skb(skb);
  1527. ppp_receive_error(ppp);
  1528. }
  1529. static struct sk_buff *
  1530. ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
  1531. {
  1532. int proto = PPP_PROTO(skb);
  1533. struct sk_buff *ns;
  1534. int len;
  1535. /* Until we fix all the decompressor's need to make sure
  1536. * data portion is linear.
  1537. */
  1538. if (!pskb_may_pull(skb, skb->len))
  1539. goto err;
  1540. if (proto == PPP_COMP) {
  1541. ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
  1542. if (ns == 0) {
  1543. printk(KERN_ERR "ppp_decompress_frame: no memory\n");
  1544. goto err;
  1545. }
  1546. /* the decompressor still expects the A/C bytes in the hdr */
  1547. len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
  1548. skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
  1549. if (len < 0) {
  1550. /* Pass the compressed frame to pppd as an
  1551. error indication. */
  1552. if (len == DECOMP_FATALERROR)
  1553. ppp->rstate |= SC_DC_FERROR;
  1554. kfree_skb(ns);
  1555. goto err;
  1556. }
  1557. kfree_skb(skb);
  1558. skb = ns;
  1559. skb_put(skb, len);
  1560. skb_pull(skb, 2); /* pull off the A/C bytes */
  1561. } else {
  1562. /* Uncompressed frame - pass to decompressor so it
  1563. can update its dictionary if necessary. */
  1564. if (ppp->rcomp->incomp)
  1565. ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
  1566. skb->len + 2);
  1567. }
  1568. return skb;
  1569. err:
  1570. ppp->rstate |= SC_DC_ERROR;
  1571. ppp_receive_error(ppp);
  1572. return skb;
  1573. }
  1574. #ifdef CONFIG_PPP_MULTILINK
  1575. /*
  1576. * Receive a multilink frame.
  1577. * We put it on the reconstruction queue and then pull off
  1578. * as many completed frames as we can.
  1579. */
  1580. static void
  1581. ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1582. {
  1583. u32 mask, seq;
  1584. struct channel *ch;
  1585. int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  1586. if (!pskb_may_pull(skb, mphdrlen) || ppp->mrru == 0)
  1587. goto err; /* no good, throw it away */
  1588. /* Decode sequence number and begin/end bits */
  1589. if (ppp->flags & SC_MP_SHORTSEQ) {
  1590. seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
  1591. mask = 0xfff;
  1592. } else {
  1593. seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
  1594. mask = 0xffffff;
  1595. }
  1596. skb->BEbits = skb->data[2];
  1597. skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
  1598. /*
  1599. * Do protocol ID decompression on the first fragment of each packet.
  1600. */
  1601. if ((skb->BEbits & B) && (skb->data[0] & 1))
  1602. *skb_push(skb, 1) = 0;
  1603. /*
  1604. * Expand sequence number to 32 bits, making it as close
  1605. * as possible to ppp->minseq.
  1606. */
  1607. seq |= ppp->minseq & ~mask;
  1608. if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
  1609. seq += mask + 1;
  1610. else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
  1611. seq -= mask + 1; /* should never happen */
  1612. skb->sequence = seq;
  1613. pch->lastseq = seq;
  1614. /*
  1615. * If this packet comes before the next one we were expecting,
  1616. * drop it.
  1617. */
  1618. if (seq_before(seq, ppp->nextseq)) {
  1619. kfree_skb(skb);
  1620. ++ppp->stats.rx_dropped;
  1621. ppp_receive_error(ppp);
  1622. return;
  1623. }
  1624. /*
  1625. * Reevaluate minseq, the minimum over all channels of the
  1626. * last sequence number received on each channel. Because of
  1627. * the increasing sequence number rule, we know that any fragment
  1628. * before `minseq' which hasn't arrived is never going to arrive.
  1629. * The list of channels can't change because we have the receive
  1630. * side of the ppp unit locked.
  1631. */
  1632. list_for_each_entry(ch, &ppp->channels, clist) {
  1633. if (seq_before(ch->lastseq, seq))
  1634. seq = ch->lastseq;
  1635. }
  1636. if (seq_before(ppp->minseq, seq))
  1637. ppp->minseq = seq;
  1638. /* Put the fragment on the reconstruction queue */
  1639. ppp_mp_insert(ppp, skb);
  1640. /* If the queue is getting long, don't wait any longer for packets
  1641. before the start of the queue. */
  1642. if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
  1643. && seq_before(ppp->minseq, ppp->mrq.next->sequence))
  1644. ppp->minseq = ppp->mrq.next->sequence;
  1645. /* Pull completed packets off the queue and receive them. */
  1646. while ((skb = ppp_mp_reconstruct(ppp)) != 0)
  1647. ppp_receive_nonmp_frame(ppp, skb);
  1648. return;
  1649. err:
  1650. kfree_skb(skb);
  1651. ppp_receive_error(ppp);
  1652. }
  1653. /*
  1654. * Insert a fragment on the MP reconstruction queue.
  1655. * The queue is ordered by increasing sequence number.
  1656. */
  1657. static void
  1658. ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
  1659. {
  1660. struct sk_buff *p;
  1661. struct sk_buff_head *list = &ppp->mrq;
  1662. u32 seq = skb->sequence;
  1663. /* N.B. we don't need to lock the list lock because we have the
  1664. ppp unit receive-side lock. */
  1665. for (p = list->next; p != (struct sk_buff *)list; p = p->next)
  1666. if (seq_before(seq, p->sequence))
  1667. break;
  1668. __skb_insert(skb, p->prev, p, list);
  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. 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->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->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->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 == 0)
  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 != 0)
  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 != 0) {
  1831. read_lock_bh(&pch->upl);
  1832. if (pch->ppp != 0)
  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 == 0)
  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 == 0)
  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 = find_compressor(ccp_option[0]);
  1901. #ifdef CONFIG_KMOD
  1902. if (cp == 0) {
  1903. request_module("ppp-compress-%d", ccp_option[0]);
  1904. cp = find_compressor(ccp_option[0]);
  1905. }
  1906. #endif /* CONFIG_KMOD */
  1907. if (cp == 0)
  1908. goto out;
  1909. err = -ENOBUFS;
  1910. if (data.transmit) {
  1911. state = cp->comp_alloc(ccp_option, data.length);
  1912. if (state != 0) {
  1913. ppp_xmit_lock(ppp);
  1914. ppp->xstate &= ~SC_COMP_RUN;
  1915. ocomp = ppp->xcomp;
  1916. ostate = ppp->xc_state;
  1917. ppp->xcomp = cp;
  1918. ppp->xc_state = state;
  1919. ppp_xmit_unlock(ppp);
  1920. if (ostate != 0) {
  1921. ocomp->comp_free(ostate);
  1922. module_put(ocomp->owner);
  1923. }
  1924. err = 0;
  1925. } else
  1926. module_put(cp->owner);
  1927. } else {
  1928. state = cp->decomp_alloc(ccp_option, data.length);
  1929. if (state != 0) {
  1930. ppp_recv_lock(ppp);
  1931. ppp->rstate &= ~SC_DECOMP_RUN;
  1932. ocomp = ppp->rcomp;
  1933. ostate = ppp->rc_state;
  1934. ppp->rcomp = cp;
  1935. ppp->rc_state = state;
  1936. ppp_recv_unlock(ppp);
  1937. if (ostate != 0) {
  1938. ocomp->decomp_free(ostate);
  1939. module_put(ocomp->owner);
  1940. }
  1941. err = 0;
  1942. } else
  1943. module_put(cp->owner);
  1944. }
  1945. out:
  1946. return err;
  1947. }
  1948. /*
  1949. * Look at a CCP packet and update our state accordingly.
  1950. * We assume the caller has the xmit or recv path locked.
  1951. */
  1952. static void
  1953. ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
  1954. {
  1955. unsigned char *dp;
  1956. int len;
  1957. if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
  1958. return; /* no header */
  1959. dp = skb->data + 2;
  1960. switch (CCP_CODE(dp)) {
  1961. case CCP_CONFREQ:
  1962. /* A ConfReq starts negotiation of compression
  1963. * in one direction of transmission,
  1964. * and hence brings it down...but which way?
  1965. *
  1966. * Remember:
  1967. * A ConfReq indicates what the sender would like to receive
  1968. */
  1969. if(inbound)
  1970. /* He is proposing what I should send */
  1971. ppp->xstate &= ~SC_COMP_RUN;
  1972. else
  1973. /* I am proposing to what he should send */
  1974. ppp->rstate &= ~SC_DECOMP_RUN;
  1975. break;
  1976. case CCP_TERMREQ:
  1977. case CCP_TERMACK:
  1978. /*
  1979. * CCP is going down, both directions of transmission
  1980. */
  1981. ppp->rstate &= ~SC_DECOMP_RUN;
  1982. ppp->xstate &= ~SC_COMP_RUN;
  1983. break;
  1984. case CCP_CONFACK:
  1985. if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
  1986. break;
  1987. len = CCP_LENGTH(dp);
  1988. if (!pskb_may_pull(skb, len + 2))
  1989. return; /* too short */
  1990. dp += CCP_HDRLEN;
  1991. len -= CCP_HDRLEN;
  1992. if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
  1993. break;
  1994. if (inbound) {
  1995. /* we will start receiving compressed packets */
  1996. if (ppp->rc_state == 0)
  1997. break;
  1998. if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
  1999. ppp->file.index, 0, ppp->mru, ppp->debug)) {
  2000. ppp->rstate |= SC_DECOMP_RUN;
  2001. ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
  2002. }
  2003. } else {
  2004. /* we will soon start sending compressed packets */
  2005. if (ppp->xc_state == 0)
  2006. break;
  2007. if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
  2008. ppp->file.index, 0, ppp->debug))
  2009. ppp->xstate |= SC_COMP_RUN;
  2010. }
  2011. break;
  2012. case CCP_RESETACK:
  2013. /* reset the [de]compressor */
  2014. if ((ppp->flags & SC_CCP_UP) == 0)
  2015. break;
  2016. if (inbound) {
  2017. if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
  2018. ppp->rcomp->decomp_reset(ppp->rc_state);
  2019. ppp->rstate &= ~SC_DC_ERROR;
  2020. }
  2021. } else {
  2022. if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
  2023. ppp->xcomp->comp_reset(ppp->xc_state);
  2024. }
  2025. break;
  2026. }
  2027. }
  2028. /* Free up compression resources. */
  2029. static void
  2030. ppp_ccp_closed(struct ppp *ppp)
  2031. {
  2032. void *xstate, *rstate;
  2033. struct compressor *xcomp, *rcomp;
  2034. ppp_lock(ppp);
  2035. ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
  2036. ppp->xstate = 0;
  2037. xcomp = ppp->xcomp;
  2038. xstate = ppp->xc_state;
  2039. ppp->xc_state = NULL;
  2040. ppp->rstate = 0;
  2041. rcomp = ppp->rcomp;
  2042. rstate = ppp->rc_state;
  2043. ppp->rc_state = NULL;
  2044. ppp_unlock(ppp);
  2045. if (xstate) {
  2046. xcomp->comp_free(xstate);
  2047. module_put(xcomp->owner);
  2048. }
  2049. if (rstate) {
  2050. rcomp->decomp_free(rstate);
  2051. module_put(rcomp->owner);
  2052. }
  2053. }
  2054. /* List of compressors. */
  2055. static LIST_HEAD(compressor_list);
  2056. static DEFINE_SPINLOCK(compressor_list_lock);
  2057. struct compressor_entry {
  2058. struct list_head list;
  2059. struct compressor *comp;
  2060. };
  2061. static struct compressor_entry *
  2062. find_comp_entry(int proto)
  2063. {
  2064. struct compressor_entry *ce;
  2065. list_for_each_entry(ce, &compressor_list, list) {
  2066. if (ce->comp->compress_proto == proto)
  2067. return ce;
  2068. }
  2069. return NULL;
  2070. }
  2071. /* Register a compressor */
  2072. int
  2073. ppp_register_compressor(struct compressor *cp)
  2074. {
  2075. struct compressor_entry *ce;
  2076. int ret;
  2077. spin_lock(&compressor_list_lock);
  2078. ret = -EEXIST;
  2079. if (find_comp_entry(cp->compress_proto) != 0)
  2080. goto out;
  2081. ret = -ENOMEM;
  2082. ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
  2083. if (ce == 0)
  2084. goto out;
  2085. ret = 0;
  2086. ce->comp = cp;
  2087. list_add(&ce->list, &compressor_list);
  2088. out:
  2089. spin_unlock(&compressor_list_lock);
  2090. return ret;
  2091. }
  2092. /* Unregister a compressor */
  2093. void
  2094. ppp_unregister_compressor(struct compressor *cp)
  2095. {
  2096. struct compressor_entry *ce;
  2097. spin_lock(&compressor_list_lock);
  2098. ce = find_comp_entry(cp->compress_proto);
  2099. if (ce != 0 && ce->comp == cp) {
  2100. list_del(&ce->list);
  2101. kfree(ce);
  2102. }
  2103. spin_unlock(&compressor_list_lock);
  2104. }
  2105. /* Find a compressor. */
  2106. static struct compressor *
  2107. find_compressor(int type)
  2108. {
  2109. struct compressor_entry *ce;
  2110. struct compressor *cp = NULL;
  2111. spin_lock(&compressor_list_lock);
  2112. ce = find_comp_entry(type);
  2113. if (ce != 0) {
  2114. cp = ce->comp;
  2115. if (!try_module_get(cp->owner))
  2116. cp = NULL;
  2117. }
  2118. spin_unlock(&compressor_list_lock);
  2119. return cp;
  2120. }
  2121. /*
  2122. * Miscelleneous stuff.
  2123. */
  2124. static void
  2125. ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
  2126. {
  2127. struct slcompress *vj = ppp->vj;
  2128. memset(st, 0, sizeof(*st));
  2129. st->p.ppp_ipackets = ppp->stats.rx_packets;
  2130. st->p.ppp_ierrors = ppp->stats.rx_errors;
  2131. st->p.ppp_ibytes = ppp->stats.rx_bytes;
  2132. st->p.ppp_opackets = ppp->stats.tx_packets;
  2133. st->p.ppp_oerrors = ppp->stats.tx_errors;
  2134. st->p.ppp_obytes = ppp->stats.tx_bytes;
  2135. if (vj == 0)
  2136. return;
  2137. st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
  2138. st->vj.vjs_compressed = vj->sls_o_compressed;
  2139. st->vj.vjs_searches = vj->sls_o_searches;
  2140. st->vj.vjs_misses = vj->sls_o_misses;
  2141. st->vj.vjs_errorin = vj->sls_i_error;
  2142. st->vj.vjs_tossed = vj->sls_i_tossed;
  2143. st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
  2144. st->vj.vjs_compressedin = vj->sls_i_compressed;
  2145. }
  2146. /*
  2147. * Stuff for handling the lists of ppp units and channels
  2148. * and for initialization.
  2149. */
  2150. /*
  2151. * Create a new ppp interface unit. Fails if it can't allocate memory
  2152. * or if there is already a unit with the requested number.
  2153. * unit == -1 means allocate a new number.
  2154. */
  2155. static struct ppp *
  2156. ppp_create_interface(int unit, int *retp)
  2157. {
  2158. struct ppp *ppp;
  2159. struct net_device *dev = NULL;
  2160. int ret = -ENOMEM;
  2161. int i;
  2162. ppp = kzalloc(sizeof(struct ppp), GFP_KERNEL);
  2163. if (!ppp)
  2164. goto out;
  2165. dev = alloc_netdev(0, "", ppp_setup);
  2166. if (!dev)
  2167. goto out1;
  2168. ppp->mru = PPP_MRU;
  2169. init_ppp_file(&ppp->file, INTERFACE);
  2170. ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
  2171. for (i = 0; i < NUM_NP; ++i)
  2172. ppp->npmode[i] = NPMODE_PASS;
  2173. INIT_LIST_HEAD(&ppp->channels);
  2174. spin_lock_init(&ppp->rlock);
  2175. spin_lock_init(&ppp->wlock);
  2176. #ifdef CONFIG_PPP_MULTILINK
  2177. ppp->minseq = -1;
  2178. skb_queue_head_init(&ppp->mrq);
  2179. #endif /* CONFIG_PPP_MULTILINK */
  2180. ppp->dev = dev;
  2181. dev->priv = ppp;
  2182. dev->hard_start_xmit = ppp_start_xmit;
  2183. dev->get_stats = ppp_net_stats;
  2184. dev->do_ioctl = ppp_net_ioctl;
  2185. ret = -EEXIST;
  2186. mutex_lock(&all_ppp_mutex);
  2187. if (unit < 0)
  2188. unit = cardmap_find_first_free(all_ppp_units);
  2189. else if (cardmap_get(all_ppp_units, unit) != NULL)
  2190. goto out2; /* unit already exists */
  2191. /* Initialize the new ppp unit */
  2192. ppp->file.index = unit;
  2193. sprintf(dev->name, "ppp%d", unit);
  2194. ret = register_netdev(dev);
  2195. if (ret != 0) {
  2196. printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
  2197. dev->name, ret);
  2198. goto out2;
  2199. }
  2200. atomic_inc(&ppp_unit_count);
  2201. ret = cardmap_set(&all_ppp_units, unit, ppp);
  2202. if (ret != 0)
  2203. goto out3;
  2204. mutex_unlock(&all_ppp_mutex);
  2205. *retp = 0;
  2206. return ppp;
  2207. out3:
  2208. atomic_dec(&ppp_unit_count);
  2209. out2:
  2210. mutex_unlock(&all_ppp_mutex);
  2211. free_netdev(dev);
  2212. out1:
  2213. kfree(ppp);
  2214. out:
  2215. *retp = ret;
  2216. return NULL;
  2217. }
  2218. /*
  2219. * Initialize a ppp_file structure.
  2220. */
  2221. static void
  2222. init_ppp_file(struct ppp_file *pf, int kind)
  2223. {
  2224. pf->kind = kind;
  2225. skb_queue_head_init(&pf->xq);
  2226. skb_queue_head_init(&pf->rq);
  2227. atomic_set(&pf->refcnt, 1);
  2228. init_waitqueue_head(&pf->rwait);
  2229. }
  2230. /*
  2231. * Take down a ppp interface unit - called when the owning file
  2232. * (the one that created the unit) is closed or detached.
  2233. */
  2234. static void ppp_shutdown_interface(struct ppp *ppp)
  2235. {
  2236. struct net_device *dev;
  2237. mutex_lock(&all_ppp_mutex);
  2238. ppp_lock(ppp);
  2239. dev = ppp->dev;
  2240. ppp->dev = NULL;
  2241. ppp_unlock(ppp);
  2242. /* This will call dev_close() for us. */
  2243. if (dev) {
  2244. unregister_netdev(dev);
  2245. free_netdev(dev);
  2246. }
  2247. cardmap_set(&all_ppp_units, ppp->file.index, NULL);
  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. kfree(ppp);
  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 cardmap_get(all_ppp_units, 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 == 0)
  2333. goto out;
  2334. write_lock_bh(&pch->upl);
  2335. ret = -EINVAL;
  2336. if (pch->ppp != 0)
  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 (ppp->dev && 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 != 0) {
  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. cardmap_destroy(&all_ppp_units);
  2403. if (unregister_chrdev(PPP_MAJOR, "ppp") != 0)
  2404. printk(KERN_ERR "PPP: failed to unregister PPP device\n");
  2405. device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
  2406. class_destroy(ppp_class);
  2407. }
  2408. /*
  2409. * Cardmap implementation.
  2410. */
  2411. static void *cardmap_get(struct cardmap *map, unsigned int nr)
  2412. {
  2413. struct cardmap *p;
  2414. int i;
  2415. for (p = map; p != NULL; ) {
  2416. if ((i = nr >> p->shift) >= CARDMAP_WIDTH)
  2417. return NULL;
  2418. if (p->shift == 0)
  2419. return p->ptr[i];
  2420. nr &= ~(CARDMAP_MASK << p->shift);
  2421. p = p->ptr[i];
  2422. }
  2423. return NULL;
  2424. }
  2425. static int cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
  2426. {
  2427. struct cardmap *p;
  2428. int i;
  2429. p = *pmap;
  2430. if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
  2431. do {
  2432. /* need a new top level */
  2433. struct cardmap *np = kzalloc(sizeof(*np), GFP_KERNEL);
  2434. if (!np)
  2435. goto enomem;
  2436. np->ptr[0] = p;
  2437. if (p != NULL) {
  2438. np->shift = p->shift + CARDMAP_ORDER;
  2439. p->parent = np;
  2440. } else
  2441. np->shift = 0;
  2442. p = np;
  2443. } while ((nr >> p->shift) >= CARDMAP_WIDTH);
  2444. *pmap = p;
  2445. }
  2446. while (p->shift > 0) {
  2447. i = (nr >> p->shift) & CARDMAP_MASK;
  2448. if (p->ptr[i] == NULL) {
  2449. struct cardmap *np = kzalloc(sizeof(*np), GFP_KERNEL);
  2450. if (!np)
  2451. goto enomem;
  2452. np->shift = p->shift - CARDMAP_ORDER;
  2453. np->parent = p;
  2454. p->ptr[i] = np;
  2455. }
  2456. if (ptr == NULL)
  2457. clear_bit(i, &p->inuse);
  2458. p = p->ptr[i];
  2459. }
  2460. i = nr & CARDMAP_MASK;
  2461. p->ptr[i] = ptr;
  2462. if (ptr != NULL)
  2463. set_bit(i, &p->inuse);
  2464. else
  2465. clear_bit(i, &p->inuse);
  2466. return 0;
  2467. enomem:
  2468. return -ENOMEM;
  2469. }
  2470. static unsigned int cardmap_find_first_free(struct cardmap *map)
  2471. {
  2472. struct cardmap *p;
  2473. unsigned int nr = 0;
  2474. int i;
  2475. if ((p = map) == NULL)
  2476. return 0;
  2477. for (;;) {
  2478. i = find_first_zero_bit(&p->inuse, CARDMAP_WIDTH);
  2479. if (i >= CARDMAP_WIDTH) {
  2480. if (p->parent == NULL)
  2481. return CARDMAP_WIDTH << p->shift;
  2482. p = p->parent;
  2483. i = (nr >> p->shift) & CARDMAP_MASK;
  2484. set_bit(i, &p->inuse);
  2485. continue;
  2486. }
  2487. nr = (nr & (~CARDMAP_MASK << p->shift)) | (i << p->shift);
  2488. if (p->shift == 0 || p->ptr[i] == NULL)
  2489. return nr;
  2490. p = p->ptr[i];
  2491. }
  2492. }
  2493. static void cardmap_destroy(struct cardmap **pmap)
  2494. {
  2495. struct cardmap *p, *np;
  2496. int i;
  2497. for (p = *pmap; p != NULL; p = np) {
  2498. if (p->shift != 0) {
  2499. for (i = 0; i < CARDMAP_WIDTH; ++i)
  2500. if (p->ptr[i] != NULL)
  2501. break;
  2502. if (i < CARDMAP_WIDTH) {
  2503. np = p->ptr[i];
  2504. p->ptr[i] = NULL;
  2505. continue;
  2506. }
  2507. }
  2508. np = p->parent;
  2509. kfree(p);
  2510. }
  2511. *pmap = NULL;
  2512. }
  2513. /* Module/initialization stuff */
  2514. module_init(ppp_init);
  2515. module_exit(ppp_cleanup);
  2516. EXPORT_SYMBOL(ppp_register_channel);
  2517. EXPORT_SYMBOL(ppp_unregister_channel);
  2518. EXPORT_SYMBOL(ppp_channel_index);
  2519. EXPORT_SYMBOL(ppp_unit_number);
  2520. EXPORT_SYMBOL(ppp_input);
  2521. EXPORT_SYMBOL(ppp_input_error);
  2522. EXPORT_SYMBOL(ppp_output_wakeup);
  2523. EXPORT_SYMBOL(ppp_register_compressor);
  2524. EXPORT_SYMBOL(ppp_unregister_compressor);
  2525. MODULE_LICENSE("GPL");
  2526. MODULE_ALIAS_CHARDEV_MAJOR(PPP_MAJOR);
  2527. MODULE_ALIAS("/dev/ppp");