ppp_generic.c 76 KB

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