sock_map.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
  3. #include <linux/bpf.h>
  4. #include <linux/btf_ids.h>
  5. #include <linux/filter.h>
  6. #include <linux/errno.h>
  7. #include <linux/file.h>
  8. #include <linux/net.h>
  9. #include <linux/workqueue.h>
  10. #include <linux/skmsg.h>
  11. #include <linux/list.h>
  12. #include <linux/jhash.h>
  13. #include <linux/sock_diag.h>
  14. #include <net/udp.h>
  15. struct bpf_stab {
  16. struct bpf_map map;
  17. struct sock **sks;
  18. struct sk_psock_progs progs;
  19. spinlock_t lock;
  20. };
  21. #define SOCK_CREATE_FLAG_MASK \
  22. (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
  23. /* This mutex is used to
  24. * - protect race between prog/link attach/detach and link prog update, and
  25. * - protect race between releasing and accessing map in bpf_link.
  26. * A single global mutex lock is used since it is expected contention is low.
  27. */
  28. static DEFINE_MUTEX(sockmap_mutex);
  29. static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
  30. struct bpf_prog *old, struct bpf_link *link,
  31. u32 which);
  32. static struct sk_psock_progs *sock_map_progs(struct bpf_map *map);
  33. static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
  34. {
  35. struct bpf_stab *stab;
  36. if (attr->max_entries == 0 ||
  37. attr->key_size != 4 ||
  38. (attr->value_size != sizeof(u32) &&
  39. attr->value_size != sizeof(u64)) ||
  40. attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
  41. return ERR_PTR(-EINVAL);
  42. stab = bpf_map_area_alloc(sizeof(*stab), NUMA_NO_NODE);
  43. if (!stab)
  44. return ERR_PTR(-ENOMEM);
  45. bpf_map_init_from_attr(&stab->map, attr);
  46. spin_lock_init(&stab->lock);
  47. stab->sks = bpf_map_area_alloc((u64) stab->map.max_entries *
  48. sizeof(struct sock *),
  49. stab->map.numa_node);
  50. if (!stab->sks) {
  51. bpf_map_area_free(stab);
  52. return ERR_PTR(-ENOMEM);
  53. }
  54. return &stab->map;
  55. }
  56. int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog)
  57. {
  58. struct bpf_map *map;
  59. int ret;
  60. if (attr->attach_flags || attr->replace_bpf_fd)
  61. return -EINVAL;
  62. CLASS(fd, f)(attr->target_fd);
  63. map = __bpf_map_get(f);
  64. if (IS_ERR(map))
  65. return PTR_ERR(map);
  66. mutex_lock(&sockmap_mutex);
  67. ret = sock_map_prog_update(map, prog, NULL, NULL, attr->attach_type);
  68. mutex_unlock(&sockmap_mutex);
  69. return ret;
  70. }
  71. int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
  72. {
  73. struct bpf_prog *prog;
  74. struct bpf_map *map;
  75. int ret;
  76. if (attr->attach_flags || attr->replace_bpf_fd)
  77. return -EINVAL;
  78. CLASS(fd, f)(attr->target_fd);
  79. map = __bpf_map_get(f);
  80. if (IS_ERR(map))
  81. return PTR_ERR(map);
  82. prog = bpf_prog_get(attr->attach_bpf_fd);
  83. if (IS_ERR(prog))
  84. return PTR_ERR(prog);
  85. if (prog->type != ptype) {
  86. ret = -EINVAL;
  87. goto put_prog;
  88. }
  89. mutex_lock(&sockmap_mutex);
  90. ret = sock_map_prog_update(map, NULL, prog, NULL, attr->attach_type);
  91. mutex_unlock(&sockmap_mutex);
  92. put_prog:
  93. bpf_prog_put(prog);
  94. return ret;
  95. }
  96. static void sock_map_sk_acquire(struct sock *sk)
  97. __acquires(&sk->sk_lock.slock)
  98. {
  99. lock_sock(sk);
  100. rcu_read_lock();
  101. }
  102. static void sock_map_sk_release(struct sock *sk)
  103. __releases(&sk->sk_lock.slock)
  104. {
  105. rcu_read_unlock();
  106. release_sock(sk);
  107. }
  108. static void sock_map_add_link(struct sk_psock *psock,
  109. struct sk_psock_link *link,
  110. struct bpf_map *map, void *link_raw)
  111. {
  112. link->link_raw = link_raw;
  113. link->map = map;
  114. spin_lock_bh(&psock->link_lock);
  115. list_add_tail(&link->list, &psock->link);
  116. spin_unlock_bh(&psock->link_lock);
  117. }
  118. static void sock_map_del_link(struct sock *sk,
  119. struct sk_psock *psock, void *link_raw)
  120. {
  121. bool strp_stop = false, verdict_stop = false;
  122. struct sk_psock_link *link, *tmp;
  123. spin_lock_bh(&psock->link_lock);
  124. list_for_each_entry_safe(link, tmp, &psock->link, list) {
  125. if (link->link_raw == link_raw) {
  126. struct bpf_map *map = link->map;
  127. struct sk_psock_progs *progs = sock_map_progs(map);
  128. if (psock->saved_data_ready && progs->stream_parser)
  129. strp_stop = true;
  130. if (psock->saved_data_ready && progs->stream_verdict)
  131. verdict_stop = true;
  132. if (psock->saved_data_ready && progs->skb_verdict)
  133. verdict_stop = true;
  134. list_del(&link->list);
  135. sk_psock_free_link(link);
  136. break;
  137. }
  138. }
  139. spin_unlock_bh(&psock->link_lock);
  140. if (strp_stop || verdict_stop) {
  141. write_lock_bh(&sk->sk_callback_lock);
  142. if (strp_stop)
  143. sk_psock_stop_strp(sk, psock);
  144. if (verdict_stop)
  145. sk_psock_stop_verdict(sk, psock);
  146. if (psock->psock_update_sk_prot)
  147. psock->psock_update_sk_prot(sk, psock, false);
  148. write_unlock_bh(&sk->sk_callback_lock);
  149. }
  150. }
  151. static void sock_map_unref(struct sock *sk, void *link_raw)
  152. {
  153. struct sk_psock *psock = sk_psock(sk);
  154. if (likely(psock)) {
  155. sock_map_del_link(sk, psock, link_raw);
  156. sk_psock_put(sk, psock);
  157. }
  158. }
  159. static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
  160. {
  161. if (!sk->sk_prot->psock_update_sk_prot)
  162. return -EINVAL;
  163. psock->psock_update_sk_prot = sk->sk_prot->psock_update_sk_prot;
  164. return sk->sk_prot->psock_update_sk_prot(sk, psock, false);
  165. }
  166. static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
  167. {
  168. struct sk_psock *psock;
  169. rcu_read_lock();
  170. psock = sk_psock(sk);
  171. if (psock) {
  172. if (sk->sk_prot->close != sock_map_close) {
  173. psock = ERR_PTR(-EBUSY);
  174. goto out;
  175. }
  176. if (!refcount_inc_not_zero(&psock->refcnt))
  177. psock = ERR_PTR(-EBUSY);
  178. }
  179. out:
  180. rcu_read_unlock();
  181. return psock;
  182. }
  183. static int sock_map_link(struct bpf_map *map, struct sock *sk)
  184. {
  185. struct sk_psock_progs *progs = sock_map_progs(map);
  186. struct bpf_prog *stream_verdict = NULL;
  187. struct bpf_prog *stream_parser = NULL;
  188. struct bpf_prog *skb_verdict = NULL;
  189. struct bpf_prog *msg_parser = NULL;
  190. struct sk_psock *psock;
  191. int ret;
  192. stream_verdict = READ_ONCE(progs->stream_verdict);
  193. if (stream_verdict) {
  194. stream_verdict = bpf_prog_inc_not_zero(stream_verdict);
  195. if (IS_ERR(stream_verdict))
  196. return PTR_ERR(stream_verdict);
  197. }
  198. stream_parser = READ_ONCE(progs->stream_parser);
  199. if (stream_parser) {
  200. stream_parser = bpf_prog_inc_not_zero(stream_parser);
  201. if (IS_ERR(stream_parser)) {
  202. ret = PTR_ERR(stream_parser);
  203. goto out_put_stream_verdict;
  204. }
  205. }
  206. msg_parser = READ_ONCE(progs->msg_parser);
  207. if (msg_parser) {
  208. msg_parser = bpf_prog_inc_not_zero(msg_parser);
  209. if (IS_ERR(msg_parser)) {
  210. ret = PTR_ERR(msg_parser);
  211. goto out_put_stream_parser;
  212. }
  213. }
  214. skb_verdict = READ_ONCE(progs->skb_verdict);
  215. if (skb_verdict) {
  216. skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
  217. if (IS_ERR(skb_verdict)) {
  218. ret = PTR_ERR(skb_verdict);
  219. goto out_put_msg_parser;
  220. }
  221. }
  222. psock = sock_map_psock_get_checked(sk);
  223. if (IS_ERR(psock)) {
  224. ret = PTR_ERR(psock);
  225. goto out_progs;
  226. }
  227. if (psock) {
  228. if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
  229. (stream_parser && READ_ONCE(psock->progs.stream_parser)) ||
  230. (skb_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
  231. (skb_verdict && READ_ONCE(psock->progs.stream_verdict)) ||
  232. (stream_verdict && READ_ONCE(psock->progs.skb_verdict)) ||
  233. (stream_verdict && READ_ONCE(psock->progs.stream_verdict))) {
  234. sk_psock_put(sk, psock);
  235. ret = -EBUSY;
  236. goto out_progs;
  237. }
  238. } else {
  239. psock = sk_psock_init(sk, map->numa_node);
  240. if (IS_ERR(psock)) {
  241. ret = PTR_ERR(psock);
  242. goto out_progs;
  243. }
  244. }
  245. if (msg_parser)
  246. psock_set_prog(&psock->progs.msg_parser, msg_parser);
  247. if (stream_parser)
  248. psock_set_prog(&psock->progs.stream_parser, stream_parser);
  249. if (stream_verdict)
  250. psock_set_prog(&psock->progs.stream_verdict, stream_verdict);
  251. if (skb_verdict)
  252. psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
  253. /* msg_* and stream_* programs references tracked in psock after this
  254. * point. Reference dec and cleanup will occur through psock destructor
  255. */
  256. ret = sock_map_init_proto(sk, psock);
  257. if (ret < 0) {
  258. sk_psock_put(sk, psock);
  259. goto out;
  260. }
  261. write_lock_bh(&sk->sk_callback_lock);
  262. if (stream_parser && stream_verdict && !psock->saved_data_ready) {
  263. ret = sk_psock_init_strp(sk, psock);
  264. if (ret) {
  265. write_unlock_bh(&sk->sk_callback_lock);
  266. sk_psock_put(sk, psock);
  267. goto out;
  268. }
  269. sk_psock_start_strp(sk, psock);
  270. } else if (!stream_parser && stream_verdict && !psock->saved_data_ready) {
  271. sk_psock_start_verdict(sk,psock);
  272. } else if (!stream_verdict && skb_verdict && !psock->saved_data_ready) {
  273. sk_psock_start_verdict(sk, psock);
  274. }
  275. write_unlock_bh(&sk->sk_callback_lock);
  276. return 0;
  277. out_progs:
  278. if (skb_verdict)
  279. bpf_prog_put(skb_verdict);
  280. out_put_msg_parser:
  281. if (msg_parser)
  282. bpf_prog_put(msg_parser);
  283. out_put_stream_parser:
  284. if (stream_parser)
  285. bpf_prog_put(stream_parser);
  286. out_put_stream_verdict:
  287. if (stream_verdict)
  288. bpf_prog_put(stream_verdict);
  289. out:
  290. return ret;
  291. }
  292. static void sock_map_free(struct bpf_map *map)
  293. {
  294. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  295. int i;
  296. /* After the sync no updates or deletes will be in-flight so it
  297. * is safe to walk map and remove entries without risking a race
  298. * in EEXIST update case.
  299. */
  300. synchronize_rcu();
  301. for (i = 0; i < stab->map.max_entries; i++) {
  302. struct sock **psk = &stab->sks[i];
  303. struct sock *sk;
  304. sk = xchg(psk, NULL);
  305. if (sk) {
  306. sock_hold(sk);
  307. lock_sock(sk);
  308. rcu_read_lock();
  309. sock_map_unref(sk, psk);
  310. rcu_read_unlock();
  311. release_sock(sk);
  312. sock_put(sk);
  313. }
  314. }
  315. /* wait for psock readers accessing its map link */
  316. synchronize_rcu();
  317. bpf_map_area_free(stab->sks);
  318. bpf_map_area_free(stab);
  319. }
  320. static void sock_map_release_progs(struct bpf_map *map)
  321. {
  322. psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
  323. }
  324. static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
  325. {
  326. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  327. WARN_ON_ONCE(!rcu_read_lock_held());
  328. if (unlikely(key >= map->max_entries))
  329. return NULL;
  330. return READ_ONCE(stab->sks[key]);
  331. }
  332. static void *sock_map_lookup(struct bpf_map *map, void *key)
  333. {
  334. struct sock *sk;
  335. sk = __sock_map_lookup_elem(map, *(u32 *)key);
  336. if (!sk)
  337. return NULL;
  338. if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
  339. return NULL;
  340. return sk;
  341. }
  342. static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
  343. {
  344. struct sock *sk;
  345. if (map->value_size != sizeof(u64))
  346. return ERR_PTR(-ENOSPC);
  347. sk = __sock_map_lookup_elem(map, *(u32 *)key);
  348. if (!sk)
  349. return ERR_PTR(-ENOENT);
  350. __sock_gen_cookie(sk);
  351. return &sk->sk_cookie;
  352. }
  353. static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
  354. struct sock **psk)
  355. {
  356. struct sock *sk = NULL;
  357. int err = 0;
  358. spin_lock_bh(&stab->lock);
  359. if (!sk_test || sk_test == *psk)
  360. sk = xchg(psk, NULL);
  361. if (likely(sk))
  362. sock_map_unref(sk, psk);
  363. else
  364. err = -EINVAL;
  365. spin_unlock_bh(&stab->lock);
  366. return err;
  367. }
  368. static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
  369. void *link_raw)
  370. {
  371. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  372. __sock_map_delete(stab, sk, link_raw);
  373. }
  374. static long sock_map_delete_elem(struct bpf_map *map, void *key)
  375. {
  376. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  377. u32 i = *(u32 *)key;
  378. struct sock **psk;
  379. if (unlikely(i >= map->max_entries))
  380. return -EINVAL;
  381. psk = &stab->sks[i];
  382. return __sock_map_delete(stab, NULL, psk);
  383. }
  384. static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
  385. {
  386. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  387. u32 i = key ? *(u32 *)key : U32_MAX;
  388. u32 *key_next = next;
  389. if (i == stab->map.max_entries - 1)
  390. return -ENOENT;
  391. if (i >= stab->map.max_entries)
  392. *key_next = 0;
  393. else
  394. *key_next = i + 1;
  395. return 0;
  396. }
  397. static int sock_map_update_common(struct bpf_map *map, u32 idx,
  398. struct sock *sk, u64 flags)
  399. {
  400. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  401. struct sk_psock_link *link;
  402. struct sk_psock *psock;
  403. struct sock *osk;
  404. int ret;
  405. WARN_ON_ONCE(!rcu_read_lock_held());
  406. if (unlikely(flags > BPF_EXIST))
  407. return -EINVAL;
  408. if (unlikely(idx >= map->max_entries))
  409. return -E2BIG;
  410. link = sk_psock_init_link();
  411. if (!link)
  412. return -ENOMEM;
  413. ret = sock_map_link(map, sk);
  414. if (ret < 0)
  415. goto out_free;
  416. psock = sk_psock(sk);
  417. WARN_ON_ONCE(!psock);
  418. spin_lock_bh(&stab->lock);
  419. osk = stab->sks[idx];
  420. if (osk && flags == BPF_NOEXIST) {
  421. ret = -EEXIST;
  422. goto out_unlock;
  423. } else if (!osk && flags == BPF_EXIST) {
  424. ret = -ENOENT;
  425. goto out_unlock;
  426. }
  427. sock_map_add_link(psock, link, map, &stab->sks[idx]);
  428. stab->sks[idx] = sk;
  429. if (osk)
  430. sock_map_unref(osk, &stab->sks[idx]);
  431. spin_unlock_bh(&stab->lock);
  432. return 0;
  433. out_unlock:
  434. spin_unlock_bh(&stab->lock);
  435. if (psock)
  436. sk_psock_put(sk, psock);
  437. out_free:
  438. sk_psock_free_link(link);
  439. return ret;
  440. }
  441. static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
  442. {
  443. return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
  444. ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ||
  445. ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
  446. }
  447. static bool sock_map_redirect_allowed(const struct sock *sk)
  448. {
  449. if (sk_is_tcp(sk))
  450. return sk->sk_state != TCP_LISTEN;
  451. else
  452. return sk->sk_state == TCP_ESTABLISHED;
  453. }
  454. static bool sock_map_sk_is_suitable(const struct sock *sk)
  455. {
  456. return !!sk->sk_prot->psock_update_sk_prot;
  457. }
  458. static bool sock_map_sk_state_allowed(const struct sock *sk)
  459. {
  460. if (sk_is_tcp(sk))
  461. return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
  462. if (sk_is_stream_unix(sk))
  463. return (1 << sk->sk_state) & TCPF_ESTABLISHED;
  464. return true;
  465. }
  466. static int sock_hash_update_common(struct bpf_map *map, void *key,
  467. struct sock *sk, u64 flags);
  468. int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
  469. u64 flags)
  470. {
  471. struct socket *sock;
  472. struct sock *sk;
  473. int ret;
  474. u64 ufd;
  475. if (map->value_size == sizeof(u64))
  476. ufd = *(u64 *)value;
  477. else
  478. ufd = *(u32 *)value;
  479. if (ufd > S32_MAX)
  480. return -EINVAL;
  481. sock = sockfd_lookup(ufd, &ret);
  482. if (!sock)
  483. return ret;
  484. sk = sock->sk;
  485. if (!sk) {
  486. ret = -EINVAL;
  487. goto out;
  488. }
  489. if (!sock_map_sk_is_suitable(sk)) {
  490. ret = -EOPNOTSUPP;
  491. goto out;
  492. }
  493. sock_map_sk_acquire(sk);
  494. if (!sock_map_sk_state_allowed(sk))
  495. ret = -EOPNOTSUPP;
  496. else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
  497. ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
  498. else
  499. ret = sock_hash_update_common(map, key, sk, flags);
  500. sock_map_sk_release(sk);
  501. out:
  502. sockfd_put(sock);
  503. return ret;
  504. }
  505. static long sock_map_update_elem(struct bpf_map *map, void *key,
  506. void *value, u64 flags)
  507. {
  508. struct sock *sk = (struct sock *)value;
  509. int ret;
  510. if (unlikely(!sk || !sk_fullsock(sk)))
  511. return -EINVAL;
  512. if (!sock_map_sk_is_suitable(sk))
  513. return -EOPNOTSUPP;
  514. local_bh_disable();
  515. bh_lock_sock(sk);
  516. if (!sock_map_sk_state_allowed(sk))
  517. ret = -EOPNOTSUPP;
  518. else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
  519. ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
  520. else
  521. ret = sock_hash_update_common(map, key, sk, flags);
  522. bh_unlock_sock(sk);
  523. local_bh_enable();
  524. return ret;
  525. }
  526. BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
  527. struct bpf_map *, map, void *, key, u64, flags)
  528. {
  529. WARN_ON_ONCE(!rcu_read_lock_held());
  530. if (likely(sock_map_sk_is_suitable(sops->sk) &&
  531. sock_map_op_okay(sops)))
  532. return sock_map_update_common(map, *(u32 *)key, sops->sk,
  533. flags);
  534. return -EOPNOTSUPP;
  535. }
  536. const struct bpf_func_proto bpf_sock_map_update_proto = {
  537. .func = bpf_sock_map_update,
  538. .gpl_only = false,
  539. .pkt_access = true,
  540. .ret_type = RET_INTEGER,
  541. .arg1_type = ARG_PTR_TO_CTX,
  542. .arg2_type = ARG_CONST_MAP_PTR,
  543. .arg3_type = ARG_PTR_TO_MAP_KEY,
  544. .arg4_type = ARG_ANYTHING,
  545. };
  546. BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
  547. struct bpf_map *, map, u32, key, u64, flags)
  548. {
  549. struct sock *sk;
  550. if (unlikely(flags & ~(BPF_F_INGRESS)))
  551. return SK_DROP;
  552. sk = __sock_map_lookup_elem(map, key);
  553. if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
  554. return SK_DROP;
  555. if ((flags & BPF_F_INGRESS) && sk_is_vsock(sk))
  556. return SK_DROP;
  557. skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
  558. return SK_PASS;
  559. }
  560. const struct bpf_func_proto bpf_sk_redirect_map_proto = {
  561. .func = bpf_sk_redirect_map,
  562. .gpl_only = false,
  563. .ret_type = RET_INTEGER,
  564. .arg1_type = ARG_PTR_TO_CTX,
  565. .arg2_type = ARG_CONST_MAP_PTR,
  566. .arg3_type = ARG_ANYTHING,
  567. .arg4_type = ARG_ANYTHING,
  568. };
  569. BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
  570. struct bpf_map *, map, u32, key, u64, flags)
  571. {
  572. struct sock *sk;
  573. if (unlikely(flags & ~(BPF_F_INGRESS)))
  574. return SK_DROP;
  575. sk = __sock_map_lookup_elem(map, key);
  576. if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
  577. return SK_DROP;
  578. if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
  579. return SK_DROP;
  580. if (sk_is_vsock(sk))
  581. return SK_DROP;
  582. msg->flags = flags;
  583. msg->sk_redir = sk;
  584. return SK_PASS;
  585. }
  586. const struct bpf_func_proto bpf_msg_redirect_map_proto = {
  587. .func = bpf_msg_redirect_map,
  588. .gpl_only = false,
  589. .ret_type = RET_INTEGER,
  590. .arg1_type = ARG_PTR_TO_CTX,
  591. .arg2_type = ARG_CONST_MAP_PTR,
  592. .arg3_type = ARG_ANYTHING,
  593. .arg4_type = ARG_ANYTHING,
  594. };
  595. struct sock_map_seq_info {
  596. struct bpf_map *map;
  597. struct sock *sk;
  598. u32 index;
  599. };
  600. struct bpf_iter__sockmap {
  601. __bpf_md_ptr(struct bpf_iter_meta *, meta);
  602. __bpf_md_ptr(struct bpf_map *, map);
  603. __bpf_md_ptr(void *, key);
  604. __bpf_md_ptr(struct sock *, sk);
  605. };
  606. DEFINE_BPF_ITER_FUNC(sockmap, struct bpf_iter_meta *meta,
  607. struct bpf_map *map, void *key,
  608. struct sock *sk)
  609. static void *sock_map_seq_lookup_elem(struct sock_map_seq_info *info)
  610. {
  611. if (unlikely(info->index >= info->map->max_entries))
  612. return NULL;
  613. info->sk = __sock_map_lookup_elem(info->map, info->index);
  614. /* can't return sk directly, since that might be NULL */
  615. return info;
  616. }
  617. static void *sock_map_seq_start(struct seq_file *seq, loff_t *pos)
  618. __acquires(rcu)
  619. {
  620. struct sock_map_seq_info *info = seq->private;
  621. if (*pos == 0)
  622. ++*pos;
  623. /* pairs with sock_map_seq_stop */
  624. rcu_read_lock();
  625. return sock_map_seq_lookup_elem(info);
  626. }
  627. static void *sock_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  628. __must_hold(rcu)
  629. {
  630. struct sock_map_seq_info *info = seq->private;
  631. ++*pos;
  632. ++info->index;
  633. return sock_map_seq_lookup_elem(info);
  634. }
  635. static int sock_map_seq_show(struct seq_file *seq, void *v)
  636. __must_hold(rcu)
  637. {
  638. struct sock_map_seq_info *info = seq->private;
  639. struct bpf_iter__sockmap ctx = {};
  640. struct bpf_iter_meta meta;
  641. struct bpf_prog *prog;
  642. meta.seq = seq;
  643. prog = bpf_iter_get_info(&meta, !v);
  644. if (!prog)
  645. return 0;
  646. ctx.meta = &meta;
  647. ctx.map = info->map;
  648. if (v) {
  649. ctx.key = &info->index;
  650. ctx.sk = info->sk;
  651. }
  652. return bpf_iter_run_prog(prog, &ctx);
  653. }
  654. static void sock_map_seq_stop(struct seq_file *seq, void *v)
  655. __releases(rcu)
  656. {
  657. if (!v)
  658. (void)sock_map_seq_show(seq, NULL);
  659. /* pairs with sock_map_seq_start */
  660. rcu_read_unlock();
  661. }
  662. static const struct seq_operations sock_map_seq_ops = {
  663. .start = sock_map_seq_start,
  664. .next = sock_map_seq_next,
  665. .stop = sock_map_seq_stop,
  666. .show = sock_map_seq_show,
  667. };
  668. static int sock_map_init_seq_private(void *priv_data,
  669. struct bpf_iter_aux_info *aux)
  670. {
  671. struct sock_map_seq_info *info = priv_data;
  672. bpf_map_inc_with_uref(aux->map);
  673. info->map = aux->map;
  674. return 0;
  675. }
  676. static void sock_map_fini_seq_private(void *priv_data)
  677. {
  678. struct sock_map_seq_info *info = priv_data;
  679. bpf_map_put_with_uref(info->map);
  680. }
  681. static u64 sock_map_mem_usage(const struct bpf_map *map)
  682. {
  683. u64 usage = sizeof(struct bpf_stab);
  684. usage += (u64)map->max_entries * sizeof(struct sock *);
  685. return usage;
  686. }
  687. static const struct bpf_iter_seq_info sock_map_iter_seq_info = {
  688. .seq_ops = &sock_map_seq_ops,
  689. .init_seq_private = sock_map_init_seq_private,
  690. .fini_seq_private = sock_map_fini_seq_private,
  691. .seq_priv_size = sizeof(struct sock_map_seq_info),
  692. };
  693. BTF_ID_LIST_SINGLE(sock_map_btf_ids, struct, bpf_stab)
  694. const struct bpf_map_ops sock_map_ops = {
  695. .map_meta_equal = bpf_map_meta_equal,
  696. .map_alloc = sock_map_alloc,
  697. .map_free = sock_map_free,
  698. .map_get_next_key = sock_map_get_next_key,
  699. .map_lookup_elem_sys_only = sock_map_lookup_sys,
  700. .map_update_elem = sock_map_update_elem,
  701. .map_delete_elem = sock_map_delete_elem,
  702. .map_lookup_elem = sock_map_lookup,
  703. .map_release_uref = sock_map_release_progs,
  704. .map_check_btf = map_check_no_btf,
  705. .map_mem_usage = sock_map_mem_usage,
  706. .map_btf_id = &sock_map_btf_ids[0],
  707. .iter_seq_info = &sock_map_iter_seq_info,
  708. };
  709. struct bpf_shtab_elem {
  710. struct rcu_head rcu;
  711. u32 hash;
  712. struct sock *sk;
  713. struct hlist_node node;
  714. u8 key[];
  715. };
  716. struct bpf_shtab_bucket {
  717. struct hlist_head head;
  718. spinlock_t lock;
  719. };
  720. struct bpf_shtab {
  721. struct bpf_map map;
  722. struct bpf_shtab_bucket *buckets;
  723. u32 buckets_num;
  724. u32 elem_size;
  725. struct sk_psock_progs progs;
  726. atomic_t count;
  727. };
  728. static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
  729. {
  730. return jhash(key, len, 0);
  731. }
  732. static struct bpf_shtab_bucket *sock_hash_select_bucket(struct bpf_shtab *htab,
  733. u32 hash)
  734. {
  735. return &htab->buckets[hash & (htab->buckets_num - 1)];
  736. }
  737. static struct bpf_shtab_elem *
  738. sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
  739. u32 key_size)
  740. {
  741. struct bpf_shtab_elem *elem;
  742. hlist_for_each_entry_rcu(elem, head, node) {
  743. if (elem->hash == hash &&
  744. !memcmp(&elem->key, key, key_size))
  745. return elem;
  746. }
  747. return NULL;
  748. }
  749. static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
  750. {
  751. struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
  752. u32 key_size = map->key_size, hash;
  753. struct bpf_shtab_bucket *bucket;
  754. struct bpf_shtab_elem *elem;
  755. WARN_ON_ONCE(!rcu_read_lock_held());
  756. hash = sock_hash_bucket_hash(key, key_size);
  757. bucket = sock_hash_select_bucket(htab, hash);
  758. elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
  759. return elem ? elem->sk : NULL;
  760. }
  761. static void sock_hash_free_elem(struct bpf_shtab *htab,
  762. struct bpf_shtab_elem *elem)
  763. {
  764. atomic_dec(&htab->count);
  765. kfree_rcu(elem, rcu);
  766. }
  767. static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
  768. void *link_raw)
  769. {
  770. struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
  771. struct bpf_shtab_elem *elem_probe, *elem = link_raw;
  772. struct bpf_shtab_bucket *bucket;
  773. WARN_ON_ONCE(!rcu_read_lock_held());
  774. bucket = sock_hash_select_bucket(htab, elem->hash);
  775. /* elem may be deleted in parallel from the map, but access here
  776. * is okay since it's going away only after RCU grace period.
  777. * However, we need to check whether it's still present.
  778. */
  779. spin_lock_bh(&bucket->lock);
  780. elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
  781. elem->key, map->key_size);
  782. if (elem_probe && elem_probe == elem) {
  783. hlist_del_rcu(&elem->node);
  784. sock_map_unref(elem->sk, elem);
  785. sock_hash_free_elem(htab, elem);
  786. }
  787. spin_unlock_bh(&bucket->lock);
  788. }
  789. static long sock_hash_delete_elem(struct bpf_map *map, void *key)
  790. {
  791. struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
  792. u32 hash, key_size = map->key_size;
  793. struct bpf_shtab_bucket *bucket;
  794. struct bpf_shtab_elem *elem;
  795. int ret = -ENOENT;
  796. hash = sock_hash_bucket_hash(key, key_size);
  797. bucket = sock_hash_select_bucket(htab, hash);
  798. spin_lock_bh(&bucket->lock);
  799. elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
  800. if (elem) {
  801. hlist_del_rcu(&elem->node);
  802. sock_map_unref(elem->sk, elem);
  803. sock_hash_free_elem(htab, elem);
  804. ret = 0;
  805. }
  806. spin_unlock_bh(&bucket->lock);
  807. return ret;
  808. }
  809. static struct bpf_shtab_elem *sock_hash_alloc_elem(struct bpf_shtab *htab,
  810. void *key, u32 key_size,
  811. u32 hash, struct sock *sk,
  812. struct bpf_shtab_elem *old)
  813. {
  814. struct bpf_shtab_elem *new;
  815. if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
  816. if (!old) {
  817. atomic_dec(&htab->count);
  818. return ERR_PTR(-E2BIG);
  819. }
  820. }
  821. new = bpf_map_kmalloc_node(&htab->map, htab->elem_size,
  822. GFP_ATOMIC | __GFP_NOWARN,
  823. htab->map.numa_node);
  824. if (!new) {
  825. atomic_dec(&htab->count);
  826. return ERR_PTR(-ENOMEM);
  827. }
  828. memcpy(new->key, key, key_size);
  829. new->sk = sk;
  830. new->hash = hash;
  831. return new;
  832. }
  833. static int sock_hash_update_common(struct bpf_map *map, void *key,
  834. struct sock *sk, u64 flags)
  835. {
  836. struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
  837. u32 key_size = map->key_size, hash;
  838. struct bpf_shtab_elem *elem, *elem_new;
  839. struct bpf_shtab_bucket *bucket;
  840. struct sk_psock_link *link;
  841. struct sk_psock *psock;
  842. int ret;
  843. WARN_ON_ONCE(!rcu_read_lock_held());
  844. if (unlikely(flags > BPF_EXIST))
  845. return -EINVAL;
  846. link = sk_psock_init_link();
  847. if (!link)
  848. return -ENOMEM;
  849. ret = sock_map_link(map, sk);
  850. if (ret < 0)
  851. goto out_free;
  852. psock = sk_psock(sk);
  853. WARN_ON_ONCE(!psock);
  854. hash = sock_hash_bucket_hash(key, key_size);
  855. bucket = sock_hash_select_bucket(htab, hash);
  856. spin_lock_bh(&bucket->lock);
  857. elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
  858. if (elem && flags == BPF_NOEXIST) {
  859. ret = -EEXIST;
  860. goto out_unlock;
  861. } else if (!elem && flags == BPF_EXIST) {
  862. ret = -ENOENT;
  863. goto out_unlock;
  864. }
  865. elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
  866. if (IS_ERR(elem_new)) {
  867. ret = PTR_ERR(elem_new);
  868. goto out_unlock;
  869. }
  870. sock_map_add_link(psock, link, map, elem_new);
  871. /* Add new element to the head of the list, so that
  872. * concurrent search will find it before old elem.
  873. */
  874. hlist_add_head_rcu(&elem_new->node, &bucket->head);
  875. if (elem) {
  876. hlist_del_rcu(&elem->node);
  877. sock_map_unref(elem->sk, elem);
  878. sock_hash_free_elem(htab, elem);
  879. }
  880. spin_unlock_bh(&bucket->lock);
  881. return 0;
  882. out_unlock:
  883. spin_unlock_bh(&bucket->lock);
  884. sk_psock_put(sk, psock);
  885. out_free:
  886. sk_psock_free_link(link);
  887. return ret;
  888. }
  889. static int sock_hash_get_next_key(struct bpf_map *map, void *key,
  890. void *key_next)
  891. {
  892. struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
  893. struct bpf_shtab_elem *elem, *elem_next;
  894. u32 hash, key_size = map->key_size;
  895. struct hlist_head *head;
  896. int i = 0;
  897. if (!key)
  898. goto find_first_elem;
  899. hash = sock_hash_bucket_hash(key, key_size);
  900. head = &sock_hash_select_bucket(htab, hash)->head;
  901. elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
  902. if (!elem)
  903. goto find_first_elem;
  904. elem_next = hlist_entry_safe(rcu_dereference(hlist_next_rcu(&elem->node)),
  905. struct bpf_shtab_elem, node);
  906. if (elem_next) {
  907. memcpy(key_next, elem_next->key, key_size);
  908. return 0;
  909. }
  910. i = hash & (htab->buckets_num - 1);
  911. i++;
  912. find_first_elem:
  913. for (; i < htab->buckets_num; i++) {
  914. head = &sock_hash_select_bucket(htab, i)->head;
  915. elem_next = hlist_entry_safe(rcu_dereference(hlist_first_rcu(head)),
  916. struct bpf_shtab_elem, node);
  917. if (elem_next) {
  918. memcpy(key_next, elem_next->key, key_size);
  919. return 0;
  920. }
  921. }
  922. return -ENOENT;
  923. }
  924. static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
  925. {
  926. struct bpf_shtab *htab;
  927. int i, err;
  928. if (attr->max_entries == 0 ||
  929. attr->key_size == 0 ||
  930. (attr->value_size != sizeof(u32) &&
  931. attr->value_size != sizeof(u64)) ||
  932. attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
  933. return ERR_PTR(-EINVAL);
  934. if (attr->key_size > MAX_BPF_STACK)
  935. return ERR_PTR(-E2BIG);
  936. htab = bpf_map_area_alloc(sizeof(*htab), NUMA_NO_NODE);
  937. if (!htab)
  938. return ERR_PTR(-ENOMEM);
  939. bpf_map_init_from_attr(&htab->map, attr);
  940. htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
  941. htab->elem_size = sizeof(struct bpf_shtab_elem) +
  942. round_up(htab->map.key_size, 8);
  943. if (htab->buckets_num == 0 ||
  944. htab->buckets_num > U32_MAX / sizeof(struct bpf_shtab_bucket)) {
  945. err = -EINVAL;
  946. goto free_htab;
  947. }
  948. htab->buckets = bpf_map_area_alloc(htab->buckets_num *
  949. sizeof(struct bpf_shtab_bucket),
  950. htab->map.numa_node);
  951. if (!htab->buckets) {
  952. err = -ENOMEM;
  953. goto free_htab;
  954. }
  955. for (i = 0; i < htab->buckets_num; i++) {
  956. INIT_HLIST_HEAD(&htab->buckets[i].head);
  957. spin_lock_init(&htab->buckets[i].lock);
  958. }
  959. return &htab->map;
  960. free_htab:
  961. bpf_map_area_free(htab);
  962. return ERR_PTR(err);
  963. }
  964. static void sock_hash_free(struct bpf_map *map)
  965. {
  966. struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
  967. struct bpf_shtab_bucket *bucket;
  968. struct hlist_head unlink_list;
  969. struct bpf_shtab_elem *elem;
  970. struct hlist_node *node;
  971. int i;
  972. /* After the sync no updates or deletes will be in-flight so it
  973. * is safe to walk map and remove entries without risking a race
  974. * in EEXIST update case.
  975. */
  976. synchronize_rcu();
  977. for (i = 0; i < htab->buckets_num; i++) {
  978. bucket = sock_hash_select_bucket(htab, i);
  979. /* We are racing with sock_hash_delete_from_link to
  980. * enter the spin-lock critical section. Every socket on
  981. * the list is still linked to sockhash. Since link
  982. * exists, psock exists and holds a ref to socket. That
  983. * lets us to grab a socket ref too.
  984. */
  985. spin_lock_bh(&bucket->lock);
  986. hlist_for_each_entry(elem, &bucket->head, node)
  987. sock_hold(elem->sk);
  988. hlist_move_list(&bucket->head, &unlink_list);
  989. spin_unlock_bh(&bucket->lock);
  990. /* Process removed entries out of atomic context to
  991. * block for socket lock before deleting the psock's
  992. * link to sockhash.
  993. */
  994. hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
  995. hlist_del(&elem->node);
  996. lock_sock(elem->sk);
  997. rcu_read_lock();
  998. sock_map_unref(elem->sk, elem);
  999. rcu_read_unlock();
  1000. release_sock(elem->sk);
  1001. sock_put(elem->sk);
  1002. sock_hash_free_elem(htab, elem);
  1003. }
  1004. cond_resched();
  1005. }
  1006. /* wait for psock readers accessing its map link */
  1007. synchronize_rcu();
  1008. bpf_map_area_free(htab->buckets);
  1009. bpf_map_area_free(htab);
  1010. }
  1011. static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
  1012. {
  1013. struct sock *sk;
  1014. if (map->value_size != sizeof(u64))
  1015. return ERR_PTR(-ENOSPC);
  1016. sk = __sock_hash_lookup_elem(map, key);
  1017. if (!sk)
  1018. return ERR_PTR(-ENOENT);
  1019. __sock_gen_cookie(sk);
  1020. return &sk->sk_cookie;
  1021. }
  1022. static void *sock_hash_lookup(struct bpf_map *map, void *key)
  1023. {
  1024. struct sock *sk;
  1025. sk = __sock_hash_lookup_elem(map, key);
  1026. if (!sk)
  1027. return NULL;
  1028. if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt))
  1029. return NULL;
  1030. return sk;
  1031. }
  1032. static void sock_hash_release_progs(struct bpf_map *map)
  1033. {
  1034. psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs);
  1035. }
  1036. BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
  1037. struct bpf_map *, map, void *, key, u64, flags)
  1038. {
  1039. WARN_ON_ONCE(!rcu_read_lock_held());
  1040. if (likely(sock_map_sk_is_suitable(sops->sk) &&
  1041. sock_map_op_okay(sops)))
  1042. return sock_hash_update_common(map, key, sops->sk, flags);
  1043. return -EOPNOTSUPP;
  1044. }
  1045. const struct bpf_func_proto bpf_sock_hash_update_proto = {
  1046. .func = bpf_sock_hash_update,
  1047. .gpl_only = false,
  1048. .pkt_access = true,
  1049. .ret_type = RET_INTEGER,
  1050. .arg1_type = ARG_PTR_TO_CTX,
  1051. .arg2_type = ARG_CONST_MAP_PTR,
  1052. .arg3_type = ARG_PTR_TO_MAP_KEY,
  1053. .arg4_type = ARG_ANYTHING,
  1054. };
  1055. BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
  1056. struct bpf_map *, map, void *, key, u64, flags)
  1057. {
  1058. struct sock *sk;
  1059. if (unlikely(flags & ~(BPF_F_INGRESS)))
  1060. return SK_DROP;
  1061. sk = __sock_hash_lookup_elem(map, key);
  1062. if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
  1063. return SK_DROP;
  1064. if ((flags & BPF_F_INGRESS) && sk_is_vsock(sk))
  1065. return SK_DROP;
  1066. skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS);
  1067. return SK_PASS;
  1068. }
  1069. const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
  1070. .func = bpf_sk_redirect_hash,
  1071. .gpl_only = false,
  1072. .ret_type = RET_INTEGER,
  1073. .arg1_type = ARG_PTR_TO_CTX,
  1074. .arg2_type = ARG_CONST_MAP_PTR,
  1075. .arg3_type = ARG_PTR_TO_MAP_KEY,
  1076. .arg4_type = ARG_ANYTHING,
  1077. };
  1078. BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
  1079. struct bpf_map *, map, void *, key, u64, flags)
  1080. {
  1081. struct sock *sk;
  1082. if (unlikely(flags & ~(BPF_F_INGRESS)))
  1083. return SK_DROP;
  1084. sk = __sock_hash_lookup_elem(map, key);
  1085. if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
  1086. return SK_DROP;
  1087. if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
  1088. return SK_DROP;
  1089. if (sk_is_vsock(sk))
  1090. return SK_DROP;
  1091. msg->flags = flags;
  1092. msg->sk_redir = sk;
  1093. return SK_PASS;
  1094. }
  1095. const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
  1096. .func = bpf_msg_redirect_hash,
  1097. .gpl_only = false,
  1098. .ret_type = RET_INTEGER,
  1099. .arg1_type = ARG_PTR_TO_CTX,
  1100. .arg2_type = ARG_CONST_MAP_PTR,
  1101. .arg3_type = ARG_PTR_TO_MAP_KEY,
  1102. .arg4_type = ARG_ANYTHING,
  1103. };
  1104. struct sock_hash_seq_info {
  1105. struct bpf_map *map;
  1106. struct bpf_shtab *htab;
  1107. u32 bucket_id;
  1108. };
  1109. static void *sock_hash_seq_find_next(struct sock_hash_seq_info *info,
  1110. struct bpf_shtab_elem *prev_elem)
  1111. {
  1112. const struct bpf_shtab *htab = info->htab;
  1113. struct bpf_shtab_bucket *bucket;
  1114. struct bpf_shtab_elem *elem;
  1115. struct hlist_node *node;
  1116. /* try to find next elem in the same bucket */
  1117. if (prev_elem) {
  1118. node = rcu_dereference(hlist_next_rcu(&prev_elem->node));
  1119. elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
  1120. if (elem)
  1121. return elem;
  1122. /* no more elements, continue in the next bucket */
  1123. info->bucket_id++;
  1124. }
  1125. for (; info->bucket_id < htab->buckets_num; info->bucket_id++) {
  1126. bucket = &htab->buckets[info->bucket_id];
  1127. node = rcu_dereference(hlist_first_rcu(&bucket->head));
  1128. elem = hlist_entry_safe(node, struct bpf_shtab_elem, node);
  1129. if (elem)
  1130. return elem;
  1131. }
  1132. return NULL;
  1133. }
  1134. static void *sock_hash_seq_start(struct seq_file *seq, loff_t *pos)
  1135. __acquires(rcu)
  1136. {
  1137. struct sock_hash_seq_info *info = seq->private;
  1138. if (*pos == 0)
  1139. ++*pos;
  1140. /* pairs with sock_hash_seq_stop */
  1141. rcu_read_lock();
  1142. return sock_hash_seq_find_next(info, NULL);
  1143. }
  1144. static void *sock_hash_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1145. __must_hold(rcu)
  1146. {
  1147. struct sock_hash_seq_info *info = seq->private;
  1148. ++*pos;
  1149. return sock_hash_seq_find_next(info, v);
  1150. }
  1151. static int sock_hash_seq_show(struct seq_file *seq, void *v)
  1152. __must_hold(rcu)
  1153. {
  1154. struct sock_hash_seq_info *info = seq->private;
  1155. struct bpf_iter__sockmap ctx = {};
  1156. struct bpf_shtab_elem *elem = v;
  1157. struct bpf_iter_meta meta;
  1158. struct bpf_prog *prog;
  1159. meta.seq = seq;
  1160. prog = bpf_iter_get_info(&meta, !elem);
  1161. if (!prog)
  1162. return 0;
  1163. ctx.meta = &meta;
  1164. ctx.map = info->map;
  1165. if (elem) {
  1166. ctx.key = elem->key;
  1167. ctx.sk = elem->sk;
  1168. }
  1169. return bpf_iter_run_prog(prog, &ctx);
  1170. }
  1171. static void sock_hash_seq_stop(struct seq_file *seq, void *v)
  1172. __releases(rcu)
  1173. {
  1174. if (!v)
  1175. (void)sock_hash_seq_show(seq, NULL);
  1176. /* pairs with sock_hash_seq_start */
  1177. rcu_read_unlock();
  1178. }
  1179. static const struct seq_operations sock_hash_seq_ops = {
  1180. .start = sock_hash_seq_start,
  1181. .next = sock_hash_seq_next,
  1182. .stop = sock_hash_seq_stop,
  1183. .show = sock_hash_seq_show,
  1184. };
  1185. static int sock_hash_init_seq_private(void *priv_data,
  1186. struct bpf_iter_aux_info *aux)
  1187. {
  1188. struct sock_hash_seq_info *info = priv_data;
  1189. bpf_map_inc_with_uref(aux->map);
  1190. info->map = aux->map;
  1191. info->htab = container_of(aux->map, struct bpf_shtab, map);
  1192. return 0;
  1193. }
  1194. static void sock_hash_fini_seq_private(void *priv_data)
  1195. {
  1196. struct sock_hash_seq_info *info = priv_data;
  1197. bpf_map_put_with_uref(info->map);
  1198. }
  1199. static u64 sock_hash_mem_usage(const struct bpf_map *map)
  1200. {
  1201. struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);
  1202. u64 usage = sizeof(*htab);
  1203. usage += htab->buckets_num * sizeof(struct bpf_shtab_bucket);
  1204. usage += atomic_read(&htab->count) * (u64)htab->elem_size;
  1205. return usage;
  1206. }
  1207. static const struct bpf_iter_seq_info sock_hash_iter_seq_info = {
  1208. .seq_ops = &sock_hash_seq_ops,
  1209. .init_seq_private = sock_hash_init_seq_private,
  1210. .fini_seq_private = sock_hash_fini_seq_private,
  1211. .seq_priv_size = sizeof(struct sock_hash_seq_info),
  1212. };
  1213. BTF_ID_LIST_SINGLE(sock_hash_map_btf_ids, struct, bpf_shtab)
  1214. const struct bpf_map_ops sock_hash_ops = {
  1215. .map_meta_equal = bpf_map_meta_equal,
  1216. .map_alloc = sock_hash_alloc,
  1217. .map_free = sock_hash_free,
  1218. .map_get_next_key = sock_hash_get_next_key,
  1219. .map_update_elem = sock_map_update_elem,
  1220. .map_delete_elem = sock_hash_delete_elem,
  1221. .map_lookup_elem = sock_hash_lookup,
  1222. .map_lookup_elem_sys_only = sock_hash_lookup_sys,
  1223. .map_release_uref = sock_hash_release_progs,
  1224. .map_check_btf = map_check_no_btf,
  1225. .map_mem_usage = sock_hash_mem_usage,
  1226. .map_btf_id = &sock_hash_map_btf_ids[0],
  1227. .iter_seq_info = &sock_hash_iter_seq_info,
  1228. };
  1229. static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
  1230. {
  1231. switch (map->map_type) {
  1232. case BPF_MAP_TYPE_SOCKMAP:
  1233. return &container_of(map, struct bpf_stab, map)->progs;
  1234. case BPF_MAP_TYPE_SOCKHASH:
  1235. return &container_of(map, struct bpf_shtab, map)->progs;
  1236. default:
  1237. break;
  1238. }
  1239. return NULL;
  1240. }
  1241. static int sock_map_prog_link_lookup(struct bpf_map *map, struct bpf_prog ***pprog,
  1242. struct bpf_link ***plink, u32 which)
  1243. {
  1244. struct sk_psock_progs *progs = sock_map_progs(map);
  1245. struct bpf_prog **cur_pprog;
  1246. struct bpf_link **cur_plink;
  1247. if (!progs)
  1248. return -EOPNOTSUPP;
  1249. switch (which) {
  1250. case BPF_SK_MSG_VERDICT:
  1251. cur_pprog = &progs->msg_parser;
  1252. cur_plink = &progs->msg_parser_link;
  1253. break;
  1254. #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
  1255. case BPF_SK_SKB_STREAM_PARSER:
  1256. cur_pprog = &progs->stream_parser;
  1257. cur_plink = &progs->stream_parser_link;
  1258. break;
  1259. #endif
  1260. case BPF_SK_SKB_STREAM_VERDICT:
  1261. if (progs->skb_verdict)
  1262. return -EBUSY;
  1263. cur_pprog = &progs->stream_verdict;
  1264. cur_plink = &progs->stream_verdict_link;
  1265. break;
  1266. case BPF_SK_SKB_VERDICT:
  1267. if (progs->stream_verdict)
  1268. return -EBUSY;
  1269. cur_pprog = &progs->skb_verdict;
  1270. cur_plink = &progs->skb_verdict_link;
  1271. break;
  1272. default:
  1273. return -EOPNOTSUPP;
  1274. }
  1275. *pprog = cur_pprog;
  1276. if (plink)
  1277. *plink = cur_plink;
  1278. return 0;
  1279. }
  1280. /* Handle the following four cases:
  1281. * prog_attach: prog != NULL, old == NULL, link == NULL
  1282. * prog_detach: prog == NULL, old != NULL, link == NULL
  1283. * link_attach: prog != NULL, old == NULL, link != NULL
  1284. * link_detach: prog == NULL, old != NULL, link != NULL
  1285. */
  1286. static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
  1287. struct bpf_prog *old, struct bpf_link *link,
  1288. u32 which)
  1289. {
  1290. struct bpf_prog **pprog;
  1291. struct bpf_link **plink;
  1292. int ret;
  1293. ret = sock_map_prog_link_lookup(map, &pprog, &plink, which);
  1294. if (ret)
  1295. return ret;
  1296. /* for prog_attach/prog_detach/link_attach, return error if a bpf_link
  1297. * exists for that prog.
  1298. */
  1299. if ((!link || prog) && *plink)
  1300. return -EBUSY;
  1301. if (old) {
  1302. ret = psock_replace_prog(pprog, prog, old);
  1303. if (!ret)
  1304. *plink = NULL;
  1305. } else {
  1306. psock_set_prog(pprog, prog);
  1307. if (link)
  1308. *plink = link;
  1309. }
  1310. return ret;
  1311. }
  1312. int sock_map_bpf_prog_query(const union bpf_attr *attr,
  1313. union bpf_attr __user *uattr)
  1314. {
  1315. __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
  1316. u32 prog_cnt = 0, flags = 0;
  1317. struct bpf_prog **pprog;
  1318. struct bpf_prog *prog;
  1319. struct bpf_map *map;
  1320. u32 id = 0;
  1321. int ret;
  1322. if (attr->query.query_flags)
  1323. return -EINVAL;
  1324. CLASS(fd, f)(attr->target_fd);
  1325. map = __bpf_map_get(f);
  1326. if (IS_ERR(map))
  1327. return PTR_ERR(map);
  1328. rcu_read_lock();
  1329. ret = sock_map_prog_link_lookup(map, &pprog, NULL, attr->query.attach_type);
  1330. if (ret)
  1331. goto end;
  1332. prog = *pprog;
  1333. prog_cnt = !prog ? 0 : 1;
  1334. if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
  1335. goto end;
  1336. /* we do not hold the refcnt, the bpf prog may be released
  1337. * asynchronously and the id would be set to 0.
  1338. */
  1339. id = data_race(prog->aux->id);
  1340. if (id == 0)
  1341. prog_cnt = 0;
  1342. end:
  1343. rcu_read_unlock();
  1344. if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)) ||
  1345. (id != 0 && copy_to_user(prog_ids, &id, sizeof(u32))) ||
  1346. copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
  1347. ret = -EFAULT;
  1348. return ret;
  1349. }
  1350. static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link)
  1351. {
  1352. switch (link->map->map_type) {
  1353. case BPF_MAP_TYPE_SOCKMAP:
  1354. return sock_map_delete_from_link(link->map, sk,
  1355. link->link_raw);
  1356. case BPF_MAP_TYPE_SOCKHASH:
  1357. return sock_hash_delete_from_link(link->map, sk,
  1358. link->link_raw);
  1359. default:
  1360. break;
  1361. }
  1362. }
  1363. static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock)
  1364. {
  1365. struct sk_psock_link *link;
  1366. while ((link = sk_psock_link_pop(psock))) {
  1367. sock_map_unlink(sk, link);
  1368. sk_psock_free_link(link);
  1369. }
  1370. }
  1371. void sock_map_unhash(struct sock *sk)
  1372. {
  1373. void (*saved_unhash)(struct sock *sk);
  1374. struct sk_psock *psock;
  1375. rcu_read_lock();
  1376. psock = sk_psock(sk);
  1377. if (unlikely(!psock)) {
  1378. rcu_read_unlock();
  1379. saved_unhash = READ_ONCE(sk->sk_prot)->unhash;
  1380. } else {
  1381. saved_unhash = psock->saved_unhash;
  1382. sock_map_remove_links(sk, psock);
  1383. rcu_read_unlock();
  1384. }
  1385. if (WARN_ON_ONCE(saved_unhash == sock_map_unhash))
  1386. return;
  1387. if (saved_unhash)
  1388. saved_unhash(sk);
  1389. }
  1390. EXPORT_SYMBOL_GPL(sock_map_unhash);
  1391. void sock_map_destroy(struct sock *sk)
  1392. {
  1393. void (*saved_destroy)(struct sock *sk);
  1394. struct sk_psock *psock;
  1395. rcu_read_lock();
  1396. psock = sk_psock_get(sk);
  1397. if (unlikely(!psock)) {
  1398. rcu_read_unlock();
  1399. saved_destroy = READ_ONCE(sk->sk_prot)->destroy;
  1400. } else {
  1401. saved_destroy = psock->saved_destroy;
  1402. sock_map_remove_links(sk, psock);
  1403. rcu_read_unlock();
  1404. sk_psock_stop(psock);
  1405. sk_psock_put(sk, psock);
  1406. }
  1407. if (WARN_ON_ONCE(saved_destroy == sock_map_destroy))
  1408. return;
  1409. if (saved_destroy)
  1410. saved_destroy(sk);
  1411. }
  1412. EXPORT_SYMBOL_GPL(sock_map_destroy);
  1413. void sock_map_close(struct sock *sk, long timeout)
  1414. {
  1415. void (*saved_close)(struct sock *sk, long timeout);
  1416. struct sk_psock *psock;
  1417. lock_sock(sk);
  1418. rcu_read_lock();
  1419. psock = sk_psock(sk);
  1420. if (likely(psock)) {
  1421. saved_close = psock->saved_close;
  1422. sock_map_remove_links(sk, psock);
  1423. psock = sk_psock_get(sk);
  1424. if (unlikely(!psock))
  1425. goto no_psock;
  1426. rcu_read_unlock();
  1427. sk_psock_stop(psock);
  1428. release_sock(sk);
  1429. cancel_delayed_work_sync(&psock->work);
  1430. sk_psock_put(sk, psock);
  1431. } else {
  1432. saved_close = READ_ONCE(sk->sk_prot)->close;
  1433. no_psock:
  1434. rcu_read_unlock();
  1435. release_sock(sk);
  1436. }
  1437. /* Make sure we do not recurse. This is a bug.
  1438. * Leak the socket instead of crashing on a stack overflow.
  1439. */
  1440. if (WARN_ON_ONCE(saved_close == sock_map_close))
  1441. return;
  1442. saved_close(sk, timeout);
  1443. }
  1444. EXPORT_SYMBOL_GPL(sock_map_close);
  1445. struct sockmap_link {
  1446. struct bpf_link link;
  1447. struct bpf_map *map;
  1448. enum bpf_attach_type attach_type;
  1449. };
  1450. static void sock_map_link_release(struct bpf_link *link)
  1451. {
  1452. struct sockmap_link *sockmap_link = container_of(link, struct sockmap_link, link);
  1453. mutex_lock(&sockmap_mutex);
  1454. if (!sockmap_link->map)
  1455. goto out;
  1456. WARN_ON_ONCE(sock_map_prog_update(sockmap_link->map, NULL, link->prog, link,
  1457. sockmap_link->attach_type));
  1458. bpf_map_put_with_uref(sockmap_link->map);
  1459. sockmap_link->map = NULL;
  1460. out:
  1461. mutex_unlock(&sockmap_mutex);
  1462. }
  1463. static int sock_map_link_detach(struct bpf_link *link)
  1464. {
  1465. sock_map_link_release(link);
  1466. return 0;
  1467. }
  1468. static void sock_map_link_dealloc(struct bpf_link *link)
  1469. {
  1470. kfree(link);
  1471. }
  1472. /* Handle the following two cases:
  1473. * case 1: link != NULL, prog != NULL, old != NULL
  1474. * case 2: link != NULL, prog != NULL, old == NULL
  1475. */
  1476. static int sock_map_link_update_prog(struct bpf_link *link,
  1477. struct bpf_prog *prog,
  1478. struct bpf_prog *old)
  1479. {
  1480. const struct sockmap_link *sockmap_link = container_of(link, struct sockmap_link, link);
  1481. struct bpf_prog **pprog, *old_link_prog;
  1482. struct bpf_link **plink;
  1483. int ret = 0;
  1484. mutex_lock(&sockmap_mutex);
  1485. /* If old prog is not NULL, ensure old prog is the same as link->prog. */
  1486. if (old && link->prog != old) {
  1487. ret = -EPERM;
  1488. goto out;
  1489. }
  1490. /* Ensure link->prog has the same type/attach_type as the new prog. */
  1491. if (link->prog->type != prog->type ||
  1492. link->prog->expected_attach_type != prog->expected_attach_type) {
  1493. ret = -EINVAL;
  1494. goto out;
  1495. }
  1496. if (!sockmap_link->map) {
  1497. ret = -ENOLINK;
  1498. goto out;
  1499. }
  1500. ret = sock_map_prog_link_lookup(sockmap_link->map, &pprog, &plink,
  1501. sockmap_link->attach_type);
  1502. if (ret)
  1503. goto out;
  1504. /* return error if the stored bpf_link does not match the incoming bpf_link. */
  1505. if (link != *plink) {
  1506. ret = -EBUSY;
  1507. goto out;
  1508. }
  1509. if (old) {
  1510. ret = psock_replace_prog(pprog, prog, old);
  1511. if (ret)
  1512. goto out;
  1513. } else {
  1514. psock_set_prog(pprog, prog);
  1515. }
  1516. bpf_prog_inc(prog);
  1517. old_link_prog = xchg(&link->prog, prog);
  1518. bpf_prog_put(old_link_prog);
  1519. out:
  1520. mutex_unlock(&sockmap_mutex);
  1521. return ret;
  1522. }
  1523. static u32 sock_map_link_get_map_id(const struct sockmap_link *sockmap_link)
  1524. {
  1525. u32 map_id = 0;
  1526. mutex_lock(&sockmap_mutex);
  1527. if (sockmap_link->map)
  1528. map_id = sockmap_link->map->id;
  1529. mutex_unlock(&sockmap_mutex);
  1530. return map_id;
  1531. }
  1532. static int sock_map_link_fill_info(const struct bpf_link *link,
  1533. struct bpf_link_info *info)
  1534. {
  1535. const struct sockmap_link *sockmap_link = container_of(link, struct sockmap_link, link);
  1536. u32 map_id = sock_map_link_get_map_id(sockmap_link);
  1537. info->sockmap.map_id = map_id;
  1538. info->sockmap.attach_type = sockmap_link->attach_type;
  1539. return 0;
  1540. }
  1541. static void sock_map_link_show_fdinfo(const struct bpf_link *link,
  1542. struct seq_file *seq)
  1543. {
  1544. const struct sockmap_link *sockmap_link = container_of(link, struct sockmap_link, link);
  1545. u32 map_id = sock_map_link_get_map_id(sockmap_link);
  1546. seq_printf(seq, "map_id:\t%u\n", map_id);
  1547. seq_printf(seq, "attach_type:\t%u\n", sockmap_link->attach_type);
  1548. }
  1549. static const struct bpf_link_ops sock_map_link_ops = {
  1550. .release = sock_map_link_release,
  1551. .dealloc = sock_map_link_dealloc,
  1552. .detach = sock_map_link_detach,
  1553. .update_prog = sock_map_link_update_prog,
  1554. .fill_link_info = sock_map_link_fill_info,
  1555. .show_fdinfo = sock_map_link_show_fdinfo,
  1556. };
  1557. int sock_map_link_create(const union bpf_attr *attr, struct bpf_prog *prog)
  1558. {
  1559. struct bpf_link_primer link_primer;
  1560. struct sockmap_link *sockmap_link;
  1561. enum bpf_attach_type attach_type;
  1562. struct bpf_map *map;
  1563. int ret;
  1564. if (attr->link_create.flags)
  1565. return -EINVAL;
  1566. map = bpf_map_get_with_uref(attr->link_create.target_fd);
  1567. if (IS_ERR(map))
  1568. return PTR_ERR(map);
  1569. if (map->map_type != BPF_MAP_TYPE_SOCKMAP && map->map_type != BPF_MAP_TYPE_SOCKHASH) {
  1570. ret = -EINVAL;
  1571. goto out;
  1572. }
  1573. sockmap_link = kzalloc(sizeof(*sockmap_link), GFP_USER);
  1574. if (!sockmap_link) {
  1575. ret = -ENOMEM;
  1576. goto out;
  1577. }
  1578. attach_type = attr->link_create.attach_type;
  1579. bpf_link_init(&sockmap_link->link, BPF_LINK_TYPE_SOCKMAP, &sock_map_link_ops, prog);
  1580. sockmap_link->map = map;
  1581. sockmap_link->attach_type = attach_type;
  1582. ret = bpf_link_prime(&sockmap_link->link, &link_primer);
  1583. if (ret) {
  1584. kfree(sockmap_link);
  1585. goto out;
  1586. }
  1587. mutex_lock(&sockmap_mutex);
  1588. ret = sock_map_prog_update(map, prog, NULL, &sockmap_link->link, attach_type);
  1589. mutex_unlock(&sockmap_mutex);
  1590. if (ret) {
  1591. bpf_link_cleanup(&link_primer);
  1592. goto out;
  1593. }
  1594. /* Increase refcnt for the prog since when old prog is replaced with
  1595. * psock_replace_prog() and psock_set_prog() its refcnt will be decreased.
  1596. *
  1597. * Actually, we do not need to increase refcnt for the prog since bpf_link
  1598. * will hold a reference. But in order to have less complexity w.r.t.
  1599. * replacing/setting prog, let us increase the refcnt to make things simpler.
  1600. */
  1601. bpf_prog_inc(prog);
  1602. return bpf_link_settle(&link_primer);
  1603. out:
  1604. bpf_map_put_with_uref(map);
  1605. return ret;
  1606. }
  1607. static int sock_map_iter_attach_target(struct bpf_prog *prog,
  1608. union bpf_iter_link_info *linfo,
  1609. struct bpf_iter_aux_info *aux)
  1610. {
  1611. struct bpf_map *map;
  1612. int err = -EINVAL;
  1613. if (!linfo->map.map_fd)
  1614. return -EBADF;
  1615. map = bpf_map_get_with_uref(linfo->map.map_fd);
  1616. if (IS_ERR(map))
  1617. return PTR_ERR(map);
  1618. if (map->map_type != BPF_MAP_TYPE_SOCKMAP &&
  1619. map->map_type != BPF_MAP_TYPE_SOCKHASH)
  1620. goto put_map;
  1621. if (prog->aux->max_rdonly_access > map->key_size) {
  1622. err = -EACCES;
  1623. goto put_map;
  1624. }
  1625. aux->map = map;
  1626. return 0;
  1627. put_map:
  1628. bpf_map_put_with_uref(map);
  1629. return err;
  1630. }
  1631. static void sock_map_iter_detach_target(struct bpf_iter_aux_info *aux)
  1632. {
  1633. bpf_map_put_with_uref(aux->map);
  1634. }
  1635. static struct bpf_iter_reg sock_map_iter_reg = {
  1636. .target = "sockmap",
  1637. .attach_target = sock_map_iter_attach_target,
  1638. .detach_target = sock_map_iter_detach_target,
  1639. .show_fdinfo = bpf_iter_map_show_fdinfo,
  1640. .fill_link_info = bpf_iter_map_fill_link_info,
  1641. .ctx_arg_info_size = 2,
  1642. .ctx_arg_info = {
  1643. { offsetof(struct bpf_iter__sockmap, key),
  1644. PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY },
  1645. { offsetof(struct bpf_iter__sockmap, sk),
  1646. PTR_TO_BTF_ID_OR_NULL },
  1647. },
  1648. };
  1649. static int __init bpf_sockmap_iter_init(void)
  1650. {
  1651. sock_map_iter_reg.ctx_arg_info[1].btf_id =
  1652. btf_sock_ids[BTF_SOCK_TYPE_SOCK];
  1653. return bpf_iter_reg_target(&sock_map_iter_reg);
  1654. }
  1655. late_initcall(bpf_sockmap_iter_init);