fou.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. #include <linux/module.h>
  2. #include <linux/errno.h>
  3. #include <linux/socket.h>
  4. #include <linux/skbuff.h>
  5. #include <linux/ip.h>
  6. #include <linux/udp.h>
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <net/genetlink.h>
  10. #include <net/gue.h>
  11. #include <net/fou.h>
  12. #include <net/ip.h>
  13. #include <net/protocol.h>
  14. #include <net/udp.h>
  15. #include <net/udp_tunnel.h>
  16. #include <net/xfrm.h>
  17. #include <uapi/linux/fou.h>
  18. #include <uapi/linux/genetlink.h>
  19. struct fou {
  20. struct socket *sock;
  21. u8 protocol;
  22. u8 flags;
  23. __be16 port;
  24. u8 family;
  25. u16 type;
  26. struct list_head list;
  27. struct rcu_head rcu;
  28. };
  29. #define FOU_F_REMCSUM_NOPARTIAL BIT(0)
  30. struct fou_cfg {
  31. u16 type;
  32. u8 protocol;
  33. u8 flags;
  34. struct udp_port_cfg udp_config;
  35. };
  36. static unsigned int fou_net_id;
  37. struct fou_net {
  38. struct list_head fou_list;
  39. struct mutex fou_lock;
  40. };
  41. static inline struct fou *fou_from_sock(struct sock *sk)
  42. {
  43. return sk->sk_user_data;
  44. }
  45. static int fou_recv_pull(struct sk_buff *skb, struct fou *fou, size_t len)
  46. {
  47. /* Remove 'len' bytes from the packet (UDP header and
  48. * FOU header if present).
  49. */
  50. if (fou->family == AF_INET)
  51. ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
  52. else
  53. ipv6_hdr(skb)->payload_len =
  54. htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
  55. __skb_pull(skb, len);
  56. skb_postpull_rcsum(skb, udp_hdr(skb), len);
  57. skb_reset_transport_header(skb);
  58. return iptunnel_pull_offloads(skb);
  59. }
  60. static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
  61. {
  62. struct fou *fou = fou_from_sock(sk);
  63. if (!fou)
  64. return 1;
  65. if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
  66. goto drop;
  67. return -fou->protocol;
  68. drop:
  69. kfree_skb(skb);
  70. return 0;
  71. }
  72. static struct guehdr *gue_remcsum(struct sk_buff *skb, struct guehdr *guehdr,
  73. void *data, size_t hdrlen, u8 ipproto,
  74. bool nopartial)
  75. {
  76. __be16 *pd = data;
  77. size_t start = ntohs(pd[0]);
  78. size_t offset = ntohs(pd[1]);
  79. size_t plen = sizeof(struct udphdr) + hdrlen +
  80. max_t(size_t, offset + sizeof(u16), start);
  81. if (skb->remcsum_offload)
  82. return guehdr;
  83. if (!pskb_may_pull(skb, plen))
  84. return NULL;
  85. guehdr = (struct guehdr *)&udp_hdr(skb)[1];
  86. skb_remcsum_process(skb, (void *)guehdr + hdrlen,
  87. start, offset, nopartial);
  88. return guehdr;
  89. }
  90. static int gue_control_message(struct sk_buff *skb, struct guehdr *guehdr)
  91. {
  92. /* No support yet */
  93. kfree_skb(skb);
  94. return 0;
  95. }
  96. static int gue_udp_recv(struct sock *sk, struct sk_buff *skb)
  97. {
  98. struct fou *fou = fou_from_sock(sk);
  99. size_t len, optlen, hdrlen;
  100. struct guehdr *guehdr;
  101. void *data;
  102. u16 doffset = 0;
  103. u8 proto_ctype;
  104. if (!fou)
  105. return 1;
  106. len = sizeof(struct udphdr) + sizeof(struct guehdr);
  107. if (!pskb_may_pull(skb, len))
  108. goto drop;
  109. guehdr = (struct guehdr *)&udp_hdr(skb)[1];
  110. switch (guehdr->version) {
  111. case 0: /* Full GUE header present */
  112. break;
  113. case 1: {
  114. /* Direct encasulation of IPv4 or IPv6 */
  115. int prot;
  116. switch (((struct iphdr *)guehdr)->version) {
  117. case 4:
  118. prot = IPPROTO_IPIP;
  119. break;
  120. case 6:
  121. prot = IPPROTO_IPV6;
  122. break;
  123. default:
  124. goto drop;
  125. }
  126. if (fou_recv_pull(skb, fou, sizeof(struct udphdr)))
  127. goto drop;
  128. return -prot;
  129. }
  130. default: /* Undefined version */
  131. goto drop;
  132. }
  133. optlen = guehdr->hlen << 2;
  134. len += optlen;
  135. if (!pskb_may_pull(skb, len))
  136. goto drop;
  137. /* guehdr may change after pull */
  138. guehdr = (struct guehdr *)&udp_hdr(skb)[1];
  139. hdrlen = sizeof(struct guehdr) + optlen;
  140. if (guehdr->version != 0 || validate_gue_flags(guehdr, optlen))
  141. goto drop;
  142. hdrlen = sizeof(struct guehdr) + optlen;
  143. if (fou->family == AF_INET)
  144. ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(skb)->tot_len) - len);
  145. else
  146. ipv6_hdr(skb)->payload_len =
  147. htons(ntohs(ipv6_hdr(skb)->payload_len) - len);
  148. /* Pull csum through the guehdr now . This can be used if
  149. * there is a remote checksum offload.
  150. */
  151. skb_postpull_rcsum(skb, udp_hdr(skb), len);
  152. data = &guehdr[1];
  153. if (guehdr->flags & GUE_FLAG_PRIV) {
  154. __be32 flags = *(__be32 *)(data + doffset);
  155. doffset += GUE_LEN_PRIV;
  156. if (flags & GUE_PFLAG_REMCSUM) {
  157. guehdr = gue_remcsum(skb, guehdr, data + doffset,
  158. hdrlen, guehdr->proto_ctype,
  159. !!(fou->flags &
  160. FOU_F_REMCSUM_NOPARTIAL));
  161. if (!guehdr)
  162. goto drop;
  163. data = &guehdr[1];
  164. doffset += GUE_PLEN_REMCSUM;
  165. }
  166. }
  167. if (unlikely(guehdr->control))
  168. return gue_control_message(skb, guehdr);
  169. proto_ctype = guehdr->proto_ctype;
  170. __skb_pull(skb, sizeof(struct udphdr) + hdrlen);
  171. skb_reset_transport_header(skb);
  172. if (iptunnel_pull_offloads(skb))
  173. goto drop;
  174. return -proto_ctype;
  175. drop:
  176. kfree_skb(skb);
  177. return 0;
  178. }
  179. static struct sk_buff *fou_gro_receive(struct sock *sk,
  180. struct list_head *head,
  181. struct sk_buff *skb)
  182. {
  183. u8 proto = fou_from_sock(sk)->protocol;
  184. const struct net_offload **offloads;
  185. const struct net_offload *ops;
  186. struct sk_buff *pp = NULL;
  187. /* We can clear the encap_mark for FOU as we are essentially doing
  188. * one of two possible things. We are either adding an L4 tunnel
  189. * header to the outer L3 tunnel header, or we are are simply
  190. * treating the GRE tunnel header as though it is a UDP protocol
  191. * specific header such as VXLAN or GENEVE.
  192. */
  193. NAPI_GRO_CB(skb)->encap_mark = 0;
  194. /* Flag this frame as already having an outer encap header */
  195. NAPI_GRO_CB(skb)->is_fou = 1;
  196. rcu_read_lock();
  197. offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
  198. ops = rcu_dereference(offloads[proto]);
  199. if (!ops || !ops->callbacks.gro_receive)
  200. goto out_unlock;
  201. pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
  202. out_unlock:
  203. rcu_read_unlock();
  204. return pp;
  205. }
  206. static int fou_gro_complete(struct sock *sk, struct sk_buff *skb,
  207. int nhoff)
  208. {
  209. const struct net_offload *ops;
  210. u8 proto = fou_from_sock(sk)->protocol;
  211. int err = -ENOSYS;
  212. const struct net_offload **offloads;
  213. rcu_read_lock();
  214. offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
  215. ops = rcu_dereference(offloads[proto]);
  216. if (WARN_ON(!ops || !ops->callbacks.gro_complete))
  217. goto out_unlock;
  218. err = ops->callbacks.gro_complete(skb, nhoff);
  219. skb_set_inner_mac_header(skb, nhoff);
  220. out_unlock:
  221. rcu_read_unlock();
  222. return err;
  223. }
  224. static struct guehdr *gue_gro_remcsum(struct sk_buff *skb, unsigned int off,
  225. struct guehdr *guehdr, void *data,
  226. size_t hdrlen, struct gro_remcsum *grc,
  227. bool nopartial)
  228. {
  229. __be16 *pd = data;
  230. size_t start = ntohs(pd[0]);
  231. size_t offset = ntohs(pd[1]);
  232. if (skb->remcsum_offload)
  233. return guehdr;
  234. if (!NAPI_GRO_CB(skb)->csum_valid)
  235. return NULL;
  236. guehdr = skb_gro_remcsum_process(skb, (void *)guehdr, off, hdrlen,
  237. start, offset, grc, nopartial);
  238. skb->remcsum_offload = 1;
  239. return guehdr;
  240. }
  241. static struct sk_buff *gue_gro_receive(struct sock *sk,
  242. struct list_head *head,
  243. struct sk_buff *skb)
  244. {
  245. const struct net_offload **offloads;
  246. const struct net_offload *ops;
  247. struct sk_buff *pp = NULL;
  248. struct sk_buff *p;
  249. struct guehdr *guehdr;
  250. size_t len, optlen, hdrlen, off;
  251. void *data;
  252. u16 doffset = 0;
  253. int flush = 1;
  254. struct fou *fou = fou_from_sock(sk);
  255. struct gro_remcsum grc;
  256. u8 proto;
  257. skb_gro_remcsum_init(&grc);
  258. off = skb_gro_offset(skb);
  259. len = off + sizeof(*guehdr);
  260. guehdr = skb_gro_header_fast(skb, off);
  261. if (skb_gro_header_hard(skb, len)) {
  262. guehdr = skb_gro_header_slow(skb, len, off);
  263. if (unlikely(!guehdr))
  264. goto out;
  265. }
  266. switch (guehdr->version) {
  267. case 0:
  268. break;
  269. case 1:
  270. switch (((struct iphdr *)guehdr)->version) {
  271. case 4:
  272. proto = IPPROTO_IPIP;
  273. break;
  274. case 6:
  275. proto = IPPROTO_IPV6;
  276. break;
  277. default:
  278. goto out;
  279. }
  280. goto next_proto;
  281. default:
  282. goto out;
  283. }
  284. optlen = guehdr->hlen << 2;
  285. len += optlen;
  286. if (skb_gro_header_hard(skb, len)) {
  287. guehdr = skb_gro_header_slow(skb, len, off);
  288. if (unlikely(!guehdr))
  289. goto out;
  290. }
  291. if (unlikely(guehdr->control) || guehdr->version != 0 ||
  292. validate_gue_flags(guehdr, optlen))
  293. goto out;
  294. hdrlen = sizeof(*guehdr) + optlen;
  295. /* Adjust NAPI_GRO_CB(skb)->csum to account for guehdr,
  296. * this is needed if there is a remote checkcsum offload.
  297. */
  298. skb_gro_postpull_rcsum(skb, guehdr, hdrlen);
  299. data = &guehdr[1];
  300. if (guehdr->flags & GUE_FLAG_PRIV) {
  301. __be32 flags = *(__be32 *)(data + doffset);
  302. doffset += GUE_LEN_PRIV;
  303. if (flags & GUE_PFLAG_REMCSUM) {
  304. guehdr = gue_gro_remcsum(skb, off, guehdr,
  305. data + doffset, hdrlen, &grc,
  306. !!(fou->flags &
  307. FOU_F_REMCSUM_NOPARTIAL));
  308. if (!guehdr)
  309. goto out;
  310. data = &guehdr[1];
  311. doffset += GUE_PLEN_REMCSUM;
  312. }
  313. }
  314. skb_gro_pull(skb, hdrlen);
  315. list_for_each_entry(p, head, list) {
  316. const struct guehdr *guehdr2;
  317. if (!NAPI_GRO_CB(p)->same_flow)
  318. continue;
  319. guehdr2 = (struct guehdr *)(p->data + off);
  320. /* Compare base GUE header to be equal (covers
  321. * hlen, version, proto_ctype, and flags.
  322. */
  323. if (guehdr->word != guehdr2->word) {
  324. NAPI_GRO_CB(p)->same_flow = 0;
  325. continue;
  326. }
  327. /* Compare optional fields are the same. */
  328. if (guehdr->hlen && memcmp(&guehdr[1], &guehdr2[1],
  329. guehdr->hlen << 2)) {
  330. NAPI_GRO_CB(p)->same_flow = 0;
  331. continue;
  332. }
  333. }
  334. proto = guehdr->proto_ctype;
  335. next_proto:
  336. /* We can clear the encap_mark for GUE as we are essentially doing
  337. * one of two possible things. We are either adding an L4 tunnel
  338. * header to the outer L3 tunnel header, or we are are simply
  339. * treating the GRE tunnel header as though it is a UDP protocol
  340. * specific header such as VXLAN or GENEVE.
  341. */
  342. NAPI_GRO_CB(skb)->encap_mark = 0;
  343. /* Flag this frame as already having an outer encap header */
  344. NAPI_GRO_CB(skb)->is_fou = 1;
  345. rcu_read_lock();
  346. offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
  347. ops = rcu_dereference(offloads[proto]);
  348. if (WARN_ON_ONCE(!ops || !ops->callbacks.gro_receive))
  349. goto out_unlock;
  350. pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
  351. flush = 0;
  352. out_unlock:
  353. rcu_read_unlock();
  354. out:
  355. skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
  356. return pp;
  357. }
  358. static int gue_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
  359. {
  360. const struct net_offload **offloads;
  361. struct guehdr *guehdr = (struct guehdr *)(skb->data + nhoff);
  362. const struct net_offload *ops;
  363. unsigned int guehlen = 0;
  364. u8 proto;
  365. int err = -ENOENT;
  366. switch (guehdr->version) {
  367. case 0:
  368. proto = guehdr->proto_ctype;
  369. guehlen = sizeof(*guehdr) + (guehdr->hlen << 2);
  370. break;
  371. case 1:
  372. switch (((struct iphdr *)guehdr)->version) {
  373. case 4:
  374. proto = IPPROTO_IPIP;
  375. break;
  376. case 6:
  377. proto = IPPROTO_IPV6;
  378. break;
  379. default:
  380. return err;
  381. }
  382. break;
  383. default:
  384. return err;
  385. }
  386. rcu_read_lock();
  387. offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
  388. ops = rcu_dereference(offloads[proto]);
  389. if (WARN_ON(!ops || !ops->callbacks.gro_complete))
  390. goto out_unlock;
  391. err = ops->callbacks.gro_complete(skb, nhoff + guehlen);
  392. skb_set_inner_mac_header(skb, nhoff + guehlen);
  393. out_unlock:
  394. rcu_read_unlock();
  395. return err;
  396. }
  397. static int fou_add_to_port_list(struct net *net, struct fou *fou)
  398. {
  399. struct fou_net *fn = net_generic(net, fou_net_id);
  400. struct fou *fout;
  401. mutex_lock(&fn->fou_lock);
  402. list_for_each_entry(fout, &fn->fou_list, list) {
  403. if (fou->port == fout->port &&
  404. fou->family == fout->family) {
  405. mutex_unlock(&fn->fou_lock);
  406. return -EALREADY;
  407. }
  408. }
  409. list_add(&fou->list, &fn->fou_list);
  410. mutex_unlock(&fn->fou_lock);
  411. return 0;
  412. }
  413. static void fou_release(struct fou *fou)
  414. {
  415. struct socket *sock = fou->sock;
  416. list_del(&fou->list);
  417. udp_tunnel_sock_release(sock);
  418. kfree_rcu(fou, rcu);
  419. }
  420. static int fou_create(struct net *net, struct fou_cfg *cfg,
  421. struct socket **sockp)
  422. {
  423. struct socket *sock = NULL;
  424. struct fou *fou = NULL;
  425. struct sock *sk;
  426. struct udp_tunnel_sock_cfg tunnel_cfg;
  427. int err;
  428. /* Open UDP socket */
  429. err = udp_sock_create(net, &cfg->udp_config, &sock);
  430. if (err < 0)
  431. goto error;
  432. /* Allocate FOU port structure */
  433. fou = kzalloc(sizeof(*fou), GFP_KERNEL);
  434. if (!fou) {
  435. err = -ENOMEM;
  436. goto error;
  437. }
  438. sk = sock->sk;
  439. fou->port = cfg->udp_config.local_udp_port;
  440. fou->family = cfg->udp_config.family;
  441. fou->flags = cfg->flags;
  442. fou->type = cfg->type;
  443. fou->sock = sock;
  444. memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
  445. tunnel_cfg.encap_type = 1;
  446. tunnel_cfg.sk_user_data = fou;
  447. tunnel_cfg.encap_destroy = NULL;
  448. /* Initial for fou type */
  449. switch (cfg->type) {
  450. case FOU_ENCAP_DIRECT:
  451. tunnel_cfg.encap_rcv = fou_udp_recv;
  452. tunnel_cfg.gro_receive = fou_gro_receive;
  453. tunnel_cfg.gro_complete = fou_gro_complete;
  454. fou->protocol = cfg->protocol;
  455. break;
  456. case FOU_ENCAP_GUE:
  457. tunnel_cfg.encap_rcv = gue_udp_recv;
  458. tunnel_cfg.gro_receive = gue_gro_receive;
  459. tunnel_cfg.gro_complete = gue_gro_complete;
  460. break;
  461. default:
  462. err = -EINVAL;
  463. goto error;
  464. }
  465. setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
  466. sk->sk_allocation = GFP_ATOMIC;
  467. err = fou_add_to_port_list(net, fou);
  468. if (err)
  469. goto error;
  470. if (sockp)
  471. *sockp = sock;
  472. return 0;
  473. error:
  474. kfree(fou);
  475. if (sock)
  476. udp_tunnel_sock_release(sock);
  477. return err;
  478. }
  479. static int fou_destroy(struct net *net, struct fou_cfg *cfg)
  480. {
  481. struct fou_net *fn = net_generic(net, fou_net_id);
  482. __be16 port = cfg->udp_config.local_udp_port;
  483. u8 family = cfg->udp_config.family;
  484. int err = -EINVAL;
  485. struct fou *fou;
  486. mutex_lock(&fn->fou_lock);
  487. list_for_each_entry(fou, &fn->fou_list, list) {
  488. if (fou->port == port && fou->family == family) {
  489. fou_release(fou);
  490. err = 0;
  491. break;
  492. }
  493. }
  494. mutex_unlock(&fn->fou_lock);
  495. return err;
  496. }
  497. static struct genl_family fou_nl_family;
  498. static const struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
  499. [FOU_ATTR_PORT] = { .type = NLA_U16, },
  500. [FOU_ATTR_AF] = { .type = NLA_U8, },
  501. [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
  502. [FOU_ATTR_TYPE] = { .type = NLA_U8, },
  503. [FOU_ATTR_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG, },
  504. };
  505. static int parse_nl_config(struct genl_info *info,
  506. struct fou_cfg *cfg)
  507. {
  508. memset(cfg, 0, sizeof(*cfg));
  509. cfg->udp_config.family = AF_INET;
  510. if (info->attrs[FOU_ATTR_AF]) {
  511. u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
  512. switch (family) {
  513. case AF_INET:
  514. break;
  515. case AF_INET6:
  516. cfg->udp_config.ipv6_v6only = 1;
  517. break;
  518. default:
  519. return -EAFNOSUPPORT;
  520. }
  521. cfg->udp_config.family = family;
  522. }
  523. if (info->attrs[FOU_ATTR_PORT]) {
  524. __be16 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
  525. cfg->udp_config.local_udp_port = port;
  526. }
  527. if (info->attrs[FOU_ATTR_IPPROTO])
  528. cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
  529. if (info->attrs[FOU_ATTR_TYPE])
  530. cfg->type = nla_get_u8(info->attrs[FOU_ATTR_TYPE]);
  531. if (info->attrs[FOU_ATTR_REMCSUM_NOPARTIAL])
  532. cfg->flags |= FOU_F_REMCSUM_NOPARTIAL;
  533. return 0;
  534. }
  535. static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
  536. {
  537. struct net *net = genl_info_net(info);
  538. struct fou_cfg cfg;
  539. int err;
  540. err = parse_nl_config(info, &cfg);
  541. if (err)
  542. return err;
  543. return fou_create(net, &cfg, NULL);
  544. }
  545. static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
  546. {
  547. struct net *net = genl_info_net(info);
  548. struct fou_cfg cfg;
  549. int err;
  550. err = parse_nl_config(info, &cfg);
  551. if (err)
  552. return err;
  553. return fou_destroy(net, &cfg);
  554. }
  555. static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
  556. {
  557. if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
  558. nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
  559. nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
  560. nla_put_u8(msg, FOU_ATTR_TYPE, fou->type))
  561. return -1;
  562. if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
  563. if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
  564. return -1;
  565. return 0;
  566. }
  567. static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
  568. u32 flags, struct sk_buff *skb, u8 cmd)
  569. {
  570. void *hdr;
  571. hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
  572. if (!hdr)
  573. return -ENOMEM;
  574. if (fou_fill_info(fou, skb) < 0)
  575. goto nla_put_failure;
  576. genlmsg_end(skb, hdr);
  577. return 0;
  578. nla_put_failure:
  579. genlmsg_cancel(skb, hdr);
  580. return -EMSGSIZE;
  581. }
  582. static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
  583. {
  584. struct net *net = genl_info_net(info);
  585. struct fou_net *fn = net_generic(net, fou_net_id);
  586. struct sk_buff *msg;
  587. struct fou_cfg cfg;
  588. struct fou *fout;
  589. __be16 port;
  590. u8 family;
  591. int ret;
  592. ret = parse_nl_config(info, &cfg);
  593. if (ret)
  594. return ret;
  595. port = cfg.udp_config.local_udp_port;
  596. if (port == 0)
  597. return -EINVAL;
  598. family = cfg.udp_config.family;
  599. if (family != AF_INET && family != AF_INET6)
  600. return -EINVAL;
  601. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  602. if (!msg)
  603. return -ENOMEM;
  604. ret = -ESRCH;
  605. mutex_lock(&fn->fou_lock);
  606. list_for_each_entry(fout, &fn->fou_list, list) {
  607. if (port == fout->port && family == fout->family) {
  608. ret = fou_dump_info(fout, info->snd_portid,
  609. info->snd_seq, 0, msg,
  610. info->genlhdr->cmd);
  611. break;
  612. }
  613. }
  614. mutex_unlock(&fn->fou_lock);
  615. if (ret < 0)
  616. goto out_free;
  617. return genlmsg_reply(msg, info);
  618. out_free:
  619. nlmsg_free(msg);
  620. return ret;
  621. }
  622. static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
  623. {
  624. struct net *net = sock_net(skb->sk);
  625. struct fou_net *fn = net_generic(net, fou_net_id);
  626. struct fou *fout;
  627. int idx = 0, ret;
  628. mutex_lock(&fn->fou_lock);
  629. list_for_each_entry(fout, &fn->fou_list, list) {
  630. if (idx++ < cb->args[0])
  631. continue;
  632. ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
  633. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  634. skb, FOU_CMD_GET);
  635. if (ret)
  636. break;
  637. }
  638. mutex_unlock(&fn->fou_lock);
  639. cb->args[0] = idx;
  640. return skb->len;
  641. }
  642. static const struct genl_ops fou_nl_ops[] = {
  643. {
  644. .cmd = FOU_CMD_ADD,
  645. .doit = fou_nl_cmd_add_port,
  646. .policy = fou_nl_policy,
  647. .flags = GENL_ADMIN_PERM,
  648. },
  649. {
  650. .cmd = FOU_CMD_DEL,
  651. .doit = fou_nl_cmd_rm_port,
  652. .policy = fou_nl_policy,
  653. .flags = GENL_ADMIN_PERM,
  654. },
  655. {
  656. .cmd = FOU_CMD_GET,
  657. .doit = fou_nl_cmd_get_port,
  658. .dumpit = fou_nl_dump,
  659. .policy = fou_nl_policy,
  660. },
  661. };
  662. static struct genl_family fou_nl_family __ro_after_init = {
  663. .hdrsize = 0,
  664. .name = FOU_GENL_NAME,
  665. .version = FOU_GENL_VERSION,
  666. .maxattr = FOU_ATTR_MAX,
  667. .netnsok = true,
  668. .module = THIS_MODULE,
  669. .ops = fou_nl_ops,
  670. .n_ops = ARRAY_SIZE(fou_nl_ops),
  671. };
  672. size_t fou_encap_hlen(struct ip_tunnel_encap *e)
  673. {
  674. return sizeof(struct udphdr);
  675. }
  676. EXPORT_SYMBOL(fou_encap_hlen);
  677. size_t gue_encap_hlen(struct ip_tunnel_encap *e)
  678. {
  679. size_t len;
  680. bool need_priv = false;
  681. len = sizeof(struct udphdr) + sizeof(struct guehdr);
  682. if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
  683. len += GUE_PLEN_REMCSUM;
  684. need_priv = true;
  685. }
  686. len += need_priv ? GUE_LEN_PRIV : 0;
  687. return len;
  688. }
  689. EXPORT_SYMBOL(gue_encap_hlen);
  690. int __fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
  691. u8 *protocol, __be16 *sport, int type)
  692. {
  693. int err;
  694. err = iptunnel_handle_offloads(skb, type);
  695. if (err)
  696. return err;
  697. *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
  698. skb, 0, 0, false);
  699. return 0;
  700. }
  701. EXPORT_SYMBOL(__fou_build_header);
  702. int __gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
  703. u8 *protocol, __be16 *sport, int type)
  704. {
  705. struct guehdr *guehdr;
  706. size_t hdrlen, optlen = 0;
  707. void *data;
  708. bool need_priv = false;
  709. int err;
  710. if ((e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) &&
  711. skb->ip_summed == CHECKSUM_PARTIAL) {
  712. optlen += GUE_PLEN_REMCSUM;
  713. type |= SKB_GSO_TUNNEL_REMCSUM;
  714. need_priv = true;
  715. }
  716. optlen += need_priv ? GUE_LEN_PRIV : 0;
  717. err = iptunnel_handle_offloads(skb, type);
  718. if (err)
  719. return err;
  720. /* Get source port (based on flow hash) before skb_push */
  721. *sport = e->sport ? : udp_flow_src_port(dev_net(skb->dev),
  722. skb, 0, 0, false);
  723. hdrlen = sizeof(struct guehdr) + optlen;
  724. skb_push(skb, hdrlen);
  725. guehdr = (struct guehdr *)skb->data;
  726. guehdr->control = 0;
  727. guehdr->version = 0;
  728. guehdr->hlen = optlen >> 2;
  729. guehdr->flags = 0;
  730. guehdr->proto_ctype = *protocol;
  731. data = &guehdr[1];
  732. if (need_priv) {
  733. __be32 *flags = data;
  734. guehdr->flags |= GUE_FLAG_PRIV;
  735. *flags = 0;
  736. data += GUE_LEN_PRIV;
  737. if (type & SKB_GSO_TUNNEL_REMCSUM) {
  738. u16 csum_start = skb_checksum_start_offset(skb);
  739. __be16 *pd = data;
  740. if (csum_start < hdrlen)
  741. return -EINVAL;
  742. csum_start -= hdrlen;
  743. pd[0] = htons(csum_start);
  744. pd[1] = htons(csum_start + skb->csum_offset);
  745. if (!skb_is_gso(skb)) {
  746. skb->ip_summed = CHECKSUM_NONE;
  747. skb->encapsulation = 0;
  748. }
  749. *flags |= GUE_PFLAG_REMCSUM;
  750. data += GUE_PLEN_REMCSUM;
  751. }
  752. }
  753. return 0;
  754. }
  755. EXPORT_SYMBOL(__gue_build_header);
  756. #ifdef CONFIG_NET_FOU_IP_TUNNELS
  757. static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
  758. struct flowi4 *fl4, u8 *protocol, __be16 sport)
  759. {
  760. struct udphdr *uh;
  761. skb_push(skb, sizeof(struct udphdr));
  762. skb_reset_transport_header(skb);
  763. uh = udp_hdr(skb);
  764. uh->dest = e->dport;
  765. uh->source = sport;
  766. uh->len = htons(skb->len);
  767. udp_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM), skb,
  768. fl4->saddr, fl4->daddr, skb->len);
  769. *protocol = IPPROTO_UDP;
  770. }
  771. static int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
  772. u8 *protocol, struct flowi4 *fl4)
  773. {
  774. int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
  775. SKB_GSO_UDP_TUNNEL;
  776. __be16 sport;
  777. int err;
  778. err = __fou_build_header(skb, e, protocol, &sport, type);
  779. if (err)
  780. return err;
  781. fou_build_udp(skb, e, fl4, protocol, sport);
  782. return 0;
  783. }
  784. static int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
  785. u8 *protocol, struct flowi4 *fl4)
  786. {
  787. int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
  788. SKB_GSO_UDP_TUNNEL;
  789. __be16 sport;
  790. int err;
  791. err = __gue_build_header(skb, e, protocol, &sport, type);
  792. if (err)
  793. return err;
  794. fou_build_udp(skb, e, fl4, protocol, sport);
  795. return 0;
  796. }
  797. static const struct ip_tunnel_encap_ops fou_iptun_ops = {
  798. .encap_hlen = fou_encap_hlen,
  799. .build_header = fou_build_header,
  800. };
  801. static const struct ip_tunnel_encap_ops gue_iptun_ops = {
  802. .encap_hlen = gue_encap_hlen,
  803. .build_header = gue_build_header,
  804. };
  805. static int ip_tunnel_encap_add_fou_ops(void)
  806. {
  807. int ret;
  808. ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
  809. if (ret < 0) {
  810. pr_err("can't add fou ops\n");
  811. return ret;
  812. }
  813. ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
  814. if (ret < 0) {
  815. pr_err("can't add gue ops\n");
  816. ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
  817. return ret;
  818. }
  819. return 0;
  820. }
  821. static void ip_tunnel_encap_del_fou_ops(void)
  822. {
  823. ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
  824. ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
  825. }
  826. #else
  827. static int ip_tunnel_encap_add_fou_ops(void)
  828. {
  829. return 0;
  830. }
  831. static void ip_tunnel_encap_del_fou_ops(void)
  832. {
  833. }
  834. #endif
  835. static __net_init int fou_init_net(struct net *net)
  836. {
  837. struct fou_net *fn = net_generic(net, fou_net_id);
  838. INIT_LIST_HEAD(&fn->fou_list);
  839. mutex_init(&fn->fou_lock);
  840. return 0;
  841. }
  842. static __net_exit void fou_exit_net(struct net *net)
  843. {
  844. struct fou_net *fn = net_generic(net, fou_net_id);
  845. struct fou *fou, *next;
  846. /* Close all the FOU sockets */
  847. mutex_lock(&fn->fou_lock);
  848. list_for_each_entry_safe(fou, next, &fn->fou_list, list)
  849. fou_release(fou);
  850. mutex_unlock(&fn->fou_lock);
  851. }
  852. static struct pernet_operations fou_net_ops = {
  853. .init = fou_init_net,
  854. .exit = fou_exit_net,
  855. .id = &fou_net_id,
  856. .size = sizeof(struct fou_net),
  857. };
  858. static int __init fou_init(void)
  859. {
  860. int ret;
  861. ret = register_pernet_device(&fou_net_ops);
  862. if (ret)
  863. goto exit;
  864. ret = genl_register_family(&fou_nl_family);
  865. if (ret < 0)
  866. goto unregister;
  867. ret = ip_tunnel_encap_add_fou_ops();
  868. if (ret == 0)
  869. return 0;
  870. genl_unregister_family(&fou_nl_family);
  871. unregister:
  872. unregister_pernet_device(&fou_net_ops);
  873. exit:
  874. return ret;
  875. }
  876. static void __exit fou_fini(void)
  877. {
  878. ip_tunnel_encap_del_fou_ops();
  879. genl_unregister_family(&fou_nl_family);
  880. unregister_pernet_device(&fou_net_ops);
  881. }
  882. module_init(fou_init);
  883. module_exit(fou_fini);
  884. MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
  885. MODULE_LICENSE("GPL");