ppp_generic.c 67 KB

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