ppp_generic.c 71 KB

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