ppp_generic.c 67 KB

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