ppp_generic.c 68 KB

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