af_mpls.c 34 KB

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