fou.c 23 KB

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