ppp_generic.c 67 KB

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