af_mpls.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. #include <linux/types.h>
  2. #include <linux/skbuff.h>
  3. #include <linux/socket.h>
  4. #include <linux/sysctl.h>
  5. #include <linux/net.h>
  6. #include <linux/module.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/ipv6.h>
  9. #include <linux/mpls.h>
  10. #include <linux/vmalloc.h>
  11. #include <net/ip.h>
  12. #include <net/dst.h>
  13. #include <net/sock.h>
  14. #include <net/arp.h>
  15. #include <net/ip_fib.h>
  16. #include <net/netevent.h>
  17. #include <net/netns/generic.h>
  18. #if IS_ENABLED(CONFIG_IPV6)
  19. #include <net/ipv6.h>
  20. #include <net/addrconf.h>
  21. #endif
  22. #include "internal.h"
  23. #define LABEL_NOT_SPECIFIED (1<<20)
  24. #define MAX_NEW_LABELS 2
  25. /* This maximum ha length copied from the definition of struct neighbour */
  26. #define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, sizeof(unsigned long)))
  27. enum mpls_payload_type {
  28. MPT_UNSPEC, /* IPv4 or IPv6 */
  29. MPT_IPV4 = 4,
  30. MPT_IPV6 = 6,
  31. /* Other types not implemented:
  32. * - Pseudo-wire with or without control word (RFC4385)
  33. * - GAL (RFC5586)
  34. */
  35. };
  36. struct mpls_route { /* next hop label forwarding entry */
  37. struct net_device __rcu *rt_dev;
  38. struct rcu_head rt_rcu;
  39. u32 rt_label[MAX_NEW_LABELS];
  40. u8 rt_protocol; /* routing protocol that set this entry */
  41. u8 rt_payload_type;
  42. u8 rt_labels;
  43. u8 rt_via_alen;
  44. u8 rt_via_table;
  45. u8 rt_via[0];
  46. };
  47. static int zero = 0;
  48. static int label_limit = (1 << 20) - 1;
  49. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  50. struct nlmsghdr *nlh, struct net *net, u32 portid,
  51. unsigned int nlm_flags);
  52. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  53. {
  54. struct mpls_route *rt = NULL;
  55. if (index < net->mpls.platform_labels) {
  56. struct mpls_route __rcu **platform_label =
  57. rcu_dereference(net->mpls.platform_label);
  58. rt = rcu_dereference(platform_label[index]);
  59. }
  60. return rt;
  61. }
  62. static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
  63. {
  64. return rcu_dereference_rtnl(dev->mpls_ptr);
  65. }
  66. bool mpls_output_possible(const struct net_device *dev)
  67. {
  68. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  69. }
  70. EXPORT_SYMBOL_GPL(mpls_output_possible);
  71. static unsigned int mpls_rt_header_size(const struct mpls_route *rt)
  72. {
  73. /* The size of the layer 2.5 labels to be added for this route */
  74. return rt->rt_labels * sizeof(struct mpls_shim_hdr);
  75. }
  76. unsigned int mpls_dev_mtu(const struct net_device *dev)
  77. {
  78. /* The amount of data the layer 2 frame can hold */
  79. return dev->mtu;
  80. }
  81. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  82. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  83. {
  84. if (skb->len <= mtu)
  85. return false;
  86. if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
  87. return false;
  88. return true;
  89. }
  90. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  91. static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
  92. struct mpls_entry_decoded dec)
  93. {
  94. enum mpls_payload_type payload_type;
  95. bool success = false;
  96. /* The IPv4 code below accesses through the IPv4 header
  97. * checksum, which is 12 bytes into the packet.
  98. * The IPv6 code below accesses through the IPv6 hop limit
  99. * which is 8 bytes into the packet.
  100. *
  101. * For all supported cases there should always be at least 12
  102. * bytes of packet data present. The IPv4 header is 20 bytes
  103. * without options and the IPv6 header is always 40 bytes
  104. * long.
  105. */
  106. if (!pskb_may_pull(skb, 12))
  107. return false;
  108. payload_type = rt->rt_payload_type;
  109. if (payload_type == MPT_UNSPEC)
  110. payload_type = ip_hdr(skb)->version;
  111. switch (payload_type) {
  112. case MPT_IPV4: {
  113. struct iphdr *hdr4 = ip_hdr(skb);
  114. skb->protocol = htons(ETH_P_IP);
  115. csum_replace2(&hdr4->check,
  116. htons(hdr4->ttl << 8),
  117. htons(dec.ttl << 8));
  118. hdr4->ttl = dec.ttl;
  119. success = true;
  120. break;
  121. }
  122. case MPT_IPV6: {
  123. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  124. skb->protocol = htons(ETH_P_IPV6);
  125. hdr6->hop_limit = dec.ttl;
  126. success = true;
  127. break;
  128. }
  129. case MPT_UNSPEC:
  130. break;
  131. }
  132. return success;
  133. }
  134. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  135. struct packet_type *pt, struct net_device *orig_dev)
  136. {
  137. struct net *net = dev_net(dev);
  138. struct mpls_shim_hdr *hdr;
  139. struct mpls_route *rt;
  140. struct mpls_entry_decoded dec;
  141. struct net_device *out_dev;
  142. struct mpls_dev *mdev;
  143. unsigned int hh_len;
  144. unsigned int new_header_size;
  145. unsigned int mtu;
  146. int err;
  147. /* Careful this entire function runs inside of an rcu critical section */
  148. mdev = mpls_dev_get(dev);
  149. if (!mdev || !mdev->input_enabled)
  150. goto drop;
  151. if (skb->pkt_type != PACKET_HOST)
  152. goto drop;
  153. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  154. goto drop;
  155. if (!pskb_may_pull(skb, sizeof(*hdr)))
  156. goto drop;
  157. /* Read and decode the label */
  158. hdr = mpls_hdr(skb);
  159. dec = mpls_entry_decode(hdr);
  160. /* Pop the label */
  161. skb_pull(skb, sizeof(*hdr));
  162. skb_reset_network_header(skb);
  163. skb_orphan(skb);
  164. rt = mpls_route_input_rcu(net, dec.label);
  165. if (!rt)
  166. goto drop;
  167. /* Find the output device */
  168. out_dev = rcu_dereference(rt->rt_dev);
  169. if (!mpls_output_possible(out_dev))
  170. goto drop;
  171. if (skb_warn_if_lro(skb))
  172. goto drop;
  173. skb_forward_csum(skb);
  174. /* Verify ttl is valid */
  175. if (dec.ttl <= 1)
  176. goto drop;
  177. dec.ttl -= 1;
  178. /* Verify the destination can hold the packet */
  179. new_header_size = mpls_rt_header_size(rt);
  180. mtu = mpls_dev_mtu(out_dev);
  181. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  182. goto drop;
  183. hh_len = LL_RESERVED_SPACE(out_dev);
  184. if (!out_dev->header_ops)
  185. hh_len = 0;
  186. /* Ensure there is enough space for the headers in the skb */
  187. if (skb_cow(skb, hh_len + new_header_size))
  188. goto drop;
  189. skb->dev = out_dev;
  190. skb->protocol = htons(ETH_P_MPLS_UC);
  191. if (unlikely(!new_header_size && dec.bos)) {
  192. /* Penultimate hop popping */
  193. if (!mpls_egress(rt, skb, dec))
  194. goto drop;
  195. } else {
  196. bool bos;
  197. int i;
  198. skb_push(skb, new_header_size);
  199. skb_reset_network_header(skb);
  200. /* Push the new labels */
  201. hdr = mpls_hdr(skb);
  202. bos = dec.bos;
  203. for (i = rt->rt_labels - 1; i >= 0; i--) {
  204. hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
  205. bos = false;
  206. }
  207. }
  208. err = neigh_xmit(rt->rt_via_table, out_dev, rt->rt_via, skb);
  209. if (err)
  210. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  211. __func__, err);
  212. return 0;
  213. drop:
  214. kfree_skb(skb);
  215. return NET_RX_DROP;
  216. }
  217. static struct packet_type mpls_packet_type __read_mostly = {
  218. .type = cpu_to_be16(ETH_P_MPLS_UC),
  219. .func = mpls_forward,
  220. };
  221. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  222. [RTA_DST] = { .type = NLA_U32 },
  223. [RTA_OIF] = { .type = NLA_U32 },
  224. };
  225. struct mpls_route_config {
  226. u32 rc_protocol;
  227. u32 rc_ifindex;
  228. u16 rc_via_table;
  229. u16 rc_via_alen;
  230. u8 rc_via[MAX_VIA_ALEN];
  231. u32 rc_label;
  232. u32 rc_output_labels;
  233. u32 rc_output_label[MAX_NEW_LABELS];
  234. u32 rc_nlflags;
  235. enum mpls_payload_type rc_payload_type;
  236. struct nl_info rc_nlinfo;
  237. };
  238. static struct mpls_route *mpls_rt_alloc(size_t alen)
  239. {
  240. struct mpls_route *rt;
  241. rt = kzalloc(sizeof(*rt) + alen, GFP_KERNEL);
  242. if (rt)
  243. rt->rt_via_alen = alen;
  244. return rt;
  245. }
  246. static void mpls_rt_free(struct mpls_route *rt)
  247. {
  248. if (rt)
  249. kfree_rcu(rt, rt_rcu);
  250. }
  251. static void mpls_notify_route(struct net *net, unsigned index,
  252. struct mpls_route *old, struct mpls_route *new,
  253. const struct nl_info *info)
  254. {
  255. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  256. unsigned portid = info ? info->portid : 0;
  257. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  258. struct mpls_route *rt = new ? new : old;
  259. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  260. /* Ignore reserved labels for now */
  261. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  262. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  263. }
  264. static void mpls_route_update(struct net *net, unsigned index,
  265. struct net_device *dev, struct mpls_route *new,
  266. const struct nl_info *info)
  267. {
  268. struct mpls_route __rcu **platform_label;
  269. struct mpls_route *rt, *old = NULL;
  270. ASSERT_RTNL();
  271. platform_label = rtnl_dereference(net->mpls.platform_label);
  272. rt = rtnl_dereference(platform_label[index]);
  273. if (!dev || (rt && (rtnl_dereference(rt->rt_dev) == dev))) {
  274. rcu_assign_pointer(platform_label[index], new);
  275. old = rt;
  276. }
  277. mpls_notify_route(net, index, old, new, info);
  278. /* If we removed a route free it now */
  279. mpls_rt_free(old);
  280. }
  281. static unsigned find_free_label(struct net *net)
  282. {
  283. struct mpls_route __rcu **platform_label;
  284. size_t platform_labels;
  285. unsigned index;
  286. platform_label = rtnl_dereference(net->mpls.platform_label);
  287. platform_labels = net->mpls.platform_labels;
  288. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  289. index++) {
  290. if (!rtnl_dereference(platform_label[index]))
  291. return index;
  292. }
  293. return LABEL_NOT_SPECIFIED;
  294. }
  295. #if IS_ENABLED(CONFIG_INET)
  296. static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
  297. {
  298. struct net_device *dev;
  299. struct rtable *rt;
  300. struct in_addr daddr;
  301. memcpy(&daddr, addr, sizeof(struct in_addr));
  302. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
  303. if (IS_ERR(rt))
  304. return ERR_CAST(rt);
  305. dev = rt->dst.dev;
  306. dev_hold(dev);
  307. ip_rt_put(rt);
  308. return dev;
  309. }
  310. #else
  311. static struct net_device *inet_fib_lookup_dev(struct net *net, void *addr)
  312. {
  313. return ERR_PTR(-EAFNOSUPPORT);
  314. }
  315. #endif
  316. #if IS_ENABLED(CONFIG_IPV6)
  317. static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
  318. {
  319. struct net_device *dev;
  320. struct dst_entry *dst;
  321. struct flowi6 fl6;
  322. int err;
  323. if (!ipv6_stub)
  324. return ERR_PTR(-EAFNOSUPPORT);
  325. memset(&fl6, 0, sizeof(fl6));
  326. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  327. err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
  328. if (err)
  329. return ERR_PTR(err);
  330. dev = dst->dev;
  331. dev_hold(dev);
  332. dst_release(dst);
  333. return dev;
  334. }
  335. #else
  336. static struct net_device *inet6_fib_lookup_dev(struct net *net, void *addr)
  337. {
  338. return ERR_PTR(-EAFNOSUPPORT);
  339. }
  340. #endif
  341. static struct net_device *find_outdev(struct net *net,
  342. struct mpls_route_config *cfg)
  343. {
  344. struct net_device *dev = NULL;
  345. if (!cfg->rc_ifindex) {
  346. switch (cfg->rc_via_table) {
  347. case NEIGH_ARP_TABLE:
  348. dev = inet_fib_lookup_dev(net, cfg->rc_via);
  349. break;
  350. case NEIGH_ND_TABLE:
  351. dev = inet6_fib_lookup_dev(net, cfg->rc_via);
  352. break;
  353. case NEIGH_LINK_TABLE:
  354. break;
  355. }
  356. } else {
  357. dev = dev_get_by_index(net, cfg->rc_ifindex);
  358. }
  359. if (!dev)
  360. return ERR_PTR(-ENODEV);
  361. return dev;
  362. }
  363. static int mpls_route_add(struct mpls_route_config *cfg)
  364. {
  365. struct mpls_route __rcu **platform_label;
  366. struct net *net = cfg->rc_nlinfo.nl_net;
  367. struct net_device *dev = NULL;
  368. struct mpls_route *rt, *old;
  369. unsigned index;
  370. int i;
  371. int err = -EINVAL;
  372. index = cfg->rc_label;
  373. /* If a label was not specified during insert pick one */
  374. if ((index == LABEL_NOT_SPECIFIED) &&
  375. (cfg->rc_nlflags & NLM_F_CREATE)) {
  376. index = find_free_label(net);
  377. }
  378. /* Reserved labels may not be set */
  379. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  380. goto errout;
  381. /* The full 20 bit range may not be supported. */
  382. if (index >= net->mpls.platform_labels)
  383. goto errout;
  384. /* Ensure only a supported number of labels are present */
  385. if (cfg->rc_output_labels > MAX_NEW_LABELS)
  386. goto errout;
  387. dev = find_outdev(net, cfg);
  388. if (IS_ERR(dev)) {
  389. err = PTR_ERR(dev);
  390. dev = NULL;
  391. goto errout;
  392. }
  393. /* Ensure this is a supported device */
  394. err = -EINVAL;
  395. if (!mpls_dev_get(dev))
  396. goto errout;
  397. err = -EINVAL;
  398. if ((cfg->rc_via_table == NEIGH_LINK_TABLE) &&
  399. (dev->addr_len != cfg->rc_via_alen))
  400. goto errout;
  401. /* Append makes no sense with mpls */
  402. err = -EOPNOTSUPP;
  403. if (cfg->rc_nlflags & NLM_F_APPEND)
  404. goto errout;
  405. err = -EEXIST;
  406. platform_label = rtnl_dereference(net->mpls.platform_label);
  407. old = rtnl_dereference(platform_label[index]);
  408. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  409. goto errout;
  410. err = -EEXIST;
  411. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  412. goto errout;
  413. err = -ENOENT;
  414. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  415. goto errout;
  416. err = -ENOMEM;
  417. rt = mpls_rt_alloc(cfg->rc_via_alen);
  418. if (!rt)
  419. goto errout;
  420. rt->rt_labels = cfg->rc_output_labels;
  421. for (i = 0; i < rt->rt_labels; i++)
  422. rt->rt_label[i] = cfg->rc_output_label[i];
  423. rt->rt_protocol = cfg->rc_protocol;
  424. RCU_INIT_POINTER(rt->rt_dev, dev);
  425. rt->rt_payload_type = cfg->rc_payload_type;
  426. rt->rt_via_table = cfg->rc_via_table;
  427. memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen);
  428. mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo);
  429. dev_put(dev);
  430. return 0;
  431. errout:
  432. if (dev)
  433. dev_put(dev);
  434. return err;
  435. }
  436. static int mpls_route_del(struct mpls_route_config *cfg)
  437. {
  438. struct net *net = cfg->rc_nlinfo.nl_net;
  439. unsigned index;
  440. int err = -EINVAL;
  441. index = cfg->rc_label;
  442. /* Reserved labels may not be removed */
  443. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  444. goto errout;
  445. /* The full 20 bit range may not be supported */
  446. if (index >= net->mpls.platform_labels)
  447. goto errout;
  448. mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo);
  449. err = 0;
  450. errout:
  451. return err;
  452. }
  453. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  454. (&((struct mpls_dev *)0)->field)
  455. static const struct ctl_table mpls_dev_table[] = {
  456. {
  457. .procname = "input",
  458. .maxlen = sizeof(int),
  459. .mode = 0644,
  460. .proc_handler = proc_dointvec,
  461. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  462. },
  463. { }
  464. };
  465. static int mpls_dev_sysctl_register(struct net_device *dev,
  466. struct mpls_dev *mdev)
  467. {
  468. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  469. struct ctl_table *table;
  470. int i;
  471. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  472. if (!table)
  473. goto out;
  474. /* Table data contains only offsets relative to the base of
  475. * the mdev at this point, so make them absolute.
  476. */
  477. for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
  478. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  479. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  480. mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
  481. if (!mdev->sysctl)
  482. goto free;
  483. return 0;
  484. free:
  485. kfree(table);
  486. out:
  487. return -ENOBUFS;
  488. }
  489. static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
  490. {
  491. struct ctl_table *table;
  492. table = mdev->sysctl->ctl_table_arg;
  493. unregister_net_sysctl_table(mdev->sysctl);
  494. kfree(table);
  495. }
  496. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  497. {
  498. struct mpls_dev *mdev;
  499. int err = -ENOMEM;
  500. ASSERT_RTNL();
  501. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  502. if (!mdev)
  503. return ERR_PTR(err);
  504. err = mpls_dev_sysctl_register(dev, mdev);
  505. if (err)
  506. goto free;
  507. rcu_assign_pointer(dev->mpls_ptr, mdev);
  508. return mdev;
  509. free:
  510. kfree(mdev);
  511. return ERR_PTR(err);
  512. }
  513. static void mpls_ifdown(struct net_device *dev)
  514. {
  515. struct mpls_route __rcu **platform_label;
  516. struct net *net = dev_net(dev);
  517. struct mpls_dev *mdev;
  518. unsigned index;
  519. platform_label = rtnl_dereference(net->mpls.platform_label);
  520. for (index = 0; index < net->mpls.platform_labels; index++) {
  521. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  522. if (!rt)
  523. continue;
  524. if (rtnl_dereference(rt->rt_dev) != dev)
  525. continue;
  526. rt->rt_dev = NULL;
  527. }
  528. mdev = mpls_dev_get(dev);
  529. if (!mdev)
  530. return;
  531. mpls_dev_sysctl_unregister(mdev);
  532. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  533. kfree_rcu(mdev, rcu);
  534. }
  535. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  536. void *ptr)
  537. {
  538. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  539. struct mpls_dev *mdev;
  540. switch(event) {
  541. case NETDEV_REGISTER:
  542. /* For now just support ethernet devices */
  543. if ((dev->type == ARPHRD_ETHER) ||
  544. (dev->type == ARPHRD_LOOPBACK)) {
  545. mdev = mpls_add_dev(dev);
  546. if (IS_ERR(mdev))
  547. return notifier_from_errno(PTR_ERR(mdev));
  548. }
  549. break;
  550. case NETDEV_UNREGISTER:
  551. mpls_ifdown(dev);
  552. break;
  553. case NETDEV_CHANGENAME:
  554. mdev = mpls_dev_get(dev);
  555. if (mdev) {
  556. int err;
  557. mpls_dev_sysctl_unregister(mdev);
  558. err = mpls_dev_sysctl_register(dev, mdev);
  559. if (err)
  560. return notifier_from_errno(err);
  561. }
  562. break;
  563. }
  564. return NOTIFY_OK;
  565. }
  566. static struct notifier_block mpls_dev_notifier = {
  567. .notifier_call = mpls_dev_notify,
  568. };
  569. static int nla_put_via(struct sk_buff *skb,
  570. u8 table, const void *addr, int alen)
  571. {
  572. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  573. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  574. };
  575. struct nlattr *nla;
  576. struct rtvia *via;
  577. int family = AF_UNSPEC;
  578. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  579. if (!nla)
  580. return -EMSGSIZE;
  581. if (table <= NEIGH_NR_TABLES)
  582. family = table_to_family[table];
  583. via = nla_data(nla);
  584. via->rtvia_family = family;
  585. memcpy(via->rtvia_addr, addr, alen);
  586. return 0;
  587. }
  588. int nla_put_labels(struct sk_buff *skb, int attrtype,
  589. u8 labels, const u32 label[])
  590. {
  591. struct nlattr *nla;
  592. struct mpls_shim_hdr *nla_label;
  593. bool bos;
  594. int i;
  595. nla = nla_reserve(skb, attrtype, labels*4);
  596. if (!nla)
  597. return -EMSGSIZE;
  598. nla_label = nla_data(nla);
  599. bos = true;
  600. for (i = labels - 1; i >= 0; i--) {
  601. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  602. bos = false;
  603. }
  604. return 0;
  605. }
  606. EXPORT_SYMBOL_GPL(nla_put_labels);
  607. int nla_get_labels(const struct nlattr *nla,
  608. u32 max_labels, u32 *labels, u32 label[])
  609. {
  610. unsigned len = nla_len(nla);
  611. unsigned nla_labels;
  612. struct mpls_shim_hdr *nla_label;
  613. bool bos;
  614. int i;
  615. /* len needs to be an even multiple of 4 (the label size) */
  616. if (len & 3)
  617. return -EINVAL;
  618. /* Limit the number of new labels allowed */
  619. nla_labels = len/4;
  620. if (nla_labels > max_labels)
  621. return -EINVAL;
  622. nla_label = nla_data(nla);
  623. bos = true;
  624. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  625. struct mpls_entry_decoded dec;
  626. dec = mpls_entry_decode(nla_label + i);
  627. /* Ensure the bottom of stack flag is properly set
  628. * and ttl and tc are both clear.
  629. */
  630. if ((dec.bos != bos) || dec.ttl || dec.tc)
  631. return -EINVAL;
  632. switch (dec.label) {
  633. case MPLS_LABEL_IMPLNULL:
  634. /* RFC3032: This is a label that an LSR may
  635. * assign and distribute, but which never
  636. * actually appears in the encapsulation.
  637. */
  638. return -EINVAL;
  639. }
  640. label[i] = dec.label;
  641. }
  642. *labels = nla_labels;
  643. return 0;
  644. }
  645. EXPORT_SYMBOL_GPL(nla_get_labels);
  646. static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  647. struct mpls_route_config *cfg)
  648. {
  649. struct rtmsg *rtm;
  650. struct nlattr *tb[RTA_MAX+1];
  651. int index;
  652. int err;
  653. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
  654. if (err < 0)
  655. goto errout;
  656. err = -EINVAL;
  657. rtm = nlmsg_data(nlh);
  658. memset(cfg, 0, sizeof(*cfg));
  659. if (rtm->rtm_family != AF_MPLS)
  660. goto errout;
  661. if (rtm->rtm_dst_len != 20)
  662. goto errout;
  663. if (rtm->rtm_src_len != 0)
  664. goto errout;
  665. if (rtm->rtm_tos != 0)
  666. goto errout;
  667. if (rtm->rtm_table != RT_TABLE_MAIN)
  668. goto errout;
  669. /* Any value is acceptable for rtm_protocol */
  670. /* As mpls uses destination specific addresses
  671. * (or source specific address in the case of multicast)
  672. * all addresses have universal scope.
  673. */
  674. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
  675. goto errout;
  676. if (rtm->rtm_type != RTN_UNICAST)
  677. goto errout;
  678. if (rtm->rtm_flags != 0)
  679. goto errout;
  680. cfg->rc_label = LABEL_NOT_SPECIFIED;
  681. cfg->rc_protocol = rtm->rtm_protocol;
  682. cfg->rc_nlflags = nlh->nlmsg_flags;
  683. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  684. cfg->rc_nlinfo.nlh = nlh;
  685. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  686. for (index = 0; index <= RTA_MAX; index++) {
  687. struct nlattr *nla = tb[index];
  688. if (!nla)
  689. continue;
  690. switch(index) {
  691. case RTA_OIF:
  692. cfg->rc_ifindex = nla_get_u32(nla);
  693. break;
  694. case RTA_NEWDST:
  695. if (nla_get_labels(nla, MAX_NEW_LABELS,
  696. &cfg->rc_output_labels,
  697. cfg->rc_output_label))
  698. goto errout;
  699. break;
  700. case RTA_DST:
  701. {
  702. u32 label_count;
  703. if (nla_get_labels(nla, 1, &label_count,
  704. &cfg->rc_label))
  705. goto errout;
  706. /* Reserved labels may not be set */
  707. if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
  708. goto errout;
  709. break;
  710. }
  711. case RTA_VIA:
  712. {
  713. struct rtvia *via = nla_data(nla);
  714. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
  715. goto errout;
  716. cfg->rc_via_alen = nla_len(nla) -
  717. offsetof(struct rtvia, rtvia_addr);
  718. if (cfg->rc_via_alen > MAX_VIA_ALEN)
  719. goto errout;
  720. /* Validate the address family */
  721. switch(via->rtvia_family) {
  722. case AF_PACKET:
  723. cfg->rc_via_table = NEIGH_LINK_TABLE;
  724. break;
  725. case AF_INET:
  726. cfg->rc_via_table = NEIGH_ARP_TABLE;
  727. if (cfg->rc_via_alen != 4)
  728. goto errout;
  729. break;
  730. case AF_INET6:
  731. cfg->rc_via_table = NEIGH_ND_TABLE;
  732. if (cfg->rc_via_alen != 16)
  733. goto errout;
  734. break;
  735. default:
  736. /* Unsupported address family */
  737. goto errout;
  738. }
  739. memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen);
  740. break;
  741. }
  742. default:
  743. /* Unsupported attribute */
  744. goto errout;
  745. }
  746. }
  747. err = 0;
  748. errout:
  749. return err;
  750. }
  751. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  752. {
  753. struct mpls_route_config cfg;
  754. int err;
  755. err = rtm_to_route_config(skb, nlh, &cfg);
  756. if (err < 0)
  757. return err;
  758. return mpls_route_del(&cfg);
  759. }
  760. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  761. {
  762. struct mpls_route_config cfg;
  763. int err;
  764. err = rtm_to_route_config(skb, nlh, &cfg);
  765. if (err < 0)
  766. return err;
  767. return mpls_route_add(&cfg);
  768. }
  769. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  770. u32 label, struct mpls_route *rt, int flags)
  771. {
  772. struct net_device *dev;
  773. struct nlmsghdr *nlh;
  774. struct rtmsg *rtm;
  775. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  776. if (nlh == NULL)
  777. return -EMSGSIZE;
  778. rtm = nlmsg_data(nlh);
  779. rtm->rtm_family = AF_MPLS;
  780. rtm->rtm_dst_len = 20;
  781. rtm->rtm_src_len = 0;
  782. rtm->rtm_tos = 0;
  783. rtm->rtm_table = RT_TABLE_MAIN;
  784. rtm->rtm_protocol = rt->rt_protocol;
  785. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  786. rtm->rtm_type = RTN_UNICAST;
  787. rtm->rtm_flags = 0;
  788. if (rt->rt_labels &&
  789. nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label))
  790. goto nla_put_failure;
  791. if (nla_put_via(skb, rt->rt_via_table, rt->rt_via, rt->rt_via_alen))
  792. goto nla_put_failure;
  793. dev = rtnl_dereference(rt->rt_dev);
  794. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  795. goto nla_put_failure;
  796. if (nla_put_labels(skb, RTA_DST, 1, &label))
  797. goto nla_put_failure;
  798. nlmsg_end(skb, nlh);
  799. return 0;
  800. nla_put_failure:
  801. nlmsg_cancel(skb, nlh);
  802. return -EMSGSIZE;
  803. }
  804. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  805. {
  806. struct net *net = sock_net(skb->sk);
  807. struct mpls_route __rcu **platform_label;
  808. size_t platform_labels;
  809. unsigned int index;
  810. ASSERT_RTNL();
  811. index = cb->args[0];
  812. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  813. index = MPLS_LABEL_FIRST_UNRESERVED;
  814. platform_label = rtnl_dereference(net->mpls.platform_label);
  815. platform_labels = net->mpls.platform_labels;
  816. for (; index < platform_labels; index++) {
  817. struct mpls_route *rt;
  818. rt = rtnl_dereference(platform_label[index]);
  819. if (!rt)
  820. continue;
  821. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  822. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  823. index, rt, NLM_F_MULTI) < 0)
  824. break;
  825. }
  826. cb->args[0] = index;
  827. return skb->len;
  828. }
  829. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  830. {
  831. size_t payload =
  832. NLMSG_ALIGN(sizeof(struct rtmsg))
  833. + nla_total_size(2 + rt->rt_via_alen) /* RTA_VIA */
  834. + nla_total_size(4); /* RTA_DST */
  835. if (rt->rt_labels) /* RTA_NEWDST */
  836. payload += nla_total_size(rt->rt_labels * 4);
  837. if (rt->rt_dev) /* RTA_OIF */
  838. payload += nla_total_size(4);
  839. return payload;
  840. }
  841. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  842. struct nlmsghdr *nlh, struct net *net, u32 portid,
  843. unsigned int nlm_flags)
  844. {
  845. struct sk_buff *skb;
  846. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  847. int err = -ENOBUFS;
  848. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  849. if (skb == NULL)
  850. goto errout;
  851. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  852. if (err < 0) {
  853. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  854. WARN_ON(err == -EMSGSIZE);
  855. kfree_skb(skb);
  856. goto errout;
  857. }
  858. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  859. return;
  860. errout:
  861. if (err < 0)
  862. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  863. }
  864. static int resize_platform_label_table(struct net *net, size_t limit)
  865. {
  866. size_t size = sizeof(struct mpls_route *) * limit;
  867. size_t old_limit;
  868. size_t cp_size;
  869. struct mpls_route __rcu **labels = NULL, **old;
  870. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  871. unsigned index;
  872. if (size) {
  873. labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  874. if (!labels)
  875. labels = vzalloc(size);
  876. if (!labels)
  877. goto nolabels;
  878. }
  879. /* In case the predefined labels need to be populated */
  880. if (limit > MPLS_LABEL_IPV4NULL) {
  881. struct net_device *lo = net->loopback_dev;
  882. rt0 = mpls_rt_alloc(lo->addr_len);
  883. if (!rt0)
  884. goto nort0;
  885. RCU_INIT_POINTER(rt0->rt_dev, lo);
  886. rt0->rt_protocol = RTPROT_KERNEL;
  887. rt0->rt_payload_type = MPT_IPV4;
  888. rt0->rt_via_table = NEIGH_LINK_TABLE;
  889. memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len);
  890. }
  891. if (limit > MPLS_LABEL_IPV6NULL) {
  892. struct net_device *lo = net->loopback_dev;
  893. rt2 = mpls_rt_alloc(lo->addr_len);
  894. if (!rt2)
  895. goto nort2;
  896. RCU_INIT_POINTER(rt2->rt_dev, lo);
  897. rt2->rt_protocol = RTPROT_KERNEL;
  898. rt2->rt_payload_type = MPT_IPV6;
  899. rt2->rt_via_table = NEIGH_LINK_TABLE;
  900. memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len);
  901. }
  902. rtnl_lock();
  903. /* Remember the original table */
  904. old = rtnl_dereference(net->mpls.platform_label);
  905. old_limit = net->mpls.platform_labels;
  906. /* Free any labels beyond the new table */
  907. for (index = limit; index < old_limit; index++)
  908. mpls_route_update(net, index, NULL, NULL, NULL);
  909. /* Copy over the old labels */
  910. cp_size = size;
  911. if (old_limit < limit)
  912. cp_size = old_limit * sizeof(struct mpls_route *);
  913. memcpy(labels, old, cp_size);
  914. /* If needed set the predefined labels */
  915. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  916. (limit > MPLS_LABEL_IPV6NULL)) {
  917. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  918. rt2 = NULL;
  919. }
  920. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  921. (limit > MPLS_LABEL_IPV4NULL)) {
  922. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  923. rt0 = NULL;
  924. }
  925. /* Update the global pointers */
  926. net->mpls.platform_labels = limit;
  927. rcu_assign_pointer(net->mpls.platform_label, labels);
  928. rtnl_unlock();
  929. mpls_rt_free(rt2);
  930. mpls_rt_free(rt0);
  931. if (old) {
  932. synchronize_rcu();
  933. kvfree(old);
  934. }
  935. return 0;
  936. nort2:
  937. mpls_rt_free(rt0);
  938. nort0:
  939. kvfree(labels);
  940. nolabels:
  941. return -ENOMEM;
  942. }
  943. static int mpls_platform_labels(struct ctl_table *table, int write,
  944. void __user *buffer, size_t *lenp, loff_t *ppos)
  945. {
  946. struct net *net = table->data;
  947. int platform_labels = net->mpls.platform_labels;
  948. int ret;
  949. struct ctl_table tmp = {
  950. .procname = table->procname,
  951. .data = &platform_labels,
  952. .maxlen = sizeof(int),
  953. .mode = table->mode,
  954. .extra1 = &zero,
  955. .extra2 = &label_limit,
  956. };
  957. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  958. if (write && ret == 0)
  959. ret = resize_platform_label_table(net, platform_labels);
  960. return ret;
  961. }
  962. static const struct ctl_table mpls_table[] = {
  963. {
  964. .procname = "platform_labels",
  965. .data = NULL,
  966. .maxlen = sizeof(int),
  967. .mode = 0644,
  968. .proc_handler = mpls_platform_labels,
  969. },
  970. { }
  971. };
  972. static int mpls_net_init(struct net *net)
  973. {
  974. struct ctl_table *table;
  975. net->mpls.platform_labels = 0;
  976. net->mpls.platform_label = NULL;
  977. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  978. if (table == NULL)
  979. return -ENOMEM;
  980. table[0].data = net;
  981. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  982. if (net->mpls.ctl == NULL) {
  983. kfree(table);
  984. return -ENOMEM;
  985. }
  986. return 0;
  987. }
  988. static void mpls_net_exit(struct net *net)
  989. {
  990. struct mpls_route __rcu **platform_label;
  991. size_t platform_labels;
  992. struct ctl_table *table;
  993. unsigned int index;
  994. table = net->mpls.ctl->ctl_table_arg;
  995. unregister_net_sysctl_table(net->mpls.ctl);
  996. kfree(table);
  997. /* An rcu grace period has passed since there was a device in
  998. * the network namespace (and thus the last in flight packet)
  999. * left this network namespace. This is because
  1000. * unregister_netdevice_many and netdev_run_todo has completed
  1001. * for each network device that was in this network namespace.
  1002. *
  1003. * As such no additional rcu synchronization is necessary when
  1004. * freeing the platform_label table.
  1005. */
  1006. rtnl_lock();
  1007. platform_label = rtnl_dereference(net->mpls.platform_label);
  1008. platform_labels = net->mpls.platform_labels;
  1009. for (index = 0; index < platform_labels; index++) {
  1010. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1011. RCU_INIT_POINTER(platform_label[index], NULL);
  1012. mpls_rt_free(rt);
  1013. }
  1014. rtnl_unlock();
  1015. kvfree(platform_label);
  1016. }
  1017. static struct pernet_operations mpls_net_ops = {
  1018. .init = mpls_net_init,
  1019. .exit = mpls_net_exit,
  1020. };
  1021. static int __init mpls_init(void)
  1022. {
  1023. int err;
  1024. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  1025. err = register_pernet_subsys(&mpls_net_ops);
  1026. if (err)
  1027. goto out;
  1028. err = register_netdevice_notifier(&mpls_dev_notifier);
  1029. if (err)
  1030. goto out_unregister_pernet;
  1031. dev_add_pack(&mpls_packet_type);
  1032. rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
  1033. rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
  1034. rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
  1035. err = 0;
  1036. out:
  1037. return err;
  1038. out_unregister_pernet:
  1039. unregister_pernet_subsys(&mpls_net_ops);
  1040. goto out;
  1041. }
  1042. module_init(mpls_init);
  1043. static void __exit mpls_exit(void)
  1044. {
  1045. rtnl_unregister_all(PF_MPLS);
  1046. dev_remove_pack(&mpls_packet_type);
  1047. unregister_netdevice_notifier(&mpls_dev_notifier);
  1048. unregister_pernet_subsys(&mpls_net_ops);
  1049. }
  1050. module_exit(mpls_exit);
  1051. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  1052. MODULE_LICENSE("GPL v2");
  1053. MODULE_ALIAS_NETPROTO(PF_MPLS);