ppp_generic.c 67 KB

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