sock_map.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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/filter.h>
  5. #include <linux/errno.h>
  6. #include <linux/file.h>
  7. #include <linux/net.h>
  8. #include <linux/workqueue.h>
  9. #include <linux/skmsg.h>
  10. #include <linux/list.h>
  11. #include <linux/jhash.h>
  12. struct bpf_stab {
  13. struct bpf_map map;
  14. struct sock **sks;
  15. struct sk_psock_progs progs;
  16. raw_spinlock_t lock;
  17. };
  18. #define SOCK_CREATE_FLAG_MASK \
  19. (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
  20. static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
  21. {
  22. struct bpf_stab *stab;
  23. u64 cost;
  24. int err;
  25. if (!capable(CAP_NET_ADMIN))
  26. return ERR_PTR(-EPERM);
  27. if (attr->max_entries == 0 ||
  28. attr->key_size != 4 ||
  29. attr->value_size != 4 ||
  30. attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
  31. return ERR_PTR(-EINVAL);
  32. stab = kzalloc(sizeof(*stab), GFP_USER);
  33. if (!stab)
  34. return ERR_PTR(-ENOMEM);
  35. bpf_map_init_from_attr(&stab->map, attr);
  36. raw_spin_lock_init(&stab->lock);
  37. /* Make sure page count doesn't overflow. */
  38. cost = (u64) stab->map.max_entries * sizeof(struct sock *);
  39. if (cost >= U32_MAX - PAGE_SIZE) {
  40. err = -EINVAL;
  41. goto free_stab;
  42. }
  43. stab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
  44. err = bpf_map_precharge_memlock(stab->map.pages);
  45. if (err)
  46. goto free_stab;
  47. stab->sks = bpf_map_area_alloc(stab->map.max_entries *
  48. sizeof(struct sock *),
  49. stab->map.numa_node);
  50. if (stab->sks)
  51. return &stab->map;
  52. err = -ENOMEM;
  53. free_stab:
  54. kfree(stab);
  55. return ERR_PTR(err);
  56. }
  57. int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog)
  58. {
  59. u32 ufd = attr->target_fd;
  60. struct bpf_map *map;
  61. struct fd f;
  62. int ret;
  63. f = fdget(ufd);
  64. map = __bpf_map_get(f);
  65. if (IS_ERR(map))
  66. return PTR_ERR(map);
  67. ret = sock_map_prog_update(map, prog, attr->attach_type);
  68. fdput(f);
  69. return ret;
  70. }
  71. static void sock_map_sk_acquire(struct sock *sk)
  72. __acquires(&sk->sk_lock.slock)
  73. {
  74. lock_sock(sk);
  75. preempt_disable();
  76. rcu_read_lock();
  77. }
  78. static void sock_map_sk_release(struct sock *sk)
  79. __releases(&sk->sk_lock.slock)
  80. {
  81. rcu_read_unlock();
  82. preempt_enable();
  83. release_sock(sk);
  84. }
  85. static void sock_map_add_link(struct sk_psock *psock,
  86. struct sk_psock_link *link,
  87. struct bpf_map *map, void *link_raw)
  88. {
  89. link->link_raw = link_raw;
  90. link->map = map;
  91. spin_lock_bh(&psock->link_lock);
  92. list_add_tail(&link->list, &psock->link);
  93. spin_unlock_bh(&psock->link_lock);
  94. }
  95. static void sock_map_del_link(struct sock *sk,
  96. struct sk_psock *psock, void *link_raw)
  97. {
  98. struct sk_psock_link *link, *tmp;
  99. bool strp_stop = false;
  100. spin_lock_bh(&psock->link_lock);
  101. list_for_each_entry_safe(link, tmp, &psock->link, list) {
  102. if (link->link_raw == link_raw) {
  103. struct bpf_map *map = link->map;
  104. struct bpf_stab *stab = container_of(map, struct bpf_stab,
  105. map);
  106. if (psock->parser.enabled && stab->progs.skb_parser)
  107. strp_stop = true;
  108. list_del(&link->list);
  109. sk_psock_free_link(link);
  110. }
  111. }
  112. spin_unlock_bh(&psock->link_lock);
  113. if (strp_stop) {
  114. write_lock_bh(&sk->sk_callback_lock);
  115. sk_psock_stop_strp(sk, psock);
  116. write_unlock_bh(&sk->sk_callback_lock);
  117. }
  118. }
  119. static void sock_map_unref(struct sock *sk, void *link_raw)
  120. {
  121. struct sk_psock *psock = sk_psock(sk);
  122. if (likely(psock)) {
  123. sock_map_del_link(sk, psock, link_raw);
  124. sk_psock_put(sk, psock);
  125. }
  126. }
  127. static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs,
  128. struct sock *sk)
  129. {
  130. struct bpf_prog *msg_parser, *skb_parser, *skb_verdict;
  131. bool skb_progs, sk_psock_is_new = false;
  132. struct sk_psock *psock;
  133. int ret;
  134. skb_verdict = READ_ONCE(progs->skb_verdict);
  135. skb_parser = READ_ONCE(progs->skb_parser);
  136. skb_progs = skb_parser && skb_verdict;
  137. if (skb_progs) {
  138. skb_verdict = bpf_prog_inc_not_zero(skb_verdict);
  139. if (IS_ERR(skb_verdict))
  140. return PTR_ERR(skb_verdict);
  141. skb_parser = bpf_prog_inc_not_zero(skb_parser);
  142. if (IS_ERR(skb_parser)) {
  143. bpf_prog_put(skb_verdict);
  144. return PTR_ERR(skb_parser);
  145. }
  146. }
  147. msg_parser = READ_ONCE(progs->msg_parser);
  148. if (msg_parser) {
  149. msg_parser = bpf_prog_inc_not_zero(msg_parser);
  150. if (IS_ERR(msg_parser)) {
  151. ret = PTR_ERR(msg_parser);
  152. goto out;
  153. }
  154. }
  155. psock = sk_psock_get_checked(sk);
  156. if (IS_ERR(psock)) {
  157. ret = PTR_ERR(psock);
  158. goto out_progs;
  159. }
  160. if (psock) {
  161. if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) ||
  162. (skb_progs && READ_ONCE(psock->progs.skb_parser))) {
  163. sk_psock_put(sk, psock);
  164. ret = -EBUSY;
  165. goto out_progs;
  166. }
  167. } else {
  168. psock = sk_psock_init(sk, map->numa_node);
  169. if (!psock) {
  170. ret = -ENOMEM;
  171. goto out_progs;
  172. }
  173. sk_psock_is_new = true;
  174. }
  175. if (msg_parser)
  176. psock_set_prog(&psock->progs.msg_parser, msg_parser);
  177. if (sk_psock_is_new) {
  178. ret = tcp_bpf_init(sk);
  179. if (ret < 0)
  180. goto out_drop;
  181. } else {
  182. tcp_bpf_reinit(sk);
  183. }
  184. write_lock_bh(&sk->sk_callback_lock);
  185. if (skb_progs && !psock->parser.enabled) {
  186. ret = sk_psock_init_strp(sk, psock);
  187. if (ret) {
  188. write_unlock_bh(&sk->sk_callback_lock);
  189. goto out_drop;
  190. }
  191. psock_set_prog(&psock->progs.skb_verdict, skb_verdict);
  192. psock_set_prog(&psock->progs.skb_parser, skb_parser);
  193. sk_psock_start_strp(sk, psock);
  194. }
  195. write_unlock_bh(&sk->sk_callback_lock);
  196. return 0;
  197. out_drop:
  198. sk_psock_put(sk, psock);
  199. out_progs:
  200. if (msg_parser)
  201. bpf_prog_put(msg_parser);
  202. out:
  203. if (skb_progs) {
  204. bpf_prog_put(skb_verdict);
  205. bpf_prog_put(skb_parser);
  206. }
  207. return ret;
  208. }
  209. static void sock_map_free(struct bpf_map *map)
  210. {
  211. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  212. int i;
  213. synchronize_rcu();
  214. rcu_read_lock();
  215. raw_spin_lock_bh(&stab->lock);
  216. for (i = 0; i < stab->map.max_entries; i++) {
  217. struct sock **psk = &stab->sks[i];
  218. struct sock *sk;
  219. sk = xchg(psk, NULL);
  220. if (sk)
  221. sock_map_unref(sk, psk);
  222. }
  223. raw_spin_unlock_bh(&stab->lock);
  224. rcu_read_unlock();
  225. bpf_map_area_free(stab->sks);
  226. kfree(stab);
  227. }
  228. static void sock_map_release_progs(struct bpf_map *map)
  229. {
  230. psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs);
  231. }
  232. static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
  233. {
  234. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  235. WARN_ON_ONCE(!rcu_read_lock_held());
  236. if (unlikely(key >= map->max_entries))
  237. return NULL;
  238. return READ_ONCE(stab->sks[key]);
  239. }
  240. static void *sock_map_lookup(struct bpf_map *map, void *key)
  241. {
  242. return ERR_PTR(-EOPNOTSUPP);
  243. }
  244. static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
  245. struct sock **psk)
  246. {
  247. struct sock *sk;
  248. raw_spin_lock_bh(&stab->lock);
  249. sk = *psk;
  250. if (!sk_test || sk_test == sk)
  251. *psk = NULL;
  252. raw_spin_unlock_bh(&stab->lock);
  253. if (unlikely(!sk))
  254. return -EINVAL;
  255. sock_map_unref(sk, psk);
  256. return 0;
  257. }
  258. static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk,
  259. void *link_raw)
  260. {
  261. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  262. __sock_map_delete(stab, sk, link_raw);
  263. }
  264. static int sock_map_delete_elem(struct bpf_map *map, void *key)
  265. {
  266. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  267. u32 i = *(u32 *)key;
  268. struct sock **psk;
  269. if (unlikely(i >= map->max_entries))
  270. return -EINVAL;
  271. psk = &stab->sks[i];
  272. return __sock_map_delete(stab, NULL, psk);
  273. }
  274. static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next)
  275. {
  276. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  277. u32 i = key ? *(u32 *)key : U32_MAX;
  278. u32 *key_next = next;
  279. if (i == stab->map.max_entries - 1)
  280. return -ENOENT;
  281. if (i >= stab->map.max_entries)
  282. *key_next = 0;
  283. else
  284. *key_next = i + 1;
  285. return 0;
  286. }
  287. static int sock_map_update_common(struct bpf_map *map, u32 idx,
  288. struct sock *sk, u64 flags)
  289. {
  290. struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
  291. struct sk_psock_link *link;
  292. struct sk_psock *psock;
  293. struct sock *osk;
  294. int ret;
  295. WARN_ON_ONCE(!rcu_read_lock_held());
  296. if (unlikely(flags > BPF_EXIST))
  297. return -EINVAL;
  298. if (unlikely(idx >= map->max_entries))
  299. return -E2BIG;
  300. link = sk_psock_init_link();
  301. if (!link)
  302. return -ENOMEM;
  303. ret = sock_map_link(map, &stab->progs, sk);
  304. if (ret < 0)
  305. goto out_free;
  306. psock = sk_psock(sk);
  307. WARN_ON_ONCE(!psock);
  308. raw_spin_lock_bh(&stab->lock);
  309. osk = stab->sks[idx];
  310. if (osk && flags == BPF_NOEXIST) {
  311. ret = -EEXIST;
  312. goto out_unlock;
  313. } else if (!osk && flags == BPF_EXIST) {
  314. ret = -ENOENT;
  315. goto out_unlock;
  316. }
  317. sock_map_add_link(psock, link, map, &stab->sks[idx]);
  318. stab->sks[idx] = sk;
  319. if (osk)
  320. sock_map_unref(osk, &stab->sks[idx]);
  321. raw_spin_unlock_bh(&stab->lock);
  322. return 0;
  323. out_unlock:
  324. raw_spin_unlock_bh(&stab->lock);
  325. if (psock)
  326. sk_psock_put(sk, psock);
  327. out_free:
  328. sk_psock_free_link(link);
  329. return ret;
  330. }
  331. static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
  332. {
  333. return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB ||
  334. ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB;
  335. }
  336. static bool sock_map_sk_is_suitable(const struct sock *sk)
  337. {
  338. return sk->sk_type == SOCK_STREAM &&
  339. sk->sk_protocol == IPPROTO_TCP;
  340. }
  341. static int sock_map_update_elem(struct bpf_map *map, void *key,
  342. void *value, u64 flags)
  343. {
  344. u32 ufd = *(u32 *)value;
  345. u32 idx = *(u32 *)key;
  346. struct socket *sock;
  347. struct sock *sk;
  348. int ret;
  349. sock = sockfd_lookup(ufd, &ret);
  350. if (!sock)
  351. return ret;
  352. sk = sock->sk;
  353. if (!sk) {
  354. ret = -EINVAL;
  355. goto out;
  356. }
  357. if (!sock_map_sk_is_suitable(sk) ||
  358. sk->sk_state != TCP_ESTABLISHED) {
  359. ret = -EOPNOTSUPP;
  360. goto out;
  361. }
  362. sock_map_sk_acquire(sk);
  363. ret = sock_map_update_common(map, idx, sk, flags);
  364. sock_map_sk_release(sk);
  365. out:
  366. fput(sock->file);
  367. return ret;
  368. }
  369. BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops,
  370. struct bpf_map *, map, void *, key, u64, flags)
  371. {
  372. WARN_ON_ONCE(!rcu_read_lock_held());
  373. if (likely(sock_map_sk_is_suitable(sops->sk) &&
  374. sock_map_op_okay(sops)))
  375. return sock_map_update_common(map, *(u32 *)key, sops->sk,
  376. flags);
  377. return -EOPNOTSUPP;
  378. }
  379. const struct bpf_func_proto bpf_sock_map_update_proto = {
  380. .func = bpf_sock_map_update,
  381. .gpl_only = false,
  382. .pkt_access = true,
  383. .ret_type = RET_INTEGER,
  384. .arg1_type = ARG_PTR_TO_CTX,
  385. .arg2_type = ARG_CONST_MAP_PTR,
  386. .arg3_type = ARG_PTR_TO_MAP_KEY,
  387. .arg4_type = ARG_ANYTHING,
  388. };
  389. BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
  390. struct bpf_map *, map, u32, key, u64, flags)
  391. {
  392. struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
  393. if (unlikely(flags & ~(BPF_F_INGRESS)))
  394. return SK_DROP;
  395. tcb->bpf.flags = flags;
  396. tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
  397. if (!tcb->bpf.sk_redir)
  398. return SK_DROP;
  399. return SK_PASS;
  400. }
  401. const struct bpf_func_proto bpf_sk_redirect_map_proto = {
  402. .func = bpf_sk_redirect_map,
  403. .gpl_only = false,
  404. .ret_type = RET_INTEGER,
  405. .arg1_type = ARG_PTR_TO_CTX,
  406. .arg2_type = ARG_CONST_MAP_PTR,
  407. .arg3_type = ARG_ANYTHING,
  408. .arg4_type = ARG_ANYTHING,
  409. };
  410. BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
  411. struct bpf_map *, map, u32, key, u64, flags)
  412. {
  413. if (unlikely(flags & ~(BPF_F_INGRESS)))
  414. return SK_DROP;
  415. msg->flags = flags;
  416. msg->sk_redir = __sock_map_lookup_elem(map, key);
  417. if (!msg->sk_redir)
  418. return SK_DROP;
  419. return SK_PASS;
  420. }
  421. const struct bpf_func_proto bpf_msg_redirect_map_proto = {
  422. .func = bpf_msg_redirect_map,
  423. .gpl_only = false,
  424. .ret_type = RET_INTEGER,
  425. .arg1_type = ARG_PTR_TO_CTX,
  426. .arg2_type = ARG_CONST_MAP_PTR,
  427. .arg3_type = ARG_ANYTHING,
  428. .arg4_type = ARG_ANYTHING,
  429. };
  430. const struct bpf_map_ops sock_map_ops = {
  431. .map_alloc = sock_map_alloc,
  432. .map_free = sock_map_free,
  433. .map_get_next_key = sock_map_get_next_key,
  434. .map_update_elem = sock_map_update_elem,
  435. .map_delete_elem = sock_map_delete_elem,
  436. .map_lookup_elem = sock_map_lookup,
  437. .map_release_uref = sock_map_release_progs,
  438. .map_check_btf = map_check_no_btf,
  439. };
  440. struct bpf_htab_elem {
  441. struct rcu_head rcu;
  442. u32 hash;
  443. struct sock *sk;
  444. struct hlist_node node;
  445. u8 key[0];
  446. };
  447. struct bpf_htab_bucket {
  448. struct hlist_head head;
  449. raw_spinlock_t lock;
  450. };
  451. struct bpf_htab {
  452. struct bpf_map map;
  453. struct bpf_htab_bucket *buckets;
  454. u32 buckets_num;
  455. u32 elem_size;
  456. struct sk_psock_progs progs;
  457. atomic_t count;
  458. };
  459. static inline u32 sock_hash_bucket_hash(const void *key, u32 len)
  460. {
  461. return jhash(key, len, 0);
  462. }
  463. static struct bpf_htab_bucket *sock_hash_select_bucket(struct bpf_htab *htab,
  464. u32 hash)
  465. {
  466. return &htab->buckets[hash & (htab->buckets_num - 1)];
  467. }
  468. static struct bpf_htab_elem *
  469. sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,
  470. u32 key_size)
  471. {
  472. struct bpf_htab_elem *elem;
  473. hlist_for_each_entry_rcu(elem, head, node) {
  474. if (elem->hash == hash &&
  475. !memcmp(&elem->key, key, key_size))
  476. return elem;
  477. }
  478. return NULL;
  479. }
  480. static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
  481. {
  482. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  483. u32 key_size = map->key_size, hash;
  484. struct bpf_htab_bucket *bucket;
  485. struct bpf_htab_elem *elem;
  486. WARN_ON_ONCE(!rcu_read_lock_held());
  487. hash = sock_hash_bucket_hash(key, key_size);
  488. bucket = sock_hash_select_bucket(htab, hash);
  489. elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
  490. return elem ? elem->sk : NULL;
  491. }
  492. static void sock_hash_free_elem(struct bpf_htab *htab,
  493. struct bpf_htab_elem *elem)
  494. {
  495. atomic_dec(&htab->count);
  496. kfree_rcu(elem, rcu);
  497. }
  498. static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,
  499. void *link_raw)
  500. {
  501. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  502. struct bpf_htab_elem *elem_probe, *elem = link_raw;
  503. struct bpf_htab_bucket *bucket;
  504. WARN_ON_ONCE(!rcu_read_lock_held());
  505. bucket = sock_hash_select_bucket(htab, elem->hash);
  506. /* elem may be deleted in parallel from the map, but access here
  507. * is okay since it's going away only after RCU grace period.
  508. * However, we need to check whether it's still present.
  509. */
  510. raw_spin_lock_bh(&bucket->lock);
  511. elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash,
  512. elem->key, map->key_size);
  513. if (elem_probe && elem_probe == elem) {
  514. hlist_del_rcu(&elem->node);
  515. sock_map_unref(elem->sk, elem);
  516. sock_hash_free_elem(htab, elem);
  517. }
  518. raw_spin_unlock_bh(&bucket->lock);
  519. }
  520. static int sock_hash_delete_elem(struct bpf_map *map, void *key)
  521. {
  522. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  523. u32 hash, key_size = map->key_size;
  524. struct bpf_htab_bucket *bucket;
  525. struct bpf_htab_elem *elem;
  526. int ret = -ENOENT;
  527. hash = sock_hash_bucket_hash(key, key_size);
  528. bucket = sock_hash_select_bucket(htab, hash);
  529. raw_spin_lock_bh(&bucket->lock);
  530. elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
  531. if (elem) {
  532. hlist_del_rcu(&elem->node);
  533. sock_map_unref(elem->sk, elem);
  534. sock_hash_free_elem(htab, elem);
  535. ret = 0;
  536. }
  537. raw_spin_unlock_bh(&bucket->lock);
  538. return ret;
  539. }
  540. static struct bpf_htab_elem *sock_hash_alloc_elem(struct bpf_htab *htab,
  541. void *key, u32 key_size,
  542. u32 hash, struct sock *sk,
  543. struct bpf_htab_elem *old)
  544. {
  545. struct bpf_htab_elem *new;
  546. if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
  547. if (!old) {
  548. atomic_dec(&htab->count);
  549. return ERR_PTR(-E2BIG);
  550. }
  551. }
  552. new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
  553. htab->map.numa_node);
  554. if (!new) {
  555. atomic_dec(&htab->count);
  556. return ERR_PTR(-ENOMEM);
  557. }
  558. memcpy(new->key, key, key_size);
  559. new->sk = sk;
  560. new->hash = hash;
  561. return new;
  562. }
  563. static int sock_hash_update_common(struct bpf_map *map, void *key,
  564. struct sock *sk, u64 flags)
  565. {
  566. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  567. u32 key_size = map->key_size, hash;
  568. struct bpf_htab_elem *elem, *elem_new;
  569. struct bpf_htab_bucket *bucket;
  570. struct sk_psock_link *link;
  571. struct sk_psock *psock;
  572. int ret;
  573. WARN_ON_ONCE(!rcu_read_lock_held());
  574. if (unlikely(flags > BPF_EXIST))
  575. return -EINVAL;
  576. link = sk_psock_init_link();
  577. if (!link)
  578. return -ENOMEM;
  579. ret = sock_map_link(map, &htab->progs, sk);
  580. if (ret < 0)
  581. goto out_free;
  582. psock = sk_psock(sk);
  583. WARN_ON_ONCE(!psock);
  584. hash = sock_hash_bucket_hash(key, key_size);
  585. bucket = sock_hash_select_bucket(htab, hash);
  586. raw_spin_lock_bh(&bucket->lock);
  587. elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size);
  588. if (elem && flags == BPF_NOEXIST) {
  589. ret = -EEXIST;
  590. goto out_unlock;
  591. } else if (!elem && flags == BPF_EXIST) {
  592. ret = -ENOENT;
  593. goto out_unlock;
  594. }
  595. elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem);
  596. if (IS_ERR(elem_new)) {
  597. ret = PTR_ERR(elem_new);
  598. goto out_unlock;
  599. }
  600. sock_map_add_link(psock, link, map, elem_new);
  601. /* Add new element to the head of the list, so that
  602. * concurrent search will find it before old elem.
  603. */
  604. hlist_add_head_rcu(&elem_new->node, &bucket->head);
  605. if (elem) {
  606. hlist_del_rcu(&elem->node);
  607. sock_map_unref(elem->sk, elem);
  608. sock_hash_free_elem(htab, elem);
  609. }
  610. raw_spin_unlock_bh(&bucket->lock);
  611. return 0;
  612. out_unlock:
  613. raw_spin_unlock_bh(&bucket->lock);
  614. sk_psock_put(sk, psock);
  615. out_free:
  616. sk_psock_free_link(link);
  617. return ret;
  618. }
  619. static int sock_hash_update_elem(struct bpf_map *map, void *key,
  620. void *value, u64 flags)
  621. {
  622. u32 ufd = *(u32 *)value;
  623. struct socket *sock;
  624. struct sock *sk;
  625. int ret;
  626. sock = sockfd_lookup(ufd, &ret);
  627. if (!sock)
  628. return ret;
  629. sk = sock->sk;
  630. if (!sk) {
  631. ret = -EINVAL;
  632. goto out;
  633. }
  634. if (!sock_map_sk_is_suitable(sk) ||
  635. sk->sk_state != TCP_ESTABLISHED) {
  636. ret = -EOPNOTSUPP;
  637. goto out;
  638. }
  639. sock_map_sk_acquire(sk);
  640. ret = sock_hash_update_common(map, key, sk, flags);
  641. sock_map_sk_release(sk);
  642. out:
  643. fput(sock->file);
  644. return ret;
  645. }
  646. static int sock_hash_get_next_key(struct bpf_map *map, void *key,
  647. void *key_next)
  648. {
  649. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  650. struct bpf_htab_elem *elem, *elem_next;
  651. u32 hash, key_size = map->key_size;
  652. struct hlist_head *head;
  653. int i = 0;
  654. if (!key)
  655. goto find_first_elem;
  656. hash = sock_hash_bucket_hash(key, key_size);
  657. head = &sock_hash_select_bucket(htab, hash)->head;
  658. elem = sock_hash_lookup_elem_raw(head, hash, key, key_size);
  659. if (!elem)
  660. goto find_first_elem;
  661. elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&elem->node)),
  662. struct bpf_htab_elem, node);
  663. if (elem_next) {
  664. memcpy(key_next, elem_next->key, key_size);
  665. return 0;
  666. }
  667. i = hash & (htab->buckets_num - 1);
  668. i++;
  669. find_first_elem:
  670. for (; i < htab->buckets_num; i++) {
  671. head = &sock_hash_select_bucket(htab, i)->head;
  672. elem_next = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
  673. struct bpf_htab_elem, node);
  674. if (elem_next) {
  675. memcpy(key_next, elem_next->key, key_size);
  676. return 0;
  677. }
  678. }
  679. return -ENOENT;
  680. }
  681. static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
  682. {
  683. struct bpf_htab *htab;
  684. int i, err;
  685. u64 cost;
  686. if (!capable(CAP_NET_ADMIN))
  687. return ERR_PTR(-EPERM);
  688. if (attr->max_entries == 0 ||
  689. attr->key_size == 0 ||
  690. attr->value_size != 4 ||
  691. attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
  692. return ERR_PTR(-EINVAL);
  693. if (attr->key_size > MAX_BPF_STACK)
  694. return ERR_PTR(-E2BIG);
  695. htab = kzalloc(sizeof(*htab), GFP_USER);
  696. if (!htab)
  697. return ERR_PTR(-ENOMEM);
  698. bpf_map_init_from_attr(&htab->map, attr);
  699. htab->buckets_num = roundup_pow_of_two(htab->map.max_entries);
  700. htab->elem_size = sizeof(struct bpf_htab_elem) +
  701. round_up(htab->map.key_size, 8);
  702. if (htab->buckets_num == 0 ||
  703. htab->buckets_num > U32_MAX / sizeof(struct bpf_htab_bucket)) {
  704. err = -EINVAL;
  705. goto free_htab;
  706. }
  707. cost = (u64) htab->buckets_num * sizeof(struct bpf_htab_bucket) +
  708. (u64) htab->elem_size * htab->map.max_entries;
  709. if (cost >= U32_MAX - PAGE_SIZE) {
  710. err = -EINVAL;
  711. goto free_htab;
  712. }
  713. htab->buckets = bpf_map_area_alloc(htab->buckets_num *
  714. sizeof(struct bpf_htab_bucket),
  715. htab->map.numa_node);
  716. if (!htab->buckets) {
  717. err = -ENOMEM;
  718. goto free_htab;
  719. }
  720. for (i = 0; i < htab->buckets_num; i++) {
  721. INIT_HLIST_HEAD(&htab->buckets[i].head);
  722. raw_spin_lock_init(&htab->buckets[i].lock);
  723. }
  724. return &htab->map;
  725. free_htab:
  726. kfree(htab);
  727. return ERR_PTR(err);
  728. }
  729. static void sock_hash_free(struct bpf_map *map)
  730. {
  731. struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
  732. struct bpf_htab_bucket *bucket;
  733. struct bpf_htab_elem *elem;
  734. struct hlist_node *node;
  735. int i;
  736. synchronize_rcu();
  737. rcu_read_lock();
  738. for (i = 0; i < htab->buckets_num; i++) {
  739. bucket = sock_hash_select_bucket(htab, i);
  740. raw_spin_lock_bh(&bucket->lock);
  741. hlist_for_each_entry_safe(elem, node, &bucket->head, node) {
  742. hlist_del_rcu(&elem->node);
  743. sock_map_unref(elem->sk, elem);
  744. }
  745. raw_spin_unlock_bh(&bucket->lock);
  746. }
  747. rcu_read_unlock();
  748. bpf_map_area_free(htab->buckets);
  749. kfree(htab);
  750. }
  751. static void sock_hash_release_progs(struct bpf_map *map)
  752. {
  753. psock_progs_drop(&container_of(map, struct bpf_htab, map)->progs);
  754. }
  755. BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
  756. struct bpf_map *, map, void *, key, u64, flags)
  757. {
  758. WARN_ON_ONCE(!rcu_read_lock_held());
  759. if (likely(sock_map_sk_is_suitable(sops->sk) &&
  760. sock_map_op_okay(sops)))
  761. return sock_hash_update_common(map, key, sops->sk, flags);
  762. return -EOPNOTSUPP;
  763. }
  764. const struct bpf_func_proto bpf_sock_hash_update_proto = {
  765. .func = bpf_sock_hash_update,
  766. .gpl_only = false,
  767. .pkt_access = true,
  768. .ret_type = RET_INTEGER,
  769. .arg1_type = ARG_PTR_TO_CTX,
  770. .arg2_type = ARG_CONST_MAP_PTR,
  771. .arg3_type = ARG_PTR_TO_MAP_KEY,
  772. .arg4_type = ARG_ANYTHING,
  773. };
  774. BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
  775. struct bpf_map *, map, void *, key, u64, flags)
  776. {
  777. struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
  778. if (unlikely(flags & ~(BPF_F_INGRESS)))
  779. return SK_DROP;
  780. tcb->bpf.flags = flags;
  781. tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key);
  782. if (!tcb->bpf.sk_redir)
  783. return SK_DROP;
  784. return SK_PASS;
  785. }
  786. const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
  787. .func = bpf_sk_redirect_hash,
  788. .gpl_only = false,
  789. .ret_type = RET_INTEGER,
  790. .arg1_type = ARG_PTR_TO_CTX,
  791. .arg2_type = ARG_CONST_MAP_PTR,
  792. .arg3_type = ARG_PTR_TO_MAP_KEY,
  793. .arg4_type = ARG_ANYTHING,
  794. };
  795. BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
  796. struct bpf_map *, map, void *, key, u64, flags)
  797. {
  798. if (unlikely(flags & ~(BPF_F_INGRESS)))
  799. return SK_DROP;
  800. msg->flags = flags;
  801. msg->sk_redir = __sock_hash_lookup_elem(map, key);
  802. if (!msg->sk_redir)
  803. return SK_DROP;
  804. return SK_PASS;
  805. }
  806. const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
  807. .func = bpf_msg_redirect_hash,
  808. .gpl_only = false,
  809. .ret_type = RET_INTEGER,
  810. .arg1_type = ARG_PTR_TO_CTX,
  811. .arg2_type = ARG_CONST_MAP_PTR,
  812. .arg3_type = ARG_PTR_TO_MAP_KEY,
  813. .arg4_type = ARG_ANYTHING,
  814. };
  815. const struct bpf_map_ops sock_hash_ops = {
  816. .map_alloc = sock_hash_alloc,
  817. .map_free = sock_hash_free,
  818. .map_get_next_key = sock_hash_get_next_key,
  819. .map_update_elem = sock_hash_update_elem,
  820. .map_delete_elem = sock_hash_delete_elem,
  821. .map_lookup_elem = sock_map_lookup,
  822. .map_release_uref = sock_hash_release_progs,
  823. .map_check_btf = map_check_no_btf,
  824. };
  825. static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
  826. {
  827. switch (map->map_type) {
  828. case BPF_MAP_TYPE_SOCKMAP:
  829. return &container_of(map, struct bpf_stab, map)->progs;
  830. case BPF_MAP_TYPE_SOCKHASH:
  831. return &container_of(map, struct bpf_htab, map)->progs;
  832. default:
  833. break;
  834. }
  835. return NULL;
  836. }
  837. int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
  838. u32 which)
  839. {
  840. struct sk_psock_progs *progs = sock_map_progs(map);
  841. if (!progs)
  842. return -EOPNOTSUPP;
  843. switch (which) {
  844. case BPF_SK_MSG_VERDICT:
  845. psock_set_prog(&progs->msg_parser, prog);
  846. break;
  847. case BPF_SK_SKB_STREAM_PARSER:
  848. psock_set_prog(&progs->skb_parser, prog);
  849. break;
  850. case BPF_SK_SKB_STREAM_VERDICT:
  851. psock_set_prog(&progs->skb_verdict, prog);
  852. break;
  853. default:
  854. return -EOPNOTSUPP;
  855. }
  856. return 0;
  857. }
  858. void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link)
  859. {
  860. switch (link->map->map_type) {
  861. case BPF_MAP_TYPE_SOCKMAP:
  862. return sock_map_delete_from_link(link->map, sk,
  863. link->link_raw);
  864. case BPF_MAP_TYPE_SOCKHASH:
  865. return sock_hash_delete_from_link(link->map, sk,
  866. link->link_raw);
  867. default:
  868. break;
  869. }
  870. }