ppp_generic.c 64 KB

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