sock.h 83 KB

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