ppp_generic.c 69 KB

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