fou.c 21 KB

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