sockmap.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632
  1. /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details.
  11. */
  12. /* A BPF sock_map is used to store sock objects. This is primarly used
  13. * for doing socket redirect with BPF helper routines.
  14. *
  15. * A sock map may have BPF programs attached to it, currently a program
  16. * used to parse packets and a program to provide a verdict and redirect
  17. * decision on the packet are supported. Any programs attached to a sock
  18. * map are inherited by sock objects when they are added to the map. If
  19. * no BPF programs are attached the sock object may only be used for sock
  20. * redirect.
  21. *
  22. * A sock object may be in multiple maps, but can only inherit a single
  23. * parse or verdict program. If adding a sock object to a map would result
  24. * in having multiple parsing programs the update will return an EBUSY error.
  25. *
  26. * For reference this program is similar to devmap used in XDP context
  27. * reviewing these together may be useful. For an example please review
  28. * ./samples/bpf/sockmap/.
  29. */
  30. #include <linux/bpf.h>
  31. #include <net/sock.h>
  32. #include <linux/filter.h>
  33. #include <linux/errno.h>
  34. #include <linux/file.h>
  35. #include <linux/kernel.h>
  36. #include <linux/net.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/workqueue.h>
  39. #include <linux/list.h>
  40. #include <linux/mm.h>
  41. #include <net/strparser.h>
  42. #include <net/tcp.h>
  43. #include <linux/ptr_ring.h>
  44. #include <net/inet_common.h>
  45. #include <linux/sched/signal.h>
  46. #define SOCK_CREATE_FLAG_MASK \
  47. (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
  48. struct bpf_sock_progs {
  49. struct bpf_prog *bpf_tx_msg;
  50. struct bpf_prog *bpf_parse;
  51. struct bpf_prog *bpf_verdict;
  52. };
  53. struct bpf_stab {
  54. struct bpf_map map;
  55. struct sock **sock_map;
  56. struct bpf_sock_progs progs;
  57. raw_spinlock_t lock;
  58. };
  59. struct bucket {
  60. struct hlist_head head;
  61. raw_spinlock_t lock;
  62. };
  63. struct bpf_htab {
  64. struct bpf_map map;
  65. struct bucket *buckets;
  66. atomic_t count;
  67. u32 n_buckets;
  68. u32 elem_size;
  69. struct bpf_sock_progs progs;
  70. struct rcu_head rcu;
  71. };
  72. struct htab_elem {
  73. struct rcu_head rcu;
  74. struct hlist_node hash_node;
  75. u32 hash;
  76. struct sock *sk;
  77. char key[0];
  78. };
  79. enum smap_psock_state {
  80. SMAP_TX_RUNNING,
  81. };
  82. struct smap_psock_map_entry {
  83. struct list_head list;
  84. struct bpf_map *map;
  85. struct sock **entry;
  86. struct htab_elem __rcu *hash_link;
  87. };
  88. struct smap_psock {
  89. struct rcu_head rcu;
  90. refcount_t refcnt;
  91. /* datapath variables */
  92. struct sk_buff_head rxqueue;
  93. bool strp_enabled;
  94. /* datapath error path cache across tx work invocations */
  95. int save_rem;
  96. int save_off;
  97. struct sk_buff *save_skb;
  98. /* datapath variables for tx_msg ULP */
  99. struct sock *sk_redir;
  100. int apply_bytes;
  101. int cork_bytes;
  102. int sg_size;
  103. int eval;
  104. struct sk_msg_buff *cork;
  105. struct list_head ingress;
  106. struct strparser strp;
  107. struct bpf_prog *bpf_tx_msg;
  108. struct bpf_prog *bpf_parse;
  109. struct bpf_prog *bpf_verdict;
  110. struct list_head maps;
  111. spinlock_t maps_lock;
  112. /* Back reference used when sock callback trigger sockmap operations */
  113. struct sock *sock;
  114. unsigned long state;
  115. struct work_struct tx_work;
  116. struct work_struct gc_work;
  117. struct proto *sk_proto;
  118. void (*save_unhash)(struct sock *sk);
  119. void (*save_close)(struct sock *sk, long timeout);
  120. void (*save_data_ready)(struct sock *sk);
  121. void (*save_write_space)(struct sock *sk);
  122. };
  123. static void smap_release_sock(struct smap_psock *psock, struct sock *sock);
  124. static int bpf_tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  125. int nonblock, int flags, int *addr_len);
  126. static int bpf_tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
  127. static int bpf_tcp_sendpage(struct sock *sk, struct page *page,
  128. int offset, size_t size, int flags);
  129. static void bpf_tcp_unhash(struct sock *sk);
  130. static void bpf_tcp_close(struct sock *sk, long timeout);
  131. static inline struct smap_psock *smap_psock_sk(const struct sock *sk)
  132. {
  133. return rcu_dereference_sk_user_data(sk);
  134. }
  135. static bool bpf_tcp_stream_read(const struct sock *sk)
  136. {
  137. struct smap_psock *psock;
  138. bool empty = true;
  139. rcu_read_lock();
  140. psock = smap_psock_sk(sk);
  141. if (unlikely(!psock))
  142. goto out;
  143. empty = list_empty(&psock->ingress);
  144. out:
  145. rcu_read_unlock();
  146. return !empty;
  147. }
  148. enum {
  149. SOCKMAP_IPV4,
  150. SOCKMAP_IPV6,
  151. SOCKMAP_NUM_PROTS,
  152. };
  153. enum {
  154. SOCKMAP_BASE,
  155. SOCKMAP_TX,
  156. SOCKMAP_NUM_CONFIGS,
  157. };
  158. static struct proto *saved_tcpv6_prot __read_mostly;
  159. static DEFINE_SPINLOCK(tcpv6_prot_lock);
  160. static struct proto bpf_tcp_prots[SOCKMAP_NUM_PROTS][SOCKMAP_NUM_CONFIGS];
  161. static void build_protos(struct proto prot[SOCKMAP_NUM_CONFIGS],
  162. struct proto *base)
  163. {
  164. prot[SOCKMAP_BASE] = *base;
  165. prot[SOCKMAP_BASE].unhash = bpf_tcp_unhash;
  166. prot[SOCKMAP_BASE].close = bpf_tcp_close;
  167. prot[SOCKMAP_BASE].recvmsg = bpf_tcp_recvmsg;
  168. prot[SOCKMAP_BASE].stream_memory_read = bpf_tcp_stream_read;
  169. prot[SOCKMAP_TX] = prot[SOCKMAP_BASE];
  170. prot[SOCKMAP_TX].sendmsg = bpf_tcp_sendmsg;
  171. prot[SOCKMAP_TX].sendpage = bpf_tcp_sendpage;
  172. }
  173. static void update_sk_prot(struct sock *sk, struct smap_psock *psock)
  174. {
  175. int family = sk->sk_family == AF_INET6 ? SOCKMAP_IPV6 : SOCKMAP_IPV4;
  176. int conf = psock->bpf_tx_msg ? SOCKMAP_TX : SOCKMAP_BASE;
  177. sk->sk_prot = &bpf_tcp_prots[family][conf];
  178. }
  179. static int bpf_tcp_init(struct sock *sk)
  180. {
  181. struct smap_psock *psock;
  182. rcu_read_lock();
  183. psock = smap_psock_sk(sk);
  184. if (unlikely(!psock)) {
  185. rcu_read_unlock();
  186. return -EINVAL;
  187. }
  188. if (unlikely(psock->sk_proto)) {
  189. rcu_read_unlock();
  190. return -EBUSY;
  191. }
  192. psock->save_unhash = sk->sk_prot->unhash;
  193. psock->save_close = sk->sk_prot->close;
  194. psock->sk_proto = sk->sk_prot;
  195. /* Build IPv6 sockmap whenever the address of tcpv6_prot changes */
  196. if (sk->sk_family == AF_INET6 &&
  197. unlikely(sk->sk_prot != smp_load_acquire(&saved_tcpv6_prot))) {
  198. spin_lock_bh(&tcpv6_prot_lock);
  199. if (likely(sk->sk_prot != saved_tcpv6_prot)) {
  200. build_protos(bpf_tcp_prots[SOCKMAP_IPV6], sk->sk_prot);
  201. smp_store_release(&saved_tcpv6_prot, sk->sk_prot);
  202. }
  203. spin_unlock_bh(&tcpv6_prot_lock);
  204. }
  205. update_sk_prot(sk, psock);
  206. rcu_read_unlock();
  207. return 0;
  208. }
  209. static void smap_release_sock(struct smap_psock *psock, struct sock *sock);
  210. static int free_start_sg(struct sock *sk, struct sk_msg_buff *md, bool charge);
  211. static void bpf_tcp_release(struct sock *sk)
  212. {
  213. struct smap_psock *psock;
  214. rcu_read_lock();
  215. psock = smap_psock_sk(sk);
  216. if (unlikely(!psock))
  217. goto out;
  218. if (psock->cork) {
  219. free_start_sg(psock->sock, psock->cork, true);
  220. kfree(psock->cork);
  221. psock->cork = NULL;
  222. }
  223. if (psock->sk_proto) {
  224. sk->sk_prot = psock->sk_proto;
  225. psock->sk_proto = NULL;
  226. }
  227. out:
  228. rcu_read_unlock();
  229. }
  230. static struct htab_elem *lookup_elem_raw(struct hlist_head *head,
  231. u32 hash, void *key, u32 key_size)
  232. {
  233. struct htab_elem *l;
  234. hlist_for_each_entry_rcu(l, head, hash_node) {
  235. if (l->hash == hash && !memcmp(&l->key, key, key_size))
  236. return l;
  237. }
  238. return NULL;
  239. }
  240. static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
  241. {
  242. return &htab->buckets[hash & (htab->n_buckets - 1)];
  243. }
  244. static inline struct hlist_head *select_bucket(struct bpf_htab *htab, u32 hash)
  245. {
  246. return &__select_bucket(htab, hash)->head;
  247. }
  248. static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
  249. {
  250. atomic_dec(&htab->count);
  251. kfree_rcu(l, rcu);
  252. }
  253. static struct smap_psock_map_entry *psock_map_pop(struct sock *sk,
  254. struct smap_psock *psock)
  255. {
  256. struct smap_psock_map_entry *e;
  257. spin_lock_bh(&psock->maps_lock);
  258. e = list_first_entry_or_null(&psock->maps,
  259. struct smap_psock_map_entry,
  260. list);
  261. if (e)
  262. list_del(&e->list);
  263. spin_unlock_bh(&psock->maps_lock);
  264. return e;
  265. }
  266. static void bpf_tcp_remove(struct sock *sk, struct smap_psock *psock)
  267. {
  268. struct smap_psock_map_entry *e;
  269. struct sk_msg_buff *md, *mtmp;
  270. struct sock *osk;
  271. if (psock->cork) {
  272. free_start_sg(psock->sock, psock->cork, true);
  273. kfree(psock->cork);
  274. psock->cork = NULL;
  275. }
  276. list_for_each_entry_safe(md, mtmp, &psock->ingress, list) {
  277. list_del(&md->list);
  278. free_start_sg(psock->sock, md, true);
  279. kfree(md);
  280. }
  281. e = psock_map_pop(sk, psock);
  282. while (e) {
  283. if (e->entry) {
  284. struct bpf_stab *stab = container_of(e->map, struct bpf_stab, map);
  285. raw_spin_lock_bh(&stab->lock);
  286. osk = *e->entry;
  287. if (osk == sk) {
  288. *e->entry = NULL;
  289. smap_release_sock(psock, sk);
  290. }
  291. raw_spin_unlock_bh(&stab->lock);
  292. } else {
  293. struct htab_elem *link = rcu_dereference(e->hash_link);
  294. struct bpf_htab *htab = container_of(e->map, struct bpf_htab, map);
  295. struct hlist_head *head;
  296. struct htab_elem *l;
  297. struct bucket *b;
  298. b = __select_bucket(htab, link->hash);
  299. head = &b->head;
  300. raw_spin_lock_bh(&b->lock);
  301. l = lookup_elem_raw(head,
  302. link->hash, link->key,
  303. htab->map.key_size);
  304. /* If another thread deleted this object skip deletion.
  305. * The refcnt on psock may or may not be zero.
  306. */
  307. if (l && l == link) {
  308. hlist_del_rcu(&link->hash_node);
  309. smap_release_sock(psock, link->sk);
  310. free_htab_elem(htab, link);
  311. }
  312. raw_spin_unlock_bh(&b->lock);
  313. }
  314. kfree(e);
  315. e = psock_map_pop(sk, psock);
  316. }
  317. }
  318. static void bpf_tcp_unhash(struct sock *sk)
  319. {
  320. void (*unhash_fun)(struct sock *sk);
  321. struct smap_psock *psock;
  322. rcu_read_lock();
  323. psock = smap_psock_sk(sk);
  324. if (unlikely(!psock)) {
  325. rcu_read_unlock();
  326. if (sk->sk_prot->unhash)
  327. sk->sk_prot->unhash(sk);
  328. return;
  329. }
  330. unhash_fun = psock->save_unhash;
  331. bpf_tcp_remove(sk, psock);
  332. rcu_read_unlock();
  333. unhash_fun(sk);
  334. }
  335. static void bpf_tcp_close(struct sock *sk, long timeout)
  336. {
  337. void (*close_fun)(struct sock *sk, long timeout);
  338. struct smap_psock *psock;
  339. lock_sock(sk);
  340. rcu_read_lock();
  341. psock = smap_psock_sk(sk);
  342. if (unlikely(!psock)) {
  343. rcu_read_unlock();
  344. release_sock(sk);
  345. return sk->sk_prot->close(sk, timeout);
  346. }
  347. close_fun = psock->save_close;
  348. bpf_tcp_remove(sk, psock);
  349. rcu_read_unlock();
  350. release_sock(sk);
  351. close_fun(sk, timeout);
  352. }
  353. enum __sk_action {
  354. __SK_DROP = 0,
  355. __SK_PASS,
  356. __SK_REDIRECT,
  357. __SK_NONE,
  358. };
  359. static struct tcp_ulp_ops bpf_tcp_ulp_ops __read_mostly = {
  360. .name = "bpf_tcp",
  361. .uid = TCP_ULP_BPF,
  362. .user_visible = false,
  363. .owner = NULL,
  364. .init = bpf_tcp_init,
  365. .release = bpf_tcp_release,
  366. };
  367. static int memcopy_from_iter(struct sock *sk,
  368. struct sk_msg_buff *md,
  369. struct iov_iter *from, int bytes)
  370. {
  371. struct scatterlist *sg = md->sg_data;
  372. int i = md->sg_curr, rc = -ENOSPC;
  373. do {
  374. int copy;
  375. char *to;
  376. if (md->sg_copybreak >= sg[i].length) {
  377. md->sg_copybreak = 0;
  378. if (++i == MAX_SKB_FRAGS)
  379. i = 0;
  380. if (i == md->sg_end)
  381. break;
  382. }
  383. copy = sg[i].length - md->sg_copybreak;
  384. to = sg_virt(&sg[i]) + md->sg_copybreak;
  385. md->sg_copybreak += copy;
  386. if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY)
  387. rc = copy_from_iter_nocache(to, copy, from);
  388. else
  389. rc = copy_from_iter(to, copy, from);
  390. if (rc != copy) {
  391. rc = -EFAULT;
  392. goto out;
  393. }
  394. bytes -= copy;
  395. if (!bytes)
  396. break;
  397. md->sg_copybreak = 0;
  398. if (++i == MAX_SKB_FRAGS)
  399. i = 0;
  400. } while (i != md->sg_end);
  401. out:
  402. md->sg_curr = i;
  403. return rc;
  404. }
  405. static int bpf_tcp_push(struct sock *sk, int apply_bytes,
  406. struct sk_msg_buff *md,
  407. int flags, bool uncharge)
  408. {
  409. bool apply = apply_bytes;
  410. struct scatterlist *sg;
  411. int offset, ret = 0;
  412. struct page *p;
  413. size_t size;
  414. while (1) {
  415. sg = md->sg_data + md->sg_start;
  416. size = (apply && apply_bytes < sg->length) ?
  417. apply_bytes : sg->length;
  418. offset = sg->offset;
  419. tcp_rate_check_app_limited(sk);
  420. p = sg_page(sg);
  421. retry:
  422. ret = do_tcp_sendpages(sk, p, offset, size, flags);
  423. if (ret != size) {
  424. if (ret > 0) {
  425. if (apply)
  426. apply_bytes -= ret;
  427. sg->offset += ret;
  428. sg->length -= ret;
  429. size -= ret;
  430. offset += ret;
  431. if (uncharge)
  432. sk_mem_uncharge(sk, ret);
  433. goto retry;
  434. }
  435. return ret;
  436. }
  437. if (apply)
  438. apply_bytes -= ret;
  439. sg->offset += ret;
  440. sg->length -= ret;
  441. if (uncharge)
  442. sk_mem_uncharge(sk, ret);
  443. if (!sg->length) {
  444. put_page(p);
  445. md->sg_start++;
  446. if (md->sg_start == MAX_SKB_FRAGS)
  447. md->sg_start = 0;
  448. sg_init_table(sg, 1);
  449. if (md->sg_start == md->sg_end)
  450. break;
  451. }
  452. if (apply && !apply_bytes)
  453. break;
  454. }
  455. return 0;
  456. }
  457. static inline void bpf_compute_data_pointers_sg(struct sk_msg_buff *md)
  458. {
  459. struct scatterlist *sg = md->sg_data + md->sg_start;
  460. if (md->sg_copy[md->sg_start]) {
  461. md->data = md->data_end = 0;
  462. } else {
  463. md->data = sg_virt(sg);
  464. md->data_end = md->data + sg->length;
  465. }
  466. }
  467. static void return_mem_sg(struct sock *sk, int bytes, struct sk_msg_buff *md)
  468. {
  469. struct scatterlist *sg = md->sg_data;
  470. int i = md->sg_start;
  471. do {
  472. int uncharge = (bytes < sg[i].length) ? bytes : sg[i].length;
  473. sk_mem_uncharge(sk, uncharge);
  474. bytes -= uncharge;
  475. if (!bytes)
  476. break;
  477. i++;
  478. if (i == MAX_SKB_FRAGS)
  479. i = 0;
  480. } while (i != md->sg_end);
  481. }
  482. static void free_bytes_sg(struct sock *sk, int bytes,
  483. struct sk_msg_buff *md, bool charge)
  484. {
  485. struct scatterlist *sg = md->sg_data;
  486. int i = md->sg_start, free;
  487. while (bytes && sg[i].length) {
  488. free = sg[i].length;
  489. if (bytes < free) {
  490. sg[i].length -= bytes;
  491. sg[i].offset += bytes;
  492. if (charge)
  493. sk_mem_uncharge(sk, bytes);
  494. break;
  495. }
  496. if (charge)
  497. sk_mem_uncharge(sk, sg[i].length);
  498. put_page(sg_page(&sg[i]));
  499. bytes -= sg[i].length;
  500. sg[i].length = 0;
  501. sg[i].page_link = 0;
  502. sg[i].offset = 0;
  503. i++;
  504. if (i == MAX_SKB_FRAGS)
  505. i = 0;
  506. }
  507. md->sg_start = i;
  508. }
  509. static int free_sg(struct sock *sk, int start,
  510. struct sk_msg_buff *md, bool charge)
  511. {
  512. struct scatterlist *sg = md->sg_data;
  513. int i = start, free = 0;
  514. while (sg[i].length) {
  515. free += sg[i].length;
  516. if (charge)
  517. sk_mem_uncharge(sk, sg[i].length);
  518. if (!md->skb)
  519. put_page(sg_page(&sg[i]));
  520. sg[i].length = 0;
  521. sg[i].page_link = 0;
  522. sg[i].offset = 0;
  523. i++;
  524. if (i == MAX_SKB_FRAGS)
  525. i = 0;
  526. }
  527. if (md->skb)
  528. consume_skb(md->skb);
  529. return free;
  530. }
  531. static int free_start_sg(struct sock *sk, struct sk_msg_buff *md, bool charge)
  532. {
  533. int free = free_sg(sk, md->sg_start, md, charge);
  534. md->sg_start = md->sg_end;
  535. return free;
  536. }
  537. static int free_curr_sg(struct sock *sk, struct sk_msg_buff *md)
  538. {
  539. return free_sg(sk, md->sg_curr, md, true);
  540. }
  541. static int bpf_map_msg_verdict(int _rc, struct sk_msg_buff *md)
  542. {
  543. return ((_rc == SK_PASS) ?
  544. (md->sk_redir ? __SK_REDIRECT : __SK_PASS) :
  545. __SK_DROP);
  546. }
  547. static unsigned int smap_do_tx_msg(struct sock *sk,
  548. struct smap_psock *psock,
  549. struct sk_msg_buff *md)
  550. {
  551. struct bpf_prog *prog;
  552. unsigned int rc, _rc;
  553. preempt_disable();
  554. rcu_read_lock();
  555. /* If the policy was removed mid-send then default to 'accept' */
  556. prog = READ_ONCE(psock->bpf_tx_msg);
  557. if (unlikely(!prog)) {
  558. _rc = SK_PASS;
  559. goto verdict;
  560. }
  561. bpf_compute_data_pointers_sg(md);
  562. md->sk = sk;
  563. rc = (*prog->bpf_func)(md, prog->insnsi);
  564. psock->apply_bytes = md->apply_bytes;
  565. /* Moving return codes from UAPI namespace into internal namespace */
  566. _rc = bpf_map_msg_verdict(rc, md);
  567. /* The psock has a refcount on the sock but not on the map and because
  568. * we need to drop rcu read lock here its possible the map could be
  569. * removed between here and when we need it to execute the sock
  570. * redirect. So do the map lookup now for future use.
  571. */
  572. if (_rc == __SK_REDIRECT) {
  573. if (psock->sk_redir)
  574. sock_put(psock->sk_redir);
  575. psock->sk_redir = do_msg_redirect_map(md);
  576. if (!psock->sk_redir) {
  577. _rc = __SK_DROP;
  578. goto verdict;
  579. }
  580. sock_hold(psock->sk_redir);
  581. }
  582. verdict:
  583. rcu_read_unlock();
  584. preempt_enable();
  585. return _rc;
  586. }
  587. static int bpf_tcp_ingress(struct sock *sk, int apply_bytes,
  588. struct smap_psock *psock,
  589. struct sk_msg_buff *md, int flags)
  590. {
  591. bool apply = apply_bytes;
  592. size_t size, copied = 0;
  593. struct sk_msg_buff *r;
  594. int err = 0, i;
  595. r = kzalloc(sizeof(struct sk_msg_buff), __GFP_NOWARN | GFP_KERNEL);
  596. if (unlikely(!r))
  597. return -ENOMEM;
  598. lock_sock(sk);
  599. r->sg_start = md->sg_start;
  600. i = md->sg_start;
  601. do {
  602. size = (apply && apply_bytes < md->sg_data[i].length) ?
  603. apply_bytes : md->sg_data[i].length;
  604. if (!sk_wmem_schedule(sk, size)) {
  605. if (!copied)
  606. err = -ENOMEM;
  607. break;
  608. }
  609. sk_mem_charge(sk, size);
  610. r->sg_data[i] = md->sg_data[i];
  611. r->sg_data[i].length = size;
  612. md->sg_data[i].length -= size;
  613. md->sg_data[i].offset += size;
  614. copied += size;
  615. if (md->sg_data[i].length) {
  616. get_page(sg_page(&r->sg_data[i]));
  617. r->sg_end = (i + 1) == MAX_SKB_FRAGS ? 0 : i + 1;
  618. } else {
  619. i++;
  620. if (i == MAX_SKB_FRAGS)
  621. i = 0;
  622. r->sg_end = i;
  623. }
  624. if (apply) {
  625. apply_bytes -= size;
  626. if (!apply_bytes)
  627. break;
  628. }
  629. } while (i != md->sg_end);
  630. md->sg_start = i;
  631. if (!err) {
  632. list_add_tail(&r->list, &psock->ingress);
  633. sk->sk_data_ready(sk);
  634. } else {
  635. free_start_sg(sk, r, true);
  636. kfree(r);
  637. }
  638. release_sock(sk);
  639. return err;
  640. }
  641. static int bpf_tcp_sendmsg_do_redirect(struct sock *sk, int send,
  642. struct sk_msg_buff *md,
  643. int flags)
  644. {
  645. bool ingress = !!(md->flags & BPF_F_INGRESS);
  646. struct smap_psock *psock;
  647. int err = 0;
  648. rcu_read_lock();
  649. psock = smap_psock_sk(sk);
  650. if (unlikely(!psock))
  651. goto out_rcu;
  652. if (!refcount_inc_not_zero(&psock->refcnt))
  653. goto out_rcu;
  654. rcu_read_unlock();
  655. if (ingress) {
  656. err = bpf_tcp_ingress(sk, send, psock, md, flags);
  657. } else {
  658. lock_sock(sk);
  659. err = bpf_tcp_push(sk, send, md, flags, false);
  660. release_sock(sk);
  661. }
  662. smap_release_sock(psock, sk);
  663. return err;
  664. out_rcu:
  665. rcu_read_unlock();
  666. return 0;
  667. }
  668. static inline void bpf_md_init(struct smap_psock *psock)
  669. {
  670. if (!psock->apply_bytes) {
  671. psock->eval = __SK_NONE;
  672. if (psock->sk_redir) {
  673. sock_put(psock->sk_redir);
  674. psock->sk_redir = NULL;
  675. }
  676. }
  677. }
  678. static void apply_bytes_dec(struct smap_psock *psock, int i)
  679. {
  680. if (psock->apply_bytes) {
  681. if (psock->apply_bytes < i)
  682. psock->apply_bytes = 0;
  683. else
  684. psock->apply_bytes -= i;
  685. }
  686. }
  687. static int bpf_exec_tx_verdict(struct smap_psock *psock,
  688. struct sk_msg_buff *m,
  689. struct sock *sk,
  690. int *copied, int flags)
  691. {
  692. bool cork = false, enospc = (m->sg_start == m->sg_end);
  693. struct sock *redir;
  694. int err = 0;
  695. int send;
  696. more_data:
  697. if (psock->eval == __SK_NONE)
  698. psock->eval = smap_do_tx_msg(sk, psock, m);
  699. if (m->cork_bytes &&
  700. m->cork_bytes > psock->sg_size && !enospc) {
  701. psock->cork_bytes = m->cork_bytes - psock->sg_size;
  702. if (!psock->cork) {
  703. psock->cork = kcalloc(1,
  704. sizeof(struct sk_msg_buff),
  705. GFP_ATOMIC | __GFP_NOWARN);
  706. if (!psock->cork) {
  707. err = -ENOMEM;
  708. goto out_err;
  709. }
  710. }
  711. memcpy(psock->cork, m, sizeof(*m));
  712. goto out_err;
  713. }
  714. send = psock->sg_size;
  715. if (psock->apply_bytes && psock->apply_bytes < send)
  716. send = psock->apply_bytes;
  717. switch (psock->eval) {
  718. case __SK_PASS:
  719. err = bpf_tcp_push(sk, send, m, flags, true);
  720. if (unlikely(err)) {
  721. *copied -= free_start_sg(sk, m, true);
  722. break;
  723. }
  724. apply_bytes_dec(psock, send);
  725. psock->sg_size -= send;
  726. break;
  727. case __SK_REDIRECT:
  728. redir = psock->sk_redir;
  729. apply_bytes_dec(psock, send);
  730. if (psock->cork) {
  731. cork = true;
  732. psock->cork = NULL;
  733. }
  734. return_mem_sg(sk, send, m);
  735. release_sock(sk);
  736. err = bpf_tcp_sendmsg_do_redirect(redir, send, m, flags);
  737. lock_sock(sk);
  738. if (unlikely(err < 0)) {
  739. int free = free_start_sg(sk, m, false);
  740. psock->sg_size = 0;
  741. if (!cork)
  742. *copied -= free;
  743. } else {
  744. psock->sg_size -= send;
  745. }
  746. if (cork) {
  747. free_start_sg(sk, m, true);
  748. psock->sg_size = 0;
  749. kfree(m);
  750. m = NULL;
  751. err = 0;
  752. }
  753. break;
  754. case __SK_DROP:
  755. default:
  756. free_bytes_sg(sk, send, m, true);
  757. apply_bytes_dec(psock, send);
  758. *copied -= send;
  759. psock->sg_size -= send;
  760. err = -EACCES;
  761. break;
  762. }
  763. if (likely(!err)) {
  764. bpf_md_init(psock);
  765. if (m &&
  766. m->sg_data[m->sg_start].page_link &&
  767. m->sg_data[m->sg_start].length)
  768. goto more_data;
  769. }
  770. out_err:
  771. return err;
  772. }
  773. static int bpf_wait_data(struct sock *sk,
  774. struct smap_psock *psk, int flags,
  775. long timeo, int *err)
  776. {
  777. int rc;
  778. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  779. add_wait_queue(sk_sleep(sk), &wait);
  780. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  781. rc = sk_wait_event(sk, &timeo,
  782. !list_empty(&psk->ingress) ||
  783. !skb_queue_empty(&sk->sk_receive_queue),
  784. &wait);
  785. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  786. remove_wait_queue(sk_sleep(sk), &wait);
  787. return rc;
  788. }
  789. static int bpf_tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  790. int nonblock, int flags, int *addr_len)
  791. {
  792. struct iov_iter *iter = &msg->msg_iter;
  793. struct smap_psock *psock;
  794. int copied = 0;
  795. if (unlikely(flags & MSG_ERRQUEUE))
  796. return inet_recv_error(sk, msg, len, addr_len);
  797. if (!skb_queue_empty(&sk->sk_receive_queue))
  798. return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len);
  799. rcu_read_lock();
  800. psock = smap_psock_sk(sk);
  801. if (unlikely(!psock))
  802. goto out;
  803. if (unlikely(!refcount_inc_not_zero(&psock->refcnt)))
  804. goto out;
  805. rcu_read_unlock();
  806. lock_sock(sk);
  807. bytes_ready:
  808. while (copied != len) {
  809. struct scatterlist *sg;
  810. struct sk_msg_buff *md;
  811. int i;
  812. md = list_first_entry_or_null(&psock->ingress,
  813. struct sk_msg_buff, list);
  814. if (unlikely(!md))
  815. break;
  816. i = md->sg_start;
  817. do {
  818. struct page *page;
  819. int n, copy;
  820. sg = &md->sg_data[i];
  821. copy = sg->length;
  822. page = sg_page(sg);
  823. if (copied + copy > len)
  824. copy = len - copied;
  825. n = copy_page_to_iter(page, sg->offset, copy, iter);
  826. if (n != copy) {
  827. md->sg_start = i;
  828. release_sock(sk);
  829. smap_release_sock(psock, sk);
  830. return -EFAULT;
  831. }
  832. copied += copy;
  833. sg->offset += copy;
  834. sg->length -= copy;
  835. sk_mem_uncharge(sk, copy);
  836. if (!sg->length) {
  837. i++;
  838. if (i == MAX_SKB_FRAGS)
  839. i = 0;
  840. if (!md->skb)
  841. put_page(page);
  842. }
  843. if (copied == len)
  844. break;
  845. } while (i != md->sg_end);
  846. md->sg_start = i;
  847. if (!sg->length && md->sg_start == md->sg_end) {
  848. list_del(&md->list);
  849. if (md->skb)
  850. consume_skb(md->skb);
  851. kfree(md);
  852. }
  853. }
  854. if (!copied) {
  855. long timeo;
  856. int data;
  857. int err = 0;
  858. timeo = sock_rcvtimeo(sk, nonblock);
  859. data = bpf_wait_data(sk, psock, flags, timeo, &err);
  860. if (data) {
  861. if (!skb_queue_empty(&sk->sk_receive_queue)) {
  862. release_sock(sk);
  863. smap_release_sock(psock, sk);
  864. copied = tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len);
  865. return copied;
  866. }
  867. goto bytes_ready;
  868. }
  869. if (err)
  870. copied = err;
  871. }
  872. release_sock(sk);
  873. smap_release_sock(psock, sk);
  874. return copied;
  875. out:
  876. rcu_read_unlock();
  877. return tcp_recvmsg(sk, msg, len, nonblock, flags, addr_len);
  878. }
  879. static int bpf_tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
  880. {
  881. int flags = msg->msg_flags | MSG_NO_SHARED_FRAGS;
  882. struct sk_msg_buff md = {0};
  883. unsigned int sg_copy = 0;
  884. struct smap_psock *psock;
  885. int copied = 0, err = 0;
  886. struct scatterlist *sg;
  887. long timeo;
  888. /* Its possible a sock event or user removed the psock _but_ the ops
  889. * have not been reprogrammed yet so we get here. In this case fallback
  890. * to tcp_sendmsg. Note this only works because we _only_ ever allow
  891. * a single ULP there is no hierarchy here.
  892. */
  893. rcu_read_lock();
  894. psock = smap_psock_sk(sk);
  895. if (unlikely(!psock)) {
  896. rcu_read_unlock();
  897. return tcp_sendmsg(sk, msg, size);
  898. }
  899. /* Increment the psock refcnt to ensure its not released while sending a
  900. * message. Required because sk lookup and bpf programs are used in
  901. * separate rcu critical sections. Its OK if we lose the map entry
  902. * but we can't lose the sock reference.
  903. */
  904. if (!refcount_inc_not_zero(&psock->refcnt)) {
  905. rcu_read_unlock();
  906. return tcp_sendmsg(sk, msg, size);
  907. }
  908. sg = md.sg_data;
  909. sg_init_marker(sg, MAX_SKB_FRAGS);
  910. rcu_read_unlock();
  911. lock_sock(sk);
  912. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  913. while (msg_data_left(msg)) {
  914. struct sk_msg_buff *m = NULL;
  915. bool enospc = false;
  916. int copy;
  917. if (sk->sk_err) {
  918. err = -sk->sk_err;
  919. goto out_err;
  920. }
  921. copy = msg_data_left(msg);
  922. if (!sk_stream_memory_free(sk))
  923. goto wait_for_sndbuf;
  924. m = psock->cork_bytes ? psock->cork : &md;
  925. m->sg_curr = m->sg_copybreak ? m->sg_curr : m->sg_end;
  926. err = sk_alloc_sg(sk, copy, m->sg_data,
  927. m->sg_start, &m->sg_end, &sg_copy,
  928. m->sg_end - 1);
  929. if (err) {
  930. if (err != -ENOSPC)
  931. goto wait_for_memory;
  932. enospc = true;
  933. copy = sg_copy;
  934. }
  935. err = memcopy_from_iter(sk, m, &msg->msg_iter, copy);
  936. if (err < 0) {
  937. free_curr_sg(sk, m);
  938. goto out_err;
  939. }
  940. psock->sg_size += copy;
  941. copied += copy;
  942. sg_copy = 0;
  943. /* When bytes are being corked skip running BPF program and
  944. * applying verdict unless there is no more buffer space. In
  945. * the ENOSPC case simply run BPF prorgram with currently
  946. * accumulated data. We don't have much choice at this point
  947. * we could try extending the page frags or chaining complex
  948. * frags but even in these cases _eventually_ we will hit an
  949. * OOM scenario. More complex recovery schemes may be
  950. * implemented in the future, but BPF programs must handle
  951. * the case where apply_cork requests are not honored. The
  952. * canonical method to verify this is to check data length.
  953. */
  954. if (psock->cork_bytes) {
  955. if (copy > psock->cork_bytes)
  956. psock->cork_bytes = 0;
  957. else
  958. psock->cork_bytes -= copy;
  959. if (psock->cork_bytes && !enospc)
  960. goto out_cork;
  961. /* All cork bytes accounted for re-run filter */
  962. psock->eval = __SK_NONE;
  963. psock->cork_bytes = 0;
  964. }
  965. err = bpf_exec_tx_verdict(psock, m, sk, &copied, flags);
  966. if (unlikely(err < 0))
  967. goto out_err;
  968. continue;
  969. wait_for_sndbuf:
  970. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  971. wait_for_memory:
  972. err = sk_stream_wait_memory(sk, &timeo);
  973. if (err) {
  974. if (m && m != psock->cork)
  975. free_start_sg(sk, m, true);
  976. goto out_err;
  977. }
  978. }
  979. out_err:
  980. if (err < 0)
  981. err = sk_stream_error(sk, msg->msg_flags, err);
  982. out_cork:
  983. release_sock(sk);
  984. smap_release_sock(psock, sk);
  985. return copied ? copied : err;
  986. }
  987. static int bpf_tcp_sendpage(struct sock *sk, struct page *page,
  988. int offset, size_t size, int flags)
  989. {
  990. struct sk_msg_buff md = {0}, *m = NULL;
  991. int err = 0, copied = 0;
  992. struct smap_psock *psock;
  993. struct scatterlist *sg;
  994. bool enospc = false;
  995. rcu_read_lock();
  996. psock = smap_psock_sk(sk);
  997. if (unlikely(!psock))
  998. goto accept;
  999. if (!refcount_inc_not_zero(&psock->refcnt))
  1000. goto accept;
  1001. rcu_read_unlock();
  1002. lock_sock(sk);
  1003. if (psock->cork_bytes) {
  1004. m = psock->cork;
  1005. sg = &m->sg_data[m->sg_end];
  1006. } else {
  1007. m = &md;
  1008. sg = m->sg_data;
  1009. sg_init_marker(sg, MAX_SKB_FRAGS);
  1010. }
  1011. /* Catch case where ring is full and sendpage is stalled. */
  1012. if (unlikely(m->sg_end == m->sg_start &&
  1013. m->sg_data[m->sg_end].length))
  1014. goto out_err;
  1015. psock->sg_size += size;
  1016. sg_set_page(sg, page, size, offset);
  1017. get_page(page);
  1018. m->sg_copy[m->sg_end] = true;
  1019. sk_mem_charge(sk, size);
  1020. m->sg_end++;
  1021. copied = size;
  1022. if (m->sg_end == MAX_SKB_FRAGS)
  1023. m->sg_end = 0;
  1024. if (m->sg_end == m->sg_start)
  1025. enospc = true;
  1026. if (psock->cork_bytes) {
  1027. if (size > psock->cork_bytes)
  1028. psock->cork_bytes = 0;
  1029. else
  1030. psock->cork_bytes -= size;
  1031. if (psock->cork_bytes && !enospc)
  1032. goto out_err;
  1033. /* All cork bytes accounted for re-run filter */
  1034. psock->eval = __SK_NONE;
  1035. psock->cork_bytes = 0;
  1036. }
  1037. err = bpf_exec_tx_verdict(psock, m, sk, &copied, flags);
  1038. out_err:
  1039. release_sock(sk);
  1040. smap_release_sock(psock, sk);
  1041. return copied ? copied : err;
  1042. accept:
  1043. rcu_read_unlock();
  1044. return tcp_sendpage(sk, page, offset, size, flags);
  1045. }
  1046. static void bpf_tcp_msg_add(struct smap_psock *psock,
  1047. struct sock *sk,
  1048. struct bpf_prog *tx_msg)
  1049. {
  1050. struct bpf_prog *orig_tx_msg;
  1051. orig_tx_msg = xchg(&psock->bpf_tx_msg, tx_msg);
  1052. if (orig_tx_msg)
  1053. bpf_prog_put(orig_tx_msg);
  1054. }
  1055. static int bpf_tcp_ulp_register(void)
  1056. {
  1057. build_protos(bpf_tcp_prots[SOCKMAP_IPV4], &tcp_prot);
  1058. /* Once BPF TX ULP is registered it is never unregistered. It
  1059. * will be in the ULP list for the lifetime of the system. Doing
  1060. * duplicate registers is not a problem.
  1061. */
  1062. return tcp_register_ulp(&bpf_tcp_ulp_ops);
  1063. }
  1064. static int smap_verdict_func(struct smap_psock *psock, struct sk_buff *skb)
  1065. {
  1066. struct bpf_prog *prog = READ_ONCE(psock->bpf_verdict);
  1067. int rc;
  1068. if (unlikely(!prog))
  1069. return __SK_DROP;
  1070. skb_orphan(skb);
  1071. /* We need to ensure that BPF metadata for maps is also cleared
  1072. * when we orphan the skb so that we don't have the possibility
  1073. * to reference a stale map.
  1074. */
  1075. TCP_SKB_CB(skb)->bpf.sk_redir = NULL;
  1076. skb->sk = psock->sock;
  1077. bpf_compute_data_end_sk_skb(skb);
  1078. preempt_disable();
  1079. rc = (*prog->bpf_func)(skb, prog->insnsi);
  1080. preempt_enable();
  1081. skb->sk = NULL;
  1082. /* Moving return codes from UAPI namespace into internal namespace */
  1083. return rc == SK_PASS ?
  1084. (TCP_SKB_CB(skb)->bpf.sk_redir ? __SK_REDIRECT : __SK_PASS) :
  1085. __SK_DROP;
  1086. }
  1087. static int smap_do_ingress(struct smap_psock *psock, struct sk_buff *skb)
  1088. {
  1089. struct sock *sk = psock->sock;
  1090. int copied = 0, num_sg;
  1091. struct sk_msg_buff *r;
  1092. r = kzalloc(sizeof(struct sk_msg_buff), __GFP_NOWARN | GFP_ATOMIC);
  1093. if (unlikely(!r))
  1094. return -EAGAIN;
  1095. if (!sk_rmem_schedule(sk, skb, skb->len)) {
  1096. kfree(r);
  1097. return -EAGAIN;
  1098. }
  1099. sg_init_table(r->sg_data, MAX_SKB_FRAGS);
  1100. num_sg = skb_to_sgvec(skb, r->sg_data, 0, skb->len);
  1101. if (unlikely(num_sg < 0)) {
  1102. kfree(r);
  1103. return num_sg;
  1104. }
  1105. sk_mem_charge(sk, skb->len);
  1106. copied = skb->len;
  1107. r->sg_start = 0;
  1108. r->sg_end = num_sg == MAX_SKB_FRAGS ? 0 : num_sg;
  1109. r->skb = skb;
  1110. list_add_tail(&r->list, &psock->ingress);
  1111. sk->sk_data_ready(sk);
  1112. return copied;
  1113. }
  1114. static void smap_do_verdict(struct smap_psock *psock, struct sk_buff *skb)
  1115. {
  1116. struct smap_psock *peer;
  1117. struct sock *sk;
  1118. __u32 in;
  1119. int rc;
  1120. rc = smap_verdict_func(psock, skb);
  1121. switch (rc) {
  1122. case __SK_REDIRECT:
  1123. sk = do_sk_redirect_map(skb);
  1124. if (!sk) {
  1125. kfree_skb(skb);
  1126. break;
  1127. }
  1128. peer = smap_psock_sk(sk);
  1129. in = (TCP_SKB_CB(skb)->bpf.flags) & BPF_F_INGRESS;
  1130. if (unlikely(!peer || sock_flag(sk, SOCK_DEAD) ||
  1131. !test_bit(SMAP_TX_RUNNING, &peer->state))) {
  1132. kfree_skb(skb);
  1133. break;
  1134. }
  1135. if (!in && sock_writeable(sk)) {
  1136. skb_set_owner_w(skb, sk);
  1137. skb_queue_tail(&peer->rxqueue, skb);
  1138. schedule_work(&peer->tx_work);
  1139. break;
  1140. } else if (in &&
  1141. atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
  1142. skb_queue_tail(&peer->rxqueue, skb);
  1143. schedule_work(&peer->tx_work);
  1144. break;
  1145. }
  1146. /* Fall through and free skb otherwise */
  1147. case __SK_DROP:
  1148. default:
  1149. kfree_skb(skb);
  1150. }
  1151. }
  1152. static void smap_report_sk_error(struct smap_psock *psock, int err)
  1153. {
  1154. struct sock *sk = psock->sock;
  1155. sk->sk_err = err;
  1156. sk->sk_error_report(sk);
  1157. }
  1158. static void smap_read_sock_strparser(struct strparser *strp,
  1159. struct sk_buff *skb)
  1160. {
  1161. struct smap_psock *psock;
  1162. rcu_read_lock();
  1163. psock = container_of(strp, struct smap_psock, strp);
  1164. smap_do_verdict(psock, skb);
  1165. rcu_read_unlock();
  1166. }
  1167. /* Called with lock held on socket */
  1168. static void smap_data_ready(struct sock *sk)
  1169. {
  1170. struct smap_psock *psock;
  1171. rcu_read_lock();
  1172. psock = smap_psock_sk(sk);
  1173. if (likely(psock)) {
  1174. write_lock_bh(&sk->sk_callback_lock);
  1175. strp_data_ready(&psock->strp);
  1176. write_unlock_bh(&sk->sk_callback_lock);
  1177. }
  1178. rcu_read_unlock();
  1179. }
  1180. static void smap_tx_work(struct work_struct *w)
  1181. {
  1182. struct smap_psock *psock;
  1183. struct sk_buff *skb;
  1184. int rem, off, n;
  1185. psock = container_of(w, struct smap_psock, tx_work);
  1186. /* lock sock to avoid losing sk_socket at some point during loop */
  1187. lock_sock(psock->sock);
  1188. if (psock->save_skb) {
  1189. skb = psock->save_skb;
  1190. rem = psock->save_rem;
  1191. off = psock->save_off;
  1192. psock->save_skb = NULL;
  1193. goto start;
  1194. }
  1195. while ((skb = skb_dequeue(&psock->rxqueue))) {
  1196. __u32 flags;
  1197. rem = skb->len;
  1198. off = 0;
  1199. start:
  1200. flags = (TCP_SKB_CB(skb)->bpf.flags) & BPF_F_INGRESS;
  1201. do {
  1202. if (likely(psock->sock->sk_socket)) {
  1203. if (flags)
  1204. n = smap_do_ingress(psock, skb);
  1205. else
  1206. n = skb_send_sock_locked(psock->sock,
  1207. skb, off, rem);
  1208. } else {
  1209. n = -EINVAL;
  1210. }
  1211. if (n <= 0) {
  1212. if (n == -EAGAIN) {
  1213. /* Retry when space is available */
  1214. psock->save_skb = skb;
  1215. psock->save_rem = rem;
  1216. psock->save_off = off;
  1217. goto out;
  1218. }
  1219. /* Hard errors break pipe and stop xmit */
  1220. smap_report_sk_error(psock, n ? -n : EPIPE);
  1221. clear_bit(SMAP_TX_RUNNING, &psock->state);
  1222. kfree_skb(skb);
  1223. goto out;
  1224. }
  1225. rem -= n;
  1226. off += n;
  1227. } while (rem);
  1228. if (!flags)
  1229. kfree_skb(skb);
  1230. }
  1231. out:
  1232. release_sock(psock->sock);
  1233. }
  1234. static void smap_write_space(struct sock *sk)
  1235. {
  1236. struct smap_psock *psock;
  1237. void (*write_space)(struct sock *sk);
  1238. rcu_read_lock();
  1239. psock = smap_psock_sk(sk);
  1240. if (likely(psock && test_bit(SMAP_TX_RUNNING, &psock->state)))
  1241. schedule_work(&psock->tx_work);
  1242. write_space = psock->save_write_space;
  1243. rcu_read_unlock();
  1244. write_space(sk);
  1245. }
  1246. static void smap_stop_sock(struct smap_psock *psock, struct sock *sk)
  1247. {
  1248. if (!psock->strp_enabled)
  1249. return;
  1250. sk->sk_data_ready = psock->save_data_ready;
  1251. sk->sk_write_space = psock->save_write_space;
  1252. psock->save_data_ready = NULL;
  1253. psock->save_write_space = NULL;
  1254. strp_stop(&psock->strp);
  1255. psock->strp_enabled = false;
  1256. }
  1257. static void smap_destroy_psock(struct rcu_head *rcu)
  1258. {
  1259. struct smap_psock *psock = container_of(rcu,
  1260. struct smap_psock, rcu);
  1261. /* Now that a grace period has passed there is no longer
  1262. * any reference to this sock in the sockmap so we can
  1263. * destroy the psock, strparser, and bpf programs. But,
  1264. * because we use workqueue sync operations we can not
  1265. * do it in rcu context
  1266. */
  1267. schedule_work(&psock->gc_work);
  1268. }
  1269. static bool psock_is_smap_sk(struct sock *sk)
  1270. {
  1271. return inet_csk(sk)->icsk_ulp_ops == &bpf_tcp_ulp_ops;
  1272. }
  1273. static void smap_release_sock(struct smap_psock *psock, struct sock *sock)
  1274. {
  1275. if (refcount_dec_and_test(&psock->refcnt)) {
  1276. if (psock_is_smap_sk(sock))
  1277. tcp_cleanup_ulp(sock);
  1278. write_lock_bh(&sock->sk_callback_lock);
  1279. smap_stop_sock(psock, sock);
  1280. write_unlock_bh(&sock->sk_callback_lock);
  1281. clear_bit(SMAP_TX_RUNNING, &psock->state);
  1282. rcu_assign_sk_user_data(sock, NULL);
  1283. call_rcu_sched(&psock->rcu, smap_destroy_psock);
  1284. }
  1285. }
  1286. static int smap_parse_func_strparser(struct strparser *strp,
  1287. struct sk_buff *skb)
  1288. {
  1289. struct smap_psock *psock;
  1290. struct bpf_prog *prog;
  1291. int rc;
  1292. rcu_read_lock();
  1293. psock = container_of(strp, struct smap_psock, strp);
  1294. prog = READ_ONCE(psock->bpf_parse);
  1295. if (unlikely(!prog)) {
  1296. rcu_read_unlock();
  1297. return skb->len;
  1298. }
  1299. /* Attach socket for bpf program to use if needed we can do this
  1300. * because strparser clones the skb before handing it to a upper
  1301. * layer, meaning skb_orphan has been called. We NULL sk on the
  1302. * way out to ensure we don't trigger a BUG_ON in skb/sk operations
  1303. * later and because we are not charging the memory of this skb to
  1304. * any socket yet.
  1305. */
  1306. skb->sk = psock->sock;
  1307. bpf_compute_data_end_sk_skb(skb);
  1308. rc = (*prog->bpf_func)(skb, prog->insnsi);
  1309. skb->sk = NULL;
  1310. rcu_read_unlock();
  1311. return rc;
  1312. }
  1313. static int smap_read_sock_done(struct strparser *strp, int err)
  1314. {
  1315. return err;
  1316. }
  1317. static int smap_init_sock(struct smap_psock *psock,
  1318. struct sock *sk)
  1319. {
  1320. static const struct strp_callbacks cb = {
  1321. .rcv_msg = smap_read_sock_strparser,
  1322. .parse_msg = smap_parse_func_strparser,
  1323. .read_sock_done = smap_read_sock_done,
  1324. };
  1325. return strp_init(&psock->strp, sk, &cb);
  1326. }
  1327. static void smap_init_progs(struct smap_psock *psock,
  1328. struct bpf_prog *verdict,
  1329. struct bpf_prog *parse)
  1330. {
  1331. struct bpf_prog *orig_parse, *orig_verdict;
  1332. orig_parse = xchg(&psock->bpf_parse, parse);
  1333. orig_verdict = xchg(&psock->bpf_verdict, verdict);
  1334. if (orig_verdict)
  1335. bpf_prog_put(orig_verdict);
  1336. if (orig_parse)
  1337. bpf_prog_put(orig_parse);
  1338. }
  1339. static void smap_start_sock(struct smap_psock *psock, struct sock *sk)
  1340. {
  1341. if (sk->sk_data_ready == smap_data_ready)
  1342. return;
  1343. psock->save_data_ready = sk->sk_data_ready;
  1344. psock->save_write_space = sk->sk_write_space;
  1345. sk->sk_data_ready = smap_data_ready;
  1346. sk->sk_write_space = smap_write_space;
  1347. psock->strp_enabled = true;
  1348. }
  1349. static void sock_map_remove_complete(struct bpf_stab *stab)
  1350. {
  1351. bpf_map_area_free(stab->sock_map);
  1352. kfree(stab);
  1353. }
  1354. static void smap_gc_work(struct work_struct *w)
  1355. {
  1356. struct smap_psock_map_entry *e, *tmp;
  1357. struct sk_msg_buff *md, *mtmp;
  1358. struct smap_psock *psock;
  1359. psock = container_of(w, struct smap_psock, gc_work);
  1360. /* no callback lock needed because we already detached sockmap ops */
  1361. if (psock->strp_enabled)
  1362. strp_done(&psock->strp);
  1363. cancel_work_sync(&psock->tx_work);
  1364. __skb_queue_purge(&psock->rxqueue);
  1365. /* At this point all strparser and xmit work must be complete */
  1366. if (psock->bpf_parse)
  1367. bpf_prog_put(psock->bpf_parse);
  1368. if (psock->bpf_verdict)
  1369. bpf_prog_put(psock->bpf_verdict);
  1370. if (psock->bpf_tx_msg)
  1371. bpf_prog_put(psock->bpf_tx_msg);
  1372. if (psock->cork) {
  1373. free_start_sg(psock->sock, psock->cork, true);
  1374. kfree(psock->cork);
  1375. }
  1376. list_for_each_entry_safe(md, mtmp, &psock->ingress, list) {
  1377. list_del(&md->list);
  1378. free_start_sg(psock->sock, md, true);
  1379. kfree(md);
  1380. }
  1381. list_for_each_entry_safe(e, tmp, &psock->maps, list) {
  1382. list_del(&e->list);
  1383. kfree(e);
  1384. }
  1385. if (psock->sk_redir)
  1386. sock_put(psock->sk_redir);
  1387. sock_put(psock->sock);
  1388. kfree(psock);
  1389. }
  1390. static struct smap_psock *smap_init_psock(struct sock *sock, int node)
  1391. {
  1392. struct smap_psock *psock;
  1393. psock = kzalloc_node(sizeof(struct smap_psock),
  1394. GFP_ATOMIC | __GFP_NOWARN,
  1395. node);
  1396. if (!psock)
  1397. return ERR_PTR(-ENOMEM);
  1398. psock->eval = __SK_NONE;
  1399. psock->sock = sock;
  1400. skb_queue_head_init(&psock->rxqueue);
  1401. INIT_WORK(&psock->tx_work, smap_tx_work);
  1402. INIT_WORK(&psock->gc_work, smap_gc_work);
  1403. INIT_LIST_HEAD(&psock->maps);
  1404. INIT_LIST_HEAD(&psock->ingress);
  1405. refcount_set(&psock->refcnt, 1);
  1406. spin_lock_init(&psock->maps_lock);
  1407. rcu_assign_sk_user_data(sock, psock);
  1408. sock_hold(sock);
  1409. return psock;
  1410. }
  1411. static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
  1412. {
  1413. struct bpf_stab *stab;
  1414. u64 cost;
  1415. int err;
  1416. if (!capable(CAP_NET_ADMIN))
  1417. return ERR_PTR(-EPERM);
  1418. /* check sanity of attributes */
  1419. if (attr->max_entries == 0 || attr->key_size != 4 ||
  1420. attr->value_size != 4 || attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
  1421. return ERR_PTR(-EINVAL);
  1422. err = bpf_tcp_ulp_register();
  1423. if (err && err != -EEXIST)
  1424. return ERR_PTR(err);
  1425. stab = kzalloc(sizeof(*stab), GFP_USER);
  1426. if (!stab)
  1427. return ERR_PTR(-ENOMEM);
  1428. bpf_map_init_from_attr(&stab->map, attr);
  1429. raw_spin_lock_init(&stab->lock);
  1430. /* make sure page count doesn't overflow */
  1431. cost = (u64) stab->map.max_entries * sizeof(struct sock *);
  1432. err = -EINVAL;
  1433. if (cost >= U32_MAX - PAGE_SIZE)
  1434. goto free_stab;
  1435. stab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
  1436. /* if map size is larger than memlock limit, reject it early */
  1437. err = bpf_map_precharge_memlock(stab->map.pages);
  1438. if (err)
  1439. goto free_stab;
  1440. err = -ENOMEM;
  1441. stab->sock_map = bpf_map_area_alloc(stab->map.max_entries *
  1442. sizeof(struct sock *),
  1443. stab->map.numa_node);
  1444. if (!stab->sock_map)
  1445. goto free_stab;
  1446. return &stab->map;
  1447. free_stab:
  1448. kfree(stab);
  1449. return ERR_PTR(err);
  1450. }
  1451. static void smap_list_map_remove(struct smap_psock *psock,
  1452. struct sock **entry)
  1453. {
  1454. struct smap_psock_map_entry *e, *tmp;
  1455. spin_lock_bh(&psock->maps_lock);
  1456. list_for_each_entry_safe(e, tmp, &psock->maps, list) {
  1457. if (e->entry == entry) {
  1458. list_del(&e->list);
  1459. kfree(e);
  1460. }
  1461. }
  1462. spin_unlock_bh(&psock->maps_lock);
  1463. }
  1464. static void smap_list_hash_remove(struct smap_psock *psock,
  1465. struct htab_elem *hash_link)
  1466. {
  1467. struct smap_psock_map_entry *e, *tmp;
  1468. spin_lock_bh(&psock->maps_lock);
  1469. list_for_each_entry_safe(e, tmp, &psock->maps, list) {
  1470. struct htab_elem *c = rcu_dereference(e->hash_link);
  1471. if (c == hash_link) {
  1472. list_del(&e->list);
  1473. kfree(e);
  1474. }
  1475. }
  1476. spin_unlock_bh(&psock->maps_lock);
  1477. }
  1478. static void sock_map_free(struct bpf_map *map)
  1479. {
  1480. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  1481. int i;
  1482. synchronize_rcu();
  1483. /* At this point no update, lookup or delete operations can happen.
  1484. * However, be aware we can still get a socket state event updates,
  1485. * and data ready callabacks that reference the psock from sk_user_data
  1486. * Also psock worker threads are still in-flight. So smap_release_sock
  1487. * will only free the psock after cancel_sync on the worker threads
  1488. * and a grace period expire to ensure psock is really safe to remove.
  1489. */
  1490. rcu_read_lock();
  1491. raw_spin_lock_bh(&stab->lock);
  1492. for (i = 0; i < stab->map.max_entries; i++) {
  1493. struct smap_psock *psock;
  1494. struct sock *sock;
  1495. sock = stab->sock_map[i];
  1496. if (!sock)
  1497. continue;
  1498. stab->sock_map[i] = NULL;
  1499. psock = smap_psock_sk(sock);
  1500. /* This check handles a racing sock event that can get the
  1501. * sk_callback_lock before this case but after xchg happens
  1502. * causing the refcnt to hit zero and sock user data (psock)
  1503. * to be null and queued for garbage collection.
  1504. */
  1505. if (likely(psock)) {
  1506. smap_list_map_remove(psock, &stab->sock_map[i]);
  1507. smap_release_sock(psock, sock);
  1508. }
  1509. }
  1510. raw_spin_unlock_bh(&stab->lock);
  1511. rcu_read_unlock();
  1512. sock_map_remove_complete(stab);
  1513. }
  1514. static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  1515. {
  1516. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  1517. u32 i = key ? *(u32 *)key : U32_MAX;
  1518. u32 *next = (u32 *)next_key;
  1519. if (i >= stab->map.max_entries) {
  1520. *next = 0;
  1521. return 0;
  1522. }
  1523. if (i == stab->map.max_entries - 1)
  1524. return -ENOENT;
  1525. *next = i + 1;
  1526. return 0;
  1527. }
  1528. struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
  1529. {
  1530. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  1531. if (key >= map->max_entries)
  1532. return NULL;
  1533. return READ_ONCE(stab->sock_map[key]);
  1534. }
  1535. static int sock_map_delete_elem(struct bpf_map *map, void *key)
  1536. {
  1537. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  1538. struct smap_psock *psock;
  1539. int k = *(u32 *)key;
  1540. struct sock *sock;
  1541. if (k >= map->max_entries)
  1542. return -EINVAL;
  1543. raw_spin_lock_bh(&stab->lock);
  1544. sock = stab->sock_map[k];
  1545. stab->sock_map[k] = NULL;
  1546. raw_spin_unlock_bh(&stab->lock);
  1547. if (!sock)
  1548. return -EINVAL;
  1549. psock = smap_psock_sk(sock);
  1550. if (!psock)
  1551. return 0;
  1552. if (psock->bpf_parse) {
  1553. write_lock_bh(&sock->sk_callback_lock);
  1554. smap_stop_sock(psock, sock);
  1555. write_unlock_bh(&sock->sk_callback_lock);
  1556. }
  1557. smap_list_map_remove(psock, &stab->sock_map[k]);
  1558. smap_release_sock(psock, sock);
  1559. return 0;
  1560. }
  1561. /* Locking notes: Concurrent updates, deletes, and lookups are allowed and are
  1562. * done inside rcu critical sections. This ensures on updates that the psock
  1563. * will not be released via smap_release_sock() until concurrent updates/deletes
  1564. * complete. All operations operate on sock_map using cmpxchg and xchg
  1565. * operations to ensure we do not get stale references. Any reads into the
  1566. * map must be done with READ_ONCE() because of this.
  1567. *
  1568. * A psock is destroyed via call_rcu and after any worker threads are cancelled
  1569. * and syncd so we are certain all references from the update/lookup/delete
  1570. * operations as well as references in the data path are no longer in use.
  1571. *
  1572. * Psocks may exist in multiple maps, but only a single set of parse/verdict
  1573. * programs may be inherited from the maps it belongs to. A reference count
  1574. * is kept with the total number of references to the psock from all maps. The
  1575. * psock will not be released until this reaches zero. The psock and sock
  1576. * user data data use the sk_callback_lock to protect critical data structures
  1577. * from concurrent access. This allows us to avoid two updates from modifying
  1578. * the user data in sock and the lock is required anyways for modifying
  1579. * callbacks, we simply increase its scope slightly.
  1580. *
  1581. * Rules to follow,
  1582. * - psock must always be read inside RCU critical section
  1583. * - sk_user_data must only be modified inside sk_callback_lock and read
  1584. * inside RCU critical section.
  1585. * - psock->maps list must only be read & modified inside sk_callback_lock
  1586. * - sock_map must use READ_ONCE and (cmp)xchg operations
  1587. * - BPF verdict/parse programs must use READ_ONCE and xchg operations
  1588. */
  1589. static int __sock_map_ctx_update_elem(struct bpf_map *map,
  1590. struct bpf_sock_progs *progs,
  1591. struct sock *sock,
  1592. void *key)
  1593. {
  1594. struct bpf_prog *verdict, *parse, *tx_msg;
  1595. struct smap_psock *psock;
  1596. bool new = false;
  1597. int err = 0;
  1598. /* 1. If sock map has BPF programs those will be inherited by the
  1599. * sock being added. If the sock is already attached to BPF programs
  1600. * this results in an error.
  1601. */
  1602. verdict = READ_ONCE(progs->bpf_verdict);
  1603. parse = READ_ONCE(progs->bpf_parse);
  1604. tx_msg = READ_ONCE(progs->bpf_tx_msg);
  1605. if (parse && verdict) {
  1606. /* bpf prog refcnt may be zero if a concurrent attach operation
  1607. * removes the program after the above READ_ONCE() but before
  1608. * we increment the refcnt. If this is the case abort with an
  1609. * error.
  1610. */
  1611. verdict = bpf_prog_inc_not_zero(verdict);
  1612. if (IS_ERR(verdict))
  1613. return PTR_ERR(verdict);
  1614. parse = bpf_prog_inc_not_zero(parse);
  1615. if (IS_ERR(parse)) {
  1616. bpf_prog_put(verdict);
  1617. return PTR_ERR(parse);
  1618. }
  1619. }
  1620. if (tx_msg) {
  1621. tx_msg = bpf_prog_inc_not_zero(tx_msg);
  1622. if (IS_ERR(tx_msg)) {
  1623. if (parse && verdict) {
  1624. bpf_prog_put(parse);
  1625. bpf_prog_put(verdict);
  1626. }
  1627. return PTR_ERR(tx_msg);
  1628. }
  1629. }
  1630. psock = smap_psock_sk(sock);
  1631. /* 2. Do not allow inheriting programs if psock exists and has
  1632. * already inherited programs. This would create confusion on
  1633. * which parser/verdict program is running. If no psock exists
  1634. * create one. Inside sk_callback_lock to ensure concurrent create
  1635. * doesn't update user data.
  1636. */
  1637. if (psock) {
  1638. if (!psock_is_smap_sk(sock)) {
  1639. err = -EBUSY;
  1640. goto out_progs;
  1641. }
  1642. if (READ_ONCE(psock->bpf_parse) && parse) {
  1643. err = -EBUSY;
  1644. goto out_progs;
  1645. }
  1646. if (READ_ONCE(psock->bpf_tx_msg) && tx_msg) {
  1647. err = -EBUSY;
  1648. goto out_progs;
  1649. }
  1650. if (!refcount_inc_not_zero(&psock->refcnt)) {
  1651. err = -EAGAIN;
  1652. goto out_progs;
  1653. }
  1654. } else {
  1655. psock = smap_init_psock(sock, map->numa_node);
  1656. if (IS_ERR(psock)) {
  1657. err = PTR_ERR(psock);
  1658. goto out_progs;
  1659. }
  1660. set_bit(SMAP_TX_RUNNING, &psock->state);
  1661. new = true;
  1662. }
  1663. /* 3. At this point we have a reference to a valid psock that is
  1664. * running. Attach any BPF programs needed.
  1665. */
  1666. if (tx_msg)
  1667. bpf_tcp_msg_add(psock, sock, tx_msg);
  1668. if (new) {
  1669. err = tcp_set_ulp_id(sock, TCP_ULP_BPF);
  1670. if (err)
  1671. goto out_free;
  1672. }
  1673. if (parse && verdict && !psock->strp_enabled) {
  1674. err = smap_init_sock(psock, sock);
  1675. if (err)
  1676. goto out_free;
  1677. smap_init_progs(psock, verdict, parse);
  1678. write_lock_bh(&sock->sk_callback_lock);
  1679. smap_start_sock(psock, sock);
  1680. write_unlock_bh(&sock->sk_callback_lock);
  1681. }
  1682. return err;
  1683. out_free:
  1684. smap_release_sock(psock, sock);
  1685. out_progs:
  1686. if (parse && verdict) {
  1687. bpf_prog_put(parse);
  1688. bpf_prog_put(verdict);
  1689. }
  1690. if (tx_msg)
  1691. bpf_prog_put(tx_msg);
  1692. return err;
  1693. }
  1694. static int sock_map_ctx_update_elem(struct bpf_sock_ops_kern *skops,
  1695. struct bpf_map *map,
  1696. void *key, u64 flags)
  1697. {
  1698. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  1699. struct bpf_sock_progs *progs = &stab->progs;
  1700. struct sock *osock, *sock = skops->sk;
  1701. struct smap_psock_map_entry *e;
  1702. struct smap_psock *psock;
  1703. u32 i = *(u32 *)key;
  1704. int err;
  1705. if (unlikely(flags > BPF_EXIST))
  1706. return -EINVAL;
  1707. if (unlikely(i >= stab->map.max_entries))
  1708. return -E2BIG;
  1709. e = kzalloc(sizeof(*e), GFP_ATOMIC | __GFP_NOWARN);
  1710. if (!e)
  1711. return -ENOMEM;
  1712. err = __sock_map_ctx_update_elem(map, progs, sock, key);
  1713. if (err)
  1714. goto out;
  1715. /* psock guaranteed to be present. */
  1716. psock = smap_psock_sk(sock);
  1717. raw_spin_lock_bh(&stab->lock);
  1718. osock = stab->sock_map[i];
  1719. if (osock && flags == BPF_NOEXIST) {
  1720. err = -EEXIST;
  1721. goto out_unlock;
  1722. }
  1723. if (!osock && flags == BPF_EXIST) {
  1724. err = -ENOENT;
  1725. goto out_unlock;
  1726. }
  1727. e->entry = &stab->sock_map[i];
  1728. e->map = map;
  1729. spin_lock_bh(&psock->maps_lock);
  1730. list_add_tail(&e->list, &psock->maps);
  1731. spin_unlock_bh(&psock->maps_lock);
  1732. stab->sock_map[i] = sock;
  1733. if (osock) {
  1734. psock = smap_psock_sk(osock);
  1735. smap_list_map_remove(psock, &stab->sock_map[i]);
  1736. smap_release_sock(psock, osock);
  1737. }
  1738. raw_spin_unlock_bh(&stab->lock);
  1739. return 0;
  1740. out_unlock:
  1741. smap_release_sock(psock, sock);
  1742. raw_spin_unlock_bh(&stab->lock);
  1743. out:
  1744. kfree(e);
  1745. return err;
  1746. }
  1747. int sock_map_prog(struct bpf_map *map, struct bpf_prog *prog, u32 type)
  1748. {
  1749. struct bpf_sock_progs *progs;
  1750. struct bpf_prog *orig;
  1751. if (map->map_type == BPF_MAP_TYPE_SOCKMAP) {
  1752. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  1753. progs = &stab->progs;
  1754. } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH) {
  1755. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1756. progs = &htab->progs;
  1757. } else {
  1758. return -EINVAL;
  1759. }
  1760. switch (type) {
  1761. case BPF_SK_MSG_VERDICT:
  1762. orig = xchg(&progs->bpf_tx_msg, prog);
  1763. break;
  1764. case BPF_SK_SKB_STREAM_PARSER:
  1765. orig = xchg(&progs->bpf_parse, prog);
  1766. break;
  1767. case BPF_SK_SKB_STREAM_VERDICT:
  1768. orig = xchg(&progs->bpf_verdict, prog);
  1769. break;
  1770. default:
  1771. return -EOPNOTSUPP;
  1772. }
  1773. if (orig)
  1774. bpf_prog_put(orig);
  1775. return 0;
  1776. }
  1777. int sockmap_get_from_fd(const union bpf_attr *attr, int type,
  1778. struct bpf_prog *prog)
  1779. {
  1780. int ufd = attr->target_fd;
  1781. struct bpf_map *map;
  1782. struct fd f;
  1783. int err;
  1784. f = fdget(ufd);
  1785. map = __bpf_map_get(f);
  1786. if (IS_ERR(map))
  1787. return PTR_ERR(map);
  1788. err = sock_map_prog(map, prog, attr->attach_type);
  1789. fdput(f);
  1790. return err;
  1791. }
  1792. static void *sock_map_lookup(struct bpf_map *map, void *key)
  1793. {
  1794. return NULL;
  1795. }
  1796. static int sock_map_update_elem(struct bpf_map *map,
  1797. void *key, void *value, u64 flags)
  1798. {
  1799. struct bpf_sock_ops_kern skops;
  1800. u32 fd = *(u32 *)value;
  1801. struct socket *socket;
  1802. int err;
  1803. socket = sockfd_lookup(fd, &err);
  1804. if (!socket)
  1805. return err;
  1806. skops.sk = socket->sk;
  1807. if (!skops.sk) {
  1808. fput(socket->file);
  1809. return -EINVAL;
  1810. }
  1811. /* ULPs are currently supported only for TCP sockets in ESTABLISHED
  1812. * state.
  1813. */
  1814. if (skops.sk->sk_type != SOCK_STREAM ||
  1815. skops.sk->sk_protocol != IPPROTO_TCP ||
  1816. skops.sk->sk_state != TCP_ESTABLISHED) {
  1817. fput(socket->file);
  1818. return -EOPNOTSUPP;
  1819. }
  1820. lock_sock(skops.sk);
  1821. preempt_disable();
  1822. rcu_read_lock();
  1823. err = sock_map_ctx_update_elem(&skops, map, key, flags);
  1824. rcu_read_unlock();
  1825. preempt_enable();
  1826. release_sock(skops.sk);
  1827. fput(socket->file);
  1828. return err;
  1829. }
  1830. static void sock_map_release(struct bpf_map *map)
  1831. {
  1832. struct bpf_sock_progs *progs;
  1833. struct bpf_prog *orig;
  1834. if (map->map_type == BPF_MAP_TYPE_SOCKMAP) {
  1835. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  1836. progs = &stab->progs;
  1837. } else {
  1838. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1839. progs = &htab->progs;
  1840. }
  1841. orig = xchg(&progs->bpf_parse, NULL);
  1842. if (orig)
  1843. bpf_prog_put(orig);
  1844. orig = xchg(&progs->bpf_verdict, NULL);
  1845. if (orig)
  1846. bpf_prog_put(orig);
  1847. orig = xchg(&progs->bpf_tx_msg, NULL);
  1848. if (orig)
  1849. bpf_prog_put(orig);
  1850. }
  1851. static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
  1852. {
  1853. struct bpf_htab *htab;
  1854. int i, err;
  1855. u64 cost;
  1856. if (!capable(CAP_NET_ADMIN))
  1857. return ERR_PTR(-EPERM);
  1858. /* check sanity of attributes */
  1859. if (attr->max_entries == 0 ||
  1860. attr->key_size == 0 ||
  1861. attr->value_size != 4 ||
  1862. attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
  1863. return ERR_PTR(-EINVAL);
  1864. if (attr->key_size > MAX_BPF_STACK)
  1865. /* eBPF programs initialize keys on stack, so they cannot be
  1866. * larger than max stack size
  1867. */
  1868. return ERR_PTR(-E2BIG);
  1869. err = bpf_tcp_ulp_register();
  1870. if (err && err != -EEXIST)
  1871. return ERR_PTR(err);
  1872. htab = kzalloc(sizeof(*htab), GFP_USER);
  1873. if (!htab)
  1874. return ERR_PTR(-ENOMEM);
  1875. bpf_map_init_from_attr(&htab->map, attr);
  1876. htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
  1877. htab->elem_size = sizeof(struct htab_elem) +
  1878. round_up(htab->map.key_size, 8);
  1879. err = -EINVAL;
  1880. if (htab->n_buckets == 0 ||
  1881. htab->n_buckets > U32_MAX / sizeof(struct bucket))
  1882. goto free_htab;
  1883. cost = (u64) htab->n_buckets * sizeof(struct bucket) +
  1884. (u64) htab->elem_size * htab->map.max_entries;
  1885. if (cost >= U32_MAX - PAGE_SIZE)
  1886. goto free_htab;
  1887. htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
  1888. err = bpf_map_precharge_memlock(htab->map.pages);
  1889. if (err)
  1890. goto free_htab;
  1891. err = -ENOMEM;
  1892. htab->buckets = bpf_map_area_alloc(
  1893. htab->n_buckets * sizeof(struct bucket),
  1894. htab->map.numa_node);
  1895. if (!htab->buckets)
  1896. goto free_htab;
  1897. for (i = 0; i < htab->n_buckets; i++) {
  1898. INIT_HLIST_HEAD(&htab->buckets[i].head);
  1899. raw_spin_lock_init(&htab->buckets[i].lock);
  1900. }
  1901. return &htab->map;
  1902. free_htab:
  1903. kfree(htab);
  1904. return ERR_PTR(err);
  1905. }
  1906. static void __bpf_htab_free(struct rcu_head *rcu)
  1907. {
  1908. struct bpf_htab *htab;
  1909. htab = container_of(rcu, struct bpf_htab, rcu);
  1910. bpf_map_area_free(htab->buckets);
  1911. kfree(htab);
  1912. }
  1913. static void sock_hash_free(struct bpf_map *map)
  1914. {
  1915. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1916. int i;
  1917. synchronize_rcu();
  1918. /* At this point no update, lookup or delete operations can happen.
  1919. * However, be aware we can still get a socket state event updates,
  1920. * and data ready callabacks that reference the psock from sk_user_data
  1921. * Also psock worker threads are still in-flight. So smap_release_sock
  1922. * will only free the psock after cancel_sync on the worker threads
  1923. * and a grace period expire to ensure psock is really safe to remove.
  1924. */
  1925. rcu_read_lock();
  1926. for (i = 0; i < htab->n_buckets; i++) {
  1927. struct bucket *b = __select_bucket(htab, i);
  1928. struct hlist_head *head;
  1929. struct hlist_node *n;
  1930. struct htab_elem *l;
  1931. raw_spin_lock_bh(&b->lock);
  1932. head = &b->head;
  1933. hlist_for_each_entry_safe(l, n, head, hash_node) {
  1934. struct sock *sock = l->sk;
  1935. struct smap_psock *psock;
  1936. hlist_del_rcu(&l->hash_node);
  1937. psock = smap_psock_sk(sock);
  1938. /* This check handles a racing sock event that can get
  1939. * the sk_callback_lock before this case but after xchg
  1940. * causing the refcnt to hit zero and sock user data
  1941. * (psock) to be null and queued for garbage collection.
  1942. */
  1943. if (likely(psock)) {
  1944. smap_list_hash_remove(psock, l);
  1945. smap_release_sock(psock, sock);
  1946. }
  1947. free_htab_elem(htab, l);
  1948. }
  1949. raw_spin_unlock_bh(&b->lock);
  1950. }
  1951. rcu_read_unlock();
  1952. call_rcu(&htab->rcu, __bpf_htab_free);
  1953. }
  1954. static struct htab_elem *alloc_sock_hash_elem(struct bpf_htab *htab,
  1955. void *key, u32 key_size, u32 hash,
  1956. struct sock *sk,
  1957. struct htab_elem *old_elem)
  1958. {
  1959. struct htab_elem *l_new;
  1960. if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
  1961. if (!old_elem) {
  1962. atomic_dec(&htab->count);
  1963. return ERR_PTR(-E2BIG);
  1964. }
  1965. }
  1966. l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
  1967. htab->map.numa_node);
  1968. if (!l_new) {
  1969. atomic_dec(&htab->count);
  1970. return ERR_PTR(-ENOMEM);
  1971. }
  1972. memcpy(l_new->key, key, key_size);
  1973. l_new->sk = sk;
  1974. l_new->hash = hash;
  1975. return l_new;
  1976. }
  1977. static inline u32 htab_map_hash(const void *key, u32 key_len)
  1978. {
  1979. return jhash(key, key_len, 0);
  1980. }
  1981. static int sock_hash_get_next_key(struct bpf_map *map,
  1982. void *key, void *next_key)
  1983. {
  1984. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  1985. struct htab_elem *l, *next_l;
  1986. struct hlist_head *h;
  1987. u32 hash, key_size;
  1988. int i = 0;
  1989. WARN_ON_ONCE(!rcu_read_lock_held());
  1990. key_size = map->key_size;
  1991. if (!key)
  1992. goto find_first_elem;
  1993. hash = htab_map_hash(key, key_size);
  1994. h = select_bucket(htab, hash);
  1995. l = lookup_elem_raw(h, hash, key, key_size);
  1996. if (!l)
  1997. goto find_first_elem;
  1998. next_l = hlist_entry_safe(
  1999. rcu_dereference_raw(hlist_next_rcu(&l->hash_node)),
  2000. struct htab_elem, hash_node);
  2001. if (next_l) {
  2002. memcpy(next_key, next_l->key, key_size);
  2003. return 0;
  2004. }
  2005. /* no more elements in this hash list, go to the next bucket */
  2006. i = hash & (htab->n_buckets - 1);
  2007. i++;
  2008. find_first_elem:
  2009. /* iterate over buckets */
  2010. for (; i < htab->n_buckets; i++) {
  2011. h = select_bucket(htab, i);
  2012. /* pick first element in the bucket */
  2013. next_l = hlist_entry_safe(
  2014. rcu_dereference_raw(hlist_first_rcu(h)),
  2015. struct htab_elem, hash_node);
  2016. if (next_l) {
  2017. /* if it's not empty, just return it */
  2018. memcpy(next_key, next_l->key, key_size);
  2019. return 0;
  2020. }
  2021. }
  2022. /* iterated over all buckets and all elements */
  2023. return -ENOENT;
  2024. }
  2025. static int sock_hash_ctx_update_elem(struct bpf_sock_ops_kern *skops,
  2026. struct bpf_map *map,
  2027. void *key, u64 map_flags)
  2028. {
  2029. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  2030. struct bpf_sock_progs *progs = &htab->progs;
  2031. struct htab_elem *l_new = NULL, *l_old;
  2032. struct smap_psock_map_entry *e = NULL;
  2033. struct hlist_head *head;
  2034. struct smap_psock *psock;
  2035. u32 key_size, hash;
  2036. struct sock *sock;
  2037. struct bucket *b;
  2038. int err;
  2039. sock = skops->sk;
  2040. if (sock->sk_type != SOCK_STREAM ||
  2041. sock->sk_protocol != IPPROTO_TCP)
  2042. return -EOPNOTSUPP;
  2043. if (unlikely(map_flags > BPF_EXIST))
  2044. return -EINVAL;
  2045. e = kzalloc(sizeof(*e), GFP_ATOMIC | __GFP_NOWARN);
  2046. if (!e)
  2047. return -ENOMEM;
  2048. WARN_ON_ONCE(!rcu_read_lock_held());
  2049. key_size = map->key_size;
  2050. hash = htab_map_hash(key, key_size);
  2051. b = __select_bucket(htab, hash);
  2052. head = &b->head;
  2053. err = __sock_map_ctx_update_elem(map, progs, sock, key);
  2054. if (err)
  2055. goto err;
  2056. /* psock is valid here because otherwise above *ctx_update_elem would
  2057. * have thrown an error. It is safe to skip error check.
  2058. */
  2059. psock = smap_psock_sk(sock);
  2060. raw_spin_lock_bh(&b->lock);
  2061. l_old = lookup_elem_raw(head, hash, key, key_size);
  2062. if (l_old && map_flags == BPF_NOEXIST) {
  2063. err = -EEXIST;
  2064. goto bucket_err;
  2065. }
  2066. if (!l_old && map_flags == BPF_EXIST) {
  2067. err = -ENOENT;
  2068. goto bucket_err;
  2069. }
  2070. l_new = alloc_sock_hash_elem(htab, key, key_size, hash, sock, l_old);
  2071. if (IS_ERR(l_new)) {
  2072. err = PTR_ERR(l_new);
  2073. goto bucket_err;
  2074. }
  2075. rcu_assign_pointer(e->hash_link, l_new);
  2076. e->map = map;
  2077. spin_lock_bh(&psock->maps_lock);
  2078. list_add_tail(&e->list, &psock->maps);
  2079. spin_unlock_bh(&psock->maps_lock);
  2080. /* add new element to the head of the list, so that
  2081. * concurrent search will find it before old elem
  2082. */
  2083. hlist_add_head_rcu(&l_new->hash_node, head);
  2084. if (l_old) {
  2085. psock = smap_psock_sk(l_old->sk);
  2086. hlist_del_rcu(&l_old->hash_node);
  2087. smap_list_hash_remove(psock, l_old);
  2088. smap_release_sock(psock, l_old->sk);
  2089. free_htab_elem(htab, l_old);
  2090. }
  2091. raw_spin_unlock_bh(&b->lock);
  2092. return 0;
  2093. bucket_err:
  2094. smap_release_sock(psock, sock);
  2095. raw_spin_unlock_bh(&b->lock);
  2096. err:
  2097. kfree(e);
  2098. return err;
  2099. }
  2100. static int sock_hash_update_elem(struct bpf_map *map,
  2101. void *key, void *value, u64 flags)
  2102. {
  2103. struct bpf_sock_ops_kern skops;
  2104. u32 fd = *(u32 *)value;
  2105. struct socket *socket;
  2106. int err;
  2107. socket = sockfd_lookup(fd, &err);
  2108. if (!socket)
  2109. return err;
  2110. skops.sk = socket->sk;
  2111. if (!skops.sk) {
  2112. fput(socket->file);
  2113. return -EINVAL;
  2114. }
  2115. /* ULPs are currently supported only for TCP sockets in ESTABLISHED
  2116. * state.
  2117. */
  2118. if (skops.sk->sk_type != SOCK_STREAM ||
  2119. skops.sk->sk_protocol != IPPROTO_TCP ||
  2120. skops.sk->sk_state != TCP_ESTABLISHED) {
  2121. fput(socket->file);
  2122. return -EOPNOTSUPP;
  2123. }
  2124. lock_sock(skops.sk);
  2125. preempt_disable();
  2126. rcu_read_lock();
  2127. err = sock_hash_ctx_update_elem(&skops, map, key, flags);
  2128. rcu_read_unlock();
  2129. preempt_enable();
  2130. release_sock(skops.sk);
  2131. fput(socket->file);
  2132. return err;
  2133. }
  2134. static int sock_hash_delete_elem(struct bpf_map *map, void *key)
  2135. {
  2136. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  2137. struct hlist_head *head;
  2138. struct bucket *b;
  2139. struct htab_elem *l;
  2140. u32 hash, key_size;
  2141. int ret = -ENOENT;
  2142. key_size = map->key_size;
  2143. hash = htab_map_hash(key, key_size);
  2144. b = __select_bucket(htab, hash);
  2145. head = &b->head;
  2146. raw_spin_lock_bh(&b->lock);
  2147. l = lookup_elem_raw(head, hash, key, key_size);
  2148. if (l) {
  2149. struct sock *sock = l->sk;
  2150. struct smap_psock *psock;
  2151. hlist_del_rcu(&l->hash_node);
  2152. psock = smap_psock_sk(sock);
  2153. /* This check handles a racing sock event that can get the
  2154. * sk_callback_lock before this case but after xchg happens
  2155. * causing the refcnt to hit zero and sock user data (psock)
  2156. * to be null and queued for garbage collection.
  2157. */
  2158. if (likely(psock)) {
  2159. smap_list_hash_remove(psock, l);
  2160. smap_release_sock(psock, sock);
  2161. }
  2162. free_htab_elem(htab, l);
  2163. ret = 0;
  2164. }
  2165. raw_spin_unlock_bh(&b->lock);
  2166. return ret;
  2167. }
  2168. struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
  2169. {
  2170. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  2171. struct hlist_head *head;
  2172. struct htab_elem *l;
  2173. u32 key_size, hash;
  2174. struct bucket *b;
  2175. struct sock *sk;
  2176. key_size = map->key_size;
  2177. hash = htab_map_hash(key, key_size);
  2178. b = __select_bucket(htab, hash);
  2179. head = &b->head;
  2180. l = lookup_elem_raw(head, hash, key, key_size);
  2181. sk = l ? l->sk : NULL;
  2182. return sk;
  2183. }
  2184. const struct bpf_map_ops sock_map_ops = {
  2185. .map_alloc = sock_map_alloc,
  2186. .map_free = sock_map_free,
  2187. .map_lookup_elem = sock_map_lookup,
  2188. .map_get_next_key = sock_map_get_next_key,
  2189. .map_update_elem = sock_map_update_elem,
  2190. .map_delete_elem = sock_map_delete_elem,
  2191. .map_release_uref = sock_map_release,
  2192. .map_check_btf = map_check_no_btf,
  2193. };
  2194. const struct bpf_map_ops sock_hash_ops = {
  2195. .map_alloc = sock_hash_alloc,
  2196. .map_free = sock_hash_free,
  2197. .map_lookup_elem = sock_map_lookup,
  2198. .map_get_next_key = sock_hash_get_next_key,
  2199. .map_update_elem = sock_hash_update_elem,
  2200. .map_delete_elem = sock_hash_delete_elem,
  2201. .map_release_uref = sock_map_release,
  2202. .map_check_btf = map_check_no_btf,
  2203. };
  2204. static bool bpf_is_valid_sock_op(struct bpf_sock_ops_kern *ops)
  2205. {
  2206. return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
  2207. ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB;
  2208. }
  2209. BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, bpf_sock,
  2210. struct bpf_map *, map, void *, key, u64, flags)
  2211. {
  2212. WARN_ON_ONCE(!rcu_read_lock_held());
  2213. /* ULPs are currently supported only for TCP sockets in ESTABLISHED
  2214. * state. This checks that the sock ops triggering the update is
  2215. * one indicating we are (or will be soon) in an ESTABLISHED state.
  2216. */
  2217. if (!bpf_is_valid_sock_op(bpf_sock))
  2218. return -EOPNOTSUPP;
  2219. return sock_map_ctx_update_elem(bpf_sock, map, key, flags);
  2220. }
  2221. const struct bpf_func_proto bpf_sock_map_update_proto = {
  2222. .func = bpf_sock_map_update,
  2223. .gpl_only = false,
  2224. .pkt_access = true,
  2225. .ret_type = RET_INTEGER,
  2226. .arg1_type = ARG_PTR_TO_CTX,
  2227. .arg2_type = ARG_CONST_MAP_PTR,
  2228. .arg3_type = ARG_PTR_TO_MAP_KEY,
  2229. .arg4_type = ARG_ANYTHING,
  2230. };
  2231. BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, bpf_sock,
  2232. struct bpf_map *, map, void *, key, u64, flags)
  2233. {
  2234. WARN_ON_ONCE(!rcu_read_lock_held());
  2235. if (!bpf_is_valid_sock_op(bpf_sock))
  2236. return -EOPNOTSUPP;
  2237. return sock_hash_ctx_update_elem(bpf_sock, map, key, flags);
  2238. }
  2239. const struct bpf_func_proto bpf_sock_hash_update_proto = {
  2240. .func = bpf_sock_hash_update,
  2241. .gpl_only = false,
  2242. .pkt_access = true,
  2243. .ret_type = RET_INTEGER,
  2244. .arg1_type = ARG_PTR_TO_CTX,
  2245. .arg2_type = ARG_CONST_MAP_PTR,
  2246. .arg3_type = ARG_PTR_TO_MAP_KEY,
  2247. .arg4_type = ARG_ANYTHING,
  2248. };