ppp_generic.c 72 KB

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