fou.c 23 KB

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