tcp_ao.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * INET An implementation of the TCP Authentication Option (TCP-AO).
  4. * See RFC5925.
  5. *
  6. * Authors: Dmitry Safonov <dima@arista.com>
  7. * Francesco Ruggeri <fruggeri@arista.com>
  8. * Salam Noureddine <noureddine@arista.com>
  9. */
  10. #define pr_fmt(fmt) "TCP: " fmt
  11. #include <crypto/hash.h>
  12. #include <linux/inetdevice.h>
  13. #include <linux/tcp.h>
  14. #include <net/tcp.h>
  15. #include <net/ipv6.h>
  16. #include <net/icmp.h>
  17. #include <trace/events/tcp.h>
  18. DEFINE_STATIC_KEY_DEFERRED_FALSE(tcp_ao_needed, HZ);
  19. int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx,
  20. unsigned int len, struct tcp_sigpool *hp)
  21. {
  22. struct scatterlist sg;
  23. int ret;
  24. if (crypto_ahash_setkey(crypto_ahash_reqtfm(hp->req),
  25. mkt->key, mkt->keylen))
  26. goto clear_hash;
  27. ret = crypto_ahash_init(hp->req);
  28. if (ret)
  29. goto clear_hash;
  30. sg_init_one(&sg, ctx, len);
  31. ahash_request_set_crypt(hp->req, &sg, key, len);
  32. crypto_ahash_update(hp->req);
  33. ret = crypto_ahash_final(hp->req);
  34. if (ret)
  35. goto clear_hash;
  36. return 0;
  37. clear_hash:
  38. memset(key, 0, tcp_ao_digest_size(mkt));
  39. return 1;
  40. }
  41. bool tcp_ao_ignore_icmp(const struct sock *sk, int family, int type, int code)
  42. {
  43. bool ignore_icmp = false;
  44. struct tcp_ao_info *ao;
  45. if (!static_branch_unlikely(&tcp_ao_needed.key))
  46. return false;
  47. /* RFC5925, 7.8:
  48. * >> A TCP-AO implementation MUST default to ignore incoming ICMPv4
  49. * messages of Type 3 (destination unreachable), Codes 2-4 (protocol
  50. * unreachable, port unreachable, and fragmentation needed -- ’hard
  51. * errors’), and ICMPv6 Type 1 (destination unreachable), Code 1
  52. * (administratively prohibited) and Code 4 (port unreachable) intended
  53. * for connections in synchronized states (ESTABLISHED, FIN-WAIT-1, FIN-
  54. * WAIT-2, CLOSE-WAIT, CLOSING, LAST-ACK, TIME-WAIT) that match MKTs.
  55. */
  56. if (family == AF_INET) {
  57. if (type != ICMP_DEST_UNREACH)
  58. return false;
  59. if (code < ICMP_PROT_UNREACH || code > ICMP_FRAG_NEEDED)
  60. return false;
  61. } else {
  62. if (type != ICMPV6_DEST_UNREACH)
  63. return false;
  64. if (code != ICMPV6_ADM_PROHIBITED && code != ICMPV6_PORT_UNREACH)
  65. return false;
  66. }
  67. rcu_read_lock();
  68. switch (sk->sk_state) {
  69. case TCP_TIME_WAIT:
  70. ao = rcu_dereference(tcp_twsk(sk)->ao_info);
  71. break;
  72. case TCP_SYN_SENT:
  73. case TCP_SYN_RECV:
  74. case TCP_LISTEN:
  75. case TCP_NEW_SYN_RECV:
  76. /* RFC5925 specifies to ignore ICMPs *only* on connections
  77. * in synchronized states.
  78. */
  79. rcu_read_unlock();
  80. return false;
  81. default:
  82. ao = rcu_dereference(tcp_sk(sk)->ao_info);
  83. }
  84. if (ao && !ao->accept_icmps) {
  85. ignore_icmp = true;
  86. __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAODROPPEDICMPS);
  87. atomic64_inc(&ao->counters.dropped_icmp);
  88. }
  89. rcu_read_unlock();
  90. return ignore_icmp;
  91. }
  92. /* Optimized version of tcp_ao_do_lookup(): only for sockets for which
  93. * it's known that the keys in ao_info are matching peer's
  94. * family/address/VRF/etc.
  95. */
  96. struct tcp_ao_key *tcp_ao_established_key(const struct sock *sk,
  97. struct tcp_ao_info *ao,
  98. int sndid, int rcvid)
  99. {
  100. struct tcp_ao_key *key;
  101. hlist_for_each_entry_rcu(key, &ao->head, node, lockdep_sock_is_held(sk)) {
  102. if ((sndid >= 0 && key->sndid != sndid) ||
  103. (rcvid >= 0 && key->rcvid != rcvid))
  104. continue;
  105. return key;
  106. }
  107. return NULL;
  108. }
  109. static int ipv4_prefix_cmp(const struct in_addr *addr1,
  110. const struct in_addr *addr2,
  111. unsigned int prefixlen)
  112. {
  113. __be32 mask = inet_make_mask(prefixlen);
  114. __be32 a1 = addr1->s_addr & mask;
  115. __be32 a2 = addr2->s_addr & mask;
  116. if (a1 == a2)
  117. return 0;
  118. return memcmp(&a1, &a2, sizeof(a1));
  119. }
  120. static int __tcp_ao_key_cmp(const struct tcp_ao_key *key, int l3index,
  121. const union tcp_ao_addr *addr, u8 prefixlen,
  122. int family, int sndid, int rcvid)
  123. {
  124. if (sndid >= 0 && key->sndid != sndid)
  125. return (key->sndid > sndid) ? 1 : -1;
  126. if (rcvid >= 0 && key->rcvid != rcvid)
  127. return (key->rcvid > rcvid) ? 1 : -1;
  128. if (l3index >= 0 && (key->keyflags & TCP_AO_KEYF_IFINDEX)) {
  129. if (key->l3index != l3index)
  130. return (key->l3index > l3index) ? 1 : -1;
  131. }
  132. if (family == AF_UNSPEC)
  133. return 0;
  134. if (key->family != family)
  135. return (key->family > family) ? 1 : -1;
  136. if (family == AF_INET) {
  137. if (ntohl(key->addr.a4.s_addr) == INADDR_ANY)
  138. return 0;
  139. if (ntohl(addr->a4.s_addr) == INADDR_ANY)
  140. return 0;
  141. return ipv4_prefix_cmp(&key->addr.a4, &addr->a4, prefixlen);
  142. #if IS_ENABLED(CONFIG_IPV6)
  143. } else {
  144. if (ipv6_addr_any(&key->addr.a6) || ipv6_addr_any(&addr->a6))
  145. return 0;
  146. if (ipv6_prefix_equal(&key->addr.a6, &addr->a6, prefixlen))
  147. return 0;
  148. return memcmp(&key->addr.a6, &addr->a6, sizeof(addr->a6));
  149. #endif
  150. }
  151. return -1;
  152. }
  153. static int tcp_ao_key_cmp(const struct tcp_ao_key *key, int l3index,
  154. const union tcp_ao_addr *addr, u8 prefixlen,
  155. int family, int sndid, int rcvid)
  156. {
  157. #if IS_ENABLED(CONFIG_IPV6)
  158. if (family == AF_INET6 && ipv6_addr_v4mapped(&addr->a6)) {
  159. __be32 addr4 = addr->a6.s6_addr32[3];
  160. return __tcp_ao_key_cmp(key, l3index,
  161. (union tcp_ao_addr *)&addr4,
  162. prefixlen, AF_INET, sndid, rcvid);
  163. }
  164. #endif
  165. return __tcp_ao_key_cmp(key, l3index, addr,
  166. prefixlen, family, sndid, rcvid);
  167. }
  168. static struct tcp_ao_key *__tcp_ao_do_lookup(const struct sock *sk, int l3index,
  169. const union tcp_ao_addr *addr, int family, u8 prefix,
  170. int sndid, int rcvid)
  171. {
  172. struct tcp_ao_key *key;
  173. struct tcp_ao_info *ao;
  174. if (!static_branch_unlikely(&tcp_ao_needed.key))
  175. return NULL;
  176. ao = rcu_dereference_check(tcp_sk(sk)->ao_info,
  177. lockdep_sock_is_held(sk));
  178. if (!ao)
  179. return NULL;
  180. hlist_for_each_entry_rcu(key, &ao->head, node, lockdep_sock_is_held(sk)) {
  181. u8 prefixlen = min(prefix, key->prefixlen);
  182. if (!tcp_ao_key_cmp(key, l3index, addr, prefixlen,
  183. family, sndid, rcvid))
  184. return key;
  185. }
  186. return NULL;
  187. }
  188. struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, int l3index,
  189. const union tcp_ao_addr *addr,
  190. int family, int sndid, int rcvid)
  191. {
  192. return __tcp_ao_do_lookup(sk, l3index, addr, family, U8_MAX, sndid, rcvid);
  193. }
  194. static struct tcp_ao_info *tcp_ao_alloc_info(gfp_t flags)
  195. {
  196. struct tcp_ao_info *ao;
  197. ao = kzalloc(sizeof(*ao), flags);
  198. if (!ao)
  199. return NULL;
  200. INIT_HLIST_HEAD(&ao->head);
  201. refcount_set(&ao->refcnt, 1);
  202. return ao;
  203. }
  204. static void tcp_ao_link_mkt(struct tcp_ao_info *ao, struct tcp_ao_key *mkt)
  205. {
  206. hlist_add_head_rcu(&mkt->node, &ao->head);
  207. }
  208. static struct tcp_ao_key *tcp_ao_copy_key(struct sock *sk,
  209. struct tcp_ao_key *key)
  210. {
  211. struct tcp_ao_key *new_key;
  212. new_key = sock_kmalloc(sk, tcp_ao_sizeof_key(key),
  213. GFP_ATOMIC);
  214. if (!new_key)
  215. return NULL;
  216. *new_key = *key;
  217. INIT_HLIST_NODE(&new_key->node);
  218. tcp_sigpool_get(new_key->tcp_sigpool_id);
  219. atomic64_set(&new_key->pkt_good, 0);
  220. atomic64_set(&new_key->pkt_bad, 0);
  221. return new_key;
  222. }
  223. static void tcp_ao_key_free_rcu(struct rcu_head *head)
  224. {
  225. struct tcp_ao_key *key = container_of(head, struct tcp_ao_key, rcu);
  226. tcp_sigpool_release(key->tcp_sigpool_id);
  227. kfree_sensitive(key);
  228. }
  229. static void tcp_ao_info_free_rcu(struct rcu_head *head)
  230. {
  231. struct tcp_ao_info *ao = container_of(head, struct tcp_ao_info, rcu);
  232. struct tcp_ao_key *key;
  233. struct hlist_node *n;
  234. hlist_for_each_entry_safe(key, n, &ao->head, node) {
  235. hlist_del(&key->node);
  236. tcp_sigpool_release(key->tcp_sigpool_id);
  237. kfree_sensitive(key);
  238. }
  239. kfree(ao);
  240. static_branch_slow_dec_deferred(&tcp_ao_needed);
  241. }
  242. static void tcp_ao_sk_omem_free(struct sock *sk, struct tcp_ao_info *ao)
  243. {
  244. size_t total_ao_sk_mem = 0;
  245. struct tcp_ao_key *key;
  246. hlist_for_each_entry(key, &ao->head, node)
  247. total_ao_sk_mem += tcp_ao_sizeof_key(key);
  248. atomic_sub(total_ao_sk_mem, &sk->sk_omem_alloc);
  249. }
  250. void tcp_ao_destroy_sock(struct sock *sk, bool twsk)
  251. {
  252. struct tcp_ao_info *ao;
  253. if (twsk) {
  254. ao = rcu_dereference_protected(tcp_twsk(sk)->ao_info, 1);
  255. rcu_assign_pointer(tcp_twsk(sk)->ao_info, NULL);
  256. } else {
  257. ao = rcu_dereference_protected(tcp_sk(sk)->ao_info, 1);
  258. rcu_assign_pointer(tcp_sk(sk)->ao_info, NULL);
  259. }
  260. if (!ao || !refcount_dec_and_test(&ao->refcnt))
  261. return;
  262. if (!twsk)
  263. tcp_ao_sk_omem_free(sk, ao);
  264. call_rcu(&ao->rcu, tcp_ao_info_free_rcu);
  265. }
  266. void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp)
  267. {
  268. struct tcp_ao_info *ao_info = rcu_dereference_protected(tp->ao_info, 1);
  269. if (ao_info) {
  270. struct tcp_ao_key *key;
  271. struct hlist_node *n;
  272. int omem = 0;
  273. hlist_for_each_entry_safe(key, n, &ao_info->head, node) {
  274. omem += tcp_ao_sizeof_key(key);
  275. }
  276. refcount_inc(&ao_info->refcnt);
  277. atomic_sub(omem, &(((struct sock *)tp)->sk_omem_alloc));
  278. rcu_assign_pointer(tcptw->ao_info, ao_info);
  279. } else {
  280. tcptw->ao_info = NULL;
  281. }
  282. }
  283. /* 4 tuple and ISNs are expected in NBO */
  284. static int tcp_v4_ao_calc_key(struct tcp_ao_key *mkt, u8 *key,
  285. __be32 saddr, __be32 daddr,
  286. __be16 sport, __be16 dport,
  287. __be32 sisn, __be32 disn)
  288. {
  289. /* See RFC5926 3.1.1 */
  290. struct kdf_input_block {
  291. u8 counter;
  292. u8 label[6];
  293. struct tcp4_ao_context ctx;
  294. __be16 outlen;
  295. } __packed * tmp;
  296. struct tcp_sigpool hp;
  297. int err;
  298. err = tcp_sigpool_start(mkt->tcp_sigpool_id, &hp);
  299. if (err)
  300. return err;
  301. tmp = hp.scratch;
  302. tmp->counter = 1;
  303. memcpy(tmp->label, "TCP-AO", 6);
  304. tmp->ctx.saddr = saddr;
  305. tmp->ctx.daddr = daddr;
  306. tmp->ctx.sport = sport;
  307. tmp->ctx.dport = dport;
  308. tmp->ctx.sisn = sisn;
  309. tmp->ctx.disn = disn;
  310. tmp->outlen = htons(tcp_ao_digest_size(mkt) * 8); /* in bits */
  311. err = tcp_ao_calc_traffic_key(mkt, key, tmp, sizeof(*tmp), &hp);
  312. tcp_sigpool_end(&hp);
  313. return err;
  314. }
  315. int tcp_v4_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
  316. const struct sock *sk,
  317. __be32 sisn, __be32 disn, bool send)
  318. {
  319. if (send)
  320. return tcp_v4_ao_calc_key(mkt, key, sk->sk_rcv_saddr,
  321. sk->sk_daddr, htons(sk->sk_num),
  322. sk->sk_dport, sisn, disn);
  323. else
  324. return tcp_v4_ao_calc_key(mkt, key, sk->sk_daddr,
  325. sk->sk_rcv_saddr, sk->sk_dport,
  326. htons(sk->sk_num), disn, sisn);
  327. }
  328. static int tcp_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
  329. const struct sock *sk,
  330. __be32 sisn, __be32 disn, bool send)
  331. {
  332. if (mkt->family == AF_INET)
  333. return tcp_v4_ao_calc_key_sk(mkt, key, sk, sisn, disn, send);
  334. #if IS_ENABLED(CONFIG_IPV6)
  335. else if (mkt->family == AF_INET6)
  336. return tcp_v6_ao_calc_key_sk(mkt, key, sk, sisn, disn, send);
  337. #endif
  338. else
  339. return -EOPNOTSUPP;
  340. }
  341. int tcp_v4_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key,
  342. struct request_sock *req)
  343. {
  344. struct inet_request_sock *ireq = inet_rsk(req);
  345. return tcp_v4_ao_calc_key(mkt, key,
  346. ireq->ir_loc_addr, ireq->ir_rmt_addr,
  347. htons(ireq->ir_num), ireq->ir_rmt_port,
  348. htonl(tcp_rsk(req)->snt_isn),
  349. htonl(tcp_rsk(req)->rcv_isn));
  350. }
  351. static int tcp_v4_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *key,
  352. const struct sk_buff *skb,
  353. __be32 sisn, __be32 disn)
  354. {
  355. const struct iphdr *iph = ip_hdr(skb);
  356. const struct tcphdr *th = tcp_hdr(skb);
  357. return tcp_v4_ao_calc_key(mkt, key, iph->saddr, iph->daddr,
  358. th->source, th->dest, sisn, disn);
  359. }
  360. static int tcp_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *key,
  361. const struct sk_buff *skb,
  362. __be32 sisn, __be32 disn, int family)
  363. {
  364. if (family == AF_INET)
  365. return tcp_v4_ao_calc_key_skb(mkt, key, skb, sisn, disn);
  366. #if IS_ENABLED(CONFIG_IPV6)
  367. else if (family == AF_INET6)
  368. return tcp_v6_ao_calc_key_skb(mkt, key, skb, sisn, disn);
  369. #endif
  370. return -EAFNOSUPPORT;
  371. }
  372. static int tcp_v4_ao_hash_pseudoheader(struct tcp_sigpool *hp,
  373. __be32 daddr, __be32 saddr,
  374. int nbytes)
  375. {
  376. struct tcp4_pseudohdr *bp;
  377. struct scatterlist sg;
  378. bp = hp->scratch;
  379. bp->saddr = saddr;
  380. bp->daddr = daddr;
  381. bp->pad = 0;
  382. bp->protocol = IPPROTO_TCP;
  383. bp->len = cpu_to_be16(nbytes);
  384. sg_init_one(&sg, bp, sizeof(*bp));
  385. ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp));
  386. return crypto_ahash_update(hp->req);
  387. }
  388. static int tcp_ao_hash_pseudoheader(unsigned short int family,
  389. const struct sock *sk,
  390. const struct sk_buff *skb,
  391. struct tcp_sigpool *hp, int nbytes)
  392. {
  393. const struct tcphdr *th = tcp_hdr(skb);
  394. /* TODO: Can we rely on checksum being zero to mean outbound pkt? */
  395. if (!th->check) {
  396. if (family == AF_INET)
  397. return tcp_v4_ao_hash_pseudoheader(hp, sk->sk_daddr,
  398. sk->sk_rcv_saddr, skb->len);
  399. #if IS_ENABLED(CONFIG_IPV6)
  400. else if (family == AF_INET6)
  401. return tcp_v6_ao_hash_pseudoheader(hp, &sk->sk_v6_daddr,
  402. &sk->sk_v6_rcv_saddr, skb->len);
  403. #endif
  404. else
  405. return -EAFNOSUPPORT;
  406. }
  407. if (family == AF_INET) {
  408. const struct iphdr *iph = ip_hdr(skb);
  409. return tcp_v4_ao_hash_pseudoheader(hp, iph->daddr,
  410. iph->saddr, skb->len);
  411. #if IS_ENABLED(CONFIG_IPV6)
  412. } else if (family == AF_INET6) {
  413. const struct ipv6hdr *iph = ipv6_hdr(skb);
  414. return tcp_v6_ao_hash_pseudoheader(hp, &iph->daddr,
  415. &iph->saddr, skb->len);
  416. #endif
  417. }
  418. return -EAFNOSUPPORT;
  419. }
  420. u32 tcp_ao_compute_sne(u32 next_sne, u32 next_seq, u32 seq)
  421. {
  422. u32 sne = next_sne;
  423. if (before(seq, next_seq)) {
  424. if (seq > next_seq)
  425. sne--;
  426. } else {
  427. if (seq < next_seq)
  428. sne++;
  429. }
  430. return sne;
  431. }
  432. /* tcp_ao_hash_sne(struct tcp_sigpool *hp)
  433. * @hp - used for hashing
  434. * @sne - sne value
  435. */
  436. static int tcp_ao_hash_sne(struct tcp_sigpool *hp, u32 sne)
  437. {
  438. struct scatterlist sg;
  439. __be32 *bp;
  440. bp = (__be32 *)hp->scratch;
  441. *bp = htonl(sne);
  442. sg_init_one(&sg, bp, sizeof(*bp));
  443. ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp));
  444. return crypto_ahash_update(hp->req);
  445. }
  446. static int tcp_ao_hash_header(struct tcp_sigpool *hp,
  447. const struct tcphdr *th,
  448. bool exclude_options, u8 *hash,
  449. int hash_offset, int hash_len)
  450. {
  451. struct scatterlist sg;
  452. u8 *hdr = hp->scratch;
  453. int err, len;
  454. /* We are not allowed to change tcphdr, make a local copy */
  455. if (exclude_options) {
  456. len = sizeof(*th) + sizeof(struct tcp_ao_hdr) + hash_len;
  457. memcpy(hdr, th, sizeof(*th));
  458. memcpy(hdr + sizeof(*th),
  459. (u8 *)th + hash_offset - sizeof(struct tcp_ao_hdr),
  460. sizeof(struct tcp_ao_hdr));
  461. memset(hdr + sizeof(*th) + sizeof(struct tcp_ao_hdr),
  462. 0, hash_len);
  463. ((struct tcphdr *)hdr)->check = 0;
  464. } else {
  465. len = th->doff << 2;
  466. memcpy(hdr, th, len);
  467. /* zero out tcp-ao hash */
  468. ((struct tcphdr *)hdr)->check = 0;
  469. memset(hdr + hash_offset, 0, hash_len);
  470. }
  471. sg_init_one(&sg, hdr, len);
  472. ahash_request_set_crypt(hp->req, &sg, NULL, len);
  473. err = crypto_ahash_update(hp->req);
  474. WARN_ON_ONCE(err != 0);
  475. return err;
  476. }
  477. int tcp_ao_hash_hdr(unsigned short int family, char *ao_hash,
  478. struct tcp_ao_key *key, const u8 *tkey,
  479. const union tcp_ao_addr *daddr,
  480. const union tcp_ao_addr *saddr,
  481. const struct tcphdr *th, u32 sne)
  482. {
  483. int tkey_len = tcp_ao_digest_size(key);
  484. int hash_offset = ao_hash - (char *)th;
  485. struct tcp_sigpool hp;
  486. void *hash_buf = NULL;
  487. hash_buf = kmalloc(tkey_len, GFP_ATOMIC);
  488. if (!hash_buf)
  489. goto clear_hash_noput;
  490. if (tcp_sigpool_start(key->tcp_sigpool_id, &hp))
  491. goto clear_hash_noput;
  492. if (crypto_ahash_setkey(crypto_ahash_reqtfm(hp.req), tkey, tkey_len))
  493. goto clear_hash;
  494. if (crypto_ahash_init(hp.req))
  495. goto clear_hash;
  496. if (tcp_ao_hash_sne(&hp, sne))
  497. goto clear_hash;
  498. if (family == AF_INET) {
  499. if (tcp_v4_ao_hash_pseudoheader(&hp, daddr->a4.s_addr,
  500. saddr->a4.s_addr, th->doff * 4))
  501. goto clear_hash;
  502. #if IS_ENABLED(CONFIG_IPV6)
  503. } else if (family == AF_INET6) {
  504. if (tcp_v6_ao_hash_pseudoheader(&hp, &daddr->a6,
  505. &saddr->a6, th->doff * 4))
  506. goto clear_hash;
  507. #endif
  508. } else {
  509. WARN_ON_ONCE(1);
  510. goto clear_hash;
  511. }
  512. if (tcp_ao_hash_header(&hp, th,
  513. !!(key->keyflags & TCP_AO_KEYF_EXCLUDE_OPT),
  514. ao_hash, hash_offset, tcp_ao_maclen(key)))
  515. goto clear_hash;
  516. ahash_request_set_crypt(hp.req, NULL, hash_buf, 0);
  517. if (crypto_ahash_final(hp.req))
  518. goto clear_hash;
  519. memcpy(ao_hash, hash_buf, tcp_ao_maclen(key));
  520. tcp_sigpool_end(&hp);
  521. kfree(hash_buf);
  522. return 0;
  523. clear_hash:
  524. tcp_sigpool_end(&hp);
  525. clear_hash_noput:
  526. memset(ao_hash, 0, tcp_ao_maclen(key));
  527. kfree(hash_buf);
  528. return 1;
  529. }
  530. int tcp_ao_hash_skb(unsigned short int family,
  531. char *ao_hash, struct tcp_ao_key *key,
  532. const struct sock *sk, const struct sk_buff *skb,
  533. const u8 *tkey, int hash_offset, u32 sne)
  534. {
  535. const struct tcphdr *th = tcp_hdr(skb);
  536. int tkey_len = tcp_ao_digest_size(key);
  537. struct tcp_sigpool hp;
  538. void *hash_buf = NULL;
  539. hash_buf = kmalloc(tkey_len, GFP_ATOMIC);
  540. if (!hash_buf)
  541. goto clear_hash_noput;
  542. if (tcp_sigpool_start(key->tcp_sigpool_id, &hp))
  543. goto clear_hash_noput;
  544. if (crypto_ahash_setkey(crypto_ahash_reqtfm(hp.req), tkey, tkey_len))
  545. goto clear_hash;
  546. /* For now use sha1 by default. Depends on alg in tcp_ao_key */
  547. if (crypto_ahash_init(hp.req))
  548. goto clear_hash;
  549. if (tcp_ao_hash_sne(&hp, sne))
  550. goto clear_hash;
  551. if (tcp_ao_hash_pseudoheader(family, sk, skb, &hp, skb->len))
  552. goto clear_hash;
  553. if (tcp_ao_hash_header(&hp, th,
  554. !!(key->keyflags & TCP_AO_KEYF_EXCLUDE_OPT),
  555. ao_hash, hash_offset, tcp_ao_maclen(key)))
  556. goto clear_hash;
  557. if (tcp_sigpool_hash_skb_data(&hp, skb, th->doff << 2))
  558. goto clear_hash;
  559. ahash_request_set_crypt(hp.req, NULL, hash_buf, 0);
  560. if (crypto_ahash_final(hp.req))
  561. goto clear_hash;
  562. memcpy(ao_hash, hash_buf, tcp_ao_maclen(key));
  563. tcp_sigpool_end(&hp);
  564. kfree(hash_buf);
  565. return 0;
  566. clear_hash:
  567. tcp_sigpool_end(&hp);
  568. clear_hash_noput:
  569. memset(ao_hash, 0, tcp_ao_maclen(key));
  570. kfree(hash_buf);
  571. return 1;
  572. }
  573. int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key,
  574. const struct sock *sk, const struct sk_buff *skb,
  575. const u8 *tkey, int hash_offset, u32 sne)
  576. {
  577. return tcp_ao_hash_skb(AF_INET, ao_hash, key, sk, skb,
  578. tkey, hash_offset, sne);
  579. }
  580. int tcp_v4_ao_synack_hash(char *ao_hash, struct tcp_ao_key *ao_key,
  581. struct request_sock *req, const struct sk_buff *skb,
  582. int hash_offset, u32 sne)
  583. {
  584. void *hash_buf = NULL;
  585. int err;
  586. hash_buf = kmalloc(tcp_ao_digest_size(ao_key), GFP_ATOMIC);
  587. if (!hash_buf)
  588. return -ENOMEM;
  589. err = tcp_v4_ao_calc_key_rsk(ao_key, hash_buf, req);
  590. if (err)
  591. goto out;
  592. err = tcp_ao_hash_skb(AF_INET, ao_hash, ao_key, req_to_sk(req), skb,
  593. hash_buf, hash_offset, sne);
  594. out:
  595. kfree(hash_buf);
  596. return err;
  597. }
  598. struct tcp_ao_key *tcp_v4_ao_lookup_rsk(const struct sock *sk,
  599. struct request_sock *req,
  600. int sndid, int rcvid)
  601. {
  602. struct inet_request_sock *ireq = inet_rsk(req);
  603. union tcp_ao_addr *addr = (union tcp_ao_addr *)&ireq->ir_rmt_addr;
  604. int l3index;
  605. l3index = l3mdev_master_ifindex_by_index(sock_net(sk), ireq->ir_iif);
  606. return tcp_ao_do_lookup(sk, l3index, addr, AF_INET, sndid, rcvid);
  607. }
  608. struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *addr_sk,
  609. int sndid, int rcvid)
  610. {
  611. int l3index = l3mdev_master_ifindex_by_index(sock_net(sk),
  612. addr_sk->sk_bound_dev_if);
  613. union tcp_ao_addr *addr = (union tcp_ao_addr *)&addr_sk->sk_daddr;
  614. return tcp_ao_do_lookup(sk, l3index, addr, AF_INET, sndid, rcvid);
  615. }
  616. int tcp_ao_prepare_reset(const struct sock *sk, struct sk_buff *skb,
  617. const struct tcp_ao_hdr *aoh, int l3index, u32 seq,
  618. struct tcp_ao_key **key, char **traffic_key,
  619. bool *allocated_traffic_key, u8 *keyid, u32 *sne)
  620. {
  621. const struct tcphdr *th = tcp_hdr(skb);
  622. struct tcp_ao_info *ao_info;
  623. *allocated_traffic_key = false;
  624. /* If there's no socket - than initial sisn/disn are unknown.
  625. * Drop the segment. RFC5925 (7.7) advises to require graceful
  626. * restart [RFC4724]. Alternatively, the RFC5925 advises to
  627. * save/restore traffic keys before/after reboot.
  628. * Linux TCP-AO support provides TCP_AO_ADD_KEY and TCP_AO_REPAIR
  629. * options to restore a socket post-reboot.
  630. */
  631. if (!sk)
  632. return -ENOTCONN;
  633. if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV)) {
  634. unsigned int family = READ_ONCE(sk->sk_family);
  635. union tcp_ao_addr *addr;
  636. __be32 disn, sisn;
  637. if (sk->sk_state == TCP_NEW_SYN_RECV) {
  638. struct request_sock *req = inet_reqsk(sk);
  639. sisn = htonl(tcp_rsk(req)->rcv_isn);
  640. disn = htonl(tcp_rsk(req)->snt_isn);
  641. *sne = tcp_ao_compute_sne(0, tcp_rsk(req)->snt_isn, seq);
  642. } else {
  643. sisn = th->seq;
  644. disn = 0;
  645. }
  646. if (IS_ENABLED(CONFIG_IPV6) && family == AF_INET6)
  647. addr = (union tcp_md5_addr *)&ipv6_hdr(skb)->saddr;
  648. else
  649. addr = (union tcp_md5_addr *)&ip_hdr(skb)->saddr;
  650. #if IS_ENABLED(CONFIG_IPV6)
  651. if (family == AF_INET6 && ipv6_addr_v4mapped(&sk->sk_v6_daddr))
  652. family = AF_INET;
  653. #endif
  654. sk = sk_const_to_full_sk(sk);
  655. ao_info = rcu_dereference(tcp_sk(sk)->ao_info);
  656. if (!ao_info)
  657. return -ENOENT;
  658. *key = tcp_ao_do_lookup(sk, l3index, addr, family,
  659. -1, aoh->rnext_keyid);
  660. if (!*key)
  661. return -ENOENT;
  662. *traffic_key = kmalloc(tcp_ao_digest_size(*key), GFP_ATOMIC);
  663. if (!*traffic_key)
  664. return -ENOMEM;
  665. *allocated_traffic_key = true;
  666. if (tcp_ao_calc_key_skb(*key, *traffic_key, skb,
  667. sisn, disn, family))
  668. return -1;
  669. *keyid = (*key)->rcvid;
  670. } else {
  671. struct tcp_ao_key *rnext_key;
  672. u32 snd_basis;
  673. if (sk->sk_state == TCP_TIME_WAIT) {
  674. ao_info = rcu_dereference(tcp_twsk(sk)->ao_info);
  675. snd_basis = tcp_twsk(sk)->tw_snd_nxt;
  676. } else {
  677. ao_info = rcu_dereference(tcp_sk(sk)->ao_info);
  678. snd_basis = tcp_sk(sk)->snd_una;
  679. }
  680. if (!ao_info)
  681. return -ENOENT;
  682. *key = tcp_ao_established_key(sk, ao_info, aoh->rnext_keyid, -1);
  683. if (!*key)
  684. return -ENOENT;
  685. *traffic_key = snd_other_key(*key);
  686. rnext_key = READ_ONCE(ao_info->rnext_key);
  687. *keyid = rnext_key->rcvid;
  688. *sne = tcp_ao_compute_sne(READ_ONCE(ao_info->snd_sne),
  689. snd_basis, seq);
  690. }
  691. return 0;
  692. }
  693. int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
  694. struct tcp_ao_key *key, struct tcphdr *th,
  695. __u8 *hash_location)
  696. {
  697. struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
  698. struct tcp_sock *tp = tcp_sk(sk);
  699. struct tcp_ao_info *ao;
  700. void *tkey_buf = NULL;
  701. u8 *traffic_key;
  702. u32 sne;
  703. ao = rcu_dereference_protected(tcp_sk(sk)->ao_info,
  704. lockdep_sock_is_held(sk));
  705. traffic_key = snd_other_key(key);
  706. if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) {
  707. __be32 disn;
  708. if (!(tcb->tcp_flags & TCPHDR_ACK)) {
  709. disn = 0;
  710. tkey_buf = kmalloc(tcp_ao_digest_size(key), GFP_ATOMIC);
  711. if (!tkey_buf)
  712. return -ENOMEM;
  713. traffic_key = tkey_buf;
  714. } else {
  715. disn = ao->risn;
  716. }
  717. tp->af_specific->ao_calc_key_sk(key, traffic_key,
  718. sk, ao->lisn, disn, true);
  719. }
  720. sne = tcp_ao_compute_sne(READ_ONCE(ao->snd_sne), READ_ONCE(tp->snd_una),
  721. ntohl(th->seq));
  722. tp->af_specific->calc_ao_hash(hash_location, key, sk, skb, traffic_key,
  723. hash_location - (u8 *)th, sne);
  724. kfree(tkey_buf);
  725. return 0;
  726. }
  727. static struct tcp_ao_key *tcp_ao_inbound_lookup(unsigned short int family,
  728. const struct sock *sk, const struct sk_buff *skb,
  729. int sndid, int rcvid, int l3index)
  730. {
  731. if (family == AF_INET) {
  732. const struct iphdr *iph = ip_hdr(skb);
  733. return tcp_ao_do_lookup(sk, l3index,
  734. (union tcp_ao_addr *)&iph->saddr,
  735. AF_INET, sndid, rcvid);
  736. } else {
  737. const struct ipv6hdr *iph = ipv6_hdr(skb);
  738. return tcp_ao_do_lookup(sk, l3index,
  739. (union tcp_ao_addr *)&iph->saddr,
  740. AF_INET6, sndid, rcvid);
  741. }
  742. }
  743. void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
  744. struct request_sock *req, unsigned short int family)
  745. {
  746. struct tcp_request_sock *treq = tcp_rsk(req);
  747. const struct tcphdr *th = tcp_hdr(skb);
  748. const struct tcp_ao_hdr *aoh;
  749. struct tcp_ao_key *key;
  750. int l3index;
  751. /* treq->af_specific is used to perform TCP_AO lookup
  752. * in tcp_create_openreq_child().
  753. */
  754. #if IS_ENABLED(CONFIG_IPV6)
  755. if (family == AF_INET6)
  756. treq->af_specific = &tcp_request_sock_ipv6_ops;
  757. else
  758. #endif
  759. treq->af_specific = &tcp_request_sock_ipv4_ops;
  760. treq->used_tcp_ao = false;
  761. if (tcp_parse_auth_options(th, NULL, &aoh) || !aoh)
  762. return;
  763. l3index = l3mdev_master_ifindex_by_index(sock_net(sk), inet_rsk(req)->ir_iif);
  764. key = tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid, l3index);
  765. if (!key)
  766. /* Key not found, continue without TCP-AO */
  767. return;
  768. treq->ao_rcv_next = aoh->keyid;
  769. treq->ao_keyid = aoh->rnext_keyid;
  770. treq->used_tcp_ao = true;
  771. }
  772. static enum skb_drop_reason
  773. tcp_ao_verify_hash(const struct sock *sk, const struct sk_buff *skb,
  774. unsigned short int family, struct tcp_ao_info *info,
  775. const struct tcp_ao_hdr *aoh, struct tcp_ao_key *key,
  776. u8 *traffic_key, u8 *phash, u32 sne, int l3index)
  777. {
  778. const struct tcphdr *th = tcp_hdr(skb);
  779. u8 maclen = tcp_ao_hdr_maclen(aoh);
  780. void *hash_buf = NULL;
  781. if (maclen != tcp_ao_maclen(key)) {
  782. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD);
  783. atomic64_inc(&info->counters.pkt_bad);
  784. atomic64_inc(&key->pkt_bad);
  785. trace_tcp_ao_wrong_maclen(sk, skb, aoh->keyid,
  786. aoh->rnext_keyid, maclen);
  787. return SKB_DROP_REASON_TCP_AOFAILURE;
  788. }
  789. hash_buf = kmalloc(tcp_ao_digest_size(key), GFP_ATOMIC);
  790. if (!hash_buf)
  791. return SKB_DROP_REASON_NOT_SPECIFIED;
  792. /* XXX: make it per-AF callback? */
  793. tcp_ao_hash_skb(family, hash_buf, key, sk, skb, traffic_key,
  794. (phash - (u8 *)th), sne);
  795. if (memcmp(phash, hash_buf, maclen)) {
  796. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD);
  797. atomic64_inc(&info->counters.pkt_bad);
  798. atomic64_inc(&key->pkt_bad);
  799. trace_tcp_ao_mismatch(sk, skb, aoh->keyid,
  800. aoh->rnext_keyid, maclen);
  801. kfree(hash_buf);
  802. return SKB_DROP_REASON_TCP_AOFAILURE;
  803. }
  804. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOGOOD);
  805. atomic64_inc(&info->counters.pkt_good);
  806. atomic64_inc(&key->pkt_good);
  807. kfree(hash_buf);
  808. return SKB_NOT_DROPPED_YET;
  809. }
  810. enum skb_drop_reason
  811. tcp_inbound_ao_hash(struct sock *sk, const struct sk_buff *skb,
  812. unsigned short int family, const struct request_sock *req,
  813. int l3index, const struct tcp_ao_hdr *aoh)
  814. {
  815. const struct tcphdr *th = tcp_hdr(skb);
  816. u8 maclen = tcp_ao_hdr_maclen(aoh);
  817. u8 *phash = (u8 *)(aoh + 1); /* hash goes just after the header */
  818. struct tcp_ao_info *info;
  819. enum skb_drop_reason ret;
  820. struct tcp_ao_key *key;
  821. __be32 sisn, disn;
  822. u8 *traffic_key;
  823. int state;
  824. u32 sne = 0;
  825. info = rcu_dereference(tcp_sk(sk)->ao_info);
  826. if (!info) {
  827. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOKEYNOTFOUND);
  828. trace_tcp_ao_key_not_found(sk, skb, aoh->keyid,
  829. aoh->rnext_keyid, maclen);
  830. return SKB_DROP_REASON_TCP_AOUNEXPECTED;
  831. }
  832. if (unlikely(th->syn)) {
  833. sisn = th->seq;
  834. disn = 0;
  835. }
  836. state = READ_ONCE(sk->sk_state);
  837. /* Fast-path */
  838. if (likely((1 << state) & TCP_AO_ESTABLISHED)) {
  839. enum skb_drop_reason err;
  840. struct tcp_ao_key *current_key;
  841. /* Check if this socket's rnext_key matches the keyid in the
  842. * packet. If not we lookup the key based on the keyid
  843. * matching the rcvid in the mkt.
  844. */
  845. key = READ_ONCE(info->rnext_key);
  846. if (key->rcvid != aoh->keyid) {
  847. key = tcp_ao_established_key(sk, info, -1, aoh->keyid);
  848. if (!key)
  849. goto key_not_found;
  850. }
  851. /* Delayed retransmitted SYN */
  852. if (unlikely(th->syn && !th->ack))
  853. goto verify_hash;
  854. sne = tcp_ao_compute_sne(info->rcv_sne, tcp_sk(sk)->rcv_nxt,
  855. ntohl(th->seq));
  856. /* Established socket, traffic key are cached */
  857. traffic_key = rcv_other_key(key);
  858. err = tcp_ao_verify_hash(sk, skb, family, info, aoh, key,
  859. traffic_key, phash, sne, l3index);
  860. if (err)
  861. return err;
  862. current_key = READ_ONCE(info->current_key);
  863. /* Key rotation: the peer asks us to use new key (RNext) */
  864. if (unlikely(aoh->rnext_keyid != current_key->sndid)) {
  865. trace_tcp_ao_rnext_request(sk, skb, current_key->sndid,
  866. aoh->rnext_keyid,
  867. tcp_ao_hdr_maclen(aoh));
  868. /* If the key is not found we do nothing. */
  869. key = tcp_ao_established_key(sk, info, aoh->rnext_keyid, -1);
  870. if (key)
  871. /* pairs with tcp_ao_del_cmd */
  872. WRITE_ONCE(info->current_key, key);
  873. }
  874. return SKB_NOT_DROPPED_YET;
  875. }
  876. if (unlikely(state == TCP_CLOSE))
  877. return SKB_DROP_REASON_TCP_CLOSE;
  878. /* Lookup key based on peer address and keyid.
  879. * current_key and rnext_key must not be used on tcp listen
  880. * sockets as otherwise:
  881. * - request sockets would race on those key pointers
  882. * - tcp_ao_del_cmd() allows async key removal
  883. */
  884. key = tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid, l3index);
  885. if (!key)
  886. goto key_not_found;
  887. if (th->syn && !th->ack)
  888. goto verify_hash;
  889. if ((1 << state) & (TCPF_LISTEN | TCPF_NEW_SYN_RECV)) {
  890. /* Make the initial syn the likely case here */
  891. if (unlikely(req)) {
  892. sne = tcp_ao_compute_sne(0, tcp_rsk(req)->rcv_isn,
  893. ntohl(th->seq));
  894. sisn = htonl(tcp_rsk(req)->rcv_isn);
  895. disn = htonl(tcp_rsk(req)->snt_isn);
  896. } else if (unlikely(th->ack && !th->syn)) {
  897. /* Possible syncookie packet */
  898. sisn = htonl(ntohl(th->seq) - 1);
  899. disn = htonl(ntohl(th->ack_seq) - 1);
  900. sne = tcp_ao_compute_sne(0, ntohl(sisn),
  901. ntohl(th->seq));
  902. } else if (unlikely(!th->syn)) {
  903. /* no way to figure out initial sisn/disn - drop */
  904. return SKB_DROP_REASON_TCP_FLAGS;
  905. }
  906. } else if ((1 << state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  907. disn = info->lisn;
  908. if (th->syn || th->rst)
  909. sisn = th->seq;
  910. else
  911. sisn = info->risn;
  912. } else {
  913. WARN_ONCE(1, "TCP-AO: Unexpected sk_state %d", state);
  914. return SKB_DROP_REASON_TCP_AOFAILURE;
  915. }
  916. verify_hash:
  917. traffic_key = kmalloc(tcp_ao_digest_size(key), GFP_ATOMIC);
  918. if (!traffic_key)
  919. return SKB_DROP_REASON_NOT_SPECIFIED;
  920. tcp_ao_calc_key_skb(key, traffic_key, skb, sisn, disn, family);
  921. ret = tcp_ao_verify_hash(sk, skb, family, info, aoh, key,
  922. traffic_key, phash, sne, l3index);
  923. kfree(traffic_key);
  924. return ret;
  925. key_not_found:
  926. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOKEYNOTFOUND);
  927. atomic64_inc(&info->counters.key_not_found);
  928. trace_tcp_ao_key_not_found(sk, skb, aoh->keyid,
  929. aoh->rnext_keyid, maclen);
  930. return SKB_DROP_REASON_TCP_AOKEYNOTFOUND;
  931. }
  932. static int tcp_ao_cache_traffic_keys(const struct sock *sk,
  933. struct tcp_ao_info *ao,
  934. struct tcp_ao_key *ao_key)
  935. {
  936. u8 *traffic_key = snd_other_key(ao_key);
  937. int ret;
  938. ret = tcp_ao_calc_key_sk(ao_key, traffic_key, sk,
  939. ao->lisn, ao->risn, true);
  940. if (ret)
  941. return ret;
  942. traffic_key = rcv_other_key(ao_key);
  943. ret = tcp_ao_calc_key_sk(ao_key, traffic_key, sk,
  944. ao->lisn, ao->risn, false);
  945. return ret;
  946. }
  947. void tcp_ao_connect_init(struct sock *sk)
  948. {
  949. struct tcp_sock *tp = tcp_sk(sk);
  950. struct tcp_ao_info *ao_info;
  951. struct hlist_node *next;
  952. union tcp_ao_addr *addr;
  953. struct tcp_ao_key *key;
  954. int family, l3index;
  955. ao_info = rcu_dereference_protected(tp->ao_info,
  956. lockdep_sock_is_held(sk));
  957. if (!ao_info)
  958. return;
  959. /* Remove all keys that don't match the peer */
  960. family = sk->sk_family;
  961. if (family == AF_INET)
  962. addr = (union tcp_ao_addr *)&sk->sk_daddr;
  963. #if IS_ENABLED(CONFIG_IPV6)
  964. else if (family == AF_INET6)
  965. addr = (union tcp_ao_addr *)&sk->sk_v6_daddr;
  966. #endif
  967. else
  968. return;
  969. l3index = l3mdev_master_ifindex_by_index(sock_net(sk),
  970. sk->sk_bound_dev_if);
  971. hlist_for_each_entry_safe(key, next, &ao_info->head, node) {
  972. if (!tcp_ao_key_cmp(key, l3index, addr, key->prefixlen, family, -1, -1))
  973. continue;
  974. if (key == ao_info->current_key)
  975. ao_info->current_key = NULL;
  976. if (key == ao_info->rnext_key)
  977. ao_info->rnext_key = NULL;
  978. hlist_del_rcu(&key->node);
  979. atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
  980. call_rcu(&key->rcu, tcp_ao_key_free_rcu);
  981. }
  982. key = tp->af_specific->ao_lookup(sk, sk, -1, -1);
  983. if (key) {
  984. /* if current_key or rnext_key were not provided,
  985. * use the first key matching the peer
  986. */
  987. if (!ao_info->current_key)
  988. ao_info->current_key = key;
  989. if (!ao_info->rnext_key)
  990. ao_info->rnext_key = key;
  991. tp->tcp_header_len += tcp_ao_len_aligned(key);
  992. ao_info->lisn = htonl(tp->write_seq);
  993. ao_info->snd_sne = 0;
  994. } else {
  995. /* Can't happen: tcp_connect() verifies that there's
  996. * at least one tcp-ao key that matches the remote peer.
  997. */
  998. WARN_ON_ONCE(1);
  999. rcu_assign_pointer(tp->ao_info, NULL);
  1000. kfree(ao_info);
  1001. }
  1002. }
  1003. void tcp_ao_established(struct sock *sk)
  1004. {
  1005. struct tcp_ao_info *ao;
  1006. struct tcp_ao_key *key;
  1007. ao = rcu_dereference_protected(tcp_sk(sk)->ao_info,
  1008. lockdep_sock_is_held(sk));
  1009. if (!ao)
  1010. return;
  1011. hlist_for_each_entry_rcu(key, &ao->head, node, lockdep_sock_is_held(sk))
  1012. tcp_ao_cache_traffic_keys(sk, ao, key);
  1013. }
  1014. void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb)
  1015. {
  1016. struct tcp_ao_info *ao;
  1017. struct tcp_ao_key *key;
  1018. ao = rcu_dereference_protected(tcp_sk(sk)->ao_info,
  1019. lockdep_sock_is_held(sk));
  1020. if (!ao)
  1021. return;
  1022. /* sk with TCP_REPAIR_ON does not have skb in tcp_finish_connect */
  1023. if (skb)
  1024. WRITE_ONCE(ao->risn, tcp_hdr(skb)->seq);
  1025. ao->rcv_sne = 0;
  1026. hlist_for_each_entry_rcu(key, &ao->head, node, lockdep_sock_is_held(sk))
  1027. tcp_ao_cache_traffic_keys(sk, ao, key);
  1028. }
  1029. int tcp_ao_copy_all_matching(const struct sock *sk, struct sock *newsk,
  1030. struct request_sock *req, struct sk_buff *skb,
  1031. int family)
  1032. {
  1033. struct tcp_ao_key *key, *new_key, *first_key;
  1034. struct tcp_ao_info *new_ao, *ao;
  1035. struct hlist_node *key_head;
  1036. int l3index, ret = -ENOMEM;
  1037. union tcp_ao_addr *addr;
  1038. bool match = false;
  1039. ao = rcu_dereference(tcp_sk(sk)->ao_info);
  1040. if (!ao)
  1041. return 0;
  1042. /* New socket without TCP-AO on it */
  1043. if (!tcp_rsk_used_ao(req))
  1044. return 0;
  1045. new_ao = tcp_ao_alloc_info(GFP_ATOMIC);
  1046. if (!new_ao)
  1047. return -ENOMEM;
  1048. new_ao->lisn = htonl(tcp_rsk(req)->snt_isn);
  1049. new_ao->risn = htonl(tcp_rsk(req)->rcv_isn);
  1050. new_ao->ao_required = ao->ao_required;
  1051. new_ao->accept_icmps = ao->accept_icmps;
  1052. if (family == AF_INET) {
  1053. addr = (union tcp_ao_addr *)&newsk->sk_daddr;
  1054. #if IS_ENABLED(CONFIG_IPV6)
  1055. } else if (family == AF_INET6) {
  1056. addr = (union tcp_ao_addr *)&newsk->sk_v6_daddr;
  1057. #endif
  1058. } else {
  1059. ret = -EAFNOSUPPORT;
  1060. goto free_ao;
  1061. }
  1062. l3index = l3mdev_master_ifindex_by_index(sock_net(newsk),
  1063. newsk->sk_bound_dev_if);
  1064. hlist_for_each_entry_rcu(key, &ao->head, node) {
  1065. if (tcp_ao_key_cmp(key, l3index, addr, key->prefixlen, family, -1, -1))
  1066. continue;
  1067. new_key = tcp_ao_copy_key(newsk, key);
  1068. if (!new_key)
  1069. goto free_and_exit;
  1070. tcp_ao_cache_traffic_keys(newsk, new_ao, new_key);
  1071. tcp_ao_link_mkt(new_ao, new_key);
  1072. match = true;
  1073. }
  1074. if (!match) {
  1075. /* RFC5925 (7.4.1) specifies that the TCP-AO status
  1076. * of a connection is determined on the initial SYN.
  1077. * At this point the connection was TCP-AO enabled, so
  1078. * it can't switch to being unsigned if peer's key
  1079. * disappears on the listening socket.
  1080. */
  1081. ret = -EKEYREJECTED;
  1082. goto free_and_exit;
  1083. }
  1084. if (!static_key_fast_inc_not_disabled(&tcp_ao_needed.key.key)) {
  1085. ret = -EUSERS;
  1086. goto free_and_exit;
  1087. }
  1088. key_head = rcu_dereference(hlist_first_rcu(&new_ao->head));
  1089. first_key = hlist_entry_safe(key_head, struct tcp_ao_key, node);
  1090. key = tcp_ao_established_key(req_to_sk(req), new_ao, tcp_rsk(req)->ao_keyid, -1);
  1091. if (key)
  1092. new_ao->current_key = key;
  1093. else
  1094. new_ao->current_key = first_key;
  1095. /* set rnext_key */
  1096. key = tcp_ao_established_key(req_to_sk(req), new_ao, -1, tcp_rsk(req)->ao_rcv_next);
  1097. if (key)
  1098. new_ao->rnext_key = key;
  1099. else
  1100. new_ao->rnext_key = first_key;
  1101. sk_gso_disable(newsk);
  1102. rcu_assign_pointer(tcp_sk(newsk)->ao_info, new_ao);
  1103. return 0;
  1104. free_and_exit:
  1105. hlist_for_each_entry_safe(key, key_head, &new_ao->head, node) {
  1106. hlist_del(&key->node);
  1107. tcp_sigpool_release(key->tcp_sigpool_id);
  1108. atomic_sub(tcp_ao_sizeof_key(key), &newsk->sk_omem_alloc);
  1109. kfree_sensitive(key);
  1110. }
  1111. free_ao:
  1112. kfree(new_ao);
  1113. return ret;
  1114. }
  1115. static bool tcp_ao_can_set_current_rnext(struct sock *sk)
  1116. {
  1117. /* There aren't current/rnext keys on TCP_LISTEN sockets */
  1118. if (sk->sk_state == TCP_LISTEN)
  1119. return false;
  1120. return true;
  1121. }
  1122. static int tcp_ao_verify_ipv4(struct sock *sk, struct tcp_ao_add *cmd,
  1123. union tcp_ao_addr **addr)
  1124. {
  1125. struct sockaddr_in *sin = (struct sockaddr_in *)&cmd->addr;
  1126. struct inet_sock *inet = inet_sk(sk);
  1127. if (sin->sin_family != AF_INET)
  1128. return -EINVAL;
  1129. /* Currently matching is not performed on port (or port ranges) */
  1130. if (sin->sin_port != 0)
  1131. return -EINVAL;
  1132. /* Check prefix and trailing 0's in addr */
  1133. if (cmd->prefix != 0) {
  1134. __be32 mask;
  1135. if (ntohl(sin->sin_addr.s_addr) == INADDR_ANY)
  1136. return -EINVAL;
  1137. if (cmd->prefix > 32)
  1138. return -EINVAL;
  1139. mask = inet_make_mask(cmd->prefix);
  1140. if (sin->sin_addr.s_addr & ~mask)
  1141. return -EINVAL;
  1142. /* Check that MKT address is consistent with socket */
  1143. if (ntohl(inet->inet_daddr) != INADDR_ANY &&
  1144. (inet->inet_daddr & mask) != sin->sin_addr.s_addr)
  1145. return -EINVAL;
  1146. } else {
  1147. if (ntohl(sin->sin_addr.s_addr) != INADDR_ANY)
  1148. return -EINVAL;
  1149. }
  1150. *addr = (union tcp_ao_addr *)&sin->sin_addr;
  1151. return 0;
  1152. }
  1153. static int tcp_ao_parse_crypto(struct tcp_ao_add *cmd, struct tcp_ao_key *key)
  1154. {
  1155. unsigned int syn_tcp_option_space;
  1156. bool is_kdf_aes_128_cmac = false;
  1157. struct crypto_ahash *tfm;
  1158. struct tcp_sigpool hp;
  1159. void *tmp_key = NULL;
  1160. int err;
  1161. /* RFC5926, 3.1.1.2. KDF_AES_128_CMAC */
  1162. if (!strcmp("cmac(aes128)", cmd->alg_name)) {
  1163. strscpy(cmd->alg_name, "cmac(aes)", sizeof(cmd->alg_name));
  1164. is_kdf_aes_128_cmac = (cmd->keylen != 16);
  1165. tmp_key = kmalloc(cmd->keylen, GFP_KERNEL);
  1166. if (!tmp_key)
  1167. return -ENOMEM;
  1168. }
  1169. key->maclen = cmd->maclen ?: 12; /* 12 is the default in RFC5925 */
  1170. /* Check: maclen + tcp-ao header <= (MAX_TCP_OPTION_SPACE - mss
  1171. * - tstamp (including sackperm)
  1172. * - wscale),
  1173. * see tcp_syn_options(), tcp_synack_options(), commit 33ad798c924b.
  1174. *
  1175. * In order to allow D-SACK with TCP-AO, the header size should be:
  1176. * (MAX_TCP_OPTION_SPACE - TCPOLEN_TSTAMP_ALIGNED
  1177. * - TCPOLEN_SACK_BASE_ALIGNED
  1178. * - 2 * TCPOLEN_SACK_PERBLOCK) = 8 (maclen = 4),
  1179. * see tcp_established_options().
  1180. *
  1181. * RFC5925, 2.2:
  1182. * Typical MACs are 96-128 bits (12-16 bytes), but any length
  1183. * that fits in the header of the segment being authenticated
  1184. * is allowed.
  1185. *
  1186. * RFC5925, 7.6:
  1187. * TCP-AO continues to consume 16 bytes in non-SYN segments,
  1188. * leaving a total of 24 bytes for other options, of which
  1189. * the timestamp consumes 10. This leaves 14 bytes, of which 10
  1190. * are used for a single SACK block. When two SACK blocks are used,
  1191. * such as to handle D-SACK, a smaller TCP-AO MAC would be required
  1192. * to make room for the additional SACK block (i.e., to leave 18
  1193. * bytes for the D-SACK variant of the SACK option) [RFC2883].
  1194. * Note that D-SACK is not supportable in TCP MD5 in the presence
  1195. * of timestamps, because TCP MD5’s MAC length is fixed and too
  1196. * large to leave sufficient option space.
  1197. */
  1198. syn_tcp_option_space = MAX_TCP_OPTION_SPACE;
  1199. syn_tcp_option_space -= TCPOLEN_MSS_ALIGNED;
  1200. syn_tcp_option_space -= TCPOLEN_TSTAMP_ALIGNED;
  1201. syn_tcp_option_space -= TCPOLEN_WSCALE_ALIGNED;
  1202. if (tcp_ao_len_aligned(key) > syn_tcp_option_space) {
  1203. err = -EMSGSIZE;
  1204. goto err_kfree;
  1205. }
  1206. key->keylen = cmd->keylen;
  1207. memcpy(key->key, cmd->key, cmd->keylen);
  1208. err = tcp_sigpool_start(key->tcp_sigpool_id, &hp);
  1209. if (err)
  1210. goto err_kfree;
  1211. tfm = crypto_ahash_reqtfm(hp.req);
  1212. if (is_kdf_aes_128_cmac) {
  1213. void *scratch = hp.scratch;
  1214. struct scatterlist sg;
  1215. memcpy(tmp_key, cmd->key, cmd->keylen);
  1216. sg_init_one(&sg, tmp_key, cmd->keylen);
  1217. /* Using zero-key of 16 bytes as described in RFC5926 */
  1218. memset(scratch, 0, 16);
  1219. err = crypto_ahash_setkey(tfm, scratch, 16);
  1220. if (err)
  1221. goto err_pool_end;
  1222. err = crypto_ahash_init(hp.req);
  1223. if (err)
  1224. goto err_pool_end;
  1225. ahash_request_set_crypt(hp.req, &sg, key->key, cmd->keylen);
  1226. err = crypto_ahash_update(hp.req);
  1227. if (err)
  1228. goto err_pool_end;
  1229. err |= crypto_ahash_final(hp.req);
  1230. if (err)
  1231. goto err_pool_end;
  1232. key->keylen = 16;
  1233. }
  1234. err = crypto_ahash_setkey(tfm, key->key, key->keylen);
  1235. if (err)
  1236. goto err_pool_end;
  1237. tcp_sigpool_end(&hp);
  1238. kfree_sensitive(tmp_key);
  1239. if (tcp_ao_maclen(key) > key->digest_size)
  1240. return -EINVAL;
  1241. return 0;
  1242. err_pool_end:
  1243. tcp_sigpool_end(&hp);
  1244. err_kfree:
  1245. kfree_sensitive(tmp_key);
  1246. return err;
  1247. }
  1248. #if IS_ENABLED(CONFIG_IPV6)
  1249. static int tcp_ao_verify_ipv6(struct sock *sk, struct tcp_ao_add *cmd,
  1250. union tcp_ao_addr **paddr,
  1251. unsigned short int *family)
  1252. {
  1253. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd->addr;
  1254. struct in6_addr *addr = &sin6->sin6_addr;
  1255. u8 prefix = cmd->prefix;
  1256. if (sin6->sin6_family != AF_INET6)
  1257. return -EINVAL;
  1258. /* Currently matching is not performed on port (or port ranges) */
  1259. if (sin6->sin6_port != 0)
  1260. return -EINVAL;
  1261. /* Check prefix and trailing 0's in addr */
  1262. if (cmd->prefix != 0 && ipv6_addr_v4mapped(addr)) {
  1263. __be32 addr4 = addr->s6_addr32[3];
  1264. __be32 mask;
  1265. if (prefix > 32 || ntohl(addr4) == INADDR_ANY)
  1266. return -EINVAL;
  1267. mask = inet_make_mask(prefix);
  1268. if (addr4 & ~mask)
  1269. return -EINVAL;
  1270. /* Check that MKT address is consistent with socket */
  1271. if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
  1272. __be32 daddr4 = sk->sk_v6_daddr.s6_addr32[3];
  1273. if (!ipv6_addr_v4mapped(&sk->sk_v6_daddr))
  1274. return -EINVAL;
  1275. if ((daddr4 & mask) != addr4)
  1276. return -EINVAL;
  1277. }
  1278. *paddr = (union tcp_ao_addr *)&addr->s6_addr32[3];
  1279. *family = AF_INET;
  1280. return 0;
  1281. } else if (cmd->prefix != 0) {
  1282. struct in6_addr pfx;
  1283. if (ipv6_addr_any(addr) || prefix > 128)
  1284. return -EINVAL;
  1285. ipv6_addr_prefix(&pfx, addr, prefix);
  1286. if (ipv6_addr_cmp(&pfx, addr))
  1287. return -EINVAL;
  1288. /* Check that MKT address is consistent with socket */
  1289. if (!ipv6_addr_any(&sk->sk_v6_daddr) &&
  1290. !ipv6_prefix_equal(&sk->sk_v6_daddr, addr, prefix))
  1291. return -EINVAL;
  1292. } else {
  1293. if (!ipv6_addr_any(addr))
  1294. return -EINVAL;
  1295. }
  1296. *paddr = (union tcp_ao_addr *)addr;
  1297. return 0;
  1298. }
  1299. #else
  1300. static int tcp_ao_verify_ipv6(struct sock *sk, struct tcp_ao_add *cmd,
  1301. union tcp_ao_addr **paddr,
  1302. unsigned short int *family)
  1303. {
  1304. return -EOPNOTSUPP;
  1305. }
  1306. #endif
  1307. static struct tcp_ao_info *setsockopt_ao_info(struct sock *sk)
  1308. {
  1309. if (sk_fullsock(sk)) {
  1310. return rcu_dereference_protected(tcp_sk(sk)->ao_info,
  1311. lockdep_sock_is_held(sk));
  1312. } else if (sk->sk_state == TCP_TIME_WAIT) {
  1313. return rcu_dereference_protected(tcp_twsk(sk)->ao_info,
  1314. lockdep_sock_is_held(sk));
  1315. }
  1316. return ERR_PTR(-ESOCKTNOSUPPORT);
  1317. }
  1318. static struct tcp_ao_info *getsockopt_ao_info(struct sock *sk)
  1319. {
  1320. if (sk_fullsock(sk))
  1321. return rcu_dereference(tcp_sk(sk)->ao_info);
  1322. else if (sk->sk_state == TCP_TIME_WAIT)
  1323. return rcu_dereference(tcp_twsk(sk)->ao_info);
  1324. return ERR_PTR(-ESOCKTNOSUPPORT);
  1325. }
  1326. #define TCP_AO_KEYF_ALL (TCP_AO_KEYF_IFINDEX | TCP_AO_KEYF_EXCLUDE_OPT)
  1327. #define TCP_AO_GET_KEYF_VALID (TCP_AO_KEYF_IFINDEX)
  1328. static struct tcp_ao_key *tcp_ao_key_alloc(struct sock *sk,
  1329. struct tcp_ao_add *cmd)
  1330. {
  1331. const char *algo = cmd->alg_name;
  1332. unsigned int digest_size;
  1333. struct crypto_ahash *tfm;
  1334. struct tcp_ao_key *key;
  1335. struct tcp_sigpool hp;
  1336. int err, pool_id;
  1337. size_t size;
  1338. /* Force null-termination of alg_name */
  1339. cmd->alg_name[ARRAY_SIZE(cmd->alg_name) - 1] = '\0';
  1340. /* RFC5926, 3.1.1.2. KDF_AES_128_CMAC */
  1341. if (!strcmp("cmac(aes128)", algo))
  1342. algo = "cmac(aes)";
  1343. /* Full TCP header (th->doff << 2) should fit into scratch area,
  1344. * see tcp_ao_hash_header().
  1345. */
  1346. pool_id = tcp_sigpool_alloc_ahash(algo, 60);
  1347. if (pool_id < 0)
  1348. return ERR_PTR(pool_id);
  1349. err = tcp_sigpool_start(pool_id, &hp);
  1350. if (err)
  1351. goto err_free_pool;
  1352. tfm = crypto_ahash_reqtfm(hp.req);
  1353. digest_size = crypto_ahash_digestsize(tfm);
  1354. tcp_sigpool_end(&hp);
  1355. size = sizeof(struct tcp_ao_key) + (digest_size << 1);
  1356. key = sock_kmalloc(sk, size, GFP_KERNEL);
  1357. if (!key) {
  1358. err = -ENOMEM;
  1359. goto err_free_pool;
  1360. }
  1361. key->tcp_sigpool_id = pool_id;
  1362. key->digest_size = digest_size;
  1363. return key;
  1364. err_free_pool:
  1365. tcp_sigpool_release(pool_id);
  1366. return ERR_PTR(err);
  1367. }
  1368. static int tcp_ao_add_cmd(struct sock *sk, unsigned short int family,
  1369. sockptr_t optval, int optlen)
  1370. {
  1371. struct tcp_ao_info *ao_info;
  1372. union tcp_ao_addr *addr;
  1373. struct tcp_ao_key *key;
  1374. struct tcp_ao_add cmd;
  1375. int ret, l3index = 0;
  1376. bool first = false;
  1377. if (optlen < sizeof(cmd))
  1378. return -EINVAL;
  1379. ret = copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen);
  1380. if (ret)
  1381. return ret;
  1382. if (cmd.keylen > TCP_AO_MAXKEYLEN)
  1383. return -EINVAL;
  1384. if (cmd.reserved != 0 || cmd.reserved2 != 0)
  1385. return -EINVAL;
  1386. if (family == AF_INET)
  1387. ret = tcp_ao_verify_ipv4(sk, &cmd, &addr);
  1388. else
  1389. ret = tcp_ao_verify_ipv6(sk, &cmd, &addr, &family);
  1390. if (ret)
  1391. return ret;
  1392. if (cmd.keyflags & ~TCP_AO_KEYF_ALL)
  1393. return -EINVAL;
  1394. if (cmd.set_current || cmd.set_rnext) {
  1395. if (!tcp_ao_can_set_current_rnext(sk))
  1396. return -EINVAL;
  1397. }
  1398. if (cmd.ifindex && !(cmd.keyflags & TCP_AO_KEYF_IFINDEX))
  1399. return -EINVAL;
  1400. /* For cmd.tcp_ifindex = 0 the key will apply to the default VRF */
  1401. if (cmd.keyflags & TCP_AO_KEYF_IFINDEX && cmd.ifindex) {
  1402. int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
  1403. struct net_device *dev;
  1404. rcu_read_lock();
  1405. dev = dev_get_by_index_rcu(sock_net(sk), cmd.ifindex);
  1406. if (dev && netif_is_l3_master(dev))
  1407. l3index = dev->ifindex;
  1408. rcu_read_unlock();
  1409. if (!dev || !l3index)
  1410. return -EINVAL;
  1411. if (!bound_dev_if || bound_dev_if != cmd.ifindex) {
  1412. /* tcp_ao_established_key() doesn't expect having
  1413. * non peer-matching key on an established TCP-AO
  1414. * connection.
  1415. */
  1416. if (!((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)))
  1417. return -EINVAL;
  1418. }
  1419. /* It's still possible to bind after adding keys or even
  1420. * re-bind to a different dev (with CAP_NET_RAW).
  1421. * So, no reason to return error here, rather try to be
  1422. * nice and warn the user.
  1423. */
  1424. if (bound_dev_if && bound_dev_if != cmd.ifindex)
  1425. net_warn_ratelimited("AO key ifindex %d != sk bound ifindex %d\n",
  1426. cmd.ifindex, bound_dev_if);
  1427. }
  1428. /* Don't allow keys for peers that have a matching TCP-MD5 key */
  1429. if (cmd.keyflags & TCP_AO_KEYF_IFINDEX) {
  1430. /* Non-_exact version of tcp_md5_do_lookup() will
  1431. * as well match keys that aren't bound to a specific VRF
  1432. * (that will make them match AO key with
  1433. * sysctl_tcp_l3dev_accept = 1
  1434. */
  1435. if (tcp_md5_do_lookup(sk, l3index, addr, family))
  1436. return -EKEYREJECTED;
  1437. } else {
  1438. if (tcp_md5_do_lookup_any_l3index(sk, addr, family))
  1439. return -EKEYREJECTED;
  1440. }
  1441. ao_info = setsockopt_ao_info(sk);
  1442. if (IS_ERR(ao_info))
  1443. return PTR_ERR(ao_info);
  1444. if (!ao_info) {
  1445. ao_info = tcp_ao_alloc_info(GFP_KERNEL);
  1446. if (!ao_info)
  1447. return -ENOMEM;
  1448. first = true;
  1449. } else {
  1450. /* Check that neither RecvID nor SendID match any
  1451. * existing key for the peer, RFC5925 3.1:
  1452. * > The IDs of MKTs MUST NOT overlap where their
  1453. * > TCP connection identifiers overlap.
  1454. */
  1455. if (__tcp_ao_do_lookup(sk, l3index, addr, family, cmd.prefix, -1, cmd.rcvid))
  1456. return -EEXIST;
  1457. if (__tcp_ao_do_lookup(sk, l3index, addr, family,
  1458. cmd.prefix, cmd.sndid, -1))
  1459. return -EEXIST;
  1460. }
  1461. key = tcp_ao_key_alloc(sk, &cmd);
  1462. if (IS_ERR(key)) {
  1463. ret = PTR_ERR(key);
  1464. goto err_free_ao;
  1465. }
  1466. INIT_HLIST_NODE(&key->node);
  1467. memcpy(&key->addr, addr, (family == AF_INET) ? sizeof(struct in_addr) :
  1468. sizeof(struct in6_addr));
  1469. key->prefixlen = cmd.prefix;
  1470. key->family = family;
  1471. key->keyflags = cmd.keyflags;
  1472. key->sndid = cmd.sndid;
  1473. key->rcvid = cmd.rcvid;
  1474. key->l3index = l3index;
  1475. atomic64_set(&key->pkt_good, 0);
  1476. atomic64_set(&key->pkt_bad, 0);
  1477. ret = tcp_ao_parse_crypto(&cmd, key);
  1478. if (ret < 0)
  1479. goto err_free_sock;
  1480. if (!((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))) {
  1481. tcp_ao_cache_traffic_keys(sk, ao_info, key);
  1482. if (first) {
  1483. ao_info->current_key = key;
  1484. ao_info->rnext_key = key;
  1485. }
  1486. }
  1487. tcp_ao_link_mkt(ao_info, key);
  1488. if (first) {
  1489. if (!static_branch_inc(&tcp_ao_needed.key)) {
  1490. ret = -EUSERS;
  1491. goto err_free_sock;
  1492. }
  1493. sk_gso_disable(sk);
  1494. rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info);
  1495. }
  1496. if (cmd.set_current)
  1497. WRITE_ONCE(ao_info->current_key, key);
  1498. if (cmd.set_rnext)
  1499. WRITE_ONCE(ao_info->rnext_key, key);
  1500. return 0;
  1501. err_free_sock:
  1502. atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
  1503. tcp_sigpool_release(key->tcp_sigpool_id);
  1504. kfree_sensitive(key);
  1505. err_free_ao:
  1506. if (first)
  1507. kfree(ao_info);
  1508. return ret;
  1509. }
  1510. static int tcp_ao_delete_key(struct sock *sk, struct tcp_ao_info *ao_info,
  1511. bool del_async, struct tcp_ao_key *key,
  1512. struct tcp_ao_key *new_current,
  1513. struct tcp_ao_key *new_rnext)
  1514. {
  1515. int err;
  1516. hlist_del_rcu(&key->node);
  1517. /* Support for async delete on listening sockets: as they don't
  1518. * need current_key/rnext_key maintaining, we don't need to check
  1519. * them and we can just free all resources in RCU fashion.
  1520. */
  1521. if (del_async) {
  1522. atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
  1523. call_rcu(&key->rcu, tcp_ao_key_free_rcu);
  1524. return 0;
  1525. }
  1526. /* At this moment another CPU could have looked this key up
  1527. * while it was unlinked from the list. Wait for RCU grace period,
  1528. * after which the key is off-list and can't be looked up again;
  1529. * the rx path [just before RCU came] might have used it and set it
  1530. * as current_key (very unlikely).
  1531. * Free the key with next RCU grace period (in case it was
  1532. * current_key before tcp_ao_current_rnext() might have
  1533. * changed it in forced-delete).
  1534. */
  1535. synchronize_rcu();
  1536. if (new_current)
  1537. WRITE_ONCE(ao_info->current_key, new_current);
  1538. if (new_rnext)
  1539. WRITE_ONCE(ao_info->rnext_key, new_rnext);
  1540. if (unlikely(READ_ONCE(ao_info->current_key) == key ||
  1541. READ_ONCE(ao_info->rnext_key) == key)) {
  1542. err = -EBUSY;
  1543. goto add_key;
  1544. }
  1545. atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
  1546. call_rcu(&key->rcu, tcp_ao_key_free_rcu);
  1547. return 0;
  1548. add_key:
  1549. hlist_add_head_rcu(&key->node, &ao_info->head);
  1550. return err;
  1551. }
  1552. #define TCP_AO_DEL_KEYF_ALL (TCP_AO_KEYF_IFINDEX)
  1553. static int tcp_ao_del_cmd(struct sock *sk, unsigned short int family,
  1554. sockptr_t optval, int optlen)
  1555. {
  1556. struct tcp_ao_key *key, *new_current = NULL, *new_rnext = NULL;
  1557. int err, addr_len, l3index = 0;
  1558. struct tcp_ao_info *ao_info;
  1559. union tcp_ao_addr *addr;
  1560. struct tcp_ao_del cmd;
  1561. __u8 prefix;
  1562. u16 port;
  1563. if (optlen < sizeof(cmd))
  1564. return -EINVAL;
  1565. err = copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen);
  1566. if (err)
  1567. return err;
  1568. if (cmd.reserved != 0 || cmd.reserved2 != 0)
  1569. return -EINVAL;
  1570. if (cmd.set_current || cmd.set_rnext) {
  1571. if (!tcp_ao_can_set_current_rnext(sk))
  1572. return -EINVAL;
  1573. }
  1574. if (cmd.keyflags & ~TCP_AO_DEL_KEYF_ALL)
  1575. return -EINVAL;
  1576. /* No sanity check for TCP_AO_KEYF_IFINDEX as if a VRF
  1577. * was destroyed, there still should be a way to delete keys,
  1578. * that were bound to that l3intf. So, fail late at lookup stage
  1579. * if there is no key for that ifindex.
  1580. */
  1581. if (cmd.ifindex && !(cmd.keyflags & TCP_AO_KEYF_IFINDEX))
  1582. return -EINVAL;
  1583. ao_info = setsockopt_ao_info(sk);
  1584. if (IS_ERR(ao_info))
  1585. return PTR_ERR(ao_info);
  1586. if (!ao_info)
  1587. return -ENOENT;
  1588. /* For sockets in TCP_CLOSED it's possible set keys that aren't
  1589. * matching the future peer (address/VRF/etc),
  1590. * tcp_ao_connect_init() will choose a correct matching MKT
  1591. * if there's any.
  1592. */
  1593. if (cmd.set_current) {
  1594. new_current = tcp_ao_established_key(sk, ao_info, cmd.current_key, -1);
  1595. if (!new_current)
  1596. return -ENOENT;
  1597. }
  1598. if (cmd.set_rnext) {
  1599. new_rnext = tcp_ao_established_key(sk, ao_info, -1, cmd.rnext);
  1600. if (!new_rnext)
  1601. return -ENOENT;
  1602. }
  1603. if (cmd.del_async && sk->sk_state != TCP_LISTEN)
  1604. return -EINVAL;
  1605. if (family == AF_INET) {
  1606. struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.addr;
  1607. addr = (union tcp_ao_addr *)&sin->sin_addr;
  1608. addr_len = sizeof(struct in_addr);
  1609. port = ntohs(sin->sin_port);
  1610. } else {
  1611. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.addr;
  1612. struct in6_addr *addr6 = &sin6->sin6_addr;
  1613. if (ipv6_addr_v4mapped(addr6)) {
  1614. addr = (union tcp_ao_addr *)&addr6->s6_addr32[3];
  1615. addr_len = sizeof(struct in_addr);
  1616. family = AF_INET;
  1617. } else {
  1618. addr = (union tcp_ao_addr *)addr6;
  1619. addr_len = sizeof(struct in6_addr);
  1620. }
  1621. port = ntohs(sin6->sin6_port);
  1622. }
  1623. prefix = cmd.prefix;
  1624. /* Currently matching is not performed on port (or port ranges) */
  1625. if (port != 0)
  1626. return -EINVAL;
  1627. /* We could choose random present key here for current/rnext
  1628. * but that's less predictable. Let's be strict and don't
  1629. * allow removing a key that's in use. RFC5925 doesn't
  1630. * specify how-to coordinate key removal, but says:
  1631. * "It is presumed that an MKT affecting a particular
  1632. * connection cannot be destroyed during an active connection"
  1633. */
  1634. hlist_for_each_entry_rcu(key, &ao_info->head, node,
  1635. lockdep_sock_is_held(sk)) {
  1636. if (cmd.sndid != key->sndid ||
  1637. cmd.rcvid != key->rcvid)
  1638. continue;
  1639. if (family != key->family ||
  1640. prefix != key->prefixlen ||
  1641. memcmp(addr, &key->addr, addr_len))
  1642. continue;
  1643. if ((cmd.keyflags & TCP_AO_KEYF_IFINDEX) !=
  1644. (key->keyflags & TCP_AO_KEYF_IFINDEX))
  1645. continue;
  1646. if (key->l3index != l3index)
  1647. continue;
  1648. if (key == new_current || key == new_rnext)
  1649. continue;
  1650. return tcp_ao_delete_key(sk, ao_info, cmd.del_async, key,
  1651. new_current, new_rnext);
  1652. }
  1653. return -ENOENT;
  1654. }
  1655. /* cmd.ao_required makes a socket TCP-AO only.
  1656. * Don't allow any md5 keys for any l3intf on the socket together with it.
  1657. * Restricting it early in setsockopt() removes a check for
  1658. * ao_info->ao_required on inbound tcp segment fast-path.
  1659. */
  1660. static int tcp_ao_required_verify(struct sock *sk)
  1661. {
  1662. #ifdef CONFIG_TCP_MD5SIG
  1663. const struct tcp_md5sig_info *md5sig;
  1664. if (!static_branch_unlikely(&tcp_md5_needed.key))
  1665. return 0;
  1666. md5sig = rcu_dereference_check(tcp_sk(sk)->md5sig_info,
  1667. lockdep_sock_is_held(sk));
  1668. if (!md5sig)
  1669. return 0;
  1670. if (rcu_dereference_check(hlist_first_rcu(&md5sig->head),
  1671. lockdep_sock_is_held(sk)))
  1672. return 1;
  1673. #endif
  1674. return 0;
  1675. }
  1676. static int tcp_ao_info_cmd(struct sock *sk, unsigned short int family,
  1677. sockptr_t optval, int optlen)
  1678. {
  1679. struct tcp_ao_key *new_current = NULL, *new_rnext = NULL;
  1680. struct tcp_ao_info *ao_info;
  1681. struct tcp_ao_info_opt cmd;
  1682. bool first = false;
  1683. int err;
  1684. if (optlen < sizeof(cmd))
  1685. return -EINVAL;
  1686. err = copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen);
  1687. if (err)
  1688. return err;
  1689. if (cmd.set_current || cmd.set_rnext) {
  1690. if (!tcp_ao_can_set_current_rnext(sk))
  1691. return -EINVAL;
  1692. }
  1693. if (cmd.reserved != 0 || cmd.reserved2 != 0)
  1694. return -EINVAL;
  1695. ao_info = setsockopt_ao_info(sk);
  1696. if (IS_ERR(ao_info))
  1697. return PTR_ERR(ao_info);
  1698. if (!ao_info) {
  1699. if (!((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)))
  1700. return -EINVAL;
  1701. ao_info = tcp_ao_alloc_info(GFP_KERNEL);
  1702. if (!ao_info)
  1703. return -ENOMEM;
  1704. first = true;
  1705. }
  1706. if (cmd.ao_required && tcp_ao_required_verify(sk)) {
  1707. err = -EKEYREJECTED;
  1708. goto out;
  1709. }
  1710. /* For sockets in TCP_CLOSED it's possible set keys that aren't
  1711. * matching the future peer (address/port/VRF/etc),
  1712. * tcp_ao_connect_init() will choose a correct matching MKT
  1713. * if there's any.
  1714. */
  1715. if (cmd.set_current) {
  1716. new_current = tcp_ao_established_key(sk, ao_info, cmd.current_key, -1);
  1717. if (!new_current) {
  1718. err = -ENOENT;
  1719. goto out;
  1720. }
  1721. }
  1722. if (cmd.set_rnext) {
  1723. new_rnext = tcp_ao_established_key(sk, ao_info, -1, cmd.rnext);
  1724. if (!new_rnext) {
  1725. err = -ENOENT;
  1726. goto out;
  1727. }
  1728. }
  1729. if (cmd.set_counters) {
  1730. atomic64_set(&ao_info->counters.pkt_good, cmd.pkt_good);
  1731. atomic64_set(&ao_info->counters.pkt_bad, cmd.pkt_bad);
  1732. atomic64_set(&ao_info->counters.key_not_found, cmd.pkt_key_not_found);
  1733. atomic64_set(&ao_info->counters.ao_required, cmd.pkt_ao_required);
  1734. atomic64_set(&ao_info->counters.dropped_icmp, cmd.pkt_dropped_icmp);
  1735. }
  1736. ao_info->ao_required = cmd.ao_required;
  1737. ao_info->accept_icmps = cmd.accept_icmps;
  1738. if (new_current)
  1739. WRITE_ONCE(ao_info->current_key, new_current);
  1740. if (new_rnext)
  1741. WRITE_ONCE(ao_info->rnext_key, new_rnext);
  1742. if (first) {
  1743. if (!static_branch_inc(&tcp_ao_needed.key)) {
  1744. err = -EUSERS;
  1745. goto out;
  1746. }
  1747. sk_gso_disable(sk);
  1748. rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info);
  1749. }
  1750. return 0;
  1751. out:
  1752. if (first)
  1753. kfree(ao_info);
  1754. return err;
  1755. }
  1756. int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family,
  1757. sockptr_t optval, int optlen)
  1758. {
  1759. if (WARN_ON_ONCE(family != AF_INET && family != AF_INET6))
  1760. return -EAFNOSUPPORT;
  1761. switch (cmd) {
  1762. case TCP_AO_ADD_KEY:
  1763. return tcp_ao_add_cmd(sk, family, optval, optlen);
  1764. case TCP_AO_DEL_KEY:
  1765. return tcp_ao_del_cmd(sk, family, optval, optlen);
  1766. case TCP_AO_INFO:
  1767. return tcp_ao_info_cmd(sk, family, optval, optlen);
  1768. default:
  1769. WARN_ON_ONCE(1);
  1770. return -EINVAL;
  1771. }
  1772. }
  1773. int tcp_v4_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen)
  1774. {
  1775. return tcp_parse_ao(sk, cmd, AF_INET, optval, optlen);
  1776. }
  1777. /* tcp_ao_copy_mkts_to_user(ao_info, optval, optlen)
  1778. *
  1779. * @ao_info: struct tcp_ao_info on the socket that
  1780. * socket getsockopt(TCP_AO_GET_KEYS) is executed on
  1781. * @optval: pointer to array of tcp_ao_getsockopt structures in user space.
  1782. * Must be != NULL.
  1783. * @optlen: pointer to size of tcp_ao_getsockopt structure.
  1784. * Must be != NULL.
  1785. *
  1786. * Return value: 0 on success, a negative error number otherwise.
  1787. *
  1788. * optval points to an array of tcp_ao_getsockopt structures in user space.
  1789. * optval[0] is used as both input and output to getsockopt. It determines
  1790. * which keys are returned by the kernel.
  1791. * optval[0].nkeys is the size of the array in user space. On return it contains
  1792. * the number of keys matching the search criteria.
  1793. * If tcp_ao_getsockopt::get_all is set, then all keys in the socket are
  1794. * returned, otherwise only keys matching <addr, prefix, sndid, rcvid>
  1795. * in optval[0] are returned.
  1796. * optlen is also used as both input and output. The user provides the size
  1797. * of struct tcp_ao_getsockopt in user space, and the kernel returns the size
  1798. * of the structure in kernel space.
  1799. * The size of struct tcp_ao_getsockopt may differ between user and kernel.
  1800. * There are three cases to consider:
  1801. * * If usize == ksize, then keys are copied verbatim.
  1802. * * If usize < ksize, then the userspace has passed an old struct to a
  1803. * newer kernel. The rest of the trailing bytes in optval[0]
  1804. * (ksize - usize) are interpreted as 0 by the kernel.
  1805. * * If usize > ksize, then the userspace has passed a new struct to an
  1806. * older kernel. The trailing bytes unknown to the kernel (usize - ksize)
  1807. * are checked to ensure they are zeroed, otherwise -E2BIG is returned.
  1808. * On return the kernel fills in min(usize, ksize) in each entry of the array.
  1809. * The layout of the fields in the user and kernel structures is expected to
  1810. * be the same (including in the 32bit vs 64bit case).
  1811. */
  1812. static int tcp_ao_copy_mkts_to_user(const struct sock *sk,
  1813. struct tcp_ao_info *ao_info,
  1814. sockptr_t optval, sockptr_t optlen)
  1815. {
  1816. struct tcp_ao_getsockopt opt_in, opt_out;
  1817. struct tcp_ao_key *key, *current_key;
  1818. bool do_address_matching = true;
  1819. union tcp_ao_addr *addr = NULL;
  1820. int err, l3index, user_len;
  1821. unsigned int max_keys; /* maximum number of keys to copy to user */
  1822. size_t out_offset = 0;
  1823. size_t bytes_to_write; /* number of bytes to write to user level */
  1824. u32 matched_keys; /* keys from ao_info matched so far */
  1825. int optlen_out;
  1826. __be16 port = 0;
  1827. if (copy_from_sockptr(&user_len, optlen, sizeof(int)))
  1828. return -EFAULT;
  1829. if (user_len <= 0)
  1830. return -EINVAL;
  1831. memset(&opt_in, 0, sizeof(struct tcp_ao_getsockopt));
  1832. err = copy_struct_from_sockptr(&opt_in, sizeof(opt_in),
  1833. optval, user_len);
  1834. if (err < 0)
  1835. return err;
  1836. if (opt_in.pkt_good || opt_in.pkt_bad)
  1837. return -EINVAL;
  1838. if (opt_in.keyflags & ~TCP_AO_GET_KEYF_VALID)
  1839. return -EINVAL;
  1840. if (opt_in.ifindex && !(opt_in.keyflags & TCP_AO_KEYF_IFINDEX))
  1841. return -EINVAL;
  1842. if (opt_in.reserved != 0)
  1843. return -EINVAL;
  1844. max_keys = opt_in.nkeys;
  1845. l3index = (opt_in.keyflags & TCP_AO_KEYF_IFINDEX) ? opt_in.ifindex : -1;
  1846. if (opt_in.get_all || opt_in.is_current || opt_in.is_rnext) {
  1847. if (opt_in.get_all && (opt_in.is_current || opt_in.is_rnext))
  1848. return -EINVAL;
  1849. do_address_matching = false;
  1850. }
  1851. switch (opt_in.addr.ss_family) {
  1852. case AF_INET: {
  1853. struct sockaddr_in *sin;
  1854. __be32 mask;
  1855. sin = (struct sockaddr_in *)&opt_in.addr;
  1856. port = sin->sin_port;
  1857. addr = (union tcp_ao_addr *)&sin->sin_addr;
  1858. if (opt_in.prefix > 32)
  1859. return -EINVAL;
  1860. if (ntohl(sin->sin_addr.s_addr) == INADDR_ANY &&
  1861. opt_in.prefix != 0)
  1862. return -EINVAL;
  1863. mask = inet_make_mask(opt_in.prefix);
  1864. if (sin->sin_addr.s_addr & ~mask)
  1865. return -EINVAL;
  1866. break;
  1867. }
  1868. case AF_INET6: {
  1869. struct sockaddr_in6 *sin6;
  1870. struct in6_addr *addr6;
  1871. sin6 = (struct sockaddr_in6 *)&opt_in.addr;
  1872. addr = (union tcp_ao_addr *)&sin6->sin6_addr;
  1873. addr6 = &sin6->sin6_addr;
  1874. port = sin6->sin6_port;
  1875. /* We don't have to change family and @addr here if
  1876. * ipv6_addr_v4mapped() like in key adding:
  1877. * tcp_ao_key_cmp() does it. Do the sanity checks though.
  1878. */
  1879. if (opt_in.prefix != 0) {
  1880. if (ipv6_addr_v4mapped(addr6)) {
  1881. __be32 mask, addr4 = addr6->s6_addr32[3];
  1882. if (opt_in.prefix > 32 ||
  1883. ntohl(addr4) == INADDR_ANY)
  1884. return -EINVAL;
  1885. mask = inet_make_mask(opt_in.prefix);
  1886. if (addr4 & ~mask)
  1887. return -EINVAL;
  1888. } else {
  1889. struct in6_addr pfx;
  1890. if (ipv6_addr_any(addr6) ||
  1891. opt_in.prefix > 128)
  1892. return -EINVAL;
  1893. ipv6_addr_prefix(&pfx, addr6, opt_in.prefix);
  1894. if (ipv6_addr_cmp(&pfx, addr6))
  1895. return -EINVAL;
  1896. }
  1897. } else if (!ipv6_addr_any(addr6)) {
  1898. return -EINVAL;
  1899. }
  1900. break;
  1901. }
  1902. case 0:
  1903. if (!do_address_matching)
  1904. break;
  1905. fallthrough;
  1906. default:
  1907. return -EAFNOSUPPORT;
  1908. }
  1909. if (!do_address_matching) {
  1910. /* We could just ignore those, but let's do stricter checks */
  1911. if (addr || port)
  1912. return -EINVAL;
  1913. if (opt_in.prefix || opt_in.sndid || opt_in.rcvid)
  1914. return -EINVAL;
  1915. }
  1916. bytes_to_write = min_t(int, user_len, sizeof(struct tcp_ao_getsockopt));
  1917. matched_keys = 0;
  1918. /* May change in RX, while we're dumping, pre-fetch it */
  1919. current_key = READ_ONCE(ao_info->current_key);
  1920. hlist_for_each_entry_rcu(key, &ao_info->head, node,
  1921. lockdep_sock_is_held(sk)) {
  1922. if (opt_in.get_all)
  1923. goto match;
  1924. if (opt_in.is_current || opt_in.is_rnext) {
  1925. if (opt_in.is_current && key == current_key)
  1926. goto match;
  1927. if (opt_in.is_rnext && key == ao_info->rnext_key)
  1928. goto match;
  1929. continue;
  1930. }
  1931. if (tcp_ao_key_cmp(key, l3index, addr, opt_in.prefix,
  1932. opt_in.addr.ss_family,
  1933. opt_in.sndid, opt_in.rcvid) != 0)
  1934. continue;
  1935. match:
  1936. matched_keys++;
  1937. if (matched_keys > max_keys)
  1938. continue;
  1939. memset(&opt_out, 0, sizeof(struct tcp_ao_getsockopt));
  1940. if (key->family == AF_INET) {
  1941. struct sockaddr_in *sin_out = (struct sockaddr_in *)&opt_out.addr;
  1942. sin_out->sin_family = key->family;
  1943. sin_out->sin_port = 0;
  1944. memcpy(&sin_out->sin_addr, &key->addr, sizeof(struct in_addr));
  1945. } else {
  1946. struct sockaddr_in6 *sin6_out = (struct sockaddr_in6 *)&opt_out.addr;
  1947. sin6_out->sin6_family = key->family;
  1948. sin6_out->sin6_port = 0;
  1949. memcpy(&sin6_out->sin6_addr, &key->addr, sizeof(struct in6_addr));
  1950. }
  1951. opt_out.sndid = key->sndid;
  1952. opt_out.rcvid = key->rcvid;
  1953. opt_out.prefix = key->prefixlen;
  1954. opt_out.keyflags = key->keyflags;
  1955. opt_out.is_current = (key == current_key);
  1956. opt_out.is_rnext = (key == ao_info->rnext_key);
  1957. opt_out.nkeys = 0;
  1958. opt_out.maclen = key->maclen;
  1959. opt_out.keylen = key->keylen;
  1960. opt_out.ifindex = key->l3index;
  1961. opt_out.pkt_good = atomic64_read(&key->pkt_good);
  1962. opt_out.pkt_bad = atomic64_read(&key->pkt_bad);
  1963. memcpy(&opt_out.key, key->key, key->keylen);
  1964. tcp_sigpool_algo(key->tcp_sigpool_id, opt_out.alg_name, 64);
  1965. /* Copy key to user */
  1966. if (copy_to_sockptr_offset(optval, out_offset,
  1967. &opt_out, bytes_to_write))
  1968. return -EFAULT;
  1969. out_offset += user_len;
  1970. }
  1971. optlen_out = (int)sizeof(struct tcp_ao_getsockopt);
  1972. if (copy_to_sockptr(optlen, &optlen_out, sizeof(int)))
  1973. return -EFAULT;
  1974. out_offset = offsetof(struct tcp_ao_getsockopt, nkeys);
  1975. if (copy_to_sockptr_offset(optval, out_offset,
  1976. &matched_keys, sizeof(u32)))
  1977. return -EFAULT;
  1978. return 0;
  1979. }
  1980. int tcp_ao_get_mkts(struct sock *sk, sockptr_t optval, sockptr_t optlen)
  1981. {
  1982. struct tcp_ao_info *ao_info;
  1983. ao_info = setsockopt_ao_info(sk);
  1984. if (IS_ERR(ao_info))
  1985. return PTR_ERR(ao_info);
  1986. if (!ao_info)
  1987. return -ENOENT;
  1988. return tcp_ao_copy_mkts_to_user(sk, ao_info, optval, optlen);
  1989. }
  1990. int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, sockptr_t optlen)
  1991. {
  1992. struct tcp_ao_info_opt out, in = {};
  1993. struct tcp_ao_key *current_key;
  1994. struct tcp_ao_info *ao;
  1995. int err, len;
  1996. if (copy_from_sockptr(&len, optlen, sizeof(int)))
  1997. return -EFAULT;
  1998. if (len <= 0)
  1999. return -EINVAL;
  2000. /* Copying this "in" only to check ::reserved, ::reserved2,
  2001. * that may be needed to extend (struct tcp_ao_info_opt) and
  2002. * what getsockopt() provides in future.
  2003. */
  2004. err = copy_struct_from_sockptr(&in, sizeof(in), optval, len);
  2005. if (err)
  2006. return err;
  2007. if (in.reserved != 0 || in.reserved2 != 0)
  2008. return -EINVAL;
  2009. ao = setsockopt_ao_info(sk);
  2010. if (IS_ERR(ao))
  2011. return PTR_ERR(ao);
  2012. if (!ao)
  2013. return -ENOENT;
  2014. memset(&out, 0, sizeof(out));
  2015. out.ao_required = ao->ao_required;
  2016. out.accept_icmps = ao->accept_icmps;
  2017. out.pkt_good = atomic64_read(&ao->counters.pkt_good);
  2018. out.pkt_bad = atomic64_read(&ao->counters.pkt_bad);
  2019. out.pkt_key_not_found = atomic64_read(&ao->counters.key_not_found);
  2020. out.pkt_ao_required = atomic64_read(&ao->counters.ao_required);
  2021. out.pkt_dropped_icmp = atomic64_read(&ao->counters.dropped_icmp);
  2022. current_key = READ_ONCE(ao->current_key);
  2023. if (current_key) {
  2024. out.set_current = 1;
  2025. out.current_key = current_key->sndid;
  2026. }
  2027. if (ao->rnext_key) {
  2028. out.set_rnext = 1;
  2029. out.rnext = ao->rnext_key->rcvid;
  2030. }
  2031. if (copy_to_sockptr(optval, &out, min_t(int, len, sizeof(out))))
  2032. return -EFAULT;
  2033. return 0;
  2034. }
  2035. int tcp_ao_set_repair(struct sock *sk, sockptr_t optval, unsigned int optlen)
  2036. {
  2037. struct tcp_sock *tp = tcp_sk(sk);
  2038. struct tcp_ao_repair cmd;
  2039. struct tcp_ao_key *key;
  2040. struct tcp_ao_info *ao;
  2041. int err;
  2042. if (optlen < sizeof(cmd))
  2043. return -EINVAL;
  2044. err = copy_struct_from_sockptr(&cmd, sizeof(cmd), optval, optlen);
  2045. if (err)
  2046. return err;
  2047. if (!tp->repair)
  2048. return -EPERM;
  2049. ao = setsockopt_ao_info(sk);
  2050. if (IS_ERR(ao))
  2051. return PTR_ERR(ao);
  2052. if (!ao)
  2053. return -ENOENT;
  2054. WRITE_ONCE(ao->lisn, cmd.snt_isn);
  2055. WRITE_ONCE(ao->risn, cmd.rcv_isn);
  2056. WRITE_ONCE(ao->snd_sne, cmd.snd_sne);
  2057. WRITE_ONCE(ao->rcv_sne, cmd.rcv_sne);
  2058. hlist_for_each_entry_rcu(key, &ao->head, node, lockdep_sock_is_held(sk))
  2059. tcp_ao_cache_traffic_keys(sk, ao, key);
  2060. return 0;
  2061. }
  2062. int tcp_ao_get_repair(struct sock *sk, sockptr_t optval, sockptr_t optlen)
  2063. {
  2064. struct tcp_sock *tp = tcp_sk(sk);
  2065. struct tcp_ao_repair opt;
  2066. struct tcp_ao_info *ao;
  2067. int len;
  2068. if (copy_from_sockptr(&len, optlen, sizeof(int)))
  2069. return -EFAULT;
  2070. if (len <= 0)
  2071. return -EINVAL;
  2072. if (!tp->repair)
  2073. return -EPERM;
  2074. rcu_read_lock();
  2075. ao = getsockopt_ao_info(sk);
  2076. if (IS_ERR_OR_NULL(ao)) {
  2077. rcu_read_unlock();
  2078. return ao ? PTR_ERR(ao) : -ENOENT;
  2079. }
  2080. opt.snt_isn = ao->lisn;
  2081. opt.rcv_isn = ao->risn;
  2082. opt.snd_sne = READ_ONCE(ao->snd_sne);
  2083. opt.rcv_sne = READ_ONCE(ao->rcv_sne);
  2084. rcu_read_unlock();
  2085. if (copy_to_sockptr(optval, &opt, min_t(int, len, sizeof(opt))))
  2086. return -EFAULT;
  2087. return 0;
  2088. }