sock.h 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * Definitions for the AF_INET socket handler.
  8. *
  9. * Version: @(#)sock.h 1.0.4 05/13/93
  10. *
  11. * Authors: Ross Biro
  12. * Fred N. van Kempen, <[email protected]>
  13. * Corey Minyard <[email protected]>
  14. * Florian La Roche <[email protected]>
  15. *
  16. * Fixes:
  17. * Alan Cox : Volatiles in skbuff pointers. See
  18. * skbuff comments. May be overdone,
  19. * better to prove they can be removed
  20. * than the reverse.
  21. * Alan Cox : Added a zapped field for tcp to note
  22. * a socket is reset and must stay shut up
  23. * Alan Cox : New fields for options
  24. * Pauline Middelink : identd support
  25. * Alan Cox : Eliminate low level recv/recvfrom
  26. * David S. Miller : New socket lookup architecture.
  27. * Steve Whitehouse: Default routines for sock_ops
  28. * Arnaldo C. Melo : removed net_pinfo, tp_pinfo and made
  29. * protinfo be just a void pointer, as the
  30. * protocol specific parts were moved to
  31. * respective headers and ipv4/v6, etc now
  32. * use private slabcaches for its socks
  33. * Pedro Hortas : New flags field for socket options
  34. */
  35. #ifndef _SOCK_H
  36. #define _SOCK_H
  37. #include <linux/hardirq.h>
  38. #include <linux/kernel.h>
  39. #include <linux/list.h>
  40. #include <linux/list_nulls.h>
  41. #include <linux/timer.h>
  42. #include <linux/cache.h>
  43. #include <linux/bitops.h>
  44. #include <linux/lockdep.h>
  45. #include <linux/netdevice.h>
  46. #include <linux/skbuff.h> /* struct sk_buff */
  47. #include <linux/mm.h>
  48. #include <linux/security.h>
  49. #include <linux/slab.h>
  50. #include <linux/uaccess.h>
  51. #include <linux/page_counter.h>
  52. #include <linux/memcontrol.h>
  53. #include <linux/static_key.h>
  54. #include <linux/sched.h>
  55. #include <linux/wait.h>
  56. #include <linux/cgroup-defs.h>
  57. #include <linux/rbtree.h>
  58. #include <linux/rculist_nulls.h>
  59. #include <linux/poll.h>
  60. #include <linux/sockptr.h>
  61. #include <linux/indirect_call_wrapper.h>
  62. #include <linux/atomic.h>
  63. #include <linux/refcount.h>
  64. #include <linux/llist.h>
  65. #include <net/dst.h>
  66. #include <net/checksum.h>
  67. #include <net/tcp_states.h>
  68. #include <linux/net_tstamp.h>
  69. #include <net/l3mdev.h>
  70. #include <uapi/linux/socket.h>
  71. #include <linux/android_vendor.h>
  72. #include <linux/android_kabi.h>
  73. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  74. #ifdef CONFIG_KNOX_NCM
  75. #define NAP_PROCESS_NAME_LEN 128
  76. #define NAP_DOMAIN_NAME_LEN 255
  77. #endif
  78. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  79. /*
  80. * This structure really needs to be cleaned up.
  81. * Most of it is for TCP, and not used by any of
  82. * the other protocols.
  83. */
  84. /* Define this to get the SOCK_DBG debugging facility. */
  85. #define SOCK_DEBUGGING
  86. #ifdef SOCK_DEBUGGING
  87. #define SOCK_DEBUG(sk, msg...) do { if ((sk) && sock_flag((sk), SOCK_DBG)) \
  88. printk(KERN_DEBUG msg); } while (0)
  89. #else
  90. /* Validate arguments and do nothing */
  91. static inline __printf(2, 3)
  92. void SOCK_DEBUG(const struct sock *sk, const char *msg, ...)
  93. {
  94. }
  95. #endif
  96. /* This is the per-socket lock. The spinlock provides a synchronization
  97. * between user contexts and software interrupt processing, whereas the
  98. * mini-semaphore synchronizes multiple users amongst themselves.
  99. */
  100. typedef struct {
  101. spinlock_t slock;
  102. int owned;
  103. wait_queue_head_t wq;
  104. /*
  105. * We express the mutex-alike socket_lock semantics
  106. * to the lock validator by explicitly managing
  107. * the slock as a lock variant (in addition to
  108. * the slock itself):
  109. */
  110. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  111. struct lockdep_map dep_map;
  112. #endif
  113. } socket_lock_t;
  114. struct sock;
  115. struct proto;
  116. struct net;
  117. typedef __u32 __bitwise __portpair;
  118. typedef __u64 __bitwise __addrpair;
  119. /**
  120. * struct sock_common - minimal network layer representation of sockets
  121. * @skc_daddr: Foreign IPv4 addr
  122. * @skc_rcv_saddr: Bound local IPv4 addr
  123. * @skc_addrpair: 8-byte-aligned __u64 union of @skc_daddr & @skc_rcv_saddr
  124. * @skc_hash: hash value used with various protocol lookup tables
  125. * @skc_u16hashes: two u16 hash values used by UDP lookup tables
  126. * @skc_dport: placeholder for inet_dport/tw_dport
  127. * @skc_num: placeholder for inet_num/tw_num
  128. * @skc_portpair: __u32 union of @skc_dport & @skc_num
  129. * @skc_family: network address family
  130. * @skc_state: Connection state
  131. * @skc_reuse: %SO_REUSEADDR setting
  132. * @skc_reuseport: %SO_REUSEPORT setting
  133. * @skc_ipv6only: socket is IPV6 only
  134. * @skc_net_refcnt: socket is using net ref counting
  135. * @skc_bound_dev_if: bound device index if != 0
  136. * @skc_bind_node: bind hash linkage for various protocol lookup tables
  137. * @skc_portaddr_node: second hash linkage for UDP/UDP-Lite protocol
  138. * @skc_prot: protocol handlers inside a network family
  139. * @skc_net: reference to the network namespace of this socket
  140. * @skc_v6_daddr: IPV6 destination address
  141. * @skc_v6_rcv_saddr: IPV6 source address
  142. * @skc_cookie: socket's cookie value
  143. * @skc_node: main hash linkage for various protocol lookup tables
  144. * @skc_nulls_node: main hash linkage for TCP/UDP/UDP-Lite protocol
  145. * @skc_tx_queue_mapping: tx queue number for this connection
  146. * @skc_rx_queue_mapping: rx queue number for this connection
  147. * @skc_flags: place holder for sk_flags
  148. * %SO_LINGER (l_onoff), %SO_BROADCAST, %SO_KEEPALIVE,
  149. * %SO_OOBINLINE settings, %SO_TIMESTAMPING settings
  150. * @skc_listener: connection request listener socket (aka rsk_listener)
  151. * [union with @skc_flags]
  152. * @skc_tw_dr: (aka tw_dr) ptr to &struct inet_timewait_death_row
  153. * [union with @skc_flags]
  154. * @skc_incoming_cpu: record/match cpu processing incoming packets
  155. * @skc_rcv_wnd: (aka rsk_rcv_wnd) TCP receive window size (possibly scaled)
  156. * [union with @skc_incoming_cpu]
  157. * @skc_tw_rcv_nxt: (aka tw_rcv_nxt) TCP window next expected seq number
  158. * [union with @skc_incoming_cpu]
  159. * @skc_refcnt: reference count
  160. *
  161. * This is the minimal network layer representation of sockets, the header
  162. * for struct sock and struct inet_timewait_sock.
  163. */
  164. struct sock_common {
  165. union {
  166. __addrpair skc_addrpair;
  167. struct {
  168. __be32 skc_daddr;
  169. __be32 skc_rcv_saddr;
  170. };
  171. };
  172. union {
  173. unsigned int skc_hash;
  174. __u16 skc_u16hashes[2];
  175. };
  176. /* skc_dport && skc_num must be grouped as well */
  177. union {
  178. __portpair skc_portpair;
  179. struct {
  180. __be16 skc_dport;
  181. __u16 skc_num;
  182. };
  183. };
  184. unsigned short skc_family;
  185. volatile unsigned char skc_state;
  186. unsigned char skc_reuse:4;
  187. unsigned char skc_reuseport:1;
  188. unsigned char skc_ipv6only:1;
  189. unsigned char skc_net_refcnt:1;
  190. int skc_bound_dev_if;
  191. union {
  192. struct hlist_node skc_bind_node;
  193. struct hlist_node skc_portaddr_node;
  194. };
  195. struct proto *skc_prot;
  196. possible_net_t skc_net;
  197. #if IS_ENABLED(CONFIG_IPV6)
  198. struct in6_addr skc_v6_daddr;
  199. struct in6_addr skc_v6_rcv_saddr;
  200. #endif
  201. atomic64_t skc_cookie;
  202. /* following fields are padding to force
  203. * offset(struct sock, sk_refcnt) == 128 on 64bit arches
  204. * assuming IPV6 is enabled. We use this padding differently
  205. * for different kind of 'sockets'
  206. */
  207. union {
  208. unsigned long skc_flags;
  209. struct sock *skc_listener; /* request_sock */
  210. struct inet_timewait_death_row *skc_tw_dr; /* inet_timewait_sock */
  211. };
  212. /*
  213. * fields between dontcopy_begin/dontcopy_end
  214. * are not copied in sock_copy()
  215. */
  216. /* private: */
  217. int skc_dontcopy_begin[0];
  218. /* public: */
  219. union {
  220. struct hlist_node skc_node;
  221. struct hlist_nulls_node skc_nulls_node;
  222. };
  223. unsigned short skc_tx_queue_mapping;
  224. #ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
  225. unsigned short skc_rx_queue_mapping;
  226. #endif
  227. union {
  228. int skc_incoming_cpu;
  229. u32 skc_rcv_wnd;
  230. u32 skc_tw_rcv_nxt; /* struct tcp_timewait_sock */
  231. };
  232. refcount_t skc_refcnt;
  233. /* private: */
  234. int skc_dontcopy_end[0];
  235. union {
  236. u32 skc_rxhash;
  237. u32 skc_window_clamp;
  238. u32 skc_tw_snd_nxt; /* struct tcp_timewait_sock */
  239. };
  240. /* public: */
  241. };
  242. struct bpf_local_storage;
  243. struct sk_filter;
  244. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA {
  245. #ifdef CONFIG_KNOX_NCM
  246. struct sock_npa_vendor_data {
  247. uid_t knox_uid;
  248. pid_t knox_pid;
  249. uid_t knox_dns_uid;
  250. char domain_name[NAP_DOMAIN_NAME_LEN];
  251. char process_name[NAP_PROCESS_NAME_LEN];
  252. uid_t knox_puid;
  253. pid_t knox_ppid;
  254. char parent_process_name[NAP_PROCESS_NAME_LEN];
  255. pid_t knox_dns_pid;
  256. char dns_process_name[NAP_PROCESS_NAME_LEN];
  257. };
  258. #define SOCK_NPA_VENDOR_DATA_GET(sock) ((struct sock_npa_vendor_data*)((sock)->android_oem_data1))
  259. #endif
  260. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_NPA }
  261. /**
  262. * struct sock - network layer representation of sockets
  263. * @__sk_common: shared layout with inet_timewait_sock
  264. * @sk_shutdown: mask of %SEND_SHUTDOWN and/or %RCV_SHUTDOWN
  265. * @sk_userlocks: %SO_SNDBUF and %SO_RCVBUF settings
  266. * @sk_lock: synchronizer
  267. * @sk_kern_sock: True if sock is using kernel lock classes
  268. * @sk_rcvbuf: size of receive buffer in bytes
  269. * @sk_wq: sock wait queue and async head
  270. * @sk_rx_dst: receive input route used by early demux
  271. * @sk_rx_dst_ifindex: ifindex for @sk_rx_dst
  272. * @sk_rx_dst_cookie: cookie for @sk_rx_dst
  273. * @sk_dst_cache: destination cache
  274. * @sk_dst_pending_confirm: need to confirm neighbour
  275. * @sk_policy: flow policy
  276. * @sk_receive_queue: incoming packets
  277. * @sk_wmem_alloc: transmit queue bytes committed
  278. * @sk_tsq_flags: TCP Small Queues flags
  279. * @sk_write_queue: Packet sending queue
  280. * @sk_omem_alloc: "o" is "option" or "other"
  281. * @sk_wmem_queued: persistent queue size
  282. * @sk_forward_alloc: space allocated forward
  283. * @sk_reserved_mem: space reserved and non-reclaimable for the socket
  284. * @sk_napi_id: id of the last napi context to receive data for sk
  285. * @sk_ll_usec: usecs to busypoll when there is no data
  286. * @sk_allocation: allocation mode
  287. * @sk_pacing_rate: Pacing rate (if supported by transport/packet scheduler)
  288. * @sk_pacing_status: Pacing status (requested, handled by sch_fq)
  289. * @sk_max_pacing_rate: Maximum pacing rate (%SO_MAX_PACING_RATE)
  290. * @sk_sndbuf: size of send buffer in bytes
  291. * @__sk_flags_offset: empty field used to determine location of bitfield
  292. * @sk_padding: unused element for alignment
  293. * @sk_no_check_tx: %SO_NO_CHECK setting, set checksum in TX packets
  294. * @sk_no_check_rx: allow zero checksum in RX packets
  295. * @sk_route_caps: route capabilities (e.g. %NETIF_F_TSO)
  296. * @sk_gso_disabled: if set, NETIF_F_GSO_MASK is forbidden.
  297. * @sk_gso_type: GSO type (e.g. %SKB_GSO_TCPV4)
  298. * @sk_gso_max_size: Maximum GSO segment size to build
  299. * @sk_gso_max_segs: Maximum number of GSO segments
  300. * @sk_pacing_shift: scaling factor for TCP Small Queues
  301. * @sk_lingertime: %SO_LINGER l_linger setting
  302. * @sk_backlog: always used with the per-socket spinlock held
  303. * @sk_callback_lock: used with the callbacks in the end of this struct
  304. * @sk_error_queue: rarely used
  305. * @sk_prot_creator: sk_prot of original sock creator (see ipv6_setsockopt,
  306. * IPV6_ADDRFORM for instance)
  307. * @sk_err: last error
  308. * @sk_err_soft: errors that don't cause failure but are the cause of a
  309. * persistent failure not just 'timed out'
  310. * @sk_drops: raw/udp drops counter
  311. * @sk_ack_backlog: current listen backlog
  312. * @sk_max_ack_backlog: listen backlog set in listen()
  313. * @sk_uid: user id of owner
  314. * @sk_prefer_busy_poll: prefer busypolling over softirq processing
  315. * @sk_busy_poll_budget: napi processing budget when busypolling
  316. * @sk_priority: %SO_PRIORITY setting
  317. * @sk_type: socket type (%SOCK_STREAM, etc)
  318. * @sk_protocol: which protocol this socket belongs in this network family
  319. * @sk_peer_lock: lock protecting @sk_peer_pid and @sk_peer_cred
  320. * @sk_peer_pid: &struct pid for this socket's peer
  321. * @sk_peer_cred: %SO_PEERCRED setting
  322. * @sk_rcvlowat: %SO_RCVLOWAT setting
  323. * @sk_rcvtimeo: %SO_RCVTIMEO setting
  324. * @sk_sndtimeo: %SO_SNDTIMEO setting
  325. * @sk_txhash: computed flow hash for use on transmit
  326. * @sk_txrehash: enable TX hash rethink
  327. * @sk_filter: socket filtering instructions
  328. * @sk_timer: sock cleanup timer
  329. * @sk_stamp: time stamp of last packet received
  330. * @sk_stamp_seq: lock for accessing sk_stamp on 32 bit architectures only
  331. * @sk_tsflags: SO_TIMESTAMPING flags
  332. * @sk_bind_phc: SO_TIMESTAMPING bind PHC index of PTP virtual clock
  333. * for timestamping
  334. * @sk_tskey: counter to disambiguate concurrent tstamp requests
  335. * @sk_zckey: counter to order MSG_ZEROCOPY notifications
  336. * @sk_socket: Identd and reporting IO signals
  337. * @sk_user_data: RPC layer private data. Write-protected by @sk_callback_lock.
  338. * @sk_frag: cached page frag
  339. * @sk_peek_off: current peek_offset value
  340. * @sk_send_head: front of stuff to transmit
  341. * @tcp_rtx_queue: TCP re-transmit queue [union with @sk_send_head]
  342. * @sk_security: used by security modules
  343. * @sk_mark: generic packet mark
  344. * @sk_cgrp_data: cgroup data for this cgroup
  345. * @sk_memcg: this socket's memory cgroup association
  346. * @sk_write_pending: a write to stream socket waits to start
  347. * @sk_wait_pending: number of threads blocked on this socket
  348. * @sk_state_change: callback to indicate change in the state of the sock
  349. * @sk_data_ready: callback to indicate there is data to be processed
  350. * @sk_write_space: callback to indicate there is bf sending space available
  351. * @sk_error_report: callback to indicate errors (e.g. %MSG_ERRQUEUE)
  352. * @sk_backlog_rcv: callback to process the backlog
  353. * @sk_validate_xmit_skb: ptr to an optional validate function
  354. * @sk_destruct: called at sock freeing time, i.e. when all refcnt == 0
  355. * @sk_reuseport_cb: reuseport group container
  356. * @sk_bpf_storage: ptr to cache and control for bpf_sk_storage
  357. * @sk_rcu: used during RCU grace period
  358. * @sk_clockid: clockid used by time-based scheduling (SO_TXTIME)
  359. * @sk_txtime_deadline_mode: set deadline mode for SO_TXTIME
  360. * @sk_txtime_report_errors: set report errors mode for SO_TXTIME
  361. * @sk_txtime_unused: unused txtime flags
  362. * @ns_tracker: tracker for netns reference
  363. * @sk_bind2_node: bind node in the bhash2 table
  364. */
  365. struct sock {
  366. /*
  367. * Now struct inet_timewait_sock also uses sock_common, so please just
  368. * don't add nothing before this first member (__sk_common) --acme
  369. */
  370. struct sock_common __sk_common;
  371. #define sk_node __sk_common.skc_node
  372. #define sk_nulls_node __sk_common.skc_nulls_node
  373. #define sk_refcnt __sk_common.skc_refcnt
  374. #define sk_tx_queue_mapping __sk_common.skc_tx_queue_mapping
  375. #ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
  376. #define sk_rx_queue_mapping __sk_common.skc_rx_queue_mapping
  377. #endif
  378. #define sk_dontcopy_begin __sk_common.skc_dontcopy_begin
  379. #define sk_dontcopy_end __sk_common.skc_dontcopy_end
  380. #define sk_hash __sk_common.skc_hash
  381. #define sk_portpair __sk_common.skc_portpair
  382. #define sk_num __sk_common.skc_num
  383. #define sk_dport __sk_common.skc_dport
  384. #define sk_addrpair __sk_common.skc_addrpair
  385. #define sk_daddr __sk_common.skc_daddr
  386. #define sk_rcv_saddr __sk_common.skc_rcv_saddr
  387. #define sk_family __sk_common.skc_family
  388. #define sk_state __sk_common.skc_state
  389. #define sk_reuse __sk_common.skc_reuse
  390. #define sk_reuseport __sk_common.skc_reuseport
  391. #define sk_ipv6only __sk_common.skc_ipv6only
  392. #define sk_net_refcnt __sk_common.skc_net_refcnt
  393. #define sk_bound_dev_if __sk_common.skc_bound_dev_if
  394. #define sk_bind_node __sk_common.skc_bind_node
  395. #define sk_prot __sk_common.skc_prot
  396. #define sk_net __sk_common.skc_net
  397. #define sk_v6_daddr __sk_common.skc_v6_daddr
  398. #define sk_v6_rcv_saddr __sk_common.skc_v6_rcv_saddr
  399. #define sk_cookie __sk_common.skc_cookie
  400. #define sk_incoming_cpu __sk_common.skc_incoming_cpu
  401. #define sk_flags __sk_common.skc_flags
  402. #define sk_rxhash __sk_common.skc_rxhash
  403. /* early demux fields */
  404. struct dst_entry __rcu *sk_rx_dst;
  405. int sk_rx_dst_ifindex;
  406. u32 sk_rx_dst_cookie;
  407. socket_lock_t sk_lock;
  408. atomic_t sk_drops;
  409. int sk_rcvlowat;
  410. struct sk_buff_head sk_error_queue;
  411. struct sk_buff_head sk_receive_queue;
  412. /*
  413. * The backlog queue is special, it is always used with
  414. * the per-socket spinlock held and requires low latency
  415. * access. Therefore we special case it's implementation.
  416. * Note : rmem_alloc is in this structure to fill a hole
  417. * on 64bit arches, not because its logically part of
  418. * backlog.
  419. */
  420. struct {
  421. atomic_t rmem_alloc;
  422. int len;
  423. struct sk_buff *head;
  424. struct sk_buff *tail;
  425. } sk_backlog;
  426. #define sk_rmem_alloc sk_backlog.rmem_alloc
  427. int sk_forward_alloc;
  428. u32 sk_reserved_mem;
  429. #ifdef CONFIG_NET_RX_BUSY_POLL
  430. unsigned int sk_ll_usec;
  431. /* ===== mostly read cache line ===== */
  432. unsigned int sk_napi_id;
  433. #endif
  434. int sk_rcvbuf;
  435. int sk_wait_pending;
  436. struct sk_filter __rcu *sk_filter;
  437. union {
  438. struct socket_wq __rcu *sk_wq;
  439. /* private: */
  440. struct socket_wq *sk_wq_raw;
  441. /* public: */
  442. };
  443. #ifdef CONFIG_XFRM
  444. struct xfrm_policy __rcu *sk_policy[2];
  445. #endif
  446. struct dst_entry __rcu *sk_dst_cache;
  447. atomic_t sk_omem_alloc;
  448. int sk_sndbuf;
  449. /* ===== cache line for TX ===== */
  450. int sk_wmem_queued;
  451. refcount_t sk_wmem_alloc;
  452. unsigned long sk_tsq_flags;
  453. union {
  454. struct sk_buff *sk_send_head;
  455. struct rb_root tcp_rtx_queue;
  456. };
  457. struct sk_buff_head sk_write_queue;
  458. __s32 sk_peek_off;
  459. int sk_write_pending;
  460. __u32 sk_dst_pending_confirm;
  461. u32 sk_pacing_status; /* see enum sk_pacing */
  462. long sk_sndtimeo;
  463. struct timer_list sk_timer;
  464. __u32 sk_priority;
  465. __u32 sk_mark;
  466. unsigned long sk_pacing_rate; /* bytes per second */
  467. unsigned long sk_max_pacing_rate;
  468. struct page_frag sk_frag;
  469. netdev_features_t sk_route_caps;
  470. int sk_gso_type;
  471. unsigned int sk_gso_max_size;
  472. gfp_t sk_allocation;
  473. __u32 sk_txhash;
  474. /*
  475. * Because of non atomicity rules, all
  476. * changes are protected by socket lock.
  477. */
  478. u8 sk_gso_disabled : 1,
  479. sk_kern_sock : 1,
  480. sk_no_check_tx : 1,
  481. sk_no_check_rx : 1,
  482. sk_userlocks : 4;
  483. u8 sk_pacing_shift;
  484. u16 sk_type;
  485. u16 sk_protocol;
  486. u16 sk_gso_max_segs;
  487. unsigned long sk_lingertime;
  488. struct proto *sk_prot_creator;
  489. rwlock_t sk_callback_lock;
  490. int sk_err,
  491. sk_err_soft;
  492. u32 sk_ack_backlog;
  493. u32 sk_max_ack_backlog;
  494. kuid_t sk_uid;
  495. u8 sk_txrehash;
  496. #ifdef CONFIG_NET_RX_BUSY_POLL
  497. u8 sk_prefer_busy_poll;
  498. u16 sk_busy_poll_budget;
  499. #endif
  500. spinlock_t sk_peer_lock;
  501. int sk_bind_phc;
  502. struct pid *sk_peer_pid;
  503. const struct cred *sk_peer_cred;
  504. long sk_rcvtimeo;
  505. ktime_t sk_stamp;
  506. #if BITS_PER_LONG==32
  507. seqlock_t sk_stamp_seq;
  508. #endif
  509. u16 sk_tsflags;
  510. u8 sk_shutdown;
  511. atomic_t sk_tskey;
  512. atomic_t sk_zckey;
  513. u8 sk_clockid;
  514. u8 sk_txtime_deadline_mode : 1,
  515. sk_txtime_report_errors : 1,
  516. sk_txtime_unused : 6;
  517. struct socket *sk_socket;
  518. void *sk_user_data;
  519. #ifdef CONFIG_SECURITY
  520. void *sk_security;
  521. #endif
  522. struct sock_cgroup_data sk_cgrp_data;
  523. struct mem_cgroup *sk_memcg;
  524. void (*sk_state_change)(struct sock *sk);
  525. void (*sk_data_ready)(struct sock *sk);
  526. void (*sk_write_space)(struct sock *sk);
  527. void (*sk_error_report)(struct sock *sk);
  528. int (*sk_backlog_rcv)(struct sock *sk,
  529. struct sk_buff *skb);
  530. #ifdef CONFIG_SOCK_VALIDATE_XMIT
  531. struct sk_buff* (*sk_validate_xmit_skb)(struct sock *sk,
  532. struct net_device *dev,
  533. struct sk_buff *skb);
  534. #endif
  535. void (*sk_destruct)(struct sock *sk);
  536. struct sock_reuseport __rcu *sk_reuseport_cb;
  537. #ifdef CONFIG_BPF_SYSCALL
  538. struct bpf_local_storage __rcu *sk_bpf_storage;
  539. #endif
  540. struct rcu_head sk_rcu;
  541. netns_tracker ns_tracker;
  542. struct hlist_node sk_bind2_node;
  543. ANDROID_OEM_DATA(1);
  544. ANDROID_KABI_RESERVE(1);
  545. ANDROID_KABI_RESERVE(2);
  546. ANDROID_KABI_RESERVE(3);
  547. ANDROID_KABI_RESERVE(4);
  548. ANDROID_KABI_RESERVE(5);
  549. ANDROID_KABI_RESERVE(6);
  550. ANDROID_KABI_RESERVE(7);
  551. ANDROID_KABI_RESERVE(8);
  552. };
  553. enum sk_pacing {
  554. SK_PACING_NONE = 0,
  555. SK_PACING_NEEDED = 1,
  556. SK_PACING_FQ = 2,
  557. };
  558. /* flag bits in sk_user_data
  559. *
  560. * - SK_USER_DATA_NOCOPY: Pointer stored in sk_user_data might
  561. * not be suitable for copying when cloning the socket. For instance,
  562. * it can point to a reference counted object. sk_user_data bottom
  563. * bit is set if pointer must not be copied.
  564. *
  565. * - SK_USER_DATA_BPF: Mark whether sk_user_data field is
  566. * managed/owned by a BPF reuseport array. This bit should be set
  567. * when sk_user_data's sk is added to the bpf's reuseport_array.
  568. *
  569. * - SK_USER_DATA_PSOCK: Mark whether pointer stored in
  570. * sk_user_data points to psock type. This bit should be set
  571. * when sk_user_data is assigned to a psock object.
  572. */
  573. #define SK_USER_DATA_NOCOPY 1UL
  574. #define SK_USER_DATA_BPF 2UL
  575. #define SK_USER_DATA_PSOCK 4UL
  576. #define SK_USER_DATA_PTRMASK ~(SK_USER_DATA_NOCOPY | SK_USER_DATA_BPF |\
  577. SK_USER_DATA_PSOCK)
  578. /**
  579. * sk_user_data_is_nocopy - Test if sk_user_data pointer must not be copied
  580. * @sk: socket
  581. */
  582. static inline bool sk_user_data_is_nocopy(const struct sock *sk)
  583. {
  584. return ((uintptr_t)sk->sk_user_data & SK_USER_DATA_NOCOPY);
  585. }
  586. #define __sk_user_data(sk) ((*((void __rcu **)&(sk)->sk_user_data)))
  587. /**
  588. * __locked_read_sk_user_data_with_flags - return the pointer
  589. * only if argument flags all has been set in sk_user_data. Otherwise
  590. * return NULL
  591. *
  592. * @sk: socket
  593. * @flags: flag bits
  594. *
  595. * The caller must be holding sk->sk_callback_lock.
  596. */
  597. static inline void *
  598. __locked_read_sk_user_data_with_flags(const struct sock *sk,
  599. uintptr_t flags)
  600. {
  601. uintptr_t sk_user_data =
  602. (uintptr_t)rcu_dereference_check(__sk_user_data(sk),
  603. lockdep_is_held(&sk->sk_callback_lock));
  604. WARN_ON_ONCE(flags & SK_USER_DATA_PTRMASK);
  605. if ((sk_user_data & flags) == flags)
  606. return (void *)(sk_user_data & SK_USER_DATA_PTRMASK);
  607. return NULL;
  608. }
  609. /**
  610. * __rcu_dereference_sk_user_data_with_flags - return the pointer
  611. * only if argument flags all has been set in sk_user_data. Otherwise
  612. * return NULL
  613. *
  614. * @sk: socket
  615. * @flags: flag bits
  616. */
  617. static inline void *
  618. __rcu_dereference_sk_user_data_with_flags(const struct sock *sk,
  619. uintptr_t flags)
  620. {
  621. uintptr_t sk_user_data = (uintptr_t)rcu_dereference(__sk_user_data(sk));
  622. WARN_ON_ONCE(flags & SK_USER_DATA_PTRMASK);
  623. if ((sk_user_data & flags) == flags)
  624. return (void *)(sk_user_data & SK_USER_DATA_PTRMASK);
  625. return NULL;
  626. }
  627. #define rcu_dereference_sk_user_data(sk) \
  628. __rcu_dereference_sk_user_data_with_flags(sk, 0)
  629. #define __rcu_assign_sk_user_data_with_flags(sk, ptr, flags) \
  630. ({ \
  631. uintptr_t __tmp1 = (uintptr_t)(ptr), \
  632. __tmp2 = (uintptr_t)(flags); \
  633. WARN_ON_ONCE(__tmp1 & ~SK_USER_DATA_PTRMASK); \
  634. WARN_ON_ONCE(__tmp2 & SK_USER_DATA_PTRMASK); \
  635. rcu_assign_pointer(__sk_user_data((sk)), \
  636. __tmp1 | __tmp2); \
  637. })
  638. #define rcu_assign_sk_user_data(sk, ptr) \
  639. __rcu_assign_sk_user_data_with_flags(sk, ptr, 0)
  640. static inline
  641. struct net *sock_net(const struct sock *sk)
  642. {
  643. return read_pnet(&sk->sk_net);
  644. }
  645. static inline
  646. void sock_net_set(struct sock *sk, struct net *net)
  647. {
  648. write_pnet(&sk->sk_net, net);
  649. }
  650. /*
  651. * SK_CAN_REUSE and SK_NO_REUSE on a socket mean that the socket is OK
  652. * or not whether his port will be reused by someone else. SK_FORCE_REUSE
  653. * on a socket means that the socket will reuse everybody else's port
  654. * without looking at the other's sk_reuse value.
  655. */
  656. #define SK_NO_REUSE 0
  657. #define SK_CAN_REUSE 1
  658. #define SK_FORCE_REUSE 2
  659. int sk_set_peek_off(struct sock *sk, int val);
  660. static inline int sk_peek_offset(const struct sock *sk, int flags)
  661. {
  662. if (unlikely(flags & MSG_PEEK)) {
  663. return READ_ONCE(sk->sk_peek_off);
  664. }
  665. return 0;
  666. }
  667. static inline void sk_peek_offset_bwd(struct sock *sk, int val)
  668. {
  669. s32 off = READ_ONCE(sk->sk_peek_off);
  670. if (unlikely(off >= 0)) {
  671. off = max_t(s32, off - val, 0);
  672. WRITE_ONCE(sk->sk_peek_off, off);
  673. }
  674. }
  675. static inline void sk_peek_offset_fwd(struct sock *sk, int val)
  676. {
  677. sk_peek_offset_bwd(sk, -val);
  678. }
  679. /*
  680. * Hashed lists helper routines
  681. */
  682. static inline struct sock *sk_entry(const struct hlist_node *node)
  683. {
  684. return hlist_entry(node, struct sock, sk_node);
  685. }
  686. static inline struct sock *__sk_head(const struct hlist_head *head)
  687. {
  688. return hlist_entry(head->first, struct sock, sk_node);
  689. }
  690. static inline struct sock *sk_head(const struct hlist_head *head)
  691. {
  692. return hlist_empty(head) ? NULL : __sk_head(head);
  693. }
  694. static inline struct sock *__sk_nulls_head(const struct hlist_nulls_head *head)
  695. {
  696. return hlist_nulls_entry(head->first, struct sock, sk_nulls_node);
  697. }
  698. static inline struct sock *sk_nulls_head(const struct hlist_nulls_head *head)
  699. {
  700. return hlist_nulls_empty(head) ? NULL : __sk_nulls_head(head);
  701. }
  702. static inline struct sock *sk_next(const struct sock *sk)
  703. {
  704. return hlist_entry_safe(sk->sk_node.next, struct sock, sk_node);
  705. }
  706. static inline struct sock *sk_nulls_next(const struct sock *sk)
  707. {
  708. return (!is_a_nulls(sk->sk_nulls_node.next)) ?
  709. hlist_nulls_entry(sk->sk_nulls_node.next,
  710. struct sock, sk_nulls_node) :
  711. NULL;
  712. }
  713. static inline bool sk_unhashed(const struct sock *sk)
  714. {
  715. return hlist_unhashed(&sk->sk_node);
  716. }
  717. static inline bool sk_hashed(const struct sock *sk)
  718. {
  719. return !sk_unhashed(sk);
  720. }
  721. static inline void sk_node_init(struct hlist_node *node)
  722. {
  723. node->pprev = NULL;
  724. }
  725. static inline void __sk_del_node(struct sock *sk)
  726. {
  727. __hlist_del(&sk->sk_node);
  728. }
  729. /* NB: equivalent to hlist_del_init_rcu */
  730. static inline bool __sk_del_node_init(struct sock *sk)
  731. {
  732. if (sk_hashed(sk)) {
  733. __sk_del_node(sk);
  734. sk_node_init(&sk->sk_node);
  735. return true;
  736. }
  737. return false;
  738. }
  739. /* Grab socket reference count. This operation is valid only
  740. when sk is ALREADY grabbed f.e. it is found in hash table
  741. or a list and the lookup is made under lock preventing hash table
  742. modifications.
  743. */
  744. static __always_inline void sock_hold(struct sock *sk)
  745. {
  746. refcount_inc(&sk->sk_refcnt);
  747. }
  748. /* Ungrab socket in the context, which assumes that socket refcnt
  749. cannot hit zero, f.e. it is true in context of any socketcall.
  750. */
  751. static __always_inline void __sock_put(struct sock *sk)
  752. {
  753. refcount_dec(&sk->sk_refcnt);
  754. }
  755. static inline bool sk_del_node_init(struct sock *sk)
  756. {
  757. bool rc = __sk_del_node_init(sk);
  758. if (rc) {
  759. /* paranoid for a while -acme */
  760. WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
  761. __sock_put(sk);
  762. }
  763. return rc;
  764. }
  765. #define sk_del_node_init_rcu(sk) sk_del_node_init(sk)
  766. static inline bool __sk_nulls_del_node_init_rcu(struct sock *sk)
  767. {
  768. if (sk_hashed(sk)) {
  769. hlist_nulls_del_init_rcu(&sk->sk_nulls_node);
  770. return true;
  771. }
  772. return false;
  773. }
  774. static inline bool sk_nulls_del_node_init_rcu(struct sock *sk)
  775. {
  776. bool rc = __sk_nulls_del_node_init_rcu(sk);
  777. if (rc) {
  778. /* paranoid for a while -acme */
  779. WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
  780. __sock_put(sk);
  781. }
  782. return rc;
  783. }
  784. static inline void __sk_add_node(struct sock *sk, struct hlist_head *list)
  785. {
  786. hlist_add_head(&sk->sk_node, list);
  787. }
  788. static inline void sk_add_node(struct sock *sk, struct hlist_head *list)
  789. {
  790. sock_hold(sk);
  791. __sk_add_node(sk, list);
  792. }
  793. static inline void sk_add_node_rcu(struct sock *sk, struct hlist_head *list)
  794. {
  795. sock_hold(sk);
  796. if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
  797. sk->sk_family == AF_INET6)
  798. hlist_add_tail_rcu(&sk->sk_node, list);
  799. else
  800. hlist_add_head_rcu(&sk->sk_node, list);
  801. }
  802. static inline void sk_add_node_tail_rcu(struct sock *sk, struct hlist_head *list)
  803. {
  804. sock_hold(sk);
  805. hlist_add_tail_rcu(&sk->sk_node, list);
  806. }
  807. static inline void __sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
  808. {
  809. hlist_nulls_add_head_rcu(&sk->sk_nulls_node, list);
  810. }
  811. static inline void __sk_nulls_add_node_tail_rcu(struct sock *sk, struct hlist_nulls_head *list)
  812. {
  813. hlist_nulls_add_tail_rcu(&sk->sk_nulls_node, list);
  814. }
  815. static inline void sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
  816. {
  817. sock_hold(sk);
  818. __sk_nulls_add_node_rcu(sk, list);
  819. }
  820. static inline void __sk_del_bind_node(struct sock *sk)
  821. {
  822. __hlist_del(&sk->sk_bind_node);
  823. }
  824. static inline void sk_add_bind_node(struct sock *sk,
  825. struct hlist_head *list)
  826. {
  827. hlist_add_head(&sk->sk_bind_node, list);
  828. }
  829. static inline void __sk_del_bind2_node(struct sock *sk)
  830. {
  831. __hlist_del(&sk->sk_bind2_node);
  832. }
  833. static inline void sk_add_bind2_node(struct sock *sk, struct hlist_head *list)
  834. {
  835. hlist_add_head(&sk->sk_bind2_node, list);
  836. }
  837. #define sk_for_each(__sk, list) \
  838. hlist_for_each_entry(__sk, list, sk_node)
  839. #define sk_for_each_rcu(__sk, list) \
  840. hlist_for_each_entry_rcu(__sk, list, sk_node)
  841. #define sk_nulls_for_each(__sk, node, list) \
  842. hlist_nulls_for_each_entry(__sk, node, list, sk_nulls_node)
  843. #define sk_nulls_for_each_rcu(__sk, node, list) \
  844. hlist_nulls_for_each_entry_rcu(__sk, node, list, sk_nulls_node)
  845. #define sk_for_each_from(__sk) \
  846. hlist_for_each_entry_from(__sk, sk_node)
  847. #define sk_nulls_for_each_from(__sk, node) \
  848. if (__sk && ({ node = &(__sk)->sk_nulls_node; 1; })) \
  849. hlist_nulls_for_each_entry_from(__sk, node, sk_nulls_node)
  850. #define sk_for_each_safe(__sk, tmp, list) \
  851. hlist_for_each_entry_safe(__sk, tmp, list, sk_node)
  852. #define sk_for_each_bound(__sk, list) \
  853. hlist_for_each_entry(__sk, list, sk_bind_node)
  854. #define sk_for_each_bound_bhash2(__sk, list) \
  855. hlist_for_each_entry(__sk, list, sk_bind2_node)
  856. /**
  857. * sk_for_each_entry_offset_rcu - iterate over a list at a given struct offset
  858. * @tpos: the type * to use as a loop cursor.
  859. * @pos: the &struct hlist_node to use as a loop cursor.
  860. * @head: the head for your list.
  861. * @offset: offset of hlist_node within the struct.
  862. *
  863. */
  864. #define sk_for_each_entry_offset_rcu(tpos, pos, head, offset) \
  865. for (pos = rcu_dereference(hlist_first_rcu(head)); \
  866. pos != NULL && \
  867. ({ tpos = (typeof(*tpos) *)((void *)pos - offset); 1;}); \
  868. pos = rcu_dereference(hlist_next_rcu(pos)))
  869. static inline struct user_namespace *sk_user_ns(const struct sock *sk)
  870. {
  871. /* Careful only use this in a context where these parameters
  872. * can not change and must all be valid, such as recvmsg from
  873. * userspace.
  874. */
  875. return sk->sk_socket->file->f_cred->user_ns;
  876. }
  877. /* Sock flags */
  878. enum sock_flags {
  879. SOCK_DEAD,
  880. SOCK_DONE,
  881. SOCK_URGINLINE,
  882. SOCK_KEEPOPEN,
  883. SOCK_LINGER,
  884. SOCK_DESTROY,
  885. SOCK_BROADCAST,
  886. SOCK_TIMESTAMP,
  887. SOCK_ZAPPED,
  888. SOCK_USE_WRITE_QUEUE, /* whether to call sk->sk_write_space in sock_wfree */
  889. SOCK_DBG, /* %SO_DEBUG setting */
  890. SOCK_RCVTSTAMP, /* %SO_TIMESTAMP setting */
  891. SOCK_RCVTSTAMPNS, /* %SO_TIMESTAMPNS setting */
  892. SOCK_LOCALROUTE, /* route locally only, %SO_DONTROUTE setting */
  893. SOCK_MEMALLOC, /* VM depends on this socket for swapping */
  894. SOCK_TIMESTAMPING_RX_SOFTWARE, /* %SOF_TIMESTAMPING_RX_SOFTWARE */
  895. SOCK_FASYNC, /* fasync() active */
  896. SOCK_RXQ_OVFL,
  897. SOCK_ZEROCOPY, /* buffers from userspace */
  898. SOCK_WIFI_STATUS, /* push wifi status to userspace */
  899. SOCK_NOFCS, /* Tell NIC not to do the Ethernet FCS.
  900. * Will use last 4 bytes of packet sent from
  901. * user-space instead.
  902. */
  903. SOCK_FILTER_LOCKED, /* Filter cannot be changed anymore */
  904. SOCK_SELECT_ERR_QUEUE, /* Wake select on error queue */
  905. SOCK_RCU_FREE, /* wait rcu grace period in sk_destruct() */
  906. SOCK_TXTIME,
  907. SOCK_XDP, /* XDP is attached */
  908. SOCK_TSTAMP_NEW, /* Indicates 64 bit timestamps always */
  909. SOCK_RCVMARK, /* Receive SO_MARK ancillary data with packet */
  910. };
  911. #define SK_FLAGS_TIMESTAMP ((1UL << SOCK_TIMESTAMP) | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE))
  912. static inline void sock_copy_flags(struct sock *nsk, const struct sock *osk)
  913. {
  914. nsk->sk_flags = osk->sk_flags;
  915. }
  916. static inline void sock_set_flag(struct sock *sk, enum sock_flags flag)
  917. {
  918. __set_bit(flag, &sk->sk_flags);
  919. }
  920. static inline void sock_reset_flag(struct sock *sk, enum sock_flags flag)
  921. {
  922. __clear_bit(flag, &sk->sk_flags);
  923. }
  924. static inline void sock_valbool_flag(struct sock *sk, enum sock_flags bit,
  925. int valbool)
  926. {
  927. if (valbool)
  928. sock_set_flag(sk, bit);
  929. else
  930. sock_reset_flag(sk, bit);
  931. }
  932. static inline bool sock_flag(const struct sock *sk, enum sock_flags flag)
  933. {
  934. return test_bit(flag, &sk->sk_flags);
  935. }
  936. #ifdef CONFIG_NET
  937. DECLARE_STATIC_KEY_FALSE(memalloc_socks_key);
  938. static inline int sk_memalloc_socks(void)
  939. {
  940. return static_branch_unlikely(&memalloc_socks_key);
  941. }
  942. void __receive_sock(struct file *file);
  943. #else
  944. static inline int sk_memalloc_socks(void)
  945. {
  946. return 0;
  947. }
  948. static inline void __receive_sock(struct file *file)
  949. { }
  950. #endif
  951. static inline gfp_t sk_gfp_mask(const struct sock *sk, gfp_t gfp_mask)
  952. {
  953. return gfp_mask | (sk->sk_allocation & __GFP_MEMALLOC);
  954. }
  955. static inline void sk_acceptq_removed(struct sock *sk)
  956. {
  957. WRITE_ONCE(sk->sk_ack_backlog, sk->sk_ack_backlog - 1);
  958. }
  959. static inline void sk_acceptq_added(struct sock *sk)
  960. {
  961. WRITE_ONCE(sk->sk_ack_backlog, sk->sk_ack_backlog + 1);
  962. }
  963. /* Note: If you think the test should be:
  964. * return READ_ONCE(sk->sk_ack_backlog) >= READ_ONCE(sk->sk_max_ack_backlog);
  965. * Then please take a look at commit 64a146513f8f ("[NET]: Revert incorrect accept queue backlog changes.")
  966. */
  967. static inline bool sk_acceptq_is_full(const struct sock *sk)
  968. {
  969. return READ_ONCE(sk->sk_ack_backlog) > READ_ONCE(sk->sk_max_ack_backlog);
  970. }
  971. /*
  972. * Compute minimal free write space needed to queue new packets.
  973. */
  974. static inline int sk_stream_min_wspace(const struct sock *sk)
  975. {
  976. return READ_ONCE(sk->sk_wmem_queued) >> 1;
  977. }
  978. static inline int sk_stream_wspace(const struct sock *sk)
  979. {
  980. return READ_ONCE(sk->sk_sndbuf) - READ_ONCE(sk->sk_wmem_queued);
  981. }
  982. static inline void sk_wmem_queued_add(struct sock *sk, int val)
  983. {
  984. WRITE_ONCE(sk->sk_wmem_queued, sk->sk_wmem_queued + val);
  985. }
  986. static inline void sk_forward_alloc_add(struct sock *sk, int val)
  987. {
  988. /* Paired with lockless reads of sk->sk_forward_alloc */
  989. WRITE_ONCE(sk->sk_forward_alloc, sk->sk_forward_alloc + val);
  990. }
  991. void sk_stream_write_space(struct sock *sk);
  992. /* OOB backlog add */
  993. static inline void __sk_add_backlog(struct sock *sk, struct sk_buff *skb)
  994. {
  995. /* dont let skb dst not refcounted, we are going to leave rcu lock */
  996. skb_dst_force(skb);
  997. if (!sk->sk_backlog.tail)
  998. WRITE_ONCE(sk->sk_backlog.head, skb);
  999. else
  1000. sk->sk_backlog.tail->next = skb;
  1001. WRITE_ONCE(sk->sk_backlog.tail, skb);
  1002. skb->next = NULL;
  1003. }
  1004. /*
  1005. * Take into account size of receive queue and backlog queue
  1006. * Do not take into account this skb truesize,
  1007. * to allow even a single big packet to come.
  1008. */
  1009. static inline bool sk_rcvqueues_full(const struct sock *sk, unsigned int limit)
  1010. {
  1011. unsigned int qsize = sk->sk_backlog.len + atomic_read(&sk->sk_rmem_alloc);
  1012. return qsize > limit;
  1013. }
  1014. /* The per-socket spinlock must be held here. */
  1015. static inline __must_check int sk_add_backlog(struct sock *sk, struct sk_buff *skb,
  1016. unsigned int limit)
  1017. {
  1018. if (sk_rcvqueues_full(sk, limit))
  1019. return -ENOBUFS;
  1020. /*
  1021. * If the skb was allocated from pfmemalloc reserves, only
  1022. * allow SOCK_MEMALLOC sockets to use it as this socket is
  1023. * helping free memory
  1024. */
  1025. if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
  1026. return -ENOMEM;
  1027. __sk_add_backlog(sk, skb);
  1028. sk->sk_backlog.len += skb->truesize;
  1029. return 0;
  1030. }
  1031. int __sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
  1032. INDIRECT_CALLABLE_DECLARE(int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb));
  1033. INDIRECT_CALLABLE_DECLARE(int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb));
  1034. static inline int sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  1035. {
  1036. if (sk_memalloc_socks() && skb_pfmemalloc(skb))
  1037. return __sk_backlog_rcv(sk, skb);
  1038. return INDIRECT_CALL_INET(sk->sk_backlog_rcv,
  1039. tcp_v6_do_rcv,
  1040. tcp_v4_do_rcv,
  1041. sk, skb);
  1042. }
  1043. static inline void sk_incoming_cpu_update(struct sock *sk)
  1044. {
  1045. int cpu = raw_smp_processor_id();
  1046. if (unlikely(READ_ONCE(sk->sk_incoming_cpu) != cpu))
  1047. WRITE_ONCE(sk->sk_incoming_cpu, cpu);
  1048. }
  1049. static inline void sock_rps_record_flow_hash(__u32 hash)
  1050. {
  1051. #ifdef CONFIG_RPS
  1052. struct rps_sock_flow_table *sock_flow_table;
  1053. rcu_read_lock();
  1054. sock_flow_table = rcu_dereference(rps_sock_flow_table);
  1055. rps_record_sock_flow(sock_flow_table, hash);
  1056. rcu_read_unlock();
  1057. #endif
  1058. }
  1059. static inline void sock_rps_record_flow(const struct sock *sk)
  1060. {
  1061. #ifdef CONFIG_RPS
  1062. if (static_branch_unlikely(&rfs_needed)) {
  1063. /* Reading sk->sk_rxhash might incur an expensive cache line
  1064. * miss.
  1065. *
  1066. * TCP_ESTABLISHED does cover almost all states where RFS
  1067. * might be useful, and is cheaper [1] than testing :
  1068. * IPv4: inet_sk(sk)->inet_daddr
  1069. * IPv6: ipv6_addr_any(&sk->sk_v6_daddr)
  1070. * OR an additional socket flag
  1071. * [1] : sk_state and sk_prot are in the same cache line.
  1072. */
  1073. if (sk->sk_state == TCP_ESTABLISHED) {
  1074. /* This READ_ONCE() is paired with the WRITE_ONCE()
  1075. * from sock_rps_save_rxhash() and sock_rps_reset_rxhash().
  1076. */
  1077. sock_rps_record_flow_hash(READ_ONCE(sk->sk_rxhash));
  1078. }
  1079. }
  1080. #endif
  1081. }
  1082. static inline void sock_rps_save_rxhash(struct sock *sk,
  1083. const struct sk_buff *skb)
  1084. {
  1085. #ifdef CONFIG_RPS
  1086. /* The following WRITE_ONCE() is paired with the READ_ONCE()
  1087. * here, and another one in sock_rps_record_flow().
  1088. */
  1089. if (unlikely(READ_ONCE(sk->sk_rxhash) != skb->hash))
  1090. WRITE_ONCE(sk->sk_rxhash, skb->hash);
  1091. #endif
  1092. }
  1093. static inline void sock_rps_reset_rxhash(struct sock *sk)
  1094. {
  1095. #ifdef CONFIG_RPS
  1096. /* Paired with READ_ONCE() in sock_rps_record_flow() */
  1097. WRITE_ONCE(sk->sk_rxhash, 0);
  1098. #endif
  1099. }
  1100. #define sk_wait_event(__sk, __timeo, __condition, __wait) \
  1101. ({ int __rc; \
  1102. __sk->sk_wait_pending++; \
  1103. release_sock(__sk); \
  1104. __rc = __condition; \
  1105. if (!__rc) { \
  1106. *(__timeo) = wait_woken(__wait, \
  1107. TASK_INTERRUPTIBLE, \
  1108. *(__timeo)); \
  1109. } \
  1110. sched_annotate_sleep(); \
  1111. lock_sock(__sk); \
  1112. __sk->sk_wait_pending--; \
  1113. __rc = __condition; \
  1114. __rc; \
  1115. })
  1116. int sk_stream_wait_connect(struct sock *sk, long *timeo_p);
  1117. int sk_stream_wait_memory(struct sock *sk, long *timeo_p);
  1118. void sk_stream_wait_close(struct sock *sk, long timeo_p);
  1119. int sk_stream_error(struct sock *sk, int flags, int err);
  1120. void sk_stream_kill_queues(struct sock *sk);
  1121. void sk_set_memalloc(struct sock *sk);
  1122. void sk_clear_memalloc(struct sock *sk);
  1123. void __sk_flush_backlog(struct sock *sk);
  1124. static inline bool sk_flush_backlog(struct sock *sk)
  1125. {
  1126. if (unlikely(READ_ONCE(sk->sk_backlog.tail))) {
  1127. __sk_flush_backlog(sk);
  1128. return true;
  1129. }
  1130. return false;
  1131. }
  1132. int sk_wait_data(struct sock *sk, long *timeo, const struct sk_buff *skb);
  1133. struct request_sock_ops;
  1134. struct timewait_sock_ops;
  1135. struct inet_hashinfo;
  1136. struct raw_hashinfo;
  1137. struct smc_hashinfo;
  1138. struct module;
  1139. struct sk_psock;
  1140. /*
  1141. * caches using SLAB_TYPESAFE_BY_RCU should let .next pointer from nulls nodes
  1142. * un-modified. Special care is taken when initializing object to zero.
  1143. */
  1144. static inline void sk_prot_clear_nulls(struct sock *sk, int size)
  1145. {
  1146. if (offsetof(struct sock, sk_node.next) != 0)
  1147. memset(sk, 0, offsetof(struct sock, sk_node.next));
  1148. memset(&sk->sk_node.pprev, 0,
  1149. size - offsetof(struct sock, sk_node.pprev));
  1150. }
  1151. /* Networking protocol blocks we attach to sockets.
  1152. * socket layer -> transport layer interface
  1153. */
  1154. struct proto {
  1155. void (*close)(struct sock *sk,
  1156. long timeout);
  1157. int (*pre_connect)(struct sock *sk,
  1158. struct sockaddr *uaddr,
  1159. int addr_len);
  1160. int (*connect)(struct sock *sk,
  1161. struct sockaddr *uaddr,
  1162. int addr_len);
  1163. int (*disconnect)(struct sock *sk, int flags);
  1164. struct sock * (*accept)(struct sock *sk, int flags, int *err,
  1165. bool kern);
  1166. int (*ioctl)(struct sock *sk, int cmd,
  1167. unsigned long arg);
  1168. int (*init)(struct sock *sk);
  1169. void (*destroy)(struct sock *sk);
  1170. void (*shutdown)(struct sock *sk, int how);
  1171. int (*setsockopt)(struct sock *sk, int level,
  1172. int optname, sockptr_t optval,
  1173. unsigned int optlen);
  1174. int (*getsockopt)(struct sock *sk, int level,
  1175. int optname, char __user *optval,
  1176. int __user *option);
  1177. void (*keepalive)(struct sock *sk, int valbool);
  1178. #ifdef CONFIG_COMPAT
  1179. int (*compat_ioctl)(struct sock *sk,
  1180. unsigned int cmd, unsigned long arg);
  1181. #endif
  1182. int (*sendmsg)(struct sock *sk, struct msghdr *msg,
  1183. size_t len);
  1184. int (*recvmsg)(struct sock *sk, struct msghdr *msg,
  1185. size_t len, int flags, int *addr_len);
  1186. int (*sendpage)(struct sock *sk, struct page *page,
  1187. int offset, size_t size, int flags);
  1188. int (*bind)(struct sock *sk,
  1189. struct sockaddr *addr, int addr_len);
  1190. int (*bind_add)(struct sock *sk,
  1191. struct sockaddr *addr, int addr_len);
  1192. int (*backlog_rcv) (struct sock *sk,
  1193. struct sk_buff *skb);
  1194. bool (*bpf_bypass_getsockopt)(int level,
  1195. int optname);
  1196. void (*release_cb)(struct sock *sk);
  1197. /* Keeping track of sk's, looking them up, and port selection methods. */
  1198. int (*hash)(struct sock *sk);
  1199. void (*unhash)(struct sock *sk);
  1200. void (*rehash)(struct sock *sk);
  1201. int (*get_port)(struct sock *sk, unsigned short snum);
  1202. void (*put_port)(struct sock *sk);
  1203. #ifdef CONFIG_BPF_SYSCALL
  1204. int (*psock_update_sk_prot)(struct sock *sk,
  1205. struct sk_psock *psock,
  1206. bool restore);
  1207. #endif
  1208. /* Keeping track of sockets in use */
  1209. #ifdef CONFIG_PROC_FS
  1210. unsigned int inuse_idx;
  1211. #endif
  1212. #if IS_ENABLED(CONFIG_MPTCP)
  1213. int (*forward_alloc_get)(const struct sock *sk);
  1214. #endif
  1215. bool (*stream_memory_free)(const struct sock *sk, int wake);
  1216. bool (*sock_is_readable)(struct sock *sk);
  1217. /* Memory pressure */
  1218. void (*enter_memory_pressure)(struct sock *sk);
  1219. void (*leave_memory_pressure)(struct sock *sk);
  1220. atomic_long_t *memory_allocated; /* Current allocated memory. */
  1221. int __percpu *per_cpu_fw_alloc;
  1222. struct percpu_counter *sockets_allocated; /* Current number of sockets. */
  1223. /*
  1224. * Pressure flag: try to collapse.
  1225. * Technical note: it is used by multiple contexts non atomically.
  1226. * Make sure to use READ_ONCE()/WRITE_ONCE() for all reads/writes.
  1227. * All the __sk_mem_schedule() is of this nature: accounting
  1228. * is strict, actions are advisory and have some latency.
  1229. */
  1230. unsigned long *memory_pressure;
  1231. long *sysctl_mem;
  1232. int *sysctl_wmem;
  1233. int *sysctl_rmem;
  1234. u32 sysctl_wmem_offset;
  1235. u32 sysctl_rmem_offset;
  1236. int max_header;
  1237. bool no_autobind;
  1238. struct kmem_cache *slab;
  1239. unsigned int obj_size;
  1240. slab_flags_t slab_flags;
  1241. unsigned int useroffset; /* Usercopy region offset */
  1242. unsigned int usersize; /* Usercopy region size */
  1243. unsigned int __percpu *orphan_count;
  1244. struct request_sock_ops *rsk_prot;
  1245. struct timewait_sock_ops *twsk_prot;
  1246. union {
  1247. struct inet_hashinfo *hashinfo;
  1248. struct udp_table *udp_table;
  1249. struct raw_hashinfo *raw_hash;
  1250. struct smc_hashinfo *smc_hash;
  1251. } h;
  1252. struct module *owner;
  1253. char name[32];
  1254. struct list_head node;
  1255. #ifdef SOCK_REFCNT_DEBUG
  1256. atomic_t socks;
  1257. #endif
  1258. int (*diag_destroy)(struct sock *sk, int err);
  1259. } __randomize_layout;
  1260. int proto_register(struct proto *prot, int alloc_slab);
  1261. void proto_unregister(struct proto *prot);
  1262. int sock_load_diag_module(int family, int protocol);
  1263. #ifdef SOCK_REFCNT_DEBUG
  1264. static inline void sk_refcnt_debug_inc(struct sock *sk)
  1265. {
  1266. atomic_inc(&sk->sk_prot->socks);
  1267. }
  1268. static inline void sk_refcnt_debug_dec(struct sock *sk)
  1269. {
  1270. atomic_dec(&sk->sk_prot->socks);
  1271. printk(KERN_DEBUG "%s socket %p released, %d are still alive\n",
  1272. sk->sk_prot->name, sk, atomic_read(&sk->sk_prot->socks));
  1273. }
  1274. static inline void sk_refcnt_debug_release(const struct sock *sk)
  1275. {
  1276. if (refcount_read(&sk->sk_refcnt) != 1)
  1277. printk(KERN_DEBUG "Destruction of the %s socket %p delayed, refcnt=%d\n",
  1278. sk->sk_prot->name, sk, refcount_read(&sk->sk_refcnt));
  1279. }
  1280. #else /* SOCK_REFCNT_DEBUG */
  1281. #define sk_refcnt_debug_inc(sk) do { } while (0)
  1282. #define sk_refcnt_debug_dec(sk) do { } while (0)
  1283. #define sk_refcnt_debug_release(sk) do { } while (0)
  1284. #endif /* SOCK_REFCNT_DEBUG */
  1285. INDIRECT_CALLABLE_DECLARE(bool tcp_stream_memory_free(const struct sock *sk, int wake));
  1286. static inline int sk_forward_alloc_get(const struct sock *sk)
  1287. {
  1288. #if IS_ENABLED(CONFIG_MPTCP)
  1289. if (sk->sk_prot->forward_alloc_get)
  1290. return sk->sk_prot->forward_alloc_get(sk);
  1291. #endif
  1292. return READ_ONCE(sk->sk_forward_alloc);
  1293. }
  1294. static inline bool __sk_stream_memory_free(const struct sock *sk, int wake)
  1295. {
  1296. if (READ_ONCE(sk->sk_wmem_queued) >= READ_ONCE(sk->sk_sndbuf))
  1297. return false;
  1298. return sk->sk_prot->stream_memory_free ?
  1299. INDIRECT_CALL_INET_1(sk->sk_prot->stream_memory_free,
  1300. tcp_stream_memory_free, sk, wake) : true;
  1301. }
  1302. static inline bool sk_stream_memory_free(const struct sock *sk)
  1303. {
  1304. return __sk_stream_memory_free(sk, 0);
  1305. }
  1306. static inline bool __sk_stream_is_writeable(const struct sock *sk, int wake)
  1307. {
  1308. return sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) &&
  1309. __sk_stream_memory_free(sk, wake);
  1310. }
  1311. static inline bool sk_stream_is_writeable(const struct sock *sk)
  1312. {
  1313. return __sk_stream_is_writeable(sk, 0);
  1314. }
  1315. static inline int sk_under_cgroup_hierarchy(struct sock *sk,
  1316. struct cgroup *ancestor)
  1317. {
  1318. #ifdef CONFIG_SOCK_CGROUP_DATA
  1319. return cgroup_is_descendant(sock_cgroup_ptr(&sk->sk_cgrp_data),
  1320. ancestor);
  1321. #else
  1322. return -ENOTSUPP;
  1323. #endif
  1324. }
  1325. static inline bool sk_has_memory_pressure(const struct sock *sk)
  1326. {
  1327. return sk->sk_prot->memory_pressure != NULL;
  1328. }
  1329. static inline bool sk_under_global_memory_pressure(const struct sock *sk)
  1330. {
  1331. return sk->sk_prot->memory_pressure &&
  1332. !!READ_ONCE(*sk->sk_prot->memory_pressure);
  1333. }
  1334. static inline bool sk_under_memory_pressure(const struct sock *sk)
  1335. {
  1336. if (!sk->sk_prot->memory_pressure)
  1337. return false;
  1338. if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
  1339. mem_cgroup_under_socket_pressure(sk->sk_memcg))
  1340. return true;
  1341. return !!READ_ONCE(*sk->sk_prot->memory_pressure);
  1342. }
  1343. static inline long
  1344. proto_memory_allocated(const struct proto *prot)
  1345. {
  1346. return max(0L, atomic_long_read(prot->memory_allocated));
  1347. }
  1348. static inline long
  1349. sk_memory_allocated(const struct sock *sk)
  1350. {
  1351. return proto_memory_allocated(sk->sk_prot);
  1352. }
  1353. /* 1 MB per cpu, in page units */
  1354. #define SK_MEMORY_PCPU_RESERVE (1 << (20 - PAGE_SHIFT))
  1355. static inline void
  1356. sk_memory_allocated_add(struct sock *sk, int amt)
  1357. {
  1358. int local_reserve;
  1359. preempt_disable();
  1360. local_reserve = __this_cpu_add_return(*sk->sk_prot->per_cpu_fw_alloc, amt);
  1361. if (local_reserve >= SK_MEMORY_PCPU_RESERVE) {
  1362. __this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve);
  1363. atomic_long_add(local_reserve, sk->sk_prot->memory_allocated);
  1364. }
  1365. preempt_enable();
  1366. }
  1367. static inline void
  1368. sk_memory_allocated_sub(struct sock *sk, int amt)
  1369. {
  1370. int local_reserve;
  1371. preempt_disable();
  1372. local_reserve = __this_cpu_sub_return(*sk->sk_prot->per_cpu_fw_alloc, amt);
  1373. if (local_reserve <= -SK_MEMORY_PCPU_RESERVE) {
  1374. __this_cpu_sub(*sk->sk_prot->per_cpu_fw_alloc, local_reserve);
  1375. atomic_long_add(local_reserve, sk->sk_prot->memory_allocated);
  1376. }
  1377. preempt_enable();
  1378. }
  1379. #define SK_ALLOC_PERCPU_COUNTER_BATCH 16
  1380. static inline void sk_sockets_allocated_dec(struct sock *sk)
  1381. {
  1382. percpu_counter_add_batch(sk->sk_prot->sockets_allocated, -1,
  1383. SK_ALLOC_PERCPU_COUNTER_BATCH);
  1384. }
  1385. static inline void sk_sockets_allocated_inc(struct sock *sk)
  1386. {
  1387. percpu_counter_add_batch(sk->sk_prot->sockets_allocated, 1,
  1388. SK_ALLOC_PERCPU_COUNTER_BATCH);
  1389. }
  1390. static inline u64
  1391. sk_sockets_allocated_read_positive(struct sock *sk)
  1392. {
  1393. return percpu_counter_read_positive(sk->sk_prot->sockets_allocated);
  1394. }
  1395. static inline int
  1396. proto_sockets_allocated_sum_positive(struct proto *prot)
  1397. {
  1398. return percpu_counter_sum_positive(prot->sockets_allocated);
  1399. }
  1400. static inline bool
  1401. proto_memory_pressure(struct proto *prot)
  1402. {
  1403. if (!prot->memory_pressure)
  1404. return false;
  1405. return !!READ_ONCE(*prot->memory_pressure);
  1406. }
  1407. #ifdef CONFIG_PROC_FS
  1408. #define PROTO_INUSE_NR 64 /* should be enough for the first time */
  1409. struct prot_inuse {
  1410. int all;
  1411. int val[PROTO_INUSE_NR];
  1412. };
  1413. static inline void sock_prot_inuse_add(const struct net *net,
  1414. const struct proto *prot, int val)
  1415. {
  1416. this_cpu_add(net->core.prot_inuse->val[prot->inuse_idx], val);
  1417. }
  1418. static inline void sock_inuse_add(const struct net *net, int val)
  1419. {
  1420. this_cpu_add(net->core.prot_inuse->all, val);
  1421. }
  1422. int sock_prot_inuse_get(struct net *net, struct proto *proto);
  1423. int sock_inuse_get(struct net *net);
  1424. #else
  1425. static inline void sock_prot_inuse_add(const struct net *net,
  1426. const struct proto *prot, int val)
  1427. {
  1428. }
  1429. static inline void sock_inuse_add(const struct net *net, int val)
  1430. {
  1431. }
  1432. #endif
  1433. /* With per-bucket locks this operation is not-atomic, so that
  1434. * this version is not worse.
  1435. */
  1436. static inline int __sk_prot_rehash(struct sock *sk)
  1437. {
  1438. sk->sk_prot->unhash(sk);
  1439. return sk->sk_prot->hash(sk);
  1440. }
  1441. /* About 10 seconds */
  1442. #define SOCK_DESTROY_TIME (10*HZ)
  1443. /* Sockets 0-1023 can't be bound to unless you are superuser */
  1444. #define PROT_SOCK 1024
  1445. #define SHUTDOWN_MASK 3
  1446. #define RCV_SHUTDOWN 1
  1447. #define SEND_SHUTDOWN 2
  1448. #define SOCK_BINDADDR_LOCK 4
  1449. #define SOCK_BINDPORT_LOCK 8
  1450. struct socket_alloc {
  1451. struct socket socket;
  1452. struct inode vfs_inode;
  1453. };
  1454. static inline struct socket *SOCKET_I(struct inode *inode)
  1455. {
  1456. return &container_of(inode, struct socket_alloc, vfs_inode)->socket;
  1457. }
  1458. static inline struct inode *SOCK_INODE(struct socket *socket)
  1459. {
  1460. return &container_of(socket, struct socket_alloc, socket)->vfs_inode;
  1461. }
  1462. /*
  1463. * Functions for memory accounting
  1464. */
  1465. int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind);
  1466. int __sk_mem_schedule(struct sock *sk, int size, int kind);
  1467. void __sk_mem_reduce_allocated(struct sock *sk, int amount);
  1468. void __sk_mem_reclaim(struct sock *sk, int amount);
  1469. #define SK_MEM_SEND 0
  1470. #define SK_MEM_RECV 1
  1471. /* sysctl_mem values are in pages */
  1472. static inline long sk_prot_mem_limits(const struct sock *sk, int index)
  1473. {
  1474. return READ_ONCE(sk->sk_prot->sysctl_mem[index]);
  1475. }
  1476. static inline int sk_mem_pages(int amt)
  1477. {
  1478. return (amt + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1479. }
  1480. static inline bool sk_has_account(struct sock *sk)
  1481. {
  1482. /* return true if protocol supports memory accounting */
  1483. return !!sk->sk_prot->memory_allocated;
  1484. }
  1485. static inline bool sk_wmem_schedule(struct sock *sk, int size)
  1486. {
  1487. int delta;
  1488. if (!sk_has_account(sk))
  1489. return true;
  1490. delta = size - sk->sk_forward_alloc;
  1491. return delta <= 0 || __sk_mem_schedule(sk, delta, SK_MEM_SEND);
  1492. }
  1493. static inline bool
  1494. sk_rmem_schedule(struct sock *sk, struct sk_buff *skb, int size)
  1495. {
  1496. int delta;
  1497. if (!sk_has_account(sk))
  1498. return true;
  1499. delta = size - sk->sk_forward_alloc;
  1500. return delta <= 0 || __sk_mem_schedule(sk, delta, SK_MEM_RECV) ||
  1501. skb_pfmemalloc(skb);
  1502. }
  1503. static inline int sk_unused_reserved_mem(const struct sock *sk)
  1504. {
  1505. int unused_mem;
  1506. if (likely(!sk->sk_reserved_mem))
  1507. return 0;
  1508. unused_mem = sk->sk_reserved_mem - sk->sk_wmem_queued -
  1509. atomic_read(&sk->sk_rmem_alloc);
  1510. return unused_mem > 0 ? unused_mem : 0;
  1511. }
  1512. static inline void sk_mem_reclaim(struct sock *sk)
  1513. {
  1514. int reclaimable;
  1515. if (!sk_has_account(sk))
  1516. return;
  1517. reclaimable = sk->sk_forward_alloc - sk_unused_reserved_mem(sk);
  1518. if (reclaimable >= (int)PAGE_SIZE)
  1519. __sk_mem_reclaim(sk, reclaimable);
  1520. }
  1521. static inline void sk_mem_reclaim_final(struct sock *sk)
  1522. {
  1523. sk->sk_reserved_mem = 0;
  1524. sk_mem_reclaim(sk);
  1525. }
  1526. static inline void sk_mem_charge(struct sock *sk, int size)
  1527. {
  1528. if (!sk_has_account(sk))
  1529. return;
  1530. sk_forward_alloc_add(sk, -size);
  1531. }
  1532. static inline void sk_mem_uncharge(struct sock *sk, int size)
  1533. {
  1534. if (!sk_has_account(sk))
  1535. return;
  1536. sk_forward_alloc_add(sk, size);
  1537. sk_mem_reclaim(sk);
  1538. }
  1539. /*
  1540. * Macro so as to not evaluate some arguments when
  1541. * lockdep is not enabled.
  1542. *
  1543. * Mark both the sk_lock and the sk_lock.slock as a
  1544. * per-address-family lock class.
  1545. */
  1546. #define sock_lock_init_class_and_name(sk, sname, skey, name, key) \
  1547. do { \
  1548. sk->sk_lock.owned = 0; \
  1549. init_waitqueue_head(&sk->sk_lock.wq); \
  1550. spin_lock_init(&(sk)->sk_lock.slock); \
  1551. debug_check_no_locks_freed((void *)&(sk)->sk_lock, \
  1552. sizeof((sk)->sk_lock)); \
  1553. lockdep_set_class_and_name(&(sk)->sk_lock.slock, \
  1554. (skey), (sname)); \
  1555. lockdep_init_map(&(sk)->sk_lock.dep_map, (name), (key), 0); \
  1556. } while (0)
  1557. static inline bool lockdep_sock_is_held(const struct sock *sk)
  1558. {
  1559. return lockdep_is_held(&sk->sk_lock) ||
  1560. lockdep_is_held(&sk->sk_lock.slock);
  1561. }
  1562. void lock_sock_nested(struct sock *sk, int subclass);
  1563. static inline void lock_sock(struct sock *sk)
  1564. {
  1565. lock_sock_nested(sk, 0);
  1566. }
  1567. void __lock_sock(struct sock *sk);
  1568. void __release_sock(struct sock *sk);
  1569. void release_sock(struct sock *sk);
  1570. /* BH context may only use the following locking interface. */
  1571. #define bh_lock_sock(__sk) spin_lock(&((__sk)->sk_lock.slock))
  1572. #define bh_lock_sock_nested(__sk) \
  1573. spin_lock_nested(&((__sk)->sk_lock.slock), \
  1574. SINGLE_DEPTH_NESTING)
  1575. #define bh_unlock_sock(__sk) spin_unlock(&((__sk)->sk_lock.slock))
  1576. bool __lock_sock_fast(struct sock *sk) __acquires(&sk->sk_lock.slock);
  1577. /**
  1578. * lock_sock_fast - fast version of lock_sock
  1579. * @sk: socket
  1580. *
  1581. * This version should be used for very small section, where process wont block
  1582. * return false if fast path is taken:
  1583. *
  1584. * sk_lock.slock locked, owned = 0, BH disabled
  1585. *
  1586. * return true if slow path is taken:
  1587. *
  1588. * sk_lock.slock unlocked, owned = 1, BH enabled
  1589. */
  1590. static inline bool lock_sock_fast(struct sock *sk)
  1591. {
  1592. /* The sk_lock has mutex_lock() semantics here. */
  1593. mutex_acquire(&sk->sk_lock.dep_map, 0, 0, _RET_IP_);
  1594. return __lock_sock_fast(sk);
  1595. }
  1596. /* fast socket lock variant for caller already holding a [different] socket lock */
  1597. static inline bool lock_sock_fast_nested(struct sock *sk)
  1598. {
  1599. mutex_acquire(&sk->sk_lock.dep_map, SINGLE_DEPTH_NESTING, 0, _RET_IP_);
  1600. return __lock_sock_fast(sk);
  1601. }
  1602. /**
  1603. * unlock_sock_fast - complement of lock_sock_fast
  1604. * @sk: socket
  1605. * @slow: slow mode
  1606. *
  1607. * fast unlock socket for user context.
  1608. * If slow mode is on, we call regular release_sock()
  1609. */
  1610. static inline void unlock_sock_fast(struct sock *sk, bool slow)
  1611. __releases(&sk->sk_lock.slock)
  1612. {
  1613. if (slow) {
  1614. release_sock(sk);
  1615. __release(&sk->sk_lock.slock);
  1616. } else {
  1617. mutex_release(&sk->sk_lock.dep_map, _RET_IP_);
  1618. spin_unlock_bh(&sk->sk_lock.slock);
  1619. }
  1620. }
  1621. void sockopt_lock_sock(struct sock *sk);
  1622. void sockopt_release_sock(struct sock *sk);
  1623. bool sockopt_ns_capable(struct user_namespace *ns, int cap);
  1624. bool sockopt_capable(int cap);
  1625. /* Used by processes to "lock" a socket state, so that
  1626. * interrupts and bottom half handlers won't change it
  1627. * from under us. It essentially blocks any incoming
  1628. * packets, so that we won't get any new data or any
  1629. * packets that change the state of the socket.
  1630. *
  1631. * While locked, BH processing will add new packets to
  1632. * the backlog queue. This queue is processed by the
  1633. * owner of the socket lock right before it is released.
  1634. *
  1635. * Since ~2.3.5 it is also exclusive sleep lock serializing
  1636. * accesses from user process context.
  1637. */
  1638. static inline void sock_owned_by_me(const struct sock *sk)
  1639. {
  1640. #ifdef CONFIG_LOCKDEP
  1641. WARN_ON_ONCE(!lockdep_sock_is_held(sk) && debug_locks);
  1642. #endif
  1643. }
  1644. static inline bool sock_owned_by_user(const struct sock *sk)
  1645. {
  1646. sock_owned_by_me(sk);
  1647. return sk->sk_lock.owned;
  1648. }
  1649. static inline bool sock_owned_by_user_nocheck(const struct sock *sk)
  1650. {
  1651. return sk->sk_lock.owned;
  1652. }
  1653. static inline void sock_release_ownership(struct sock *sk)
  1654. {
  1655. if (sock_owned_by_user_nocheck(sk)) {
  1656. sk->sk_lock.owned = 0;
  1657. /* The sk_lock has mutex_unlock() semantics: */
  1658. mutex_release(&sk->sk_lock.dep_map, _RET_IP_);
  1659. }
  1660. }
  1661. /* no reclassification while locks are held */
  1662. static inline bool sock_allow_reclassification(const struct sock *csk)
  1663. {
  1664. struct sock *sk = (struct sock *)csk;
  1665. return !sock_owned_by_user_nocheck(sk) &&
  1666. !spin_is_locked(&sk->sk_lock.slock);
  1667. }
  1668. struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
  1669. struct proto *prot, int kern);
  1670. void sk_free(struct sock *sk);
  1671. void sk_destruct(struct sock *sk);
  1672. struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority);
  1673. void sk_free_unlock_clone(struct sock *sk);
  1674. struct sk_buff *sock_wmalloc(struct sock *sk, unsigned long size, int force,
  1675. gfp_t priority);
  1676. void __sock_wfree(struct sk_buff *skb);
  1677. void sock_wfree(struct sk_buff *skb);
  1678. struct sk_buff *sock_omalloc(struct sock *sk, unsigned long size,
  1679. gfp_t priority);
  1680. void skb_orphan_partial(struct sk_buff *skb);
  1681. void sock_rfree(struct sk_buff *skb);
  1682. void sock_efree(struct sk_buff *skb);
  1683. #ifdef CONFIG_INET
  1684. void sock_edemux(struct sk_buff *skb);
  1685. void sock_pfree(struct sk_buff *skb);
  1686. #else
  1687. #define sock_edemux sock_efree
  1688. #endif
  1689. int sk_setsockopt(struct sock *sk, int level, int optname,
  1690. sockptr_t optval, unsigned int optlen);
  1691. int sock_setsockopt(struct socket *sock, int level, int op,
  1692. sockptr_t optval, unsigned int optlen);
  1693. int sk_getsockopt(struct sock *sk, int level, int optname,
  1694. sockptr_t optval, sockptr_t optlen);
  1695. int sock_getsockopt(struct socket *sock, int level, int op,
  1696. char __user *optval, int __user *optlen);
  1697. int sock_gettstamp(struct socket *sock, void __user *userstamp,
  1698. bool timeval, bool time32);
  1699. struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len,
  1700. unsigned long data_len, int noblock,
  1701. int *errcode, int max_page_order);
  1702. static inline struct sk_buff *sock_alloc_send_skb(struct sock *sk,
  1703. unsigned long size,
  1704. int noblock, int *errcode)
  1705. {
  1706. return sock_alloc_send_pskb(sk, size, 0, noblock, errcode, 0);
  1707. }
  1708. void *sock_kmalloc(struct sock *sk, int size, gfp_t priority);
  1709. void sock_kfree_s(struct sock *sk, void *mem, int size);
  1710. void sock_kzfree_s(struct sock *sk, void *mem, int size);
  1711. void sk_send_sigurg(struct sock *sk);
  1712. static inline void sock_replace_proto(struct sock *sk, struct proto *proto)
  1713. {
  1714. if (sk->sk_socket)
  1715. clear_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags);
  1716. WRITE_ONCE(sk->sk_prot, proto);
  1717. }
  1718. struct sockcm_cookie {
  1719. u64 transmit_time;
  1720. u32 mark;
  1721. u16 tsflags;
  1722. };
  1723. static inline void sockcm_init(struct sockcm_cookie *sockc,
  1724. const struct sock *sk)
  1725. {
  1726. *sockc = (struct sockcm_cookie) { .tsflags = sk->sk_tsflags };
  1727. }
  1728. int __sock_cmsg_send(struct sock *sk, struct msghdr *msg, struct cmsghdr *cmsg,
  1729. struct sockcm_cookie *sockc);
  1730. int sock_cmsg_send(struct sock *sk, struct msghdr *msg,
  1731. struct sockcm_cookie *sockc);
  1732. /*
  1733. * Functions to fill in entries in struct proto_ops when a protocol
  1734. * does not implement a particular function.
  1735. */
  1736. int sock_no_bind(struct socket *, struct sockaddr *, int);
  1737. int sock_no_connect(struct socket *, struct sockaddr *, int, int);
  1738. int sock_no_socketpair(struct socket *, struct socket *);
  1739. int sock_no_accept(struct socket *, struct socket *, int, bool);
  1740. int sock_no_getname(struct socket *, struct sockaddr *, int);
  1741. int sock_no_ioctl(struct socket *, unsigned int, unsigned long);
  1742. int sock_no_listen(struct socket *, int);
  1743. int sock_no_shutdown(struct socket *, int);
  1744. int sock_no_sendmsg(struct socket *, struct msghdr *, size_t);
  1745. int sock_no_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t len);
  1746. int sock_no_recvmsg(struct socket *, struct msghdr *, size_t, int);
  1747. int sock_no_mmap(struct file *file, struct socket *sock,
  1748. struct vm_area_struct *vma);
  1749. ssize_t sock_no_sendpage(struct socket *sock, struct page *page, int offset,
  1750. size_t size, int flags);
  1751. ssize_t sock_no_sendpage_locked(struct sock *sk, struct page *page,
  1752. int offset, size_t size, int flags);
  1753. /*
  1754. * Functions to fill in entries in struct proto_ops when a protocol
  1755. * uses the inet style.
  1756. */
  1757. int sock_common_getsockopt(struct socket *sock, int level, int optname,
  1758. char __user *optval, int __user *optlen);
  1759. int sock_common_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  1760. int flags);
  1761. int sock_common_setsockopt(struct socket *sock, int level, int optname,
  1762. sockptr_t optval, unsigned int optlen);
  1763. void sk_common_release(struct sock *sk);
  1764. /*
  1765. * Default socket callbacks and setup code
  1766. */
  1767. /* Initialise core socket variables using an explicit uid. */
  1768. void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid);
  1769. /* Initialise core socket variables.
  1770. * Assumes struct socket *sock is embedded in a struct socket_alloc.
  1771. */
  1772. void sock_init_data(struct socket *sock, struct sock *sk);
  1773. /*
  1774. * Socket reference counting postulates.
  1775. *
  1776. * * Each user of socket SHOULD hold a reference count.
  1777. * * Each access point to socket (an hash table bucket, reference from a list,
  1778. * running timer, skb in flight MUST hold a reference count.
  1779. * * When reference count hits 0, it means it will never increase back.
  1780. * * When reference count hits 0, it means that no references from
  1781. * outside exist to this socket and current process on current CPU
  1782. * is last user and may/should destroy this socket.
  1783. * * sk_free is called from any context: process, BH, IRQ. When
  1784. * it is called, socket has no references from outside -> sk_free
  1785. * may release descendant resources allocated by the socket, but
  1786. * to the time when it is called, socket is NOT referenced by any
  1787. * hash tables, lists etc.
  1788. * * Packets, delivered from outside (from network or from another process)
  1789. * and enqueued on receive/error queues SHOULD NOT grab reference count,
  1790. * when they sit in queue. Otherwise, packets will leak to hole, when
  1791. * socket is looked up by one cpu and unhasing is made by another CPU.
  1792. * It is true for udp/raw, netlink (leak to receive and error queues), tcp
  1793. * (leak to backlog). Packet socket does all the processing inside
  1794. * BR_NETPROTO_LOCK, so that it has not this race condition. UNIX sockets
  1795. * use separate SMP lock, so that they are prone too.
  1796. */
  1797. /* Ungrab socket and destroy it, if it was the last reference. */
  1798. static inline void sock_put(struct sock *sk)
  1799. {
  1800. if (refcount_dec_and_test(&sk->sk_refcnt))
  1801. sk_free(sk);
  1802. }
  1803. /* Generic version of sock_put(), dealing with all sockets
  1804. * (TCP_TIMEWAIT, TCP_NEW_SYN_RECV, ESTABLISHED...)
  1805. */
  1806. void sock_gen_put(struct sock *sk);
  1807. int __sk_receive_skb(struct sock *sk, struct sk_buff *skb, const int nested,
  1808. unsigned int trim_cap, bool refcounted);
  1809. static inline int sk_receive_skb(struct sock *sk, struct sk_buff *skb,
  1810. const int nested)
  1811. {
  1812. return __sk_receive_skb(sk, skb, nested, 1, true);
  1813. }
  1814. static inline void sk_tx_queue_set(struct sock *sk, int tx_queue)
  1815. {
  1816. /* sk_tx_queue_mapping accept only upto a 16-bit value */
  1817. if (WARN_ON_ONCE((unsigned short)tx_queue >= USHRT_MAX))
  1818. return;
  1819. /* Paired with READ_ONCE() in sk_tx_queue_get() and
  1820. * other WRITE_ONCE() because socket lock might be not held.
  1821. */
  1822. WRITE_ONCE(sk->sk_tx_queue_mapping, tx_queue);
  1823. }
  1824. #define NO_QUEUE_MAPPING USHRT_MAX
  1825. static inline void sk_tx_queue_clear(struct sock *sk)
  1826. {
  1827. /* Paired with READ_ONCE() in sk_tx_queue_get() and
  1828. * other WRITE_ONCE() because socket lock might be not held.
  1829. */
  1830. WRITE_ONCE(sk->sk_tx_queue_mapping, NO_QUEUE_MAPPING);
  1831. }
  1832. static inline int sk_tx_queue_get(const struct sock *sk)
  1833. {
  1834. if (sk) {
  1835. /* Paired with WRITE_ONCE() in sk_tx_queue_clear()
  1836. * and sk_tx_queue_set().
  1837. */
  1838. int val = READ_ONCE(sk->sk_tx_queue_mapping);
  1839. if (val != NO_QUEUE_MAPPING)
  1840. return val;
  1841. }
  1842. return -1;
  1843. }
  1844. static inline void __sk_rx_queue_set(struct sock *sk,
  1845. const struct sk_buff *skb,
  1846. bool force_set)
  1847. {
  1848. #ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
  1849. if (skb_rx_queue_recorded(skb)) {
  1850. u16 rx_queue = skb_get_rx_queue(skb);
  1851. if (force_set ||
  1852. unlikely(READ_ONCE(sk->sk_rx_queue_mapping) != rx_queue))
  1853. WRITE_ONCE(sk->sk_rx_queue_mapping, rx_queue);
  1854. }
  1855. #endif
  1856. }
  1857. static inline void sk_rx_queue_set(struct sock *sk, const struct sk_buff *skb)
  1858. {
  1859. __sk_rx_queue_set(sk, skb, true);
  1860. }
  1861. static inline void sk_rx_queue_update(struct sock *sk, const struct sk_buff *skb)
  1862. {
  1863. __sk_rx_queue_set(sk, skb, false);
  1864. }
  1865. static inline void sk_rx_queue_clear(struct sock *sk)
  1866. {
  1867. #ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
  1868. WRITE_ONCE(sk->sk_rx_queue_mapping, NO_QUEUE_MAPPING);
  1869. #endif
  1870. }
  1871. static inline int sk_rx_queue_get(const struct sock *sk)
  1872. {
  1873. #ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
  1874. if (sk) {
  1875. int res = READ_ONCE(sk->sk_rx_queue_mapping);
  1876. if (res != NO_QUEUE_MAPPING)
  1877. return res;
  1878. }
  1879. #endif
  1880. return -1;
  1881. }
  1882. static inline void sk_set_socket(struct sock *sk, struct socket *sock)
  1883. {
  1884. sk->sk_socket = sock;
  1885. }
  1886. static inline wait_queue_head_t *sk_sleep(struct sock *sk)
  1887. {
  1888. BUILD_BUG_ON(offsetof(struct socket_wq, wait) != 0);
  1889. return &rcu_dereference_raw(sk->sk_wq)->wait;
  1890. }
  1891. /* Detach socket from process context.
  1892. * Announce socket dead, detach it from wait queue and inode.
  1893. * Note that parent inode held reference count on this struct sock,
  1894. * we do not release it in this function, because protocol
  1895. * probably wants some additional cleanups or even continuing
  1896. * to work with this socket (TCP).
  1897. */
  1898. static inline void sock_orphan(struct sock *sk)
  1899. {
  1900. write_lock_bh(&sk->sk_callback_lock);
  1901. sock_set_flag(sk, SOCK_DEAD);
  1902. sk_set_socket(sk, NULL);
  1903. sk->sk_wq = NULL;
  1904. write_unlock_bh(&sk->sk_callback_lock);
  1905. }
  1906. static inline void sock_graft(struct sock *sk, struct socket *parent)
  1907. {
  1908. WARN_ON(parent->sk);
  1909. write_lock_bh(&sk->sk_callback_lock);
  1910. rcu_assign_pointer(sk->sk_wq, &parent->wq);
  1911. parent->sk = sk;
  1912. sk_set_socket(sk, parent);
  1913. sk->sk_uid = SOCK_INODE(parent)->i_uid;
  1914. security_sock_graft(sk, parent);
  1915. write_unlock_bh(&sk->sk_callback_lock);
  1916. }
  1917. kuid_t sock_i_uid(struct sock *sk);
  1918. unsigned long __sock_i_ino(struct sock *sk);
  1919. unsigned long sock_i_ino(struct sock *sk);
  1920. static inline kuid_t sock_net_uid(const struct net *net, const struct sock *sk)
  1921. {
  1922. return sk ? sk->sk_uid : make_kuid(net->user_ns, 0);
  1923. }
  1924. static inline u32 net_tx_rndhash(void)
  1925. {
  1926. u32 v = get_random_u32();
  1927. return v ?: 1;
  1928. }
  1929. static inline void sk_set_txhash(struct sock *sk)
  1930. {
  1931. /* This pairs with READ_ONCE() in skb_set_hash_from_sk() */
  1932. WRITE_ONCE(sk->sk_txhash, net_tx_rndhash());
  1933. }
  1934. static inline bool sk_rethink_txhash(struct sock *sk)
  1935. {
  1936. if (sk->sk_txhash && sk->sk_txrehash == SOCK_TXREHASH_ENABLED) {
  1937. sk_set_txhash(sk);
  1938. return true;
  1939. }
  1940. return false;
  1941. }
  1942. static inline struct dst_entry *
  1943. __sk_dst_get(struct sock *sk)
  1944. {
  1945. return rcu_dereference_check(sk->sk_dst_cache,
  1946. lockdep_sock_is_held(sk));
  1947. }
  1948. static inline struct dst_entry *
  1949. sk_dst_get(struct sock *sk)
  1950. {
  1951. struct dst_entry *dst;
  1952. rcu_read_lock();
  1953. dst = rcu_dereference(sk->sk_dst_cache);
  1954. if (dst && !atomic_inc_not_zero(&dst->__refcnt))
  1955. dst = NULL;
  1956. rcu_read_unlock();
  1957. return dst;
  1958. }
  1959. static inline void __dst_negative_advice(struct sock *sk)
  1960. {
  1961. /* *** ANDROID FIXUP ***
  1962. * See b/343727534 for more details why this typedef is needed here.
  1963. * *** ANDROID FIXUP ***
  1964. */
  1965. android_dst_ops_negative_advice_new_t negative_advice;
  1966. void *c_is_fun; /* Work around --Werror=cast-function-type */
  1967. struct dst_entry *dst = __sk_dst_get(sk);
  1968. if (dst && dst->ops->negative_advice) {
  1969. c_is_fun = dst->ops->negative_advice;
  1970. negative_advice = c_is_fun;
  1971. negative_advice(sk, dst);
  1972. }
  1973. }
  1974. static inline void dst_negative_advice(struct sock *sk)
  1975. {
  1976. sk_rethink_txhash(sk);
  1977. __dst_negative_advice(sk);
  1978. }
  1979. static inline void
  1980. __sk_dst_set(struct sock *sk, struct dst_entry *dst)
  1981. {
  1982. struct dst_entry *old_dst;
  1983. sk_tx_queue_clear(sk);
  1984. WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
  1985. old_dst = rcu_dereference_protected(sk->sk_dst_cache,
  1986. lockdep_sock_is_held(sk));
  1987. rcu_assign_pointer(sk->sk_dst_cache, dst);
  1988. dst_release(old_dst);
  1989. }
  1990. static inline void
  1991. sk_dst_set(struct sock *sk, struct dst_entry *dst)
  1992. {
  1993. struct dst_entry *old_dst;
  1994. sk_tx_queue_clear(sk);
  1995. WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
  1996. old_dst = xchg((__force struct dst_entry **)&sk->sk_dst_cache, dst);
  1997. dst_release(old_dst);
  1998. }
  1999. static inline void
  2000. __sk_dst_reset(struct sock *sk)
  2001. {
  2002. __sk_dst_set(sk, NULL);
  2003. }
  2004. static inline void
  2005. sk_dst_reset(struct sock *sk)
  2006. {
  2007. sk_dst_set(sk, NULL);
  2008. }
  2009. struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie);
  2010. struct dst_entry *sk_dst_check(struct sock *sk, u32 cookie);
  2011. static inline void sk_dst_confirm(struct sock *sk)
  2012. {
  2013. if (!READ_ONCE(sk->sk_dst_pending_confirm))
  2014. WRITE_ONCE(sk->sk_dst_pending_confirm, 1);
  2015. }
  2016. static inline void sock_confirm_neigh(struct sk_buff *skb, struct neighbour *n)
  2017. {
  2018. if (skb_get_dst_pending_confirm(skb)) {
  2019. struct sock *sk = skb->sk;
  2020. if (sk && READ_ONCE(sk->sk_dst_pending_confirm))
  2021. WRITE_ONCE(sk->sk_dst_pending_confirm, 0);
  2022. neigh_confirm(n);
  2023. }
  2024. }
  2025. bool sk_mc_loop(struct sock *sk);
  2026. static inline bool sk_can_gso(const struct sock *sk)
  2027. {
  2028. return net_gso_ok(sk->sk_route_caps, sk->sk_gso_type);
  2029. }
  2030. void sk_setup_caps(struct sock *sk, struct dst_entry *dst);
  2031. static inline void sk_gso_disable(struct sock *sk)
  2032. {
  2033. sk->sk_gso_disabled = 1;
  2034. sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
  2035. }
  2036. static inline int skb_do_copy_data_nocache(struct sock *sk, struct sk_buff *skb,
  2037. struct iov_iter *from, char *to,
  2038. int copy, int offset)
  2039. {
  2040. if (skb->ip_summed == CHECKSUM_NONE) {
  2041. __wsum csum = 0;
  2042. if (!csum_and_copy_from_iter_full(to, copy, &csum, from))
  2043. return -EFAULT;
  2044. skb->csum = csum_block_add(skb->csum, csum, offset);
  2045. } else if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY) {
  2046. if (!copy_from_iter_full_nocache(to, copy, from))
  2047. return -EFAULT;
  2048. } else if (!copy_from_iter_full(to, copy, from))
  2049. return -EFAULT;
  2050. return 0;
  2051. }
  2052. static inline int skb_add_data_nocache(struct sock *sk, struct sk_buff *skb,
  2053. struct iov_iter *from, int copy)
  2054. {
  2055. int err, offset = skb->len;
  2056. err = skb_do_copy_data_nocache(sk, skb, from, skb_put(skb, copy),
  2057. copy, offset);
  2058. if (err)
  2059. __skb_trim(skb, offset);
  2060. return err;
  2061. }
  2062. static inline int skb_copy_to_page_nocache(struct sock *sk, struct iov_iter *from,
  2063. struct sk_buff *skb,
  2064. struct page *page,
  2065. int off, int copy)
  2066. {
  2067. int err;
  2068. err = skb_do_copy_data_nocache(sk, skb, from, page_address(page) + off,
  2069. copy, skb->len);
  2070. if (err)
  2071. return err;
  2072. skb_len_add(skb, copy);
  2073. sk_wmem_queued_add(sk, copy);
  2074. sk_mem_charge(sk, copy);
  2075. return 0;
  2076. }
  2077. /**
  2078. * sk_wmem_alloc_get - returns write allocations
  2079. * @sk: socket
  2080. *
  2081. * Return: sk_wmem_alloc minus initial offset of one
  2082. */
  2083. static inline int sk_wmem_alloc_get(const struct sock *sk)
  2084. {
  2085. return refcount_read(&sk->sk_wmem_alloc) - 1;
  2086. }
  2087. /**
  2088. * sk_rmem_alloc_get - returns read allocations
  2089. * @sk: socket
  2090. *
  2091. * Return: sk_rmem_alloc
  2092. */
  2093. static inline int sk_rmem_alloc_get(const struct sock *sk)
  2094. {
  2095. return atomic_read(&sk->sk_rmem_alloc);
  2096. }
  2097. /**
  2098. * sk_has_allocations - check if allocations are outstanding
  2099. * @sk: socket
  2100. *
  2101. * Return: true if socket has write or read allocations
  2102. */
  2103. static inline bool sk_has_allocations(const struct sock *sk)
  2104. {
  2105. return sk_wmem_alloc_get(sk) || sk_rmem_alloc_get(sk);
  2106. }
  2107. /**
  2108. * skwq_has_sleeper - check if there are any waiting processes
  2109. * @wq: struct socket_wq
  2110. *
  2111. * Return: true if socket_wq has waiting processes
  2112. *
  2113. * The purpose of the skwq_has_sleeper and sock_poll_wait is to wrap the memory
  2114. * barrier call. They were added due to the race found within the tcp code.
  2115. *
  2116. * Consider following tcp code paths::
  2117. *
  2118. * CPU1 CPU2
  2119. * sys_select receive packet
  2120. * ... ...
  2121. * __add_wait_queue update tp->rcv_nxt
  2122. * ... ...
  2123. * tp->rcv_nxt check sock_def_readable
  2124. * ... {
  2125. * schedule rcu_read_lock();
  2126. * wq = rcu_dereference(sk->sk_wq);
  2127. * if (wq && waitqueue_active(&wq->wait))
  2128. * wake_up_interruptible(&wq->wait)
  2129. * ...
  2130. * }
  2131. *
  2132. * The race for tcp fires when the __add_wait_queue changes done by CPU1 stay
  2133. * in its cache, and so does the tp->rcv_nxt update on CPU2 side. The CPU1
  2134. * could then endup calling schedule and sleep forever if there are no more
  2135. * data on the socket.
  2136. *
  2137. */
  2138. static inline bool skwq_has_sleeper(struct socket_wq *wq)
  2139. {
  2140. return wq && wq_has_sleeper(&wq->wait);
  2141. }
  2142. /**
  2143. * sock_poll_wait - place memory barrier behind the poll_wait call.
  2144. * @filp: file
  2145. * @sock: socket to wait on
  2146. * @p: poll_table
  2147. *
  2148. * See the comments in the wq_has_sleeper function.
  2149. */
  2150. static inline void sock_poll_wait(struct file *filp, struct socket *sock,
  2151. poll_table *p)
  2152. {
  2153. if (!poll_does_not_wait(p)) {
  2154. poll_wait(filp, &sock->wq.wait, p);
  2155. /* We need to be sure we are in sync with the
  2156. * socket flags modification.
  2157. *
  2158. * This memory barrier is paired in the wq_has_sleeper.
  2159. */
  2160. smp_mb();
  2161. }
  2162. }
  2163. static inline void skb_set_hash_from_sk(struct sk_buff *skb, struct sock *sk)
  2164. {
  2165. /* This pairs with WRITE_ONCE() in sk_set_txhash() */
  2166. u32 txhash = READ_ONCE(sk->sk_txhash);
  2167. if (txhash) {
  2168. skb->l4_hash = 1;
  2169. skb->hash = txhash;
  2170. }
  2171. }
  2172. void skb_set_owner_w(struct sk_buff *skb, struct sock *sk);
  2173. /*
  2174. * Queue a received datagram if it will fit. Stream and sequenced
  2175. * protocols can't normally use this as they need to fit buffers in
  2176. * and play with them.
  2177. *
  2178. * Inlined as it's very short and called for pretty much every
  2179. * packet ever received.
  2180. */
  2181. static inline void skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
  2182. {
  2183. skb_orphan(skb);
  2184. skb->sk = sk;
  2185. skb->destructor = sock_rfree;
  2186. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  2187. sk_mem_charge(sk, skb->truesize);
  2188. }
  2189. static inline __must_check bool skb_set_owner_sk_safe(struct sk_buff *skb, struct sock *sk)
  2190. {
  2191. if (sk && refcount_inc_not_zero(&sk->sk_refcnt)) {
  2192. skb_orphan(skb);
  2193. skb->destructor = sock_efree;
  2194. skb->sk = sk;
  2195. return true;
  2196. }
  2197. return false;
  2198. }
  2199. static inline struct sk_buff *skb_clone_and_charge_r(struct sk_buff *skb, struct sock *sk)
  2200. {
  2201. skb = skb_clone(skb, sk_gfp_mask(sk, GFP_ATOMIC));
  2202. if (skb) {
  2203. if (sk_rmem_schedule(sk, skb, skb->truesize)) {
  2204. skb_set_owner_r(skb, sk);
  2205. return skb;
  2206. }
  2207. __kfree_skb(skb);
  2208. }
  2209. return NULL;
  2210. }
  2211. static inline void skb_prepare_for_gro(struct sk_buff *skb)
  2212. {
  2213. if (skb->destructor != sock_wfree) {
  2214. skb_orphan(skb);
  2215. return;
  2216. }
  2217. skb->slow_gro = 1;
  2218. }
  2219. void sk_reset_timer(struct sock *sk, struct timer_list *timer,
  2220. unsigned long expires);
  2221. void sk_stop_timer(struct sock *sk, struct timer_list *timer);
  2222. void sk_stop_timer_sync(struct sock *sk, struct timer_list *timer);
  2223. int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue,
  2224. struct sk_buff *skb, unsigned int flags,
  2225. void (*destructor)(struct sock *sk,
  2226. struct sk_buff *skb));
  2227. int __sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
  2228. int sock_queue_rcv_skb_reason(struct sock *sk, struct sk_buff *skb,
  2229. enum skb_drop_reason *reason);
  2230. static inline int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  2231. {
  2232. return sock_queue_rcv_skb_reason(sk, skb, NULL);
  2233. }
  2234. int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb);
  2235. struct sk_buff *sock_dequeue_err_skb(struct sock *sk);
  2236. /*
  2237. * Recover an error report and clear atomically
  2238. */
  2239. static inline int sock_error(struct sock *sk)
  2240. {
  2241. int err;
  2242. /* Avoid an atomic operation for the common case.
  2243. * This is racy since another cpu/thread can change sk_err under us.
  2244. */
  2245. if (likely(data_race(!sk->sk_err)))
  2246. return 0;
  2247. err = xchg(&sk->sk_err, 0);
  2248. return -err;
  2249. }
  2250. void sk_error_report(struct sock *sk);
  2251. static inline unsigned long sock_wspace(struct sock *sk)
  2252. {
  2253. int amt = 0;
  2254. if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
  2255. amt = sk->sk_sndbuf - refcount_read(&sk->sk_wmem_alloc);
  2256. if (amt < 0)
  2257. amt = 0;
  2258. }
  2259. return amt;
  2260. }
  2261. /* Note:
  2262. * We use sk->sk_wq_raw, from contexts knowing this
  2263. * pointer is not NULL and cannot disappear/change.
  2264. */
  2265. static inline void sk_set_bit(int nr, struct sock *sk)
  2266. {
  2267. if ((nr == SOCKWQ_ASYNC_NOSPACE || nr == SOCKWQ_ASYNC_WAITDATA) &&
  2268. !sock_flag(sk, SOCK_FASYNC))
  2269. return;
  2270. set_bit(nr, &sk->sk_wq_raw->flags);
  2271. }
  2272. static inline void sk_clear_bit(int nr, struct sock *sk)
  2273. {
  2274. if ((nr == SOCKWQ_ASYNC_NOSPACE || nr == SOCKWQ_ASYNC_WAITDATA) &&
  2275. !sock_flag(sk, SOCK_FASYNC))
  2276. return;
  2277. clear_bit(nr, &sk->sk_wq_raw->flags);
  2278. }
  2279. static inline void sk_wake_async(const struct sock *sk, int how, int band)
  2280. {
  2281. if (sock_flag(sk, SOCK_FASYNC)) {
  2282. rcu_read_lock();
  2283. sock_wake_async(rcu_dereference(sk->sk_wq), how, band);
  2284. rcu_read_unlock();
  2285. }
  2286. }
  2287. /* Since sk_{r,w}mem_alloc sums skb->truesize, even a small frame might
  2288. * need sizeof(sk_buff) + MTU + padding, unless net driver perform copybreak.
  2289. * Note: for send buffers, TCP works better if we can build two skbs at
  2290. * minimum.
  2291. */
  2292. #define TCP_SKB_MIN_TRUESIZE (2048 + SKB_DATA_ALIGN(sizeof(struct sk_buff)))
  2293. #define SOCK_MIN_SNDBUF (TCP_SKB_MIN_TRUESIZE * 2)
  2294. #define SOCK_MIN_RCVBUF TCP_SKB_MIN_TRUESIZE
  2295. static inline void sk_stream_moderate_sndbuf(struct sock *sk)
  2296. {
  2297. u32 val;
  2298. if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
  2299. return;
  2300. val = min(sk->sk_sndbuf, sk->sk_wmem_queued >> 1);
  2301. val = max_t(u32, val, sk_unused_reserved_mem(sk));
  2302. WRITE_ONCE(sk->sk_sndbuf, max_t(u32, val, SOCK_MIN_SNDBUF));
  2303. }
  2304. /**
  2305. * sk_page_frag - return an appropriate page_frag
  2306. * @sk: socket
  2307. *
  2308. * Use the per task page_frag instead of the per socket one for
  2309. * optimization when we know that we're in process context and own
  2310. * everything that's associated with %current.
  2311. *
  2312. * Both direct reclaim and page faults can nest inside other
  2313. * socket operations and end up recursing into sk_page_frag()
  2314. * while it's already in use: explicitly avoid task page_frag
  2315. * usage if the caller is potentially doing any of them.
  2316. * This assumes that page fault handlers use the GFP_NOFS flags.
  2317. *
  2318. * Return: a per task page_frag if context allows that,
  2319. * otherwise a per socket one.
  2320. */
  2321. static inline struct page_frag *sk_page_frag(struct sock *sk)
  2322. {
  2323. if ((sk->sk_allocation & (__GFP_DIRECT_RECLAIM | __GFP_MEMALLOC | __GFP_FS)) ==
  2324. (__GFP_DIRECT_RECLAIM | __GFP_FS))
  2325. return &current->task_frag;
  2326. return &sk->sk_frag;
  2327. }
  2328. bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag);
  2329. /*
  2330. * Default write policy as shown to user space via poll/select/SIGIO
  2331. */
  2332. static inline bool sock_writeable(const struct sock *sk)
  2333. {
  2334. return refcount_read(&sk->sk_wmem_alloc) < (READ_ONCE(sk->sk_sndbuf) >> 1);
  2335. }
  2336. static inline gfp_t gfp_any(void)
  2337. {
  2338. return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
  2339. }
  2340. static inline gfp_t gfp_memcg_charge(void)
  2341. {
  2342. return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
  2343. }
  2344. static inline long sock_rcvtimeo(const struct sock *sk, bool noblock)
  2345. {
  2346. return noblock ? 0 : sk->sk_rcvtimeo;
  2347. }
  2348. static inline long sock_sndtimeo(const struct sock *sk, bool noblock)
  2349. {
  2350. return noblock ? 0 : sk->sk_sndtimeo;
  2351. }
  2352. static inline int sock_rcvlowat(const struct sock *sk, int waitall, int len)
  2353. {
  2354. int v = waitall ? len : min_t(int, READ_ONCE(sk->sk_rcvlowat), len);
  2355. return v ?: 1;
  2356. }
  2357. /* Alas, with timeout socket operations are not restartable.
  2358. * Compare this to poll().
  2359. */
  2360. static inline int sock_intr_errno(long timeo)
  2361. {
  2362. return timeo == MAX_SCHEDULE_TIMEOUT ? -ERESTARTSYS : -EINTR;
  2363. }
  2364. struct sock_skb_cb {
  2365. u32 dropcount;
  2366. };
  2367. /* Store sock_skb_cb at the end of skb->cb[] so protocol families
  2368. * using skb->cb[] would keep using it directly and utilize its
  2369. * alignement guarantee.
  2370. */
  2371. #define SOCK_SKB_CB_OFFSET ((sizeof_field(struct sk_buff, cb) - \
  2372. sizeof(struct sock_skb_cb)))
  2373. #define SOCK_SKB_CB(__skb) ((struct sock_skb_cb *)((__skb)->cb + \
  2374. SOCK_SKB_CB_OFFSET))
  2375. #define sock_skb_cb_check_size(size) \
  2376. BUILD_BUG_ON((size) > SOCK_SKB_CB_OFFSET)
  2377. static inline void
  2378. sock_skb_set_dropcount(const struct sock *sk, struct sk_buff *skb)
  2379. {
  2380. SOCK_SKB_CB(skb)->dropcount = sock_flag(sk, SOCK_RXQ_OVFL) ?
  2381. atomic_read(&sk->sk_drops) : 0;
  2382. }
  2383. static inline void sk_drops_add(struct sock *sk, const struct sk_buff *skb)
  2384. {
  2385. int segs = max_t(u16, 1, skb_shinfo(skb)->gso_segs);
  2386. atomic_add(segs, &sk->sk_drops);
  2387. }
  2388. static inline ktime_t sock_read_timestamp(struct sock *sk)
  2389. {
  2390. #if BITS_PER_LONG==32
  2391. unsigned int seq;
  2392. ktime_t kt;
  2393. do {
  2394. seq = read_seqbegin(&sk->sk_stamp_seq);
  2395. kt = sk->sk_stamp;
  2396. } while (read_seqretry(&sk->sk_stamp_seq, seq));
  2397. return kt;
  2398. #else
  2399. return READ_ONCE(sk->sk_stamp);
  2400. #endif
  2401. }
  2402. static inline void sock_write_timestamp(struct sock *sk, ktime_t kt)
  2403. {
  2404. #if BITS_PER_LONG==32
  2405. write_seqlock(&sk->sk_stamp_seq);
  2406. sk->sk_stamp = kt;
  2407. write_sequnlock(&sk->sk_stamp_seq);
  2408. #else
  2409. WRITE_ONCE(sk->sk_stamp, kt);
  2410. #endif
  2411. }
  2412. void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  2413. struct sk_buff *skb);
  2414. void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
  2415. struct sk_buff *skb);
  2416. static inline void
  2417. sock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
  2418. {
  2419. ktime_t kt = skb->tstamp;
  2420. struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
  2421. /*
  2422. * generate control messages if
  2423. * - receive time stamping in software requested
  2424. * - software time stamp available and wanted
  2425. * - hardware time stamps available and wanted
  2426. */
  2427. if (sock_flag(sk, SOCK_RCVTSTAMP) ||
  2428. (sk->sk_tsflags & SOF_TIMESTAMPING_RX_SOFTWARE) ||
  2429. (kt && sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) ||
  2430. (hwtstamps->hwtstamp &&
  2431. (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE)))
  2432. __sock_recv_timestamp(msg, sk, skb);
  2433. else
  2434. sock_write_timestamp(sk, kt);
  2435. if (sock_flag(sk, SOCK_WIFI_STATUS) && skb->wifi_acked_valid)
  2436. __sock_recv_wifi_status(msg, sk, skb);
  2437. }
  2438. void __sock_recv_cmsgs(struct msghdr *msg, struct sock *sk,
  2439. struct sk_buff *skb);
  2440. #define SK_DEFAULT_STAMP (-1L * NSEC_PER_SEC)
  2441. static inline void sock_recv_cmsgs(struct msghdr *msg, struct sock *sk,
  2442. struct sk_buff *skb)
  2443. {
  2444. #define FLAGS_RECV_CMSGS ((1UL << SOCK_RXQ_OVFL) | \
  2445. (1UL << SOCK_RCVTSTAMP) | \
  2446. (1UL << SOCK_RCVMARK))
  2447. #define TSFLAGS_ANY (SOF_TIMESTAMPING_SOFTWARE | \
  2448. SOF_TIMESTAMPING_RAW_HARDWARE)
  2449. if (sk->sk_flags & FLAGS_RECV_CMSGS || sk->sk_tsflags & TSFLAGS_ANY)
  2450. __sock_recv_cmsgs(msg, sk, skb);
  2451. else if (unlikely(sock_flag(sk, SOCK_TIMESTAMP)))
  2452. sock_write_timestamp(sk, skb->tstamp);
  2453. else if (unlikely(sock_read_timestamp(sk) == SK_DEFAULT_STAMP))
  2454. sock_write_timestamp(sk, 0);
  2455. }
  2456. void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags);
  2457. /**
  2458. * _sock_tx_timestamp - checks whether the outgoing packet is to be time stamped
  2459. * @sk: socket sending this packet
  2460. * @tsflags: timestamping flags to use
  2461. * @tx_flags: completed with instructions for time stamping
  2462. * @tskey: filled in with next sk_tskey (not for TCP, which uses seqno)
  2463. *
  2464. * Note: callers should take care of initial ``*tx_flags`` value (usually 0)
  2465. */
  2466. static inline void _sock_tx_timestamp(struct sock *sk, __u16 tsflags,
  2467. __u8 *tx_flags, __u32 *tskey)
  2468. {
  2469. if (unlikely(tsflags)) {
  2470. __sock_tx_timestamp(tsflags, tx_flags);
  2471. if (tsflags & SOF_TIMESTAMPING_OPT_ID && tskey &&
  2472. tsflags & SOF_TIMESTAMPING_TX_RECORD_MASK)
  2473. *tskey = atomic_inc_return(&sk->sk_tskey) - 1;
  2474. }
  2475. if (unlikely(sock_flag(sk, SOCK_WIFI_STATUS)))
  2476. *tx_flags |= SKBTX_WIFI_STATUS;
  2477. }
  2478. static inline void sock_tx_timestamp(struct sock *sk, __u16 tsflags,
  2479. __u8 *tx_flags)
  2480. {
  2481. _sock_tx_timestamp(sk, tsflags, tx_flags, NULL);
  2482. }
  2483. static inline void skb_setup_tx_timestamp(struct sk_buff *skb, __u16 tsflags)
  2484. {
  2485. _sock_tx_timestamp(skb->sk, tsflags, &skb_shinfo(skb)->tx_flags,
  2486. &skb_shinfo(skb)->tskey);
  2487. }
  2488. static inline bool sk_is_tcp(const struct sock *sk)
  2489. {
  2490. return sk->sk_type == SOCK_STREAM && sk->sk_protocol == IPPROTO_TCP;
  2491. }
  2492. /**
  2493. * sk_eat_skb - Release a skb if it is no longer needed
  2494. * @sk: socket to eat this skb from
  2495. * @skb: socket buffer to eat
  2496. *
  2497. * This routine must be called with interrupts disabled or with the socket
  2498. * locked so that the sk_buff queue operation is ok.
  2499. */
  2500. static inline void sk_eat_skb(struct sock *sk, struct sk_buff *skb)
  2501. {
  2502. __skb_unlink(skb, &sk->sk_receive_queue);
  2503. __kfree_skb(skb);
  2504. }
  2505. static inline bool
  2506. skb_sk_is_prefetched(struct sk_buff *skb)
  2507. {
  2508. #ifdef CONFIG_INET
  2509. return skb->destructor == sock_pfree;
  2510. #else
  2511. return false;
  2512. #endif /* CONFIG_INET */
  2513. }
  2514. /* This helper checks if a socket is a full socket,
  2515. * ie _not_ a timewait or request socket.
  2516. */
  2517. static inline bool sk_fullsock(const struct sock *sk)
  2518. {
  2519. return (1 << sk->sk_state) & ~(TCPF_TIME_WAIT | TCPF_NEW_SYN_RECV);
  2520. }
  2521. static inline bool
  2522. sk_is_refcounted(struct sock *sk)
  2523. {
  2524. /* Only full sockets have sk->sk_flags. */
  2525. return !sk_fullsock(sk) || !sock_flag(sk, SOCK_RCU_FREE);
  2526. }
  2527. /**
  2528. * skb_steal_sock - steal a socket from an sk_buff
  2529. * @skb: sk_buff to steal the socket from
  2530. * @refcounted: is set to true if the socket is reference-counted
  2531. */
  2532. static inline struct sock *
  2533. skb_steal_sock(struct sk_buff *skb, bool *refcounted)
  2534. {
  2535. if (skb->sk) {
  2536. struct sock *sk = skb->sk;
  2537. *refcounted = true;
  2538. if (skb_sk_is_prefetched(skb))
  2539. *refcounted = sk_is_refcounted(sk);
  2540. skb->destructor = NULL;
  2541. skb->sk = NULL;
  2542. return sk;
  2543. }
  2544. *refcounted = false;
  2545. return NULL;
  2546. }
  2547. /* Checks if this SKB belongs to an HW offloaded socket
  2548. * and whether any SW fallbacks are required based on dev.
  2549. * Check decrypted mark in case skb_orphan() cleared socket.
  2550. */
  2551. static inline struct sk_buff *sk_validate_xmit_skb(struct sk_buff *skb,
  2552. struct net_device *dev)
  2553. {
  2554. #ifdef CONFIG_SOCK_VALIDATE_XMIT
  2555. struct sock *sk = skb->sk;
  2556. if (sk && sk_fullsock(sk) && sk->sk_validate_xmit_skb) {
  2557. skb = sk->sk_validate_xmit_skb(sk, dev, skb);
  2558. #ifdef CONFIG_TLS_DEVICE
  2559. } else if (unlikely(skb->decrypted)) {
  2560. pr_warn_ratelimited("unencrypted skb with no associated socket - dropping\n");
  2561. kfree_skb(skb);
  2562. skb = NULL;
  2563. #endif
  2564. }
  2565. #endif
  2566. return skb;
  2567. }
  2568. /* This helper checks if a socket is a LISTEN or NEW_SYN_RECV
  2569. * SYNACK messages can be attached to either ones (depending on SYNCOOKIE)
  2570. */
  2571. static inline bool sk_listener(const struct sock *sk)
  2572. {
  2573. return (1 << sk->sk_state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV);
  2574. }
  2575. void sock_enable_timestamp(struct sock *sk, enum sock_flags flag);
  2576. int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len, int level,
  2577. int type);
  2578. bool sk_ns_capable(const struct sock *sk,
  2579. struct user_namespace *user_ns, int cap);
  2580. bool sk_capable(const struct sock *sk, int cap);
  2581. bool sk_net_capable(const struct sock *sk, int cap);
  2582. void sk_get_meminfo(const struct sock *sk, u32 *meminfo);
  2583. /* Take into consideration the size of the struct sk_buff overhead in the
  2584. * determination of these values, since that is non-constant across
  2585. * platforms. This makes socket queueing behavior and performance
  2586. * not depend upon such differences.
  2587. */
  2588. #define _SK_MEM_PACKETS 256
  2589. #define _SK_MEM_OVERHEAD SKB_TRUESIZE(256)
  2590. #define SK_WMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
  2591. #define SK_RMEM_MAX (_SK_MEM_OVERHEAD * _SK_MEM_PACKETS)
  2592. extern __u32 sysctl_wmem_max;
  2593. extern __u32 sysctl_rmem_max;
  2594. extern int sysctl_tstamp_allow_data;
  2595. extern int sysctl_optmem_max;
  2596. extern __u32 sysctl_wmem_default;
  2597. extern __u32 sysctl_rmem_default;
  2598. #define SKB_FRAG_PAGE_ORDER get_order(32768)
  2599. DECLARE_STATIC_KEY_FALSE(net_high_order_alloc_disable_key);
  2600. static inline int sk_get_wmem0(const struct sock *sk, const struct proto *proto)
  2601. {
  2602. /* Does this proto have per netns sysctl_wmem ? */
  2603. if (proto->sysctl_wmem_offset)
  2604. return READ_ONCE(*(int *)((void *)sock_net(sk) + proto->sysctl_wmem_offset));
  2605. return READ_ONCE(*proto->sysctl_wmem);
  2606. }
  2607. static inline int sk_get_rmem0(const struct sock *sk, const struct proto *proto)
  2608. {
  2609. /* Does this proto have per netns sysctl_rmem ? */
  2610. if (proto->sysctl_rmem_offset)
  2611. return READ_ONCE(*(int *)((void *)sock_net(sk) + proto->sysctl_rmem_offset));
  2612. return READ_ONCE(*proto->sysctl_rmem);
  2613. }
  2614. /* Default TCP Small queue budget is ~1 ms of data (1sec >> 10)
  2615. * Some wifi drivers need to tweak it to get more chunks.
  2616. * They can use this helper from their ndo_start_xmit()
  2617. */
  2618. static inline void sk_pacing_shift_update(struct sock *sk, int val)
  2619. {
  2620. if (!sk || !sk_fullsock(sk) || READ_ONCE(sk->sk_pacing_shift) == val)
  2621. return;
  2622. WRITE_ONCE(sk->sk_pacing_shift, val);
  2623. }
  2624. /* if a socket is bound to a device, check that the given device
  2625. * index is either the same or that the socket is bound to an L3
  2626. * master device and the given device index is also enslaved to
  2627. * that L3 master
  2628. */
  2629. static inline bool sk_dev_equal_l3scope(struct sock *sk, int dif)
  2630. {
  2631. int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
  2632. int mdif;
  2633. if (!bound_dev_if || bound_dev_if == dif)
  2634. return true;
  2635. mdif = l3mdev_master_ifindex_by_index(sock_net(sk), dif);
  2636. if (mdif && mdif == bound_dev_if)
  2637. return true;
  2638. return false;
  2639. }
  2640. void sock_def_readable(struct sock *sk);
  2641. int sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk);
  2642. void sock_set_timestamp(struct sock *sk, int optname, bool valbool);
  2643. int sock_set_timestamping(struct sock *sk, int optname,
  2644. struct so_timestamping timestamping);
  2645. void sock_enable_timestamps(struct sock *sk);
  2646. void sock_no_linger(struct sock *sk);
  2647. void sock_set_keepalive(struct sock *sk);
  2648. void sock_set_priority(struct sock *sk, u32 priority);
  2649. void sock_set_rcvbuf(struct sock *sk, int val);
  2650. void sock_set_mark(struct sock *sk, u32 val);
  2651. void sock_set_reuseaddr(struct sock *sk);
  2652. void sock_set_reuseport(struct sock *sk);
  2653. void sock_set_sndtimeo(struct sock *sk, s64 secs);
  2654. int sock_bind_add(struct sock *sk, struct sockaddr *addr, int addr_len);
  2655. int sock_get_timeout(long timeo, void *optval, bool old_timeval);
  2656. int sock_copy_user_timeval(struct __kernel_sock_timeval *tv,
  2657. sockptr_t optval, int optlen, bool old_timeval);
  2658. static inline bool sk_is_readable(struct sock *sk)
  2659. {
  2660. if (sk->sk_prot->sock_is_readable)
  2661. return sk->sk_prot->sock_is_readable(sk);
  2662. return false;
  2663. }
  2664. #endif /* _SOCK_H */